blob: fa6e37261aa1271b85797896fde5f698268856f7 [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;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800112 protected boolean hasCrashed = false;
John Reck1cf4b792011-07-26 10:22:22 -0700113
Michael Kolba3194d02011-09-07 11:23:51 -0700114 private static Paint sAlphaPaint = new Paint();
115 static {
116 sAlphaPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
117 sAlphaPaint.setColor(Color.TRANSPARENT);
118 }
119
Steve Block2466eff2011-10-03 15:33:09 +0100120 public enum SecurityState {
Steve Block4895b012011-10-03 16:26:46 +0100121 // The page's main resource does not use SSL. Note that we use this
122 // state irrespective of the SSL authentication state of sub-resources.
Steve Block2466eff2011-10-03 15:33:09 +0100123 SECURITY_STATE_NOT_SECURE,
Steve Block4895b012011-10-03 16:26:46 +0100124 // The page's main resource uses SSL and the certificate is good. The
125 // same is true of all sub-resources.
Steve Block2466eff2011-10-03 15:33:09 +0100126 SECURITY_STATE_SECURE,
Steve Block4895b012011-10-03 16:26:46 +0100127 // The page's main resource uses SSL and the certificate is good, but
128 // some sub-resources either do not use SSL or have problems with their
129 // certificates.
Steve Block2466eff2011-10-03 15:33:09 +0100130 SECURITY_STATE_MIXED,
Steve Block4895b012011-10-03 16:26:46 +0100131 // The page's main resource uses SSL but there is a problem with its
132 // certificate.
133 SECURITY_STATE_BAD_CERTIFICATE,
John Reck30c714c2010-12-16 17:30:34 -0800134 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700135
Michael Kolb14612442011-06-24 13:06:29 -0700136 Context mContext;
John Reckd8c74522011-06-14 08:45:00 -0700137 protected WebViewController mWebViewController;
Michael Kolb8233fac2010-10-26 16:08:53 -0700138
Michael Kolbc831b632011-05-11 09:30:34 -0700139 // The tab ID
John Reckd8c74522011-06-14 08:45:00 -0700140 private long mId = -1;
Michael Kolbc831b632011-05-11 09:30:34 -0700141
Grace Kloba22ac16e2009-10-07 18:00:23 -0700142 // The Geolocation permissions prompt
143 private GeolocationPermissionsPrompt mGeolocationPermissionsPrompt;
144 // Main WebView wrapper
Michael Kolba713ec82010-11-29 17:27:06 -0800145 private View mContainer;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700146 // Main WebView
147 private WebView mMainView;
148 // Subwindow container
149 private View mSubViewContainer;
150 // Subwindow WebView
151 private WebView mSubView;
152 // Saved bundle for when we are running low on memory. It contains the
153 // information needed to restore the WebView if the user goes back to the
154 // tab.
155 private Bundle mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700156 // Parent Tab. This is the Tab that created this Tab, or null if the Tab was
157 // created by the UI
Michael Kolbc831b632011-05-11 09:30:34 -0700158 private Tab mParent;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700159 // Tab that constructed by this Tab. This is used when this Tab is
160 // destroyed, it clears all mParentTab values in the children.
Michael Kolbc831b632011-05-11 09:30:34 -0700161 private Vector<Tab> mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700162 // If true, the tab is in the foreground of the current activity.
163 private boolean mInForeground;
Michael Kolb8233fac2010-10-26 16:08:53 -0700164 // If true, the tab is in page loading state (after onPageStarted,
165 // before onPageFinsihed)
166 private boolean mInPageLoad;
Tarun Nainani8eb00912014-07-17 12:28:32 -0700167 private boolean mPageFinished;
John Reck38b39652012-06-05 09:22:59 -0700168 private boolean mDisableOverrideUrlLoading;
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
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800329 protected void replaceCrashView(View view, View container) {
330 if (hasCrashed && (view == mMainView)) {
331 final FrameLayout wrapper = (FrameLayout) container.findViewById(R.id.webview_wrapper);
332 wrapper.removeAllViewsInLayout();
333 wrapper.addView(view);
334 hasCrashed = false;
335 }
Vivek Sekhar2868b8d2014-12-03 17:22:50 -0800336 mMainView.reload();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800337 }
338
339 protected void showCrashView() {
340 if (hasCrashed) {
341 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
342 Context.LAYOUT_INFLATER_SERVICE);
343 final View crashLayout = inflater.inflate(R.layout.browser_tab_crash, null);
344 final FrameLayout wrapper =
345 (FrameLayout) mContainer.findViewById(R.id.webview_wrapper);
346 wrapper.removeAllViewsInLayout();
347 wrapper.addView(crashLayout);
348 mContainer.requestFocus();
349 Button reloadBtn = (Button) crashLayout.findViewById(R.id.browser_crash_reload_btn);
350 reloadBtn.setOnClickListener(new OnClickListener() {
351 @Override
352 public void onClick(View arg0) {
353 replaceCrashView(mMainView, mContainer);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800354 }
355 });
356 }
357 }
358
Grace Kloba22ac16e2009-10-07 18:00:23 -0700359 // -------------------------------------------------------------------------
360 // WebViewClient implementation for the main WebView
361 // -------------------------------------------------------------------------
362
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800363 private final WebViewClient mWebViewClient = new WebViewClient() {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500364 private Message mDontResend;
365 private Message mResend;
Michael Kolb47bd1e42011-09-01 15:25:00 -0700366
367 private boolean providersDiffer(String url, String otherUrl) {
368 Uri uri1 = Uri.parse(url);
369 Uri uri2 = Uri.parse(otherUrl);
370 return !uri1.getEncodedAuthority().equals(uri2.getEncodedAuthority());
371 }
372
Grace Kloba22ac16e2009-10-07 18:00:23 -0700373 @Override
374 public void onPageStarted(WebView view, String url, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700375 mInPageLoad = true;
Tarun Nainani8eb00912014-07-17 12:28:32 -0700376 mPageFinished = false;
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800377 mReceivedError = false;
Michael Kolb72864272012-05-03 15:42:15 -0700378 mUpdateThumbnail = true;
Michael Kolba53c9892011-10-05 13:31:40 -0700379 mPageLoadProgress = INITIAL_PROGRESS;
Michael Kolb14612442011-06-24 13:06:29 -0700380 mCurrentState = new PageState(mContext,
John Reck30c714c2010-12-16 17:30:34 -0800381 view.isPrivateBrowsingEnabled(), url, favicon);
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000382 mLoadStartTime = SystemClock.uptimeMillis();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700383
384 // If we start a touch icon load and then load a new page, we don't
385 // want to cancel the current touch icon loader. But, we do want to
386 // create a new one when the touch icon url is known.
387 if (mTouchIconLoader != null) {
388 mTouchIconLoader.mTab = null;
389 mTouchIconLoader = null;
390 }
391
Panos Thomasb298aad2014-10-22 12:24:21 -0700392 // Loading a new page voids any ongoing Geolocation permission
393 // requests.
394 if (mGeolocationPermissionsPrompt != null) {
395 mGeolocationPermissionsPrompt.dismiss();
396 }
397
Grace Kloba22ac16e2009-10-07 18:00:23 -0700398 // finally update the UI in the activity if it is in the foreground
John Reck324d4402011-01-11 16:56:42 -0800399 mWebViewController.onPageStarted(Tab.this, view, favicon);
Leon Scroggins4cd97792010-12-03 15:31:56 -0500400
John Recke969cc52010-12-21 17:24:43 -0800401 updateBookmarkedStatus();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700402 }
403
404 @Override
405 public void onPageFinished(WebView view, String url) {
John Reck38b39652012-06-05 09:22:59 -0700406 mDisableOverrideUrlLoading = false;
John Reck5b691842010-11-29 11:21:13 -0800407 if (!isPrivateBrowsingEnabled()) {
408 LogTag.logPageFinishedLoading(
409 url, SystemClock.uptimeMillis() - mLoadStartTime);
410 }
John Reck1cf4b792011-07-26 10:22:22 -0700411 syncCurrentState(view, url);
John Reck324d4402011-01-11 16:56:42 -0800412 mWebViewController.onPageFinished(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700413 }
414
415 // return true if want to hijack the url to let another app to handle it
416 @Override
417 public boolean shouldOverrideUrlLoading(WebView view, String url) {
John Reck38b39652012-06-05 09:22:59 -0700418 if (!mDisableOverrideUrlLoading && mInForeground) {
Michael Kolb18eb3772010-12-10 14:29:51 -0800419 return mWebViewController.shouldOverrideUrlLoading(Tab.this,
420 view, url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700421 } else {
422 return false;
423 }
424 }
425
Vivek Sekharb991edb2014-12-17 18:18:07 -0800426 @Override
427 public boolean shouldDownloadFavicon(WebView view, String url) {
428 return true;
429 }
430
Grace Kloba22ac16e2009-10-07 18:00:23 -0700431 /**
Steve Block2466eff2011-10-03 15:33:09 +0100432 * Updates the security state. This method is called when we discover
433 * another resource to be loaded for this page (for example,
434 * javascript). While we update the security state, we do not update
435 * the lock icon until we are done loading, as it is slightly more
436 * secure this way.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700437 */
438 @Override
439 public void onLoadResource(WebView view, String url) {
440 if (url != null && url.length() > 0) {
441 // It is only if the page claims to be secure that we may have
Steve Block2466eff2011-10-03 15:33:09 +0100442 // to update the security state:
443 if (mCurrentState.mSecurityState == SecurityState.SECURITY_STATE_SECURE) {
444 // If NOT a 'safe' url, change the state to mixed content!
Grace Kloba22ac16e2009-10-07 18:00:23 -0700445 if (!(URLUtil.isHttpsUrl(url) || URLUtil.isDataUrl(url)
446 || URLUtil.isAboutUrl(url))) {
Steve Block2466eff2011-10-03 15:33:09 +0100447 mCurrentState.mSecurityState = SecurityState.SECURITY_STATE_MIXED;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700448 }
449 }
450 }
451 }
452
453 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700454 * Show a dialog informing the user of the network error reported by
455 * WebCore if it is in the foreground.
456 */
457 @Override
458 public void onReceivedError(WebView view, int errorCode,
459 String description, String failingUrl) {
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800460 // Used for the syncCurrentState to use
461 // the failing url instead of using webview url
462 mReceivedError = true;
463
Grace Kloba22ac16e2009-10-07 18:00:23 -0700464 if (errorCode != WebViewClient.ERROR_HOST_LOOKUP &&
465 errorCode != WebViewClient.ERROR_CONNECT &&
466 errorCode != WebViewClient.ERROR_BAD_URL &&
467 errorCode != WebViewClient.ERROR_UNSUPPORTED_SCHEME &&
468 errorCode != WebViewClient.ERROR_FILE) {
469 queueError(errorCode, description);
Jeff Hamilton47654f42010-09-07 09:57:51 -0500470
Selim Gurun3da06b82011-10-10 13:58:12 -0700471 // Don't log URLs when in private browsing mode
472 if (!isPrivateBrowsingEnabled()) {
473 Log.e(LOGTAG, "onReceivedError " + errorCode + " " + failingUrl
Jeff Hamilton47654f42010-09-07 09:57:51 -0500474 + " " + description);
Selim Gurun3da06b82011-10-10 13:58:12 -0700475 }
Jeff Hamilton47654f42010-09-07 09:57:51 -0500476 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700477 }
478
479 /**
480 * Check with the user if it is ok to resend POST data as the page they
481 * are trying to navigate to is the result of a POST.
482 */
483 @Override
484 public void onFormResubmission(WebView view, final Message dontResend,
485 final Message resend) {
486 if (!mInForeground) {
487 dontResend.sendToTarget();
488 return;
489 }
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500490 if (mDontResend != null) {
491 Log.w(LOGTAG, "onFormResubmission should not be called again "
492 + "while dialog is still up");
493 dontResend.sendToTarget();
494 return;
495 }
496 mDontResend = dontResend;
497 mResend = resend;
Michael Kolb14612442011-06-24 13:06:29 -0700498 new AlertDialog.Builder(mContext).setTitle(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700499 R.string.browserFrameFormResubmitLabel).setMessage(
500 R.string.browserFrameFormResubmitMessage)
501 .setPositiveButton(R.string.ok,
502 new DialogInterface.OnClickListener() {
503 public void onClick(DialogInterface dialog,
504 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500505 if (mResend != null) {
506 mResend.sendToTarget();
507 mResend = null;
508 mDontResend = null;
509 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700510 }
511 }).setNegativeButton(R.string.cancel,
512 new DialogInterface.OnClickListener() {
513 public void onClick(DialogInterface dialog,
514 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500515 if (mDontResend != null) {
516 mDontResend.sendToTarget();
517 mResend = null;
518 mDontResend = null;
519 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700520 }
521 }).setOnCancelListener(new OnCancelListener() {
522 public void onCancel(DialogInterface dialog) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500523 if (mDontResend != null) {
524 mDontResend.sendToTarget();
525 mResend = null;
526 mDontResend = null;
527 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700528 }
529 }).show();
530 }
531
532 /**
533 * Insert the url into the visited history database.
534 * @param url The url to be inserted.
535 * @param isReload True if this url is being reloaded.
536 * FIXME: Not sure what to do when reloading the page.
537 */
538 @Override
539 public void doUpdateVisitedHistory(WebView view, String url,
540 boolean isReload) {
John Reck324d4402011-01-11 16:56:42 -0800541 mWebViewController.doUpdateVisitedHistory(Tab.this, isReload);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700542 }
543
544 /**
545 * Displays SSL error(s) dialog to the user.
546 */
547 @Override
548 public void onReceivedSslError(final WebView view,
549 final SslErrorHandler handler, final SslError error) {
550 if (!mInForeground) {
551 handler.cancel();
Steve Block2466eff2011-10-03 15:33:09 +0100552 setSecurityState(SecurityState.SECURITY_STATE_NOT_SECURE);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700553 return;
554 }
John Reck35e9dd62011-04-25 09:01:54 -0700555 if (mSettings.showSecurityWarnings()) {
John Reckcb28b2c2011-08-26 17:39:44 -0700556 new AlertDialog.Builder(mContext)
557 .setTitle(R.string.security_warning)
558 .setMessage(R.string.ssl_warnings_header)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200559 .setIconAttribute(android.R.attr.alertDialogIcon)
John Reckcb28b2c2011-08-26 17:39:44 -0700560 .setPositiveButton(R.string.ssl_continue,
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) {
565 handler.proceed();
Steve Block4895b012011-10-03 16:26:46 +0100566 handleProceededAfterSslError(error);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700567 }
John Reckcb28b2c2011-08-26 17:39:44 -0700568 })
569 .setNeutralButton(R.string.view_certificate,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700570 new DialogInterface.OnClickListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700571 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700572 public void onClick(DialogInterface dialog,
573 int whichButton) {
John Reckcb28b2c2011-08-26 17:39:44 -0700574 mWebViewController.showSslCertificateOnError(
575 view, handler, error);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700576 }
John Reckcb28b2c2011-08-26 17:39:44 -0700577 })
578 .setNegativeButton(R.string.ssl_go_back,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700579 new DialogInterface.OnClickListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700580 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700581 public void onClick(DialogInterface dialog,
582 int whichButton) {
John Reck30c714c2010-12-16 17:30:34 -0800583 dialog.cancel();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700584 }
John Reckcb28b2c2011-08-26 17:39:44 -0700585 })
586 .setOnCancelListener(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700587 new DialogInterface.OnCancelListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700588 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700589 public void onCancel(DialogInterface dialog) {
590 handler.cancel();
Steve Block2466eff2011-10-03 15:33:09 +0100591 setSecurityState(SecurityState.SECURITY_STATE_NOT_SECURE);
John Reck30c714c2010-12-16 17:30:34 -0800592 mWebViewController.onUserCanceledSsl(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700593 }
John Reckcb28b2c2011-08-26 17:39:44 -0700594 })
595 .show();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700596 } else {
597 handler.proceed();
598 }
599 }
600
601 /**
Steve Block4895b012011-10-03 16:26:46 +0100602 * Called when an SSL error occurred while loading a resource, but the
603 * WebView but chose to proceed anyway based on a decision retained
604 * from a previous response to onReceivedSslError(). We update our
605 * security state to reflect this.
606 */
607 @Override
608 public void onProceededAfterSslError(WebView view, SslError error) {
609 handleProceededAfterSslError(error);
610 }
611
612 /**
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700613 * Displays client certificate request to the user.
614 */
615 @Override
616 public void onReceivedClientCertRequest(final WebView view,
617 final ClientCertRequestHandler handler, final String host_and_port) {
618 if (!mInForeground) {
619 handler.ignore();
620 return;
621 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700622 int colon = host_and_port.lastIndexOf(':');
623 String host;
624 int port;
625 if (colon == -1) {
626 host = host_and_port;
627 port = -1;
628 } else {
629 String portString = host_and_port.substring(colon + 1);
630 try {
631 port = Integer.parseInt(portString);
632 host = host_and_port.substring(0, colon);
633 } catch (NumberFormatException e) {
634 host = host_and_port;
635 port = -1;
636 }
637 }
Michael Kolb14612442011-06-24 13:06:29 -0700638 KeyChain.choosePrivateKeyAlias(
639 mWebViewController.getActivity(), new KeyChainAliasCallback() {
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700640 @Override public void alias(String alias) {
641 if (alias == null) {
642 handler.cancel();
643 return;
644 }
Michael Kolb14612442011-06-24 13:06:29 -0700645 new KeyChainLookup(mContext, handler, alias).execute();
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700646 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700647 }, null, null, host, port, null);
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700648 }
649
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800650 @Override
651 public void onRendererCrash(WebView view, boolean crashedWhileOomProtected) {
652 Log.e(LOGTAG, "Tab Crashed");
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700653 if (mWebViewController.getTabControl().getCurrentTab() == Tab.this) {
654 hasCrashed = true;
655 showCrashView();
656 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800657 }
658
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700659 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700660 * Handles an HTTP authentication request.
661 *
662 * @param handler The authentication handler
663 * @param host The host
664 * @param realm The realm
665 */
666 @Override
667 public void onReceivedHttpAuthRequest(WebView view,
668 final HttpAuthHandler handler, final String host,
669 final String realm) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700670 mWebViewController.onReceivedHttpAuthRequest(Tab.this, view, handler, host, realm);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700671 }
672
673 @Override
John Reck438bf462011-01-12 18:11:46 -0800674 public WebResourceResponse shouldInterceptRequest(WebView view,
675 String url) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800676 //intercept if opening a new incognito tab - show the incognito welcome page
Vivek Sekhared791da2015-02-22 12:39:05 -0800677 if (url.startsWith("chrome://incognito")) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800678 Resources resourceHandle = mContext.getResources();
679 InputStream inStream = resourceHandle.openRawResource(
Bijan Amirzada41242f22014-03-21 12:12:18 -0700680 com.android.browser.R.raw.incognito_mode_start_page);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800681 return new WebResourceResponse("text/html", "utf8", inStream);
682 }
kaiyiz6e5b3e02013-08-19 20:02:01 +0800683 WebResourceResponse res;
684 if (MyNavigationUtil.MY_NAVIGATION.equals(url)) {
685 res = MyNavigationProvider.shouldInterceptRequest(mContext, url);
686 } else {
687 res = HomeProvider.shouldInterceptRequest(mContext, url);
688 }
John Reck438bf462011-01-12 18:11:46 -0800689 return res;
690 }
691
692 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700693 public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
694 if (!mInForeground) {
695 return false;
696 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700697 return mWebViewController.shouldOverrideKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700698 }
699
700 @Override
701 public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700702 if (!mInForeground) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700703 return;
704 }
John Reck997b1b72012-04-19 18:08:25 -0700705 if (!mWebViewController.onUnhandledKeyEvent(event)) {
706 super.onUnhandledKeyEvent(view, event);
707 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700708 }
Vivek Sekhared791da2015-02-22 12:39:05 -0800709 //SWE-download-file
710 @Override
711 public void onDownloadStart(String url,
712 String userAgent,
713 String contentDisposition,
714 String mimeType,
715 long contentLength) {
716 if (mDownloadListener != null) {
717 if (mDownloadListener instanceof BrowserDownloadListener) {
718 ((BrowserDownloadListener) mDownloadListener).onDownloadStart(url,
719 userAgent, contentDisposition, mimeType, contentLength);
720 } else {
721 mDownloadListener.onDownloadStart(url, userAgent,
722 contentDisposition, mimeType, contentLength);
723 }
724 }
725 }
726 //SWE-download-file
Grace Kloba22ac16e2009-10-07 18:00:23 -0700727 };
728
John Reck1cf4b792011-07-26 10:22:22 -0700729 private void syncCurrentState(WebView view, String url) {
730 // Sync state (in case of stop/timeout)
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800731
732 if (mReceivedError) {
733 mCurrentState.mUrl = url;
734 mCurrentState.mOriginalUrl = url;
735 } else {
736 mCurrentState.mUrl = view.getUrl();
737 mCurrentState.mOriginalUrl = view.getOriginalUrl();
738 mCurrentState.mFavicon = view.getFavicon();
739 }
740
John Reck1cf4b792011-07-26 10:22:22 -0700741 if (mCurrentState.mUrl == null) {
John Reck8bcafc12011-08-29 16:43:02 -0700742 mCurrentState.mUrl = "";
John Reck1cf4b792011-07-26 10:22:22 -0700743 }
John Reck1cf4b792011-07-26 10:22:22 -0700744 mCurrentState.mTitle = view.getTitle();
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800745
746
John Reck1cf4b792011-07-26 10:22:22 -0700747 if (!URLUtil.isHttpsUrl(mCurrentState.mUrl)) {
748 // In case we stop when loading an HTTPS page from an HTTP page
749 // but before a provisional load occurred
Steve Block2466eff2011-10-03 15:33:09 +0100750 mCurrentState.mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
Steve Block08a6f0c2011-10-06 12:12:53 +0100751 mCurrentState.mSslCertificateError = null;
John Reck1cf4b792011-07-26 10:22:22 -0700752 }
John Reck502a3532011-08-16 14:21:46 -0700753 mCurrentState.mIncognito = view.isPrivateBrowsingEnabled();
John Reck1cf4b792011-07-26 10:22:22 -0700754 }
755
Tarun Nainani8eb00912014-07-17 12:28:32 -0700756
757 public boolean isTabFullScreen() {
758 return mFullScreen;
759 }
760
Vivek Sekharf96064b2014-07-28 16:32:34 -0700761 protected void setTabFullscreen(boolean fullScreen) {
Tarun Nainani8eb00912014-07-17 12:28:32 -0700762 Controller controller = (Controller)mWebViewController;
Sudheer Koganti24766882014-10-02 10:58:09 -0700763 controller.getUi().showFullscreen(fullScreen);
Tarun Nainani8eb00912014-07-17 12:28:32 -0700764 mFullScreen = fullScreen;
Vivek Sekharf96064b2014-07-28 16:32:34 -0700765 }
766
Sudheer Koganti24766882014-10-02 10:58:09 -0700767 public boolean exitFullscreen() {
768 if (mFullScreen) {
769 Controller controller = (Controller)mWebViewController;
770 controller.getUi().showFullscreen(false);
771 if (getWebView() != null)
772 getWebView().exitFullscreen();
773 mFullScreen = false;
774 return true;
775 }
776 return false;
777 }
778
779
780
781
Grace Kloba22ac16e2009-10-07 18:00:23 -0700782 // -------------------------------------------------------------------------
783 // WebChromeClient implementation for the main WebView
784 // -------------------------------------------------------------------------
785
786 private final WebChromeClient mWebChromeClient = new WebChromeClient() {
787 // Helper method to create a new tab or sub window.
788 private void createWindow(final boolean dialog, final Message msg) {
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700789 this.createWindow(dialog, msg, null, false);
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700790 }
791
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700792 private void createWindow(final boolean dialog, final Message msg, final String url,
793 final boolean opener_suppressed) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700794 WebView.WebViewTransport transport =
795 (WebView.WebViewTransport) msg.obj;
796 if (dialog) {
797 createSubWindow();
Michael Kolb8233fac2010-10-26 16:08:53 -0700798 mWebViewController.attachSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700799 transport.setWebView(mSubView);
800 } else {
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700801 final Tab newTab = mWebViewController.openTab(url,
John Reck5949c662011-05-27 09:52:29 -0700802 Tab.this, true, true);
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700803 // This is special case for rendering links on a webpage in
804 // a new tab. If opener is suppressed, the WebContents created
805 // by the content layer are not fully initialized. This check
806 // will prevent content layer from overriding WebContents
807 // created by new tab with the uninitialized instance.
808 if (!opener_suppressed) {
809 transport.setWebView(newTab.getWebView());
810 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700811 }
812 msg.sendToTarget();
813 }
814
815 @Override
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700816 public void toggleFullscreenModeForTab(boolean enterFullscreen) {
817 if (mWebViewController instanceof Controller) {
Vivek Sekharf96064b2014-07-28 16:32:34 -0700818 setTabFullscreen(enterFullscreen);
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700819 }
820 }
821
822 @Override
Tarun Nainani8eb00912014-07-17 12:28:32 -0700823 public void onOffsetsForFullscreenChanged(float topControlsOffsetYPix,
824 float contentOffsetYPix,
825 float overdrawBottomHeightPix) {
826 if (mWebViewController instanceof Controller) {
Vivek Sekhar8ee3abb2014-07-14 12:32:05 -0700827 Controller controller = (Controller)mWebViewController;
Tarun Nainani8eb00912014-07-17 12:28:32 -0700828 controller.getUi().translateTitleBar(topControlsOffsetYPix);
Vivek Sekhar8ee3abb2014-07-14 12:32:05 -0700829 }
830 }
831
832 @Override
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700833 public boolean isTabFullScreen() {
Vivek Sekharf96064b2014-07-28 16:32:34 -0700834 return mFullScreen;
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700835 }
836
837 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700838 public boolean onCreateWindow(WebView view, final boolean dialog,
839 final boolean userGesture, final Message resultMsg) {
840 // only allow new window or sub window for the foreground case
841 if (!mInForeground) {
842 return false;
843 }
844 // Short-circuit if we can't create any more tabs or sub windows.
845 if (dialog && mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -0700846 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700847 .setTitle(R.string.too_many_subwindows_dialog_title)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200848 .setIconAttribute(android.R.attr.alertDialogIcon)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700849 .setMessage(R.string.too_many_subwindows_dialog_message)
850 .setPositiveButton(R.string.ok, null)
851 .show();
852 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700853 } else if (!mWebViewController.getTabControl().canCreateNewTab()) {
Michael Kolb14612442011-06-24 13:06:29 -0700854 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700855 .setTitle(R.string.too_many_windows_dialog_title)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200856 .setIconAttribute(android.R.attr.alertDialogIcon)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700857 .setMessage(R.string.too_many_windows_dialog_message)
858 .setPositiveButton(R.string.ok, null)
859 .show();
860 return false;
861 }
862
863 // Short-circuit if this was a user gesture.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800864 if (userGesture || !mSettings.blockPopupWindows()) {
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700865 CreateWindowParams windowParams = view.getCreateWindowParams();
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700866 if (windowParams.mOpenerSuppressed) {
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700867 createWindow(dialog, resultMsg, windowParams.mURL, true);
868 // This is special case for rendering links on a webpage in
869 // a new tab. If opener is suppressed, the WebContents created
870 // by the content layer are not fully initialized. Returning false
871 // will prevent content layer from overriding WebContents
872 // created by new tab with the uninitialized instance.
873 return false;
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700874 }
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700875
876 createWindow(dialog, resultMsg);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700877 return true;
878 }
879
880 // Allow the popup and create the appropriate window.
881 final AlertDialog.OnClickListener allowListener =
882 new AlertDialog.OnClickListener() {
883 public void onClick(DialogInterface d,
884 int which) {
885 createWindow(dialog, resultMsg);
886 }
887 };
888
889 // Block the popup by returning a null WebView.
890 final AlertDialog.OnClickListener blockListener =
891 new AlertDialog.OnClickListener() {
892 public void onClick(DialogInterface d, int which) {
893 resultMsg.sendToTarget();
894 }
895 };
896
897 // Build a confirmation dialog to display to the user.
898 final AlertDialog d =
Michael Kolb14612442011-06-24 13:06:29 -0700899 new AlertDialog.Builder(mContext)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200900 .setIconAttribute(android.R.attr.alertDialogIcon)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700901 .setMessage(R.string.popup_window_attempt)
902 .setPositiveButton(R.string.allow, allowListener)
903 .setNegativeButton(R.string.block, blockListener)
904 .setCancelable(false)
905 .create();
906
907 // Show the confirmation dialog.
908 d.show();
909 return true;
910 }
911
912 @Override
Patrick Scotteb5061b2009-11-18 15:00:30 -0500913 public void onRequestFocus(WebView view) {
914 if (!mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700915 mWebViewController.switchToTab(Tab.this);
Patrick Scotteb5061b2009-11-18 15:00:30 -0500916 }
917 }
918
919 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700920 public void onCloseWindow(WebView window) {
Michael Kolbc831b632011-05-11 09:30:34 -0700921 if (mParent != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700922 // JavaScript can only close popup window.
923 if (mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700924 mWebViewController.switchToTab(mParent);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700925 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700926 mWebViewController.closeTab(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700927 }
928 }
929
930 @Override
931 public void onProgressChanged(WebView view, int newProgress) {
John Reck30c714c2010-12-16 17:30:34 -0800932 mPageLoadProgress = newProgress;
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800933 if (newProgress == 100) {
934 mInPageLoad = false;
935 }
John Reck30c714c2010-12-16 17:30:34 -0800936 mWebViewController.onProgressChanged(Tab.this);
Michael Kolb72864272012-05-03 15:42:15 -0700937 if (mUpdateThumbnail && newProgress == 100) {
938 mUpdateThumbnail = false;
939 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700940 }
941
942 @Override
Leon Scroggins21d9b902010-03-11 09:33:11 -0500943 public void onReceivedTitle(WebView view, final String title) {
John Reck30c714c2010-12-16 17:30:34 -0800944 mCurrentState.mTitle = title;
Michael Kolb8233fac2010-10-26 16:08:53 -0700945 mWebViewController.onReceivedTitle(Tab.this, title);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700946 }
947
948 @Override
949 public void onReceivedIcon(WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -0800950 mCurrentState.mFavicon = icon;
Michael Kolb8233fac2010-10-26 16:08:53 -0700951 mWebViewController.onFavicon(Tab.this, view, icon);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700952 }
953
954 @Override
955 public void onReceivedTouchIconUrl(WebView view, String url,
956 boolean precomposed) {
Michael Kolb14612442011-06-24 13:06:29 -0700957 final ContentResolver cr = mContext.getContentResolver();
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400958 // Let precomposed icons take precedence over non-composed
959 // icons.
960 if (precomposed && mTouchIconLoader != null) {
961 mTouchIconLoader.cancel(false);
962 mTouchIconLoader = null;
963 }
964 // Have only one async task at a time.
965 if (mTouchIconLoader == null) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700966 mTouchIconLoader = new DownloadTouchIcon(Tab.this,
Michael Kolb14612442011-06-24 13:06:29 -0700967 mContext, cr, view);
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400968 mTouchIconLoader.execute(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700969 }
970 }
971
972 @Override
973 public void onShowCustomView(View view,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800974 CustomViewCallback callback) {
Michael Kolb14612442011-06-24 13:06:29 -0700975 Activity activity = mWebViewController.getActivity();
976 if (activity != null) {
977 onShowCustomView(view, activity.getRequestedOrientation(), callback);
978 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400979 }
980
981 @Override
982 public void onShowCustomView(View view, int requestedOrientation,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800983 CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700984 if (mInForeground) mWebViewController.showCustomView(Tab.this, view,
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400985 requestedOrientation, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700986 }
987
988 @Override
989 public void onHideCustomView() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700990 if (mInForeground) mWebViewController.hideCustomView();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700991 }
992
993 /**
994 * The origin has exceeded its database quota.
995 * @param url the URL that exceeded the quota
996 * @param databaseIdentifier the identifier of the database on which the
997 * transaction that caused the quota overflow was run
998 * @param currentQuota the current quota for the origin.
999 * @param estimatedSize the estimated size of the database.
1000 * @param totalUsedQuota is the sum of all origins' quota.
1001 * @param quotaUpdater The callback to run when a decision to allow or
1002 * deny quota has been made. Don't forget to call this!
1003 */
1004 @Override
1005 public void onExceededDatabaseQuota(String url,
1006 String databaseIdentifier, long currentQuota, long estimatedSize,
1007 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -07001008 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001009 .onExceededDatabaseQuota(url, databaseIdentifier,
1010 currentQuota, estimatedSize, totalUsedQuota,
1011 quotaUpdater);
1012 }
1013
1014 /**
1015 * The Application Cache has exceeded its max size.
1016 * @param spaceNeeded is the amount of disk space that would be needed
1017 * in order for the last appcache operation to succeed.
1018 * @param totalUsedQuota is the sum of all origins' quota.
1019 * @param quotaUpdater A callback to inform the WebCore thread that a
1020 * new app cache size is available. This callback must always
1021 * be executed at some point to ensure that the sleeping
1022 * WebCore thread is woken up.
1023 */
1024 @Override
1025 public void onReachedMaxAppCacheSize(long spaceNeeded,
1026 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -07001027 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001028 .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota,
1029 quotaUpdater);
1030 }
1031
1032 /**
1033 * Instructs the browser to show a prompt to ask the user to set the
1034 * Geolocation permission state for the specified origin.
1035 * @param origin The origin for which Geolocation permissions are
1036 * requested.
1037 * @param callback The callback to call once the user has set the
1038 * Geolocation permission state.
1039 */
1040 @Override
1041 public void onGeolocationPermissionsShowPrompt(String origin,
1042 GeolocationPermissions.Callback callback) {
1043 if (mInForeground) {
Grace Kloba50c241e2010-04-20 11:07:50 -07001044 getGeolocationPermissionsPrompt().show(origin, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001045 }
1046 }
1047
1048 /**
1049 * Instructs the browser to hide the Geolocation permissions prompt.
1050 */
1051 @Override
1052 public void onGeolocationPermissionsHidePrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001053 if (mInForeground && mGeolocationPermissionsPrompt != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001054 mGeolocationPermissionsPrompt.hide();
1055 }
1056 }
1057
Ben Murdoch65acc352009-11-19 18:16:04 +00001058 /* Adds a JavaScript error message to the system log and if the JS
1059 * console is enabled in the about:debug options, to that console
1060 * also.
Ben Murdochc42addf2010-01-28 15:19:59 +00001061 * @param consoleMessage the message object.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001062 */
1063 @Override
Ben Murdochc42addf2010-01-28 15:19:59 +00001064 public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Jeff Hamilton47654f42010-09-07 09:57:51 -05001065 // Don't log console messages in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001066 if (isPrivateBrowsingEnabled()) return true;
Jeff Hamilton47654f42010-09-07 09:57:51 -05001067
Ben Murdochc42addf2010-01-28 15:19:59 +00001068 String message = "Console: " + consoleMessage.message() + " "
1069 + consoleMessage.sourceId() + ":"
1070 + consoleMessage.lineNumber();
1071
1072 switch (consoleMessage.messageLevel()) {
1073 case TIP:
1074 Log.v(CONSOLE_LOGTAG, message);
1075 break;
1076 case LOG:
1077 Log.i(CONSOLE_LOGTAG, message);
1078 break;
1079 case WARNING:
1080 Log.w(CONSOLE_LOGTAG, message);
1081 break;
1082 case ERROR:
1083 Log.e(CONSOLE_LOGTAG, message);
1084 break;
1085 case DEBUG:
1086 Log.d(CONSOLE_LOGTAG, message);
1087 break;
1088 }
1089
1090 return true;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001091 }
1092
1093 /**
1094 * Ask the browser for an icon to represent a <video> element.
1095 * This icon will be used if the Web page did not specify a poster attribute.
1096 * @return Bitmap The icon or null if no such icon is available.
1097 */
1098 @Override
1099 public Bitmap getDefaultVideoPoster() {
1100 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001101 return mWebViewController.getDefaultVideoPoster();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001102 }
1103 return null;
1104 }
1105
1106 /**
1107 * Ask the host application for a custom progress view to show while
1108 * a <video> is loading.
1109 * @return View The progress view.
1110 */
1111 @Override
1112 public View getVideoLoadingProgressView() {
1113 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001114 return mWebViewController.getVideoLoadingProgressView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001115 }
1116 return null;
1117 }
1118
1119 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00001120 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001121 if (mInForeground) {
Ben Murdoch8cad4132012-01-11 10:56:43 +00001122 mWebViewController.openFileChooser(uploadMsg, acceptType, capture);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001123 } else {
1124 uploadMsg.onReceiveValue(null);
1125 }
1126 }
1127
Vivek Sekharb54614f2014-05-01 19:03:37 -07001128 @Override
1129 public void showFileChooser(ValueCallback<String[]> uploadFilePaths, String acceptTypes,
1130 boolean capture) {
1131 if (mInForeground) {
1132 mWebViewController.showFileChooser(uploadFilePaths, acceptTypes, capture);
1133 } else {
1134 uploadFilePaths.onReceiveValue(null);
1135 }
1136 }
1137
Grace Kloba22ac16e2009-10-07 18:00:23 -07001138 /**
1139 * Deliver a list of already-visited URLs
1140 */
1141 @Override
1142 public void getVisitedHistory(final ValueCallback<String[]> callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001143 mWebViewController.getVisitedHistory(callback);
1144 }
Ben Murdoch8029a772010-11-16 11:58:21 +00001145
1146 @Override
1147 public void setupAutoFill(Message message) {
1148 // Prompt the user to set up their profile.
1149 final Message msg = message;
Michael Kolb14612442011-06-24 13:06:29 -07001150 AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
1151 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
Ben Murdoch1d676b62011-01-17 12:54:24 +00001152 Context.LAYOUT_INFLATER_SERVICE);
1153 final View layout = inflater.inflate(R.layout.setup_autofill_dialog, null);
1154
1155 builder.setView(layout)
1156 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1157 @Override
1158 public void onClick(DialogInterface dialog, int id) {
1159 CheckBox disableAutoFill = (CheckBox) layout.findViewById(
1160 R.id.setup_autofill_dialog_disable_autofill);
1161
1162 if (disableAutoFill.isChecked()) {
1163 // Disable autofill and show a toast with how to turn it on again.
John Reck35e9dd62011-04-25 09:01:54 -07001164 mSettings.setAutofillEnabled(false);
Michael Kolb14612442011-06-24 13:06:29 -07001165 Toast.makeText(mContext,
Ben Murdoch1d676b62011-01-17 12:54:24 +00001166 R.string.autofill_setup_dialog_negative_toast,
1167 Toast.LENGTH_LONG).show();
1168 } else {
1169 // Take user to the AutoFill profile editor. When they return,
1170 // we will send the message that we pass here which will trigger
1171 // the form to get filled out with their new profile.
1172 mWebViewController.setupAutoFill(msg);
1173 }
1174 }
1175 })
1176 .setNegativeButton(R.string.cancel, null)
1177 .show();
Ben Murdoch8029a772010-11-16 11:58:21 +00001178 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001179 };
1180
1181 // -------------------------------------------------------------------------
1182 // WebViewClient implementation for the sub window
1183 // -------------------------------------------------------------------------
1184
1185 // Subclass of WebViewClient used in subwindows to notify the main
1186 // WebViewClient of certain WebView activities.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001187 private static class SubWindowClient extends WebViewClient {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001188 // The main WebViewClient.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001189 private final WebViewClient mClient;
Michael Kolb8233fac2010-10-26 16:08:53 -07001190 private final WebViewController mController;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001191
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001192 SubWindowClient(WebViewClient client, WebViewController controller) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001193 mClient = client;
Michael Kolb8233fac2010-10-26 16:08:53 -07001194 mController = controller;
Leon Scroggins III211ba542010-04-19 13:21:13 -04001195 }
1196 @Override
1197 public void onPageStarted(WebView view, String url, Bitmap favicon) {
1198 // Unlike the others, do not call mClient's version, which would
1199 // change the progress bar. However, we do want to remove the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001200 // find or select dialog.
Michael Kolb8233fac2010-10-26 16:08:53 -07001201 mController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001202 }
1203 @Override
1204 public void doUpdateVisitedHistory(WebView view, String url,
1205 boolean isReload) {
1206 mClient.doUpdateVisitedHistory(view, url, isReload);
1207 }
1208 @Override
1209 public boolean shouldOverrideUrlLoading(WebView view, String url) {
1210 return mClient.shouldOverrideUrlLoading(view, url);
1211 }
1212 @Override
1213 public void onReceivedSslError(WebView view, SslErrorHandler handler,
1214 SslError error) {
1215 mClient.onReceivedSslError(view, handler, error);
1216 }
1217 @Override
Brian Carlstrom8862c1d2011-06-02 01:05:55 -07001218 public void onReceivedClientCertRequest(WebView view,
1219 ClientCertRequestHandler handler, String host_and_port) {
1220 mClient.onReceivedClientCertRequest(view, handler, host_and_port);
1221 }
1222 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -07001223 public void onReceivedHttpAuthRequest(WebView view,
1224 HttpAuthHandler handler, String host, String realm) {
1225 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
1226 }
1227 @Override
1228 public void onFormResubmission(WebView view, Message dontResend,
1229 Message resend) {
1230 mClient.onFormResubmission(view, dontResend, resend);
1231 }
1232 @Override
1233 public void onReceivedError(WebView view, int errorCode,
1234 String description, String failingUrl) {
1235 mClient.onReceivedError(view, errorCode, description, failingUrl);
1236 }
1237 @Override
1238 public boolean shouldOverrideKeyEvent(WebView view,
1239 android.view.KeyEvent event) {
1240 return mClient.shouldOverrideKeyEvent(view, event);
1241 }
1242 @Override
1243 public void onUnhandledKeyEvent(WebView view,
1244 android.view.KeyEvent event) {
1245 mClient.onUnhandledKeyEvent(view, event);
1246 }
1247 }
1248
1249 // -------------------------------------------------------------------------
1250 // WebChromeClient implementation for the sub window
1251 // -------------------------------------------------------------------------
1252
1253 private class SubWindowChromeClient extends WebChromeClient {
1254 // The main WebChromeClient.
1255 private final WebChromeClient mClient;
1256
1257 SubWindowChromeClient(WebChromeClient client) {
1258 mClient = client;
1259 }
1260 @Override
1261 public void onProgressChanged(WebView view, int newProgress) {
1262 mClient.onProgressChanged(view, newProgress);
1263 }
1264 @Override
1265 public boolean onCreateWindow(WebView view, boolean dialog,
1266 boolean userGesture, android.os.Message resultMsg) {
1267 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
1268 }
1269 @Override
1270 public void onCloseWindow(WebView window) {
1271 if (window != mSubView) {
1272 Log.e(LOGTAG, "Can't close the window");
1273 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001274 mWebViewController.dismissSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001275 }
1276 }
1277
1278 // -------------------------------------------------------------------------
1279
1280 // Construct a new tab
Michael Kolb7bcafde2011-05-09 13:55:59 -07001281 Tab(WebViewController wvcontroller, WebView w) {
John Reck1cf4b792011-07-26 10:22:22 -07001282 this(wvcontroller, w, null);
1283 }
1284
1285 Tab(WebViewController wvcontroller, Bundle state) {
1286 this(wvcontroller, null, state);
1287 }
1288
1289 Tab(WebViewController wvcontroller, WebView w, Bundle state) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001290 mWebViewController = wvcontroller;
Michael Kolb14612442011-06-24 13:06:29 -07001291 mContext = mWebViewController.getContext();
John Reck35e9dd62011-04-25 09:01:54 -07001292 mSettings = BrowserSettings.getInstance();
Michael Kolb14612442011-06-24 13:06:29 -07001293 mDataController = DataController.getInstance(mContext);
1294 mCurrentState = new PageState(mContext, w != null
John Reck847b5322011-04-14 17:02:18 -07001295 ? w.isPrivateBrowsingEnabled() : false);
Tarun Nainani8084c822014-06-25 13:38:06 -07001296 setTimeStamp();
Michael Kolb8233fac2010-10-26 16:08:53 -07001297 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001298 mInForeground = false;
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001299 mWebViewDestroyedByMemoryMonitor = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001300
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001301 mDownloadListener = new BrowserDownloadListener() {
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001302 public void onDownloadStart(String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001303 String contentDisposition, String mimetype, String referer,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001304 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001305 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001306 mimetype, referer, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001307 }
1308 };
1309
John Reck1cf4b792011-07-26 10:22:22 -07001310 mCaptureWidth = mContext.getResources().getDimensionPixelSize(
1311 R.dimen.tab_thumbnail_width);
1312 mCaptureHeight = mContext.getResources().getDimensionPixelSize(
1313 R.dimen.tab_thumbnail_height);
1314 updateShouldCaptureThumbnails();
1315 restoreState(state);
John Reck52be4782011-08-26 15:37:29 -07001316 if (getId() == -1) {
1317 mId = TabControl.getNextId();
1318 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001319 setWebView(w);
Michael Kolb9ef259a2011-07-12 15:33:08 -07001320 mHandler = new Handler() {
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001321 @Override
Michael Kolb9ef259a2011-07-12 15:33:08 -07001322 public void handleMessage(Message m) {
John Reck1cf4b792011-07-26 10:22:22 -07001323 switch (m.what) {
1324 case MSG_CAPTURE:
1325 capture();
1326 break;
1327 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001328 }
1329 };
John Reck1cf4b792011-07-26 10:22:22 -07001330 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001331
Michael Kolb72864272012-05-03 15:42:15 -07001332 public boolean shouldUpdateThumbnail() {
1333 return mUpdateThumbnail;
1334 }
1335
Mathew Inwoode09305e2011-09-02 12:03:26 +01001336 /**
1337 * This is used to get a new ID when the tab has been preloaded, before it is displayed and
1338 * added to TabControl. Preloaded tabs can be created before restoreInstanceState, leading
1339 * to overlapping IDs between the preloaded and restored tabs.
1340 */
1341 public void refreshIdAfterPreload() {
1342 mId = TabControl.getNextId();
1343 }
1344
John Reck1cf4b792011-07-26 10:22:22 -07001345 public void updateShouldCaptureThumbnails() {
1346 if (mWebViewController.shouldCaptureThumbnails()) {
1347 synchronized (Tab.this) {
1348 if (mCapture == null) {
1349 mCapture = Bitmap.createBitmap(mCaptureWidth, mCaptureHeight,
1350 Bitmap.Config.RGB_565);
Michael Kolbc3af0672011-08-09 10:24:41 -07001351 mCapture.eraseColor(Color.WHITE);
John Reck1cf4b792011-07-26 10:22:22 -07001352 if (mInForeground) {
1353 postCapture();
1354 }
1355 }
1356 }
1357 } else {
1358 synchronized (Tab.this) {
1359 mCapture = null;
1360 deleteThumbnail();
1361 }
1362 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001363 }
1364
Michael Kolb14612442011-06-24 13:06:29 -07001365 public void setController(WebViewController ctl) {
1366 mWebViewController = ctl;
John Reck1cf4b792011-07-26 10:22:22 -07001367 updateShouldCaptureThumbnails();
Michael Kolb14612442011-06-24 13:06:29 -07001368 }
1369
Michael Kolbc831b632011-05-11 09:30:34 -07001370 public long getId() {
1371 return mId;
1372 }
1373
Michael Kolb91911a22012-01-17 11:21:25 -08001374 void setWebView(WebView w) {
1375 setWebView(w, true);
1376 }
1377
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001378 public boolean isNativeActive(){
1379 if (mMainView == null)
1380 return false;
1381 return true;
1382 }
1383
1384 public void setTimeStamp(){
1385 Date d = new Date();
1386 timestamp = (new Timestamp(d.getTime()));
1387 }
1388
1389 public Timestamp getTimestamp() {
1390 return timestamp;
1391 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001392 /**
1393 * Sets the WebView for this tab, correctly removing the old WebView from
1394 * the container view.
1395 */
Michael Kolb91911a22012-01-17 11:21:25 -08001396 void setWebView(WebView w, boolean restore) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001397 if (mMainView == w) {
1398 return;
1399 }
Michael Kolba713ec82010-11-29 17:27:06 -08001400
Grace Kloba22ac16e2009-10-07 18:00:23 -07001401 // If the WebView is changing, the page will be reloaded, so any ongoing
1402 // Geolocation permission requests are void.
Grace Kloba50c241e2010-04-20 11:07:50 -07001403 if (mGeolocationPermissionsPrompt != null) {
1404 mGeolocationPermissionsPrompt.hide();
1405 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001406
Michael Kolba713ec82010-11-29 17:27:06 -08001407 mWebViewController.onSetWebView(this, w);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001408
John Reck1cf4b792011-07-26 10:22:22 -07001409 if (mMainView != null) {
John Reck8ee633f2011-08-09 16:00:35 -07001410 mMainView.setPictureListener(null);
John Reck1cf4b792011-07-26 10:22:22 -07001411 if (w != null) {
1412 syncCurrentState(w, null);
1413 } else {
Panos Thomasa9a5a582014-03-18 19:20:08 -07001414 mCurrentState = new PageState(mContext, mMainView.isPrivateBrowsingEnabled());
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001415
1416 if (mWebViewDestroyedByMemoryMonitor) {
1417 /*
1418 * If tab was destroyed as a result of the MemoryMonitor
1419 * then we need to restore the state properties
1420 * from the old WebView (mMainView)
1421 */
1422 syncCurrentState(mMainView, null);
1423 mWebViewDestroyedByMemoryMonitor = false;
1424 }
John Reck1cf4b792011-07-26 10:22:22 -07001425 }
1426 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001427 // set the new one
1428 mMainView = w;
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001429
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001430 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001431 if (mMainView != null) {
1432 mMainView.setWebViewClient(mWebViewClient);
1433 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001434 // Attach DownloadManager so that downloads can start in an active
1435 // or a non-active window. This can happen when going to a site that
1436 // does a redirect after a period of time. The user could have
1437 // switched to another tab while waiting for the download to start.
1438 mMainView.setDownloadListener(mDownloadListener);
John Reck8ee633f2011-08-09 16:00:35 -07001439 TabControl tc = mWebViewController.getTabControl();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001440 if (tc != null /*&& tc.getOnThumbnailUpdatedListener() != null*/) {
John Reck8ee633f2011-08-09 16:00:35 -07001441 mMainView.setPictureListener(this);
1442 }
Michael Kolb91911a22012-01-17 11:21:25 -08001443 if (restore && (mSavedState != null)) {
John Reck8b9bb8b2012-03-08 13:19:40 -08001444 restoreUserAgent();
John Reck6c2e2f32011-08-22 13:41:23 -07001445 WebBackForwardList restoredState
1446 = mMainView.restoreState(mSavedState);
1447 if (restoredState == null || restoredState.getSize() == 0) {
1448 Log.w(LOGTAG, "Failed to restore WebView state!");
1449 loadUrl(mCurrentState.mOriginalUrl, null);
1450 }
John Reck1cf4b792011-07-26 10:22:22 -07001451 mSavedState = null;
1452 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001453 }
1454 }
1455
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001456 public void destroyThroughMemoryMonitor() {
1457 mWebViewDestroyedByMemoryMonitor = true;
1458 destroy();
1459 }
1460
Grace Kloba22ac16e2009-10-07 18:00:23 -07001461 /**
1462 * Destroy the tab's main WebView and subWindow if any
1463 */
1464 void destroy() {
Tarun Nainani2c1dd7c2014-07-05 16:40:12 -07001465
Matthew Huib7f2e9c2014-04-16 11:12:37 -04001466 if (mPostponeDestroy) {
1467 mShouldDestroy = true;
1468 return;
1469 }
1470 mShouldDestroy = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001471 if (mMainView != null) {
1472 dismissSubWindow();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001473 // save the WebView to call destroy() after detach it from the tab
1474 WebView webView = mMainView;
1475 setWebView(null);
1476 webView.destroy();
1477 }
1478 }
1479
Matthew Huib7f2e9c2014-04-16 11:12:37 -04001480 private boolean mPostponeDestroy = false;
1481 private boolean mShouldDestroy = false;
1482
1483 public void postponeDestroy() {
1484 mPostponeDestroy = true;
1485 }
1486
1487 public void performPostponedDestroy() {
1488 mPostponeDestroy = false;
1489 if (mShouldDestroy) {
1490 destroy();
1491 }
1492 }
1493
Grace Kloba22ac16e2009-10-07 18:00:23 -07001494 /**
1495 * Remove the tab from the parent
1496 */
1497 void removeFromTree() {
1498 // detach the children
Michael Kolbc831b632011-05-11 09:30:34 -07001499 if (mChildren != null) {
1500 for(Tab t : mChildren) {
1501 t.setParent(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001502 }
1503 }
1504 // remove itself from the parent list
Michael Kolbc831b632011-05-11 09:30:34 -07001505 if (mParent != null) {
1506 mParent.mChildren.remove(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001507 }
Tarun Nainani8eb00912014-07-17 12:28:32 -07001508
1509 mCapture = null;
John Reck1cf4b792011-07-26 10:22:22 -07001510 deleteThumbnail();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001511 }
1512
1513 /**
1514 * Create a new subwindow unless a subwindow already exists.
1515 * @return True if a new subwindow was created. False if one already exists.
1516 */
1517 boolean createSubWindow() {
1518 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001519 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001520 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001521 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001522 mSubView.setWebChromeClient(new SubWindowChromeClient(
1523 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001524 // Set a different DownloadListener for the mSubView, since it will
1525 // just need to dismiss the mSubView, rather than close the Tab
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001526 mSubView.setDownloadListener(new BrowserDownloadListener() {
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001527 public void onDownloadStart(String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001528 String contentDisposition, String mimetype, String referer,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001529 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001530 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001531 contentDisposition, mimetype, referer, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001532 if (mSubView.copyBackForwardList().getSize() == 0) {
1533 // This subwindow was opened for the sole purpose of
1534 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001535 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001536 }
1537 }
1538 });
Michael Kolb14612442011-06-24 13:06:29 -07001539 mSubView.setOnCreateContextMenuListener(mWebViewController.getActivity());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001540 return true;
1541 }
1542 return false;
1543 }
1544
1545 /**
1546 * Dismiss the subWindow for the tab.
1547 */
1548 void dismissSubWindow() {
1549 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001550 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001551 mSubView.destroy();
1552 mSubView = null;
1553 mSubViewContainer = null;
1554 }
1555 }
1556
Grace Kloba22ac16e2009-10-07 18:00:23 -07001557
1558 /**
1559 * Set the parent tab of this tab.
1560 */
Michael Kolbc831b632011-05-11 09:30:34 -07001561 void setParent(Tab parent) {
John Reck52be4782011-08-26 15:37:29 -07001562 if (parent == this) {
1563 throw new IllegalStateException("Cannot set parent to self!");
1564 }
Michael Kolbc831b632011-05-11 09:30:34 -07001565 mParent = parent;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001566 // This tab may have been freed due to low memory. If that is the case,
Michael Kolbc831b632011-05-11 09:30:34 -07001567 // the parent tab id is already saved. If we are changing that id
Grace Kloba22ac16e2009-10-07 18:00:23 -07001568 // (most likely due to removing the parent tab) we must update the
Michael Kolbc831b632011-05-11 09:30:34 -07001569 // parent tab id in the saved Bundle.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001570 if (mSavedState != null) {
1571 if (parent == null) {
1572 mSavedState.remove(PARENTTAB);
1573 } else {
Michael Kolbc831b632011-05-11 09:30:34 -07001574 mSavedState.putLong(PARENTTAB, parent.getId());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001575 }
1576 }
John Reckb0a86db2011-05-24 14:05:58 -07001577
1578 // Sync the WebView useragent with the parent
1579 if (parent != null && mSettings.hasDesktopUseragent(parent.getWebView())
1580 != mSettings.hasDesktopUseragent(getWebView())) {
1581 mSettings.toggleDesktopUseragent(getWebView());
1582 }
John Reck52be4782011-08-26 15:37:29 -07001583
1584 if (parent != null && parent.getId() == getId()) {
1585 throw new IllegalStateException("Parent has same ID as child!");
1586 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001587 }
1588
1589 /**
Michael Kolbc831b632011-05-11 09:30:34 -07001590 * If this Tab was created through another Tab, then this method returns
1591 * that Tab.
1592 * @return the Tab parent or null
1593 */
1594 public Tab getParent() {
1595 return mParent;
1596 }
1597
1598 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001599 * When a Tab is created through the content of another Tab, then we
1600 * associate the Tabs.
1601 * @param child the Tab that was created from this Tab
1602 */
1603 void addChildTab(Tab child) {
Michael Kolbc831b632011-05-11 09:30:34 -07001604 if (mChildren == null) {
1605 mChildren = new Vector<Tab>();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001606 }
Michael Kolbc831b632011-05-11 09:30:34 -07001607 mChildren.add(child);
1608 child.setParent(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001609 }
1610
Michael Kolbc831b632011-05-11 09:30:34 -07001611 Vector<Tab> getChildren() {
1612 return mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001613 }
1614
1615 void resume() {
1616 if (mMainView != null) {
Pankaj Garg60221af2014-09-23 17:57:50 -07001617 if (mMainView.hasCrashed()) {
1618 // Reload if render process has crashed. This is done here so that
1619 // setFocus call sends wasShown message to correct render process.
Vivek Sekhared791da2015-02-22 12:39:05 -08001620 mMainView.setNeedsReload(true);
Pankaj Garg60221af2014-09-23 17:57:50 -07001621 }
John Reck56c1fcf2011-08-17 10:15:16 -07001622 setupHwAcceleration(mMainView);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001623 mMainView.onResume();
1624 if (mSubView != null) {
1625 mSubView.onResume();
1626 }
1627 }
1628 }
1629
John Reck56c1fcf2011-08-17 10:15:16 -07001630 private void setupHwAcceleration(View web) {
1631 if (web == null) return;
1632 BrowserSettings settings = BrowserSettings.getInstance();
1633 if (settings.isHardwareAccelerated()) {
1634 web.setLayerType(View.LAYER_TYPE_NONE, null);
1635 } else {
1636 web.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
1637 }
1638 }
1639
Grace Kloba22ac16e2009-10-07 18:00:23 -07001640 void pause() {
1641 if (mMainView != null) {
1642 mMainView.onPause();
1643 if (mSubView != null) {
1644 mSubView.onPause();
1645 }
1646 }
1647 }
1648
1649 void putInForeground() {
John Reck8ee633f2011-08-09 16:00:35 -07001650 if (mInForeground) {
1651 return;
1652 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001653 mInForeground = true;
1654 resume();
Michael Kolb14612442011-06-24 13:06:29 -07001655 Activity activity = mWebViewController.getActivity();
1656 mMainView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001657 if (mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -07001658 mSubView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001659 }
1660 // Show the pending error dialog if the queue is not empty
1661 if (mQueuedErrors != null && mQueuedErrors.size() > 0) {
1662 showError(mQueuedErrors.getFirst());
1663 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001664 mWebViewController.bookmarkedStatusHasChanged(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001665 }
1666
1667 void putInBackground() {
John Reck8ee633f2011-08-09 16:00:35 -07001668 if (!mInForeground) {
1669 return;
1670 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001671 mInForeground = false;
1672 pause();
1673 mMainView.setOnCreateContextMenuListener(null);
1674 if (mSubView != null) {
1675 mSubView.setOnCreateContextMenuListener(null);
1676 }
1677 }
1678
Michael Kolb8233fac2010-10-26 16:08:53 -07001679 boolean inForeground() {
1680 return mInForeground;
1681 }
1682
Grace Kloba22ac16e2009-10-07 18:00:23 -07001683 /**
1684 * Return the top window of this tab; either the subwindow if it is not
1685 * null or the main window.
1686 * @return The top window of this tab.
1687 */
1688 WebView getTopWindow() {
1689 if (mSubView != null) {
1690 return mSubView;
1691 }
1692 return mMainView;
1693 }
1694
1695 /**
1696 * Return the main window of this tab. Note: if a tab is freed in the
1697 * background, this can return null. It is only guaranteed to be
1698 * non-null for the current tab.
1699 * @return The main WebView of this tab.
1700 */
1701 WebView getWebView() {
1702 return mMainView;
1703 }
1704
Michael Kolba713ec82010-11-29 17:27:06 -08001705 void setViewContainer(View container) {
1706 mContainer = container;
1707 }
1708
Michael Kolb8233fac2010-10-26 16:08:53 -07001709 View getViewContainer() {
1710 return mContainer;
1711 }
1712
Grace Kloba22ac16e2009-10-07 18:00:23 -07001713 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001714 * Return whether private browsing is enabled for the main window of
1715 * this tab.
1716 * @return True if private browsing is enabled.
1717 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001718 boolean isPrivateBrowsingEnabled() {
John Reck502a3532011-08-16 14:21:46 -07001719 return mCurrentState.mIncognito;
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001720 }
1721
1722 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001723 * Return the subwindow of this tab or null if there is no subwindow.
1724 * @return The subwindow of this tab or null.
1725 */
1726 WebView getSubWebView() {
1727 return mSubView;
1728 }
1729
Michael Kolb1514bb72010-11-22 09:11:48 -08001730 void setSubWebView(WebView subView) {
1731 mSubView = subView;
1732 }
1733
Michael Kolb8233fac2010-10-26 16:08:53 -07001734 View getSubViewContainer() {
1735 return mSubViewContainer;
1736 }
1737
Michael Kolb1514bb72010-11-22 09:11:48 -08001738 void setSubViewContainer(View subViewContainer) {
1739 mSubViewContainer = subViewContainer;
1740 }
1741
Grace Kloba22ac16e2009-10-07 18:00:23 -07001742 /**
1743 * @return The geolocation permissions prompt for this tab.
1744 */
1745 GeolocationPermissionsPrompt getGeolocationPermissionsPrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001746 if (mGeolocationPermissionsPrompt == null) {
1747 ViewStub stub = (ViewStub) mContainer
1748 .findViewById(R.id.geolocation_permissions_prompt);
1749 mGeolocationPermissionsPrompt = (GeolocationPermissionsPrompt) stub
1750 .inflate();
Panos Thomasb298aad2014-10-22 12:24:21 -07001751 mGeolocationPermissionsPrompt.init(mCurrentState.mIncognito);
Grace Kloba50c241e2010-04-20 11:07:50 -07001752 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001753 return mGeolocationPermissionsPrompt;
1754 }
1755
1756 /**
1757 * @return The application id string
1758 */
1759 String getAppId() {
1760 return mAppId;
1761 }
1762
1763 /**
1764 * Set the application id string
1765 * @param id
1766 */
1767 void setAppId(String id) {
1768 mAppId = id;
1769 }
1770
Michael Kolbe28b3472011-08-04 16:54:31 -07001771 boolean closeOnBack() {
1772 return mCloseOnBack;
1773 }
1774
1775 void setCloseOnBack(boolean close) {
1776 mCloseOnBack = close;
1777 }
1778
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001779 boolean getDerivedFromIntent() {
1780 return mDerivedFromIntent;
1781 }
1782
1783 void setDerivedFromIntent(boolean derived) {
1784 mDerivedFromIntent = derived;
1785 }
1786
Grace Kloba22ac16e2009-10-07 18:00:23 -07001787 String getUrl() {
John Reck324d4402011-01-11 16:56:42 -08001788 return UrlUtils.filteredUrl(mCurrentState.mUrl);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001789 }
1790
Tarun Nainani8eb00912014-07-17 12:28:32 -07001791
1792 protected void onPageFinished() {
1793 mPageFinished = true;
1794 }
1795
1796 public boolean getPageFinishedStatus() {
1797 return mPageFinished;
1798 }
1799
John Reck49a603c2011-03-03 09:33:05 -08001800 String getOriginalUrl() {
Vivek Sekhar361065a2014-11-25 15:34:35 -08001801 if (mMainView != null)
1802 return mMainView.getOriginalUrl();
John Reckdb22ec42011-06-29 11:31:24 -07001803 if (mCurrentState.mOriginalUrl == null) {
1804 return getUrl();
John Reck49a603c2011-03-03 09:33:05 -08001805 }
John Reckdb22ec42011-06-29 11:31:24 -07001806 return UrlUtils.filteredUrl(mCurrentState.mOriginalUrl);
John Reck49a603c2011-03-03 09:33:05 -08001807 }
1808
Grace Kloba22ac16e2009-10-07 18:00:23 -07001809 /**
John Reck30c714c2010-12-16 17:30:34 -08001810 * Get the title of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001811 */
1812 String getTitle() {
John Reck30c714c2010-12-16 17:30:34 -08001813 return mCurrentState.mTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001814 }
1815
1816 /**
John Reck30c714c2010-12-16 17:30:34 -08001817 * Get the favicon of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001818 */
1819 Bitmap getFavicon() {
John Reck1cf4b792011-07-26 10:22:22 -07001820 if (mCurrentState.mFavicon != null) {
1821 return mCurrentState.mFavicon;
1822 }
1823 return getDefaultFavicon(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001824 }
1825
John Recke969cc52010-12-21 17:24:43 -08001826 public boolean isBookmarkedSite() {
1827 return mCurrentState.mIsBookmarkedSite;
1828 }
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001829
Grace Kloba22ac16e2009-10-07 18:00:23 -07001830 /**
Steve Block08a6f0c2011-10-06 12:12:53 +01001831 * Sets the security state, clears the SSL certificate error and informs
1832 * the controller.
1833 */
Steve Block2466eff2011-10-03 15:33:09 +01001834 private void setSecurityState(SecurityState securityState) {
1835 mCurrentState.mSecurityState = securityState;
Steve Block08a6f0c2011-10-06 12:12:53 +01001836 mCurrentState.mSslCertificateError = null;
Steve Block2466eff2011-10-03 15:33:09 +01001837 mWebViewController.onUpdatedSecurityState(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001838 }
1839
1840 /**
Steve Block2466eff2011-10-03 15:33:09 +01001841 * @return The tab's security state.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001842 */
Steve Block2466eff2011-10-03 15:33:09 +01001843 SecurityState getSecurityState() {
1844 return mCurrentState.mSecurityState;
John Reck30c714c2010-12-16 17:30:34 -08001845 }
1846
Steve Block08a6f0c2011-10-06 12:12:53 +01001847 /**
1848 * Gets the SSL certificate error, if any, for the page's main resource.
1849 * This is only non-null when the security state is
1850 * SECURITY_STATE_BAD_CERTIFICATE.
1851 */
1852 SslError getSslCertificateError() {
1853 return mCurrentState.mSslCertificateError;
1854 }
1855
John Reck30c714c2010-12-16 17:30:34 -08001856 int getLoadProgress() {
1857 if (mInPageLoad) {
1858 return mPageLoadProgress;
1859 }
1860 return 100;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001861 }
1862
1863 /**
1864 * @return TRUE if onPageStarted is called while onPageFinished is not
1865 * called yet.
1866 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001867 boolean inPageLoad() {
1868 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001869 }
1870
Grace Kloba22ac16e2009-10-07 18:00:23 -07001871 /**
John Reck1cf4b792011-07-26 10:22:22 -07001872 * @return The Bundle with the tab's state if it can be saved, otherwise null
Grace Kloba22ac16e2009-10-07 18:00:23 -07001873 */
John Reck1cf4b792011-07-26 10:22:22 -07001874 public Bundle saveState() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001875 // If the WebView is null it means we ran low on memory and we already
1876 // stored the saved state in mSavedState.
1877 if (mMainView == null) {
John Reck1cf4b792011-07-26 10:22:22 -07001878 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001879 }
John Reck6c2e2f32011-08-22 13:41:23 -07001880
1881 if (TextUtils.isEmpty(mCurrentState.mUrl)) {
John Reck1cf4b792011-07-26 10:22:22 -07001882 return null;
John Reck24f18262011-06-17 14:47:20 -07001883 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001884
1885 mSavedState = new Bundle();
John Reck6c2e2f32011-08-22 13:41:23 -07001886 WebBackForwardList savedList = mMainView.saveState(mSavedState);
1887 if (savedList == null || savedList.getSize() == 0) {
1888 Log.w(LOGTAG, "Failed to save back/forward list for "
1889 + mCurrentState.mUrl);
1890 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001891
Michael Kolbc831b632011-05-11 09:30:34 -07001892 mSavedState.putLong(ID, mId);
John Reck30c714c2010-12-16 17:30:34 -08001893 mSavedState.putString(CURRURL, mCurrentState.mUrl);
1894 mSavedState.putString(CURRTITLE, mCurrentState.mTitle);
John Reck1cf4b792011-07-26 10:22:22 -07001895 mSavedState.putBoolean(INCOGNITO, mMainView.isPrivateBrowsingEnabled());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001896 if (mAppId != null) {
1897 mSavedState.putString(APPID, mAppId);
1898 }
Michael Kolbe28b3472011-08-04 16:54:31 -07001899 mSavedState.putBoolean(CLOSEFLAG, mCloseOnBack);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001900 // Remember the parent tab so the relationship can be restored.
Michael Kolbc831b632011-05-11 09:30:34 -07001901 if (mParent != null) {
1902 mSavedState.putLong(PARENTTAB, mParent.mId);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001903 }
John Reckb0a86db2011-05-24 14:05:58 -07001904 mSavedState.putBoolean(USERAGENT,
1905 mSettings.hasDesktopUseragent(getWebView()));
John Reck1cf4b792011-07-26 10:22:22 -07001906 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001907 }
1908
1909 /*
1910 * Restore the state of the tab.
1911 */
John Reck1cf4b792011-07-26 10:22:22 -07001912 private void restoreState(Bundle b) {
1913 mSavedState = b;
1914 if (mSavedState == null) {
1915 return;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001916 }
1917 // Restore the internal state even if the WebView fails to restore.
1918 // This will maintain the app id, original url and close-on-exit values.
Michael Kolbc831b632011-05-11 09:30:34 -07001919 mId = b.getLong(ID);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001920 mAppId = b.getString(APPID);
Michael Kolbe28b3472011-08-04 16:54:31 -07001921 mCloseOnBack = b.getBoolean(CLOSEFLAG);
John Reck8b9bb8b2012-03-08 13:19:40 -08001922 restoreUserAgent();
John Reck1cf4b792011-07-26 10:22:22 -07001923 String url = b.getString(CURRURL);
1924 String title = b.getString(CURRTITLE);
1925 boolean incognito = b.getBoolean(INCOGNITO);
1926 mCurrentState = new PageState(mContext, incognito, url, null);
1927 mCurrentState.mTitle = title;
1928 synchronized (Tab.this) {
1929 if (mCapture != null) {
John Reck4eadc342011-10-31 14:04:10 -07001930 DataController.getInstance(mContext).loadThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07001931 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001932 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001933 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04001934
John Reck8b9bb8b2012-03-08 13:19:40 -08001935 private void restoreUserAgent() {
1936 if (mMainView == null || mSavedState == null) {
1937 return;
1938 }
1939 if (mSavedState.getBoolean(USERAGENT)
1940 != mSettings.hasDesktopUseragent(mMainView)) {
1941 mSettings.toggleDesktopUseragent(mMainView);
1942 }
1943 }
1944
Leon Scroggins1961ed22010-12-07 15:22:21 -05001945 public void updateBookmarkedStatus() {
John Recke969cc52010-12-21 17:24:43 -08001946 mDataController.queryBookmarkStatus(getUrl(), mIsBookmarkCallback);
Leon Scroggins1961ed22010-12-07 15:22:21 -05001947 }
1948
John Recke969cc52010-12-21 17:24:43 -08001949 private DataController.OnQueryUrlIsBookmark mIsBookmarkCallback
1950 = new DataController.OnQueryUrlIsBookmark() {
1951 @Override
1952 public void onQueryUrlIsBookmark(String url, boolean isBookmark) {
1953 if (mCurrentState.mUrl.equals(url)) {
1954 mCurrentState.mIsBookmarkedSite = isBookmark;
1955 mWebViewController.bookmarkedStatusHasChanged(Tab.this);
1956 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001957 }
John Recke969cc52010-12-21 17:24:43 -08001958 };
Michael Kolb1acef692011-03-08 14:12:06 -08001959
Michael Kolbeb95db42011-03-03 10:38:40 -08001960 public Bitmap getScreenshot() {
John Reck1cf4b792011-07-26 10:22:22 -07001961 synchronized (Tab.this) {
1962 return mCapture;
1963 }
Michael Kolbeb95db42011-03-03 10:38:40 -08001964 }
1965
John Reck541f55a2011-06-07 16:34:43 -07001966 public boolean isSnapshot() {
John Reck541f55a2011-06-07 16:34:43 -07001967 return false;
1968 }
1969
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001970 private static class SaveCallback implements ValueCallback<String> {
1971 boolean onReceiveValueCalled = false;
1972 private String mPath;
John Reck68234a92012-04-19 15:27:12 -07001973
1974 @Override
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001975 public void onReceiveValue(String path) {
1976 this.onReceiveValueCalled = true;
1977 this.mPath = path;
John Reck68234a92012-04-19 15:27:12 -07001978 synchronized (this) {
1979 notifyAll();
John Reck8cc92352011-07-06 17:41:52 -07001980 }
John Reck541f55a2011-06-07 16:34:43 -07001981 }
John Reck68234a92012-04-19 15:27:12 -07001982
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001983 public String getPath() {
1984 return mPath;
1985 }
John Reck68234a92012-04-19 15:27:12 -07001986 }
1987
1988 /**
1989 * Must be called on the UI thread
1990 */
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001991 public ContentValues createSnapshotValues(Bitmap bm) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001992 WebView web = getWebView();
John Reck68234a92012-04-19 15:27:12 -07001993 if (web == null) return null;
John Reckd8c74522011-06-14 08:45:00 -07001994 ContentValues values = new ContentValues();
1995 values.put(Snapshots.TITLE, mCurrentState.mTitle);
1996 values.put(Snapshots.URL, mCurrentState.mUrl);
John Reck68234a92012-04-19 15:27:12 -07001997 values.put(Snapshots.BACKGROUND, web.getPageBackgroundColor());
John Reck8cc92352011-07-06 17:41:52 -07001998 values.put(Snapshots.DATE_CREATED, System.currentTimeMillis());
1999 values.put(Snapshots.FAVICON, compressBitmap(getFavicon()));
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07002000 values.put(Snapshots.THUMBNAIL, compressBitmap(bm));
John Reckd8c74522011-06-14 08:45:00 -07002001 return values;
John Reck541f55a2011-06-07 16:34:43 -07002002 }
2003
John Reck68234a92012-04-19 15:27:12 -07002004 /**
2005 * Probably want to call this on a background thread
2006 */
2007 public boolean saveViewState(ContentValues values) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002008 WebView web = getWebView();
John Reck68234a92012-04-19 15:27:12 -07002009 if (web == null) return false;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002010 String filename = UUID.randomUUID().toString();
John Reck68234a92012-04-19 15:27:12 -07002011 SaveCallback callback = new SaveCallback();
John Reck68234a92012-04-19 15:27:12 -07002012 try {
John Reck68234a92012-04-19 15:27:12 -07002013 synchronized (callback) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002014 web.saveViewState(filename, callback);
2015 callback.wait();
John Reck68234a92012-04-19 15:27:12 -07002016 }
John Reck68234a92012-04-19 15:27:12 -07002017 } catch (Exception e) {
2018 Log.w(LOGTAG, "Failed to save view state", e);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002019 String path = callback.getPath();
2020 if (path != null) {
2021 File file = mContext.getFileStreamPath(path);
2022 if (file.exists() && !file.delete()) {
2023 file.deleteOnExit();
2024 }
John Reck68234a92012-04-19 15:27:12 -07002025 }
2026 return false;
2027 }
Tarun Nainani8eb00912014-07-17 12:28:32 -07002028
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002029 String path = callback.getPath();
Tarun Nainani8eb00912014-07-17 12:28:32 -07002030 // could be that saving of file failed
2031 if (path == null) {
2032 return false;
2033 }
2034
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002035 File savedFile = new File(path);
2036 if (!savedFile.exists()) {
2037 return false;
John Reck68234a92012-04-19 15:27:12 -07002038 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002039 values.put(Snapshots.VIEWSTATE_PATH, path.substring(path.lastIndexOf('/') + 1));
2040 values.put(Snapshots.VIEWSTATE_SIZE, savedFile.length());
John Reck68234a92012-04-19 15:27:12 -07002041 return true;
2042 }
2043
John Reck8cc92352011-07-06 17:41:52 -07002044 public byte[] compressBitmap(Bitmap bitmap) {
2045 if (bitmap == null) {
2046 return null;
2047 }
2048 ByteArrayOutputStream stream = new ByteArrayOutputStream();
2049 bitmap.compress(CompressFormat.PNG, 100, stream);
2050 return stream.toByteArray();
2051 }
2052
John Reck26b18322011-06-21 13:08:58 -07002053 public void loadUrl(String url, Map<String, String> headers) {
2054 if (mMainView != null) {
Michael Kolba53c9892011-10-05 13:31:40 -07002055 mPageLoadProgress = INITIAL_PROGRESS;
Michael Kolb14612442011-06-24 13:06:29 -07002056 mCurrentState = new PageState(mContext, false, url, null);
John Reck26b18322011-06-21 13:08:58 -07002057 mMainView.loadUrl(url, headers);
2058 }
2059 }
2060
John Reck38b39652012-06-05 09:22:59 -07002061 public void disableUrlOverridingForLoad() {
2062 mDisableOverrideUrlLoading = true;
2063 }
2064
Michael Kolb9ef259a2011-07-12 15:33:08 -07002065 protected void capture() {
2066 if (mMainView == null || mCapture == null) return;
John Reck4eadc342011-10-31 14:04:10 -07002067 if (mMainView.getContentWidth() <= 0 || mMainView.getContentHeight() <= 0) {
2068 return;
2069 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002070
2071 mMainView
2072 .getContentBitmapAsync(
2073 (float) mCaptureWidth / mMainView.getWidth(),
2074 new Rect(),
2075 new ValueCallback<Bitmap>() {
2076 @Override
2077 public void onReceiveValue(Bitmap bitmap) {
2078 onCaptureCallback(bitmap);
2079 }});
2080 }
2081
2082 private void onCaptureCallback(Bitmap bitmap) {
2083 if (mCapture == null || bitmap == null)
2084 return;
2085
Michael Kolb9ef259a2011-07-12 15:33:08 -07002086 Canvas c = new Canvas(mCapture);
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002087 mCapture.eraseColor(Color.WHITE);
2088 c.drawBitmap(bitmap, 0, 0, null);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002089
Michael Kolba3194d02011-09-07 11:23:51 -07002090 // manually anti-alias the edges for the tilt
2091 c.drawRect(0, 0, 1, mCapture.getHeight(), sAlphaPaint);
2092 c.drawRect(mCapture.getWidth() - 1, 0, mCapture.getWidth(),
2093 mCapture.getHeight(), sAlphaPaint);
2094 c.drawRect(0, 0, mCapture.getWidth(), 1, sAlphaPaint);
2095 c.drawRect(0, mCapture.getHeight() - 1, mCapture.getWidth(),
2096 mCapture.getHeight(), sAlphaPaint);
Dianne Hackborn43cfe8a2011-08-02 16:59:35 -07002097 c.setBitmap(null);
John Reck8ee633f2011-08-09 16:00:35 -07002098 mHandler.removeMessages(MSG_CAPTURE);
John Reck1cf4b792011-07-26 10:22:22 -07002099 persistThumbnail();
John Reck8ee633f2011-08-09 16:00:35 -07002100 TabControl tc = mWebViewController.getTabControl();
2101 if (tc != null) {
2102 OnThumbnailUpdatedListener updateListener
2103 = tc.getOnThumbnailUpdatedListener();
2104 if (updateListener != null) {
2105 updateListener.onThumbnailUpdated(this);
2106 }
2107 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07002108 }
2109
2110 @Override
2111 public void onNewPicture(WebView view, Picture picture) {
John Reck1cf4b792011-07-26 10:22:22 -07002112 postCapture();
2113 }
2114
2115 private void postCapture() {
Michael Kolb9ef259a2011-07-12 15:33:08 -07002116 if (!mHandler.hasMessages(MSG_CAPTURE)) {
2117 mHandler.sendEmptyMessageDelayed(MSG_CAPTURE, CAPTURE_DELAY);
2118 }
2119 }
2120
John Reckef654f12011-07-12 16:42:08 -07002121 public boolean canGoBack() {
2122 return mMainView != null ? mMainView.canGoBack() : false;
2123 }
2124
2125 public boolean canGoForward() {
2126 return mMainView != null ? mMainView.canGoForward() : false;
2127 }
2128
2129 public void goBack() {
2130 if (mMainView != null) {
2131 mMainView.goBack();
2132 }
2133 }
2134
2135 public void goForward() {
2136 if (mMainView != null) {
2137 mMainView.goForward();
2138 }
2139 }
2140
John Reck1cf4b792011-07-26 10:22:22 -07002141 protected void persistThumbnail() {
John Reck4eadc342011-10-31 14:04:10 -07002142 DataController.getInstance(mContext).saveThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07002143 }
2144
2145 protected void deleteThumbnail() {
John Reck4eadc342011-10-31 14:04:10 -07002146 DataController.getInstance(mContext).deleteThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07002147 }
2148
John Reck4eadc342011-10-31 14:04:10 -07002149 void updateCaptureFromBlob(byte[] blob) {
John Reck1cf4b792011-07-26 10:22:22 -07002150 synchronized (Tab.this) {
2151 if (mCapture == null) {
2152 return;
2153 }
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002154 ByteBuffer buffer = ByteBuffer.wrap(blob);
Michael Kolbd837a112011-08-09 14:00:18 -07002155 try {
2156 mCapture.copyPixelsFromBuffer(buffer);
2157 } catch (RuntimeException rex) {
2158 Log.e(LOGTAG, "Load capture has mismatched sizes; buffer: "
2159 + buffer.capacity() + " blob: " + blob.length
2160 + "capture: " + mCapture.getByteCount());
2161 throw rex;
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002162 }
John Reck1cf4b792011-07-26 10:22:22 -07002163 }
2164 }
2165
John Reck52be4782011-08-26 15:37:29 -07002166 @Override
2167 public String toString() {
2168 StringBuilder builder = new StringBuilder(100);
2169 builder.append(mId);
2170 builder.append(") has parent: ");
2171 if (getParent() != null) {
2172 builder.append("true[");
2173 builder.append(getParent().getId());
2174 builder.append("]");
2175 } else {
2176 builder.append("false");
2177 }
2178 builder.append(", incog: ");
2179 builder.append(isPrivateBrowsingEnabled());
2180 if (!isPrivateBrowsingEnabled()) {
2181 builder.append(", title: ");
2182 builder.append(getTitle());
2183 builder.append(", url: ");
2184 builder.append(getUrl());
2185 }
2186 return builder.toString();
2187 }
2188
Steve Block4895b012011-10-03 16:26:46 +01002189 private void handleProceededAfterSslError(SslError error) {
2190 if (error.getUrl().equals(mCurrentState.mUrl)) {
2191 // The security state should currently be SECURITY_STATE_SECURE.
2192 setSecurityState(SecurityState.SECURITY_STATE_BAD_CERTIFICATE);
Steve Block08a6f0c2011-10-06 12:12:53 +01002193 mCurrentState.mSslCertificateError = error;
Steve Block4895b012011-10-03 16:26:46 +01002194 } else if (getSecurityState() == SecurityState.SECURITY_STATE_SECURE) {
Steve Block08a6f0c2011-10-06 12:12:53 +01002195 // The page's main resource is secure and this error is for a
2196 // sub-resource.
Steve Block4895b012011-10-03 16:26:46 +01002197 setSecurityState(SecurityState.SECURITY_STATE_MIXED);
2198 }
2199 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002200}