blob: 620028600258e117b1abc9878f49b4772aadff68 [file] [log] [blame]
Grace Kloba22ac16e2009-10-07 18:00:23 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.browser;
18
Michael Kolb8233fac2010-10-26 16:08:53 -070019import android.app.Activity;
Grace Kloba22ac16e2009-10-07 18:00:23 -070020import android.app.AlertDialog;
21import android.content.ContentResolver;
John Reckd8c74522011-06-14 08:45:00 -070022import android.content.ContentValues;
John Reck30c714c2010-12-16 17:30:34 -080023import android.content.Context;
Grace Kloba22ac16e2009-10-07 18:00:23 -070024import android.content.DialogInterface;
Michael Kolbfe251992010-07-08 15:41:55 -070025import android.content.DialogInterface.OnCancelListener;
Grace Kloba22ac16e2009-10-07 18:00:23 -070026import android.graphics.Bitmap;
John Reck8cc92352011-07-06 17:41:52 -070027import android.graphics.Bitmap.CompressFormat;
Michael Kolb9ef259a2011-07-12 15:33:08 -070028import android.graphics.BitmapFactory;
29import android.graphics.Canvas;
Michael Kolbc3af0672011-08-09 10:24:41 -070030import android.graphics.Color;
Michael Kolba3194d02011-09-07 11:23:51 -070031import android.graphics.Paint;
Michael Kolb9ef259a2011-07-12 15:33:08 -070032import android.graphics.Picture;
Michael Kolba3194d02011-09-07 11:23:51 -070033import android.graphics.PorterDuff;
34import android.graphics.PorterDuffXfermode;
Grace Kloba22ac16e2009-10-07 18:00:23 -070035import android.net.Uri;
36import android.net.http.SslError;
Grace Kloba22ac16e2009-10-07 18:00:23 -070037import android.os.Bundle;
Michael Kolb9ef259a2011-07-12 15:33:08 -070038import android.os.Handler;
Grace Kloba22ac16e2009-10-07 18:00:23 -070039import android.os.Message;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +000040import android.os.SystemClock;
Brian Carlstrom8862c1d2011-06-02 01:05:55 -070041import android.security.KeyChain;
Brian Carlstromaa09cd82011-06-09 16:04:40 -070042import android.security.KeyChainAliasCallback;
John Reck24f18262011-06-17 14:47:20 -070043import android.text.TextUtils;
Grace Kloba22ac16e2009-10-07 18:00:23 -070044import android.util.Log;
45import android.view.KeyEvent;
46import android.view.LayoutInflater;
47import android.view.View;
Grace Kloba50c241e2010-04-20 11:07:50 -070048import android.view.ViewStub;
Brian Carlstrom8862c1d2011-06-02 01:05:55 -070049import android.webkit.ClientCertRequestHandler;
Ben Murdochc42addf2010-01-28 15:19:59 +000050import android.webkit.ConsoleMessage;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -050051import android.webkit.DownloadListener;
Grace Kloba22ac16e2009-10-07 18:00:23 -070052import android.webkit.GeolocationPermissions;
53import android.webkit.HttpAuthHandler;
54import android.webkit.SslErrorHandler;
55import android.webkit.URLUtil;
56import android.webkit.ValueCallback;
John Reck6c2e2f32011-08-22 13:41:23 -070057import android.webkit.WebBackForwardList;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -050058import android.webkit.WebBackForwardListClient;
Grace Kloba22ac16e2009-10-07 18:00:23 -070059import android.webkit.WebChromeClient;
60import android.webkit.WebHistoryItem;
John Reck438bf462011-01-12 18:11:46 -080061import android.webkit.WebResourceResponse;
Grace Kloba22ac16e2009-10-07 18:00:23 -070062import android.webkit.WebStorage;
63import android.webkit.WebView;
Michael Kolb9ef259a2011-07-12 15:33:08 -070064import android.webkit.WebView.PictureListener;
John Reck2b71d6d2012-04-18 17:42:06 -070065import android.webkit.WebViewClassic;
Grace Kloba22ac16e2009-10-07 18:00:23 -070066import android.webkit.WebViewClient;
Ben Murdoch1d676b62011-01-17 12:54:24 +000067import android.widget.CheckBox;
Ben Murdoch8029a772010-11-16 11:58:21 +000068import android.widget.Toast;
Grace Kloba22ac16e2009-10-07 18:00:23 -070069
John Reck8ee633f2011-08-09 16:00:35 -070070import com.android.browser.TabControl.OnThumbnailUpdatedListener;
John Reck541f55a2011-06-07 16:34:43 -070071import com.android.browser.homepages.HomeProvider;
John Reck8cc92352011-07-06 17:41:52 -070072import com.android.browser.provider.SnapshotProvider.Snapshots;
John Reck541f55a2011-06-07 16:34:43 -070073
74import java.io.ByteArrayOutputStream;
John Reck2b71d6d2012-04-18 17:42:06 -070075import java.io.File;
John Reck68234a92012-04-19 15:27:12 -070076import java.io.IOException;
John Reck2b71d6d2012-04-18 17:42:06 -070077import java.io.OutputStream;
John Reck1cf4b792011-07-26 10:22:22 -070078import java.nio.ByteBuffer;
Michael Kolbfe251992010-07-08 15:41:55 -070079import java.util.LinkedList;
80import java.util.Map;
John Reck2b71d6d2012-04-18 17:42:06 -070081import java.util.UUID;
Michael Kolbfe251992010-07-08 15:41:55 -070082import java.util.Vector;
Mathew Inwooda829d552011-09-02 14:16:25 +010083import java.util.regex.Pattern;
John Reck8cc92352011-07-06 17:41:52 -070084import java.util.zip.GZIPOutputStream;
Michael Kolbfe251992010-07-08 15:41:55 -070085
Grace Kloba22ac16e2009-10-07 18:00:23 -070086/**
87 * Class for maintaining Tabs with a main WebView and a subwindow.
88 */
Michael Kolb9ef259a2011-07-12 15:33:08 -070089class Tab implements PictureListener {
Michael Kolb8233fac2010-10-26 16:08:53 -070090
Grace Kloba22ac16e2009-10-07 18:00:23 -070091 // Log Tag
92 private static final String LOGTAG = "Tab";
Mathew Inwood1dd8e822011-08-03 14:34:29 +010093 private static final boolean LOGD_ENABLED = com.android.browser.Browser.LOGD_ENABLED;
Ben Murdochc42addf2010-01-28 15:19:59 +000094 // Special case the logtag for messages for the Console to make it easier to
95 // filter them and match the logtag used for these messages in older versions
96 // of the browser.
97 private static final String CONSOLE_LOGTAG = "browser";
98
Michael Kolb9ef259a2011-07-12 15:33:08 -070099 private static final int MSG_CAPTURE = 42;
John Reck8ee633f2011-08-09 16:00:35 -0700100 private static final int CAPTURE_DELAY = 100;
Michael Kolba53c9892011-10-05 13:31:40 -0700101 private static final int INITIAL_PROGRESS = 5;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700102
John Reck1cf4b792011-07-26 10:22:22 -0700103 private static Bitmap sDefaultFavicon;
104
Michael Kolba3194d02011-09-07 11:23:51 -0700105 private static Paint sAlphaPaint = new Paint();
106 static {
107 sAlphaPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
108 sAlphaPaint.setColor(Color.TRANSPARENT);
109 }
110
Steve Block2466eff2011-10-03 15:33:09 +0100111 public enum SecurityState {
Steve Block4895b012011-10-03 16:26:46 +0100112 // The page's main resource does not use SSL. Note that we use this
113 // state irrespective of the SSL authentication state of sub-resources.
Steve Block2466eff2011-10-03 15:33:09 +0100114 SECURITY_STATE_NOT_SECURE,
Steve Block4895b012011-10-03 16:26:46 +0100115 // The page's main resource uses SSL and the certificate is good. The
116 // same is true of all sub-resources.
Steve Block2466eff2011-10-03 15:33:09 +0100117 SECURITY_STATE_SECURE,
Steve Block4895b012011-10-03 16:26:46 +0100118 // The page's main resource uses SSL and the certificate is good, but
119 // some sub-resources either do not use SSL or have problems with their
120 // certificates.
Steve Block2466eff2011-10-03 15:33:09 +0100121 SECURITY_STATE_MIXED,
Steve Block4895b012011-10-03 16:26:46 +0100122 // The page's main resource uses SSL but there is a problem with its
123 // certificate.
124 SECURITY_STATE_BAD_CERTIFICATE,
John Reck30c714c2010-12-16 17:30:34 -0800125 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700126
Michael Kolb14612442011-06-24 13:06:29 -0700127 Context mContext;
John Reckd8c74522011-06-14 08:45:00 -0700128 protected WebViewController mWebViewController;
Michael Kolb8233fac2010-10-26 16:08:53 -0700129
Michael Kolbc831b632011-05-11 09:30:34 -0700130 // The tab ID
John Reckd8c74522011-06-14 08:45:00 -0700131 private long mId = -1;
Michael Kolbc831b632011-05-11 09:30:34 -0700132
Grace Kloba22ac16e2009-10-07 18:00:23 -0700133 // The Geolocation permissions prompt
134 private GeolocationPermissionsPrompt mGeolocationPermissionsPrompt;
135 // Main WebView wrapper
Michael Kolba713ec82010-11-29 17:27:06 -0800136 private View mContainer;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700137 // Main WebView
138 private WebView mMainView;
139 // Subwindow container
140 private View mSubViewContainer;
141 // Subwindow WebView
142 private WebView mSubView;
143 // Saved bundle for when we are running low on memory. It contains the
144 // information needed to restore the WebView if the user goes back to the
145 // tab.
146 private Bundle mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700147 // Parent Tab. This is the Tab that created this Tab, or null if the Tab was
148 // created by the UI
Michael Kolbc831b632011-05-11 09:30:34 -0700149 private Tab mParent;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700150 // Tab that constructed by this Tab. This is used when this Tab is
151 // destroyed, it clears all mParentTab values in the children.
Michael Kolbc831b632011-05-11 09:30:34 -0700152 private Vector<Tab> mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700153 // If true, the tab is in the foreground of the current activity.
154 private boolean mInForeground;
Michael Kolb8233fac2010-10-26 16:08:53 -0700155 // If true, the tab is in page loading state (after onPageStarted,
156 // before onPageFinsihed)
157 private boolean mInPageLoad;
John Reck30c714c2010-12-16 17:30:34 -0800158 // The last reported progress of the current page
159 private int mPageLoadProgress;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000160 // The time the load started, used to find load page time
161 private long mLoadStartTime;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700162 // Application identifier used to find tabs that another application wants
163 // to reuse.
164 private String mAppId;
Michael Kolbe28b3472011-08-04 16:54:31 -0700165 // flag to indicate if tab should be closed on back
166 private boolean mCloseOnBack;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700167 // Keep the original url around to avoid killing the old WebView if the url
168 // has not changed.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700169 // Error console for the tab
170 private ErrorConsoleView mErrorConsole;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -0500171 // The listener that gets invoked when a download is started from the
172 // mMainView
173 private final DownloadListener mDownloadListener;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500174 // Listener used to know when we move forward or back in the history list.
175 private final WebBackForwardListClient mWebBackForwardListClient;
John Recke969cc52010-12-21 17:24:43 -0800176 private DataController mDataController;
Patrick Scott92066772011-03-10 08:46:27 -0500177 // State of the auto-login request.
178 private DeviceAccountLogin mDeviceAccountLogin;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700179
180 // AsyncTask for downloading touch icons
181 DownloadTouchIcon mTouchIconLoader;
182
John Reck35e9dd62011-04-25 09:01:54 -0700183 private BrowserSettings mSettings;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700184 private int mCaptureWidth;
185 private int mCaptureHeight;
186 private Bitmap mCapture;
187 private Handler mHandler;
Michael Kolb72864272012-05-03 15:42:15 -0700188 private boolean mUpdateThumbnail;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700189
Mathew Inwood1dd8e822011-08-03 14:34:29 +0100190 /**
Mathew Inwood9ad1eac2011-09-15 11:29:50 +0100191 * See {@link #clearBackStackWhenItemAdded(String)}.
Mathew Inwood1dd8e822011-08-03 14:34:29 +0100192 */
Mathew Inwooda829d552011-09-02 14:16:25 +0100193 private Pattern mClearHistoryUrlPattern;
Mathew Inwood1dd8e822011-08-03 14:34:29 +0100194
John Reck1cf4b792011-07-26 10:22:22 -0700195 private static synchronized Bitmap getDefaultFavicon(Context context) {
196 if (sDefaultFavicon == null) {
197 sDefaultFavicon = BitmapFactory.decodeResource(
198 context.getResources(), R.drawable.app_web_browser_sm);
199 }
200 return sDefaultFavicon;
201 }
Michael Kolbeb95db42011-03-03 10:38:40 -0800202
John Reck30c714c2010-12-16 17:30:34 -0800203 // All the state needed for a page
John Reckd8c74522011-06-14 08:45:00 -0700204 protected static class PageState {
John Reck30c714c2010-12-16 17:30:34 -0800205 String mUrl;
John Reckdb22ec42011-06-29 11:31:24 -0700206 String mOriginalUrl;
John Reck30c714c2010-12-16 17:30:34 -0800207 String mTitle;
Steve Block2466eff2011-10-03 15:33:09 +0100208 SecurityState mSecurityState;
Steve Block08a6f0c2011-10-06 12:12:53 +0100209 // This is non-null only when mSecurityState is SECURITY_STATE_BAD_CERTIFICATE.
210 SslError mSslCertificateError;
John Reck30c714c2010-12-16 17:30:34 -0800211 Bitmap mFavicon;
Steve Block08a6f0c2011-10-06 12:12:53 +0100212 boolean mIsBookmarkedSite;
213 boolean mIncognito;
John Reck30c714c2010-12-16 17:30:34 -0800214
215 PageState(Context c, boolean incognito) {
John Reck502a3532011-08-16 14:21:46 -0700216 mIncognito = incognito;
217 if (mIncognito) {
John Reckdb22ec42011-06-29 11:31:24 -0700218 mOriginalUrl = mUrl = "browser:incognito";
John Reck30c714c2010-12-16 17:30:34 -0800219 mTitle = c.getString(R.string.new_incognito_tab);
John Reck30c714c2010-12-16 17:30:34 -0800220 } else {
John Reckdb22ec42011-06-29 11:31:24 -0700221 mOriginalUrl = mUrl = "";
John Reck30c714c2010-12-16 17:30:34 -0800222 mTitle = c.getString(R.string.new_tab);
John Reck30c714c2010-12-16 17:30:34 -0800223 }
Steve Block2466eff2011-10-03 15:33:09 +0100224 mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
John Reck30c714c2010-12-16 17:30:34 -0800225 }
226
227 PageState(Context c, boolean incognito, String url, Bitmap favicon) {
John Reck502a3532011-08-16 14:21:46 -0700228 mIncognito = incognito;
John Reckdb22ec42011-06-29 11:31:24 -0700229 mOriginalUrl = mUrl = url;
John Reck30c714c2010-12-16 17:30:34 -0800230 if (URLUtil.isHttpsUrl(url)) {
Steve Block2466eff2011-10-03 15:33:09 +0100231 mSecurityState = SecurityState.SECURITY_STATE_SECURE;
John Reck30c714c2010-12-16 17:30:34 -0800232 } else {
Steve Block2466eff2011-10-03 15:33:09 +0100233 mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
John Reck30c714c2010-12-16 17:30:34 -0800234 }
John Reck1cf4b792011-07-26 10:22:22 -0700235 mFavicon = favicon;
John Reck30c714c2010-12-16 17:30:34 -0800236 }
John Reck1cf4b792011-07-26 10:22:22 -0700237
Grace Kloba22ac16e2009-10-07 18:00:23 -0700238 }
239
John Reck30c714c2010-12-16 17:30:34 -0800240 // The current/loading page's state
John Reckd8c74522011-06-14 08:45:00 -0700241 protected PageState mCurrentState;
John Reck30c714c2010-12-16 17:30:34 -0800242
Grace Kloba22ac16e2009-10-07 18:00:23 -0700243 // Used for saving and restoring each Tab
Michael Kolbc831b632011-05-11 09:30:34 -0700244 static final String ID = "ID";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700245 static final String CURRURL = "currentUrl";
246 static final String CURRTITLE = "currentTitle";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700247 static final String PARENTTAB = "parentTab";
248 static final String APPID = "appid";
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700249 static final String INCOGNITO = "privateBrowsingEnabled";
John Reckb0a86db2011-05-24 14:05:58 -0700250 static final String USERAGENT = "useragent";
Michael Kolbe28b3472011-08-04 16:54:31 -0700251 static final String CLOSEFLAG = "closeOnBack";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700252
Grace Kloba22ac16e2009-10-07 18:00:23 -0700253 // Container class for the next error dialog that needs to be displayed
254 private class ErrorDialog {
255 public final int mTitle;
256 public final String mDescription;
257 public final int mError;
258 ErrorDialog(int title, String desc, int error) {
259 mTitle = title;
260 mDescription = desc;
261 mError = error;
262 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700263 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700264
265 private void processNextError() {
266 if (mQueuedErrors == null) {
267 return;
268 }
269 // The first one is currently displayed so just remove it.
270 mQueuedErrors.removeFirst();
271 if (mQueuedErrors.size() == 0) {
272 mQueuedErrors = null;
273 return;
274 }
275 showError(mQueuedErrors.getFirst());
276 }
277
278 private DialogInterface.OnDismissListener mDialogListener =
279 new DialogInterface.OnDismissListener() {
280 public void onDismiss(DialogInterface d) {
281 processNextError();
282 }
283 };
284 private LinkedList<ErrorDialog> mQueuedErrors;
285
286 private void queueError(int err, String desc) {
287 if (mQueuedErrors == null) {
288 mQueuedErrors = new LinkedList<ErrorDialog>();
289 }
290 for (ErrorDialog d : mQueuedErrors) {
291 if (d.mError == err) {
292 // Already saw a similar error, ignore the new one.
293 return;
294 }
295 }
296 ErrorDialog errDialog = new ErrorDialog(
297 err == WebViewClient.ERROR_FILE_NOT_FOUND ?
298 R.string.browserFrameFileErrorLabel :
299 R.string.browserFrameNetworkErrorLabel,
300 desc, err);
301 mQueuedErrors.addLast(errDialog);
302
303 // Show the dialog now if the queue was empty and it is in foreground
304 if (mQueuedErrors.size() == 1 && mInForeground) {
305 showError(errDialog);
306 }
307 }
308
309 private void showError(ErrorDialog errDialog) {
310 if (mInForeground) {
Michael Kolb14612442011-06-24 13:06:29 -0700311 AlertDialog d = new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700312 .setTitle(errDialog.mTitle)
313 .setMessage(errDialog.mDescription)
314 .setPositiveButton(R.string.ok, null)
315 .create();
316 d.setOnDismissListener(mDialogListener);
317 d.show();
318 }
319 }
320
321 // -------------------------------------------------------------------------
322 // WebViewClient implementation for the main WebView
323 // -------------------------------------------------------------------------
324
325 private final WebViewClient mWebViewClient = new WebViewClient() {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500326 private Message mDontResend;
327 private Message mResend;
Michael Kolb47bd1e42011-09-01 15:25:00 -0700328
329 private boolean providersDiffer(String url, String otherUrl) {
330 Uri uri1 = Uri.parse(url);
331 Uri uri2 = Uri.parse(otherUrl);
332 return !uri1.getEncodedAuthority().equals(uri2.getEncodedAuthority());
333 }
334
Grace Kloba22ac16e2009-10-07 18:00:23 -0700335 @Override
336 public void onPageStarted(WebView view, String url, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700337 mInPageLoad = true;
Michael Kolb72864272012-05-03 15:42:15 -0700338 mUpdateThumbnail = true;
Michael Kolba53c9892011-10-05 13:31:40 -0700339 mPageLoadProgress = INITIAL_PROGRESS;
Michael Kolb14612442011-06-24 13:06:29 -0700340 mCurrentState = new PageState(mContext,
John Reck30c714c2010-12-16 17:30:34 -0800341 view.isPrivateBrowsingEnabled(), url, favicon);
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000342 mLoadStartTime = SystemClock.uptimeMillis();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700343
344 // If we start a touch icon load and then load a new page, we don't
345 // want to cancel the current touch icon loader. But, we do want to
346 // create a new one when the touch icon url is known.
347 if (mTouchIconLoader != null) {
348 mTouchIconLoader.mTab = null;
349 mTouchIconLoader = null;
350 }
351
352 // reset the error console
353 if (mErrorConsole != null) {
354 mErrorConsole.clearErrorMessages();
Michael Kolb8233fac2010-10-26 16:08:53 -0700355 if (mWebViewController.shouldShowErrorConsole()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700356 mErrorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
357 }
358 }
359
Patrick Scott92066772011-03-10 08:46:27 -0500360 // Cancel the auto-login process.
361 if (mDeviceAccountLogin != null) {
362 mDeviceAccountLogin.cancel();
363 mDeviceAccountLogin = null;
364 mWebViewController.hideAutoLogin(Tab.this);
365 }
366
Grace Kloba22ac16e2009-10-07 18:00:23 -0700367 // finally update the UI in the activity if it is in the foreground
John Reck324d4402011-01-11 16:56:42 -0800368 mWebViewController.onPageStarted(Tab.this, view, favicon);
Leon Scroggins4cd97792010-12-03 15:31:56 -0500369
John Recke969cc52010-12-21 17:24:43 -0800370 updateBookmarkedStatus();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700371 }
372
373 @Override
374 public void onPageFinished(WebView view, String url) {
John Reck5b691842010-11-29 11:21:13 -0800375 if (!isPrivateBrowsingEnabled()) {
376 LogTag.logPageFinishedLoading(
377 url, SystemClock.uptimeMillis() - mLoadStartTime);
378 }
John Reck1cf4b792011-07-26 10:22:22 -0700379 syncCurrentState(view, url);
John Reck324d4402011-01-11 16:56:42 -0800380 mWebViewController.onPageFinished(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700381 }
382
383 // return true if want to hijack the url to let another app to handle it
384 @Override
385 public boolean shouldOverrideUrlLoading(WebView view, String url) {
386 if (mInForeground) {
Michael Kolb18eb3772010-12-10 14:29:51 -0800387 return mWebViewController.shouldOverrideUrlLoading(Tab.this,
388 view, url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700389 } else {
390 return false;
391 }
392 }
393
394 /**
Steve Block2466eff2011-10-03 15:33:09 +0100395 * Updates the security state. This method is called when we discover
396 * another resource to be loaded for this page (for example,
397 * javascript). While we update the security state, we do not update
398 * the lock icon until we are done loading, as it is slightly more
399 * secure this way.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700400 */
401 @Override
402 public void onLoadResource(WebView view, String url) {
403 if (url != null && url.length() > 0) {
404 // It is only if the page claims to be secure that we may have
Steve Block2466eff2011-10-03 15:33:09 +0100405 // to update the security state:
406 if (mCurrentState.mSecurityState == SecurityState.SECURITY_STATE_SECURE) {
407 // If NOT a 'safe' url, change the state to mixed content!
Grace Kloba22ac16e2009-10-07 18:00:23 -0700408 if (!(URLUtil.isHttpsUrl(url) || URLUtil.isDataUrl(url)
409 || URLUtil.isAboutUrl(url))) {
Steve Block2466eff2011-10-03 15:33:09 +0100410 mCurrentState.mSecurityState = SecurityState.SECURITY_STATE_MIXED;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700411 }
412 }
413 }
414 }
415
416 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700417 * Show a dialog informing the user of the network error reported by
418 * WebCore if it is in the foreground.
419 */
420 @Override
421 public void onReceivedError(WebView view, int errorCode,
422 String description, String failingUrl) {
423 if (errorCode != WebViewClient.ERROR_HOST_LOOKUP &&
424 errorCode != WebViewClient.ERROR_CONNECT &&
425 errorCode != WebViewClient.ERROR_BAD_URL &&
426 errorCode != WebViewClient.ERROR_UNSUPPORTED_SCHEME &&
427 errorCode != WebViewClient.ERROR_FILE) {
428 queueError(errorCode, description);
Jeff Hamilton47654f42010-09-07 09:57:51 -0500429
Selim Gurun3da06b82011-10-10 13:58:12 -0700430 // Don't log URLs when in private browsing mode
431 if (!isPrivateBrowsingEnabled()) {
432 Log.e(LOGTAG, "onReceivedError " + errorCode + " " + failingUrl
Jeff Hamilton47654f42010-09-07 09:57:51 -0500433 + " " + description);
Selim Gurun3da06b82011-10-10 13:58:12 -0700434 }
Jeff Hamilton47654f42010-09-07 09:57:51 -0500435 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700436 }
437
438 /**
439 * Check with the user if it is ok to resend POST data as the page they
440 * are trying to navigate to is the result of a POST.
441 */
442 @Override
443 public void onFormResubmission(WebView view, final Message dontResend,
444 final Message resend) {
445 if (!mInForeground) {
446 dontResend.sendToTarget();
447 return;
448 }
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500449 if (mDontResend != null) {
450 Log.w(LOGTAG, "onFormResubmission should not be called again "
451 + "while dialog is still up");
452 dontResend.sendToTarget();
453 return;
454 }
455 mDontResend = dontResend;
456 mResend = resend;
Michael Kolb14612442011-06-24 13:06:29 -0700457 new AlertDialog.Builder(mContext).setTitle(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700458 R.string.browserFrameFormResubmitLabel).setMessage(
459 R.string.browserFrameFormResubmitMessage)
460 .setPositiveButton(R.string.ok,
461 new DialogInterface.OnClickListener() {
462 public void onClick(DialogInterface dialog,
463 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500464 if (mResend != null) {
465 mResend.sendToTarget();
466 mResend = null;
467 mDontResend = null;
468 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700469 }
470 }).setNegativeButton(R.string.cancel,
471 new DialogInterface.OnClickListener() {
472 public void onClick(DialogInterface dialog,
473 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500474 if (mDontResend != null) {
475 mDontResend.sendToTarget();
476 mResend = null;
477 mDontResend = null;
478 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700479 }
480 }).setOnCancelListener(new OnCancelListener() {
481 public void onCancel(DialogInterface dialog) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500482 if (mDontResend != null) {
483 mDontResend.sendToTarget();
484 mResend = null;
485 mDontResend = null;
486 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700487 }
488 }).show();
489 }
490
491 /**
492 * Insert the url into the visited history database.
493 * @param url The url to be inserted.
494 * @param isReload True if this url is being reloaded.
495 * FIXME: Not sure what to do when reloading the page.
496 */
497 @Override
498 public void doUpdateVisitedHistory(WebView view, String url,
499 boolean isReload) {
John Reck324d4402011-01-11 16:56:42 -0800500 mWebViewController.doUpdateVisitedHistory(Tab.this, isReload);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700501 }
502
503 /**
504 * Displays SSL error(s) dialog to the user.
505 */
506 @Override
507 public void onReceivedSslError(final WebView view,
508 final SslErrorHandler handler, final SslError error) {
509 if (!mInForeground) {
510 handler.cancel();
Steve Block2466eff2011-10-03 15:33:09 +0100511 setSecurityState(SecurityState.SECURITY_STATE_NOT_SECURE);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700512 return;
513 }
John Reck35e9dd62011-04-25 09:01:54 -0700514 if (mSettings.showSecurityWarnings()) {
John Reckcb28b2c2011-08-26 17:39:44 -0700515 new AlertDialog.Builder(mContext)
516 .setTitle(R.string.security_warning)
517 .setMessage(R.string.ssl_warnings_header)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200518 .setIconAttribute(android.R.attr.alertDialogIcon)
John Reckcb28b2c2011-08-26 17:39:44 -0700519 .setPositiveButton(R.string.ssl_continue,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700520 new DialogInterface.OnClickListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700521 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700522 public void onClick(DialogInterface dialog,
523 int whichButton) {
524 handler.proceed();
Steve Block4895b012011-10-03 16:26:46 +0100525 handleProceededAfterSslError(error);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700526 }
John Reckcb28b2c2011-08-26 17:39:44 -0700527 })
528 .setNeutralButton(R.string.view_certificate,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700529 new DialogInterface.OnClickListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700530 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700531 public void onClick(DialogInterface dialog,
532 int whichButton) {
John Reckcb28b2c2011-08-26 17:39:44 -0700533 mWebViewController.showSslCertificateOnError(
534 view, handler, error);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700535 }
John Reckcb28b2c2011-08-26 17:39:44 -0700536 })
537 .setNegativeButton(R.string.ssl_go_back,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700538 new DialogInterface.OnClickListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700539 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700540 public void onClick(DialogInterface dialog,
541 int whichButton) {
John Reck30c714c2010-12-16 17:30:34 -0800542 dialog.cancel();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700543 }
John Reckcb28b2c2011-08-26 17:39:44 -0700544 })
545 .setOnCancelListener(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700546 new DialogInterface.OnCancelListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700547 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700548 public void onCancel(DialogInterface dialog) {
549 handler.cancel();
Steve Block2466eff2011-10-03 15:33:09 +0100550 setSecurityState(SecurityState.SECURITY_STATE_NOT_SECURE);
John Reck30c714c2010-12-16 17:30:34 -0800551 mWebViewController.onUserCanceledSsl(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700552 }
John Reckcb28b2c2011-08-26 17:39:44 -0700553 })
554 .show();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700555 } else {
556 handler.proceed();
557 }
558 }
559
560 /**
Steve Block4895b012011-10-03 16:26:46 +0100561 * Called when an SSL error occurred while loading a resource, but the
562 * WebView but chose to proceed anyway based on a decision retained
563 * from a previous response to onReceivedSslError(). We update our
564 * security state to reflect this.
565 */
566 @Override
567 public void onProceededAfterSslError(WebView view, SslError error) {
568 handleProceededAfterSslError(error);
569 }
570
571 /**
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700572 * Displays client certificate request to the user.
573 */
574 @Override
575 public void onReceivedClientCertRequest(final WebView view,
576 final ClientCertRequestHandler handler, final String host_and_port) {
577 if (!mInForeground) {
578 handler.ignore();
579 return;
580 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700581 int colon = host_and_port.lastIndexOf(':');
582 String host;
583 int port;
584 if (colon == -1) {
585 host = host_and_port;
586 port = -1;
587 } else {
588 String portString = host_and_port.substring(colon + 1);
589 try {
590 port = Integer.parseInt(portString);
591 host = host_and_port.substring(0, colon);
592 } catch (NumberFormatException e) {
593 host = host_and_port;
594 port = -1;
595 }
596 }
Michael Kolb14612442011-06-24 13:06:29 -0700597 KeyChain.choosePrivateKeyAlias(
598 mWebViewController.getActivity(), new KeyChainAliasCallback() {
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700599 @Override public void alias(String alias) {
600 if (alias == null) {
601 handler.cancel();
602 return;
603 }
Michael Kolb14612442011-06-24 13:06:29 -0700604 new KeyChainLookup(mContext, handler, alias).execute();
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700605 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700606 }, null, null, host, port, null);
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700607 }
608
609 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700610 * Handles an HTTP authentication request.
611 *
612 * @param handler The authentication handler
613 * @param host The host
614 * @param realm The realm
615 */
616 @Override
617 public void onReceivedHttpAuthRequest(WebView view,
618 final HttpAuthHandler handler, final String host,
619 final String realm) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700620 mWebViewController.onReceivedHttpAuthRequest(Tab.this, view, handler, host, realm);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700621 }
622
623 @Override
John Reck438bf462011-01-12 18:11:46 -0800624 public WebResourceResponse shouldInterceptRequest(WebView view,
625 String url) {
626 WebResourceResponse res = HomeProvider.shouldInterceptRequest(
Michael Kolb14612442011-06-24 13:06:29 -0700627 mContext, url);
John Reck438bf462011-01-12 18:11:46 -0800628 return res;
629 }
630
631 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700632 public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
633 if (!mInForeground) {
634 return false;
635 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700636 return mWebViewController.shouldOverrideKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700637 }
638
639 @Override
640 public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700641 if (!mInForeground) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700642 return;
643 }
John Reck997b1b72012-04-19 18:08:25 -0700644 if (!mWebViewController.onUnhandledKeyEvent(event)) {
645 super.onUnhandledKeyEvent(view, event);
646 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700647 }
Patrick Scott92066772011-03-10 08:46:27 -0500648
649 @Override
650 public void onReceivedLoginRequest(WebView view, String realm,
651 String account, String args) {
Michael Kolb14612442011-06-24 13:06:29 -0700652 new DeviceAccountLogin(mWebViewController.getActivity(), view, Tab.this, mWebViewController)
Patrick Scott92066772011-03-10 08:46:27 -0500653 .handleLogin(realm, account, args);
654 }
655
Grace Kloba22ac16e2009-10-07 18:00:23 -0700656 };
657
John Reck1cf4b792011-07-26 10:22:22 -0700658 private void syncCurrentState(WebView view, String url) {
659 // Sync state (in case of stop/timeout)
660 mCurrentState.mUrl = view.getUrl();
661 if (mCurrentState.mUrl == null) {
John Reck8bcafc12011-08-29 16:43:02 -0700662 mCurrentState.mUrl = "";
John Reck1cf4b792011-07-26 10:22:22 -0700663 }
664 mCurrentState.mOriginalUrl = view.getOriginalUrl();
665 mCurrentState.mTitle = view.getTitle();
666 mCurrentState.mFavicon = view.getFavicon();
667 if (!URLUtil.isHttpsUrl(mCurrentState.mUrl)) {
668 // In case we stop when loading an HTTPS page from an HTTP page
669 // but before a provisional load occurred
Steve Block2466eff2011-10-03 15:33:09 +0100670 mCurrentState.mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
Steve Block08a6f0c2011-10-06 12:12:53 +0100671 mCurrentState.mSslCertificateError = null;
John Reck1cf4b792011-07-26 10:22:22 -0700672 }
John Reck502a3532011-08-16 14:21:46 -0700673 mCurrentState.mIncognito = view.isPrivateBrowsingEnabled();
John Reck1cf4b792011-07-26 10:22:22 -0700674 }
675
Patrick Scott92066772011-03-10 08:46:27 -0500676 // Called by DeviceAccountLogin when the Tab needs to have the auto-login UI
677 // displayed.
678 void setDeviceAccountLogin(DeviceAccountLogin login) {
679 mDeviceAccountLogin = login;
680 }
681
682 // Returns non-null if the title bar should display the auto-login UI.
683 DeviceAccountLogin getDeviceAccountLogin() {
684 return mDeviceAccountLogin;
685 }
686
Grace Kloba22ac16e2009-10-07 18:00:23 -0700687 // -------------------------------------------------------------------------
688 // WebChromeClient implementation for the main WebView
689 // -------------------------------------------------------------------------
690
691 private final WebChromeClient mWebChromeClient = new WebChromeClient() {
692 // Helper method to create a new tab or sub window.
693 private void createWindow(final boolean dialog, final Message msg) {
694 WebView.WebViewTransport transport =
695 (WebView.WebViewTransport) msg.obj;
696 if (dialog) {
697 createSubWindow();
Michael Kolb8233fac2010-10-26 16:08:53 -0700698 mWebViewController.attachSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700699 transport.setWebView(mSubView);
700 } else {
Michael Kolb7bcafde2011-05-09 13:55:59 -0700701 final Tab newTab = mWebViewController.openTab(null,
John Reck5949c662011-05-27 09:52:29 -0700702 Tab.this, true, true);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700703 transport.setWebView(newTab.getWebView());
704 }
705 msg.sendToTarget();
706 }
707
708 @Override
709 public boolean onCreateWindow(WebView view, final boolean dialog,
710 final boolean userGesture, final Message resultMsg) {
711 // only allow new window or sub window for the foreground case
712 if (!mInForeground) {
713 return false;
714 }
715 // Short-circuit if we can't create any more tabs or sub windows.
716 if (dialog && mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -0700717 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700718 .setTitle(R.string.too_many_subwindows_dialog_title)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200719 .setIconAttribute(android.R.attr.alertDialogIcon)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700720 .setMessage(R.string.too_many_subwindows_dialog_message)
721 .setPositiveButton(R.string.ok, null)
722 .show();
723 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700724 } else if (!mWebViewController.getTabControl().canCreateNewTab()) {
Michael Kolb14612442011-06-24 13:06:29 -0700725 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700726 .setTitle(R.string.too_many_windows_dialog_title)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200727 .setIconAttribute(android.R.attr.alertDialogIcon)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700728 .setMessage(R.string.too_many_windows_dialog_message)
729 .setPositiveButton(R.string.ok, null)
730 .show();
731 return false;
732 }
733
734 // Short-circuit if this was a user gesture.
735 if (userGesture) {
736 createWindow(dialog, resultMsg);
737 return true;
738 }
739
740 // Allow the popup and create the appropriate window.
741 final AlertDialog.OnClickListener allowListener =
742 new AlertDialog.OnClickListener() {
743 public void onClick(DialogInterface d,
744 int which) {
745 createWindow(dialog, resultMsg);
746 }
747 };
748
749 // Block the popup by returning a null WebView.
750 final AlertDialog.OnClickListener blockListener =
751 new AlertDialog.OnClickListener() {
752 public void onClick(DialogInterface d, int which) {
753 resultMsg.sendToTarget();
754 }
755 };
756
757 // Build a confirmation dialog to display to the user.
758 final AlertDialog d =
Michael Kolb14612442011-06-24 13:06:29 -0700759 new AlertDialog.Builder(mContext)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200760 .setIconAttribute(android.R.attr.alertDialogIcon)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700761 .setMessage(R.string.popup_window_attempt)
762 .setPositiveButton(R.string.allow, allowListener)
763 .setNegativeButton(R.string.block, blockListener)
764 .setCancelable(false)
765 .create();
766
767 // Show the confirmation dialog.
768 d.show();
769 return true;
770 }
771
772 @Override
Patrick Scotteb5061b2009-11-18 15:00:30 -0500773 public void onRequestFocus(WebView view) {
774 if (!mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700775 mWebViewController.switchToTab(Tab.this);
Patrick Scotteb5061b2009-11-18 15:00:30 -0500776 }
777 }
778
779 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700780 public void onCloseWindow(WebView window) {
Michael Kolbc831b632011-05-11 09:30:34 -0700781 if (mParent != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700782 // JavaScript can only close popup window.
783 if (mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700784 mWebViewController.switchToTab(mParent);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700785 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700786 mWebViewController.closeTab(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700787 }
788 }
789
790 @Override
791 public void onProgressChanged(WebView view, int newProgress) {
John Reck30c714c2010-12-16 17:30:34 -0800792 mPageLoadProgress = newProgress;
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800793 if (newProgress == 100) {
794 mInPageLoad = false;
795 }
John Reck30c714c2010-12-16 17:30:34 -0800796 mWebViewController.onProgressChanged(Tab.this);
Michael Kolb72864272012-05-03 15:42:15 -0700797 if (mUpdateThumbnail && newProgress == 100) {
798 mUpdateThumbnail = false;
799 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700800 }
801
802 @Override
Leon Scroggins21d9b902010-03-11 09:33:11 -0500803 public void onReceivedTitle(WebView view, final String title) {
John Reck30c714c2010-12-16 17:30:34 -0800804 mCurrentState.mTitle = title;
Michael Kolb8233fac2010-10-26 16:08:53 -0700805 mWebViewController.onReceivedTitle(Tab.this, title);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700806 }
807
808 @Override
809 public void onReceivedIcon(WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -0800810 mCurrentState.mFavicon = icon;
Michael Kolb8233fac2010-10-26 16:08:53 -0700811 mWebViewController.onFavicon(Tab.this, view, icon);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700812 }
813
814 @Override
815 public void onReceivedTouchIconUrl(WebView view, String url,
816 boolean precomposed) {
Michael Kolb14612442011-06-24 13:06:29 -0700817 final ContentResolver cr = mContext.getContentResolver();
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400818 // Let precomposed icons take precedence over non-composed
819 // icons.
820 if (precomposed && mTouchIconLoader != null) {
821 mTouchIconLoader.cancel(false);
822 mTouchIconLoader = null;
823 }
824 // Have only one async task at a time.
825 if (mTouchIconLoader == null) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700826 mTouchIconLoader = new DownloadTouchIcon(Tab.this,
Michael Kolb14612442011-06-24 13:06:29 -0700827 mContext, cr, view);
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400828 mTouchIconLoader.execute(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700829 }
830 }
831
832 @Override
833 public void onShowCustomView(View view,
834 WebChromeClient.CustomViewCallback callback) {
Michael Kolb14612442011-06-24 13:06:29 -0700835 Activity activity = mWebViewController.getActivity();
836 if (activity != null) {
837 onShowCustomView(view, activity.getRequestedOrientation(), callback);
838 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400839 }
840
841 @Override
842 public void onShowCustomView(View view, int requestedOrientation,
843 WebChromeClient.CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700844 if (mInForeground) mWebViewController.showCustomView(Tab.this, view,
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400845 requestedOrientation, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700846 }
847
848 @Override
849 public void onHideCustomView() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700850 if (mInForeground) mWebViewController.hideCustomView();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700851 }
852
853 /**
854 * The origin has exceeded its database quota.
855 * @param url the URL that exceeded the quota
856 * @param databaseIdentifier the identifier of the database on which the
857 * transaction that caused the quota overflow was run
858 * @param currentQuota the current quota for the origin.
859 * @param estimatedSize the estimated size of the database.
860 * @param totalUsedQuota is the sum of all origins' quota.
861 * @param quotaUpdater The callback to run when a decision to allow or
862 * deny quota has been made. Don't forget to call this!
863 */
864 @Override
865 public void onExceededDatabaseQuota(String url,
866 String databaseIdentifier, long currentQuota, long estimatedSize,
867 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -0700868 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -0700869 .onExceededDatabaseQuota(url, databaseIdentifier,
870 currentQuota, estimatedSize, totalUsedQuota,
871 quotaUpdater);
872 }
873
874 /**
875 * The Application Cache has exceeded its max size.
876 * @param spaceNeeded is the amount of disk space that would be needed
877 * in order for the last appcache operation to succeed.
878 * @param totalUsedQuota is the sum of all origins' quota.
879 * @param quotaUpdater A callback to inform the WebCore thread that a
880 * new app cache size is available. This callback must always
881 * be executed at some point to ensure that the sleeping
882 * WebCore thread is woken up.
883 */
884 @Override
885 public void onReachedMaxAppCacheSize(long spaceNeeded,
886 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -0700887 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -0700888 .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota,
889 quotaUpdater);
890 }
891
892 /**
893 * Instructs the browser to show a prompt to ask the user to set the
894 * Geolocation permission state for the specified origin.
895 * @param origin The origin for which Geolocation permissions are
896 * requested.
897 * @param callback The callback to call once the user has set the
898 * Geolocation permission state.
899 */
900 @Override
901 public void onGeolocationPermissionsShowPrompt(String origin,
902 GeolocationPermissions.Callback callback) {
903 if (mInForeground) {
Grace Kloba50c241e2010-04-20 11:07:50 -0700904 getGeolocationPermissionsPrompt().show(origin, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700905 }
906 }
907
908 /**
909 * Instructs the browser to hide the Geolocation permissions prompt.
910 */
911 @Override
912 public void onGeolocationPermissionsHidePrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -0700913 if (mInForeground && mGeolocationPermissionsPrompt != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700914 mGeolocationPermissionsPrompt.hide();
915 }
916 }
917
Ben Murdoch65acc352009-11-19 18:16:04 +0000918 /* Adds a JavaScript error message to the system log and if the JS
919 * console is enabled in the about:debug options, to that console
920 * also.
Ben Murdochc42addf2010-01-28 15:19:59 +0000921 * @param consoleMessage the message object.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700922 */
923 @Override
Ben Murdochc42addf2010-01-28 15:19:59 +0000924 public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700925 if (mInForeground) {
926 // call getErrorConsole(true) so it will create one if needed
927 ErrorConsoleView errorConsole = getErrorConsole(true);
Ben Murdochc42addf2010-01-28 15:19:59 +0000928 errorConsole.addErrorMessage(consoleMessage);
Michael Kolb8233fac2010-10-26 16:08:53 -0700929 if (mWebViewController.shouldShowErrorConsole()
930 && errorConsole.getShowState() !=
931 ErrorConsoleView.SHOW_MAXIMIZED) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700932 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
933 }
934 }
Ben Murdochc42addf2010-01-28 15:19:59 +0000935
Jeff Hamilton47654f42010-09-07 09:57:51 -0500936 // Don't log console messages in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -0700937 if (isPrivateBrowsingEnabled()) return true;
Jeff Hamilton47654f42010-09-07 09:57:51 -0500938
Ben Murdochc42addf2010-01-28 15:19:59 +0000939 String message = "Console: " + consoleMessage.message() + " "
940 + consoleMessage.sourceId() + ":"
941 + consoleMessage.lineNumber();
942
943 switch (consoleMessage.messageLevel()) {
944 case TIP:
945 Log.v(CONSOLE_LOGTAG, message);
946 break;
947 case LOG:
948 Log.i(CONSOLE_LOGTAG, message);
949 break;
950 case WARNING:
951 Log.w(CONSOLE_LOGTAG, message);
952 break;
953 case ERROR:
954 Log.e(CONSOLE_LOGTAG, message);
955 break;
956 case DEBUG:
957 Log.d(CONSOLE_LOGTAG, message);
958 break;
959 }
960
961 return true;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700962 }
963
964 /**
965 * Ask the browser for an icon to represent a <video> element.
966 * This icon will be used if the Web page did not specify a poster attribute.
967 * @return Bitmap The icon or null if no such icon is available.
968 */
969 @Override
970 public Bitmap getDefaultVideoPoster() {
971 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700972 return mWebViewController.getDefaultVideoPoster();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700973 }
974 return null;
975 }
976
977 /**
978 * Ask the host application for a custom progress view to show while
979 * a <video> is loading.
980 * @return View The progress view.
981 */
982 @Override
983 public View getVideoLoadingProgressView() {
984 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700985 return mWebViewController.getVideoLoadingProgressView();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700986 }
987 return null;
988 }
989
990 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +0000991 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700992 if (mInForeground) {
Ben Murdoch8cad4132012-01-11 10:56:43 +0000993 mWebViewController.openFileChooser(uploadMsg, acceptType, capture);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700994 } else {
995 uploadMsg.onReceiveValue(null);
996 }
997 }
998
999 /**
1000 * Deliver a list of already-visited URLs
1001 */
1002 @Override
1003 public void getVisitedHistory(final ValueCallback<String[]> callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001004 mWebViewController.getVisitedHistory(callback);
1005 }
Ben Murdoch8029a772010-11-16 11:58:21 +00001006
1007 @Override
1008 public void setupAutoFill(Message message) {
1009 // Prompt the user to set up their profile.
1010 final Message msg = message;
Michael Kolb14612442011-06-24 13:06:29 -07001011 AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
1012 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
Ben Murdoch1d676b62011-01-17 12:54:24 +00001013 Context.LAYOUT_INFLATER_SERVICE);
1014 final View layout = inflater.inflate(R.layout.setup_autofill_dialog, null);
1015
1016 builder.setView(layout)
1017 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1018 @Override
1019 public void onClick(DialogInterface dialog, int id) {
1020 CheckBox disableAutoFill = (CheckBox) layout.findViewById(
1021 R.id.setup_autofill_dialog_disable_autofill);
1022
1023 if (disableAutoFill.isChecked()) {
1024 // Disable autofill and show a toast with how to turn it on again.
John Reck35e9dd62011-04-25 09:01:54 -07001025 mSettings.setAutofillEnabled(false);
Michael Kolb14612442011-06-24 13:06:29 -07001026 Toast.makeText(mContext,
Ben Murdoch1d676b62011-01-17 12:54:24 +00001027 R.string.autofill_setup_dialog_negative_toast,
1028 Toast.LENGTH_LONG).show();
1029 } else {
1030 // Take user to the AutoFill profile editor. When they return,
1031 // we will send the message that we pass here which will trigger
1032 // the form to get filled out with their new profile.
1033 mWebViewController.setupAutoFill(msg);
1034 }
1035 }
1036 })
1037 .setNegativeButton(R.string.cancel, null)
1038 .show();
Ben Murdoch8029a772010-11-16 11:58:21 +00001039 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001040 };
1041
1042 // -------------------------------------------------------------------------
1043 // WebViewClient implementation for the sub window
1044 // -------------------------------------------------------------------------
1045
1046 // Subclass of WebViewClient used in subwindows to notify the main
1047 // WebViewClient of certain WebView activities.
1048 private static class SubWindowClient extends WebViewClient {
1049 // The main WebViewClient.
1050 private final WebViewClient mClient;
Michael Kolb8233fac2010-10-26 16:08:53 -07001051 private final WebViewController mController;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001052
Michael Kolb8233fac2010-10-26 16:08:53 -07001053 SubWindowClient(WebViewClient client, WebViewController controller) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001054 mClient = client;
Michael Kolb8233fac2010-10-26 16:08:53 -07001055 mController = controller;
Leon Scroggins III211ba542010-04-19 13:21:13 -04001056 }
1057 @Override
1058 public void onPageStarted(WebView view, String url, Bitmap favicon) {
1059 // Unlike the others, do not call mClient's version, which would
1060 // change the progress bar. However, we do want to remove the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001061 // find or select dialog.
Michael Kolb8233fac2010-10-26 16:08:53 -07001062 mController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001063 }
1064 @Override
1065 public void doUpdateVisitedHistory(WebView view, String url,
1066 boolean isReload) {
1067 mClient.doUpdateVisitedHistory(view, url, isReload);
1068 }
1069 @Override
1070 public boolean shouldOverrideUrlLoading(WebView view, String url) {
1071 return mClient.shouldOverrideUrlLoading(view, url);
1072 }
1073 @Override
1074 public void onReceivedSslError(WebView view, SslErrorHandler handler,
1075 SslError error) {
1076 mClient.onReceivedSslError(view, handler, error);
1077 }
1078 @Override
Brian Carlstrom8862c1d2011-06-02 01:05:55 -07001079 public void onReceivedClientCertRequest(WebView view,
1080 ClientCertRequestHandler handler, String host_and_port) {
1081 mClient.onReceivedClientCertRequest(view, handler, host_and_port);
1082 }
1083 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -07001084 public void onReceivedHttpAuthRequest(WebView view,
1085 HttpAuthHandler handler, String host, String realm) {
1086 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
1087 }
1088 @Override
1089 public void onFormResubmission(WebView view, Message dontResend,
1090 Message resend) {
1091 mClient.onFormResubmission(view, dontResend, resend);
1092 }
1093 @Override
1094 public void onReceivedError(WebView view, int errorCode,
1095 String description, String failingUrl) {
1096 mClient.onReceivedError(view, errorCode, description, failingUrl);
1097 }
1098 @Override
1099 public boolean shouldOverrideKeyEvent(WebView view,
1100 android.view.KeyEvent event) {
1101 return mClient.shouldOverrideKeyEvent(view, event);
1102 }
1103 @Override
1104 public void onUnhandledKeyEvent(WebView view,
1105 android.view.KeyEvent event) {
1106 mClient.onUnhandledKeyEvent(view, event);
1107 }
1108 }
1109
1110 // -------------------------------------------------------------------------
1111 // WebChromeClient implementation for the sub window
1112 // -------------------------------------------------------------------------
1113
1114 private class SubWindowChromeClient extends WebChromeClient {
1115 // The main WebChromeClient.
1116 private final WebChromeClient mClient;
1117
1118 SubWindowChromeClient(WebChromeClient client) {
1119 mClient = client;
1120 }
1121 @Override
1122 public void onProgressChanged(WebView view, int newProgress) {
1123 mClient.onProgressChanged(view, newProgress);
1124 }
1125 @Override
1126 public boolean onCreateWindow(WebView view, boolean dialog,
1127 boolean userGesture, android.os.Message resultMsg) {
1128 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
1129 }
1130 @Override
1131 public void onCloseWindow(WebView window) {
1132 if (window != mSubView) {
1133 Log.e(LOGTAG, "Can't close the window");
1134 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001135 mWebViewController.dismissSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001136 }
1137 }
1138
1139 // -------------------------------------------------------------------------
1140
1141 // Construct a new tab
Michael Kolb7bcafde2011-05-09 13:55:59 -07001142 Tab(WebViewController wvcontroller, WebView w) {
John Reck1cf4b792011-07-26 10:22:22 -07001143 this(wvcontroller, w, null);
1144 }
1145
1146 Tab(WebViewController wvcontroller, Bundle state) {
1147 this(wvcontroller, null, state);
1148 }
1149
1150 Tab(WebViewController wvcontroller, WebView w, Bundle state) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001151 mWebViewController = wvcontroller;
Michael Kolb14612442011-06-24 13:06:29 -07001152 mContext = mWebViewController.getContext();
John Reck35e9dd62011-04-25 09:01:54 -07001153 mSettings = BrowserSettings.getInstance();
Michael Kolb14612442011-06-24 13:06:29 -07001154 mDataController = DataController.getInstance(mContext);
1155 mCurrentState = new PageState(mContext, w != null
John Reck847b5322011-04-14 17:02:18 -07001156 ? w.isPrivateBrowsingEnabled() : false);
Michael Kolb8233fac2010-10-26 16:08:53 -07001157 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001158 mInForeground = false;
1159
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001160 mDownloadListener = new DownloadListener() {
1161 public void onDownloadStart(String url, String userAgent,
1162 String contentDisposition, String mimetype,
1163 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001164 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001165 mimetype, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001166 }
1167 };
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001168 mWebBackForwardListClient = new WebBackForwardListClient() {
1169 @Override
1170 public void onNewHistoryItem(WebHistoryItem item) {
Mathew Inwooda829d552011-09-02 14:16:25 +01001171 if (mClearHistoryUrlPattern != null) {
1172 boolean match =
1173 mClearHistoryUrlPattern.matcher(item.getOriginalUrl()).matches();
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001174 if (LOGD_ENABLED) {
Mathew Inwooda829d552011-09-02 14:16:25 +01001175 Log.d(LOGTAG, "onNewHistoryItem: match=" + match + "\n\t"
1176 + item.getUrl() + "\n\t"
1177 + mClearHistoryUrlPattern);
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001178 }
Mathew Inwooda829d552011-09-02 14:16:25 +01001179 if (match) {
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001180 if (mMainView != null) {
1181 mMainView.clearHistory();
1182 }
1183 }
Mathew Inwooda829d552011-09-02 14:16:25 +01001184 mClearHistoryUrlPattern = null;
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001185 }
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001186 }
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001187 };
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001188
John Reck1cf4b792011-07-26 10:22:22 -07001189 mCaptureWidth = mContext.getResources().getDimensionPixelSize(
1190 R.dimen.tab_thumbnail_width);
1191 mCaptureHeight = mContext.getResources().getDimensionPixelSize(
1192 R.dimen.tab_thumbnail_height);
1193 updateShouldCaptureThumbnails();
1194 restoreState(state);
John Reck52be4782011-08-26 15:37:29 -07001195 if (getId() == -1) {
1196 mId = TabControl.getNextId();
1197 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001198 setWebView(w);
Michael Kolb9ef259a2011-07-12 15:33:08 -07001199 mHandler = new Handler() {
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001200 @Override
Michael Kolb9ef259a2011-07-12 15:33:08 -07001201 public void handleMessage(Message m) {
John Reck1cf4b792011-07-26 10:22:22 -07001202 switch (m.what) {
1203 case MSG_CAPTURE:
1204 capture();
1205 break;
1206 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001207 }
1208 };
John Reck1cf4b792011-07-26 10:22:22 -07001209 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001210
Michael Kolb72864272012-05-03 15:42:15 -07001211 public boolean shouldUpdateThumbnail() {
1212 return mUpdateThumbnail;
1213 }
1214
Mathew Inwoode09305e2011-09-02 12:03:26 +01001215 /**
1216 * This is used to get a new ID when the tab has been preloaded, before it is displayed and
1217 * added to TabControl. Preloaded tabs can be created before restoreInstanceState, leading
1218 * to overlapping IDs between the preloaded and restored tabs.
1219 */
1220 public void refreshIdAfterPreload() {
1221 mId = TabControl.getNextId();
1222 }
1223
John Reck1cf4b792011-07-26 10:22:22 -07001224 public void updateShouldCaptureThumbnails() {
1225 if (mWebViewController.shouldCaptureThumbnails()) {
1226 synchronized (Tab.this) {
1227 if (mCapture == null) {
1228 mCapture = Bitmap.createBitmap(mCaptureWidth, mCaptureHeight,
1229 Bitmap.Config.RGB_565);
Michael Kolbc3af0672011-08-09 10:24:41 -07001230 mCapture.eraseColor(Color.WHITE);
John Reck1cf4b792011-07-26 10:22:22 -07001231 if (mInForeground) {
1232 postCapture();
1233 }
1234 }
1235 }
1236 } else {
1237 synchronized (Tab.this) {
1238 mCapture = null;
1239 deleteThumbnail();
1240 }
1241 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001242 }
1243
Michael Kolb14612442011-06-24 13:06:29 -07001244 public void setController(WebViewController ctl) {
1245 mWebViewController = ctl;
John Reck1cf4b792011-07-26 10:22:22 -07001246 updateShouldCaptureThumbnails();
Michael Kolb14612442011-06-24 13:06:29 -07001247 }
1248
Michael Kolbc831b632011-05-11 09:30:34 -07001249 public long getId() {
1250 return mId;
1251 }
1252
Michael Kolb91911a22012-01-17 11:21:25 -08001253 void setWebView(WebView w) {
1254 setWebView(w, true);
1255 }
1256
Grace Kloba22ac16e2009-10-07 18:00:23 -07001257 /**
1258 * Sets the WebView for this tab, correctly removing the old WebView from
1259 * the container view.
1260 */
Michael Kolb91911a22012-01-17 11:21:25 -08001261 void setWebView(WebView w, boolean restore) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001262 if (mMainView == w) {
1263 return;
1264 }
Michael Kolba713ec82010-11-29 17:27:06 -08001265
Grace Kloba22ac16e2009-10-07 18:00:23 -07001266 // If the WebView is changing, the page will be reloaded, so any ongoing
1267 // Geolocation permission requests are void.
Grace Kloba50c241e2010-04-20 11:07:50 -07001268 if (mGeolocationPermissionsPrompt != null) {
1269 mGeolocationPermissionsPrompt.hide();
1270 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001271
Michael Kolba713ec82010-11-29 17:27:06 -08001272 mWebViewController.onSetWebView(this, w);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001273
John Reck1cf4b792011-07-26 10:22:22 -07001274 if (mMainView != null) {
John Reck8ee633f2011-08-09 16:00:35 -07001275 mMainView.setPictureListener(null);
John Reck1cf4b792011-07-26 10:22:22 -07001276 if (w != null) {
1277 syncCurrentState(w, null);
1278 } else {
1279 mCurrentState = new PageState(mContext, false);
1280 }
1281 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001282 // set the new one
1283 mMainView = w;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001284 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001285 if (mMainView != null) {
1286 mMainView.setWebViewClient(mWebViewClient);
1287 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001288 // Attach DownloadManager so that downloads can start in an active
1289 // or a non-active window. This can happen when going to a site that
1290 // does a redirect after a period of time. The user could have
1291 // switched to another tab while waiting for the download to start.
1292 mMainView.setDownloadListener(mDownloadListener);
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00001293 getWebViewClassic().setWebBackForwardListClient(mWebBackForwardListClient);
John Reck8ee633f2011-08-09 16:00:35 -07001294 TabControl tc = mWebViewController.getTabControl();
1295 if (tc != null && tc.getOnThumbnailUpdatedListener() != null) {
1296 mMainView.setPictureListener(this);
1297 }
Michael Kolb91911a22012-01-17 11:21:25 -08001298 if (restore && (mSavedState != null)) {
John Reck8b9bb8b2012-03-08 13:19:40 -08001299 restoreUserAgent();
John Reck6c2e2f32011-08-22 13:41:23 -07001300 WebBackForwardList restoredState
1301 = mMainView.restoreState(mSavedState);
1302 if (restoredState == null || restoredState.getSize() == 0) {
1303 Log.w(LOGTAG, "Failed to restore WebView state!");
1304 loadUrl(mCurrentState.mOriginalUrl, null);
1305 }
John Reck1cf4b792011-07-26 10:22:22 -07001306 mSavedState = null;
1307 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001308 }
1309 }
1310
1311 /**
1312 * Destroy the tab's main WebView and subWindow if any
1313 */
1314 void destroy() {
1315 if (mMainView != null) {
1316 dismissSubWindow();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001317 // save the WebView to call destroy() after detach it from the tab
1318 WebView webView = mMainView;
1319 setWebView(null);
1320 webView.destroy();
1321 }
1322 }
1323
1324 /**
1325 * Remove the tab from the parent
1326 */
1327 void removeFromTree() {
1328 // detach the children
Michael Kolbc831b632011-05-11 09:30:34 -07001329 if (mChildren != null) {
1330 for(Tab t : mChildren) {
1331 t.setParent(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001332 }
1333 }
1334 // remove itself from the parent list
Michael Kolbc831b632011-05-11 09:30:34 -07001335 if (mParent != null) {
1336 mParent.mChildren.remove(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001337 }
John Reck1cf4b792011-07-26 10:22:22 -07001338 deleteThumbnail();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001339 }
1340
1341 /**
1342 * Create a new subwindow unless a subwindow already exists.
1343 * @return True if a new subwindow was created. False if one already exists.
1344 */
1345 boolean createSubWindow() {
1346 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001347 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001348 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001349 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001350 mSubView.setWebChromeClient(new SubWindowChromeClient(
1351 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001352 // Set a different DownloadListener for the mSubView, since it will
1353 // just need to dismiss the mSubView, rather than close the Tab
1354 mSubView.setDownloadListener(new DownloadListener() {
1355 public void onDownloadStart(String url, String userAgent,
1356 String contentDisposition, String mimetype,
1357 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001358 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001359 contentDisposition, mimetype, contentLength);
1360 if (mSubView.copyBackForwardList().getSize() == 0) {
1361 // This subwindow was opened for the sole purpose of
1362 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001363 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001364 }
1365 }
1366 });
Michael Kolb14612442011-06-24 13:06:29 -07001367 mSubView.setOnCreateContextMenuListener(mWebViewController.getActivity());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001368 return true;
1369 }
1370 return false;
1371 }
1372
1373 /**
1374 * Dismiss the subWindow for the tab.
1375 */
1376 void dismissSubWindow() {
1377 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001378 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001379 mSubView.destroy();
1380 mSubView = null;
1381 mSubViewContainer = null;
1382 }
1383 }
1384
Grace Kloba22ac16e2009-10-07 18:00:23 -07001385
1386 /**
1387 * Set the parent tab of this tab.
1388 */
Michael Kolbc831b632011-05-11 09:30:34 -07001389 void setParent(Tab parent) {
John Reck52be4782011-08-26 15:37:29 -07001390 if (parent == this) {
1391 throw new IllegalStateException("Cannot set parent to self!");
1392 }
Michael Kolbc831b632011-05-11 09:30:34 -07001393 mParent = parent;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001394 // This tab may have been freed due to low memory. If that is the case,
Michael Kolbc831b632011-05-11 09:30:34 -07001395 // the parent tab id is already saved. If we are changing that id
Grace Kloba22ac16e2009-10-07 18:00:23 -07001396 // (most likely due to removing the parent tab) we must update the
Michael Kolbc831b632011-05-11 09:30:34 -07001397 // parent tab id in the saved Bundle.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001398 if (mSavedState != null) {
1399 if (parent == null) {
1400 mSavedState.remove(PARENTTAB);
1401 } else {
Michael Kolbc831b632011-05-11 09:30:34 -07001402 mSavedState.putLong(PARENTTAB, parent.getId());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001403 }
1404 }
John Reckb0a86db2011-05-24 14:05:58 -07001405
1406 // Sync the WebView useragent with the parent
1407 if (parent != null && mSettings.hasDesktopUseragent(parent.getWebView())
1408 != mSettings.hasDesktopUseragent(getWebView())) {
1409 mSettings.toggleDesktopUseragent(getWebView());
1410 }
John Reck52be4782011-08-26 15:37:29 -07001411
1412 if (parent != null && parent.getId() == getId()) {
1413 throw new IllegalStateException("Parent has same ID as child!");
1414 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001415 }
1416
1417 /**
Michael Kolbc831b632011-05-11 09:30:34 -07001418 * If this Tab was created through another Tab, then this method returns
1419 * that Tab.
1420 * @return the Tab parent or null
1421 */
1422 public Tab getParent() {
1423 return mParent;
1424 }
1425
1426 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001427 * When a Tab is created through the content of another Tab, then we
1428 * associate the Tabs.
1429 * @param child the Tab that was created from this Tab
1430 */
1431 void addChildTab(Tab child) {
Michael Kolbc831b632011-05-11 09:30:34 -07001432 if (mChildren == null) {
1433 mChildren = new Vector<Tab>();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001434 }
Michael Kolbc831b632011-05-11 09:30:34 -07001435 mChildren.add(child);
1436 child.setParent(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001437 }
1438
Michael Kolbc831b632011-05-11 09:30:34 -07001439 Vector<Tab> getChildren() {
1440 return mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001441 }
1442
1443 void resume() {
1444 if (mMainView != null) {
John Reck56c1fcf2011-08-17 10:15:16 -07001445 setupHwAcceleration(mMainView);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001446 mMainView.onResume();
1447 if (mSubView != null) {
1448 mSubView.onResume();
1449 }
1450 }
1451 }
1452
John Reck56c1fcf2011-08-17 10:15:16 -07001453 private void setupHwAcceleration(View web) {
1454 if (web == null) return;
1455 BrowserSettings settings = BrowserSettings.getInstance();
1456 if (settings.isHardwareAccelerated()) {
1457 web.setLayerType(View.LAYER_TYPE_NONE, null);
1458 } else {
1459 web.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
1460 }
1461 }
1462
Grace Kloba22ac16e2009-10-07 18:00:23 -07001463 void pause() {
1464 if (mMainView != null) {
1465 mMainView.onPause();
1466 if (mSubView != null) {
1467 mSubView.onPause();
1468 }
1469 }
1470 }
1471
1472 void putInForeground() {
John Reck8ee633f2011-08-09 16:00:35 -07001473 if (mInForeground) {
1474 return;
1475 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001476 mInForeground = true;
1477 resume();
Michael Kolb14612442011-06-24 13:06:29 -07001478 Activity activity = mWebViewController.getActivity();
1479 mMainView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001480 if (mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -07001481 mSubView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001482 }
1483 // Show the pending error dialog if the queue is not empty
1484 if (mQueuedErrors != null && mQueuedErrors.size() > 0) {
1485 showError(mQueuedErrors.getFirst());
1486 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001487 mWebViewController.bookmarkedStatusHasChanged(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001488 }
1489
1490 void putInBackground() {
John Reck8ee633f2011-08-09 16:00:35 -07001491 if (!mInForeground) {
1492 return;
1493 }
John Reck52be4782011-08-26 15:37:29 -07001494 capture();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001495 mInForeground = false;
1496 pause();
1497 mMainView.setOnCreateContextMenuListener(null);
1498 if (mSubView != null) {
1499 mSubView.setOnCreateContextMenuListener(null);
1500 }
1501 }
1502
Michael Kolb8233fac2010-10-26 16:08:53 -07001503 boolean inForeground() {
1504 return mInForeground;
1505 }
1506
Grace Kloba22ac16e2009-10-07 18:00:23 -07001507 /**
1508 * Return the top window of this tab; either the subwindow if it is not
1509 * null or the main window.
1510 * @return The top window of this tab.
1511 */
1512 WebView getTopWindow() {
1513 if (mSubView != null) {
1514 return mSubView;
1515 }
1516 return mMainView;
1517 }
1518
1519 /**
1520 * Return the main window of this tab. Note: if a tab is freed in the
1521 * background, this can return null. It is only guaranteed to be
1522 * non-null for the current tab.
1523 * @return The main WebView of this tab.
1524 */
1525 WebView getWebView() {
1526 return mMainView;
1527 }
1528
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00001529 /**
1530 * Return the underlying WebViewClassic implementation. As with getWebView,
1531 * this maybe null for background tabs.
1532 * @return The main WebView of this tab.
1533 */
1534 WebViewClassic getWebViewClassic() {
1535 return WebViewClassic.fromWebView(mMainView);
1536 }
1537
Michael Kolba713ec82010-11-29 17:27:06 -08001538 void setViewContainer(View container) {
1539 mContainer = container;
1540 }
1541
Michael Kolb8233fac2010-10-26 16:08:53 -07001542 View getViewContainer() {
1543 return mContainer;
1544 }
1545
Grace Kloba22ac16e2009-10-07 18:00:23 -07001546 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001547 * Return whether private browsing is enabled for the main window of
1548 * this tab.
1549 * @return True if private browsing is enabled.
1550 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001551 boolean isPrivateBrowsingEnabled() {
John Reck502a3532011-08-16 14:21:46 -07001552 return mCurrentState.mIncognito;
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001553 }
1554
1555 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001556 * Return the subwindow of this tab or null if there is no subwindow.
1557 * @return The subwindow of this tab or null.
1558 */
1559 WebView getSubWebView() {
1560 return mSubView;
1561 }
1562
Michael Kolb1514bb72010-11-22 09:11:48 -08001563 void setSubWebView(WebView subView) {
1564 mSubView = subView;
1565 }
1566
Michael Kolb8233fac2010-10-26 16:08:53 -07001567 View getSubViewContainer() {
1568 return mSubViewContainer;
1569 }
1570
Michael Kolb1514bb72010-11-22 09:11:48 -08001571 void setSubViewContainer(View subViewContainer) {
1572 mSubViewContainer = subViewContainer;
1573 }
1574
Grace Kloba22ac16e2009-10-07 18:00:23 -07001575 /**
1576 * @return The geolocation permissions prompt for this tab.
1577 */
1578 GeolocationPermissionsPrompt getGeolocationPermissionsPrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001579 if (mGeolocationPermissionsPrompt == null) {
1580 ViewStub stub = (ViewStub) mContainer
1581 .findViewById(R.id.geolocation_permissions_prompt);
1582 mGeolocationPermissionsPrompt = (GeolocationPermissionsPrompt) stub
1583 .inflate();
Grace Kloba50c241e2010-04-20 11:07:50 -07001584 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001585 return mGeolocationPermissionsPrompt;
1586 }
1587
1588 /**
1589 * @return The application id string
1590 */
1591 String getAppId() {
1592 return mAppId;
1593 }
1594
1595 /**
1596 * Set the application id string
1597 * @param id
1598 */
1599 void setAppId(String id) {
1600 mAppId = id;
1601 }
1602
Michael Kolbe28b3472011-08-04 16:54:31 -07001603 boolean closeOnBack() {
1604 return mCloseOnBack;
1605 }
1606
1607 void setCloseOnBack(boolean close) {
1608 mCloseOnBack = close;
1609 }
1610
Grace Kloba22ac16e2009-10-07 18:00:23 -07001611 String getUrl() {
John Reck324d4402011-01-11 16:56:42 -08001612 return UrlUtils.filteredUrl(mCurrentState.mUrl);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001613 }
1614
John Reck49a603c2011-03-03 09:33:05 -08001615 String getOriginalUrl() {
John Reckdb22ec42011-06-29 11:31:24 -07001616 if (mCurrentState.mOriginalUrl == null) {
1617 return getUrl();
John Reck49a603c2011-03-03 09:33:05 -08001618 }
John Reckdb22ec42011-06-29 11:31:24 -07001619 return UrlUtils.filteredUrl(mCurrentState.mOriginalUrl);
John Reck49a603c2011-03-03 09:33:05 -08001620 }
1621
Grace Kloba22ac16e2009-10-07 18:00:23 -07001622 /**
John Reck30c714c2010-12-16 17:30:34 -08001623 * Get the title of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001624 */
1625 String getTitle() {
John Reck30c714c2010-12-16 17:30:34 -08001626 if (mCurrentState.mTitle == null && mInPageLoad) {
Michael Kolb14612442011-06-24 13:06:29 -07001627 return mContext.getString(R.string.title_bar_loading);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001628 }
John Reck30c714c2010-12-16 17:30:34 -08001629 return mCurrentState.mTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001630 }
1631
1632 /**
John Reck30c714c2010-12-16 17:30:34 -08001633 * Get the favicon of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001634 */
1635 Bitmap getFavicon() {
John Reck1cf4b792011-07-26 10:22:22 -07001636 if (mCurrentState.mFavicon != null) {
1637 return mCurrentState.mFavicon;
1638 }
1639 return getDefaultFavicon(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001640 }
1641
John Recke969cc52010-12-21 17:24:43 -08001642 public boolean isBookmarkedSite() {
1643 return mCurrentState.mIsBookmarkedSite;
1644 }
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001645
Grace Kloba22ac16e2009-10-07 18:00:23 -07001646 /**
1647 * Return the tab's error console. Creates the console if createIfNEcessary
1648 * is true and we haven't already created the console.
1649 * @param createIfNecessary Flag to indicate if the console should be
1650 * created if it has not been already.
1651 * @return The tab's error console, or null if one has not been created and
1652 * createIfNecessary is false.
1653 */
1654 ErrorConsoleView getErrorConsole(boolean createIfNecessary) {
1655 if (createIfNecessary && mErrorConsole == null) {
Michael Kolb14612442011-06-24 13:06:29 -07001656 mErrorConsole = new ErrorConsoleView(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001657 mErrorConsole.setWebView(mMainView);
1658 }
1659 return mErrorConsole;
1660 }
1661
Steve Block08a6f0c2011-10-06 12:12:53 +01001662 /**
1663 * Sets the security state, clears the SSL certificate error and informs
1664 * the controller.
1665 */
Steve Block2466eff2011-10-03 15:33:09 +01001666 private void setSecurityState(SecurityState securityState) {
1667 mCurrentState.mSecurityState = securityState;
Steve Block08a6f0c2011-10-06 12:12:53 +01001668 mCurrentState.mSslCertificateError = null;
Steve Block2466eff2011-10-03 15:33:09 +01001669 mWebViewController.onUpdatedSecurityState(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001670 }
1671
1672 /**
Steve Block2466eff2011-10-03 15:33:09 +01001673 * @return The tab's security state.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001674 */
Steve Block2466eff2011-10-03 15:33:09 +01001675 SecurityState getSecurityState() {
1676 return mCurrentState.mSecurityState;
John Reck30c714c2010-12-16 17:30:34 -08001677 }
1678
Steve Block08a6f0c2011-10-06 12:12:53 +01001679 /**
1680 * Gets the SSL certificate error, if any, for the page's main resource.
1681 * This is only non-null when the security state is
1682 * SECURITY_STATE_BAD_CERTIFICATE.
1683 */
1684 SslError getSslCertificateError() {
1685 return mCurrentState.mSslCertificateError;
1686 }
1687
John Reck30c714c2010-12-16 17:30:34 -08001688 int getLoadProgress() {
1689 if (mInPageLoad) {
1690 return mPageLoadProgress;
1691 }
1692 return 100;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001693 }
1694
1695 /**
1696 * @return TRUE if onPageStarted is called while onPageFinished is not
1697 * called yet.
1698 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001699 boolean inPageLoad() {
1700 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001701 }
1702
Grace Kloba22ac16e2009-10-07 18:00:23 -07001703 /**
John Reck1cf4b792011-07-26 10:22:22 -07001704 * @return The Bundle with the tab's state if it can be saved, otherwise null
Grace Kloba22ac16e2009-10-07 18:00:23 -07001705 */
John Reck1cf4b792011-07-26 10:22:22 -07001706 public Bundle saveState() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001707 // If the WebView is null it means we ran low on memory and we already
1708 // stored the saved state in mSavedState.
1709 if (mMainView == null) {
John Reck1cf4b792011-07-26 10:22:22 -07001710 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001711 }
John Reck6c2e2f32011-08-22 13:41:23 -07001712
1713 if (TextUtils.isEmpty(mCurrentState.mUrl)) {
John Reck1cf4b792011-07-26 10:22:22 -07001714 return null;
John Reck24f18262011-06-17 14:47:20 -07001715 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001716
1717 mSavedState = new Bundle();
John Reck6c2e2f32011-08-22 13:41:23 -07001718 WebBackForwardList savedList = mMainView.saveState(mSavedState);
1719 if (savedList == null || savedList.getSize() == 0) {
1720 Log.w(LOGTAG, "Failed to save back/forward list for "
1721 + mCurrentState.mUrl);
1722 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001723
Michael Kolbc831b632011-05-11 09:30:34 -07001724 mSavedState.putLong(ID, mId);
John Reck30c714c2010-12-16 17:30:34 -08001725 mSavedState.putString(CURRURL, mCurrentState.mUrl);
1726 mSavedState.putString(CURRTITLE, mCurrentState.mTitle);
John Reck1cf4b792011-07-26 10:22:22 -07001727 mSavedState.putBoolean(INCOGNITO, mMainView.isPrivateBrowsingEnabled());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001728 if (mAppId != null) {
1729 mSavedState.putString(APPID, mAppId);
1730 }
Michael Kolbe28b3472011-08-04 16:54:31 -07001731 mSavedState.putBoolean(CLOSEFLAG, mCloseOnBack);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001732 // Remember the parent tab so the relationship can be restored.
Michael Kolbc831b632011-05-11 09:30:34 -07001733 if (mParent != null) {
1734 mSavedState.putLong(PARENTTAB, mParent.mId);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001735 }
John Reckb0a86db2011-05-24 14:05:58 -07001736 mSavedState.putBoolean(USERAGENT,
1737 mSettings.hasDesktopUseragent(getWebView()));
John Reck1cf4b792011-07-26 10:22:22 -07001738 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001739 }
1740
1741 /*
1742 * Restore the state of the tab.
1743 */
John Reck1cf4b792011-07-26 10:22:22 -07001744 private void restoreState(Bundle b) {
1745 mSavedState = b;
1746 if (mSavedState == null) {
1747 return;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001748 }
1749 // Restore the internal state even if the WebView fails to restore.
1750 // This will maintain the app id, original url and close-on-exit values.
Michael Kolbc831b632011-05-11 09:30:34 -07001751 mId = b.getLong(ID);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001752 mAppId = b.getString(APPID);
Michael Kolbe28b3472011-08-04 16:54:31 -07001753 mCloseOnBack = b.getBoolean(CLOSEFLAG);
John Reck8b9bb8b2012-03-08 13:19:40 -08001754 restoreUserAgent();
John Reck1cf4b792011-07-26 10:22:22 -07001755 String url = b.getString(CURRURL);
1756 String title = b.getString(CURRTITLE);
1757 boolean incognito = b.getBoolean(INCOGNITO);
1758 mCurrentState = new PageState(mContext, incognito, url, null);
1759 mCurrentState.mTitle = title;
1760 synchronized (Tab.this) {
1761 if (mCapture != null) {
John Reck4eadc342011-10-31 14:04:10 -07001762 DataController.getInstance(mContext).loadThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07001763 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001764 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001765 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04001766
John Reck8b9bb8b2012-03-08 13:19:40 -08001767 private void restoreUserAgent() {
1768 if (mMainView == null || mSavedState == null) {
1769 return;
1770 }
1771 if (mSavedState.getBoolean(USERAGENT)
1772 != mSettings.hasDesktopUseragent(mMainView)) {
1773 mSettings.toggleDesktopUseragent(mMainView);
1774 }
1775 }
1776
Leon Scroggins1961ed22010-12-07 15:22:21 -05001777 public void updateBookmarkedStatus() {
John Recke969cc52010-12-21 17:24:43 -08001778 mDataController.queryBookmarkStatus(getUrl(), mIsBookmarkCallback);
Leon Scroggins1961ed22010-12-07 15:22:21 -05001779 }
1780
John Recke969cc52010-12-21 17:24:43 -08001781 private DataController.OnQueryUrlIsBookmark mIsBookmarkCallback
1782 = new DataController.OnQueryUrlIsBookmark() {
1783 @Override
1784 public void onQueryUrlIsBookmark(String url, boolean isBookmark) {
1785 if (mCurrentState.mUrl.equals(url)) {
1786 mCurrentState.mIsBookmarkedSite = isBookmark;
1787 mWebViewController.bookmarkedStatusHasChanged(Tab.this);
1788 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001789 }
John Recke969cc52010-12-21 17:24:43 -08001790 };
Michael Kolb1acef692011-03-08 14:12:06 -08001791
Michael Kolbeb95db42011-03-03 10:38:40 -08001792 public Bitmap getScreenshot() {
John Reck1cf4b792011-07-26 10:22:22 -07001793 synchronized (Tab.this) {
1794 return mCapture;
1795 }
Michael Kolbeb95db42011-03-03 10:38:40 -08001796 }
1797
John Reck541f55a2011-06-07 16:34:43 -07001798 public boolean isSnapshot() {
John Reck541f55a2011-06-07 16:34:43 -07001799 return false;
1800 }
1801
John Reck68234a92012-04-19 15:27:12 -07001802 private static class SaveCallback implements ValueCallback<Boolean> {
1803 boolean mResult;
1804
1805 @Override
1806 public void onReceiveValue(Boolean value) {
1807 mResult = value;
1808 synchronized (this) {
1809 notifyAll();
John Reck8cc92352011-07-06 17:41:52 -07001810 }
John Reck541f55a2011-06-07 16:34:43 -07001811 }
John Reck68234a92012-04-19 15:27:12 -07001812
1813 }
1814
1815 /**
1816 * Must be called on the UI thread
1817 */
1818 public ContentValues createSnapshotValues() {
1819 WebViewClassic web = getWebViewClassic();
1820 if (web == null) return null;
John Reckd8c74522011-06-14 08:45:00 -07001821 ContentValues values = new ContentValues();
1822 values.put(Snapshots.TITLE, mCurrentState.mTitle);
1823 values.put(Snapshots.URL, mCurrentState.mUrl);
John Reck68234a92012-04-19 15:27:12 -07001824 values.put(Snapshots.BACKGROUND, web.getPageBackgroundColor());
John Reck8cc92352011-07-06 17:41:52 -07001825 values.put(Snapshots.DATE_CREATED, System.currentTimeMillis());
1826 values.put(Snapshots.FAVICON, compressBitmap(getFavicon()));
1827 Bitmap screenshot = Controller.createScreenshot(mMainView,
1828 Controller.getDesiredThumbnailWidth(mContext),
1829 Controller.getDesiredThumbnailHeight(mContext));
1830 values.put(Snapshots.THUMBNAIL, compressBitmap(screenshot));
John Reckd8c74522011-06-14 08:45:00 -07001831 return values;
John Reck541f55a2011-06-07 16:34:43 -07001832 }
1833
John Reck68234a92012-04-19 15:27:12 -07001834 /**
1835 * Probably want to call this on a background thread
1836 */
1837 public boolean saveViewState(ContentValues values) {
1838 WebViewClassic web = getWebViewClassic();
1839 if (web == null) return false;
1840 String path = UUID.randomUUID().toString();
1841 SaveCallback callback = new SaveCallback();
1842 OutputStream outs = null;
1843 try {
1844 outs = mContext.openFileOutput(path, Context.MODE_PRIVATE);
1845 GZIPOutputStream stream = new GZIPOutputStream(outs);
1846 synchronized (callback) {
1847 web.saveViewState(stream, callback);
1848 callback.wait();
1849 }
1850 stream.flush();
1851 stream.close();
1852 } catch (Exception e) {
1853 Log.w(LOGTAG, "Failed to save view state", e);
1854 if (outs != null) {
1855 try {
1856 outs.close();
1857 } catch (IOException ignore) {}
1858 }
1859 File file = mContext.getFileStreamPath(path);
1860 if (file.exists() && !file.delete()) {
1861 file.deleteOnExit();
1862 }
1863 return false;
1864 }
1865 File savedFile = mContext.getFileStreamPath(path);
1866 if (!callback.mResult) {
1867 if (!savedFile.delete()) {
1868 savedFile.deleteOnExit();
1869 }
1870 return false;
1871 }
1872 long size = savedFile.length();
1873 values.put(Snapshots.VIEWSTATE_PATH, path);
1874 values.put(Snapshots.VIEWSTATE_SIZE, size);
1875 return true;
1876 }
1877
John Reck8cc92352011-07-06 17:41:52 -07001878 public byte[] compressBitmap(Bitmap bitmap) {
1879 if (bitmap == null) {
1880 return null;
1881 }
1882 ByteArrayOutputStream stream = new ByteArrayOutputStream();
1883 bitmap.compress(CompressFormat.PNG, 100, stream);
1884 return stream.toByteArray();
1885 }
1886
John Reck26b18322011-06-21 13:08:58 -07001887 public void loadUrl(String url, Map<String, String> headers) {
1888 if (mMainView != null) {
Michael Kolba53c9892011-10-05 13:31:40 -07001889 mPageLoadProgress = INITIAL_PROGRESS;
1890 mInPageLoad = true;
Michael Kolb14612442011-06-24 13:06:29 -07001891 mCurrentState = new PageState(mContext, false, url, null);
John Reck26b18322011-06-21 13:08:58 -07001892 mWebViewController.onPageStarted(this, mMainView, null);
1893 mMainView.loadUrl(url, headers);
1894 }
1895 }
1896
Michael Kolb9ef259a2011-07-12 15:33:08 -07001897 protected void capture() {
1898 if (mMainView == null || mCapture == null) return;
John Reck4eadc342011-10-31 14:04:10 -07001899 if (mMainView.getContentWidth() <= 0 || mMainView.getContentHeight() <= 0) {
1900 return;
1901 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001902 Canvas c = new Canvas(mCapture);
1903 final int left = mMainView.getScrollX();
1904 final int top = mMainView.getScrollY() + mMainView.getVisibleTitleHeight();
Michael Kolba3194d02011-09-07 11:23:51 -07001905 int state = c.save();
Michael Kolb9ef259a2011-07-12 15:33:08 -07001906 c.translate(-left, -top);
1907 float scale = mCaptureWidth / (float) mMainView.getWidth();
1908 c.scale(scale, scale, left, top);
John Reck8ee633f2011-08-09 16:00:35 -07001909 if (mMainView instanceof BrowserWebView) {
1910 ((BrowserWebView)mMainView).drawContent(c);
1911 } else {
1912 mMainView.draw(c);
1913 }
Michael Kolba3194d02011-09-07 11:23:51 -07001914 c.restoreToCount(state);
1915 // manually anti-alias the edges for the tilt
1916 c.drawRect(0, 0, 1, mCapture.getHeight(), sAlphaPaint);
1917 c.drawRect(mCapture.getWidth() - 1, 0, mCapture.getWidth(),
1918 mCapture.getHeight(), sAlphaPaint);
1919 c.drawRect(0, 0, mCapture.getWidth(), 1, sAlphaPaint);
1920 c.drawRect(0, mCapture.getHeight() - 1, mCapture.getWidth(),
1921 mCapture.getHeight(), sAlphaPaint);
Dianne Hackborn43cfe8a2011-08-02 16:59:35 -07001922 c.setBitmap(null);
John Reck8ee633f2011-08-09 16:00:35 -07001923 mHandler.removeMessages(MSG_CAPTURE);
John Reck1cf4b792011-07-26 10:22:22 -07001924 persistThumbnail();
John Reck8ee633f2011-08-09 16:00:35 -07001925 TabControl tc = mWebViewController.getTabControl();
1926 if (tc != null) {
1927 OnThumbnailUpdatedListener updateListener
1928 = tc.getOnThumbnailUpdatedListener();
1929 if (updateListener != null) {
1930 updateListener.onThumbnailUpdated(this);
1931 }
1932 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001933 }
1934
1935 @Override
1936 public void onNewPicture(WebView view, Picture picture) {
John Reck1cf4b792011-07-26 10:22:22 -07001937 postCapture();
1938 }
1939
1940 private void postCapture() {
Michael Kolb9ef259a2011-07-12 15:33:08 -07001941 if (!mHandler.hasMessages(MSG_CAPTURE)) {
1942 mHandler.sendEmptyMessageDelayed(MSG_CAPTURE, CAPTURE_DELAY);
1943 }
1944 }
1945
John Reckef654f12011-07-12 16:42:08 -07001946 public boolean canGoBack() {
1947 return mMainView != null ? mMainView.canGoBack() : false;
1948 }
1949
1950 public boolean canGoForward() {
1951 return mMainView != null ? mMainView.canGoForward() : false;
1952 }
1953
1954 public void goBack() {
1955 if (mMainView != null) {
1956 mMainView.goBack();
1957 }
1958 }
1959
1960 public void goForward() {
1961 if (mMainView != null) {
1962 mMainView.goForward();
1963 }
1964 }
1965
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001966 /**
1967 * Causes the tab back/forward stack to be cleared once, if the given URL is the next URL
1968 * to be added to the stack.
1969 *
1970 * This is used to ensure that preloaded URLs that are not subsequently seen by the user do
1971 * not appear in the back stack.
1972 */
Mathew Inwooda829d552011-09-02 14:16:25 +01001973 public void clearBackStackWhenItemAdded(Pattern urlPattern) {
1974 mClearHistoryUrlPattern = urlPattern;
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001975 }
1976
John Reck1cf4b792011-07-26 10:22:22 -07001977 protected void persistThumbnail() {
John Reck4eadc342011-10-31 14:04:10 -07001978 DataController.getInstance(mContext).saveThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07001979 }
1980
1981 protected void deleteThumbnail() {
John Reck4eadc342011-10-31 14:04:10 -07001982 DataController.getInstance(mContext).deleteThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07001983 }
1984
John Reck4eadc342011-10-31 14:04:10 -07001985 void updateCaptureFromBlob(byte[] blob) {
John Reck1cf4b792011-07-26 10:22:22 -07001986 synchronized (Tab.this) {
1987 if (mCapture == null) {
1988 return;
1989 }
Michael Kolbbd4c00a2011-08-02 11:27:12 -07001990 ByteBuffer buffer = ByteBuffer.wrap(blob);
Michael Kolbd837a112011-08-09 14:00:18 -07001991 try {
1992 mCapture.copyPixelsFromBuffer(buffer);
1993 } catch (RuntimeException rex) {
1994 Log.e(LOGTAG, "Load capture has mismatched sizes; buffer: "
1995 + buffer.capacity() + " blob: " + blob.length
1996 + "capture: " + mCapture.getByteCount());
1997 throw rex;
Michael Kolbbd4c00a2011-08-02 11:27:12 -07001998 }
John Reck1cf4b792011-07-26 10:22:22 -07001999 }
2000 }
2001
John Reck52be4782011-08-26 15:37:29 -07002002 @Override
2003 public String toString() {
2004 StringBuilder builder = new StringBuilder(100);
2005 builder.append(mId);
2006 builder.append(") has parent: ");
2007 if (getParent() != null) {
2008 builder.append("true[");
2009 builder.append(getParent().getId());
2010 builder.append("]");
2011 } else {
2012 builder.append("false");
2013 }
2014 builder.append(", incog: ");
2015 builder.append(isPrivateBrowsingEnabled());
2016 if (!isPrivateBrowsingEnabled()) {
2017 builder.append(", title: ");
2018 builder.append(getTitle());
2019 builder.append(", url: ");
2020 builder.append(getUrl());
2021 }
2022 return builder.toString();
2023 }
2024
Steve Block4895b012011-10-03 16:26:46 +01002025 private void handleProceededAfterSslError(SslError error) {
2026 if (error.getUrl().equals(mCurrentState.mUrl)) {
2027 // The security state should currently be SECURITY_STATE_SECURE.
2028 setSecurityState(SecurityState.SECURITY_STATE_BAD_CERTIFICATE);
Steve Block08a6f0c2011-10-06 12:12:53 +01002029 mCurrentState.mSslCertificateError = error;
Steve Block4895b012011-10-03 16:26:46 +01002030 } else if (getSecurityState() == SecurityState.SECURITY_STATE_SECURE) {
Steve Block08a6f0c2011-10-06 12:12:53 +01002031 // The page's main resource is secure and this error is for a
2032 // sub-resource.
Steve Block4895b012011-10-03 16:26:46 +01002033 setSecurityState(SecurityState.SECURITY_STATE_MIXED);
2034 }
2035 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002036}