blob: 04bee08dd7e4aa7f4a2c821b4c32e36360d094f2 [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;
80import java.io.OutputStream;
John Reck1cf4b792011-07-26 10:22:22 -070081import java.nio.ByteBuffer;
Michael Kolbfe251992010-07-08 15:41:55 -070082import java.util.ArrayList;
83import java.util.HashMap;
84import java.util.Iterator;
85import java.util.LinkedList;
86import java.util.Map;
John Reck2b71d6d2012-04-18 17:42:06 -070087import java.util.UUID;
Michael Kolbfe251992010-07-08 15:41:55 -070088import java.util.Vector;
Mathew Inwooda829d552011-09-02 14:16:25 +010089import java.util.regex.Pattern;
John Reck8cc92352011-07-06 17:41:52 -070090import java.util.zip.GZIPOutputStream;
Michael Kolbfe251992010-07-08 15:41:55 -070091
Grace Kloba22ac16e2009-10-07 18:00:23 -070092/**
93 * Class for maintaining Tabs with a main WebView and a subwindow.
94 */
Michael Kolb9ef259a2011-07-12 15:33:08 -070095class Tab implements PictureListener {
Michael Kolb8233fac2010-10-26 16:08:53 -070096
Grace Kloba22ac16e2009-10-07 18:00:23 -070097 // Log Tag
98 private static final String LOGTAG = "Tab";
Mathew Inwood1dd8e822011-08-03 14:34:29 +010099 private static final boolean LOGD_ENABLED = com.android.browser.Browser.LOGD_ENABLED;
Ben Murdochc42addf2010-01-28 15:19:59 +0000100 // Special case the logtag for messages for the Console to make it easier to
101 // filter them and match the logtag used for these messages in older versions
102 // of the browser.
103 private static final String CONSOLE_LOGTAG = "browser";
104
Michael Kolb9ef259a2011-07-12 15:33:08 -0700105 private static final int MSG_CAPTURE = 42;
John Reck8ee633f2011-08-09 16:00:35 -0700106 private static final int CAPTURE_DELAY = 100;
Michael Kolba53c9892011-10-05 13:31:40 -0700107 private static final int INITIAL_PROGRESS = 5;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700108
John Reck1cf4b792011-07-26 10:22:22 -0700109 private static Bitmap sDefaultFavicon;
110
Michael Kolba3194d02011-09-07 11:23:51 -0700111 private static Paint sAlphaPaint = new Paint();
112 static {
113 sAlphaPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
114 sAlphaPaint.setColor(Color.TRANSPARENT);
115 }
116
Steve Block2466eff2011-10-03 15:33:09 +0100117 public enum SecurityState {
Steve Block4895b012011-10-03 16:26:46 +0100118 // The page's main resource does not use SSL. Note that we use this
119 // state irrespective of the SSL authentication state of sub-resources.
Steve Block2466eff2011-10-03 15:33:09 +0100120 SECURITY_STATE_NOT_SECURE,
Steve Block4895b012011-10-03 16:26:46 +0100121 // The page's main resource uses SSL and the certificate is good. The
122 // same is true of all sub-resources.
Steve Block2466eff2011-10-03 15:33:09 +0100123 SECURITY_STATE_SECURE,
Steve Block4895b012011-10-03 16:26:46 +0100124 // The page's main resource uses SSL and the certificate is good, but
125 // some sub-resources either do not use SSL or have problems with their
126 // certificates.
Steve Block2466eff2011-10-03 15:33:09 +0100127 SECURITY_STATE_MIXED,
Steve Block4895b012011-10-03 16:26:46 +0100128 // The page's main resource uses SSL but there is a problem with its
129 // certificate.
130 SECURITY_STATE_BAD_CERTIFICATE,
John Reck30c714c2010-12-16 17:30:34 -0800131 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700132
Michael Kolb14612442011-06-24 13:06:29 -0700133 Context mContext;
John Reckd8c74522011-06-14 08:45:00 -0700134 protected WebViewController mWebViewController;
Michael Kolb8233fac2010-10-26 16:08:53 -0700135
Michael Kolbc831b632011-05-11 09:30:34 -0700136 // The tab ID
John Reckd8c74522011-06-14 08:45:00 -0700137 private long mId = -1;
Michael Kolbc831b632011-05-11 09:30:34 -0700138
Grace Kloba22ac16e2009-10-07 18:00:23 -0700139 // The Geolocation permissions prompt
140 private GeolocationPermissionsPrompt mGeolocationPermissionsPrompt;
141 // Main WebView wrapper
Michael Kolba713ec82010-11-29 17:27:06 -0800142 private View mContainer;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700143 // Main WebView
144 private WebView mMainView;
145 // Subwindow container
146 private View mSubViewContainer;
147 // Subwindow WebView
148 private WebView mSubView;
149 // Saved bundle for when we are running low on memory. It contains the
150 // information needed to restore the WebView if the user goes back to the
151 // tab.
152 private Bundle mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700153 // Parent Tab. This is the Tab that created this Tab, or null if the Tab was
154 // created by the UI
Michael Kolbc831b632011-05-11 09:30:34 -0700155 private Tab mParent;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700156 // Tab that constructed by this Tab. This is used when this Tab is
157 // destroyed, it clears all mParentTab values in the children.
Michael Kolbc831b632011-05-11 09:30:34 -0700158 private Vector<Tab> mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700159 // If true, the tab is in the foreground of the current activity.
160 private boolean mInForeground;
Michael Kolb8233fac2010-10-26 16:08:53 -0700161 // If true, the tab is in page loading state (after onPageStarted,
162 // before onPageFinsihed)
163 private boolean mInPageLoad;
John Reck30c714c2010-12-16 17:30:34 -0800164 // The last reported progress of the current page
165 private int mPageLoadProgress;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000166 // The time the load started, used to find load page time
167 private long mLoadStartTime;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700168 // Application identifier used to find tabs that another application wants
169 // to reuse.
170 private String mAppId;
Michael Kolbe28b3472011-08-04 16:54:31 -0700171 // flag to indicate if tab should be closed on back
172 private boolean mCloseOnBack;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700173 // Keep the original url around to avoid killing the old WebView if the url
174 // has not changed.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700175 // Error console for the tab
176 private ErrorConsoleView mErrorConsole;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -0500177 // The listener that gets invoked when a download is started from the
178 // mMainView
179 private final DownloadListener mDownloadListener;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500180 // Listener used to know when we move forward or back in the history list.
181 private final WebBackForwardListClient mWebBackForwardListClient;
John Recke969cc52010-12-21 17:24:43 -0800182 private DataController mDataController;
Patrick Scott92066772011-03-10 08:46:27 -0500183 // State of the auto-login request.
184 private DeviceAccountLogin mDeviceAccountLogin;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700185
186 // AsyncTask for downloading touch icons
187 DownloadTouchIcon mTouchIconLoader;
188
John Reck35e9dd62011-04-25 09:01:54 -0700189 private BrowserSettings mSettings;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700190 private int mCaptureWidth;
191 private int mCaptureHeight;
192 private Bitmap mCapture;
193 private Handler mHandler;
194
Mathew Inwood1dd8e822011-08-03 14:34:29 +0100195 /**
Mathew Inwood9ad1eac2011-09-15 11:29:50 +0100196 * See {@link #clearBackStackWhenItemAdded(String)}.
Mathew Inwood1dd8e822011-08-03 14:34:29 +0100197 */
Mathew Inwooda829d552011-09-02 14:16:25 +0100198 private Pattern mClearHistoryUrlPattern;
Mathew Inwood1dd8e822011-08-03 14:34:29 +0100199
John Reck1cf4b792011-07-26 10:22:22 -0700200 private static synchronized Bitmap getDefaultFavicon(Context context) {
201 if (sDefaultFavicon == null) {
202 sDefaultFavicon = BitmapFactory.decodeResource(
203 context.getResources(), R.drawable.app_web_browser_sm);
204 }
205 return sDefaultFavicon;
206 }
Michael Kolbeb95db42011-03-03 10:38:40 -0800207
John Reck30c714c2010-12-16 17:30:34 -0800208 // All the state needed for a page
John Reckd8c74522011-06-14 08:45:00 -0700209 protected static class PageState {
John Reck30c714c2010-12-16 17:30:34 -0800210 String mUrl;
John Reckdb22ec42011-06-29 11:31:24 -0700211 String mOriginalUrl;
John Reck30c714c2010-12-16 17:30:34 -0800212 String mTitle;
Steve Block2466eff2011-10-03 15:33:09 +0100213 SecurityState mSecurityState;
Steve Block08a6f0c2011-10-06 12:12:53 +0100214 // This is non-null only when mSecurityState is SECURITY_STATE_BAD_CERTIFICATE.
215 SslError mSslCertificateError;
John Reck30c714c2010-12-16 17:30:34 -0800216 Bitmap mFavicon;
Steve Block08a6f0c2011-10-06 12:12:53 +0100217 boolean mIsBookmarkedSite;
218 boolean mIncognito;
John Reck30c714c2010-12-16 17:30:34 -0800219
220 PageState(Context c, boolean incognito) {
John Reck502a3532011-08-16 14:21:46 -0700221 mIncognito = incognito;
222 if (mIncognito) {
John Reckdb22ec42011-06-29 11:31:24 -0700223 mOriginalUrl = mUrl = "browser:incognito";
John Reck30c714c2010-12-16 17:30:34 -0800224 mTitle = c.getString(R.string.new_incognito_tab);
John Reck30c714c2010-12-16 17:30:34 -0800225 } else {
John Reckdb22ec42011-06-29 11:31:24 -0700226 mOriginalUrl = mUrl = "";
John Reck30c714c2010-12-16 17:30:34 -0800227 mTitle = c.getString(R.string.new_tab);
John Reck30c714c2010-12-16 17:30:34 -0800228 }
Steve Block2466eff2011-10-03 15:33:09 +0100229 mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
John Reck30c714c2010-12-16 17:30:34 -0800230 }
231
232 PageState(Context c, boolean incognito, String url, Bitmap favicon) {
John Reck502a3532011-08-16 14:21:46 -0700233 mIncognito = incognito;
John Reckdb22ec42011-06-29 11:31:24 -0700234 mOriginalUrl = mUrl = url;
John Reck30c714c2010-12-16 17:30:34 -0800235 if (URLUtil.isHttpsUrl(url)) {
Steve Block2466eff2011-10-03 15:33:09 +0100236 mSecurityState = SecurityState.SECURITY_STATE_SECURE;
John Reck30c714c2010-12-16 17:30:34 -0800237 } else {
Steve Block2466eff2011-10-03 15:33:09 +0100238 mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
John Reck30c714c2010-12-16 17:30:34 -0800239 }
John Reck1cf4b792011-07-26 10:22:22 -0700240 mFavicon = favicon;
John Reck30c714c2010-12-16 17:30:34 -0800241 }
John Reck1cf4b792011-07-26 10:22:22 -0700242
Grace Kloba22ac16e2009-10-07 18:00:23 -0700243 }
244
John Reck30c714c2010-12-16 17:30:34 -0800245 // The current/loading page's state
John Reckd8c74522011-06-14 08:45:00 -0700246 protected PageState mCurrentState;
John Reck30c714c2010-12-16 17:30:34 -0800247
Grace Kloba22ac16e2009-10-07 18:00:23 -0700248 // Used for saving and restoring each Tab
Michael Kolbc831b632011-05-11 09:30:34 -0700249 static final String ID = "ID";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700250 static final String CURRURL = "currentUrl";
251 static final String CURRTITLE = "currentTitle";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700252 static final String PARENTTAB = "parentTab";
253 static final String APPID = "appid";
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700254 static final String INCOGNITO = "privateBrowsingEnabled";
John Reckb0a86db2011-05-24 14:05:58 -0700255 static final String USERAGENT = "useragent";
Michael Kolbe28b3472011-08-04 16:54:31 -0700256 static final String CLOSEFLAG = "closeOnBack";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700257
258 // -------------------------------------------------------------------------
259
Leon Scroggins58d56c62010-01-28 15:12:40 -0500260 /**
261 * Private information regarding the latest voice search. If the Tab is not
262 * in voice search mode, this will be null.
263 */
264 private VoiceSearchData mVoiceSearchData;
265 /**
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400266 * Remove voice search mode from this tab.
267 */
268 public void revertVoiceSearchMode() {
269 if (mVoiceSearchData != null) {
270 mVoiceSearchData = null;
271 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700272 mWebViewController.revertVoiceSearchMode(this);
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400273 }
274 }
275 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700276
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400277 /**
Leon Scroggins58d56c62010-01-28 15:12:40 -0500278 * Return whether the tab is in voice search mode.
279 */
280 public boolean isInVoiceSearchMode() {
281 return mVoiceSearchData != null;
282 }
283 /**
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400284 * Return true if the Tab is in voice search mode and the voice search
285 * Intent came with a String identifying that Google provided the Intent.
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500286 */
287 public boolean voiceSearchSourceIsGoogle() {
288 return mVoiceSearchData != null && mVoiceSearchData.mSourceIsGoogle;
289 }
290 /**
Leon Scroggins58d56c62010-01-28 15:12:40 -0500291 * Get the title to display for the current voice search page. If the Tab
292 * is not in voice search mode, return null.
293 */
294 public String getVoiceDisplayTitle() {
295 if (mVoiceSearchData == null) return null;
296 return mVoiceSearchData.mLastVoiceSearchTitle;
297 }
298 /**
299 * Get the latest array of voice search results, to be passed to the
300 * BrowserProvider. If the Tab is not in voice search mode, return null.
301 */
302 public ArrayList<String> getVoiceSearchResults() {
303 if (mVoiceSearchData == null) return null;
304 return mVoiceSearchData.mVoiceSearchResults;
305 }
306 /**
307 * Activate voice search mode.
308 * @param intent Intent which has the results to use, or an index into the
309 * results when reusing the old results.
310 */
311 /* package */ void activateVoiceSearchMode(Intent intent) {
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500312 int index = 0;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500313 ArrayList<String> results = intent.getStringArrayListExtra(
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -0500314 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_STRINGS);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500315 if (results != null) {
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500316 ArrayList<String> urls = intent.getStringArrayListExtra(
317 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_URLS);
318 ArrayList<String> htmls = intent.getStringArrayListExtra(
319 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_HTML);
320 ArrayList<String> baseUrls = intent.getStringArrayListExtra(
321 RecognizerResultsIntent
322 .EXTRA_VOICE_SEARCH_RESULT_HTML_BASE_URLS);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500323 // This tab is now entering voice search mode for the first time, or
324 // a new voice search was done.
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500325 int size = results.size();
326 if (urls == null || size != urls.size()) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500327 throw new AssertionError("improper extras passed in Intent");
328 }
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500329 if (htmls == null || htmls.size() != size || baseUrls == null ||
330 (baseUrls.size() != size && baseUrls.size() != 1)) {
331 // If either of these arrays are empty/incorrectly sized, ignore
332 // them.
333 htmls = null;
334 baseUrls = null;
335 }
336 mVoiceSearchData = new VoiceSearchData(results, urls, htmls,
337 baseUrls);
Leon Scroggins9df94972010-03-08 18:20:35 -0500338 mVoiceSearchData.mHeaders = intent.getParcelableArrayListExtra(
339 RecognizerResultsIntent
340 .EXTRA_VOICE_SEARCH_RESULT_HTTP_HEADERS);
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500341 mVoiceSearchData.mSourceIsGoogle = intent.getBooleanExtra(
342 VoiceSearchData.SOURCE_IS_GOOGLE, false);
Leon Scroggins2ee4a5a2010-03-15 16:56:57 -0400343 mVoiceSearchData.mVoiceSearchIntent = new Intent(intent);
Leon Scrogginse10dde52010-03-08 19:53:03 -0500344 }
345 String extraData = intent.getStringExtra(
346 SearchManager.EXTRA_DATA_KEY);
347 if (extraData != null) {
348 index = Integer.parseInt(extraData);
349 if (index >= mVoiceSearchData.mVoiceSearchResults.size()) {
350 throw new AssertionError("index must be less than "
351 + "size of mVoiceSearchResults");
352 }
353 if (mVoiceSearchData.mSourceIsGoogle) {
354 Intent logIntent = new Intent(
355 LoggingEvents.ACTION_LOG_EVENT);
356 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
357 LoggingEvents.VoiceSearch.N_BEST_CHOOSE);
358 logIntent.putExtra(
359 LoggingEvents.VoiceSearch.EXTRA_N_BEST_CHOOSE_INDEX,
360 index);
Michael Kolb14612442011-06-24 13:06:29 -0700361 mContext.sendBroadcast(logIntent);
Leon Scrogginse10dde52010-03-08 19:53:03 -0500362 }
363 if (mVoiceSearchData.mVoiceSearchIntent != null) {
Leon Scroggins2ee4a5a2010-03-15 16:56:57 -0400364 // Copy the Intent, so that each history item will have its own
365 // Intent, with different (or none) extra data.
366 Intent latest = new Intent(mVoiceSearchData.mVoiceSearchIntent);
367 latest.putExtra(SearchManager.EXTRA_DATA_KEY, extraData);
368 mVoiceSearchData.mVoiceSearchIntent = latest;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500369 }
370 }
371 mVoiceSearchData.mLastVoiceSearchTitle
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500372 = mVoiceSearchData.mVoiceSearchResults.get(index);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500373 if (mInForeground) {
Michael Kolb11d19782011-03-20 10:17:40 -0700374 mWebViewController.activateVoiceSearchMode(
375 mVoiceSearchData.mLastVoiceSearchTitle,
376 mVoiceSearchData.mVoiceSearchResults);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500377 }
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500378 if (mVoiceSearchData.mVoiceSearchHtmls != null) {
379 // When index was found it was already ensured that it was valid
380 String uriString = mVoiceSearchData.mVoiceSearchHtmls.get(index);
381 if (uriString != null) {
382 Uri dataUri = Uri.parse(uriString);
383 if (RecognizerResultsIntent.URI_SCHEME_INLINE.equals(
384 dataUri.getScheme())) {
385 // If there is only one base URL, use it. If there are
386 // more, there will be one for each index, so use the base
387 // URL corresponding to the index.
388 String baseUrl = mVoiceSearchData.mVoiceSearchBaseUrls.get(
389 mVoiceSearchData.mVoiceSearchBaseUrls.size() > 1 ?
390 index : 0);
391 mVoiceSearchData.mLastVoiceSearchUrl = baseUrl;
392 mMainView.loadDataWithBaseURL(baseUrl,
393 uriString.substring(RecognizerResultsIntent
394 .URI_SCHEME_INLINE.length() + 1), "text/html",
395 "utf-8", baseUrl);
396 return;
397 }
398 }
399 }
Leon Scroggins58d56c62010-01-28 15:12:40 -0500400 mVoiceSearchData.mLastVoiceSearchUrl
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500401 = mVoiceSearchData.mVoiceSearchUrls.get(index);
402 if (null == mVoiceSearchData.mLastVoiceSearchUrl) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700403 mVoiceSearchData.mLastVoiceSearchUrl = UrlUtils.smartUrlFilter(
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500404 mVoiceSearchData.mLastVoiceSearchTitle);
405 }
Leon Scroggins9df94972010-03-08 18:20:35 -0500406 Map<String, String> headers = null;
407 if (mVoiceSearchData.mHeaders != null) {
408 int bundleIndex = mVoiceSearchData.mHeaders.size() == 1 ? 0
409 : index;
410 Bundle bundle = mVoiceSearchData.mHeaders.get(bundleIndex);
411 if (bundle != null && !bundle.isEmpty()) {
412 Iterator<String> iter = bundle.keySet().iterator();
413 headers = new HashMap<String, String>();
414 while (iter.hasNext()) {
415 String key = iter.next();
416 headers.put(key, bundle.getString(key));
417 }
418 }
419 }
420 mMainView.loadUrl(mVoiceSearchData.mLastVoiceSearchUrl, headers);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500421 }
422 /* package */ static class VoiceSearchData {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500423 public VoiceSearchData(ArrayList<String> results,
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500424 ArrayList<String> urls, ArrayList<String> htmls,
425 ArrayList<String> baseUrls) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500426 mVoiceSearchResults = results;
427 mVoiceSearchUrls = urls;
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500428 mVoiceSearchHtmls = htmls;
429 mVoiceSearchBaseUrls = baseUrls;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500430 }
431 /*
432 * ArrayList of suggestions to be displayed when opening the
433 * SearchManager
434 */
435 public ArrayList<String> mVoiceSearchResults;
436 /*
437 * ArrayList of urls, associated with the suggestions in
438 * mVoiceSearchResults.
439 */
440 public ArrayList<String> mVoiceSearchUrls;
441 /*
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500442 * ArrayList holding content to load for each item in
443 * mVoiceSearchResults.
444 */
445 public ArrayList<String> mVoiceSearchHtmls;
446 /*
447 * ArrayList holding base urls for the items in mVoiceSearchResults.
448 * If non null, this will either have the same size as
449 * mVoiceSearchResults or have a size of 1, in which case all will use
450 * the same base url
451 */
452 public ArrayList<String> mVoiceSearchBaseUrls;
453 /*
Leon Scroggins58d56c62010-01-28 15:12:40 -0500454 * The last url provided by voice search. Used for comparison to see if
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500455 * we are going to a page by some method besides voice search.
Leon Scroggins58d56c62010-01-28 15:12:40 -0500456 */
457 public String mLastVoiceSearchUrl;
458 /**
459 * The last title used for voice search. Needed to update the title bar
460 * when switching tabs.
461 */
462 public String mLastVoiceSearchTitle;
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500463 /**
464 * Whether the Intent which turned on voice search mode contained the
465 * String signifying that Google was the source.
466 */
467 public boolean mSourceIsGoogle;
468 /**
Leon Scroggins9df94972010-03-08 18:20:35 -0500469 * List of headers to be passed into the WebView containing location
470 * information
471 */
472 public ArrayList<Bundle> mHeaders;
473 /**
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500474 * The Intent used to invoke voice search. Placed on the
475 * WebHistoryItem so that when coming back to a previous voice search
476 * page we can again activate voice search.
477 */
Leon Scrogginse10dde52010-03-08 19:53:03 -0500478 public Intent mVoiceSearchIntent;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500479 /**
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500480 * String used to identify Google as the source of voice search.
481 */
482 public static String SOURCE_IS_GOOGLE
483 = "android.speech.extras.SOURCE_IS_GOOGLE";
Leon Scroggins58d56c62010-01-28 15:12:40 -0500484 }
485
Grace Kloba22ac16e2009-10-07 18:00:23 -0700486 // Container class for the next error dialog that needs to be displayed
487 private class ErrorDialog {
488 public final int mTitle;
489 public final String mDescription;
490 public final int mError;
491 ErrorDialog(int title, String desc, int error) {
492 mTitle = title;
493 mDescription = desc;
494 mError = error;
495 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700496 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700497
498 private void processNextError() {
499 if (mQueuedErrors == null) {
500 return;
501 }
502 // The first one is currently displayed so just remove it.
503 mQueuedErrors.removeFirst();
504 if (mQueuedErrors.size() == 0) {
505 mQueuedErrors = null;
506 return;
507 }
508 showError(mQueuedErrors.getFirst());
509 }
510
511 private DialogInterface.OnDismissListener mDialogListener =
512 new DialogInterface.OnDismissListener() {
513 public void onDismiss(DialogInterface d) {
514 processNextError();
515 }
516 };
517 private LinkedList<ErrorDialog> mQueuedErrors;
518
519 private void queueError(int err, String desc) {
520 if (mQueuedErrors == null) {
521 mQueuedErrors = new LinkedList<ErrorDialog>();
522 }
523 for (ErrorDialog d : mQueuedErrors) {
524 if (d.mError == err) {
525 // Already saw a similar error, ignore the new one.
526 return;
527 }
528 }
529 ErrorDialog errDialog = new ErrorDialog(
530 err == WebViewClient.ERROR_FILE_NOT_FOUND ?
531 R.string.browserFrameFileErrorLabel :
532 R.string.browserFrameNetworkErrorLabel,
533 desc, err);
534 mQueuedErrors.addLast(errDialog);
535
536 // Show the dialog now if the queue was empty and it is in foreground
537 if (mQueuedErrors.size() == 1 && mInForeground) {
538 showError(errDialog);
539 }
540 }
541
542 private void showError(ErrorDialog errDialog) {
543 if (mInForeground) {
Michael Kolb14612442011-06-24 13:06:29 -0700544 AlertDialog d = new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700545 .setTitle(errDialog.mTitle)
546 .setMessage(errDialog.mDescription)
547 .setPositiveButton(R.string.ok, null)
548 .create();
549 d.setOnDismissListener(mDialogListener);
550 d.show();
551 }
552 }
553
554 // -------------------------------------------------------------------------
555 // WebViewClient implementation for the main WebView
556 // -------------------------------------------------------------------------
557
558 private final WebViewClient mWebViewClient = new WebViewClient() {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500559 private Message mDontResend;
560 private Message mResend;
Michael Kolb47bd1e42011-09-01 15:25:00 -0700561
562 private boolean providersDiffer(String url, String otherUrl) {
563 Uri uri1 = Uri.parse(url);
564 Uri uri2 = Uri.parse(otherUrl);
565 return !uri1.getEncodedAuthority().equals(uri2.getEncodedAuthority());
566 }
567
Grace Kloba22ac16e2009-10-07 18:00:23 -0700568 @Override
569 public void onPageStarted(WebView view, String url, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700570 mInPageLoad = true;
Michael Kolba53c9892011-10-05 13:31:40 -0700571 mPageLoadProgress = INITIAL_PROGRESS;
Michael Kolb14612442011-06-24 13:06:29 -0700572 mCurrentState = new PageState(mContext,
John Reck30c714c2010-12-16 17:30:34 -0800573 view.isPrivateBrowsingEnabled(), url, favicon);
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000574 mLoadStartTime = SystemClock.uptimeMillis();
Leon Scroggins58d56c62010-01-28 15:12:40 -0500575 if (mVoiceSearchData != null
Michael Kolb47bd1e42011-09-01 15:25:00 -0700576 && providersDiffer(url, mVoiceSearchData.mLastVoiceSearchUrl)) {
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500577 if (mVoiceSearchData.mSourceIsGoogle) {
578 Intent i = new Intent(LoggingEvents.ACTION_LOG_EVENT);
579 i.putExtra(LoggingEvents.EXTRA_FLUSH, true);
Michael Kolb14612442011-06-24 13:06:29 -0700580 mContext.sendBroadcast(i);
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500581 }
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400582 revertVoiceSearchMode();
Leon Scroggins58d56c62010-01-28 15:12:40 -0500583 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700584
Grace Kloba22ac16e2009-10-07 18:00:23 -0700585
586 // If we start a touch icon load and then load a new page, we don't
587 // want to cancel the current touch icon loader. But, we do want to
588 // create a new one when the touch icon url is known.
589 if (mTouchIconLoader != null) {
590 mTouchIconLoader.mTab = null;
591 mTouchIconLoader = null;
592 }
593
594 // reset the error console
595 if (mErrorConsole != null) {
596 mErrorConsole.clearErrorMessages();
Michael Kolb8233fac2010-10-26 16:08:53 -0700597 if (mWebViewController.shouldShowErrorConsole()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700598 mErrorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
599 }
600 }
601
Patrick Scott92066772011-03-10 08:46:27 -0500602 // Cancel the auto-login process.
603 if (mDeviceAccountLogin != null) {
604 mDeviceAccountLogin.cancel();
605 mDeviceAccountLogin = null;
606 mWebViewController.hideAutoLogin(Tab.this);
607 }
608
Grace Kloba22ac16e2009-10-07 18:00:23 -0700609 // finally update the UI in the activity if it is in the foreground
John Reck324d4402011-01-11 16:56:42 -0800610 mWebViewController.onPageStarted(Tab.this, view, favicon);
Leon Scroggins4cd97792010-12-03 15:31:56 -0500611
John Recke969cc52010-12-21 17:24:43 -0800612 updateBookmarkedStatus();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700613 }
614
615 @Override
616 public void onPageFinished(WebView view, String url) {
John Reck5b691842010-11-29 11:21:13 -0800617 if (!isPrivateBrowsingEnabled()) {
618 LogTag.logPageFinishedLoading(
619 url, SystemClock.uptimeMillis() - mLoadStartTime);
620 }
John Reck1cf4b792011-07-26 10:22:22 -0700621 syncCurrentState(view, url);
John Reck324d4402011-01-11 16:56:42 -0800622 mWebViewController.onPageFinished(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700623 }
624
625 // return true if want to hijack the url to let another app to handle it
626 @Override
627 public boolean shouldOverrideUrlLoading(WebView view, String url) {
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400628 if (voiceSearchSourceIsGoogle()) {
629 // This method is called when the user clicks on a link.
630 // VoiceSearchMode is turned off when the user leaves the
631 // Google results page, so at this point the user must be on
632 // that page. If the user clicked a link on that page, assume
633 // that the voice search was effective, and broadcast an Intent
634 // so a receiver can take note of that fact.
635 Intent logIntent = new Intent(LoggingEvents.ACTION_LOG_EVENT);
636 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
637 LoggingEvents.VoiceSearch.RESULT_CLICKED);
Michael Kolb14612442011-06-24 13:06:29 -0700638 mContext.sendBroadcast(logIntent);
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400639 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700640 if (mInForeground) {
Michael Kolb18eb3772010-12-10 14:29:51 -0800641 return mWebViewController.shouldOverrideUrlLoading(Tab.this,
642 view, url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700643 } else {
644 return false;
645 }
646 }
647
648 /**
Steve Block2466eff2011-10-03 15:33:09 +0100649 * Updates the security state. This method is called when we discover
650 * another resource to be loaded for this page (for example,
651 * javascript). While we update the security state, we do not update
652 * the lock icon until we are done loading, as it is slightly more
653 * secure this way.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700654 */
655 @Override
656 public void onLoadResource(WebView view, String url) {
657 if (url != null && url.length() > 0) {
658 // It is only if the page claims to be secure that we may have
Steve Block2466eff2011-10-03 15:33:09 +0100659 // to update the security state:
660 if (mCurrentState.mSecurityState == SecurityState.SECURITY_STATE_SECURE) {
661 // If NOT a 'safe' url, change the state to mixed content!
Grace Kloba22ac16e2009-10-07 18:00:23 -0700662 if (!(URLUtil.isHttpsUrl(url) || URLUtil.isDataUrl(url)
663 || URLUtil.isAboutUrl(url))) {
Steve Block2466eff2011-10-03 15:33:09 +0100664 mCurrentState.mSecurityState = SecurityState.SECURITY_STATE_MIXED;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700665 }
666 }
667 }
668 }
669
670 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700671 * Show a dialog informing the user of the network error reported by
672 * WebCore if it is in the foreground.
673 */
674 @Override
675 public void onReceivedError(WebView view, int errorCode,
676 String description, String failingUrl) {
677 if (errorCode != WebViewClient.ERROR_HOST_LOOKUP &&
678 errorCode != WebViewClient.ERROR_CONNECT &&
679 errorCode != WebViewClient.ERROR_BAD_URL &&
680 errorCode != WebViewClient.ERROR_UNSUPPORTED_SCHEME &&
681 errorCode != WebViewClient.ERROR_FILE) {
682 queueError(errorCode, description);
Jeff Hamilton47654f42010-09-07 09:57:51 -0500683
Selim Gurun3da06b82011-10-10 13:58:12 -0700684 // Don't log URLs when in private browsing mode
685 if (!isPrivateBrowsingEnabled()) {
686 Log.e(LOGTAG, "onReceivedError " + errorCode + " " + failingUrl
Jeff Hamilton47654f42010-09-07 09:57:51 -0500687 + " " + description);
Selim Gurun3da06b82011-10-10 13:58:12 -0700688 }
Jeff Hamilton47654f42010-09-07 09:57:51 -0500689 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700690 }
691
692 /**
693 * Check with the user if it is ok to resend POST data as the page they
694 * are trying to navigate to is the result of a POST.
695 */
696 @Override
697 public void onFormResubmission(WebView view, final Message dontResend,
698 final Message resend) {
699 if (!mInForeground) {
700 dontResend.sendToTarget();
701 return;
702 }
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500703 if (mDontResend != null) {
704 Log.w(LOGTAG, "onFormResubmission should not be called again "
705 + "while dialog is still up");
706 dontResend.sendToTarget();
707 return;
708 }
709 mDontResend = dontResend;
710 mResend = resend;
Michael Kolb14612442011-06-24 13:06:29 -0700711 new AlertDialog.Builder(mContext).setTitle(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700712 R.string.browserFrameFormResubmitLabel).setMessage(
713 R.string.browserFrameFormResubmitMessage)
714 .setPositiveButton(R.string.ok,
715 new DialogInterface.OnClickListener() {
716 public void onClick(DialogInterface dialog,
717 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500718 if (mResend != null) {
719 mResend.sendToTarget();
720 mResend = null;
721 mDontResend = null;
722 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700723 }
724 }).setNegativeButton(R.string.cancel,
725 new DialogInterface.OnClickListener() {
726 public void onClick(DialogInterface dialog,
727 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500728 if (mDontResend != null) {
729 mDontResend.sendToTarget();
730 mResend = null;
731 mDontResend = null;
732 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700733 }
734 }).setOnCancelListener(new OnCancelListener() {
735 public void onCancel(DialogInterface dialog) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500736 if (mDontResend != null) {
737 mDontResend.sendToTarget();
738 mResend = null;
739 mDontResend = null;
740 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700741 }
742 }).show();
743 }
744
745 /**
746 * Insert the url into the visited history database.
747 * @param url The url to be inserted.
748 * @param isReload True if this url is being reloaded.
749 * FIXME: Not sure what to do when reloading the page.
750 */
751 @Override
752 public void doUpdateVisitedHistory(WebView view, String url,
753 boolean isReload) {
John Reck324d4402011-01-11 16:56:42 -0800754 mWebViewController.doUpdateVisitedHistory(Tab.this, isReload);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700755 }
756
757 /**
758 * Displays SSL error(s) dialog to the user.
759 */
760 @Override
761 public void onReceivedSslError(final WebView view,
762 final SslErrorHandler handler, final SslError error) {
763 if (!mInForeground) {
764 handler.cancel();
Steve Block2466eff2011-10-03 15:33:09 +0100765 setSecurityState(SecurityState.SECURITY_STATE_NOT_SECURE);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700766 return;
767 }
John Reck35e9dd62011-04-25 09:01:54 -0700768 if (mSettings.showSecurityWarnings()) {
John Reckcb28b2c2011-08-26 17:39:44 -0700769 new AlertDialog.Builder(mContext)
770 .setTitle(R.string.security_warning)
771 .setMessage(R.string.ssl_warnings_header)
772 .setIcon(android.R.drawable.ic_dialog_alert)
773 .setPositiveButton(R.string.ssl_continue,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700774 new DialogInterface.OnClickListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700775 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700776 public void onClick(DialogInterface dialog,
777 int whichButton) {
778 handler.proceed();
Steve Block4895b012011-10-03 16:26:46 +0100779 handleProceededAfterSslError(error);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700780 }
John Reckcb28b2c2011-08-26 17:39:44 -0700781 })
782 .setNeutralButton(R.string.view_certificate,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700783 new DialogInterface.OnClickListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700784 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700785 public void onClick(DialogInterface dialog,
786 int whichButton) {
John Reckcb28b2c2011-08-26 17:39:44 -0700787 mWebViewController.showSslCertificateOnError(
788 view, handler, error);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700789 }
John Reckcb28b2c2011-08-26 17:39:44 -0700790 })
791 .setNegativeButton(R.string.ssl_go_back,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700792 new DialogInterface.OnClickListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700793 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700794 public void onClick(DialogInterface dialog,
795 int whichButton) {
John Reck30c714c2010-12-16 17:30:34 -0800796 dialog.cancel();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700797 }
John Reckcb28b2c2011-08-26 17:39:44 -0700798 })
799 .setOnCancelListener(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700800 new DialogInterface.OnCancelListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700801 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700802 public void onCancel(DialogInterface dialog) {
803 handler.cancel();
Steve Block2466eff2011-10-03 15:33:09 +0100804 setSecurityState(SecurityState.SECURITY_STATE_NOT_SECURE);
John Reck30c714c2010-12-16 17:30:34 -0800805 mWebViewController.onUserCanceledSsl(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700806 }
John Reckcb28b2c2011-08-26 17:39:44 -0700807 })
808 .show();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700809 } else {
810 handler.proceed();
811 }
812 }
813
814 /**
Steve Block4895b012011-10-03 16:26:46 +0100815 * Called when an SSL error occurred while loading a resource, but the
816 * WebView but chose to proceed anyway based on a decision retained
817 * from a previous response to onReceivedSslError(). We update our
818 * security state to reflect this.
819 */
820 @Override
821 public void onProceededAfterSslError(WebView view, SslError error) {
822 handleProceededAfterSslError(error);
823 }
824
825 /**
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700826 * Displays client certificate request to the user.
827 */
828 @Override
829 public void onReceivedClientCertRequest(final WebView view,
830 final ClientCertRequestHandler handler, final String host_and_port) {
831 if (!mInForeground) {
832 handler.ignore();
833 return;
834 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700835 int colon = host_and_port.lastIndexOf(':');
836 String host;
837 int port;
838 if (colon == -1) {
839 host = host_and_port;
840 port = -1;
841 } else {
842 String portString = host_and_port.substring(colon + 1);
843 try {
844 port = Integer.parseInt(portString);
845 host = host_and_port.substring(0, colon);
846 } catch (NumberFormatException e) {
847 host = host_and_port;
848 port = -1;
849 }
850 }
Michael Kolb14612442011-06-24 13:06:29 -0700851 KeyChain.choosePrivateKeyAlias(
852 mWebViewController.getActivity(), new KeyChainAliasCallback() {
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700853 @Override public void alias(String alias) {
854 if (alias == null) {
855 handler.cancel();
856 return;
857 }
Michael Kolb14612442011-06-24 13:06:29 -0700858 new KeyChainLookup(mContext, handler, alias).execute();
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700859 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700860 }, null, null, host, port, null);
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700861 }
862
863 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700864 * Handles an HTTP authentication request.
865 *
866 * @param handler The authentication handler
867 * @param host The host
868 * @param realm The realm
869 */
870 @Override
871 public void onReceivedHttpAuthRequest(WebView view,
872 final HttpAuthHandler handler, final String host,
873 final String realm) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700874 mWebViewController.onReceivedHttpAuthRequest(Tab.this, view, handler, host, realm);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700875 }
876
877 @Override
John Reck438bf462011-01-12 18:11:46 -0800878 public WebResourceResponse shouldInterceptRequest(WebView view,
879 String url) {
880 WebResourceResponse res = HomeProvider.shouldInterceptRequest(
Michael Kolb14612442011-06-24 13:06:29 -0700881 mContext, url);
John Reck438bf462011-01-12 18:11:46 -0800882 return res;
883 }
884
885 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700886 public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
887 if (!mInForeground) {
888 return false;
889 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700890 return mWebViewController.shouldOverrideKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700891 }
892
893 @Override
894 public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700895 if (!mInForeground) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700896 return;
897 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700898 mWebViewController.onUnhandledKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700899 }
Patrick Scott92066772011-03-10 08:46:27 -0500900
901 @Override
902 public void onReceivedLoginRequest(WebView view, String realm,
903 String account, String args) {
Michael Kolb14612442011-06-24 13:06:29 -0700904 new DeviceAccountLogin(mWebViewController.getActivity(), view, Tab.this, mWebViewController)
Patrick Scott92066772011-03-10 08:46:27 -0500905 .handleLogin(realm, account, args);
906 }
907
Grace Kloba22ac16e2009-10-07 18:00:23 -0700908 };
909
John Reck1cf4b792011-07-26 10:22:22 -0700910 private void syncCurrentState(WebView view, String url) {
911 // Sync state (in case of stop/timeout)
912 mCurrentState.mUrl = view.getUrl();
913 if (mCurrentState.mUrl == null) {
John Reck8bcafc12011-08-29 16:43:02 -0700914 mCurrentState.mUrl = "";
John Reck1cf4b792011-07-26 10:22:22 -0700915 }
916 mCurrentState.mOriginalUrl = view.getOriginalUrl();
917 mCurrentState.mTitle = view.getTitle();
918 mCurrentState.mFavicon = view.getFavicon();
919 if (!URLUtil.isHttpsUrl(mCurrentState.mUrl)) {
920 // In case we stop when loading an HTTPS page from an HTTP page
921 // but before a provisional load occurred
Steve Block2466eff2011-10-03 15:33:09 +0100922 mCurrentState.mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
Steve Block08a6f0c2011-10-06 12:12:53 +0100923 mCurrentState.mSslCertificateError = null;
John Reck1cf4b792011-07-26 10:22:22 -0700924 }
John Reck502a3532011-08-16 14:21:46 -0700925 mCurrentState.mIncognito = view.isPrivateBrowsingEnabled();
John Reck1cf4b792011-07-26 10:22:22 -0700926 }
927
Patrick Scott92066772011-03-10 08:46:27 -0500928 // Called by DeviceAccountLogin when the Tab needs to have the auto-login UI
929 // displayed.
930 void setDeviceAccountLogin(DeviceAccountLogin login) {
931 mDeviceAccountLogin = login;
932 }
933
934 // Returns non-null if the title bar should display the auto-login UI.
935 DeviceAccountLogin getDeviceAccountLogin() {
936 return mDeviceAccountLogin;
937 }
938
Grace Kloba22ac16e2009-10-07 18:00:23 -0700939 // -------------------------------------------------------------------------
940 // WebChromeClient implementation for the main WebView
941 // -------------------------------------------------------------------------
942
943 private final WebChromeClient mWebChromeClient = new WebChromeClient() {
944 // Helper method to create a new tab or sub window.
945 private void createWindow(final boolean dialog, final Message msg) {
946 WebView.WebViewTransport transport =
947 (WebView.WebViewTransport) msg.obj;
948 if (dialog) {
949 createSubWindow();
Michael Kolb8233fac2010-10-26 16:08:53 -0700950 mWebViewController.attachSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700951 transport.setWebView(mSubView);
952 } else {
Michael Kolb7bcafde2011-05-09 13:55:59 -0700953 final Tab newTab = mWebViewController.openTab(null,
John Reck5949c662011-05-27 09:52:29 -0700954 Tab.this, true, true);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700955 transport.setWebView(newTab.getWebView());
956 }
957 msg.sendToTarget();
958 }
959
960 @Override
961 public boolean onCreateWindow(WebView view, final boolean dialog,
962 final boolean userGesture, final Message resultMsg) {
963 // only allow new window or sub window for the foreground case
964 if (!mInForeground) {
965 return false;
966 }
967 // Short-circuit if we can't create any more tabs or sub windows.
968 if (dialog && mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -0700969 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700970 .setTitle(R.string.too_many_subwindows_dialog_title)
971 .setIcon(android.R.drawable.ic_dialog_alert)
972 .setMessage(R.string.too_many_subwindows_dialog_message)
973 .setPositiveButton(R.string.ok, null)
974 .show();
975 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700976 } else if (!mWebViewController.getTabControl().canCreateNewTab()) {
Michael Kolb14612442011-06-24 13:06:29 -0700977 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700978 .setTitle(R.string.too_many_windows_dialog_title)
979 .setIcon(android.R.drawable.ic_dialog_alert)
980 .setMessage(R.string.too_many_windows_dialog_message)
981 .setPositiveButton(R.string.ok, null)
982 .show();
983 return false;
984 }
985
986 // Short-circuit if this was a user gesture.
987 if (userGesture) {
988 createWindow(dialog, resultMsg);
989 return true;
990 }
991
992 // Allow the popup and create the appropriate window.
993 final AlertDialog.OnClickListener allowListener =
994 new AlertDialog.OnClickListener() {
995 public void onClick(DialogInterface d,
996 int which) {
997 createWindow(dialog, resultMsg);
998 }
999 };
1000
1001 // Block the popup by returning a null WebView.
1002 final AlertDialog.OnClickListener blockListener =
1003 new AlertDialog.OnClickListener() {
1004 public void onClick(DialogInterface d, int which) {
1005 resultMsg.sendToTarget();
1006 }
1007 };
1008
1009 // Build a confirmation dialog to display to the user.
1010 final AlertDialog d =
Michael Kolb14612442011-06-24 13:06:29 -07001011 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -07001012 .setIcon(android.R.drawable.ic_dialog_alert)
1013 .setMessage(R.string.popup_window_attempt)
1014 .setPositiveButton(R.string.allow, allowListener)
1015 .setNegativeButton(R.string.block, blockListener)
1016 .setCancelable(false)
1017 .create();
1018
1019 // Show the confirmation dialog.
1020 d.show();
1021 return true;
1022 }
1023
1024 @Override
Patrick Scotteb5061b2009-11-18 15:00:30 -05001025 public void onRequestFocus(WebView view) {
1026 if (!mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -07001027 mWebViewController.switchToTab(Tab.this);
Patrick Scotteb5061b2009-11-18 15:00:30 -05001028 }
1029 }
1030
1031 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -07001032 public void onCloseWindow(WebView window) {
Michael Kolbc831b632011-05-11 09:30:34 -07001033 if (mParent != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001034 // JavaScript can only close popup window.
1035 if (mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -07001036 mWebViewController.switchToTab(mParent);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001037 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001038 mWebViewController.closeTab(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001039 }
1040 }
1041
1042 @Override
1043 public void onProgressChanged(WebView view, int newProgress) {
John Reck30c714c2010-12-16 17:30:34 -08001044 mPageLoadProgress = newProgress;
Michael Kolbb1fb70c2011-11-21 12:38:14 -08001045 if (newProgress == 100) {
1046 mInPageLoad = false;
1047 }
John Reck30c714c2010-12-16 17:30:34 -08001048 mWebViewController.onProgressChanged(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001049 }
1050
1051 @Override
Leon Scroggins21d9b902010-03-11 09:33:11 -05001052 public void onReceivedTitle(WebView view, final String title) {
John Reck30c714c2010-12-16 17:30:34 -08001053 mCurrentState.mTitle = title;
Michael Kolb8233fac2010-10-26 16:08:53 -07001054 mWebViewController.onReceivedTitle(Tab.this, title);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001055 }
1056
1057 @Override
1058 public void onReceivedIcon(WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -08001059 mCurrentState.mFavicon = icon;
Michael Kolb8233fac2010-10-26 16:08:53 -07001060 mWebViewController.onFavicon(Tab.this, view, icon);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001061 }
1062
1063 @Override
1064 public void onReceivedTouchIconUrl(WebView view, String url,
1065 boolean precomposed) {
Michael Kolb14612442011-06-24 13:06:29 -07001066 final ContentResolver cr = mContext.getContentResolver();
Leon Scrogginsc8393d92010-04-23 14:58:16 -04001067 // Let precomposed icons take precedence over non-composed
1068 // icons.
1069 if (precomposed && mTouchIconLoader != null) {
1070 mTouchIconLoader.cancel(false);
1071 mTouchIconLoader = null;
1072 }
1073 // Have only one async task at a time.
1074 if (mTouchIconLoader == null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001075 mTouchIconLoader = new DownloadTouchIcon(Tab.this,
Michael Kolb14612442011-06-24 13:06:29 -07001076 mContext, cr, view);
Leon Scrogginsc8393d92010-04-23 14:58:16 -04001077 mTouchIconLoader.execute(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001078 }
1079 }
1080
1081 @Override
1082 public void onShowCustomView(View view,
1083 WebChromeClient.CustomViewCallback callback) {
Michael Kolb14612442011-06-24 13:06:29 -07001084 Activity activity = mWebViewController.getActivity();
1085 if (activity != null) {
1086 onShowCustomView(view, activity.getRequestedOrientation(), callback);
1087 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001088 }
1089
1090 @Override
1091 public void onShowCustomView(View view, int requestedOrientation,
1092 WebChromeClient.CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001093 if (mInForeground) mWebViewController.showCustomView(Tab.this, view,
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001094 requestedOrientation, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001095 }
1096
1097 @Override
1098 public void onHideCustomView() {
Michael Kolb8233fac2010-10-26 16:08:53 -07001099 if (mInForeground) mWebViewController.hideCustomView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001100 }
1101
1102 /**
1103 * The origin has exceeded its database quota.
1104 * @param url the URL that exceeded the quota
1105 * @param databaseIdentifier the identifier of the database on which the
1106 * transaction that caused the quota overflow was run
1107 * @param currentQuota the current quota for the origin.
1108 * @param estimatedSize the estimated size of the database.
1109 * @param totalUsedQuota is the sum of all origins' quota.
1110 * @param quotaUpdater The callback to run when a decision to allow or
1111 * deny quota has been made. Don't forget to call this!
1112 */
1113 @Override
1114 public void onExceededDatabaseQuota(String url,
1115 String databaseIdentifier, long currentQuota, long estimatedSize,
1116 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -07001117 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001118 .onExceededDatabaseQuota(url, databaseIdentifier,
1119 currentQuota, estimatedSize, totalUsedQuota,
1120 quotaUpdater);
1121 }
1122
1123 /**
1124 * The Application Cache has exceeded its max size.
1125 * @param spaceNeeded is the amount of disk space that would be needed
1126 * in order for the last appcache operation to succeed.
1127 * @param totalUsedQuota is the sum of all origins' quota.
1128 * @param quotaUpdater A callback to inform the WebCore thread that a
1129 * new app cache size is available. This callback must always
1130 * be executed at some point to ensure that the sleeping
1131 * WebCore thread is woken up.
1132 */
1133 @Override
1134 public void onReachedMaxAppCacheSize(long spaceNeeded,
1135 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -07001136 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001137 .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota,
1138 quotaUpdater);
1139 }
1140
1141 /**
1142 * Instructs the browser to show a prompt to ask the user to set the
1143 * Geolocation permission state for the specified origin.
1144 * @param origin The origin for which Geolocation permissions are
1145 * requested.
1146 * @param callback The callback to call once the user has set the
1147 * Geolocation permission state.
1148 */
1149 @Override
1150 public void onGeolocationPermissionsShowPrompt(String origin,
1151 GeolocationPermissions.Callback callback) {
1152 if (mInForeground) {
Grace Kloba50c241e2010-04-20 11:07:50 -07001153 getGeolocationPermissionsPrompt().show(origin, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001154 }
1155 }
1156
1157 /**
1158 * Instructs the browser to hide the Geolocation permissions prompt.
1159 */
1160 @Override
1161 public void onGeolocationPermissionsHidePrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001162 if (mInForeground && mGeolocationPermissionsPrompt != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001163 mGeolocationPermissionsPrompt.hide();
1164 }
1165 }
1166
Ben Murdoch65acc352009-11-19 18:16:04 +00001167 /* Adds a JavaScript error message to the system log and if the JS
1168 * console is enabled in the about:debug options, to that console
1169 * also.
Ben Murdochc42addf2010-01-28 15:19:59 +00001170 * @param consoleMessage the message object.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001171 */
1172 @Override
Ben Murdochc42addf2010-01-28 15:19:59 +00001173 public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001174 if (mInForeground) {
1175 // call getErrorConsole(true) so it will create one if needed
1176 ErrorConsoleView errorConsole = getErrorConsole(true);
Ben Murdochc42addf2010-01-28 15:19:59 +00001177 errorConsole.addErrorMessage(consoleMessage);
Michael Kolb8233fac2010-10-26 16:08:53 -07001178 if (mWebViewController.shouldShowErrorConsole()
1179 && errorConsole.getShowState() !=
1180 ErrorConsoleView.SHOW_MAXIMIZED) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001181 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
1182 }
1183 }
Ben Murdochc42addf2010-01-28 15:19:59 +00001184
Jeff Hamilton47654f42010-09-07 09:57:51 -05001185 // Don't log console messages in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001186 if (isPrivateBrowsingEnabled()) return true;
Jeff Hamilton47654f42010-09-07 09:57:51 -05001187
Ben Murdochc42addf2010-01-28 15:19:59 +00001188 String message = "Console: " + consoleMessage.message() + " "
1189 + consoleMessage.sourceId() + ":"
1190 + consoleMessage.lineNumber();
1191
1192 switch (consoleMessage.messageLevel()) {
1193 case TIP:
1194 Log.v(CONSOLE_LOGTAG, message);
1195 break;
1196 case LOG:
1197 Log.i(CONSOLE_LOGTAG, message);
1198 break;
1199 case WARNING:
1200 Log.w(CONSOLE_LOGTAG, message);
1201 break;
1202 case ERROR:
1203 Log.e(CONSOLE_LOGTAG, message);
1204 break;
1205 case DEBUG:
1206 Log.d(CONSOLE_LOGTAG, message);
1207 break;
1208 }
1209
1210 return true;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001211 }
1212
1213 /**
1214 * Ask the browser for an icon to represent a <video> element.
1215 * This icon will be used if the Web page did not specify a poster attribute.
1216 * @return Bitmap The icon or null if no such icon is available.
1217 */
1218 @Override
1219 public Bitmap getDefaultVideoPoster() {
1220 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001221 return mWebViewController.getDefaultVideoPoster();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001222 }
1223 return null;
1224 }
1225
1226 /**
1227 * Ask the host application for a custom progress view to show while
1228 * a <video> is loading.
1229 * @return View The progress view.
1230 */
1231 @Override
1232 public View getVideoLoadingProgressView() {
1233 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001234 return mWebViewController.getVideoLoadingProgressView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001235 }
1236 return null;
1237 }
1238
1239 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00001240 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001241 if (mInForeground) {
Ben Murdoch8cad4132012-01-11 10:56:43 +00001242 mWebViewController.openFileChooser(uploadMsg, acceptType, capture);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001243 } else {
1244 uploadMsg.onReceiveValue(null);
1245 }
1246 }
1247
1248 /**
1249 * Deliver a list of already-visited URLs
1250 */
1251 @Override
1252 public void getVisitedHistory(final ValueCallback<String[]> callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001253 mWebViewController.getVisitedHistory(callback);
1254 }
Ben Murdoch8029a772010-11-16 11:58:21 +00001255
1256 @Override
1257 public void setupAutoFill(Message message) {
1258 // Prompt the user to set up their profile.
1259 final Message msg = message;
Michael Kolb14612442011-06-24 13:06:29 -07001260 AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
1261 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
Ben Murdoch1d676b62011-01-17 12:54:24 +00001262 Context.LAYOUT_INFLATER_SERVICE);
1263 final View layout = inflater.inflate(R.layout.setup_autofill_dialog, null);
1264
1265 builder.setView(layout)
1266 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1267 @Override
1268 public void onClick(DialogInterface dialog, int id) {
1269 CheckBox disableAutoFill = (CheckBox) layout.findViewById(
1270 R.id.setup_autofill_dialog_disable_autofill);
1271
1272 if (disableAutoFill.isChecked()) {
1273 // Disable autofill and show a toast with how to turn it on again.
John Reck35e9dd62011-04-25 09:01:54 -07001274 mSettings.setAutofillEnabled(false);
Michael Kolb14612442011-06-24 13:06:29 -07001275 Toast.makeText(mContext,
Ben Murdoch1d676b62011-01-17 12:54:24 +00001276 R.string.autofill_setup_dialog_negative_toast,
1277 Toast.LENGTH_LONG).show();
1278 } else {
1279 // Take user to the AutoFill profile editor. When they return,
1280 // we will send the message that we pass here which will trigger
1281 // the form to get filled out with their new profile.
1282 mWebViewController.setupAutoFill(msg);
1283 }
1284 }
1285 })
1286 .setNegativeButton(R.string.cancel, null)
1287 .show();
Ben Murdoch8029a772010-11-16 11:58:21 +00001288 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001289 };
1290
1291 // -------------------------------------------------------------------------
1292 // WebViewClient implementation for the sub window
1293 // -------------------------------------------------------------------------
1294
1295 // Subclass of WebViewClient used in subwindows to notify the main
1296 // WebViewClient of certain WebView activities.
1297 private static class SubWindowClient extends WebViewClient {
1298 // The main WebViewClient.
1299 private final WebViewClient mClient;
Michael Kolb8233fac2010-10-26 16:08:53 -07001300 private final WebViewController mController;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001301
Michael Kolb8233fac2010-10-26 16:08:53 -07001302 SubWindowClient(WebViewClient client, WebViewController controller) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001303 mClient = client;
Michael Kolb8233fac2010-10-26 16:08:53 -07001304 mController = controller;
Leon Scroggins III211ba542010-04-19 13:21:13 -04001305 }
1306 @Override
1307 public void onPageStarted(WebView view, String url, Bitmap favicon) {
1308 // Unlike the others, do not call mClient's version, which would
1309 // change the progress bar. However, we do want to remove the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001310 // find or select dialog.
Michael Kolb8233fac2010-10-26 16:08:53 -07001311 mController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001312 }
1313 @Override
1314 public void doUpdateVisitedHistory(WebView view, String url,
1315 boolean isReload) {
1316 mClient.doUpdateVisitedHistory(view, url, isReload);
1317 }
1318 @Override
1319 public boolean shouldOverrideUrlLoading(WebView view, String url) {
1320 return mClient.shouldOverrideUrlLoading(view, url);
1321 }
1322 @Override
1323 public void onReceivedSslError(WebView view, SslErrorHandler handler,
1324 SslError error) {
1325 mClient.onReceivedSslError(view, handler, error);
1326 }
1327 @Override
Brian Carlstrom8862c1d2011-06-02 01:05:55 -07001328 public void onReceivedClientCertRequest(WebView view,
1329 ClientCertRequestHandler handler, String host_and_port) {
1330 mClient.onReceivedClientCertRequest(view, handler, host_and_port);
1331 }
1332 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -07001333 public void onReceivedHttpAuthRequest(WebView view,
1334 HttpAuthHandler handler, String host, String realm) {
1335 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
1336 }
1337 @Override
1338 public void onFormResubmission(WebView view, Message dontResend,
1339 Message resend) {
1340 mClient.onFormResubmission(view, dontResend, resend);
1341 }
1342 @Override
1343 public void onReceivedError(WebView view, int errorCode,
1344 String description, String failingUrl) {
1345 mClient.onReceivedError(view, errorCode, description, failingUrl);
1346 }
1347 @Override
1348 public boolean shouldOverrideKeyEvent(WebView view,
1349 android.view.KeyEvent event) {
1350 return mClient.shouldOverrideKeyEvent(view, event);
1351 }
1352 @Override
1353 public void onUnhandledKeyEvent(WebView view,
1354 android.view.KeyEvent event) {
1355 mClient.onUnhandledKeyEvent(view, event);
1356 }
1357 }
1358
1359 // -------------------------------------------------------------------------
1360 // WebChromeClient implementation for the sub window
1361 // -------------------------------------------------------------------------
1362
1363 private class SubWindowChromeClient extends WebChromeClient {
1364 // The main WebChromeClient.
1365 private final WebChromeClient mClient;
1366
1367 SubWindowChromeClient(WebChromeClient client) {
1368 mClient = client;
1369 }
1370 @Override
1371 public void onProgressChanged(WebView view, int newProgress) {
1372 mClient.onProgressChanged(view, newProgress);
1373 }
1374 @Override
1375 public boolean onCreateWindow(WebView view, boolean dialog,
1376 boolean userGesture, android.os.Message resultMsg) {
1377 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
1378 }
1379 @Override
1380 public void onCloseWindow(WebView window) {
1381 if (window != mSubView) {
1382 Log.e(LOGTAG, "Can't close the window");
1383 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001384 mWebViewController.dismissSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001385 }
1386 }
1387
1388 // -------------------------------------------------------------------------
1389
1390 // Construct a new tab
Michael Kolb7bcafde2011-05-09 13:55:59 -07001391 Tab(WebViewController wvcontroller, WebView w) {
John Reck1cf4b792011-07-26 10:22:22 -07001392 this(wvcontroller, w, null);
1393 }
1394
1395 Tab(WebViewController wvcontroller, Bundle state) {
1396 this(wvcontroller, null, state);
1397 }
1398
1399 Tab(WebViewController wvcontroller, WebView w, Bundle state) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001400 mWebViewController = wvcontroller;
Michael Kolb14612442011-06-24 13:06:29 -07001401 mContext = mWebViewController.getContext();
John Reck35e9dd62011-04-25 09:01:54 -07001402 mSettings = BrowserSettings.getInstance();
Michael Kolb14612442011-06-24 13:06:29 -07001403 mDataController = DataController.getInstance(mContext);
1404 mCurrentState = new PageState(mContext, w != null
John Reck847b5322011-04-14 17:02:18 -07001405 ? w.isPrivateBrowsingEnabled() : false);
Michael Kolb8233fac2010-10-26 16:08:53 -07001406 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001407 mInForeground = false;
1408
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001409 mDownloadListener = new DownloadListener() {
1410 public void onDownloadStart(String url, String userAgent,
1411 String contentDisposition, String mimetype,
1412 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001413 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001414 mimetype, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001415 }
1416 };
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001417 mWebBackForwardListClient = new WebBackForwardListClient() {
1418 @Override
1419 public void onNewHistoryItem(WebHistoryItem item) {
1420 if (isInVoiceSearchMode()) {
1421 item.setCustomData(mVoiceSearchData.mVoiceSearchIntent);
1422 }
Mathew Inwooda829d552011-09-02 14:16:25 +01001423 if (mClearHistoryUrlPattern != null) {
1424 boolean match =
1425 mClearHistoryUrlPattern.matcher(item.getOriginalUrl()).matches();
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001426 if (LOGD_ENABLED) {
Mathew Inwooda829d552011-09-02 14:16:25 +01001427 Log.d(LOGTAG, "onNewHistoryItem: match=" + match + "\n\t"
1428 + item.getUrl() + "\n\t"
1429 + mClearHistoryUrlPattern);
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001430 }
Mathew Inwooda829d552011-09-02 14:16:25 +01001431 if (match) {
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001432 if (mMainView != null) {
1433 mMainView.clearHistory();
1434 }
1435 }
Mathew Inwooda829d552011-09-02 14:16:25 +01001436 mClearHistoryUrlPattern = null;
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001437 }
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001438 }
1439 @Override
1440 public void onIndexChanged(WebHistoryItem item, int index) {
1441 Object data = item.getCustomData();
1442 if (data != null && data instanceof Intent) {
1443 activateVoiceSearchMode((Intent) data);
1444 }
1445 }
1446 };
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001447
John Reck1cf4b792011-07-26 10:22:22 -07001448 mCaptureWidth = mContext.getResources().getDimensionPixelSize(
1449 R.dimen.tab_thumbnail_width);
1450 mCaptureHeight = mContext.getResources().getDimensionPixelSize(
1451 R.dimen.tab_thumbnail_height);
1452 updateShouldCaptureThumbnails();
1453 restoreState(state);
John Reck52be4782011-08-26 15:37:29 -07001454 if (getId() == -1) {
1455 mId = TabControl.getNextId();
1456 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001457 setWebView(w);
Michael Kolb9ef259a2011-07-12 15:33:08 -07001458 mHandler = new Handler() {
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001459 @Override
Michael Kolb9ef259a2011-07-12 15:33:08 -07001460 public void handleMessage(Message m) {
John Reck1cf4b792011-07-26 10:22:22 -07001461 switch (m.what) {
1462 case MSG_CAPTURE:
1463 capture();
1464 break;
1465 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001466 }
1467 };
John Reck1cf4b792011-07-26 10:22:22 -07001468 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001469
Mathew Inwoode09305e2011-09-02 12:03:26 +01001470 /**
1471 * This is used to get a new ID when the tab has been preloaded, before it is displayed and
1472 * added to TabControl. Preloaded tabs can be created before restoreInstanceState, leading
1473 * to overlapping IDs between the preloaded and restored tabs.
1474 */
1475 public void refreshIdAfterPreload() {
1476 mId = TabControl.getNextId();
1477 }
1478
John Reck1cf4b792011-07-26 10:22:22 -07001479 public void updateShouldCaptureThumbnails() {
1480 if (mWebViewController.shouldCaptureThumbnails()) {
1481 synchronized (Tab.this) {
1482 if (mCapture == null) {
1483 mCapture = Bitmap.createBitmap(mCaptureWidth, mCaptureHeight,
1484 Bitmap.Config.RGB_565);
Michael Kolbc3af0672011-08-09 10:24:41 -07001485 mCapture.eraseColor(Color.WHITE);
John Reck1cf4b792011-07-26 10:22:22 -07001486 if (mInForeground) {
1487 postCapture();
1488 }
1489 }
1490 }
1491 } else {
1492 synchronized (Tab.this) {
1493 mCapture = null;
1494 deleteThumbnail();
1495 }
1496 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001497 }
1498
Michael Kolb14612442011-06-24 13:06:29 -07001499 public void setController(WebViewController ctl) {
1500 mWebViewController = ctl;
John Reck1cf4b792011-07-26 10:22:22 -07001501 updateShouldCaptureThumbnails();
Michael Kolb14612442011-06-24 13:06:29 -07001502 }
1503
Michael Kolbc831b632011-05-11 09:30:34 -07001504 public long getId() {
1505 return mId;
1506 }
1507
Michael Kolb91911a22012-01-17 11:21:25 -08001508 void setWebView(WebView w) {
1509 setWebView(w, true);
1510 }
1511
Grace Kloba22ac16e2009-10-07 18:00:23 -07001512 /**
1513 * Sets the WebView for this tab, correctly removing the old WebView from
1514 * the container view.
1515 */
Michael Kolb91911a22012-01-17 11:21:25 -08001516 void setWebView(WebView w, boolean restore) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001517 if (mMainView == w) {
1518 return;
1519 }
Michael Kolba713ec82010-11-29 17:27:06 -08001520
Grace Kloba22ac16e2009-10-07 18:00:23 -07001521 // If the WebView is changing, the page will be reloaded, so any ongoing
1522 // Geolocation permission requests are void.
Grace Kloba50c241e2010-04-20 11:07:50 -07001523 if (mGeolocationPermissionsPrompt != null) {
1524 mGeolocationPermissionsPrompt.hide();
1525 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001526
Michael Kolba713ec82010-11-29 17:27:06 -08001527 mWebViewController.onSetWebView(this, w);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001528
John Reck1cf4b792011-07-26 10:22:22 -07001529 if (mMainView != null) {
John Reck8ee633f2011-08-09 16:00:35 -07001530 mMainView.setPictureListener(null);
John Reck1cf4b792011-07-26 10:22:22 -07001531 if (w != null) {
1532 syncCurrentState(w, null);
1533 } else {
1534 mCurrentState = new PageState(mContext, false);
1535 }
1536 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001537 // set the new one
1538 mMainView = w;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001539 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001540 if (mMainView != null) {
1541 mMainView.setWebViewClient(mWebViewClient);
1542 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001543 // Attach DownloadManager so that downloads can start in an active
1544 // or a non-active window. This can happen when going to a site that
1545 // does a redirect after a period of time. The user could have
1546 // switched to another tab while waiting for the download to start.
1547 mMainView.setDownloadListener(mDownloadListener);
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00001548 getWebViewClassic().setWebBackForwardListClient(mWebBackForwardListClient);
John Reck8ee633f2011-08-09 16:00:35 -07001549 TabControl tc = mWebViewController.getTabControl();
1550 if (tc != null && tc.getOnThumbnailUpdatedListener() != null) {
1551 mMainView.setPictureListener(this);
1552 }
Michael Kolb91911a22012-01-17 11:21:25 -08001553 if (restore && (mSavedState != null)) {
John Reck8b9bb8b2012-03-08 13:19:40 -08001554 restoreUserAgent();
John Reck6c2e2f32011-08-22 13:41:23 -07001555 WebBackForwardList restoredState
1556 = mMainView.restoreState(mSavedState);
1557 if (restoredState == null || restoredState.getSize() == 0) {
1558 Log.w(LOGTAG, "Failed to restore WebView state!");
1559 loadUrl(mCurrentState.mOriginalUrl, null);
1560 }
John Reck1cf4b792011-07-26 10:22:22 -07001561 mSavedState = null;
1562 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001563 }
1564 }
1565
1566 /**
1567 * Destroy the tab's main WebView and subWindow if any
1568 */
1569 void destroy() {
1570 if (mMainView != null) {
1571 dismissSubWindow();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001572 // save the WebView to call destroy() after detach it from the tab
1573 WebView webView = mMainView;
1574 setWebView(null);
1575 webView.destroy();
1576 }
1577 }
1578
1579 /**
1580 * Remove the tab from the parent
1581 */
1582 void removeFromTree() {
1583 // detach the children
Michael Kolbc831b632011-05-11 09:30:34 -07001584 if (mChildren != null) {
1585 for(Tab t : mChildren) {
1586 t.setParent(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001587 }
1588 }
1589 // remove itself from the parent list
Michael Kolbc831b632011-05-11 09:30:34 -07001590 if (mParent != null) {
1591 mParent.mChildren.remove(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001592 }
John Reck1cf4b792011-07-26 10:22:22 -07001593 deleteThumbnail();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001594 }
1595
1596 /**
1597 * Create a new subwindow unless a subwindow already exists.
1598 * @return True if a new subwindow was created. False if one already exists.
1599 */
1600 boolean createSubWindow() {
1601 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001602 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001603 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001604 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001605 mSubView.setWebChromeClient(new SubWindowChromeClient(
1606 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001607 // Set a different DownloadListener for the mSubView, since it will
1608 // just need to dismiss the mSubView, rather than close the Tab
1609 mSubView.setDownloadListener(new DownloadListener() {
1610 public void onDownloadStart(String url, String userAgent,
1611 String contentDisposition, String mimetype,
1612 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001613 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001614 contentDisposition, mimetype, contentLength);
1615 if (mSubView.copyBackForwardList().getSize() == 0) {
1616 // This subwindow was opened for the sole purpose of
1617 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001618 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001619 }
1620 }
1621 });
Michael Kolb14612442011-06-24 13:06:29 -07001622 mSubView.setOnCreateContextMenuListener(mWebViewController.getActivity());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001623 return true;
1624 }
1625 return false;
1626 }
1627
1628 /**
1629 * Dismiss the subWindow for the tab.
1630 */
1631 void dismissSubWindow() {
1632 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001633 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001634 mSubView.destroy();
1635 mSubView = null;
1636 mSubViewContainer = null;
1637 }
1638 }
1639
Grace Kloba22ac16e2009-10-07 18:00:23 -07001640
1641 /**
1642 * Set the parent tab of this tab.
1643 */
Michael Kolbc831b632011-05-11 09:30:34 -07001644 void setParent(Tab parent) {
John Reck52be4782011-08-26 15:37:29 -07001645 if (parent == this) {
1646 throw new IllegalStateException("Cannot set parent to self!");
1647 }
Michael Kolbc831b632011-05-11 09:30:34 -07001648 mParent = parent;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001649 // This tab may have been freed due to low memory. If that is the case,
Michael Kolbc831b632011-05-11 09:30:34 -07001650 // the parent tab id is already saved. If we are changing that id
Grace Kloba22ac16e2009-10-07 18:00:23 -07001651 // (most likely due to removing the parent tab) we must update the
Michael Kolbc831b632011-05-11 09:30:34 -07001652 // parent tab id in the saved Bundle.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001653 if (mSavedState != null) {
1654 if (parent == null) {
1655 mSavedState.remove(PARENTTAB);
1656 } else {
Michael Kolbc831b632011-05-11 09:30:34 -07001657 mSavedState.putLong(PARENTTAB, parent.getId());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001658 }
1659 }
John Reckb0a86db2011-05-24 14:05:58 -07001660
1661 // Sync the WebView useragent with the parent
1662 if (parent != null && mSettings.hasDesktopUseragent(parent.getWebView())
1663 != mSettings.hasDesktopUseragent(getWebView())) {
1664 mSettings.toggleDesktopUseragent(getWebView());
1665 }
John Reck52be4782011-08-26 15:37:29 -07001666
1667 if (parent != null && parent.getId() == getId()) {
1668 throw new IllegalStateException("Parent has same ID as child!");
1669 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001670 }
1671
1672 /**
Michael Kolbc831b632011-05-11 09:30:34 -07001673 * If this Tab was created through another Tab, then this method returns
1674 * that Tab.
1675 * @return the Tab parent or null
1676 */
1677 public Tab getParent() {
1678 return mParent;
1679 }
1680
1681 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001682 * When a Tab is created through the content of another Tab, then we
1683 * associate the Tabs.
1684 * @param child the Tab that was created from this Tab
1685 */
1686 void addChildTab(Tab child) {
Michael Kolbc831b632011-05-11 09:30:34 -07001687 if (mChildren == null) {
1688 mChildren = new Vector<Tab>();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001689 }
Michael Kolbc831b632011-05-11 09:30:34 -07001690 mChildren.add(child);
1691 child.setParent(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001692 }
1693
Michael Kolbc831b632011-05-11 09:30:34 -07001694 Vector<Tab> getChildren() {
1695 return mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001696 }
1697
1698 void resume() {
1699 if (mMainView != null) {
John Reck56c1fcf2011-08-17 10:15:16 -07001700 setupHwAcceleration(mMainView);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001701 mMainView.onResume();
1702 if (mSubView != null) {
1703 mSubView.onResume();
1704 }
1705 }
1706 }
1707
John Reck56c1fcf2011-08-17 10:15:16 -07001708 private void setupHwAcceleration(View web) {
1709 if (web == null) return;
1710 BrowserSettings settings = BrowserSettings.getInstance();
1711 if (settings.isHardwareAccelerated()) {
1712 web.setLayerType(View.LAYER_TYPE_NONE, null);
1713 } else {
1714 web.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
1715 }
1716 }
1717
Grace Kloba22ac16e2009-10-07 18:00:23 -07001718 void pause() {
1719 if (mMainView != null) {
1720 mMainView.onPause();
1721 if (mSubView != null) {
1722 mSubView.onPause();
1723 }
1724 }
1725 }
1726
1727 void putInForeground() {
John Reck8ee633f2011-08-09 16:00:35 -07001728 if (mInForeground) {
1729 return;
1730 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001731 mInForeground = true;
1732 resume();
Michael Kolb14612442011-06-24 13:06:29 -07001733 Activity activity = mWebViewController.getActivity();
1734 mMainView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001735 if (mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -07001736 mSubView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001737 }
1738 // Show the pending error dialog if the queue is not empty
1739 if (mQueuedErrors != null && mQueuedErrors.size() > 0) {
1740 showError(mQueuedErrors.getFirst());
1741 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001742 mWebViewController.bookmarkedStatusHasChanged(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001743 }
1744
1745 void putInBackground() {
John Reck8ee633f2011-08-09 16:00:35 -07001746 if (!mInForeground) {
1747 return;
1748 }
John Reck52be4782011-08-26 15:37:29 -07001749 capture();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001750 mInForeground = false;
1751 pause();
1752 mMainView.setOnCreateContextMenuListener(null);
1753 if (mSubView != null) {
1754 mSubView.setOnCreateContextMenuListener(null);
1755 }
1756 }
1757
Michael Kolb8233fac2010-10-26 16:08:53 -07001758 boolean inForeground() {
1759 return mInForeground;
1760 }
1761
Grace Kloba22ac16e2009-10-07 18:00:23 -07001762 /**
1763 * Return the top window of this tab; either the subwindow if it is not
1764 * null or the main window.
1765 * @return The top window of this tab.
1766 */
1767 WebView getTopWindow() {
1768 if (mSubView != null) {
1769 return mSubView;
1770 }
1771 return mMainView;
1772 }
1773
1774 /**
1775 * Return the main window of this tab. Note: if a tab is freed in the
1776 * background, this can return null. It is only guaranteed to be
1777 * non-null for the current tab.
1778 * @return The main WebView of this tab.
1779 */
1780 WebView getWebView() {
1781 return mMainView;
1782 }
1783
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00001784 /**
1785 * Return the underlying WebViewClassic implementation. As with getWebView,
1786 * this maybe null for background tabs.
1787 * @return The main WebView of this tab.
1788 */
1789 WebViewClassic getWebViewClassic() {
1790 return WebViewClassic.fromWebView(mMainView);
1791 }
1792
Michael Kolba713ec82010-11-29 17:27:06 -08001793 void setViewContainer(View container) {
1794 mContainer = container;
1795 }
1796
Michael Kolb8233fac2010-10-26 16:08:53 -07001797 View getViewContainer() {
1798 return mContainer;
1799 }
1800
Grace Kloba22ac16e2009-10-07 18:00:23 -07001801 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001802 * Return whether private browsing is enabled for the main window of
1803 * this tab.
1804 * @return True if private browsing is enabled.
1805 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001806 boolean isPrivateBrowsingEnabled() {
John Reck502a3532011-08-16 14:21:46 -07001807 return mCurrentState.mIncognito;
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001808 }
1809
1810 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001811 * Return the subwindow of this tab or null if there is no subwindow.
1812 * @return The subwindow of this tab or null.
1813 */
1814 WebView getSubWebView() {
1815 return mSubView;
1816 }
1817
Michael Kolb1514bb72010-11-22 09:11:48 -08001818 void setSubWebView(WebView subView) {
1819 mSubView = subView;
1820 }
1821
Michael Kolb8233fac2010-10-26 16:08:53 -07001822 View getSubViewContainer() {
1823 return mSubViewContainer;
1824 }
1825
Michael Kolb1514bb72010-11-22 09:11:48 -08001826 void setSubViewContainer(View subViewContainer) {
1827 mSubViewContainer = subViewContainer;
1828 }
1829
Grace Kloba22ac16e2009-10-07 18:00:23 -07001830 /**
1831 * @return The geolocation permissions prompt for this tab.
1832 */
1833 GeolocationPermissionsPrompt getGeolocationPermissionsPrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001834 if (mGeolocationPermissionsPrompt == null) {
1835 ViewStub stub = (ViewStub) mContainer
1836 .findViewById(R.id.geolocation_permissions_prompt);
1837 mGeolocationPermissionsPrompt = (GeolocationPermissionsPrompt) stub
1838 .inflate();
Grace Kloba50c241e2010-04-20 11:07:50 -07001839 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001840 return mGeolocationPermissionsPrompt;
1841 }
1842
1843 /**
1844 * @return The application id string
1845 */
1846 String getAppId() {
1847 return mAppId;
1848 }
1849
1850 /**
1851 * Set the application id string
1852 * @param id
1853 */
1854 void setAppId(String id) {
1855 mAppId = id;
1856 }
1857
Michael Kolbe28b3472011-08-04 16:54:31 -07001858 boolean closeOnBack() {
1859 return mCloseOnBack;
1860 }
1861
1862 void setCloseOnBack(boolean close) {
1863 mCloseOnBack = close;
1864 }
1865
Grace Kloba22ac16e2009-10-07 18:00:23 -07001866 String getUrl() {
John Reck324d4402011-01-11 16:56:42 -08001867 return UrlUtils.filteredUrl(mCurrentState.mUrl);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001868 }
1869
John Reck49a603c2011-03-03 09:33:05 -08001870 String getOriginalUrl() {
John Reckdb22ec42011-06-29 11:31:24 -07001871 if (mCurrentState.mOriginalUrl == null) {
1872 return getUrl();
John Reck49a603c2011-03-03 09:33:05 -08001873 }
John Reckdb22ec42011-06-29 11:31:24 -07001874 return UrlUtils.filteredUrl(mCurrentState.mOriginalUrl);
John Reck49a603c2011-03-03 09:33:05 -08001875 }
1876
Grace Kloba22ac16e2009-10-07 18:00:23 -07001877 /**
John Reck30c714c2010-12-16 17:30:34 -08001878 * Get the title of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001879 */
1880 String getTitle() {
John Reck30c714c2010-12-16 17:30:34 -08001881 if (mCurrentState.mTitle == null && mInPageLoad) {
Michael Kolb14612442011-06-24 13:06:29 -07001882 return mContext.getString(R.string.title_bar_loading);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001883 }
John Reck30c714c2010-12-16 17:30:34 -08001884 return mCurrentState.mTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001885 }
1886
1887 /**
John Reck30c714c2010-12-16 17:30:34 -08001888 * Get the favicon of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001889 */
1890 Bitmap getFavicon() {
John Reck1cf4b792011-07-26 10:22:22 -07001891 if (mCurrentState.mFavicon != null) {
1892 return mCurrentState.mFavicon;
1893 }
1894 return getDefaultFavicon(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001895 }
1896
John Recke969cc52010-12-21 17:24:43 -08001897 public boolean isBookmarkedSite() {
1898 return mCurrentState.mIsBookmarkedSite;
1899 }
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001900
Grace Kloba22ac16e2009-10-07 18:00:23 -07001901 /**
1902 * Return the tab's error console. Creates the console if createIfNEcessary
1903 * is true and we haven't already created the console.
1904 * @param createIfNecessary Flag to indicate if the console should be
1905 * created if it has not been already.
1906 * @return The tab's error console, or null if one has not been created and
1907 * createIfNecessary is false.
1908 */
1909 ErrorConsoleView getErrorConsole(boolean createIfNecessary) {
1910 if (createIfNecessary && mErrorConsole == null) {
Michael Kolb14612442011-06-24 13:06:29 -07001911 mErrorConsole = new ErrorConsoleView(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001912 mErrorConsole.setWebView(mMainView);
1913 }
1914 return mErrorConsole;
1915 }
1916
Steve Block08a6f0c2011-10-06 12:12:53 +01001917 /**
1918 * Sets the security state, clears the SSL certificate error and informs
1919 * the controller.
1920 */
Steve Block2466eff2011-10-03 15:33:09 +01001921 private void setSecurityState(SecurityState securityState) {
1922 mCurrentState.mSecurityState = securityState;
Steve Block08a6f0c2011-10-06 12:12:53 +01001923 mCurrentState.mSslCertificateError = null;
Steve Block2466eff2011-10-03 15:33:09 +01001924 mWebViewController.onUpdatedSecurityState(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001925 }
1926
1927 /**
Steve Block2466eff2011-10-03 15:33:09 +01001928 * @return The tab's security state.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001929 */
Steve Block2466eff2011-10-03 15:33:09 +01001930 SecurityState getSecurityState() {
1931 return mCurrentState.mSecurityState;
John Reck30c714c2010-12-16 17:30:34 -08001932 }
1933
Steve Block08a6f0c2011-10-06 12:12:53 +01001934 /**
1935 * Gets the SSL certificate error, if any, for the page's main resource.
1936 * This is only non-null when the security state is
1937 * SECURITY_STATE_BAD_CERTIFICATE.
1938 */
1939 SslError getSslCertificateError() {
1940 return mCurrentState.mSslCertificateError;
1941 }
1942
John Reck30c714c2010-12-16 17:30:34 -08001943 int getLoadProgress() {
1944 if (mInPageLoad) {
1945 return mPageLoadProgress;
1946 }
1947 return 100;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001948 }
1949
1950 /**
1951 * @return TRUE if onPageStarted is called while onPageFinished is not
1952 * called yet.
1953 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001954 boolean inPageLoad() {
1955 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001956 }
1957
1958 // force mInLoad to be false. This should only be called before closing the
1959 // tab to ensure BrowserActivity's pauseWebViewTimers() is called correctly.
Michael Kolb8233fac2010-10-26 16:08:53 -07001960 void clearInPageLoad() {
1961 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001962 }
1963
Grace Kloba22ac16e2009-10-07 18:00:23 -07001964 /**
John Reck1cf4b792011-07-26 10:22:22 -07001965 * @return The Bundle with the tab's state if it can be saved, otherwise null
Grace Kloba22ac16e2009-10-07 18:00:23 -07001966 */
John Reck1cf4b792011-07-26 10:22:22 -07001967 public Bundle saveState() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001968 // If the WebView is null it means we ran low on memory and we already
1969 // stored the saved state in mSavedState.
1970 if (mMainView == null) {
John Reck1cf4b792011-07-26 10:22:22 -07001971 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001972 }
John Reck6c2e2f32011-08-22 13:41:23 -07001973
1974 if (TextUtils.isEmpty(mCurrentState.mUrl)) {
John Reck1cf4b792011-07-26 10:22:22 -07001975 return null;
John Reck24f18262011-06-17 14:47:20 -07001976 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001977
1978 mSavedState = new Bundle();
John Reck6c2e2f32011-08-22 13:41:23 -07001979 WebBackForwardList savedList = mMainView.saveState(mSavedState);
1980 if (savedList == null || savedList.getSize() == 0) {
1981 Log.w(LOGTAG, "Failed to save back/forward list for "
1982 + mCurrentState.mUrl);
1983 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001984
Michael Kolbc831b632011-05-11 09:30:34 -07001985 mSavedState.putLong(ID, mId);
John Reck30c714c2010-12-16 17:30:34 -08001986 mSavedState.putString(CURRURL, mCurrentState.mUrl);
1987 mSavedState.putString(CURRTITLE, mCurrentState.mTitle);
John Reck1cf4b792011-07-26 10:22:22 -07001988 mSavedState.putBoolean(INCOGNITO, mMainView.isPrivateBrowsingEnabled());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001989 if (mAppId != null) {
1990 mSavedState.putString(APPID, mAppId);
1991 }
Michael Kolbe28b3472011-08-04 16:54:31 -07001992 mSavedState.putBoolean(CLOSEFLAG, mCloseOnBack);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001993 // Remember the parent tab so the relationship can be restored.
Michael Kolbc831b632011-05-11 09:30:34 -07001994 if (mParent != null) {
1995 mSavedState.putLong(PARENTTAB, mParent.mId);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001996 }
John Reckb0a86db2011-05-24 14:05:58 -07001997 mSavedState.putBoolean(USERAGENT,
1998 mSettings.hasDesktopUseragent(getWebView()));
John Reck1cf4b792011-07-26 10:22:22 -07001999 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07002000 }
2001
2002 /*
2003 * Restore the state of the tab.
2004 */
John Reck1cf4b792011-07-26 10:22:22 -07002005 private void restoreState(Bundle b) {
2006 mSavedState = b;
2007 if (mSavedState == null) {
2008 return;
Grace Kloba22ac16e2009-10-07 18:00:23 -07002009 }
2010 // Restore the internal state even if the WebView fails to restore.
2011 // This will maintain the app id, original url and close-on-exit values.
Michael Kolbc831b632011-05-11 09:30:34 -07002012 mId = b.getLong(ID);
Grace Kloba22ac16e2009-10-07 18:00:23 -07002013 mAppId = b.getString(APPID);
Michael Kolbe28b3472011-08-04 16:54:31 -07002014 mCloseOnBack = b.getBoolean(CLOSEFLAG);
John Reck8b9bb8b2012-03-08 13:19:40 -08002015 restoreUserAgent();
John Reck1cf4b792011-07-26 10:22:22 -07002016 String url = b.getString(CURRURL);
2017 String title = b.getString(CURRTITLE);
2018 boolean incognito = b.getBoolean(INCOGNITO);
2019 mCurrentState = new PageState(mContext, incognito, url, null);
2020 mCurrentState.mTitle = title;
2021 synchronized (Tab.this) {
2022 if (mCapture != null) {
John Reck4eadc342011-10-31 14:04:10 -07002023 DataController.getInstance(mContext).loadThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07002024 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002025 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002026 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04002027
John Reck8b9bb8b2012-03-08 13:19:40 -08002028 private void restoreUserAgent() {
2029 if (mMainView == null || mSavedState == null) {
2030 return;
2031 }
2032 if (mSavedState.getBoolean(USERAGENT)
2033 != mSettings.hasDesktopUseragent(mMainView)) {
2034 mSettings.toggleDesktopUseragent(mMainView);
2035 }
2036 }
2037
Leon Scroggins1961ed22010-12-07 15:22:21 -05002038 public void updateBookmarkedStatus() {
John Recke969cc52010-12-21 17:24:43 -08002039 mDataController.queryBookmarkStatus(getUrl(), mIsBookmarkCallback);
Leon Scroggins1961ed22010-12-07 15:22:21 -05002040 }
2041
John Recke969cc52010-12-21 17:24:43 -08002042 private DataController.OnQueryUrlIsBookmark mIsBookmarkCallback
2043 = new DataController.OnQueryUrlIsBookmark() {
2044 @Override
2045 public void onQueryUrlIsBookmark(String url, boolean isBookmark) {
2046 if (mCurrentState.mUrl.equals(url)) {
2047 mCurrentState.mIsBookmarkedSite = isBookmark;
2048 mWebViewController.bookmarkedStatusHasChanged(Tab.this);
2049 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05002050 }
John Recke969cc52010-12-21 17:24:43 -08002051 };
Michael Kolb1acef692011-03-08 14:12:06 -08002052
Michael Kolbeb95db42011-03-03 10:38:40 -08002053 public Bitmap getScreenshot() {
John Reck1cf4b792011-07-26 10:22:22 -07002054 synchronized (Tab.this) {
2055 return mCapture;
2056 }
Michael Kolbeb95db42011-03-03 10:38:40 -08002057 }
2058
John Reck541f55a2011-06-07 16:34:43 -07002059 public boolean isSnapshot() {
John Reck541f55a2011-06-07 16:34:43 -07002060 return false;
2061 }
2062
John Reckd8c74522011-06-14 08:45:00 -07002063 public ContentValues createSnapshotValues() {
2064 if (mMainView == null) return null;
John Reck2b71d6d2012-04-18 17:42:06 -07002065 String path = UUID.randomUUID().toString();
John Reck8cc92352011-07-06 17:41:52 -07002066 try {
John Reck2b71d6d2012-04-18 17:42:06 -07002067 OutputStream outs = mContext.openFileOutput(path, Context.MODE_PRIVATE);
2068 GZIPOutputStream stream = new GZIPOutputStream(outs);
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002069 if (!getWebViewClassic().saveViewState(stream)) {
John Reck8cc92352011-07-06 17:41:52 -07002070 return null;
2071 }
2072 stream.flush();
2073 stream.close();
2074 } catch (Exception e) {
2075 Log.w(LOGTAG, "Failed to save view state", e);
John Reck541f55a2011-06-07 16:34:43 -07002076 return null;
2077 }
John Reck2b71d6d2012-04-18 17:42:06 -07002078 File savedFile = mContext.getFileStreamPath(path);
2079 long size = savedFile.length();
John Reckd8c74522011-06-14 08:45:00 -07002080 ContentValues values = new ContentValues();
2081 values.put(Snapshots.TITLE, mCurrentState.mTitle);
2082 values.put(Snapshots.URL, mCurrentState.mUrl);
John Reck2b71d6d2012-04-18 17:42:06 -07002083 values.put(Snapshots.VIEWSTATE_PATH, path);
2084 values.put(Snapshots.VIEWSTATE_SIZE, size);
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00002085 values.put(Snapshots.BACKGROUND, getWebViewClassic().getPageBackgroundColor());
John Reck8cc92352011-07-06 17:41:52 -07002086 values.put(Snapshots.DATE_CREATED, System.currentTimeMillis());
2087 values.put(Snapshots.FAVICON, compressBitmap(getFavicon()));
2088 Bitmap screenshot = Controller.createScreenshot(mMainView,
2089 Controller.getDesiredThumbnailWidth(mContext),
2090 Controller.getDesiredThumbnailHeight(mContext));
2091 values.put(Snapshots.THUMBNAIL, compressBitmap(screenshot));
John Reckd8c74522011-06-14 08:45:00 -07002092 return values;
John Reck541f55a2011-06-07 16:34:43 -07002093 }
2094
John Reck8cc92352011-07-06 17:41:52 -07002095 public byte[] compressBitmap(Bitmap bitmap) {
2096 if (bitmap == null) {
2097 return null;
2098 }
2099 ByteArrayOutputStream stream = new ByteArrayOutputStream();
2100 bitmap.compress(CompressFormat.PNG, 100, stream);
2101 return stream.toByteArray();
2102 }
2103
John Reck26b18322011-06-21 13:08:58 -07002104 public void loadUrl(String url, Map<String, String> headers) {
2105 if (mMainView != null) {
Michael Kolba53c9892011-10-05 13:31:40 -07002106 mPageLoadProgress = INITIAL_PROGRESS;
2107 mInPageLoad = true;
Michael Kolb14612442011-06-24 13:06:29 -07002108 mCurrentState = new PageState(mContext, false, url, null);
John Reck26b18322011-06-21 13:08:58 -07002109 mWebViewController.onPageStarted(this, mMainView, null);
2110 mMainView.loadUrl(url, headers);
2111 }
2112 }
2113
Michael Kolb9ef259a2011-07-12 15:33:08 -07002114 protected void capture() {
2115 if (mMainView == null || mCapture == null) return;
John Reck4eadc342011-10-31 14:04:10 -07002116 if (mMainView.getContentWidth() <= 0 || mMainView.getContentHeight() <= 0) {
2117 return;
2118 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07002119 Canvas c = new Canvas(mCapture);
2120 final int left = mMainView.getScrollX();
2121 final int top = mMainView.getScrollY() + mMainView.getVisibleTitleHeight();
Michael Kolba3194d02011-09-07 11:23:51 -07002122 int state = c.save();
Michael Kolb9ef259a2011-07-12 15:33:08 -07002123 c.translate(-left, -top);
2124 float scale = mCaptureWidth / (float) mMainView.getWidth();
2125 c.scale(scale, scale, left, top);
John Reck8ee633f2011-08-09 16:00:35 -07002126 if (mMainView instanceof BrowserWebView) {
2127 ((BrowserWebView)mMainView).drawContent(c);
2128 } else {
2129 mMainView.draw(c);
2130 }
Michael Kolba3194d02011-09-07 11:23:51 -07002131 c.restoreToCount(state);
2132 // manually anti-alias the edges for the tilt
2133 c.drawRect(0, 0, 1, mCapture.getHeight(), sAlphaPaint);
2134 c.drawRect(mCapture.getWidth() - 1, 0, mCapture.getWidth(),
2135 mCapture.getHeight(), sAlphaPaint);
2136 c.drawRect(0, 0, mCapture.getWidth(), 1, sAlphaPaint);
2137 c.drawRect(0, mCapture.getHeight() - 1, mCapture.getWidth(),
2138 mCapture.getHeight(), sAlphaPaint);
Dianne Hackborn43cfe8a2011-08-02 16:59:35 -07002139 c.setBitmap(null);
John Reck8ee633f2011-08-09 16:00:35 -07002140 mHandler.removeMessages(MSG_CAPTURE);
John Reck1cf4b792011-07-26 10:22:22 -07002141 persistThumbnail();
John Reck8ee633f2011-08-09 16:00:35 -07002142 TabControl tc = mWebViewController.getTabControl();
2143 if (tc != null) {
2144 OnThumbnailUpdatedListener updateListener
2145 = tc.getOnThumbnailUpdatedListener();
2146 if (updateListener != null) {
2147 updateListener.onThumbnailUpdated(this);
2148 }
2149 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07002150 }
2151
2152 @Override
2153 public void onNewPicture(WebView view, Picture picture) {
2154 //update screenshot
John Reck1cf4b792011-07-26 10:22:22 -07002155 postCapture();
2156 }
2157
2158 private void postCapture() {
Michael Kolb9ef259a2011-07-12 15:33:08 -07002159 if (!mHandler.hasMessages(MSG_CAPTURE)) {
2160 mHandler.sendEmptyMessageDelayed(MSG_CAPTURE, CAPTURE_DELAY);
2161 }
2162 }
2163
John Reckef654f12011-07-12 16:42:08 -07002164 public boolean canGoBack() {
2165 return mMainView != null ? mMainView.canGoBack() : false;
2166 }
2167
2168 public boolean canGoForward() {
2169 return mMainView != null ? mMainView.canGoForward() : false;
2170 }
2171
2172 public void goBack() {
2173 if (mMainView != null) {
2174 mMainView.goBack();
2175 }
2176 }
2177
2178 public void goForward() {
2179 if (mMainView != null) {
2180 mMainView.goForward();
2181 }
2182 }
2183
Mathew Inwood1dd8e822011-08-03 14:34:29 +01002184 /**
2185 * Causes the tab back/forward stack to be cleared once, if the given URL is the next URL
2186 * to be added to the stack.
2187 *
2188 * This is used to ensure that preloaded URLs that are not subsequently seen by the user do
2189 * not appear in the back stack.
2190 */
Mathew Inwooda829d552011-09-02 14:16:25 +01002191 public void clearBackStackWhenItemAdded(Pattern urlPattern) {
2192 mClearHistoryUrlPattern = urlPattern;
Mathew Inwood1dd8e822011-08-03 14:34:29 +01002193 }
2194
John Reck1cf4b792011-07-26 10:22:22 -07002195 protected void persistThumbnail() {
John Reck4eadc342011-10-31 14:04:10 -07002196 DataController.getInstance(mContext).saveThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07002197 }
2198
2199 protected void deleteThumbnail() {
John Reck4eadc342011-10-31 14:04:10 -07002200 DataController.getInstance(mContext).deleteThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07002201 }
2202
John Reck4eadc342011-10-31 14:04:10 -07002203 void updateCaptureFromBlob(byte[] blob) {
John Reck1cf4b792011-07-26 10:22:22 -07002204 synchronized (Tab.this) {
2205 if (mCapture == null) {
2206 return;
2207 }
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002208 ByteBuffer buffer = ByteBuffer.wrap(blob);
Michael Kolbd837a112011-08-09 14:00:18 -07002209 try {
2210 mCapture.copyPixelsFromBuffer(buffer);
2211 } catch (RuntimeException rex) {
2212 Log.e(LOGTAG, "Load capture has mismatched sizes; buffer: "
2213 + buffer.capacity() + " blob: " + blob.length
2214 + "capture: " + mCapture.getByteCount());
2215 throw rex;
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002216 }
John Reck1cf4b792011-07-26 10:22:22 -07002217 }
2218 }
2219
John Reck52be4782011-08-26 15:37:29 -07002220 @Override
2221 public String toString() {
2222 StringBuilder builder = new StringBuilder(100);
2223 builder.append(mId);
2224 builder.append(") has parent: ");
2225 if (getParent() != null) {
2226 builder.append("true[");
2227 builder.append(getParent().getId());
2228 builder.append("]");
2229 } else {
2230 builder.append("false");
2231 }
2232 builder.append(", incog: ");
2233 builder.append(isPrivateBrowsingEnabled());
2234 if (!isPrivateBrowsingEnabled()) {
2235 builder.append(", title: ");
2236 builder.append(getTitle());
2237 builder.append(", url: ");
2238 builder.append(getUrl());
2239 }
2240 return builder.toString();
2241 }
2242
Steve Block4895b012011-10-03 16:26:46 +01002243 private void handleProceededAfterSslError(SslError error) {
2244 if (error.getUrl().equals(mCurrentState.mUrl)) {
2245 // The security state should currently be SECURITY_STATE_SECURE.
2246 setSecurityState(SecurityState.SECURITY_STATE_BAD_CERTIFICATE);
Steve Block08a6f0c2011-10-06 12:12:53 +01002247 mCurrentState.mSslCertificateError = error;
Steve Block4895b012011-10-03 16:26:46 +01002248 } else if (getSecurityState() == SecurityState.SECURITY_STATE_SECURE) {
Steve Block08a6f0c2011-10-06 12:12:53 +01002249 // The page's main resource is secure and this error is for a
2250 // sub-resource.
Steve Block4895b012011-10-03 16:26:46 +01002251 setSecurityState(SecurityState.SECURITY_STATE_MIXED);
2252 }
2253 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002254}