blob: 3b283cc2ddc9f11c9ebc426011373deb745c5c45 [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;
Michael Kolbc3af0672011-08-09 10:24:41 -070034import android.graphics.Color;
Michael Kolb9ef259a2011-07-12 15:33:08 -070035import android.graphics.Picture;
Grace Kloba22ac16e2009-10-07 18:00:23 -070036import android.net.Uri;
37import android.net.http.SslError;
Grace Kloba22ac16e2009-10-07 18:00:23 -070038import android.os.Bundle;
Michael Kolb9ef259a2011-07-12 15:33:08 -070039import android.os.Handler;
Grace Kloba22ac16e2009-10-07 18:00:23 -070040import android.os.Message;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +000041import android.os.SystemClock;
Brian Carlstrom8862c1d2011-06-02 01:05:55 -070042import android.security.KeyChain;
Brian Carlstromaa09cd82011-06-09 16:04:40 -070043import android.security.KeyChainAliasCallback;
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -050044import android.speech.RecognizerResultsIntent;
John Reck24f18262011-06-17 14:47:20 -070045import android.text.TextUtils;
Grace Kloba22ac16e2009-10-07 18:00:23 -070046import android.util.Log;
47import android.view.KeyEvent;
48import android.view.LayoutInflater;
49import android.view.View;
Grace Kloba50c241e2010-04-20 11:07:50 -070050import android.view.ViewStub;
Brian Carlstrom8862c1d2011-06-02 01:05:55 -070051import android.webkit.ClientCertRequestHandler;
Ben Murdochc42addf2010-01-28 15:19:59 +000052import android.webkit.ConsoleMessage;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -050053import android.webkit.DownloadListener;
Grace Kloba22ac16e2009-10-07 18:00:23 -070054import android.webkit.GeolocationPermissions;
55import android.webkit.HttpAuthHandler;
56import android.webkit.SslErrorHandler;
57import android.webkit.URLUtil;
58import android.webkit.ValueCallback;
John Reck6c2e2f32011-08-22 13:41:23 -070059import android.webkit.WebBackForwardList;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -050060import android.webkit.WebBackForwardListClient;
Grace Kloba22ac16e2009-10-07 18:00:23 -070061import android.webkit.WebChromeClient;
62import android.webkit.WebHistoryItem;
John Reck438bf462011-01-12 18:11:46 -080063import android.webkit.WebResourceResponse;
Grace Kloba22ac16e2009-10-07 18:00:23 -070064import android.webkit.WebStorage;
65import android.webkit.WebView;
Michael Kolb9ef259a2011-07-12 15:33:08 -070066import android.webkit.WebView.PictureListener;
Grace Kloba22ac16e2009-10-07 18:00:23 -070067import android.webkit.WebViewClient;
Ben Murdoch1d676b62011-01-17 12:54:24 +000068import android.widget.CheckBox;
Grace Kloba22ac16e2009-10-07 18:00:23 -070069import android.widget.LinearLayout;
70import android.widget.TextView;
Ben Murdoch8029a772010-11-16 11:58:21 +000071import android.widget.Toast;
Grace Kloba22ac16e2009-10-07 18:00:23 -070072
John Reck8ee633f2011-08-09 16:00:35 -070073import com.android.browser.TabControl.OnThumbnailUpdatedListener;
John Reck541f55a2011-06-07 16:34:43 -070074import com.android.browser.homepages.HomeProvider;
John Reck1cf4b792011-07-26 10:22:22 -070075import com.android.browser.provider.BrowserProvider2.Thumbnails;
John Reck8cc92352011-07-06 17:41:52 -070076import com.android.browser.provider.SnapshotProvider.Snapshots;
John Reck541f55a2011-06-07 16:34:43 -070077import com.android.common.speech.LoggingEvents;
78
79import java.io.ByteArrayOutputStream;
John Reck1cf4b792011-07-26 10:22:22 -070080import java.nio.ByteBuffer;
Michael Kolbfe251992010-07-08 15:41:55 -070081import java.util.ArrayList;
82import java.util.HashMap;
83import java.util.Iterator;
84import java.util.LinkedList;
85import java.util.Map;
86import java.util.Vector;
John Reck8cc92352011-07-06 17:41:52 -070087import java.util.zip.GZIPOutputStream;
Michael Kolbfe251992010-07-08 15:41:55 -070088
Grace Kloba22ac16e2009-10-07 18:00:23 -070089/**
90 * Class for maintaining Tabs with a main WebView and a subwindow.
91 */
Michael Kolb9ef259a2011-07-12 15:33:08 -070092class Tab implements PictureListener {
Michael Kolb8233fac2010-10-26 16:08:53 -070093
Grace Kloba22ac16e2009-10-07 18:00:23 -070094 // Log Tag
95 private static final String LOGTAG = "Tab";
Mathew Inwood1dd8e822011-08-03 14:34:29 +010096 private static final boolean LOGD_ENABLED = com.android.browser.Browser.LOGD_ENABLED;
Ben Murdochc42addf2010-01-28 15:19:59 +000097 // Special case the logtag for messages for the Console to make it easier to
98 // filter them and match the logtag used for these messages in older versions
99 // of the browser.
100 private static final String CONSOLE_LOGTAG = "browser";
101
Michael Kolb9ef259a2011-07-12 15:33:08 -0700102 private static final int MSG_CAPTURE = 42;
John Reck8ee633f2011-08-09 16:00:35 -0700103 private static final int CAPTURE_DELAY = 100;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700104
John Reck1cf4b792011-07-26 10:22:22 -0700105 private static Bitmap sDefaultFavicon;
106
John Reck30c714c2010-12-16 17:30:34 -0800107 public enum LockIcon {
108 LOCK_ICON_UNSECURE,
109 LOCK_ICON_SECURE,
110 LOCK_ICON_MIXED,
111 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700112
Michael Kolb14612442011-06-24 13:06:29 -0700113 Context mContext;
John Reckd8c74522011-06-14 08:45:00 -0700114 protected WebViewController mWebViewController;
Michael Kolb8233fac2010-10-26 16:08:53 -0700115
Michael Kolbc831b632011-05-11 09:30:34 -0700116 // The tab ID
John Reckd8c74522011-06-14 08:45:00 -0700117 private long mId = -1;
Michael Kolbc831b632011-05-11 09:30:34 -0700118
Grace Kloba22ac16e2009-10-07 18:00:23 -0700119 // The Geolocation permissions prompt
120 private GeolocationPermissionsPrompt mGeolocationPermissionsPrompt;
121 // Main WebView wrapper
Michael Kolba713ec82010-11-29 17:27:06 -0800122 private View mContainer;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700123 // Main WebView
124 private WebView mMainView;
125 // Subwindow container
126 private View mSubViewContainer;
127 // Subwindow WebView
128 private WebView mSubView;
129 // Saved bundle for when we are running low on memory. It contains the
130 // information needed to restore the WebView if the user goes back to the
131 // tab.
132 private Bundle mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700133 // Parent Tab. This is the Tab that created this Tab, or null if the Tab was
134 // created by the UI
Michael Kolbc831b632011-05-11 09:30:34 -0700135 private Tab mParent;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700136 // Tab that constructed by this Tab. This is used when this Tab is
137 // destroyed, it clears all mParentTab values in the children.
Michael Kolbc831b632011-05-11 09:30:34 -0700138 private Vector<Tab> mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700139 // If true, the tab is in the foreground of the current activity.
140 private boolean mInForeground;
Michael Kolb8233fac2010-10-26 16:08:53 -0700141 // If true, the tab is in page loading state (after onPageStarted,
142 // before onPageFinsihed)
143 private boolean mInPageLoad;
John Reck30c714c2010-12-16 17:30:34 -0800144 // The last reported progress of the current page
145 private int mPageLoadProgress;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000146 // The time the load started, used to find load page time
147 private long mLoadStartTime;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700148 // Application identifier used to find tabs that another application wants
149 // to reuse.
150 private String mAppId;
Michael Kolbe28b3472011-08-04 16:54:31 -0700151 // flag to indicate if tab should be closed on back
152 private boolean mCloseOnBack;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700153 // Keep the original url around to avoid killing the old WebView if the url
154 // has not changed.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700155 // Error console for the tab
156 private ErrorConsoleView mErrorConsole;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -0500157 // The listener that gets invoked when a download is started from the
158 // mMainView
159 private final DownloadListener mDownloadListener;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500160 // Listener used to know when we move forward or back in the history list.
161 private final WebBackForwardListClient mWebBackForwardListClient;
John Recke969cc52010-12-21 17:24:43 -0800162 private DataController mDataController;
Patrick Scott92066772011-03-10 08:46:27 -0500163 // State of the auto-login request.
164 private DeviceAccountLogin mDeviceAccountLogin;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700165
166 // AsyncTask for downloading touch icons
167 DownloadTouchIcon mTouchIconLoader;
168
John Reck35e9dd62011-04-25 09:01:54 -0700169 private BrowserSettings mSettings;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700170 private int mCaptureWidth;
171 private int mCaptureHeight;
172 private Bitmap mCapture;
173 private Handler mHandler;
174
Mathew Inwood1dd8e822011-08-03 14:34:29 +0100175 /**
176 * See {@link #clearBackStackWhenItemAdded(String)}.
177 */
178 private String mClearHistoryMatchUrl;
179
John Reck1cf4b792011-07-26 10:22:22 -0700180 private static synchronized Bitmap getDefaultFavicon(Context context) {
181 if (sDefaultFavicon == null) {
182 sDefaultFavicon = BitmapFactory.decodeResource(
183 context.getResources(), R.drawable.app_web_browser_sm);
184 }
185 return sDefaultFavicon;
186 }
Michael Kolbeb95db42011-03-03 10:38:40 -0800187
John Reck30c714c2010-12-16 17:30:34 -0800188 // All the state needed for a page
John Reckd8c74522011-06-14 08:45:00 -0700189 protected static class PageState {
John Reck30c714c2010-12-16 17:30:34 -0800190 String mUrl;
John Reckdb22ec42011-06-29 11:31:24 -0700191 String mOriginalUrl;
John Reck30c714c2010-12-16 17:30:34 -0800192 String mTitle;
193 LockIcon mLockIcon;
194 Bitmap mFavicon;
John Reck502a3532011-08-16 14:21:46 -0700195 boolean mIsBookmarkedSite = false;
196 boolean mIncognito = false;
John Reck30c714c2010-12-16 17:30:34 -0800197
198 PageState(Context c, boolean incognito) {
John Reck502a3532011-08-16 14:21:46 -0700199 mIncognito = incognito;
200 if (mIncognito) {
John Reckdb22ec42011-06-29 11:31:24 -0700201 mOriginalUrl = mUrl = "browser:incognito";
John Reck30c714c2010-12-16 17:30:34 -0800202 mTitle = c.getString(R.string.new_incognito_tab);
John Reck30c714c2010-12-16 17:30:34 -0800203 } else {
John Reckdb22ec42011-06-29 11:31:24 -0700204 mOriginalUrl = mUrl = "";
John Reck30c714c2010-12-16 17:30:34 -0800205 mTitle = c.getString(R.string.new_tab);
John Reck30c714c2010-12-16 17:30:34 -0800206 }
John Reck1cf4b792011-07-26 10:22:22 -0700207 mFavicon = null;
John Reck30c714c2010-12-16 17:30:34 -0800208 mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
209 }
210
211 PageState(Context c, boolean incognito, String url, Bitmap favicon) {
John Reck502a3532011-08-16 14:21:46 -0700212 mIncognito = incognito;
John Reckdb22ec42011-06-29 11:31:24 -0700213 mOriginalUrl = mUrl = url;
John Reck30c714c2010-12-16 17:30:34 -0800214 mTitle = null;
215 if (URLUtil.isHttpsUrl(url)) {
216 mLockIcon = LockIcon.LOCK_ICON_SECURE;
217 } else {
218 mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
219 }
John Reck1cf4b792011-07-26 10:22:22 -0700220 mFavicon = favicon;
John Reck30c714c2010-12-16 17:30:34 -0800221 }
John Reck1cf4b792011-07-26 10:22:22 -0700222
Grace Kloba22ac16e2009-10-07 18:00:23 -0700223 }
224
John Reck30c714c2010-12-16 17:30:34 -0800225 // The current/loading page's state
John Reckd8c74522011-06-14 08:45:00 -0700226 protected PageState mCurrentState;
John Reck30c714c2010-12-16 17:30:34 -0800227
Grace Kloba22ac16e2009-10-07 18:00:23 -0700228 // Used for saving and restoring each Tab
Michael Kolbc831b632011-05-11 09:30:34 -0700229 static final String ID = "ID";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700230 static final String CURRURL = "currentUrl";
231 static final String CURRTITLE = "currentTitle";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700232 static final String PARENTTAB = "parentTab";
233 static final String APPID = "appid";
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700234 static final String INCOGNITO = "privateBrowsingEnabled";
John Reckb0a86db2011-05-24 14:05:58 -0700235 static final String USERAGENT = "useragent";
Michael Kolbe28b3472011-08-04 16:54:31 -0700236 static final String CLOSEFLAG = "closeOnBack";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700237
238 // -------------------------------------------------------------------------
239
Leon Scroggins58d56c62010-01-28 15:12:40 -0500240 /**
241 * Private information regarding the latest voice search. If the Tab is not
242 * in voice search mode, this will be null.
243 */
244 private VoiceSearchData mVoiceSearchData;
245 /**
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400246 * Remove voice search mode from this tab.
247 */
248 public void revertVoiceSearchMode() {
249 if (mVoiceSearchData != null) {
250 mVoiceSearchData = null;
251 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700252 mWebViewController.revertVoiceSearchMode(this);
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400253 }
254 }
255 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700256
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400257 /**
Leon Scroggins58d56c62010-01-28 15:12:40 -0500258 * Return whether the tab is in voice search mode.
259 */
260 public boolean isInVoiceSearchMode() {
261 return mVoiceSearchData != null;
262 }
263 /**
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400264 * Return true if the Tab is in voice search mode and the voice search
265 * Intent came with a String identifying that Google provided the Intent.
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500266 */
267 public boolean voiceSearchSourceIsGoogle() {
268 return mVoiceSearchData != null && mVoiceSearchData.mSourceIsGoogle;
269 }
270 /**
Leon Scroggins58d56c62010-01-28 15:12:40 -0500271 * Get the title to display for the current voice search page. If the Tab
272 * is not in voice search mode, return null.
273 */
274 public String getVoiceDisplayTitle() {
275 if (mVoiceSearchData == null) return null;
276 return mVoiceSearchData.mLastVoiceSearchTitle;
277 }
278 /**
279 * Get the latest array of voice search results, to be passed to the
280 * BrowserProvider. If the Tab is not in voice search mode, return null.
281 */
282 public ArrayList<String> getVoiceSearchResults() {
283 if (mVoiceSearchData == null) return null;
284 return mVoiceSearchData.mVoiceSearchResults;
285 }
286 /**
287 * Activate voice search mode.
288 * @param intent Intent which has the results to use, or an index into the
289 * results when reusing the old results.
290 */
291 /* package */ void activateVoiceSearchMode(Intent intent) {
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500292 int index = 0;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500293 ArrayList<String> results = intent.getStringArrayListExtra(
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -0500294 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_STRINGS);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500295 if (results != null) {
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500296 ArrayList<String> urls = intent.getStringArrayListExtra(
297 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_URLS);
298 ArrayList<String> htmls = intent.getStringArrayListExtra(
299 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_HTML);
300 ArrayList<String> baseUrls = intent.getStringArrayListExtra(
301 RecognizerResultsIntent
302 .EXTRA_VOICE_SEARCH_RESULT_HTML_BASE_URLS);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500303 // This tab is now entering voice search mode for the first time, or
304 // a new voice search was done.
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500305 int size = results.size();
306 if (urls == null || size != urls.size()) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500307 throw new AssertionError("improper extras passed in Intent");
308 }
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500309 if (htmls == null || htmls.size() != size || baseUrls == null ||
310 (baseUrls.size() != size && baseUrls.size() != 1)) {
311 // If either of these arrays are empty/incorrectly sized, ignore
312 // them.
313 htmls = null;
314 baseUrls = null;
315 }
316 mVoiceSearchData = new VoiceSearchData(results, urls, htmls,
317 baseUrls);
Leon Scroggins9df94972010-03-08 18:20:35 -0500318 mVoiceSearchData.mHeaders = intent.getParcelableArrayListExtra(
319 RecognizerResultsIntent
320 .EXTRA_VOICE_SEARCH_RESULT_HTTP_HEADERS);
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500321 mVoiceSearchData.mSourceIsGoogle = intent.getBooleanExtra(
322 VoiceSearchData.SOURCE_IS_GOOGLE, false);
Leon Scroggins2ee4a5a2010-03-15 16:56:57 -0400323 mVoiceSearchData.mVoiceSearchIntent = new Intent(intent);
Leon Scrogginse10dde52010-03-08 19:53:03 -0500324 }
325 String extraData = intent.getStringExtra(
326 SearchManager.EXTRA_DATA_KEY);
327 if (extraData != null) {
328 index = Integer.parseInt(extraData);
329 if (index >= mVoiceSearchData.mVoiceSearchResults.size()) {
330 throw new AssertionError("index must be less than "
331 + "size of mVoiceSearchResults");
332 }
333 if (mVoiceSearchData.mSourceIsGoogle) {
334 Intent logIntent = new Intent(
335 LoggingEvents.ACTION_LOG_EVENT);
336 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
337 LoggingEvents.VoiceSearch.N_BEST_CHOOSE);
338 logIntent.putExtra(
339 LoggingEvents.VoiceSearch.EXTRA_N_BEST_CHOOSE_INDEX,
340 index);
Michael Kolb14612442011-06-24 13:06:29 -0700341 mContext.sendBroadcast(logIntent);
Leon Scrogginse10dde52010-03-08 19:53:03 -0500342 }
343 if (mVoiceSearchData.mVoiceSearchIntent != null) {
Leon Scroggins2ee4a5a2010-03-15 16:56:57 -0400344 // Copy the Intent, so that each history item will have its own
345 // Intent, with different (or none) extra data.
346 Intent latest = new Intent(mVoiceSearchData.mVoiceSearchIntent);
347 latest.putExtra(SearchManager.EXTRA_DATA_KEY, extraData);
348 mVoiceSearchData.mVoiceSearchIntent = latest;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500349 }
350 }
351 mVoiceSearchData.mLastVoiceSearchTitle
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500352 = mVoiceSearchData.mVoiceSearchResults.get(index);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500353 if (mInForeground) {
Michael Kolb11d19782011-03-20 10:17:40 -0700354 mWebViewController.activateVoiceSearchMode(
355 mVoiceSearchData.mLastVoiceSearchTitle,
356 mVoiceSearchData.mVoiceSearchResults);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500357 }
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500358 if (mVoiceSearchData.mVoiceSearchHtmls != null) {
359 // When index was found it was already ensured that it was valid
360 String uriString = mVoiceSearchData.mVoiceSearchHtmls.get(index);
361 if (uriString != null) {
362 Uri dataUri = Uri.parse(uriString);
363 if (RecognizerResultsIntent.URI_SCHEME_INLINE.equals(
364 dataUri.getScheme())) {
365 // If there is only one base URL, use it. If there are
366 // more, there will be one for each index, so use the base
367 // URL corresponding to the index.
368 String baseUrl = mVoiceSearchData.mVoiceSearchBaseUrls.get(
369 mVoiceSearchData.mVoiceSearchBaseUrls.size() > 1 ?
370 index : 0);
371 mVoiceSearchData.mLastVoiceSearchUrl = baseUrl;
372 mMainView.loadDataWithBaseURL(baseUrl,
373 uriString.substring(RecognizerResultsIntent
374 .URI_SCHEME_INLINE.length() + 1), "text/html",
375 "utf-8", baseUrl);
376 return;
377 }
378 }
379 }
Leon Scroggins58d56c62010-01-28 15:12:40 -0500380 mVoiceSearchData.mLastVoiceSearchUrl
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500381 = mVoiceSearchData.mVoiceSearchUrls.get(index);
382 if (null == mVoiceSearchData.mLastVoiceSearchUrl) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700383 mVoiceSearchData.mLastVoiceSearchUrl = UrlUtils.smartUrlFilter(
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500384 mVoiceSearchData.mLastVoiceSearchTitle);
385 }
Leon Scroggins9df94972010-03-08 18:20:35 -0500386 Map<String, String> headers = null;
387 if (mVoiceSearchData.mHeaders != null) {
388 int bundleIndex = mVoiceSearchData.mHeaders.size() == 1 ? 0
389 : index;
390 Bundle bundle = mVoiceSearchData.mHeaders.get(bundleIndex);
391 if (bundle != null && !bundle.isEmpty()) {
392 Iterator<String> iter = bundle.keySet().iterator();
393 headers = new HashMap<String, String>();
394 while (iter.hasNext()) {
395 String key = iter.next();
396 headers.put(key, bundle.getString(key));
397 }
398 }
399 }
400 mMainView.loadUrl(mVoiceSearchData.mLastVoiceSearchUrl, headers);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500401 }
402 /* package */ static class VoiceSearchData {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500403 public VoiceSearchData(ArrayList<String> results,
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500404 ArrayList<String> urls, ArrayList<String> htmls,
405 ArrayList<String> baseUrls) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500406 mVoiceSearchResults = results;
407 mVoiceSearchUrls = urls;
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500408 mVoiceSearchHtmls = htmls;
409 mVoiceSearchBaseUrls = baseUrls;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500410 }
411 /*
412 * ArrayList of suggestions to be displayed when opening the
413 * SearchManager
414 */
415 public ArrayList<String> mVoiceSearchResults;
416 /*
417 * ArrayList of urls, associated with the suggestions in
418 * mVoiceSearchResults.
419 */
420 public ArrayList<String> mVoiceSearchUrls;
421 /*
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500422 * ArrayList holding content to load for each item in
423 * mVoiceSearchResults.
424 */
425 public ArrayList<String> mVoiceSearchHtmls;
426 /*
427 * ArrayList holding base urls for the items in mVoiceSearchResults.
428 * If non null, this will either have the same size as
429 * mVoiceSearchResults or have a size of 1, in which case all will use
430 * the same base url
431 */
432 public ArrayList<String> mVoiceSearchBaseUrls;
433 /*
Leon Scroggins58d56c62010-01-28 15:12:40 -0500434 * The last url provided by voice search. Used for comparison to see if
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500435 * we are going to a page by some method besides voice search.
Leon Scroggins58d56c62010-01-28 15:12:40 -0500436 */
437 public String mLastVoiceSearchUrl;
438 /**
439 * The last title used for voice search. Needed to update the title bar
440 * when switching tabs.
441 */
442 public String mLastVoiceSearchTitle;
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500443 /**
444 * Whether the Intent which turned on voice search mode contained the
445 * String signifying that Google was the source.
446 */
447 public boolean mSourceIsGoogle;
448 /**
Leon Scroggins9df94972010-03-08 18:20:35 -0500449 * List of headers to be passed into the WebView containing location
450 * information
451 */
452 public ArrayList<Bundle> mHeaders;
453 /**
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500454 * The Intent used to invoke voice search. Placed on the
455 * WebHistoryItem so that when coming back to a previous voice search
456 * page we can again activate voice search.
457 */
Leon Scrogginse10dde52010-03-08 19:53:03 -0500458 public Intent mVoiceSearchIntent;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500459 /**
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500460 * String used to identify Google as the source of voice search.
461 */
462 public static String SOURCE_IS_GOOGLE
463 = "android.speech.extras.SOURCE_IS_GOOGLE";
Leon Scroggins58d56c62010-01-28 15:12:40 -0500464 }
465
Grace Kloba22ac16e2009-10-07 18:00:23 -0700466 // Container class for the next error dialog that needs to be displayed
467 private class ErrorDialog {
468 public final int mTitle;
469 public final String mDescription;
470 public final int mError;
471 ErrorDialog(int title, String desc, int error) {
472 mTitle = title;
473 mDescription = desc;
474 mError = error;
475 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700476 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700477
478 private void processNextError() {
479 if (mQueuedErrors == null) {
480 return;
481 }
482 // The first one is currently displayed so just remove it.
483 mQueuedErrors.removeFirst();
484 if (mQueuedErrors.size() == 0) {
485 mQueuedErrors = null;
486 return;
487 }
488 showError(mQueuedErrors.getFirst());
489 }
490
491 private DialogInterface.OnDismissListener mDialogListener =
492 new DialogInterface.OnDismissListener() {
493 public void onDismiss(DialogInterface d) {
494 processNextError();
495 }
496 };
497 private LinkedList<ErrorDialog> mQueuedErrors;
498
499 private void queueError(int err, String desc) {
500 if (mQueuedErrors == null) {
501 mQueuedErrors = new LinkedList<ErrorDialog>();
502 }
503 for (ErrorDialog d : mQueuedErrors) {
504 if (d.mError == err) {
505 // Already saw a similar error, ignore the new one.
506 return;
507 }
508 }
509 ErrorDialog errDialog = new ErrorDialog(
510 err == WebViewClient.ERROR_FILE_NOT_FOUND ?
511 R.string.browserFrameFileErrorLabel :
512 R.string.browserFrameNetworkErrorLabel,
513 desc, err);
514 mQueuedErrors.addLast(errDialog);
515
516 // Show the dialog now if the queue was empty and it is in foreground
517 if (mQueuedErrors.size() == 1 && mInForeground) {
518 showError(errDialog);
519 }
520 }
521
522 private void showError(ErrorDialog errDialog) {
523 if (mInForeground) {
Michael Kolb14612442011-06-24 13:06:29 -0700524 AlertDialog d = new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700525 .setTitle(errDialog.mTitle)
526 .setMessage(errDialog.mDescription)
527 .setPositiveButton(R.string.ok, null)
528 .create();
529 d.setOnDismissListener(mDialogListener);
530 d.show();
531 }
532 }
533
534 // -------------------------------------------------------------------------
535 // WebViewClient implementation for the main WebView
536 // -------------------------------------------------------------------------
537
538 private final WebViewClient mWebViewClient = new WebViewClient() {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500539 private Message mDontResend;
540 private Message mResend;
Michael Kolb47bd1e42011-09-01 15:25:00 -0700541
542 private boolean providersDiffer(String url, String otherUrl) {
543 Uri uri1 = Uri.parse(url);
544 Uri uri2 = Uri.parse(otherUrl);
545 return !uri1.getEncodedAuthority().equals(uri2.getEncodedAuthority());
546 }
547
Grace Kloba22ac16e2009-10-07 18:00:23 -0700548 @Override
549 public void onPageStarted(WebView view, String url, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700550 mInPageLoad = true;
John Reck30c714c2010-12-16 17:30:34 -0800551 mPageLoadProgress = 0;
Michael Kolb14612442011-06-24 13:06:29 -0700552 mCurrentState = new PageState(mContext,
John Reck30c714c2010-12-16 17:30:34 -0800553 view.isPrivateBrowsingEnabled(), url, favicon);
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000554 mLoadStartTime = SystemClock.uptimeMillis();
Leon Scroggins58d56c62010-01-28 15:12:40 -0500555 if (mVoiceSearchData != null
Michael Kolb47bd1e42011-09-01 15:25:00 -0700556 && providersDiffer(url, mVoiceSearchData.mLastVoiceSearchUrl)) {
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500557 if (mVoiceSearchData.mSourceIsGoogle) {
558 Intent i = new Intent(LoggingEvents.ACTION_LOG_EVENT);
559 i.putExtra(LoggingEvents.EXTRA_FLUSH, true);
Michael Kolb14612442011-06-24 13:06:29 -0700560 mContext.sendBroadcast(i);
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500561 }
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400562 revertVoiceSearchMode();
Leon Scroggins58d56c62010-01-28 15:12:40 -0500563 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700564
Grace Kloba22ac16e2009-10-07 18:00:23 -0700565
566 // If we start a touch icon load and then load a new page, we don't
567 // want to cancel the current touch icon loader. But, we do want to
568 // create a new one when the touch icon url is known.
569 if (mTouchIconLoader != null) {
570 mTouchIconLoader.mTab = null;
571 mTouchIconLoader = null;
572 }
573
574 // reset the error console
575 if (mErrorConsole != null) {
576 mErrorConsole.clearErrorMessages();
Michael Kolb8233fac2010-10-26 16:08:53 -0700577 if (mWebViewController.shouldShowErrorConsole()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700578 mErrorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
579 }
580 }
581
Patrick Scott92066772011-03-10 08:46:27 -0500582 // Cancel the auto-login process.
583 if (mDeviceAccountLogin != null) {
584 mDeviceAccountLogin.cancel();
585 mDeviceAccountLogin = null;
586 mWebViewController.hideAutoLogin(Tab.this);
587 }
588
Grace Kloba22ac16e2009-10-07 18:00:23 -0700589 // finally update the UI in the activity if it is in the foreground
John Reck324d4402011-01-11 16:56:42 -0800590 mWebViewController.onPageStarted(Tab.this, view, favicon);
Leon Scroggins4cd97792010-12-03 15:31:56 -0500591
John Recke969cc52010-12-21 17:24:43 -0800592 updateBookmarkedStatus();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700593 }
594
595 @Override
596 public void onPageFinished(WebView view, String url) {
John Recka1696282011-07-08 14:10:37 -0700597 if (!mInPageLoad) {
598 // In page navigation links (www.something.com#footer) will
599 // trigger an onPageFinished which we don't care about.
600 return;
601 }
John Reck5b691842010-11-29 11:21:13 -0800602 if (!isPrivateBrowsingEnabled()) {
603 LogTag.logPageFinishedLoading(
604 url, SystemClock.uptimeMillis() - mLoadStartTime);
605 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700606 mInPageLoad = false;
John Reck1cf4b792011-07-26 10:22:22 -0700607 syncCurrentState(view, url);
John Reck324d4402011-01-11 16:56:42 -0800608 mWebViewController.onPageFinished(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700609 }
610
611 // return true if want to hijack the url to let another app to handle it
612 @Override
613 public boolean shouldOverrideUrlLoading(WebView view, String url) {
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400614 if (voiceSearchSourceIsGoogle()) {
615 // This method is called when the user clicks on a link.
616 // VoiceSearchMode is turned off when the user leaves the
617 // Google results page, so at this point the user must be on
618 // that page. If the user clicked a link on that page, assume
619 // that the voice search was effective, and broadcast an Intent
620 // so a receiver can take note of that fact.
621 Intent logIntent = new Intent(LoggingEvents.ACTION_LOG_EVENT);
622 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
623 LoggingEvents.VoiceSearch.RESULT_CLICKED);
Michael Kolb14612442011-06-24 13:06:29 -0700624 mContext.sendBroadcast(logIntent);
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400625 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700626 if (mInForeground) {
Michael Kolb18eb3772010-12-10 14:29:51 -0800627 return mWebViewController.shouldOverrideUrlLoading(Tab.this,
628 view, url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700629 } else {
630 return false;
631 }
632 }
633
634 /**
635 * Updates the lock icon. This method is called when we discover another
636 * resource to be loaded for this page (for example, javascript). While
637 * we update the icon type, we do not update the lock icon itself until
638 * we are done loading, it is slightly more secure this way.
639 */
640 @Override
641 public void onLoadResource(WebView view, String url) {
642 if (url != null && url.length() > 0) {
643 // It is only if the page claims to be secure that we may have
644 // to update the lock:
John Reck30c714c2010-12-16 17:30:34 -0800645 if (mCurrentState.mLockIcon == LockIcon.LOCK_ICON_SECURE) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700646 // If NOT a 'safe' url, change the lock to mixed content!
647 if (!(URLUtil.isHttpsUrl(url) || URLUtil.isDataUrl(url)
648 || URLUtil.isAboutUrl(url))) {
John Reck30c714c2010-12-16 17:30:34 -0800649 mCurrentState.mLockIcon = LockIcon.LOCK_ICON_MIXED;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700650 }
651 }
652 }
653 }
654
655 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700656 * Show a dialog informing the user of the network error reported by
657 * WebCore if it is in the foreground.
658 */
659 @Override
660 public void onReceivedError(WebView view, int errorCode,
661 String description, String failingUrl) {
662 if (errorCode != WebViewClient.ERROR_HOST_LOOKUP &&
663 errorCode != WebViewClient.ERROR_CONNECT &&
664 errorCode != WebViewClient.ERROR_BAD_URL &&
665 errorCode != WebViewClient.ERROR_UNSUPPORTED_SCHEME &&
666 errorCode != WebViewClient.ERROR_FILE) {
667 queueError(errorCode, description);
668 }
Jeff Hamilton47654f42010-09-07 09:57:51 -0500669
670 // Don't log URLs when in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -0700671 if (!isPrivateBrowsingEnabled()) {
Jeff Hamilton47654f42010-09-07 09:57:51 -0500672 Log.e(LOGTAG, "onReceivedError " + errorCode + " " + failingUrl
673 + " " + description);
674 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700675 }
676
677 /**
678 * Check with the user if it is ok to resend POST data as the page they
679 * are trying to navigate to is the result of a POST.
680 */
681 @Override
682 public void onFormResubmission(WebView view, final Message dontResend,
683 final Message resend) {
684 if (!mInForeground) {
685 dontResend.sendToTarget();
686 return;
687 }
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500688 if (mDontResend != null) {
689 Log.w(LOGTAG, "onFormResubmission should not be called again "
690 + "while dialog is still up");
691 dontResend.sendToTarget();
692 return;
693 }
694 mDontResend = dontResend;
695 mResend = resend;
Michael Kolb14612442011-06-24 13:06:29 -0700696 new AlertDialog.Builder(mContext).setTitle(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700697 R.string.browserFrameFormResubmitLabel).setMessage(
698 R.string.browserFrameFormResubmitMessage)
699 .setPositiveButton(R.string.ok,
700 new DialogInterface.OnClickListener() {
701 public void onClick(DialogInterface dialog,
702 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500703 if (mResend != null) {
704 mResend.sendToTarget();
705 mResend = null;
706 mDontResend = null;
707 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700708 }
709 }).setNegativeButton(R.string.cancel,
710 new DialogInterface.OnClickListener() {
711 public void onClick(DialogInterface dialog,
712 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500713 if (mDontResend != null) {
714 mDontResend.sendToTarget();
715 mResend = null;
716 mDontResend = null;
717 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700718 }
719 }).setOnCancelListener(new OnCancelListener() {
720 public void onCancel(DialogInterface dialog) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500721 if (mDontResend != null) {
722 mDontResend.sendToTarget();
723 mResend = null;
724 mDontResend = null;
725 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700726 }
727 }).show();
728 }
729
730 /**
731 * Insert the url into the visited history database.
732 * @param url The url to be inserted.
733 * @param isReload True if this url is being reloaded.
734 * FIXME: Not sure what to do when reloading the page.
735 */
736 @Override
737 public void doUpdateVisitedHistory(WebView view, String url,
738 boolean isReload) {
John Reck324d4402011-01-11 16:56:42 -0800739 mWebViewController.doUpdateVisitedHistory(Tab.this, isReload);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700740 }
741
742 /**
743 * Displays SSL error(s) dialog to the user.
744 */
745 @Override
746 public void onReceivedSslError(final WebView view,
747 final SslErrorHandler handler, final SslError error) {
748 if (!mInForeground) {
749 handler.cancel();
John Reck30c714c2010-12-16 17:30:34 -0800750 setLockIconType(LockIcon.LOCK_ICON_UNSECURE);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700751 return;
752 }
John Reck35e9dd62011-04-25 09:01:54 -0700753 if (mSettings.showSecurityWarnings()) {
John Reckcb28b2c2011-08-26 17:39:44 -0700754 new AlertDialog.Builder(mContext)
755 .setTitle(R.string.security_warning)
756 .setMessage(R.string.ssl_warnings_header)
757 .setIcon(android.R.drawable.ic_dialog_alert)
758 .setPositiveButton(R.string.ssl_continue,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700759 new DialogInterface.OnClickListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700760 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700761 public void onClick(DialogInterface dialog,
762 int whichButton) {
763 handler.proceed();
764 }
John Reckcb28b2c2011-08-26 17:39:44 -0700765 })
766 .setNeutralButton(R.string.view_certificate,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700767 new DialogInterface.OnClickListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700768 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700769 public void onClick(DialogInterface dialog,
770 int whichButton) {
John Reckcb28b2c2011-08-26 17:39:44 -0700771 mWebViewController.showSslCertificateOnError(
772 view, handler, error);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700773 }
John Reckcb28b2c2011-08-26 17:39:44 -0700774 })
775 .setNegativeButton(R.string.ssl_go_back,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700776 new DialogInterface.OnClickListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700777 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700778 public void onClick(DialogInterface dialog,
779 int whichButton) {
John Reck30c714c2010-12-16 17:30:34 -0800780 dialog.cancel();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700781 }
John Reckcb28b2c2011-08-26 17:39:44 -0700782 })
783 .setOnCancelListener(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700784 new DialogInterface.OnCancelListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700785 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700786 public void onCancel(DialogInterface dialog) {
787 handler.cancel();
John Reck30c714c2010-12-16 17:30:34 -0800788 setLockIconType(LockIcon.LOCK_ICON_UNSECURE);
789 mWebViewController.onUserCanceledSsl(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700790 }
John Reckcb28b2c2011-08-26 17:39:44 -0700791 })
792 .show();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700793 } else {
794 handler.proceed();
795 }
796 }
797
798 /**
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700799 * Displays client certificate request to the user.
800 */
801 @Override
802 public void onReceivedClientCertRequest(final WebView view,
803 final ClientCertRequestHandler handler, final String host_and_port) {
804 if (!mInForeground) {
805 handler.ignore();
806 return;
807 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700808 int colon = host_and_port.lastIndexOf(':');
809 String host;
810 int port;
811 if (colon == -1) {
812 host = host_and_port;
813 port = -1;
814 } else {
815 String portString = host_and_port.substring(colon + 1);
816 try {
817 port = Integer.parseInt(portString);
818 host = host_and_port.substring(0, colon);
819 } catch (NumberFormatException e) {
820 host = host_and_port;
821 port = -1;
822 }
823 }
Michael Kolb14612442011-06-24 13:06:29 -0700824 KeyChain.choosePrivateKeyAlias(
825 mWebViewController.getActivity(), new KeyChainAliasCallback() {
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700826 @Override public void alias(String alias) {
827 if (alias == null) {
828 handler.cancel();
829 return;
830 }
Michael Kolb14612442011-06-24 13:06:29 -0700831 new KeyChainLookup(mContext, handler, alias).execute();
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700832 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700833 }, null, null, host, port, null);
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700834 }
835
836 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700837 * Handles an HTTP authentication request.
838 *
839 * @param handler The authentication handler
840 * @param host The host
841 * @param realm The realm
842 */
843 @Override
844 public void onReceivedHttpAuthRequest(WebView view,
845 final HttpAuthHandler handler, final String host,
846 final String realm) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700847 mWebViewController.onReceivedHttpAuthRequest(Tab.this, view, handler, host, realm);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700848 }
849
850 @Override
John Reck438bf462011-01-12 18:11:46 -0800851 public WebResourceResponse shouldInterceptRequest(WebView view,
852 String url) {
853 WebResourceResponse res = HomeProvider.shouldInterceptRequest(
Michael Kolb14612442011-06-24 13:06:29 -0700854 mContext, url);
John Reck438bf462011-01-12 18:11:46 -0800855 return res;
856 }
857
858 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700859 public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
860 if (!mInForeground) {
861 return false;
862 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700863 return mWebViewController.shouldOverrideKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700864 }
865
866 @Override
867 public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700868 if (!mInForeground) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700869 return;
870 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700871 mWebViewController.onUnhandledKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700872 }
Patrick Scott92066772011-03-10 08:46:27 -0500873
874 @Override
875 public void onReceivedLoginRequest(WebView view, String realm,
876 String account, String args) {
Michael Kolb14612442011-06-24 13:06:29 -0700877 new DeviceAccountLogin(mWebViewController.getActivity(), view, Tab.this, mWebViewController)
Patrick Scott92066772011-03-10 08:46:27 -0500878 .handleLogin(realm, account, args);
879 }
880
Grace Kloba22ac16e2009-10-07 18:00:23 -0700881 };
882
John Reck1cf4b792011-07-26 10:22:22 -0700883 private void syncCurrentState(WebView view, String url) {
884 // Sync state (in case of stop/timeout)
885 mCurrentState.mUrl = view.getUrl();
886 if (mCurrentState.mUrl == null) {
John Reck8bcafc12011-08-29 16:43:02 -0700887 mCurrentState.mUrl = "";
John Reck1cf4b792011-07-26 10:22:22 -0700888 }
889 mCurrentState.mOriginalUrl = view.getOriginalUrl();
890 mCurrentState.mTitle = view.getTitle();
891 mCurrentState.mFavicon = view.getFavicon();
892 if (!URLUtil.isHttpsUrl(mCurrentState.mUrl)) {
893 // In case we stop when loading an HTTPS page from an HTTP page
894 // but before a provisional load occurred
895 mCurrentState.mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
896 }
John Reck502a3532011-08-16 14:21:46 -0700897 mCurrentState.mIncognito = view.isPrivateBrowsingEnabled();
John Reck1cf4b792011-07-26 10:22:22 -0700898 }
899
Patrick Scott92066772011-03-10 08:46:27 -0500900 // Called by DeviceAccountLogin when the Tab needs to have the auto-login UI
901 // displayed.
902 void setDeviceAccountLogin(DeviceAccountLogin login) {
903 mDeviceAccountLogin = login;
904 }
905
906 // Returns non-null if the title bar should display the auto-login UI.
907 DeviceAccountLogin getDeviceAccountLogin() {
908 return mDeviceAccountLogin;
909 }
910
Grace Kloba22ac16e2009-10-07 18:00:23 -0700911 // -------------------------------------------------------------------------
912 // WebChromeClient implementation for the main WebView
913 // -------------------------------------------------------------------------
914
915 private final WebChromeClient mWebChromeClient = new WebChromeClient() {
916 // Helper method to create a new tab or sub window.
917 private void createWindow(final boolean dialog, final Message msg) {
918 WebView.WebViewTransport transport =
919 (WebView.WebViewTransport) msg.obj;
920 if (dialog) {
921 createSubWindow();
Michael Kolb8233fac2010-10-26 16:08:53 -0700922 mWebViewController.attachSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700923 transport.setWebView(mSubView);
924 } else {
Michael Kolb7bcafde2011-05-09 13:55:59 -0700925 final Tab newTab = mWebViewController.openTab(null,
John Reck5949c662011-05-27 09:52:29 -0700926 Tab.this, true, true);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700927 transport.setWebView(newTab.getWebView());
928 }
929 msg.sendToTarget();
930 }
931
932 @Override
933 public boolean onCreateWindow(WebView view, final boolean dialog,
934 final boolean userGesture, final Message resultMsg) {
935 // only allow new window or sub window for the foreground case
936 if (!mInForeground) {
937 return false;
938 }
939 // Short-circuit if we can't create any more tabs or sub windows.
940 if (dialog && mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -0700941 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700942 .setTitle(R.string.too_many_subwindows_dialog_title)
943 .setIcon(android.R.drawable.ic_dialog_alert)
944 .setMessage(R.string.too_many_subwindows_dialog_message)
945 .setPositiveButton(R.string.ok, null)
946 .show();
947 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700948 } else if (!mWebViewController.getTabControl().canCreateNewTab()) {
Michael Kolb14612442011-06-24 13:06:29 -0700949 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700950 .setTitle(R.string.too_many_windows_dialog_title)
951 .setIcon(android.R.drawable.ic_dialog_alert)
952 .setMessage(R.string.too_many_windows_dialog_message)
953 .setPositiveButton(R.string.ok, null)
954 .show();
955 return false;
956 }
957
958 // Short-circuit if this was a user gesture.
959 if (userGesture) {
960 createWindow(dialog, resultMsg);
961 return true;
962 }
963
964 // Allow the popup and create the appropriate window.
965 final AlertDialog.OnClickListener allowListener =
966 new AlertDialog.OnClickListener() {
967 public void onClick(DialogInterface d,
968 int which) {
969 createWindow(dialog, resultMsg);
970 }
971 };
972
973 // Block the popup by returning a null WebView.
974 final AlertDialog.OnClickListener blockListener =
975 new AlertDialog.OnClickListener() {
976 public void onClick(DialogInterface d, int which) {
977 resultMsg.sendToTarget();
978 }
979 };
980
981 // Build a confirmation dialog to display to the user.
982 final AlertDialog d =
Michael Kolb14612442011-06-24 13:06:29 -0700983 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700984 .setTitle(R.string.attention)
985 .setIcon(android.R.drawable.ic_dialog_alert)
986 .setMessage(R.string.popup_window_attempt)
987 .setPositiveButton(R.string.allow, allowListener)
988 .setNegativeButton(R.string.block, blockListener)
989 .setCancelable(false)
990 .create();
991
992 // Show the confirmation dialog.
993 d.show();
994 return true;
995 }
996
997 @Override
Patrick Scotteb5061b2009-11-18 15:00:30 -0500998 public void onRequestFocus(WebView view) {
999 if (!mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -07001000 mWebViewController.switchToTab(Tab.this);
Patrick Scotteb5061b2009-11-18 15:00:30 -05001001 }
1002 }
1003
1004 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -07001005 public void onCloseWindow(WebView window) {
Michael Kolbc831b632011-05-11 09:30:34 -07001006 if (mParent != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001007 // JavaScript can only close popup window.
1008 if (mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -07001009 mWebViewController.switchToTab(mParent);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001010 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001011 mWebViewController.closeTab(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001012 }
1013 }
1014
1015 @Override
1016 public void onProgressChanged(WebView view, int newProgress) {
John Reck30c714c2010-12-16 17:30:34 -08001017 mPageLoadProgress = newProgress;
1018 mWebViewController.onProgressChanged(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001019 }
1020
1021 @Override
Leon Scroggins21d9b902010-03-11 09:33:11 -05001022 public void onReceivedTitle(WebView view, final String title) {
John Reck30c714c2010-12-16 17:30:34 -08001023 mCurrentState.mTitle = title;
Michael Kolb8233fac2010-10-26 16:08:53 -07001024 mWebViewController.onReceivedTitle(Tab.this, title);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001025 }
1026
1027 @Override
1028 public void onReceivedIcon(WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -08001029 mCurrentState.mFavicon = icon;
Michael Kolb8233fac2010-10-26 16:08:53 -07001030 mWebViewController.onFavicon(Tab.this, view, icon);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001031 }
1032
1033 @Override
1034 public void onReceivedTouchIconUrl(WebView view, String url,
1035 boolean precomposed) {
Michael Kolb14612442011-06-24 13:06:29 -07001036 final ContentResolver cr = mContext.getContentResolver();
Leon Scrogginsc8393d92010-04-23 14:58:16 -04001037 // Let precomposed icons take precedence over non-composed
1038 // icons.
1039 if (precomposed && mTouchIconLoader != null) {
1040 mTouchIconLoader.cancel(false);
1041 mTouchIconLoader = null;
1042 }
1043 // Have only one async task at a time.
1044 if (mTouchIconLoader == null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001045 mTouchIconLoader = new DownloadTouchIcon(Tab.this,
Michael Kolb14612442011-06-24 13:06:29 -07001046 mContext, cr, view);
Leon Scrogginsc8393d92010-04-23 14:58:16 -04001047 mTouchIconLoader.execute(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001048 }
1049 }
1050
1051 @Override
1052 public void onShowCustomView(View view,
1053 WebChromeClient.CustomViewCallback callback) {
Michael Kolb14612442011-06-24 13:06:29 -07001054 Activity activity = mWebViewController.getActivity();
1055 if (activity != null) {
1056 onShowCustomView(view, activity.getRequestedOrientation(), callback);
1057 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001058 }
1059
1060 @Override
1061 public void onShowCustomView(View view, int requestedOrientation,
1062 WebChromeClient.CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001063 if (mInForeground) mWebViewController.showCustomView(Tab.this, view,
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001064 requestedOrientation, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001065 }
1066
1067 @Override
1068 public void onHideCustomView() {
Michael Kolb8233fac2010-10-26 16:08:53 -07001069 if (mInForeground) mWebViewController.hideCustomView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001070 }
1071
1072 /**
1073 * The origin has exceeded its database quota.
1074 * @param url the URL that exceeded the quota
1075 * @param databaseIdentifier the identifier of the database on which the
1076 * transaction that caused the quota overflow was run
1077 * @param currentQuota the current quota for the origin.
1078 * @param estimatedSize the estimated size of the database.
1079 * @param totalUsedQuota is the sum of all origins' quota.
1080 * @param quotaUpdater The callback to run when a decision to allow or
1081 * deny quota has been made. Don't forget to call this!
1082 */
1083 @Override
1084 public void onExceededDatabaseQuota(String url,
1085 String databaseIdentifier, long currentQuota, long estimatedSize,
1086 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -07001087 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001088 .onExceededDatabaseQuota(url, databaseIdentifier,
1089 currentQuota, estimatedSize, totalUsedQuota,
1090 quotaUpdater);
1091 }
1092
1093 /**
1094 * The Application Cache has exceeded its max size.
1095 * @param spaceNeeded is the amount of disk space that would be needed
1096 * in order for the last appcache operation to succeed.
1097 * @param totalUsedQuota is the sum of all origins' quota.
1098 * @param quotaUpdater A callback to inform the WebCore thread that a
1099 * new app cache size is available. This callback must always
1100 * be executed at some point to ensure that the sleeping
1101 * WebCore thread is woken up.
1102 */
1103 @Override
1104 public void onReachedMaxAppCacheSize(long spaceNeeded,
1105 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -07001106 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001107 .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota,
1108 quotaUpdater);
1109 }
1110
1111 /**
1112 * Instructs the browser to show a prompt to ask the user to set the
1113 * Geolocation permission state for the specified origin.
1114 * @param origin The origin for which Geolocation permissions are
1115 * requested.
1116 * @param callback The callback to call once the user has set the
1117 * Geolocation permission state.
1118 */
1119 @Override
1120 public void onGeolocationPermissionsShowPrompt(String origin,
1121 GeolocationPermissions.Callback callback) {
1122 if (mInForeground) {
Grace Kloba50c241e2010-04-20 11:07:50 -07001123 getGeolocationPermissionsPrompt().show(origin, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001124 }
1125 }
1126
1127 /**
1128 * Instructs the browser to hide the Geolocation permissions prompt.
1129 */
1130 @Override
1131 public void onGeolocationPermissionsHidePrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001132 if (mInForeground && mGeolocationPermissionsPrompt != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001133 mGeolocationPermissionsPrompt.hide();
1134 }
1135 }
1136
Ben Murdoch65acc352009-11-19 18:16:04 +00001137 /* Adds a JavaScript error message to the system log and if the JS
1138 * console is enabled in the about:debug options, to that console
1139 * also.
Ben Murdochc42addf2010-01-28 15:19:59 +00001140 * @param consoleMessage the message object.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001141 */
1142 @Override
Ben Murdochc42addf2010-01-28 15:19:59 +00001143 public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001144 if (mInForeground) {
1145 // call getErrorConsole(true) so it will create one if needed
1146 ErrorConsoleView errorConsole = getErrorConsole(true);
Ben Murdochc42addf2010-01-28 15:19:59 +00001147 errorConsole.addErrorMessage(consoleMessage);
Michael Kolb8233fac2010-10-26 16:08:53 -07001148 if (mWebViewController.shouldShowErrorConsole()
1149 && errorConsole.getShowState() !=
1150 ErrorConsoleView.SHOW_MAXIMIZED) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001151 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
1152 }
1153 }
Ben Murdochc42addf2010-01-28 15:19:59 +00001154
Jeff Hamilton47654f42010-09-07 09:57:51 -05001155 // Don't log console messages in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001156 if (isPrivateBrowsingEnabled()) return true;
Jeff Hamilton47654f42010-09-07 09:57:51 -05001157
Ben Murdochc42addf2010-01-28 15:19:59 +00001158 String message = "Console: " + consoleMessage.message() + " "
1159 + consoleMessage.sourceId() + ":"
1160 + consoleMessage.lineNumber();
1161
1162 switch (consoleMessage.messageLevel()) {
1163 case TIP:
1164 Log.v(CONSOLE_LOGTAG, message);
1165 break;
1166 case LOG:
1167 Log.i(CONSOLE_LOGTAG, message);
1168 break;
1169 case WARNING:
1170 Log.w(CONSOLE_LOGTAG, message);
1171 break;
1172 case ERROR:
1173 Log.e(CONSOLE_LOGTAG, message);
1174 break;
1175 case DEBUG:
1176 Log.d(CONSOLE_LOGTAG, message);
1177 break;
1178 }
1179
1180 return true;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001181 }
1182
1183 /**
1184 * Ask the browser for an icon to represent a <video> element.
1185 * This icon will be used if the Web page did not specify a poster attribute.
1186 * @return Bitmap The icon or null if no such icon is available.
1187 */
1188 @Override
1189 public Bitmap getDefaultVideoPoster() {
1190 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001191 return mWebViewController.getDefaultVideoPoster();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001192 }
1193 return null;
1194 }
1195
1196 /**
1197 * Ask the host application for a custom progress view to show while
1198 * a <video> is loading.
1199 * @return View The progress view.
1200 */
1201 @Override
1202 public View getVideoLoadingProgressView() {
1203 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001204 return mWebViewController.getVideoLoadingProgressView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001205 }
1206 return null;
1207 }
1208
1209 @Override
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01001210 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001211 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001212 mWebViewController.openFileChooser(uploadMsg, acceptType);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001213 } else {
1214 uploadMsg.onReceiveValue(null);
1215 }
1216 }
1217
1218 /**
1219 * Deliver a list of already-visited URLs
1220 */
1221 @Override
1222 public void getVisitedHistory(final ValueCallback<String[]> callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001223 mWebViewController.getVisitedHistory(callback);
1224 }
Ben Murdoch8029a772010-11-16 11:58:21 +00001225
1226 @Override
1227 public void setupAutoFill(Message message) {
1228 // Prompt the user to set up their profile.
1229 final Message msg = message;
Michael Kolb14612442011-06-24 13:06:29 -07001230 AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
1231 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
Ben Murdoch1d676b62011-01-17 12:54:24 +00001232 Context.LAYOUT_INFLATER_SERVICE);
1233 final View layout = inflater.inflate(R.layout.setup_autofill_dialog, null);
1234
1235 builder.setView(layout)
Ben Murdochb7e6f942011-07-08 13:00:21 +01001236 .setTitle(R.string.autofill_setup_dialog_title)
Ben Murdoch1d676b62011-01-17 12:54:24 +00001237 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1238 @Override
1239 public void onClick(DialogInterface dialog, int id) {
1240 CheckBox disableAutoFill = (CheckBox) layout.findViewById(
1241 R.id.setup_autofill_dialog_disable_autofill);
1242
1243 if (disableAutoFill.isChecked()) {
1244 // Disable autofill and show a toast with how to turn it on again.
John Reck35e9dd62011-04-25 09:01:54 -07001245 mSettings.setAutofillEnabled(false);
Michael Kolb14612442011-06-24 13:06:29 -07001246 Toast.makeText(mContext,
Ben Murdoch1d676b62011-01-17 12:54:24 +00001247 R.string.autofill_setup_dialog_negative_toast,
1248 Toast.LENGTH_LONG).show();
1249 } else {
1250 // Take user to the AutoFill profile editor. When they return,
1251 // we will send the message that we pass here which will trigger
1252 // the form to get filled out with their new profile.
1253 mWebViewController.setupAutoFill(msg);
1254 }
1255 }
1256 })
1257 .setNegativeButton(R.string.cancel, null)
1258 .show();
Ben Murdoch8029a772010-11-16 11:58:21 +00001259 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001260 };
1261
1262 // -------------------------------------------------------------------------
1263 // WebViewClient implementation for the sub window
1264 // -------------------------------------------------------------------------
1265
1266 // Subclass of WebViewClient used in subwindows to notify the main
1267 // WebViewClient of certain WebView activities.
1268 private static class SubWindowClient extends WebViewClient {
1269 // The main WebViewClient.
1270 private final WebViewClient mClient;
Michael Kolb8233fac2010-10-26 16:08:53 -07001271 private final WebViewController mController;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001272
Michael Kolb8233fac2010-10-26 16:08:53 -07001273 SubWindowClient(WebViewClient client, WebViewController controller) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001274 mClient = client;
Michael Kolb8233fac2010-10-26 16:08:53 -07001275 mController = controller;
Leon Scroggins III211ba542010-04-19 13:21:13 -04001276 }
1277 @Override
1278 public void onPageStarted(WebView view, String url, Bitmap favicon) {
1279 // Unlike the others, do not call mClient's version, which would
1280 // change the progress bar. However, we do want to remove the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001281 // find or select dialog.
Michael Kolb8233fac2010-10-26 16:08:53 -07001282 mController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001283 }
1284 @Override
1285 public void doUpdateVisitedHistory(WebView view, String url,
1286 boolean isReload) {
1287 mClient.doUpdateVisitedHistory(view, url, isReload);
1288 }
1289 @Override
1290 public boolean shouldOverrideUrlLoading(WebView view, String url) {
1291 return mClient.shouldOverrideUrlLoading(view, url);
1292 }
1293 @Override
1294 public void onReceivedSslError(WebView view, SslErrorHandler handler,
1295 SslError error) {
1296 mClient.onReceivedSslError(view, handler, error);
1297 }
1298 @Override
Brian Carlstrom8862c1d2011-06-02 01:05:55 -07001299 public void onReceivedClientCertRequest(WebView view,
1300 ClientCertRequestHandler handler, String host_and_port) {
1301 mClient.onReceivedClientCertRequest(view, handler, host_and_port);
1302 }
1303 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -07001304 public void onReceivedHttpAuthRequest(WebView view,
1305 HttpAuthHandler handler, String host, String realm) {
1306 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
1307 }
1308 @Override
1309 public void onFormResubmission(WebView view, Message dontResend,
1310 Message resend) {
1311 mClient.onFormResubmission(view, dontResend, resend);
1312 }
1313 @Override
1314 public void onReceivedError(WebView view, int errorCode,
1315 String description, String failingUrl) {
1316 mClient.onReceivedError(view, errorCode, description, failingUrl);
1317 }
1318 @Override
1319 public boolean shouldOverrideKeyEvent(WebView view,
1320 android.view.KeyEvent event) {
1321 return mClient.shouldOverrideKeyEvent(view, event);
1322 }
1323 @Override
1324 public void onUnhandledKeyEvent(WebView view,
1325 android.view.KeyEvent event) {
1326 mClient.onUnhandledKeyEvent(view, event);
1327 }
1328 }
1329
1330 // -------------------------------------------------------------------------
1331 // WebChromeClient implementation for the sub window
1332 // -------------------------------------------------------------------------
1333
1334 private class SubWindowChromeClient extends WebChromeClient {
1335 // The main WebChromeClient.
1336 private final WebChromeClient mClient;
1337
1338 SubWindowChromeClient(WebChromeClient client) {
1339 mClient = client;
1340 }
1341 @Override
1342 public void onProgressChanged(WebView view, int newProgress) {
1343 mClient.onProgressChanged(view, newProgress);
1344 }
1345 @Override
1346 public boolean onCreateWindow(WebView view, boolean dialog,
1347 boolean userGesture, android.os.Message resultMsg) {
1348 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
1349 }
1350 @Override
1351 public void onCloseWindow(WebView window) {
1352 if (window != mSubView) {
1353 Log.e(LOGTAG, "Can't close the window");
1354 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001355 mWebViewController.dismissSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001356 }
1357 }
1358
1359 // -------------------------------------------------------------------------
1360
1361 // Construct a new tab
Michael Kolb7bcafde2011-05-09 13:55:59 -07001362 Tab(WebViewController wvcontroller, WebView w) {
John Reck1cf4b792011-07-26 10:22:22 -07001363 this(wvcontroller, w, null);
1364 }
1365
1366 Tab(WebViewController wvcontroller, Bundle state) {
1367 this(wvcontroller, null, state);
1368 }
1369
1370 Tab(WebViewController wvcontroller, WebView w, Bundle state) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001371 mWebViewController = wvcontroller;
Michael Kolb14612442011-06-24 13:06:29 -07001372 mContext = mWebViewController.getContext();
John Reck35e9dd62011-04-25 09:01:54 -07001373 mSettings = BrowserSettings.getInstance();
Michael Kolb14612442011-06-24 13:06:29 -07001374 mDataController = DataController.getInstance(mContext);
1375 mCurrentState = new PageState(mContext, w != null
John Reck847b5322011-04-14 17:02:18 -07001376 ? w.isPrivateBrowsingEnabled() : false);
Michael Kolb8233fac2010-10-26 16:08:53 -07001377 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001378 mInForeground = false;
1379
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001380 mDownloadListener = new DownloadListener() {
1381 public void onDownloadStart(String url, String userAgent,
1382 String contentDisposition, String mimetype,
1383 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001384 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001385 mimetype, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001386 }
1387 };
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001388 mWebBackForwardListClient = new WebBackForwardListClient() {
1389 @Override
1390 public void onNewHistoryItem(WebHistoryItem item) {
1391 if (isInVoiceSearchMode()) {
1392 item.setCustomData(mVoiceSearchData.mVoiceSearchIntent);
1393 }
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001394 if (mClearHistoryMatchUrl != null) {
1395 if (LOGD_ENABLED) {
1396 Log.d(LOGTAG, "onNewHistoryItem:\n\t" + item.getUrl() + "\n\t"
1397 + mClearHistoryMatchUrl);
1398 }
1399 if (TextUtils.equals(item.getOriginalUrl(), mClearHistoryMatchUrl)) {
1400 if (mMainView != null) {
1401 mMainView.clearHistory();
1402 }
1403 }
1404 mClearHistoryMatchUrl = null;
1405 }
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001406 }
1407 @Override
1408 public void onIndexChanged(WebHistoryItem item, int index) {
1409 Object data = item.getCustomData();
1410 if (data != null && data instanceof Intent) {
1411 activateVoiceSearchMode((Intent) data);
1412 }
1413 }
1414 };
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001415
John Reck1cf4b792011-07-26 10:22:22 -07001416 mCaptureWidth = mContext.getResources().getDimensionPixelSize(
1417 R.dimen.tab_thumbnail_width);
1418 mCaptureHeight = mContext.getResources().getDimensionPixelSize(
1419 R.dimen.tab_thumbnail_height);
1420 updateShouldCaptureThumbnails();
1421 restoreState(state);
John Reck52be4782011-08-26 15:37:29 -07001422 if (getId() == -1) {
1423 mId = TabControl.getNextId();
1424 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001425 setWebView(w);
Michael Kolb9ef259a2011-07-12 15:33:08 -07001426 mHandler = new Handler() {
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001427 @Override
Michael Kolb9ef259a2011-07-12 15:33:08 -07001428 public void handleMessage(Message m) {
John Reck1cf4b792011-07-26 10:22:22 -07001429 switch (m.what) {
1430 case MSG_CAPTURE:
1431 capture();
1432 break;
1433 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001434 }
1435 };
John Reck1cf4b792011-07-26 10:22:22 -07001436 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001437
Mathew Inwoode09305e2011-09-02 12:03:26 +01001438 /**
1439 * This is used to get a new ID when the tab has been preloaded, before it is displayed and
1440 * added to TabControl. Preloaded tabs can be created before restoreInstanceState, leading
1441 * to overlapping IDs between the preloaded and restored tabs.
1442 */
1443 public void refreshIdAfterPreload() {
1444 mId = TabControl.getNextId();
1445 }
1446
John Reck1cf4b792011-07-26 10:22:22 -07001447 public void updateShouldCaptureThumbnails() {
1448 if (mWebViewController.shouldCaptureThumbnails()) {
1449 synchronized (Tab.this) {
1450 if (mCapture == null) {
1451 mCapture = Bitmap.createBitmap(mCaptureWidth, mCaptureHeight,
1452 Bitmap.Config.RGB_565);
Michael Kolbc3af0672011-08-09 10:24:41 -07001453 mCapture.eraseColor(Color.WHITE);
John Reck1cf4b792011-07-26 10:22:22 -07001454 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 long getId() {
1473 return mId;
1474 }
1475
Grace Kloba22ac16e2009-10-07 18:00:23 -07001476 /**
1477 * Sets the WebView for this tab, correctly removing the old WebView from
1478 * the container view.
1479 */
1480 void setWebView(WebView w) {
1481 if (mMainView == w) {
1482 return;
1483 }
Michael Kolba713ec82010-11-29 17:27:06 -08001484
Grace Kloba22ac16e2009-10-07 18:00:23 -07001485 // If the WebView is changing, the page will be reloaded, so any ongoing
1486 // Geolocation permission requests are void.
Grace Kloba50c241e2010-04-20 11:07:50 -07001487 if (mGeolocationPermissionsPrompt != null) {
1488 mGeolocationPermissionsPrompt.hide();
1489 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001490
Michael Kolba713ec82010-11-29 17:27:06 -08001491 mWebViewController.onSetWebView(this, w);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001492
John Reck1cf4b792011-07-26 10:22:22 -07001493 if (mMainView != null) {
John Reck8ee633f2011-08-09 16:00:35 -07001494 mMainView.setPictureListener(null);
John Reck1cf4b792011-07-26 10:22:22 -07001495 if (w != null) {
1496 syncCurrentState(w, null);
1497 } else {
1498 mCurrentState = new PageState(mContext, false);
1499 }
1500 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001501 // set the new one
1502 mMainView = w;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001503 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001504 if (mMainView != null) {
1505 mMainView.setWebViewClient(mWebViewClient);
1506 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001507 // Attach DownloadManager so that downloads can start in an active
1508 // or a non-active window. This can happen when going to a site that
1509 // does a redirect after a period of time. The user could have
1510 // switched to another tab while waiting for the download to start.
1511 mMainView.setDownloadListener(mDownloadListener);
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001512 mMainView.setWebBackForwardListClient(mWebBackForwardListClient);
John Reck8ee633f2011-08-09 16:00:35 -07001513 TabControl tc = mWebViewController.getTabControl();
1514 if (tc != null && tc.getOnThumbnailUpdatedListener() != null) {
1515 mMainView.setPictureListener(this);
1516 }
John Reck1cf4b792011-07-26 10:22:22 -07001517 if (mSavedState != null) {
John Reck6c2e2f32011-08-22 13:41:23 -07001518 WebBackForwardList restoredState
1519 = mMainView.restoreState(mSavedState);
1520 if (restoredState == null || restoredState.getSize() == 0) {
1521 Log.w(LOGTAG, "Failed to restore WebView state!");
1522 loadUrl(mCurrentState.mOriginalUrl, null);
1523 }
John Reck1cf4b792011-07-26 10:22:22 -07001524 mSavedState = null;
1525 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001526 }
1527 }
1528
1529 /**
1530 * Destroy the tab's main WebView and subWindow if any
1531 */
1532 void destroy() {
1533 if (mMainView != null) {
1534 dismissSubWindow();
John Reckef654f12011-07-12 16:42:08 -07001535 // Make sure the embedded title bar isn't still attached
1536 mMainView.setEmbeddedTitleBar(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001537 // save the WebView to call destroy() after detach it from the tab
1538 WebView webView = mMainView;
1539 setWebView(null);
1540 webView.destroy();
1541 }
1542 }
1543
1544 /**
1545 * Remove the tab from the parent
1546 */
1547 void removeFromTree() {
1548 // detach the children
Michael Kolbc831b632011-05-11 09:30:34 -07001549 if (mChildren != null) {
1550 for(Tab t : mChildren) {
1551 t.setParent(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001552 }
1553 }
1554 // remove itself from the parent list
Michael Kolbc831b632011-05-11 09:30:34 -07001555 if (mParent != null) {
1556 mParent.mChildren.remove(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001557 }
John Reck1cf4b792011-07-26 10:22:22 -07001558 deleteThumbnail();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001559 }
1560
1561 /**
1562 * Create a new subwindow unless a subwindow already exists.
1563 * @return True if a new subwindow was created. False if one already exists.
1564 */
1565 boolean createSubWindow() {
1566 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001567 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001568 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001569 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001570 mSubView.setWebChromeClient(new SubWindowChromeClient(
1571 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001572 // Set a different DownloadListener for the mSubView, since it will
1573 // just need to dismiss the mSubView, rather than close the Tab
1574 mSubView.setDownloadListener(new DownloadListener() {
1575 public void onDownloadStart(String url, String userAgent,
1576 String contentDisposition, String mimetype,
1577 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001578 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001579 contentDisposition, mimetype, contentLength);
1580 if (mSubView.copyBackForwardList().getSize() == 0) {
1581 // This subwindow was opened for the sole purpose of
1582 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001583 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001584 }
1585 }
1586 });
Michael Kolb14612442011-06-24 13:06:29 -07001587 mSubView.setOnCreateContextMenuListener(mWebViewController.getActivity());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001588 return true;
1589 }
1590 return false;
1591 }
1592
1593 /**
1594 * Dismiss the subWindow for the tab.
1595 */
1596 void dismissSubWindow() {
1597 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001598 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001599 mSubView.destroy();
1600 mSubView = null;
1601 mSubViewContainer = null;
1602 }
1603 }
1604
Grace Kloba22ac16e2009-10-07 18:00:23 -07001605
1606 /**
1607 * Set the parent tab of this tab.
1608 */
Michael Kolbc831b632011-05-11 09:30:34 -07001609 void setParent(Tab parent) {
John Reck52be4782011-08-26 15:37:29 -07001610 if (parent == this) {
1611 throw new IllegalStateException("Cannot set parent to self!");
1612 }
Michael Kolbc831b632011-05-11 09:30:34 -07001613 mParent = parent;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001614 // This tab may have been freed due to low memory. If that is the case,
Michael Kolbc831b632011-05-11 09:30:34 -07001615 // the parent tab id is already saved. If we are changing that id
Grace Kloba22ac16e2009-10-07 18:00:23 -07001616 // (most likely due to removing the parent tab) we must update the
Michael Kolbc831b632011-05-11 09:30:34 -07001617 // parent tab id in the saved Bundle.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001618 if (mSavedState != null) {
1619 if (parent == null) {
1620 mSavedState.remove(PARENTTAB);
1621 } else {
Michael Kolbc831b632011-05-11 09:30:34 -07001622 mSavedState.putLong(PARENTTAB, parent.getId());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001623 }
1624 }
John Reckb0a86db2011-05-24 14:05:58 -07001625
1626 // Sync the WebView useragent with the parent
1627 if (parent != null && mSettings.hasDesktopUseragent(parent.getWebView())
1628 != mSettings.hasDesktopUseragent(getWebView())) {
1629 mSettings.toggleDesktopUseragent(getWebView());
1630 }
John Reck52be4782011-08-26 15:37:29 -07001631
1632 if (parent != null && parent.getId() == getId()) {
1633 throw new IllegalStateException("Parent has same ID as child!");
1634 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001635 }
1636
1637 /**
Michael Kolbc831b632011-05-11 09:30:34 -07001638 * If this Tab was created through another Tab, then this method returns
1639 * that Tab.
1640 * @return the Tab parent or null
1641 */
1642 public Tab getParent() {
1643 return mParent;
1644 }
1645
1646 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001647 * When a Tab is created through the content of another Tab, then we
1648 * associate the Tabs.
1649 * @param child the Tab that was created from this Tab
1650 */
1651 void addChildTab(Tab child) {
Michael Kolbc831b632011-05-11 09:30:34 -07001652 if (mChildren == null) {
1653 mChildren = new Vector<Tab>();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001654 }
Michael Kolbc831b632011-05-11 09:30:34 -07001655 mChildren.add(child);
1656 child.setParent(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001657 }
1658
Michael Kolbc831b632011-05-11 09:30:34 -07001659 Vector<Tab> getChildren() {
1660 return mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001661 }
1662
1663 void resume() {
1664 if (mMainView != null) {
John Reck56c1fcf2011-08-17 10:15:16 -07001665 setupHwAcceleration(mMainView);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001666 mMainView.onResume();
1667 if (mSubView != null) {
1668 mSubView.onResume();
1669 }
1670 }
1671 }
1672
John Reck56c1fcf2011-08-17 10:15:16 -07001673 private void setupHwAcceleration(View web) {
1674 if (web == null) return;
1675 BrowserSettings settings = BrowserSettings.getInstance();
1676 if (settings.isHardwareAccelerated()) {
1677 web.setLayerType(View.LAYER_TYPE_NONE, null);
1678 } else {
1679 web.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
1680 }
1681 }
1682
Grace Kloba22ac16e2009-10-07 18:00:23 -07001683 void pause() {
1684 if (mMainView != null) {
1685 mMainView.onPause();
1686 if (mSubView != null) {
1687 mSubView.onPause();
1688 }
1689 }
1690 }
1691
1692 void putInForeground() {
John Reck8ee633f2011-08-09 16:00:35 -07001693 if (mInForeground) {
1694 return;
1695 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001696 mInForeground = true;
1697 resume();
Michael Kolb14612442011-06-24 13:06:29 -07001698 Activity activity = mWebViewController.getActivity();
1699 mMainView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001700 if (mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -07001701 mSubView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001702 }
1703 // Show the pending error dialog if the queue is not empty
1704 if (mQueuedErrors != null && mQueuedErrors.size() > 0) {
1705 showError(mQueuedErrors.getFirst());
1706 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001707 mWebViewController.bookmarkedStatusHasChanged(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001708 }
1709
1710 void putInBackground() {
John Reck8ee633f2011-08-09 16:00:35 -07001711 if (!mInForeground) {
1712 return;
1713 }
John Reck52be4782011-08-26 15:37:29 -07001714 capture();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001715 mInForeground = false;
1716 pause();
1717 mMainView.setOnCreateContextMenuListener(null);
1718 if (mSubView != null) {
1719 mSubView.setOnCreateContextMenuListener(null);
1720 }
1721 }
1722
Michael Kolb8233fac2010-10-26 16:08:53 -07001723 boolean inForeground() {
1724 return mInForeground;
1725 }
1726
Grace Kloba22ac16e2009-10-07 18:00:23 -07001727 /**
1728 * Return the top window of this tab; either the subwindow if it is not
1729 * null or the main window.
1730 * @return The top window of this tab.
1731 */
1732 WebView getTopWindow() {
1733 if (mSubView != null) {
1734 return mSubView;
1735 }
1736 return mMainView;
1737 }
1738
1739 /**
1740 * Return the main window of this tab. Note: if a tab is freed in the
1741 * background, this can return null. It is only guaranteed to be
1742 * non-null for the current tab.
1743 * @return The main WebView of this tab.
1744 */
1745 WebView getWebView() {
1746 return mMainView;
1747 }
1748
Michael Kolba713ec82010-11-29 17:27:06 -08001749 void setViewContainer(View container) {
1750 mContainer = container;
1751 }
1752
Michael Kolb8233fac2010-10-26 16:08:53 -07001753 View getViewContainer() {
1754 return mContainer;
1755 }
1756
Grace Kloba22ac16e2009-10-07 18:00:23 -07001757 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001758 * Return whether private browsing is enabled for the main window of
1759 * this tab.
1760 * @return True if private browsing is enabled.
1761 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001762 boolean isPrivateBrowsingEnabled() {
John Reck502a3532011-08-16 14:21:46 -07001763 return mCurrentState.mIncognito;
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001764 }
1765
1766 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001767 * Return the subwindow of this tab or null if there is no subwindow.
1768 * @return The subwindow of this tab or null.
1769 */
1770 WebView getSubWebView() {
1771 return mSubView;
1772 }
1773
Michael Kolb1514bb72010-11-22 09:11:48 -08001774 void setSubWebView(WebView subView) {
1775 mSubView = subView;
1776 }
1777
Michael Kolb8233fac2010-10-26 16:08:53 -07001778 View getSubViewContainer() {
1779 return mSubViewContainer;
1780 }
1781
Michael Kolb1514bb72010-11-22 09:11:48 -08001782 void setSubViewContainer(View subViewContainer) {
1783 mSubViewContainer = subViewContainer;
1784 }
1785
Grace Kloba22ac16e2009-10-07 18:00:23 -07001786 /**
1787 * @return The geolocation permissions prompt for this tab.
1788 */
1789 GeolocationPermissionsPrompt getGeolocationPermissionsPrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001790 if (mGeolocationPermissionsPrompt == null) {
1791 ViewStub stub = (ViewStub) mContainer
1792 .findViewById(R.id.geolocation_permissions_prompt);
1793 mGeolocationPermissionsPrompt = (GeolocationPermissionsPrompt) stub
1794 .inflate();
Grace Kloba50c241e2010-04-20 11:07:50 -07001795 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001796 return mGeolocationPermissionsPrompt;
1797 }
1798
1799 /**
1800 * @return The application id string
1801 */
1802 String getAppId() {
1803 return mAppId;
1804 }
1805
1806 /**
1807 * Set the application id string
1808 * @param id
1809 */
1810 void setAppId(String id) {
1811 mAppId = id;
1812 }
1813
Michael Kolbe28b3472011-08-04 16:54:31 -07001814 boolean closeOnBack() {
1815 return mCloseOnBack;
1816 }
1817
1818 void setCloseOnBack(boolean close) {
1819 mCloseOnBack = close;
1820 }
1821
Grace Kloba22ac16e2009-10-07 18:00:23 -07001822 String getUrl() {
John Reck324d4402011-01-11 16:56:42 -08001823 return UrlUtils.filteredUrl(mCurrentState.mUrl);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001824 }
1825
John Reck49a603c2011-03-03 09:33:05 -08001826 String getOriginalUrl() {
John Reckdb22ec42011-06-29 11:31:24 -07001827 if (mCurrentState.mOriginalUrl == null) {
1828 return getUrl();
John Reck49a603c2011-03-03 09:33:05 -08001829 }
John Reckdb22ec42011-06-29 11:31:24 -07001830 return UrlUtils.filteredUrl(mCurrentState.mOriginalUrl);
John Reck49a603c2011-03-03 09:33:05 -08001831 }
1832
Grace Kloba22ac16e2009-10-07 18:00:23 -07001833 /**
John Reck30c714c2010-12-16 17:30:34 -08001834 * Get the title of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001835 */
1836 String getTitle() {
John Reck30c714c2010-12-16 17:30:34 -08001837 if (mCurrentState.mTitle == null && mInPageLoad) {
Michael Kolb14612442011-06-24 13:06:29 -07001838 return mContext.getString(R.string.title_bar_loading);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001839 }
John Reck30c714c2010-12-16 17:30:34 -08001840 return mCurrentState.mTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001841 }
1842
1843 /**
John Reck30c714c2010-12-16 17:30:34 -08001844 * Get the favicon of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001845 */
1846 Bitmap getFavicon() {
John Reck1cf4b792011-07-26 10:22:22 -07001847 if (mCurrentState.mFavicon != null) {
1848 return mCurrentState.mFavicon;
1849 }
1850 return getDefaultFavicon(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001851 }
1852
John Recke969cc52010-12-21 17:24:43 -08001853 public boolean isBookmarkedSite() {
1854 return mCurrentState.mIsBookmarkedSite;
1855 }
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001856
Grace Kloba22ac16e2009-10-07 18:00:23 -07001857 /**
1858 * Return the tab's error console. Creates the console if createIfNEcessary
1859 * is true and we haven't already created the console.
1860 * @param createIfNecessary Flag to indicate if the console should be
1861 * created if it has not been already.
1862 * @return The tab's error console, or null if one has not been created and
1863 * createIfNecessary is false.
1864 */
1865 ErrorConsoleView getErrorConsole(boolean createIfNecessary) {
1866 if (createIfNecessary && mErrorConsole == null) {
Michael Kolb14612442011-06-24 13:06:29 -07001867 mErrorConsole = new ErrorConsoleView(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001868 mErrorConsole.setWebView(mMainView);
1869 }
1870 return mErrorConsole;
1871 }
1872
John Reck30c714c2010-12-16 17:30:34 -08001873 private void setLockIconType(LockIcon icon) {
1874 mCurrentState.mLockIcon = icon;
1875 mWebViewController.onUpdatedLockIcon(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001876 }
1877
1878 /**
1879 * @return The tab's lock icon type.
1880 */
John Reck30c714c2010-12-16 17:30:34 -08001881 LockIcon getLockIconType() {
1882 return mCurrentState.mLockIcon;
1883 }
1884
1885 int getLoadProgress() {
1886 if (mInPageLoad) {
1887 return mPageLoadProgress;
1888 }
1889 return 100;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001890 }
1891
1892 /**
1893 * @return TRUE if onPageStarted is called while onPageFinished is not
1894 * called yet.
1895 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001896 boolean inPageLoad() {
1897 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001898 }
1899
1900 // force mInLoad to be false. This should only be called before closing the
1901 // tab to ensure BrowserActivity's pauseWebViewTimers() is called correctly.
Michael Kolb8233fac2010-10-26 16:08:53 -07001902 void clearInPageLoad() {
1903 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001904 }
1905
Grace Kloba22ac16e2009-10-07 18:00:23 -07001906 /**
John Reck1cf4b792011-07-26 10:22:22 -07001907 * @return The Bundle with the tab's state if it can be saved, otherwise null
Grace Kloba22ac16e2009-10-07 18:00:23 -07001908 */
John Reck1cf4b792011-07-26 10:22:22 -07001909 public Bundle saveState() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001910 // If the WebView is null it means we ran low on memory and we already
1911 // stored the saved state in mSavedState.
1912 if (mMainView == null) {
John Reck1cf4b792011-07-26 10:22:22 -07001913 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001914 }
John Reck6c2e2f32011-08-22 13:41:23 -07001915
1916 if (TextUtils.isEmpty(mCurrentState.mUrl)) {
John Reck1cf4b792011-07-26 10:22:22 -07001917 return null;
John Reck24f18262011-06-17 14:47:20 -07001918 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001919
1920 mSavedState = new Bundle();
John Reck6c2e2f32011-08-22 13:41:23 -07001921 WebBackForwardList savedList = mMainView.saveState(mSavedState);
1922 if (savedList == null || savedList.getSize() == 0) {
1923 Log.w(LOGTAG, "Failed to save back/forward list for "
1924 + mCurrentState.mUrl);
1925 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001926
Michael Kolbc831b632011-05-11 09:30:34 -07001927 mSavedState.putLong(ID, mId);
John Reck30c714c2010-12-16 17:30:34 -08001928 mSavedState.putString(CURRURL, mCurrentState.mUrl);
1929 mSavedState.putString(CURRTITLE, mCurrentState.mTitle);
John Reck1cf4b792011-07-26 10:22:22 -07001930 mSavedState.putBoolean(INCOGNITO, mMainView.isPrivateBrowsingEnabled());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001931 if (mAppId != null) {
1932 mSavedState.putString(APPID, mAppId);
1933 }
Michael Kolbe28b3472011-08-04 16:54:31 -07001934 mSavedState.putBoolean(CLOSEFLAG, mCloseOnBack);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001935 // Remember the parent tab so the relationship can be restored.
Michael Kolbc831b632011-05-11 09:30:34 -07001936 if (mParent != null) {
1937 mSavedState.putLong(PARENTTAB, mParent.mId);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001938 }
John Reckb0a86db2011-05-24 14:05:58 -07001939 mSavedState.putBoolean(USERAGENT,
1940 mSettings.hasDesktopUseragent(getWebView()));
John Reck1cf4b792011-07-26 10:22:22 -07001941 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001942 }
1943
1944 /*
1945 * Restore the state of the tab.
1946 */
John Reck1cf4b792011-07-26 10:22:22 -07001947 private void restoreState(Bundle b) {
1948 mSavedState = b;
1949 if (mSavedState == null) {
1950 return;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001951 }
1952 // Restore the internal state even if the WebView fails to restore.
1953 // This will maintain the app id, original url and close-on-exit values.
Michael Kolbc831b632011-05-11 09:30:34 -07001954 mId = b.getLong(ID);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001955 mAppId = b.getString(APPID);
Michael Kolbe28b3472011-08-04 16:54:31 -07001956 mCloseOnBack = b.getBoolean(CLOSEFLAG);
John Reckb0a86db2011-05-24 14:05:58 -07001957 if (b.getBoolean(USERAGENT)
1958 != mSettings.hasDesktopUseragent(getWebView())) {
1959 mSettings.toggleDesktopUseragent(getWebView());
1960 }
John Reck1cf4b792011-07-26 10:22:22 -07001961 String url = b.getString(CURRURL);
1962 String title = b.getString(CURRTITLE);
1963 boolean incognito = b.getBoolean(INCOGNITO);
1964 mCurrentState = new PageState(mContext, incognito, url, null);
1965 mCurrentState.mTitle = title;
1966 synchronized (Tab.this) {
1967 if (mCapture != null) {
1968 BackgroundHandler.execute(mLoadThumbnail);
1969 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001970 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001971 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04001972
Leon Scroggins1961ed22010-12-07 15:22:21 -05001973 public void updateBookmarkedStatus() {
John Recke969cc52010-12-21 17:24:43 -08001974 mDataController.queryBookmarkStatus(getUrl(), mIsBookmarkCallback);
Leon Scroggins1961ed22010-12-07 15:22:21 -05001975 }
1976
John Recke969cc52010-12-21 17:24:43 -08001977 private DataController.OnQueryUrlIsBookmark mIsBookmarkCallback
1978 = new DataController.OnQueryUrlIsBookmark() {
1979 @Override
1980 public void onQueryUrlIsBookmark(String url, boolean isBookmark) {
1981 if (mCurrentState.mUrl.equals(url)) {
1982 mCurrentState.mIsBookmarkedSite = isBookmark;
1983 mWebViewController.bookmarkedStatusHasChanged(Tab.this);
1984 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001985 }
John Recke969cc52010-12-21 17:24:43 -08001986 };
Michael Kolb1acef692011-03-08 14:12:06 -08001987
Michael Kolbeb95db42011-03-03 10:38:40 -08001988 public Bitmap getScreenshot() {
John Reck1cf4b792011-07-26 10:22:22 -07001989 synchronized (Tab.this) {
1990 return mCapture;
1991 }
Michael Kolbeb95db42011-03-03 10:38:40 -08001992 }
1993
John Reck541f55a2011-06-07 16:34:43 -07001994 public boolean isSnapshot() {
John Reck541f55a2011-06-07 16:34:43 -07001995 return false;
1996 }
1997
John Reckd8c74522011-06-14 08:45:00 -07001998 public ContentValues createSnapshotValues() {
1999 if (mMainView == null) return null;
John Reck8cc92352011-07-06 17:41:52 -07002000 ByteArrayOutputStream bos = new ByteArrayOutputStream();
2001 try {
2002 GZIPOutputStream stream = new GZIPOutputStream(bos);
2003 if (!mMainView.saveViewState(stream)) {
2004 return null;
2005 }
2006 stream.flush();
2007 stream.close();
2008 } catch (Exception e) {
2009 Log.w(LOGTAG, "Failed to save view state", e);
John Reck541f55a2011-06-07 16:34:43 -07002010 return null;
2011 }
John Reck8cc92352011-07-06 17:41:52 -07002012 byte[] data = bos.toByteArray();
John Reckd8c74522011-06-14 08:45:00 -07002013 ContentValues values = new ContentValues();
2014 values.put(Snapshots.TITLE, mCurrentState.mTitle);
2015 values.put(Snapshots.URL, mCurrentState.mUrl);
2016 values.put(Snapshots.VIEWSTATE, data);
2017 values.put(Snapshots.BACKGROUND, mMainView.getPageBackgroundColor());
John Reck8cc92352011-07-06 17:41:52 -07002018 values.put(Snapshots.DATE_CREATED, System.currentTimeMillis());
2019 values.put(Snapshots.FAVICON, compressBitmap(getFavicon()));
2020 Bitmap screenshot = Controller.createScreenshot(mMainView,
2021 Controller.getDesiredThumbnailWidth(mContext),
2022 Controller.getDesiredThumbnailHeight(mContext));
2023 values.put(Snapshots.THUMBNAIL, compressBitmap(screenshot));
John Reckd8c74522011-06-14 08:45:00 -07002024 return values;
John Reck541f55a2011-06-07 16:34:43 -07002025 }
2026
John Reck8cc92352011-07-06 17:41:52 -07002027 public byte[] compressBitmap(Bitmap bitmap) {
2028 if (bitmap == null) {
2029 return null;
2030 }
2031 ByteArrayOutputStream stream = new ByteArrayOutputStream();
2032 bitmap.compress(CompressFormat.PNG, 100, stream);
2033 return stream.toByteArray();
2034 }
2035
John Reck26b18322011-06-21 13:08:58 -07002036 public void loadUrl(String url, Map<String, String> headers) {
2037 if (mMainView != null) {
Michael Kolb14612442011-06-24 13:06:29 -07002038 mCurrentState = new PageState(mContext, false, url, null);
John Reck26b18322011-06-21 13:08:58 -07002039 mWebViewController.onPageStarted(this, mMainView, null);
2040 mMainView.loadUrl(url, headers);
2041 }
2042 }
2043
Michael Kolb9ef259a2011-07-12 15:33:08 -07002044 protected void capture() {
2045 if (mMainView == null || mCapture == null) return;
2046 Canvas c = new Canvas(mCapture);
2047 final int left = mMainView.getScrollX();
2048 final int top = mMainView.getScrollY() + mMainView.getVisibleTitleHeight();
2049 c.translate(-left, -top);
2050 float scale = mCaptureWidth / (float) mMainView.getWidth();
2051 c.scale(scale, scale, left, top);
John Reck8ee633f2011-08-09 16:00:35 -07002052 if (mMainView instanceof BrowserWebView) {
2053 ((BrowserWebView)mMainView).drawContent(c);
2054 } else {
2055 mMainView.draw(c);
2056 }
Dianne Hackborn43cfe8a2011-08-02 16:59:35 -07002057 c.setBitmap(null);
John Reck8ee633f2011-08-09 16:00:35 -07002058 mHandler.removeMessages(MSG_CAPTURE);
John Reck1cf4b792011-07-26 10:22:22 -07002059 persistThumbnail();
John Reck8ee633f2011-08-09 16:00:35 -07002060 TabControl tc = mWebViewController.getTabControl();
2061 if (tc != null) {
2062 OnThumbnailUpdatedListener updateListener
2063 = tc.getOnThumbnailUpdatedListener();
2064 if (updateListener != null) {
2065 updateListener.onThumbnailUpdated(this);
2066 }
2067 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07002068 }
2069
2070 @Override
2071 public void onNewPicture(WebView view, Picture picture) {
2072 //update screenshot
John Reck1cf4b792011-07-26 10:22:22 -07002073 postCapture();
2074 }
2075
2076 private void postCapture() {
Michael Kolb9ef259a2011-07-12 15:33:08 -07002077 if (!mHandler.hasMessages(MSG_CAPTURE)) {
2078 mHandler.sendEmptyMessageDelayed(MSG_CAPTURE, CAPTURE_DELAY);
2079 }
2080 }
2081
John Reckef654f12011-07-12 16:42:08 -07002082 public boolean canGoBack() {
2083 return mMainView != null ? mMainView.canGoBack() : false;
2084 }
2085
2086 public boolean canGoForward() {
2087 return mMainView != null ? mMainView.canGoForward() : false;
2088 }
2089
2090 public void goBack() {
2091 if (mMainView != null) {
2092 mMainView.goBack();
2093 }
2094 }
2095
2096 public void goForward() {
2097 if (mMainView != null) {
2098 mMainView.goForward();
2099 }
2100 }
2101
Mathew Inwood1dd8e822011-08-03 14:34:29 +01002102 /**
2103 * Causes the tab back/forward stack to be cleared once, if the given URL is the next URL
2104 * to be added to the stack.
2105 *
2106 * This is used to ensure that preloaded URLs that are not subsequently seen by the user do
2107 * not appear in the back stack.
2108 */
2109 public void clearBackStackWhenItemAdded(String urlToMatch) {
2110 mClearHistoryMatchUrl = urlToMatch;
2111 }
2112
John Reck1cf4b792011-07-26 10:22:22 -07002113 protected void persistThumbnail() {
2114 BackgroundHandler.execute(mSaveThumbnail);
2115 }
2116
2117 protected void deleteThumbnail() {
2118 BackgroundHandler.execute(mDeleteThumbnail);
2119 }
2120
2121 private void updateCaptureFromBlob(byte[] blob) {
2122 synchronized (Tab.this) {
2123 if (mCapture == null) {
2124 return;
2125 }
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002126 ByteBuffer buffer = ByteBuffer.wrap(blob);
Michael Kolbd837a112011-08-09 14:00:18 -07002127 try {
2128 mCapture.copyPixelsFromBuffer(buffer);
2129 } catch (RuntimeException rex) {
2130 Log.e(LOGTAG, "Load capture has mismatched sizes; buffer: "
2131 + buffer.capacity() + " blob: " + blob.length
2132 + "capture: " + mCapture.getByteCount());
2133 throw rex;
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002134 }
John Reck1cf4b792011-07-26 10:22:22 -07002135 }
2136 }
2137
2138 private byte[] getCaptureBlob() {
2139 synchronized (Tab.this) {
2140 if (mCapture == null) {
2141 return null;
2142 }
2143 ByteBuffer buffer = ByteBuffer.allocate(mCapture.getByteCount());
2144 mCapture.copyPixelsToBuffer(buffer);
2145 return buffer.array();
2146 }
2147 }
2148
2149 private Runnable mSaveThumbnail = new Runnable() {
2150
2151 @Override
2152 public void run() {
2153 byte[] blob = getCaptureBlob();
2154 if (blob == null) {
2155 return;
2156 }
2157 ContentResolver cr = mContext.getContentResolver();
2158 ContentValues values = new ContentValues();
2159 values.put(Thumbnails._ID, mId);
2160 values.put(Thumbnails.THUMBNAIL, blob);
2161 cr.insert(Thumbnails.CONTENT_URI, values);
2162 }
2163 };
2164
2165 private Runnable mDeleteThumbnail = new Runnable() {
2166
2167 @Override
2168 public void run() {
2169 ContentResolver cr = mContext.getContentResolver();
2170 try {
2171 cr.delete(ContentUris.withAppendedId(Thumbnails.CONTENT_URI, mId),
2172 null, null);
2173 } catch (Throwable t) {}
2174 }
2175 };
2176
2177 private Runnable mLoadThumbnail = new Runnable() {
2178
2179 @Override
2180 public void run() {
2181 ContentResolver cr = mContext.getContentResolver();
2182 Cursor c = null;
2183 try {
2184 Uri uri = ContentUris.withAppendedId(Thumbnails.CONTENT_URI, mId);
2185 c = cr.query(uri, new String[] {Thumbnails._ID,
2186 Thumbnails.THUMBNAIL}, null, null, null);
2187 if (c.moveToFirst()) {
2188 byte[] data = c.getBlob(1);
2189 if (data != null && data.length > 0) {
2190 updateCaptureFromBlob(data);
2191 }
2192 }
2193 } finally {
2194 if (c != null) {
2195 c.close();
2196 }
2197 }
2198 }
2199 };
2200
John Reck52be4782011-08-26 15:37:29 -07002201 @Override
2202 public String toString() {
2203 StringBuilder builder = new StringBuilder(100);
2204 builder.append(mId);
2205 builder.append(") has parent: ");
2206 if (getParent() != null) {
2207 builder.append("true[");
2208 builder.append(getParent().getId());
2209 builder.append("]");
2210 } else {
2211 builder.append("false");
2212 }
2213 builder.append(", incog: ");
2214 builder.append(isPrivateBrowsingEnabled());
2215 if (!isPrivateBrowsingEnabled()) {
2216 builder.append(", title: ");
2217 builder.append(getTitle());
2218 builder.append(", url: ");
2219 builder.append(getUrl());
2220 }
2221 return builder.toString();
2222 }
2223
Grace Kloba22ac16e2009-10-07 18:00:23 -07002224}