blob: 0cf598fc76863978288af912bd49e7469da5b7af [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;
Jonathan Dixonf4ff1492012-06-08 10:15:32 -070067import android.webkit.WebViewClientClassicExt;
Ben Murdoch1d676b62011-01-17 12:54:24 +000068import android.widget.CheckBox;
Ben Murdoch8029a772010-11-16 11:58:21 +000069import android.widget.Toast;
Grace Kloba22ac16e2009-10-07 18:00:23 -070070
John Reck8ee633f2011-08-09 16:00:35 -070071import com.android.browser.TabControl.OnThumbnailUpdatedListener;
John Reck541f55a2011-06-07 16:34:43 -070072import com.android.browser.homepages.HomeProvider;
John Reck8cc92352011-07-06 17:41:52 -070073import com.android.browser.provider.SnapshotProvider.Snapshots;
John Reck541f55a2011-06-07 16:34:43 -070074
75import java.io.ByteArrayOutputStream;
John Reck2b71d6d2012-04-18 17:42:06 -070076import java.io.File;
John Reck68234a92012-04-19 15:27:12 -070077import java.io.IOException;
John Reck2b71d6d2012-04-18 17:42:06 -070078import java.io.OutputStream;
John Reck1cf4b792011-07-26 10:22:22 -070079import java.nio.ByteBuffer;
Michael Kolbfe251992010-07-08 15:41:55 -070080import java.util.LinkedList;
81import java.util.Map;
John Reck2b71d6d2012-04-18 17:42:06 -070082import java.util.UUID;
Michael Kolbfe251992010-07-08 15:41:55 -070083import java.util.Vector;
Mathew Inwooda829d552011-09-02 14:16:25 +010084import java.util.regex.Pattern;
John Reck8cc92352011-07-06 17:41:52 -070085import java.util.zip.GZIPOutputStream;
Michael Kolbfe251992010-07-08 15:41:55 -070086
Grace Kloba22ac16e2009-10-07 18:00:23 -070087/**
88 * Class for maintaining Tabs with a main WebView and a subwindow.
89 */
Michael Kolb9ef259a2011-07-12 15:33:08 -070090class Tab implements PictureListener {
Michael Kolb8233fac2010-10-26 16:08:53 -070091
Grace Kloba22ac16e2009-10-07 18:00:23 -070092 // Log Tag
93 private static final String LOGTAG = "Tab";
Mathew Inwood1dd8e822011-08-03 14:34:29 +010094 private static final boolean LOGD_ENABLED = com.android.browser.Browser.LOGD_ENABLED;
Ben Murdochc42addf2010-01-28 15:19:59 +000095 // Special case the logtag for messages for the Console to make it easier to
96 // filter them and match the logtag used for these messages in older versions
97 // of the browser.
98 private static final String CONSOLE_LOGTAG = "browser";
99
Michael Kolb9ef259a2011-07-12 15:33:08 -0700100 private static final int MSG_CAPTURE = 42;
John Reck8ee633f2011-08-09 16:00:35 -0700101 private static final int CAPTURE_DELAY = 100;
Michael Kolba53c9892011-10-05 13:31:40 -0700102 private static final int INITIAL_PROGRESS = 5;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700103
John Reck1cf4b792011-07-26 10:22:22 -0700104 private static Bitmap sDefaultFavicon;
105
Michael Kolba3194d02011-09-07 11:23:51 -0700106 private static Paint sAlphaPaint = new Paint();
107 static {
108 sAlphaPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
109 sAlphaPaint.setColor(Color.TRANSPARENT);
110 }
111
Steve Block2466eff2011-10-03 15:33:09 +0100112 public enum SecurityState {
Steve Block4895b012011-10-03 16:26:46 +0100113 // The page's main resource does not use SSL. Note that we use this
114 // state irrespective of the SSL authentication state of sub-resources.
Steve Block2466eff2011-10-03 15:33:09 +0100115 SECURITY_STATE_NOT_SECURE,
Steve Block4895b012011-10-03 16:26:46 +0100116 // The page's main resource uses SSL and the certificate is good. The
117 // same is true of all sub-resources.
Steve Block2466eff2011-10-03 15:33:09 +0100118 SECURITY_STATE_SECURE,
Steve Block4895b012011-10-03 16:26:46 +0100119 // The page's main resource uses SSL and the certificate is good, but
120 // some sub-resources either do not use SSL or have problems with their
121 // certificates.
Steve Block2466eff2011-10-03 15:33:09 +0100122 SECURITY_STATE_MIXED,
Steve Block4895b012011-10-03 16:26:46 +0100123 // The page's main resource uses SSL but there is a problem with its
124 // certificate.
125 SECURITY_STATE_BAD_CERTIFICATE,
John Reck30c714c2010-12-16 17:30:34 -0800126 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700127
Michael Kolb14612442011-06-24 13:06:29 -0700128 Context mContext;
John Reckd8c74522011-06-14 08:45:00 -0700129 protected WebViewController mWebViewController;
Michael Kolb8233fac2010-10-26 16:08:53 -0700130
Michael Kolbc831b632011-05-11 09:30:34 -0700131 // The tab ID
John Reckd8c74522011-06-14 08:45:00 -0700132 private long mId = -1;
Michael Kolbc831b632011-05-11 09:30:34 -0700133
Grace Kloba22ac16e2009-10-07 18:00:23 -0700134 // The Geolocation permissions prompt
135 private GeolocationPermissionsPrompt mGeolocationPermissionsPrompt;
136 // Main WebView wrapper
Michael Kolba713ec82010-11-29 17:27:06 -0800137 private View mContainer;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700138 // Main WebView
139 private WebView mMainView;
140 // Subwindow container
141 private View mSubViewContainer;
142 // Subwindow WebView
143 private WebView mSubView;
144 // Saved bundle for when we are running low on memory. It contains the
145 // information needed to restore the WebView if the user goes back to the
146 // tab.
147 private Bundle mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700148 // Parent Tab. This is the Tab that created this Tab, or null if the Tab was
149 // created by the UI
Michael Kolbc831b632011-05-11 09:30:34 -0700150 private Tab mParent;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700151 // Tab that constructed by this Tab. This is used when this Tab is
152 // destroyed, it clears all mParentTab values in the children.
Michael Kolbc831b632011-05-11 09:30:34 -0700153 private Vector<Tab> mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700154 // If true, the tab is in the foreground of the current activity.
155 private boolean mInForeground;
Michael Kolb8233fac2010-10-26 16:08:53 -0700156 // If true, the tab is in page loading state (after onPageStarted,
157 // before onPageFinsihed)
158 private boolean mInPageLoad;
John Reck30c714c2010-12-16 17:30:34 -0800159 // The last reported progress of the current page
160 private int mPageLoadProgress;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000161 // The time the load started, used to find load page time
162 private long mLoadStartTime;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700163 // Application identifier used to find tabs that another application wants
164 // to reuse.
165 private String mAppId;
Michael Kolbe28b3472011-08-04 16:54:31 -0700166 // flag to indicate if tab should be closed on back
167 private boolean mCloseOnBack;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700168 // Keep the original url around to avoid killing the old WebView if the url
169 // has not changed.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700170 // Error console for the tab
171 private ErrorConsoleView mErrorConsole;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -0500172 // The listener that gets invoked when a download is started from the
173 // mMainView
174 private final DownloadListener mDownloadListener;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500175 // Listener used to know when we move forward or back in the history list.
176 private final WebBackForwardListClient mWebBackForwardListClient;
John Recke969cc52010-12-21 17:24:43 -0800177 private DataController mDataController;
Patrick Scott92066772011-03-10 08:46:27 -0500178 // State of the auto-login request.
179 private DeviceAccountLogin mDeviceAccountLogin;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700180
181 // AsyncTask for downloading touch icons
182 DownloadTouchIcon mTouchIconLoader;
183
John Reck35e9dd62011-04-25 09:01:54 -0700184 private BrowserSettings mSettings;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700185 private int mCaptureWidth;
186 private int mCaptureHeight;
187 private Bitmap mCapture;
188 private Handler mHandler;
Michael Kolb72864272012-05-03 15:42:15 -0700189 private boolean mUpdateThumbnail;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700190
Mathew Inwood1dd8e822011-08-03 14:34:29 +0100191 /**
Mathew Inwood9ad1eac2011-09-15 11:29:50 +0100192 * See {@link #clearBackStackWhenItemAdded(String)}.
Mathew Inwood1dd8e822011-08-03 14:34:29 +0100193 */
Mathew Inwooda829d552011-09-02 14:16:25 +0100194 private Pattern mClearHistoryUrlPattern;
Mathew Inwood1dd8e822011-08-03 14:34:29 +0100195
John Reck1cf4b792011-07-26 10:22:22 -0700196 private static synchronized Bitmap getDefaultFavicon(Context context) {
197 if (sDefaultFavicon == null) {
198 sDefaultFavicon = BitmapFactory.decodeResource(
199 context.getResources(), R.drawable.app_web_browser_sm);
200 }
201 return sDefaultFavicon;
202 }
Michael Kolbeb95db42011-03-03 10:38:40 -0800203
John Reck30c714c2010-12-16 17:30:34 -0800204 // All the state needed for a page
John Reckd8c74522011-06-14 08:45:00 -0700205 protected static class PageState {
John Reck30c714c2010-12-16 17:30:34 -0800206 String mUrl;
John Reckdb22ec42011-06-29 11:31:24 -0700207 String mOriginalUrl;
John Reck30c714c2010-12-16 17:30:34 -0800208 String mTitle;
Steve Block2466eff2011-10-03 15:33:09 +0100209 SecurityState mSecurityState;
Steve Block08a6f0c2011-10-06 12:12:53 +0100210 // This is non-null only when mSecurityState is SECURITY_STATE_BAD_CERTIFICATE.
211 SslError mSslCertificateError;
John Reck30c714c2010-12-16 17:30:34 -0800212 Bitmap mFavicon;
Steve Block08a6f0c2011-10-06 12:12:53 +0100213 boolean mIsBookmarkedSite;
214 boolean mIncognito;
John Reck30c714c2010-12-16 17:30:34 -0800215
216 PageState(Context c, boolean incognito) {
John Reck502a3532011-08-16 14:21:46 -0700217 mIncognito = incognito;
218 if (mIncognito) {
John Reckdb22ec42011-06-29 11:31:24 -0700219 mOriginalUrl = mUrl = "browser:incognito";
John Reck30c714c2010-12-16 17:30:34 -0800220 mTitle = c.getString(R.string.new_incognito_tab);
John Reck30c714c2010-12-16 17:30:34 -0800221 } else {
John Reckdb22ec42011-06-29 11:31:24 -0700222 mOriginalUrl = mUrl = "";
John Reck30c714c2010-12-16 17:30:34 -0800223 mTitle = c.getString(R.string.new_tab);
John Reck30c714c2010-12-16 17:30:34 -0800224 }
Steve Block2466eff2011-10-03 15:33:09 +0100225 mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
John Reck30c714c2010-12-16 17:30:34 -0800226 }
227
228 PageState(Context c, boolean incognito, String url, Bitmap favicon) {
John Reck502a3532011-08-16 14:21:46 -0700229 mIncognito = incognito;
John Reckdb22ec42011-06-29 11:31:24 -0700230 mOriginalUrl = mUrl = url;
John Reck30c714c2010-12-16 17:30:34 -0800231 if (URLUtil.isHttpsUrl(url)) {
Steve Block2466eff2011-10-03 15:33:09 +0100232 mSecurityState = SecurityState.SECURITY_STATE_SECURE;
John Reck30c714c2010-12-16 17:30:34 -0800233 } else {
Steve Block2466eff2011-10-03 15:33:09 +0100234 mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
John Reck30c714c2010-12-16 17:30:34 -0800235 }
John Reck1cf4b792011-07-26 10:22:22 -0700236 mFavicon = favicon;
John Reck30c714c2010-12-16 17:30:34 -0800237 }
John Reck1cf4b792011-07-26 10:22:22 -0700238
Grace Kloba22ac16e2009-10-07 18:00:23 -0700239 }
240
John Reck30c714c2010-12-16 17:30:34 -0800241 // The current/loading page's state
John Reckd8c74522011-06-14 08:45:00 -0700242 protected PageState mCurrentState;
John Reck30c714c2010-12-16 17:30:34 -0800243
Grace Kloba22ac16e2009-10-07 18:00:23 -0700244 // Used for saving and restoring each Tab
Michael Kolbc831b632011-05-11 09:30:34 -0700245 static final String ID = "ID";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700246 static final String CURRURL = "currentUrl";
247 static final String CURRTITLE = "currentTitle";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700248 static final String PARENTTAB = "parentTab";
249 static final String APPID = "appid";
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700250 static final String INCOGNITO = "privateBrowsingEnabled";
John Reckb0a86db2011-05-24 14:05:58 -0700251 static final String USERAGENT = "useragent";
Michael Kolbe28b3472011-08-04 16:54:31 -0700252 static final String CLOSEFLAG = "closeOnBack";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700253
Grace Kloba22ac16e2009-10-07 18:00:23 -0700254 // Container class for the next error dialog that needs to be displayed
255 private class ErrorDialog {
256 public final int mTitle;
257 public final String mDescription;
258 public final int mError;
259 ErrorDialog(int title, String desc, int error) {
260 mTitle = title;
261 mDescription = desc;
262 mError = error;
263 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700264 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700265
266 private void processNextError() {
267 if (mQueuedErrors == null) {
268 return;
269 }
270 // The first one is currently displayed so just remove it.
271 mQueuedErrors.removeFirst();
272 if (mQueuedErrors.size() == 0) {
273 mQueuedErrors = null;
274 return;
275 }
276 showError(mQueuedErrors.getFirst());
277 }
278
279 private DialogInterface.OnDismissListener mDialogListener =
280 new DialogInterface.OnDismissListener() {
281 public void onDismiss(DialogInterface d) {
282 processNextError();
283 }
284 };
285 private LinkedList<ErrorDialog> mQueuedErrors;
286
287 private void queueError(int err, String desc) {
288 if (mQueuedErrors == null) {
289 mQueuedErrors = new LinkedList<ErrorDialog>();
290 }
291 for (ErrorDialog d : mQueuedErrors) {
292 if (d.mError == err) {
293 // Already saw a similar error, ignore the new one.
294 return;
295 }
296 }
297 ErrorDialog errDialog = new ErrorDialog(
298 err == WebViewClient.ERROR_FILE_NOT_FOUND ?
299 R.string.browserFrameFileErrorLabel :
300 R.string.browserFrameNetworkErrorLabel,
301 desc, err);
302 mQueuedErrors.addLast(errDialog);
303
304 // Show the dialog now if the queue was empty and it is in foreground
305 if (mQueuedErrors.size() == 1 && mInForeground) {
306 showError(errDialog);
307 }
308 }
309
310 private void showError(ErrorDialog errDialog) {
311 if (mInForeground) {
Michael Kolb14612442011-06-24 13:06:29 -0700312 AlertDialog d = new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700313 .setTitle(errDialog.mTitle)
314 .setMessage(errDialog.mDescription)
315 .setPositiveButton(R.string.ok, null)
316 .create();
317 d.setOnDismissListener(mDialogListener);
318 d.show();
319 }
320 }
321
322 // -------------------------------------------------------------------------
323 // WebViewClient implementation for the main WebView
324 // -------------------------------------------------------------------------
325
Jonathan Dixonf4ff1492012-06-08 10:15:32 -0700326 private final WebViewClientClassicExt mWebViewClient = new WebViewClientClassicExt() {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500327 private Message mDontResend;
328 private Message mResend;
Michael Kolb47bd1e42011-09-01 15:25:00 -0700329
330 private boolean providersDiffer(String url, String otherUrl) {
331 Uri uri1 = Uri.parse(url);
332 Uri uri2 = Uri.parse(otherUrl);
333 return !uri1.getEncodedAuthority().equals(uri2.getEncodedAuthority());
334 }
335
Grace Kloba22ac16e2009-10-07 18:00:23 -0700336 @Override
337 public void onPageStarted(WebView view, String url, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700338 mInPageLoad = true;
Michael Kolb72864272012-05-03 15:42:15 -0700339 mUpdateThumbnail = true;
Michael Kolba53c9892011-10-05 13:31:40 -0700340 mPageLoadProgress = INITIAL_PROGRESS;
Michael Kolb14612442011-06-24 13:06:29 -0700341 mCurrentState = new PageState(mContext,
John Reck30c714c2010-12-16 17:30:34 -0800342 view.isPrivateBrowsingEnabled(), url, favicon);
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000343 mLoadStartTime = SystemClock.uptimeMillis();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700344
345 // If we start a touch icon load and then load a new page, we don't
346 // want to cancel the current touch icon loader. But, we do want to
347 // create a new one when the touch icon url is known.
348 if (mTouchIconLoader != null) {
349 mTouchIconLoader.mTab = null;
350 mTouchIconLoader = null;
351 }
352
353 // reset the error console
354 if (mErrorConsole != null) {
355 mErrorConsole.clearErrorMessages();
Michael Kolb8233fac2010-10-26 16:08:53 -0700356 if (mWebViewController.shouldShowErrorConsole()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700357 mErrorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
358 }
359 }
360
Patrick Scott92066772011-03-10 08:46:27 -0500361 // Cancel the auto-login process.
362 if (mDeviceAccountLogin != null) {
363 mDeviceAccountLogin.cancel();
364 mDeviceAccountLogin = null;
365 mWebViewController.hideAutoLogin(Tab.this);
366 }
367
Grace Kloba22ac16e2009-10-07 18:00:23 -0700368 // finally update the UI in the activity if it is in the foreground
John Reck324d4402011-01-11 16:56:42 -0800369 mWebViewController.onPageStarted(Tab.this, view, favicon);
Leon Scroggins4cd97792010-12-03 15:31:56 -0500370
John Recke969cc52010-12-21 17:24:43 -0800371 updateBookmarkedStatus();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700372 }
373
374 @Override
375 public void onPageFinished(WebView view, String url) {
John Reck5b691842010-11-29 11:21:13 -0800376 if (!isPrivateBrowsingEnabled()) {
377 LogTag.logPageFinishedLoading(
378 url, SystemClock.uptimeMillis() - mLoadStartTime);
379 }
John Reck1cf4b792011-07-26 10:22:22 -0700380 syncCurrentState(view, url);
John Reck324d4402011-01-11 16:56:42 -0800381 mWebViewController.onPageFinished(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700382 }
383
384 // return true if want to hijack the url to let another app to handle it
385 @Override
386 public boolean shouldOverrideUrlLoading(WebView view, String url) {
387 if (mInForeground) {
Michael Kolb18eb3772010-12-10 14:29:51 -0800388 return mWebViewController.shouldOverrideUrlLoading(Tab.this,
389 view, url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700390 } else {
391 return false;
392 }
393 }
394
395 /**
Steve Block2466eff2011-10-03 15:33:09 +0100396 * Updates the security state. This method is called when we discover
397 * another resource to be loaded for this page (for example,
398 * javascript). While we update the security state, we do not update
399 * the lock icon until we are done loading, as it is slightly more
400 * secure this way.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700401 */
402 @Override
403 public void onLoadResource(WebView view, String url) {
404 if (url != null && url.length() > 0) {
405 // It is only if the page claims to be secure that we may have
Steve Block2466eff2011-10-03 15:33:09 +0100406 // to update the security state:
407 if (mCurrentState.mSecurityState == SecurityState.SECURITY_STATE_SECURE) {
408 // If NOT a 'safe' url, change the state to mixed content!
Grace Kloba22ac16e2009-10-07 18:00:23 -0700409 if (!(URLUtil.isHttpsUrl(url) || URLUtil.isDataUrl(url)
410 || URLUtil.isAboutUrl(url))) {
Steve Block2466eff2011-10-03 15:33:09 +0100411 mCurrentState.mSecurityState = SecurityState.SECURITY_STATE_MIXED;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700412 }
413 }
414 }
415 }
416
417 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700418 * Show a dialog informing the user of the network error reported by
419 * WebCore if it is in the foreground.
420 */
421 @Override
422 public void onReceivedError(WebView view, int errorCode,
423 String description, String failingUrl) {
424 if (errorCode != WebViewClient.ERROR_HOST_LOOKUP &&
425 errorCode != WebViewClient.ERROR_CONNECT &&
426 errorCode != WebViewClient.ERROR_BAD_URL &&
427 errorCode != WebViewClient.ERROR_UNSUPPORTED_SCHEME &&
428 errorCode != WebViewClient.ERROR_FILE) {
429 queueError(errorCode, description);
Jeff Hamilton47654f42010-09-07 09:57:51 -0500430
Selim Gurun3da06b82011-10-10 13:58:12 -0700431 // Don't log URLs when in private browsing mode
432 if (!isPrivateBrowsingEnabled()) {
433 Log.e(LOGTAG, "onReceivedError " + errorCode + " " + failingUrl
Jeff Hamilton47654f42010-09-07 09:57:51 -0500434 + " " + description);
Selim Gurun3da06b82011-10-10 13:58:12 -0700435 }
Jeff Hamilton47654f42010-09-07 09:57:51 -0500436 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700437 }
438
439 /**
440 * Check with the user if it is ok to resend POST data as the page they
441 * are trying to navigate to is the result of a POST.
442 */
443 @Override
444 public void onFormResubmission(WebView view, final Message dontResend,
445 final Message resend) {
446 if (!mInForeground) {
447 dontResend.sendToTarget();
448 return;
449 }
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500450 if (mDontResend != null) {
451 Log.w(LOGTAG, "onFormResubmission should not be called again "
452 + "while dialog is still up");
453 dontResend.sendToTarget();
454 return;
455 }
456 mDontResend = dontResend;
457 mResend = resend;
Michael Kolb14612442011-06-24 13:06:29 -0700458 new AlertDialog.Builder(mContext).setTitle(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700459 R.string.browserFrameFormResubmitLabel).setMessage(
460 R.string.browserFrameFormResubmitMessage)
461 .setPositiveButton(R.string.ok,
462 new DialogInterface.OnClickListener() {
463 public void onClick(DialogInterface dialog,
464 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500465 if (mResend != null) {
466 mResend.sendToTarget();
467 mResend = null;
468 mDontResend = null;
469 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700470 }
471 }).setNegativeButton(R.string.cancel,
472 new DialogInterface.OnClickListener() {
473 public void onClick(DialogInterface dialog,
474 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500475 if (mDontResend != null) {
476 mDontResend.sendToTarget();
477 mResend = null;
478 mDontResend = null;
479 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700480 }
481 }).setOnCancelListener(new OnCancelListener() {
482 public void onCancel(DialogInterface dialog) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500483 if (mDontResend != null) {
484 mDontResend.sendToTarget();
485 mResend = null;
486 mDontResend = null;
487 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700488 }
489 }).show();
490 }
491
492 /**
493 * Insert the url into the visited history database.
494 * @param url The url to be inserted.
495 * @param isReload True if this url is being reloaded.
496 * FIXME: Not sure what to do when reloading the page.
497 */
498 @Override
499 public void doUpdateVisitedHistory(WebView view, String url,
500 boolean isReload) {
John Reck324d4402011-01-11 16:56:42 -0800501 mWebViewController.doUpdateVisitedHistory(Tab.this, isReload);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700502 }
503
504 /**
505 * Displays SSL error(s) dialog to the user.
506 */
507 @Override
508 public void onReceivedSslError(final WebView view,
509 final SslErrorHandler handler, final SslError error) {
510 if (!mInForeground) {
511 handler.cancel();
Steve Block2466eff2011-10-03 15:33:09 +0100512 setSecurityState(SecurityState.SECURITY_STATE_NOT_SECURE);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700513 return;
514 }
John Reck35e9dd62011-04-25 09:01:54 -0700515 if (mSettings.showSecurityWarnings()) {
John Reckcb28b2c2011-08-26 17:39:44 -0700516 new AlertDialog.Builder(mContext)
517 .setTitle(R.string.security_warning)
518 .setMessage(R.string.ssl_warnings_header)
519 .setIcon(android.R.drawable.ic_dialog_alert)
520 .setPositiveButton(R.string.ssl_continue,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700521 new DialogInterface.OnClickListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700522 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700523 public void onClick(DialogInterface dialog,
524 int whichButton) {
525 handler.proceed();
Steve Block4895b012011-10-03 16:26:46 +0100526 handleProceededAfterSslError(error);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700527 }
John Reckcb28b2c2011-08-26 17:39:44 -0700528 })
529 .setNeutralButton(R.string.view_certificate,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700530 new DialogInterface.OnClickListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700531 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700532 public void onClick(DialogInterface dialog,
533 int whichButton) {
John Reckcb28b2c2011-08-26 17:39:44 -0700534 mWebViewController.showSslCertificateOnError(
535 view, handler, error);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700536 }
John Reckcb28b2c2011-08-26 17:39:44 -0700537 })
538 .setNegativeButton(R.string.ssl_go_back,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700539 new DialogInterface.OnClickListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700540 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700541 public void onClick(DialogInterface dialog,
542 int whichButton) {
John Reck30c714c2010-12-16 17:30:34 -0800543 dialog.cancel();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700544 }
John Reckcb28b2c2011-08-26 17:39:44 -0700545 })
546 .setOnCancelListener(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700547 new DialogInterface.OnCancelListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700548 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700549 public void onCancel(DialogInterface dialog) {
550 handler.cancel();
Steve Block2466eff2011-10-03 15:33:09 +0100551 setSecurityState(SecurityState.SECURITY_STATE_NOT_SECURE);
John Reck30c714c2010-12-16 17:30:34 -0800552 mWebViewController.onUserCanceledSsl(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700553 }
John Reckcb28b2c2011-08-26 17:39:44 -0700554 })
555 .show();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700556 } else {
557 handler.proceed();
558 }
559 }
560
561 /**
Steve Block4895b012011-10-03 16:26:46 +0100562 * Called when an SSL error occurred while loading a resource, but the
563 * WebView but chose to proceed anyway based on a decision retained
564 * from a previous response to onReceivedSslError(). We update our
565 * security state to reflect this.
566 */
567 @Override
568 public void onProceededAfterSslError(WebView view, SslError error) {
569 handleProceededAfterSslError(error);
570 }
571
572 /**
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700573 * Displays client certificate request to the user.
574 */
575 @Override
576 public void onReceivedClientCertRequest(final WebView view,
577 final ClientCertRequestHandler handler, final String host_and_port) {
578 if (!mInForeground) {
579 handler.ignore();
580 return;
581 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700582 int colon = host_and_port.lastIndexOf(':');
583 String host;
584 int port;
585 if (colon == -1) {
586 host = host_and_port;
587 port = -1;
588 } else {
589 String portString = host_and_port.substring(colon + 1);
590 try {
591 port = Integer.parseInt(portString);
592 host = host_and_port.substring(0, colon);
593 } catch (NumberFormatException e) {
594 host = host_and_port;
595 port = -1;
596 }
597 }
Michael Kolb14612442011-06-24 13:06:29 -0700598 KeyChain.choosePrivateKeyAlias(
599 mWebViewController.getActivity(), new KeyChainAliasCallback() {
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700600 @Override public void alias(String alias) {
601 if (alias == null) {
602 handler.cancel();
603 return;
604 }
Michael Kolb14612442011-06-24 13:06:29 -0700605 new KeyChainLookup(mContext, handler, alias).execute();
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700606 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700607 }, null, null, host, port, null);
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700608 }
609
610 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700611 * Handles an HTTP authentication request.
612 *
613 * @param handler The authentication handler
614 * @param host The host
615 * @param realm The realm
616 */
617 @Override
618 public void onReceivedHttpAuthRequest(WebView view,
619 final HttpAuthHandler handler, final String host,
620 final String realm) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700621 mWebViewController.onReceivedHttpAuthRequest(Tab.this, view, handler, host, realm);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700622 }
623
624 @Override
John Reck438bf462011-01-12 18:11:46 -0800625 public WebResourceResponse shouldInterceptRequest(WebView view,
626 String url) {
627 WebResourceResponse res = HomeProvider.shouldInterceptRequest(
Michael Kolb14612442011-06-24 13:06:29 -0700628 mContext, url);
John Reck438bf462011-01-12 18:11:46 -0800629 return res;
630 }
631
632 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700633 public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
634 if (!mInForeground) {
635 return false;
636 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700637 return mWebViewController.shouldOverrideKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700638 }
639
640 @Override
641 public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700642 if (!mInForeground) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700643 return;
644 }
John Reck997b1b72012-04-19 18:08:25 -0700645 if (!mWebViewController.onUnhandledKeyEvent(event)) {
646 super.onUnhandledKeyEvent(view, event);
647 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700648 }
Patrick Scott92066772011-03-10 08:46:27 -0500649
650 @Override
651 public void onReceivedLoginRequest(WebView view, String realm,
652 String account, String args) {
Michael Kolb14612442011-06-24 13:06:29 -0700653 new DeviceAccountLogin(mWebViewController.getActivity(), view, Tab.this, mWebViewController)
Patrick Scott92066772011-03-10 08:46:27 -0500654 .handleLogin(realm, account, args);
655 }
656
Grace Kloba22ac16e2009-10-07 18:00:23 -0700657 };
658
John Reck1cf4b792011-07-26 10:22:22 -0700659 private void syncCurrentState(WebView view, String url) {
660 // Sync state (in case of stop/timeout)
661 mCurrentState.mUrl = view.getUrl();
662 if (mCurrentState.mUrl == null) {
John Reck8bcafc12011-08-29 16:43:02 -0700663 mCurrentState.mUrl = "";
John Reck1cf4b792011-07-26 10:22:22 -0700664 }
665 mCurrentState.mOriginalUrl = view.getOriginalUrl();
666 mCurrentState.mTitle = view.getTitle();
667 mCurrentState.mFavicon = view.getFavicon();
668 if (!URLUtil.isHttpsUrl(mCurrentState.mUrl)) {
669 // In case we stop when loading an HTTPS page from an HTTP page
670 // but before a provisional load occurred
Steve Block2466eff2011-10-03 15:33:09 +0100671 mCurrentState.mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
Steve Block08a6f0c2011-10-06 12:12:53 +0100672 mCurrentState.mSslCertificateError = null;
John Reck1cf4b792011-07-26 10:22:22 -0700673 }
John Reck502a3532011-08-16 14:21:46 -0700674 mCurrentState.mIncognito = view.isPrivateBrowsingEnabled();
John Reck1cf4b792011-07-26 10:22:22 -0700675 }
676
Patrick Scott92066772011-03-10 08:46:27 -0500677 // Called by DeviceAccountLogin when the Tab needs to have the auto-login UI
678 // displayed.
679 void setDeviceAccountLogin(DeviceAccountLogin login) {
680 mDeviceAccountLogin = login;
681 }
682
683 // Returns non-null if the title bar should display the auto-login UI.
684 DeviceAccountLogin getDeviceAccountLogin() {
685 return mDeviceAccountLogin;
686 }
687
Grace Kloba22ac16e2009-10-07 18:00:23 -0700688 // -------------------------------------------------------------------------
689 // WebChromeClient implementation for the main WebView
690 // -------------------------------------------------------------------------
691
692 private final WebChromeClient mWebChromeClient = new WebChromeClient() {
693 // Helper method to create a new tab or sub window.
694 private void createWindow(final boolean dialog, final Message msg) {
695 WebView.WebViewTransport transport =
696 (WebView.WebViewTransport) msg.obj;
697 if (dialog) {
698 createSubWindow();
Michael Kolb8233fac2010-10-26 16:08:53 -0700699 mWebViewController.attachSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700700 transport.setWebView(mSubView);
701 } else {
Michael Kolb7bcafde2011-05-09 13:55:59 -0700702 final Tab newTab = mWebViewController.openTab(null,
John Reck5949c662011-05-27 09:52:29 -0700703 Tab.this, true, true);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700704 transport.setWebView(newTab.getWebView());
705 }
706 msg.sendToTarget();
707 }
708
709 @Override
710 public boolean onCreateWindow(WebView view, final boolean dialog,
711 final boolean userGesture, final Message resultMsg) {
712 // only allow new window or sub window for the foreground case
713 if (!mInForeground) {
714 return false;
715 }
716 // Short-circuit if we can't create any more tabs or sub windows.
717 if (dialog && mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -0700718 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700719 .setTitle(R.string.too_many_subwindows_dialog_title)
720 .setIcon(android.R.drawable.ic_dialog_alert)
721 .setMessage(R.string.too_many_subwindows_dialog_message)
722 .setPositiveButton(R.string.ok, null)
723 .show();
724 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700725 } else if (!mWebViewController.getTabControl().canCreateNewTab()) {
Michael Kolb14612442011-06-24 13:06:29 -0700726 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700727 .setTitle(R.string.too_many_windows_dialog_title)
728 .setIcon(android.R.drawable.ic_dialog_alert)
729 .setMessage(R.string.too_many_windows_dialog_message)
730 .setPositiveButton(R.string.ok, null)
731 .show();
732 return false;
733 }
734
735 // Short-circuit if this was a user gesture.
736 if (userGesture) {
737 createWindow(dialog, resultMsg);
738 return true;
739 }
740
741 // Allow the popup and create the appropriate window.
742 final AlertDialog.OnClickListener allowListener =
743 new AlertDialog.OnClickListener() {
744 public void onClick(DialogInterface d,
745 int which) {
746 createWindow(dialog, resultMsg);
747 }
748 };
749
750 // Block the popup by returning a null WebView.
751 final AlertDialog.OnClickListener blockListener =
752 new AlertDialog.OnClickListener() {
753 public void onClick(DialogInterface d, int which) {
754 resultMsg.sendToTarget();
755 }
756 };
757
758 // Build a confirmation dialog to display to the user.
759 final AlertDialog d =
Michael Kolb14612442011-06-24 13:06:29 -0700760 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700761 .setIcon(android.R.drawable.ic_dialog_alert)
762 .setMessage(R.string.popup_window_attempt)
763 .setPositiveButton(R.string.allow, allowListener)
764 .setNegativeButton(R.string.block, blockListener)
765 .setCancelable(false)
766 .create();
767
768 // Show the confirmation dialog.
769 d.show();
770 return true;
771 }
772
773 @Override
Patrick Scotteb5061b2009-11-18 15:00:30 -0500774 public void onRequestFocus(WebView view) {
775 if (!mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700776 mWebViewController.switchToTab(Tab.this);
Patrick Scotteb5061b2009-11-18 15:00:30 -0500777 }
778 }
779
780 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700781 public void onCloseWindow(WebView window) {
Michael Kolbc831b632011-05-11 09:30:34 -0700782 if (mParent != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700783 // JavaScript can only close popup window.
784 if (mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700785 mWebViewController.switchToTab(mParent);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700786 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700787 mWebViewController.closeTab(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700788 }
789 }
790
791 @Override
792 public void onProgressChanged(WebView view, int newProgress) {
John Reck30c714c2010-12-16 17:30:34 -0800793 mPageLoadProgress = newProgress;
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800794 if (newProgress == 100) {
795 mInPageLoad = false;
796 }
John Reck30c714c2010-12-16 17:30:34 -0800797 mWebViewController.onProgressChanged(Tab.this);
Michael Kolb72864272012-05-03 15:42:15 -0700798 if (mUpdateThumbnail && newProgress == 100) {
799 mUpdateThumbnail = false;
800 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700801 }
802
803 @Override
Leon Scroggins21d9b902010-03-11 09:33:11 -0500804 public void onReceivedTitle(WebView view, final String title) {
John Reck30c714c2010-12-16 17:30:34 -0800805 mCurrentState.mTitle = title;
Michael Kolb8233fac2010-10-26 16:08:53 -0700806 mWebViewController.onReceivedTitle(Tab.this, title);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700807 }
808
809 @Override
810 public void onReceivedIcon(WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -0800811 mCurrentState.mFavicon = icon;
Michael Kolb8233fac2010-10-26 16:08:53 -0700812 mWebViewController.onFavicon(Tab.this, view, icon);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700813 }
814
815 @Override
816 public void onReceivedTouchIconUrl(WebView view, String url,
817 boolean precomposed) {
Michael Kolb14612442011-06-24 13:06:29 -0700818 final ContentResolver cr = mContext.getContentResolver();
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400819 // Let precomposed icons take precedence over non-composed
820 // icons.
821 if (precomposed && mTouchIconLoader != null) {
822 mTouchIconLoader.cancel(false);
823 mTouchIconLoader = null;
824 }
825 // Have only one async task at a time.
826 if (mTouchIconLoader == null) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700827 mTouchIconLoader = new DownloadTouchIcon(Tab.this,
Michael Kolb14612442011-06-24 13:06:29 -0700828 mContext, cr, view);
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400829 mTouchIconLoader.execute(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700830 }
831 }
832
833 @Override
834 public void onShowCustomView(View view,
835 WebChromeClient.CustomViewCallback callback) {
Michael Kolb14612442011-06-24 13:06:29 -0700836 Activity activity = mWebViewController.getActivity();
837 if (activity != null) {
838 onShowCustomView(view, activity.getRequestedOrientation(), callback);
839 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400840 }
841
842 @Override
843 public void onShowCustomView(View view, int requestedOrientation,
844 WebChromeClient.CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700845 if (mInForeground) mWebViewController.showCustomView(Tab.this, view,
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400846 requestedOrientation, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700847 }
848
849 @Override
850 public void onHideCustomView() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700851 if (mInForeground) mWebViewController.hideCustomView();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700852 }
853
854 /**
855 * The origin has exceeded its database quota.
856 * @param url the URL that exceeded the quota
857 * @param databaseIdentifier the identifier of the database on which the
858 * transaction that caused the quota overflow was run
859 * @param currentQuota the current quota for the origin.
860 * @param estimatedSize the estimated size of the database.
861 * @param totalUsedQuota is the sum of all origins' quota.
862 * @param quotaUpdater The callback to run when a decision to allow or
863 * deny quota has been made. Don't forget to call this!
864 */
865 @Override
866 public void onExceededDatabaseQuota(String url,
867 String databaseIdentifier, long currentQuota, long estimatedSize,
868 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -0700869 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -0700870 .onExceededDatabaseQuota(url, databaseIdentifier,
871 currentQuota, estimatedSize, totalUsedQuota,
872 quotaUpdater);
873 }
874
875 /**
876 * The Application Cache has exceeded its max size.
877 * @param spaceNeeded is the amount of disk space that would be needed
878 * in order for the last appcache operation to succeed.
879 * @param totalUsedQuota is the sum of all origins' quota.
880 * @param quotaUpdater A callback to inform the WebCore thread that a
881 * new app cache size is available. This callback must always
882 * be executed at some point to ensure that the sleeping
883 * WebCore thread is woken up.
884 */
885 @Override
886 public void onReachedMaxAppCacheSize(long spaceNeeded,
887 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -0700888 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -0700889 .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota,
890 quotaUpdater);
891 }
892
893 /**
894 * Instructs the browser to show a prompt to ask the user to set the
895 * Geolocation permission state for the specified origin.
896 * @param origin The origin for which Geolocation permissions are
897 * requested.
898 * @param callback The callback to call once the user has set the
899 * Geolocation permission state.
900 */
901 @Override
902 public void onGeolocationPermissionsShowPrompt(String origin,
903 GeolocationPermissions.Callback callback) {
904 if (mInForeground) {
Grace Kloba50c241e2010-04-20 11:07:50 -0700905 getGeolocationPermissionsPrompt().show(origin, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700906 }
907 }
908
909 /**
910 * Instructs the browser to hide the Geolocation permissions prompt.
911 */
912 @Override
913 public void onGeolocationPermissionsHidePrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -0700914 if (mInForeground && mGeolocationPermissionsPrompt != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700915 mGeolocationPermissionsPrompt.hide();
916 }
917 }
918
Ben Murdoch65acc352009-11-19 18:16:04 +0000919 /* Adds a JavaScript error message to the system log and if the JS
920 * console is enabled in the about:debug options, to that console
921 * also.
Ben Murdochc42addf2010-01-28 15:19:59 +0000922 * @param consoleMessage the message object.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700923 */
924 @Override
Ben Murdochc42addf2010-01-28 15:19:59 +0000925 public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700926 if (mInForeground) {
927 // call getErrorConsole(true) so it will create one if needed
928 ErrorConsoleView errorConsole = getErrorConsole(true);
Ben Murdochc42addf2010-01-28 15:19:59 +0000929 errorConsole.addErrorMessage(consoleMessage);
Michael Kolb8233fac2010-10-26 16:08:53 -0700930 if (mWebViewController.shouldShowErrorConsole()
931 && errorConsole.getShowState() !=
932 ErrorConsoleView.SHOW_MAXIMIZED) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700933 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
934 }
935 }
Ben Murdochc42addf2010-01-28 15:19:59 +0000936
Jeff Hamilton47654f42010-09-07 09:57:51 -0500937 // Don't log console messages in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -0700938 if (isPrivateBrowsingEnabled()) return true;
Jeff Hamilton47654f42010-09-07 09:57:51 -0500939
Ben Murdochc42addf2010-01-28 15:19:59 +0000940 String message = "Console: " + consoleMessage.message() + " "
941 + consoleMessage.sourceId() + ":"
942 + consoleMessage.lineNumber();
943
944 switch (consoleMessage.messageLevel()) {
945 case TIP:
946 Log.v(CONSOLE_LOGTAG, message);
947 break;
948 case LOG:
949 Log.i(CONSOLE_LOGTAG, message);
950 break;
951 case WARNING:
952 Log.w(CONSOLE_LOGTAG, message);
953 break;
954 case ERROR:
955 Log.e(CONSOLE_LOGTAG, message);
956 break;
957 case DEBUG:
958 Log.d(CONSOLE_LOGTAG, message);
959 break;
960 }
961
962 return true;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700963 }
964
965 /**
966 * Ask the browser for an icon to represent a <video> element.
967 * This icon will be used if the Web page did not specify a poster attribute.
968 * @return Bitmap The icon or null if no such icon is available.
969 */
970 @Override
971 public Bitmap getDefaultVideoPoster() {
972 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700973 return mWebViewController.getDefaultVideoPoster();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700974 }
975 return null;
976 }
977
978 /**
979 * Ask the host application for a custom progress view to show while
980 * a <video> is loading.
981 * @return View The progress view.
982 */
983 @Override
984 public View getVideoLoadingProgressView() {
985 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700986 return mWebViewController.getVideoLoadingProgressView();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700987 }
988 return null;
989 }
990
991 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +0000992 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700993 if (mInForeground) {
Ben Murdoch8cad4132012-01-11 10:56:43 +0000994 mWebViewController.openFileChooser(uploadMsg, acceptType, capture);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700995 } else {
996 uploadMsg.onReceiveValue(null);
997 }
998 }
999
1000 /**
1001 * Deliver a list of already-visited URLs
1002 */
1003 @Override
1004 public void getVisitedHistory(final ValueCallback<String[]> callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001005 mWebViewController.getVisitedHistory(callback);
1006 }
Ben Murdoch8029a772010-11-16 11:58:21 +00001007
1008 @Override
1009 public void setupAutoFill(Message message) {
1010 // Prompt the user to set up their profile.
1011 final Message msg = message;
Michael Kolb14612442011-06-24 13:06:29 -07001012 AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
1013 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
Ben Murdoch1d676b62011-01-17 12:54:24 +00001014 Context.LAYOUT_INFLATER_SERVICE);
1015 final View layout = inflater.inflate(R.layout.setup_autofill_dialog, null);
1016
1017 builder.setView(layout)
1018 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1019 @Override
1020 public void onClick(DialogInterface dialog, int id) {
1021 CheckBox disableAutoFill = (CheckBox) layout.findViewById(
1022 R.id.setup_autofill_dialog_disable_autofill);
1023
1024 if (disableAutoFill.isChecked()) {
1025 // Disable autofill and show a toast with how to turn it on again.
John Reck35e9dd62011-04-25 09:01:54 -07001026 mSettings.setAutofillEnabled(false);
Michael Kolb14612442011-06-24 13:06:29 -07001027 Toast.makeText(mContext,
Ben Murdoch1d676b62011-01-17 12:54:24 +00001028 R.string.autofill_setup_dialog_negative_toast,
1029 Toast.LENGTH_LONG).show();
1030 } else {
1031 // Take user to the AutoFill profile editor. When they return,
1032 // we will send the message that we pass here which will trigger
1033 // the form to get filled out with their new profile.
1034 mWebViewController.setupAutoFill(msg);
1035 }
1036 }
1037 })
1038 .setNegativeButton(R.string.cancel, null)
1039 .show();
Ben Murdoch8029a772010-11-16 11:58:21 +00001040 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001041 };
1042
1043 // -------------------------------------------------------------------------
1044 // WebViewClient implementation for the sub window
1045 // -------------------------------------------------------------------------
1046
1047 // Subclass of WebViewClient used in subwindows to notify the main
1048 // WebViewClient of certain WebView activities.
Jonathan Dixonf4ff1492012-06-08 10:15:32 -07001049 private static class SubWindowClient extends WebViewClientClassicExt {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001050 // The main WebViewClient.
Jonathan Dixonf4ff1492012-06-08 10:15:32 -07001051 private final WebViewClientClassicExt mClient;
Michael Kolb8233fac2010-10-26 16:08:53 -07001052 private final WebViewController mController;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001053
Jonathan Dixonf4ff1492012-06-08 10:15:32 -07001054 SubWindowClient(WebViewClientClassicExt client, WebViewController controller) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001055 mClient = client;
Michael Kolb8233fac2010-10-26 16:08:53 -07001056 mController = controller;
Leon Scroggins III211ba542010-04-19 13:21:13 -04001057 }
1058 @Override
1059 public void onPageStarted(WebView view, String url, Bitmap favicon) {
1060 // Unlike the others, do not call mClient's version, which would
1061 // change the progress bar. However, we do want to remove the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001062 // find or select dialog.
Michael Kolb8233fac2010-10-26 16:08:53 -07001063 mController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001064 }
1065 @Override
1066 public void doUpdateVisitedHistory(WebView view, String url,
1067 boolean isReload) {
1068 mClient.doUpdateVisitedHistory(view, url, isReload);
1069 }
1070 @Override
1071 public boolean shouldOverrideUrlLoading(WebView view, String url) {
1072 return mClient.shouldOverrideUrlLoading(view, url);
1073 }
1074 @Override
1075 public void onReceivedSslError(WebView view, SslErrorHandler handler,
1076 SslError error) {
1077 mClient.onReceivedSslError(view, handler, error);
1078 }
1079 @Override
Brian Carlstrom8862c1d2011-06-02 01:05:55 -07001080 public void onReceivedClientCertRequest(WebView view,
1081 ClientCertRequestHandler handler, String host_and_port) {
1082 mClient.onReceivedClientCertRequest(view, handler, host_and_port);
1083 }
1084 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -07001085 public void onReceivedHttpAuthRequest(WebView view,
1086 HttpAuthHandler handler, String host, String realm) {
1087 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
1088 }
1089 @Override
1090 public void onFormResubmission(WebView view, Message dontResend,
1091 Message resend) {
1092 mClient.onFormResubmission(view, dontResend, resend);
1093 }
1094 @Override
1095 public void onReceivedError(WebView view, int errorCode,
1096 String description, String failingUrl) {
1097 mClient.onReceivedError(view, errorCode, description, failingUrl);
1098 }
1099 @Override
1100 public boolean shouldOverrideKeyEvent(WebView view,
1101 android.view.KeyEvent event) {
1102 return mClient.shouldOverrideKeyEvent(view, event);
1103 }
1104 @Override
1105 public void onUnhandledKeyEvent(WebView view,
1106 android.view.KeyEvent event) {
1107 mClient.onUnhandledKeyEvent(view, event);
1108 }
1109 }
1110
1111 // -------------------------------------------------------------------------
1112 // WebChromeClient implementation for the sub window
1113 // -------------------------------------------------------------------------
1114
1115 private class SubWindowChromeClient extends WebChromeClient {
1116 // The main WebChromeClient.
1117 private final WebChromeClient mClient;
1118
1119 SubWindowChromeClient(WebChromeClient client) {
1120 mClient = client;
1121 }
1122 @Override
1123 public void onProgressChanged(WebView view, int newProgress) {
1124 mClient.onProgressChanged(view, newProgress);
1125 }
1126 @Override
1127 public boolean onCreateWindow(WebView view, boolean dialog,
1128 boolean userGesture, android.os.Message resultMsg) {
1129 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
1130 }
1131 @Override
1132 public void onCloseWindow(WebView window) {
1133 if (window != mSubView) {
1134 Log.e(LOGTAG, "Can't close the window");
1135 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001136 mWebViewController.dismissSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001137 }
1138 }
1139
1140 // -------------------------------------------------------------------------
1141
1142 // Construct a new tab
Michael Kolb7bcafde2011-05-09 13:55:59 -07001143 Tab(WebViewController wvcontroller, WebView w) {
John Reck1cf4b792011-07-26 10:22:22 -07001144 this(wvcontroller, w, null);
1145 }
1146
1147 Tab(WebViewController wvcontroller, Bundle state) {
1148 this(wvcontroller, null, state);
1149 }
1150
1151 Tab(WebViewController wvcontroller, WebView w, Bundle state) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001152 mWebViewController = wvcontroller;
Michael Kolb14612442011-06-24 13:06:29 -07001153 mContext = mWebViewController.getContext();
John Reck35e9dd62011-04-25 09:01:54 -07001154 mSettings = BrowserSettings.getInstance();
Michael Kolb14612442011-06-24 13:06:29 -07001155 mDataController = DataController.getInstance(mContext);
1156 mCurrentState = new PageState(mContext, w != null
John Reck847b5322011-04-14 17:02:18 -07001157 ? w.isPrivateBrowsingEnabled() : false);
Michael Kolb8233fac2010-10-26 16:08:53 -07001158 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001159 mInForeground = false;
1160
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001161 mDownloadListener = new DownloadListener() {
1162 public void onDownloadStart(String url, String userAgent,
1163 String contentDisposition, String mimetype,
1164 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001165 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001166 mimetype, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001167 }
1168 };
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001169 mWebBackForwardListClient = new WebBackForwardListClient() {
1170 @Override
1171 public void onNewHistoryItem(WebHistoryItem item) {
Mathew Inwooda829d552011-09-02 14:16:25 +01001172 if (mClearHistoryUrlPattern != null) {
1173 boolean match =
1174 mClearHistoryUrlPattern.matcher(item.getOriginalUrl()).matches();
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001175 if (LOGD_ENABLED) {
Mathew Inwooda829d552011-09-02 14:16:25 +01001176 Log.d(LOGTAG, "onNewHistoryItem: match=" + match + "\n\t"
1177 + item.getUrl() + "\n\t"
1178 + mClearHistoryUrlPattern);
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001179 }
Mathew Inwooda829d552011-09-02 14:16:25 +01001180 if (match) {
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001181 if (mMainView != null) {
1182 mMainView.clearHistory();
1183 }
1184 }
Mathew Inwooda829d552011-09-02 14:16:25 +01001185 mClearHistoryUrlPattern = null;
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001186 }
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001187 }
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001188 };
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001189
John Reck1cf4b792011-07-26 10:22:22 -07001190 mCaptureWidth = mContext.getResources().getDimensionPixelSize(
1191 R.dimen.tab_thumbnail_width);
1192 mCaptureHeight = mContext.getResources().getDimensionPixelSize(
1193 R.dimen.tab_thumbnail_height);
1194 updateShouldCaptureThumbnails();
1195 restoreState(state);
John Reck52be4782011-08-26 15:37:29 -07001196 if (getId() == -1) {
1197 mId = TabControl.getNextId();
1198 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001199 setWebView(w);
Michael Kolb9ef259a2011-07-12 15:33:08 -07001200 mHandler = new Handler() {
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001201 @Override
Michael Kolb9ef259a2011-07-12 15:33:08 -07001202 public void handleMessage(Message m) {
John Reck1cf4b792011-07-26 10:22:22 -07001203 switch (m.what) {
1204 case MSG_CAPTURE:
1205 capture();
1206 break;
1207 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001208 }
1209 };
John Reck1cf4b792011-07-26 10:22:22 -07001210 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001211
Michael Kolb72864272012-05-03 15:42:15 -07001212 public boolean shouldUpdateThumbnail() {
1213 return mUpdateThumbnail;
1214 }
1215
Mathew Inwoode09305e2011-09-02 12:03:26 +01001216 /**
1217 * This is used to get a new ID when the tab has been preloaded, before it is displayed and
1218 * added to TabControl. Preloaded tabs can be created before restoreInstanceState, leading
1219 * to overlapping IDs between the preloaded and restored tabs.
1220 */
1221 public void refreshIdAfterPreload() {
1222 mId = TabControl.getNextId();
1223 }
1224
John Reck1cf4b792011-07-26 10:22:22 -07001225 public void updateShouldCaptureThumbnails() {
1226 if (mWebViewController.shouldCaptureThumbnails()) {
1227 synchronized (Tab.this) {
1228 if (mCapture == null) {
1229 mCapture = Bitmap.createBitmap(mCaptureWidth, mCaptureHeight,
1230 Bitmap.Config.RGB_565);
Michael Kolbc3af0672011-08-09 10:24:41 -07001231 mCapture.eraseColor(Color.WHITE);
John Reck1cf4b792011-07-26 10:22:22 -07001232 if (mInForeground) {
1233 postCapture();
1234 }
1235 }
1236 }
1237 } else {
1238 synchronized (Tab.this) {
1239 mCapture = null;
1240 deleteThumbnail();
1241 }
1242 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001243 }
1244
Michael Kolb14612442011-06-24 13:06:29 -07001245 public void setController(WebViewController ctl) {
1246 mWebViewController = ctl;
John Reck1cf4b792011-07-26 10:22:22 -07001247 updateShouldCaptureThumbnails();
Michael Kolb14612442011-06-24 13:06:29 -07001248 }
1249
Michael Kolbc831b632011-05-11 09:30:34 -07001250 public long getId() {
1251 return mId;
1252 }
1253
Michael Kolb91911a22012-01-17 11:21:25 -08001254 void setWebView(WebView w) {
1255 setWebView(w, true);
1256 }
1257
Grace Kloba22ac16e2009-10-07 18:00:23 -07001258 /**
1259 * Sets the WebView for this tab, correctly removing the old WebView from
1260 * the container view.
1261 */
Michael Kolb91911a22012-01-17 11:21:25 -08001262 void setWebView(WebView w, boolean restore) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001263 if (mMainView == w) {
1264 return;
1265 }
Michael Kolba713ec82010-11-29 17:27:06 -08001266
Grace Kloba22ac16e2009-10-07 18:00:23 -07001267 // If the WebView is changing, the page will be reloaded, so any ongoing
1268 // Geolocation permission requests are void.
Grace Kloba50c241e2010-04-20 11:07:50 -07001269 if (mGeolocationPermissionsPrompt != null) {
1270 mGeolocationPermissionsPrompt.hide();
1271 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001272
Michael Kolba713ec82010-11-29 17:27:06 -08001273 mWebViewController.onSetWebView(this, w);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001274
John Reck1cf4b792011-07-26 10:22:22 -07001275 if (mMainView != null) {
John Reck8ee633f2011-08-09 16:00:35 -07001276 mMainView.setPictureListener(null);
John Reck1cf4b792011-07-26 10:22:22 -07001277 if (w != null) {
1278 syncCurrentState(w, null);
1279 } else {
1280 mCurrentState = new PageState(mContext, false);
1281 }
1282 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001283 // set the new one
1284 mMainView = w;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001285 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001286 if (mMainView != null) {
1287 mMainView.setWebViewClient(mWebViewClient);
1288 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001289 // Attach DownloadManager so that downloads can start in an active
1290 // or a non-active window. This can happen when going to a site that
1291 // does a redirect after a period of time. The user could have
1292 // switched to another tab while waiting for the download to start.
1293 mMainView.setDownloadListener(mDownloadListener);
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00001294 getWebViewClassic().setWebBackForwardListClient(mWebBackForwardListClient);
John Reck8ee633f2011-08-09 16:00:35 -07001295 TabControl tc = mWebViewController.getTabControl();
1296 if (tc != null && tc.getOnThumbnailUpdatedListener() != null) {
1297 mMainView.setPictureListener(this);
1298 }
Michael Kolb91911a22012-01-17 11:21:25 -08001299 if (restore && (mSavedState != null)) {
John Reck8b9bb8b2012-03-08 13:19:40 -08001300 restoreUserAgent();
John Reck6c2e2f32011-08-22 13:41:23 -07001301 WebBackForwardList restoredState
1302 = mMainView.restoreState(mSavedState);
1303 if (restoredState == null || restoredState.getSize() == 0) {
1304 Log.w(LOGTAG, "Failed to restore WebView state!");
1305 loadUrl(mCurrentState.mOriginalUrl, null);
1306 }
John Reck1cf4b792011-07-26 10:22:22 -07001307 mSavedState = null;
1308 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001309 }
1310 }
1311
1312 /**
1313 * Destroy the tab's main WebView and subWindow if any
1314 */
1315 void destroy() {
1316 if (mMainView != null) {
1317 dismissSubWindow();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001318 // save the WebView to call destroy() after detach it from the tab
1319 WebView webView = mMainView;
1320 setWebView(null);
1321 webView.destroy();
1322 }
1323 }
1324
1325 /**
1326 * Remove the tab from the parent
1327 */
1328 void removeFromTree() {
1329 // detach the children
Michael Kolbc831b632011-05-11 09:30:34 -07001330 if (mChildren != null) {
1331 for(Tab t : mChildren) {
1332 t.setParent(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001333 }
1334 }
1335 // remove itself from the parent list
Michael Kolbc831b632011-05-11 09:30:34 -07001336 if (mParent != null) {
1337 mParent.mChildren.remove(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001338 }
John Reck1cf4b792011-07-26 10:22:22 -07001339 deleteThumbnail();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001340 }
1341
1342 /**
1343 * Create a new subwindow unless a subwindow already exists.
1344 * @return True if a new subwindow was created. False if one already exists.
1345 */
1346 boolean createSubWindow() {
1347 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001348 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001349 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001350 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001351 mSubView.setWebChromeClient(new SubWindowChromeClient(
1352 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001353 // Set a different DownloadListener for the mSubView, since it will
1354 // just need to dismiss the mSubView, rather than close the Tab
1355 mSubView.setDownloadListener(new DownloadListener() {
1356 public void onDownloadStart(String url, String userAgent,
1357 String contentDisposition, String mimetype,
1358 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001359 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001360 contentDisposition, mimetype, contentLength);
1361 if (mSubView.copyBackForwardList().getSize() == 0) {
1362 // This subwindow was opened for the sole purpose of
1363 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001364 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001365 }
1366 }
1367 });
Michael Kolb14612442011-06-24 13:06:29 -07001368 mSubView.setOnCreateContextMenuListener(mWebViewController.getActivity());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001369 return true;
1370 }
1371 return false;
1372 }
1373
1374 /**
1375 * Dismiss the subWindow for the tab.
1376 */
1377 void dismissSubWindow() {
1378 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001379 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001380 mSubView.destroy();
1381 mSubView = null;
1382 mSubViewContainer = null;
1383 }
1384 }
1385
Grace Kloba22ac16e2009-10-07 18:00:23 -07001386
1387 /**
1388 * Set the parent tab of this tab.
1389 */
Michael Kolbc831b632011-05-11 09:30:34 -07001390 void setParent(Tab parent) {
John Reck52be4782011-08-26 15:37:29 -07001391 if (parent == this) {
1392 throw new IllegalStateException("Cannot set parent to self!");
1393 }
Michael Kolbc831b632011-05-11 09:30:34 -07001394 mParent = parent;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001395 // This tab may have been freed due to low memory. If that is the case,
Michael Kolbc831b632011-05-11 09:30:34 -07001396 // the parent tab id is already saved. If we are changing that id
Grace Kloba22ac16e2009-10-07 18:00:23 -07001397 // (most likely due to removing the parent tab) we must update the
Michael Kolbc831b632011-05-11 09:30:34 -07001398 // parent tab id in the saved Bundle.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001399 if (mSavedState != null) {
1400 if (parent == null) {
1401 mSavedState.remove(PARENTTAB);
1402 } else {
Michael Kolbc831b632011-05-11 09:30:34 -07001403 mSavedState.putLong(PARENTTAB, parent.getId());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001404 }
1405 }
John Reckb0a86db2011-05-24 14:05:58 -07001406
1407 // Sync the WebView useragent with the parent
1408 if (parent != null && mSettings.hasDesktopUseragent(parent.getWebView())
1409 != mSettings.hasDesktopUseragent(getWebView())) {
1410 mSettings.toggleDesktopUseragent(getWebView());
1411 }
John Reck52be4782011-08-26 15:37:29 -07001412
1413 if (parent != null && parent.getId() == getId()) {
1414 throw new IllegalStateException("Parent has same ID as child!");
1415 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001416 }
1417
1418 /**
Michael Kolbc831b632011-05-11 09:30:34 -07001419 * If this Tab was created through another Tab, then this method returns
1420 * that Tab.
1421 * @return the Tab parent or null
1422 */
1423 public Tab getParent() {
1424 return mParent;
1425 }
1426
1427 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001428 * When a Tab is created through the content of another Tab, then we
1429 * associate the Tabs.
1430 * @param child the Tab that was created from this Tab
1431 */
1432 void addChildTab(Tab child) {
Michael Kolbc831b632011-05-11 09:30:34 -07001433 if (mChildren == null) {
1434 mChildren = new Vector<Tab>();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001435 }
Michael Kolbc831b632011-05-11 09:30:34 -07001436 mChildren.add(child);
1437 child.setParent(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001438 }
1439
Michael Kolbc831b632011-05-11 09:30:34 -07001440 Vector<Tab> getChildren() {
1441 return mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001442 }
1443
1444 void resume() {
1445 if (mMainView != null) {
John Reck56c1fcf2011-08-17 10:15:16 -07001446 setupHwAcceleration(mMainView);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001447 mMainView.onResume();
1448 if (mSubView != null) {
1449 mSubView.onResume();
1450 }
1451 }
1452 }
1453
John Reck56c1fcf2011-08-17 10:15:16 -07001454 private void setupHwAcceleration(View web) {
1455 if (web == null) return;
1456 BrowserSettings settings = BrowserSettings.getInstance();
1457 if (settings.isHardwareAccelerated()) {
1458 web.setLayerType(View.LAYER_TYPE_NONE, null);
1459 } else {
1460 web.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
1461 }
1462 }
1463
Grace Kloba22ac16e2009-10-07 18:00:23 -07001464 void pause() {
1465 if (mMainView != null) {
1466 mMainView.onPause();
1467 if (mSubView != null) {
1468 mSubView.onPause();
1469 }
1470 }
1471 }
1472
1473 void putInForeground() {
John Reck8ee633f2011-08-09 16:00:35 -07001474 if (mInForeground) {
1475 return;
1476 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001477 mInForeground = true;
1478 resume();
Michael Kolb14612442011-06-24 13:06:29 -07001479 Activity activity = mWebViewController.getActivity();
1480 mMainView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001481 if (mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -07001482 mSubView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001483 }
1484 // Show the pending error dialog if the queue is not empty
1485 if (mQueuedErrors != null && mQueuedErrors.size() > 0) {
1486 showError(mQueuedErrors.getFirst());
1487 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001488 mWebViewController.bookmarkedStatusHasChanged(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001489 }
1490
1491 void putInBackground() {
John Reck8ee633f2011-08-09 16:00:35 -07001492 if (!mInForeground) {
1493 return;
1494 }
John Reck52be4782011-08-26 15:37:29 -07001495 capture();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001496 mInForeground = false;
1497 pause();
1498 mMainView.setOnCreateContextMenuListener(null);
1499 if (mSubView != null) {
1500 mSubView.setOnCreateContextMenuListener(null);
1501 }
1502 }
1503
Michael Kolb8233fac2010-10-26 16:08:53 -07001504 boolean inForeground() {
1505 return mInForeground;
1506 }
1507
Grace Kloba22ac16e2009-10-07 18:00:23 -07001508 /**
1509 * Return the top window of this tab; either the subwindow if it is not
1510 * null or the main window.
1511 * @return The top window of this tab.
1512 */
1513 WebView getTopWindow() {
1514 if (mSubView != null) {
1515 return mSubView;
1516 }
1517 return mMainView;
1518 }
1519
1520 /**
1521 * Return the main window of this tab. Note: if a tab is freed in the
1522 * background, this can return null. It is only guaranteed to be
1523 * non-null for the current tab.
1524 * @return The main WebView of this tab.
1525 */
1526 WebView getWebView() {
1527 return mMainView;
1528 }
1529
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00001530 /**
1531 * Return the underlying WebViewClassic implementation. As with getWebView,
1532 * this maybe null for background tabs.
1533 * @return The main WebView of this tab.
1534 */
1535 WebViewClassic getWebViewClassic() {
1536 return WebViewClassic.fromWebView(mMainView);
1537 }
1538
Michael Kolba713ec82010-11-29 17:27:06 -08001539 void setViewContainer(View container) {
1540 mContainer = container;
1541 }
1542
Michael Kolb8233fac2010-10-26 16:08:53 -07001543 View getViewContainer() {
1544 return mContainer;
1545 }
1546
Grace Kloba22ac16e2009-10-07 18:00:23 -07001547 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001548 * Return whether private browsing is enabled for the main window of
1549 * this tab.
1550 * @return True if private browsing is enabled.
1551 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001552 boolean isPrivateBrowsingEnabled() {
John Reck502a3532011-08-16 14:21:46 -07001553 return mCurrentState.mIncognito;
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001554 }
1555
1556 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001557 * Return the subwindow of this tab or null if there is no subwindow.
1558 * @return The subwindow of this tab or null.
1559 */
1560 WebView getSubWebView() {
1561 return mSubView;
1562 }
1563
Michael Kolb1514bb72010-11-22 09:11:48 -08001564 void setSubWebView(WebView subView) {
1565 mSubView = subView;
1566 }
1567
Michael Kolb8233fac2010-10-26 16:08:53 -07001568 View getSubViewContainer() {
1569 return mSubViewContainer;
1570 }
1571
Michael Kolb1514bb72010-11-22 09:11:48 -08001572 void setSubViewContainer(View subViewContainer) {
1573 mSubViewContainer = subViewContainer;
1574 }
1575
Grace Kloba22ac16e2009-10-07 18:00:23 -07001576 /**
1577 * @return The geolocation permissions prompt for this tab.
1578 */
1579 GeolocationPermissionsPrompt getGeolocationPermissionsPrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001580 if (mGeolocationPermissionsPrompt == null) {
1581 ViewStub stub = (ViewStub) mContainer
1582 .findViewById(R.id.geolocation_permissions_prompt);
1583 mGeolocationPermissionsPrompt = (GeolocationPermissionsPrompt) stub
1584 .inflate();
Grace Kloba50c241e2010-04-20 11:07:50 -07001585 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001586 return mGeolocationPermissionsPrompt;
1587 }
1588
1589 /**
1590 * @return The application id string
1591 */
1592 String getAppId() {
1593 return mAppId;
1594 }
1595
1596 /**
1597 * Set the application id string
1598 * @param id
1599 */
1600 void setAppId(String id) {
1601 mAppId = id;
1602 }
1603
Michael Kolbe28b3472011-08-04 16:54:31 -07001604 boolean closeOnBack() {
1605 return mCloseOnBack;
1606 }
1607
1608 void setCloseOnBack(boolean close) {
1609 mCloseOnBack = close;
1610 }
1611
Grace Kloba22ac16e2009-10-07 18:00:23 -07001612 String getUrl() {
John Reck324d4402011-01-11 16:56:42 -08001613 return UrlUtils.filteredUrl(mCurrentState.mUrl);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001614 }
1615
John Reck49a603c2011-03-03 09:33:05 -08001616 String getOriginalUrl() {
John Reckdb22ec42011-06-29 11:31:24 -07001617 if (mCurrentState.mOriginalUrl == null) {
1618 return getUrl();
John Reck49a603c2011-03-03 09:33:05 -08001619 }
John Reckdb22ec42011-06-29 11:31:24 -07001620 return UrlUtils.filteredUrl(mCurrentState.mOriginalUrl);
John Reck49a603c2011-03-03 09:33:05 -08001621 }
1622
Grace Kloba22ac16e2009-10-07 18:00:23 -07001623 /**
John Reck30c714c2010-12-16 17:30:34 -08001624 * Get the title of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001625 */
1626 String getTitle() {
John Reck30c714c2010-12-16 17:30:34 -08001627 if (mCurrentState.mTitle == null && mInPageLoad) {
Michael Kolb14612442011-06-24 13:06:29 -07001628 return mContext.getString(R.string.title_bar_loading);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001629 }
John Reck30c714c2010-12-16 17:30:34 -08001630 return mCurrentState.mTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001631 }
1632
1633 /**
John Reck30c714c2010-12-16 17:30:34 -08001634 * Get the favicon of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001635 */
1636 Bitmap getFavicon() {
John Reck1cf4b792011-07-26 10:22:22 -07001637 if (mCurrentState.mFavicon != null) {
1638 return mCurrentState.mFavicon;
1639 }
1640 return getDefaultFavicon(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001641 }
1642
John Recke969cc52010-12-21 17:24:43 -08001643 public boolean isBookmarkedSite() {
1644 return mCurrentState.mIsBookmarkedSite;
1645 }
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001646
Grace Kloba22ac16e2009-10-07 18:00:23 -07001647 /**
1648 * Return the tab's error console. Creates the console if createIfNEcessary
1649 * is true and we haven't already created the console.
1650 * @param createIfNecessary Flag to indicate if the console should be
1651 * created if it has not been already.
1652 * @return The tab's error console, or null if one has not been created and
1653 * createIfNecessary is false.
1654 */
1655 ErrorConsoleView getErrorConsole(boolean createIfNecessary) {
1656 if (createIfNecessary && mErrorConsole == null) {
Michael Kolb14612442011-06-24 13:06:29 -07001657 mErrorConsole = new ErrorConsoleView(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001658 mErrorConsole.setWebView(mMainView);
1659 }
1660 return mErrorConsole;
1661 }
1662
Steve Block08a6f0c2011-10-06 12:12:53 +01001663 /**
1664 * Sets the security state, clears the SSL certificate error and informs
1665 * the controller.
1666 */
Steve Block2466eff2011-10-03 15:33:09 +01001667 private void setSecurityState(SecurityState securityState) {
1668 mCurrentState.mSecurityState = securityState;
Steve Block08a6f0c2011-10-06 12:12:53 +01001669 mCurrentState.mSslCertificateError = null;
Steve Block2466eff2011-10-03 15:33:09 +01001670 mWebViewController.onUpdatedSecurityState(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001671 }
1672
1673 /**
Steve Block2466eff2011-10-03 15:33:09 +01001674 * @return The tab's security state.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001675 */
Steve Block2466eff2011-10-03 15:33:09 +01001676 SecurityState getSecurityState() {
1677 return mCurrentState.mSecurityState;
John Reck30c714c2010-12-16 17:30:34 -08001678 }
1679
Steve Block08a6f0c2011-10-06 12:12:53 +01001680 /**
1681 * Gets the SSL certificate error, if any, for the page's main resource.
1682 * This is only non-null when the security state is
1683 * SECURITY_STATE_BAD_CERTIFICATE.
1684 */
1685 SslError getSslCertificateError() {
1686 return mCurrentState.mSslCertificateError;
1687 }
1688
John Reck30c714c2010-12-16 17:30:34 -08001689 int getLoadProgress() {
1690 if (mInPageLoad) {
1691 return mPageLoadProgress;
1692 }
1693 return 100;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001694 }
1695
1696 /**
1697 * @return TRUE if onPageStarted is called while onPageFinished is not
1698 * called yet.
1699 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001700 boolean inPageLoad() {
1701 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001702 }
1703
Grace Kloba22ac16e2009-10-07 18:00:23 -07001704 /**
John Reck1cf4b792011-07-26 10:22:22 -07001705 * @return The Bundle with the tab's state if it can be saved, otherwise null
Grace Kloba22ac16e2009-10-07 18:00:23 -07001706 */
John Reck1cf4b792011-07-26 10:22:22 -07001707 public Bundle saveState() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001708 // If the WebView is null it means we ran low on memory and we already
1709 // stored the saved state in mSavedState.
1710 if (mMainView == null) {
John Reck1cf4b792011-07-26 10:22:22 -07001711 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001712 }
John Reck6c2e2f32011-08-22 13:41:23 -07001713
1714 if (TextUtils.isEmpty(mCurrentState.mUrl)) {
John Reck1cf4b792011-07-26 10:22:22 -07001715 return null;
John Reck24f18262011-06-17 14:47:20 -07001716 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001717
1718 mSavedState = new Bundle();
John Reck6c2e2f32011-08-22 13:41:23 -07001719 WebBackForwardList savedList = mMainView.saveState(mSavedState);
1720 if (savedList == null || savedList.getSize() == 0) {
1721 Log.w(LOGTAG, "Failed to save back/forward list for "
1722 + mCurrentState.mUrl);
1723 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001724
Michael Kolbc831b632011-05-11 09:30:34 -07001725 mSavedState.putLong(ID, mId);
John Reck30c714c2010-12-16 17:30:34 -08001726 mSavedState.putString(CURRURL, mCurrentState.mUrl);
1727 mSavedState.putString(CURRTITLE, mCurrentState.mTitle);
John Reck1cf4b792011-07-26 10:22:22 -07001728 mSavedState.putBoolean(INCOGNITO, mMainView.isPrivateBrowsingEnabled());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001729 if (mAppId != null) {
1730 mSavedState.putString(APPID, mAppId);
1731 }
Michael Kolbe28b3472011-08-04 16:54:31 -07001732 mSavedState.putBoolean(CLOSEFLAG, mCloseOnBack);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001733 // Remember the parent tab so the relationship can be restored.
Michael Kolbc831b632011-05-11 09:30:34 -07001734 if (mParent != null) {
1735 mSavedState.putLong(PARENTTAB, mParent.mId);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001736 }
John Reckb0a86db2011-05-24 14:05:58 -07001737 mSavedState.putBoolean(USERAGENT,
1738 mSettings.hasDesktopUseragent(getWebView()));
John Reck1cf4b792011-07-26 10:22:22 -07001739 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001740 }
1741
1742 /*
1743 * Restore the state of the tab.
1744 */
John Reck1cf4b792011-07-26 10:22:22 -07001745 private void restoreState(Bundle b) {
1746 mSavedState = b;
1747 if (mSavedState == null) {
1748 return;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001749 }
1750 // Restore the internal state even if the WebView fails to restore.
1751 // This will maintain the app id, original url and close-on-exit values.
Michael Kolbc831b632011-05-11 09:30:34 -07001752 mId = b.getLong(ID);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001753 mAppId = b.getString(APPID);
Michael Kolbe28b3472011-08-04 16:54:31 -07001754 mCloseOnBack = b.getBoolean(CLOSEFLAG);
John Reck8b9bb8b2012-03-08 13:19:40 -08001755 restoreUserAgent();
John Reck1cf4b792011-07-26 10:22:22 -07001756 String url = b.getString(CURRURL);
1757 String title = b.getString(CURRTITLE);
1758 boolean incognito = b.getBoolean(INCOGNITO);
1759 mCurrentState = new PageState(mContext, incognito, url, null);
1760 mCurrentState.mTitle = title;
1761 synchronized (Tab.this) {
1762 if (mCapture != null) {
John Reck4eadc342011-10-31 14:04:10 -07001763 DataController.getInstance(mContext).loadThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07001764 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001765 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001766 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04001767
John Reck8b9bb8b2012-03-08 13:19:40 -08001768 private void restoreUserAgent() {
1769 if (mMainView == null || mSavedState == null) {
1770 return;
1771 }
1772 if (mSavedState.getBoolean(USERAGENT)
1773 != mSettings.hasDesktopUseragent(mMainView)) {
1774 mSettings.toggleDesktopUseragent(mMainView);
1775 }
1776 }
1777
Leon Scroggins1961ed22010-12-07 15:22:21 -05001778 public void updateBookmarkedStatus() {
John Recke969cc52010-12-21 17:24:43 -08001779 mDataController.queryBookmarkStatus(getUrl(), mIsBookmarkCallback);
Leon Scroggins1961ed22010-12-07 15:22:21 -05001780 }
1781
John Recke969cc52010-12-21 17:24:43 -08001782 private DataController.OnQueryUrlIsBookmark mIsBookmarkCallback
1783 = new DataController.OnQueryUrlIsBookmark() {
1784 @Override
1785 public void onQueryUrlIsBookmark(String url, boolean isBookmark) {
1786 if (mCurrentState.mUrl.equals(url)) {
1787 mCurrentState.mIsBookmarkedSite = isBookmark;
1788 mWebViewController.bookmarkedStatusHasChanged(Tab.this);
1789 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001790 }
John Recke969cc52010-12-21 17:24:43 -08001791 };
Michael Kolb1acef692011-03-08 14:12:06 -08001792
Michael Kolbeb95db42011-03-03 10:38:40 -08001793 public Bitmap getScreenshot() {
John Reck1cf4b792011-07-26 10:22:22 -07001794 synchronized (Tab.this) {
1795 return mCapture;
1796 }
Michael Kolbeb95db42011-03-03 10:38:40 -08001797 }
1798
John Reck541f55a2011-06-07 16:34:43 -07001799 public boolean isSnapshot() {
John Reck541f55a2011-06-07 16:34:43 -07001800 return false;
1801 }
1802
John Reck68234a92012-04-19 15:27:12 -07001803 private static class SaveCallback implements ValueCallback<Boolean> {
1804 boolean mResult;
1805
1806 @Override
1807 public void onReceiveValue(Boolean value) {
1808 mResult = value;
1809 synchronized (this) {
1810 notifyAll();
John Reck8cc92352011-07-06 17:41:52 -07001811 }
John Reck541f55a2011-06-07 16:34:43 -07001812 }
John Reck68234a92012-04-19 15:27:12 -07001813
1814 }
1815
1816 /**
1817 * Must be called on the UI thread
1818 */
1819 public ContentValues createSnapshotValues() {
1820 WebViewClassic web = getWebViewClassic();
1821 if (web == null) return null;
John Reckd8c74522011-06-14 08:45:00 -07001822 ContentValues values = new ContentValues();
1823 values.put(Snapshots.TITLE, mCurrentState.mTitle);
1824 values.put(Snapshots.URL, mCurrentState.mUrl);
John Reck68234a92012-04-19 15:27:12 -07001825 values.put(Snapshots.BACKGROUND, web.getPageBackgroundColor());
John Reck8cc92352011-07-06 17:41:52 -07001826 values.put(Snapshots.DATE_CREATED, System.currentTimeMillis());
1827 values.put(Snapshots.FAVICON, compressBitmap(getFavicon()));
1828 Bitmap screenshot = Controller.createScreenshot(mMainView,
1829 Controller.getDesiredThumbnailWidth(mContext),
1830 Controller.getDesiredThumbnailHeight(mContext));
1831 values.put(Snapshots.THUMBNAIL, compressBitmap(screenshot));
John Reckd8c74522011-06-14 08:45:00 -07001832 return values;
John Reck541f55a2011-06-07 16:34:43 -07001833 }
1834
John Reck68234a92012-04-19 15:27:12 -07001835 /**
1836 * Probably want to call this on a background thread
1837 */
1838 public boolean saveViewState(ContentValues values) {
1839 WebViewClassic web = getWebViewClassic();
1840 if (web == null) return false;
1841 String path = UUID.randomUUID().toString();
1842 SaveCallback callback = new SaveCallback();
1843 OutputStream outs = null;
1844 try {
1845 outs = mContext.openFileOutput(path, Context.MODE_PRIVATE);
1846 GZIPOutputStream stream = new GZIPOutputStream(outs);
1847 synchronized (callback) {
1848 web.saveViewState(stream, callback);
1849 callback.wait();
1850 }
1851 stream.flush();
1852 stream.close();
1853 } catch (Exception e) {
1854 Log.w(LOGTAG, "Failed to save view state", e);
1855 if (outs != null) {
1856 try {
1857 outs.close();
1858 } catch (IOException ignore) {}
1859 }
1860 File file = mContext.getFileStreamPath(path);
1861 if (file.exists() && !file.delete()) {
1862 file.deleteOnExit();
1863 }
1864 return false;
1865 }
1866 File savedFile = mContext.getFileStreamPath(path);
1867 if (!callback.mResult) {
1868 if (!savedFile.delete()) {
1869 savedFile.deleteOnExit();
1870 }
1871 return false;
1872 }
1873 long size = savedFile.length();
1874 values.put(Snapshots.VIEWSTATE_PATH, path);
1875 values.put(Snapshots.VIEWSTATE_SIZE, size);
1876 return true;
1877 }
1878
John Reck8cc92352011-07-06 17:41:52 -07001879 public byte[] compressBitmap(Bitmap bitmap) {
1880 if (bitmap == null) {
1881 return null;
1882 }
1883 ByteArrayOutputStream stream = new ByteArrayOutputStream();
1884 bitmap.compress(CompressFormat.PNG, 100, stream);
1885 return stream.toByteArray();
1886 }
1887
John Reck26b18322011-06-21 13:08:58 -07001888 public void loadUrl(String url, Map<String, String> headers) {
1889 if (mMainView != null) {
Michael Kolba53c9892011-10-05 13:31:40 -07001890 mPageLoadProgress = INITIAL_PROGRESS;
1891 mInPageLoad = true;
Michael Kolb14612442011-06-24 13:06:29 -07001892 mCurrentState = new PageState(mContext, false, url, null);
John Reck26b18322011-06-21 13:08:58 -07001893 mWebViewController.onPageStarted(this, mMainView, null);
1894 mMainView.loadUrl(url, headers);
1895 }
1896 }
1897
Michael Kolb9ef259a2011-07-12 15:33:08 -07001898 protected void capture() {
1899 if (mMainView == null || mCapture == null) return;
John Reck4eadc342011-10-31 14:04:10 -07001900 if (mMainView.getContentWidth() <= 0 || mMainView.getContentHeight() <= 0) {
1901 return;
1902 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001903 Canvas c = new Canvas(mCapture);
1904 final int left = mMainView.getScrollX();
1905 final int top = mMainView.getScrollY() + mMainView.getVisibleTitleHeight();
Michael Kolba3194d02011-09-07 11:23:51 -07001906 int state = c.save();
Michael Kolb9ef259a2011-07-12 15:33:08 -07001907 c.translate(-left, -top);
1908 float scale = mCaptureWidth / (float) mMainView.getWidth();
1909 c.scale(scale, scale, left, top);
John Reck8ee633f2011-08-09 16:00:35 -07001910 if (mMainView instanceof BrowserWebView) {
1911 ((BrowserWebView)mMainView).drawContent(c);
1912 } else {
1913 mMainView.draw(c);
1914 }
Michael Kolba3194d02011-09-07 11:23:51 -07001915 c.restoreToCount(state);
1916 // manually anti-alias the edges for the tilt
1917 c.drawRect(0, 0, 1, mCapture.getHeight(), sAlphaPaint);
1918 c.drawRect(mCapture.getWidth() - 1, 0, mCapture.getWidth(),
1919 mCapture.getHeight(), sAlphaPaint);
1920 c.drawRect(0, 0, mCapture.getWidth(), 1, sAlphaPaint);
1921 c.drawRect(0, mCapture.getHeight() - 1, mCapture.getWidth(),
1922 mCapture.getHeight(), sAlphaPaint);
Dianne Hackborn43cfe8a2011-08-02 16:59:35 -07001923 c.setBitmap(null);
John Reck8ee633f2011-08-09 16:00:35 -07001924 mHandler.removeMessages(MSG_CAPTURE);
John Reck1cf4b792011-07-26 10:22:22 -07001925 persistThumbnail();
John Reck8ee633f2011-08-09 16:00:35 -07001926 TabControl tc = mWebViewController.getTabControl();
1927 if (tc != null) {
1928 OnThumbnailUpdatedListener updateListener
1929 = tc.getOnThumbnailUpdatedListener();
1930 if (updateListener != null) {
1931 updateListener.onThumbnailUpdated(this);
1932 }
1933 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001934 }
1935
1936 @Override
1937 public void onNewPicture(WebView view, Picture picture) {
John Reck1cf4b792011-07-26 10:22:22 -07001938 postCapture();
1939 }
1940
1941 private void postCapture() {
Michael Kolb9ef259a2011-07-12 15:33:08 -07001942 if (!mHandler.hasMessages(MSG_CAPTURE)) {
1943 mHandler.sendEmptyMessageDelayed(MSG_CAPTURE, CAPTURE_DELAY);
1944 }
1945 }
1946
John Reckef654f12011-07-12 16:42:08 -07001947 public boolean canGoBack() {
1948 return mMainView != null ? mMainView.canGoBack() : false;
1949 }
1950
1951 public boolean canGoForward() {
1952 return mMainView != null ? mMainView.canGoForward() : false;
1953 }
1954
1955 public void goBack() {
1956 if (mMainView != null) {
1957 mMainView.goBack();
1958 }
1959 }
1960
1961 public void goForward() {
1962 if (mMainView != null) {
1963 mMainView.goForward();
1964 }
1965 }
1966
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001967 /**
1968 * Causes the tab back/forward stack to be cleared once, if the given URL is the next URL
1969 * to be added to the stack.
1970 *
1971 * This is used to ensure that preloaded URLs that are not subsequently seen by the user do
1972 * not appear in the back stack.
1973 */
Mathew Inwooda829d552011-09-02 14:16:25 +01001974 public void clearBackStackWhenItemAdded(Pattern urlPattern) {
1975 mClearHistoryUrlPattern = urlPattern;
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001976 }
1977
John Reck1cf4b792011-07-26 10:22:22 -07001978 protected void persistThumbnail() {
John Reck4eadc342011-10-31 14:04:10 -07001979 DataController.getInstance(mContext).saveThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07001980 }
1981
1982 protected void deleteThumbnail() {
John Reck4eadc342011-10-31 14:04:10 -07001983 DataController.getInstance(mContext).deleteThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07001984 }
1985
John Reck4eadc342011-10-31 14:04:10 -07001986 void updateCaptureFromBlob(byte[] blob) {
John Reck1cf4b792011-07-26 10:22:22 -07001987 synchronized (Tab.this) {
1988 if (mCapture == null) {
1989 return;
1990 }
Michael Kolbbd4c00a2011-08-02 11:27:12 -07001991 ByteBuffer buffer = ByteBuffer.wrap(blob);
Michael Kolbd837a112011-08-09 14:00:18 -07001992 try {
1993 mCapture.copyPixelsFromBuffer(buffer);
1994 } catch (RuntimeException rex) {
1995 Log.e(LOGTAG, "Load capture has mismatched sizes; buffer: "
1996 + buffer.capacity() + " blob: " + blob.length
1997 + "capture: " + mCapture.getByteCount());
1998 throw rex;
Michael Kolbbd4c00a2011-08-02 11:27:12 -07001999 }
John Reck1cf4b792011-07-26 10:22:22 -07002000 }
2001 }
2002
John Reck52be4782011-08-26 15:37:29 -07002003 @Override
2004 public String toString() {
2005 StringBuilder builder = new StringBuilder(100);
2006 builder.append(mId);
2007 builder.append(") has parent: ");
2008 if (getParent() != null) {
2009 builder.append("true[");
2010 builder.append(getParent().getId());
2011 builder.append("]");
2012 } else {
2013 builder.append("false");
2014 }
2015 builder.append(", incog: ");
2016 builder.append(isPrivateBrowsingEnabled());
2017 if (!isPrivateBrowsingEnabled()) {
2018 builder.append(", title: ");
2019 builder.append(getTitle());
2020 builder.append(", url: ");
2021 builder.append(getUrl());
2022 }
2023 return builder.toString();
2024 }
2025
Steve Block4895b012011-10-03 16:26:46 +01002026 private void handleProceededAfterSslError(SslError error) {
2027 if (error.getUrl().equals(mCurrentState.mUrl)) {
2028 // The security state should currently be SECURITY_STATE_SECURE.
2029 setSecurityState(SecurityState.SECURITY_STATE_BAD_CERTIFICATE);
Steve Block08a6f0c2011-10-06 12:12:53 +01002030 mCurrentState.mSslCertificateError = error;
Steve Block4895b012011-10-03 16:26:46 +01002031 } else if (getSecurityState() == SecurityState.SECURITY_STATE_SECURE) {
Steve Block08a6f0c2011-10-06 12:12:53 +01002032 // The page's main resource is secure and this error is for a
2033 // sub-resource.
Steve Block4895b012011-10-03 16:26:46 +01002034 setSecurityState(SecurityState.SECURITY_STATE_MIXED);
2035 }
2036 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002037}