blob: 863fc951331199af4c0d1599e16933aca96cd0d2 [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;
111 // If true, the tab will be removed when back out of the first page.
112 private boolean mCloseOnExit;
113 // If true, the tab is in the foreground of the current activity.
114 private boolean mInForeground;
Michael Kolb8233fac2010-10-26 16:08:53 -0700115 // If true, the tab is in page loading state (after onPageStarted,
116 // before onPageFinsihed)
117 private boolean mInPageLoad;
John Reck30c714c2010-12-16 17:30:34 -0800118 // The last reported progress of the current page
119 private int mPageLoadProgress;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000120 // The time the load started, used to find load page time
121 private long mLoadStartTime;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700122 // Application identifier used to find tabs that another application wants
123 // to reuse.
124 private String mAppId;
125 // Keep the original url around to avoid killing the old WebView if the url
126 // has not changed.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700127 // Error console for the tab
128 private ErrorConsoleView mErrorConsole;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -0500129 // The listener that gets invoked when a download is started from the
130 // mMainView
131 private final DownloadListener mDownloadListener;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500132 // Listener used to know when we move forward or back in the history list.
133 private final WebBackForwardListClient mWebBackForwardListClient;
John Recke969cc52010-12-21 17:24:43 -0800134 private DataController mDataController;
Patrick Scott92066772011-03-10 08:46:27 -0500135 // State of the auto-login request.
136 private DeviceAccountLogin mDeviceAccountLogin;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700137
138 // AsyncTask for downloading touch icons
139 DownloadTouchIcon mTouchIconLoader;
140
Michael Kolbeb95db42011-03-03 10:38:40 -0800141 private Bitmap mScreenshot;
142
John Reck30c714c2010-12-16 17:30:34 -0800143 // All the state needed for a page
144 private static class PageState {
145 String mUrl;
146 String mTitle;
147 LockIcon mLockIcon;
148 Bitmap mFavicon;
John Recke969cc52010-12-21 17:24:43 -0800149 Boolean mIsBookmarkedSite = false;
John Reck30c714c2010-12-16 17:30:34 -0800150
151 PageState(Context c, boolean incognito) {
152 if (incognito) {
153 mUrl = "browser:incognito";
154 mTitle = c.getString(R.string.new_incognito_tab);
John Reck30c714c2010-12-16 17:30:34 -0800155 } else {
156 mUrl = "";
157 mTitle = c.getString(R.string.new_tab);
John Reck30c714c2010-12-16 17:30:34 -0800158 }
Justin Hodc6cbb72011-01-24 15:14:54 -0800159 mFavicon = BitmapFactory.decodeResource(
160 c.getResources(), R.drawable.app_web_browser_sm);
John Reck30c714c2010-12-16 17:30:34 -0800161 mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
162 }
163
164 PageState(Context c, boolean incognito, String url, Bitmap favicon) {
165 mUrl = url;
166 mTitle = null;
167 if (URLUtil.isHttpsUrl(url)) {
168 mLockIcon = LockIcon.LOCK_ICON_SECURE;
169 } else {
170 mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
171 }
172 if (favicon != null) {
173 mFavicon = favicon;
174 } else {
Justin Hodc6cbb72011-01-24 15:14:54 -0800175 mFavicon = BitmapFactory.decodeResource(
176 c.getResources(), R.drawable.app_web_browser_sm);
John Reck30c714c2010-12-16 17:30:34 -0800177 }
178 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700179 }
180
John Reck30c714c2010-12-16 17:30:34 -0800181 // The current/loading page's state
182 private PageState mCurrentState;
183
Grace Kloba22ac16e2009-10-07 18:00:23 -0700184 // Used for saving and restoring each Tab
John Reck30c714c2010-12-16 17:30:34 -0800185 // TODO: Figure out who uses what and where
186 // Some of these aren't use in this class, and some are only used in
187 // restoring state but not saving it - FIX THIS
Grace Kloba22ac16e2009-10-07 18:00:23 -0700188 static final String WEBVIEW = "webview";
189 static final String NUMTABS = "numTabs";
190 static final String CURRTAB = "currentTab";
191 static final String CURRURL = "currentUrl";
192 static final String CURRTITLE = "currentTitle";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700193 static final String CLOSEONEXIT = "closeonexit";
194 static final String PARENTTAB = "parentTab";
195 static final String APPID = "appid";
196 static final String ORIGINALURL = "originalUrl";
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700197 static final String INCOGNITO = "privateBrowsingEnabled";
Michael Kolbeb95db42011-03-03 10:38:40 -0800198 static final String SCREENSHOT = "screenshot";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700199
200 // -------------------------------------------------------------------------
201
Leon Scroggins58d56c62010-01-28 15:12:40 -0500202 /**
203 * Private information regarding the latest voice search. If the Tab is not
204 * in voice search mode, this will be null.
205 */
206 private VoiceSearchData mVoiceSearchData;
207 /**
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400208 * Remove voice search mode from this tab.
209 */
210 public void revertVoiceSearchMode() {
211 if (mVoiceSearchData != null) {
212 mVoiceSearchData = null;
213 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700214 mWebViewController.revertVoiceSearchMode(this);
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400215 }
216 }
217 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700218
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400219 /**
Leon Scroggins58d56c62010-01-28 15:12:40 -0500220 * Return whether the tab is in voice search mode.
221 */
222 public boolean isInVoiceSearchMode() {
223 return mVoiceSearchData != null;
224 }
225 /**
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400226 * Return true if the Tab is in voice search mode and the voice search
227 * Intent came with a String identifying that Google provided the Intent.
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500228 */
229 public boolean voiceSearchSourceIsGoogle() {
230 return mVoiceSearchData != null && mVoiceSearchData.mSourceIsGoogle;
231 }
232 /**
Leon Scroggins58d56c62010-01-28 15:12:40 -0500233 * Get the title to display for the current voice search page. If the Tab
234 * is not in voice search mode, return null.
235 */
236 public String getVoiceDisplayTitle() {
237 if (mVoiceSearchData == null) return null;
238 return mVoiceSearchData.mLastVoiceSearchTitle;
239 }
240 /**
241 * Get the latest array of voice search results, to be passed to the
242 * BrowserProvider. If the Tab is not in voice search mode, return null.
243 */
244 public ArrayList<String> getVoiceSearchResults() {
245 if (mVoiceSearchData == null) return null;
246 return mVoiceSearchData.mVoiceSearchResults;
247 }
248 /**
249 * Activate voice search mode.
250 * @param intent Intent which has the results to use, or an index into the
251 * results when reusing the old results.
252 */
253 /* package */ void activateVoiceSearchMode(Intent intent) {
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500254 int index = 0;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500255 ArrayList<String> results = intent.getStringArrayListExtra(
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -0500256 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_STRINGS);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500257 if (results != null) {
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500258 ArrayList<String> urls = intent.getStringArrayListExtra(
259 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_URLS);
260 ArrayList<String> htmls = intent.getStringArrayListExtra(
261 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_HTML);
262 ArrayList<String> baseUrls = intent.getStringArrayListExtra(
263 RecognizerResultsIntent
264 .EXTRA_VOICE_SEARCH_RESULT_HTML_BASE_URLS);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500265 // This tab is now entering voice search mode for the first time, or
266 // a new voice search was done.
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500267 int size = results.size();
268 if (urls == null || size != urls.size()) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500269 throw new AssertionError("improper extras passed in Intent");
270 }
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500271 if (htmls == null || htmls.size() != size || baseUrls == null ||
272 (baseUrls.size() != size && baseUrls.size() != 1)) {
273 // If either of these arrays are empty/incorrectly sized, ignore
274 // them.
275 htmls = null;
276 baseUrls = null;
277 }
278 mVoiceSearchData = new VoiceSearchData(results, urls, htmls,
279 baseUrls);
Leon Scroggins9df94972010-03-08 18:20:35 -0500280 mVoiceSearchData.mHeaders = intent.getParcelableArrayListExtra(
281 RecognizerResultsIntent
282 .EXTRA_VOICE_SEARCH_RESULT_HTTP_HEADERS);
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500283 mVoiceSearchData.mSourceIsGoogle = intent.getBooleanExtra(
284 VoiceSearchData.SOURCE_IS_GOOGLE, false);
Leon Scroggins2ee4a5a2010-03-15 16:56:57 -0400285 mVoiceSearchData.mVoiceSearchIntent = new Intent(intent);
Leon Scrogginse10dde52010-03-08 19:53:03 -0500286 }
287 String extraData = intent.getStringExtra(
288 SearchManager.EXTRA_DATA_KEY);
289 if (extraData != null) {
290 index = Integer.parseInt(extraData);
291 if (index >= mVoiceSearchData.mVoiceSearchResults.size()) {
292 throw new AssertionError("index must be less than "
293 + "size of mVoiceSearchResults");
294 }
295 if (mVoiceSearchData.mSourceIsGoogle) {
296 Intent logIntent = new Intent(
297 LoggingEvents.ACTION_LOG_EVENT);
298 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
299 LoggingEvents.VoiceSearch.N_BEST_CHOOSE);
300 logIntent.putExtra(
301 LoggingEvents.VoiceSearch.EXTRA_N_BEST_CHOOSE_INDEX,
302 index);
303 mActivity.sendBroadcast(logIntent);
304 }
305 if (mVoiceSearchData.mVoiceSearchIntent != null) {
Leon Scroggins2ee4a5a2010-03-15 16:56:57 -0400306 // Copy the Intent, so that each history item will have its own
307 // Intent, with different (or none) extra data.
308 Intent latest = new Intent(mVoiceSearchData.mVoiceSearchIntent);
309 latest.putExtra(SearchManager.EXTRA_DATA_KEY, extraData);
310 mVoiceSearchData.mVoiceSearchIntent = latest;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500311 }
312 }
313 mVoiceSearchData.mLastVoiceSearchTitle
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500314 = mVoiceSearchData.mVoiceSearchResults.get(index);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500315 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700316 mWebViewController.activateVoiceSearchMode(mVoiceSearchData.mLastVoiceSearchTitle);
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 }
712 if (BrowserSettings.getInstance().showSecurityWarnings()) {
713 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 Kolb8233fac2010-10-26 16:08:53 -0700859 final Tab newTab = mWebViewController.openTabAndShow(
Michael Kolb18eb3772010-12-10 14:29:51 -0800860 Tab.this,
Michael Kolb8233fac2010-10-26 16:08:53 -0700861 IntentHandler.EMPTY_URL_DATA, false, null);
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) {
1018 BrowserSettings.getInstance().getWebStorageSizeManager()
1019 .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) {
1037 BrowserSettings.getInstance().getWebStorageSizeManager()
1038 .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.
1175 BrowserSettings s = BrowserSettings.getInstance();
1176 s.addObserver(mMainView.getSettings());
1177 s.disableAutoFill(mActivity);
1178 s.update();
1179 Toast.makeText(mActivity,
1180 R.string.autofill_setup_dialog_negative_toast,
1181 Toast.LENGTH_LONG).show();
1182 } else {
1183 // Take user to the AutoFill profile editor. When they return,
1184 // we will send the message that we pass here which will trigger
1185 // the form to get filled out with their new profile.
1186 mWebViewController.setupAutoFill(msg);
1187 }
1188 }
1189 })
1190 .setNegativeButton(R.string.cancel, null)
1191 .show();
Ben Murdoch8029a772010-11-16 11:58:21 +00001192 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001193 };
1194
1195 // -------------------------------------------------------------------------
1196 // WebViewClient implementation for the sub window
1197 // -------------------------------------------------------------------------
1198
1199 // Subclass of WebViewClient used in subwindows to notify the main
1200 // WebViewClient of certain WebView activities.
1201 private static class SubWindowClient extends WebViewClient {
1202 // The main WebViewClient.
1203 private final WebViewClient mClient;
Michael Kolb8233fac2010-10-26 16:08:53 -07001204 private final WebViewController mController;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001205
Michael Kolb8233fac2010-10-26 16:08:53 -07001206 SubWindowClient(WebViewClient client, WebViewController controller) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001207 mClient = client;
Michael Kolb8233fac2010-10-26 16:08:53 -07001208 mController = controller;
Leon Scroggins III211ba542010-04-19 13:21:13 -04001209 }
1210 @Override
1211 public void onPageStarted(WebView view, String url, Bitmap favicon) {
1212 // Unlike the others, do not call mClient's version, which would
1213 // change the progress bar. However, we do want to remove the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001214 // find or select dialog.
Michael Kolb8233fac2010-10-26 16:08:53 -07001215 mController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001216 }
1217 @Override
1218 public void doUpdateVisitedHistory(WebView view, String url,
1219 boolean isReload) {
1220 mClient.doUpdateVisitedHistory(view, url, isReload);
1221 }
1222 @Override
1223 public boolean shouldOverrideUrlLoading(WebView view, String url) {
1224 return mClient.shouldOverrideUrlLoading(view, url);
1225 }
1226 @Override
1227 public void onReceivedSslError(WebView view, SslErrorHandler handler,
1228 SslError error) {
1229 mClient.onReceivedSslError(view, handler, error);
1230 }
1231 @Override
1232 public void onReceivedHttpAuthRequest(WebView view,
1233 HttpAuthHandler handler, String host, String realm) {
1234 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
1235 }
1236 @Override
1237 public void onFormResubmission(WebView view, Message dontResend,
1238 Message resend) {
1239 mClient.onFormResubmission(view, dontResend, resend);
1240 }
1241 @Override
1242 public void onReceivedError(WebView view, int errorCode,
1243 String description, String failingUrl) {
1244 mClient.onReceivedError(view, errorCode, description, failingUrl);
1245 }
1246 @Override
1247 public boolean shouldOverrideKeyEvent(WebView view,
1248 android.view.KeyEvent event) {
1249 return mClient.shouldOverrideKeyEvent(view, event);
1250 }
1251 @Override
1252 public void onUnhandledKeyEvent(WebView view,
1253 android.view.KeyEvent event) {
1254 mClient.onUnhandledKeyEvent(view, event);
1255 }
1256 }
1257
1258 // -------------------------------------------------------------------------
1259 // WebChromeClient implementation for the sub window
1260 // -------------------------------------------------------------------------
1261
1262 private class SubWindowChromeClient extends WebChromeClient {
1263 // The main WebChromeClient.
1264 private final WebChromeClient mClient;
1265
1266 SubWindowChromeClient(WebChromeClient client) {
1267 mClient = client;
1268 }
1269 @Override
1270 public void onProgressChanged(WebView view, int newProgress) {
1271 mClient.onProgressChanged(view, newProgress);
1272 }
1273 @Override
1274 public boolean onCreateWindow(WebView view, boolean dialog,
1275 boolean userGesture, android.os.Message resultMsg) {
1276 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
1277 }
1278 @Override
1279 public void onCloseWindow(WebView window) {
1280 if (window != mSubView) {
1281 Log.e(LOGTAG, "Can't close the window");
1282 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001283 mWebViewController.dismissSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001284 }
1285 }
1286
1287 // -------------------------------------------------------------------------
1288
Michael Kolb8233fac2010-10-26 16:08:53 -07001289 // TODO temporarily use activity here
1290 // remove later
1291
Grace Kloba22ac16e2009-10-07 18:00:23 -07001292 // Construct a new tab
Michael Kolb8233fac2010-10-26 16:08:53 -07001293 Tab(WebViewController wvcontroller, WebView w, boolean closeOnExit, String appId,
Grace Kloba22ac16e2009-10-07 18:00:23 -07001294 String url) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001295 mWebViewController = wvcontroller;
1296 mActivity = mWebViewController.getActivity();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001297 mCloseOnExit = closeOnExit;
1298 mAppId = appId;
John Recke969cc52010-12-21 17:24:43 -08001299 mDataController = DataController.getInstance(mActivity);
John Reck30c714c2010-12-16 17:30:34 -08001300 mCurrentState = new PageState(mActivity, w.isPrivateBrowsingEnabled());
Michael Kolb8233fac2010-10-26 16:08:53 -07001301 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001302 mInForeground = false;
1303
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001304 mDownloadListener = new DownloadListener() {
1305 public void onDownloadStart(String url, String userAgent,
1306 String contentDisposition, String mimetype,
1307 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001308 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001309 mimetype, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001310 }
1311 };
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001312 mWebBackForwardListClient = new WebBackForwardListClient() {
1313 @Override
1314 public void onNewHistoryItem(WebHistoryItem item) {
1315 if (isInVoiceSearchMode()) {
1316 item.setCustomData(mVoiceSearchData.mVoiceSearchIntent);
1317 }
1318 }
1319 @Override
1320 public void onIndexChanged(WebHistoryItem item, int index) {
1321 Object data = item.getCustomData();
1322 if (data != null && data instanceof Intent) {
1323 activateVoiceSearchMode((Intent) data);
1324 }
1325 }
1326 };
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001327
Grace Kloba22ac16e2009-10-07 18:00:23 -07001328 setWebView(w);
1329 }
1330
1331 /**
1332 * Sets the WebView for this tab, correctly removing the old WebView from
1333 * the container view.
1334 */
1335 void setWebView(WebView w) {
1336 if (mMainView == w) {
1337 return;
1338 }
Michael Kolba713ec82010-11-29 17:27:06 -08001339
Grace Kloba22ac16e2009-10-07 18:00:23 -07001340 // If the WebView is changing, the page will be reloaded, so any ongoing
1341 // Geolocation permission requests are void.
Grace Kloba50c241e2010-04-20 11:07:50 -07001342 if (mGeolocationPermissionsPrompt != null) {
1343 mGeolocationPermissionsPrompt.hide();
1344 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001345
Michael Kolba713ec82010-11-29 17:27:06 -08001346 mWebViewController.onSetWebView(this, w);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001347
1348 // set the new one
1349 mMainView = w;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001350 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001351 if (mMainView != null) {
1352 mMainView.setWebViewClient(mWebViewClient);
1353 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001354 // Attach DownloadManager so that downloads can start in an active
1355 // or a non-active window. This can happen when going to a site that
1356 // does a redirect after a period of time. The user could have
1357 // switched to another tab while waiting for the download to start.
1358 mMainView.setDownloadListener(mDownloadListener);
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001359 mMainView.setWebBackForwardListClient(mWebBackForwardListClient);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001360 }
1361 }
1362
1363 /**
1364 * Destroy the tab's main WebView and subWindow if any
1365 */
1366 void destroy() {
1367 if (mMainView != null) {
1368 dismissSubWindow();
1369 BrowserSettings.getInstance().deleteObserver(mMainView.getSettings());
1370 // save the WebView to call destroy() after detach it from the tab
1371 WebView webView = mMainView;
1372 setWebView(null);
1373 webView.destroy();
1374 }
1375 }
1376
1377 /**
1378 * Remove the tab from the parent
1379 */
1380 void removeFromTree() {
1381 // detach the children
1382 if (mChildTabs != null) {
1383 for(Tab t : mChildTabs) {
1384 t.setParentTab(null);
1385 }
1386 }
1387 // remove itself from the parent list
1388 if (mParentTab != null) {
1389 mParentTab.mChildTabs.remove(this);
1390 }
1391 }
1392
1393 /**
1394 * Create a new subwindow unless a subwindow already exists.
1395 * @return True if a new subwindow was created. False if one already exists.
1396 */
1397 boolean createSubWindow() {
1398 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001399 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001400 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001401 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001402 mSubView.setWebChromeClient(new SubWindowChromeClient(
1403 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001404 // Set a different DownloadListener for the mSubView, since it will
1405 // just need to dismiss the mSubView, rather than close the Tab
1406 mSubView.setDownloadListener(new DownloadListener() {
1407 public void onDownloadStart(String url, String userAgent,
1408 String contentDisposition, String mimetype,
1409 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001410 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001411 contentDisposition, mimetype, contentLength);
1412 if (mSubView.copyBackForwardList().getSize() == 0) {
1413 // This subwindow was opened for the sole purpose of
1414 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001415 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001416 }
1417 }
1418 });
Grace Kloba22ac16e2009-10-07 18:00:23 -07001419 mSubView.setOnCreateContextMenuListener(mActivity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001420 return true;
1421 }
1422 return false;
1423 }
1424
1425 /**
1426 * Dismiss the subWindow for the tab.
1427 */
1428 void dismissSubWindow() {
1429 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001430 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001431 BrowserSettings.getInstance().deleteObserver(
1432 mSubView.getSettings());
1433 mSubView.destroy();
1434 mSubView = null;
1435 mSubViewContainer = null;
1436 }
1437 }
1438
Grace Kloba22ac16e2009-10-07 18:00:23 -07001439
1440 /**
1441 * Set the parent tab of this tab.
1442 */
1443 void setParentTab(Tab parent) {
1444 mParentTab = parent;
1445 // This tab may have been freed due to low memory. If that is the case,
1446 // the parent tab index is already saved. If we are changing that index
1447 // (most likely due to removing the parent tab) we must update the
1448 // parent tab index in the saved Bundle.
1449 if (mSavedState != null) {
1450 if (parent == null) {
1451 mSavedState.remove(PARENTTAB);
1452 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -07001453 mSavedState.putInt(PARENTTAB, mWebViewController.getTabControl()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001454 .getTabIndex(parent));
1455 }
1456 }
1457 }
1458
1459 /**
1460 * When a Tab is created through the content of another Tab, then we
1461 * associate the Tabs.
1462 * @param child the Tab that was created from this Tab
1463 */
1464 void addChildTab(Tab child) {
1465 if (mChildTabs == null) {
1466 mChildTabs = new Vector<Tab>();
1467 }
1468 mChildTabs.add(child);
1469 child.setParentTab(this);
1470 }
1471
1472 Vector<Tab> getChildTabs() {
1473 return mChildTabs;
1474 }
1475
1476 void resume() {
1477 if (mMainView != null) {
1478 mMainView.onResume();
1479 if (mSubView != null) {
1480 mSubView.onResume();
1481 }
1482 }
1483 }
1484
1485 void pause() {
1486 if (mMainView != null) {
1487 mMainView.onPause();
1488 if (mSubView != null) {
1489 mSubView.onPause();
1490 }
1491 }
1492 }
1493
1494 void putInForeground() {
1495 mInForeground = true;
1496 resume();
1497 mMainView.setOnCreateContextMenuListener(mActivity);
1498 if (mSubView != null) {
1499 mSubView.setOnCreateContextMenuListener(mActivity);
1500 }
1501 // Show the pending error dialog if the queue is not empty
1502 if (mQueuedErrors != null && mQueuedErrors.size() > 0) {
1503 showError(mQueuedErrors.getFirst());
1504 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001505 mWebViewController.bookmarkedStatusHasChanged(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001506 }
1507
1508 void putInBackground() {
1509 mInForeground = false;
1510 pause();
1511 mMainView.setOnCreateContextMenuListener(null);
1512 if (mSubView != null) {
1513 mSubView.setOnCreateContextMenuListener(null);
1514 }
1515 }
1516
Michael Kolb8233fac2010-10-26 16:08:53 -07001517 boolean inForeground() {
1518 return mInForeground;
1519 }
1520
Grace Kloba22ac16e2009-10-07 18:00:23 -07001521 /**
1522 * Return the top window of this tab; either the subwindow if it is not
1523 * null or the main window.
1524 * @return The top window of this tab.
1525 */
1526 WebView getTopWindow() {
1527 if (mSubView != null) {
1528 return mSubView;
1529 }
1530 return mMainView;
1531 }
1532
1533 /**
1534 * Return the main window of this tab. Note: if a tab is freed in the
1535 * background, this can return null. It is only guaranteed to be
1536 * non-null for the current tab.
1537 * @return The main WebView of this tab.
1538 */
1539 WebView getWebView() {
1540 return mMainView;
1541 }
1542
Michael Kolba713ec82010-11-29 17:27:06 -08001543 void setViewContainer(View container) {
1544 mContainer = container;
1545 }
1546
Michael Kolb8233fac2010-10-26 16:08:53 -07001547 View getViewContainer() {
1548 return mContainer;
1549 }
1550
Grace Kloba22ac16e2009-10-07 18:00:23 -07001551 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001552 * Return whether private browsing is enabled for the main window of
1553 * this tab.
1554 * @return True if private browsing is enabled.
1555 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001556 boolean isPrivateBrowsingEnabled() {
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001557 WebView webView = getWebView();
1558 if (webView == null) {
1559 return false;
1560 }
1561 return webView.isPrivateBrowsingEnabled();
1562 }
1563
1564 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001565 * Return the subwindow of this tab or null if there is no subwindow.
1566 * @return The subwindow of this tab or null.
1567 */
1568 WebView getSubWebView() {
1569 return mSubView;
1570 }
1571
Michael Kolb1514bb72010-11-22 09:11:48 -08001572 void setSubWebView(WebView subView) {
1573 mSubView = subView;
1574 }
1575
Michael Kolb8233fac2010-10-26 16:08:53 -07001576 View getSubViewContainer() {
1577 return mSubViewContainer;
1578 }
1579
Michael Kolb1514bb72010-11-22 09:11:48 -08001580 void setSubViewContainer(View subViewContainer) {
1581 mSubViewContainer = subViewContainer;
1582 }
1583
Grace Kloba22ac16e2009-10-07 18:00:23 -07001584 /**
1585 * @return The geolocation permissions prompt for this tab.
1586 */
1587 GeolocationPermissionsPrompt getGeolocationPermissionsPrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001588 if (mGeolocationPermissionsPrompt == null) {
1589 ViewStub stub = (ViewStub) mContainer
1590 .findViewById(R.id.geolocation_permissions_prompt);
1591 mGeolocationPermissionsPrompt = (GeolocationPermissionsPrompt) stub
1592 .inflate();
1593 mGeolocationPermissionsPrompt.init();
1594 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001595 return mGeolocationPermissionsPrompt;
1596 }
1597
1598 /**
1599 * @return The application id string
1600 */
1601 String getAppId() {
1602 return mAppId;
1603 }
1604
1605 /**
1606 * Set the application id string
1607 * @param id
1608 */
1609 void setAppId(String id) {
1610 mAppId = id;
1611 }
1612
Grace Kloba22ac16e2009-10-07 18:00:23 -07001613 String getUrl() {
John Reck324d4402011-01-11 16:56:42 -08001614 return UrlUtils.filteredUrl(mCurrentState.mUrl);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001615 }
1616
John Reck49a603c2011-03-03 09:33:05 -08001617 String getOriginalUrl() {
1618 if (mMainView == null) {
1619 return "";
1620 }
1621 return UrlUtils.filteredUrl(mMainView.getOriginalUrl());
1622 }
1623
Grace Kloba22ac16e2009-10-07 18:00:23 -07001624 /**
John Reck30c714c2010-12-16 17:30:34 -08001625 * Get the title of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001626 */
1627 String getTitle() {
John Reck30c714c2010-12-16 17:30:34 -08001628 if (mCurrentState.mTitle == null && mInPageLoad) {
1629 return mActivity.getString(R.string.title_bar_loading);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001630 }
John Reck30c714c2010-12-16 17:30:34 -08001631 return mCurrentState.mTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001632 }
1633
1634 /**
John Reck30c714c2010-12-16 17:30:34 -08001635 * Get the favicon of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001636 */
1637 Bitmap getFavicon() {
John Reck30c714c2010-12-16 17:30:34 -08001638 return mCurrentState.mFavicon;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001639 }
1640
John Recke969cc52010-12-21 17:24:43 -08001641 public boolean isBookmarkedSite() {
1642 return mCurrentState.mIsBookmarkedSite;
1643 }
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001644
Grace Kloba22ac16e2009-10-07 18:00:23 -07001645 /**
1646 * Return the tab's error console. Creates the console if createIfNEcessary
1647 * is true and we haven't already created the console.
1648 * @param createIfNecessary Flag to indicate if the console should be
1649 * created if it has not been already.
1650 * @return The tab's error console, or null if one has not been created and
1651 * createIfNecessary is false.
1652 */
1653 ErrorConsoleView getErrorConsole(boolean createIfNecessary) {
1654 if (createIfNecessary && mErrorConsole == null) {
1655 mErrorConsole = new ErrorConsoleView(mActivity);
1656 mErrorConsole.setWebView(mMainView);
1657 }
1658 return mErrorConsole;
1659 }
1660
1661 /**
1662 * If this Tab was created through another Tab, then this method returns
1663 * that Tab.
1664 * @return the Tab parent or null
1665 */
1666 public Tab getParentTab() {
1667 return mParentTab;
1668 }
1669
1670 /**
1671 * Return whether this tab should be closed when it is backing out of the
1672 * first page.
1673 * @return TRUE if this tab should be closed when exit.
1674 */
1675 boolean closeOnExit() {
1676 return mCloseOnExit;
1677 }
1678
John Reck30c714c2010-12-16 17:30:34 -08001679 private void setLockIconType(LockIcon icon) {
1680 mCurrentState.mLockIcon = icon;
1681 mWebViewController.onUpdatedLockIcon(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001682 }
1683
1684 /**
1685 * @return The tab's lock icon type.
1686 */
John Reck30c714c2010-12-16 17:30:34 -08001687 LockIcon getLockIconType() {
1688 return mCurrentState.mLockIcon;
1689 }
1690
1691 int getLoadProgress() {
1692 if (mInPageLoad) {
1693 return mPageLoadProgress;
1694 }
1695 return 100;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001696 }
1697
1698 /**
1699 * @return TRUE if onPageStarted is called while onPageFinished is not
1700 * called yet.
1701 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001702 boolean inPageLoad() {
1703 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001704 }
1705
1706 // force mInLoad to be false. This should only be called before closing the
1707 // tab to ensure BrowserActivity's pauseWebViewTimers() is called correctly.
Michael Kolb8233fac2010-10-26 16:08:53 -07001708 void clearInPageLoad() {
1709 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001710 }
1711
Grace Kloba22ac16e2009-10-07 18:00:23 -07001712 /**
John Reck30c714c2010-12-16 17:30:34 -08001713 * Get the cached saved state bundle.
1714 * @return cached state bundle
Grace Kloba22ac16e2009-10-07 18:00:23 -07001715 */
1716 Bundle getSavedState() {
1717 return mSavedState;
1718 }
1719
1720 /**
1721 * Set the saved state.
1722 */
1723 void setSavedState(Bundle state) {
1724 mSavedState = state;
1725 }
1726
1727 /**
1728 * @return TRUE if succeed in saving the state.
1729 */
1730 boolean saveState() {
1731 // If the WebView is null it means we ran low on memory and we already
1732 // stored the saved state in mSavedState.
1733 if (mMainView == null) {
1734 return mSavedState != null;
1735 }
1736
1737 mSavedState = new Bundle();
1738 final WebBackForwardList list = mMainView.saveState(mSavedState);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001739
1740 // Store some extra info for displaying the tab in the picker.
1741 final WebHistoryItem item = list != null ? list.getCurrentItem() : null;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001742
John Reck30c714c2010-12-16 17:30:34 -08001743 mSavedState.putString(CURRURL, mCurrentState.mUrl);
1744 mSavedState.putString(CURRTITLE, mCurrentState.mTitle);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001745 mSavedState.putBoolean(CLOSEONEXIT, mCloseOnExit);
1746 if (mAppId != null) {
1747 mSavedState.putString(APPID, mAppId);
1748 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001749 // Remember the parent tab so the relationship can be restored.
1750 if (mParentTab != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001751 mSavedState.putInt(PARENTTAB, mWebViewController.getTabControl().getTabIndex(
Grace Kloba22ac16e2009-10-07 18:00:23 -07001752 mParentTab));
1753 }
Michael Kolbeb95db42011-03-03 10:38:40 -08001754 if (mScreenshot != null) {
1755 mSavedState.putParcelable(SCREENSHOT, mScreenshot);
1756 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001757 return true;
1758 }
1759
1760 /*
1761 * Restore the state of the tab.
1762 */
1763 boolean restoreState(Bundle b) {
1764 if (b == null) {
1765 return false;
1766 }
1767 // Restore the internal state even if the WebView fails to restore.
1768 // This will maintain the app id, original url and close-on-exit values.
1769 mSavedState = null;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001770 mCloseOnExit = b.getBoolean(CLOSEONEXIT);
1771 mAppId = b.getString(APPID);
Michael Kolbeb95db42011-03-03 10:38:40 -08001772 mScreenshot = b.getParcelable(SCREENSHOT);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001773
1774 final WebBackForwardList list = mMainView.restoreState(b);
1775 if (list == null) {
1776 return false;
1777 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001778 return true;
1779 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04001780
Leon Scroggins1961ed22010-12-07 15:22:21 -05001781 public void updateBookmarkedStatus() {
John Recke969cc52010-12-21 17:24:43 -08001782 mDataController.queryBookmarkStatus(getUrl(), mIsBookmarkCallback);
Leon Scroggins1961ed22010-12-07 15:22:21 -05001783 }
1784
John Recke969cc52010-12-21 17:24:43 -08001785 private DataController.OnQueryUrlIsBookmark mIsBookmarkCallback
1786 = new DataController.OnQueryUrlIsBookmark() {
1787 @Override
1788 public void onQueryUrlIsBookmark(String url, boolean isBookmark) {
1789 if (mCurrentState.mUrl.equals(url)) {
1790 mCurrentState.mIsBookmarkedSite = isBookmark;
1791 mWebViewController.bookmarkedStatusHasChanged(Tab.this);
1792 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001793 }
John Recke969cc52010-12-21 17:24:43 -08001794 };
Michael Kolb1acef692011-03-08 14:12:06 -08001795
Michael Kolbeb95db42011-03-03 10:38:40 -08001796 public void setScreenshot(Bitmap screenshot) {
1797 mScreenshot = screenshot;
1798 }
1799
1800 public Bitmap getScreenshot() {
1801 return mScreenshot;
1802 }
1803
Grace Kloba22ac16e2009-10-07 18:00:23 -07001804}