blob: 64be28918adc5d4412b70fdc33884e44424dc883 [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
John Reck1cf4b792011-07-26 10:22:22 -07001231 mCaptureWidth = mContext.getResources().getDimensionPixelSize(
1232 R.dimen.tab_thumbnail_width);
1233 mCaptureHeight = mContext.getResources().getDimensionPixelSize(
1234 R.dimen.tab_thumbnail_height);
1235 updateShouldCaptureThumbnails();
1236 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
Michael Kolb72864272012-05-03 15:42:15 -07001257 public boolean shouldUpdateThumbnail() {
1258 return mUpdateThumbnail;
1259 }
1260
Mathew Inwoode09305e2011-09-02 12:03:26 +01001261 /**
1262 * This is used to get a new ID when the tab has been preloaded, before it is displayed and
1263 * added to TabControl. Preloaded tabs can be created before restoreInstanceState, leading
1264 * to overlapping IDs between the preloaded and restored tabs.
1265 */
1266 public void refreshIdAfterPreload() {
1267 mId = TabControl.getNextId();
1268 }
1269
John Reck1cf4b792011-07-26 10:22:22 -07001270 public void updateShouldCaptureThumbnails() {
1271 if (mWebViewController.shouldCaptureThumbnails()) {
1272 synchronized (Tab.this) {
1273 if (mCapture == null) {
1274 mCapture = Bitmap.createBitmap(mCaptureWidth, mCaptureHeight,
1275 Bitmap.Config.RGB_565);
Michael Kolbc3af0672011-08-09 10:24:41 -07001276 mCapture.eraseColor(Color.WHITE);
John Reck1cf4b792011-07-26 10:22:22 -07001277 if (mInForeground) {
1278 postCapture();
1279 }
1280 }
1281 }
1282 } else {
1283 synchronized (Tab.this) {
1284 mCapture = null;
1285 deleteThumbnail();
1286 }
1287 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001288 }
1289
Michael Kolb14612442011-06-24 13:06:29 -07001290 public void setController(WebViewController ctl) {
1291 mWebViewController = ctl;
John Reck1cf4b792011-07-26 10:22:22 -07001292 updateShouldCaptureThumbnails();
Michael Kolb14612442011-06-24 13:06:29 -07001293 }
1294
Michael Kolbc831b632011-05-11 09:30:34 -07001295 public long getId() {
1296 return mId;
1297 }
1298
Michael Kolb91911a22012-01-17 11:21:25 -08001299 void setWebView(WebView w) {
1300 setWebView(w, true);
1301 }
1302
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001303 public boolean isNativeActive(){
1304 if (mMainView == null)
1305 return false;
1306 return true;
1307 }
1308
1309 public void setTimeStamp(){
1310 Date d = new Date();
1311 timestamp = (new Timestamp(d.getTime()));
1312 }
1313
1314 public Timestamp getTimestamp() {
1315 return timestamp;
1316 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001317 /**
1318 * Sets the WebView for this tab, correctly removing the old WebView from
1319 * the container view.
1320 */
Michael Kolb91911a22012-01-17 11:21:25 -08001321 void setWebView(WebView w, boolean restore) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001322 if (mMainView == w) {
1323 return;
1324 }
Michael Kolba713ec82010-11-29 17:27:06 -08001325
Michael Kolba713ec82010-11-29 17:27:06 -08001326 mWebViewController.onSetWebView(this, w);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001327
John Reck1cf4b792011-07-26 10:22:22 -07001328 if (mMainView != null) {
John Reck8ee633f2011-08-09 16:00:35 -07001329 mMainView.setPictureListener(null);
John Reck1cf4b792011-07-26 10:22:22 -07001330 if (w != null) {
1331 syncCurrentState(w, null);
1332 } else {
Panos Thomasa9a5a582014-03-18 19:20:08 -07001333 mCurrentState = new PageState(mContext, mMainView.isPrivateBrowsingEnabled());
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001334
1335 if (mWebViewDestroyedByMemoryMonitor) {
1336 /*
1337 * If tab was destroyed as a result of the MemoryMonitor
1338 * then we need to restore the state properties
1339 * from the old WebView (mMainView)
1340 */
1341 syncCurrentState(mMainView, null);
1342 mWebViewDestroyedByMemoryMonitor = false;
1343 }
John Reck1cf4b792011-07-26 10:22:22 -07001344 }
1345 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001346 // set the new one
1347 mMainView = w;
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001348
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001349 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001350 if (mMainView != null) {
1351 mMainView.setWebViewClient(mWebViewClient);
1352 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001353 // Attach DownloadManager so that downloads can start in an active
1354 // or a non-active window. This can happen when going to a site that
1355 // does a redirect after a period of time. The user could have
1356 // switched to another tab while waiting for the download to start.
1357 mMainView.setDownloadListener(mDownloadListener);
John Reck8ee633f2011-08-09 16:00:35 -07001358 TabControl tc = mWebViewController.getTabControl();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001359 if (tc != null /*&& tc.getOnThumbnailUpdatedListener() != null*/) {
John Reck8ee633f2011-08-09 16:00:35 -07001360 mMainView.setPictureListener(this);
1361 }
Michael Kolb91911a22012-01-17 11:21:25 -08001362 if (restore && (mSavedState != null)) {
John Reck8b9bb8b2012-03-08 13:19:40 -08001363 restoreUserAgent();
John Reck6c2e2f32011-08-22 13:41:23 -07001364 WebBackForwardList restoredState
1365 = mMainView.restoreState(mSavedState);
1366 if (restoredState == null || restoredState.getSize() == 0) {
1367 Log.w(LOGTAG, "Failed to restore WebView state!");
1368 loadUrl(mCurrentState.mOriginalUrl, null);
1369 }
John Reck1cf4b792011-07-26 10:22:22 -07001370 mSavedState = null;
1371 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001372 }
1373 }
1374
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001375 public void destroyThroughMemoryMonitor() {
1376 mWebViewDestroyedByMemoryMonitor = true;
1377 destroy();
1378 }
1379
Grace Kloba22ac16e2009-10-07 18:00:23 -07001380 /**
1381 * Destroy the tab's main WebView and subWindow if any
1382 */
1383 void destroy() {
Tarun Nainani2c1dd7c2014-07-05 16:40:12 -07001384
Matthew Huib7f2e9c2014-04-16 11:12:37 -04001385 if (mPostponeDestroy) {
1386 mShouldDestroy = true;
1387 return;
1388 }
1389 mShouldDestroy = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001390 if (mMainView != null) {
1391 dismissSubWindow();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001392 // save the WebView to call destroy() after detach it from the tab
Vivek Sekhar53ef8932015-06-18 16:51:43 -07001393 final WebView webView = mMainView;
Pankaj Garg18aa0a12015-06-22 11:06:12 -07001394 if (!mWebViewDestroyedByMemoryMonitor &&
1395 !BrowserCommandLine.hasSwitch("ui-low-power-mode")) {
Vivek Sekhar53ef8932015-06-18 16:51:43 -07001396 webView.getSnapshotIds(new ValueCallback<List<Integer>>() {
1397 @Override
1398 public void onReceiveValue(List<Integer> ids) {
1399 int currentTabIdx = mWebViewController.getTabControl().getCurrentPosition();
1400 for (Integer id : ids) {
1401 if (getTabIdxFromCaptureIdx(id) == currentTabIdx) {
1402 webView.deleteSnapshot(id);
1403 }
1404 }
1405 setWebView(null);
1406 webView.destroy();
Pankaj Garg1c13cab2015-05-12 11:52:17 -07001407 }
Vivek Sekhar53ef8932015-06-18 16:51:43 -07001408 });
1409 return;
Pankaj Garg1c13cab2015-05-12 11:52:17 -07001410 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001411 setWebView(null);
1412 webView.destroy();
1413 }
1414 }
1415
Matthew Huib7f2e9c2014-04-16 11:12:37 -04001416 private boolean mPostponeDestroy = false;
1417 private boolean mShouldDestroy = false;
1418
1419 public void postponeDestroy() {
1420 mPostponeDestroy = true;
1421 }
1422
1423 public void performPostponedDestroy() {
1424 mPostponeDestroy = false;
1425 if (mShouldDestroy) {
1426 destroy();
1427 }
1428 }
1429
Grace Kloba22ac16e2009-10-07 18:00:23 -07001430 /**
1431 * Remove the tab from the parent
1432 */
1433 void removeFromTree() {
1434 // detach the children
Michael Kolbc831b632011-05-11 09:30:34 -07001435 if (mChildren != null) {
1436 for(Tab t : mChildren) {
1437 t.setParent(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001438 }
1439 }
1440 // remove itself from the parent list
Michael Kolbc831b632011-05-11 09:30:34 -07001441 if (mParent != null) {
1442 mParent.mChildren.remove(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001443 }
Tarun Nainani8eb00912014-07-17 12:28:32 -07001444
1445 mCapture = null;
John Reck1cf4b792011-07-26 10:22:22 -07001446 deleteThumbnail();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001447 }
1448
1449 /**
1450 * Create a new subwindow unless a subwindow already exists.
1451 * @return True if a new subwindow was created. False if one already exists.
1452 */
1453 boolean createSubWindow() {
1454 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001455 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001456 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001457 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001458 mSubView.setWebChromeClient(new SubWindowChromeClient(
1459 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001460 // Set a different DownloadListener for the mSubView, since it will
1461 // just need to dismiss the mSubView, rather than close the Tab
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001462 mSubView.setDownloadListener(new BrowserDownloadListener() {
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001463 public void onDownloadStart(String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001464 String contentDisposition, String mimetype, String referer,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001465 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001466 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001467 contentDisposition, mimetype, referer, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001468 if (mSubView.copyBackForwardList().getSize() == 0) {
1469 // This subwindow was opened for the sole purpose of
1470 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001471 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001472 }
1473 }
1474 });
Michael Kolb14612442011-06-24 13:06:29 -07001475 mSubView.setOnCreateContextMenuListener(mWebViewController.getActivity());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001476 return true;
1477 }
1478 return false;
1479 }
1480
1481 /**
1482 * Dismiss the subWindow for the tab.
1483 */
1484 void dismissSubWindow() {
1485 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001486 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001487 mSubView.destroy();
1488 mSubView = null;
1489 mSubViewContainer = null;
1490 }
1491 }
1492
Grace Kloba22ac16e2009-10-07 18:00:23 -07001493
1494 /**
1495 * Set the parent tab of this tab.
1496 */
Michael Kolbc831b632011-05-11 09:30:34 -07001497 void setParent(Tab parent) {
John Reck52be4782011-08-26 15:37:29 -07001498 if (parent == this) {
1499 throw new IllegalStateException("Cannot set parent to self!");
1500 }
Michael Kolbc831b632011-05-11 09:30:34 -07001501 mParent = parent;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001502 // This tab may have been freed due to low memory. If that is the case,
Michael Kolbc831b632011-05-11 09:30:34 -07001503 // the parent tab id is already saved. If we are changing that id
Grace Kloba22ac16e2009-10-07 18:00:23 -07001504 // (most likely due to removing the parent tab) we must update the
Michael Kolbc831b632011-05-11 09:30:34 -07001505 // parent tab id in the saved Bundle.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001506 if (mSavedState != null) {
1507 if (parent == null) {
1508 mSavedState.remove(PARENTTAB);
1509 } else {
Michael Kolbc831b632011-05-11 09:30:34 -07001510 mSavedState.putLong(PARENTTAB, parent.getId());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001511 }
1512 }
John Reckb0a86db2011-05-24 14:05:58 -07001513
1514 // Sync the WebView useragent with the parent
1515 if (parent != null && mSettings.hasDesktopUseragent(parent.getWebView())
1516 != mSettings.hasDesktopUseragent(getWebView())) {
1517 mSettings.toggleDesktopUseragent(getWebView());
1518 }
John Reck52be4782011-08-26 15:37:29 -07001519
1520 if (parent != null && parent.getId() == getId()) {
1521 throw new IllegalStateException("Parent has same ID as child!");
1522 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001523 }
1524
1525 /**
Michael Kolbc831b632011-05-11 09:30:34 -07001526 * If this Tab was created through another Tab, then this method returns
1527 * that Tab.
1528 * @return the Tab parent or null
1529 */
1530 public Tab getParent() {
1531 return mParent;
1532 }
1533
1534 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001535 * When a Tab is created through the content of another Tab, then we
1536 * associate the Tabs.
1537 * @param child the Tab that was created from this Tab
1538 */
1539 void addChildTab(Tab child) {
Michael Kolbc831b632011-05-11 09:30:34 -07001540 if (mChildren == null) {
1541 mChildren = new Vector<Tab>();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001542 }
Michael Kolbc831b632011-05-11 09:30:34 -07001543 mChildren.add(child);
1544 child.setParent(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001545 }
1546
Michael Kolbc831b632011-05-11 09:30:34 -07001547 Vector<Tab> getChildren() {
1548 return mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001549 }
1550
1551 void resume() {
1552 if (mMainView != null) {
John Reck56c1fcf2011-08-17 10:15:16 -07001553 setupHwAcceleration(mMainView);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001554 mMainView.onResume();
1555 if (mSubView != null) {
1556 mSubView.onResume();
1557 }
1558 }
1559 }
1560
John Reck56c1fcf2011-08-17 10:15:16 -07001561 private void setupHwAcceleration(View web) {
1562 if (web == null) return;
1563 BrowserSettings settings = BrowserSettings.getInstance();
1564 if (settings.isHardwareAccelerated()) {
1565 web.setLayerType(View.LAYER_TYPE_NONE, null);
1566 } else {
1567 web.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
1568 }
1569 }
1570
Grace Kloba22ac16e2009-10-07 18:00:23 -07001571 void pause() {
1572 if (mMainView != null) {
1573 mMainView.onPause();
1574 if (mSubView != null) {
1575 mSubView.onPause();
1576 }
1577 }
1578 }
1579
1580 void putInForeground() {
John Reck8ee633f2011-08-09 16:00:35 -07001581 if (mInForeground) {
1582 return;
1583 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001584 mInForeground = true;
1585 resume();
Michael Kolb14612442011-06-24 13:06:29 -07001586 Activity activity = mWebViewController.getActivity();
1587 mMainView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001588 if (mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -07001589 mSubView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001590 }
Axesh R. Ajmerac6b5c322015-05-01 11:06:10 -07001591
Leon Scroggins1961ed22010-12-07 15:22:21 -05001592 mWebViewController.bookmarkedStatusHasChanged(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001593 }
1594
1595 void putInBackground() {
John Reck8ee633f2011-08-09 16:00:35 -07001596 if (!mInForeground) {
1597 return;
1598 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001599 mInForeground = false;
1600 pause();
1601 mMainView.setOnCreateContextMenuListener(null);
1602 if (mSubView != null) {
1603 mSubView.setOnCreateContextMenuListener(null);
1604 }
1605 }
1606
Michael Kolb8233fac2010-10-26 16:08:53 -07001607 boolean inForeground() {
1608 return mInForeground;
1609 }
1610
Grace Kloba22ac16e2009-10-07 18:00:23 -07001611 /**
1612 * Return the top window of this tab; either the subwindow if it is not
1613 * null or the main window.
1614 * @return The top window of this tab.
1615 */
1616 WebView getTopWindow() {
1617 if (mSubView != null) {
1618 return mSubView;
1619 }
1620 return mMainView;
1621 }
1622
1623 /**
1624 * Return the main window of this tab. Note: if a tab is freed in the
1625 * background, this can return null. It is only guaranteed to be
1626 * non-null for the current tab.
1627 * @return The main WebView of this tab.
1628 */
1629 WebView getWebView() {
1630 return mMainView;
1631 }
1632
Michael Kolba713ec82010-11-29 17:27:06 -08001633 void setViewContainer(View container) {
1634 mContainer = container;
1635 }
1636
Michael Kolb8233fac2010-10-26 16:08:53 -07001637 View getViewContainer() {
1638 return mContainer;
1639 }
1640
Grace Kloba22ac16e2009-10-07 18:00:23 -07001641 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001642 * Return whether private browsing is enabled for the main window of
1643 * this tab.
1644 * @return True if private browsing is enabled.
1645 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001646 boolean isPrivateBrowsingEnabled() {
John Reck502a3532011-08-16 14:21:46 -07001647 return mCurrentState.mIncognito;
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001648 }
1649
1650 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001651 * Return the subwindow of this tab or null if there is no subwindow.
1652 * @return The subwindow of this tab or null.
1653 */
1654 WebView getSubWebView() {
1655 return mSubView;
1656 }
1657
Michael Kolb1514bb72010-11-22 09:11:48 -08001658 void setSubWebView(WebView subView) {
1659 mSubView = subView;
1660 }
1661
Michael Kolb8233fac2010-10-26 16:08:53 -07001662 View getSubViewContainer() {
1663 return mSubViewContainer;
1664 }
1665
Michael Kolb1514bb72010-11-22 09:11:48 -08001666 void setSubViewContainer(View subViewContainer) {
1667 mSubViewContainer = subViewContainer;
1668 }
1669
Grace Kloba22ac16e2009-10-07 18:00:23 -07001670
1671 /**
1672 * @return The application id string
1673 */
1674 String getAppId() {
1675 return mAppId;
1676 }
1677
1678 /**
1679 * Set the application id string
1680 * @param id
1681 */
1682 void setAppId(String id) {
1683 mAppId = id;
1684 }
1685
Michael Kolbe28b3472011-08-04 16:54:31 -07001686 boolean closeOnBack() {
1687 return mCloseOnBack;
1688 }
1689
1690 void setCloseOnBack(boolean close) {
1691 mCloseOnBack = close;
1692 }
1693
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001694 boolean getDerivedFromIntent() {
1695 return mDerivedFromIntent;
1696 }
1697
1698 void setDerivedFromIntent(boolean derived) {
1699 mDerivedFromIntent = derived;
1700 }
1701
Grace Kloba22ac16e2009-10-07 18:00:23 -07001702 String getUrl() {
John Reck324d4402011-01-11 16:56:42 -08001703 return UrlUtils.filteredUrl(mCurrentState.mUrl);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001704 }
1705
Tarun Nainani8eb00912014-07-17 12:28:32 -07001706
1707 protected void onPageFinished() {
1708 mPageFinished = true;
1709 }
1710
1711 public boolean getPageFinishedStatus() {
1712 return mPageFinished;
1713 }
1714
John Reck49a603c2011-03-03 09:33:05 -08001715 String getOriginalUrl() {
John Reckdb22ec42011-06-29 11:31:24 -07001716 if (mCurrentState.mOriginalUrl == null) {
1717 return getUrl();
John Reck49a603c2011-03-03 09:33:05 -08001718 }
John Reckdb22ec42011-06-29 11:31:24 -07001719 return UrlUtils.filteredUrl(mCurrentState.mOriginalUrl);
John Reck49a603c2011-03-03 09:33:05 -08001720 }
1721
Grace Kloba22ac16e2009-10-07 18:00:23 -07001722 /**
John Reck30c714c2010-12-16 17:30:34 -08001723 * Get the title of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001724 */
1725 String getTitle() {
John Reck30c714c2010-12-16 17:30:34 -08001726 return mCurrentState.mTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001727 }
1728
1729 /**
John Reck30c714c2010-12-16 17:30:34 -08001730 * Get the favicon of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001731 */
1732 Bitmap getFavicon() {
John Reck1cf4b792011-07-26 10:22:22 -07001733 if (mCurrentState.mFavicon != null) {
1734 return mCurrentState.mFavicon;
1735 }
1736 return getDefaultFavicon(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001737 }
1738
Pankaj Garg32e1b942015-06-03 18:13:24 -07001739 public boolean hasFavicon() {
1740 return mCurrentState.mFavicon != null;
1741 }
1742
John Recke969cc52010-12-21 17:24:43 -08001743 public boolean isBookmarkedSite() {
1744 return mCurrentState.mIsBookmarkedSite;
1745 }
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001746
Grace Kloba22ac16e2009-10-07 18:00:23 -07001747 /**
Steve Block08a6f0c2011-10-06 12:12:53 +01001748 * Sets the security state, clears the SSL certificate error and informs
1749 * the controller.
1750 */
Steve Block2466eff2011-10-03 15:33:09 +01001751 private void setSecurityState(SecurityState securityState) {
1752 mCurrentState.mSecurityState = securityState;
Steve Block08a6f0c2011-10-06 12:12:53 +01001753 mCurrentState.mSslCertificateError = null;
Steve Block2466eff2011-10-03 15:33:09 +01001754 mWebViewController.onUpdatedSecurityState(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001755 }
1756
1757 /**
Steve Block2466eff2011-10-03 15:33:09 +01001758 * @return The tab's security state.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001759 */
Steve Block2466eff2011-10-03 15:33:09 +01001760 SecurityState getSecurityState() {
1761 return mCurrentState.mSecurityState;
John Reck30c714c2010-12-16 17:30:34 -08001762 }
1763
Steve Block08a6f0c2011-10-06 12:12:53 +01001764 /**
1765 * Gets the SSL certificate error, if any, for the page's main resource.
1766 * This is only non-null when the security state is
1767 * SECURITY_STATE_BAD_CERTIFICATE.
1768 */
1769 SslError getSslCertificateError() {
1770 return mCurrentState.mSslCertificateError;
1771 }
1772
John Reck30c714c2010-12-16 17:30:34 -08001773 int getLoadProgress() {
1774 if (mInPageLoad) {
1775 return mPageLoadProgress;
1776 }
1777 return 100;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001778 }
1779
1780 /**
1781 * @return TRUE if onPageStarted is called while onPageFinished is not
1782 * called yet.
1783 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001784 boolean inPageLoad() {
1785 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001786 }
1787
Grace Kloba22ac16e2009-10-07 18:00:23 -07001788 /**
John Reck1cf4b792011-07-26 10:22:22 -07001789 * @return The Bundle with the tab's state if it can be saved, otherwise null
Grace Kloba22ac16e2009-10-07 18:00:23 -07001790 */
John Reck1cf4b792011-07-26 10:22:22 -07001791 public Bundle saveState() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001792 // If the WebView is null it means we ran low on memory and we already
1793 // stored the saved state in mSavedState.
1794 if (mMainView == null) {
John Reck1cf4b792011-07-26 10:22:22 -07001795 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001796 }
John Reck6c2e2f32011-08-22 13:41:23 -07001797
1798 if (TextUtils.isEmpty(mCurrentState.mUrl)) {
John Reck1cf4b792011-07-26 10:22:22 -07001799 return null;
John Reck24f18262011-06-17 14:47:20 -07001800 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001801
1802 mSavedState = new Bundle();
John Reck6c2e2f32011-08-22 13:41:23 -07001803 WebBackForwardList savedList = mMainView.saveState(mSavedState);
1804 if (savedList == null || savedList.getSize() == 0) {
1805 Log.w(LOGTAG, "Failed to save back/forward list for "
1806 + mCurrentState.mUrl);
1807 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001808
Michael Kolbc831b632011-05-11 09:30:34 -07001809 mSavedState.putLong(ID, mId);
John Reck30c714c2010-12-16 17:30:34 -08001810 mSavedState.putString(CURRURL, mCurrentState.mUrl);
1811 mSavedState.putString(CURRTITLE, mCurrentState.mTitle);
John Reck1cf4b792011-07-26 10:22:22 -07001812 mSavedState.putBoolean(INCOGNITO, mMainView.isPrivateBrowsingEnabled());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001813 if (mAppId != null) {
1814 mSavedState.putString(APPID, mAppId);
1815 }
Michael Kolbe28b3472011-08-04 16:54:31 -07001816 mSavedState.putBoolean(CLOSEFLAG, mCloseOnBack);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001817 // Remember the parent tab so the relationship can be restored.
Michael Kolbc831b632011-05-11 09:30:34 -07001818 if (mParent != null) {
1819 mSavedState.putLong(PARENTTAB, mParent.mId);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001820 }
John Reckb0a86db2011-05-24 14:05:58 -07001821 mSavedState.putBoolean(USERAGENT,
1822 mSettings.hasDesktopUseragent(getWebView()));
John Reck1cf4b792011-07-26 10:22:22 -07001823 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001824 }
1825
1826 /*
1827 * Restore the state of the tab.
1828 */
John Reck1cf4b792011-07-26 10:22:22 -07001829 private void restoreState(Bundle b) {
1830 mSavedState = b;
1831 if (mSavedState == null) {
1832 return;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001833 }
1834 // Restore the internal state even if the WebView fails to restore.
1835 // This will maintain the app id, original url and close-on-exit values.
Michael Kolbc831b632011-05-11 09:30:34 -07001836 mId = b.getLong(ID);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001837 mAppId = b.getString(APPID);
Michael Kolbe28b3472011-08-04 16:54:31 -07001838 mCloseOnBack = b.getBoolean(CLOSEFLAG);
John Reck8b9bb8b2012-03-08 13:19:40 -08001839 restoreUserAgent();
John Reck1cf4b792011-07-26 10:22:22 -07001840 String url = b.getString(CURRURL);
1841 String title = b.getString(CURRTITLE);
1842 boolean incognito = b.getBoolean(INCOGNITO);
1843 mCurrentState = new PageState(mContext, incognito, url, null);
1844 mCurrentState.mTitle = title;
1845 synchronized (Tab.this) {
1846 if (mCapture != null) {
John Reck4eadc342011-10-31 14:04:10 -07001847 DataController.getInstance(mContext).loadThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07001848 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001849 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001850 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04001851
John Reck8b9bb8b2012-03-08 13:19:40 -08001852 private void restoreUserAgent() {
1853 if (mMainView == null || mSavedState == null) {
1854 return;
1855 }
1856 if (mSavedState.getBoolean(USERAGENT)
1857 != mSettings.hasDesktopUseragent(mMainView)) {
1858 mSettings.toggleDesktopUseragent(mMainView);
1859 }
1860 }
1861
Leon Scroggins1961ed22010-12-07 15:22:21 -05001862 public void updateBookmarkedStatus() {
John Recke969cc52010-12-21 17:24:43 -08001863 mDataController.queryBookmarkStatus(getUrl(), mIsBookmarkCallback);
Leon Scroggins1961ed22010-12-07 15:22:21 -05001864 }
1865
John Recke969cc52010-12-21 17:24:43 -08001866 private DataController.OnQueryUrlIsBookmark mIsBookmarkCallback
1867 = new DataController.OnQueryUrlIsBookmark() {
1868 @Override
1869 public void onQueryUrlIsBookmark(String url, boolean isBookmark) {
1870 if (mCurrentState.mUrl.equals(url)) {
1871 mCurrentState.mIsBookmarkedSite = isBookmark;
1872 mWebViewController.bookmarkedStatusHasChanged(Tab.this);
1873 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001874 }
John Recke969cc52010-12-21 17:24:43 -08001875 };
Michael Kolb1acef692011-03-08 14:12:06 -08001876
Michael Kolbeb95db42011-03-03 10:38:40 -08001877 public Bitmap getScreenshot() {
John Reck1cf4b792011-07-26 10:22:22 -07001878 synchronized (Tab.this) {
1879 return mCapture;
1880 }
Michael Kolbeb95db42011-03-03 10:38:40 -08001881 }
1882
John Reck541f55a2011-06-07 16:34:43 -07001883 public boolean isSnapshot() {
John Reck541f55a2011-06-07 16:34:43 -07001884 return false;
1885 }
1886
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001887 private static class SaveCallback implements ValueCallback<String> {
1888 boolean onReceiveValueCalled = false;
1889 private String mPath;
John Reck68234a92012-04-19 15:27:12 -07001890
1891 @Override
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001892 public void onReceiveValue(String path) {
1893 this.onReceiveValueCalled = true;
1894 this.mPath = path;
John Reck68234a92012-04-19 15:27:12 -07001895 synchronized (this) {
1896 notifyAll();
John Reck8cc92352011-07-06 17:41:52 -07001897 }
John Reck541f55a2011-06-07 16:34:43 -07001898 }
John Reck68234a92012-04-19 15:27:12 -07001899
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001900 public String getPath() {
1901 return mPath;
1902 }
John Reck68234a92012-04-19 15:27:12 -07001903 }
1904
1905 /**
1906 * Must be called on the UI thread
1907 */
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001908 public ContentValues createSnapshotValues(Bitmap bm) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001909 WebView web = getWebView();
John Reck68234a92012-04-19 15:27:12 -07001910 if (web == null) return null;
John Reckd8c74522011-06-14 08:45:00 -07001911 ContentValues values = new ContentValues();
1912 values.put(Snapshots.TITLE, mCurrentState.mTitle);
1913 values.put(Snapshots.URL, mCurrentState.mUrl);
John Reck68234a92012-04-19 15:27:12 -07001914 values.put(Snapshots.BACKGROUND, web.getPageBackgroundColor());
John Reck8cc92352011-07-06 17:41:52 -07001915 values.put(Snapshots.DATE_CREATED, System.currentTimeMillis());
1916 values.put(Snapshots.FAVICON, compressBitmap(getFavicon()));
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001917 values.put(Snapshots.THUMBNAIL, compressBitmap(bm));
John Reckd8c74522011-06-14 08:45:00 -07001918 return values;
John Reck541f55a2011-06-07 16:34:43 -07001919 }
1920
John Reck68234a92012-04-19 15:27:12 -07001921 /**
1922 * Probably want to call this on a background thread
1923 */
1924 public boolean saveViewState(ContentValues values) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001925 WebView web = getWebView();
John Reck68234a92012-04-19 15:27:12 -07001926 if (web == null) return false;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001927 String filename = UUID.randomUUID().toString();
John Reck68234a92012-04-19 15:27:12 -07001928 SaveCallback callback = new SaveCallback();
John Reck68234a92012-04-19 15:27:12 -07001929 try {
John Reck68234a92012-04-19 15:27:12 -07001930 synchronized (callback) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001931 web.saveViewState(filename, callback);
1932 callback.wait();
John Reck68234a92012-04-19 15:27:12 -07001933 }
John Reck68234a92012-04-19 15:27:12 -07001934 } catch (Exception e) {
1935 Log.w(LOGTAG, "Failed to save view state", e);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001936 String path = callback.getPath();
1937 if (path != null) {
1938 File file = mContext.getFileStreamPath(path);
1939 if (file.exists() && !file.delete()) {
1940 file.deleteOnExit();
1941 }
John Reck68234a92012-04-19 15:27:12 -07001942 }
1943 return false;
1944 }
Tarun Nainani8eb00912014-07-17 12:28:32 -07001945
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001946 String path = callback.getPath();
Tarun Nainani8eb00912014-07-17 12:28:32 -07001947 // could be that saving of file failed
1948 if (path == null) {
1949 return false;
1950 }
1951
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001952 File savedFile = new File(path);
1953 if (!savedFile.exists()) {
1954 return false;
John Reck68234a92012-04-19 15:27:12 -07001955 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001956 values.put(Snapshots.VIEWSTATE_PATH, path.substring(path.lastIndexOf('/') + 1));
1957 values.put(Snapshots.VIEWSTATE_SIZE, savedFile.length());
John Reck68234a92012-04-19 15:27:12 -07001958 return true;
1959 }
1960
John Reck8cc92352011-07-06 17:41:52 -07001961 public byte[] compressBitmap(Bitmap bitmap) {
1962 if (bitmap == null) {
1963 return null;
1964 }
1965 ByteArrayOutputStream stream = new ByteArrayOutputStream();
1966 bitmap.compress(CompressFormat.PNG, 100, stream);
1967 return stream.toByteArray();
1968 }
1969
John Reck26b18322011-06-21 13:08:58 -07001970 public void loadUrl(String url, Map<String, String> headers) {
1971 if (mMainView != null) {
Michael Kolba53c9892011-10-05 13:31:40 -07001972 mPageLoadProgress = INITIAL_PROGRESS;
Michael Kolb14612442011-06-24 13:06:29 -07001973 mCurrentState = new PageState(mContext, false, url, null);
John Reck26b18322011-06-21 13:08:58 -07001974 mMainView.loadUrl(url, headers);
1975 }
1976 }
1977
John Reck38b39652012-06-05 09:22:59 -07001978 public void disableUrlOverridingForLoad() {
1979 mDisableOverrideUrlLoading = true;
1980 }
1981
Michael Kolb9ef259a2011-07-12 15:33:08 -07001982 protected void capture() {
Vivek Sekhar6bdf6452015-05-12 17:38:45 -07001983 boolean returnEmptyCapture = false;
1984 if (mMainView == null || mCapture == null || !mMainView.isReady())
1985 returnEmptyCapture = true;
John Reck4eadc342011-10-31 14:04:10 -07001986 if (mMainView.getContentWidth() <= 0 || mMainView.getContentHeight() <= 0) {
Vivek Sekhar6bdf6452015-05-12 17:38:45 -07001987 returnEmptyCapture = true;
John Reck4eadc342011-10-31 14:04:10 -07001988 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07001989
Vivek Sekhar6bdf6452015-05-12 17:38:45 -07001990 if (returnEmptyCapture || !mFirstVisualPixelPainted || mMainView.isShowingCrashView()) {
Pankaj Garg79878492015-04-01 14:48:21 -07001991 mCapture = Bitmap.createBitmap(
1992 mCaptureWidth,
1993 mCaptureHeight,
1994 Bitmap.Config.RGB_565);
1995 mCapture.eraseColor(Color.WHITE);
1996
1997 mHandler.removeMessages(MSG_CAPTURE);
1998
1999 TabControl tc = mWebViewController.getTabControl();
2000 if (tc != null) {
2001 OnThumbnailUpdatedListener updateListener
2002 = tc.getOnThumbnailUpdatedListener();
2003 if (updateListener != null) {
2004 updateListener.onThumbnailUpdated(this);
2005 }
2006 }
2007 return;
2008 }
2009
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002010 mMainView
2011 .getContentBitmapAsync(
2012 (float) mCaptureWidth / mMainView.getWidth(),
2013 new Rect(),
2014 new ValueCallback<Bitmap>() {
2015 @Override
2016 public void onReceiveValue(Bitmap bitmap) {
2017 onCaptureCallback(bitmap);
2018 }});
2019 }
2020
2021 private void onCaptureCallback(Bitmap bitmap) {
2022 if (mCapture == null || bitmap == null)
2023 return;
2024
Michael Kolb9ef259a2011-07-12 15:33:08 -07002025 Canvas c = new Canvas(mCapture);
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002026 mCapture.eraseColor(Color.WHITE);
2027 c.drawBitmap(bitmap, 0, 0, null);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002028
Michael Kolba3194d02011-09-07 11:23:51 -07002029 // manually anti-alias the edges for the tilt
2030 c.drawRect(0, 0, 1, mCapture.getHeight(), sAlphaPaint);
2031 c.drawRect(mCapture.getWidth() - 1, 0, mCapture.getWidth(),
2032 mCapture.getHeight(), sAlphaPaint);
2033 c.drawRect(0, 0, mCapture.getWidth(), 1, sAlphaPaint);
2034 c.drawRect(0, mCapture.getHeight() - 1, mCapture.getWidth(),
2035 mCapture.getHeight(), sAlphaPaint);
Dianne Hackborn43cfe8a2011-08-02 16:59:35 -07002036 c.setBitmap(null);
John Reck8ee633f2011-08-09 16:00:35 -07002037 mHandler.removeMessages(MSG_CAPTURE);
John Reck1cf4b792011-07-26 10:22:22 -07002038 persistThumbnail();
John Reck8ee633f2011-08-09 16:00:35 -07002039 TabControl tc = mWebViewController.getTabControl();
2040 if (tc != null) {
2041 OnThumbnailUpdatedListener updateListener
2042 = tc.getOnThumbnailUpdatedListener();
2043 if (updateListener != null) {
2044 updateListener.onThumbnailUpdated(this);
2045 }
2046 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07002047 }
2048
2049 @Override
2050 public void onNewPicture(WebView view, Picture picture) {
John Reck1cf4b792011-07-26 10:22:22 -07002051 postCapture();
2052 }
2053
2054 private void postCapture() {
Michael Kolb9ef259a2011-07-12 15:33:08 -07002055 if (!mHandler.hasMessages(MSG_CAPTURE)) {
2056 mHandler.sendEmptyMessageDelayed(MSG_CAPTURE, CAPTURE_DELAY);
2057 }
2058 }
2059
John Reckef654f12011-07-12 16:42:08 -07002060 public boolean canGoBack() {
2061 return mMainView != null ? mMainView.canGoBack() : false;
2062 }
2063
2064 public boolean canGoForward() {
2065 return mMainView != null ? mMainView.canGoForward() : false;
2066 }
2067
2068 public void goBack() {
2069 if (mMainView != null) {
2070 mMainView.goBack();
2071 }
2072 }
2073
2074 public void goForward() {
2075 if (mMainView != null) {
2076 mMainView.goForward();
2077 }
2078 }
2079
John Reck1cf4b792011-07-26 10:22:22 -07002080 protected void persistThumbnail() {
John Reck4eadc342011-10-31 14:04:10 -07002081 DataController.getInstance(mContext).saveThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07002082 }
2083
2084 protected void deleteThumbnail() {
John Reck4eadc342011-10-31 14:04:10 -07002085 DataController.getInstance(mContext).deleteThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07002086 }
2087
John Reck4eadc342011-10-31 14:04:10 -07002088 void updateCaptureFromBlob(byte[] blob) {
John Reck1cf4b792011-07-26 10:22:22 -07002089 synchronized (Tab.this) {
2090 if (mCapture == null) {
2091 return;
2092 }
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002093 ByteBuffer buffer = ByteBuffer.wrap(blob);
Michael Kolbd837a112011-08-09 14:00:18 -07002094 try {
2095 mCapture.copyPixelsFromBuffer(buffer);
2096 } catch (RuntimeException rex) {
2097 Log.e(LOGTAG, "Load capture has mismatched sizes; buffer: "
2098 + buffer.capacity() + " blob: " + blob.length
2099 + "capture: " + mCapture.getByteCount());
2100 throw rex;
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002101 }
John Reck1cf4b792011-07-26 10:22:22 -07002102 }
2103 }
2104
John Reck52be4782011-08-26 15:37:29 -07002105 @Override
2106 public String toString() {
2107 StringBuilder builder = new StringBuilder(100);
2108 builder.append(mId);
2109 builder.append(") has parent: ");
2110 if (getParent() != null) {
2111 builder.append("true[");
2112 builder.append(getParent().getId());
2113 builder.append("]");
2114 } else {
2115 builder.append("false");
2116 }
2117 builder.append(", incog: ");
2118 builder.append(isPrivateBrowsingEnabled());
2119 if (!isPrivateBrowsingEnabled()) {
2120 builder.append(", title: ");
2121 builder.append(getTitle());
2122 builder.append(", url: ");
2123 builder.append(getUrl());
2124 }
2125 return builder.toString();
2126 }
2127
Steve Block4895b012011-10-03 16:26:46 +01002128 private void handleProceededAfterSslError(SslError error) {
2129 if (error.getUrl().equals(mCurrentState.mUrl)) {
2130 // The security state should currently be SECURITY_STATE_SECURE.
2131 setSecurityState(SecurityState.SECURITY_STATE_BAD_CERTIFICATE);
Steve Block08a6f0c2011-10-06 12:12:53 +01002132 mCurrentState.mSslCertificateError = error;
Steve Block4895b012011-10-03 16:26:46 +01002133 } else if (getSecurityState() == SecurityState.SECURITY_STATE_SECURE) {
Steve Block08a6f0c2011-10-06 12:12:53 +01002134 // The page's main resource is secure and this error is for a
2135 // sub-resource.
Steve Block4895b012011-10-03 16:26:46 +01002136 setSecurityState(SecurityState.SECURITY_STATE_MIXED);
2137 }
2138 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002139}