blob: 78ae98735f4fed02dd4f016ae540fcf33c44983a [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
Michael Kolb8233fac2010-10-26 16:08:53 -070019import android.app.Activity;
Grace Kloba22ac16e2009-10-07 18:00:23 -070020import android.app.AlertDialog;
Leon Scroggins58d56c62010-01-28 15:12:40 -050021import android.app.SearchManager;
Grace Kloba22ac16e2009-10-07 18:00:23 -070022import android.content.ContentResolver;
John Reck1cf4b792011-07-26 10:22:22 -070023import android.content.ContentUris;
John Reckd8c74522011-06-14 08:45:00 -070024import android.content.ContentValues;
John Reck30c714c2010-12-16 17:30:34 -080025import android.content.Context;
Grace Kloba22ac16e2009-10-07 18:00:23 -070026import android.content.DialogInterface;
Michael Kolbfe251992010-07-08 15:41:55 -070027import android.content.DialogInterface.OnCancelListener;
Jeff Hamilton8ce956c2010-08-17 11:13:53 -050028import android.content.Intent;
John Reck1cf4b792011-07-26 10:22:22 -070029import android.database.Cursor;
Grace Kloba22ac16e2009-10-07 18:00:23 -070030import android.graphics.Bitmap;
John Reck8cc92352011-07-06 17:41:52 -070031import android.graphics.Bitmap.CompressFormat;
Michael Kolb9ef259a2011-07-12 15:33:08 -070032import android.graphics.BitmapFactory;
33import android.graphics.Canvas;
34import android.graphics.Picture;
Grace Kloba22ac16e2009-10-07 18:00:23 -070035import android.net.Uri;
36import android.net.http.SslError;
Grace Kloba22ac16e2009-10-07 18:00:23 -070037import android.os.Bundle;
Michael Kolb9ef259a2011-07-12 15:33:08 -070038import android.os.Handler;
Grace Kloba22ac16e2009-10-07 18:00:23 -070039import android.os.Message;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +000040import android.os.SystemClock;
Brian Carlstrom8862c1d2011-06-02 01:05:55 -070041import android.security.KeyChain;
Brian Carlstromaa09cd82011-06-09 16:04:40 -070042import android.security.KeyChainAliasCallback;
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -050043import android.speech.RecognizerResultsIntent;
John Reck24f18262011-06-17 14:47:20 -070044import android.text.TextUtils;
Grace Kloba22ac16e2009-10-07 18:00:23 -070045import android.util.Log;
46import android.view.KeyEvent;
47import android.view.LayoutInflater;
48import android.view.View;
Grace Kloba50c241e2010-04-20 11:07:50 -070049import android.view.ViewStub;
Brian Carlstrom8862c1d2011-06-02 01:05:55 -070050import android.webkit.ClientCertRequestHandler;
Ben Murdochc42addf2010-01-28 15:19:59 +000051import android.webkit.ConsoleMessage;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -050052import android.webkit.DownloadListener;
Grace Kloba22ac16e2009-10-07 18:00:23 -070053import android.webkit.GeolocationPermissions;
54import android.webkit.HttpAuthHandler;
55import android.webkit.SslErrorHandler;
56import android.webkit.URLUtil;
57import android.webkit.ValueCallback;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -050058import android.webkit.WebBackForwardListClient;
Grace Kloba22ac16e2009-10-07 18:00:23 -070059import android.webkit.WebChromeClient;
60import android.webkit.WebHistoryItem;
John Reck438bf462011-01-12 18:11:46 -080061import android.webkit.WebResourceResponse;
Grace Kloba22ac16e2009-10-07 18:00:23 -070062import android.webkit.WebStorage;
63import android.webkit.WebView;
Michael Kolb9ef259a2011-07-12 15:33:08 -070064import android.webkit.WebView.PictureListener;
Grace Kloba22ac16e2009-10-07 18:00:23 -070065import android.webkit.WebViewClient;
Ben Murdoch1d676b62011-01-17 12:54:24 +000066import android.widget.CheckBox;
Grace Kloba22ac16e2009-10-07 18:00:23 -070067import android.widget.LinearLayout;
68import android.widget.TextView;
Ben Murdoch8029a772010-11-16 11:58:21 +000069import android.widget.Toast;
Grace Kloba22ac16e2009-10-07 18:00:23 -070070
John Reck8ee633f2011-08-09 16:00:35 -070071import com.android.browser.TabControl.OnThumbnailUpdatedListener;
John Reck541f55a2011-06-07 16:34:43 -070072import com.android.browser.homepages.HomeProvider;
John Reck1cf4b792011-07-26 10:22:22 -070073import com.android.browser.provider.BrowserProvider2.Thumbnails;
John Reck8cc92352011-07-06 17:41:52 -070074import com.android.browser.provider.SnapshotProvider.Snapshots;
John Reck541f55a2011-06-07 16:34:43 -070075import com.android.common.speech.LoggingEvents;
76
77import java.io.ByteArrayOutputStream;
John Reck1cf4b792011-07-26 10:22:22 -070078import java.nio.ByteBuffer;
Michael Kolbfe251992010-07-08 15:41:55 -070079import java.util.ArrayList;
80import java.util.HashMap;
81import java.util.Iterator;
82import java.util.LinkedList;
83import java.util.Map;
84import java.util.Vector;
John Reck8cc92352011-07-06 17:41:52 -070085import java.util.zip.GZIPOutputStream;
Michael Kolbfe251992010-07-08 15:41:55 -070086
Grace Kloba22ac16e2009-10-07 18:00:23 -070087/**
88 * Class for maintaining Tabs with a main WebView and a subwindow.
89 */
Michael Kolb9ef259a2011-07-12 15:33:08 -070090class Tab implements PictureListener {
Michael Kolb8233fac2010-10-26 16:08:53 -070091
Grace Kloba22ac16e2009-10-07 18:00:23 -070092 // Log Tag
93 private static final String LOGTAG = "Tab";
Mathew Inwood1dd8e822011-08-03 14:34:29 +010094 private static final boolean LOGD_ENABLED = com.android.browser.Browser.LOGD_ENABLED;
Ben Murdochc42addf2010-01-28 15:19:59 +000095 // Special case the logtag for messages for the Console to make it easier to
96 // filter them and match the logtag used for these messages in older versions
97 // of the browser.
98 private static final String CONSOLE_LOGTAG = "browser";
99
Michael Kolb9ef259a2011-07-12 15:33:08 -0700100 private static final int MSG_CAPTURE = 42;
John Reck8ee633f2011-08-09 16:00:35 -0700101 private static final int CAPTURE_DELAY = 100;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700102
John Reck1cf4b792011-07-26 10:22:22 -0700103 private static Bitmap sDefaultFavicon;
104
John Reck30c714c2010-12-16 17:30:34 -0800105 public enum LockIcon {
106 LOCK_ICON_UNSECURE,
107 LOCK_ICON_SECURE,
108 LOCK_ICON_MIXED,
109 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700110
Michael Kolb14612442011-06-24 13:06:29 -0700111 Context mContext;
John Reckd8c74522011-06-14 08:45:00 -0700112 protected WebViewController mWebViewController;
Michael Kolb8233fac2010-10-26 16:08:53 -0700113
Michael Kolbc831b632011-05-11 09:30:34 -0700114 // The tab ID
John Reckd8c74522011-06-14 08:45:00 -0700115 private long mId = -1;
Michael Kolbc831b632011-05-11 09:30:34 -0700116
Grace Kloba22ac16e2009-10-07 18:00:23 -0700117 // The Geolocation permissions prompt
118 private GeolocationPermissionsPrompt mGeolocationPermissionsPrompt;
119 // Main WebView wrapper
Michael Kolba713ec82010-11-29 17:27:06 -0800120 private View mContainer;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700121 // Main WebView
122 private WebView mMainView;
123 // Subwindow container
124 private View mSubViewContainer;
125 // Subwindow WebView
126 private WebView mSubView;
127 // Saved bundle for when we are running low on memory. It contains the
128 // information needed to restore the WebView if the user goes back to the
129 // tab.
130 private Bundle mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700131 // Parent Tab. This is the Tab that created this Tab, or null if the Tab was
132 // created by the UI
Michael Kolbc831b632011-05-11 09:30:34 -0700133 private Tab mParent;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700134 // Tab that constructed by this Tab. This is used when this Tab is
135 // destroyed, it clears all mParentTab values in the children.
Michael Kolbc831b632011-05-11 09:30:34 -0700136 private Vector<Tab> mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700137 // If true, the tab is in the foreground of the current activity.
138 private boolean mInForeground;
Michael Kolb8233fac2010-10-26 16:08:53 -0700139 // If true, the tab is in page loading state (after onPageStarted,
140 // before onPageFinsihed)
141 private boolean mInPageLoad;
John Reck30c714c2010-12-16 17:30:34 -0800142 // The last reported progress of the current page
143 private int mPageLoadProgress;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000144 // The time the load started, used to find load page time
145 private long mLoadStartTime;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700146 // Application identifier used to find tabs that another application wants
147 // to reuse.
148 private String mAppId;
Michael Kolbe28b3472011-08-04 16:54:31 -0700149 // flag to indicate if tab should be closed on back
150 private boolean mCloseOnBack;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700151 // Keep the original url around to avoid killing the old WebView if the url
152 // has not changed.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700153 // Error console for the tab
154 private ErrorConsoleView mErrorConsole;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -0500155 // The listener that gets invoked when a download is started from the
156 // mMainView
157 private final DownloadListener mDownloadListener;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500158 // Listener used to know when we move forward or back in the history list.
159 private final WebBackForwardListClient mWebBackForwardListClient;
John Recke969cc52010-12-21 17:24:43 -0800160 private DataController mDataController;
Patrick Scott92066772011-03-10 08:46:27 -0500161 // State of the auto-login request.
162 private DeviceAccountLogin mDeviceAccountLogin;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700163
164 // AsyncTask for downloading touch icons
165 DownloadTouchIcon mTouchIconLoader;
166
John Reck35e9dd62011-04-25 09:01:54 -0700167 private BrowserSettings mSettings;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700168 private int mCaptureWidth;
169 private int mCaptureHeight;
170 private Bitmap mCapture;
171 private Handler mHandler;
172
Mathew Inwood1dd8e822011-08-03 14:34:29 +0100173 /**
174 * See {@link #clearBackStackWhenItemAdded(String)}.
175 */
176 private String mClearHistoryMatchUrl;
177
John Reck1cf4b792011-07-26 10:22:22 -0700178 private static synchronized Bitmap getDefaultFavicon(Context context) {
179 if (sDefaultFavicon == null) {
180 sDefaultFavicon = BitmapFactory.decodeResource(
181 context.getResources(), R.drawable.app_web_browser_sm);
182 }
183 return sDefaultFavicon;
184 }
Michael Kolbeb95db42011-03-03 10:38:40 -0800185
John Reck30c714c2010-12-16 17:30:34 -0800186 // All the state needed for a page
John Reckd8c74522011-06-14 08:45:00 -0700187 protected static class PageState {
John Reck30c714c2010-12-16 17:30:34 -0800188 String mUrl;
John Reckdb22ec42011-06-29 11:31:24 -0700189 String mOriginalUrl;
John Reck30c714c2010-12-16 17:30:34 -0800190 String mTitle;
191 LockIcon mLockIcon;
192 Bitmap mFavicon;
John Recke969cc52010-12-21 17:24:43 -0800193 Boolean mIsBookmarkedSite = false;
John Reck30c714c2010-12-16 17:30:34 -0800194
195 PageState(Context c, boolean incognito) {
196 if (incognito) {
John Reckdb22ec42011-06-29 11:31:24 -0700197 mOriginalUrl = mUrl = "browser:incognito";
John Reck30c714c2010-12-16 17:30:34 -0800198 mTitle = c.getString(R.string.new_incognito_tab);
John Reck30c714c2010-12-16 17:30:34 -0800199 } else {
John Reckdb22ec42011-06-29 11:31:24 -0700200 mOriginalUrl = mUrl = "";
John Reck30c714c2010-12-16 17:30:34 -0800201 mTitle = c.getString(R.string.new_tab);
John Reck30c714c2010-12-16 17:30:34 -0800202 }
John Reck1cf4b792011-07-26 10:22:22 -0700203 mFavicon = null;
John Reck30c714c2010-12-16 17:30:34 -0800204 mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
205 }
206
207 PageState(Context c, boolean incognito, String url, Bitmap favicon) {
John Reckdb22ec42011-06-29 11:31:24 -0700208 mOriginalUrl = mUrl = url;
John Reck30c714c2010-12-16 17:30:34 -0800209 mTitle = null;
210 if (URLUtil.isHttpsUrl(url)) {
211 mLockIcon = LockIcon.LOCK_ICON_SECURE;
212 } else {
213 mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
214 }
John Reck1cf4b792011-07-26 10:22:22 -0700215 mFavicon = favicon;
John Reck30c714c2010-12-16 17:30:34 -0800216 }
John Reck1cf4b792011-07-26 10:22:22 -0700217
Grace Kloba22ac16e2009-10-07 18:00:23 -0700218 }
219
John Reck30c714c2010-12-16 17:30:34 -0800220 // The current/loading page's state
John Reckd8c74522011-06-14 08:45:00 -0700221 protected PageState mCurrentState;
John Reck30c714c2010-12-16 17:30:34 -0800222
Grace Kloba22ac16e2009-10-07 18:00:23 -0700223 // Used for saving and restoring each Tab
Michael Kolbc831b632011-05-11 09:30:34 -0700224 static final String ID = "ID";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700225 static final String CURRURL = "currentUrl";
226 static final String CURRTITLE = "currentTitle";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700227 static final String PARENTTAB = "parentTab";
228 static final String APPID = "appid";
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700229 static final String INCOGNITO = "privateBrowsingEnabled";
John Reckb0a86db2011-05-24 14:05:58 -0700230 static final String USERAGENT = "useragent";
Michael Kolbe28b3472011-08-04 16:54:31 -0700231 static final String CLOSEFLAG = "closeOnBack";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700232
233 // -------------------------------------------------------------------------
234
Leon Scroggins58d56c62010-01-28 15:12:40 -0500235 /**
236 * Private information regarding the latest voice search. If the Tab is not
237 * in voice search mode, this will be null.
238 */
239 private VoiceSearchData mVoiceSearchData;
240 /**
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400241 * Remove voice search mode from this tab.
242 */
243 public void revertVoiceSearchMode() {
244 if (mVoiceSearchData != null) {
245 mVoiceSearchData = null;
246 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700247 mWebViewController.revertVoiceSearchMode(this);
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400248 }
249 }
250 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700251
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400252 /**
Leon Scroggins58d56c62010-01-28 15:12:40 -0500253 * Return whether the tab is in voice search mode.
254 */
255 public boolean isInVoiceSearchMode() {
256 return mVoiceSearchData != null;
257 }
258 /**
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400259 * Return true if the Tab is in voice search mode and the voice search
260 * Intent came with a String identifying that Google provided the Intent.
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500261 */
262 public boolean voiceSearchSourceIsGoogle() {
263 return mVoiceSearchData != null && mVoiceSearchData.mSourceIsGoogle;
264 }
265 /**
Leon Scroggins58d56c62010-01-28 15:12:40 -0500266 * Get the title to display for the current voice search page. If the Tab
267 * is not in voice search mode, return null.
268 */
269 public String getVoiceDisplayTitle() {
270 if (mVoiceSearchData == null) return null;
271 return mVoiceSearchData.mLastVoiceSearchTitle;
272 }
273 /**
274 * Get the latest array of voice search results, to be passed to the
275 * BrowserProvider. If the Tab is not in voice search mode, return null.
276 */
277 public ArrayList<String> getVoiceSearchResults() {
278 if (mVoiceSearchData == null) return null;
279 return mVoiceSearchData.mVoiceSearchResults;
280 }
281 /**
282 * Activate voice search mode.
283 * @param intent Intent which has the results to use, or an index into the
284 * results when reusing the old results.
285 */
286 /* package */ void activateVoiceSearchMode(Intent intent) {
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500287 int index = 0;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500288 ArrayList<String> results = intent.getStringArrayListExtra(
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -0500289 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_STRINGS);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500290 if (results != null) {
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500291 ArrayList<String> urls = intent.getStringArrayListExtra(
292 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_URLS);
293 ArrayList<String> htmls = intent.getStringArrayListExtra(
294 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_HTML);
295 ArrayList<String> baseUrls = intent.getStringArrayListExtra(
296 RecognizerResultsIntent
297 .EXTRA_VOICE_SEARCH_RESULT_HTML_BASE_URLS);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500298 // This tab is now entering voice search mode for the first time, or
299 // a new voice search was done.
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500300 int size = results.size();
301 if (urls == null || size != urls.size()) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500302 throw new AssertionError("improper extras passed in Intent");
303 }
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500304 if (htmls == null || htmls.size() != size || baseUrls == null ||
305 (baseUrls.size() != size && baseUrls.size() != 1)) {
306 // If either of these arrays are empty/incorrectly sized, ignore
307 // them.
308 htmls = null;
309 baseUrls = null;
310 }
311 mVoiceSearchData = new VoiceSearchData(results, urls, htmls,
312 baseUrls);
Leon Scroggins9df94972010-03-08 18:20:35 -0500313 mVoiceSearchData.mHeaders = intent.getParcelableArrayListExtra(
314 RecognizerResultsIntent
315 .EXTRA_VOICE_SEARCH_RESULT_HTTP_HEADERS);
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500316 mVoiceSearchData.mSourceIsGoogle = intent.getBooleanExtra(
317 VoiceSearchData.SOURCE_IS_GOOGLE, false);
Leon Scroggins2ee4a5a2010-03-15 16:56:57 -0400318 mVoiceSearchData.mVoiceSearchIntent = new Intent(intent);
Leon Scrogginse10dde52010-03-08 19:53:03 -0500319 }
320 String extraData = intent.getStringExtra(
321 SearchManager.EXTRA_DATA_KEY);
322 if (extraData != null) {
323 index = Integer.parseInt(extraData);
324 if (index >= mVoiceSearchData.mVoiceSearchResults.size()) {
325 throw new AssertionError("index must be less than "
326 + "size of mVoiceSearchResults");
327 }
328 if (mVoiceSearchData.mSourceIsGoogle) {
329 Intent logIntent = new Intent(
330 LoggingEvents.ACTION_LOG_EVENT);
331 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
332 LoggingEvents.VoiceSearch.N_BEST_CHOOSE);
333 logIntent.putExtra(
334 LoggingEvents.VoiceSearch.EXTRA_N_BEST_CHOOSE_INDEX,
335 index);
Michael Kolb14612442011-06-24 13:06:29 -0700336 mContext.sendBroadcast(logIntent);
Leon Scrogginse10dde52010-03-08 19:53:03 -0500337 }
338 if (mVoiceSearchData.mVoiceSearchIntent != null) {
Leon Scroggins2ee4a5a2010-03-15 16:56:57 -0400339 // Copy the Intent, so that each history item will have its own
340 // Intent, with different (or none) extra data.
341 Intent latest = new Intent(mVoiceSearchData.mVoiceSearchIntent);
342 latest.putExtra(SearchManager.EXTRA_DATA_KEY, extraData);
343 mVoiceSearchData.mVoiceSearchIntent = latest;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500344 }
345 }
346 mVoiceSearchData.mLastVoiceSearchTitle
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500347 = mVoiceSearchData.mVoiceSearchResults.get(index);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500348 if (mInForeground) {
Michael Kolb11d19782011-03-20 10:17:40 -0700349 mWebViewController.activateVoiceSearchMode(
350 mVoiceSearchData.mLastVoiceSearchTitle,
351 mVoiceSearchData.mVoiceSearchResults);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500352 }
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500353 if (mVoiceSearchData.mVoiceSearchHtmls != null) {
354 // When index was found it was already ensured that it was valid
355 String uriString = mVoiceSearchData.mVoiceSearchHtmls.get(index);
356 if (uriString != null) {
357 Uri dataUri = Uri.parse(uriString);
358 if (RecognizerResultsIntent.URI_SCHEME_INLINE.equals(
359 dataUri.getScheme())) {
360 // If there is only one base URL, use it. If there are
361 // more, there will be one for each index, so use the base
362 // URL corresponding to the index.
363 String baseUrl = mVoiceSearchData.mVoiceSearchBaseUrls.get(
364 mVoiceSearchData.mVoiceSearchBaseUrls.size() > 1 ?
365 index : 0);
366 mVoiceSearchData.mLastVoiceSearchUrl = baseUrl;
367 mMainView.loadDataWithBaseURL(baseUrl,
368 uriString.substring(RecognizerResultsIntent
369 .URI_SCHEME_INLINE.length() + 1), "text/html",
370 "utf-8", baseUrl);
371 return;
372 }
373 }
374 }
Leon Scroggins58d56c62010-01-28 15:12:40 -0500375 mVoiceSearchData.mLastVoiceSearchUrl
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500376 = mVoiceSearchData.mVoiceSearchUrls.get(index);
377 if (null == mVoiceSearchData.mLastVoiceSearchUrl) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700378 mVoiceSearchData.mLastVoiceSearchUrl = UrlUtils.smartUrlFilter(
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500379 mVoiceSearchData.mLastVoiceSearchTitle);
380 }
Leon Scroggins9df94972010-03-08 18:20:35 -0500381 Map<String, String> headers = null;
382 if (mVoiceSearchData.mHeaders != null) {
383 int bundleIndex = mVoiceSearchData.mHeaders.size() == 1 ? 0
384 : index;
385 Bundle bundle = mVoiceSearchData.mHeaders.get(bundleIndex);
386 if (bundle != null && !bundle.isEmpty()) {
387 Iterator<String> iter = bundle.keySet().iterator();
388 headers = new HashMap<String, String>();
389 while (iter.hasNext()) {
390 String key = iter.next();
391 headers.put(key, bundle.getString(key));
392 }
393 }
394 }
395 mMainView.loadUrl(mVoiceSearchData.mLastVoiceSearchUrl, headers);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500396 }
397 /* package */ static class VoiceSearchData {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500398 public VoiceSearchData(ArrayList<String> results,
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500399 ArrayList<String> urls, ArrayList<String> htmls,
400 ArrayList<String> baseUrls) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500401 mVoiceSearchResults = results;
402 mVoiceSearchUrls = urls;
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500403 mVoiceSearchHtmls = htmls;
404 mVoiceSearchBaseUrls = baseUrls;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500405 }
406 /*
407 * ArrayList of suggestions to be displayed when opening the
408 * SearchManager
409 */
410 public ArrayList<String> mVoiceSearchResults;
411 /*
412 * ArrayList of urls, associated with the suggestions in
413 * mVoiceSearchResults.
414 */
415 public ArrayList<String> mVoiceSearchUrls;
416 /*
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500417 * ArrayList holding content to load for each item in
418 * mVoiceSearchResults.
419 */
420 public ArrayList<String> mVoiceSearchHtmls;
421 /*
422 * ArrayList holding base urls for the items in mVoiceSearchResults.
423 * If non null, this will either have the same size as
424 * mVoiceSearchResults or have a size of 1, in which case all will use
425 * the same base url
426 */
427 public ArrayList<String> mVoiceSearchBaseUrls;
428 /*
Leon Scroggins58d56c62010-01-28 15:12:40 -0500429 * The last url provided by voice search. Used for comparison to see if
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500430 * we are going to a page by some method besides voice search.
Leon Scroggins58d56c62010-01-28 15:12:40 -0500431 */
432 public String mLastVoiceSearchUrl;
433 /**
434 * The last title used for voice search. Needed to update the title bar
435 * when switching tabs.
436 */
437 public String mLastVoiceSearchTitle;
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500438 /**
439 * Whether the Intent which turned on voice search mode contained the
440 * String signifying that Google was the source.
441 */
442 public boolean mSourceIsGoogle;
443 /**
Leon Scroggins9df94972010-03-08 18:20:35 -0500444 * List of headers to be passed into the WebView containing location
445 * information
446 */
447 public ArrayList<Bundle> mHeaders;
448 /**
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500449 * The Intent used to invoke voice search. Placed on the
450 * WebHistoryItem so that when coming back to a previous voice search
451 * page we can again activate voice search.
452 */
Leon Scrogginse10dde52010-03-08 19:53:03 -0500453 public Intent mVoiceSearchIntent;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500454 /**
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500455 * String used to identify Google as the source of voice search.
456 */
457 public static String SOURCE_IS_GOOGLE
458 = "android.speech.extras.SOURCE_IS_GOOGLE";
Leon Scroggins58d56c62010-01-28 15:12:40 -0500459 }
460
Grace Kloba22ac16e2009-10-07 18:00:23 -0700461 // Container class for the next error dialog that needs to be displayed
462 private class ErrorDialog {
463 public final int mTitle;
464 public final String mDescription;
465 public final int mError;
466 ErrorDialog(int title, String desc, int error) {
467 mTitle = title;
468 mDescription = desc;
469 mError = error;
470 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700471 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700472
473 private void processNextError() {
474 if (mQueuedErrors == null) {
475 return;
476 }
477 // The first one is currently displayed so just remove it.
478 mQueuedErrors.removeFirst();
479 if (mQueuedErrors.size() == 0) {
480 mQueuedErrors = null;
481 return;
482 }
483 showError(mQueuedErrors.getFirst());
484 }
485
486 private DialogInterface.OnDismissListener mDialogListener =
487 new DialogInterface.OnDismissListener() {
488 public void onDismiss(DialogInterface d) {
489 processNextError();
490 }
491 };
492 private LinkedList<ErrorDialog> mQueuedErrors;
493
494 private void queueError(int err, String desc) {
495 if (mQueuedErrors == null) {
496 mQueuedErrors = new LinkedList<ErrorDialog>();
497 }
498 for (ErrorDialog d : mQueuedErrors) {
499 if (d.mError == err) {
500 // Already saw a similar error, ignore the new one.
501 return;
502 }
503 }
504 ErrorDialog errDialog = new ErrorDialog(
505 err == WebViewClient.ERROR_FILE_NOT_FOUND ?
506 R.string.browserFrameFileErrorLabel :
507 R.string.browserFrameNetworkErrorLabel,
508 desc, err);
509 mQueuedErrors.addLast(errDialog);
510
511 // Show the dialog now if the queue was empty and it is in foreground
512 if (mQueuedErrors.size() == 1 && mInForeground) {
513 showError(errDialog);
514 }
515 }
516
517 private void showError(ErrorDialog errDialog) {
518 if (mInForeground) {
Michael Kolb14612442011-06-24 13:06:29 -0700519 AlertDialog d = new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700520 .setTitle(errDialog.mTitle)
521 .setMessage(errDialog.mDescription)
522 .setPositiveButton(R.string.ok, null)
523 .create();
524 d.setOnDismissListener(mDialogListener);
525 d.show();
526 }
527 }
528
529 // -------------------------------------------------------------------------
530 // WebViewClient implementation for the main WebView
531 // -------------------------------------------------------------------------
532
533 private final WebViewClient mWebViewClient = new WebViewClient() {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500534 private Message mDontResend;
535 private Message mResend;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700536 @Override
537 public void onPageStarted(WebView view, String url, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700538 mInPageLoad = true;
John Reck30c714c2010-12-16 17:30:34 -0800539 mPageLoadProgress = 0;
Michael Kolb14612442011-06-24 13:06:29 -0700540 mCurrentState = new PageState(mContext,
John Reck30c714c2010-12-16 17:30:34 -0800541 view.isPrivateBrowsingEnabled(), url, favicon);
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000542 mLoadStartTime = SystemClock.uptimeMillis();
Leon Scroggins58d56c62010-01-28 15:12:40 -0500543 if (mVoiceSearchData != null
544 && !url.equals(mVoiceSearchData.mLastVoiceSearchUrl)) {
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500545 if (mVoiceSearchData.mSourceIsGoogle) {
546 Intent i = new Intent(LoggingEvents.ACTION_LOG_EVENT);
547 i.putExtra(LoggingEvents.EXTRA_FLUSH, true);
Michael Kolb14612442011-06-24 13:06:29 -0700548 mContext.sendBroadcast(i);
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500549 }
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400550 revertVoiceSearchMode();
Leon Scroggins58d56c62010-01-28 15:12:40 -0500551 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700552
Grace Kloba22ac16e2009-10-07 18:00:23 -0700553
554 // If we start a touch icon load and then load a new page, we don't
555 // want to cancel the current touch icon loader. But, we do want to
556 // create a new one when the touch icon url is known.
557 if (mTouchIconLoader != null) {
558 mTouchIconLoader.mTab = null;
559 mTouchIconLoader = null;
560 }
561
562 // reset the error console
563 if (mErrorConsole != null) {
564 mErrorConsole.clearErrorMessages();
Michael Kolb8233fac2010-10-26 16:08:53 -0700565 if (mWebViewController.shouldShowErrorConsole()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700566 mErrorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
567 }
568 }
569
Patrick Scott92066772011-03-10 08:46:27 -0500570 // Cancel the auto-login process.
571 if (mDeviceAccountLogin != null) {
572 mDeviceAccountLogin.cancel();
573 mDeviceAccountLogin = null;
574 mWebViewController.hideAutoLogin(Tab.this);
575 }
576
Grace Kloba22ac16e2009-10-07 18:00:23 -0700577 // finally update the UI in the activity if it is in the foreground
John Reck324d4402011-01-11 16:56:42 -0800578 mWebViewController.onPageStarted(Tab.this, view, favicon);
Leon Scroggins4cd97792010-12-03 15:31:56 -0500579
John Recke969cc52010-12-21 17:24:43 -0800580 updateBookmarkedStatus();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700581 }
582
583 @Override
584 public void onPageFinished(WebView view, String url) {
John Recka1696282011-07-08 14:10:37 -0700585 if (!mInPageLoad) {
586 // In page navigation links (www.something.com#footer) will
587 // trigger an onPageFinished which we don't care about.
588 return;
589 }
John Reck5b691842010-11-29 11:21:13 -0800590 if (!isPrivateBrowsingEnabled()) {
591 LogTag.logPageFinishedLoading(
592 url, SystemClock.uptimeMillis() - mLoadStartTime);
593 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700594 mInPageLoad = false;
John Reck1cf4b792011-07-26 10:22:22 -0700595 syncCurrentState(view, url);
John Reck324d4402011-01-11 16:56:42 -0800596 mWebViewController.onPageFinished(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700597 }
598
599 // return true if want to hijack the url to let another app to handle it
600 @Override
601 public boolean shouldOverrideUrlLoading(WebView view, String url) {
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400602 if (voiceSearchSourceIsGoogle()) {
603 // This method is called when the user clicks on a link.
604 // VoiceSearchMode is turned off when the user leaves the
605 // Google results page, so at this point the user must be on
606 // that page. If the user clicked a link on that page, assume
607 // that the voice search was effective, and broadcast an Intent
608 // so a receiver can take note of that fact.
609 Intent logIntent = new Intent(LoggingEvents.ACTION_LOG_EVENT);
610 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
611 LoggingEvents.VoiceSearch.RESULT_CLICKED);
Michael Kolb14612442011-06-24 13:06:29 -0700612 mContext.sendBroadcast(logIntent);
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400613 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700614 if (mInForeground) {
Michael Kolb18eb3772010-12-10 14:29:51 -0800615 return mWebViewController.shouldOverrideUrlLoading(Tab.this,
616 view, url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700617 } else {
618 return false;
619 }
620 }
621
622 /**
623 * Updates the lock icon. This method is called when we discover another
624 * resource to be loaded for this page (for example, javascript). While
625 * we update the icon type, we do not update the lock icon itself until
626 * we are done loading, it is slightly more secure this way.
627 */
628 @Override
629 public void onLoadResource(WebView view, String url) {
630 if (url != null && url.length() > 0) {
631 // It is only if the page claims to be secure that we may have
632 // to update the lock:
John Reck30c714c2010-12-16 17:30:34 -0800633 if (mCurrentState.mLockIcon == LockIcon.LOCK_ICON_SECURE) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700634 // If NOT a 'safe' url, change the lock to mixed content!
635 if (!(URLUtil.isHttpsUrl(url) || URLUtil.isDataUrl(url)
636 || URLUtil.isAboutUrl(url))) {
John Reck30c714c2010-12-16 17:30:34 -0800637 mCurrentState.mLockIcon = LockIcon.LOCK_ICON_MIXED;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700638 }
639 }
640 }
641 }
642
643 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700644 * Show a dialog informing the user of the network error reported by
645 * WebCore if it is in the foreground.
646 */
647 @Override
648 public void onReceivedError(WebView view, int errorCode,
649 String description, String failingUrl) {
650 if (errorCode != WebViewClient.ERROR_HOST_LOOKUP &&
651 errorCode != WebViewClient.ERROR_CONNECT &&
652 errorCode != WebViewClient.ERROR_BAD_URL &&
653 errorCode != WebViewClient.ERROR_UNSUPPORTED_SCHEME &&
654 errorCode != WebViewClient.ERROR_FILE) {
655 queueError(errorCode, description);
656 }
Jeff Hamilton47654f42010-09-07 09:57:51 -0500657
658 // Don't log URLs when in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -0700659 if (!isPrivateBrowsingEnabled()) {
Jeff Hamilton47654f42010-09-07 09:57:51 -0500660 Log.e(LOGTAG, "onReceivedError " + errorCode + " " + failingUrl
661 + " " + description);
662 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700663 }
664
665 /**
666 * Check with the user if it is ok to resend POST data as the page they
667 * are trying to navigate to is the result of a POST.
668 */
669 @Override
670 public void onFormResubmission(WebView view, final Message dontResend,
671 final Message resend) {
672 if (!mInForeground) {
673 dontResend.sendToTarget();
674 return;
675 }
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500676 if (mDontResend != null) {
677 Log.w(LOGTAG, "onFormResubmission should not be called again "
678 + "while dialog is still up");
679 dontResend.sendToTarget();
680 return;
681 }
682 mDontResend = dontResend;
683 mResend = resend;
Michael Kolb14612442011-06-24 13:06:29 -0700684 new AlertDialog.Builder(mContext).setTitle(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700685 R.string.browserFrameFormResubmitLabel).setMessage(
686 R.string.browserFrameFormResubmitMessage)
687 .setPositiveButton(R.string.ok,
688 new DialogInterface.OnClickListener() {
689 public void onClick(DialogInterface dialog,
690 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500691 if (mResend != null) {
692 mResend.sendToTarget();
693 mResend = null;
694 mDontResend = null;
695 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700696 }
697 }).setNegativeButton(R.string.cancel,
698 new DialogInterface.OnClickListener() {
699 public void onClick(DialogInterface dialog,
700 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500701 if (mDontResend != null) {
702 mDontResend.sendToTarget();
703 mResend = null;
704 mDontResend = null;
705 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700706 }
707 }).setOnCancelListener(new OnCancelListener() {
708 public void onCancel(DialogInterface dialog) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500709 if (mDontResend != null) {
710 mDontResend.sendToTarget();
711 mResend = null;
712 mDontResend = null;
713 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700714 }
715 }).show();
716 }
717
718 /**
719 * Insert the url into the visited history database.
720 * @param url The url to be inserted.
721 * @param isReload True if this url is being reloaded.
722 * FIXME: Not sure what to do when reloading the page.
723 */
724 @Override
725 public void doUpdateVisitedHistory(WebView view, String url,
726 boolean isReload) {
John Reck324d4402011-01-11 16:56:42 -0800727 mWebViewController.doUpdateVisitedHistory(Tab.this, isReload);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700728 }
729
730 /**
731 * Displays SSL error(s) dialog to the user.
732 */
733 @Override
734 public void onReceivedSslError(final WebView view,
735 final SslErrorHandler handler, final SslError error) {
736 if (!mInForeground) {
737 handler.cancel();
John Reck30c714c2010-12-16 17:30:34 -0800738 setLockIconType(LockIcon.LOCK_ICON_UNSECURE);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700739 return;
740 }
John Reck35e9dd62011-04-25 09:01:54 -0700741 if (mSettings.showSecurityWarnings()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700742 final LayoutInflater factory =
Michael Kolb14612442011-06-24 13:06:29 -0700743 LayoutInflater.from(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700744 final View warningsView =
745 factory.inflate(R.layout.ssl_warnings, null);
746 final LinearLayout placeholder =
747 (LinearLayout)warningsView.findViewById(R.id.placeholder);
748
749 if (error.hasError(SslError.SSL_UNTRUSTED)) {
750 LinearLayout ll = (LinearLayout)factory
751 .inflate(R.layout.ssl_warning, null);
752 ((TextView)ll.findViewById(R.id.warning))
753 .setText(R.string.ssl_untrusted);
754 placeholder.addView(ll);
755 }
756
757 if (error.hasError(SslError.SSL_IDMISMATCH)) {
758 LinearLayout ll = (LinearLayout)factory
759 .inflate(R.layout.ssl_warning, null);
760 ((TextView)ll.findViewById(R.id.warning))
761 .setText(R.string.ssl_mismatch);
762 placeholder.addView(ll);
763 }
764
765 if (error.hasError(SslError.SSL_EXPIRED)) {
766 LinearLayout ll = (LinearLayout)factory
767 .inflate(R.layout.ssl_warning, null);
768 ((TextView)ll.findViewById(R.id.warning))
769 .setText(R.string.ssl_expired);
770 placeholder.addView(ll);
771 }
772
773 if (error.hasError(SslError.SSL_NOTYETVALID)) {
774 LinearLayout ll = (LinearLayout)factory
775 .inflate(R.layout.ssl_warning, null);
776 ((TextView)ll.findViewById(R.id.warning))
777 .setText(R.string.ssl_not_yet_valid);
778 placeholder.addView(ll);
779 }
780
Michael Kolb14612442011-06-24 13:06:29 -0700781 new AlertDialog.Builder(mContext).setTitle(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700782 R.string.security_warning).setIcon(
783 android.R.drawable.ic_dialog_alert).setView(
784 warningsView).setPositiveButton(R.string.ssl_continue,
785 new DialogInterface.OnClickListener() {
786 public void onClick(DialogInterface dialog,
787 int whichButton) {
788 handler.proceed();
789 }
790 }).setNeutralButton(R.string.view_certificate,
791 new DialogInterface.OnClickListener() {
792 public void onClick(DialogInterface dialog,
793 int whichButton) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700794 mWebViewController.showSslCertificateOnError(view,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700795 handler, error);
796 }
Ben Murdocha49b8292010-11-16 11:56:04 +0000797 }).setNegativeButton(R.string.ssl_go_back,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700798 new DialogInterface.OnClickListener() {
799 public void onClick(DialogInterface dialog,
800 int whichButton) {
John Reck30c714c2010-12-16 17:30:34 -0800801 dialog.cancel();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700802 }
803 }).setOnCancelListener(
804 new DialogInterface.OnCancelListener() {
805 public void onCancel(DialogInterface dialog) {
806 handler.cancel();
John Reck30c714c2010-12-16 17:30:34 -0800807 setLockIconType(LockIcon.LOCK_ICON_UNSECURE);
808 mWebViewController.onUserCanceledSsl(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700809 }
810 }).show();
811 } else {
812 handler.proceed();
813 }
814 }
815
816 /**
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700817 * Displays client certificate request to the user.
818 */
819 @Override
820 public void onReceivedClientCertRequest(final WebView view,
821 final ClientCertRequestHandler handler, final String host_and_port) {
822 if (!mInForeground) {
823 handler.ignore();
824 return;
825 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700826 int colon = host_and_port.lastIndexOf(':');
827 String host;
828 int port;
829 if (colon == -1) {
830 host = host_and_port;
831 port = -1;
832 } else {
833 String portString = host_and_port.substring(colon + 1);
834 try {
835 port = Integer.parseInt(portString);
836 host = host_and_port.substring(0, colon);
837 } catch (NumberFormatException e) {
838 host = host_and_port;
839 port = -1;
840 }
841 }
Michael Kolb14612442011-06-24 13:06:29 -0700842 KeyChain.choosePrivateKeyAlias(
843 mWebViewController.getActivity(), new KeyChainAliasCallback() {
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700844 @Override public void alias(String alias) {
845 if (alias == null) {
846 handler.cancel();
847 return;
848 }
Michael Kolb14612442011-06-24 13:06:29 -0700849 new KeyChainLookup(mContext, handler, alias).execute();
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700850 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700851 }, null, null, host, port, null);
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700852 }
853
854 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700855 * Handles an HTTP authentication request.
856 *
857 * @param handler The authentication handler
858 * @param host The host
859 * @param realm The realm
860 */
861 @Override
862 public void onReceivedHttpAuthRequest(WebView view,
863 final HttpAuthHandler handler, final String host,
864 final String realm) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700865 mWebViewController.onReceivedHttpAuthRequest(Tab.this, view, handler, host, realm);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700866 }
867
868 @Override
John Reck438bf462011-01-12 18:11:46 -0800869 public WebResourceResponse shouldInterceptRequest(WebView view,
870 String url) {
871 WebResourceResponse res = HomeProvider.shouldInterceptRequest(
Michael Kolb14612442011-06-24 13:06:29 -0700872 mContext, url);
John Reck438bf462011-01-12 18:11:46 -0800873 return res;
874 }
875
876 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700877 public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
878 if (!mInForeground) {
879 return false;
880 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700881 return mWebViewController.shouldOverrideKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700882 }
883
884 @Override
885 public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700886 if (!mInForeground) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700887 return;
888 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700889 mWebViewController.onUnhandledKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700890 }
Patrick Scott92066772011-03-10 08:46:27 -0500891
892 @Override
893 public void onReceivedLoginRequest(WebView view, String realm,
894 String account, String args) {
Michael Kolb14612442011-06-24 13:06:29 -0700895 new DeviceAccountLogin(mWebViewController.getActivity(), view, Tab.this, mWebViewController)
Patrick Scott92066772011-03-10 08:46:27 -0500896 .handleLogin(realm, account, args);
897 }
898
Grace Kloba22ac16e2009-10-07 18:00:23 -0700899 };
900
John Reck1cf4b792011-07-26 10:22:22 -0700901 private void syncCurrentState(WebView view, String url) {
902 // Sync state (in case of stop/timeout)
903 mCurrentState.mUrl = view.getUrl();
904 if (mCurrentState.mUrl == null) {
905 mCurrentState.mUrl = url != null ? url : "";
906 }
907 mCurrentState.mOriginalUrl = view.getOriginalUrl();
908 mCurrentState.mTitle = view.getTitle();
909 mCurrentState.mFavicon = view.getFavicon();
910 if (!URLUtil.isHttpsUrl(mCurrentState.mUrl)) {
911 // In case we stop when loading an HTTPS page from an HTTP page
912 // but before a provisional load occurred
913 mCurrentState.mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
914 }
915 }
916
Patrick Scott92066772011-03-10 08:46:27 -0500917 // Called by DeviceAccountLogin when the Tab needs to have the auto-login UI
918 // displayed.
919 void setDeviceAccountLogin(DeviceAccountLogin login) {
920 mDeviceAccountLogin = login;
921 }
922
923 // Returns non-null if the title bar should display the auto-login UI.
924 DeviceAccountLogin getDeviceAccountLogin() {
925 return mDeviceAccountLogin;
926 }
927
Grace Kloba22ac16e2009-10-07 18:00:23 -0700928 // -------------------------------------------------------------------------
929 // WebChromeClient implementation for the main WebView
930 // -------------------------------------------------------------------------
931
932 private final WebChromeClient mWebChromeClient = new WebChromeClient() {
933 // Helper method to create a new tab or sub window.
934 private void createWindow(final boolean dialog, final Message msg) {
935 WebView.WebViewTransport transport =
936 (WebView.WebViewTransport) msg.obj;
937 if (dialog) {
938 createSubWindow();
Michael Kolb8233fac2010-10-26 16:08:53 -0700939 mWebViewController.attachSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700940 transport.setWebView(mSubView);
941 } else {
Michael Kolb7bcafde2011-05-09 13:55:59 -0700942 final Tab newTab = mWebViewController.openTab(null,
John Reck5949c662011-05-27 09:52:29 -0700943 Tab.this, true, true);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700944 transport.setWebView(newTab.getWebView());
945 }
946 msg.sendToTarget();
947 }
948
949 @Override
950 public boolean onCreateWindow(WebView view, final boolean dialog,
951 final boolean userGesture, final Message resultMsg) {
952 // only allow new window or sub window for the foreground case
953 if (!mInForeground) {
954 return false;
955 }
956 // Short-circuit if we can't create any more tabs or sub windows.
957 if (dialog && mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -0700958 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700959 .setTitle(R.string.too_many_subwindows_dialog_title)
960 .setIcon(android.R.drawable.ic_dialog_alert)
961 .setMessage(R.string.too_many_subwindows_dialog_message)
962 .setPositiveButton(R.string.ok, null)
963 .show();
964 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700965 } else if (!mWebViewController.getTabControl().canCreateNewTab()) {
Michael Kolb14612442011-06-24 13:06:29 -0700966 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700967 .setTitle(R.string.too_many_windows_dialog_title)
968 .setIcon(android.R.drawable.ic_dialog_alert)
969 .setMessage(R.string.too_many_windows_dialog_message)
970 .setPositiveButton(R.string.ok, null)
971 .show();
972 return false;
973 }
974
975 // Short-circuit if this was a user gesture.
976 if (userGesture) {
977 createWindow(dialog, resultMsg);
978 return true;
979 }
980
981 // Allow the popup and create the appropriate window.
982 final AlertDialog.OnClickListener allowListener =
983 new AlertDialog.OnClickListener() {
984 public void onClick(DialogInterface d,
985 int which) {
986 createWindow(dialog, resultMsg);
987 }
988 };
989
990 // Block the popup by returning a null WebView.
991 final AlertDialog.OnClickListener blockListener =
992 new AlertDialog.OnClickListener() {
993 public void onClick(DialogInterface d, int which) {
994 resultMsg.sendToTarget();
995 }
996 };
997
998 // Build a confirmation dialog to display to the user.
999 final AlertDialog d =
Michael Kolb14612442011-06-24 13:06:29 -07001000 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -07001001 .setTitle(R.string.attention)
1002 .setIcon(android.R.drawable.ic_dialog_alert)
1003 .setMessage(R.string.popup_window_attempt)
1004 .setPositiveButton(R.string.allow, allowListener)
1005 .setNegativeButton(R.string.block, blockListener)
1006 .setCancelable(false)
1007 .create();
1008
1009 // Show the confirmation dialog.
1010 d.show();
1011 return true;
1012 }
1013
1014 @Override
Patrick Scotteb5061b2009-11-18 15:00:30 -05001015 public void onRequestFocus(WebView view) {
1016 if (!mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -07001017 mWebViewController.switchToTab(Tab.this);
Patrick Scotteb5061b2009-11-18 15:00:30 -05001018 }
1019 }
1020
1021 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -07001022 public void onCloseWindow(WebView window) {
Michael Kolbc831b632011-05-11 09:30:34 -07001023 if (mParent != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001024 // JavaScript can only close popup window.
1025 if (mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -07001026 mWebViewController.switchToTab(mParent);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001027 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001028 mWebViewController.closeTab(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001029 }
1030 }
1031
1032 @Override
1033 public void onProgressChanged(WebView view, int newProgress) {
John Reck30c714c2010-12-16 17:30:34 -08001034 mPageLoadProgress = newProgress;
1035 mWebViewController.onProgressChanged(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001036 }
1037
1038 @Override
Leon Scroggins21d9b902010-03-11 09:33:11 -05001039 public void onReceivedTitle(WebView view, final String title) {
John Reck30c714c2010-12-16 17:30:34 -08001040 mCurrentState.mTitle = title;
Michael Kolb8233fac2010-10-26 16:08:53 -07001041 mWebViewController.onReceivedTitle(Tab.this, title);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001042 }
1043
1044 @Override
1045 public void onReceivedIcon(WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -08001046 mCurrentState.mFavicon = icon;
Michael Kolb8233fac2010-10-26 16:08:53 -07001047 mWebViewController.onFavicon(Tab.this, view, icon);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001048 }
1049
1050 @Override
1051 public void onReceivedTouchIconUrl(WebView view, String url,
1052 boolean precomposed) {
Michael Kolb14612442011-06-24 13:06:29 -07001053 final ContentResolver cr = mContext.getContentResolver();
Leon Scrogginsc8393d92010-04-23 14:58:16 -04001054 // Let precomposed icons take precedence over non-composed
1055 // icons.
1056 if (precomposed && mTouchIconLoader != null) {
1057 mTouchIconLoader.cancel(false);
1058 mTouchIconLoader = null;
1059 }
1060 // Have only one async task at a time.
1061 if (mTouchIconLoader == null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001062 mTouchIconLoader = new DownloadTouchIcon(Tab.this,
Michael Kolb14612442011-06-24 13:06:29 -07001063 mContext, cr, view);
Leon Scrogginsc8393d92010-04-23 14:58:16 -04001064 mTouchIconLoader.execute(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001065 }
1066 }
1067
1068 @Override
1069 public void onShowCustomView(View view,
1070 WebChromeClient.CustomViewCallback callback) {
Michael Kolb14612442011-06-24 13:06:29 -07001071 Activity activity = mWebViewController.getActivity();
1072 if (activity != null) {
1073 onShowCustomView(view, activity.getRequestedOrientation(), callback);
1074 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001075 }
1076
1077 @Override
1078 public void onShowCustomView(View view, int requestedOrientation,
1079 WebChromeClient.CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001080 if (mInForeground) mWebViewController.showCustomView(Tab.this, view,
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001081 requestedOrientation, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001082 }
1083
1084 @Override
1085 public void onHideCustomView() {
Michael Kolb8233fac2010-10-26 16:08:53 -07001086 if (mInForeground) mWebViewController.hideCustomView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001087 }
1088
1089 /**
1090 * The origin has exceeded its database quota.
1091 * @param url the URL that exceeded the quota
1092 * @param databaseIdentifier the identifier of the database on which the
1093 * transaction that caused the quota overflow was run
1094 * @param currentQuota the current quota for the origin.
1095 * @param estimatedSize the estimated size of the database.
1096 * @param totalUsedQuota is the sum of all origins' quota.
1097 * @param quotaUpdater The callback to run when a decision to allow or
1098 * deny quota has been made. Don't forget to call this!
1099 */
1100 @Override
1101 public void onExceededDatabaseQuota(String url,
1102 String databaseIdentifier, long currentQuota, long estimatedSize,
1103 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -07001104 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001105 .onExceededDatabaseQuota(url, databaseIdentifier,
1106 currentQuota, estimatedSize, totalUsedQuota,
1107 quotaUpdater);
1108 }
1109
1110 /**
1111 * The Application Cache has exceeded its max size.
1112 * @param spaceNeeded is the amount of disk space that would be needed
1113 * in order for the last appcache operation to succeed.
1114 * @param totalUsedQuota is the sum of all origins' quota.
1115 * @param quotaUpdater A callback to inform the WebCore thread that a
1116 * new app cache size is available. This callback must always
1117 * be executed at some point to ensure that the sleeping
1118 * WebCore thread is woken up.
1119 */
1120 @Override
1121 public void onReachedMaxAppCacheSize(long spaceNeeded,
1122 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -07001123 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001124 .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota,
1125 quotaUpdater);
1126 }
1127
1128 /**
1129 * Instructs the browser to show a prompt to ask the user to set the
1130 * Geolocation permission state for the specified origin.
1131 * @param origin The origin for which Geolocation permissions are
1132 * requested.
1133 * @param callback The callback to call once the user has set the
1134 * Geolocation permission state.
1135 */
1136 @Override
1137 public void onGeolocationPermissionsShowPrompt(String origin,
1138 GeolocationPermissions.Callback callback) {
1139 if (mInForeground) {
Grace Kloba50c241e2010-04-20 11:07:50 -07001140 getGeolocationPermissionsPrompt().show(origin, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001141 }
1142 }
1143
1144 /**
1145 * Instructs the browser to hide the Geolocation permissions prompt.
1146 */
1147 @Override
1148 public void onGeolocationPermissionsHidePrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001149 if (mInForeground && mGeolocationPermissionsPrompt != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001150 mGeolocationPermissionsPrompt.hide();
1151 }
1152 }
1153
Ben Murdoch65acc352009-11-19 18:16:04 +00001154 /* Adds a JavaScript error message to the system log and if the JS
1155 * console is enabled in the about:debug options, to that console
1156 * also.
Ben Murdochc42addf2010-01-28 15:19:59 +00001157 * @param consoleMessage the message object.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001158 */
1159 @Override
Ben Murdochc42addf2010-01-28 15:19:59 +00001160 public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001161 if (mInForeground) {
1162 // call getErrorConsole(true) so it will create one if needed
1163 ErrorConsoleView errorConsole = getErrorConsole(true);
Ben Murdochc42addf2010-01-28 15:19:59 +00001164 errorConsole.addErrorMessage(consoleMessage);
Michael Kolb8233fac2010-10-26 16:08:53 -07001165 if (mWebViewController.shouldShowErrorConsole()
1166 && errorConsole.getShowState() !=
1167 ErrorConsoleView.SHOW_MAXIMIZED) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001168 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
1169 }
1170 }
Ben Murdochc42addf2010-01-28 15:19:59 +00001171
Jeff Hamilton47654f42010-09-07 09:57:51 -05001172 // Don't log console messages in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001173 if (isPrivateBrowsingEnabled()) return true;
Jeff Hamilton47654f42010-09-07 09:57:51 -05001174
Ben Murdochc42addf2010-01-28 15:19:59 +00001175 String message = "Console: " + consoleMessage.message() + " "
1176 + consoleMessage.sourceId() + ":"
1177 + consoleMessage.lineNumber();
1178
1179 switch (consoleMessage.messageLevel()) {
1180 case TIP:
1181 Log.v(CONSOLE_LOGTAG, message);
1182 break;
1183 case LOG:
1184 Log.i(CONSOLE_LOGTAG, message);
1185 break;
1186 case WARNING:
1187 Log.w(CONSOLE_LOGTAG, message);
1188 break;
1189 case ERROR:
1190 Log.e(CONSOLE_LOGTAG, message);
1191 break;
1192 case DEBUG:
1193 Log.d(CONSOLE_LOGTAG, message);
1194 break;
1195 }
1196
1197 return true;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001198 }
1199
1200 /**
1201 * Ask the browser for an icon to represent a <video> element.
1202 * This icon will be used if the Web page did not specify a poster attribute.
1203 * @return Bitmap The icon or null if no such icon is available.
1204 */
1205 @Override
1206 public Bitmap getDefaultVideoPoster() {
1207 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001208 return mWebViewController.getDefaultVideoPoster();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001209 }
1210 return null;
1211 }
1212
1213 /**
1214 * Ask the host application for a custom progress view to show while
1215 * a <video> is loading.
1216 * @return View The progress view.
1217 */
1218 @Override
1219 public View getVideoLoadingProgressView() {
1220 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001221 return mWebViewController.getVideoLoadingProgressView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001222 }
1223 return null;
1224 }
1225
1226 @Override
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01001227 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001228 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001229 mWebViewController.openFileChooser(uploadMsg, acceptType);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001230 } else {
1231 uploadMsg.onReceiveValue(null);
1232 }
1233 }
1234
1235 /**
1236 * Deliver a list of already-visited URLs
1237 */
1238 @Override
1239 public void getVisitedHistory(final ValueCallback<String[]> callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001240 mWebViewController.getVisitedHistory(callback);
1241 }
Ben Murdoch8029a772010-11-16 11:58:21 +00001242
1243 @Override
1244 public void setupAutoFill(Message message) {
1245 // Prompt the user to set up their profile.
1246 final Message msg = message;
Michael Kolb14612442011-06-24 13:06:29 -07001247 AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
1248 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
Ben Murdoch1d676b62011-01-17 12:54:24 +00001249 Context.LAYOUT_INFLATER_SERVICE);
1250 final View layout = inflater.inflate(R.layout.setup_autofill_dialog, null);
1251
1252 builder.setView(layout)
Ben Murdochb7e6f942011-07-08 13:00:21 +01001253 .setTitle(R.string.autofill_setup_dialog_title)
Ben Murdoch1d676b62011-01-17 12:54:24 +00001254 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1255 @Override
1256 public void onClick(DialogInterface dialog, int id) {
1257 CheckBox disableAutoFill = (CheckBox) layout.findViewById(
1258 R.id.setup_autofill_dialog_disable_autofill);
1259
1260 if (disableAutoFill.isChecked()) {
1261 // Disable autofill and show a toast with how to turn it on again.
John Reck35e9dd62011-04-25 09:01:54 -07001262 mSettings.setAutofillEnabled(false);
Michael Kolb14612442011-06-24 13:06:29 -07001263 Toast.makeText(mContext,
Ben Murdoch1d676b62011-01-17 12:54:24 +00001264 R.string.autofill_setup_dialog_negative_toast,
1265 Toast.LENGTH_LONG).show();
1266 } else {
1267 // Take user to the AutoFill profile editor. When they return,
1268 // we will send the message that we pass here which will trigger
1269 // the form to get filled out with their new profile.
1270 mWebViewController.setupAutoFill(msg);
1271 }
1272 }
1273 })
1274 .setNegativeButton(R.string.cancel, null)
1275 .show();
Ben Murdoch8029a772010-11-16 11:58:21 +00001276 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001277 };
1278
1279 // -------------------------------------------------------------------------
1280 // WebViewClient implementation for the sub window
1281 // -------------------------------------------------------------------------
1282
1283 // Subclass of WebViewClient used in subwindows to notify the main
1284 // WebViewClient of certain WebView activities.
1285 private static class SubWindowClient extends WebViewClient {
1286 // The main WebViewClient.
1287 private final WebViewClient mClient;
Michael Kolb8233fac2010-10-26 16:08:53 -07001288 private final WebViewController mController;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001289
Michael Kolb8233fac2010-10-26 16:08:53 -07001290 SubWindowClient(WebViewClient client, WebViewController controller) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001291 mClient = client;
Michael Kolb8233fac2010-10-26 16:08:53 -07001292 mController = controller;
Leon Scroggins III211ba542010-04-19 13:21:13 -04001293 }
1294 @Override
1295 public void onPageStarted(WebView view, String url, Bitmap favicon) {
1296 // Unlike the others, do not call mClient's version, which would
1297 // change the progress bar. However, we do want to remove the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001298 // find or select dialog.
Michael Kolb8233fac2010-10-26 16:08:53 -07001299 mController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001300 }
1301 @Override
1302 public void doUpdateVisitedHistory(WebView view, String url,
1303 boolean isReload) {
1304 mClient.doUpdateVisitedHistory(view, url, isReload);
1305 }
1306 @Override
1307 public boolean shouldOverrideUrlLoading(WebView view, String url) {
1308 return mClient.shouldOverrideUrlLoading(view, url);
1309 }
1310 @Override
1311 public void onReceivedSslError(WebView view, SslErrorHandler handler,
1312 SslError error) {
1313 mClient.onReceivedSslError(view, handler, error);
1314 }
1315 @Override
Brian Carlstrom8862c1d2011-06-02 01:05:55 -07001316 public void onReceivedClientCertRequest(WebView view,
1317 ClientCertRequestHandler handler, String host_and_port) {
1318 mClient.onReceivedClientCertRequest(view, handler, host_and_port);
1319 }
1320 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -07001321 public void onReceivedHttpAuthRequest(WebView view,
1322 HttpAuthHandler handler, String host, String realm) {
1323 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
1324 }
1325 @Override
1326 public void onFormResubmission(WebView view, Message dontResend,
1327 Message resend) {
1328 mClient.onFormResubmission(view, dontResend, resend);
1329 }
1330 @Override
1331 public void onReceivedError(WebView view, int errorCode,
1332 String description, String failingUrl) {
1333 mClient.onReceivedError(view, errorCode, description, failingUrl);
1334 }
1335 @Override
1336 public boolean shouldOverrideKeyEvent(WebView view,
1337 android.view.KeyEvent event) {
1338 return mClient.shouldOverrideKeyEvent(view, event);
1339 }
1340 @Override
1341 public void onUnhandledKeyEvent(WebView view,
1342 android.view.KeyEvent event) {
1343 mClient.onUnhandledKeyEvent(view, event);
1344 }
1345 }
1346
1347 // -------------------------------------------------------------------------
1348 // WebChromeClient implementation for the sub window
1349 // -------------------------------------------------------------------------
1350
1351 private class SubWindowChromeClient extends WebChromeClient {
1352 // The main WebChromeClient.
1353 private final WebChromeClient mClient;
1354
1355 SubWindowChromeClient(WebChromeClient client) {
1356 mClient = client;
1357 }
1358 @Override
1359 public void onProgressChanged(WebView view, int newProgress) {
1360 mClient.onProgressChanged(view, newProgress);
1361 }
1362 @Override
1363 public boolean onCreateWindow(WebView view, boolean dialog,
1364 boolean userGesture, android.os.Message resultMsg) {
1365 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
1366 }
1367 @Override
1368 public void onCloseWindow(WebView window) {
1369 if (window != mSubView) {
1370 Log.e(LOGTAG, "Can't close the window");
1371 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001372 mWebViewController.dismissSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001373 }
1374 }
1375
1376 // -------------------------------------------------------------------------
1377
1378 // Construct a new tab
Michael Kolb7bcafde2011-05-09 13:55:59 -07001379 Tab(WebViewController wvcontroller, WebView w) {
John Reck1cf4b792011-07-26 10:22:22 -07001380 this(wvcontroller, w, null);
1381 }
1382
1383 Tab(WebViewController wvcontroller, Bundle state) {
1384 this(wvcontroller, null, state);
1385 }
1386
1387 Tab(WebViewController wvcontroller, WebView w, Bundle state) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001388 mWebViewController = wvcontroller;
Michael Kolb14612442011-06-24 13:06:29 -07001389 mContext = mWebViewController.getContext();
John Reck35e9dd62011-04-25 09:01:54 -07001390 mSettings = BrowserSettings.getInstance();
Michael Kolb14612442011-06-24 13:06:29 -07001391 mDataController = DataController.getInstance(mContext);
1392 mCurrentState = new PageState(mContext, w != null
John Reck847b5322011-04-14 17:02:18 -07001393 ? w.isPrivateBrowsingEnabled() : false);
Michael Kolb8233fac2010-10-26 16:08:53 -07001394 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001395 mInForeground = false;
1396
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001397 mDownloadListener = new DownloadListener() {
1398 public void onDownloadStart(String url, String userAgent,
1399 String contentDisposition, String mimetype,
1400 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001401 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001402 mimetype, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001403 }
1404 };
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001405 mWebBackForwardListClient = new WebBackForwardListClient() {
1406 @Override
1407 public void onNewHistoryItem(WebHistoryItem item) {
1408 if (isInVoiceSearchMode()) {
1409 item.setCustomData(mVoiceSearchData.mVoiceSearchIntent);
1410 }
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001411 if (mClearHistoryMatchUrl != null) {
1412 if (LOGD_ENABLED) {
1413 Log.d(LOGTAG, "onNewHistoryItem:\n\t" + item.getUrl() + "\n\t"
1414 + mClearHistoryMatchUrl);
1415 }
1416 if (TextUtils.equals(item.getOriginalUrl(), mClearHistoryMatchUrl)) {
1417 if (mMainView != null) {
1418 mMainView.clearHistory();
1419 }
1420 }
1421 mClearHistoryMatchUrl = null;
1422 }
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001423 }
1424 @Override
1425 public void onIndexChanged(WebHistoryItem item, int index) {
1426 Object data = item.getCustomData();
1427 if (data != null && data instanceof Intent) {
1428 activateVoiceSearchMode((Intent) data);
1429 }
1430 }
1431 };
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001432
John Reck1cf4b792011-07-26 10:22:22 -07001433 mCaptureWidth = mContext.getResources().getDimensionPixelSize(
1434 R.dimen.tab_thumbnail_width);
1435 mCaptureHeight = mContext.getResources().getDimensionPixelSize(
1436 R.dimen.tab_thumbnail_height);
1437 updateShouldCaptureThumbnails();
1438 restoreState(state);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001439 setWebView(w);
Michael Kolb9ef259a2011-07-12 15:33:08 -07001440 mHandler = new Handler() {
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001441 @Override
Michael Kolb9ef259a2011-07-12 15:33:08 -07001442 public void handleMessage(Message m) {
John Reck1cf4b792011-07-26 10:22:22 -07001443 switch (m.what) {
1444 case MSG_CAPTURE:
1445 capture();
1446 break;
1447 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001448 }
1449 };
John Reck1cf4b792011-07-26 10:22:22 -07001450 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001451
John Reck1cf4b792011-07-26 10:22:22 -07001452 public void updateShouldCaptureThumbnails() {
1453 if (mWebViewController.shouldCaptureThumbnails()) {
1454 synchronized (Tab.this) {
1455 if (mCapture == null) {
1456 mCapture = Bitmap.createBitmap(mCaptureWidth, mCaptureHeight,
1457 Bitmap.Config.RGB_565);
1458 if (mInForeground) {
1459 postCapture();
1460 }
1461 }
1462 }
1463 } else {
1464 synchronized (Tab.this) {
1465 mCapture = null;
1466 deleteThumbnail();
1467 }
1468 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001469 }
1470
Michael Kolb14612442011-06-24 13:06:29 -07001471 public void setController(WebViewController ctl) {
1472 mWebViewController = ctl;
John Reck1cf4b792011-07-26 10:22:22 -07001473 updateShouldCaptureThumbnails();
Michael Kolb14612442011-06-24 13:06:29 -07001474 }
1475
Michael Kolbc831b632011-05-11 09:30:34 -07001476 public void setId(long id) {
1477 mId = id;
1478 }
1479
1480 public long getId() {
1481 return mId;
1482 }
1483
Grace Kloba22ac16e2009-10-07 18:00:23 -07001484 /**
1485 * Sets the WebView for this tab, correctly removing the old WebView from
1486 * the container view.
1487 */
1488 void setWebView(WebView w) {
1489 if (mMainView == w) {
1490 return;
1491 }
Michael Kolba713ec82010-11-29 17:27:06 -08001492
Grace Kloba22ac16e2009-10-07 18:00:23 -07001493 // If the WebView is changing, the page will be reloaded, so any ongoing
1494 // Geolocation permission requests are void.
Grace Kloba50c241e2010-04-20 11:07:50 -07001495 if (mGeolocationPermissionsPrompt != null) {
1496 mGeolocationPermissionsPrompt.hide();
1497 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001498
Michael Kolba713ec82010-11-29 17:27:06 -08001499 mWebViewController.onSetWebView(this, w);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001500
John Reck1cf4b792011-07-26 10:22:22 -07001501 if (mMainView != null) {
John Reck8ee633f2011-08-09 16:00:35 -07001502 mMainView.setPictureListener(null);
John Reck1cf4b792011-07-26 10:22:22 -07001503 if (w != null) {
1504 syncCurrentState(w, null);
1505 } else {
1506 mCurrentState = new PageState(mContext, false);
1507 }
1508 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001509 // set the new one
1510 mMainView = w;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001511 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001512 if (mMainView != null) {
1513 mMainView.setWebViewClient(mWebViewClient);
1514 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001515 // Attach DownloadManager so that downloads can start in an active
1516 // or a non-active window. This can happen when going to a site that
1517 // does a redirect after a period of time. The user could have
1518 // switched to another tab while waiting for the download to start.
1519 mMainView.setDownloadListener(mDownloadListener);
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001520 mMainView.setWebBackForwardListClient(mWebBackForwardListClient);
John Reck8ee633f2011-08-09 16:00:35 -07001521 TabControl tc = mWebViewController.getTabControl();
1522 if (tc != null && tc.getOnThumbnailUpdatedListener() != null) {
1523 mMainView.setPictureListener(this);
1524 }
John Reck1cf4b792011-07-26 10:22:22 -07001525 if (mSavedState != null) {
1526 mMainView.restoreState(mSavedState);
1527 mSavedState = null;
1528 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001529 }
1530 }
1531
1532 /**
1533 * Destroy the tab's main WebView and subWindow if any
1534 */
1535 void destroy() {
1536 if (mMainView != null) {
1537 dismissSubWindow();
John Reckef654f12011-07-12 16:42:08 -07001538 // Make sure the embedded title bar isn't still attached
1539 mMainView.setEmbeddedTitleBar(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001540 // save the WebView to call destroy() after detach it from the tab
1541 WebView webView = mMainView;
1542 setWebView(null);
1543 webView.destroy();
1544 }
1545 }
1546
1547 /**
1548 * Remove the tab from the parent
1549 */
1550 void removeFromTree() {
1551 // detach the children
Michael Kolbc831b632011-05-11 09:30:34 -07001552 if (mChildren != null) {
1553 for(Tab t : mChildren) {
1554 t.setParent(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001555 }
1556 }
1557 // remove itself from the parent list
Michael Kolbc831b632011-05-11 09:30:34 -07001558 if (mParent != null) {
1559 mParent.mChildren.remove(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001560 }
John Reck1cf4b792011-07-26 10:22:22 -07001561 deleteThumbnail();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001562 }
1563
1564 /**
1565 * Create a new subwindow unless a subwindow already exists.
1566 * @return True if a new subwindow was created. False if one already exists.
1567 */
1568 boolean createSubWindow() {
1569 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001570 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001571 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001572 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001573 mSubView.setWebChromeClient(new SubWindowChromeClient(
1574 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001575 // Set a different DownloadListener for the mSubView, since it will
1576 // just need to dismiss the mSubView, rather than close the Tab
1577 mSubView.setDownloadListener(new DownloadListener() {
1578 public void onDownloadStart(String url, String userAgent,
1579 String contentDisposition, String mimetype,
1580 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001581 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001582 contentDisposition, mimetype, contentLength);
1583 if (mSubView.copyBackForwardList().getSize() == 0) {
1584 // This subwindow was opened for the sole purpose of
1585 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001586 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001587 }
1588 }
1589 });
Michael Kolb14612442011-06-24 13:06:29 -07001590 mSubView.setOnCreateContextMenuListener(mWebViewController.getActivity());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001591 return true;
1592 }
1593 return false;
1594 }
1595
1596 /**
1597 * Dismiss the subWindow for the tab.
1598 */
1599 void dismissSubWindow() {
1600 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001601 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001602 mSubView.destroy();
1603 mSubView = null;
1604 mSubViewContainer = null;
1605 }
1606 }
1607
Grace Kloba22ac16e2009-10-07 18:00:23 -07001608
1609 /**
1610 * Set the parent tab of this tab.
1611 */
Michael Kolbc831b632011-05-11 09:30:34 -07001612 void setParent(Tab parent) {
1613 mParent = parent;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001614 // This tab may have been freed due to low memory. If that is the case,
Michael Kolbc831b632011-05-11 09:30:34 -07001615 // the parent tab id is already saved. If we are changing that id
Grace Kloba22ac16e2009-10-07 18:00:23 -07001616 // (most likely due to removing the parent tab) we must update the
Michael Kolbc831b632011-05-11 09:30:34 -07001617 // parent tab id in the saved Bundle.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001618 if (mSavedState != null) {
1619 if (parent == null) {
1620 mSavedState.remove(PARENTTAB);
1621 } else {
Michael Kolbc831b632011-05-11 09:30:34 -07001622 mSavedState.putLong(PARENTTAB, parent.getId());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001623 }
1624 }
John Reckb0a86db2011-05-24 14:05:58 -07001625
1626 // Sync the WebView useragent with the parent
1627 if (parent != null && mSettings.hasDesktopUseragent(parent.getWebView())
1628 != mSettings.hasDesktopUseragent(getWebView())) {
1629 mSettings.toggleDesktopUseragent(getWebView());
1630 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001631 }
1632
1633 /**
Michael Kolbc831b632011-05-11 09:30:34 -07001634 * 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 getParent() {
1639 return mParent;
1640 }
1641
1642 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001643 * When a Tab is created through the content of another Tab, then we
1644 * associate the Tabs.
1645 * @param child the Tab that was created from this Tab
1646 */
1647 void addChildTab(Tab child) {
Michael Kolbc831b632011-05-11 09:30:34 -07001648 if (mChildren == null) {
1649 mChildren = new Vector<Tab>();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001650 }
Michael Kolbc831b632011-05-11 09:30:34 -07001651 mChildren.add(child);
1652 child.setParent(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001653 }
1654
Michael Kolbc831b632011-05-11 09:30:34 -07001655 Vector<Tab> getChildren() {
1656 return mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001657 }
1658
1659 void resume() {
1660 if (mMainView != null) {
1661 mMainView.onResume();
1662 if (mSubView != null) {
1663 mSubView.onResume();
1664 }
1665 }
1666 }
1667
1668 void pause() {
1669 if (mMainView != null) {
1670 mMainView.onPause();
1671 if (mSubView != null) {
1672 mSubView.onPause();
1673 }
1674 }
1675 }
1676
1677 void putInForeground() {
John Reck8ee633f2011-08-09 16:00:35 -07001678 if (mInForeground) {
1679 return;
1680 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001681 mInForeground = true;
1682 resume();
Michael Kolb14612442011-06-24 13:06:29 -07001683 Activity activity = mWebViewController.getActivity();
1684 mMainView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001685 if (mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -07001686 mSubView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001687 }
1688 // Show the pending error dialog if the queue is not empty
1689 if (mQueuedErrors != null && mQueuedErrors.size() > 0) {
1690 showError(mQueuedErrors.getFirst());
1691 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001692 mWebViewController.bookmarkedStatusHasChanged(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001693 }
1694
1695 void putInBackground() {
John Reck8ee633f2011-08-09 16:00:35 -07001696 if (!mInForeground) {
1697 return;
1698 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001699 mInForeground = false;
1700 pause();
1701 mMainView.setOnCreateContextMenuListener(null);
1702 if (mSubView != null) {
1703 mSubView.setOnCreateContextMenuListener(null);
1704 }
1705 }
1706
Michael Kolb8233fac2010-10-26 16:08:53 -07001707 boolean inForeground() {
1708 return mInForeground;
1709 }
1710
Grace Kloba22ac16e2009-10-07 18:00:23 -07001711 /**
1712 * Return the top window of this tab; either the subwindow if it is not
1713 * null or the main window.
1714 * @return The top window of this tab.
1715 */
1716 WebView getTopWindow() {
1717 if (mSubView != null) {
1718 return mSubView;
1719 }
1720 return mMainView;
1721 }
1722
1723 /**
1724 * Return the main window of this tab. Note: if a tab is freed in the
1725 * background, this can return null. It is only guaranteed to be
1726 * non-null for the current tab.
1727 * @return The main WebView of this tab.
1728 */
1729 WebView getWebView() {
1730 return mMainView;
1731 }
1732
Michael Kolba713ec82010-11-29 17:27:06 -08001733 void setViewContainer(View container) {
1734 mContainer = container;
1735 }
1736
Michael Kolb8233fac2010-10-26 16:08:53 -07001737 View getViewContainer() {
1738 return mContainer;
1739 }
1740
Grace Kloba22ac16e2009-10-07 18:00:23 -07001741 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001742 * Return whether private browsing is enabled for the main window of
1743 * this tab.
1744 * @return True if private browsing is enabled.
1745 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001746 boolean isPrivateBrowsingEnabled() {
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001747 WebView webView = getWebView();
1748 if (webView == null) {
1749 return false;
1750 }
1751 return webView.isPrivateBrowsingEnabled();
1752 }
1753
1754 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001755 * Return the subwindow of this tab or null if there is no subwindow.
1756 * @return The subwindow of this tab or null.
1757 */
1758 WebView getSubWebView() {
1759 return mSubView;
1760 }
1761
Michael Kolb1514bb72010-11-22 09:11:48 -08001762 void setSubWebView(WebView subView) {
1763 mSubView = subView;
1764 }
1765
Michael Kolb8233fac2010-10-26 16:08:53 -07001766 View getSubViewContainer() {
1767 return mSubViewContainer;
1768 }
1769
Michael Kolb1514bb72010-11-22 09:11:48 -08001770 void setSubViewContainer(View subViewContainer) {
1771 mSubViewContainer = subViewContainer;
1772 }
1773
Grace Kloba22ac16e2009-10-07 18:00:23 -07001774 /**
1775 * @return The geolocation permissions prompt for this tab.
1776 */
1777 GeolocationPermissionsPrompt getGeolocationPermissionsPrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001778 if (mGeolocationPermissionsPrompt == null) {
1779 ViewStub stub = (ViewStub) mContainer
1780 .findViewById(R.id.geolocation_permissions_prompt);
1781 mGeolocationPermissionsPrompt = (GeolocationPermissionsPrompt) stub
1782 .inflate();
Grace Kloba50c241e2010-04-20 11:07:50 -07001783 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001784 return mGeolocationPermissionsPrompt;
1785 }
1786
1787 /**
1788 * @return The application id string
1789 */
1790 String getAppId() {
1791 return mAppId;
1792 }
1793
1794 /**
1795 * Set the application id string
1796 * @param id
1797 */
1798 void setAppId(String id) {
1799 mAppId = id;
1800 }
1801
Michael Kolbe28b3472011-08-04 16:54:31 -07001802 boolean closeOnBack() {
1803 return mCloseOnBack;
1804 }
1805
1806 void setCloseOnBack(boolean close) {
1807 mCloseOnBack = close;
1808 }
1809
Grace Kloba22ac16e2009-10-07 18:00:23 -07001810 String getUrl() {
John Reck324d4402011-01-11 16:56:42 -08001811 return UrlUtils.filteredUrl(mCurrentState.mUrl);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001812 }
1813
John Reck49a603c2011-03-03 09:33:05 -08001814 String getOriginalUrl() {
John Reckdb22ec42011-06-29 11:31:24 -07001815 if (mCurrentState.mOriginalUrl == null) {
1816 return getUrl();
John Reck49a603c2011-03-03 09:33:05 -08001817 }
John Reckdb22ec42011-06-29 11:31:24 -07001818 return UrlUtils.filteredUrl(mCurrentState.mOriginalUrl);
John Reck49a603c2011-03-03 09:33:05 -08001819 }
1820
Grace Kloba22ac16e2009-10-07 18:00:23 -07001821 /**
John Reck30c714c2010-12-16 17:30:34 -08001822 * Get the title of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001823 */
1824 String getTitle() {
John Reck30c714c2010-12-16 17:30:34 -08001825 if (mCurrentState.mTitle == null && mInPageLoad) {
Michael Kolb14612442011-06-24 13:06:29 -07001826 return mContext.getString(R.string.title_bar_loading);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001827 }
John Reck30c714c2010-12-16 17:30:34 -08001828 return mCurrentState.mTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001829 }
1830
1831 /**
John Reck30c714c2010-12-16 17:30:34 -08001832 * Get the favicon of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001833 */
1834 Bitmap getFavicon() {
John Reck1cf4b792011-07-26 10:22:22 -07001835 if (mCurrentState.mFavicon != null) {
1836 return mCurrentState.mFavicon;
1837 }
1838 return getDefaultFavicon(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001839 }
1840
John Recke969cc52010-12-21 17:24:43 -08001841 public boolean isBookmarkedSite() {
1842 return mCurrentState.mIsBookmarkedSite;
1843 }
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001844
Grace Kloba22ac16e2009-10-07 18:00:23 -07001845 /**
1846 * Return the tab's error console. Creates the console if createIfNEcessary
1847 * is true and we haven't already created the console.
1848 * @param createIfNecessary Flag to indicate if the console should be
1849 * created if it has not been already.
1850 * @return The tab's error console, or null if one has not been created and
1851 * createIfNecessary is false.
1852 */
1853 ErrorConsoleView getErrorConsole(boolean createIfNecessary) {
1854 if (createIfNecessary && mErrorConsole == null) {
Michael Kolb14612442011-06-24 13:06:29 -07001855 mErrorConsole = new ErrorConsoleView(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001856 mErrorConsole.setWebView(mMainView);
1857 }
1858 return mErrorConsole;
1859 }
1860
John Reck30c714c2010-12-16 17:30:34 -08001861 private void setLockIconType(LockIcon icon) {
1862 mCurrentState.mLockIcon = icon;
1863 mWebViewController.onUpdatedLockIcon(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001864 }
1865
1866 /**
1867 * @return The tab's lock icon type.
1868 */
John Reck30c714c2010-12-16 17:30:34 -08001869 LockIcon getLockIconType() {
1870 return mCurrentState.mLockIcon;
1871 }
1872
1873 int getLoadProgress() {
1874 if (mInPageLoad) {
1875 return mPageLoadProgress;
1876 }
1877 return 100;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001878 }
1879
1880 /**
1881 * @return TRUE if onPageStarted is called while onPageFinished is not
1882 * called yet.
1883 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001884 boolean inPageLoad() {
1885 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001886 }
1887
1888 // force mInLoad to be false. This should only be called before closing the
1889 // tab to ensure BrowserActivity's pauseWebViewTimers() is called correctly.
Michael Kolb8233fac2010-10-26 16:08:53 -07001890 void clearInPageLoad() {
1891 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001892 }
1893
Grace Kloba22ac16e2009-10-07 18:00:23 -07001894 /**
John Reck1cf4b792011-07-26 10:22:22 -07001895 * @return The Bundle with the tab's state if it can be saved, otherwise null
Grace Kloba22ac16e2009-10-07 18:00:23 -07001896 */
John Reck1cf4b792011-07-26 10:22:22 -07001897 public Bundle saveState() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001898 // If the WebView is null it means we ran low on memory and we already
1899 // stored the saved state in mSavedState.
1900 if (mMainView == null) {
John Reck1cf4b792011-07-26 10:22:22 -07001901 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001902 }
John Reck24f18262011-06-17 14:47:20 -07001903 // If the tab is the homepage or has no URL, don't save it
1904 String homepage = BrowserSettings.getInstance().getHomePage();
1905 if (TextUtils.equals(homepage, mCurrentState.mUrl)
1906 || TextUtils.isEmpty(mCurrentState.mUrl)) {
John Reck1cf4b792011-07-26 10:22:22 -07001907 return null;
John Reck24f18262011-06-17 14:47:20 -07001908 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001909
1910 mSavedState = new Bundle();
John Reck541f55a2011-06-07 16:34:43 -07001911 mMainView.saveState(mSavedState);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001912
Michael Kolbc831b632011-05-11 09:30:34 -07001913 mSavedState.putLong(ID, mId);
John Reck30c714c2010-12-16 17:30:34 -08001914 mSavedState.putString(CURRURL, mCurrentState.mUrl);
1915 mSavedState.putString(CURRTITLE, mCurrentState.mTitle);
John Reck1cf4b792011-07-26 10:22:22 -07001916 mSavedState.putBoolean(INCOGNITO, mMainView.isPrivateBrowsingEnabled());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001917 if (mAppId != null) {
1918 mSavedState.putString(APPID, mAppId);
1919 }
Michael Kolbe28b3472011-08-04 16:54:31 -07001920 mSavedState.putBoolean(CLOSEFLAG, mCloseOnBack);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001921 // Remember the parent tab so the relationship can be restored.
Michael Kolbc831b632011-05-11 09:30:34 -07001922 if (mParent != null) {
1923 mSavedState.putLong(PARENTTAB, mParent.mId);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001924 }
John Reckb0a86db2011-05-24 14:05:58 -07001925 mSavedState.putBoolean(USERAGENT,
1926 mSettings.hasDesktopUseragent(getWebView()));
John Reck1cf4b792011-07-26 10:22:22 -07001927 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001928 }
1929
1930 /*
1931 * Restore the state of the tab.
1932 */
John Reck1cf4b792011-07-26 10:22:22 -07001933 private void restoreState(Bundle b) {
1934 mSavedState = b;
1935 if (mSavedState == null) {
1936 return;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001937 }
1938 // Restore the internal state even if the WebView fails to restore.
1939 // This will maintain the app id, original url and close-on-exit values.
Michael Kolbc831b632011-05-11 09:30:34 -07001940 mId = b.getLong(ID);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001941 mAppId = b.getString(APPID);
Michael Kolbe28b3472011-08-04 16:54:31 -07001942 mCloseOnBack = b.getBoolean(CLOSEFLAG);
John Reckb0a86db2011-05-24 14:05:58 -07001943 if (b.getBoolean(USERAGENT)
1944 != mSettings.hasDesktopUseragent(getWebView())) {
1945 mSettings.toggleDesktopUseragent(getWebView());
1946 }
John Reck1cf4b792011-07-26 10:22:22 -07001947 String url = b.getString(CURRURL);
1948 String title = b.getString(CURRTITLE);
1949 boolean incognito = b.getBoolean(INCOGNITO);
1950 mCurrentState = new PageState(mContext, incognito, url, null);
1951 mCurrentState.mTitle = title;
1952 synchronized (Tab.this) {
1953 if (mCapture != null) {
1954 BackgroundHandler.execute(mLoadThumbnail);
1955 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001956 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001957 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04001958
Leon Scroggins1961ed22010-12-07 15:22:21 -05001959 public void updateBookmarkedStatus() {
John Recke969cc52010-12-21 17:24:43 -08001960 mDataController.queryBookmarkStatus(getUrl(), mIsBookmarkCallback);
Leon Scroggins1961ed22010-12-07 15:22:21 -05001961 }
1962
John Recke969cc52010-12-21 17:24:43 -08001963 private DataController.OnQueryUrlIsBookmark mIsBookmarkCallback
1964 = new DataController.OnQueryUrlIsBookmark() {
1965 @Override
1966 public void onQueryUrlIsBookmark(String url, boolean isBookmark) {
1967 if (mCurrentState.mUrl.equals(url)) {
1968 mCurrentState.mIsBookmarkedSite = isBookmark;
1969 mWebViewController.bookmarkedStatusHasChanged(Tab.this);
1970 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001971 }
John Recke969cc52010-12-21 17:24:43 -08001972 };
Michael Kolb1acef692011-03-08 14:12:06 -08001973
Michael Kolbeb95db42011-03-03 10:38:40 -08001974 public Bitmap getScreenshot() {
John Reck1cf4b792011-07-26 10:22:22 -07001975 synchronized (Tab.this) {
1976 return mCapture;
1977 }
Michael Kolbeb95db42011-03-03 10:38:40 -08001978 }
1979
John Reck541f55a2011-06-07 16:34:43 -07001980 public boolean isSnapshot() {
John Reck541f55a2011-06-07 16:34:43 -07001981 return false;
1982 }
1983
John Reckd8c74522011-06-14 08:45:00 -07001984 public ContentValues createSnapshotValues() {
1985 if (mMainView == null) return null;
John Reck8cc92352011-07-06 17:41:52 -07001986 ByteArrayOutputStream bos = new ByteArrayOutputStream();
1987 try {
1988 GZIPOutputStream stream = new GZIPOutputStream(bos);
1989 if (!mMainView.saveViewState(stream)) {
1990 return null;
1991 }
1992 stream.flush();
1993 stream.close();
1994 } catch (Exception e) {
1995 Log.w(LOGTAG, "Failed to save view state", e);
John Reck541f55a2011-06-07 16:34:43 -07001996 return null;
1997 }
John Reck8cc92352011-07-06 17:41:52 -07001998 byte[] data = bos.toByteArray();
John Reckd8c74522011-06-14 08:45:00 -07001999 ContentValues values = new ContentValues();
2000 values.put(Snapshots.TITLE, mCurrentState.mTitle);
2001 values.put(Snapshots.URL, mCurrentState.mUrl);
2002 values.put(Snapshots.VIEWSTATE, data);
2003 values.put(Snapshots.BACKGROUND, mMainView.getPageBackgroundColor());
John Reck8cc92352011-07-06 17:41:52 -07002004 values.put(Snapshots.DATE_CREATED, System.currentTimeMillis());
2005 values.put(Snapshots.FAVICON, compressBitmap(getFavicon()));
2006 Bitmap screenshot = Controller.createScreenshot(mMainView,
2007 Controller.getDesiredThumbnailWidth(mContext),
2008 Controller.getDesiredThumbnailHeight(mContext));
2009 values.put(Snapshots.THUMBNAIL, compressBitmap(screenshot));
John Reckd8c74522011-06-14 08:45:00 -07002010 return values;
John Reck541f55a2011-06-07 16:34:43 -07002011 }
2012
John Reck8cc92352011-07-06 17:41:52 -07002013 public byte[] compressBitmap(Bitmap bitmap) {
2014 if (bitmap == null) {
2015 return null;
2016 }
2017 ByteArrayOutputStream stream = new ByteArrayOutputStream();
2018 bitmap.compress(CompressFormat.PNG, 100, stream);
2019 return stream.toByteArray();
2020 }
2021
John Reck26b18322011-06-21 13:08:58 -07002022 public void loadUrl(String url, Map<String, String> headers) {
2023 if (mMainView != null) {
Michael Kolb14612442011-06-24 13:06:29 -07002024 mCurrentState = new PageState(mContext, false, url, null);
John Reck26b18322011-06-21 13:08:58 -07002025 mWebViewController.onPageStarted(this, mMainView, null);
2026 mMainView.loadUrl(url, headers);
2027 }
2028 }
2029
Michael Kolb9ef259a2011-07-12 15:33:08 -07002030 protected void capture() {
2031 if (mMainView == null || mCapture == null) return;
2032 Canvas c = new Canvas(mCapture);
2033 final int left = mMainView.getScrollX();
2034 final int top = mMainView.getScrollY() + mMainView.getVisibleTitleHeight();
2035 c.translate(-left, -top);
2036 float scale = mCaptureWidth / (float) mMainView.getWidth();
2037 c.scale(scale, scale, left, top);
John Reck8ee633f2011-08-09 16:00:35 -07002038 if (mMainView instanceof BrowserWebView) {
2039 ((BrowserWebView)mMainView).drawContent(c);
2040 } else {
2041 mMainView.draw(c);
2042 }
Dianne Hackborn43cfe8a2011-08-02 16:59:35 -07002043 c.setBitmap(null);
John Reck8ee633f2011-08-09 16:00:35 -07002044 mHandler.removeMessages(MSG_CAPTURE);
John Reck1cf4b792011-07-26 10:22:22 -07002045 persistThumbnail();
John Reck8ee633f2011-08-09 16:00:35 -07002046 TabControl tc = mWebViewController.getTabControl();
2047 if (tc != null) {
2048 OnThumbnailUpdatedListener updateListener
2049 = tc.getOnThumbnailUpdatedListener();
2050 if (updateListener != null) {
2051 updateListener.onThumbnailUpdated(this);
2052 }
2053 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07002054 }
2055
2056 @Override
2057 public void onNewPicture(WebView view, Picture picture) {
2058 //update screenshot
John Reck1cf4b792011-07-26 10:22:22 -07002059 postCapture();
2060 }
2061
2062 private void postCapture() {
Michael Kolb9ef259a2011-07-12 15:33:08 -07002063 if (!mHandler.hasMessages(MSG_CAPTURE)) {
2064 mHandler.sendEmptyMessageDelayed(MSG_CAPTURE, CAPTURE_DELAY);
2065 }
2066 }
2067
John Reckef654f12011-07-12 16:42:08 -07002068 public boolean canGoBack() {
2069 return mMainView != null ? mMainView.canGoBack() : false;
2070 }
2071
2072 public boolean canGoForward() {
2073 return mMainView != null ? mMainView.canGoForward() : false;
2074 }
2075
2076 public void goBack() {
2077 if (mMainView != null) {
2078 mMainView.goBack();
2079 }
2080 }
2081
2082 public void goForward() {
2083 if (mMainView != null) {
2084 mMainView.goForward();
2085 }
2086 }
2087
Mathew Inwood1dd8e822011-08-03 14:34:29 +01002088 /**
2089 * Causes the tab back/forward stack to be cleared once, if the given URL is the next URL
2090 * to be added to the stack.
2091 *
2092 * This is used to ensure that preloaded URLs that are not subsequently seen by the user do
2093 * not appear in the back stack.
2094 */
2095 public void clearBackStackWhenItemAdded(String urlToMatch) {
2096 mClearHistoryMatchUrl = urlToMatch;
2097 }
2098
John Reck1cf4b792011-07-26 10:22:22 -07002099 protected void persistThumbnail() {
2100 BackgroundHandler.execute(mSaveThumbnail);
2101 }
2102
2103 protected void deleteThumbnail() {
2104 BackgroundHandler.execute(mDeleteThumbnail);
2105 }
2106
2107 private void updateCaptureFromBlob(byte[] blob) {
2108 synchronized (Tab.this) {
2109 if (mCapture == null) {
2110 return;
2111 }
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002112 ByteBuffer buffer = ByteBuffer.wrap(blob);
Michael Kolbd837a112011-08-09 14:00:18 -07002113 try {
2114 mCapture.copyPixelsFromBuffer(buffer);
2115 } catch (RuntimeException rex) {
2116 Log.e(LOGTAG, "Load capture has mismatched sizes; buffer: "
2117 + buffer.capacity() + " blob: " + blob.length
2118 + "capture: " + mCapture.getByteCount());
2119 throw rex;
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002120 }
John Reck1cf4b792011-07-26 10:22:22 -07002121 }
2122 }
2123
2124 private byte[] getCaptureBlob() {
2125 synchronized (Tab.this) {
2126 if (mCapture == null) {
2127 return null;
2128 }
2129 ByteBuffer buffer = ByteBuffer.allocate(mCapture.getByteCount());
2130 mCapture.copyPixelsToBuffer(buffer);
2131 return buffer.array();
2132 }
2133 }
2134
2135 private Runnable mSaveThumbnail = new Runnable() {
2136
2137 @Override
2138 public void run() {
2139 byte[] blob = getCaptureBlob();
2140 if (blob == null) {
2141 return;
2142 }
2143 ContentResolver cr = mContext.getContentResolver();
2144 ContentValues values = new ContentValues();
2145 values.put(Thumbnails._ID, mId);
2146 values.put(Thumbnails.THUMBNAIL, blob);
2147 cr.insert(Thumbnails.CONTENT_URI, values);
2148 }
2149 };
2150
2151 private Runnable mDeleteThumbnail = new Runnable() {
2152
2153 @Override
2154 public void run() {
2155 ContentResolver cr = mContext.getContentResolver();
2156 try {
2157 cr.delete(ContentUris.withAppendedId(Thumbnails.CONTENT_URI, mId),
2158 null, null);
2159 } catch (Throwable t) {}
2160 }
2161 };
2162
2163 private Runnable mLoadThumbnail = new Runnable() {
2164
2165 @Override
2166 public void run() {
2167 ContentResolver cr = mContext.getContentResolver();
2168 Cursor c = null;
2169 try {
2170 Uri uri = ContentUris.withAppendedId(Thumbnails.CONTENT_URI, mId);
2171 c = cr.query(uri, new String[] {Thumbnails._ID,
2172 Thumbnails.THUMBNAIL}, null, null, null);
2173 if (c.moveToFirst()) {
2174 byte[] data = c.getBlob(1);
2175 if (data != null && data.length > 0) {
2176 updateCaptureFromBlob(data);
2177 }
2178 }
2179 } finally {
2180 if (c != null) {
2181 c.close();
2182 }
2183 }
2184 }
2185 };
2186
Grace Kloba22ac16e2009-10-07 18:00:23 -07002187}