blob: 3e3c9aaf60933c72198174a738488b9033c1e1af [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 Sekhar11b40062015-06-24 11:49:04 -0700802 // Resize the viewport if top controls is not visible
Vivek Sekhar2ee19a32015-07-02 17:03:57 -0700803 if (mMainView != null &&
804 (topControlsOffsetYPix == 0.0f || contentOffsetYPix == 0.0f))
Vivek Sekhar11b40062015-06-24 11:49:04 -0700805 ((BrowserWebView)mMainView).enableTopControls(
806 (topControlsOffsetYPix == 0.0f) ? true : false);
Vivek Sekhar8ee3abb2014-07-14 12:32:05 -0700807 }
808 }
809
810 @Override
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700811 public boolean isTabFullScreen() {
Vivek Sekharf96064b2014-07-28 16:32:34 -0700812 return mFullScreen;
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700813 }
814
815 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700816 public boolean onCreateWindow(WebView view, final boolean dialog,
817 final boolean userGesture, final Message resultMsg) {
818 // only allow new window or sub window for the foreground case
819 if (!mInForeground) {
820 return false;
821 }
822 // Short-circuit if we can't create any more tabs or sub windows.
823 if (dialog && mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -0700824 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700825 .setTitle(R.string.too_many_subwindows_dialog_title)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200826 .setIconAttribute(android.R.attr.alertDialogIcon)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700827 .setMessage(R.string.too_many_subwindows_dialog_message)
828 .setPositiveButton(R.string.ok, null)
829 .show();
830 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700831 } else if (!mWebViewController.getTabControl().canCreateNewTab()) {
Michael Kolb14612442011-06-24 13:06:29 -0700832 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700833 .setTitle(R.string.too_many_windows_dialog_title)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200834 .setIconAttribute(android.R.attr.alertDialogIcon)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700835 .setMessage(R.string.too_many_windows_dialog_message)
836 .setPositiveButton(R.string.ok, null)
837 .show();
838 return false;
839 }
840
841 // Short-circuit if this was a user gesture.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800842 if (userGesture || !mSettings.blockPopupWindows()) {
Tarun Nainani4f5137d2015-04-16 17:26:18 -0700843 WebView.WebViewTransport transport =
844 (WebView.WebViewTransport) resultMsg.obj;
845 CreateWindowParams windowParams = transport.getCreateWindowParams();
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700846 if (windowParams.mOpenerSuppressed) {
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700847 createWindow(dialog, resultMsg, windowParams.mURL, true);
848 // This is special case for rendering links on a webpage in
849 // a new tab. If opener is suppressed, the WebContents created
850 // by the content layer are not fully initialized. Returning false
851 // will prevent content layer from overriding WebContents
852 // created by new tab with the uninitialized instance.
853 return false;
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700854 }
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700855
856 createWindow(dialog, resultMsg);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700857 return true;
858 }
859
Tarun Nainani4f5137d2015-04-16 17:26:18 -0700860 createWindow(dialog, resultMsg);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700861 return true;
862 }
863
864 @Override
Patrick Scotteb5061b2009-11-18 15:00:30 -0500865 public void onRequestFocus(WebView view) {
866 if (!mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700867 mWebViewController.switchToTab(Tab.this);
Patrick Scotteb5061b2009-11-18 15:00:30 -0500868 }
869 }
870
871 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700872 public void onCloseWindow(WebView window) {
Michael Kolbc831b632011-05-11 09:30:34 -0700873 if (mParent != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700874 // JavaScript can only close popup window.
875 if (mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700876 mWebViewController.switchToTab(mParent);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700877 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700878 mWebViewController.closeTab(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700879 }
880 }
881
882 @Override
883 public void onProgressChanged(WebView view, int newProgress) {
John Reck30c714c2010-12-16 17:30:34 -0800884 mPageLoadProgress = newProgress;
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800885 if (newProgress == 100) {
886 mInPageLoad = false;
887 }
John Reck30c714c2010-12-16 17:30:34 -0800888 mWebViewController.onProgressChanged(Tab.this);
Michael Kolb72864272012-05-03 15:42:15 -0700889 if (mUpdateThumbnail && newProgress == 100) {
890 mUpdateThumbnail = false;
891 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700892 }
893
894 @Override
Leon Scroggins21d9b902010-03-11 09:33:11 -0500895 public void onReceivedTitle(WebView view, final String title) {
John Reck30c714c2010-12-16 17:30:34 -0800896 mCurrentState.mTitle = title;
Michael Kolb8233fac2010-10-26 16:08:53 -0700897 mWebViewController.onReceivedTitle(Tab.this, title);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700898 }
899
900 @Override
901 public void onReceivedIcon(WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -0800902 mCurrentState.mFavicon = icon;
Michael Kolb8233fac2010-10-26 16:08:53 -0700903 mWebViewController.onFavicon(Tab.this, view, icon);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700904 }
905
906 @Override
907 public void onReceivedTouchIconUrl(WebView view, String url,
908 boolean precomposed) {
Michael Kolb14612442011-06-24 13:06:29 -0700909 final ContentResolver cr = mContext.getContentResolver();
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400910 // Let precomposed icons take precedence over non-composed
911 // icons.
912 if (precomposed && mTouchIconLoader != null) {
913 mTouchIconLoader.cancel(false);
914 mTouchIconLoader = null;
915 }
916 // Have only one async task at a time.
917 if (mTouchIconLoader == null) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700918 mTouchIconLoader = new DownloadTouchIcon(Tab.this,
Michael Kolb14612442011-06-24 13:06:29 -0700919 mContext, cr, view);
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400920 mTouchIconLoader.execute(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700921 }
922 }
923
924 @Override
925 public void onShowCustomView(View view,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800926 CustomViewCallback callback) {
Michael Kolb14612442011-06-24 13:06:29 -0700927 Activity activity = mWebViewController.getActivity();
928 if (activity != null) {
929 onShowCustomView(view, activity.getRequestedOrientation(), callback);
930 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400931 }
932
933 @Override
934 public void onShowCustomView(View view, int requestedOrientation,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800935 CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700936 if (mInForeground) mWebViewController.showCustomView(Tab.this, view,
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400937 requestedOrientation, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700938 }
939
940 @Override
941 public void onHideCustomView() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700942 if (mInForeground) mWebViewController.hideCustomView();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700943 }
944
945 /**
946 * The origin has exceeded its database quota.
947 * @param url the URL that exceeded the quota
948 * @param databaseIdentifier the identifier of the database on which the
949 * transaction that caused the quota overflow was run
950 * @param currentQuota the current quota for the origin.
951 * @param estimatedSize the estimated size of the database.
952 * @param totalUsedQuota is the sum of all origins' quota.
953 * @param quotaUpdater The callback to run when a decision to allow or
954 * deny quota has been made. Don't forget to call this!
955 */
956 @Override
957 public void onExceededDatabaseQuota(String url,
958 String databaseIdentifier, long currentQuota, long estimatedSize,
959 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -0700960 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -0700961 .onExceededDatabaseQuota(url, databaseIdentifier,
962 currentQuota, estimatedSize, totalUsedQuota,
963 quotaUpdater);
964 }
965
966 /**
967 * The Application Cache has exceeded its max size.
968 * @param spaceNeeded is the amount of disk space that would be needed
969 * in order for the last appcache operation to succeed.
970 * @param totalUsedQuota is the sum of all origins' quota.
971 * @param quotaUpdater A callback to inform the WebCore thread that a
972 * new app cache size is available. This callback must always
973 * be executed at some point to ensure that the sleeping
974 * WebCore thread is woken up.
975 */
976 @Override
977 public void onReachedMaxAppCacheSize(long spaceNeeded,
978 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -0700979 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -0700980 .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota,
981 quotaUpdater);
982 }
983
Ben Murdoch65acc352009-11-19 18:16:04 +0000984 /* Adds a JavaScript error message to the system log and if the JS
985 * console is enabled in the about:debug options, to that console
986 * also.
Ben Murdochc42addf2010-01-28 15:19:59 +0000987 * @param consoleMessage the message object.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700988 */
989 @Override
Ben Murdochc42addf2010-01-28 15:19:59 +0000990 public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Jeff Hamilton47654f42010-09-07 09:57:51 -0500991 // Don't log console messages in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -0700992 if (isPrivateBrowsingEnabled()) return true;
Jeff Hamilton47654f42010-09-07 09:57:51 -0500993
Ben Murdochc42addf2010-01-28 15:19:59 +0000994 String message = "Console: " + consoleMessage.message() + " "
995 + consoleMessage.sourceId() + ":"
996 + consoleMessage.lineNumber();
997
998 switch (consoleMessage.messageLevel()) {
999 case TIP:
1000 Log.v(CONSOLE_LOGTAG, message);
1001 break;
1002 case LOG:
1003 Log.i(CONSOLE_LOGTAG, message);
1004 break;
1005 case WARNING:
1006 Log.w(CONSOLE_LOGTAG, message);
1007 break;
1008 case ERROR:
1009 Log.e(CONSOLE_LOGTAG, message);
1010 break;
1011 case DEBUG:
1012 Log.d(CONSOLE_LOGTAG, message);
1013 break;
1014 }
1015
1016 return true;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001017 }
1018
1019 /**
1020 * Ask the browser for an icon to represent a <video> element.
1021 * This icon will be used if the Web page did not specify a poster attribute.
1022 * @return Bitmap The icon or null if no such icon is available.
1023 */
1024 @Override
1025 public Bitmap getDefaultVideoPoster() {
1026 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001027 return mWebViewController.getDefaultVideoPoster();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001028 }
1029 return null;
1030 }
1031
1032 /**
1033 * Ask the host application for a custom progress view to show while
1034 * a <video> is loading.
1035 * @return View The progress view.
1036 */
1037 @Override
1038 public View getVideoLoadingProgressView() {
1039 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001040 return mWebViewController.getVideoLoadingProgressView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001041 }
1042 return null;
1043 }
1044
1045 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00001046 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001047 if (mInForeground) {
Ben Murdoch8cad4132012-01-11 10:56:43 +00001048 mWebViewController.openFileChooser(uploadMsg, acceptType, capture);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001049 } else {
1050 uploadMsg.onReceiveValue(null);
1051 }
1052 }
1053
Vivek Sekharb54614f2014-05-01 19:03:37 -07001054 @Override
1055 public void showFileChooser(ValueCallback<String[]> uploadFilePaths, String acceptTypes,
1056 boolean capture) {
1057 if (mInForeground) {
1058 mWebViewController.showFileChooser(uploadFilePaths, acceptTypes, capture);
1059 } else {
1060 uploadFilePaths.onReceiveValue(null);
1061 }
1062 }
1063
Grace Kloba22ac16e2009-10-07 18:00:23 -07001064 /**
1065 * Deliver a list of already-visited URLs
1066 */
1067 @Override
1068 public void getVisitedHistory(final ValueCallback<String[]> callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001069 mWebViewController.getVisitedHistory(callback);
1070 }
Ben Murdoch8029a772010-11-16 11:58:21 +00001071
1072 @Override
1073 public void setupAutoFill(Message message) {
1074 // Prompt the user to set up their profile.
1075 final Message msg = message;
Michael Kolb14612442011-06-24 13:06:29 -07001076 AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
1077 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
Ben Murdoch1d676b62011-01-17 12:54:24 +00001078 Context.LAYOUT_INFLATER_SERVICE);
1079 final View layout = inflater.inflate(R.layout.setup_autofill_dialog, null);
1080
1081 builder.setView(layout)
1082 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1083 @Override
1084 public void onClick(DialogInterface dialog, int id) {
1085 CheckBox disableAutoFill = (CheckBox) layout.findViewById(
1086 R.id.setup_autofill_dialog_disable_autofill);
1087
1088 if (disableAutoFill.isChecked()) {
1089 // Disable autofill and show a toast with how to turn it on again.
John Reck35e9dd62011-04-25 09:01:54 -07001090 mSettings.setAutofillEnabled(false);
Michael Kolb14612442011-06-24 13:06:29 -07001091 Toast.makeText(mContext,
Ben Murdoch1d676b62011-01-17 12:54:24 +00001092 R.string.autofill_setup_dialog_negative_toast,
1093 Toast.LENGTH_LONG).show();
1094 } else {
1095 // Take user to the AutoFill profile editor. When they return,
1096 // we will send the message that we pass here which will trigger
1097 // the form to get filled out with their new profile.
1098 mWebViewController.setupAutoFill(msg);
1099 }
1100 }
1101 })
1102 .setNegativeButton(R.string.cancel, null)
1103 .show();
Ben Murdoch8029a772010-11-16 11:58:21 +00001104 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001105 };
1106
1107 // -------------------------------------------------------------------------
1108 // WebViewClient implementation for the sub window
1109 // -------------------------------------------------------------------------
1110
1111 // Subclass of WebViewClient used in subwindows to notify the main
1112 // WebViewClient of certain WebView activities.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001113 private static class SubWindowClient extends WebViewClient {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001114 // The main WebViewClient.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001115 private final WebViewClient mClient;
Michael Kolb8233fac2010-10-26 16:08:53 -07001116 private final WebViewController mController;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001117
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001118 SubWindowClient(WebViewClient client, WebViewController controller) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001119 mClient = client;
Michael Kolb8233fac2010-10-26 16:08:53 -07001120 mController = controller;
Leon Scroggins III211ba542010-04-19 13:21:13 -04001121 }
1122 @Override
1123 public void onPageStarted(WebView view, String url, Bitmap favicon) {
1124 // Unlike the others, do not call mClient's version, which would
1125 // change the progress bar. However, we do want to remove the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001126 // find or select dialog.
Michael Kolb8233fac2010-10-26 16:08:53 -07001127 mController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001128 }
1129 @Override
1130 public void doUpdateVisitedHistory(WebView view, String url,
1131 boolean isReload) {
1132 mClient.doUpdateVisitedHistory(view, url, isReload);
1133 }
1134 @Override
1135 public boolean shouldOverrideUrlLoading(WebView view, String url) {
1136 return mClient.shouldOverrideUrlLoading(view, url);
1137 }
1138 @Override
1139 public void onReceivedSslError(WebView view, SslErrorHandler handler,
1140 SslError error) {
1141 mClient.onReceivedSslError(view, handler, error);
1142 }
1143 @Override
Brian Carlstrom8862c1d2011-06-02 01:05:55 -07001144 public void onReceivedClientCertRequest(WebView view,
1145 ClientCertRequestHandler handler, String host_and_port) {
1146 mClient.onReceivedClientCertRequest(view, handler, host_and_port);
1147 }
1148 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -07001149 public void onReceivedHttpAuthRequest(WebView view,
1150 HttpAuthHandler handler, String host, String realm) {
1151 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
1152 }
1153 @Override
1154 public void onFormResubmission(WebView view, Message dontResend,
1155 Message resend) {
1156 mClient.onFormResubmission(view, dontResend, resend);
1157 }
1158 @Override
1159 public void onReceivedError(WebView view, int errorCode,
1160 String description, String failingUrl) {
1161 mClient.onReceivedError(view, errorCode, description, failingUrl);
1162 }
1163 @Override
1164 public boolean shouldOverrideKeyEvent(WebView view,
1165 android.view.KeyEvent event) {
1166 return mClient.shouldOverrideKeyEvent(view, event);
1167 }
1168 @Override
1169 public void onUnhandledKeyEvent(WebView view,
1170 android.view.KeyEvent event) {
1171 mClient.onUnhandledKeyEvent(view, event);
1172 }
1173 }
1174
1175 // -------------------------------------------------------------------------
1176 // WebChromeClient implementation for the sub window
1177 // -------------------------------------------------------------------------
1178
1179 private class SubWindowChromeClient extends WebChromeClient {
1180 // The main WebChromeClient.
1181 private final WebChromeClient mClient;
1182
1183 SubWindowChromeClient(WebChromeClient client) {
1184 mClient = client;
1185 }
1186 @Override
1187 public void onProgressChanged(WebView view, int newProgress) {
1188 mClient.onProgressChanged(view, newProgress);
1189 }
1190 @Override
1191 public boolean onCreateWindow(WebView view, boolean dialog,
1192 boolean userGesture, android.os.Message resultMsg) {
1193 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
1194 }
1195 @Override
1196 public void onCloseWindow(WebView window) {
1197 if (window != mSubView) {
1198 Log.e(LOGTAG, "Can't close the window");
1199 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001200 mWebViewController.dismissSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001201 }
1202 }
1203
1204 // -------------------------------------------------------------------------
1205
1206 // Construct a new tab
Michael Kolb7bcafde2011-05-09 13:55:59 -07001207 Tab(WebViewController wvcontroller, WebView w) {
John Reck1cf4b792011-07-26 10:22:22 -07001208 this(wvcontroller, w, null);
1209 }
1210
1211 Tab(WebViewController wvcontroller, Bundle state) {
1212 this(wvcontroller, null, state);
1213 }
1214
1215 Tab(WebViewController wvcontroller, WebView w, Bundle state) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001216 mWebViewController = wvcontroller;
Michael Kolb14612442011-06-24 13:06:29 -07001217 mContext = mWebViewController.getContext();
John Reck35e9dd62011-04-25 09:01:54 -07001218 mSettings = BrowserSettings.getInstance();
Michael Kolb14612442011-06-24 13:06:29 -07001219 mDataController = DataController.getInstance(mContext);
1220 mCurrentState = new PageState(mContext, w != null
John Reck847b5322011-04-14 17:02:18 -07001221 ? w.isPrivateBrowsingEnabled() : false);
Tarun Nainani8084c822014-06-25 13:38:06 -07001222 setTimeStamp();
Michael Kolb8233fac2010-10-26 16:08:53 -07001223 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001224 mInForeground = false;
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001225 mWebViewDestroyedByMemoryMonitor = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001226
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001227 mDownloadListener = new BrowserDownloadListener() {
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001228 public void onDownloadStart(String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001229 String contentDisposition, String mimetype, String referer,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001230 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001231 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001232 mimetype, referer, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001233 }
1234 };
1235
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001236 mCaptureWidth = mContext.getResources().getDimensionPixelSize(R.dimen.tab_thumbnail_width);
1237 mCaptureHeight =mContext.getResources().getDimensionPixelSize(R.dimen.tab_thumbnail_height);
1238
1239 initCaptureBitmap();
1240
John Reck1cf4b792011-07-26 10:22:22 -07001241 restoreState(state);
John Reck52be4782011-08-26 15:37:29 -07001242 if (getId() == -1) {
1243 mId = TabControl.getNextId();
1244 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001245 setWebView(w);
Michael Kolb9ef259a2011-07-12 15:33:08 -07001246 mHandler = new Handler() {
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001247 @Override
Michael Kolb9ef259a2011-07-12 15:33:08 -07001248 public void handleMessage(Message m) {
John Reck1cf4b792011-07-26 10:22:22 -07001249 switch (m.what) {
1250 case MSG_CAPTURE:
1251 capture();
1252 break;
1253 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001254 }
1255 };
Pankaj Garg1c13cab2015-05-12 11:52:17 -07001256
1257 mFirstPixelObservable = new Observable();
1258 mFirstPixelObservable.set(false);
1259 mTabHistoryUpdateObservable = new Observable();
John Reck1cf4b792011-07-26 10:22:22 -07001260 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001261
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001262 private void initCaptureBitmap() {
1263 mCapture = Bitmap.createBitmap(mCaptureWidth, mCaptureHeight, Bitmap.Config.RGB_565);
1264 mCapture.eraseColor(Color.WHITE);
Michael Kolb72864272012-05-03 15:42:15 -07001265 }
1266
Mathew Inwoode09305e2011-09-02 12:03:26 +01001267 /**
1268 * This is used to get a new ID when the tab has been preloaded, before it is displayed and
1269 * added to TabControl. Preloaded tabs can be created before restoreInstanceState, leading
1270 * to overlapping IDs between the preloaded and restored tabs.
1271 */
1272 public void refreshIdAfterPreload() {
1273 mId = TabControl.getNextId();
1274 }
1275
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001276 public void setController(WebViewController ctl) {
1277 mWebViewController = ctl;
1278
John Reck1cf4b792011-07-26 10:22:22 -07001279 if (mWebViewController.shouldCaptureThumbnails()) {
1280 synchronized (Tab.this) {
1281 if (mCapture == null) {
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001282 initCaptureBitmap();
1283 if (mInForeground && !mHandler.hasMessages(MSG_CAPTURE)) {
1284 mHandler.sendEmptyMessageDelayed(MSG_CAPTURE, CAPTURE_DELAY);
John Reck1cf4b792011-07-26 10:22:22 -07001285 }
1286 }
1287 }
1288 } else {
1289 synchronized (Tab.this) {
1290 mCapture = null;
1291 deleteThumbnail();
1292 }
1293 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001294 }
1295
Michael Kolbc831b632011-05-11 09:30:34 -07001296 public long getId() {
1297 return mId;
1298 }
1299
Michael Kolb91911a22012-01-17 11:21:25 -08001300 void setWebView(WebView w) {
1301 setWebView(w, true);
1302 }
1303
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001304 public boolean isNativeActive(){
1305 if (mMainView == null)
1306 return false;
1307 return true;
1308 }
1309
1310 public void setTimeStamp(){
1311 Date d = new Date();
1312 timestamp = (new Timestamp(d.getTime()));
1313 }
1314
1315 public Timestamp getTimestamp() {
1316 return timestamp;
1317 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001318 /**
1319 * Sets the WebView for this tab, correctly removing the old WebView from
1320 * the container view.
1321 */
Michael Kolb91911a22012-01-17 11:21:25 -08001322 void setWebView(WebView w, boolean restore) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001323 if (mMainView == w) {
1324 return;
1325 }
Michael Kolba713ec82010-11-29 17:27:06 -08001326
Michael Kolba713ec82010-11-29 17:27:06 -08001327 mWebViewController.onSetWebView(this, w);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001328
John Reck1cf4b792011-07-26 10:22:22 -07001329 if (mMainView != null) {
John Reck8ee633f2011-08-09 16:00:35 -07001330 mMainView.setPictureListener(null);
John Reck1cf4b792011-07-26 10:22:22 -07001331 if (w != null) {
1332 syncCurrentState(w, null);
1333 } else {
Panos Thomasa9a5a582014-03-18 19:20:08 -07001334 mCurrentState = new PageState(mContext, mMainView.isPrivateBrowsingEnabled());
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001335
1336 if (mWebViewDestroyedByMemoryMonitor) {
1337 /*
1338 * If tab was destroyed as a result of the MemoryMonitor
1339 * then we need to restore the state properties
1340 * from the old WebView (mMainView)
1341 */
1342 syncCurrentState(mMainView, null);
1343 mWebViewDestroyedByMemoryMonitor = false;
1344 }
John Reck1cf4b792011-07-26 10:22:22 -07001345 }
1346 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001347 // set the new one
1348 mMainView = w;
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001349
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001350 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001351 if (mMainView != null) {
1352 mMainView.setWebViewClient(mWebViewClient);
1353 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001354 // Attach DownloadManager so that downloads can start in an active
1355 // or a non-active window. This can happen when going to a site that
1356 // does a redirect after a period of time. The user could have
1357 // switched to another tab while waiting for the download to start.
1358 mMainView.setDownloadListener(mDownloadListener);
John Reck8ee633f2011-08-09 16:00:35 -07001359 TabControl tc = mWebViewController.getTabControl();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001360 if (tc != null /*&& tc.getOnThumbnailUpdatedListener() != null*/) {
John Reck8ee633f2011-08-09 16:00:35 -07001361 mMainView.setPictureListener(this);
1362 }
Michael Kolb91911a22012-01-17 11:21:25 -08001363 if (restore && (mSavedState != null)) {
John Reck8b9bb8b2012-03-08 13:19:40 -08001364 restoreUserAgent();
John Reck6c2e2f32011-08-22 13:41:23 -07001365 WebBackForwardList restoredState
1366 = mMainView.restoreState(mSavedState);
1367 if (restoredState == null || restoredState.getSize() == 0) {
1368 Log.w(LOGTAG, "Failed to restore WebView state!");
1369 loadUrl(mCurrentState.mOriginalUrl, null);
1370 }
John Reck1cf4b792011-07-26 10:22:22 -07001371 mSavedState = null;
1372 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001373 }
1374 }
1375
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001376 public void destroyThroughMemoryMonitor() {
1377 mWebViewDestroyedByMemoryMonitor = true;
1378 destroy();
1379 }
1380
Grace Kloba22ac16e2009-10-07 18:00:23 -07001381 /**
1382 * Destroy the tab's main WebView and subWindow if any
1383 */
1384 void destroy() {
Tarun Nainani2c1dd7c2014-07-05 16:40:12 -07001385
Matthew Huib7f2e9c2014-04-16 11:12:37 -04001386 if (mPostponeDestroy) {
1387 mShouldDestroy = true;
1388 return;
1389 }
1390 mShouldDestroy = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001391 if (mMainView != null) {
1392 dismissSubWindow();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001393 // save the WebView to call destroy() after detach it from the tab
Vivek Sekhar53ef8932015-06-18 16:51:43 -07001394 final WebView webView = mMainView;
Vivek Sekhar2ee19a32015-07-02 17:03:57 -07001395 setWebView(null);
Pankaj Garg18aa0a12015-06-22 11:06:12 -07001396 if (!mWebViewDestroyedByMemoryMonitor &&
1397 !BrowserCommandLine.hasSwitch("ui-low-power-mode")) {
Vivek Sekhar2ee19a32015-07-02 17:03:57 -07001398 // Tabs can be reused with new instance of WebView so delete the snapshots
Vivek Sekhar53ef8932015-06-18 16:51:43 -07001399 webView.getSnapshotIds(new ValueCallback<List<Integer>>() {
1400 @Override
1401 public void onReceiveValue(List<Integer> ids) {
1402 int currentTabIdx = mWebViewController.getTabControl().getCurrentPosition();
1403 for (Integer id : ids) {
1404 if (getTabIdxFromCaptureIdx(id) == currentTabIdx) {
1405 webView.deleteSnapshot(id);
1406 }
1407 }
Vivek Sekhar53ef8932015-06-18 16:51:43 -07001408 webView.destroy();
Pankaj Garg1c13cab2015-05-12 11:52:17 -07001409 }
Vivek Sekhar53ef8932015-06-18 16:51:43 -07001410 });
Vivek Sekhar2ee19a32015-07-02 17:03:57 -07001411 } else {
1412 webView.destroy();
Pankaj Garg1c13cab2015-05-12 11:52:17 -07001413 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001414 }
1415 }
1416
Matthew Huib7f2e9c2014-04-16 11:12:37 -04001417 private boolean mPostponeDestroy = false;
1418 private boolean mShouldDestroy = false;
1419
1420 public void postponeDestroy() {
1421 mPostponeDestroy = true;
1422 }
1423
1424 public void performPostponedDestroy() {
1425 mPostponeDestroy = false;
1426 if (mShouldDestroy) {
1427 destroy();
1428 }
1429 }
1430
Grace Kloba22ac16e2009-10-07 18:00:23 -07001431 /**
1432 * Remove the tab from the parent
1433 */
1434 void removeFromTree() {
1435 // detach the children
Michael Kolbc831b632011-05-11 09:30:34 -07001436 if (mChildren != null) {
1437 for(Tab t : mChildren) {
1438 t.setParent(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001439 }
1440 }
1441 // remove itself from the parent list
Michael Kolbc831b632011-05-11 09:30:34 -07001442 if (mParent != null) {
1443 mParent.mChildren.remove(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001444 }
Tarun Nainani8eb00912014-07-17 12:28:32 -07001445
1446 mCapture = null;
John Reck1cf4b792011-07-26 10:22:22 -07001447 deleteThumbnail();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001448 }
1449
1450 /**
1451 * Create a new subwindow unless a subwindow already exists.
1452 * @return True if a new subwindow was created. False if one already exists.
1453 */
1454 boolean createSubWindow() {
1455 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001456 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001457 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001458 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001459 mSubView.setWebChromeClient(new SubWindowChromeClient(
1460 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001461 // Set a different DownloadListener for the mSubView, since it will
1462 // just need to dismiss the mSubView, rather than close the Tab
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001463 mSubView.setDownloadListener(new BrowserDownloadListener() {
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001464 public void onDownloadStart(String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001465 String contentDisposition, String mimetype, String referer,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001466 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001467 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001468 contentDisposition, mimetype, referer, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001469 if (mSubView.copyBackForwardList().getSize() == 0) {
1470 // This subwindow was opened for the sole purpose of
1471 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001472 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001473 }
1474 }
1475 });
Michael Kolb14612442011-06-24 13:06:29 -07001476 mSubView.setOnCreateContextMenuListener(mWebViewController.getActivity());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001477 return true;
1478 }
1479 return false;
1480 }
1481
1482 /**
1483 * Dismiss the subWindow for the tab.
1484 */
1485 void dismissSubWindow() {
1486 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001487 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001488 mSubView.destroy();
1489 mSubView = null;
1490 mSubViewContainer = null;
1491 }
1492 }
1493
Grace Kloba22ac16e2009-10-07 18:00:23 -07001494
1495 /**
1496 * Set the parent tab of this tab.
1497 */
Michael Kolbc831b632011-05-11 09:30:34 -07001498 void setParent(Tab parent) {
John Reck52be4782011-08-26 15:37:29 -07001499 if (parent == this) {
1500 throw new IllegalStateException("Cannot set parent to self!");
1501 }
Michael Kolbc831b632011-05-11 09:30:34 -07001502 mParent = parent;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001503 // This tab may have been freed due to low memory. If that is the case,
Michael Kolbc831b632011-05-11 09:30:34 -07001504 // the parent tab id is already saved. If we are changing that id
Grace Kloba22ac16e2009-10-07 18:00:23 -07001505 // (most likely due to removing the parent tab) we must update the
Michael Kolbc831b632011-05-11 09:30:34 -07001506 // parent tab id in the saved Bundle.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001507 if (mSavedState != null) {
1508 if (parent == null) {
1509 mSavedState.remove(PARENTTAB);
1510 } else {
Michael Kolbc831b632011-05-11 09:30:34 -07001511 mSavedState.putLong(PARENTTAB, parent.getId());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001512 }
1513 }
John Reckb0a86db2011-05-24 14:05:58 -07001514
1515 // Sync the WebView useragent with the parent
1516 if (parent != null && mSettings.hasDesktopUseragent(parent.getWebView())
1517 != mSettings.hasDesktopUseragent(getWebView())) {
1518 mSettings.toggleDesktopUseragent(getWebView());
1519 }
John Reck52be4782011-08-26 15:37:29 -07001520
1521 if (parent != null && parent.getId() == getId()) {
1522 throw new IllegalStateException("Parent has same ID as child!");
1523 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001524 }
1525
1526 /**
Michael Kolbc831b632011-05-11 09:30:34 -07001527 * If this Tab was created through another Tab, then this method returns
1528 * that Tab.
1529 * @return the Tab parent or null
1530 */
1531 public Tab getParent() {
1532 return mParent;
1533 }
1534
1535 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001536 * When a Tab is created through the content of another Tab, then we
1537 * associate the Tabs.
1538 * @param child the Tab that was created from this Tab
1539 */
1540 void addChildTab(Tab child) {
Michael Kolbc831b632011-05-11 09:30:34 -07001541 if (mChildren == null) {
1542 mChildren = new Vector<Tab>();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001543 }
Michael Kolbc831b632011-05-11 09:30:34 -07001544 mChildren.add(child);
1545 child.setParent(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001546 }
1547
Michael Kolbc831b632011-05-11 09:30:34 -07001548 Vector<Tab> getChildren() {
1549 return mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001550 }
1551
1552 void resume() {
1553 if (mMainView != null) {
John Reck56c1fcf2011-08-17 10:15:16 -07001554 setupHwAcceleration(mMainView);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001555 mMainView.onResume();
1556 if (mSubView != null) {
1557 mSubView.onResume();
1558 }
1559 }
1560 }
1561
John Reck56c1fcf2011-08-17 10:15:16 -07001562 private void setupHwAcceleration(View web) {
1563 if (web == null) return;
1564 BrowserSettings settings = BrowserSettings.getInstance();
1565 if (settings.isHardwareAccelerated()) {
1566 web.setLayerType(View.LAYER_TYPE_NONE, null);
1567 } else {
1568 web.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
1569 }
1570 }
1571
Grace Kloba22ac16e2009-10-07 18:00:23 -07001572 void pause() {
1573 if (mMainView != null) {
1574 mMainView.onPause();
1575 if (mSubView != null) {
1576 mSubView.onPause();
1577 }
1578 }
1579 }
1580
1581 void putInForeground() {
John Reck8ee633f2011-08-09 16:00:35 -07001582 if (mInForeground) {
1583 return;
1584 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001585 mInForeground = true;
1586 resume();
Michael Kolb14612442011-06-24 13:06:29 -07001587 Activity activity = mWebViewController.getActivity();
1588 mMainView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001589 if (mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -07001590 mSubView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001591 }
Axesh R. Ajmerac6b5c322015-05-01 11:06:10 -07001592
Leon Scroggins1961ed22010-12-07 15:22:21 -05001593 mWebViewController.bookmarkedStatusHasChanged(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001594 }
1595
1596 void putInBackground() {
John Reck8ee633f2011-08-09 16:00:35 -07001597 if (!mInForeground) {
1598 return;
1599 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001600 mInForeground = false;
1601 pause();
1602 mMainView.setOnCreateContextMenuListener(null);
1603 if (mSubView != null) {
1604 mSubView.setOnCreateContextMenuListener(null);
1605 }
1606 }
1607
Michael Kolb8233fac2010-10-26 16:08:53 -07001608 boolean inForeground() {
1609 return mInForeground;
1610 }
1611
Grace Kloba22ac16e2009-10-07 18:00:23 -07001612 /**
1613 * Return the top window of this tab; either the subwindow if it is not
1614 * null or the main window.
1615 * @return The top window of this tab.
1616 */
1617 WebView getTopWindow() {
1618 if (mSubView != null) {
1619 return mSubView;
1620 }
1621 return mMainView;
1622 }
1623
1624 /**
1625 * Return the main window of this tab. Note: if a tab is freed in the
1626 * background, this can return null. It is only guaranteed to be
1627 * non-null for the current tab.
1628 * @return The main WebView of this tab.
1629 */
1630 WebView getWebView() {
1631 return mMainView;
1632 }
1633
Michael Kolba713ec82010-11-29 17:27:06 -08001634 void setViewContainer(View container) {
1635 mContainer = container;
1636 }
1637
Michael Kolb8233fac2010-10-26 16:08:53 -07001638 View getViewContainer() {
1639 return mContainer;
1640 }
1641
Grace Kloba22ac16e2009-10-07 18:00:23 -07001642 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001643 * Return whether private browsing is enabled for the main window of
1644 * this tab.
1645 * @return True if private browsing is enabled.
1646 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001647 boolean isPrivateBrowsingEnabled() {
John Reck502a3532011-08-16 14:21:46 -07001648 return mCurrentState.mIncognito;
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001649 }
1650
1651 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001652 * Return the subwindow of this tab or null if there is no subwindow.
1653 * @return The subwindow of this tab or null.
1654 */
1655 WebView getSubWebView() {
1656 return mSubView;
1657 }
1658
Michael Kolb1514bb72010-11-22 09:11:48 -08001659 void setSubWebView(WebView subView) {
1660 mSubView = subView;
1661 }
1662
Michael Kolb8233fac2010-10-26 16:08:53 -07001663 View getSubViewContainer() {
1664 return mSubViewContainer;
1665 }
1666
Michael Kolb1514bb72010-11-22 09:11:48 -08001667 void setSubViewContainer(View subViewContainer) {
1668 mSubViewContainer = subViewContainer;
1669 }
1670
Grace Kloba22ac16e2009-10-07 18:00:23 -07001671
1672 /**
1673 * @return The application id string
1674 */
1675 String getAppId() {
1676 return mAppId;
1677 }
1678
1679 /**
1680 * Set the application id string
1681 * @param id
1682 */
1683 void setAppId(String id) {
1684 mAppId = id;
1685 }
1686
Michael Kolbe28b3472011-08-04 16:54:31 -07001687 boolean closeOnBack() {
1688 return mCloseOnBack;
1689 }
1690
1691 void setCloseOnBack(boolean close) {
1692 mCloseOnBack = close;
1693 }
1694
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001695 boolean getDerivedFromIntent() {
1696 return mDerivedFromIntent;
1697 }
1698
1699 void setDerivedFromIntent(boolean derived) {
1700 mDerivedFromIntent = derived;
1701 }
1702
Grace Kloba22ac16e2009-10-07 18:00:23 -07001703 String getUrl() {
John Reck324d4402011-01-11 16:56:42 -08001704 return UrlUtils.filteredUrl(mCurrentState.mUrl);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001705 }
1706
Tarun Nainani8eb00912014-07-17 12:28:32 -07001707
1708 protected void onPageFinished() {
1709 mPageFinished = true;
1710 }
1711
1712 public boolean getPageFinishedStatus() {
1713 return mPageFinished;
1714 }
1715
John Reck49a603c2011-03-03 09:33:05 -08001716 String getOriginalUrl() {
John Reckdb22ec42011-06-29 11:31:24 -07001717 if (mCurrentState.mOriginalUrl == null) {
1718 return getUrl();
John Reck49a603c2011-03-03 09:33:05 -08001719 }
John Reckdb22ec42011-06-29 11:31:24 -07001720 return UrlUtils.filteredUrl(mCurrentState.mOriginalUrl);
John Reck49a603c2011-03-03 09:33:05 -08001721 }
1722
Grace Kloba22ac16e2009-10-07 18:00:23 -07001723 /**
John Reck30c714c2010-12-16 17:30:34 -08001724 * Get the title of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001725 */
1726 String getTitle() {
John Reck30c714c2010-12-16 17:30:34 -08001727 return mCurrentState.mTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001728 }
1729
1730 /**
John Reck30c714c2010-12-16 17:30:34 -08001731 * Get the favicon of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001732 */
1733 Bitmap getFavicon() {
John Reck1cf4b792011-07-26 10:22:22 -07001734 if (mCurrentState.mFavicon != null) {
1735 return mCurrentState.mFavicon;
1736 }
1737 return getDefaultFavicon(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001738 }
1739
Pankaj Garg32e1b942015-06-03 18:13:24 -07001740 public boolean hasFavicon() {
1741 return mCurrentState.mFavicon != null;
1742 }
1743
John Recke969cc52010-12-21 17:24:43 -08001744 public boolean isBookmarkedSite() {
1745 return mCurrentState.mIsBookmarkedSite;
1746 }
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001747
Grace Kloba22ac16e2009-10-07 18:00:23 -07001748 /**
Steve Block08a6f0c2011-10-06 12:12:53 +01001749 * Sets the security state, clears the SSL certificate error and informs
1750 * the controller.
1751 */
Steve Block2466eff2011-10-03 15:33:09 +01001752 private void setSecurityState(SecurityState securityState) {
1753 mCurrentState.mSecurityState = securityState;
Steve Block08a6f0c2011-10-06 12:12:53 +01001754 mCurrentState.mSslCertificateError = null;
Steve Block2466eff2011-10-03 15:33:09 +01001755 mWebViewController.onUpdatedSecurityState(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001756 }
1757
1758 /**
Steve Block2466eff2011-10-03 15:33:09 +01001759 * @return The tab's security state.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001760 */
Steve Block2466eff2011-10-03 15:33:09 +01001761 SecurityState getSecurityState() {
1762 return mCurrentState.mSecurityState;
John Reck30c714c2010-12-16 17:30:34 -08001763 }
1764
Steve Block08a6f0c2011-10-06 12:12:53 +01001765 /**
1766 * Gets the SSL certificate error, if any, for the page's main resource.
1767 * This is only non-null when the security state is
1768 * SECURITY_STATE_BAD_CERTIFICATE.
1769 */
1770 SslError getSslCertificateError() {
1771 return mCurrentState.mSslCertificateError;
1772 }
1773
John Reck30c714c2010-12-16 17:30:34 -08001774 int getLoadProgress() {
1775 if (mInPageLoad) {
1776 return mPageLoadProgress;
1777 }
1778 return 100;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001779 }
1780
1781 /**
1782 * @return TRUE if onPageStarted is called while onPageFinished is not
1783 * called yet.
1784 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001785 boolean inPageLoad() {
1786 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001787 }
1788
Grace Kloba22ac16e2009-10-07 18:00:23 -07001789 /**
John Reck1cf4b792011-07-26 10:22:22 -07001790 * @return The Bundle with the tab's state if it can be saved, otherwise null
Grace Kloba22ac16e2009-10-07 18:00:23 -07001791 */
John Reck1cf4b792011-07-26 10:22:22 -07001792 public Bundle saveState() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001793 // If the WebView is null it means we ran low on memory and we already
1794 // stored the saved state in mSavedState.
1795 if (mMainView == null) {
John Reck1cf4b792011-07-26 10:22:22 -07001796 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001797 }
John Reck6c2e2f32011-08-22 13:41:23 -07001798
1799 if (TextUtils.isEmpty(mCurrentState.mUrl)) {
John Reck1cf4b792011-07-26 10:22:22 -07001800 return null;
John Reck24f18262011-06-17 14:47:20 -07001801 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001802
1803 mSavedState = new Bundle();
John Reck6c2e2f32011-08-22 13:41:23 -07001804 WebBackForwardList savedList = mMainView.saveState(mSavedState);
1805 if (savedList == null || savedList.getSize() == 0) {
1806 Log.w(LOGTAG, "Failed to save back/forward list for "
1807 + mCurrentState.mUrl);
1808 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001809
Michael Kolbc831b632011-05-11 09:30:34 -07001810 mSavedState.putLong(ID, mId);
John Reck30c714c2010-12-16 17:30:34 -08001811 mSavedState.putString(CURRURL, mCurrentState.mUrl);
1812 mSavedState.putString(CURRTITLE, mCurrentState.mTitle);
John Reck1cf4b792011-07-26 10:22:22 -07001813 mSavedState.putBoolean(INCOGNITO, mMainView.isPrivateBrowsingEnabled());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001814 if (mAppId != null) {
1815 mSavedState.putString(APPID, mAppId);
1816 }
Michael Kolbe28b3472011-08-04 16:54:31 -07001817 mSavedState.putBoolean(CLOSEFLAG, mCloseOnBack);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001818 // Remember the parent tab so the relationship can be restored.
Michael Kolbc831b632011-05-11 09:30:34 -07001819 if (mParent != null) {
1820 mSavedState.putLong(PARENTTAB, mParent.mId);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001821 }
John Reckb0a86db2011-05-24 14:05:58 -07001822 mSavedState.putBoolean(USERAGENT,
1823 mSettings.hasDesktopUseragent(getWebView()));
John Reck1cf4b792011-07-26 10:22:22 -07001824 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001825 }
1826
1827 /*
1828 * Restore the state of the tab.
1829 */
John Reck1cf4b792011-07-26 10:22:22 -07001830 private void restoreState(Bundle b) {
1831 mSavedState = b;
1832 if (mSavedState == null) {
1833 return;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001834 }
1835 // Restore the internal state even if the WebView fails to restore.
1836 // This will maintain the app id, original url and close-on-exit values.
Michael Kolbc831b632011-05-11 09:30:34 -07001837 mId = b.getLong(ID);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001838 mAppId = b.getString(APPID);
Michael Kolbe28b3472011-08-04 16:54:31 -07001839 mCloseOnBack = b.getBoolean(CLOSEFLAG);
John Reck8b9bb8b2012-03-08 13:19:40 -08001840 restoreUserAgent();
John Reck1cf4b792011-07-26 10:22:22 -07001841 String url = b.getString(CURRURL);
1842 String title = b.getString(CURRTITLE);
1843 boolean incognito = b.getBoolean(INCOGNITO);
1844 mCurrentState = new PageState(mContext, incognito, url, null);
1845 mCurrentState.mTitle = title;
1846 synchronized (Tab.this) {
1847 if (mCapture != null) {
John Reck4eadc342011-10-31 14:04:10 -07001848 DataController.getInstance(mContext).loadThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07001849 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001850 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001851 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04001852
John Reck8b9bb8b2012-03-08 13:19:40 -08001853 private void restoreUserAgent() {
1854 if (mMainView == null || mSavedState == null) {
1855 return;
1856 }
1857 if (mSavedState.getBoolean(USERAGENT)
1858 != mSettings.hasDesktopUseragent(mMainView)) {
1859 mSettings.toggleDesktopUseragent(mMainView);
1860 }
1861 }
1862
Leon Scroggins1961ed22010-12-07 15:22:21 -05001863 public void updateBookmarkedStatus() {
John Recke969cc52010-12-21 17:24:43 -08001864 mDataController.queryBookmarkStatus(getUrl(), mIsBookmarkCallback);
Leon Scroggins1961ed22010-12-07 15:22:21 -05001865 }
1866
John Recke969cc52010-12-21 17:24:43 -08001867 private DataController.OnQueryUrlIsBookmark mIsBookmarkCallback
1868 = new DataController.OnQueryUrlIsBookmark() {
1869 @Override
1870 public void onQueryUrlIsBookmark(String url, boolean isBookmark) {
1871 if (mCurrentState.mUrl.equals(url)) {
1872 mCurrentState.mIsBookmarkedSite = isBookmark;
1873 mWebViewController.bookmarkedStatusHasChanged(Tab.this);
1874 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001875 }
John Recke969cc52010-12-21 17:24:43 -08001876 };
Michael Kolb1acef692011-03-08 14:12:06 -08001877
Michael Kolbeb95db42011-03-03 10:38:40 -08001878 public Bitmap getScreenshot() {
John Reck1cf4b792011-07-26 10:22:22 -07001879 synchronized (Tab.this) {
1880 return mCapture;
1881 }
Michael Kolbeb95db42011-03-03 10:38:40 -08001882 }
1883
John Reck541f55a2011-06-07 16:34:43 -07001884 public boolean isSnapshot() {
John Reck541f55a2011-06-07 16:34:43 -07001885 return false;
1886 }
1887
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001888 private static class SaveCallback implements ValueCallback<String> {
1889 boolean onReceiveValueCalled = false;
1890 private String mPath;
John Reck68234a92012-04-19 15:27:12 -07001891
1892 @Override
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001893 public void onReceiveValue(String path) {
1894 this.onReceiveValueCalled = true;
1895 this.mPath = path;
John Reck68234a92012-04-19 15:27:12 -07001896 synchronized (this) {
1897 notifyAll();
John Reck8cc92352011-07-06 17:41:52 -07001898 }
John Reck541f55a2011-06-07 16:34:43 -07001899 }
John Reck68234a92012-04-19 15:27:12 -07001900
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001901 public String getPath() {
1902 return mPath;
1903 }
John Reck68234a92012-04-19 15:27:12 -07001904 }
1905
1906 /**
1907 * Must be called on the UI thread
1908 */
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001909 public ContentValues createSnapshotValues(Bitmap bm) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001910 WebView web = getWebView();
John Reck68234a92012-04-19 15:27:12 -07001911 if (web == null) return null;
John Reckd8c74522011-06-14 08:45:00 -07001912 ContentValues values = new ContentValues();
1913 values.put(Snapshots.TITLE, mCurrentState.mTitle);
1914 values.put(Snapshots.URL, mCurrentState.mUrl);
John Reck68234a92012-04-19 15:27:12 -07001915 values.put(Snapshots.BACKGROUND, web.getPageBackgroundColor());
John Reck8cc92352011-07-06 17:41:52 -07001916 values.put(Snapshots.DATE_CREATED, System.currentTimeMillis());
1917 values.put(Snapshots.FAVICON, compressBitmap(getFavicon()));
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001918 values.put(Snapshots.THUMBNAIL, compressBitmap(bm));
John Reckd8c74522011-06-14 08:45:00 -07001919 return values;
John Reck541f55a2011-06-07 16:34:43 -07001920 }
1921
John Reck68234a92012-04-19 15:27:12 -07001922 /**
1923 * Probably want to call this on a background thread
1924 */
1925 public boolean saveViewState(ContentValues values) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001926 WebView web = getWebView();
John Reck68234a92012-04-19 15:27:12 -07001927 if (web == null) return false;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001928 String filename = UUID.randomUUID().toString();
John Reck68234a92012-04-19 15:27:12 -07001929 SaveCallback callback = new SaveCallback();
John Reck68234a92012-04-19 15:27:12 -07001930 try {
John Reck68234a92012-04-19 15:27:12 -07001931 synchronized (callback) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001932 web.saveViewState(filename, callback);
1933 callback.wait();
John Reck68234a92012-04-19 15:27:12 -07001934 }
John Reck68234a92012-04-19 15:27:12 -07001935 } catch (Exception e) {
1936 Log.w(LOGTAG, "Failed to save view state", e);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001937 String path = callback.getPath();
1938 if (path != null) {
1939 File file = mContext.getFileStreamPath(path);
1940 if (file.exists() && !file.delete()) {
1941 file.deleteOnExit();
1942 }
John Reck68234a92012-04-19 15:27:12 -07001943 }
1944 return false;
1945 }
Tarun Nainani8eb00912014-07-17 12:28:32 -07001946
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001947 String path = callback.getPath();
Tarun Nainani8eb00912014-07-17 12:28:32 -07001948 // could be that saving of file failed
1949 if (path == null) {
1950 return false;
1951 }
1952
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001953 File savedFile = new File(path);
1954 if (!savedFile.exists()) {
1955 return false;
John Reck68234a92012-04-19 15:27:12 -07001956 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001957 values.put(Snapshots.VIEWSTATE_PATH, path.substring(path.lastIndexOf('/') + 1));
1958 values.put(Snapshots.VIEWSTATE_SIZE, savedFile.length());
John Reck68234a92012-04-19 15:27:12 -07001959 return true;
1960 }
1961
John Reck8cc92352011-07-06 17:41:52 -07001962 public byte[] compressBitmap(Bitmap bitmap) {
1963 if (bitmap == null) {
1964 return null;
1965 }
1966 ByteArrayOutputStream stream = new ByteArrayOutputStream();
1967 bitmap.compress(CompressFormat.PNG, 100, stream);
1968 return stream.toByteArray();
1969 }
1970
John Reck26b18322011-06-21 13:08:58 -07001971 public void loadUrl(String url, Map<String, String> headers) {
1972 if (mMainView != null) {
Michael Kolba53c9892011-10-05 13:31:40 -07001973 mPageLoadProgress = INITIAL_PROGRESS;
Michael Kolb14612442011-06-24 13:06:29 -07001974 mCurrentState = new PageState(mContext, false, url, null);
John Reck26b18322011-06-21 13:08:58 -07001975 mMainView.loadUrl(url, headers);
1976 }
1977 }
1978
John Reck38b39652012-06-05 09:22:59 -07001979 public void disableUrlOverridingForLoad() {
1980 mDisableOverrideUrlLoading = true;
1981 }
1982
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001983 private void thumbnailUpdated() {
John Reck8ee633f2011-08-09 16:00:35 -07001984 mHandler.removeMessages(MSG_CAPTURE);
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001985
John Reck8ee633f2011-08-09 16:00:35 -07001986 TabControl tc = mWebViewController.getTabControl();
1987 if (tc != null) {
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001988 OnThumbnailUpdatedListener updateListener = tc.getOnThumbnailUpdatedListener();
John Reck8ee633f2011-08-09 16:00:35 -07001989 if (updateListener != null) {
1990 updateListener.onThumbnailUpdated(this);
1991 }
1992 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001993 }
1994
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001995 protected void capture() {
1996 if (mMainView == null || mCapture == null || !mMainView.isReady() ||
1997 mMainView.getContentWidth() <= 0 || mMainView.getContentHeight() <= 0 ||
1998 !mFirstVisualPixelPainted || mMainView.isShowingCrashView()) {
1999
2000 initCaptureBitmap();
2001 thumbnailUpdated();
2002 return;
2003 }
2004
2005 mMainView.getContentBitmapAsync((float) mCaptureWidth / mMainView.getWidth(), new Rect(),
2006 new ValueCallback<Bitmap>() {
2007 @Override
2008 public void onReceiveValue(Bitmap bitmap) {
2009 if (mCapture == null) {
2010 initCaptureBitmap();
2011 }
2012
2013 if (bitmap == null) {
2014 thumbnailUpdated();
2015 return;
2016 }
2017
2018 Canvas c = new Canvas(mCapture);
2019 mCapture.eraseColor(Color.WHITE);
2020 c.drawBitmap(bitmap, 0, 0, null);
2021
2022 // manually anti-alias the edges for the tilt
2023 c.drawRect(0, 0, 1, mCapture.getHeight(), sAlphaPaint);
2024 c.drawRect(mCapture.getWidth() - 1, 0, mCapture.getWidth(),
2025 mCapture.getHeight(), sAlphaPaint);
2026 c.drawRect(0, 0, mCapture.getWidth(), 1, sAlphaPaint);
2027 c.drawRect(0, mCapture.getHeight() - 1, mCapture.getWidth(),
2028 mCapture.getHeight(), sAlphaPaint);
2029 c.setBitmap(null);
2030
2031 persistThumbnail();
2032 thumbnailUpdated();
2033 }
2034 }
2035 );
John Reck1cf4b792011-07-26 10:22:22 -07002036 }
2037
Pankaj Garg6bedeba2015-06-23 15:47:37 -07002038 @Override
2039 public void onNewPicture(WebView view, Picture picture) {
Michael Kolb9ef259a2011-07-12 15:33:08 -07002040 }
2041
John Reckef654f12011-07-12 16:42:08 -07002042 public boolean canGoBack() {
2043 return mMainView != null ? mMainView.canGoBack() : false;
2044 }
2045
2046 public boolean canGoForward() {
2047 return mMainView != null ? mMainView.canGoForward() : false;
2048 }
2049
2050 public void goBack() {
2051 if (mMainView != null) {
2052 mMainView.goBack();
2053 }
2054 }
2055
2056 public void goForward() {
2057 if (mMainView != null) {
2058 mMainView.goForward();
2059 }
2060 }
2061
John Reck1cf4b792011-07-26 10:22:22 -07002062 protected void persistThumbnail() {
John Reck4eadc342011-10-31 14:04:10 -07002063 DataController.getInstance(mContext).saveThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07002064 }
2065
2066 protected void deleteThumbnail() {
John Reck4eadc342011-10-31 14:04:10 -07002067 DataController.getInstance(mContext).deleteThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07002068 }
2069
John Reck4eadc342011-10-31 14:04:10 -07002070 void updateCaptureFromBlob(byte[] blob) {
John Reck1cf4b792011-07-26 10:22:22 -07002071 synchronized (Tab.this) {
2072 if (mCapture == null) {
2073 return;
2074 }
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002075 ByteBuffer buffer = ByteBuffer.wrap(blob);
Michael Kolbd837a112011-08-09 14:00:18 -07002076 try {
2077 mCapture.copyPixelsFromBuffer(buffer);
2078 } catch (RuntimeException rex) {
2079 Log.e(LOGTAG, "Load capture has mismatched sizes; buffer: "
2080 + buffer.capacity() + " blob: " + blob.length
2081 + "capture: " + mCapture.getByteCount());
2082 throw rex;
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002083 }
John Reck1cf4b792011-07-26 10:22:22 -07002084 }
2085 }
2086
John Reck52be4782011-08-26 15:37:29 -07002087 @Override
2088 public String toString() {
2089 StringBuilder builder = new StringBuilder(100);
2090 builder.append(mId);
2091 builder.append(") has parent: ");
2092 if (getParent() != null) {
2093 builder.append("true[");
2094 builder.append(getParent().getId());
2095 builder.append("]");
2096 } else {
2097 builder.append("false");
2098 }
2099 builder.append(", incog: ");
2100 builder.append(isPrivateBrowsingEnabled());
2101 if (!isPrivateBrowsingEnabled()) {
2102 builder.append(", title: ");
2103 builder.append(getTitle());
2104 builder.append(", url: ");
2105 builder.append(getUrl());
2106 }
2107 return builder.toString();
2108 }
2109
Steve Block4895b012011-10-03 16:26:46 +01002110 private void handleProceededAfterSslError(SslError error) {
2111 if (error.getUrl().equals(mCurrentState.mUrl)) {
2112 // The security state should currently be SECURITY_STATE_SECURE.
2113 setSecurityState(SecurityState.SECURITY_STATE_BAD_CERTIFICATE);
Steve Block08a6f0c2011-10-06 12:12:53 +01002114 mCurrentState.mSslCertificateError = error;
Steve Block4895b012011-10-03 16:26:46 +01002115 } else if (getSecurityState() == SecurityState.SECURITY_STATE_SECURE) {
Steve Block08a6f0c2011-10-06 12:12:53 +01002116 // The page's main resource is secure and this error is for a
2117 // sub-resource.
Steve Block4895b012011-10-03 16:26:46 +01002118 setSecurityState(SecurityState.SECURITY_STATE_MIXED);
2119 }
2120 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002121}