blob: d6106a56929fe5efbeccf0c57409de82efbfcc3a [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;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700541 @Override
542 public void onPageStarted(WebView view, String url, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700543 mInPageLoad = true;
John Reck30c714c2010-12-16 17:30:34 -0800544 mPageLoadProgress = 0;
Michael Kolb14612442011-06-24 13:06:29 -0700545 mCurrentState = new PageState(mContext,
John Reck30c714c2010-12-16 17:30:34 -0800546 view.isPrivateBrowsingEnabled(), url, favicon);
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000547 mLoadStartTime = SystemClock.uptimeMillis();
Leon Scroggins58d56c62010-01-28 15:12:40 -0500548 if (mVoiceSearchData != null
549 && !url.equals(mVoiceSearchData.mLastVoiceSearchUrl)) {
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500550 if (mVoiceSearchData.mSourceIsGoogle) {
551 Intent i = new Intent(LoggingEvents.ACTION_LOG_EVENT);
552 i.putExtra(LoggingEvents.EXTRA_FLUSH, true);
Michael Kolb14612442011-06-24 13:06:29 -0700553 mContext.sendBroadcast(i);
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500554 }
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400555 revertVoiceSearchMode();
Leon Scroggins58d56c62010-01-28 15:12:40 -0500556 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700557
Grace Kloba22ac16e2009-10-07 18:00:23 -0700558
559 // If we start a touch icon load and then load a new page, we don't
560 // want to cancel the current touch icon loader. But, we do want to
561 // create a new one when the touch icon url is known.
562 if (mTouchIconLoader != null) {
563 mTouchIconLoader.mTab = null;
564 mTouchIconLoader = null;
565 }
566
567 // reset the error console
568 if (mErrorConsole != null) {
569 mErrorConsole.clearErrorMessages();
Michael Kolb8233fac2010-10-26 16:08:53 -0700570 if (mWebViewController.shouldShowErrorConsole()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700571 mErrorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
572 }
573 }
574
Patrick Scott92066772011-03-10 08:46:27 -0500575 // Cancel the auto-login process.
576 if (mDeviceAccountLogin != null) {
577 mDeviceAccountLogin.cancel();
578 mDeviceAccountLogin = null;
579 mWebViewController.hideAutoLogin(Tab.this);
580 }
581
Grace Kloba22ac16e2009-10-07 18:00:23 -0700582 // finally update the UI in the activity if it is in the foreground
John Reck324d4402011-01-11 16:56:42 -0800583 mWebViewController.onPageStarted(Tab.this, view, favicon);
Leon Scroggins4cd97792010-12-03 15:31:56 -0500584
John Recke969cc52010-12-21 17:24:43 -0800585 updateBookmarkedStatus();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700586 }
587
588 @Override
589 public void onPageFinished(WebView view, String url) {
John Recka1696282011-07-08 14:10:37 -0700590 if (!mInPageLoad) {
591 // In page navigation links (www.something.com#footer) will
592 // trigger an onPageFinished which we don't care about.
593 return;
594 }
John Reck5b691842010-11-29 11:21:13 -0800595 if (!isPrivateBrowsingEnabled()) {
596 LogTag.logPageFinishedLoading(
597 url, SystemClock.uptimeMillis() - mLoadStartTime);
598 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700599 mInPageLoad = false;
John Reck1cf4b792011-07-26 10:22:22 -0700600 syncCurrentState(view, url);
John Reck324d4402011-01-11 16:56:42 -0800601 mWebViewController.onPageFinished(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700602 }
603
604 // return true if want to hijack the url to let another app to handle it
605 @Override
606 public boolean shouldOverrideUrlLoading(WebView view, String url) {
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400607 if (voiceSearchSourceIsGoogle()) {
608 // This method is called when the user clicks on a link.
609 // VoiceSearchMode is turned off when the user leaves the
610 // Google results page, so at this point the user must be on
611 // that page. If the user clicked a link on that page, assume
612 // that the voice search was effective, and broadcast an Intent
613 // so a receiver can take note of that fact.
614 Intent logIntent = new Intent(LoggingEvents.ACTION_LOG_EVENT);
615 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
616 LoggingEvents.VoiceSearch.RESULT_CLICKED);
Michael Kolb14612442011-06-24 13:06:29 -0700617 mContext.sendBroadcast(logIntent);
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400618 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700619 if (mInForeground) {
Michael Kolb18eb3772010-12-10 14:29:51 -0800620 return mWebViewController.shouldOverrideUrlLoading(Tab.this,
621 view, url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700622 } else {
623 return false;
624 }
625 }
626
627 /**
628 * Updates the lock icon. This method is called when we discover another
629 * resource to be loaded for this page (for example, javascript). While
630 * we update the icon type, we do not update the lock icon itself until
631 * we are done loading, it is slightly more secure this way.
632 */
633 @Override
634 public void onLoadResource(WebView view, String url) {
635 if (url != null && url.length() > 0) {
636 // It is only if the page claims to be secure that we may have
637 // to update the lock:
John Reck30c714c2010-12-16 17:30:34 -0800638 if (mCurrentState.mLockIcon == LockIcon.LOCK_ICON_SECURE) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700639 // If NOT a 'safe' url, change the lock to mixed content!
640 if (!(URLUtil.isHttpsUrl(url) || URLUtil.isDataUrl(url)
641 || URLUtil.isAboutUrl(url))) {
John Reck30c714c2010-12-16 17:30:34 -0800642 mCurrentState.mLockIcon = LockIcon.LOCK_ICON_MIXED;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700643 }
644 }
645 }
646 }
647
648 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700649 * Show a dialog informing the user of the network error reported by
650 * WebCore if it is in the foreground.
651 */
652 @Override
653 public void onReceivedError(WebView view, int errorCode,
654 String description, String failingUrl) {
655 if (errorCode != WebViewClient.ERROR_HOST_LOOKUP &&
656 errorCode != WebViewClient.ERROR_CONNECT &&
657 errorCode != WebViewClient.ERROR_BAD_URL &&
658 errorCode != WebViewClient.ERROR_UNSUPPORTED_SCHEME &&
659 errorCode != WebViewClient.ERROR_FILE) {
660 queueError(errorCode, description);
661 }
Jeff Hamilton47654f42010-09-07 09:57:51 -0500662
663 // Don't log URLs when in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -0700664 if (!isPrivateBrowsingEnabled()) {
Jeff Hamilton47654f42010-09-07 09:57:51 -0500665 Log.e(LOGTAG, "onReceivedError " + errorCode + " " + failingUrl
666 + " " + description);
667 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700668 }
669
670 /**
671 * Check with the user if it is ok to resend POST data as the page they
672 * are trying to navigate to is the result of a POST.
673 */
674 @Override
675 public void onFormResubmission(WebView view, final Message dontResend,
676 final Message resend) {
677 if (!mInForeground) {
678 dontResend.sendToTarget();
679 return;
680 }
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500681 if (mDontResend != null) {
682 Log.w(LOGTAG, "onFormResubmission should not be called again "
683 + "while dialog is still up");
684 dontResend.sendToTarget();
685 return;
686 }
687 mDontResend = dontResend;
688 mResend = resend;
Michael Kolb14612442011-06-24 13:06:29 -0700689 new AlertDialog.Builder(mContext).setTitle(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700690 R.string.browserFrameFormResubmitLabel).setMessage(
691 R.string.browserFrameFormResubmitMessage)
692 .setPositiveButton(R.string.ok,
693 new DialogInterface.OnClickListener() {
694 public void onClick(DialogInterface dialog,
695 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500696 if (mResend != null) {
697 mResend.sendToTarget();
698 mResend = null;
699 mDontResend = null;
700 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700701 }
702 }).setNegativeButton(R.string.cancel,
703 new DialogInterface.OnClickListener() {
704 public void onClick(DialogInterface dialog,
705 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500706 if (mDontResend != null) {
707 mDontResend.sendToTarget();
708 mResend = null;
709 mDontResend = null;
710 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700711 }
712 }).setOnCancelListener(new OnCancelListener() {
713 public void onCancel(DialogInterface dialog) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500714 if (mDontResend != null) {
715 mDontResend.sendToTarget();
716 mResend = null;
717 mDontResend = null;
718 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700719 }
720 }).show();
721 }
722
723 /**
724 * Insert the url into the visited history database.
725 * @param url The url to be inserted.
726 * @param isReload True if this url is being reloaded.
727 * FIXME: Not sure what to do when reloading the page.
728 */
729 @Override
730 public void doUpdateVisitedHistory(WebView view, String url,
731 boolean isReload) {
John Reck324d4402011-01-11 16:56:42 -0800732 mWebViewController.doUpdateVisitedHistory(Tab.this, isReload);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700733 }
734
735 /**
736 * Displays SSL error(s) dialog to the user.
737 */
738 @Override
739 public void onReceivedSslError(final WebView view,
740 final SslErrorHandler handler, final SslError error) {
741 if (!mInForeground) {
742 handler.cancel();
John Reck30c714c2010-12-16 17:30:34 -0800743 setLockIconType(LockIcon.LOCK_ICON_UNSECURE);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700744 return;
745 }
John Reck35e9dd62011-04-25 09:01:54 -0700746 if (mSettings.showSecurityWarnings()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700747 final LayoutInflater factory =
Michael Kolb14612442011-06-24 13:06:29 -0700748 LayoutInflater.from(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700749 final View warningsView =
750 factory.inflate(R.layout.ssl_warnings, null);
751 final LinearLayout placeholder =
752 (LinearLayout)warningsView.findViewById(R.id.placeholder);
753
754 if (error.hasError(SslError.SSL_UNTRUSTED)) {
755 LinearLayout ll = (LinearLayout)factory
756 .inflate(R.layout.ssl_warning, null);
757 ((TextView)ll.findViewById(R.id.warning))
758 .setText(R.string.ssl_untrusted);
759 placeholder.addView(ll);
760 }
761
762 if (error.hasError(SslError.SSL_IDMISMATCH)) {
763 LinearLayout ll = (LinearLayout)factory
764 .inflate(R.layout.ssl_warning, null);
765 ((TextView)ll.findViewById(R.id.warning))
766 .setText(R.string.ssl_mismatch);
767 placeholder.addView(ll);
768 }
769
770 if (error.hasError(SslError.SSL_EXPIRED)) {
771 LinearLayout ll = (LinearLayout)factory
772 .inflate(R.layout.ssl_warning, null);
773 ((TextView)ll.findViewById(R.id.warning))
774 .setText(R.string.ssl_expired);
775 placeholder.addView(ll);
776 }
777
778 if (error.hasError(SslError.SSL_NOTYETVALID)) {
779 LinearLayout ll = (LinearLayout)factory
780 .inflate(R.layout.ssl_warning, null);
781 ((TextView)ll.findViewById(R.id.warning))
782 .setText(R.string.ssl_not_yet_valid);
783 placeholder.addView(ll);
784 }
785
Michael Kolb14612442011-06-24 13:06:29 -0700786 new AlertDialog.Builder(mContext).setTitle(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700787 R.string.security_warning).setIcon(
788 android.R.drawable.ic_dialog_alert).setView(
789 warningsView).setPositiveButton(R.string.ssl_continue,
790 new DialogInterface.OnClickListener() {
791 public void onClick(DialogInterface dialog,
792 int whichButton) {
793 handler.proceed();
794 }
795 }).setNeutralButton(R.string.view_certificate,
796 new DialogInterface.OnClickListener() {
797 public void onClick(DialogInterface dialog,
798 int whichButton) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700799 mWebViewController.showSslCertificateOnError(view,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700800 handler, error);
801 }
Ben Murdocha49b8292010-11-16 11:56:04 +0000802 }).setNegativeButton(R.string.ssl_go_back,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700803 new DialogInterface.OnClickListener() {
804 public void onClick(DialogInterface dialog,
805 int whichButton) {
John Reck30c714c2010-12-16 17:30:34 -0800806 dialog.cancel();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700807 }
808 }).setOnCancelListener(
809 new DialogInterface.OnCancelListener() {
810 public void onCancel(DialogInterface dialog) {
811 handler.cancel();
John Reck30c714c2010-12-16 17:30:34 -0800812 setLockIconType(LockIcon.LOCK_ICON_UNSECURE);
813 mWebViewController.onUserCanceledSsl(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700814 }
815 }).show();
816 } else {
817 handler.proceed();
818 }
819 }
820
821 /**
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700822 * Displays client certificate request to the user.
823 */
824 @Override
825 public void onReceivedClientCertRequest(final WebView view,
826 final ClientCertRequestHandler handler, final String host_and_port) {
827 if (!mInForeground) {
828 handler.ignore();
829 return;
830 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700831 int colon = host_and_port.lastIndexOf(':');
832 String host;
833 int port;
834 if (colon == -1) {
835 host = host_and_port;
836 port = -1;
837 } else {
838 String portString = host_and_port.substring(colon + 1);
839 try {
840 port = Integer.parseInt(portString);
841 host = host_and_port.substring(0, colon);
842 } catch (NumberFormatException e) {
843 host = host_and_port;
844 port = -1;
845 }
846 }
Michael Kolb14612442011-06-24 13:06:29 -0700847 KeyChain.choosePrivateKeyAlias(
848 mWebViewController.getActivity(), new KeyChainAliasCallback() {
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700849 @Override public void alias(String alias) {
850 if (alias == null) {
851 handler.cancel();
852 return;
853 }
Michael Kolb14612442011-06-24 13:06:29 -0700854 new KeyChainLookup(mContext, handler, alias).execute();
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700855 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700856 }, null, null, host, port, null);
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700857 }
858
859 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700860 * Handles an HTTP authentication request.
861 *
862 * @param handler The authentication handler
863 * @param host The host
864 * @param realm The realm
865 */
866 @Override
867 public void onReceivedHttpAuthRequest(WebView view,
868 final HttpAuthHandler handler, final String host,
869 final String realm) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700870 mWebViewController.onReceivedHttpAuthRequest(Tab.this, view, handler, host, realm);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700871 }
872
873 @Override
John Reck438bf462011-01-12 18:11:46 -0800874 public WebResourceResponse shouldInterceptRequest(WebView view,
875 String url) {
876 WebResourceResponse res = HomeProvider.shouldInterceptRequest(
Michael Kolb14612442011-06-24 13:06:29 -0700877 mContext, url);
John Reck438bf462011-01-12 18:11:46 -0800878 return res;
879 }
880
881 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700882 public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
883 if (!mInForeground) {
884 return false;
885 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700886 return mWebViewController.shouldOverrideKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700887 }
888
889 @Override
890 public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700891 if (!mInForeground) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700892 return;
893 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700894 mWebViewController.onUnhandledKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700895 }
Patrick Scott92066772011-03-10 08:46:27 -0500896
897 @Override
898 public void onReceivedLoginRequest(WebView view, String realm,
899 String account, String args) {
Michael Kolb14612442011-06-24 13:06:29 -0700900 new DeviceAccountLogin(mWebViewController.getActivity(), view, Tab.this, mWebViewController)
Patrick Scott92066772011-03-10 08:46:27 -0500901 .handleLogin(realm, account, args);
902 }
903
Grace Kloba22ac16e2009-10-07 18:00:23 -0700904 };
905
John Reck1cf4b792011-07-26 10:22:22 -0700906 private void syncCurrentState(WebView view, String url) {
907 // Sync state (in case of stop/timeout)
908 mCurrentState.mUrl = view.getUrl();
909 if (mCurrentState.mUrl == null) {
910 mCurrentState.mUrl = url != null ? url : "";
911 }
912 mCurrentState.mOriginalUrl = view.getOriginalUrl();
913 mCurrentState.mTitle = view.getTitle();
914 mCurrentState.mFavicon = view.getFavicon();
915 if (!URLUtil.isHttpsUrl(mCurrentState.mUrl)) {
916 // In case we stop when loading an HTTPS page from an HTTP page
917 // but before a provisional load occurred
918 mCurrentState.mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
919 }
John Reck502a3532011-08-16 14:21:46 -0700920 mCurrentState.mIncognito = view.isPrivateBrowsingEnabled();
John Reck1cf4b792011-07-26 10:22:22 -0700921 }
922
Patrick Scott92066772011-03-10 08:46:27 -0500923 // Called by DeviceAccountLogin when the Tab needs to have the auto-login UI
924 // displayed.
925 void setDeviceAccountLogin(DeviceAccountLogin login) {
926 mDeviceAccountLogin = login;
927 }
928
929 // Returns non-null if the title bar should display the auto-login UI.
930 DeviceAccountLogin getDeviceAccountLogin() {
931 return mDeviceAccountLogin;
932 }
933
Grace Kloba22ac16e2009-10-07 18:00:23 -0700934 // -------------------------------------------------------------------------
935 // WebChromeClient implementation for the main WebView
936 // -------------------------------------------------------------------------
937
938 private final WebChromeClient mWebChromeClient = new WebChromeClient() {
939 // Helper method to create a new tab or sub window.
940 private void createWindow(final boolean dialog, final Message msg) {
941 WebView.WebViewTransport transport =
942 (WebView.WebViewTransport) msg.obj;
943 if (dialog) {
944 createSubWindow();
Michael Kolb8233fac2010-10-26 16:08:53 -0700945 mWebViewController.attachSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700946 transport.setWebView(mSubView);
947 } else {
Michael Kolb7bcafde2011-05-09 13:55:59 -0700948 final Tab newTab = mWebViewController.openTab(null,
John Reck5949c662011-05-27 09:52:29 -0700949 Tab.this, true, true);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700950 transport.setWebView(newTab.getWebView());
951 }
952 msg.sendToTarget();
953 }
954
955 @Override
956 public boolean onCreateWindow(WebView view, final boolean dialog,
957 final boolean userGesture, final Message resultMsg) {
958 // only allow new window or sub window for the foreground case
959 if (!mInForeground) {
960 return false;
961 }
962 // Short-circuit if we can't create any more tabs or sub windows.
963 if (dialog && mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -0700964 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700965 .setTitle(R.string.too_many_subwindows_dialog_title)
966 .setIcon(android.R.drawable.ic_dialog_alert)
967 .setMessage(R.string.too_many_subwindows_dialog_message)
968 .setPositiveButton(R.string.ok, null)
969 .show();
970 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700971 } else if (!mWebViewController.getTabControl().canCreateNewTab()) {
Michael Kolb14612442011-06-24 13:06:29 -0700972 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700973 .setTitle(R.string.too_many_windows_dialog_title)
974 .setIcon(android.R.drawable.ic_dialog_alert)
975 .setMessage(R.string.too_many_windows_dialog_message)
976 .setPositiveButton(R.string.ok, null)
977 .show();
978 return false;
979 }
980
981 // Short-circuit if this was a user gesture.
982 if (userGesture) {
983 createWindow(dialog, resultMsg);
984 return true;
985 }
986
987 // Allow the popup and create the appropriate window.
988 final AlertDialog.OnClickListener allowListener =
989 new AlertDialog.OnClickListener() {
990 public void onClick(DialogInterface d,
991 int which) {
992 createWindow(dialog, resultMsg);
993 }
994 };
995
996 // Block the popup by returning a null WebView.
997 final AlertDialog.OnClickListener blockListener =
998 new AlertDialog.OnClickListener() {
999 public void onClick(DialogInterface d, int which) {
1000 resultMsg.sendToTarget();
1001 }
1002 };
1003
1004 // Build a confirmation dialog to display to the user.
1005 final AlertDialog d =
Michael Kolb14612442011-06-24 13:06:29 -07001006 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -07001007 .setTitle(R.string.attention)
1008 .setIcon(android.R.drawable.ic_dialog_alert)
1009 .setMessage(R.string.popup_window_attempt)
1010 .setPositiveButton(R.string.allow, allowListener)
1011 .setNegativeButton(R.string.block, blockListener)
1012 .setCancelable(false)
1013 .create();
1014
1015 // Show the confirmation dialog.
1016 d.show();
1017 return true;
1018 }
1019
1020 @Override
Patrick Scotteb5061b2009-11-18 15:00:30 -05001021 public void onRequestFocus(WebView view) {
1022 if (!mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -07001023 mWebViewController.switchToTab(Tab.this);
Patrick Scotteb5061b2009-11-18 15:00:30 -05001024 }
1025 }
1026
1027 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -07001028 public void onCloseWindow(WebView window) {
Michael Kolbc831b632011-05-11 09:30:34 -07001029 if (mParent != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001030 // JavaScript can only close popup window.
1031 if (mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -07001032 mWebViewController.switchToTab(mParent);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001033 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001034 mWebViewController.closeTab(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001035 }
1036 }
1037
1038 @Override
1039 public void onProgressChanged(WebView view, int newProgress) {
John Reck30c714c2010-12-16 17:30:34 -08001040 mPageLoadProgress = newProgress;
1041 mWebViewController.onProgressChanged(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001042 }
1043
1044 @Override
Leon Scroggins21d9b902010-03-11 09:33:11 -05001045 public void onReceivedTitle(WebView view, final String title) {
John Reck30c714c2010-12-16 17:30:34 -08001046 mCurrentState.mTitle = title;
Michael Kolb8233fac2010-10-26 16:08:53 -07001047 mWebViewController.onReceivedTitle(Tab.this, title);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001048 }
1049
1050 @Override
1051 public void onReceivedIcon(WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -08001052 mCurrentState.mFavicon = icon;
Michael Kolb8233fac2010-10-26 16:08:53 -07001053 mWebViewController.onFavicon(Tab.this, view, icon);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001054 }
1055
1056 @Override
1057 public void onReceivedTouchIconUrl(WebView view, String url,
1058 boolean precomposed) {
Michael Kolb14612442011-06-24 13:06:29 -07001059 final ContentResolver cr = mContext.getContentResolver();
Leon Scrogginsc8393d92010-04-23 14:58:16 -04001060 // Let precomposed icons take precedence over non-composed
1061 // icons.
1062 if (precomposed && mTouchIconLoader != null) {
1063 mTouchIconLoader.cancel(false);
1064 mTouchIconLoader = null;
1065 }
1066 // Have only one async task at a time.
1067 if (mTouchIconLoader == null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001068 mTouchIconLoader = new DownloadTouchIcon(Tab.this,
Michael Kolb14612442011-06-24 13:06:29 -07001069 mContext, cr, view);
Leon Scrogginsc8393d92010-04-23 14:58:16 -04001070 mTouchIconLoader.execute(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001071 }
1072 }
1073
1074 @Override
1075 public void onShowCustomView(View view,
1076 WebChromeClient.CustomViewCallback callback) {
Michael Kolb14612442011-06-24 13:06:29 -07001077 Activity activity = mWebViewController.getActivity();
1078 if (activity != null) {
1079 onShowCustomView(view, activity.getRequestedOrientation(), callback);
1080 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001081 }
1082
1083 @Override
1084 public void onShowCustomView(View view, int requestedOrientation,
1085 WebChromeClient.CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001086 if (mInForeground) mWebViewController.showCustomView(Tab.this, view,
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001087 requestedOrientation, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001088 }
1089
1090 @Override
1091 public void onHideCustomView() {
Michael Kolb8233fac2010-10-26 16:08:53 -07001092 if (mInForeground) mWebViewController.hideCustomView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001093 }
1094
1095 /**
1096 * The origin has exceeded its database quota.
1097 * @param url the URL that exceeded the quota
1098 * @param databaseIdentifier the identifier of the database on which the
1099 * transaction that caused the quota overflow was run
1100 * @param currentQuota the current quota for the origin.
1101 * @param estimatedSize the estimated size of the database.
1102 * @param totalUsedQuota is the sum of all origins' quota.
1103 * @param quotaUpdater The callback to run when a decision to allow or
1104 * deny quota has been made. Don't forget to call this!
1105 */
1106 @Override
1107 public void onExceededDatabaseQuota(String url,
1108 String databaseIdentifier, long currentQuota, long estimatedSize,
1109 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -07001110 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001111 .onExceededDatabaseQuota(url, databaseIdentifier,
1112 currentQuota, estimatedSize, totalUsedQuota,
1113 quotaUpdater);
1114 }
1115
1116 /**
1117 * The Application Cache has exceeded its max size.
1118 * @param spaceNeeded is the amount of disk space that would be needed
1119 * in order for the last appcache operation to succeed.
1120 * @param totalUsedQuota is the sum of all origins' quota.
1121 * @param quotaUpdater A callback to inform the WebCore thread that a
1122 * new app cache size is available. This callback must always
1123 * be executed at some point to ensure that the sleeping
1124 * WebCore thread is woken up.
1125 */
1126 @Override
1127 public void onReachedMaxAppCacheSize(long spaceNeeded,
1128 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -07001129 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001130 .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota,
1131 quotaUpdater);
1132 }
1133
1134 /**
1135 * Instructs the browser to show a prompt to ask the user to set the
1136 * Geolocation permission state for the specified origin.
1137 * @param origin The origin for which Geolocation permissions are
1138 * requested.
1139 * @param callback The callback to call once the user has set the
1140 * Geolocation permission state.
1141 */
1142 @Override
1143 public void onGeolocationPermissionsShowPrompt(String origin,
1144 GeolocationPermissions.Callback callback) {
1145 if (mInForeground) {
Grace Kloba50c241e2010-04-20 11:07:50 -07001146 getGeolocationPermissionsPrompt().show(origin, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001147 }
1148 }
1149
1150 /**
1151 * Instructs the browser to hide the Geolocation permissions prompt.
1152 */
1153 @Override
1154 public void onGeolocationPermissionsHidePrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001155 if (mInForeground && mGeolocationPermissionsPrompt != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001156 mGeolocationPermissionsPrompt.hide();
1157 }
1158 }
1159
Ben Murdoch65acc352009-11-19 18:16:04 +00001160 /* Adds a JavaScript error message to the system log and if the JS
1161 * console is enabled in the about:debug options, to that console
1162 * also.
Ben Murdochc42addf2010-01-28 15:19:59 +00001163 * @param consoleMessage the message object.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001164 */
1165 @Override
Ben Murdochc42addf2010-01-28 15:19:59 +00001166 public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001167 if (mInForeground) {
1168 // call getErrorConsole(true) so it will create one if needed
1169 ErrorConsoleView errorConsole = getErrorConsole(true);
Ben Murdochc42addf2010-01-28 15:19:59 +00001170 errorConsole.addErrorMessage(consoleMessage);
Michael Kolb8233fac2010-10-26 16:08:53 -07001171 if (mWebViewController.shouldShowErrorConsole()
1172 && errorConsole.getShowState() !=
1173 ErrorConsoleView.SHOW_MAXIMIZED) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001174 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
1175 }
1176 }
Ben Murdochc42addf2010-01-28 15:19:59 +00001177
Jeff Hamilton47654f42010-09-07 09:57:51 -05001178 // Don't log console messages in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001179 if (isPrivateBrowsingEnabled()) return true;
Jeff Hamilton47654f42010-09-07 09:57:51 -05001180
Ben Murdochc42addf2010-01-28 15:19:59 +00001181 String message = "Console: " + consoleMessage.message() + " "
1182 + consoleMessage.sourceId() + ":"
1183 + consoleMessage.lineNumber();
1184
1185 switch (consoleMessage.messageLevel()) {
1186 case TIP:
1187 Log.v(CONSOLE_LOGTAG, message);
1188 break;
1189 case LOG:
1190 Log.i(CONSOLE_LOGTAG, message);
1191 break;
1192 case WARNING:
1193 Log.w(CONSOLE_LOGTAG, message);
1194 break;
1195 case ERROR:
1196 Log.e(CONSOLE_LOGTAG, message);
1197 break;
1198 case DEBUG:
1199 Log.d(CONSOLE_LOGTAG, message);
1200 break;
1201 }
1202
1203 return true;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001204 }
1205
1206 /**
1207 * Ask the browser for an icon to represent a <video> element.
1208 * This icon will be used if the Web page did not specify a poster attribute.
1209 * @return Bitmap The icon or null if no such icon is available.
1210 */
1211 @Override
1212 public Bitmap getDefaultVideoPoster() {
1213 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001214 return mWebViewController.getDefaultVideoPoster();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001215 }
1216 return null;
1217 }
1218
1219 /**
1220 * Ask the host application for a custom progress view to show while
1221 * a <video> is loading.
1222 * @return View The progress view.
1223 */
1224 @Override
1225 public View getVideoLoadingProgressView() {
1226 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001227 return mWebViewController.getVideoLoadingProgressView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001228 }
1229 return null;
1230 }
1231
1232 @Override
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01001233 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001234 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001235 mWebViewController.openFileChooser(uploadMsg, acceptType);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001236 } else {
1237 uploadMsg.onReceiveValue(null);
1238 }
1239 }
1240
1241 /**
1242 * Deliver a list of already-visited URLs
1243 */
1244 @Override
1245 public void getVisitedHistory(final ValueCallback<String[]> callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001246 mWebViewController.getVisitedHistory(callback);
1247 }
Ben Murdoch8029a772010-11-16 11:58:21 +00001248
1249 @Override
1250 public void setupAutoFill(Message message) {
1251 // Prompt the user to set up their profile.
1252 final Message msg = message;
Michael Kolb14612442011-06-24 13:06:29 -07001253 AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
1254 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
Ben Murdoch1d676b62011-01-17 12:54:24 +00001255 Context.LAYOUT_INFLATER_SERVICE);
1256 final View layout = inflater.inflate(R.layout.setup_autofill_dialog, null);
1257
1258 builder.setView(layout)
Ben Murdochb7e6f942011-07-08 13:00:21 +01001259 .setTitle(R.string.autofill_setup_dialog_title)
Ben Murdoch1d676b62011-01-17 12:54:24 +00001260 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1261 @Override
1262 public void onClick(DialogInterface dialog, int id) {
1263 CheckBox disableAutoFill = (CheckBox) layout.findViewById(
1264 R.id.setup_autofill_dialog_disable_autofill);
1265
1266 if (disableAutoFill.isChecked()) {
1267 // Disable autofill and show a toast with how to turn it on again.
John Reck35e9dd62011-04-25 09:01:54 -07001268 mSettings.setAutofillEnabled(false);
Michael Kolb14612442011-06-24 13:06:29 -07001269 Toast.makeText(mContext,
Ben Murdoch1d676b62011-01-17 12:54:24 +00001270 R.string.autofill_setup_dialog_negative_toast,
1271 Toast.LENGTH_LONG).show();
1272 } else {
1273 // Take user to the AutoFill profile editor. When they return,
1274 // we will send the message that we pass here which will trigger
1275 // the form to get filled out with their new profile.
1276 mWebViewController.setupAutoFill(msg);
1277 }
1278 }
1279 })
1280 .setNegativeButton(R.string.cancel, null)
1281 .show();
Ben Murdoch8029a772010-11-16 11:58:21 +00001282 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001283 };
1284
1285 // -------------------------------------------------------------------------
1286 // WebViewClient implementation for the sub window
1287 // -------------------------------------------------------------------------
1288
1289 // Subclass of WebViewClient used in subwindows to notify the main
1290 // WebViewClient of certain WebView activities.
1291 private static class SubWindowClient extends WebViewClient {
1292 // The main WebViewClient.
1293 private final WebViewClient mClient;
Michael Kolb8233fac2010-10-26 16:08:53 -07001294 private final WebViewController mController;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001295
Michael Kolb8233fac2010-10-26 16:08:53 -07001296 SubWindowClient(WebViewClient client, WebViewController controller) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001297 mClient = client;
Michael Kolb8233fac2010-10-26 16:08:53 -07001298 mController = controller;
Leon Scroggins III211ba542010-04-19 13:21:13 -04001299 }
1300 @Override
1301 public void onPageStarted(WebView view, String url, Bitmap favicon) {
1302 // Unlike the others, do not call mClient's version, which would
1303 // change the progress bar. However, we do want to remove the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001304 // find or select dialog.
Michael Kolb8233fac2010-10-26 16:08:53 -07001305 mController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001306 }
1307 @Override
1308 public void doUpdateVisitedHistory(WebView view, String url,
1309 boolean isReload) {
1310 mClient.doUpdateVisitedHistory(view, url, isReload);
1311 }
1312 @Override
1313 public boolean shouldOverrideUrlLoading(WebView view, String url) {
1314 return mClient.shouldOverrideUrlLoading(view, url);
1315 }
1316 @Override
1317 public void onReceivedSslError(WebView view, SslErrorHandler handler,
1318 SslError error) {
1319 mClient.onReceivedSslError(view, handler, error);
1320 }
1321 @Override
Brian Carlstrom8862c1d2011-06-02 01:05:55 -07001322 public void onReceivedClientCertRequest(WebView view,
1323 ClientCertRequestHandler handler, String host_and_port) {
1324 mClient.onReceivedClientCertRequest(view, handler, host_and_port);
1325 }
1326 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -07001327 public void onReceivedHttpAuthRequest(WebView view,
1328 HttpAuthHandler handler, String host, String realm) {
1329 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
1330 }
1331 @Override
1332 public void onFormResubmission(WebView view, Message dontResend,
1333 Message resend) {
1334 mClient.onFormResubmission(view, dontResend, resend);
1335 }
1336 @Override
1337 public void onReceivedError(WebView view, int errorCode,
1338 String description, String failingUrl) {
1339 mClient.onReceivedError(view, errorCode, description, failingUrl);
1340 }
1341 @Override
1342 public boolean shouldOverrideKeyEvent(WebView view,
1343 android.view.KeyEvent event) {
1344 return mClient.shouldOverrideKeyEvent(view, event);
1345 }
1346 @Override
1347 public void onUnhandledKeyEvent(WebView view,
1348 android.view.KeyEvent event) {
1349 mClient.onUnhandledKeyEvent(view, event);
1350 }
1351 }
1352
1353 // -------------------------------------------------------------------------
1354 // WebChromeClient implementation for the sub window
1355 // -------------------------------------------------------------------------
1356
1357 private class SubWindowChromeClient extends WebChromeClient {
1358 // The main WebChromeClient.
1359 private final WebChromeClient mClient;
1360
1361 SubWindowChromeClient(WebChromeClient client) {
1362 mClient = client;
1363 }
1364 @Override
1365 public void onProgressChanged(WebView view, int newProgress) {
1366 mClient.onProgressChanged(view, newProgress);
1367 }
1368 @Override
1369 public boolean onCreateWindow(WebView view, boolean dialog,
1370 boolean userGesture, android.os.Message resultMsg) {
1371 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
1372 }
1373 @Override
1374 public void onCloseWindow(WebView window) {
1375 if (window != mSubView) {
1376 Log.e(LOGTAG, "Can't close the window");
1377 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001378 mWebViewController.dismissSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001379 }
1380 }
1381
1382 // -------------------------------------------------------------------------
1383
1384 // Construct a new tab
Michael Kolb7bcafde2011-05-09 13:55:59 -07001385 Tab(WebViewController wvcontroller, WebView w) {
John Reck1cf4b792011-07-26 10:22:22 -07001386 this(wvcontroller, w, null);
1387 }
1388
1389 Tab(WebViewController wvcontroller, Bundle state) {
1390 this(wvcontroller, null, state);
1391 }
1392
1393 Tab(WebViewController wvcontroller, WebView w, Bundle state) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001394 mWebViewController = wvcontroller;
Michael Kolb14612442011-06-24 13:06:29 -07001395 mContext = mWebViewController.getContext();
John Reck35e9dd62011-04-25 09:01:54 -07001396 mSettings = BrowserSettings.getInstance();
Michael Kolb14612442011-06-24 13:06:29 -07001397 mDataController = DataController.getInstance(mContext);
1398 mCurrentState = new PageState(mContext, w != null
John Reck847b5322011-04-14 17:02:18 -07001399 ? w.isPrivateBrowsingEnabled() : false);
Michael Kolb8233fac2010-10-26 16:08:53 -07001400 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001401 mInForeground = false;
1402
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001403 mDownloadListener = new DownloadListener() {
1404 public void onDownloadStart(String url, String userAgent,
1405 String contentDisposition, String mimetype,
1406 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001407 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001408 mimetype, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001409 }
1410 };
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001411 mWebBackForwardListClient = new WebBackForwardListClient() {
1412 @Override
1413 public void onNewHistoryItem(WebHistoryItem item) {
1414 if (isInVoiceSearchMode()) {
1415 item.setCustomData(mVoiceSearchData.mVoiceSearchIntent);
1416 }
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001417 if (mClearHistoryMatchUrl != null) {
1418 if (LOGD_ENABLED) {
1419 Log.d(LOGTAG, "onNewHistoryItem:\n\t" + item.getUrl() + "\n\t"
1420 + mClearHistoryMatchUrl);
1421 }
1422 if (TextUtils.equals(item.getOriginalUrl(), mClearHistoryMatchUrl)) {
1423 if (mMainView != null) {
1424 mMainView.clearHistory();
1425 }
1426 }
1427 mClearHistoryMatchUrl = null;
1428 }
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001429 }
1430 @Override
1431 public void onIndexChanged(WebHistoryItem item, int index) {
1432 Object data = item.getCustomData();
1433 if (data != null && data instanceof Intent) {
1434 activateVoiceSearchMode((Intent) data);
1435 }
1436 }
1437 };
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001438
John Reck1cf4b792011-07-26 10:22:22 -07001439 mCaptureWidth = mContext.getResources().getDimensionPixelSize(
1440 R.dimen.tab_thumbnail_width);
1441 mCaptureHeight = mContext.getResources().getDimensionPixelSize(
1442 R.dimen.tab_thumbnail_height);
1443 updateShouldCaptureThumbnails();
1444 restoreState(state);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001445 setWebView(w);
Michael Kolb9ef259a2011-07-12 15:33:08 -07001446 mHandler = new Handler() {
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001447 @Override
Michael Kolb9ef259a2011-07-12 15:33:08 -07001448 public void handleMessage(Message m) {
John Reck1cf4b792011-07-26 10:22:22 -07001449 switch (m.what) {
1450 case MSG_CAPTURE:
1451 capture();
1452 break;
1453 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001454 }
1455 };
John Reck1cf4b792011-07-26 10:22:22 -07001456 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001457
John Reck1cf4b792011-07-26 10:22:22 -07001458 public void updateShouldCaptureThumbnails() {
1459 if (mWebViewController.shouldCaptureThumbnails()) {
1460 synchronized (Tab.this) {
1461 if (mCapture == null) {
1462 mCapture = Bitmap.createBitmap(mCaptureWidth, mCaptureHeight,
1463 Bitmap.Config.RGB_565);
Michael Kolbc3af0672011-08-09 10:24:41 -07001464 mCapture.eraseColor(Color.WHITE);
John Reck1cf4b792011-07-26 10:22:22 -07001465 if (mInForeground) {
1466 postCapture();
1467 }
1468 }
1469 }
1470 } else {
1471 synchronized (Tab.this) {
1472 mCapture = null;
1473 deleteThumbnail();
1474 }
1475 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001476 }
1477
Michael Kolb14612442011-06-24 13:06:29 -07001478 public void setController(WebViewController ctl) {
1479 mWebViewController = ctl;
John Reck1cf4b792011-07-26 10:22:22 -07001480 updateShouldCaptureThumbnails();
Michael Kolb14612442011-06-24 13:06:29 -07001481 }
1482
Michael Kolbc831b632011-05-11 09:30:34 -07001483 public void setId(long id) {
1484 mId = id;
1485 }
1486
1487 public long getId() {
1488 return mId;
1489 }
1490
Grace Kloba22ac16e2009-10-07 18:00:23 -07001491 /**
1492 * Sets the WebView for this tab, correctly removing the old WebView from
1493 * the container view.
1494 */
1495 void setWebView(WebView w) {
1496 if (mMainView == w) {
1497 return;
1498 }
Michael Kolba713ec82010-11-29 17:27:06 -08001499
Grace Kloba22ac16e2009-10-07 18:00:23 -07001500 // If the WebView is changing, the page will be reloaded, so any ongoing
1501 // Geolocation permission requests are void.
Grace Kloba50c241e2010-04-20 11:07:50 -07001502 if (mGeolocationPermissionsPrompt != null) {
1503 mGeolocationPermissionsPrompt.hide();
1504 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001505
Michael Kolba713ec82010-11-29 17:27:06 -08001506 mWebViewController.onSetWebView(this, w);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001507
John Reck1cf4b792011-07-26 10:22:22 -07001508 if (mMainView != null) {
John Reck8ee633f2011-08-09 16:00:35 -07001509 mMainView.setPictureListener(null);
John Reck1cf4b792011-07-26 10:22:22 -07001510 if (w != null) {
1511 syncCurrentState(w, null);
1512 } else {
1513 mCurrentState = new PageState(mContext, false);
1514 }
1515 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001516 // set the new one
1517 mMainView = w;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001518 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001519 if (mMainView != null) {
1520 mMainView.setWebViewClient(mWebViewClient);
1521 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001522 // Attach DownloadManager so that downloads can start in an active
1523 // or a non-active window. This can happen when going to a site that
1524 // does a redirect after a period of time. The user could have
1525 // switched to another tab while waiting for the download to start.
1526 mMainView.setDownloadListener(mDownloadListener);
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001527 mMainView.setWebBackForwardListClient(mWebBackForwardListClient);
John Reck8ee633f2011-08-09 16:00:35 -07001528 TabControl tc = mWebViewController.getTabControl();
1529 if (tc != null && tc.getOnThumbnailUpdatedListener() != null) {
1530 mMainView.setPictureListener(this);
1531 }
John Reck1cf4b792011-07-26 10:22:22 -07001532 if (mSavedState != null) {
John Reck6c2e2f32011-08-22 13:41:23 -07001533 WebBackForwardList restoredState
1534 = mMainView.restoreState(mSavedState);
1535 if (restoredState == null || restoredState.getSize() == 0) {
1536 Log.w(LOGTAG, "Failed to restore WebView state!");
1537 loadUrl(mCurrentState.mOriginalUrl, null);
1538 }
John Reck1cf4b792011-07-26 10:22:22 -07001539 mSavedState = null;
1540 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001541 }
1542 }
1543
1544 /**
1545 * Destroy the tab's main WebView and subWindow if any
1546 */
1547 void destroy() {
1548 if (mMainView != null) {
1549 dismissSubWindow();
John Reckef654f12011-07-12 16:42:08 -07001550 // Make sure the embedded title bar isn't still attached
1551 mMainView.setEmbeddedTitleBar(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001552 // save the WebView to call destroy() after detach it from the tab
1553 WebView webView = mMainView;
1554 setWebView(null);
1555 webView.destroy();
1556 }
1557 }
1558
1559 /**
1560 * Remove the tab from the parent
1561 */
1562 void removeFromTree() {
1563 // detach the children
Michael Kolbc831b632011-05-11 09:30:34 -07001564 if (mChildren != null) {
1565 for(Tab t : mChildren) {
1566 t.setParent(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001567 }
1568 }
1569 // remove itself from the parent list
Michael Kolbc831b632011-05-11 09:30:34 -07001570 if (mParent != null) {
1571 mParent.mChildren.remove(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001572 }
John Reck1cf4b792011-07-26 10:22:22 -07001573 deleteThumbnail();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001574 }
1575
1576 /**
1577 * Create a new subwindow unless a subwindow already exists.
1578 * @return True if a new subwindow was created. False if one already exists.
1579 */
1580 boolean createSubWindow() {
1581 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001582 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001583 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001584 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001585 mSubView.setWebChromeClient(new SubWindowChromeClient(
1586 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001587 // Set a different DownloadListener for the mSubView, since it will
1588 // just need to dismiss the mSubView, rather than close the Tab
1589 mSubView.setDownloadListener(new DownloadListener() {
1590 public void onDownloadStart(String url, String userAgent,
1591 String contentDisposition, String mimetype,
1592 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001593 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001594 contentDisposition, mimetype, contentLength);
1595 if (mSubView.copyBackForwardList().getSize() == 0) {
1596 // This subwindow was opened for the sole purpose of
1597 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001598 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001599 }
1600 }
1601 });
Michael Kolb14612442011-06-24 13:06:29 -07001602 mSubView.setOnCreateContextMenuListener(mWebViewController.getActivity());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001603 return true;
1604 }
1605 return false;
1606 }
1607
1608 /**
1609 * Dismiss the subWindow for the tab.
1610 */
1611 void dismissSubWindow() {
1612 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001613 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001614 mSubView.destroy();
1615 mSubView = null;
1616 mSubViewContainer = null;
1617 }
1618 }
1619
Grace Kloba22ac16e2009-10-07 18:00:23 -07001620
1621 /**
1622 * Set the parent tab of this tab.
1623 */
Michael Kolbc831b632011-05-11 09:30:34 -07001624 void setParent(Tab parent) {
1625 mParent = parent;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001626 // This tab may have been freed due to low memory. If that is the case,
Michael Kolbc831b632011-05-11 09:30:34 -07001627 // the parent tab id is already saved. If we are changing that id
Grace Kloba22ac16e2009-10-07 18:00:23 -07001628 // (most likely due to removing the parent tab) we must update the
Michael Kolbc831b632011-05-11 09:30:34 -07001629 // parent tab id in the saved Bundle.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001630 if (mSavedState != null) {
1631 if (parent == null) {
1632 mSavedState.remove(PARENTTAB);
1633 } else {
Michael Kolbc831b632011-05-11 09:30:34 -07001634 mSavedState.putLong(PARENTTAB, parent.getId());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001635 }
1636 }
John Reckb0a86db2011-05-24 14:05:58 -07001637
1638 // Sync the WebView useragent with the parent
1639 if (parent != null && mSettings.hasDesktopUseragent(parent.getWebView())
1640 != mSettings.hasDesktopUseragent(getWebView())) {
1641 mSettings.toggleDesktopUseragent(getWebView());
1642 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001643 }
1644
1645 /**
Michael Kolbc831b632011-05-11 09:30:34 -07001646 * If this Tab was created through another Tab, then this method returns
1647 * that Tab.
1648 * @return the Tab parent or null
1649 */
1650 public Tab getParent() {
1651 return mParent;
1652 }
1653
1654 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001655 * When a Tab is created through the content of another Tab, then we
1656 * associate the Tabs.
1657 * @param child the Tab that was created from this Tab
1658 */
1659 void addChildTab(Tab child) {
Michael Kolbc831b632011-05-11 09:30:34 -07001660 if (mChildren == null) {
1661 mChildren = new Vector<Tab>();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001662 }
Michael Kolbc831b632011-05-11 09:30:34 -07001663 mChildren.add(child);
1664 child.setParent(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001665 }
1666
Michael Kolbc831b632011-05-11 09:30:34 -07001667 Vector<Tab> getChildren() {
1668 return mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001669 }
1670
1671 void resume() {
1672 if (mMainView != null) {
John Reck56c1fcf2011-08-17 10:15:16 -07001673 setupHwAcceleration(mMainView);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001674 mMainView.onResume();
1675 if (mSubView != null) {
1676 mSubView.onResume();
1677 }
1678 }
1679 }
1680
John Reck56c1fcf2011-08-17 10:15:16 -07001681 private void setupHwAcceleration(View web) {
1682 if (web == null) return;
1683 BrowserSettings settings = BrowserSettings.getInstance();
1684 if (settings.isHardwareAccelerated()) {
1685 web.setLayerType(View.LAYER_TYPE_NONE, null);
1686 } else {
1687 web.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
1688 }
1689 }
1690
Grace Kloba22ac16e2009-10-07 18:00:23 -07001691 void pause() {
1692 if (mMainView != null) {
1693 mMainView.onPause();
1694 if (mSubView != null) {
1695 mSubView.onPause();
1696 }
1697 }
1698 }
1699
1700 void putInForeground() {
John Reck8ee633f2011-08-09 16:00:35 -07001701 if (mInForeground) {
1702 return;
1703 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001704 mInForeground = true;
1705 resume();
Michael Kolb14612442011-06-24 13:06:29 -07001706 Activity activity = mWebViewController.getActivity();
1707 mMainView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001708 if (mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -07001709 mSubView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001710 }
1711 // Show the pending error dialog if the queue is not empty
1712 if (mQueuedErrors != null && mQueuedErrors.size() > 0) {
1713 showError(mQueuedErrors.getFirst());
1714 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001715 mWebViewController.bookmarkedStatusHasChanged(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001716 }
1717
1718 void putInBackground() {
John Reck8ee633f2011-08-09 16:00:35 -07001719 if (!mInForeground) {
1720 return;
1721 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001722 mInForeground = false;
1723 pause();
1724 mMainView.setOnCreateContextMenuListener(null);
1725 if (mSubView != null) {
1726 mSubView.setOnCreateContextMenuListener(null);
1727 }
1728 }
1729
Michael Kolb8233fac2010-10-26 16:08:53 -07001730 boolean inForeground() {
1731 return mInForeground;
1732 }
1733
Grace Kloba22ac16e2009-10-07 18:00:23 -07001734 /**
1735 * Return the top window of this tab; either the subwindow if it is not
1736 * null or the main window.
1737 * @return The top window of this tab.
1738 */
1739 WebView getTopWindow() {
1740 if (mSubView != null) {
1741 return mSubView;
1742 }
1743 return mMainView;
1744 }
1745
1746 /**
1747 * Return the main window of this tab. Note: if a tab is freed in the
1748 * background, this can return null. It is only guaranteed to be
1749 * non-null for the current tab.
1750 * @return The main WebView of this tab.
1751 */
1752 WebView getWebView() {
1753 return mMainView;
1754 }
1755
Michael Kolba713ec82010-11-29 17:27:06 -08001756 void setViewContainer(View container) {
1757 mContainer = container;
1758 }
1759
Michael Kolb8233fac2010-10-26 16:08:53 -07001760 View getViewContainer() {
1761 return mContainer;
1762 }
1763
Grace Kloba22ac16e2009-10-07 18:00:23 -07001764 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001765 * Return whether private browsing is enabled for the main window of
1766 * this tab.
1767 * @return True if private browsing is enabled.
1768 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001769 boolean isPrivateBrowsingEnabled() {
John Reck502a3532011-08-16 14:21:46 -07001770 return mCurrentState.mIncognito;
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001771 }
1772
1773 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001774 * Return the subwindow of this tab or null if there is no subwindow.
1775 * @return The subwindow of this tab or null.
1776 */
1777 WebView getSubWebView() {
1778 return mSubView;
1779 }
1780
Michael Kolb1514bb72010-11-22 09:11:48 -08001781 void setSubWebView(WebView subView) {
1782 mSubView = subView;
1783 }
1784
Michael Kolb8233fac2010-10-26 16:08:53 -07001785 View getSubViewContainer() {
1786 return mSubViewContainer;
1787 }
1788
Michael Kolb1514bb72010-11-22 09:11:48 -08001789 void setSubViewContainer(View subViewContainer) {
1790 mSubViewContainer = subViewContainer;
1791 }
1792
Grace Kloba22ac16e2009-10-07 18:00:23 -07001793 /**
1794 * @return The geolocation permissions prompt for this tab.
1795 */
1796 GeolocationPermissionsPrompt getGeolocationPermissionsPrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001797 if (mGeolocationPermissionsPrompt == null) {
1798 ViewStub stub = (ViewStub) mContainer
1799 .findViewById(R.id.geolocation_permissions_prompt);
1800 mGeolocationPermissionsPrompt = (GeolocationPermissionsPrompt) stub
1801 .inflate();
Grace Kloba50c241e2010-04-20 11:07:50 -07001802 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001803 return mGeolocationPermissionsPrompt;
1804 }
1805
1806 /**
1807 * @return The application id string
1808 */
1809 String getAppId() {
1810 return mAppId;
1811 }
1812
1813 /**
1814 * Set the application id string
1815 * @param id
1816 */
1817 void setAppId(String id) {
1818 mAppId = id;
1819 }
1820
Michael Kolbe28b3472011-08-04 16:54:31 -07001821 boolean closeOnBack() {
1822 return mCloseOnBack;
1823 }
1824
1825 void setCloseOnBack(boolean close) {
1826 mCloseOnBack = close;
1827 }
1828
Grace Kloba22ac16e2009-10-07 18:00:23 -07001829 String getUrl() {
John Reck324d4402011-01-11 16:56:42 -08001830 return UrlUtils.filteredUrl(mCurrentState.mUrl);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001831 }
1832
John Reck49a603c2011-03-03 09:33:05 -08001833 String getOriginalUrl() {
John Reckdb22ec42011-06-29 11:31:24 -07001834 if (mCurrentState.mOriginalUrl == null) {
1835 return getUrl();
John Reck49a603c2011-03-03 09:33:05 -08001836 }
John Reckdb22ec42011-06-29 11:31:24 -07001837 return UrlUtils.filteredUrl(mCurrentState.mOriginalUrl);
John Reck49a603c2011-03-03 09:33:05 -08001838 }
1839
Grace Kloba22ac16e2009-10-07 18:00:23 -07001840 /**
John Reck30c714c2010-12-16 17:30:34 -08001841 * Get the title of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001842 */
1843 String getTitle() {
John Reck30c714c2010-12-16 17:30:34 -08001844 if (mCurrentState.mTitle == null && mInPageLoad) {
Michael Kolb14612442011-06-24 13:06:29 -07001845 return mContext.getString(R.string.title_bar_loading);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001846 }
John Reck30c714c2010-12-16 17:30:34 -08001847 return mCurrentState.mTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001848 }
1849
1850 /**
John Reck30c714c2010-12-16 17:30:34 -08001851 * Get the favicon of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001852 */
1853 Bitmap getFavicon() {
John Reck1cf4b792011-07-26 10:22:22 -07001854 if (mCurrentState.mFavicon != null) {
1855 return mCurrentState.mFavicon;
1856 }
1857 return getDefaultFavicon(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001858 }
1859
John Recke969cc52010-12-21 17:24:43 -08001860 public boolean isBookmarkedSite() {
1861 return mCurrentState.mIsBookmarkedSite;
1862 }
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001863
Grace Kloba22ac16e2009-10-07 18:00:23 -07001864 /**
1865 * Return the tab's error console. Creates the console if createIfNEcessary
1866 * is true and we haven't already created the console.
1867 * @param createIfNecessary Flag to indicate if the console should be
1868 * created if it has not been already.
1869 * @return The tab's error console, or null if one has not been created and
1870 * createIfNecessary is false.
1871 */
1872 ErrorConsoleView getErrorConsole(boolean createIfNecessary) {
1873 if (createIfNecessary && mErrorConsole == null) {
Michael Kolb14612442011-06-24 13:06:29 -07001874 mErrorConsole = new ErrorConsoleView(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001875 mErrorConsole.setWebView(mMainView);
1876 }
1877 return mErrorConsole;
1878 }
1879
John Reck30c714c2010-12-16 17:30:34 -08001880 private void setLockIconType(LockIcon icon) {
1881 mCurrentState.mLockIcon = icon;
1882 mWebViewController.onUpdatedLockIcon(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001883 }
1884
1885 /**
1886 * @return The tab's lock icon type.
1887 */
John Reck30c714c2010-12-16 17:30:34 -08001888 LockIcon getLockIconType() {
1889 return mCurrentState.mLockIcon;
1890 }
1891
1892 int getLoadProgress() {
1893 if (mInPageLoad) {
1894 return mPageLoadProgress;
1895 }
1896 return 100;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001897 }
1898
1899 /**
1900 * @return TRUE if onPageStarted is called while onPageFinished is not
1901 * called yet.
1902 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001903 boolean inPageLoad() {
1904 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001905 }
1906
1907 // force mInLoad to be false. This should only be called before closing the
1908 // tab to ensure BrowserActivity's pauseWebViewTimers() is called correctly.
Michael Kolb8233fac2010-10-26 16:08:53 -07001909 void clearInPageLoad() {
1910 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001911 }
1912
Grace Kloba22ac16e2009-10-07 18:00:23 -07001913 /**
John Reck1cf4b792011-07-26 10:22:22 -07001914 * @return The Bundle with the tab's state if it can be saved, otherwise null
Grace Kloba22ac16e2009-10-07 18:00:23 -07001915 */
John Reck1cf4b792011-07-26 10:22:22 -07001916 public Bundle saveState() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001917 // If the WebView is null it means we ran low on memory and we already
1918 // stored the saved state in mSavedState.
1919 if (mMainView == null) {
John Reck1cf4b792011-07-26 10:22:22 -07001920 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001921 }
John Reck6c2e2f32011-08-22 13:41:23 -07001922
1923 if (TextUtils.isEmpty(mCurrentState.mUrl)) {
John Reck1cf4b792011-07-26 10:22:22 -07001924 return null;
John Reck24f18262011-06-17 14:47:20 -07001925 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001926
1927 mSavedState = new Bundle();
John Reck6c2e2f32011-08-22 13:41:23 -07001928 WebBackForwardList savedList = mMainView.saveState(mSavedState);
1929 if (savedList == null || savedList.getSize() == 0) {
1930 Log.w(LOGTAG, "Failed to save back/forward list for "
1931 + mCurrentState.mUrl);
1932 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001933
Michael Kolbc831b632011-05-11 09:30:34 -07001934 mSavedState.putLong(ID, mId);
John Reck30c714c2010-12-16 17:30:34 -08001935 mSavedState.putString(CURRURL, mCurrentState.mUrl);
1936 mSavedState.putString(CURRTITLE, mCurrentState.mTitle);
John Reck1cf4b792011-07-26 10:22:22 -07001937 mSavedState.putBoolean(INCOGNITO, mMainView.isPrivateBrowsingEnabled());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001938 if (mAppId != null) {
1939 mSavedState.putString(APPID, mAppId);
1940 }
Michael Kolbe28b3472011-08-04 16:54:31 -07001941 mSavedState.putBoolean(CLOSEFLAG, mCloseOnBack);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001942 // Remember the parent tab so the relationship can be restored.
Michael Kolbc831b632011-05-11 09:30:34 -07001943 if (mParent != null) {
1944 mSavedState.putLong(PARENTTAB, mParent.mId);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001945 }
John Reckb0a86db2011-05-24 14:05:58 -07001946 mSavedState.putBoolean(USERAGENT,
1947 mSettings.hasDesktopUseragent(getWebView()));
John Reck1cf4b792011-07-26 10:22:22 -07001948 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001949 }
1950
1951 /*
1952 * Restore the state of the tab.
1953 */
John Reck1cf4b792011-07-26 10:22:22 -07001954 private void restoreState(Bundle b) {
1955 mSavedState = b;
1956 if (mSavedState == null) {
1957 return;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001958 }
1959 // Restore the internal state even if the WebView fails to restore.
1960 // This will maintain the app id, original url and close-on-exit values.
Michael Kolbc831b632011-05-11 09:30:34 -07001961 mId = b.getLong(ID);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001962 mAppId = b.getString(APPID);
Michael Kolbe28b3472011-08-04 16:54:31 -07001963 mCloseOnBack = b.getBoolean(CLOSEFLAG);
John Reckb0a86db2011-05-24 14:05:58 -07001964 if (b.getBoolean(USERAGENT)
1965 != mSettings.hasDesktopUseragent(getWebView())) {
1966 mSettings.toggleDesktopUseragent(getWebView());
1967 }
John Reck1cf4b792011-07-26 10:22:22 -07001968 String url = b.getString(CURRURL);
1969 String title = b.getString(CURRTITLE);
1970 boolean incognito = b.getBoolean(INCOGNITO);
1971 mCurrentState = new PageState(mContext, incognito, url, null);
1972 mCurrentState.mTitle = title;
1973 synchronized (Tab.this) {
1974 if (mCapture != null) {
1975 BackgroundHandler.execute(mLoadThumbnail);
1976 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001977 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001978 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04001979
Leon Scroggins1961ed22010-12-07 15:22:21 -05001980 public void updateBookmarkedStatus() {
John Recke969cc52010-12-21 17:24:43 -08001981 mDataController.queryBookmarkStatus(getUrl(), mIsBookmarkCallback);
Leon Scroggins1961ed22010-12-07 15:22:21 -05001982 }
1983
John Recke969cc52010-12-21 17:24:43 -08001984 private DataController.OnQueryUrlIsBookmark mIsBookmarkCallback
1985 = new DataController.OnQueryUrlIsBookmark() {
1986 @Override
1987 public void onQueryUrlIsBookmark(String url, boolean isBookmark) {
1988 if (mCurrentState.mUrl.equals(url)) {
1989 mCurrentState.mIsBookmarkedSite = isBookmark;
1990 mWebViewController.bookmarkedStatusHasChanged(Tab.this);
1991 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001992 }
John Recke969cc52010-12-21 17:24:43 -08001993 };
Michael Kolb1acef692011-03-08 14:12:06 -08001994
Michael Kolbeb95db42011-03-03 10:38:40 -08001995 public Bitmap getScreenshot() {
John Reck1cf4b792011-07-26 10:22:22 -07001996 synchronized (Tab.this) {
1997 return mCapture;
1998 }
Michael Kolbeb95db42011-03-03 10:38:40 -08001999 }
2000
John Reck541f55a2011-06-07 16:34:43 -07002001 public boolean isSnapshot() {
John Reck541f55a2011-06-07 16:34:43 -07002002 return false;
2003 }
2004
John Reckd8c74522011-06-14 08:45:00 -07002005 public ContentValues createSnapshotValues() {
2006 if (mMainView == null) return null;
John Reck8cc92352011-07-06 17:41:52 -07002007 ByteArrayOutputStream bos = new ByteArrayOutputStream();
2008 try {
2009 GZIPOutputStream stream = new GZIPOutputStream(bos);
2010 if (!mMainView.saveViewState(stream)) {
2011 return null;
2012 }
2013 stream.flush();
2014 stream.close();
2015 } catch (Exception e) {
2016 Log.w(LOGTAG, "Failed to save view state", e);
John Reck541f55a2011-06-07 16:34:43 -07002017 return null;
2018 }
John Reck8cc92352011-07-06 17:41:52 -07002019 byte[] data = bos.toByteArray();
John Reckd8c74522011-06-14 08:45:00 -07002020 ContentValues values = new ContentValues();
2021 values.put(Snapshots.TITLE, mCurrentState.mTitle);
2022 values.put(Snapshots.URL, mCurrentState.mUrl);
2023 values.put(Snapshots.VIEWSTATE, data);
2024 values.put(Snapshots.BACKGROUND, mMainView.getPageBackgroundColor());
John Reck8cc92352011-07-06 17:41:52 -07002025 values.put(Snapshots.DATE_CREATED, System.currentTimeMillis());
2026 values.put(Snapshots.FAVICON, compressBitmap(getFavicon()));
2027 Bitmap screenshot = Controller.createScreenshot(mMainView,
2028 Controller.getDesiredThumbnailWidth(mContext),
2029 Controller.getDesiredThumbnailHeight(mContext));
2030 values.put(Snapshots.THUMBNAIL, compressBitmap(screenshot));
John Reckd8c74522011-06-14 08:45:00 -07002031 return values;
John Reck541f55a2011-06-07 16:34:43 -07002032 }
2033
John Reck8cc92352011-07-06 17:41:52 -07002034 public byte[] compressBitmap(Bitmap bitmap) {
2035 if (bitmap == null) {
2036 return null;
2037 }
2038 ByteArrayOutputStream stream = new ByteArrayOutputStream();
2039 bitmap.compress(CompressFormat.PNG, 100, stream);
2040 return stream.toByteArray();
2041 }
2042
John Reck26b18322011-06-21 13:08:58 -07002043 public void loadUrl(String url, Map<String, String> headers) {
2044 if (mMainView != null) {
Michael Kolb14612442011-06-24 13:06:29 -07002045 mCurrentState = new PageState(mContext, false, url, null);
John Reck26b18322011-06-21 13:08:58 -07002046 mWebViewController.onPageStarted(this, mMainView, null);
2047 mMainView.loadUrl(url, headers);
2048 }
2049 }
2050
Michael Kolb9ef259a2011-07-12 15:33:08 -07002051 protected void capture() {
2052 if (mMainView == null || mCapture == null) return;
2053 Canvas c = new Canvas(mCapture);
2054 final int left = mMainView.getScrollX();
2055 final int top = mMainView.getScrollY() + mMainView.getVisibleTitleHeight();
2056 c.translate(-left, -top);
2057 float scale = mCaptureWidth / (float) mMainView.getWidth();
2058 c.scale(scale, scale, left, top);
John Reck8ee633f2011-08-09 16:00:35 -07002059 if (mMainView instanceof BrowserWebView) {
2060 ((BrowserWebView)mMainView).drawContent(c);
2061 } else {
2062 mMainView.draw(c);
2063 }
Dianne Hackborn43cfe8a2011-08-02 16:59:35 -07002064 c.setBitmap(null);
John Reck8ee633f2011-08-09 16:00:35 -07002065 mHandler.removeMessages(MSG_CAPTURE);
John Reck1cf4b792011-07-26 10:22:22 -07002066 persistThumbnail();
John Reck8ee633f2011-08-09 16:00:35 -07002067 TabControl tc = mWebViewController.getTabControl();
2068 if (tc != null) {
2069 OnThumbnailUpdatedListener updateListener
2070 = tc.getOnThumbnailUpdatedListener();
2071 if (updateListener != null) {
2072 updateListener.onThumbnailUpdated(this);
2073 }
2074 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07002075 }
2076
2077 @Override
2078 public void onNewPicture(WebView view, Picture picture) {
2079 //update screenshot
John Reck1cf4b792011-07-26 10:22:22 -07002080 postCapture();
2081 }
2082
2083 private void postCapture() {
Michael Kolb9ef259a2011-07-12 15:33:08 -07002084 if (!mHandler.hasMessages(MSG_CAPTURE)) {
2085 mHandler.sendEmptyMessageDelayed(MSG_CAPTURE, CAPTURE_DELAY);
2086 }
2087 }
2088
John Reckef654f12011-07-12 16:42:08 -07002089 public boolean canGoBack() {
2090 return mMainView != null ? mMainView.canGoBack() : false;
2091 }
2092
2093 public boolean canGoForward() {
2094 return mMainView != null ? mMainView.canGoForward() : false;
2095 }
2096
2097 public void goBack() {
2098 if (mMainView != null) {
2099 mMainView.goBack();
2100 }
2101 }
2102
2103 public void goForward() {
2104 if (mMainView != null) {
2105 mMainView.goForward();
2106 }
2107 }
2108
Mathew Inwood1dd8e822011-08-03 14:34:29 +01002109 /**
2110 * Causes the tab back/forward stack to be cleared once, if the given URL is the next URL
2111 * to be added to the stack.
2112 *
2113 * This is used to ensure that preloaded URLs that are not subsequently seen by the user do
2114 * not appear in the back stack.
2115 */
2116 public void clearBackStackWhenItemAdded(String urlToMatch) {
2117 mClearHistoryMatchUrl = urlToMatch;
2118 }
2119
John Reck1cf4b792011-07-26 10:22:22 -07002120 protected void persistThumbnail() {
2121 BackgroundHandler.execute(mSaveThumbnail);
2122 }
2123
2124 protected void deleteThumbnail() {
2125 BackgroundHandler.execute(mDeleteThumbnail);
2126 }
2127
2128 private void updateCaptureFromBlob(byte[] blob) {
2129 synchronized (Tab.this) {
2130 if (mCapture == null) {
2131 return;
2132 }
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002133 ByteBuffer buffer = ByteBuffer.wrap(blob);
Michael Kolbd837a112011-08-09 14:00:18 -07002134 try {
2135 mCapture.copyPixelsFromBuffer(buffer);
2136 } catch (RuntimeException rex) {
2137 Log.e(LOGTAG, "Load capture has mismatched sizes; buffer: "
2138 + buffer.capacity() + " blob: " + blob.length
2139 + "capture: " + mCapture.getByteCount());
2140 throw rex;
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002141 }
John Reck1cf4b792011-07-26 10:22:22 -07002142 }
2143 }
2144
2145 private byte[] getCaptureBlob() {
2146 synchronized (Tab.this) {
2147 if (mCapture == null) {
2148 return null;
2149 }
2150 ByteBuffer buffer = ByteBuffer.allocate(mCapture.getByteCount());
2151 mCapture.copyPixelsToBuffer(buffer);
2152 return buffer.array();
2153 }
2154 }
2155
2156 private Runnable mSaveThumbnail = new Runnable() {
2157
2158 @Override
2159 public void run() {
2160 byte[] blob = getCaptureBlob();
2161 if (blob == null) {
2162 return;
2163 }
2164 ContentResolver cr = mContext.getContentResolver();
2165 ContentValues values = new ContentValues();
2166 values.put(Thumbnails._ID, mId);
2167 values.put(Thumbnails.THUMBNAIL, blob);
2168 cr.insert(Thumbnails.CONTENT_URI, values);
2169 }
2170 };
2171
2172 private Runnable mDeleteThumbnail = new Runnable() {
2173
2174 @Override
2175 public void run() {
2176 ContentResolver cr = mContext.getContentResolver();
2177 try {
2178 cr.delete(ContentUris.withAppendedId(Thumbnails.CONTENT_URI, mId),
2179 null, null);
2180 } catch (Throwable t) {}
2181 }
2182 };
2183
2184 private Runnable mLoadThumbnail = new Runnable() {
2185
2186 @Override
2187 public void run() {
2188 ContentResolver cr = mContext.getContentResolver();
2189 Cursor c = null;
2190 try {
2191 Uri uri = ContentUris.withAppendedId(Thumbnails.CONTENT_URI, mId);
2192 c = cr.query(uri, new String[] {Thumbnails._ID,
2193 Thumbnails.THUMBNAIL}, null, null, null);
2194 if (c.moveToFirst()) {
2195 byte[] data = c.getBlob(1);
2196 if (data != null && data.length > 0) {
2197 updateCaptureFromBlob(data);
2198 }
2199 }
2200 } finally {
2201 if (c != null) {
2202 c.close();
2203 }
2204 }
2205 }
2206 };
2207
Grace Kloba22ac16e2009-10-07 18:00:23 -07002208}