blob: c73bdf6b3481e6c4f6c222480b912738224e63a8 [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 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700899 mWebViewController.onUnhandledKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700900 }
Patrick Scott92066772011-03-10 08:46:27 -0500901
902 @Override
903 public void onReceivedLoginRequest(WebView view, String realm,
904 String account, String args) {
Michael Kolb14612442011-06-24 13:06:29 -0700905 new DeviceAccountLogin(mWebViewController.getActivity(), view, Tab.this, mWebViewController)
Patrick Scott92066772011-03-10 08:46:27 -0500906 .handleLogin(realm, account, args);
907 }
908
Grace Kloba22ac16e2009-10-07 18:00:23 -0700909 };
910
John Reck1cf4b792011-07-26 10:22:22 -0700911 private void syncCurrentState(WebView view, String url) {
912 // Sync state (in case of stop/timeout)
913 mCurrentState.mUrl = view.getUrl();
914 if (mCurrentState.mUrl == null) {
John Reck8bcafc12011-08-29 16:43:02 -0700915 mCurrentState.mUrl = "";
John Reck1cf4b792011-07-26 10:22:22 -0700916 }
917 mCurrentState.mOriginalUrl = view.getOriginalUrl();
918 mCurrentState.mTitle = view.getTitle();
919 mCurrentState.mFavicon = view.getFavicon();
920 if (!URLUtil.isHttpsUrl(mCurrentState.mUrl)) {
921 // In case we stop when loading an HTTPS page from an HTTP page
922 // but before a provisional load occurred
Steve Block2466eff2011-10-03 15:33:09 +0100923 mCurrentState.mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
Steve Block08a6f0c2011-10-06 12:12:53 +0100924 mCurrentState.mSslCertificateError = null;
John Reck1cf4b792011-07-26 10:22:22 -0700925 }
John Reck502a3532011-08-16 14:21:46 -0700926 mCurrentState.mIncognito = view.isPrivateBrowsingEnabled();
John Reck1cf4b792011-07-26 10:22:22 -0700927 }
928
Patrick Scott92066772011-03-10 08:46:27 -0500929 // Called by DeviceAccountLogin when the Tab needs to have the auto-login UI
930 // displayed.
931 void setDeviceAccountLogin(DeviceAccountLogin login) {
932 mDeviceAccountLogin = login;
933 }
934
935 // Returns non-null if the title bar should display the auto-login UI.
936 DeviceAccountLogin getDeviceAccountLogin() {
937 return mDeviceAccountLogin;
938 }
939
Grace Kloba22ac16e2009-10-07 18:00:23 -0700940 // -------------------------------------------------------------------------
941 // WebChromeClient implementation for the main WebView
942 // -------------------------------------------------------------------------
943
944 private final WebChromeClient mWebChromeClient = new WebChromeClient() {
945 // Helper method to create a new tab or sub window.
946 private void createWindow(final boolean dialog, final Message msg) {
947 WebView.WebViewTransport transport =
948 (WebView.WebViewTransport) msg.obj;
949 if (dialog) {
950 createSubWindow();
Michael Kolb8233fac2010-10-26 16:08:53 -0700951 mWebViewController.attachSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700952 transport.setWebView(mSubView);
953 } else {
Michael Kolb7bcafde2011-05-09 13:55:59 -0700954 final Tab newTab = mWebViewController.openTab(null,
John Reck5949c662011-05-27 09:52:29 -0700955 Tab.this, true, true);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700956 transport.setWebView(newTab.getWebView());
957 }
958 msg.sendToTarget();
959 }
960
961 @Override
962 public boolean onCreateWindow(WebView view, final boolean dialog,
963 final boolean userGesture, final Message resultMsg) {
964 // only allow new window or sub window for the foreground case
965 if (!mInForeground) {
966 return false;
967 }
968 // Short-circuit if we can't create any more tabs or sub windows.
969 if (dialog && mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -0700970 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700971 .setTitle(R.string.too_many_subwindows_dialog_title)
972 .setIcon(android.R.drawable.ic_dialog_alert)
973 .setMessage(R.string.too_many_subwindows_dialog_message)
974 .setPositiveButton(R.string.ok, null)
975 .show();
976 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700977 } else if (!mWebViewController.getTabControl().canCreateNewTab()) {
Michael Kolb14612442011-06-24 13:06:29 -0700978 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700979 .setTitle(R.string.too_many_windows_dialog_title)
980 .setIcon(android.R.drawable.ic_dialog_alert)
981 .setMessage(R.string.too_many_windows_dialog_message)
982 .setPositiveButton(R.string.ok, null)
983 .show();
984 return false;
985 }
986
987 // Short-circuit if this was a user gesture.
988 if (userGesture) {
989 createWindow(dialog, resultMsg);
990 return true;
991 }
992
993 // Allow the popup and create the appropriate window.
994 final AlertDialog.OnClickListener allowListener =
995 new AlertDialog.OnClickListener() {
996 public void onClick(DialogInterface d,
997 int which) {
998 createWindow(dialog, resultMsg);
999 }
1000 };
1001
1002 // Block the popup by returning a null WebView.
1003 final AlertDialog.OnClickListener blockListener =
1004 new AlertDialog.OnClickListener() {
1005 public void onClick(DialogInterface d, int which) {
1006 resultMsg.sendToTarget();
1007 }
1008 };
1009
1010 // Build a confirmation dialog to display to the user.
1011 final AlertDialog d =
Michael Kolb14612442011-06-24 13:06:29 -07001012 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -07001013 .setIcon(android.R.drawable.ic_dialog_alert)
1014 .setMessage(R.string.popup_window_attempt)
1015 .setPositiveButton(R.string.allow, allowListener)
1016 .setNegativeButton(R.string.block, blockListener)
1017 .setCancelable(false)
1018 .create();
1019
1020 // Show the confirmation dialog.
1021 d.show();
1022 return true;
1023 }
1024
1025 @Override
Patrick Scotteb5061b2009-11-18 15:00:30 -05001026 public void onRequestFocus(WebView view) {
1027 if (!mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -07001028 mWebViewController.switchToTab(Tab.this);
Patrick Scotteb5061b2009-11-18 15:00:30 -05001029 }
1030 }
1031
1032 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -07001033 public void onCloseWindow(WebView window) {
Michael Kolbc831b632011-05-11 09:30:34 -07001034 if (mParent != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001035 // JavaScript can only close popup window.
1036 if (mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -07001037 mWebViewController.switchToTab(mParent);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001038 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001039 mWebViewController.closeTab(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001040 }
1041 }
1042
1043 @Override
1044 public void onProgressChanged(WebView view, int newProgress) {
John Reck30c714c2010-12-16 17:30:34 -08001045 mPageLoadProgress = newProgress;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001046 if (newProgress == 100) {
1047 mInPageLoad = false;
1048 }
John Reck30c714c2010-12-16 17:30:34 -08001049 mWebViewController.onProgressChanged(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001050 }
1051
1052 @Override
Leon Scroggins21d9b902010-03-11 09:33:11 -05001053 public void onReceivedTitle(WebView view, final String title) {
John Reck30c714c2010-12-16 17:30:34 -08001054 mCurrentState.mTitle = title;
Michael Kolb8233fac2010-10-26 16:08:53 -07001055 mWebViewController.onReceivedTitle(Tab.this, title);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001056 }
1057
1058 @Override
1059 public void onReceivedIcon(WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -08001060 mCurrentState.mFavicon = icon;
Michael Kolb8233fac2010-10-26 16:08:53 -07001061 mWebViewController.onFavicon(Tab.this, view, icon);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001062 }
1063
1064 @Override
1065 public void onReceivedTouchIconUrl(WebView view, String url,
1066 boolean precomposed) {
Michael Kolb14612442011-06-24 13:06:29 -07001067 final ContentResolver cr = mContext.getContentResolver();
Leon Scrogginsc8393d92010-04-23 14:58:16 -04001068 // Let precomposed icons take precedence over non-composed
1069 // icons.
1070 if (precomposed && mTouchIconLoader != null) {
1071 mTouchIconLoader.cancel(false);
1072 mTouchIconLoader = null;
1073 }
1074 // Have only one async task at a time.
1075 if (mTouchIconLoader == null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001076 mTouchIconLoader = new DownloadTouchIcon(Tab.this,
Michael Kolb14612442011-06-24 13:06:29 -07001077 mContext, cr, view);
Leon Scrogginsc8393d92010-04-23 14:58:16 -04001078 mTouchIconLoader.execute(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001079 }
1080 }
1081
1082 @Override
1083 public void onShowCustomView(View view,
1084 WebChromeClient.CustomViewCallback callback) {
Michael Kolb14612442011-06-24 13:06:29 -07001085 Activity activity = mWebViewController.getActivity();
1086 if (activity != null) {
1087 onShowCustomView(view, activity.getRequestedOrientation(), callback);
1088 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001089 }
1090
1091 @Override
1092 public void onShowCustomView(View view, int requestedOrientation,
1093 WebChromeClient.CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001094 if (mInForeground) mWebViewController.showCustomView(Tab.this, view,
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001095 requestedOrientation, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001096 }
1097
1098 @Override
1099 public void onHideCustomView() {
Michael Kolb8233fac2010-10-26 16:08:53 -07001100 if (mInForeground) mWebViewController.hideCustomView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001101 }
1102
1103 /**
1104 * The origin has exceeded its database quota.
1105 * @param url the URL that exceeded the quota
1106 * @param databaseIdentifier the identifier of the database on which the
1107 * transaction that caused the quota overflow was run
1108 * @param currentQuota the current quota for the origin.
1109 * @param estimatedSize the estimated size of the database.
1110 * @param totalUsedQuota is the sum of all origins' quota.
1111 * @param quotaUpdater The callback to run when a decision to allow or
1112 * deny quota has been made. Don't forget to call this!
1113 */
1114 @Override
1115 public void onExceededDatabaseQuota(String url,
1116 String databaseIdentifier, long currentQuota, long estimatedSize,
1117 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -07001118 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001119 .onExceededDatabaseQuota(url, databaseIdentifier,
1120 currentQuota, estimatedSize, totalUsedQuota,
1121 quotaUpdater);
1122 }
1123
1124 /**
1125 * The Application Cache has exceeded its max size.
1126 * @param spaceNeeded is the amount of disk space that would be needed
1127 * in order for the last appcache operation to succeed.
1128 * @param totalUsedQuota is the sum of all origins' quota.
1129 * @param quotaUpdater A callback to inform the WebCore thread that a
1130 * new app cache size is available. This callback must always
1131 * be executed at some point to ensure that the sleeping
1132 * WebCore thread is woken up.
1133 */
1134 @Override
1135 public void onReachedMaxAppCacheSize(long spaceNeeded,
1136 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -07001137 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001138 .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota,
1139 quotaUpdater);
1140 }
1141
1142 /**
1143 * Instructs the browser to show a prompt to ask the user to set the
1144 * Geolocation permission state for the specified origin.
1145 * @param origin The origin for which Geolocation permissions are
1146 * requested.
1147 * @param callback The callback to call once the user has set the
1148 * Geolocation permission state.
1149 */
1150 @Override
1151 public void onGeolocationPermissionsShowPrompt(String origin,
1152 GeolocationPermissions.Callback callback) {
1153 if (mInForeground) {
Grace Kloba50c241e2010-04-20 11:07:50 -07001154 getGeolocationPermissionsPrompt().show(origin, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001155 }
1156 }
1157
1158 /**
1159 * Instructs the browser to hide the Geolocation permissions prompt.
1160 */
1161 @Override
1162 public void onGeolocationPermissionsHidePrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001163 if (mInForeground && mGeolocationPermissionsPrompt != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001164 mGeolocationPermissionsPrompt.hide();
1165 }
1166 }
1167
Ben Murdoch65acc352009-11-19 18:16:04 +00001168 /* Adds a JavaScript error message to the system log and if the JS
1169 * console is enabled in the about:debug options, to that console
1170 * also.
Ben Murdochc42addf2010-01-28 15:19:59 +00001171 * @param consoleMessage the message object.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001172 */
1173 @Override
Ben Murdochc42addf2010-01-28 15:19:59 +00001174 public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001175 if (mInForeground) {
1176 // call getErrorConsole(true) so it will create one if needed
1177 ErrorConsoleView errorConsole = getErrorConsole(true);
Ben Murdochc42addf2010-01-28 15:19:59 +00001178 errorConsole.addErrorMessage(consoleMessage);
Michael Kolb8233fac2010-10-26 16:08:53 -07001179 if (mWebViewController.shouldShowErrorConsole()
1180 && errorConsole.getShowState() !=
1181 ErrorConsoleView.SHOW_MAXIMIZED) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001182 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
1183 }
1184 }
Ben Murdochc42addf2010-01-28 15:19:59 +00001185
Jeff Hamilton47654f42010-09-07 09:57:51 -05001186 // Don't log console messages in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001187 if (isPrivateBrowsingEnabled()) return true;
Jeff Hamilton47654f42010-09-07 09:57:51 -05001188
Ben Murdochc42addf2010-01-28 15:19:59 +00001189 String message = "Console: " + consoleMessage.message() + " "
1190 + consoleMessage.sourceId() + ":"
1191 + consoleMessage.lineNumber();
1192
1193 switch (consoleMessage.messageLevel()) {
1194 case TIP:
1195 Log.v(CONSOLE_LOGTAG, message);
1196 break;
1197 case LOG:
1198 Log.i(CONSOLE_LOGTAG, message);
1199 break;
1200 case WARNING:
1201 Log.w(CONSOLE_LOGTAG, message);
1202 break;
1203 case ERROR:
1204 Log.e(CONSOLE_LOGTAG, message);
1205 break;
1206 case DEBUG:
1207 Log.d(CONSOLE_LOGTAG, message);
1208 break;
1209 }
1210
1211 return true;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001212 }
1213
1214 /**
1215 * Ask the browser for an icon to represent a <video> element.
1216 * This icon will be used if the Web page did not specify a poster attribute.
1217 * @return Bitmap The icon or null if no such icon is available.
1218 */
1219 @Override
1220 public Bitmap getDefaultVideoPoster() {
1221 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001222 return mWebViewController.getDefaultVideoPoster();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001223 }
1224 return null;
1225 }
1226
1227 /**
1228 * Ask the host application for a custom progress view to show while
1229 * a <video> is loading.
1230 * @return View The progress view.
1231 */
1232 @Override
1233 public View getVideoLoadingProgressView() {
1234 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001235 return mWebViewController.getVideoLoadingProgressView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001236 }
1237 return null;
1238 }
1239
1240 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00001241 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001242 if (mInForeground) {
Ben Murdoch8cad4132012-01-11 10:56:43 +00001243 mWebViewController.openFileChooser(uploadMsg, acceptType, capture);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001244 } else {
1245 uploadMsg.onReceiveValue(null);
1246 }
1247 }
1248
1249 /**
1250 * Deliver a list of already-visited URLs
1251 */
1252 @Override
1253 public void getVisitedHistory(final ValueCallback<String[]> callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001254 mWebViewController.getVisitedHistory(callback);
1255 }
Ben Murdoch8029a772010-11-16 11:58:21 +00001256
1257 @Override
1258 public void setupAutoFill(Message message) {
1259 // Prompt the user to set up their profile.
1260 final Message msg = message;
Michael Kolb14612442011-06-24 13:06:29 -07001261 AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
1262 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
Ben Murdoch1d676b62011-01-17 12:54:24 +00001263 Context.LAYOUT_INFLATER_SERVICE);
1264 final View layout = inflater.inflate(R.layout.setup_autofill_dialog, null);
1265
1266 builder.setView(layout)
1267 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1268 @Override
1269 public void onClick(DialogInterface dialog, int id) {
1270 CheckBox disableAutoFill = (CheckBox) layout.findViewById(
1271 R.id.setup_autofill_dialog_disable_autofill);
1272
1273 if (disableAutoFill.isChecked()) {
1274 // Disable autofill and show a toast with how to turn it on again.
John Reck35e9dd62011-04-25 09:01:54 -07001275 mSettings.setAutofillEnabled(false);
Michael Kolb14612442011-06-24 13:06:29 -07001276 Toast.makeText(mContext,
Ben Murdoch1d676b62011-01-17 12:54:24 +00001277 R.string.autofill_setup_dialog_negative_toast,
1278 Toast.LENGTH_LONG).show();
1279 } else {
1280 // Take user to the AutoFill profile editor. When they return,
1281 // we will send the message that we pass here which will trigger
1282 // the form to get filled out with their new profile.
1283 mWebViewController.setupAutoFill(msg);
1284 }
1285 }
1286 })
1287 .setNegativeButton(R.string.cancel, null)
1288 .show();
Ben Murdoch8029a772010-11-16 11:58:21 +00001289 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001290 };
1291
1292 // -------------------------------------------------------------------------
1293 // WebViewClient implementation for the sub window
1294 // -------------------------------------------------------------------------
1295
1296 // Subclass of WebViewClient used in subwindows to notify the main
1297 // WebViewClient of certain WebView activities.
1298 private static class SubWindowClient extends WebViewClient {
1299 // The main WebViewClient.
1300 private final WebViewClient mClient;
Michael Kolb8233fac2010-10-26 16:08:53 -07001301 private final WebViewController mController;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001302
Michael Kolb8233fac2010-10-26 16:08:53 -07001303 SubWindowClient(WebViewClient client, WebViewController controller) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001304 mClient = client;
Michael Kolb8233fac2010-10-26 16:08:53 -07001305 mController = controller;
Leon Scroggins III211ba542010-04-19 13:21:13 -04001306 }
1307 @Override
1308 public void onPageStarted(WebView view, String url, Bitmap favicon) {
1309 // Unlike the others, do not call mClient's version, which would
1310 // change the progress bar. However, we do want to remove the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001311 // find or select dialog.
Michael Kolb8233fac2010-10-26 16:08:53 -07001312 mController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001313 }
1314 @Override
1315 public void doUpdateVisitedHistory(WebView view, String url,
1316 boolean isReload) {
1317 mClient.doUpdateVisitedHistory(view, url, isReload);
1318 }
1319 @Override
1320 public boolean shouldOverrideUrlLoading(WebView view, String url) {
1321 return mClient.shouldOverrideUrlLoading(view, url);
1322 }
1323 @Override
1324 public void onReceivedSslError(WebView view, SslErrorHandler handler,
1325 SslError error) {
1326 mClient.onReceivedSslError(view, handler, error);
1327 }
1328 @Override
Brian Carlstrom8862c1d2011-06-02 01:05:55 -07001329 public void onReceivedClientCertRequest(WebView view,
1330 ClientCertRequestHandler handler, String host_and_port) {
1331 mClient.onReceivedClientCertRequest(view, handler, host_and_port);
1332 }
1333 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -07001334 public void onReceivedHttpAuthRequest(WebView view,
1335 HttpAuthHandler handler, String host, String realm) {
1336 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
1337 }
1338 @Override
1339 public void onFormResubmission(WebView view, Message dontResend,
1340 Message resend) {
1341 mClient.onFormResubmission(view, dontResend, resend);
1342 }
1343 @Override
1344 public void onReceivedError(WebView view, int errorCode,
1345 String description, String failingUrl) {
1346 mClient.onReceivedError(view, errorCode, description, failingUrl);
1347 }
1348 @Override
1349 public boolean shouldOverrideKeyEvent(WebView view,
1350 android.view.KeyEvent event) {
1351 return mClient.shouldOverrideKeyEvent(view, event);
1352 }
1353 @Override
1354 public void onUnhandledKeyEvent(WebView view,
1355 android.view.KeyEvent event) {
1356 mClient.onUnhandledKeyEvent(view, event);
1357 }
1358 }
1359
1360 // -------------------------------------------------------------------------
1361 // WebChromeClient implementation for the sub window
1362 // -------------------------------------------------------------------------
1363
1364 private class SubWindowChromeClient extends WebChromeClient {
1365 // The main WebChromeClient.
1366 private final WebChromeClient mClient;
1367
1368 SubWindowChromeClient(WebChromeClient client) {
1369 mClient = client;
1370 }
1371 @Override
1372 public void onProgressChanged(WebView view, int newProgress) {
1373 mClient.onProgressChanged(view, newProgress);
1374 }
1375 @Override
1376 public boolean onCreateWindow(WebView view, boolean dialog,
1377 boolean userGesture, android.os.Message resultMsg) {
1378 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
1379 }
1380 @Override
1381 public void onCloseWindow(WebView window) {
1382 if (window != mSubView) {
1383 Log.e(LOGTAG, "Can't close the window");
1384 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001385 mWebViewController.dismissSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001386 }
1387 }
1388
1389 // -------------------------------------------------------------------------
1390
1391 // Construct a new tab
Michael Kolb7bcafde2011-05-09 13:55:59 -07001392 Tab(WebViewController wvcontroller, WebView w) {
John Reck1cf4b792011-07-26 10:22:22 -07001393 this(wvcontroller, w, null);
1394 }
1395
1396 Tab(WebViewController wvcontroller, Bundle state) {
1397 this(wvcontroller, null, state);
1398 }
1399
1400 Tab(WebViewController wvcontroller, WebView w, Bundle state) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001401 mWebViewController = wvcontroller;
Michael Kolb14612442011-06-24 13:06:29 -07001402 mContext = mWebViewController.getContext();
John Reck35e9dd62011-04-25 09:01:54 -07001403 mSettings = BrowserSettings.getInstance();
Michael Kolb14612442011-06-24 13:06:29 -07001404 mDataController = DataController.getInstance(mContext);
1405 mCurrentState = new PageState(mContext, w != null
John Reck847b5322011-04-14 17:02:18 -07001406 ? w.isPrivateBrowsingEnabled() : false);
Michael Kolb8233fac2010-10-26 16:08:53 -07001407 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001408 mInForeground = false;
1409
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001410 mDownloadListener = new DownloadListener() {
1411 public void onDownloadStart(String url, String userAgent,
1412 String contentDisposition, String mimetype,
1413 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001414 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001415 mimetype, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001416 }
1417 };
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001418 mWebBackForwardListClient = new WebBackForwardListClient() {
1419 @Override
1420 public void onNewHistoryItem(WebHistoryItem item) {
1421 if (isInVoiceSearchMode()) {
1422 item.setCustomData(mVoiceSearchData.mVoiceSearchIntent);
1423 }
Mathew Inwooda829d552011-09-02 14:16:25 +01001424 if (mClearHistoryUrlPattern != null) {
1425 boolean match =
1426 mClearHistoryUrlPattern.matcher(item.getOriginalUrl()).matches();
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001427 if (LOGD_ENABLED) {
Mathew Inwooda829d552011-09-02 14:16:25 +01001428 Log.d(LOGTAG, "onNewHistoryItem: match=" + match + "\n\t"
1429 + item.getUrl() + "\n\t"
1430 + mClearHistoryUrlPattern);
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001431 }
Mathew Inwooda829d552011-09-02 14:16:25 +01001432 if (match) {
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001433 if (mMainView != null) {
1434 mMainView.clearHistory();
1435 }
1436 }
Mathew Inwooda829d552011-09-02 14:16:25 +01001437 mClearHistoryUrlPattern = null;
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001438 }
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001439 }
1440 @Override
1441 public void onIndexChanged(WebHistoryItem item, int index) {
1442 Object data = item.getCustomData();
1443 if (data != null && data instanceof Intent) {
1444 activateVoiceSearchMode((Intent) data);
1445 }
1446 }
1447 };
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001448
John Reck1cf4b792011-07-26 10:22:22 -07001449 mCaptureWidth = mContext.getResources().getDimensionPixelSize(
1450 R.dimen.tab_thumbnail_width);
1451 mCaptureHeight = mContext.getResources().getDimensionPixelSize(
1452 R.dimen.tab_thumbnail_height);
1453 updateShouldCaptureThumbnails();
1454 restoreState(state);
John Reck52be4782011-08-26 15:37:29 -07001455 if (getId() == -1) {
1456 mId = TabControl.getNextId();
1457 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001458 setWebView(w);
Michael Kolb9ef259a2011-07-12 15:33:08 -07001459 mHandler = new Handler() {
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001460 @Override
Michael Kolb9ef259a2011-07-12 15:33:08 -07001461 public void handleMessage(Message m) {
John Reck1cf4b792011-07-26 10:22:22 -07001462 switch (m.what) {
1463 case MSG_CAPTURE:
1464 capture();
1465 break;
1466 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001467 }
1468 };
John Reck1cf4b792011-07-26 10:22:22 -07001469 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001470
Mathew Inwoode09305e2011-09-02 12:03:26 +01001471 /**
1472 * This is used to get a new ID when the tab has been preloaded, before it is displayed and
1473 * added to TabControl. Preloaded tabs can be created before restoreInstanceState, leading
1474 * to overlapping IDs between the preloaded and restored tabs.
1475 */
1476 public void refreshIdAfterPreload() {
1477 mId = TabControl.getNextId();
1478 }
1479
John Reck1cf4b792011-07-26 10:22:22 -07001480 public void updateShouldCaptureThumbnails() {
1481 if (mWebViewController.shouldCaptureThumbnails()) {
1482 synchronized (Tab.this) {
1483 if (mCapture == null) {
1484 mCapture = Bitmap.createBitmap(mCaptureWidth, mCaptureHeight,
1485 Bitmap.Config.RGB_565);
Michael Kolbc3af0672011-08-09 10:24:41 -07001486 mCapture.eraseColor(Color.WHITE);
John Reck1cf4b792011-07-26 10:22:22 -07001487 if (mInForeground) {
1488 postCapture();
1489 }
1490 }
1491 }
1492 } else {
1493 synchronized (Tab.this) {
1494 mCapture = null;
1495 deleteThumbnail();
1496 }
1497 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001498 }
1499
Michael Kolb14612442011-06-24 13:06:29 -07001500 public void setController(WebViewController ctl) {
1501 mWebViewController = ctl;
John Reck1cf4b792011-07-26 10:22:22 -07001502 updateShouldCaptureThumbnails();
Michael Kolb14612442011-06-24 13:06:29 -07001503 }
1504
Michael Kolbc831b632011-05-11 09:30:34 -07001505 public long getId() {
1506 return mId;
1507 }
1508
Michael Kolb91911a22012-01-17 11:21:25 -08001509 void setWebView(WebView w) {
1510 setWebView(w, true);
1511 }
1512
Grace Kloba22ac16e2009-10-07 18:00:23 -07001513 /**
1514 * Sets the WebView for this tab, correctly removing the old WebView from
1515 * the container view.
1516 */
Michael Kolb91911a22012-01-17 11:21:25 -08001517 void setWebView(WebView w, boolean restore) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001518 if (mMainView == w) {
1519 return;
1520 }
Michael Kolba713ec82010-11-29 17:27:06 -08001521
Grace Kloba22ac16e2009-10-07 18:00:23 -07001522 // If the WebView is changing, the page will be reloaded, so any ongoing
1523 // Geolocation permission requests are void.
Grace Kloba50c241e2010-04-20 11:07:50 -07001524 if (mGeolocationPermissionsPrompt != null) {
1525 mGeolocationPermissionsPrompt.hide();
1526 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001527
Michael Kolba713ec82010-11-29 17:27:06 -08001528 mWebViewController.onSetWebView(this, w);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001529
John Reck1cf4b792011-07-26 10:22:22 -07001530 if (mMainView != null) {
John Reck8ee633f2011-08-09 16:00:35 -07001531 mMainView.setPictureListener(null);
John Reck1cf4b792011-07-26 10:22:22 -07001532 if (w != null) {
1533 syncCurrentState(w, null);
1534 } else {
1535 mCurrentState = new PageState(mContext, false);
1536 }
1537 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001538 // set the new one
1539 mMainView = w;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001540 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001541 if (mMainView != null) {
1542 mMainView.setWebViewClient(mWebViewClient);
1543 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001544 // Attach DownloadManager so that downloads can start in an active
1545 // or a non-active window. This can happen when going to a site that
1546 // does a redirect after a period of time. The user could have
1547 // switched to another tab while waiting for the download to start.
1548 mMainView.setDownloadListener(mDownloadListener);
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00001549 getWebViewClassic().setWebBackForwardListClient(mWebBackForwardListClient);
John Reck8ee633f2011-08-09 16:00:35 -07001550 TabControl tc = mWebViewController.getTabControl();
1551 if (tc != null && tc.getOnThumbnailUpdatedListener() != null) {
1552 mMainView.setPictureListener(this);
1553 }
Michael Kolb91911a22012-01-17 11:21:25 -08001554 if (restore && (mSavedState != null)) {
John Reck8b9bb8b2012-03-08 13:19:40 -08001555 restoreUserAgent();
John Reck6c2e2f32011-08-22 13:41:23 -07001556 WebBackForwardList restoredState
1557 = mMainView.restoreState(mSavedState);
1558 if (restoredState == null || restoredState.getSize() == 0) {
1559 Log.w(LOGTAG, "Failed to restore WebView state!");
1560 loadUrl(mCurrentState.mOriginalUrl, null);
1561 }
John Reck1cf4b792011-07-26 10:22:22 -07001562 mSavedState = null;
1563 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001564 }
1565 }
1566
1567 /**
1568 * Destroy the tab's main WebView and subWindow if any
1569 */
1570 void destroy() {
1571 if (mMainView != null) {
1572 dismissSubWindow();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001573 // save the WebView to call destroy() after detach it from the tab
1574 WebView webView = mMainView;
1575 setWebView(null);
1576 webView.destroy();
1577 }
1578 }
1579
1580 /**
1581 * Remove the tab from the parent
1582 */
1583 void removeFromTree() {
1584 // detach the children
Michael Kolbc831b632011-05-11 09:30:34 -07001585 if (mChildren != null) {
1586 for(Tab t : mChildren) {
1587 t.setParent(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001588 }
1589 }
1590 // remove itself from the parent list
Michael Kolbc831b632011-05-11 09:30:34 -07001591 if (mParent != null) {
1592 mParent.mChildren.remove(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001593 }
John Reck1cf4b792011-07-26 10:22:22 -07001594 deleteThumbnail();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001595 }
1596
1597 /**
1598 * Create a new subwindow unless a subwindow already exists.
1599 * @return True if a new subwindow was created. False if one already exists.
1600 */
1601 boolean createSubWindow() {
1602 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001603 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001604 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001605 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001606 mSubView.setWebChromeClient(new SubWindowChromeClient(
1607 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001608 // Set a different DownloadListener for the mSubView, since it will
1609 // just need to dismiss the mSubView, rather than close the Tab
1610 mSubView.setDownloadListener(new DownloadListener() {
1611 public void onDownloadStart(String url, String userAgent,
1612 String contentDisposition, String mimetype,
1613 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001614 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001615 contentDisposition, mimetype, contentLength);
1616 if (mSubView.copyBackForwardList().getSize() == 0) {
1617 // This subwindow was opened for the sole purpose of
1618 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001619 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001620 }
1621 }
1622 });
Michael Kolb14612442011-06-24 13:06:29 -07001623 mSubView.setOnCreateContextMenuListener(mWebViewController.getActivity());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001624 return true;
1625 }
1626 return false;
1627 }
1628
1629 /**
1630 * Dismiss the subWindow for the tab.
1631 */
1632 void dismissSubWindow() {
1633 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001634 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001635 mSubView.destroy();
1636 mSubView = null;
1637 mSubViewContainer = null;
1638 }
1639 }
1640
Grace Kloba22ac16e2009-10-07 18:00:23 -07001641
1642 /**
1643 * Set the parent tab of this tab.
1644 */
Michael Kolbc831b632011-05-11 09:30:34 -07001645 void setParent(Tab parent) {
John Reck52be4782011-08-26 15:37:29 -07001646 if (parent == this) {
1647 throw new IllegalStateException("Cannot set parent to self!");
1648 }
Michael Kolbc831b632011-05-11 09:30:34 -07001649 mParent = parent;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001650 // This tab may have been freed due to low memory. If that is the case,
Michael Kolbc831b632011-05-11 09:30:34 -07001651 // the parent tab id is already saved. If we are changing that id
Grace Kloba22ac16e2009-10-07 18:00:23 -07001652 // (most likely due to removing the parent tab) we must update the
Michael Kolbc831b632011-05-11 09:30:34 -07001653 // parent tab id in the saved Bundle.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001654 if (mSavedState != null) {
1655 if (parent == null) {
1656 mSavedState.remove(PARENTTAB);
1657 } else {
Michael Kolbc831b632011-05-11 09:30:34 -07001658 mSavedState.putLong(PARENTTAB, parent.getId());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001659 }
1660 }
John Reckb0a86db2011-05-24 14:05:58 -07001661
1662 // Sync the WebView useragent with the parent
1663 if (parent != null && mSettings.hasDesktopUseragent(parent.getWebView())
1664 != mSettings.hasDesktopUseragent(getWebView())) {
1665 mSettings.toggleDesktopUseragent(getWebView());
1666 }
John Reck52be4782011-08-26 15:37:29 -07001667
1668 if (parent != null && parent.getId() == getId()) {
1669 throw new IllegalStateException("Parent has same ID as child!");
1670 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001671 }
1672
1673 /**
Michael Kolbc831b632011-05-11 09:30:34 -07001674 * If this Tab was created through another Tab, then this method returns
1675 * that Tab.
1676 * @return the Tab parent or null
1677 */
1678 public Tab getParent() {
1679 return mParent;
1680 }
1681
1682 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001683 * When a Tab is created through the content of another Tab, then we
1684 * associate the Tabs.
1685 * @param child the Tab that was created from this Tab
1686 */
1687 void addChildTab(Tab child) {
Michael Kolbc831b632011-05-11 09:30:34 -07001688 if (mChildren == null) {
1689 mChildren = new Vector<Tab>();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001690 }
Michael Kolbc831b632011-05-11 09:30:34 -07001691 mChildren.add(child);
1692 child.setParent(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001693 }
1694
Michael Kolbc831b632011-05-11 09:30:34 -07001695 Vector<Tab> getChildren() {
1696 return mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001697 }
1698
1699 void resume() {
1700 if (mMainView != null) {
John Reck56c1fcf2011-08-17 10:15:16 -07001701 setupHwAcceleration(mMainView);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001702 mMainView.onResume();
1703 if (mSubView != null) {
1704 mSubView.onResume();
1705 }
1706 }
1707 }
1708
John Reck56c1fcf2011-08-17 10:15:16 -07001709 private void setupHwAcceleration(View web) {
1710 if (web == null) return;
1711 BrowserSettings settings = BrowserSettings.getInstance();
1712 if (settings.isHardwareAccelerated()) {
1713 web.setLayerType(View.LAYER_TYPE_NONE, null);
1714 } else {
1715 web.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
1716 }
1717 }
1718
Grace Kloba22ac16e2009-10-07 18:00:23 -07001719 void pause() {
1720 if (mMainView != null) {
1721 mMainView.onPause();
1722 if (mSubView != null) {
1723 mSubView.onPause();
1724 }
1725 }
1726 }
1727
1728 void putInForeground() {
John Reck8ee633f2011-08-09 16:00:35 -07001729 if (mInForeground) {
1730 return;
1731 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001732 mInForeground = true;
1733 resume();
Michael Kolb14612442011-06-24 13:06:29 -07001734 Activity activity = mWebViewController.getActivity();
1735 mMainView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001736 if (mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -07001737 mSubView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001738 }
1739 // Show the pending error dialog if the queue is not empty
1740 if (mQueuedErrors != null && mQueuedErrors.size() > 0) {
1741 showError(mQueuedErrors.getFirst());
1742 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001743 mWebViewController.bookmarkedStatusHasChanged(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001744 }
1745
1746 void putInBackground() {
John Reck8ee633f2011-08-09 16:00:35 -07001747 if (!mInForeground) {
1748 return;
1749 }
John Reck52be4782011-08-26 15:37:29 -07001750 capture();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001751 mInForeground = false;
1752 pause();
1753 mMainView.setOnCreateContextMenuListener(null);
1754 if (mSubView != null) {
1755 mSubView.setOnCreateContextMenuListener(null);
1756 }
1757 }
1758
Michael Kolb8233fac2010-10-26 16:08:53 -07001759 boolean inForeground() {
1760 return mInForeground;
1761 }
1762
Grace Kloba22ac16e2009-10-07 18:00:23 -07001763 /**
1764 * Return the top window of this tab; either the subwindow if it is not
1765 * null or the main window.
1766 * @return The top window of this tab.
1767 */
1768 WebView getTopWindow() {
1769 if (mSubView != null) {
1770 return mSubView;
1771 }
1772 return mMainView;
1773 }
1774
1775 /**
1776 * Return the main window of this tab. Note: if a tab is freed in the
1777 * background, this can return null. It is only guaranteed to be
1778 * non-null for the current tab.
1779 * @return The main WebView of this tab.
1780 */
1781 WebView getWebView() {
1782 return mMainView;
1783 }
1784
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00001785 /**
1786 * Return the underlying WebViewClassic implementation. As with getWebView,
1787 * this maybe null for background tabs.
1788 * @return The main WebView of this tab.
1789 */
1790 WebViewClassic getWebViewClassic() {
1791 return WebViewClassic.fromWebView(mMainView);
1792 }
1793
Michael Kolba713ec82010-11-29 17:27:06 -08001794 void setViewContainer(View container) {
1795 mContainer = container;
1796 }
1797
Michael Kolb8233fac2010-10-26 16:08:53 -07001798 View getViewContainer() {
1799 return mContainer;
1800 }
1801
Grace Kloba22ac16e2009-10-07 18:00:23 -07001802 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001803 * Return whether private browsing is enabled for the main window of
1804 * this tab.
1805 * @return True if private browsing is enabled.
1806 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001807 boolean isPrivateBrowsingEnabled() {
John Reck502a3532011-08-16 14:21:46 -07001808 return mCurrentState.mIncognito;
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001809 }
1810
1811 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001812 * Return the subwindow of this tab or null if there is no subwindow.
1813 * @return The subwindow of this tab or null.
1814 */
1815 WebView getSubWebView() {
1816 return mSubView;
1817 }
1818
Michael Kolb1514bb72010-11-22 09:11:48 -08001819 void setSubWebView(WebView subView) {
1820 mSubView = subView;
1821 }
1822
Michael Kolb8233fac2010-10-26 16:08:53 -07001823 View getSubViewContainer() {
1824 return mSubViewContainer;
1825 }
1826
Michael Kolb1514bb72010-11-22 09:11:48 -08001827 void setSubViewContainer(View subViewContainer) {
1828 mSubViewContainer = subViewContainer;
1829 }
1830
Grace Kloba22ac16e2009-10-07 18:00:23 -07001831 /**
1832 * @return The geolocation permissions prompt for this tab.
1833 */
1834 GeolocationPermissionsPrompt getGeolocationPermissionsPrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001835 if (mGeolocationPermissionsPrompt == null) {
1836 ViewStub stub = (ViewStub) mContainer
1837 .findViewById(R.id.geolocation_permissions_prompt);
1838 mGeolocationPermissionsPrompt = (GeolocationPermissionsPrompt) stub
1839 .inflate();
Grace Kloba50c241e2010-04-20 11:07:50 -07001840 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001841 return mGeolocationPermissionsPrompt;
1842 }
1843
1844 /**
1845 * @return The application id string
1846 */
1847 String getAppId() {
1848 return mAppId;
1849 }
1850
1851 /**
1852 * Set the application id string
1853 * @param id
1854 */
1855 void setAppId(String id) {
1856 mAppId = id;
1857 }
1858
Michael Kolbe28b3472011-08-04 16:54:31 -07001859 boolean closeOnBack() {
1860 return mCloseOnBack;
1861 }
1862
1863 void setCloseOnBack(boolean close) {
1864 mCloseOnBack = close;
1865 }
1866
Grace Kloba22ac16e2009-10-07 18:00:23 -07001867 String getUrl() {
John Reck324d4402011-01-11 16:56:42 -08001868 return UrlUtils.filteredUrl(mCurrentState.mUrl);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001869 }
1870
John Reck49a603c2011-03-03 09:33:05 -08001871 String getOriginalUrl() {
John Reckdb22ec42011-06-29 11:31:24 -07001872 if (mCurrentState.mOriginalUrl == null) {
1873 return getUrl();
John Reck49a603c2011-03-03 09:33:05 -08001874 }
John Reckdb22ec42011-06-29 11:31:24 -07001875 return UrlUtils.filteredUrl(mCurrentState.mOriginalUrl);
John Reck49a603c2011-03-03 09:33:05 -08001876 }
1877
Grace Kloba22ac16e2009-10-07 18:00:23 -07001878 /**
John Reck30c714c2010-12-16 17:30:34 -08001879 * Get the title of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001880 */
1881 String getTitle() {
John Reck30c714c2010-12-16 17:30:34 -08001882 if (mCurrentState.mTitle == null && mInPageLoad) {
Michael Kolb14612442011-06-24 13:06:29 -07001883 return mContext.getString(R.string.title_bar_loading);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001884 }
John Reck30c714c2010-12-16 17:30:34 -08001885 return mCurrentState.mTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001886 }
1887
1888 /**
John Reck30c714c2010-12-16 17:30:34 -08001889 * Get the favicon of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001890 */
1891 Bitmap getFavicon() {
John Reck1cf4b792011-07-26 10:22:22 -07001892 if (mCurrentState.mFavicon != null) {
1893 return mCurrentState.mFavicon;
1894 }
1895 return getDefaultFavicon(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001896 }
1897
John Recke969cc52010-12-21 17:24:43 -08001898 public boolean isBookmarkedSite() {
1899 return mCurrentState.mIsBookmarkedSite;
1900 }
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001901
Grace Kloba22ac16e2009-10-07 18:00:23 -07001902 /**
1903 * Return the tab's error console. Creates the console if createIfNEcessary
1904 * is true and we haven't already created the console.
1905 * @param createIfNecessary Flag to indicate if the console should be
1906 * created if it has not been already.
1907 * @return The tab's error console, or null if one has not been created and
1908 * createIfNecessary is false.
1909 */
1910 ErrorConsoleView getErrorConsole(boolean createIfNecessary) {
1911 if (createIfNecessary && mErrorConsole == null) {
Michael Kolb14612442011-06-24 13:06:29 -07001912 mErrorConsole = new ErrorConsoleView(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001913 mErrorConsole.setWebView(mMainView);
1914 }
1915 return mErrorConsole;
1916 }
1917
Steve Block08a6f0c2011-10-06 12:12:53 +01001918 /**
1919 * Sets the security state, clears the SSL certificate error and informs
1920 * the controller.
1921 */
Steve Block2466eff2011-10-03 15:33:09 +01001922 private void setSecurityState(SecurityState securityState) {
1923 mCurrentState.mSecurityState = securityState;
Steve Block08a6f0c2011-10-06 12:12:53 +01001924 mCurrentState.mSslCertificateError = null;
Steve Block2466eff2011-10-03 15:33:09 +01001925 mWebViewController.onUpdatedSecurityState(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001926 }
1927
1928 /**
Steve Block2466eff2011-10-03 15:33:09 +01001929 * @return The tab's security state.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001930 */
Steve Block2466eff2011-10-03 15:33:09 +01001931 SecurityState getSecurityState() {
1932 return mCurrentState.mSecurityState;
John Reck30c714c2010-12-16 17:30:34 -08001933 }
1934
Steve Block08a6f0c2011-10-06 12:12:53 +01001935 /**
1936 * Gets the SSL certificate error, if any, for the page's main resource.
1937 * This is only non-null when the security state is
1938 * SECURITY_STATE_BAD_CERTIFICATE.
1939 */
1940 SslError getSslCertificateError() {
1941 return mCurrentState.mSslCertificateError;
1942 }
1943
John Reck30c714c2010-12-16 17:30:34 -08001944 int getLoadProgress() {
1945 if (mInPageLoad) {
1946 return mPageLoadProgress;
1947 }
1948 return 100;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001949 }
1950
1951 /**
1952 * @return TRUE if onPageStarted is called while onPageFinished is not
1953 * called yet.
1954 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001955 boolean inPageLoad() {
1956 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001957 }
1958
1959 // force mInLoad to be false. This should only be called before closing the
1960 // tab to ensure BrowserActivity's pauseWebViewTimers() is called correctly.
Michael Kolb8233fac2010-10-26 16:08:53 -07001961 void clearInPageLoad() {
1962 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001963 }
1964
Grace Kloba22ac16e2009-10-07 18:00:23 -07001965 /**
John Reck1cf4b792011-07-26 10:22:22 -07001966 * @return The Bundle with the tab's state if it can be saved, otherwise null
Grace Kloba22ac16e2009-10-07 18:00:23 -07001967 */
John Reck1cf4b792011-07-26 10:22:22 -07001968 public Bundle saveState() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001969 // If the WebView is null it means we ran low on memory and we already
1970 // stored the saved state in mSavedState.
1971 if (mMainView == null) {
John Reck1cf4b792011-07-26 10:22:22 -07001972 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001973 }
John Reck6c2e2f32011-08-22 13:41:23 -07001974
1975 if (TextUtils.isEmpty(mCurrentState.mUrl)) {
John Reck1cf4b792011-07-26 10:22:22 -07001976 return null;
John Reck24f18262011-06-17 14:47:20 -07001977 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001978
1979 mSavedState = new Bundle();
John Reck6c2e2f32011-08-22 13:41:23 -07001980 WebBackForwardList savedList = mMainView.saveState(mSavedState);
1981 if (savedList == null || savedList.getSize() == 0) {
1982 Log.w(LOGTAG, "Failed to save back/forward list for "
1983 + mCurrentState.mUrl);
1984 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001985
Michael Kolbc831b632011-05-11 09:30:34 -07001986 mSavedState.putLong(ID, mId);
John Reck30c714c2010-12-16 17:30:34 -08001987 mSavedState.putString(CURRURL, mCurrentState.mUrl);
1988 mSavedState.putString(CURRTITLE, mCurrentState.mTitle);
John Reck1cf4b792011-07-26 10:22:22 -07001989 mSavedState.putBoolean(INCOGNITO, mMainView.isPrivateBrowsingEnabled());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001990 if (mAppId != null) {
1991 mSavedState.putString(APPID, mAppId);
1992 }
Michael Kolbe28b3472011-08-04 16:54:31 -07001993 mSavedState.putBoolean(CLOSEFLAG, mCloseOnBack);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001994 // Remember the parent tab so the relationship can be restored.
Michael Kolbc831b632011-05-11 09:30:34 -07001995 if (mParent != null) {
1996 mSavedState.putLong(PARENTTAB, mParent.mId);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001997 }
John Reckb0a86db2011-05-24 14:05:58 -07001998 mSavedState.putBoolean(USERAGENT,
1999 mSettings.hasDesktopUseragent(getWebView()));
John Reck1cf4b792011-07-26 10:22:22 -07002000 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07002001 }
2002
2003 /*
2004 * Restore the state of the tab.
2005 */
John Reck1cf4b792011-07-26 10:22:22 -07002006 private void restoreState(Bundle b) {
2007 mSavedState = b;
2008 if (mSavedState == null) {
2009 return;
Grace Kloba22ac16e2009-10-07 18:00:23 -07002010 }
2011 // Restore the internal state even if the WebView fails to restore.
2012 // This will maintain the app id, original url and close-on-exit values.
Michael Kolbc831b632011-05-11 09:30:34 -07002013 mId = b.getLong(ID);
Grace Kloba22ac16e2009-10-07 18:00:23 -07002014 mAppId = b.getString(APPID);
Michael Kolbe28b3472011-08-04 16:54:31 -07002015 mCloseOnBack = b.getBoolean(CLOSEFLAG);
John Reck8b9bb8b2012-03-08 13:19:40 -08002016 restoreUserAgent();
John Reck1cf4b792011-07-26 10:22:22 -07002017 String url = b.getString(CURRURL);
2018 String title = b.getString(CURRTITLE);
2019 boolean incognito = b.getBoolean(INCOGNITO);
2020 mCurrentState = new PageState(mContext, incognito, url, null);
2021 mCurrentState.mTitle = title;
2022 synchronized (Tab.this) {
2023 if (mCapture != null) {
John Reck4eadc342011-10-31 14:04:10 -07002024 DataController.getInstance(mContext).loadThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07002025 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002026 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002027 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04002028
John Reck8b9bb8b2012-03-08 13:19:40 -08002029 private void restoreUserAgent() {
2030 if (mMainView == null || mSavedState == null) {
2031 return;
2032 }
2033 if (mSavedState.getBoolean(USERAGENT)
2034 != mSettings.hasDesktopUseragent(mMainView)) {
2035 mSettings.toggleDesktopUseragent(mMainView);
2036 }
2037 }
2038
Leon Scroggins1961ed22010-12-07 15:22:21 -05002039 public void updateBookmarkedStatus() {
John Recke969cc52010-12-21 17:24:43 -08002040 mDataController.queryBookmarkStatus(getUrl(), mIsBookmarkCallback);
Leon Scroggins1961ed22010-12-07 15:22:21 -05002041 }
2042
John Recke969cc52010-12-21 17:24:43 -08002043 private DataController.OnQueryUrlIsBookmark mIsBookmarkCallback
2044 = new DataController.OnQueryUrlIsBookmark() {
2045 @Override
2046 public void onQueryUrlIsBookmark(String url, boolean isBookmark) {
2047 if (mCurrentState.mUrl.equals(url)) {
2048 mCurrentState.mIsBookmarkedSite = isBookmark;
2049 mWebViewController.bookmarkedStatusHasChanged(Tab.this);
2050 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05002051 }
John Recke969cc52010-12-21 17:24:43 -08002052 };
Michael Kolb1acef692011-03-08 14:12:06 -08002053
Michael Kolbeb95db42011-03-03 10:38:40 -08002054 public Bitmap getScreenshot() {
John Reck1cf4b792011-07-26 10:22:22 -07002055 synchronized (Tab.this) {
2056 return mCapture;
2057 }
Michael Kolbeb95db42011-03-03 10:38:40 -08002058 }
2059
John Reck541f55a2011-06-07 16:34:43 -07002060 public boolean isSnapshot() {
John Reck541f55a2011-06-07 16:34:43 -07002061 return false;
2062 }
2063
John Reck68234a92012-04-19 15:27:12 -07002064 private static class SaveCallback implements ValueCallback<Boolean> {
2065 boolean mResult;
2066
2067 @Override
2068 public void onReceiveValue(Boolean value) {
2069 mResult = value;
2070 synchronized (this) {
2071 notifyAll();
John Reck8cc92352011-07-06 17:41:52 -07002072 }
John Reck541f55a2011-06-07 16:34:43 -07002073 }
John Reck68234a92012-04-19 15:27:12 -07002074
2075 }
2076
2077 /**
2078 * Must be called on the UI thread
2079 */
2080 public ContentValues createSnapshotValues() {
2081 WebViewClassic web = getWebViewClassic();
2082 if (web == null) return null;
John Reckd8c74522011-06-14 08:45:00 -07002083 ContentValues values = new ContentValues();
2084 values.put(Snapshots.TITLE, mCurrentState.mTitle);
2085 values.put(Snapshots.URL, mCurrentState.mUrl);
John Reck68234a92012-04-19 15:27:12 -07002086 values.put(Snapshots.BACKGROUND, web.getPageBackgroundColor());
John Reck8cc92352011-07-06 17:41:52 -07002087 values.put(Snapshots.DATE_CREATED, System.currentTimeMillis());
2088 values.put(Snapshots.FAVICON, compressBitmap(getFavicon()));
2089 Bitmap screenshot = Controller.createScreenshot(mMainView,
2090 Controller.getDesiredThumbnailWidth(mContext),
2091 Controller.getDesiredThumbnailHeight(mContext));
2092 values.put(Snapshots.THUMBNAIL, compressBitmap(screenshot));
John Reckd8c74522011-06-14 08:45:00 -07002093 return values;
John Reck541f55a2011-06-07 16:34:43 -07002094 }
2095
John Reck68234a92012-04-19 15:27:12 -07002096 /**
2097 * Probably want to call this on a background thread
2098 */
2099 public boolean saveViewState(ContentValues values) {
2100 WebViewClassic web = getWebViewClassic();
2101 if (web == null) return false;
2102 String path = UUID.randomUUID().toString();
2103 SaveCallback callback = new SaveCallback();
2104 OutputStream outs = null;
2105 try {
2106 outs = mContext.openFileOutput(path, Context.MODE_PRIVATE);
2107 GZIPOutputStream stream = new GZIPOutputStream(outs);
2108 synchronized (callback) {
2109 web.saveViewState(stream, callback);
2110 callback.wait();
2111 }
2112 stream.flush();
2113 stream.close();
2114 } catch (Exception e) {
2115 Log.w(LOGTAG, "Failed to save view state", e);
2116 if (outs != null) {
2117 try {
2118 outs.close();
2119 } catch (IOException ignore) {}
2120 }
2121 File file = mContext.getFileStreamPath(path);
2122 if (file.exists() && !file.delete()) {
2123 file.deleteOnExit();
2124 }
2125 return false;
2126 }
2127 File savedFile = mContext.getFileStreamPath(path);
2128 if (!callback.mResult) {
2129 if (!savedFile.delete()) {
2130 savedFile.deleteOnExit();
2131 }
2132 return false;
2133 }
2134 long size = savedFile.length();
2135 values.put(Snapshots.VIEWSTATE_PATH, path);
2136 values.put(Snapshots.VIEWSTATE_SIZE, size);
2137 return true;
2138 }
2139
John Reck8cc92352011-07-06 17:41:52 -07002140 public byte[] compressBitmap(Bitmap bitmap) {
2141 if (bitmap == null) {
2142 return null;
2143 }
2144 ByteArrayOutputStream stream = new ByteArrayOutputStream();
2145 bitmap.compress(CompressFormat.PNG, 100, stream);
2146 return stream.toByteArray();
2147 }
2148
John Reck26b18322011-06-21 13:08:58 -07002149 public void loadUrl(String url, Map<String, String> headers) {
2150 if (mMainView != null) {
Michael Kolba53c9892011-10-05 13:31:40 -07002151 mPageLoadProgress = INITIAL_PROGRESS;
2152 mInPageLoad = true;
Michael Kolb14612442011-06-24 13:06:29 -07002153 mCurrentState = new PageState(mContext, false, url, null);
John Reck26b18322011-06-21 13:08:58 -07002154 mWebViewController.onPageStarted(this, mMainView, null);
2155 mMainView.loadUrl(url, headers);
2156 }
2157 }
2158
Michael Kolb9ef259a2011-07-12 15:33:08 -07002159 protected void capture() {
2160 if (mMainView == null || mCapture == null) return;
John Reck4eadc342011-10-31 14:04:10 -07002161 if (mMainView.getContentWidth() <= 0 || mMainView.getContentHeight() <= 0) {
2162 return;
2163 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07002164 Canvas c = new Canvas(mCapture);
2165 final int left = mMainView.getScrollX();
2166 final int top = mMainView.getScrollY() + mMainView.getVisibleTitleHeight();
Michael Kolba3194d02011-09-07 11:23:51 -07002167 int state = c.save();
Michael Kolb9ef259a2011-07-12 15:33:08 -07002168 c.translate(-left, -top);
2169 float scale = mCaptureWidth / (float) mMainView.getWidth();
2170 c.scale(scale, scale, left, top);
John Reck8ee633f2011-08-09 16:00:35 -07002171 if (mMainView instanceof BrowserWebView) {
2172 ((BrowserWebView)mMainView).drawContent(c);
2173 } else {
2174 mMainView.draw(c);
2175 }
Michael Kolba3194d02011-09-07 11:23:51 -07002176 c.restoreToCount(state);
2177 // manually anti-alias the edges for the tilt
2178 c.drawRect(0, 0, 1, mCapture.getHeight(), sAlphaPaint);
2179 c.drawRect(mCapture.getWidth() - 1, 0, mCapture.getWidth(),
2180 mCapture.getHeight(), sAlphaPaint);
2181 c.drawRect(0, 0, mCapture.getWidth(), 1, sAlphaPaint);
2182 c.drawRect(0, mCapture.getHeight() - 1, mCapture.getWidth(),
2183 mCapture.getHeight(), sAlphaPaint);
Dianne Hackborn43cfe8a2011-08-02 16:59:35 -07002184 c.setBitmap(null);
John Reck8ee633f2011-08-09 16:00:35 -07002185 mHandler.removeMessages(MSG_CAPTURE);
John Reck1cf4b792011-07-26 10:22:22 -07002186 persistThumbnail();
John Reck8ee633f2011-08-09 16:00:35 -07002187 TabControl tc = mWebViewController.getTabControl();
2188 if (tc != null) {
2189 OnThumbnailUpdatedListener updateListener
2190 = tc.getOnThumbnailUpdatedListener();
2191 if (updateListener != null) {
2192 updateListener.onThumbnailUpdated(this);
2193 }
2194 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07002195 }
2196
2197 @Override
2198 public void onNewPicture(WebView view, Picture picture) {
2199 //update screenshot
John Reck1cf4b792011-07-26 10:22:22 -07002200 postCapture();
2201 }
2202
2203 private void postCapture() {
Michael Kolb9ef259a2011-07-12 15:33:08 -07002204 if (!mHandler.hasMessages(MSG_CAPTURE)) {
2205 mHandler.sendEmptyMessageDelayed(MSG_CAPTURE, CAPTURE_DELAY);
2206 }
2207 }
2208
John Reckef654f12011-07-12 16:42:08 -07002209 public boolean canGoBack() {
2210 return mMainView != null ? mMainView.canGoBack() : false;
2211 }
2212
2213 public boolean canGoForward() {
2214 return mMainView != null ? mMainView.canGoForward() : false;
2215 }
2216
2217 public void goBack() {
2218 if (mMainView != null) {
2219 mMainView.goBack();
2220 }
2221 }
2222
2223 public void goForward() {
2224 if (mMainView != null) {
2225 mMainView.goForward();
2226 }
2227 }
2228
Mathew Inwood1dd8e822011-08-03 14:34:29 +01002229 /**
2230 * Causes the tab back/forward stack to be cleared once, if the given URL is the next URL
2231 * to be added to the stack.
2232 *
2233 * This is used to ensure that preloaded URLs that are not subsequently seen by the user do
2234 * not appear in the back stack.
2235 */
Mathew Inwooda829d552011-09-02 14:16:25 +01002236 public void clearBackStackWhenItemAdded(Pattern urlPattern) {
2237 mClearHistoryUrlPattern = urlPattern;
Mathew Inwood1dd8e822011-08-03 14:34:29 +01002238 }
2239
John Reck1cf4b792011-07-26 10:22:22 -07002240 protected void persistThumbnail() {
John Reck4eadc342011-10-31 14:04:10 -07002241 DataController.getInstance(mContext).saveThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07002242 }
2243
2244 protected void deleteThumbnail() {
John Reck4eadc342011-10-31 14:04:10 -07002245 DataController.getInstance(mContext).deleteThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07002246 }
2247
John Reck4eadc342011-10-31 14:04:10 -07002248 void updateCaptureFromBlob(byte[] blob) {
John Reck1cf4b792011-07-26 10:22:22 -07002249 synchronized (Tab.this) {
2250 if (mCapture == null) {
2251 return;
2252 }
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002253 ByteBuffer buffer = ByteBuffer.wrap(blob);
Michael Kolbd837a112011-08-09 14:00:18 -07002254 try {
2255 mCapture.copyPixelsFromBuffer(buffer);
2256 } catch (RuntimeException rex) {
2257 Log.e(LOGTAG, "Load capture has mismatched sizes; buffer: "
2258 + buffer.capacity() + " blob: " + blob.length
2259 + "capture: " + mCapture.getByteCount());
2260 throw rex;
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002261 }
John Reck1cf4b792011-07-26 10:22:22 -07002262 }
2263 }
2264
John Reck52be4782011-08-26 15:37:29 -07002265 @Override
2266 public String toString() {
2267 StringBuilder builder = new StringBuilder(100);
2268 builder.append(mId);
2269 builder.append(") has parent: ");
2270 if (getParent() != null) {
2271 builder.append("true[");
2272 builder.append(getParent().getId());
2273 builder.append("]");
2274 } else {
2275 builder.append("false");
2276 }
2277 builder.append(", incog: ");
2278 builder.append(isPrivateBrowsingEnabled());
2279 if (!isPrivateBrowsingEnabled()) {
2280 builder.append(", title: ");
2281 builder.append(getTitle());
2282 builder.append(", url: ");
2283 builder.append(getUrl());
2284 }
2285 return builder.toString();
2286 }
2287
Steve Block4895b012011-10-03 16:26:46 +01002288 private void handleProceededAfterSslError(SslError error) {
2289 if (error.getUrl().equals(mCurrentState.mUrl)) {
2290 // The security state should currently be SECURITY_STATE_SECURE.
2291 setSecurityState(SecurityState.SECURITY_STATE_BAD_CERTIFICATE);
Steve Block08a6f0c2011-10-06 12:12:53 +01002292 mCurrentState.mSslCertificateError = error;
Steve Block4895b012011-10-03 16:26:46 +01002293 } else if (getSecurityState() == SecurityState.SECURITY_STATE_SECURE) {
Steve Block08a6f0c2011-10-06 12:12:53 +01002294 // The page's main resource is secure and this error is for a
2295 // sub-resource.
Steve Block4895b012011-10-03 16:26:46 +01002296 setSecurityState(SecurityState.SECURITY_STATE_MIXED);
2297 }
2298 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002299}