blob: b5000c2ca62582a0be1ca8bc3881af8b9a2def62 [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;
Selim Gurun0b3d66f2012-08-29 13:08:13 -070049import android.webkit.BrowserDownloadListener;
Brian Carlstrom8862c1d2011-06-02 01:05:55 -070050import android.webkit.ClientCertRequestHandler;
Ben Murdochc42addf2010-01-28 15:19:59 +000051import android.webkit.ConsoleMessage;
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;
Jonathan Dixonf4ff1492012-06-08 10:15:32 -070067import android.webkit.WebViewClientClassicExt;
Ben Murdoch1d676b62011-01-17 12:54:24 +000068import android.widget.CheckBox;
Ben Murdoch8029a772010-11-16 11:58:21 +000069import android.widget.Toast;
Grace Kloba22ac16e2009-10-07 18:00:23 -070070
John Reck8ee633f2011-08-09 16:00:35 -070071import com.android.browser.TabControl.OnThumbnailUpdatedListener;
John Reck541f55a2011-06-07 16:34:43 -070072import com.android.browser.homepages.HomeProvider;
John Reck8cc92352011-07-06 17:41:52 -070073import com.android.browser.provider.SnapshotProvider.Snapshots;
John Reck541f55a2011-06-07 16:34:43 -070074
75import java.io.ByteArrayOutputStream;
John Reck2b71d6d2012-04-18 17:42:06 -070076import java.io.File;
John Reck68234a92012-04-19 15:27:12 -070077import java.io.IOException;
John Reck2b71d6d2012-04-18 17:42:06 -070078import java.io.OutputStream;
John Reck1cf4b792011-07-26 10:22:22 -070079import java.nio.ByteBuffer;
Michael Kolbfe251992010-07-08 15:41:55 -070080import java.util.LinkedList;
81import java.util.Map;
John Reck2b71d6d2012-04-18 17:42:06 -070082import java.util.UUID;
Michael Kolbfe251992010-07-08 15:41:55 -070083import java.util.Vector;
Mathew Inwooda829d552011-09-02 14:16:25 +010084import java.util.regex.Pattern;
John Reck8cc92352011-07-06 17:41:52 -070085import java.util.zip.GZIPOutputStream;
Michael Kolbfe251992010-07-08 15:41:55 -070086
Grace Kloba22ac16e2009-10-07 18:00:23 -070087/**
88 * Class for maintaining Tabs with a main WebView and a subwindow.
89 */
Michael Kolb9ef259a2011-07-12 15:33:08 -070090class Tab implements PictureListener {
Michael Kolb8233fac2010-10-26 16:08:53 -070091
Grace Kloba22ac16e2009-10-07 18:00:23 -070092 // Log Tag
93 private static final String LOGTAG = "Tab";
Mathew Inwood1dd8e822011-08-03 14:34:29 +010094 private static final boolean LOGD_ENABLED = com.android.browser.Browser.LOGD_ENABLED;
Ben Murdochc42addf2010-01-28 15:19:59 +000095 // Special case the logtag for messages for the Console to make it easier to
96 // filter them and match the logtag used for these messages in older versions
97 // of the browser.
98 private static final String CONSOLE_LOGTAG = "browser";
99
Michael Kolb9ef259a2011-07-12 15:33:08 -0700100 private static final int MSG_CAPTURE = 42;
John Reck8ee633f2011-08-09 16:00:35 -0700101 private static final int CAPTURE_DELAY = 100;
Michael Kolba53c9892011-10-05 13:31:40 -0700102 private static final int INITIAL_PROGRESS = 5;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700103
John Reck1cf4b792011-07-26 10:22:22 -0700104 private static Bitmap sDefaultFavicon;
105
Michael Kolba3194d02011-09-07 11:23:51 -0700106 private static Paint sAlphaPaint = new Paint();
107 static {
108 sAlphaPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
109 sAlphaPaint.setColor(Color.TRANSPARENT);
110 }
111
Steve Block2466eff2011-10-03 15:33:09 +0100112 public enum SecurityState {
Steve Block4895b012011-10-03 16:26:46 +0100113 // The page's main resource does not use SSL. Note that we use this
114 // state irrespective of the SSL authentication state of sub-resources.
Steve Block2466eff2011-10-03 15:33:09 +0100115 SECURITY_STATE_NOT_SECURE,
Steve Block4895b012011-10-03 16:26:46 +0100116 // The page's main resource uses SSL and the certificate is good. The
117 // same is true of all sub-resources.
Steve Block2466eff2011-10-03 15:33:09 +0100118 SECURITY_STATE_SECURE,
Steve Block4895b012011-10-03 16:26:46 +0100119 // The page's main resource uses SSL and the certificate is good, but
120 // some sub-resources either do not use SSL or have problems with their
121 // certificates.
Steve Block2466eff2011-10-03 15:33:09 +0100122 SECURITY_STATE_MIXED,
Steve Block4895b012011-10-03 16:26:46 +0100123 // The page's main resource uses SSL but there is a problem with its
124 // certificate.
125 SECURITY_STATE_BAD_CERTIFICATE,
John Reck30c714c2010-12-16 17:30:34 -0800126 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700127
Michael Kolb14612442011-06-24 13:06:29 -0700128 Context mContext;
John Reckd8c74522011-06-14 08:45:00 -0700129 protected WebViewController mWebViewController;
Michael Kolb8233fac2010-10-26 16:08:53 -0700130
Michael Kolbc831b632011-05-11 09:30:34 -0700131 // The tab ID
John Reckd8c74522011-06-14 08:45:00 -0700132 private long mId = -1;
Michael Kolbc831b632011-05-11 09:30:34 -0700133
Grace Kloba22ac16e2009-10-07 18:00:23 -0700134 // The Geolocation permissions prompt
135 private GeolocationPermissionsPrompt mGeolocationPermissionsPrompt;
136 // Main WebView wrapper
Michael Kolba713ec82010-11-29 17:27:06 -0800137 private View mContainer;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700138 // Main WebView
139 private WebView mMainView;
140 // Subwindow container
141 private View mSubViewContainer;
142 // Subwindow WebView
143 private WebView mSubView;
144 // Saved bundle for when we are running low on memory. It contains the
145 // information needed to restore the WebView if the user goes back to the
146 // tab.
147 private Bundle mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700148 // Parent Tab. This is the Tab that created this Tab, or null if the Tab was
149 // created by the UI
Michael Kolbc831b632011-05-11 09:30:34 -0700150 private Tab mParent;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700151 // Tab that constructed by this Tab. This is used when this Tab is
152 // destroyed, it clears all mParentTab values in the children.
Michael Kolbc831b632011-05-11 09:30:34 -0700153 private Vector<Tab> mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700154 // If true, the tab is in the foreground of the current activity.
155 private boolean mInForeground;
Michael Kolb8233fac2010-10-26 16:08:53 -0700156 // If true, the tab is in page loading state (after onPageStarted,
157 // before onPageFinsihed)
158 private boolean mInPageLoad;
John Reck38b39652012-06-05 09:22:59 -0700159 private boolean mDisableOverrideUrlLoading;
John Reck30c714c2010-12-16 17:30:34 -0800160 // The last reported progress of the current page
161 private int mPageLoadProgress;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000162 // The time the load started, used to find load page time
163 private long mLoadStartTime;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700164 // Application identifier used to find tabs that another application wants
165 // to reuse.
166 private String mAppId;
Michael Kolbe28b3472011-08-04 16:54:31 -0700167 // flag to indicate if tab should be closed on back
168 private boolean mCloseOnBack;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700169 // Keep the original url around to avoid killing the old WebView if the url
170 // has not changed.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700171 // Error console for the tab
172 private ErrorConsoleView mErrorConsole;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -0500173 // The listener that gets invoked when a download is started from the
174 // mMainView
Selim Gurun0b3d66f2012-08-29 13:08:13 -0700175 private final BrowserDownloadListener mDownloadListener;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500176 // Listener used to know when we move forward or back in the history list.
177 private final WebBackForwardListClient mWebBackForwardListClient;
John Recke969cc52010-12-21 17:24:43 -0800178 private DataController mDataController;
Patrick Scott92066772011-03-10 08:46:27 -0500179 // State of the auto-login request.
180 private DeviceAccountLogin mDeviceAccountLogin;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700181
182 // AsyncTask for downloading touch icons
183 DownloadTouchIcon mTouchIconLoader;
184
John Reck35e9dd62011-04-25 09:01:54 -0700185 private BrowserSettings mSettings;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700186 private int mCaptureWidth;
187 private int mCaptureHeight;
188 private Bitmap mCapture;
189 private Handler mHandler;
Michael Kolb72864272012-05-03 15:42:15 -0700190 private boolean mUpdateThumbnail;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700191
Mathew Inwood1dd8e822011-08-03 14:34:29 +0100192 /**
Mathew Inwood9ad1eac2011-09-15 11:29:50 +0100193 * See {@link #clearBackStackWhenItemAdded(String)}.
Mathew Inwood1dd8e822011-08-03 14:34:29 +0100194 */
Mathew Inwooda829d552011-09-02 14:16:25 +0100195 private Pattern mClearHistoryUrlPattern;
Mathew Inwood1dd8e822011-08-03 14:34:29 +0100196
John Reck1cf4b792011-07-26 10:22:22 -0700197 private static synchronized Bitmap getDefaultFavicon(Context context) {
198 if (sDefaultFavicon == null) {
199 sDefaultFavicon = BitmapFactory.decodeResource(
200 context.getResources(), R.drawable.app_web_browser_sm);
201 }
202 return sDefaultFavicon;
203 }
Michael Kolbeb95db42011-03-03 10:38:40 -0800204
John Reck30c714c2010-12-16 17:30:34 -0800205 // All the state needed for a page
John Reckd8c74522011-06-14 08:45:00 -0700206 protected static class PageState {
John Reck30c714c2010-12-16 17:30:34 -0800207 String mUrl;
John Reckdb22ec42011-06-29 11:31:24 -0700208 String mOriginalUrl;
John Reck30c714c2010-12-16 17:30:34 -0800209 String mTitle;
Steve Block2466eff2011-10-03 15:33:09 +0100210 SecurityState mSecurityState;
Steve Block08a6f0c2011-10-06 12:12:53 +0100211 // This is non-null only when mSecurityState is SECURITY_STATE_BAD_CERTIFICATE.
212 SslError mSslCertificateError;
John Reck30c714c2010-12-16 17:30:34 -0800213 Bitmap mFavicon;
Steve Block08a6f0c2011-10-06 12:12:53 +0100214 boolean mIsBookmarkedSite;
215 boolean mIncognito;
John Reck30c714c2010-12-16 17:30:34 -0800216
217 PageState(Context c, boolean incognito) {
John Reck502a3532011-08-16 14:21:46 -0700218 mIncognito = incognito;
219 if (mIncognito) {
John Reckdb22ec42011-06-29 11:31:24 -0700220 mOriginalUrl = mUrl = "browser:incognito";
John Reck30c714c2010-12-16 17:30:34 -0800221 mTitle = c.getString(R.string.new_incognito_tab);
John Reck30c714c2010-12-16 17:30:34 -0800222 } else {
John Reckdb22ec42011-06-29 11:31:24 -0700223 mOriginalUrl = mUrl = "";
John Reck30c714c2010-12-16 17:30:34 -0800224 mTitle = c.getString(R.string.new_tab);
John Reck30c714c2010-12-16 17:30:34 -0800225 }
Steve Block2466eff2011-10-03 15:33:09 +0100226 mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
John Reck30c714c2010-12-16 17:30:34 -0800227 }
228
229 PageState(Context c, boolean incognito, String url, Bitmap favicon) {
John Reck502a3532011-08-16 14:21:46 -0700230 mIncognito = incognito;
John Reckdb22ec42011-06-29 11:31:24 -0700231 mOriginalUrl = mUrl = url;
John Reck30c714c2010-12-16 17:30:34 -0800232 if (URLUtil.isHttpsUrl(url)) {
Steve Block2466eff2011-10-03 15:33:09 +0100233 mSecurityState = SecurityState.SECURITY_STATE_SECURE;
John Reck30c714c2010-12-16 17:30:34 -0800234 } else {
Steve Block2466eff2011-10-03 15:33:09 +0100235 mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
John Reck30c714c2010-12-16 17:30:34 -0800236 }
John Reck1cf4b792011-07-26 10:22:22 -0700237 mFavicon = favicon;
John Reck30c714c2010-12-16 17:30:34 -0800238 }
John Reck1cf4b792011-07-26 10:22:22 -0700239
Grace Kloba22ac16e2009-10-07 18:00:23 -0700240 }
241
John Reck30c714c2010-12-16 17:30:34 -0800242 // The current/loading page's state
John Reckd8c74522011-06-14 08:45:00 -0700243 protected PageState mCurrentState;
John Reck30c714c2010-12-16 17:30:34 -0800244
Grace Kloba22ac16e2009-10-07 18:00:23 -0700245 // Used for saving and restoring each Tab
Michael Kolbc831b632011-05-11 09:30:34 -0700246 static final String ID = "ID";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700247 static final String CURRURL = "currentUrl";
248 static final String CURRTITLE = "currentTitle";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700249 static final String PARENTTAB = "parentTab";
250 static final String APPID = "appid";
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700251 static final String INCOGNITO = "privateBrowsingEnabled";
John Reckb0a86db2011-05-24 14:05:58 -0700252 static final String USERAGENT = "useragent";
Michael Kolbe28b3472011-08-04 16:54:31 -0700253 static final String CLOSEFLAG = "closeOnBack";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700254
Grace Kloba22ac16e2009-10-07 18:00:23 -0700255 // Container class for the next error dialog that needs to be displayed
256 private class ErrorDialog {
257 public final int mTitle;
258 public final String mDescription;
259 public final int mError;
260 ErrorDialog(int title, String desc, int error) {
261 mTitle = title;
262 mDescription = desc;
263 mError = error;
264 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700265 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700266
267 private void processNextError() {
268 if (mQueuedErrors == null) {
269 return;
270 }
271 // The first one is currently displayed so just remove it.
272 mQueuedErrors.removeFirst();
273 if (mQueuedErrors.size() == 0) {
274 mQueuedErrors = null;
275 return;
276 }
277 showError(mQueuedErrors.getFirst());
278 }
279
280 private DialogInterface.OnDismissListener mDialogListener =
281 new DialogInterface.OnDismissListener() {
282 public void onDismiss(DialogInterface d) {
283 processNextError();
284 }
285 };
286 private LinkedList<ErrorDialog> mQueuedErrors;
287
288 private void queueError(int err, String desc) {
289 if (mQueuedErrors == null) {
290 mQueuedErrors = new LinkedList<ErrorDialog>();
291 }
292 for (ErrorDialog d : mQueuedErrors) {
293 if (d.mError == err) {
294 // Already saw a similar error, ignore the new one.
295 return;
296 }
297 }
298 ErrorDialog errDialog = new ErrorDialog(
299 err == WebViewClient.ERROR_FILE_NOT_FOUND ?
300 R.string.browserFrameFileErrorLabel :
301 R.string.browserFrameNetworkErrorLabel,
302 desc, err);
303 mQueuedErrors.addLast(errDialog);
304
305 // Show the dialog now if the queue was empty and it is in foreground
306 if (mQueuedErrors.size() == 1 && mInForeground) {
307 showError(errDialog);
308 }
309 }
310
311 private void showError(ErrorDialog errDialog) {
312 if (mInForeground) {
Michael Kolb14612442011-06-24 13:06:29 -0700313 AlertDialog d = new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700314 .setTitle(errDialog.mTitle)
315 .setMessage(errDialog.mDescription)
316 .setPositiveButton(R.string.ok, null)
317 .create();
318 d.setOnDismissListener(mDialogListener);
319 d.show();
320 }
321 }
322
323 // -------------------------------------------------------------------------
324 // WebViewClient implementation for the main WebView
325 // -------------------------------------------------------------------------
326
Jonathan Dixonf4ff1492012-06-08 10:15:32 -0700327 private final WebViewClientClassicExt mWebViewClient = new WebViewClientClassicExt() {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500328 private Message mDontResend;
329 private Message mResend;
Michael Kolb47bd1e42011-09-01 15:25:00 -0700330
331 private boolean providersDiffer(String url, String otherUrl) {
332 Uri uri1 = Uri.parse(url);
333 Uri uri2 = Uri.parse(otherUrl);
334 return !uri1.getEncodedAuthority().equals(uri2.getEncodedAuthority());
335 }
336
Grace Kloba22ac16e2009-10-07 18:00:23 -0700337 @Override
338 public void onPageStarted(WebView view, String url, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700339 mInPageLoad = true;
Michael Kolb72864272012-05-03 15:42:15 -0700340 mUpdateThumbnail = true;
Michael Kolba53c9892011-10-05 13:31:40 -0700341 mPageLoadProgress = INITIAL_PROGRESS;
Michael Kolb14612442011-06-24 13:06:29 -0700342 mCurrentState = new PageState(mContext,
John Reck30c714c2010-12-16 17:30:34 -0800343 view.isPrivateBrowsingEnabled(), url, favicon);
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000344 mLoadStartTime = SystemClock.uptimeMillis();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700345
346 // If we start a touch icon load and then load a new page, we don't
347 // want to cancel the current touch icon loader. But, we do want to
348 // create a new one when the touch icon url is known.
349 if (mTouchIconLoader != null) {
350 mTouchIconLoader.mTab = null;
351 mTouchIconLoader = null;
352 }
353
354 // reset the error console
355 if (mErrorConsole != null) {
356 mErrorConsole.clearErrorMessages();
Michael Kolb8233fac2010-10-26 16:08:53 -0700357 if (mWebViewController.shouldShowErrorConsole()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700358 mErrorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
359 }
360 }
361
Patrick Scott92066772011-03-10 08:46:27 -0500362 // Cancel the auto-login process.
363 if (mDeviceAccountLogin != null) {
364 mDeviceAccountLogin.cancel();
365 mDeviceAccountLogin = null;
366 mWebViewController.hideAutoLogin(Tab.this);
367 }
368
Grace Kloba22ac16e2009-10-07 18:00:23 -0700369 // finally update the UI in the activity if it is in the foreground
John Reck324d4402011-01-11 16:56:42 -0800370 mWebViewController.onPageStarted(Tab.this, view, favicon);
Leon Scroggins4cd97792010-12-03 15:31:56 -0500371
John Recke969cc52010-12-21 17:24:43 -0800372 updateBookmarkedStatus();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700373 }
374
375 @Override
376 public void onPageFinished(WebView view, String url) {
John Reck38b39652012-06-05 09:22:59 -0700377 mDisableOverrideUrlLoading = false;
John Reck5b691842010-11-29 11:21:13 -0800378 if (!isPrivateBrowsingEnabled()) {
379 LogTag.logPageFinishedLoading(
380 url, SystemClock.uptimeMillis() - mLoadStartTime);
381 }
John Reck1cf4b792011-07-26 10:22:22 -0700382 syncCurrentState(view, url);
John Reck324d4402011-01-11 16:56:42 -0800383 mWebViewController.onPageFinished(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700384 }
385
386 // return true if want to hijack the url to let another app to handle it
387 @Override
388 public boolean shouldOverrideUrlLoading(WebView view, String url) {
John Reck38b39652012-06-05 09:22:59 -0700389 if (!mDisableOverrideUrlLoading && mInForeground) {
Michael Kolb18eb3772010-12-10 14:29:51 -0800390 return mWebViewController.shouldOverrideUrlLoading(Tab.this,
391 view, url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700392 } else {
393 return false;
394 }
395 }
396
397 /**
Steve Block2466eff2011-10-03 15:33:09 +0100398 * Updates the security state. This method is called when we discover
399 * another resource to be loaded for this page (for example,
400 * javascript). While we update the security state, we do not update
401 * the lock icon until we are done loading, as it is slightly more
402 * secure this way.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700403 */
404 @Override
405 public void onLoadResource(WebView view, String url) {
406 if (url != null && url.length() > 0) {
407 // It is only if the page claims to be secure that we may have
Steve Block2466eff2011-10-03 15:33:09 +0100408 // to update the security state:
409 if (mCurrentState.mSecurityState == SecurityState.SECURITY_STATE_SECURE) {
410 // If NOT a 'safe' url, change the state to mixed content!
Grace Kloba22ac16e2009-10-07 18:00:23 -0700411 if (!(URLUtil.isHttpsUrl(url) || URLUtil.isDataUrl(url)
412 || URLUtil.isAboutUrl(url))) {
Steve Block2466eff2011-10-03 15:33:09 +0100413 mCurrentState.mSecurityState = SecurityState.SECURITY_STATE_MIXED;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700414 }
415 }
416 }
417 }
418
419 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700420 * Show a dialog informing the user of the network error reported by
421 * WebCore if it is in the foreground.
422 */
423 @Override
424 public void onReceivedError(WebView view, int errorCode,
425 String description, String failingUrl) {
426 if (errorCode != WebViewClient.ERROR_HOST_LOOKUP &&
427 errorCode != WebViewClient.ERROR_CONNECT &&
428 errorCode != WebViewClient.ERROR_BAD_URL &&
429 errorCode != WebViewClient.ERROR_UNSUPPORTED_SCHEME &&
430 errorCode != WebViewClient.ERROR_FILE) {
431 queueError(errorCode, description);
Jeff Hamilton47654f42010-09-07 09:57:51 -0500432
Selim Gurun3da06b82011-10-10 13:58:12 -0700433 // Don't log URLs when in private browsing mode
434 if (!isPrivateBrowsingEnabled()) {
435 Log.e(LOGTAG, "onReceivedError " + errorCode + " " + failingUrl
Jeff Hamilton47654f42010-09-07 09:57:51 -0500436 + " " + description);
Selim Gurun3da06b82011-10-10 13:58:12 -0700437 }
Jeff Hamilton47654f42010-09-07 09:57:51 -0500438 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700439 }
440
441 /**
442 * Check with the user if it is ok to resend POST data as the page they
443 * are trying to navigate to is the result of a POST.
444 */
445 @Override
446 public void onFormResubmission(WebView view, final Message dontResend,
447 final Message resend) {
448 if (!mInForeground) {
449 dontResend.sendToTarget();
450 return;
451 }
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500452 if (mDontResend != null) {
453 Log.w(LOGTAG, "onFormResubmission should not be called again "
454 + "while dialog is still up");
455 dontResend.sendToTarget();
456 return;
457 }
458 mDontResend = dontResend;
459 mResend = resend;
Michael Kolb14612442011-06-24 13:06:29 -0700460 new AlertDialog.Builder(mContext).setTitle(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700461 R.string.browserFrameFormResubmitLabel).setMessage(
462 R.string.browserFrameFormResubmitMessage)
463 .setPositiveButton(R.string.ok,
464 new DialogInterface.OnClickListener() {
465 public void onClick(DialogInterface dialog,
466 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500467 if (mResend != null) {
468 mResend.sendToTarget();
469 mResend = null;
470 mDontResend = null;
471 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700472 }
473 }).setNegativeButton(R.string.cancel,
474 new DialogInterface.OnClickListener() {
475 public void onClick(DialogInterface dialog,
476 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500477 if (mDontResend != null) {
478 mDontResend.sendToTarget();
479 mResend = null;
480 mDontResend = null;
481 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700482 }
483 }).setOnCancelListener(new OnCancelListener() {
484 public void onCancel(DialogInterface dialog) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500485 if (mDontResend != null) {
486 mDontResend.sendToTarget();
487 mResend = null;
488 mDontResend = null;
489 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700490 }
491 }).show();
492 }
493
494 /**
495 * Insert the url into the visited history database.
496 * @param url The url to be inserted.
497 * @param isReload True if this url is being reloaded.
498 * FIXME: Not sure what to do when reloading the page.
499 */
500 @Override
501 public void doUpdateVisitedHistory(WebView view, String url,
502 boolean isReload) {
John Reck324d4402011-01-11 16:56:42 -0800503 mWebViewController.doUpdateVisitedHistory(Tab.this, isReload);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700504 }
505
506 /**
507 * Displays SSL error(s) dialog to the user.
508 */
509 @Override
510 public void onReceivedSslError(final WebView view,
511 final SslErrorHandler handler, final SslError error) {
512 if (!mInForeground) {
513 handler.cancel();
Steve Block2466eff2011-10-03 15:33:09 +0100514 setSecurityState(SecurityState.SECURITY_STATE_NOT_SECURE);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700515 return;
516 }
John Reck35e9dd62011-04-25 09:01:54 -0700517 if (mSettings.showSecurityWarnings()) {
John Reckcb28b2c2011-08-26 17:39:44 -0700518 new AlertDialog.Builder(mContext)
519 .setTitle(R.string.security_warning)
520 .setMessage(R.string.ssl_warnings_header)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200521 .setIconAttribute(android.R.attr.alertDialogIcon)
John Reckcb28b2c2011-08-26 17:39:44 -0700522 .setPositiveButton(R.string.ssl_continue,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700523 new DialogInterface.OnClickListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700524 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700525 public void onClick(DialogInterface dialog,
526 int whichButton) {
527 handler.proceed();
Steve Block4895b012011-10-03 16:26:46 +0100528 handleProceededAfterSslError(error);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700529 }
John Reckcb28b2c2011-08-26 17:39:44 -0700530 })
531 .setNeutralButton(R.string.view_certificate,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700532 new DialogInterface.OnClickListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700533 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700534 public void onClick(DialogInterface dialog,
535 int whichButton) {
John Reckcb28b2c2011-08-26 17:39:44 -0700536 mWebViewController.showSslCertificateOnError(
537 view, handler, error);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700538 }
John Reckcb28b2c2011-08-26 17:39:44 -0700539 })
540 .setNegativeButton(R.string.ssl_go_back,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700541 new DialogInterface.OnClickListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700542 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700543 public void onClick(DialogInterface dialog,
544 int whichButton) {
John Reck30c714c2010-12-16 17:30:34 -0800545 dialog.cancel();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700546 }
John Reckcb28b2c2011-08-26 17:39:44 -0700547 })
548 .setOnCancelListener(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700549 new DialogInterface.OnCancelListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700550 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700551 public void onCancel(DialogInterface dialog) {
552 handler.cancel();
Steve Block2466eff2011-10-03 15:33:09 +0100553 setSecurityState(SecurityState.SECURITY_STATE_NOT_SECURE);
John Reck30c714c2010-12-16 17:30:34 -0800554 mWebViewController.onUserCanceledSsl(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700555 }
John Reckcb28b2c2011-08-26 17:39:44 -0700556 })
557 .show();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700558 } else {
559 handler.proceed();
560 }
561 }
562
563 /**
Steve Block4895b012011-10-03 16:26:46 +0100564 * Called when an SSL error occurred while loading a resource, but the
565 * WebView but chose to proceed anyway based on a decision retained
566 * from a previous response to onReceivedSslError(). We update our
567 * security state to reflect this.
568 */
569 @Override
570 public void onProceededAfterSslError(WebView view, SslError error) {
571 handleProceededAfterSslError(error);
572 }
573
574 /**
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700575 * Displays client certificate request to the user.
576 */
577 @Override
578 public void onReceivedClientCertRequest(final WebView view,
579 final ClientCertRequestHandler handler, final String host_and_port) {
580 if (!mInForeground) {
581 handler.ignore();
582 return;
583 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700584 int colon = host_and_port.lastIndexOf(':');
585 String host;
586 int port;
587 if (colon == -1) {
588 host = host_and_port;
589 port = -1;
590 } else {
591 String portString = host_and_port.substring(colon + 1);
592 try {
593 port = Integer.parseInt(portString);
594 host = host_and_port.substring(0, colon);
595 } catch (NumberFormatException e) {
596 host = host_and_port;
597 port = -1;
598 }
599 }
Michael Kolb14612442011-06-24 13:06:29 -0700600 KeyChain.choosePrivateKeyAlias(
601 mWebViewController.getActivity(), new KeyChainAliasCallback() {
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700602 @Override public void alias(String alias) {
603 if (alias == null) {
604 handler.cancel();
605 return;
606 }
Michael Kolb14612442011-06-24 13:06:29 -0700607 new KeyChainLookup(mContext, handler, alias).execute();
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700608 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700609 }, null, null, host, port, null);
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700610 }
611
612 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700613 * Handles an HTTP authentication request.
614 *
615 * @param handler The authentication handler
616 * @param host The host
617 * @param realm The realm
618 */
619 @Override
620 public void onReceivedHttpAuthRequest(WebView view,
621 final HttpAuthHandler handler, final String host,
622 final String realm) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700623 mWebViewController.onReceivedHttpAuthRequest(Tab.this, view, handler, host, realm);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700624 }
625
626 @Override
John Reck438bf462011-01-12 18:11:46 -0800627 public WebResourceResponse shouldInterceptRequest(WebView view,
628 String url) {
629 WebResourceResponse res = HomeProvider.shouldInterceptRequest(
Michael Kolb14612442011-06-24 13:06:29 -0700630 mContext, url);
John Reck438bf462011-01-12 18:11:46 -0800631 return res;
632 }
633
634 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700635 public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
636 if (!mInForeground) {
637 return false;
638 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700639 return mWebViewController.shouldOverrideKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700640 }
641
642 @Override
643 public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700644 if (!mInForeground) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700645 return;
646 }
John Reck997b1b72012-04-19 18:08:25 -0700647 if (!mWebViewController.onUnhandledKeyEvent(event)) {
648 super.onUnhandledKeyEvent(view, event);
649 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700650 }
Patrick Scott92066772011-03-10 08:46:27 -0500651
652 @Override
653 public void onReceivedLoginRequest(WebView view, String realm,
654 String account, String args) {
Michael Kolb14612442011-06-24 13:06:29 -0700655 new DeviceAccountLogin(mWebViewController.getActivity(), view, Tab.this, mWebViewController)
Patrick Scott92066772011-03-10 08:46:27 -0500656 .handleLogin(realm, account, args);
657 }
658
Grace Kloba22ac16e2009-10-07 18:00:23 -0700659 };
660
John Reck1cf4b792011-07-26 10:22:22 -0700661 private void syncCurrentState(WebView view, String url) {
662 // Sync state (in case of stop/timeout)
663 mCurrentState.mUrl = view.getUrl();
664 if (mCurrentState.mUrl == null) {
John Reck8bcafc12011-08-29 16:43:02 -0700665 mCurrentState.mUrl = "";
John Reck1cf4b792011-07-26 10:22:22 -0700666 }
667 mCurrentState.mOriginalUrl = view.getOriginalUrl();
668 mCurrentState.mTitle = view.getTitle();
669 mCurrentState.mFavicon = view.getFavicon();
670 if (!URLUtil.isHttpsUrl(mCurrentState.mUrl)) {
671 // In case we stop when loading an HTTPS page from an HTTP page
672 // but before a provisional load occurred
Steve Block2466eff2011-10-03 15:33:09 +0100673 mCurrentState.mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
Steve Block08a6f0c2011-10-06 12:12:53 +0100674 mCurrentState.mSslCertificateError = null;
John Reck1cf4b792011-07-26 10:22:22 -0700675 }
John Reck502a3532011-08-16 14:21:46 -0700676 mCurrentState.mIncognito = view.isPrivateBrowsingEnabled();
John Reck1cf4b792011-07-26 10:22:22 -0700677 }
678
Patrick Scott92066772011-03-10 08:46:27 -0500679 // Called by DeviceAccountLogin when the Tab needs to have the auto-login UI
680 // displayed.
681 void setDeviceAccountLogin(DeviceAccountLogin login) {
682 mDeviceAccountLogin = login;
683 }
684
685 // Returns non-null if the title bar should display the auto-login UI.
686 DeviceAccountLogin getDeviceAccountLogin() {
687 return mDeviceAccountLogin;
688 }
689
Grace Kloba22ac16e2009-10-07 18:00:23 -0700690 // -------------------------------------------------------------------------
691 // WebChromeClient implementation for the main WebView
692 // -------------------------------------------------------------------------
693
694 private final WebChromeClient mWebChromeClient = new WebChromeClient() {
695 // Helper method to create a new tab or sub window.
696 private void createWindow(final boolean dialog, final Message msg) {
697 WebView.WebViewTransport transport =
698 (WebView.WebViewTransport) msg.obj;
699 if (dialog) {
700 createSubWindow();
Michael Kolb8233fac2010-10-26 16:08:53 -0700701 mWebViewController.attachSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700702 transport.setWebView(mSubView);
703 } else {
Michael Kolb7bcafde2011-05-09 13:55:59 -0700704 final Tab newTab = mWebViewController.openTab(null,
John Reck5949c662011-05-27 09:52:29 -0700705 Tab.this, true, true);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700706 transport.setWebView(newTab.getWebView());
707 }
708 msg.sendToTarget();
709 }
710
711 @Override
712 public boolean onCreateWindow(WebView view, final boolean dialog,
713 final boolean userGesture, final Message resultMsg) {
714 // only allow new window or sub window for the foreground case
715 if (!mInForeground) {
716 return false;
717 }
718 // Short-circuit if we can't create any more tabs or sub windows.
719 if (dialog && mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -0700720 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700721 .setTitle(R.string.too_many_subwindows_dialog_title)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200722 .setIconAttribute(android.R.attr.alertDialogIcon)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700723 .setMessage(R.string.too_many_subwindows_dialog_message)
724 .setPositiveButton(R.string.ok, null)
725 .show();
726 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700727 } else if (!mWebViewController.getTabControl().canCreateNewTab()) {
Michael Kolb14612442011-06-24 13:06:29 -0700728 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700729 .setTitle(R.string.too_many_windows_dialog_title)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200730 .setIconAttribute(android.R.attr.alertDialogIcon)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700731 .setMessage(R.string.too_many_windows_dialog_message)
732 .setPositiveButton(R.string.ok, null)
733 .show();
734 return false;
735 }
736
737 // Short-circuit if this was a user gesture.
738 if (userGesture) {
739 createWindow(dialog, resultMsg);
740 return true;
741 }
742
743 // Allow the popup and create the appropriate window.
744 final AlertDialog.OnClickListener allowListener =
745 new AlertDialog.OnClickListener() {
746 public void onClick(DialogInterface d,
747 int which) {
748 createWindow(dialog, resultMsg);
749 }
750 };
751
752 // Block the popup by returning a null WebView.
753 final AlertDialog.OnClickListener blockListener =
754 new AlertDialog.OnClickListener() {
755 public void onClick(DialogInterface d, int which) {
756 resultMsg.sendToTarget();
757 }
758 };
759
760 // Build a confirmation dialog to display to the user.
761 final AlertDialog d =
Michael Kolb14612442011-06-24 13:06:29 -0700762 new AlertDialog.Builder(mContext)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200763 .setIconAttribute(android.R.attr.alertDialogIcon)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700764 .setMessage(R.string.popup_window_attempt)
765 .setPositiveButton(R.string.allow, allowListener)
766 .setNegativeButton(R.string.block, blockListener)
767 .setCancelable(false)
768 .create();
769
770 // Show the confirmation dialog.
771 d.show();
772 return true;
773 }
774
775 @Override
Patrick Scotteb5061b2009-11-18 15:00:30 -0500776 public void onRequestFocus(WebView view) {
777 if (!mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700778 mWebViewController.switchToTab(Tab.this);
Patrick Scotteb5061b2009-11-18 15:00:30 -0500779 }
780 }
781
782 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700783 public void onCloseWindow(WebView window) {
Michael Kolbc831b632011-05-11 09:30:34 -0700784 if (mParent != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700785 // JavaScript can only close popup window.
786 if (mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700787 mWebViewController.switchToTab(mParent);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700788 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700789 mWebViewController.closeTab(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700790 }
791 }
792
793 @Override
794 public void onProgressChanged(WebView view, int newProgress) {
John Reck30c714c2010-12-16 17:30:34 -0800795 mPageLoadProgress = newProgress;
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800796 if (newProgress == 100) {
797 mInPageLoad = false;
798 }
John Reck30c714c2010-12-16 17:30:34 -0800799 mWebViewController.onProgressChanged(Tab.this);
Michael Kolb72864272012-05-03 15:42:15 -0700800 if (mUpdateThumbnail && newProgress == 100) {
801 mUpdateThumbnail = false;
802 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700803 }
804
805 @Override
Leon Scroggins21d9b902010-03-11 09:33:11 -0500806 public void onReceivedTitle(WebView view, final String title) {
John Reck30c714c2010-12-16 17:30:34 -0800807 mCurrentState.mTitle = title;
Michael Kolb8233fac2010-10-26 16:08:53 -0700808 mWebViewController.onReceivedTitle(Tab.this, title);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700809 }
810
811 @Override
812 public void onReceivedIcon(WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -0800813 mCurrentState.mFavicon = icon;
Michael Kolb8233fac2010-10-26 16:08:53 -0700814 mWebViewController.onFavicon(Tab.this, view, icon);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700815 }
816
817 @Override
818 public void onReceivedTouchIconUrl(WebView view, String url,
819 boolean precomposed) {
Michael Kolb14612442011-06-24 13:06:29 -0700820 final ContentResolver cr = mContext.getContentResolver();
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400821 // Let precomposed icons take precedence over non-composed
822 // icons.
823 if (precomposed && mTouchIconLoader != null) {
824 mTouchIconLoader.cancel(false);
825 mTouchIconLoader = null;
826 }
827 // Have only one async task at a time.
828 if (mTouchIconLoader == null) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700829 mTouchIconLoader = new DownloadTouchIcon(Tab.this,
Michael Kolb14612442011-06-24 13:06:29 -0700830 mContext, cr, view);
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400831 mTouchIconLoader.execute(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700832 }
833 }
834
835 @Override
836 public void onShowCustomView(View view,
837 WebChromeClient.CustomViewCallback callback) {
Michael Kolb14612442011-06-24 13:06:29 -0700838 Activity activity = mWebViewController.getActivity();
839 if (activity != null) {
840 onShowCustomView(view, activity.getRequestedOrientation(), callback);
841 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400842 }
843
844 @Override
845 public void onShowCustomView(View view, int requestedOrientation,
846 WebChromeClient.CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700847 if (mInForeground) mWebViewController.showCustomView(Tab.this, view,
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400848 requestedOrientation, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700849 }
850
851 @Override
852 public void onHideCustomView() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700853 if (mInForeground) mWebViewController.hideCustomView();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700854 }
855
856 /**
857 * The origin has exceeded its database quota.
858 * @param url the URL that exceeded the quota
859 * @param databaseIdentifier the identifier of the database on which the
860 * transaction that caused the quota overflow was run
861 * @param currentQuota the current quota for the origin.
862 * @param estimatedSize the estimated size of the database.
863 * @param totalUsedQuota is the sum of all origins' quota.
864 * @param quotaUpdater The callback to run when a decision to allow or
865 * deny quota has been made. Don't forget to call this!
866 */
867 @Override
868 public void onExceededDatabaseQuota(String url,
869 String databaseIdentifier, long currentQuota, long estimatedSize,
870 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -0700871 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -0700872 .onExceededDatabaseQuota(url, databaseIdentifier,
873 currentQuota, estimatedSize, totalUsedQuota,
874 quotaUpdater);
875 }
876
877 /**
878 * The Application Cache has exceeded its max size.
879 * @param spaceNeeded is the amount of disk space that would be needed
880 * in order for the last appcache operation to succeed.
881 * @param totalUsedQuota is the sum of all origins' quota.
882 * @param quotaUpdater A callback to inform the WebCore thread that a
883 * new app cache size is available. This callback must always
884 * be executed at some point to ensure that the sleeping
885 * WebCore thread is woken up.
886 */
887 @Override
888 public void onReachedMaxAppCacheSize(long spaceNeeded,
889 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -0700890 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -0700891 .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota,
892 quotaUpdater);
893 }
894
895 /**
896 * Instructs the browser to show a prompt to ask the user to set the
897 * Geolocation permission state for the specified origin.
898 * @param origin The origin for which Geolocation permissions are
899 * requested.
900 * @param callback The callback to call once the user has set the
901 * Geolocation permission state.
902 */
903 @Override
904 public void onGeolocationPermissionsShowPrompt(String origin,
905 GeolocationPermissions.Callback callback) {
906 if (mInForeground) {
Grace Kloba50c241e2010-04-20 11:07:50 -0700907 getGeolocationPermissionsPrompt().show(origin, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700908 }
909 }
910
911 /**
912 * Instructs the browser to hide the Geolocation permissions prompt.
913 */
914 @Override
915 public void onGeolocationPermissionsHidePrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -0700916 if (mInForeground && mGeolocationPermissionsPrompt != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700917 mGeolocationPermissionsPrompt.hide();
918 }
919 }
920
Ben Murdoch65acc352009-11-19 18:16:04 +0000921 /* Adds a JavaScript error message to the system log and if the JS
922 * console is enabled in the about:debug options, to that console
923 * also.
Ben Murdochc42addf2010-01-28 15:19:59 +0000924 * @param consoleMessage the message object.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700925 */
926 @Override
Ben Murdochc42addf2010-01-28 15:19:59 +0000927 public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700928 if (mInForeground) {
929 // call getErrorConsole(true) so it will create one if needed
930 ErrorConsoleView errorConsole = getErrorConsole(true);
Ben Murdochc42addf2010-01-28 15:19:59 +0000931 errorConsole.addErrorMessage(consoleMessage);
Michael Kolb8233fac2010-10-26 16:08:53 -0700932 if (mWebViewController.shouldShowErrorConsole()
933 && errorConsole.getShowState() !=
934 ErrorConsoleView.SHOW_MAXIMIZED) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700935 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
936 }
937 }
Ben Murdochc42addf2010-01-28 15:19:59 +0000938
Jeff Hamilton47654f42010-09-07 09:57:51 -0500939 // Don't log console messages in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -0700940 if (isPrivateBrowsingEnabled()) return true;
Jeff Hamilton47654f42010-09-07 09:57:51 -0500941
Ben Murdochc42addf2010-01-28 15:19:59 +0000942 String message = "Console: " + consoleMessage.message() + " "
943 + consoleMessage.sourceId() + ":"
944 + consoleMessage.lineNumber();
945
946 switch (consoleMessage.messageLevel()) {
947 case TIP:
948 Log.v(CONSOLE_LOGTAG, message);
949 break;
950 case LOG:
951 Log.i(CONSOLE_LOGTAG, message);
952 break;
953 case WARNING:
954 Log.w(CONSOLE_LOGTAG, message);
955 break;
956 case ERROR:
957 Log.e(CONSOLE_LOGTAG, message);
958 break;
959 case DEBUG:
960 Log.d(CONSOLE_LOGTAG, message);
961 break;
962 }
963
964 return true;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700965 }
966
967 /**
968 * Ask the browser for an icon to represent a <video> element.
969 * This icon will be used if the Web page did not specify a poster attribute.
970 * @return Bitmap The icon or null if no such icon is available.
971 */
972 @Override
973 public Bitmap getDefaultVideoPoster() {
974 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700975 return mWebViewController.getDefaultVideoPoster();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700976 }
977 return null;
978 }
979
980 /**
981 * Ask the host application for a custom progress view to show while
982 * a <video> is loading.
983 * @return View The progress view.
984 */
985 @Override
986 public View getVideoLoadingProgressView() {
987 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700988 return mWebViewController.getVideoLoadingProgressView();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700989 }
990 return null;
991 }
992
993 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +0000994 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700995 if (mInForeground) {
Ben Murdoch8cad4132012-01-11 10:56:43 +0000996 mWebViewController.openFileChooser(uploadMsg, acceptType, capture);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700997 } else {
998 uploadMsg.onReceiveValue(null);
999 }
1000 }
1001
1002 /**
1003 * Deliver a list of already-visited URLs
1004 */
1005 @Override
1006 public void getVisitedHistory(final ValueCallback<String[]> callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001007 mWebViewController.getVisitedHistory(callback);
1008 }
Ben Murdoch8029a772010-11-16 11:58:21 +00001009
1010 @Override
1011 public void setupAutoFill(Message message) {
1012 // Prompt the user to set up their profile.
1013 final Message msg = message;
Michael Kolb14612442011-06-24 13:06:29 -07001014 AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
1015 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
Ben Murdoch1d676b62011-01-17 12:54:24 +00001016 Context.LAYOUT_INFLATER_SERVICE);
1017 final View layout = inflater.inflate(R.layout.setup_autofill_dialog, null);
1018
1019 builder.setView(layout)
1020 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1021 @Override
1022 public void onClick(DialogInterface dialog, int id) {
1023 CheckBox disableAutoFill = (CheckBox) layout.findViewById(
1024 R.id.setup_autofill_dialog_disable_autofill);
1025
1026 if (disableAutoFill.isChecked()) {
1027 // Disable autofill and show a toast with how to turn it on again.
John Reck35e9dd62011-04-25 09:01:54 -07001028 mSettings.setAutofillEnabled(false);
Michael Kolb14612442011-06-24 13:06:29 -07001029 Toast.makeText(mContext,
Ben Murdoch1d676b62011-01-17 12:54:24 +00001030 R.string.autofill_setup_dialog_negative_toast,
1031 Toast.LENGTH_LONG).show();
1032 } else {
1033 // Take user to the AutoFill profile editor. When they return,
1034 // we will send the message that we pass here which will trigger
1035 // the form to get filled out with their new profile.
1036 mWebViewController.setupAutoFill(msg);
1037 }
1038 }
1039 })
1040 .setNegativeButton(R.string.cancel, null)
1041 .show();
Ben Murdoch8029a772010-11-16 11:58:21 +00001042 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001043 };
1044
1045 // -------------------------------------------------------------------------
1046 // WebViewClient implementation for the sub window
1047 // -------------------------------------------------------------------------
1048
1049 // Subclass of WebViewClient used in subwindows to notify the main
1050 // WebViewClient of certain WebView activities.
Jonathan Dixonf4ff1492012-06-08 10:15:32 -07001051 private static class SubWindowClient extends WebViewClientClassicExt {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001052 // The main WebViewClient.
Jonathan Dixonf4ff1492012-06-08 10:15:32 -07001053 private final WebViewClientClassicExt mClient;
Michael Kolb8233fac2010-10-26 16:08:53 -07001054 private final WebViewController mController;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001055
Jonathan Dixonf4ff1492012-06-08 10:15:32 -07001056 SubWindowClient(WebViewClientClassicExt client, WebViewController controller) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001057 mClient = client;
Michael Kolb8233fac2010-10-26 16:08:53 -07001058 mController = controller;
Leon Scroggins III211ba542010-04-19 13:21:13 -04001059 }
1060 @Override
1061 public void onPageStarted(WebView view, String url, Bitmap favicon) {
1062 // Unlike the others, do not call mClient's version, which would
1063 // change the progress bar. However, we do want to remove the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001064 // find or select dialog.
Michael Kolb8233fac2010-10-26 16:08:53 -07001065 mController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001066 }
1067 @Override
1068 public void doUpdateVisitedHistory(WebView view, String url,
1069 boolean isReload) {
1070 mClient.doUpdateVisitedHistory(view, url, isReload);
1071 }
1072 @Override
1073 public boolean shouldOverrideUrlLoading(WebView view, String url) {
1074 return mClient.shouldOverrideUrlLoading(view, url);
1075 }
1076 @Override
1077 public void onReceivedSslError(WebView view, SslErrorHandler handler,
1078 SslError error) {
1079 mClient.onReceivedSslError(view, handler, error);
1080 }
1081 @Override
Brian Carlstrom8862c1d2011-06-02 01:05:55 -07001082 public void onReceivedClientCertRequest(WebView view,
1083 ClientCertRequestHandler handler, String host_and_port) {
1084 mClient.onReceivedClientCertRequest(view, handler, host_and_port);
1085 }
1086 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -07001087 public void onReceivedHttpAuthRequest(WebView view,
1088 HttpAuthHandler handler, String host, String realm) {
1089 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
1090 }
1091 @Override
1092 public void onFormResubmission(WebView view, Message dontResend,
1093 Message resend) {
1094 mClient.onFormResubmission(view, dontResend, resend);
1095 }
1096 @Override
1097 public void onReceivedError(WebView view, int errorCode,
1098 String description, String failingUrl) {
1099 mClient.onReceivedError(view, errorCode, description, failingUrl);
1100 }
1101 @Override
1102 public boolean shouldOverrideKeyEvent(WebView view,
1103 android.view.KeyEvent event) {
1104 return mClient.shouldOverrideKeyEvent(view, event);
1105 }
1106 @Override
1107 public void onUnhandledKeyEvent(WebView view,
1108 android.view.KeyEvent event) {
1109 mClient.onUnhandledKeyEvent(view, event);
1110 }
1111 }
1112
1113 // -------------------------------------------------------------------------
1114 // WebChromeClient implementation for the sub window
1115 // -------------------------------------------------------------------------
1116
1117 private class SubWindowChromeClient extends WebChromeClient {
1118 // The main WebChromeClient.
1119 private final WebChromeClient mClient;
1120
1121 SubWindowChromeClient(WebChromeClient client) {
1122 mClient = client;
1123 }
1124 @Override
1125 public void onProgressChanged(WebView view, int newProgress) {
1126 mClient.onProgressChanged(view, newProgress);
1127 }
1128 @Override
1129 public boolean onCreateWindow(WebView view, boolean dialog,
1130 boolean userGesture, android.os.Message resultMsg) {
1131 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
1132 }
1133 @Override
1134 public void onCloseWindow(WebView window) {
1135 if (window != mSubView) {
1136 Log.e(LOGTAG, "Can't close the window");
1137 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001138 mWebViewController.dismissSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001139 }
1140 }
1141
1142 // -------------------------------------------------------------------------
1143
1144 // Construct a new tab
Michael Kolb7bcafde2011-05-09 13:55:59 -07001145 Tab(WebViewController wvcontroller, WebView w) {
John Reck1cf4b792011-07-26 10:22:22 -07001146 this(wvcontroller, w, null);
1147 }
1148
1149 Tab(WebViewController wvcontroller, Bundle state) {
1150 this(wvcontroller, null, state);
1151 }
1152
1153 Tab(WebViewController wvcontroller, WebView w, Bundle state) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001154 mWebViewController = wvcontroller;
Michael Kolb14612442011-06-24 13:06:29 -07001155 mContext = mWebViewController.getContext();
John Reck35e9dd62011-04-25 09:01:54 -07001156 mSettings = BrowserSettings.getInstance();
Michael Kolb14612442011-06-24 13:06:29 -07001157 mDataController = DataController.getInstance(mContext);
1158 mCurrentState = new PageState(mContext, w != null
John Reck847b5322011-04-14 17:02:18 -07001159 ? w.isPrivateBrowsingEnabled() : false);
Michael Kolb8233fac2010-10-26 16:08:53 -07001160 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001161 mInForeground = false;
1162
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001163 mDownloadListener = new BrowserDownloadListener() {
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001164 public void onDownloadStart(String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001165 String contentDisposition, String mimetype, String referer,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001166 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001167 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001168 mimetype, referer, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001169 }
1170 };
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001171 mWebBackForwardListClient = new WebBackForwardListClient() {
1172 @Override
1173 public void onNewHistoryItem(WebHistoryItem item) {
Mathew Inwooda829d552011-09-02 14:16:25 +01001174 if (mClearHistoryUrlPattern != null) {
1175 boolean match =
1176 mClearHistoryUrlPattern.matcher(item.getOriginalUrl()).matches();
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001177 if (LOGD_ENABLED) {
Mathew Inwooda829d552011-09-02 14:16:25 +01001178 Log.d(LOGTAG, "onNewHistoryItem: match=" + match + "\n\t"
1179 + item.getUrl() + "\n\t"
1180 + mClearHistoryUrlPattern);
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001181 }
Mathew Inwooda829d552011-09-02 14:16:25 +01001182 if (match) {
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001183 if (mMainView != null) {
1184 mMainView.clearHistory();
1185 }
1186 }
Mathew Inwooda829d552011-09-02 14:16:25 +01001187 mClearHistoryUrlPattern = null;
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001188 }
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001189 }
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001190 };
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001191
John Reck1cf4b792011-07-26 10:22:22 -07001192 mCaptureWidth = mContext.getResources().getDimensionPixelSize(
1193 R.dimen.tab_thumbnail_width);
1194 mCaptureHeight = mContext.getResources().getDimensionPixelSize(
1195 R.dimen.tab_thumbnail_height);
1196 updateShouldCaptureThumbnails();
1197 restoreState(state);
John Reck52be4782011-08-26 15:37:29 -07001198 if (getId() == -1) {
1199 mId = TabControl.getNextId();
1200 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001201 setWebView(w);
Michael Kolb9ef259a2011-07-12 15:33:08 -07001202 mHandler = new Handler() {
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001203 @Override
Michael Kolb9ef259a2011-07-12 15:33:08 -07001204 public void handleMessage(Message m) {
John Reck1cf4b792011-07-26 10:22:22 -07001205 switch (m.what) {
1206 case MSG_CAPTURE:
1207 capture();
1208 break;
1209 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001210 }
1211 };
John Reck1cf4b792011-07-26 10:22:22 -07001212 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001213
Michael Kolb72864272012-05-03 15:42:15 -07001214 public boolean shouldUpdateThumbnail() {
1215 return mUpdateThumbnail;
1216 }
1217
Mathew Inwoode09305e2011-09-02 12:03:26 +01001218 /**
1219 * This is used to get a new ID when the tab has been preloaded, before it is displayed and
1220 * added to TabControl. Preloaded tabs can be created before restoreInstanceState, leading
1221 * to overlapping IDs between the preloaded and restored tabs.
1222 */
1223 public void refreshIdAfterPreload() {
1224 mId = TabControl.getNextId();
1225 }
1226
John Reck1cf4b792011-07-26 10:22:22 -07001227 public void updateShouldCaptureThumbnails() {
1228 if (mWebViewController.shouldCaptureThumbnails()) {
1229 synchronized (Tab.this) {
1230 if (mCapture == null) {
1231 mCapture = Bitmap.createBitmap(mCaptureWidth, mCaptureHeight,
1232 Bitmap.Config.RGB_565);
Michael Kolbc3af0672011-08-09 10:24:41 -07001233 mCapture.eraseColor(Color.WHITE);
John Reck1cf4b792011-07-26 10:22:22 -07001234 if (mInForeground) {
1235 postCapture();
1236 }
1237 }
1238 }
1239 } else {
1240 synchronized (Tab.this) {
1241 mCapture = null;
1242 deleteThumbnail();
1243 }
1244 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001245 }
1246
Michael Kolb14612442011-06-24 13:06:29 -07001247 public void setController(WebViewController ctl) {
1248 mWebViewController = ctl;
John Reck1cf4b792011-07-26 10:22:22 -07001249 updateShouldCaptureThumbnails();
Michael Kolb14612442011-06-24 13:06:29 -07001250 }
1251
Michael Kolbc831b632011-05-11 09:30:34 -07001252 public long getId() {
1253 return mId;
1254 }
1255
Michael Kolb91911a22012-01-17 11:21:25 -08001256 void setWebView(WebView w) {
1257 setWebView(w, true);
1258 }
1259
Grace Kloba22ac16e2009-10-07 18:00:23 -07001260 /**
1261 * Sets the WebView for this tab, correctly removing the old WebView from
1262 * the container view.
1263 */
Michael Kolb91911a22012-01-17 11:21:25 -08001264 void setWebView(WebView w, boolean restore) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001265 if (mMainView == w) {
1266 return;
1267 }
Michael Kolba713ec82010-11-29 17:27:06 -08001268
Grace Kloba22ac16e2009-10-07 18:00:23 -07001269 // If the WebView is changing, the page will be reloaded, so any ongoing
1270 // Geolocation permission requests are void.
Grace Kloba50c241e2010-04-20 11:07:50 -07001271 if (mGeolocationPermissionsPrompt != null) {
1272 mGeolocationPermissionsPrompt.hide();
1273 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001274
Michael Kolba713ec82010-11-29 17:27:06 -08001275 mWebViewController.onSetWebView(this, w);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001276
John Reck1cf4b792011-07-26 10:22:22 -07001277 if (mMainView != null) {
John Reck8ee633f2011-08-09 16:00:35 -07001278 mMainView.setPictureListener(null);
John Reck1cf4b792011-07-26 10:22:22 -07001279 if (w != null) {
1280 syncCurrentState(w, null);
1281 } else {
1282 mCurrentState = new PageState(mContext, false);
1283 }
1284 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001285 // set the new one
1286 mMainView = w;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001287 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001288 if (mMainView != null) {
1289 mMainView.setWebViewClient(mWebViewClient);
1290 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001291 // Attach DownloadManager so that downloads can start in an active
1292 // or a non-active window. This can happen when going to a site that
1293 // does a redirect after a period of time. The user could have
1294 // switched to another tab while waiting for the download to start.
1295 mMainView.setDownloadListener(mDownloadListener);
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00001296 getWebViewClassic().setWebBackForwardListClient(mWebBackForwardListClient);
John Reck8ee633f2011-08-09 16:00:35 -07001297 TabControl tc = mWebViewController.getTabControl();
1298 if (tc != null && tc.getOnThumbnailUpdatedListener() != null) {
1299 mMainView.setPictureListener(this);
1300 }
Michael Kolb91911a22012-01-17 11:21:25 -08001301 if (restore && (mSavedState != null)) {
John Reck8b9bb8b2012-03-08 13:19:40 -08001302 restoreUserAgent();
John Reck6c2e2f32011-08-22 13:41:23 -07001303 WebBackForwardList restoredState
1304 = mMainView.restoreState(mSavedState);
1305 if (restoredState == null || restoredState.getSize() == 0) {
1306 Log.w(LOGTAG, "Failed to restore WebView state!");
1307 loadUrl(mCurrentState.mOriginalUrl, null);
1308 }
John Reck1cf4b792011-07-26 10:22:22 -07001309 mSavedState = null;
1310 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001311 }
1312 }
1313
1314 /**
1315 * Destroy the tab's main WebView and subWindow if any
1316 */
1317 void destroy() {
1318 if (mMainView != null) {
1319 dismissSubWindow();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001320 // save the WebView to call destroy() after detach it from the tab
1321 WebView webView = mMainView;
1322 setWebView(null);
1323 webView.destroy();
1324 }
1325 }
1326
1327 /**
1328 * Remove the tab from the parent
1329 */
1330 void removeFromTree() {
1331 // detach the children
Michael Kolbc831b632011-05-11 09:30:34 -07001332 if (mChildren != null) {
1333 for(Tab t : mChildren) {
1334 t.setParent(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001335 }
1336 }
1337 // remove itself from the parent list
Michael Kolbc831b632011-05-11 09:30:34 -07001338 if (mParent != null) {
1339 mParent.mChildren.remove(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001340 }
John Reck1cf4b792011-07-26 10:22:22 -07001341 deleteThumbnail();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001342 }
1343
1344 /**
1345 * Create a new subwindow unless a subwindow already exists.
1346 * @return True if a new subwindow was created. False if one already exists.
1347 */
1348 boolean createSubWindow() {
1349 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001350 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001351 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001352 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001353 mSubView.setWebChromeClient(new SubWindowChromeClient(
1354 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001355 // Set a different DownloadListener for the mSubView, since it will
1356 // just need to dismiss the mSubView, rather than close the Tab
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001357 mSubView.setDownloadListener(new BrowserDownloadListener() {
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001358 public void onDownloadStart(String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001359 String contentDisposition, String mimetype, String referer,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001360 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001361 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001362 contentDisposition, mimetype, referer, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001363 if (mSubView.copyBackForwardList().getSize() == 0) {
1364 // This subwindow was opened for the sole purpose of
1365 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001366 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001367 }
1368 }
1369 });
Michael Kolb14612442011-06-24 13:06:29 -07001370 mSubView.setOnCreateContextMenuListener(mWebViewController.getActivity());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001371 return true;
1372 }
1373 return false;
1374 }
1375
1376 /**
1377 * Dismiss the subWindow for the tab.
1378 */
1379 void dismissSubWindow() {
1380 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001381 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001382 mSubView.destroy();
1383 mSubView = null;
1384 mSubViewContainer = null;
1385 }
1386 }
1387
Grace Kloba22ac16e2009-10-07 18:00:23 -07001388
1389 /**
1390 * Set the parent tab of this tab.
1391 */
Michael Kolbc831b632011-05-11 09:30:34 -07001392 void setParent(Tab parent) {
John Reck52be4782011-08-26 15:37:29 -07001393 if (parent == this) {
1394 throw new IllegalStateException("Cannot set parent to self!");
1395 }
Michael Kolbc831b632011-05-11 09:30:34 -07001396 mParent = parent;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001397 // This tab may have been freed due to low memory. If that is the case,
Michael Kolbc831b632011-05-11 09:30:34 -07001398 // the parent tab id is already saved. If we are changing that id
Grace Kloba22ac16e2009-10-07 18:00:23 -07001399 // (most likely due to removing the parent tab) we must update the
Michael Kolbc831b632011-05-11 09:30:34 -07001400 // parent tab id in the saved Bundle.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001401 if (mSavedState != null) {
1402 if (parent == null) {
1403 mSavedState.remove(PARENTTAB);
1404 } else {
Michael Kolbc831b632011-05-11 09:30:34 -07001405 mSavedState.putLong(PARENTTAB, parent.getId());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001406 }
1407 }
John Reckb0a86db2011-05-24 14:05:58 -07001408
1409 // Sync the WebView useragent with the parent
1410 if (parent != null && mSettings.hasDesktopUseragent(parent.getWebView())
1411 != mSettings.hasDesktopUseragent(getWebView())) {
1412 mSettings.toggleDesktopUseragent(getWebView());
1413 }
John Reck52be4782011-08-26 15:37:29 -07001414
1415 if (parent != null && parent.getId() == getId()) {
1416 throw new IllegalStateException("Parent has same ID as child!");
1417 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001418 }
1419
1420 /**
Michael Kolbc831b632011-05-11 09:30:34 -07001421 * If this Tab was created through another Tab, then this method returns
1422 * that Tab.
1423 * @return the Tab parent or null
1424 */
1425 public Tab getParent() {
1426 return mParent;
1427 }
1428
1429 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001430 * When a Tab is created through the content of another Tab, then we
1431 * associate the Tabs.
1432 * @param child the Tab that was created from this Tab
1433 */
1434 void addChildTab(Tab child) {
Michael Kolbc831b632011-05-11 09:30:34 -07001435 if (mChildren == null) {
1436 mChildren = new Vector<Tab>();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001437 }
Michael Kolbc831b632011-05-11 09:30:34 -07001438 mChildren.add(child);
1439 child.setParent(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001440 }
1441
Michael Kolbc831b632011-05-11 09:30:34 -07001442 Vector<Tab> getChildren() {
1443 return mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001444 }
1445
1446 void resume() {
1447 if (mMainView != null) {
John Reck56c1fcf2011-08-17 10:15:16 -07001448 setupHwAcceleration(mMainView);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001449 mMainView.onResume();
1450 if (mSubView != null) {
1451 mSubView.onResume();
1452 }
1453 }
1454 }
1455
John Reck56c1fcf2011-08-17 10:15:16 -07001456 private void setupHwAcceleration(View web) {
1457 if (web == null) return;
1458 BrowserSettings settings = BrowserSettings.getInstance();
1459 if (settings.isHardwareAccelerated()) {
1460 web.setLayerType(View.LAYER_TYPE_NONE, null);
1461 } else {
1462 web.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
1463 }
1464 }
1465
Grace Kloba22ac16e2009-10-07 18:00:23 -07001466 void pause() {
1467 if (mMainView != null) {
1468 mMainView.onPause();
1469 if (mSubView != null) {
1470 mSubView.onPause();
1471 }
1472 }
1473 }
1474
1475 void putInForeground() {
John Reck8ee633f2011-08-09 16:00:35 -07001476 if (mInForeground) {
1477 return;
1478 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001479 mInForeground = true;
1480 resume();
Michael Kolb14612442011-06-24 13:06:29 -07001481 Activity activity = mWebViewController.getActivity();
1482 mMainView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001483 if (mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -07001484 mSubView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001485 }
1486 // Show the pending error dialog if the queue is not empty
1487 if (mQueuedErrors != null && mQueuedErrors.size() > 0) {
1488 showError(mQueuedErrors.getFirst());
1489 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001490 mWebViewController.bookmarkedStatusHasChanged(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001491 }
1492
1493 void putInBackground() {
John Reck8ee633f2011-08-09 16:00:35 -07001494 if (!mInForeground) {
1495 return;
1496 }
John Reck52be4782011-08-26 15:37:29 -07001497 capture();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001498 mInForeground = false;
1499 pause();
1500 mMainView.setOnCreateContextMenuListener(null);
1501 if (mSubView != null) {
1502 mSubView.setOnCreateContextMenuListener(null);
1503 }
1504 }
1505
Michael Kolb8233fac2010-10-26 16:08:53 -07001506 boolean inForeground() {
1507 return mInForeground;
1508 }
1509
Grace Kloba22ac16e2009-10-07 18:00:23 -07001510 /**
1511 * Return the top window of this tab; either the subwindow if it is not
1512 * null or the main window.
1513 * @return The top window of this tab.
1514 */
1515 WebView getTopWindow() {
1516 if (mSubView != null) {
1517 return mSubView;
1518 }
1519 return mMainView;
1520 }
1521
1522 /**
1523 * Return the main window of this tab. Note: if a tab is freed in the
1524 * background, this can return null. It is only guaranteed to be
1525 * non-null for the current tab.
1526 * @return The main WebView of this tab.
1527 */
1528 WebView getWebView() {
1529 return mMainView;
1530 }
1531
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00001532 /**
1533 * Return the underlying WebViewClassic implementation. As with getWebView,
1534 * this maybe null for background tabs.
1535 * @return The main WebView of this tab.
1536 */
1537 WebViewClassic getWebViewClassic() {
1538 return WebViewClassic.fromWebView(mMainView);
1539 }
1540
Michael Kolba713ec82010-11-29 17:27:06 -08001541 void setViewContainer(View container) {
1542 mContainer = container;
1543 }
1544
Michael Kolb8233fac2010-10-26 16:08:53 -07001545 View getViewContainer() {
1546 return mContainer;
1547 }
1548
Grace Kloba22ac16e2009-10-07 18:00:23 -07001549 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001550 * Return whether private browsing is enabled for the main window of
1551 * this tab.
1552 * @return True if private browsing is enabled.
1553 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001554 boolean isPrivateBrowsingEnabled() {
John Reck502a3532011-08-16 14:21:46 -07001555 return mCurrentState.mIncognito;
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001556 }
1557
1558 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001559 * Return the subwindow of this tab or null if there is no subwindow.
1560 * @return The subwindow of this tab or null.
1561 */
1562 WebView getSubWebView() {
1563 return mSubView;
1564 }
1565
Michael Kolb1514bb72010-11-22 09:11:48 -08001566 void setSubWebView(WebView subView) {
1567 mSubView = subView;
1568 }
1569
Michael Kolb8233fac2010-10-26 16:08:53 -07001570 View getSubViewContainer() {
1571 return mSubViewContainer;
1572 }
1573
Michael Kolb1514bb72010-11-22 09:11:48 -08001574 void setSubViewContainer(View subViewContainer) {
1575 mSubViewContainer = subViewContainer;
1576 }
1577
Grace Kloba22ac16e2009-10-07 18:00:23 -07001578 /**
1579 * @return The geolocation permissions prompt for this tab.
1580 */
1581 GeolocationPermissionsPrompt getGeolocationPermissionsPrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001582 if (mGeolocationPermissionsPrompt == null) {
1583 ViewStub stub = (ViewStub) mContainer
1584 .findViewById(R.id.geolocation_permissions_prompt);
1585 mGeolocationPermissionsPrompt = (GeolocationPermissionsPrompt) stub
1586 .inflate();
Grace Kloba50c241e2010-04-20 11:07:50 -07001587 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001588 return mGeolocationPermissionsPrompt;
1589 }
1590
1591 /**
1592 * @return The application id string
1593 */
1594 String getAppId() {
1595 return mAppId;
1596 }
1597
1598 /**
1599 * Set the application id string
1600 * @param id
1601 */
1602 void setAppId(String id) {
1603 mAppId = id;
1604 }
1605
Michael Kolbe28b3472011-08-04 16:54:31 -07001606 boolean closeOnBack() {
1607 return mCloseOnBack;
1608 }
1609
1610 void setCloseOnBack(boolean close) {
1611 mCloseOnBack = close;
1612 }
1613
Grace Kloba22ac16e2009-10-07 18:00:23 -07001614 String getUrl() {
John Reck324d4402011-01-11 16:56:42 -08001615 return UrlUtils.filteredUrl(mCurrentState.mUrl);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001616 }
1617
John Reck49a603c2011-03-03 09:33:05 -08001618 String getOriginalUrl() {
John Reckdb22ec42011-06-29 11:31:24 -07001619 if (mCurrentState.mOriginalUrl == null) {
1620 return getUrl();
John Reck49a603c2011-03-03 09:33:05 -08001621 }
John Reckdb22ec42011-06-29 11:31:24 -07001622 return UrlUtils.filteredUrl(mCurrentState.mOriginalUrl);
John Reck49a603c2011-03-03 09:33:05 -08001623 }
1624
Grace Kloba22ac16e2009-10-07 18:00:23 -07001625 /**
John Reck30c714c2010-12-16 17:30:34 -08001626 * Get the title of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001627 */
1628 String getTitle() {
John Reck30c714c2010-12-16 17:30:34 -08001629 if (mCurrentState.mTitle == null && mInPageLoad) {
Michael Kolb14612442011-06-24 13:06:29 -07001630 return mContext.getString(R.string.title_bar_loading);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001631 }
John Reck30c714c2010-12-16 17:30:34 -08001632 return mCurrentState.mTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001633 }
1634
1635 /**
John Reck30c714c2010-12-16 17:30:34 -08001636 * Get the favicon of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001637 */
1638 Bitmap getFavicon() {
John Reck1cf4b792011-07-26 10:22:22 -07001639 if (mCurrentState.mFavicon != null) {
1640 return mCurrentState.mFavicon;
1641 }
1642 return getDefaultFavicon(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001643 }
1644
John Recke969cc52010-12-21 17:24:43 -08001645 public boolean isBookmarkedSite() {
1646 return mCurrentState.mIsBookmarkedSite;
1647 }
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001648
Grace Kloba22ac16e2009-10-07 18:00:23 -07001649 /**
1650 * Return the tab's error console. Creates the console if createIfNEcessary
1651 * is true and we haven't already created the console.
1652 * @param createIfNecessary Flag to indicate if the console should be
1653 * created if it has not been already.
1654 * @return The tab's error console, or null if one has not been created and
1655 * createIfNecessary is false.
1656 */
1657 ErrorConsoleView getErrorConsole(boolean createIfNecessary) {
1658 if (createIfNecessary && mErrorConsole == null) {
Michael Kolb14612442011-06-24 13:06:29 -07001659 mErrorConsole = new ErrorConsoleView(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001660 mErrorConsole.setWebView(mMainView);
1661 }
1662 return mErrorConsole;
1663 }
1664
Steve Block08a6f0c2011-10-06 12:12:53 +01001665 /**
1666 * Sets the security state, clears the SSL certificate error and informs
1667 * the controller.
1668 */
Steve Block2466eff2011-10-03 15:33:09 +01001669 private void setSecurityState(SecurityState securityState) {
1670 mCurrentState.mSecurityState = securityState;
Steve Block08a6f0c2011-10-06 12:12:53 +01001671 mCurrentState.mSslCertificateError = null;
Steve Block2466eff2011-10-03 15:33:09 +01001672 mWebViewController.onUpdatedSecurityState(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001673 }
1674
1675 /**
Steve Block2466eff2011-10-03 15:33:09 +01001676 * @return The tab's security state.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001677 */
Steve Block2466eff2011-10-03 15:33:09 +01001678 SecurityState getSecurityState() {
1679 return mCurrentState.mSecurityState;
John Reck30c714c2010-12-16 17:30:34 -08001680 }
1681
Steve Block08a6f0c2011-10-06 12:12:53 +01001682 /**
1683 * Gets the SSL certificate error, if any, for the page's main resource.
1684 * This is only non-null when the security state is
1685 * SECURITY_STATE_BAD_CERTIFICATE.
1686 */
1687 SslError getSslCertificateError() {
1688 return mCurrentState.mSslCertificateError;
1689 }
1690
John Reck30c714c2010-12-16 17:30:34 -08001691 int getLoadProgress() {
1692 if (mInPageLoad) {
1693 return mPageLoadProgress;
1694 }
1695 return 100;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001696 }
1697
1698 /**
1699 * @return TRUE if onPageStarted is called while onPageFinished is not
1700 * called yet.
1701 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001702 boolean inPageLoad() {
1703 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001704 }
1705
Grace Kloba22ac16e2009-10-07 18:00:23 -07001706 /**
John Reck1cf4b792011-07-26 10:22:22 -07001707 * @return The Bundle with the tab's state if it can be saved, otherwise null
Grace Kloba22ac16e2009-10-07 18:00:23 -07001708 */
John Reck1cf4b792011-07-26 10:22:22 -07001709 public Bundle saveState() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001710 // If the WebView is null it means we ran low on memory and we already
1711 // stored the saved state in mSavedState.
1712 if (mMainView == null) {
John Reck1cf4b792011-07-26 10:22:22 -07001713 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001714 }
John Reck6c2e2f32011-08-22 13:41:23 -07001715
1716 if (TextUtils.isEmpty(mCurrentState.mUrl)) {
John Reck1cf4b792011-07-26 10:22:22 -07001717 return null;
John Reck24f18262011-06-17 14:47:20 -07001718 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001719
1720 mSavedState = new Bundle();
John Reck6c2e2f32011-08-22 13:41:23 -07001721 WebBackForwardList savedList = mMainView.saveState(mSavedState);
1722 if (savedList == null || savedList.getSize() == 0) {
1723 Log.w(LOGTAG, "Failed to save back/forward list for "
1724 + mCurrentState.mUrl);
1725 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001726
Michael Kolbc831b632011-05-11 09:30:34 -07001727 mSavedState.putLong(ID, mId);
John Reck30c714c2010-12-16 17:30:34 -08001728 mSavedState.putString(CURRURL, mCurrentState.mUrl);
1729 mSavedState.putString(CURRTITLE, mCurrentState.mTitle);
John Reck1cf4b792011-07-26 10:22:22 -07001730 mSavedState.putBoolean(INCOGNITO, mMainView.isPrivateBrowsingEnabled());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001731 if (mAppId != null) {
1732 mSavedState.putString(APPID, mAppId);
1733 }
Michael Kolbe28b3472011-08-04 16:54:31 -07001734 mSavedState.putBoolean(CLOSEFLAG, mCloseOnBack);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001735 // Remember the parent tab so the relationship can be restored.
Michael Kolbc831b632011-05-11 09:30:34 -07001736 if (mParent != null) {
1737 mSavedState.putLong(PARENTTAB, mParent.mId);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001738 }
John Reckb0a86db2011-05-24 14:05:58 -07001739 mSavedState.putBoolean(USERAGENT,
1740 mSettings.hasDesktopUseragent(getWebView()));
John Reck1cf4b792011-07-26 10:22:22 -07001741 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001742 }
1743
1744 /*
1745 * Restore the state of the tab.
1746 */
John Reck1cf4b792011-07-26 10:22:22 -07001747 private void restoreState(Bundle b) {
1748 mSavedState = b;
1749 if (mSavedState == null) {
1750 return;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001751 }
1752 // Restore the internal state even if the WebView fails to restore.
1753 // This will maintain the app id, original url and close-on-exit values.
Michael Kolbc831b632011-05-11 09:30:34 -07001754 mId = b.getLong(ID);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001755 mAppId = b.getString(APPID);
Michael Kolbe28b3472011-08-04 16:54:31 -07001756 mCloseOnBack = b.getBoolean(CLOSEFLAG);
John Reck8b9bb8b2012-03-08 13:19:40 -08001757 restoreUserAgent();
John Reck1cf4b792011-07-26 10:22:22 -07001758 String url = b.getString(CURRURL);
1759 String title = b.getString(CURRTITLE);
1760 boolean incognito = b.getBoolean(INCOGNITO);
1761 mCurrentState = new PageState(mContext, incognito, url, null);
1762 mCurrentState.mTitle = title;
1763 synchronized (Tab.this) {
1764 if (mCapture != null) {
John Reck4eadc342011-10-31 14:04:10 -07001765 DataController.getInstance(mContext).loadThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07001766 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001767 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001768 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04001769
John Reck8b9bb8b2012-03-08 13:19:40 -08001770 private void restoreUserAgent() {
1771 if (mMainView == null || mSavedState == null) {
1772 return;
1773 }
1774 if (mSavedState.getBoolean(USERAGENT)
1775 != mSettings.hasDesktopUseragent(mMainView)) {
1776 mSettings.toggleDesktopUseragent(mMainView);
1777 }
1778 }
1779
Leon Scroggins1961ed22010-12-07 15:22:21 -05001780 public void updateBookmarkedStatus() {
John Recke969cc52010-12-21 17:24:43 -08001781 mDataController.queryBookmarkStatus(getUrl(), mIsBookmarkCallback);
Leon Scroggins1961ed22010-12-07 15:22:21 -05001782 }
1783
John Recke969cc52010-12-21 17:24:43 -08001784 private DataController.OnQueryUrlIsBookmark mIsBookmarkCallback
1785 = new DataController.OnQueryUrlIsBookmark() {
1786 @Override
1787 public void onQueryUrlIsBookmark(String url, boolean isBookmark) {
1788 if (mCurrentState.mUrl.equals(url)) {
1789 mCurrentState.mIsBookmarkedSite = isBookmark;
1790 mWebViewController.bookmarkedStatusHasChanged(Tab.this);
1791 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001792 }
John Recke969cc52010-12-21 17:24:43 -08001793 };
Michael Kolb1acef692011-03-08 14:12:06 -08001794
Michael Kolbeb95db42011-03-03 10:38:40 -08001795 public Bitmap getScreenshot() {
John Reck1cf4b792011-07-26 10:22:22 -07001796 synchronized (Tab.this) {
1797 return mCapture;
1798 }
Michael Kolbeb95db42011-03-03 10:38:40 -08001799 }
1800
John Reck541f55a2011-06-07 16:34:43 -07001801 public boolean isSnapshot() {
John Reck541f55a2011-06-07 16:34:43 -07001802 return false;
1803 }
1804
John Reck68234a92012-04-19 15:27:12 -07001805 private static class SaveCallback implements ValueCallback<Boolean> {
1806 boolean mResult;
1807
1808 @Override
1809 public void onReceiveValue(Boolean value) {
1810 mResult = value;
1811 synchronized (this) {
1812 notifyAll();
John Reck8cc92352011-07-06 17:41:52 -07001813 }
John Reck541f55a2011-06-07 16:34:43 -07001814 }
John Reck68234a92012-04-19 15:27:12 -07001815
1816 }
1817
1818 /**
1819 * Must be called on the UI thread
1820 */
1821 public ContentValues createSnapshotValues() {
1822 WebViewClassic web = getWebViewClassic();
1823 if (web == null) return null;
John Reckd8c74522011-06-14 08:45:00 -07001824 ContentValues values = new ContentValues();
1825 values.put(Snapshots.TITLE, mCurrentState.mTitle);
1826 values.put(Snapshots.URL, mCurrentState.mUrl);
John Reck68234a92012-04-19 15:27:12 -07001827 values.put(Snapshots.BACKGROUND, web.getPageBackgroundColor());
John Reck8cc92352011-07-06 17:41:52 -07001828 values.put(Snapshots.DATE_CREATED, System.currentTimeMillis());
1829 values.put(Snapshots.FAVICON, compressBitmap(getFavicon()));
1830 Bitmap screenshot = Controller.createScreenshot(mMainView,
1831 Controller.getDesiredThumbnailWidth(mContext),
1832 Controller.getDesiredThumbnailHeight(mContext));
1833 values.put(Snapshots.THUMBNAIL, compressBitmap(screenshot));
John Reckd8c74522011-06-14 08:45:00 -07001834 return values;
John Reck541f55a2011-06-07 16:34:43 -07001835 }
1836
John Reck68234a92012-04-19 15:27:12 -07001837 /**
1838 * Probably want to call this on a background thread
1839 */
1840 public boolean saveViewState(ContentValues values) {
1841 WebViewClassic web = getWebViewClassic();
1842 if (web == null) return false;
1843 String path = UUID.randomUUID().toString();
1844 SaveCallback callback = new SaveCallback();
1845 OutputStream outs = null;
1846 try {
1847 outs = mContext.openFileOutput(path, Context.MODE_PRIVATE);
1848 GZIPOutputStream stream = new GZIPOutputStream(outs);
1849 synchronized (callback) {
1850 web.saveViewState(stream, callback);
1851 callback.wait();
1852 }
1853 stream.flush();
1854 stream.close();
1855 } catch (Exception e) {
1856 Log.w(LOGTAG, "Failed to save view state", e);
1857 if (outs != null) {
1858 try {
1859 outs.close();
1860 } catch (IOException ignore) {}
1861 }
1862 File file = mContext.getFileStreamPath(path);
1863 if (file.exists() && !file.delete()) {
1864 file.deleteOnExit();
1865 }
1866 return false;
1867 }
1868 File savedFile = mContext.getFileStreamPath(path);
1869 if (!callback.mResult) {
1870 if (!savedFile.delete()) {
1871 savedFile.deleteOnExit();
1872 }
1873 return false;
1874 }
1875 long size = savedFile.length();
1876 values.put(Snapshots.VIEWSTATE_PATH, path);
1877 values.put(Snapshots.VIEWSTATE_SIZE, size);
1878 return true;
1879 }
1880
John Reck8cc92352011-07-06 17:41:52 -07001881 public byte[] compressBitmap(Bitmap bitmap) {
1882 if (bitmap == null) {
1883 return null;
1884 }
1885 ByteArrayOutputStream stream = new ByteArrayOutputStream();
1886 bitmap.compress(CompressFormat.PNG, 100, stream);
1887 return stream.toByteArray();
1888 }
1889
John Reck26b18322011-06-21 13:08:58 -07001890 public void loadUrl(String url, Map<String, String> headers) {
1891 if (mMainView != null) {
Michael Kolba53c9892011-10-05 13:31:40 -07001892 mPageLoadProgress = INITIAL_PROGRESS;
1893 mInPageLoad = true;
Michael Kolb14612442011-06-24 13:06:29 -07001894 mCurrentState = new PageState(mContext, false, url, null);
John Reck26b18322011-06-21 13:08:58 -07001895 mWebViewController.onPageStarted(this, mMainView, null);
1896 mMainView.loadUrl(url, headers);
1897 }
1898 }
1899
John Reck38b39652012-06-05 09:22:59 -07001900 public void disableUrlOverridingForLoad() {
1901 mDisableOverrideUrlLoading = true;
1902 }
1903
Michael Kolb9ef259a2011-07-12 15:33:08 -07001904 protected void capture() {
1905 if (mMainView == null || mCapture == null) return;
John Reck4eadc342011-10-31 14:04:10 -07001906 if (mMainView.getContentWidth() <= 0 || mMainView.getContentHeight() <= 0) {
1907 return;
1908 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001909 Canvas c = new Canvas(mCapture);
1910 final int left = mMainView.getScrollX();
1911 final int top = mMainView.getScrollY() + mMainView.getVisibleTitleHeight();
Michael Kolba3194d02011-09-07 11:23:51 -07001912 int state = c.save();
Michael Kolb9ef259a2011-07-12 15:33:08 -07001913 c.translate(-left, -top);
1914 float scale = mCaptureWidth / (float) mMainView.getWidth();
1915 c.scale(scale, scale, left, top);
John Reck8ee633f2011-08-09 16:00:35 -07001916 if (mMainView instanceof BrowserWebView) {
1917 ((BrowserWebView)mMainView).drawContent(c);
1918 } else {
1919 mMainView.draw(c);
1920 }
Michael Kolba3194d02011-09-07 11:23:51 -07001921 c.restoreToCount(state);
1922 // manually anti-alias the edges for the tilt
1923 c.drawRect(0, 0, 1, mCapture.getHeight(), sAlphaPaint);
1924 c.drawRect(mCapture.getWidth() - 1, 0, mCapture.getWidth(),
1925 mCapture.getHeight(), sAlphaPaint);
1926 c.drawRect(0, 0, mCapture.getWidth(), 1, sAlphaPaint);
1927 c.drawRect(0, mCapture.getHeight() - 1, mCapture.getWidth(),
1928 mCapture.getHeight(), sAlphaPaint);
Dianne Hackborn43cfe8a2011-08-02 16:59:35 -07001929 c.setBitmap(null);
John Reck8ee633f2011-08-09 16:00:35 -07001930 mHandler.removeMessages(MSG_CAPTURE);
John Reck1cf4b792011-07-26 10:22:22 -07001931 persistThumbnail();
John Reck8ee633f2011-08-09 16:00:35 -07001932 TabControl tc = mWebViewController.getTabControl();
1933 if (tc != null) {
1934 OnThumbnailUpdatedListener updateListener
1935 = tc.getOnThumbnailUpdatedListener();
1936 if (updateListener != null) {
1937 updateListener.onThumbnailUpdated(this);
1938 }
1939 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001940 }
1941
1942 @Override
1943 public void onNewPicture(WebView view, Picture picture) {
John Reck1cf4b792011-07-26 10:22:22 -07001944 postCapture();
1945 }
1946
1947 private void postCapture() {
Michael Kolb9ef259a2011-07-12 15:33:08 -07001948 if (!mHandler.hasMessages(MSG_CAPTURE)) {
1949 mHandler.sendEmptyMessageDelayed(MSG_CAPTURE, CAPTURE_DELAY);
1950 }
1951 }
1952
John Reckef654f12011-07-12 16:42:08 -07001953 public boolean canGoBack() {
1954 return mMainView != null ? mMainView.canGoBack() : false;
1955 }
1956
1957 public boolean canGoForward() {
1958 return mMainView != null ? mMainView.canGoForward() : false;
1959 }
1960
1961 public void goBack() {
1962 if (mMainView != null) {
1963 mMainView.goBack();
1964 }
1965 }
1966
1967 public void goForward() {
1968 if (mMainView != null) {
1969 mMainView.goForward();
1970 }
1971 }
1972
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001973 /**
1974 * Causes the tab back/forward stack to be cleared once, if the given URL is the next URL
1975 * to be added to the stack.
1976 *
1977 * This is used to ensure that preloaded URLs that are not subsequently seen by the user do
1978 * not appear in the back stack.
1979 */
Mathew Inwooda829d552011-09-02 14:16:25 +01001980 public void clearBackStackWhenItemAdded(Pattern urlPattern) {
1981 mClearHistoryUrlPattern = urlPattern;
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001982 }
1983
John Reck1cf4b792011-07-26 10:22:22 -07001984 protected void persistThumbnail() {
John Reck4eadc342011-10-31 14:04:10 -07001985 DataController.getInstance(mContext).saveThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07001986 }
1987
1988 protected void deleteThumbnail() {
John Reck4eadc342011-10-31 14:04:10 -07001989 DataController.getInstance(mContext).deleteThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07001990 }
1991
John Reck4eadc342011-10-31 14:04:10 -07001992 void updateCaptureFromBlob(byte[] blob) {
John Reck1cf4b792011-07-26 10:22:22 -07001993 synchronized (Tab.this) {
1994 if (mCapture == null) {
1995 return;
1996 }
Michael Kolbbd4c00a2011-08-02 11:27:12 -07001997 ByteBuffer buffer = ByteBuffer.wrap(blob);
Michael Kolbd837a112011-08-09 14:00:18 -07001998 try {
1999 mCapture.copyPixelsFromBuffer(buffer);
2000 } catch (RuntimeException rex) {
2001 Log.e(LOGTAG, "Load capture has mismatched sizes; buffer: "
2002 + buffer.capacity() + " blob: " + blob.length
2003 + "capture: " + mCapture.getByteCount());
2004 throw rex;
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002005 }
John Reck1cf4b792011-07-26 10:22:22 -07002006 }
2007 }
2008
John Reck52be4782011-08-26 15:37:29 -07002009 @Override
2010 public String toString() {
2011 StringBuilder builder = new StringBuilder(100);
2012 builder.append(mId);
2013 builder.append(") has parent: ");
2014 if (getParent() != null) {
2015 builder.append("true[");
2016 builder.append(getParent().getId());
2017 builder.append("]");
2018 } else {
2019 builder.append("false");
2020 }
2021 builder.append(", incog: ");
2022 builder.append(isPrivateBrowsingEnabled());
2023 if (!isPrivateBrowsingEnabled()) {
2024 builder.append(", title: ");
2025 builder.append(getTitle());
2026 builder.append(", url: ");
2027 builder.append(getUrl());
2028 }
2029 return builder.toString();
2030 }
2031
Steve Block4895b012011-10-03 16:26:46 +01002032 private void handleProceededAfterSslError(SslError error) {
2033 if (error.getUrl().equals(mCurrentState.mUrl)) {
2034 // The security state should currently be SECURITY_STATE_SECURE.
2035 setSecurityState(SecurityState.SECURITY_STATE_BAD_CERTIFICATE);
Steve Block08a6f0c2011-10-06 12:12:53 +01002036 mCurrentState.mSslCertificateError = error;
Steve Block4895b012011-10-03 16:26:46 +01002037 } else if (getSecurityState() == SecurityState.SECURITY_STATE_SECURE) {
Steve Block08a6f0c2011-10-06 12:12:53 +01002038 // The page's main resource is secure and this error is for a
2039 // sub-resource.
Steve Block4895b012011-10-03 16:26:46 +01002040 setSecurityState(SecurityState.SECURITY_STATE_MIXED);
2041 }
2042 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002043}