blob: 5e1f61472969ac8431153130727ed7b76de89774 [file] [log] [blame]
Grace Kloba22ac16e2009-10-07 18:00:23 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Bijan Amirzada41242f22014-03-21 12:12:18 -070017package com.android.browser;
Grace Kloba22ac16e2009-10-07 18:00:23 -070018
Michael Kolb8233fac2010-10-26 16:08:53 -070019import android.app.Activity;
Grace Kloba22ac16e2009-10-07 18:00:23 -070020import android.app.AlertDialog;
21import android.content.ContentResolver;
John Reckd8c74522011-06-14 08:45:00 -070022import android.content.ContentValues;
John Reck30c714c2010-12-16 17:30:34 -080023import android.content.Context;
Grace Kloba22ac16e2009-10-07 18:00:23 -070024import android.content.DialogInterface;
Michael Kolbfe251992010-07-08 15:41:55 -070025import android.content.DialogInterface.OnCancelListener;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080026import android.content.res.Resources;
Grace Kloba22ac16e2009-10-07 18:00:23 -070027import android.graphics.Bitmap;
John Reck8cc92352011-07-06 17:41:52 -070028import android.graphics.Bitmap.CompressFormat;
Michael Kolb9ef259a2011-07-12 15:33:08 -070029import android.graphics.BitmapFactory;
30import android.graphics.Canvas;
Michael Kolbc3af0672011-08-09 10:24:41 -070031import android.graphics.Color;
Michael Kolba3194d02011-09-07 11:23:51 -070032import android.graphics.Paint;
Michael Kolb9ef259a2011-07-12 15:33:08 -070033import android.graphics.Picture;
Michael Kolba3194d02011-09-07 11:23:51 -070034import android.graphics.PorterDuff;
35import android.graphics.PorterDuffXfermode;
Tarun Nainaniea28dde2014-08-27 17:25:09 -070036import android.graphics.Rect;
Grace Kloba22ac16e2009-10-07 18:00:23 -070037import android.net.Uri;
38import android.net.http.SslError;
Grace Kloba22ac16e2009-10-07 18:00:23 -070039import android.os.Bundle;
Michael Kolb9ef259a2011-07-12 15:33:08 -070040import android.os.Handler;
Grace Kloba22ac16e2009-10-07 18:00:23 -070041import android.os.Message;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +000042import android.os.SystemClock;
Brian Carlstrom8862c1d2011-06-02 01:05:55 -070043import android.security.KeyChain;
Brian Carlstromaa09cd82011-06-09 16:04:40 -070044import android.security.KeyChainAliasCallback;
John Reck24f18262011-06-17 14:47:20 -070045import android.text.TextUtils;
Grace Kloba22ac16e2009-10-07 18:00:23 -070046import android.util.Log;
47import android.view.KeyEvent;
48import android.view.LayoutInflater;
49import android.view.View;
Grace Kloba50c241e2010-04-20 11:07:50 -070050import android.view.ViewStub;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080051import android.view.View.OnClickListener;
Ben Murdochc42addf2010-01-28 15:19:59 +000052import android.webkit.ConsoleMessage;
Grace Kloba22ac16e2009-10-07 18:00:23 -070053import android.webkit.GeolocationPermissions;
Grace Kloba22ac16e2009-10-07 18:00:23 -070054import android.webkit.URLUtil;
John Reck438bf462011-01-12 18:11:46 -080055import android.webkit.WebResourceResponse;
Grace Kloba22ac16e2009-10-07 18:00:23 -070056import android.webkit.WebStorage;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080057import android.webkit.WebChromeClient.CustomViewCallback;
58import android.webkit.ValueCallback;
Ben Murdoch1d676b62011-01-17 12:54:24 +000059import android.widget.CheckBox;
Ben Murdoch8029a772010-11-16 11:58:21 +000060import android.widget.Toast;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080061import android.widget.FrameLayout;
62import android.widget.Button;
Grace Kloba22ac16e2009-10-07 18:00:23 -070063
Bijan Amirzada41242f22014-03-21 12:12:18 -070064import com.android.browser.TabControl.OnThumbnailUpdatedListener;
65import com.android.browser.homepages.HomeProvider;
66import com.android.browser.mynavigation.MyNavigationUtil;
67import com.android.browser.provider.MyNavigationProvider;
68import com.android.browser.provider.SnapshotProvider.Snapshots;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080069
70import org.codeaurora.swe.BrowserDownloadListener;
71import org.codeaurora.swe.ClientCertRequestHandler;
72import org.codeaurora.swe.HttpAuthHandler;
73import org.codeaurora.swe.SslErrorHandler;
74import org.codeaurora.swe.WebBackForwardList;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080075import org.codeaurora.swe.WebChromeClient;
76import org.codeaurora.swe.WebHistoryItem;
77import org.codeaurora.swe.WebView;
78import org.codeaurora.swe.WebView.PictureListener;
Pankaj Garg1c7380d2014-08-27 14:17:12 -070079import org.codeaurora.swe.WebView.CreateWindowParams;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080080import org.codeaurora.swe.WebViewClient;
81
John Reck541f55a2011-06-07 16:34:43 -070082import java.io.ByteArrayOutputStream;
John Reck2b71d6d2012-04-18 17:42:06 -070083import java.io.File;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080084import java.io.InputStream;
John Reck1cf4b792011-07-26 10:22:22 -070085import java.nio.ByteBuffer;
Michael Kolbfe251992010-07-08 15:41:55 -070086import java.util.LinkedList;
87import java.util.Map;
John Reck2b71d6d2012-04-18 17:42:06 -070088import java.util.UUID;
Michael Kolbfe251992010-07-08 15:41:55 -070089import java.util.Vector;
Mathew Inwooda829d552011-09-02 14:16:25 +010090import java.util.regex.Pattern;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080091import java.sql.Timestamp;
92import java.util.Date;
Michael Kolbfe251992010-07-08 15:41:55 -070093
Grace Kloba22ac16e2009-10-07 18:00:23 -070094/**
95 * Class for maintaining Tabs with a main WebView and a subwindow.
96 */
Michael Kolb9ef259a2011-07-12 15:33:08 -070097class Tab implements PictureListener {
Michael Kolb8233fac2010-10-26 16:08:53 -070098
Grace Kloba22ac16e2009-10-07 18:00:23 -070099 // Log Tag
100 private static final String LOGTAG = "Tab";
Bijan Amirzada41242f22014-03-21 12:12:18 -0700101 private static final boolean LOGD_ENABLED = com.android.browser.Browser.LOGD_ENABLED;
Ben Murdochc42addf2010-01-28 15:19:59 +0000102 // Special case the logtag for messages for the Console to make it easier to
103 // filter them and match the logtag used for these messages in older versions
104 // of the browser.
105 private static final String CONSOLE_LOGTAG = "browser";
106
Michael Kolb9ef259a2011-07-12 15:33:08 -0700107 private static final int MSG_CAPTURE = 42;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800108 private static final int CAPTURE_DELAY = 1000;
Michael Kolba53c9892011-10-05 13:31:40 -0700109 private static final int INITIAL_PROGRESS = 5;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700110
John Reck1cf4b792011-07-26 10:22:22 -0700111 private static Bitmap sDefaultFavicon;
112
Michael Kolba3194d02011-09-07 11:23:51 -0700113 private static Paint sAlphaPaint = new Paint();
114 static {
115 sAlphaPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
116 sAlphaPaint.setColor(Color.TRANSPARENT);
117 }
118
Steve Block2466eff2011-10-03 15:33:09 +0100119 public enum SecurityState {
Steve Block4895b012011-10-03 16:26:46 +0100120 // The page's main resource does not use SSL. Note that we use this
121 // state irrespective of the SSL authentication state of sub-resources.
Steve Block2466eff2011-10-03 15:33:09 +0100122 SECURITY_STATE_NOT_SECURE,
Steve Block4895b012011-10-03 16:26:46 +0100123 // The page's main resource uses SSL and the certificate is good. The
124 // same is true of all sub-resources.
Steve Block2466eff2011-10-03 15:33:09 +0100125 SECURITY_STATE_SECURE,
Steve Block4895b012011-10-03 16:26:46 +0100126 // The page's main resource uses SSL and the certificate is good, but
127 // some sub-resources either do not use SSL or have problems with their
128 // certificates.
Steve Block2466eff2011-10-03 15:33:09 +0100129 SECURITY_STATE_MIXED,
Steve Block4895b012011-10-03 16:26:46 +0100130 // The page's main resource uses SSL but there is a problem with its
131 // certificate.
132 SECURITY_STATE_BAD_CERTIFICATE,
John Reck30c714c2010-12-16 17:30:34 -0800133 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700134
Michael Kolb14612442011-06-24 13:06:29 -0700135 Context mContext;
John Reckd8c74522011-06-14 08:45:00 -0700136 protected WebViewController mWebViewController;
Michael Kolb8233fac2010-10-26 16:08:53 -0700137
Michael Kolbc831b632011-05-11 09:30:34 -0700138 // The tab ID
John Reckd8c74522011-06-14 08:45:00 -0700139 private long mId = -1;
Michael Kolbc831b632011-05-11 09:30:34 -0700140
Grace Kloba22ac16e2009-10-07 18:00:23 -0700141 // The Geolocation permissions prompt
142 private GeolocationPermissionsPrompt mGeolocationPermissionsPrompt;
143 // Main WebView wrapper
Michael Kolba713ec82010-11-29 17:27:06 -0800144 private View mContainer;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700145 // Main WebView
146 private WebView mMainView;
147 // Subwindow container
148 private View mSubViewContainer;
149 // Subwindow WebView
150 private WebView mSubView;
151 // Saved bundle for when we are running low on memory. It contains the
152 // information needed to restore the WebView if the user goes back to the
153 // tab.
154 private Bundle mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700155 // Parent Tab. This is the Tab that created this Tab, or null if the Tab was
156 // created by the UI
Michael Kolbc831b632011-05-11 09:30:34 -0700157 private Tab mParent;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700158 // Tab that constructed by this Tab. This is used when this Tab is
159 // destroyed, it clears all mParentTab values in the children.
Michael Kolbc831b632011-05-11 09:30:34 -0700160 private Vector<Tab> mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700161 // If true, the tab is in the foreground of the current activity.
162 private boolean mInForeground;
Michael Kolb8233fac2010-10-26 16:08:53 -0700163 // If true, the tab is in page loading state (after onPageStarted,
164 // before onPageFinsihed)
165 private boolean mInPageLoad;
Tarun Nainani8eb00912014-07-17 12:28:32 -0700166 private boolean mPageFinished;
John Reck38b39652012-06-05 09:22:59 -0700167 private boolean mDisableOverrideUrlLoading;
Pankaj Garg79878492015-04-01 14:48:21 -0700168 private boolean mFirstVisualPixelPainted = false;
John Reck30c714c2010-12-16 17:30:34 -0800169 // The last reported progress of the current page
170 private int mPageLoadProgress;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000171 // The time the load started, used to find load page time
172 private long mLoadStartTime;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700173 // Application identifier used to find tabs that another application wants
174 // to reuse.
175 private String mAppId;
Michael Kolbe28b3472011-08-04 16:54:31 -0700176 // flag to indicate if tab should be closed on back
177 private boolean mCloseOnBack;
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700178 // flag to indicate if the tab was opened from an intent
179 private boolean mDerivedFromIntent = false;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -0500180 // The listener that gets invoked when a download is started from the
181 // mMainView
Selim Gurun0b3d66f2012-08-29 13:08:13 -0700182 private final BrowserDownloadListener mDownloadListener;
John Recke969cc52010-12-21 17:24:43 -0800183 private DataController mDataController;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700184
185 // AsyncTask for downloading touch icons
186 DownloadTouchIcon mTouchIconLoader;
187
John Reck35e9dd62011-04-25 09:01:54 -0700188 private BrowserSettings mSettings;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700189 private int mCaptureWidth;
190 private int mCaptureHeight;
191 private Bitmap mCapture;
192 private Handler mHandler;
Michael Kolb72864272012-05-03 15:42:15 -0700193 private boolean mUpdateThumbnail;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800194 private Timestamp timestamp;
Vivek Sekharf96064b2014-07-28 16:32:34 -0700195 private boolean mFullScreen = false;
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800196 private boolean mReceivedError;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700197
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -0700198 // determine if webview is destroyed to MemoryMonitor
199 private boolean mWebViewDestroyedByMemoryMonitor;
200
201
Mathew Inwood1dd8e822011-08-03 14:34:29 +0100202
John Reck1cf4b792011-07-26 10:22:22 -0700203 private static synchronized Bitmap getDefaultFavicon(Context context) {
204 if (sDefaultFavicon == null) {
205 sDefaultFavicon = BitmapFactory.decodeResource(
Enrico Rosd6efa972014-12-02 19:49:59 -0800206 context.getResources(), R.drawable.ic_deco_favicon_normal);
John Reck1cf4b792011-07-26 10:22:22 -0700207 }
208 return sDefaultFavicon;
209 }
Michael Kolbeb95db42011-03-03 10:38:40 -0800210
John Reck30c714c2010-12-16 17:30:34 -0800211 // All the state needed for a page
John Reckd8c74522011-06-14 08:45:00 -0700212 protected static class PageState {
John Reck30c714c2010-12-16 17:30:34 -0800213 String mUrl;
John Reckdb22ec42011-06-29 11:31:24 -0700214 String mOriginalUrl;
John Reck30c714c2010-12-16 17:30:34 -0800215 String mTitle;
Steve Block2466eff2011-10-03 15:33:09 +0100216 SecurityState mSecurityState;
Steve Block08a6f0c2011-10-06 12:12:53 +0100217 // This is non-null only when mSecurityState is SECURITY_STATE_BAD_CERTIFICATE.
218 SslError mSslCertificateError;
John Reck30c714c2010-12-16 17:30:34 -0800219 Bitmap mFavicon;
Steve Block08a6f0c2011-10-06 12:12:53 +0100220 boolean mIsBookmarkedSite;
221 boolean mIncognito;
John Reck30c714c2010-12-16 17:30:34 -0800222
223 PageState(Context c, boolean incognito) {
John Reck502a3532011-08-16 14:21:46 -0700224 mIncognito = incognito;
225 if (mIncognito) {
Vivek Sekhared791da2015-02-22 12:39:05 -0800226 mOriginalUrl = mUrl = "chrome://incognito";
John Reck30c714c2010-12-16 17:30:34 -0800227 mTitle = c.getString(R.string.new_incognito_tab);
John Reck30c714c2010-12-16 17:30:34 -0800228 } else {
John Reckdb22ec42011-06-29 11:31:24 -0700229 mOriginalUrl = mUrl = "";
John Reck30c714c2010-12-16 17:30:34 -0800230 mTitle = c.getString(R.string.new_tab);
John Reck30c714c2010-12-16 17:30:34 -0800231 }
Steve Block2466eff2011-10-03 15:33:09 +0100232 mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
John Reck30c714c2010-12-16 17:30:34 -0800233 }
234
235 PageState(Context c, boolean incognito, String url, Bitmap favicon) {
John Reck502a3532011-08-16 14:21:46 -0700236 mIncognito = incognito;
John Reckdb22ec42011-06-29 11:31:24 -0700237 mOriginalUrl = mUrl = url;
John Reck30c714c2010-12-16 17:30:34 -0800238 if (URLUtil.isHttpsUrl(url)) {
Steve Block2466eff2011-10-03 15:33:09 +0100239 mSecurityState = SecurityState.SECURITY_STATE_SECURE;
John Reck30c714c2010-12-16 17:30:34 -0800240 } else {
Steve Block2466eff2011-10-03 15:33:09 +0100241 mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
John Reck30c714c2010-12-16 17:30:34 -0800242 }
John Reck1cf4b792011-07-26 10:22:22 -0700243 mFavicon = favicon;
John Reck30c714c2010-12-16 17:30:34 -0800244 }
John Reck1cf4b792011-07-26 10:22:22 -0700245
Grace Kloba22ac16e2009-10-07 18:00:23 -0700246 }
247
John Reck30c714c2010-12-16 17:30:34 -0800248 // The current/loading page's state
John Reckd8c74522011-06-14 08:45:00 -0700249 protected PageState mCurrentState;
John Reck30c714c2010-12-16 17:30:34 -0800250
Grace Kloba22ac16e2009-10-07 18:00:23 -0700251 // Used for saving and restoring each Tab
Michael Kolbc831b632011-05-11 09:30:34 -0700252 static final String ID = "ID";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700253 static final String CURRURL = "currentUrl";
254 static final String CURRTITLE = "currentTitle";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700255 static final String PARENTTAB = "parentTab";
256 static final String APPID = "appid";
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700257 static final String INCOGNITO = "privateBrowsingEnabled";
John Reckb0a86db2011-05-24 14:05:58 -0700258 static final String USERAGENT = "useragent";
Michael Kolbe28b3472011-08-04 16:54:31 -0700259 static final String CLOSEFLAG = "closeOnBack";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700260
Pankaj Garg18186a92015-03-31 14:59:33 -0700261 public void setNetworkAvailable(boolean networkUp) {
262 if (networkUp && mReceivedError && (mMainView != null)) {
263 mMainView.reload();
264 }
265 }
266
Grace Kloba22ac16e2009-10-07 18:00:23 -0700267 // -------------------------------------------------------------------------
268 // WebViewClient implementation for the main WebView
269 // -------------------------------------------------------------------------
270
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800271 private final WebViewClient mWebViewClient = new WebViewClient() {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500272 private Message mDontResend;
273 private Message mResend;
Michael Kolb47bd1e42011-09-01 15:25:00 -0700274
275 private boolean providersDiffer(String url, String otherUrl) {
276 Uri uri1 = Uri.parse(url);
277 Uri uri2 = Uri.parse(otherUrl);
278 return !uri1.getEncodedAuthority().equals(uri2.getEncodedAuthority());
279 }
280
Grace Kloba22ac16e2009-10-07 18:00:23 -0700281 @Override
282 public void onPageStarted(WebView view, String url, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700283 mInPageLoad = true;
Tarun Nainani8eb00912014-07-17 12:28:32 -0700284 mPageFinished = false;
Pankaj Garg79878492015-04-01 14:48:21 -0700285 mFirstVisualPixelPainted = false;
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800286 mReceivedError = false;
Michael Kolb72864272012-05-03 15:42:15 -0700287 mUpdateThumbnail = true;
Michael Kolba53c9892011-10-05 13:31:40 -0700288 mPageLoadProgress = INITIAL_PROGRESS;
Michael Kolb14612442011-06-24 13:06:29 -0700289 mCurrentState = new PageState(mContext,
John Reck30c714c2010-12-16 17:30:34 -0800290 view.isPrivateBrowsingEnabled(), url, favicon);
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000291 mLoadStartTime = SystemClock.uptimeMillis();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700292
293 // If we start a touch icon load and then load a new page, we don't
294 // want to cancel the current touch icon loader. But, we do want to
295 // create a new one when the touch icon url is known.
296 if (mTouchIconLoader != null) {
297 mTouchIconLoader.mTab = null;
298 mTouchIconLoader = null;
299 }
300
Panos Thomasb298aad2014-10-22 12:24:21 -0700301 // Loading a new page voids any ongoing Geolocation permission
302 // requests.
303 if (mGeolocationPermissionsPrompt != null) {
304 mGeolocationPermissionsPrompt.dismiss();
305 }
306
Grace Kloba22ac16e2009-10-07 18:00:23 -0700307 // finally update the UI in the activity if it is in the foreground
John Reck324d4402011-01-11 16:56:42 -0800308 mWebViewController.onPageStarted(Tab.this, view, favicon);
Leon Scroggins4cd97792010-12-03 15:31:56 -0500309
John Recke969cc52010-12-21 17:24:43 -0800310 updateBookmarkedStatus();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700311 }
312
313 @Override
314 public void onPageFinished(WebView view, String url) {
John Reck38b39652012-06-05 09:22:59 -0700315 mDisableOverrideUrlLoading = false;
John Reck5b691842010-11-29 11:21:13 -0800316 if (!isPrivateBrowsingEnabled()) {
317 LogTag.logPageFinishedLoading(
318 url, SystemClock.uptimeMillis() - mLoadStartTime);
319 }
John Reck1cf4b792011-07-26 10:22:22 -0700320 syncCurrentState(view, url);
John Reck324d4402011-01-11 16:56:42 -0800321 mWebViewController.onPageFinished(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700322 }
323
Pankaj Garg79878492015-04-01 14:48:21 -0700324 @Override
325 public void onFirstVisualPixel(WebView view) {
326 mFirstVisualPixelPainted = true;
327 }
328
Grace Kloba22ac16e2009-10-07 18:00:23 -0700329 // return true if want to hijack the url to let another app to handle it
330 @Override
331 public boolean shouldOverrideUrlLoading(WebView view, String url) {
John Reck38b39652012-06-05 09:22:59 -0700332 if (!mDisableOverrideUrlLoading && mInForeground) {
Michael Kolb18eb3772010-12-10 14:29:51 -0800333 return mWebViewController.shouldOverrideUrlLoading(Tab.this,
334 view, url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700335 } else {
336 return false;
337 }
338 }
339
Vivek Sekharb991edb2014-12-17 18:18:07 -0800340 @Override
341 public boolean shouldDownloadFavicon(WebView view, String url) {
342 return true;
343 }
344
Grace Kloba22ac16e2009-10-07 18:00:23 -0700345 /**
Steve Block2466eff2011-10-03 15:33:09 +0100346 * Updates the security state. This method is called when we discover
347 * another resource to be loaded for this page (for example,
348 * javascript). While we update the security state, we do not update
349 * the lock icon until we are done loading, as it is slightly more
350 * secure this way.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700351 */
352 @Override
353 public void onLoadResource(WebView view, String url) {
354 if (url != null && url.length() > 0) {
355 // It is only if the page claims to be secure that we may have
Steve Block2466eff2011-10-03 15:33:09 +0100356 // to update the security state:
357 if (mCurrentState.mSecurityState == SecurityState.SECURITY_STATE_SECURE) {
358 // If NOT a 'safe' url, change the state to mixed content!
Grace Kloba22ac16e2009-10-07 18:00:23 -0700359 if (!(URLUtil.isHttpsUrl(url) || URLUtil.isDataUrl(url)
360 || URLUtil.isAboutUrl(url))) {
Steve Block2466eff2011-10-03 15:33:09 +0100361 mCurrentState.mSecurityState = SecurityState.SECURITY_STATE_MIXED;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700362 }
363 }
364 }
365 }
366
367 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700368 * Show a dialog informing the user of the network error reported by
369 * WebCore if it is in the foreground.
370 */
371 @Override
372 public void onReceivedError(WebView view, int errorCode,
373 String description, String failingUrl) {
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800374 // Used for the syncCurrentState to use
375 // the failing url instead of using webview url
376 mReceivedError = true;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700377 }
378
379 /**
380 * Check with the user if it is ok to resend POST data as the page they
381 * are trying to navigate to is the result of a POST.
382 */
383 @Override
384 public void onFormResubmission(WebView view, final Message dontResend,
385 final Message resend) {
386 if (!mInForeground) {
387 dontResend.sendToTarget();
388 return;
389 }
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500390 if (mDontResend != null) {
391 Log.w(LOGTAG, "onFormResubmission should not be called again "
392 + "while dialog is still up");
393 dontResend.sendToTarget();
394 return;
395 }
396 mDontResend = dontResend;
397 mResend = resend;
Michael Kolb14612442011-06-24 13:06:29 -0700398 new AlertDialog.Builder(mContext).setTitle(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700399 R.string.browserFrameFormResubmitLabel).setMessage(
400 R.string.browserFrameFormResubmitMessage)
401 .setPositiveButton(R.string.ok,
402 new DialogInterface.OnClickListener() {
403 public void onClick(DialogInterface dialog,
404 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500405 if (mResend != null) {
406 mResend.sendToTarget();
407 mResend = null;
408 mDontResend = null;
409 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700410 }
411 }).setNegativeButton(R.string.cancel,
412 new DialogInterface.OnClickListener() {
413 public void onClick(DialogInterface dialog,
414 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500415 if (mDontResend != null) {
416 mDontResend.sendToTarget();
417 mResend = null;
418 mDontResend = null;
419 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700420 }
421 }).setOnCancelListener(new OnCancelListener() {
422 public void onCancel(DialogInterface dialog) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500423 if (mDontResend != null) {
424 mDontResend.sendToTarget();
425 mResend = null;
426 mDontResend = null;
427 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700428 }
429 }).show();
430 }
431
432 /**
433 * Insert the url into the visited history database.
434 * @param url The url to be inserted.
435 * @param isReload True if this url is being reloaded.
436 * FIXME: Not sure what to do when reloading the page.
437 */
438 @Override
439 public void doUpdateVisitedHistory(WebView view, String url,
440 boolean isReload) {
John Reck324d4402011-01-11 16:56:42 -0800441 mWebViewController.doUpdateVisitedHistory(Tab.this, isReload);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700442 }
443
444 /**
445 * Displays SSL error(s) dialog to the user.
446 */
447 @Override
448 public void onReceivedSslError(final WebView view,
449 final SslErrorHandler handler, final SslError error) {
450 if (!mInForeground) {
451 handler.cancel();
Steve Block2466eff2011-10-03 15:33:09 +0100452 setSecurityState(SecurityState.SECURITY_STATE_NOT_SECURE);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700453 return;
454 }
John Reck35e9dd62011-04-25 09:01:54 -0700455 if (mSettings.showSecurityWarnings()) {
John Reckcb28b2c2011-08-26 17:39:44 -0700456 new AlertDialog.Builder(mContext)
457 .setTitle(R.string.security_warning)
458 .setMessage(R.string.ssl_warnings_header)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200459 .setIconAttribute(android.R.attr.alertDialogIcon)
John Reckcb28b2c2011-08-26 17:39:44 -0700460 .setPositiveButton(R.string.ssl_continue,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700461 new DialogInterface.OnClickListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700462 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700463 public void onClick(DialogInterface dialog,
464 int whichButton) {
465 handler.proceed();
Steve Block4895b012011-10-03 16:26:46 +0100466 handleProceededAfterSslError(error);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700467 }
John Reckcb28b2c2011-08-26 17:39:44 -0700468 })
469 .setNeutralButton(R.string.view_certificate,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700470 new DialogInterface.OnClickListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700471 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700472 public void onClick(DialogInterface dialog,
473 int whichButton) {
John Reckcb28b2c2011-08-26 17:39:44 -0700474 mWebViewController.showSslCertificateOnError(
475 view, handler, error);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700476 }
John Reckcb28b2c2011-08-26 17:39:44 -0700477 })
478 .setNegativeButton(R.string.ssl_go_back,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700479 new DialogInterface.OnClickListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700480 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700481 public void onClick(DialogInterface dialog,
482 int whichButton) {
John Reck30c714c2010-12-16 17:30:34 -0800483 dialog.cancel();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700484 }
John Reckcb28b2c2011-08-26 17:39:44 -0700485 })
486 .setOnCancelListener(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700487 new DialogInterface.OnCancelListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700488 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700489 public void onCancel(DialogInterface dialog) {
490 handler.cancel();
Steve Block2466eff2011-10-03 15:33:09 +0100491 setSecurityState(SecurityState.SECURITY_STATE_NOT_SECURE);
John Reck30c714c2010-12-16 17:30:34 -0800492 mWebViewController.onUserCanceledSsl(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700493 }
John Reckcb28b2c2011-08-26 17:39:44 -0700494 })
495 .show();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700496 } else {
497 handler.proceed();
498 }
499 }
500
501 /**
Steve Block4895b012011-10-03 16:26:46 +0100502 * Called when an SSL error occurred while loading a resource, but the
503 * WebView but chose to proceed anyway based on a decision retained
504 * from a previous response to onReceivedSslError(). We update our
505 * security state to reflect this.
506 */
507 @Override
508 public void onProceededAfterSslError(WebView view, SslError error) {
509 handleProceededAfterSslError(error);
510 }
511
512 /**
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700513 * Displays client certificate request to the user.
514 */
515 @Override
516 public void onReceivedClientCertRequest(final WebView view,
517 final ClientCertRequestHandler handler, final String host_and_port) {
518 if (!mInForeground) {
519 handler.ignore();
520 return;
521 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700522 int colon = host_and_port.lastIndexOf(':');
523 String host;
524 int port;
525 if (colon == -1) {
526 host = host_and_port;
527 port = -1;
528 } else {
529 String portString = host_and_port.substring(colon + 1);
530 try {
531 port = Integer.parseInt(portString);
532 host = host_and_port.substring(0, colon);
533 } catch (NumberFormatException e) {
534 host = host_and_port;
535 port = -1;
536 }
537 }
Michael Kolb14612442011-06-24 13:06:29 -0700538 KeyChain.choosePrivateKeyAlias(
539 mWebViewController.getActivity(), new KeyChainAliasCallback() {
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700540 @Override public void alias(String alias) {
541 if (alias == null) {
542 handler.cancel();
543 return;
544 }
Michael Kolb14612442011-06-24 13:06:29 -0700545 new KeyChainLookup(mContext, handler, alias).execute();
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700546 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700547 }, null, null, host, port, null);
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700548 }
549
550 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700551 * Handles an HTTP authentication request.
552 *
553 * @param handler The authentication handler
554 * @param host The host
555 * @param realm The realm
556 */
557 @Override
558 public void onReceivedHttpAuthRequest(WebView view,
559 final HttpAuthHandler handler, final String host,
560 final String realm) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700561 mWebViewController.onReceivedHttpAuthRequest(Tab.this, view, handler, host, realm);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700562 }
563
564 @Override
John Reck438bf462011-01-12 18:11:46 -0800565 public WebResourceResponse shouldInterceptRequest(WebView view,
566 String url) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800567 //intercept if opening a new incognito tab - show the incognito welcome page
Vivek Sekhared791da2015-02-22 12:39:05 -0800568 if (url.startsWith("chrome://incognito")) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800569 Resources resourceHandle = mContext.getResources();
570 InputStream inStream = resourceHandle.openRawResource(
Bijan Amirzada41242f22014-03-21 12:12:18 -0700571 com.android.browser.R.raw.incognito_mode_start_page);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800572 return new WebResourceResponse("text/html", "utf8", inStream);
573 }
kaiyiz6e5b3e02013-08-19 20:02:01 +0800574 WebResourceResponse res;
575 if (MyNavigationUtil.MY_NAVIGATION.equals(url)) {
576 res = MyNavigationProvider.shouldInterceptRequest(mContext, url);
577 } else {
578 res = HomeProvider.shouldInterceptRequest(mContext, url);
579 }
John Reck438bf462011-01-12 18:11:46 -0800580 return res;
581 }
582
583 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700584 public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
585 if (!mInForeground) {
586 return false;
587 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700588 return mWebViewController.shouldOverrideKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700589 }
590
591 @Override
592 public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700593 if (!mInForeground) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700594 return;
595 }
John Reck997b1b72012-04-19 18:08:25 -0700596 if (!mWebViewController.onUnhandledKeyEvent(event)) {
597 super.onUnhandledKeyEvent(view, event);
598 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700599 }
600 };
601
John Reck1cf4b792011-07-26 10:22:22 -0700602 private void syncCurrentState(WebView view, String url) {
603 // Sync state (in case of stop/timeout)
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800604
605 if (mReceivedError) {
606 mCurrentState.mUrl = url;
607 mCurrentState.mOriginalUrl = url;
608 } else {
609 mCurrentState.mUrl = view.getUrl();
610 mCurrentState.mOriginalUrl = view.getOriginalUrl();
611 mCurrentState.mFavicon = view.getFavicon();
612 }
613
John Reck1cf4b792011-07-26 10:22:22 -0700614 if (mCurrentState.mUrl == null) {
John Reck8bcafc12011-08-29 16:43:02 -0700615 mCurrentState.mUrl = "";
John Reck1cf4b792011-07-26 10:22:22 -0700616 }
John Reck1cf4b792011-07-26 10:22:22 -0700617 mCurrentState.mTitle = view.getTitle();
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800618
619
John Reck1cf4b792011-07-26 10:22:22 -0700620 if (!URLUtil.isHttpsUrl(mCurrentState.mUrl)) {
621 // In case we stop when loading an HTTPS page from an HTTP page
622 // but before a provisional load occurred
Steve Block2466eff2011-10-03 15:33:09 +0100623 mCurrentState.mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
Steve Block08a6f0c2011-10-06 12:12:53 +0100624 mCurrentState.mSslCertificateError = null;
John Reck1cf4b792011-07-26 10:22:22 -0700625 }
John Reck502a3532011-08-16 14:21:46 -0700626 mCurrentState.mIncognito = view.isPrivateBrowsingEnabled();
John Reck1cf4b792011-07-26 10:22:22 -0700627 }
628
Tarun Nainani8eb00912014-07-17 12:28:32 -0700629
630 public boolean isTabFullScreen() {
631 return mFullScreen;
632 }
633
Vivek Sekharf96064b2014-07-28 16:32:34 -0700634 protected void setTabFullscreen(boolean fullScreen) {
Tarun Nainani8eb00912014-07-17 12:28:32 -0700635 Controller controller = (Controller)mWebViewController;
Sudheer Koganti24766882014-10-02 10:58:09 -0700636 controller.getUi().showFullscreen(fullScreen);
Tarun Nainani8eb00912014-07-17 12:28:32 -0700637 mFullScreen = fullScreen;
Vivek Sekharf96064b2014-07-28 16:32:34 -0700638 }
639
Sudheer Koganti24766882014-10-02 10:58:09 -0700640 public boolean exitFullscreen() {
641 if (mFullScreen) {
642 Controller controller = (Controller)mWebViewController;
643 controller.getUi().showFullscreen(false);
644 if (getWebView() != null)
645 getWebView().exitFullscreen();
646 mFullScreen = false;
647 return true;
648 }
649 return false;
650 }
651
652
653
654
Grace Kloba22ac16e2009-10-07 18:00:23 -0700655 // -------------------------------------------------------------------------
656 // WebChromeClient implementation for the main WebView
657 // -------------------------------------------------------------------------
658
659 private final WebChromeClient mWebChromeClient = new WebChromeClient() {
660 // Helper method to create a new tab or sub window.
661 private void createWindow(final boolean dialog, final Message msg) {
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700662 this.createWindow(dialog, msg, null, false);
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700663 }
664
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700665 private void createWindow(final boolean dialog, final Message msg, final String url,
666 final boolean opener_suppressed) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700667 WebView.WebViewTransport transport =
668 (WebView.WebViewTransport) msg.obj;
669 if (dialog) {
670 createSubWindow();
Michael Kolb8233fac2010-10-26 16:08:53 -0700671 mWebViewController.attachSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700672 transport.setWebView(mSubView);
673 } else {
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700674 final Tab newTab = mWebViewController.openTab(url,
John Reck5949c662011-05-27 09:52:29 -0700675 Tab.this, true, true);
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700676 // This is special case for rendering links on a webpage in
677 // a new tab. If opener is suppressed, the WebContents created
678 // by the content layer are not fully initialized. This check
679 // will prevent content layer from overriding WebContents
680 // created by new tab with the uninitialized instance.
681 if (!opener_suppressed) {
682 transport.setWebView(newTab.getWebView());
683 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700684 }
685 msg.sendToTarget();
686 }
687
688 @Override
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700689 public void toggleFullscreenModeForTab(boolean enterFullscreen) {
690 if (mWebViewController instanceof Controller) {
Vivek Sekharf96064b2014-07-28 16:32:34 -0700691 setTabFullscreen(enterFullscreen);
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700692 }
693 }
694
695 @Override
Tarun Nainani8eb00912014-07-17 12:28:32 -0700696 public void onOffsetsForFullscreenChanged(float topControlsOffsetYPix,
697 float contentOffsetYPix,
698 float overdrawBottomHeightPix) {
699 if (mWebViewController instanceof Controller) {
Vivek Sekhar8ee3abb2014-07-14 12:32:05 -0700700 Controller controller = (Controller)mWebViewController;
Tarun Nainani8eb00912014-07-17 12:28:32 -0700701 controller.getUi().translateTitleBar(topControlsOffsetYPix);
Vivek Sekhar8ee3abb2014-07-14 12:32:05 -0700702 }
703 }
704
705 @Override
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700706 public boolean isTabFullScreen() {
Vivek Sekharf96064b2014-07-28 16:32:34 -0700707 return mFullScreen;
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700708 }
709
710 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700711 public boolean onCreateWindow(WebView view, final boolean dialog,
712 final boolean userGesture, final Message resultMsg) {
713 // only allow new window or sub window for the foreground case
714 if (!mInForeground) {
715 return false;
716 }
717 // Short-circuit if we can't create any more tabs or sub windows.
718 if (dialog && mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -0700719 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700720 .setTitle(R.string.too_many_subwindows_dialog_title)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200721 .setIconAttribute(android.R.attr.alertDialogIcon)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700722 .setMessage(R.string.too_many_subwindows_dialog_message)
723 .setPositiveButton(R.string.ok, null)
724 .show();
725 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700726 } else if (!mWebViewController.getTabControl().canCreateNewTab()) {
Michael Kolb14612442011-06-24 13:06:29 -0700727 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700728 .setTitle(R.string.too_many_windows_dialog_title)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200729 .setIconAttribute(android.R.attr.alertDialogIcon)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700730 .setMessage(R.string.too_many_windows_dialog_message)
731 .setPositiveButton(R.string.ok, null)
732 .show();
733 return false;
734 }
735
736 // Short-circuit if this was a user gesture.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800737 if (userGesture || !mSettings.blockPopupWindows()) {
Tarun Nainani4f5137d2015-04-16 17:26:18 -0700738 WebView.WebViewTransport transport =
739 (WebView.WebViewTransport) resultMsg.obj;
740 CreateWindowParams windowParams = transport.getCreateWindowParams();
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700741 if (windowParams.mOpenerSuppressed) {
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700742 createWindow(dialog, resultMsg, windowParams.mURL, true);
743 // This is special case for rendering links on a webpage in
744 // a new tab. If opener is suppressed, the WebContents created
745 // by the content layer are not fully initialized. Returning false
746 // will prevent content layer from overriding WebContents
747 // created by new tab with the uninitialized instance.
748 return false;
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700749 }
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700750
751 createWindow(dialog, resultMsg);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700752 return true;
753 }
754
Tarun Nainani4f5137d2015-04-16 17:26:18 -0700755 createWindow(dialog, resultMsg);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700756 return true;
757 }
758
759 @Override
Patrick Scotteb5061b2009-11-18 15:00:30 -0500760 public void onRequestFocus(WebView view) {
761 if (!mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700762 mWebViewController.switchToTab(Tab.this);
Patrick Scotteb5061b2009-11-18 15:00:30 -0500763 }
764 }
765
766 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700767 public void onCloseWindow(WebView window) {
Michael Kolbc831b632011-05-11 09:30:34 -0700768 if (mParent != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700769 // JavaScript can only close popup window.
770 if (mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700771 mWebViewController.switchToTab(mParent);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700772 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700773 mWebViewController.closeTab(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700774 }
775 }
776
777 @Override
778 public void onProgressChanged(WebView view, int newProgress) {
John Reck30c714c2010-12-16 17:30:34 -0800779 mPageLoadProgress = newProgress;
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800780 if (newProgress == 100) {
781 mInPageLoad = false;
782 }
John Reck30c714c2010-12-16 17:30:34 -0800783 mWebViewController.onProgressChanged(Tab.this);
Michael Kolb72864272012-05-03 15:42:15 -0700784 if (mUpdateThumbnail && newProgress == 100) {
785 mUpdateThumbnail = false;
786 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700787 }
788
789 @Override
Leon Scroggins21d9b902010-03-11 09:33:11 -0500790 public void onReceivedTitle(WebView view, final String title) {
John Reck30c714c2010-12-16 17:30:34 -0800791 mCurrentState.mTitle = title;
Michael Kolb8233fac2010-10-26 16:08:53 -0700792 mWebViewController.onReceivedTitle(Tab.this, title);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700793 }
794
795 @Override
796 public void onReceivedIcon(WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -0800797 mCurrentState.mFavicon = icon;
Michael Kolb8233fac2010-10-26 16:08:53 -0700798 mWebViewController.onFavicon(Tab.this, view, icon);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700799 }
800
801 @Override
802 public void onReceivedTouchIconUrl(WebView view, String url,
803 boolean precomposed) {
Michael Kolb14612442011-06-24 13:06:29 -0700804 final ContentResolver cr = mContext.getContentResolver();
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400805 // Let precomposed icons take precedence over non-composed
806 // icons.
807 if (precomposed && mTouchIconLoader != null) {
808 mTouchIconLoader.cancel(false);
809 mTouchIconLoader = null;
810 }
811 // Have only one async task at a time.
812 if (mTouchIconLoader == null) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700813 mTouchIconLoader = new DownloadTouchIcon(Tab.this,
Michael Kolb14612442011-06-24 13:06:29 -0700814 mContext, cr, view);
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400815 mTouchIconLoader.execute(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700816 }
817 }
818
819 @Override
820 public void onShowCustomView(View view,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800821 CustomViewCallback callback) {
Michael Kolb14612442011-06-24 13:06:29 -0700822 Activity activity = mWebViewController.getActivity();
823 if (activity != null) {
824 onShowCustomView(view, activity.getRequestedOrientation(), callback);
825 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400826 }
827
828 @Override
829 public void onShowCustomView(View view, int requestedOrientation,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800830 CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700831 if (mInForeground) mWebViewController.showCustomView(Tab.this, view,
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400832 requestedOrientation, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700833 }
834
835 @Override
836 public void onHideCustomView() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700837 if (mInForeground) mWebViewController.hideCustomView();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700838 }
839
840 /**
841 * The origin has exceeded its database quota.
842 * @param url the URL that exceeded the quota
843 * @param databaseIdentifier the identifier of the database on which the
844 * transaction that caused the quota overflow was run
845 * @param currentQuota the current quota for the origin.
846 * @param estimatedSize the estimated size of the database.
847 * @param totalUsedQuota is the sum of all origins' quota.
848 * @param quotaUpdater The callback to run when a decision to allow or
849 * deny quota has been made. Don't forget to call this!
850 */
851 @Override
852 public void onExceededDatabaseQuota(String url,
853 String databaseIdentifier, long currentQuota, long estimatedSize,
854 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -0700855 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -0700856 .onExceededDatabaseQuota(url, databaseIdentifier,
857 currentQuota, estimatedSize, totalUsedQuota,
858 quotaUpdater);
859 }
860
861 /**
862 * The Application Cache has exceeded its max size.
863 * @param spaceNeeded is the amount of disk space that would be needed
864 * in order for the last appcache operation to succeed.
865 * @param totalUsedQuota is the sum of all origins' quota.
866 * @param quotaUpdater A callback to inform the WebCore thread that a
867 * new app cache size is available. This callback must always
868 * be executed at some point to ensure that the sleeping
869 * WebCore thread is woken up.
870 */
871 @Override
872 public void onReachedMaxAppCacheSize(long spaceNeeded,
873 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -0700874 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -0700875 .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota,
876 quotaUpdater);
877 }
878
879 /**
880 * Instructs the browser to show a prompt to ask the user to set the
881 * Geolocation permission state for the specified origin.
882 * @param origin The origin for which Geolocation permissions are
883 * requested.
884 * @param callback The callback to call once the user has set the
885 * Geolocation permission state.
886 */
887 @Override
888 public void onGeolocationPermissionsShowPrompt(String origin,
889 GeolocationPermissions.Callback callback) {
890 if (mInForeground) {
Grace Kloba50c241e2010-04-20 11:07:50 -0700891 getGeolocationPermissionsPrompt().show(origin, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700892 }
893 }
894
895 /**
896 * Instructs the browser to hide the Geolocation permissions prompt.
897 */
898 @Override
899 public void onGeolocationPermissionsHidePrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -0700900 if (mInForeground && mGeolocationPermissionsPrompt != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700901 mGeolocationPermissionsPrompt.hide();
902 }
903 }
904
Ben Murdoch65acc352009-11-19 18:16:04 +0000905 /* Adds a JavaScript error message to the system log and if the JS
906 * console is enabled in the about:debug options, to that console
907 * also.
Ben Murdochc42addf2010-01-28 15:19:59 +0000908 * @param consoleMessage the message object.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700909 */
910 @Override
Ben Murdochc42addf2010-01-28 15:19:59 +0000911 public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Jeff Hamilton47654f42010-09-07 09:57:51 -0500912 // Don't log console messages in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -0700913 if (isPrivateBrowsingEnabled()) return true;
Jeff Hamilton47654f42010-09-07 09:57:51 -0500914
Ben Murdochc42addf2010-01-28 15:19:59 +0000915 String message = "Console: " + consoleMessage.message() + " "
916 + consoleMessage.sourceId() + ":"
917 + consoleMessage.lineNumber();
918
919 switch (consoleMessage.messageLevel()) {
920 case TIP:
921 Log.v(CONSOLE_LOGTAG, message);
922 break;
923 case LOG:
924 Log.i(CONSOLE_LOGTAG, message);
925 break;
926 case WARNING:
927 Log.w(CONSOLE_LOGTAG, message);
928 break;
929 case ERROR:
930 Log.e(CONSOLE_LOGTAG, message);
931 break;
932 case DEBUG:
933 Log.d(CONSOLE_LOGTAG, message);
934 break;
935 }
936
937 return true;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700938 }
939
940 /**
941 * Ask the browser for an icon to represent a <video> element.
942 * This icon will be used if the Web page did not specify a poster attribute.
943 * @return Bitmap The icon or null if no such icon is available.
944 */
945 @Override
946 public Bitmap getDefaultVideoPoster() {
947 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700948 return mWebViewController.getDefaultVideoPoster();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700949 }
950 return null;
951 }
952
953 /**
954 * Ask the host application for a custom progress view to show while
955 * a <video> is loading.
956 * @return View The progress view.
957 */
958 @Override
959 public View getVideoLoadingProgressView() {
960 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700961 return mWebViewController.getVideoLoadingProgressView();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700962 }
963 return null;
964 }
965
966 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +0000967 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700968 if (mInForeground) {
Ben Murdoch8cad4132012-01-11 10:56:43 +0000969 mWebViewController.openFileChooser(uploadMsg, acceptType, capture);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700970 } else {
971 uploadMsg.onReceiveValue(null);
972 }
973 }
974
Vivek Sekharb54614f2014-05-01 19:03:37 -0700975 @Override
976 public void showFileChooser(ValueCallback<String[]> uploadFilePaths, String acceptTypes,
977 boolean capture) {
978 if (mInForeground) {
979 mWebViewController.showFileChooser(uploadFilePaths, acceptTypes, capture);
980 } else {
981 uploadFilePaths.onReceiveValue(null);
982 }
983 }
984
Grace Kloba22ac16e2009-10-07 18:00:23 -0700985 /**
986 * Deliver a list of already-visited URLs
987 */
988 @Override
989 public void getVisitedHistory(final ValueCallback<String[]> callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700990 mWebViewController.getVisitedHistory(callback);
991 }
Ben Murdoch8029a772010-11-16 11:58:21 +0000992
993 @Override
994 public void setupAutoFill(Message message) {
995 // Prompt the user to set up their profile.
996 final Message msg = message;
Michael Kolb14612442011-06-24 13:06:29 -0700997 AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
998 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
Ben Murdoch1d676b62011-01-17 12:54:24 +0000999 Context.LAYOUT_INFLATER_SERVICE);
1000 final View layout = inflater.inflate(R.layout.setup_autofill_dialog, null);
1001
1002 builder.setView(layout)
1003 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1004 @Override
1005 public void onClick(DialogInterface dialog, int id) {
1006 CheckBox disableAutoFill = (CheckBox) layout.findViewById(
1007 R.id.setup_autofill_dialog_disable_autofill);
1008
1009 if (disableAutoFill.isChecked()) {
1010 // Disable autofill and show a toast with how to turn it on again.
John Reck35e9dd62011-04-25 09:01:54 -07001011 mSettings.setAutofillEnabled(false);
Michael Kolb14612442011-06-24 13:06:29 -07001012 Toast.makeText(mContext,
Ben Murdoch1d676b62011-01-17 12:54:24 +00001013 R.string.autofill_setup_dialog_negative_toast,
1014 Toast.LENGTH_LONG).show();
1015 } else {
1016 // Take user to the AutoFill profile editor. When they return,
1017 // we will send the message that we pass here which will trigger
1018 // the form to get filled out with their new profile.
1019 mWebViewController.setupAutoFill(msg);
1020 }
1021 }
1022 })
1023 .setNegativeButton(R.string.cancel, null)
1024 .show();
Ben Murdoch8029a772010-11-16 11:58:21 +00001025 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001026 };
1027
1028 // -------------------------------------------------------------------------
1029 // WebViewClient implementation for the sub window
1030 // -------------------------------------------------------------------------
1031
1032 // Subclass of WebViewClient used in subwindows to notify the main
1033 // WebViewClient of certain WebView activities.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001034 private static class SubWindowClient extends WebViewClient {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001035 // The main WebViewClient.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001036 private final WebViewClient mClient;
Michael Kolb8233fac2010-10-26 16:08:53 -07001037 private final WebViewController mController;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001038
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001039 SubWindowClient(WebViewClient client, WebViewController controller) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001040 mClient = client;
Michael Kolb8233fac2010-10-26 16:08:53 -07001041 mController = controller;
Leon Scroggins III211ba542010-04-19 13:21:13 -04001042 }
1043 @Override
1044 public void onPageStarted(WebView view, String url, Bitmap favicon) {
1045 // Unlike the others, do not call mClient's version, which would
1046 // change the progress bar. However, we do want to remove the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001047 // find or select dialog.
Michael Kolb8233fac2010-10-26 16:08:53 -07001048 mController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001049 }
1050 @Override
1051 public void doUpdateVisitedHistory(WebView view, String url,
1052 boolean isReload) {
1053 mClient.doUpdateVisitedHistory(view, url, isReload);
1054 }
1055 @Override
1056 public boolean shouldOverrideUrlLoading(WebView view, String url) {
1057 return mClient.shouldOverrideUrlLoading(view, url);
1058 }
1059 @Override
1060 public void onReceivedSslError(WebView view, SslErrorHandler handler,
1061 SslError error) {
1062 mClient.onReceivedSslError(view, handler, error);
1063 }
1064 @Override
Brian Carlstrom8862c1d2011-06-02 01:05:55 -07001065 public void onReceivedClientCertRequest(WebView view,
1066 ClientCertRequestHandler handler, String host_and_port) {
1067 mClient.onReceivedClientCertRequest(view, handler, host_and_port);
1068 }
1069 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -07001070 public void onReceivedHttpAuthRequest(WebView view,
1071 HttpAuthHandler handler, String host, String realm) {
1072 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
1073 }
1074 @Override
1075 public void onFormResubmission(WebView view, Message dontResend,
1076 Message resend) {
1077 mClient.onFormResubmission(view, dontResend, resend);
1078 }
1079 @Override
1080 public void onReceivedError(WebView view, int errorCode,
1081 String description, String failingUrl) {
1082 mClient.onReceivedError(view, errorCode, description, failingUrl);
1083 }
1084 @Override
1085 public boolean shouldOverrideKeyEvent(WebView view,
1086 android.view.KeyEvent event) {
1087 return mClient.shouldOverrideKeyEvent(view, event);
1088 }
1089 @Override
1090 public void onUnhandledKeyEvent(WebView view,
1091 android.view.KeyEvent event) {
1092 mClient.onUnhandledKeyEvent(view, event);
1093 }
1094 }
1095
1096 // -------------------------------------------------------------------------
1097 // WebChromeClient implementation for the sub window
1098 // -------------------------------------------------------------------------
1099
1100 private class SubWindowChromeClient extends WebChromeClient {
1101 // The main WebChromeClient.
1102 private final WebChromeClient mClient;
1103
1104 SubWindowChromeClient(WebChromeClient client) {
1105 mClient = client;
1106 }
1107 @Override
1108 public void onProgressChanged(WebView view, int newProgress) {
1109 mClient.onProgressChanged(view, newProgress);
1110 }
1111 @Override
1112 public boolean onCreateWindow(WebView view, boolean dialog,
1113 boolean userGesture, android.os.Message resultMsg) {
1114 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
1115 }
1116 @Override
1117 public void onCloseWindow(WebView window) {
1118 if (window != mSubView) {
1119 Log.e(LOGTAG, "Can't close the window");
1120 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001121 mWebViewController.dismissSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001122 }
1123 }
1124
1125 // -------------------------------------------------------------------------
1126
1127 // Construct a new tab
Michael Kolb7bcafde2011-05-09 13:55:59 -07001128 Tab(WebViewController wvcontroller, WebView w) {
John Reck1cf4b792011-07-26 10:22:22 -07001129 this(wvcontroller, w, null);
1130 }
1131
1132 Tab(WebViewController wvcontroller, Bundle state) {
1133 this(wvcontroller, null, state);
1134 }
1135
1136 Tab(WebViewController wvcontroller, WebView w, Bundle state) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001137 mWebViewController = wvcontroller;
Michael Kolb14612442011-06-24 13:06:29 -07001138 mContext = mWebViewController.getContext();
John Reck35e9dd62011-04-25 09:01:54 -07001139 mSettings = BrowserSettings.getInstance();
Michael Kolb14612442011-06-24 13:06:29 -07001140 mDataController = DataController.getInstance(mContext);
1141 mCurrentState = new PageState(mContext, w != null
John Reck847b5322011-04-14 17:02:18 -07001142 ? w.isPrivateBrowsingEnabled() : false);
Tarun Nainani8084c822014-06-25 13:38:06 -07001143 setTimeStamp();
Michael Kolb8233fac2010-10-26 16:08:53 -07001144 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001145 mInForeground = false;
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001146 mWebViewDestroyedByMemoryMonitor = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001147
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001148 mDownloadListener = new BrowserDownloadListener() {
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001149 public void onDownloadStart(String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001150 String contentDisposition, String mimetype, String referer,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001151 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001152 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001153 mimetype, referer, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001154 }
1155 };
1156
John Reck1cf4b792011-07-26 10:22:22 -07001157 mCaptureWidth = mContext.getResources().getDimensionPixelSize(
1158 R.dimen.tab_thumbnail_width);
1159 mCaptureHeight = mContext.getResources().getDimensionPixelSize(
1160 R.dimen.tab_thumbnail_height);
1161 updateShouldCaptureThumbnails();
1162 restoreState(state);
John Reck52be4782011-08-26 15:37:29 -07001163 if (getId() == -1) {
1164 mId = TabControl.getNextId();
1165 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001166 setWebView(w);
Michael Kolb9ef259a2011-07-12 15:33:08 -07001167 mHandler = new Handler() {
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001168 @Override
Michael Kolb9ef259a2011-07-12 15:33:08 -07001169 public void handleMessage(Message m) {
John Reck1cf4b792011-07-26 10:22:22 -07001170 switch (m.what) {
1171 case MSG_CAPTURE:
1172 capture();
1173 break;
1174 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001175 }
1176 };
John Reck1cf4b792011-07-26 10:22:22 -07001177 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001178
Michael Kolb72864272012-05-03 15:42:15 -07001179 public boolean shouldUpdateThumbnail() {
1180 return mUpdateThumbnail;
1181 }
1182
Mathew Inwoode09305e2011-09-02 12:03:26 +01001183 /**
1184 * This is used to get a new ID when the tab has been preloaded, before it is displayed and
1185 * added to TabControl. Preloaded tabs can be created before restoreInstanceState, leading
1186 * to overlapping IDs between the preloaded and restored tabs.
1187 */
1188 public void refreshIdAfterPreload() {
1189 mId = TabControl.getNextId();
1190 }
1191
John Reck1cf4b792011-07-26 10:22:22 -07001192 public void updateShouldCaptureThumbnails() {
1193 if (mWebViewController.shouldCaptureThumbnails()) {
1194 synchronized (Tab.this) {
1195 if (mCapture == null) {
1196 mCapture = Bitmap.createBitmap(mCaptureWidth, mCaptureHeight,
1197 Bitmap.Config.RGB_565);
Michael Kolbc3af0672011-08-09 10:24:41 -07001198 mCapture.eraseColor(Color.WHITE);
John Reck1cf4b792011-07-26 10:22:22 -07001199 if (mInForeground) {
1200 postCapture();
1201 }
1202 }
1203 }
1204 } else {
1205 synchronized (Tab.this) {
1206 mCapture = null;
1207 deleteThumbnail();
1208 }
1209 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001210 }
1211
Michael Kolb14612442011-06-24 13:06:29 -07001212 public void setController(WebViewController ctl) {
1213 mWebViewController = ctl;
John Reck1cf4b792011-07-26 10:22:22 -07001214 updateShouldCaptureThumbnails();
Michael Kolb14612442011-06-24 13:06:29 -07001215 }
1216
Michael Kolbc831b632011-05-11 09:30:34 -07001217 public long getId() {
1218 return mId;
1219 }
1220
Michael Kolb91911a22012-01-17 11:21:25 -08001221 void setWebView(WebView w) {
1222 setWebView(w, true);
1223 }
1224
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001225 public boolean isNativeActive(){
1226 if (mMainView == null)
1227 return false;
1228 return true;
1229 }
1230
1231 public void setTimeStamp(){
1232 Date d = new Date();
1233 timestamp = (new Timestamp(d.getTime()));
1234 }
1235
1236 public Timestamp getTimestamp() {
1237 return timestamp;
1238 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001239 /**
1240 * Sets the WebView for this tab, correctly removing the old WebView from
1241 * the container view.
1242 */
Michael Kolb91911a22012-01-17 11:21:25 -08001243 void setWebView(WebView w, boolean restore) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001244 if (mMainView == w) {
1245 return;
1246 }
Michael Kolba713ec82010-11-29 17:27:06 -08001247
Grace Kloba22ac16e2009-10-07 18:00:23 -07001248 // If the WebView is changing, the page will be reloaded, so any ongoing
1249 // Geolocation permission requests are void.
Grace Kloba50c241e2010-04-20 11:07:50 -07001250 if (mGeolocationPermissionsPrompt != null) {
1251 mGeolocationPermissionsPrompt.hide();
1252 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001253
Michael Kolba713ec82010-11-29 17:27:06 -08001254 mWebViewController.onSetWebView(this, w);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001255
John Reck1cf4b792011-07-26 10:22:22 -07001256 if (mMainView != null) {
John Reck8ee633f2011-08-09 16:00:35 -07001257 mMainView.setPictureListener(null);
John Reck1cf4b792011-07-26 10:22:22 -07001258 if (w != null) {
1259 syncCurrentState(w, null);
1260 } else {
Panos Thomasa9a5a582014-03-18 19:20:08 -07001261 mCurrentState = new PageState(mContext, mMainView.isPrivateBrowsingEnabled());
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001262
1263 if (mWebViewDestroyedByMemoryMonitor) {
1264 /*
1265 * If tab was destroyed as a result of the MemoryMonitor
1266 * then we need to restore the state properties
1267 * from the old WebView (mMainView)
1268 */
1269 syncCurrentState(mMainView, null);
1270 mWebViewDestroyedByMemoryMonitor = false;
1271 }
John Reck1cf4b792011-07-26 10:22:22 -07001272 }
1273 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001274 // set the new one
1275 mMainView = w;
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001276
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001277 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001278 if (mMainView != null) {
1279 mMainView.setWebViewClient(mWebViewClient);
1280 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001281 // Attach DownloadManager so that downloads can start in an active
1282 // or a non-active window. This can happen when going to a site that
1283 // does a redirect after a period of time. The user could have
1284 // switched to another tab while waiting for the download to start.
1285 mMainView.setDownloadListener(mDownloadListener);
John Reck8ee633f2011-08-09 16:00:35 -07001286 TabControl tc = mWebViewController.getTabControl();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001287 if (tc != null /*&& tc.getOnThumbnailUpdatedListener() != null*/) {
John Reck8ee633f2011-08-09 16:00:35 -07001288 mMainView.setPictureListener(this);
1289 }
Michael Kolb91911a22012-01-17 11:21:25 -08001290 if (restore && (mSavedState != null)) {
John Reck8b9bb8b2012-03-08 13:19:40 -08001291 restoreUserAgent();
John Reck6c2e2f32011-08-22 13:41:23 -07001292 WebBackForwardList restoredState
1293 = mMainView.restoreState(mSavedState);
1294 if (restoredState == null || restoredState.getSize() == 0) {
1295 Log.w(LOGTAG, "Failed to restore WebView state!");
1296 loadUrl(mCurrentState.mOriginalUrl, null);
1297 }
John Reck1cf4b792011-07-26 10:22:22 -07001298 mSavedState = null;
1299 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001300 }
1301 }
1302
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001303 public void destroyThroughMemoryMonitor() {
1304 mWebViewDestroyedByMemoryMonitor = true;
1305 destroy();
1306 }
1307
Grace Kloba22ac16e2009-10-07 18:00:23 -07001308 /**
1309 * Destroy the tab's main WebView and subWindow if any
1310 */
1311 void destroy() {
Tarun Nainani2c1dd7c2014-07-05 16:40:12 -07001312
Matthew Huib7f2e9c2014-04-16 11:12:37 -04001313 if (mPostponeDestroy) {
1314 mShouldDestroy = true;
1315 return;
1316 }
1317 mShouldDestroy = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001318 if (mMainView != null) {
1319 dismissSubWindow();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001320 // save the WebView to call destroy() after detach it from the tab
1321 WebView webView = mMainView;
1322 setWebView(null);
1323 webView.destroy();
1324 }
1325 }
1326
Matthew Huib7f2e9c2014-04-16 11:12:37 -04001327 private boolean mPostponeDestroy = false;
1328 private boolean mShouldDestroy = false;
1329
1330 public void postponeDestroy() {
1331 mPostponeDestroy = true;
1332 }
1333
1334 public void performPostponedDestroy() {
1335 mPostponeDestroy = false;
1336 if (mShouldDestroy) {
1337 destroy();
1338 }
1339 }
1340
Grace Kloba22ac16e2009-10-07 18:00:23 -07001341 /**
1342 * Remove the tab from the parent
1343 */
1344 void removeFromTree() {
1345 // detach the children
Michael Kolbc831b632011-05-11 09:30:34 -07001346 if (mChildren != null) {
1347 for(Tab t : mChildren) {
1348 t.setParent(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001349 }
1350 }
1351 // remove itself from the parent list
Michael Kolbc831b632011-05-11 09:30:34 -07001352 if (mParent != null) {
1353 mParent.mChildren.remove(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001354 }
Tarun Nainani8eb00912014-07-17 12:28:32 -07001355
1356 mCapture = null;
John Reck1cf4b792011-07-26 10:22:22 -07001357 deleteThumbnail();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001358 }
1359
1360 /**
1361 * Create a new subwindow unless a subwindow already exists.
1362 * @return True if a new subwindow was created. False if one already exists.
1363 */
1364 boolean createSubWindow() {
1365 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001366 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001367 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001368 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001369 mSubView.setWebChromeClient(new SubWindowChromeClient(
1370 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001371 // Set a different DownloadListener for the mSubView, since it will
1372 // just need to dismiss the mSubView, rather than close the Tab
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001373 mSubView.setDownloadListener(new BrowserDownloadListener() {
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001374 public void onDownloadStart(String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001375 String contentDisposition, String mimetype, String referer,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001376 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001377 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001378 contentDisposition, mimetype, referer, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001379 if (mSubView.copyBackForwardList().getSize() == 0) {
1380 // This subwindow was opened for the sole purpose of
1381 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001382 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001383 }
1384 }
1385 });
Michael Kolb14612442011-06-24 13:06:29 -07001386 mSubView.setOnCreateContextMenuListener(mWebViewController.getActivity());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001387 return true;
1388 }
1389 return false;
1390 }
1391
1392 /**
1393 * Dismiss the subWindow for the tab.
1394 */
1395 void dismissSubWindow() {
1396 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001397 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001398 mSubView.destroy();
1399 mSubView = null;
1400 mSubViewContainer = null;
1401 }
1402 }
1403
Grace Kloba22ac16e2009-10-07 18:00:23 -07001404
1405 /**
1406 * Set the parent tab of this tab.
1407 */
Michael Kolbc831b632011-05-11 09:30:34 -07001408 void setParent(Tab parent) {
John Reck52be4782011-08-26 15:37:29 -07001409 if (parent == this) {
1410 throw new IllegalStateException("Cannot set parent to self!");
1411 }
Michael Kolbc831b632011-05-11 09:30:34 -07001412 mParent = parent;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001413 // This tab may have been freed due to low memory. If that is the case,
Michael Kolbc831b632011-05-11 09:30:34 -07001414 // the parent tab id is already saved. If we are changing that id
Grace Kloba22ac16e2009-10-07 18:00:23 -07001415 // (most likely due to removing the parent tab) we must update the
Michael Kolbc831b632011-05-11 09:30:34 -07001416 // parent tab id in the saved Bundle.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001417 if (mSavedState != null) {
1418 if (parent == null) {
1419 mSavedState.remove(PARENTTAB);
1420 } else {
Michael Kolbc831b632011-05-11 09:30:34 -07001421 mSavedState.putLong(PARENTTAB, parent.getId());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001422 }
1423 }
John Reckb0a86db2011-05-24 14:05:58 -07001424
1425 // Sync the WebView useragent with the parent
1426 if (parent != null && mSettings.hasDesktopUseragent(parent.getWebView())
1427 != mSettings.hasDesktopUseragent(getWebView())) {
1428 mSettings.toggleDesktopUseragent(getWebView());
1429 }
John Reck52be4782011-08-26 15:37:29 -07001430
1431 if (parent != null && parent.getId() == getId()) {
1432 throw new IllegalStateException("Parent has same ID as child!");
1433 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001434 }
1435
1436 /**
Michael Kolbc831b632011-05-11 09:30:34 -07001437 * If this Tab was created through another Tab, then this method returns
1438 * that Tab.
1439 * @return the Tab parent or null
1440 */
1441 public Tab getParent() {
1442 return mParent;
1443 }
1444
1445 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001446 * When a Tab is created through the content of another Tab, then we
1447 * associate the Tabs.
1448 * @param child the Tab that was created from this Tab
1449 */
1450 void addChildTab(Tab child) {
Michael Kolbc831b632011-05-11 09:30:34 -07001451 if (mChildren == null) {
1452 mChildren = new Vector<Tab>();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001453 }
Michael Kolbc831b632011-05-11 09:30:34 -07001454 mChildren.add(child);
1455 child.setParent(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001456 }
1457
Michael Kolbc831b632011-05-11 09:30:34 -07001458 Vector<Tab> getChildren() {
1459 return mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001460 }
1461
1462 void resume() {
1463 if (mMainView != null) {
John Reck56c1fcf2011-08-17 10:15:16 -07001464 setupHwAcceleration(mMainView);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001465 mMainView.onResume();
1466 if (mSubView != null) {
1467 mSubView.onResume();
1468 }
1469 }
1470 }
1471
John Reck56c1fcf2011-08-17 10:15:16 -07001472 private void setupHwAcceleration(View web) {
1473 if (web == null) return;
1474 BrowserSettings settings = BrowserSettings.getInstance();
1475 if (settings.isHardwareAccelerated()) {
1476 web.setLayerType(View.LAYER_TYPE_NONE, null);
1477 } else {
1478 web.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
1479 }
1480 }
1481
Grace Kloba22ac16e2009-10-07 18:00:23 -07001482 void pause() {
1483 if (mMainView != null) {
1484 mMainView.onPause();
1485 if (mSubView != null) {
1486 mSubView.onPause();
1487 }
1488 }
1489 }
1490
1491 void putInForeground() {
John Reck8ee633f2011-08-09 16:00:35 -07001492 if (mInForeground) {
1493 return;
1494 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001495 mInForeground = true;
1496 resume();
Michael Kolb14612442011-06-24 13:06:29 -07001497 Activity activity = mWebViewController.getActivity();
1498 mMainView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001499 if (mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -07001500 mSubView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001501 }
Axesh R. Ajmerac6b5c322015-05-01 11:06:10 -07001502
Leon Scroggins1961ed22010-12-07 15:22:21 -05001503 mWebViewController.bookmarkedStatusHasChanged(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001504 }
1505
1506 void putInBackground() {
John Reck8ee633f2011-08-09 16:00:35 -07001507 if (!mInForeground) {
1508 return;
1509 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001510 mInForeground = false;
1511 pause();
1512 mMainView.setOnCreateContextMenuListener(null);
1513 if (mSubView != null) {
1514 mSubView.setOnCreateContextMenuListener(null);
1515 }
1516 }
1517
Michael Kolb8233fac2010-10-26 16:08:53 -07001518 boolean inForeground() {
1519 return mInForeground;
1520 }
1521
Grace Kloba22ac16e2009-10-07 18:00:23 -07001522 /**
1523 * Return the top window of this tab; either the subwindow if it is not
1524 * null or the main window.
1525 * @return The top window of this tab.
1526 */
1527 WebView getTopWindow() {
1528 if (mSubView != null) {
1529 return mSubView;
1530 }
1531 return mMainView;
1532 }
1533
1534 /**
1535 * Return the main window of this tab. Note: if a tab is freed in the
1536 * background, this can return null. It is only guaranteed to be
1537 * non-null for the current tab.
1538 * @return The main WebView of this tab.
1539 */
1540 WebView getWebView() {
1541 return mMainView;
1542 }
1543
Michael Kolba713ec82010-11-29 17:27:06 -08001544 void setViewContainer(View container) {
1545 mContainer = container;
1546 }
1547
Michael Kolb8233fac2010-10-26 16:08:53 -07001548 View getViewContainer() {
1549 return mContainer;
1550 }
1551
Grace Kloba22ac16e2009-10-07 18:00:23 -07001552 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001553 * Return whether private browsing is enabled for the main window of
1554 * this tab.
1555 * @return True if private browsing is enabled.
1556 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001557 boolean isPrivateBrowsingEnabled() {
John Reck502a3532011-08-16 14:21:46 -07001558 return mCurrentState.mIncognito;
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001559 }
1560
1561 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001562 * Return the subwindow of this tab or null if there is no subwindow.
1563 * @return The subwindow of this tab or null.
1564 */
1565 WebView getSubWebView() {
1566 return mSubView;
1567 }
1568
Michael Kolb1514bb72010-11-22 09:11:48 -08001569 void setSubWebView(WebView subView) {
1570 mSubView = subView;
1571 }
1572
Michael Kolb8233fac2010-10-26 16:08:53 -07001573 View getSubViewContainer() {
1574 return mSubViewContainer;
1575 }
1576
Michael Kolb1514bb72010-11-22 09:11:48 -08001577 void setSubViewContainer(View subViewContainer) {
1578 mSubViewContainer = subViewContainer;
1579 }
1580
Grace Kloba22ac16e2009-10-07 18:00:23 -07001581 /**
1582 * @return The geolocation permissions prompt for this tab.
1583 */
1584 GeolocationPermissionsPrompt getGeolocationPermissionsPrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001585 if (mGeolocationPermissionsPrompt == null) {
1586 ViewStub stub = (ViewStub) mContainer
1587 .findViewById(R.id.geolocation_permissions_prompt);
1588 mGeolocationPermissionsPrompt = (GeolocationPermissionsPrompt) stub
1589 .inflate();
Panos Thomasb298aad2014-10-22 12:24:21 -07001590 mGeolocationPermissionsPrompt.init(mCurrentState.mIncognito);
Grace Kloba50c241e2010-04-20 11:07:50 -07001591 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001592 return mGeolocationPermissionsPrompt;
1593 }
1594
1595 /**
1596 * @return The application id string
1597 */
1598 String getAppId() {
1599 return mAppId;
1600 }
1601
1602 /**
1603 * Set the application id string
1604 * @param id
1605 */
1606 void setAppId(String id) {
1607 mAppId = id;
1608 }
1609
Michael Kolbe28b3472011-08-04 16:54:31 -07001610 boolean closeOnBack() {
1611 return mCloseOnBack;
1612 }
1613
1614 void setCloseOnBack(boolean close) {
1615 mCloseOnBack = close;
1616 }
1617
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001618 boolean getDerivedFromIntent() {
1619 return mDerivedFromIntent;
1620 }
1621
1622 void setDerivedFromIntent(boolean derived) {
1623 mDerivedFromIntent = derived;
1624 }
1625
Grace Kloba22ac16e2009-10-07 18:00:23 -07001626 String getUrl() {
John Reck324d4402011-01-11 16:56:42 -08001627 return UrlUtils.filteredUrl(mCurrentState.mUrl);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001628 }
1629
Tarun Nainani8eb00912014-07-17 12:28:32 -07001630
1631 protected void onPageFinished() {
1632 mPageFinished = true;
1633 }
1634
1635 public boolean getPageFinishedStatus() {
1636 return mPageFinished;
1637 }
1638
John Reck49a603c2011-03-03 09:33:05 -08001639 String getOriginalUrl() {
Vivek Sekhar361065a2014-11-25 15:34:35 -08001640 if (mMainView != null)
1641 return mMainView.getOriginalUrl();
John Reckdb22ec42011-06-29 11:31:24 -07001642 if (mCurrentState.mOriginalUrl == null) {
1643 return getUrl();
John Reck49a603c2011-03-03 09:33:05 -08001644 }
John Reckdb22ec42011-06-29 11:31:24 -07001645 return UrlUtils.filteredUrl(mCurrentState.mOriginalUrl);
John Reck49a603c2011-03-03 09:33:05 -08001646 }
1647
Grace Kloba22ac16e2009-10-07 18:00:23 -07001648 /**
John Reck30c714c2010-12-16 17:30:34 -08001649 * Get the title of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001650 */
1651 String getTitle() {
John Reck30c714c2010-12-16 17:30:34 -08001652 return mCurrentState.mTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001653 }
1654
1655 /**
John Reck30c714c2010-12-16 17:30:34 -08001656 * Get the favicon of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001657 */
1658 Bitmap getFavicon() {
John Reck1cf4b792011-07-26 10:22:22 -07001659 if (mCurrentState.mFavicon != null) {
1660 return mCurrentState.mFavicon;
1661 }
1662 return getDefaultFavicon(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001663 }
1664
John Recke969cc52010-12-21 17:24:43 -08001665 public boolean isBookmarkedSite() {
1666 return mCurrentState.mIsBookmarkedSite;
1667 }
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001668
Grace Kloba22ac16e2009-10-07 18:00:23 -07001669 /**
Steve Block08a6f0c2011-10-06 12:12:53 +01001670 * Sets the security state, clears the SSL certificate error and informs
1671 * the controller.
1672 */
Steve Block2466eff2011-10-03 15:33:09 +01001673 private void setSecurityState(SecurityState securityState) {
1674 mCurrentState.mSecurityState = securityState;
Steve Block08a6f0c2011-10-06 12:12:53 +01001675 mCurrentState.mSslCertificateError = null;
Steve Block2466eff2011-10-03 15:33:09 +01001676 mWebViewController.onUpdatedSecurityState(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001677 }
1678
1679 /**
Steve Block2466eff2011-10-03 15:33:09 +01001680 * @return The tab's security state.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001681 */
Steve Block2466eff2011-10-03 15:33:09 +01001682 SecurityState getSecurityState() {
1683 return mCurrentState.mSecurityState;
John Reck30c714c2010-12-16 17:30:34 -08001684 }
1685
Steve Block08a6f0c2011-10-06 12:12:53 +01001686 /**
1687 * Gets the SSL certificate error, if any, for the page's main resource.
1688 * This is only non-null when the security state is
1689 * SECURITY_STATE_BAD_CERTIFICATE.
1690 */
1691 SslError getSslCertificateError() {
1692 return mCurrentState.mSslCertificateError;
1693 }
1694
John Reck30c714c2010-12-16 17:30:34 -08001695 int getLoadProgress() {
1696 if (mInPageLoad) {
1697 return mPageLoadProgress;
1698 }
1699 return 100;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001700 }
1701
1702 /**
1703 * @return TRUE if onPageStarted is called while onPageFinished is not
1704 * called yet.
1705 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001706 boolean inPageLoad() {
1707 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001708 }
1709
Grace Kloba22ac16e2009-10-07 18:00:23 -07001710 /**
John Reck1cf4b792011-07-26 10:22:22 -07001711 * @return The Bundle with the tab's state if it can be saved, otherwise null
Grace Kloba22ac16e2009-10-07 18:00:23 -07001712 */
John Reck1cf4b792011-07-26 10:22:22 -07001713 public Bundle saveState() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001714 // If the WebView is null it means we ran low on memory and we already
1715 // stored the saved state in mSavedState.
1716 if (mMainView == null) {
John Reck1cf4b792011-07-26 10:22:22 -07001717 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001718 }
John Reck6c2e2f32011-08-22 13:41:23 -07001719
1720 if (TextUtils.isEmpty(mCurrentState.mUrl)) {
John Reck1cf4b792011-07-26 10:22:22 -07001721 return null;
John Reck24f18262011-06-17 14:47:20 -07001722 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001723
1724 mSavedState = new Bundle();
John Reck6c2e2f32011-08-22 13:41:23 -07001725 WebBackForwardList savedList = mMainView.saveState(mSavedState);
1726 if (savedList == null || savedList.getSize() == 0) {
1727 Log.w(LOGTAG, "Failed to save back/forward list for "
1728 + mCurrentState.mUrl);
1729 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001730
Michael Kolbc831b632011-05-11 09:30:34 -07001731 mSavedState.putLong(ID, mId);
John Reck30c714c2010-12-16 17:30:34 -08001732 mSavedState.putString(CURRURL, mCurrentState.mUrl);
1733 mSavedState.putString(CURRTITLE, mCurrentState.mTitle);
John Reck1cf4b792011-07-26 10:22:22 -07001734 mSavedState.putBoolean(INCOGNITO, mMainView.isPrivateBrowsingEnabled());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001735 if (mAppId != null) {
1736 mSavedState.putString(APPID, mAppId);
1737 }
Michael Kolbe28b3472011-08-04 16:54:31 -07001738 mSavedState.putBoolean(CLOSEFLAG, mCloseOnBack);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001739 // Remember the parent tab so the relationship can be restored.
Michael Kolbc831b632011-05-11 09:30:34 -07001740 if (mParent != null) {
1741 mSavedState.putLong(PARENTTAB, mParent.mId);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001742 }
John Reckb0a86db2011-05-24 14:05:58 -07001743 mSavedState.putBoolean(USERAGENT,
1744 mSettings.hasDesktopUseragent(getWebView()));
John Reck1cf4b792011-07-26 10:22:22 -07001745 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001746 }
1747
1748 /*
1749 * Restore the state of the tab.
1750 */
John Reck1cf4b792011-07-26 10:22:22 -07001751 private void restoreState(Bundle b) {
1752 mSavedState = b;
1753 if (mSavedState == null) {
1754 return;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001755 }
1756 // Restore the internal state even if the WebView fails to restore.
1757 // This will maintain the app id, original url and close-on-exit values.
Michael Kolbc831b632011-05-11 09:30:34 -07001758 mId = b.getLong(ID);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001759 mAppId = b.getString(APPID);
Michael Kolbe28b3472011-08-04 16:54:31 -07001760 mCloseOnBack = b.getBoolean(CLOSEFLAG);
John Reck8b9bb8b2012-03-08 13:19:40 -08001761 restoreUserAgent();
John Reck1cf4b792011-07-26 10:22:22 -07001762 String url = b.getString(CURRURL);
1763 String title = b.getString(CURRTITLE);
1764 boolean incognito = b.getBoolean(INCOGNITO);
1765 mCurrentState = new PageState(mContext, incognito, url, null);
1766 mCurrentState.mTitle = title;
1767 synchronized (Tab.this) {
1768 if (mCapture != null) {
John Reck4eadc342011-10-31 14:04:10 -07001769 DataController.getInstance(mContext).loadThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07001770 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001771 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001772 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04001773
John Reck8b9bb8b2012-03-08 13:19:40 -08001774 private void restoreUserAgent() {
1775 if (mMainView == null || mSavedState == null) {
1776 return;
1777 }
1778 if (mSavedState.getBoolean(USERAGENT)
1779 != mSettings.hasDesktopUseragent(mMainView)) {
1780 mSettings.toggleDesktopUseragent(mMainView);
1781 }
1782 }
1783
Leon Scroggins1961ed22010-12-07 15:22:21 -05001784 public void updateBookmarkedStatus() {
John Recke969cc52010-12-21 17:24:43 -08001785 mDataController.queryBookmarkStatus(getUrl(), mIsBookmarkCallback);
Leon Scroggins1961ed22010-12-07 15:22:21 -05001786 }
1787
John Recke969cc52010-12-21 17:24:43 -08001788 private DataController.OnQueryUrlIsBookmark mIsBookmarkCallback
1789 = new DataController.OnQueryUrlIsBookmark() {
1790 @Override
1791 public void onQueryUrlIsBookmark(String url, boolean isBookmark) {
1792 if (mCurrentState.mUrl.equals(url)) {
1793 mCurrentState.mIsBookmarkedSite = isBookmark;
1794 mWebViewController.bookmarkedStatusHasChanged(Tab.this);
1795 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001796 }
John Recke969cc52010-12-21 17:24:43 -08001797 };
Michael Kolb1acef692011-03-08 14:12:06 -08001798
Michael Kolbeb95db42011-03-03 10:38:40 -08001799 public Bitmap getScreenshot() {
John Reck1cf4b792011-07-26 10:22:22 -07001800 synchronized (Tab.this) {
1801 return mCapture;
1802 }
Michael Kolbeb95db42011-03-03 10:38:40 -08001803 }
1804
John Reck541f55a2011-06-07 16:34:43 -07001805 public boolean isSnapshot() {
John Reck541f55a2011-06-07 16:34:43 -07001806 return false;
1807 }
1808
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001809 private static class SaveCallback implements ValueCallback<String> {
1810 boolean onReceiveValueCalled = false;
1811 private String mPath;
John Reck68234a92012-04-19 15:27:12 -07001812
1813 @Override
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001814 public void onReceiveValue(String path) {
1815 this.onReceiveValueCalled = true;
1816 this.mPath = path;
John Reck68234a92012-04-19 15:27:12 -07001817 synchronized (this) {
1818 notifyAll();
John Reck8cc92352011-07-06 17:41:52 -07001819 }
John Reck541f55a2011-06-07 16:34:43 -07001820 }
John Reck68234a92012-04-19 15:27:12 -07001821
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001822 public String getPath() {
1823 return mPath;
1824 }
John Reck68234a92012-04-19 15:27:12 -07001825 }
1826
1827 /**
1828 * Must be called on the UI thread
1829 */
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001830 public ContentValues createSnapshotValues(Bitmap bm) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001831 WebView web = getWebView();
John Reck68234a92012-04-19 15:27:12 -07001832 if (web == null) return null;
John Reckd8c74522011-06-14 08:45:00 -07001833 ContentValues values = new ContentValues();
1834 values.put(Snapshots.TITLE, mCurrentState.mTitle);
1835 values.put(Snapshots.URL, mCurrentState.mUrl);
John Reck68234a92012-04-19 15:27:12 -07001836 values.put(Snapshots.BACKGROUND, web.getPageBackgroundColor());
John Reck8cc92352011-07-06 17:41:52 -07001837 values.put(Snapshots.DATE_CREATED, System.currentTimeMillis());
1838 values.put(Snapshots.FAVICON, compressBitmap(getFavicon()));
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001839 values.put(Snapshots.THUMBNAIL, compressBitmap(bm));
John Reckd8c74522011-06-14 08:45:00 -07001840 return values;
John Reck541f55a2011-06-07 16:34:43 -07001841 }
1842
John Reck68234a92012-04-19 15:27:12 -07001843 /**
1844 * Probably want to call this on a background thread
1845 */
1846 public boolean saveViewState(ContentValues values) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001847 WebView web = getWebView();
John Reck68234a92012-04-19 15:27:12 -07001848 if (web == null) return false;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001849 String filename = UUID.randomUUID().toString();
John Reck68234a92012-04-19 15:27:12 -07001850 SaveCallback callback = new SaveCallback();
John Reck68234a92012-04-19 15:27:12 -07001851 try {
John Reck68234a92012-04-19 15:27:12 -07001852 synchronized (callback) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001853 web.saveViewState(filename, callback);
1854 callback.wait();
John Reck68234a92012-04-19 15:27:12 -07001855 }
John Reck68234a92012-04-19 15:27:12 -07001856 } catch (Exception e) {
1857 Log.w(LOGTAG, "Failed to save view state", e);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001858 String path = callback.getPath();
1859 if (path != null) {
1860 File file = mContext.getFileStreamPath(path);
1861 if (file.exists() && !file.delete()) {
1862 file.deleteOnExit();
1863 }
John Reck68234a92012-04-19 15:27:12 -07001864 }
1865 return false;
1866 }
Tarun Nainani8eb00912014-07-17 12:28:32 -07001867
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001868 String path = callback.getPath();
Tarun Nainani8eb00912014-07-17 12:28:32 -07001869 // could be that saving of file failed
1870 if (path == null) {
1871 return false;
1872 }
1873
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001874 File savedFile = new File(path);
1875 if (!savedFile.exists()) {
1876 return false;
John Reck68234a92012-04-19 15:27:12 -07001877 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001878 values.put(Snapshots.VIEWSTATE_PATH, path.substring(path.lastIndexOf('/') + 1));
1879 values.put(Snapshots.VIEWSTATE_SIZE, savedFile.length());
John Reck68234a92012-04-19 15:27:12 -07001880 return true;
1881 }
1882
John Reck8cc92352011-07-06 17:41:52 -07001883 public byte[] compressBitmap(Bitmap bitmap) {
1884 if (bitmap == null) {
1885 return null;
1886 }
1887 ByteArrayOutputStream stream = new ByteArrayOutputStream();
1888 bitmap.compress(CompressFormat.PNG, 100, stream);
1889 return stream.toByteArray();
1890 }
1891
John Reck26b18322011-06-21 13:08:58 -07001892 public void loadUrl(String url, Map<String, String> headers) {
1893 if (mMainView != null) {
Michael Kolba53c9892011-10-05 13:31:40 -07001894 mPageLoadProgress = INITIAL_PROGRESS;
Michael Kolb14612442011-06-24 13:06:29 -07001895 mCurrentState = new PageState(mContext, false, url, null);
John Reck26b18322011-06-21 13:08:58 -07001896 mMainView.loadUrl(url, headers);
1897 }
1898 }
1899
John Reck38b39652012-06-05 09:22:59 -07001900 public void disableUrlOverridingForLoad() {
1901 mDisableOverrideUrlLoading = true;
1902 }
1903
Michael Kolb9ef259a2011-07-12 15:33:08 -07001904 protected void capture() {
Vivek Sekhar6bdf6452015-05-12 17:38:45 -07001905 boolean returnEmptyCapture = false;
1906 if (mMainView == null || mCapture == null || !mMainView.isReady())
1907 returnEmptyCapture = true;
John Reck4eadc342011-10-31 14:04:10 -07001908 if (mMainView.getContentWidth() <= 0 || mMainView.getContentHeight() <= 0) {
Vivek Sekhar6bdf6452015-05-12 17:38:45 -07001909 returnEmptyCapture = true;
John Reck4eadc342011-10-31 14:04:10 -07001910 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07001911
Vivek Sekhar6bdf6452015-05-12 17:38:45 -07001912 if (returnEmptyCapture || !mFirstVisualPixelPainted || mMainView.isShowingCrashView()) {
Pankaj Garg79878492015-04-01 14:48:21 -07001913 mCapture = Bitmap.createBitmap(
1914 mCaptureWidth,
1915 mCaptureHeight,
1916 Bitmap.Config.RGB_565);
1917 mCapture.eraseColor(Color.WHITE);
1918
1919 mHandler.removeMessages(MSG_CAPTURE);
1920
1921 TabControl tc = mWebViewController.getTabControl();
1922 if (tc != null) {
1923 OnThumbnailUpdatedListener updateListener
1924 = tc.getOnThumbnailUpdatedListener();
1925 if (updateListener != null) {
1926 updateListener.onThumbnailUpdated(this);
1927 }
1928 }
1929 return;
1930 }
1931
Tarun Nainaniea28dde2014-08-27 17:25:09 -07001932 mMainView
1933 .getContentBitmapAsync(
1934 (float) mCaptureWidth / mMainView.getWidth(),
1935 new Rect(),
1936 new ValueCallback<Bitmap>() {
1937 @Override
1938 public void onReceiveValue(Bitmap bitmap) {
1939 onCaptureCallback(bitmap);
1940 }});
1941 }
1942
1943 private void onCaptureCallback(Bitmap bitmap) {
1944 if (mCapture == null || bitmap == null)
1945 return;
1946
Michael Kolb9ef259a2011-07-12 15:33:08 -07001947 Canvas c = new Canvas(mCapture);
Tarun Nainaniea28dde2014-08-27 17:25:09 -07001948 mCapture.eraseColor(Color.WHITE);
1949 c.drawBitmap(bitmap, 0, 0, null);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001950
Michael Kolba3194d02011-09-07 11:23:51 -07001951 // manually anti-alias the edges for the tilt
1952 c.drawRect(0, 0, 1, mCapture.getHeight(), sAlphaPaint);
1953 c.drawRect(mCapture.getWidth() - 1, 0, mCapture.getWidth(),
1954 mCapture.getHeight(), sAlphaPaint);
1955 c.drawRect(0, 0, mCapture.getWidth(), 1, sAlphaPaint);
1956 c.drawRect(0, mCapture.getHeight() - 1, mCapture.getWidth(),
1957 mCapture.getHeight(), sAlphaPaint);
Dianne Hackborn43cfe8a2011-08-02 16:59:35 -07001958 c.setBitmap(null);
John Reck8ee633f2011-08-09 16:00:35 -07001959 mHandler.removeMessages(MSG_CAPTURE);
John Reck1cf4b792011-07-26 10:22:22 -07001960 persistThumbnail();
John Reck8ee633f2011-08-09 16:00:35 -07001961 TabControl tc = mWebViewController.getTabControl();
1962 if (tc != null) {
1963 OnThumbnailUpdatedListener updateListener
1964 = tc.getOnThumbnailUpdatedListener();
1965 if (updateListener != null) {
1966 updateListener.onThumbnailUpdated(this);
1967 }
1968 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001969 }
1970
1971 @Override
1972 public void onNewPicture(WebView view, Picture picture) {
John Reck1cf4b792011-07-26 10:22:22 -07001973 postCapture();
1974 }
1975
1976 private void postCapture() {
Michael Kolb9ef259a2011-07-12 15:33:08 -07001977 if (!mHandler.hasMessages(MSG_CAPTURE)) {
1978 mHandler.sendEmptyMessageDelayed(MSG_CAPTURE, CAPTURE_DELAY);
1979 }
1980 }
1981
John Reckef654f12011-07-12 16:42:08 -07001982 public boolean canGoBack() {
1983 return mMainView != null ? mMainView.canGoBack() : false;
1984 }
1985
1986 public boolean canGoForward() {
1987 return mMainView != null ? mMainView.canGoForward() : false;
1988 }
1989
1990 public void goBack() {
1991 if (mMainView != null) {
1992 mMainView.goBack();
1993 }
1994 }
1995
1996 public void goForward() {
1997 if (mMainView != null) {
1998 mMainView.goForward();
1999 }
2000 }
2001
John Reck1cf4b792011-07-26 10:22:22 -07002002 protected void persistThumbnail() {
John Reck4eadc342011-10-31 14:04:10 -07002003 DataController.getInstance(mContext).saveThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07002004 }
2005
2006 protected void deleteThumbnail() {
John Reck4eadc342011-10-31 14:04:10 -07002007 DataController.getInstance(mContext).deleteThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07002008 }
2009
John Reck4eadc342011-10-31 14:04:10 -07002010 void updateCaptureFromBlob(byte[] blob) {
John Reck1cf4b792011-07-26 10:22:22 -07002011 synchronized (Tab.this) {
2012 if (mCapture == null) {
2013 return;
2014 }
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002015 ByteBuffer buffer = ByteBuffer.wrap(blob);
Michael Kolbd837a112011-08-09 14:00:18 -07002016 try {
2017 mCapture.copyPixelsFromBuffer(buffer);
2018 } catch (RuntimeException rex) {
2019 Log.e(LOGTAG, "Load capture has mismatched sizes; buffer: "
2020 + buffer.capacity() + " blob: " + blob.length
2021 + "capture: " + mCapture.getByteCount());
2022 throw rex;
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002023 }
John Reck1cf4b792011-07-26 10:22:22 -07002024 }
2025 }
2026
John Reck52be4782011-08-26 15:37:29 -07002027 @Override
2028 public String toString() {
2029 StringBuilder builder = new StringBuilder(100);
2030 builder.append(mId);
2031 builder.append(") has parent: ");
2032 if (getParent() != null) {
2033 builder.append("true[");
2034 builder.append(getParent().getId());
2035 builder.append("]");
2036 } else {
2037 builder.append("false");
2038 }
2039 builder.append(", incog: ");
2040 builder.append(isPrivateBrowsingEnabled());
2041 if (!isPrivateBrowsingEnabled()) {
2042 builder.append(", title: ");
2043 builder.append(getTitle());
2044 builder.append(", url: ");
2045 builder.append(getUrl());
2046 }
2047 return builder.toString();
2048 }
2049
Steve Block4895b012011-10-03 16:26:46 +01002050 private void handleProceededAfterSslError(SslError error) {
2051 if (error.getUrl().equals(mCurrentState.mUrl)) {
2052 // The security state should currently be SECURITY_STATE_SECURE.
2053 setSecurityState(SecurityState.SECURITY_STATE_BAD_CERTIFICATE);
Steve Block08a6f0c2011-10-06 12:12:53 +01002054 mCurrentState.mSslCertificateError = error;
Steve Block4895b012011-10-03 16:26:46 +01002055 } else if (getSecurityState() == SecurityState.SECURITY_STATE_SECURE) {
Steve Block08a6f0c2011-10-06 12:12:53 +01002056 // The page's main resource is secure and this error is for a
2057 // sub-resource.
Steve Block4895b012011-10-03 16:26:46 +01002058 setSecurityState(SecurityState.SECURITY_STATE_MIXED);
2059 }
2060 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002061}