blob: 69213cd56e20edde4d70b4342bd3e6f297ab4f7f [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
John Reck1cf4b792011-07-26 10:22:22 -07001438 public void updateShouldCaptureThumbnails() {
1439 if (mWebViewController.shouldCaptureThumbnails()) {
1440 synchronized (Tab.this) {
1441 if (mCapture == null) {
1442 mCapture = Bitmap.createBitmap(mCaptureWidth, mCaptureHeight,
1443 Bitmap.Config.RGB_565);
Michael Kolbc3af0672011-08-09 10:24:41 -07001444 mCapture.eraseColor(Color.WHITE);
John Reck1cf4b792011-07-26 10:22:22 -07001445 if (mInForeground) {
1446 postCapture();
1447 }
1448 }
1449 }
1450 } else {
1451 synchronized (Tab.this) {
1452 mCapture = null;
1453 deleteThumbnail();
1454 }
1455 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001456 }
1457
Michael Kolb14612442011-06-24 13:06:29 -07001458 public void setController(WebViewController ctl) {
1459 mWebViewController = ctl;
John Reck1cf4b792011-07-26 10:22:22 -07001460 updateShouldCaptureThumbnails();
Michael Kolb14612442011-06-24 13:06:29 -07001461 }
1462
Michael Kolbc831b632011-05-11 09:30:34 -07001463 public long getId() {
1464 return mId;
1465 }
1466
Grace Kloba22ac16e2009-10-07 18:00:23 -07001467 /**
1468 * Sets the WebView for this tab, correctly removing the old WebView from
1469 * the container view.
1470 */
1471 void setWebView(WebView w) {
1472 if (mMainView == w) {
1473 return;
1474 }
Michael Kolba713ec82010-11-29 17:27:06 -08001475
Grace Kloba22ac16e2009-10-07 18:00:23 -07001476 // If the WebView is changing, the page will be reloaded, so any ongoing
1477 // Geolocation permission requests are void.
Grace Kloba50c241e2010-04-20 11:07:50 -07001478 if (mGeolocationPermissionsPrompt != null) {
1479 mGeolocationPermissionsPrompt.hide();
1480 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001481
Michael Kolba713ec82010-11-29 17:27:06 -08001482 mWebViewController.onSetWebView(this, w);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001483
John Reck1cf4b792011-07-26 10:22:22 -07001484 if (mMainView != null) {
John Reck8ee633f2011-08-09 16:00:35 -07001485 mMainView.setPictureListener(null);
John Reck1cf4b792011-07-26 10:22:22 -07001486 if (w != null) {
1487 syncCurrentState(w, null);
1488 } else {
1489 mCurrentState = new PageState(mContext, false);
1490 }
1491 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001492 // set the new one
1493 mMainView = w;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001494 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001495 if (mMainView != null) {
1496 mMainView.setWebViewClient(mWebViewClient);
1497 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001498 // Attach DownloadManager so that downloads can start in an active
1499 // or a non-active window. This can happen when going to a site that
1500 // does a redirect after a period of time. The user could have
1501 // switched to another tab while waiting for the download to start.
1502 mMainView.setDownloadListener(mDownloadListener);
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001503 mMainView.setWebBackForwardListClient(mWebBackForwardListClient);
John Reck8ee633f2011-08-09 16:00:35 -07001504 TabControl tc = mWebViewController.getTabControl();
1505 if (tc != null && tc.getOnThumbnailUpdatedListener() != null) {
1506 mMainView.setPictureListener(this);
1507 }
John Reck1cf4b792011-07-26 10:22:22 -07001508 if (mSavedState != null) {
John Reck6c2e2f32011-08-22 13:41:23 -07001509 WebBackForwardList restoredState
1510 = mMainView.restoreState(mSavedState);
1511 if (restoredState == null || restoredState.getSize() == 0) {
1512 Log.w(LOGTAG, "Failed to restore WebView state!");
1513 loadUrl(mCurrentState.mOriginalUrl, null);
1514 }
John Reck1cf4b792011-07-26 10:22:22 -07001515 mSavedState = null;
1516 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001517 }
1518 }
1519
1520 /**
1521 * Destroy the tab's main WebView and subWindow if any
1522 */
1523 void destroy() {
1524 if (mMainView != null) {
1525 dismissSubWindow();
John Reckef654f12011-07-12 16:42:08 -07001526 // Make sure the embedded title bar isn't still attached
1527 mMainView.setEmbeddedTitleBar(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001528 // save the WebView to call destroy() after detach it from the tab
1529 WebView webView = mMainView;
1530 setWebView(null);
1531 webView.destroy();
1532 }
1533 }
1534
1535 /**
1536 * Remove the tab from the parent
1537 */
1538 void removeFromTree() {
1539 // detach the children
Michael Kolbc831b632011-05-11 09:30:34 -07001540 if (mChildren != null) {
1541 for(Tab t : mChildren) {
1542 t.setParent(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001543 }
1544 }
1545 // remove itself from the parent list
Michael Kolbc831b632011-05-11 09:30:34 -07001546 if (mParent != null) {
1547 mParent.mChildren.remove(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001548 }
John Reck1cf4b792011-07-26 10:22:22 -07001549 deleteThumbnail();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001550 }
1551
1552 /**
1553 * Create a new subwindow unless a subwindow already exists.
1554 * @return True if a new subwindow was created. False if one already exists.
1555 */
1556 boolean createSubWindow() {
1557 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001558 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001559 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001560 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001561 mSubView.setWebChromeClient(new SubWindowChromeClient(
1562 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001563 // Set a different DownloadListener for the mSubView, since it will
1564 // just need to dismiss the mSubView, rather than close the Tab
1565 mSubView.setDownloadListener(new DownloadListener() {
1566 public void onDownloadStart(String url, String userAgent,
1567 String contentDisposition, String mimetype,
1568 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001569 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001570 contentDisposition, mimetype, contentLength);
1571 if (mSubView.copyBackForwardList().getSize() == 0) {
1572 // This subwindow was opened for the sole purpose of
1573 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001574 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001575 }
1576 }
1577 });
Michael Kolb14612442011-06-24 13:06:29 -07001578 mSubView.setOnCreateContextMenuListener(mWebViewController.getActivity());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001579 return true;
1580 }
1581 return false;
1582 }
1583
1584 /**
1585 * Dismiss the subWindow for the tab.
1586 */
1587 void dismissSubWindow() {
1588 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001589 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001590 mSubView.destroy();
1591 mSubView = null;
1592 mSubViewContainer = null;
1593 }
1594 }
1595
Grace Kloba22ac16e2009-10-07 18:00:23 -07001596
1597 /**
1598 * Set the parent tab of this tab.
1599 */
Michael Kolbc831b632011-05-11 09:30:34 -07001600 void setParent(Tab parent) {
John Reck52be4782011-08-26 15:37:29 -07001601 if (parent == this) {
1602 throw new IllegalStateException("Cannot set parent to self!");
1603 }
Michael Kolbc831b632011-05-11 09:30:34 -07001604 mParent = parent;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001605 // This tab may have been freed due to low memory. If that is the case,
Michael Kolbc831b632011-05-11 09:30:34 -07001606 // the parent tab id is already saved. If we are changing that id
Grace Kloba22ac16e2009-10-07 18:00:23 -07001607 // (most likely due to removing the parent tab) we must update the
Michael Kolbc831b632011-05-11 09:30:34 -07001608 // parent tab id in the saved Bundle.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001609 if (mSavedState != null) {
1610 if (parent == null) {
1611 mSavedState.remove(PARENTTAB);
1612 } else {
Michael Kolbc831b632011-05-11 09:30:34 -07001613 mSavedState.putLong(PARENTTAB, parent.getId());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001614 }
1615 }
John Reckb0a86db2011-05-24 14:05:58 -07001616
1617 // Sync the WebView useragent with the parent
1618 if (parent != null && mSettings.hasDesktopUseragent(parent.getWebView())
1619 != mSettings.hasDesktopUseragent(getWebView())) {
1620 mSettings.toggleDesktopUseragent(getWebView());
1621 }
John Reck52be4782011-08-26 15:37:29 -07001622
1623 if (parent != null && parent.getId() == getId()) {
1624 throw new IllegalStateException("Parent has same ID as child!");
1625 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001626 }
1627
1628 /**
Michael Kolbc831b632011-05-11 09:30:34 -07001629 * If this Tab was created through another Tab, then this method returns
1630 * that Tab.
1631 * @return the Tab parent or null
1632 */
1633 public Tab getParent() {
1634 return mParent;
1635 }
1636
1637 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001638 * When a Tab is created through the content of another Tab, then we
1639 * associate the Tabs.
1640 * @param child the Tab that was created from this Tab
1641 */
1642 void addChildTab(Tab child) {
Michael Kolbc831b632011-05-11 09:30:34 -07001643 if (mChildren == null) {
1644 mChildren = new Vector<Tab>();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001645 }
Michael Kolbc831b632011-05-11 09:30:34 -07001646 mChildren.add(child);
1647 child.setParent(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001648 }
1649
Michael Kolbc831b632011-05-11 09:30:34 -07001650 Vector<Tab> getChildren() {
1651 return mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001652 }
1653
1654 void resume() {
1655 if (mMainView != null) {
John Reck56c1fcf2011-08-17 10:15:16 -07001656 setupHwAcceleration(mMainView);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001657 mMainView.onResume();
1658 if (mSubView != null) {
1659 mSubView.onResume();
1660 }
1661 }
1662 }
1663
John Reck56c1fcf2011-08-17 10:15:16 -07001664 private void setupHwAcceleration(View web) {
1665 if (web == null) return;
1666 BrowserSettings settings = BrowserSettings.getInstance();
1667 if (settings.isHardwareAccelerated()) {
1668 web.setLayerType(View.LAYER_TYPE_NONE, null);
1669 } else {
1670 web.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
1671 }
1672 }
1673
Grace Kloba22ac16e2009-10-07 18:00:23 -07001674 void pause() {
1675 if (mMainView != null) {
1676 mMainView.onPause();
1677 if (mSubView != null) {
1678 mSubView.onPause();
1679 }
1680 }
1681 }
1682
1683 void putInForeground() {
John Reck8ee633f2011-08-09 16:00:35 -07001684 if (mInForeground) {
1685 return;
1686 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001687 mInForeground = true;
1688 resume();
Michael Kolb14612442011-06-24 13:06:29 -07001689 Activity activity = mWebViewController.getActivity();
1690 mMainView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001691 if (mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -07001692 mSubView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001693 }
1694 // Show the pending error dialog if the queue is not empty
1695 if (mQueuedErrors != null && mQueuedErrors.size() > 0) {
1696 showError(mQueuedErrors.getFirst());
1697 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001698 mWebViewController.bookmarkedStatusHasChanged(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001699 }
1700
1701 void putInBackground() {
John Reck8ee633f2011-08-09 16:00:35 -07001702 if (!mInForeground) {
1703 return;
1704 }
John Reck52be4782011-08-26 15:37:29 -07001705 capture();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001706 mInForeground = false;
1707 pause();
1708 mMainView.setOnCreateContextMenuListener(null);
1709 if (mSubView != null) {
1710 mSubView.setOnCreateContextMenuListener(null);
1711 }
1712 }
1713
Michael Kolb8233fac2010-10-26 16:08:53 -07001714 boolean inForeground() {
1715 return mInForeground;
1716 }
1717
Grace Kloba22ac16e2009-10-07 18:00:23 -07001718 /**
1719 * Return the top window of this tab; either the subwindow if it is not
1720 * null or the main window.
1721 * @return The top window of this tab.
1722 */
1723 WebView getTopWindow() {
1724 if (mSubView != null) {
1725 return mSubView;
1726 }
1727 return mMainView;
1728 }
1729
1730 /**
1731 * Return the main window of this tab. Note: if a tab is freed in the
1732 * background, this can return null. It is only guaranteed to be
1733 * non-null for the current tab.
1734 * @return The main WebView of this tab.
1735 */
1736 WebView getWebView() {
1737 return mMainView;
1738 }
1739
Michael Kolba713ec82010-11-29 17:27:06 -08001740 void setViewContainer(View container) {
1741 mContainer = container;
1742 }
1743
Michael Kolb8233fac2010-10-26 16:08:53 -07001744 View getViewContainer() {
1745 return mContainer;
1746 }
1747
Grace Kloba22ac16e2009-10-07 18:00:23 -07001748 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001749 * Return whether private browsing is enabled for the main window of
1750 * this tab.
1751 * @return True if private browsing is enabled.
1752 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001753 boolean isPrivateBrowsingEnabled() {
John Reck502a3532011-08-16 14:21:46 -07001754 return mCurrentState.mIncognito;
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001755 }
1756
1757 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001758 * Return the subwindow of this tab or null if there is no subwindow.
1759 * @return The subwindow of this tab or null.
1760 */
1761 WebView getSubWebView() {
1762 return mSubView;
1763 }
1764
Michael Kolb1514bb72010-11-22 09:11:48 -08001765 void setSubWebView(WebView subView) {
1766 mSubView = subView;
1767 }
1768
Michael Kolb8233fac2010-10-26 16:08:53 -07001769 View getSubViewContainer() {
1770 return mSubViewContainer;
1771 }
1772
Michael Kolb1514bb72010-11-22 09:11:48 -08001773 void setSubViewContainer(View subViewContainer) {
1774 mSubViewContainer = subViewContainer;
1775 }
1776
Grace Kloba22ac16e2009-10-07 18:00:23 -07001777 /**
1778 * @return The geolocation permissions prompt for this tab.
1779 */
1780 GeolocationPermissionsPrompt getGeolocationPermissionsPrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001781 if (mGeolocationPermissionsPrompt == null) {
1782 ViewStub stub = (ViewStub) mContainer
1783 .findViewById(R.id.geolocation_permissions_prompt);
1784 mGeolocationPermissionsPrompt = (GeolocationPermissionsPrompt) stub
1785 .inflate();
Grace Kloba50c241e2010-04-20 11:07:50 -07001786 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001787 return mGeolocationPermissionsPrompt;
1788 }
1789
1790 /**
1791 * @return The application id string
1792 */
1793 String getAppId() {
1794 return mAppId;
1795 }
1796
1797 /**
1798 * Set the application id string
1799 * @param id
1800 */
1801 void setAppId(String id) {
1802 mAppId = id;
1803 }
1804
Michael Kolbe28b3472011-08-04 16:54:31 -07001805 boolean closeOnBack() {
1806 return mCloseOnBack;
1807 }
1808
1809 void setCloseOnBack(boolean close) {
1810 mCloseOnBack = close;
1811 }
1812
Grace Kloba22ac16e2009-10-07 18:00:23 -07001813 String getUrl() {
John Reck324d4402011-01-11 16:56:42 -08001814 return UrlUtils.filteredUrl(mCurrentState.mUrl);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001815 }
1816
John Reck49a603c2011-03-03 09:33:05 -08001817 String getOriginalUrl() {
John Reckdb22ec42011-06-29 11:31:24 -07001818 if (mCurrentState.mOriginalUrl == null) {
1819 return getUrl();
John Reck49a603c2011-03-03 09:33:05 -08001820 }
John Reckdb22ec42011-06-29 11:31:24 -07001821 return UrlUtils.filteredUrl(mCurrentState.mOriginalUrl);
John Reck49a603c2011-03-03 09:33:05 -08001822 }
1823
Grace Kloba22ac16e2009-10-07 18:00:23 -07001824 /**
John Reck30c714c2010-12-16 17:30:34 -08001825 * Get the title of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001826 */
1827 String getTitle() {
John Reck30c714c2010-12-16 17:30:34 -08001828 if (mCurrentState.mTitle == null && mInPageLoad) {
Michael Kolb14612442011-06-24 13:06:29 -07001829 return mContext.getString(R.string.title_bar_loading);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001830 }
John Reck30c714c2010-12-16 17:30:34 -08001831 return mCurrentState.mTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001832 }
1833
1834 /**
John Reck30c714c2010-12-16 17:30:34 -08001835 * Get the favicon of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001836 */
1837 Bitmap getFavicon() {
John Reck1cf4b792011-07-26 10:22:22 -07001838 if (mCurrentState.mFavicon != null) {
1839 return mCurrentState.mFavicon;
1840 }
1841 return getDefaultFavicon(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001842 }
1843
John Recke969cc52010-12-21 17:24:43 -08001844 public boolean isBookmarkedSite() {
1845 return mCurrentState.mIsBookmarkedSite;
1846 }
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001847
Grace Kloba22ac16e2009-10-07 18:00:23 -07001848 /**
1849 * Return the tab's error console. Creates the console if createIfNEcessary
1850 * is true and we haven't already created the console.
1851 * @param createIfNecessary Flag to indicate if the console should be
1852 * created if it has not been already.
1853 * @return The tab's error console, or null if one has not been created and
1854 * createIfNecessary is false.
1855 */
1856 ErrorConsoleView getErrorConsole(boolean createIfNecessary) {
1857 if (createIfNecessary && mErrorConsole == null) {
Michael Kolb14612442011-06-24 13:06:29 -07001858 mErrorConsole = new ErrorConsoleView(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001859 mErrorConsole.setWebView(mMainView);
1860 }
1861 return mErrorConsole;
1862 }
1863
John Reck30c714c2010-12-16 17:30:34 -08001864 private void setLockIconType(LockIcon icon) {
1865 mCurrentState.mLockIcon = icon;
1866 mWebViewController.onUpdatedLockIcon(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001867 }
1868
1869 /**
1870 * @return The tab's lock icon type.
1871 */
John Reck30c714c2010-12-16 17:30:34 -08001872 LockIcon getLockIconType() {
1873 return mCurrentState.mLockIcon;
1874 }
1875
1876 int getLoadProgress() {
1877 if (mInPageLoad) {
1878 return mPageLoadProgress;
1879 }
1880 return 100;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001881 }
1882
1883 /**
1884 * @return TRUE if onPageStarted is called while onPageFinished is not
1885 * called yet.
1886 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001887 boolean inPageLoad() {
1888 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001889 }
1890
1891 // force mInLoad to be false. This should only be called before closing the
1892 // tab to ensure BrowserActivity's pauseWebViewTimers() is called correctly.
Michael Kolb8233fac2010-10-26 16:08:53 -07001893 void clearInPageLoad() {
1894 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001895 }
1896
Grace Kloba22ac16e2009-10-07 18:00:23 -07001897 /**
John Reck1cf4b792011-07-26 10:22:22 -07001898 * @return The Bundle with the tab's state if it can be saved, otherwise null
Grace Kloba22ac16e2009-10-07 18:00:23 -07001899 */
John Reck1cf4b792011-07-26 10:22:22 -07001900 public Bundle saveState() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001901 // If the WebView is null it means we ran low on memory and we already
1902 // stored the saved state in mSavedState.
1903 if (mMainView == null) {
John Reck1cf4b792011-07-26 10:22:22 -07001904 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001905 }
John Reck6c2e2f32011-08-22 13:41:23 -07001906
1907 if (TextUtils.isEmpty(mCurrentState.mUrl)) {
John Reck1cf4b792011-07-26 10:22:22 -07001908 return null;
John Reck24f18262011-06-17 14:47:20 -07001909 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001910
1911 mSavedState = new Bundle();
John Reck6c2e2f32011-08-22 13:41:23 -07001912 WebBackForwardList savedList = mMainView.saveState(mSavedState);
1913 if (savedList == null || savedList.getSize() == 0) {
1914 Log.w(LOGTAG, "Failed to save back/forward list for "
1915 + mCurrentState.mUrl);
1916 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001917
Michael Kolbc831b632011-05-11 09:30:34 -07001918 mSavedState.putLong(ID, mId);
John Reck30c714c2010-12-16 17:30:34 -08001919 mSavedState.putString(CURRURL, mCurrentState.mUrl);
1920 mSavedState.putString(CURRTITLE, mCurrentState.mTitle);
John Reck1cf4b792011-07-26 10:22:22 -07001921 mSavedState.putBoolean(INCOGNITO, mMainView.isPrivateBrowsingEnabled());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001922 if (mAppId != null) {
1923 mSavedState.putString(APPID, mAppId);
1924 }
Michael Kolbe28b3472011-08-04 16:54:31 -07001925 mSavedState.putBoolean(CLOSEFLAG, mCloseOnBack);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001926 // Remember the parent tab so the relationship can be restored.
Michael Kolbc831b632011-05-11 09:30:34 -07001927 if (mParent != null) {
1928 mSavedState.putLong(PARENTTAB, mParent.mId);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001929 }
John Reckb0a86db2011-05-24 14:05:58 -07001930 mSavedState.putBoolean(USERAGENT,
1931 mSettings.hasDesktopUseragent(getWebView()));
John Reck1cf4b792011-07-26 10:22:22 -07001932 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001933 }
1934
1935 /*
1936 * Restore the state of the tab.
1937 */
John Reck1cf4b792011-07-26 10:22:22 -07001938 private void restoreState(Bundle b) {
1939 mSavedState = b;
1940 if (mSavedState == null) {
1941 return;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001942 }
1943 // Restore the internal state even if the WebView fails to restore.
1944 // This will maintain the app id, original url and close-on-exit values.
Michael Kolbc831b632011-05-11 09:30:34 -07001945 mId = b.getLong(ID);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001946 mAppId = b.getString(APPID);
Michael Kolbe28b3472011-08-04 16:54:31 -07001947 mCloseOnBack = b.getBoolean(CLOSEFLAG);
John Reckb0a86db2011-05-24 14:05:58 -07001948 if (b.getBoolean(USERAGENT)
1949 != mSettings.hasDesktopUseragent(getWebView())) {
1950 mSettings.toggleDesktopUseragent(getWebView());
1951 }
John Reck1cf4b792011-07-26 10:22:22 -07001952 String url = b.getString(CURRURL);
1953 String title = b.getString(CURRTITLE);
1954 boolean incognito = b.getBoolean(INCOGNITO);
1955 mCurrentState = new PageState(mContext, incognito, url, null);
1956 mCurrentState.mTitle = title;
1957 synchronized (Tab.this) {
1958 if (mCapture != null) {
1959 BackgroundHandler.execute(mLoadThumbnail);
1960 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001961 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001962 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04001963
Leon Scroggins1961ed22010-12-07 15:22:21 -05001964 public void updateBookmarkedStatus() {
John Recke969cc52010-12-21 17:24:43 -08001965 mDataController.queryBookmarkStatus(getUrl(), mIsBookmarkCallback);
Leon Scroggins1961ed22010-12-07 15:22:21 -05001966 }
1967
John Recke969cc52010-12-21 17:24:43 -08001968 private DataController.OnQueryUrlIsBookmark mIsBookmarkCallback
1969 = new DataController.OnQueryUrlIsBookmark() {
1970 @Override
1971 public void onQueryUrlIsBookmark(String url, boolean isBookmark) {
1972 if (mCurrentState.mUrl.equals(url)) {
1973 mCurrentState.mIsBookmarkedSite = isBookmark;
1974 mWebViewController.bookmarkedStatusHasChanged(Tab.this);
1975 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001976 }
John Recke969cc52010-12-21 17:24:43 -08001977 };
Michael Kolb1acef692011-03-08 14:12:06 -08001978
Michael Kolbeb95db42011-03-03 10:38:40 -08001979 public Bitmap getScreenshot() {
John Reck1cf4b792011-07-26 10:22:22 -07001980 synchronized (Tab.this) {
1981 return mCapture;
1982 }
Michael Kolbeb95db42011-03-03 10:38:40 -08001983 }
1984
John Reck541f55a2011-06-07 16:34:43 -07001985 public boolean isSnapshot() {
John Reck541f55a2011-06-07 16:34:43 -07001986 return false;
1987 }
1988
John Reckd8c74522011-06-14 08:45:00 -07001989 public ContentValues createSnapshotValues() {
1990 if (mMainView == null) return null;
John Reck8cc92352011-07-06 17:41:52 -07001991 ByteArrayOutputStream bos = new ByteArrayOutputStream();
1992 try {
1993 GZIPOutputStream stream = new GZIPOutputStream(bos);
1994 if (!mMainView.saveViewState(stream)) {
1995 return null;
1996 }
1997 stream.flush();
1998 stream.close();
1999 } catch (Exception e) {
2000 Log.w(LOGTAG, "Failed to save view state", e);
John Reck541f55a2011-06-07 16:34:43 -07002001 return null;
2002 }
John Reck8cc92352011-07-06 17:41:52 -07002003 byte[] data = bos.toByteArray();
John Reckd8c74522011-06-14 08:45:00 -07002004 ContentValues values = new ContentValues();
2005 values.put(Snapshots.TITLE, mCurrentState.mTitle);
2006 values.put(Snapshots.URL, mCurrentState.mUrl);
2007 values.put(Snapshots.VIEWSTATE, data);
2008 values.put(Snapshots.BACKGROUND, mMainView.getPageBackgroundColor());
John Reck8cc92352011-07-06 17:41:52 -07002009 values.put(Snapshots.DATE_CREATED, System.currentTimeMillis());
2010 values.put(Snapshots.FAVICON, compressBitmap(getFavicon()));
2011 Bitmap screenshot = Controller.createScreenshot(mMainView,
2012 Controller.getDesiredThumbnailWidth(mContext),
2013 Controller.getDesiredThumbnailHeight(mContext));
2014 values.put(Snapshots.THUMBNAIL, compressBitmap(screenshot));
John Reckd8c74522011-06-14 08:45:00 -07002015 return values;
John Reck541f55a2011-06-07 16:34:43 -07002016 }
2017
John Reck8cc92352011-07-06 17:41:52 -07002018 public byte[] compressBitmap(Bitmap bitmap) {
2019 if (bitmap == null) {
2020 return null;
2021 }
2022 ByteArrayOutputStream stream = new ByteArrayOutputStream();
2023 bitmap.compress(CompressFormat.PNG, 100, stream);
2024 return stream.toByteArray();
2025 }
2026
John Reck26b18322011-06-21 13:08:58 -07002027 public void loadUrl(String url, Map<String, String> headers) {
2028 if (mMainView != null) {
Michael Kolb14612442011-06-24 13:06:29 -07002029 mCurrentState = new PageState(mContext, false, url, null);
John Reck26b18322011-06-21 13:08:58 -07002030 mWebViewController.onPageStarted(this, mMainView, null);
2031 mMainView.loadUrl(url, headers);
2032 }
2033 }
2034
Michael Kolb9ef259a2011-07-12 15:33:08 -07002035 protected void capture() {
2036 if (mMainView == null || mCapture == null) return;
2037 Canvas c = new Canvas(mCapture);
2038 final int left = mMainView.getScrollX();
2039 final int top = mMainView.getScrollY() + mMainView.getVisibleTitleHeight();
2040 c.translate(-left, -top);
2041 float scale = mCaptureWidth / (float) mMainView.getWidth();
2042 c.scale(scale, scale, left, top);
John Reck8ee633f2011-08-09 16:00:35 -07002043 if (mMainView instanceof BrowserWebView) {
2044 ((BrowserWebView)mMainView).drawContent(c);
2045 } else {
2046 mMainView.draw(c);
2047 }
Dianne Hackborn43cfe8a2011-08-02 16:59:35 -07002048 c.setBitmap(null);
John Reck8ee633f2011-08-09 16:00:35 -07002049 mHandler.removeMessages(MSG_CAPTURE);
John Reck1cf4b792011-07-26 10:22:22 -07002050 persistThumbnail();
John Reck8ee633f2011-08-09 16:00:35 -07002051 TabControl tc = mWebViewController.getTabControl();
2052 if (tc != null) {
2053 OnThumbnailUpdatedListener updateListener
2054 = tc.getOnThumbnailUpdatedListener();
2055 if (updateListener != null) {
2056 updateListener.onThumbnailUpdated(this);
2057 }
2058 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07002059 }
2060
2061 @Override
2062 public void onNewPicture(WebView view, Picture picture) {
2063 //update screenshot
John Reck1cf4b792011-07-26 10:22:22 -07002064 postCapture();
2065 }
2066
2067 private void postCapture() {
Michael Kolb9ef259a2011-07-12 15:33:08 -07002068 if (!mHandler.hasMessages(MSG_CAPTURE)) {
2069 mHandler.sendEmptyMessageDelayed(MSG_CAPTURE, CAPTURE_DELAY);
2070 }
2071 }
2072
John Reckef654f12011-07-12 16:42:08 -07002073 public boolean canGoBack() {
2074 return mMainView != null ? mMainView.canGoBack() : false;
2075 }
2076
2077 public boolean canGoForward() {
2078 return mMainView != null ? mMainView.canGoForward() : false;
2079 }
2080
2081 public void goBack() {
2082 if (mMainView != null) {
2083 mMainView.goBack();
2084 }
2085 }
2086
2087 public void goForward() {
2088 if (mMainView != null) {
2089 mMainView.goForward();
2090 }
2091 }
2092
Mathew Inwood1dd8e822011-08-03 14:34:29 +01002093 /**
2094 * Causes the tab back/forward stack to be cleared once, if the given URL is the next URL
2095 * to be added to the stack.
2096 *
2097 * This is used to ensure that preloaded URLs that are not subsequently seen by the user do
2098 * not appear in the back stack.
2099 */
2100 public void clearBackStackWhenItemAdded(String urlToMatch) {
2101 mClearHistoryMatchUrl = urlToMatch;
2102 }
2103
John Reck1cf4b792011-07-26 10:22:22 -07002104 protected void persistThumbnail() {
2105 BackgroundHandler.execute(mSaveThumbnail);
2106 }
2107
2108 protected void deleteThumbnail() {
2109 BackgroundHandler.execute(mDeleteThumbnail);
2110 }
2111
2112 private void updateCaptureFromBlob(byte[] blob) {
2113 synchronized (Tab.this) {
2114 if (mCapture == null) {
2115 return;
2116 }
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002117 ByteBuffer buffer = ByteBuffer.wrap(blob);
Michael Kolbd837a112011-08-09 14:00:18 -07002118 try {
2119 mCapture.copyPixelsFromBuffer(buffer);
2120 } catch (RuntimeException rex) {
2121 Log.e(LOGTAG, "Load capture has mismatched sizes; buffer: "
2122 + buffer.capacity() + " blob: " + blob.length
2123 + "capture: " + mCapture.getByteCount());
2124 throw rex;
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002125 }
John Reck1cf4b792011-07-26 10:22:22 -07002126 }
2127 }
2128
2129 private byte[] getCaptureBlob() {
2130 synchronized (Tab.this) {
2131 if (mCapture == null) {
2132 return null;
2133 }
2134 ByteBuffer buffer = ByteBuffer.allocate(mCapture.getByteCount());
2135 mCapture.copyPixelsToBuffer(buffer);
2136 return buffer.array();
2137 }
2138 }
2139
2140 private Runnable mSaveThumbnail = new Runnable() {
2141
2142 @Override
2143 public void run() {
2144 byte[] blob = getCaptureBlob();
2145 if (blob == null) {
2146 return;
2147 }
2148 ContentResolver cr = mContext.getContentResolver();
2149 ContentValues values = new ContentValues();
2150 values.put(Thumbnails._ID, mId);
2151 values.put(Thumbnails.THUMBNAIL, blob);
2152 cr.insert(Thumbnails.CONTENT_URI, values);
2153 }
2154 };
2155
2156 private Runnable mDeleteThumbnail = new Runnable() {
2157
2158 @Override
2159 public void run() {
2160 ContentResolver cr = mContext.getContentResolver();
2161 try {
2162 cr.delete(ContentUris.withAppendedId(Thumbnails.CONTENT_URI, mId),
2163 null, null);
2164 } catch (Throwable t) {}
2165 }
2166 };
2167
2168 private Runnable mLoadThumbnail = new Runnable() {
2169
2170 @Override
2171 public void run() {
2172 ContentResolver cr = mContext.getContentResolver();
2173 Cursor c = null;
2174 try {
2175 Uri uri = ContentUris.withAppendedId(Thumbnails.CONTENT_URI, mId);
2176 c = cr.query(uri, new String[] {Thumbnails._ID,
2177 Thumbnails.THUMBNAIL}, null, null, null);
2178 if (c.moveToFirst()) {
2179 byte[] data = c.getBlob(1);
2180 if (data != null && data.length > 0) {
2181 updateCaptureFromBlob(data);
2182 }
2183 }
2184 } finally {
2185 if (c != null) {
2186 c.close();
2187 }
2188 }
2189 }
2190 };
2191
John Reck52be4782011-08-26 15:37:29 -07002192 @Override
2193 public String toString() {
2194 StringBuilder builder = new StringBuilder(100);
2195 builder.append(mId);
2196 builder.append(") has parent: ");
2197 if (getParent() != null) {
2198 builder.append("true[");
2199 builder.append(getParent().getId());
2200 builder.append("]");
2201 } else {
2202 builder.append("false");
2203 }
2204 builder.append(", incog: ");
2205 builder.append(isPrivateBrowsingEnabled());
2206 if (!isPrivateBrowsingEnabled()) {
2207 builder.append(", title: ");
2208 builder.append(getTitle());
2209 builder.append(", url: ");
2210 builder.append(getUrl());
2211 }
2212 return builder.toString();
2213 }
2214
Grace Kloba22ac16e2009-10-07 18:00:23 -07002215}