blob: 99d57a88d74bf237dc7ec1793132808cb21174dd [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.URLUtil;
John Reck438bf462011-01-12 18:11:46 -080054import android.webkit.WebResourceResponse;
Grace Kloba22ac16e2009-10-07 18:00:23 -070055import android.webkit.WebStorage;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080056import android.webkit.WebChromeClient.CustomViewCallback;
57import android.webkit.ValueCallback;
Ben Murdoch1d676b62011-01-17 12:54:24 +000058import android.widget.CheckBox;
Ben Murdoch8029a772010-11-16 11:58:21 +000059import android.widget.Toast;
Grace Kloba22ac16e2009-10-07 18:00:23 -070060
Bijan Amirzada41242f22014-03-21 12:12:18 -070061import com.android.browser.TabControl.OnThumbnailUpdatedListener;
62import com.android.browser.homepages.HomeProvider;
63import com.android.browser.mynavigation.MyNavigationUtil;
64import com.android.browser.provider.MyNavigationProvider;
65import com.android.browser.provider.SnapshotProvider.Snapshots;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080066
67import org.codeaurora.swe.BrowserDownloadListener;
68import org.codeaurora.swe.ClientCertRequestHandler;
69import org.codeaurora.swe.HttpAuthHandler;
70import org.codeaurora.swe.SslErrorHandler;
71import org.codeaurora.swe.WebBackForwardList;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080072import org.codeaurora.swe.WebChromeClient;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080073import org.codeaurora.swe.WebView;
74import org.codeaurora.swe.WebView.PictureListener;
Pankaj Garg1c7380d2014-08-27 14:17:12 -070075import org.codeaurora.swe.WebView.CreateWindowParams;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080076import org.codeaurora.swe.WebViewClient;
Pankaj Garg1c13cab2015-05-12 11:52:17 -070077import org.codeaurora.swe.util.Observable;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080078
John Reck541f55a2011-06-07 16:34:43 -070079import java.io.ByteArrayOutputStream;
John Reck2b71d6d2012-04-18 17:42:06 -070080import java.io.File;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080081import java.io.InputStream;
John Reck1cf4b792011-07-26 10:22:22 -070082import java.nio.ByteBuffer;
Michael Kolbfe251992010-07-08 15:41:55 -070083import java.util.Map;
John Reck2b71d6d2012-04-18 17:42:06 -070084import java.util.UUID;
Michael Kolbfe251992010-07-08 15:41:55 -070085import java.util.Vector;
Vivek Sekhar53ef8932015-06-18 16:51:43 -070086import java.util.List;
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 // Main WebView wrapper
Michael Kolba713ec82010-11-29 17:27:06 -0800138 private View mContainer;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700139 // Main WebView
140 private WebView mMainView;
141 // Subwindow container
142 private View mSubViewContainer;
143 // Subwindow WebView
144 private WebView mSubView;
145 // Saved bundle for when we are running low on memory. It contains the
146 // information needed to restore the WebView if the user goes back to the
147 // tab.
148 private Bundle mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700149 // Parent Tab. This is the Tab that created this Tab, or null if the Tab was
150 // created by the UI
Michael Kolbc831b632011-05-11 09:30:34 -0700151 private Tab mParent;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700152 // Tab that constructed by this Tab. This is used when this Tab is
153 // destroyed, it clears all mParentTab values in the children.
Michael Kolbc831b632011-05-11 09:30:34 -0700154 private Vector<Tab> mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700155 // If true, the tab is in the foreground of the current activity.
156 private boolean mInForeground;
Michael Kolb8233fac2010-10-26 16:08:53 -0700157 // If true, the tab is in page loading state (after onPageStarted,
158 // before onPageFinsihed)
159 private boolean mInPageLoad;
Tarun Nainani8eb00912014-07-17 12:28:32 -0700160 private boolean mPageFinished;
John Reck38b39652012-06-05 09:22:59 -0700161 private boolean mDisableOverrideUrlLoading;
Pankaj Garg79878492015-04-01 14:48:21 -0700162 private boolean mFirstVisualPixelPainted = false;
John Reck30c714c2010-12-16 17:30:34 -0800163 // The last reported progress of the current page
164 private int mPageLoadProgress;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000165 // The time the load started, used to find load page time
166 private long mLoadStartTime;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700167 // Application identifier used to find tabs that another application wants
168 // to reuse.
169 private String mAppId;
Michael Kolbe28b3472011-08-04 16:54:31 -0700170 // flag to indicate if tab should be closed on back
171 private boolean mCloseOnBack;
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700172 // flag to indicate if the tab was opened from an intent
173 private boolean mDerivedFromIntent = false;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -0500174 // The listener that gets invoked when a download is started from the
175 // mMainView
Selim Gurun0b3d66f2012-08-29 13:08:13 -0700176 private final BrowserDownloadListener mDownloadListener;
John Recke969cc52010-12-21 17:24:43 -0800177 private DataController mDataController;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700178
179 // AsyncTask for downloading touch icons
180 DownloadTouchIcon mTouchIconLoader;
181
John Reck35e9dd62011-04-25 09:01:54 -0700182 private BrowserSettings mSettings;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700183 private int mCaptureWidth;
184 private int mCaptureHeight;
185 private Bitmap mCapture;
186 private Handler mHandler;
Michael Kolb72864272012-05-03 15:42:15 -0700187 private boolean mUpdateThumbnail;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800188 private Timestamp timestamp;
Vivek Sekharf96064b2014-07-28 16:32:34 -0700189 private boolean mFullScreen = false;
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800190 private boolean mReceivedError;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700191
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -0700192 // determine if webview is destroyed to MemoryMonitor
193 private boolean mWebViewDestroyedByMemoryMonitor;
194
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700195 private Observable mFirstPixelObservable;
196 private Observable mTabHistoryUpdateObservable;
197
198 Observable getFirstPixelObservable() {
199 return mFirstPixelObservable;
200 }
201
202 Observable getTabHistoryUpdateObservable() {
203 return mTabHistoryUpdateObservable;
204 }
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -0700205
Mathew Inwood1dd8e822011-08-03 14:34:29 +0100206
John Reck1cf4b792011-07-26 10:22:22 -0700207 private static synchronized Bitmap getDefaultFavicon(Context context) {
208 if (sDefaultFavicon == null) {
209 sDefaultFavicon = BitmapFactory.decodeResource(
Enrico Rosd6efa972014-12-02 19:49:59 -0800210 context.getResources(), R.drawable.ic_deco_favicon_normal);
John Reck1cf4b792011-07-26 10:22:22 -0700211 }
212 return sDefaultFavicon;
213 }
Michael Kolbeb95db42011-03-03 10:38:40 -0800214
John Reck30c714c2010-12-16 17:30:34 -0800215 // All the state needed for a page
John Reckd8c74522011-06-14 08:45:00 -0700216 protected static class PageState {
John Reck30c714c2010-12-16 17:30:34 -0800217 String mUrl;
John Reckdb22ec42011-06-29 11:31:24 -0700218 String mOriginalUrl;
John Reck30c714c2010-12-16 17:30:34 -0800219 String mTitle;
Steve Block2466eff2011-10-03 15:33:09 +0100220 SecurityState mSecurityState;
Steve Block08a6f0c2011-10-06 12:12:53 +0100221 // This is non-null only when mSecurityState is SECURITY_STATE_BAD_CERTIFICATE.
222 SslError mSslCertificateError;
John Reck30c714c2010-12-16 17:30:34 -0800223 Bitmap mFavicon;
Steve Block08a6f0c2011-10-06 12:12:53 +0100224 boolean mIsBookmarkedSite;
225 boolean mIncognito;
John Reck30c714c2010-12-16 17:30:34 -0800226
227 PageState(Context c, boolean incognito) {
John Reck502a3532011-08-16 14:21:46 -0700228 mIncognito = incognito;
229 if (mIncognito) {
Vivek Sekhared791da2015-02-22 12:39:05 -0800230 mOriginalUrl = mUrl = "chrome://incognito";
John Reck30c714c2010-12-16 17:30:34 -0800231 mTitle = c.getString(R.string.new_incognito_tab);
John Reck30c714c2010-12-16 17:30:34 -0800232 } else {
John Reckdb22ec42011-06-29 11:31:24 -0700233 mOriginalUrl = mUrl = "";
John Reck30c714c2010-12-16 17:30:34 -0800234 mTitle = c.getString(R.string.new_tab);
John Reck30c714c2010-12-16 17:30:34 -0800235 }
Steve Block2466eff2011-10-03 15:33:09 +0100236 mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
John Reck30c714c2010-12-16 17:30:34 -0800237 }
238
239 PageState(Context c, boolean incognito, String url, Bitmap favicon) {
John Reck502a3532011-08-16 14:21:46 -0700240 mIncognito = incognito;
John Reckdb22ec42011-06-29 11:31:24 -0700241 mOriginalUrl = mUrl = url;
John Reck30c714c2010-12-16 17:30:34 -0800242 if (URLUtil.isHttpsUrl(url)) {
Steve Block2466eff2011-10-03 15:33:09 +0100243 mSecurityState = SecurityState.SECURITY_STATE_SECURE;
John Reck30c714c2010-12-16 17:30:34 -0800244 } else {
Steve Block2466eff2011-10-03 15:33:09 +0100245 mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
John Reck30c714c2010-12-16 17:30:34 -0800246 }
John Reck1cf4b792011-07-26 10:22:22 -0700247 mFavicon = favicon;
John Reck30c714c2010-12-16 17:30:34 -0800248 }
John Reck1cf4b792011-07-26 10:22:22 -0700249
Grace Kloba22ac16e2009-10-07 18:00:23 -0700250 }
251
John Reck30c714c2010-12-16 17:30:34 -0800252 // The current/loading page's state
John Reckd8c74522011-06-14 08:45:00 -0700253 protected PageState mCurrentState;
John Reck30c714c2010-12-16 17:30:34 -0800254
Grace Kloba22ac16e2009-10-07 18:00:23 -0700255 // Used for saving and restoring each Tab
Michael Kolbc831b632011-05-11 09:30:34 -0700256 static final String ID = "ID";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700257 static final String CURRURL = "currentUrl";
258 static final String CURRTITLE = "currentTitle";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700259 static final String PARENTTAB = "parentTab";
260 static final String APPID = "appid";
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700261 static final String INCOGNITO = "privateBrowsingEnabled";
John Reckb0a86db2011-05-24 14:05:58 -0700262 static final String USERAGENT = "useragent";
Michael Kolbe28b3472011-08-04 16:54:31 -0700263 static final String CLOSEFLAG = "closeOnBack";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700264
Pankaj Garg18186a92015-03-31 14:59:33 -0700265 public void setNetworkAvailable(boolean networkUp) {
266 if (networkUp && mReceivedError && (mMainView != null)) {
267 mMainView.reload();
268 }
269 }
270
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700271 public boolean isFirstVisualPixelPainted() {
272 return mFirstVisualPixelPainted;
273 }
274
275 public int getCaptureIndex(int navIndex) {
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700276 int orientation = mWebViewController.getActivity().
277 getResources().getConfiguration().orientation;
278
279 int orientationBit = (orientation == Configuration.ORIENTATION_LANDSCAPE) ? 0 : 1;
280
Vivek Sekhard0f60402015-06-05 14:07:11 -0700281 int index = orientationBit << 31 | (((int)mId & 0x7f) << 24) | (navIndex & 0xffffff);
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700282 return index;
283 }
284
285 public int getTabIdxFromCaptureIdx(int index) {
286 return (index & 0x7f000000) >> 24;
287 }
288
289 public int getOrientationFromCaptureIdx(int index) {
290 return ((index & 0x80000000) == 0) ? Configuration.ORIENTATION_LANDSCAPE :
291 Configuration.ORIENTATION_PORTRAIT;
292
293 }
294
295 public int getNavIdxFromCaptureIdx(int index) {
296 return (index & 0xffffff);
297 }
298
Grace Kloba22ac16e2009-10-07 18:00:23 -0700299 // -------------------------------------------------------------------------
300 // WebViewClient implementation for the main WebView
301 // -------------------------------------------------------------------------
302
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800303 private final WebViewClient mWebViewClient = new WebViewClient() {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500304 private Message mDontResend;
305 private Message mResend;
Michael Kolb47bd1e42011-09-01 15:25:00 -0700306
307 private boolean providersDiffer(String url, String otherUrl) {
308 Uri uri1 = Uri.parse(url);
309 Uri uri2 = Uri.parse(otherUrl);
310 return !uri1.getEncodedAuthority().equals(uri2.getEncodedAuthority());
311 }
312
Grace Kloba22ac16e2009-10-07 18:00:23 -0700313 @Override
314 public void onPageStarted(WebView view, String url, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700315 mInPageLoad = true;
Tarun Nainani8eb00912014-07-17 12:28:32 -0700316 mPageFinished = false;
Pankaj Garg79878492015-04-01 14:48:21 -0700317 mFirstVisualPixelPainted = false;
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700318 mFirstPixelObservable.set(false);
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800319 mReceivedError = false;
Michael Kolb72864272012-05-03 15:42:15 -0700320 mUpdateThumbnail = true;
Michael Kolba53c9892011-10-05 13:31:40 -0700321 mPageLoadProgress = INITIAL_PROGRESS;
Michael Kolb14612442011-06-24 13:06:29 -0700322 mCurrentState = new PageState(mContext,
John Reck30c714c2010-12-16 17:30:34 -0800323 view.isPrivateBrowsingEnabled(), url, favicon);
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000324 mLoadStartTime = SystemClock.uptimeMillis();
Pankaj Garg62bc7912015-04-14 16:08:59 -0700325 // Need re-enable FullScreenMode on Page navigation if needed
326 if (BrowserSettings.getInstance().useFullscreen()){
327 Controller controller = (Controller) mWebViewController;
328 BaseUi ui = (BaseUi) controller.getUi();
329 ui.forceDisableFullscreenMode(false);
330 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700331 // If we start a touch icon load and then load a new page, we don't
332 // want to cancel the current touch icon loader. But, we do want to
333 // create a new one when the touch icon url is known.
334 if (mTouchIconLoader != null) {
335 mTouchIconLoader.mTab = null;
336 mTouchIconLoader = null;
337 }
338
Grace Kloba22ac16e2009-10-07 18:00:23 -0700339 // finally update the UI in the activity if it is in the foreground
John Reck324d4402011-01-11 16:56:42 -0800340 mWebViewController.onPageStarted(Tab.this, view, favicon);
Leon Scroggins4cd97792010-12-03 15:31:56 -0500341
John Recke969cc52010-12-21 17:24:43 -0800342 updateBookmarkedStatus();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700343 }
344
345 @Override
346 public void onPageFinished(WebView view, String url) {
John Reck38b39652012-06-05 09:22:59 -0700347 mDisableOverrideUrlLoading = false;
John Reck5b691842010-11-29 11:21:13 -0800348 if (!isPrivateBrowsingEnabled()) {
349 LogTag.logPageFinishedLoading(
350 url, SystemClock.uptimeMillis() - mLoadStartTime);
351 }
John Reck1cf4b792011-07-26 10:22:22 -0700352 syncCurrentState(view, url);
John Reck324d4402011-01-11 16:56:42 -0800353 mWebViewController.onPageFinished(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700354 }
355
Pankaj Garg79878492015-04-01 14:48:21 -0700356 @Override
357 public void onFirstVisualPixel(WebView view) {
358 mFirstVisualPixelPainted = true;
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700359 mFirstPixelObservable.set(true);
Pankaj Garg79878492015-04-01 14:48:21 -0700360 }
361
Grace Kloba22ac16e2009-10-07 18:00:23 -0700362 // return true if want to hijack the url to let another app to handle it
363 @Override
364 public boolean shouldOverrideUrlLoading(WebView view, String url) {
John Reck38b39652012-06-05 09:22:59 -0700365 if (!mDisableOverrideUrlLoading && mInForeground) {
Michael Kolb18eb3772010-12-10 14:29:51 -0800366 return mWebViewController.shouldOverrideUrlLoading(Tab.this,
367 view, url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700368 } else {
369 return false;
370 }
371 }
372
Vivek Sekharb991edb2014-12-17 18:18:07 -0800373 @Override
374 public boolean shouldDownloadFavicon(WebView view, String url) {
375 return true;
376 }
377
Grace Kloba22ac16e2009-10-07 18:00:23 -0700378 /**
Steve Block2466eff2011-10-03 15:33:09 +0100379 * Updates the security state. This method is called when we discover
380 * another resource to be loaded for this page (for example,
381 * javascript). While we update the security state, we do not update
382 * the lock icon until we are done loading, as it is slightly more
383 * secure this way.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700384 */
385 @Override
386 public void onLoadResource(WebView view, String url) {
387 if (url != null && url.length() > 0) {
388 // It is only if the page claims to be secure that we may have
Steve Block2466eff2011-10-03 15:33:09 +0100389 // to update the security state:
390 if (mCurrentState.mSecurityState == SecurityState.SECURITY_STATE_SECURE) {
391 // If NOT a 'safe' url, change the state to mixed content!
Grace Kloba22ac16e2009-10-07 18:00:23 -0700392 if (!(URLUtil.isHttpsUrl(url) || URLUtil.isDataUrl(url)
393 || URLUtil.isAboutUrl(url))) {
Steve Block2466eff2011-10-03 15:33:09 +0100394 mCurrentState.mSecurityState = SecurityState.SECURITY_STATE_MIXED;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700395 }
396 }
397 }
398 }
399
400 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700401 * Show a dialog informing the user of the network error reported by
402 * WebCore if it is in the foreground.
403 */
404 @Override
405 public void onReceivedError(WebView view, int errorCode,
406 String description, String failingUrl) {
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800407 // Used for the syncCurrentState to use
408 // the failing url instead of using webview url
409 mReceivedError = true;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700410 }
411
412 /**
413 * Check with the user if it is ok to resend POST data as the page they
414 * are trying to navigate to is the result of a POST.
415 */
416 @Override
417 public void onFormResubmission(WebView view, final Message dontResend,
418 final Message resend) {
419 if (!mInForeground) {
420 dontResend.sendToTarget();
421 return;
422 }
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500423 if (mDontResend != null) {
424 Log.w(LOGTAG, "onFormResubmission should not be called again "
425 + "while dialog is still up");
426 dontResend.sendToTarget();
427 return;
428 }
429 mDontResend = dontResend;
430 mResend = resend;
Michael Kolb14612442011-06-24 13:06:29 -0700431 new AlertDialog.Builder(mContext).setTitle(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700432 R.string.browserFrameFormResubmitLabel).setMessage(
433 R.string.browserFrameFormResubmitMessage)
434 .setPositiveButton(R.string.ok,
435 new DialogInterface.OnClickListener() {
436 public void onClick(DialogInterface dialog,
437 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500438 if (mResend != null) {
439 mResend.sendToTarget();
440 mResend = null;
441 mDontResend = null;
442 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700443 }
444 }).setNegativeButton(R.string.cancel,
445 new DialogInterface.OnClickListener() {
446 public void onClick(DialogInterface dialog,
447 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500448 if (mDontResend != null) {
449 mDontResend.sendToTarget();
450 mResend = null;
451 mDontResend = null;
452 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700453 }
454 }).setOnCancelListener(new OnCancelListener() {
455 public void onCancel(DialogInterface dialog) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500456 if (mDontResend != null) {
457 mDontResend.sendToTarget();
458 mResend = null;
459 mDontResend = null;
460 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700461 }
462 }).show();
463 }
464
465 /**
466 * Insert the url into the visited history database.
467 * @param url The url to be inserted.
468 * @param isReload True if this url is being reloaded.
469 * FIXME: Not sure what to do when reloading the page.
470 */
471 @Override
472 public void doUpdateVisitedHistory(WebView view, String url,
473 boolean isReload) {
John Reck324d4402011-01-11 16:56:42 -0800474 mWebViewController.doUpdateVisitedHistory(Tab.this, isReload);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700475 }
476
477 /**
478 * Displays SSL error(s) dialog to the user.
479 */
480 @Override
481 public void onReceivedSslError(final WebView view,
482 final SslErrorHandler handler, final SslError error) {
483 if (!mInForeground) {
484 handler.cancel();
Steve Block2466eff2011-10-03 15:33:09 +0100485 setSecurityState(SecurityState.SECURITY_STATE_NOT_SECURE);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700486 return;
487 }
John Reck35e9dd62011-04-25 09:01:54 -0700488 if (mSettings.showSecurityWarnings()) {
John Reckcb28b2c2011-08-26 17:39:44 -0700489 new AlertDialog.Builder(mContext)
490 .setTitle(R.string.security_warning)
491 .setMessage(R.string.ssl_warnings_header)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200492 .setIconAttribute(android.R.attr.alertDialogIcon)
John Reckcb28b2c2011-08-26 17:39:44 -0700493 .setPositiveButton(R.string.ssl_continue,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700494 new DialogInterface.OnClickListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700495 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700496 public void onClick(DialogInterface dialog,
497 int whichButton) {
498 handler.proceed();
Steve Block4895b012011-10-03 16:26:46 +0100499 handleProceededAfterSslError(error);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700500 }
John Reckcb28b2c2011-08-26 17:39:44 -0700501 })
502 .setNeutralButton(R.string.view_certificate,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700503 new DialogInterface.OnClickListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700504 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700505 public void onClick(DialogInterface dialog,
506 int whichButton) {
John Reckcb28b2c2011-08-26 17:39:44 -0700507 mWebViewController.showSslCertificateOnError(
508 view, handler, error);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700509 }
John Reckcb28b2c2011-08-26 17:39:44 -0700510 })
511 .setNegativeButton(R.string.ssl_go_back,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700512 new DialogInterface.OnClickListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700513 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700514 public void onClick(DialogInterface dialog,
515 int whichButton) {
John Reck30c714c2010-12-16 17:30:34 -0800516 dialog.cancel();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700517 }
John Reckcb28b2c2011-08-26 17:39:44 -0700518 })
519 .setOnCancelListener(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700520 new DialogInterface.OnCancelListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700521 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700522 public void onCancel(DialogInterface dialog) {
523 handler.cancel();
Steve Block2466eff2011-10-03 15:33:09 +0100524 setSecurityState(SecurityState.SECURITY_STATE_NOT_SECURE);
John Reck30c714c2010-12-16 17:30:34 -0800525 mWebViewController.onUserCanceledSsl(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700526 }
John Reckcb28b2c2011-08-26 17:39:44 -0700527 })
528 .show();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700529 } else {
530 handler.proceed();
531 }
532 }
533
534 /**
Steve Block4895b012011-10-03 16:26:46 +0100535 * Called when an SSL error occurred while loading a resource, but the
536 * WebView but chose to proceed anyway based on a decision retained
537 * from a previous response to onReceivedSslError(). We update our
538 * security state to reflect this.
539 */
540 @Override
541 public void onProceededAfterSslError(WebView view, SslError error) {
542 handleProceededAfterSslError(error);
543 }
544
545 /**
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700546 * Displays client certificate request to the user.
547 */
548 @Override
549 public void onReceivedClientCertRequest(final WebView view,
550 final ClientCertRequestHandler handler, final String host_and_port) {
551 if (!mInForeground) {
552 handler.ignore();
553 return;
554 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700555 int colon = host_and_port.lastIndexOf(':');
556 String host;
557 int port;
558 if (colon == -1) {
559 host = host_and_port;
560 port = -1;
561 } else {
562 String portString = host_and_port.substring(colon + 1);
563 try {
564 port = Integer.parseInt(portString);
565 host = host_and_port.substring(0, colon);
566 } catch (NumberFormatException e) {
567 host = host_and_port;
568 port = -1;
569 }
570 }
Michael Kolb14612442011-06-24 13:06:29 -0700571 KeyChain.choosePrivateKeyAlias(
572 mWebViewController.getActivity(), new KeyChainAliasCallback() {
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700573 @Override public void alias(String alias) {
574 if (alias == null) {
575 handler.cancel();
576 return;
577 }
Michael Kolb14612442011-06-24 13:06:29 -0700578 new KeyChainLookup(mContext, handler, alias).execute();
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700579 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700580 }, null, null, host, port, null);
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700581 }
582
583 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700584 * Handles an HTTP authentication request.
585 *
586 * @param handler The authentication handler
587 * @param host The host
588 * @param realm The realm
589 */
590 @Override
591 public void onReceivedHttpAuthRequest(WebView view,
592 final HttpAuthHandler handler, final String host,
593 final String realm) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700594 mWebViewController.onReceivedHttpAuthRequest(Tab.this, view, handler, host, realm);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700595 }
596
597 @Override
John Reck438bf462011-01-12 18:11:46 -0800598 public WebResourceResponse shouldInterceptRequest(WebView view,
599 String url) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800600 //intercept if opening a new incognito tab - show the incognito welcome page
Vivek Sekhared791da2015-02-22 12:39:05 -0800601 if (url.startsWith("chrome://incognito")) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800602 Resources resourceHandle = mContext.getResources();
603 InputStream inStream = resourceHandle.openRawResource(
Bijan Amirzada41242f22014-03-21 12:12:18 -0700604 com.android.browser.R.raw.incognito_mode_start_page);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800605 return new WebResourceResponse("text/html", "utf8", inStream);
606 }
kaiyiz6e5b3e02013-08-19 20:02:01 +0800607 WebResourceResponse res;
608 if (MyNavigationUtil.MY_NAVIGATION.equals(url)) {
609 res = MyNavigationProvider.shouldInterceptRequest(mContext, url);
610 } else {
611 res = HomeProvider.shouldInterceptRequest(mContext, url);
612 }
John Reck438bf462011-01-12 18:11:46 -0800613 return res;
614 }
615
616 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700617 public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
618 if (!mInForeground) {
619 return false;
620 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700621 return mWebViewController.shouldOverrideKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700622 }
623
624 @Override
625 public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700626 if (!mInForeground) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700627 return;
628 }
John Reck997b1b72012-04-19 18:08:25 -0700629 if (!mWebViewController.onUnhandledKeyEvent(event)) {
630 super.onUnhandledKeyEvent(view, event);
631 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700632 }
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700633
634 @Override
635 public void beforeNavigation(WebView view, String url) {
636 if (isPrivateBrowsingEnabled()) {
637 return;
638 }
639
640 if (!mFirstVisualPixelPainted) {
641 return;
642 }
643
644 final int idx = view.copyBackForwardList().getCurrentIndex();
645 boolean bitmapExists = view.hasSnapshot(idx);
646
647 int progress = 100;
648 Controller controller = (Controller)mWebViewController;
649 UI ui = controller.getUi();
650 if (ui instanceof BaseUi) {
651 BaseUi baseUi = (BaseUi) ui;
652 TitleBar titleBar = baseUi.getTitleBar();
653 progress = titleBar.getProgressView().getProgressPercent();
654 }
655
656 if (bitmapExists && progress < 85) {
657 return;
658 }
659
660 int index = getCaptureIndex(view.getLastCommittedHistoryIndex());
661 view.captureSnapshot(index , null);
662 }
663
664 @Override
665 public void onHistoryItemCommit(WebView view, int index) {
666 mTabHistoryUpdateObservable.set(index);
Vivek Sekhar53ef8932015-06-18 16:51:43 -0700667 final int maxIdx = view.copyBackForwardList().getSize();
668 final WebView wv = view;
669 view.getSnapshotIds(new ValueCallback <List<Integer>>() {
670 @Override
671 public void onReceiveValue(List<Integer> ids) {
672 int currentTabIdx = mWebViewController.getTabControl().getCurrentPosition();
673 for (Integer id : ids) {
674 if (getTabIdxFromCaptureIdx(id) == currentTabIdx &&
675 getNavIdxFromCaptureIdx(id) >= maxIdx) {
676 wv.deleteSnapshot(id);
677 }
678 }
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700679 }
Vivek Sekhar53ef8932015-06-18 16:51:43 -0700680 });
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700681 }
Pankaj Garg62bc7912015-04-14 16:08:59 -0700682
683 @Override
684 public void onKeyboardStateChange(boolean popup) {
685 if (BrowserSettings.getInstance().useFullscreen()) {
686 Controller controller = (Controller) mWebViewController;
687 BaseUi ui = (BaseUi) controller.getUi();
688 ui.forceDisableFullscreenMode(popup);
689 }
690 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700691 };
692
John Reck1cf4b792011-07-26 10:22:22 -0700693 private void syncCurrentState(WebView view, String url) {
694 // Sync state (in case of stop/timeout)
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800695
696 if (mReceivedError) {
697 mCurrentState.mUrl = url;
698 mCurrentState.mOriginalUrl = url;
699 } else {
700 mCurrentState.mUrl = view.getUrl();
701 mCurrentState.mOriginalUrl = view.getOriginalUrl();
702 mCurrentState.mFavicon = view.getFavicon();
703 }
704
John Reck1cf4b792011-07-26 10:22:22 -0700705 if (mCurrentState.mUrl == null) {
John Reck8bcafc12011-08-29 16:43:02 -0700706 mCurrentState.mUrl = "";
John Reck1cf4b792011-07-26 10:22:22 -0700707 }
John Reck1cf4b792011-07-26 10:22:22 -0700708 mCurrentState.mTitle = view.getTitle();
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800709
710
John Reck1cf4b792011-07-26 10:22:22 -0700711 if (!URLUtil.isHttpsUrl(mCurrentState.mUrl)) {
712 // In case we stop when loading an HTTPS page from an HTTP page
713 // but before a provisional load occurred
Steve Block2466eff2011-10-03 15:33:09 +0100714 mCurrentState.mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
Steve Block08a6f0c2011-10-06 12:12:53 +0100715 mCurrentState.mSslCertificateError = null;
John Reck1cf4b792011-07-26 10:22:22 -0700716 }
John Reck502a3532011-08-16 14:21:46 -0700717 mCurrentState.mIncognito = view.isPrivateBrowsingEnabled();
John Reck1cf4b792011-07-26 10:22:22 -0700718 }
719
Tarun Nainani8eb00912014-07-17 12:28:32 -0700720
721 public boolean isTabFullScreen() {
722 return mFullScreen;
723 }
724
Vivek Sekharf96064b2014-07-28 16:32:34 -0700725 protected void setTabFullscreen(boolean fullScreen) {
Tarun Nainani8eb00912014-07-17 12:28:32 -0700726 Controller controller = (Controller)mWebViewController;
Sudheer Koganti24766882014-10-02 10:58:09 -0700727 controller.getUi().showFullscreen(fullScreen);
Tarun Nainani8eb00912014-07-17 12:28:32 -0700728 mFullScreen = fullScreen;
Vivek Sekharf96064b2014-07-28 16:32:34 -0700729 }
730
Sudheer Koganti24766882014-10-02 10:58:09 -0700731 public boolean exitFullscreen() {
732 if (mFullScreen) {
733 Controller controller = (Controller)mWebViewController;
734 controller.getUi().showFullscreen(false);
735 if (getWebView() != null)
736 getWebView().exitFullscreen();
737 mFullScreen = false;
738 return true;
739 }
740 return false;
741 }
742
743
744
745
Grace Kloba22ac16e2009-10-07 18:00:23 -0700746 // -------------------------------------------------------------------------
747 // WebChromeClient implementation for the main WebView
748 // -------------------------------------------------------------------------
749
750 private final WebChromeClient mWebChromeClient = new WebChromeClient() {
751 // Helper method to create a new tab or sub window.
752 private void createWindow(final boolean dialog, final Message msg) {
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700753 this.createWindow(dialog, msg, null, false);
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700754 }
755
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700756 private void createWindow(final boolean dialog, final Message msg, final String url,
757 final boolean opener_suppressed) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700758 WebView.WebViewTransport transport =
759 (WebView.WebViewTransport) msg.obj;
760 if (dialog) {
761 createSubWindow();
Michael Kolb8233fac2010-10-26 16:08:53 -0700762 mWebViewController.attachSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700763 transport.setWebView(mSubView);
764 } else {
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700765 final Tab newTab = mWebViewController.openTab(url,
John Reck5949c662011-05-27 09:52:29 -0700766 Tab.this, true, true);
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700767 // This is special case for rendering links on a webpage in
768 // a new tab. If opener is suppressed, the WebContents created
769 // by the content layer are not fully initialized. This check
770 // will prevent content layer from overriding WebContents
771 // created by new tab with the uninitialized instance.
772 if (!opener_suppressed) {
773 transport.setWebView(newTab.getWebView());
774 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700775 }
776 msg.sendToTarget();
777 }
778
779 @Override
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700780 public void toggleFullscreenModeForTab(boolean enterFullscreen) {
781 if (mWebViewController instanceof Controller) {
Vivek Sekharf96064b2014-07-28 16:32:34 -0700782 setTabFullscreen(enterFullscreen);
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700783 }
784 }
785
786 @Override
Tarun Nainani8eb00912014-07-17 12:28:32 -0700787 public void onOffsetsForFullscreenChanged(float topControlsOffsetYPix,
788 float contentOffsetYPix,
789 float overdrawBottomHeightPix) {
790 if (mWebViewController instanceof Controller) {
Vivek Sekhar8ee3abb2014-07-14 12:32:05 -0700791 Controller controller = (Controller)mWebViewController;
Tarun Nainani8eb00912014-07-17 12:28:32 -0700792 controller.getUi().translateTitleBar(topControlsOffsetYPix);
Vivek Sekhar8ee3abb2014-07-14 12:32:05 -0700793 }
794 }
795
796 @Override
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700797 public boolean isTabFullScreen() {
Vivek Sekharf96064b2014-07-28 16:32:34 -0700798 return mFullScreen;
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700799 }
800
801 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700802 public boolean onCreateWindow(WebView view, final boolean dialog,
803 final boolean userGesture, final Message resultMsg) {
804 // only allow new window or sub window for the foreground case
805 if (!mInForeground) {
806 return false;
807 }
808 // Short-circuit if we can't create any more tabs or sub windows.
809 if (dialog && mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -0700810 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700811 .setTitle(R.string.too_many_subwindows_dialog_title)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200812 .setIconAttribute(android.R.attr.alertDialogIcon)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700813 .setMessage(R.string.too_many_subwindows_dialog_message)
814 .setPositiveButton(R.string.ok, null)
815 .show();
816 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700817 } else if (!mWebViewController.getTabControl().canCreateNewTab()) {
Michael Kolb14612442011-06-24 13:06:29 -0700818 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700819 .setTitle(R.string.too_many_windows_dialog_title)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200820 .setIconAttribute(android.R.attr.alertDialogIcon)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700821 .setMessage(R.string.too_many_windows_dialog_message)
822 .setPositiveButton(R.string.ok, null)
823 .show();
824 return false;
825 }
826
827 // Short-circuit if this was a user gesture.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800828 if (userGesture || !mSettings.blockPopupWindows()) {
Tarun Nainani4f5137d2015-04-16 17:26:18 -0700829 WebView.WebViewTransport transport =
830 (WebView.WebViewTransport) resultMsg.obj;
831 CreateWindowParams windowParams = transport.getCreateWindowParams();
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700832 if (windowParams.mOpenerSuppressed) {
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700833 createWindow(dialog, resultMsg, windowParams.mURL, true);
834 // This is special case for rendering links on a webpage in
835 // a new tab. If opener is suppressed, the WebContents created
836 // by the content layer are not fully initialized. Returning false
837 // will prevent content layer from overriding WebContents
838 // created by new tab with the uninitialized instance.
839 return false;
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700840 }
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700841
842 createWindow(dialog, resultMsg);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700843 return true;
844 }
845
Tarun Nainani4f5137d2015-04-16 17:26:18 -0700846 createWindow(dialog, resultMsg);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700847 return true;
848 }
849
850 @Override
Patrick Scotteb5061b2009-11-18 15:00:30 -0500851 public void onRequestFocus(WebView view) {
852 if (!mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700853 mWebViewController.switchToTab(Tab.this);
Patrick Scotteb5061b2009-11-18 15:00:30 -0500854 }
855 }
856
857 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700858 public void onCloseWindow(WebView window) {
Michael Kolbc831b632011-05-11 09:30:34 -0700859 if (mParent != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700860 // JavaScript can only close popup window.
861 if (mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700862 mWebViewController.switchToTab(mParent);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700863 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700864 mWebViewController.closeTab(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700865 }
866 }
867
868 @Override
869 public void onProgressChanged(WebView view, int newProgress) {
John Reck30c714c2010-12-16 17:30:34 -0800870 mPageLoadProgress = newProgress;
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800871 if (newProgress == 100) {
872 mInPageLoad = false;
873 }
John Reck30c714c2010-12-16 17:30:34 -0800874 mWebViewController.onProgressChanged(Tab.this);
Michael Kolb72864272012-05-03 15:42:15 -0700875 if (mUpdateThumbnail && newProgress == 100) {
876 mUpdateThumbnail = false;
877 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700878 }
879
880 @Override
Leon Scroggins21d9b902010-03-11 09:33:11 -0500881 public void onReceivedTitle(WebView view, final String title) {
John Reck30c714c2010-12-16 17:30:34 -0800882 mCurrentState.mTitle = title;
Michael Kolb8233fac2010-10-26 16:08:53 -0700883 mWebViewController.onReceivedTitle(Tab.this, title);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700884 }
885
886 @Override
887 public void onReceivedIcon(WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -0800888 mCurrentState.mFavicon = icon;
Michael Kolb8233fac2010-10-26 16:08:53 -0700889 mWebViewController.onFavicon(Tab.this, view, icon);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700890 }
891
892 @Override
893 public void onReceivedTouchIconUrl(WebView view, String url,
894 boolean precomposed) {
Michael Kolb14612442011-06-24 13:06:29 -0700895 final ContentResolver cr = mContext.getContentResolver();
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400896 // Let precomposed icons take precedence over non-composed
897 // icons.
898 if (precomposed && mTouchIconLoader != null) {
899 mTouchIconLoader.cancel(false);
900 mTouchIconLoader = null;
901 }
902 // Have only one async task at a time.
903 if (mTouchIconLoader == null) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700904 mTouchIconLoader = new DownloadTouchIcon(Tab.this,
Michael Kolb14612442011-06-24 13:06:29 -0700905 mContext, cr, view);
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400906 mTouchIconLoader.execute(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700907 }
908 }
909
910 @Override
911 public void onShowCustomView(View view,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800912 CustomViewCallback callback) {
Michael Kolb14612442011-06-24 13:06:29 -0700913 Activity activity = mWebViewController.getActivity();
914 if (activity != null) {
915 onShowCustomView(view, activity.getRequestedOrientation(), callback);
916 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400917 }
918
919 @Override
920 public void onShowCustomView(View view, int requestedOrientation,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800921 CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700922 if (mInForeground) mWebViewController.showCustomView(Tab.this, view,
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400923 requestedOrientation, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700924 }
925
926 @Override
927 public void onHideCustomView() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700928 if (mInForeground) mWebViewController.hideCustomView();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700929 }
930
931 /**
932 * The origin has exceeded its database quota.
933 * @param url the URL that exceeded the quota
934 * @param databaseIdentifier the identifier of the database on which the
935 * transaction that caused the quota overflow was run
936 * @param currentQuota the current quota for the origin.
937 * @param estimatedSize the estimated size of the database.
938 * @param totalUsedQuota is the sum of all origins' quota.
939 * @param quotaUpdater The callback to run when a decision to allow or
940 * deny quota has been made. Don't forget to call this!
941 */
942 @Override
943 public void onExceededDatabaseQuota(String url,
944 String databaseIdentifier, long currentQuota, long estimatedSize,
945 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -0700946 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -0700947 .onExceededDatabaseQuota(url, databaseIdentifier,
948 currentQuota, estimatedSize, totalUsedQuota,
949 quotaUpdater);
950 }
951
952 /**
953 * The Application Cache has exceeded its max size.
954 * @param spaceNeeded is the amount of disk space that would be needed
955 * in order for the last appcache operation to succeed.
956 * @param totalUsedQuota is the sum of all origins' quota.
957 * @param quotaUpdater A callback to inform the WebCore thread that a
958 * new app cache size is available. This callback must always
959 * be executed at some point to ensure that the sleeping
960 * WebCore thread is woken up.
961 */
962 @Override
963 public void onReachedMaxAppCacheSize(long spaceNeeded,
964 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -0700965 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -0700966 .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota,
967 quotaUpdater);
968 }
969
Ben Murdoch65acc352009-11-19 18:16:04 +0000970 /* Adds a JavaScript error message to the system log and if the JS
971 * console is enabled in the about:debug options, to that console
972 * also.
Ben Murdochc42addf2010-01-28 15:19:59 +0000973 * @param consoleMessage the message object.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700974 */
975 @Override
Ben Murdochc42addf2010-01-28 15:19:59 +0000976 public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Jeff Hamilton47654f42010-09-07 09:57:51 -0500977 // Don't log console messages in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -0700978 if (isPrivateBrowsingEnabled()) return true;
Jeff Hamilton47654f42010-09-07 09:57:51 -0500979
Ben Murdochc42addf2010-01-28 15:19:59 +0000980 String message = "Console: " + consoleMessage.message() + " "
981 + consoleMessage.sourceId() + ":"
982 + consoleMessage.lineNumber();
983
984 switch (consoleMessage.messageLevel()) {
985 case TIP:
986 Log.v(CONSOLE_LOGTAG, message);
987 break;
988 case LOG:
989 Log.i(CONSOLE_LOGTAG, message);
990 break;
991 case WARNING:
992 Log.w(CONSOLE_LOGTAG, message);
993 break;
994 case ERROR:
995 Log.e(CONSOLE_LOGTAG, message);
996 break;
997 case DEBUG:
998 Log.d(CONSOLE_LOGTAG, message);
999 break;
1000 }
1001
1002 return true;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001003 }
1004
1005 /**
1006 * Ask the browser for an icon to represent a <video> element.
1007 * This icon will be used if the Web page did not specify a poster attribute.
1008 * @return Bitmap The icon or null if no such icon is available.
1009 */
1010 @Override
1011 public Bitmap getDefaultVideoPoster() {
1012 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001013 return mWebViewController.getDefaultVideoPoster();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001014 }
1015 return null;
1016 }
1017
1018 /**
1019 * Ask the host application for a custom progress view to show while
1020 * a <video> is loading.
1021 * @return View The progress view.
1022 */
1023 @Override
1024 public View getVideoLoadingProgressView() {
1025 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001026 return mWebViewController.getVideoLoadingProgressView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001027 }
1028 return null;
1029 }
1030
1031 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00001032 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001033 if (mInForeground) {
Ben Murdoch8cad4132012-01-11 10:56:43 +00001034 mWebViewController.openFileChooser(uploadMsg, acceptType, capture);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001035 } else {
1036 uploadMsg.onReceiveValue(null);
1037 }
1038 }
1039
Vivek Sekharb54614f2014-05-01 19:03:37 -07001040 @Override
1041 public void showFileChooser(ValueCallback<String[]> uploadFilePaths, String acceptTypes,
1042 boolean capture) {
1043 if (mInForeground) {
1044 mWebViewController.showFileChooser(uploadFilePaths, acceptTypes, capture);
1045 } else {
1046 uploadFilePaths.onReceiveValue(null);
1047 }
1048 }
1049
Grace Kloba22ac16e2009-10-07 18:00:23 -07001050 /**
1051 * Deliver a list of already-visited URLs
1052 */
1053 @Override
1054 public void getVisitedHistory(final ValueCallback<String[]> callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001055 mWebViewController.getVisitedHistory(callback);
1056 }
Ben Murdoch8029a772010-11-16 11:58:21 +00001057
1058 @Override
1059 public void setupAutoFill(Message message) {
1060 // Prompt the user to set up their profile.
1061 final Message msg = message;
Michael Kolb14612442011-06-24 13:06:29 -07001062 AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
1063 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
Ben Murdoch1d676b62011-01-17 12:54:24 +00001064 Context.LAYOUT_INFLATER_SERVICE);
1065 final View layout = inflater.inflate(R.layout.setup_autofill_dialog, null);
1066
1067 builder.setView(layout)
1068 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1069 @Override
1070 public void onClick(DialogInterface dialog, int id) {
1071 CheckBox disableAutoFill = (CheckBox) layout.findViewById(
1072 R.id.setup_autofill_dialog_disable_autofill);
1073
1074 if (disableAutoFill.isChecked()) {
1075 // Disable autofill and show a toast with how to turn it on again.
John Reck35e9dd62011-04-25 09:01:54 -07001076 mSettings.setAutofillEnabled(false);
Michael Kolb14612442011-06-24 13:06:29 -07001077 Toast.makeText(mContext,
Ben Murdoch1d676b62011-01-17 12:54:24 +00001078 R.string.autofill_setup_dialog_negative_toast,
1079 Toast.LENGTH_LONG).show();
1080 } else {
1081 // Take user to the AutoFill profile editor. When they return,
1082 // we will send the message that we pass here which will trigger
1083 // the form to get filled out with their new profile.
1084 mWebViewController.setupAutoFill(msg);
1085 }
1086 }
1087 })
1088 .setNegativeButton(R.string.cancel, null)
1089 .show();
Ben Murdoch8029a772010-11-16 11:58:21 +00001090 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001091 };
1092
1093 // -------------------------------------------------------------------------
1094 // WebViewClient implementation for the sub window
1095 // -------------------------------------------------------------------------
1096
1097 // Subclass of WebViewClient used in subwindows to notify the main
1098 // WebViewClient of certain WebView activities.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001099 private static class SubWindowClient extends WebViewClient {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001100 // The main WebViewClient.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001101 private final WebViewClient mClient;
Michael Kolb8233fac2010-10-26 16:08:53 -07001102 private final WebViewController mController;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001103
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001104 SubWindowClient(WebViewClient client, WebViewController controller) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001105 mClient = client;
Michael Kolb8233fac2010-10-26 16:08:53 -07001106 mController = controller;
Leon Scroggins III211ba542010-04-19 13:21:13 -04001107 }
1108 @Override
1109 public void onPageStarted(WebView view, String url, Bitmap favicon) {
1110 // Unlike the others, do not call mClient's version, which would
1111 // change the progress bar. However, we do want to remove the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001112 // find or select dialog.
Michael Kolb8233fac2010-10-26 16:08:53 -07001113 mController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001114 }
1115 @Override
1116 public void doUpdateVisitedHistory(WebView view, String url,
1117 boolean isReload) {
1118 mClient.doUpdateVisitedHistory(view, url, isReload);
1119 }
1120 @Override
1121 public boolean shouldOverrideUrlLoading(WebView view, String url) {
1122 return mClient.shouldOverrideUrlLoading(view, url);
1123 }
1124 @Override
1125 public void onReceivedSslError(WebView view, SslErrorHandler handler,
1126 SslError error) {
1127 mClient.onReceivedSslError(view, handler, error);
1128 }
1129 @Override
Brian Carlstrom8862c1d2011-06-02 01:05:55 -07001130 public void onReceivedClientCertRequest(WebView view,
1131 ClientCertRequestHandler handler, String host_and_port) {
1132 mClient.onReceivedClientCertRequest(view, handler, host_and_port);
1133 }
1134 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -07001135 public void onReceivedHttpAuthRequest(WebView view,
1136 HttpAuthHandler handler, String host, String realm) {
1137 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
1138 }
1139 @Override
1140 public void onFormResubmission(WebView view, Message dontResend,
1141 Message resend) {
1142 mClient.onFormResubmission(view, dontResend, resend);
1143 }
1144 @Override
1145 public void onReceivedError(WebView view, int errorCode,
1146 String description, String failingUrl) {
1147 mClient.onReceivedError(view, errorCode, description, failingUrl);
1148 }
1149 @Override
1150 public boolean shouldOverrideKeyEvent(WebView view,
1151 android.view.KeyEvent event) {
1152 return mClient.shouldOverrideKeyEvent(view, event);
1153 }
1154 @Override
1155 public void onUnhandledKeyEvent(WebView view,
1156 android.view.KeyEvent event) {
1157 mClient.onUnhandledKeyEvent(view, event);
1158 }
1159 }
1160
1161 // -------------------------------------------------------------------------
1162 // WebChromeClient implementation for the sub window
1163 // -------------------------------------------------------------------------
1164
1165 private class SubWindowChromeClient extends WebChromeClient {
1166 // The main WebChromeClient.
1167 private final WebChromeClient mClient;
1168
1169 SubWindowChromeClient(WebChromeClient client) {
1170 mClient = client;
1171 }
1172 @Override
1173 public void onProgressChanged(WebView view, int newProgress) {
1174 mClient.onProgressChanged(view, newProgress);
1175 }
1176 @Override
1177 public boolean onCreateWindow(WebView view, boolean dialog,
1178 boolean userGesture, android.os.Message resultMsg) {
1179 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
1180 }
1181 @Override
1182 public void onCloseWindow(WebView window) {
1183 if (window != mSubView) {
1184 Log.e(LOGTAG, "Can't close the window");
1185 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001186 mWebViewController.dismissSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001187 }
1188 }
1189
1190 // -------------------------------------------------------------------------
1191
1192 // Construct a new tab
Michael Kolb7bcafde2011-05-09 13:55:59 -07001193 Tab(WebViewController wvcontroller, WebView w) {
John Reck1cf4b792011-07-26 10:22:22 -07001194 this(wvcontroller, w, null);
1195 }
1196
1197 Tab(WebViewController wvcontroller, Bundle state) {
1198 this(wvcontroller, null, state);
1199 }
1200
1201 Tab(WebViewController wvcontroller, WebView w, Bundle state) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001202 mWebViewController = wvcontroller;
Michael Kolb14612442011-06-24 13:06:29 -07001203 mContext = mWebViewController.getContext();
John Reck35e9dd62011-04-25 09:01:54 -07001204 mSettings = BrowserSettings.getInstance();
Michael Kolb14612442011-06-24 13:06:29 -07001205 mDataController = DataController.getInstance(mContext);
1206 mCurrentState = new PageState(mContext, w != null
John Reck847b5322011-04-14 17:02:18 -07001207 ? w.isPrivateBrowsingEnabled() : false);
Tarun Nainani8084c822014-06-25 13:38:06 -07001208 setTimeStamp();
Michael Kolb8233fac2010-10-26 16:08:53 -07001209 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001210 mInForeground = false;
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001211 mWebViewDestroyedByMemoryMonitor = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001212
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001213 mDownloadListener = new BrowserDownloadListener() {
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001214 public void onDownloadStart(String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001215 String contentDisposition, String mimetype, String referer,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001216 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001217 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001218 mimetype, referer, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001219 }
1220 };
1221
John Reck1cf4b792011-07-26 10:22:22 -07001222 mCaptureWidth = mContext.getResources().getDimensionPixelSize(
1223 R.dimen.tab_thumbnail_width);
1224 mCaptureHeight = mContext.getResources().getDimensionPixelSize(
1225 R.dimen.tab_thumbnail_height);
1226 updateShouldCaptureThumbnails();
1227 restoreState(state);
John Reck52be4782011-08-26 15:37:29 -07001228 if (getId() == -1) {
1229 mId = TabControl.getNextId();
1230 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001231 setWebView(w);
Michael Kolb9ef259a2011-07-12 15:33:08 -07001232 mHandler = new Handler() {
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001233 @Override
Michael Kolb9ef259a2011-07-12 15:33:08 -07001234 public void handleMessage(Message m) {
John Reck1cf4b792011-07-26 10:22:22 -07001235 switch (m.what) {
1236 case MSG_CAPTURE:
1237 capture();
1238 break;
1239 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001240 }
1241 };
Pankaj Garg1c13cab2015-05-12 11:52:17 -07001242
1243 mFirstPixelObservable = new Observable();
1244 mFirstPixelObservable.set(false);
1245 mTabHistoryUpdateObservable = new Observable();
John Reck1cf4b792011-07-26 10:22:22 -07001246 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001247
Michael Kolb72864272012-05-03 15:42:15 -07001248 public boolean shouldUpdateThumbnail() {
1249 return mUpdateThumbnail;
1250 }
1251
Mathew Inwoode09305e2011-09-02 12:03:26 +01001252 /**
1253 * This is used to get a new ID when the tab has been preloaded, before it is displayed and
1254 * added to TabControl. Preloaded tabs can be created before restoreInstanceState, leading
1255 * to overlapping IDs between the preloaded and restored tabs.
1256 */
1257 public void refreshIdAfterPreload() {
1258 mId = TabControl.getNextId();
1259 }
1260
John Reck1cf4b792011-07-26 10:22:22 -07001261 public void updateShouldCaptureThumbnails() {
1262 if (mWebViewController.shouldCaptureThumbnails()) {
1263 synchronized (Tab.this) {
1264 if (mCapture == null) {
1265 mCapture = Bitmap.createBitmap(mCaptureWidth, mCaptureHeight,
1266 Bitmap.Config.RGB_565);
Michael Kolbc3af0672011-08-09 10:24:41 -07001267 mCapture.eraseColor(Color.WHITE);
John Reck1cf4b792011-07-26 10:22:22 -07001268 if (mInForeground) {
1269 postCapture();
1270 }
1271 }
1272 }
1273 } else {
1274 synchronized (Tab.this) {
1275 mCapture = null;
1276 deleteThumbnail();
1277 }
1278 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001279 }
1280
Michael Kolb14612442011-06-24 13:06:29 -07001281 public void setController(WebViewController ctl) {
1282 mWebViewController = ctl;
John Reck1cf4b792011-07-26 10:22:22 -07001283 updateShouldCaptureThumbnails();
Michael Kolb14612442011-06-24 13:06:29 -07001284 }
1285
Michael Kolbc831b632011-05-11 09:30:34 -07001286 public long getId() {
1287 return mId;
1288 }
1289
Michael Kolb91911a22012-01-17 11:21:25 -08001290 void setWebView(WebView w) {
1291 setWebView(w, true);
1292 }
1293
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001294 public boolean isNativeActive(){
1295 if (mMainView == null)
1296 return false;
1297 return true;
1298 }
1299
1300 public void setTimeStamp(){
1301 Date d = new Date();
1302 timestamp = (new Timestamp(d.getTime()));
1303 }
1304
1305 public Timestamp getTimestamp() {
1306 return timestamp;
1307 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001308 /**
1309 * Sets the WebView for this tab, correctly removing the old WebView from
1310 * the container view.
1311 */
Michael Kolb91911a22012-01-17 11:21:25 -08001312 void setWebView(WebView w, boolean restore) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001313 if (mMainView == w) {
1314 return;
1315 }
Michael Kolba713ec82010-11-29 17:27:06 -08001316
Michael Kolba713ec82010-11-29 17:27:06 -08001317 mWebViewController.onSetWebView(this, w);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001318
John Reck1cf4b792011-07-26 10:22:22 -07001319 if (mMainView != null) {
John Reck8ee633f2011-08-09 16:00:35 -07001320 mMainView.setPictureListener(null);
John Reck1cf4b792011-07-26 10:22:22 -07001321 if (w != null) {
1322 syncCurrentState(w, null);
1323 } else {
Panos Thomasa9a5a582014-03-18 19:20:08 -07001324 mCurrentState = new PageState(mContext, mMainView.isPrivateBrowsingEnabled());
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001325
1326 if (mWebViewDestroyedByMemoryMonitor) {
1327 /*
1328 * If tab was destroyed as a result of the MemoryMonitor
1329 * then we need to restore the state properties
1330 * from the old WebView (mMainView)
1331 */
1332 syncCurrentState(mMainView, null);
1333 mWebViewDestroyedByMemoryMonitor = false;
1334 }
John Reck1cf4b792011-07-26 10:22:22 -07001335 }
1336 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001337 // set the new one
1338 mMainView = w;
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001339
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001340 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001341 if (mMainView != null) {
1342 mMainView.setWebViewClient(mWebViewClient);
1343 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001344 // Attach DownloadManager so that downloads can start in an active
1345 // or a non-active window. This can happen when going to a site that
1346 // does a redirect after a period of time. The user could have
1347 // switched to another tab while waiting for the download to start.
1348 mMainView.setDownloadListener(mDownloadListener);
John Reck8ee633f2011-08-09 16:00:35 -07001349 TabControl tc = mWebViewController.getTabControl();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001350 if (tc != null /*&& tc.getOnThumbnailUpdatedListener() != null*/) {
John Reck8ee633f2011-08-09 16:00:35 -07001351 mMainView.setPictureListener(this);
1352 }
Michael Kolb91911a22012-01-17 11:21:25 -08001353 if (restore && (mSavedState != null)) {
John Reck8b9bb8b2012-03-08 13:19:40 -08001354 restoreUserAgent();
John Reck6c2e2f32011-08-22 13:41:23 -07001355 WebBackForwardList restoredState
1356 = mMainView.restoreState(mSavedState);
1357 if (restoredState == null || restoredState.getSize() == 0) {
1358 Log.w(LOGTAG, "Failed to restore WebView state!");
1359 loadUrl(mCurrentState.mOriginalUrl, null);
1360 }
John Reck1cf4b792011-07-26 10:22:22 -07001361 mSavedState = null;
1362 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001363 }
1364 }
1365
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001366 public void destroyThroughMemoryMonitor() {
1367 mWebViewDestroyedByMemoryMonitor = true;
1368 destroy();
1369 }
1370
Grace Kloba22ac16e2009-10-07 18:00:23 -07001371 /**
1372 * Destroy the tab's main WebView and subWindow if any
1373 */
1374 void destroy() {
Tarun Nainani2c1dd7c2014-07-05 16:40:12 -07001375
Matthew Huib7f2e9c2014-04-16 11:12:37 -04001376 if (mPostponeDestroy) {
1377 mShouldDestroy = true;
1378 return;
1379 }
1380 mShouldDestroy = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001381 if (mMainView != null) {
1382 dismissSubWindow();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001383 // save the WebView to call destroy() after detach it from the tab
Vivek Sekhar53ef8932015-06-18 16:51:43 -07001384 final WebView webView = mMainView;
Pankaj Garg1c13cab2015-05-12 11:52:17 -07001385 if (!mWebViewDestroyedByMemoryMonitor) {
Vivek Sekhar53ef8932015-06-18 16:51:43 -07001386 webView.getSnapshotIds(new ValueCallback<List<Integer>>() {
1387 @Override
1388 public void onReceiveValue(List<Integer> ids) {
1389 int currentTabIdx = mWebViewController.getTabControl().getCurrentPosition();
1390 for (Integer id : ids) {
1391 if (getTabIdxFromCaptureIdx(id) == currentTabIdx) {
1392 webView.deleteSnapshot(id);
1393 }
1394 }
1395 setWebView(null);
1396 webView.destroy();
Pankaj Garg1c13cab2015-05-12 11:52:17 -07001397 }
Vivek Sekhar53ef8932015-06-18 16:51:43 -07001398 });
1399 return;
Pankaj Garg1c13cab2015-05-12 11:52:17 -07001400 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001401 setWebView(null);
1402 webView.destroy();
1403 }
1404 }
1405
Matthew Huib7f2e9c2014-04-16 11:12:37 -04001406 private boolean mPostponeDestroy = false;
1407 private boolean mShouldDestroy = false;
1408
1409 public void postponeDestroy() {
1410 mPostponeDestroy = true;
1411 }
1412
1413 public void performPostponedDestroy() {
1414 mPostponeDestroy = false;
1415 if (mShouldDestroy) {
1416 destroy();
1417 }
1418 }
1419
Grace Kloba22ac16e2009-10-07 18:00:23 -07001420 /**
1421 * Remove the tab from the parent
1422 */
1423 void removeFromTree() {
1424 // detach the children
Michael Kolbc831b632011-05-11 09:30:34 -07001425 if (mChildren != null) {
1426 for(Tab t : mChildren) {
1427 t.setParent(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001428 }
1429 }
1430 // remove itself from the parent list
Michael Kolbc831b632011-05-11 09:30:34 -07001431 if (mParent != null) {
1432 mParent.mChildren.remove(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001433 }
Tarun Nainani8eb00912014-07-17 12:28:32 -07001434
1435 mCapture = null;
John Reck1cf4b792011-07-26 10:22:22 -07001436 deleteThumbnail();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001437 }
1438
1439 /**
1440 * Create a new subwindow unless a subwindow already exists.
1441 * @return True if a new subwindow was created. False if one already exists.
1442 */
1443 boolean createSubWindow() {
1444 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001445 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001446 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001447 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001448 mSubView.setWebChromeClient(new SubWindowChromeClient(
1449 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001450 // Set a different DownloadListener for the mSubView, since it will
1451 // just need to dismiss the mSubView, rather than close the Tab
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001452 mSubView.setDownloadListener(new BrowserDownloadListener() {
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001453 public void onDownloadStart(String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001454 String contentDisposition, String mimetype, String referer,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001455 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001456 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001457 contentDisposition, mimetype, referer, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001458 if (mSubView.copyBackForwardList().getSize() == 0) {
1459 // This subwindow was opened for the sole purpose of
1460 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001461 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001462 }
1463 }
1464 });
Michael Kolb14612442011-06-24 13:06:29 -07001465 mSubView.setOnCreateContextMenuListener(mWebViewController.getActivity());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001466 return true;
1467 }
1468 return false;
1469 }
1470
1471 /**
1472 * Dismiss the subWindow for the tab.
1473 */
1474 void dismissSubWindow() {
1475 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001476 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001477 mSubView.destroy();
1478 mSubView = null;
1479 mSubViewContainer = null;
1480 }
1481 }
1482
Grace Kloba22ac16e2009-10-07 18:00:23 -07001483
1484 /**
1485 * Set the parent tab of this tab.
1486 */
Michael Kolbc831b632011-05-11 09:30:34 -07001487 void setParent(Tab parent) {
John Reck52be4782011-08-26 15:37:29 -07001488 if (parent == this) {
1489 throw new IllegalStateException("Cannot set parent to self!");
1490 }
Michael Kolbc831b632011-05-11 09:30:34 -07001491 mParent = parent;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001492 // This tab may have been freed due to low memory. If that is the case,
Michael Kolbc831b632011-05-11 09:30:34 -07001493 // the parent tab id is already saved. If we are changing that id
Grace Kloba22ac16e2009-10-07 18:00:23 -07001494 // (most likely due to removing the parent tab) we must update the
Michael Kolbc831b632011-05-11 09:30:34 -07001495 // parent tab id in the saved Bundle.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001496 if (mSavedState != null) {
1497 if (parent == null) {
1498 mSavedState.remove(PARENTTAB);
1499 } else {
Michael Kolbc831b632011-05-11 09:30:34 -07001500 mSavedState.putLong(PARENTTAB, parent.getId());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001501 }
1502 }
John Reckb0a86db2011-05-24 14:05:58 -07001503
1504 // Sync the WebView useragent with the parent
1505 if (parent != null && mSettings.hasDesktopUseragent(parent.getWebView())
1506 != mSettings.hasDesktopUseragent(getWebView())) {
1507 mSettings.toggleDesktopUseragent(getWebView());
1508 }
John Reck52be4782011-08-26 15:37:29 -07001509
1510 if (parent != null && parent.getId() == getId()) {
1511 throw new IllegalStateException("Parent has same ID as child!");
1512 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001513 }
1514
1515 /**
Michael Kolbc831b632011-05-11 09:30:34 -07001516 * If this Tab was created through another Tab, then this method returns
1517 * that Tab.
1518 * @return the Tab parent or null
1519 */
1520 public Tab getParent() {
1521 return mParent;
1522 }
1523
1524 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001525 * When a Tab is created through the content of another Tab, then we
1526 * associate the Tabs.
1527 * @param child the Tab that was created from this Tab
1528 */
1529 void addChildTab(Tab child) {
Michael Kolbc831b632011-05-11 09:30:34 -07001530 if (mChildren == null) {
1531 mChildren = new Vector<Tab>();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001532 }
Michael Kolbc831b632011-05-11 09:30:34 -07001533 mChildren.add(child);
1534 child.setParent(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001535 }
1536
Michael Kolbc831b632011-05-11 09:30:34 -07001537 Vector<Tab> getChildren() {
1538 return mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001539 }
1540
1541 void resume() {
1542 if (mMainView != null) {
John Reck56c1fcf2011-08-17 10:15:16 -07001543 setupHwAcceleration(mMainView);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001544 mMainView.onResume();
1545 if (mSubView != null) {
1546 mSubView.onResume();
1547 }
1548 }
1549 }
1550
John Reck56c1fcf2011-08-17 10:15:16 -07001551 private void setupHwAcceleration(View web) {
1552 if (web == null) return;
1553 BrowserSettings settings = BrowserSettings.getInstance();
1554 if (settings.isHardwareAccelerated()) {
1555 web.setLayerType(View.LAYER_TYPE_NONE, null);
1556 } else {
1557 web.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
1558 }
1559 }
1560
Grace Kloba22ac16e2009-10-07 18:00:23 -07001561 void pause() {
1562 if (mMainView != null) {
1563 mMainView.onPause();
1564 if (mSubView != null) {
1565 mSubView.onPause();
1566 }
1567 }
1568 }
1569
1570 void putInForeground() {
John Reck8ee633f2011-08-09 16:00:35 -07001571 if (mInForeground) {
1572 return;
1573 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001574 mInForeground = true;
1575 resume();
Michael Kolb14612442011-06-24 13:06:29 -07001576 Activity activity = mWebViewController.getActivity();
1577 mMainView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001578 if (mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -07001579 mSubView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001580 }
Axesh R. Ajmerac6b5c322015-05-01 11:06:10 -07001581
Leon Scroggins1961ed22010-12-07 15:22:21 -05001582 mWebViewController.bookmarkedStatusHasChanged(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001583 }
1584
1585 void putInBackground() {
John Reck8ee633f2011-08-09 16:00:35 -07001586 if (!mInForeground) {
1587 return;
1588 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001589 mInForeground = false;
1590 pause();
1591 mMainView.setOnCreateContextMenuListener(null);
1592 if (mSubView != null) {
1593 mSubView.setOnCreateContextMenuListener(null);
1594 }
1595 }
1596
Michael Kolb8233fac2010-10-26 16:08:53 -07001597 boolean inForeground() {
1598 return mInForeground;
1599 }
1600
Grace Kloba22ac16e2009-10-07 18:00:23 -07001601 /**
1602 * Return the top window of this tab; either the subwindow if it is not
1603 * null or the main window.
1604 * @return The top window of this tab.
1605 */
1606 WebView getTopWindow() {
1607 if (mSubView != null) {
1608 return mSubView;
1609 }
1610 return mMainView;
1611 }
1612
1613 /**
1614 * Return the main window of this tab. Note: if a tab is freed in the
1615 * background, this can return null. It is only guaranteed to be
1616 * non-null for the current tab.
1617 * @return The main WebView of this tab.
1618 */
1619 WebView getWebView() {
1620 return mMainView;
1621 }
1622
Michael Kolba713ec82010-11-29 17:27:06 -08001623 void setViewContainer(View container) {
1624 mContainer = container;
1625 }
1626
Michael Kolb8233fac2010-10-26 16:08:53 -07001627 View getViewContainer() {
1628 return mContainer;
1629 }
1630
Grace Kloba22ac16e2009-10-07 18:00:23 -07001631 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001632 * Return whether private browsing is enabled for the main window of
1633 * this tab.
1634 * @return True if private browsing is enabled.
1635 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001636 boolean isPrivateBrowsingEnabled() {
John Reck502a3532011-08-16 14:21:46 -07001637 return mCurrentState.mIncognito;
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001638 }
1639
1640 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001641 * Return the subwindow of this tab or null if there is no subwindow.
1642 * @return The subwindow of this tab or null.
1643 */
1644 WebView getSubWebView() {
1645 return mSubView;
1646 }
1647
Michael Kolb1514bb72010-11-22 09:11:48 -08001648 void setSubWebView(WebView subView) {
1649 mSubView = subView;
1650 }
1651
Michael Kolb8233fac2010-10-26 16:08:53 -07001652 View getSubViewContainer() {
1653 return mSubViewContainer;
1654 }
1655
Michael Kolb1514bb72010-11-22 09:11:48 -08001656 void setSubViewContainer(View subViewContainer) {
1657 mSubViewContainer = subViewContainer;
1658 }
1659
Grace Kloba22ac16e2009-10-07 18:00:23 -07001660
1661 /**
1662 * @return The application id string
1663 */
1664 String getAppId() {
1665 return mAppId;
1666 }
1667
1668 /**
1669 * Set the application id string
1670 * @param id
1671 */
1672 void setAppId(String id) {
1673 mAppId = id;
1674 }
1675
Michael Kolbe28b3472011-08-04 16:54:31 -07001676 boolean closeOnBack() {
1677 return mCloseOnBack;
1678 }
1679
1680 void setCloseOnBack(boolean close) {
1681 mCloseOnBack = close;
1682 }
1683
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001684 boolean getDerivedFromIntent() {
1685 return mDerivedFromIntent;
1686 }
1687
1688 void setDerivedFromIntent(boolean derived) {
1689 mDerivedFromIntent = derived;
1690 }
1691
Grace Kloba22ac16e2009-10-07 18:00:23 -07001692 String getUrl() {
John Reck324d4402011-01-11 16:56:42 -08001693 return UrlUtils.filteredUrl(mCurrentState.mUrl);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001694 }
1695
Tarun Nainani8eb00912014-07-17 12:28:32 -07001696
1697 protected void onPageFinished() {
1698 mPageFinished = true;
1699 }
1700
1701 public boolean getPageFinishedStatus() {
1702 return mPageFinished;
1703 }
1704
John Reck49a603c2011-03-03 09:33:05 -08001705 String getOriginalUrl() {
John Reckdb22ec42011-06-29 11:31:24 -07001706 if (mCurrentState.mOriginalUrl == null) {
1707 return getUrl();
John Reck49a603c2011-03-03 09:33:05 -08001708 }
John Reckdb22ec42011-06-29 11:31:24 -07001709 return UrlUtils.filteredUrl(mCurrentState.mOriginalUrl);
John Reck49a603c2011-03-03 09:33:05 -08001710 }
1711
Grace Kloba22ac16e2009-10-07 18:00:23 -07001712 /**
John Reck30c714c2010-12-16 17:30:34 -08001713 * Get the title of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001714 */
1715 String getTitle() {
John Reck30c714c2010-12-16 17:30:34 -08001716 return mCurrentState.mTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001717 }
1718
1719 /**
John Reck30c714c2010-12-16 17:30:34 -08001720 * Get the favicon of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001721 */
1722 Bitmap getFavicon() {
John Reck1cf4b792011-07-26 10:22:22 -07001723 if (mCurrentState.mFavicon != null) {
1724 return mCurrentState.mFavicon;
1725 }
1726 return getDefaultFavicon(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001727 }
1728
John Recke969cc52010-12-21 17:24:43 -08001729 public boolean isBookmarkedSite() {
1730 return mCurrentState.mIsBookmarkedSite;
1731 }
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001732
Grace Kloba22ac16e2009-10-07 18:00:23 -07001733 /**
Steve Block08a6f0c2011-10-06 12:12:53 +01001734 * Sets the security state, clears the SSL certificate error and informs
1735 * the controller.
1736 */
Steve Block2466eff2011-10-03 15:33:09 +01001737 private void setSecurityState(SecurityState securityState) {
1738 mCurrentState.mSecurityState = securityState;
Steve Block08a6f0c2011-10-06 12:12:53 +01001739 mCurrentState.mSslCertificateError = null;
Steve Block2466eff2011-10-03 15:33:09 +01001740 mWebViewController.onUpdatedSecurityState(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001741 }
1742
1743 /**
Steve Block2466eff2011-10-03 15:33:09 +01001744 * @return The tab's security state.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001745 */
Steve Block2466eff2011-10-03 15:33:09 +01001746 SecurityState getSecurityState() {
1747 return mCurrentState.mSecurityState;
John Reck30c714c2010-12-16 17:30:34 -08001748 }
1749
Steve Block08a6f0c2011-10-06 12:12:53 +01001750 /**
1751 * Gets the SSL certificate error, if any, for the page's main resource.
1752 * This is only non-null when the security state is
1753 * SECURITY_STATE_BAD_CERTIFICATE.
1754 */
1755 SslError getSslCertificateError() {
1756 return mCurrentState.mSslCertificateError;
1757 }
1758
John Reck30c714c2010-12-16 17:30:34 -08001759 int getLoadProgress() {
1760 if (mInPageLoad) {
1761 return mPageLoadProgress;
1762 }
1763 return 100;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001764 }
1765
1766 /**
1767 * @return TRUE if onPageStarted is called while onPageFinished is not
1768 * called yet.
1769 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001770 boolean inPageLoad() {
1771 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001772 }
1773
Grace Kloba22ac16e2009-10-07 18:00:23 -07001774 /**
John Reck1cf4b792011-07-26 10:22:22 -07001775 * @return The Bundle with the tab's state if it can be saved, otherwise null
Grace Kloba22ac16e2009-10-07 18:00:23 -07001776 */
John Reck1cf4b792011-07-26 10:22:22 -07001777 public Bundle saveState() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001778 // If the WebView is null it means we ran low on memory and we already
1779 // stored the saved state in mSavedState.
1780 if (mMainView == null) {
John Reck1cf4b792011-07-26 10:22:22 -07001781 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001782 }
John Reck6c2e2f32011-08-22 13:41:23 -07001783
1784 if (TextUtils.isEmpty(mCurrentState.mUrl)) {
John Reck1cf4b792011-07-26 10:22:22 -07001785 return null;
John Reck24f18262011-06-17 14:47:20 -07001786 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001787
1788 mSavedState = new Bundle();
John Reck6c2e2f32011-08-22 13:41:23 -07001789 WebBackForwardList savedList = mMainView.saveState(mSavedState);
1790 if (savedList == null || savedList.getSize() == 0) {
1791 Log.w(LOGTAG, "Failed to save back/forward list for "
1792 + mCurrentState.mUrl);
1793 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001794
Michael Kolbc831b632011-05-11 09:30:34 -07001795 mSavedState.putLong(ID, mId);
John Reck30c714c2010-12-16 17:30:34 -08001796 mSavedState.putString(CURRURL, mCurrentState.mUrl);
1797 mSavedState.putString(CURRTITLE, mCurrentState.mTitle);
John Reck1cf4b792011-07-26 10:22:22 -07001798 mSavedState.putBoolean(INCOGNITO, mMainView.isPrivateBrowsingEnabled());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001799 if (mAppId != null) {
1800 mSavedState.putString(APPID, mAppId);
1801 }
Michael Kolbe28b3472011-08-04 16:54:31 -07001802 mSavedState.putBoolean(CLOSEFLAG, mCloseOnBack);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001803 // Remember the parent tab so the relationship can be restored.
Michael Kolbc831b632011-05-11 09:30:34 -07001804 if (mParent != null) {
1805 mSavedState.putLong(PARENTTAB, mParent.mId);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001806 }
John Reckb0a86db2011-05-24 14:05:58 -07001807 mSavedState.putBoolean(USERAGENT,
1808 mSettings.hasDesktopUseragent(getWebView()));
John Reck1cf4b792011-07-26 10:22:22 -07001809 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001810 }
1811
1812 /*
1813 * Restore the state of the tab.
1814 */
John Reck1cf4b792011-07-26 10:22:22 -07001815 private void restoreState(Bundle b) {
1816 mSavedState = b;
1817 if (mSavedState == null) {
1818 return;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001819 }
1820 // Restore the internal state even if the WebView fails to restore.
1821 // This will maintain the app id, original url and close-on-exit values.
Michael Kolbc831b632011-05-11 09:30:34 -07001822 mId = b.getLong(ID);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001823 mAppId = b.getString(APPID);
Michael Kolbe28b3472011-08-04 16:54:31 -07001824 mCloseOnBack = b.getBoolean(CLOSEFLAG);
John Reck8b9bb8b2012-03-08 13:19:40 -08001825 restoreUserAgent();
John Reck1cf4b792011-07-26 10:22:22 -07001826 String url = b.getString(CURRURL);
1827 String title = b.getString(CURRTITLE);
1828 boolean incognito = b.getBoolean(INCOGNITO);
1829 mCurrentState = new PageState(mContext, incognito, url, null);
1830 mCurrentState.mTitle = title;
1831 synchronized (Tab.this) {
1832 if (mCapture != null) {
John Reck4eadc342011-10-31 14:04:10 -07001833 DataController.getInstance(mContext).loadThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07001834 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001835 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001836 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04001837
John Reck8b9bb8b2012-03-08 13:19:40 -08001838 private void restoreUserAgent() {
1839 if (mMainView == null || mSavedState == null) {
1840 return;
1841 }
1842 if (mSavedState.getBoolean(USERAGENT)
1843 != mSettings.hasDesktopUseragent(mMainView)) {
1844 mSettings.toggleDesktopUseragent(mMainView);
1845 }
1846 }
1847
Leon Scroggins1961ed22010-12-07 15:22:21 -05001848 public void updateBookmarkedStatus() {
John Recke969cc52010-12-21 17:24:43 -08001849 mDataController.queryBookmarkStatus(getUrl(), mIsBookmarkCallback);
Leon Scroggins1961ed22010-12-07 15:22:21 -05001850 }
1851
John Recke969cc52010-12-21 17:24:43 -08001852 private DataController.OnQueryUrlIsBookmark mIsBookmarkCallback
1853 = new DataController.OnQueryUrlIsBookmark() {
1854 @Override
1855 public void onQueryUrlIsBookmark(String url, boolean isBookmark) {
1856 if (mCurrentState.mUrl.equals(url)) {
1857 mCurrentState.mIsBookmarkedSite = isBookmark;
1858 mWebViewController.bookmarkedStatusHasChanged(Tab.this);
1859 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001860 }
John Recke969cc52010-12-21 17:24:43 -08001861 };
Michael Kolb1acef692011-03-08 14:12:06 -08001862
Michael Kolbeb95db42011-03-03 10:38:40 -08001863 public Bitmap getScreenshot() {
John Reck1cf4b792011-07-26 10:22:22 -07001864 synchronized (Tab.this) {
1865 return mCapture;
1866 }
Michael Kolbeb95db42011-03-03 10:38:40 -08001867 }
1868
John Reck541f55a2011-06-07 16:34:43 -07001869 public boolean isSnapshot() {
John Reck541f55a2011-06-07 16:34:43 -07001870 return false;
1871 }
1872
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001873 private static class SaveCallback implements ValueCallback<String> {
1874 boolean onReceiveValueCalled = false;
1875 private String mPath;
John Reck68234a92012-04-19 15:27:12 -07001876
1877 @Override
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001878 public void onReceiveValue(String path) {
1879 this.onReceiveValueCalled = true;
1880 this.mPath = path;
John Reck68234a92012-04-19 15:27:12 -07001881 synchronized (this) {
1882 notifyAll();
John Reck8cc92352011-07-06 17:41:52 -07001883 }
John Reck541f55a2011-06-07 16:34:43 -07001884 }
John Reck68234a92012-04-19 15:27:12 -07001885
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001886 public String getPath() {
1887 return mPath;
1888 }
John Reck68234a92012-04-19 15:27:12 -07001889 }
1890
1891 /**
1892 * Must be called on the UI thread
1893 */
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001894 public ContentValues createSnapshotValues(Bitmap bm) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001895 WebView web = getWebView();
John Reck68234a92012-04-19 15:27:12 -07001896 if (web == null) return null;
John Reckd8c74522011-06-14 08:45:00 -07001897 ContentValues values = new ContentValues();
1898 values.put(Snapshots.TITLE, mCurrentState.mTitle);
1899 values.put(Snapshots.URL, mCurrentState.mUrl);
John Reck68234a92012-04-19 15:27:12 -07001900 values.put(Snapshots.BACKGROUND, web.getPageBackgroundColor());
John Reck8cc92352011-07-06 17:41:52 -07001901 values.put(Snapshots.DATE_CREATED, System.currentTimeMillis());
1902 values.put(Snapshots.FAVICON, compressBitmap(getFavicon()));
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001903 values.put(Snapshots.THUMBNAIL, compressBitmap(bm));
John Reckd8c74522011-06-14 08:45:00 -07001904 return values;
John Reck541f55a2011-06-07 16:34:43 -07001905 }
1906
John Reck68234a92012-04-19 15:27:12 -07001907 /**
1908 * Probably want to call this on a background thread
1909 */
1910 public boolean saveViewState(ContentValues values) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001911 WebView web = getWebView();
John Reck68234a92012-04-19 15:27:12 -07001912 if (web == null) return false;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001913 String filename = UUID.randomUUID().toString();
John Reck68234a92012-04-19 15:27:12 -07001914 SaveCallback callback = new SaveCallback();
John Reck68234a92012-04-19 15:27:12 -07001915 try {
John Reck68234a92012-04-19 15:27:12 -07001916 synchronized (callback) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001917 web.saveViewState(filename, callback);
1918 callback.wait();
John Reck68234a92012-04-19 15:27:12 -07001919 }
John Reck68234a92012-04-19 15:27:12 -07001920 } catch (Exception e) {
1921 Log.w(LOGTAG, "Failed to save view state", e);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001922 String path = callback.getPath();
1923 if (path != null) {
1924 File file = mContext.getFileStreamPath(path);
1925 if (file.exists() && !file.delete()) {
1926 file.deleteOnExit();
1927 }
John Reck68234a92012-04-19 15:27:12 -07001928 }
1929 return false;
1930 }
Tarun Nainani8eb00912014-07-17 12:28:32 -07001931
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001932 String path = callback.getPath();
Tarun Nainani8eb00912014-07-17 12:28:32 -07001933 // could be that saving of file failed
1934 if (path == null) {
1935 return false;
1936 }
1937
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001938 File savedFile = new File(path);
1939 if (!savedFile.exists()) {
1940 return false;
John Reck68234a92012-04-19 15:27:12 -07001941 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001942 values.put(Snapshots.VIEWSTATE_PATH, path.substring(path.lastIndexOf('/') + 1));
1943 values.put(Snapshots.VIEWSTATE_SIZE, savedFile.length());
John Reck68234a92012-04-19 15:27:12 -07001944 return true;
1945 }
1946
John Reck8cc92352011-07-06 17:41:52 -07001947 public byte[] compressBitmap(Bitmap bitmap) {
1948 if (bitmap == null) {
1949 return null;
1950 }
1951 ByteArrayOutputStream stream = new ByteArrayOutputStream();
1952 bitmap.compress(CompressFormat.PNG, 100, stream);
1953 return stream.toByteArray();
1954 }
1955
John Reck26b18322011-06-21 13:08:58 -07001956 public void loadUrl(String url, Map<String, String> headers) {
1957 if (mMainView != null) {
Michael Kolba53c9892011-10-05 13:31:40 -07001958 mPageLoadProgress = INITIAL_PROGRESS;
Michael Kolb14612442011-06-24 13:06:29 -07001959 mCurrentState = new PageState(mContext, false, url, null);
John Reck26b18322011-06-21 13:08:58 -07001960 mMainView.loadUrl(url, headers);
1961 }
1962 }
1963
John Reck38b39652012-06-05 09:22:59 -07001964 public void disableUrlOverridingForLoad() {
1965 mDisableOverrideUrlLoading = true;
1966 }
1967
Michael Kolb9ef259a2011-07-12 15:33:08 -07001968 protected void capture() {
Vivek Sekhar6bdf6452015-05-12 17:38:45 -07001969 boolean returnEmptyCapture = false;
1970 if (mMainView == null || mCapture == null || !mMainView.isReady())
1971 returnEmptyCapture = true;
John Reck4eadc342011-10-31 14:04:10 -07001972 if (mMainView.getContentWidth() <= 0 || mMainView.getContentHeight() <= 0) {
Vivek Sekhar6bdf6452015-05-12 17:38:45 -07001973 returnEmptyCapture = true;
John Reck4eadc342011-10-31 14:04:10 -07001974 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07001975
Vivek Sekhar6bdf6452015-05-12 17:38:45 -07001976 if (returnEmptyCapture || !mFirstVisualPixelPainted || mMainView.isShowingCrashView()) {
Pankaj Garg79878492015-04-01 14:48:21 -07001977 mCapture = Bitmap.createBitmap(
1978 mCaptureWidth,
1979 mCaptureHeight,
1980 Bitmap.Config.RGB_565);
1981 mCapture.eraseColor(Color.WHITE);
1982
1983 mHandler.removeMessages(MSG_CAPTURE);
1984
1985 TabControl tc = mWebViewController.getTabControl();
1986 if (tc != null) {
1987 OnThumbnailUpdatedListener updateListener
1988 = tc.getOnThumbnailUpdatedListener();
1989 if (updateListener != null) {
1990 updateListener.onThumbnailUpdated(this);
1991 }
1992 }
1993 return;
1994 }
1995
Tarun Nainaniea28dde2014-08-27 17:25:09 -07001996 mMainView
1997 .getContentBitmapAsync(
1998 (float) mCaptureWidth / mMainView.getWidth(),
1999 new Rect(),
2000 new ValueCallback<Bitmap>() {
2001 @Override
2002 public void onReceiveValue(Bitmap bitmap) {
2003 onCaptureCallback(bitmap);
2004 }});
2005 }
2006
2007 private void onCaptureCallback(Bitmap bitmap) {
2008 if (mCapture == null || bitmap == null)
2009 return;
2010
Michael Kolb9ef259a2011-07-12 15:33:08 -07002011 Canvas c = new Canvas(mCapture);
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002012 mCapture.eraseColor(Color.WHITE);
2013 c.drawBitmap(bitmap, 0, 0, null);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002014
Michael Kolba3194d02011-09-07 11:23:51 -07002015 // manually anti-alias the edges for the tilt
2016 c.drawRect(0, 0, 1, mCapture.getHeight(), sAlphaPaint);
2017 c.drawRect(mCapture.getWidth() - 1, 0, mCapture.getWidth(),
2018 mCapture.getHeight(), sAlphaPaint);
2019 c.drawRect(0, 0, mCapture.getWidth(), 1, sAlphaPaint);
2020 c.drawRect(0, mCapture.getHeight() - 1, mCapture.getWidth(),
2021 mCapture.getHeight(), sAlphaPaint);
Dianne Hackborn43cfe8a2011-08-02 16:59:35 -07002022 c.setBitmap(null);
John Reck8ee633f2011-08-09 16:00:35 -07002023 mHandler.removeMessages(MSG_CAPTURE);
John Reck1cf4b792011-07-26 10:22:22 -07002024 persistThumbnail();
John Reck8ee633f2011-08-09 16:00:35 -07002025 TabControl tc = mWebViewController.getTabControl();
2026 if (tc != null) {
2027 OnThumbnailUpdatedListener updateListener
2028 = tc.getOnThumbnailUpdatedListener();
2029 if (updateListener != null) {
2030 updateListener.onThumbnailUpdated(this);
2031 }
2032 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07002033 }
2034
2035 @Override
2036 public void onNewPicture(WebView view, Picture picture) {
John Reck1cf4b792011-07-26 10:22:22 -07002037 postCapture();
2038 }
2039
2040 private void postCapture() {
Michael Kolb9ef259a2011-07-12 15:33:08 -07002041 if (!mHandler.hasMessages(MSG_CAPTURE)) {
2042 mHandler.sendEmptyMessageDelayed(MSG_CAPTURE, CAPTURE_DELAY);
2043 }
2044 }
2045
John Reckef654f12011-07-12 16:42:08 -07002046 public boolean canGoBack() {
2047 return mMainView != null ? mMainView.canGoBack() : false;
2048 }
2049
2050 public boolean canGoForward() {
2051 return mMainView != null ? mMainView.canGoForward() : false;
2052 }
2053
2054 public void goBack() {
2055 if (mMainView != null) {
2056 mMainView.goBack();
2057 }
2058 }
2059
2060 public void goForward() {
2061 if (mMainView != null) {
2062 mMainView.goForward();
2063 }
2064 }
2065
John Reck1cf4b792011-07-26 10:22:22 -07002066 protected void persistThumbnail() {
John Reck4eadc342011-10-31 14:04:10 -07002067 DataController.getInstance(mContext).saveThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07002068 }
2069
2070 protected void deleteThumbnail() {
John Reck4eadc342011-10-31 14:04:10 -07002071 DataController.getInstance(mContext).deleteThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07002072 }
2073
John Reck4eadc342011-10-31 14:04:10 -07002074 void updateCaptureFromBlob(byte[] blob) {
John Reck1cf4b792011-07-26 10:22:22 -07002075 synchronized (Tab.this) {
2076 if (mCapture == null) {
2077 return;
2078 }
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002079 ByteBuffer buffer = ByteBuffer.wrap(blob);
Michael Kolbd837a112011-08-09 14:00:18 -07002080 try {
2081 mCapture.copyPixelsFromBuffer(buffer);
2082 } catch (RuntimeException rex) {
2083 Log.e(LOGTAG, "Load capture has mismatched sizes; buffer: "
2084 + buffer.capacity() + " blob: " + blob.length
2085 + "capture: " + mCapture.getByteCount());
2086 throw rex;
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002087 }
John Reck1cf4b792011-07-26 10:22:22 -07002088 }
2089 }
2090
John Reck52be4782011-08-26 15:37:29 -07002091 @Override
2092 public String toString() {
2093 StringBuilder builder = new StringBuilder(100);
2094 builder.append(mId);
2095 builder.append(") has parent: ");
2096 if (getParent() != null) {
2097 builder.append("true[");
2098 builder.append(getParent().getId());
2099 builder.append("]");
2100 } else {
2101 builder.append("false");
2102 }
2103 builder.append(", incog: ");
2104 builder.append(isPrivateBrowsingEnabled());
2105 if (!isPrivateBrowsingEnabled()) {
2106 builder.append(", title: ");
2107 builder.append(getTitle());
2108 builder.append(", url: ");
2109 builder.append(getUrl());
2110 }
2111 return builder.toString();
2112 }
2113
Steve Block4895b012011-10-03 16:26:46 +01002114 private void handleProceededAfterSslError(SslError error) {
2115 if (error.getUrl().equals(mCurrentState.mUrl)) {
2116 // The security state should currently be SECURITY_STATE_SECURE.
2117 setSecurityState(SecurityState.SECURITY_STATE_BAD_CERTIFICATE);
Steve Block08a6f0c2011-10-06 12:12:53 +01002118 mCurrentState.mSslCertificateError = error;
Steve Block4895b012011-10-03 16:26:46 +01002119 } else if (getSecurityState() == SecurityState.SECURITY_STATE_SECURE) {
Steve Block08a6f0c2011-10-06 12:12:53 +01002120 // The page's main resource is secure and this error is for a
2121 // sub-resource.
Steve Block4895b012011-10-03 16:26:46 +01002122 setSecurityState(SecurityState.SECURITY_STATE_MIXED);
2123 }
2124 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002125}