blob: c217e6e550fdff43b550093e0fbde77115a9aedf [file] [log] [blame]
Grace Kloba22ac16e2009-10-07 18:00:23 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.browser;
18
John Reck438bf462011-01-12 18:11:46 -080019import com.android.browser.homepages.HomeProvider;
Jeff Hamilton8ce956c2010-08-17 11:13:53 -050020import com.android.common.speech.LoggingEvents;
21
Michael Kolb8233fac2010-10-26 16:08:53 -070022import android.app.Activity;
Grace Kloba22ac16e2009-10-07 18:00:23 -070023import android.app.AlertDialog;
Leon Scroggins58d56c62010-01-28 15:12:40 -050024import android.app.SearchManager;
Grace Kloba22ac16e2009-10-07 18:00:23 -070025import android.content.ContentResolver;
John Reck30c714c2010-12-16 17:30:34 -080026import android.content.Context;
Grace Kloba22ac16e2009-10-07 18:00:23 -070027import android.content.DialogInterface;
Michael Kolbfe251992010-07-08 15:41:55 -070028import android.content.DialogInterface.OnCancelListener;
Jeff Hamilton8ce956c2010-08-17 11:13:53 -050029import android.content.Intent;
Grace Kloba22ac16e2009-10-07 18:00:23 -070030import android.graphics.Bitmap;
John Reck30c714c2010-12-16 17:30:34 -080031import android.graphics.BitmapFactory;
Grace Kloba22ac16e2009-10-07 18:00:23 -070032import android.net.Uri;
33import android.net.http.SslError;
Grace Kloba22ac16e2009-10-07 18:00:23 -070034import android.os.Bundle;
35import android.os.Message;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +000036import android.os.SystemClock;
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -050037import android.speech.RecognizerResultsIntent;
Grace Kloba22ac16e2009-10-07 18:00:23 -070038import android.util.Log;
39import android.view.KeyEvent;
40import android.view.LayoutInflater;
41import android.view.View;
Grace Kloba50c241e2010-04-20 11:07:50 -070042import android.view.ViewStub;
Ben Murdochc42addf2010-01-28 15:19:59 +000043import android.webkit.ConsoleMessage;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -050044import android.webkit.DownloadListener;
Grace Kloba22ac16e2009-10-07 18:00:23 -070045import android.webkit.GeolocationPermissions;
46import android.webkit.HttpAuthHandler;
47import android.webkit.SslErrorHandler;
48import android.webkit.URLUtil;
49import android.webkit.ValueCallback;
50import android.webkit.WebBackForwardList;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -050051import android.webkit.WebBackForwardListClient;
Grace Kloba22ac16e2009-10-07 18:00:23 -070052import android.webkit.WebChromeClient;
53import android.webkit.WebHistoryItem;
John Reck438bf462011-01-12 18:11:46 -080054import android.webkit.WebResourceResponse;
Grace Kloba22ac16e2009-10-07 18:00:23 -070055import android.webkit.WebStorage;
56import android.webkit.WebView;
57import android.webkit.WebViewClient;
Ben Murdoch1d676b62011-01-17 12:54:24 +000058import android.widget.CheckBox;
Grace Kloba22ac16e2009-10-07 18:00:23 -070059import android.widget.LinearLayout;
60import android.widget.TextView;
Ben Murdoch8029a772010-11-16 11:58:21 +000061import android.widget.Toast;
Grace Kloba22ac16e2009-10-07 18:00:23 -070062
Michael Kolbfe251992010-07-08 15:41:55 -070063import java.util.ArrayList;
64import java.util.HashMap;
65import java.util.Iterator;
66import java.util.LinkedList;
67import java.util.Map;
68import java.util.Vector;
69
Grace Kloba22ac16e2009-10-07 18:00:23 -070070/**
71 * Class for maintaining Tabs with a main WebView and a subwindow.
72 */
73class Tab {
Michael Kolb8233fac2010-10-26 16:08:53 -070074
Grace Kloba22ac16e2009-10-07 18:00:23 -070075 // Log Tag
76 private static final String LOGTAG = "Tab";
Ben Murdochc42addf2010-01-28 15:19:59 +000077 // Special case the logtag for messages for the Console to make it easier to
78 // filter them and match the logtag used for these messages in older versions
79 // of the browser.
80 private static final String CONSOLE_LOGTAG = "browser";
81
John Reck30c714c2010-12-16 17:30:34 -080082 public enum LockIcon {
83 LOCK_ICON_UNSECURE,
84 LOCK_ICON_SECURE,
85 LOCK_ICON_MIXED,
86 }
Michael Kolb8233fac2010-10-26 16:08:53 -070087
88 Activity mActivity;
89 private WebViewController mWebViewController;
90
Grace Kloba22ac16e2009-10-07 18:00:23 -070091 // The Geolocation permissions prompt
92 private GeolocationPermissionsPrompt mGeolocationPermissionsPrompt;
93 // Main WebView wrapper
Michael Kolba713ec82010-11-29 17:27:06 -080094 private View mContainer;
Grace Kloba22ac16e2009-10-07 18:00:23 -070095 // Main WebView
96 private WebView mMainView;
97 // Subwindow container
98 private View mSubViewContainer;
99 // Subwindow WebView
100 private WebView mSubView;
101 // Saved bundle for when we are running low on memory. It contains the
102 // information needed to restore the WebView if the user goes back to the
103 // tab.
104 private Bundle mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700105 // Parent Tab. This is the Tab that created this Tab, or null if the Tab was
106 // created by the UI
107 private Tab mParentTab;
108 // Tab that constructed by this Tab. This is used when this Tab is
109 // destroyed, it clears all mParentTab values in the children.
110 private Vector<Tab> mChildTabs;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700111 // If true, the tab is in the foreground of the current activity.
112 private boolean mInForeground;
Michael Kolb8233fac2010-10-26 16:08:53 -0700113 // If true, the tab is in page loading state (after onPageStarted,
114 // before onPageFinsihed)
115 private boolean mInPageLoad;
John Reck30c714c2010-12-16 17:30:34 -0800116 // The last reported progress of the current page
117 private int mPageLoadProgress;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000118 // The time the load started, used to find load page time
119 private long mLoadStartTime;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700120 // Application identifier used to find tabs that another application wants
121 // to reuse.
122 private String mAppId;
123 // Keep the original url around to avoid killing the old WebView if the url
124 // has not changed.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700125 // Error console for the tab
126 private ErrorConsoleView mErrorConsole;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -0500127 // The listener that gets invoked when a download is started from the
128 // mMainView
129 private final DownloadListener mDownloadListener;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500130 // Listener used to know when we move forward or back in the history list.
131 private final WebBackForwardListClient mWebBackForwardListClient;
John Recke969cc52010-12-21 17:24:43 -0800132 private DataController mDataController;
Patrick Scott92066772011-03-10 08:46:27 -0500133 // State of the auto-login request.
134 private DeviceAccountLogin mDeviceAccountLogin;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700135
136 // AsyncTask for downloading touch icons
137 DownloadTouchIcon mTouchIconLoader;
138
Michael Kolbeb95db42011-03-03 10:38:40 -0800139 private Bitmap mScreenshot;
John Reck35e9dd62011-04-25 09:01:54 -0700140 private BrowserSettings mSettings;
Michael Kolbeb95db42011-03-03 10:38:40 -0800141
John Reck30c714c2010-12-16 17:30:34 -0800142 // All the state needed for a page
143 private static class PageState {
144 String mUrl;
145 String mTitle;
146 LockIcon mLockIcon;
147 Bitmap mFavicon;
John Recke969cc52010-12-21 17:24:43 -0800148 Boolean mIsBookmarkedSite = false;
John Reck30c714c2010-12-16 17:30:34 -0800149
150 PageState(Context c, boolean incognito) {
151 if (incognito) {
152 mUrl = "browser:incognito";
153 mTitle = c.getString(R.string.new_incognito_tab);
John Reck30c714c2010-12-16 17:30:34 -0800154 } else {
155 mUrl = "";
156 mTitle = c.getString(R.string.new_tab);
John Reck30c714c2010-12-16 17:30:34 -0800157 }
Justin Hodc6cbb72011-01-24 15:14:54 -0800158 mFavicon = BitmapFactory.decodeResource(
159 c.getResources(), R.drawable.app_web_browser_sm);
John Reck30c714c2010-12-16 17:30:34 -0800160 mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
161 }
162
163 PageState(Context c, boolean incognito, String url, Bitmap favicon) {
164 mUrl = url;
165 mTitle = null;
166 if (URLUtil.isHttpsUrl(url)) {
167 mLockIcon = LockIcon.LOCK_ICON_SECURE;
168 } else {
169 mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
170 }
171 if (favicon != null) {
172 mFavicon = favicon;
173 } else {
Justin Hodc6cbb72011-01-24 15:14:54 -0800174 mFavicon = BitmapFactory.decodeResource(
175 c.getResources(), R.drawable.app_web_browser_sm);
John Reck30c714c2010-12-16 17:30:34 -0800176 }
177 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700178 }
179
John Reck30c714c2010-12-16 17:30:34 -0800180 // The current/loading page's state
181 private PageState mCurrentState;
182
Grace Kloba22ac16e2009-10-07 18:00:23 -0700183 // Used for saving and restoring each Tab
John Reck30c714c2010-12-16 17:30:34 -0800184 // TODO: Figure out who uses what and where
185 // Some of these aren't use in this class, and some are only used in
186 // restoring state but not saving it - FIX THIS
Grace Kloba22ac16e2009-10-07 18:00:23 -0700187 static final String WEBVIEW = "webview";
188 static final String NUMTABS = "numTabs";
189 static final String CURRTAB = "currentTab";
190 static final String CURRURL = "currentUrl";
191 static final String CURRTITLE = "currentTitle";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700192 static final String PARENTTAB = "parentTab";
193 static final String APPID = "appid";
194 static final String ORIGINALURL = "originalUrl";
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700195 static final String INCOGNITO = "privateBrowsingEnabled";
Michael Kolbeb95db42011-03-03 10:38:40 -0800196 static final String SCREENSHOT = "screenshot";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700197
198 // -------------------------------------------------------------------------
199
Leon Scroggins58d56c62010-01-28 15:12:40 -0500200 /**
201 * Private information regarding the latest voice search. If the Tab is not
202 * in voice search mode, this will be null.
203 */
204 private VoiceSearchData mVoiceSearchData;
205 /**
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400206 * Remove voice search mode from this tab.
207 */
208 public void revertVoiceSearchMode() {
209 if (mVoiceSearchData != null) {
210 mVoiceSearchData = null;
211 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700212 mWebViewController.revertVoiceSearchMode(this);
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400213 }
214 }
215 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700216
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400217 /**
Leon Scroggins58d56c62010-01-28 15:12:40 -0500218 * Return whether the tab is in voice search mode.
219 */
220 public boolean isInVoiceSearchMode() {
221 return mVoiceSearchData != null;
222 }
223 /**
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400224 * Return true if the Tab is in voice search mode and the voice search
225 * Intent came with a String identifying that Google provided the Intent.
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500226 */
227 public boolean voiceSearchSourceIsGoogle() {
228 return mVoiceSearchData != null && mVoiceSearchData.mSourceIsGoogle;
229 }
230 /**
Leon Scroggins58d56c62010-01-28 15:12:40 -0500231 * Get the title to display for the current voice search page. If the Tab
232 * is not in voice search mode, return null.
233 */
234 public String getVoiceDisplayTitle() {
235 if (mVoiceSearchData == null) return null;
236 return mVoiceSearchData.mLastVoiceSearchTitle;
237 }
238 /**
239 * Get the latest array of voice search results, to be passed to the
240 * BrowserProvider. If the Tab is not in voice search mode, return null.
241 */
242 public ArrayList<String> getVoiceSearchResults() {
243 if (mVoiceSearchData == null) return null;
244 return mVoiceSearchData.mVoiceSearchResults;
245 }
246 /**
247 * Activate voice search mode.
248 * @param intent Intent which has the results to use, or an index into the
249 * results when reusing the old results.
250 */
251 /* package */ void activateVoiceSearchMode(Intent intent) {
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500252 int index = 0;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500253 ArrayList<String> results = intent.getStringArrayListExtra(
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -0500254 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_STRINGS);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500255 if (results != null) {
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500256 ArrayList<String> urls = intent.getStringArrayListExtra(
257 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_URLS);
258 ArrayList<String> htmls = intent.getStringArrayListExtra(
259 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_HTML);
260 ArrayList<String> baseUrls = intent.getStringArrayListExtra(
261 RecognizerResultsIntent
262 .EXTRA_VOICE_SEARCH_RESULT_HTML_BASE_URLS);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500263 // This tab is now entering voice search mode for the first time, or
264 // a new voice search was done.
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500265 int size = results.size();
266 if (urls == null || size != urls.size()) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500267 throw new AssertionError("improper extras passed in Intent");
268 }
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500269 if (htmls == null || htmls.size() != size || baseUrls == null ||
270 (baseUrls.size() != size && baseUrls.size() != 1)) {
271 // If either of these arrays are empty/incorrectly sized, ignore
272 // them.
273 htmls = null;
274 baseUrls = null;
275 }
276 mVoiceSearchData = new VoiceSearchData(results, urls, htmls,
277 baseUrls);
Leon Scroggins9df94972010-03-08 18:20:35 -0500278 mVoiceSearchData.mHeaders = intent.getParcelableArrayListExtra(
279 RecognizerResultsIntent
280 .EXTRA_VOICE_SEARCH_RESULT_HTTP_HEADERS);
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500281 mVoiceSearchData.mSourceIsGoogle = intent.getBooleanExtra(
282 VoiceSearchData.SOURCE_IS_GOOGLE, false);
Leon Scroggins2ee4a5a2010-03-15 16:56:57 -0400283 mVoiceSearchData.mVoiceSearchIntent = new Intent(intent);
Leon Scrogginse10dde52010-03-08 19:53:03 -0500284 }
285 String extraData = intent.getStringExtra(
286 SearchManager.EXTRA_DATA_KEY);
287 if (extraData != null) {
288 index = Integer.parseInt(extraData);
289 if (index >= mVoiceSearchData.mVoiceSearchResults.size()) {
290 throw new AssertionError("index must be less than "
291 + "size of mVoiceSearchResults");
292 }
293 if (mVoiceSearchData.mSourceIsGoogle) {
294 Intent logIntent = new Intent(
295 LoggingEvents.ACTION_LOG_EVENT);
296 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
297 LoggingEvents.VoiceSearch.N_BEST_CHOOSE);
298 logIntent.putExtra(
299 LoggingEvents.VoiceSearch.EXTRA_N_BEST_CHOOSE_INDEX,
300 index);
301 mActivity.sendBroadcast(logIntent);
302 }
303 if (mVoiceSearchData.mVoiceSearchIntent != null) {
Leon Scroggins2ee4a5a2010-03-15 16:56:57 -0400304 // Copy the Intent, so that each history item will have its own
305 // Intent, with different (or none) extra data.
306 Intent latest = new Intent(mVoiceSearchData.mVoiceSearchIntent);
307 latest.putExtra(SearchManager.EXTRA_DATA_KEY, extraData);
308 mVoiceSearchData.mVoiceSearchIntent = latest;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500309 }
310 }
311 mVoiceSearchData.mLastVoiceSearchTitle
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500312 = mVoiceSearchData.mVoiceSearchResults.get(index);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500313 if (mInForeground) {
Michael Kolb11d19782011-03-20 10:17:40 -0700314 mWebViewController.activateVoiceSearchMode(
315 mVoiceSearchData.mLastVoiceSearchTitle,
316 mVoiceSearchData.mVoiceSearchResults);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500317 }
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500318 if (mVoiceSearchData.mVoiceSearchHtmls != null) {
319 // When index was found it was already ensured that it was valid
320 String uriString = mVoiceSearchData.mVoiceSearchHtmls.get(index);
321 if (uriString != null) {
322 Uri dataUri = Uri.parse(uriString);
323 if (RecognizerResultsIntent.URI_SCHEME_INLINE.equals(
324 dataUri.getScheme())) {
325 // If there is only one base URL, use it. If there are
326 // more, there will be one for each index, so use the base
327 // URL corresponding to the index.
328 String baseUrl = mVoiceSearchData.mVoiceSearchBaseUrls.get(
329 mVoiceSearchData.mVoiceSearchBaseUrls.size() > 1 ?
330 index : 0);
331 mVoiceSearchData.mLastVoiceSearchUrl = baseUrl;
332 mMainView.loadDataWithBaseURL(baseUrl,
333 uriString.substring(RecognizerResultsIntent
334 .URI_SCHEME_INLINE.length() + 1), "text/html",
335 "utf-8", baseUrl);
336 return;
337 }
338 }
339 }
Leon Scroggins58d56c62010-01-28 15:12:40 -0500340 mVoiceSearchData.mLastVoiceSearchUrl
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500341 = mVoiceSearchData.mVoiceSearchUrls.get(index);
342 if (null == mVoiceSearchData.mLastVoiceSearchUrl) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700343 mVoiceSearchData.mLastVoiceSearchUrl = UrlUtils.smartUrlFilter(
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500344 mVoiceSearchData.mLastVoiceSearchTitle);
345 }
Leon Scroggins9df94972010-03-08 18:20:35 -0500346 Map<String, String> headers = null;
347 if (mVoiceSearchData.mHeaders != null) {
348 int bundleIndex = mVoiceSearchData.mHeaders.size() == 1 ? 0
349 : index;
350 Bundle bundle = mVoiceSearchData.mHeaders.get(bundleIndex);
351 if (bundle != null && !bundle.isEmpty()) {
352 Iterator<String> iter = bundle.keySet().iterator();
353 headers = new HashMap<String, String>();
354 while (iter.hasNext()) {
355 String key = iter.next();
356 headers.put(key, bundle.getString(key));
357 }
358 }
359 }
360 mMainView.loadUrl(mVoiceSearchData.mLastVoiceSearchUrl, headers);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500361 }
362 /* package */ static class VoiceSearchData {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500363 public VoiceSearchData(ArrayList<String> results,
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500364 ArrayList<String> urls, ArrayList<String> htmls,
365 ArrayList<String> baseUrls) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500366 mVoiceSearchResults = results;
367 mVoiceSearchUrls = urls;
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500368 mVoiceSearchHtmls = htmls;
369 mVoiceSearchBaseUrls = baseUrls;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500370 }
371 /*
372 * ArrayList of suggestions to be displayed when opening the
373 * SearchManager
374 */
375 public ArrayList<String> mVoiceSearchResults;
376 /*
377 * ArrayList of urls, associated with the suggestions in
378 * mVoiceSearchResults.
379 */
380 public ArrayList<String> mVoiceSearchUrls;
381 /*
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500382 * ArrayList holding content to load for each item in
383 * mVoiceSearchResults.
384 */
385 public ArrayList<String> mVoiceSearchHtmls;
386 /*
387 * ArrayList holding base urls for the items in mVoiceSearchResults.
388 * If non null, this will either have the same size as
389 * mVoiceSearchResults or have a size of 1, in which case all will use
390 * the same base url
391 */
392 public ArrayList<String> mVoiceSearchBaseUrls;
393 /*
Leon Scroggins58d56c62010-01-28 15:12:40 -0500394 * The last url provided by voice search. Used for comparison to see if
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500395 * we are going to a page by some method besides voice search.
Leon Scroggins58d56c62010-01-28 15:12:40 -0500396 */
397 public String mLastVoiceSearchUrl;
398 /**
399 * The last title used for voice search. Needed to update the title bar
400 * when switching tabs.
401 */
402 public String mLastVoiceSearchTitle;
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500403 /**
404 * Whether the Intent which turned on voice search mode contained the
405 * String signifying that Google was the source.
406 */
407 public boolean mSourceIsGoogle;
408 /**
Leon Scroggins9df94972010-03-08 18:20:35 -0500409 * List of headers to be passed into the WebView containing location
410 * information
411 */
412 public ArrayList<Bundle> mHeaders;
413 /**
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500414 * The Intent used to invoke voice search. Placed on the
415 * WebHistoryItem so that when coming back to a previous voice search
416 * page we can again activate voice search.
417 */
Leon Scrogginse10dde52010-03-08 19:53:03 -0500418 public Intent mVoiceSearchIntent;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500419 /**
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500420 * String used to identify Google as the source of voice search.
421 */
422 public static String SOURCE_IS_GOOGLE
423 = "android.speech.extras.SOURCE_IS_GOOGLE";
Leon Scroggins58d56c62010-01-28 15:12:40 -0500424 }
425
Grace Kloba22ac16e2009-10-07 18:00:23 -0700426 // Container class for the next error dialog that needs to be displayed
427 private class ErrorDialog {
428 public final int mTitle;
429 public final String mDescription;
430 public final int mError;
431 ErrorDialog(int title, String desc, int error) {
432 mTitle = title;
433 mDescription = desc;
434 mError = error;
435 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700436 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700437
438 private void processNextError() {
439 if (mQueuedErrors == null) {
440 return;
441 }
442 // The first one is currently displayed so just remove it.
443 mQueuedErrors.removeFirst();
444 if (mQueuedErrors.size() == 0) {
445 mQueuedErrors = null;
446 return;
447 }
448 showError(mQueuedErrors.getFirst());
449 }
450
451 private DialogInterface.OnDismissListener mDialogListener =
452 new DialogInterface.OnDismissListener() {
453 public void onDismiss(DialogInterface d) {
454 processNextError();
455 }
456 };
457 private LinkedList<ErrorDialog> mQueuedErrors;
458
459 private void queueError(int err, String desc) {
460 if (mQueuedErrors == null) {
461 mQueuedErrors = new LinkedList<ErrorDialog>();
462 }
463 for (ErrorDialog d : mQueuedErrors) {
464 if (d.mError == err) {
465 // Already saw a similar error, ignore the new one.
466 return;
467 }
468 }
469 ErrorDialog errDialog = new ErrorDialog(
470 err == WebViewClient.ERROR_FILE_NOT_FOUND ?
471 R.string.browserFrameFileErrorLabel :
472 R.string.browserFrameNetworkErrorLabel,
473 desc, err);
474 mQueuedErrors.addLast(errDialog);
475
476 // Show the dialog now if the queue was empty and it is in foreground
477 if (mQueuedErrors.size() == 1 && mInForeground) {
478 showError(errDialog);
479 }
480 }
481
482 private void showError(ErrorDialog errDialog) {
483 if (mInForeground) {
484 AlertDialog d = new AlertDialog.Builder(mActivity)
485 .setTitle(errDialog.mTitle)
486 .setMessage(errDialog.mDescription)
487 .setPositiveButton(R.string.ok, null)
488 .create();
489 d.setOnDismissListener(mDialogListener);
490 d.show();
491 }
492 }
493
494 // -------------------------------------------------------------------------
495 // WebViewClient implementation for the main WebView
496 // -------------------------------------------------------------------------
497
498 private final WebViewClient mWebViewClient = new WebViewClient() {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500499 private Message mDontResend;
500 private Message mResend;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700501 @Override
502 public void onPageStarted(WebView view, String url, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700503 mInPageLoad = true;
John Reck30c714c2010-12-16 17:30:34 -0800504 mPageLoadProgress = 0;
505 mCurrentState = new PageState(mActivity,
506 view.isPrivateBrowsingEnabled(), url, favicon);
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000507 mLoadStartTime = SystemClock.uptimeMillis();
Leon Scroggins58d56c62010-01-28 15:12:40 -0500508 if (mVoiceSearchData != null
509 && !url.equals(mVoiceSearchData.mLastVoiceSearchUrl)) {
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500510 if (mVoiceSearchData.mSourceIsGoogle) {
511 Intent i = new Intent(LoggingEvents.ACTION_LOG_EVENT);
512 i.putExtra(LoggingEvents.EXTRA_FLUSH, true);
513 mActivity.sendBroadcast(i);
514 }
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400515 revertVoiceSearchMode();
Leon Scroggins58d56c62010-01-28 15:12:40 -0500516 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700517
Grace Kloba22ac16e2009-10-07 18:00:23 -0700518
519 // If we start a touch icon load and then load a new page, we don't
520 // want to cancel the current touch icon loader. But, we do want to
521 // create a new one when the touch icon url is known.
522 if (mTouchIconLoader != null) {
523 mTouchIconLoader.mTab = null;
524 mTouchIconLoader = null;
525 }
526
527 // reset the error console
528 if (mErrorConsole != null) {
529 mErrorConsole.clearErrorMessages();
Michael Kolb8233fac2010-10-26 16:08:53 -0700530 if (mWebViewController.shouldShowErrorConsole()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700531 mErrorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
532 }
533 }
534
Patrick Scott92066772011-03-10 08:46:27 -0500535 // Cancel the auto-login process.
536 if (mDeviceAccountLogin != null) {
537 mDeviceAccountLogin.cancel();
538 mDeviceAccountLogin = null;
539 mWebViewController.hideAutoLogin(Tab.this);
540 }
541
Grace Kloba22ac16e2009-10-07 18:00:23 -0700542 // finally update the UI in the activity if it is in the foreground
John Reck324d4402011-01-11 16:56:42 -0800543 mWebViewController.onPageStarted(Tab.this, view, favicon);
Leon Scroggins4cd97792010-12-03 15:31:56 -0500544
John Recke969cc52010-12-21 17:24:43 -0800545 updateBookmarkedStatus();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700546 }
547
548 @Override
549 public void onPageFinished(WebView view, String url) {
John Reck5b691842010-11-29 11:21:13 -0800550 if (!isPrivateBrowsingEnabled()) {
551 LogTag.logPageFinishedLoading(
552 url, SystemClock.uptimeMillis() - mLoadStartTime);
553 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700554 mInPageLoad = false;
John Reck30c714c2010-12-16 17:30:34 -0800555 // Sync state (in case of stop/timeout)
556 mCurrentState.mUrl = view.getUrl();
John Reck6c702ee2011-01-07 09:41:53 -0800557 if (mCurrentState.mUrl == null) {
558 mCurrentState.mUrl = url != null ? url : "";
559 }
John Reck30c714c2010-12-16 17:30:34 -0800560 mCurrentState.mTitle = view.getTitle();
561 mCurrentState.mFavicon = view.getFavicon();
562 if (!URLUtil.isHttpsUrl(mCurrentState.mUrl)) {
563 // In case we stop when loading an HTTPS page from an HTTP page
564 // but before a provisional load occurred
565 mCurrentState.mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
566 }
John Reck324d4402011-01-11 16:56:42 -0800567 mWebViewController.onPageFinished(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700568 }
569
570 // return true if want to hijack the url to let another app to handle it
571 @Override
572 public boolean shouldOverrideUrlLoading(WebView view, String url) {
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400573 if (voiceSearchSourceIsGoogle()) {
574 // This method is called when the user clicks on a link.
575 // VoiceSearchMode is turned off when the user leaves the
576 // Google results page, so at this point the user must be on
577 // that page. If the user clicked a link on that page, assume
578 // that the voice search was effective, and broadcast an Intent
579 // so a receiver can take note of that fact.
580 Intent logIntent = new Intent(LoggingEvents.ACTION_LOG_EVENT);
581 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
582 LoggingEvents.VoiceSearch.RESULT_CLICKED);
583 mActivity.sendBroadcast(logIntent);
584 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700585 if (mInForeground) {
Michael Kolb18eb3772010-12-10 14:29:51 -0800586 return mWebViewController.shouldOverrideUrlLoading(Tab.this,
587 view, url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700588 } else {
589 return false;
590 }
591 }
592
593 /**
594 * Updates the lock icon. This method is called when we discover another
595 * resource to be loaded for this page (for example, javascript). While
596 * we update the icon type, we do not update the lock icon itself until
597 * we are done loading, it is slightly more secure this way.
598 */
599 @Override
600 public void onLoadResource(WebView view, String url) {
601 if (url != null && url.length() > 0) {
602 // It is only if the page claims to be secure that we may have
603 // to update the lock:
John Reck30c714c2010-12-16 17:30:34 -0800604 if (mCurrentState.mLockIcon == LockIcon.LOCK_ICON_SECURE) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700605 // If NOT a 'safe' url, change the lock to mixed content!
606 if (!(URLUtil.isHttpsUrl(url) || URLUtil.isDataUrl(url)
607 || URLUtil.isAboutUrl(url))) {
John Reck30c714c2010-12-16 17:30:34 -0800608 mCurrentState.mLockIcon = LockIcon.LOCK_ICON_MIXED;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700609 }
610 }
611 }
612 }
613
614 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700615 * Show a dialog informing the user of the network error reported by
616 * WebCore if it is in the foreground.
617 */
618 @Override
619 public void onReceivedError(WebView view, int errorCode,
620 String description, String failingUrl) {
621 if (errorCode != WebViewClient.ERROR_HOST_LOOKUP &&
622 errorCode != WebViewClient.ERROR_CONNECT &&
623 errorCode != WebViewClient.ERROR_BAD_URL &&
624 errorCode != WebViewClient.ERROR_UNSUPPORTED_SCHEME &&
625 errorCode != WebViewClient.ERROR_FILE) {
626 queueError(errorCode, description);
627 }
Jeff Hamilton47654f42010-09-07 09:57:51 -0500628
629 // Don't log URLs when in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -0700630 if (!isPrivateBrowsingEnabled()) {
Jeff Hamilton47654f42010-09-07 09:57:51 -0500631 Log.e(LOGTAG, "onReceivedError " + errorCode + " " + failingUrl
632 + " " + description);
633 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700634 }
635
636 /**
637 * Check with the user if it is ok to resend POST data as the page they
638 * are trying to navigate to is the result of a POST.
639 */
640 @Override
641 public void onFormResubmission(WebView view, final Message dontResend,
642 final Message resend) {
643 if (!mInForeground) {
644 dontResend.sendToTarget();
645 return;
646 }
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500647 if (mDontResend != null) {
648 Log.w(LOGTAG, "onFormResubmission should not be called again "
649 + "while dialog is still up");
650 dontResend.sendToTarget();
651 return;
652 }
653 mDontResend = dontResend;
654 mResend = resend;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700655 new AlertDialog.Builder(mActivity).setTitle(
656 R.string.browserFrameFormResubmitLabel).setMessage(
657 R.string.browserFrameFormResubmitMessage)
658 .setPositiveButton(R.string.ok,
659 new DialogInterface.OnClickListener() {
660 public void onClick(DialogInterface dialog,
661 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500662 if (mResend != null) {
663 mResend.sendToTarget();
664 mResend = null;
665 mDontResend = null;
666 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700667 }
668 }).setNegativeButton(R.string.cancel,
669 new DialogInterface.OnClickListener() {
670 public void onClick(DialogInterface dialog,
671 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500672 if (mDontResend != null) {
673 mDontResend.sendToTarget();
674 mResend = null;
675 mDontResend = null;
676 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700677 }
678 }).setOnCancelListener(new OnCancelListener() {
679 public void onCancel(DialogInterface dialog) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500680 if (mDontResend != null) {
681 mDontResend.sendToTarget();
682 mResend = null;
683 mDontResend = null;
684 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700685 }
686 }).show();
687 }
688
689 /**
690 * Insert the url into the visited history database.
691 * @param url The url to be inserted.
692 * @param isReload True if this url is being reloaded.
693 * FIXME: Not sure what to do when reloading the page.
694 */
695 @Override
696 public void doUpdateVisitedHistory(WebView view, String url,
697 boolean isReload) {
John Reck324d4402011-01-11 16:56:42 -0800698 mWebViewController.doUpdateVisitedHistory(Tab.this, isReload);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700699 }
700
701 /**
702 * Displays SSL error(s) dialog to the user.
703 */
704 @Override
705 public void onReceivedSslError(final WebView view,
706 final SslErrorHandler handler, final SslError error) {
707 if (!mInForeground) {
708 handler.cancel();
John Reck30c714c2010-12-16 17:30:34 -0800709 setLockIconType(LockIcon.LOCK_ICON_UNSECURE);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700710 return;
711 }
John Reck35e9dd62011-04-25 09:01:54 -0700712 if (mSettings.showSecurityWarnings()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700713 final LayoutInflater factory =
714 LayoutInflater.from(mActivity);
715 final View warningsView =
716 factory.inflate(R.layout.ssl_warnings, null);
717 final LinearLayout placeholder =
718 (LinearLayout)warningsView.findViewById(R.id.placeholder);
719
720 if (error.hasError(SslError.SSL_UNTRUSTED)) {
721 LinearLayout ll = (LinearLayout)factory
722 .inflate(R.layout.ssl_warning, null);
723 ((TextView)ll.findViewById(R.id.warning))
724 .setText(R.string.ssl_untrusted);
725 placeholder.addView(ll);
726 }
727
728 if (error.hasError(SslError.SSL_IDMISMATCH)) {
729 LinearLayout ll = (LinearLayout)factory
730 .inflate(R.layout.ssl_warning, null);
731 ((TextView)ll.findViewById(R.id.warning))
732 .setText(R.string.ssl_mismatch);
733 placeholder.addView(ll);
734 }
735
736 if (error.hasError(SslError.SSL_EXPIRED)) {
737 LinearLayout ll = (LinearLayout)factory
738 .inflate(R.layout.ssl_warning, null);
739 ((TextView)ll.findViewById(R.id.warning))
740 .setText(R.string.ssl_expired);
741 placeholder.addView(ll);
742 }
743
744 if (error.hasError(SslError.SSL_NOTYETVALID)) {
745 LinearLayout ll = (LinearLayout)factory
746 .inflate(R.layout.ssl_warning, null);
747 ((TextView)ll.findViewById(R.id.warning))
748 .setText(R.string.ssl_not_yet_valid);
749 placeholder.addView(ll);
750 }
751
752 new AlertDialog.Builder(mActivity).setTitle(
753 R.string.security_warning).setIcon(
754 android.R.drawable.ic_dialog_alert).setView(
755 warningsView).setPositiveButton(R.string.ssl_continue,
756 new DialogInterface.OnClickListener() {
757 public void onClick(DialogInterface dialog,
758 int whichButton) {
759 handler.proceed();
760 }
761 }).setNeutralButton(R.string.view_certificate,
762 new DialogInterface.OnClickListener() {
763 public void onClick(DialogInterface dialog,
764 int whichButton) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700765 mWebViewController.showSslCertificateOnError(view,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700766 handler, error);
767 }
Ben Murdocha49b8292010-11-16 11:56:04 +0000768 }).setNegativeButton(R.string.ssl_go_back,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700769 new DialogInterface.OnClickListener() {
770 public void onClick(DialogInterface dialog,
771 int whichButton) {
John Reck30c714c2010-12-16 17:30:34 -0800772 dialog.cancel();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700773 }
774 }).setOnCancelListener(
775 new DialogInterface.OnCancelListener() {
776 public void onCancel(DialogInterface dialog) {
777 handler.cancel();
John Reck30c714c2010-12-16 17:30:34 -0800778 setLockIconType(LockIcon.LOCK_ICON_UNSECURE);
779 mWebViewController.onUserCanceledSsl(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700780 }
781 }).show();
782 } else {
783 handler.proceed();
784 }
785 }
786
787 /**
788 * Handles an HTTP authentication request.
789 *
790 * @param handler The authentication handler
791 * @param host The host
792 * @param realm The realm
793 */
794 @Override
795 public void onReceivedHttpAuthRequest(WebView view,
796 final HttpAuthHandler handler, final String host,
797 final String realm) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700798 mWebViewController.onReceivedHttpAuthRequest(Tab.this, view, handler, host, realm);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700799 }
800
801 @Override
John Reck438bf462011-01-12 18:11:46 -0800802 public WebResourceResponse shouldInterceptRequest(WebView view,
803 String url) {
804 WebResourceResponse res = HomeProvider.shouldInterceptRequest(
805 mActivity, url);
806 return res;
807 }
808
809 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700810 public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
811 if (!mInForeground) {
812 return false;
813 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700814 return mWebViewController.shouldOverrideKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700815 }
816
817 @Override
818 public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700819 if (!mInForeground) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700820 return;
821 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700822 mWebViewController.onUnhandledKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700823 }
Patrick Scott92066772011-03-10 08:46:27 -0500824
825 @Override
826 public void onReceivedLoginRequest(WebView view, String realm,
827 String account, String args) {
828 new DeviceAccountLogin(mActivity, view, Tab.this, mWebViewController)
829 .handleLogin(realm, account, args);
830 }
831
Grace Kloba22ac16e2009-10-07 18:00:23 -0700832 };
833
Patrick Scott92066772011-03-10 08:46:27 -0500834 // Called by DeviceAccountLogin when the Tab needs to have the auto-login UI
835 // displayed.
836 void setDeviceAccountLogin(DeviceAccountLogin login) {
837 mDeviceAccountLogin = login;
838 }
839
840 // Returns non-null if the title bar should display the auto-login UI.
841 DeviceAccountLogin getDeviceAccountLogin() {
842 return mDeviceAccountLogin;
843 }
844
Grace Kloba22ac16e2009-10-07 18:00:23 -0700845 // -------------------------------------------------------------------------
846 // WebChromeClient implementation for the main WebView
847 // -------------------------------------------------------------------------
848
849 private final WebChromeClient mWebChromeClient = new WebChromeClient() {
850 // Helper method to create a new tab or sub window.
851 private void createWindow(final boolean dialog, final Message msg) {
852 WebView.WebViewTransport transport =
853 (WebView.WebViewTransport) msg.obj;
854 if (dialog) {
855 createSubWindow();
Michael Kolb8233fac2010-10-26 16:08:53 -0700856 mWebViewController.attachSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700857 transport.setWebView(mSubView);
858 } else {
Michael Kolb7bcafde2011-05-09 13:55:59 -0700859 final Tab newTab = mWebViewController.openTab(null,
860 Tab.this.isPrivateBrowsingEnabled(),
861 true, true);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700862 if (newTab != Tab.this) {
863 Tab.this.addChildTab(newTab);
864 }
865 transport.setWebView(newTab.getWebView());
866 }
867 msg.sendToTarget();
868 }
869
870 @Override
871 public boolean onCreateWindow(WebView view, final boolean dialog,
872 final boolean userGesture, final Message resultMsg) {
873 // only allow new window or sub window for the foreground case
874 if (!mInForeground) {
875 return false;
876 }
877 // Short-circuit if we can't create any more tabs or sub windows.
878 if (dialog && mSubView != null) {
879 new AlertDialog.Builder(mActivity)
880 .setTitle(R.string.too_many_subwindows_dialog_title)
881 .setIcon(android.R.drawable.ic_dialog_alert)
882 .setMessage(R.string.too_many_subwindows_dialog_message)
883 .setPositiveButton(R.string.ok, null)
884 .show();
885 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700886 } else if (!mWebViewController.getTabControl().canCreateNewTab()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700887 new AlertDialog.Builder(mActivity)
888 .setTitle(R.string.too_many_windows_dialog_title)
889 .setIcon(android.R.drawable.ic_dialog_alert)
890 .setMessage(R.string.too_many_windows_dialog_message)
891 .setPositiveButton(R.string.ok, null)
892 .show();
893 return false;
894 }
895
896 // Short-circuit if this was a user gesture.
897 if (userGesture) {
898 createWindow(dialog, resultMsg);
899 return true;
900 }
901
902 // Allow the popup and create the appropriate window.
903 final AlertDialog.OnClickListener allowListener =
904 new AlertDialog.OnClickListener() {
905 public void onClick(DialogInterface d,
906 int which) {
907 createWindow(dialog, resultMsg);
908 }
909 };
910
911 // Block the popup by returning a null WebView.
912 final AlertDialog.OnClickListener blockListener =
913 new AlertDialog.OnClickListener() {
914 public void onClick(DialogInterface d, int which) {
915 resultMsg.sendToTarget();
916 }
917 };
918
919 // Build a confirmation dialog to display to the user.
920 final AlertDialog d =
921 new AlertDialog.Builder(mActivity)
922 .setTitle(R.string.attention)
923 .setIcon(android.R.drawable.ic_dialog_alert)
924 .setMessage(R.string.popup_window_attempt)
925 .setPositiveButton(R.string.allow, allowListener)
926 .setNegativeButton(R.string.block, blockListener)
927 .setCancelable(false)
928 .create();
929
930 // Show the confirmation dialog.
931 d.show();
932 return true;
933 }
934
935 @Override
Patrick Scotteb5061b2009-11-18 15:00:30 -0500936 public void onRequestFocus(WebView view) {
937 if (!mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700938 mWebViewController.switchToTab(mWebViewController.getTabControl().getTabIndex(
Patrick Scotteb5061b2009-11-18 15:00:30 -0500939 Tab.this));
940 }
941 }
942
943 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700944 public void onCloseWindow(WebView window) {
945 if (mParentTab != null) {
946 // JavaScript can only close popup window.
947 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700948 mWebViewController.switchToTab(mWebViewController.getTabControl()
Grace Kloba22ac16e2009-10-07 18:00:23 -0700949 .getTabIndex(mParentTab));
950 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700951 mWebViewController.closeTab(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700952 }
953 }
954
955 @Override
956 public void onProgressChanged(WebView view, int newProgress) {
John Reck30c714c2010-12-16 17:30:34 -0800957 mPageLoadProgress = newProgress;
958 mWebViewController.onProgressChanged(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700959 }
960
961 @Override
Leon Scroggins21d9b902010-03-11 09:33:11 -0500962 public void onReceivedTitle(WebView view, final String title) {
John Reck30c714c2010-12-16 17:30:34 -0800963 mCurrentState.mTitle = title;
Michael Kolb8233fac2010-10-26 16:08:53 -0700964 mWebViewController.onReceivedTitle(Tab.this, title);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700965 }
966
967 @Override
968 public void onReceivedIcon(WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -0800969 mCurrentState.mFavicon = icon;
Michael Kolb8233fac2010-10-26 16:08:53 -0700970 mWebViewController.onFavicon(Tab.this, view, icon);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700971 }
972
973 @Override
974 public void onReceivedTouchIconUrl(WebView view, String url,
975 boolean precomposed) {
976 final ContentResolver cr = mActivity.getContentResolver();
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400977 // Let precomposed icons take precedence over non-composed
978 // icons.
979 if (precomposed && mTouchIconLoader != null) {
980 mTouchIconLoader.cancel(false);
981 mTouchIconLoader = null;
982 }
983 // Have only one async task at a time.
984 if (mTouchIconLoader == null) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700985 mTouchIconLoader = new DownloadTouchIcon(Tab.this,
986 mActivity, cr, view);
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400987 mTouchIconLoader.execute(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700988 }
989 }
990
991 @Override
992 public void onShowCustomView(View view,
993 WebChromeClient.CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700994 if (mInForeground) mWebViewController.showCustomView(Tab.this, view,
995 callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700996 }
997
998 @Override
999 public void onHideCustomView() {
Michael Kolb8233fac2010-10-26 16:08:53 -07001000 if (mInForeground) mWebViewController.hideCustomView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001001 }
1002
1003 /**
1004 * The origin has exceeded its database quota.
1005 * @param url the URL that exceeded the quota
1006 * @param databaseIdentifier the identifier of the database on which the
1007 * transaction that caused the quota overflow was run
1008 * @param currentQuota the current quota for the origin.
1009 * @param estimatedSize the estimated size of the database.
1010 * @param totalUsedQuota is the sum of all origins' quota.
1011 * @param quotaUpdater The callback to run when a decision to allow or
1012 * deny quota has been made. Don't forget to call this!
1013 */
1014 @Override
1015 public void onExceededDatabaseQuota(String url,
1016 String databaseIdentifier, long currentQuota, long estimatedSize,
1017 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -07001018 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001019 .onExceededDatabaseQuota(url, databaseIdentifier,
1020 currentQuota, estimatedSize, totalUsedQuota,
1021 quotaUpdater);
1022 }
1023
1024 /**
1025 * The Application Cache has exceeded its max size.
1026 * @param spaceNeeded is the amount of disk space that would be needed
1027 * in order for the last appcache operation to succeed.
1028 * @param totalUsedQuota is the sum of all origins' quota.
1029 * @param quotaUpdater A callback to inform the WebCore thread that a
1030 * new app cache size is available. This callback must always
1031 * be executed at some point to ensure that the sleeping
1032 * WebCore thread is woken up.
1033 */
1034 @Override
1035 public void onReachedMaxAppCacheSize(long spaceNeeded,
1036 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -07001037 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001038 .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota,
1039 quotaUpdater);
1040 }
1041
1042 /**
1043 * Instructs the browser to show a prompt to ask the user to set the
1044 * Geolocation permission state for the specified origin.
1045 * @param origin The origin for which Geolocation permissions are
1046 * requested.
1047 * @param callback The callback to call once the user has set the
1048 * Geolocation permission state.
1049 */
1050 @Override
1051 public void onGeolocationPermissionsShowPrompt(String origin,
1052 GeolocationPermissions.Callback callback) {
1053 if (mInForeground) {
Grace Kloba50c241e2010-04-20 11:07:50 -07001054 getGeolocationPermissionsPrompt().show(origin, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001055 }
1056 }
1057
1058 /**
1059 * Instructs the browser to hide the Geolocation permissions prompt.
1060 */
1061 @Override
1062 public void onGeolocationPermissionsHidePrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001063 if (mInForeground && mGeolocationPermissionsPrompt != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001064 mGeolocationPermissionsPrompt.hide();
1065 }
1066 }
1067
Ben Murdoch65acc352009-11-19 18:16:04 +00001068 /* Adds a JavaScript error message to the system log and if the JS
1069 * console is enabled in the about:debug options, to that console
1070 * also.
Ben Murdochc42addf2010-01-28 15:19:59 +00001071 * @param consoleMessage the message object.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001072 */
1073 @Override
Ben Murdochc42addf2010-01-28 15:19:59 +00001074 public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001075 if (mInForeground) {
1076 // call getErrorConsole(true) so it will create one if needed
1077 ErrorConsoleView errorConsole = getErrorConsole(true);
Ben Murdochc42addf2010-01-28 15:19:59 +00001078 errorConsole.addErrorMessage(consoleMessage);
Michael Kolb8233fac2010-10-26 16:08:53 -07001079 if (mWebViewController.shouldShowErrorConsole()
1080 && errorConsole.getShowState() !=
1081 ErrorConsoleView.SHOW_MAXIMIZED) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001082 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
1083 }
1084 }
Ben Murdochc42addf2010-01-28 15:19:59 +00001085
Jeff Hamilton47654f42010-09-07 09:57:51 -05001086 // Don't log console messages in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001087 if (isPrivateBrowsingEnabled()) return true;
Jeff Hamilton47654f42010-09-07 09:57:51 -05001088
Ben Murdochc42addf2010-01-28 15:19:59 +00001089 String message = "Console: " + consoleMessage.message() + " "
1090 + consoleMessage.sourceId() + ":"
1091 + consoleMessage.lineNumber();
1092
1093 switch (consoleMessage.messageLevel()) {
1094 case TIP:
1095 Log.v(CONSOLE_LOGTAG, message);
1096 break;
1097 case LOG:
1098 Log.i(CONSOLE_LOGTAG, message);
1099 break;
1100 case WARNING:
1101 Log.w(CONSOLE_LOGTAG, message);
1102 break;
1103 case ERROR:
1104 Log.e(CONSOLE_LOGTAG, message);
1105 break;
1106 case DEBUG:
1107 Log.d(CONSOLE_LOGTAG, message);
1108 break;
1109 }
1110
1111 return true;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001112 }
1113
1114 /**
1115 * Ask the browser for an icon to represent a <video> element.
1116 * This icon will be used if the Web page did not specify a poster attribute.
1117 * @return Bitmap The icon or null if no such icon is available.
1118 */
1119 @Override
1120 public Bitmap getDefaultVideoPoster() {
1121 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001122 return mWebViewController.getDefaultVideoPoster();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001123 }
1124 return null;
1125 }
1126
1127 /**
1128 * Ask the host application for a custom progress view to show while
1129 * a <video> is loading.
1130 * @return View The progress view.
1131 */
1132 @Override
1133 public View getVideoLoadingProgressView() {
1134 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001135 return mWebViewController.getVideoLoadingProgressView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001136 }
1137 return null;
1138 }
1139
1140 @Override
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01001141 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001142 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001143 mWebViewController.openFileChooser(uploadMsg, acceptType);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001144 } else {
1145 uploadMsg.onReceiveValue(null);
1146 }
1147 }
1148
1149 /**
1150 * Deliver a list of already-visited URLs
1151 */
1152 @Override
1153 public void getVisitedHistory(final ValueCallback<String[]> callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001154 mWebViewController.getVisitedHistory(callback);
1155 }
Ben Murdoch8029a772010-11-16 11:58:21 +00001156
1157 @Override
1158 public void setupAutoFill(Message message) {
1159 // Prompt the user to set up their profile.
1160 final Message msg = message;
1161 AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
Ben Murdoch1d676b62011-01-17 12:54:24 +00001162 LayoutInflater inflater = (LayoutInflater) mActivity.getSystemService(
1163 Context.LAYOUT_INFLATER_SERVICE);
1164 final View layout = inflater.inflate(R.layout.setup_autofill_dialog, null);
1165
1166 builder.setView(layout)
1167 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1168 @Override
1169 public void onClick(DialogInterface dialog, int id) {
1170 CheckBox disableAutoFill = (CheckBox) layout.findViewById(
1171 R.id.setup_autofill_dialog_disable_autofill);
1172
1173 if (disableAutoFill.isChecked()) {
1174 // Disable autofill and show a toast with how to turn it on again.
John Reck35e9dd62011-04-25 09:01:54 -07001175 mSettings.setAutofillEnabled(false);
Ben Murdoch1d676b62011-01-17 12:54:24 +00001176 Toast.makeText(mActivity,
1177 R.string.autofill_setup_dialog_negative_toast,
1178 Toast.LENGTH_LONG).show();
1179 } else {
1180 // Take user to the AutoFill profile editor. When they return,
1181 // we will send the message that we pass here which will trigger
1182 // the form to get filled out with their new profile.
1183 mWebViewController.setupAutoFill(msg);
1184 }
1185 }
1186 })
1187 .setNegativeButton(R.string.cancel, null)
1188 .show();
Ben Murdoch8029a772010-11-16 11:58:21 +00001189 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001190 };
1191
1192 // -------------------------------------------------------------------------
1193 // WebViewClient implementation for the sub window
1194 // -------------------------------------------------------------------------
1195
1196 // Subclass of WebViewClient used in subwindows to notify the main
1197 // WebViewClient of certain WebView activities.
1198 private static class SubWindowClient extends WebViewClient {
1199 // The main WebViewClient.
1200 private final WebViewClient mClient;
Michael Kolb8233fac2010-10-26 16:08:53 -07001201 private final WebViewController mController;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001202
Michael Kolb8233fac2010-10-26 16:08:53 -07001203 SubWindowClient(WebViewClient client, WebViewController controller) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001204 mClient = client;
Michael Kolb8233fac2010-10-26 16:08:53 -07001205 mController = controller;
Leon Scroggins III211ba542010-04-19 13:21:13 -04001206 }
1207 @Override
1208 public void onPageStarted(WebView view, String url, Bitmap favicon) {
1209 // Unlike the others, do not call mClient's version, which would
1210 // change the progress bar. However, we do want to remove the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001211 // find or select dialog.
Michael Kolb8233fac2010-10-26 16:08:53 -07001212 mController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001213 }
1214 @Override
1215 public void doUpdateVisitedHistory(WebView view, String url,
1216 boolean isReload) {
1217 mClient.doUpdateVisitedHistory(view, url, isReload);
1218 }
1219 @Override
1220 public boolean shouldOverrideUrlLoading(WebView view, String url) {
1221 return mClient.shouldOverrideUrlLoading(view, url);
1222 }
1223 @Override
1224 public void onReceivedSslError(WebView view, SslErrorHandler handler,
1225 SslError error) {
1226 mClient.onReceivedSslError(view, handler, error);
1227 }
1228 @Override
1229 public void onReceivedHttpAuthRequest(WebView view,
1230 HttpAuthHandler handler, String host, String realm) {
1231 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
1232 }
1233 @Override
1234 public void onFormResubmission(WebView view, Message dontResend,
1235 Message resend) {
1236 mClient.onFormResubmission(view, dontResend, resend);
1237 }
1238 @Override
1239 public void onReceivedError(WebView view, int errorCode,
1240 String description, String failingUrl) {
1241 mClient.onReceivedError(view, errorCode, description, failingUrl);
1242 }
1243 @Override
1244 public boolean shouldOverrideKeyEvent(WebView view,
1245 android.view.KeyEvent event) {
1246 return mClient.shouldOverrideKeyEvent(view, event);
1247 }
1248 @Override
1249 public void onUnhandledKeyEvent(WebView view,
1250 android.view.KeyEvent event) {
1251 mClient.onUnhandledKeyEvent(view, event);
1252 }
1253 }
1254
1255 // -------------------------------------------------------------------------
1256 // WebChromeClient implementation for the sub window
1257 // -------------------------------------------------------------------------
1258
1259 private class SubWindowChromeClient extends WebChromeClient {
1260 // The main WebChromeClient.
1261 private final WebChromeClient mClient;
1262
1263 SubWindowChromeClient(WebChromeClient client) {
1264 mClient = client;
1265 }
1266 @Override
1267 public void onProgressChanged(WebView view, int newProgress) {
1268 mClient.onProgressChanged(view, newProgress);
1269 }
1270 @Override
1271 public boolean onCreateWindow(WebView view, boolean dialog,
1272 boolean userGesture, android.os.Message resultMsg) {
1273 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
1274 }
1275 @Override
1276 public void onCloseWindow(WebView window) {
1277 if (window != mSubView) {
1278 Log.e(LOGTAG, "Can't close the window");
1279 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001280 mWebViewController.dismissSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001281 }
1282 }
1283
1284 // -------------------------------------------------------------------------
1285
Michael Kolb8233fac2010-10-26 16:08:53 -07001286 // TODO temporarily use activity here
1287 // remove later
1288
Grace Kloba22ac16e2009-10-07 18:00:23 -07001289 // Construct a new tab
Michael Kolb7bcafde2011-05-09 13:55:59 -07001290 Tab(WebViewController wvcontroller, WebView w) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001291 mWebViewController = wvcontroller;
1292 mActivity = mWebViewController.getActivity();
John Reck35e9dd62011-04-25 09:01:54 -07001293 mSettings = BrowserSettings.getInstance();
John Recke969cc52010-12-21 17:24:43 -08001294 mDataController = DataController.getInstance(mActivity);
John Reck847b5322011-04-14 17:02:18 -07001295 mCurrentState = new PageState(mActivity, w != null
1296 ? w.isPrivateBrowsingEnabled() : false);
Michael Kolb8233fac2010-10-26 16:08:53 -07001297 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001298 mInForeground = false;
1299
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001300 mDownloadListener = new DownloadListener() {
1301 public void onDownloadStart(String url, String userAgent,
1302 String contentDisposition, String mimetype,
1303 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001304 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001305 mimetype, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001306 }
1307 };
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001308 mWebBackForwardListClient = new WebBackForwardListClient() {
1309 @Override
1310 public void onNewHistoryItem(WebHistoryItem item) {
1311 if (isInVoiceSearchMode()) {
1312 item.setCustomData(mVoiceSearchData.mVoiceSearchIntent);
1313 }
1314 }
1315 @Override
1316 public void onIndexChanged(WebHistoryItem item, int index) {
1317 Object data = item.getCustomData();
1318 if (data != null && data instanceof Intent) {
1319 activateVoiceSearchMode((Intent) data);
1320 }
1321 }
1322 };
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001323
Grace Kloba22ac16e2009-10-07 18:00:23 -07001324 setWebView(w);
1325 }
1326
1327 /**
1328 * Sets the WebView for this tab, correctly removing the old WebView from
1329 * the container view.
1330 */
1331 void setWebView(WebView w) {
1332 if (mMainView == w) {
1333 return;
1334 }
Michael Kolba713ec82010-11-29 17:27:06 -08001335
Grace Kloba22ac16e2009-10-07 18:00:23 -07001336 // If the WebView is changing, the page will be reloaded, so any ongoing
1337 // Geolocation permission requests are void.
Grace Kloba50c241e2010-04-20 11:07:50 -07001338 if (mGeolocationPermissionsPrompt != null) {
1339 mGeolocationPermissionsPrompt.hide();
1340 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001341
Michael Kolba713ec82010-11-29 17:27:06 -08001342 mWebViewController.onSetWebView(this, w);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001343
1344 // set the new one
1345 mMainView = w;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001346 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001347 if (mMainView != null) {
1348 mMainView.setWebViewClient(mWebViewClient);
1349 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001350 // Attach DownloadManager so that downloads can start in an active
1351 // or a non-active window. This can happen when going to a site that
1352 // does a redirect after a period of time. The user could have
1353 // switched to another tab while waiting for the download to start.
1354 mMainView.setDownloadListener(mDownloadListener);
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001355 mMainView.setWebBackForwardListClient(mWebBackForwardListClient);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001356 }
1357 }
1358
1359 /**
1360 * Destroy the tab's main WebView and subWindow if any
1361 */
1362 void destroy() {
1363 if (mMainView != null) {
1364 dismissSubWindow();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001365 // save the WebView to call destroy() after detach it from the tab
1366 WebView webView = mMainView;
1367 setWebView(null);
1368 webView.destroy();
1369 }
1370 }
1371
1372 /**
1373 * Remove the tab from the parent
1374 */
1375 void removeFromTree() {
1376 // detach the children
1377 if (mChildTabs != null) {
1378 for(Tab t : mChildTabs) {
1379 t.setParentTab(null);
1380 }
1381 }
1382 // remove itself from the parent list
1383 if (mParentTab != null) {
1384 mParentTab.mChildTabs.remove(this);
1385 }
1386 }
1387
1388 /**
1389 * Create a new subwindow unless a subwindow already exists.
1390 * @return True if a new subwindow was created. False if one already exists.
1391 */
1392 boolean createSubWindow() {
1393 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001394 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001395 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001396 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001397 mSubView.setWebChromeClient(new SubWindowChromeClient(
1398 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001399 // Set a different DownloadListener for the mSubView, since it will
1400 // just need to dismiss the mSubView, rather than close the Tab
1401 mSubView.setDownloadListener(new DownloadListener() {
1402 public void onDownloadStart(String url, String userAgent,
1403 String contentDisposition, String mimetype,
1404 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001405 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001406 contentDisposition, mimetype, contentLength);
1407 if (mSubView.copyBackForwardList().getSize() == 0) {
1408 // This subwindow was opened for the sole purpose of
1409 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001410 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001411 }
1412 }
1413 });
Grace Kloba22ac16e2009-10-07 18:00:23 -07001414 mSubView.setOnCreateContextMenuListener(mActivity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001415 return true;
1416 }
1417 return false;
1418 }
1419
1420 /**
1421 * Dismiss the subWindow for the tab.
1422 */
1423 void dismissSubWindow() {
1424 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001425 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001426 mSubView.destroy();
1427 mSubView = null;
1428 mSubViewContainer = null;
1429 }
1430 }
1431
Grace Kloba22ac16e2009-10-07 18:00:23 -07001432
1433 /**
1434 * Set the parent tab of this tab.
1435 */
1436 void setParentTab(Tab parent) {
1437 mParentTab = parent;
1438 // This tab may have been freed due to low memory. If that is the case,
1439 // the parent tab index is already saved. If we are changing that index
1440 // (most likely due to removing the parent tab) we must update the
1441 // parent tab index in the saved Bundle.
1442 if (mSavedState != null) {
1443 if (parent == null) {
1444 mSavedState.remove(PARENTTAB);
1445 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -07001446 mSavedState.putInt(PARENTTAB, mWebViewController.getTabControl()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001447 .getTabIndex(parent));
1448 }
1449 }
1450 }
1451
1452 /**
1453 * When a Tab is created through the content of another Tab, then we
1454 * associate the Tabs.
1455 * @param child the Tab that was created from this Tab
1456 */
1457 void addChildTab(Tab child) {
1458 if (mChildTabs == null) {
1459 mChildTabs = new Vector<Tab>();
1460 }
1461 mChildTabs.add(child);
1462 child.setParentTab(this);
1463 }
1464
1465 Vector<Tab> getChildTabs() {
1466 return mChildTabs;
1467 }
1468
1469 void resume() {
1470 if (mMainView != null) {
1471 mMainView.onResume();
1472 if (mSubView != null) {
1473 mSubView.onResume();
1474 }
1475 }
1476 }
1477
1478 void pause() {
1479 if (mMainView != null) {
1480 mMainView.onPause();
1481 if (mSubView != null) {
1482 mSubView.onPause();
1483 }
1484 }
1485 }
1486
1487 void putInForeground() {
1488 mInForeground = true;
1489 resume();
1490 mMainView.setOnCreateContextMenuListener(mActivity);
1491 if (mSubView != null) {
1492 mSubView.setOnCreateContextMenuListener(mActivity);
1493 }
1494 // Show the pending error dialog if the queue is not empty
1495 if (mQueuedErrors != null && mQueuedErrors.size() > 0) {
1496 showError(mQueuedErrors.getFirst());
1497 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001498 mWebViewController.bookmarkedStatusHasChanged(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001499 }
1500
1501 void putInBackground() {
1502 mInForeground = false;
1503 pause();
1504 mMainView.setOnCreateContextMenuListener(null);
1505 if (mSubView != null) {
1506 mSubView.setOnCreateContextMenuListener(null);
1507 }
1508 }
1509
Michael Kolb8233fac2010-10-26 16:08:53 -07001510 boolean inForeground() {
1511 return mInForeground;
1512 }
1513
Grace Kloba22ac16e2009-10-07 18:00:23 -07001514 /**
1515 * Return the top window of this tab; either the subwindow if it is not
1516 * null or the main window.
1517 * @return The top window of this tab.
1518 */
1519 WebView getTopWindow() {
1520 if (mSubView != null) {
1521 return mSubView;
1522 }
1523 return mMainView;
1524 }
1525
1526 /**
1527 * Return the main window of this tab. Note: if a tab is freed in the
1528 * background, this can return null. It is only guaranteed to be
1529 * non-null for the current tab.
1530 * @return The main WebView of this tab.
1531 */
1532 WebView getWebView() {
1533 return mMainView;
1534 }
1535
Michael Kolba713ec82010-11-29 17:27:06 -08001536 void setViewContainer(View container) {
1537 mContainer = container;
1538 }
1539
Michael Kolb8233fac2010-10-26 16:08:53 -07001540 View getViewContainer() {
1541 return mContainer;
1542 }
1543
Grace Kloba22ac16e2009-10-07 18:00:23 -07001544 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001545 * Return whether private browsing is enabled for the main window of
1546 * this tab.
1547 * @return True if private browsing is enabled.
1548 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001549 boolean isPrivateBrowsingEnabled() {
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001550 WebView webView = getWebView();
1551 if (webView == null) {
1552 return false;
1553 }
1554 return webView.isPrivateBrowsingEnabled();
1555 }
1556
1557 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001558 * Return the subwindow of this tab or null if there is no subwindow.
1559 * @return The subwindow of this tab or null.
1560 */
1561 WebView getSubWebView() {
1562 return mSubView;
1563 }
1564
Michael Kolb1514bb72010-11-22 09:11:48 -08001565 void setSubWebView(WebView subView) {
1566 mSubView = subView;
1567 }
1568
Michael Kolb8233fac2010-10-26 16:08:53 -07001569 View getSubViewContainer() {
1570 return mSubViewContainer;
1571 }
1572
Michael Kolb1514bb72010-11-22 09:11:48 -08001573 void setSubViewContainer(View subViewContainer) {
1574 mSubViewContainer = subViewContainer;
1575 }
1576
Grace Kloba22ac16e2009-10-07 18:00:23 -07001577 /**
1578 * @return The geolocation permissions prompt for this tab.
1579 */
1580 GeolocationPermissionsPrompt getGeolocationPermissionsPrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001581 if (mGeolocationPermissionsPrompt == null) {
1582 ViewStub stub = (ViewStub) mContainer
1583 .findViewById(R.id.geolocation_permissions_prompt);
1584 mGeolocationPermissionsPrompt = (GeolocationPermissionsPrompt) stub
1585 .inflate();
1586 mGeolocationPermissionsPrompt.init();
1587 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001588 return mGeolocationPermissionsPrompt;
1589 }
1590
1591 /**
1592 * @return The application id string
1593 */
1594 String getAppId() {
1595 return mAppId;
1596 }
1597
1598 /**
1599 * Set the application id string
1600 * @param id
1601 */
1602 void setAppId(String id) {
1603 mAppId = id;
1604 }
1605
Grace Kloba22ac16e2009-10-07 18:00:23 -07001606 String getUrl() {
John Reck324d4402011-01-11 16:56:42 -08001607 return UrlUtils.filteredUrl(mCurrentState.mUrl);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001608 }
1609
John Reck49a603c2011-03-03 09:33:05 -08001610 String getOriginalUrl() {
1611 if (mMainView == null) {
1612 return "";
1613 }
1614 return UrlUtils.filteredUrl(mMainView.getOriginalUrl());
1615 }
1616
Grace Kloba22ac16e2009-10-07 18:00:23 -07001617 /**
John Reck30c714c2010-12-16 17:30:34 -08001618 * Get the title of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001619 */
1620 String getTitle() {
John Reck30c714c2010-12-16 17:30:34 -08001621 if (mCurrentState.mTitle == null && mInPageLoad) {
1622 return mActivity.getString(R.string.title_bar_loading);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001623 }
John Reck30c714c2010-12-16 17:30:34 -08001624 return mCurrentState.mTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001625 }
1626
1627 /**
John Reck30c714c2010-12-16 17:30:34 -08001628 * Get the favicon of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001629 */
1630 Bitmap getFavicon() {
John Reck30c714c2010-12-16 17:30:34 -08001631 return mCurrentState.mFavicon;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001632 }
1633
John Recke969cc52010-12-21 17:24:43 -08001634 public boolean isBookmarkedSite() {
1635 return mCurrentState.mIsBookmarkedSite;
1636 }
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001637
Grace Kloba22ac16e2009-10-07 18:00:23 -07001638 /**
1639 * Return the tab's error console. Creates the console if createIfNEcessary
1640 * is true and we haven't already created the console.
1641 * @param createIfNecessary Flag to indicate if the console should be
1642 * created if it has not been already.
1643 * @return The tab's error console, or null if one has not been created and
1644 * createIfNecessary is false.
1645 */
1646 ErrorConsoleView getErrorConsole(boolean createIfNecessary) {
1647 if (createIfNecessary && mErrorConsole == null) {
1648 mErrorConsole = new ErrorConsoleView(mActivity);
1649 mErrorConsole.setWebView(mMainView);
1650 }
1651 return mErrorConsole;
1652 }
1653
1654 /**
1655 * If this Tab was created through another Tab, then this method returns
1656 * that Tab.
1657 * @return the Tab parent or null
1658 */
1659 public Tab getParentTab() {
1660 return mParentTab;
1661 }
1662
John Reck30c714c2010-12-16 17:30:34 -08001663 private void setLockIconType(LockIcon icon) {
1664 mCurrentState.mLockIcon = icon;
1665 mWebViewController.onUpdatedLockIcon(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001666 }
1667
1668 /**
1669 * @return The tab's lock icon type.
1670 */
John Reck30c714c2010-12-16 17:30:34 -08001671 LockIcon getLockIconType() {
1672 return mCurrentState.mLockIcon;
1673 }
1674
1675 int getLoadProgress() {
1676 if (mInPageLoad) {
1677 return mPageLoadProgress;
1678 }
1679 return 100;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001680 }
1681
1682 /**
1683 * @return TRUE if onPageStarted is called while onPageFinished is not
1684 * called yet.
1685 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001686 boolean inPageLoad() {
1687 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001688 }
1689
1690 // force mInLoad to be false. This should only be called before closing the
1691 // tab to ensure BrowserActivity's pauseWebViewTimers() is called correctly.
Michael Kolb8233fac2010-10-26 16:08:53 -07001692 void clearInPageLoad() {
1693 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001694 }
1695
Grace Kloba22ac16e2009-10-07 18:00:23 -07001696 /**
John Reck30c714c2010-12-16 17:30:34 -08001697 * Get the cached saved state bundle.
1698 * @return cached state bundle
Grace Kloba22ac16e2009-10-07 18:00:23 -07001699 */
1700 Bundle getSavedState() {
1701 return mSavedState;
1702 }
1703
1704 /**
1705 * Set the saved state.
1706 */
1707 void setSavedState(Bundle state) {
1708 mSavedState = state;
1709 }
1710
1711 /**
1712 * @return TRUE if succeed in saving the state.
1713 */
1714 boolean saveState() {
1715 // If the WebView is null it means we ran low on memory and we already
1716 // stored the saved state in mSavedState.
1717 if (mMainView == null) {
1718 return mSavedState != null;
1719 }
1720
1721 mSavedState = new Bundle();
1722 final WebBackForwardList list = mMainView.saveState(mSavedState);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001723
1724 // Store some extra info for displaying the tab in the picker.
1725 final WebHistoryItem item = list != null ? list.getCurrentItem() : null;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001726
John Reck30c714c2010-12-16 17:30:34 -08001727 mSavedState.putString(CURRURL, mCurrentState.mUrl);
1728 mSavedState.putString(CURRTITLE, mCurrentState.mTitle);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001729 if (mAppId != null) {
1730 mSavedState.putString(APPID, mAppId);
1731 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001732 // Remember the parent tab so the relationship can be restored.
1733 if (mParentTab != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001734 mSavedState.putInt(PARENTTAB, mWebViewController.getTabControl().getTabIndex(
Grace Kloba22ac16e2009-10-07 18:00:23 -07001735 mParentTab));
1736 }
Michael Kolbeb95db42011-03-03 10:38:40 -08001737 if (mScreenshot != null) {
1738 mSavedState.putParcelable(SCREENSHOT, mScreenshot);
1739 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001740 return true;
1741 }
1742
1743 /*
1744 * Restore the state of the tab.
1745 */
1746 boolean restoreState(Bundle b) {
1747 if (b == null) {
1748 return false;
1749 }
1750 // Restore the internal state even if the WebView fails to restore.
1751 // This will maintain the app id, original url and close-on-exit values.
1752 mSavedState = null;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001753 mAppId = b.getString(APPID);
Michael Kolbeb95db42011-03-03 10:38:40 -08001754 mScreenshot = b.getParcelable(SCREENSHOT);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001755
1756 final WebBackForwardList list = mMainView.restoreState(b);
1757 if (list == null) {
1758 return false;
1759 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001760 return true;
1761 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04001762
Leon Scroggins1961ed22010-12-07 15:22:21 -05001763 public void updateBookmarkedStatus() {
John Recke969cc52010-12-21 17:24:43 -08001764 mDataController.queryBookmarkStatus(getUrl(), mIsBookmarkCallback);
Leon Scroggins1961ed22010-12-07 15:22:21 -05001765 }
1766
John Recke969cc52010-12-21 17:24:43 -08001767 private DataController.OnQueryUrlIsBookmark mIsBookmarkCallback
1768 = new DataController.OnQueryUrlIsBookmark() {
1769 @Override
1770 public void onQueryUrlIsBookmark(String url, boolean isBookmark) {
1771 if (mCurrentState.mUrl.equals(url)) {
1772 mCurrentState.mIsBookmarkedSite = isBookmark;
1773 mWebViewController.bookmarkedStatusHasChanged(Tab.this);
1774 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001775 }
John Recke969cc52010-12-21 17:24:43 -08001776 };
Michael Kolb1acef692011-03-08 14:12:06 -08001777
Michael Kolbeb95db42011-03-03 10:38:40 -08001778 public void setScreenshot(Bitmap screenshot) {
1779 mScreenshot = screenshot;
1780 }
1781
1782 public Bitmap getScreenshot() {
1783 return mScreenshot;
1784 }
1785
Grace Kloba22ac16e2009-10-07 18:00:23 -07001786}