blob: 3d90ab6c0dac9668040646290b5d1c173591b55e [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 Kolba3194d02011-09-07 11:23:51 -070035import android.graphics.Paint;
Michael Kolb9ef259a2011-07-12 15:33:08 -070036import android.graphics.Picture;
Michael Kolba3194d02011-09-07 11:23:51 -070037import android.graphics.PorterDuff;
38import android.graphics.PorterDuffXfermode;
Grace Kloba22ac16e2009-10-07 18:00:23 -070039import android.net.Uri;
40import android.net.http.SslError;
Grace Kloba22ac16e2009-10-07 18:00:23 -070041import android.os.Bundle;
Michael Kolb9ef259a2011-07-12 15:33:08 -070042import android.os.Handler;
Grace Kloba22ac16e2009-10-07 18:00:23 -070043import android.os.Message;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +000044import android.os.SystemClock;
Brian Carlstrom8862c1d2011-06-02 01:05:55 -070045import android.security.KeyChain;
Brian Carlstromaa09cd82011-06-09 16:04:40 -070046import android.security.KeyChainAliasCallback;
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -050047import android.speech.RecognizerResultsIntent;
John Reck24f18262011-06-17 14:47:20 -070048import android.text.TextUtils;
Grace Kloba22ac16e2009-10-07 18:00:23 -070049import android.util.Log;
50import android.view.KeyEvent;
51import android.view.LayoutInflater;
52import android.view.View;
Grace Kloba50c241e2010-04-20 11:07:50 -070053import android.view.ViewStub;
Brian Carlstrom8862c1d2011-06-02 01:05:55 -070054import android.webkit.ClientCertRequestHandler;
Ben Murdochc42addf2010-01-28 15:19:59 +000055import android.webkit.ConsoleMessage;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -050056import android.webkit.DownloadListener;
Grace Kloba22ac16e2009-10-07 18:00:23 -070057import android.webkit.GeolocationPermissions;
58import android.webkit.HttpAuthHandler;
59import android.webkit.SslErrorHandler;
60import android.webkit.URLUtil;
61import android.webkit.ValueCallback;
John Reck6c2e2f32011-08-22 13:41:23 -070062import android.webkit.WebBackForwardList;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -050063import android.webkit.WebBackForwardListClient;
Grace Kloba22ac16e2009-10-07 18:00:23 -070064import android.webkit.WebChromeClient;
65import android.webkit.WebHistoryItem;
John Reck438bf462011-01-12 18:11:46 -080066import android.webkit.WebResourceResponse;
Grace Kloba22ac16e2009-10-07 18:00:23 -070067import android.webkit.WebStorage;
68import android.webkit.WebView;
Michael Kolb9ef259a2011-07-12 15:33:08 -070069import android.webkit.WebView.PictureListener;
Grace Kloba22ac16e2009-10-07 18:00:23 -070070import android.webkit.WebViewClient;
Ben Murdoch1d676b62011-01-17 12:54:24 +000071import android.widget.CheckBox;
Ben Murdoch8029a772010-11-16 11:58:21 +000072import android.widget.Toast;
Grace Kloba22ac16e2009-10-07 18:00:23 -070073
John Reck8ee633f2011-08-09 16:00:35 -070074import com.android.browser.TabControl.OnThumbnailUpdatedListener;
John Reck541f55a2011-06-07 16:34:43 -070075import com.android.browser.homepages.HomeProvider;
John Reck1cf4b792011-07-26 10:22:22 -070076import com.android.browser.provider.BrowserProvider2.Thumbnails;
John Reck8cc92352011-07-06 17:41:52 -070077import com.android.browser.provider.SnapshotProvider.Snapshots;
John Reck541f55a2011-06-07 16:34:43 -070078import com.android.common.speech.LoggingEvents;
79
80import java.io.ByteArrayOutputStream;
John Reck1cf4b792011-07-26 10:22:22 -070081import java.nio.ByteBuffer;
Michael Kolbfe251992010-07-08 15:41:55 -070082import java.util.ArrayList;
83import java.util.HashMap;
84import java.util.Iterator;
85import java.util.LinkedList;
86import java.util.Map;
87import java.util.Vector;
Mathew Inwooda829d552011-09-02 14:16:25 +010088import java.util.regex.Pattern;
John Reck8cc92352011-07-06 17:41:52 -070089import java.util.zip.GZIPOutputStream;
Michael Kolbfe251992010-07-08 15:41:55 -070090
Grace Kloba22ac16e2009-10-07 18:00:23 -070091/**
92 * Class for maintaining Tabs with a main WebView and a subwindow.
93 */
Michael Kolb9ef259a2011-07-12 15:33:08 -070094class Tab implements PictureListener {
Michael Kolb8233fac2010-10-26 16:08:53 -070095
Grace Kloba22ac16e2009-10-07 18:00:23 -070096 // Log Tag
97 private static final String LOGTAG = "Tab";
Mathew Inwood1dd8e822011-08-03 14:34:29 +010098 private static final boolean LOGD_ENABLED = com.android.browser.Browser.LOGD_ENABLED;
Ben Murdochc42addf2010-01-28 15:19:59 +000099 // Special case the logtag for messages for the Console to make it easier to
100 // filter them and match the logtag used for these messages in older versions
101 // of the browser.
102 private static final String CONSOLE_LOGTAG = "browser";
103
Michael Kolb9ef259a2011-07-12 15:33:08 -0700104 private static final int MSG_CAPTURE = 42;
John Reck8ee633f2011-08-09 16:00:35 -0700105 private static final int CAPTURE_DELAY = 100;
Michael Kolba53c9892011-10-05 13:31:40 -0700106 private static final int INITIAL_PROGRESS = 5;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700107
John Reck1cf4b792011-07-26 10:22:22 -0700108 private static Bitmap sDefaultFavicon;
109
Michael Kolba3194d02011-09-07 11:23:51 -0700110 private static Paint sAlphaPaint = new Paint();
111 static {
112 sAlphaPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
113 sAlphaPaint.setColor(Color.TRANSPARENT);
114 }
115
Steve Block2466eff2011-10-03 15:33:09 +0100116 public enum SecurityState {
Steve Block4895b012011-10-03 16:26:46 +0100117 // The page's main resource does not use SSL. Note that we use this
118 // state irrespective of the SSL authentication state of sub-resources.
Steve Block2466eff2011-10-03 15:33:09 +0100119 SECURITY_STATE_NOT_SECURE,
Steve Block4895b012011-10-03 16:26:46 +0100120 // The page's main resource uses SSL and the certificate is good. The
121 // same is true of all sub-resources.
Steve Block2466eff2011-10-03 15:33:09 +0100122 SECURITY_STATE_SECURE,
Steve Block4895b012011-10-03 16:26:46 +0100123 // The page's main resource uses SSL and the certificate is good, but
124 // some sub-resources either do not use SSL or have problems with their
125 // certificates.
Steve Block2466eff2011-10-03 15:33:09 +0100126 SECURITY_STATE_MIXED,
Steve Block4895b012011-10-03 16:26:46 +0100127 // The page's main resource uses SSL but there is a problem with its
128 // certificate.
129 SECURITY_STATE_BAD_CERTIFICATE,
John Reck30c714c2010-12-16 17:30:34 -0800130 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700131
Michael Kolb14612442011-06-24 13:06:29 -0700132 Context mContext;
John Reckd8c74522011-06-14 08:45:00 -0700133 protected WebViewController mWebViewController;
Michael Kolb8233fac2010-10-26 16:08:53 -0700134
Michael Kolbc831b632011-05-11 09:30:34 -0700135 // The tab ID
John Reckd8c74522011-06-14 08:45:00 -0700136 private long mId = -1;
Michael Kolbc831b632011-05-11 09:30:34 -0700137
Grace Kloba22ac16e2009-10-07 18:00:23 -0700138 // The Geolocation permissions prompt
139 private GeolocationPermissionsPrompt mGeolocationPermissionsPrompt;
140 // Main WebView wrapper
Michael Kolba713ec82010-11-29 17:27:06 -0800141 private View mContainer;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700142 // Main WebView
143 private WebView mMainView;
144 // Subwindow container
145 private View mSubViewContainer;
146 // Subwindow WebView
147 private WebView mSubView;
148 // Saved bundle for when we are running low on memory. It contains the
149 // information needed to restore the WebView if the user goes back to the
150 // tab.
151 private Bundle mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700152 // Parent Tab. This is the Tab that created this Tab, or null if the Tab was
153 // created by the UI
Michael Kolbc831b632011-05-11 09:30:34 -0700154 private Tab mParent;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700155 // Tab that constructed by this Tab. This is used when this Tab is
156 // destroyed, it clears all mParentTab values in the children.
Michael Kolbc831b632011-05-11 09:30:34 -0700157 private Vector<Tab> mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700158 // If true, the tab is in the foreground of the current activity.
159 private boolean mInForeground;
Michael Kolb8233fac2010-10-26 16:08:53 -0700160 // If true, the tab is in page loading state (after onPageStarted,
161 // before onPageFinsihed)
162 private boolean mInPageLoad;
John Reck30c714c2010-12-16 17:30:34 -0800163 // The last reported progress of the current page
164 private int mPageLoadProgress;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000165 // The time the load started, used to find load page time
166 private long mLoadStartTime;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700167 // Application identifier used to find tabs that another application wants
168 // to reuse.
169 private String mAppId;
Michael Kolbe28b3472011-08-04 16:54:31 -0700170 // flag to indicate if tab should be closed on back
171 private boolean mCloseOnBack;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700172 // Keep the original url around to avoid killing the old WebView if the url
173 // has not changed.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700174 // Error console for the tab
175 private ErrorConsoleView mErrorConsole;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -0500176 // The listener that gets invoked when a download is started from the
177 // mMainView
178 private final DownloadListener mDownloadListener;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500179 // Listener used to know when we move forward or back in the history list.
180 private final WebBackForwardListClient mWebBackForwardListClient;
John Recke969cc52010-12-21 17:24:43 -0800181 private DataController mDataController;
Patrick Scott92066772011-03-10 08:46:27 -0500182 // State of the auto-login request.
183 private DeviceAccountLogin mDeviceAccountLogin;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700184
185 // AsyncTask for downloading touch icons
186 DownloadTouchIcon mTouchIconLoader;
187
John Reck35e9dd62011-04-25 09:01:54 -0700188 private BrowserSettings mSettings;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700189 private int mCaptureWidth;
190 private int mCaptureHeight;
191 private Bitmap mCapture;
192 private Handler mHandler;
193
Mathew Inwood1dd8e822011-08-03 14:34:29 +0100194 /**
Mathew Inwood9ad1eac2011-09-15 11:29:50 +0100195 * See {@link #clearBackStackWhenItemAdded(String)}.
Mathew Inwood1dd8e822011-08-03 14:34:29 +0100196 */
Mathew Inwooda829d552011-09-02 14:16:25 +0100197 private Pattern mClearHistoryUrlPattern;
Mathew Inwood1dd8e822011-08-03 14:34:29 +0100198
John Reck1cf4b792011-07-26 10:22:22 -0700199 private static synchronized Bitmap getDefaultFavicon(Context context) {
200 if (sDefaultFavicon == null) {
201 sDefaultFavicon = BitmapFactory.decodeResource(
202 context.getResources(), R.drawable.app_web_browser_sm);
203 }
204 return sDefaultFavicon;
205 }
Michael Kolbeb95db42011-03-03 10:38:40 -0800206
John Reck30c714c2010-12-16 17:30:34 -0800207 // All the state needed for a page
John Reckd8c74522011-06-14 08:45:00 -0700208 protected static class PageState {
John Reck30c714c2010-12-16 17:30:34 -0800209 String mUrl;
John Reckdb22ec42011-06-29 11:31:24 -0700210 String mOriginalUrl;
John Reck30c714c2010-12-16 17:30:34 -0800211 String mTitle;
Steve Block2466eff2011-10-03 15:33:09 +0100212 SecurityState mSecurityState;
John Reck30c714c2010-12-16 17:30:34 -0800213 Bitmap mFavicon;
John Reck502a3532011-08-16 14:21:46 -0700214 boolean mIsBookmarkedSite = false;
215 boolean mIncognito = false;
John Reck30c714c2010-12-16 17:30:34 -0800216
217 PageState(Context c, boolean incognito) {
John Reck502a3532011-08-16 14:21:46 -0700218 mIncognito = incognito;
219 if (mIncognito) {
John Reckdb22ec42011-06-29 11:31:24 -0700220 mOriginalUrl = mUrl = "browser:incognito";
John Reck30c714c2010-12-16 17:30:34 -0800221 mTitle = c.getString(R.string.new_incognito_tab);
John Reck30c714c2010-12-16 17:30:34 -0800222 } else {
John Reckdb22ec42011-06-29 11:31:24 -0700223 mOriginalUrl = mUrl = "";
John Reck30c714c2010-12-16 17:30:34 -0800224 mTitle = c.getString(R.string.new_tab);
John Reck30c714c2010-12-16 17:30:34 -0800225 }
John Reck1cf4b792011-07-26 10:22:22 -0700226 mFavicon = null;
Steve Block2466eff2011-10-03 15:33:09 +0100227 mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
John Reck30c714c2010-12-16 17:30:34 -0800228 }
229
230 PageState(Context c, boolean incognito, String url, Bitmap favicon) {
John Reck502a3532011-08-16 14:21:46 -0700231 mIncognito = incognito;
John Reckdb22ec42011-06-29 11:31:24 -0700232 mOriginalUrl = mUrl = url;
John Reck30c714c2010-12-16 17:30:34 -0800233 mTitle = null;
234 if (URLUtil.isHttpsUrl(url)) {
Steve Block2466eff2011-10-03 15:33:09 +0100235 mSecurityState = SecurityState.SECURITY_STATE_SECURE;
John Reck30c714c2010-12-16 17:30:34 -0800236 } else {
Steve Block2466eff2011-10-03 15:33:09 +0100237 mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
John Reck30c714c2010-12-16 17:30:34 -0800238 }
John Reck1cf4b792011-07-26 10:22:22 -0700239 mFavicon = favicon;
John Reck30c714c2010-12-16 17:30:34 -0800240 }
John Reck1cf4b792011-07-26 10:22:22 -0700241
Grace Kloba22ac16e2009-10-07 18:00:23 -0700242 }
243
John Reck30c714c2010-12-16 17:30:34 -0800244 // The current/loading page's state
John Reckd8c74522011-06-14 08:45:00 -0700245 protected PageState mCurrentState;
John Reck30c714c2010-12-16 17:30:34 -0800246
Grace Kloba22ac16e2009-10-07 18:00:23 -0700247 // Used for saving and restoring each Tab
Michael Kolbc831b632011-05-11 09:30:34 -0700248 static final String ID = "ID";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700249 static final String CURRURL = "currentUrl";
250 static final String CURRTITLE = "currentTitle";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700251 static final String PARENTTAB = "parentTab";
252 static final String APPID = "appid";
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700253 static final String INCOGNITO = "privateBrowsingEnabled";
John Reckb0a86db2011-05-24 14:05:58 -0700254 static final String USERAGENT = "useragent";
Michael Kolbe28b3472011-08-04 16:54:31 -0700255 static final String CLOSEFLAG = "closeOnBack";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700256
257 // -------------------------------------------------------------------------
258
Leon Scroggins58d56c62010-01-28 15:12:40 -0500259 /**
260 * Private information regarding the latest voice search. If the Tab is not
261 * in voice search mode, this will be null.
262 */
263 private VoiceSearchData mVoiceSearchData;
264 /**
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400265 * Remove voice search mode from this tab.
266 */
267 public void revertVoiceSearchMode() {
268 if (mVoiceSearchData != null) {
269 mVoiceSearchData = null;
270 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700271 mWebViewController.revertVoiceSearchMode(this);
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400272 }
273 }
274 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700275
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400276 /**
Leon Scroggins58d56c62010-01-28 15:12:40 -0500277 * Return whether the tab is in voice search mode.
278 */
279 public boolean isInVoiceSearchMode() {
280 return mVoiceSearchData != null;
281 }
282 /**
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400283 * Return true if the Tab is in voice search mode and the voice search
284 * Intent came with a String identifying that Google provided the Intent.
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500285 */
286 public boolean voiceSearchSourceIsGoogle() {
287 return mVoiceSearchData != null && mVoiceSearchData.mSourceIsGoogle;
288 }
289 /**
Leon Scroggins58d56c62010-01-28 15:12:40 -0500290 * Get the title to display for the current voice search page. If the Tab
291 * is not in voice search mode, return null.
292 */
293 public String getVoiceDisplayTitle() {
294 if (mVoiceSearchData == null) return null;
295 return mVoiceSearchData.mLastVoiceSearchTitle;
296 }
297 /**
298 * Get the latest array of voice search results, to be passed to the
299 * BrowserProvider. If the Tab is not in voice search mode, return null.
300 */
301 public ArrayList<String> getVoiceSearchResults() {
302 if (mVoiceSearchData == null) return null;
303 return mVoiceSearchData.mVoiceSearchResults;
304 }
305 /**
306 * Activate voice search mode.
307 * @param intent Intent which has the results to use, or an index into the
308 * results when reusing the old results.
309 */
310 /* package */ void activateVoiceSearchMode(Intent intent) {
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500311 int index = 0;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500312 ArrayList<String> results = intent.getStringArrayListExtra(
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -0500313 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_STRINGS);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500314 if (results != null) {
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500315 ArrayList<String> urls = intent.getStringArrayListExtra(
316 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_URLS);
317 ArrayList<String> htmls = intent.getStringArrayListExtra(
318 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_HTML);
319 ArrayList<String> baseUrls = intent.getStringArrayListExtra(
320 RecognizerResultsIntent
321 .EXTRA_VOICE_SEARCH_RESULT_HTML_BASE_URLS);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500322 // This tab is now entering voice search mode for the first time, or
323 // a new voice search was done.
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500324 int size = results.size();
325 if (urls == null || size != urls.size()) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500326 throw new AssertionError("improper extras passed in Intent");
327 }
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500328 if (htmls == null || htmls.size() != size || baseUrls == null ||
329 (baseUrls.size() != size && baseUrls.size() != 1)) {
330 // If either of these arrays are empty/incorrectly sized, ignore
331 // them.
332 htmls = null;
333 baseUrls = null;
334 }
335 mVoiceSearchData = new VoiceSearchData(results, urls, htmls,
336 baseUrls);
Leon Scroggins9df94972010-03-08 18:20:35 -0500337 mVoiceSearchData.mHeaders = intent.getParcelableArrayListExtra(
338 RecognizerResultsIntent
339 .EXTRA_VOICE_SEARCH_RESULT_HTTP_HEADERS);
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500340 mVoiceSearchData.mSourceIsGoogle = intent.getBooleanExtra(
341 VoiceSearchData.SOURCE_IS_GOOGLE, false);
Leon Scroggins2ee4a5a2010-03-15 16:56:57 -0400342 mVoiceSearchData.mVoiceSearchIntent = new Intent(intent);
Leon Scrogginse10dde52010-03-08 19:53:03 -0500343 }
344 String extraData = intent.getStringExtra(
345 SearchManager.EXTRA_DATA_KEY);
346 if (extraData != null) {
347 index = Integer.parseInt(extraData);
348 if (index >= mVoiceSearchData.mVoiceSearchResults.size()) {
349 throw new AssertionError("index must be less than "
350 + "size of mVoiceSearchResults");
351 }
352 if (mVoiceSearchData.mSourceIsGoogle) {
353 Intent logIntent = new Intent(
354 LoggingEvents.ACTION_LOG_EVENT);
355 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
356 LoggingEvents.VoiceSearch.N_BEST_CHOOSE);
357 logIntent.putExtra(
358 LoggingEvents.VoiceSearch.EXTRA_N_BEST_CHOOSE_INDEX,
359 index);
Michael Kolb14612442011-06-24 13:06:29 -0700360 mContext.sendBroadcast(logIntent);
Leon Scrogginse10dde52010-03-08 19:53:03 -0500361 }
362 if (mVoiceSearchData.mVoiceSearchIntent != null) {
Leon Scroggins2ee4a5a2010-03-15 16:56:57 -0400363 // Copy the Intent, so that each history item will have its own
364 // Intent, with different (or none) extra data.
365 Intent latest = new Intent(mVoiceSearchData.mVoiceSearchIntent);
366 latest.putExtra(SearchManager.EXTRA_DATA_KEY, extraData);
367 mVoiceSearchData.mVoiceSearchIntent = latest;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500368 }
369 }
370 mVoiceSearchData.mLastVoiceSearchTitle
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500371 = mVoiceSearchData.mVoiceSearchResults.get(index);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500372 if (mInForeground) {
Michael Kolb11d19782011-03-20 10:17:40 -0700373 mWebViewController.activateVoiceSearchMode(
374 mVoiceSearchData.mLastVoiceSearchTitle,
375 mVoiceSearchData.mVoiceSearchResults);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500376 }
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500377 if (mVoiceSearchData.mVoiceSearchHtmls != null) {
378 // When index was found it was already ensured that it was valid
379 String uriString = mVoiceSearchData.mVoiceSearchHtmls.get(index);
380 if (uriString != null) {
381 Uri dataUri = Uri.parse(uriString);
382 if (RecognizerResultsIntent.URI_SCHEME_INLINE.equals(
383 dataUri.getScheme())) {
384 // If there is only one base URL, use it. If there are
385 // more, there will be one for each index, so use the base
386 // URL corresponding to the index.
387 String baseUrl = mVoiceSearchData.mVoiceSearchBaseUrls.get(
388 mVoiceSearchData.mVoiceSearchBaseUrls.size() > 1 ?
389 index : 0);
390 mVoiceSearchData.mLastVoiceSearchUrl = baseUrl;
391 mMainView.loadDataWithBaseURL(baseUrl,
392 uriString.substring(RecognizerResultsIntent
393 .URI_SCHEME_INLINE.length() + 1), "text/html",
394 "utf-8", baseUrl);
395 return;
396 }
397 }
398 }
Leon Scroggins58d56c62010-01-28 15:12:40 -0500399 mVoiceSearchData.mLastVoiceSearchUrl
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500400 = mVoiceSearchData.mVoiceSearchUrls.get(index);
401 if (null == mVoiceSearchData.mLastVoiceSearchUrl) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700402 mVoiceSearchData.mLastVoiceSearchUrl = UrlUtils.smartUrlFilter(
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500403 mVoiceSearchData.mLastVoiceSearchTitle);
404 }
Leon Scroggins9df94972010-03-08 18:20:35 -0500405 Map<String, String> headers = null;
406 if (mVoiceSearchData.mHeaders != null) {
407 int bundleIndex = mVoiceSearchData.mHeaders.size() == 1 ? 0
408 : index;
409 Bundle bundle = mVoiceSearchData.mHeaders.get(bundleIndex);
410 if (bundle != null && !bundle.isEmpty()) {
411 Iterator<String> iter = bundle.keySet().iterator();
412 headers = new HashMap<String, String>();
413 while (iter.hasNext()) {
414 String key = iter.next();
415 headers.put(key, bundle.getString(key));
416 }
417 }
418 }
419 mMainView.loadUrl(mVoiceSearchData.mLastVoiceSearchUrl, headers);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500420 }
421 /* package */ static class VoiceSearchData {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500422 public VoiceSearchData(ArrayList<String> results,
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500423 ArrayList<String> urls, ArrayList<String> htmls,
424 ArrayList<String> baseUrls) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500425 mVoiceSearchResults = results;
426 mVoiceSearchUrls = urls;
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500427 mVoiceSearchHtmls = htmls;
428 mVoiceSearchBaseUrls = baseUrls;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500429 }
430 /*
431 * ArrayList of suggestions to be displayed when opening the
432 * SearchManager
433 */
434 public ArrayList<String> mVoiceSearchResults;
435 /*
436 * ArrayList of urls, associated with the suggestions in
437 * mVoiceSearchResults.
438 */
439 public ArrayList<String> mVoiceSearchUrls;
440 /*
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500441 * ArrayList holding content to load for each item in
442 * mVoiceSearchResults.
443 */
444 public ArrayList<String> mVoiceSearchHtmls;
445 /*
446 * ArrayList holding base urls for the items in mVoiceSearchResults.
447 * If non null, this will either have the same size as
448 * mVoiceSearchResults or have a size of 1, in which case all will use
449 * the same base url
450 */
451 public ArrayList<String> mVoiceSearchBaseUrls;
452 /*
Leon Scroggins58d56c62010-01-28 15:12:40 -0500453 * The last url provided by voice search. Used for comparison to see if
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500454 * we are going to a page by some method besides voice search.
Leon Scroggins58d56c62010-01-28 15:12:40 -0500455 */
456 public String mLastVoiceSearchUrl;
457 /**
458 * The last title used for voice search. Needed to update the title bar
459 * when switching tabs.
460 */
461 public String mLastVoiceSearchTitle;
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500462 /**
463 * Whether the Intent which turned on voice search mode contained the
464 * String signifying that Google was the source.
465 */
466 public boolean mSourceIsGoogle;
467 /**
Leon Scroggins9df94972010-03-08 18:20:35 -0500468 * List of headers to be passed into the WebView containing location
469 * information
470 */
471 public ArrayList<Bundle> mHeaders;
472 /**
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500473 * The Intent used to invoke voice search. Placed on the
474 * WebHistoryItem so that when coming back to a previous voice search
475 * page we can again activate voice search.
476 */
Leon Scrogginse10dde52010-03-08 19:53:03 -0500477 public Intent mVoiceSearchIntent;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500478 /**
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500479 * String used to identify Google as the source of voice search.
480 */
481 public static String SOURCE_IS_GOOGLE
482 = "android.speech.extras.SOURCE_IS_GOOGLE";
Leon Scroggins58d56c62010-01-28 15:12:40 -0500483 }
484
Grace Kloba22ac16e2009-10-07 18:00:23 -0700485 // Container class for the next error dialog that needs to be displayed
486 private class ErrorDialog {
487 public final int mTitle;
488 public final String mDescription;
489 public final int mError;
490 ErrorDialog(int title, String desc, int error) {
491 mTitle = title;
492 mDescription = desc;
493 mError = error;
494 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700495 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700496
497 private void processNextError() {
498 if (mQueuedErrors == null) {
499 return;
500 }
501 // The first one is currently displayed so just remove it.
502 mQueuedErrors.removeFirst();
503 if (mQueuedErrors.size() == 0) {
504 mQueuedErrors = null;
505 return;
506 }
507 showError(mQueuedErrors.getFirst());
508 }
509
510 private DialogInterface.OnDismissListener mDialogListener =
511 new DialogInterface.OnDismissListener() {
512 public void onDismiss(DialogInterface d) {
513 processNextError();
514 }
515 };
516 private LinkedList<ErrorDialog> mQueuedErrors;
517
518 private void queueError(int err, String desc) {
519 if (mQueuedErrors == null) {
520 mQueuedErrors = new LinkedList<ErrorDialog>();
521 }
522 for (ErrorDialog d : mQueuedErrors) {
523 if (d.mError == err) {
524 // Already saw a similar error, ignore the new one.
525 return;
526 }
527 }
528 ErrorDialog errDialog = new ErrorDialog(
529 err == WebViewClient.ERROR_FILE_NOT_FOUND ?
530 R.string.browserFrameFileErrorLabel :
531 R.string.browserFrameNetworkErrorLabel,
532 desc, err);
533 mQueuedErrors.addLast(errDialog);
534
535 // Show the dialog now if the queue was empty and it is in foreground
536 if (mQueuedErrors.size() == 1 && mInForeground) {
537 showError(errDialog);
538 }
539 }
540
541 private void showError(ErrorDialog errDialog) {
542 if (mInForeground) {
Michael Kolb14612442011-06-24 13:06:29 -0700543 AlertDialog d = new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700544 .setTitle(errDialog.mTitle)
545 .setMessage(errDialog.mDescription)
546 .setPositiveButton(R.string.ok, null)
547 .create();
548 d.setOnDismissListener(mDialogListener);
549 d.show();
550 }
551 }
552
553 // -------------------------------------------------------------------------
554 // WebViewClient implementation for the main WebView
555 // -------------------------------------------------------------------------
556
557 private final WebViewClient mWebViewClient = new WebViewClient() {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500558 private Message mDontResend;
559 private Message mResend;
Michael Kolb47bd1e42011-09-01 15:25:00 -0700560
561 private boolean providersDiffer(String url, String otherUrl) {
562 Uri uri1 = Uri.parse(url);
563 Uri uri2 = Uri.parse(otherUrl);
564 return !uri1.getEncodedAuthority().equals(uri2.getEncodedAuthority());
565 }
566
Grace Kloba22ac16e2009-10-07 18:00:23 -0700567 @Override
568 public void onPageStarted(WebView view, String url, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700569 mInPageLoad = true;
Michael Kolba53c9892011-10-05 13:31:40 -0700570 mPageLoadProgress = INITIAL_PROGRESS;
Michael Kolb14612442011-06-24 13:06:29 -0700571 mCurrentState = new PageState(mContext,
John Reck30c714c2010-12-16 17:30:34 -0800572 view.isPrivateBrowsingEnabled(), url, favicon);
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000573 mLoadStartTime = SystemClock.uptimeMillis();
Leon Scroggins58d56c62010-01-28 15:12:40 -0500574 if (mVoiceSearchData != null
Michael Kolb47bd1e42011-09-01 15:25:00 -0700575 && providersDiffer(url, mVoiceSearchData.mLastVoiceSearchUrl)) {
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500576 if (mVoiceSearchData.mSourceIsGoogle) {
577 Intent i = new Intent(LoggingEvents.ACTION_LOG_EVENT);
578 i.putExtra(LoggingEvents.EXTRA_FLUSH, true);
Michael Kolb14612442011-06-24 13:06:29 -0700579 mContext.sendBroadcast(i);
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500580 }
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400581 revertVoiceSearchMode();
Leon Scroggins58d56c62010-01-28 15:12:40 -0500582 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700583
Grace Kloba22ac16e2009-10-07 18:00:23 -0700584
585 // If we start a touch icon load and then load a new page, we don't
586 // want to cancel the current touch icon loader. But, we do want to
587 // create a new one when the touch icon url is known.
588 if (mTouchIconLoader != null) {
589 mTouchIconLoader.mTab = null;
590 mTouchIconLoader = null;
591 }
592
593 // reset the error console
594 if (mErrorConsole != null) {
595 mErrorConsole.clearErrorMessages();
Michael Kolb8233fac2010-10-26 16:08:53 -0700596 if (mWebViewController.shouldShowErrorConsole()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700597 mErrorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
598 }
599 }
600
Patrick Scott92066772011-03-10 08:46:27 -0500601 // Cancel the auto-login process.
602 if (mDeviceAccountLogin != null) {
603 mDeviceAccountLogin.cancel();
604 mDeviceAccountLogin = null;
605 mWebViewController.hideAutoLogin(Tab.this);
606 }
607
Grace Kloba22ac16e2009-10-07 18:00:23 -0700608 // finally update the UI in the activity if it is in the foreground
John Reck324d4402011-01-11 16:56:42 -0800609 mWebViewController.onPageStarted(Tab.this, view, favicon);
Leon Scroggins4cd97792010-12-03 15:31:56 -0500610
John Recke969cc52010-12-21 17:24:43 -0800611 updateBookmarkedStatus();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700612 }
613
614 @Override
615 public void onPageFinished(WebView view, String url) {
John Recka1696282011-07-08 14:10:37 -0700616 if (!mInPageLoad) {
617 // In page navigation links (www.something.com#footer) will
618 // trigger an onPageFinished which we don't care about.
619 return;
620 }
John Reck5b691842010-11-29 11:21:13 -0800621 if (!isPrivateBrowsingEnabled()) {
622 LogTag.logPageFinishedLoading(
623 url, SystemClock.uptimeMillis() - mLoadStartTime);
624 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700625 mInPageLoad = false;
John Reck1cf4b792011-07-26 10:22:22 -0700626 syncCurrentState(view, url);
John Reck324d4402011-01-11 16:56:42 -0800627 mWebViewController.onPageFinished(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700628 }
629
630 // return true if want to hijack the url to let another app to handle it
631 @Override
632 public boolean shouldOverrideUrlLoading(WebView view, String url) {
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400633 if (voiceSearchSourceIsGoogle()) {
634 // This method is called when the user clicks on a link.
635 // VoiceSearchMode is turned off when the user leaves the
636 // Google results page, so at this point the user must be on
637 // that page. If the user clicked a link on that page, assume
638 // that the voice search was effective, and broadcast an Intent
639 // so a receiver can take note of that fact.
640 Intent logIntent = new Intent(LoggingEvents.ACTION_LOG_EVENT);
641 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
642 LoggingEvents.VoiceSearch.RESULT_CLICKED);
Michael Kolb14612442011-06-24 13:06:29 -0700643 mContext.sendBroadcast(logIntent);
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400644 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700645 if (mInForeground) {
Michael Kolb18eb3772010-12-10 14:29:51 -0800646 return mWebViewController.shouldOverrideUrlLoading(Tab.this,
647 view, url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700648 } else {
649 return false;
650 }
651 }
652
653 /**
Steve Block2466eff2011-10-03 15:33:09 +0100654 * Updates the security state. This method is called when we discover
655 * another resource to be loaded for this page (for example,
656 * javascript). While we update the security state, we do not update
657 * the lock icon until we are done loading, as it is slightly more
658 * secure this way.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700659 */
660 @Override
661 public void onLoadResource(WebView view, String url) {
662 if (url != null && url.length() > 0) {
663 // It is only if the page claims to be secure that we may have
Steve Block2466eff2011-10-03 15:33:09 +0100664 // to update the security state:
665 if (mCurrentState.mSecurityState == SecurityState.SECURITY_STATE_SECURE) {
666 // If NOT a 'safe' url, change the state to mixed content!
Grace Kloba22ac16e2009-10-07 18:00:23 -0700667 if (!(URLUtil.isHttpsUrl(url) || URLUtil.isDataUrl(url)
668 || URLUtil.isAboutUrl(url))) {
Steve Block2466eff2011-10-03 15:33:09 +0100669 mCurrentState.mSecurityState = SecurityState.SECURITY_STATE_MIXED;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700670 }
671 }
672 }
673 }
674
675 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700676 * Show a dialog informing the user of the network error reported by
677 * WebCore if it is in the foreground.
678 */
679 @Override
680 public void onReceivedError(WebView view, int errorCode,
681 String description, String failingUrl) {
682 if (errorCode != WebViewClient.ERROR_HOST_LOOKUP &&
683 errorCode != WebViewClient.ERROR_CONNECT &&
684 errorCode != WebViewClient.ERROR_BAD_URL &&
685 errorCode != WebViewClient.ERROR_UNSUPPORTED_SCHEME &&
686 errorCode != WebViewClient.ERROR_FILE) {
687 queueError(errorCode, description);
Jeff Hamilton47654f42010-09-07 09:57:51 -0500688
Selim Gurun3da06b82011-10-10 13:58:12 -0700689 // Don't log URLs when in private browsing mode
690 if (!isPrivateBrowsingEnabled()) {
691 Log.e(LOGTAG, "onReceivedError " + errorCode + " " + failingUrl
Jeff Hamilton47654f42010-09-07 09:57:51 -0500692 + " " + description);
Selim Gurun3da06b82011-10-10 13:58:12 -0700693 }
Jeff Hamilton47654f42010-09-07 09:57:51 -0500694 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700695 }
696
697 /**
698 * Check with the user if it is ok to resend POST data as the page they
699 * are trying to navigate to is the result of a POST.
700 */
701 @Override
702 public void onFormResubmission(WebView view, final Message dontResend,
703 final Message resend) {
704 if (!mInForeground) {
705 dontResend.sendToTarget();
706 return;
707 }
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500708 if (mDontResend != null) {
709 Log.w(LOGTAG, "onFormResubmission should not be called again "
710 + "while dialog is still up");
711 dontResend.sendToTarget();
712 return;
713 }
714 mDontResend = dontResend;
715 mResend = resend;
Michael Kolb14612442011-06-24 13:06:29 -0700716 new AlertDialog.Builder(mContext).setTitle(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700717 R.string.browserFrameFormResubmitLabel).setMessage(
718 R.string.browserFrameFormResubmitMessage)
719 .setPositiveButton(R.string.ok,
720 new DialogInterface.OnClickListener() {
721 public void onClick(DialogInterface dialog,
722 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500723 if (mResend != null) {
724 mResend.sendToTarget();
725 mResend = null;
726 mDontResend = null;
727 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700728 }
729 }).setNegativeButton(R.string.cancel,
730 new DialogInterface.OnClickListener() {
731 public void onClick(DialogInterface dialog,
732 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500733 if (mDontResend != null) {
734 mDontResend.sendToTarget();
735 mResend = null;
736 mDontResend = null;
737 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700738 }
739 }).setOnCancelListener(new OnCancelListener() {
740 public void onCancel(DialogInterface dialog) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500741 if (mDontResend != null) {
742 mDontResend.sendToTarget();
743 mResend = null;
744 mDontResend = null;
745 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700746 }
747 }).show();
748 }
749
750 /**
751 * Insert the url into the visited history database.
752 * @param url The url to be inserted.
753 * @param isReload True if this url is being reloaded.
754 * FIXME: Not sure what to do when reloading the page.
755 */
756 @Override
757 public void doUpdateVisitedHistory(WebView view, String url,
758 boolean isReload) {
John Reck324d4402011-01-11 16:56:42 -0800759 mWebViewController.doUpdateVisitedHistory(Tab.this, isReload);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700760 }
761
762 /**
763 * Displays SSL error(s) dialog to the user.
764 */
765 @Override
766 public void onReceivedSslError(final WebView view,
767 final SslErrorHandler handler, final SslError error) {
768 if (!mInForeground) {
769 handler.cancel();
Steve Block2466eff2011-10-03 15:33:09 +0100770 setSecurityState(SecurityState.SECURITY_STATE_NOT_SECURE);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700771 return;
772 }
John Reck35e9dd62011-04-25 09:01:54 -0700773 if (mSettings.showSecurityWarnings()) {
John Reckcb28b2c2011-08-26 17:39:44 -0700774 new AlertDialog.Builder(mContext)
775 .setTitle(R.string.security_warning)
776 .setMessage(R.string.ssl_warnings_header)
777 .setIcon(android.R.drawable.ic_dialog_alert)
778 .setPositiveButton(R.string.ssl_continue,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700779 new DialogInterface.OnClickListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700780 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700781 public void onClick(DialogInterface dialog,
782 int whichButton) {
783 handler.proceed();
Steve Block4895b012011-10-03 16:26:46 +0100784 handleProceededAfterSslError(error);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700785 }
John Reckcb28b2c2011-08-26 17:39:44 -0700786 })
787 .setNeutralButton(R.string.view_certificate,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700788 new DialogInterface.OnClickListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700789 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700790 public void onClick(DialogInterface dialog,
791 int whichButton) {
John Reckcb28b2c2011-08-26 17:39:44 -0700792 mWebViewController.showSslCertificateOnError(
793 view, handler, error);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700794 }
John Reckcb28b2c2011-08-26 17:39:44 -0700795 })
796 .setNegativeButton(R.string.ssl_go_back,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700797 new DialogInterface.OnClickListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700798 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700799 public void onClick(DialogInterface dialog,
800 int whichButton) {
John Reck30c714c2010-12-16 17:30:34 -0800801 dialog.cancel();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700802 }
John Reckcb28b2c2011-08-26 17:39:44 -0700803 })
804 .setOnCancelListener(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700805 new DialogInterface.OnCancelListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700806 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700807 public void onCancel(DialogInterface dialog) {
808 handler.cancel();
Steve Block2466eff2011-10-03 15:33:09 +0100809 setSecurityState(SecurityState.SECURITY_STATE_NOT_SECURE);
John Reck30c714c2010-12-16 17:30:34 -0800810 mWebViewController.onUserCanceledSsl(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700811 }
John Reckcb28b2c2011-08-26 17:39:44 -0700812 })
813 .show();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700814 } else {
815 handler.proceed();
816 }
817 }
818
819 /**
Steve Block4895b012011-10-03 16:26:46 +0100820 * Called when an SSL error occurred while loading a resource, but the
821 * WebView but chose to proceed anyway based on a decision retained
822 * from a previous response to onReceivedSslError(). We update our
823 * security state to reflect this.
824 */
825 @Override
826 public void onProceededAfterSslError(WebView view, SslError error) {
827 handleProceededAfterSslError(error);
828 }
829
830 /**
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700831 * Displays client certificate request to the user.
832 */
833 @Override
834 public void onReceivedClientCertRequest(final WebView view,
835 final ClientCertRequestHandler handler, final String host_and_port) {
836 if (!mInForeground) {
837 handler.ignore();
838 return;
839 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700840 int colon = host_and_port.lastIndexOf(':');
841 String host;
842 int port;
843 if (colon == -1) {
844 host = host_and_port;
845 port = -1;
846 } else {
847 String portString = host_and_port.substring(colon + 1);
848 try {
849 port = Integer.parseInt(portString);
850 host = host_and_port.substring(0, colon);
851 } catch (NumberFormatException e) {
852 host = host_and_port;
853 port = -1;
854 }
855 }
Michael Kolb14612442011-06-24 13:06:29 -0700856 KeyChain.choosePrivateKeyAlias(
857 mWebViewController.getActivity(), new KeyChainAliasCallback() {
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700858 @Override public void alias(String alias) {
859 if (alias == null) {
860 handler.cancel();
861 return;
862 }
Michael Kolb14612442011-06-24 13:06:29 -0700863 new KeyChainLookup(mContext, handler, alias).execute();
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700864 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700865 }, null, null, host, port, null);
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700866 }
867
868 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700869 * Handles an HTTP authentication request.
870 *
871 * @param handler The authentication handler
872 * @param host The host
873 * @param realm The realm
874 */
875 @Override
876 public void onReceivedHttpAuthRequest(WebView view,
877 final HttpAuthHandler handler, final String host,
878 final String realm) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700879 mWebViewController.onReceivedHttpAuthRequest(Tab.this, view, handler, host, realm);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700880 }
881
882 @Override
John Reck438bf462011-01-12 18:11:46 -0800883 public WebResourceResponse shouldInterceptRequest(WebView view,
884 String url) {
885 WebResourceResponse res = HomeProvider.shouldInterceptRequest(
Michael Kolb14612442011-06-24 13:06:29 -0700886 mContext, url);
John Reck438bf462011-01-12 18:11:46 -0800887 return res;
888 }
889
890 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700891 public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
892 if (!mInForeground) {
893 return false;
894 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700895 return mWebViewController.shouldOverrideKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700896 }
897
898 @Override
899 public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700900 if (!mInForeground) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700901 return;
902 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700903 mWebViewController.onUnhandledKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700904 }
Patrick Scott92066772011-03-10 08:46:27 -0500905
906 @Override
907 public void onReceivedLoginRequest(WebView view, String realm,
908 String account, String args) {
Michael Kolb14612442011-06-24 13:06:29 -0700909 new DeviceAccountLogin(mWebViewController.getActivity(), view, Tab.this, mWebViewController)
Patrick Scott92066772011-03-10 08:46:27 -0500910 .handleLogin(realm, account, args);
911 }
912
Grace Kloba22ac16e2009-10-07 18:00:23 -0700913 };
914
John Reck1cf4b792011-07-26 10:22:22 -0700915 private void syncCurrentState(WebView view, String url) {
916 // Sync state (in case of stop/timeout)
917 mCurrentState.mUrl = view.getUrl();
918 if (mCurrentState.mUrl == null) {
John Reck8bcafc12011-08-29 16:43:02 -0700919 mCurrentState.mUrl = "";
John Reck1cf4b792011-07-26 10:22:22 -0700920 }
921 mCurrentState.mOriginalUrl = view.getOriginalUrl();
922 mCurrentState.mTitle = view.getTitle();
923 mCurrentState.mFavicon = view.getFavicon();
924 if (!URLUtil.isHttpsUrl(mCurrentState.mUrl)) {
925 // In case we stop when loading an HTTPS page from an HTTP page
926 // but before a provisional load occurred
Steve Block2466eff2011-10-03 15:33:09 +0100927 mCurrentState.mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
John Reck1cf4b792011-07-26 10:22:22 -0700928 }
John Reck502a3532011-08-16 14:21:46 -0700929 mCurrentState.mIncognito = view.isPrivateBrowsingEnabled();
John Reck1cf4b792011-07-26 10:22:22 -0700930 }
931
Patrick Scott92066772011-03-10 08:46:27 -0500932 // Called by DeviceAccountLogin when the Tab needs to have the auto-login UI
933 // displayed.
934 void setDeviceAccountLogin(DeviceAccountLogin login) {
935 mDeviceAccountLogin = login;
936 }
937
938 // Returns non-null if the title bar should display the auto-login UI.
939 DeviceAccountLogin getDeviceAccountLogin() {
940 return mDeviceAccountLogin;
941 }
942
Grace Kloba22ac16e2009-10-07 18:00:23 -0700943 // -------------------------------------------------------------------------
944 // WebChromeClient implementation for the main WebView
945 // -------------------------------------------------------------------------
946
947 private final WebChromeClient mWebChromeClient = new WebChromeClient() {
948 // Helper method to create a new tab or sub window.
949 private void createWindow(final boolean dialog, final Message msg) {
950 WebView.WebViewTransport transport =
951 (WebView.WebViewTransport) msg.obj;
952 if (dialog) {
953 createSubWindow();
Michael Kolb8233fac2010-10-26 16:08:53 -0700954 mWebViewController.attachSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700955 transport.setWebView(mSubView);
956 } else {
Michael Kolb7bcafde2011-05-09 13:55:59 -0700957 final Tab newTab = mWebViewController.openTab(null,
John Reck5949c662011-05-27 09:52:29 -0700958 Tab.this, true, true);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700959 transport.setWebView(newTab.getWebView());
960 }
961 msg.sendToTarget();
962 }
963
964 @Override
965 public boolean onCreateWindow(WebView view, final boolean dialog,
966 final boolean userGesture, final Message resultMsg) {
967 // only allow new window or sub window for the foreground case
968 if (!mInForeground) {
969 return false;
970 }
971 // Short-circuit if we can't create any more tabs or sub windows.
972 if (dialog && mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -0700973 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700974 .setTitle(R.string.too_many_subwindows_dialog_title)
975 .setIcon(android.R.drawable.ic_dialog_alert)
976 .setMessage(R.string.too_many_subwindows_dialog_message)
977 .setPositiveButton(R.string.ok, null)
978 .show();
979 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700980 } else if (!mWebViewController.getTabControl().canCreateNewTab()) {
Michael Kolb14612442011-06-24 13:06:29 -0700981 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700982 .setTitle(R.string.too_many_windows_dialog_title)
983 .setIcon(android.R.drawable.ic_dialog_alert)
984 .setMessage(R.string.too_many_windows_dialog_message)
985 .setPositiveButton(R.string.ok, null)
986 .show();
987 return false;
988 }
989
990 // Short-circuit if this was a user gesture.
991 if (userGesture) {
992 createWindow(dialog, resultMsg);
993 return true;
994 }
995
996 // Allow the popup and create the appropriate window.
997 final AlertDialog.OnClickListener allowListener =
998 new AlertDialog.OnClickListener() {
999 public void onClick(DialogInterface d,
1000 int which) {
1001 createWindow(dialog, resultMsg);
1002 }
1003 };
1004
1005 // Block the popup by returning a null WebView.
1006 final AlertDialog.OnClickListener blockListener =
1007 new AlertDialog.OnClickListener() {
1008 public void onClick(DialogInterface d, int which) {
1009 resultMsg.sendToTarget();
1010 }
1011 };
1012
1013 // Build a confirmation dialog to display to the user.
1014 final AlertDialog d =
Michael Kolb14612442011-06-24 13:06:29 -07001015 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -07001016 .setTitle(R.string.attention)
1017 .setIcon(android.R.drawable.ic_dialog_alert)
1018 .setMessage(R.string.popup_window_attempt)
1019 .setPositiveButton(R.string.allow, allowListener)
1020 .setNegativeButton(R.string.block, blockListener)
1021 .setCancelable(false)
1022 .create();
1023
1024 // Show the confirmation dialog.
1025 d.show();
1026 return true;
1027 }
1028
1029 @Override
Patrick Scotteb5061b2009-11-18 15:00:30 -05001030 public void onRequestFocus(WebView view) {
1031 if (!mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -07001032 mWebViewController.switchToTab(Tab.this);
Patrick Scotteb5061b2009-11-18 15:00:30 -05001033 }
1034 }
1035
1036 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -07001037 public void onCloseWindow(WebView window) {
Michael Kolbc831b632011-05-11 09:30:34 -07001038 if (mParent != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001039 // JavaScript can only close popup window.
1040 if (mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -07001041 mWebViewController.switchToTab(mParent);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001042 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001043 mWebViewController.closeTab(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001044 }
1045 }
1046
1047 @Override
1048 public void onProgressChanged(WebView view, int newProgress) {
John Reck30c714c2010-12-16 17:30:34 -08001049 mPageLoadProgress = newProgress;
1050 mWebViewController.onProgressChanged(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001051 }
1052
1053 @Override
Leon Scroggins21d9b902010-03-11 09:33:11 -05001054 public void onReceivedTitle(WebView view, final String title) {
John Reck30c714c2010-12-16 17:30:34 -08001055 mCurrentState.mTitle = title;
Michael Kolb8233fac2010-10-26 16:08:53 -07001056 mWebViewController.onReceivedTitle(Tab.this, title);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001057 }
1058
1059 @Override
1060 public void onReceivedIcon(WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -08001061 mCurrentState.mFavicon = icon;
Michael Kolb8233fac2010-10-26 16:08:53 -07001062 mWebViewController.onFavicon(Tab.this, view, icon);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001063 }
1064
1065 @Override
1066 public void onReceivedTouchIconUrl(WebView view, String url,
1067 boolean precomposed) {
Michael Kolb14612442011-06-24 13:06:29 -07001068 final ContentResolver cr = mContext.getContentResolver();
Leon Scrogginsc8393d92010-04-23 14:58:16 -04001069 // Let precomposed icons take precedence over non-composed
1070 // icons.
1071 if (precomposed && mTouchIconLoader != null) {
1072 mTouchIconLoader.cancel(false);
1073 mTouchIconLoader = null;
1074 }
1075 // Have only one async task at a time.
1076 if (mTouchIconLoader == null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001077 mTouchIconLoader = new DownloadTouchIcon(Tab.this,
Michael Kolb14612442011-06-24 13:06:29 -07001078 mContext, cr, view);
Leon Scrogginsc8393d92010-04-23 14:58:16 -04001079 mTouchIconLoader.execute(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001080 }
1081 }
1082
1083 @Override
1084 public void onShowCustomView(View view,
1085 WebChromeClient.CustomViewCallback callback) {
Michael Kolb14612442011-06-24 13:06:29 -07001086 Activity activity = mWebViewController.getActivity();
1087 if (activity != null) {
1088 onShowCustomView(view, activity.getRequestedOrientation(), callback);
1089 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001090 }
1091
1092 @Override
1093 public void onShowCustomView(View view, int requestedOrientation,
1094 WebChromeClient.CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001095 if (mInForeground) mWebViewController.showCustomView(Tab.this, view,
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001096 requestedOrientation, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001097 }
1098
1099 @Override
1100 public void onHideCustomView() {
Michael Kolb8233fac2010-10-26 16:08:53 -07001101 if (mInForeground) mWebViewController.hideCustomView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001102 }
1103
1104 /**
1105 * The origin has exceeded its database quota.
1106 * @param url the URL that exceeded the quota
1107 * @param databaseIdentifier the identifier of the database on which the
1108 * transaction that caused the quota overflow was run
1109 * @param currentQuota the current quota for the origin.
1110 * @param estimatedSize the estimated size of the database.
1111 * @param totalUsedQuota is the sum of all origins' quota.
1112 * @param quotaUpdater The callback to run when a decision to allow or
1113 * deny quota has been made. Don't forget to call this!
1114 */
1115 @Override
1116 public void onExceededDatabaseQuota(String url,
1117 String databaseIdentifier, long currentQuota, long estimatedSize,
1118 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -07001119 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001120 .onExceededDatabaseQuota(url, databaseIdentifier,
1121 currentQuota, estimatedSize, totalUsedQuota,
1122 quotaUpdater);
1123 }
1124
1125 /**
1126 * The Application Cache has exceeded its max size.
1127 * @param spaceNeeded is the amount of disk space that would be needed
1128 * in order for the last appcache operation to succeed.
1129 * @param totalUsedQuota is the sum of all origins' quota.
1130 * @param quotaUpdater A callback to inform the WebCore thread that a
1131 * new app cache size is available. This callback must always
1132 * be executed at some point to ensure that the sleeping
1133 * WebCore thread is woken up.
1134 */
1135 @Override
1136 public void onReachedMaxAppCacheSize(long spaceNeeded,
1137 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -07001138 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001139 .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota,
1140 quotaUpdater);
1141 }
1142
1143 /**
1144 * Instructs the browser to show a prompt to ask the user to set the
1145 * Geolocation permission state for the specified origin.
1146 * @param origin The origin for which Geolocation permissions are
1147 * requested.
1148 * @param callback The callback to call once the user has set the
1149 * Geolocation permission state.
1150 */
1151 @Override
1152 public void onGeolocationPermissionsShowPrompt(String origin,
1153 GeolocationPermissions.Callback callback) {
1154 if (mInForeground) {
Grace Kloba50c241e2010-04-20 11:07:50 -07001155 getGeolocationPermissionsPrompt().show(origin, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001156 }
1157 }
1158
1159 /**
1160 * Instructs the browser to hide the Geolocation permissions prompt.
1161 */
1162 @Override
1163 public void onGeolocationPermissionsHidePrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001164 if (mInForeground && mGeolocationPermissionsPrompt != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001165 mGeolocationPermissionsPrompt.hide();
1166 }
1167 }
1168
Ben Murdoch65acc352009-11-19 18:16:04 +00001169 /* Adds a JavaScript error message to the system log and if the JS
1170 * console is enabled in the about:debug options, to that console
1171 * also.
Ben Murdochc42addf2010-01-28 15:19:59 +00001172 * @param consoleMessage the message object.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001173 */
1174 @Override
Ben Murdochc42addf2010-01-28 15:19:59 +00001175 public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001176 if (mInForeground) {
1177 // call getErrorConsole(true) so it will create one if needed
1178 ErrorConsoleView errorConsole = getErrorConsole(true);
Ben Murdochc42addf2010-01-28 15:19:59 +00001179 errorConsole.addErrorMessage(consoleMessage);
Michael Kolb8233fac2010-10-26 16:08:53 -07001180 if (mWebViewController.shouldShowErrorConsole()
1181 && errorConsole.getShowState() !=
1182 ErrorConsoleView.SHOW_MAXIMIZED) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001183 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
1184 }
1185 }
Ben Murdochc42addf2010-01-28 15:19:59 +00001186
Jeff Hamilton47654f42010-09-07 09:57:51 -05001187 // Don't log console messages in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001188 if (isPrivateBrowsingEnabled()) return true;
Jeff Hamilton47654f42010-09-07 09:57:51 -05001189
Ben Murdochc42addf2010-01-28 15:19:59 +00001190 String message = "Console: " + consoleMessage.message() + " "
1191 + consoleMessage.sourceId() + ":"
1192 + consoleMessage.lineNumber();
1193
1194 switch (consoleMessage.messageLevel()) {
1195 case TIP:
1196 Log.v(CONSOLE_LOGTAG, message);
1197 break;
1198 case LOG:
1199 Log.i(CONSOLE_LOGTAG, message);
1200 break;
1201 case WARNING:
1202 Log.w(CONSOLE_LOGTAG, message);
1203 break;
1204 case ERROR:
1205 Log.e(CONSOLE_LOGTAG, message);
1206 break;
1207 case DEBUG:
1208 Log.d(CONSOLE_LOGTAG, message);
1209 break;
1210 }
1211
1212 return true;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001213 }
1214
1215 /**
1216 * Ask the browser for an icon to represent a <video> element.
1217 * This icon will be used if the Web page did not specify a poster attribute.
1218 * @return Bitmap The icon or null if no such icon is available.
1219 */
1220 @Override
1221 public Bitmap getDefaultVideoPoster() {
1222 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001223 return mWebViewController.getDefaultVideoPoster();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001224 }
1225 return null;
1226 }
1227
1228 /**
1229 * Ask the host application for a custom progress view to show while
1230 * a <video> is loading.
1231 * @return View The progress view.
1232 */
1233 @Override
1234 public View getVideoLoadingProgressView() {
1235 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001236 return mWebViewController.getVideoLoadingProgressView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001237 }
1238 return null;
1239 }
1240
1241 @Override
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01001242 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001243 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001244 mWebViewController.openFileChooser(uploadMsg, acceptType);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001245 } else {
1246 uploadMsg.onReceiveValue(null);
1247 }
1248 }
1249
1250 /**
1251 * Deliver a list of already-visited URLs
1252 */
1253 @Override
1254 public void getVisitedHistory(final ValueCallback<String[]> callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001255 mWebViewController.getVisitedHistory(callback);
1256 }
Ben Murdoch8029a772010-11-16 11:58:21 +00001257
1258 @Override
1259 public void setupAutoFill(Message message) {
1260 // Prompt the user to set up their profile.
1261 final Message msg = message;
Michael Kolb14612442011-06-24 13:06:29 -07001262 AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
1263 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
Ben Murdoch1d676b62011-01-17 12:54:24 +00001264 Context.LAYOUT_INFLATER_SERVICE);
1265 final View layout = inflater.inflate(R.layout.setup_autofill_dialog, null);
1266
1267 builder.setView(layout)
Ben Murdochb7e6f942011-07-08 13:00:21 +01001268 .setTitle(R.string.autofill_setup_dialog_title)
Ben Murdoch1d676b62011-01-17 12:54:24 +00001269 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1270 @Override
1271 public void onClick(DialogInterface dialog, int id) {
1272 CheckBox disableAutoFill = (CheckBox) layout.findViewById(
1273 R.id.setup_autofill_dialog_disable_autofill);
1274
1275 if (disableAutoFill.isChecked()) {
1276 // Disable autofill and show a toast with how to turn it on again.
John Reck35e9dd62011-04-25 09:01:54 -07001277 mSettings.setAutofillEnabled(false);
Michael Kolb14612442011-06-24 13:06:29 -07001278 Toast.makeText(mContext,
Ben Murdoch1d676b62011-01-17 12:54:24 +00001279 R.string.autofill_setup_dialog_negative_toast,
1280 Toast.LENGTH_LONG).show();
1281 } else {
1282 // Take user to the AutoFill profile editor. When they return,
1283 // we will send the message that we pass here which will trigger
1284 // the form to get filled out with their new profile.
1285 mWebViewController.setupAutoFill(msg);
1286 }
1287 }
1288 })
1289 .setNegativeButton(R.string.cancel, null)
1290 .show();
Ben Murdoch8029a772010-11-16 11:58:21 +00001291 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001292 };
1293
1294 // -------------------------------------------------------------------------
1295 // WebViewClient implementation for the sub window
1296 // -------------------------------------------------------------------------
1297
1298 // Subclass of WebViewClient used in subwindows to notify the main
1299 // WebViewClient of certain WebView activities.
1300 private static class SubWindowClient extends WebViewClient {
1301 // The main WebViewClient.
1302 private final WebViewClient mClient;
Michael Kolb8233fac2010-10-26 16:08:53 -07001303 private final WebViewController mController;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001304
Michael Kolb8233fac2010-10-26 16:08:53 -07001305 SubWindowClient(WebViewClient client, WebViewController controller) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001306 mClient = client;
Michael Kolb8233fac2010-10-26 16:08:53 -07001307 mController = controller;
Leon Scroggins III211ba542010-04-19 13:21:13 -04001308 }
1309 @Override
1310 public void onPageStarted(WebView view, String url, Bitmap favicon) {
1311 // Unlike the others, do not call mClient's version, which would
1312 // change the progress bar. However, we do want to remove the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001313 // find or select dialog.
Michael Kolb8233fac2010-10-26 16:08:53 -07001314 mController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001315 }
1316 @Override
1317 public void doUpdateVisitedHistory(WebView view, String url,
1318 boolean isReload) {
1319 mClient.doUpdateVisitedHistory(view, url, isReload);
1320 }
1321 @Override
1322 public boolean shouldOverrideUrlLoading(WebView view, String url) {
1323 return mClient.shouldOverrideUrlLoading(view, url);
1324 }
1325 @Override
1326 public void onReceivedSslError(WebView view, SslErrorHandler handler,
1327 SslError error) {
1328 mClient.onReceivedSslError(view, handler, error);
1329 }
1330 @Override
Brian Carlstrom8862c1d2011-06-02 01:05:55 -07001331 public void onReceivedClientCertRequest(WebView view,
1332 ClientCertRequestHandler handler, String host_and_port) {
1333 mClient.onReceivedClientCertRequest(view, handler, host_and_port);
1334 }
1335 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -07001336 public void onReceivedHttpAuthRequest(WebView view,
1337 HttpAuthHandler handler, String host, String realm) {
1338 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
1339 }
1340 @Override
1341 public void onFormResubmission(WebView view, Message dontResend,
1342 Message resend) {
1343 mClient.onFormResubmission(view, dontResend, resend);
1344 }
1345 @Override
1346 public void onReceivedError(WebView view, int errorCode,
1347 String description, String failingUrl) {
1348 mClient.onReceivedError(view, errorCode, description, failingUrl);
1349 }
1350 @Override
1351 public boolean shouldOverrideKeyEvent(WebView view,
1352 android.view.KeyEvent event) {
1353 return mClient.shouldOverrideKeyEvent(view, event);
1354 }
1355 @Override
1356 public void onUnhandledKeyEvent(WebView view,
1357 android.view.KeyEvent event) {
1358 mClient.onUnhandledKeyEvent(view, event);
1359 }
1360 }
1361
1362 // -------------------------------------------------------------------------
1363 // WebChromeClient implementation for the sub window
1364 // -------------------------------------------------------------------------
1365
1366 private class SubWindowChromeClient extends WebChromeClient {
1367 // The main WebChromeClient.
1368 private final WebChromeClient mClient;
1369
1370 SubWindowChromeClient(WebChromeClient client) {
1371 mClient = client;
1372 }
1373 @Override
1374 public void onProgressChanged(WebView view, int newProgress) {
1375 mClient.onProgressChanged(view, newProgress);
1376 }
1377 @Override
1378 public boolean onCreateWindow(WebView view, boolean dialog,
1379 boolean userGesture, android.os.Message resultMsg) {
1380 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
1381 }
1382 @Override
1383 public void onCloseWindow(WebView window) {
1384 if (window != mSubView) {
1385 Log.e(LOGTAG, "Can't close the window");
1386 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001387 mWebViewController.dismissSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001388 }
1389 }
1390
1391 // -------------------------------------------------------------------------
1392
1393 // Construct a new tab
Michael Kolb7bcafde2011-05-09 13:55:59 -07001394 Tab(WebViewController wvcontroller, WebView w) {
John Reck1cf4b792011-07-26 10:22:22 -07001395 this(wvcontroller, w, null);
1396 }
1397
1398 Tab(WebViewController wvcontroller, Bundle state) {
1399 this(wvcontroller, null, state);
1400 }
1401
1402 Tab(WebViewController wvcontroller, WebView w, Bundle state) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001403 mWebViewController = wvcontroller;
Michael Kolb14612442011-06-24 13:06:29 -07001404 mContext = mWebViewController.getContext();
John Reck35e9dd62011-04-25 09:01:54 -07001405 mSettings = BrowserSettings.getInstance();
Michael Kolb14612442011-06-24 13:06:29 -07001406 mDataController = DataController.getInstance(mContext);
1407 mCurrentState = new PageState(mContext, w != null
John Reck847b5322011-04-14 17:02:18 -07001408 ? w.isPrivateBrowsingEnabled() : false);
Michael Kolb8233fac2010-10-26 16:08:53 -07001409 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001410 mInForeground = false;
1411
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001412 mDownloadListener = new DownloadListener() {
1413 public void onDownloadStart(String url, String userAgent,
1414 String contentDisposition, String mimetype,
1415 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001416 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001417 mimetype, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001418 }
1419 };
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001420 mWebBackForwardListClient = new WebBackForwardListClient() {
1421 @Override
1422 public void onNewHistoryItem(WebHistoryItem item) {
1423 if (isInVoiceSearchMode()) {
1424 item.setCustomData(mVoiceSearchData.mVoiceSearchIntent);
1425 }
Mathew Inwooda829d552011-09-02 14:16:25 +01001426 if (mClearHistoryUrlPattern != null) {
1427 boolean match =
1428 mClearHistoryUrlPattern.matcher(item.getOriginalUrl()).matches();
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001429 if (LOGD_ENABLED) {
Mathew Inwooda829d552011-09-02 14:16:25 +01001430 Log.d(LOGTAG, "onNewHistoryItem: match=" + match + "\n\t"
1431 + item.getUrl() + "\n\t"
1432 + mClearHistoryUrlPattern);
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001433 }
Mathew Inwooda829d552011-09-02 14:16:25 +01001434 if (match) {
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001435 if (mMainView != null) {
1436 mMainView.clearHistory();
1437 }
1438 }
Mathew Inwooda829d552011-09-02 14:16:25 +01001439 mClearHistoryUrlPattern = null;
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001440 }
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001441 }
1442 @Override
1443 public void onIndexChanged(WebHistoryItem item, int index) {
1444 Object data = item.getCustomData();
1445 if (data != null && data instanceof Intent) {
1446 activateVoiceSearchMode((Intent) data);
1447 }
1448 }
1449 };
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001450
John Reck1cf4b792011-07-26 10:22:22 -07001451 mCaptureWidth = mContext.getResources().getDimensionPixelSize(
1452 R.dimen.tab_thumbnail_width);
1453 mCaptureHeight = mContext.getResources().getDimensionPixelSize(
1454 R.dimen.tab_thumbnail_height);
1455 updateShouldCaptureThumbnails();
1456 restoreState(state);
John Reck52be4782011-08-26 15:37:29 -07001457 if (getId() == -1) {
1458 mId = TabControl.getNextId();
1459 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001460 setWebView(w);
Michael Kolb9ef259a2011-07-12 15:33:08 -07001461 mHandler = new Handler() {
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001462 @Override
Michael Kolb9ef259a2011-07-12 15:33:08 -07001463 public void handleMessage(Message m) {
John Reck1cf4b792011-07-26 10:22:22 -07001464 switch (m.what) {
1465 case MSG_CAPTURE:
1466 capture();
1467 break;
1468 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001469 }
1470 };
John Reck1cf4b792011-07-26 10:22:22 -07001471 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001472
Mathew Inwoode09305e2011-09-02 12:03:26 +01001473 /**
1474 * This is used to get a new ID when the tab has been preloaded, before it is displayed and
1475 * added to TabControl. Preloaded tabs can be created before restoreInstanceState, leading
1476 * to overlapping IDs between the preloaded and restored tabs.
1477 */
1478 public void refreshIdAfterPreload() {
1479 mId = TabControl.getNextId();
1480 }
1481
John Reck1cf4b792011-07-26 10:22:22 -07001482 public void updateShouldCaptureThumbnails() {
1483 if (mWebViewController.shouldCaptureThumbnails()) {
1484 synchronized (Tab.this) {
1485 if (mCapture == null) {
1486 mCapture = Bitmap.createBitmap(mCaptureWidth, mCaptureHeight,
1487 Bitmap.Config.RGB_565);
Michael Kolbc3af0672011-08-09 10:24:41 -07001488 mCapture.eraseColor(Color.WHITE);
John Reck1cf4b792011-07-26 10:22:22 -07001489 if (mInForeground) {
1490 postCapture();
1491 }
1492 }
1493 }
1494 } else {
1495 synchronized (Tab.this) {
1496 mCapture = null;
1497 deleteThumbnail();
1498 }
1499 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001500 }
1501
Michael Kolb14612442011-06-24 13:06:29 -07001502 public void setController(WebViewController ctl) {
1503 mWebViewController = ctl;
John Reck1cf4b792011-07-26 10:22:22 -07001504 updateShouldCaptureThumbnails();
Michael Kolb14612442011-06-24 13:06:29 -07001505 }
1506
Michael Kolbc831b632011-05-11 09:30:34 -07001507 public long getId() {
1508 return mId;
1509 }
1510
Grace Kloba22ac16e2009-10-07 18:00:23 -07001511 /**
1512 * Sets the WebView for this tab, correctly removing the old WebView from
1513 * the container view.
1514 */
1515 void setWebView(WebView w) {
1516 if (mMainView == w) {
1517 return;
1518 }
Michael Kolba713ec82010-11-29 17:27:06 -08001519
Grace Kloba22ac16e2009-10-07 18:00:23 -07001520 // If the WebView is changing, the page will be reloaded, so any ongoing
1521 // Geolocation permission requests are void.
Grace Kloba50c241e2010-04-20 11:07:50 -07001522 if (mGeolocationPermissionsPrompt != null) {
1523 mGeolocationPermissionsPrompt.hide();
1524 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001525
Michael Kolba713ec82010-11-29 17:27:06 -08001526 mWebViewController.onSetWebView(this, w);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001527
John Reck1cf4b792011-07-26 10:22:22 -07001528 if (mMainView != null) {
John Reck8ee633f2011-08-09 16:00:35 -07001529 mMainView.setPictureListener(null);
John Reck1cf4b792011-07-26 10:22:22 -07001530 if (w != null) {
1531 syncCurrentState(w, null);
1532 } else {
1533 mCurrentState = new PageState(mContext, false);
1534 }
1535 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001536 // set the new one
1537 mMainView = w;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001538 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001539 if (mMainView != null) {
1540 mMainView.setWebViewClient(mWebViewClient);
1541 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001542 // Attach DownloadManager so that downloads can start in an active
1543 // or a non-active window. This can happen when going to a site that
1544 // does a redirect after a period of time. The user could have
1545 // switched to another tab while waiting for the download to start.
1546 mMainView.setDownloadListener(mDownloadListener);
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001547 mMainView.setWebBackForwardListClient(mWebBackForwardListClient);
John Reck8ee633f2011-08-09 16:00:35 -07001548 TabControl tc = mWebViewController.getTabControl();
1549 if (tc != null && tc.getOnThumbnailUpdatedListener() != null) {
1550 mMainView.setPictureListener(this);
1551 }
John Reck1cf4b792011-07-26 10:22:22 -07001552 if (mSavedState != null) {
John Reck6c2e2f32011-08-22 13:41:23 -07001553 WebBackForwardList restoredState
1554 = mMainView.restoreState(mSavedState);
1555 if (restoredState == null || restoredState.getSize() == 0) {
1556 Log.w(LOGTAG, "Failed to restore WebView state!");
1557 loadUrl(mCurrentState.mOriginalUrl, null);
1558 }
John Reck1cf4b792011-07-26 10:22:22 -07001559 mSavedState = null;
1560 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001561 }
1562 }
1563
1564 /**
1565 * Destroy the tab's main WebView and subWindow if any
1566 */
1567 void destroy() {
1568 if (mMainView != null) {
1569 dismissSubWindow();
John Reckef654f12011-07-12 16:42:08 -07001570 // Make sure the embedded title bar isn't still attached
1571 mMainView.setEmbeddedTitleBar(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001572 // save the WebView to call destroy() after detach it from the tab
1573 WebView webView = mMainView;
1574 setWebView(null);
1575 webView.destroy();
1576 }
1577 }
1578
1579 /**
1580 * Remove the tab from the parent
1581 */
1582 void removeFromTree() {
1583 // detach the children
Michael Kolbc831b632011-05-11 09:30:34 -07001584 if (mChildren != null) {
1585 for(Tab t : mChildren) {
1586 t.setParent(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001587 }
1588 }
1589 // remove itself from the parent list
Michael Kolbc831b632011-05-11 09:30:34 -07001590 if (mParent != null) {
1591 mParent.mChildren.remove(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001592 }
John Reck1cf4b792011-07-26 10:22:22 -07001593 deleteThumbnail();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001594 }
1595
1596 /**
1597 * Create a new subwindow unless a subwindow already exists.
1598 * @return True if a new subwindow was created. False if one already exists.
1599 */
1600 boolean createSubWindow() {
1601 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001602 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001603 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001604 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001605 mSubView.setWebChromeClient(new SubWindowChromeClient(
1606 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001607 // Set a different DownloadListener for the mSubView, since it will
1608 // just need to dismiss the mSubView, rather than close the Tab
1609 mSubView.setDownloadListener(new DownloadListener() {
1610 public void onDownloadStart(String url, String userAgent,
1611 String contentDisposition, String mimetype,
1612 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001613 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001614 contentDisposition, mimetype, contentLength);
1615 if (mSubView.copyBackForwardList().getSize() == 0) {
1616 // This subwindow was opened for the sole purpose of
1617 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001618 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001619 }
1620 }
1621 });
Michael Kolb14612442011-06-24 13:06:29 -07001622 mSubView.setOnCreateContextMenuListener(mWebViewController.getActivity());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001623 return true;
1624 }
1625 return false;
1626 }
1627
1628 /**
1629 * Dismiss the subWindow for the tab.
1630 */
1631 void dismissSubWindow() {
1632 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001633 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001634 mSubView.destroy();
1635 mSubView = null;
1636 mSubViewContainer = null;
1637 }
1638 }
1639
Grace Kloba22ac16e2009-10-07 18:00:23 -07001640
1641 /**
1642 * Set the parent tab of this tab.
1643 */
Michael Kolbc831b632011-05-11 09:30:34 -07001644 void setParent(Tab parent) {
John Reck52be4782011-08-26 15:37:29 -07001645 if (parent == this) {
1646 throw new IllegalStateException("Cannot set parent to self!");
1647 }
Michael Kolbc831b632011-05-11 09:30:34 -07001648 mParent = parent;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001649 // This tab may have been freed due to low memory. If that is the case,
Michael Kolbc831b632011-05-11 09:30:34 -07001650 // the parent tab id is already saved. If we are changing that id
Grace Kloba22ac16e2009-10-07 18:00:23 -07001651 // (most likely due to removing the parent tab) we must update the
Michael Kolbc831b632011-05-11 09:30:34 -07001652 // parent tab id in the saved Bundle.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001653 if (mSavedState != null) {
1654 if (parent == null) {
1655 mSavedState.remove(PARENTTAB);
1656 } else {
Michael Kolbc831b632011-05-11 09:30:34 -07001657 mSavedState.putLong(PARENTTAB, parent.getId());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001658 }
1659 }
John Reckb0a86db2011-05-24 14:05:58 -07001660
1661 // Sync the WebView useragent with the parent
1662 if (parent != null && mSettings.hasDesktopUseragent(parent.getWebView())
1663 != mSettings.hasDesktopUseragent(getWebView())) {
1664 mSettings.toggleDesktopUseragent(getWebView());
1665 }
John Reck52be4782011-08-26 15:37:29 -07001666
1667 if (parent != null && parent.getId() == getId()) {
1668 throw new IllegalStateException("Parent has same ID as child!");
1669 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001670 }
1671
1672 /**
Michael Kolbc831b632011-05-11 09:30:34 -07001673 * If this Tab was created through another Tab, then this method returns
1674 * that Tab.
1675 * @return the Tab parent or null
1676 */
1677 public Tab getParent() {
1678 return mParent;
1679 }
1680
1681 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001682 * When a Tab is created through the content of another Tab, then we
1683 * associate the Tabs.
1684 * @param child the Tab that was created from this Tab
1685 */
1686 void addChildTab(Tab child) {
Michael Kolbc831b632011-05-11 09:30:34 -07001687 if (mChildren == null) {
1688 mChildren = new Vector<Tab>();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001689 }
Michael Kolbc831b632011-05-11 09:30:34 -07001690 mChildren.add(child);
1691 child.setParent(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001692 }
1693
Michael Kolbc831b632011-05-11 09:30:34 -07001694 Vector<Tab> getChildren() {
1695 return mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001696 }
1697
1698 void resume() {
1699 if (mMainView != null) {
John Reck56c1fcf2011-08-17 10:15:16 -07001700 setupHwAcceleration(mMainView);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001701 mMainView.onResume();
1702 if (mSubView != null) {
1703 mSubView.onResume();
1704 }
1705 }
1706 }
1707
John Reck56c1fcf2011-08-17 10:15:16 -07001708 private void setupHwAcceleration(View web) {
1709 if (web == null) return;
1710 BrowserSettings settings = BrowserSettings.getInstance();
1711 if (settings.isHardwareAccelerated()) {
1712 web.setLayerType(View.LAYER_TYPE_NONE, null);
1713 } else {
1714 web.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
1715 }
1716 }
1717
Grace Kloba22ac16e2009-10-07 18:00:23 -07001718 void pause() {
1719 if (mMainView != null) {
1720 mMainView.onPause();
1721 if (mSubView != null) {
1722 mSubView.onPause();
1723 }
1724 }
1725 }
1726
1727 void putInForeground() {
John Reck8ee633f2011-08-09 16:00:35 -07001728 if (mInForeground) {
1729 return;
1730 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001731 mInForeground = true;
1732 resume();
Michael Kolb14612442011-06-24 13:06:29 -07001733 Activity activity = mWebViewController.getActivity();
1734 mMainView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001735 if (mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -07001736 mSubView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001737 }
1738 // Show the pending error dialog if the queue is not empty
1739 if (mQueuedErrors != null && mQueuedErrors.size() > 0) {
1740 showError(mQueuedErrors.getFirst());
1741 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001742 mWebViewController.bookmarkedStatusHasChanged(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001743 }
1744
1745 void putInBackground() {
John Reck8ee633f2011-08-09 16:00:35 -07001746 if (!mInForeground) {
1747 return;
1748 }
John Reck52be4782011-08-26 15:37:29 -07001749 capture();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001750 mInForeground = false;
1751 pause();
1752 mMainView.setOnCreateContextMenuListener(null);
1753 if (mSubView != null) {
1754 mSubView.setOnCreateContextMenuListener(null);
1755 }
1756 }
1757
Michael Kolb8233fac2010-10-26 16:08:53 -07001758 boolean inForeground() {
1759 return mInForeground;
1760 }
1761
Grace Kloba22ac16e2009-10-07 18:00:23 -07001762 /**
1763 * Return the top window of this tab; either the subwindow if it is not
1764 * null or the main window.
1765 * @return The top window of this tab.
1766 */
1767 WebView getTopWindow() {
1768 if (mSubView != null) {
1769 return mSubView;
1770 }
1771 return mMainView;
1772 }
1773
1774 /**
1775 * Return the main window of this tab. Note: if a tab is freed in the
1776 * background, this can return null. It is only guaranteed to be
1777 * non-null for the current tab.
1778 * @return The main WebView of this tab.
1779 */
1780 WebView getWebView() {
1781 return mMainView;
1782 }
1783
Michael Kolba713ec82010-11-29 17:27:06 -08001784 void setViewContainer(View container) {
1785 mContainer = container;
1786 }
1787
Michael Kolb8233fac2010-10-26 16:08:53 -07001788 View getViewContainer() {
1789 return mContainer;
1790 }
1791
Grace Kloba22ac16e2009-10-07 18:00:23 -07001792 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001793 * Return whether private browsing is enabled for the main window of
1794 * this tab.
1795 * @return True if private browsing is enabled.
1796 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001797 boolean isPrivateBrowsingEnabled() {
John Reck502a3532011-08-16 14:21:46 -07001798 return mCurrentState.mIncognito;
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001799 }
1800
1801 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001802 * Return the subwindow of this tab or null if there is no subwindow.
1803 * @return The subwindow of this tab or null.
1804 */
1805 WebView getSubWebView() {
1806 return mSubView;
1807 }
1808
Michael Kolb1514bb72010-11-22 09:11:48 -08001809 void setSubWebView(WebView subView) {
1810 mSubView = subView;
1811 }
1812
Michael Kolb8233fac2010-10-26 16:08:53 -07001813 View getSubViewContainer() {
1814 return mSubViewContainer;
1815 }
1816
Michael Kolb1514bb72010-11-22 09:11:48 -08001817 void setSubViewContainer(View subViewContainer) {
1818 mSubViewContainer = subViewContainer;
1819 }
1820
Grace Kloba22ac16e2009-10-07 18:00:23 -07001821 /**
1822 * @return The geolocation permissions prompt for this tab.
1823 */
1824 GeolocationPermissionsPrompt getGeolocationPermissionsPrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001825 if (mGeolocationPermissionsPrompt == null) {
1826 ViewStub stub = (ViewStub) mContainer
1827 .findViewById(R.id.geolocation_permissions_prompt);
1828 mGeolocationPermissionsPrompt = (GeolocationPermissionsPrompt) stub
1829 .inflate();
Grace Kloba50c241e2010-04-20 11:07:50 -07001830 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001831 return mGeolocationPermissionsPrompt;
1832 }
1833
1834 /**
1835 * @return The application id string
1836 */
1837 String getAppId() {
1838 return mAppId;
1839 }
1840
1841 /**
1842 * Set the application id string
1843 * @param id
1844 */
1845 void setAppId(String id) {
1846 mAppId = id;
1847 }
1848
Michael Kolbe28b3472011-08-04 16:54:31 -07001849 boolean closeOnBack() {
1850 return mCloseOnBack;
1851 }
1852
1853 void setCloseOnBack(boolean close) {
1854 mCloseOnBack = close;
1855 }
1856
Grace Kloba22ac16e2009-10-07 18:00:23 -07001857 String getUrl() {
John Reck324d4402011-01-11 16:56:42 -08001858 return UrlUtils.filteredUrl(mCurrentState.mUrl);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001859 }
1860
John Reck49a603c2011-03-03 09:33:05 -08001861 String getOriginalUrl() {
John Reckdb22ec42011-06-29 11:31:24 -07001862 if (mCurrentState.mOriginalUrl == null) {
1863 return getUrl();
John Reck49a603c2011-03-03 09:33:05 -08001864 }
John Reckdb22ec42011-06-29 11:31:24 -07001865 return UrlUtils.filteredUrl(mCurrentState.mOriginalUrl);
John Reck49a603c2011-03-03 09:33:05 -08001866 }
1867
Grace Kloba22ac16e2009-10-07 18:00:23 -07001868 /**
John Reck30c714c2010-12-16 17:30:34 -08001869 * Get the title of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001870 */
1871 String getTitle() {
John Reck30c714c2010-12-16 17:30:34 -08001872 if (mCurrentState.mTitle == null && mInPageLoad) {
Michael Kolb14612442011-06-24 13:06:29 -07001873 return mContext.getString(R.string.title_bar_loading);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001874 }
John Reck30c714c2010-12-16 17:30:34 -08001875 return mCurrentState.mTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001876 }
1877
1878 /**
John Reck30c714c2010-12-16 17:30:34 -08001879 * Get the favicon of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001880 */
1881 Bitmap getFavicon() {
John Reck1cf4b792011-07-26 10:22:22 -07001882 if (mCurrentState.mFavicon != null) {
1883 return mCurrentState.mFavicon;
1884 }
1885 return getDefaultFavicon(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001886 }
1887
John Recke969cc52010-12-21 17:24:43 -08001888 public boolean isBookmarkedSite() {
1889 return mCurrentState.mIsBookmarkedSite;
1890 }
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001891
Grace Kloba22ac16e2009-10-07 18:00:23 -07001892 /**
1893 * Return the tab's error console. Creates the console if createIfNEcessary
1894 * is true and we haven't already created the console.
1895 * @param createIfNecessary Flag to indicate if the console should be
1896 * created if it has not been already.
1897 * @return The tab's error console, or null if one has not been created and
1898 * createIfNecessary is false.
1899 */
1900 ErrorConsoleView getErrorConsole(boolean createIfNecessary) {
1901 if (createIfNecessary && mErrorConsole == null) {
Michael Kolb14612442011-06-24 13:06:29 -07001902 mErrorConsole = new ErrorConsoleView(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001903 mErrorConsole.setWebView(mMainView);
1904 }
1905 return mErrorConsole;
1906 }
1907
Steve Block2466eff2011-10-03 15:33:09 +01001908 private void setSecurityState(SecurityState securityState) {
1909 mCurrentState.mSecurityState = securityState;
1910 mWebViewController.onUpdatedSecurityState(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001911 }
1912
1913 /**
Steve Block2466eff2011-10-03 15:33:09 +01001914 * @return The tab's security state.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001915 */
Steve Block2466eff2011-10-03 15:33:09 +01001916 SecurityState getSecurityState() {
1917 return mCurrentState.mSecurityState;
John Reck30c714c2010-12-16 17:30:34 -08001918 }
1919
1920 int getLoadProgress() {
1921 if (mInPageLoad) {
1922 return mPageLoadProgress;
1923 }
1924 return 100;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001925 }
1926
1927 /**
1928 * @return TRUE if onPageStarted is called while onPageFinished is not
1929 * called yet.
1930 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001931 boolean inPageLoad() {
1932 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001933 }
1934
1935 // force mInLoad to be false. This should only be called before closing the
1936 // tab to ensure BrowserActivity's pauseWebViewTimers() is called correctly.
Michael Kolb8233fac2010-10-26 16:08:53 -07001937 void clearInPageLoad() {
1938 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001939 }
1940
Grace Kloba22ac16e2009-10-07 18:00:23 -07001941 /**
John Reck1cf4b792011-07-26 10:22:22 -07001942 * @return The Bundle with the tab's state if it can be saved, otherwise null
Grace Kloba22ac16e2009-10-07 18:00:23 -07001943 */
John Reck1cf4b792011-07-26 10:22:22 -07001944 public Bundle saveState() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001945 // If the WebView is null it means we ran low on memory and we already
1946 // stored the saved state in mSavedState.
1947 if (mMainView == null) {
John Reck1cf4b792011-07-26 10:22:22 -07001948 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001949 }
John Reck6c2e2f32011-08-22 13:41:23 -07001950
1951 if (TextUtils.isEmpty(mCurrentState.mUrl)) {
John Reck1cf4b792011-07-26 10:22:22 -07001952 return null;
John Reck24f18262011-06-17 14:47:20 -07001953 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001954
1955 mSavedState = new Bundle();
John Reck6c2e2f32011-08-22 13:41:23 -07001956 WebBackForwardList savedList = mMainView.saveState(mSavedState);
1957 if (savedList == null || savedList.getSize() == 0) {
1958 Log.w(LOGTAG, "Failed to save back/forward list for "
1959 + mCurrentState.mUrl);
1960 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001961
Michael Kolbc831b632011-05-11 09:30:34 -07001962 mSavedState.putLong(ID, mId);
John Reck30c714c2010-12-16 17:30:34 -08001963 mSavedState.putString(CURRURL, mCurrentState.mUrl);
1964 mSavedState.putString(CURRTITLE, mCurrentState.mTitle);
John Reck1cf4b792011-07-26 10:22:22 -07001965 mSavedState.putBoolean(INCOGNITO, mMainView.isPrivateBrowsingEnabled());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001966 if (mAppId != null) {
1967 mSavedState.putString(APPID, mAppId);
1968 }
Michael Kolbe28b3472011-08-04 16:54:31 -07001969 mSavedState.putBoolean(CLOSEFLAG, mCloseOnBack);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001970 // Remember the parent tab so the relationship can be restored.
Michael Kolbc831b632011-05-11 09:30:34 -07001971 if (mParent != null) {
1972 mSavedState.putLong(PARENTTAB, mParent.mId);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001973 }
John Reckb0a86db2011-05-24 14:05:58 -07001974 mSavedState.putBoolean(USERAGENT,
1975 mSettings.hasDesktopUseragent(getWebView()));
John Reck1cf4b792011-07-26 10:22:22 -07001976 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001977 }
1978
1979 /*
1980 * Restore the state of the tab.
1981 */
John Reck1cf4b792011-07-26 10:22:22 -07001982 private void restoreState(Bundle b) {
1983 mSavedState = b;
1984 if (mSavedState == null) {
1985 return;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001986 }
1987 // Restore the internal state even if the WebView fails to restore.
1988 // This will maintain the app id, original url and close-on-exit values.
Michael Kolbc831b632011-05-11 09:30:34 -07001989 mId = b.getLong(ID);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001990 mAppId = b.getString(APPID);
Michael Kolbe28b3472011-08-04 16:54:31 -07001991 mCloseOnBack = b.getBoolean(CLOSEFLAG);
John Reckb0a86db2011-05-24 14:05:58 -07001992 if (b.getBoolean(USERAGENT)
1993 != mSettings.hasDesktopUseragent(getWebView())) {
1994 mSettings.toggleDesktopUseragent(getWebView());
1995 }
John Reck1cf4b792011-07-26 10:22:22 -07001996 String url = b.getString(CURRURL);
1997 String title = b.getString(CURRTITLE);
1998 boolean incognito = b.getBoolean(INCOGNITO);
1999 mCurrentState = new PageState(mContext, incognito, url, null);
2000 mCurrentState.mTitle = title;
2001 synchronized (Tab.this) {
2002 if (mCapture != null) {
2003 BackgroundHandler.execute(mLoadThumbnail);
2004 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002005 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002006 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04002007
Leon Scroggins1961ed22010-12-07 15:22:21 -05002008 public void updateBookmarkedStatus() {
John Recke969cc52010-12-21 17:24:43 -08002009 mDataController.queryBookmarkStatus(getUrl(), mIsBookmarkCallback);
Leon Scroggins1961ed22010-12-07 15:22:21 -05002010 }
2011
John Recke969cc52010-12-21 17:24:43 -08002012 private DataController.OnQueryUrlIsBookmark mIsBookmarkCallback
2013 = new DataController.OnQueryUrlIsBookmark() {
2014 @Override
2015 public void onQueryUrlIsBookmark(String url, boolean isBookmark) {
2016 if (mCurrentState.mUrl.equals(url)) {
2017 mCurrentState.mIsBookmarkedSite = isBookmark;
2018 mWebViewController.bookmarkedStatusHasChanged(Tab.this);
2019 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05002020 }
John Recke969cc52010-12-21 17:24:43 -08002021 };
Michael Kolb1acef692011-03-08 14:12:06 -08002022
Michael Kolbeb95db42011-03-03 10:38:40 -08002023 public Bitmap getScreenshot() {
John Reck1cf4b792011-07-26 10:22:22 -07002024 synchronized (Tab.this) {
2025 return mCapture;
2026 }
Michael Kolbeb95db42011-03-03 10:38:40 -08002027 }
2028
John Reck541f55a2011-06-07 16:34:43 -07002029 public boolean isSnapshot() {
John Reck541f55a2011-06-07 16:34:43 -07002030 return false;
2031 }
2032
John Reckd8c74522011-06-14 08:45:00 -07002033 public ContentValues createSnapshotValues() {
2034 if (mMainView == null) return null;
John Reck9d2718e2011-10-05 17:10:17 -07002035 SnapshotByteArrayOutputStream bos = new SnapshotByteArrayOutputStream();
John Reck8cc92352011-07-06 17:41:52 -07002036 try {
2037 GZIPOutputStream stream = new GZIPOutputStream(bos);
2038 if (!mMainView.saveViewState(stream)) {
2039 return null;
2040 }
2041 stream.flush();
2042 stream.close();
2043 } catch (Exception e) {
2044 Log.w(LOGTAG, "Failed to save view state", e);
John Reck541f55a2011-06-07 16:34:43 -07002045 return null;
2046 }
John Reck8cc92352011-07-06 17:41:52 -07002047 byte[] data = bos.toByteArray();
John Reckd8c74522011-06-14 08:45:00 -07002048 ContentValues values = new ContentValues();
2049 values.put(Snapshots.TITLE, mCurrentState.mTitle);
2050 values.put(Snapshots.URL, mCurrentState.mUrl);
2051 values.put(Snapshots.VIEWSTATE, data);
2052 values.put(Snapshots.BACKGROUND, mMainView.getPageBackgroundColor());
John Reck8cc92352011-07-06 17:41:52 -07002053 values.put(Snapshots.DATE_CREATED, System.currentTimeMillis());
2054 values.put(Snapshots.FAVICON, compressBitmap(getFavicon()));
2055 Bitmap screenshot = Controller.createScreenshot(mMainView,
2056 Controller.getDesiredThumbnailWidth(mContext),
2057 Controller.getDesiredThumbnailHeight(mContext));
2058 values.put(Snapshots.THUMBNAIL, compressBitmap(screenshot));
John Reckd8c74522011-06-14 08:45:00 -07002059 return values;
John Reck541f55a2011-06-07 16:34:43 -07002060 }
2061
John Reck8cc92352011-07-06 17:41:52 -07002062 public byte[] compressBitmap(Bitmap bitmap) {
2063 if (bitmap == null) {
2064 return null;
2065 }
2066 ByteArrayOutputStream stream = new ByteArrayOutputStream();
2067 bitmap.compress(CompressFormat.PNG, 100, stream);
2068 return stream.toByteArray();
2069 }
2070
John Reck26b18322011-06-21 13:08:58 -07002071 public void loadUrl(String url, Map<String, String> headers) {
2072 if (mMainView != null) {
Michael Kolba53c9892011-10-05 13:31:40 -07002073 mPageLoadProgress = INITIAL_PROGRESS;
2074 mInPageLoad = true;
Michael Kolb14612442011-06-24 13:06:29 -07002075 mCurrentState = new PageState(mContext, false, url, null);
John Reck26b18322011-06-21 13:08:58 -07002076 mWebViewController.onPageStarted(this, mMainView, null);
2077 mMainView.loadUrl(url, headers);
2078 }
2079 }
2080
Michael Kolb9ef259a2011-07-12 15:33:08 -07002081 protected void capture() {
2082 if (mMainView == null || mCapture == null) return;
2083 Canvas c = new Canvas(mCapture);
2084 final int left = mMainView.getScrollX();
2085 final int top = mMainView.getScrollY() + mMainView.getVisibleTitleHeight();
Michael Kolba3194d02011-09-07 11:23:51 -07002086 int state = c.save();
Michael Kolb9ef259a2011-07-12 15:33:08 -07002087 c.translate(-left, -top);
2088 float scale = mCaptureWidth / (float) mMainView.getWidth();
2089 c.scale(scale, scale, left, top);
John Reck8ee633f2011-08-09 16:00:35 -07002090 if (mMainView instanceof BrowserWebView) {
2091 ((BrowserWebView)mMainView).drawContent(c);
2092 } else {
2093 mMainView.draw(c);
2094 }
Michael Kolba3194d02011-09-07 11:23:51 -07002095 c.restoreToCount(state);
2096 // manually anti-alias the edges for the tilt
2097 c.drawRect(0, 0, 1, mCapture.getHeight(), sAlphaPaint);
2098 c.drawRect(mCapture.getWidth() - 1, 0, mCapture.getWidth(),
2099 mCapture.getHeight(), sAlphaPaint);
2100 c.drawRect(0, 0, mCapture.getWidth(), 1, sAlphaPaint);
2101 c.drawRect(0, mCapture.getHeight() - 1, mCapture.getWidth(),
2102 mCapture.getHeight(), sAlphaPaint);
Dianne Hackborn43cfe8a2011-08-02 16:59:35 -07002103 c.setBitmap(null);
John Reck8ee633f2011-08-09 16:00:35 -07002104 mHandler.removeMessages(MSG_CAPTURE);
John Reck1cf4b792011-07-26 10:22:22 -07002105 persistThumbnail();
John Reck8ee633f2011-08-09 16:00:35 -07002106 TabControl tc = mWebViewController.getTabControl();
2107 if (tc != null) {
2108 OnThumbnailUpdatedListener updateListener
2109 = tc.getOnThumbnailUpdatedListener();
2110 if (updateListener != null) {
2111 updateListener.onThumbnailUpdated(this);
2112 }
2113 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07002114 }
2115
2116 @Override
2117 public void onNewPicture(WebView view, Picture picture) {
2118 //update screenshot
John Reck1cf4b792011-07-26 10:22:22 -07002119 postCapture();
2120 }
2121
2122 private void postCapture() {
Michael Kolb9ef259a2011-07-12 15:33:08 -07002123 if (!mHandler.hasMessages(MSG_CAPTURE)) {
2124 mHandler.sendEmptyMessageDelayed(MSG_CAPTURE, CAPTURE_DELAY);
2125 }
2126 }
2127
John Reckef654f12011-07-12 16:42:08 -07002128 public boolean canGoBack() {
2129 return mMainView != null ? mMainView.canGoBack() : false;
2130 }
2131
2132 public boolean canGoForward() {
2133 return mMainView != null ? mMainView.canGoForward() : false;
2134 }
2135
2136 public void goBack() {
2137 if (mMainView != null) {
2138 mMainView.goBack();
2139 }
2140 }
2141
2142 public void goForward() {
2143 if (mMainView != null) {
2144 mMainView.goForward();
2145 }
2146 }
2147
Mathew Inwood1dd8e822011-08-03 14:34:29 +01002148 /**
2149 * Causes the tab back/forward stack to be cleared once, if the given URL is the next URL
2150 * to be added to the stack.
2151 *
2152 * This is used to ensure that preloaded URLs that are not subsequently seen by the user do
2153 * not appear in the back stack.
2154 */
Mathew Inwooda829d552011-09-02 14:16:25 +01002155 public void clearBackStackWhenItemAdded(Pattern urlPattern) {
2156 mClearHistoryUrlPattern = urlPattern;
Mathew Inwood1dd8e822011-08-03 14:34:29 +01002157 }
2158
John Reck1cf4b792011-07-26 10:22:22 -07002159 protected void persistThumbnail() {
2160 BackgroundHandler.execute(mSaveThumbnail);
2161 }
2162
2163 protected void deleteThumbnail() {
2164 BackgroundHandler.execute(mDeleteThumbnail);
2165 }
2166
2167 private void updateCaptureFromBlob(byte[] blob) {
2168 synchronized (Tab.this) {
2169 if (mCapture == null) {
2170 return;
2171 }
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002172 ByteBuffer buffer = ByteBuffer.wrap(blob);
Michael Kolbd837a112011-08-09 14:00:18 -07002173 try {
2174 mCapture.copyPixelsFromBuffer(buffer);
2175 } catch (RuntimeException rex) {
2176 Log.e(LOGTAG, "Load capture has mismatched sizes; buffer: "
2177 + buffer.capacity() + " blob: " + blob.length
2178 + "capture: " + mCapture.getByteCount());
2179 throw rex;
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002180 }
John Reck1cf4b792011-07-26 10:22:22 -07002181 }
2182 }
2183
John Reck9c5004e2011-10-07 16:00:12 -07002184 private static final ThreadLocal<ByteBuffer> sBuffer = new ThreadLocal<ByteBuffer>();
2185
John Reck1cf4b792011-07-26 10:22:22 -07002186 private byte[] getCaptureBlob() {
2187 synchronized (Tab.this) {
2188 if (mCapture == null) {
2189 return null;
2190 }
John Reck9c5004e2011-10-07 16:00:12 -07002191 ByteBuffer buffer = sBuffer.get();
2192 if (buffer == null || buffer.limit() < mCapture.getByteCount()) {
2193 buffer = ByteBuffer.allocate(mCapture.getByteCount());
2194 sBuffer.set(buffer);
2195 }
John Reck1cf4b792011-07-26 10:22:22 -07002196 mCapture.copyPixelsToBuffer(buffer);
John Reck9c5004e2011-10-07 16:00:12 -07002197 buffer.rewind();
John Reck1cf4b792011-07-26 10:22:22 -07002198 return buffer.array();
2199 }
2200 }
2201
2202 private Runnable mSaveThumbnail = new Runnable() {
2203
2204 @Override
2205 public void run() {
2206 byte[] blob = getCaptureBlob();
2207 if (blob == null) {
2208 return;
2209 }
2210 ContentResolver cr = mContext.getContentResolver();
2211 ContentValues values = new ContentValues();
2212 values.put(Thumbnails._ID, mId);
2213 values.put(Thumbnails.THUMBNAIL, blob);
2214 cr.insert(Thumbnails.CONTENT_URI, values);
2215 }
2216 };
2217
2218 private Runnable mDeleteThumbnail = new Runnable() {
2219
2220 @Override
2221 public void run() {
2222 ContentResolver cr = mContext.getContentResolver();
2223 try {
2224 cr.delete(ContentUris.withAppendedId(Thumbnails.CONTENT_URI, mId),
2225 null, null);
2226 } catch (Throwable t) {}
2227 }
2228 };
2229
2230 private Runnable mLoadThumbnail = new Runnable() {
2231
2232 @Override
2233 public void run() {
2234 ContentResolver cr = mContext.getContentResolver();
2235 Cursor c = null;
2236 try {
2237 Uri uri = ContentUris.withAppendedId(Thumbnails.CONTENT_URI, mId);
2238 c = cr.query(uri, new String[] {Thumbnails._ID,
2239 Thumbnails.THUMBNAIL}, null, null, null);
2240 if (c.moveToFirst()) {
2241 byte[] data = c.getBlob(1);
2242 if (data != null && data.length > 0) {
2243 updateCaptureFromBlob(data);
2244 }
2245 }
2246 } finally {
2247 if (c != null) {
2248 c.close();
2249 }
2250 }
2251 }
2252 };
2253
John Reck52be4782011-08-26 15:37:29 -07002254 @Override
2255 public String toString() {
2256 StringBuilder builder = new StringBuilder(100);
2257 builder.append(mId);
2258 builder.append(") has parent: ");
2259 if (getParent() != null) {
2260 builder.append("true[");
2261 builder.append(getParent().getId());
2262 builder.append("]");
2263 } else {
2264 builder.append("false");
2265 }
2266 builder.append(", incog: ");
2267 builder.append(isPrivateBrowsingEnabled());
2268 if (!isPrivateBrowsingEnabled()) {
2269 builder.append(", title: ");
2270 builder.append(getTitle());
2271 builder.append(", url: ");
2272 builder.append(getUrl());
2273 }
2274 return builder.toString();
2275 }
2276
Steve Block4895b012011-10-03 16:26:46 +01002277 private void handleProceededAfterSslError(SslError error) {
2278 if (error.getUrl().equals(mCurrentState.mUrl)) {
2279 // The security state should currently be SECURITY_STATE_SECURE.
2280 setSecurityState(SecurityState.SECURITY_STATE_BAD_CERTIFICATE);
2281 } else if (getSecurityState() == SecurityState.SECURITY_STATE_SECURE) {
2282 setSecurityState(SecurityState.SECURITY_STATE_MIXED);
2283 }
2284 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002285}