blob: 712e30afe25ef323da66d76d0674a4e3f136fb73 [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;
21import android.content.ContentResolver;
John Reckd8c74522011-06-14 08:45:00 -070022import android.content.ContentValues;
John Reck30c714c2010-12-16 17:30:34 -080023import android.content.Context;
Grace Kloba22ac16e2009-10-07 18:00:23 -070024import android.content.DialogInterface;
Michael Kolbfe251992010-07-08 15:41:55 -070025import android.content.DialogInterface.OnCancelListener;
Grace Kloba22ac16e2009-10-07 18:00:23 -070026import android.graphics.Bitmap;
John Reck8cc92352011-07-06 17:41:52 -070027import android.graphics.Bitmap.CompressFormat;
Michael Kolb9ef259a2011-07-12 15:33:08 -070028import android.graphics.BitmapFactory;
29import android.graphics.Canvas;
Michael Kolbc3af0672011-08-09 10:24:41 -070030import android.graphics.Color;
Michael Kolba3194d02011-09-07 11:23:51 -070031import android.graphics.Paint;
Michael Kolb9ef259a2011-07-12 15:33:08 -070032import android.graphics.Picture;
Michael Kolba3194d02011-09-07 11:23:51 -070033import android.graphics.PorterDuff;
34import android.graphics.PorterDuffXfermode;
Grace Kloba22ac16e2009-10-07 18:00:23 -070035import android.net.Uri;
36import android.net.http.SslError;
Grace Kloba22ac16e2009-10-07 18:00:23 -070037import android.os.Bundle;
Michael Kolb9ef259a2011-07-12 15:33:08 -070038import android.os.Handler;
Grace Kloba22ac16e2009-10-07 18:00:23 -070039import android.os.Message;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +000040import android.os.SystemClock;
Brian Carlstrom8862c1d2011-06-02 01:05:55 -070041import android.security.KeyChain;
Brian Carlstromaa09cd82011-06-09 16:04:40 -070042import android.security.KeyChainAliasCallback;
John Reck24f18262011-06-17 14:47:20 -070043import android.text.TextUtils;
Grace Kloba22ac16e2009-10-07 18:00:23 -070044import android.util.Log;
45import android.view.KeyEvent;
46import android.view.LayoutInflater;
47import android.view.View;
Grace Kloba50c241e2010-04-20 11:07:50 -070048import android.view.ViewStub;
Brian Carlstrom8862c1d2011-06-02 01:05:55 -070049import android.webkit.ClientCertRequestHandler;
Ben Murdochc42addf2010-01-28 15:19:59 +000050import android.webkit.ConsoleMessage;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -050051import android.webkit.DownloadListener;
Grace Kloba22ac16e2009-10-07 18:00:23 -070052import android.webkit.GeolocationPermissions;
53import android.webkit.HttpAuthHandler;
54import android.webkit.SslErrorHandler;
55import android.webkit.URLUtil;
56import android.webkit.ValueCallback;
John Reck6c2e2f32011-08-22 13:41:23 -070057import android.webkit.WebBackForwardList;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -050058import android.webkit.WebBackForwardListClient;
Grace Kloba22ac16e2009-10-07 18:00:23 -070059import android.webkit.WebChromeClient;
60import android.webkit.WebHistoryItem;
John Reck438bf462011-01-12 18:11:46 -080061import android.webkit.WebResourceResponse;
Grace Kloba22ac16e2009-10-07 18:00:23 -070062import android.webkit.WebStorage;
63import android.webkit.WebView;
Michael Kolb9ef259a2011-07-12 15:33:08 -070064import android.webkit.WebView.PictureListener;
John Reck2b71d6d2012-04-18 17:42:06 -070065import android.webkit.WebViewClassic;
Grace Kloba22ac16e2009-10-07 18:00:23 -070066import android.webkit.WebViewClient;
Ben Murdoch1d676b62011-01-17 12:54:24 +000067import android.widget.CheckBox;
Ben Murdoch8029a772010-11-16 11:58:21 +000068import android.widget.Toast;
Grace Kloba22ac16e2009-10-07 18:00:23 -070069
John Reck8ee633f2011-08-09 16:00:35 -070070import com.android.browser.TabControl.OnThumbnailUpdatedListener;
John Reck541f55a2011-06-07 16:34:43 -070071import com.android.browser.homepages.HomeProvider;
John Reck8cc92352011-07-06 17:41:52 -070072import com.android.browser.provider.SnapshotProvider.Snapshots;
John Reck541f55a2011-06-07 16:34:43 -070073
74import java.io.ByteArrayOutputStream;
John Reck2b71d6d2012-04-18 17:42:06 -070075import java.io.File;
John Reck68234a92012-04-19 15:27:12 -070076import java.io.IOException;
John Reck2b71d6d2012-04-18 17:42:06 -070077import java.io.OutputStream;
John Reck1cf4b792011-07-26 10:22:22 -070078import java.nio.ByteBuffer;
Michael Kolbfe251992010-07-08 15:41:55 -070079import java.util.LinkedList;
80import java.util.Map;
John Reck2b71d6d2012-04-18 17:42:06 -070081import java.util.UUID;
Michael Kolbfe251992010-07-08 15:41:55 -070082import java.util.Vector;
Mathew Inwooda829d552011-09-02 14:16:25 +010083import java.util.regex.Pattern;
John Reck8cc92352011-07-06 17:41:52 -070084import java.util.zip.GZIPOutputStream;
Michael Kolbfe251992010-07-08 15:41:55 -070085
Grace Kloba22ac16e2009-10-07 18:00:23 -070086/**
87 * Class for maintaining Tabs with a main WebView and a subwindow.
88 */
Michael Kolb9ef259a2011-07-12 15:33:08 -070089class Tab implements PictureListener {
Michael Kolb8233fac2010-10-26 16:08:53 -070090
Grace Kloba22ac16e2009-10-07 18:00:23 -070091 // Log Tag
92 private static final String LOGTAG = "Tab";
Mathew Inwood1dd8e822011-08-03 14:34:29 +010093 private static final boolean LOGD_ENABLED = com.android.browser.Browser.LOGD_ENABLED;
Ben Murdochc42addf2010-01-28 15:19:59 +000094 // Special case the logtag for messages for the Console to make it easier to
95 // filter them and match the logtag used for these messages in older versions
96 // of the browser.
97 private static final String CONSOLE_LOGTAG = "browser";
98
Michael Kolb9ef259a2011-07-12 15:33:08 -070099 private static final int MSG_CAPTURE = 42;
John Reck8ee633f2011-08-09 16:00:35 -0700100 private static final int CAPTURE_DELAY = 100;
Michael Kolba53c9892011-10-05 13:31:40 -0700101 private static final int INITIAL_PROGRESS = 5;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700102
John Reck1cf4b792011-07-26 10:22:22 -0700103 private static Bitmap sDefaultFavicon;
104
Michael Kolba3194d02011-09-07 11:23:51 -0700105 private static Paint sAlphaPaint = new Paint();
106 static {
107 sAlphaPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
108 sAlphaPaint.setColor(Color.TRANSPARENT);
109 }
110
Steve Block2466eff2011-10-03 15:33:09 +0100111 public enum SecurityState {
Steve Block4895b012011-10-03 16:26:46 +0100112 // The page's main resource does not use SSL. Note that we use this
113 // state irrespective of the SSL authentication state of sub-resources.
Steve Block2466eff2011-10-03 15:33:09 +0100114 SECURITY_STATE_NOT_SECURE,
Steve Block4895b012011-10-03 16:26:46 +0100115 // The page's main resource uses SSL and the certificate is good. The
116 // same is true of all sub-resources.
Steve Block2466eff2011-10-03 15:33:09 +0100117 SECURITY_STATE_SECURE,
Steve Block4895b012011-10-03 16:26:46 +0100118 // The page's main resource uses SSL and the certificate is good, but
119 // some sub-resources either do not use SSL or have problems with their
120 // certificates.
Steve Block2466eff2011-10-03 15:33:09 +0100121 SECURITY_STATE_MIXED,
Steve Block4895b012011-10-03 16:26:46 +0100122 // The page's main resource uses SSL but there is a problem with its
123 // certificate.
124 SECURITY_STATE_BAD_CERTIFICATE,
John Reck30c714c2010-12-16 17:30:34 -0800125 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700126
Michael Kolb14612442011-06-24 13:06:29 -0700127 Context mContext;
John Reckd8c74522011-06-14 08:45:00 -0700128 protected WebViewController mWebViewController;
Michael Kolb8233fac2010-10-26 16:08:53 -0700129
Michael Kolbc831b632011-05-11 09:30:34 -0700130 // The tab ID
John Reckd8c74522011-06-14 08:45:00 -0700131 private long mId = -1;
Michael Kolbc831b632011-05-11 09:30:34 -0700132
Grace Kloba22ac16e2009-10-07 18:00:23 -0700133 // The Geolocation permissions prompt
134 private GeolocationPermissionsPrompt mGeolocationPermissionsPrompt;
135 // Main WebView wrapper
Michael Kolba713ec82010-11-29 17:27:06 -0800136 private View mContainer;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700137 // Main WebView
138 private WebView mMainView;
139 // Subwindow container
140 private View mSubViewContainer;
141 // Subwindow WebView
142 private WebView mSubView;
143 // Saved bundle for when we are running low on memory. It contains the
144 // information needed to restore the WebView if the user goes back to the
145 // tab.
146 private Bundle mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700147 // Parent Tab. This is the Tab that created this Tab, or null if the Tab was
148 // created by the UI
Michael Kolbc831b632011-05-11 09:30:34 -0700149 private Tab mParent;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700150 // Tab that constructed by this Tab. This is used when this Tab is
151 // destroyed, it clears all mParentTab values in the children.
Michael Kolbc831b632011-05-11 09:30:34 -0700152 private Vector<Tab> mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700153 // If true, the tab is in the foreground of the current activity.
154 private boolean mInForeground;
Michael Kolb8233fac2010-10-26 16:08:53 -0700155 // If true, the tab is in page loading state (after onPageStarted,
156 // before onPageFinsihed)
157 private boolean mInPageLoad;
John Reck30c714c2010-12-16 17:30:34 -0800158 // The last reported progress of the current page
159 private int mPageLoadProgress;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000160 // The time the load started, used to find load page time
161 private long mLoadStartTime;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700162 // Application identifier used to find tabs that another application wants
163 // to reuse.
164 private String mAppId;
Michael Kolbe28b3472011-08-04 16:54:31 -0700165 // flag to indicate if tab should be closed on back
166 private boolean mCloseOnBack;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700167 // Keep the original url around to avoid killing the old WebView if the url
168 // has not changed.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700169 // Error console for the tab
170 private ErrorConsoleView mErrorConsole;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -0500171 // The listener that gets invoked when a download is started from the
172 // mMainView
173 private final DownloadListener mDownloadListener;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500174 // Listener used to know when we move forward or back in the history list.
175 private final WebBackForwardListClient mWebBackForwardListClient;
John Recke969cc52010-12-21 17:24:43 -0800176 private DataController mDataController;
Patrick Scott92066772011-03-10 08:46:27 -0500177 // State of the auto-login request.
178 private DeviceAccountLogin mDeviceAccountLogin;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700179
180 // AsyncTask for downloading touch icons
181 DownloadTouchIcon mTouchIconLoader;
182
John Reck35e9dd62011-04-25 09:01:54 -0700183 private BrowserSettings mSettings;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700184 private int mCaptureWidth;
185 private int mCaptureHeight;
186 private Bitmap mCapture;
187 private Handler mHandler;
188
Mathew Inwood1dd8e822011-08-03 14:34:29 +0100189 /**
Mathew Inwood9ad1eac2011-09-15 11:29:50 +0100190 * See {@link #clearBackStackWhenItemAdded(String)}.
Mathew Inwood1dd8e822011-08-03 14:34:29 +0100191 */
Mathew Inwooda829d552011-09-02 14:16:25 +0100192 private Pattern mClearHistoryUrlPattern;
Mathew Inwood1dd8e822011-08-03 14:34:29 +0100193
John Reck1cf4b792011-07-26 10:22:22 -0700194 private static synchronized Bitmap getDefaultFavicon(Context context) {
195 if (sDefaultFavicon == null) {
196 sDefaultFavicon = BitmapFactory.decodeResource(
197 context.getResources(), R.drawable.app_web_browser_sm);
198 }
199 return sDefaultFavicon;
200 }
Michael Kolbeb95db42011-03-03 10:38:40 -0800201
John Reck30c714c2010-12-16 17:30:34 -0800202 // All the state needed for a page
John Reckd8c74522011-06-14 08:45:00 -0700203 protected static class PageState {
John Reck30c714c2010-12-16 17:30:34 -0800204 String mUrl;
John Reckdb22ec42011-06-29 11:31:24 -0700205 String mOriginalUrl;
John Reck30c714c2010-12-16 17:30:34 -0800206 String mTitle;
Steve Block2466eff2011-10-03 15:33:09 +0100207 SecurityState mSecurityState;
Steve Block08a6f0c2011-10-06 12:12:53 +0100208 // This is non-null only when mSecurityState is SECURITY_STATE_BAD_CERTIFICATE.
209 SslError mSslCertificateError;
John Reck30c714c2010-12-16 17:30:34 -0800210 Bitmap mFavicon;
Steve Block08a6f0c2011-10-06 12:12:53 +0100211 boolean mIsBookmarkedSite;
212 boolean mIncognito;
John Reck30c714c2010-12-16 17:30:34 -0800213
214 PageState(Context c, boolean incognito) {
John Reck502a3532011-08-16 14:21:46 -0700215 mIncognito = incognito;
216 if (mIncognito) {
John Reckdb22ec42011-06-29 11:31:24 -0700217 mOriginalUrl = mUrl = "browser:incognito";
John Reck30c714c2010-12-16 17:30:34 -0800218 mTitle = c.getString(R.string.new_incognito_tab);
John Reck30c714c2010-12-16 17:30:34 -0800219 } else {
John Reckdb22ec42011-06-29 11:31:24 -0700220 mOriginalUrl = mUrl = "";
John Reck30c714c2010-12-16 17:30:34 -0800221 mTitle = c.getString(R.string.new_tab);
John Reck30c714c2010-12-16 17:30:34 -0800222 }
Steve Block2466eff2011-10-03 15:33:09 +0100223 mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
John Reck30c714c2010-12-16 17:30:34 -0800224 }
225
226 PageState(Context c, boolean incognito, String url, Bitmap favicon) {
John Reck502a3532011-08-16 14:21:46 -0700227 mIncognito = incognito;
John Reckdb22ec42011-06-29 11:31:24 -0700228 mOriginalUrl = mUrl = url;
John Reck30c714c2010-12-16 17:30:34 -0800229 if (URLUtil.isHttpsUrl(url)) {
Steve Block2466eff2011-10-03 15:33:09 +0100230 mSecurityState = SecurityState.SECURITY_STATE_SECURE;
John Reck30c714c2010-12-16 17:30:34 -0800231 } else {
Steve Block2466eff2011-10-03 15:33:09 +0100232 mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
John Reck30c714c2010-12-16 17:30:34 -0800233 }
John Reck1cf4b792011-07-26 10:22:22 -0700234 mFavicon = favicon;
John Reck30c714c2010-12-16 17:30:34 -0800235 }
John Reck1cf4b792011-07-26 10:22:22 -0700236
Grace Kloba22ac16e2009-10-07 18:00:23 -0700237 }
238
John Reck30c714c2010-12-16 17:30:34 -0800239 // The current/loading page's state
John Reckd8c74522011-06-14 08:45:00 -0700240 protected PageState mCurrentState;
John Reck30c714c2010-12-16 17:30:34 -0800241
Grace Kloba22ac16e2009-10-07 18:00:23 -0700242 // Used for saving and restoring each Tab
Michael Kolbc831b632011-05-11 09:30:34 -0700243 static final String ID = "ID";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700244 static final String CURRURL = "currentUrl";
245 static final String CURRTITLE = "currentTitle";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700246 static final String PARENTTAB = "parentTab";
247 static final String APPID = "appid";
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700248 static final String INCOGNITO = "privateBrowsingEnabled";
John Reckb0a86db2011-05-24 14:05:58 -0700249 static final String USERAGENT = "useragent";
Michael Kolbe28b3472011-08-04 16:54:31 -0700250 static final String CLOSEFLAG = "closeOnBack";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700251
Grace Kloba22ac16e2009-10-07 18:00:23 -0700252 // Container class for the next error dialog that needs to be displayed
253 private class ErrorDialog {
254 public final int mTitle;
255 public final String mDescription;
256 public final int mError;
257 ErrorDialog(int title, String desc, int error) {
258 mTitle = title;
259 mDescription = desc;
260 mError = error;
261 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700262 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700263
264 private void processNextError() {
265 if (mQueuedErrors == null) {
266 return;
267 }
268 // The first one is currently displayed so just remove it.
269 mQueuedErrors.removeFirst();
270 if (mQueuedErrors.size() == 0) {
271 mQueuedErrors = null;
272 return;
273 }
274 showError(mQueuedErrors.getFirst());
275 }
276
277 private DialogInterface.OnDismissListener mDialogListener =
278 new DialogInterface.OnDismissListener() {
279 public void onDismiss(DialogInterface d) {
280 processNextError();
281 }
282 };
283 private LinkedList<ErrorDialog> mQueuedErrors;
284
285 private void queueError(int err, String desc) {
286 if (mQueuedErrors == null) {
287 mQueuedErrors = new LinkedList<ErrorDialog>();
288 }
289 for (ErrorDialog d : mQueuedErrors) {
290 if (d.mError == err) {
291 // Already saw a similar error, ignore the new one.
292 return;
293 }
294 }
295 ErrorDialog errDialog = new ErrorDialog(
296 err == WebViewClient.ERROR_FILE_NOT_FOUND ?
297 R.string.browserFrameFileErrorLabel :
298 R.string.browserFrameNetworkErrorLabel,
299 desc, err);
300 mQueuedErrors.addLast(errDialog);
301
302 // Show the dialog now if the queue was empty and it is in foreground
303 if (mQueuedErrors.size() == 1 && mInForeground) {
304 showError(errDialog);
305 }
306 }
307
308 private void showError(ErrorDialog errDialog) {
309 if (mInForeground) {
Michael Kolb14612442011-06-24 13:06:29 -0700310 AlertDialog d = new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700311 .setTitle(errDialog.mTitle)
312 .setMessage(errDialog.mDescription)
313 .setPositiveButton(R.string.ok, null)
314 .create();
315 d.setOnDismissListener(mDialogListener);
316 d.show();
317 }
318 }
319
320 // -------------------------------------------------------------------------
321 // WebViewClient implementation for the main WebView
322 // -------------------------------------------------------------------------
323
324 private final WebViewClient mWebViewClient = new WebViewClient() {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500325 private Message mDontResend;
326 private Message mResend;
Michael Kolb47bd1e42011-09-01 15:25:00 -0700327
328 private boolean providersDiffer(String url, String otherUrl) {
329 Uri uri1 = Uri.parse(url);
330 Uri uri2 = Uri.parse(otherUrl);
331 return !uri1.getEncodedAuthority().equals(uri2.getEncodedAuthority());
332 }
333
Grace Kloba22ac16e2009-10-07 18:00:23 -0700334 @Override
335 public void onPageStarted(WebView view, String url, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700336 mInPageLoad = true;
Michael Kolba53c9892011-10-05 13:31:40 -0700337 mPageLoadProgress = INITIAL_PROGRESS;
Michael Kolb14612442011-06-24 13:06:29 -0700338 mCurrentState = new PageState(mContext,
John Reck30c714c2010-12-16 17:30:34 -0800339 view.isPrivateBrowsingEnabled(), url, favicon);
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000340 mLoadStartTime = SystemClock.uptimeMillis();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700341
342 // If we start a touch icon load and then load a new page, we don't
343 // want to cancel the current touch icon loader. But, we do want to
344 // create a new one when the touch icon url is known.
345 if (mTouchIconLoader != null) {
346 mTouchIconLoader.mTab = null;
347 mTouchIconLoader = null;
348 }
349
350 // reset the error console
351 if (mErrorConsole != null) {
352 mErrorConsole.clearErrorMessages();
Michael Kolb8233fac2010-10-26 16:08:53 -0700353 if (mWebViewController.shouldShowErrorConsole()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700354 mErrorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
355 }
356 }
357
Patrick Scott92066772011-03-10 08:46:27 -0500358 // Cancel the auto-login process.
359 if (mDeviceAccountLogin != null) {
360 mDeviceAccountLogin.cancel();
361 mDeviceAccountLogin = null;
362 mWebViewController.hideAutoLogin(Tab.this);
363 }
364
Grace Kloba22ac16e2009-10-07 18:00:23 -0700365 // finally update the UI in the activity if it is in the foreground
John Reck324d4402011-01-11 16:56:42 -0800366 mWebViewController.onPageStarted(Tab.this, view, favicon);
Leon Scroggins4cd97792010-12-03 15:31:56 -0500367
John Recke969cc52010-12-21 17:24:43 -0800368 updateBookmarkedStatus();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700369 }
370
371 @Override
372 public void onPageFinished(WebView view, String url) {
John Reck5b691842010-11-29 11:21:13 -0800373 if (!isPrivateBrowsingEnabled()) {
374 LogTag.logPageFinishedLoading(
375 url, SystemClock.uptimeMillis() - mLoadStartTime);
376 }
John Reck1cf4b792011-07-26 10:22:22 -0700377 syncCurrentState(view, url);
John Reck324d4402011-01-11 16:56:42 -0800378 mWebViewController.onPageFinished(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700379 }
380
381 // return true if want to hijack the url to let another app to handle it
382 @Override
383 public boolean shouldOverrideUrlLoading(WebView view, String url) {
384 if (mInForeground) {
Michael Kolb18eb3772010-12-10 14:29:51 -0800385 return mWebViewController.shouldOverrideUrlLoading(Tab.this,
386 view, url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700387 } else {
388 return false;
389 }
390 }
391
392 /**
Steve Block2466eff2011-10-03 15:33:09 +0100393 * Updates the security state. This method is called when we discover
394 * another resource to be loaded for this page (for example,
395 * javascript). While we update the security state, we do not update
396 * the lock icon until we are done loading, as it is slightly more
397 * secure this way.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700398 */
399 @Override
400 public void onLoadResource(WebView view, String url) {
401 if (url != null && url.length() > 0) {
402 // It is only if the page claims to be secure that we may have
Steve Block2466eff2011-10-03 15:33:09 +0100403 // to update the security state:
404 if (mCurrentState.mSecurityState == SecurityState.SECURITY_STATE_SECURE) {
405 // If NOT a 'safe' url, change the state to mixed content!
Grace Kloba22ac16e2009-10-07 18:00:23 -0700406 if (!(URLUtil.isHttpsUrl(url) || URLUtil.isDataUrl(url)
407 || URLUtil.isAboutUrl(url))) {
Steve Block2466eff2011-10-03 15:33:09 +0100408 mCurrentState.mSecurityState = SecurityState.SECURITY_STATE_MIXED;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700409 }
410 }
411 }
412 }
413
414 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700415 * Show a dialog informing the user of the network error reported by
416 * WebCore if it is in the foreground.
417 */
418 @Override
419 public void onReceivedError(WebView view, int errorCode,
420 String description, String failingUrl) {
421 if (errorCode != WebViewClient.ERROR_HOST_LOOKUP &&
422 errorCode != WebViewClient.ERROR_CONNECT &&
423 errorCode != WebViewClient.ERROR_BAD_URL &&
424 errorCode != WebViewClient.ERROR_UNSUPPORTED_SCHEME &&
425 errorCode != WebViewClient.ERROR_FILE) {
426 queueError(errorCode, description);
Jeff Hamilton47654f42010-09-07 09:57:51 -0500427
Selim Gurun3da06b82011-10-10 13:58:12 -0700428 // Don't log URLs when in private browsing mode
429 if (!isPrivateBrowsingEnabled()) {
430 Log.e(LOGTAG, "onReceivedError " + errorCode + " " + failingUrl
Jeff Hamilton47654f42010-09-07 09:57:51 -0500431 + " " + description);
Selim Gurun3da06b82011-10-10 13:58:12 -0700432 }
Jeff Hamilton47654f42010-09-07 09:57:51 -0500433 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700434 }
435
436 /**
437 * Check with the user if it is ok to resend POST data as the page they
438 * are trying to navigate to is the result of a POST.
439 */
440 @Override
441 public void onFormResubmission(WebView view, final Message dontResend,
442 final Message resend) {
443 if (!mInForeground) {
444 dontResend.sendToTarget();
445 return;
446 }
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500447 if (mDontResend != null) {
448 Log.w(LOGTAG, "onFormResubmission should not be called again "
449 + "while dialog is still up");
450 dontResend.sendToTarget();
451 return;
452 }
453 mDontResend = dontResend;
454 mResend = resend;
Michael Kolb14612442011-06-24 13:06:29 -0700455 new AlertDialog.Builder(mContext).setTitle(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700456 R.string.browserFrameFormResubmitLabel).setMessage(
457 R.string.browserFrameFormResubmitMessage)
458 .setPositiveButton(R.string.ok,
459 new DialogInterface.OnClickListener() {
460 public void onClick(DialogInterface dialog,
461 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500462 if (mResend != null) {
463 mResend.sendToTarget();
464 mResend = null;
465 mDontResend = null;
466 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700467 }
468 }).setNegativeButton(R.string.cancel,
469 new DialogInterface.OnClickListener() {
470 public void onClick(DialogInterface dialog,
471 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500472 if (mDontResend != null) {
473 mDontResend.sendToTarget();
474 mResend = null;
475 mDontResend = null;
476 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700477 }
478 }).setOnCancelListener(new OnCancelListener() {
479 public void onCancel(DialogInterface dialog) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500480 if (mDontResend != null) {
481 mDontResend.sendToTarget();
482 mResend = null;
483 mDontResend = null;
484 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700485 }
486 }).show();
487 }
488
489 /**
490 * Insert the url into the visited history database.
491 * @param url The url to be inserted.
492 * @param isReload True if this url is being reloaded.
493 * FIXME: Not sure what to do when reloading the page.
494 */
495 @Override
496 public void doUpdateVisitedHistory(WebView view, String url,
497 boolean isReload) {
John Reck324d4402011-01-11 16:56:42 -0800498 mWebViewController.doUpdateVisitedHistory(Tab.this, isReload);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700499 }
500
501 /**
502 * Displays SSL error(s) dialog to the user.
503 */
504 @Override
505 public void onReceivedSslError(final WebView view,
506 final SslErrorHandler handler, final SslError error) {
507 if (!mInForeground) {
508 handler.cancel();
Steve Block2466eff2011-10-03 15:33:09 +0100509 setSecurityState(SecurityState.SECURITY_STATE_NOT_SECURE);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700510 return;
511 }
John Reck35e9dd62011-04-25 09:01:54 -0700512 if (mSettings.showSecurityWarnings()) {
John Reckcb28b2c2011-08-26 17:39:44 -0700513 new AlertDialog.Builder(mContext)
514 .setTitle(R.string.security_warning)
515 .setMessage(R.string.ssl_warnings_header)
516 .setIcon(android.R.drawable.ic_dialog_alert)
517 .setPositiveButton(R.string.ssl_continue,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700518 new DialogInterface.OnClickListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700519 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700520 public void onClick(DialogInterface dialog,
521 int whichButton) {
522 handler.proceed();
Steve Block4895b012011-10-03 16:26:46 +0100523 handleProceededAfterSslError(error);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700524 }
John Reckcb28b2c2011-08-26 17:39:44 -0700525 })
526 .setNeutralButton(R.string.view_certificate,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700527 new DialogInterface.OnClickListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700528 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700529 public void onClick(DialogInterface dialog,
530 int whichButton) {
John Reckcb28b2c2011-08-26 17:39:44 -0700531 mWebViewController.showSslCertificateOnError(
532 view, handler, error);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700533 }
John Reckcb28b2c2011-08-26 17:39:44 -0700534 })
535 .setNegativeButton(R.string.ssl_go_back,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700536 new DialogInterface.OnClickListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700537 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700538 public void onClick(DialogInterface dialog,
539 int whichButton) {
John Reck30c714c2010-12-16 17:30:34 -0800540 dialog.cancel();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700541 }
John Reckcb28b2c2011-08-26 17:39:44 -0700542 })
543 .setOnCancelListener(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700544 new DialogInterface.OnCancelListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700545 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700546 public void onCancel(DialogInterface dialog) {
547 handler.cancel();
Steve Block2466eff2011-10-03 15:33:09 +0100548 setSecurityState(SecurityState.SECURITY_STATE_NOT_SECURE);
John Reck30c714c2010-12-16 17:30:34 -0800549 mWebViewController.onUserCanceledSsl(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700550 }
John Reckcb28b2c2011-08-26 17:39:44 -0700551 })
552 .show();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700553 } else {
554 handler.proceed();
555 }
556 }
557
558 /**
Steve Block4895b012011-10-03 16:26:46 +0100559 * Called when an SSL error occurred while loading a resource, but the
560 * WebView but chose to proceed anyway based on a decision retained
561 * from a previous response to onReceivedSslError(). We update our
562 * security state to reflect this.
563 */
564 @Override
565 public void onProceededAfterSslError(WebView view, SslError error) {
566 handleProceededAfterSslError(error);
567 }
568
569 /**
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700570 * Displays client certificate request to the user.
571 */
572 @Override
573 public void onReceivedClientCertRequest(final WebView view,
574 final ClientCertRequestHandler handler, final String host_and_port) {
575 if (!mInForeground) {
576 handler.ignore();
577 return;
578 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700579 int colon = host_and_port.lastIndexOf(':');
580 String host;
581 int port;
582 if (colon == -1) {
583 host = host_and_port;
584 port = -1;
585 } else {
586 String portString = host_and_port.substring(colon + 1);
587 try {
588 port = Integer.parseInt(portString);
589 host = host_and_port.substring(0, colon);
590 } catch (NumberFormatException e) {
591 host = host_and_port;
592 port = -1;
593 }
594 }
Michael Kolb14612442011-06-24 13:06:29 -0700595 KeyChain.choosePrivateKeyAlias(
596 mWebViewController.getActivity(), new KeyChainAliasCallback() {
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700597 @Override public void alias(String alias) {
598 if (alias == null) {
599 handler.cancel();
600 return;
601 }
Michael Kolb14612442011-06-24 13:06:29 -0700602 new KeyChainLookup(mContext, handler, alias).execute();
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700603 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700604 }, null, null, host, port, null);
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700605 }
606
607 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700608 * Handles an HTTP authentication request.
609 *
610 * @param handler The authentication handler
611 * @param host The host
612 * @param realm The realm
613 */
614 @Override
615 public void onReceivedHttpAuthRequest(WebView view,
616 final HttpAuthHandler handler, final String host,
617 final String realm) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700618 mWebViewController.onReceivedHttpAuthRequest(Tab.this, view, handler, host, realm);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700619 }
620
621 @Override
John Reck438bf462011-01-12 18:11:46 -0800622 public WebResourceResponse shouldInterceptRequest(WebView view,
623 String url) {
624 WebResourceResponse res = HomeProvider.shouldInterceptRequest(
Michael Kolb14612442011-06-24 13:06:29 -0700625 mContext, url);
John Reck438bf462011-01-12 18:11:46 -0800626 return res;
627 }
628
629 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700630 public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
631 if (!mInForeground) {
632 return false;
633 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700634 return mWebViewController.shouldOverrideKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700635 }
636
637 @Override
638 public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700639 if (!mInForeground) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700640 return;
641 }
John Reck997b1b72012-04-19 18:08:25 -0700642 if (!mWebViewController.onUnhandledKeyEvent(event)) {
643 super.onUnhandledKeyEvent(view, event);
644 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700645 }
Patrick Scott92066772011-03-10 08:46:27 -0500646
647 @Override
648 public void onReceivedLoginRequest(WebView view, String realm,
649 String account, String args) {
Michael Kolb14612442011-06-24 13:06:29 -0700650 new DeviceAccountLogin(mWebViewController.getActivity(), view, Tab.this, mWebViewController)
Patrick Scott92066772011-03-10 08:46:27 -0500651 .handleLogin(realm, account, args);
652 }
653
Grace Kloba22ac16e2009-10-07 18:00:23 -0700654 };
655
John Reck1cf4b792011-07-26 10:22:22 -0700656 private void syncCurrentState(WebView view, String url) {
657 // Sync state (in case of stop/timeout)
658 mCurrentState.mUrl = view.getUrl();
659 if (mCurrentState.mUrl == null) {
John Reck8bcafc12011-08-29 16:43:02 -0700660 mCurrentState.mUrl = "";
John Reck1cf4b792011-07-26 10:22:22 -0700661 }
662 mCurrentState.mOriginalUrl = view.getOriginalUrl();
663 mCurrentState.mTitle = view.getTitle();
664 mCurrentState.mFavicon = view.getFavicon();
665 if (!URLUtil.isHttpsUrl(mCurrentState.mUrl)) {
666 // In case we stop when loading an HTTPS page from an HTTP page
667 // but before a provisional load occurred
Steve Block2466eff2011-10-03 15:33:09 +0100668 mCurrentState.mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
Steve Block08a6f0c2011-10-06 12:12:53 +0100669 mCurrentState.mSslCertificateError = null;
John Reck1cf4b792011-07-26 10:22:22 -0700670 }
John Reck502a3532011-08-16 14:21:46 -0700671 mCurrentState.mIncognito = view.isPrivateBrowsingEnabled();
John Reck1cf4b792011-07-26 10:22:22 -0700672 }
673
Patrick Scott92066772011-03-10 08:46:27 -0500674 // Called by DeviceAccountLogin when the Tab needs to have the auto-login UI
675 // displayed.
676 void setDeviceAccountLogin(DeviceAccountLogin login) {
677 mDeviceAccountLogin = login;
678 }
679
680 // Returns non-null if the title bar should display the auto-login UI.
681 DeviceAccountLogin getDeviceAccountLogin() {
682 return mDeviceAccountLogin;
683 }
684
Grace Kloba22ac16e2009-10-07 18:00:23 -0700685 // -------------------------------------------------------------------------
686 // WebChromeClient implementation for the main WebView
687 // -------------------------------------------------------------------------
688
689 private final WebChromeClient mWebChromeClient = new WebChromeClient() {
690 // Helper method to create a new tab or sub window.
691 private void createWindow(final boolean dialog, final Message msg) {
692 WebView.WebViewTransport transport =
693 (WebView.WebViewTransport) msg.obj;
694 if (dialog) {
695 createSubWindow();
Michael Kolb8233fac2010-10-26 16:08:53 -0700696 mWebViewController.attachSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700697 transport.setWebView(mSubView);
698 } else {
Michael Kolb7bcafde2011-05-09 13:55:59 -0700699 final Tab newTab = mWebViewController.openTab(null,
John Reck5949c662011-05-27 09:52:29 -0700700 Tab.this, true, true);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700701 transport.setWebView(newTab.getWebView());
702 }
703 msg.sendToTarget();
704 }
705
706 @Override
707 public boolean onCreateWindow(WebView view, final boolean dialog,
708 final boolean userGesture, final Message resultMsg) {
709 // only allow new window or sub window for the foreground case
710 if (!mInForeground) {
711 return false;
712 }
713 // Short-circuit if we can't create any more tabs or sub windows.
714 if (dialog && mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -0700715 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700716 .setTitle(R.string.too_many_subwindows_dialog_title)
717 .setIcon(android.R.drawable.ic_dialog_alert)
718 .setMessage(R.string.too_many_subwindows_dialog_message)
719 .setPositiveButton(R.string.ok, null)
720 .show();
721 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700722 } else if (!mWebViewController.getTabControl().canCreateNewTab()) {
Michael Kolb14612442011-06-24 13:06:29 -0700723 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700724 .setTitle(R.string.too_many_windows_dialog_title)
725 .setIcon(android.R.drawable.ic_dialog_alert)
726 .setMessage(R.string.too_many_windows_dialog_message)
727 .setPositiveButton(R.string.ok, null)
728 .show();
729 return false;
730 }
731
732 // Short-circuit if this was a user gesture.
733 if (userGesture) {
734 createWindow(dialog, resultMsg);
735 return true;
736 }
737
738 // Allow the popup and create the appropriate window.
739 final AlertDialog.OnClickListener allowListener =
740 new AlertDialog.OnClickListener() {
741 public void onClick(DialogInterface d,
742 int which) {
743 createWindow(dialog, resultMsg);
744 }
745 };
746
747 // Block the popup by returning a null WebView.
748 final AlertDialog.OnClickListener blockListener =
749 new AlertDialog.OnClickListener() {
750 public void onClick(DialogInterface d, int which) {
751 resultMsg.sendToTarget();
752 }
753 };
754
755 // Build a confirmation dialog to display to the user.
756 final AlertDialog d =
Michael Kolb14612442011-06-24 13:06:29 -0700757 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700758 .setIcon(android.R.drawable.ic_dialog_alert)
759 .setMessage(R.string.popup_window_attempt)
760 .setPositiveButton(R.string.allow, allowListener)
761 .setNegativeButton(R.string.block, blockListener)
762 .setCancelable(false)
763 .create();
764
765 // Show the confirmation dialog.
766 d.show();
767 return true;
768 }
769
770 @Override
Patrick Scotteb5061b2009-11-18 15:00:30 -0500771 public void onRequestFocus(WebView view) {
772 if (!mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700773 mWebViewController.switchToTab(Tab.this);
Patrick Scotteb5061b2009-11-18 15:00:30 -0500774 }
775 }
776
777 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700778 public void onCloseWindow(WebView window) {
Michael Kolbc831b632011-05-11 09:30:34 -0700779 if (mParent != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700780 // JavaScript can only close popup window.
781 if (mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700782 mWebViewController.switchToTab(mParent);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700783 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700784 mWebViewController.closeTab(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700785 }
786 }
787
788 @Override
789 public void onProgressChanged(WebView view, int newProgress) {
John Reck30c714c2010-12-16 17:30:34 -0800790 mPageLoadProgress = newProgress;
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800791 if (newProgress == 100) {
792 mInPageLoad = false;
793 }
John Reck30c714c2010-12-16 17:30:34 -0800794 mWebViewController.onProgressChanged(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700795 }
796
797 @Override
Leon Scroggins21d9b902010-03-11 09:33:11 -0500798 public void onReceivedTitle(WebView view, final String title) {
John Reck30c714c2010-12-16 17:30:34 -0800799 mCurrentState.mTitle = title;
Michael Kolb8233fac2010-10-26 16:08:53 -0700800 mWebViewController.onReceivedTitle(Tab.this, title);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700801 }
802
803 @Override
804 public void onReceivedIcon(WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -0800805 mCurrentState.mFavicon = icon;
Michael Kolb8233fac2010-10-26 16:08:53 -0700806 mWebViewController.onFavicon(Tab.this, view, icon);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700807 }
808
809 @Override
810 public void onReceivedTouchIconUrl(WebView view, String url,
811 boolean precomposed) {
Michael Kolb14612442011-06-24 13:06:29 -0700812 final ContentResolver cr = mContext.getContentResolver();
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400813 // Let precomposed icons take precedence over non-composed
814 // icons.
815 if (precomposed && mTouchIconLoader != null) {
816 mTouchIconLoader.cancel(false);
817 mTouchIconLoader = null;
818 }
819 // Have only one async task at a time.
820 if (mTouchIconLoader == null) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700821 mTouchIconLoader = new DownloadTouchIcon(Tab.this,
Michael Kolb14612442011-06-24 13:06:29 -0700822 mContext, cr, view);
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400823 mTouchIconLoader.execute(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700824 }
825 }
826
827 @Override
828 public void onShowCustomView(View view,
829 WebChromeClient.CustomViewCallback callback) {
Michael Kolb14612442011-06-24 13:06:29 -0700830 Activity activity = mWebViewController.getActivity();
831 if (activity != null) {
832 onShowCustomView(view, activity.getRequestedOrientation(), callback);
833 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400834 }
835
836 @Override
837 public void onShowCustomView(View view, int requestedOrientation,
838 WebChromeClient.CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700839 if (mInForeground) mWebViewController.showCustomView(Tab.this, view,
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400840 requestedOrientation, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700841 }
842
843 @Override
844 public void onHideCustomView() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700845 if (mInForeground) mWebViewController.hideCustomView();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700846 }
847
848 /**
849 * The origin has exceeded its database quota.
850 * @param url the URL that exceeded the quota
851 * @param databaseIdentifier the identifier of the database on which the
852 * transaction that caused the quota overflow was run
853 * @param currentQuota the current quota for the origin.
854 * @param estimatedSize the estimated size of the database.
855 * @param totalUsedQuota is the sum of all origins' quota.
856 * @param quotaUpdater The callback to run when a decision to allow or
857 * deny quota has been made. Don't forget to call this!
858 */
859 @Override
860 public void onExceededDatabaseQuota(String url,
861 String databaseIdentifier, long currentQuota, long estimatedSize,
862 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -0700863 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -0700864 .onExceededDatabaseQuota(url, databaseIdentifier,
865 currentQuota, estimatedSize, totalUsedQuota,
866 quotaUpdater);
867 }
868
869 /**
870 * The Application Cache has exceeded its max size.
871 * @param spaceNeeded is the amount of disk space that would be needed
872 * in order for the last appcache operation to succeed.
873 * @param totalUsedQuota is the sum of all origins' quota.
874 * @param quotaUpdater A callback to inform the WebCore thread that a
875 * new app cache size is available. This callback must always
876 * be executed at some point to ensure that the sleeping
877 * WebCore thread is woken up.
878 */
879 @Override
880 public void onReachedMaxAppCacheSize(long spaceNeeded,
881 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -0700882 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -0700883 .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota,
884 quotaUpdater);
885 }
886
887 /**
888 * Instructs the browser to show a prompt to ask the user to set the
889 * Geolocation permission state for the specified origin.
890 * @param origin The origin for which Geolocation permissions are
891 * requested.
892 * @param callback The callback to call once the user has set the
893 * Geolocation permission state.
894 */
895 @Override
896 public void onGeolocationPermissionsShowPrompt(String origin,
897 GeolocationPermissions.Callback callback) {
898 if (mInForeground) {
Grace Kloba50c241e2010-04-20 11:07:50 -0700899 getGeolocationPermissionsPrompt().show(origin, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700900 }
901 }
902
903 /**
904 * Instructs the browser to hide the Geolocation permissions prompt.
905 */
906 @Override
907 public void onGeolocationPermissionsHidePrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -0700908 if (mInForeground && mGeolocationPermissionsPrompt != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700909 mGeolocationPermissionsPrompt.hide();
910 }
911 }
912
Ben Murdoch65acc352009-11-19 18:16:04 +0000913 /* Adds a JavaScript error message to the system log and if the JS
914 * console is enabled in the about:debug options, to that console
915 * also.
Ben Murdochc42addf2010-01-28 15:19:59 +0000916 * @param consoleMessage the message object.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700917 */
918 @Override
Ben Murdochc42addf2010-01-28 15:19:59 +0000919 public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700920 if (mInForeground) {
921 // call getErrorConsole(true) so it will create one if needed
922 ErrorConsoleView errorConsole = getErrorConsole(true);
Ben Murdochc42addf2010-01-28 15:19:59 +0000923 errorConsole.addErrorMessage(consoleMessage);
Michael Kolb8233fac2010-10-26 16:08:53 -0700924 if (mWebViewController.shouldShowErrorConsole()
925 && errorConsole.getShowState() !=
926 ErrorConsoleView.SHOW_MAXIMIZED) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700927 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
928 }
929 }
Ben Murdochc42addf2010-01-28 15:19:59 +0000930
Jeff Hamilton47654f42010-09-07 09:57:51 -0500931 // Don't log console messages in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -0700932 if (isPrivateBrowsingEnabled()) return true;
Jeff Hamilton47654f42010-09-07 09:57:51 -0500933
Ben Murdochc42addf2010-01-28 15:19:59 +0000934 String message = "Console: " + consoleMessage.message() + " "
935 + consoleMessage.sourceId() + ":"
936 + consoleMessage.lineNumber();
937
938 switch (consoleMessage.messageLevel()) {
939 case TIP:
940 Log.v(CONSOLE_LOGTAG, message);
941 break;
942 case LOG:
943 Log.i(CONSOLE_LOGTAG, message);
944 break;
945 case WARNING:
946 Log.w(CONSOLE_LOGTAG, message);
947 break;
948 case ERROR:
949 Log.e(CONSOLE_LOGTAG, message);
950 break;
951 case DEBUG:
952 Log.d(CONSOLE_LOGTAG, message);
953 break;
954 }
955
956 return true;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700957 }
958
959 /**
960 * Ask the browser for an icon to represent a <video> element.
961 * This icon will be used if the Web page did not specify a poster attribute.
962 * @return Bitmap The icon or null if no such icon is available.
963 */
964 @Override
965 public Bitmap getDefaultVideoPoster() {
966 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700967 return mWebViewController.getDefaultVideoPoster();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700968 }
969 return null;
970 }
971
972 /**
973 * Ask the host application for a custom progress view to show while
974 * a <video> is loading.
975 * @return View The progress view.
976 */
977 @Override
978 public View getVideoLoadingProgressView() {
979 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700980 return mWebViewController.getVideoLoadingProgressView();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700981 }
982 return null;
983 }
984
985 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +0000986 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700987 if (mInForeground) {
Ben Murdoch8cad4132012-01-11 10:56:43 +0000988 mWebViewController.openFileChooser(uploadMsg, acceptType, capture);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700989 } else {
990 uploadMsg.onReceiveValue(null);
991 }
992 }
993
994 /**
995 * Deliver a list of already-visited URLs
996 */
997 @Override
998 public void getVisitedHistory(final ValueCallback<String[]> callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700999 mWebViewController.getVisitedHistory(callback);
1000 }
Ben Murdoch8029a772010-11-16 11:58:21 +00001001
1002 @Override
1003 public void setupAutoFill(Message message) {
1004 // Prompt the user to set up their profile.
1005 final Message msg = message;
Michael Kolb14612442011-06-24 13:06:29 -07001006 AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
1007 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
Ben Murdoch1d676b62011-01-17 12:54:24 +00001008 Context.LAYOUT_INFLATER_SERVICE);
1009 final View layout = inflater.inflate(R.layout.setup_autofill_dialog, null);
1010
1011 builder.setView(layout)
1012 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1013 @Override
1014 public void onClick(DialogInterface dialog, int id) {
1015 CheckBox disableAutoFill = (CheckBox) layout.findViewById(
1016 R.id.setup_autofill_dialog_disable_autofill);
1017
1018 if (disableAutoFill.isChecked()) {
1019 // Disable autofill and show a toast with how to turn it on again.
John Reck35e9dd62011-04-25 09:01:54 -07001020 mSettings.setAutofillEnabled(false);
Michael Kolb14612442011-06-24 13:06:29 -07001021 Toast.makeText(mContext,
Ben Murdoch1d676b62011-01-17 12:54:24 +00001022 R.string.autofill_setup_dialog_negative_toast,
1023 Toast.LENGTH_LONG).show();
1024 } else {
1025 // Take user to the AutoFill profile editor. When they return,
1026 // we will send the message that we pass here which will trigger
1027 // the form to get filled out with their new profile.
1028 mWebViewController.setupAutoFill(msg);
1029 }
1030 }
1031 })
1032 .setNegativeButton(R.string.cancel, null)
1033 .show();
Ben Murdoch8029a772010-11-16 11:58:21 +00001034 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001035 };
1036
1037 // -------------------------------------------------------------------------
1038 // WebViewClient implementation for the sub window
1039 // -------------------------------------------------------------------------
1040
1041 // Subclass of WebViewClient used in subwindows to notify the main
1042 // WebViewClient of certain WebView activities.
1043 private static class SubWindowClient extends WebViewClient {
1044 // The main WebViewClient.
1045 private final WebViewClient mClient;
Michael Kolb8233fac2010-10-26 16:08:53 -07001046 private final WebViewController mController;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001047
Michael Kolb8233fac2010-10-26 16:08:53 -07001048 SubWindowClient(WebViewClient client, WebViewController controller) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001049 mClient = client;
Michael Kolb8233fac2010-10-26 16:08:53 -07001050 mController = controller;
Leon Scroggins III211ba542010-04-19 13:21:13 -04001051 }
1052 @Override
1053 public void onPageStarted(WebView view, String url, Bitmap favicon) {
1054 // Unlike the others, do not call mClient's version, which would
1055 // change the progress bar. However, we do want to remove the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001056 // find or select dialog.
Michael Kolb8233fac2010-10-26 16:08:53 -07001057 mController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001058 }
1059 @Override
1060 public void doUpdateVisitedHistory(WebView view, String url,
1061 boolean isReload) {
1062 mClient.doUpdateVisitedHistory(view, url, isReload);
1063 }
1064 @Override
1065 public boolean shouldOverrideUrlLoading(WebView view, String url) {
1066 return mClient.shouldOverrideUrlLoading(view, url);
1067 }
1068 @Override
1069 public void onReceivedSslError(WebView view, SslErrorHandler handler,
1070 SslError error) {
1071 mClient.onReceivedSslError(view, handler, error);
1072 }
1073 @Override
Brian Carlstrom8862c1d2011-06-02 01:05:55 -07001074 public void onReceivedClientCertRequest(WebView view,
1075 ClientCertRequestHandler handler, String host_and_port) {
1076 mClient.onReceivedClientCertRequest(view, handler, host_and_port);
1077 }
1078 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -07001079 public void onReceivedHttpAuthRequest(WebView view,
1080 HttpAuthHandler handler, String host, String realm) {
1081 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
1082 }
1083 @Override
1084 public void onFormResubmission(WebView view, Message dontResend,
1085 Message resend) {
1086 mClient.onFormResubmission(view, dontResend, resend);
1087 }
1088 @Override
1089 public void onReceivedError(WebView view, int errorCode,
1090 String description, String failingUrl) {
1091 mClient.onReceivedError(view, errorCode, description, failingUrl);
1092 }
1093 @Override
1094 public boolean shouldOverrideKeyEvent(WebView view,
1095 android.view.KeyEvent event) {
1096 return mClient.shouldOverrideKeyEvent(view, event);
1097 }
1098 @Override
1099 public void onUnhandledKeyEvent(WebView view,
1100 android.view.KeyEvent event) {
1101 mClient.onUnhandledKeyEvent(view, event);
1102 }
1103 }
1104
1105 // -------------------------------------------------------------------------
1106 // WebChromeClient implementation for the sub window
1107 // -------------------------------------------------------------------------
1108
1109 private class SubWindowChromeClient extends WebChromeClient {
1110 // The main WebChromeClient.
1111 private final WebChromeClient mClient;
1112
1113 SubWindowChromeClient(WebChromeClient client) {
1114 mClient = client;
1115 }
1116 @Override
1117 public void onProgressChanged(WebView view, int newProgress) {
1118 mClient.onProgressChanged(view, newProgress);
1119 }
1120 @Override
1121 public boolean onCreateWindow(WebView view, boolean dialog,
1122 boolean userGesture, android.os.Message resultMsg) {
1123 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
1124 }
1125 @Override
1126 public void onCloseWindow(WebView window) {
1127 if (window != mSubView) {
1128 Log.e(LOGTAG, "Can't close the window");
1129 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001130 mWebViewController.dismissSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001131 }
1132 }
1133
1134 // -------------------------------------------------------------------------
1135
1136 // Construct a new tab
Michael Kolb7bcafde2011-05-09 13:55:59 -07001137 Tab(WebViewController wvcontroller, WebView w) {
John Reck1cf4b792011-07-26 10:22:22 -07001138 this(wvcontroller, w, null);
1139 }
1140
1141 Tab(WebViewController wvcontroller, Bundle state) {
1142 this(wvcontroller, null, state);
1143 }
1144
1145 Tab(WebViewController wvcontroller, WebView w, Bundle state) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001146 mWebViewController = wvcontroller;
Michael Kolb14612442011-06-24 13:06:29 -07001147 mContext = mWebViewController.getContext();
John Reck35e9dd62011-04-25 09:01:54 -07001148 mSettings = BrowserSettings.getInstance();
Michael Kolb14612442011-06-24 13:06:29 -07001149 mDataController = DataController.getInstance(mContext);
1150 mCurrentState = new PageState(mContext, w != null
John Reck847b5322011-04-14 17:02:18 -07001151 ? w.isPrivateBrowsingEnabled() : false);
Michael Kolb8233fac2010-10-26 16:08:53 -07001152 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001153 mInForeground = false;
1154
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001155 mDownloadListener = new DownloadListener() {
1156 public void onDownloadStart(String url, String userAgent,
1157 String contentDisposition, String mimetype,
1158 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001159 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001160 mimetype, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001161 }
1162 };
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001163 mWebBackForwardListClient = new WebBackForwardListClient() {
1164 @Override
1165 public void onNewHistoryItem(WebHistoryItem item) {
Mathew Inwooda829d552011-09-02 14:16:25 +01001166 if (mClearHistoryUrlPattern != null) {
1167 boolean match =
1168 mClearHistoryUrlPattern.matcher(item.getOriginalUrl()).matches();
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001169 if (LOGD_ENABLED) {
Mathew Inwooda829d552011-09-02 14:16:25 +01001170 Log.d(LOGTAG, "onNewHistoryItem: match=" + match + "\n\t"
1171 + item.getUrl() + "\n\t"
1172 + mClearHistoryUrlPattern);
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001173 }
Mathew Inwooda829d552011-09-02 14:16:25 +01001174 if (match) {
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001175 if (mMainView != null) {
1176 mMainView.clearHistory();
1177 }
1178 }
Mathew Inwooda829d552011-09-02 14:16:25 +01001179 mClearHistoryUrlPattern = null;
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001180 }
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001181 }
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001182 };
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001183
John Reck1cf4b792011-07-26 10:22:22 -07001184 mCaptureWidth = mContext.getResources().getDimensionPixelSize(
1185 R.dimen.tab_thumbnail_width);
1186 mCaptureHeight = mContext.getResources().getDimensionPixelSize(
1187 R.dimen.tab_thumbnail_height);
1188 updateShouldCaptureThumbnails();
1189 restoreState(state);
John Reck52be4782011-08-26 15:37:29 -07001190 if (getId() == -1) {
1191 mId = TabControl.getNextId();
1192 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001193 setWebView(w);
Michael Kolb9ef259a2011-07-12 15:33:08 -07001194 mHandler = new Handler() {
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001195 @Override
Michael Kolb9ef259a2011-07-12 15:33:08 -07001196 public void handleMessage(Message m) {
John Reck1cf4b792011-07-26 10:22:22 -07001197 switch (m.what) {
1198 case MSG_CAPTURE:
1199 capture();
1200 break;
1201 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001202 }
1203 };
John Reck1cf4b792011-07-26 10:22:22 -07001204 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001205
Mathew Inwoode09305e2011-09-02 12:03:26 +01001206 /**
1207 * This is used to get a new ID when the tab has been preloaded, before it is displayed and
1208 * added to TabControl. Preloaded tabs can be created before restoreInstanceState, leading
1209 * to overlapping IDs between the preloaded and restored tabs.
1210 */
1211 public void refreshIdAfterPreload() {
1212 mId = TabControl.getNextId();
1213 }
1214
John Reck1cf4b792011-07-26 10:22:22 -07001215 public void updateShouldCaptureThumbnails() {
1216 if (mWebViewController.shouldCaptureThumbnails()) {
1217 synchronized (Tab.this) {
1218 if (mCapture == null) {
1219 mCapture = Bitmap.createBitmap(mCaptureWidth, mCaptureHeight,
1220 Bitmap.Config.RGB_565);
Michael Kolbc3af0672011-08-09 10:24:41 -07001221 mCapture.eraseColor(Color.WHITE);
John Reck1cf4b792011-07-26 10:22:22 -07001222 if (mInForeground) {
1223 postCapture();
1224 }
1225 }
1226 }
1227 } else {
1228 synchronized (Tab.this) {
1229 mCapture = null;
1230 deleteThumbnail();
1231 }
1232 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001233 }
1234
Michael Kolb14612442011-06-24 13:06:29 -07001235 public void setController(WebViewController ctl) {
1236 mWebViewController = ctl;
John Reck1cf4b792011-07-26 10:22:22 -07001237 updateShouldCaptureThumbnails();
Michael Kolb14612442011-06-24 13:06:29 -07001238 }
1239
Michael Kolbc831b632011-05-11 09:30:34 -07001240 public long getId() {
1241 return mId;
1242 }
1243
Michael Kolb91911a22012-01-17 11:21:25 -08001244 void setWebView(WebView w) {
1245 setWebView(w, true);
1246 }
1247
Grace Kloba22ac16e2009-10-07 18:00:23 -07001248 /**
1249 * Sets the WebView for this tab, correctly removing the old WebView from
1250 * the container view.
1251 */
Michael Kolb91911a22012-01-17 11:21:25 -08001252 void setWebView(WebView w, boolean restore) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001253 if (mMainView == w) {
1254 return;
1255 }
Michael Kolba713ec82010-11-29 17:27:06 -08001256
Grace Kloba22ac16e2009-10-07 18:00:23 -07001257 // If the WebView is changing, the page will be reloaded, so any ongoing
1258 // Geolocation permission requests are void.
Grace Kloba50c241e2010-04-20 11:07:50 -07001259 if (mGeolocationPermissionsPrompt != null) {
1260 mGeolocationPermissionsPrompt.hide();
1261 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001262
Michael Kolba713ec82010-11-29 17:27:06 -08001263 mWebViewController.onSetWebView(this, w);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001264
John Reck1cf4b792011-07-26 10:22:22 -07001265 if (mMainView != null) {
John Reck8ee633f2011-08-09 16:00:35 -07001266 mMainView.setPictureListener(null);
John Reck1cf4b792011-07-26 10:22:22 -07001267 if (w != null) {
1268 syncCurrentState(w, null);
1269 } else {
1270 mCurrentState = new PageState(mContext, false);
1271 }
1272 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001273 // set the new one
1274 mMainView = w;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001275 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001276 if (mMainView != null) {
1277 mMainView.setWebViewClient(mWebViewClient);
1278 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001279 // Attach DownloadManager so that downloads can start in an active
1280 // or a non-active window. This can happen when going to a site that
1281 // does a redirect after a period of time. The user could have
1282 // switched to another tab while waiting for the download to start.
1283 mMainView.setDownloadListener(mDownloadListener);
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00001284 getWebViewClassic().setWebBackForwardListClient(mWebBackForwardListClient);
John Reck8ee633f2011-08-09 16:00:35 -07001285 TabControl tc = mWebViewController.getTabControl();
1286 if (tc != null && tc.getOnThumbnailUpdatedListener() != null) {
1287 mMainView.setPictureListener(this);
1288 }
Michael Kolb91911a22012-01-17 11:21:25 -08001289 if (restore && (mSavedState != null)) {
John Reck8b9bb8b2012-03-08 13:19:40 -08001290 restoreUserAgent();
John Reck6c2e2f32011-08-22 13:41:23 -07001291 WebBackForwardList restoredState
1292 = mMainView.restoreState(mSavedState);
1293 if (restoredState == null || restoredState.getSize() == 0) {
1294 Log.w(LOGTAG, "Failed to restore WebView state!");
1295 loadUrl(mCurrentState.mOriginalUrl, null);
1296 }
John Reck1cf4b792011-07-26 10:22:22 -07001297 mSavedState = null;
1298 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001299 }
1300 }
1301
1302 /**
1303 * Destroy the tab's main WebView and subWindow if any
1304 */
1305 void destroy() {
1306 if (mMainView != null) {
1307 dismissSubWindow();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001308 // save the WebView to call destroy() after detach it from the tab
1309 WebView webView = mMainView;
1310 setWebView(null);
1311 webView.destroy();
1312 }
1313 }
1314
1315 /**
1316 * Remove the tab from the parent
1317 */
1318 void removeFromTree() {
1319 // detach the children
Michael Kolbc831b632011-05-11 09:30:34 -07001320 if (mChildren != null) {
1321 for(Tab t : mChildren) {
1322 t.setParent(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001323 }
1324 }
1325 // remove itself from the parent list
Michael Kolbc831b632011-05-11 09:30:34 -07001326 if (mParent != null) {
1327 mParent.mChildren.remove(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001328 }
John Reck1cf4b792011-07-26 10:22:22 -07001329 deleteThumbnail();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001330 }
1331
1332 /**
1333 * Create a new subwindow unless a subwindow already exists.
1334 * @return True if a new subwindow was created. False if one already exists.
1335 */
1336 boolean createSubWindow() {
1337 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001338 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001339 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001340 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001341 mSubView.setWebChromeClient(new SubWindowChromeClient(
1342 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001343 // Set a different DownloadListener for the mSubView, since it will
1344 // just need to dismiss the mSubView, rather than close the Tab
1345 mSubView.setDownloadListener(new DownloadListener() {
1346 public void onDownloadStart(String url, String userAgent,
1347 String contentDisposition, String mimetype,
1348 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001349 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001350 contentDisposition, mimetype, contentLength);
1351 if (mSubView.copyBackForwardList().getSize() == 0) {
1352 // This subwindow was opened for the sole purpose of
1353 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001354 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001355 }
1356 }
1357 });
Michael Kolb14612442011-06-24 13:06:29 -07001358 mSubView.setOnCreateContextMenuListener(mWebViewController.getActivity());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001359 return true;
1360 }
1361 return false;
1362 }
1363
1364 /**
1365 * Dismiss the subWindow for the tab.
1366 */
1367 void dismissSubWindow() {
1368 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001369 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001370 mSubView.destroy();
1371 mSubView = null;
1372 mSubViewContainer = null;
1373 }
1374 }
1375
Grace Kloba22ac16e2009-10-07 18:00:23 -07001376
1377 /**
1378 * Set the parent tab of this tab.
1379 */
Michael Kolbc831b632011-05-11 09:30:34 -07001380 void setParent(Tab parent) {
John Reck52be4782011-08-26 15:37:29 -07001381 if (parent == this) {
1382 throw new IllegalStateException("Cannot set parent to self!");
1383 }
Michael Kolbc831b632011-05-11 09:30:34 -07001384 mParent = parent;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001385 // This tab may have been freed due to low memory. If that is the case,
Michael Kolbc831b632011-05-11 09:30:34 -07001386 // the parent tab id is already saved. If we are changing that id
Grace Kloba22ac16e2009-10-07 18:00:23 -07001387 // (most likely due to removing the parent tab) we must update the
Michael Kolbc831b632011-05-11 09:30:34 -07001388 // parent tab id in the saved Bundle.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001389 if (mSavedState != null) {
1390 if (parent == null) {
1391 mSavedState.remove(PARENTTAB);
1392 } else {
Michael Kolbc831b632011-05-11 09:30:34 -07001393 mSavedState.putLong(PARENTTAB, parent.getId());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001394 }
1395 }
John Reckb0a86db2011-05-24 14:05:58 -07001396
1397 // Sync the WebView useragent with the parent
1398 if (parent != null && mSettings.hasDesktopUseragent(parent.getWebView())
1399 != mSettings.hasDesktopUseragent(getWebView())) {
1400 mSettings.toggleDesktopUseragent(getWebView());
1401 }
John Reck52be4782011-08-26 15:37:29 -07001402
1403 if (parent != null && parent.getId() == getId()) {
1404 throw new IllegalStateException("Parent has same ID as child!");
1405 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001406 }
1407
1408 /**
Michael Kolbc831b632011-05-11 09:30:34 -07001409 * If this Tab was created through another Tab, then this method returns
1410 * that Tab.
1411 * @return the Tab parent or null
1412 */
1413 public Tab getParent() {
1414 return mParent;
1415 }
1416
1417 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001418 * When a Tab is created through the content of another Tab, then we
1419 * associate the Tabs.
1420 * @param child the Tab that was created from this Tab
1421 */
1422 void addChildTab(Tab child) {
Michael Kolbc831b632011-05-11 09:30:34 -07001423 if (mChildren == null) {
1424 mChildren = new Vector<Tab>();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001425 }
Michael Kolbc831b632011-05-11 09:30:34 -07001426 mChildren.add(child);
1427 child.setParent(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001428 }
1429
Michael Kolbc831b632011-05-11 09:30:34 -07001430 Vector<Tab> getChildren() {
1431 return mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001432 }
1433
1434 void resume() {
1435 if (mMainView != null) {
John Reck56c1fcf2011-08-17 10:15:16 -07001436 setupHwAcceleration(mMainView);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001437 mMainView.onResume();
1438 if (mSubView != null) {
1439 mSubView.onResume();
1440 }
1441 }
1442 }
1443
John Reck56c1fcf2011-08-17 10:15:16 -07001444 private void setupHwAcceleration(View web) {
1445 if (web == null) return;
1446 BrowserSettings settings = BrowserSettings.getInstance();
1447 if (settings.isHardwareAccelerated()) {
1448 web.setLayerType(View.LAYER_TYPE_NONE, null);
1449 } else {
1450 web.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
1451 }
1452 }
1453
Grace Kloba22ac16e2009-10-07 18:00:23 -07001454 void pause() {
1455 if (mMainView != null) {
1456 mMainView.onPause();
1457 if (mSubView != null) {
1458 mSubView.onPause();
1459 }
1460 }
1461 }
1462
1463 void putInForeground() {
John Reck8ee633f2011-08-09 16:00:35 -07001464 if (mInForeground) {
1465 return;
1466 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001467 mInForeground = true;
1468 resume();
Michael Kolb14612442011-06-24 13:06:29 -07001469 Activity activity = mWebViewController.getActivity();
1470 mMainView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001471 if (mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -07001472 mSubView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001473 }
1474 // Show the pending error dialog if the queue is not empty
1475 if (mQueuedErrors != null && mQueuedErrors.size() > 0) {
1476 showError(mQueuedErrors.getFirst());
1477 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001478 mWebViewController.bookmarkedStatusHasChanged(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001479 }
1480
1481 void putInBackground() {
John Reck8ee633f2011-08-09 16:00:35 -07001482 if (!mInForeground) {
1483 return;
1484 }
John Reck52be4782011-08-26 15:37:29 -07001485 capture();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001486 mInForeground = false;
1487 pause();
1488 mMainView.setOnCreateContextMenuListener(null);
1489 if (mSubView != null) {
1490 mSubView.setOnCreateContextMenuListener(null);
1491 }
1492 }
1493
Michael Kolb8233fac2010-10-26 16:08:53 -07001494 boolean inForeground() {
1495 return mInForeground;
1496 }
1497
Grace Kloba22ac16e2009-10-07 18:00:23 -07001498 /**
1499 * Return the top window of this tab; either the subwindow if it is not
1500 * null or the main window.
1501 * @return The top window of this tab.
1502 */
1503 WebView getTopWindow() {
1504 if (mSubView != null) {
1505 return mSubView;
1506 }
1507 return mMainView;
1508 }
1509
1510 /**
1511 * Return the main window of this tab. Note: if a tab is freed in the
1512 * background, this can return null. It is only guaranteed to be
1513 * non-null for the current tab.
1514 * @return The main WebView of this tab.
1515 */
1516 WebView getWebView() {
1517 return mMainView;
1518 }
1519
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00001520 /**
1521 * Return the underlying WebViewClassic implementation. As with getWebView,
1522 * this maybe null for background tabs.
1523 * @return The main WebView of this tab.
1524 */
1525 WebViewClassic getWebViewClassic() {
1526 return WebViewClassic.fromWebView(mMainView);
1527 }
1528
Michael Kolba713ec82010-11-29 17:27:06 -08001529 void setViewContainer(View container) {
1530 mContainer = container;
1531 }
1532
Michael Kolb8233fac2010-10-26 16:08:53 -07001533 View getViewContainer() {
1534 return mContainer;
1535 }
1536
Grace Kloba22ac16e2009-10-07 18:00:23 -07001537 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001538 * Return whether private browsing is enabled for the main window of
1539 * this tab.
1540 * @return True if private browsing is enabled.
1541 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001542 boolean isPrivateBrowsingEnabled() {
John Reck502a3532011-08-16 14:21:46 -07001543 return mCurrentState.mIncognito;
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001544 }
1545
1546 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001547 * Return the subwindow of this tab or null if there is no subwindow.
1548 * @return The subwindow of this tab or null.
1549 */
1550 WebView getSubWebView() {
1551 return mSubView;
1552 }
1553
Michael Kolb1514bb72010-11-22 09:11:48 -08001554 void setSubWebView(WebView subView) {
1555 mSubView = subView;
1556 }
1557
Michael Kolb8233fac2010-10-26 16:08:53 -07001558 View getSubViewContainer() {
1559 return mSubViewContainer;
1560 }
1561
Michael Kolb1514bb72010-11-22 09:11:48 -08001562 void setSubViewContainer(View subViewContainer) {
1563 mSubViewContainer = subViewContainer;
1564 }
1565
Grace Kloba22ac16e2009-10-07 18:00:23 -07001566 /**
1567 * @return The geolocation permissions prompt for this tab.
1568 */
1569 GeolocationPermissionsPrompt getGeolocationPermissionsPrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001570 if (mGeolocationPermissionsPrompt == null) {
1571 ViewStub stub = (ViewStub) mContainer
1572 .findViewById(R.id.geolocation_permissions_prompt);
1573 mGeolocationPermissionsPrompt = (GeolocationPermissionsPrompt) stub
1574 .inflate();
Grace Kloba50c241e2010-04-20 11:07:50 -07001575 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001576 return mGeolocationPermissionsPrompt;
1577 }
1578
1579 /**
1580 * @return The application id string
1581 */
1582 String getAppId() {
1583 return mAppId;
1584 }
1585
1586 /**
1587 * Set the application id string
1588 * @param id
1589 */
1590 void setAppId(String id) {
1591 mAppId = id;
1592 }
1593
Michael Kolbe28b3472011-08-04 16:54:31 -07001594 boolean closeOnBack() {
1595 return mCloseOnBack;
1596 }
1597
1598 void setCloseOnBack(boolean close) {
1599 mCloseOnBack = close;
1600 }
1601
Grace Kloba22ac16e2009-10-07 18:00:23 -07001602 String getUrl() {
John Reck324d4402011-01-11 16:56:42 -08001603 return UrlUtils.filteredUrl(mCurrentState.mUrl);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001604 }
1605
John Reck49a603c2011-03-03 09:33:05 -08001606 String getOriginalUrl() {
John Reckdb22ec42011-06-29 11:31:24 -07001607 if (mCurrentState.mOriginalUrl == null) {
1608 return getUrl();
John Reck49a603c2011-03-03 09:33:05 -08001609 }
John Reckdb22ec42011-06-29 11:31:24 -07001610 return UrlUtils.filteredUrl(mCurrentState.mOriginalUrl);
John Reck49a603c2011-03-03 09:33:05 -08001611 }
1612
Grace Kloba22ac16e2009-10-07 18:00:23 -07001613 /**
John Reck30c714c2010-12-16 17:30:34 -08001614 * Get the title of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001615 */
1616 String getTitle() {
John Reck30c714c2010-12-16 17:30:34 -08001617 if (mCurrentState.mTitle == null && mInPageLoad) {
Michael Kolb14612442011-06-24 13:06:29 -07001618 return mContext.getString(R.string.title_bar_loading);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001619 }
John Reck30c714c2010-12-16 17:30:34 -08001620 return mCurrentState.mTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001621 }
1622
1623 /**
John Reck30c714c2010-12-16 17:30:34 -08001624 * Get the favicon of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001625 */
1626 Bitmap getFavicon() {
John Reck1cf4b792011-07-26 10:22:22 -07001627 if (mCurrentState.mFavicon != null) {
1628 return mCurrentState.mFavicon;
1629 }
1630 return getDefaultFavicon(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001631 }
1632
John Recke969cc52010-12-21 17:24:43 -08001633 public boolean isBookmarkedSite() {
1634 return mCurrentState.mIsBookmarkedSite;
1635 }
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001636
Grace Kloba22ac16e2009-10-07 18:00:23 -07001637 /**
1638 * Return the tab's error console. Creates the console if createIfNEcessary
1639 * is true and we haven't already created the console.
1640 * @param createIfNecessary Flag to indicate if the console should be
1641 * created if it has not been already.
1642 * @return The tab's error console, or null if one has not been created and
1643 * createIfNecessary is false.
1644 */
1645 ErrorConsoleView getErrorConsole(boolean createIfNecessary) {
1646 if (createIfNecessary && mErrorConsole == null) {
Michael Kolb14612442011-06-24 13:06:29 -07001647 mErrorConsole = new ErrorConsoleView(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001648 mErrorConsole.setWebView(mMainView);
1649 }
1650 return mErrorConsole;
1651 }
1652
Steve Block08a6f0c2011-10-06 12:12:53 +01001653 /**
1654 * Sets the security state, clears the SSL certificate error and informs
1655 * the controller.
1656 */
Steve Block2466eff2011-10-03 15:33:09 +01001657 private void setSecurityState(SecurityState securityState) {
1658 mCurrentState.mSecurityState = securityState;
Steve Block08a6f0c2011-10-06 12:12:53 +01001659 mCurrentState.mSslCertificateError = null;
Steve Block2466eff2011-10-03 15:33:09 +01001660 mWebViewController.onUpdatedSecurityState(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001661 }
1662
1663 /**
Steve Block2466eff2011-10-03 15:33:09 +01001664 * @return The tab's security state.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001665 */
Steve Block2466eff2011-10-03 15:33:09 +01001666 SecurityState getSecurityState() {
1667 return mCurrentState.mSecurityState;
John Reck30c714c2010-12-16 17:30:34 -08001668 }
1669
Steve Block08a6f0c2011-10-06 12:12:53 +01001670 /**
1671 * Gets the SSL certificate error, if any, for the page's main resource.
1672 * This is only non-null when the security state is
1673 * SECURITY_STATE_BAD_CERTIFICATE.
1674 */
1675 SslError getSslCertificateError() {
1676 return mCurrentState.mSslCertificateError;
1677 }
1678
John Reck30c714c2010-12-16 17:30:34 -08001679 int getLoadProgress() {
1680 if (mInPageLoad) {
1681 return mPageLoadProgress;
1682 }
1683 return 100;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001684 }
1685
1686 /**
1687 * @return TRUE if onPageStarted is called while onPageFinished is not
1688 * called yet.
1689 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001690 boolean inPageLoad() {
1691 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001692 }
1693
Grace Kloba22ac16e2009-10-07 18:00:23 -07001694 /**
John Reck1cf4b792011-07-26 10:22:22 -07001695 * @return The Bundle with the tab's state if it can be saved, otherwise null
Grace Kloba22ac16e2009-10-07 18:00:23 -07001696 */
John Reck1cf4b792011-07-26 10:22:22 -07001697 public Bundle saveState() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001698 // If the WebView is null it means we ran low on memory and we already
1699 // stored the saved state in mSavedState.
1700 if (mMainView == null) {
John Reck1cf4b792011-07-26 10:22:22 -07001701 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001702 }
John Reck6c2e2f32011-08-22 13:41:23 -07001703
1704 if (TextUtils.isEmpty(mCurrentState.mUrl)) {
John Reck1cf4b792011-07-26 10:22:22 -07001705 return null;
John Reck24f18262011-06-17 14:47:20 -07001706 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001707
1708 mSavedState = new Bundle();
John Reck6c2e2f32011-08-22 13:41:23 -07001709 WebBackForwardList savedList = mMainView.saveState(mSavedState);
1710 if (savedList == null || savedList.getSize() == 0) {
1711 Log.w(LOGTAG, "Failed to save back/forward list for "
1712 + mCurrentState.mUrl);
1713 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001714
Michael Kolbc831b632011-05-11 09:30:34 -07001715 mSavedState.putLong(ID, mId);
John Reck30c714c2010-12-16 17:30:34 -08001716 mSavedState.putString(CURRURL, mCurrentState.mUrl);
1717 mSavedState.putString(CURRTITLE, mCurrentState.mTitle);
John Reck1cf4b792011-07-26 10:22:22 -07001718 mSavedState.putBoolean(INCOGNITO, mMainView.isPrivateBrowsingEnabled());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001719 if (mAppId != null) {
1720 mSavedState.putString(APPID, mAppId);
1721 }
Michael Kolbe28b3472011-08-04 16:54:31 -07001722 mSavedState.putBoolean(CLOSEFLAG, mCloseOnBack);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001723 // Remember the parent tab so the relationship can be restored.
Michael Kolbc831b632011-05-11 09:30:34 -07001724 if (mParent != null) {
1725 mSavedState.putLong(PARENTTAB, mParent.mId);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001726 }
John Reckb0a86db2011-05-24 14:05:58 -07001727 mSavedState.putBoolean(USERAGENT,
1728 mSettings.hasDesktopUseragent(getWebView()));
John Reck1cf4b792011-07-26 10:22:22 -07001729 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001730 }
1731
1732 /*
1733 * Restore the state of the tab.
1734 */
John Reck1cf4b792011-07-26 10:22:22 -07001735 private void restoreState(Bundle b) {
1736 mSavedState = b;
1737 if (mSavedState == null) {
1738 return;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001739 }
1740 // Restore the internal state even if the WebView fails to restore.
1741 // This will maintain the app id, original url and close-on-exit values.
Michael Kolbc831b632011-05-11 09:30:34 -07001742 mId = b.getLong(ID);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001743 mAppId = b.getString(APPID);
Michael Kolbe28b3472011-08-04 16:54:31 -07001744 mCloseOnBack = b.getBoolean(CLOSEFLAG);
John Reck8b9bb8b2012-03-08 13:19:40 -08001745 restoreUserAgent();
John Reck1cf4b792011-07-26 10:22:22 -07001746 String url = b.getString(CURRURL);
1747 String title = b.getString(CURRTITLE);
1748 boolean incognito = b.getBoolean(INCOGNITO);
1749 mCurrentState = new PageState(mContext, incognito, url, null);
1750 mCurrentState.mTitle = title;
1751 synchronized (Tab.this) {
1752 if (mCapture != null) {
John Reck4eadc342011-10-31 14:04:10 -07001753 DataController.getInstance(mContext).loadThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07001754 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001755 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001756 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04001757
John Reck8b9bb8b2012-03-08 13:19:40 -08001758 private void restoreUserAgent() {
1759 if (mMainView == null || mSavedState == null) {
1760 return;
1761 }
1762 if (mSavedState.getBoolean(USERAGENT)
1763 != mSettings.hasDesktopUseragent(mMainView)) {
1764 mSettings.toggleDesktopUseragent(mMainView);
1765 }
1766 }
1767
Leon Scroggins1961ed22010-12-07 15:22:21 -05001768 public void updateBookmarkedStatus() {
John Recke969cc52010-12-21 17:24:43 -08001769 mDataController.queryBookmarkStatus(getUrl(), mIsBookmarkCallback);
Leon Scroggins1961ed22010-12-07 15:22:21 -05001770 }
1771
John Recke969cc52010-12-21 17:24:43 -08001772 private DataController.OnQueryUrlIsBookmark mIsBookmarkCallback
1773 = new DataController.OnQueryUrlIsBookmark() {
1774 @Override
1775 public void onQueryUrlIsBookmark(String url, boolean isBookmark) {
1776 if (mCurrentState.mUrl.equals(url)) {
1777 mCurrentState.mIsBookmarkedSite = isBookmark;
1778 mWebViewController.bookmarkedStatusHasChanged(Tab.this);
1779 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001780 }
John Recke969cc52010-12-21 17:24:43 -08001781 };
Michael Kolb1acef692011-03-08 14:12:06 -08001782
Michael Kolbeb95db42011-03-03 10:38:40 -08001783 public Bitmap getScreenshot() {
John Reck1cf4b792011-07-26 10:22:22 -07001784 synchronized (Tab.this) {
1785 return mCapture;
1786 }
Michael Kolbeb95db42011-03-03 10:38:40 -08001787 }
1788
John Reck541f55a2011-06-07 16:34:43 -07001789 public boolean isSnapshot() {
John Reck541f55a2011-06-07 16:34:43 -07001790 return false;
1791 }
1792
John Reck68234a92012-04-19 15:27:12 -07001793 private static class SaveCallback implements ValueCallback<Boolean> {
1794 boolean mResult;
1795
1796 @Override
1797 public void onReceiveValue(Boolean value) {
1798 mResult = value;
1799 synchronized (this) {
1800 notifyAll();
John Reck8cc92352011-07-06 17:41:52 -07001801 }
John Reck541f55a2011-06-07 16:34:43 -07001802 }
John Reck68234a92012-04-19 15:27:12 -07001803
1804 }
1805
1806 /**
1807 * Must be called on the UI thread
1808 */
1809 public ContentValues createSnapshotValues() {
1810 WebViewClassic web = getWebViewClassic();
1811 if (web == null) return null;
John Reckd8c74522011-06-14 08:45:00 -07001812 ContentValues values = new ContentValues();
1813 values.put(Snapshots.TITLE, mCurrentState.mTitle);
1814 values.put(Snapshots.URL, mCurrentState.mUrl);
John Reck68234a92012-04-19 15:27:12 -07001815 values.put(Snapshots.BACKGROUND, web.getPageBackgroundColor());
John Reck8cc92352011-07-06 17:41:52 -07001816 values.put(Snapshots.DATE_CREATED, System.currentTimeMillis());
1817 values.put(Snapshots.FAVICON, compressBitmap(getFavicon()));
1818 Bitmap screenshot = Controller.createScreenshot(mMainView,
1819 Controller.getDesiredThumbnailWidth(mContext),
1820 Controller.getDesiredThumbnailHeight(mContext));
1821 values.put(Snapshots.THUMBNAIL, compressBitmap(screenshot));
John Reckd8c74522011-06-14 08:45:00 -07001822 return values;
John Reck541f55a2011-06-07 16:34:43 -07001823 }
1824
John Reck68234a92012-04-19 15:27:12 -07001825 /**
1826 * Probably want to call this on a background thread
1827 */
1828 public boolean saveViewState(ContentValues values) {
1829 WebViewClassic web = getWebViewClassic();
1830 if (web == null) return false;
1831 String path = UUID.randomUUID().toString();
1832 SaveCallback callback = new SaveCallback();
1833 OutputStream outs = null;
1834 try {
1835 outs = mContext.openFileOutput(path, Context.MODE_PRIVATE);
1836 GZIPOutputStream stream = new GZIPOutputStream(outs);
1837 synchronized (callback) {
1838 web.saveViewState(stream, callback);
1839 callback.wait();
1840 }
1841 stream.flush();
1842 stream.close();
1843 } catch (Exception e) {
1844 Log.w(LOGTAG, "Failed to save view state", e);
1845 if (outs != null) {
1846 try {
1847 outs.close();
1848 } catch (IOException ignore) {}
1849 }
1850 File file = mContext.getFileStreamPath(path);
1851 if (file.exists() && !file.delete()) {
1852 file.deleteOnExit();
1853 }
1854 return false;
1855 }
1856 File savedFile = mContext.getFileStreamPath(path);
1857 if (!callback.mResult) {
1858 if (!savedFile.delete()) {
1859 savedFile.deleteOnExit();
1860 }
1861 return false;
1862 }
1863 long size = savedFile.length();
1864 values.put(Snapshots.VIEWSTATE_PATH, path);
1865 values.put(Snapshots.VIEWSTATE_SIZE, size);
1866 return true;
1867 }
1868
John Reck8cc92352011-07-06 17:41:52 -07001869 public byte[] compressBitmap(Bitmap bitmap) {
1870 if (bitmap == null) {
1871 return null;
1872 }
1873 ByteArrayOutputStream stream = new ByteArrayOutputStream();
1874 bitmap.compress(CompressFormat.PNG, 100, stream);
1875 return stream.toByteArray();
1876 }
1877
John Reck26b18322011-06-21 13:08:58 -07001878 public void loadUrl(String url, Map<String, String> headers) {
1879 if (mMainView != null) {
Michael Kolba53c9892011-10-05 13:31:40 -07001880 mPageLoadProgress = INITIAL_PROGRESS;
1881 mInPageLoad = true;
Michael Kolb14612442011-06-24 13:06:29 -07001882 mCurrentState = new PageState(mContext, false, url, null);
John Reck26b18322011-06-21 13:08:58 -07001883 mWebViewController.onPageStarted(this, mMainView, null);
1884 mMainView.loadUrl(url, headers);
1885 }
1886 }
1887
Michael Kolb9ef259a2011-07-12 15:33:08 -07001888 protected void capture() {
1889 if (mMainView == null || mCapture == null) return;
John Reck4eadc342011-10-31 14:04:10 -07001890 if (mMainView.getContentWidth() <= 0 || mMainView.getContentHeight() <= 0) {
1891 return;
1892 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001893 Canvas c = new Canvas(mCapture);
1894 final int left = mMainView.getScrollX();
1895 final int top = mMainView.getScrollY() + mMainView.getVisibleTitleHeight();
Michael Kolba3194d02011-09-07 11:23:51 -07001896 int state = c.save();
Michael Kolb9ef259a2011-07-12 15:33:08 -07001897 c.translate(-left, -top);
1898 float scale = mCaptureWidth / (float) mMainView.getWidth();
1899 c.scale(scale, scale, left, top);
John Reck8ee633f2011-08-09 16:00:35 -07001900 if (mMainView instanceof BrowserWebView) {
1901 ((BrowserWebView)mMainView).drawContent(c);
1902 } else {
1903 mMainView.draw(c);
1904 }
Michael Kolba3194d02011-09-07 11:23:51 -07001905 c.restoreToCount(state);
1906 // manually anti-alias the edges for the tilt
1907 c.drawRect(0, 0, 1, mCapture.getHeight(), sAlphaPaint);
1908 c.drawRect(mCapture.getWidth() - 1, 0, mCapture.getWidth(),
1909 mCapture.getHeight(), sAlphaPaint);
1910 c.drawRect(0, 0, mCapture.getWidth(), 1, sAlphaPaint);
1911 c.drawRect(0, mCapture.getHeight() - 1, mCapture.getWidth(),
1912 mCapture.getHeight(), sAlphaPaint);
Dianne Hackborn43cfe8a2011-08-02 16:59:35 -07001913 c.setBitmap(null);
John Reck8ee633f2011-08-09 16:00:35 -07001914 mHandler.removeMessages(MSG_CAPTURE);
John Reck1cf4b792011-07-26 10:22:22 -07001915 persistThumbnail();
John Reck8ee633f2011-08-09 16:00:35 -07001916 TabControl tc = mWebViewController.getTabControl();
1917 if (tc != null) {
1918 OnThumbnailUpdatedListener updateListener
1919 = tc.getOnThumbnailUpdatedListener();
1920 if (updateListener != null) {
1921 updateListener.onThumbnailUpdated(this);
1922 }
1923 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001924 }
1925
1926 @Override
1927 public void onNewPicture(WebView view, Picture picture) {
1928 //update screenshot
John Reck1cf4b792011-07-26 10:22:22 -07001929 postCapture();
1930 }
1931
1932 private void postCapture() {
Michael Kolb9ef259a2011-07-12 15:33:08 -07001933 if (!mHandler.hasMessages(MSG_CAPTURE)) {
1934 mHandler.sendEmptyMessageDelayed(MSG_CAPTURE, CAPTURE_DELAY);
1935 }
1936 }
1937
John Reckef654f12011-07-12 16:42:08 -07001938 public boolean canGoBack() {
1939 return mMainView != null ? mMainView.canGoBack() : false;
1940 }
1941
1942 public boolean canGoForward() {
1943 return mMainView != null ? mMainView.canGoForward() : false;
1944 }
1945
1946 public void goBack() {
1947 if (mMainView != null) {
1948 mMainView.goBack();
1949 }
1950 }
1951
1952 public void goForward() {
1953 if (mMainView != null) {
1954 mMainView.goForward();
1955 }
1956 }
1957
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001958 /**
1959 * Causes the tab back/forward stack to be cleared once, if the given URL is the next URL
1960 * to be added to the stack.
1961 *
1962 * This is used to ensure that preloaded URLs that are not subsequently seen by the user do
1963 * not appear in the back stack.
1964 */
Mathew Inwooda829d552011-09-02 14:16:25 +01001965 public void clearBackStackWhenItemAdded(Pattern urlPattern) {
1966 mClearHistoryUrlPattern = urlPattern;
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001967 }
1968
John Reck1cf4b792011-07-26 10:22:22 -07001969 protected void persistThumbnail() {
John Reck4eadc342011-10-31 14:04:10 -07001970 DataController.getInstance(mContext).saveThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07001971 }
1972
1973 protected void deleteThumbnail() {
John Reck4eadc342011-10-31 14:04:10 -07001974 DataController.getInstance(mContext).deleteThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07001975 }
1976
John Reck4eadc342011-10-31 14:04:10 -07001977 void updateCaptureFromBlob(byte[] blob) {
John Reck1cf4b792011-07-26 10:22:22 -07001978 synchronized (Tab.this) {
1979 if (mCapture == null) {
1980 return;
1981 }
Michael Kolbbd4c00a2011-08-02 11:27:12 -07001982 ByteBuffer buffer = ByteBuffer.wrap(blob);
Michael Kolbd837a112011-08-09 14:00:18 -07001983 try {
1984 mCapture.copyPixelsFromBuffer(buffer);
1985 } catch (RuntimeException rex) {
1986 Log.e(LOGTAG, "Load capture has mismatched sizes; buffer: "
1987 + buffer.capacity() + " blob: " + blob.length
1988 + "capture: " + mCapture.getByteCount());
1989 throw rex;
Michael Kolbbd4c00a2011-08-02 11:27:12 -07001990 }
John Reck1cf4b792011-07-26 10:22:22 -07001991 }
1992 }
1993
John Reck52be4782011-08-26 15:37:29 -07001994 @Override
1995 public String toString() {
1996 StringBuilder builder = new StringBuilder(100);
1997 builder.append(mId);
1998 builder.append(") has parent: ");
1999 if (getParent() != null) {
2000 builder.append("true[");
2001 builder.append(getParent().getId());
2002 builder.append("]");
2003 } else {
2004 builder.append("false");
2005 }
2006 builder.append(", incog: ");
2007 builder.append(isPrivateBrowsingEnabled());
2008 if (!isPrivateBrowsingEnabled()) {
2009 builder.append(", title: ");
2010 builder.append(getTitle());
2011 builder.append(", url: ");
2012 builder.append(getUrl());
2013 }
2014 return builder.toString();
2015 }
2016
Steve Block4895b012011-10-03 16:26:46 +01002017 private void handleProceededAfterSslError(SslError error) {
2018 if (error.getUrl().equals(mCurrentState.mUrl)) {
2019 // The security state should currently be SECURITY_STATE_SECURE.
2020 setSecurityState(SecurityState.SECURITY_STATE_BAD_CERTIFICATE);
Steve Block08a6f0c2011-10-06 12:12:53 +01002021 mCurrentState.mSslCertificateError = error;
Steve Block4895b012011-10-03 16:26:46 +01002022 } else if (getSecurityState() == SecurityState.SECURITY_STATE_SECURE) {
Steve Block08a6f0c2011-10-06 12:12:53 +01002023 // The page's main resource is secure and this error is for a
2024 // sub-resource.
Steve Block4895b012011-10-03 16:26:46 +01002025 setSecurityState(SecurityState.SECURITY_STATE_MIXED);
2026 }
2027 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002028}