blob: a86b2e6da6920fa587d4ee394913720a3cc2852d [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;
Grace Kloba22ac16e2009-10-07 18:00:23 -070039import android.os.Bundle;
Michael Kolb9ef259a2011-07-12 15:33:08 -070040import android.os.Handler;
Grace Kloba22ac16e2009-10-07 18:00:23 -070041import android.os.Message;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +000042import android.os.SystemClock;
Brian Carlstrom8862c1d2011-06-02 01:05:55 -070043import android.security.KeyChain;
Brian Carlstromaa09cd82011-06-09 16:04:40 -070044import android.security.KeyChainAliasCallback;
John Reck24f18262011-06-17 14:47:20 -070045import android.text.TextUtils;
Grace Kloba22ac16e2009-10-07 18:00:23 -070046import android.util.Log;
47import android.view.KeyEvent;
48import android.view.LayoutInflater;
49import android.view.View;
Ben Murdochc42addf2010-01-28 15:19:59 +000050import android.webkit.ConsoleMessage;
Grace Kloba22ac16e2009-10-07 18:00:23 -070051import android.webkit.URLUtil;
John Reck438bf462011-01-12 18:11:46 -080052import android.webkit.WebResourceResponse;
Grace Kloba22ac16e2009-10-07 18:00:23 -070053import android.webkit.WebStorage;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080054import android.webkit.WebChromeClient.CustomViewCallback;
55import android.webkit.ValueCallback;
Ben Murdoch1d676b62011-01-17 12:54:24 +000056import android.widget.CheckBox;
Ben Murdoch8029a772010-11-16 11:58:21 +000057import android.widget.Toast;
Grace Kloba22ac16e2009-10-07 18:00:23 -070058
Bijan Amirzada41242f22014-03-21 12:12:18 -070059import com.android.browser.TabControl.OnThumbnailUpdatedListener;
60import com.android.browser.homepages.HomeProvider;
61import com.android.browser.mynavigation.MyNavigationUtil;
62import com.android.browser.provider.MyNavigationProvider;
63import com.android.browser.provider.SnapshotProvider.Snapshots;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080064
Pankaj Garg18aa0a12015-06-22 11:06:12 -070065import org.codeaurora.swe.BrowserCommandLine;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080066import org.codeaurora.swe.BrowserDownloadListener;
67import org.codeaurora.swe.ClientCertRequestHandler;
68import org.codeaurora.swe.HttpAuthHandler;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080069import org.codeaurora.swe.WebBackForwardList;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080070import org.codeaurora.swe.WebChromeClient;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080071import org.codeaurora.swe.WebView;
72import org.codeaurora.swe.WebView.PictureListener;
Pankaj Garg1c7380d2014-08-27 14:17:12 -070073import org.codeaurora.swe.WebView.CreateWindowParams;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080074import org.codeaurora.swe.WebViewClient;
Pankaj Garg1c13cab2015-05-12 11:52:17 -070075import org.codeaurora.swe.util.Observable;
Ze G Riande2a675c22015-06-03 11:15:24 -070076import org.codeaurora.swe.DomDistillerUtils;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080077
John Reck541f55a2011-06-07 16:34:43 -070078import java.io.ByteArrayOutputStream;
John Reck2b71d6d2012-04-18 17:42:06 -070079import java.io.File;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080080import java.io.InputStream;
John Reck1cf4b792011-07-26 10:22:22 -070081import java.nio.ByteBuffer;
Michael Kolbfe251992010-07-08 15:41:55 -070082import java.util.Map;
John Reck2b71d6d2012-04-18 17:42:06 -070083import java.util.UUID;
Michael Kolbfe251992010-07-08 15:41:55 -070084import java.util.Vector;
Vivek Sekhar53ef8932015-06-18 16:51:43 -070085import java.util.List;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080086import java.sql.Timestamp;
87import java.util.Date;
Michael Kolbfe251992010-07-08 15:41:55 -070088
Grace Kloba22ac16e2009-10-07 18:00:23 -070089/**
90 * Class for maintaining Tabs with a main WebView and a subwindow.
91 */
Michael Kolb9ef259a2011-07-12 15:33:08 -070092class Tab implements PictureListener {
Michael Kolb8233fac2010-10-26 16:08:53 -070093
Grace Kloba22ac16e2009-10-07 18:00:23 -070094 // Log Tag
95 private static final String LOGTAG = "Tab";
Bijan Amirzada41242f22014-03-21 12:12:18 -070096 private static final boolean LOGD_ENABLED = com.android.browser.Browser.LOGD_ENABLED;
Ben Murdochc42addf2010-01-28 15:19:59 +000097 // Special case the logtag for messages for the Console to make it easier to
98 // filter them and match the logtag used for these messages in older versions
99 // of the browser.
100 private static final String CONSOLE_LOGTAG = "browser";
101
Michael Kolb9ef259a2011-07-12 15:33:08 -0700102 private static final int MSG_CAPTURE = 42;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800103 private static final int CAPTURE_DELAY = 1000;
Michael Kolba53c9892011-10-05 13:31:40 -0700104 private static final int INITIAL_PROGRESS = 5;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700105
John Reck1cf4b792011-07-26 10:22:22 -0700106 private static Bitmap sDefaultFavicon;
Sagar Dhawan33551ff2015-07-08 17:24:44 -0700107 private boolean mIsKeyboardUp = false;
John Reck1cf4b792011-07-26 10:22:22 -0700108
Michael Kolba3194d02011-09-07 11:23:51 -0700109 private static Paint sAlphaPaint = new Paint();
110 static {
111 sAlphaPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
112 sAlphaPaint.setColor(Color.TRANSPARENT);
113 }
114
Steve Block2466eff2011-10-03 15:33:09 +0100115 public enum SecurityState {
Steve Block4895b012011-10-03 16:26:46 +0100116 // The page's main resource does not use SSL. Note that we use this
117 // state irrespective of the SSL authentication state of sub-resources.
Steve Block2466eff2011-10-03 15:33:09 +0100118 SECURITY_STATE_NOT_SECURE,
Steve Block4895b012011-10-03 16:26:46 +0100119 // The page's main resource uses SSL and the certificate is good. The
120 // same is true of all sub-resources.
Steve Block2466eff2011-10-03 15:33:09 +0100121 SECURITY_STATE_SECURE,
Steve Block4895b012011-10-03 16:26:46 +0100122 // The page's main resource uses SSL and the certificate is good, but
123 // some sub-resources either do not use SSL or have problems with their
124 // certificates.
Steve Block2466eff2011-10-03 15:33:09 +0100125 SECURITY_STATE_MIXED,
Steve Block4895b012011-10-03 16:26:46 +0100126 // The page's main resource uses SSL but there is a problem with its
127 // certificate.
128 SECURITY_STATE_BAD_CERTIFICATE,
John Reck30c714c2010-12-16 17:30:34 -0800129 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700130
Michael Kolb14612442011-06-24 13:06:29 -0700131 Context mContext;
John Reckd8c74522011-06-14 08:45:00 -0700132 protected WebViewController mWebViewController;
Michael Kolb8233fac2010-10-26 16:08:53 -0700133
Michael Kolbc831b632011-05-11 09:30:34 -0700134 // The tab ID
John Reckd8c74522011-06-14 08:45:00 -0700135 private long mId = -1;
Michael Kolbc831b632011-05-11 09:30:34 -0700136
Grace Kloba22ac16e2009-10-07 18:00:23 -0700137 // Main WebView wrapper
Michael Kolba713ec82010-11-29 17:27:06 -0800138 private View mContainer;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700139 // Main WebView
140 private WebView mMainView;
141 // Subwindow container
142 private View mSubViewContainer;
143 // Subwindow WebView
144 private WebView mSubView;
145 // Saved bundle for when we are running low on memory. It contains the
146 // information needed to restore the WebView if the user goes back to the
147 // tab.
148 private Bundle mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700149 // Parent Tab. This is the Tab that created this Tab, or null if the Tab was
150 // created by the UI
Michael Kolbc831b632011-05-11 09:30:34 -0700151 private Tab mParent;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700152 // Tab that constructed by this Tab. This is used when this Tab is
153 // destroyed, it clears all mParentTab values in the children.
Michael Kolbc831b632011-05-11 09:30:34 -0700154 private Vector<Tab> mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700155 // If true, the tab is in the foreground of the current activity.
156 private boolean mInForeground;
Michael Kolb8233fac2010-10-26 16:08:53 -0700157 // If true, the tab is in page loading state (after onPageStarted,
158 // before onPageFinsihed)
159 private boolean mInPageLoad;
Tarun Nainani8eb00912014-07-17 12:28:32 -0700160 private boolean mPageFinished;
John Reck38b39652012-06-05 09:22:59 -0700161 private boolean mDisableOverrideUrlLoading;
Pankaj Garg79878492015-04-01 14:48:21 -0700162 private boolean mFirstVisualPixelPainted = false;
John Reck30c714c2010-12-16 17:30:34 -0800163 // The last reported progress of the current page
164 private int mPageLoadProgress;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000165 // The time the load started, used to find load page time
166 private long mLoadStartTime;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700167 // Application identifier used to find tabs that another application wants
168 // to reuse.
169 private String mAppId;
Michael Kolbe28b3472011-08-04 16:54:31 -0700170 // flag to indicate if tab should be closed on back
171 private boolean mCloseOnBack;
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700172 // flag to indicate if the tab was opened from an intent
173 private boolean mDerivedFromIntent = false;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -0500174 // The listener that gets invoked when a download is started from the
175 // mMainView
Selim Gurun0b3d66f2012-08-29 13:08:13 -0700176 private final BrowserDownloadListener mDownloadListener;
John Recke969cc52010-12-21 17:24:43 -0800177 private DataController mDataController;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700178
179 // AsyncTask for downloading touch icons
180 DownloadTouchIcon mTouchIconLoader;
181
John Reck35e9dd62011-04-25 09:01:54 -0700182 private BrowserSettings mSettings;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700183 private int mCaptureWidth;
184 private int mCaptureHeight;
185 private Bitmap mCapture;
186 private Handler mHandler;
Michael Kolb72864272012-05-03 15:42:15 -0700187 private boolean mUpdateThumbnail;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800188 private Timestamp timestamp;
Vivek Sekharf96064b2014-07-28 16:32:34 -0700189 private boolean mFullScreen = false;
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800190 private boolean mReceivedError;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700191
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -0700192 // determine if webview is destroyed to MemoryMonitor
193 private boolean mWebViewDestroyedByMemoryMonitor;
194
Pankaj Garg21dad562015-07-02 17:17:24 -0700195 private String mTouchIconUrl;
196
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700197 private Observable mFirstPixelObservable;
198 private Observable mTabHistoryUpdateObservable;
199
200 Observable getFirstPixelObservable() {
201 return mFirstPixelObservable;
202 }
203
204 Observable getTabHistoryUpdateObservable() {
205 return mTabHistoryUpdateObservable;
206 }
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -0700207
Ze G Riande2a675c22015-06-03 11:15:24 -0700208 // dertermines if the tab contains a disllable page
209 private boolean mIsDistillable = false;
Mathew Inwood1dd8e822011-08-03 14:34:29 +0100210
John Reck1cf4b792011-07-26 10:22:22 -0700211 private static synchronized Bitmap getDefaultFavicon(Context context) {
212 if (sDefaultFavicon == null) {
213 sDefaultFavicon = BitmapFactory.decodeResource(
Enrico Rosd6efa972014-12-02 19:49:59 -0800214 context.getResources(), R.drawable.ic_deco_favicon_normal);
John Reck1cf4b792011-07-26 10:22:22 -0700215 }
216 return sDefaultFavicon;
217 }
Michael Kolbeb95db42011-03-03 10:38:40 -0800218
John Reck30c714c2010-12-16 17:30:34 -0800219 // All the state needed for a page
John Reckd8c74522011-06-14 08:45:00 -0700220 protected static class PageState {
John Reck30c714c2010-12-16 17:30:34 -0800221 String mUrl;
John Reckdb22ec42011-06-29 11:31:24 -0700222 String mOriginalUrl;
John Reck30c714c2010-12-16 17:30:34 -0800223 String mTitle;
Steve Block2466eff2011-10-03 15:33:09 +0100224 SecurityState mSecurityState;
Steve Block08a6f0c2011-10-06 12:12:53 +0100225 // This is non-null only when mSecurityState is SECURITY_STATE_BAD_CERTIFICATE.
John Reck30c714c2010-12-16 17:30:34 -0800226 Bitmap mFavicon;
Steve Block08a6f0c2011-10-06 12:12:53 +0100227 boolean mIsBookmarkedSite;
228 boolean mIncognito;
John Reck30c714c2010-12-16 17:30:34 -0800229
230 PageState(Context c, boolean incognito) {
John Reck502a3532011-08-16 14:21:46 -0700231 mIncognito = incognito;
232 if (mIncognito) {
Axesh R. Ajmera1f998ae2015-04-21 14:16:41 -0700233 mOriginalUrl = mUrl = Controller.INCOGNITO_URI;
John Reck30c714c2010-12-16 17:30:34 -0800234 mTitle = c.getString(R.string.new_incognito_tab);
John Reck30c714c2010-12-16 17:30:34 -0800235 } else {
John Reckdb22ec42011-06-29 11:31:24 -0700236 mOriginalUrl = mUrl = "";
John Reck30c714c2010-12-16 17:30:34 -0800237 mTitle = c.getString(R.string.new_tab);
John Reck30c714c2010-12-16 17:30:34 -0800238 }
Steve Block2466eff2011-10-03 15:33:09 +0100239 mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
John Reck30c714c2010-12-16 17:30:34 -0800240 }
241
242 PageState(Context c, boolean incognito, String url, Bitmap favicon) {
John Reck502a3532011-08-16 14:21:46 -0700243 mIncognito = incognito;
John Reckdb22ec42011-06-29 11:31:24 -0700244 mOriginalUrl = mUrl = url;
Pankaj Garg96d0ccd2015-07-30 16:49:47 -0700245 mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
John Reck1cf4b792011-07-26 10:22:22 -0700246 mFavicon = favicon;
John Reck30c714c2010-12-16 17:30:34 -0800247 }
John Reck1cf4b792011-07-26 10:22:22 -0700248
Grace Kloba22ac16e2009-10-07 18:00:23 -0700249 }
250
John Reck30c714c2010-12-16 17:30:34 -0800251 // The current/loading page's state
John Reckd8c74522011-06-14 08:45:00 -0700252 protected PageState mCurrentState;
John Reck30c714c2010-12-16 17:30:34 -0800253
Grace Kloba22ac16e2009-10-07 18:00:23 -0700254 // Used for saving and restoring each Tab
Michael Kolbc831b632011-05-11 09:30:34 -0700255 static final String ID = "ID";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700256 static final String CURRURL = "currentUrl";
257 static final String CURRTITLE = "currentTitle";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700258 static final String PARENTTAB = "parentTab";
259 static final String APPID = "appid";
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700260 static final String INCOGNITO = "privateBrowsingEnabled";
John Reckb0a86db2011-05-24 14:05:58 -0700261 static final String USERAGENT = "useragent";
Michael Kolbe28b3472011-08-04 16:54:31 -0700262 static final String CLOSEFLAG = "closeOnBack";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700263
Pankaj Garg18186a92015-03-31 14:59:33 -0700264 public void setNetworkAvailable(boolean networkUp) {
265 if (networkUp && mReceivedError && (mMainView != null)) {
266 mMainView.reload();
267 }
268 }
269
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700270 public boolean isFirstVisualPixelPainted() {
271 return mFirstVisualPixelPainted;
272 }
273
274 public int getCaptureIndex(int navIndex) {
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700275 int orientation = mWebViewController.getActivity().
276 getResources().getConfiguration().orientation;
277
278 int orientationBit = (orientation == Configuration.ORIENTATION_LANDSCAPE) ? 0 : 1;
279
Vivek Sekhard0f60402015-06-05 14:07:11 -0700280 int index = orientationBit << 31 | (((int)mId & 0x7f) << 24) | (navIndex & 0xffffff);
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700281 return index;
282 }
283
284 public int getTabIdxFromCaptureIdx(int index) {
285 return (index & 0x7f000000) >> 24;
286 }
287
288 public int getOrientationFromCaptureIdx(int index) {
289 return ((index & 0x80000000) == 0) ? Configuration.ORIENTATION_LANDSCAPE :
290 Configuration.ORIENTATION_PORTRAIT;
291
292 }
293
294 public int getNavIdxFromCaptureIdx(int index) {
295 return (index & 0xffffff);
296 }
297
Pankaj Garg96d0ccd2015-07-30 16:49:47 -0700298 public static SecurityState getWebViewSecurityState(WebView view) {
299 switch (view.getSecurityLevel()) {
300 case WebView.SecurityLevel.EV_SECURE:
301 case WebView.SecurityLevel.SECURE:
302 return SecurityState.SECURITY_STATE_SECURE;
303 case WebView.SecurityLevel.SECURITY_ERROR:
304 return SecurityState.SECURITY_STATE_BAD_CERTIFICATE;
305 case WebView.SecurityLevel.SECURITY_POLICY_WARNING:
306 case WebView.SecurityLevel.SECURITY_WARNING:
307 return SecurityState.SECURITY_STATE_MIXED;
308 }
309 return SecurityState.SECURITY_STATE_NOT_SECURE;
310 }
311
Grace Kloba22ac16e2009-10-07 18:00:23 -0700312 // -------------------------------------------------------------------------
313 // WebViewClient implementation for the main WebView
314 // -------------------------------------------------------------------------
315
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800316 private final WebViewClient mWebViewClient = new WebViewClient() {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500317 private Message mDontResend;
318 private Message mResend;
Michael Kolb47bd1e42011-09-01 15:25:00 -0700319
320 private boolean providersDiffer(String url, String otherUrl) {
321 Uri uri1 = Uri.parse(url);
322 Uri uri2 = Uri.parse(otherUrl);
323 return !uri1.getEncodedAuthority().equals(uri2.getEncodedAuthority());
324 }
325
Grace Kloba22ac16e2009-10-07 18:00:23 -0700326 @Override
327 public void onPageStarted(WebView view, String url, Bitmap favicon) {
Ze G Riande2a675c22015-06-03 11:15:24 -0700328 setIsDistillable(false);
Michael Kolb8233fac2010-10-26 16:08:53 -0700329 mInPageLoad = true;
Tarun Nainani8eb00912014-07-17 12:28:32 -0700330 mPageFinished = false;
Pankaj Garg79878492015-04-01 14:48:21 -0700331 mFirstVisualPixelPainted = false;
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700332 mFirstPixelObservable.set(false);
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800333 mReceivedError = false;
Michael Kolb72864272012-05-03 15:42:15 -0700334 mUpdateThumbnail = true;
Michael Kolba53c9892011-10-05 13:31:40 -0700335 mPageLoadProgress = INITIAL_PROGRESS;
Michael Kolb14612442011-06-24 13:06:29 -0700336 mCurrentState = new PageState(mContext,
John Reck30c714c2010-12-16 17:30:34 -0800337 view.isPrivateBrowsingEnabled(), url, favicon);
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000338 mLoadStartTime = SystemClock.uptimeMillis();
Pankaj Garg62bc7912015-04-14 16:08:59 -0700339 // Need re-enable FullScreenMode on Page navigation if needed
340 if (BrowserSettings.getInstance().useFullscreen()){
341 Controller controller = (Controller) mWebViewController;
342 BaseUi ui = (BaseUi) controller.getUi();
343 ui.forceDisableFullscreenMode(false);
344 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700345 // If we start a touch icon load and then load a new page, we don't
346 // want to cancel the current touch icon loader. But, we do want to
347 // create a new one when the touch icon url is known.
348 if (mTouchIconLoader != null) {
349 mTouchIconLoader.mTab = null;
350 mTouchIconLoader = null;
351 }
352
Grace Kloba22ac16e2009-10-07 18:00:23 -0700353 // finally update the UI in the activity if it is in the foreground
John Reck324d4402011-01-11 16:56:42 -0800354 mWebViewController.onPageStarted(Tab.this, view, favicon);
Leon Scroggins4cd97792010-12-03 15:31:56 -0500355
John Recke969cc52010-12-21 17:24:43 -0800356 updateBookmarkedStatus();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700357 }
358
359 @Override
360 public void onPageFinished(WebView view, String url) {
John Reck38b39652012-06-05 09:22:59 -0700361 mDisableOverrideUrlLoading = false;
John Reck5b691842010-11-29 11:21:13 -0800362 if (!isPrivateBrowsingEnabled()) {
363 LogTag.logPageFinishedLoading(
364 url, SystemClock.uptimeMillis() - mLoadStartTime);
365 }
John Reck1cf4b792011-07-26 10:22:22 -0700366 syncCurrentState(view, url);
John Reck324d4402011-01-11 16:56:42 -0800367 mWebViewController.onPageFinished(Tab.this);
Pankaj Garg96d0ccd2015-07-30 16:49:47 -0700368 setSecurityState(getWebViewSecurityState(view));
Grace Kloba22ac16e2009-10-07 18:00:23 -0700369 }
370
Pankaj Garg79878492015-04-01 14:48:21 -0700371 @Override
372 public void onFirstVisualPixel(WebView view) {
373 mFirstVisualPixelPainted = true;
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700374 mFirstPixelObservable.set(true);
Pankaj Garg79878492015-04-01 14:48:21 -0700375 }
376
Grace Kloba22ac16e2009-10-07 18:00:23 -0700377 // return true if want to hijack the url to let another app to handle it
378 @Override
379 public boolean shouldOverrideUrlLoading(WebView view, String url) {
John Reck38b39652012-06-05 09:22:59 -0700380 if (!mDisableOverrideUrlLoading && mInForeground) {
Michael Kolb18eb3772010-12-10 14:29:51 -0800381 return mWebViewController.shouldOverrideUrlLoading(Tab.this,
382 view, url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700383 } else {
384 return false;
385 }
386 }
387
Vivek Sekharb991edb2014-12-17 18:18:07 -0800388 @Override
389 public boolean shouldDownloadFavicon(WebView view, String url) {
390 return true;
391 }
392
Grace Kloba22ac16e2009-10-07 18:00:23 -0700393 /**
Steve Block2466eff2011-10-03 15:33:09 +0100394 * Updates the security state. This method is called when we discover
395 * another resource to be loaded for this page (for example,
396 * javascript). While we update the security state, we do not update
397 * the lock icon until we are done loading, as it is slightly more
398 * secure this way.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700399 */
400 @Override
401 public void onLoadResource(WebView view, String url) {
402 if (url != null && url.length() > 0) {
403 // It is only if the page claims to be secure that we may have
Steve Block2466eff2011-10-03 15:33:09 +0100404 // to update the security state:
405 if (mCurrentState.mSecurityState == SecurityState.SECURITY_STATE_SECURE) {
406 // If NOT a 'safe' url, change the state to mixed content!
Grace Kloba22ac16e2009-10-07 18:00:23 -0700407 if (!(URLUtil.isHttpsUrl(url) || URLUtil.isDataUrl(url)
408 || URLUtil.isAboutUrl(url))) {
Steve Block2466eff2011-10-03 15:33:09 +0100409 mCurrentState.mSecurityState = SecurityState.SECURITY_STATE_MIXED;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700410 }
411 }
412 }
413 }
414
415 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700416 * Show a dialog informing the user of the network error reported by
417 * WebCore if it is in the foreground.
418 */
419 @Override
420 public void onReceivedError(WebView view, int errorCode,
421 String description, String failingUrl) {
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800422 // Used for the syncCurrentState to use
423 // the failing url instead of using webview url
424 mReceivedError = true;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700425 }
426
427 /**
428 * Check with the user if it is ok to resend POST data as the page they
429 * are trying to navigate to is the result of a POST.
430 */
431 @Override
432 public void onFormResubmission(WebView view, final Message dontResend,
433 final Message resend) {
434 if (!mInForeground) {
435 dontResend.sendToTarget();
436 return;
437 }
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500438 if (mDontResend != null) {
439 Log.w(LOGTAG, "onFormResubmission should not be called again "
440 + "while dialog is still up");
441 dontResend.sendToTarget();
442 return;
443 }
444 mDontResend = dontResend;
445 mResend = resend;
Michael Kolb14612442011-06-24 13:06:29 -0700446 new AlertDialog.Builder(mContext).setTitle(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700447 R.string.browserFrameFormResubmitLabel).setMessage(
448 R.string.browserFrameFormResubmitMessage)
449 .setPositiveButton(R.string.ok,
450 new DialogInterface.OnClickListener() {
451 public void onClick(DialogInterface dialog,
452 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500453 if (mResend != null) {
454 mResend.sendToTarget();
455 mResend = null;
456 mDontResend = null;
457 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700458 }
459 }).setNegativeButton(R.string.cancel,
460 new DialogInterface.OnClickListener() {
461 public void onClick(DialogInterface dialog,
462 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500463 if (mDontResend != null) {
464 mDontResend.sendToTarget();
465 mResend = null;
466 mDontResend = null;
467 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700468 }
469 }).setOnCancelListener(new OnCancelListener() {
470 public void onCancel(DialogInterface dialog) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500471 if (mDontResend != null) {
472 mDontResend.sendToTarget();
473 mResend = null;
474 mDontResend = null;
475 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700476 }
477 }).show();
478 }
479
480 /**
481 * Insert the url into the visited history database.
482 * @param url The url to be inserted.
483 * @param isReload True if this url is being reloaded.
484 * FIXME: Not sure what to do when reloading the page.
485 */
486 @Override
487 public void doUpdateVisitedHistory(WebView view, String url,
488 boolean isReload) {
John Reck324d4402011-01-11 16:56:42 -0800489 mWebViewController.doUpdateVisitedHistory(Tab.this, isReload);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700490 }
491
492 /**
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700493 * Displays client certificate request to the user.
494 */
495 @Override
496 public void onReceivedClientCertRequest(final WebView view,
497 final ClientCertRequestHandler handler, final String host_and_port) {
498 if (!mInForeground) {
499 handler.ignore();
500 return;
501 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700502 int colon = host_and_port.lastIndexOf(':');
503 String host;
504 int port;
505 if (colon == -1) {
506 host = host_and_port;
507 port = -1;
508 } else {
509 String portString = host_and_port.substring(colon + 1);
510 try {
511 port = Integer.parseInt(portString);
512 host = host_and_port.substring(0, colon);
513 } catch (NumberFormatException e) {
514 host = host_and_port;
515 port = -1;
516 }
517 }
Michael Kolb14612442011-06-24 13:06:29 -0700518 KeyChain.choosePrivateKeyAlias(
519 mWebViewController.getActivity(), new KeyChainAliasCallback() {
Sagar Dhawan49f85cf2015-07-10 16:54:20 -0700520 @Override
521 public void alias(String alias) {
522 if (alias == null) {
523 handler.cancel();
524 return;
525 }
526 new KeyChainLookup(mContext, handler, alias).execute();
527 }
528 }, null, null, host, port, null);
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700529 }
530
531 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700532 * Handles an HTTP authentication request.
533 *
534 * @param handler The authentication handler
535 * @param host The host
536 * @param realm The realm
537 */
538 @Override
539 public void onReceivedHttpAuthRequest(WebView view,
540 final HttpAuthHandler handler, final String host,
541 final String realm) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700542 mWebViewController.onReceivedHttpAuthRequest(Tab.this, view, handler, host, realm);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700543 }
544
545 @Override
John Reck438bf462011-01-12 18:11:46 -0800546 public WebResourceResponse shouldInterceptRequest(WebView view,
547 String url) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800548 //intercept if opening a new incognito tab - show the incognito welcome page
Axesh R. Ajmera1f998ae2015-04-21 14:16:41 -0700549
550 // show only incognito content and webview has private
551 // and cannot go back(only supported if explicit from UI )
552 if (view.isPrivateBrowsingEnabled() &&
553 !view.canGoBack() &&
554 url.startsWith(Controller.INCOGNITO_URI) ) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800555 Resources resourceHandle = mContext.getResources();
556 InputStream inStream = resourceHandle.openRawResource(
Bijan Amirzada41242f22014-03-21 12:12:18 -0700557 com.android.browser.R.raw.incognito_mode_start_page);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800558 return new WebResourceResponse("text/html", "utf8", inStream);
559 }
kaiyiz6e5b3e02013-08-19 20:02:01 +0800560 WebResourceResponse res;
561 if (MyNavigationUtil.MY_NAVIGATION.equals(url)) {
562 res = MyNavigationProvider.shouldInterceptRequest(mContext, url);
563 } else {
564 res = HomeProvider.shouldInterceptRequest(mContext, url);
565 }
John Reck438bf462011-01-12 18:11:46 -0800566 return res;
567 }
568
569 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700570 public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
571 if (!mInForeground) {
572 return false;
573 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700574 return mWebViewController.shouldOverrideKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700575 }
576
577 @Override
578 public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700579 if (!mInForeground) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700580 return;
581 }
John Reck997b1b72012-04-19 18:08:25 -0700582 if (!mWebViewController.onUnhandledKeyEvent(event)) {
583 super.onUnhandledKeyEvent(view, event);
584 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700585 }
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700586
587 @Override
588 public void beforeNavigation(WebView view, String url) {
Pankaj Garg21dad562015-07-02 17:17:24 -0700589 mTouchIconUrl = null;
Site Mao61b68212015-07-16 10:56:31 -0700590 if (BaseUi.isUiLowPowerMode()) {
Pankaj Garg18aa0a12015-06-22 11:06:12 -0700591 return;
592 }
593
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700594 if (isPrivateBrowsingEnabled()) {
595 return;
596 }
597
598 if (!mFirstVisualPixelPainted) {
599 return;
600 }
601
Pankaj Garg8ee61462015-07-29 18:17:24 -0700602 if (view.getUrl().equals(url)) {
603 return;
604 }
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -0700605
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700606 final int idx = view.copyBackForwardList().getCurrentIndex();
607 boolean bitmapExists = view.hasSnapshot(idx);
608
609 int progress = 100;
610 Controller controller = (Controller)mWebViewController;
611 UI ui = controller.getUi();
612 if (ui instanceof BaseUi) {
613 BaseUi baseUi = (BaseUi) ui;
614 TitleBar titleBar = baseUi.getTitleBar();
615 progress = titleBar.getProgressView().getProgressPercent();
616 }
617
618 if (bitmapExists && progress < 85) {
619 return;
620 }
621
622 int index = getCaptureIndex(view.getLastCommittedHistoryIndex());
Sagar Dhawan49f85cf2015-07-10 16:54:20 -0700623 view.captureSnapshot(index, null);
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700624 }
625
626 @Override
627 public void onHistoryItemCommit(WebView view, int index) {
Site Mao61b68212015-07-16 10:56:31 -0700628 if (BaseUi.isUiLowPowerMode()) {
Pankaj Garg18aa0a12015-06-22 11:06:12 -0700629 return;
630 }
631
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -0700632 // prevent snapshot tab from commiting any history
633 if (isSnapshot()) {
634 return;
635 }
636
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700637 mTabHistoryUpdateObservable.set(index);
Vivek Sekhar53ef8932015-06-18 16:51:43 -0700638 final int maxIdx = view.copyBackForwardList().getSize();
639 final WebView wv = view;
640 view.getSnapshotIds(new ValueCallback <List<Integer>>() {
641 @Override
642 public void onReceiveValue(List<Integer> ids) {
643 int currentTabIdx = mWebViewController.getTabControl().getCurrentPosition();
644 for (Integer id : ids) {
645 if (getTabIdxFromCaptureIdx(id) == currentTabIdx &&
646 getNavIdxFromCaptureIdx(id) >= maxIdx) {
647 wv.deleteSnapshot(id);
648 }
649 }
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700650 }
Vivek Sekhar53ef8932015-06-18 16:51:43 -0700651 });
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700652 }
Pankaj Garg62bc7912015-04-14 16:08:59 -0700653
654 @Override
655 public void onKeyboardStateChange(boolean popup) {
Vivek Sekhard4de6162015-07-21 15:01:45 -0700656 boolean keyboardWasShowing = isKeyboardShowing();
Sagar Dhawan33551ff2015-07-08 17:24:44 -0700657 mIsKeyboardUp = popup;
Vivek Sekhard4de6162015-07-21 15:01:45 -0700658 Controller controller = (Controller)mWebViewController;
659 BaseUi ui = (BaseUi) controller.getUi();
660 // lock the title bar
661 if (popup)
662 ui.getTitleBar().showTopControls(true);
663 if (keyboardWasShowing && popup)
664 ui.getTitleBar().enableTopControls(true);
Pankaj Garg62bc7912015-04-14 16:08:59 -0700665 if (BrowserSettings.getInstance().useFullscreen()) {
Pankaj Garg62bc7912015-04-14 16:08:59 -0700666 ui.forceDisableFullscreenMode(popup);
667 }
668 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700669 };
670
John Reck1cf4b792011-07-26 10:22:22 -0700671 private void syncCurrentState(WebView view, String url) {
672 // Sync state (in case of stop/timeout)
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800673
674 if (mReceivedError) {
675 mCurrentState.mUrl = url;
676 mCurrentState.mOriginalUrl = url;
677 } else {
678 mCurrentState.mUrl = view.getUrl();
679 mCurrentState.mOriginalUrl = view.getOriginalUrl();
680 mCurrentState.mFavicon = view.getFavicon();
681 }
682
John Reck1cf4b792011-07-26 10:22:22 -0700683 if (mCurrentState.mUrl == null) {
John Reck8bcafc12011-08-29 16:43:02 -0700684 mCurrentState.mUrl = "";
John Reck1cf4b792011-07-26 10:22:22 -0700685 }
John Reck1cf4b792011-07-26 10:22:22 -0700686 mCurrentState.mTitle = view.getTitle();
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800687
688
John Reck1cf4b792011-07-26 10:22:22 -0700689 if (!URLUtil.isHttpsUrl(mCurrentState.mUrl)) {
690 // In case we stop when loading an HTTPS page from an HTTP page
691 // but before a provisional load occurred
Steve Block2466eff2011-10-03 15:33:09 +0100692 mCurrentState.mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
John Reck1cf4b792011-07-26 10:22:22 -0700693 }
John Reck502a3532011-08-16 14:21:46 -0700694 mCurrentState.mIncognito = view.isPrivateBrowsingEnabled();
John Reck1cf4b792011-07-26 10:22:22 -0700695 }
696
Pankaj Garg21dad562015-07-02 17:17:24 -0700697 public String getTouchIconUrl() {
698 return mTouchIconUrl;
699 }
Tarun Nainani8eb00912014-07-17 12:28:32 -0700700
Sagar Dhawan33551ff2015-07-08 17:24:44 -0700701 public boolean isKeyboardShowing() {
702 Controller controller = (Controller)mWebViewController;
703 return (mIsKeyboardUp || controller.getUi().isEditingUrl());
704 }
705
Tarun Nainani8eb00912014-07-17 12:28:32 -0700706 public boolean isTabFullScreen() {
707 return mFullScreen;
708 }
709
Vivek Sekharf96064b2014-07-28 16:32:34 -0700710 protected void setTabFullscreen(boolean fullScreen) {
Tarun Nainani8eb00912014-07-17 12:28:32 -0700711 Controller controller = (Controller)mWebViewController;
Sudheer Koganti24766882014-10-02 10:58:09 -0700712 controller.getUi().showFullscreen(fullScreen);
Tarun Nainani8eb00912014-07-17 12:28:32 -0700713 mFullScreen = fullScreen;
Vivek Sekharf96064b2014-07-28 16:32:34 -0700714 }
715
Sudheer Koganti24766882014-10-02 10:58:09 -0700716 public boolean exitFullscreen() {
717 if (mFullScreen) {
718 Controller controller = (Controller)mWebViewController;
719 controller.getUi().showFullscreen(false);
720 if (getWebView() != null)
721 getWebView().exitFullscreen();
722 mFullScreen = false;
723 return true;
724 }
725 return false;
726 }
727
728
729
730
Grace Kloba22ac16e2009-10-07 18:00:23 -0700731 // -------------------------------------------------------------------------
732 // WebChromeClient implementation for the main WebView
733 // -------------------------------------------------------------------------
734
735 private final WebChromeClient mWebChromeClient = new WebChromeClient() {
736 // Helper method to create a new tab or sub window.
737 private void createWindow(final boolean dialog, final Message msg) {
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700738 this.createWindow(dialog, msg, null, false);
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700739 }
740
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700741 private void createWindow(final boolean dialog, final Message msg, final String url,
742 final boolean opener_suppressed) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700743 WebView.WebViewTransport transport =
744 (WebView.WebViewTransport) msg.obj;
745 if (dialog) {
746 createSubWindow();
Michael Kolb8233fac2010-10-26 16:08:53 -0700747 mWebViewController.attachSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700748 transport.setWebView(mSubView);
749 } else {
Pankaj Garg21dad562015-07-02 17:17:24 -0700750 capture();
751
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700752 final Tab newTab = mWebViewController.openTab(url,
John Reck5949c662011-05-27 09:52:29 -0700753 Tab.this, true, true);
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700754 // This is special case for rendering links on a webpage in
755 // a new tab. If opener is suppressed, the WebContents created
756 // by the content layer are not fully initialized. This check
757 // will prevent content layer from overriding WebContents
758 // created by new tab with the uninitialized instance.
759 if (!opener_suppressed) {
760 transport.setWebView(newTab.getWebView());
761 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700762 }
763 msg.sendToTarget();
764 }
765
766 @Override
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700767 public void toggleFullscreenModeForTab(boolean enterFullscreen) {
768 if (mWebViewController instanceof Controller) {
Vivek Sekharf96064b2014-07-28 16:32:34 -0700769 setTabFullscreen(enterFullscreen);
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700770 }
771 }
772
773 @Override
Tarun Nainani8eb00912014-07-17 12:28:32 -0700774 public void onOffsetsForFullscreenChanged(float topControlsOffsetYPix,
775 float contentOffsetYPix,
776 float overdrawBottomHeightPix) {
777 if (mWebViewController instanceof Controller) {
Vivek Sekhar8ee3abb2014-07-14 12:32:05 -0700778 Controller controller = (Controller)mWebViewController;
Tarun Nainani8eb00912014-07-17 12:28:32 -0700779 controller.getUi().translateTitleBar(topControlsOffsetYPix);
Vivek Sekhar11b40062015-06-24 11:49:04 -0700780 // Resize the viewport if top controls is not visible
Vivek Sekhar2ee19a32015-07-02 17:03:57 -0700781 if (mMainView != null &&
782 (topControlsOffsetYPix == 0.0f || contentOffsetYPix == 0.0f))
Vivek Sekhar11b40062015-06-24 11:49:04 -0700783 ((BrowserWebView)mMainView).enableTopControls(
784 (topControlsOffsetYPix == 0.0f) ? true : false);
Vivek Sekhar8ee3abb2014-07-14 12:32:05 -0700785 }
786 }
787
788 @Override
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700789 public boolean isTabFullScreen() {
Vivek Sekharf96064b2014-07-28 16:32:34 -0700790 return mFullScreen;
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700791 }
792
793 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700794 public boolean onCreateWindow(WebView view, final boolean dialog,
795 final boolean userGesture, final Message resultMsg) {
796 // only allow new window or sub window for the foreground case
797 if (!mInForeground) {
798 return false;
799 }
800 // Short-circuit if we can't create any more tabs or sub windows.
801 if (dialog && mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -0700802 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700803 .setTitle(R.string.too_many_subwindows_dialog_title)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200804 .setIconAttribute(android.R.attr.alertDialogIcon)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700805 .setMessage(R.string.too_many_subwindows_dialog_message)
806 .setPositiveButton(R.string.ok, null)
807 .show();
808 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700809 } else if (!mWebViewController.getTabControl().canCreateNewTab()) {
Michael Kolb14612442011-06-24 13:06:29 -0700810 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700811 .setTitle(R.string.too_many_windows_dialog_title)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200812 .setIconAttribute(android.R.attr.alertDialogIcon)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700813 .setMessage(R.string.too_many_windows_dialog_message)
814 .setPositiveButton(R.string.ok, null)
815 .show();
816 return false;
817 }
818
819 // Short-circuit if this was a user gesture.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800820 if (userGesture || !mSettings.blockPopupWindows()) {
Tarun Nainani4f5137d2015-04-16 17:26:18 -0700821 WebView.WebViewTransport transport =
822 (WebView.WebViewTransport) resultMsg.obj;
823 CreateWindowParams windowParams = transport.getCreateWindowParams();
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700824 if (windowParams.mOpenerSuppressed) {
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700825 createWindow(dialog, resultMsg, windowParams.mURL, true);
826 // This is special case for rendering links on a webpage in
827 // a new tab. If opener is suppressed, the WebContents created
828 // by the content layer are not fully initialized. Returning false
829 // will prevent content layer from overriding WebContents
830 // created by new tab with the uninitialized instance.
831 return false;
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700832 }
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700833
834 createWindow(dialog, resultMsg);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700835 return true;
836 }
837
Tarun Nainani4f5137d2015-04-16 17:26:18 -0700838 createWindow(dialog, resultMsg);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700839 return true;
840 }
841
842 @Override
Patrick Scotteb5061b2009-11-18 15:00:30 -0500843 public void onRequestFocus(WebView view) {
844 if (!mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700845 mWebViewController.switchToTab(Tab.this);
Patrick Scotteb5061b2009-11-18 15:00:30 -0500846 }
847 }
848
849 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700850 public void onCloseWindow(WebView window) {
Michael Kolbc831b632011-05-11 09:30:34 -0700851 if (mParent != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700852 // JavaScript can only close popup window.
853 if (mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700854 mWebViewController.switchToTab(mParent);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700855 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700856 mWebViewController.closeTab(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700857 }
858 }
859
860 @Override
861 public void onProgressChanged(WebView view, int newProgress) {
John Reck30c714c2010-12-16 17:30:34 -0800862 mPageLoadProgress = newProgress;
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800863 if (newProgress == 100) {
864 mInPageLoad = false;
865 }
John Reck30c714c2010-12-16 17:30:34 -0800866 mWebViewController.onProgressChanged(Tab.this);
Michael Kolb72864272012-05-03 15:42:15 -0700867 if (mUpdateThumbnail && newProgress == 100) {
868 mUpdateThumbnail = false;
869 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700870 }
871
872 @Override
Leon Scroggins21d9b902010-03-11 09:33:11 -0500873 public void onReceivedTitle(WebView view, final String title) {
John Reck30c714c2010-12-16 17:30:34 -0800874 mCurrentState.mTitle = title;
Michael Kolb8233fac2010-10-26 16:08:53 -0700875 mWebViewController.onReceivedTitle(Tab.this, title);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700876 }
877
878 @Override
879 public void onReceivedIcon(WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -0800880 mCurrentState.mFavicon = icon;
Michael Kolb8233fac2010-10-26 16:08:53 -0700881 mWebViewController.onFavicon(Tab.this, view, icon);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700882 }
883
884 @Override
885 public void onReceivedTouchIconUrl(WebView view, String url,
886 boolean precomposed) {
Michael Kolb14612442011-06-24 13:06:29 -0700887 final ContentResolver cr = mContext.getContentResolver();
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400888 // Let precomposed icons take precedence over non-composed
889 // icons.
890 if (precomposed && mTouchIconLoader != null) {
891 mTouchIconLoader.cancel(false);
892 mTouchIconLoader = null;
893 }
894 // Have only one async task at a time.
895 if (mTouchIconLoader == null) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700896 mTouchIconLoader = new DownloadTouchIcon(Tab.this,
Michael Kolb14612442011-06-24 13:06:29 -0700897 mContext, cr, view);
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400898 mTouchIconLoader.execute(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700899 }
Pankaj Garg21dad562015-07-02 17:17:24 -0700900 mTouchIconUrl = url;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700901 }
902
903 @Override
904 public void onShowCustomView(View view,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800905 CustomViewCallback callback) {
Michael Kolb14612442011-06-24 13:06:29 -0700906 Activity activity = mWebViewController.getActivity();
907 if (activity != null) {
908 onShowCustomView(view, activity.getRequestedOrientation(), callback);
909 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400910 }
911
912 @Override
913 public void onShowCustomView(View view, int requestedOrientation,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800914 CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700915 if (mInForeground) mWebViewController.showCustomView(Tab.this, view,
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400916 requestedOrientation, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700917 }
918
919 @Override
920 public void onHideCustomView() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700921 if (mInForeground) mWebViewController.hideCustomView();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700922 }
923
924 /**
925 * The origin has exceeded its database quota.
926 * @param url the URL that exceeded the quota
927 * @param databaseIdentifier the identifier of the database on which the
928 * transaction that caused the quota overflow was run
929 * @param currentQuota the current quota for the origin.
930 * @param estimatedSize the estimated size of the database.
931 * @param totalUsedQuota is the sum of all origins' quota.
932 * @param quotaUpdater The callback to run when a decision to allow or
933 * deny quota has been made. Don't forget to call this!
934 */
935 @Override
936 public void onExceededDatabaseQuota(String url,
937 String databaseIdentifier, long currentQuota, long estimatedSize,
938 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -0700939 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -0700940 .onExceededDatabaseQuota(url, databaseIdentifier,
941 currentQuota, estimatedSize, totalUsedQuota,
942 quotaUpdater);
943 }
944
945 /**
946 * The Application Cache has exceeded its max size.
947 * @param spaceNeeded is the amount of disk space that would be needed
948 * in order for the last appcache operation to succeed.
949 * @param totalUsedQuota is the sum of all origins' quota.
950 * @param quotaUpdater A callback to inform the WebCore thread that a
951 * new app cache size is available. This callback must always
952 * be executed at some point to ensure that the sleeping
953 * WebCore thread is woken up.
954 */
955 @Override
956 public void onReachedMaxAppCacheSize(long spaceNeeded,
957 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -0700958 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -0700959 .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota,
960 quotaUpdater);
961 }
962
Ben Murdoch65acc352009-11-19 18:16:04 +0000963 /* Adds a JavaScript error message to the system log and if the JS
964 * console is enabled in the about:debug options, to that console
965 * also.
Ben Murdochc42addf2010-01-28 15:19:59 +0000966 * @param consoleMessage the message object.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700967 */
968 @Override
Ben Murdochc42addf2010-01-28 15:19:59 +0000969 public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Jeff Hamilton47654f42010-09-07 09:57:51 -0500970 // Don't log console messages in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -0700971 if (isPrivateBrowsingEnabled()) return true;
Jeff Hamilton47654f42010-09-07 09:57:51 -0500972
Ben Murdochc42addf2010-01-28 15:19:59 +0000973 String message = "Console: " + consoleMessage.message() + " "
974 + consoleMessage.sourceId() + ":"
975 + consoleMessage.lineNumber();
976
977 switch (consoleMessage.messageLevel()) {
978 case TIP:
979 Log.v(CONSOLE_LOGTAG, message);
980 break;
981 case LOG:
982 Log.i(CONSOLE_LOGTAG, message);
983 break;
984 case WARNING:
985 Log.w(CONSOLE_LOGTAG, message);
986 break;
987 case ERROR:
988 Log.e(CONSOLE_LOGTAG, message);
989 break;
990 case DEBUG:
991 Log.d(CONSOLE_LOGTAG, message);
992 break;
993 }
994
995 return true;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700996 }
997
998 /**
999 * Ask the browser for an icon to represent a <video> element.
1000 * This icon will be used if the Web page did not specify a poster attribute.
1001 * @return Bitmap The icon or null if no such icon is available.
1002 */
1003 @Override
1004 public Bitmap getDefaultVideoPoster() {
1005 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001006 return mWebViewController.getDefaultVideoPoster();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001007 }
1008 return null;
1009 }
1010
1011 /**
1012 * Ask the host application for a custom progress view to show while
1013 * a <video> is loading.
1014 * @return View The progress view.
1015 */
1016 @Override
1017 public View getVideoLoadingProgressView() {
1018 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001019 return mWebViewController.getVideoLoadingProgressView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001020 }
1021 return null;
1022 }
1023
1024 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00001025 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001026 if (mInForeground) {
Ben Murdoch8cad4132012-01-11 10:56:43 +00001027 mWebViewController.openFileChooser(uploadMsg, acceptType, capture);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001028 } else {
1029 uploadMsg.onReceiveValue(null);
1030 }
1031 }
1032
Vivek Sekharb54614f2014-05-01 19:03:37 -07001033 @Override
1034 public void showFileChooser(ValueCallback<String[]> uploadFilePaths, String acceptTypes,
1035 boolean capture) {
1036 if (mInForeground) {
1037 mWebViewController.showFileChooser(uploadFilePaths, acceptTypes, capture);
1038 } else {
1039 uploadFilePaths.onReceiveValue(null);
1040 }
1041 }
1042
Grace Kloba22ac16e2009-10-07 18:00:23 -07001043 /**
1044 * Deliver a list of already-visited URLs
1045 */
1046 @Override
1047 public void getVisitedHistory(final ValueCallback<String[]> callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001048 mWebViewController.getVisitedHistory(callback);
1049 }
Ben Murdoch8029a772010-11-16 11:58:21 +00001050
1051 @Override
1052 public void setupAutoFill(Message message) {
1053 // Prompt the user to set up their profile.
1054 final Message msg = message;
Michael Kolb14612442011-06-24 13:06:29 -07001055 AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
1056 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
Ben Murdoch1d676b62011-01-17 12:54:24 +00001057 Context.LAYOUT_INFLATER_SERVICE);
1058 final View layout = inflater.inflate(R.layout.setup_autofill_dialog, null);
1059
1060 builder.setView(layout)
1061 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1062 @Override
1063 public void onClick(DialogInterface dialog, int id) {
1064 CheckBox disableAutoFill = (CheckBox) layout.findViewById(
1065 R.id.setup_autofill_dialog_disable_autofill);
1066
1067 if (disableAutoFill.isChecked()) {
1068 // Disable autofill and show a toast with how to turn it on again.
John Reck35e9dd62011-04-25 09:01:54 -07001069 mSettings.setAutofillEnabled(false);
Michael Kolb14612442011-06-24 13:06:29 -07001070 Toast.makeText(mContext,
Ben Murdoch1d676b62011-01-17 12:54:24 +00001071 R.string.autofill_setup_dialog_negative_toast,
1072 Toast.LENGTH_LONG).show();
1073 } else {
1074 // Take user to the AutoFill profile editor. When they return,
1075 // we will send the message that we pass here which will trigger
1076 // the form to get filled out with their new profile.
1077 mWebViewController.setupAutoFill(msg);
1078 }
1079 }
1080 })
1081 .setNegativeButton(R.string.cancel, null)
1082 .show();
Ben Murdoch8029a772010-11-16 11:58:21 +00001083 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001084 };
1085
1086 // -------------------------------------------------------------------------
1087 // WebViewClient implementation for the sub window
1088 // -------------------------------------------------------------------------
1089
1090 // Subclass of WebViewClient used in subwindows to notify the main
1091 // WebViewClient of certain WebView activities.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001092 private static class SubWindowClient extends WebViewClient {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001093 // The main WebViewClient.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001094 private final WebViewClient mClient;
Michael Kolb8233fac2010-10-26 16:08:53 -07001095 private final WebViewController mController;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001096
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001097 SubWindowClient(WebViewClient client, WebViewController controller) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001098 mClient = client;
Michael Kolb8233fac2010-10-26 16:08:53 -07001099 mController = controller;
Leon Scroggins III211ba542010-04-19 13:21:13 -04001100 }
1101 @Override
1102 public void onPageStarted(WebView view, String url, Bitmap favicon) {
1103 // Unlike the others, do not call mClient's version, which would
1104 // change the progress bar. However, we do want to remove the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001105 // find or select dialog.
Michael Kolb8233fac2010-10-26 16:08:53 -07001106 mController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001107 }
1108 @Override
1109 public void doUpdateVisitedHistory(WebView view, String url,
1110 boolean isReload) {
1111 mClient.doUpdateVisitedHistory(view, url, isReload);
1112 }
1113 @Override
1114 public boolean shouldOverrideUrlLoading(WebView view, String url) {
1115 return mClient.shouldOverrideUrlLoading(view, url);
1116 }
1117 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -07001118 public void onReceivedHttpAuthRequest(WebView view,
1119 HttpAuthHandler handler, String host, String realm) {
1120 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
1121 }
1122 @Override
1123 public void onFormResubmission(WebView view, Message dontResend,
1124 Message resend) {
1125 mClient.onFormResubmission(view, dontResend, resend);
1126 }
1127 @Override
1128 public void onReceivedError(WebView view, int errorCode,
1129 String description, String failingUrl) {
1130 mClient.onReceivedError(view, errorCode, description, failingUrl);
1131 }
1132 @Override
1133 public boolean shouldOverrideKeyEvent(WebView view,
1134 android.view.KeyEvent event) {
1135 return mClient.shouldOverrideKeyEvent(view, event);
1136 }
1137 @Override
1138 public void onUnhandledKeyEvent(WebView view,
1139 android.view.KeyEvent event) {
1140 mClient.onUnhandledKeyEvent(view, event);
1141 }
1142 }
1143
1144 // -------------------------------------------------------------------------
1145 // WebChromeClient implementation for the sub window
1146 // -------------------------------------------------------------------------
1147
1148 private class SubWindowChromeClient extends WebChromeClient {
1149 // The main WebChromeClient.
1150 private final WebChromeClient mClient;
1151
1152 SubWindowChromeClient(WebChromeClient client) {
1153 mClient = client;
1154 }
1155 @Override
1156 public void onProgressChanged(WebView view, int newProgress) {
1157 mClient.onProgressChanged(view, newProgress);
1158 }
1159 @Override
1160 public boolean onCreateWindow(WebView view, boolean dialog,
1161 boolean userGesture, android.os.Message resultMsg) {
1162 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
1163 }
1164 @Override
1165 public void onCloseWindow(WebView window) {
1166 if (window != mSubView) {
1167 Log.e(LOGTAG, "Can't close the window");
1168 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001169 mWebViewController.dismissSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001170 }
1171 }
1172
1173 // -------------------------------------------------------------------------
1174
1175 // Construct a new tab
Michael Kolb7bcafde2011-05-09 13:55:59 -07001176 Tab(WebViewController wvcontroller, WebView w) {
John Reck1cf4b792011-07-26 10:22:22 -07001177 this(wvcontroller, w, null);
1178 }
1179
1180 Tab(WebViewController wvcontroller, Bundle state) {
1181 this(wvcontroller, null, state);
1182 }
1183
1184 Tab(WebViewController wvcontroller, WebView w, Bundle state) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001185 mWebViewController = wvcontroller;
Michael Kolb14612442011-06-24 13:06:29 -07001186 mContext = mWebViewController.getContext();
John Reck35e9dd62011-04-25 09:01:54 -07001187 mSettings = BrowserSettings.getInstance();
Michael Kolb14612442011-06-24 13:06:29 -07001188 mDataController = DataController.getInstance(mContext);
1189 mCurrentState = new PageState(mContext, w != null
John Reck847b5322011-04-14 17:02:18 -07001190 ? w.isPrivateBrowsingEnabled() : false);
Tarun Nainani8084c822014-06-25 13:38:06 -07001191 setTimeStamp();
Michael Kolb8233fac2010-10-26 16:08:53 -07001192 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001193 mInForeground = false;
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001194 mWebViewDestroyedByMemoryMonitor = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001195
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001196 mDownloadListener = new BrowserDownloadListener() {
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001197 public void onDownloadStart(String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001198 String contentDisposition, String mimetype, String referer,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001199 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001200 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001201 mimetype, referer, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001202 }
1203 };
1204
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001205 mCaptureWidth = mContext.getResources().getDimensionPixelSize(R.dimen.tab_thumbnail_width);
1206 mCaptureHeight =mContext.getResources().getDimensionPixelSize(R.dimen.tab_thumbnail_height);
1207
1208 initCaptureBitmap();
1209
John Reck1cf4b792011-07-26 10:22:22 -07001210 restoreState(state);
John Reck52be4782011-08-26 15:37:29 -07001211 if (getId() == -1) {
1212 mId = TabControl.getNextId();
1213 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001214 setWebView(w);
Michael Kolb9ef259a2011-07-12 15:33:08 -07001215 mHandler = new Handler() {
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001216 @Override
Michael Kolb9ef259a2011-07-12 15:33:08 -07001217 public void handleMessage(Message m) {
John Reck1cf4b792011-07-26 10:22:22 -07001218 switch (m.what) {
1219 case MSG_CAPTURE:
1220 capture();
1221 break;
1222 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001223 }
1224 };
Pankaj Garg1c13cab2015-05-12 11:52:17 -07001225
1226 mFirstPixelObservable = new Observable();
1227 mFirstPixelObservable.set(false);
1228 mTabHistoryUpdateObservable = new Observable();
John Reck1cf4b792011-07-26 10:22:22 -07001229 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001230
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001231 private void initCaptureBitmap() {
1232 mCapture = Bitmap.createBitmap(mCaptureWidth, mCaptureHeight, Bitmap.Config.RGB_565);
1233 mCapture.eraseColor(Color.WHITE);
Michael Kolb72864272012-05-03 15:42:15 -07001234 }
1235
Mathew Inwoode09305e2011-09-02 12:03:26 +01001236 /**
1237 * This is used to get a new ID when the tab has been preloaded, before it is displayed and
1238 * added to TabControl. Preloaded tabs can be created before restoreInstanceState, leading
1239 * to overlapping IDs between the preloaded and restored tabs.
1240 */
1241 public void refreshIdAfterPreload() {
1242 mId = TabControl.getNextId();
1243 }
1244
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001245 public void setController(WebViewController ctl) {
1246 mWebViewController = ctl;
1247
John Reck1cf4b792011-07-26 10:22:22 -07001248 if (mWebViewController.shouldCaptureThumbnails()) {
1249 synchronized (Tab.this) {
1250 if (mCapture == null) {
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001251 initCaptureBitmap();
1252 if (mInForeground && !mHandler.hasMessages(MSG_CAPTURE)) {
1253 mHandler.sendEmptyMessageDelayed(MSG_CAPTURE, CAPTURE_DELAY);
John Reck1cf4b792011-07-26 10:22:22 -07001254 }
1255 }
1256 }
1257 } else {
1258 synchronized (Tab.this) {
1259 mCapture = null;
1260 deleteThumbnail();
1261 }
1262 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001263 }
1264
Michael Kolbc831b632011-05-11 09:30:34 -07001265 public long getId() {
1266 return mId;
1267 }
1268
Michael Kolb91911a22012-01-17 11:21:25 -08001269 void setWebView(WebView w) {
1270 setWebView(w, true);
1271 }
1272
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001273 public boolean isNativeActive(){
1274 if (mMainView == null)
1275 return false;
1276 return true;
1277 }
1278
1279 public void setTimeStamp(){
1280 Date d = new Date();
1281 timestamp = (new Timestamp(d.getTime()));
1282 }
1283
1284 public Timestamp getTimestamp() {
1285 return timestamp;
1286 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001287 /**
1288 * Sets the WebView for this tab, correctly removing the old WebView from
1289 * the container view.
1290 */
Michael Kolb91911a22012-01-17 11:21:25 -08001291 void setWebView(WebView w, boolean restore) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001292 if (mMainView == w) {
1293 return;
1294 }
Michael Kolba713ec82010-11-29 17:27:06 -08001295
Michael Kolba713ec82010-11-29 17:27:06 -08001296 mWebViewController.onSetWebView(this, w);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001297
John Reck1cf4b792011-07-26 10:22:22 -07001298 if (mMainView != null) {
John Reck8ee633f2011-08-09 16:00:35 -07001299 mMainView.setPictureListener(null);
John Reck1cf4b792011-07-26 10:22:22 -07001300 if (w != null) {
1301 syncCurrentState(w, null);
1302 } else {
Panos Thomasa9a5a582014-03-18 19:20:08 -07001303 mCurrentState = new PageState(mContext, mMainView.isPrivateBrowsingEnabled());
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001304
1305 if (mWebViewDestroyedByMemoryMonitor) {
1306 /*
1307 * If tab was destroyed as a result of the MemoryMonitor
1308 * then we need to restore the state properties
1309 * from the old WebView (mMainView)
1310 */
1311 syncCurrentState(mMainView, null);
1312 mWebViewDestroyedByMemoryMonitor = false;
1313 }
John Reck1cf4b792011-07-26 10:22:22 -07001314 }
1315 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001316 // set the new one
1317 mMainView = w;
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001318
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001319 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001320 if (mMainView != null) {
1321 mMainView.setWebViewClient(mWebViewClient);
1322 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001323 // Attach DownloadManager so that downloads can start in an active
1324 // or a non-active window. This can happen when going to a site that
1325 // does a redirect after a period of time. The user could have
1326 // switched to another tab while waiting for the download to start.
1327 mMainView.setDownloadListener(mDownloadListener);
John Reck8ee633f2011-08-09 16:00:35 -07001328 TabControl tc = mWebViewController.getTabControl();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001329 if (tc != null /*&& tc.getOnThumbnailUpdatedListener() != null*/) {
John Reck8ee633f2011-08-09 16:00:35 -07001330 mMainView.setPictureListener(this);
1331 }
Michael Kolb91911a22012-01-17 11:21:25 -08001332 if (restore && (mSavedState != null)) {
John Reck8b9bb8b2012-03-08 13:19:40 -08001333 restoreUserAgent();
John Reck6c2e2f32011-08-22 13:41:23 -07001334 WebBackForwardList restoredState
1335 = mMainView.restoreState(mSavedState);
1336 if (restoredState == null || restoredState.getSize() == 0) {
1337 Log.w(LOGTAG, "Failed to restore WebView state!");
1338 loadUrl(mCurrentState.mOriginalUrl, null);
1339 }
John Reck1cf4b792011-07-26 10:22:22 -07001340 mSavedState = null;
1341 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001342 }
1343 }
1344
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001345 public void destroyThroughMemoryMonitor() {
1346 mWebViewDestroyedByMemoryMonitor = true;
1347 destroy();
1348 }
1349
Grace Kloba22ac16e2009-10-07 18:00:23 -07001350 /**
1351 * Destroy the tab's main WebView and subWindow if any
1352 */
1353 void destroy() {
Tarun Nainani2c1dd7c2014-07-05 16:40:12 -07001354
Matthew Huib7f2e9c2014-04-16 11:12:37 -04001355 if (mPostponeDestroy) {
1356 mShouldDestroy = true;
1357 return;
1358 }
1359 mShouldDestroy = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001360 if (mMainView != null) {
1361 dismissSubWindow();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001362 // save the WebView to call destroy() after detach it from the tab
Vivek Sekhar53ef8932015-06-18 16:51:43 -07001363 final WebView webView = mMainView;
Vivek Sekhar2ee19a32015-07-02 17:03:57 -07001364 setWebView(null);
Site Mao61b68212015-07-16 10:56:31 -07001365 if (!mWebViewDestroyedByMemoryMonitor && !BaseUi.isUiLowPowerMode()) {
Vivek Sekhar2ee19a32015-07-02 17:03:57 -07001366 // Tabs can be reused with new instance of WebView so delete the snapshots
Vivek Sekhar53ef8932015-06-18 16:51:43 -07001367 webView.getSnapshotIds(new ValueCallback<List<Integer>>() {
1368 @Override
1369 public void onReceiveValue(List<Integer> ids) {
1370 int currentTabIdx = mWebViewController.getTabControl().getCurrentPosition();
1371 for (Integer id : ids) {
1372 if (getTabIdxFromCaptureIdx(id) == currentTabIdx) {
1373 webView.deleteSnapshot(id);
1374 }
1375 }
Vivek Sekhar53ef8932015-06-18 16:51:43 -07001376 webView.destroy();
Pankaj Garg1c13cab2015-05-12 11:52:17 -07001377 }
Vivek Sekhar53ef8932015-06-18 16:51:43 -07001378 });
Vivek Sekhar2ee19a32015-07-02 17:03:57 -07001379 } else {
1380 webView.destroy();
Pankaj Garg1c13cab2015-05-12 11:52:17 -07001381 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001382 }
1383 }
1384
Matthew Huib7f2e9c2014-04-16 11:12:37 -04001385 private boolean mPostponeDestroy = false;
1386 private boolean mShouldDestroy = false;
1387
1388 public void postponeDestroy() {
1389 mPostponeDestroy = true;
1390 }
1391
1392 public void performPostponedDestroy() {
1393 mPostponeDestroy = false;
1394 if (mShouldDestroy) {
1395 destroy();
1396 }
1397 }
1398
Grace Kloba22ac16e2009-10-07 18:00:23 -07001399 /**
1400 * Remove the tab from the parent
1401 */
1402 void removeFromTree() {
1403 // detach the children
Michael Kolbc831b632011-05-11 09:30:34 -07001404 if (mChildren != null) {
1405 for(Tab t : mChildren) {
1406 t.setParent(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001407 }
1408 }
1409 // remove itself from the parent list
Michael Kolbc831b632011-05-11 09:30:34 -07001410 if (mParent != null) {
1411 mParent.mChildren.remove(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001412 }
Tarun Nainani8eb00912014-07-17 12:28:32 -07001413
1414 mCapture = null;
John Reck1cf4b792011-07-26 10:22:22 -07001415 deleteThumbnail();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001416 }
1417
1418 /**
1419 * Create a new subwindow unless a subwindow already exists.
1420 * @return True if a new subwindow was created. False if one already exists.
1421 */
1422 boolean createSubWindow() {
1423 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001424 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001425 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001426 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001427 mSubView.setWebChromeClient(new SubWindowChromeClient(
1428 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001429 // Set a different DownloadListener for the mSubView, since it will
1430 // just need to dismiss the mSubView, rather than close the Tab
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001431 mSubView.setDownloadListener(new BrowserDownloadListener() {
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001432 public void onDownloadStart(String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001433 String contentDisposition, String mimetype, String referer,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001434 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001435 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001436 contentDisposition, mimetype, referer, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001437 if (mSubView.copyBackForwardList().getSize() == 0) {
1438 // This subwindow was opened for the sole purpose of
1439 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001440 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001441 }
1442 }
1443 });
Michael Kolb14612442011-06-24 13:06:29 -07001444 mSubView.setOnCreateContextMenuListener(mWebViewController.getActivity());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001445 return true;
1446 }
1447 return false;
1448 }
1449
1450 /**
1451 * Dismiss the subWindow for the tab.
1452 */
1453 void dismissSubWindow() {
1454 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001455 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001456 mSubView.destroy();
1457 mSubView = null;
1458 mSubViewContainer = null;
1459 }
1460 }
1461
Grace Kloba22ac16e2009-10-07 18:00:23 -07001462
1463 /**
1464 * Set the parent tab of this tab.
1465 */
Michael Kolbc831b632011-05-11 09:30:34 -07001466 void setParent(Tab parent) {
John Reck52be4782011-08-26 15:37:29 -07001467 if (parent == this) {
1468 throw new IllegalStateException("Cannot set parent to self!");
1469 }
Michael Kolbc831b632011-05-11 09:30:34 -07001470 mParent = parent;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001471 // This tab may have been freed due to low memory. If that is the case,
Michael Kolbc831b632011-05-11 09:30:34 -07001472 // the parent tab id is already saved. If we are changing that id
Grace Kloba22ac16e2009-10-07 18:00:23 -07001473 // (most likely due to removing the parent tab) we must update the
Michael Kolbc831b632011-05-11 09:30:34 -07001474 // parent tab id in the saved Bundle.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001475 if (mSavedState != null) {
1476 if (parent == null) {
1477 mSavedState.remove(PARENTTAB);
1478 } else {
Michael Kolbc831b632011-05-11 09:30:34 -07001479 mSavedState.putLong(PARENTTAB, parent.getId());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001480 }
1481 }
John Reckb0a86db2011-05-24 14:05:58 -07001482
1483 // Sync the WebView useragent with the parent
1484 if (parent != null && mSettings.hasDesktopUseragent(parent.getWebView())
1485 != mSettings.hasDesktopUseragent(getWebView())) {
1486 mSettings.toggleDesktopUseragent(getWebView());
1487 }
John Reck52be4782011-08-26 15:37:29 -07001488
1489 if (parent != null && parent.getId() == getId()) {
1490 throw new IllegalStateException("Parent has same ID as child!");
1491 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001492 }
1493
1494 /**
Michael Kolbc831b632011-05-11 09:30:34 -07001495 * If this Tab was created through another Tab, then this method returns
1496 * that Tab.
1497 * @return the Tab parent or null
1498 */
1499 public Tab getParent() {
1500 return mParent;
1501 }
1502
1503 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001504 * When a Tab is created through the content of another Tab, then we
1505 * associate the Tabs.
1506 * @param child the Tab that was created from this Tab
1507 */
1508 void addChildTab(Tab child) {
Michael Kolbc831b632011-05-11 09:30:34 -07001509 if (mChildren == null) {
1510 mChildren = new Vector<Tab>();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001511 }
Michael Kolbc831b632011-05-11 09:30:34 -07001512 mChildren.add(child);
1513 child.setParent(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001514 }
1515
Michael Kolbc831b632011-05-11 09:30:34 -07001516 Vector<Tab> getChildren() {
1517 return mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001518 }
1519
1520 void resume() {
1521 if (mMainView != null) {
John Reck56c1fcf2011-08-17 10:15:16 -07001522 setupHwAcceleration(mMainView);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001523 mMainView.onResume();
1524 if (mSubView != null) {
1525 mSubView.onResume();
1526 }
1527 }
1528 }
1529
John Reck56c1fcf2011-08-17 10:15:16 -07001530 private void setupHwAcceleration(View web) {
1531 if (web == null) return;
1532 BrowserSettings settings = BrowserSettings.getInstance();
1533 if (settings.isHardwareAccelerated()) {
1534 web.setLayerType(View.LAYER_TYPE_NONE, null);
1535 } else {
1536 web.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
1537 }
1538 }
1539
Grace Kloba22ac16e2009-10-07 18:00:23 -07001540 void pause() {
1541 if (mMainView != null) {
1542 mMainView.onPause();
1543 if (mSubView != null) {
1544 mSubView.onPause();
1545 }
1546 }
1547 }
1548
1549 void putInForeground() {
John Reck8ee633f2011-08-09 16:00:35 -07001550 if (mInForeground) {
1551 return;
1552 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001553 mInForeground = true;
1554 resume();
Michael Kolb14612442011-06-24 13:06:29 -07001555 Activity activity = mWebViewController.getActivity();
1556 mMainView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001557 if (mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -07001558 mSubView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001559 }
Axesh R. Ajmerac6b5c322015-05-01 11:06:10 -07001560
Leon Scroggins1961ed22010-12-07 15:22:21 -05001561 mWebViewController.bookmarkedStatusHasChanged(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001562 }
1563
1564 void putInBackground() {
John Reck8ee633f2011-08-09 16:00:35 -07001565 if (!mInForeground) {
1566 return;
1567 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001568 mInForeground = false;
1569 pause();
1570 mMainView.setOnCreateContextMenuListener(null);
1571 if (mSubView != null) {
1572 mSubView.setOnCreateContextMenuListener(null);
1573 }
1574 }
1575
Michael Kolb8233fac2010-10-26 16:08:53 -07001576 boolean inForeground() {
1577 return mInForeground;
1578 }
1579
Grace Kloba22ac16e2009-10-07 18:00:23 -07001580 /**
1581 * Return the top window of this tab; either the subwindow if it is not
1582 * null or the main window.
1583 * @return The top window of this tab.
1584 */
1585 WebView getTopWindow() {
1586 if (mSubView != null) {
1587 return mSubView;
1588 }
1589 return mMainView;
1590 }
1591
1592 /**
1593 * Return the main window of this tab. Note: if a tab is freed in the
1594 * background, this can return null. It is only guaranteed to be
1595 * non-null for the current tab.
1596 * @return The main WebView of this tab.
1597 */
1598 WebView getWebView() {
1599 return mMainView;
1600 }
1601
Michael Kolba713ec82010-11-29 17:27:06 -08001602 void setViewContainer(View container) {
1603 mContainer = container;
1604 }
1605
Michael Kolb8233fac2010-10-26 16:08:53 -07001606 View getViewContainer() {
1607 return mContainer;
1608 }
1609
Grace Kloba22ac16e2009-10-07 18:00:23 -07001610 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001611 * Return whether private browsing is enabled for the main window of
1612 * this tab.
1613 * @return True if private browsing is enabled.
1614 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001615 boolean isPrivateBrowsingEnabled() {
John Reck502a3532011-08-16 14:21:46 -07001616 return mCurrentState.mIncognito;
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001617 }
1618
1619 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001620 * Return the subwindow of this tab or null if there is no subwindow.
1621 * @return The subwindow of this tab or null.
1622 */
1623 WebView getSubWebView() {
1624 return mSubView;
1625 }
1626
Michael Kolb1514bb72010-11-22 09:11:48 -08001627 void setSubWebView(WebView subView) {
1628 mSubView = subView;
1629 }
1630
Michael Kolb8233fac2010-10-26 16:08:53 -07001631 View getSubViewContainer() {
1632 return mSubViewContainer;
1633 }
1634
Michael Kolb1514bb72010-11-22 09:11:48 -08001635 void setSubViewContainer(View subViewContainer) {
1636 mSubViewContainer = subViewContainer;
1637 }
1638
Grace Kloba22ac16e2009-10-07 18:00:23 -07001639
1640 /**
1641 * @return The application id string
1642 */
1643 String getAppId() {
1644 return mAppId;
1645 }
1646
1647 /**
1648 * Set the application id string
1649 * @param id
1650 */
1651 void setAppId(String id) {
1652 mAppId = id;
1653 }
1654
Michael Kolbe28b3472011-08-04 16:54:31 -07001655 boolean closeOnBack() {
1656 return mCloseOnBack;
1657 }
1658
1659 void setCloseOnBack(boolean close) {
1660 mCloseOnBack = close;
1661 }
1662
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001663 boolean getDerivedFromIntent() {
1664 return mDerivedFromIntent;
1665 }
1666
1667 void setDerivedFromIntent(boolean derived) {
1668 mDerivedFromIntent = derived;
1669 }
1670
Grace Kloba22ac16e2009-10-07 18:00:23 -07001671 String getUrl() {
John Reck324d4402011-01-11 16:56:42 -08001672 return UrlUtils.filteredUrl(mCurrentState.mUrl);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001673 }
1674
Tarun Nainani8eb00912014-07-17 12:28:32 -07001675
1676 protected void onPageFinished() {
1677 mPageFinished = true;
Ze G Riande2a675c22015-06-03 11:15:24 -07001678 isDistillable();
Tarun Nainani8eb00912014-07-17 12:28:32 -07001679 }
1680
1681 public boolean getPageFinishedStatus() {
1682 return mPageFinished;
1683 }
1684
John Reck49a603c2011-03-03 09:33:05 -08001685 String getOriginalUrl() {
John Reckdb22ec42011-06-29 11:31:24 -07001686 if (mCurrentState.mOriginalUrl == null) {
1687 return getUrl();
John Reck49a603c2011-03-03 09:33:05 -08001688 }
John Reckdb22ec42011-06-29 11:31:24 -07001689 return UrlUtils.filteredUrl(mCurrentState.mOriginalUrl);
John Reck49a603c2011-03-03 09:33:05 -08001690 }
1691
Grace Kloba22ac16e2009-10-07 18:00:23 -07001692 /**
John Reck30c714c2010-12-16 17:30:34 -08001693 * Get the title of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001694 */
1695 String getTitle() {
John Reck30c714c2010-12-16 17:30:34 -08001696 return mCurrentState.mTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001697 }
1698
1699 /**
John Reck30c714c2010-12-16 17:30:34 -08001700 * Get the favicon of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001701 */
1702 Bitmap getFavicon() {
John Reck1cf4b792011-07-26 10:22:22 -07001703 if (mCurrentState.mFavicon != null) {
1704 return mCurrentState.mFavicon;
1705 }
1706 return getDefaultFavicon(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001707 }
1708
Pankaj Garg32e1b942015-06-03 18:13:24 -07001709 public boolean hasFavicon() {
1710 return mCurrentState.mFavicon != null;
1711 }
1712
John Recke969cc52010-12-21 17:24:43 -08001713 public boolean isBookmarkedSite() {
1714 return mCurrentState.mIsBookmarkedSite;
1715 }
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001716
Grace Kloba22ac16e2009-10-07 18:00:23 -07001717 /**
Steve Block08a6f0c2011-10-06 12:12:53 +01001718 * Sets the security state, clears the SSL certificate error and informs
1719 * the controller.
1720 */
Steve Block2466eff2011-10-03 15:33:09 +01001721 private void setSecurityState(SecurityState securityState) {
1722 mCurrentState.mSecurityState = securityState;
1723 mWebViewController.onUpdatedSecurityState(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001724 }
1725
1726 /**
Steve Block2466eff2011-10-03 15:33:09 +01001727 * @return The tab's security state.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001728 */
Steve Block2466eff2011-10-03 15:33:09 +01001729 SecurityState getSecurityState() {
1730 return mCurrentState.mSecurityState;
John Reck30c714c2010-12-16 17:30:34 -08001731 }
1732
1733 int getLoadProgress() {
1734 if (mInPageLoad) {
1735 return mPageLoadProgress;
1736 }
1737 return 100;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001738 }
1739
1740 /**
1741 * @return TRUE if onPageStarted is called while onPageFinished is not
1742 * called yet.
1743 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001744 boolean inPageLoad() {
1745 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001746 }
1747
Grace Kloba22ac16e2009-10-07 18:00:23 -07001748 /**
John Reck1cf4b792011-07-26 10:22:22 -07001749 * @return The Bundle with the tab's state if it can be saved, otherwise null
Grace Kloba22ac16e2009-10-07 18:00:23 -07001750 */
John Reck1cf4b792011-07-26 10:22:22 -07001751 public Bundle saveState() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001752 // If the WebView is null it means we ran low on memory and we already
1753 // stored the saved state in mSavedState.
1754 if (mMainView == null) {
John Reck1cf4b792011-07-26 10:22:22 -07001755 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001756 }
John Reck6c2e2f32011-08-22 13:41:23 -07001757
1758 if (TextUtils.isEmpty(mCurrentState.mUrl)) {
John Reck1cf4b792011-07-26 10:22:22 -07001759 return null;
John Reck24f18262011-06-17 14:47:20 -07001760 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001761
1762 mSavedState = new Bundle();
John Reck6c2e2f32011-08-22 13:41:23 -07001763 WebBackForwardList savedList = mMainView.saveState(mSavedState);
1764 if (savedList == null || savedList.getSize() == 0) {
1765 Log.w(LOGTAG, "Failed to save back/forward list for "
1766 + mCurrentState.mUrl);
1767 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001768
Michael Kolbc831b632011-05-11 09:30:34 -07001769 mSavedState.putLong(ID, mId);
John Reck30c714c2010-12-16 17:30:34 -08001770 mSavedState.putString(CURRURL, mCurrentState.mUrl);
1771 mSavedState.putString(CURRTITLE, mCurrentState.mTitle);
John Reck1cf4b792011-07-26 10:22:22 -07001772 mSavedState.putBoolean(INCOGNITO, mMainView.isPrivateBrowsingEnabled());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001773 if (mAppId != null) {
1774 mSavedState.putString(APPID, mAppId);
1775 }
Michael Kolbe28b3472011-08-04 16:54:31 -07001776 mSavedState.putBoolean(CLOSEFLAG, mCloseOnBack);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001777 // Remember the parent tab so the relationship can be restored.
Michael Kolbc831b632011-05-11 09:30:34 -07001778 if (mParent != null) {
1779 mSavedState.putLong(PARENTTAB, mParent.mId);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001780 }
John Reckb0a86db2011-05-24 14:05:58 -07001781 mSavedState.putBoolean(USERAGENT,
1782 mSettings.hasDesktopUseragent(getWebView()));
John Reck1cf4b792011-07-26 10:22:22 -07001783 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001784 }
1785
1786 /*
1787 * Restore the state of the tab.
1788 */
John Reck1cf4b792011-07-26 10:22:22 -07001789 private void restoreState(Bundle b) {
1790 mSavedState = b;
1791 if (mSavedState == null) {
1792 return;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001793 }
1794 // Restore the internal state even if the WebView fails to restore.
1795 // This will maintain the app id, original url and close-on-exit values.
Michael Kolbc831b632011-05-11 09:30:34 -07001796 mId = b.getLong(ID);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001797 mAppId = b.getString(APPID);
Michael Kolbe28b3472011-08-04 16:54:31 -07001798 mCloseOnBack = b.getBoolean(CLOSEFLAG);
John Reck8b9bb8b2012-03-08 13:19:40 -08001799 restoreUserAgent();
John Reck1cf4b792011-07-26 10:22:22 -07001800 String url = b.getString(CURRURL);
1801 String title = b.getString(CURRTITLE);
1802 boolean incognito = b.getBoolean(INCOGNITO);
1803 mCurrentState = new PageState(mContext, incognito, url, null);
1804 mCurrentState.mTitle = title;
1805 synchronized (Tab.this) {
1806 if (mCapture != null) {
John Reck4eadc342011-10-31 14:04:10 -07001807 DataController.getInstance(mContext).loadThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07001808 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001809 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001810 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04001811
John Reck8b9bb8b2012-03-08 13:19:40 -08001812 private void restoreUserAgent() {
1813 if (mMainView == null || mSavedState == null) {
1814 return;
1815 }
1816 if (mSavedState.getBoolean(USERAGENT)
1817 != mSettings.hasDesktopUseragent(mMainView)) {
1818 mSettings.toggleDesktopUseragent(mMainView);
1819 }
1820 }
1821
Leon Scroggins1961ed22010-12-07 15:22:21 -05001822 public void updateBookmarkedStatus() {
John Recke969cc52010-12-21 17:24:43 -08001823 mDataController.queryBookmarkStatus(getUrl(), mIsBookmarkCallback);
Leon Scroggins1961ed22010-12-07 15:22:21 -05001824 }
1825
John Recke969cc52010-12-21 17:24:43 -08001826 private DataController.OnQueryUrlIsBookmark mIsBookmarkCallback
1827 = new DataController.OnQueryUrlIsBookmark() {
1828 @Override
1829 public void onQueryUrlIsBookmark(String url, boolean isBookmark) {
1830 if (mCurrentState.mUrl.equals(url)) {
1831 mCurrentState.mIsBookmarkedSite = isBookmark;
1832 mWebViewController.bookmarkedStatusHasChanged(Tab.this);
1833 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001834 }
John Recke969cc52010-12-21 17:24:43 -08001835 };
Michael Kolb1acef692011-03-08 14:12:06 -08001836
Michael Kolbeb95db42011-03-03 10:38:40 -08001837 public Bitmap getScreenshot() {
John Reck1cf4b792011-07-26 10:22:22 -07001838 synchronized (Tab.this) {
1839 return mCapture;
1840 }
Michael Kolbeb95db42011-03-03 10:38:40 -08001841 }
1842
John Reck541f55a2011-06-07 16:34:43 -07001843 public boolean isSnapshot() {
John Reck541f55a2011-06-07 16:34:43 -07001844 return false;
1845 }
1846
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001847 private static class SaveCallback implements ValueCallback<String> {
1848 boolean onReceiveValueCalled = false;
1849 private String mPath;
John Reck68234a92012-04-19 15:27:12 -07001850
1851 @Override
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001852 public void onReceiveValue(String path) {
1853 this.onReceiveValueCalled = true;
1854 this.mPath = path;
John Reck68234a92012-04-19 15:27:12 -07001855 synchronized (this) {
1856 notifyAll();
John Reck8cc92352011-07-06 17:41:52 -07001857 }
John Reck541f55a2011-06-07 16:34:43 -07001858 }
John Reck68234a92012-04-19 15:27:12 -07001859
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001860 public String getPath() {
1861 return mPath;
1862 }
John Reck68234a92012-04-19 15:27:12 -07001863 }
1864
1865 /**
1866 * Must be called on the UI thread
1867 */
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001868 public ContentValues createSnapshotValues(Bitmap bm) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001869 WebView web = getWebView();
John Reck68234a92012-04-19 15:27:12 -07001870 if (web == null) return null;
John Reckd8c74522011-06-14 08:45:00 -07001871 ContentValues values = new ContentValues();
1872 values.put(Snapshots.TITLE, mCurrentState.mTitle);
1873 values.put(Snapshots.URL, mCurrentState.mUrl);
John Reck68234a92012-04-19 15:27:12 -07001874 values.put(Snapshots.BACKGROUND, web.getPageBackgroundColor());
John Reck8cc92352011-07-06 17:41:52 -07001875 values.put(Snapshots.DATE_CREATED, System.currentTimeMillis());
1876 values.put(Snapshots.FAVICON, compressBitmap(getFavicon()));
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001877 values.put(Snapshots.THUMBNAIL, compressBitmap(bm));
John Reckd8c74522011-06-14 08:45:00 -07001878 return values;
John Reck541f55a2011-06-07 16:34:43 -07001879 }
1880
John Reck68234a92012-04-19 15:27:12 -07001881 /**
1882 * Probably want to call this on a background thread
1883 */
1884 public boolean saveViewState(ContentValues values) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001885 WebView web = getWebView();
John Reck68234a92012-04-19 15:27:12 -07001886 if (web == null) return false;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001887 String filename = UUID.randomUUID().toString();
John Reck68234a92012-04-19 15:27:12 -07001888 SaveCallback callback = new SaveCallback();
John Reck68234a92012-04-19 15:27:12 -07001889 try {
John Reck68234a92012-04-19 15:27:12 -07001890 synchronized (callback) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001891 web.saveViewState(filename, callback);
1892 callback.wait();
John Reck68234a92012-04-19 15:27:12 -07001893 }
John Reck68234a92012-04-19 15:27:12 -07001894 } catch (Exception e) {
1895 Log.w(LOGTAG, "Failed to save view state", e);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001896 String path = callback.getPath();
1897 if (path != null) {
1898 File file = mContext.getFileStreamPath(path);
1899 if (file.exists() && !file.delete()) {
1900 file.deleteOnExit();
1901 }
John Reck68234a92012-04-19 15:27:12 -07001902 }
1903 return false;
1904 }
Tarun Nainani8eb00912014-07-17 12:28:32 -07001905
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001906 String path = callback.getPath();
Tarun Nainani8eb00912014-07-17 12:28:32 -07001907 // could be that saving of file failed
1908 if (path == null) {
1909 return false;
1910 }
1911
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001912 File savedFile = new File(path);
1913 if (!savedFile.exists()) {
1914 return false;
John Reck68234a92012-04-19 15:27:12 -07001915 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001916 values.put(Snapshots.VIEWSTATE_PATH, path.substring(path.lastIndexOf('/') + 1));
1917 values.put(Snapshots.VIEWSTATE_SIZE, savedFile.length());
John Reck68234a92012-04-19 15:27:12 -07001918 return true;
1919 }
1920
John Reck8cc92352011-07-06 17:41:52 -07001921 public byte[] compressBitmap(Bitmap bitmap) {
1922 if (bitmap == null) {
1923 return null;
1924 }
1925 ByteArrayOutputStream stream = new ByteArrayOutputStream();
1926 bitmap.compress(CompressFormat.PNG, 100, stream);
1927 return stream.toByteArray();
1928 }
1929
John Reck26b18322011-06-21 13:08:58 -07001930 public void loadUrl(String url, Map<String, String> headers) {
1931 if (mMainView != null) {
Michael Kolba53c9892011-10-05 13:31:40 -07001932 mPageLoadProgress = INITIAL_PROGRESS;
Michael Kolb14612442011-06-24 13:06:29 -07001933 mCurrentState = new PageState(mContext, false, url, null);
John Reck26b18322011-06-21 13:08:58 -07001934 mMainView.loadUrl(url, headers);
1935 }
1936 }
1937
John Reck38b39652012-06-05 09:22:59 -07001938 public void disableUrlOverridingForLoad() {
1939 mDisableOverrideUrlLoading = true;
1940 }
1941
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001942 private void thumbnailUpdated() {
John Reck8ee633f2011-08-09 16:00:35 -07001943 mHandler.removeMessages(MSG_CAPTURE);
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001944
John Reck8ee633f2011-08-09 16:00:35 -07001945 TabControl tc = mWebViewController.getTabControl();
1946 if (tc != null) {
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001947 OnThumbnailUpdatedListener updateListener = tc.getOnThumbnailUpdatedListener();
John Reck8ee633f2011-08-09 16:00:35 -07001948 if (updateListener != null) {
1949 updateListener.onThumbnailUpdated(this);
1950 }
1951 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001952 }
1953
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001954 protected void capture() {
1955 if (mMainView == null || mCapture == null || !mMainView.isReady() ||
1956 mMainView.getContentWidth() <= 0 || mMainView.getContentHeight() <= 0 ||
1957 !mFirstVisualPixelPainted || mMainView.isShowingCrashView()) {
1958
1959 initCaptureBitmap();
1960 thumbnailUpdated();
1961 return;
1962 }
1963
1964 mMainView.getContentBitmapAsync((float) mCaptureWidth / mMainView.getWidth(), new Rect(),
1965 new ValueCallback<Bitmap>() {
1966 @Override
1967 public void onReceiveValue(Bitmap bitmap) {
1968 if (mCapture == null) {
1969 initCaptureBitmap();
1970 }
1971
1972 if (bitmap == null) {
1973 thumbnailUpdated();
1974 return;
1975 }
1976
1977 Canvas c = new Canvas(mCapture);
1978 mCapture.eraseColor(Color.WHITE);
1979 c.drawBitmap(bitmap, 0, 0, null);
1980
1981 // manually anti-alias the edges for the tilt
1982 c.drawRect(0, 0, 1, mCapture.getHeight(), sAlphaPaint);
1983 c.drawRect(mCapture.getWidth() - 1, 0, mCapture.getWidth(),
1984 mCapture.getHeight(), sAlphaPaint);
1985 c.drawRect(0, 0, mCapture.getWidth(), 1, sAlphaPaint);
1986 c.drawRect(0, mCapture.getHeight() - 1, mCapture.getWidth(),
1987 mCapture.getHeight(), sAlphaPaint);
1988 c.setBitmap(null);
1989
1990 persistThumbnail();
1991 thumbnailUpdated();
1992 }
1993 }
1994 );
John Reck1cf4b792011-07-26 10:22:22 -07001995 }
1996
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001997 @Override
1998 public void onNewPicture(WebView view, Picture picture) {
Michael Kolb9ef259a2011-07-12 15:33:08 -07001999 }
2000
John Reckef654f12011-07-12 16:42:08 -07002001 public boolean canGoBack() {
2002 return mMainView != null ? mMainView.canGoBack() : false;
2003 }
2004
2005 public boolean canGoForward() {
2006 return mMainView != null ? mMainView.canGoForward() : false;
2007 }
2008
2009 public void goBack() {
2010 if (mMainView != null) {
2011 mMainView.goBack();
2012 }
2013 }
2014
2015 public void goForward() {
2016 if (mMainView != null) {
2017 mMainView.goForward();
2018 }
2019 }
2020
John Reck1cf4b792011-07-26 10:22:22 -07002021 protected void persistThumbnail() {
John Reck4eadc342011-10-31 14:04:10 -07002022 DataController.getInstance(mContext).saveThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07002023 }
2024
2025 protected void deleteThumbnail() {
John Reck4eadc342011-10-31 14:04:10 -07002026 DataController.getInstance(mContext).deleteThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07002027 }
2028
John Reck4eadc342011-10-31 14:04:10 -07002029 void updateCaptureFromBlob(byte[] blob) {
John Reck1cf4b792011-07-26 10:22:22 -07002030 synchronized (Tab.this) {
2031 if (mCapture == null) {
2032 return;
2033 }
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002034 ByteBuffer buffer = ByteBuffer.wrap(blob);
Michael Kolbd837a112011-08-09 14:00:18 -07002035 try {
2036 mCapture.copyPixelsFromBuffer(buffer);
2037 } catch (RuntimeException rex) {
2038 Log.e(LOGTAG, "Load capture has mismatched sizes; buffer: "
2039 + buffer.capacity() + " blob: " + blob.length
2040 + "capture: " + mCapture.getByteCount());
2041 throw rex;
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002042 }
John Reck1cf4b792011-07-26 10:22:22 -07002043 }
2044 }
2045
John Reck52be4782011-08-26 15:37:29 -07002046 @Override
2047 public String toString() {
2048 StringBuilder builder = new StringBuilder(100);
2049 builder.append(mId);
2050 builder.append(") has parent: ");
2051 if (getParent() != null) {
2052 builder.append("true[");
2053 builder.append(getParent().getId());
2054 builder.append("]");
2055 } else {
2056 builder.append("false");
2057 }
2058 builder.append(", incog: ");
2059 builder.append(isPrivateBrowsingEnabled());
2060 if (!isPrivateBrowsingEnabled()) {
2061 builder.append(", title: ");
2062 builder.append(getTitle());
2063 builder.append(", url: ");
2064 builder.append(getUrl());
2065 }
2066 return builder.toString();
2067 }
2068
Ze G Riande2a675c22015-06-03 11:15:24 -07002069 // dertermines if the tab contains a dislled page
2070 public boolean isDistilled() {
2071 if (!BrowserCommandLine.hasSwitch("reader-mode")) {
2072 return false;
2073 }
2074 try {
2075 return DomDistillerUtils.isUrlDistilled(getUrl());
2076 } catch (Exception e) {
2077 return false;
2078 }
2079 }
2080
2081 //determines if the tab contains a distillable page
2082 public boolean isDistillable() {
2083 if (!BrowserCommandLine.hasSwitch("reader-mode")) {
2084 mIsDistillable = false;
2085 return mIsDistillable;
2086 }
2087 final ValueCallback<String> onIsDistillable = new ValueCallback<String>() {
2088 @Override
2089 public void onReceiveValue(String str) {
2090 mIsDistillable = Boolean.parseBoolean(str);
2091 }
2092 };
2093
2094 if (isDistilled()) {
2095 mIsDistillable = true;
2096 return mIsDistillable;
2097 }
2098
2099 try {
2100 DomDistillerUtils.isWebViewDistillable(getWebView(), onIsDistillable);
2101 } catch (Exception e) {
2102 mIsDistillable = false;
2103 }
2104
2105 return mIsDistillable;
2106 }
2107
2108 // Function that sets the mIsDistillable variable
2109 public void setIsDistillable(boolean value) {
2110 if (!BrowserCommandLine.hasSwitch("reader-mode")) {
2111 mIsDistillable = false;
2112 }
2113 mIsDistillable = value;
2114 }
2115
2116 // Function that returns the distilled url of the current url
2117 public String getDistilledUrl() {
2118 if (getUrl() != null) {
2119 return DomDistillerUtils.getDistilledUrl(getUrl());
2120 }
2121 return new String();
2122 }
2123
2124 // function that returns the non-distilled version of the current url
2125 public String getNonDistilledUrl() {
2126 if (getUrl() != null) {
2127 return DomDistillerUtils.getOriginalUrlFromDistilledUrl(getUrl());
2128 }
2129 return new String();
2130 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002131}