blob: a65fda5285a77f6467e508f666d23eb566106038 [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;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080067import org.codeaurora.swe.HttpAuthHandler;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080068import org.codeaurora.swe.WebBackForwardList;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080069import org.codeaurora.swe.WebChromeClient;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080070import org.codeaurora.swe.WebView;
71import org.codeaurora.swe.WebView.PictureListener;
Pankaj Garg1c7380d2014-08-27 14:17:12 -070072import org.codeaurora.swe.WebView.CreateWindowParams;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080073import org.codeaurora.swe.WebViewClient;
Pankaj Garg1c13cab2015-05-12 11:52:17 -070074import org.codeaurora.swe.util.Observable;
Ze G Riande2a675c22015-06-03 11:15:24 -070075import org.codeaurora.swe.DomDistillerUtils;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080076
John Reck541f55a2011-06-07 16:34:43 -070077import java.io.ByteArrayOutputStream;
John Reck2b71d6d2012-04-18 17:42:06 -070078import java.io.File;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080079import java.io.InputStream;
John Reck1cf4b792011-07-26 10:22:22 -070080import java.nio.ByteBuffer;
Michael Kolbfe251992010-07-08 15:41:55 -070081import java.util.Map;
John Reck2b71d6d2012-04-18 17:42:06 -070082import java.util.UUID;
Michael Kolbfe251992010-07-08 15:41:55 -070083import java.util.Vector;
Vivek Sekhar53ef8932015-06-18 16:51:43 -070084import java.util.List;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080085import java.sql.Timestamp;
86import java.util.Date;
Michael Kolbfe251992010-07-08 15:41:55 -070087
Grace Kloba22ac16e2009-10-07 18:00:23 -070088/**
89 * Class for maintaining Tabs with a main WebView and a subwindow.
90 */
Michael Kolb9ef259a2011-07-12 15:33:08 -070091class Tab implements PictureListener {
Michael Kolb8233fac2010-10-26 16:08:53 -070092
Grace Kloba22ac16e2009-10-07 18:00:23 -070093 // Log Tag
94 private static final String LOGTAG = "Tab";
Bijan Amirzada41242f22014-03-21 12:12:18 -070095 private static final boolean LOGD_ENABLED = com.android.browser.Browser.LOGD_ENABLED;
Ben Murdochc42addf2010-01-28 15:19:59 +000096 // Special case the logtag for messages for the Console to make it easier to
97 // filter them and match the logtag used for these messages in older versions
98 // of the browser.
99 private static final String CONSOLE_LOGTAG = "browser";
100
Michael Kolb9ef259a2011-07-12 15:33:08 -0700101 private static final int MSG_CAPTURE = 42;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800102 private static final int CAPTURE_DELAY = 1000;
Michael Kolba53c9892011-10-05 13:31:40 -0700103 private static final int INITIAL_PROGRESS = 5;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700104
John Reck1cf4b792011-07-26 10:22:22 -0700105 private static Bitmap sDefaultFavicon;
Sagar Dhawan33551ff2015-07-08 17:24:44 -0700106 private boolean mIsKeyboardUp = false;
John Reck1cf4b792011-07-26 10:22:22 -0700107
Michael Kolba3194d02011-09-07 11:23:51 -0700108 private static Paint sAlphaPaint = new Paint();
109 static {
110 sAlphaPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
111 sAlphaPaint.setColor(Color.TRANSPARENT);
112 }
113
Steve Block2466eff2011-10-03 15:33:09 +0100114 public enum SecurityState {
Steve Block4895b012011-10-03 16:26:46 +0100115 // The page's main resource does not use SSL. Note that we use this
116 // state irrespective of the SSL authentication state of sub-resources.
Steve Block2466eff2011-10-03 15:33:09 +0100117 SECURITY_STATE_NOT_SECURE,
Steve Block4895b012011-10-03 16:26:46 +0100118 // The page's main resource uses SSL and the certificate is good. The
119 // same is true of all sub-resources.
Steve Block2466eff2011-10-03 15:33:09 +0100120 SECURITY_STATE_SECURE,
Steve Block4895b012011-10-03 16:26:46 +0100121 // The page's main resource uses SSL and the certificate is good, but
122 // some sub-resources either do not use SSL or have problems with their
123 // certificates.
Steve Block2466eff2011-10-03 15:33:09 +0100124 SECURITY_STATE_MIXED,
Steve Block4895b012011-10-03 16:26:46 +0100125 // The page's main resource uses SSL but there is a problem with its
126 // certificate.
127 SECURITY_STATE_BAD_CERTIFICATE,
John Reck30c714c2010-12-16 17:30:34 -0800128 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700129
Michael Kolb14612442011-06-24 13:06:29 -0700130 Context mContext;
John Reckd8c74522011-06-14 08:45:00 -0700131 protected WebViewController mWebViewController;
Michael Kolb8233fac2010-10-26 16:08:53 -0700132
Michael Kolbc831b632011-05-11 09:30:34 -0700133 // The tab ID
John Reckd8c74522011-06-14 08:45:00 -0700134 private long mId = -1;
Michael Kolbc831b632011-05-11 09:30:34 -0700135
Grace Kloba22ac16e2009-10-07 18:00:23 -0700136 // Main WebView wrapper
Michael Kolba713ec82010-11-29 17:27:06 -0800137 private View mContainer;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700138 // Main WebView
139 private WebView mMainView;
140 // Subwindow container
141 private View mSubViewContainer;
142 // Subwindow WebView
143 private WebView mSubView;
144 // Saved bundle for when we are running low on memory. It contains the
145 // information needed to restore the WebView if the user goes back to the
146 // tab.
147 private Bundle mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700148 // Parent Tab. This is the Tab that created this Tab, or null if the Tab was
149 // created by the UI
Michael Kolbc831b632011-05-11 09:30:34 -0700150 private Tab mParent;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700151 // Tab that constructed by this Tab. This is used when this Tab is
152 // destroyed, it clears all mParentTab values in the children.
Michael Kolbc831b632011-05-11 09:30:34 -0700153 private Vector<Tab> mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700154 // If true, the tab is in the foreground of the current activity.
155 private boolean mInForeground;
Michael Kolb8233fac2010-10-26 16:08:53 -0700156 // If true, the tab is in page loading state (after onPageStarted,
157 // before onPageFinsihed)
158 private boolean mInPageLoad;
Tarun Nainani8eb00912014-07-17 12:28:32 -0700159 private boolean mPageFinished;
John Reck38b39652012-06-05 09:22:59 -0700160 private boolean mDisableOverrideUrlLoading;
Pankaj Garg79878492015-04-01 14:48:21 -0700161 private boolean mFirstVisualPixelPainted = false;
John Reck30c714c2010-12-16 17:30:34 -0800162 // The last reported progress of the current page
163 private int mPageLoadProgress;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000164 // The time the load started, used to find load page time
165 private long mLoadStartTime;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700166 // Application identifier used to find tabs that another application wants
167 // to reuse.
168 private String mAppId;
Michael Kolbe28b3472011-08-04 16:54:31 -0700169 // flag to indicate if tab should be closed on back
170 private boolean mCloseOnBack;
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700171 // flag to indicate if the tab was opened from an intent
172 private boolean mDerivedFromIntent = false;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -0500173 // The listener that gets invoked when a download is started from the
174 // mMainView
Selim Gurun0b3d66f2012-08-29 13:08:13 -0700175 private final BrowserDownloadListener mDownloadListener;
John Recke969cc52010-12-21 17:24:43 -0800176 private DataController mDataController;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700177
178 // AsyncTask for downloading touch icons
179 DownloadTouchIcon mTouchIconLoader;
180
John Reck35e9dd62011-04-25 09:01:54 -0700181 private BrowserSettings mSettings;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700182 private int mCaptureWidth;
183 private int mCaptureHeight;
184 private Bitmap mCapture;
185 private Handler mHandler;
Michael Kolb72864272012-05-03 15:42:15 -0700186 private boolean mUpdateThumbnail;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800187 private Timestamp timestamp;
Vivek Sekharf96064b2014-07-28 16:32:34 -0700188 private boolean mFullScreen = false;
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800189 private boolean mReceivedError;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700190
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -0700191 // determine if webview is destroyed to MemoryMonitor
192 private boolean mWebViewDestroyedByMemoryMonitor;
193
Pankaj Garg21dad562015-07-02 17:17:24 -0700194 private String mTouchIconUrl;
195
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700196 private Observable mFirstPixelObservable;
197 private Observable mTabHistoryUpdateObservable;
198
199 Observable getFirstPixelObservable() {
200 return mFirstPixelObservable;
201 }
202
203 Observable getTabHistoryUpdateObservable() {
204 return mTabHistoryUpdateObservable;
205 }
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -0700206
Ze G Riande2a675c22015-06-03 11:15:24 -0700207 // dertermines if the tab contains a disllable page
208 private boolean mIsDistillable = false;
Mathew Inwood1dd8e822011-08-03 14:34:29 +0100209
John Reck1cf4b792011-07-26 10:22:22 -0700210 private static synchronized Bitmap getDefaultFavicon(Context context) {
211 if (sDefaultFavicon == null) {
212 sDefaultFavicon = BitmapFactory.decodeResource(
Enrico Rosd6efa972014-12-02 19:49:59 -0800213 context.getResources(), R.drawable.ic_deco_favicon_normal);
John Reck1cf4b792011-07-26 10:22:22 -0700214 }
215 return sDefaultFavicon;
216 }
Michael Kolbeb95db42011-03-03 10:38:40 -0800217
John Reck30c714c2010-12-16 17:30:34 -0800218 // All the state needed for a page
John Reckd8c74522011-06-14 08:45:00 -0700219 protected static class PageState {
John Reck30c714c2010-12-16 17:30:34 -0800220 String mUrl;
John Reckdb22ec42011-06-29 11:31:24 -0700221 String mOriginalUrl;
John Reck30c714c2010-12-16 17:30:34 -0800222 String mTitle;
Steve Block2466eff2011-10-03 15:33:09 +0100223 SecurityState mSecurityState;
Steve Block08a6f0c2011-10-06 12:12:53 +0100224 // This is non-null only when mSecurityState is SECURITY_STATE_BAD_CERTIFICATE.
John Reck30c714c2010-12-16 17:30:34 -0800225 Bitmap mFavicon;
Steve Block08a6f0c2011-10-06 12:12:53 +0100226 boolean mIsBookmarkedSite;
227 boolean mIncognito;
John Reck30c714c2010-12-16 17:30:34 -0800228
229 PageState(Context c, boolean incognito) {
John Reck502a3532011-08-16 14:21:46 -0700230 mIncognito = incognito;
231 if (mIncognito) {
Axesh R. Ajmera6fc73692015-08-11 16:32:10 -0700232 mOriginalUrl = mUrl = "chrome://incognito";
John Reck30c714c2010-12-16 17:30:34 -0800233 mTitle = c.getString(R.string.new_incognito_tab);
John Reck30c714c2010-12-16 17:30:34 -0800234 } else {
John Reckdb22ec42011-06-29 11:31:24 -0700235 mOriginalUrl = mUrl = "";
John Reck30c714c2010-12-16 17:30:34 -0800236 mTitle = c.getString(R.string.new_tab);
John Reck30c714c2010-12-16 17:30:34 -0800237 }
Steve Block2466eff2011-10-03 15:33:09 +0100238 mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
John Reck30c714c2010-12-16 17:30:34 -0800239 }
240
241 PageState(Context c, boolean incognito, String url, Bitmap favicon) {
John Reck502a3532011-08-16 14:21:46 -0700242 mIncognito = incognito;
John Reckdb22ec42011-06-29 11:31:24 -0700243 mOriginalUrl = mUrl = url;
Pankaj Garg96d0ccd2015-07-30 16:49:47 -0700244 mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
John Reck1cf4b792011-07-26 10:22:22 -0700245 mFavicon = favicon;
John Reck30c714c2010-12-16 17:30:34 -0800246 }
John Reck1cf4b792011-07-26 10:22:22 -0700247
Grace Kloba22ac16e2009-10-07 18:00:23 -0700248 }
249
John Reck30c714c2010-12-16 17:30:34 -0800250 // The current/loading page's state
John Reckd8c74522011-06-14 08:45:00 -0700251 protected PageState mCurrentState;
John Reck30c714c2010-12-16 17:30:34 -0800252
Grace Kloba22ac16e2009-10-07 18:00:23 -0700253 // Used for saving and restoring each Tab
Michael Kolbc831b632011-05-11 09:30:34 -0700254 static final String ID = "ID";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700255 static final String CURRURL = "currentUrl";
256 static final String CURRTITLE = "currentTitle";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700257 static final String PARENTTAB = "parentTab";
258 static final String APPID = "appid";
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700259 static final String INCOGNITO = "privateBrowsingEnabled";
John Reckb0a86db2011-05-24 14:05:58 -0700260 static final String USERAGENT = "useragent";
Michael Kolbe28b3472011-08-04 16:54:31 -0700261 static final String CLOSEFLAG = "closeOnBack";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700262
Pankaj Garg18186a92015-03-31 14:59:33 -0700263 public void setNetworkAvailable(boolean networkUp) {
264 if (networkUp && mReceivedError && (mMainView != null)) {
265 mMainView.reload();
266 }
267 }
268
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700269 public boolean isFirstVisualPixelPainted() {
270 return mFirstVisualPixelPainted;
271 }
272
273 public int getCaptureIndex(int navIndex) {
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700274 int orientation = mWebViewController.getActivity().
275 getResources().getConfiguration().orientation;
276
277 int orientationBit = (orientation == Configuration.ORIENTATION_LANDSCAPE) ? 0 : 1;
278
Vivek Sekhard0f60402015-06-05 14:07:11 -0700279 int index = orientationBit << 31 | (((int)mId & 0x7f) << 24) | (navIndex & 0xffffff);
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700280 return index;
281 }
282
283 public int getTabIdxFromCaptureIdx(int index) {
284 return (index & 0x7f000000) >> 24;
285 }
286
287 public int getOrientationFromCaptureIdx(int index) {
288 return ((index & 0x80000000) == 0) ? Configuration.ORIENTATION_LANDSCAPE :
289 Configuration.ORIENTATION_PORTRAIT;
290
291 }
292
293 public int getNavIdxFromCaptureIdx(int index) {
294 return (index & 0xffffff);
295 }
296
Pankaj Garg96d0ccd2015-07-30 16:49:47 -0700297 public static SecurityState getWebViewSecurityState(WebView view) {
298 switch (view.getSecurityLevel()) {
299 case WebView.SecurityLevel.EV_SECURE:
300 case WebView.SecurityLevel.SECURE:
301 return SecurityState.SECURITY_STATE_SECURE;
302 case WebView.SecurityLevel.SECURITY_ERROR:
303 return SecurityState.SECURITY_STATE_BAD_CERTIFICATE;
304 case WebView.SecurityLevel.SECURITY_POLICY_WARNING:
305 case WebView.SecurityLevel.SECURITY_WARNING:
306 return SecurityState.SECURITY_STATE_MIXED;
307 }
308 return SecurityState.SECURITY_STATE_NOT_SECURE;
309 }
310
Grace Kloba22ac16e2009-10-07 18:00:23 -0700311 // -------------------------------------------------------------------------
312 // WebViewClient implementation for the main WebView
313 // -------------------------------------------------------------------------
314
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800315 private final WebViewClient mWebViewClient = new WebViewClient() {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500316 private Message mDontResend;
317 private Message mResend;
Michael Kolb47bd1e42011-09-01 15:25:00 -0700318
319 private boolean providersDiffer(String url, String otherUrl) {
320 Uri uri1 = Uri.parse(url);
321 Uri uri2 = Uri.parse(otherUrl);
322 return !uri1.getEncodedAuthority().equals(uri2.getEncodedAuthority());
323 }
324
Grace Kloba22ac16e2009-10-07 18:00:23 -0700325 @Override
326 public void onPageStarted(WebView view, String url, Bitmap favicon) {
Ze G Riande2a675c22015-06-03 11:15:24 -0700327 setIsDistillable(false);
Michael Kolb8233fac2010-10-26 16:08:53 -0700328 mInPageLoad = true;
Tarun Nainani8eb00912014-07-17 12:28:32 -0700329 mPageFinished = false;
Pankaj Garg79878492015-04-01 14:48:21 -0700330 mFirstVisualPixelPainted = false;
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700331 mFirstPixelObservable.set(false);
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800332 mReceivedError = false;
Michael Kolb72864272012-05-03 15:42:15 -0700333 mUpdateThumbnail = true;
Michael Kolba53c9892011-10-05 13:31:40 -0700334 mPageLoadProgress = INITIAL_PROGRESS;
Michael Kolb14612442011-06-24 13:06:29 -0700335 mCurrentState = new PageState(mContext,
John Reck30c714c2010-12-16 17:30:34 -0800336 view.isPrivateBrowsingEnabled(), url, favicon);
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000337 mLoadStartTime = SystemClock.uptimeMillis();
Pankaj Garg62bc7912015-04-14 16:08:59 -0700338 // Need re-enable FullScreenMode on Page navigation if needed
339 if (BrowserSettings.getInstance().useFullscreen()){
340 Controller controller = (Controller) mWebViewController;
341 BaseUi ui = (BaseUi) controller.getUi();
342 ui.forceDisableFullscreenMode(false);
343 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700344 // If we start a touch icon load and then load a new page, we don't
345 // want to cancel the current touch icon loader. But, we do want to
346 // create a new one when the touch icon url is known.
347 if (mTouchIconLoader != null) {
348 mTouchIconLoader.mTab = null;
349 mTouchIconLoader = null;
350 }
351
Grace Kloba22ac16e2009-10-07 18:00:23 -0700352 // finally update the UI in the activity if it is in the foreground
John Reck324d4402011-01-11 16:56:42 -0800353 mWebViewController.onPageStarted(Tab.this, view, favicon);
Leon Scroggins4cd97792010-12-03 15:31:56 -0500354
John Recke969cc52010-12-21 17:24:43 -0800355 updateBookmarkedStatus();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700356 }
357
358 @Override
359 public void onPageFinished(WebView view, String url) {
John Reck38b39652012-06-05 09:22:59 -0700360 mDisableOverrideUrlLoading = false;
John Reck5b691842010-11-29 11:21:13 -0800361 if (!isPrivateBrowsingEnabled()) {
362 LogTag.logPageFinishedLoading(
363 url, SystemClock.uptimeMillis() - mLoadStartTime);
364 }
John Reck1cf4b792011-07-26 10:22:22 -0700365 syncCurrentState(view, url);
John Reck324d4402011-01-11 16:56:42 -0800366 mWebViewController.onPageFinished(Tab.this);
Pankaj Garg96d0ccd2015-07-30 16:49:47 -0700367 setSecurityState(getWebViewSecurityState(view));
Grace Kloba22ac16e2009-10-07 18:00:23 -0700368 }
369
Pankaj Garg79878492015-04-01 14:48:21 -0700370 @Override
371 public void onFirstVisualPixel(WebView view) {
372 mFirstVisualPixelPainted = true;
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700373 mFirstPixelObservable.set(true);
Pankaj Garg79878492015-04-01 14:48:21 -0700374 }
375
Grace Kloba22ac16e2009-10-07 18:00:23 -0700376 // return true if want to hijack the url to let another app to handle it
377 @Override
378 public boolean shouldOverrideUrlLoading(WebView view, String url) {
John Reck38b39652012-06-05 09:22:59 -0700379 if (!mDisableOverrideUrlLoading && mInForeground) {
Michael Kolb18eb3772010-12-10 14:29:51 -0800380 return mWebViewController.shouldOverrideUrlLoading(Tab.this,
381 view, url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700382 } else {
383 return false;
384 }
385 }
386
Vivek Sekharb991edb2014-12-17 18:18:07 -0800387 @Override
388 public boolean shouldDownloadFavicon(WebView view, String url) {
389 return true;
390 }
391
Grace Kloba22ac16e2009-10-07 18:00:23 -0700392 /**
Steve Block2466eff2011-10-03 15:33:09 +0100393 * Updates the security state. This method is called when we discover
394 * another resource to be loaded for this page (for example,
395 * javascript). While we update the security state, we do not update
396 * the lock icon until we are done loading, as it is slightly more
397 * secure this way.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700398 */
399 @Override
400 public void onLoadResource(WebView view, String url) {
401 if (url != null && url.length() > 0) {
402 // It is only if the page claims to be secure that we may have
Steve Block2466eff2011-10-03 15:33:09 +0100403 // to update the security state:
404 if (mCurrentState.mSecurityState == SecurityState.SECURITY_STATE_SECURE) {
405 // If NOT a 'safe' url, change the state to mixed content!
Grace Kloba22ac16e2009-10-07 18:00:23 -0700406 if (!(URLUtil.isHttpsUrl(url) || URLUtil.isDataUrl(url)
407 || URLUtil.isAboutUrl(url))) {
Steve Block2466eff2011-10-03 15:33:09 +0100408 mCurrentState.mSecurityState = SecurityState.SECURITY_STATE_MIXED;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700409 }
410 }
411 }
412 }
413
414 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700415 * Show a dialog informing the user of the network error reported by
416 * WebCore if it is in the foreground.
417 */
418 @Override
419 public void onReceivedError(WebView view, int errorCode,
420 String description, String failingUrl) {
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800421 // Used for the syncCurrentState to use
422 // the failing url instead of using webview url
423 mReceivedError = true;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700424 }
425
426 /**
427 * Check with the user if it is ok to resend POST data as the page they
428 * are trying to navigate to is the result of a POST.
429 */
430 @Override
431 public void onFormResubmission(WebView view, final Message dontResend,
432 final Message resend) {
433 if (!mInForeground) {
434 dontResend.sendToTarget();
435 return;
436 }
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500437 if (mDontResend != null) {
438 Log.w(LOGTAG, "onFormResubmission should not be called again "
439 + "while dialog is still up");
440 dontResend.sendToTarget();
441 return;
442 }
443 mDontResend = dontResend;
444 mResend = resend;
Michael Kolb14612442011-06-24 13:06:29 -0700445 new AlertDialog.Builder(mContext).setTitle(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700446 R.string.browserFrameFormResubmitLabel).setMessage(
447 R.string.browserFrameFormResubmitMessage)
448 .setPositiveButton(R.string.ok,
449 new DialogInterface.OnClickListener() {
450 public void onClick(DialogInterface dialog,
451 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500452 if (mResend != null) {
453 mResend.sendToTarget();
454 mResend = null;
455 mDontResend = null;
456 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700457 }
458 }).setNegativeButton(R.string.cancel,
459 new DialogInterface.OnClickListener() {
460 public void onClick(DialogInterface dialog,
461 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500462 if (mDontResend != null) {
463 mDontResend.sendToTarget();
464 mResend = null;
465 mDontResend = null;
466 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700467 }
468 }).setOnCancelListener(new OnCancelListener() {
469 public void onCancel(DialogInterface dialog) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500470 if (mDontResend != null) {
471 mDontResend.sendToTarget();
472 mResend = null;
473 mDontResend = null;
474 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700475 }
476 }).show();
477 }
478
479 /**
480 * Insert the url into the visited history database.
481 * @param url The url to be inserted.
482 * @param isReload True if this url is being reloaded.
483 * FIXME: Not sure what to do when reloading the page.
484 */
485 @Override
486 public void doUpdateVisitedHistory(WebView view, String url,
487 boolean isReload) {
John Reck324d4402011-01-11 16:56:42 -0800488 mWebViewController.doUpdateVisitedHistory(Tab.this, isReload);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700489 }
490
491 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700492 * Handles an HTTP authentication request.
493 *
494 * @param handler The authentication handler
495 * @param host The host
496 * @param realm The realm
497 */
498 @Override
499 public void onReceivedHttpAuthRequest(WebView view,
500 final HttpAuthHandler handler, final String host,
501 final String realm) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700502 mWebViewController.onReceivedHttpAuthRequest(Tab.this, view, handler, host, realm);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700503 }
504
505 @Override
John Reck438bf462011-01-12 18:11:46 -0800506 public WebResourceResponse shouldInterceptRequest(WebView view,
507 String url) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800508 //intercept if opening a new incognito tab - show the incognito welcome page
Axesh R. Ajmera6fc73692015-08-11 16:32:10 -0700509 if (url.startsWith("chrome://incognito")) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800510 Resources resourceHandle = mContext.getResources();
511 InputStream inStream = resourceHandle.openRawResource(
Bijan Amirzada41242f22014-03-21 12:12:18 -0700512 com.android.browser.R.raw.incognito_mode_start_page);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800513 return new WebResourceResponse("text/html", "utf8", inStream);
514 }
kaiyiz6e5b3e02013-08-19 20:02:01 +0800515 WebResourceResponse res;
516 if (MyNavigationUtil.MY_NAVIGATION.equals(url)) {
517 res = MyNavigationProvider.shouldInterceptRequest(mContext, url);
518 } else {
519 res = HomeProvider.shouldInterceptRequest(mContext, url);
520 }
John Reck438bf462011-01-12 18:11:46 -0800521 return res;
522 }
523
524 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700525 public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
526 if (!mInForeground) {
527 return false;
528 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700529 return mWebViewController.shouldOverrideKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700530 }
531
532 @Override
533 public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700534 if (!mInForeground) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700535 return;
536 }
John Reck997b1b72012-04-19 18:08:25 -0700537 if (!mWebViewController.onUnhandledKeyEvent(event)) {
538 super.onUnhandledKeyEvent(view, event);
539 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700540 }
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700541
542 @Override
543 public void beforeNavigation(WebView view, String url) {
Pankaj Garg21dad562015-07-02 17:17:24 -0700544 mTouchIconUrl = null;
Site Mao61b68212015-07-16 10:56:31 -0700545 if (BaseUi.isUiLowPowerMode()) {
Pankaj Garg18aa0a12015-06-22 11:06:12 -0700546 return;
547 }
548
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700549 if (isPrivateBrowsingEnabled()) {
550 return;
551 }
552
553 if (!mFirstVisualPixelPainted) {
554 return;
555 }
556
Pankaj Garg8ee61462015-07-29 18:17:24 -0700557 if (view.getUrl().equals(url)) {
558 return;
559 }
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -0700560
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700561 final int idx = view.copyBackForwardList().getCurrentIndex();
562 boolean bitmapExists = view.hasSnapshot(idx);
563
564 int progress = 100;
565 Controller controller = (Controller)mWebViewController;
566 UI ui = controller.getUi();
567 if (ui instanceof BaseUi) {
568 BaseUi baseUi = (BaseUi) ui;
569 TitleBar titleBar = baseUi.getTitleBar();
570 progress = titleBar.getProgressView().getProgressPercent();
571 }
572
573 if (bitmapExists && progress < 85) {
574 return;
575 }
576
577 int index = getCaptureIndex(view.getLastCommittedHistoryIndex());
Sagar Dhawan49f85cf2015-07-10 16:54:20 -0700578 view.captureSnapshot(index, null);
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700579 }
580
581 @Override
582 public void onHistoryItemCommit(WebView view, int index) {
Site Mao61b68212015-07-16 10:56:31 -0700583 if (BaseUi.isUiLowPowerMode()) {
Pankaj Garg18aa0a12015-06-22 11:06:12 -0700584 return;
585 }
586
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -0700587 // prevent snapshot tab from commiting any history
588 if (isSnapshot()) {
589 return;
590 }
591
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700592 mTabHistoryUpdateObservable.set(index);
Vivek Sekhar53ef8932015-06-18 16:51:43 -0700593 final int maxIdx = view.copyBackForwardList().getSize();
594 final WebView wv = view;
595 view.getSnapshotIds(new ValueCallback <List<Integer>>() {
596 @Override
597 public void onReceiveValue(List<Integer> ids) {
598 int currentTabIdx = mWebViewController.getTabControl().getCurrentPosition();
599 for (Integer id : ids) {
600 if (getTabIdxFromCaptureIdx(id) == currentTabIdx &&
601 getNavIdxFromCaptureIdx(id) >= maxIdx) {
602 wv.deleteSnapshot(id);
603 }
604 }
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700605 }
Vivek Sekhar53ef8932015-06-18 16:51:43 -0700606 });
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700607 }
Pankaj Garg62bc7912015-04-14 16:08:59 -0700608
609 @Override
610 public void onKeyboardStateChange(boolean popup) {
Vivek Sekhard4de6162015-07-21 15:01:45 -0700611 boolean keyboardWasShowing = isKeyboardShowing();
Sagar Dhawan33551ff2015-07-08 17:24:44 -0700612 mIsKeyboardUp = popup;
Vivek Sekhard4de6162015-07-21 15:01:45 -0700613 Controller controller = (Controller)mWebViewController;
614 BaseUi ui = (BaseUi) controller.getUi();
615 // lock the title bar
616 if (popup)
617 ui.getTitleBar().showTopControls(true);
618 if (keyboardWasShowing && popup)
619 ui.getTitleBar().enableTopControls(true);
Pankaj Garg62bc7912015-04-14 16:08:59 -0700620 if (BrowserSettings.getInstance().useFullscreen()) {
Pankaj Garg62bc7912015-04-14 16:08:59 -0700621 ui.forceDisableFullscreenMode(popup);
622 }
623 }
Vivek Sekharae3b1792015-08-03 12:26:41 -0700624
625 @Override
626 public void onAttachInterstitialPage(WebView mWebView) {
627 Controller controller = (Controller)mWebViewController;
628 BaseUi ui = (BaseUi) controller.getUi();
629 ui.getTitleBar().showTopControls(false);
630 }
631
632 @Override
633 public void onDetachInterstitialPage(WebView mWebView) {
634 Controller controller = (Controller)mWebViewController;
635 BaseUi ui = (BaseUi) controller.getUi();
636 ui.getTitleBar().enableTopControls(true);
637 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700638 };
639
John Reck1cf4b792011-07-26 10:22:22 -0700640 private void syncCurrentState(WebView view, String url) {
641 // Sync state (in case of stop/timeout)
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800642
643 if (mReceivedError) {
644 mCurrentState.mUrl = url;
645 mCurrentState.mOriginalUrl = url;
646 } else {
647 mCurrentState.mUrl = view.getUrl();
648 mCurrentState.mOriginalUrl = view.getOriginalUrl();
649 mCurrentState.mFavicon = view.getFavicon();
650 }
651
John Reck1cf4b792011-07-26 10:22:22 -0700652 if (mCurrentState.mUrl == null) {
John Reck8bcafc12011-08-29 16:43:02 -0700653 mCurrentState.mUrl = "";
John Reck1cf4b792011-07-26 10:22:22 -0700654 }
John Reck1cf4b792011-07-26 10:22:22 -0700655 mCurrentState.mTitle = view.getTitle();
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800656
657
John Reck1cf4b792011-07-26 10:22:22 -0700658 if (!URLUtil.isHttpsUrl(mCurrentState.mUrl)) {
659 // In case we stop when loading an HTTPS page from an HTTP page
660 // but before a provisional load occurred
Steve Block2466eff2011-10-03 15:33:09 +0100661 mCurrentState.mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
John Reck1cf4b792011-07-26 10:22:22 -0700662 }
John Reck502a3532011-08-16 14:21:46 -0700663 mCurrentState.mIncognito = view.isPrivateBrowsingEnabled();
John Reck1cf4b792011-07-26 10:22:22 -0700664 }
665
Pankaj Garg21dad562015-07-02 17:17:24 -0700666 public String getTouchIconUrl() {
667 return mTouchIconUrl;
668 }
Tarun Nainani8eb00912014-07-17 12:28:32 -0700669
Sagar Dhawan33551ff2015-07-08 17:24:44 -0700670 public boolean isKeyboardShowing() {
671 Controller controller = (Controller)mWebViewController;
672 return (mIsKeyboardUp || controller.getUi().isEditingUrl());
673 }
674
Tarun Nainani8eb00912014-07-17 12:28:32 -0700675 public boolean isTabFullScreen() {
676 return mFullScreen;
677 }
678
Vivek Sekharf96064b2014-07-28 16:32:34 -0700679 protected void setTabFullscreen(boolean fullScreen) {
Tarun Nainani8eb00912014-07-17 12:28:32 -0700680 Controller controller = (Controller)mWebViewController;
Sudheer Koganti24766882014-10-02 10:58:09 -0700681 controller.getUi().showFullscreen(fullScreen);
Tarun Nainani8eb00912014-07-17 12:28:32 -0700682 mFullScreen = fullScreen;
Vivek Sekharf96064b2014-07-28 16:32:34 -0700683 }
684
Sudheer Koganti24766882014-10-02 10:58:09 -0700685 public boolean exitFullscreen() {
686 if (mFullScreen) {
687 Controller controller = (Controller)mWebViewController;
688 controller.getUi().showFullscreen(false);
689 if (getWebView() != null)
690 getWebView().exitFullscreen();
691 mFullScreen = false;
692 return true;
693 }
694 return false;
695 }
696
697
698
699
Grace Kloba22ac16e2009-10-07 18:00:23 -0700700 // -------------------------------------------------------------------------
701 // WebChromeClient implementation for the main WebView
702 // -------------------------------------------------------------------------
703
704 private final WebChromeClient mWebChromeClient = new WebChromeClient() {
705 // Helper method to create a new tab or sub window.
706 private void createWindow(final boolean dialog, final Message msg) {
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700707 this.createWindow(dialog, msg, null, false);
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700708 }
709
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700710 private void createWindow(final boolean dialog, final Message msg, final String url,
711 final boolean opener_suppressed) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700712 WebView.WebViewTransport transport =
713 (WebView.WebViewTransport) msg.obj;
714 if (dialog) {
715 createSubWindow();
Michael Kolb8233fac2010-10-26 16:08:53 -0700716 mWebViewController.attachSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700717 transport.setWebView(mSubView);
718 } else {
Pankaj Garg21dad562015-07-02 17:17:24 -0700719 capture();
720
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700721 final Tab newTab = mWebViewController.openTab(url,
John Reck5949c662011-05-27 09:52:29 -0700722 Tab.this, true, true);
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700723 // This is special case for rendering links on a webpage in
724 // a new tab. If opener is suppressed, the WebContents created
725 // by the content layer are not fully initialized. This check
726 // will prevent content layer from overriding WebContents
727 // created by new tab with the uninitialized instance.
728 if (!opener_suppressed) {
729 transport.setWebView(newTab.getWebView());
730 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700731 }
732 msg.sendToTarget();
733 }
734
735 @Override
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700736 public void toggleFullscreenModeForTab(boolean enterFullscreen) {
737 if (mWebViewController instanceof Controller) {
Vivek Sekharf96064b2014-07-28 16:32:34 -0700738 setTabFullscreen(enterFullscreen);
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700739 }
740 }
741
742 @Override
Tarun Nainani8eb00912014-07-17 12:28:32 -0700743 public void onOffsetsForFullscreenChanged(float topControlsOffsetYPix,
744 float contentOffsetYPix,
745 float overdrawBottomHeightPix) {
746 if (mWebViewController instanceof Controller) {
Vivek Sekhar8ee3abb2014-07-14 12:32:05 -0700747 Controller controller = (Controller)mWebViewController;
Tarun Nainani8eb00912014-07-17 12:28:32 -0700748 controller.getUi().translateTitleBar(topControlsOffsetYPix);
Vivek Sekhar11b40062015-06-24 11:49:04 -0700749 // Resize the viewport if top controls is not visible
Vivek Sekhar2ee19a32015-07-02 17:03:57 -0700750 if (mMainView != null &&
751 (topControlsOffsetYPix == 0.0f || contentOffsetYPix == 0.0f))
Vivek Sekhar11b40062015-06-24 11:49:04 -0700752 ((BrowserWebView)mMainView).enableTopControls(
753 (topControlsOffsetYPix == 0.0f) ? true : false);
Vivek Sekhar8ee3abb2014-07-14 12:32:05 -0700754 }
755 }
756
757 @Override
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700758 public boolean isTabFullScreen() {
Vivek Sekharf96064b2014-07-28 16:32:34 -0700759 return mFullScreen;
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700760 }
761
762 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700763 public boolean onCreateWindow(WebView view, final boolean dialog,
764 final boolean userGesture, final Message resultMsg) {
765 // only allow new window or sub window for the foreground case
766 if (!mInForeground) {
767 return false;
768 }
769 // Short-circuit if we can't create any more tabs or sub windows.
770 if (dialog && mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -0700771 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700772 .setTitle(R.string.too_many_subwindows_dialog_title)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200773 .setIconAttribute(android.R.attr.alertDialogIcon)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700774 .setMessage(R.string.too_many_subwindows_dialog_message)
775 .setPositiveButton(R.string.ok, null)
776 .show();
777 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700778 } else if (!mWebViewController.getTabControl().canCreateNewTab()) {
Michael Kolb14612442011-06-24 13:06:29 -0700779 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700780 .setTitle(R.string.too_many_windows_dialog_title)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200781 .setIconAttribute(android.R.attr.alertDialogIcon)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700782 .setMessage(R.string.too_many_windows_dialog_message)
783 .setPositiveButton(R.string.ok, null)
784 .show();
785 return false;
786 }
787
788 // Short-circuit if this was a user gesture.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800789 if (userGesture || !mSettings.blockPopupWindows()) {
Tarun Nainani4f5137d2015-04-16 17:26:18 -0700790 WebView.WebViewTransport transport =
791 (WebView.WebViewTransport) resultMsg.obj;
792 CreateWindowParams windowParams = transport.getCreateWindowParams();
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700793 if (windowParams.mOpenerSuppressed) {
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700794 createWindow(dialog, resultMsg, windowParams.mURL, true);
795 // This is special case for rendering links on a webpage in
796 // a new tab. If opener is suppressed, the WebContents created
797 // by the content layer are not fully initialized. Returning false
798 // will prevent content layer from overriding WebContents
799 // created by new tab with the uninitialized instance.
800 return false;
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700801 }
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700802
803 createWindow(dialog, resultMsg);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700804 return true;
805 }
806
Tarun Nainani4f5137d2015-04-16 17:26:18 -0700807 createWindow(dialog, resultMsg);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700808 return true;
809 }
810
811 @Override
Patrick Scotteb5061b2009-11-18 15:00:30 -0500812 public void onRequestFocus(WebView view) {
813 if (!mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700814 mWebViewController.switchToTab(Tab.this);
Patrick Scotteb5061b2009-11-18 15:00:30 -0500815 }
816 }
817
818 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700819 public void onCloseWindow(WebView window) {
Michael Kolbc831b632011-05-11 09:30:34 -0700820 if (mParent != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700821 // JavaScript can only close popup window.
822 if (mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700823 mWebViewController.switchToTab(mParent);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700824 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700825 mWebViewController.closeTab(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700826 }
827 }
828
829 @Override
830 public void onProgressChanged(WebView view, int newProgress) {
John Reck30c714c2010-12-16 17:30:34 -0800831 mPageLoadProgress = newProgress;
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800832 if (newProgress == 100) {
833 mInPageLoad = false;
834 }
John Reck30c714c2010-12-16 17:30:34 -0800835 mWebViewController.onProgressChanged(Tab.this);
Michael Kolb72864272012-05-03 15:42:15 -0700836 if (mUpdateThumbnail && newProgress == 100) {
837 mUpdateThumbnail = false;
838 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700839 }
840
841 @Override
Leon Scroggins21d9b902010-03-11 09:33:11 -0500842 public void onReceivedTitle(WebView view, final String title) {
John Reck30c714c2010-12-16 17:30:34 -0800843 mCurrentState.mTitle = title;
Michael Kolb8233fac2010-10-26 16:08:53 -0700844 mWebViewController.onReceivedTitle(Tab.this, title);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700845 }
846
847 @Override
848 public void onReceivedIcon(WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -0800849 mCurrentState.mFavicon = icon;
Michael Kolb8233fac2010-10-26 16:08:53 -0700850 mWebViewController.onFavicon(Tab.this, view, icon);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700851 }
852
853 @Override
854 public void onReceivedTouchIconUrl(WebView view, String url,
855 boolean precomposed) {
Michael Kolb14612442011-06-24 13:06:29 -0700856 final ContentResolver cr = mContext.getContentResolver();
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400857 // Let precomposed icons take precedence over non-composed
858 // icons.
859 if (precomposed && mTouchIconLoader != null) {
860 mTouchIconLoader.cancel(false);
861 mTouchIconLoader = null;
862 }
863 // Have only one async task at a time.
864 if (mTouchIconLoader == null) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700865 mTouchIconLoader = new DownloadTouchIcon(Tab.this,
Michael Kolb14612442011-06-24 13:06:29 -0700866 mContext, cr, view);
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400867 mTouchIconLoader.execute(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700868 }
Pankaj Garg21dad562015-07-02 17:17:24 -0700869 mTouchIconUrl = url;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700870 }
871
872 @Override
873 public void onShowCustomView(View view,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800874 CustomViewCallback callback) {
Michael Kolb14612442011-06-24 13:06:29 -0700875 Activity activity = mWebViewController.getActivity();
876 if (activity != null) {
877 onShowCustomView(view, activity.getRequestedOrientation(), callback);
878 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400879 }
880
881 @Override
882 public void onShowCustomView(View view, int requestedOrientation,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800883 CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700884 if (mInForeground) mWebViewController.showCustomView(Tab.this, view,
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400885 requestedOrientation, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700886 }
887
888 @Override
889 public void onHideCustomView() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700890 if (mInForeground) mWebViewController.hideCustomView();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700891 }
892
893 /**
894 * The origin has exceeded its database quota.
895 * @param url the URL that exceeded the quota
896 * @param databaseIdentifier the identifier of the database on which the
897 * transaction that caused the quota overflow was run
898 * @param currentQuota the current quota for the origin.
899 * @param estimatedSize the estimated size of the database.
900 * @param totalUsedQuota is the sum of all origins' quota.
901 * @param quotaUpdater The callback to run when a decision to allow or
902 * deny quota has been made. Don't forget to call this!
903 */
904 @Override
905 public void onExceededDatabaseQuota(String url,
906 String databaseIdentifier, long currentQuota, long estimatedSize,
907 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -0700908 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -0700909 .onExceededDatabaseQuota(url, databaseIdentifier,
910 currentQuota, estimatedSize, totalUsedQuota,
911 quotaUpdater);
912 }
913
914 /**
915 * The Application Cache has exceeded its max size.
916 * @param spaceNeeded is the amount of disk space that would be needed
917 * in order for the last appcache operation to succeed.
918 * @param totalUsedQuota is the sum of all origins' quota.
919 * @param quotaUpdater A callback to inform the WebCore thread that a
920 * new app cache size is available. This callback must always
921 * be executed at some point to ensure that the sleeping
922 * WebCore thread is woken up.
923 */
924 @Override
925 public void onReachedMaxAppCacheSize(long spaceNeeded,
926 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -0700927 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -0700928 .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota,
929 quotaUpdater);
930 }
931
Ben Murdoch65acc352009-11-19 18:16:04 +0000932 /* Adds a JavaScript error message to the system log and if the JS
933 * console is enabled in the about:debug options, to that console
934 * also.
Ben Murdochc42addf2010-01-28 15:19:59 +0000935 * @param consoleMessage the message object.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700936 */
937 @Override
Ben Murdochc42addf2010-01-28 15:19:59 +0000938 public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Jeff Hamilton47654f42010-09-07 09:57:51 -0500939 // Don't log console messages in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -0700940 if (isPrivateBrowsingEnabled()) return true;
Jeff Hamilton47654f42010-09-07 09:57:51 -0500941
Ben Murdochc42addf2010-01-28 15:19:59 +0000942 String message = "Console: " + consoleMessage.message() + " "
943 + consoleMessage.sourceId() + ":"
944 + consoleMessage.lineNumber();
945
946 switch (consoleMessage.messageLevel()) {
947 case TIP:
948 Log.v(CONSOLE_LOGTAG, message);
949 break;
950 case LOG:
951 Log.i(CONSOLE_LOGTAG, message);
952 break;
953 case WARNING:
954 Log.w(CONSOLE_LOGTAG, message);
955 break;
956 case ERROR:
957 Log.e(CONSOLE_LOGTAG, message);
958 break;
959 case DEBUG:
960 Log.d(CONSOLE_LOGTAG, message);
961 break;
962 }
963
964 return true;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700965 }
966
967 /**
968 * Ask the browser for an icon to represent a <video> element.
969 * This icon will be used if the Web page did not specify a poster attribute.
970 * @return Bitmap The icon or null if no such icon is available.
971 */
972 @Override
973 public Bitmap getDefaultVideoPoster() {
974 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700975 return mWebViewController.getDefaultVideoPoster();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700976 }
977 return null;
978 }
979
980 /**
981 * Ask the host application for a custom progress view to show while
982 * a <video> is loading.
983 * @return View The progress view.
984 */
985 @Override
986 public View getVideoLoadingProgressView() {
987 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700988 return mWebViewController.getVideoLoadingProgressView();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700989 }
990 return null;
991 }
992
993 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +0000994 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700995 if (mInForeground) {
Ben Murdoch8cad4132012-01-11 10:56:43 +0000996 mWebViewController.openFileChooser(uploadMsg, acceptType, capture);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700997 } else {
998 uploadMsg.onReceiveValue(null);
999 }
1000 }
1001
Vivek Sekharb54614f2014-05-01 19:03:37 -07001002 @Override
1003 public void showFileChooser(ValueCallback<String[]> uploadFilePaths, String acceptTypes,
1004 boolean capture) {
1005 if (mInForeground) {
1006 mWebViewController.showFileChooser(uploadFilePaths, acceptTypes, capture);
1007 } else {
1008 uploadFilePaths.onReceiveValue(null);
1009 }
1010 }
1011
Grace Kloba22ac16e2009-10-07 18:00:23 -07001012 /**
1013 * Deliver a list of already-visited URLs
1014 */
1015 @Override
1016 public void getVisitedHistory(final ValueCallback<String[]> callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001017 mWebViewController.getVisitedHistory(callback);
1018 }
Ben Murdoch8029a772010-11-16 11:58:21 +00001019
1020 @Override
1021 public void setupAutoFill(Message message) {
1022 // Prompt the user to set up their profile.
1023 final Message msg = message;
Michael Kolb14612442011-06-24 13:06:29 -07001024 AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
1025 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
Ben Murdoch1d676b62011-01-17 12:54:24 +00001026 Context.LAYOUT_INFLATER_SERVICE);
1027 final View layout = inflater.inflate(R.layout.setup_autofill_dialog, null);
1028
1029 builder.setView(layout)
1030 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1031 @Override
1032 public void onClick(DialogInterface dialog, int id) {
1033 CheckBox disableAutoFill = (CheckBox) layout.findViewById(
1034 R.id.setup_autofill_dialog_disable_autofill);
1035
1036 if (disableAutoFill.isChecked()) {
1037 // Disable autofill and show a toast with how to turn it on again.
John Reck35e9dd62011-04-25 09:01:54 -07001038 mSettings.setAutofillEnabled(false);
Michael Kolb14612442011-06-24 13:06:29 -07001039 Toast.makeText(mContext,
Ben Murdoch1d676b62011-01-17 12:54:24 +00001040 R.string.autofill_setup_dialog_negative_toast,
1041 Toast.LENGTH_LONG).show();
1042 } else {
1043 // Take user to the AutoFill profile editor. When they return,
1044 // we will send the message that we pass here which will trigger
1045 // the form to get filled out with their new profile.
1046 mWebViewController.setupAutoFill(msg);
1047 }
1048 }
1049 })
1050 .setNegativeButton(R.string.cancel, null)
1051 .show();
Ben Murdoch8029a772010-11-16 11:58:21 +00001052 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001053 };
1054
1055 // -------------------------------------------------------------------------
1056 // WebViewClient implementation for the sub window
1057 // -------------------------------------------------------------------------
1058
1059 // Subclass of WebViewClient used in subwindows to notify the main
1060 // WebViewClient of certain WebView activities.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001061 private static class SubWindowClient extends WebViewClient {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001062 // The main WebViewClient.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001063 private final WebViewClient mClient;
Michael Kolb8233fac2010-10-26 16:08:53 -07001064 private final WebViewController mController;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001065
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001066 SubWindowClient(WebViewClient client, WebViewController controller) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001067 mClient = client;
Michael Kolb8233fac2010-10-26 16:08:53 -07001068 mController = controller;
Leon Scroggins III211ba542010-04-19 13:21:13 -04001069 }
1070 @Override
1071 public void onPageStarted(WebView view, String url, Bitmap favicon) {
1072 // Unlike the others, do not call mClient's version, which would
1073 // change the progress bar. However, we do want to remove the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001074 // find or select dialog.
Michael Kolb8233fac2010-10-26 16:08:53 -07001075 mController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001076 }
1077 @Override
1078 public void doUpdateVisitedHistory(WebView view, String url,
1079 boolean isReload) {
1080 mClient.doUpdateVisitedHistory(view, url, isReload);
1081 }
1082 @Override
1083 public boolean shouldOverrideUrlLoading(WebView view, String url) {
1084 return mClient.shouldOverrideUrlLoading(view, url);
1085 }
1086 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -07001087 public void onReceivedHttpAuthRequest(WebView view,
1088 HttpAuthHandler handler, String host, String realm) {
1089 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
1090 }
1091 @Override
1092 public void onFormResubmission(WebView view, Message dontResend,
1093 Message resend) {
1094 mClient.onFormResubmission(view, dontResend, resend);
1095 }
1096 @Override
1097 public void onReceivedError(WebView view, int errorCode,
1098 String description, String failingUrl) {
1099 mClient.onReceivedError(view, errorCode, description, failingUrl);
1100 }
1101 @Override
1102 public boolean shouldOverrideKeyEvent(WebView view,
1103 android.view.KeyEvent event) {
1104 return mClient.shouldOverrideKeyEvent(view, event);
1105 }
1106 @Override
1107 public void onUnhandledKeyEvent(WebView view,
1108 android.view.KeyEvent event) {
1109 mClient.onUnhandledKeyEvent(view, event);
1110 }
1111 }
1112
1113 // -------------------------------------------------------------------------
1114 // WebChromeClient implementation for the sub window
1115 // -------------------------------------------------------------------------
1116
1117 private class SubWindowChromeClient extends WebChromeClient {
1118 // The main WebChromeClient.
1119 private final WebChromeClient mClient;
1120
1121 SubWindowChromeClient(WebChromeClient client) {
1122 mClient = client;
1123 }
1124 @Override
1125 public void onProgressChanged(WebView view, int newProgress) {
1126 mClient.onProgressChanged(view, newProgress);
1127 }
1128 @Override
1129 public boolean onCreateWindow(WebView view, boolean dialog,
1130 boolean userGesture, android.os.Message resultMsg) {
1131 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
1132 }
1133 @Override
1134 public void onCloseWindow(WebView window) {
1135 if (window != mSubView) {
1136 Log.e(LOGTAG, "Can't close the window");
1137 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001138 mWebViewController.dismissSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001139 }
1140 }
1141
1142 // -------------------------------------------------------------------------
1143
1144 // Construct a new tab
Michael Kolb7bcafde2011-05-09 13:55:59 -07001145 Tab(WebViewController wvcontroller, WebView w) {
John Reck1cf4b792011-07-26 10:22:22 -07001146 this(wvcontroller, w, null);
1147 }
1148
1149 Tab(WebViewController wvcontroller, Bundle state) {
1150 this(wvcontroller, null, state);
1151 }
1152
1153 Tab(WebViewController wvcontroller, WebView w, Bundle state) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001154 mWebViewController = wvcontroller;
Michael Kolb14612442011-06-24 13:06:29 -07001155 mContext = mWebViewController.getContext();
John Reck35e9dd62011-04-25 09:01:54 -07001156 mSettings = BrowserSettings.getInstance();
Michael Kolb14612442011-06-24 13:06:29 -07001157 mDataController = DataController.getInstance(mContext);
1158 mCurrentState = new PageState(mContext, w != null
John Reck847b5322011-04-14 17:02:18 -07001159 ? w.isPrivateBrowsingEnabled() : false);
Tarun Nainani8084c822014-06-25 13:38:06 -07001160 setTimeStamp();
Michael Kolb8233fac2010-10-26 16:08:53 -07001161 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001162 mInForeground = false;
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001163 mWebViewDestroyedByMemoryMonitor = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001164
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001165 mDownloadListener = new BrowserDownloadListener() {
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001166 public void onDownloadStart(String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001167 String contentDisposition, String mimetype, String referer,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001168 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001169 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001170 mimetype, referer, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001171 }
1172 };
1173
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001174 mCaptureWidth = mContext.getResources().getDimensionPixelSize(R.dimen.tab_thumbnail_width);
1175 mCaptureHeight =mContext.getResources().getDimensionPixelSize(R.dimen.tab_thumbnail_height);
1176
1177 initCaptureBitmap();
1178
John Reck1cf4b792011-07-26 10:22:22 -07001179 restoreState(state);
John Reck52be4782011-08-26 15:37:29 -07001180 if (getId() == -1) {
1181 mId = TabControl.getNextId();
1182 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001183 setWebView(w);
Michael Kolb9ef259a2011-07-12 15:33:08 -07001184 mHandler = new Handler() {
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001185 @Override
Michael Kolb9ef259a2011-07-12 15:33:08 -07001186 public void handleMessage(Message m) {
John Reck1cf4b792011-07-26 10:22:22 -07001187 switch (m.what) {
1188 case MSG_CAPTURE:
1189 capture();
1190 break;
1191 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001192 }
1193 };
Pankaj Garg1c13cab2015-05-12 11:52:17 -07001194
1195 mFirstPixelObservable = new Observable();
1196 mFirstPixelObservable.set(false);
1197 mTabHistoryUpdateObservable = new Observable();
John Reck1cf4b792011-07-26 10:22:22 -07001198 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001199
Pankaj Gargb4b4f012015-08-04 16:19:14 -07001200 public void initCaptureBitmap() {
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001201 mCapture = Bitmap.createBitmap(mCaptureWidth, mCaptureHeight, Bitmap.Config.RGB_565);
1202 mCapture.eraseColor(Color.WHITE);
Michael Kolb72864272012-05-03 15:42:15 -07001203 }
1204
Mathew Inwoode09305e2011-09-02 12:03:26 +01001205 /**
1206 * This is used to get a new ID when the tab has been preloaded, before it is displayed and
1207 * added to TabControl. Preloaded tabs can be created before restoreInstanceState, leading
1208 * to overlapping IDs between the preloaded and restored tabs.
1209 */
1210 public void refreshIdAfterPreload() {
1211 mId = TabControl.getNextId();
1212 }
1213
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001214 public void setController(WebViewController ctl) {
1215 mWebViewController = ctl;
1216
John Reck1cf4b792011-07-26 10:22:22 -07001217 if (mWebViewController.shouldCaptureThumbnails()) {
1218 synchronized (Tab.this) {
1219 if (mCapture == null) {
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001220 initCaptureBitmap();
1221 if (mInForeground && !mHandler.hasMessages(MSG_CAPTURE)) {
1222 mHandler.sendEmptyMessageDelayed(MSG_CAPTURE, CAPTURE_DELAY);
John Reck1cf4b792011-07-26 10:22:22 -07001223 }
1224 }
1225 }
1226 } else {
1227 synchronized (Tab.this) {
1228 mCapture = null;
1229 deleteThumbnail();
1230 }
1231 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001232 }
1233
Michael Kolbc831b632011-05-11 09:30:34 -07001234 public long getId() {
1235 return mId;
1236 }
1237
Michael Kolb91911a22012-01-17 11:21:25 -08001238 void setWebView(WebView w) {
1239 setWebView(w, true);
1240 }
1241
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001242 public boolean isNativeActive(){
1243 if (mMainView == null)
1244 return false;
1245 return true;
1246 }
1247
1248 public void setTimeStamp(){
1249 Date d = new Date();
1250 timestamp = (new Timestamp(d.getTime()));
1251 }
1252
1253 public Timestamp getTimestamp() {
1254 return timestamp;
1255 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001256 /**
1257 * Sets the WebView for this tab, correctly removing the old WebView from
1258 * the container view.
1259 */
Michael Kolb91911a22012-01-17 11:21:25 -08001260 void setWebView(WebView w, boolean restore) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001261 if (mMainView == w) {
1262 return;
1263 }
Michael Kolba713ec82010-11-29 17:27:06 -08001264
Michael Kolba713ec82010-11-29 17:27:06 -08001265 mWebViewController.onSetWebView(this, w);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001266
John Reck1cf4b792011-07-26 10:22:22 -07001267 if (mMainView != null) {
John Reck8ee633f2011-08-09 16:00:35 -07001268 mMainView.setPictureListener(null);
John Reck1cf4b792011-07-26 10:22:22 -07001269 if (w != null) {
1270 syncCurrentState(w, null);
1271 } else {
Panos Thomasa9a5a582014-03-18 19:20:08 -07001272 mCurrentState = new PageState(mContext, mMainView.isPrivateBrowsingEnabled());
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001273
1274 if (mWebViewDestroyedByMemoryMonitor) {
1275 /*
1276 * If tab was destroyed as a result of the MemoryMonitor
1277 * then we need to restore the state properties
1278 * from the old WebView (mMainView)
1279 */
1280 syncCurrentState(mMainView, null);
1281 mWebViewDestroyedByMemoryMonitor = false;
1282 }
John Reck1cf4b792011-07-26 10:22:22 -07001283 }
1284 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001285 // set the new one
1286 mMainView = w;
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001287
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001288 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001289 if (mMainView != null) {
1290 mMainView.setWebViewClient(mWebViewClient);
1291 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001292 // Attach DownloadManager so that downloads can start in an active
1293 // or a non-active window. This can happen when going to a site that
1294 // does a redirect after a period of time. The user could have
1295 // switched to another tab while waiting for the download to start.
1296 mMainView.setDownloadListener(mDownloadListener);
John Reck8ee633f2011-08-09 16:00:35 -07001297 TabControl tc = mWebViewController.getTabControl();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001298 if (tc != null /*&& tc.getOnThumbnailUpdatedListener() != null*/) {
John Reck8ee633f2011-08-09 16:00:35 -07001299 mMainView.setPictureListener(this);
1300 }
Michael Kolb91911a22012-01-17 11:21:25 -08001301 if (restore && (mSavedState != null)) {
John Reck8b9bb8b2012-03-08 13:19:40 -08001302 restoreUserAgent();
John Reck6c2e2f32011-08-22 13:41:23 -07001303 WebBackForwardList restoredState
1304 = mMainView.restoreState(mSavedState);
1305 if (restoredState == null || restoredState.getSize() == 0) {
1306 Log.w(LOGTAG, "Failed to restore WebView state!");
1307 loadUrl(mCurrentState.mOriginalUrl, null);
1308 }
John Reck1cf4b792011-07-26 10:22:22 -07001309 mSavedState = null;
1310 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001311 }
1312 }
1313
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001314 public void destroyThroughMemoryMonitor() {
1315 mWebViewDestroyedByMemoryMonitor = true;
1316 destroy();
1317 }
1318
Grace Kloba22ac16e2009-10-07 18:00:23 -07001319 /**
1320 * Destroy the tab's main WebView and subWindow if any
1321 */
1322 void destroy() {
Tarun Nainani2c1dd7c2014-07-05 16:40:12 -07001323
Matthew Huib7f2e9c2014-04-16 11:12:37 -04001324 if (mPostponeDestroy) {
1325 mShouldDestroy = true;
1326 return;
1327 }
1328 mShouldDestroy = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001329 if (mMainView != null) {
1330 dismissSubWindow();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001331 // save the WebView to call destroy() after detach it from the tab
Vivek Sekhar53ef8932015-06-18 16:51:43 -07001332 final WebView webView = mMainView;
Vivek Sekhar2ee19a32015-07-02 17:03:57 -07001333 setWebView(null);
Site Mao61b68212015-07-16 10:56:31 -07001334 if (!mWebViewDestroyedByMemoryMonitor && !BaseUi.isUiLowPowerMode()) {
Vivek Sekhar2ee19a32015-07-02 17:03:57 -07001335 // Tabs can be reused with new instance of WebView so delete the snapshots
Vivek Sekhar53ef8932015-06-18 16:51:43 -07001336 webView.getSnapshotIds(new ValueCallback<List<Integer>>() {
1337 @Override
1338 public void onReceiveValue(List<Integer> ids) {
1339 int currentTabIdx = mWebViewController.getTabControl().getCurrentPosition();
1340 for (Integer id : ids) {
1341 if (getTabIdxFromCaptureIdx(id) == currentTabIdx) {
1342 webView.deleteSnapshot(id);
1343 }
1344 }
Vivek Sekhar53ef8932015-06-18 16:51:43 -07001345 webView.destroy();
Pankaj Garg1c13cab2015-05-12 11:52:17 -07001346 }
Vivek Sekhar53ef8932015-06-18 16:51:43 -07001347 });
Vivek Sekhar2ee19a32015-07-02 17:03:57 -07001348 } else {
1349 webView.destroy();
Pankaj Garg1c13cab2015-05-12 11:52:17 -07001350 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001351 }
1352 }
1353
Matthew Huib7f2e9c2014-04-16 11:12:37 -04001354 private boolean mPostponeDestroy = false;
1355 private boolean mShouldDestroy = false;
1356
1357 public void postponeDestroy() {
1358 mPostponeDestroy = true;
1359 }
1360
1361 public void performPostponedDestroy() {
1362 mPostponeDestroy = false;
1363 if (mShouldDestroy) {
1364 destroy();
1365 }
1366 }
1367
Grace Kloba22ac16e2009-10-07 18:00:23 -07001368 /**
1369 * Remove the tab from the parent
1370 */
1371 void removeFromTree() {
1372 // detach the children
Michael Kolbc831b632011-05-11 09:30:34 -07001373 if (mChildren != null) {
1374 for(Tab t : mChildren) {
1375 t.setParent(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001376 }
1377 }
1378 // remove itself from the parent list
Michael Kolbc831b632011-05-11 09:30:34 -07001379 if (mParent != null) {
1380 mParent.mChildren.remove(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001381 }
Tarun Nainani8eb00912014-07-17 12:28:32 -07001382
1383 mCapture = null;
John Reck1cf4b792011-07-26 10:22:22 -07001384 deleteThumbnail();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001385 }
1386
1387 /**
1388 * Create a new subwindow unless a subwindow already exists.
1389 * @return True if a new subwindow was created. False if one already exists.
1390 */
1391 boolean createSubWindow() {
1392 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001393 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001394 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001395 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001396 mSubView.setWebChromeClient(new SubWindowChromeClient(
1397 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001398 // Set a different DownloadListener for the mSubView, since it will
1399 // just need to dismiss the mSubView, rather than close the Tab
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001400 mSubView.setDownloadListener(new BrowserDownloadListener() {
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001401 public void onDownloadStart(String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001402 String contentDisposition, String mimetype, String referer,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001403 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001404 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001405 contentDisposition, mimetype, referer, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001406 if (mSubView.copyBackForwardList().getSize() == 0) {
1407 // This subwindow was opened for the sole purpose of
1408 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001409 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001410 }
1411 }
1412 });
Michael Kolb14612442011-06-24 13:06:29 -07001413 mSubView.setOnCreateContextMenuListener(mWebViewController.getActivity());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001414 return true;
1415 }
1416 return false;
1417 }
1418
1419 /**
1420 * Dismiss the subWindow for the tab.
1421 */
1422 void dismissSubWindow() {
1423 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001424 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001425 mSubView.destroy();
1426 mSubView = null;
1427 mSubViewContainer = null;
1428 }
1429 }
1430
Grace Kloba22ac16e2009-10-07 18:00:23 -07001431
1432 /**
1433 * Set the parent tab of this tab.
1434 */
Michael Kolbc831b632011-05-11 09:30:34 -07001435 void setParent(Tab parent) {
John Reck52be4782011-08-26 15:37:29 -07001436 if (parent == this) {
1437 throw new IllegalStateException("Cannot set parent to self!");
1438 }
Michael Kolbc831b632011-05-11 09:30:34 -07001439 mParent = parent;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001440 // This tab may have been freed due to low memory. If that is the case,
Michael Kolbc831b632011-05-11 09:30:34 -07001441 // the parent tab id is already saved. If we are changing that id
Grace Kloba22ac16e2009-10-07 18:00:23 -07001442 // (most likely due to removing the parent tab) we must update the
Michael Kolbc831b632011-05-11 09:30:34 -07001443 // parent tab id in the saved Bundle.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001444 if (mSavedState != null) {
1445 if (parent == null) {
1446 mSavedState.remove(PARENTTAB);
1447 } else {
Michael Kolbc831b632011-05-11 09:30:34 -07001448 mSavedState.putLong(PARENTTAB, parent.getId());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001449 }
1450 }
John Reckb0a86db2011-05-24 14:05:58 -07001451
1452 // Sync the WebView useragent with the parent
1453 if (parent != null && mSettings.hasDesktopUseragent(parent.getWebView())
1454 != mSettings.hasDesktopUseragent(getWebView())) {
1455 mSettings.toggleDesktopUseragent(getWebView());
1456 }
John Reck52be4782011-08-26 15:37:29 -07001457
1458 if (parent != null && parent.getId() == getId()) {
1459 throw new IllegalStateException("Parent has same ID as child!");
1460 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001461 }
1462
1463 /**
Michael Kolbc831b632011-05-11 09:30:34 -07001464 * If this Tab was created through another Tab, then this method returns
1465 * that Tab.
1466 * @return the Tab parent or null
1467 */
1468 public Tab getParent() {
1469 return mParent;
1470 }
1471
1472 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001473 * When a Tab is created through the content of another Tab, then we
1474 * associate the Tabs.
1475 * @param child the Tab that was created from this Tab
1476 */
1477 void addChildTab(Tab child) {
Michael Kolbc831b632011-05-11 09:30:34 -07001478 if (mChildren == null) {
1479 mChildren = new Vector<Tab>();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001480 }
Michael Kolbc831b632011-05-11 09:30:34 -07001481 mChildren.add(child);
1482 child.setParent(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001483 }
1484
Michael Kolbc831b632011-05-11 09:30:34 -07001485 Vector<Tab> getChildren() {
1486 return mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001487 }
1488
1489 void resume() {
1490 if (mMainView != null) {
John Reck56c1fcf2011-08-17 10:15:16 -07001491 setupHwAcceleration(mMainView);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001492 mMainView.onResume();
1493 if (mSubView != null) {
1494 mSubView.onResume();
1495 }
1496 }
1497 }
1498
John Reck56c1fcf2011-08-17 10:15:16 -07001499 private void setupHwAcceleration(View web) {
1500 if (web == null) return;
1501 BrowserSettings settings = BrowserSettings.getInstance();
1502 if (settings.isHardwareAccelerated()) {
1503 web.setLayerType(View.LAYER_TYPE_NONE, null);
1504 } else {
1505 web.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
1506 }
1507 }
1508
Grace Kloba22ac16e2009-10-07 18:00:23 -07001509 void pause() {
1510 if (mMainView != null) {
1511 mMainView.onPause();
1512 if (mSubView != null) {
1513 mSubView.onPause();
1514 }
1515 }
1516 }
1517
1518 void putInForeground() {
John Reck8ee633f2011-08-09 16:00:35 -07001519 if (mInForeground) {
1520 return;
1521 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001522 mInForeground = true;
1523 resume();
Michael Kolb14612442011-06-24 13:06:29 -07001524 Activity activity = mWebViewController.getActivity();
1525 mMainView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001526 if (mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -07001527 mSubView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001528 }
Axesh R. Ajmerac6b5c322015-05-01 11:06:10 -07001529
Leon Scroggins1961ed22010-12-07 15:22:21 -05001530 mWebViewController.bookmarkedStatusHasChanged(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001531 }
1532
1533 void putInBackground() {
John Reck8ee633f2011-08-09 16:00:35 -07001534 if (!mInForeground) {
1535 return;
1536 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001537 mInForeground = false;
1538 pause();
1539 mMainView.setOnCreateContextMenuListener(null);
1540 if (mSubView != null) {
1541 mSubView.setOnCreateContextMenuListener(null);
1542 }
1543 }
1544
Michael Kolb8233fac2010-10-26 16:08:53 -07001545 boolean inForeground() {
1546 return mInForeground;
1547 }
1548
Grace Kloba22ac16e2009-10-07 18:00:23 -07001549 /**
1550 * Return the top window of this tab; either the subwindow if it is not
1551 * null or the main window.
1552 * @return The top window of this tab.
1553 */
1554 WebView getTopWindow() {
1555 if (mSubView != null) {
1556 return mSubView;
1557 }
1558 return mMainView;
1559 }
1560
1561 /**
1562 * Return the main window of this tab. Note: if a tab is freed in the
1563 * background, this can return null. It is only guaranteed to be
1564 * non-null for the current tab.
1565 * @return The main WebView of this tab.
1566 */
1567 WebView getWebView() {
1568 return mMainView;
1569 }
1570
Michael Kolba713ec82010-11-29 17:27:06 -08001571 void setViewContainer(View container) {
1572 mContainer = container;
1573 }
1574
Michael Kolb8233fac2010-10-26 16:08:53 -07001575 View getViewContainer() {
1576 return mContainer;
1577 }
1578
Grace Kloba22ac16e2009-10-07 18:00:23 -07001579 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001580 * Return whether private browsing is enabled for the main window of
1581 * this tab.
1582 * @return True if private browsing is enabled.
1583 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001584 boolean isPrivateBrowsingEnabled() {
John Reck502a3532011-08-16 14:21:46 -07001585 return mCurrentState.mIncognito;
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001586 }
1587
1588 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001589 * Return the subwindow of this tab or null if there is no subwindow.
1590 * @return The subwindow of this tab or null.
1591 */
1592 WebView getSubWebView() {
1593 return mSubView;
1594 }
1595
Michael Kolb1514bb72010-11-22 09:11:48 -08001596 void setSubWebView(WebView subView) {
1597 mSubView = subView;
1598 }
1599
Michael Kolb8233fac2010-10-26 16:08:53 -07001600 View getSubViewContainer() {
1601 return mSubViewContainer;
1602 }
1603
Michael Kolb1514bb72010-11-22 09:11:48 -08001604 void setSubViewContainer(View subViewContainer) {
1605 mSubViewContainer = subViewContainer;
1606 }
1607
Grace Kloba22ac16e2009-10-07 18:00:23 -07001608
1609 /**
1610 * @return The application id string
1611 */
1612 String getAppId() {
1613 return mAppId;
1614 }
1615
1616 /**
1617 * Set the application id string
1618 * @param id
1619 */
1620 void setAppId(String id) {
1621 mAppId = id;
1622 }
1623
Michael Kolbe28b3472011-08-04 16:54:31 -07001624 boolean closeOnBack() {
1625 return mCloseOnBack;
1626 }
1627
1628 void setCloseOnBack(boolean close) {
1629 mCloseOnBack = close;
1630 }
1631
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001632 boolean getDerivedFromIntent() {
1633 return mDerivedFromIntent;
1634 }
1635
1636 void setDerivedFromIntent(boolean derived) {
1637 mDerivedFromIntent = derived;
1638 }
1639
Grace Kloba22ac16e2009-10-07 18:00:23 -07001640 String getUrl() {
John Reck324d4402011-01-11 16:56:42 -08001641 return UrlUtils.filteredUrl(mCurrentState.mUrl);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001642 }
1643
Tarun Nainani8eb00912014-07-17 12:28:32 -07001644
1645 protected void onPageFinished() {
1646 mPageFinished = true;
Ze G Riande2a675c22015-06-03 11:15:24 -07001647 isDistillable();
Tarun Nainani8eb00912014-07-17 12:28:32 -07001648 }
1649
1650 public boolean getPageFinishedStatus() {
1651 return mPageFinished;
1652 }
1653
John Reck49a603c2011-03-03 09:33:05 -08001654 String getOriginalUrl() {
John Reckdb22ec42011-06-29 11:31:24 -07001655 if (mCurrentState.mOriginalUrl == null) {
1656 return getUrl();
John Reck49a603c2011-03-03 09:33:05 -08001657 }
John Reckdb22ec42011-06-29 11:31:24 -07001658 return UrlUtils.filteredUrl(mCurrentState.mOriginalUrl);
John Reck49a603c2011-03-03 09:33:05 -08001659 }
1660
Grace Kloba22ac16e2009-10-07 18:00:23 -07001661 /**
John Reck30c714c2010-12-16 17:30:34 -08001662 * Get the title of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001663 */
1664 String getTitle() {
John Reck30c714c2010-12-16 17:30:34 -08001665 return mCurrentState.mTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001666 }
1667
1668 /**
John Reck30c714c2010-12-16 17:30:34 -08001669 * Get the favicon of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001670 */
1671 Bitmap getFavicon() {
John Reck1cf4b792011-07-26 10:22:22 -07001672 if (mCurrentState.mFavicon != null) {
1673 return mCurrentState.mFavicon;
1674 }
1675 return getDefaultFavicon(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001676 }
1677
Pankaj Garg32e1b942015-06-03 18:13:24 -07001678 public boolean hasFavicon() {
1679 return mCurrentState.mFavicon != null;
1680 }
1681
John Recke969cc52010-12-21 17:24:43 -08001682 public boolean isBookmarkedSite() {
1683 return mCurrentState.mIsBookmarkedSite;
1684 }
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001685
Grace Kloba22ac16e2009-10-07 18:00:23 -07001686 /**
Steve Block08a6f0c2011-10-06 12:12:53 +01001687 * Sets the security state, clears the SSL certificate error and informs
1688 * the controller.
1689 */
Steve Block2466eff2011-10-03 15:33:09 +01001690 private void setSecurityState(SecurityState securityState) {
1691 mCurrentState.mSecurityState = securityState;
1692 mWebViewController.onUpdatedSecurityState(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001693 }
1694
1695 /**
Steve Block2466eff2011-10-03 15:33:09 +01001696 * @return The tab's security state.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001697 */
Steve Block2466eff2011-10-03 15:33:09 +01001698 SecurityState getSecurityState() {
1699 return mCurrentState.mSecurityState;
John Reck30c714c2010-12-16 17:30:34 -08001700 }
1701
1702 int getLoadProgress() {
1703 if (mInPageLoad) {
1704 return mPageLoadProgress;
1705 }
1706 return 100;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001707 }
1708
1709 /**
1710 * @return TRUE if onPageStarted is called while onPageFinished is not
1711 * called yet.
1712 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001713 boolean inPageLoad() {
1714 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001715 }
1716
Grace Kloba22ac16e2009-10-07 18:00:23 -07001717 /**
John Reck1cf4b792011-07-26 10:22:22 -07001718 * @return The Bundle with the tab's state if it can be saved, otherwise null
Grace Kloba22ac16e2009-10-07 18:00:23 -07001719 */
John Reck1cf4b792011-07-26 10:22:22 -07001720 public Bundle saveState() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001721 // If the WebView is null it means we ran low on memory and we already
1722 // stored the saved state in mSavedState.
1723 if (mMainView == null) {
John Reck1cf4b792011-07-26 10:22:22 -07001724 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001725 }
John Reck6c2e2f32011-08-22 13:41:23 -07001726
1727 if (TextUtils.isEmpty(mCurrentState.mUrl)) {
John Reck1cf4b792011-07-26 10:22:22 -07001728 return null;
John Reck24f18262011-06-17 14:47:20 -07001729 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001730
1731 mSavedState = new Bundle();
John Reck6c2e2f32011-08-22 13:41:23 -07001732 WebBackForwardList savedList = mMainView.saveState(mSavedState);
1733 if (savedList == null || savedList.getSize() == 0) {
1734 Log.w(LOGTAG, "Failed to save back/forward list for "
1735 + mCurrentState.mUrl);
1736 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001737
Michael Kolbc831b632011-05-11 09:30:34 -07001738 mSavedState.putLong(ID, mId);
John Reck30c714c2010-12-16 17:30:34 -08001739 mSavedState.putString(CURRURL, mCurrentState.mUrl);
1740 mSavedState.putString(CURRTITLE, mCurrentState.mTitle);
John Reck1cf4b792011-07-26 10:22:22 -07001741 mSavedState.putBoolean(INCOGNITO, mMainView.isPrivateBrowsingEnabled());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001742 if (mAppId != null) {
1743 mSavedState.putString(APPID, mAppId);
1744 }
Michael Kolbe28b3472011-08-04 16:54:31 -07001745 mSavedState.putBoolean(CLOSEFLAG, mCloseOnBack);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001746 // Remember the parent tab so the relationship can be restored.
Michael Kolbc831b632011-05-11 09:30:34 -07001747 if (mParent != null) {
1748 mSavedState.putLong(PARENTTAB, mParent.mId);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001749 }
John Reckb0a86db2011-05-24 14:05:58 -07001750 mSavedState.putBoolean(USERAGENT,
1751 mSettings.hasDesktopUseragent(getWebView()));
John Reck1cf4b792011-07-26 10:22:22 -07001752 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001753 }
1754
1755 /*
1756 * Restore the state of the tab.
1757 */
John Reck1cf4b792011-07-26 10:22:22 -07001758 private void restoreState(Bundle b) {
1759 mSavedState = b;
1760 if (mSavedState == null) {
1761 return;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001762 }
1763 // Restore the internal state even if the WebView fails to restore.
1764 // This will maintain the app id, original url and close-on-exit values.
Michael Kolbc831b632011-05-11 09:30:34 -07001765 mId = b.getLong(ID);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001766 mAppId = b.getString(APPID);
Michael Kolbe28b3472011-08-04 16:54:31 -07001767 mCloseOnBack = b.getBoolean(CLOSEFLAG);
John Reck8b9bb8b2012-03-08 13:19:40 -08001768 restoreUserAgent();
John Reck1cf4b792011-07-26 10:22:22 -07001769 String url = b.getString(CURRURL);
1770 String title = b.getString(CURRTITLE);
1771 boolean incognito = b.getBoolean(INCOGNITO);
1772 mCurrentState = new PageState(mContext, incognito, url, null);
1773 mCurrentState.mTitle = title;
1774 synchronized (Tab.this) {
1775 if (mCapture != null) {
John Reck4eadc342011-10-31 14:04:10 -07001776 DataController.getInstance(mContext).loadThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07001777 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001778 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001779 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04001780
John Reck8b9bb8b2012-03-08 13:19:40 -08001781 private void restoreUserAgent() {
1782 if (mMainView == null || mSavedState == null) {
1783 return;
1784 }
1785 if (mSavedState.getBoolean(USERAGENT)
1786 != mSettings.hasDesktopUseragent(mMainView)) {
1787 mSettings.toggleDesktopUseragent(mMainView);
1788 }
1789 }
1790
Leon Scroggins1961ed22010-12-07 15:22:21 -05001791 public void updateBookmarkedStatus() {
John Recke969cc52010-12-21 17:24:43 -08001792 mDataController.queryBookmarkStatus(getUrl(), mIsBookmarkCallback);
Leon Scroggins1961ed22010-12-07 15:22:21 -05001793 }
1794
John Recke969cc52010-12-21 17:24:43 -08001795 private DataController.OnQueryUrlIsBookmark mIsBookmarkCallback
1796 = new DataController.OnQueryUrlIsBookmark() {
1797 @Override
1798 public void onQueryUrlIsBookmark(String url, boolean isBookmark) {
1799 if (mCurrentState.mUrl.equals(url)) {
1800 mCurrentState.mIsBookmarkedSite = isBookmark;
1801 mWebViewController.bookmarkedStatusHasChanged(Tab.this);
1802 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001803 }
John Recke969cc52010-12-21 17:24:43 -08001804 };
Michael Kolb1acef692011-03-08 14:12:06 -08001805
Michael Kolbeb95db42011-03-03 10:38:40 -08001806 public Bitmap getScreenshot() {
John Reck1cf4b792011-07-26 10:22:22 -07001807 synchronized (Tab.this) {
1808 return mCapture;
1809 }
Michael Kolbeb95db42011-03-03 10:38:40 -08001810 }
1811
John Reck541f55a2011-06-07 16:34:43 -07001812 public boolean isSnapshot() {
John Reck541f55a2011-06-07 16:34:43 -07001813 return false;
1814 }
1815
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001816 private static class SaveCallback implements ValueCallback<String> {
1817 boolean onReceiveValueCalled = false;
1818 private String mPath;
John Reck68234a92012-04-19 15:27:12 -07001819
1820 @Override
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001821 public void onReceiveValue(String path) {
1822 this.onReceiveValueCalled = true;
1823 this.mPath = path;
John Reck68234a92012-04-19 15:27:12 -07001824 synchronized (this) {
1825 notifyAll();
John Reck8cc92352011-07-06 17:41:52 -07001826 }
John Reck541f55a2011-06-07 16:34:43 -07001827 }
John Reck68234a92012-04-19 15:27:12 -07001828
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001829 public String getPath() {
1830 return mPath;
1831 }
John Reck68234a92012-04-19 15:27:12 -07001832 }
1833
1834 /**
1835 * Must be called on the UI thread
1836 */
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001837 public ContentValues createSnapshotValues(Bitmap bm) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001838 WebView web = getWebView();
John Reck68234a92012-04-19 15:27:12 -07001839 if (web == null) return null;
John Reckd8c74522011-06-14 08:45:00 -07001840 ContentValues values = new ContentValues();
1841 values.put(Snapshots.TITLE, mCurrentState.mTitle);
1842 values.put(Snapshots.URL, mCurrentState.mUrl);
John Reck68234a92012-04-19 15:27:12 -07001843 values.put(Snapshots.BACKGROUND, web.getPageBackgroundColor());
John Reck8cc92352011-07-06 17:41:52 -07001844 values.put(Snapshots.DATE_CREATED, System.currentTimeMillis());
1845 values.put(Snapshots.FAVICON, compressBitmap(getFavicon()));
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001846 values.put(Snapshots.THUMBNAIL, compressBitmap(bm));
John Reckd8c74522011-06-14 08:45:00 -07001847 return values;
John Reck541f55a2011-06-07 16:34:43 -07001848 }
1849
John Reck68234a92012-04-19 15:27:12 -07001850 /**
1851 * Probably want to call this on a background thread
1852 */
1853 public boolean saveViewState(ContentValues values) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001854 WebView web = getWebView();
John Reck68234a92012-04-19 15:27:12 -07001855 if (web == null) return false;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001856 String filename = UUID.randomUUID().toString();
John Reck68234a92012-04-19 15:27:12 -07001857 SaveCallback callback = new SaveCallback();
John Reck68234a92012-04-19 15:27:12 -07001858 try {
John Reck68234a92012-04-19 15:27:12 -07001859 synchronized (callback) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001860 web.saveViewState(filename, callback);
1861 callback.wait();
John Reck68234a92012-04-19 15:27:12 -07001862 }
John Reck68234a92012-04-19 15:27:12 -07001863 } catch (Exception e) {
1864 Log.w(LOGTAG, "Failed to save view state", e);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001865 String path = callback.getPath();
1866 if (path != null) {
1867 File file = mContext.getFileStreamPath(path);
1868 if (file.exists() && !file.delete()) {
1869 file.deleteOnExit();
1870 }
John Reck68234a92012-04-19 15:27:12 -07001871 }
1872 return false;
1873 }
Tarun Nainani8eb00912014-07-17 12:28:32 -07001874
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001875 String path = callback.getPath();
Tarun Nainani8eb00912014-07-17 12:28:32 -07001876 // could be that saving of file failed
1877 if (path == null) {
1878 return false;
1879 }
1880
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001881 File savedFile = new File(path);
1882 if (!savedFile.exists()) {
1883 return false;
John Reck68234a92012-04-19 15:27:12 -07001884 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001885 values.put(Snapshots.VIEWSTATE_PATH, path.substring(path.lastIndexOf('/') + 1));
1886 values.put(Snapshots.VIEWSTATE_SIZE, savedFile.length());
John Reck68234a92012-04-19 15:27:12 -07001887 return true;
1888 }
1889
John Reck8cc92352011-07-06 17:41:52 -07001890 public byte[] compressBitmap(Bitmap bitmap) {
1891 if (bitmap == null) {
1892 return null;
1893 }
1894 ByteArrayOutputStream stream = new ByteArrayOutputStream();
1895 bitmap.compress(CompressFormat.PNG, 100, stream);
1896 return stream.toByteArray();
1897 }
1898
John Reck26b18322011-06-21 13:08:58 -07001899 public void loadUrl(String url, Map<String, String> headers) {
1900 if (mMainView != null) {
Michael Kolba53c9892011-10-05 13:31:40 -07001901 mPageLoadProgress = INITIAL_PROGRESS;
Michael Kolb14612442011-06-24 13:06:29 -07001902 mCurrentState = new PageState(mContext, false, url, null);
John Reck26b18322011-06-21 13:08:58 -07001903 mMainView.loadUrl(url, headers);
1904 }
1905 }
1906
John Reck38b39652012-06-05 09:22:59 -07001907 public void disableUrlOverridingForLoad() {
1908 mDisableOverrideUrlLoading = true;
1909 }
1910
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001911 private void thumbnailUpdated() {
John Reck8ee633f2011-08-09 16:00:35 -07001912 mHandler.removeMessages(MSG_CAPTURE);
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001913
John Reck8ee633f2011-08-09 16:00:35 -07001914 TabControl tc = mWebViewController.getTabControl();
1915 if (tc != null) {
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001916 OnThumbnailUpdatedListener updateListener = tc.getOnThumbnailUpdatedListener();
John Reck8ee633f2011-08-09 16:00:35 -07001917 if (updateListener != null) {
1918 updateListener.onThumbnailUpdated(this);
1919 }
1920 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001921 }
1922
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001923 protected void capture() {
1924 if (mMainView == null || mCapture == null || !mMainView.isReady() ||
1925 mMainView.getContentWidth() <= 0 || mMainView.getContentHeight() <= 0 ||
1926 !mFirstVisualPixelPainted || mMainView.isShowingCrashView()) {
1927
1928 initCaptureBitmap();
1929 thumbnailUpdated();
1930 return;
1931 }
1932
1933 mMainView.getContentBitmapAsync((float) mCaptureWidth / mMainView.getWidth(), new Rect(),
1934 new ValueCallback<Bitmap>() {
1935 @Override
1936 public void onReceiveValue(Bitmap bitmap) {
1937 if (mCapture == null) {
1938 initCaptureBitmap();
1939 }
1940
1941 if (bitmap == null) {
1942 thumbnailUpdated();
1943 return;
1944 }
1945
1946 Canvas c = new Canvas(mCapture);
1947 mCapture.eraseColor(Color.WHITE);
1948 c.drawBitmap(bitmap, 0, 0, null);
1949
1950 // manually anti-alias the edges for the tilt
1951 c.drawRect(0, 0, 1, mCapture.getHeight(), sAlphaPaint);
1952 c.drawRect(mCapture.getWidth() - 1, 0, mCapture.getWidth(),
1953 mCapture.getHeight(), sAlphaPaint);
1954 c.drawRect(0, 0, mCapture.getWidth(), 1, sAlphaPaint);
1955 c.drawRect(0, mCapture.getHeight() - 1, mCapture.getWidth(),
1956 mCapture.getHeight(), sAlphaPaint);
1957 c.setBitmap(null);
1958
1959 persistThumbnail();
1960 thumbnailUpdated();
1961 }
1962 }
1963 );
John Reck1cf4b792011-07-26 10:22:22 -07001964 }
1965
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001966 @Override
1967 public void onNewPicture(WebView view, Picture picture) {
Michael Kolb9ef259a2011-07-12 15:33:08 -07001968 }
1969
John Reckef654f12011-07-12 16:42:08 -07001970 public boolean canGoBack() {
1971 return mMainView != null ? mMainView.canGoBack() : false;
1972 }
1973
1974 public boolean canGoForward() {
1975 return mMainView != null ? mMainView.canGoForward() : false;
1976 }
1977
1978 public void goBack() {
1979 if (mMainView != null) {
1980 mMainView.goBack();
1981 }
1982 }
1983
1984 public void goForward() {
1985 if (mMainView != null) {
1986 mMainView.goForward();
1987 }
1988 }
1989
John Reck1cf4b792011-07-26 10:22:22 -07001990 protected void persistThumbnail() {
John Reck4eadc342011-10-31 14:04:10 -07001991 DataController.getInstance(mContext).saveThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07001992 }
1993
1994 protected void deleteThumbnail() {
John Reck4eadc342011-10-31 14:04:10 -07001995 DataController.getInstance(mContext).deleteThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07001996 }
1997
John Reck4eadc342011-10-31 14:04:10 -07001998 void updateCaptureFromBlob(byte[] blob) {
John Reck1cf4b792011-07-26 10:22:22 -07001999 synchronized (Tab.this) {
2000 if (mCapture == null) {
2001 return;
2002 }
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002003 ByteBuffer buffer = ByteBuffer.wrap(blob);
Michael Kolbd837a112011-08-09 14:00:18 -07002004 try {
2005 mCapture.copyPixelsFromBuffer(buffer);
2006 } catch (RuntimeException rex) {
2007 Log.e(LOGTAG, "Load capture has mismatched sizes; buffer: "
2008 + buffer.capacity() + " blob: " + blob.length
2009 + "capture: " + mCapture.getByteCount());
2010 throw rex;
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002011 }
John Reck1cf4b792011-07-26 10:22:22 -07002012 }
2013 }
2014
John Reck52be4782011-08-26 15:37:29 -07002015 @Override
2016 public String toString() {
2017 StringBuilder builder = new StringBuilder(100);
2018 builder.append(mId);
2019 builder.append(") has parent: ");
2020 if (getParent() != null) {
2021 builder.append("true[");
2022 builder.append(getParent().getId());
2023 builder.append("]");
2024 } else {
2025 builder.append("false");
2026 }
2027 builder.append(", incog: ");
2028 builder.append(isPrivateBrowsingEnabled());
2029 if (!isPrivateBrowsingEnabled()) {
2030 builder.append(", title: ");
2031 builder.append(getTitle());
2032 builder.append(", url: ");
2033 builder.append(getUrl());
2034 }
2035 return builder.toString();
2036 }
2037
Ze G Riande2a675c22015-06-03 11:15:24 -07002038 // dertermines if the tab contains a dislled page
2039 public boolean isDistilled() {
2040 if (!BrowserCommandLine.hasSwitch("reader-mode")) {
2041 return false;
2042 }
2043 try {
2044 return DomDistillerUtils.isUrlDistilled(getUrl());
2045 } catch (Exception e) {
2046 return false;
2047 }
2048 }
2049
2050 //determines if the tab contains a distillable page
2051 public boolean isDistillable() {
2052 if (!BrowserCommandLine.hasSwitch("reader-mode")) {
2053 mIsDistillable = false;
2054 return mIsDistillable;
2055 }
2056 final ValueCallback<String> onIsDistillable = new ValueCallback<String>() {
2057 @Override
2058 public void onReceiveValue(String str) {
2059 mIsDistillable = Boolean.parseBoolean(str);
2060 }
2061 };
2062
2063 if (isDistilled()) {
2064 mIsDistillable = true;
2065 return mIsDistillable;
2066 }
2067
2068 try {
2069 DomDistillerUtils.isWebViewDistillable(getWebView(), onIsDistillable);
2070 } catch (Exception e) {
2071 mIsDistillable = false;
2072 }
2073
2074 return mIsDistillable;
2075 }
2076
2077 // Function that sets the mIsDistillable variable
2078 public void setIsDistillable(boolean value) {
2079 if (!BrowserCommandLine.hasSwitch("reader-mode")) {
2080 mIsDistillable = false;
2081 }
2082 mIsDistillable = value;
2083 }
2084
2085 // Function that returns the distilled url of the current url
2086 public String getDistilledUrl() {
2087 if (getUrl() != null) {
2088 return DomDistillerUtils.getDistilledUrl(getUrl());
2089 }
2090 return new String();
2091 }
2092
2093 // function that returns the non-distilled version of the current url
2094 public String getNonDistilledUrl() {
2095 if (getUrl() != null) {
2096 return DomDistillerUtils.getOriginalUrlFromDistilledUrl(getUrl());
2097 }
2098 return new String();
2099 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002100}