blob: 8bf32b0f23189fb6950414df6fb1703b9bbce7f7 [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()) {
John Reckcb28b2c2011-08-26 17:39:44 -0700747 new AlertDialog.Builder(mContext)
748 .setTitle(R.string.security_warning)
749 .setMessage(R.string.ssl_warnings_header)
750 .setIcon(android.R.drawable.ic_dialog_alert)
751 .setPositiveButton(R.string.ssl_continue,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700752 new DialogInterface.OnClickListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700753 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700754 public void onClick(DialogInterface dialog,
755 int whichButton) {
756 handler.proceed();
757 }
John Reckcb28b2c2011-08-26 17:39:44 -0700758 })
759 .setNeutralButton(R.string.view_certificate,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700760 new DialogInterface.OnClickListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700761 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700762 public void onClick(DialogInterface dialog,
763 int whichButton) {
John Reckcb28b2c2011-08-26 17:39:44 -0700764 mWebViewController.showSslCertificateOnError(
765 view, handler, error);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700766 }
John Reckcb28b2c2011-08-26 17:39:44 -0700767 })
768 .setNegativeButton(R.string.ssl_go_back,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700769 new DialogInterface.OnClickListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700770 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700771 public void onClick(DialogInterface dialog,
772 int whichButton) {
John Reck30c714c2010-12-16 17:30:34 -0800773 dialog.cancel();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700774 }
John Reckcb28b2c2011-08-26 17:39:44 -0700775 })
776 .setOnCancelListener(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700777 new DialogInterface.OnCancelListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700778 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700779 public void onCancel(DialogInterface dialog) {
780 handler.cancel();
John Reck30c714c2010-12-16 17:30:34 -0800781 setLockIconType(LockIcon.LOCK_ICON_UNSECURE);
782 mWebViewController.onUserCanceledSsl(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700783 }
John Reckcb28b2c2011-08-26 17:39:44 -0700784 })
785 .show();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700786 } else {
787 handler.proceed();
788 }
789 }
790
791 /**
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700792 * Displays client certificate request to the user.
793 */
794 @Override
795 public void onReceivedClientCertRequest(final WebView view,
796 final ClientCertRequestHandler handler, final String host_and_port) {
797 if (!mInForeground) {
798 handler.ignore();
799 return;
800 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700801 int colon = host_and_port.lastIndexOf(':');
802 String host;
803 int port;
804 if (colon == -1) {
805 host = host_and_port;
806 port = -1;
807 } else {
808 String portString = host_and_port.substring(colon + 1);
809 try {
810 port = Integer.parseInt(portString);
811 host = host_and_port.substring(0, colon);
812 } catch (NumberFormatException e) {
813 host = host_and_port;
814 port = -1;
815 }
816 }
Michael Kolb14612442011-06-24 13:06:29 -0700817 KeyChain.choosePrivateKeyAlias(
818 mWebViewController.getActivity(), new KeyChainAliasCallback() {
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700819 @Override public void alias(String alias) {
820 if (alias == null) {
821 handler.cancel();
822 return;
823 }
Michael Kolb14612442011-06-24 13:06:29 -0700824 new KeyChainLookup(mContext, handler, alias).execute();
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700825 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700826 }, null, null, host, port, null);
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700827 }
828
829 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700830 * Handles an HTTP authentication request.
831 *
832 * @param handler The authentication handler
833 * @param host The host
834 * @param realm The realm
835 */
836 @Override
837 public void onReceivedHttpAuthRequest(WebView view,
838 final HttpAuthHandler handler, final String host,
839 final String realm) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700840 mWebViewController.onReceivedHttpAuthRequest(Tab.this, view, handler, host, realm);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700841 }
842
843 @Override
John Reck438bf462011-01-12 18:11:46 -0800844 public WebResourceResponse shouldInterceptRequest(WebView view,
845 String url) {
846 WebResourceResponse res = HomeProvider.shouldInterceptRequest(
Michael Kolb14612442011-06-24 13:06:29 -0700847 mContext, url);
John Reck438bf462011-01-12 18:11:46 -0800848 return res;
849 }
850
851 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700852 public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
853 if (!mInForeground) {
854 return false;
855 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700856 return mWebViewController.shouldOverrideKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700857 }
858
859 @Override
860 public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700861 if (!mInForeground) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700862 return;
863 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700864 mWebViewController.onUnhandledKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700865 }
Patrick Scott92066772011-03-10 08:46:27 -0500866
867 @Override
868 public void onReceivedLoginRequest(WebView view, String realm,
869 String account, String args) {
Michael Kolb14612442011-06-24 13:06:29 -0700870 new DeviceAccountLogin(mWebViewController.getActivity(), view, Tab.this, mWebViewController)
Patrick Scott92066772011-03-10 08:46:27 -0500871 .handleLogin(realm, account, args);
872 }
873
Grace Kloba22ac16e2009-10-07 18:00:23 -0700874 };
875
John Reck1cf4b792011-07-26 10:22:22 -0700876 private void syncCurrentState(WebView view, String url) {
877 // Sync state (in case of stop/timeout)
878 mCurrentState.mUrl = view.getUrl();
879 if (mCurrentState.mUrl == null) {
John Reck8bcafc12011-08-29 16:43:02 -0700880 mCurrentState.mUrl = "";
John Reck1cf4b792011-07-26 10:22:22 -0700881 }
882 mCurrentState.mOriginalUrl = view.getOriginalUrl();
883 mCurrentState.mTitle = view.getTitle();
884 mCurrentState.mFavicon = view.getFavicon();
885 if (!URLUtil.isHttpsUrl(mCurrentState.mUrl)) {
886 // In case we stop when loading an HTTPS page from an HTTP page
887 // but before a provisional load occurred
888 mCurrentState.mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
889 }
John Reck502a3532011-08-16 14:21:46 -0700890 mCurrentState.mIncognito = view.isPrivateBrowsingEnabled();
John Reck1cf4b792011-07-26 10:22:22 -0700891 }
892
Patrick Scott92066772011-03-10 08:46:27 -0500893 // Called by DeviceAccountLogin when the Tab needs to have the auto-login UI
894 // displayed.
895 void setDeviceAccountLogin(DeviceAccountLogin login) {
896 mDeviceAccountLogin = login;
897 }
898
899 // Returns non-null if the title bar should display the auto-login UI.
900 DeviceAccountLogin getDeviceAccountLogin() {
901 return mDeviceAccountLogin;
902 }
903
Grace Kloba22ac16e2009-10-07 18:00:23 -0700904 // -------------------------------------------------------------------------
905 // WebChromeClient implementation for the main WebView
906 // -------------------------------------------------------------------------
907
908 private final WebChromeClient mWebChromeClient = new WebChromeClient() {
909 // Helper method to create a new tab or sub window.
910 private void createWindow(final boolean dialog, final Message msg) {
911 WebView.WebViewTransport transport =
912 (WebView.WebViewTransport) msg.obj;
913 if (dialog) {
914 createSubWindow();
Michael Kolb8233fac2010-10-26 16:08:53 -0700915 mWebViewController.attachSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700916 transport.setWebView(mSubView);
917 } else {
Michael Kolb7bcafde2011-05-09 13:55:59 -0700918 final Tab newTab = mWebViewController.openTab(null,
John Reck5949c662011-05-27 09:52:29 -0700919 Tab.this, true, true);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700920 transport.setWebView(newTab.getWebView());
921 }
922 msg.sendToTarget();
923 }
924
925 @Override
926 public boolean onCreateWindow(WebView view, final boolean dialog,
927 final boolean userGesture, final Message resultMsg) {
928 // only allow new window or sub window for the foreground case
929 if (!mInForeground) {
930 return false;
931 }
932 // Short-circuit if we can't create any more tabs or sub windows.
933 if (dialog && mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -0700934 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700935 .setTitle(R.string.too_many_subwindows_dialog_title)
936 .setIcon(android.R.drawable.ic_dialog_alert)
937 .setMessage(R.string.too_many_subwindows_dialog_message)
938 .setPositiveButton(R.string.ok, null)
939 .show();
940 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700941 } else if (!mWebViewController.getTabControl().canCreateNewTab()) {
Michael Kolb14612442011-06-24 13:06:29 -0700942 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700943 .setTitle(R.string.too_many_windows_dialog_title)
944 .setIcon(android.R.drawable.ic_dialog_alert)
945 .setMessage(R.string.too_many_windows_dialog_message)
946 .setPositiveButton(R.string.ok, null)
947 .show();
948 return false;
949 }
950
951 // Short-circuit if this was a user gesture.
952 if (userGesture) {
953 createWindow(dialog, resultMsg);
954 return true;
955 }
956
957 // Allow the popup and create the appropriate window.
958 final AlertDialog.OnClickListener allowListener =
959 new AlertDialog.OnClickListener() {
960 public void onClick(DialogInterface d,
961 int which) {
962 createWindow(dialog, resultMsg);
963 }
964 };
965
966 // Block the popup by returning a null WebView.
967 final AlertDialog.OnClickListener blockListener =
968 new AlertDialog.OnClickListener() {
969 public void onClick(DialogInterface d, int which) {
970 resultMsg.sendToTarget();
971 }
972 };
973
974 // Build a confirmation dialog to display to the user.
975 final AlertDialog d =
Michael Kolb14612442011-06-24 13:06:29 -0700976 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700977 .setTitle(R.string.attention)
978 .setIcon(android.R.drawable.ic_dialog_alert)
979 .setMessage(R.string.popup_window_attempt)
980 .setPositiveButton(R.string.allow, allowListener)
981 .setNegativeButton(R.string.block, blockListener)
982 .setCancelable(false)
983 .create();
984
985 // Show the confirmation dialog.
986 d.show();
987 return true;
988 }
989
990 @Override
Patrick Scotteb5061b2009-11-18 15:00:30 -0500991 public void onRequestFocus(WebView view) {
992 if (!mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700993 mWebViewController.switchToTab(Tab.this);
Patrick Scotteb5061b2009-11-18 15:00:30 -0500994 }
995 }
996
997 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700998 public void onCloseWindow(WebView window) {
Michael Kolbc831b632011-05-11 09:30:34 -0700999 if (mParent != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001000 // JavaScript can only close popup window.
1001 if (mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -07001002 mWebViewController.switchToTab(mParent);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001003 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001004 mWebViewController.closeTab(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001005 }
1006 }
1007
1008 @Override
1009 public void onProgressChanged(WebView view, int newProgress) {
John Reck30c714c2010-12-16 17:30:34 -08001010 mPageLoadProgress = newProgress;
1011 mWebViewController.onProgressChanged(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001012 }
1013
1014 @Override
Leon Scroggins21d9b902010-03-11 09:33:11 -05001015 public void onReceivedTitle(WebView view, final String title) {
John Reck30c714c2010-12-16 17:30:34 -08001016 mCurrentState.mTitle = title;
Michael Kolb8233fac2010-10-26 16:08:53 -07001017 mWebViewController.onReceivedTitle(Tab.this, title);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001018 }
1019
1020 @Override
1021 public void onReceivedIcon(WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -08001022 mCurrentState.mFavicon = icon;
Michael Kolb8233fac2010-10-26 16:08:53 -07001023 mWebViewController.onFavicon(Tab.this, view, icon);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001024 }
1025
1026 @Override
1027 public void onReceivedTouchIconUrl(WebView view, String url,
1028 boolean precomposed) {
Michael Kolb14612442011-06-24 13:06:29 -07001029 final ContentResolver cr = mContext.getContentResolver();
Leon Scrogginsc8393d92010-04-23 14:58:16 -04001030 // Let precomposed icons take precedence over non-composed
1031 // icons.
1032 if (precomposed && mTouchIconLoader != null) {
1033 mTouchIconLoader.cancel(false);
1034 mTouchIconLoader = null;
1035 }
1036 // Have only one async task at a time.
1037 if (mTouchIconLoader == null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001038 mTouchIconLoader = new DownloadTouchIcon(Tab.this,
Michael Kolb14612442011-06-24 13:06:29 -07001039 mContext, cr, view);
Leon Scrogginsc8393d92010-04-23 14:58:16 -04001040 mTouchIconLoader.execute(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001041 }
1042 }
1043
1044 @Override
1045 public void onShowCustomView(View view,
1046 WebChromeClient.CustomViewCallback callback) {
Michael Kolb14612442011-06-24 13:06:29 -07001047 Activity activity = mWebViewController.getActivity();
1048 if (activity != null) {
1049 onShowCustomView(view, activity.getRequestedOrientation(), callback);
1050 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001051 }
1052
1053 @Override
1054 public void onShowCustomView(View view, int requestedOrientation,
1055 WebChromeClient.CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001056 if (mInForeground) mWebViewController.showCustomView(Tab.this, view,
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001057 requestedOrientation, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001058 }
1059
1060 @Override
1061 public void onHideCustomView() {
Michael Kolb8233fac2010-10-26 16:08:53 -07001062 if (mInForeground) mWebViewController.hideCustomView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001063 }
1064
1065 /**
1066 * The origin has exceeded its database quota.
1067 * @param url the URL that exceeded the quota
1068 * @param databaseIdentifier the identifier of the database on which the
1069 * transaction that caused the quota overflow was run
1070 * @param currentQuota the current quota for the origin.
1071 * @param estimatedSize the estimated size of the database.
1072 * @param totalUsedQuota is the sum of all origins' quota.
1073 * @param quotaUpdater The callback to run when a decision to allow or
1074 * deny quota has been made. Don't forget to call this!
1075 */
1076 @Override
1077 public void onExceededDatabaseQuota(String url,
1078 String databaseIdentifier, long currentQuota, long estimatedSize,
1079 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -07001080 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001081 .onExceededDatabaseQuota(url, databaseIdentifier,
1082 currentQuota, estimatedSize, totalUsedQuota,
1083 quotaUpdater);
1084 }
1085
1086 /**
1087 * The Application Cache has exceeded its max size.
1088 * @param spaceNeeded is the amount of disk space that would be needed
1089 * in order for the last appcache operation to succeed.
1090 * @param totalUsedQuota is the sum of all origins' quota.
1091 * @param quotaUpdater A callback to inform the WebCore thread that a
1092 * new app cache size is available. This callback must always
1093 * be executed at some point to ensure that the sleeping
1094 * WebCore thread is woken up.
1095 */
1096 @Override
1097 public void onReachedMaxAppCacheSize(long spaceNeeded,
1098 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -07001099 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001100 .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota,
1101 quotaUpdater);
1102 }
1103
1104 /**
1105 * Instructs the browser to show a prompt to ask the user to set the
1106 * Geolocation permission state for the specified origin.
1107 * @param origin The origin for which Geolocation permissions are
1108 * requested.
1109 * @param callback The callback to call once the user has set the
1110 * Geolocation permission state.
1111 */
1112 @Override
1113 public void onGeolocationPermissionsShowPrompt(String origin,
1114 GeolocationPermissions.Callback callback) {
1115 if (mInForeground) {
Grace Kloba50c241e2010-04-20 11:07:50 -07001116 getGeolocationPermissionsPrompt().show(origin, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001117 }
1118 }
1119
1120 /**
1121 * Instructs the browser to hide the Geolocation permissions prompt.
1122 */
1123 @Override
1124 public void onGeolocationPermissionsHidePrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001125 if (mInForeground && mGeolocationPermissionsPrompt != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001126 mGeolocationPermissionsPrompt.hide();
1127 }
1128 }
1129
Ben Murdoch65acc352009-11-19 18:16:04 +00001130 /* Adds a JavaScript error message to the system log and if the JS
1131 * console is enabled in the about:debug options, to that console
1132 * also.
Ben Murdochc42addf2010-01-28 15:19:59 +00001133 * @param consoleMessage the message object.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001134 */
1135 @Override
Ben Murdochc42addf2010-01-28 15:19:59 +00001136 public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001137 if (mInForeground) {
1138 // call getErrorConsole(true) so it will create one if needed
1139 ErrorConsoleView errorConsole = getErrorConsole(true);
Ben Murdochc42addf2010-01-28 15:19:59 +00001140 errorConsole.addErrorMessage(consoleMessage);
Michael Kolb8233fac2010-10-26 16:08:53 -07001141 if (mWebViewController.shouldShowErrorConsole()
1142 && errorConsole.getShowState() !=
1143 ErrorConsoleView.SHOW_MAXIMIZED) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001144 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
1145 }
1146 }
Ben Murdochc42addf2010-01-28 15:19:59 +00001147
Jeff Hamilton47654f42010-09-07 09:57:51 -05001148 // Don't log console messages in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001149 if (isPrivateBrowsingEnabled()) return true;
Jeff Hamilton47654f42010-09-07 09:57:51 -05001150
Ben Murdochc42addf2010-01-28 15:19:59 +00001151 String message = "Console: " + consoleMessage.message() + " "
1152 + consoleMessage.sourceId() + ":"
1153 + consoleMessage.lineNumber();
1154
1155 switch (consoleMessage.messageLevel()) {
1156 case TIP:
1157 Log.v(CONSOLE_LOGTAG, message);
1158 break;
1159 case LOG:
1160 Log.i(CONSOLE_LOGTAG, message);
1161 break;
1162 case WARNING:
1163 Log.w(CONSOLE_LOGTAG, message);
1164 break;
1165 case ERROR:
1166 Log.e(CONSOLE_LOGTAG, message);
1167 break;
1168 case DEBUG:
1169 Log.d(CONSOLE_LOGTAG, message);
1170 break;
1171 }
1172
1173 return true;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001174 }
1175
1176 /**
1177 * Ask the browser for an icon to represent a <video> element.
1178 * This icon will be used if the Web page did not specify a poster attribute.
1179 * @return Bitmap The icon or null if no such icon is available.
1180 */
1181 @Override
1182 public Bitmap getDefaultVideoPoster() {
1183 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001184 return mWebViewController.getDefaultVideoPoster();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001185 }
1186 return null;
1187 }
1188
1189 /**
1190 * Ask the host application for a custom progress view to show while
1191 * a <video> is loading.
1192 * @return View The progress view.
1193 */
1194 @Override
1195 public View getVideoLoadingProgressView() {
1196 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001197 return mWebViewController.getVideoLoadingProgressView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001198 }
1199 return null;
1200 }
1201
1202 @Override
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01001203 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001204 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001205 mWebViewController.openFileChooser(uploadMsg, acceptType);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001206 } else {
1207 uploadMsg.onReceiveValue(null);
1208 }
1209 }
1210
1211 /**
1212 * Deliver a list of already-visited URLs
1213 */
1214 @Override
1215 public void getVisitedHistory(final ValueCallback<String[]> callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001216 mWebViewController.getVisitedHistory(callback);
1217 }
Ben Murdoch8029a772010-11-16 11:58:21 +00001218
1219 @Override
1220 public void setupAutoFill(Message message) {
1221 // Prompt the user to set up their profile.
1222 final Message msg = message;
Michael Kolb14612442011-06-24 13:06:29 -07001223 AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
1224 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
Ben Murdoch1d676b62011-01-17 12:54:24 +00001225 Context.LAYOUT_INFLATER_SERVICE);
1226 final View layout = inflater.inflate(R.layout.setup_autofill_dialog, null);
1227
1228 builder.setView(layout)
Ben Murdochb7e6f942011-07-08 13:00:21 +01001229 .setTitle(R.string.autofill_setup_dialog_title)
Ben Murdoch1d676b62011-01-17 12:54:24 +00001230 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1231 @Override
1232 public void onClick(DialogInterface dialog, int id) {
1233 CheckBox disableAutoFill = (CheckBox) layout.findViewById(
1234 R.id.setup_autofill_dialog_disable_autofill);
1235
1236 if (disableAutoFill.isChecked()) {
1237 // Disable autofill and show a toast with how to turn it on again.
John Reck35e9dd62011-04-25 09:01:54 -07001238 mSettings.setAutofillEnabled(false);
Michael Kolb14612442011-06-24 13:06:29 -07001239 Toast.makeText(mContext,
Ben Murdoch1d676b62011-01-17 12:54:24 +00001240 R.string.autofill_setup_dialog_negative_toast,
1241 Toast.LENGTH_LONG).show();
1242 } else {
1243 // Take user to the AutoFill profile editor. When they return,
1244 // we will send the message that we pass here which will trigger
1245 // the form to get filled out with their new profile.
1246 mWebViewController.setupAutoFill(msg);
1247 }
1248 }
1249 })
1250 .setNegativeButton(R.string.cancel, null)
1251 .show();
Ben Murdoch8029a772010-11-16 11:58:21 +00001252 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001253 };
1254
1255 // -------------------------------------------------------------------------
1256 // WebViewClient implementation for the sub window
1257 // -------------------------------------------------------------------------
1258
1259 // Subclass of WebViewClient used in subwindows to notify the main
1260 // WebViewClient of certain WebView activities.
1261 private static class SubWindowClient extends WebViewClient {
1262 // The main WebViewClient.
1263 private final WebViewClient mClient;
Michael Kolb8233fac2010-10-26 16:08:53 -07001264 private final WebViewController mController;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001265
Michael Kolb8233fac2010-10-26 16:08:53 -07001266 SubWindowClient(WebViewClient client, WebViewController controller) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001267 mClient = client;
Michael Kolb8233fac2010-10-26 16:08:53 -07001268 mController = controller;
Leon Scroggins III211ba542010-04-19 13:21:13 -04001269 }
1270 @Override
1271 public void onPageStarted(WebView view, String url, Bitmap favicon) {
1272 // Unlike the others, do not call mClient's version, which would
1273 // change the progress bar. However, we do want to remove the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001274 // find or select dialog.
Michael Kolb8233fac2010-10-26 16:08:53 -07001275 mController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001276 }
1277 @Override
1278 public void doUpdateVisitedHistory(WebView view, String url,
1279 boolean isReload) {
1280 mClient.doUpdateVisitedHistory(view, url, isReload);
1281 }
1282 @Override
1283 public boolean shouldOverrideUrlLoading(WebView view, String url) {
1284 return mClient.shouldOverrideUrlLoading(view, url);
1285 }
1286 @Override
1287 public void onReceivedSslError(WebView view, SslErrorHandler handler,
1288 SslError error) {
1289 mClient.onReceivedSslError(view, handler, error);
1290 }
1291 @Override
Brian Carlstrom8862c1d2011-06-02 01:05:55 -07001292 public void onReceivedClientCertRequest(WebView view,
1293 ClientCertRequestHandler handler, String host_and_port) {
1294 mClient.onReceivedClientCertRequest(view, handler, host_and_port);
1295 }
1296 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -07001297 public void onReceivedHttpAuthRequest(WebView view,
1298 HttpAuthHandler handler, String host, String realm) {
1299 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
1300 }
1301 @Override
1302 public void onFormResubmission(WebView view, Message dontResend,
1303 Message resend) {
1304 mClient.onFormResubmission(view, dontResend, resend);
1305 }
1306 @Override
1307 public void onReceivedError(WebView view, int errorCode,
1308 String description, String failingUrl) {
1309 mClient.onReceivedError(view, errorCode, description, failingUrl);
1310 }
1311 @Override
1312 public boolean shouldOverrideKeyEvent(WebView view,
1313 android.view.KeyEvent event) {
1314 return mClient.shouldOverrideKeyEvent(view, event);
1315 }
1316 @Override
1317 public void onUnhandledKeyEvent(WebView view,
1318 android.view.KeyEvent event) {
1319 mClient.onUnhandledKeyEvent(view, event);
1320 }
1321 }
1322
1323 // -------------------------------------------------------------------------
1324 // WebChromeClient implementation for the sub window
1325 // -------------------------------------------------------------------------
1326
1327 private class SubWindowChromeClient extends WebChromeClient {
1328 // The main WebChromeClient.
1329 private final WebChromeClient mClient;
1330
1331 SubWindowChromeClient(WebChromeClient client) {
1332 mClient = client;
1333 }
1334 @Override
1335 public void onProgressChanged(WebView view, int newProgress) {
1336 mClient.onProgressChanged(view, newProgress);
1337 }
1338 @Override
1339 public boolean onCreateWindow(WebView view, boolean dialog,
1340 boolean userGesture, android.os.Message resultMsg) {
1341 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
1342 }
1343 @Override
1344 public void onCloseWindow(WebView window) {
1345 if (window != mSubView) {
1346 Log.e(LOGTAG, "Can't close the window");
1347 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001348 mWebViewController.dismissSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001349 }
1350 }
1351
1352 // -------------------------------------------------------------------------
1353
1354 // Construct a new tab
Michael Kolb7bcafde2011-05-09 13:55:59 -07001355 Tab(WebViewController wvcontroller, WebView w) {
John Reck1cf4b792011-07-26 10:22:22 -07001356 this(wvcontroller, w, null);
1357 }
1358
1359 Tab(WebViewController wvcontroller, Bundle state) {
1360 this(wvcontroller, null, state);
1361 }
1362
1363 Tab(WebViewController wvcontroller, WebView w, Bundle state) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001364 mWebViewController = wvcontroller;
Michael Kolb14612442011-06-24 13:06:29 -07001365 mContext = mWebViewController.getContext();
John Reck35e9dd62011-04-25 09:01:54 -07001366 mSettings = BrowserSettings.getInstance();
Michael Kolb14612442011-06-24 13:06:29 -07001367 mDataController = DataController.getInstance(mContext);
1368 mCurrentState = new PageState(mContext, w != null
John Reck847b5322011-04-14 17:02:18 -07001369 ? w.isPrivateBrowsingEnabled() : false);
Michael Kolb8233fac2010-10-26 16:08:53 -07001370 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001371 mInForeground = false;
1372
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001373 mDownloadListener = new DownloadListener() {
1374 public void onDownloadStart(String url, String userAgent,
1375 String contentDisposition, String mimetype,
1376 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001377 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001378 mimetype, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001379 }
1380 };
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001381 mWebBackForwardListClient = new WebBackForwardListClient() {
1382 @Override
1383 public void onNewHistoryItem(WebHistoryItem item) {
1384 if (isInVoiceSearchMode()) {
1385 item.setCustomData(mVoiceSearchData.mVoiceSearchIntent);
1386 }
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001387 if (mClearHistoryMatchUrl != null) {
1388 if (LOGD_ENABLED) {
1389 Log.d(LOGTAG, "onNewHistoryItem:\n\t" + item.getUrl() + "\n\t"
1390 + mClearHistoryMatchUrl);
1391 }
1392 if (TextUtils.equals(item.getOriginalUrl(), mClearHistoryMatchUrl)) {
1393 if (mMainView != null) {
1394 mMainView.clearHistory();
1395 }
1396 }
1397 mClearHistoryMatchUrl = null;
1398 }
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001399 }
1400 @Override
1401 public void onIndexChanged(WebHistoryItem item, int index) {
1402 Object data = item.getCustomData();
1403 if (data != null && data instanceof Intent) {
1404 activateVoiceSearchMode((Intent) data);
1405 }
1406 }
1407 };
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001408
John Reck1cf4b792011-07-26 10:22:22 -07001409 mCaptureWidth = mContext.getResources().getDimensionPixelSize(
1410 R.dimen.tab_thumbnail_width);
1411 mCaptureHeight = mContext.getResources().getDimensionPixelSize(
1412 R.dimen.tab_thumbnail_height);
1413 updateShouldCaptureThumbnails();
1414 restoreState(state);
John Reck52be4782011-08-26 15:37:29 -07001415 if (getId() == -1) {
1416 mId = TabControl.getNextId();
1417 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001418 setWebView(w);
Michael Kolb9ef259a2011-07-12 15:33:08 -07001419 mHandler = new Handler() {
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001420 @Override
Michael Kolb9ef259a2011-07-12 15:33:08 -07001421 public void handleMessage(Message m) {
John Reck1cf4b792011-07-26 10:22:22 -07001422 switch (m.what) {
1423 case MSG_CAPTURE:
1424 capture();
1425 break;
1426 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001427 }
1428 };
John Reck1cf4b792011-07-26 10:22:22 -07001429 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001430
John Reck1cf4b792011-07-26 10:22:22 -07001431 public void updateShouldCaptureThumbnails() {
1432 if (mWebViewController.shouldCaptureThumbnails()) {
1433 synchronized (Tab.this) {
1434 if (mCapture == null) {
1435 mCapture = Bitmap.createBitmap(mCaptureWidth, mCaptureHeight,
1436 Bitmap.Config.RGB_565);
Michael Kolbc3af0672011-08-09 10:24:41 -07001437 mCapture.eraseColor(Color.WHITE);
John Reck1cf4b792011-07-26 10:22:22 -07001438 if (mInForeground) {
1439 postCapture();
1440 }
1441 }
1442 }
1443 } else {
1444 synchronized (Tab.this) {
1445 mCapture = null;
1446 deleteThumbnail();
1447 }
1448 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001449 }
1450
Michael Kolb14612442011-06-24 13:06:29 -07001451 public void setController(WebViewController ctl) {
1452 mWebViewController = ctl;
John Reck1cf4b792011-07-26 10:22:22 -07001453 updateShouldCaptureThumbnails();
Michael Kolb14612442011-06-24 13:06:29 -07001454 }
1455
Michael Kolbc831b632011-05-11 09:30:34 -07001456 public long getId() {
1457 return mId;
1458 }
1459
Grace Kloba22ac16e2009-10-07 18:00:23 -07001460 /**
1461 * Sets the WebView for this tab, correctly removing the old WebView from
1462 * the container view.
1463 */
1464 void setWebView(WebView w) {
1465 if (mMainView == w) {
1466 return;
1467 }
Michael Kolba713ec82010-11-29 17:27:06 -08001468
Grace Kloba22ac16e2009-10-07 18:00:23 -07001469 // If the WebView is changing, the page will be reloaded, so any ongoing
1470 // Geolocation permission requests are void.
Grace Kloba50c241e2010-04-20 11:07:50 -07001471 if (mGeolocationPermissionsPrompt != null) {
1472 mGeolocationPermissionsPrompt.hide();
1473 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001474
Michael Kolba713ec82010-11-29 17:27:06 -08001475 mWebViewController.onSetWebView(this, w);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001476
John Reck1cf4b792011-07-26 10:22:22 -07001477 if (mMainView != null) {
John Reck8ee633f2011-08-09 16:00:35 -07001478 mMainView.setPictureListener(null);
John Reck1cf4b792011-07-26 10:22:22 -07001479 if (w != null) {
1480 syncCurrentState(w, null);
1481 } else {
1482 mCurrentState = new PageState(mContext, false);
1483 }
1484 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001485 // set the new one
1486 mMainView = w;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001487 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001488 if (mMainView != null) {
1489 mMainView.setWebViewClient(mWebViewClient);
1490 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001491 // Attach DownloadManager so that downloads can start in an active
1492 // or a non-active window. This can happen when going to a site that
1493 // does a redirect after a period of time. The user could have
1494 // switched to another tab while waiting for the download to start.
1495 mMainView.setDownloadListener(mDownloadListener);
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001496 mMainView.setWebBackForwardListClient(mWebBackForwardListClient);
John Reck8ee633f2011-08-09 16:00:35 -07001497 TabControl tc = mWebViewController.getTabControl();
1498 if (tc != null && tc.getOnThumbnailUpdatedListener() != null) {
1499 mMainView.setPictureListener(this);
1500 }
John Reck1cf4b792011-07-26 10:22:22 -07001501 if (mSavedState != null) {
John Reck6c2e2f32011-08-22 13:41:23 -07001502 WebBackForwardList restoredState
1503 = mMainView.restoreState(mSavedState);
1504 if (restoredState == null || restoredState.getSize() == 0) {
1505 Log.w(LOGTAG, "Failed to restore WebView state!");
1506 loadUrl(mCurrentState.mOriginalUrl, null);
1507 }
John Reck1cf4b792011-07-26 10:22:22 -07001508 mSavedState = null;
1509 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001510 }
1511 }
1512
1513 /**
1514 * Destroy the tab's main WebView and subWindow if any
1515 */
1516 void destroy() {
1517 if (mMainView != null) {
1518 dismissSubWindow();
John Reckef654f12011-07-12 16:42:08 -07001519 // Make sure the embedded title bar isn't still attached
1520 mMainView.setEmbeddedTitleBar(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001521 // save the WebView to call destroy() after detach it from the tab
1522 WebView webView = mMainView;
1523 setWebView(null);
1524 webView.destroy();
1525 }
1526 }
1527
1528 /**
1529 * Remove the tab from the parent
1530 */
1531 void removeFromTree() {
1532 // detach the children
Michael Kolbc831b632011-05-11 09:30:34 -07001533 if (mChildren != null) {
1534 for(Tab t : mChildren) {
1535 t.setParent(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001536 }
1537 }
1538 // remove itself from the parent list
Michael Kolbc831b632011-05-11 09:30:34 -07001539 if (mParent != null) {
1540 mParent.mChildren.remove(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001541 }
John Reck1cf4b792011-07-26 10:22:22 -07001542 deleteThumbnail();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001543 }
1544
1545 /**
1546 * Create a new subwindow unless a subwindow already exists.
1547 * @return True if a new subwindow was created. False if one already exists.
1548 */
1549 boolean createSubWindow() {
1550 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001551 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001552 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001553 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001554 mSubView.setWebChromeClient(new SubWindowChromeClient(
1555 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001556 // Set a different DownloadListener for the mSubView, since it will
1557 // just need to dismiss the mSubView, rather than close the Tab
1558 mSubView.setDownloadListener(new DownloadListener() {
1559 public void onDownloadStart(String url, String userAgent,
1560 String contentDisposition, String mimetype,
1561 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001562 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001563 contentDisposition, mimetype, contentLength);
1564 if (mSubView.copyBackForwardList().getSize() == 0) {
1565 // This subwindow was opened for the sole purpose of
1566 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001567 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001568 }
1569 }
1570 });
Michael Kolb14612442011-06-24 13:06:29 -07001571 mSubView.setOnCreateContextMenuListener(mWebViewController.getActivity());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001572 return true;
1573 }
1574 return false;
1575 }
1576
1577 /**
1578 * Dismiss the subWindow for the tab.
1579 */
1580 void dismissSubWindow() {
1581 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001582 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001583 mSubView.destroy();
1584 mSubView = null;
1585 mSubViewContainer = null;
1586 }
1587 }
1588
Grace Kloba22ac16e2009-10-07 18:00:23 -07001589
1590 /**
1591 * Set the parent tab of this tab.
1592 */
Michael Kolbc831b632011-05-11 09:30:34 -07001593 void setParent(Tab parent) {
John Reck52be4782011-08-26 15:37:29 -07001594 if (parent == this) {
1595 throw new IllegalStateException("Cannot set parent to self!");
1596 }
Michael Kolbc831b632011-05-11 09:30:34 -07001597 mParent = parent;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001598 // This tab may have been freed due to low memory. If that is the case,
Michael Kolbc831b632011-05-11 09:30:34 -07001599 // the parent tab id is already saved. If we are changing that id
Grace Kloba22ac16e2009-10-07 18:00:23 -07001600 // (most likely due to removing the parent tab) we must update the
Michael Kolbc831b632011-05-11 09:30:34 -07001601 // parent tab id in the saved Bundle.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001602 if (mSavedState != null) {
1603 if (parent == null) {
1604 mSavedState.remove(PARENTTAB);
1605 } else {
Michael Kolbc831b632011-05-11 09:30:34 -07001606 mSavedState.putLong(PARENTTAB, parent.getId());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001607 }
1608 }
John Reckb0a86db2011-05-24 14:05:58 -07001609
1610 // Sync the WebView useragent with the parent
1611 if (parent != null && mSettings.hasDesktopUseragent(parent.getWebView())
1612 != mSettings.hasDesktopUseragent(getWebView())) {
1613 mSettings.toggleDesktopUseragent(getWebView());
1614 }
John Reck52be4782011-08-26 15:37:29 -07001615
1616 if (parent != null && parent.getId() == getId()) {
1617 throw new IllegalStateException("Parent has same ID as child!");
1618 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001619 }
1620
1621 /**
Michael Kolbc831b632011-05-11 09:30:34 -07001622 * If this Tab was created through another Tab, then this method returns
1623 * that Tab.
1624 * @return the Tab parent or null
1625 */
1626 public Tab getParent() {
1627 return mParent;
1628 }
1629
1630 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001631 * When a Tab is created through the content of another Tab, then we
1632 * associate the Tabs.
1633 * @param child the Tab that was created from this Tab
1634 */
1635 void addChildTab(Tab child) {
Michael Kolbc831b632011-05-11 09:30:34 -07001636 if (mChildren == null) {
1637 mChildren = new Vector<Tab>();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001638 }
Michael Kolbc831b632011-05-11 09:30:34 -07001639 mChildren.add(child);
1640 child.setParent(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001641 }
1642
Michael Kolbc831b632011-05-11 09:30:34 -07001643 Vector<Tab> getChildren() {
1644 return mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001645 }
1646
1647 void resume() {
1648 if (mMainView != null) {
John Reck56c1fcf2011-08-17 10:15:16 -07001649 setupHwAcceleration(mMainView);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001650 mMainView.onResume();
1651 if (mSubView != null) {
1652 mSubView.onResume();
1653 }
1654 }
1655 }
1656
John Reck56c1fcf2011-08-17 10:15:16 -07001657 private void setupHwAcceleration(View web) {
1658 if (web == null) return;
1659 BrowserSettings settings = BrowserSettings.getInstance();
1660 if (settings.isHardwareAccelerated()) {
1661 web.setLayerType(View.LAYER_TYPE_NONE, null);
1662 } else {
1663 web.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
1664 }
1665 }
1666
Grace Kloba22ac16e2009-10-07 18:00:23 -07001667 void pause() {
1668 if (mMainView != null) {
1669 mMainView.onPause();
1670 if (mSubView != null) {
1671 mSubView.onPause();
1672 }
1673 }
1674 }
1675
1676 void putInForeground() {
John Reck8ee633f2011-08-09 16:00:35 -07001677 if (mInForeground) {
1678 return;
1679 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001680 mInForeground = true;
1681 resume();
Michael Kolb14612442011-06-24 13:06:29 -07001682 Activity activity = mWebViewController.getActivity();
1683 mMainView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001684 if (mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -07001685 mSubView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001686 }
1687 // Show the pending error dialog if the queue is not empty
1688 if (mQueuedErrors != null && mQueuedErrors.size() > 0) {
1689 showError(mQueuedErrors.getFirst());
1690 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001691 mWebViewController.bookmarkedStatusHasChanged(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001692 }
1693
1694 void putInBackground() {
John Reck8ee633f2011-08-09 16:00:35 -07001695 if (!mInForeground) {
1696 return;
1697 }
John Reck52be4782011-08-26 15:37:29 -07001698 capture();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001699 mInForeground = false;
1700 pause();
1701 mMainView.setOnCreateContextMenuListener(null);
1702 if (mSubView != null) {
1703 mSubView.setOnCreateContextMenuListener(null);
1704 }
1705 }
1706
Michael Kolb8233fac2010-10-26 16:08:53 -07001707 boolean inForeground() {
1708 return mInForeground;
1709 }
1710
Grace Kloba22ac16e2009-10-07 18:00:23 -07001711 /**
1712 * Return the top window of this tab; either the subwindow if it is not
1713 * null or the main window.
1714 * @return The top window of this tab.
1715 */
1716 WebView getTopWindow() {
1717 if (mSubView != null) {
1718 return mSubView;
1719 }
1720 return mMainView;
1721 }
1722
1723 /**
1724 * Return the main window of this tab. Note: if a tab is freed in the
1725 * background, this can return null. It is only guaranteed to be
1726 * non-null for the current tab.
1727 * @return The main WebView of this tab.
1728 */
1729 WebView getWebView() {
1730 return mMainView;
1731 }
1732
Michael Kolba713ec82010-11-29 17:27:06 -08001733 void setViewContainer(View container) {
1734 mContainer = container;
1735 }
1736
Michael Kolb8233fac2010-10-26 16:08:53 -07001737 View getViewContainer() {
1738 return mContainer;
1739 }
1740
Grace Kloba22ac16e2009-10-07 18:00:23 -07001741 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001742 * Return whether private browsing is enabled for the main window of
1743 * this tab.
1744 * @return True if private browsing is enabled.
1745 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001746 boolean isPrivateBrowsingEnabled() {
John Reck502a3532011-08-16 14:21:46 -07001747 return mCurrentState.mIncognito;
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001748 }
1749
1750 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001751 * Return the subwindow of this tab or null if there is no subwindow.
1752 * @return The subwindow of this tab or null.
1753 */
1754 WebView getSubWebView() {
1755 return mSubView;
1756 }
1757
Michael Kolb1514bb72010-11-22 09:11:48 -08001758 void setSubWebView(WebView subView) {
1759 mSubView = subView;
1760 }
1761
Michael Kolb8233fac2010-10-26 16:08:53 -07001762 View getSubViewContainer() {
1763 return mSubViewContainer;
1764 }
1765
Michael Kolb1514bb72010-11-22 09:11:48 -08001766 void setSubViewContainer(View subViewContainer) {
1767 mSubViewContainer = subViewContainer;
1768 }
1769
Grace Kloba22ac16e2009-10-07 18:00:23 -07001770 /**
1771 * @return The geolocation permissions prompt for this tab.
1772 */
1773 GeolocationPermissionsPrompt getGeolocationPermissionsPrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001774 if (mGeolocationPermissionsPrompt == null) {
1775 ViewStub stub = (ViewStub) mContainer
1776 .findViewById(R.id.geolocation_permissions_prompt);
1777 mGeolocationPermissionsPrompt = (GeolocationPermissionsPrompt) stub
1778 .inflate();
Grace Kloba50c241e2010-04-20 11:07:50 -07001779 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001780 return mGeolocationPermissionsPrompt;
1781 }
1782
1783 /**
1784 * @return The application id string
1785 */
1786 String getAppId() {
1787 return mAppId;
1788 }
1789
1790 /**
1791 * Set the application id string
1792 * @param id
1793 */
1794 void setAppId(String id) {
1795 mAppId = id;
1796 }
1797
Michael Kolbe28b3472011-08-04 16:54:31 -07001798 boolean closeOnBack() {
1799 return mCloseOnBack;
1800 }
1801
1802 void setCloseOnBack(boolean close) {
1803 mCloseOnBack = close;
1804 }
1805
Grace Kloba22ac16e2009-10-07 18:00:23 -07001806 String getUrl() {
John Reck324d4402011-01-11 16:56:42 -08001807 return UrlUtils.filteredUrl(mCurrentState.mUrl);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001808 }
1809
John Reck49a603c2011-03-03 09:33:05 -08001810 String getOriginalUrl() {
John Reckdb22ec42011-06-29 11:31:24 -07001811 if (mCurrentState.mOriginalUrl == null) {
1812 return getUrl();
John Reck49a603c2011-03-03 09:33:05 -08001813 }
John Reckdb22ec42011-06-29 11:31:24 -07001814 return UrlUtils.filteredUrl(mCurrentState.mOriginalUrl);
John Reck49a603c2011-03-03 09:33:05 -08001815 }
1816
Grace Kloba22ac16e2009-10-07 18:00:23 -07001817 /**
John Reck30c714c2010-12-16 17:30:34 -08001818 * Get the title of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001819 */
1820 String getTitle() {
John Reck30c714c2010-12-16 17:30:34 -08001821 if (mCurrentState.mTitle == null && mInPageLoad) {
Michael Kolb14612442011-06-24 13:06:29 -07001822 return mContext.getString(R.string.title_bar_loading);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001823 }
John Reck30c714c2010-12-16 17:30:34 -08001824 return mCurrentState.mTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001825 }
1826
1827 /**
John Reck30c714c2010-12-16 17:30:34 -08001828 * Get the favicon of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001829 */
1830 Bitmap getFavicon() {
John Reck1cf4b792011-07-26 10:22:22 -07001831 if (mCurrentState.mFavicon != null) {
1832 return mCurrentState.mFavicon;
1833 }
1834 return getDefaultFavicon(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001835 }
1836
John Recke969cc52010-12-21 17:24:43 -08001837 public boolean isBookmarkedSite() {
1838 return mCurrentState.mIsBookmarkedSite;
1839 }
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001840
Grace Kloba22ac16e2009-10-07 18:00:23 -07001841 /**
1842 * Return the tab's error console. Creates the console if createIfNEcessary
1843 * is true and we haven't already created the console.
1844 * @param createIfNecessary Flag to indicate if the console should be
1845 * created if it has not been already.
1846 * @return The tab's error console, or null if one has not been created and
1847 * createIfNecessary is false.
1848 */
1849 ErrorConsoleView getErrorConsole(boolean createIfNecessary) {
1850 if (createIfNecessary && mErrorConsole == null) {
Michael Kolb14612442011-06-24 13:06:29 -07001851 mErrorConsole = new ErrorConsoleView(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001852 mErrorConsole.setWebView(mMainView);
1853 }
1854 return mErrorConsole;
1855 }
1856
John Reck30c714c2010-12-16 17:30:34 -08001857 private void setLockIconType(LockIcon icon) {
1858 mCurrentState.mLockIcon = icon;
1859 mWebViewController.onUpdatedLockIcon(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001860 }
1861
1862 /**
1863 * @return The tab's lock icon type.
1864 */
John Reck30c714c2010-12-16 17:30:34 -08001865 LockIcon getLockIconType() {
1866 return mCurrentState.mLockIcon;
1867 }
1868
1869 int getLoadProgress() {
1870 if (mInPageLoad) {
1871 return mPageLoadProgress;
1872 }
1873 return 100;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001874 }
1875
1876 /**
1877 * @return TRUE if onPageStarted is called while onPageFinished is not
1878 * called yet.
1879 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001880 boolean inPageLoad() {
1881 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001882 }
1883
1884 // force mInLoad to be false. This should only be called before closing the
1885 // tab to ensure BrowserActivity's pauseWebViewTimers() is called correctly.
Michael Kolb8233fac2010-10-26 16:08:53 -07001886 void clearInPageLoad() {
1887 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001888 }
1889
Grace Kloba22ac16e2009-10-07 18:00:23 -07001890 /**
John Reck1cf4b792011-07-26 10:22:22 -07001891 * @return The Bundle with the tab's state if it can be saved, otherwise null
Grace Kloba22ac16e2009-10-07 18:00:23 -07001892 */
John Reck1cf4b792011-07-26 10:22:22 -07001893 public Bundle saveState() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001894 // If the WebView is null it means we ran low on memory and we already
1895 // stored the saved state in mSavedState.
1896 if (mMainView == null) {
John Reck1cf4b792011-07-26 10:22:22 -07001897 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001898 }
John Reck6c2e2f32011-08-22 13:41:23 -07001899
1900 if (TextUtils.isEmpty(mCurrentState.mUrl)) {
John Reck1cf4b792011-07-26 10:22:22 -07001901 return null;
John Reck24f18262011-06-17 14:47:20 -07001902 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001903
1904 mSavedState = new Bundle();
John Reck6c2e2f32011-08-22 13:41:23 -07001905 WebBackForwardList savedList = mMainView.saveState(mSavedState);
1906 if (savedList == null || savedList.getSize() == 0) {
1907 Log.w(LOGTAG, "Failed to save back/forward list for "
1908 + mCurrentState.mUrl);
1909 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001910
Michael Kolbc831b632011-05-11 09:30:34 -07001911 mSavedState.putLong(ID, mId);
John Reck30c714c2010-12-16 17:30:34 -08001912 mSavedState.putString(CURRURL, mCurrentState.mUrl);
1913 mSavedState.putString(CURRTITLE, mCurrentState.mTitle);
John Reck1cf4b792011-07-26 10:22:22 -07001914 mSavedState.putBoolean(INCOGNITO, mMainView.isPrivateBrowsingEnabled());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001915 if (mAppId != null) {
1916 mSavedState.putString(APPID, mAppId);
1917 }
Michael Kolbe28b3472011-08-04 16:54:31 -07001918 mSavedState.putBoolean(CLOSEFLAG, mCloseOnBack);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001919 // Remember the parent tab so the relationship can be restored.
Michael Kolbc831b632011-05-11 09:30:34 -07001920 if (mParent != null) {
1921 mSavedState.putLong(PARENTTAB, mParent.mId);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001922 }
John Reckb0a86db2011-05-24 14:05:58 -07001923 mSavedState.putBoolean(USERAGENT,
1924 mSettings.hasDesktopUseragent(getWebView()));
John Reck1cf4b792011-07-26 10:22:22 -07001925 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001926 }
1927
1928 /*
1929 * Restore the state of the tab.
1930 */
John Reck1cf4b792011-07-26 10:22:22 -07001931 private void restoreState(Bundle b) {
1932 mSavedState = b;
1933 if (mSavedState == null) {
1934 return;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001935 }
1936 // Restore the internal state even if the WebView fails to restore.
1937 // This will maintain the app id, original url and close-on-exit values.
Michael Kolbc831b632011-05-11 09:30:34 -07001938 mId = b.getLong(ID);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001939 mAppId = b.getString(APPID);
Michael Kolbe28b3472011-08-04 16:54:31 -07001940 mCloseOnBack = b.getBoolean(CLOSEFLAG);
John Reckb0a86db2011-05-24 14:05:58 -07001941 if (b.getBoolean(USERAGENT)
1942 != mSettings.hasDesktopUseragent(getWebView())) {
1943 mSettings.toggleDesktopUseragent(getWebView());
1944 }
John Reck1cf4b792011-07-26 10:22:22 -07001945 String url = b.getString(CURRURL);
1946 String title = b.getString(CURRTITLE);
1947 boolean incognito = b.getBoolean(INCOGNITO);
1948 mCurrentState = new PageState(mContext, incognito, url, null);
1949 mCurrentState.mTitle = title;
1950 synchronized (Tab.this) {
1951 if (mCapture != null) {
1952 BackgroundHandler.execute(mLoadThumbnail);
1953 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001954 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001955 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04001956
Leon Scroggins1961ed22010-12-07 15:22:21 -05001957 public void updateBookmarkedStatus() {
John Recke969cc52010-12-21 17:24:43 -08001958 mDataController.queryBookmarkStatus(getUrl(), mIsBookmarkCallback);
Leon Scroggins1961ed22010-12-07 15:22:21 -05001959 }
1960
John Recke969cc52010-12-21 17:24:43 -08001961 private DataController.OnQueryUrlIsBookmark mIsBookmarkCallback
1962 = new DataController.OnQueryUrlIsBookmark() {
1963 @Override
1964 public void onQueryUrlIsBookmark(String url, boolean isBookmark) {
1965 if (mCurrentState.mUrl.equals(url)) {
1966 mCurrentState.mIsBookmarkedSite = isBookmark;
1967 mWebViewController.bookmarkedStatusHasChanged(Tab.this);
1968 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001969 }
John Recke969cc52010-12-21 17:24:43 -08001970 };
Michael Kolb1acef692011-03-08 14:12:06 -08001971
Michael Kolbeb95db42011-03-03 10:38:40 -08001972 public Bitmap getScreenshot() {
John Reck1cf4b792011-07-26 10:22:22 -07001973 synchronized (Tab.this) {
1974 return mCapture;
1975 }
Michael Kolbeb95db42011-03-03 10:38:40 -08001976 }
1977
John Reck541f55a2011-06-07 16:34:43 -07001978 public boolean isSnapshot() {
John Reck541f55a2011-06-07 16:34:43 -07001979 return false;
1980 }
1981
John Reckd8c74522011-06-14 08:45:00 -07001982 public ContentValues createSnapshotValues() {
1983 if (mMainView == null) return null;
John Reck8cc92352011-07-06 17:41:52 -07001984 ByteArrayOutputStream bos = new ByteArrayOutputStream();
1985 try {
1986 GZIPOutputStream stream = new GZIPOutputStream(bos);
1987 if (!mMainView.saveViewState(stream)) {
1988 return null;
1989 }
1990 stream.flush();
1991 stream.close();
1992 } catch (Exception e) {
1993 Log.w(LOGTAG, "Failed to save view state", e);
John Reck541f55a2011-06-07 16:34:43 -07001994 return null;
1995 }
John Reck8cc92352011-07-06 17:41:52 -07001996 byte[] data = bos.toByteArray();
John Reckd8c74522011-06-14 08:45:00 -07001997 ContentValues values = new ContentValues();
1998 values.put(Snapshots.TITLE, mCurrentState.mTitle);
1999 values.put(Snapshots.URL, mCurrentState.mUrl);
2000 values.put(Snapshots.VIEWSTATE, data);
2001 values.put(Snapshots.BACKGROUND, mMainView.getPageBackgroundColor());
John Reck8cc92352011-07-06 17:41:52 -07002002 values.put(Snapshots.DATE_CREATED, System.currentTimeMillis());
2003 values.put(Snapshots.FAVICON, compressBitmap(getFavicon()));
2004 Bitmap screenshot = Controller.createScreenshot(mMainView,
2005 Controller.getDesiredThumbnailWidth(mContext),
2006 Controller.getDesiredThumbnailHeight(mContext));
2007 values.put(Snapshots.THUMBNAIL, compressBitmap(screenshot));
John Reckd8c74522011-06-14 08:45:00 -07002008 return values;
John Reck541f55a2011-06-07 16:34:43 -07002009 }
2010
John Reck8cc92352011-07-06 17:41:52 -07002011 public byte[] compressBitmap(Bitmap bitmap) {
2012 if (bitmap == null) {
2013 return null;
2014 }
2015 ByteArrayOutputStream stream = new ByteArrayOutputStream();
2016 bitmap.compress(CompressFormat.PNG, 100, stream);
2017 return stream.toByteArray();
2018 }
2019
John Reck26b18322011-06-21 13:08:58 -07002020 public void loadUrl(String url, Map<String, String> headers) {
2021 if (mMainView != null) {
Michael Kolb14612442011-06-24 13:06:29 -07002022 mCurrentState = new PageState(mContext, false, url, null);
John Reck26b18322011-06-21 13:08:58 -07002023 mWebViewController.onPageStarted(this, mMainView, null);
2024 mMainView.loadUrl(url, headers);
2025 }
2026 }
2027
Michael Kolb9ef259a2011-07-12 15:33:08 -07002028 protected void capture() {
2029 if (mMainView == null || mCapture == null) return;
2030 Canvas c = new Canvas(mCapture);
2031 final int left = mMainView.getScrollX();
2032 final int top = mMainView.getScrollY() + mMainView.getVisibleTitleHeight();
2033 c.translate(-left, -top);
2034 float scale = mCaptureWidth / (float) mMainView.getWidth();
2035 c.scale(scale, scale, left, top);
John Reck8ee633f2011-08-09 16:00:35 -07002036 if (mMainView instanceof BrowserWebView) {
2037 ((BrowserWebView)mMainView).drawContent(c);
2038 } else {
2039 mMainView.draw(c);
2040 }
Dianne Hackborn43cfe8a2011-08-02 16:59:35 -07002041 c.setBitmap(null);
John Reck8ee633f2011-08-09 16:00:35 -07002042 mHandler.removeMessages(MSG_CAPTURE);
John Reck1cf4b792011-07-26 10:22:22 -07002043 persistThumbnail();
John Reck8ee633f2011-08-09 16:00:35 -07002044 TabControl tc = mWebViewController.getTabControl();
2045 if (tc != null) {
2046 OnThumbnailUpdatedListener updateListener
2047 = tc.getOnThumbnailUpdatedListener();
2048 if (updateListener != null) {
2049 updateListener.onThumbnailUpdated(this);
2050 }
2051 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07002052 }
2053
2054 @Override
2055 public void onNewPicture(WebView view, Picture picture) {
2056 //update screenshot
John Reck1cf4b792011-07-26 10:22:22 -07002057 postCapture();
2058 }
2059
2060 private void postCapture() {
Michael Kolb9ef259a2011-07-12 15:33:08 -07002061 if (!mHandler.hasMessages(MSG_CAPTURE)) {
2062 mHandler.sendEmptyMessageDelayed(MSG_CAPTURE, CAPTURE_DELAY);
2063 }
2064 }
2065
John Reckef654f12011-07-12 16:42:08 -07002066 public boolean canGoBack() {
2067 return mMainView != null ? mMainView.canGoBack() : false;
2068 }
2069
2070 public boolean canGoForward() {
2071 return mMainView != null ? mMainView.canGoForward() : false;
2072 }
2073
2074 public void goBack() {
2075 if (mMainView != null) {
2076 mMainView.goBack();
2077 }
2078 }
2079
2080 public void goForward() {
2081 if (mMainView != null) {
2082 mMainView.goForward();
2083 }
2084 }
2085
Mathew Inwood1dd8e822011-08-03 14:34:29 +01002086 /**
2087 * Causes the tab back/forward stack to be cleared once, if the given URL is the next URL
2088 * to be added to the stack.
2089 *
2090 * This is used to ensure that preloaded URLs that are not subsequently seen by the user do
2091 * not appear in the back stack.
2092 */
2093 public void clearBackStackWhenItemAdded(String urlToMatch) {
2094 mClearHistoryMatchUrl = urlToMatch;
2095 }
2096
John Reck1cf4b792011-07-26 10:22:22 -07002097 protected void persistThumbnail() {
2098 BackgroundHandler.execute(mSaveThumbnail);
2099 }
2100
2101 protected void deleteThumbnail() {
2102 BackgroundHandler.execute(mDeleteThumbnail);
2103 }
2104
2105 private void updateCaptureFromBlob(byte[] blob) {
2106 synchronized (Tab.this) {
2107 if (mCapture == null) {
2108 return;
2109 }
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002110 ByteBuffer buffer = ByteBuffer.wrap(blob);
Michael Kolbd837a112011-08-09 14:00:18 -07002111 try {
2112 mCapture.copyPixelsFromBuffer(buffer);
2113 } catch (RuntimeException rex) {
2114 Log.e(LOGTAG, "Load capture has mismatched sizes; buffer: "
2115 + buffer.capacity() + " blob: " + blob.length
2116 + "capture: " + mCapture.getByteCount());
2117 throw rex;
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002118 }
John Reck1cf4b792011-07-26 10:22:22 -07002119 }
2120 }
2121
2122 private byte[] getCaptureBlob() {
2123 synchronized (Tab.this) {
2124 if (mCapture == null) {
2125 return null;
2126 }
2127 ByteBuffer buffer = ByteBuffer.allocate(mCapture.getByteCount());
2128 mCapture.copyPixelsToBuffer(buffer);
2129 return buffer.array();
2130 }
2131 }
2132
2133 private Runnable mSaveThumbnail = new Runnable() {
2134
2135 @Override
2136 public void run() {
2137 byte[] blob = getCaptureBlob();
2138 if (blob == null) {
2139 return;
2140 }
2141 ContentResolver cr = mContext.getContentResolver();
2142 ContentValues values = new ContentValues();
2143 values.put(Thumbnails._ID, mId);
2144 values.put(Thumbnails.THUMBNAIL, blob);
2145 cr.insert(Thumbnails.CONTENT_URI, values);
2146 }
2147 };
2148
2149 private Runnable mDeleteThumbnail = new Runnable() {
2150
2151 @Override
2152 public void run() {
2153 ContentResolver cr = mContext.getContentResolver();
2154 try {
2155 cr.delete(ContentUris.withAppendedId(Thumbnails.CONTENT_URI, mId),
2156 null, null);
2157 } catch (Throwable t) {}
2158 }
2159 };
2160
2161 private Runnable mLoadThumbnail = new Runnable() {
2162
2163 @Override
2164 public void run() {
2165 ContentResolver cr = mContext.getContentResolver();
2166 Cursor c = null;
2167 try {
2168 Uri uri = ContentUris.withAppendedId(Thumbnails.CONTENT_URI, mId);
2169 c = cr.query(uri, new String[] {Thumbnails._ID,
2170 Thumbnails.THUMBNAIL}, null, null, null);
2171 if (c.moveToFirst()) {
2172 byte[] data = c.getBlob(1);
2173 if (data != null && data.length > 0) {
2174 updateCaptureFromBlob(data);
2175 }
2176 }
2177 } finally {
2178 if (c != null) {
2179 c.close();
2180 }
2181 }
2182 }
2183 };
2184
John Reck52be4782011-08-26 15:37:29 -07002185 @Override
2186 public String toString() {
2187 StringBuilder builder = new StringBuilder(100);
2188 builder.append(mId);
2189 builder.append(") has parent: ");
2190 if (getParent() != null) {
2191 builder.append("true[");
2192 builder.append(getParent().getId());
2193 builder.append("]");
2194 } else {
2195 builder.append("false");
2196 }
2197 builder.append(", incog: ");
2198 builder.append(isPrivateBrowsingEnabled());
2199 if (!isPrivateBrowsingEnabled()) {
2200 builder.append(", title: ");
2201 builder.append(getTitle());
2202 builder.append(", url: ");
2203 builder.append(getUrl());
2204 }
2205 return builder.toString();
2206 }
2207
Grace Kloba22ac16e2009-10-07 18:00:23 -07002208}