blob: bab34581fd146036614997be8b281b844b736884 [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;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700135
136 // AsyncTask for downloading touch icons
137 DownloadTouchIcon mTouchIconLoader;
138
Michael Kolbeb95db42011-03-03 10:38:40 -0800139 private Bitmap mScreenshot;
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";
Michael Kolbeb95db42011-03-03 10:38:40 -0800196 static final String SCREENSHOT = "screenshot";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700197
198 // -------------------------------------------------------------------------
199
Leon Scroggins58d56c62010-01-28 15:12:40 -0500200 /**
201 * Private information regarding the latest voice search. If the Tab is not
202 * in voice search mode, this will be null.
203 */
204 private VoiceSearchData mVoiceSearchData;
205 /**
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400206 * Remove voice search mode from this tab.
207 */
208 public void revertVoiceSearchMode() {
209 if (mVoiceSearchData != null) {
210 mVoiceSearchData = null;
211 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700212 mWebViewController.revertVoiceSearchMode(this);
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400213 }
214 }
215 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700216
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400217 /**
Leon Scroggins58d56c62010-01-28 15:12:40 -0500218 * Return whether the tab is in voice search mode.
219 */
220 public boolean isInVoiceSearchMode() {
221 return mVoiceSearchData != null;
222 }
223 /**
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400224 * Return true if the Tab is in voice search mode and the voice search
225 * Intent came with a String identifying that Google provided the Intent.
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500226 */
227 public boolean voiceSearchSourceIsGoogle() {
228 return mVoiceSearchData != null && mVoiceSearchData.mSourceIsGoogle;
229 }
230 /**
Leon Scroggins58d56c62010-01-28 15:12:40 -0500231 * Get the title to display for the current voice search page. If the Tab
232 * is not in voice search mode, return null.
233 */
234 public String getVoiceDisplayTitle() {
235 if (mVoiceSearchData == null) return null;
236 return mVoiceSearchData.mLastVoiceSearchTitle;
237 }
238 /**
239 * Get the latest array of voice search results, to be passed to the
240 * BrowserProvider. If the Tab is not in voice search mode, return null.
241 */
242 public ArrayList<String> getVoiceSearchResults() {
243 if (mVoiceSearchData == null) return null;
244 return mVoiceSearchData.mVoiceSearchResults;
245 }
246 /**
247 * Activate voice search mode.
248 * @param intent Intent which has the results to use, or an index into the
249 * results when reusing the old results.
250 */
251 /* package */ void activateVoiceSearchMode(Intent intent) {
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500252 int index = 0;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500253 ArrayList<String> results = intent.getStringArrayListExtra(
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -0500254 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_STRINGS);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500255 if (results != null) {
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500256 ArrayList<String> urls = intent.getStringArrayListExtra(
257 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_URLS);
258 ArrayList<String> htmls = intent.getStringArrayListExtra(
259 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_HTML);
260 ArrayList<String> baseUrls = intent.getStringArrayListExtra(
261 RecognizerResultsIntent
262 .EXTRA_VOICE_SEARCH_RESULT_HTML_BASE_URLS);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500263 // This tab is now entering voice search mode for the first time, or
264 // a new voice search was done.
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500265 int size = results.size();
266 if (urls == null || size != urls.size()) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500267 throw new AssertionError("improper extras passed in Intent");
268 }
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500269 if (htmls == null || htmls.size() != size || baseUrls == null ||
270 (baseUrls.size() != size && baseUrls.size() != 1)) {
271 // If either of these arrays are empty/incorrectly sized, ignore
272 // them.
273 htmls = null;
274 baseUrls = null;
275 }
276 mVoiceSearchData = new VoiceSearchData(results, urls, htmls,
277 baseUrls);
Leon Scroggins9df94972010-03-08 18:20:35 -0500278 mVoiceSearchData.mHeaders = intent.getParcelableArrayListExtra(
279 RecognizerResultsIntent
280 .EXTRA_VOICE_SEARCH_RESULT_HTTP_HEADERS);
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500281 mVoiceSearchData.mSourceIsGoogle = intent.getBooleanExtra(
282 VoiceSearchData.SOURCE_IS_GOOGLE, false);
Leon Scroggins2ee4a5a2010-03-15 16:56:57 -0400283 mVoiceSearchData.mVoiceSearchIntent = new Intent(intent);
Leon Scrogginse10dde52010-03-08 19:53:03 -0500284 }
285 String extraData = intent.getStringExtra(
286 SearchManager.EXTRA_DATA_KEY);
287 if (extraData != null) {
288 index = Integer.parseInt(extraData);
289 if (index >= mVoiceSearchData.mVoiceSearchResults.size()) {
290 throw new AssertionError("index must be less than "
291 + "size of mVoiceSearchResults");
292 }
293 if (mVoiceSearchData.mSourceIsGoogle) {
294 Intent logIntent = new Intent(
295 LoggingEvents.ACTION_LOG_EVENT);
296 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
297 LoggingEvents.VoiceSearch.N_BEST_CHOOSE);
298 logIntent.putExtra(
299 LoggingEvents.VoiceSearch.EXTRA_N_BEST_CHOOSE_INDEX,
300 index);
301 mActivity.sendBroadcast(logIntent);
302 }
303 if (mVoiceSearchData.mVoiceSearchIntent != null) {
Leon Scroggins2ee4a5a2010-03-15 16:56:57 -0400304 // Copy the Intent, so that each history item will have its own
305 // Intent, with different (or none) extra data.
306 Intent latest = new Intent(mVoiceSearchData.mVoiceSearchIntent);
307 latest.putExtra(SearchManager.EXTRA_DATA_KEY, extraData);
308 mVoiceSearchData.mVoiceSearchIntent = latest;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500309 }
310 }
311 mVoiceSearchData.mLastVoiceSearchTitle
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500312 = mVoiceSearchData.mVoiceSearchResults.get(index);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500313 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700314 mWebViewController.activateVoiceSearchMode(mVoiceSearchData.mLastVoiceSearchTitle);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500315 }
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500316 if (mVoiceSearchData.mVoiceSearchHtmls != null) {
317 // When index was found it was already ensured that it was valid
318 String uriString = mVoiceSearchData.mVoiceSearchHtmls.get(index);
319 if (uriString != null) {
320 Uri dataUri = Uri.parse(uriString);
321 if (RecognizerResultsIntent.URI_SCHEME_INLINE.equals(
322 dataUri.getScheme())) {
323 // If there is only one base URL, use it. If there are
324 // more, there will be one for each index, so use the base
325 // URL corresponding to the index.
326 String baseUrl = mVoiceSearchData.mVoiceSearchBaseUrls.get(
327 mVoiceSearchData.mVoiceSearchBaseUrls.size() > 1 ?
328 index : 0);
329 mVoiceSearchData.mLastVoiceSearchUrl = baseUrl;
330 mMainView.loadDataWithBaseURL(baseUrl,
331 uriString.substring(RecognizerResultsIntent
332 .URI_SCHEME_INLINE.length() + 1), "text/html",
333 "utf-8", baseUrl);
334 return;
335 }
336 }
337 }
Leon Scroggins58d56c62010-01-28 15:12:40 -0500338 mVoiceSearchData.mLastVoiceSearchUrl
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500339 = mVoiceSearchData.mVoiceSearchUrls.get(index);
340 if (null == mVoiceSearchData.mLastVoiceSearchUrl) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700341 mVoiceSearchData.mLastVoiceSearchUrl = UrlUtils.smartUrlFilter(
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500342 mVoiceSearchData.mLastVoiceSearchTitle);
343 }
Leon Scroggins9df94972010-03-08 18:20:35 -0500344 Map<String, String> headers = null;
345 if (mVoiceSearchData.mHeaders != null) {
346 int bundleIndex = mVoiceSearchData.mHeaders.size() == 1 ? 0
347 : index;
348 Bundle bundle = mVoiceSearchData.mHeaders.get(bundleIndex);
349 if (bundle != null && !bundle.isEmpty()) {
350 Iterator<String> iter = bundle.keySet().iterator();
351 headers = new HashMap<String, String>();
352 while (iter.hasNext()) {
353 String key = iter.next();
354 headers.put(key, bundle.getString(key));
355 }
356 }
357 }
358 mMainView.loadUrl(mVoiceSearchData.mLastVoiceSearchUrl, headers);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500359 }
360 /* package */ static class VoiceSearchData {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500361 public VoiceSearchData(ArrayList<String> results,
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500362 ArrayList<String> urls, ArrayList<String> htmls,
363 ArrayList<String> baseUrls) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500364 mVoiceSearchResults = results;
365 mVoiceSearchUrls = urls;
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500366 mVoiceSearchHtmls = htmls;
367 mVoiceSearchBaseUrls = baseUrls;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500368 }
369 /*
370 * ArrayList of suggestions to be displayed when opening the
371 * SearchManager
372 */
373 public ArrayList<String> mVoiceSearchResults;
374 /*
375 * ArrayList of urls, associated with the suggestions in
376 * mVoiceSearchResults.
377 */
378 public ArrayList<String> mVoiceSearchUrls;
379 /*
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500380 * ArrayList holding content to load for each item in
381 * mVoiceSearchResults.
382 */
383 public ArrayList<String> mVoiceSearchHtmls;
384 /*
385 * ArrayList holding base urls for the items in mVoiceSearchResults.
386 * If non null, this will either have the same size as
387 * mVoiceSearchResults or have a size of 1, in which case all will use
388 * the same base url
389 */
390 public ArrayList<String> mVoiceSearchBaseUrls;
391 /*
Leon Scroggins58d56c62010-01-28 15:12:40 -0500392 * The last url provided by voice search. Used for comparison to see if
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500393 * we are going to a page by some method besides voice search.
Leon Scroggins58d56c62010-01-28 15:12:40 -0500394 */
395 public String mLastVoiceSearchUrl;
396 /**
397 * The last title used for voice search. Needed to update the title bar
398 * when switching tabs.
399 */
400 public String mLastVoiceSearchTitle;
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500401 /**
402 * Whether the Intent which turned on voice search mode contained the
403 * String signifying that Google was the source.
404 */
405 public boolean mSourceIsGoogle;
406 /**
Leon Scroggins9df94972010-03-08 18:20:35 -0500407 * List of headers to be passed into the WebView containing location
408 * information
409 */
410 public ArrayList<Bundle> mHeaders;
411 /**
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500412 * The Intent used to invoke voice search. Placed on the
413 * WebHistoryItem so that when coming back to a previous voice search
414 * page we can again activate voice search.
415 */
Leon Scrogginse10dde52010-03-08 19:53:03 -0500416 public Intent mVoiceSearchIntent;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500417 /**
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500418 * String used to identify Google as the source of voice search.
419 */
420 public static String SOURCE_IS_GOOGLE
421 = "android.speech.extras.SOURCE_IS_GOOGLE";
Leon Scroggins58d56c62010-01-28 15:12:40 -0500422 }
423
Grace Kloba22ac16e2009-10-07 18:00:23 -0700424 // Container class for the next error dialog that needs to be displayed
425 private class ErrorDialog {
426 public final int mTitle;
427 public final String mDescription;
428 public final int mError;
429 ErrorDialog(int title, String desc, int error) {
430 mTitle = title;
431 mDescription = desc;
432 mError = error;
433 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700434 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700435
436 private void processNextError() {
437 if (mQueuedErrors == null) {
438 return;
439 }
440 // The first one is currently displayed so just remove it.
441 mQueuedErrors.removeFirst();
442 if (mQueuedErrors.size() == 0) {
443 mQueuedErrors = null;
444 return;
445 }
446 showError(mQueuedErrors.getFirst());
447 }
448
449 private DialogInterface.OnDismissListener mDialogListener =
450 new DialogInterface.OnDismissListener() {
451 public void onDismiss(DialogInterface d) {
452 processNextError();
453 }
454 };
455 private LinkedList<ErrorDialog> mQueuedErrors;
456
457 private void queueError(int err, String desc) {
458 if (mQueuedErrors == null) {
459 mQueuedErrors = new LinkedList<ErrorDialog>();
460 }
461 for (ErrorDialog d : mQueuedErrors) {
462 if (d.mError == err) {
463 // Already saw a similar error, ignore the new one.
464 return;
465 }
466 }
467 ErrorDialog errDialog = new ErrorDialog(
468 err == WebViewClient.ERROR_FILE_NOT_FOUND ?
469 R.string.browserFrameFileErrorLabel :
470 R.string.browserFrameNetworkErrorLabel,
471 desc, err);
472 mQueuedErrors.addLast(errDialog);
473
474 // Show the dialog now if the queue was empty and it is in foreground
475 if (mQueuedErrors.size() == 1 && mInForeground) {
476 showError(errDialog);
477 }
478 }
479
480 private void showError(ErrorDialog errDialog) {
481 if (mInForeground) {
482 AlertDialog d = new AlertDialog.Builder(mActivity)
483 .setTitle(errDialog.mTitle)
484 .setMessage(errDialog.mDescription)
485 .setPositiveButton(R.string.ok, null)
486 .create();
487 d.setOnDismissListener(mDialogListener);
488 d.show();
489 }
490 }
491
492 // -------------------------------------------------------------------------
493 // WebViewClient implementation for the main WebView
494 // -------------------------------------------------------------------------
495
496 private final WebViewClient mWebViewClient = new WebViewClient() {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500497 private Message mDontResend;
498 private Message mResend;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700499 @Override
500 public void onPageStarted(WebView view, String url, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700501 mInPageLoad = true;
John Reck30c714c2010-12-16 17:30:34 -0800502 mPageLoadProgress = 0;
503 mCurrentState = new PageState(mActivity,
504 view.isPrivateBrowsingEnabled(), url, favicon);
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000505 mLoadStartTime = SystemClock.uptimeMillis();
Leon Scroggins58d56c62010-01-28 15:12:40 -0500506 if (mVoiceSearchData != null
507 && !url.equals(mVoiceSearchData.mLastVoiceSearchUrl)) {
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500508 if (mVoiceSearchData.mSourceIsGoogle) {
509 Intent i = new Intent(LoggingEvents.ACTION_LOG_EVENT);
510 i.putExtra(LoggingEvents.EXTRA_FLUSH, true);
511 mActivity.sendBroadcast(i);
512 }
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400513 revertVoiceSearchMode();
Leon Scroggins58d56c62010-01-28 15:12:40 -0500514 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700515
Grace Kloba22ac16e2009-10-07 18:00:23 -0700516
517 // If we start a touch icon load and then load a new page, we don't
518 // want to cancel the current touch icon loader. But, we do want to
519 // create a new one when the touch icon url is known.
520 if (mTouchIconLoader != null) {
521 mTouchIconLoader.mTab = null;
522 mTouchIconLoader = null;
523 }
524
525 // reset the error console
526 if (mErrorConsole != null) {
527 mErrorConsole.clearErrorMessages();
Michael Kolb8233fac2010-10-26 16:08:53 -0700528 if (mWebViewController.shouldShowErrorConsole()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700529 mErrorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
530 }
531 }
532
Grace Kloba22ac16e2009-10-07 18:00:23 -0700533 // finally update the UI in the activity if it is in the foreground
John Reck324d4402011-01-11 16:56:42 -0800534 mWebViewController.onPageStarted(Tab.this, view, favicon);
Leon Scroggins4cd97792010-12-03 15:31:56 -0500535
John Recke969cc52010-12-21 17:24:43 -0800536 updateBookmarkedStatus();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700537 }
538
539 @Override
540 public void onPageFinished(WebView view, String url) {
John Reck5b691842010-11-29 11:21:13 -0800541 if (!isPrivateBrowsingEnabled()) {
542 LogTag.logPageFinishedLoading(
543 url, SystemClock.uptimeMillis() - mLoadStartTime);
544 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700545 mInPageLoad = false;
John Reck30c714c2010-12-16 17:30:34 -0800546 // Sync state (in case of stop/timeout)
547 mCurrentState.mUrl = view.getUrl();
John Reck6c702ee2011-01-07 09:41:53 -0800548 if (mCurrentState.mUrl == null) {
549 mCurrentState.mUrl = url != null ? url : "";
550 }
John Reck30c714c2010-12-16 17:30:34 -0800551 mCurrentState.mTitle = view.getTitle();
552 mCurrentState.mFavicon = view.getFavicon();
553 if (!URLUtil.isHttpsUrl(mCurrentState.mUrl)) {
554 // In case we stop when loading an HTTPS page from an HTTP page
555 // but before a provisional load occurred
556 mCurrentState.mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
557 }
John Reck324d4402011-01-11 16:56:42 -0800558 mWebViewController.onPageFinished(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700559 }
560
561 // return true if want to hijack the url to let another app to handle it
562 @Override
563 public boolean shouldOverrideUrlLoading(WebView view, String url) {
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400564 if (voiceSearchSourceIsGoogle()) {
565 // This method is called when the user clicks on a link.
566 // VoiceSearchMode is turned off when the user leaves the
567 // Google results page, so at this point the user must be on
568 // that page. If the user clicked a link on that page, assume
569 // that the voice search was effective, and broadcast an Intent
570 // so a receiver can take note of that fact.
571 Intent logIntent = new Intent(LoggingEvents.ACTION_LOG_EVENT);
572 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
573 LoggingEvents.VoiceSearch.RESULT_CLICKED);
574 mActivity.sendBroadcast(logIntent);
575 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700576 if (mInForeground) {
Michael Kolb18eb3772010-12-10 14:29:51 -0800577 return mWebViewController.shouldOverrideUrlLoading(Tab.this,
578 view, url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700579 } else {
580 return false;
581 }
582 }
583
584 /**
585 * Updates the lock icon. This method is called when we discover another
586 * resource to be loaded for this page (for example, javascript). While
587 * we update the icon type, we do not update the lock icon itself until
588 * we are done loading, it is slightly more secure this way.
589 */
590 @Override
591 public void onLoadResource(WebView view, String url) {
592 if (url != null && url.length() > 0) {
593 // It is only if the page claims to be secure that we may have
594 // to update the lock:
John Reck30c714c2010-12-16 17:30:34 -0800595 if (mCurrentState.mLockIcon == LockIcon.LOCK_ICON_SECURE) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700596 // If NOT a 'safe' url, change the lock to mixed content!
597 if (!(URLUtil.isHttpsUrl(url) || URLUtil.isDataUrl(url)
598 || URLUtil.isAboutUrl(url))) {
John Reck30c714c2010-12-16 17:30:34 -0800599 mCurrentState.mLockIcon = LockIcon.LOCK_ICON_MIXED;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700600 }
601 }
602 }
603 }
604
605 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700606 * Show a dialog informing the user of the network error reported by
607 * WebCore if it is in the foreground.
608 */
609 @Override
610 public void onReceivedError(WebView view, int errorCode,
611 String description, String failingUrl) {
612 if (errorCode != WebViewClient.ERROR_HOST_LOOKUP &&
613 errorCode != WebViewClient.ERROR_CONNECT &&
614 errorCode != WebViewClient.ERROR_BAD_URL &&
615 errorCode != WebViewClient.ERROR_UNSUPPORTED_SCHEME &&
616 errorCode != WebViewClient.ERROR_FILE) {
617 queueError(errorCode, description);
618 }
Jeff Hamilton47654f42010-09-07 09:57:51 -0500619
620 // Don't log URLs when in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -0700621 if (!isPrivateBrowsingEnabled()) {
Jeff Hamilton47654f42010-09-07 09:57:51 -0500622 Log.e(LOGTAG, "onReceivedError " + errorCode + " " + failingUrl
623 + " " + description);
624 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700625 }
626
627 /**
628 * Check with the user if it is ok to resend POST data as the page they
629 * are trying to navigate to is the result of a POST.
630 */
631 @Override
632 public void onFormResubmission(WebView view, final Message dontResend,
633 final Message resend) {
634 if (!mInForeground) {
635 dontResend.sendToTarget();
636 return;
637 }
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500638 if (mDontResend != null) {
639 Log.w(LOGTAG, "onFormResubmission should not be called again "
640 + "while dialog is still up");
641 dontResend.sendToTarget();
642 return;
643 }
644 mDontResend = dontResend;
645 mResend = resend;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700646 new AlertDialog.Builder(mActivity).setTitle(
647 R.string.browserFrameFormResubmitLabel).setMessage(
648 R.string.browserFrameFormResubmitMessage)
649 .setPositiveButton(R.string.ok,
650 new DialogInterface.OnClickListener() {
651 public void onClick(DialogInterface dialog,
652 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500653 if (mResend != null) {
654 mResend.sendToTarget();
655 mResend = null;
656 mDontResend = null;
657 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700658 }
659 }).setNegativeButton(R.string.cancel,
660 new DialogInterface.OnClickListener() {
661 public void onClick(DialogInterface dialog,
662 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500663 if (mDontResend != null) {
664 mDontResend.sendToTarget();
665 mResend = null;
666 mDontResend = null;
667 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700668 }
669 }).setOnCancelListener(new OnCancelListener() {
670 public void onCancel(DialogInterface dialog) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500671 if (mDontResend != null) {
672 mDontResend.sendToTarget();
673 mResend = null;
674 mDontResend = null;
675 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700676 }
677 }).show();
678 }
679
680 /**
681 * Insert the url into the visited history database.
682 * @param url The url to be inserted.
683 * @param isReload True if this url is being reloaded.
684 * FIXME: Not sure what to do when reloading the page.
685 */
686 @Override
687 public void doUpdateVisitedHistory(WebView view, String url,
688 boolean isReload) {
John Reck324d4402011-01-11 16:56:42 -0800689 mWebViewController.doUpdateVisitedHistory(Tab.this, isReload);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700690 }
691
692 /**
693 * Displays SSL error(s) dialog to the user.
694 */
695 @Override
696 public void onReceivedSslError(final WebView view,
697 final SslErrorHandler handler, final SslError error) {
698 if (!mInForeground) {
699 handler.cancel();
John Reck30c714c2010-12-16 17:30:34 -0800700 setLockIconType(LockIcon.LOCK_ICON_UNSECURE);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700701 return;
702 }
703 if (BrowserSettings.getInstance().showSecurityWarnings()) {
704 final LayoutInflater factory =
705 LayoutInflater.from(mActivity);
706 final View warningsView =
707 factory.inflate(R.layout.ssl_warnings, null);
708 final LinearLayout placeholder =
709 (LinearLayout)warningsView.findViewById(R.id.placeholder);
710
711 if (error.hasError(SslError.SSL_UNTRUSTED)) {
712 LinearLayout ll = (LinearLayout)factory
713 .inflate(R.layout.ssl_warning, null);
714 ((TextView)ll.findViewById(R.id.warning))
715 .setText(R.string.ssl_untrusted);
716 placeholder.addView(ll);
717 }
718
719 if (error.hasError(SslError.SSL_IDMISMATCH)) {
720 LinearLayout ll = (LinearLayout)factory
721 .inflate(R.layout.ssl_warning, null);
722 ((TextView)ll.findViewById(R.id.warning))
723 .setText(R.string.ssl_mismatch);
724 placeholder.addView(ll);
725 }
726
727 if (error.hasError(SslError.SSL_EXPIRED)) {
728 LinearLayout ll = (LinearLayout)factory
729 .inflate(R.layout.ssl_warning, null);
730 ((TextView)ll.findViewById(R.id.warning))
731 .setText(R.string.ssl_expired);
732 placeholder.addView(ll);
733 }
734
735 if (error.hasError(SslError.SSL_NOTYETVALID)) {
736 LinearLayout ll = (LinearLayout)factory
737 .inflate(R.layout.ssl_warning, null);
738 ((TextView)ll.findViewById(R.id.warning))
739 .setText(R.string.ssl_not_yet_valid);
740 placeholder.addView(ll);
741 }
742
743 new AlertDialog.Builder(mActivity).setTitle(
744 R.string.security_warning).setIcon(
745 android.R.drawable.ic_dialog_alert).setView(
746 warningsView).setPositiveButton(R.string.ssl_continue,
747 new DialogInterface.OnClickListener() {
748 public void onClick(DialogInterface dialog,
749 int whichButton) {
750 handler.proceed();
751 }
752 }).setNeutralButton(R.string.view_certificate,
753 new DialogInterface.OnClickListener() {
754 public void onClick(DialogInterface dialog,
755 int whichButton) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700756 mWebViewController.showSslCertificateOnError(view,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700757 handler, error);
758 }
Ben Murdocha49b8292010-11-16 11:56:04 +0000759 }).setNegativeButton(R.string.ssl_go_back,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700760 new DialogInterface.OnClickListener() {
761 public void onClick(DialogInterface dialog,
762 int whichButton) {
John Reck30c714c2010-12-16 17:30:34 -0800763 dialog.cancel();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700764 }
765 }).setOnCancelListener(
766 new DialogInterface.OnCancelListener() {
767 public void onCancel(DialogInterface dialog) {
768 handler.cancel();
John Reck30c714c2010-12-16 17:30:34 -0800769 setLockIconType(LockIcon.LOCK_ICON_UNSECURE);
770 mWebViewController.onUserCanceledSsl(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700771 }
772 }).show();
773 } else {
774 handler.proceed();
775 }
776 }
777
778 /**
779 * Handles an HTTP authentication request.
780 *
781 * @param handler The authentication handler
782 * @param host The host
783 * @param realm The realm
784 */
785 @Override
786 public void onReceivedHttpAuthRequest(WebView view,
787 final HttpAuthHandler handler, final String host,
788 final String realm) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700789 mWebViewController.onReceivedHttpAuthRequest(Tab.this, view, handler, host, realm);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700790 }
791
792 @Override
John Reck438bf462011-01-12 18:11:46 -0800793 public WebResourceResponse shouldInterceptRequest(WebView view,
794 String url) {
795 WebResourceResponse res = HomeProvider.shouldInterceptRequest(
796 mActivity, url);
797 return res;
798 }
799
800 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700801 public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
802 if (!mInForeground) {
803 return false;
804 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700805 return mWebViewController.shouldOverrideKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700806 }
807
808 @Override
809 public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700810 if (!mInForeground) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700811 return;
812 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700813 mWebViewController.onUnhandledKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700814 }
815 };
816
817 // -------------------------------------------------------------------------
818 // WebChromeClient implementation for the main WebView
819 // -------------------------------------------------------------------------
820
821 private final WebChromeClient mWebChromeClient = new WebChromeClient() {
822 // Helper method to create a new tab or sub window.
823 private void createWindow(final boolean dialog, final Message msg) {
824 WebView.WebViewTransport transport =
825 (WebView.WebViewTransport) msg.obj;
826 if (dialog) {
827 createSubWindow();
Michael Kolb8233fac2010-10-26 16:08:53 -0700828 mWebViewController.attachSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700829 transport.setWebView(mSubView);
830 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -0700831 final Tab newTab = mWebViewController.openTabAndShow(
Michael Kolb18eb3772010-12-10 14:29:51 -0800832 Tab.this,
Michael Kolb8233fac2010-10-26 16:08:53 -0700833 IntentHandler.EMPTY_URL_DATA, false, null);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700834 if (newTab != Tab.this) {
835 Tab.this.addChildTab(newTab);
836 }
837 transport.setWebView(newTab.getWebView());
838 }
839 msg.sendToTarget();
840 }
841
842 @Override
843 public boolean onCreateWindow(WebView view, final boolean dialog,
844 final boolean userGesture, final Message resultMsg) {
845 // only allow new window or sub window for the foreground case
846 if (!mInForeground) {
847 return false;
848 }
849 // Short-circuit if we can't create any more tabs or sub windows.
850 if (dialog && mSubView != null) {
851 new AlertDialog.Builder(mActivity)
852 .setTitle(R.string.too_many_subwindows_dialog_title)
853 .setIcon(android.R.drawable.ic_dialog_alert)
854 .setMessage(R.string.too_many_subwindows_dialog_message)
855 .setPositiveButton(R.string.ok, null)
856 .show();
857 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700858 } else if (!mWebViewController.getTabControl().canCreateNewTab()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700859 new AlertDialog.Builder(mActivity)
860 .setTitle(R.string.too_many_windows_dialog_title)
861 .setIcon(android.R.drawable.ic_dialog_alert)
862 .setMessage(R.string.too_many_windows_dialog_message)
863 .setPositiveButton(R.string.ok, null)
864 .show();
865 return false;
866 }
867
868 // Short-circuit if this was a user gesture.
869 if (userGesture) {
870 createWindow(dialog, resultMsg);
871 return true;
872 }
873
874 // Allow the popup and create the appropriate window.
875 final AlertDialog.OnClickListener allowListener =
876 new AlertDialog.OnClickListener() {
877 public void onClick(DialogInterface d,
878 int which) {
879 createWindow(dialog, resultMsg);
880 }
881 };
882
883 // Block the popup by returning a null WebView.
884 final AlertDialog.OnClickListener blockListener =
885 new AlertDialog.OnClickListener() {
886 public void onClick(DialogInterface d, int which) {
887 resultMsg.sendToTarget();
888 }
889 };
890
891 // Build a confirmation dialog to display to the user.
892 final AlertDialog d =
893 new AlertDialog.Builder(mActivity)
894 .setTitle(R.string.attention)
895 .setIcon(android.R.drawable.ic_dialog_alert)
896 .setMessage(R.string.popup_window_attempt)
897 .setPositiveButton(R.string.allow, allowListener)
898 .setNegativeButton(R.string.block, blockListener)
899 .setCancelable(false)
900 .create();
901
902 // Show the confirmation dialog.
903 d.show();
904 return true;
905 }
906
907 @Override
Patrick Scotteb5061b2009-11-18 15:00:30 -0500908 public void onRequestFocus(WebView view) {
909 if (!mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700910 mWebViewController.switchToTab(mWebViewController.getTabControl().getTabIndex(
Patrick Scotteb5061b2009-11-18 15:00:30 -0500911 Tab.this));
912 }
913 }
914
915 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700916 public void onCloseWindow(WebView window) {
917 if (mParentTab != null) {
918 // JavaScript can only close popup window.
919 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700920 mWebViewController.switchToTab(mWebViewController.getTabControl()
Grace Kloba22ac16e2009-10-07 18:00:23 -0700921 .getTabIndex(mParentTab));
922 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700923 mWebViewController.closeTab(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700924 }
925 }
926
927 @Override
928 public void onProgressChanged(WebView view, int newProgress) {
John Reck30c714c2010-12-16 17:30:34 -0800929 mPageLoadProgress = newProgress;
930 mWebViewController.onProgressChanged(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700931 }
932
933 @Override
Leon Scroggins21d9b902010-03-11 09:33:11 -0500934 public void onReceivedTitle(WebView view, final String title) {
John Reck30c714c2010-12-16 17:30:34 -0800935 mCurrentState.mTitle = title;
Michael Kolb8233fac2010-10-26 16:08:53 -0700936 mWebViewController.onReceivedTitle(Tab.this, title);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700937 }
938
939 @Override
940 public void onReceivedIcon(WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -0800941 mCurrentState.mFavicon = icon;
Michael Kolb8233fac2010-10-26 16:08:53 -0700942 mWebViewController.onFavicon(Tab.this, view, icon);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700943 }
944
945 @Override
946 public void onReceivedTouchIconUrl(WebView view, String url,
947 boolean precomposed) {
948 final ContentResolver cr = mActivity.getContentResolver();
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400949 // Let precomposed icons take precedence over non-composed
950 // icons.
951 if (precomposed && mTouchIconLoader != null) {
952 mTouchIconLoader.cancel(false);
953 mTouchIconLoader = null;
954 }
955 // Have only one async task at a time.
956 if (mTouchIconLoader == null) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700957 mTouchIconLoader = new DownloadTouchIcon(Tab.this,
958 mActivity, cr, view);
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400959 mTouchIconLoader.execute(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700960 }
961 }
962
963 @Override
964 public void onShowCustomView(View view,
965 WebChromeClient.CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700966 if (mInForeground) mWebViewController.showCustomView(Tab.this, view,
967 callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700968 }
969
970 @Override
971 public void onHideCustomView() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700972 if (mInForeground) mWebViewController.hideCustomView();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700973 }
974
975 /**
976 * The origin has exceeded its database quota.
977 * @param url the URL that exceeded the quota
978 * @param databaseIdentifier the identifier of the database on which the
979 * transaction that caused the quota overflow was run
980 * @param currentQuota the current quota for the origin.
981 * @param estimatedSize the estimated size of the database.
982 * @param totalUsedQuota is the sum of all origins' quota.
983 * @param quotaUpdater The callback to run when a decision to allow or
984 * deny quota has been made. Don't forget to call this!
985 */
986 @Override
987 public void onExceededDatabaseQuota(String url,
988 String databaseIdentifier, long currentQuota, long estimatedSize,
989 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
990 BrowserSettings.getInstance().getWebStorageSizeManager()
991 .onExceededDatabaseQuota(url, databaseIdentifier,
992 currentQuota, estimatedSize, totalUsedQuota,
993 quotaUpdater);
994 }
995
996 /**
997 * The Application Cache has exceeded its max size.
998 * @param spaceNeeded is the amount of disk space that would be needed
999 * in order for the last appcache operation to succeed.
1000 * @param totalUsedQuota is the sum of all origins' quota.
1001 * @param quotaUpdater A callback to inform the WebCore thread that a
1002 * new app cache size is available. This callback must always
1003 * be executed at some point to ensure that the sleeping
1004 * WebCore thread is woken up.
1005 */
1006 @Override
1007 public void onReachedMaxAppCacheSize(long spaceNeeded,
1008 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
1009 BrowserSettings.getInstance().getWebStorageSizeManager()
1010 .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota,
1011 quotaUpdater);
1012 }
1013
1014 /**
1015 * Instructs the browser to show a prompt to ask the user to set the
1016 * Geolocation permission state for the specified origin.
1017 * @param origin The origin for which Geolocation permissions are
1018 * requested.
1019 * @param callback The callback to call once the user has set the
1020 * Geolocation permission state.
1021 */
1022 @Override
1023 public void onGeolocationPermissionsShowPrompt(String origin,
1024 GeolocationPermissions.Callback callback) {
1025 if (mInForeground) {
Grace Kloba50c241e2010-04-20 11:07:50 -07001026 getGeolocationPermissionsPrompt().show(origin, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001027 }
1028 }
1029
1030 /**
1031 * Instructs the browser to hide the Geolocation permissions prompt.
1032 */
1033 @Override
1034 public void onGeolocationPermissionsHidePrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001035 if (mInForeground && mGeolocationPermissionsPrompt != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001036 mGeolocationPermissionsPrompt.hide();
1037 }
1038 }
1039
Ben Murdoch65acc352009-11-19 18:16:04 +00001040 /* Adds a JavaScript error message to the system log and if the JS
1041 * console is enabled in the about:debug options, to that console
1042 * also.
Ben Murdochc42addf2010-01-28 15:19:59 +00001043 * @param consoleMessage the message object.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001044 */
1045 @Override
Ben Murdochc42addf2010-01-28 15:19:59 +00001046 public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001047 if (mInForeground) {
1048 // call getErrorConsole(true) so it will create one if needed
1049 ErrorConsoleView errorConsole = getErrorConsole(true);
Ben Murdochc42addf2010-01-28 15:19:59 +00001050 errorConsole.addErrorMessage(consoleMessage);
Michael Kolb8233fac2010-10-26 16:08:53 -07001051 if (mWebViewController.shouldShowErrorConsole()
1052 && errorConsole.getShowState() !=
1053 ErrorConsoleView.SHOW_MAXIMIZED) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001054 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
1055 }
1056 }
Ben Murdochc42addf2010-01-28 15:19:59 +00001057
Jeff Hamilton47654f42010-09-07 09:57:51 -05001058 // Don't log console messages in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001059 if (isPrivateBrowsingEnabled()) return true;
Jeff Hamilton47654f42010-09-07 09:57:51 -05001060
Ben Murdochc42addf2010-01-28 15:19:59 +00001061 String message = "Console: " + consoleMessage.message() + " "
1062 + consoleMessage.sourceId() + ":"
1063 + consoleMessage.lineNumber();
1064
1065 switch (consoleMessage.messageLevel()) {
1066 case TIP:
1067 Log.v(CONSOLE_LOGTAG, message);
1068 break;
1069 case LOG:
1070 Log.i(CONSOLE_LOGTAG, message);
1071 break;
1072 case WARNING:
1073 Log.w(CONSOLE_LOGTAG, message);
1074 break;
1075 case ERROR:
1076 Log.e(CONSOLE_LOGTAG, message);
1077 break;
1078 case DEBUG:
1079 Log.d(CONSOLE_LOGTAG, message);
1080 break;
1081 }
1082
1083 return true;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001084 }
1085
1086 /**
1087 * Ask the browser for an icon to represent a <video> element.
1088 * This icon will be used if the Web page did not specify a poster attribute.
1089 * @return Bitmap The icon or null if no such icon is available.
1090 */
1091 @Override
1092 public Bitmap getDefaultVideoPoster() {
1093 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001094 return mWebViewController.getDefaultVideoPoster();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001095 }
1096 return null;
1097 }
1098
1099 /**
1100 * Ask the host application for a custom progress view to show while
1101 * a <video> is loading.
1102 * @return View The progress view.
1103 */
1104 @Override
1105 public View getVideoLoadingProgressView() {
1106 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001107 return mWebViewController.getVideoLoadingProgressView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001108 }
1109 return null;
1110 }
1111
1112 @Override
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01001113 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001114 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001115 mWebViewController.openFileChooser(uploadMsg, acceptType);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001116 } else {
1117 uploadMsg.onReceiveValue(null);
1118 }
1119 }
1120
1121 /**
1122 * Deliver a list of already-visited URLs
1123 */
1124 @Override
1125 public void getVisitedHistory(final ValueCallback<String[]> callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001126 mWebViewController.getVisitedHistory(callback);
1127 }
Ben Murdoch8029a772010-11-16 11:58:21 +00001128
1129 @Override
1130 public void setupAutoFill(Message message) {
1131 // Prompt the user to set up their profile.
1132 final Message msg = message;
1133 AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
Ben Murdoch1d676b62011-01-17 12:54:24 +00001134 LayoutInflater inflater = (LayoutInflater) mActivity.getSystemService(
1135 Context.LAYOUT_INFLATER_SERVICE);
1136 final View layout = inflater.inflate(R.layout.setup_autofill_dialog, null);
1137
1138 builder.setView(layout)
1139 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1140 @Override
1141 public void onClick(DialogInterface dialog, int id) {
1142 CheckBox disableAutoFill = (CheckBox) layout.findViewById(
1143 R.id.setup_autofill_dialog_disable_autofill);
1144
1145 if (disableAutoFill.isChecked()) {
1146 // Disable autofill and show a toast with how to turn it on again.
1147 BrowserSettings s = BrowserSettings.getInstance();
1148 s.addObserver(mMainView.getSettings());
1149 s.disableAutoFill(mActivity);
1150 s.update();
1151 Toast.makeText(mActivity,
1152 R.string.autofill_setup_dialog_negative_toast,
1153 Toast.LENGTH_LONG).show();
1154 } else {
1155 // Take user to the AutoFill profile editor. When they return,
1156 // we will send the message that we pass here which will trigger
1157 // the form to get filled out with their new profile.
1158 mWebViewController.setupAutoFill(msg);
1159 }
1160 }
1161 })
1162 .setNegativeButton(R.string.cancel, null)
1163 .show();
Ben Murdoch8029a772010-11-16 11:58:21 +00001164 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001165 };
1166
1167 // -------------------------------------------------------------------------
1168 // WebViewClient implementation for the sub window
1169 // -------------------------------------------------------------------------
1170
1171 // Subclass of WebViewClient used in subwindows to notify the main
1172 // WebViewClient of certain WebView activities.
1173 private static class SubWindowClient extends WebViewClient {
1174 // The main WebViewClient.
1175 private final WebViewClient mClient;
Michael Kolb8233fac2010-10-26 16:08:53 -07001176 private final WebViewController mController;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001177
Michael Kolb8233fac2010-10-26 16:08:53 -07001178 SubWindowClient(WebViewClient client, WebViewController controller) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001179 mClient = client;
Michael Kolb8233fac2010-10-26 16:08:53 -07001180 mController = controller;
Leon Scroggins III211ba542010-04-19 13:21:13 -04001181 }
1182 @Override
1183 public void onPageStarted(WebView view, String url, Bitmap favicon) {
1184 // Unlike the others, do not call mClient's version, which would
1185 // change the progress bar. However, we do want to remove the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001186 // find or select dialog.
Michael Kolb8233fac2010-10-26 16:08:53 -07001187 mController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001188 }
1189 @Override
1190 public void doUpdateVisitedHistory(WebView view, String url,
1191 boolean isReload) {
1192 mClient.doUpdateVisitedHistory(view, url, isReload);
1193 }
1194 @Override
1195 public boolean shouldOverrideUrlLoading(WebView view, String url) {
1196 return mClient.shouldOverrideUrlLoading(view, url);
1197 }
1198 @Override
1199 public void onReceivedSslError(WebView view, SslErrorHandler handler,
1200 SslError error) {
1201 mClient.onReceivedSslError(view, handler, error);
1202 }
1203 @Override
1204 public void onReceivedHttpAuthRequest(WebView view,
1205 HttpAuthHandler handler, String host, String realm) {
1206 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
1207 }
1208 @Override
1209 public void onFormResubmission(WebView view, Message dontResend,
1210 Message resend) {
1211 mClient.onFormResubmission(view, dontResend, resend);
1212 }
1213 @Override
1214 public void onReceivedError(WebView view, int errorCode,
1215 String description, String failingUrl) {
1216 mClient.onReceivedError(view, errorCode, description, failingUrl);
1217 }
1218 @Override
1219 public boolean shouldOverrideKeyEvent(WebView view,
1220 android.view.KeyEvent event) {
1221 return mClient.shouldOverrideKeyEvent(view, event);
1222 }
1223 @Override
1224 public void onUnhandledKeyEvent(WebView view,
1225 android.view.KeyEvent event) {
1226 mClient.onUnhandledKeyEvent(view, event);
1227 }
1228 }
1229
1230 // -------------------------------------------------------------------------
1231 // WebChromeClient implementation for the sub window
1232 // -------------------------------------------------------------------------
1233
1234 private class SubWindowChromeClient extends WebChromeClient {
1235 // The main WebChromeClient.
1236 private final WebChromeClient mClient;
1237
1238 SubWindowChromeClient(WebChromeClient client) {
1239 mClient = client;
1240 }
1241 @Override
1242 public void onProgressChanged(WebView view, int newProgress) {
1243 mClient.onProgressChanged(view, newProgress);
1244 }
1245 @Override
1246 public boolean onCreateWindow(WebView view, boolean dialog,
1247 boolean userGesture, android.os.Message resultMsg) {
1248 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
1249 }
1250 @Override
1251 public void onCloseWindow(WebView window) {
1252 if (window != mSubView) {
1253 Log.e(LOGTAG, "Can't close the window");
1254 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001255 mWebViewController.dismissSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001256 }
1257 }
1258
1259 // -------------------------------------------------------------------------
1260
Michael Kolb8233fac2010-10-26 16:08:53 -07001261 // TODO temporarily use activity here
1262 // remove later
1263
Grace Kloba22ac16e2009-10-07 18:00:23 -07001264 // Construct a new tab
Michael Kolb8233fac2010-10-26 16:08:53 -07001265 Tab(WebViewController wvcontroller, WebView w, boolean closeOnExit, String appId,
Grace Kloba22ac16e2009-10-07 18:00:23 -07001266 String url) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001267 mWebViewController = wvcontroller;
1268 mActivity = mWebViewController.getActivity();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001269 mCloseOnExit = closeOnExit;
1270 mAppId = appId;
John Recke969cc52010-12-21 17:24:43 -08001271 mDataController = DataController.getInstance(mActivity);
John Reck30c714c2010-12-16 17:30:34 -08001272 mCurrentState = new PageState(mActivity, w.isPrivateBrowsingEnabled());
Michael Kolb8233fac2010-10-26 16:08:53 -07001273 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001274 mInForeground = false;
1275
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001276 mDownloadListener = new DownloadListener() {
1277 public void onDownloadStart(String url, String userAgent,
1278 String contentDisposition, String mimetype,
1279 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001280 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001281 mimetype, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001282 }
1283 };
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001284 mWebBackForwardListClient = new WebBackForwardListClient() {
1285 @Override
1286 public void onNewHistoryItem(WebHistoryItem item) {
1287 if (isInVoiceSearchMode()) {
1288 item.setCustomData(mVoiceSearchData.mVoiceSearchIntent);
1289 }
1290 }
1291 @Override
1292 public void onIndexChanged(WebHistoryItem item, int index) {
1293 Object data = item.getCustomData();
1294 if (data != null && data instanceof Intent) {
1295 activateVoiceSearchMode((Intent) data);
1296 }
1297 }
1298 };
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001299
Grace Kloba22ac16e2009-10-07 18:00:23 -07001300 setWebView(w);
1301 }
1302
1303 /**
1304 * Sets the WebView for this tab, correctly removing the old WebView from
1305 * the container view.
1306 */
1307 void setWebView(WebView w) {
1308 if (mMainView == w) {
1309 return;
1310 }
Michael Kolba713ec82010-11-29 17:27:06 -08001311
Grace Kloba22ac16e2009-10-07 18:00:23 -07001312 // If the WebView is changing, the page will be reloaded, so any ongoing
1313 // Geolocation permission requests are void.
Grace Kloba50c241e2010-04-20 11:07:50 -07001314 if (mGeolocationPermissionsPrompt != null) {
1315 mGeolocationPermissionsPrompt.hide();
1316 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001317
Michael Kolba713ec82010-11-29 17:27:06 -08001318 mWebViewController.onSetWebView(this, w);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001319
1320 // set the new one
1321 mMainView = w;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001322 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001323 if (mMainView != null) {
1324 mMainView.setWebViewClient(mWebViewClient);
1325 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001326 // Attach DownloadManager so that downloads can start in an active
1327 // or a non-active window. This can happen when going to a site that
1328 // does a redirect after a period of time. The user could have
1329 // switched to another tab while waiting for the download to start.
1330 mMainView.setDownloadListener(mDownloadListener);
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001331 mMainView.setWebBackForwardListClient(mWebBackForwardListClient);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001332 }
1333 }
1334
1335 /**
1336 * Destroy the tab's main WebView and subWindow if any
1337 */
1338 void destroy() {
1339 if (mMainView != null) {
1340 dismissSubWindow();
1341 BrowserSettings.getInstance().deleteObserver(mMainView.getSettings());
1342 // save the WebView to call destroy() after detach it from the tab
1343 WebView webView = mMainView;
1344 setWebView(null);
1345 webView.destroy();
1346 }
1347 }
1348
1349 /**
1350 * Remove the tab from the parent
1351 */
1352 void removeFromTree() {
1353 // detach the children
1354 if (mChildTabs != null) {
1355 for(Tab t : mChildTabs) {
1356 t.setParentTab(null);
1357 }
1358 }
1359 // remove itself from the parent list
1360 if (mParentTab != null) {
1361 mParentTab.mChildTabs.remove(this);
1362 }
1363 }
1364
1365 /**
1366 * Create a new subwindow unless a subwindow already exists.
1367 * @return True if a new subwindow was created. False if one already exists.
1368 */
1369 boolean createSubWindow() {
1370 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001371 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001372 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001373 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001374 mSubView.setWebChromeClient(new SubWindowChromeClient(
1375 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001376 // Set a different DownloadListener for the mSubView, since it will
1377 // just need to dismiss the mSubView, rather than close the Tab
1378 mSubView.setDownloadListener(new DownloadListener() {
1379 public void onDownloadStart(String url, String userAgent,
1380 String contentDisposition, String mimetype,
1381 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001382 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001383 contentDisposition, mimetype, contentLength);
1384 if (mSubView.copyBackForwardList().getSize() == 0) {
1385 // This subwindow was opened for the sole purpose of
1386 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001387 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001388 }
1389 }
1390 });
Grace Kloba22ac16e2009-10-07 18:00:23 -07001391 mSubView.setOnCreateContextMenuListener(mActivity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001392 return true;
1393 }
1394 return false;
1395 }
1396
1397 /**
1398 * Dismiss the subWindow for the tab.
1399 */
1400 void dismissSubWindow() {
1401 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001402 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001403 BrowserSettings.getInstance().deleteObserver(
1404 mSubView.getSettings());
1405 mSubView.destroy();
1406 mSubView = null;
1407 mSubViewContainer = null;
1408 }
1409 }
1410
Grace Kloba22ac16e2009-10-07 18:00:23 -07001411
1412 /**
1413 * Set the parent tab of this tab.
1414 */
1415 void setParentTab(Tab parent) {
1416 mParentTab = parent;
1417 // This tab may have been freed due to low memory. If that is the case,
1418 // the parent tab index is already saved. If we are changing that index
1419 // (most likely due to removing the parent tab) we must update the
1420 // parent tab index in the saved Bundle.
1421 if (mSavedState != null) {
1422 if (parent == null) {
1423 mSavedState.remove(PARENTTAB);
1424 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -07001425 mSavedState.putInt(PARENTTAB, mWebViewController.getTabControl()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001426 .getTabIndex(parent));
1427 }
1428 }
1429 }
1430
1431 /**
1432 * When a Tab is created through the content of another Tab, then we
1433 * associate the Tabs.
1434 * @param child the Tab that was created from this Tab
1435 */
1436 void addChildTab(Tab child) {
1437 if (mChildTabs == null) {
1438 mChildTabs = new Vector<Tab>();
1439 }
1440 mChildTabs.add(child);
1441 child.setParentTab(this);
1442 }
1443
1444 Vector<Tab> getChildTabs() {
1445 return mChildTabs;
1446 }
1447
1448 void resume() {
1449 if (mMainView != null) {
1450 mMainView.onResume();
1451 if (mSubView != null) {
1452 mSubView.onResume();
1453 }
1454 }
1455 }
1456
1457 void pause() {
1458 if (mMainView != null) {
1459 mMainView.onPause();
1460 if (mSubView != null) {
1461 mSubView.onPause();
1462 }
1463 }
1464 }
1465
1466 void putInForeground() {
1467 mInForeground = true;
1468 resume();
1469 mMainView.setOnCreateContextMenuListener(mActivity);
1470 if (mSubView != null) {
1471 mSubView.setOnCreateContextMenuListener(mActivity);
1472 }
1473 // Show the pending error dialog if the queue is not empty
1474 if (mQueuedErrors != null && mQueuedErrors.size() > 0) {
1475 showError(mQueuedErrors.getFirst());
1476 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001477 mWebViewController.bookmarkedStatusHasChanged(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001478 }
1479
1480 void putInBackground() {
1481 mInForeground = false;
1482 pause();
1483 mMainView.setOnCreateContextMenuListener(null);
1484 if (mSubView != null) {
1485 mSubView.setOnCreateContextMenuListener(null);
1486 }
1487 }
1488
Michael Kolb8233fac2010-10-26 16:08:53 -07001489 boolean inForeground() {
1490 return mInForeground;
1491 }
1492
Grace Kloba22ac16e2009-10-07 18:00:23 -07001493 /**
1494 * Return the top window of this tab; either the subwindow if it is not
1495 * null or the main window.
1496 * @return The top window of this tab.
1497 */
1498 WebView getTopWindow() {
1499 if (mSubView != null) {
1500 return mSubView;
1501 }
1502 return mMainView;
1503 }
1504
1505 /**
1506 * Return the main window of this tab. Note: if a tab is freed in the
1507 * background, this can return null. It is only guaranteed to be
1508 * non-null for the current tab.
1509 * @return The main WebView of this tab.
1510 */
1511 WebView getWebView() {
1512 return mMainView;
1513 }
1514
Michael Kolba713ec82010-11-29 17:27:06 -08001515 void setViewContainer(View container) {
1516 mContainer = container;
1517 }
1518
Michael Kolb8233fac2010-10-26 16:08:53 -07001519 View getViewContainer() {
1520 return mContainer;
1521 }
1522
Grace Kloba22ac16e2009-10-07 18:00:23 -07001523 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001524 * Return whether private browsing is enabled for the main window of
1525 * this tab.
1526 * @return True if private browsing is enabled.
1527 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001528 boolean isPrivateBrowsingEnabled() {
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001529 WebView webView = getWebView();
1530 if (webView == null) {
1531 return false;
1532 }
1533 return webView.isPrivateBrowsingEnabled();
1534 }
1535
1536 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001537 * Return the subwindow of this tab or null if there is no subwindow.
1538 * @return The subwindow of this tab or null.
1539 */
1540 WebView getSubWebView() {
1541 return mSubView;
1542 }
1543
Michael Kolb1514bb72010-11-22 09:11:48 -08001544 void setSubWebView(WebView subView) {
1545 mSubView = subView;
1546 }
1547
Michael Kolb8233fac2010-10-26 16:08:53 -07001548 View getSubViewContainer() {
1549 return mSubViewContainer;
1550 }
1551
Michael Kolb1514bb72010-11-22 09:11:48 -08001552 void setSubViewContainer(View subViewContainer) {
1553 mSubViewContainer = subViewContainer;
1554 }
1555
Grace Kloba22ac16e2009-10-07 18:00:23 -07001556 /**
1557 * @return The geolocation permissions prompt for this tab.
1558 */
1559 GeolocationPermissionsPrompt getGeolocationPermissionsPrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001560 if (mGeolocationPermissionsPrompt == null) {
1561 ViewStub stub = (ViewStub) mContainer
1562 .findViewById(R.id.geolocation_permissions_prompt);
1563 mGeolocationPermissionsPrompt = (GeolocationPermissionsPrompt) stub
1564 .inflate();
1565 mGeolocationPermissionsPrompt.init();
1566 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001567 return mGeolocationPermissionsPrompt;
1568 }
1569
1570 /**
1571 * @return The application id string
1572 */
1573 String getAppId() {
1574 return mAppId;
1575 }
1576
1577 /**
1578 * Set the application id string
1579 * @param id
1580 */
1581 void setAppId(String id) {
1582 mAppId = id;
1583 }
1584
Grace Kloba22ac16e2009-10-07 18:00:23 -07001585 String getUrl() {
John Reck324d4402011-01-11 16:56:42 -08001586 return UrlUtils.filteredUrl(mCurrentState.mUrl);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001587 }
1588
John Reck49a603c2011-03-03 09:33:05 -08001589 String getOriginalUrl() {
1590 if (mMainView == null) {
1591 return "";
1592 }
1593 return UrlUtils.filteredUrl(mMainView.getOriginalUrl());
1594 }
1595
Grace Kloba22ac16e2009-10-07 18:00:23 -07001596 /**
John Reck30c714c2010-12-16 17:30:34 -08001597 * Get the title of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001598 */
1599 String getTitle() {
John Reck30c714c2010-12-16 17:30:34 -08001600 if (mCurrentState.mTitle == null && mInPageLoad) {
1601 return mActivity.getString(R.string.title_bar_loading);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001602 }
John Reck30c714c2010-12-16 17:30:34 -08001603 return mCurrentState.mTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001604 }
1605
1606 /**
John Reck30c714c2010-12-16 17:30:34 -08001607 * Get the favicon of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001608 */
1609 Bitmap getFavicon() {
John Reck30c714c2010-12-16 17:30:34 -08001610 return mCurrentState.mFavicon;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001611 }
1612
John Recke969cc52010-12-21 17:24:43 -08001613 public boolean isBookmarkedSite() {
1614 return mCurrentState.mIsBookmarkedSite;
1615 }
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001616
Grace Kloba22ac16e2009-10-07 18:00:23 -07001617 /**
1618 * Return the tab's error console. Creates the console if createIfNEcessary
1619 * is true and we haven't already created the console.
1620 * @param createIfNecessary Flag to indicate if the console should be
1621 * created if it has not been already.
1622 * @return The tab's error console, or null if one has not been created and
1623 * createIfNecessary is false.
1624 */
1625 ErrorConsoleView getErrorConsole(boolean createIfNecessary) {
1626 if (createIfNecessary && mErrorConsole == null) {
1627 mErrorConsole = new ErrorConsoleView(mActivity);
1628 mErrorConsole.setWebView(mMainView);
1629 }
1630 return mErrorConsole;
1631 }
1632
1633 /**
1634 * If this Tab was created through another Tab, then this method returns
1635 * that Tab.
1636 * @return the Tab parent or null
1637 */
1638 public Tab getParentTab() {
1639 return mParentTab;
1640 }
1641
1642 /**
1643 * Return whether this tab should be closed when it is backing out of the
1644 * first page.
1645 * @return TRUE if this tab should be closed when exit.
1646 */
1647 boolean closeOnExit() {
1648 return mCloseOnExit;
1649 }
1650
John Reck30c714c2010-12-16 17:30:34 -08001651 private void setLockIconType(LockIcon icon) {
1652 mCurrentState.mLockIcon = icon;
1653 mWebViewController.onUpdatedLockIcon(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001654 }
1655
1656 /**
1657 * @return The tab's lock icon type.
1658 */
John Reck30c714c2010-12-16 17:30:34 -08001659 LockIcon getLockIconType() {
1660 return mCurrentState.mLockIcon;
1661 }
1662
1663 int getLoadProgress() {
1664 if (mInPageLoad) {
1665 return mPageLoadProgress;
1666 }
1667 return 100;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001668 }
1669
1670 /**
1671 * @return TRUE if onPageStarted is called while onPageFinished is not
1672 * called yet.
1673 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001674 boolean inPageLoad() {
1675 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001676 }
1677
1678 // force mInLoad to be false. This should only be called before closing the
1679 // tab to ensure BrowserActivity's pauseWebViewTimers() is called correctly.
Michael Kolb8233fac2010-10-26 16:08:53 -07001680 void clearInPageLoad() {
1681 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001682 }
1683
Grace Kloba22ac16e2009-10-07 18:00:23 -07001684 /**
John Reck30c714c2010-12-16 17:30:34 -08001685 * Get the cached saved state bundle.
1686 * @return cached state bundle
Grace Kloba22ac16e2009-10-07 18:00:23 -07001687 */
1688 Bundle getSavedState() {
1689 return mSavedState;
1690 }
1691
1692 /**
1693 * Set the saved state.
1694 */
1695 void setSavedState(Bundle state) {
1696 mSavedState = state;
1697 }
1698
1699 /**
1700 * @return TRUE if succeed in saving the state.
1701 */
1702 boolean saveState() {
1703 // If the WebView is null it means we ran low on memory and we already
1704 // stored the saved state in mSavedState.
1705 if (mMainView == null) {
1706 return mSavedState != null;
1707 }
1708
1709 mSavedState = new Bundle();
1710 final WebBackForwardList list = mMainView.saveState(mSavedState);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001711
1712 // Store some extra info for displaying the tab in the picker.
1713 final WebHistoryItem item = list != null ? list.getCurrentItem() : null;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001714
John Reck30c714c2010-12-16 17:30:34 -08001715 mSavedState.putString(CURRURL, mCurrentState.mUrl);
1716 mSavedState.putString(CURRTITLE, mCurrentState.mTitle);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001717 mSavedState.putBoolean(CLOSEONEXIT, mCloseOnExit);
1718 if (mAppId != null) {
1719 mSavedState.putString(APPID, mAppId);
1720 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001721 // Remember the parent tab so the relationship can be restored.
1722 if (mParentTab != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001723 mSavedState.putInt(PARENTTAB, mWebViewController.getTabControl().getTabIndex(
Grace Kloba22ac16e2009-10-07 18:00:23 -07001724 mParentTab));
1725 }
Michael Kolbeb95db42011-03-03 10:38:40 -08001726 if (mScreenshot != null) {
1727 mSavedState.putParcelable(SCREENSHOT, mScreenshot);
1728 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001729 return true;
1730 }
1731
1732 /*
1733 * Restore the state of the tab.
1734 */
1735 boolean restoreState(Bundle b) {
1736 if (b == null) {
1737 return false;
1738 }
1739 // Restore the internal state even if the WebView fails to restore.
1740 // This will maintain the app id, original url and close-on-exit values.
1741 mSavedState = null;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001742 mCloseOnExit = b.getBoolean(CLOSEONEXIT);
1743 mAppId = b.getString(APPID);
Michael Kolbeb95db42011-03-03 10:38:40 -08001744 mScreenshot = b.getParcelable(SCREENSHOT);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001745
1746 final WebBackForwardList list = mMainView.restoreState(b);
1747 if (list == null) {
1748 return false;
1749 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001750 return true;
1751 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04001752
Leon Scroggins1961ed22010-12-07 15:22:21 -05001753 public void updateBookmarkedStatus() {
John Recke969cc52010-12-21 17:24:43 -08001754 mDataController.queryBookmarkStatus(getUrl(), mIsBookmarkCallback);
Leon Scroggins1961ed22010-12-07 15:22:21 -05001755 }
1756
John Recke969cc52010-12-21 17:24:43 -08001757 private DataController.OnQueryUrlIsBookmark mIsBookmarkCallback
1758 = new DataController.OnQueryUrlIsBookmark() {
1759 @Override
1760 public void onQueryUrlIsBookmark(String url, boolean isBookmark) {
1761 if (mCurrentState.mUrl.equals(url)) {
1762 mCurrentState.mIsBookmarkedSite = isBookmark;
1763 mWebViewController.bookmarkedStatusHasChanged(Tab.this);
1764 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001765 }
John Recke969cc52010-12-21 17:24:43 -08001766 };
Michael Kolb1acef692011-03-08 14:12:06 -08001767
Michael Kolbeb95db42011-03-03 10:38:40 -08001768 public void setScreenshot(Bitmap screenshot) {
1769 mScreenshot = screenshot;
1770 }
1771
1772 public Bitmap getScreenshot() {
1773 return mScreenshot;
1774 }
1775
Grace Kloba22ac16e2009-10-07 18:00:23 -07001776}