blob: ccaa522a3c0e6164fa6eb110e837897a3ab5cd6e [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;
Pankaj Garg1c13cab2015-05-12 11:52:17 -070026import android.content.res.Configuration;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080027import android.content.res.Resources;
Grace Kloba22ac16e2009-10-07 18:00:23 -070028import android.graphics.Bitmap;
John Reck8cc92352011-07-06 17:41:52 -070029import android.graphics.Bitmap.CompressFormat;
Michael Kolb9ef259a2011-07-12 15:33:08 -070030import android.graphics.BitmapFactory;
31import android.graphics.Canvas;
Michael Kolbc3af0672011-08-09 10:24:41 -070032import android.graphics.Color;
Michael Kolba3194d02011-09-07 11:23:51 -070033import android.graphics.Paint;
Michael Kolb9ef259a2011-07-12 15:33:08 -070034import android.graphics.Picture;
Michael Kolba3194d02011-09-07 11:23:51 -070035import android.graphics.PorterDuff;
36import android.graphics.PorterDuffXfermode;
Tarun Nainaniea28dde2014-08-27 17:25:09 -070037import android.graphics.Rect;
Grace Kloba22ac16e2009-10-07 18:00:23 -070038import android.net.Uri;
39import android.net.http.SslError;
Grace Kloba22ac16e2009-10-07 18:00:23 -070040import android.os.Bundle;
Michael Kolb9ef259a2011-07-12 15:33:08 -070041import android.os.Handler;
Grace Kloba22ac16e2009-10-07 18:00:23 -070042import android.os.Message;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +000043import android.os.SystemClock;
Brian Carlstrom8862c1d2011-06-02 01:05:55 -070044import android.security.KeyChain;
Brian Carlstromaa09cd82011-06-09 16:04:40 -070045import android.security.KeyChainAliasCallback;
John Reck24f18262011-06-17 14:47:20 -070046import android.text.TextUtils;
Grace Kloba22ac16e2009-10-07 18:00:23 -070047import android.util.Log;
48import android.view.KeyEvent;
49import android.view.LayoutInflater;
50import android.view.View;
Grace Kloba50c241e2010-04-20 11:07:50 -070051import android.view.ViewStub;
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;
Grace Kloba22ac16e2009-10-07 18:00:23 -070061
Bijan Amirzada41242f22014-03-21 12:12:18 -070062import com.android.browser.TabControl.OnThumbnailUpdatedListener;
63import com.android.browser.homepages.HomeProvider;
64import com.android.browser.mynavigation.MyNavigationUtil;
65import com.android.browser.provider.MyNavigationProvider;
66import com.android.browser.provider.SnapshotProvider.Snapshots;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080067
68import org.codeaurora.swe.BrowserDownloadListener;
69import org.codeaurora.swe.ClientCertRequestHandler;
70import org.codeaurora.swe.HttpAuthHandler;
71import org.codeaurora.swe.SslErrorHandler;
72import org.codeaurora.swe.WebBackForwardList;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080073import org.codeaurora.swe.WebChromeClient;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080074import org.codeaurora.swe.WebView;
75import org.codeaurora.swe.WebView.PictureListener;
Pankaj Garg1c7380d2014-08-27 14:17:12 -070076import org.codeaurora.swe.WebView.CreateWindowParams;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080077import org.codeaurora.swe.WebViewClient;
Pankaj Garg1c13cab2015-05-12 11:52:17 -070078import org.codeaurora.swe.util.Observable;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080079
John Reck541f55a2011-06-07 16:34:43 -070080import java.io.ByteArrayOutputStream;
John Reck2b71d6d2012-04-18 17:42:06 -070081import java.io.File;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080082import java.io.InputStream;
John Reck1cf4b792011-07-26 10:22:22 -070083import java.nio.ByteBuffer;
Michael Kolbfe251992010-07-08 15:41:55 -070084import java.util.Map;
John Reck2b71d6d2012-04-18 17:42:06 -070085import java.util.UUID;
Michael Kolbfe251992010-07-08 15:41:55 -070086import java.util.Vector;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080087import java.sql.Timestamp;
88import java.util.Date;
Michael Kolbfe251992010-07-08 15:41:55 -070089
Grace Kloba22ac16e2009-10-07 18:00:23 -070090/**
91 * Class for maintaining Tabs with a main WebView and a subwindow.
92 */
Michael Kolb9ef259a2011-07-12 15:33:08 -070093class Tab implements PictureListener {
Michael Kolb8233fac2010-10-26 16:08:53 -070094
Grace Kloba22ac16e2009-10-07 18:00:23 -070095 // Log Tag
96 private static final String LOGTAG = "Tab";
Bijan Amirzada41242f22014-03-21 12:12:18 -070097 private static final boolean LOGD_ENABLED = com.android.browser.Browser.LOGD_ENABLED;
Ben Murdochc42addf2010-01-28 15:19:59 +000098 // Special case the logtag for messages for the Console to make it easier to
99 // filter them and match the logtag used for these messages in older versions
100 // of the browser.
101 private static final String CONSOLE_LOGTAG = "browser";
102
Michael Kolb9ef259a2011-07-12 15:33:08 -0700103 private static final int MSG_CAPTURE = 42;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800104 private static final int CAPTURE_DELAY = 1000;
Michael Kolba53c9892011-10-05 13:31:40 -0700105 private static final int INITIAL_PROGRESS = 5;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700106
John Reck1cf4b792011-07-26 10:22:22 -0700107 private static Bitmap sDefaultFavicon;
108
Michael Kolba3194d02011-09-07 11:23:51 -0700109 private static Paint sAlphaPaint = new Paint();
110 static {
111 sAlphaPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
112 sAlphaPaint.setColor(Color.TRANSPARENT);
113 }
114
Steve Block2466eff2011-10-03 15:33:09 +0100115 public enum SecurityState {
Steve Block4895b012011-10-03 16:26:46 +0100116 // The page's main resource does not use SSL. Note that we use this
117 // state irrespective of the SSL authentication state of sub-resources.
Steve Block2466eff2011-10-03 15:33:09 +0100118 SECURITY_STATE_NOT_SECURE,
Steve Block4895b012011-10-03 16:26:46 +0100119 // The page's main resource uses SSL and the certificate is good. The
120 // same is true of all sub-resources.
Steve Block2466eff2011-10-03 15:33:09 +0100121 SECURITY_STATE_SECURE,
Steve Block4895b012011-10-03 16:26:46 +0100122 // The page's main resource uses SSL and the certificate is good, but
123 // some sub-resources either do not use SSL or have problems with their
124 // certificates.
Steve Block2466eff2011-10-03 15:33:09 +0100125 SECURITY_STATE_MIXED,
Steve Block4895b012011-10-03 16:26:46 +0100126 // The page's main resource uses SSL but there is a problem with its
127 // certificate.
128 SECURITY_STATE_BAD_CERTIFICATE,
John Reck30c714c2010-12-16 17:30:34 -0800129 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700130
Michael Kolb14612442011-06-24 13:06:29 -0700131 Context mContext;
John Reckd8c74522011-06-14 08:45:00 -0700132 protected WebViewController mWebViewController;
Michael Kolb8233fac2010-10-26 16:08:53 -0700133
Michael Kolbc831b632011-05-11 09:30:34 -0700134 // The tab ID
John Reckd8c74522011-06-14 08:45:00 -0700135 private long mId = -1;
Michael Kolbc831b632011-05-11 09:30:34 -0700136
Grace Kloba22ac16e2009-10-07 18:00:23 -0700137 // The Geolocation permissions prompt
138 private GeolocationPermissionsPrompt mGeolocationPermissionsPrompt;
139 // Main WebView wrapper
Michael Kolba713ec82010-11-29 17:27:06 -0800140 private View mContainer;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700141 // Main WebView
142 private WebView mMainView;
143 // Subwindow container
144 private View mSubViewContainer;
145 // Subwindow WebView
146 private WebView mSubView;
147 // Saved bundle for when we are running low on memory. It contains the
148 // information needed to restore the WebView if the user goes back to the
149 // tab.
150 private Bundle mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700151 // Parent Tab. This is the Tab that created this Tab, or null if the Tab was
152 // created by the UI
Michael Kolbc831b632011-05-11 09:30:34 -0700153 private Tab mParent;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700154 // Tab that constructed by this Tab. This is used when this Tab is
155 // destroyed, it clears all mParentTab values in the children.
Michael Kolbc831b632011-05-11 09:30:34 -0700156 private Vector<Tab> mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700157 // If true, the tab is in the foreground of the current activity.
158 private boolean mInForeground;
Michael Kolb8233fac2010-10-26 16:08:53 -0700159 // If true, the tab is in page loading state (after onPageStarted,
160 // before onPageFinsihed)
161 private boolean mInPageLoad;
Tarun Nainani8eb00912014-07-17 12:28:32 -0700162 private boolean mPageFinished;
John Reck38b39652012-06-05 09:22:59 -0700163 private boolean mDisableOverrideUrlLoading;
Pankaj Garg79878492015-04-01 14:48:21 -0700164 private boolean mFirstVisualPixelPainted = false;
John Reck30c714c2010-12-16 17:30:34 -0800165 // The last reported progress of the current page
166 private int mPageLoadProgress;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000167 // The time the load started, used to find load page time
168 private long mLoadStartTime;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700169 // Application identifier used to find tabs that another application wants
170 // to reuse.
171 private String mAppId;
Michael Kolbe28b3472011-08-04 16:54:31 -0700172 // flag to indicate if tab should be closed on back
173 private boolean mCloseOnBack;
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700174 // flag to indicate if the tab was opened from an intent
175 private boolean mDerivedFromIntent = false;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -0500176 // The listener that gets invoked when a download is started from the
177 // mMainView
Selim Gurun0b3d66f2012-08-29 13:08:13 -0700178 private final BrowserDownloadListener mDownloadListener;
John Recke969cc52010-12-21 17:24:43 -0800179 private DataController mDataController;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700180
181 // AsyncTask for downloading touch icons
182 DownloadTouchIcon mTouchIconLoader;
183
John Reck35e9dd62011-04-25 09:01:54 -0700184 private BrowserSettings mSettings;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700185 private int mCaptureWidth;
186 private int mCaptureHeight;
187 private Bitmap mCapture;
188 private Handler mHandler;
Michael Kolb72864272012-05-03 15:42:15 -0700189 private boolean mUpdateThumbnail;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800190 private Timestamp timestamp;
Vivek Sekharf96064b2014-07-28 16:32:34 -0700191 private boolean mFullScreen = false;
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800192 private boolean mReceivedError;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700193
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -0700194 // determine if webview is destroyed to MemoryMonitor
195 private boolean mWebViewDestroyedByMemoryMonitor;
196
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700197 private Observable mFirstPixelObservable;
198 private Observable mTabHistoryUpdateObservable;
199
200 Observable getFirstPixelObservable() {
201 return mFirstPixelObservable;
202 }
203
204 Observable getTabHistoryUpdateObservable() {
205 return mTabHistoryUpdateObservable;
206 }
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -0700207
Mathew Inwood1dd8e822011-08-03 14:34:29 +0100208
John Reck1cf4b792011-07-26 10:22:22 -0700209 private static synchronized Bitmap getDefaultFavicon(Context context) {
210 if (sDefaultFavicon == null) {
211 sDefaultFavicon = BitmapFactory.decodeResource(
Enrico Rosd6efa972014-12-02 19:49:59 -0800212 context.getResources(), R.drawable.ic_deco_favicon_normal);
John Reck1cf4b792011-07-26 10:22:22 -0700213 }
214 return sDefaultFavicon;
215 }
Michael Kolbeb95db42011-03-03 10:38:40 -0800216
John Reck30c714c2010-12-16 17:30:34 -0800217 // All the state needed for a page
John Reckd8c74522011-06-14 08:45:00 -0700218 protected static class PageState {
John Reck30c714c2010-12-16 17:30:34 -0800219 String mUrl;
John Reckdb22ec42011-06-29 11:31:24 -0700220 String mOriginalUrl;
John Reck30c714c2010-12-16 17:30:34 -0800221 String mTitle;
Steve Block2466eff2011-10-03 15:33:09 +0100222 SecurityState mSecurityState;
Steve Block08a6f0c2011-10-06 12:12:53 +0100223 // This is non-null only when mSecurityState is SECURITY_STATE_BAD_CERTIFICATE.
224 SslError mSslCertificateError;
John Reck30c714c2010-12-16 17:30:34 -0800225 Bitmap mFavicon;
Steve Block08a6f0c2011-10-06 12:12:53 +0100226 boolean mIsBookmarkedSite;
227 boolean mIncognito;
John Reck30c714c2010-12-16 17:30:34 -0800228
229 PageState(Context c, boolean incognito) {
John Reck502a3532011-08-16 14:21:46 -0700230 mIncognito = incognito;
231 if (mIncognito) {
Vivek Sekhared791da2015-02-22 12:39:05 -0800232 mOriginalUrl = mUrl = "chrome://incognito";
John Reck30c714c2010-12-16 17:30:34 -0800233 mTitle = c.getString(R.string.new_incognito_tab);
John Reck30c714c2010-12-16 17:30:34 -0800234 } else {
John Reckdb22ec42011-06-29 11:31:24 -0700235 mOriginalUrl = mUrl = "";
John Reck30c714c2010-12-16 17:30:34 -0800236 mTitle = c.getString(R.string.new_tab);
John Reck30c714c2010-12-16 17:30:34 -0800237 }
Steve Block2466eff2011-10-03 15:33:09 +0100238 mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
John Reck30c714c2010-12-16 17:30:34 -0800239 }
240
241 PageState(Context c, boolean incognito, String url, Bitmap favicon) {
John Reck502a3532011-08-16 14:21:46 -0700242 mIncognito = incognito;
John Reckdb22ec42011-06-29 11:31:24 -0700243 mOriginalUrl = mUrl = url;
John Reck30c714c2010-12-16 17:30:34 -0800244 if (URLUtil.isHttpsUrl(url)) {
Steve Block2466eff2011-10-03 15:33:09 +0100245 mSecurityState = SecurityState.SECURITY_STATE_SECURE;
John Reck30c714c2010-12-16 17:30:34 -0800246 } else {
Steve Block2466eff2011-10-03 15:33:09 +0100247 mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
John Reck30c714c2010-12-16 17:30:34 -0800248 }
John Reck1cf4b792011-07-26 10:22:22 -0700249 mFavicon = favicon;
John Reck30c714c2010-12-16 17:30:34 -0800250 }
John Reck1cf4b792011-07-26 10:22:22 -0700251
Grace Kloba22ac16e2009-10-07 18:00:23 -0700252 }
253
John Reck30c714c2010-12-16 17:30:34 -0800254 // The current/loading page's state
John Reckd8c74522011-06-14 08:45:00 -0700255 protected PageState mCurrentState;
John Reck30c714c2010-12-16 17:30:34 -0800256
Grace Kloba22ac16e2009-10-07 18:00:23 -0700257 // Used for saving and restoring each Tab
Michael Kolbc831b632011-05-11 09:30:34 -0700258 static final String ID = "ID";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700259 static final String CURRURL = "currentUrl";
260 static final String CURRTITLE = "currentTitle";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700261 static final String PARENTTAB = "parentTab";
262 static final String APPID = "appid";
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700263 static final String INCOGNITO = "privateBrowsingEnabled";
John Reckb0a86db2011-05-24 14:05:58 -0700264 static final String USERAGENT = "useragent";
Michael Kolbe28b3472011-08-04 16:54:31 -0700265 static final String CLOSEFLAG = "closeOnBack";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700266
Pankaj Garg18186a92015-03-31 14:59:33 -0700267 public void setNetworkAvailable(boolean networkUp) {
268 if (networkUp && mReceivedError && (mMainView != null)) {
269 mMainView.reload();
270 }
271 }
272
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700273 public boolean isFirstVisualPixelPainted() {
274 return mFirstVisualPixelPainted;
275 }
276
277 public int getCaptureIndex(int navIndex) {
278 int tabPosition = mWebViewController.getTabControl().getCurrentPosition();
279 int orientation = mWebViewController.getActivity().
280 getResources().getConfiguration().orientation;
281
282 int orientationBit = (orientation == Configuration.ORIENTATION_LANDSCAPE) ? 0 : 1;
283
284 int index = orientationBit << 31 | ((tabPosition & 0x7f) << 24) | (navIndex & 0xffffff);
285 return index;
286 }
287
288 public int getTabIdxFromCaptureIdx(int index) {
289 return (index & 0x7f000000) >> 24;
290 }
291
292 public int getOrientationFromCaptureIdx(int index) {
293 return ((index & 0x80000000) == 0) ? Configuration.ORIENTATION_LANDSCAPE :
294 Configuration.ORIENTATION_PORTRAIT;
295
296 }
297
298 public int getNavIdxFromCaptureIdx(int index) {
299 return (index & 0xffffff);
300 }
301
Grace Kloba22ac16e2009-10-07 18:00:23 -0700302 // -------------------------------------------------------------------------
303 // WebViewClient implementation for the main WebView
304 // -------------------------------------------------------------------------
305
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800306 private final WebViewClient mWebViewClient = new WebViewClient() {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500307 private Message mDontResend;
308 private Message mResend;
Michael Kolb47bd1e42011-09-01 15:25:00 -0700309
310 private boolean providersDiffer(String url, String otherUrl) {
311 Uri uri1 = Uri.parse(url);
312 Uri uri2 = Uri.parse(otherUrl);
313 return !uri1.getEncodedAuthority().equals(uri2.getEncodedAuthority());
314 }
315
Grace Kloba22ac16e2009-10-07 18:00:23 -0700316 @Override
317 public void onPageStarted(WebView view, String url, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700318 mInPageLoad = true;
Tarun Nainani8eb00912014-07-17 12:28:32 -0700319 mPageFinished = false;
Pankaj Garg79878492015-04-01 14:48:21 -0700320 mFirstVisualPixelPainted = false;
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700321 mFirstPixelObservable.set(false);
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800322 mReceivedError = false;
Michael Kolb72864272012-05-03 15:42:15 -0700323 mUpdateThumbnail = true;
Michael Kolba53c9892011-10-05 13:31:40 -0700324 mPageLoadProgress = INITIAL_PROGRESS;
Michael Kolb14612442011-06-24 13:06:29 -0700325 mCurrentState = new PageState(mContext,
John Reck30c714c2010-12-16 17:30:34 -0800326 view.isPrivateBrowsingEnabled(), url, favicon);
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000327 mLoadStartTime = SystemClock.uptimeMillis();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700328
329 // If we start a touch icon load and then load a new page, we don't
330 // want to cancel the current touch icon loader. But, we do want to
331 // create a new one when the touch icon url is known.
332 if (mTouchIconLoader != null) {
333 mTouchIconLoader.mTab = null;
334 mTouchIconLoader = null;
335 }
336
Panos Thomasb298aad2014-10-22 12:24:21 -0700337 // Loading a new page voids any ongoing Geolocation permission
338 // requests.
339 if (mGeolocationPermissionsPrompt != null) {
340 mGeolocationPermissionsPrompt.dismiss();
341 }
342
Grace Kloba22ac16e2009-10-07 18:00:23 -0700343 // finally update the UI in the activity if it is in the foreground
John Reck324d4402011-01-11 16:56:42 -0800344 mWebViewController.onPageStarted(Tab.this, view, favicon);
Leon Scroggins4cd97792010-12-03 15:31:56 -0500345
John Recke969cc52010-12-21 17:24:43 -0800346 updateBookmarkedStatus();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700347 }
348
349 @Override
350 public void onPageFinished(WebView view, String url) {
John Reck38b39652012-06-05 09:22:59 -0700351 mDisableOverrideUrlLoading = false;
John Reck5b691842010-11-29 11:21:13 -0800352 if (!isPrivateBrowsingEnabled()) {
353 LogTag.logPageFinishedLoading(
354 url, SystemClock.uptimeMillis() - mLoadStartTime);
355 }
John Reck1cf4b792011-07-26 10:22:22 -0700356 syncCurrentState(view, url);
John Reck324d4402011-01-11 16:56:42 -0800357 mWebViewController.onPageFinished(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700358 }
359
Pankaj Garg79878492015-04-01 14:48:21 -0700360 @Override
361 public void onFirstVisualPixel(WebView view) {
362 mFirstVisualPixelPainted = true;
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700363 mFirstPixelObservable.set(true);
Pankaj Garg79878492015-04-01 14:48:21 -0700364 }
365
Grace Kloba22ac16e2009-10-07 18:00:23 -0700366 // return true if want to hijack the url to let another app to handle it
367 @Override
368 public boolean shouldOverrideUrlLoading(WebView view, String url) {
John Reck38b39652012-06-05 09:22:59 -0700369 if (!mDisableOverrideUrlLoading && mInForeground) {
Michael Kolb18eb3772010-12-10 14:29:51 -0800370 return mWebViewController.shouldOverrideUrlLoading(Tab.this,
371 view, url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700372 } else {
373 return false;
374 }
375 }
376
Vivek Sekharb991edb2014-12-17 18:18:07 -0800377 @Override
378 public boolean shouldDownloadFavicon(WebView view, String url) {
379 return true;
380 }
381
Grace Kloba22ac16e2009-10-07 18:00:23 -0700382 /**
Steve Block2466eff2011-10-03 15:33:09 +0100383 * Updates the security state. This method is called when we discover
384 * another resource to be loaded for this page (for example,
385 * javascript). While we update the security state, we do not update
386 * the lock icon until we are done loading, as it is slightly more
387 * secure this way.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700388 */
389 @Override
390 public void onLoadResource(WebView view, String url) {
391 if (url != null && url.length() > 0) {
392 // It is only if the page claims to be secure that we may have
Steve Block2466eff2011-10-03 15:33:09 +0100393 // to update the security state:
394 if (mCurrentState.mSecurityState == SecurityState.SECURITY_STATE_SECURE) {
395 // If NOT a 'safe' url, change the state to mixed content!
Grace Kloba22ac16e2009-10-07 18:00:23 -0700396 if (!(URLUtil.isHttpsUrl(url) || URLUtil.isDataUrl(url)
397 || URLUtil.isAboutUrl(url))) {
Steve Block2466eff2011-10-03 15:33:09 +0100398 mCurrentState.mSecurityState = SecurityState.SECURITY_STATE_MIXED;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700399 }
400 }
401 }
402 }
403
404 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700405 * Show a dialog informing the user of the network error reported by
406 * WebCore if it is in the foreground.
407 */
408 @Override
409 public void onReceivedError(WebView view, int errorCode,
410 String description, String failingUrl) {
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800411 // Used for the syncCurrentState to use
412 // the failing url instead of using webview url
413 mReceivedError = true;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700414 }
415
416 /**
417 * Check with the user if it is ok to resend POST data as the page they
418 * are trying to navigate to is the result of a POST.
419 */
420 @Override
421 public void onFormResubmission(WebView view, final Message dontResend,
422 final Message resend) {
423 if (!mInForeground) {
424 dontResend.sendToTarget();
425 return;
426 }
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500427 if (mDontResend != null) {
428 Log.w(LOGTAG, "onFormResubmission should not be called again "
429 + "while dialog is still up");
430 dontResend.sendToTarget();
431 return;
432 }
433 mDontResend = dontResend;
434 mResend = resend;
Michael Kolb14612442011-06-24 13:06:29 -0700435 new AlertDialog.Builder(mContext).setTitle(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700436 R.string.browserFrameFormResubmitLabel).setMessage(
437 R.string.browserFrameFormResubmitMessage)
438 .setPositiveButton(R.string.ok,
439 new DialogInterface.OnClickListener() {
440 public void onClick(DialogInterface dialog,
441 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500442 if (mResend != null) {
443 mResend.sendToTarget();
444 mResend = null;
445 mDontResend = null;
446 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700447 }
448 }).setNegativeButton(R.string.cancel,
449 new DialogInterface.OnClickListener() {
450 public void onClick(DialogInterface dialog,
451 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500452 if (mDontResend != null) {
453 mDontResend.sendToTarget();
454 mResend = null;
455 mDontResend = null;
456 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700457 }
458 }).setOnCancelListener(new OnCancelListener() {
459 public void onCancel(DialogInterface dialog) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500460 if (mDontResend != null) {
461 mDontResend.sendToTarget();
462 mResend = null;
463 mDontResend = null;
464 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700465 }
466 }).show();
467 }
468
469 /**
470 * Insert the url into the visited history database.
471 * @param url The url to be inserted.
472 * @param isReload True if this url is being reloaded.
473 * FIXME: Not sure what to do when reloading the page.
474 */
475 @Override
476 public void doUpdateVisitedHistory(WebView view, String url,
477 boolean isReload) {
John Reck324d4402011-01-11 16:56:42 -0800478 mWebViewController.doUpdateVisitedHistory(Tab.this, isReload);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700479 }
480
481 /**
482 * Displays SSL error(s) dialog to the user.
483 */
484 @Override
485 public void onReceivedSslError(final WebView view,
486 final SslErrorHandler handler, final SslError error) {
487 if (!mInForeground) {
488 handler.cancel();
Steve Block2466eff2011-10-03 15:33:09 +0100489 setSecurityState(SecurityState.SECURITY_STATE_NOT_SECURE);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700490 return;
491 }
John Reck35e9dd62011-04-25 09:01:54 -0700492 if (mSettings.showSecurityWarnings()) {
John Reckcb28b2c2011-08-26 17:39:44 -0700493 new AlertDialog.Builder(mContext)
494 .setTitle(R.string.security_warning)
495 .setMessage(R.string.ssl_warnings_header)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200496 .setIconAttribute(android.R.attr.alertDialogIcon)
John Reckcb28b2c2011-08-26 17:39:44 -0700497 .setPositiveButton(R.string.ssl_continue,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700498 new DialogInterface.OnClickListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700499 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700500 public void onClick(DialogInterface dialog,
501 int whichButton) {
502 handler.proceed();
Steve Block4895b012011-10-03 16:26:46 +0100503 handleProceededAfterSslError(error);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700504 }
John Reckcb28b2c2011-08-26 17:39:44 -0700505 })
506 .setNeutralButton(R.string.view_certificate,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700507 new DialogInterface.OnClickListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700508 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700509 public void onClick(DialogInterface dialog,
510 int whichButton) {
John Reckcb28b2c2011-08-26 17:39:44 -0700511 mWebViewController.showSslCertificateOnError(
512 view, handler, error);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700513 }
John Reckcb28b2c2011-08-26 17:39:44 -0700514 })
515 .setNegativeButton(R.string.ssl_go_back,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700516 new DialogInterface.OnClickListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700517 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700518 public void onClick(DialogInterface dialog,
519 int whichButton) {
John Reck30c714c2010-12-16 17:30:34 -0800520 dialog.cancel();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700521 }
John Reckcb28b2c2011-08-26 17:39:44 -0700522 })
523 .setOnCancelListener(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700524 new DialogInterface.OnCancelListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700525 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700526 public void onCancel(DialogInterface dialog) {
527 handler.cancel();
Steve Block2466eff2011-10-03 15:33:09 +0100528 setSecurityState(SecurityState.SECURITY_STATE_NOT_SECURE);
John Reck30c714c2010-12-16 17:30:34 -0800529 mWebViewController.onUserCanceledSsl(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700530 }
John Reckcb28b2c2011-08-26 17:39:44 -0700531 })
532 .show();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700533 } else {
534 handler.proceed();
535 }
536 }
537
538 /**
Steve Block4895b012011-10-03 16:26:46 +0100539 * Called when an SSL error occurred while loading a resource, but the
540 * WebView but chose to proceed anyway based on a decision retained
541 * from a previous response to onReceivedSslError(). We update our
542 * security state to reflect this.
543 */
544 @Override
545 public void onProceededAfterSslError(WebView view, SslError error) {
546 handleProceededAfterSslError(error);
547 }
548
549 /**
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700550 * Displays client certificate request to the user.
551 */
552 @Override
553 public void onReceivedClientCertRequest(final WebView view,
554 final ClientCertRequestHandler handler, final String host_and_port) {
555 if (!mInForeground) {
556 handler.ignore();
557 return;
558 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700559 int colon = host_and_port.lastIndexOf(':');
560 String host;
561 int port;
562 if (colon == -1) {
563 host = host_and_port;
564 port = -1;
565 } else {
566 String portString = host_and_port.substring(colon + 1);
567 try {
568 port = Integer.parseInt(portString);
569 host = host_and_port.substring(0, colon);
570 } catch (NumberFormatException e) {
571 host = host_and_port;
572 port = -1;
573 }
574 }
Michael Kolb14612442011-06-24 13:06:29 -0700575 KeyChain.choosePrivateKeyAlias(
576 mWebViewController.getActivity(), new KeyChainAliasCallback() {
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700577 @Override public void alias(String alias) {
578 if (alias == null) {
579 handler.cancel();
580 return;
581 }
Michael Kolb14612442011-06-24 13:06:29 -0700582 new KeyChainLookup(mContext, handler, alias).execute();
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700583 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700584 }, null, null, host, port, null);
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700585 }
586
587 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700588 * Handles an HTTP authentication request.
589 *
590 * @param handler The authentication handler
591 * @param host The host
592 * @param realm The realm
593 */
594 @Override
595 public void onReceivedHttpAuthRequest(WebView view,
596 final HttpAuthHandler handler, final String host,
597 final String realm) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700598 mWebViewController.onReceivedHttpAuthRequest(Tab.this, view, handler, host, realm);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700599 }
600
601 @Override
John Reck438bf462011-01-12 18:11:46 -0800602 public WebResourceResponse shouldInterceptRequest(WebView view,
603 String url) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800604 //intercept if opening a new incognito tab - show the incognito welcome page
Vivek Sekhared791da2015-02-22 12:39:05 -0800605 if (url.startsWith("chrome://incognito")) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800606 Resources resourceHandle = mContext.getResources();
607 InputStream inStream = resourceHandle.openRawResource(
Bijan Amirzada41242f22014-03-21 12:12:18 -0700608 com.android.browser.R.raw.incognito_mode_start_page);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800609 return new WebResourceResponse("text/html", "utf8", inStream);
610 }
kaiyiz6e5b3e02013-08-19 20:02:01 +0800611 WebResourceResponse res;
612 if (MyNavigationUtil.MY_NAVIGATION.equals(url)) {
613 res = MyNavigationProvider.shouldInterceptRequest(mContext, url);
614 } else {
615 res = HomeProvider.shouldInterceptRequest(mContext, url);
616 }
John Reck438bf462011-01-12 18:11:46 -0800617 return res;
618 }
619
620 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700621 public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
622 if (!mInForeground) {
623 return false;
624 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700625 return mWebViewController.shouldOverrideKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700626 }
627
628 @Override
629 public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700630 if (!mInForeground) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700631 return;
632 }
John Reck997b1b72012-04-19 18:08:25 -0700633 if (!mWebViewController.onUnhandledKeyEvent(event)) {
634 super.onUnhandledKeyEvent(view, event);
635 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700636 }
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700637
638 @Override
639 public void beforeNavigation(WebView view, String url) {
640 if (isPrivateBrowsingEnabled()) {
641 return;
642 }
643
644 if (!mFirstVisualPixelPainted) {
645 return;
646 }
647
648 final int idx = view.copyBackForwardList().getCurrentIndex();
649 boolean bitmapExists = view.hasSnapshot(idx);
650
651 int progress = 100;
652 Controller controller = (Controller)mWebViewController;
653 UI ui = controller.getUi();
654 if (ui instanceof BaseUi) {
655 BaseUi baseUi = (BaseUi) ui;
656 TitleBar titleBar = baseUi.getTitleBar();
657 progress = titleBar.getProgressView().getProgressPercent();
658 }
659
660 if (bitmapExists && progress < 85) {
661 return;
662 }
663
664 int index = getCaptureIndex(view.getLastCommittedHistoryIndex());
665 view.captureSnapshot(index , null);
666 }
667
668 @Override
669 public void onHistoryItemCommit(WebView view, int index) {
670 mTabHistoryUpdateObservable.set(index);
671 int maxIdx = view.copyBackForwardList().getSize();
672 int[] ids = view.getSnapshotIds();
673 int currentTabIdx = mWebViewController.getTabControl().getCurrentPosition();
674 for (int id : ids) {
675 if (getTabIdxFromCaptureIdx(id) == currentTabIdx &&
676 getNavIdxFromCaptureIdx(id) >= maxIdx) {
677 view.deleteSnapshot(id);
678 }
679 }
680 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700681 };
682
John Reck1cf4b792011-07-26 10:22:22 -0700683 private void syncCurrentState(WebView view, String url) {
684 // Sync state (in case of stop/timeout)
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800685
686 if (mReceivedError) {
687 mCurrentState.mUrl = url;
688 mCurrentState.mOriginalUrl = url;
689 } else {
690 mCurrentState.mUrl = view.getUrl();
691 mCurrentState.mOriginalUrl = view.getOriginalUrl();
692 mCurrentState.mFavicon = view.getFavicon();
693 }
694
John Reck1cf4b792011-07-26 10:22:22 -0700695 if (mCurrentState.mUrl == null) {
John Reck8bcafc12011-08-29 16:43:02 -0700696 mCurrentState.mUrl = "";
John Reck1cf4b792011-07-26 10:22:22 -0700697 }
John Reck1cf4b792011-07-26 10:22:22 -0700698 mCurrentState.mTitle = view.getTitle();
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800699
700
John Reck1cf4b792011-07-26 10:22:22 -0700701 if (!URLUtil.isHttpsUrl(mCurrentState.mUrl)) {
702 // In case we stop when loading an HTTPS page from an HTTP page
703 // but before a provisional load occurred
Steve Block2466eff2011-10-03 15:33:09 +0100704 mCurrentState.mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
Steve Block08a6f0c2011-10-06 12:12:53 +0100705 mCurrentState.mSslCertificateError = null;
John Reck1cf4b792011-07-26 10:22:22 -0700706 }
John Reck502a3532011-08-16 14:21:46 -0700707 mCurrentState.mIncognito = view.isPrivateBrowsingEnabled();
John Reck1cf4b792011-07-26 10:22:22 -0700708 }
709
Tarun Nainani8eb00912014-07-17 12:28:32 -0700710
711 public boolean isTabFullScreen() {
712 return mFullScreen;
713 }
714
Vivek Sekharf96064b2014-07-28 16:32:34 -0700715 protected void setTabFullscreen(boolean fullScreen) {
Tarun Nainani8eb00912014-07-17 12:28:32 -0700716 Controller controller = (Controller)mWebViewController;
Sudheer Koganti24766882014-10-02 10:58:09 -0700717 controller.getUi().showFullscreen(fullScreen);
Tarun Nainani8eb00912014-07-17 12:28:32 -0700718 mFullScreen = fullScreen;
Vivek Sekharf96064b2014-07-28 16:32:34 -0700719 }
720
Sudheer Koganti24766882014-10-02 10:58:09 -0700721 public boolean exitFullscreen() {
722 if (mFullScreen) {
723 Controller controller = (Controller)mWebViewController;
724 controller.getUi().showFullscreen(false);
725 if (getWebView() != null)
726 getWebView().exitFullscreen();
727 mFullScreen = false;
728 return true;
729 }
730 return false;
731 }
732
733
734
735
Grace Kloba22ac16e2009-10-07 18:00:23 -0700736 // -------------------------------------------------------------------------
737 // WebChromeClient implementation for the main WebView
738 // -------------------------------------------------------------------------
739
740 private final WebChromeClient mWebChromeClient = new WebChromeClient() {
741 // Helper method to create a new tab or sub window.
742 private void createWindow(final boolean dialog, final Message msg) {
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700743 this.createWindow(dialog, msg, null, false);
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700744 }
745
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700746 private void createWindow(final boolean dialog, final Message msg, final String url,
747 final boolean opener_suppressed) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700748 WebView.WebViewTransport transport =
749 (WebView.WebViewTransport) msg.obj;
750 if (dialog) {
751 createSubWindow();
Michael Kolb8233fac2010-10-26 16:08:53 -0700752 mWebViewController.attachSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700753 transport.setWebView(mSubView);
754 } else {
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700755 final Tab newTab = mWebViewController.openTab(url,
John Reck5949c662011-05-27 09:52:29 -0700756 Tab.this, true, true);
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700757 // This is special case for rendering links on a webpage in
758 // a new tab. If opener is suppressed, the WebContents created
759 // by the content layer are not fully initialized. This check
760 // will prevent content layer from overriding WebContents
761 // created by new tab with the uninitialized instance.
762 if (!opener_suppressed) {
763 transport.setWebView(newTab.getWebView());
764 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700765 }
766 msg.sendToTarget();
767 }
768
769 @Override
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700770 public void toggleFullscreenModeForTab(boolean enterFullscreen) {
771 if (mWebViewController instanceof Controller) {
Vivek Sekharf96064b2014-07-28 16:32:34 -0700772 setTabFullscreen(enterFullscreen);
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700773 }
774 }
775
776 @Override
Tarun Nainani8eb00912014-07-17 12:28:32 -0700777 public void onOffsetsForFullscreenChanged(float topControlsOffsetYPix,
778 float contentOffsetYPix,
779 float overdrawBottomHeightPix) {
780 if (mWebViewController instanceof Controller) {
Vivek Sekhar8ee3abb2014-07-14 12:32:05 -0700781 Controller controller = (Controller)mWebViewController;
Tarun Nainani8eb00912014-07-17 12:28:32 -0700782 controller.getUi().translateTitleBar(topControlsOffsetYPix);
Vivek Sekhar8ee3abb2014-07-14 12:32:05 -0700783 }
784 }
785
786 @Override
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700787 public boolean isTabFullScreen() {
Vivek Sekharf96064b2014-07-28 16:32:34 -0700788 return mFullScreen;
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700789 }
790
791 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700792 public boolean onCreateWindow(WebView view, final boolean dialog,
793 final boolean userGesture, final Message resultMsg) {
794 // only allow new window or sub window for the foreground case
795 if (!mInForeground) {
796 return false;
797 }
798 // Short-circuit if we can't create any more tabs or sub windows.
799 if (dialog && mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -0700800 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700801 .setTitle(R.string.too_many_subwindows_dialog_title)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200802 .setIconAttribute(android.R.attr.alertDialogIcon)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700803 .setMessage(R.string.too_many_subwindows_dialog_message)
804 .setPositiveButton(R.string.ok, null)
805 .show();
806 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700807 } else if (!mWebViewController.getTabControl().canCreateNewTab()) {
Michael Kolb14612442011-06-24 13:06:29 -0700808 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700809 .setTitle(R.string.too_many_windows_dialog_title)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200810 .setIconAttribute(android.R.attr.alertDialogIcon)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700811 .setMessage(R.string.too_many_windows_dialog_message)
812 .setPositiveButton(R.string.ok, null)
813 .show();
814 return false;
815 }
816
817 // Short-circuit if this was a user gesture.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800818 if (userGesture || !mSettings.blockPopupWindows()) {
Tarun Nainani4f5137d2015-04-16 17:26:18 -0700819 WebView.WebViewTransport transport =
820 (WebView.WebViewTransport) resultMsg.obj;
821 CreateWindowParams windowParams = transport.getCreateWindowParams();
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700822 if (windowParams.mOpenerSuppressed) {
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700823 createWindow(dialog, resultMsg, windowParams.mURL, true);
824 // This is special case for rendering links on a webpage in
825 // a new tab. If opener is suppressed, the WebContents created
826 // by the content layer are not fully initialized. Returning false
827 // will prevent content layer from overriding WebContents
828 // created by new tab with the uninitialized instance.
829 return false;
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700830 }
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700831
832 createWindow(dialog, resultMsg);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700833 return true;
834 }
835
Tarun Nainani4f5137d2015-04-16 17:26:18 -0700836 createWindow(dialog, resultMsg);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700837 return true;
838 }
839
840 @Override
Patrick Scotteb5061b2009-11-18 15:00:30 -0500841 public void onRequestFocus(WebView view) {
842 if (!mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700843 mWebViewController.switchToTab(Tab.this);
Patrick Scotteb5061b2009-11-18 15:00:30 -0500844 }
845 }
846
847 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700848 public void onCloseWindow(WebView window) {
Michael Kolbc831b632011-05-11 09:30:34 -0700849 if (mParent != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700850 // JavaScript can only close popup window.
851 if (mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700852 mWebViewController.switchToTab(mParent);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700853 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700854 mWebViewController.closeTab(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700855 }
856 }
857
858 @Override
859 public void onProgressChanged(WebView view, int newProgress) {
John Reck30c714c2010-12-16 17:30:34 -0800860 mPageLoadProgress = newProgress;
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800861 if (newProgress == 100) {
862 mInPageLoad = false;
863 }
John Reck30c714c2010-12-16 17:30:34 -0800864 mWebViewController.onProgressChanged(Tab.this);
Michael Kolb72864272012-05-03 15:42:15 -0700865 if (mUpdateThumbnail && newProgress == 100) {
866 mUpdateThumbnail = false;
867 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700868 }
869
870 @Override
Leon Scroggins21d9b902010-03-11 09:33:11 -0500871 public void onReceivedTitle(WebView view, final String title) {
John Reck30c714c2010-12-16 17:30:34 -0800872 mCurrentState.mTitle = title;
Michael Kolb8233fac2010-10-26 16:08:53 -0700873 mWebViewController.onReceivedTitle(Tab.this, title);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700874 }
875
876 @Override
877 public void onReceivedIcon(WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -0800878 mCurrentState.mFavicon = icon;
Michael Kolb8233fac2010-10-26 16:08:53 -0700879 mWebViewController.onFavicon(Tab.this, view, icon);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700880 }
881
882 @Override
883 public void onReceivedTouchIconUrl(WebView view, String url,
884 boolean precomposed) {
Michael Kolb14612442011-06-24 13:06:29 -0700885 final ContentResolver cr = mContext.getContentResolver();
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400886 // Let precomposed icons take precedence over non-composed
887 // icons.
888 if (precomposed && mTouchIconLoader != null) {
889 mTouchIconLoader.cancel(false);
890 mTouchIconLoader = null;
891 }
892 // Have only one async task at a time.
893 if (mTouchIconLoader == null) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700894 mTouchIconLoader = new DownloadTouchIcon(Tab.this,
Michael Kolb14612442011-06-24 13:06:29 -0700895 mContext, cr, view);
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400896 mTouchIconLoader.execute(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700897 }
898 }
899
900 @Override
901 public void onShowCustomView(View view,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800902 CustomViewCallback callback) {
Michael Kolb14612442011-06-24 13:06:29 -0700903 Activity activity = mWebViewController.getActivity();
904 if (activity != null) {
905 onShowCustomView(view, activity.getRequestedOrientation(), callback);
906 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400907 }
908
909 @Override
910 public void onShowCustomView(View view, int requestedOrientation,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800911 CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700912 if (mInForeground) mWebViewController.showCustomView(Tab.this, view,
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400913 requestedOrientation, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700914 }
915
916 @Override
917 public void onHideCustomView() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700918 if (mInForeground) mWebViewController.hideCustomView();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700919 }
920
921 /**
922 * The origin has exceeded its database quota.
923 * @param url the URL that exceeded the quota
924 * @param databaseIdentifier the identifier of the database on which the
925 * transaction that caused the quota overflow was run
926 * @param currentQuota the current quota for the origin.
927 * @param estimatedSize the estimated size of the database.
928 * @param totalUsedQuota is the sum of all origins' quota.
929 * @param quotaUpdater The callback to run when a decision to allow or
930 * deny quota has been made. Don't forget to call this!
931 */
932 @Override
933 public void onExceededDatabaseQuota(String url,
934 String databaseIdentifier, long currentQuota, long estimatedSize,
935 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -0700936 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -0700937 .onExceededDatabaseQuota(url, databaseIdentifier,
938 currentQuota, estimatedSize, totalUsedQuota,
939 quotaUpdater);
940 }
941
942 /**
943 * The Application Cache has exceeded its max size.
944 * @param spaceNeeded is the amount of disk space that would be needed
945 * in order for the last appcache operation to succeed.
946 * @param totalUsedQuota is the sum of all origins' quota.
947 * @param quotaUpdater A callback to inform the WebCore thread that a
948 * new app cache size is available. This callback must always
949 * be executed at some point to ensure that the sleeping
950 * WebCore thread is woken up.
951 */
952 @Override
953 public void onReachedMaxAppCacheSize(long spaceNeeded,
954 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -0700955 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -0700956 .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota,
957 quotaUpdater);
958 }
959
960 /**
961 * Instructs the browser to show a prompt to ask the user to set the
962 * Geolocation permission state for the specified origin.
963 * @param origin The origin for which Geolocation permissions are
964 * requested.
965 * @param callback The callback to call once the user has set the
966 * Geolocation permission state.
967 */
968 @Override
969 public void onGeolocationPermissionsShowPrompt(String origin,
970 GeolocationPermissions.Callback callback) {
971 if (mInForeground) {
Grace Kloba50c241e2010-04-20 11:07:50 -0700972 getGeolocationPermissionsPrompt().show(origin, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700973 }
974 }
975
976 /**
977 * Instructs the browser to hide the Geolocation permissions prompt.
978 */
979 @Override
980 public void onGeolocationPermissionsHidePrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -0700981 if (mInForeground && mGeolocationPermissionsPrompt != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700982 mGeolocationPermissionsPrompt.hide();
983 }
984 }
985
Ben Murdoch65acc352009-11-19 18:16:04 +0000986 /* Adds a JavaScript error message to the system log and if the JS
987 * console is enabled in the about:debug options, to that console
988 * also.
Ben Murdochc42addf2010-01-28 15:19:59 +0000989 * @param consoleMessage the message object.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700990 */
991 @Override
Ben Murdochc42addf2010-01-28 15:19:59 +0000992 public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Jeff Hamilton47654f42010-09-07 09:57:51 -0500993 // Don't log console messages in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -0700994 if (isPrivateBrowsingEnabled()) return true;
Jeff Hamilton47654f42010-09-07 09:57:51 -0500995
Ben Murdochc42addf2010-01-28 15:19:59 +0000996 String message = "Console: " + consoleMessage.message() + " "
997 + consoleMessage.sourceId() + ":"
998 + consoleMessage.lineNumber();
999
1000 switch (consoleMessage.messageLevel()) {
1001 case TIP:
1002 Log.v(CONSOLE_LOGTAG, message);
1003 break;
1004 case LOG:
1005 Log.i(CONSOLE_LOGTAG, message);
1006 break;
1007 case WARNING:
1008 Log.w(CONSOLE_LOGTAG, message);
1009 break;
1010 case ERROR:
1011 Log.e(CONSOLE_LOGTAG, message);
1012 break;
1013 case DEBUG:
1014 Log.d(CONSOLE_LOGTAG, message);
1015 break;
1016 }
1017
1018 return true;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001019 }
1020
1021 /**
1022 * Ask the browser for an icon to represent a <video> element.
1023 * This icon will be used if the Web page did not specify a poster attribute.
1024 * @return Bitmap The icon or null if no such icon is available.
1025 */
1026 @Override
1027 public Bitmap getDefaultVideoPoster() {
1028 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001029 return mWebViewController.getDefaultVideoPoster();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001030 }
1031 return null;
1032 }
1033
1034 /**
1035 * Ask the host application for a custom progress view to show while
1036 * a <video> is loading.
1037 * @return View The progress view.
1038 */
1039 @Override
1040 public View getVideoLoadingProgressView() {
1041 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001042 return mWebViewController.getVideoLoadingProgressView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001043 }
1044 return null;
1045 }
1046
1047 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00001048 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001049 if (mInForeground) {
Ben Murdoch8cad4132012-01-11 10:56:43 +00001050 mWebViewController.openFileChooser(uploadMsg, acceptType, capture);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001051 } else {
1052 uploadMsg.onReceiveValue(null);
1053 }
1054 }
1055
Vivek Sekharb54614f2014-05-01 19:03:37 -07001056 @Override
1057 public void showFileChooser(ValueCallback<String[]> uploadFilePaths, String acceptTypes,
1058 boolean capture) {
1059 if (mInForeground) {
1060 mWebViewController.showFileChooser(uploadFilePaths, acceptTypes, capture);
1061 } else {
1062 uploadFilePaths.onReceiveValue(null);
1063 }
1064 }
1065
Grace Kloba22ac16e2009-10-07 18:00:23 -07001066 /**
1067 * Deliver a list of already-visited URLs
1068 */
1069 @Override
1070 public void getVisitedHistory(final ValueCallback<String[]> callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001071 mWebViewController.getVisitedHistory(callback);
1072 }
Ben Murdoch8029a772010-11-16 11:58:21 +00001073
1074 @Override
1075 public void setupAutoFill(Message message) {
1076 // Prompt the user to set up their profile.
1077 final Message msg = message;
Michael Kolb14612442011-06-24 13:06:29 -07001078 AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
1079 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
Ben Murdoch1d676b62011-01-17 12:54:24 +00001080 Context.LAYOUT_INFLATER_SERVICE);
1081 final View layout = inflater.inflate(R.layout.setup_autofill_dialog, null);
1082
1083 builder.setView(layout)
1084 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1085 @Override
1086 public void onClick(DialogInterface dialog, int id) {
1087 CheckBox disableAutoFill = (CheckBox) layout.findViewById(
1088 R.id.setup_autofill_dialog_disable_autofill);
1089
1090 if (disableAutoFill.isChecked()) {
1091 // Disable autofill and show a toast with how to turn it on again.
John Reck35e9dd62011-04-25 09:01:54 -07001092 mSettings.setAutofillEnabled(false);
Michael Kolb14612442011-06-24 13:06:29 -07001093 Toast.makeText(mContext,
Ben Murdoch1d676b62011-01-17 12:54:24 +00001094 R.string.autofill_setup_dialog_negative_toast,
1095 Toast.LENGTH_LONG).show();
1096 } else {
1097 // Take user to the AutoFill profile editor. When they return,
1098 // we will send the message that we pass here which will trigger
1099 // the form to get filled out with their new profile.
1100 mWebViewController.setupAutoFill(msg);
1101 }
1102 }
1103 })
1104 .setNegativeButton(R.string.cancel, null)
1105 .show();
Ben Murdoch8029a772010-11-16 11:58:21 +00001106 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001107 };
1108
1109 // -------------------------------------------------------------------------
1110 // WebViewClient implementation for the sub window
1111 // -------------------------------------------------------------------------
1112
1113 // Subclass of WebViewClient used in subwindows to notify the main
1114 // WebViewClient of certain WebView activities.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001115 private static class SubWindowClient extends WebViewClient {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001116 // The main WebViewClient.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001117 private final WebViewClient mClient;
Michael Kolb8233fac2010-10-26 16:08:53 -07001118 private final WebViewController mController;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001119
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001120 SubWindowClient(WebViewClient client, WebViewController controller) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001121 mClient = client;
Michael Kolb8233fac2010-10-26 16:08:53 -07001122 mController = controller;
Leon Scroggins III211ba542010-04-19 13:21:13 -04001123 }
1124 @Override
1125 public void onPageStarted(WebView view, String url, Bitmap favicon) {
1126 // Unlike the others, do not call mClient's version, which would
1127 // change the progress bar. However, we do want to remove the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001128 // find or select dialog.
Michael Kolb8233fac2010-10-26 16:08:53 -07001129 mController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001130 }
1131 @Override
1132 public void doUpdateVisitedHistory(WebView view, String url,
1133 boolean isReload) {
1134 mClient.doUpdateVisitedHistory(view, url, isReload);
1135 }
1136 @Override
1137 public boolean shouldOverrideUrlLoading(WebView view, String url) {
1138 return mClient.shouldOverrideUrlLoading(view, url);
1139 }
1140 @Override
1141 public void onReceivedSslError(WebView view, SslErrorHandler handler,
1142 SslError error) {
1143 mClient.onReceivedSslError(view, handler, error);
1144 }
1145 @Override
Brian Carlstrom8862c1d2011-06-02 01:05:55 -07001146 public void onReceivedClientCertRequest(WebView view,
1147 ClientCertRequestHandler handler, String host_and_port) {
1148 mClient.onReceivedClientCertRequest(view, handler, host_and_port);
1149 }
1150 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -07001151 public void onReceivedHttpAuthRequest(WebView view,
1152 HttpAuthHandler handler, String host, String realm) {
1153 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
1154 }
1155 @Override
1156 public void onFormResubmission(WebView view, Message dontResend,
1157 Message resend) {
1158 mClient.onFormResubmission(view, dontResend, resend);
1159 }
1160 @Override
1161 public void onReceivedError(WebView view, int errorCode,
1162 String description, String failingUrl) {
1163 mClient.onReceivedError(view, errorCode, description, failingUrl);
1164 }
1165 @Override
1166 public boolean shouldOverrideKeyEvent(WebView view,
1167 android.view.KeyEvent event) {
1168 return mClient.shouldOverrideKeyEvent(view, event);
1169 }
1170 @Override
1171 public void onUnhandledKeyEvent(WebView view,
1172 android.view.KeyEvent event) {
1173 mClient.onUnhandledKeyEvent(view, event);
1174 }
1175 }
1176
1177 // -------------------------------------------------------------------------
1178 // WebChromeClient implementation for the sub window
1179 // -------------------------------------------------------------------------
1180
1181 private class SubWindowChromeClient extends WebChromeClient {
1182 // The main WebChromeClient.
1183 private final WebChromeClient mClient;
1184
1185 SubWindowChromeClient(WebChromeClient client) {
1186 mClient = client;
1187 }
1188 @Override
1189 public void onProgressChanged(WebView view, int newProgress) {
1190 mClient.onProgressChanged(view, newProgress);
1191 }
1192 @Override
1193 public boolean onCreateWindow(WebView view, boolean dialog,
1194 boolean userGesture, android.os.Message resultMsg) {
1195 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
1196 }
1197 @Override
1198 public void onCloseWindow(WebView window) {
1199 if (window != mSubView) {
1200 Log.e(LOGTAG, "Can't close the window");
1201 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001202 mWebViewController.dismissSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001203 }
1204 }
1205
1206 // -------------------------------------------------------------------------
1207
1208 // Construct a new tab
Michael Kolb7bcafde2011-05-09 13:55:59 -07001209 Tab(WebViewController wvcontroller, WebView w) {
John Reck1cf4b792011-07-26 10:22:22 -07001210 this(wvcontroller, w, null);
1211 }
1212
1213 Tab(WebViewController wvcontroller, Bundle state) {
1214 this(wvcontroller, null, state);
1215 }
1216
1217 Tab(WebViewController wvcontroller, WebView w, Bundle state) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001218 mWebViewController = wvcontroller;
Michael Kolb14612442011-06-24 13:06:29 -07001219 mContext = mWebViewController.getContext();
John Reck35e9dd62011-04-25 09:01:54 -07001220 mSettings = BrowserSettings.getInstance();
Michael Kolb14612442011-06-24 13:06:29 -07001221 mDataController = DataController.getInstance(mContext);
1222 mCurrentState = new PageState(mContext, w != null
John Reck847b5322011-04-14 17:02:18 -07001223 ? w.isPrivateBrowsingEnabled() : false);
Tarun Nainani8084c822014-06-25 13:38:06 -07001224 setTimeStamp();
Michael Kolb8233fac2010-10-26 16:08:53 -07001225 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001226 mInForeground = false;
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001227 mWebViewDestroyedByMemoryMonitor = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001228
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001229 mDownloadListener = new BrowserDownloadListener() {
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001230 public void onDownloadStart(String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001231 String contentDisposition, String mimetype, String referer,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001232 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001233 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001234 mimetype, referer, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001235 }
1236 };
1237
John Reck1cf4b792011-07-26 10:22:22 -07001238 mCaptureWidth = mContext.getResources().getDimensionPixelSize(
1239 R.dimen.tab_thumbnail_width);
1240 mCaptureHeight = mContext.getResources().getDimensionPixelSize(
1241 R.dimen.tab_thumbnail_height);
1242 updateShouldCaptureThumbnails();
1243 restoreState(state);
John Reck52be4782011-08-26 15:37:29 -07001244 if (getId() == -1) {
1245 mId = TabControl.getNextId();
1246 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001247 setWebView(w);
Michael Kolb9ef259a2011-07-12 15:33:08 -07001248 mHandler = new Handler() {
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001249 @Override
Michael Kolb9ef259a2011-07-12 15:33:08 -07001250 public void handleMessage(Message m) {
John Reck1cf4b792011-07-26 10:22:22 -07001251 switch (m.what) {
1252 case MSG_CAPTURE:
1253 capture();
1254 break;
1255 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001256 }
1257 };
Pankaj Garg1c13cab2015-05-12 11:52:17 -07001258
1259 mFirstPixelObservable = new Observable();
1260 mFirstPixelObservable.set(false);
1261 mTabHistoryUpdateObservable = new Observable();
John Reck1cf4b792011-07-26 10:22:22 -07001262 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001263
Michael Kolb72864272012-05-03 15:42:15 -07001264 public boolean shouldUpdateThumbnail() {
1265 return mUpdateThumbnail;
1266 }
1267
Mathew Inwoode09305e2011-09-02 12:03:26 +01001268 /**
1269 * This is used to get a new ID when the tab has been preloaded, before it is displayed and
1270 * added to TabControl. Preloaded tabs can be created before restoreInstanceState, leading
1271 * to overlapping IDs between the preloaded and restored tabs.
1272 */
1273 public void refreshIdAfterPreload() {
1274 mId = TabControl.getNextId();
1275 }
1276
John Reck1cf4b792011-07-26 10:22:22 -07001277 public void updateShouldCaptureThumbnails() {
1278 if (mWebViewController.shouldCaptureThumbnails()) {
1279 synchronized (Tab.this) {
1280 if (mCapture == null) {
1281 mCapture = Bitmap.createBitmap(mCaptureWidth, mCaptureHeight,
1282 Bitmap.Config.RGB_565);
Michael Kolbc3af0672011-08-09 10:24:41 -07001283 mCapture.eraseColor(Color.WHITE);
John Reck1cf4b792011-07-26 10:22:22 -07001284 if (mInForeground) {
1285 postCapture();
1286 }
1287 }
1288 }
1289 } else {
1290 synchronized (Tab.this) {
1291 mCapture = null;
1292 deleteThumbnail();
1293 }
1294 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001295 }
1296
Michael Kolb14612442011-06-24 13:06:29 -07001297 public void setController(WebViewController ctl) {
1298 mWebViewController = ctl;
John Reck1cf4b792011-07-26 10:22:22 -07001299 updateShouldCaptureThumbnails();
Michael Kolb14612442011-06-24 13:06:29 -07001300 }
1301
Michael Kolbc831b632011-05-11 09:30:34 -07001302 public long getId() {
1303 return mId;
1304 }
1305
Michael Kolb91911a22012-01-17 11:21:25 -08001306 void setWebView(WebView w) {
1307 setWebView(w, true);
1308 }
1309
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001310 public boolean isNativeActive(){
1311 if (mMainView == null)
1312 return false;
1313 return true;
1314 }
1315
1316 public void setTimeStamp(){
1317 Date d = new Date();
1318 timestamp = (new Timestamp(d.getTime()));
1319 }
1320
1321 public Timestamp getTimestamp() {
1322 return timestamp;
1323 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001324 /**
1325 * Sets the WebView for this tab, correctly removing the old WebView from
1326 * the container view.
1327 */
Michael Kolb91911a22012-01-17 11:21:25 -08001328 void setWebView(WebView w, boolean restore) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001329 if (mMainView == w) {
1330 return;
1331 }
Michael Kolba713ec82010-11-29 17:27:06 -08001332
Grace Kloba22ac16e2009-10-07 18:00:23 -07001333 // If the WebView is changing, the page will be reloaded, so any ongoing
1334 // Geolocation permission requests are void.
Grace Kloba50c241e2010-04-20 11:07:50 -07001335 if (mGeolocationPermissionsPrompt != null) {
1336 mGeolocationPermissionsPrompt.hide();
1337 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001338
Michael Kolba713ec82010-11-29 17:27:06 -08001339 mWebViewController.onSetWebView(this, w);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001340
John Reck1cf4b792011-07-26 10:22:22 -07001341 if (mMainView != null) {
John Reck8ee633f2011-08-09 16:00:35 -07001342 mMainView.setPictureListener(null);
John Reck1cf4b792011-07-26 10:22:22 -07001343 if (w != null) {
1344 syncCurrentState(w, null);
1345 } else {
Panos Thomasa9a5a582014-03-18 19:20:08 -07001346 mCurrentState = new PageState(mContext, mMainView.isPrivateBrowsingEnabled());
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001347
1348 if (mWebViewDestroyedByMemoryMonitor) {
1349 /*
1350 * If tab was destroyed as a result of the MemoryMonitor
1351 * then we need to restore the state properties
1352 * from the old WebView (mMainView)
1353 */
1354 syncCurrentState(mMainView, null);
1355 mWebViewDestroyedByMemoryMonitor = false;
1356 }
John Reck1cf4b792011-07-26 10:22:22 -07001357 }
1358 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001359 // set the new one
1360 mMainView = w;
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001361
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001362 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001363 if (mMainView != null) {
1364 mMainView.setWebViewClient(mWebViewClient);
1365 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001366 // Attach DownloadManager so that downloads can start in an active
1367 // or a non-active window. This can happen when going to a site that
1368 // does a redirect after a period of time. The user could have
1369 // switched to another tab while waiting for the download to start.
1370 mMainView.setDownloadListener(mDownloadListener);
John Reck8ee633f2011-08-09 16:00:35 -07001371 TabControl tc = mWebViewController.getTabControl();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001372 if (tc != null /*&& tc.getOnThumbnailUpdatedListener() != null*/) {
John Reck8ee633f2011-08-09 16:00:35 -07001373 mMainView.setPictureListener(this);
1374 }
Michael Kolb91911a22012-01-17 11:21:25 -08001375 if (restore && (mSavedState != null)) {
John Reck8b9bb8b2012-03-08 13:19:40 -08001376 restoreUserAgent();
John Reck6c2e2f32011-08-22 13:41:23 -07001377 WebBackForwardList restoredState
1378 = mMainView.restoreState(mSavedState);
1379 if (restoredState == null || restoredState.getSize() == 0) {
1380 Log.w(LOGTAG, "Failed to restore WebView state!");
1381 loadUrl(mCurrentState.mOriginalUrl, null);
1382 }
John Reck1cf4b792011-07-26 10:22:22 -07001383 mSavedState = null;
1384 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001385 }
1386 }
1387
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001388 public void destroyThroughMemoryMonitor() {
1389 mWebViewDestroyedByMemoryMonitor = true;
1390 destroy();
1391 }
1392
Grace Kloba22ac16e2009-10-07 18:00:23 -07001393 /**
1394 * Destroy the tab's main WebView and subWindow if any
1395 */
1396 void destroy() {
Tarun Nainani2c1dd7c2014-07-05 16:40:12 -07001397
Matthew Huib7f2e9c2014-04-16 11:12:37 -04001398 if (mPostponeDestroy) {
1399 mShouldDestroy = true;
1400 return;
1401 }
1402 mShouldDestroy = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001403 if (mMainView != null) {
1404 dismissSubWindow();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001405 // save the WebView to call destroy() after detach it from the tab
1406 WebView webView = mMainView;
Pankaj Garg1c13cab2015-05-12 11:52:17 -07001407 if (!mWebViewDestroyedByMemoryMonitor) {
1408 int[] ids = webView.getSnapshotIds();
1409 int currentTabIdx = mWebViewController.getTabControl().getCurrentPosition();
1410 for (int id : ids) {
1411 if (getTabIdxFromCaptureIdx(id) == currentTabIdx) {
1412 webView.deleteSnapshot(id);
1413 }
1414 }
1415 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001416 setWebView(null);
1417 webView.destroy();
1418 }
1419 }
1420
Matthew Huib7f2e9c2014-04-16 11:12:37 -04001421 private boolean mPostponeDestroy = false;
1422 private boolean mShouldDestroy = false;
1423
1424 public void postponeDestroy() {
1425 mPostponeDestroy = true;
1426 }
1427
1428 public void performPostponedDestroy() {
1429 mPostponeDestroy = false;
1430 if (mShouldDestroy) {
1431 destroy();
1432 }
1433 }
1434
Grace Kloba22ac16e2009-10-07 18:00:23 -07001435 /**
1436 * Remove the tab from the parent
1437 */
1438 void removeFromTree() {
1439 // detach the children
Michael Kolbc831b632011-05-11 09:30:34 -07001440 if (mChildren != null) {
1441 for(Tab t : mChildren) {
1442 t.setParent(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001443 }
1444 }
1445 // remove itself from the parent list
Michael Kolbc831b632011-05-11 09:30:34 -07001446 if (mParent != null) {
1447 mParent.mChildren.remove(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001448 }
Tarun Nainani8eb00912014-07-17 12:28:32 -07001449
1450 mCapture = null;
John Reck1cf4b792011-07-26 10:22:22 -07001451 deleteThumbnail();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001452 }
1453
1454 /**
1455 * Create a new subwindow unless a subwindow already exists.
1456 * @return True if a new subwindow was created. False if one already exists.
1457 */
1458 boolean createSubWindow() {
1459 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001460 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001461 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001462 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001463 mSubView.setWebChromeClient(new SubWindowChromeClient(
1464 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001465 // Set a different DownloadListener for the mSubView, since it will
1466 // just need to dismiss the mSubView, rather than close the Tab
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001467 mSubView.setDownloadListener(new BrowserDownloadListener() {
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001468 public void onDownloadStart(String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001469 String contentDisposition, String mimetype, String referer,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001470 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001471 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001472 contentDisposition, mimetype, referer, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001473 if (mSubView.copyBackForwardList().getSize() == 0) {
1474 // This subwindow was opened for the sole purpose of
1475 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001476 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001477 }
1478 }
1479 });
Michael Kolb14612442011-06-24 13:06:29 -07001480 mSubView.setOnCreateContextMenuListener(mWebViewController.getActivity());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001481 return true;
1482 }
1483 return false;
1484 }
1485
1486 /**
1487 * Dismiss the subWindow for the tab.
1488 */
1489 void dismissSubWindow() {
1490 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001491 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001492 mSubView.destroy();
1493 mSubView = null;
1494 mSubViewContainer = null;
1495 }
1496 }
1497
Grace Kloba22ac16e2009-10-07 18:00:23 -07001498
1499 /**
1500 * Set the parent tab of this tab.
1501 */
Michael Kolbc831b632011-05-11 09:30:34 -07001502 void setParent(Tab parent) {
John Reck52be4782011-08-26 15:37:29 -07001503 if (parent == this) {
1504 throw new IllegalStateException("Cannot set parent to self!");
1505 }
Michael Kolbc831b632011-05-11 09:30:34 -07001506 mParent = parent;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001507 // This tab may have been freed due to low memory. If that is the case,
Michael Kolbc831b632011-05-11 09:30:34 -07001508 // the parent tab id is already saved. If we are changing that id
Grace Kloba22ac16e2009-10-07 18:00:23 -07001509 // (most likely due to removing the parent tab) we must update the
Michael Kolbc831b632011-05-11 09:30:34 -07001510 // parent tab id in the saved Bundle.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001511 if (mSavedState != null) {
1512 if (parent == null) {
1513 mSavedState.remove(PARENTTAB);
1514 } else {
Michael Kolbc831b632011-05-11 09:30:34 -07001515 mSavedState.putLong(PARENTTAB, parent.getId());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001516 }
1517 }
John Reckb0a86db2011-05-24 14:05:58 -07001518
1519 // Sync the WebView useragent with the parent
1520 if (parent != null && mSettings.hasDesktopUseragent(parent.getWebView())
1521 != mSettings.hasDesktopUseragent(getWebView())) {
1522 mSettings.toggleDesktopUseragent(getWebView());
1523 }
John Reck52be4782011-08-26 15:37:29 -07001524
1525 if (parent != null && parent.getId() == getId()) {
1526 throw new IllegalStateException("Parent has same ID as child!");
1527 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001528 }
1529
1530 /**
Michael Kolbc831b632011-05-11 09:30:34 -07001531 * If this Tab was created through another Tab, then this method returns
1532 * that Tab.
1533 * @return the Tab parent or null
1534 */
1535 public Tab getParent() {
1536 return mParent;
1537 }
1538
1539 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001540 * When a Tab is created through the content of another Tab, then we
1541 * associate the Tabs.
1542 * @param child the Tab that was created from this Tab
1543 */
1544 void addChildTab(Tab child) {
Michael Kolbc831b632011-05-11 09:30:34 -07001545 if (mChildren == null) {
1546 mChildren = new Vector<Tab>();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001547 }
Michael Kolbc831b632011-05-11 09:30:34 -07001548 mChildren.add(child);
1549 child.setParent(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001550 }
1551
Michael Kolbc831b632011-05-11 09:30:34 -07001552 Vector<Tab> getChildren() {
1553 return mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001554 }
1555
1556 void resume() {
1557 if (mMainView != null) {
John Reck56c1fcf2011-08-17 10:15:16 -07001558 setupHwAcceleration(mMainView);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001559 mMainView.onResume();
1560 if (mSubView != null) {
1561 mSubView.onResume();
1562 }
1563 }
1564 }
1565
John Reck56c1fcf2011-08-17 10:15:16 -07001566 private void setupHwAcceleration(View web) {
1567 if (web == null) return;
1568 BrowserSettings settings = BrowserSettings.getInstance();
1569 if (settings.isHardwareAccelerated()) {
1570 web.setLayerType(View.LAYER_TYPE_NONE, null);
1571 } else {
1572 web.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
1573 }
1574 }
1575
Grace Kloba22ac16e2009-10-07 18:00:23 -07001576 void pause() {
1577 if (mMainView != null) {
1578 mMainView.onPause();
1579 if (mSubView != null) {
1580 mSubView.onPause();
1581 }
1582 }
1583 }
1584
1585 void putInForeground() {
John Reck8ee633f2011-08-09 16:00:35 -07001586 if (mInForeground) {
1587 return;
1588 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001589 mInForeground = true;
1590 resume();
Michael Kolb14612442011-06-24 13:06:29 -07001591 Activity activity = mWebViewController.getActivity();
1592 mMainView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001593 if (mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -07001594 mSubView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001595 }
Axesh R. Ajmerac6b5c322015-05-01 11:06:10 -07001596
Leon Scroggins1961ed22010-12-07 15:22:21 -05001597 mWebViewController.bookmarkedStatusHasChanged(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001598 }
1599
1600 void putInBackground() {
John Reck8ee633f2011-08-09 16:00:35 -07001601 if (!mInForeground) {
1602 return;
1603 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001604 mInForeground = false;
1605 pause();
1606 mMainView.setOnCreateContextMenuListener(null);
1607 if (mSubView != null) {
1608 mSubView.setOnCreateContextMenuListener(null);
1609 }
1610 }
1611
Michael Kolb8233fac2010-10-26 16:08:53 -07001612 boolean inForeground() {
1613 return mInForeground;
1614 }
1615
Grace Kloba22ac16e2009-10-07 18:00:23 -07001616 /**
1617 * Return the top window of this tab; either the subwindow if it is not
1618 * null or the main window.
1619 * @return The top window of this tab.
1620 */
1621 WebView getTopWindow() {
1622 if (mSubView != null) {
1623 return mSubView;
1624 }
1625 return mMainView;
1626 }
1627
1628 /**
1629 * Return the main window of this tab. Note: if a tab is freed in the
1630 * background, this can return null. It is only guaranteed to be
1631 * non-null for the current tab.
1632 * @return The main WebView of this tab.
1633 */
1634 WebView getWebView() {
1635 return mMainView;
1636 }
1637
Michael Kolba713ec82010-11-29 17:27:06 -08001638 void setViewContainer(View container) {
1639 mContainer = container;
1640 }
1641
Michael Kolb8233fac2010-10-26 16:08:53 -07001642 View getViewContainer() {
1643 return mContainer;
1644 }
1645
Grace Kloba22ac16e2009-10-07 18:00:23 -07001646 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001647 * Return whether private browsing is enabled for the main window of
1648 * this tab.
1649 * @return True if private browsing is enabled.
1650 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001651 boolean isPrivateBrowsingEnabled() {
John Reck502a3532011-08-16 14:21:46 -07001652 return mCurrentState.mIncognito;
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001653 }
1654
1655 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001656 * Return the subwindow of this tab or null if there is no subwindow.
1657 * @return The subwindow of this tab or null.
1658 */
1659 WebView getSubWebView() {
1660 return mSubView;
1661 }
1662
Michael Kolb1514bb72010-11-22 09:11:48 -08001663 void setSubWebView(WebView subView) {
1664 mSubView = subView;
1665 }
1666
Michael Kolb8233fac2010-10-26 16:08:53 -07001667 View getSubViewContainer() {
1668 return mSubViewContainer;
1669 }
1670
Michael Kolb1514bb72010-11-22 09:11:48 -08001671 void setSubViewContainer(View subViewContainer) {
1672 mSubViewContainer = subViewContainer;
1673 }
1674
Grace Kloba22ac16e2009-10-07 18:00:23 -07001675 /**
1676 * @return The geolocation permissions prompt for this tab.
1677 */
1678 GeolocationPermissionsPrompt getGeolocationPermissionsPrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001679 if (mGeolocationPermissionsPrompt == null) {
1680 ViewStub stub = (ViewStub) mContainer
1681 .findViewById(R.id.geolocation_permissions_prompt);
1682 mGeolocationPermissionsPrompt = (GeolocationPermissionsPrompt) stub
1683 .inflate();
Panos Thomasb298aad2014-10-22 12:24:21 -07001684 mGeolocationPermissionsPrompt.init(mCurrentState.mIncognito);
Grace Kloba50c241e2010-04-20 11:07:50 -07001685 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001686 return mGeolocationPermissionsPrompt;
1687 }
1688
1689 /**
1690 * @return The application id string
1691 */
1692 String getAppId() {
1693 return mAppId;
1694 }
1695
1696 /**
1697 * Set the application id string
1698 * @param id
1699 */
1700 void setAppId(String id) {
1701 mAppId = id;
1702 }
1703
Michael Kolbe28b3472011-08-04 16:54:31 -07001704 boolean closeOnBack() {
1705 return mCloseOnBack;
1706 }
1707
1708 void setCloseOnBack(boolean close) {
1709 mCloseOnBack = close;
1710 }
1711
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001712 boolean getDerivedFromIntent() {
1713 return mDerivedFromIntent;
1714 }
1715
1716 void setDerivedFromIntent(boolean derived) {
1717 mDerivedFromIntent = derived;
1718 }
1719
Grace Kloba22ac16e2009-10-07 18:00:23 -07001720 String getUrl() {
John Reck324d4402011-01-11 16:56:42 -08001721 return UrlUtils.filteredUrl(mCurrentState.mUrl);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001722 }
1723
Tarun Nainani8eb00912014-07-17 12:28:32 -07001724
1725 protected void onPageFinished() {
1726 mPageFinished = true;
1727 }
1728
1729 public boolean getPageFinishedStatus() {
1730 return mPageFinished;
1731 }
1732
John Reck49a603c2011-03-03 09:33:05 -08001733 String getOriginalUrl() {
Vivek Sekhar361065a2014-11-25 15:34:35 -08001734 if (mMainView != null)
1735 return mMainView.getOriginalUrl();
John Reckdb22ec42011-06-29 11:31:24 -07001736 if (mCurrentState.mOriginalUrl == null) {
1737 return getUrl();
John Reck49a603c2011-03-03 09:33:05 -08001738 }
John Reckdb22ec42011-06-29 11:31:24 -07001739 return UrlUtils.filteredUrl(mCurrentState.mOriginalUrl);
John Reck49a603c2011-03-03 09:33:05 -08001740 }
1741
Grace Kloba22ac16e2009-10-07 18:00:23 -07001742 /**
John Reck30c714c2010-12-16 17:30:34 -08001743 * Get the title of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001744 */
1745 String getTitle() {
John Reck30c714c2010-12-16 17:30:34 -08001746 return mCurrentState.mTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001747 }
1748
1749 /**
John Reck30c714c2010-12-16 17:30:34 -08001750 * Get the favicon of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001751 */
1752 Bitmap getFavicon() {
John Reck1cf4b792011-07-26 10:22:22 -07001753 if (mCurrentState.mFavicon != null) {
1754 return mCurrentState.mFavicon;
1755 }
1756 return getDefaultFavicon(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001757 }
1758
John Recke969cc52010-12-21 17:24:43 -08001759 public boolean isBookmarkedSite() {
1760 return mCurrentState.mIsBookmarkedSite;
1761 }
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001762
Grace Kloba22ac16e2009-10-07 18:00:23 -07001763 /**
Steve Block08a6f0c2011-10-06 12:12:53 +01001764 * Sets the security state, clears the SSL certificate error and informs
1765 * the controller.
1766 */
Steve Block2466eff2011-10-03 15:33:09 +01001767 private void setSecurityState(SecurityState securityState) {
1768 mCurrentState.mSecurityState = securityState;
Steve Block08a6f0c2011-10-06 12:12:53 +01001769 mCurrentState.mSslCertificateError = null;
Steve Block2466eff2011-10-03 15:33:09 +01001770 mWebViewController.onUpdatedSecurityState(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001771 }
1772
1773 /**
Steve Block2466eff2011-10-03 15:33:09 +01001774 * @return The tab's security state.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001775 */
Steve Block2466eff2011-10-03 15:33:09 +01001776 SecurityState getSecurityState() {
1777 return mCurrentState.mSecurityState;
John Reck30c714c2010-12-16 17:30:34 -08001778 }
1779
Steve Block08a6f0c2011-10-06 12:12:53 +01001780 /**
1781 * Gets the SSL certificate error, if any, for the page's main resource.
1782 * This is only non-null when the security state is
1783 * SECURITY_STATE_BAD_CERTIFICATE.
1784 */
1785 SslError getSslCertificateError() {
1786 return mCurrentState.mSslCertificateError;
1787 }
1788
John Reck30c714c2010-12-16 17:30:34 -08001789 int getLoadProgress() {
1790 if (mInPageLoad) {
1791 return mPageLoadProgress;
1792 }
1793 return 100;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001794 }
1795
1796 /**
1797 * @return TRUE if onPageStarted is called while onPageFinished is not
1798 * called yet.
1799 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001800 boolean inPageLoad() {
1801 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001802 }
1803
Grace Kloba22ac16e2009-10-07 18:00:23 -07001804 /**
John Reck1cf4b792011-07-26 10:22:22 -07001805 * @return The Bundle with the tab's state if it can be saved, otherwise null
Grace Kloba22ac16e2009-10-07 18:00:23 -07001806 */
John Reck1cf4b792011-07-26 10:22:22 -07001807 public Bundle saveState() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001808 // If the WebView is null it means we ran low on memory and we already
1809 // stored the saved state in mSavedState.
1810 if (mMainView == null) {
John Reck1cf4b792011-07-26 10:22:22 -07001811 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001812 }
John Reck6c2e2f32011-08-22 13:41:23 -07001813
1814 if (TextUtils.isEmpty(mCurrentState.mUrl)) {
John Reck1cf4b792011-07-26 10:22:22 -07001815 return null;
John Reck24f18262011-06-17 14:47:20 -07001816 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001817
1818 mSavedState = new Bundle();
John Reck6c2e2f32011-08-22 13:41:23 -07001819 WebBackForwardList savedList = mMainView.saveState(mSavedState);
1820 if (savedList == null || savedList.getSize() == 0) {
1821 Log.w(LOGTAG, "Failed to save back/forward list for "
1822 + mCurrentState.mUrl);
1823 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001824
Michael Kolbc831b632011-05-11 09:30:34 -07001825 mSavedState.putLong(ID, mId);
John Reck30c714c2010-12-16 17:30:34 -08001826 mSavedState.putString(CURRURL, mCurrentState.mUrl);
1827 mSavedState.putString(CURRTITLE, mCurrentState.mTitle);
John Reck1cf4b792011-07-26 10:22:22 -07001828 mSavedState.putBoolean(INCOGNITO, mMainView.isPrivateBrowsingEnabled());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001829 if (mAppId != null) {
1830 mSavedState.putString(APPID, mAppId);
1831 }
Michael Kolbe28b3472011-08-04 16:54:31 -07001832 mSavedState.putBoolean(CLOSEFLAG, mCloseOnBack);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001833 // Remember the parent tab so the relationship can be restored.
Michael Kolbc831b632011-05-11 09:30:34 -07001834 if (mParent != null) {
1835 mSavedState.putLong(PARENTTAB, mParent.mId);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001836 }
John Reckb0a86db2011-05-24 14:05:58 -07001837 mSavedState.putBoolean(USERAGENT,
1838 mSettings.hasDesktopUseragent(getWebView()));
John Reck1cf4b792011-07-26 10:22:22 -07001839 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001840 }
1841
1842 /*
1843 * Restore the state of the tab.
1844 */
John Reck1cf4b792011-07-26 10:22:22 -07001845 private void restoreState(Bundle b) {
1846 mSavedState = b;
1847 if (mSavedState == null) {
1848 return;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001849 }
1850 // Restore the internal state even if the WebView fails to restore.
1851 // This will maintain the app id, original url and close-on-exit values.
Michael Kolbc831b632011-05-11 09:30:34 -07001852 mId = b.getLong(ID);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001853 mAppId = b.getString(APPID);
Michael Kolbe28b3472011-08-04 16:54:31 -07001854 mCloseOnBack = b.getBoolean(CLOSEFLAG);
John Reck8b9bb8b2012-03-08 13:19:40 -08001855 restoreUserAgent();
John Reck1cf4b792011-07-26 10:22:22 -07001856 String url = b.getString(CURRURL);
1857 String title = b.getString(CURRTITLE);
1858 boolean incognito = b.getBoolean(INCOGNITO);
1859 mCurrentState = new PageState(mContext, incognito, url, null);
1860 mCurrentState.mTitle = title;
1861 synchronized (Tab.this) {
1862 if (mCapture != null) {
John Reck4eadc342011-10-31 14:04:10 -07001863 DataController.getInstance(mContext).loadThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07001864 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001865 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001866 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04001867
John Reck8b9bb8b2012-03-08 13:19:40 -08001868 private void restoreUserAgent() {
1869 if (mMainView == null || mSavedState == null) {
1870 return;
1871 }
1872 if (mSavedState.getBoolean(USERAGENT)
1873 != mSettings.hasDesktopUseragent(mMainView)) {
1874 mSettings.toggleDesktopUseragent(mMainView);
1875 }
1876 }
1877
Leon Scroggins1961ed22010-12-07 15:22:21 -05001878 public void updateBookmarkedStatus() {
John Recke969cc52010-12-21 17:24:43 -08001879 mDataController.queryBookmarkStatus(getUrl(), mIsBookmarkCallback);
Leon Scroggins1961ed22010-12-07 15:22:21 -05001880 }
1881
John Recke969cc52010-12-21 17:24:43 -08001882 private DataController.OnQueryUrlIsBookmark mIsBookmarkCallback
1883 = new DataController.OnQueryUrlIsBookmark() {
1884 @Override
1885 public void onQueryUrlIsBookmark(String url, boolean isBookmark) {
1886 if (mCurrentState.mUrl.equals(url)) {
1887 mCurrentState.mIsBookmarkedSite = isBookmark;
1888 mWebViewController.bookmarkedStatusHasChanged(Tab.this);
1889 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001890 }
John Recke969cc52010-12-21 17:24:43 -08001891 };
Michael Kolb1acef692011-03-08 14:12:06 -08001892
Michael Kolbeb95db42011-03-03 10:38:40 -08001893 public Bitmap getScreenshot() {
John Reck1cf4b792011-07-26 10:22:22 -07001894 synchronized (Tab.this) {
1895 return mCapture;
1896 }
Michael Kolbeb95db42011-03-03 10:38:40 -08001897 }
1898
John Reck541f55a2011-06-07 16:34:43 -07001899 public boolean isSnapshot() {
John Reck541f55a2011-06-07 16:34:43 -07001900 return false;
1901 }
1902
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001903 private static class SaveCallback implements ValueCallback<String> {
1904 boolean onReceiveValueCalled = false;
1905 private String mPath;
John Reck68234a92012-04-19 15:27:12 -07001906
1907 @Override
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001908 public void onReceiveValue(String path) {
1909 this.onReceiveValueCalled = true;
1910 this.mPath = path;
John Reck68234a92012-04-19 15:27:12 -07001911 synchronized (this) {
1912 notifyAll();
John Reck8cc92352011-07-06 17:41:52 -07001913 }
John Reck541f55a2011-06-07 16:34:43 -07001914 }
John Reck68234a92012-04-19 15:27:12 -07001915
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001916 public String getPath() {
1917 return mPath;
1918 }
John Reck68234a92012-04-19 15:27:12 -07001919 }
1920
1921 /**
1922 * Must be called on the UI thread
1923 */
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001924 public ContentValues createSnapshotValues(Bitmap bm) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001925 WebView web = getWebView();
John Reck68234a92012-04-19 15:27:12 -07001926 if (web == null) return null;
John Reckd8c74522011-06-14 08:45:00 -07001927 ContentValues values = new ContentValues();
1928 values.put(Snapshots.TITLE, mCurrentState.mTitle);
1929 values.put(Snapshots.URL, mCurrentState.mUrl);
John Reck68234a92012-04-19 15:27:12 -07001930 values.put(Snapshots.BACKGROUND, web.getPageBackgroundColor());
John Reck8cc92352011-07-06 17:41:52 -07001931 values.put(Snapshots.DATE_CREATED, System.currentTimeMillis());
1932 values.put(Snapshots.FAVICON, compressBitmap(getFavicon()));
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001933 values.put(Snapshots.THUMBNAIL, compressBitmap(bm));
John Reckd8c74522011-06-14 08:45:00 -07001934 return values;
John Reck541f55a2011-06-07 16:34:43 -07001935 }
1936
John Reck68234a92012-04-19 15:27:12 -07001937 /**
1938 * Probably want to call this on a background thread
1939 */
1940 public boolean saveViewState(ContentValues values) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001941 WebView web = getWebView();
John Reck68234a92012-04-19 15:27:12 -07001942 if (web == null) return false;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001943 String filename = UUID.randomUUID().toString();
John Reck68234a92012-04-19 15:27:12 -07001944 SaveCallback callback = new SaveCallback();
John Reck68234a92012-04-19 15:27:12 -07001945 try {
John Reck68234a92012-04-19 15:27:12 -07001946 synchronized (callback) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001947 web.saveViewState(filename, callback);
1948 callback.wait();
John Reck68234a92012-04-19 15:27:12 -07001949 }
John Reck68234a92012-04-19 15:27:12 -07001950 } catch (Exception e) {
1951 Log.w(LOGTAG, "Failed to save view state", e);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001952 String path = callback.getPath();
1953 if (path != null) {
1954 File file = mContext.getFileStreamPath(path);
1955 if (file.exists() && !file.delete()) {
1956 file.deleteOnExit();
1957 }
John Reck68234a92012-04-19 15:27:12 -07001958 }
1959 return false;
1960 }
Tarun Nainani8eb00912014-07-17 12:28:32 -07001961
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001962 String path = callback.getPath();
Tarun Nainani8eb00912014-07-17 12:28:32 -07001963 // could be that saving of file failed
1964 if (path == null) {
1965 return false;
1966 }
1967
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001968 File savedFile = new File(path);
1969 if (!savedFile.exists()) {
1970 return false;
John Reck68234a92012-04-19 15:27:12 -07001971 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001972 values.put(Snapshots.VIEWSTATE_PATH, path.substring(path.lastIndexOf('/') + 1));
1973 values.put(Snapshots.VIEWSTATE_SIZE, savedFile.length());
John Reck68234a92012-04-19 15:27:12 -07001974 return true;
1975 }
1976
John Reck8cc92352011-07-06 17:41:52 -07001977 public byte[] compressBitmap(Bitmap bitmap) {
1978 if (bitmap == null) {
1979 return null;
1980 }
1981 ByteArrayOutputStream stream = new ByteArrayOutputStream();
1982 bitmap.compress(CompressFormat.PNG, 100, stream);
1983 return stream.toByteArray();
1984 }
1985
John Reck26b18322011-06-21 13:08:58 -07001986 public void loadUrl(String url, Map<String, String> headers) {
1987 if (mMainView != null) {
Michael Kolba53c9892011-10-05 13:31:40 -07001988 mPageLoadProgress = INITIAL_PROGRESS;
Michael Kolb14612442011-06-24 13:06:29 -07001989 mCurrentState = new PageState(mContext, false, url, null);
John Reck26b18322011-06-21 13:08:58 -07001990 mMainView.loadUrl(url, headers);
1991 }
1992 }
1993
John Reck38b39652012-06-05 09:22:59 -07001994 public void disableUrlOverridingForLoad() {
1995 mDisableOverrideUrlLoading = true;
1996 }
1997
Michael Kolb9ef259a2011-07-12 15:33:08 -07001998 protected void capture() {
Vivek Sekhar6bdf6452015-05-12 17:38:45 -07001999 boolean returnEmptyCapture = false;
2000 if (mMainView == null || mCapture == null || !mMainView.isReady())
2001 returnEmptyCapture = true;
John Reck4eadc342011-10-31 14:04:10 -07002002 if (mMainView.getContentWidth() <= 0 || mMainView.getContentHeight() <= 0) {
Vivek Sekhar6bdf6452015-05-12 17:38:45 -07002003 returnEmptyCapture = true;
John Reck4eadc342011-10-31 14:04:10 -07002004 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002005
Vivek Sekhar6bdf6452015-05-12 17:38:45 -07002006 if (returnEmptyCapture || !mFirstVisualPixelPainted || mMainView.isShowingCrashView()) {
Pankaj Garg79878492015-04-01 14:48:21 -07002007 mCapture = Bitmap.createBitmap(
2008 mCaptureWidth,
2009 mCaptureHeight,
2010 Bitmap.Config.RGB_565);
2011 mCapture.eraseColor(Color.WHITE);
2012
2013 mHandler.removeMessages(MSG_CAPTURE);
2014
2015 TabControl tc = mWebViewController.getTabControl();
2016 if (tc != null) {
2017 OnThumbnailUpdatedListener updateListener
2018 = tc.getOnThumbnailUpdatedListener();
2019 if (updateListener != null) {
2020 updateListener.onThumbnailUpdated(this);
2021 }
2022 }
2023 return;
2024 }
2025
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002026 mMainView
2027 .getContentBitmapAsync(
2028 (float) mCaptureWidth / mMainView.getWidth(),
2029 new Rect(),
2030 new ValueCallback<Bitmap>() {
2031 @Override
2032 public void onReceiveValue(Bitmap bitmap) {
2033 onCaptureCallback(bitmap);
2034 }});
2035 }
2036
2037 private void onCaptureCallback(Bitmap bitmap) {
2038 if (mCapture == null || bitmap == null)
2039 return;
2040
Michael Kolb9ef259a2011-07-12 15:33:08 -07002041 Canvas c = new Canvas(mCapture);
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002042 mCapture.eraseColor(Color.WHITE);
2043 c.drawBitmap(bitmap, 0, 0, null);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002044
Michael Kolba3194d02011-09-07 11:23:51 -07002045 // manually anti-alias the edges for the tilt
2046 c.drawRect(0, 0, 1, mCapture.getHeight(), sAlphaPaint);
2047 c.drawRect(mCapture.getWidth() - 1, 0, mCapture.getWidth(),
2048 mCapture.getHeight(), sAlphaPaint);
2049 c.drawRect(0, 0, mCapture.getWidth(), 1, sAlphaPaint);
2050 c.drawRect(0, mCapture.getHeight() - 1, mCapture.getWidth(),
2051 mCapture.getHeight(), sAlphaPaint);
Dianne Hackborn43cfe8a2011-08-02 16:59:35 -07002052 c.setBitmap(null);
John Reck8ee633f2011-08-09 16:00:35 -07002053 mHandler.removeMessages(MSG_CAPTURE);
John Reck1cf4b792011-07-26 10:22:22 -07002054 persistThumbnail();
John Reck8ee633f2011-08-09 16:00:35 -07002055 TabControl tc = mWebViewController.getTabControl();
2056 if (tc != null) {
2057 OnThumbnailUpdatedListener updateListener
2058 = tc.getOnThumbnailUpdatedListener();
2059 if (updateListener != null) {
2060 updateListener.onThumbnailUpdated(this);
2061 }
2062 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07002063 }
2064
2065 @Override
2066 public void onNewPicture(WebView view, Picture picture) {
John Reck1cf4b792011-07-26 10:22:22 -07002067 postCapture();
2068 }
2069
2070 private void postCapture() {
Michael Kolb9ef259a2011-07-12 15:33:08 -07002071 if (!mHandler.hasMessages(MSG_CAPTURE)) {
2072 mHandler.sendEmptyMessageDelayed(MSG_CAPTURE, CAPTURE_DELAY);
2073 }
2074 }
2075
John Reckef654f12011-07-12 16:42:08 -07002076 public boolean canGoBack() {
2077 return mMainView != null ? mMainView.canGoBack() : false;
2078 }
2079
2080 public boolean canGoForward() {
2081 return mMainView != null ? mMainView.canGoForward() : false;
2082 }
2083
2084 public void goBack() {
2085 if (mMainView != null) {
2086 mMainView.goBack();
2087 }
2088 }
2089
2090 public void goForward() {
2091 if (mMainView != null) {
2092 mMainView.goForward();
2093 }
2094 }
2095
John Reck1cf4b792011-07-26 10:22:22 -07002096 protected void persistThumbnail() {
John Reck4eadc342011-10-31 14:04:10 -07002097 DataController.getInstance(mContext).saveThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07002098 }
2099
2100 protected void deleteThumbnail() {
John Reck4eadc342011-10-31 14:04:10 -07002101 DataController.getInstance(mContext).deleteThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07002102 }
2103
John Reck4eadc342011-10-31 14:04:10 -07002104 void updateCaptureFromBlob(byte[] blob) {
John Reck1cf4b792011-07-26 10:22:22 -07002105 synchronized (Tab.this) {
2106 if (mCapture == null) {
2107 return;
2108 }
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002109 ByteBuffer buffer = ByteBuffer.wrap(blob);
Michael Kolbd837a112011-08-09 14:00:18 -07002110 try {
2111 mCapture.copyPixelsFromBuffer(buffer);
2112 } catch (RuntimeException rex) {
2113 Log.e(LOGTAG, "Load capture has mismatched sizes; buffer: "
2114 + buffer.capacity() + " blob: " + blob.length
2115 + "capture: " + mCapture.getByteCount());
2116 throw rex;
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002117 }
John Reck1cf4b792011-07-26 10:22:22 -07002118 }
2119 }
2120
John Reck52be4782011-08-26 15:37:29 -07002121 @Override
2122 public String toString() {
2123 StringBuilder builder = new StringBuilder(100);
2124 builder.append(mId);
2125 builder.append(") has parent: ");
2126 if (getParent() != null) {
2127 builder.append("true[");
2128 builder.append(getParent().getId());
2129 builder.append("]");
2130 } else {
2131 builder.append("false");
2132 }
2133 builder.append(", incog: ");
2134 builder.append(isPrivateBrowsingEnabled());
2135 if (!isPrivateBrowsingEnabled()) {
2136 builder.append(", title: ");
2137 builder.append(getTitle());
2138 builder.append(", url: ");
2139 builder.append(getUrl());
2140 }
2141 return builder.toString();
2142 }
2143
Steve Block4895b012011-10-03 16:26:46 +01002144 private void handleProceededAfterSslError(SslError error) {
2145 if (error.getUrl().equals(mCurrentState.mUrl)) {
2146 // The security state should currently be SECURITY_STATE_SECURE.
2147 setSecurityState(SecurityState.SECURITY_STATE_BAD_CERTIFICATE);
Steve Block08a6f0c2011-10-06 12:12:53 +01002148 mCurrentState.mSslCertificateError = error;
Steve Block4895b012011-10-03 16:26:46 +01002149 } else if (getSecurityState() == SecurityState.SECURITY_STATE_SECURE) {
Steve Block08a6f0c2011-10-06 12:12:53 +01002150 // The page's main resource is secure and this error is for a
2151 // sub-resource.
Steve Block4895b012011-10-03 16:26:46 +01002152 setSecurityState(SecurityState.SECURITY_STATE_MIXED);
2153 }
2154 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002155}