blob: 3b57d9eefbac218821249f7adb84dae6ec10053f [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
Bijan Amirzada41242f22014-03-21 12:12:18 -070017package com.android.browser;
Grace Kloba22ac16e2009-10-07 18:00:23 -070018
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;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080026import android.content.res.Resources;
Grace Kloba22ac16e2009-10-07 18:00:23 -070027import android.graphics.Bitmap;
John Reck8cc92352011-07-06 17:41:52 -070028import android.graphics.Bitmap.CompressFormat;
Michael Kolb9ef259a2011-07-12 15:33:08 -070029import android.graphics.BitmapFactory;
30import android.graphics.Canvas;
Michael Kolbc3af0672011-08-09 10:24:41 -070031import android.graphics.Color;
Michael Kolba3194d02011-09-07 11:23:51 -070032import android.graphics.Paint;
Michael Kolb9ef259a2011-07-12 15:33:08 -070033import android.graphics.Picture;
Michael Kolba3194d02011-09-07 11:23:51 -070034import android.graphics.PorterDuff;
35import android.graphics.PorterDuffXfermode;
Tarun Nainaniea28dde2014-08-27 17:25:09 -070036import android.graphics.Rect;
Grace Kloba22ac16e2009-10-07 18:00:23 -070037import android.net.Uri;
38import android.net.http.SslError;
Grace Kloba22ac16e2009-10-07 18:00:23 -070039import android.os.Bundle;
Michael Kolb9ef259a2011-07-12 15:33:08 -070040import android.os.Handler;
Grace Kloba22ac16e2009-10-07 18:00:23 -070041import android.os.Message;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +000042import android.os.SystemClock;
Brian Carlstrom8862c1d2011-06-02 01:05:55 -070043import android.security.KeyChain;
Brian Carlstromaa09cd82011-06-09 16:04:40 -070044import android.security.KeyChainAliasCallback;
John Reck24f18262011-06-17 14:47:20 -070045import android.text.TextUtils;
Grace Kloba22ac16e2009-10-07 18:00:23 -070046import android.util.Log;
47import android.view.KeyEvent;
48import android.view.LayoutInflater;
49import android.view.View;
Grace Kloba50c241e2010-04-20 11:07:50 -070050import android.view.ViewStub;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080051import android.view.View.OnClickListener;
Ben Murdochc42addf2010-01-28 15:19:59 +000052import android.webkit.ConsoleMessage;
Grace Kloba22ac16e2009-10-07 18:00:23 -070053import android.webkit.GeolocationPermissions;
Grace Kloba22ac16e2009-10-07 18:00:23 -070054import android.webkit.URLUtil;
John Reck438bf462011-01-12 18:11:46 -080055import android.webkit.WebResourceResponse;
Grace Kloba22ac16e2009-10-07 18:00:23 -070056import android.webkit.WebStorage;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080057import android.webkit.WebChromeClient.CustomViewCallback;
58import android.webkit.ValueCallback;
Ben Murdoch1d676b62011-01-17 12:54:24 +000059import android.widget.CheckBox;
Ben Murdoch8029a772010-11-16 11:58:21 +000060import android.widget.Toast;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080061import android.widget.FrameLayout;
62import android.widget.Button;
Grace Kloba22ac16e2009-10-07 18:00:23 -070063
Bijan Amirzada41242f22014-03-21 12:12:18 -070064import com.android.browser.TabControl.OnThumbnailUpdatedListener;
65import com.android.browser.homepages.HomeProvider;
66import com.android.browser.mynavigation.MyNavigationUtil;
67import com.android.browser.provider.MyNavigationProvider;
68import com.android.browser.provider.SnapshotProvider.Snapshots;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080069
70import org.codeaurora.swe.BrowserDownloadListener;
71import org.codeaurora.swe.ClientCertRequestHandler;
72import org.codeaurora.swe.HttpAuthHandler;
73import org.codeaurora.swe.SslErrorHandler;
74import org.codeaurora.swe.WebBackForwardList;
75import org.codeaurora.swe.WebBackForwardListClient;
76import org.codeaurora.swe.WebChromeClient;
77import org.codeaurora.swe.WebHistoryItem;
78import org.codeaurora.swe.WebView;
79import org.codeaurora.swe.WebView.PictureListener;
Pankaj Garg1c7380d2014-08-27 14:17:12 -070080import org.codeaurora.swe.WebView.CreateWindowParams;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080081import org.codeaurora.swe.WebViewClient;
82
John Reck541f55a2011-06-07 16:34:43 -070083import java.io.ByteArrayOutputStream;
John Reck2b71d6d2012-04-18 17:42:06 -070084import java.io.File;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080085import java.io.InputStream;
John Reck1cf4b792011-07-26 10:22:22 -070086import java.nio.ByteBuffer;
Michael Kolbfe251992010-07-08 15:41:55 -070087import java.util.LinkedList;
88import java.util.Map;
John Reck2b71d6d2012-04-18 17:42:06 -070089import java.util.UUID;
Michael Kolbfe251992010-07-08 15:41:55 -070090import java.util.Vector;
Mathew Inwooda829d552011-09-02 14:16:25 +010091import java.util.regex.Pattern;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080092import java.sql.Timestamp;
93import java.util.Date;
Michael Kolbfe251992010-07-08 15:41:55 -070094
Grace Kloba22ac16e2009-10-07 18:00:23 -070095/**
96 * Class for maintaining Tabs with a main WebView and a subwindow.
97 */
Michael Kolb9ef259a2011-07-12 15:33:08 -070098class Tab implements PictureListener {
Michael Kolb8233fac2010-10-26 16:08:53 -070099
Grace Kloba22ac16e2009-10-07 18:00:23 -0700100 // Log Tag
101 private static final String LOGTAG = "Tab";
Bijan Amirzada41242f22014-03-21 12:12:18 -0700102 private static final boolean LOGD_ENABLED = com.android.browser.Browser.LOGD_ENABLED;
Ben Murdochc42addf2010-01-28 15:19:59 +0000103 // Special case the logtag for messages for the Console to make it easier to
104 // filter them and match the logtag used for these messages in older versions
105 // of the browser.
106 private static final String CONSOLE_LOGTAG = "browser";
107
Michael Kolb9ef259a2011-07-12 15:33:08 -0700108 private static final int MSG_CAPTURE = 42;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800109 private static final int CAPTURE_DELAY = 1000;
Michael Kolba53c9892011-10-05 13:31:40 -0700110 private static final int INITIAL_PROGRESS = 5;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700111
John Reck1cf4b792011-07-26 10:22:22 -0700112 private static Bitmap sDefaultFavicon;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800113 protected boolean hasCrashed = false;
John Reck1cf4b792011-07-26 10:22:22 -0700114
Michael Kolba3194d02011-09-07 11:23:51 -0700115 private static Paint sAlphaPaint = new Paint();
116 static {
117 sAlphaPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
118 sAlphaPaint.setColor(Color.TRANSPARENT);
119 }
120
Steve Block2466eff2011-10-03 15:33:09 +0100121 public enum SecurityState {
Steve Block4895b012011-10-03 16:26:46 +0100122 // The page's main resource does not use SSL. Note that we use this
123 // state irrespective of the SSL authentication state of sub-resources.
Steve Block2466eff2011-10-03 15:33:09 +0100124 SECURITY_STATE_NOT_SECURE,
Steve Block4895b012011-10-03 16:26:46 +0100125 // The page's main resource uses SSL and the certificate is good. The
126 // same is true of all sub-resources.
Steve Block2466eff2011-10-03 15:33:09 +0100127 SECURITY_STATE_SECURE,
Steve Block4895b012011-10-03 16:26:46 +0100128 // The page's main resource uses SSL and the certificate is good, but
129 // some sub-resources either do not use SSL or have problems with their
130 // certificates.
Steve Block2466eff2011-10-03 15:33:09 +0100131 SECURITY_STATE_MIXED,
Steve Block4895b012011-10-03 16:26:46 +0100132 // The page's main resource uses SSL but there is a problem with its
133 // certificate.
134 SECURITY_STATE_BAD_CERTIFICATE,
John Reck30c714c2010-12-16 17:30:34 -0800135 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700136
Michael Kolb14612442011-06-24 13:06:29 -0700137 Context mContext;
John Reckd8c74522011-06-14 08:45:00 -0700138 protected WebViewController mWebViewController;
Michael Kolb8233fac2010-10-26 16:08:53 -0700139
Michael Kolbc831b632011-05-11 09:30:34 -0700140 // The tab ID
John Reckd8c74522011-06-14 08:45:00 -0700141 private long mId = -1;
Michael Kolbc831b632011-05-11 09:30:34 -0700142
Grace Kloba22ac16e2009-10-07 18:00:23 -0700143 // The Geolocation permissions prompt
144 private GeolocationPermissionsPrompt mGeolocationPermissionsPrompt;
145 // Main WebView wrapper
Michael Kolba713ec82010-11-29 17:27:06 -0800146 private View mContainer;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700147 // Main WebView
148 private WebView mMainView;
149 // Subwindow container
150 private View mSubViewContainer;
151 // Subwindow WebView
152 private WebView mSubView;
153 // Saved bundle for when we are running low on memory. It contains the
154 // information needed to restore the WebView if the user goes back to the
155 // tab.
156 private Bundle mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700157 // Parent Tab. This is the Tab that created this Tab, or null if the Tab was
158 // created by the UI
Michael Kolbc831b632011-05-11 09:30:34 -0700159 private Tab mParent;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700160 // Tab that constructed by this Tab. This is used when this Tab is
161 // destroyed, it clears all mParentTab values in the children.
Michael Kolbc831b632011-05-11 09:30:34 -0700162 private Vector<Tab> mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700163 // If true, the tab is in the foreground of the current activity.
164 private boolean mInForeground;
Michael Kolb8233fac2010-10-26 16:08:53 -0700165 // If true, the tab is in page loading state (after onPageStarted,
166 // before onPageFinsihed)
167 private boolean mInPageLoad;
Tarun Nainani8eb00912014-07-17 12:28:32 -0700168 private boolean mPageFinished;
John Reck38b39652012-06-05 09:22:59 -0700169 private boolean mDisableOverrideUrlLoading;
John Reck30c714c2010-12-16 17:30:34 -0800170 // The last reported progress of the current page
171 private int mPageLoadProgress;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000172 // The time the load started, used to find load page time
173 private long mLoadStartTime;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700174 // Application identifier used to find tabs that another application wants
175 // to reuse.
176 private String mAppId;
Michael Kolbe28b3472011-08-04 16:54:31 -0700177 // flag to indicate if tab should be closed on back
178 private boolean mCloseOnBack;
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700179 // flag to indicate if the tab was opened from an intent
180 private boolean mDerivedFromIntent = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700181 // Keep the original url around to avoid killing the old WebView if the url
182 // has not changed.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700183 // Error console for the tab
184 private ErrorConsoleView mErrorConsole;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -0500185 // The listener that gets invoked when a download is started from the
186 // mMainView
Selim Gurun0b3d66f2012-08-29 13:08:13 -0700187 private final BrowserDownloadListener mDownloadListener;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500188 // Listener used to know when we move forward or back in the history list.
189 private final WebBackForwardListClient mWebBackForwardListClient;
John Recke969cc52010-12-21 17:24:43 -0800190 private DataController mDataController;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700191
192 // AsyncTask for downloading touch icons
193 DownloadTouchIcon mTouchIconLoader;
194
John Reck35e9dd62011-04-25 09:01:54 -0700195 private BrowserSettings mSettings;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700196 private int mCaptureWidth;
197 private int mCaptureHeight;
198 private Bitmap mCapture;
199 private Handler mHandler;
Michael Kolb72864272012-05-03 15:42:15 -0700200 private boolean mUpdateThumbnail;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800201 private Timestamp timestamp;
Vivek Sekharf96064b2014-07-28 16:32:34 -0700202 private boolean mFullScreen = false;
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800203 private boolean mReceivedError;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700204
Mathew Inwood1dd8e822011-08-03 14:34:29 +0100205 /**
Tarun Nainaniac724562014-11-03 17:40:38 -0800206 * See {@link #clearBackStackWhenItemAdded(java.util.regex.Pattern)}.
Mathew Inwood1dd8e822011-08-03 14:34:29 +0100207 */
Mathew Inwooda829d552011-09-02 14:16:25 +0100208 private Pattern mClearHistoryUrlPattern;
Mathew Inwood1dd8e822011-08-03 14:34:29 +0100209
John Reck1cf4b792011-07-26 10:22:22 -0700210 private static synchronized Bitmap getDefaultFavicon(Context context) {
211 if (sDefaultFavicon == null) {
212 sDefaultFavicon = BitmapFactory.decodeResource(
Enrico Rosd6efa972014-12-02 19:49:59 -0800213 context.getResources(), R.drawable.ic_deco_favicon_normal);
John Reck1cf4b792011-07-26 10:22:22 -0700214 }
215 return sDefaultFavicon;
216 }
Michael Kolbeb95db42011-03-03 10:38:40 -0800217
John Reck30c714c2010-12-16 17:30:34 -0800218 // All the state needed for a page
John Reckd8c74522011-06-14 08:45:00 -0700219 protected static class PageState {
John Reck30c714c2010-12-16 17:30:34 -0800220 String mUrl;
John Reckdb22ec42011-06-29 11:31:24 -0700221 String mOriginalUrl;
John Reck30c714c2010-12-16 17:30:34 -0800222 String mTitle;
Steve Block2466eff2011-10-03 15:33:09 +0100223 SecurityState mSecurityState;
Steve Block08a6f0c2011-10-06 12:12:53 +0100224 // This is non-null only when mSecurityState is SECURITY_STATE_BAD_CERTIFICATE.
225 SslError mSslCertificateError;
John Reck30c714c2010-12-16 17:30:34 -0800226 Bitmap mFavicon;
Steve Block08a6f0c2011-10-06 12:12:53 +0100227 boolean mIsBookmarkedSite;
228 boolean mIncognito;
John Reck30c714c2010-12-16 17:30:34 -0800229
230 PageState(Context c, boolean incognito) {
John Reck502a3532011-08-16 14:21:46 -0700231 mIncognito = incognito;
232 if (mIncognito) {
John Reckdb22ec42011-06-29 11:31:24 -0700233 mOriginalUrl = mUrl = "browser:incognito";
John Reck30c714c2010-12-16 17:30:34 -0800234 mTitle = c.getString(R.string.new_incognito_tab);
John Reck30c714c2010-12-16 17:30:34 -0800235 } else {
John Reckdb22ec42011-06-29 11:31:24 -0700236 mOriginalUrl = mUrl = "";
John Reck30c714c2010-12-16 17:30:34 -0800237 mTitle = c.getString(R.string.new_tab);
John Reck30c714c2010-12-16 17:30:34 -0800238 }
Steve Block2466eff2011-10-03 15:33:09 +0100239 mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
John Reck30c714c2010-12-16 17:30:34 -0800240 }
241
242 PageState(Context c, boolean incognito, String url, Bitmap favicon) {
John Reck502a3532011-08-16 14:21:46 -0700243 mIncognito = incognito;
John Reckdb22ec42011-06-29 11:31:24 -0700244 mOriginalUrl = mUrl = url;
John Reck30c714c2010-12-16 17:30:34 -0800245 if (URLUtil.isHttpsUrl(url)) {
Steve Block2466eff2011-10-03 15:33:09 +0100246 mSecurityState = SecurityState.SECURITY_STATE_SECURE;
John Reck30c714c2010-12-16 17:30:34 -0800247 } else {
Steve Block2466eff2011-10-03 15:33:09 +0100248 mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
John Reck30c714c2010-12-16 17:30:34 -0800249 }
John Reck1cf4b792011-07-26 10:22:22 -0700250 mFavicon = favicon;
John Reck30c714c2010-12-16 17:30:34 -0800251 }
John Reck1cf4b792011-07-26 10:22:22 -0700252
Grace Kloba22ac16e2009-10-07 18:00:23 -0700253 }
254
John Reck30c714c2010-12-16 17:30:34 -0800255 // The current/loading page's state
John Reckd8c74522011-06-14 08:45:00 -0700256 protected PageState mCurrentState;
John Reck30c714c2010-12-16 17:30:34 -0800257
Grace Kloba22ac16e2009-10-07 18:00:23 -0700258 // Used for saving and restoring each Tab
Michael Kolbc831b632011-05-11 09:30:34 -0700259 static final String ID = "ID";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700260 static final String CURRURL = "currentUrl";
261 static final String CURRTITLE = "currentTitle";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700262 static final String PARENTTAB = "parentTab";
263 static final String APPID = "appid";
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700264 static final String INCOGNITO = "privateBrowsingEnabled";
John Reckb0a86db2011-05-24 14:05:58 -0700265 static final String USERAGENT = "useragent";
Michael Kolbe28b3472011-08-04 16:54:31 -0700266 static final String CLOSEFLAG = "closeOnBack";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700267
Grace Kloba22ac16e2009-10-07 18:00:23 -0700268 // Container class for the next error dialog that needs to be displayed
269 private class ErrorDialog {
270 public final int mTitle;
271 public final String mDescription;
272 public final int mError;
273 ErrorDialog(int title, String desc, int error) {
274 mTitle = title;
275 mDescription = desc;
276 mError = error;
277 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700278 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700279
280 private void processNextError() {
281 if (mQueuedErrors == null) {
282 return;
283 }
284 // The first one is currently displayed so just remove it.
285 mQueuedErrors.removeFirst();
286 if (mQueuedErrors.size() == 0) {
287 mQueuedErrors = null;
288 return;
289 }
290 showError(mQueuedErrors.getFirst());
291 }
292
293 private DialogInterface.OnDismissListener mDialogListener =
294 new DialogInterface.OnDismissListener() {
295 public void onDismiss(DialogInterface d) {
296 processNextError();
297 }
298 };
299 private LinkedList<ErrorDialog> mQueuedErrors;
300
301 private void queueError(int err, String desc) {
302 if (mQueuedErrors == null) {
303 mQueuedErrors = new LinkedList<ErrorDialog>();
304 }
305 for (ErrorDialog d : mQueuedErrors) {
306 if (d.mError == err) {
307 // Already saw a similar error, ignore the new one.
308 return;
309 }
310 }
311 ErrorDialog errDialog = new ErrorDialog(
312 err == WebViewClient.ERROR_FILE_NOT_FOUND ?
313 R.string.browserFrameFileErrorLabel :
314 R.string.browserFrameNetworkErrorLabel,
315 desc, err);
316 mQueuedErrors.addLast(errDialog);
317
318 // Show the dialog now if the queue was empty and it is in foreground
319 if (mQueuedErrors.size() == 1 && mInForeground) {
320 showError(errDialog);
321 }
322 }
323
324 private void showError(ErrorDialog errDialog) {
325 if (mInForeground) {
Michael Kolb14612442011-06-24 13:06:29 -0700326 AlertDialog d = new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700327 .setTitle(errDialog.mTitle)
328 .setMessage(errDialog.mDescription)
329 .setPositiveButton(R.string.ok, null)
330 .create();
331 d.setOnDismissListener(mDialogListener);
332 d.show();
333 }
334 }
335
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800336 protected void replaceCrashView(View view, View container) {
337 if (hasCrashed && (view == mMainView)) {
338 final FrameLayout wrapper = (FrameLayout) container.findViewById(R.id.webview_wrapper);
339 wrapper.removeAllViewsInLayout();
340 wrapper.addView(view);
341 hasCrashed = false;
342 }
Vivek Sekhar2868b8d2014-12-03 17:22:50 -0800343 mMainView.reload();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800344 }
345
346 protected void showCrashView() {
347 if (hasCrashed) {
348 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
349 Context.LAYOUT_INFLATER_SERVICE);
350 final View crashLayout = inflater.inflate(R.layout.browser_tab_crash, null);
351 final FrameLayout wrapper =
352 (FrameLayout) mContainer.findViewById(R.id.webview_wrapper);
353 wrapper.removeAllViewsInLayout();
354 wrapper.addView(crashLayout);
355 mContainer.requestFocus();
356 Button reloadBtn = (Button) crashLayout.findViewById(R.id.browser_crash_reload_btn);
357 reloadBtn.setOnClickListener(new OnClickListener() {
358 @Override
359 public void onClick(View arg0) {
360 replaceCrashView(mMainView, mContainer);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800361 }
362 });
363 }
364 }
365
Grace Kloba22ac16e2009-10-07 18:00:23 -0700366 // -------------------------------------------------------------------------
367 // WebViewClient implementation for the main WebView
368 // -------------------------------------------------------------------------
369
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800370 private final WebViewClient mWebViewClient = new WebViewClient() {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500371 private Message mDontResend;
372 private Message mResend;
Michael Kolb47bd1e42011-09-01 15:25:00 -0700373
374 private boolean providersDiffer(String url, String otherUrl) {
375 Uri uri1 = Uri.parse(url);
376 Uri uri2 = Uri.parse(otherUrl);
377 return !uri1.getEncodedAuthority().equals(uri2.getEncodedAuthority());
378 }
379
Grace Kloba22ac16e2009-10-07 18:00:23 -0700380 @Override
381 public void onPageStarted(WebView view, String url, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700382 mInPageLoad = true;
Tarun Nainani8eb00912014-07-17 12:28:32 -0700383 mPageFinished = false;
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800384 mReceivedError = false;
Michael Kolb72864272012-05-03 15:42:15 -0700385 mUpdateThumbnail = true;
Michael Kolba53c9892011-10-05 13:31:40 -0700386 mPageLoadProgress = INITIAL_PROGRESS;
Michael Kolb14612442011-06-24 13:06:29 -0700387 mCurrentState = new PageState(mContext,
John Reck30c714c2010-12-16 17:30:34 -0800388 view.isPrivateBrowsingEnabled(), url, favicon);
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000389 mLoadStartTime = SystemClock.uptimeMillis();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700390
391 // If we start a touch icon load and then load a new page, we don't
392 // want to cancel the current touch icon loader. But, we do want to
393 // create a new one when the touch icon url is known.
394 if (mTouchIconLoader != null) {
395 mTouchIconLoader.mTab = null;
396 mTouchIconLoader = null;
397 }
398
399 // reset the error console
400 if (mErrorConsole != null) {
401 mErrorConsole.clearErrorMessages();
Michael Kolb8233fac2010-10-26 16:08:53 -0700402 if (mWebViewController.shouldShowErrorConsole()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700403 mErrorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
404 }
405 }
406
Grace Kloba22ac16e2009-10-07 18:00:23 -0700407 // finally update the UI in the activity if it is in the foreground
John Reck324d4402011-01-11 16:56:42 -0800408 mWebViewController.onPageStarted(Tab.this, view, favicon);
Leon Scroggins4cd97792010-12-03 15:31:56 -0500409
John Recke969cc52010-12-21 17:24:43 -0800410 updateBookmarkedStatus();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700411 }
412
413 @Override
414 public void onPageFinished(WebView view, String url) {
John Reck38b39652012-06-05 09:22:59 -0700415 mDisableOverrideUrlLoading = false;
John Reck5b691842010-11-29 11:21:13 -0800416 if (!isPrivateBrowsingEnabled()) {
417 LogTag.logPageFinishedLoading(
418 url, SystemClock.uptimeMillis() - mLoadStartTime);
419 }
John Reck1cf4b792011-07-26 10:22:22 -0700420 syncCurrentState(view, url);
John Reck324d4402011-01-11 16:56:42 -0800421 mWebViewController.onPageFinished(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700422 }
423
424 // return true if want to hijack the url to let another app to handle it
425 @Override
426 public boolean shouldOverrideUrlLoading(WebView view, String url) {
John Reck38b39652012-06-05 09:22:59 -0700427 if (!mDisableOverrideUrlLoading && mInForeground) {
Michael Kolb18eb3772010-12-10 14:29:51 -0800428 return mWebViewController.shouldOverrideUrlLoading(Tab.this,
429 view, url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700430 } else {
431 return false;
432 }
433 }
434
Vivek Sekharb991edb2014-12-17 18:18:07 -0800435 @Override
436 public boolean shouldDownloadFavicon(WebView view, String url) {
437 return true;
438 }
439
Grace Kloba22ac16e2009-10-07 18:00:23 -0700440 /**
Steve Block2466eff2011-10-03 15:33:09 +0100441 * Updates the security state. This method is called when we discover
442 * another resource to be loaded for this page (for example,
443 * javascript). While we update the security state, we do not update
444 * the lock icon until we are done loading, as it is slightly more
445 * secure this way.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700446 */
447 @Override
448 public void onLoadResource(WebView view, String url) {
449 if (url != null && url.length() > 0) {
450 // It is only if the page claims to be secure that we may have
Steve Block2466eff2011-10-03 15:33:09 +0100451 // to update the security state:
452 if (mCurrentState.mSecurityState == SecurityState.SECURITY_STATE_SECURE) {
453 // If NOT a 'safe' url, change the state to mixed content!
Grace Kloba22ac16e2009-10-07 18:00:23 -0700454 if (!(URLUtil.isHttpsUrl(url) || URLUtil.isDataUrl(url)
455 || URLUtil.isAboutUrl(url))) {
Steve Block2466eff2011-10-03 15:33:09 +0100456 mCurrentState.mSecurityState = SecurityState.SECURITY_STATE_MIXED;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700457 }
458 }
459 }
460 }
461
462 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700463 * Show a dialog informing the user of the network error reported by
464 * WebCore if it is in the foreground.
465 */
466 @Override
467 public void onReceivedError(WebView view, int errorCode,
468 String description, String failingUrl) {
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800469 // Used for the syncCurrentState to use
470 // the failing url instead of using webview url
471 mReceivedError = true;
472
Grace Kloba22ac16e2009-10-07 18:00:23 -0700473 if (errorCode != WebViewClient.ERROR_HOST_LOOKUP &&
474 errorCode != WebViewClient.ERROR_CONNECT &&
475 errorCode != WebViewClient.ERROR_BAD_URL &&
476 errorCode != WebViewClient.ERROR_UNSUPPORTED_SCHEME &&
477 errorCode != WebViewClient.ERROR_FILE) {
478 queueError(errorCode, description);
Jeff Hamilton47654f42010-09-07 09:57:51 -0500479
Selim Gurun3da06b82011-10-10 13:58:12 -0700480 // Don't log URLs when in private browsing mode
481 if (!isPrivateBrowsingEnabled()) {
482 Log.e(LOGTAG, "onReceivedError " + errorCode + " " + failingUrl
Jeff Hamilton47654f42010-09-07 09:57:51 -0500483 + " " + description);
Selim Gurun3da06b82011-10-10 13:58:12 -0700484 }
Jeff Hamilton47654f42010-09-07 09:57:51 -0500485 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700486 }
487
488 /**
489 * Check with the user if it is ok to resend POST data as the page they
490 * are trying to navigate to is the result of a POST.
491 */
492 @Override
493 public void onFormResubmission(WebView view, final Message dontResend,
494 final Message resend) {
495 if (!mInForeground) {
496 dontResend.sendToTarget();
497 return;
498 }
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500499 if (mDontResend != null) {
500 Log.w(LOGTAG, "onFormResubmission should not be called again "
501 + "while dialog is still up");
502 dontResend.sendToTarget();
503 return;
504 }
505 mDontResend = dontResend;
506 mResend = resend;
Michael Kolb14612442011-06-24 13:06:29 -0700507 new AlertDialog.Builder(mContext).setTitle(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700508 R.string.browserFrameFormResubmitLabel).setMessage(
509 R.string.browserFrameFormResubmitMessage)
510 .setPositiveButton(R.string.ok,
511 new DialogInterface.OnClickListener() {
512 public void onClick(DialogInterface dialog,
513 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500514 if (mResend != null) {
515 mResend.sendToTarget();
516 mResend = null;
517 mDontResend = null;
518 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700519 }
520 }).setNegativeButton(R.string.cancel,
521 new DialogInterface.OnClickListener() {
522 public void onClick(DialogInterface dialog,
523 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500524 if (mDontResend != null) {
525 mDontResend.sendToTarget();
526 mResend = null;
527 mDontResend = null;
528 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700529 }
530 }).setOnCancelListener(new OnCancelListener() {
531 public void onCancel(DialogInterface dialog) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500532 if (mDontResend != null) {
533 mDontResend.sendToTarget();
534 mResend = null;
535 mDontResend = null;
536 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700537 }
538 }).show();
539 }
540
541 /**
542 * Insert the url into the visited history database.
543 * @param url The url to be inserted.
544 * @param isReload True if this url is being reloaded.
545 * FIXME: Not sure what to do when reloading the page.
546 */
547 @Override
548 public void doUpdateVisitedHistory(WebView view, String url,
549 boolean isReload) {
John Reck324d4402011-01-11 16:56:42 -0800550 mWebViewController.doUpdateVisitedHistory(Tab.this, isReload);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700551 }
552
553 /**
554 * Displays SSL error(s) dialog to the user.
555 */
556 @Override
557 public void onReceivedSslError(final WebView view,
558 final SslErrorHandler handler, final SslError error) {
559 if (!mInForeground) {
560 handler.cancel();
Steve Block2466eff2011-10-03 15:33:09 +0100561 setSecurityState(SecurityState.SECURITY_STATE_NOT_SECURE);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700562 return;
563 }
John Reck35e9dd62011-04-25 09:01:54 -0700564 if (mSettings.showSecurityWarnings()) {
John Reckcb28b2c2011-08-26 17:39:44 -0700565 new AlertDialog.Builder(mContext)
566 .setTitle(R.string.security_warning)
567 .setMessage(R.string.ssl_warnings_header)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200568 .setIconAttribute(android.R.attr.alertDialogIcon)
John Reckcb28b2c2011-08-26 17:39:44 -0700569 .setPositiveButton(R.string.ssl_continue,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700570 new DialogInterface.OnClickListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700571 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700572 public void onClick(DialogInterface dialog,
573 int whichButton) {
574 handler.proceed();
Steve Block4895b012011-10-03 16:26:46 +0100575 handleProceededAfterSslError(error);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700576 }
John Reckcb28b2c2011-08-26 17:39:44 -0700577 })
578 .setNeutralButton(R.string.view_certificate,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700579 new DialogInterface.OnClickListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700580 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700581 public void onClick(DialogInterface dialog,
582 int whichButton) {
John Reckcb28b2c2011-08-26 17:39:44 -0700583 mWebViewController.showSslCertificateOnError(
584 view, handler, error);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700585 }
John Reckcb28b2c2011-08-26 17:39:44 -0700586 })
587 .setNegativeButton(R.string.ssl_go_back,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700588 new DialogInterface.OnClickListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700589 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700590 public void onClick(DialogInterface dialog,
591 int whichButton) {
John Reck30c714c2010-12-16 17:30:34 -0800592 dialog.cancel();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700593 }
John Reckcb28b2c2011-08-26 17:39:44 -0700594 })
595 .setOnCancelListener(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700596 new DialogInterface.OnCancelListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700597 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700598 public void onCancel(DialogInterface dialog) {
599 handler.cancel();
Steve Block2466eff2011-10-03 15:33:09 +0100600 setSecurityState(SecurityState.SECURITY_STATE_NOT_SECURE);
John Reck30c714c2010-12-16 17:30:34 -0800601 mWebViewController.onUserCanceledSsl(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700602 }
John Reckcb28b2c2011-08-26 17:39:44 -0700603 })
604 .show();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700605 } else {
606 handler.proceed();
607 }
608 }
609
610 /**
Steve Block4895b012011-10-03 16:26:46 +0100611 * Called when an SSL error occurred while loading a resource, but the
612 * WebView but chose to proceed anyway based on a decision retained
613 * from a previous response to onReceivedSslError(). We update our
614 * security state to reflect this.
615 */
616 @Override
617 public void onProceededAfterSslError(WebView view, SslError error) {
618 handleProceededAfterSslError(error);
619 }
620
621 /**
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700622 * Displays client certificate request to the user.
623 */
624 @Override
625 public void onReceivedClientCertRequest(final WebView view,
626 final ClientCertRequestHandler handler, final String host_and_port) {
627 if (!mInForeground) {
628 handler.ignore();
629 return;
630 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700631 int colon = host_and_port.lastIndexOf(':');
632 String host;
633 int port;
634 if (colon == -1) {
635 host = host_and_port;
636 port = -1;
637 } else {
638 String portString = host_and_port.substring(colon + 1);
639 try {
640 port = Integer.parseInt(portString);
641 host = host_and_port.substring(0, colon);
642 } catch (NumberFormatException e) {
643 host = host_and_port;
644 port = -1;
645 }
646 }
Michael Kolb14612442011-06-24 13:06:29 -0700647 KeyChain.choosePrivateKeyAlias(
648 mWebViewController.getActivity(), new KeyChainAliasCallback() {
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700649 @Override public void alias(String alias) {
650 if (alias == null) {
651 handler.cancel();
652 return;
653 }
Michael Kolb14612442011-06-24 13:06:29 -0700654 new KeyChainLookup(mContext, handler, alias).execute();
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700655 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700656 }, null, null, host, port, null);
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700657 }
658
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800659 @Override
660 public void onRendererCrash(WebView view, boolean crashedWhileOomProtected) {
661 Log.e(LOGTAG, "Tab Crashed");
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700662 if (mWebViewController.getTabControl().getCurrentTab() == Tab.this) {
663 hasCrashed = true;
664 showCrashView();
665 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800666 }
667
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700668 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700669 * Handles an HTTP authentication request.
670 *
671 * @param handler The authentication handler
672 * @param host The host
673 * @param realm The realm
674 */
675 @Override
676 public void onReceivedHttpAuthRequest(WebView view,
677 final HttpAuthHandler handler, final String host,
678 final String realm) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700679 mWebViewController.onReceivedHttpAuthRequest(Tab.this, view, handler, host, realm);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700680 }
681
682 @Override
John Reck438bf462011-01-12 18:11:46 -0800683 public WebResourceResponse shouldInterceptRequest(WebView view,
684 String url) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800685 //intercept if opening a new incognito tab - show the incognito welcome page
686 if (url.startsWith("browser:incognito")) {
687 Resources resourceHandle = mContext.getResources();
688 InputStream inStream = resourceHandle.openRawResource(
Bijan Amirzada41242f22014-03-21 12:12:18 -0700689 com.android.browser.R.raw.incognito_mode_start_page);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800690 return new WebResourceResponse("text/html", "utf8", inStream);
691 }
kaiyiz6e5b3e02013-08-19 20:02:01 +0800692 WebResourceResponse res;
693 if (MyNavigationUtil.MY_NAVIGATION.equals(url)) {
694 res = MyNavigationProvider.shouldInterceptRequest(mContext, url);
695 } else {
696 res = HomeProvider.shouldInterceptRequest(mContext, url);
697 }
John Reck438bf462011-01-12 18:11:46 -0800698 return res;
699 }
700
701 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700702 public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
703 if (!mInForeground) {
704 return false;
705 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700706 return mWebViewController.shouldOverrideKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700707 }
708
709 @Override
710 public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700711 if (!mInForeground) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700712 return;
713 }
John Reck997b1b72012-04-19 18:08:25 -0700714 if (!mWebViewController.onUnhandledKeyEvent(event)) {
715 super.onUnhandledKeyEvent(view, event);
716 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700717 }
718 };
719
John Reck1cf4b792011-07-26 10:22:22 -0700720 private void syncCurrentState(WebView view, String url) {
721 // Sync state (in case of stop/timeout)
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800722
723 if (mReceivedError) {
724 mCurrentState.mUrl = url;
725 mCurrentState.mOriginalUrl = url;
726 } else {
727 mCurrentState.mUrl = view.getUrl();
728 mCurrentState.mOriginalUrl = view.getOriginalUrl();
729 mCurrentState.mFavicon = view.getFavicon();
730 }
731
John Reck1cf4b792011-07-26 10:22:22 -0700732 if (mCurrentState.mUrl == null) {
John Reck8bcafc12011-08-29 16:43:02 -0700733 mCurrentState.mUrl = "";
John Reck1cf4b792011-07-26 10:22:22 -0700734 }
John Reck1cf4b792011-07-26 10:22:22 -0700735 mCurrentState.mTitle = view.getTitle();
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800736
737
John Reck1cf4b792011-07-26 10:22:22 -0700738 if (!URLUtil.isHttpsUrl(mCurrentState.mUrl)) {
739 // In case we stop when loading an HTTPS page from an HTTP page
740 // but before a provisional load occurred
Steve Block2466eff2011-10-03 15:33:09 +0100741 mCurrentState.mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
Steve Block08a6f0c2011-10-06 12:12:53 +0100742 mCurrentState.mSslCertificateError = null;
John Reck1cf4b792011-07-26 10:22:22 -0700743 }
John Reck502a3532011-08-16 14:21:46 -0700744 mCurrentState.mIncognito = view.isPrivateBrowsingEnabled();
John Reck1cf4b792011-07-26 10:22:22 -0700745 }
746
Tarun Nainani8eb00912014-07-17 12:28:32 -0700747
748 public boolean isTabFullScreen() {
749 return mFullScreen;
750 }
751
Vivek Sekharf96064b2014-07-28 16:32:34 -0700752 protected void setTabFullscreen(boolean fullScreen) {
Tarun Nainani8eb00912014-07-17 12:28:32 -0700753 Controller controller = (Controller)mWebViewController;
Sudheer Koganti24766882014-10-02 10:58:09 -0700754 controller.getUi().showFullscreen(fullScreen);
Tarun Nainani8eb00912014-07-17 12:28:32 -0700755 mFullScreen = fullScreen;
Vivek Sekharf96064b2014-07-28 16:32:34 -0700756 }
757
Sudheer Koganti24766882014-10-02 10:58:09 -0700758 public boolean exitFullscreen() {
759 if (mFullScreen) {
760 Controller controller = (Controller)mWebViewController;
761 controller.getUi().showFullscreen(false);
762 if (getWebView() != null)
763 getWebView().exitFullscreen();
764 mFullScreen = false;
765 return true;
766 }
767 return false;
768 }
769
770
771
772
Grace Kloba22ac16e2009-10-07 18:00:23 -0700773 // -------------------------------------------------------------------------
774 // WebChromeClient implementation for the main WebView
775 // -------------------------------------------------------------------------
776
777 private final WebChromeClient mWebChromeClient = new WebChromeClient() {
778 // Helper method to create a new tab or sub window.
779 private void createWindow(final boolean dialog, final Message msg) {
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700780 this.createWindow(dialog, msg, null, false);
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700781 }
782
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700783 private void createWindow(final boolean dialog, final Message msg, final String url,
784 final boolean opener_suppressed) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700785 WebView.WebViewTransport transport =
786 (WebView.WebViewTransport) msg.obj;
787 if (dialog) {
788 createSubWindow();
Michael Kolb8233fac2010-10-26 16:08:53 -0700789 mWebViewController.attachSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700790 transport.setWebView(mSubView);
791 } else {
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700792 final Tab newTab = mWebViewController.openTab(url,
John Reck5949c662011-05-27 09:52:29 -0700793 Tab.this, true, true);
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700794 // This is special case for rendering links on a webpage in
795 // a new tab. If opener is suppressed, the WebContents created
796 // by the content layer are not fully initialized. This check
797 // will prevent content layer from overriding WebContents
798 // created by new tab with the uninitialized instance.
799 if (!opener_suppressed) {
800 transport.setWebView(newTab.getWebView());
801 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700802 }
803 msg.sendToTarget();
804 }
805
806 @Override
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700807 public void toggleFullscreenModeForTab(boolean enterFullscreen) {
808 if (mWebViewController instanceof Controller) {
Vivek Sekharf96064b2014-07-28 16:32:34 -0700809 setTabFullscreen(enterFullscreen);
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700810 }
811 }
812
813 @Override
Tarun Nainani8eb00912014-07-17 12:28:32 -0700814 public void onOffsetsForFullscreenChanged(float topControlsOffsetYPix,
815 float contentOffsetYPix,
816 float overdrawBottomHeightPix) {
817 if (mWebViewController instanceof Controller) {
Vivek Sekhar8ee3abb2014-07-14 12:32:05 -0700818 Controller controller = (Controller)mWebViewController;
Tarun Nainani8eb00912014-07-17 12:28:32 -0700819 controller.getUi().translateTitleBar(topControlsOffsetYPix);
Vivek Sekhar8ee3abb2014-07-14 12:32:05 -0700820 }
821 }
822
823 @Override
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700824 public boolean isTabFullScreen() {
Vivek Sekharf96064b2014-07-28 16:32:34 -0700825 return mFullScreen;
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700826 }
827
828 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700829 public boolean onCreateWindow(WebView view, final boolean dialog,
830 final boolean userGesture, final Message resultMsg) {
831 // only allow new window or sub window for the foreground case
832 if (!mInForeground) {
833 return false;
834 }
835 // Short-circuit if we can't create any more tabs or sub windows.
836 if (dialog && mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -0700837 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700838 .setTitle(R.string.too_many_subwindows_dialog_title)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200839 .setIconAttribute(android.R.attr.alertDialogIcon)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700840 .setMessage(R.string.too_many_subwindows_dialog_message)
841 .setPositiveButton(R.string.ok, null)
842 .show();
843 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700844 } else if (!mWebViewController.getTabControl().canCreateNewTab()) {
Michael Kolb14612442011-06-24 13:06:29 -0700845 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700846 .setTitle(R.string.too_many_windows_dialog_title)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200847 .setIconAttribute(android.R.attr.alertDialogIcon)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700848 .setMessage(R.string.too_many_windows_dialog_message)
849 .setPositiveButton(R.string.ok, null)
850 .show();
851 return false;
852 }
853
854 // Short-circuit if this was a user gesture.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800855 if (userGesture || !mSettings.blockPopupWindows()) {
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700856 CreateWindowParams windowParams = view.getCreateWindowParams();
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700857 if (windowParams.mOpenerSuppressed) {
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700858 createWindow(dialog, resultMsg, windowParams.mURL, true);
859 // This is special case for rendering links on a webpage in
860 // a new tab. If opener is suppressed, the WebContents created
861 // by the content layer are not fully initialized. Returning false
862 // will prevent content layer from overriding WebContents
863 // created by new tab with the uninitialized instance.
864 return false;
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700865 }
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700866
867 createWindow(dialog, resultMsg);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700868 return true;
869 }
870
871 // Allow the popup and create the appropriate window.
872 final AlertDialog.OnClickListener allowListener =
873 new AlertDialog.OnClickListener() {
874 public void onClick(DialogInterface d,
875 int which) {
876 createWindow(dialog, resultMsg);
877 }
878 };
879
880 // Block the popup by returning a null WebView.
881 final AlertDialog.OnClickListener blockListener =
882 new AlertDialog.OnClickListener() {
883 public void onClick(DialogInterface d, int which) {
884 resultMsg.sendToTarget();
885 }
886 };
887
888 // Build a confirmation dialog to display to the user.
889 final AlertDialog d =
Michael Kolb14612442011-06-24 13:06:29 -0700890 new AlertDialog.Builder(mContext)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200891 .setIconAttribute(android.R.attr.alertDialogIcon)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700892 .setMessage(R.string.popup_window_attempt)
893 .setPositiveButton(R.string.allow, allowListener)
894 .setNegativeButton(R.string.block, blockListener)
895 .setCancelable(false)
896 .create();
897
898 // Show the confirmation dialog.
899 d.show();
900 return true;
901 }
902
903 @Override
Patrick Scotteb5061b2009-11-18 15:00:30 -0500904 public void onRequestFocus(WebView view) {
905 if (!mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700906 mWebViewController.switchToTab(Tab.this);
Patrick Scotteb5061b2009-11-18 15:00:30 -0500907 }
908 }
909
910 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700911 public void onCloseWindow(WebView window) {
Michael Kolbc831b632011-05-11 09:30:34 -0700912 if (mParent != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700913 // JavaScript can only close popup window.
914 if (mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700915 mWebViewController.switchToTab(mParent);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700916 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700917 mWebViewController.closeTab(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700918 }
919 }
920
921 @Override
922 public void onProgressChanged(WebView view, int newProgress) {
John Reck30c714c2010-12-16 17:30:34 -0800923 mPageLoadProgress = newProgress;
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800924 if (newProgress == 100) {
925 mInPageLoad = false;
926 }
John Reck30c714c2010-12-16 17:30:34 -0800927 mWebViewController.onProgressChanged(Tab.this);
Michael Kolb72864272012-05-03 15:42:15 -0700928 if (mUpdateThumbnail && newProgress == 100) {
929 mUpdateThumbnail = false;
930 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700931 }
932
933 @Override
Leon Scroggins21d9b902010-03-11 09:33:11 -0500934 public void onReceivedTitle(WebView view, final String title) {
John Reck30c714c2010-12-16 17:30:34 -0800935 mCurrentState.mTitle = title;
Michael Kolb8233fac2010-10-26 16:08:53 -0700936 mWebViewController.onReceivedTitle(Tab.this, title);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700937 }
938
939 @Override
940 public void onReceivedIcon(WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -0800941 mCurrentState.mFavicon = icon;
Michael Kolb8233fac2010-10-26 16:08:53 -0700942 mWebViewController.onFavicon(Tab.this, view, icon);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700943 }
944
945 @Override
946 public void onReceivedTouchIconUrl(WebView view, String url,
947 boolean precomposed) {
Michael Kolb14612442011-06-24 13:06:29 -0700948 final ContentResolver cr = mContext.getContentResolver();
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400949 // Let precomposed icons take precedence over non-composed
950 // icons.
951 if (precomposed && mTouchIconLoader != null) {
952 mTouchIconLoader.cancel(false);
953 mTouchIconLoader = null;
954 }
955 // Have only one async task at a time.
956 if (mTouchIconLoader == null) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700957 mTouchIconLoader = new DownloadTouchIcon(Tab.this,
Michael Kolb14612442011-06-24 13:06:29 -0700958 mContext, cr, view);
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400959 mTouchIconLoader.execute(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700960 }
961 }
962
963 @Override
964 public void onShowCustomView(View view,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800965 CustomViewCallback callback) {
Michael Kolb14612442011-06-24 13:06:29 -0700966 Activity activity = mWebViewController.getActivity();
967 if (activity != null) {
968 onShowCustomView(view, activity.getRequestedOrientation(), callback);
969 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400970 }
971
972 @Override
973 public void onShowCustomView(View view, int requestedOrientation,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800974 CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700975 if (mInForeground) mWebViewController.showCustomView(Tab.this, view,
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400976 requestedOrientation, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700977 }
978
979 @Override
980 public void onHideCustomView() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700981 if (mInForeground) mWebViewController.hideCustomView();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700982 }
983
984 /**
985 * The origin has exceeded its database quota.
986 * @param url the URL that exceeded the quota
987 * @param databaseIdentifier the identifier of the database on which the
988 * transaction that caused the quota overflow was run
989 * @param currentQuota the current quota for the origin.
990 * @param estimatedSize the estimated size of the database.
991 * @param totalUsedQuota is the sum of all origins' quota.
992 * @param quotaUpdater The callback to run when a decision to allow or
993 * deny quota has been made. Don't forget to call this!
994 */
995 @Override
996 public void onExceededDatabaseQuota(String url,
997 String databaseIdentifier, long currentQuota, long estimatedSize,
998 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -0700999 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001000 .onExceededDatabaseQuota(url, databaseIdentifier,
1001 currentQuota, estimatedSize, totalUsedQuota,
1002 quotaUpdater);
1003 }
1004
1005 /**
1006 * The Application Cache has exceeded its max size.
1007 * @param spaceNeeded is the amount of disk space that would be needed
1008 * in order for the last appcache operation to succeed.
1009 * @param totalUsedQuota is the sum of all origins' quota.
1010 * @param quotaUpdater A callback to inform the WebCore thread that a
1011 * new app cache size is available. This callback must always
1012 * be executed at some point to ensure that the sleeping
1013 * WebCore thread is woken up.
1014 */
1015 @Override
1016 public void onReachedMaxAppCacheSize(long spaceNeeded,
1017 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -07001018 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001019 .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota,
1020 quotaUpdater);
1021 }
1022
1023 /**
1024 * Instructs the browser to show a prompt to ask the user to set the
1025 * Geolocation permission state for the specified origin.
1026 * @param origin The origin for which Geolocation permissions are
1027 * requested.
1028 * @param callback The callback to call once the user has set the
1029 * Geolocation permission state.
1030 */
1031 @Override
1032 public void onGeolocationPermissionsShowPrompt(String origin,
1033 GeolocationPermissions.Callback callback) {
1034 if (mInForeground) {
Grace Kloba50c241e2010-04-20 11:07:50 -07001035 getGeolocationPermissionsPrompt().show(origin, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001036 }
1037 }
1038
1039 /**
1040 * Instructs the browser to hide the Geolocation permissions prompt.
1041 */
1042 @Override
1043 public void onGeolocationPermissionsHidePrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001044 if (mInForeground && mGeolocationPermissionsPrompt != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001045 mGeolocationPermissionsPrompt.hide();
1046 }
1047 }
1048
Ben Murdoch65acc352009-11-19 18:16:04 +00001049 /* Adds a JavaScript error message to the system log and if the JS
1050 * console is enabled in the about:debug options, to that console
1051 * also.
Ben Murdochc42addf2010-01-28 15:19:59 +00001052 * @param consoleMessage the message object.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001053 */
1054 @Override
Ben Murdochc42addf2010-01-28 15:19:59 +00001055 public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001056 if (mInForeground) {
1057 // call getErrorConsole(true) so it will create one if needed
1058 ErrorConsoleView errorConsole = getErrorConsole(true);
Ben Murdochc42addf2010-01-28 15:19:59 +00001059 errorConsole.addErrorMessage(consoleMessage);
Michael Kolb8233fac2010-10-26 16:08:53 -07001060 if (mWebViewController.shouldShowErrorConsole()
1061 && errorConsole.getShowState() !=
1062 ErrorConsoleView.SHOW_MAXIMIZED) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001063 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
1064 }
1065 }
Ben Murdochc42addf2010-01-28 15:19:59 +00001066
Jeff Hamilton47654f42010-09-07 09:57:51 -05001067 // Don't log console messages in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001068 if (isPrivateBrowsingEnabled()) return true;
Jeff Hamilton47654f42010-09-07 09:57:51 -05001069
Ben Murdochc42addf2010-01-28 15:19:59 +00001070 String message = "Console: " + consoleMessage.message() + " "
1071 + consoleMessage.sourceId() + ":"
1072 + consoleMessage.lineNumber();
1073
1074 switch (consoleMessage.messageLevel()) {
1075 case TIP:
1076 Log.v(CONSOLE_LOGTAG, message);
1077 break;
1078 case LOG:
1079 Log.i(CONSOLE_LOGTAG, message);
1080 break;
1081 case WARNING:
1082 Log.w(CONSOLE_LOGTAG, message);
1083 break;
1084 case ERROR:
1085 Log.e(CONSOLE_LOGTAG, message);
1086 break;
1087 case DEBUG:
1088 Log.d(CONSOLE_LOGTAG, message);
1089 break;
1090 }
1091
1092 return true;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001093 }
1094
1095 /**
1096 * Ask the browser for an icon to represent a <video> element.
1097 * This icon will be used if the Web page did not specify a poster attribute.
1098 * @return Bitmap The icon or null if no such icon is available.
1099 */
1100 @Override
1101 public Bitmap getDefaultVideoPoster() {
1102 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001103 return mWebViewController.getDefaultVideoPoster();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001104 }
1105 return null;
1106 }
1107
1108 /**
1109 * Ask the host application for a custom progress view to show while
1110 * a <video> is loading.
1111 * @return View The progress view.
1112 */
1113 @Override
1114 public View getVideoLoadingProgressView() {
1115 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001116 return mWebViewController.getVideoLoadingProgressView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001117 }
1118 return null;
1119 }
1120
1121 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00001122 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001123 if (mInForeground) {
Ben Murdoch8cad4132012-01-11 10:56:43 +00001124 mWebViewController.openFileChooser(uploadMsg, acceptType, capture);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001125 } else {
1126 uploadMsg.onReceiveValue(null);
1127 }
1128 }
1129
Vivek Sekharb54614f2014-05-01 19:03:37 -07001130 @Override
1131 public void showFileChooser(ValueCallback<String[]> uploadFilePaths, String acceptTypes,
1132 boolean capture) {
1133 if (mInForeground) {
1134 mWebViewController.showFileChooser(uploadFilePaths, acceptTypes, capture);
1135 } else {
1136 uploadFilePaths.onReceiveValue(null);
1137 }
1138 }
1139
Grace Kloba22ac16e2009-10-07 18:00:23 -07001140 /**
1141 * Deliver a list of already-visited URLs
1142 */
1143 @Override
1144 public void getVisitedHistory(final ValueCallback<String[]> callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001145 mWebViewController.getVisitedHistory(callback);
1146 }
Ben Murdoch8029a772010-11-16 11:58:21 +00001147
1148 @Override
1149 public void setupAutoFill(Message message) {
1150 // Prompt the user to set up their profile.
1151 final Message msg = message;
Michael Kolb14612442011-06-24 13:06:29 -07001152 AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
1153 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
Ben Murdoch1d676b62011-01-17 12:54:24 +00001154 Context.LAYOUT_INFLATER_SERVICE);
1155 final View layout = inflater.inflate(R.layout.setup_autofill_dialog, null);
1156
1157 builder.setView(layout)
1158 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1159 @Override
1160 public void onClick(DialogInterface dialog, int id) {
1161 CheckBox disableAutoFill = (CheckBox) layout.findViewById(
1162 R.id.setup_autofill_dialog_disable_autofill);
1163
1164 if (disableAutoFill.isChecked()) {
1165 // Disable autofill and show a toast with how to turn it on again.
John Reck35e9dd62011-04-25 09:01:54 -07001166 mSettings.setAutofillEnabled(false);
Michael Kolb14612442011-06-24 13:06:29 -07001167 Toast.makeText(mContext,
Ben Murdoch1d676b62011-01-17 12:54:24 +00001168 R.string.autofill_setup_dialog_negative_toast,
1169 Toast.LENGTH_LONG).show();
1170 } else {
1171 // Take user to the AutoFill profile editor. When they return,
1172 // we will send the message that we pass here which will trigger
1173 // the form to get filled out with their new profile.
1174 mWebViewController.setupAutoFill(msg);
1175 }
1176 }
1177 })
1178 .setNegativeButton(R.string.cancel, null)
1179 .show();
Ben Murdoch8029a772010-11-16 11:58:21 +00001180 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001181 };
1182
1183 // -------------------------------------------------------------------------
1184 // WebViewClient implementation for the sub window
1185 // -------------------------------------------------------------------------
1186
1187 // Subclass of WebViewClient used in subwindows to notify the main
1188 // WebViewClient of certain WebView activities.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001189 private static class SubWindowClient extends WebViewClient {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001190 // The main WebViewClient.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001191 private final WebViewClient mClient;
Michael Kolb8233fac2010-10-26 16:08:53 -07001192 private final WebViewController mController;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001193
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001194 SubWindowClient(WebViewClient client, WebViewController controller) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001195 mClient = client;
Michael Kolb8233fac2010-10-26 16:08:53 -07001196 mController = controller;
Leon Scroggins III211ba542010-04-19 13:21:13 -04001197 }
1198 @Override
1199 public void onPageStarted(WebView view, String url, Bitmap favicon) {
1200 // Unlike the others, do not call mClient's version, which would
1201 // change the progress bar. However, we do want to remove the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001202 // find or select dialog.
Michael Kolb8233fac2010-10-26 16:08:53 -07001203 mController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001204 }
1205 @Override
1206 public void doUpdateVisitedHistory(WebView view, String url,
1207 boolean isReload) {
1208 mClient.doUpdateVisitedHistory(view, url, isReload);
1209 }
1210 @Override
1211 public boolean shouldOverrideUrlLoading(WebView view, String url) {
1212 return mClient.shouldOverrideUrlLoading(view, url);
1213 }
1214 @Override
1215 public void onReceivedSslError(WebView view, SslErrorHandler handler,
1216 SslError error) {
1217 mClient.onReceivedSslError(view, handler, error);
1218 }
1219 @Override
Brian Carlstrom8862c1d2011-06-02 01:05:55 -07001220 public void onReceivedClientCertRequest(WebView view,
1221 ClientCertRequestHandler handler, String host_and_port) {
1222 mClient.onReceivedClientCertRequest(view, handler, host_and_port);
1223 }
1224 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -07001225 public void onReceivedHttpAuthRequest(WebView view,
1226 HttpAuthHandler handler, String host, String realm) {
1227 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
1228 }
1229 @Override
1230 public void onFormResubmission(WebView view, Message dontResend,
1231 Message resend) {
1232 mClient.onFormResubmission(view, dontResend, resend);
1233 }
1234 @Override
1235 public void onReceivedError(WebView view, int errorCode,
1236 String description, String failingUrl) {
1237 mClient.onReceivedError(view, errorCode, description, failingUrl);
1238 }
1239 @Override
1240 public boolean shouldOverrideKeyEvent(WebView view,
1241 android.view.KeyEvent event) {
1242 return mClient.shouldOverrideKeyEvent(view, event);
1243 }
1244 @Override
1245 public void onUnhandledKeyEvent(WebView view,
1246 android.view.KeyEvent event) {
1247 mClient.onUnhandledKeyEvent(view, event);
1248 }
1249 }
1250
1251 // -------------------------------------------------------------------------
1252 // WebChromeClient implementation for the sub window
1253 // -------------------------------------------------------------------------
1254
1255 private class SubWindowChromeClient extends WebChromeClient {
1256 // The main WebChromeClient.
1257 private final WebChromeClient mClient;
1258
1259 SubWindowChromeClient(WebChromeClient client) {
1260 mClient = client;
1261 }
1262 @Override
1263 public void onProgressChanged(WebView view, int newProgress) {
1264 mClient.onProgressChanged(view, newProgress);
1265 }
1266 @Override
1267 public boolean onCreateWindow(WebView view, boolean dialog,
1268 boolean userGesture, android.os.Message resultMsg) {
1269 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
1270 }
1271 @Override
1272 public void onCloseWindow(WebView window) {
1273 if (window != mSubView) {
1274 Log.e(LOGTAG, "Can't close the window");
1275 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001276 mWebViewController.dismissSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001277 }
1278 }
1279
1280 // -------------------------------------------------------------------------
1281
1282 // Construct a new tab
Michael Kolb7bcafde2011-05-09 13:55:59 -07001283 Tab(WebViewController wvcontroller, WebView w) {
John Reck1cf4b792011-07-26 10:22:22 -07001284 this(wvcontroller, w, null);
1285 }
1286
1287 Tab(WebViewController wvcontroller, Bundle state) {
1288 this(wvcontroller, null, state);
1289 }
1290
1291 Tab(WebViewController wvcontroller, WebView w, Bundle state) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001292 mWebViewController = wvcontroller;
Michael Kolb14612442011-06-24 13:06:29 -07001293 mContext = mWebViewController.getContext();
John Reck35e9dd62011-04-25 09:01:54 -07001294 mSettings = BrowserSettings.getInstance();
Michael Kolb14612442011-06-24 13:06:29 -07001295 mDataController = DataController.getInstance(mContext);
1296 mCurrentState = new PageState(mContext, w != null
John Reck847b5322011-04-14 17:02:18 -07001297 ? w.isPrivateBrowsingEnabled() : false);
Tarun Nainani8084c822014-06-25 13:38:06 -07001298 setTimeStamp();
Michael Kolb8233fac2010-10-26 16:08:53 -07001299 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001300 mInForeground = false;
1301
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001302 mDownloadListener = new BrowserDownloadListener() {
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001303 public void onDownloadStart(String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001304 String contentDisposition, String mimetype, String referer,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001305 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001306 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001307 mimetype, referer, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001308 }
1309 };
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001310 mWebBackForwardListClient = new WebBackForwardListClient() {
1311 @Override
1312 public void onNewHistoryItem(WebHistoryItem item) {
Mathew Inwooda829d552011-09-02 14:16:25 +01001313 if (mClearHistoryUrlPattern != null) {
1314 boolean match =
1315 mClearHistoryUrlPattern.matcher(item.getOriginalUrl()).matches();
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001316 if (LOGD_ENABLED) {
Mathew Inwooda829d552011-09-02 14:16:25 +01001317 Log.d(LOGTAG, "onNewHistoryItem: match=" + match + "\n\t"
1318 + item.getUrl() + "\n\t"
1319 + mClearHistoryUrlPattern);
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001320 }
Mathew Inwooda829d552011-09-02 14:16:25 +01001321 if (match) {
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001322 if (mMainView != null) {
1323 mMainView.clearHistory();
1324 }
1325 }
Mathew Inwooda829d552011-09-02 14:16:25 +01001326 mClearHistoryUrlPattern = null;
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001327 }
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001328 }
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001329 };
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001330
John Reck1cf4b792011-07-26 10:22:22 -07001331 mCaptureWidth = mContext.getResources().getDimensionPixelSize(
1332 R.dimen.tab_thumbnail_width);
1333 mCaptureHeight = mContext.getResources().getDimensionPixelSize(
1334 R.dimen.tab_thumbnail_height);
1335 updateShouldCaptureThumbnails();
1336 restoreState(state);
John Reck52be4782011-08-26 15:37:29 -07001337 if (getId() == -1) {
1338 mId = TabControl.getNextId();
1339 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001340 setWebView(w);
Michael Kolb9ef259a2011-07-12 15:33:08 -07001341 mHandler = new Handler() {
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001342 @Override
Michael Kolb9ef259a2011-07-12 15:33:08 -07001343 public void handleMessage(Message m) {
John Reck1cf4b792011-07-26 10:22:22 -07001344 switch (m.what) {
1345 case MSG_CAPTURE:
1346 capture();
1347 break;
1348 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001349 }
1350 };
John Reck1cf4b792011-07-26 10:22:22 -07001351 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001352
Michael Kolb72864272012-05-03 15:42:15 -07001353 public boolean shouldUpdateThumbnail() {
1354 return mUpdateThumbnail;
1355 }
1356
Mathew Inwoode09305e2011-09-02 12:03:26 +01001357 /**
1358 * This is used to get a new ID when the tab has been preloaded, before it is displayed and
1359 * added to TabControl. Preloaded tabs can be created before restoreInstanceState, leading
1360 * to overlapping IDs between the preloaded and restored tabs.
1361 */
1362 public void refreshIdAfterPreload() {
1363 mId = TabControl.getNextId();
1364 }
1365
John Reck1cf4b792011-07-26 10:22:22 -07001366 public void updateShouldCaptureThumbnails() {
1367 if (mWebViewController.shouldCaptureThumbnails()) {
1368 synchronized (Tab.this) {
1369 if (mCapture == null) {
1370 mCapture = Bitmap.createBitmap(mCaptureWidth, mCaptureHeight,
1371 Bitmap.Config.RGB_565);
Michael Kolbc3af0672011-08-09 10:24:41 -07001372 mCapture.eraseColor(Color.WHITE);
John Reck1cf4b792011-07-26 10:22:22 -07001373 if (mInForeground) {
1374 postCapture();
1375 }
1376 }
1377 }
1378 } else {
1379 synchronized (Tab.this) {
1380 mCapture = null;
1381 deleteThumbnail();
1382 }
1383 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001384 }
1385
Michael Kolb14612442011-06-24 13:06:29 -07001386 public void setController(WebViewController ctl) {
1387 mWebViewController = ctl;
John Reck1cf4b792011-07-26 10:22:22 -07001388 updateShouldCaptureThumbnails();
Michael Kolb14612442011-06-24 13:06:29 -07001389 }
1390
Michael Kolbc831b632011-05-11 09:30:34 -07001391 public long getId() {
1392 return mId;
1393 }
1394
Michael Kolb91911a22012-01-17 11:21:25 -08001395 void setWebView(WebView w) {
1396 setWebView(w, true);
1397 }
1398
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001399 public boolean isNativeActive(){
1400 if (mMainView == null)
1401 return false;
1402 return true;
1403 }
1404
1405 public void setTimeStamp(){
1406 Date d = new Date();
1407 timestamp = (new Timestamp(d.getTime()));
1408 }
1409
1410 public Timestamp getTimestamp() {
1411 return timestamp;
1412 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001413 /**
1414 * Sets the WebView for this tab, correctly removing the old WebView from
1415 * the container view.
1416 */
Michael Kolb91911a22012-01-17 11:21:25 -08001417 void setWebView(WebView w, boolean restore) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001418 if (mMainView == w) {
1419 return;
1420 }
Michael Kolba713ec82010-11-29 17:27:06 -08001421
Grace Kloba22ac16e2009-10-07 18:00:23 -07001422 // If the WebView is changing, the page will be reloaded, so any ongoing
1423 // Geolocation permission requests are void.
Grace Kloba50c241e2010-04-20 11:07:50 -07001424 if (mGeolocationPermissionsPrompt != null) {
1425 mGeolocationPermissionsPrompt.hide();
1426 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001427
Michael Kolba713ec82010-11-29 17:27:06 -08001428 mWebViewController.onSetWebView(this, w);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001429
John Reck1cf4b792011-07-26 10:22:22 -07001430 if (mMainView != null) {
John Reck8ee633f2011-08-09 16:00:35 -07001431 mMainView.setPictureListener(null);
John Reck1cf4b792011-07-26 10:22:22 -07001432 if (w != null) {
1433 syncCurrentState(w, null);
1434 } else {
Panos Thomasa9a5a582014-03-18 19:20:08 -07001435 mCurrentState = new PageState(mContext, mMainView.isPrivateBrowsingEnabled());
John Reck1cf4b792011-07-26 10:22:22 -07001436 }
1437 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001438 // set the new one
1439 mMainView = w;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001440 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001441 if (mMainView != null) {
1442 mMainView.setWebViewClient(mWebViewClient);
1443 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001444 // Attach DownloadManager so that downloads can start in an active
1445 // or a non-active window. This can happen when going to a site that
1446 // does a redirect after a period of time. The user could have
1447 // switched to another tab while waiting for the download to start.
1448 mMainView.setDownloadListener(mDownloadListener);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001449 getWebView().setWebBackForwardListClient(mWebBackForwardListClient);
John Reck8ee633f2011-08-09 16:00:35 -07001450 TabControl tc = mWebViewController.getTabControl();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001451 if (tc != null /*&& tc.getOnThumbnailUpdatedListener() != null*/) {
John Reck8ee633f2011-08-09 16:00:35 -07001452 mMainView.setPictureListener(this);
1453 }
Michael Kolb91911a22012-01-17 11:21:25 -08001454 if (restore && (mSavedState != null)) {
John Reck8b9bb8b2012-03-08 13:19:40 -08001455 restoreUserAgent();
John Reck6c2e2f32011-08-22 13:41:23 -07001456 WebBackForwardList restoredState
1457 = mMainView.restoreState(mSavedState);
1458 if (restoredState == null || restoredState.getSize() == 0) {
1459 Log.w(LOGTAG, "Failed to restore WebView state!");
1460 loadUrl(mCurrentState.mOriginalUrl, null);
1461 }
John Reck1cf4b792011-07-26 10:22:22 -07001462 mSavedState = null;
1463 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001464 }
1465 }
1466
1467 /**
1468 * Destroy the tab's main WebView and subWindow if any
1469 */
1470 void destroy() {
Tarun Nainani2c1dd7c2014-07-05 16:40:12 -07001471
Matthew Huib7f2e9c2014-04-16 11:12:37 -04001472 if (mPostponeDestroy) {
1473 mShouldDestroy = true;
1474 return;
1475 }
1476 mShouldDestroy = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001477 if (mMainView != null) {
1478 dismissSubWindow();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001479 // save the WebView to call destroy() after detach it from the tab
1480 WebView webView = mMainView;
1481 setWebView(null);
1482 webView.destroy();
1483 }
1484 }
1485
Matthew Huib7f2e9c2014-04-16 11:12:37 -04001486 private boolean mPostponeDestroy = false;
1487 private boolean mShouldDestroy = false;
1488
1489 public void postponeDestroy() {
1490 mPostponeDestroy = true;
1491 }
1492
1493 public void performPostponedDestroy() {
1494 mPostponeDestroy = false;
1495 if (mShouldDestroy) {
1496 destroy();
1497 }
1498 }
1499
Grace Kloba22ac16e2009-10-07 18:00:23 -07001500 /**
1501 * Remove the tab from the parent
1502 */
1503 void removeFromTree() {
1504 // detach the children
Michael Kolbc831b632011-05-11 09:30:34 -07001505 if (mChildren != null) {
1506 for(Tab t : mChildren) {
1507 t.setParent(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001508 }
1509 }
1510 // remove itself from the parent list
Michael Kolbc831b632011-05-11 09:30:34 -07001511 if (mParent != null) {
1512 mParent.mChildren.remove(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001513 }
Tarun Nainani8eb00912014-07-17 12:28:32 -07001514
1515 mCapture = null;
John Reck1cf4b792011-07-26 10:22:22 -07001516 deleteThumbnail();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001517 }
1518
1519 /**
1520 * Create a new subwindow unless a subwindow already exists.
1521 * @return True if a new subwindow was created. False if one already exists.
1522 */
1523 boolean createSubWindow() {
1524 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001525 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001526 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001527 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001528 mSubView.setWebChromeClient(new SubWindowChromeClient(
1529 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001530 // Set a different DownloadListener for the mSubView, since it will
1531 // just need to dismiss the mSubView, rather than close the Tab
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001532 mSubView.setDownloadListener(new BrowserDownloadListener() {
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001533 public void onDownloadStart(String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001534 String contentDisposition, String mimetype, String referer,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001535 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001536 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001537 contentDisposition, mimetype, referer, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001538 if (mSubView.copyBackForwardList().getSize() == 0) {
1539 // This subwindow was opened for the sole purpose of
1540 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001541 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001542 }
1543 }
1544 });
Michael Kolb14612442011-06-24 13:06:29 -07001545 mSubView.setOnCreateContextMenuListener(mWebViewController.getActivity());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001546 return true;
1547 }
1548 return false;
1549 }
1550
1551 /**
1552 * Dismiss the subWindow for the tab.
1553 */
1554 void dismissSubWindow() {
1555 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001556 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001557 mSubView.destroy();
1558 mSubView = null;
1559 mSubViewContainer = null;
1560 }
1561 }
1562
Grace Kloba22ac16e2009-10-07 18:00:23 -07001563
1564 /**
1565 * Set the parent tab of this tab.
1566 */
Michael Kolbc831b632011-05-11 09:30:34 -07001567 void setParent(Tab parent) {
John Reck52be4782011-08-26 15:37:29 -07001568 if (parent == this) {
1569 throw new IllegalStateException("Cannot set parent to self!");
1570 }
Michael Kolbc831b632011-05-11 09:30:34 -07001571 mParent = parent;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001572 // This tab may have been freed due to low memory. If that is the case,
Michael Kolbc831b632011-05-11 09:30:34 -07001573 // the parent tab id is already saved. If we are changing that id
Grace Kloba22ac16e2009-10-07 18:00:23 -07001574 // (most likely due to removing the parent tab) we must update the
Michael Kolbc831b632011-05-11 09:30:34 -07001575 // parent tab id in the saved Bundle.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001576 if (mSavedState != null) {
1577 if (parent == null) {
1578 mSavedState.remove(PARENTTAB);
1579 } else {
Michael Kolbc831b632011-05-11 09:30:34 -07001580 mSavedState.putLong(PARENTTAB, parent.getId());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001581 }
1582 }
John Reckb0a86db2011-05-24 14:05:58 -07001583
1584 // Sync the WebView useragent with the parent
1585 if (parent != null && mSettings.hasDesktopUseragent(parent.getWebView())
1586 != mSettings.hasDesktopUseragent(getWebView())) {
1587 mSettings.toggleDesktopUseragent(getWebView());
1588 }
John Reck52be4782011-08-26 15:37:29 -07001589
1590 if (parent != null && parent.getId() == getId()) {
1591 throw new IllegalStateException("Parent has same ID as child!");
1592 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001593 }
1594
1595 /**
Michael Kolbc831b632011-05-11 09:30:34 -07001596 * If this Tab was created through another Tab, then this method returns
1597 * that Tab.
1598 * @return the Tab parent or null
1599 */
1600 public Tab getParent() {
1601 return mParent;
1602 }
1603
1604 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001605 * When a Tab is created through the content of another Tab, then we
1606 * associate the Tabs.
1607 * @param child the Tab that was created from this Tab
1608 */
1609 void addChildTab(Tab child) {
Michael Kolbc831b632011-05-11 09:30:34 -07001610 if (mChildren == null) {
1611 mChildren = new Vector<Tab>();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001612 }
Michael Kolbc831b632011-05-11 09:30:34 -07001613 mChildren.add(child);
1614 child.setParent(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001615 }
1616
Michael Kolbc831b632011-05-11 09:30:34 -07001617 Vector<Tab> getChildren() {
1618 return mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001619 }
1620
1621 void resume() {
1622 if (mMainView != null) {
Pankaj Garg60221af2014-09-23 17:57:50 -07001623 if (mMainView.hasCrashed()) {
1624 // Reload if render process has crashed. This is done here so that
1625 // setFocus call sends wasShown message to correct render process.
1626 mMainView.reload();
1627 }
John Reck56c1fcf2011-08-17 10:15:16 -07001628 setupHwAcceleration(mMainView);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001629 mMainView.onResume();
1630 if (mSubView != null) {
1631 mSubView.onResume();
1632 }
1633 }
1634 }
1635
John Reck56c1fcf2011-08-17 10:15:16 -07001636 private void setupHwAcceleration(View web) {
1637 if (web == null) return;
1638 BrowserSettings settings = BrowserSettings.getInstance();
1639 if (settings.isHardwareAccelerated()) {
1640 web.setLayerType(View.LAYER_TYPE_NONE, null);
1641 } else {
1642 web.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
1643 }
1644 }
1645
Grace Kloba22ac16e2009-10-07 18:00:23 -07001646 void pause() {
1647 if (mMainView != null) {
1648 mMainView.onPause();
1649 if (mSubView != null) {
1650 mSubView.onPause();
1651 }
1652 }
1653 }
1654
1655 void putInForeground() {
John Reck8ee633f2011-08-09 16:00:35 -07001656 if (mInForeground) {
1657 return;
1658 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001659 mInForeground = true;
1660 resume();
Michael Kolb14612442011-06-24 13:06:29 -07001661 Activity activity = mWebViewController.getActivity();
1662 mMainView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001663 if (mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -07001664 mSubView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001665 }
1666 // Show the pending error dialog if the queue is not empty
1667 if (mQueuedErrors != null && mQueuedErrors.size() > 0) {
1668 showError(mQueuedErrors.getFirst());
1669 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001670 mWebViewController.bookmarkedStatusHasChanged(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001671 }
1672
1673 void putInBackground() {
John Reck8ee633f2011-08-09 16:00:35 -07001674 if (!mInForeground) {
1675 return;
1676 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001677 mInForeground = false;
1678 pause();
1679 mMainView.setOnCreateContextMenuListener(null);
1680 if (mSubView != null) {
1681 mSubView.setOnCreateContextMenuListener(null);
1682 }
1683 }
1684
Michael Kolb8233fac2010-10-26 16:08:53 -07001685 boolean inForeground() {
1686 return mInForeground;
1687 }
1688
Grace Kloba22ac16e2009-10-07 18:00:23 -07001689 /**
1690 * Return the top window of this tab; either the subwindow if it is not
1691 * null or the main window.
1692 * @return The top window of this tab.
1693 */
1694 WebView getTopWindow() {
1695 if (mSubView != null) {
1696 return mSubView;
1697 }
1698 return mMainView;
1699 }
1700
1701 /**
1702 * Return the main window of this tab. Note: if a tab is freed in the
1703 * background, this can return null. It is only guaranteed to be
1704 * non-null for the current tab.
1705 * @return The main WebView of this tab.
1706 */
1707 WebView getWebView() {
1708 return mMainView;
1709 }
1710
Michael Kolba713ec82010-11-29 17:27:06 -08001711 void setViewContainer(View container) {
1712 mContainer = container;
1713 }
1714
Michael Kolb8233fac2010-10-26 16:08:53 -07001715 View getViewContainer() {
1716 return mContainer;
1717 }
1718
Grace Kloba22ac16e2009-10-07 18:00:23 -07001719 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001720 * Return whether private browsing is enabled for the main window of
1721 * this tab.
1722 * @return True if private browsing is enabled.
1723 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001724 boolean isPrivateBrowsingEnabled() {
John Reck502a3532011-08-16 14:21:46 -07001725 return mCurrentState.mIncognito;
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001726 }
1727
1728 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001729 * Return the subwindow of this tab or null if there is no subwindow.
1730 * @return The subwindow of this tab or null.
1731 */
1732 WebView getSubWebView() {
1733 return mSubView;
1734 }
1735
Michael Kolb1514bb72010-11-22 09:11:48 -08001736 void setSubWebView(WebView subView) {
1737 mSubView = subView;
1738 }
1739
Michael Kolb8233fac2010-10-26 16:08:53 -07001740 View getSubViewContainer() {
1741 return mSubViewContainer;
1742 }
1743
Michael Kolb1514bb72010-11-22 09:11:48 -08001744 void setSubViewContainer(View subViewContainer) {
1745 mSubViewContainer = subViewContainer;
1746 }
1747
Grace Kloba22ac16e2009-10-07 18:00:23 -07001748 /**
1749 * @return The geolocation permissions prompt for this tab.
1750 */
1751 GeolocationPermissionsPrompt getGeolocationPermissionsPrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001752 if (mGeolocationPermissionsPrompt == null) {
1753 ViewStub stub = (ViewStub) mContainer
1754 .findViewById(R.id.geolocation_permissions_prompt);
1755 mGeolocationPermissionsPrompt = (GeolocationPermissionsPrompt) stub
1756 .inflate();
Grace Kloba50c241e2010-04-20 11:07:50 -07001757 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001758 return mGeolocationPermissionsPrompt;
1759 }
1760
1761 /**
1762 * @return The application id string
1763 */
1764 String getAppId() {
1765 return mAppId;
1766 }
1767
1768 /**
1769 * Set the application id string
1770 * @param id
1771 */
1772 void setAppId(String id) {
1773 mAppId = id;
1774 }
1775
Michael Kolbe28b3472011-08-04 16:54:31 -07001776 boolean closeOnBack() {
1777 return mCloseOnBack;
1778 }
1779
1780 void setCloseOnBack(boolean close) {
1781 mCloseOnBack = close;
1782 }
1783
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001784 boolean getDerivedFromIntent() {
1785 return mDerivedFromIntent;
1786 }
1787
1788 void setDerivedFromIntent(boolean derived) {
1789 mDerivedFromIntent = derived;
1790 }
1791
Grace Kloba22ac16e2009-10-07 18:00:23 -07001792 String getUrl() {
John Reck324d4402011-01-11 16:56:42 -08001793 return UrlUtils.filteredUrl(mCurrentState.mUrl);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001794 }
1795
Tarun Nainani8eb00912014-07-17 12:28:32 -07001796
1797 protected void onPageFinished() {
1798 mPageFinished = true;
1799 }
1800
1801 public boolean getPageFinishedStatus() {
1802 return mPageFinished;
1803 }
1804
John Reck49a603c2011-03-03 09:33:05 -08001805 String getOriginalUrl() {
Vivek Sekhar361065a2014-11-25 15:34:35 -08001806 if (mMainView != null)
1807 return mMainView.getOriginalUrl();
John Reckdb22ec42011-06-29 11:31:24 -07001808 if (mCurrentState.mOriginalUrl == null) {
1809 return getUrl();
John Reck49a603c2011-03-03 09:33:05 -08001810 }
John Reckdb22ec42011-06-29 11:31:24 -07001811 return UrlUtils.filteredUrl(mCurrentState.mOriginalUrl);
John Reck49a603c2011-03-03 09:33:05 -08001812 }
1813
Grace Kloba22ac16e2009-10-07 18:00:23 -07001814 /**
John Reck30c714c2010-12-16 17:30:34 -08001815 * Get the title of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001816 */
1817 String getTitle() {
John Reck30c714c2010-12-16 17:30:34 -08001818 return mCurrentState.mTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001819 }
1820
1821 /**
John Reck30c714c2010-12-16 17:30:34 -08001822 * Get the favicon of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001823 */
1824 Bitmap getFavicon() {
John Reck1cf4b792011-07-26 10:22:22 -07001825 if (mCurrentState.mFavicon != null) {
1826 return mCurrentState.mFavicon;
1827 }
1828 return getDefaultFavicon(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001829 }
1830
John Recke969cc52010-12-21 17:24:43 -08001831 public boolean isBookmarkedSite() {
1832 return mCurrentState.mIsBookmarkedSite;
1833 }
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001834
Grace Kloba22ac16e2009-10-07 18:00:23 -07001835 /**
1836 * Return the tab's error console. Creates the console if createIfNEcessary
1837 * is true and we haven't already created the console.
1838 * @param createIfNecessary Flag to indicate if the console should be
1839 * created if it has not been already.
1840 * @return The tab's error console, or null if one has not been created and
1841 * createIfNecessary is false.
1842 */
1843 ErrorConsoleView getErrorConsole(boolean createIfNecessary) {
1844 if (createIfNecessary && mErrorConsole == null) {
Michael Kolb14612442011-06-24 13:06:29 -07001845 mErrorConsole = new ErrorConsoleView(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001846 mErrorConsole.setWebView(mMainView);
1847 }
1848 return mErrorConsole;
1849 }
1850
Steve Block08a6f0c2011-10-06 12:12:53 +01001851 /**
1852 * Sets the security state, clears the SSL certificate error and informs
1853 * the controller.
1854 */
Steve Block2466eff2011-10-03 15:33:09 +01001855 private void setSecurityState(SecurityState securityState) {
1856 mCurrentState.mSecurityState = securityState;
Steve Block08a6f0c2011-10-06 12:12:53 +01001857 mCurrentState.mSslCertificateError = null;
Steve Block2466eff2011-10-03 15:33:09 +01001858 mWebViewController.onUpdatedSecurityState(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001859 }
1860
1861 /**
Steve Block2466eff2011-10-03 15:33:09 +01001862 * @return The tab's security state.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001863 */
Steve Block2466eff2011-10-03 15:33:09 +01001864 SecurityState getSecurityState() {
1865 return mCurrentState.mSecurityState;
John Reck30c714c2010-12-16 17:30:34 -08001866 }
1867
Steve Block08a6f0c2011-10-06 12:12:53 +01001868 /**
1869 * Gets the SSL certificate error, if any, for the page's main resource.
1870 * This is only non-null when the security state is
1871 * SECURITY_STATE_BAD_CERTIFICATE.
1872 */
1873 SslError getSslCertificateError() {
1874 return mCurrentState.mSslCertificateError;
1875 }
1876
John Reck30c714c2010-12-16 17:30:34 -08001877 int getLoadProgress() {
1878 if (mInPageLoad) {
1879 return mPageLoadProgress;
1880 }
1881 return 100;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001882 }
1883
1884 /**
1885 * @return TRUE if onPageStarted is called while onPageFinished is not
1886 * called yet.
1887 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001888 boolean inPageLoad() {
1889 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001890 }
1891
Grace Kloba22ac16e2009-10-07 18:00:23 -07001892 /**
John Reck1cf4b792011-07-26 10:22:22 -07001893 * @return The Bundle with the tab's state if it can be saved, otherwise null
Grace Kloba22ac16e2009-10-07 18:00:23 -07001894 */
John Reck1cf4b792011-07-26 10:22:22 -07001895 public Bundle saveState() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001896 // If the WebView is null it means we ran low on memory and we already
1897 // stored the saved state in mSavedState.
1898 if (mMainView == null) {
John Reck1cf4b792011-07-26 10:22:22 -07001899 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001900 }
John Reck6c2e2f32011-08-22 13:41:23 -07001901
1902 if (TextUtils.isEmpty(mCurrentState.mUrl)) {
John Reck1cf4b792011-07-26 10:22:22 -07001903 return null;
John Reck24f18262011-06-17 14:47:20 -07001904 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001905
1906 mSavedState = new Bundle();
John Reck6c2e2f32011-08-22 13:41:23 -07001907 WebBackForwardList savedList = mMainView.saveState(mSavedState);
1908 if (savedList == null || savedList.getSize() == 0) {
1909 Log.w(LOGTAG, "Failed to save back/forward list for "
1910 + mCurrentState.mUrl);
1911 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001912
Michael Kolbc831b632011-05-11 09:30:34 -07001913 mSavedState.putLong(ID, mId);
John Reck30c714c2010-12-16 17:30:34 -08001914 mSavedState.putString(CURRURL, mCurrentState.mUrl);
1915 mSavedState.putString(CURRTITLE, mCurrentState.mTitle);
John Reck1cf4b792011-07-26 10:22:22 -07001916 mSavedState.putBoolean(INCOGNITO, mMainView.isPrivateBrowsingEnabled());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001917 if (mAppId != null) {
1918 mSavedState.putString(APPID, mAppId);
1919 }
Michael Kolbe28b3472011-08-04 16:54:31 -07001920 mSavedState.putBoolean(CLOSEFLAG, mCloseOnBack);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001921 // Remember the parent tab so the relationship can be restored.
Michael Kolbc831b632011-05-11 09:30:34 -07001922 if (mParent != null) {
1923 mSavedState.putLong(PARENTTAB, mParent.mId);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001924 }
John Reckb0a86db2011-05-24 14:05:58 -07001925 mSavedState.putBoolean(USERAGENT,
1926 mSettings.hasDesktopUseragent(getWebView()));
John Reck1cf4b792011-07-26 10:22:22 -07001927 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001928 }
1929
1930 /*
1931 * Restore the state of the tab.
1932 */
John Reck1cf4b792011-07-26 10:22:22 -07001933 private void restoreState(Bundle b) {
1934 mSavedState = b;
1935 if (mSavedState == null) {
1936 return;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001937 }
1938 // Restore the internal state even if the WebView fails to restore.
1939 // This will maintain the app id, original url and close-on-exit values.
Michael Kolbc831b632011-05-11 09:30:34 -07001940 mId = b.getLong(ID);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001941 mAppId = b.getString(APPID);
Michael Kolbe28b3472011-08-04 16:54:31 -07001942 mCloseOnBack = b.getBoolean(CLOSEFLAG);
John Reck8b9bb8b2012-03-08 13:19:40 -08001943 restoreUserAgent();
John Reck1cf4b792011-07-26 10:22:22 -07001944 String url = b.getString(CURRURL);
1945 String title = b.getString(CURRTITLE);
1946 boolean incognito = b.getBoolean(INCOGNITO);
1947 mCurrentState = new PageState(mContext, incognito, url, null);
1948 mCurrentState.mTitle = title;
1949 synchronized (Tab.this) {
1950 if (mCapture != null) {
John Reck4eadc342011-10-31 14:04:10 -07001951 DataController.getInstance(mContext).loadThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07001952 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001953 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001954 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04001955
John Reck8b9bb8b2012-03-08 13:19:40 -08001956 private void restoreUserAgent() {
1957 if (mMainView == null || mSavedState == null) {
1958 return;
1959 }
1960 if (mSavedState.getBoolean(USERAGENT)
1961 != mSettings.hasDesktopUseragent(mMainView)) {
1962 mSettings.toggleDesktopUseragent(mMainView);
1963 }
1964 }
1965
Leon Scroggins1961ed22010-12-07 15:22:21 -05001966 public void updateBookmarkedStatus() {
John Recke969cc52010-12-21 17:24:43 -08001967 mDataController.queryBookmarkStatus(getUrl(), mIsBookmarkCallback);
Leon Scroggins1961ed22010-12-07 15:22:21 -05001968 }
1969
John Recke969cc52010-12-21 17:24:43 -08001970 private DataController.OnQueryUrlIsBookmark mIsBookmarkCallback
1971 = new DataController.OnQueryUrlIsBookmark() {
1972 @Override
1973 public void onQueryUrlIsBookmark(String url, boolean isBookmark) {
1974 if (mCurrentState.mUrl.equals(url)) {
1975 mCurrentState.mIsBookmarkedSite = isBookmark;
1976 mWebViewController.bookmarkedStatusHasChanged(Tab.this);
1977 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001978 }
John Recke969cc52010-12-21 17:24:43 -08001979 };
Michael Kolb1acef692011-03-08 14:12:06 -08001980
Michael Kolbeb95db42011-03-03 10:38:40 -08001981 public Bitmap getScreenshot() {
John Reck1cf4b792011-07-26 10:22:22 -07001982 synchronized (Tab.this) {
1983 return mCapture;
1984 }
Michael Kolbeb95db42011-03-03 10:38:40 -08001985 }
1986
John Reck541f55a2011-06-07 16:34:43 -07001987 public boolean isSnapshot() {
John Reck541f55a2011-06-07 16:34:43 -07001988 return false;
1989 }
1990
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001991 private static class SaveCallback implements ValueCallback<String> {
1992 boolean onReceiveValueCalled = false;
1993 private String mPath;
John Reck68234a92012-04-19 15:27:12 -07001994
1995 @Override
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001996 public void onReceiveValue(String path) {
1997 this.onReceiveValueCalled = true;
1998 this.mPath = path;
John Reck68234a92012-04-19 15:27:12 -07001999 synchronized (this) {
2000 notifyAll();
John Reck8cc92352011-07-06 17:41:52 -07002001 }
John Reck541f55a2011-06-07 16:34:43 -07002002 }
John Reck68234a92012-04-19 15:27:12 -07002003
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002004 public String getPath() {
2005 return mPath;
2006 }
John Reck68234a92012-04-19 15:27:12 -07002007 }
2008
2009 /**
2010 * Must be called on the UI thread
2011 */
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002012 public ContentValues createSnapshotValues(Bitmap bm) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002013 WebView web = getWebView();
John Reck68234a92012-04-19 15:27:12 -07002014 if (web == null) return null;
John Reckd8c74522011-06-14 08:45:00 -07002015 ContentValues values = new ContentValues();
2016 values.put(Snapshots.TITLE, mCurrentState.mTitle);
2017 values.put(Snapshots.URL, mCurrentState.mUrl);
John Reck68234a92012-04-19 15:27:12 -07002018 values.put(Snapshots.BACKGROUND, web.getPageBackgroundColor());
John Reck8cc92352011-07-06 17:41:52 -07002019 values.put(Snapshots.DATE_CREATED, System.currentTimeMillis());
2020 values.put(Snapshots.FAVICON, compressBitmap(getFavicon()));
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002021 values.put(Snapshots.THUMBNAIL, compressBitmap(bm));
John Reckd8c74522011-06-14 08:45:00 -07002022 return values;
John Reck541f55a2011-06-07 16:34:43 -07002023 }
2024
John Reck68234a92012-04-19 15:27:12 -07002025 /**
2026 * Probably want to call this on a background thread
2027 */
2028 public boolean saveViewState(ContentValues values) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002029 WebView web = getWebView();
John Reck68234a92012-04-19 15:27:12 -07002030 if (web == null) return false;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002031 String filename = UUID.randomUUID().toString();
John Reck68234a92012-04-19 15:27:12 -07002032 SaveCallback callback = new SaveCallback();
John Reck68234a92012-04-19 15:27:12 -07002033 try {
John Reck68234a92012-04-19 15:27:12 -07002034 synchronized (callback) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002035 web.saveViewState(filename, callback);
2036 callback.wait();
John Reck68234a92012-04-19 15:27:12 -07002037 }
John Reck68234a92012-04-19 15:27:12 -07002038 } catch (Exception e) {
2039 Log.w(LOGTAG, "Failed to save view state", e);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002040 String path = callback.getPath();
2041 if (path != null) {
2042 File file = mContext.getFileStreamPath(path);
2043 if (file.exists() && !file.delete()) {
2044 file.deleteOnExit();
2045 }
John Reck68234a92012-04-19 15:27:12 -07002046 }
2047 return false;
2048 }
Tarun Nainani8eb00912014-07-17 12:28:32 -07002049
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002050 String path = callback.getPath();
Tarun Nainani8eb00912014-07-17 12:28:32 -07002051 // could be that saving of file failed
2052 if (path == null) {
2053 return false;
2054 }
2055
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002056 File savedFile = new File(path);
2057 if (!savedFile.exists()) {
2058 return false;
John Reck68234a92012-04-19 15:27:12 -07002059 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002060 values.put(Snapshots.VIEWSTATE_PATH, path.substring(path.lastIndexOf('/') + 1));
2061 values.put(Snapshots.VIEWSTATE_SIZE, savedFile.length());
John Reck68234a92012-04-19 15:27:12 -07002062 return true;
2063 }
2064
John Reck8cc92352011-07-06 17:41:52 -07002065 public byte[] compressBitmap(Bitmap bitmap) {
2066 if (bitmap == null) {
2067 return null;
2068 }
2069 ByteArrayOutputStream stream = new ByteArrayOutputStream();
2070 bitmap.compress(CompressFormat.PNG, 100, stream);
2071 return stream.toByteArray();
2072 }
2073
John Reck26b18322011-06-21 13:08:58 -07002074 public void loadUrl(String url, Map<String, String> headers) {
2075 if (mMainView != null) {
Michael Kolba53c9892011-10-05 13:31:40 -07002076 mPageLoadProgress = INITIAL_PROGRESS;
Michael Kolb14612442011-06-24 13:06:29 -07002077 mCurrentState = new PageState(mContext, false, url, null);
John Reck26b18322011-06-21 13:08:58 -07002078 mMainView.loadUrl(url, headers);
2079 }
2080 }
2081
John Reck38b39652012-06-05 09:22:59 -07002082 public void disableUrlOverridingForLoad() {
2083 mDisableOverrideUrlLoading = true;
2084 }
2085
Michael Kolb9ef259a2011-07-12 15:33:08 -07002086 protected void capture() {
2087 if (mMainView == null || mCapture == null) return;
John Reck4eadc342011-10-31 14:04:10 -07002088 if (mMainView.getContentWidth() <= 0 || mMainView.getContentHeight() <= 0) {
2089 return;
2090 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002091
2092 mMainView
2093 .getContentBitmapAsync(
2094 (float) mCaptureWidth / mMainView.getWidth(),
2095 new Rect(),
2096 new ValueCallback<Bitmap>() {
2097 @Override
2098 public void onReceiveValue(Bitmap bitmap) {
2099 onCaptureCallback(bitmap);
2100 }});
2101 }
2102
2103 private void onCaptureCallback(Bitmap bitmap) {
2104 if (mCapture == null || bitmap == null)
2105 return;
2106
Michael Kolb9ef259a2011-07-12 15:33:08 -07002107 Canvas c = new Canvas(mCapture);
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002108 mCapture.eraseColor(Color.WHITE);
2109 c.drawBitmap(bitmap, 0, 0, null);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002110
Michael Kolba3194d02011-09-07 11:23:51 -07002111 // manually anti-alias the edges for the tilt
2112 c.drawRect(0, 0, 1, mCapture.getHeight(), sAlphaPaint);
2113 c.drawRect(mCapture.getWidth() - 1, 0, mCapture.getWidth(),
2114 mCapture.getHeight(), sAlphaPaint);
2115 c.drawRect(0, 0, mCapture.getWidth(), 1, sAlphaPaint);
2116 c.drawRect(0, mCapture.getHeight() - 1, mCapture.getWidth(),
2117 mCapture.getHeight(), sAlphaPaint);
Dianne Hackborn43cfe8a2011-08-02 16:59:35 -07002118 c.setBitmap(null);
John Reck8ee633f2011-08-09 16:00:35 -07002119 mHandler.removeMessages(MSG_CAPTURE);
John Reck1cf4b792011-07-26 10:22:22 -07002120 persistThumbnail();
John Reck8ee633f2011-08-09 16:00:35 -07002121 TabControl tc = mWebViewController.getTabControl();
2122 if (tc != null) {
2123 OnThumbnailUpdatedListener updateListener
2124 = tc.getOnThumbnailUpdatedListener();
2125 if (updateListener != null) {
2126 updateListener.onThumbnailUpdated(this);
2127 }
2128 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07002129 }
2130
2131 @Override
2132 public void onNewPicture(WebView view, Picture picture) {
John Reck1cf4b792011-07-26 10:22:22 -07002133 postCapture();
2134 }
2135
2136 private void postCapture() {
Michael Kolb9ef259a2011-07-12 15:33:08 -07002137 if (!mHandler.hasMessages(MSG_CAPTURE)) {
2138 mHandler.sendEmptyMessageDelayed(MSG_CAPTURE, CAPTURE_DELAY);
2139 }
2140 }
2141
John Reckef654f12011-07-12 16:42:08 -07002142 public boolean canGoBack() {
2143 return mMainView != null ? mMainView.canGoBack() : false;
2144 }
2145
2146 public boolean canGoForward() {
2147 return mMainView != null ? mMainView.canGoForward() : false;
2148 }
2149
2150 public void goBack() {
2151 if (mMainView != null) {
2152 mMainView.goBack();
2153 }
2154 }
2155
2156 public void goForward() {
2157 if (mMainView != null) {
2158 mMainView.goForward();
2159 }
2160 }
2161
Mathew Inwood1dd8e822011-08-03 14:34:29 +01002162 /**
2163 * Causes the tab back/forward stack to be cleared once, if the given URL is the next URL
2164 * to be added to the stack.
2165 *
2166 * This is used to ensure that preloaded URLs that are not subsequently seen by the user do
2167 * not appear in the back stack.
2168 */
Mathew Inwooda829d552011-09-02 14:16:25 +01002169 public void clearBackStackWhenItemAdded(Pattern urlPattern) {
2170 mClearHistoryUrlPattern = urlPattern;
Mathew Inwood1dd8e822011-08-03 14:34:29 +01002171 }
2172
John Reck1cf4b792011-07-26 10:22:22 -07002173 protected void persistThumbnail() {
John Reck4eadc342011-10-31 14:04:10 -07002174 DataController.getInstance(mContext).saveThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07002175 }
2176
2177 protected void deleteThumbnail() {
John Reck4eadc342011-10-31 14:04:10 -07002178 DataController.getInstance(mContext).deleteThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07002179 }
2180
John Reck4eadc342011-10-31 14:04:10 -07002181 void updateCaptureFromBlob(byte[] blob) {
John Reck1cf4b792011-07-26 10:22:22 -07002182 synchronized (Tab.this) {
2183 if (mCapture == null) {
2184 return;
2185 }
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002186 ByteBuffer buffer = ByteBuffer.wrap(blob);
Michael Kolbd837a112011-08-09 14:00:18 -07002187 try {
2188 mCapture.copyPixelsFromBuffer(buffer);
2189 } catch (RuntimeException rex) {
2190 Log.e(LOGTAG, "Load capture has mismatched sizes; buffer: "
2191 + buffer.capacity() + " blob: " + blob.length
2192 + "capture: " + mCapture.getByteCount());
2193 throw rex;
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002194 }
John Reck1cf4b792011-07-26 10:22:22 -07002195 }
2196 }
2197
John Reck52be4782011-08-26 15:37:29 -07002198 @Override
2199 public String toString() {
2200 StringBuilder builder = new StringBuilder(100);
2201 builder.append(mId);
2202 builder.append(") has parent: ");
2203 if (getParent() != null) {
2204 builder.append("true[");
2205 builder.append(getParent().getId());
2206 builder.append("]");
2207 } else {
2208 builder.append("false");
2209 }
2210 builder.append(", incog: ");
2211 builder.append(isPrivateBrowsingEnabled());
2212 if (!isPrivateBrowsingEnabled()) {
2213 builder.append(", title: ");
2214 builder.append(getTitle());
2215 builder.append(", url: ");
2216 builder.append(getUrl());
2217 }
2218 return builder.toString();
2219 }
2220
Steve Block4895b012011-10-03 16:26:46 +01002221 private void handleProceededAfterSslError(SslError error) {
2222 if (error.getUrl().equals(mCurrentState.mUrl)) {
2223 // The security state should currently be SECURITY_STATE_SECURE.
2224 setSecurityState(SecurityState.SECURITY_STATE_BAD_CERTIFICATE);
Steve Block08a6f0c2011-10-06 12:12:53 +01002225 mCurrentState.mSslCertificateError = error;
Steve Block4895b012011-10-03 16:26:46 +01002226 } else if (getSecurityState() == SecurityState.SECURITY_STATE_SECURE) {
Steve Block08a6f0c2011-10-06 12:12:53 +01002227 // The page's main resource is secure and this error is for a
2228 // sub-resource.
Steve Block4895b012011-10-03 16:26:46 +01002229 setSecurityState(SecurityState.SECURITY_STATE_MIXED);
2230 }
2231 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002232}