blob: cbda456a1cb0e653411951bfeee0f80ddb28ed5e [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 Reckd8c74522011-06-14 08:45:00 -070023import android.content.ContentValues;
John Reck30c714c2010-12-16 17:30:34 -080024import android.content.Context;
Grace Kloba22ac16e2009-10-07 18:00:23 -070025import android.content.DialogInterface;
Michael Kolbfe251992010-07-08 15:41:55 -070026import android.content.DialogInterface.OnCancelListener;
Jeff Hamilton8ce956c2010-08-17 11:13:53 -050027import android.content.Intent;
Grace Kloba22ac16e2009-10-07 18:00:23 -070028import android.graphics.Bitmap;
John Reck8cc92352011-07-06 17:41:52 -070029import android.graphics.Bitmap.CompressFormat;
Michael Kolb9ef259a2011-07-12 15:33:08 -070030import android.graphics.BitmapFactory;
31import android.graphics.Canvas;
Michael Kolbc3af0672011-08-09 10:24:41 -070032import android.graphics.Color;
Michael Kolba3194d02011-09-07 11:23:51 -070033import android.graphics.Paint;
Michael Kolb9ef259a2011-07-12 15:33:08 -070034import android.graphics.Picture;
Michael Kolba3194d02011-09-07 11:23:51 -070035import android.graphics.PorterDuff;
36import android.graphics.PorterDuffXfermode;
Grace Kloba22ac16e2009-10-07 18:00:23 -070037import android.net.Uri;
38import android.net.http.SslError;
Grace Kloba22ac16e2009-10-07 18:00:23 -070039import android.os.Bundle;
Michael Kolb9ef259a2011-07-12 15:33:08 -070040import android.os.Handler;
Grace Kloba22ac16e2009-10-07 18:00:23 -070041import android.os.Message;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +000042import android.os.SystemClock;
Brian Carlstrom8862c1d2011-06-02 01:05:55 -070043import android.security.KeyChain;
Brian Carlstromaa09cd82011-06-09 16:04:40 -070044import android.security.KeyChainAliasCallback;
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -050045import android.speech.RecognizerResultsIntent;
John Reck24f18262011-06-17 14:47:20 -070046import android.text.TextUtils;
Grace Kloba22ac16e2009-10-07 18:00:23 -070047import android.util.Log;
48import android.view.KeyEvent;
49import android.view.LayoutInflater;
50import android.view.View;
Grace Kloba50c241e2010-04-20 11:07:50 -070051import android.view.ViewStub;
Brian Carlstrom8862c1d2011-06-02 01:05:55 -070052import android.webkit.ClientCertRequestHandler;
Ben Murdochc42addf2010-01-28 15:19:59 +000053import android.webkit.ConsoleMessage;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -050054import android.webkit.DownloadListener;
Grace Kloba22ac16e2009-10-07 18:00:23 -070055import android.webkit.GeolocationPermissions;
56import android.webkit.HttpAuthHandler;
57import android.webkit.SslErrorHandler;
58import android.webkit.URLUtil;
59import android.webkit.ValueCallback;
John Reck6c2e2f32011-08-22 13:41:23 -070060import android.webkit.WebBackForwardList;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -050061import android.webkit.WebBackForwardListClient;
Grace Kloba22ac16e2009-10-07 18:00:23 -070062import android.webkit.WebChromeClient;
63import android.webkit.WebHistoryItem;
John Reck438bf462011-01-12 18:11:46 -080064import android.webkit.WebResourceResponse;
Grace Kloba22ac16e2009-10-07 18:00:23 -070065import android.webkit.WebStorage;
66import android.webkit.WebView;
Michael Kolb9ef259a2011-07-12 15:33:08 -070067import android.webkit.WebView.PictureListener;
John Reck2b71d6d2012-04-18 17:42:06 -070068import android.webkit.WebViewClassic;
Grace Kloba22ac16e2009-10-07 18:00:23 -070069import android.webkit.WebViewClient;
Ben Murdoch1d676b62011-01-17 12:54:24 +000070import android.widget.CheckBox;
Ben Murdoch8029a772010-11-16 11:58:21 +000071import android.widget.Toast;
Grace Kloba22ac16e2009-10-07 18:00:23 -070072
John Reck8ee633f2011-08-09 16:00:35 -070073import com.android.browser.TabControl.OnThumbnailUpdatedListener;
John Reck541f55a2011-06-07 16:34:43 -070074import com.android.browser.homepages.HomeProvider;
John Reck8cc92352011-07-06 17:41:52 -070075import com.android.browser.provider.SnapshotProvider.Snapshots;
John Reck541f55a2011-06-07 16:34:43 -070076import com.android.common.speech.LoggingEvents;
77
78import java.io.ByteArrayOutputStream;
John Reck2b71d6d2012-04-18 17:42:06 -070079import java.io.File;
John Reck68234a92012-04-19 15:27:12 -070080import java.io.IOException;
John Reck2b71d6d2012-04-18 17:42:06 -070081import java.io.OutputStream;
John Reck1cf4b792011-07-26 10:22:22 -070082import java.nio.ByteBuffer;
Michael Kolbfe251992010-07-08 15:41:55 -070083import java.util.ArrayList;
84import java.util.HashMap;
85import java.util.Iterator;
86import java.util.LinkedList;
87import java.util.Map;
John Reck2b71d6d2012-04-18 17:42:06 -070088import java.util.UUID;
Michael Kolbfe251992010-07-08 15:41:55 -070089import java.util.Vector;
Mathew Inwooda829d552011-09-02 14:16:25 +010090import java.util.regex.Pattern;
John Reck8cc92352011-07-06 17:41:52 -070091import java.util.zip.GZIPOutputStream;
Michael Kolbfe251992010-07-08 15:41:55 -070092
Grace Kloba22ac16e2009-10-07 18:00:23 -070093/**
94 * Class for maintaining Tabs with a main WebView and a subwindow.
95 */
Michael Kolb9ef259a2011-07-12 15:33:08 -070096class Tab implements PictureListener {
Michael Kolb8233fac2010-10-26 16:08:53 -070097
Grace Kloba22ac16e2009-10-07 18:00:23 -070098 // Log Tag
99 private static final String LOGTAG = "Tab";
Mathew Inwood1dd8e822011-08-03 14:34:29 +0100100 private static final boolean LOGD_ENABLED = com.android.browser.Browser.LOGD_ENABLED;
Ben Murdochc42addf2010-01-28 15:19:59 +0000101 // Special case the logtag for messages for the Console to make it easier to
102 // filter them and match the logtag used for these messages in older versions
103 // of the browser.
104 private static final String CONSOLE_LOGTAG = "browser";
105
Michael Kolb9ef259a2011-07-12 15:33:08 -0700106 private static final int MSG_CAPTURE = 42;
John Reck8ee633f2011-08-09 16:00:35 -0700107 private static final int CAPTURE_DELAY = 100;
Michael Kolba53c9892011-10-05 13:31:40 -0700108 private static final int INITIAL_PROGRESS = 5;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700109
John Reck1cf4b792011-07-26 10:22:22 -0700110 private static Bitmap sDefaultFavicon;
111
Michael Kolba3194d02011-09-07 11:23:51 -0700112 private static Paint sAlphaPaint = new Paint();
113 static {
114 sAlphaPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
115 sAlphaPaint.setColor(Color.TRANSPARENT);
116 }
117
Steve Block2466eff2011-10-03 15:33:09 +0100118 public enum SecurityState {
Steve Block4895b012011-10-03 16:26:46 +0100119 // The page's main resource does not use SSL. Note that we use this
120 // state irrespective of the SSL authentication state of sub-resources.
Steve Block2466eff2011-10-03 15:33:09 +0100121 SECURITY_STATE_NOT_SECURE,
Steve Block4895b012011-10-03 16:26:46 +0100122 // The page's main resource uses SSL and the certificate is good. The
123 // same is true of all sub-resources.
Steve Block2466eff2011-10-03 15:33:09 +0100124 SECURITY_STATE_SECURE,
Steve Block4895b012011-10-03 16:26:46 +0100125 // The page's main resource uses SSL and the certificate is good, but
126 // some sub-resources either do not use SSL or have problems with their
127 // certificates.
Steve Block2466eff2011-10-03 15:33:09 +0100128 SECURITY_STATE_MIXED,
Steve Block4895b012011-10-03 16:26:46 +0100129 // The page's main resource uses SSL but there is a problem with its
130 // certificate.
131 SECURITY_STATE_BAD_CERTIFICATE,
John Reck30c714c2010-12-16 17:30:34 -0800132 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700133
Michael Kolb14612442011-06-24 13:06:29 -0700134 Context mContext;
John Reckd8c74522011-06-14 08:45:00 -0700135 protected WebViewController mWebViewController;
Michael Kolb8233fac2010-10-26 16:08:53 -0700136
Michael Kolbc831b632011-05-11 09:30:34 -0700137 // The tab ID
John Reckd8c74522011-06-14 08:45:00 -0700138 private long mId = -1;
Michael Kolbc831b632011-05-11 09:30:34 -0700139
Grace Kloba22ac16e2009-10-07 18:00:23 -0700140 // The Geolocation permissions prompt
141 private GeolocationPermissionsPrompt mGeolocationPermissionsPrompt;
142 // Main WebView wrapper
Michael Kolba713ec82010-11-29 17:27:06 -0800143 private View mContainer;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700144 // Main WebView
145 private WebView mMainView;
146 // Subwindow container
147 private View mSubViewContainer;
148 // Subwindow WebView
149 private WebView mSubView;
150 // Saved bundle for when we are running low on memory. It contains the
151 // information needed to restore the WebView if the user goes back to the
152 // tab.
153 private Bundle mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700154 // Parent Tab. This is the Tab that created this Tab, or null if the Tab was
155 // created by the UI
Michael Kolbc831b632011-05-11 09:30:34 -0700156 private Tab mParent;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700157 // Tab that constructed by this Tab. This is used when this Tab is
158 // destroyed, it clears all mParentTab values in the children.
Michael Kolbc831b632011-05-11 09:30:34 -0700159 private Vector<Tab> mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700160 // If true, the tab is in the foreground of the current activity.
161 private boolean mInForeground;
Michael Kolb8233fac2010-10-26 16:08:53 -0700162 // If true, the tab is in page loading state (after onPageStarted,
163 // before onPageFinsihed)
164 private boolean mInPageLoad;
John Reck30c714c2010-12-16 17:30:34 -0800165 // The last reported progress of the current page
166 private int mPageLoadProgress;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000167 // The time the load started, used to find load page time
168 private long mLoadStartTime;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700169 // Application identifier used to find tabs that another application wants
170 // to reuse.
171 private String mAppId;
Michael Kolbe28b3472011-08-04 16:54:31 -0700172 // flag to indicate if tab should be closed on back
173 private boolean mCloseOnBack;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700174 // Keep the original url around to avoid killing the old WebView if the url
175 // has not changed.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700176 // Error console for the tab
177 private ErrorConsoleView mErrorConsole;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -0500178 // The listener that gets invoked when a download is started from the
179 // mMainView
180 private final DownloadListener mDownloadListener;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500181 // Listener used to know when we move forward or back in the history list.
182 private final WebBackForwardListClient mWebBackForwardListClient;
John Recke969cc52010-12-21 17:24:43 -0800183 private DataController mDataController;
Patrick Scott92066772011-03-10 08:46:27 -0500184 // State of the auto-login request.
185 private DeviceAccountLogin mDeviceAccountLogin;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700186
187 // AsyncTask for downloading touch icons
188 DownloadTouchIcon mTouchIconLoader;
189
John Reck35e9dd62011-04-25 09:01:54 -0700190 private BrowserSettings mSettings;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700191 private int mCaptureWidth;
192 private int mCaptureHeight;
193 private Bitmap mCapture;
194 private Handler mHandler;
195
Mathew Inwood1dd8e822011-08-03 14:34:29 +0100196 /**
Mathew Inwood9ad1eac2011-09-15 11:29:50 +0100197 * See {@link #clearBackStackWhenItemAdded(String)}.
Mathew Inwood1dd8e822011-08-03 14:34:29 +0100198 */
Mathew Inwooda829d552011-09-02 14:16:25 +0100199 private Pattern mClearHistoryUrlPattern;
Mathew Inwood1dd8e822011-08-03 14:34:29 +0100200
John Reck1cf4b792011-07-26 10:22:22 -0700201 private static synchronized Bitmap getDefaultFavicon(Context context) {
202 if (sDefaultFavicon == null) {
203 sDefaultFavicon = BitmapFactory.decodeResource(
204 context.getResources(), R.drawable.app_web_browser_sm);
205 }
206 return sDefaultFavicon;
207 }
Michael Kolbeb95db42011-03-03 10:38:40 -0800208
John Reck30c714c2010-12-16 17:30:34 -0800209 // All the state needed for a page
John Reckd8c74522011-06-14 08:45:00 -0700210 protected static class PageState {
John Reck30c714c2010-12-16 17:30:34 -0800211 String mUrl;
John Reckdb22ec42011-06-29 11:31:24 -0700212 String mOriginalUrl;
John Reck30c714c2010-12-16 17:30:34 -0800213 String mTitle;
Steve Block2466eff2011-10-03 15:33:09 +0100214 SecurityState mSecurityState;
Steve Block08a6f0c2011-10-06 12:12:53 +0100215 // This is non-null only when mSecurityState is SECURITY_STATE_BAD_CERTIFICATE.
216 SslError mSslCertificateError;
John Reck30c714c2010-12-16 17:30:34 -0800217 Bitmap mFavicon;
Steve Block08a6f0c2011-10-06 12:12:53 +0100218 boolean mIsBookmarkedSite;
219 boolean mIncognito;
John Reck30c714c2010-12-16 17:30:34 -0800220
221 PageState(Context c, boolean incognito) {
John Reck502a3532011-08-16 14:21:46 -0700222 mIncognito = incognito;
223 if (mIncognito) {
John Reckdb22ec42011-06-29 11:31:24 -0700224 mOriginalUrl = mUrl = "browser:incognito";
John Reck30c714c2010-12-16 17:30:34 -0800225 mTitle = c.getString(R.string.new_incognito_tab);
John Reck30c714c2010-12-16 17:30:34 -0800226 } else {
John Reckdb22ec42011-06-29 11:31:24 -0700227 mOriginalUrl = mUrl = "";
John Reck30c714c2010-12-16 17:30:34 -0800228 mTitle = c.getString(R.string.new_tab);
John Reck30c714c2010-12-16 17:30:34 -0800229 }
Steve Block2466eff2011-10-03 15:33:09 +0100230 mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
John Reck30c714c2010-12-16 17:30:34 -0800231 }
232
233 PageState(Context c, boolean incognito, String url, Bitmap favicon) {
John Reck502a3532011-08-16 14:21:46 -0700234 mIncognito = incognito;
John Reckdb22ec42011-06-29 11:31:24 -0700235 mOriginalUrl = mUrl = url;
John Reck30c714c2010-12-16 17:30:34 -0800236 if (URLUtil.isHttpsUrl(url)) {
Steve Block2466eff2011-10-03 15:33:09 +0100237 mSecurityState = SecurityState.SECURITY_STATE_SECURE;
John Reck30c714c2010-12-16 17:30:34 -0800238 } else {
Steve Block2466eff2011-10-03 15:33:09 +0100239 mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
John Reck30c714c2010-12-16 17:30:34 -0800240 }
John Reck1cf4b792011-07-26 10:22:22 -0700241 mFavicon = favicon;
John Reck30c714c2010-12-16 17:30:34 -0800242 }
John Reck1cf4b792011-07-26 10:22:22 -0700243
Grace Kloba22ac16e2009-10-07 18:00:23 -0700244 }
245
John Reck30c714c2010-12-16 17:30:34 -0800246 // The current/loading page's state
John Reckd8c74522011-06-14 08:45:00 -0700247 protected PageState mCurrentState;
John Reck30c714c2010-12-16 17:30:34 -0800248
Grace Kloba22ac16e2009-10-07 18:00:23 -0700249 // Used for saving and restoring each Tab
Michael Kolbc831b632011-05-11 09:30:34 -0700250 static final String ID = "ID";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700251 static final String CURRURL = "currentUrl";
252 static final String CURRTITLE = "currentTitle";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700253 static final String PARENTTAB = "parentTab";
254 static final String APPID = "appid";
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700255 static final String INCOGNITO = "privateBrowsingEnabled";
John Reckb0a86db2011-05-24 14:05:58 -0700256 static final String USERAGENT = "useragent";
Michael Kolbe28b3472011-08-04 16:54:31 -0700257 static final String CLOSEFLAG = "closeOnBack";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700258
259 // -------------------------------------------------------------------------
260
Leon Scroggins58d56c62010-01-28 15:12:40 -0500261 /**
262 * Private information regarding the latest voice search. If the Tab is not
263 * in voice search mode, this will be null.
264 */
265 private VoiceSearchData mVoiceSearchData;
266 /**
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400267 * Remove voice search mode from this tab.
268 */
269 public void revertVoiceSearchMode() {
270 if (mVoiceSearchData != null) {
271 mVoiceSearchData = null;
272 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700273 mWebViewController.revertVoiceSearchMode(this);
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400274 }
275 }
276 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700277
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400278 /**
Leon Scroggins58d56c62010-01-28 15:12:40 -0500279 * Return whether the tab is in voice search mode.
280 */
281 public boolean isInVoiceSearchMode() {
282 return mVoiceSearchData != null;
283 }
284 /**
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400285 * Return true if the Tab is in voice search mode and the voice search
286 * Intent came with a String identifying that Google provided the Intent.
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500287 */
288 public boolean voiceSearchSourceIsGoogle() {
289 return mVoiceSearchData != null && mVoiceSearchData.mSourceIsGoogle;
290 }
291 /**
Leon Scroggins58d56c62010-01-28 15:12:40 -0500292 * Get the title to display for the current voice search page. If the Tab
293 * is not in voice search mode, return null.
294 */
295 public String getVoiceDisplayTitle() {
296 if (mVoiceSearchData == null) return null;
297 return mVoiceSearchData.mLastVoiceSearchTitle;
298 }
299 /**
300 * Get the latest array of voice search results, to be passed to the
301 * BrowserProvider. If the Tab is not in voice search mode, return null.
302 */
303 public ArrayList<String> getVoiceSearchResults() {
304 if (mVoiceSearchData == null) return null;
305 return mVoiceSearchData.mVoiceSearchResults;
306 }
307 /**
308 * Activate voice search mode.
309 * @param intent Intent which has the results to use, or an index into the
310 * results when reusing the old results.
311 */
312 /* package */ void activateVoiceSearchMode(Intent intent) {
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500313 int index = 0;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500314 ArrayList<String> results = intent.getStringArrayListExtra(
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -0500315 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_STRINGS);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500316 if (results != null) {
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500317 ArrayList<String> urls = intent.getStringArrayListExtra(
318 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_URLS);
319 ArrayList<String> htmls = intent.getStringArrayListExtra(
320 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_HTML);
321 ArrayList<String> baseUrls = intent.getStringArrayListExtra(
322 RecognizerResultsIntent
323 .EXTRA_VOICE_SEARCH_RESULT_HTML_BASE_URLS);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500324 // This tab is now entering voice search mode for the first time, or
325 // a new voice search was done.
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500326 int size = results.size();
327 if (urls == null || size != urls.size()) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500328 throw new AssertionError("improper extras passed in Intent");
329 }
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500330 if (htmls == null || htmls.size() != size || baseUrls == null ||
331 (baseUrls.size() != size && baseUrls.size() != 1)) {
332 // If either of these arrays are empty/incorrectly sized, ignore
333 // them.
334 htmls = null;
335 baseUrls = null;
336 }
337 mVoiceSearchData = new VoiceSearchData(results, urls, htmls,
338 baseUrls);
Leon Scroggins9df94972010-03-08 18:20:35 -0500339 mVoiceSearchData.mHeaders = intent.getParcelableArrayListExtra(
340 RecognizerResultsIntent
341 .EXTRA_VOICE_SEARCH_RESULT_HTTP_HEADERS);
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500342 mVoiceSearchData.mSourceIsGoogle = intent.getBooleanExtra(
343 VoiceSearchData.SOURCE_IS_GOOGLE, false);
Leon Scroggins2ee4a5a2010-03-15 16:56:57 -0400344 mVoiceSearchData.mVoiceSearchIntent = new Intent(intent);
Leon Scrogginse10dde52010-03-08 19:53:03 -0500345 }
346 String extraData = intent.getStringExtra(
347 SearchManager.EXTRA_DATA_KEY);
348 if (extraData != null) {
349 index = Integer.parseInt(extraData);
350 if (index >= mVoiceSearchData.mVoiceSearchResults.size()) {
351 throw new AssertionError("index must be less than "
352 + "size of mVoiceSearchResults");
353 }
354 if (mVoiceSearchData.mSourceIsGoogle) {
355 Intent logIntent = new Intent(
356 LoggingEvents.ACTION_LOG_EVENT);
357 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
358 LoggingEvents.VoiceSearch.N_BEST_CHOOSE);
359 logIntent.putExtra(
360 LoggingEvents.VoiceSearch.EXTRA_N_BEST_CHOOSE_INDEX,
361 index);
Michael Kolb14612442011-06-24 13:06:29 -0700362 mContext.sendBroadcast(logIntent);
Leon Scrogginse10dde52010-03-08 19:53:03 -0500363 }
364 if (mVoiceSearchData.mVoiceSearchIntent != null) {
Leon Scroggins2ee4a5a2010-03-15 16:56:57 -0400365 // Copy the Intent, so that each history item will have its own
366 // Intent, with different (or none) extra data.
367 Intent latest = new Intent(mVoiceSearchData.mVoiceSearchIntent);
368 latest.putExtra(SearchManager.EXTRA_DATA_KEY, extraData);
369 mVoiceSearchData.mVoiceSearchIntent = latest;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500370 }
371 }
372 mVoiceSearchData.mLastVoiceSearchTitle
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500373 = mVoiceSearchData.mVoiceSearchResults.get(index);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500374 if (mInForeground) {
Michael Kolb11d19782011-03-20 10:17:40 -0700375 mWebViewController.activateVoiceSearchMode(
376 mVoiceSearchData.mLastVoiceSearchTitle,
377 mVoiceSearchData.mVoiceSearchResults);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500378 }
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500379 if (mVoiceSearchData.mVoiceSearchHtmls != null) {
380 // When index was found it was already ensured that it was valid
381 String uriString = mVoiceSearchData.mVoiceSearchHtmls.get(index);
382 if (uriString != null) {
383 Uri dataUri = Uri.parse(uriString);
384 if (RecognizerResultsIntent.URI_SCHEME_INLINE.equals(
385 dataUri.getScheme())) {
386 // If there is only one base URL, use it. If there are
387 // more, there will be one for each index, so use the base
388 // URL corresponding to the index.
389 String baseUrl = mVoiceSearchData.mVoiceSearchBaseUrls.get(
390 mVoiceSearchData.mVoiceSearchBaseUrls.size() > 1 ?
391 index : 0);
392 mVoiceSearchData.mLastVoiceSearchUrl = baseUrl;
393 mMainView.loadDataWithBaseURL(baseUrl,
394 uriString.substring(RecognizerResultsIntent
395 .URI_SCHEME_INLINE.length() + 1), "text/html",
396 "utf-8", baseUrl);
397 return;
398 }
399 }
400 }
Leon Scroggins58d56c62010-01-28 15:12:40 -0500401 mVoiceSearchData.mLastVoiceSearchUrl
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500402 = mVoiceSearchData.mVoiceSearchUrls.get(index);
403 if (null == mVoiceSearchData.mLastVoiceSearchUrl) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700404 mVoiceSearchData.mLastVoiceSearchUrl = UrlUtils.smartUrlFilter(
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500405 mVoiceSearchData.mLastVoiceSearchTitle);
406 }
Leon Scroggins9df94972010-03-08 18:20:35 -0500407 Map<String, String> headers = null;
408 if (mVoiceSearchData.mHeaders != null) {
409 int bundleIndex = mVoiceSearchData.mHeaders.size() == 1 ? 0
410 : index;
411 Bundle bundle = mVoiceSearchData.mHeaders.get(bundleIndex);
412 if (bundle != null && !bundle.isEmpty()) {
413 Iterator<String> iter = bundle.keySet().iterator();
414 headers = new HashMap<String, String>();
415 while (iter.hasNext()) {
416 String key = iter.next();
417 headers.put(key, bundle.getString(key));
418 }
419 }
420 }
421 mMainView.loadUrl(mVoiceSearchData.mLastVoiceSearchUrl, headers);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500422 }
423 /* package */ static class VoiceSearchData {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500424 public VoiceSearchData(ArrayList<String> results,
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500425 ArrayList<String> urls, ArrayList<String> htmls,
426 ArrayList<String> baseUrls) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500427 mVoiceSearchResults = results;
428 mVoiceSearchUrls = urls;
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500429 mVoiceSearchHtmls = htmls;
430 mVoiceSearchBaseUrls = baseUrls;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500431 }
432 /*
433 * ArrayList of suggestions to be displayed when opening the
434 * SearchManager
435 */
436 public ArrayList<String> mVoiceSearchResults;
437 /*
438 * ArrayList of urls, associated with the suggestions in
439 * mVoiceSearchResults.
440 */
441 public ArrayList<String> mVoiceSearchUrls;
442 /*
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500443 * ArrayList holding content to load for each item in
444 * mVoiceSearchResults.
445 */
446 public ArrayList<String> mVoiceSearchHtmls;
447 /*
448 * ArrayList holding base urls for the items in mVoiceSearchResults.
449 * If non null, this will either have the same size as
450 * mVoiceSearchResults or have a size of 1, in which case all will use
451 * the same base url
452 */
453 public ArrayList<String> mVoiceSearchBaseUrls;
454 /*
Leon Scroggins58d56c62010-01-28 15:12:40 -0500455 * The last url provided by voice search. Used for comparison to see if
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500456 * we are going to a page by some method besides voice search.
Leon Scroggins58d56c62010-01-28 15:12:40 -0500457 */
458 public String mLastVoiceSearchUrl;
459 /**
460 * The last title used for voice search. Needed to update the title bar
461 * when switching tabs.
462 */
463 public String mLastVoiceSearchTitle;
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500464 /**
465 * Whether the Intent which turned on voice search mode contained the
466 * String signifying that Google was the source.
467 */
468 public boolean mSourceIsGoogle;
469 /**
Leon Scroggins9df94972010-03-08 18:20:35 -0500470 * List of headers to be passed into the WebView containing location
471 * information
472 */
473 public ArrayList<Bundle> mHeaders;
474 /**
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500475 * The Intent used to invoke voice search. Placed on the
476 * WebHistoryItem so that when coming back to a previous voice search
477 * page we can again activate voice search.
478 */
Leon Scrogginse10dde52010-03-08 19:53:03 -0500479 public Intent mVoiceSearchIntent;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500480 /**
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500481 * String used to identify Google as the source of voice search.
482 */
483 public static String SOURCE_IS_GOOGLE
484 = "android.speech.extras.SOURCE_IS_GOOGLE";
Leon Scroggins58d56c62010-01-28 15:12:40 -0500485 }
486
Grace Kloba22ac16e2009-10-07 18:00:23 -0700487 // Container class for the next error dialog that needs to be displayed
488 private class ErrorDialog {
489 public final int mTitle;
490 public final String mDescription;
491 public final int mError;
492 ErrorDialog(int title, String desc, int error) {
493 mTitle = title;
494 mDescription = desc;
495 mError = error;
496 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700497 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700498
499 private void processNextError() {
500 if (mQueuedErrors == null) {
501 return;
502 }
503 // The first one is currently displayed so just remove it.
504 mQueuedErrors.removeFirst();
505 if (mQueuedErrors.size() == 0) {
506 mQueuedErrors = null;
507 return;
508 }
509 showError(mQueuedErrors.getFirst());
510 }
511
512 private DialogInterface.OnDismissListener mDialogListener =
513 new DialogInterface.OnDismissListener() {
514 public void onDismiss(DialogInterface d) {
515 processNextError();
516 }
517 };
518 private LinkedList<ErrorDialog> mQueuedErrors;
519
520 private void queueError(int err, String desc) {
521 if (mQueuedErrors == null) {
522 mQueuedErrors = new LinkedList<ErrorDialog>();
523 }
524 for (ErrorDialog d : mQueuedErrors) {
525 if (d.mError == err) {
526 // Already saw a similar error, ignore the new one.
527 return;
528 }
529 }
530 ErrorDialog errDialog = new ErrorDialog(
531 err == WebViewClient.ERROR_FILE_NOT_FOUND ?
532 R.string.browserFrameFileErrorLabel :
533 R.string.browserFrameNetworkErrorLabel,
534 desc, err);
535 mQueuedErrors.addLast(errDialog);
536
537 // Show the dialog now if the queue was empty and it is in foreground
538 if (mQueuedErrors.size() == 1 && mInForeground) {
539 showError(errDialog);
540 }
541 }
542
543 private void showError(ErrorDialog errDialog) {
544 if (mInForeground) {
Michael Kolb14612442011-06-24 13:06:29 -0700545 AlertDialog d = new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700546 .setTitle(errDialog.mTitle)
547 .setMessage(errDialog.mDescription)
548 .setPositiveButton(R.string.ok, null)
549 .create();
550 d.setOnDismissListener(mDialogListener);
551 d.show();
552 }
553 }
554
555 // -------------------------------------------------------------------------
556 // WebViewClient implementation for the main WebView
557 // -------------------------------------------------------------------------
558
559 private final WebViewClient mWebViewClient = new WebViewClient() {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500560 private Message mDontResend;
561 private Message mResend;
Michael Kolb47bd1e42011-09-01 15:25:00 -0700562
563 private boolean providersDiffer(String url, String otherUrl) {
564 Uri uri1 = Uri.parse(url);
565 Uri uri2 = Uri.parse(otherUrl);
566 return !uri1.getEncodedAuthority().equals(uri2.getEncodedAuthority());
567 }
568
Grace Kloba22ac16e2009-10-07 18:00:23 -0700569 @Override
570 public void onPageStarted(WebView view, String url, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700571 mInPageLoad = true;
Michael Kolba53c9892011-10-05 13:31:40 -0700572 mPageLoadProgress = INITIAL_PROGRESS;
Michael Kolb14612442011-06-24 13:06:29 -0700573 mCurrentState = new PageState(mContext,
John Reck30c714c2010-12-16 17:30:34 -0800574 view.isPrivateBrowsingEnabled(), url, favicon);
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000575 mLoadStartTime = SystemClock.uptimeMillis();
Leon Scroggins58d56c62010-01-28 15:12:40 -0500576 if (mVoiceSearchData != null
Michael Kolb47bd1e42011-09-01 15:25:00 -0700577 && providersDiffer(url, mVoiceSearchData.mLastVoiceSearchUrl)) {
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500578 if (mVoiceSearchData.mSourceIsGoogle) {
579 Intent i = new Intent(LoggingEvents.ACTION_LOG_EVENT);
580 i.putExtra(LoggingEvents.EXTRA_FLUSH, true);
Michael Kolb14612442011-06-24 13:06:29 -0700581 mContext.sendBroadcast(i);
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500582 }
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400583 revertVoiceSearchMode();
Leon Scroggins58d56c62010-01-28 15:12:40 -0500584 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700585
Grace Kloba22ac16e2009-10-07 18:00:23 -0700586
587 // If we start a touch icon load and then load a new page, we don't
588 // want to cancel the current touch icon loader. But, we do want to
589 // create a new one when the touch icon url is known.
590 if (mTouchIconLoader != null) {
591 mTouchIconLoader.mTab = null;
592 mTouchIconLoader = null;
593 }
594
595 // reset the error console
596 if (mErrorConsole != null) {
597 mErrorConsole.clearErrorMessages();
Michael Kolb8233fac2010-10-26 16:08:53 -0700598 if (mWebViewController.shouldShowErrorConsole()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700599 mErrorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
600 }
601 }
602
Patrick Scott92066772011-03-10 08:46:27 -0500603 // Cancel the auto-login process.
604 if (mDeviceAccountLogin != null) {
605 mDeviceAccountLogin.cancel();
606 mDeviceAccountLogin = null;
607 mWebViewController.hideAutoLogin(Tab.this);
608 }
609
Grace Kloba22ac16e2009-10-07 18:00:23 -0700610 // finally update the UI in the activity if it is in the foreground
John Reck324d4402011-01-11 16:56:42 -0800611 mWebViewController.onPageStarted(Tab.this, view, favicon);
Leon Scroggins4cd97792010-12-03 15:31:56 -0500612
John Recke969cc52010-12-21 17:24:43 -0800613 updateBookmarkedStatus();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700614 }
615
616 @Override
617 public void onPageFinished(WebView view, String url) {
John Reck5b691842010-11-29 11:21:13 -0800618 if (!isPrivateBrowsingEnabled()) {
619 LogTag.logPageFinishedLoading(
620 url, SystemClock.uptimeMillis() - mLoadStartTime);
621 }
John Reck1cf4b792011-07-26 10:22:22 -0700622 syncCurrentState(view, url);
John Reck324d4402011-01-11 16:56:42 -0800623 mWebViewController.onPageFinished(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700624 }
625
626 // return true if want to hijack the url to let another app to handle it
627 @Override
628 public boolean shouldOverrideUrlLoading(WebView view, String url) {
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400629 if (voiceSearchSourceIsGoogle()) {
630 // This method is called when the user clicks on a link.
631 // VoiceSearchMode is turned off when the user leaves the
632 // Google results page, so at this point the user must be on
633 // that page. If the user clicked a link on that page, assume
634 // that the voice search was effective, and broadcast an Intent
635 // so a receiver can take note of that fact.
636 Intent logIntent = new Intent(LoggingEvents.ACTION_LOG_EVENT);
637 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
638 LoggingEvents.VoiceSearch.RESULT_CLICKED);
Michael Kolb14612442011-06-24 13:06:29 -0700639 mContext.sendBroadcast(logIntent);
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400640 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700641 if (mInForeground) {
Michael Kolb18eb3772010-12-10 14:29:51 -0800642 return mWebViewController.shouldOverrideUrlLoading(Tab.this,
643 view, url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700644 } else {
645 return false;
646 }
647 }
648
649 /**
Steve Block2466eff2011-10-03 15:33:09 +0100650 * Updates the security state. This method is called when we discover
651 * another resource to be loaded for this page (for example,
652 * javascript). While we update the security state, we do not update
653 * the lock icon until we are done loading, as it is slightly more
654 * secure this way.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700655 */
656 @Override
657 public void onLoadResource(WebView view, String url) {
658 if (url != null && url.length() > 0) {
659 // It is only if the page claims to be secure that we may have
Steve Block2466eff2011-10-03 15:33:09 +0100660 // to update the security state:
661 if (mCurrentState.mSecurityState == SecurityState.SECURITY_STATE_SECURE) {
662 // If NOT a 'safe' url, change the state to mixed content!
Grace Kloba22ac16e2009-10-07 18:00:23 -0700663 if (!(URLUtil.isHttpsUrl(url) || URLUtil.isDataUrl(url)
664 || URLUtil.isAboutUrl(url))) {
Steve Block2466eff2011-10-03 15:33:09 +0100665 mCurrentState.mSecurityState = SecurityState.SECURITY_STATE_MIXED;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700666 }
667 }
668 }
669 }
670
671 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700672 * Show a dialog informing the user of the network error reported by
673 * WebCore if it is in the foreground.
674 */
675 @Override
676 public void onReceivedError(WebView view, int errorCode,
677 String description, String failingUrl) {
678 if (errorCode != WebViewClient.ERROR_HOST_LOOKUP &&
679 errorCode != WebViewClient.ERROR_CONNECT &&
680 errorCode != WebViewClient.ERROR_BAD_URL &&
681 errorCode != WebViewClient.ERROR_UNSUPPORTED_SCHEME &&
682 errorCode != WebViewClient.ERROR_FILE) {
683 queueError(errorCode, description);
Jeff Hamilton47654f42010-09-07 09:57:51 -0500684
Selim Gurun3da06b82011-10-10 13:58:12 -0700685 // Don't log URLs when in private browsing mode
686 if (!isPrivateBrowsingEnabled()) {
687 Log.e(LOGTAG, "onReceivedError " + errorCode + " " + failingUrl
Jeff Hamilton47654f42010-09-07 09:57:51 -0500688 + " " + description);
Selim Gurun3da06b82011-10-10 13:58:12 -0700689 }
Jeff Hamilton47654f42010-09-07 09:57:51 -0500690 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700691 }
692
693 /**
694 * Check with the user if it is ok to resend POST data as the page they
695 * are trying to navigate to is the result of a POST.
696 */
697 @Override
698 public void onFormResubmission(WebView view, final Message dontResend,
699 final Message resend) {
700 if (!mInForeground) {
701 dontResend.sendToTarget();
702 return;
703 }
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500704 if (mDontResend != null) {
705 Log.w(LOGTAG, "onFormResubmission should not be called again "
706 + "while dialog is still up");
707 dontResend.sendToTarget();
708 return;
709 }
710 mDontResend = dontResend;
711 mResend = resend;
Michael Kolb14612442011-06-24 13:06:29 -0700712 new AlertDialog.Builder(mContext).setTitle(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700713 R.string.browserFrameFormResubmitLabel).setMessage(
714 R.string.browserFrameFormResubmitMessage)
715 .setPositiveButton(R.string.ok,
716 new DialogInterface.OnClickListener() {
717 public void onClick(DialogInterface dialog,
718 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500719 if (mResend != null) {
720 mResend.sendToTarget();
721 mResend = null;
722 mDontResend = null;
723 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700724 }
725 }).setNegativeButton(R.string.cancel,
726 new DialogInterface.OnClickListener() {
727 public void onClick(DialogInterface dialog,
728 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500729 if (mDontResend != null) {
730 mDontResend.sendToTarget();
731 mResend = null;
732 mDontResend = null;
733 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700734 }
735 }).setOnCancelListener(new OnCancelListener() {
736 public void onCancel(DialogInterface dialog) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500737 if (mDontResend != null) {
738 mDontResend.sendToTarget();
739 mResend = null;
740 mDontResend = null;
741 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700742 }
743 }).show();
744 }
745
746 /**
747 * Insert the url into the visited history database.
748 * @param url The url to be inserted.
749 * @param isReload True if this url is being reloaded.
750 * FIXME: Not sure what to do when reloading the page.
751 */
752 @Override
753 public void doUpdateVisitedHistory(WebView view, String url,
754 boolean isReload) {
John Reck324d4402011-01-11 16:56:42 -0800755 mWebViewController.doUpdateVisitedHistory(Tab.this, isReload);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700756 }
757
758 /**
759 * Displays SSL error(s) dialog to the user.
760 */
761 @Override
762 public void onReceivedSslError(final WebView view,
763 final SslErrorHandler handler, final SslError error) {
764 if (!mInForeground) {
765 handler.cancel();
Steve Block2466eff2011-10-03 15:33:09 +0100766 setSecurityState(SecurityState.SECURITY_STATE_NOT_SECURE);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700767 return;
768 }
John Reck35e9dd62011-04-25 09:01:54 -0700769 if (mSettings.showSecurityWarnings()) {
John Reckcb28b2c2011-08-26 17:39:44 -0700770 new AlertDialog.Builder(mContext)
771 .setTitle(R.string.security_warning)
772 .setMessage(R.string.ssl_warnings_header)
773 .setIcon(android.R.drawable.ic_dialog_alert)
774 .setPositiveButton(R.string.ssl_continue,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700775 new DialogInterface.OnClickListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700776 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700777 public void onClick(DialogInterface dialog,
778 int whichButton) {
779 handler.proceed();
Steve Block4895b012011-10-03 16:26:46 +0100780 handleProceededAfterSslError(error);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700781 }
John Reckcb28b2c2011-08-26 17:39:44 -0700782 })
783 .setNeutralButton(R.string.view_certificate,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700784 new DialogInterface.OnClickListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700785 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700786 public void onClick(DialogInterface dialog,
787 int whichButton) {
John Reckcb28b2c2011-08-26 17:39:44 -0700788 mWebViewController.showSslCertificateOnError(
789 view, handler, error);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700790 }
John Reckcb28b2c2011-08-26 17:39:44 -0700791 })
792 .setNegativeButton(R.string.ssl_go_back,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700793 new DialogInterface.OnClickListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700794 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700795 public void onClick(DialogInterface dialog,
796 int whichButton) {
John Reck30c714c2010-12-16 17:30:34 -0800797 dialog.cancel();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700798 }
John Reckcb28b2c2011-08-26 17:39:44 -0700799 })
800 .setOnCancelListener(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700801 new DialogInterface.OnCancelListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700802 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700803 public void onCancel(DialogInterface dialog) {
804 handler.cancel();
Steve Block2466eff2011-10-03 15:33:09 +0100805 setSecurityState(SecurityState.SECURITY_STATE_NOT_SECURE);
John Reck30c714c2010-12-16 17:30:34 -0800806 mWebViewController.onUserCanceledSsl(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700807 }
John Reckcb28b2c2011-08-26 17:39:44 -0700808 })
809 .show();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700810 } else {
811 handler.proceed();
812 }
813 }
814
815 /**
Steve Block4895b012011-10-03 16:26:46 +0100816 * Called when an SSL error occurred while loading a resource, but the
817 * WebView but chose to proceed anyway based on a decision retained
818 * from a previous response to onReceivedSslError(). We update our
819 * security state to reflect this.
820 */
821 @Override
822 public void onProceededAfterSslError(WebView view, SslError error) {
823 handleProceededAfterSslError(error);
824 }
825
826 /**
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700827 * Displays client certificate request to the user.
828 */
829 @Override
830 public void onReceivedClientCertRequest(final WebView view,
831 final ClientCertRequestHandler handler, final String host_and_port) {
832 if (!mInForeground) {
833 handler.ignore();
834 return;
835 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700836 int colon = host_and_port.lastIndexOf(':');
837 String host;
838 int port;
839 if (colon == -1) {
840 host = host_and_port;
841 port = -1;
842 } else {
843 String portString = host_and_port.substring(colon + 1);
844 try {
845 port = Integer.parseInt(portString);
846 host = host_and_port.substring(0, colon);
847 } catch (NumberFormatException e) {
848 host = host_and_port;
849 port = -1;
850 }
851 }
Michael Kolb14612442011-06-24 13:06:29 -0700852 KeyChain.choosePrivateKeyAlias(
853 mWebViewController.getActivity(), new KeyChainAliasCallback() {
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700854 @Override public void alias(String alias) {
855 if (alias == null) {
856 handler.cancel();
857 return;
858 }
Michael Kolb14612442011-06-24 13:06:29 -0700859 new KeyChainLookup(mContext, handler, alias).execute();
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700860 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700861 }, null, null, host, port, null);
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700862 }
863
864 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700865 * Handles an HTTP authentication request.
866 *
867 * @param handler The authentication handler
868 * @param host The host
869 * @param realm The realm
870 */
871 @Override
872 public void onReceivedHttpAuthRequest(WebView view,
873 final HttpAuthHandler handler, final String host,
874 final String realm) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700875 mWebViewController.onReceivedHttpAuthRequest(Tab.this, view, handler, host, realm);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700876 }
877
878 @Override
John Reck438bf462011-01-12 18:11:46 -0800879 public WebResourceResponse shouldInterceptRequest(WebView view,
880 String url) {
881 WebResourceResponse res = HomeProvider.shouldInterceptRequest(
Michael Kolb14612442011-06-24 13:06:29 -0700882 mContext, url);
John Reck438bf462011-01-12 18:11:46 -0800883 return res;
884 }
885
886 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700887 public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
888 if (!mInForeground) {
889 return false;
890 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700891 return mWebViewController.shouldOverrideKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700892 }
893
894 @Override
895 public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700896 if (!mInForeground) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700897 return;
898 }
John Reck997b1b72012-04-19 18:08:25 -0700899 if (!mWebViewController.onUnhandledKeyEvent(event)) {
900 super.onUnhandledKeyEvent(view, event);
901 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700902 }
Patrick Scott92066772011-03-10 08:46:27 -0500903
904 @Override
905 public void onReceivedLoginRequest(WebView view, String realm,
906 String account, String args) {
Michael Kolb14612442011-06-24 13:06:29 -0700907 new DeviceAccountLogin(mWebViewController.getActivity(), view, Tab.this, mWebViewController)
Patrick Scott92066772011-03-10 08:46:27 -0500908 .handleLogin(realm, account, args);
909 }
910
Grace Kloba22ac16e2009-10-07 18:00:23 -0700911 };
912
John Reck1cf4b792011-07-26 10:22:22 -0700913 private void syncCurrentState(WebView view, String url) {
914 // Sync state (in case of stop/timeout)
915 mCurrentState.mUrl = view.getUrl();
916 if (mCurrentState.mUrl == null) {
John Reck8bcafc12011-08-29 16:43:02 -0700917 mCurrentState.mUrl = "";
John Reck1cf4b792011-07-26 10:22:22 -0700918 }
919 mCurrentState.mOriginalUrl = view.getOriginalUrl();
920 mCurrentState.mTitle = view.getTitle();
921 mCurrentState.mFavicon = view.getFavicon();
922 if (!URLUtil.isHttpsUrl(mCurrentState.mUrl)) {
923 // In case we stop when loading an HTTPS page from an HTTP page
924 // but before a provisional load occurred
Steve Block2466eff2011-10-03 15:33:09 +0100925 mCurrentState.mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
Steve Block08a6f0c2011-10-06 12:12:53 +0100926 mCurrentState.mSslCertificateError = null;
John Reck1cf4b792011-07-26 10:22:22 -0700927 }
John Reck502a3532011-08-16 14:21:46 -0700928 mCurrentState.mIncognito = view.isPrivateBrowsingEnabled();
John Reck1cf4b792011-07-26 10:22:22 -0700929 }
930
Patrick Scott92066772011-03-10 08:46:27 -0500931 // Called by DeviceAccountLogin when the Tab needs to have the auto-login UI
932 // displayed.
933 void setDeviceAccountLogin(DeviceAccountLogin login) {
934 mDeviceAccountLogin = login;
935 }
936
937 // Returns non-null if the title bar should display the auto-login UI.
938 DeviceAccountLogin getDeviceAccountLogin() {
939 return mDeviceAccountLogin;
940 }
941
Grace Kloba22ac16e2009-10-07 18:00:23 -0700942 // -------------------------------------------------------------------------
943 // WebChromeClient implementation for the main WebView
944 // -------------------------------------------------------------------------
945
946 private final WebChromeClient mWebChromeClient = new WebChromeClient() {
947 // Helper method to create a new tab or sub window.
948 private void createWindow(final boolean dialog, final Message msg) {
949 WebView.WebViewTransport transport =
950 (WebView.WebViewTransport) msg.obj;
951 if (dialog) {
952 createSubWindow();
Michael Kolb8233fac2010-10-26 16:08:53 -0700953 mWebViewController.attachSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700954 transport.setWebView(mSubView);
955 } else {
Michael Kolb7bcafde2011-05-09 13:55:59 -0700956 final Tab newTab = mWebViewController.openTab(null,
John Reck5949c662011-05-27 09:52:29 -0700957 Tab.this, true, true);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700958 transport.setWebView(newTab.getWebView());
959 }
960 msg.sendToTarget();
961 }
962
963 @Override
964 public boolean onCreateWindow(WebView view, final boolean dialog,
965 final boolean userGesture, final Message resultMsg) {
966 // only allow new window or sub window for the foreground case
967 if (!mInForeground) {
968 return false;
969 }
970 // Short-circuit if we can't create any more tabs or sub windows.
971 if (dialog && mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -0700972 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700973 .setTitle(R.string.too_many_subwindows_dialog_title)
974 .setIcon(android.R.drawable.ic_dialog_alert)
975 .setMessage(R.string.too_many_subwindows_dialog_message)
976 .setPositiveButton(R.string.ok, null)
977 .show();
978 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700979 } else if (!mWebViewController.getTabControl().canCreateNewTab()) {
Michael Kolb14612442011-06-24 13:06:29 -0700980 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700981 .setTitle(R.string.too_many_windows_dialog_title)
982 .setIcon(android.R.drawable.ic_dialog_alert)
983 .setMessage(R.string.too_many_windows_dialog_message)
984 .setPositiveButton(R.string.ok, null)
985 .show();
986 return false;
987 }
988
989 // Short-circuit if this was a user gesture.
990 if (userGesture) {
991 createWindow(dialog, resultMsg);
992 return true;
993 }
994
995 // Allow the popup and create the appropriate window.
996 final AlertDialog.OnClickListener allowListener =
997 new AlertDialog.OnClickListener() {
998 public void onClick(DialogInterface d,
999 int which) {
1000 createWindow(dialog, resultMsg);
1001 }
1002 };
1003
1004 // Block the popup by returning a null WebView.
1005 final AlertDialog.OnClickListener blockListener =
1006 new AlertDialog.OnClickListener() {
1007 public void onClick(DialogInterface d, int which) {
1008 resultMsg.sendToTarget();
1009 }
1010 };
1011
1012 // Build a confirmation dialog to display to the user.
1013 final AlertDialog d =
Michael Kolb14612442011-06-24 13:06:29 -07001014 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -07001015 .setIcon(android.R.drawable.ic_dialog_alert)
1016 .setMessage(R.string.popup_window_attempt)
1017 .setPositiveButton(R.string.allow, allowListener)
1018 .setNegativeButton(R.string.block, blockListener)
1019 .setCancelable(false)
1020 .create();
1021
1022 // Show the confirmation dialog.
1023 d.show();
1024 return true;
1025 }
1026
1027 @Override
Patrick Scotteb5061b2009-11-18 15:00:30 -05001028 public void onRequestFocus(WebView view) {
1029 if (!mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -07001030 mWebViewController.switchToTab(Tab.this);
Patrick Scotteb5061b2009-11-18 15:00:30 -05001031 }
1032 }
1033
1034 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -07001035 public void onCloseWindow(WebView window) {
Michael Kolbc831b632011-05-11 09:30:34 -07001036 if (mParent != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001037 // JavaScript can only close popup window.
1038 if (mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -07001039 mWebViewController.switchToTab(mParent);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001040 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001041 mWebViewController.closeTab(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001042 }
1043 }
1044
1045 @Override
1046 public void onProgressChanged(WebView view, int newProgress) {
John Reck30c714c2010-12-16 17:30:34 -08001047 mPageLoadProgress = newProgress;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001048 if (newProgress == 100) {
1049 mInPageLoad = false;
1050 }
John Reck30c714c2010-12-16 17:30:34 -08001051 mWebViewController.onProgressChanged(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001052 }
1053
1054 @Override
Leon Scroggins21d9b902010-03-11 09:33:11 -05001055 public void onReceivedTitle(WebView view, final String title) {
John Reck30c714c2010-12-16 17:30:34 -08001056 mCurrentState.mTitle = title;
Michael Kolb8233fac2010-10-26 16:08:53 -07001057 mWebViewController.onReceivedTitle(Tab.this, title);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001058 }
1059
1060 @Override
1061 public void onReceivedIcon(WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -08001062 mCurrentState.mFavicon = icon;
Michael Kolb8233fac2010-10-26 16:08:53 -07001063 mWebViewController.onFavicon(Tab.this, view, icon);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001064 }
1065
1066 @Override
1067 public void onReceivedTouchIconUrl(WebView view, String url,
1068 boolean precomposed) {
Michael Kolb14612442011-06-24 13:06:29 -07001069 final ContentResolver cr = mContext.getContentResolver();
Leon Scrogginsc8393d92010-04-23 14:58:16 -04001070 // Let precomposed icons take precedence over non-composed
1071 // icons.
1072 if (precomposed && mTouchIconLoader != null) {
1073 mTouchIconLoader.cancel(false);
1074 mTouchIconLoader = null;
1075 }
1076 // Have only one async task at a time.
1077 if (mTouchIconLoader == null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001078 mTouchIconLoader = new DownloadTouchIcon(Tab.this,
Michael Kolb14612442011-06-24 13:06:29 -07001079 mContext, cr, view);
Leon Scrogginsc8393d92010-04-23 14:58:16 -04001080 mTouchIconLoader.execute(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001081 }
1082 }
1083
1084 @Override
1085 public void onShowCustomView(View view,
1086 WebChromeClient.CustomViewCallback callback) {
Michael Kolb14612442011-06-24 13:06:29 -07001087 Activity activity = mWebViewController.getActivity();
1088 if (activity != null) {
1089 onShowCustomView(view, activity.getRequestedOrientation(), callback);
1090 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001091 }
1092
1093 @Override
1094 public void onShowCustomView(View view, int requestedOrientation,
1095 WebChromeClient.CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001096 if (mInForeground) mWebViewController.showCustomView(Tab.this, view,
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001097 requestedOrientation, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001098 }
1099
1100 @Override
1101 public void onHideCustomView() {
Michael Kolb8233fac2010-10-26 16:08:53 -07001102 if (mInForeground) mWebViewController.hideCustomView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001103 }
1104
1105 /**
1106 * The origin has exceeded its database quota.
1107 * @param url the URL that exceeded the quota
1108 * @param databaseIdentifier the identifier of the database on which the
1109 * transaction that caused the quota overflow was run
1110 * @param currentQuota the current quota for the origin.
1111 * @param estimatedSize the estimated size of the database.
1112 * @param totalUsedQuota is the sum of all origins' quota.
1113 * @param quotaUpdater The callback to run when a decision to allow or
1114 * deny quota has been made. Don't forget to call this!
1115 */
1116 @Override
1117 public void onExceededDatabaseQuota(String url,
1118 String databaseIdentifier, long currentQuota, long estimatedSize,
1119 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -07001120 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001121 .onExceededDatabaseQuota(url, databaseIdentifier,
1122 currentQuota, estimatedSize, totalUsedQuota,
1123 quotaUpdater);
1124 }
1125
1126 /**
1127 * The Application Cache has exceeded its max size.
1128 * @param spaceNeeded is the amount of disk space that would be needed
1129 * in order for the last appcache operation to succeed.
1130 * @param totalUsedQuota is the sum of all origins' quota.
1131 * @param quotaUpdater A callback to inform the WebCore thread that a
1132 * new app cache size is available. This callback must always
1133 * be executed at some point to ensure that the sleeping
1134 * WebCore thread is woken up.
1135 */
1136 @Override
1137 public void onReachedMaxAppCacheSize(long spaceNeeded,
1138 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -07001139 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001140 .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota,
1141 quotaUpdater);
1142 }
1143
1144 /**
1145 * Instructs the browser to show a prompt to ask the user to set the
1146 * Geolocation permission state for the specified origin.
1147 * @param origin The origin for which Geolocation permissions are
1148 * requested.
1149 * @param callback The callback to call once the user has set the
1150 * Geolocation permission state.
1151 */
1152 @Override
1153 public void onGeolocationPermissionsShowPrompt(String origin,
1154 GeolocationPermissions.Callback callback) {
1155 if (mInForeground) {
Grace Kloba50c241e2010-04-20 11:07:50 -07001156 getGeolocationPermissionsPrompt().show(origin, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001157 }
1158 }
1159
1160 /**
1161 * Instructs the browser to hide the Geolocation permissions prompt.
1162 */
1163 @Override
1164 public void onGeolocationPermissionsHidePrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001165 if (mInForeground && mGeolocationPermissionsPrompt != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001166 mGeolocationPermissionsPrompt.hide();
1167 }
1168 }
1169
Ben Murdoch65acc352009-11-19 18:16:04 +00001170 /* Adds a JavaScript error message to the system log and if the JS
1171 * console is enabled in the about:debug options, to that console
1172 * also.
Ben Murdochc42addf2010-01-28 15:19:59 +00001173 * @param consoleMessage the message object.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001174 */
1175 @Override
Ben Murdochc42addf2010-01-28 15:19:59 +00001176 public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001177 if (mInForeground) {
1178 // call getErrorConsole(true) so it will create one if needed
1179 ErrorConsoleView errorConsole = getErrorConsole(true);
Ben Murdochc42addf2010-01-28 15:19:59 +00001180 errorConsole.addErrorMessage(consoleMessage);
Michael Kolb8233fac2010-10-26 16:08:53 -07001181 if (mWebViewController.shouldShowErrorConsole()
1182 && errorConsole.getShowState() !=
1183 ErrorConsoleView.SHOW_MAXIMIZED) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001184 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
1185 }
1186 }
Ben Murdochc42addf2010-01-28 15:19:59 +00001187
Jeff Hamilton47654f42010-09-07 09:57:51 -05001188 // Don't log console messages in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001189 if (isPrivateBrowsingEnabled()) return true;
Jeff Hamilton47654f42010-09-07 09:57:51 -05001190
Ben Murdochc42addf2010-01-28 15:19:59 +00001191 String message = "Console: " + consoleMessage.message() + " "
1192 + consoleMessage.sourceId() + ":"
1193 + consoleMessage.lineNumber();
1194
1195 switch (consoleMessage.messageLevel()) {
1196 case TIP:
1197 Log.v(CONSOLE_LOGTAG, message);
1198 break;
1199 case LOG:
1200 Log.i(CONSOLE_LOGTAG, message);
1201 break;
1202 case WARNING:
1203 Log.w(CONSOLE_LOGTAG, message);
1204 break;
1205 case ERROR:
1206 Log.e(CONSOLE_LOGTAG, message);
1207 break;
1208 case DEBUG:
1209 Log.d(CONSOLE_LOGTAG, message);
1210 break;
1211 }
1212
1213 return true;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001214 }
1215
1216 /**
1217 * Ask the browser for an icon to represent a <video> element.
1218 * This icon will be used if the Web page did not specify a poster attribute.
1219 * @return Bitmap The icon or null if no such icon is available.
1220 */
1221 @Override
1222 public Bitmap getDefaultVideoPoster() {
1223 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001224 return mWebViewController.getDefaultVideoPoster();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001225 }
1226 return null;
1227 }
1228
1229 /**
1230 * Ask the host application for a custom progress view to show while
1231 * a <video> is loading.
1232 * @return View The progress view.
1233 */
1234 @Override
1235 public View getVideoLoadingProgressView() {
1236 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001237 return mWebViewController.getVideoLoadingProgressView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001238 }
1239 return null;
1240 }
1241
1242 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00001243 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001244 if (mInForeground) {
Ben Murdoch8cad4132012-01-11 10:56:43 +00001245 mWebViewController.openFileChooser(uploadMsg, acceptType, capture);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001246 } else {
1247 uploadMsg.onReceiveValue(null);
1248 }
1249 }
1250
1251 /**
1252 * Deliver a list of already-visited URLs
1253 */
1254 @Override
1255 public void getVisitedHistory(final ValueCallback<String[]> callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001256 mWebViewController.getVisitedHistory(callback);
1257 }
Ben Murdoch8029a772010-11-16 11:58:21 +00001258
1259 @Override
1260 public void setupAutoFill(Message message) {
1261 // Prompt the user to set up their profile.
1262 final Message msg = message;
Michael Kolb14612442011-06-24 13:06:29 -07001263 AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
1264 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
Ben Murdoch1d676b62011-01-17 12:54:24 +00001265 Context.LAYOUT_INFLATER_SERVICE);
1266 final View layout = inflater.inflate(R.layout.setup_autofill_dialog, null);
1267
1268 builder.setView(layout)
1269 .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
Michael Kolb91911a22012-01-17 11:21:25 -08001511 void setWebView(WebView w) {
1512 setWebView(w, true);
1513 }
1514
Grace Kloba22ac16e2009-10-07 18:00:23 -07001515 /**
1516 * Sets the WebView for this tab, correctly removing the old WebView from
1517 * the container view.
1518 */
Michael Kolb91911a22012-01-17 11:21:25 -08001519 void setWebView(WebView w, boolean restore) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001520 if (mMainView == w) {
1521 return;
1522 }
Michael Kolba713ec82010-11-29 17:27:06 -08001523
Grace Kloba22ac16e2009-10-07 18:00:23 -07001524 // If the WebView is changing, the page will be reloaded, so any ongoing
1525 // Geolocation permission requests are void.
Grace Kloba50c241e2010-04-20 11:07:50 -07001526 if (mGeolocationPermissionsPrompt != null) {
1527 mGeolocationPermissionsPrompt.hide();
1528 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001529
Michael Kolba713ec82010-11-29 17:27:06 -08001530 mWebViewController.onSetWebView(this, w);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001531
John Reck1cf4b792011-07-26 10:22:22 -07001532 if (mMainView != null) {
John Reck8ee633f2011-08-09 16:00:35 -07001533 mMainView.setPictureListener(null);
John Reck1cf4b792011-07-26 10:22:22 -07001534 if (w != null) {
1535 syncCurrentState(w, null);
1536 } else {
1537 mCurrentState = new PageState(mContext, false);
1538 }
1539 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001540 // set the new one
1541 mMainView = w;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001542 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001543 if (mMainView != null) {
1544 mMainView.setWebViewClient(mWebViewClient);
1545 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001546 // Attach DownloadManager so that downloads can start in an active
1547 // or a non-active window. This can happen when going to a site that
1548 // does a redirect after a period of time. The user could have
1549 // switched to another tab while waiting for the download to start.
1550 mMainView.setDownloadListener(mDownloadListener);
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00001551 getWebViewClassic().setWebBackForwardListClient(mWebBackForwardListClient);
John Reck8ee633f2011-08-09 16:00:35 -07001552 TabControl tc = mWebViewController.getTabControl();
1553 if (tc != null && tc.getOnThumbnailUpdatedListener() != null) {
1554 mMainView.setPictureListener(this);
1555 }
Michael Kolb91911a22012-01-17 11:21:25 -08001556 if (restore && (mSavedState != null)) {
John Reck8b9bb8b2012-03-08 13:19:40 -08001557 restoreUserAgent();
John Reck6c2e2f32011-08-22 13:41:23 -07001558 WebBackForwardList restoredState
1559 = mMainView.restoreState(mSavedState);
1560 if (restoredState == null || restoredState.getSize() == 0) {
1561 Log.w(LOGTAG, "Failed to restore WebView state!");
1562 loadUrl(mCurrentState.mOriginalUrl, null);
1563 }
John Reck1cf4b792011-07-26 10:22:22 -07001564 mSavedState = null;
1565 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001566 }
1567 }
1568
1569 /**
1570 * Destroy the tab's main WebView and subWindow if any
1571 */
1572 void destroy() {
1573 if (mMainView != null) {
1574 dismissSubWindow();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001575 // save the WebView to call destroy() after detach it from the tab
1576 WebView webView = mMainView;
1577 setWebView(null);
1578 webView.destroy();
1579 }
1580 }
1581
1582 /**
1583 * Remove the tab from the parent
1584 */
1585 void removeFromTree() {
1586 // detach the children
Michael Kolbc831b632011-05-11 09:30:34 -07001587 if (mChildren != null) {
1588 for(Tab t : mChildren) {
1589 t.setParent(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001590 }
1591 }
1592 // remove itself from the parent list
Michael Kolbc831b632011-05-11 09:30:34 -07001593 if (mParent != null) {
1594 mParent.mChildren.remove(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001595 }
John Reck1cf4b792011-07-26 10:22:22 -07001596 deleteThumbnail();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001597 }
1598
1599 /**
1600 * Create a new subwindow unless a subwindow already exists.
1601 * @return True if a new subwindow was created. False if one already exists.
1602 */
1603 boolean createSubWindow() {
1604 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001605 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001606 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001607 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001608 mSubView.setWebChromeClient(new SubWindowChromeClient(
1609 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001610 // Set a different DownloadListener for the mSubView, since it will
1611 // just need to dismiss the mSubView, rather than close the Tab
1612 mSubView.setDownloadListener(new DownloadListener() {
1613 public void onDownloadStart(String url, String userAgent,
1614 String contentDisposition, String mimetype,
1615 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001616 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001617 contentDisposition, mimetype, contentLength);
1618 if (mSubView.copyBackForwardList().getSize() == 0) {
1619 // This subwindow was opened for the sole purpose of
1620 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001621 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001622 }
1623 }
1624 });
Michael Kolb14612442011-06-24 13:06:29 -07001625 mSubView.setOnCreateContextMenuListener(mWebViewController.getActivity());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001626 return true;
1627 }
1628 return false;
1629 }
1630
1631 /**
1632 * Dismiss the subWindow for the tab.
1633 */
1634 void dismissSubWindow() {
1635 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001636 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001637 mSubView.destroy();
1638 mSubView = null;
1639 mSubViewContainer = null;
1640 }
1641 }
1642
Grace Kloba22ac16e2009-10-07 18:00:23 -07001643
1644 /**
1645 * Set the parent tab of this tab.
1646 */
Michael Kolbc831b632011-05-11 09:30:34 -07001647 void setParent(Tab parent) {
John Reck52be4782011-08-26 15:37:29 -07001648 if (parent == this) {
1649 throw new IllegalStateException("Cannot set parent to self!");
1650 }
Michael Kolbc831b632011-05-11 09:30:34 -07001651 mParent = parent;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001652 // This tab may have been freed due to low memory. If that is the case,
Michael Kolbc831b632011-05-11 09:30:34 -07001653 // the parent tab id is already saved. If we are changing that id
Grace Kloba22ac16e2009-10-07 18:00:23 -07001654 // (most likely due to removing the parent tab) we must update the
Michael Kolbc831b632011-05-11 09:30:34 -07001655 // parent tab id in the saved Bundle.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001656 if (mSavedState != null) {
1657 if (parent == null) {
1658 mSavedState.remove(PARENTTAB);
1659 } else {
Michael Kolbc831b632011-05-11 09:30:34 -07001660 mSavedState.putLong(PARENTTAB, parent.getId());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001661 }
1662 }
John Reckb0a86db2011-05-24 14:05:58 -07001663
1664 // Sync the WebView useragent with the parent
1665 if (parent != null && mSettings.hasDesktopUseragent(parent.getWebView())
1666 != mSettings.hasDesktopUseragent(getWebView())) {
1667 mSettings.toggleDesktopUseragent(getWebView());
1668 }
John Reck52be4782011-08-26 15:37:29 -07001669
1670 if (parent != null && parent.getId() == getId()) {
1671 throw new IllegalStateException("Parent has same ID as child!");
1672 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001673 }
1674
1675 /**
Michael Kolbc831b632011-05-11 09:30:34 -07001676 * If this Tab was created through another Tab, then this method returns
1677 * that Tab.
1678 * @return the Tab parent or null
1679 */
1680 public Tab getParent() {
1681 return mParent;
1682 }
1683
1684 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001685 * When a Tab is created through the content of another Tab, then we
1686 * associate the Tabs.
1687 * @param child the Tab that was created from this Tab
1688 */
1689 void addChildTab(Tab child) {
Michael Kolbc831b632011-05-11 09:30:34 -07001690 if (mChildren == null) {
1691 mChildren = new Vector<Tab>();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001692 }
Michael Kolbc831b632011-05-11 09:30:34 -07001693 mChildren.add(child);
1694 child.setParent(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001695 }
1696
Michael Kolbc831b632011-05-11 09:30:34 -07001697 Vector<Tab> getChildren() {
1698 return mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001699 }
1700
1701 void resume() {
1702 if (mMainView != null) {
John Reck56c1fcf2011-08-17 10:15:16 -07001703 setupHwAcceleration(mMainView);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001704 mMainView.onResume();
1705 if (mSubView != null) {
1706 mSubView.onResume();
1707 }
1708 }
1709 }
1710
John Reck56c1fcf2011-08-17 10:15:16 -07001711 private void setupHwAcceleration(View web) {
1712 if (web == null) return;
1713 BrowserSettings settings = BrowserSettings.getInstance();
1714 if (settings.isHardwareAccelerated()) {
1715 web.setLayerType(View.LAYER_TYPE_NONE, null);
1716 } else {
1717 web.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
1718 }
1719 }
1720
Grace Kloba22ac16e2009-10-07 18:00:23 -07001721 void pause() {
1722 if (mMainView != null) {
1723 mMainView.onPause();
1724 if (mSubView != null) {
1725 mSubView.onPause();
1726 }
1727 }
1728 }
1729
1730 void putInForeground() {
John Reck8ee633f2011-08-09 16:00:35 -07001731 if (mInForeground) {
1732 return;
1733 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001734 mInForeground = true;
1735 resume();
Michael Kolb14612442011-06-24 13:06:29 -07001736 Activity activity = mWebViewController.getActivity();
1737 mMainView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001738 if (mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -07001739 mSubView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001740 }
1741 // Show the pending error dialog if the queue is not empty
1742 if (mQueuedErrors != null && mQueuedErrors.size() > 0) {
1743 showError(mQueuedErrors.getFirst());
1744 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001745 mWebViewController.bookmarkedStatusHasChanged(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001746 }
1747
1748 void putInBackground() {
John Reck8ee633f2011-08-09 16:00:35 -07001749 if (!mInForeground) {
1750 return;
1751 }
John Reck52be4782011-08-26 15:37:29 -07001752 capture();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001753 mInForeground = false;
1754 pause();
1755 mMainView.setOnCreateContextMenuListener(null);
1756 if (mSubView != null) {
1757 mSubView.setOnCreateContextMenuListener(null);
1758 }
1759 }
1760
Michael Kolb8233fac2010-10-26 16:08:53 -07001761 boolean inForeground() {
1762 return mInForeground;
1763 }
1764
Grace Kloba22ac16e2009-10-07 18:00:23 -07001765 /**
1766 * Return the top window of this tab; either the subwindow if it is not
1767 * null or the main window.
1768 * @return The top window of this tab.
1769 */
1770 WebView getTopWindow() {
1771 if (mSubView != null) {
1772 return mSubView;
1773 }
1774 return mMainView;
1775 }
1776
1777 /**
1778 * Return the main window of this tab. Note: if a tab is freed in the
1779 * background, this can return null. It is only guaranteed to be
1780 * non-null for the current tab.
1781 * @return The main WebView of this tab.
1782 */
1783 WebView getWebView() {
1784 return mMainView;
1785 }
1786
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00001787 /**
1788 * Return the underlying WebViewClassic implementation. As with getWebView,
1789 * this maybe null for background tabs.
1790 * @return The main WebView of this tab.
1791 */
1792 WebViewClassic getWebViewClassic() {
1793 return WebViewClassic.fromWebView(mMainView);
1794 }
1795
Michael Kolba713ec82010-11-29 17:27:06 -08001796 void setViewContainer(View container) {
1797 mContainer = container;
1798 }
1799
Michael Kolb8233fac2010-10-26 16:08:53 -07001800 View getViewContainer() {
1801 return mContainer;
1802 }
1803
Grace Kloba22ac16e2009-10-07 18:00:23 -07001804 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001805 * Return whether private browsing is enabled for the main window of
1806 * this tab.
1807 * @return True if private browsing is enabled.
1808 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001809 boolean isPrivateBrowsingEnabled() {
John Reck502a3532011-08-16 14:21:46 -07001810 return mCurrentState.mIncognito;
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001811 }
1812
1813 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001814 * Return the subwindow of this tab or null if there is no subwindow.
1815 * @return The subwindow of this tab or null.
1816 */
1817 WebView getSubWebView() {
1818 return mSubView;
1819 }
1820
Michael Kolb1514bb72010-11-22 09:11:48 -08001821 void setSubWebView(WebView subView) {
1822 mSubView = subView;
1823 }
1824
Michael Kolb8233fac2010-10-26 16:08:53 -07001825 View getSubViewContainer() {
1826 return mSubViewContainer;
1827 }
1828
Michael Kolb1514bb72010-11-22 09:11:48 -08001829 void setSubViewContainer(View subViewContainer) {
1830 mSubViewContainer = subViewContainer;
1831 }
1832
Grace Kloba22ac16e2009-10-07 18:00:23 -07001833 /**
1834 * @return The geolocation permissions prompt for this tab.
1835 */
1836 GeolocationPermissionsPrompt getGeolocationPermissionsPrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001837 if (mGeolocationPermissionsPrompt == null) {
1838 ViewStub stub = (ViewStub) mContainer
1839 .findViewById(R.id.geolocation_permissions_prompt);
1840 mGeolocationPermissionsPrompt = (GeolocationPermissionsPrompt) stub
1841 .inflate();
Grace Kloba50c241e2010-04-20 11:07:50 -07001842 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001843 return mGeolocationPermissionsPrompt;
1844 }
1845
1846 /**
1847 * @return The application id string
1848 */
1849 String getAppId() {
1850 return mAppId;
1851 }
1852
1853 /**
1854 * Set the application id string
1855 * @param id
1856 */
1857 void setAppId(String id) {
1858 mAppId = id;
1859 }
1860
Michael Kolbe28b3472011-08-04 16:54:31 -07001861 boolean closeOnBack() {
1862 return mCloseOnBack;
1863 }
1864
1865 void setCloseOnBack(boolean close) {
1866 mCloseOnBack = close;
1867 }
1868
Grace Kloba22ac16e2009-10-07 18:00:23 -07001869 String getUrl() {
John Reck324d4402011-01-11 16:56:42 -08001870 return UrlUtils.filteredUrl(mCurrentState.mUrl);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001871 }
1872
John Reck49a603c2011-03-03 09:33:05 -08001873 String getOriginalUrl() {
John Reckdb22ec42011-06-29 11:31:24 -07001874 if (mCurrentState.mOriginalUrl == null) {
1875 return getUrl();
John Reck49a603c2011-03-03 09:33:05 -08001876 }
John Reckdb22ec42011-06-29 11:31:24 -07001877 return UrlUtils.filteredUrl(mCurrentState.mOriginalUrl);
John Reck49a603c2011-03-03 09:33:05 -08001878 }
1879
Grace Kloba22ac16e2009-10-07 18:00:23 -07001880 /**
John Reck30c714c2010-12-16 17:30:34 -08001881 * Get the title of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001882 */
1883 String getTitle() {
John Reck30c714c2010-12-16 17:30:34 -08001884 if (mCurrentState.mTitle == null && mInPageLoad) {
Michael Kolb14612442011-06-24 13:06:29 -07001885 return mContext.getString(R.string.title_bar_loading);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001886 }
John Reck30c714c2010-12-16 17:30:34 -08001887 return mCurrentState.mTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001888 }
1889
1890 /**
John Reck30c714c2010-12-16 17:30:34 -08001891 * Get the favicon of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001892 */
1893 Bitmap getFavicon() {
John Reck1cf4b792011-07-26 10:22:22 -07001894 if (mCurrentState.mFavicon != null) {
1895 return mCurrentState.mFavicon;
1896 }
1897 return getDefaultFavicon(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001898 }
1899
John Recke969cc52010-12-21 17:24:43 -08001900 public boolean isBookmarkedSite() {
1901 return mCurrentState.mIsBookmarkedSite;
1902 }
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001903
Grace Kloba22ac16e2009-10-07 18:00:23 -07001904 /**
1905 * Return the tab's error console. Creates the console if createIfNEcessary
1906 * is true and we haven't already created the console.
1907 * @param createIfNecessary Flag to indicate if the console should be
1908 * created if it has not been already.
1909 * @return The tab's error console, or null if one has not been created and
1910 * createIfNecessary is false.
1911 */
1912 ErrorConsoleView getErrorConsole(boolean createIfNecessary) {
1913 if (createIfNecessary && mErrorConsole == null) {
Michael Kolb14612442011-06-24 13:06:29 -07001914 mErrorConsole = new ErrorConsoleView(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001915 mErrorConsole.setWebView(mMainView);
1916 }
1917 return mErrorConsole;
1918 }
1919
Steve Block08a6f0c2011-10-06 12:12:53 +01001920 /**
1921 * Sets the security state, clears the SSL certificate error and informs
1922 * the controller.
1923 */
Steve Block2466eff2011-10-03 15:33:09 +01001924 private void setSecurityState(SecurityState securityState) {
1925 mCurrentState.mSecurityState = securityState;
Steve Block08a6f0c2011-10-06 12:12:53 +01001926 mCurrentState.mSslCertificateError = null;
Steve Block2466eff2011-10-03 15:33:09 +01001927 mWebViewController.onUpdatedSecurityState(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001928 }
1929
1930 /**
Steve Block2466eff2011-10-03 15:33:09 +01001931 * @return The tab's security state.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001932 */
Steve Block2466eff2011-10-03 15:33:09 +01001933 SecurityState getSecurityState() {
1934 return mCurrentState.mSecurityState;
John Reck30c714c2010-12-16 17:30:34 -08001935 }
1936
Steve Block08a6f0c2011-10-06 12:12:53 +01001937 /**
1938 * Gets the SSL certificate error, if any, for the page's main resource.
1939 * This is only non-null when the security state is
1940 * SECURITY_STATE_BAD_CERTIFICATE.
1941 */
1942 SslError getSslCertificateError() {
1943 return mCurrentState.mSslCertificateError;
1944 }
1945
John Reck30c714c2010-12-16 17:30:34 -08001946 int getLoadProgress() {
1947 if (mInPageLoad) {
1948 return mPageLoadProgress;
1949 }
1950 return 100;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001951 }
1952
1953 /**
1954 * @return TRUE if onPageStarted is called while onPageFinished is not
1955 * called yet.
1956 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001957 boolean inPageLoad() {
1958 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001959 }
1960
1961 // force mInLoad to be false. This should only be called before closing the
1962 // tab to ensure BrowserActivity's pauseWebViewTimers() is called correctly.
Michael Kolb8233fac2010-10-26 16:08:53 -07001963 void clearInPageLoad() {
1964 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001965 }
1966
Grace Kloba22ac16e2009-10-07 18:00:23 -07001967 /**
John Reck1cf4b792011-07-26 10:22:22 -07001968 * @return The Bundle with the tab's state if it can be saved, otherwise null
Grace Kloba22ac16e2009-10-07 18:00:23 -07001969 */
John Reck1cf4b792011-07-26 10:22:22 -07001970 public Bundle saveState() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001971 // If the WebView is null it means we ran low on memory and we already
1972 // stored the saved state in mSavedState.
1973 if (mMainView == null) {
John Reck1cf4b792011-07-26 10:22:22 -07001974 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001975 }
John Reck6c2e2f32011-08-22 13:41:23 -07001976
1977 if (TextUtils.isEmpty(mCurrentState.mUrl)) {
John Reck1cf4b792011-07-26 10:22:22 -07001978 return null;
John Reck24f18262011-06-17 14:47:20 -07001979 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001980
1981 mSavedState = new Bundle();
John Reck6c2e2f32011-08-22 13:41:23 -07001982 WebBackForwardList savedList = mMainView.saveState(mSavedState);
1983 if (savedList == null || savedList.getSize() == 0) {
1984 Log.w(LOGTAG, "Failed to save back/forward list for "
1985 + mCurrentState.mUrl);
1986 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001987
Michael Kolbc831b632011-05-11 09:30:34 -07001988 mSavedState.putLong(ID, mId);
John Reck30c714c2010-12-16 17:30:34 -08001989 mSavedState.putString(CURRURL, mCurrentState.mUrl);
1990 mSavedState.putString(CURRTITLE, mCurrentState.mTitle);
John Reck1cf4b792011-07-26 10:22:22 -07001991 mSavedState.putBoolean(INCOGNITO, mMainView.isPrivateBrowsingEnabled());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001992 if (mAppId != null) {
1993 mSavedState.putString(APPID, mAppId);
1994 }
Michael Kolbe28b3472011-08-04 16:54:31 -07001995 mSavedState.putBoolean(CLOSEFLAG, mCloseOnBack);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001996 // Remember the parent tab so the relationship can be restored.
Michael Kolbc831b632011-05-11 09:30:34 -07001997 if (mParent != null) {
1998 mSavedState.putLong(PARENTTAB, mParent.mId);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001999 }
John Reckb0a86db2011-05-24 14:05:58 -07002000 mSavedState.putBoolean(USERAGENT,
2001 mSettings.hasDesktopUseragent(getWebView()));
John Reck1cf4b792011-07-26 10:22:22 -07002002 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07002003 }
2004
2005 /*
2006 * Restore the state of the tab.
2007 */
John Reck1cf4b792011-07-26 10:22:22 -07002008 private void restoreState(Bundle b) {
2009 mSavedState = b;
2010 if (mSavedState == null) {
2011 return;
Grace Kloba22ac16e2009-10-07 18:00:23 -07002012 }
2013 // Restore the internal state even if the WebView fails to restore.
2014 // This will maintain the app id, original url and close-on-exit values.
Michael Kolbc831b632011-05-11 09:30:34 -07002015 mId = b.getLong(ID);
Grace Kloba22ac16e2009-10-07 18:00:23 -07002016 mAppId = b.getString(APPID);
Michael Kolbe28b3472011-08-04 16:54:31 -07002017 mCloseOnBack = b.getBoolean(CLOSEFLAG);
John Reck8b9bb8b2012-03-08 13:19:40 -08002018 restoreUserAgent();
John Reck1cf4b792011-07-26 10:22:22 -07002019 String url = b.getString(CURRURL);
2020 String title = b.getString(CURRTITLE);
2021 boolean incognito = b.getBoolean(INCOGNITO);
2022 mCurrentState = new PageState(mContext, incognito, url, null);
2023 mCurrentState.mTitle = title;
2024 synchronized (Tab.this) {
2025 if (mCapture != null) {
John Reck4eadc342011-10-31 14:04:10 -07002026 DataController.getInstance(mContext).loadThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07002027 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002028 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002029 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04002030
John Reck8b9bb8b2012-03-08 13:19:40 -08002031 private void restoreUserAgent() {
2032 if (mMainView == null || mSavedState == null) {
2033 return;
2034 }
2035 if (mSavedState.getBoolean(USERAGENT)
2036 != mSettings.hasDesktopUseragent(mMainView)) {
2037 mSettings.toggleDesktopUseragent(mMainView);
2038 }
2039 }
2040
Leon Scroggins1961ed22010-12-07 15:22:21 -05002041 public void updateBookmarkedStatus() {
John Recke969cc52010-12-21 17:24:43 -08002042 mDataController.queryBookmarkStatus(getUrl(), mIsBookmarkCallback);
Leon Scroggins1961ed22010-12-07 15:22:21 -05002043 }
2044
John Recke969cc52010-12-21 17:24:43 -08002045 private DataController.OnQueryUrlIsBookmark mIsBookmarkCallback
2046 = new DataController.OnQueryUrlIsBookmark() {
2047 @Override
2048 public void onQueryUrlIsBookmark(String url, boolean isBookmark) {
2049 if (mCurrentState.mUrl.equals(url)) {
2050 mCurrentState.mIsBookmarkedSite = isBookmark;
2051 mWebViewController.bookmarkedStatusHasChanged(Tab.this);
2052 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05002053 }
John Recke969cc52010-12-21 17:24:43 -08002054 };
Michael Kolb1acef692011-03-08 14:12:06 -08002055
Michael Kolbeb95db42011-03-03 10:38:40 -08002056 public Bitmap getScreenshot() {
John Reck1cf4b792011-07-26 10:22:22 -07002057 synchronized (Tab.this) {
2058 return mCapture;
2059 }
Michael Kolbeb95db42011-03-03 10:38:40 -08002060 }
2061
John Reck541f55a2011-06-07 16:34:43 -07002062 public boolean isSnapshot() {
John Reck541f55a2011-06-07 16:34:43 -07002063 return false;
2064 }
2065
John Reck68234a92012-04-19 15:27:12 -07002066 private static class SaveCallback implements ValueCallback<Boolean> {
2067 boolean mResult;
2068
2069 @Override
2070 public void onReceiveValue(Boolean value) {
2071 mResult = value;
2072 synchronized (this) {
2073 notifyAll();
John Reck8cc92352011-07-06 17:41:52 -07002074 }
John Reck541f55a2011-06-07 16:34:43 -07002075 }
John Reck68234a92012-04-19 15:27:12 -07002076
2077 }
2078
2079 /**
2080 * Must be called on the UI thread
2081 */
2082 public ContentValues createSnapshotValues() {
2083 WebViewClassic web = getWebViewClassic();
2084 if (web == null) return null;
John Reckd8c74522011-06-14 08:45:00 -07002085 ContentValues values = new ContentValues();
2086 values.put(Snapshots.TITLE, mCurrentState.mTitle);
2087 values.put(Snapshots.URL, mCurrentState.mUrl);
John Reck68234a92012-04-19 15:27:12 -07002088 values.put(Snapshots.BACKGROUND, web.getPageBackgroundColor());
John Reck8cc92352011-07-06 17:41:52 -07002089 values.put(Snapshots.DATE_CREATED, System.currentTimeMillis());
2090 values.put(Snapshots.FAVICON, compressBitmap(getFavicon()));
2091 Bitmap screenshot = Controller.createScreenshot(mMainView,
2092 Controller.getDesiredThumbnailWidth(mContext),
2093 Controller.getDesiredThumbnailHeight(mContext));
2094 values.put(Snapshots.THUMBNAIL, compressBitmap(screenshot));
John Reckd8c74522011-06-14 08:45:00 -07002095 return values;
John Reck541f55a2011-06-07 16:34:43 -07002096 }
2097
John Reck68234a92012-04-19 15:27:12 -07002098 /**
2099 * Probably want to call this on a background thread
2100 */
2101 public boolean saveViewState(ContentValues values) {
2102 WebViewClassic web = getWebViewClassic();
2103 if (web == null) return false;
2104 String path = UUID.randomUUID().toString();
2105 SaveCallback callback = new SaveCallback();
2106 OutputStream outs = null;
2107 try {
2108 outs = mContext.openFileOutput(path, Context.MODE_PRIVATE);
2109 GZIPOutputStream stream = new GZIPOutputStream(outs);
2110 synchronized (callback) {
2111 web.saveViewState(stream, callback);
2112 callback.wait();
2113 }
2114 stream.flush();
2115 stream.close();
2116 } catch (Exception e) {
2117 Log.w(LOGTAG, "Failed to save view state", e);
2118 if (outs != null) {
2119 try {
2120 outs.close();
2121 } catch (IOException ignore) {}
2122 }
2123 File file = mContext.getFileStreamPath(path);
2124 if (file.exists() && !file.delete()) {
2125 file.deleteOnExit();
2126 }
2127 return false;
2128 }
2129 File savedFile = mContext.getFileStreamPath(path);
2130 if (!callback.mResult) {
2131 if (!savedFile.delete()) {
2132 savedFile.deleteOnExit();
2133 }
2134 return false;
2135 }
2136 long size = savedFile.length();
2137 values.put(Snapshots.VIEWSTATE_PATH, path);
2138 values.put(Snapshots.VIEWSTATE_SIZE, size);
2139 return true;
2140 }
2141
John Reck8cc92352011-07-06 17:41:52 -07002142 public byte[] compressBitmap(Bitmap bitmap) {
2143 if (bitmap == null) {
2144 return null;
2145 }
2146 ByteArrayOutputStream stream = new ByteArrayOutputStream();
2147 bitmap.compress(CompressFormat.PNG, 100, stream);
2148 return stream.toByteArray();
2149 }
2150
John Reck26b18322011-06-21 13:08:58 -07002151 public void loadUrl(String url, Map<String, String> headers) {
2152 if (mMainView != null) {
Michael Kolba53c9892011-10-05 13:31:40 -07002153 mPageLoadProgress = INITIAL_PROGRESS;
2154 mInPageLoad = true;
Michael Kolb14612442011-06-24 13:06:29 -07002155 mCurrentState = new PageState(mContext, false, url, null);
John Reck26b18322011-06-21 13:08:58 -07002156 mWebViewController.onPageStarted(this, mMainView, null);
2157 mMainView.loadUrl(url, headers);
2158 }
2159 }
2160
Michael Kolb9ef259a2011-07-12 15:33:08 -07002161 protected void capture() {
2162 if (mMainView == null || mCapture == null) return;
John Reck4eadc342011-10-31 14:04:10 -07002163 if (mMainView.getContentWidth() <= 0 || mMainView.getContentHeight() <= 0) {
2164 return;
2165 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07002166 Canvas c = new Canvas(mCapture);
2167 final int left = mMainView.getScrollX();
2168 final int top = mMainView.getScrollY() + mMainView.getVisibleTitleHeight();
Michael Kolba3194d02011-09-07 11:23:51 -07002169 int state = c.save();
Michael Kolb9ef259a2011-07-12 15:33:08 -07002170 c.translate(-left, -top);
2171 float scale = mCaptureWidth / (float) mMainView.getWidth();
2172 c.scale(scale, scale, left, top);
John Reck8ee633f2011-08-09 16:00:35 -07002173 if (mMainView instanceof BrowserWebView) {
2174 ((BrowserWebView)mMainView).drawContent(c);
2175 } else {
2176 mMainView.draw(c);
2177 }
Michael Kolba3194d02011-09-07 11:23:51 -07002178 c.restoreToCount(state);
2179 // manually anti-alias the edges for the tilt
2180 c.drawRect(0, 0, 1, mCapture.getHeight(), sAlphaPaint);
2181 c.drawRect(mCapture.getWidth() - 1, 0, mCapture.getWidth(),
2182 mCapture.getHeight(), sAlphaPaint);
2183 c.drawRect(0, 0, mCapture.getWidth(), 1, sAlphaPaint);
2184 c.drawRect(0, mCapture.getHeight() - 1, mCapture.getWidth(),
2185 mCapture.getHeight(), sAlphaPaint);
Dianne Hackborn43cfe8a2011-08-02 16:59:35 -07002186 c.setBitmap(null);
John Reck8ee633f2011-08-09 16:00:35 -07002187 mHandler.removeMessages(MSG_CAPTURE);
John Reck1cf4b792011-07-26 10:22:22 -07002188 persistThumbnail();
John Reck8ee633f2011-08-09 16:00:35 -07002189 TabControl tc = mWebViewController.getTabControl();
2190 if (tc != null) {
2191 OnThumbnailUpdatedListener updateListener
2192 = tc.getOnThumbnailUpdatedListener();
2193 if (updateListener != null) {
2194 updateListener.onThumbnailUpdated(this);
2195 }
2196 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07002197 }
2198
2199 @Override
2200 public void onNewPicture(WebView view, Picture picture) {
2201 //update screenshot
John Reck1cf4b792011-07-26 10:22:22 -07002202 postCapture();
2203 }
2204
2205 private void postCapture() {
Michael Kolb9ef259a2011-07-12 15:33:08 -07002206 if (!mHandler.hasMessages(MSG_CAPTURE)) {
2207 mHandler.sendEmptyMessageDelayed(MSG_CAPTURE, CAPTURE_DELAY);
2208 }
2209 }
2210
John Reckef654f12011-07-12 16:42:08 -07002211 public boolean canGoBack() {
2212 return mMainView != null ? mMainView.canGoBack() : false;
2213 }
2214
2215 public boolean canGoForward() {
2216 return mMainView != null ? mMainView.canGoForward() : false;
2217 }
2218
2219 public void goBack() {
2220 if (mMainView != null) {
2221 mMainView.goBack();
2222 }
2223 }
2224
2225 public void goForward() {
2226 if (mMainView != null) {
2227 mMainView.goForward();
2228 }
2229 }
2230
Mathew Inwood1dd8e822011-08-03 14:34:29 +01002231 /**
2232 * Causes the tab back/forward stack to be cleared once, if the given URL is the next URL
2233 * to be added to the stack.
2234 *
2235 * This is used to ensure that preloaded URLs that are not subsequently seen by the user do
2236 * not appear in the back stack.
2237 */
Mathew Inwooda829d552011-09-02 14:16:25 +01002238 public void clearBackStackWhenItemAdded(Pattern urlPattern) {
2239 mClearHistoryUrlPattern = urlPattern;
Mathew Inwood1dd8e822011-08-03 14:34:29 +01002240 }
2241
John Reck1cf4b792011-07-26 10:22:22 -07002242 protected void persistThumbnail() {
John Reck4eadc342011-10-31 14:04:10 -07002243 DataController.getInstance(mContext).saveThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07002244 }
2245
2246 protected void deleteThumbnail() {
John Reck4eadc342011-10-31 14:04:10 -07002247 DataController.getInstance(mContext).deleteThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07002248 }
2249
John Reck4eadc342011-10-31 14:04:10 -07002250 void updateCaptureFromBlob(byte[] blob) {
John Reck1cf4b792011-07-26 10:22:22 -07002251 synchronized (Tab.this) {
2252 if (mCapture == null) {
2253 return;
2254 }
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002255 ByteBuffer buffer = ByteBuffer.wrap(blob);
Michael Kolbd837a112011-08-09 14:00:18 -07002256 try {
2257 mCapture.copyPixelsFromBuffer(buffer);
2258 } catch (RuntimeException rex) {
2259 Log.e(LOGTAG, "Load capture has mismatched sizes; buffer: "
2260 + buffer.capacity() + " blob: " + blob.length
2261 + "capture: " + mCapture.getByteCount());
2262 throw rex;
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002263 }
John Reck1cf4b792011-07-26 10:22:22 -07002264 }
2265 }
2266
John Reck52be4782011-08-26 15:37:29 -07002267 @Override
2268 public String toString() {
2269 StringBuilder builder = new StringBuilder(100);
2270 builder.append(mId);
2271 builder.append(") has parent: ");
2272 if (getParent() != null) {
2273 builder.append("true[");
2274 builder.append(getParent().getId());
2275 builder.append("]");
2276 } else {
2277 builder.append("false");
2278 }
2279 builder.append(", incog: ");
2280 builder.append(isPrivateBrowsingEnabled());
2281 if (!isPrivateBrowsingEnabled()) {
2282 builder.append(", title: ");
2283 builder.append(getTitle());
2284 builder.append(", url: ");
2285 builder.append(getUrl());
2286 }
2287 return builder.toString();
2288 }
2289
Steve Block4895b012011-10-03 16:26:46 +01002290 private void handleProceededAfterSslError(SslError error) {
2291 if (error.getUrl().equals(mCurrentState.mUrl)) {
2292 // The security state should currently be SECURITY_STATE_SECURE.
2293 setSecurityState(SecurityState.SECURITY_STATE_BAD_CERTIFICATE);
Steve Block08a6f0c2011-10-06 12:12:53 +01002294 mCurrentState.mSslCertificateError = error;
Steve Block4895b012011-10-03 16:26:46 +01002295 } else if (getSecurityState() == SecurityState.SECURITY_STATE_SECURE) {
Steve Block08a6f0c2011-10-06 12:12:53 +01002296 // The page's main resource is secure and this error is for a
2297 // sub-resource.
Steve Block4895b012011-10-03 16:26:46 +01002298 setSecurityState(SecurityState.SECURITY_STATE_MIXED);
2299 }
2300 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002301}