blob: 2d194f0c4f3c216a82a139f5a471ce3002504a75 [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
John Reck30c714c2010-12-16 17:30:34 -0800141 // All the state needed for a page
142 private static class PageState {
143 String mUrl;
144 String mTitle;
145 LockIcon mLockIcon;
146 Bitmap mFavicon;
John Recke969cc52010-12-21 17:24:43 -0800147 Boolean mIsBookmarkedSite = false;
John Reck30c714c2010-12-16 17:30:34 -0800148
149 PageState(Context c, boolean incognito) {
150 if (incognito) {
151 mUrl = "browser:incognito";
152 mTitle = c.getString(R.string.new_incognito_tab);
John Reck30c714c2010-12-16 17:30:34 -0800153 } else {
154 mUrl = "";
155 mTitle = c.getString(R.string.new_tab);
John Reck30c714c2010-12-16 17:30:34 -0800156 }
Justin Hodc6cbb72011-01-24 15:14:54 -0800157 mFavicon = BitmapFactory.decodeResource(
158 c.getResources(), R.drawable.app_web_browser_sm);
John Reck30c714c2010-12-16 17:30:34 -0800159 mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
160 }
161
162 PageState(Context c, boolean incognito, String url, Bitmap favicon) {
163 mUrl = url;
164 mTitle = null;
165 if (URLUtil.isHttpsUrl(url)) {
166 mLockIcon = LockIcon.LOCK_ICON_SECURE;
167 } else {
168 mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
169 }
170 if (favicon != null) {
171 mFavicon = favicon;
172 } else {
Justin Hodc6cbb72011-01-24 15:14:54 -0800173 mFavicon = BitmapFactory.decodeResource(
174 c.getResources(), R.drawable.app_web_browser_sm);
John Reck30c714c2010-12-16 17:30:34 -0800175 }
176 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700177 }
178
John Reck30c714c2010-12-16 17:30:34 -0800179 // The current/loading page's state
180 private PageState mCurrentState;
181
Grace Kloba22ac16e2009-10-07 18:00:23 -0700182 // Used for saving and restoring each Tab
John Reck30c714c2010-12-16 17:30:34 -0800183 // TODO: Figure out who uses what and where
184 // Some of these aren't use in this class, and some are only used in
185 // restoring state but not saving it - FIX THIS
Grace Kloba22ac16e2009-10-07 18:00:23 -0700186 static final String WEBVIEW = "webview";
187 static final String NUMTABS = "numTabs";
188 static final String CURRTAB = "currentTab";
189 static final String CURRURL = "currentUrl";
190 static final String CURRTITLE = "currentTitle";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700191 static final String CLOSEONEXIT = "closeonexit";
192 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";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700196
197 // -------------------------------------------------------------------------
198
Leon Scroggins58d56c62010-01-28 15:12:40 -0500199 /**
200 * Private information regarding the latest voice search. If the Tab is not
201 * in voice search mode, this will be null.
202 */
203 private VoiceSearchData mVoiceSearchData;
204 /**
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400205 * Remove voice search mode from this tab.
206 */
207 public void revertVoiceSearchMode() {
208 if (mVoiceSearchData != null) {
209 mVoiceSearchData = null;
210 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700211 mWebViewController.revertVoiceSearchMode(this);
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400212 }
213 }
214 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700215
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400216 /**
Leon Scroggins58d56c62010-01-28 15:12:40 -0500217 * Return whether the tab is in voice search mode.
218 */
219 public boolean isInVoiceSearchMode() {
220 return mVoiceSearchData != null;
221 }
222 /**
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400223 * Return true if the Tab is in voice search mode and the voice search
224 * Intent came with a String identifying that Google provided the Intent.
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500225 */
226 public boolean voiceSearchSourceIsGoogle() {
227 return mVoiceSearchData != null && mVoiceSearchData.mSourceIsGoogle;
228 }
229 /**
Leon Scroggins58d56c62010-01-28 15:12:40 -0500230 * Get the title to display for the current voice search page. If the Tab
231 * is not in voice search mode, return null.
232 */
233 public String getVoiceDisplayTitle() {
234 if (mVoiceSearchData == null) return null;
235 return mVoiceSearchData.mLastVoiceSearchTitle;
236 }
237 /**
238 * Get the latest array of voice search results, to be passed to the
239 * BrowserProvider. If the Tab is not in voice search mode, return null.
240 */
241 public ArrayList<String> getVoiceSearchResults() {
242 if (mVoiceSearchData == null) return null;
243 return mVoiceSearchData.mVoiceSearchResults;
244 }
245 /**
246 * Activate voice search mode.
247 * @param intent Intent which has the results to use, or an index into the
248 * results when reusing the old results.
249 */
250 /* package */ void activateVoiceSearchMode(Intent intent) {
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500251 int index = 0;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500252 ArrayList<String> results = intent.getStringArrayListExtra(
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -0500253 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_STRINGS);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500254 if (results != null) {
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500255 ArrayList<String> urls = intent.getStringArrayListExtra(
256 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_URLS);
257 ArrayList<String> htmls = intent.getStringArrayListExtra(
258 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_HTML);
259 ArrayList<String> baseUrls = intent.getStringArrayListExtra(
260 RecognizerResultsIntent
261 .EXTRA_VOICE_SEARCH_RESULT_HTML_BASE_URLS);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500262 // This tab is now entering voice search mode for the first time, or
263 // a new voice search was done.
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500264 int size = results.size();
265 if (urls == null || size != urls.size()) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500266 throw new AssertionError("improper extras passed in Intent");
267 }
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500268 if (htmls == null || htmls.size() != size || baseUrls == null ||
269 (baseUrls.size() != size && baseUrls.size() != 1)) {
270 // If either of these arrays are empty/incorrectly sized, ignore
271 // them.
272 htmls = null;
273 baseUrls = null;
274 }
275 mVoiceSearchData = new VoiceSearchData(results, urls, htmls,
276 baseUrls);
Leon Scroggins9df94972010-03-08 18:20:35 -0500277 mVoiceSearchData.mHeaders = intent.getParcelableArrayListExtra(
278 RecognizerResultsIntent
279 .EXTRA_VOICE_SEARCH_RESULT_HTTP_HEADERS);
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500280 mVoiceSearchData.mSourceIsGoogle = intent.getBooleanExtra(
281 VoiceSearchData.SOURCE_IS_GOOGLE, false);
Leon Scroggins2ee4a5a2010-03-15 16:56:57 -0400282 mVoiceSearchData.mVoiceSearchIntent = new Intent(intent);
Leon Scrogginse10dde52010-03-08 19:53:03 -0500283 }
284 String extraData = intent.getStringExtra(
285 SearchManager.EXTRA_DATA_KEY);
286 if (extraData != null) {
287 index = Integer.parseInt(extraData);
288 if (index >= mVoiceSearchData.mVoiceSearchResults.size()) {
289 throw new AssertionError("index must be less than "
290 + "size of mVoiceSearchResults");
291 }
292 if (mVoiceSearchData.mSourceIsGoogle) {
293 Intent logIntent = new Intent(
294 LoggingEvents.ACTION_LOG_EVENT);
295 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
296 LoggingEvents.VoiceSearch.N_BEST_CHOOSE);
297 logIntent.putExtra(
298 LoggingEvents.VoiceSearch.EXTRA_N_BEST_CHOOSE_INDEX,
299 index);
300 mActivity.sendBroadcast(logIntent);
301 }
302 if (mVoiceSearchData.mVoiceSearchIntent != null) {
Leon Scroggins2ee4a5a2010-03-15 16:56:57 -0400303 // Copy the Intent, so that each history item will have its own
304 // Intent, with different (or none) extra data.
305 Intent latest = new Intent(mVoiceSearchData.mVoiceSearchIntent);
306 latest.putExtra(SearchManager.EXTRA_DATA_KEY, extraData);
307 mVoiceSearchData.mVoiceSearchIntent = latest;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500308 }
309 }
310 mVoiceSearchData.mLastVoiceSearchTitle
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500311 = mVoiceSearchData.mVoiceSearchResults.get(index);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500312 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700313 mWebViewController.activateVoiceSearchMode(mVoiceSearchData.mLastVoiceSearchTitle);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500314 }
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500315 if (mVoiceSearchData.mVoiceSearchHtmls != null) {
316 // When index was found it was already ensured that it was valid
317 String uriString = mVoiceSearchData.mVoiceSearchHtmls.get(index);
318 if (uriString != null) {
319 Uri dataUri = Uri.parse(uriString);
320 if (RecognizerResultsIntent.URI_SCHEME_INLINE.equals(
321 dataUri.getScheme())) {
322 // If there is only one base URL, use it. If there are
323 // more, there will be one for each index, so use the base
324 // URL corresponding to the index.
325 String baseUrl = mVoiceSearchData.mVoiceSearchBaseUrls.get(
326 mVoiceSearchData.mVoiceSearchBaseUrls.size() > 1 ?
327 index : 0);
328 mVoiceSearchData.mLastVoiceSearchUrl = baseUrl;
329 mMainView.loadDataWithBaseURL(baseUrl,
330 uriString.substring(RecognizerResultsIntent
331 .URI_SCHEME_INLINE.length() + 1), "text/html",
332 "utf-8", baseUrl);
333 return;
334 }
335 }
336 }
Leon Scroggins58d56c62010-01-28 15:12:40 -0500337 mVoiceSearchData.mLastVoiceSearchUrl
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500338 = mVoiceSearchData.mVoiceSearchUrls.get(index);
339 if (null == mVoiceSearchData.mLastVoiceSearchUrl) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700340 mVoiceSearchData.mLastVoiceSearchUrl = UrlUtils.smartUrlFilter(
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500341 mVoiceSearchData.mLastVoiceSearchTitle);
342 }
Leon Scroggins9df94972010-03-08 18:20:35 -0500343 Map<String, String> headers = null;
344 if (mVoiceSearchData.mHeaders != null) {
345 int bundleIndex = mVoiceSearchData.mHeaders.size() == 1 ? 0
346 : index;
347 Bundle bundle = mVoiceSearchData.mHeaders.get(bundleIndex);
348 if (bundle != null && !bundle.isEmpty()) {
349 Iterator<String> iter = bundle.keySet().iterator();
350 headers = new HashMap<String, String>();
351 while (iter.hasNext()) {
352 String key = iter.next();
353 headers.put(key, bundle.getString(key));
354 }
355 }
356 }
357 mMainView.loadUrl(mVoiceSearchData.mLastVoiceSearchUrl, headers);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500358 }
359 /* package */ static class VoiceSearchData {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500360 public VoiceSearchData(ArrayList<String> results,
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500361 ArrayList<String> urls, ArrayList<String> htmls,
362 ArrayList<String> baseUrls) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500363 mVoiceSearchResults = results;
364 mVoiceSearchUrls = urls;
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500365 mVoiceSearchHtmls = htmls;
366 mVoiceSearchBaseUrls = baseUrls;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500367 }
368 /*
369 * ArrayList of suggestions to be displayed when opening the
370 * SearchManager
371 */
372 public ArrayList<String> mVoiceSearchResults;
373 /*
374 * ArrayList of urls, associated with the suggestions in
375 * mVoiceSearchResults.
376 */
377 public ArrayList<String> mVoiceSearchUrls;
378 /*
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500379 * ArrayList holding content to load for each item in
380 * mVoiceSearchResults.
381 */
382 public ArrayList<String> mVoiceSearchHtmls;
383 /*
384 * ArrayList holding base urls for the items in mVoiceSearchResults.
385 * If non null, this will either have the same size as
386 * mVoiceSearchResults or have a size of 1, in which case all will use
387 * the same base url
388 */
389 public ArrayList<String> mVoiceSearchBaseUrls;
390 /*
Leon Scroggins58d56c62010-01-28 15:12:40 -0500391 * The last url provided by voice search. Used for comparison to see if
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500392 * we are going to a page by some method besides voice search.
Leon Scroggins58d56c62010-01-28 15:12:40 -0500393 */
394 public String mLastVoiceSearchUrl;
395 /**
396 * The last title used for voice search. Needed to update the title bar
397 * when switching tabs.
398 */
399 public String mLastVoiceSearchTitle;
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500400 /**
401 * Whether the Intent which turned on voice search mode contained the
402 * String signifying that Google was the source.
403 */
404 public boolean mSourceIsGoogle;
405 /**
Leon Scroggins9df94972010-03-08 18:20:35 -0500406 * List of headers to be passed into the WebView containing location
407 * information
408 */
409 public ArrayList<Bundle> mHeaders;
410 /**
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500411 * The Intent used to invoke voice search. Placed on the
412 * WebHistoryItem so that when coming back to a previous voice search
413 * page we can again activate voice search.
414 */
Leon Scrogginse10dde52010-03-08 19:53:03 -0500415 public Intent mVoiceSearchIntent;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500416 /**
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500417 * String used to identify Google as the source of voice search.
418 */
419 public static String SOURCE_IS_GOOGLE
420 = "android.speech.extras.SOURCE_IS_GOOGLE";
Leon Scroggins58d56c62010-01-28 15:12:40 -0500421 }
422
Grace Kloba22ac16e2009-10-07 18:00:23 -0700423 // Container class for the next error dialog that needs to be displayed
424 private class ErrorDialog {
425 public final int mTitle;
426 public final String mDescription;
427 public final int mError;
428 ErrorDialog(int title, String desc, int error) {
429 mTitle = title;
430 mDescription = desc;
431 mError = error;
432 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700433 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700434
435 private void processNextError() {
436 if (mQueuedErrors == null) {
437 return;
438 }
439 // The first one is currently displayed so just remove it.
440 mQueuedErrors.removeFirst();
441 if (mQueuedErrors.size() == 0) {
442 mQueuedErrors = null;
443 return;
444 }
445 showError(mQueuedErrors.getFirst());
446 }
447
448 private DialogInterface.OnDismissListener mDialogListener =
449 new DialogInterface.OnDismissListener() {
450 public void onDismiss(DialogInterface d) {
451 processNextError();
452 }
453 };
454 private LinkedList<ErrorDialog> mQueuedErrors;
455
456 private void queueError(int err, String desc) {
457 if (mQueuedErrors == null) {
458 mQueuedErrors = new LinkedList<ErrorDialog>();
459 }
460 for (ErrorDialog d : mQueuedErrors) {
461 if (d.mError == err) {
462 // Already saw a similar error, ignore the new one.
463 return;
464 }
465 }
466 ErrorDialog errDialog = new ErrorDialog(
467 err == WebViewClient.ERROR_FILE_NOT_FOUND ?
468 R.string.browserFrameFileErrorLabel :
469 R.string.browserFrameNetworkErrorLabel,
470 desc, err);
471 mQueuedErrors.addLast(errDialog);
472
473 // Show the dialog now if the queue was empty and it is in foreground
474 if (mQueuedErrors.size() == 1 && mInForeground) {
475 showError(errDialog);
476 }
477 }
478
479 private void showError(ErrorDialog errDialog) {
480 if (mInForeground) {
481 AlertDialog d = new AlertDialog.Builder(mActivity)
482 .setTitle(errDialog.mTitle)
483 .setMessage(errDialog.mDescription)
484 .setPositiveButton(R.string.ok, null)
485 .create();
486 d.setOnDismissListener(mDialogListener);
487 d.show();
488 }
489 }
490
491 // -------------------------------------------------------------------------
492 // WebViewClient implementation for the main WebView
493 // -------------------------------------------------------------------------
494
495 private final WebViewClient mWebViewClient = new WebViewClient() {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500496 private Message mDontResend;
497 private Message mResend;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700498 @Override
499 public void onPageStarted(WebView view, String url, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700500 mInPageLoad = true;
John Reck30c714c2010-12-16 17:30:34 -0800501 mPageLoadProgress = 0;
502 mCurrentState = new PageState(mActivity,
503 view.isPrivateBrowsingEnabled(), url, favicon);
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000504 mLoadStartTime = SystemClock.uptimeMillis();
Leon Scroggins58d56c62010-01-28 15:12:40 -0500505 if (mVoiceSearchData != null
506 && !url.equals(mVoiceSearchData.mLastVoiceSearchUrl)) {
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500507 if (mVoiceSearchData.mSourceIsGoogle) {
508 Intent i = new Intent(LoggingEvents.ACTION_LOG_EVENT);
509 i.putExtra(LoggingEvents.EXTRA_FLUSH, true);
510 mActivity.sendBroadcast(i);
511 }
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400512 revertVoiceSearchMode();
Leon Scroggins58d56c62010-01-28 15:12:40 -0500513 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700514
Grace Kloba22ac16e2009-10-07 18:00:23 -0700515
516 // If we start a touch icon load and then load a new page, we don't
517 // want to cancel the current touch icon loader. But, we do want to
518 // create a new one when the touch icon url is known.
519 if (mTouchIconLoader != null) {
520 mTouchIconLoader.mTab = null;
521 mTouchIconLoader = null;
522 }
523
524 // reset the error console
525 if (mErrorConsole != null) {
526 mErrorConsole.clearErrorMessages();
Michael Kolb8233fac2010-10-26 16:08:53 -0700527 if (mWebViewController.shouldShowErrorConsole()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700528 mErrorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
529 }
530 }
531
Patrick Scott92066772011-03-10 08:46:27 -0500532 // Cancel the auto-login process.
533 if (mDeviceAccountLogin != null) {
534 mDeviceAccountLogin.cancel();
535 mDeviceAccountLogin = null;
536 mWebViewController.hideAutoLogin(Tab.this);
537 }
538
Grace Kloba22ac16e2009-10-07 18:00:23 -0700539 // finally update the UI in the activity if it is in the foreground
John Reck324d4402011-01-11 16:56:42 -0800540 mWebViewController.onPageStarted(Tab.this, view, favicon);
Leon Scroggins4cd97792010-12-03 15:31:56 -0500541
John Recke969cc52010-12-21 17:24:43 -0800542 updateBookmarkedStatus();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700543 }
544
545 @Override
546 public void onPageFinished(WebView view, String url) {
John Reck5b691842010-11-29 11:21:13 -0800547 if (!isPrivateBrowsingEnabled()) {
548 LogTag.logPageFinishedLoading(
549 url, SystemClock.uptimeMillis() - mLoadStartTime);
550 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700551 mInPageLoad = false;
John Reck30c714c2010-12-16 17:30:34 -0800552 // Sync state (in case of stop/timeout)
553 mCurrentState.mUrl = view.getUrl();
John Reck6c702ee2011-01-07 09:41:53 -0800554 if (mCurrentState.mUrl == null) {
555 mCurrentState.mUrl = url != null ? url : "";
556 }
John Reck30c714c2010-12-16 17:30:34 -0800557 mCurrentState.mTitle = view.getTitle();
558 mCurrentState.mFavicon = view.getFavicon();
559 if (!URLUtil.isHttpsUrl(mCurrentState.mUrl)) {
560 // In case we stop when loading an HTTPS page from an HTTP page
561 // but before a provisional load occurred
562 mCurrentState.mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
563 }
John Reck324d4402011-01-11 16:56:42 -0800564 mWebViewController.onPageFinished(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700565 }
566
567 // return true if want to hijack the url to let another app to handle it
568 @Override
569 public boolean shouldOverrideUrlLoading(WebView view, String url) {
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400570 if (voiceSearchSourceIsGoogle()) {
571 // This method is called when the user clicks on a link.
572 // VoiceSearchMode is turned off when the user leaves the
573 // Google results page, so at this point the user must be on
574 // that page. If the user clicked a link on that page, assume
575 // that the voice search was effective, and broadcast an Intent
576 // so a receiver can take note of that fact.
577 Intent logIntent = new Intent(LoggingEvents.ACTION_LOG_EVENT);
578 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
579 LoggingEvents.VoiceSearch.RESULT_CLICKED);
580 mActivity.sendBroadcast(logIntent);
581 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700582 if (mInForeground) {
Michael Kolb18eb3772010-12-10 14:29:51 -0800583 return mWebViewController.shouldOverrideUrlLoading(Tab.this,
584 view, url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700585 } else {
586 return false;
587 }
588 }
589
590 /**
591 * Updates the lock icon. This method is called when we discover another
592 * resource to be loaded for this page (for example, javascript). While
593 * we update the icon type, we do not update the lock icon itself until
594 * we are done loading, it is slightly more secure this way.
595 */
596 @Override
597 public void onLoadResource(WebView view, String url) {
598 if (url != null && url.length() > 0) {
599 // It is only if the page claims to be secure that we may have
600 // to update the lock:
John Reck30c714c2010-12-16 17:30:34 -0800601 if (mCurrentState.mLockIcon == LockIcon.LOCK_ICON_SECURE) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700602 // If NOT a 'safe' url, change the lock to mixed content!
603 if (!(URLUtil.isHttpsUrl(url) || URLUtil.isDataUrl(url)
604 || URLUtil.isAboutUrl(url))) {
John Reck30c714c2010-12-16 17:30:34 -0800605 mCurrentState.mLockIcon = LockIcon.LOCK_ICON_MIXED;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700606 }
607 }
608 }
609 }
610
611 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700612 * Show a dialog informing the user of the network error reported by
613 * WebCore if it is in the foreground.
614 */
615 @Override
616 public void onReceivedError(WebView view, int errorCode,
617 String description, String failingUrl) {
618 if (errorCode != WebViewClient.ERROR_HOST_LOOKUP &&
619 errorCode != WebViewClient.ERROR_CONNECT &&
620 errorCode != WebViewClient.ERROR_BAD_URL &&
621 errorCode != WebViewClient.ERROR_UNSUPPORTED_SCHEME &&
622 errorCode != WebViewClient.ERROR_FILE) {
623 queueError(errorCode, description);
624 }
Jeff Hamilton47654f42010-09-07 09:57:51 -0500625
626 // Don't log URLs when in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -0700627 if (!isPrivateBrowsingEnabled()) {
Jeff Hamilton47654f42010-09-07 09:57:51 -0500628 Log.e(LOGTAG, "onReceivedError " + errorCode + " " + failingUrl
629 + " " + description);
630 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700631 }
632
633 /**
634 * Check with the user if it is ok to resend POST data as the page they
635 * are trying to navigate to is the result of a POST.
636 */
637 @Override
638 public void onFormResubmission(WebView view, final Message dontResend,
639 final Message resend) {
640 if (!mInForeground) {
641 dontResend.sendToTarget();
642 return;
643 }
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500644 if (mDontResend != null) {
645 Log.w(LOGTAG, "onFormResubmission should not be called again "
646 + "while dialog is still up");
647 dontResend.sendToTarget();
648 return;
649 }
650 mDontResend = dontResend;
651 mResend = resend;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700652 new AlertDialog.Builder(mActivity).setTitle(
653 R.string.browserFrameFormResubmitLabel).setMessage(
654 R.string.browserFrameFormResubmitMessage)
655 .setPositiveButton(R.string.ok,
656 new DialogInterface.OnClickListener() {
657 public void onClick(DialogInterface dialog,
658 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500659 if (mResend != null) {
660 mResend.sendToTarget();
661 mResend = null;
662 mDontResend = null;
663 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700664 }
665 }).setNegativeButton(R.string.cancel,
666 new DialogInterface.OnClickListener() {
667 public void onClick(DialogInterface dialog,
668 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500669 if (mDontResend != null) {
670 mDontResend.sendToTarget();
671 mResend = null;
672 mDontResend = null;
673 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700674 }
675 }).setOnCancelListener(new OnCancelListener() {
676 public void onCancel(DialogInterface dialog) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500677 if (mDontResend != null) {
678 mDontResend.sendToTarget();
679 mResend = null;
680 mDontResend = null;
681 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700682 }
683 }).show();
684 }
685
686 /**
687 * Insert the url into the visited history database.
688 * @param url The url to be inserted.
689 * @param isReload True if this url is being reloaded.
690 * FIXME: Not sure what to do when reloading the page.
691 */
692 @Override
693 public void doUpdateVisitedHistory(WebView view, String url,
694 boolean isReload) {
John Reck324d4402011-01-11 16:56:42 -0800695 mWebViewController.doUpdateVisitedHistory(Tab.this, isReload);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700696 }
697
698 /**
699 * Displays SSL error(s) dialog to the user.
700 */
701 @Override
702 public void onReceivedSslError(final WebView view,
703 final SslErrorHandler handler, final SslError error) {
704 if (!mInForeground) {
705 handler.cancel();
John Reck30c714c2010-12-16 17:30:34 -0800706 setLockIconType(LockIcon.LOCK_ICON_UNSECURE);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700707 return;
708 }
709 if (BrowserSettings.getInstance().showSecurityWarnings()) {
710 final LayoutInflater factory =
711 LayoutInflater.from(mActivity);
712 final View warningsView =
713 factory.inflate(R.layout.ssl_warnings, null);
714 final LinearLayout placeholder =
715 (LinearLayout)warningsView.findViewById(R.id.placeholder);
716
717 if (error.hasError(SslError.SSL_UNTRUSTED)) {
718 LinearLayout ll = (LinearLayout)factory
719 .inflate(R.layout.ssl_warning, null);
720 ((TextView)ll.findViewById(R.id.warning))
721 .setText(R.string.ssl_untrusted);
722 placeholder.addView(ll);
723 }
724
725 if (error.hasError(SslError.SSL_IDMISMATCH)) {
726 LinearLayout ll = (LinearLayout)factory
727 .inflate(R.layout.ssl_warning, null);
728 ((TextView)ll.findViewById(R.id.warning))
729 .setText(R.string.ssl_mismatch);
730 placeholder.addView(ll);
731 }
732
733 if (error.hasError(SslError.SSL_EXPIRED)) {
734 LinearLayout ll = (LinearLayout)factory
735 .inflate(R.layout.ssl_warning, null);
736 ((TextView)ll.findViewById(R.id.warning))
737 .setText(R.string.ssl_expired);
738 placeholder.addView(ll);
739 }
740
741 if (error.hasError(SslError.SSL_NOTYETVALID)) {
742 LinearLayout ll = (LinearLayout)factory
743 .inflate(R.layout.ssl_warning, null);
744 ((TextView)ll.findViewById(R.id.warning))
745 .setText(R.string.ssl_not_yet_valid);
746 placeholder.addView(ll);
747 }
748
749 new AlertDialog.Builder(mActivity).setTitle(
750 R.string.security_warning).setIcon(
751 android.R.drawable.ic_dialog_alert).setView(
752 warningsView).setPositiveButton(R.string.ssl_continue,
753 new DialogInterface.OnClickListener() {
754 public void onClick(DialogInterface dialog,
755 int whichButton) {
756 handler.proceed();
757 }
758 }).setNeutralButton(R.string.view_certificate,
759 new DialogInterface.OnClickListener() {
760 public void onClick(DialogInterface dialog,
761 int whichButton) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700762 mWebViewController.showSslCertificateOnError(view,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700763 handler, error);
764 }
Ben Murdocha49b8292010-11-16 11:56:04 +0000765 }).setNegativeButton(R.string.ssl_go_back,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700766 new DialogInterface.OnClickListener() {
767 public void onClick(DialogInterface dialog,
768 int whichButton) {
John Reck30c714c2010-12-16 17:30:34 -0800769 dialog.cancel();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700770 }
771 }).setOnCancelListener(
772 new DialogInterface.OnCancelListener() {
773 public void onCancel(DialogInterface dialog) {
774 handler.cancel();
John Reck30c714c2010-12-16 17:30:34 -0800775 setLockIconType(LockIcon.LOCK_ICON_UNSECURE);
776 mWebViewController.onUserCanceledSsl(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700777 }
778 }).show();
779 } else {
780 handler.proceed();
781 }
782 }
783
784 /**
785 * Handles an HTTP authentication request.
786 *
787 * @param handler The authentication handler
788 * @param host The host
789 * @param realm The realm
790 */
791 @Override
792 public void onReceivedHttpAuthRequest(WebView view,
793 final HttpAuthHandler handler, final String host,
794 final String realm) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700795 mWebViewController.onReceivedHttpAuthRequest(Tab.this, view, handler, host, realm);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700796 }
797
798 @Override
John Reck438bf462011-01-12 18:11:46 -0800799 public WebResourceResponse shouldInterceptRequest(WebView view,
800 String url) {
801 WebResourceResponse res = HomeProvider.shouldInterceptRequest(
802 mActivity, url);
803 return res;
804 }
805
806 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700807 public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
808 if (!mInForeground) {
809 return false;
810 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700811 return mWebViewController.shouldOverrideKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700812 }
813
814 @Override
815 public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700816 if (!mInForeground) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700817 return;
818 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700819 mWebViewController.onUnhandledKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700820 }
Patrick Scott92066772011-03-10 08:46:27 -0500821
822 @Override
823 public void onReceivedLoginRequest(WebView view, String realm,
824 String account, String args) {
825 new DeviceAccountLogin(mActivity, view, Tab.this, mWebViewController)
826 .handleLogin(realm, account, args);
827 }
828
Grace Kloba22ac16e2009-10-07 18:00:23 -0700829 };
830
Patrick Scott92066772011-03-10 08:46:27 -0500831 // Called by DeviceAccountLogin when the Tab needs to have the auto-login UI
832 // displayed.
833 void setDeviceAccountLogin(DeviceAccountLogin login) {
834 mDeviceAccountLogin = login;
835 }
836
837 // Returns non-null if the title bar should display the auto-login UI.
838 DeviceAccountLogin getDeviceAccountLogin() {
839 return mDeviceAccountLogin;
840 }
841
Grace Kloba22ac16e2009-10-07 18:00:23 -0700842 // -------------------------------------------------------------------------
843 // WebChromeClient implementation for the main WebView
844 // -------------------------------------------------------------------------
845
846 private final WebChromeClient mWebChromeClient = new WebChromeClient() {
847 // Helper method to create a new tab or sub window.
848 private void createWindow(final boolean dialog, final Message msg) {
849 WebView.WebViewTransport transport =
850 (WebView.WebViewTransport) msg.obj;
851 if (dialog) {
852 createSubWindow();
Michael Kolb8233fac2010-10-26 16:08:53 -0700853 mWebViewController.attachSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700854 transport.setWebView(mSubView);
855 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -0700856 final Tab newTab = mWebViewController.openTabAndShow(
Michael Kolb18eb3772010-12-10 14:29:51 -0800857 Tab.this,
Michael Kolb8233fac2010-10-26 16:08:53 -0700858 IntentHandler.EMPTY_URL_DATA, false, null);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700859 if (newTab != Tab.this) {
860 Tab.this.addChildTab(newTab);
861 }
862 transport.setWebView(newTab.getWebView());
863 }
864 msg.sendToTarget();
865 }
866
867 @Override
868 public boolean onCreateWindow(WebView view, final boolean dialog,
869 final boolean userGesture, final Message resultMsg) {
870 // only allow new window or sub window for the foreground case
871 if (!mInForeground) {
872 return false;
873 }
874 // Short-circuit if we can't create any more tabs or sub windows.
875 if (dialog && mSubView != null) {
876 new AlertDialog.Builder(mActivity)
877 .setTitle(R.string.too_many_subwindows_dialog_title)
878 .setIcon(android.R.drawable.ic_dialog_alert)
879 .setMessage(R.string.too_many_subwindows_dialog_message)
880 .setPositiveButton(R.string.ok, null)
881 .show();
882 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700883 } else if (!mWebViewController.getTabControl().canCreateNewTab()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700884 new AlertDialog.Builder(mActivity)
885 .setTitle(R.string.too_many_windows_dialog_title)
886 .setIcon(android.R.drawable.ic_dialog_alert)
887 .setMessage(R.string.too_many_windows_dialog_message)
888 .setPositiveButton(R.string.ok, null)
889 .show();
890 return false;
891 }
892
893 // Short-circuit if this was a user gesture.
894 if (userGesture) {
895 createWindow(dialog, resultMsg);
896 return true;
897 }
898
899 // Allow the popup and create the appropriate window.
900 final AlertDialog.OnClickListener allowListener =
901 new AlertDialog.OnClickListener() {
902 public void onClick(DialogInterface d,
903 int which) {
904 createWindow(dialog, resultMsg);
905 }
906 };
907
908 // Block the popup by returning a null WebView.
909 final AlertDialog.OnClickListener blockListener =
910 new AlertDialog.OnClickListener() {
911 public void onClick(DialogInterface d, int which) {
912 resultMsg.sendToTarget();
913 }
914 };
915
916 // Build a confirmation dialog to display to the user.
917 final AlertDialog d =
918 new AlertDialog.Builder(mActivity)
919 .setTitle(R.string.attention)
920 .setIcon(android.R.drawable.ic_dialog_alert)
921 .setMessage(R.string.popup_window_attempt)
922 .setPositiveButton(R.string.allow, allowListener)
923 .setNegativeButton(R.string.block, blockListener)
924 .setCancelable(false)
925 .create();
926
927 // Show the confirmation dialog.
928 d.show();
929 return true;
930 }
931
932 @Override
Patrick Scotteb5061b2009-11-18 15:00:30 -0500933 public void onRequestFocus(WebView view) {
934 if (!mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700935 mWebViewController.switchToTab(mWebViewController.getTabControl().getTabIndex(
Patrick Scotteb5061b2009-11-18 15:00:30 -0500936 Tab.this));
937 }
938 }
939
940 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700941 public void onCloseWindow(WebView window) {
942 if (mParentTab != null) {
943 // JavaScript can only close popup window.
944 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700945 mWebViewController.switchToTab(mWebViewController.getTabControl()
Grace Kloba22ac16e2009-10-07 18:00:23 -0700946 .getTabIndex(mParentTab));
947 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700948 mWebViewController.closeTab(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700949 }
950 }
951
952 @Override
953 public void onProgressChanged(WebView view, int newProgress) {
John Reck30c714c2010-12-16 17:30:34 -0800954 mPageLoadProgress = newProgress;
955 mWebViewController.onProgressChanged(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700956 }
957
958 @Override
Leon Scroggins21d9b902010-03-11 09:33:11 -0500959 public void onReceivedTitle(WebView view, final String title) {
John Reck30c714c2010-12-16 17:30:34 -0800960 mCurrentState.mTitle = title;
Michael Kolb8233fac2010-10-26 16:08:53 -0700961 mWebViewController.onReceivedTitle(Tab.this, title);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700962 }
963
964 @Override
965 public void onReceivedIcon(WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -0800966 mCurrentState.mFavicon = icon;
Michael Kolb8233fac2010-10-26 16:08:53 -0700967 mWebViewController.onFavicon(Tab.this, view, icon);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700968 }
969
970 @Override
971 public void onReceivedTouchIconUrl(WebView view, String url,
972 boolean precomposed) {
973 final ContentResolver cr = mActivity.getContentResolver();
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400974 // Let precomposed icons take precedence over non-composed
975 // icons.
976 if (precomposed && mTouchIconLoader != null) {
977 mTouchIconLoader.cancel(false);
978 mTouchIconLoader = null;
979 }
980 // Have only one async task at a time.
981 if (mTouchIconLoader == null) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700982 mTouchIconLoader = new DownloadTouchIcon(Tab.this,
983 mActivity, cr, view);
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400984 mTouchIconLoader.execute(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700985 }
986 }
987
988 @Override
989 public void onShowCustomView(View view,
990 WebChromeClient.CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700991 if (mInForeground) mWebViewController.showCustomView(Tab.this, view,
992 callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700993 }
994
995 @Override
996 public void onHideCustomView() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700997 if (mInForeground) mWebViewController.hideCustomView();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700998 }
999
1000 /**
1001 * The origin has exceeded its database quota.
1002 * @param url the URL that exceeded the quota
1003 * @param databaseIdentifier the identifier of the database on which the
1004 * transaction that caused the quota overflow was run
1005 * @param currentQuota the current quota for the origin.
1006 * @param estimatedSize the estimated size of the database.
1007 * @param totalUsedQuota is the sum of all origins' quota.
1008 * @param quotaUpdater The callback to run when a decision to allow or
1009 * deny quota has been made. Don't forget to call this!
1010 */
1011 @Override
1012 public void onExceededDatabaseQuota(String url,
1013 String databaseIdentifier, long currentQuota, long estimatedSize,
1014 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
1015 BrowserSettings.getInstance().getWebStorageSizeManager()
1016 .onExceededDatabaseQuota(url, databaseIdentifier,
1017 currentQuota, estimatedSize, totalUsedQuota,
1018 quotaUpdater);
1019 }
1020
1021 /**
1022 * The Application Cache has exceeded its max size.
1023 * @param spaceNeeded is the amount of disk space that would be needed
1024 * in order for the last appcache operation to succeed.
1025 * @param totalUsedQuota is the sum of all origins' quota.
1026 * @param quotaUpdater A callback to inform the WebCore thread that a
1027 * new app cache size is available. This callback must always
1028 * be executed at some point to ensure that the sleeping
1029 * WebCore thread is woken up.
1030 */
1031 @Override
1032 public void onReachedMaxAppCacheSize(long spaceNeeded,
1033 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
1034 BrowserSettings.getInstance().getWebStorageSizeManager()
1035 .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota,
1036 quotaUpdater);
1037 }
1038
1039 /**
1040 * Instructs the browser to show a prompt to ask the user to set the
1041 * Geolocation permission state for the specified origin.
1042 * @param origin The origin for which Geolocation permissions are
1043 * requested.
1044 * @param callback The callback to call once the user has set the
1045 * Geolocation permission state.
1046 */
1047 @Override
1048 public void onGeolocationPermissionsShowPrompt(String origin,
1049 GeolocationPermissions.Callback callback) {
1050 if (mInForeground) {
Grace Kloba50c241e2010-04-20 11:07:50 -07001051 getGeolocationPermissionsPrompt().show(origin, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001052 }
1053 }
1054
1055 /**
1056 * Instructs the browser to hide the Geolocation permissions prompt.
1057 */
1058 @Override
1059 public void onGeolocationPermissionsHidePrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001060 if (mInForeground && mGeolocationPermissionsPrompt != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001061 mGeolocationPermissionsPrompt.hide();
1062 }
1063 }
1064
Ben Murdoch65acc352009-11-19 18:16:04 +00001065 /* Adds a JavaScript error message to the system log and if the JS
1066 * console is enabled in the about:debug options, to that console
1067 * also.
Ben Murdochc42addf2010-01-28 15:19:59 +00001068 * @param consoleMessage the message object.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001069 */
1070 @Override
Ben Murdochc42addf2010-01-28 15:19:59 +00001071 public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001072 if (mInForeground) {
1073 // call getErrorConsole(true) so it will create one if needed
1074 ErrorConsoleView errorConsole = getErrorConsole(true);
Ben Murdochc42addf2010-01-28 15:19:59 +00001075 errorConsole.addErrorMessage(consoleMessage);
Michael Kolb8233fac2010-10-26 16:08:53 -07001076 if (mWebViewController.shouldShowErrorConsole()
1077 && errorConsole.getShowState() !=
1078 ErrorConsoleView.SHOW_MAXIMIZED) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001079 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
1080 }
1081 }
Ben Murdochc42addf2010-01-28 15:19:59 +00001082
Jeff Hamilton47654f42010-09-07 09:57:51 -05001083 // Don't log console messages in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001084 if (isPrivateBrowsingEnabled()) return true;
Jeff Hamilton47654f42010-09-07 09:57:51 -05001085
Ben Murdochc42addf2010-01-28 15:19:59 +00001086 String message = "Console: " + consoleMessage.message() + " "
1087 + consoleMessage.sourceId() + ":"
1088 + consoleMessage.lineNumber();
1089
1090 switch (consoleMessage.messageLevel()) {
1091 case TIP:
1092 Log.v(CONSOLE_LOGTAG, message);
1093 break;
1094 case LOG:
1095 Log.i(CONSOLE_LOGTAG, message);
1096 break;
1097 case WARNING:
1098 Log.w(CONSOLE_LOGTAG, message);
1099 break;
1100 case ERROR:
1101 Log.e(CONSOLE_LOGTAG, message);
1102 break;
1103 case DEBUG:
1104 Log.d(CONSOLE_LOGTAG, message);
1105 break;
1106 }
1107
1108 return true;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001109 }
1110
1111 /**
1112 * Ask the browser for an icon to represent a <video> element.
1113 * This icon will be used if the Web page did not specify a poster attribute.
1114 * @return Bitmap The icon or null if no such icon is available.
1115 */
1116 @Override
1117 public Bitmap getDefaultVideoPoster() {
1118 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001119 return mWebViewController.getDefaultVideoPoster();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001120 }
1121 return null;
1122 }
1123
1124 /**
1125 * Ask the host application for a custom progress view to show while
1126 * a <video> is loading.
1127 * @return View The progress view.
1128 */
1129 @Override
1130 public View getVideoLoadingProgressView() {
1131 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001132 return mWebViewController.getVideoLoadingProgressView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001133 }
1134 return null;
1135 }
1136
1137 @Override
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01001138 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001139 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001140 mWebViewController.openFileChooser(uploadMsg, acceptType);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001141 } else {
1142 uploadMsg.onReceiveValue(null);
1143 }
1144 }
1145
1146 /**
1147 * Deliver a list of already-visited URLs
1148 */
1149 @Override
1150 public void getVisitedHistory(final ValueCallback<String[]> callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001151 mWebViewController.getVisitedHistory(callback);
1152 }
Ben Murdoch8029a772010-11-16 11:58:21 +00001153
1154 @Override
1155 public void setupAutoFill(Message message) {
1156 // Prompt the user to set up their profile.
1157 final Message msg = message;
1158 AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
Ben Murdoch1d676b62011-01-17 12:54:24 +00001159 LayoutInflater inflater = (LayoutInflater) mActivity.getSystemService(
1160 Context.LAYOUT_INFLATER_SERVICE);
1161 final View layout = inflater.inflate(R.layout.setup_autofill_dialog, null);
1162
1163 builder.setView(layout)
1164 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1165 @Override
1166 public void onClick(DialogInterface dialog, int id) {
1167 CheckBox disableAutoFill = (CheckBox) layout.findViewById(
1168 R.id.setup_autofill_dialog_disable_autofill);
1169
1170 if (disableAutoFill.isChecked()) {
1171 // Disable autofill and show a toast with how to turn it on again.
1172 BrowserSettings s = BrowserSettings.getInstance();
1173 s.addObserver(mMainView.getSettings());
1174 s.disableAutoFill(mActivity);
1175 s.update();
1176 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 Kolb8233fac2010-10-26 16:08:53 -07001290 Tab(WebViewController wvcontroller, WebView w, boolean closeOnExit, String appId,
Grace Kloba22ac16e2009-10-07 18:00:23 -07001291 String url) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001292 mWebViewController = wvcontroller;
1293 mActivity = mWebViewController.getActivity();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001294 mCloseOnExit = closeOnExit;
1295 mAppId = appId;
John Recke969cc52010-12-21 17:24:43 -08001296 mDataController = DataController.getInstance(mActivity);
John Reck30c714c2010-12-16 17:30:34 -08001297 mCurrentState = new PageState(mActivity, w.isPrivateBrowsingEnabled());
Michael Kolb8233fac2010-10-26 16:08:53 -07001298 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001299 mInForeground = false;
1300
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001301 mDownloadListener = new DownloadListener() {
1302 public void onDownloadStart(String url, String userAgent,
1303 String contentDisposition, String mimetype,
1304 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001305 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001306 mimetype, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001307 }
1308 };
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001309 mWebBackForwardListClient = new WebBackForwardListClient() {
1310 @Override
1311 public void onNewHistoryItem(WebHistoryItem item) {
1312 if (isInVoiceSearchMode()) {
1313 item.setCustomData(mVoiceSearchData.mVoiceSearchIntent);
1314 }
1315 }
1316 @Override
1317 public void onIndexChanged(WebHistoryItem item, int index) {
1318 Object data = item.getCustomData();
1319 if (data != null && data instanceof Intent) {
1320 activateVoiceSearchMode((Intent) data);
1321 }
1322 }
1323 };
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001324
Grace Kloba22ac16e2009-10-07 18:00:23 -07001325 setWebView(w);
1326 }
1327
1328 /**
1329 * Sets the WebView for this tab, correctly removing the old WebView from
1330 * the container view.
1331 */
1332 void setWebView(WebView w) {
1333 if (mMainView == w) {
1334 return;
1335 }
Michael Kolba713ec82010-11-29 17:27:06 -08001336
Grace Kloba22ac16e2009-10-07 18:00:23 -07001337 // If the WebView is changing, the page will be reloaded, so any ongoing
1338 // Geolocation permission requests are void.
Grace Kloba50c241e2010-04-20 11:07:50 -07001339 if (mGeolocationPermissionsPrompt != null) {
1340 mGeolocationPermissionsPrompt.hide();
1341 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001342
Michael Kolba713ec82010-11-29 17:27:06 -08001343 mWebViewController.onSetWebView(this, w);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001344
1345 // set the new one
1346 mMainView = w;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001347 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001348 if (mMainView != null) {
1349 mMainView.setWebViewClient(mWebViewClient);
1350 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001351 // Attach DownloadManager so that downloads can start in an active
1352 // or a non-active window. This can happen when going to a site that
1353 // does a redirect after a period of time. The user could have
1354 // switched to another tab while waiting for the download to start.
1355 mMainView.setDownloadListener(mDownloadListener);
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001356 mMainView.setWebBackForwardListClient(mWebBackForwardListClient);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001357 }
1358 }
1359
1360 /**
1361 * Destroy the tab's main WebView and subWindow if any
1362 */
1363 void destroy() {
1364 if (mMainView != null) {
1365 dismissSubWindow();
1366 BrowserSettings.getInstance().deleteObserver(mMainView.getSettings());
1367 // save the WebView to call destroy() after detach it from the tab
1368 WebView webView = mMainView;
1369 setWebView(null);
1370 webView.destroy();
1371 }
1372 }
1373
1374 /**
1375 * Remove the tab from the parent
1376 */
1377 void removeFromTree() {
1378 // detach the children
1379 if (mChildTabs != null) {
1380 for(Tab t : mChildTabs) {
1381 t.setParentTab(null);
1382 }
1383 }
1384 // remove itself from the parent list
1385 if (mParentTab != null) {
1386 mParentTab.mChildTabs.remove(this);
1387 }
1388 }
1389
1390 /**
1391 * Create a new subwindow unless a subwindow already exists.
1392 * @return True if a new subwindow was created. False if one already exists.
1393 */
1394 boolean createSubWindow() {
1395 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001396 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001397 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001398 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001399 mSubView.setWebChromeClient(new SubWindowChromeClient(
1400 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001401 // Set a different DownloadListener for the mSubView, since it will
1402 // just need to dismiss the mSubView, rather than close the Tab
1403 mSubView.setDownloadListener(new DownloadListener() {
1404 public void onDownloadStart(String url, String userAgent,
1405 String contentDisposition, String mimetype,
1406 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001407 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001408 contentDisposition, mimetype, contentLength);
1409 if (mSubView.copyBackForwardList().getSize() == 0) {
1410 // This subwindow was opened for the sole purpose of
1411 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001412 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001413 }
1414 }
1415 });
Grace Kloba22ac16e2009-10-07 18:00:23 -07001416 mSubView.setOnCreateContextMenuListener(mActivity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001417 return true;
1418 }
1419 return false;
1420 }
1421
1422 /**
1423 * Dismiss the subWindow for the tab.
1424 */
1425 void dismissSubWindow() {
1426 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001427 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001428 BrowserSettings.getInstance().deleteObserver(
1429 mSubView.getSettings());
1430 mSubView.destroy();
1431 mSubView = null;
1432 mSubViewContainer = null;
1433 }
1434 }
1435
Grace Kloba22ac16e2009-10-07 18:00:23 -07001436
1437 /**
1438 * Set the parent tab of this tab.
1439 */
1440 void setParentTab(Tab parent) {
1441 mParentTab = parent;
1442 // This tab may have been freed due to low memory. If that is the case,
1443 // the parent tab index is already saved. If we are changing that index
1444 // (most likely due to removing the parent tab) we must update the
1445 // parent tab index in the saved Bundle.
1446 if (mSavedState != null) {
1447 if (parent == null) {
1448 mSavedState.remove(PARENTTAB);
1449 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -07001450 mSavedState.putInt(PARENTTAB, mWebViewController.getTabControl()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001451 .getTabIndex(parent));
1452 }
1453 }
1454 }
1455
1456 /**
1457 * When a Tab is created through the content of another Tab, then we
1458 * associate the Tabs.
1459 * @param child the Tab that was created from this Tab
1460 */
1461 void addChildTab(Tab child) {
1462 if (mChildTabs == null) {
1463 mChildTabs = new Vector<Tab>();
1464 }
1465 mChildTabs.add(child);
1466 child.setParentTab(this);
1467 }
1468
1469 Vector<Tab> getChildTabs() {
1470 return mChildTabs;
1471 }
1472
1473 void resume() {
1474 if (mMainView != null) {
1475 mMainView.onResume();
1476 if (mSubView != null) {
1477 mSubView.onResume();
1478 }
1479 }
1480 }
1481
1482 void pause() {
1483 if (mMainView != null) {
1484 mMainView.onPause();
1485 if (mSubView != null) {
1486 mSubView.onPause();
1487 }
1488 }
1489 }
1490
1491 void putInForeground() {
1492 mInForeground = true;
1493 resume();
1494 mMainView.setOnCreateContextMenuListener(mActivity);
1495 if (mSubView != null) {
1496 mSubView.setOnCreateContextMenuListener(mActivity);
1497 }
1498 // Show the pending error dialog if the queue is not empty
1499 if (mQueuedErrors != null && mQueuedErrors.size() > 0) {
1500 showError(mQueuedErrors.getFirst());
1501 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001502 mWebViewController.bookmarkedStatusHasChanged(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001503 }
1504
1505 void putInBackground() {
1506 mInForeground = false;
1507 pause();
1508 mMainView.setOnCreateContextMenuListener(null);
1509 if (mSubView != null) {
1510 mSubView.setOnCreateContextMenuListener(null);
1511 }
1512 }
1513
Michael Kolb8233fac2010-10-26 16:08:53 -07001514 boolean inForeground() {
1515 return mInForeground;
1516 }
1517
Grace Kloba22ac16e2009-10-07 18:00:23 -07001518 /**
1519 * Return the top window of this tab; either the subwindow if it is not
1520 * null or the main window.
1521 * @return The top window of this tab.
1522 */
1523 WebView getTopWindow() {
1524 if (mSubView != null) {
1525 return mSubView;
1526 }
1527 return mMainView;
1528 }
1529
1530 /**
1531 * Return the main window of this tab. Note: if a tab is freed in the
1532 * background, this can return null. It is only guaranteed to be
1533 * non-null for the current tab.
1534 * @return The main WebView of this tab.
1535 */
1536 WebView getWebView() {
1537 return mMainView;
1538 }
1539
Michael Kolba713ec82010-11-29 17:27:06 -08001540 void setViewContainer(View container) {
1541 mContainer = container;
1542 }
1543
Michael Kolb8233fac2010-10-26 16:08:53 -07001544 View getViewContainer() {
1545 return mContainer;
1546 }
1547
Grace Kloba22ac16e2009-10-07 18:00:23 -07001548 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001549 * Return whether private browsing is enabled for the main window of
1550 * this tab.
1551 * @return True if private browsing is enabled.
1552 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001553 boolean isPrivateBrowsingEnabled() {
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001554 WebView webView = getWebView();
1555 if (webView == null) {
1556 return false;
1557 }
1558 return webView.isPrivateBrowsingEnabled();
1559 }
1560
1561 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001562 * Return the subwindow of this tab or null if there is no subwindow.
1563 * @return The subwindow of this tab or null.
1564 */
1565 WebView getSubWebView() {
1566 return mSubView;
1567 }
1568
Michael Kolb1514bb72010-11-22 09:11:48 -08001569 void setSubWebView(WebView subView) {
1570 mSubView = subView;
1571 }
1572
Michael Kolb8233fac2010-10-26 16:08:53 -07001573 View getSubViewContainer() {
1574 return mSubViewContainer;
1575 }
1576
Michael Kolb1514bb72010-11-22 09:11:48 -08001577 void setSubViewContainer(View subViewContainer) {
1578 mSubViewContainer = subViewContainer;
1579 }
1580
Grace Kloba22ac16e2009-10-07 18:00:23 -07001581 /**
1582 * @return The geolocation permissions prompt for this tab.
1583 */
1584 GeolocationPermissionsPrompt getGeolocationPermissionsPrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001585 if (mGeolocationPermissionsPrompt == null) {
1586 ViewStub stub = (ViewStub) mContainer
1587 .findViewById(R.id.geolocation_permissions_prompt);
1588 mGeolocationPermissionsPrompt = (GeolocationPermissionsPrompt) stub
1589 .inflate();
1590 mGeolocationPermissionsPrompt.init();
1591 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001592 return mGeolocationPermissionsPrompt;
1593 }
1594
1595 /**
1596 * @return The application id string
1597 */
1598 String getAppId() {
1599 return mAppId;
1600 }
1601
1602 /**
1603 * Set the application id string
1604 * @param id
1605 */
1606 void setAppId(String id) {
1607 mAppId = id;
1608 }
1609
Grace Kloba22ac16e2009-10-07 18:00:23 -07001610 String getUrl() {
John Reck324d4402011-01-11 16:56:42 -08001611 return UrlUtils.filteredUrl(mCurrentState.mUrl);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001612 }
1613
John Reck49a603c2011-03-03 09:33:05 -08001614 String getOriginalUrl() {
1615 if (mMainView == null) {
1616 return "";
1617 }
1618 return UrlUtils.filteredUrl(mMainView.getOriginalUrl());
1619 }
1620
Grace Kloba22ac16e2009-10-07 18:00:23 -07001621 /**
John Reck30c714c2010-12-16 17:30:34 -08001622 * Get the title of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001623 */
1624 String getTitle() {
John Reck30c714c2010-12-16 17:30:34 -08001625 if (mCurrentState.mTitle == null && mInPageLoad) {
1626 return mActivity.getString(R.string.title_bar_loading);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001627 }
John Reck30c714c2010-12-16 17:30:34 -08001628 return mCurrentState.mTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001629 }
1630
1631 /**
John Reck30c714c2010-12-16 17:30:34 -08001632 * Get the favicon of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001633 */
1634 Bitmap getFavicon() {
John Reck30c714c2010-12-16 17:30:34 -08001635 return mCurrentState.mFavicon;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001636 }
1637
John Recke969cc52010-12-21 17:24:43 -08001638 public boolean isBookmarkedSite() {
1639 return mCurrentState.mIsBookmarkedSite;
1640 }
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001641
Grace Kloba22ac16e2009-10-07 18:00:23 -07001642 /**
1643 * Return the tab's error console. Creates the console if createIfNEcessary
1644 * is true and we haven't already created the console.
1645 * @param createIfNecessary Flag to indicate if the console should be
1646 * created if it has not been already.
1647 * @return The tab's error console, or null if one has not been created and
1648 * createIfNecessary is false.
1649 */
1650 ErrorConsoleView getErrorConsole(boolean createIfNecessary) {
1651 if (createIfNecessary && mErrorConsole == null) {
1652 mErrorConsole = new ErrorConsoleView(mActivity);
1653 mErrorConsole.setWebView(mMainView);
1654 }
1655 return mErrorConsole;
1656 }
1657
1658 /**
1659 * If this Tab was created through another Tab, then this method returns
1660 * that Tab.
1661 * @return the Tab parent or null
1662 */
1663 public Tab getParentTab() {
1664 return mParentTab;
1665 }
1666
1667 /**
1668 * Return whether this tab should be closed when it is backing out of the
1669 * first page.
1670 * @return TRUE if this tab should be closed when exit.
1671 */
1672 boolean closeOnExit() {
1673 return mCloseOnExit;
1674 }
1675
John Reck30c714c2010-12-16 17:30:34 -08001676 private void setLockIconType(LockIcon icon) {
1677 mCurrentState.mLockIcon = icon;
1678 mWebViewController.onUpdatedLockIcon(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001679 }
1680
1681 /**
1682 * @return The tab's lock icon type.
1683 */
John Reck30c714c2010-12-16 17:30:34 -08001684 LockIcon getLockIconType() {
1685 return mCurrentState.mLockIcon;
1686 }
1687
1688 int getLoadProgress() {
1689 if (mInPageLoad) {
1690 return mPageLoadProgress;
1691 }
1692 return 100;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001693 }
1694
1695 /**
1696 * @return TRUE if onPageStarted is called while onPageFinished is not
1697 * called yet.
1698 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001699 boolean inPageLoad() {
1700 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001701 }
1702
1703 // force mInLoad to be false. This should only be called before closing the
1704 // tab to ensure BrowserActivity's pauseWebViewTimers() is called correctly.
Michael Kolb8233fac2010-10-26 16:08:53 -07001705 void clearInPageLoad() {
1706 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001707 }
1708
Grace Kloba22ac16e2009-10-07 18:00:23 -07001709 /**
John Reck30c714c2010-12-16 17:30:34 -08001710 * Get the cached saved state bundle.
1711 * @return cached state bundle
Grace Kloba22ac16e2009-10-07 18:00:23 -07001712 */
1713 Bundle getSavedState() {
1714 return mSavedState;
1715 }
1716
1717 /**
1718 * Set the saved state.
1719 */
1720 void setSavedState(Bundle state) {
1721 mSavedState = state;
1722 }
1723
1724 /**
1725 * @return TRUE if succeed in saving the state.
1726 */
1727 boolean saveState() {
1728 // If the WebView is null it means we ran low on memory and we already
1729 // stored the saved state in mSavedState.
1730 if (mMainView == null) {
1731 return mSavedState != null;
1732 }
1733
1734 mSavedState = new Bundle();
1735 final WebBackForwardList list = mMainView.saveState(mSavedState);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001736
1737 // Store some extra info for displaying the tab in the picker.
1738 final WebHistoryItem item = list != null ? list.getCurrentItem() : null;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001739
John Reck30c714c2010-12-16 17:30:34 -08001740 mSavedState.putString(CURRURL, mCurrentState.mUrl);
1741 mSavedState.putString(CURRTITLE, mCurrentState.mTitle);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001742 mSavedState.putBoolean(CLOSEONEXIT, mCloseOnExit);
1743 if (mAppId != null) {
1744 mSavedState.putString(APPID, mAppId);
1745 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001746 // Remember the parent tab so the relationship can be restored.
1747 if (mParentTab != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001748 mSavedState.putInt(PARENTTAB, mWebViewController.getTabControl().getTabIndex(
Grace Kloba22ac16e2009-10-07 18:00:23 -07001749 mParentTab));
1750 }
1751 return true;
1752 }
1753
1754 /*
1755 * Restore the state of the tab.
1756 */
1757 boolean restoreState(Bundle b) {
1758 if (b == null) {
1759 return false;
1760 }
1761 // Restore the internal state even if the WebView fails to restore.
1762 // This will maintain the app id, original url and close-on-exit values.
1763 mSavedState = null;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001764 mCloseOnExit = b.getBoolean(CLOSEONEXIT);
1765 mAppId = b.getString(APPID);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001766
1767 final WebBackForwardList list = mMainView.restoreState(b);
1768 if (list == null) {
1769 return false;
1770 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001771 return true;
1772 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04001773
Leon Scroggins1961ed22010-12-07 15:22:21 -05001774 public void updateBookmarkedStatus() {
John Recke969cc52010-12-21 17:24:43 -08001775 mDataController.queryBookmarkStatus(getUrl(), mIsBookmarkCallback);
Leon Scroggins1961ed22010-12-07 15:22:21 -05001776 }
1777
John Recke969cc52010-12-21 17:24:43 -08001778 private DataController.OnQueryUrlIsBookmark mIsBookmarkCallback
1779 = new DataController.OnQueryUrlIsBookmark() {
1780 @Override
1781 public void onQueryUrlIsBookmark(String url, boolean isBookmark) {
1782 if (mCurrentState.mUrl.equals(url)) {
1783 mCurrentState.mIsBookmarkedSite = isBookmark;
1784 mWebViewController.bookmarkedStatusHasChanged(Tab.this);
1785 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001786 }
John Recke969cc52010-12-21 17:24:43 -08001787 };
Michael Kolb1acef692011-03-08 14:12:06 -08001788
Grace Kloba22ac16e2009-10-07 18:00:23 -07001789}