blob: a251212ce640376d30d55461bba03cc37236700c [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 Reck541f55a2011-06-07 16:34:43 -070071import com.android.browser.homepages.HomeProvider;
John Reck1cf4b792011-07-26 10:22:22 -070072import com.android.browser.provider.BrowserProvider2.Thumbnails;
John Reck8cc92352011-07-06 17:41:52 -070073import com.android.browser.provider.SnapshotProvider.Snapshots;
John Reck541f55a2011-06-07 16:34:43 -070074import com.android.common.speech.LoggingEvents;
75
76import java.io.ByteArrayOutputStream;
John Reck1cf4b792011-07-26 10:22:22 -070077import java.nio.ByteBuffer;
Michael Kolbfe251992010-07-08 15:41:55 -070078import java.util.ArrayList;
79import java.util.HashMap;
80import java.util.Iterator;
81import java.util.LinkedList;
82import java.util.Map;
83import java.util.Vector;
John Reck8cc92352011-07-06 17:41:52 -070084import java.util.zip.GZIPOutputStream;
Michael Kolbfe251992010-07-08 15:41:55 -070085
Grace Kloba22ac16e2009-10-07 18:00:23 -070086/**
87 * Class for maintaining Tabs with a main WebView and a subwindow.
88 */
Michael Kolb9ef259a2011-07-12 15:33:08 -070089class Tab implements PictureListener {
Michael Kolb8233fac2010-10-26 16:08:53 -070090
Grace Kloba22ac16e2009-10-07 18:00:23 -070091 // Log Tag
92 private static final String LOGTAG = "Tab";
Mathew Inwood1dd8e822011-08-03 14:34:29 +010093 private static final boolean LOGD_ENABLED = com.android.browser.Browser.LOGD_ENABLED;
Ben Murdochc42addf2010-01-28 15:19:59 +000094 // Special case the logtag for messages for the Console to make it easier to
95 // filter them and match the logtag used for these messages in older versions
96 // of the browser.
97 private static final String CONSOLE_LOGTAG = "browser";
98
Michael Kolb9ef259a2011-07-12 15:33:08 -070099 private static final int MSG_CAPTURE = 42;
100 private static final int CAPTURE_DELAY = 500;
101
John Reck1cf4b792011-07-26 10:22:22 -0700102 private static Bitmap sDefaultFavicon;
103
John Reck30c714c2010-12-16 17:30:34 -0800104 public enum LockIcon {
105 LOCK_ICON_UNSECURE,
106 LOCK_ICON_SECURE,
107 LOCK_ICON_MIXED,
108 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700109
Michael Kolb14612442011-06-24 13:06:29 -0700110 Context mContext;
John Reckd8c74522011-06-14 08:45:00 -0700111 protected WebViewController mWebViewController;
Michael Kolb8233fac2010-10-26 16:08:53 -0700112
Michael Kolbc831b632011-05-11 09:30:34 -0700113 // The tab ID
John Reckd8c74522011-06-14 08:45:00 -0700114 private long mId = -1;
Michael Kolbc831b632011-05-11 09:30:34 -0700115
Grace Kloba22ac16e2009-10-07 18:00:23 -0700116 // The Geolocation permissions prompt
117 private GeolocationPermissionsPrompt mGeolocationPermissionsPrompt;
118 // Main WebView wrapper
Michael Kolba713ec82010-11-29 17:27:06 -0800119 private View mContainer;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700120 // Main WebView
121 private WebView mMainView;
122 // Subwindow container
123 private View mSubViewContainer;
124 // Subwindow WebView
125 private WebView mSubView;
126 // Saved bundle for when we are running low on memory. It contains the
127 // information needed to restore the WebView if the user goes back to the
128 // tab.
129 private Bundle mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700130 // Parent Tab. This is the Tab that created this Tab, or null if the Tab was
131 // created by the UI
Michael Kolbc831b632011-05-11 09:30:34 -0700132 private Tab mParent;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700133 // Tab that constructed by this Tab. This is used when this Tab is
134 // destroyed, it clears all mParentTab values in the children.
Michael Kolbc831b632011-05-11 09:30:34 -0700135 private Vector<Tab> mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700136 // If true, the tab is in the foreground of the current activity.
137 private boolean mInForeground;
Michael Kolb8233fac2010-10-26 16:08:53 -0700138 // If true, the tab is in page loading state (after onPageStarted,
139 // before onPageFinsihed)
140 private boolean mInPageLoad;
John Reck30c714c2010-12-16 17:30:34 -0800141 // The last reported progress of the current page
142 private int mPageLoadProgress;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000143 // The time the load started, used to find load page time
144 private long mLoadStartTime;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700145 // Application identifier used to find tabs that another application wants
146 // to reuse.
147 private String mAppId;
148 // Keep the original url around to avoid killing the old WebView if the url
149 // has not changed.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700150 // Error console for the tab
151 private ErrorConsoleView mErrorConsole;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -0500152 // The listener that gets invoked when a download is started from the
153 // mMainView
154 private final DownloadListener mDownloadListener;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500155 // Listener used to know when we move forward or back in the history list.
156 private final WebBackForwardListClient mWebBackForwardListClient;
John Recke969cc52010-12-21 17:24:43 -0800157 private DataController mDataController;
Patrick Scott92066772011-03-10 08:46:27 -0500158 // State of the auto-login request.
159 private DeviceAccountLogin mDeviceAccountLogin;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700160
161 // AsyncTask for downloading touch icons
162 DownloadTouchIcon mTouchIconLoader;
163
John Reck35e9dd62011-04-25 09:01:54 -0700164 private BrowserSettings mSettings;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700165 private int mCaptureWidth;
166 private int mCaptureHeight;
167 private Bitmap mCapture;
168 private Handler mHandler;
169
Mathew Inwood1dd8e822011-08-03 14:34:29 +0100170 /**
171 * See {@link #clearBackStackWhenItemAdded(String)}.
172 */
173 private String mClearHistoryMatchUrl;
174
John Reck1cf4b792011-07-26 10:22:22 -0700175 private static synchronized Bitmap getDefaultFavicon(Context context) {
176 if (sDefaultFavicon == null) {
177 sDefaultFavicon = BitmapFactory.decodeResource(
178 context.getResources(), R.drawable.app_web_browser_sm);
179 }
180 return sDefaultFavicon;
181 }
Michael Kolbeb95db42011-03-03 10:38:40 -0800182
John Reck30c714c2010-12-16 17:30:34 -0800183 // All the state needed for a page
John Reckd8c74522011-06-14 08:45:00 -0700184 protected static class PageState {
John Reck30c714c2010-12-16 17:30:34 -0800185 String mUrl;
John Reckdb22ec42011-06-29 11:31:24 -0700186 String mOriginalUrl;
John Reck30c714c2010-12-16 17:30:34 -0800187 String mTitle;
188 LockIcon mLockIcon;
189 Bitmap mFavicon;
John Recke969cc52010-12-21 17:24:43 -0800190 Boolean mIsBookmarkedSite = false;
John Reck30c714c2010-12-16 17:30:34 -0800191
192 PageState(Context c, boolean incognito) {
193 if (incognito) {
John Reckdb22ec42011-06-29 11:31:24 -0700194 mOriginalUrl = mUrl = "browser:incognito";
John Reck30c714c2010-12-16 17:30:34 -0800195 mTitle = c.getString(R.string.new_incognito_tab);
John Reck30c714c2010-12-16 17:30:34 -0800196 } else {
John Reckdb22ec42011-06-29 11:31:24 -0700197 mOriginalUrl = mUrl = "";
John Reck30c714c2010-12-16 17:30:34 -0800198 mTitle = c.getString(R.string.new_tab);
John Reck30c714c2010-12-16 17:30:34 -0800199 }
John Reck1cf4b792011-07-26 10:22:22 -0700200 mFavicon = null;
John Reck30c714c2010-12-16 17:30:34 -0800201 mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
202 }
203
204 PageState(Context c, boolean incognito, String url, Bitmap favicon) {
John Reckdb22ec42011-06-29 11:31:24 -0700205 mOriginalUrl = mUrl = url;
John Reck30c714c2010-12-16 17:30:34 -0800206 mTitle = null;
207 if (URLUtil.isHttpsUrl(url)) {
208 mLockIcon = LockIcon.LOCK_ICON_SECURE;
209 } else {
210 mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
211 }
John Reck1cf4b792011-07-26 10:22:22 -0700212 mFavicon = favicon;
John Reck30c714c2010-12-16 17:30:34 -0800213 }
John Reck1cf4b792011-07-26 10:22:22 -0700214
Grace Kloba22ac16e2009-10-07 18:00:23 -0700215 }
216
John Reck30c714c2010-12-16 17:30:34 -0800217 // The current/loading page's state
John Reckd8c74522011-06-14 08:45:00 -0700218 protected PageState mCurrentState;
John Reck30c714c2010-12-16 17:30:34 -0800219
Grace Kloba22ac16e2009-10-07 18:00:23 -0700220 // Used for saving and restoring each Tab
Michael Kolbc831b632011-05-11 09:30:34 -0700221 static final String ID = "ID";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700222 static final String CURRURL = "currentUrl";
223 static final String CURRTITLE = "currentTitle";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700224 static final String PARENTTAB = "parentTab";
225 static final String APPID = "appid";
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700226 static final String INCOGNITO = "privateBrowsingEnabled";
John Reckb0a86db2011-05-24 14:05:58 -0700227 static final String USERAGENT = "useragent";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700228
229 // -------------------------------------------------------------------------
230
Leon Scroggins58d56c62010-01-28 15:12:40 -0500231 /**
232 * Private information regarding the latest voice search. If the Tab is not
233 * in voice search mode, this will be null.
234 */
235 private VoiceSearchData mVoiceSearchData;
236 /**
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400237 * Remove voice search mode from this tab.
238 */
239 public void revertVoiceSearchMode() {
240 if (mVoiceSearchData != null) {
241 mVoiceSearchData = null;
242 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700243 mWebViewController.revertVoiceSearchMode(this);
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400244 }
245 }
246 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700247
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400248 /**
Leon Scroggins58d56c62010-01-28 15:12:40 -0500249 * Return whether the tab is in voice search mode.
250 */
251 public boolean isInVoiceSearchMode() {
252 return mVoiceSearchData != null;
253 }
254 /**
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400255 * Return true if the Tab is in voice search mode and the voice search
256 * Intent came with a String identifying that Google provided the Intent.
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500257 */
258 public boolean voiceSearchSourceIsGoogle() {
259 return mVoiceSearchData != null && mVoiceSearchData.mSourceIsGoogle;
260 }
261 /**
Leon Scroggins58d56c62010-01-28 15:12:40 -0500262 * Get the title to display for the current voice search page. If the Tab
263 * is not in voice search mode, return null.
264 */
265 public String getVoiceDisplayTitle() {
266 if (mVoiceSearchData == null) return null;
267 return mVoiceSearchData.mLastVoiceSearchTitle;
268 }
269 /**
270 * Get the latest array of voice search results, to be passed to the
271 * BrowserProvider. If the Tab is not in voice search mode, return null.
272 */
273 public ArrayList<String> getVoiceSearchResults() {
274 if (mVoiceSearchData == null) return null;
275 return mVoiceSearchData.mVoiceSearchResults;
276 }
277 /**
278 * Activate voice search mode.
279 * @param intent Intent which has the results to use, or an index into the
280 * results when reusing the old results.
281 */
282 /* package */ void activateVoiceSearchMode(Intent intent) {
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500283 int index = 0;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500284 ArrayList<String> results = intent.getStringArrayListExtra(
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -0500285 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_STRINGS);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500286 if (results != null) {
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500287 ArrayList<String> urls = intent.getStringArrayListExtra(
288 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_URLS);
289 ArrayList<String> htmls = intent.getStringArrayListExtra(
290 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_HTML);
291 ArrayList<String> baseUrls = intent.getStringArrayListExtra(
292 RecognizerResultsIntent
293 .EXTRA_VOICE_SEARCH_RESULT_HTML_BASE_URLS);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500294 // This tab is now entering voice search mode for the first time, or
295 // a new voice search was done.
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500296 int size = results.size();
297 if (urls == null || size != urls.size()) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500298 throw new AssertionError("improper extras passed in Intent");
299 }
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500300 if (htmls == null || htmls.size() != size || baseUrls == null ||
301 (baseUrls.size() != size && baseUrls.size() != 1)) {
302 // If either of these arrays are empty/incorrectly sized, ignore
303 // them.
304 htmls = null;
305 baseUrls = null;
306 }
307 mVoiceSearchData = new VoiceSearchData(results, urls, htmls,
308 baseUrls);
Leon Scroggins9df94972010-03-08 18:20:35 -0500309 mVoiceSearchData.mHeaders = intent.getParcelableArrayListExtra(
310 RecognizerResultsIntent
311 .EXTRA_VOICE_SEARCH_RESULT_HTTP_HEADERS);
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500312 mVoiceSearchData.mSourceIsGoogle = intent.getBooleanExtra(
313 VoiceSearchData.SOURCE_IS_GOOGLE, false);
Leon Scroggins2ee4a5a2010-03-15 16:56:57 -0400314 mVoiceSearchData.mVoiceSearchIntent = new Intent(intent);
Leon Scrogginse10dde52010-03-08 19:53:03 -0500315 }
316 String extraData = intent.getStringExtra(
317 SearchManager.EXTRA_DATA_KEY);
318 if (extraData != null) {
319 index = Integer.parseInt(extraData);
320 if (index >= mVoiceSearchData.mVoiceSearchResults.size()) {
321 throw new AssertionError("index must be less than "
322 + "size of mVoiceSearchResults");
323 }
324 if (mVoiceSearchData.mSourceIsGoogle) {
325 Intent logIntent = new Intent(
326 LoggingEvents.ACTION_LOG_EVENT);
327 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
328 LoggingEvents.VoiceSearch.N_BEST_CHOOSE);
329 logIntent.putExtra(
330 LoggingEvents.VoiceSearch.EXTRA_N_BEST_CHOOSE_INDEX,
331 index);
Michael Kolb14612442011-06-24 13:06:29 -0700332 mContext.sendBroadcast(logIntent);
Leon Scrogginse10dde52010-03-08 19:53:03 -0500333 }
334 if (mVoiceSearchData.mVoiceSearchIntent != null) {
Leon Scroggins2ee4a5a2010-03-15 16:56:57 -0400335 // Copy the Intent, so that each history item will have its own
336 // Intent, with different (or none) extra data.
337 Intent latest = new Intent(mVoiceSearchData.mVoiceSearchIntent);
338 latest.putExtra(SearchManager.EXTRA_DATA_KEY, extraData);
339 mVoiceSearchData.mVoiceSearchIntent = latest;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500340 }
341 }
342 mVoiceSearchData.mLastVoiceSearchTitle
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500343 = mVoiceSearchData.mVoiceSearchResults.get(index);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500344 if (mInForeground) {
Michael Kolb11d19782011-03-20 10:17:40 -0700345 mWebViewController.activateVoiceSearchMode(
346 mVoiceSearchData.mLastVoiceSearchTitle,
347 mVoiceSearchData.mVoiceSearchResults);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500348 }
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500349 if (mVoiceSearchData.mVoiceSearchHtmls != null) {
350 // When index was found it was already ensured that it was valid
351 String uriString = mVoiceSearchData.mVoiceSearchHtmls.get(index);
352 if (uriString != null) {
353 Uri dataUri = Uri.parse(uriString);
354 if (RecognizerResultsIntent.URI_SCHEME_INLINE.equals(
355 dataUri.getScheme())) {
356 // If there is only one base URL, use it. If there are
357 // more, there will be one for each index, so use the base
358 // URL corresponding to the index.
359 String baseUrl = mVoiceSearchData.mVoiceSearchBaseUrls.get(
360 mVoiceSearchData.mVoiceSearchBaseUrls.size() > 1 ?
361 index : 0);
362 mVoiceSearchData.mLastVoiceSearchUrl = baseUrl;
363 mMainView.loadDataWithBaseURL(baseUrl,
364 uriString.substring(RecognizerResultsIntent
365 .URI_SCHEME_INLINE.length() + 1), "text/html",
366 "utf-8", baseUrl);
367 return;
368 }
369 }
370 }
Leon Scroggins58d56c62010-01-28 15:12:40 -0500371 mVoiceSearchData.mLastVoiceSearchUrl
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500372 = mVoiceSearchData.mVoiceSearchUrls.get(index);
373 if (null == mVoiceSearchData.mLastVoiceSearchUrl) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700374 mVoiceSearchData.mLastVoiceSearchUrl = UrlUtils.smartUrlFilter(
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500375 mVoiceSearchData.mLastVoiceSearchTitle);
376 }
Leon Scroggins9df94972010-03-08 18:20:35 -0500377 Map<String, String> headers = null;
378 if (mVoiceSearchData.mHeaders != null) {
379 int bundleIndex = mVoiceSearchData.mHeaders.size() == 1 ? 0
380 : index;
381 Bundle bundle = mVoiceSearchData.mHeaders.get(bundleIndex);
382 if (bundle != null && !bundle.isEmpty()) {
383 Iterator<String> iter = bundle.keySet().iterator();
384 headers = new HashMap<String, String>();
385 while (iter.hasNext()) {
386 String key = iter.next();
387 headers.put(key, bundle.getString(key));
388 }
389 }
390 }
391 mMainView.loadUrl(mVoiceSearchData.mLastVoiceSearchUrl, headers);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500392 }
393 /* package */ static class VoiceSearchData {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500394 public VoiceSearchData(ArrayList<String> results,
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500395 ArrayList<String> urls, ArrayList<String> htmls,
396 ArrayList<String> baseUrls) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500397 mVoiceSearchResults = results;
398 mVoiceSearchUrls = urls;
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500399 mVoiceSearchHtmls = htmls;
400 mVoiceSearchBaseUrls = baseUrls;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500401 }
402 /*
403 * ArrayList of suggestions to be displayed when opening the
404 * SearchManager
405 */
406 public ArrayList<String> mVoiceSearchResults;
407 /*
408 * ArrayList of urls, associated with the suggestions in
409 * mVoiceSearchResults.
410 */
411 public ArrayList<String> mVoiceSearchUrls;
412 /*
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500413 * ArrayList holding content to load for each item in
414 * mVoiceSearchResults.
415 */
416 public ArrayList<String> mVoiceSearchHtmls;
417 /*
418 * ArrayList holding base urls for the items in mVoiceSearchResults.
419 * If non null, this will either have the same size as
420 * mVoiceSearchResults or have a size of 1, in which case all will use
421 * the same base url
422 */
423 public ArrayList<String> mVoiceSearchBaseUrls;
424 /*
Leon Scroggins58d56c62010-01-28 15:12:40 -0500425 * The last url provided by voice search. Used for comparison to see if
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500426 * we are going to a page by some method besides voice search.
Leon Scroggins58d56c62010-01-28 15:12:40 -0500427 */
428 public String mLastVoiceSearchUrl;
429 /**
430 * The last title used for voice search. Needed to update the title bar
431 * when switching tabs.
432 */
433 public String mLastVoiceSearchTitle;
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500434 /**
435 * Whether the Intent which turned on voice search mode contained the
436 * String signifying that Google was the source.
437 */
438 public boolean mSourceIsGoogle;
439 /**
Leon Scroggins9df94972010-03-08 18:20:35 -0500440 * List of headers to be passed into the WebView containing location
441 * information
442 */
443 public ArrayList<Bundle> mHeaders;
444 /**
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500445 * The Intent used to invoke voice search. Placed on the
446 * WebHistoryItem so that when coming back to a previous voice search
447 * page we can again activate voice search.
448 */
Leon Scrogginse10dde52010-03-08 19:53:03 -0500449 public Intent mVoiceSearchIntent;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500450 /**
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500451 * String used to identify Google as the source of voice search.
452 */
453 public static String SOURCE_IS_GOOGLE
454 = "android.speech.extras.SOURCE_IS_GOOGLE";
Leon Scroggins58d56c62010-01-28 15:12:40 -0500455 }
456
Grace Kloba22ac16e2009-10-07 18:00:23 -0700457 // Container class for the next error dialog that needs to be displayed
458 private class ErrorDialog {
459 public final int mTitle;
460 public final String mDescription;
461 public final int mError;
462 ErrorDialog(int title, String desc, int error) {
463 mTitle = title;
464 mDescription = desc;
465 mError = error;
466 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700467 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700468
469 private void processNextError() {
470 if (mQueuedErrors == null) {
471 return;
472 }
473 // The first one is currently displayed so just remove it.
474 mQueuedErrors.removeFirst();
475 if (mQueuedErrors.size() == 0) {
476 mQueuedErrors = null;
477 return;
478 }
479 showError(mQueuedErrors.getFirst());
480 }
481
482 private DialogInterface.OnDismissListener mDialogListener =
483 new DialogInterface.OnDismissListener() {
484 public void onDismiss(DialogInterface d) {
485 processNextError();
486 }
487 };
488 private LinkedList<ErrorDialog> mQueuedErrors;
489
490 private void queueError(int err, String desc) {
491 if (mQueuedErrors == null) {
492 mQueuedErrors = new LinkedList<ErrorDialog>();
493 }
494 for (ErrorDialog d : mQueuedErrors) {
495 if (d.mError == err) {
496 // Already saw a similar error, ignore the new one.
497 return;
498 }
499 }
500 ErrorDialog errDialog = new ErrorDialog(
501 err == WebViewClient.ERROR_FILE_NOT_FOUND ?
502 R.string.browserFrameFileErrorLabel :
503 R.string.browserFrameNetworkErrorLabel,
504 desc, err);
505 mQueuedErrors.addLast(errDialog);
506
507 // Show the dialog now if the queue was empty and it is in foreground
508 if (mQueuedErrors.size() == 1 && mInForeground) {
509 showError(errDialog);
510 }
511 }
512
513 private void showError(ErrorDialog errDialog) {
514 if (mInForeground) {
Michael Kolb14612442011-06-24 13:06:29 -0700515 AlertDialog d = new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700516 .setTitle(errDialog.mTitle)
517 .setMessage(errDialog.mDescription)
518 .setPositiveButton(R.string.ok, null)
519 .create();
520 d.setOnDismissListener(mDialogListener);
521 d.show();
522 }
523 }
524
525 // -------------------------------------------------------------------------
526 // WebViewClient implementation for the main WebView
527 // -------------------------------------------------------------------------
528
529 private final WebViewClient mWebViewClient = new WebViewClient() {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500530 private Message mDontResend;
531 private Message mResend;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700532 @Override
533 public void onPageStarted(WebView view, String url, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700534 mInPageLoad = true;
John Reck30c714c2010-12-16 17:30:34 -0800535 mPageLoadProgress = 0;
Michael Kolb14612442011-06-24 13:06:29 -0700536 mCurrentState = new PageState(mContext,
John Reck30c714c2010-12-16 17:30:34 -0800537 view.isPrivateBrowsingEnabled(), url, favicon);
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000538 mLoadStartTime = SystemClock.uptimeMillis();
Leon Scroggins58d56c62010-01-28 15:12:40 -0500539 if (mVoiceSearchData != null
540 && !url.equals(mVoiceSearchData.mLastVoiceSearchUrl)) {
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500541 if (mVoiceSearchData.mSourceIsGoogle) {
542 Intent i = new Intent(LoggingEvents.ACTION_LOG_EVENT);
543 i.putExtra(LoggingEvents.EXTRA_FLUSH, true);
Michael Kolb14612442011-06-24 13:06:29 -0700544 mContext.sendBroadcast(i);
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500545 }
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400546 revertVoiceSearchMode();
Leon Scroggins58d56c62010-01-28 15:12:40 -0500547 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700548
Grace Kloba22ac16e2009-10-07 18:00:23 -0700549
550 // If we start a touch icon load and then load a new page, we don't
551 // want to cancel the current touch icon loader. But, we do want to
552 // create a new one when the touch icon url is known.
553 if (mTouchIconLoader != null) {
554 mTouchIconLoader.mTab = null;
555 mTouchIconLoader = null;
556 }
557
558 // reset the error console
559 if (mErrorConsole != null) {
560 mErrorConsole.clearErrorMessages();
Michael Kolb8233fac2010-10-26 16:08:53 -0700561 if (mWebViewController.shouldShowErrorConsole()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700562 mErrorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
563 }
564 }
565
Patrick Scott92066772011-03-10 08:46:27 -0500566 // Cancel the auto-login process.
567 if (mDeviceAccountLogin != null) {
568 mDeviceAccountLogin.cancel();
569 mDeviceAccountLogin = null;
570 mWebViewController.hideAutoLogin(Tab.this);
571 }
572
Grace Kloba22ac16e2009-10-07 18:00:23 -0700573 // finally update the UI in the activity if it is in the foreground
John Reck324d4402011-01-11 16:56:42 -0800574 mWebViewController.onPageStarted(Tab.this, view, favicon);
Leon Scroggins4cd97792010-12-03 15:31:56 -0500575
John Recke969cc52010-12-21 17:24:43 -0800576 updateBookmarkedStatus();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700577 }
578
579 @Override
580 public void onPageFinished(WebView view, String url) {
John Recka1696282011-07-08 14:10:37 -0700581 if (!mInPageLoad) {
582 // In page navigation links (www.something.com#footer) will
583 // trigger an onPageFinished which we don't care about.
584 return;
585 }
John Reck5b691842010-11-29 11:21:13 -0800586 if (!isPrivateBrowsingEnabled()) {
587 LogTag.logPageFinishedLoading(
588 url, SystemClock.uptimeMillis() - mLoadStartTime);
589 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700590 mInPageLoad = false;
John Reck1cf4b792011-07-26 10:22:22 -0700591 syncCurrentState(view, url);
John Reck324d4402011-01-11 16:56:42 -0800592 mWebViewController.onPageFinished(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700593 }
594
595 // return true if want to hijack the url to let another app to handle it
596 @Override
597 public boolean shouldOverrideUrlLoading(WebView view, String url) {
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400598 if (voiceSearchSourceIsGoogle()) {
599 // This method is called when the user clicks on a link.
600 // VoiceSearchMode is turned off when the user leaves the
601 // Google results page, so at this point the user must be on
602 // that page. If the user clicked a link on that page, assume
603 // that the voice search was effective, and broadcast an Intent
604 // so a receiver can take note of that fact.
605 Intent logIntent = new Intent(LoggingEvents.ACTION_LOG_EVENT);
606 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
607 LoggingEvents.VoiceSearch.RESULT_CLICKED);
Michael Kolb14612442011-06-24 13:06:29 -0700608 mContext.sendBroadcast(logIntent);
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400609 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700610 if (mInForeground) {
Michael Kolb18eb3772010-12-10 14:29:51 -0800611 return mWebViewController.shouldOverrideUrlLoading(Tab.this,
612 view, url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700613 } else {
614 return false;
615 }
616 }
617
618 /**
619 * Updates the lock icon. This method is called when we discover another
620 * resource to be loaded for this page (for example, javascript). While
621 * we update the icon type, we do not update the lock icon itself until
622 * we are done loading, it is slightly more secure this way.
623 */
624 @Override
625 public void onLoadResource(WebView view, String url) {
626 if (url != null && url.length() > 0) {
627 // It is only if the page claims to be secure that we may have
628 // to update the lock:
John Reck30c714c2010-12-16 17:30:34 -0800629 if (mCurrentState.mLockIcon == LockIcon.LOCK_ICON_SECURE) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700630 // If NOT a 'safe' url, change the lock to mixed content!
631 if (!(URLUtil.isHttpsUrl(url) || URLUtil.isDataUrl(url)
632 || URLUtil.isAboutUrl(url))) {
John Reck30c714c2010-12-16 17:30:34 -0800633 mCurrentState.mLockIcon = LockIcon.LOCK_ICON_MIXED;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700634 }
635 }
636 }
637 }
638
639 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700640 * Show a dialog informing the user of the network error reported by
641 * WebCore if it is in the foreground.
642 */
643 @Override
644 public void onReceivedError(WebView view, int errorCode,
645 String description, String failingUrl) {
646 if (errorCode != WebViewClient.ERROR_HOST_LOOKUP &&
647 errorCode != WebViewClient.ERROR_CONNECT &&
648 errorCode != WebViewClient.ERROR_BAD_URL &&
649 errorCode != WebViewClient.ERROR_UNSUPPORTED_SCHEME &&
650 errorCode != WebViewClient.ERROR_FILE) {
651 queueError(errorCode, description);
652 }
Jeff Hamilton47654f42010-09-07 09:57:51 -0500653
654 // Don't log URLs when in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -0700655 if (!isPrivateBrowsingEnabled()) {
Jeff Hamilton47654f42010-09-07 09:57:51 -0500656 Log.e(LOGTAG, "onReceivedError " + errorCode + " " + failingUrl
657 + " " + description);
658 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700659 }
660
661 /**
662 * Check with the user if it is ok to resend POST data as the page they
663 * are trying to navigate to is the result of a POST.
664 */
665 @Override
666 public void onFormResubmission(WebView view, final Message dontResend,
667 final Message resend) {
668 if (!mInForeground) {
669 dontResend.sendToTarget();
670 return;
671 }
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500672 if (mDontResend != null) {
673 Log.w(LOGTAG, "onFormResubmission should not be called again "
674 + "while dialog is still up");
675 dontResend.sendToTarget();
676 return;
677 }
678 mDontResend = dontResend;
679 mResend = resend;
Michael Kolb14612442011-06-24 13:06:29 -0700680 new AlertDialog.Builder(mContext).setTitle(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700681 R.string.browserFrameFormResubmitLabel).setMessage(
682 R.string.browserFrameFormResubmitMessage)
683 .setPositiveButton(R.string.ok,
684 new DialogInterface.OnClickListener() {
685 public void onClick(DialogInterface dialog,
686 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500687 if (mResend != null) {
688 mResend.sendToTarget();
689 mResend = null;
690 mDontResend = null;
691 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700692 }
693 }).setNegativeButton(R.string.cancel,
694 new DialogInterface.OnClickListener() {
695 public void onClick(DialogInterface dialog,
696 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500697 if (mDontResend != null) {
698 mDontResend.sendToTarget();
699 mResend = null;
700 mDontResend = null;
701 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700702 }
703 }).setOnCancelListener(new OnCancelListener() {
704 public void onCancel(DialogInterface dialog) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500705 if (mDontResend != null) {
706 mDontResend.sendToTarget();
707 mResend = null;
708 mDontResend = null;
709 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700710 }
711 }).show();
712 }
713
714 /**
715 * Insert the url into the visited history database.
716 * @param url The url to be inserted.
717 * @param isReload True if this url is being reloaded.
718 * FIXME: Not sure what to do when reloading the page.
719 */
720 @Override
721 public void doUpdateVisitedHistory(WebView view, String url,
722 boolean isReload) {
John Reck324d4402011-01-11 16:56:42 -0800723 mWebViewController.doUpdateVisitedHistory(Tab.this, isReload);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700724 }
725
726 /**
727 * Displays SSL error(s) dialog to the user.
728 */
729 @Override
730 public void onReceivedSslError(final WebView view,
731 final SslErrorHandler handler, final SslError error) {
732 if (!mInForeground) {
733 handler.cancel();
John Reck30c714c2010-12-16 17:30:34 -0800734 setLockIconType(LockIcon.LOCK_ICON_UNSECURE);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700735 return;
736 }
John Reck35e9dd62011-04-25 09:01:54 -0700737 if (mSettings.showSecurityWarnings()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700738 final LayoutInflater factory =
Michael Kolb14612442011-06-24 13:06:29 -0700739 LayoutInflater.from(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700740 final View warningsView =
741 factory.inflate(R.layout.ssl_warnings, null);
742 final LinearLayout placeholder =
743 (LinearLayout)warningsView.findViewById(R.id.placeholder);
744
745 if (error.hasError(SslError.SSL_UNTRUSTED)) {
746 LinearLayout ll = (LinearLayout)factory
747 .inflate(R.layout.ssl_warning, null);
748 ((TextView)ll.findViewById(R.id.warning))
749 .setText(R.string.ssl_untrusted);
750 placeholder.addView(ll);
751 }
752
753 if (error.hasError(SslError.SSL_IDMISMATCH)) {
754 LinearLayout ll = (LinearLayout)factory
755 .inflate(R.layout.ssl_warning, null);
756 ((TextView)ll.findViewById(R.id.warning))
757 .setText(R.string.ssl_mismatch);
758 placeholder.addView(ll);
759 }
760
761 if (error.hasError(SslError.SSL_EXPIRED)) {
762 LinearLayout ll = (LinearLayout)factory
763 .inflate(R.layout.ssl_warning, null);
764 ((TextView)ll.findViewById(R.id.warning))
765 .setText(R.string.ssl_expired);
766 placeholder.addView(ll);
767 }
768
769 if (error.hasError(SslError.SSL_NOTYETVALID)) {
770 LinearLayout ll = (LinearLayout)factory
771 .inflate(R.layout.ssl_warning, null);
772 ((TextView)ll.findViewById(R.id.warning))
773 .setText(R.string.ssl_not_yet_valid);
774 placeholder.addView(ll);
775 }
776
Michael Kolb14612442011-06-24 13:06:29 -0700777 new AlertDialog.Builder(mContext).setTitle(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700778 R.string.security_warning).setIcon(
779 android.R.drawable.ic_dialog_alert).setView(
780 warningsView).setPositiveButton(R.string.ssl_continue,
781 new DialogInterface.OnClickListener() {
782 public void onClick(DialogInterface dialog,
783 int whichButton) {
784 handler.proceed();
785 }
786 }).setNeutralButton(R.string.view_certificate,
787 new DialogInterface.OnClickListener() {
788 public void onClick(DialogInterface dialog,
789 int whichButton) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700790 mWebViewController.showSslCertificateOnError(view,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700791 handler, error);
792 }
Ben Murdocha49b8292010-11-16 11:56:04 +0000793 }).setNegativeButton(R.string.ssl_go_back,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700794 new DialogInterface.OnClickListener() {
795 public void onClick(DialogInterface dialog,
796 int whichButton) {
John Reck30c714c2010-12-16 17:30:34 -0800797 dialog.cancel();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700798 }
799 }).setOnCancelListener(
800 new DialogInterface.OnCancelListener() {
801 public void onCancel(DialogInterface dialog) {
802 handler.cancel();
John Reck30c714c2010-12-16 17:30:34 -0800803 setLockIconType(LockIcon.LOCK_ICON_UNSECURE);
804 mWebViewController.onUserCanceledSsl(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700805 }
806 }).show();
807 } else {
808 handler.proceed();
809 }
810 }
811
812 /**
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700813 * Displays client certificate request to the user.
814 */
815 @Override
816 public void onReceivedClientCertRequest(final WebView view,
817 final ClientCertRequestHandler handler, final String host_and_port) {
818 if (!mInForeground) {
819 handler.ignore();
820 return;
821 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700822 int colon = host_and_port.lastIndexOf(':');
823 String host;
824 int port;
825 if (colon == -1) {
826 host = host_and_port;
827 port = -1;
828 } else {
829 String portString = host_and_port.substring(colon + 1);
830 try {
831 port = Integer.parseInt(portString);
832 host = host_and_port.substring(0, colon);
833 } catch (NumberFormatException e) {
834 host = host_and_port;
835 port = -1;
836 }
837 }
Michael Kolb14612442011-06-24 13:06:29 -0700838 KeyChain.choosePrivateKeyAlias(
839 mWebViewController.getActivity(), new KeyChainAliasCallback() {
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700840 @Override public void alias(String alias) {
841 if (alias == null) {
842 handler.cancel();
843 return;
844 }
Michael Kolb14612442011-06-24 13:06:29 -0700845 new KeyChainLookup(mContext, handler, alias).execute();
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700846 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700847 }, null, null, host, port, null);
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700848 }
849
850 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700851 * Handles an HTTP authentication request.
852 *
853 * @param handler The authentication handler
854 * @param host The host
855 * @param realm The realm
856 */
857 @Override
858 public void onReceivedHttpAuthRequest(WebView view,
859 final HttpAuthHandler handler, final String host,
860 final String realm) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700861 mWebViewController.onReceivedHttpAuthRequest(Tab.this, view, handler, host, realm);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700862 }
863
864 @Override
John Reck438bf462011-01-12 18:11:46 -0800865 public WebResourceResponse shouldInterceptRequest(WebView view,
866 String url) {
867 WebResourceResponse res = HomeProvider.shouldInterceptRequest(
Michael Kolb14612442011-06-24 13:06:29 -0700868 mContext, url);
John Reck438bf462011-01-12 18:11:46 -0800869 return res;
870 }
871
872 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700873 public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
874 if (!mInForeground) {
875 return false;
876 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700877 return mWebViewController.shouldOverrideKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700878 }
879
880 @Override
881 public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700882 if (!mInForeground) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700883 return;
884 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700885 mWebViewController.onUnhandledKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700886 }
Patrick Scott92066772011-03-10 08:46:27 -0500887
888 @Override
889 public void onReceivedLoginRequest(WebView view, String realm,
890 String account, String args) {
Michael Kolb14612442011-06-24 13:06:29 -0700891 new DeviceAccountLogin(mWebViewController.getActivity(), view, Tab.this, mWebViewController)
Patrick Scott92066772011-03-10 08:46:27 -0500892 .handleLogin(realm, account, args);
893 }
894
Grace Kloba22ac16e2009-10-07 18:00:23 -0700895 };
896
John Reck1cf4b792011-07-26 10:22:22 -0700897 private void syncCurrentState(WebView view, String url) {
898 // Sync state (in case of stop/timeout)
899 mCurrentState.mUrl = view.getUrl();
900 if (mCurrentState.mUrl == null) {
901 mCurrentState.mUrl = url != null ? url : "";
902 }
903 mCurrentState.mOriginalUrl = view.getOriginalUrl();
904 mCurrentState.mTitle = view.getTitle();
905 mCurrentState.mFavicon = view.getFavicon();
906 if (!URLUtil.isHttpsUrl(mCurrentState.mUrl)) {
907 // In case we stop when loading an HTTPS page from an HTTP page
908 // but before a provisional load occurred
909 mCurrentState.mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
910 }
911 }
912
Patrick Scott92066772011-03-10 08:46:27 -0500913 // Called by DeviceAccountLogin when the Tab needs to have the auto-login UI
914 // displayed.
915 void setDeviceAccountLogin(DeviceAccountLogin login) {
916 mDeviceAccountLogin = login;
917 }
918
919 // Returns non-null if the title bar should display the auto-login UI.
920 DeviceAccountLogin getDeviceAccountLogin() {
921 return mDeviceAccountLogin;
922 }
923
Grace Kloba22ac16e2009-10-07 18:00:23 -0700924 // -------------------------------------------------------------------------
925 // WebChromeClient implementation for the main WebView
926 // -------------------------------------------------------------------------
927
928 private final WebChromeClient mWebChromeClient = new WebChromeClient() {
929 // Helper method to create a new tab or sub window.
930 private void createWindow(final boolean dialog, final Message msg) {
931 WebView.WebViewTransport transport =
932 (WebView.WebViewTransport) msg.obj;
933 if (dialog) {
934 createSubWindow();
Michael Kolb8233fac2010-10-26 16:08:53 -0700935 mWebViewController.attachSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700936 transport.setWebView(mSubView);
937 } else {
Michael Kolb7bcafde2011-05-09 13:55:59 -0700938 final Tab newTab = mWebViewController.openTab(null,
John Reck5949c662011-05-27 09:52:29 -0700939 Tab.this, true, true);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700940 transport.setWebView(newTab.getWebView());
941 }
942 msg.sendToTarget();
943 }
944
945 @Override
946 public boolean onCreateWindow(WebView view, final boolean dialog,
947 final boolean userGesture, final Message resultMsg) {
948 // only allow new window or sub window for the foreground case
949 if (!mInForeground) {
950 return false;
951 }
952 // Short-circuit if we can't create any more tabs or sub windows.
953 if (dialog && mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -0700954 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700955 .setTitle(R.string.too_many_subwindows_dialog_title)
956 .setIcon(android.R.drawable.ic_dialog_alert)
957 .setMessage(R.string.too_many_subwindows_dialog_message)
958 .setPositiveButton(R.string.ok, null)
959 .show();
960 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700961 } else if (!mWebViewController.getTabControl().canCreateNewTab()) {
Michael Kolb14612442011-06-24 13:06:29 -0700962 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700963 .setTitle(R.string.too_many_windows_dialog_title)
964 .setIcon(android.R.drawable.ic_dialog_alert)
965 .setMessage(R.string.too_many_windows_dialog_message)
966 .setPositiveButton(R.string.ok, null)
967 .show();
968 return false;
969 }
970
971 // Short-circuit if this was a user gesture.
972 if (userGesture) {
973 createWindow(dialog, resultMsg);
974 return true;
975 }
976
977 // Allow the popup and create the appropriate window.
978 final AlertDialog.OnClickListener allowListener =
979 new AlertDialog.OnClickListener() {
980 public void onClick(DialogInterface d,
981 int which) {
982 createWindow(dialog, resultMsg);
983 }
984 };
985
986 // Block the popup by returning a null WebView.
987 final AlertDialog.OnClickListener blockListener =
988 new AlertDialog.OnClickListener() {
989 public void onClick(DialogInterface d, int which) {
990 resultMsg.sendToTarget();
991 }
992 };
993
994 // Build a confirmation dialog to display to the user.
995 final AlertDialog d =
Michael Kolb14612442011-06-24 13:06:29 -0700996 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700997 .setTitle(R.string.attention)
998 .setIcon(android.R.drawable.ic_dialog_alert)
999 .setMessage(R.string.popup_window_attempt)
1000 .setPositiveButton(R.string.allow, allowListener)
1001 .setNegativeButton(R.string.block, blockListener)
1002 .setCancelable(false)
1003 .create();
1004
1005 // Show the confirmation dialog.
1006 d.show();
1007 return true;
1008 }
1009
1010 @Override
Patrick Scotteb5061b2009-11-18 15:00:30 -05001011 public void onRequestFocus(WebView view) {
1012 if (!mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -07001013 mWebViewController.switchToTab(Tab.this);
Patrick Scotteb5061b2009-11-18 15:00:30 -05001014 }
1015 }
1016
1017 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -07001018 public void onCloseWindow(WebView window) {
Michael Kolbc831b632011-05-11 09:30:34 -07001019 if (mParent != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001020 // JavaScript can only close popup window.
1021 if (mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -07001022 mWebViewController.switchToTab(mParent);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001023 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001024 mWebViewController.closeTab(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001025 }
1026 }
1027
1028 @Override
1029 public void onProgressChanged(WebView view, int newProgress) {
John Reck30c714c2010-12-16 17:30:34 -08001030 mPageLoadProgress = newProgress;
1031 mWebViewController.onProgressChanged(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001032 }
1033
1034 @Override
Leon Scroggins21d9b902010-03-11 09:33:11 -05001035 public void onReceivedTitle(WebView view, final String title) {
John Reck30c714c2010-12-16 17:30:34 -08001036 mCurrentState.mTitle = title;
Michael Kolb8233fac2010-10-26 16:08:53 -07001037 mWebViewController.onReceivedTitle(Tab.this, title);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001038 }
1039
1040 @Override
1041 public void onReceivedIcon(WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -08001042 mCurrentState.mFavicon = icon;
Michael Kolb8233fac2010-10-26 16:08:53 -07001043 mWebViewController.onFavicon(Tab.this, view, icon);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001044 }
1045
1046 @Override
1047 public void onReceivedTouchIconUrl(WebView view, String url,
1048 boolean precomposed) {
Michael Kolb14612442011-06-24 13:06:29 -07001049 final ContentResolver cr = mContext.getContentResolver();
Leon Scrogginsc8393d92010-04-23 14:58:16 -04001050 // Let precomposed icons take precedence over non-composed
1051 // icons.
1052 if (precomposed && mTouchIconLoader != null) {
1053 mTouchIconLoader.cancel(false);
1054 mTouchIconLoader = null;
1055 }
1056 // Have only one async task at a time.
1057 if (mTouchIconLoader == null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001058 mTouchIconLoader = new DownloadTouchIcon(Tab.this,
Michael Kolb14612442011-06-24 13:06:29 -07001059 mContext, cr, view);
Leon Scrogginsc8393d92010-04-23 14:58:16 -04001060 mTouchIconLoader.execute(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001061 }
1062 }
1063
1064 @Override
1065 public void onShowCustomView(View view,
1066 WebChromeClient.CustomViewCallback callback) {
Michael Kolb14612442011-06-24 13:06:29 -07001067 Activity activity = mWebViewController.getActivity();
1068 if (activity != null) {
1069 onShowCustomView(view, activity.getRequestedOrientation(), callback);
1070 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001071 }
1072
1073 @Override
1074 public void onShowCustomView(View view, int requestedOrientation,
1075 WebChromeClient.CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001076 if (mInForeground) mWebViewController.showCustomView(Tab.this, view,
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001077 requestedOrientation, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001078 }
1079
1080 @Override
1081 public void onHideCustomView() {
Michael Kolb8233fac2010-10-26 16:08:53 -07001082 if (mInForeground) mWebViewController.hideCustomView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001083 }
1084
1085 /**
1086 * The origin has exceeded its database quota.
1087 * @param url the URL that exceeded the quota
1088 * @param databaseIdentifier the identifier of the database on which the
1089 * transaction that caused the quota overflow was run
1090 * @param currentQuota the current quota for the origin.
1091 * @param estimatedSize the estimated size of the database.
1092 * @param totalUsedQuota is the sum of all origins' quota.
1093 * @param quotaUpdater The callback to run when a decision to allow or
1094 * deny quota has been made. Don't forget to call this!
1095 */
1096 @Override
1097 public void onExceededDatabaseQuota(String url,
1098 String databaseIdentifier, long currentQuota, long estimatedSize,
1099 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -07001100 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001101 .onExceededDatabaseQuota(url, databaseIdentifier,
1102 currentQuota, estimatedSize, totalUsedQuota,
1103 quotaUpdater);
1104 }
1105
1106 /**
1107 * The Application Cache has exceeded its max size.
1108 * @param spaceNeeded is the amount of disk space that would be needed
1109 * in order for the last appcache operation to succeed.
1110 * @param totalUsedQuota is the sum of all origins' quota.
1111 * @param quotaUpdater A callback to inform the WebCore thread that a
1112 * new app cache size is available. This callback must always
1113 * be executed at some point to ensure that the sleeping
1114 * WebCore thread is woken up.
1115 */
1116 @Override
1117 public void onReachedMaxAppCacheSize(long spaceNeeded,
1118 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -07001119 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001120 .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota,
1121 quotaUpdater);
1122 }
1123
1124 /**
1125 * Instructs the browser to show a prompt to ask the user to set the
1126 * Geolocation permission state for the specified origin.
1127 * @param origin The origin for which Geolocation permissions are
1128 * requested.
1129 * @param callback The callback to call once the user has set the
1130 * Geolocation permission state.
1131 */
1132 @Override
1133 public void onGeolocationPermissionsShowPrompt(String origin,
1134 GeolocationPermissions.Callback callback) {
1135 if (mInForeground) {
Grace Kloba50c241e2010-04-20 11:07:50 -07001136 getGeolocationPermissionsPrompt().show(origin, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001137 }
1138 }
1139
1140 /**
1141 * Instructs the browser to hide the Geolocation permissions prompt.
1142 */
1143 @Override
1144 public void onGeolocationPermissionsHidePrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001145 if (mInForeground && mGeolocationPermissionsPrompt != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001146 mGeolocationPermissionsPrompt.hide();
1147 }
1148 }
1149
Ben Murdoch65acc352009-11-19 18:16:04 +00001150 /* Adds a JavaScript error message to the system log and if the JS
1151 * console is enabled in the about:debug options, to that console
1152 * also.
Ben Murdochc42addf2010-01-28 15:19:59 +00001153 * @param consoleMessage the message object.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001154 */
1155 @Override
Ben Murdochc42addf2010-01-28 15:19:59 +00001156 public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001157 if (mInForeground) {
1158 // call getErrorConsole(true) so it will create one if needed
1159 ErrorConsoleView errorConsole = getErrorConsole(true);
Ben Murdochc42addf2010-01-28 15:19:59 +00001160 errorConsole.addErrorMessage(consoleMessage);
Michael Kolb8233fac2010-10-26 16:08:53 -07001161 if (mWebViewController.shouldShowErrorConsole()
1162 && errorConsole.getShowState() !=
1163 ErrorConsoleView.SHOW_MAXIMIZED) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001164 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
1165 }
1166 }
Ben Murdochc42addf2010-01-28 15:19:59 +00001167
Jeff Hamilton47654f42010-09-07 09:57:51 -05001168 // Don't log console messages in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001169 if (isPrivateBrowsingEnabled()) return true;
Jeff Hamilton47654f42010-09-07 09:57:51 -05001170
Ben Murdochc42addf2010-01-28 15:19:59 +00001171 String message = "Console: " + consoleMessage.message() + " "
1172 + consoleMessage.sourceId() + ":"
1173 + consoleMessage.lineNumber();
1174
1175 switch (consoleMessage.messageLevel()) {
1176 case TIP:
1177 Log.v(CONSOLE_LOGTAG, message);
1178 break;
1179 case LOG:
1180 Log.i(CONSOLE_LOGTAG, message);
1181 break;
1182 case WARNING:
1183 Log.w(CONSOLE_LOGTAG, message);
1184 break;
1185 case ERROR:
1186 Log.e(CONSOLE_LOGTAG, message);
1187 break;
1188 case DEBUG:
1189 Log.d(CONSOLE_LOGTAG, message);
1190 break;
1191 }
1192
1193 return true;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001194 }
1195
1196 /**
1197 * Ask the browser for an icon to represent a <video> element.
1198 * This icon will be used if the Web page did not specify a poster attribute.
1199 * @return Bitmap The icon or null if no such icon is available.
1200 */
1201 @Override
1202 public Bitmap getDefaultVideoPoster() {
1203 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001204 return mWebViewController.getDefaultVideoPoster();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001205 }
1206 return null;
1207 }
1208
1209 /**
1210 * Ask the host application for a custom progress view to show while
1211 * a <video> is loading.
1212 * @return View The progress view.
1213 */
1214 @Override
1215 public View getVideoLoadingProgressView() {
1216 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001217 return mWebViewController.getVideoLoadingProgressView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001218 }
1219 return null;
1220 }
1221
1222 @Override
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01001223 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001224 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001225 mWebViewController.openFileChooser(uploadMsg, acceptType);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001226 } else {
1227 uploadMsg.onReceiveValue(null);
1228 }
1229 }
1230
1231 /**
1232 * Deliver a list of already-visited URLs
1233 */
1234 @Override
1235 public void getVisitedHistory(final ValueCallback<String[]> callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001236 mWebViewController.getVisitedHistory(callback);
1237 }
Ben Murdoch8029a772010-11-16 11:58:21 +00001238
1239 @Override
1240 public void setupAutoFill(Message message) {
1241 // Prompt the user to set up their profile.
1242 final Message msg = message;
Michael Kolb14612442011-06-24 13:06:29 -07001243 AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
1244 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
Ben Murdoch1d676b62011-01-17 12:54:24 +00001245 Context.LAYOUT_INFLATER_SERVICE);
1246 final View layout = inflater.inflate(R.layout.setup_autofill_dialog, null);
1247
1248 builder.setView(layout)
Ben Murdochb7e6f942011-07-08 13:00:21 +01001249 .setTitle(R.string.autofill_setup_dialog_title)
Ben Murdoch1d676b62011-01-17 12:54:24 +00001250 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1251 @Override
1252 public void onClick(DialogInterface dialog, int id) {
1253 CheckBox disableAutoFill = (CheckBox) layout.findViewById(
1254 R.id.setup_autofill_dialog_disable_autofill);
1255
1256 if (disableAutoFill.isChecked()) {
1257 // Disable autofill and show a toast with how to turn it on again.
John Reck35e9dd62011-04-25 09:01:54 -07001258 mSettings.setAutofillEnabled(false);
Michael Kolb14612442011-06-24 13:06:29 -07001259 Toast.makeText(mContext,
Ben Murdoch1d676b62011-01-17 12:54:24 +00001260 R.string.autofill_setup_dialog_negative_toast,
1261 Toast.LENGTH_LONG).show();
1262 } else {
1263 // Take user to the AutoFill profile editor. When they return,
1264 // we will send the message that we pass here which will trigger
1265 // the form to get filled out with their new profile.
1266 mWebViewController.setupAutoFill(msg);
1267 }
1268 }
1269 })
1270 .setNegativeButton(R.string.cancel, null)
1271 .show();
Ben Murdoch8029a772010-11-16 11:58:21 +00001272 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001273 };
1274
1275 // -------------------------------------------------------------------------
1276 // WebViewClient implementation for the sub window
1277 // -------------------------------------------------------------------------
1278
1279 // Subclass of WebViewClient used in subwindows to notify the main
1280 // WebViewClient of certain WebView activities.
1281 private static class SubWindowClient extends WebViewClient {
1282 // The main WebViewClient.
1283 private final WebViewClient mClient;
Michael Kolb8233fac2010-10-26 16:08:53 -07001284 private final WebViewController mController;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001285
Michael Kolb8233fac2010-10-26 16:08:53 -07001286 SubWindowClient(WebViewClient client, WebViewController controller) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001287 mClient = client;
Michael Kolb8233fac2010-10-26 16:08:53 -07001288 mController = controller;
Leon Scroggins III211ba542010-04-19 13:21:13 -04001289 }
1290 @Override
1291 public void onPageStarted(WebView view, String url, Bitmap favicon) {
1292 // Unlike the others, do not call mClient's version, which would
1293 // change the progress bar. However, we do want to remove the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001294 // find or select dialog.
Michael Kolb8233fac2010-10-26 16:08:53 -07001295 mController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001296 }
1297 @Override
1298 public void doUpdateVisitedHistory(WebView view, String url,
1299 boolean isReload) {
1300 mClient.doUpdateVisitedHistory(view, url, isReload);
1301 }
1302 @Override
1303 public boolean shouldOverrideUrlLoading(WebView view, String url) {
1304 return mClient.shouldOverrideUrlLoading(view, url);
1305 }
1306 @Override
1307 public void onReceivedSslError(WebView view, SslErrorHandler handler,
1308 SslError error) {
1309 mClient.onReceivedSslError(view, handler, error);
1310 }
1311 @Override
Brian Carlstrom8862c1d2011-06-02 01:05:55 -07001312 public void onReceivedClientCertRequest(WebView view,
1313 ClientCertRequestHandler handler, String host_and_port) {
1314 mClient.onReceivedClientCertRequest(view, handler, host_and_port);
1315 }
1316 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -07001317 public void onReceivedHttpAuthRequest(WebView view,
1318 HttpAuthHandler handler, String host, String realm) {
1319 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
1320 }
1321 @Override
1322 public void onFormResubmission(WebView view, Message dontResend,
1323 Message resend) {
1324 mClient.onFormResubmission(view, dontResend, resend);
1325 }
1326 @Override
1327 public void onReceivedError(WebView view, int errorCode,
1328 String description, String failingUrl) {
1329 mClient.onReceivedError(view, errorCode, description, failingUrl);
1330 }
1331 @Override
1332 public boolean shouldOverrideKeyEvent(WebView view,
1333 android.view.KeyEvent event) {
1334 return mClient.shouldOverrideKeyEvent(view, event);
1335 }
1336 @Override
1337 public void onUnhandledKeyEvent(WebView view,
1338 android.view.KeyEvent event) {
1339 mClient.onUnhandledKeyEvent(view, event);
1340 }
1341 }
1342
1343 // -------------------------------------------------------------------------
1344 // WebChromeClient implementation for the sub window
1345 // -------------------------------------------------------------------------
1346
1347 private class SubWindowChromeClient extends WebChromeClient {
1348 // The main WebChromeClient.
1349 private final WebChromeClient mClient;
1350
1351 SubWindowChromeClient(WebChromeClient client) {
1352 mClient = client;
1353 }
1354 @Override
1355 public void onProgressChanged(WebView view, int newProgress) {
1356 mClient.onProgressChanged(view, newProgress);
1357 }
1358 @Override
1359 public boolean onCreateWindow(WebView view, boolean dialog,
1360 boolean userGesture, android.os.Message resultMsg) {
1361 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
1362 }
1363 @Override
1364 public void onCloseWindow(WebView window) {
1365 if (window != mSubView) {
1366 Log.e(LOGTAG, "Can't close the window");
1367 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001368 mWebViewController.dismissSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001369 }
1370 }
1371
1372 // -------------------------------------------------------------------------
1373
1374 // Construct a new tab
Michael Kolb7bcafde2011-05-09 13:55:59 -07001375 Tab(WebViewController wvcontroller, WebView w) {
John Reck1cf4b792011-07-26 10:22:22 -07001376 this(wvcontroller, w, null);
1377 }
1378
1379 Tab(WebViewController wvcontroller, Bundle state) {
1380 this(wvcontroller, null, state);
1381 }
1382
1383 Tab(WebViewController wvcontroller, WebView w, Bundle state) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001384 mWebViewController = wvcontroller;
Michael Kolb14612442011-06-24 13:06:29 -07001385 mContext = mWebViewController.getContext();
John Reck35e9dd62011-04-25 09:01:54 -07001386 mSettings = BrowserSettings.getInstance();
Michael Kolb14612442011-06-24 13:06:29 -07001387 mDataController = DataController.getInstance(mContext);
1388 mCurrentState = new PageState(mContext, w != null
John Reck847b5322011-04-14 17:02:18 -07001389 ? w.isPrivateBrowsingEnabled() : false);
Michael Kolb8233fac2010-10-26 16:08:53 -07001390 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001391 mInForeground = false;
1392
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001393 mDownloadListener = new DownloadListener() {
1394 public void onDownloadStart(String url, String userAgent,
1395 String contentDisposition, String mimetype,
1396 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001397 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001398 mimetype, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001399 }
1400 };
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001401 mWebBackForwardListClient = new WebBackForwardListClient() {
1402 @Override
1403 public void onNewHistoryItem(WebHistoryItem item) {
1404 if (isInVoiceSearchMode()) {
1405 item.setCustomData(mVoiceSearchData.mVoiceSearchIntent);
1406 }
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001407 if (mClearHistoryMatchUrl != null) {
1408 if (LOGD_ENABLED) {
1409 Log.d(LOGTAG, "onNewHistoryItem:\n\t" + item.getUrl() + "\n\t"
1410 + mClearHistoryMatchUrl);
1411 }
1412 if (TextUtils.equals(item.getOriginalUrl(), mClearHistoryMatchUrl)) {
1413 if (mMainView != null) {
1414 mMainView.clearHistory();
1415 }
1416 }
1417 mClearHistoryMatchUrl = null;
1418 }
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001419 }
1420 @Override
1421 public void onIndexChanged(WebHistoryItem item, int index) {
1422 Object data = item.getCustomData();
1423 if (data != null && data instanceof Intent) {
1424 activateVoiceSearchMode((Intent) data);
1425 }
1426 }
1427 };
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001428
John Reck1cf4b792011-07-26 10:22:22 -07001429 mCaptureWidth = mContext.getResources().getDimensionPixelSize(
1430 R.dimen.tab_thumbnail_width);
1431 mCaptureHeight = mContext.getResources().getDimensionPixelSize(
1432 R.dimen.tab_thumbnail_height);
1433 updateShouldCaptureThumbnails();
1434 restoreState(state);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001435 setWebView(w);
Michael Kolb9ef259a2011-07-12 15:33:08 -07001436 mHandler = new Handler() {
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001437 @Override
Michael Kolb9ef259a2011-07-12 15:33:08 -07001438 public void handleMessage(Message m) {
John Reck1cf4b792011-07-26 10:22:22 -07001439 switch (m.what) {
1440 case MSG_CAPTURE:
1441 capture();
1442 break;
1443 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001444 }
1445 };
John Reck1cf4b792011-07-26 10:22:22 -07001446 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001447
John Reck1cf4b792011-07-26 10:22:22 -07001448 public void updateShouldCaptureThumbnails() {
1449 if (mWebViewController.shouldCaptureThumbnails()) {
1450 synchronized (Tab.this) {
1451 if (mCapture == null) {
1452 mCapture = Bitmap.createBitmap(mCaptureWidth, mCaptureHeight,
1453 Bitmap.Config.RGB_565);
1454 if (mInForeground) {
1455 postCapture();
1456 }
1457 }
1458 }
1459 } else {
1460 synchronized (Tab.this) {
1461 mCapture = null;
1462 deleteThumbnail();
1463 }
1464 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001465 }
1466
Michael Kolb14612442011-06-24 13:06:29 -07001467 public void setController(WebViewController ctl) {
1468 mWebViewController = ctl;
John Reck1cf4b792011-07-26 10:22:22 -07001469 updateShouldCaptureThumbnails();
Michael Kolb14612442011-06-24 13:06:29 -07001470 }
1471
Michael Kolbc831b632011-05-11 09:30:34 -07001472 public void setId(long id) {
1473 mId = id;
1474 }
1475
1476 public long getId() {
1477 return mId;
1478 }
1479
Grace Kloba22ac16e2009-10-07 18:00:23 -07001480 /**
1481 * Sets the WebView for this tab, correctly removing the old WebView from
1482 * the container view.
1483 */
1484 void setWebView(WebView w) {
1485 if (mMainView == w) {
1486 return;
1487 }
Michael Kolba713ec82010-11-29 17:27:06 -08001488
Grace Kloba22ac16e2009-10-07 18:00:23 -07001489 // If the WebView is changing, the page will be reloaded, so any ongoing
1490 // Geolocation permission requests are void.
Grace Kloba50c241e2010-04-20 11:07:50 -07001491 if (mGeolocationPermissionsPrompt != null) {
1492 mGeolocationPermissionsPrompt.hide();
1493 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001494
Michael Kolba713ec82010-11-29 17:27:06 -08001495 mWebViewController.onSetWebView(this, w);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001496
John Reck1cf4b792011-07-26 10:22:22 -07001497 if (mMainView != null) {
1498 if (w != null) {
1499 syncCurrentState(w, null);
1500 } else {
1501 mCurrentState = new PageState(mContext, false);
1502 }
1503 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001504 // set the new one
1505 mMainView = w;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001506 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001507 if (mMainView != null) {
1508 mMainView.setWebViewClient(mWebViewClient);
1509 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001510 // Attach DownloadManager so that downloads can start in an active
1511 // or a non-active window. This can happen when going to a site that
1512 // does a redirect after a period of time. The user could have
1513 // switched to another tab while waiting for the download to start.
1514 mMainView.setDownloadListener(mDownloadListener);
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001515 mMainView.setWebBackForwardListClient(mWebBackForwardListClient);
Michael Kolb9ef259a2011-07-12 15:33:08 -07001516 mMainView.setPictureListener(this);
John Reck1cf4b792011-07-26 10:22:22 -07001517 if (mSavedState != null) {
1518 mMainView.restoreState(mSavedState);
1519 mSavedState = null;
1520 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001521 }
1522 }
1523
1524 /**
1525 * Destroy the tab's main WebView and subWindow if any
1526 */
1527 void destroy() {
1528 if (mMainView != null) {
1529 dismissSubWindow();
John Reckef654f12011-07-12 16:42:08 -07001530 // Make sure the embedded title bar isn't still attached
1531 mMainView.setEmbeddedTitleBar(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001532 // save the WebView to call destroy() after detach it from the tab
1533 WebView webView = mMainView;
1534 setWebView(null);
1535 webView.destroy();
1536 }
1537 }
1538
1539 /**
1540 * Remove the tab from the parent
1541 */
1542 void removeFromTree() {
1543 // detach the children
Michael Kolbc831b632011-05-11 09:30:34 -07001544 if (mChildren != null) {
1545 for(Tab t : mChildren) {
1546 t.setParent(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001547 }
1548 }
1549 // remove itself from the parent list
Michael Kolbc831b632011-05-11 09:30:34 -07001550 if (mParent != null) {
1551 mParent.mChildren.remove(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001552 }
John Reck1cf4b792011-07-26 10:22:22 -07001553 deleteThumbnail();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001554 }
1555
1556 /**
1557 * Create a new subwindow unless a subwindow already exists.
1558 * @return True if a new subwindow was created. False if one already exists.
1559 */
1560 boolean createSubWindow() {
1561 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001562 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001563 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001564 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001565 mSubView.setWebChromeClient(new SubWindowChromeClient(
1566 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001567 // Set a different DownloadListener for the mSubView, since it will
1568 // just need to dismiss the mSubView, rather than close the Tab
1569 mSubView.setDownloadListener(new DownloadListener() {
1570 public void onDownloadStart(String url, String userAgent,
1571 String contentDisposition, String mimetype,
1572 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001573 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001574 contentDisposition, mimetype, contentLength);
1575 if (mSubView.copyBackForwardList().getSize() == 0) {
1576 // This subwindow was opened for the sole purpose of
1577 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001578 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001579 }
1580 }
1581 });
Michael Kolb14612442011-06-24 13:06:29 -07001582 mSubView.setOnCreateContextMenuListener(mWebViewController.getActivity());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001583 return true;
1584 }
1585 return false;
1586 }
1587
1588 /**
1589 * Dismiss the subWindow for the tab.
1590 */
1591 void dismissSubWindow() {
1592 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001593 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001594 mSubView.destroy();
1595 mSubView = null;
1596 mSubViewContainer = null;
1597 }
1598 }
1599
Grace Kloba22ac16e2009-10-07 18:00:23 -07001600
1601 /**
1602 * Set the parent tab of this tab.
1603 */
Michael Kolbc831b632011-05-11 09:30:34 -07001604 void setParent(Tab parent) {
1605 mParent = parent;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001606 // This tab may have been freed due to low memory. If that is the case,
Michael Kolbc831b632011-05-11 09:30:34 -07001607 // the parent tab id is already saved. If we are changing that id
Grace Kloba22ac16e2009-10-07 18:00:23 -07001608 // (most likely due to removing the parent tab) we must update the
Michael Kolbc831b632011-05-11 09:30:34 -07001609 // parent tab id in the saved Bundle.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001610 if (mSavedState != null) {
1611 if (parent == null) {
1612 mSavedState.remove(PARENTTAB);
1613 } else {
Michael Kolbc831b632011-05-11 09:30:34 -07001614 mSavedState.putLong(PARENTTAB, parent.getId());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001615 }
1616 }
John Reckb0a86db2011-05-24 14:05:58 -07001617
1618 // Sync the WebView useragent with the parent
1619 if (parent != null && mSettings.hasDesktopUseragent(parent.getWebView())
1620 != mSettings.hasDesktopUseragent(getWebView())) {
1621 mSettings.toggleDesktopUseragent(getWebView());
1622 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001623 }
1624
1625 /**
Michael Kolbc831b632011-05-11 09:30:34 -07001626 * If this Tab was created through another Tab, then this method returns
1627 * that Tab.
1628 * @return the Tab parent or null
1629 */
1630 public Tab getParent() {
1631 return mParent;
1632 }
1633
1634 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001635 * When a Tab is created through the content of another Tab, then we
1636 * associate the Tabs.
1637 * @param child the Tab that was created from this Tab
1638 */
1639 void addChildTab(Tab child) {
Michael Kolbc831b632011-05-11 09:30:34 -07001640 if (mChildren == null) {
1641 mChildren = new Vector<Tab>();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001642 }
Michael Kolbc831b632011-05-11 09:30:34 -07001643 mChildren.add(child);
1644 child.setParent(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001645 }
1646
Michael Kolbc831b632011-05-11 09:30:34 -07001647 Vector<Tab> getChildren() {
1648 return mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001649 }
1650
1651 void resume() {
1652 if (mMainView != null) {
1653 mMainView.onResume();
1654 if (mSubView != null) {
1655 mSubView.onResume();
1656 }
1657 }
1658 }
1659
1660 void pause() {
1661 if (mMainView != null) {
1662 mMainView.onPause();
1663 if (mSubView != null) {
1664 mSubView.onPause();
1665 }
1666 }
1667 }
1668
1669 void putInForeground() {
1670 mInForeground = true;
1671 resume();
Michael Kolb14612442011-06-24 13:06:29 -07001672 Activity activity = mWebViewController.getActivity();
1673 mMainView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001674 if (mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -07001675 mSubView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001676 }
1677 // Show the pending error dialog if the queue is not empty
1678 if (mQueuedErrors != null && mQueuedErrors.size() > 0) {
1679 showError(mQueuedErrors.getFirst());
1680 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001681 mWebViewController.bookmarkedStatusHasChanged(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001682 }
1683
1684 void putInBackground() {
1685 mInForeground = false;
1686 pause();
1687 mMainView.setOnCreateContextMenuListener(null);
1688 if (mSubView != null) {
1689 mSubView.setOnCreateContextMenuListener(null);
1690 }
1691 }
1692
Michael Kolb8233fac2010-10-26 16:08:53 -07001693 boolean inForeground() {
1694 return mInForeground;
1695 }
1696
Grace Kloba22ac16e2009-10-07 18:00:23 -07001697 /**
1698 * Return the top window of this tab; either the subwindow if it is not
1699 * null or the main window.
1700 * @return The top window of this tab.
1701 */
1702 WebView getTopWindow() {
1703 if (mSubView != null) {
1704 return mSubView;
1705 }
1706 return mMainView;
1707 }
1708
1709 /**
1710 * Return the main window of this tab. Note: if a tab is freed in the
1711 * background, this can return null. It is only guaranteed to be
1712 * non-null for the current tab.
1713 * @return The main WebView of this tab.
1714 */
1715 WebView getWebView() {
1716 return mMainView;
1717 }
1718
Michael Kolba713ec82010-11-29 17:27:06 -08001719 void setViewContainer(View container) {
1720 mContainer = container;
1721 }
1722
Michael Kolb8233fac2010-10-26 16:08:53 -07001723 View getViewContainer() {
1724 return mContainer;
1725 }
1726
Grace Kloba22ac16e2009-10-07 18:00:23 -07001727 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001728 * Return whether private browsing is enabled for the main window of
1729 * this tab.
1730 * @return True if private browsing is enabled.
1731 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001732 boolean isPrivateBrowsingEnabled() {
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001733 WebView webView = getWebView();
1734 if (webView == null) {
1735 return false;
1736 }
1737 return webView.isPrivateBrowsingEnabled();
1738 }
1739
1740 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001741 * Return the subwindow of this tab or null if there is no subwindow.
1742 * @return The subwindow of this tab or null.
1743 */
1744 WebView getSubWebView() {
1745 return mSubView;
1746 }
1747
Michael Kolb1514bb72010-11-22 09:11:48 -08001748 void setSubWebView(WebView subView) {
1749 mSubView = subView;
1750 }
1751
Michael Kolb8233fac2010-10-26 16:08:53 -07001752 View getSubViewContainer() {
1753 return mSubViewContainer;
1754 }
1755
Michael Kolb1514bb72010-11-22 09:11:48 -08001756 void setSubViewContainer(View subViewContainer) {
1757 mSubViewContainer = subViewContainer;
1758 }
1759
Grace Kloba22ac16e2009-10-07 18:00:23 -07001760 /**
1761 * @return The geolocation permissions prompt for this tab.
1762 */
1763 GeolocationPermissionsPrompt getGeolocationPermissionsPrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001764 if (mGeolocationPermissionsPrompt == null) {
1765 ViewStub stub = (ViewStub) mContainer
1766 .findViewById(R.id.geolocation_permissions_prompt);
1767 mGeolocationPermissionsPrompt = (GeolocationPermissionsPrompt) stub
1768 .inflate();
Grace Kloba50c241e2010-04-20 11:07:50 -07001769 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001770 return mGeolocationPermissionsPrompt;
1771 }
1772
1773 /**
1774 * @return The application id string
1775 */
1776 String getAppId() {
1777 return mAppId;
1778 }
1779
1780 /**
1781 * Set the application id string
1782 * @param id
1783 */
1784 void setAppId(String id) {
1785 mAppId = id;
1786 }
1787
Grace Kloba22ac16e2009-10-07 18:00:23 -07001788 String getUrl() {
John Reck324d4402011-01-11 16:56:42 -08001789 return UrlUtils.filteredUrl(mCurrentState.mUrl);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001790 }
1791
John Reck49a603c2011-03-03 09:33:05 -08001792 String getOriginalUrl() {
John Reckdb22ec42011-06-29 11:31:24 -07001793 if (mCurrentState.mOriginalUrl == null) {
1794 return getUrl();
John Reck49a603c2011-03-03 09:33:05 -08001795 }
John Reckdb22ec42011-06-29 11:31:24 -07001796 return UrlUtils.filteredUrl(mCurrentState.mOriginalUrl);
John Reck49a603c2011-03-03 09:33:05 -08001797 }
1798
Grace Kloba22ac16e2009-10-07 18:00:23 -07001799 /**
John Reck30c714c2010-12-16 17:30:34 -08001800 * Get the title of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001801 */
1802 String getTitle() {
John Reck30c714c2010-12-16 17:30:34 -08001803 if (mCurrentState.mTitle == null && mInPageLoad) {
Michael Kolb14612442011-06-24 13:06:29 -07001804 return mContext.getString(R.string.title_bar_loading);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001805 }
John Reck30c714c2010-12-16 17:30:34 -08001806 return mCurrentState.mTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001807 }
1808
1809 /**
John Reck30c714c2010-12-16 17:30:34 -08001810 * Get the favicon of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001811 */
1812 Bitmap getFavicon() {
John Reck1cf4b792011-07-26 10:22:22 -07001813 if (mCurrentState.mFavicon != null) {
1814 return mCurrentState.mFavicon;
1815 }
1816 return getDefaultFavicon(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001817 }
1818
John Recke969cc52010-12-21 17:24:43 -08001819 public boolean isBookmarkedSite() {
1820 return mCurrentState.mIsBookmarkedSite;
1821 }
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001822
Grace Kloba22ac16e2009-10-07 18:00:23 -07001823 /**
1824 * Return the tab's error console. Creates the console if createIfNEcessary
1825 * is true and we haven't already created the console.
1826 * @param createIfNecessary Flag to indicate if the console should be
1827 * created if it has not been already.
1828 * @return The tab's error console, or null if one has not been created and
1829 * createIfNecessary is false.
1830 */
1831 ErrorConsoleView getErrorConsole(boolean createIfNecessary) {
1832 if (createIfNecessary && mErrorConsole == null) {
Michael Kolb14612442011-06-24 13:06:29 -07001833 mErrorConsole = new ErrorConsoleView(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001834 mErrorConsole.setWebView(mMainView);
1835 }
1836 return mErrorConsole;
1837 }
1838
John Reck30c714c2010-12-16 17:30:34 -08001839 private void setLockIconType(LockIcon icon) {
1840 mCurrentState.mLockIcon = icon;
1841 mWebViewController.onUpdatedLockIcon(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001842 }
1843
1844 /**
1845 * @return The tab's lock icon type.
1846 */
John Reck30c714c2010-12-16 17:30:34 -08001847 LockIcon getLockIconType() {
1848 return mCurrentState.mLockIcon;
1849 }
1850
1851 int getLoadProgress() {
1852 if (mInPageLoad) {
1853 return mPageLoadProgress;
1854 }
1855 return 100;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001856 }
1857
1858 /**
1859 * @return TRUE if onPageStarted is called while onPageFinished is not
1860 * called yet.
1861 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001862 boolean inPageLoad() {
1863 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001864 }
1865
1866 // force mInLoad to be false. This should only be called before closing the
1867 // tab to ensure BrowserActivity's pauseWebViewTimers() is called correctly.
Michael Kolb8233fac2010-10-26 16:08:53 -07001868 void clearInPageLoad() {
1869 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001870 }
1871
Grace Kloba22ac16e2009-10-07 18:00:23 -07001872 /**
John Reck1cf4b792011-07-26 10:22:22 -07001873 * @return The Bundle with the tab's state if it can be saved, otherwise null
Grace Kloba22ac16e2009-10-07 18:00:23 -07001874 */
John Reck1cf4b792011-07-26 10:22:22 -07001875 public Bundle saveState() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001876 // If the WebView is null it means we ran low on memory and we already
1877 // stored the saved state in mSavedState.
1878 if (mMainView == null) {
John Reck1cf4b792011-07-26 10:22:22 -07001879 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001880 }
John Reck24f18262011-06-17 14:47:20 -07001881 // If the tab is the homepage or has no URL, don't save it
1882 String homepage = BrowserSettings.getInstance().getHomePage();
1883 if (TextUtils.equals(homepage, mCurrentState.mUrl)
1884 || TextUtils.isEmpty(mCurrentState.mUrl)) {
John Reck1cf4b792011-07-26 10:22:22 -07001885 return null;
John Reck24f18262011-06-17 14:47:20 -07001886 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001887
1888 mSavedState = new Bundle();
John Reck541f55a2011-06-07 16:34:43 -07001889 mMainView.saveState(mSavedState);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001890
Michael Kolbc831b632011-05-11 09:30:34 -07001891 mSavedState.putLong(ID, mId);
John Reck30c714c2010-12-16 17:30:34 -08001892 mSavedState.putString(CURRURL, mCurrentState.mUrl);
1893 mSavedState.putString(CURRTITLE, mCurrentState.mTitle);
John Reck1cf4b792011-07-26 10:22:22 -07001894 mSavedState.putBoolean(INCOGNITO, mMainView.isPrivateBrowsingEnabled());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001895 if (mAppId != null) {
1896 mSavedState.putString(APPID, mAppId);
1897 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001898 // Remember the parent tab so the relationship can be restored.
Michael Kolbc831b632011-05-11 09:30:34 -07001899 if (mParent != null) {
1900 mSavedState.putLong(PARENTTAB, mParent.mId);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001901 }
John Reckb0a86db2011-05-24 14:05:58 -07001902 mSavedState.putBoolean(USERAGENT,
1903 mSettings.hasDesktopUseragent(getWebView()));
John Reck1cf4b792011-07-26 10:22:22 -07001904 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001905 }
1906
1907 /*
1908 * Restore the state of the tab.
1909 */
John Reck1cf4b792011-07-26 10:22:22 -07001910 private void restoreState(Bundle b) {
1911 mSavedState = b;
1912 if (mSavedState == null) {
1913 return;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001914 }
1915 // Restore the internal state even if the WebView fails to restore.
1916 // This will maintain the app id, original url and close-on-exit values.
Michael Kolbc831b632011-05-11 09:30:34 -07001917 mId = b.getLong(ID);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001918 mAppId = b.getString(APPID);
John Reckb0a86db2011-05-24 14:05:58 -07001919 if (b.getBoolean(USERAGENT)
1920 != mSettings.hasDesktopUseragent(getWebView())) {
1921 mSettings.toggleDesktopUseragent(getWebView());
1922 }
John Reck1cf4b792011-07-26 10:22:22 -07001923 String url = b.getString(CURRURL);
1924 String title = b.getString(CURRTITLE);
1925 boolean incognito = b.getBoolean(INCOGNITO);
1926 mCurrentState = new PageState(mContext, incognito, url, null);
1927 mCurrentState.mTitle = title;
1928 synchronized (Tab.this) {
1929 if (mCapture != null) {
1930 BackgroundHandler.execute(mLoadThumbnail);
1931 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001932 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001933 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04001934
Leon Scroggins1961ed22010-12-07 15:22:21 -05001935 public void updateBookmarkedStatus() {
John Recke969cc52010-12-21 17:24:43 -08001936 mDataController.queryBookmarkStatus(getUrl(), mIsBookmarkCallback);
Leon Scroggins1961ed22010-12-07 15:22:21 -05001937 }
1938
John Recke969cc52010-12-21 17:24:43 -08001939 private DataController.OnQueryUrlIsBookmark mIsBookmarkCallback
1940 = new DataController.OnQueryUrlIsBookmark() {
1941 @Override
1942 public void onQueryUrlIsBookmark(String url, boolean isBookmark) {
1943 if (mCurrentState.mUrl.equals(url)) {
1944 mCurrentState.mIsBookmarkedSite = isBookmark;
1945 mWebViewController.bookmarkedStatusHasChanged(Tab.this);
1946 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001947 }
John Recke969cc52010-12-21 17:24:43 -08001948 };
Michael Kolb1acef692011-03-08 14:12:06 -08001949
Michael Kolbeb95db42011-03-03 10:38:40 -08001950 public Bitmap getScreenshot() {
John Reck1cf4b792011-07-26 10:22:22 -07001951 synchronized (Tab.this) {
1952 return mCapture;
1953 }
Michael Kolbeb95db42011-03-03 10:38:40 -08001954 }
1955
John Reck541f55a2011-06-07 16:34:43 -07001956 public boolean isSnapshot() {
John Reck541f55a2011-06-07 16:34:43 -07001957 return false;
1958 }
1959
John Reckd8c74522011-06-14 08:45:00 -07001960 public ContentValues createSnapshotValues() {
1961 if (mMainView == null) return null;
John Reck8cc92352011-07-06 17:41:52 -07001962 ByteArrayOutputStream bos = new ByteArrayOutputStream();
1963 try {
1964 GZIPOutputStream stream = new GZIPOutputStream(bos);
1965 if (!mMainView.saveViewState(stream)) {
1966 return null;
1967 }
1968 stream.flush();
1969 stream.close();
1970 } catch (Exception e) {
1971 Log.w(LOGTAG, "Failed to save view state", e);
John Reck541f55a2011-06-07 16:34:43 -07001972 return null;
1973 }
John Reck8cc92352011-07-06 17:41:52 -07001974 byte[] data = bos.toByteArray();
John Reckd8c74522011-06-14 08:45:00 -07001975 ContentValues values = new ContentValues();
1976 values.put(Snapshots.TITLE, mCurrentState.mTitle);
1977 values.put(Snapshots.URL, mCurrentState.mUrl);
1978 values.put(Snapshots.VIEWSTATE, data);
1979 values.put(Snapshots.BACKGROUND, mMainView.getPageBackgroundColor());
John Reck8cc92352011-07-06 17:41:52 -07001980 values.put(Snapshots.DATE_CREATED, System.currentTimeMillis());
1981 values.put(Snapshots.FAVICON, compressBitmap(getFavicon()));
1982 Bitmap screenshot = Controller.createScreenshot(mMainView,
1983 Controller.getDesiredThumbnailWidth(mContext),
1984 Controller.getDesiredThumbnailHeight(mContext));
1985 values.put(Snapshots.THUMBNAIL, compressBitmap(screenshot));
John Reckd8c74522011-06-14 08:45:00 -07001986 return values;
John Reck541f55a2011-06-07 16:34:43 -07001987 }
1988
John Reck8cc92352011-07-06 17:41:52 -07001989 public byte[] compressBitmap(Bitmap bitmap) {
1990 if (bitmap == null) {
1991 return null;
1992 }
1993 ByteArrayOutputStream stream = new ByteArrayOutputStream();
1994 bitmap.compress(CompressFormat.PNG, 100, stream);
1995 return stream.toByteArray();
1996 }
1997
John Reck26b18322011-06-21 13:08:58 -07001998 public void loadUrl(String url, Map<String, String> headers) {
1999 if (mMainView != null) {
Michael Kolb14612442011-06-24 13:06:29 -07002000 mCurrentState = new PageState(mContext, false, url, null);
John Reck26b18322011-06-21 13:08:58 -07002001 mWebViewController.onPageStarted(this, mMainView, null);
2002 mMainView.loadUrl(url, headers);
2003 }
2004 }
2005
Michael Kolb9ef259a2011-07-12 15:33:08 -07002006 protected void capture() {
2007 if (mMainView == null || mCapture == null) return;
2008 Canvas c = new Canvas(mCapture);
2009 final int left = mMainView.getScrollX();
2010 final int top = mMainView.getScrollY() + mMainView.getVisibleTitleHeight();
2011 c.translate(-left, -top);
2012 float scale = mCaptureWidth / (float) mMainView.getWidth();
2013 c.scale(scale, scale, left, top);
2014 mMainView.draw(c);
Dianne Hackborn43cfe8a2011-08-02 16:59:35 -07002015 c.setBitmap(null);
John Reck1cf4b792011-07-26 10:22:22 -07002016 persistThumbnail();
Michael Kolb9ef259a2011-07-12 15:33:08 -07002017 }
2018
2019 @Override
2020 public void onNewPicture(WebView view, Picture picture) {
2021 //update screenshot
John Reck1cf4b792011-07-26 10:22:22 -07002022 postCapture();
2023 }
2024
2025 private void postCapture() {
Michael Kolb9ef259a2011-07-12 15:33:08 -07002026 if (!mHandler.hasMessages(MSG_CAPTURE)) {
2027 mHandler.sendEmptyMessageDelayed(MSG_CAPTURE, CAPTURE_DELAY);
2028 }
2029 }
2030
John Reckef654f12011-07-12 16:42:08 -07002031 public boolean canGoBack() {
2032 return mMainView != null ? mMainView.canGoBack() : false;
2033 }
2034
2035 public boolean canGoForward() {
2036 return mMainView != null ? mMainView.canGoForward() : false;
2037 }
2038
2039 public void goBack() {
2040 if (mMainView != null) {
2041 mMainView.goBack();
2042 }
2043 }
2044
2045 public void goForward() {
2046 if (mMainView != null) {
2047 mMainView.goForward();
2048 }
2049 }
2050
Mathew Inwood1dd8e822011-08-03 14:34:29 +01002051 /**
2052 * Causes the tab back/forward stack to be cleared once, if the given URL is the next URL
2053 * to be added to the stack.
2054 *
2055 * This is used to ensure that preloaded URLs that are not subsequently seen by the user do
2056 * not appear in the back stack.
2057 */
2058 public void clearBackStackWhenItemAdded(String urlToMatch) {
2059 mClearHistoryMatchUrl = urlToMatch;
2060 }
2061
John Reck1cf4b792011-07-26 10:22:22 -07002062 protected void persistThumbnail() {
2063 BackgroundHandler.execute(mSaveThumbnail);
2064 }
2065
2066 protected void deleteThumbnail() {
2067 BackgroundHandler.execute(mDeleteThumbnail);
2068 }
2069
2070 private void updateCaptureFromBlob(byte[] blob) {
2071 synchronized (Tab.this) {
2072 if (mCapture == null) {
2073 return;
2074 }
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002075 ByteBuffer buffer = ByteBuffer.wrap(blob);
2076 if (buffer.capacity() != blob.length) {
2077 Log.e(LOGTAG, "Load capture has mismatched sizes: "
2078 + buffer.capacity() + " vs " + blob.length);
2079 }
2080 mCapture.copyPixelsFromBuffer(buffer);
John Reck1cf4b792011-07-26 10:22:22 -07002081 }
2082 }
2083
2084 private byte[] getCaptureBlob() {
2085 synchronized (Tab.this) {
2086 if (mCapture == null) {
2087 return null;
2088 }
2089 ByteBuffer buffer = ByteBuffer.allocate(mCapture.getByteCount());
2090 mCapture.copyPixelsToBuffer(buffer);
2091 return buffer.array();
2092 }
2093 }
2094
2095 private Runnable mSaveThumbnail = new Runnable() {
2096
2097 @Override
2098 public void run() {
2099 byte[] blob = getCaptureBlob();
2100 if (blob == null) {
2101 return;
2102 }
2103 ContentResolver cr = mContext.getContentResolver();
2104 ContentValues values = new ContentValues();
2105 values.put(Thumbnails._ID, mId);
2106 values.put(Thumbnails.THUMBNAIL, blob);
2107 cr.insert(Thumbnails.CONTENT_URI, values);
2108 }
2109 };
2110
2111 private Runnable mDeleteThumbnail = new Runnable() {
2112
2113 @Override
2114 public void run() {
2115 ContentResolver cr = mContext.getContentResolver();
2116 try {
2117 cr.delete(ContentUris.withAppendedId(Thumbnails.CONTENT_URI, mId),
2118 null, null);
2119 } catch (Throwable t) {}
2120 }
2121 };
2122
2123 private Runnable mLoadThumbnail = new Runnable() {
2124
2125 @Override
2126 public void run() {
2127 ContentResolver cr = mContext.getContentResolver();
2128 Cursor c = null;
2129 try {
2130 Uri uri = ContentUris.withAppendedId(Thumbnails.CONTENT_URI, mId);
2131 c = cr.query(uri, new String[] {Thumbnails._ID,
2132 Thumbnails.THUMBNAIL}, null, null, null);
2133 if (c.moveToFirst()) {
2134 byte[] data = c.getBlob(1);
2135 if (data != null && data.length > 0) {
2136 updateCaptureFromBlob(data);
2137 }
2138 }
2139 } finally {
2140 if (c != null) {
2141 c.close();
2142 }
2143 }
2144 }
2145 };
2146
Grace Kloba22ac16e2009-10-07 18:00:23 -07002147}