blob: 01876a476d57d2e7c7c76294a6205fd61a562463 [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
Pankaj Garg18aa0a12015-06-22 11:06:12 -070067import org.codeaurora.swe.BrowserCommandLine;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080068import org.codeaurora.swe.BrowserDownloadListener;
69import org.codeaurora.swe.ClientCertRequestHandler;
70import org.codeaurora.swe.HttpAuthHandler;
71import org.codeaurora.swe.SslErrorHandler;
72import org.codeaurora.swe.WebBackForwardList;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080073import org.codeaurora.swe.WebChromeClient;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080074import org.codeaurora.swe.WebView;
75import org.codeaurora.swe.WebView.PictureListener;
Pankaj Garg1c7380d2014-08-27 14:17:12 -070076import org.codeaurora.swe.WebView.CreateWindowParams;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080077import org.codeaurora.swe.WebViewClient;
Pankaj Garg1c13cab2015-05-12 11:52:17 -070078import org.codeaurora.swe.util.Observable;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080079
John Reck541f55a2011-06-07 16:34:43 -070080import java.io.ByteArrayOutputStream;
John Reck2b71d6d2012-04-18 17:42:06 -070081import java.io.File;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080082import java.io.InputStream;
John Reck1cf4b792011-07-26 10:22:22 -070083import java.nio.ByteBuffer;
Michael Kolbfe251992010-07-08 15:41:55 -070084import java.util.Map;
John Reck2b71d6d2012-04-18 17:42:06 -070085import java.util.UUID;
Michael Kolbfe251992010-07-08 15:41:55 -070086import java.util.Vector;
Vivek Sekhar53ef8932015-06-18 16:51:43 -070087import java.util.List;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080088import java.sql.Timestamp;
89import java.util.Date;
Michael Kolbfe251992010-07-08 15:41:55 -070090
Grace Kloba22ac16e2009-10-07 18:00:23 -070091/**
92 * Class for maintaining Tabs with a main WebView and a subwindow.
93 */
Michael Kolb9ef259a2011-07-12 15:33:08 -070094class Tab implements PictureListener {
Michael Kolb8233fac2010-10-26 16:08:53 -070095
Grace Kloba22ac16e2009-10-07 18:00:23 -070096 // Log Tag
97 private static final String LOGTAG = "Tab";
Bijan Amirzada41242f22014-03-21 12:12:18 -070098 private static final boolean LOGD_ENABLED = com.android.browser.Browser.LOGD_ENABLED;
Ben Murdochc42addf2010-01-28 15:19:59 +000099 // Special case the logtag for messages for the Console to make it easier to
100 // filter them and match the logtag used for these messages in older versions
101 // of the browser.
102 private static final String CONSOLE_LOGTAG = "browser";
103
Michael Kolb9ef259a2011-07-12 15:33:08 -0700104 private static final int MSG_CAPTURE = 42;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800105 private static final int CAPTURE_DELAY = 1000;
Michael Kolba53c9892011-10-05 13:31:40 -0700106 private static final int INITIAL_PROGRESS = 5;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700107
John Reck1cf4b792011-07-26 10:22:22 -0700108 private static Bitmap sDefaultFavicon;
109
Michael Kolba3194d02011-09-07 11:23:51 -0700110 private static Paint sAlphaPaint = new Paint();
111 static {
112 sAlphaPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
113 sAlphaPaint.setColor(Color.TRANSPARENT);
114 }
115
Steve Block2466eff2011-10-03 15:33:09 +0100116 public enum SecurityState {
Steve Block4895b012011-10-03 16:26:46 +0100117 // The page's main resource does not use SSL. Note that we use this
118 // state irrespective of the SSL authentication state of sub-resources.
Steve Block2466eff2011-10-03 15:33:09 +0100119 SECURITY_STATE_NOT_SECURE,
Steve Block4895b012011-10-03 16:26:46 +0100120 // The page's main resource uses SSL and the certificate is good. The
121 // same is true of all sub-resources.
Steve Block2466eff2011-10-03 15:33:09 +0100122 SECURITY_STATE_SECURE,
Steve Block4895b012011-10-03 16:26:46 +0100123 // The page's main resource uses SSL and the certificate is good, but
124 // some sub-resources either do not use SSL or have problems with their
125 // certificates.
Steve Block2466eff2011-10-03 15:33:09 +0100126 SECURITY_STATE_MIXED,
Steve Block4895b012011-10-03 16:26:46 +0100127 // The page's main resource uses SSL but there is a problem with its
128 // certificate.
129 SECURITY_STATE_BAD_CERTIFICATE,
John Reck30c714c2010-12-16 17:30:34 -0800130 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700131
Michael Kolb14612442011-06-24 13:06:29 -0700132 Context mContext;
John Reckd8c74522011-06-14 08:45:00 -0700133 protected WebViewController mWebViewController;
Michael Kolb8233fac2010-10-26 16:08:53 -0700134
Michael Kolbc831b632011-05-11 09:30:34 -0700135 // The tab ID
John Reckd8c74522011-06-14 08:45:00 -0700136 private long mId = -1;
Michael Kolbc831b632011-05-11 09:30:34 -0700137
Grace Kloba22ac16e2009-10-07 18:00:23 -0700138 // Main WebView wrapper
Michael Kolba713ec82010-11-29 17:27:06 -0800139 private View mContainer;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700140 // Main WebView
141 private WebView mMainView;
142 // Subwindow container
143 private View mSubViewContainer;
144 // Subwindow WebView
145 private WebView mSubView;
146 // Saved bundle for when we are running low on memory. It contains the
147 // information needed to restore the WebView if the user goes back to the
148 // tab.
149 private Bundle mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700150 // Parent Tab. This is the Tab that created this Tab, or null if the Tab was
151 // created by the UI
Michael Kolbc831b632011-05-11 09:30:34 -0700152 private Tab mParent;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700153 // Tab that constructed by this Tab. This is used when this Tab is
154 // destroyed, it clears all mParentTab values in the children.
Michael Kolbc831b632011-05-11 09:30:34 -0700155 private Vector<Tab> mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700156 // If true, the tab is in the foreground of the current activity.
157 private boolean mInForeground;
Michael Kolb8233fac2010-10-26 16:08:53 -0700158 // If true, the tab is in page loading state (after onPageStarted,
159 // before onPageFinsihed)
160 private boolean mInPageLoad;
Tarun Nainani8eb00912014-07-17 12:28:32 -0700161 private boolean mPageFinished;
John Reck38b39652012-06-05 09:22:59 -0700162 private boolean mDisableOverrideUrlLoading;
Pankaj Garg79878492015-04-01 14:48:21 -0700163 private boolean mFirstVisualPixelPainted = false;
John Reck30c714c2010-12-16 17:30:34 -0800164 // The last reported progress of the current page
165 private int mPageLoadProgress;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000166 // The time the load started, used to find load page time
167 private long mLoadStartTime;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700168 // Application identifier used to find tabs that another application wants
169 // to reuse.
170 private String mAppId;
Michael Kolbe28b3472011-08-04 16:54:31 -0700171 // flag to indicate if tab should be closed on back
172 private boolean mCloseOnBack;
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700173 // flag to indicate if the tab was opened from an intent
174 private boolean mDerivedFromIntent = false;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -0500175 // The listener that gets invoked when a download is started from the
176 // mMainView
Selim Gurun0b3d66f2012-08-29 13:08:13 -0700177 private final BrowserDownloadListener mDownloadListener;
John Recke969cc52010-12-21 17:24:43 -0800178 private DataController mDataController;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700179
180 // AsyncTask for downloading touch icons
181 DownloadTouchIcon mTouchIconLoader;
182
John Reck35e9dd62011-04-25 09:01:54 -0700183 private BrowserSettings mSettings;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700184 private int mCaptureWidth;
185 private int mCaptureHeight;
186 private Bitmap mCapture;
187 private Handler mHandler;
Michael Kolb72864272012-05-03 15:42:15 -0700188 private boolean mUpdateThumbnail;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800189 private Timestamp timestamp;
Vivek Sekharf96064b2014-07-28 16:32:34 -0700190 private boolean mFullScreen = false;
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800191 private boolean mReceivedError;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700192
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -0700193 // determine if webview is destroyed to MemoryMonitor
194 private boolean mWebViewDestroyedByMemoryMonitor;
195
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700196 private Observable mFirstPixelObservable;
197 private Observable mTabHistoryUpdateObservable;
198
199 Observable getFirstPixelObservable() {
200 return mFirstPixelObservable;
201 }
202
203 Observable getTabHistoryUpdateObservable() {
204 return mTabHistoryUpdateObservable;
205 }
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -0700206
Mathew Inwood1dd8e822011-08-03 14:34:29 +0100207
John Reck1cf4b792011-07-26 10:22:22 -0700208 private static synchronized Bitmap getDefaultFavicon(Context context) {
209 if (sDefaultFavicon == null) {
210 sDefaultFavicon = BitmapFactory.decodeResource(
Enrico Rosd6efa972014-12-02 19:49:59 -0800211 context.getResources(), R.drawable.ic_deco_favicon_normal);
John Reck1cf4b792011-07-26 10:22:22 -0700212 }
213 return sDefaultFavicon;
214 }
Michael Kolbeb95db42011-03-03 10:38:40 -0800215
John Reck30c714c2010-12-16 17:30:34 -0800216 // All the state needed for a page
John Reckd8c74522011-06-14 08:45:00 -0700217 protected static class PageState {
John Reck30c714c2010-12-16 17:30:34 -0800218 String mUrl;
John Reckdb22ec42011-06-29 11:31:24 -0700219 String mOriginalUrl;
John Reck30c714c2010-12-16 17:30:34 -0800220 String mTitle;
Steve Block2466eff2011-10-03 15:33:09 +0100221 SecurityState mSecurityState;
Steve Block08a6f0c2011-10-06 12:12:53 +0100222 // This is non-null only when mSecurityState is SECURITY_STATE_BAD_CERTIFICATE.
223 SslError mSslCertificateError;
John Reck30c714c2010-12-16 17:30:34 -0800224 Bitmap mFavicon;
Steve Block08a6f0c2011-10-06 12:12:53 +0100225 boolean mIsBookmarkedSite;
226 boolean mIncognito;
John Reck30c714c2010-12-16 17:30:34 -0800227
228 PageState(Context c, boolean incognito) {
John Reck502a3532011-08-16 14:21:46 -0700229 mIncognito = incognito;
230 if (mIncognito) {
Vivek Sekhared791da2015-02-22 12:39:05 -0800231 mOriginalUrl = mUrl = "chrome://incognito";
John Reck30c714c2010-12-16 17:30:34 -0800232 mTitle = c.getString(R.string.new_incognito_tab);
John Reck30c714c2010-12-16 17:30:34 -0800233 } else {
John Reckdb22ec42011-06-29 11:31:24 -0700234 mOriginalUrl = mUrl = "";
John Reck30c714c2010-12-16 17:30:34 -0800235 mTitle = c.getString(R.string.new_tab);
John Reck30c714c2010-12-16 17:30:34 -0800236 }
Steve Block2466eff2011-10-03 15:33:09 +0100237 mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
John Reck30c714c2010-12-16 17:30:34 -0800238 }
239
240 PageState(Context c, boolean incognito, String url, Bitmap favicon) {
John Reck502a3532011-08-16 14:21:46 -0700241 mIncognito = incognito;
John Reckdb22ec42011-06-29 11:31:24 -0700242 mOriginalUrl = mUrl = url;
John Reck30c714c2010-12-16 17:30:34 -0800243 if (URLUtil.isHttpsUrl(url)) {
Steve Block2466eff2011-10-03 15:33:09 +0100244 mSecurityState = SecurityState.SECURITY_STATE_SECURE;
John Reck30c714c2010-12-16 17:30:34 -0800245 } else {
Steve Block2466eff2011-10-03 15:33:09 +0100246 mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
John Reck30c714c2010-12-16 17:30:34 -0800247 }
John Reck1cf4b792011-07-26 10:22:22 -0700248 mFavicon = favicon;
John Reck30c714c2010-12-16 17:30:34 -0800249 }
John Reck1cf4b792011-07-26 10:22:22 -0700250
Grace Kloba22ac16e2009-10-07 18:00:23 -0700251 }
252
John Reck30c714c2010-12-16 17:30:34 -0800253 // The current/loading page's state
John Reckd8c74522011-06-14 08:45:00 -0700254 protected PageState mCurrentState;
John Reck30c714c2010-12-16 17:30:34 -0800255
Grace Kloba22ac16e2009-10-07 18:00:23 -0700256 // Used for saving and restoring each Tab
Michael Kolbc831b632011-05-11 09:30:34 -0700257 static final String ID = "ID";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700258 static final String CURRURL = "currentUrl";
259 static final String CURRTITLE = "currentTitle";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700260 static final String PARENTTAB = "parentTab";
261 static final String APPID = "appid";
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700262 static final String INCOGNITO = "privateBrowsingEnabled";
John Reckb0a86db2011-05-24 14:05:58 -0700263 static final String USERAGENT = "useragent";
Michael Kolbe28b3472011-08-04 16:54:31 -0700264 static final String CLOSEFLAG = "closeOnBack";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700265
Pankaj Garg18186a92015-03-31 14:59:33 -0700266 public void setNetworkAvailable(boolean networkUp) {
267 if (networkUp && mReceivedError && (mMainView != null)) {
268 mMainView.reload();
269 }
270 }
271
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700272 public boolean isFirstVisualPixelPainted() {
273 return mFirstVisualPixelPainted;
274 }
275
276 public int getCaptureIndex(int navIndex) {
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700277 int orientation = mWebViewController.getActivity().
278 getResources().getConfiguration().orientation;
279
280 int orientationBit = (orientation == Configuration.ORIENTATION_LANDSCAPE) ? 0 : 1;
281
Vivek Sekhard0f60402015-06-05 14:07:11 -0700282 int index = orientationBit << 31 | (((int)mId & 0x7f) << 24) | (navIndex & 0xffffff);
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700283 return index;
284 }
285
286 public int getTabIdxFromCaptureIdx(int index) {
287 return (index & 0x7f000000) >> 24;
288 }
289
290 public int getOrientationFromCaptureIdx(int index) {
291 return ((index & 0x80000000) == 0) ? Configuration.ORIENTATION_LANDSCAPE :
292 Configuration.ORIENTATION_PORTRAIT;
293
294 }
295
296 public int getNavIdxFromCaptureIdx(int index) {
297 return (index & 0xffffff);
298 }
299
Grace Kloba22ac16e2009-10-07 18:00:23 -0700300 // -------------------------------------------------------------------------
301 // WebViewClient implementation for the main WebView
302 // -------------------------------------------------------------------------
303
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800304 private final WebViewClient mWebViewClient = new WebViewClient() {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500305 private Message mDontResend;
306 private Message mResend;
Michael Kolb47bd1e42011-09-01 15:25:00 -0700307
308 private boolean providersDiffer(String url, String otherUrl) {
309 Uri uri1 = Uri.parse(url);
310 Uri uri2 = Uri.parse(otherUrl);
311 return !uri1.getEncodedAuthority().equals(uri2.getEncodedAuthority());
312 }
313
Grace Kloba22ac16e2009-10-07 18:00:23 -0700314 @Override
315 public void onPageStarted(WebView view, String url, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700316 mInPageLoad = true;
Tarun Nainani8eb00912014-07-17 12:28:32 -0700317 mPageFinished = false;
Pankaj Garg79878492015-04-01 14:48:21 -0700318 mFirstVisualPixelPainted = false;
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700319 mFirstPixelObservable.set(false);
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800320 mReceivedError = false;
Michael Kolb72864272012-05-03 15:42:15 -0700321 mUpdateThumbnail = true;
Michael Kolba53c9892011-10-05 13:31:40 -0700322 mPageLoadProgress = INITIAL_PROGRESS;
Michael Kolb14612442011-06-24 13:06:29 -0700323 mCurrentState = new PageState(mContext,
John Reck30c714c2010-12-16 17:30:34 -0800324 view.isPrivateBrowsingEnabled(), url, favicon);
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000325 mLoadStartTime = SystemClock.uptimeMillis();
Pankaj Garg62bc7912015-04-14 16:08:59 -0700326 // Need re-enable FullScreenMode on Page navigation if needed
327 if (BrowserSettings.getInstance().useFullscreen()){
328 Controller controller = (Controller) mWebViewController;
329 BaseUi ui = (BaseUi) controller.getUi();
330 ui.forceDisableFullscreenMode(false);
331 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700332 // If we start a touch icon load and then load a new page, we don't
333 // want to cancel the current touch icon loader. But, we do want to
334 // create a new one when the touch icon url is known.
335 if (mTouchIconLoader != null) {
336 mTouchIconLoader.mTab = null;
337 mTouchIconLoader = null;
338 }
339
Grace Kloba22ac16e2009-10-07 18:00:23 -0700340 // finally update the UI in the activity if it is in the foreground
John Reck324d4402011-01-11 16:56:42 -0800341 mWebViewController.onPageStarted(Tab.this, view, favicon);
Leon Scroggins4cd97792010-12-03 15:31:56 -0500342
John Recke969cc52010-12-21 17:24:43 -0800343 updateBookmarkedStatus();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700344 }
345
346 @Override
347 public void onPageFinished(WebView view, String url) {
John Reck38b39652012-06-05 09:22:59 -0700348 mDisableOverrideUrlLoading = false;
John Reck5b691842010-11-29 11:21:13 -0800349 if (!isPrivateBrowsingEnabled()) {
350 LogTag.logPageFinishedLoading(
351 url, SystemClock.uptimeMillis() - mLoadStartTime);
352 }
John Reck1cf4b792011-07-26 10:22:22 -0700353 syncCurrentState(view, url);
John Reck324d4402011-01-11 16:56:42 -0800354 mWebViewController.onPageFinished(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700355 }
356
Pankaj Garg79878492015-04-01 14:48:21 -0700357 @Override
358 public void onFirstVisualPixel(WebView view) {
359 mFirstVisualPixelPainted = true;
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700360 mFirstPixelObservable.set(true);
Pankaj Garg79878492015-04-01 14:48:21 -0700361 }
362
Grace Kloba22ac16e2009-10-07 18:00:23 -0700363 // return true if want to hijack the url to let another app to handle it
364 @Override
365 public boolean shouldOverrideUrlLoading(WebView view, String url) {
John Reck38b39652012-06-05 09:22:59 -0700366 if (!mDisableOverrideUrlLoading && mInForeground) {
Michael Kolb18eb3772010-12-10 14:29:51 -0800367 return mWebViewController.shouldOverrideUrlLoading(Tab.this,
368 view, url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700369 } else {
370 return false;
371 }
372 }
373
Vivek Sekharb991edb2014-12-17 18:18:07 -0800374 @Override
375 public boolean shouldDownloadFavicon(WebView view, String url) {
376 return true;
377 }
378
Grace Kloba22ac16e2009-10-07 18:00:23 -0700379 /**
Steve Block2466eff2011-10-03 15:33:09 +0100380 * Updates the security state. This method is called when we discover
381 * another resource to be loaded for this page (for example,
382 * javascript). While we update the security state, we do not update
383 * the lock icon until we are done loading, as it is slightly more
384 * secure this way.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700385 */
386 @Override
387 public void onLoadResource(WebView view, String url) {
388 if (url != null && url.length() > 0) {
389 // It is only if the page claims to be secure that we may have
Steve Block2466eff2011-10-03 15:33:09 +0100390 // to update the security state:
391 if (mCurrentState.mSecurityState == SecurityState.SECURITY_STATE_SECURE) {
392 // If NOT a 'safe' url, change the state to mixed content!
Grace Kloba22ac16e2009-10-07 18:00:23 -0700393 if (!(URLUtil.isHttpsUrl(url) || URLUtil.isDataUrl(url)
394 || URLUtil.isAboutUrl(url))) {
Steve Block2466eff2011-10-03 15:33:09 +0100395 mCurrentState.mSecurityState = SecurityState.SECURITY_STATE_MIXED;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700396 }
397 }
398 }
399 }
400
401 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700402 * Show a dialog informing the user of the network error reported by
403 * WebCore if it is in the foreground.
404 */
405 @Override
406 public void onReceivedError(WebView view, int errorCode,
407 String description, String failingUrl) {
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800408 // Used for the syncCurrentState to use
409 // the failing url instead of using webview url
410 mReceivedError = true;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700411 }
412
413 /**
414 * Check with the user if it is ok to resend POST data as the page they
415 * are trying to navigate to is the result of a POST.
416 */
417 @Override
418 public void onFormResubmission(WebView view, final Message dontResend,
419 final Message resend) {
420 if (!mInForeground) {
421 dontResend.sendToTarget();
422 return;
423 }
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500424 if (mDontResend != null) {
425 Log.w(LOGTAG, "onFormResubmission should not be called again "
426 + "while dialog is still up");
427 dontResend.sendToTarget();
428 return;
429 }
430 mDontResend = dontResend;
431 mResend = resend;
Michael Kolb14612442011-06-24 13:06:29 -0700432 new AlertDialog.Builder(mContext).setTitle(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700433 R.string.browserFrameFormResubmitLabel).setMessage(
434 R.string.browserFrameFormResubmitMessage)
435 .setPositiveButton(R.string.ok,
436 new DialogInterface.OnClickListener() {
437 public void onClick(DialogInterface dialog,
438 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500439 if (mResend != null) {
440 mResend.sendToTarget();
441 mResend = null;
442 mDontResend = null;
443 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700444 }
445 }).setNegativeButton(R.string.cancel,
446 new DialogInterface.OnClickListener() {
447 public void onClick(DialogInterface dialog,
448 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500449 if (mDontResend != null) {
450 mDontResend.sendToTarget();
451 mResend = null;
452 mDontResend = null;
453 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700454 }
455 }).setOnCancelListener(new OnCancelListener() {
456 public void onCancel(DialogInterface dialog) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500457 if (mDontResend != null) {
458 mDontResend.sendToTarget();
459 mResend = null;
460 mDontResend = null;
461 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700462 }
463 }).show();
464 }
465
466 /**
467 * Insert the url into the visited history database.
468 * @param url The url to be inserted.
469 * @param isReload True if this url is being reloaded.
470 * FIXME: Not sure what to do when reloading the page.
471 */
472 @Override
473 public void doUpdateVisitedHistory(WebView view, String url,
474 boolean isReload) {
John Reck324d4402011-01-11 16:56:42 -0800475 mWebViewController.doUpdateVisitedHistory(Tab.this, isReload);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700476 }
477
478 /**
479 * Displays SSL error(s) dialog to the user.
480 */
481 @Override
482 public void onReceivedSslError(final WebView view,
483 final SslErrorHandler handler, final SslError error) {
484 if (!mInForeground) {
485 handler.cancel();
Steve Block2466eff2011-10-03 15:33:09 +0100486 setSecurityState(SecurityState.SECURITY_STATE_NOT_SECURE);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700487 return;
488 }
John Reck35e9dd62011-04-25 09:01:54 -0700489 if (mSettings.showSecurityWarnings()) {
John Reckcb28b2c2011-08-26 17:39:44 -0700490 new AlertDialog.Builder(mContext)
491 .setTitle(R.string.security_warning)
492 .setMessage(R.string.ssl_warnings_header)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200493 .setIconAttribute(android.R.attr.alertDialogIcon)
John Reckcb28b2c2011-08-26 17:39:44 -0700494 .setPositiveButton(R.string.ssl_continue,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700495 new DialogInterface.OnClickListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700496 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700497 public void onClick(DialogInterface dialog,
498 int whichButton) {
499 handler.proceed();
Steve Block4895b012011-10-03 16:26:46 +0100500 handleProceededAfterSslError(error);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700501 }
John Reckcb28b2c2011-08-26 17:39:44 -0700502 })
503 .setNeutralButton(R.string.view_certificate,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700504 new DialogInterface.OnClickListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700505 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700506 public void onClick(DialogInterface dialog,
507 int whichButton) {
John Reckcb28b2c2011-08-26 17:39:44 -0700508 mWebViewController.showSslCertificateOnError(
509 view, handler, error);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700510 }
John Reckcb28b2c2011-08-26 17:39:44 -0700511 })
512 .setNegativeButton(R.string.ssl_go_back,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700513 new DialogInterface.OnClickListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700514 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700515 public void onClick(DialogInterface dialog,
516 int whichButton) {
John Reck30c714c2010-12-16 17:30:34 -0800517 dialog.cancel();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700518 }
John Reckcb28b2c2011-08-26 17:39:44 -0700519 })
520 .setOnCancelListener(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700521 new DialogInterface.OnCancelListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700522 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700523 public void onCancel(DialogInterface dialog) {
524 handler.cancel();
Steve Block2466eff2011-10-03 15:33:09 +0100525 setSecurityState(SecurityState.SECURITY_STATE_NOT_SECURE);
John Reck30c714c2010-12-16 17:30:34 -0800526 mWebViewController.onUserCanceledSsl(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700527 }
John Reckcb28b2c2011-08-26 17:39:44 -0700528 })
529 .show();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700530 } else {
531 handler.proceed();
532 }
533 }
534
535 /**
Steve Block4895b012011-10-03 16:26:46 +0100536 * Called when an SSL error occurred while loading a resource, but the
537 * WebView but chose to proceed anyway based on a decision retained
538 * from a previous response to onReceivedSslError(). We update our
539 * security state to reflect this.
540 */
541 @Override
542 public void onProceededAfterSslError(WebView view, SslError error) {
543 handleProceededAfterSslError(error);
544 }
545
546 /**
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700547 * Displays client certificate request to the user.
548 */
549 @Override
550 public void onReceivedClientCertRequest(final WebView view,
551 final ClientCertRequestHandler handler, final String host_and_port) {
552 if (!mInForeground) {
553 handler.ignore();
554 return;
555 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700556 int colon = host_and_port.lastIndexOf(':');
557 String host;
558 int port;
559 if (colon == -1) {
560 host = host_and_port;
561 port = -1;
562 } else {
563 String portString = host_and_port.substring(colon + 1);
564 try {
565 port = Integer.parseInt(portString);
566 host = host_and_port.substring(0, colon);
567 } catch (NumberFormatException e) {
568 host = host_and_port;
569 port = -1;
570 }
571 }
Michael Kolb14612442011-06-24 13:06:29 -0700572 KeyChain.choosePrivateKeyAlias(
573 mWebViewController.getActivity(), new KeyChainAliasCallback() {
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700574 @Override public void alias(String alias) {
575 if (alias == null) {
576 handler.cancel();
577 return;
578 }
Michael Kolb14612442011-06-24 13:06:29 -0700579 new KeyChainLookup(mContext, handler, alias).execute();
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700580 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700581 }, null, null, host, port, null);
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700582 }
583
584 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700585 * Handles an HTTP authentication request.
586 *
587 * @param handler The authentication handler
588 * @param host The host
589 * @param realm The realm
590 */
591 @Override
592 public void onReceivedHttpAuthRequest(WebView view,
593 final HttpAuthHandler handler, final String host,
594 final String realm) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700595 mWebViewController.onReceivedHttpAuthRequest(Tab.this, view, handler, host, realm);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700596 }
597
598 @Override
John Reck438bf462011-01-12 18:11:46 -0800599 public WebResourceResponse shouldInterceptRequest(WebView view,
600 String url) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800601 //intercept if opening a new incognito tab - show the incognito welcome page
Vivek Sekhared791da2015-02-22 12:39:05 -0800602 if (url.startsWith("chrome://incognito")) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800603 Resources resourceHandle = mContext.getResources();
604 InputStream inStream = resourceHandle.openRawResource(
Bijan Amirzada41242f22014-03-21 12:12:18 -0700605 com.android.browser.R.raw.incognito_mode_start_page);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800606 return new WebResourceResponse("text/html", "utf8", inStream);
607 }
kaiyiz6e5b3e02013-08-19 20:02:01 +0800608 WebResourceResponse res;
609 if (MyNavigationUtil.MY_NAVIGATION.equals(url)) {
610 res = MyNavigationProvider.shouldInterceptRequest(mContext, url);
611 } else {
612 res = HomeProvider.shouldInterceptRequest(mContext, url);
613 }
John Reck438bf462011-01-12 18:11:46 -0800614 return res;
615 }
616
617 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700618 public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
619 if (!mInForeground) {
620 return false;
621 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700622 return mWebViewController.shouldOverrideKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700623 }
624
625 @Override
626 public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700627 if (!mInForeground) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700628 return;
629 }
John Reck997b1b72012-04-19 18:08:25 -0700630 if (!mWebViewController.onUnhandledKeyEvent(event)) {
631 super.onUnhandledKeyEvent(view, event);
632 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700633 }
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700634
635 @Override
636 public void beforeNavigation(WebView view, String url) {
Pankaj Garg18aa0a12015-06-22 11:06:12 -0700637 if (BrowserCommandLine.hasSwitch("ui-low-power-mode")) {
638 return;
639 }
640
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700641 if (isPrivateBrowsingEnabled()) {
642 return;
643 }
644
645 if (!mFirstVisualPixelPainted) {
646 return;
647 }
648
649 final int idx = view.copyBackForwardList().getCurrentIndex();
650 boolean bitmapExists = view.hasSnapshot(idx);
651
652 int progress = 100;
653 Controller controller = (Controller)mWebViewController;
654 UI ui = controller.getUi();
655 if (ui instanceof BaseUi) {
656 BaseUi baseUi = (BaseUi) ui;
657 TitleBar titleBar = baseUi.getTitleBar();
658 progress = titleBar.getProgressView().getProgressPercent();
659 }
660
661 if (bitmapExists && progress < 85) {
662 return;
663 }
664
665 int index = getCaptureIndex(view.getLastCommittedHistoryIndex());
666 view.captureSnapshot(index , null);
667 }
668
669 @Override
670 public void onHistoryItemCommit(WebView view, int index) {
Pankaj Garg18aa0a12015-06-22 11:06:12 -0700671 if (BrowserCommandLine.hasSwitch("ui-low-power-mode")) {
672 return;
673 }
674
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700675 mTabHistoryUpdateObservable.set(index);
Vivek Sekhar53ef8932015-06-18 16:51:43 -0700676 final int maxIdx = view.copyBackForwardList().getSize();
677 final WebView wv = view;
678 view.getSnapshotIds(new ValueCallback <List<Integer>>() {
679 @Override
680 public void onReceiveValue(List<Integer> ids) {
681 int currentTabIdx = mWebViewController.getTabControl().getCurrentPosition();
682 for (Integer id : ids) {
683 if (getTabIdxFromCaptureIdx(id) == currentTabIdx &&
684 getNavIdxFromCaptureIdx(id) >= maxIdx) {
685 wv.deleteSnapshot(id);
686 }
687 }
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700688 }
Vivek Sekhar53ef8932015-06-18 16:51:43 -0700689 });
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700690 }
Pankaj Garg62bc7912015-04-14 16:08:59 -0700691
692 @Override
693 public void onKeyboardStateChange(boolean popup) {
694 if (BrowserSettings.getInstance().useFullscreen()) {
695 Controller controller = (Controller) mWebViewController;
696 BaseUi ui = (BaseUi) controller.getUi();
697 ui.forceDisableFullscreenMode(popup);
698 }
699 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700700 };
701
John Reck1cf4b792011-07-26 10:22:22 -0700702 private void syncCurrentState(WebView view, String url) {
703 // Sync state (in case of stop/timeout)
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800704
705 if (mReceivedError) {
706 mCurrentState.mUrl = url;
707 mCurrentState.mOriginalUrl = url;
708 } else {
709 mCurrentState.mUrl = view.getUrl();
710 mCurrentState.mOriginalUrl = view.getOriginalUrl();
711 mCurrentState.mFavicon = view.getFavicon();
712 }
713
John Reck1cf4b792011-07-26 10:22:22 -0700714 if (mCurrentState.mUrl == null) {
John Reck8bcafc12011-08-29 16:43:02 -0700715 mCurrentState.mUrl = "";
John Reck1cf4b792011-07-26 10:22:22 -0700716 }
John Reck1cf4b792011-07-26 10:22:22 -0700717 mCurrentState.mTitle = view.getTitle();
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800718
719
John Reck1cf4b792011-07-26 10:22:22 -0700720 if (!URLUtil.isHttpsUrl(mCurrentState.mUrl)) {
721 // In case we stop when loading an HTTPS page from an HTTP page
722 // but before a provisional load occurred
Steve Block2466eff2011-10-03 15:33:09 +0100723 mCurrentState.mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
Steve Block08a6f0c2011-10-06 12:12:53 +0100724 mCurrentState.mSslCertificateError = null;
John Reck1cf4b792011-07-26 10:22:22 -0700725 }
John Reck502a3532011-08-16 14:21:46 -0700726 mCurrentState.mIncognito = view.isPrivateBrowsingEnabled();
John Reck1cf4b792011-07-26 10:22:22 -0700727 }
728
Tarun Nainani8eb00912014-07-17 12:28:32 -0700729
730 public boolean isTabFullScreen() {
731 return mFullScreen;
732 }
733
Vivek Sekharf96064b2014-07-28 16:32:34 -0700734 protected void setTabFullscreen(boolean fullScreen) {
Tarun Nainani8eb00912014-07-17 12:28:32 -0700735 Controller controller = (Controller)mWebViewController;
Sudheer Koganti24766882014-10-02 10:58:09 -0700736 controller.getUi().showFullscreen(fullScreen);
Tarun Nainani8eb00912014-07-17 12:28:32 -0700737 mFullScreen = fullScreen;
Vivek Sekharf96064b2014-07-28 16:32:34 -0700738 }
739
Sudheer Koganti24766882014-10-02 10:58:09 -0700740 public boolean exitFullscreen() {
741 if (mFullScreen) {
742 Controller controller = (Controller)mWebViewController;
743 controller.getUi().showFullscreen(false);
744 if (getWebView() != null)
745 getWebView().exitFullscreen();
746 mFullScreen = false;
747 return true;
748 }
749 return false;
750 }
751
752
753
754
Grace Kloba22ac16e2009-10-07 18:00:23 -0700755 // -------------------------------------------------------------------------
756 // WebChromeClient implementation for the main WebView
757 // -------------------------------------------------------------------------
758
759 private final WebChromeClient mWebChromeClient = new WebChromeClient() {
760 // Helper method to create a new tab or sub window.
761 private void createWindow(final boolean dialog, final Message msg) {
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700762 this.createWindow(dialog, msg, null, false);
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700763 }
764
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700765 private void createWindow(final boolean dialog, final Message msg, final String url,
766 final boolean opener_suppressed) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700767 WebView.WebViewTransport transport =
768 (WebView.WebViewTransport) msg.obj;
769 if (dialog) {
770 createSubWindow();
Michael Kolb8233fac2010-10-26 16:08:53 -0700771 mWebViewController.attachSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700772 transport.setWebView(mSubView);
773 } else {
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700774 final Tab newTab = mWebViewController.openTab(url,
John Reck5949c662011-05-27 09:52:29 -0700775 Tab.this, true, true);
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700776 // This is special case for rendering links on a webpage in
777 // a new tab. If opener is suppressed, the WebContents created
778 // by the content layer are not fully initialized. This check
779 // will prevent content layer from overriding WebContents
780 // created by new tab with the uninitialized instance.
781 if (!opener_suppressed) {
782 transport.setWebView(newTab.getWebView());
783 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700784 }
785 msg.sendToTarget();
786 }
787
788 @Override
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700789 public void toggleFullscreenModeForTab(boolean enterFullscreen) {
790 if (mWebViewController instanceof Controller) {
Vivek Sekharf96064b2014-07-28 16:32:34 -0700791 setTabFullscreen(enterFullscreen);
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700792 }
793 }
794
795 @Override
Tarun Nainani8eb00912014-07-17 12:28:32 -0700796 public void onOffsetsForFullscreenChanged(float topControlsOffsetYPix,
797 float contentOffsetYPix,
798 float overdrawBottomHeightPix) {
799 if (mWebViewController instanceof Controller) {
Vivek Sekhar8ee3abb2014-07-14 12:32:05 -0700800 Controller controller = (Controller)mWebViewController;
Tarun Nainani8eb00912014-07-17 12:28:32 -0700801 controller.getUi().translateTitleBar(topControlsOffsetYPix);
Vivek Sekhar8ee3abb2014-07-14 12:32:05 -0700802 }
803 }
804
805 @Override
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700806 public boolean isTabFullScreen() {
Vivek Sekharf96064b2014-07-28 16:32:34 -0700807 return mFullScreen;
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700808 }
809
810 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700811 public boolean onCreateWindow(WebView view, final boolean dialog,
812 final boolean userGesture, final Message resultMsg) {
813 // only allow new window or sub window for the foreground case
814 if (!mInForeground) {
815 return false;
816 }
817 // Short-circuit if we can't create any more tabs or sub windows.
818 if (dialog && mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -0700819 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700820 .setTitle(R.string.too_many_subwindows_dialog_title)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200821 .setIconAttribute(android.R.attr.alertDialogIcon)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700822 .setMessage(R.string.too_many_subwindows_dialog_message)
823 .setPositiveButton(R.string.ok, null)
824 .show();
825 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700826 } else if (!mWebViewController.getTabControl().canCreateNewTab()) {
Michael Kolb14612442011-06-24 13:06:29 -0700827 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700828 .setTitle(R.string.too_many_windows_dialog_title)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200829 .setIconAttribute(android.R.attr.alertDialogIcon)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700830 .setMessage(R.string.too_many_windows_dialog_message)
831 .setPositiveButton(R.string.ok, null)
832 .show();
833 return false;
834 }
835
836 // Short-circuit if this was a user gesture.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800837 if (userGesture || !mSettings.blockPopupWindows()) {
Tarun Nainani4f5137d2015-04-16 17:26:18 -0700838 WebView.WebViewTransport transport =
839 (WebView.WebViewTransport) resultMsg.obj;
840 CreateWindowParams windowParams = transport.getCreateWindowParams();
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700841 if (windowParams.mOpenerSuppressed) {
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700842 createWindow(dialog, resultMsg, windowParams.mURL, true);
843 // This is special case for rendering links on a webpage in
844 // a new tab. If opener is suppressed, the WebContents created
845 // by the content layer are not fully initialized. Returning false
846 // will prevent content layer from overriding WebContents
847 // created by new tab with the uninitialized instance.
848 return false;
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700849 }
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700850
851 createWindow(dialog, resultMsg);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700852 return true;
853 }
854
Tarun Nainani4f5137d2015-04-16 17:26:18 -0700855 createWindow(dialog, resultMsg);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700856 return true;
857 }
858
859 @Override
Patrick Scotteb5061b2009-11-18 15:00:30 -0500860 public void onRequestFocus(WebView view) {
861 if (!mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700862 mWebViewController.switchToTab(Tab.this);
Patrick Scotteb5061b2009-11-18 15:00:30 -0500863 }
864 }
865
866 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700867 public void onCloseWindow(WebView window) {
Michael Kolbc831b632011-05-11 09:30:34 -0700868 if (mParent != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700869 // JavaScript can only close popup window.
870 if (mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700871 mWebViewController.switchToTab(mParent);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700872 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700873 mWebViewController.closeTab(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700874 }
875 }
876
877 @Override
878 public void onProgressChanged(WebView view, int newProgress) {
John Reck30c714c2010-12-16 17:30:34 -0800879 mPageLoadProgress = newProgress;
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800880 if (newProgress == 100) {
881 mInPageLoad = false;
882 }
John Reck30c714c2010-12-16 17:30:34 -0800883 mWebViewController.onProgressChanged(Tab.this);
Michael Kolb72864272012-05-03 15:42:15 -0700884 if (mUpdateThumbnail && newProgress == 100) {
885 mUpdateThumbnail = false;
886 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700887 }
888
889 @Override
Leon Scroggins21d9b902010-03-11 09:33:11 -0500890 public void onReceivedTitle(WebView view, final String title) {
John Reck30c714c2010-12-16 17:30:34 -0800891 mCurrentState.mTitle = title;
Michael Kolb8233fac2010-10-26 16:08:53 -0700892 mWebViewController.onReceivedTitle(Tab.this, title);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700893 }
894
895 @Override
896 public void onReceivedIcon(WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -0800897 mCurrentState.mFavicon = icon;
Michael Kolb8233fac2010-10-26 16:08:53 -0700898 mWebViewController.onFavicon(Tab.this, view, icon);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700899 }
900
901 @Override
902 public void onReceivedTouchIconUrl(WebView view, String url,
903 boolean precomposed) {
Michael Kolb14612442011-06-24 13:06:29 -0700904 final ContentResolver cr = mContext.getContentResolver();
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400905 // Let precomposed icons take precedence over non-composed
906 // icons.
907 if (precomposed && mTouchIconLoader != null) {
908 mTouchIconLoader.cancel(false);
909 mTouchIconLoader = null;
910 }
911 // Have only one async task at a time.
912 if (mTouchIconLoader == null) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700913 mTouchIconLoader = new DownloadTouchIcon(Tab.this,
Michael Kolb14612442011-06-24 13:06:29 -0700914 mContext, cr, view);
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400915 mTouchIconLoader.execute(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700916 }
917 }
918
919 @Override
920 public void onShowCustomView(View view,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800921 CustomViewCallback callback) {
Michael Kolb14612442011-06-24 13:06:29 -0700922 Activity activity = mWebViewController.getActivity();
923 if (activity != null) {
924 onShowCustomView(view, activity.getRequestedOrientation(), callback);
925 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400926 }
927
928 @Override
929 public void onShowCustomView(View view, int requestedOrientation,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800930 CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700931 if (mInForeground) mWebViewController.showCustomView(Tab.this, view,
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400932 requestedOrientation, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700933 }
934
935 @Override
936 public void onHideCustomView() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700937 if (mInForeground) mWebViewController.hideCustomView();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700938 }
939
940 /**
941 * The origin has exceeded its database quota.
942 * @param url the URL that exceeded the quota
943 * @param databaseIdentifier the identifier of the database on which the
944 * transaction that caused the quota overflow was run
945 * @param currentQuota the current quota for the origin.
946 * @param estimatedSize the estimated size of the database.
947 * @param totalUsedQuota is the sum of all origins' quota.
948 * @param quotaUpdater The callback to run when a decision to allow or
949 * deny quota has been made. Don't forget to call this!
950 */
951 @Override
952 public void onExceededDatabaseQuota(String url,
953 String databaseIdentifier, long currentQuota, long estimatedSize,
954 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -0700955 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -0700956 .onExceededDatabaseQuota(url, databaseIdentifier,
957 currentQuota, estimatedSize, totalUsedQuota,
958 quotaUpdater);
959 }
960
961 /**
962 * The Application Cache has exceeded its max size.
963 * @param spaceNeeded is the amount of disk space that would be needed
964 * in order for the last appcache operation to succeed.
965 * @param totalUsedQuota is the sum of all origins' quota.
966 * @param quotaUpdater A callback to inform the WebCore thread that a
967 * new app cache size is available. This callback must always
968 * be executed at some point to ensure that the sleeping
969 * WebCore thread is woken up.
970 */
971 @Override
972 public void onReachedMaxAppCacheSize(long spaceNeeded,
973 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -0700974 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -0700975 .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota,
976 quotaUpdater);
977 }
978
Ben Murdoch65acc352009-11-19 18:16:04 +0000979 /* Adds a JavaScript error message to the system log and if the JS
980 * console is enabled in the about:debug options, to that console
981 * also.
Ben Murdochc42addf2010-01-28 15:19:59 +0000982 * @param consoleMessage the message object.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700983 */
984 @Override
Ben Murdochc42addf2010-01-28 15:19:59 +0000985 public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Jeff Hamilton47654f42010-09-07 09:57:51 -0500986 // Don't log console messages in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -0700987 if (isPrivateBrowsingEnabled()) return true;
Jeff Hamilton47654f42010-09-07 09:57:51 -0500988
Ben Murdochc42addf2010-01-28 15:19:59 +0000989 String message = "Console: " + consoleMessage.message() + " "
990 + consoleMessage.sourceId() + ":"
991 + consoleMessage.lineNumber();
992
993 switch (consoleMessage.messageLevel()) {
994 case TIP:
995 Log.v(CONSOLE_LOGTAG, message);
996 break;
997 case LOG:
998 Log.i(CONSOLE_LOGTAG, message);
999 break;
1000 case WARNING:
1001 Log.w(CONSOLE_LOGTAG, message);
1002 break;
1003 case ERROR:
1004 Log.e(CONSOLE_LOGTAG, message);
1005 break;
1006 case DEBUG:
1007 Log.d(CONSOLE_LOGTAG, message);
1008 break;
1009 }
1010
1011 return true;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001012 }
1013
1014 /**
1015 * Ask the browser for an icon to represent a <video> element.
1016 * This icon will be used if the Web page did not specify a poster attribute.
1017 * @return Bitmap The icon or null if no such icon is available.
1018 */
1019 @Override
1020 public Bitmap getDefaultVideoPoster() {
1021 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001022 return mWebViewController.getDefaultVideoPoster();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001023 }
1024 return null;
1025 }
1026
1027 /**
1028 * Ask the host application for a custom progress view to show while
1029 * a <video> is loading.
1030 * @return View The progress view.
1031 */
1032 @Override
1033 public View getVideoLoadingProgressView() {
1034 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001035 return mWebViewController.getVideoLoadingProgressView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001036 }
1037 return null;
1038 }
1039
1040 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00001041 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001042 if (mInForeground) {
Ben Murdoch8cad4132012-01-11 10:56:43 +00001043 mWebViewController.openFileChooser(uploadMsg, acceptType, capture);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001044 } else {
1045 uploadMsg.onReceiveValue(null);
1046 }
1047 }
1048
Vivek Sekharb54614f2014-05-01 19:03:37 -07001049 @Override
1050 public void showFileChooser(ValueCallback<String[]> uploadFilePaths, String acceptTypes,
1051 boolean capture) {
1052 if (mInForeground) {
1053 mWebViewController.showFileChooser(uploadFilePaths, acceptTypes, capture);
1054 } else {
1055 uploadFilePaths.onReceiveValue(null);
1056 }
1057 }
1058
Grace Kloba22ac16e2009-10-07 18:00:23 -07001059 /**
1060 * Deliver a list of already-visited URLs
1061 */
1062 @Override
1063 public void getVisitedHistory(final ValueCallback<String[]> callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001064 mWebViewController.getVisitedHistory(callback);
1065 }
Ben Murdoch8029a772010-11-16 11:58:21 +00001066
1067 @Override
1068 public void setupAutoFill(Message message) {
1069 // Prompt the user to set up their profile.
1070 final Message msg = message;
Michael Kolb14612442011-06-24 13:06:29 -07001071 AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
1072 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
Ben Murdoch1d676b62011-01-17 12:54:24 +00001073 Context.LAYOUT_INFLATER_SERVICE);
1074 final View layout = inflater.inflate(R.layout.setup_autofill_dialog, null);
1075
1076 builder.setView(layout)
1077 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1078 @Override
1079 public void onClick(DialogInterface dialog, int id) {
1080 CheckBox disableAutoFill = (CheckBox) layout.findViewById(
1081 R.id.setup_autofill_dialog_disable_autofill);
1082
1083 if (disableAutoFill.isChecked()) {
1084 // Disable autofill and show a toast with how to turn it on again.
John Reck35e9dd62011-04-25 09:01:54 -07001085 mSettings.setAutofillEnabled(false);
Michael Kolb14612442011-06-24 13:06:29 -07001086 Toast.makeText(mContext,
Ben Murdoch1d676b62011-01-17 12:54:24 +00001087 R.string.autofill_setup_dialog_negative_toast,
1088 Toast.LENGTH_LONG).show();
1089 } else {
1090 // Take user to the AutoFill profile editor. When they return,
1091 // we will send the message that we pass here which will trigger
1092 // the form to get filled out with their new profile.
1093 mWebViewController.setupAutoFill(msg);
1094 }
1095 }
1096 })
1097 .setNegativeButton(R.string.cancel, null)
1098 .show();
Ben Murdoch8029a772010-11-16 11:58:21 +00001099 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001100 };
1101
1102 // -------------------------------------------------------------------------
1103 // WebViewClient implementation for the sub window
1104 // -------------------------------------------------------------------------
1105
1106 // Subclass of WebViewClient used in subwindows to notify the main
1107 // WebViewClient of certain WebView activities.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001108 private static class SubWindowClient extends WebViewClient {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001109 // The main WebViewClient.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001110 private final WebViewClient mClient;
Michael Kolb8233fac2010-10-26 16:08:53 -07001111 private final WebViewController mController;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001112
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001113 SubWindowClient(WebViewClient client, WebViewController controller) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001114 mClient = client;
Michael Kolb8233fac2010-10-26 16:08:53 -07001115 mController = controller;
Leon Scroggins III211ba542010-04-19 13:21:13 -04001116 }
1117 @Override
1118 public void onPageStarted(WebView view, String url, Bitmap favicon) {
1119 // Unlike the others, do not call mClient's version, which would
1120 // change the progress bar. However, we do want to remove the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001121 // find or select dialog.
Michael Kolb8233fac2010-10-26 16:08:53 -07001122 mController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001123 }
1124 @Override
1125 public void doUpdateVisitedHistory(WebView view, String url,
1126 boolean isReload) {
1127 mClient.doUpdateVisitedHistory(view, url, isReload);
1128 }
1129 @Override
1130 public boolean shouldOverrideUrlLoading(WebView view, String url) {
1131 return mClient.shouldOverrideUrlLoading(view, url);
1132 }
1133 @Override
1134 public void onReceivedSslError(WebView view, SslErrorHandler handler,
1135 SslError error) {
1136 mClient.onReceivedSslError(view, handler, error);
1137 }
1138 @Override
Brian Carlstrom8862c1d2011-06-02 01:05:55 -07001139 public void onReceivedClientCertRequest(WebView view,
1140 ClientCertRequestHandler handler, String host_and_port) {
1141 mClient.onReceivedClientCertRequest(view, handler, host_and_port);
1142 }
1143 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -07001144 public void onReceivedHttpAuthRequest(WebView view,
1145 HttpAuthHandler handler, String host, String realm) {
1146 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
1147 }
1148 @Override
1149 public void onFormResubmission(WebView view, Message dontResend,
1150 Message resend) {
1151 mClient.onFormResubmission(view, dontResend, resend);
1152 }
1153 @Override
1154 public void onReceivedError(WebView view, int errorCode,
1155 String description, String failingUrl) {
1156 mClient.onReceivedError(view, errorCode, description, failingUrl);
1157 }
1158 @Override
1159 public boolean shouldOverrideKeyEvent(WebView view,
1160 android.view.KeyEvent event) {
1161 return mClient.shouldOverrideKeyEvent(view, event);
1162 }
1163 @Override
1164 public void onUnhandledKeyEvent(WebView view,
1165 android.view.KeyEvent event) {
1166 mClient.onUnhandledKeyEvent(view, event);
1167 }
1168 }
1169
1170 // -------------------------------------------------------------------------
1171 // WebChromeClient implementation for the sub window
1172 // -------------------------------------------------------------------------
1173
1174 private class SubWindowChromeClient extends WebChromeClient {
1175 // The main WebChromeClient.
1176 private final WebChromeClient mClient;
1177
1178 SubWindowChromeClient(WebChromeClient client) {
1179 mClient = client;
1180 }
1181 @Override
1182 public void onProgressChanged(WebView view, int newProgress) {
1183 mClient.onProgressChanged(view, newProgress);
1184 }
1185 @Override
1186 public boolean onCreateWindow(WebView view, boolean dialog,
1187 boolean userGesture, android.os.Message resultMsg) {
1188 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
1189 }
1190 @Override
1191 public void onCloseWindow(WebView window) {
1192 if (window != mSubView) {
1193 Log.e(LOGTAG, "Can't close the window");
1194 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001195 mWebViewController.dismissSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001196 }
1197 }
1198
1199 // -------------------------------------------------------------------------
1200
1201 // Construct a new tab
Michael Kolb7bcafde2011-05-09 13:55:59 -07001202 Tab(WebViewController wvcontroller, WebView w) {
John Reck1cf4b792011-07-26 10:22:22 -07001203 this(wvcontroller, w, null);
1204 }
1205
1206 Tab(WebViewController wvcontroller, Bundle state) {
1207 this(wvcontroller, null, state);
1208 }
1209
1210 Tab(WebViewController wvcontroller, WebView w, Bundle state) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001211 mWebViewController = wvcontroller;
Michael Kolb14612442011-06-24 13:06:29 -07001212 mContext = mWebViewController.getContext();
John Reck35e9dd62011-04-25 09:01:54 -07001213 mSettings = BrowserSettings.getInstance();
Michael Kolb14612442011-06-24 13:06:29 -07001214 mDataController = DataController.getInstance(mContext);
1215 mCurrentState = new PageState(mContext, w != null
John Reck847b5322011-04-14 17:02:18 -07001216 ? w.isPrivateBrowsingEnabled() : false);
Tarun Nainani8084c822014-06-25 13:38:06 -07001217 setTimeStamp();
Michael Kolb8233fac2010-10-26 16:08:53 -07001218 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001219 mInForeground = false;
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001220 mWebViewDestroyedByMemoryMonitor = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001221
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001222 mDownloadListener = new BrowserDownloadListener() {
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001223 public void onDownloadStart(String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001224 String contentDisposition, String mimetype, String referer,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001225 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001226 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001227 mimetype, referer, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001228 }
1229 };
1230
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001231 mCaptureWidth = mContext.getResources().getDimensionPixelSize(R.dimen.tab_thumbnail_width);
1232 mCaptureHeight =mContext.getResources().getDimensionPixelSize(R.dimen.tab_thumbnail_height);
1233
1234 initCaptureBitmap();
1235
John Reck1cf4b792011-07-26 10:22:22 -07001236 restoreState(state);
John Reck52be4782011-08-26 15:37:29 -07001237 if (getId() == -1) {
1238 mId = TabControl.getNextId();
1239 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001240 setWebView(w);
Michael Kolb9ef259a2011-07-12 15:33:08 -07001241 mHandler = new Handler() {
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001242 @Override
Michael Kolb9ef259a2011-07-12 15:33:08 -07001243 public void handleMessage(Message m) {
John Reck1cf4b792011-07-26 10:22:22 -07001244 switch (m.what) {
1245 case MSG_CAPTURE:
1246 capture();
1247 break;
1248 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001249 }
1250 };
Pankaj Garg1c13cab2015-05-12 11:52:17 -07001251
1252 mFirstPixelObservable = new Observable();
1253 mFirstPixelObservable.set(false);
1254 mTabHistoryUpdateObservable = new Observable();
John Reck1cf4b792011-07-26 10:22:22 -07001255 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001256
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001257 private void initCaptureBitmap() {
1258 mCapture = Bitmap.createBitmap(mCaptureWidth, mCaptureHeight, Bitmap.Config.RGB_565);
1259 mCapture.eraseColor(Color.WHITE);
Michael Kolb72864272012-05-03 15:42:15 -07001260 }
1261
Mathew Inwoode09305e2011-09-02 12:03:26 +01001262 /**
1263 * This is used to get a new ID when the tab has been preloaded, before it is displayed and
1264 * added to TabControl. Preloaded tabs can be created before restoreInstanceState, leading
1265 * to overlapping IDs between the preloaded and restored tabs.
1266 */
1267 public void refreshIdAfterPreload() {
1268 mId = TabControl.getNextId();
1269 }
1270
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001271 public void setController(WebViewController ctl) {
1272 mWebViewController = ctl;
1273
John Reck1cf4b792011-07-26 10:22:22 -07001274 if (mWebViewController.shouldCaptureThumbnails()) {
1275 synchronized (Tab.this) {
1276 if (mCapture == null) {
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001277 initCaptureBitmap();
1278 if (mInForeground && !mHandler.hasMessages(MSG_CAPTURE)) {
1279 mHandler.sendEmptyMessageDelayed(MSG_CAPTURE, CAPTURE_DELAY);
John Reck1cf4b792011-07-26 10:22:22 -07001280 }
1281 }
1282 }
1283 } else {
1284 synchronized (Tab.this) {
1285 mCapture = null;
1286 deleteThumbnail();
1287 }
1288 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001289 }
1290
Michael Kolbc831b632011-05-11 09:30:34 -07001291 public long getId() {
1292 return mId;
1293 }
1294
Michael Kolb91911a22012-01-17 11:21:25 -08001295 void setWebView(WebView w) {
1296 setWebView(w, true);
1297 }
1298
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001299 public boolean isNativeActive(){
1300 if (mMainView == null)
1301 return false;
1302 return true;
1303 }
1304
1305 public void setTimeStamp(){
1306 Date d = new Date();
1307 timestamp = (new Timestamp(d.getTime()));
1308 }
1309
1310 public Timestamp getTimestamp() {
1311 return timestamp;
1312 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001313 /**
1314 * Sets the WebView for this tab, correctly removing the old WebView from
1315 * the container view.
1316 */
Michael Kolb91911a22012-01-17 11:21:25 -08001317 void setWebView(WebView w, boolean restore) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001318 if (mMainView == w) {
1319 return;
1320 }
Michael Kolba713ec82010-11-29 17:27:06 -08001321
Michael Kolba713ec82010-11-29 17:27:06 -08001322 mWebViewController.onSetWebView(this, w);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001323
John Reck1cf4b792011-07-26 10:22:22 -07001324 if (mMainView != null) {
John Reck8ee633f2011-08-09 16:00:35 -07001325 mMainView.setPictureListener(null);
John Reck1cf4b792011-07-26 10:22:22 -07001326 if (w != null) {
1327 syncCurrentState(w, null);
1328 } else {
Panos Thomasa9a5a582014-03-18 19:20:08 -07001329 mCurrentState = new PageState(mContext, mMainView.isPrivateBrowsingEnabled());
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001330
1331 if (mWebViewDestroyedByMemoryMonitor) {
1332 /*
1333 * If tab was destroyed as a result of the MemoryMonitor
1334 * then we need to restore the state properties
1335 * from the old WebView (mMainView)
1336 */
1337 syncCurrentState(mMainView, null);
1338 mWebViewDestroyedByMemoryMonitor = false;
1339 }
John Reck1cf4b792011-07-26 10:22:22 -07001340 }
1341 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001342 // set the new one
1343 mMainView = w;
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001344
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001345 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001346 if (mMainView != null) {
1347 mMainView.setWebViewClient(mWebViewClient);
1348 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001349 // Attach DownloadManager so that downloads can start in an active
1350 // or a non-active window. This can happen when going to a site that
1351 // does a redirect after a period of time. The user could have
1352 // switched to another tab while waiting for the download to start.
1353 mMainView.setDownloadListener(mDownloadListener);
John Reck8ee633f2011-08-09 16:00:35 -07001354 TabControl tc = mWebViewController.getTabControl();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001355 if (tc != null /*&& tc.getOnThumbnailUpdatedListener() != null*/) {
John Reck8ee633f2011-08-09 16:00:35 -07001356 mMainView.setPictureListener(this);
1357 }
Michael Kolb91911a22012-01-17 11:21:25 -08001358 if (restore && (mSavedState != null)) {
John Reck8b9bb8b2012-03-08 13:19:40 -08001359 restoreUserAgent();
John Reck6c2e2f32011-08-22 13:41:23 -07001360 WebBackForwardList restoredState
1361 = mMainView.restoreState(mSavedState);
1362 if (restoredState == null || restoredState.getSize() == 0) {
1363 Log.w(LOGTAG, "Failed to restore WebView state!");
1364 loadUrl(mCurrentState.mOriginalUrl, null);
1365 }
John Reck1cf4b792011-07-26 10:22:22 -07001366 mSavedState = null;
1367 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001368 }
1369 }
1370
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001371 public void destroyThroughMemoryMonitor() {
1372 mWebViewDestroyedByMemoryMonitor = true;
1373 destroy();
1374 }
1375
Grace Kloba22ac16e2009-10-07 18:00:23 -07001376 /**
1377 * Destroy the tab's main WebView and subWindow if any
1378 */
1379 void destroy() {
Tarun Nainani2c1dd7c2014-07-05 16:40:12 -07001380
Matthew Huib7f2e9c2014-04-16 11:12:37 -04001381 if (mPostponeDestroy) {
1382 mShouldDestroy = true;
1383 return;
1384 }
1385 mShouldDestroy = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001386 if (mMainView != null) {
1387 dismissSubWindow();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001388 // save the WebView to call destroy() after detach it from the tab
Vivek Sekhar53ef8932015-06-18 16:51:43 -07001389 final WebView webView = mMainView;
Pankaj Garg18aa0a12015-06-22 11:06:12 -07001390 if (!mWebViewDestroyedByMemoryMonitor &&
1391 !BrowserCommandLine.hasSwitch("ui-low-power-mode")) {
Vivek Sekhar53ef8932015-06-18 16:51:43 -07001392 webView.getSnapshotIds(new ValueCallback<List<Integer>>() {
1393 @Override
1394 public void onReceiveValue(List<Integer> ids) {
1395 int currentTabIdx = mWebViewController.getTabControl().getCurrentPosition();
1396 for (Integer id : ids) {
1397 if (getTabIdxFromCaptureIdx(id) == currentTabIdx) {
1398 webView.deleteSnapshot(id);
1399 }
1400 }
1401 setWebView(null);
1402 webView.destroy();
Pankaj Garg1c13cab2015-05-12 11:52:17 -07001403 }
Vivek Sekhar53ef8932015-06-18 16:51:43 -07001404 });
1405 return;
Pankaj Garg1c13cab2015-05-12 11:52:17 -07001406 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001407 setWebView(null);
1408 webView.destroy();
1409 }
1410 }
1411
Matthew Huib7f2e9c2014-04-16 11:12:37 -04001412 private boolean mPostponeDestroy = false;
1413 private boolean mShouldDestroy = false;
1414
1415 public void postponeDestroy() {
1416 mPostponeDestroy = true;
1417 }
1418
1419 public void performPostponedDestroy() {
1420 mPostponeDestroy = false;
1421 if (mShouldDestroy) {
1422 destroy();
1423 }
1424 }
1425
Grace Kloba22ac16e2009-10-07 18:00:23 -07001426 /**
1427 * Remove the tab from the parent
1428 */
1429 void removeFromTree() {
1430 // detach the children
Michael Kolbc831b632011-05-11 09:30:34 -07001431 if (mChildren != null) {
1432 for(Tab t : mChildren) {
1433 t.setParent(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001434 }
1435 }
1436 // remove itself from the parent list
Michael Kolbc831b632011-05-11 09:30:34 -07001437 if (mParent != null) {
1438 mParent.mChildren.remove(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001439 }
Tarun Nainani8eb00912014-07-17 12:28:32 -07001440
1441 mCapture = null;
John Reck1cf4b792011-07-26 10:22:22 -07001442 deleteThumbnail();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001443 }
1444
1445 /**
1446 * Create a new subwindow unless a subwindow already exists.
1447 * @return True if a new subwindow was created. False if one already exists.
1448 */
1449 boolean createSubWindow() {
1450 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001451 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001452 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001453 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001454 mSubView.setWebChromeClient(new SubWindowChromeClient(
1455 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001456 // Set a different DownloadListener for the mSubView, since it will
1457 // just need to dismiss the mSubView, rather than close the Tab
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001458 mSubView.setDownloadListener(new BrowserDownloadListener() {
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001459 public void onDownloadStart(String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001460 String contentDisposition, String mimetype, String referer,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001461 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001462 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001463 contentDisposition, mimetype, referer, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001464 if (mSubView.copyBackForwardList().getSize() == 0) {
1465 // This subwindow was opened for the sole purpose of
1466 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001467 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001468 }
1469 }
1470 });
Michael Kolb14612442011-06-24 13:06:29 -07001471 mSubView.setOnCreateContextMenuListener(mWebViewController.getActivity());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001472 return true;
1473 }
1474 return false;
1475 }
1476
1477 /**
1478 * Dismiss the subWindow for the tab.
1479 */
1480 void dismissSubWindow() {
1481 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001482 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001483 mSubView.destroy();
1484 mSubView = null;
1485 mSubViewContainer = null;
1486 }
1487 }
1488
Grace Kloba22ac16e2009-10-07 18:00:23 -07001489
1490 /**
1491 * Set the parent tab of this tab.
1492 */
Michael Kolbc831b632011-05-11 09:30:34 -07001493 void setParent(Tab parent) {
John Reck52be4782011-08-26 15:37:29 -07001494 if (parent == this) {
1495 throw new IllegalStateException("Cannot set parent to self!");
1496 }
Michael Kolbc831b632011-05-11 09:30:34 -07001497 mParent = parent;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001498 // This tab may have been freed due to low memory. If that is the case,
Michael Kolbc831b632011-05-11 09:30:34 -07001499 // the parent tab id is already saved. If we are changing that id
Grace Kloba22ac16e2009-10-07 18:00:23 -07001500 // (most likely due to removing the parent tab) we must update the
Michael Kolbc831b632011-05-11 09:30:34 -07001501 // parent tab id in the saved Bundle.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001502 if (mSavedState != null) {
1503 if (parent == null) {
1504 mSavedState.remove(PARENTTAB);
1505 } else {
Michael Kolbc831b632011-05-11 09:30:34 -07001506 mSavedState.putLong(PARENTTAB, parent.getId());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001507 }
1508 }
John Reckb0a86db2011-05-24 14:05:58 -07001509
1510 // Sync the WebView useragent with the parent
1511 if (parent != null && mSettings.hasDesktopUseragent(parent.getWebView())
1512 != mSettings.hasDesktopUseragent(getWebView())) {
1513 mSettings.toggleDesktopUseragent(getWebView());
1514 }
John Reck52be4782011-08-26 15:37:29 -07001515
1516 if (parent != null && parent.getId() == getId()) {
1517 throw new IllegalStateException("Parent has same ID as child!");
1518 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001519 }
1520
1521 /**
Michael Kolbc831b632011-05-11 09:30:34 -07001522 * If this Tab was created through another Tab, then this method returns
1523 * that Tab.
1524 * @return the Tab parent or null
1525 */
1526 public Tab getParent() {
1527 return mParent;
1528 }
1529
1530 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001531 * When a Tab is created through the content of another Tab, then we
1532 * associate the Tabs.
1533 * @param child the Tab that was created from this Tab
1534 */
1535 void addChildTab(Tab child) {
Michael Kolbc831b632011-05-11 09:30:34 -07001536 if (mChildren == null) {
1537 mChildren = new Vector<Tab>();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001538 }
Michael Kolbc831b632011-05-11 09:30:34 -07001539 mChildren.add(child);
1540 child.setParent(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001541 }
1542
Michael Kolbc831b632011-05-11 09:30:34 -07001543 Vector<Tab> getChildren() {
1544 return mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001545 }
1546
1547 void resume() {
1548 if (mMainView != null) {
John Reck56c1fcf2011-08-17 10:15:16 -07001549 setupHwAcceleration(mMainView);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001550 mMainView.onResume();
1551 if (mSubView != null) {
1552 mSubView.onResume();
1553 }
1554 }
1555 }
1556
John Reck56c1fcf2011-08-17 10:15:16 -07001557 private void setupHwAcceleration(View web) {
1558 if (web == null) return;
1559 BrowserSettings settings = BrowserSettings.getInstance();
1560 if (settings.isHardwareAccelerated()) {
1561 web.setLayerType(View.LAYER_TYPE_NONE, null);
1562 } else {
1563 web.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
1564 }
1565 }
1566
Grace Kloba22ac16e2009-10-07 18:00:23 -07001567 void pause() {
1568 if (mMainView != null) {
1569 mMainView.onPause();
1570 if (mSubView != null) {
1571 mSubView.onPause();
1572 }
1573 }
1574 }
1575
1576 void putInForeground() {
John Reck8ee633f2011-08-09 16:00:35 -07001577 if (mInForeground) {
1578 return;
1579 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001580 mInForeground = true;
1581 resume();
Michael Kolb14612442011-06-24 13:06:29 -07001582 Activity activity = mWebViewController.getActivity();
1583 mMainView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001584 if (mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -07001585 mSubView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001586 }
Axesh R. Ajmerac6b5c322015-05-01 11:06:10 -07001587
Leon Scroggins1961ed22010-12-07 15:22:21 -05001588 mWebViewController.bookmarkedStatusHasChanged(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001589 }
1590
1591 void putInBackground() {
John Reck8ee633f2011-08-09 16:00:35 -07001592 if (!mInForeground) {
1593 return;
1594 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001595 mInForeground = false;
1596 pause();
1597 mMainView.setOnCreateContextMenuListener(null);
1598 if (mSubView != null) {
1599 mSubView.setOnCreateContextMenuListener(null);
1600 }
1601 }
1602
Michael Kolb8233fac2010-10-26 16:08:53 -07001603 boolean inForeground() {
1604 return mInForeground;
1605 }
1606
Grace Kloba22ac16e2009-10-07 18:00:23 -07001607 /**
1608 * Return the top window of this tab; either the subwindow if it is not
1609 * null or the main window.
1610 * @return The top window of this tab.
1611 */
1612 WebView getTopWindow() {
1613 if (mSubView != null) {
1614 return mSubView;
1615 }
1616 return mMainView;
1617 }
1618
1619 /**
1620 * Return the main window of this tab. Note: if a tab is freed in the
1621 * background, this can return null. It is only guaranteed to be
1622 * non-null for the current tab.
1623 * @return The main WebView of this tab.
1624 */
1625 WebView getWebView() {
1626 return mMainView;
1627 }
1628
Michael Kolba713ec82010-11-29 17:27:06 -08001629 void setViewContainer(View container) {
1630 mContainer = container;
1631 }
1632
Michael Kolb8233fac2010-10-26 16:08:53 -07001633 View getViewContainer() {
1634 return mContainer;
1635 }
1636
Grace Kloba22ac16e2009-10-07 18:00:23 -07001637 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001638 * Return whether private browsing is enabled for the main window of
1639 * this tab.
1640 * @return True if private browsing is enabled.
1641 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001642 boolean isPrivateBrowsingEnabled() {
John Reck502a3532011-08-16 14:21:46 -07001643 return mCurrentState.mIncognito;
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001644 }
1645
1646 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001647 * Return the subwindow of this tab or null if there is no subwindow.
1648 * @return The subwindow of this tab or null.
1649 */
1650 WebView getSubWebView() {
1651 return mSubView;
1652 }
1653
Michael Kolb1514bb72010-11-22 09:11:48 -08001654 void setSubWebView(WebView subView) {
1655 mSubView = subView;
1656 }
1657
Michael Kolb8233fac2010-10-26 16:08:53 -07001658 View getSubViewContainer() {
1659 return mSubViewContainer;
1660 }
1661
Michael Kolb1514bb72010-11-22 09:11:48 -08001662 void setSubViewContainer(View subViewContainer) {
1663 mSubViewContainer = subViewContainer;
1664 }
1665
Grace Kloba22ac16e2009-10-07 18:00:23 -07001666
1667 /**
1668 * @return The application id string
1669 */
1670 String getAppId() {
1671 return mAppId;
1672 }
1673
1674 /**
1675 * Set the application id string
1676 * @param id
1677 */
1678 void setAppId(String id) {
1679 mAppId = id;
1680 }
1681
Michael Kolbe28b3472011-08-04 16:54:31 -07001682 boolean closeOnBack() {
1683 return mCloseOnBack;
1684 }
1685
1686 void setCloseOnBack(boolean close) {
1687 mCloseOnBack = close;
1688 }
1689
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001690 boolean getDerivedFromIntent() {
1691 return mDerivedFromIntent;
1692 }
1693
1694 void setDerivedFromIntent(boolean derived) {
1695 mDerivedFromIntent = derived;
1696 }
1697
Grace Kloba22ac16e2009-10-07 18:00:23 -07001698 String getUrl() {
John Reck324d4402011-01-11 16:56:42 -08001699 return UrlUtils.filteredUrl(mCurrentState.mUrl);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001700 }
1701
Tarun Nainani8eb00912014-07-17 12:28:32 -07001702
1703 protected void onPageFinished() {
1704 mPageFinished = true;
1705 }
1706
1707 public boolean getPageFinishedStatus() {
1708 return mPageFinished;
1709 }
1710
John Reck49a603c2011-03-03 09:33:05 -08001711 String getOriginalUrl() {
John Reckdb22ec42011-06-29 11:31:24 -07001712 if (mCurrentState.mOriginalUrl == null) {
1713 return getUrl();
John Reck49a603c2011-03-03 09:33:05 -08001714 }
John Reckdb22ec42011-06-29 11:31:24 -07001715 return UrlUtils.filteredUrl(mCurrentState.mOriginalUrl);
John Reck49a603c2011-03-03 09:33:05 -08001716 }
1717
Grace Kloba22ac16e2009-10-07 18:00:23 -07001718 /**
John Reck30c714c2010-12-16 17:30:34 -08001719 * Get the title of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001720 */
1721 String getTitle() {
John Reck30c714c2010-12-16 17:30:34 -08001722 return mCurrentState.mTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001723 }
1724
1725 /**
John Reck30c714c2010-12-16 17:30:34 -08001726 * Get the favicon of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001727 */
1728 Bitmap getFavicon() {
John Reck1cf4b792011-07-26 10:22:22 -07001729 if (mCurrentState.mFavicon != null) {
1730 return mCurrentState.mFavicon;
1731 }
1732 return getDefaultFavicon(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001733 }
1734
Pankaj Garg32e1b942015-06-03 18:13:24 -07001735 public boolean hasFavicon() {
1736 return mCurrentState.mFavicon != null;
1737 }
1738
John Recke969cc52010-12-21 17:24:43 -08001739 public boolean isBookmarkedSite() {
1740 return mCurrentState.mIsBookmarkedSite;
1741 }
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001742
Grace Kloba22ac16e2009-10-07 18:00:23 -07001743 /**
Steve Block08a6f0c2011-10-06 12:12:53 +01001744 * Sets the security state, clears the SSL certificate error and informs
1745 * the controller.
1746 */
Steve Block2466eff2011-10-03 15:33:09 +01001747 private void setSecurityState(SecurityState securityState) {
1748 mCurrentState.mSecurityState = securityState;
Steve Block08a6f0c2011-10-06 12:12:53 +01001749 mCurrentState.mSslCertificateError = null;
Steve Block2466eff2011-10-03 15:33:09 +01001750 mWebViewController.onUpdatedSecurityState(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001751 }
1752
1753 /**
Steve Block2466eff2011-10-03 15:33:09 +01001754 * @return The tab's security state.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001755 */
Steve Block2466eff2011-10-03 15:33:09 +01001756 SecurityState getSecurityState() {
1757 return mCurrentState.mSecurityState;
John Reck30c714c2010-12-16 17:30:34 -08001758 }
1759
Steve Block08a6f0c2011-10-06 12:12:53 +01001760 /**
1761 * Gets the SSL certificate error, if any, for the page's main resource.
1762 * This is only non-null when the security state is
1763 * SECURITY_STATE_BAD_CERTIFICATE.
1764 */
1765 SslError getSslCertificateError() {
1766 return mCurrentState.mSslCertificateError;
1767 }
1768
John Reck30c714c2010-12-16 17:30:34 -08001769 int getLoadProgress() {
1770 if (mInPageLoad) {
1771 return mPageLoadProgress;
1772 }
1773 return 100;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001774 }
1775
1776 /**
1777 * @return TRUE if onPageStarted is called while onPageFinished is not
1778 * called yet.
1779 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001780 boolean inPageLoad() {
1781 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001782 }
1783
Grace Kloba22ac16e2009-10-07 18:00:23 -07001784 /**
John Reck1cf4b792011-07-26 10:22:22 -07001785 * @return The Bundle with the tab's state if it can be saved, otherwise null
Grace Kloba22ac16e2009-10-07 18:00:23 -07001786 */
John Reck1cf4b792011-07-26 10:22:22 -07001787 public Bundle saveState() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001788 // If the WebView is null it means we ran low on memory and we already
1789 // stored the saved state in mSavedState.
1790 if (mMainView == null) {
John Reck1cf4b792011-07-26 10:22:22 -07001791 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001792 }
John Reck6c2e2f32011-08-22 13:41:23 -07001793
1794 if (TextUtils.isEmpty(mCurrentState.mUrl)) {
John Reck1cf4b792011-07-26 10:22:22 -07001795 return null;
John Reck24f18262011-06-17 14:47:20 -07001796 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001797
1798 mSavedState = new Bundle();
John Reck6c2e2f32011-08-22 13:41:23 -07001799 WebBackForwardList savedList = mMainView.saveState(mSavedState);
1800 if (savedList == null || savedList.getSize() == 0) {
1801 Log.w(LOGTAG, "Failed to save back/forward list for "
1802 + mCurrentState.mUrl);
1803 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001804
Michael Kolbc831b632011-05-11 09:30:34 -07001805 mSavedState.putLong(ID, mId);
John Reck30c714c2010-12-16 17:30:34 -08001806 mSavedState.putString(CURRURL, mCurrentState.mUrl);
1807 mSavedState.putString(CURRTITLE, mCurrentState.mTitle);
John Reck1cf4b792011-07-26 10:22:22 -07001808 mSavedState.putBoolean(INCOGNITO, mMainView.isPrivateBrowsingEnabled());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001809 if (mAppId != null) {
1810 mSavedState.putString(APPID, mAppId);
1811 }
Michael Kolbe28b3472011-08-04 16:54:31 -07001812 mSavedState.putBoolean(CLOSEFLAG, mCloseOnBack);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001813 // Remember the parent tab so the relationship can be restored.
Michael Kolbc831b632011-05-11 09:30:34 -07001814 if (mParent != null) {
1815 mSavedState.putLong(PARENTTAB, mParent.mId);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001816 }
John Reckb0a86db2011-05-24 14:05:58 -07001817 mSavedState.putBoolean(USERAGENT,
1818 mSettings.hasDesktopUseragent(getWebView()));
John Reck1cf4b792011-07-26 10:22:22 -07001819 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001820 }
1821
1822 /*
1823 * Restore the state of the tab.
1824 */
John Reck1cf4b792011-07-26 10:22:22 -07001825 private void restoreState(Bundle b) {
1826 mSavedState = b;
1827 if (mSavedState == null) {
1828 return;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001829 }
1830 // Restore the internal state even if the WebView fails to restore.
1831 // This will maintain the app id, original url and close-on-exit values.
Michael Kolbc831b632011-05-11 09:30:34 -07001832 mId = b.getLong(ID);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001833 mAppId = b.getString(APPID);
Michael Kolbe28b3472011-08-04 16:54:31 -07001834 mCloseOnBack = b.getBoolean(CLOSEFLAG);
John Reck8b9bb8b2012-03-08 13:19:40 -08001835 restoreUserAgent();
John Reck1cf4b792011-07-26 10:22:22 -07001836 String url = b.getString(CURRURL);
1837 String title = b.getString(CURRTITLE);
1838 boolean incognito = b.getBoolean(INCOGNITO);
1839 mCurrentState = new PageState(mContext, incognito, url, null);
1840 mCurrentState.mTitle = title;
1841 synchronized (Tab.this) {
1842 if (mCapture != null) {
John Reck4eadc342011-10-31 14:04:10 -07001843 DataController.getInstance(mContext).loadThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07001844 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001845 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001846 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04001847
John Reck8b9bb8b2012-03-08 13:19:40 -08001848 private void restoreUserAgent() {
1849 if (mMainView == null || mSavedState == null) {
1850 return;
1851 }
1852 if (mSavedState.getBoolean(USERAGENT)
1853 != mSettings.hasDesktopUseragent(mMainView)) {
1854 mSettings.toggleDesktopUseragent(mMainView);
1855 }
1856 }
1857
Leon Scroggins1961ed22010-12-07 15:22:21 -05001858 public void updateBookmarkedStatus() {
John Recke969cc52010-12-21 17:24:43 -08001859 mDataController.queryBookmarkStatus(getUrl(), mIsBookmarkCallback);
Leon Scroggins1961ed22010-12-07 15:22:21 -05001860 }
1861
John Recke969cc52010-12-21 17:24:43 -08001862 private DataController.OnQueryUrlIsBookmark mIsBookmarkCallback
1863 = new DataController.OnQueryUrlIsBookmark() {
1864 @Override
1865 public void onQueryUrlIsBookmark(String url, boolean isBookmark) {
1866 if (mCurrentState.mUrl.equals(url)) {
1867 mCurrentState.mIsBookmarkedSite = isBookmark;
1868 mWebViewController.bookmarkedStatusHasChanged(Tab.this);
1869 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001870 }
John Recke969cc52010-12-21 17:24:43 -08001871 };
Michael Kolb1acef692011-03-08 14:12:06 -08001872
Michael Kolbeb95db42011-03-03 10:38:40 -08001873 public Bitmap getScreenshot() {
John Reck1cf4b792011-07-26 10:22:22 -07001874 synchronized (Tab.this) {
1875 return mCapture;
1876 }
Michael Kolbeb95db42011-03-03 10:38:40 -08001877 }
1878
John Reck541f55a2011-06-07 16:34:43 -07001879 public boolean isSnapshot() {
John Reck541f55a2011-06-07 16:34:43 -07001880 return false;
1881 }
1882
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001883 private static class SaveCallback implements ValueCallback<String> {
1884 boolean onReceiveValueCalled = false;
1885 private String mPath;
John Reck68234a92012-04-19 15:27:12 -07001886
1887 @Override
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001888 public void onReceiveValue(String path) {
1889 this.onReceiveValueCalled = true;
1890 this.mPath = path;
John Reck68234a92012-04-19 15:27:12 -07001891 synchronized (this) {
1892 notifyAll();
John Reck8cc92352011-07-06 17:41:52 -07001893 }
John Reck541f55a2011-06-07 16:34:43 -07001894 }
John Reck68234a92012-04-19 15:27:12 -07001895
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001896 public String getPath() {
1897 return mPath;
1898 }
John Reck68234a92012-04-19 15:27:12 -07001899 }
1900
1901 /**
1902 * Must be called on the UI thread
1903 */
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001904 public ContentValues createSnapshotValues(Bitmap bm) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001905 WebView web = getWebView();
John Reck68234a92012-04-19 15:27:12 -07001906 if (web == null) return null;
John Reckd8c74522011-06-14 08:45:00 -07001907 ContentValues values = new ContentValues();
1908 values.put(Snapshots.TITLE, mCurrentState.mTitle);
1909 values.put(Snapshots.URL, mCurrentState.mUrl);
John Reck68234a92012-04-19 15:27:12 -07001910 values.put(Snapshots.BACKGROUND, web.getPageBackgroundColor());
John Reck8cc92352011-07-06 17:41:52 -07001911 values.put(Snapshots.DATE_CREATED, System.currentTimeMillis());
1912 values.put(Snapshots.FAVICON, compressBitmap(getFavicon()));
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001913 values.put(Snapshots.THUMBNAIL, compressBitmap(bm));
John Reckd8c74522011-06-14 08:45:00 -07001914 return values;
John Reck541f55a2011-06-07 16:34:43 -07001915 }
1916
John Reck68234a92012-04-19 15:27:12 -07001917 /**
1918 * Probably want to call this on a background thread
1919 */
1920 public boolean saveViewState(ContentValues values) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001921 WebView web = getWebView();
John Reck68234a92012-04-19 15:27:12 -07001922 if (web == null) return false;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001923 String filename = UUID.randomUUID().toString();
John Reck68234a92012-04-19 15:27:12 -07001924 SaveCallback callback = new SaveCallback();
John Reck68234a92012-04-19 15:27:12 -07001925 try {
John Reck68234a92012-04-19 15:27:12 -07001926 synchronized (callback) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001927 web.saveViewState(filename, callback);
1928 callback.wait();
John Reck68234a92012-04-19 15:27:12 -07001929 }
John Reck68234a92012-04-19 15:27:12 -07001930 } catch (Exception e) {
1931 Log.w(LOGTAG, "Failed to save view state", e);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001932 String path = callback.getPath();
1933 if (path != null) {
1934 File file = mContext.getFileStreamPath(path);
1935 if (file.exists() && !file.delete()) {
1936 file.deleteOnExit();
1937 }
John Reck68234a92012-04-19 15:27:12 -07001938 }
1939 return false;
1940 }
Tarun Nainani8eb00912014-07-17 12:28:32 -07001941
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001942 String path = callback.getPath();
Tarun Nainani8eb00912014-07-17 12:28:32 -07001943 // could be that saving of file failed
1944 if (path == null) {
1945 return false;
1946 }
1947
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001948 File savedFile = new File(path);
1949 if (!savedFile.exists()) {
1950 return false;
John Reck68234a92012-04-19 15:27:12 -07001951 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001952 values.put(Snapshots.VIEWSTATE_PATH, path.substring(path.lastIndexOf('/') + 1));
1953 values.put(Snapshots.VIEWSTATE_SIZE, savedFile.length());
John Reck68234a92012-04-19 15:27:12 -07001954 return true;
1955 }
1956
John Reck8cc92352011-07-06 17:41:52 -07001957 public byte[] compressBitmap(Bitmap bitmap) {
1958 if (bitmap == null) {
1959 return null;
1960 }
1961 ByteArrayOutputStream stream = new ByteArrayOutputStream();
1962 bitmap.compress(CompressFormat.PNG, 100, stream);
1963 return stream.toByteArray();
1964 }
1965
John Reck26b18322011-06-21 13:08:58 -07001966 public void loadUrl(String url, Map<String, String> headers) {
1967 if (mMainView != null) {
Michael Kolba53c9892011-10-05 13:31:40 -07001968 mPageLoadProgress = INITIAL_PROGRESS;
Michael Kolb14612442011-06-24 13:06:29 -07001969 mCurrentState = new PageState(mContext, false, url, null);
John Reck26b18322011-06-21 13:08:58 -07001970 mMainView.loadUrl(url, headers);
1971 }
1972 }
1973
John Reck38b39652012-06-05 09:22:59 -07001974 public void disableUrlOverridingForLoad() {
1975 mDisableOverrideUrlLoading = true;
1976 }
1977
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001978 private void thumbnailUpdated() {
John Reck8ee633f2011-08-09 16:00:35 -07001979 mHandler.removeMessages(MSG_CAPTURE);
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001980
John Reck8ee633f2011-08-09 16:00:35 -07001981 TabControl tc = mWebViewController.getTabControl();
1982 if (tc != null) {
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001983 OnThumbnailUpdatedListener updateListener = tc.getOnThumbnailUpdatedListener();
John Reck8ee633f2011-08-09 16:00:35 -07001984 if (updateListener != null) {
1985 updateListener.onThumbnailUpdated(this);
1986 }
1987 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001988 }
1989
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001990 protected void capture() {
1991 if (mMainView == null || mCapture == null || !mMainView.isReady() ||
1992 mMainView.getContentWidth() <= 0 || mMainView.getContentHeight() <= 0 ||
1993 !mFirstVisualPixelPainted || mMainView.isShowingCrashView()) {
1994
1995 initCaptureBitmap();
1996 thumbnailUpdated();
1997 return;
1998 }
1999
2000 mMainView.getContentBitmapAsync((float) mCaptureWidth / mMainView.getWidth(), new Rect(),
2001 new ValueCallback<Bitmap>() {
2002 @Override
2003 public void onReceiveValue(Bitmap bitmap) {
2004 if (mCapture == null) {
2005 initCaptureBitmap();
2006 }
2007
2008 if (bitmap == null) {
2009 thumbnailUpdated();
2010 return;
2011 }
2012
2013 Canvas c = new Canvas(mCapture);
2014 mCapture.eraseColor(Color.WHITE);
2015 c.drawBitmap(bitmap, 0, 0, null);
2016
2017 // manually anti-alias the edges for the tilt
2018 c.drawRect(0, 0, 1, mCapture.getHeight(), sAlphaPaint);
2019 c.drawRect(mCapture.getWidth() - 1, 0, mCapture.getWidth(),
2020 mCapture.getHeight(), sAlphaPaint);
2021 c.drawRect(0, 0, mCapture.getWidth(), 1, sAlphaPaint);
2022 c.drawRect(0, mCapture.getHeight() - 1, mCapture.getWidth(),
2023 mCapture.getHeight(), sAlphaPaint);
2024 c.setBitmap(null);
2025
2026 persistThumbnail();
2027 thumbnailUpdated();
2028 }
2029 }
2030 );
John Reck1cf4b792011-07-26 10:22:22 -07002031 }
2032
Pankaj Garg6bedeba2015-06-23 15:47:37 -07002033 @Override
2034 public void onNewPicture(WebView view, Picture picture) {
Michael Kolb9ef259a2011-07-12 15:33:08 -07002035 }
2036
John Reckef654f12011-07-12 16:42:08 -07002037 public boolean canGoBack() {
2038 return mMainView != null ? mMainView.canGoBack() : false;
2039 }
2040
2041 public boolean canGoForward() {
2042 return mMainView != null ? mMainView.canGoForward() : false;
2043 }
2044
2045 public void goBack() {
2046 if (mMainView != null) {
2047 mMainView.goBack();
2048 }
2049 }
2050
2051 public void goForward() {
2052 if (mMainView != null) {
2053 mMainView.goForward();
2054 }
2055 }
2056
John Reck1cf4b792011-07-26 10:22:22 -07002057 protected void persistThumbnail() {
John Reck4eadc342011-10-31 14:04:10 -07002058 DataController.getInstance(mContext).saveThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07002059 }
2060
2061 protected void deleteThumbnail() {
John Reck4eadc342011-10-31 14:04:10 -07002062 DataController.getInstance(mContext).deleteThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07002063 }
2064
John Reck4eadc342011-10-31 14:04:10 -07002065 void updateCaptureFromBlob(byte[] blob) {
John Reck1cf4b792011-07-26 10:22:22 -07002066 synchronized (Tab.this) {
2067 if (mCapture == null) {
2068 return;
2069 }
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002070 ByteBuffer buffer = ByteBuffer.wrap(blob);
Michael Kolbd837a112011-08-09 14:00:18 -07002071 try {
2072 mCapture.copyPixelsFromBuffer(buffer);
2073 } catch (RuntimeException rex) {
2074 Log.e(LOGTAG, "Load capture has mismatched sizes; buffer: "
2075 + buffer.capacity() + " blob: " + blob.length
2076 + "capture: " + mCapture.getByteCount());
2077 throw rex;
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002078 }
John Reck1cf4b792011-07-26 10:22:22 -07002079 }
2080 }
2081
John Reck52be4782011-08-26 15:37:29 -07002082 @Override
2083 public String toString() {
2084 StringBuilder builder = new StringBuilder(100);
2085 builder.append(mId);
2086 builder.append(") has parent: ");
2087 if (getParent() != null) {
2088 builder.append("true[");
2089 builder.append(getParent().getId());
2090 builder.append("]");
2091 } else {
2092 builder.append("false");
2093 }
2094 builder.append(", incog: ");
2095 builder.append(isPrivateBrowsingEnabled());
2096 if (!isPrivateBrowsingEnabled()) {
2097 builder.append(", title: ");
2098 builder.append(getTitle());
2099 builder.append(", url: ");
2100 builder.append(getUrl());
2101 }
2102 return builder.toString();
2103 }
2104
Steve Block4895b012011-10-03 16:26:46 +01002105 private void handleProceededAfterSslError(SslError error) {
2106 if (error.getUrl().equals(mCurrentState.mUrl)) {
2107 // The security state should currently be SECURITY_STATE_SECURE.
2108 setSecurityState(SecurityState.SECURITY_STATE_BAD_CERTIFICATE);
Steve Block08a6f0c2011-10-06 12:12:53 +01002109 mCurrentState.mSslCertificateError = error;
Steve Block4895b012011-10-03 16:26:46 +01002110 } else if (getSecurityState() == SecurityState.SECURITY_STATE_SECURE) {
Steve Block08a6f0c2011-10-06 12:12:53 +01002111 // The page's main resource is secure and this error is for a
2112 // sub-resource.
Steve Block4895b012011-10-03 16:26:46 +01002113 setSecurityState(SecurityState.SECURITY_STATE_MIXED);
2114 }
2115 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002116}