blob: cc9ddbd26372cfa9b588155965469d93144258c0 [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;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080075import org.codeaurora.swe.WebChromeClient;
76import org.codeaurora.swe.WebHistoryItem;
77import org.codeaurora.swe.WebView;
78import org.codeaurora.swe.WebView.PictureListener;
Pankaj Garg1c7380d2014-08-27 14:17:12 -070079import org.codeaurora.swe.WebView.CreateWindowParams;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080080import org.codeaurora.swe.WebViewClient;
81
John Reck541f55a2011-06-07 16:34:43 -070082import java.io.ByteArrayOutputStream;
John Reck2b71d6d2012-04-18 17:42:06 -070083import java.io.File;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080084import java.io.InputStream;
John Reck1cf4b792011-07-26 10:22:22 -070085import java.nio.ByteBuffer;
Michael Kolbfe251992010-07-08 15:41:55 -070086import java.util.LinkedList;
87import java.util.Map;
John Reck2b71d6d2012-04-18 17:42:06 -070088import java.util.UUID;
Michael Kolbfe251992010-07-08 15:41:55 -070089import java.util.Vector;
Mathew Inwooda829d552011-09-02 14:16:25 +010090import java.util.regex.Pattern;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080091import java.sql.Timestamp;
92import java.util.Date;
Michael Kolbfe251992010-07-08 15:41:55 -070093
Grace Kloba22ac16e2009-10-07 18:00:23 -070094/**
95 * Class for maintaining Tabs with a main WebView and a subwindow.
96 */
Michael Kolb9ef259a2011-07-12 15:33:08 -070097class Tab implements PictureListener {
Michael Kolb8233fac2010-10-26 16:08:53 -070098
Grace Kloba22ac16e2009-10-07 18:00:23 -070099 // Log Tag
100 private static final String LOGTAG = "Tab";
Bijan Amirzada41242f22014-03-21 12:12:18 -0700101 private static final boolean LOGD_ENABLED = com.android.browser.Browser.LOGD_ENABLED;
Ben Murdochc42addf2010-01-28 15:19:59 +0000102 // Special case the logtag for messages for the Console to make it easier to
103 // filter them and match the logtag used for these messages in older versions
104 // of the browser.
105 private static final String CONSOLE_LOGTAG = "browser";
106
Michael Kolb9ef259a2011-07-12 15:33:08 -0700107 private static final int MSG_CAPTURE = 42;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800108 private static final int CAPTURE_DELAY = 1000;
Michael Kolba53c9892011-10-05 13:31:40 -0700109 private static final int INITIAL_PROGRESS = 5;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700110
John Reck1cf4b792011-07-26 10:22:22 -0700111 private static Bitmap sDefaultFavicon;
112
Michael Kolba3194d02011-09-07 11:23:51 -0700113 private static Paint sAlphaPaint = new Paint();
114 static {
115 sAlphaPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
116 sAlphaPaint.setColor(Color.TRANSPARENT);
117 }
118
Steve Block2466eff2011-10-03 15:33:09 +0100119 public enum SecurityState {
Steve Block4895b012011-10-03 16:26:46 +0100120 // The page's main resource does not use SSL. Note that we use this
121 // state irrespective of the SSL authentication state of sub-resources.
Steve Block2466eff2011-10-03 15:33:09 +0100122 SECURITY_STATE_NOT_SECURE,
Steve Block4895b012011-10-03 16:26:46 +0100123 // The page's main resource uses SSL and the certificate is good. The
124 // same is true of all sub-resources.
Steve Block2466eff2011-10-03 15:33:09 +0100125 SECURITY_STATE_SECURE,
Steve Block4895b012011-10-03 16:26:46 +0100126 // The page's main resource uses SSL and the certificate is good, but
127 // some sub-resources either do not use SSL or have problems with their
128 // certificates.
Steve Block2466eff2011-10-03 15:33:09 +0100129 SECURITY_STATE_MIXED,
Steve Block4895b012011-10-03 16:26:46 +0100130 // The page's main resource uses SSL but there is a problem with its
131 // certificate.
132 SECURITY_STATE_BAD_CERTIFICATE,
John Reck30c714c2010-12-16 17:30:34 -0800133 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700134
Michael Kolb14612442011-06-24 13:06:29 -0700135 Context mContext;
John Reckd8c74522011-06-14 08:45:00 -0700136 protected WebViewController mWebViewController;
Michael Kolb8233fac2010-10-26 16:08:53 -0700137
Michael Kolbc831b632011-05-11 09:30:34 -0700138 // The tab ID
John Reckd8c74522011-06-14 08:45:00 -0700139 private long mId = -1;
Michael Kolbc831b632011-05-11 09:30:34 -0700140
Grace Kloba22ac16e2009-10-07 18:00:23 -0700141 // The Geolocation permissions prompt
142 private GeolocationPermissionsPrompt mGeolocationPermissionsPrompt;
143 // Main WebView wrapper
Michael Kolba713ec82010-11-29 17:27:06 -0800144 private View mContainer;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700145 // Main WebView
146 private WebView mMainView;
147 // Subwindow container
148 private View mSubViewContainer;
149 // Subwindow WebView
150 private WebView mSubView;
151 // Saved bundle for when we are running low on memory. It contains the
152 // information needed to restore the WebView if the user goes back to the
153 // tab.
154 private Bundle mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700155 // Parent Tab. This is the Tab that created this Tab, or null if the Tab was
156 // created by the UI
Michael Kolbc831b632011-05-11 09:30:34 -0700157 private Tab mParent;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700158 // Tab that constructed by this Tab. This is used when this Tab is
159 // destroyed, it clears all mParentTab values in the children.
Michael Kolbc831b632011-05-11 09:30:34 -0700160 private Vector<Tab> mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700161 // If true, the tab is in the foreground of the current activity.
162 private boolean mInForeground;
Michael Kolb8233fac2010-10-26 16:08:53 -0700163 // If true, the tab is in page loading state (after onPageStarted,
164 // before onPageFinsihed)
165 private boolean mInPageLoad;
Tarun Nainani8eb00912014-07-17 12:28:32 -0700166 private boolean mPageFinished;
John Reck38b39652012-06-05 09:22:59 -0700167 private boolean mDisableOverrideUrlLoading;
Pankaj Garg79878492015-04-01 14:48:21 -0700168 private boolean mFirstVisualPixelPainted = false;
John Reck30c714c2010-12-16 17:30:34 -0800169 // The last reported progress of the current page
170 private int mPageLoadProgress;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000171 // The time the load started, used to find load page time
172 private long mLoadStartTime;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700173 // Application identifier used to find tabs that another application wants
174 // to reuse.
175 private String mAppId;
Michael Kolbe28b3472011-08-04 16:54:31 -0700176 // flag to indicate if tab should be closed on back
177 private boolean mCloseOnBack;
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700178 // flag to indicate if the tab was opened from an intent
179 private boolean mDerivedFromIntent = false;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -0500180 // The listener that gets invoked when a download is started from the
181 // mMainView
Selim Gurun0b3d66f2012-08-29 13:08:13 -0700182 private final BrowserDownloadListener mDownloadListener;
John Recke969cc52010-12-21 17:24:43 -0800183 private DataController mDataController;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700184
185 // AsyncTask for downloading touch icons
186 DownloadTouchIcon mTouchIconLoader;
187
John Reck35e9dd62011-04-25 09:01:54 -0700188 private BrowserSettings mSettings;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700189 private int mCaptureWidth;
190 private int mCaptureHeight;
191 private Bitmap mCapture;
192 private Handler mHandler;
Michael Kolb72864272012-05-03 15:42:15 -0700193 private boolean mUpdateThumbnail;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800194 private Timestamp timestamp;
Vivek Sekharf96064b2014-07-28 16:32:34 -0700195 private boolean mFullScreen = false;
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800196 private boolean mReceivedError;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700197
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -0700198 // determine if webview is destroyed to MemoryMonitor
199 private boolean mWebViewDestroyedByMemoryMonitor;
200
201
Mathew Inwood1dd8e822011-08-03 14:34:29 +0100202
John Reck1cf4b792011-07-26 10:22:22 -0700203 private static synchronized Bitmap getDefaultFavicon(Context context) {
204 if (sDefaultFavicon == null) {
205 sDefaultFavicon = BitmapFactory.decodeResource(
Enrico Rosd6efa972014-12-02 19:49:59 -0800206 context.getResources(), R.drawable.ic_deco_favicon_normal);
John Reck1cf4b792011-07-26 10:22:22 -0700207 }
208 return sDefaultFavicon;
209 }
Michael Kolbeb95db42011-03-03 10:38:40 -0800210
John Reck30c714c2010-12-16 17:30:34 -0800211 // All the state needed for a page
John Reckd8c74522011-06-14 08:45:00 -0700212 protected static class PageState {
John Reck30c714c2010-12-16 17:30:34 -0800213 String mUrl;
John Reckdb22ec42011-06-29 11:31:24 -0700214 String mOriginalUrl;
John Reck30c714c2010-12-16 17:30:34 -0800215 String mTitle;
Steve Block2466eff2011-10-03 15:33:09 +0100216 SecurityState mSecurityState;
Steve Block08a6f0c2011-10-06 12:12:53 +0100217 // This is non-null only when mSecurityState is SECURITY_STATE_BAD_CERTIFICATE.
218 SslError mSslCertificateError;
John Reck30c714c2010-12-16 17:30:34 -0800219 Bitmap mFavicon;
Steve Block08a6f0c2011-10-06 12:12:53 +0100220 boolean mIsBookmarkedSite;
221 boolean mIncognito;
John Reck30c714c2010-12-16 17:30:34 -0800222
223 PageState(Context c, boolean incognito) {
John Reck502a3532011-08-16 14:21:46 -0700224 mIncognito = incognito;
225 if (mIncognito) {
Vivek Sekhared791da2015-02-22 12:39:05 -0800226 mOriginalUrl = mUrl = "chrome://incognito";
John Reck30c714c2010-12-16 17:30:34 -0800227 mTitle = c.getString(R.string.new_incognito_tab);
John Reck30c714c2010-12-16 17:30:34 -0800228 } else {
John Reckdb22ec42011-06-29 11:31:24 -0700229 mOriginalUrl = mUrl = "";
John Reck30c714c2010-12-16 17:30:34 -0800230 mTitle = c.getString(R.string.new_tab);
John Reck30c714c2010-12-16 17:30:34 -0800231 }
Steve Block2466eff2011-10-03 15:33:09 +0100232 mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
John Reck30c714c2010-12-16 17:30:34 -0800233 }
234
235 PageState(Context c, boolean incognito, String url, Bitmap favicon) {
John Reck502a3532011-08-16 14:21:46 -0700236 mIncognito = incognito;
John Reckdb22ec42011-06-29 11:31:24 -0700237 mOriginalUrl = mUrl = url;
John Reck30c714c2010-12-16 17:30:34 -0800238 if (URLUtil.isHttpsUrl(url)) {
Steve Block2466eff2011-10-03 15:33:09 +0100239 mSecurityState = SecurityState.SECURITY_STATE_SECURE;
John Reck30c714c2010-12-16 17:30:34 -0800240 } else {
Steve Block2466eff2011-10-03 15:33:09 +0100241 mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
John Reck30c714c2010-12-16 17:30:34 -0800242 }
John Reck1cf4b792011-07-26 10:22:22 -0700243 mFavicon = favicon;
John Reck30c714c2010-12-16 17:30:34 -0800244 }
John Reck1cf4b792011-07-26 10:22:22 -0700245
Grace Kloba22ac16e2009-10-07 18:00:23 -0700246 }
247
John Reck30c714c2010-12-16 17:30:34 -0800248 // The current/loading page's state
John Reckd8c74522011-06-14 08:45:00 -0700249 protected PageState mCurrentState;
John Reck30c714c2010-12-16 17:30:34 -0800250
Grace Kloba22ac16e2009-10-07 18:00:23 -0700251 // Used for saving and restoring each Tab
Michael Kolbc831b632011-05-11 09:30:34 -0700252 static final String ID = "ID";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700253 static final String CURRURL = "currentUrl";
254 static final String CURRTITLE = "currentTitle";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700255 static final String PARENTTAB = "parentTab";
256 static final String APPID = "appid";
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700257 static final String INCOGNITO = "privateBrowsingEnabled";
John Reckb0a86db2011-05-24 14:05:58 -0700258 static final String USERAGENT = "useragent";
Michael Kolbe28b3472011-08-04 16:54:31 -0700259 static final String CLOSEFLAG = "closeOnBack";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700260
Grace Kloba22ac16e2009-10-07 18:00:23 -0700261 // Container class for the next error dialog that needs to be displayed
262 private class ErrorDialog {
263 public final int mTitle;
264 public final String mDescription;
265 public final int mError;
266 ErrorDialog(int title, String desc, int error) {
267 mTitle = title;
268 mDescription = desc;
269 mError = error;
270 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700271 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700272
273 private void processNextError() {
274 if (mQueuedErrors == null) {
275 return;
276 }
277 // The first one is currently displayed so just remove it.
278 mQueuedErrors.removeFirst();
279 if (mQueuedErrors.size() == 0) {
280 mQueuedErrors = null;
281 return;
282 }
283 showError(mQueuedErrors.getFirst());
284 }
285
286 private DialogInterface.OnDismissListener mDialogListener =
287 new DialogInterface.OnDismissListener() {
288 public void onDismiss(DialogInterface d) {
289 processNextError();
290 }
291 };
292 private LinkedList<ErrorDialog> mQueuedErrors;
293
294 private void queueError(int err, String desc) {
295 if (mQueuedErrors == null) {
296 mQueuedErrors = new LinkedList<ErrorDialog>();
297 }
298 for (ErrorDialog d : mQueuedErrors) {
299 if (d.mError == err) {
300 // Already saw a similar error, ignore the new one.
301 return;
302 }
303 }
304 ErrorDialog errDialog = new ErrorDialog(
305 err == WebViewClient.ERROR_FILE_NOT_FOUND ?
306 R.string.browserFrameFileErrorLabel :
307 R.string.browserFrameNetworkErrorLabel,
308 desc, err);
309 mQueuedErrors.addLast(errDialog);
310
311 // Show the dialog now if the queue was empty and it is in foreground
312 if (mQueuedErrors.size() == 1 && mInForeground) {
313 showError(errDialog);
314 }
315 }
316
317 private void showError(ErrorDialog errDialog) {
318 if (mInForeground) {
Michael Kolb14612442011-06-24 13:06:29 -0700319 AlertDialog d = new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700320 .setTitle(errDialog.mTitle)
321 .setMessage(errDialog.mDescription)
322 .setPositiveButton(R.string.ok, null)
323 .create();
324 d.setOnDismissListener(mDialogListener);
325 d.show();
326 }
327 }
328
Pankaj Garg18186a92015-03-31 14:59:33 -0700329 public void setNetworkAvailable(boolean networkUp) {
330 if (networkUp && mReceivedError && (mMainView != null)) {
331 mMainView.reload();
332 }
333 }
334
Grace Kloba22ac16e2009-10-07 18:00:23 -0700335 // -------------------------------------------------------------------------
336 // WebViewClient implementation for the main WebView
337 // -------------------------------------------------------------------------
338
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800339 private final WebViewClient mWebViewClient = new WebViewClient() {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500340 private Message mDontResend;
341 private Message mResend;
Michael Kolb47bd1e42011-09-01 15:25:00 -0700342
343 private boolean providersDiffer(String url, String otherUrl) {
344 Uri uri1 = Uri.parse(url);
345 Uri uri2 = Uri.parse(otherUrl);
346 return !uri1.getEncodedAuthority().equals(uri2.getEncodedAuthority());
347 }
348
Grace Kloba22ac16e2009-10-07 18:00:23 -0700349 @Override
350 public void onPageStarted(WebView view, String url, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700351 mInPageLoad = true;
Tarun Nainani8eb00912014-07-17 12:28:32 -0700352 mPageFinished = false;
Pankaj Garg79878492015-04-01 14:48:21 -0700353 mFirstVisualPixelPainted = false;
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800354 mReceivedError = false;
Michael Kolb72864272012-05-03 15:42:15 -0700355 mUpdateThumbnail = true;
Michael Kolba53c9892011-10-05 13:31:40 -0700356 mPageLoadProgress = INITIAL_PROGRESS;
Michael Kolb14612442011-06-24 13:06:29 -0700357 mCurrentState = new PageState(mContext,
John Reck30c714c2010-12-16 17:30:34 -0800358 view.isPrivateBrowsingEnabled(), url, favicon);
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000359 mLoadStartTime = SystemClock.uptimeMillis();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700360
361 // If we start a touch icon load and then load a new page, we don't
362 // want to cancel the current touch icon loader. But, we do want to
363 // create a new one when the touch icon url is known.
364 if (mTouchIconLoader != null) {
365 mTouchIconLoader.mTab = null;
366 mTouchIconLoader = null;
367 }
368
Panos Thomasb298aad2014-10-22 12:24:21 -0700369 // Loading a new page voids any ongoing Geolocation permission
370 // requests.
371 if (mGeolocationPermissionsPrompt != null) {
372 mGeolocationPermissionsPrompt.dismiss();
373 }
374
Grace Kloba22ac16e2009-10-07 18:00:23 -0700375 // finally update the UI in the activity if it is in the foreground
John Reck324d4402011-01-11 16:56:42 -0800376 mWebViewController.onPageStarted(Tab.this, view, favicon);
Leon Scroggins4cd97792010-12-03 15:31:56 -0500377
John Recke969cc52010-12-21 17:24:43 -0800378 updateBookmarkedStatus();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700379 }
380
381 @Override
382 public void onPageFinished(WebView view, String url) {
John Reck38b39652012-06-05 09:22:59 -0700383 mDisableOverrideUrlLoading = false;
John Reck5b691842010-11-29 11:21:13 -0800384 if (!isPrivateBrowsingEnabled()) {
385 LogTag.logPageFinishedLoading(
386 url, SystemClock.uptimeMillis() - mLoadStartTime);
387 }
John Reck1cf4b792011-07-26 10:22:22 -0700388 syncCurrentState(view, url);
John Reck324d4402011-01-11 16:56:42 -0800389 mWebViewController.onPageFinished(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700390 }
391
Pankaj Garg79878492015-04-01 14:48:21 -0700392 @Override
393 public void onFirstVisualPixel(WebView view) {
394 mFirstVisualPixelPainted = true;
395 }
396
Grace Kloba22ac16e2009-10-07 18:00:23 -0700397 // return true if want to hijack the url to let another app to handle it
398 @Override
399 public boolean shouldOverrideUrlLoading(WebView view, String url) {
John Reck38b39652012-06-05 09:22:59 -0700400 if (!mDisableOverrideUrlLoading && mInForeground) {
Michael Kolb18eb3772010-12-10 14:29:51 -0800401 return mWebViewController.shouldOverrideUrlLoading(Tab.this,
402 view, url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700403 } else {
404 return false;
405 }
406 }
407
Vivek Sekharb991edb2014-12-17 18:18:07 -0800408 @Override
409 public boolean shouldDownloadFavicon(WebView view, String url) {
410 return true;
411 }
412
Grace Kloba22ac16e2009-10-07 18:00:23 -0700413 /**
Steve Block2466eff2011-10-03 15:33:09 +0100414 * Updates the security state. This method is called when we discover
415 * another resource to be loaded for this page (for example,
416 * javascript). While we update the security state, we do not update
417 * the lock icon until we are done loading, as it is slightly more
418 * secure this way.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700419 */
420 @Override
421 public void onLoadResource(WebView view, String url) {
422 if (url != null && url.length() > 0) {
423 // It is only if the page claims to be secure that we may have
Steve Block2466eff2011-10-03 15:33:09 +0100424 // to update the security state:
425 if (mCurrentState.mSecurityState == SecurityState.SECURITY_STATE_SECURE) {
426 // If NOT a 'safe' url, change the state to mixed content!
Grace Kloba22ac16e2009-10-07 18:00:23 -0700427 if (!(URLUtil.isHttpsUrl(url) || URLUtil.isDataUrl(url)
428 || URLUtil.isAboutUrl(url))) {
Steve Block2466eff2011-10-03 15:33:09 +0100429 mCurrentState.mSecurityState = SecurityState.SECURITY_STATE_MIXED;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700430 }
431 }
432 }
433 }
434
435 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700436 * Show a dialog informing the user of the network error reported by
437 * WebCore if it is in the foreground.
438 */
439 @Override
440 public void onReceivedError(WebView view, int errorCode,
441 String description, String failingUrl) {
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800442 // Used for the syncCurrentState to use
443 // the failing url instead of using webview url
444 mReceivedError = true;
445
Grace Kloba22ac16e2009-10-07 18:00:23 -0700446 if (errorCode != WebViewClient.ERROR_HOST_LOOKUP &&
447 errorCode != WebViewClient.ERROR_CONNECT &&
448 errorCode != WebViewClient.ERROR_BAD_URL &&
449 errorCode != WebViewClient.ERROR_UNSUPPORTED_SCHEME &&
450 errorCode != WebViewClient.ERROR_FILE) {
451 queueError(errorCode, description);
Jeff Hamilton47654f42010-09-07 09:57:51 -0500452
Selim Gurun3da06b82011-10-10 13:58:12 -0700453 // Don't log URLs when in private browsing mode
454 if (!isPrivateBrowsingEnabled()) {
455 Log.e(LOGTAG, "onReceivedError " + errorCode + " " + failingUrl
Jeff Hamilton47654f42010-09-07 09:57:51 -0500456 + " " + description);
Selim Gurun3da06b82011-10-10 13:58:12 -0700457 }
Jeff Hamilton47654f42010-09-07 09:57:51 -0500458 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700459 }
460
461 /**
462 * Check with the user if it is ok to resend POST data as the page they
463 * are trying to navigate to is the result of a POST.
464 */
465 @Override
466 public void onFormResubmission(WebView view, final Message dontResend,
467 final Message resend) {
468 if (!mInForeground) {
469 dontResend.sendToTarget();
470 return;
471 }
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500472 if (mDontResend != null) {
473 Log.w(LOGTAG, "onFormResubmission should not be called again "
474 + "while dialog is still up");
475 dontResend.sendToTarget();
476 return;
477 }
478 mDontResend = dontResend;
479 mResend = resend;
Michael Kolb14612442011-06-24 13:06:29 -0700480 new AlertDialog.Builder(mContext).setTitle(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700481 R.string.browserFrameFormResubmitLabel).setMessage(
482 R.string.browserFrameFormResubmitMessage)
483 .setPositiveButton(R.string.ok,
484 new DialogInterface.OnClickListener() {
485 public void onClick(DialogInterface dialog,
486 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500487 if (mResend != null) {
488 mResend.sendToTarget();
489 mResend = null;
490 mDontResend = null;
491 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700492 }
493 }).setNegativeButton(R.string.cancel,
494 new DialogInterface.OnClickListener() {
495 public void onClick(DialogInterface dialog,
496 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500497 if (mDontResend != null) {
498 mDontResend.sendToTarget();
499 mResend = null;
500 mDontResend = null;
501 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700502 }
503 }).setOnCancelListener(new OnCancelListener() {
504 public void onCancel(DialogInterface dialog) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500505 if (mDontResend != null) {
506 mDontResend.sendToTarget();
507 mResend = null;
508 mDontResend = null;
509 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700510 }
511 }).show();
512 }
513
514 /**
515 * Insert the url into the visited history database.
516 * @param url The url to be inserted.
517 * @param isReload True if this url is being reloaded.
518 * FIXME: Not sure what to do when reloading the page.
519 */
520 @Override
521 public void doUpdateVisitedHistory(WebView view, String url,
522 boolean isReload) {
John Reck324d4402011-01-11 16:56:42 -0800523 mWebViewController.doUpdateVisitedHistory(Tab.this, isReload);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700524 }
525
526 /**
527 * Displays SSL error(s) dialog to the user.
528 */
529 @Override
530 public void onReceivedSslError(final WebView view,
531 final SslErrorHandler handler, final SslError error) {
532 if (!mInForeground) {
533 handler.cancel();
Steve Block2466eff2011-10-03 15:33:09 +0100534 setSecurityState(SecurityState.SECURITY_STATE_NOT_SECURE);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700535 return;
536 }
John Reck35e9dd62011-04-25 09:01:54 -0700537 if (mSettings.showSecurityWarnings()) {
John Reckcb28b2c2011-08-26 17:39:44 -0700538 new AlertDialog.Builder(mContext)
539 .setTitle(R.string.security_warning)
540 .setMessage(R.string.ssl_warnings_header)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200541 .setIconAttribute(android.R.attr.alertDialogIcon)
John Reckcb28b2c2011-08-26 17:39:44 -0700542 .setPositiveButton(R.string.ssl_continue,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700543 new DialogInterface.OnClickListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700544 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700545 public void onClick(DialogInterface dialog,
546 int whichButton) {
547 handler.proceed();
Steve Block4895b012011-10-03 16:26:46 +0100548 handleProceededAfterSslError(error);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700549 }
John Reckcb28b2c2011-08-26 17:39:44 -0700550 })
551 .setNeutralButton(R.string.view_certificate,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700552 new DialogInterface.OnClickListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700553 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700554 public void onClick(DialogInterface dialog,
555 int whichButton) {
John Reckcb28b2c2011-08-26 17:39:44 -0700556 mWebViewController.showSslCertificateOnError(
557 view, handler, error);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700558 }
John Reckcb28b2c2011-08-26 17:39:44 -0700559 })
560 .setNegativeButton(R.string.ssl_go_back,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700561 new DialogInterface.OnClickListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700562 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700563 public void onClick(DialogInterface dialog,
564 int whichButton) {
John Reck30c714c2010-12-16 17:30:34 -0800565 dialog.cancel();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700566 }
John Reckcb28b2c2011-08-26 17:39:44 -0700567 })
568 .setOnCancelListener(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700569 new DialogInterface.OnCancelListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700570 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700571 public void onCancel(DialogInterface dialog) {
572 handler.cancel();
Steve Block2466eff2011-10-03 15:33:09 +0100573 setSecurityState(SecurityState.SECURITY_STATE_NOT_SECURE);
John Reck30c714c2010-12-16 17:30:34 -0800574 mWebViewController.onUserCanceledSsl(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700575 }
John Reckcb28b2c2011-08-26 17:39:44 -0700576 })
577 .show();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700578 } else {
579 handler.proceed();
580 }
581 }
582
583 /**
Steve Block4895b012011-10-03 16:26:46 +0100584 * Called when an SSL error occurred while loading a resource, but the
585 * WebView but chose to proceed anyway based on a decision retained
586 * from a previous response to onReceivedSslError(). We update our
587 * security state to reflect this.
588 */
589 @Override
590 public void onProceededAfterSslError(WebView view, SslError error) {
591 handleProceededAfterSslError(error);
592 }
593
594 /**
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700595 * Displays client certificate request to the user.
596 */
597 @Override
598 public void onReceivedClientCertRequest(final WebView view,
599 final ClientCertRequestHandler handler, final String host_and_port) {
600 if (!mInForeground) {
601 handler.ignore();
602 return;
603 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700604 int colon = host_and_port.lastIndexOf(':');
605 String host;
606 int port;
607 if (colon == -1) {
608 host = host_and_port;
609 port = -1;
610 } else {
611 String portString = host_and_port.substring(colon + 1);
612 try {
613 port = Integer.parseInt(portString);
614 host = host_and_port.substring(0, colon);
615 } catch (NumberFormatException e) {
616 host = host_and_port;
617 port = -1;
618 }
619 }
Michael Kolb14612442011-06-24 13:06:29 -0700620 KeyChain.choosePrivateKeyAlias(
621 mWebViewController.getActivity(), new KeyChainAliasCallback() {
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700622 @Override public void alias(String alias) {
623 if (alias == null) {
624 handler.cancel();
625 return;
626 }
Michael Kolb14612442011-06-24 13:06:29 -0700627 new KeyChainLookup(mContext, handler, alias).execute();
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700628 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700629 }, null, null, host, port, null);
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700630 }
631
632 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700633 * Handles an HTTP authentication request.
634 *
635 * @param handler The authentication handler
636 * @param host The host
637 * @param realm The realm
638 */
639 @Override
640 public void onReceivedHttpAuthRequest(WebView view,
641 final HttpAuthHandler handler, final String host,
642 final String realm) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700643 mWebViewController.onReceivedHttpAuthRequest(Tab.this, view, handler, host, realm);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700644 }
645
646 @Override
John Reck438bf462011-01-12 18:11:46 -0800647 public WebResourceResponse shouldInterceptRequest(WebView view,
648 String url) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800649 //intercept if opening a new incognito tab - show the incognito welcome page
Vivek Sekhared791da2015-02-22 12:39:05 -0800650 if (url.startsWith("chrome://incognito")) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800651 Resources resourceHandle = mContext.getResources();
652 InputStream inStream = resourceHandle.openRawResource(
Bijan Amirzada41242f22014-03-21 12:12:18 -0700653 com.android.browser.R.raw.incognito_mode_start_page);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800654 return new WebResourceResponse("text/html", "utf8", inStream);
655 }
kaiyiz6e5b3e02013-08-19 20:02:01 +0800656 WebResourceResponse res;
657 if (MyNavigationUtil.MY_NAVIGATION.equals(url)) {
658 res = MyNavigationProvider.shouldInterceptRequest(mContext, url);
659 } else {
660 res = HomeProvider.shouldInterceptRequest(mContext, url);
661 }
John Reck438bf462011-01-12 18:11:46 -0800662 return res;
663 }
664
665 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700666 public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
667 if (!mInForeground) {
668 return false;
669 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700670 return mWebViewController.shouldOverrideKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700671 }
672
673 @Override
674 public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700675 if (!mInForeground) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700676 return;
677 }
John Reck997b1b72012-04-19 18:08:25 -0700678 if (!mWebViewController.onUnhandledKeyEvent(event)) {
679 super.onUnhandledKeyEvent(view, event);
680 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700681 }
682 };
683
John Reck1cf4b792011-07-26 10:22:22 -0700684 private void syncCurrentState(WebView view, String url) {
685 // Sync state (in case of stop/timeout)
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800686
687 if (mReceivedError) {
688 mCurrentState.mUrl = url;
689 mCurrentState.mOriginalUrl = url;
690 } else {
691 mCurrentState.mUrl = view.getUrl();
692 mCurrentState.mOriginalUrl = view.getOriginalUrl();
693 mCurrentState.mFavicon = view.getFavicon();
694 }
695
John Reck1cf4b792011-07-26 10:22:22 -0700696 if (mCurrentState.mUrl == null) {
John Reck8bcafc12011-08-29 16:43:02 -0700697 mCurrentState.mUrl = "";
John Reck1cf4b792011-07-26 10:22:22 -0700698 }
John Reck1cf4b792011-07-26 10:22:22 -0700699 mCurrentState.mTitle = view.getTitle();
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800700
701
John Reck1cf4b792011-07-26 10:22:22 -0700702 if (!URLUtil.isHttpsUrl(mCurrentState.mUrl)) {
703 // In case we stop when loading an HTTPS page from an HTTP page
704 // but before a provisional load occurred
Steve Block2466eff2011-10-03 15:33:09 +0100705 mCurrentState.mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
Steve Block08a6f0c2011-10-06 12:12:53 +0100706 mCurrentState.mSslCertificateError = null;
John Reck1cf4b792011-07-26 10:22:22 -0700707 }
John Reck502a3532011-08-16 14:21:46 -0700708 mCurrentState.mIncognito = view.isPrivateBrowsingEnabled();
John Reck1cf4b792011-07-26 10:22:22 -0700709 }
710
Tarun Nainani8eb00912014-07-17 12:28:32 -0700711
712 public boolean isTabFullScreen() {
713 return mFullScreen;
714 }
715
Vivek Sekharf96064b2014-07-28 16:32:34 -0700716 protected void setTabFullscreen(boolean fullScreen) {
Tarun Nainani8eb00912014-07-17 12:28:32 -0700717 Controller controller = (Controller)mWebViewController;
Sudheer Koganti24766882014-10-02 10:58:09 -0700718 controller.getUi().showFullscreen(fullScreen);
Tarun Nainani8eb00912014-07-17 12:28:32 -0700719 mFullScreen = fullScreen;
Vivek Sekharf96064b2014-07-28 16:32:34 -0700720 }
721
Sudheer Koganti24766882014-10-02 10:58:09 -0700722 public boolean exitFullscreen() {
723 if (mFullScreen) {
724 Controller controller = (Controller)mWebViewController;
725 controller.getUi().showFullscreen(false);
726 if (getWebView() != null)
727 getWebView().exitFullscreen();
728 mFullScreen = false;
729 return true;
730 }
731 return false;
732 }
733
734
735
736
Grace Kloba22ac16e2009-10-07 18:00:23 -0700737 // -------------------------------------------------------------------------
738 // WebChromeClient implementation for the main WebView
739 // -------------------------------------------------------------------------
740
741 private final WebChromeClient mWebChromeClient = new WebChromeClient() {
742 // Helper method to create a new tab or sub window.
743 private void createWindow(final boolean dialog, final Message msg) {
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700744 this.createWindow(dialog, msg, null, false);
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700745 }
746
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700747 private void createWindow(final boolean dialog, final Message msg, final String url,
748 final boolean opener_suppressed) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700749 WebView.WebViewTransport transport =
750 (WebView.WebViewTransport) msg.obj;
751 if (dialog) {
752 createSubWindow();
Michael Kolb8233fac2010-10-26 16:08:53 -0700753 mWebViewController.attachSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700754 transport.setWebView(mSubView);
755 } else {
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700756 final Tab newTab = mWebViewController.openTab(url,
John Reck5949c662011-05-27 09:52:29 -0700757 Tab.this, true, true);
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700758 // This is special case for rendering links on a webpage in
759 // a new tab. If opener is suppressed, the WebContents created
760 // by the content layer are not fully initialized. This check
761 // will prevent content layer from overriding WebContents
762 // created by new tab with the uninitialized instance.
763 if (!opener_suppressed) {
764 transport.setWebView(newTab.getWebView());
765 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700766 }
767 msg.sendToTarget();
768 }
769
770 @Override
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700771 public void toggleFullscreenModeForTab(boolean enterFullscreen) {
772 if (mWebViewController instanceof Controller) {
Vivek Sekharf96064b2014-07-28 16:32:34 -0700773 setTabFullscreen(enterFullscreen);
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700774 }
775 }
776
777 @Override
Tarun Nainani8eb00912014-07-17 12:28:32 -0700778 public void onOffsetsForFullscreenChanged(float topControlsOffsetYPix,
779 float contentOffsetYPix,
780 float overdrawBottomHeightPix) {
781 if (mWebViewController instanceof Controller) {
Vivek Sekhar8ee3abb2014-07-14 12:32:05 -0700782 Controller controller = (Controller)mWebViewController;
Tarun Nainani8eb00912014-07-17 12:28:32 -0700783 controller.getUi().translateTitleBar(topControlsOffsetYPix);
Vivek Sekhar8ee3abb2014-07-14 12:32:05 -0700784 }
785 }
786
787 @Override
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700788 public boolean isTabFullScreen() {
Vivek Sekharf96064b2014-07-28 16:32:34 -0700789 return mFullScreen;
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700790 }
791
792 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700793 public boolean onCreateWindow(WebView view, final boolean dialog,
794 final boolean userGesture, final Message resultMsg) {
795 // only allow new window or sub window for the foreground case
796 if (!mInForeground) {
797 return false;
798 }
799 // Short-circuit if we can't create any more tabs or sub windows.
800 if (dialog && mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -0700801 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700802 .setTitle(R.string.too_many_subwindows_dialog_title)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200803 .setIconAttribute(android.R.attr.alertDialogIcon)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700804 .setMessage(R.string.too_many_subwindows_dialog_message)
805 .setPositiveButton(R.string.ok, null)
806 .show();
807 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700808 } else if (!mWebViewController.getTabControl().canCreateNewTab()) {
Michael Kolb14612442011-06-24 13:06:29 -0700809 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700810 .setTitle(R.string.too_many_windows_dialog_title)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200811 .setIconAttribute(android.R.attr.alertDialogIcon)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700812 .setMessage(R.string.too_many_windows_dialog_message)
813 .setPositiveButton(R.string.ok, null)
814 .show();
815 return false;
816 }
817
818 // Short-circuit if this was a user gesture.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800819 if (userGesture || !mSettings.blockPopupWindows()) {
Tarun Nainani4f5137d2015-04-16 17:26:18 -0700820 WebView.WebViewTransport transport =
821 (WebView.WebViewTransport) resultMsg.obj;
822 CreateWindowParams windowParams = transport.getCreateWindowParams();
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700823 if (windowParams.mOpenerSuppressed) {
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700824 createWindow(dialog, resultMsg, windowParams.mURL, true);
825 // This is special case for rendering links on a webpage in
826 // a new tab. If opener is suppressed, the WebContents created
827 // by the content layer are not fully initialized. Returning false
828 // will prevent content layer from overriding WebContents
829 // created by new tab with the uninitialized instance.
830 return false;
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700831 }
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700832
833 createWindow(dialog, resultMsg);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700834 return true;
835 }
836
Tarun Nainani4f5137d2015-04-16 17:26:18 -0700837 createWindow(dialog, resultMsg);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700838 return true;
839 }
840
841 @Override
Patrick Scotteb5061b2009-11-18 15:00:30 -0500842 public void onRequestFocus(WebView view) {
843 if (!mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700844 mWebViewController.switchToTab(Tab.this);
Patrick Scotteb5061b2009-11-18 15:00:30 -0500845 }
846 }
847
848 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700849 public void onCloseWindow(WebView window) {
Michael Kolbc831b632011-05-11 09:30:34 -0700850 if (mParent != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700851 // JavaScript can only close popup window.
852 if (mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700853 mWebViewController.switchToTab(mParent);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700854 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700855 mWebViewController.closeTab(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700856 }
857 }
858
859 @Override
860 public void onProgressChanged(WebView view, int newProgress) {
John Reck30c714c2010-12-16 17:30:34 -0800861 mPageLoadProgress = newProgress;
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800862 if (newProgress == 100) {
863 mInPageLoad = false;
864 }
John Reck30c714c2010-12-16 17:30:34 -0800865 mWebViewController.onProgressChanged(Tab.this);
Michael Kolb72864272012-05-03 15:42:15 -0700866 if (mUpdateThumbnail && newProgress == 100) {
867 mUpdateThumbnail = false;
868 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700869 }
870
871 @Override
Leon Scroggins21d9b902010-03-11 09:33:11 -0500872 public void onReceivedTitle(WebView view, final String title) {
John Reck30c714c2010-12-16 17:30:34 -0800873 mCurrentState.mTitle = title;
Michael Kolb8233fac2010-10-26 16:08:53 -0700874 mWebViewController.onReceivedTitle(Tab.this, title);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700875 }
876
877 @Override
878 public void onReceivedIcon(WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -0800879 mCurrentState.mFavicon = icon;
Michael Kolb8233fac2010-10-26 16:08:53 -0700880 mWebViewController.onFavicon(Tab.this, view, icon);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700881 }
882
883 @Override
884 public void onReceivedTouchIconUrl(WebView view, String url,
885 boolean precomposed) {
Michael Kolb14612442011-06-24 13:06:29 -0700886 final ContentResolver cr = mContext.getContentResolver();
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400887 // Let precomposed icons take precedence over non-composed
888 // icons.
889 if (precomposed && mTouchIconLoader != null) {
890 mTouchIconLoader.cancel(false);
891 mTouchIconLoader = null;
892 }
893 // Have only one async task at a time.
894 if (mTouchIconLoader == null) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700895 mTouchIconLoader = new DownloadTouchIcon(Tab.this,
Michael Kolb14612442011-06-24 13:06:29 -0700896 mContext, cr, view);
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400897 mTouchIconLoader.execute(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700898 }
899 }
900
901 @Override
902 public void onShowCustomView(View view,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800903 CustomViewCallback callback) {
Michael Kolb14612442011-06-24 13:06:29 -0700904 Activity activity = mWebViewController.getActivity();
905 if (activity != null) {
906 onShowCustomView(view, activity.getRequestedOrientation(), callback);
907 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400908 }
909
910 @Override
911 public void onShowCustomView(View view, int requestedOrientation,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800912 CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700913 if (mInForeground) mWebViewController.showCustomView(Tab.this, view,
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400914 requestedOrientation, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700915 }
916
917 @Override
918 public void onHideCustomView() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700919 if (mInForeground) mWebViewController.hideCustomView();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700920 }
921
922 /**
923 * The origin has exceeded its database quota.
924 * @param url the URL that exceeded the quota
925 * @param databaseIdentifier the identifier of the database on which the
926 * transaction that caused the quota overflow was run
927 * @param currentQuota the current quota for the origin.
928 * @param estimatedSize the estimated size of the database.
929 * @param totalUsedQuota is the sum of all origins' quota.
930 * @param quotaUpdater The callback to run when a decision to allow or
931 * deny quota has been made. Don't forget to call this!
932 */
933 @Override
934 public void onExceededDatabaseQuota(String url,
935 String databaseIdentifier, long currentQuota, long estimatedSize,
936 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -0700937 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -0700938 .onExceededDatabaseQuota(url, databaseIdentifier,
939 currentQuota, estimatedSize, totalUsedQuota,
940 quotaUpdater);
941 }
942
943 /**
944 * The Application Cache has exceeded its max size.
945 * @param spaceNeeded is the amount of disk space that would be needed
946 * in order for the last appcache operation to succeed.
947 * @param totalUsedQuota is the sum of all origins' quota.
948 * @param quotaUpdater A callback to inform the WebCore thread that a
949 * new app cache size is available. This callback must always
950 * be executed at some point to ensure that the sleeping
951 * WebCore thread is woken up.
952 */
953 @Override
954 public void onReachedMaxAppCacheSize(long spaceNeeded,
955 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -0700956 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -0700957 .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota,
958 quotaUpdater);
959 }
960
961 /**
962 * Instructs the browser to show a prompt to ask the user to set the
963 * Geolocation permission state for the specified origin.
964 * @param origin The origin for which Geolocation permissions are
965 * requested.
966 * @param callback The callback to call once the user has set the
967 * Geolocation permission state.
968 */
969 @Override
970 public void onGeolocationPermissionsShowPrompt(String origin,
971 GeolocationPermissions.Callback callback) {
972 if (mInForeground) {
Grace Kloba50c241e2010-04-20 11:07:50 -0700973 getGeolocationPermissionsPrompt().show(origin, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700974 }
975 }
976
977 /**
978 * Instructs the browser to hide the Geolocation permissions prompt.
979 */
980 @Override
981 public void onGeolocationPermissionsHidePrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -0700982 if (mInForeground && mGeolocationPermissionsPrompt != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700983 mGeolocationPermissionsPrompt.hide();
984 }
985 }
986
Ben Murdoch65acc352009-11-19 18:16:04 +0000987 /* Adds a JavaScript error message to the system log and if the JS
988 * console is enabled in the about:debug options, to that console
989 * also.
Ben Murdochc42addf2010-01-28 15:19:59 +0000990 * @param consoleMessage the message object.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700991 */
992 @Override
Ben Murdochc42addf2010-01-28 15:19:59 +0000993 public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Jeff Hamilton47654f42010-09-07 09:57:51 -0500994 // Don't log console messages in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -0700995 if (isPrivateBrowsingEnabled()) return true;
Jeff Hamilton47654f42010-09-07 09:57:51 -0500996
Ben Murdochc42addf2010-01-28 15:19:59 +0000997 String message = "Console: " + consoleMessage.message() + " "
998 + consoleMessage.sourceId() + ":"
999 + consoleMessage.lineNumber();
1000
1001 switch (consoleMessage.messageLevel()) {
1002 case TIP:
1003 Log.v(CONSOLE_LOGTAG, message);
1004 break;
1005 case LOG:
1006 Log.i(CONSOLE_LOGTAG, message);
1007 break;
1008 case WARNING:
1009 Log.w(CONSOLE_LOGTAG, message);
1010 break;
1011 case ERROR:
1012 Log.e(CONSOLE_LOGTAG, message);
1013 break;
1014 case DEBUG:
1015 Log.d(CONSOLE_LOGTAG, message);
1016 break;
1017 }
1018
1019 return true;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001020 }
1021
1022 /**
1023 * Ask the browser for an icon to represent a <video> element.
1024 * This icon will be used if the Web page did not specify a poster attribute.
1025 * @return Bitmap The icon or null if no such icon is available.
1026 */
1027 @Override
1028 public Bitmap getDefaultVideoPoster() {
1029 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001030 return mWebViewController.getDefaultVideoPoster();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001031 }
1032 return null;
1033 }
1034
1035 /**
1036 * Ask the host application for a custom progress view to show while
1037 * a <video> is loading.
1038 * @return View The progress view.
1039 */
1040 @Override
1041 public View getVideoLoadingProgressView() {
1042 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001043 return mWebViewController.getVideoLoadingProgressView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001044 }
1045 return null;
1046 }
1047
1048 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00001049 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001050 if (mInForeground) {
Ben Murdoch8cad4132012-01-11 10:56:43 +00001051 mWebViewController.openFileChooser(uploadMsg, acceptType, capture);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001052 } else {
1053 uploadMsg.onReceiveValue(null);
1054 }
1055 }
1056
Vivek Sekharb54614f2014-05-01 19:03:37 -07001057 @Override
1058 public void showFileChooser(ValueCallback<String[]> uploadFilePaths, String acceptTypes,
1059 boolean capture) {
1060 if (mInForeground) {
1061 mWebViewController.showFileChooser(uploadFilePaths, acceptTypes, capture);
1062 } else {
1063 uploadFilePaths.onReceiveValue(null);
1064 }
1065 }
1066
Grace Kloba22ac16e2009-10-07 18:00:23 -07001067 /**
1068 * Deliver a list of already-visited URLs
1069 */
1070 @Override
1071 public void getVisitedHistory(final ValueCallback<String[]> callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001072 mWebViewController.getVisitedHistory(callback);
1073 }
Ben Murdoch8029a772010-11-16 11:58:21 +00001074
1075 @Override
1076 public void setupAutoFill(Message message) {
1077 // Prompt the user to set up their profile.
1078 final Message msg = message;
Michael Kolb14612442011-06-24 13:06:29 -07001079 AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
1080 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
Ben Murdoch1d676b62011-01-17 12:54:24 +00001081 Context.LAYOUT_INFLATER_SERVICE);
1082 final View layout = inflater.inflate(R.layout.setup_autofill_dialog, null);
1083
1084 builder.setView(layout)
1085 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1086 @Override
1087 public void onClick(DialogInterface dialog, int id) {
1088 CheckBox disableAutoFill = (CheckBox) layout.findViewById(
1089 R.id.setup_autofill_dialog_disable_autofill);
1090
1091 if (disableAutoFill.isChecked()) {
1092 // Disable autofill and show a toast with how to turn it on again.
John Reck35e9dd62011-04-25 09:01:54 -07001093 mSettings.setAutofillEnabled(false);
Michael Kolb14612442011-06-24 13:06:29 -07001094 Toast.makeText(mContext,
Ben Murdoch1d676b62011-01-17 12:54:24 +00001095 R.string.autofill_setup_dialog_negative_toast,
1096 Toast.LENGTH_LONG).show();
1097 } else {
1098 // Take user to the AutoFill profile editor. When they return,
1099 // we will send the message that we pass here which will trigger
1100 // the form to get filled out with their new profile.
1101 mWebViewController.setupAutoFill(msg);
1102 }
1103 }
1104 })
1105 .setNegativeButton(R.string.cancel, null)
1106 .show();
Ben Murdoch8029a772010-11-16 11:58:21 +00001107 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001108 };
1109
1110 // -------------------------------------------------------------------------
1111 // WebViewClient implementation for the sub window
1112 // -------------------------------------------------------------------------
1113
1114 // Subclass of WebViewClient used in subwindows to notify the main
1115 // WebViewClient of certain WebView activities.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001116 private static class SubWindowClient extends WebViewClient {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001117 // The main WebViewClient.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001118 private final WebViewClient mClient;
Michael Kolb8233fac2010-10-26 16:08:53 -07001119 private final WebViewController mController;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001120
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001121 SubWindowClient(WebViewClient client, WebViewController controller) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001122 mClient = client;
Michael Kolb8233fac2010-10-26 16:08:53 -07001123 mController = controller;
Leon Scroggins III211ba542010-04-19 13:21:13 -04001124 }
1125 @Override
1126 public void onPageStarted(WebView view, String url, Bitmap favicon) {
1127 // Unlike the others, do not call mClient's version, which would
1128 // change the progress bar. However, we do want to remove the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001129 // find or select dialog.
Michael Kolb8233fac2010-10-26 16:08:53 -07001130 mController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001131 }
1132 @Override
1133 public void doUpdateVisitedHistory(WebView view, String url,
1134 boolean isReload) {
1135 mClient.doUpdateVisitedHistory(view, url, isReload);
1136 }
1137 @Override
1138 public boolean shouldOverrideUrlLoading(WebView view, String url) {
1139 return mClient.shouldOverrideUrlLoading(view, url);
1140 }
1141 @Override
1142 public void onReceivedSslError(WebView view, SslErrorHandler handler,
1143 SslError error) {
1144 mClient.onReceivedSslError(view, handler, error);
1145 }
1146 @Override
Brian Carlstrom8862c1d2011-06-02 01:05:55 -07001147 public void onReceivedClientCertRequest(WebView view,
1148 ClientCertRequestHandler handler, String host_and_port) {
1149 mClient.onReceivedClientCertRequest(view, handler, host_and_port);
1150 }
1151 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -07001152 public void onReceivedHttpAuthRequest(WebView view,
1153 HttpAuthHandler handler, String host, String realm) {
1154 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
1155 }
1156 @Override
1157 public void onFormResubmission(WebView view, Message dontResend,
1158 Message resend) {
1159 mClient.onFormResubmission(view, dontResend, resend);
1160 }
1161 @Override
1162 public void onReceivedError(WebView view, int errorCode,
1163 String description, String failingUrl) {
1164 mClient.onReceivedError(view, errorCode, description, failingUrl);
1165 }
1166 @Override
1167 public boolean shouldOverrideKeyEvent(WebView view,
1168 android.view.KeyEvent event) {
1169 return mClient.shouldOverrideKeyEvent(view, event);
1170 }
1171 @Override
1172 public void onUnhandledKeyEvent(WebView view,
1173 android.view.KeyEvent event) {
1174 mClient.onUnhandledKeyEvent(view, event);
1175 }
1176 }
1177
1178 // -------------------------------------------------------------------------
1179 // WebChromeClient implementation for the sub window
1180 // -------------------------------------------------------------------------
1181
1182 private class SubWindowChromeClient extends WebChromeClient {
1183 // The main WebChromeClient.
1184 private final WebChromeClient mClient;
1185
1186 SubWindowChromeClient(WebChromeClient client) {
1187 mClient = client;
1188 }
1189 @Override
1190 public void onProgressChanged(WebView view, int newProgress) {
1191 mClient.onProgressChanged(view, newProgress);
1192 }
1193 @Override
1194 public boolean onCreateWindow(WebView view, boolean dialog,
1195 boolean userGesture, android.os.Message resultMsg) {
1196 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
1197 }
1198 @Override
1199 public void onCloseWindow(WebView window) {
1200 if (window != mSubView) {
1201 Log.e(LOGTAG, "Can't close the window");
1202 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001203 mWebViewController.dismissSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001204 }
1205 }
1206
1207 // -------------------------------------------------------------------------
1208
1209 // Construct a new tab
Michael Kolb7bcafde2011-05-09 13:55:59 -07001210 Tab(WebViewController wvcontroller, WebView w) {
John Reck1cf4b792011-07-26 10:22:22 -07001211 this(wvcontroller, w, null);
1212 }
1213
1214 Tab(WebViewController wvcontroller, Bundle state) {
1215 this(wvcontroller, null, state);
1216 }
1217
1218 Tab(WebViewController wvcontroller, WebView w, Bundle state) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001219 mWebViewController = wvcontroller;
Michael Kolb14612442011-06-24 13:06:29 -07001220 mContext = mWebViewController.getContext();
John Reck35e9dd62011-04-25 09:01:54 -07001221 mSettings = BrowserSettings.getInstance();
Michael Kolb14612442011-06-24 13:06:29 -07001222 mDataController = DataController.getInstance(mContext);
1223 mCurrentState = new PageState(mContext, w != null
John Reck847b5322011-04-14 17:02:18 -07001224 ? w.isPrivateBrowsingEnabled() : false);
Tarun Nainani8084c822014-06-25 13:38:06 -07001225 setTimeStamp();
Michael Kolb8233fac2010-10-26 16:08:53 -07001226 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001227 mInForeground = false;
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001228 mWebViewDestroyedByMemoryMonitor = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001229
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001230 mDownloadListener = new BrowserDownloadListener() {
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001231 public void onDownloadStart(String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001232 String contentDisposition, String mimetype, String referer,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001233 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001234 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001235 mimetype, referer, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001236 }
1237 };
1238
John Reck1cf4b792011-07-26 10:22:22 -07001239 mCaptureWidth = mContext.getResources().getDimensionPixelSize(
1240 R.dimen.tab_thumbnail_width);
1241 mCaptureHeight = mContext.getResources().getDimensionPixelSize(
1242 R.dimen.tab_thumbnail_height);
1243 updateShouldCaptureThumbnails();
1244 restoreState(state);
John Reck52be4782011-08-26 15:37:29 -07001245 if (getId() == -1) {
1246 mId = TabControl.getNextId();
1247 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001248 setWebView(w);
Michael Kolb9ef259a2011-07-12 15:33:08 -07001249 mHandler = new Handler() {
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001250 @Override
Michael Kolb9ef259a2011-07-12 15:33:08 -07001251 public void handleMessage(Message m) {
John Reck1cf4b792011-07-26 10:22:22 -07001252 switch (m.what) {
1253 case MSG_CAPTURE:
1254 capture();
1255 break;
1256 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001257 }
1258 };
John Reck1cf4b792011-07-26 10:22:22 -07001259 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001260
Michael Kolb72864272012-05-03 15:42:15 -07001261 public boolean shouldUpdateThumbnail() {
1262 return mUpdateThumbnail;
1263 }
1264
Mathew Inwoode09305e2011-09-02 12:03:26 +01001265 /**
1266 * This is used to get a new ID when the tab has been preloaded, before it is displayed and
1267 * added to TabControl. Preloaded tabs can be created before restoreInstanceState, leading
1268 * to overlapping IDs between the preloaded and restored tabs.
1269 */
1270 public void refreshIdAfterPreload() {
1271 mId = TabControl.getNextId();
1272 }
1273
John Reck1cf4b792011-07-26 10:22:22 -07001274 public void updateShouldCaptureThumbnails() {
1275 if (mWebViewController.shouldCaptureThumbnails()) {
1276 synchronized (Tab.this) {
1277 if (mCapture == null) {
1278 mCapture = Bitmap.createBitmap(mCaptureWidth, mCaptureHeight,
1279 Bitmap.Config.RGB_565);
Michael Kolbc3af0672011-08-09 10:24:41 -07001280 mCapture.eraseColor(Color.WHITE);
John Reck1cf4b792011-07-26 10:22:22 -07001281 if (mInForeground) {
1282 postCapture();
1283 }
1284 }
1285 }
1286 } else {
1287 synchronized (Tab.this) {
1288 mCapture = null;
1289 deleteThumbnail();
1290 }
1291 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001292 }
1293
Michael Kolb14612442011-06-24 13:06:29 -07001294 public void setController(WebViewController ctl) {
1295 mWebViewController = ctl;
John Reck1cf4b792011-07-26 10:22:22 -07001296 updateShouldCaptureThumbnails();
Michael Kolb14612442011-06-24 13:06:29 -07001297 }
1298
Michael Kolbc831b632011-05-11 09:30:34 -07001299 public long getId() {
1300 return mId;
1301 }
1302
Michael Kolb91911a22012-01-17 11:21:25 -08001303 void setWebView(WebView w) {
1304 setWebView(w, true);
1305 }
1306
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001307 public boolean isNativeActive(){
1308 if (mMainView == null)
1309 return false;
1310 return true;
1311 }
1312
1313 public void setTimeStamp(){
1314 Date d = new Date();
1315 timestamp = (new Timestamp(d.getTime()));
1316 }
1317
1318 public Timestamp getTimestamp() {
1319 return timestamp;
1320 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001321 /**
1322 * Sets the WebView for this tab, correctly removing the old WebView from
1323 * the container view.
1324 */
Michael Kolb91911a22012-01-17 11:21:25 -08001325 void setWebView(WebView w, boolean restore) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001326 if (mMainView == w) {
1327 return;
1328 }
Michael Kolba713ec82010-11-29 17:27:06 -08001329
Grace Kloba22ac16e2009-10-07 18:00:23 -07001330 // If the WebView is changing, the page will be reloaded, so any ongoing
1331 // Geolocation permission requests are void.
Grace Kloba50c241e2010-04-20 11:07:50 -07001332 if (mGeolocationPermissionsPrompt != null) {
1333 mGeolocationPermissionsPrompt.hide();
1334 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001335
Michael Kolba713ec82010-11-29 17:27:06 -08001336 mWebViewController.onSetWebView(this, w);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001337
John Reck1cf4b792011-07-26 10:22:22 -07001338 if (mMainView != null) {
John Reck8ee633f2011-08-09 16:00:35 -07001339 mMainView.setPictureListener(null);
John Reck1cf4b792011-07-26 10:22:22 -07001340 if (w != null) {
1341 syncCurrentState(w, null);
1342 } else {
Panos Thomasa9a5a582014-03-18 19:20:08 -07001343 mCurrentState = new PageState(mContext, mMainView.isPrivateBrowsingEnabled());
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001344
1345 if (mWebViewDestroyedByMemoryMonitor) {
1346 /*
1347 * If tab was destroyed as a result of the MemoryMonitor
1348 * then we need to restore the state properties
1349 * from the old WebView (mMainView)
1350 */
1351 syncCurrentState(mMainView, null);
1352 mWebViewDestroyedByMemoryMonitor = false;
1353 }
John Reck1cf4b792011-07-26 10:22:22 -07001354 }
1355 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001356 // set the new one
1357 mMainView = w;
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001358
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001359 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001360 if (mMainView != null) {
1361 mMainView.setWebViewClient(mWebViewClient);
1362 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001363 // Attach DownloadManager so that downloads can start in an active
1364 // or a non-active window. This can happen when going to a site that
1365 // does a redirect after a period of time. The user could have
1366 // switched to another tab while waiting for the download to start.
1367 mMainView.setDownloadListener(mDownloadListener);
John Reck8ee633f2011-08-09 16:00:35 -07001368 TabControl tc = mWebViewController.getTabControl();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001369 if (tc != null /*&& tc.getOnThumbnailUpdatedListener() != null*/) {
John Reck8ee633f2011-08-09 16:00:35 -07001370 mMainView.setPictureListener(this);
1371 }
Michael Kolb91911a22012-01-17 11:21:25 -08001372 if (restore && (mSavedState != null)) {
John Reck8b9bb8b2012-03-08 13:19:40 -08001373 restoreUserAgent();
John Reck6c2e2f32011-08-22 13:41:23 -07001374 WebBackForwardList restoredState
1375 = mMainView.restoreState(mSavedState);
1376 if (restoredState == null || restoredState.getSize() == 0) {
1377 Log.w(LOGTAG, "Failed to restore WebView state!");
1378 loadUrl(mCurrentState.mOriginalUrl, null);
1379 }
John Reck1cf4b792011-07-26 10:22:22 -07001380 mSavedState = null;
1381 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001382 }
1383 }
1384
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001385 public void destroyThroughMemoryMonitor() {
1386 mWebViewDestroyedByMemoryMonitor = true;
1387 destroy();
1388 }
1389
Grace Kloba22ac16e2009-10-07 18:00:23 -07001390 /**
1391 * Destroy the tab's main WebView and subWindow if any
1392 */
1393 void destroy() {
Tarun Nainani2c1dd7c2014-07-05 16:40:12 -07001394
Matthew Huib7f2e9c2014-04-16 11:12:37 -04001395 if (mPostponeDestroy) {
1396 mShouldDestroy = true;
1397 return;
1398 }
1399 mShouldDestroy = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001400 if (mMainView != null) {
1401 dismissSubWindow();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001402 // save the WebView to call destroy() after detach it from the tab
1403 WebView webView = mMainView;
1404 setWebView(null);
1405 webView.destroy();
1406 }
1407 }
1408
Matthew Huib7f2e9c2014-04-16 11:12:37 -04001409 private boolean mPostponeDestroy = false;
1410 private boolean mShouldDestroy = false;
1411
1412 public void postponeDestroy() {
1413 mPostponeDestroy = true;
1414 }
1415
1416 public void performPostponedDestroy() {
1417 mPostponeDestroy = false;
1418 if (mShouldDestroy) {
1419 destroy();
1420 }
1421 }
1422
Grace Kloba22ac16e2009-10-07 18:00:23 -07001423 /**
1424 * Remove the tab from the parent
1425 */
1426 void removeFromTree() {
1427 // detach the children
Michael Kolbc831b632011-05-11 09:30:34 -07001428 if (mChildren != null) {
1429 for(Tab t : mChildren) {
1430 t.setParent(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001431 }
1432 }
1433 // remove itself from the parent list
Michael Kolbc831b632011-05-11 09:30:34 -07001434 if (mParent != null) {
1435 mParent.mChildren.remove(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001436 }
Tarun Nainani8eb00912014-07-17 12:28:32 -07001437
1438 mCapture = null;
John Reck1cf4b792011-07-26 10:22:22 -07001439 deleteThumbnail();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001440 }
1441
1442 /**
1443 * Create a new subwindow unless a subwindow already exists.
1444 * @return True if a new subwindow was created. False if one already exists.
1445 */
1446 boolean createSubWindow() {
1447 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001448 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001449 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001450 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001451 mSubView.setWebChromeClient(new SubWindowChromeClient(
1452 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001453 // Set a different DownloadListener for the mSubView, since it will
1454 // just need to dismiss the mSubView, rather than close the Tab
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001455 mSubView.setDownloadListener(new BrowserDownloadListener() {
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001456 public void onDownloadStart(String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001457 String contentDisposition, String mimetype, String referer,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001458 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001459 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001460 contentDisposition, mimetype, referer, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001461 if (mSubView.copyBackForwardList().getSize() == 0) {
1462 // This subwindow was opened for the sole purpose of
1463 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001464 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001465 }
1466 }
1467 });
Michael Kolb14612442011-06-24 13:06:29 -07001468 mSubView.setOnCreateContextMenuListener(mWebViewController.getActivity());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001469 return true;
1470 }
1471 return false;
1472 }
1473
1474 /**
1475 * Dismiss the subWindow for the tab.
1476 */
1477 void dismissSubWindow() {
1478 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001479 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001480 mSubView.destroy();
1481 mSubView = null;
1482 mSubViewContainer = null;
1483 }
1484 }
1485
Grace Kloba22ac16e2009-10-07 18:00:23 -07001486
1487 /**
1488 * Set the parent tab of this tab.
1489 */
Michael Kolbc831b632011-05-11 09:30:34 -07001490 void setParent(Tab parent) {
John Reck52be4782011-08-26 15:37:29 -07001491 if (parent == this) {
1492 throw new IllegalStateException("Cannot set parent to self!");
1493 }
Michael Kolbc831b632011-05-11 09:30:34 -07001494 mParent = parent;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001495 // This tab may have been freed due to low memory. If that is the case,
Michael Kolbc831b632011-05-11 09:30:34 -07001496 // the parent tab id is already saved. If we are changing that id
Grace Kloba22ac16e2009-10-07 18:00:23 -07001497 // (most likely due to removing the parent tab) we must update the
Michael Kolbc831b632011-05-11 09:30:34 -07001498 // parent tab id in the saved Bundle.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001499 if (mSavedState != null) {
1500 if (parent == null) {
1501 mSavedState.remove(PARENTTAB);
1502 } else {
Michael Kolbc831b632011-05-11 09:30:34 -07001503 mSavedState.putLong(PARENTTAB, parent.getId());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001504 }
1505 }
John Reckb0a86db2011-05-24 14:05:58 -07001506
1507 // Sync the WebView useragent with the parent
1508 if (parent != null && mSettings.hasDesktopUseragent(parent.getWebView())
1509 != mSettings.hasDesktopUseragent(getWebView())) {
1510 mSettings.toggleDesktopUseragent(getWebView());
1511 }
John Reck52be4782011-08-26 15:37:29 -07001512
1513 if (parent != null && parent.getId() == getId()) {
1514 throw new IllegalStateException("Parent has same ID as child!");
1515 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001516 }
1517
1518 /**
Michael Kolbc831b632011-05-11 09:30:34 -07001519 * If this Tab was created through another Tab, then this method returns
1520 * that Tab.
1521 * @return the Tab parent or null
1522 */
1523 public Tab getParent() {
1524 return mParent;
1525 }
1526
1527 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001528 * When a Tab is created through the content of another Tab, then we
1529 * associate the Tabs.
1530 * @param child the Tab that was created from this Tab
1531 */
1532 void addChildTab(Tab child) {
Michael Kolbc831b632011-05-11 09:30:34 -07001533 if (mChildren == null) {
1534 mChildren = new Vector<Tab>();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001535 }
Michael Kolbc831b632011-05-11 09:30:34 -07001536 mChildren.add(child);
1537 child.setParent(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001538 }
1539
Michael Kolbc831b632011-05-11 09:30:34 -07001540 Vector<Tab> getChildren() {
1541 return mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001542 }
1543
1544 void resume() {
1545 if (mMainView != null) {
John Reck56c1fcf2011-08-17 10:15:16 -07001546 setupHwAcceleration(mMainView);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001547 mMainView.onResume();
1548 if (mSubView != null) {
1549 mSubView.onResume();
1550 }
1551 }
1552 }
1553
John Reck56c1fcf2011-08-17 10:15:16 -07001554 private void setupHwAcceleration(View web) {
1555 if (web == null) return;
1556 BrowserSettings settings = BrowserSettings.getInstance();
1557 if (settings.isHardwareAccelerated()) {
1558 web.setLayerType(View.LAYER_TYPE_NONE, null);
1559 } else {
1560 web.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
1561 }
1562 }
1563
Grace Kloba22ac16e2009-10-07 18:00:23 -07001564 void pause() {
1565 if (mMainView != null) {
1566 mMainView.onPause();
1567 if (mSubView != null) {
1568 mSubView.onPause();
1569 }
1570 }
1571 }
1572
1573 void putInForeground() {
John Reck8ee633f2011-08-09 16:00:35 -07001574 if (mInForeground) {
1575 return;
1576 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001577 mInForeground = true;
1578 resume();
Michael Kolb14612442011-06-24 13:06:29 -07001579 Activity activity = mWebViewController.getActivity();
1580 mMainView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001581 if (mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -07001582 mSubView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001583 }
1584 // Show the pending error dialog if the queue is not empty
1585 if (mQueuedErrors != null && mQueuedErrors.size() > 0) {
1586 showError(mQueuedErrors.getFirst());
1587 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001588 mWebViewController.bookmarkedStatusHasChanged(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001589 }
1590
1591 void putInBackground() {
John Reck8ee633f2011-08-09 16:00:35 -07001592 if (!mInForeground) {
1593 return;
1594 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001595 mInForeground = false;
1596 pause();
1597 mMainView.setOnCreateContextMenuListener(null);
1598 if (mSubView != null) {
1599 mSubView.setOnCreateContextMenuListener(null);
1600 }
1601 }
1602
Michael Kolb8233fac2010-10-26 16:08:53 -07001603 boolean inForeground() {
1604 return mInForeground;
1605 }
1606
Grace Kloba22ac16e2009-10-07 18:00:23 -07001607 /**
1608 * Return the top window of this tab; either the subwindow if it is not
1609 * null or the main window.
1610 * @return The top window of this tab.
1611 */
1612 WebView getTopWindow() {
1613 if (mSubView != null) {
1614 return mSubView;
1615 }
1616 return mMainView;
1617 }
1618
1619 /**
1620 * Return the main window of this tab. Note: if a tab is freed in the
1621 * background, this can return null. It is only guaranteed to be
1622 * non-null for the current tab.
1623 * @return The main WebView of this tab.
1624 */
1625 WebView getWebView() {
1626 return mMainView;
1627 }
1628
Michael Kolba713ec82010-11-29 17:27:06 -08001629 void setViewContainer(View container) {
1630 mContainer = container;
1631 }
1632
Michael Kolb8233fac2010-10-26 16:08:53 -07001633 View getViewContainer() {
1634 return mContainer;
1635 }
1636
Grace Kloba22ac16e2009-10-07 18:00:23 -07001637 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001638 * Return whether private browsing is enabled for the main window of
1639 * this tab.
1640 * @return True if private browsing is enabled.
1641 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001642 boolean isPrivateBrowsingEnabled() {
John Reck502a3532011-08-16 14:21:46 -07001643 return mCurrentState.mIncognito;
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001644 }
1645
1646 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001647 * Return the subwindow of this tab or null if there is no subwindow.
1648 * @return The subwindow of this tab or null.
1649 */
1650 WebView getSubWebView() {
1651 return mSubView;
1652 }
1653
Michael Kolb1514bb72010-11-22 09:11:48 -08001654 void setSubWebView(WebView subView) {
1655 mSubView = subView;
1656 }
1657
Michael Kolb8233fac2010-10-26 16:08:53 -07001658 View getSubViewContainer() {
1659 return mSubViewContainer;
1660 }
1661
Michael Kolb1514bb72010-11-22 09:11:48 -08001662 void setSubViewContainer(View subViewContainer) {
1663 mSubViewContainer = subViewContainer;
1664 }
1665
Grace Kloba22ac16e2009-10-07 18:00:23 -07001666 /**
1667 * @return The geolocation permissions prompt for this tab.
1668 */
1669 GeolocationPermissionsPrompt getGeolocationPermissionsPrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001670 if (mGeolocationPermissionsPrompt == null) {
1671 ViewStub stub = (ViewStub) mContainer
1672 .findViewById(R.id.geolocation_permissions_prompt);
1673 mGeolocationPermissionsPrompt = (GeolocationPermissionsPrompt) stub
1674 .inflate();
Panos Thomasb298aad2014-10-22 12:24:21 -07001675 mGeolocationPermissionsPrompt.init(mCurrentState.mIncognito);
Grace Kloba50c241e2010-04-20 11:07:50 -07001676 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001677 return mGeolocationPermissionsPrompt;
1678 }
1679
1680 /**
1681 * @return The application id string
1682 */
1683 String getAppId() {
1684 return mAppId;
1685 }
1686
1687 /**
1688 * Set the application id string
1689 * @param id
1690 */
1691 void setAppId(String id) {
1692 mAppId = id;
1693 }
1694
Michael Kolbe28b3472011-08-04 16:54:31 -07001695 boolean closeOnBack() {
1696 return mCloseOnBack;
1697 }
1698
1699 void setCloseOnBack(boolean close) {
1700 mCloseOnBack = close;
1701 }
1702
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001703 boolean getDerivedFromIntent() {
1704 return mDerivedFromIntent;
1705 }
1706
1707 void setDerivedFromIntent(boolean derived) {
1708 mDerivedFromIntent = derived;
1709 }
1710
Grace Kloba22ac16e2009-10-07 18:00:23 -07001711 String getUrl() {
John Reck324d4402011-01-11 16:56:42 -08001712 return UrlUtils.filteredUrl(mCurrentState.mUrl);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001713 }
1714
Tarun Nainani8eb00912014-07-17 12:28:32 -07001715
1716 protected void onPageFinished() {
1717 mPageFinished = true;
1718 }
1719
1720 public boolean getPageFinishedStatus() {
1721 return mPageFinished;
1722 }
1723
John Reck49a603c2011-03-03 09:33:05 -08001724 String getOriginalUrl() {
Vivek Sekhar361065a2014-11-25 15:34:35 -08001725 if (mMainView != null)
1726 return mMainView.getOriginalUrl();
John Reckdb22ec42011-06-29 11:31:24 -07001727 if (mCurrentState.mOriginalUrl == null) {
1728 return getUrl();
John Reck49a603c2011-03-03 09:33:05 -08001729 }
John Reckdb22ec42011-06-29 11:31:24 -07001730 return UrlUtils.filteredUrl(mCurrentState.mOriginalUrl);
John Reck49a603c2011-03-03 09:33:05 -08001731 }
1732
Grace Kloba22ac16e2009-10-07 18:00:23 -07001733 /**
John Reck30c714c2010-12-16 17:30:34 -08001734 * Get the title of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001735 */
1736 String getTitle() {
John Reck30c714c2010-12-16 17:30:34 -08001737 return mCurrentState.mTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001738 }
1739
1740 /**
John Reck30c714c2010-12-16 17:30:34 -08001741 * Get the favicon of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001742 */
1743 Bitmap getFavicon() {
John Reck1cf4b792011-07-26 10:22:22 -07001744 if (mCurrentState.mFavicon != null) {
1745 return mCurrentState.mFavicon;
1746 }
1747 return getDefaultFavicon(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001748 }
1749
John Recke969cc52010-12-21 17:24:43 -08001750 public boolean isBookmarkedSite() {
1751 return mCurrentState.mIsBookmarkedSite;
1752 }
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001753
Grace Kloba22ac16e2009-10-07 18:00:23 -07001754 /**
Steve Block08a6f0c2011-10-06 12:12:53 +01001755 * Sets the security state, clears the SSL certificate error and informs
1756 * the controller.
1757 */
Steve Block2466eff2011-10-03 15:33:09 +01001758 private void setSecurityState(SecurityState securityState) {
1759 mCurrentState.mSecurityState = securityState;
Steve Block08a6f0c2011-10-06 12:12:53 +01001760 mCurrentState.mSslCertificateError = null;
Steve Block2466eff2011-10-03 15:33:09 +01001761 mWebViewController.onUpdatedSecurityState(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001762 }
1763
1764 /**
Steve Block2466eff2011-10-03 15:33:09 +01001765 * @return The tab's security state.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001766 */
Steve Block2466eff2011-10-03 15:33:09 +01001767 SecurityState getSecurityState() {
1768 return mCurrentState.mSecurityState;
John Reck30c714c2010-12-16 17:30:34 -08001769 }
1770
Steve Block08a6f0c2011-10-06 12:12:53 +01001771 /**
1772 * Gets the SSL certificate error, if any, for the page's main resource.
1773 * This is only non-null when the security state is
1774 * SECURITY_STATE_BAD_CERTIFICATE.
1775 */
1776 SslError getSslCertificateError() {
1777 return mCurrentState.mSslCertificateError;
1778 }
1779
John Reck30c714c2010-12-16 17:30:34 -08001780 int getLoadProgress() {
1781 if (mInPageLoad) {
1782 return mPageLoadProgress;
1783 }
1784 return 100;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001785 }
1786
1787 /**
1788 * @return TRUE if onPageStarted is called while onPageFinished is not
1789 * called yet.
1790 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001791 boolean inPageLoad() {
1792 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001793 }
1794
Grace Kloba22ac16e2009-10-07 18:00:23 -07001795 /**
John Reck1cf4b792011-07-26 10:22:22 -07001796 * @return The Bundle with the tab's state if it can be saved, otherwise null
Grace Kloba22ac16e2009-10-07 18:00:23 -07001797 */
John Reck1cf4b792011-07-26 10:22:22 -07001798 public Bundle saveState() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001799 // If the WebView is null it means we ran low on memory and we already
1800 // stored the saved state in mSavedState.
1801 if (mMainView == null) {
John Reck1cf4b792011-07-26 10:22:22 -07001802 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001803 }
John Reck6c2e2f32011-08-22 13:41:23 -07001804
1805 if (TextUtils.isEmpty(mCurrentState.mUrl)) {
John Reck1cf4b792011-07-26 10:22:22 -07001806 return null;
John Reck24f18262011-06-17 14:47:20 -07001807 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001808
1809 mSavedState = new Bundle();
John Reck6c2e2f32011-08-22 13:41:23 -07001810 WebBackForwardList savedList = mMainView.saveState(mSavedState);
1811 if (savedList == null || savedList.getSize() == 0) {
1812 Log.w(LOGTAG, "Failed to save back/forward list for "
1813 + mCurrentState.mUrl);
1814 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001815
Michael Kolbc831b632011-05-11 09:30:34 -07001816 mSavedState.putLong(ID, mId);
John Reck30c714c2010-12-16 17:30:34 -08001817 mSavedState.putString(CURRURL, mCurrentState.mUrl);
1818 mSavedState.putString(CURRTITLE, mCurrentState.mTitle);
John Reck1cf4b792011-07-26 10:22:22 -07001819 mSavedState.putBoolean(INCOGNITO, mMainView.isPrivateBrowsingEnabled());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001820 if (mAppId != null) {
1821 mSavedState.putString(APPID, mAppId);
1822 }
Michael Kolbe28b3472011-08-04 16:54:31 -07001823 mSavedState.putBoolean(CLOSEFLAG, mCloseOnBack);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001824 // Remember the parent tab so the relationship can be restored.
Michael Kolbc831b632011-05-11 09:30:34 -07001825 if (mParent != null) {
1826 mSavedState.putLong(PARENTTAB, mParent.mId);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001827 }
John Reckb0a86db2011-05-24 14:05:58 -07001828 mSavedState.putBoolean(USERAGENT,
1829 mSettings.hasDesktopUseragent(getWebView()));
John Reck1cf4b792011-07-26 10:22:22 -07001830 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001831 }
1832
1833 /*
1834 * Restore the state of the tab.
1835 */
John Reck1cf4b792011-07-26 10:22:22 -07001836 private void restoreState(Bundle b) {
1837 mSavedState = b;
1838 if (mSavedState == null) {
1839 return;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001840 }
1841 // Restore the internal state even if the WebView fails to restore.
1842 // This will maintain the app id, original url and close-on-exit values.
Michael Kolbc831b632011-05-11 09:30:34 -07001843 mId = b.getLong(ID);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001844 mAppId = b.getString(APPID);
Michael Kolbe28b3472011-08-04 16:54:31 -07001845 mCloseOnBack = b.getBoolean(CLOSEFLAG);
John Reck8b9bb8b2012-03-08 13:19:40 -08001846 restoreUserAgent();
John Reck1cf4b792011-07-26 10:22:22 -07001847 String url = b.getString(CURRURL);
1848 String title = b.getString(CURRTITLE);
1849 boolean incognito = b.getBoolean(INCOGNITO);
1850 mCurrentState = new PageState(mContext, incognito, url, null);
1851 mCurrentState.mTitle = title;
1852 synchronized (Tab.this) {
1853 if (mCapture != null) {
John Reck4eadc342011-10-31 14:04:10 -07001854 DataController.getInstance(mContext).loadThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07001855 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001856 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001857 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04001858
John Reck8b9bb8b2012-03-08 13:19:40 -08001859 private void restoreUserAgent() {
1860 if (mMainView == null || mSavedState == null) {
1861 return;
1862 }
1863 if (mSavedState.getBoolean(USERAGENT)
1864 != mSettings.hasDesktopUseragent(mMainView)) {
1865 mSettings.toggleDesktopUseragent(mMainView);
1866 }
1867 }
1868
Leon Scroggins1961ed22010-12-07 15:22:21 -05001869 public void updateBookmarkedStatus() {
John Recke969cc52010-12-21 17:24:43 -08001870 mDataController.queryBookmarkStatus(getUrl(), mIsBookmarkCallback);
Leon Scroggins1961ed22010-12-07 15:22:21 -05001871 }
1872
John Recke969cc52010-12-21 17:24:43 -08001873 private DataController.OnQueryUrlIsBookmark mIsBookmarkCallback
1874 = new DataController.OnQueryUrlIsBookmark() {
1875 @Override
1876 public void onQueryUrlIsBookmark(String url, boolean isBookmark) {
1877 if (mCurrentState.mUrl.equals(url)) {
1878 mCurrentState.mIsBookmarkedSite = isBookmark;
1879 mWebViewController.bookmarkedStatusHasChanged(Tab.this);
1880 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001881 }
John Recke969cc52010-12-21 17:24:43 -08001882 };
Michael Kolb1acef692011-03-08 14:12:06 -08001883
Michael Kolbeb95db42011-03-03 10:38:40 -08001884 public Bitmap getScreenshot() {
John Reck1cf4b792011-07-26 10:22:22 -07001885 synchronized (Tab.this) {
1886 return mCapture;
1887 }
Michael Kolbeb95db42011-03-03 10:38:40 -08001888 }
1889
John Reck541f55a2011-06-07 16:34:43 -07001890 public boolean isSnapshot() {
John Reck541f55a2011-06-07 16:34:43 -07001891 return false;
1892 }
1893
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001894 private static class SaveCallback implements ValueCallback<String> {
1895 boolean onReceiveValueCalled = false;
1896 private String mPath;
John Reck68234a92012-04-19 15:27:12 -07001897
1898 @Override
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001899 public void onReceiveValue(String path) {
1900 this.onReceiveValueCalled = true;
1901 this.mPath = path;
John Reck68234a92012-04-19 15:27:12 -07001902 synchronized (this) {
1903 notifyAll();
John Reck8cc92352011-07-06 17:41:52 -07001904 }
John Reck541f55a2011-06-07 16:34:43 -07001905 }
John Reck68234a92012-04-19 15:27:12 -07001906
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001907 public String getPath() {
1908 return mPath;
1909 }
John Reck68234a92012-04-19 15:27:12 -07001910 }
1911
1912 /**
1913 * Must be called on the UI thread
1914 */
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001915 public ContentValues createSnapshotValues(Bitmap bm) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001916 WebView web = getWebView();
John Reck68234a92012-04-19 15:27:12 -07001917 if (web == null) return null;
John Reckd8c74522011-06-14 08:45:00 -07001918 ContentValues values = new ContentValues();
1919 values.put(Snapshots.TITLE, mCurrentState.mTitle);
1920 values.put(Snapshots.URL, mCurrentState.mUrl);
John Reck68234a92012-04-19 15:27:12 -07001921 values.put(Snapshots.BACKGROUND, web.getPageBackgroundColor());
John Reck8cc92352011-07-06 17:41:52 -07001922 values.put(Snapshots.DATE_CREATED, System.currentTimeMillis());
1923 values.put(Snapshots.FAVICON, compressBitmap(getFavicon()));
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001924 values.put(Snapshots.THUMBNAIL, compressBitmap(bm));
John Reckd8c74522011-06-14 08:45:00 -07001925 return values;
John Reck541f55a2011-06-07 16:34:43 -07001926 }
1927
John Reck68234a92012-04-19 15:27:12 -07001928 /**
1929 * Probably want to call this on a background thread
1930 */
1931 public boolean saveViewState(ContentValues values) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001932 WebView web = getWebView();
John Reck68234a92012-04-19 15:27:12 -07001933 if (web == null) return false;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001934 String filename = UUID.randomUUID().toString();
John Reck68234a92012-04-19 15:27:12 -07001935 SaveCallback callback = new SaveCallback();
John Reck68234a92012-04-19 15:27:12 -07001936 try {
John Reck68234a92012-04-19 15:27:12 -07001937 synchronized (callback) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001938 web.saveViewState(filename, callback);
1939 callback.wait();
John Reck68234a92012-04-19 15:27:12 -07001940 }
John Reck68234a92012-04-19 15:27:12 -07001941 } catch (Exception e) {
1942 Log.w(LOGTAG, "Failed to save view state", e);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001943 String path = callback.getPath();
1944 if (path != null) {
1945 File file = mContext.getFileStreamPath(path);
1946 if (file.exists() && !file.delete()) {
1947 file.deleteOnExit();
1948 }
John Reck68234a92012-04-19 15:27:12 -07001949 }
1950 return false;
1951 }
Tarun Nainani8eb00912014-07-17 12:28:32 -07001952
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001953 String path = callback.getPath();
Tarun Nainani8eb00912014-07-17 12:28:32 -07001954 // could be that saving of file failed
1955 if (path == null) {
1956 return false;
1957 }
1958
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001959 File savedFile = new File(path);
1960 if (!savedFile.exists()) {
1961 return false;
John Reck68234a92012-04-19 15:27:12 -07001962 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001963 values.put(Snapshots.VIEWSTATE_PATH, path.substring(path.lastIndexOf('/') + 1));
1964 values.put(Snapshots.VIEWSTATE_SIZE, savedFile.length());
John Reck68234a92012-04-19 15:27:12 -07001965 return true;
1966 }
1967
John Reck8cc92352011-07-06 17:41:52 -07001968 public byte[] compressBitmap(Bitmap bitmap) {
1969 if (bitmap == null) {
1970 return null;
1971 }
1972 ByteArrayOutputStream stream = new ByteArrayOutputStream();
1973 bitmap.compress(CompressFormat.PNG, 100, stream);
1974 return stream.toByteArray();
1975 }
1976
John Reck26b18322011-06-21 13:08:58 -07001977 public void loadUrl(String url, Map<String, String> headers) {
1978 if (mMainView != null) {
Michael Kolba53c9892011-10-05 13:31:40 -07001979 mPageLoadProgress = INITIAL_PROGRESS;
Michael Kolb14612442011-06-24 13:06:29 -07001980 mCurrentState = new PageState(mContext, false, url, null);
John Reck26b18322011-06-21 13:08:58 -07001981 mMainView.loadUrl(url, headers);
1982 }
1983 }
1984
John Reck38b39652012-06-05 09:22:59 -07001985 public void disableUrlOverridingForLoad() {
1986 mDisableOverrideUrlLoading = true;
1987 }
1988
Michael Kolb9ef259a2011-07-12 15:33:08 -07001989 protected void capture() {
1990 if (mMainView == null || mCapture == null) return;
John Reck4eadc342011-10-31 14:04:10 -07001991 if (mMainView.getContentWidth() <= 0 || mMainView.getContentHeight() <= 0) {
1992 return;
1993 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07001994
Vivek Sekharc70ae632015-04-08 17:54:05 -07001995 if (!mFirstVisualPixelPainted || mMainView.isShowingCrashView()) {
Pankaj Garg79878492015-04-01 14:48:21 -07001996 mCapture = Bitmap.createBitmap(
1997 mCaptureWidth,
1998 mCaptureHeight,
1999 Bitmap.Config.RGB_565);
2000 mCapture.eraseColor(Color.WHITE);
2001
2002 mHandler.removeMessages(MSG_CAPTURE);
2003
2004 TabControl tc = mWebViewController.getTabControl();
2005 if (tc != null) {
2006 OnThumbnailUpdatedListener updateListener
2007 = tc.getOnThumbnailUpdatedListener();
2008 if (updateListener != null) {
2009 updateListener.onThumbnailUpdated(this);
2010 }
2011 }
2012 return;
2013 }
2014
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002015 mMainView
2016 .getContentBitmapAsync(
2017 (float) mCaptureWidth / mMainView.getWidth(),
2018 new Rect(),
2019 new ValueCallback<Bitmap>() {
2020 @Override
2021 public void onReceiveValue(Bitmap bitmap) {
2022 onCaptureCallback(bitmap);
2023 }});
2024 }
2025
2026 private void onCaptureCallback(Bitmap bitmap) {
2027 if (mCapture == null || bitmap == null)
2028 return;
2029
Michael Kolb9ef259a2011-07-12 15:33:08 -07002030 Canvas c = new Canvas(mCapture);
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002031 mCapture.eraseColor(Color.WHITE);
2032 c.drawBitmap(bitmap, 0, 0, null);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002033
Michael Kolba3194d02011-09-07 11:23:51 -07002034 // manually anti-alias the edges for the tilt
2035 c.drawRect(0, 0, 1, mCapture.getHeight(), sAlphaPaint);
2036 c.drawRect(mCapture.getWidth() - 1, 0, mCapture.getWidth(),
2037 mCapture.getHeight(), sAlphaPaint);
2038 c.drawRect(0, 0, mCapture.getWidth(), 1, sAlphaPaint);
2039 c.drawRect(0, mCapture.getHeight() - 1, mCapture.getWidth(),
2040 mCapture.getHeight(), sAlphaPaint);
Dianne Hackborn43cfe8a2011-08-02 16:59:35 -07002041 c.setBitmap(null);
John Reck8ee633f2011-08-09 16:00:35 -07002042 mHandler.removeMessages(MSG_CAPTURE);
John Reck1cf4b792011-07-26 10:22:22 -07002043 persistThumbnail();
John Reck8ee633f2011-08-09 16:00:35 -07002044 TabControl tc = mWebViewController.getTabControl();
2045 if (tc != null) {
2046 OnThumbnailUpdatedListener updateListener
2047 = tc.getOnThumbnailUpdatedListener();
2048 if (updateListener != null) {
2049 updateListener.onThumbnailUpdated(this);
2050 }
2051 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07002052 }
2053
2054 @Override
2055 public void onNewPicture(WebView view, Picture picture) {
John Reck1cf4b792011-07-26 10:22:22 -07002056 postCapture();
2057 }
2058
2059 private void postCapture() {
Michael Kolb9ef259a2011-07-12 15:33:08 -07002060 if (!mHandler.hasMessages(MSG_CAPTURE)) {
2061 mHandler.sendEmptyMessageDelayed(MSG_CAPTURE, CAPTURE_DELAY);
2062 }
2063 }
2064
John Reckef654f12011-07-12 16:42:08 -07002065 public boolean canGoBack() {
2066 return mMainView != null ? mMainView.canGoBack() : false;
2067 }
2068
2069 public boolean canGoForward() {
2070 return mMainView != null ? mMainView.canGoForward() : false;
2071 }
2072
2073 public void goBack() {
2074 if (mMainView != null) {
2075 mMainView.goBack();
2076 }
2077 }
2078
2079 public void goForward() {
2080 if (mMainView != null) {
2081 mMainView.goForward();
2082 }
2083 }
2084
John Reck1cf4b792011-07-26 10:22:22 -07002085 protected void persistThumbnail() {
John Reck4eadc342011-10-31 14:04:10 -07002086 DataController.getInstance(mContext).saveThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07002087 }
2088
2089 protected void deleteThumbnail() {
John Reck4eadc342011-10-31 14:04:10 -07002090 DataController.getInstance(mContext).deleteThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07002091 }
2092
John Reck4eadc342011-10-31 14:04:10 -07002093 void updateCaptureFromBlob(byte[] blob) {
John Reck1cf4b792011-07-26 10:22:22 -07002094 synchronized (Tab.this) {
2095 if (mCapture == null) {
2096 return;
2097 }
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002098 ByteBuffer buffer = ByteBuffer.wrap(blob);
Michael Kolbd837a112011-08-09 14:00:18 -07002099 try {
2100 mCapture.copyPixelsFromBuffer(buffer);
2101 } catch (RuntimeException rex) {
2102 Log.e(LOGTAG, "Load capture has mismatched sizes; buffer: "
2103 + buffer.capacity() + " blob: " + blob.length
2104 + "capture: " + mCapture.getByteCount());
2105 throw rex;
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002106 }
John Reck1cf4b792011-07-26 10:22:22 -07002107 }
2108 }
2109
John Reck52be4782011-08-26 15:37:29 -07002110 @Override
2111 public String toString() {
2112 StringBuilder builder = new StringBuilder(100);
2113 builder.append(mId);
2114 builder.append(") has parent: ");
2115 if (getParent() != null) {
2116 builder.append("true[");
2117 builder.append(getParent().getId());
2118 builder.append("]");
2119 } else {
2120 builder.append("false");
2121 }
2122 builder.append(", incog: ");
2123 builder.append(isPrivateBrowsingEnabled());
2124 if (!isPrivateBrowsingEnabled()) {
2125 builder.append(", title: ");
2126 builder.append(getTitle());
2127 builder.append(", url: ");
2128 builder.append(getUrl());
2129 }
2130 return builder.toString();
2131 }
2132
Steve Block4895b012011-10-03 16:26:46 +01002133 private void handleProceededAfterSslError(SslError error) {
2134 if (error.getUrl().equals(mCurrentState.mUrl)) {
2135 // The security state should currently be SECURITY_STATE_SECURE.
2136 setSecurityState(SecurityState.SECURITY_STATE_BAD_CERTIFICATE);
Steve Block08a6f0c2011-10-06 12:12:53 +01002137 mCurrentState.mSslCertificateError = error;
Steve Block4895b012011-10-03 16:26:46 +01002138 } else if (getSecurityState() == SecurityState.SECURITY_STATE_SECURE) {
Steve Block08a6f0c2011-10-06 12:12:53 +01002139 // The page's main resource is secure and this error is for a
2140 // sub-resource.
Steve Block4895b012011-10-03 16:26:46 +01002141 setSecurityState(SecurityState.SECURITY_STATE_MIXED);
2142 }
2143 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002144}