blob: d3621ef51fbcab70ab0f9ca9c1c4e0c7028ee95f [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;
Axesh R. Ajmerad04cad12015-08-12 12:02:44 -0700231 mOriginalUrl = mUrl = "";
John Reck502a3532011-08-16 14:21:46 -0700232 if (mIncognito) {
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 Reck30c714c2010-12-16 17:30:34 -0800235 mTitle = c.getString(R.string.new_tab);
John Reck30c714c2010-12-16 17:30:34 -0800236 }
Steve Block2466eff2011-10-03 15:33:09 +0100237 mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
John Reck30c714c2010-12-16 17:30:34 -0800238 }
239
240 PageState(Context c, boolean incognito, String url, Bitmap favicon) {
John Reck502a3532011-08-16 14:21:46 -0700241 mIncognito = incognito;
Axesh R. Ajmerad04cad12015-08-12 12:02:44 -0700242 if (mIncognito)
243 mOriginalUrl = mUrl = "";
244 else
245 mOriginalUrl = mUrl = url;
Pankaj Garg96d0ccd2015-07-30 16:49:47 -0700246 mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
John Reck1cf4b792011-07-26 10:22:22 -0700247 mFavicon = favicon;
John Reck30c714c2010-12-16 17:30:34 -0800248 }
John Reck1cf4b792011-07-26 10:22:22 -0700249
Grace Kloba22ac16e2009-10-07 18:00:23 -0700250 }
251
John Reck30c714c2010-12-16 17:30:34 -0800252 // The current/loading page's state
John Reckd8c74522011-06-14 08:45:00 -0700253 protected PageState mCurrentState;
John Reck30c714c2010-12-16 17:30:34 -0800254
Grace Kloba22ac16e2009-10-07 18:00:23 -0700255 // Used for saving and restoring each Tab
Michael Kolbc831b632011-05-11 09:30:34 -0700256 static final String ID = "ID";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700257 static final String CURRURL = "currentUrl";
258 static final String CURRTITLE = "currentTitle";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700259 static final String PARENTTAB = "parentTab";
260 static final String APPID = "appid";
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700261 static final String INCOGNITO = "privateBrowsingEnabled";
John Reckb0a86db2011-05-24 14:05:58 -0700262 static final String USERAGENT = "useragent";
Michael Kolbe28b3472011-08-04 16:54:31 -0700263 static final String CLOSEFLAG = "closeOnBack";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700264
Pankaj Garg18186a92015-03-31 14:59:33 -0700265 public void setNetworkAvailable(boolean networkUp) {
266 if (networkUp && mReceivedError && (mMainView != null)) {
267 mMainView.reload();
268 }
269 }
270
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700271 public boolean isFirstVisualPixelPainted() {
272 return mFirstVisualPixelPainted;
273 }
274
275 public int getCaptureIndex(int navIndex) {
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700276 int orientation = mWebViewController.getActivity().
277 getResources().getConfiguration().orientation;
278
279 int orientationBit = (orientation == Configuration.ORIENTATION_LANDSCAPE) ? 0 : 1;
280
Vivek Sekhard0f60402015-06-05 14:07:11 -0700281 int index = orientationBit << 31 | (((int)mId & 0x7f) << 24) | (navIndex & 0xffffff);
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700282 return index;
283 }
284
285 public int getTabIdxFromCaptureIdx(int index) {
286 return (index & 0x7f000000) >> 24;
287 }
288
289 public int getOrientationFromCaptureIdx(int index) {
290 return ((index & 0x80000000) == 0) ? Configuration.ORIENTATION_LANDSCAPE :
291 Configuration.ORIENTATION_PORTRAIT;
292
293 }
294
295 public int getNavIdxFromCaptureIdx(int index) {
296 return (index & 0xffffff);
297 }
298
Pankaj Garg96d0ccd2015-07-30 16:49:47 -0700299 public static SecurityState getWebViewSecurityState(WebView view) {
300 switch (view.getSecurityLevel()) {
301 case WebView.SecurityLevel.EV_SECURE:
302 case WebView.SecurityLevel.SECURE:
303 return SecurityState.SECURITY_STATE_SECURE;
304 case WebView.SecurityLevel.SECURITY_ERROR:
305 return SecurityState.SECURITY_STATE_BAD_CERTIFICATE;
306 case WebView.SecurityLevel.SECURITY_POLICY_WARNING:
307 case WebView.SecurityLevel.SECURITY_WARNING:
308 return SecurityState.SECURITY_STATE_MIXED;
309 }
310 return SecurityState.SECURITY_STATE_NOT_SECURE;
311 }
312
Grace Kloba22ac16e2009-10-07 18:00:23 -0700313 // -------------------------------------------------------------------------
314 // WebViewClient implementation for the main WebView
315 // -------------------------------------------------------------------------
316
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800317 private final WebViewClient mWebViewClient = new WebViewClient() {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500318 private Message mDontResend;
319 private Message mResend;
Michael Kolb47bd1e42011-09-01 15:25:00 -0700320
321 private boolean providersDiffer(String url, String otherUrl) {
322 Uri uri1 = Uri.parse(url);
323 Uri uri2 = Uri.parse(otherUrl);
324 return !uri1.getEncodedAuthority().equals(uri2.getEncodedAuthority());
325 }
326
Grace Kloba22ac16e2009-10-07 18:00:23 -0700327 @Override
328 public void onPageStarted(WebView view, String url, Bitmap favicon) {
Ze G Riande2a675c22015-06-03 11:15:24 -0700329 setIsDistillable(false);
Michael Kolb8233fac2010-10-26 16:08:53 -0700330 mInPageLoad = true;
Tarun Nainani8eb00912014-07-17 12:28:32 -0700331 mPageFinished = false;
Pankaj Garg79878492015-04-01 14:48:21 -0700332 mFirstVisualPixelPainted = false;
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700333 mFirstPixelObservable.set(false);
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800334 mReceivedError = false;
Michael Kolb72864272012-05-03 15:42:15 -0700335 mUpdateThumbnail = true;
Michael Kolba53c9892011-10-05 13:31:40 -0700336 mPageLoadProgress = INITIAL_PROGRESS;
Michael Kolb14612442011-06-24 13:06:29 -0700337 mCurrentState = new PageState(mContext,
John Reck30c714c2010-12-16 17:30:34 -0800338 view.isPrivateBrowsingEnabled(), url, favicon);
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000339 mLoadStartTime = SystemClock.uptimeMillis();
Pankaj Garg62bc7912015-04-14 16:08:59 -0700340 // Need re-enable FullScreenMode on Page navigation if needed
341 if (BrowserSettings.getInstance().useFullscreen()){
342 Controller controller = (Controller) mWebViewController;
343 BaseUi ui = (BaseUi) controller.getUi();
344 ui.forceDisableFullscreenMode(false);
345 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700346 // If we start a touch icon load and then load a new page, we don't
347 // want to cancel the current touch icon loader. But, we do want to
348 // create a new one when the touch icon url is known.
349 if (mTouchIconLoader != null) {
350 mTouchIconLoader.mTab = null;
351 mTouchIconLoader = null;
352 }
353
Grace Kloba22ac16e2009-10-07 18:00:23 -0700354 // finally update the UI in the activity if it is in the foreground
John Reck324d4402011-01-11 16:56:42 -0800355 mWebViewController.onPageStarted(Tab.this, view, favicon);
Leon Scroggins4cd97792010-12-03 15:31:56 -0500356
John Recke969cc52010-12-21 17:24:43 -0800357 updateBookmarkedStatus();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700358 }
359
360 @Override
361 public void onPageFinished(WebView view, String url) {
John Reck38b39652012-06-05 09:22:59 -0700362 mDisableOverrideUrlLoading = false;
John Reck5b691842010-11-29 11:21:13 -0800363 if (!isPrivateBrowsingEnabled()) {
364 LogTag.logPageFinishedLoading(
365 url, SystemClock.uptimeMillis() - mLoadStartTime);
366 }
John Reck1cf4b792011-07-26 10:22:22 -0700367 syncCurrentState(view, url);
John Reck324d4402011-01-11 16:56:42 -0800368 mWebViewController.onPageFinished(Tab.this);
Pankaj Garg96d0ccd2015-07-30 16:49:47 -0700369 setSecurityState(getWebViewSecurityState(view));
Grace Kloba22ac16e2009-10-07 18:00:23 -0700370 }
371
Pankaj Garg79878492015-04-01 14:48:21 -0700372 @Override
373 public void onFirstVisualPixel(WebView view) {
374 mFirstVisualPixelPainted = true;
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700375 mFirstPixelObservable.set(true);
Pankaj Garg79878492015-04-01 14:48:21 -0700376 }
377
Grace Kloba22ac16e2009-10-07 18:00:23 -0700378 // return true if want to hijack the url to let another app to handle it
379 @Override
380 public boolean shouldOverrideUrlLoading(WebView view, String url) {
John Reck38b39652012-06-05 09:22:59 -0700381 if (!mDisableOverrideUrlLoading && mInForeground) {
Michael Kolb18eb3772010-12-10 14:29:51 -0800382 return mWebViewController.shouldOverrideUrlLoading(Tab.this,
383 view, url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700384 } else {
385 return false;
386 }
387 }
388
Vivek Sekharb991edb2014-12-17 18:18:07 -0800389 @Override
390 public boolean shouldDownloadFavicon(WebView view, String url) {
391 return true;
392 }
393
Grace Kloba22ac16e2009-10-07 18:00:23 -0700394 /**
Steve Block2466eff2011-10-03 15:33:09 +0100395 * Updates the security state. This method is called when we discover
396 * another resource to be loaded for this page (for example,
397 * javascript). While we update the security state, we do not update
398 * the lock icon until we are done loading, as it is slightly more
399 * secure this way.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700400 */
401 @Override
402 public void onLoadResource(WebView view, String url) {
403 if (url != null && url.length() > 0) {
404 // It is only if the page claims to be secure that we may have
Steve Block2466eff2011-10-03 15:33:09 +0100405 // to update the security state:
406 if (mCurrentState.mSecurityState == SecurityState.SECURITY_STATE_SECURE) {
407 // If NOT a 'safe' url, change the state to mixed content!
Grace Kloba22ac16e2009-10-07 18:00:23 -0700408 if (!(URLUtil.isHttpsUrl(url) || URLUtil.isDataUrl(url)
409 || URLUtil.isAboutUrl(url))) {
Steve Block2466eff2011-10-03 15:33:09 +0100410 mCurrentState.mSecurityState = SecurityState.SECURITY_STATE_MIXED;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700411 }
412 }
413 }
414 }
415
416 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700417 * Show a dialog informing the user of the network error reported by
418 * WebCore if it is in the foreground.
419 */
420 @Override
421 public void onReceivedError(WebView view, int errorCode,
422 String description, String failingUrl) {
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800423 // Used for the syncCurrentState to use
424 // the failing url instead of using webview url
425 mReceivedError = true;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700426 }
427
428 /**
429 * Check with the user if it is ok to resend POST data as the page they
430 * are trying to navigate to is the result of a POST.
431 */
432 @Override
433 public void onFormResubmission(WebView view, final Message dontResend,
434 final Message resend) {
435 if (!mInForeground) {
436 dontResend.sendToTarget();
437 return;
438 }
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500439 if (mDontResend != null) {
440 Log.w(LOGTAG, "onFormResubmission should not be called again "
441 + "while dialog is still up");
442 dontResend.sendToTarget();
443 return;
444 }
445 mDontResend = dontResend;
446 mResend = resend;
Michael Kolb14612442011-06-24 13:06:29 -0700447 new AlertDialog.Builder(mContext).setTitle(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700448 R.string.browserFrameFormResubmitLabel).setMessage(
449 R.string.browserFrameFormResubmitMessage)
450 .setPositiveButton(R.string.ok,
451 new DialogInterface.OnClickListener() {
452 public void onClick(DialogInterface dialog,
453 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500454 if (mResend != null) {
455 mResend.sendToTarget();
456 mResend = null;
457 mDontResend = null;
458 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700459 }
460 }).setNegativeButton(R.string.cancel,
461 new DialogInterface.OnClickListener() {
462 public void onClick(DialogInterface dialog,
463 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500464 if (mDontResend != null) {
465 mDontResend.sendToTarget();
466 mResend = null;
467 mDontResend = null;
468 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700469 }
470 }).setOnCancelListener(new OnCancelListener() {
471 public void onCancel(DialogInterface dialog) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500472 if (mDontResend != null) {
473 mDontResend.sendToTarget();
474 mResend = null;
475 mDontResend = null;
476 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700477 }
478 }).show();
479 }
480
481 /**
482 * Insert the url into the visited history database.
483 * @param url The url to be inserted.
484 * @param isReload True if this url is being reloaded.
485 * FIXME: Not sure what to do when reloading the page.
486 */
487 @Override
488 public void doUpdateVisitedHistory(WebView view, String url,
489 boolean isReload) {
John Reck324d4402011-01-11 16:56:42 -0800490 mWebViewController.doUpdateVisitedHistory(Tab.this, isReload);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700491 }
492
493 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700494 * Handles an HTTP authentication request.
495 *
496 * @param handler The authentication handler
497 * @param host The host
498 * @param realm The realm
499 */
500 @Override
501 public void onReceivedHttpAuthRequest(WebView view,
502 final HttpAuthHandler handler, final String host,
503 final String realm) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700504 mWebViewController.onReceivedHttpAuthRequest(Tab.this, view, handler, host, realm);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700505 }
506
507 @Override
John Reck438bf462011-01-12 18:11:46 -0800508 public WebResourceResponse shouldInterceptRequest(WebView view,
509 String url) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800510 //intercept if opening a new incognito tab - show the incognito welcome page
Axesh R. Ajmerad04cad12015-08-12 12:02:44 -0700511
512 // show only incognito content and webview has private
513 // and cannot go back(only supported if explicit from UI )
514 if (view.isPrivateBrowsingEnabled() &&
515 !view.canGoBack() &&
516 url.startsWith(Controller.INCOGNITO_URI)) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800517 Resources resourceHandle = mContext.getResources();
518 InputStream inStream = resourceHandle.openRawResource(
Bijan Amirzada41242f22014-03-21 12:12:18 -0700519 com.android.browser.R.raw.incognito_mode_start_page);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800520 return new WebResourceResponse("text/html", "utf8", inStream);
521 }
kaiyiz6e5b3e02013-08-19 20:02:01 +0800522 WebResourceResponse res;
523 if (MyNavigationUtil.MY_NAVIGATION.equals(url)) {
524 res = MyNavigationProvider.shouldInterceptRequest(mContext, url);
525 } else {
526 res = HomeProvider.shouldInterceptRequest(mContext, url);
527 }
John Reck438bf462011-01-12 18:11:46 -0800528 return res;
529 }
530
531 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700532 public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
533 if (!mInForeground) {
534 return false;
535 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700536 return mWebViewController.shouldOverrideKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700537 }
538
539 @Override
540 public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700541 if (!mInForeground) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700542 return;
543 }
John Reck997b1b72012-04-19 18:08:25 -0700544 if (!mWebViewController.onUnhandledKeyEvent(event)) {
545 super.onUnhandledKeyEvent(view, event);
546 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700547 }
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700548
549 @Override
550 public void beforeNavigation(WebView view, String url) {
Pankaj Garg21dad562015-07-02 17:17:24 -0700551 mTouchIconUrl = null;
Site Mao61b68212015-07-16 10:56:31 -0700552 if (BaseUi.isUiLowPowerMode()) {
Pankaj Garg18aa0a12015-06-22 11:06:12 -0700553 return;
554 }
555
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700556 if (isPrivateBrowsingEnabled()) {
557 return;
558 }
559
560 if (!mFirstVisualPixelPainted) {
561 return;
562 }
563
Pankaj Garg8ee61462015-07-29 18:17:24 -0700564 if (view.getUrl().equals(url)) {
565 return;
566 }
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -0700567
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700568 final int idx = view.copyBackForwardList().getCurrentIndex();
569 boolean bitmapExists = view.hasSnapshot(idx);
570
571 int progress = 100;
572 Controller controller = (Controller)mWebViewController;
573 UI ui = controller.getUi();
574 if (ui instanceof BaseUi) {
575 BaseUi baseUi = (BaseUi) ui;
576 TitleBar titleBar = baseUi.getTitleBar();
577 progress = titleBar.getProgressView().getProgressPercent();
578 }
579
580 if (bitmapExists && progress < 85) {
581 return;
582 }
583
584 int index = getCaptureIndex(view.getLastCommittedHistoryIndex());
Sagar Dhawan49f85cf2015-07-10 16:54:20 -0700585 view.captureSnapshot(index, null);
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700586 }
587
588 @Override
589 public void onHistoryItemCommit(WebView view, int index) {
Site Mao61b68212015-07-16 10:56:31 -0700590 if (BaseUi.isUiLowPowerMode()) {
Pankaj Garg18aa0a12015-06-22 11:06:12 -0700591 return;
592 }
593
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -0700594 // prevent snapshot tab from commiting any history
595 if (isSnapshot()) {
596 return;
597 }
598
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700599 mTabHistoryUpdateObservable.set(index);
Vivek Sekhar53ef8932015-06-18 16:51:43 -0700600 final int maxIdx = view.copyBackForwardList().getSize();
601 final WebView wv = view;
602 view.getSnapshotIds(new ValueCallback <List<Integer>>() {
603 @Override
604 public void onReceiveValue(List<Integer> ids) {
605 int currentTabIdx = mWebViewController.getTabControl().getCurrentPosition();
606 for (Integer id : ids) {
607 if (getTabIdxFromCaptureIdx(id) == currentTabIdx &&
608 getNavIdxFromCaptureIdx(id) >= maxIdx) {
609 wv.deleteSnapshot(id);
610 }
611 }
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700612 }
Vivek Sekhar53ef8932015-06-18 16:51:43 -0700613 });
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700614 }
Pankaj Garg62bc7912015-04-14 16:08:59 -0700615
616 @Override
617 public void onKeyboardStateChange(boolean popup) {
Vivek Sekhard4de6162015-07-21 15:01:45 -0700618 boolean keyboardWasShowing = isKeyboardShowing();
Sagar Dhawan33551ff2015-07-08 17:24:44 -0700619 mIsKeyboardUp = popup;
Vivek Sekhard4de6162015-07-21 15:01:45 -0700620 Controller controller = (Controller)mWebViewController;
621 BaseUi ui = (BaseUi) controller.getUi();
622 // lock the title bar
623 if (popup)
624 ui.getTitleBar().showTopControls(true);
625 if (keyboardWasShowing && popup)
626 ui.getTitleBar().enableTopControls(true);
Pankaj Garg62bc7912015-04-14 16:08:59 -0700627 if (BrowserSettings.getInstance().useFullscreen()) {
Pankaj Garg62bc7912015-04-14 16:08:59 -0700628 ui.forceDisableFullscreenMode(popup);
629 }
630 }
Vivek Sekharae3b1792015-08-03 12:26:41 -0700631
632 @Override
633 public void onAttachInterstitialPage(WebView mWebView) {
634 Controller controller = (Controller)mWebViewController;
635 BaseUi ui = (BaseUi) controller.getUi();
636 ui.getTitleBar().showTopControls(false);
637 }
638
639 @Override
640 public void onDetachInterstitialPage(WebView mWebView) {
641 Controller controller = (Controller)mWebViewController;
642 BaseUi ui = (BaseUi) controller.getUi();
643 ui.getTitleBar().enableTopControls(true);
644 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700645 };
646
John Reck1cf4b792011-07-26 10:22:22 -0700647 private void syncCurrentState(WebView view, String url) {
648 // Sync state (in case of stop/timeout)
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800649
Axesh R. Ajmerad04cad12015-08-12 12:02:44 -0700650
651
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800652 if (mReceivedError) {
653 mCurrentState.mUrl = url;
654 mCurrentState.mOriginalUrl = url;
Axesh R. Ajmerad04cad12015-08-12 12:02:44 -0700655 } else if (view.isPrivateBrowsingEnabled() &&
656 !TextUtils.isEmpty(url) &&
657 url.contains(Controller.INCOGNITO_URI)) {
658 mCurrentState.mUrl = mCurrentState.mOriginalUrl = "";
659 }
660 else {
661 mCurrentState.mUrl = view.getUrl();
662 mCurrentState.mOriginalUrl = view.getOriginalUrl();
663 mCurrentState.mFavicon = view.getFavicon();
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800664 }
665
John Reck1cf4b792011-07-26 10:22:22 -0700666 if (mCurrentState.mUrl == null) {
John Reck8bcafc12011-08-29 16:43:02 -0700667 mCurrentState.mUrl = "";
John Reck1cf4b792011-07-26 10:22:22 -0700668 }
John Reck1cf4b792011-07-26 10:22:22 -0700669 mCurrentState.mTitle = view.getTitle();
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800670
671
John Reck1cf4b792011-07-26 10:22:22 -0700672 if (!URLUtil.isHttpsUrl(mCurrentState.mUrl)) {
673 // In case we stop when loading an HTTPS page from an HTTP page
674 // but before a provisional load occurred
Steve Block2466eff2011-10-03 15:33:09 +0100675 mCurrentState.mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
John Reck1cf4b792011-07-26 10:22:22 -0700676 }
John Reck502a3532011-08-16 14:21:46 -0700677 mCurrentState.mIncognito = view.isPrivateBrowsingEnabled();
John Reck1cf4b792011-07-26 10:22:22 -0700678 }
679
Pankaj Garg21dad562015-07-02 17:17:24 -0700680 public String getTouchIconUrl() {
681 return mTouchIconUrl;
682 }
Tarun Nainani8eb00912014-07-17 12:28:32 -0700683
Sagar Dhawan33551ff2015-07-08 17:24:44 -0700684 public boolean isKeyboardShowing() {
685 Controller controller = (Controller)mWebViewController;
686 return (mIsKeyboardUp || controller.getUi().isEditingUrl());
687 }
688
Tarun Nainani8eb00912014-07-17 12:28:32 -0700689 public boolean isTabFullScreen() {
690 return mFullScreen;
691 }
692
Vivek Sekharf96064b2014-07-28 16:32:34 -0700693 protected void setTabFullscreen(boolean fullScreen) {
Tarun Nainani8eb00912014-07-17 12:28:32 -0700694 Controller controller = (Controller)mWebViewController;
Sudheer Koganti24766882014-10-02 10:58:09 -0700695 controller.getUi().showFullscreen(fullScreen);
Tarun Nainani8eb00912014-07-17 12:28:32 -0700696 mFullScreen = fullScreen;
Vivek Sekharf96064b2014-07-28 16:32:34 -0700697 }
698
Sudheer Koganti24766882014-10-02 10:58:09 -0700699 public boolean exitFullscreen() {
700 if (mFullScreen) {
701 Controller controller = (Controller)mWebViewController;
702 controller.getUi().showFullscreen(false);
703 if (getWebView() != null)
704 getWebView().exitFullscreen();
705 mFullScreen = false;
706 return true;
707 }
708 return false;
709 }
710
711
712
713
Grace Kloba22ac16e2009-10-07 18:00:23 -0700714 // -------------------------------------------------------------------------
715 // WebChromeClient implementation for the main WebView
716 // -------------------------------------------------------------------------
717
718 private final WebChromeClient mWebChromeClient = new WebChromeClient() {
719 // Helper method to create a new tab or sub window.
720 private void createWindow(final boolean dialog, final Message msg) {
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700721 this.createWindow(dialog, msg, null, false);
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700722 }
723
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700724 private void createWindow(final boolean dialog, final Message msg, final String url,
725 final boolean opener_suppressed) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700726 WebView.WebViewTransport transport =
727 (WebView.WebViewTransport) msg.obj;
728 if (dialog) {
729 createSubWindow();
Michael Kolb8233fac2010-10-26 16:08:53 -0700730 mWebViewController.attachSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700731 transport.setWebView(mSubView);
732 } else {
Pankaj Garg21dad562015-07-02 17:17:24 -0700733 capture();
734
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700735 final Tab newTab = mWebViewController.openTab(url,
John Reck5949c662011-05-27 09:52:29 -0700736 Tab.this, true, true);
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700737 // This is special case for rendering links on a webpage in
738 // a new tab. If opener is suppressed, the WebContents created
739 // by the content layer are not fully initialized. This check
740 // will prevent content layer from overriding WebContents
741 // created by new tab with the uninitialized instance.
742 if (!opener_suppressed) {
743 transport.setWebView(newTab.getWebView());
744 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700745 }
746 msg.sendToTarget();
747 }
748
749 @Override
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700750 public void toggleFullscreenModeForTab(boolean enterFullscreen) {
751 if (mWebViewController instanceof Controller) {
Vivek Sekharf96064b2014-07-28 16:32:34 -0700752 setTabFullscreen(enterFullscreen);
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700753 }
754 }
755
756 @Override
Tarun Nainani8eb00912014-07-17 12:28:32 -0700757 public void onOffsetsForFullscreenChanged(float topControlsOffsetYPix,
758 float contentOffsetYPix,
759 float overdrawBottomHeightPix) {
760 if (mWebViewController instanceof Controller) {
Vivek Sekhar8ee3abb2014-07-14 12:32:05 -0700761 Controller controller = (Controller)mWebViewController;
Tarun Nainani8eb00912014-07-17 12:28:32 -0700762 controller.getUi().translateTitleBar(topControlsOffsetYPix);
Vivek Sekhar11b40062015-06-24 11:49:04 -0700763 // Resize the viewport if top controls is not visible
Vivek Sekhar2ee19a32015-07-02 17:03:57 -0700764 if (mMainView != null &&
765 (topControlsOffsetYPix == 0.0f || contentOffsetYPix == 0.0f))
Vivek Sekhar11b40062015-06-24 11:49:04 -0700766 ((BrowserWebView)mMainView).enableTopControls(
767 (topControlsOffsetYPix == 0.0f) ? true : false);
Vivek Sekhar8ee3abb2014-07-14 12:32:05 -0700768 }
769 }
770
771 @Override
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700772 public boolean isTabFullScreen() {
Vivek Sekharf96064b2014-07-28 16:32:34 -0700773 return mFullScreen;
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700774 }
775
776 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700777 public boolean onCreateWindow(WebView view, final boolean dialog,
778 final boolean userGesture, final Message resultMsg) {
779 // only allow new window or sub window for the foreground case
780 if (!mInForeground) {
781 return false;
782 }
783 // Short-circuit if we can't create any more tabs or sub windows.
784 if (dialog && mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -0700785 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700786 .setTitle(R.string.too_many_subwindows_dialog_title)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200787 .setIconAttribute(android.R.attr.alertDialogIcon)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700788 .setMessage(R.string.too_many_subwindows_dialog_message)
789 .setPositiveButton(R.string.ok, null)
790 .show();
791 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700792 } else if (!mWebViewController.getTabControl().canCreateNewTab()) {
Michael Kolb14612442011-06-24 13:06:29 -0700793 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700794 .setTitle(R.string.too_many_windows_dialog_title)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200795 .setIconAttribute(android.R.attr.alertDialogIcon)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700796 .setMessage(R.string.too_many_windows_dialog_message)
797 .setPositiveButton(R.string.ok, null)
798 .show();
799 return false;
800 }
801
802 // Short-circuit if this was a user gesture.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800803 if (userGesture || !mSettings.blockPopupWindows()) {
Tarun Nainani4f5137d2015-04-16 17:26:18 -0700804 WebView.WebViewTransport transport =
805 (WebView.WebViewTransport) resultMsg.obj;
806 CreateWindowParams windowParams = transport.getCreateWindowParams();
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700807 if (windowParams.mOpenerSuppressed) {
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700808 createWindow(dialog, resultMsg, windowParams.mURL, true);
809 // This is special case for rendering links on a webpage in
810 // a new tab. If opener is suppressed, the WebContents created
811 // by the content layer are not fully initialized. Returning false
812 // will prevent content layer from overriding WebContents
813 // created by new tab with the uninitialized instance.
814 return false;
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700815 }
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700816
817 createWindow(dialog, resultMsg);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700818 return true;
819 }
820
Tarun Nainani4f5137d2015-04-16 17:26:18 -0700821 createWindow(dialog, resultMsg);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700822 return true;
823 }
824
825 @Override
Patrick Scotteb5061b2009-11-18 15:00:30 -0500826 public void onRequestFocus(WebView view) {
827 if (!mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700828 mWebViewController.switchToTab(Tab.this);
Patrick Scotteb5061b2009-11-18 15:00:30 -0500829 }
830 }
831
832 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700833 public void onCloseWindow(WebView window) {
Michael Kolbc831b632011-05-11 09:30:34 -0700834 if (mParent != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700835 // JavaScript can only close popup window.
836 if (mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700837 mWebViewController.switchToTab(mParent);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700838 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700839 mWebViewController.closeTab(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700840 }
841 }
842
843 @Override
844 public void onProgressChanged(WebView view, int newProgress) {
John Reck30c714c2010-12-16 17:30:34 -0800845 mPageLoadProgress = newProgress;
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800846 if (newProgress == 100) {
847 mInPageLoad = false;
848 }
John Reck30c714c2010-12-16 17:30:34 -0800849 mWebViewController.onProgressChanged(Tab.this);
Michael Kolb72864272012-05-03 15:42:15 -0700850 if (mUpdateThumbnail && newProgress == 100) {
851 mUpdateThumbnail = false;
852 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700853 }
854
855 @Override
Leon Scroggins21d9b902010-03-11 09:33:11 -0500856 public void onReceivedTitle(WebView view, final String title) {
John Reck30c714c2010-12-16 17:30:34 -0800857 mCurrentState.mTitle = title;
Michael Kolb8233fac2010-10-26 16:08:53 -0700858 mWebViewController.onReceivedTitle(Tab.this, title);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700859 }
860
861 @Override
862 public void onReceivedIcon(WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -0800863 mCurrentState.mFavicon = icon;
Michael Kolb8233fac2010-10-26 16:08:53 -0700864 mWebViewController.onFavicon(Tab.this, view, icon);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700865 }
866
867 @Override
868 public void onReceivedTouchIconUrl(WebView view, String url,
869 boolean precomposed) {
Michael Kolb14612442011-06-24 13:06:29 -0700870 final ContentResolver cr = mContext.getContentResolver();
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400871 // Let precomposed icons take precedence over non-composed
872 // icons.
873 if (precomposed && mTouchIconLoader != null) {
874 mTouchIconLoader.cancel(false);
875 mTouchIconLoader = null;
876 }
877 // Have only one async task at a time.
878 if (mTouchIconLoader == null) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700879 mTouchIconLoader = new DownloadTouchIcon(Tab.this,
Michael Kolb14612442011-06-24 13:06:29 -0700880 mContext, cr, view);
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400881 mTouchIconLoader.execute(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700882 }
Pankaj Garg21dad562015-07-02 17:17:24 -0700883 mTouchIconUrl = url;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700884 }
885
886 @Override
887 public void onShowCustomView(View view,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800888 CustomViewCallback callback) {
Michael Kolb14612442011-06-24 13:06:29 -0700889 Activity activity = mWebViewController.getActivity();
890 if (activity != null) {
891 onShowCustomView(view, activity.getRequestedOrientation(), callback);
892 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400893 }
894
895 @Override
896 public void onShowCustomView(View view, int requestedOrientation,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800897 CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700898 if (mInForeground) mWebViewController.showCustomView(Tab.this, view,
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400899 requestedOrientation, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700900 }
901
902 @Override
903 public void onHideCustomView() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700904 if (mInForeground) mWebViewController.hideCustomView();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700905 }
906
907 /**
908 * The origin has exceeded its database quota.
909 * @param url the URL that exceeded the quota
910 * @param databaseIdentifier the identifier of the database on which the
911 * transaction that caused the quota overflow was run
912 * @param currentQuota the current quota for the origin.
913 * @param estimatedSize the estimated size of the database.
914 * @param totalUsedQuota is the sum of all origins' quota.
915 * @param quotaUpdater The callback to run when a decision to allow or
916 * deny quota has been made. Don't forget to call this!
917 */
918 @Override
919 public void onExceededDatabaseQuota(String url,
920 String databaseIdentifier, long currentQuota, long estimatedSize,
921 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -0700922 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -0700923 .onExceededDatabaseQuota(url, databaseIdentifier,
924 currentQuota, estimatedSize, totalUsedQuota,
925 quotaUpdater);
926 }
927
928 /**
929 * The Application Cache has exceeded its max size.
930 * @param spaceNeeded is the amount of disk space that would be needed
931 * in order for the last appcache operation to succeed.
932 * @param totalUsedQuota is the sum of all origins' quota.
933 * @param quotaUpdater A callback to inform the WebCore thread that a
934 * new app cache size is available. This callback must always
935 * be executed at some point to ensure that the sleeping
936 * WebCore thread is woken up.
937 */
938 @Override
939 public void onReachedMaxAppCacheSize(long spaceNeeded,
940 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -0700941 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -0700942 .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota,
943 quotaUpdater);
944 }
945
Ben Murdoch65acc352009-11-19 18:16:04 +0000946 /* Adds a JavaScript error message to the system log and if the JS
947 * console is enabled in the about:debug options, to that console
948 * also.
Ben Murdochc42addf2010-01-28 15:19:59 +0000949 * @param consoleMessage the message object.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700950 */
951 @Override
Ben Murdochc42addf2010-01-28 15:19:59 +0000952 public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Jeff Hamilton47654f42010-09-07 09:57:51 -0500953 // Don't log console messages in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -0700954 if (isPrivateBrowsingEnabled()) return true;
Jeff Hamilton47654f42010-09-07 09:57:51 -0500955
Ben Murdochc42addf2010-01-28 15:19:59 +0000956 String message = "Console: " + consoleMessage.message() + " "
957 + consoleMessage.sourceId() + ":"
958 + consoleMessage.lineNumber();
959
960 switch (consoleMessage.messageLevel()) {
961 case TIP:
962 Log.v(CONSOLE_LOGTAG, message);
963 break;
964 case LOG:
965 Log.i(CONSOLE_LOGTAG, message);
966 break;
967 case WARNING:
968 Log.w(CONSOLE_LOGTAG, message);
969 break;
970 case ERROR:
971 Log.e(CONSOLE_LOGTAG, message);
972 break;
973 case DEBUG:
974 Log.d(CONSOLE_LOGTAG, message);
975 break;
976 }
977
978 return true;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700979 }
980
981 /**
982 * Ask the browser for an icon to represent a <video> element.
983 * This icon will be used if the Web page did not specify a poster attribute.
984 * @return Bitmap The icon or null if no such icon is available.
985 */
986 @Override
987 public Bitmap getDefaultVideoPoster() {
988 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700989 return mWebViewController.getDefaultVideoPoster();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700990 }
991 return null;
992 }
993
994 /**
995 * Ask the host application for a custom progress view to show while
996 * a <video> is loading.
997 * @return View The progress view.
998 */
999 @Override
1000 public View getVideoLoadingProgressView() {
1001 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001002 return mWebViewController.getVideoLoadingProgressView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001003 }
1004 return null;
1005 }
1006
1007 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00001008 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001009 if (mInForeground) {
Ben Murdoch8cad4132012-01-11 10:56:43 +00001010 mWebViewController.openFileChooser(uploadMsg, acceptType, capture);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001011 } else {
1012 uploadMsg.onReceiveValue(null);
1013 }
1014 }
1015
Vivek Sekharb54614f2014-05-01 19:03:37 -07001016 @Override
1017 public void showFileChooser(ValueCallback<String[]> uploadFilePaths, String acceptTypes,
1018 boolean capture) {
1019 if (mInForeground) {
1020 mWebViewController.showFileChooser(uploadFilePaths, acceptTypes, capture);
1021 } else {
1022 uploadFilePaths.onReceiveValue(null);
1023 }
1024 }
1025
Grace Kloba22ac16e2009-10-07 18:00:23 -07001026 /**
1027 * Deliver a list of already-visited URLs
1028 */
1029 @Override
1030 public void getVisitedHistory(final ValueCallback<String[]> callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001031 mWebViewController.getVisitedHistory(callback);
1032 }
Ben Murdoch8029a772010-11-16 11:58:21 +00001033
1034 @Override
1035 public void setupAutoFill(Message message) {
1036 // Prompt the user to set up their profile.
1037 final Message msg = message;
Michael Kolb14612442011-06-24 13:06:29 -07001038 AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
1039 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
Ben Murdoch1d676b62011-01-17 12:54:24 +00001040 Context.LAYOUT_INFLATER_SERVICE);
1041 final View layout = inflater.inflate(R.layout.setup_autofill_dialog, null);
1042
1043 builder.setView(layout)
1044 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1045 @Override
1046 public void onClick(DialogInterface dialog, int id) {
1047 CheckBox disableAutoFill = (CheckBox) layout.findViewById(
1048 R.id.setup_autofill_dialog_disable_autofill);
1049
1050 if (disableAutoFill.isChecked()) {
1051 // Disable autofill and show a toast with how to turn it on again.
John Reck35e9dd62011-04-25 09:01:54 -07001052 mSettings.setAutofillEnabled(false);
Michael Kolb14612442011-06-24 13:06:29 -07001053 Toast.makeText(mContext,
Ben Murdoch1d676b62011-01-17 12:54:24 +00001054 R.string.autofill_setup_dialog_negative_toast,
1055 Toast.LENGTH_LONG).show();
1056 } else {
1057 // Take user to the AutoFill profile editor. When they return,
1058 // we will send the message that we pass here which will trigger
1059 // the form to get filled out with their new profile.
1060 mWebViewController.setupAutoFill(msg);
1061 }
1062 }
1063 })
1064 .setNegativeButton(R.string.cancel, null)
1065 .show();
Ben Murdoch8029a772010-11-16 11:58:21 +00001066 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001067 };
1068
1069 // -------------------------------------------------------------------------
1070 // WebViewClient implementation for the sub window
1071 // -------------------------------------------------------------------------
1072
1073 // Subclass of WebViewClient used in subwindows to notify the main
1074 // WebViewClient of certain WebView activities.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001075 private static class SubWindowClient extends WebViewClient {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001076 // The main WebViewClient.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001077 private final WebViewClient mClient;
Michael Kolb8233fac2010-10-26 16:08:53 -07001078 private final WebViewController mController;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001079
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001080 SubWindowClient(WebViewClient client, WebViewController controller) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001081 mClient = client;
Michael Kolb8233fac2010-10-26 16:08:53 -07001082 mController = controller;
Leon Scroggins III211ba542010-04-19 13:21:13 -04001083 }
1084 @Override
1085 public void onPageStarted(WebView view, String url, Bitmap favicon) {
1086 // Unlike the others, do not call mClient's version, which would
1087 // change the progress bar. However, we do want to remove the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001088 // find or select dialog.
Michael Kolb8233fac2010-10-26 16:08:53 -07001089 mController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001090 }
1091 @Override
1092 public void doUpdateVisitedHistory(WebView view, String url,
1093 boolean isReload) {
1094 mClient.doUpdateVisitedHistory(view, url, isReload);
1095 }
1096 @Override
1097 public boolean shouldOverrideUrlLoading(WebView view, String url) {
1098 return mClient.shouldOverrideUrlLoading(view, url);
1099 }
1100 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -07001101 public void onReceivedHttpAuthRequest(WebView view,
1102 HttpAuthHandler handler, String host, String realm) {
1103 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
1104 }
1105 @Override
1106 public void onFormResubmission(WebView view, Message dontResend,
1107 Message resend) {
1108 mClient.onFormResubmission(view, dontResend, resend);
1109 }
1110 @Override
1111 public void onReceivedError(WebView view, int errorCode,
1112 String description, String failingUrl) {
1113 mClient.onReceivedError(view, errorCode, description, failingUrl);
1114 }
1115 @Override
1116 public boolean shouldOverrideKeyEvent(WebView view,
1117 android.view.KeyEvent event) {
1118 return mClient.shouldOverrideKeyEvent(view, event);
1119 }
1120 @Override
1121 public void onUnhandledKeyEvent(WebView view,
1122 android.view.KeyEvent event) {
1123 mClient.onUnhandledKeyEvent(view, event);
1124 }
1125 }
1126
1127 // -------------------------------------------------------------------------
1128 // WebChromeClient implementation for the sub window
1129 // -------------------------------------------------------------------------
1130
1131 private class SubWindowChromeClient extends WebChromeClient {
1132 // The main WebChromeClient.
1133 private final WebChromeClient mClient;
1134
1135 SubWindowChromeClient(WebChromeClient client) {
1136 mClient = client;
1137 }
1138 @Override
1139 public void onProgressChanged(WebView view, int newProgress) {
1140 mClient.onProgressChanged(view, newProgress);
1141 }
1142 @Override
1143 public boolean onCreateWindow(WebView view, boolean dialog,
1144 boolean userGesture, android.os.Message resultMsg) {
1145 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
1146 }
1147 @Override
1148 public void onCloseWindow(WebView window) {
1149 if (window != mSubView) {
1150 Log.e(LOGTAG, "Can't close the window");
1151 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001152 mWebViewController.dismissSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001153 }
1154 }
1155
1156 // -------------------------------------------------------------------------
1157
1158 // Construct a new tab
Michael Kolb7bcafde2011-05-09 13:55:59 -07001159 Tab(WebViewController wvcontroller, WebView w) {
John Reck1cf4b792011-07-26 10:22:22 -07001160 this(wvcontroller, w, null);
1161 }
1162
1163 Tab(WebViewController wvcontroller, Bundle state) {
1164 this(wvcontroller, null, state);
1165 }
1166
1167 Tab(WebViewController wvcontroller, WebView w, Bundle state) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001168 mWebViewController = wvcontroller;
Michael Kolb14612442011-06-24 13:06:29 -07001169 mContext = mWebViewController.getContext();
John Reck35e9dd62011-04-25 09:01:54 -07001170 mSettings = BrowserSettings.getInstance();
Michael Kolb14612442011-06-24 13:06:29 -07001171 mDataController = DataController.getInstance(mContext);
1172 mCurrentState = new PageState(mContext, w != null
John Reck847b5322011-04-14 17:02:18 -07001173 ? w.isPrivateBrowsingEnabled() : false);
Tarun Nainani8084c822014-06-25 13:38:06 -07001174 setTimeStamp();
Michael Kolb8233fac2010-10-26 16:08:53 -07001175 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001176 mInForeground = false;
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001177 mWebViewDestroyedByMemoryMonitor = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001178
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001179 mDownloadListener = new BrowserDownloadListener() {
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001180 public void onDownloadStart(String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001181 String contentDisposition, String mimetype, String referer,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001182 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001183 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001184 mimetype, referer, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001185 }
1186 };
1187
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001188 mCaptureWidth = mContext.getResources().getDimensionPixelSize(R.dimen.tab_thumbnail_width);
1189 mCaptureHeight =mContext.getResources().getDimensionPixelSize(R.dimen.tab_thumbnail_height);
1190
1191 initCaptureBitmap();
1192
John Reck1cf4b792011-07-26 10:22:22 -07001193 restoreState(state);
John Reck52be4782011-08-26 15:37:29 -07001194 if (getId() == -1) {
1195 mId = TabControl.getNextId();
1196 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001197 setWebView(w);
Michael Kolb9ef259a2011-07-12 15:33:08 -07001198 mHandler = new Handler() {
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001199 @Override
Michael Kolb9ef259a2011-07-12 15:33:08 -07001200 public void handleMessage(Message m) {
John Reck1cf4b792011-07-26 10:22:22 -07001201 switch (m.what) {
1202 case MSG_CAPTURE:
1203 capture();
1204 break;
1205 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001206 }
1207 };
Pankaj Garg1c13cab2015-05-12 11:52:17 -07001208
1209 mFirstPixelObservable = new Observable();
1210 mFirstPixelObservable.set(false);
1211 mTabHistoryUpdateObservable = new Observable();
John Reck1cf4b792011-07-26 10:22:22 -07001212 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001213
Pankaj Gargb4b4f012015-08-04 16:19:14 -07001214 public void initCaptureBitmap() {
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001215 mCapture = Bitmap.createBitmap(mCaptureWidth, mCaptureHeight, Bitmap.Config.RGB_565);
1216 mCapture.eraseColor(Color.WHITE);
Michael Kolb72864272012-05-03 15:42:15 -07001217 }
1218
Mathew Inwoode09305e2011-09-02 12:03:26 +01001219 /**
1220 * This is used to get a new ID when the tab has been preloaded, before it is displayed and
1221 * added to TabControl. Preloaded tabs can be created before restoreInstanceState, leading
1222 * to overlapping IDs between the preloaded and restored tabs.
1223 */
1224 public void refreshIdAfterPreload() {
1225 mId = TabControl.getNextId();
1226 }
1227
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001228 public void setController(WebViewController ctl) {
1229 mWebViewController = ctl;
1230
John Reck1cf4b792011-07-26 10:22:22 -07001231 if (mWebViewController.shouldCaptureThumbnails()) {
1232 synchronized (Tab.this) {
1233 if (mCapture == null) {
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001234 initCaptureBitmap();
1235 if (mInForeground && !mHandler.hasMessages(MSG_CAPTURE)) {
1236 mHandler.sendEmptyMessageDelayed(MSG_CAPTURE, CAPTURE_DELAY);
John Reck1cf4b792011-07-26 10:22:22 -07001237 }
1238 }
1239 }
1240 } else {
1241 synchronized (Tab.this) {
1242 mCapture = null;
1243 deleteThumbnail();
1244 }
1245 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001246 }
1247
Michael Kolbc831b632011-05-11 09:30:34 -07001248 public long getId() {
1249 return mId;
1250 }
1251
Michael Kolb91911a22012-01-17 11:21:25 -08001252 void setWebView(WebView w) {
1253 setWebView(w, true);
1254 }
1255
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001256 public boolean isNativeActive(){
1257 if (mMainView == null)
1258 return false;
1259 return true;
1260 }
1261
1262 public void setTimeStamp(){
1263 Date d = new Date();
1264 timestamp = (new Timestamp(d.getTime()));
1265 }
1266
1267 public Timestamp getTimestamp() {
1268 return timestamp;
1269 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001270 /**
1271 * Sets the WebView for this tab, correctly removing the old WebView from
1272 * the container view.
1273 */
Michael Kolb91911a22012-01-17 11:21:25 -08001274 void setWebView(WebView w, boolean restore) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001275 if (mMainView == w) {
1276 return;
1277 }
Michael Kolba713ec82010-11-29 17:27:06 -08001278
Michael Kolba713ec82010-11-29 17:27:06 -08001279 mWebViewController.onSetWebView(this, w);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001280
John Reck1cf4b792011-07-26 10:22:22 -07001281 if (mMainView != null) {
John Reck8ee633f2011-08-09 16:00:35 -07001282 mMainView.setPictureListener(null);
John Reck1cf4b792011-07-26 10:22:22 -07001283 if (w != null) {
1284 syncCurrentState(w, null);
1285 } else {
Panos Thomasa9a5a582014-03-18 19:20:08 -07001286 mCurrentState = new PageState(mContext, mMainView.isPrivateBrowsingEnabled());
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001287
1288 if (mWebViewDestroyedByMemoryMonitor) {
1289 /*
1290 * If tab was destroyed as a result of the MemoryMonitor
1291 * then we need to restore the state properties
1292 * from the old WebView (mMainView)
1293 */
1294 syncCurrentState(mMainView, null);
1295 mWebViewDestroyedByMemoryMonitor = false;
1296 }
John Reck1cf4b792011-07-26 10:22:22 -07001297 }
1298 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001299 // set the new one
1300 mMainView = w;
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001301
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001302 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001303 if (mMainView != null) {
1304 mMainView.setWebViewClient(mWebViewClient);
1305 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001306 // Attach DownloadManager so that downloads can start in an active
1307 // or a non-active window. This can happen when going to a site that
1308 // does a redirect after a period of time. The user could have
1309 // switched to another tab while waiting for the download to start.
1310 mMainView.setDownloadListener(mDownloadListener);
John Reck8ee633f2011-08-09 16:00:35 -07001311 TabControl tc = mWebViewController.getTabControl();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001312 if (tc != null /*&& tc.getOnThumbnailUpdatedListener() != null*/) {
John Reck8ee633f2011-08-09 16:00:35 -07001313 mMainView.setPictureListener(this);
1314 }
Michael Kolb91911a22012-01-17 11:21:25 -08001315 if (restore && (mSavedState != null)) {
John Reck8b9bb8b2012-03-08 13:19:40 -08001316 restoreUserAgent();
John Reck6c2e2f32011-08-22 13:41:23 -07001317 WebBackForwardList restoredState
1318 = mMainView.restoreState(mSavedState);
1319 if (restoredState == null || restoredState.getSize() == 0) {
1320 Log.w(LOGTAG, "Failed to restore WebView state!");
1321 loadUrl(mCurrentState.mOriginalUrl, null);
1322 }
John Reck1cf4b792011-07-26 10:22:22 -07001323 mSavedState = null;
1324 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001325 }
1326 }
1327
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001328 public void destroyThroughMemoryMonitor() {
1329 mWebViewDestroyedByMemoryMonitor = true;
1330 destroy();
1331 }
1332
Grace Kloba22ac16e2009-10-07 18:00:23 -07001333 /**
1334 * Destroy the tab's main WebView and subWindow if any
1335 */
1336 void destroy() {
Tarun Nainani2c1dd7c2014-07-05 16:40:12 -07001337
Matthew Huib7f2e9c2014-04-16 11:12:37 -04001338 if (mPostponeDestroy) {
1339 mShouldDestroy = true;
1340 return;
1341 }
1342 mShouldDestroy = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001343 if (mMainView != null) {
1344 dismissSubWindow();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001345 // save the WebView to call destroy() after detach it from the tab
Vivek Sekhar53ef8932015-06-18 16:51:43 -07001346 final WebView webView = mMainView;
Vivek Sekhar2ee19a32015-07-02 17:03:57 -07001347 setWebView(null);
Site Mao61b68212015-07-16 10:56:31 -07001348 if (!mWebViewDestroyedByMemoryMonitor && !BaseUi.isUiLowPowerMode()) {
Vivek Sekhar2ee19a32015-07-02 17:03:57 -07001349 // Tabs can be reused with new instance of WebView so delete the snapshots
Vivek Sekhar53ef8932015-06-18 16:51:43 -07001350 webView.getSnapshotIds(new ValueCallback<List<Integer>>() {
1351 @Override
1352 public void onReceiveValue(List<Integer> ids) {
1353 int currentTabIdx = mWebViewController.getTabControl().getCurrentPosition();
1354 for (Integer id : ids) {
1355 if (getTabIdxFromCaptureIdx(id) == currentTabIdx) {
1356 webView.deleteSnapshot(id);
1357 }
1358 }
Vivek Sekhar53ef8932015-06-18 16:51:43 -07001359 webView.destroy();
Pankaj Garg1c13cab2015-05-12 11:52:17 -07001360 }
Vivek Sekhar53ef8932015-06-18 16:51:43 -07001361 });
Vivek Sekhar2ee19a32015-07-02 17:03:57 -07001362 } else {
1363 webView.destroy();
Pankaj Garg1c13cab2015-05-12 11:52:17 -07001364 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001365 }
1366 }
1367
Matthew Huib7f2e9c2014-04-16 11:12:37 -04001368 private boolean mPostponeDestroy = false;
1369 private boolean mShouldDestroy = false;
1370
1371 public void postponeDestroy() {
1372 mPostponeDestroy = true;
1373 }
1374
1375 public void performPostponedDestroy() {
1376 mPostponeDestroy = false;
1377 if (mShouldDestroy) {
1378 destroy();
1379 }
1380 }
1381
Grace Kloba22ac16e2009-10-07 18:00:23 -07001382 /**
1383 * Remove the tab from the parent
1384 */
1385 void removeFromTree() {
1386 // detach the children
Michael Kolbc831b632011-05-11 09:30:34 -07001387 if (mChildren != null) {
1388 for(Tab t : mChildren) {
1389 t.setParent(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001390 }
1391 }
1392 // remove itself from the parent list
Michael Kolbc831b632011-05-11 09:30:34 -07001393 if (mParent != null) {
1394 mParent.mChildren.remove(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001395 }
Tarun Nainani8eb00912014-07-17 12:28:32 -07001396
1397 mCapture = null;
John Reck1cf4b792011-07-26 10:22:22 -07001398 deleteThumbnail();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001399 }
1400
1401 /**
1402 * Create a new subwindow unless a subwindow already exists.
1403 * @return True if a new subwindow was created. False if one already exists.
1404 */
1405 boolean createSubWindow() {
1406 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001407 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001408 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001409 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001410 mSubView.setWebChromeClient(new SubWindowChromeClient(
1411 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001412 // Set a different DownloadListener for the mSubView, since it will
1413 // just need to dismiss the mSubView, rather than close the Tab
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001414 mSubView.setDownloadListener(new BrowserDownloadListener() {
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001415 public void onDownloadStart(String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001416 String contentDisposition, String mimetype, String referer,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001417 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001418 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001419 contentDisposition, mimetype, referer, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001420 if (mSubView.copyBackForwardList().getSize() == 0) {
1421 // This subwindow was opened for the sole purpose of
1422 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001423 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001424 }
1425 }
1426 });
Michael Kolb14612442011-06-24 13:06:29 -07001427 mSubView.setOnCreateContextMenuListener(mWebViewController.getActivity());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001428 return true;
1429 }
1430 return false;
1431 }
1432
1433 /**
1434 * Dismiss the subWindow for the tab.
1435 */
1436 void dismissSubWindow() {
1437 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001438 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001439 mSubView.destroy();
1440 mSubView = null;
1441 mSubViewContainer = null;
1442 }
1443 }
1444
Grace Kloba22ac16e2009-10-07 18:00:23 -07001445
1446 /**
1447 * Set the parent tab of this tab.
1448 */
Michael Kolbc831b632011-05-11 09:30:34 -07001449 void setParent(Tab parent) {
John Reck52be4782011-08-26 15:37:29 -07001450 if (parent == this) {
1451 throw new IllegalStateException("Cannot set parent to self!");
1452 }
Michael Kolbc831b632011-05-11 09:30:34 -07001453 mParent = parent;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001454 // This tab may have been freed due to low memory. If that is the case,
Michael Kolbc831b632011-05-11 09:30:34 -07001455 // the parent tab id is already saved. If we are changing that id
Grace Kloba22ac16e2009-10-07 18:00:23 -07001456 // (most likely due to removing the parent tab) we must update the
Michael Kolbc831b632011-05-11 09:30:34 -07001457 // parent tab id in the saved Bundle.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001458 if (mSavedState != null) {
1459 if (parent == null) {
1460 mSavedState.remove(PARENTTAB);
1461 } else {
Michael Kolbc831b632011-05-11 09:30:34 -07001462 mSavedState.putLong(PARENTTAB, parent.getId());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001463 }
1464 }
John Reckb0a86db2011-05-24 14:05:58 -07001465
1466 // Sync the WebView useragent with the parent
1467 if (parent != null && mSettings.hasDesktopUseragent(parent.getWebView())
1468 != mSettings.hasDesktopUseragent(getWebView())) {
1469 mSettings.toggleDesktopUseragent(getWebView());
1470 }
John Reck52be4782011-08-26 15:37:29 -07001471
1472 if (parent != null && parent.getId() == getId()) {
1473 throw new IllegalStateException("Parent has same ID as child!");
1474 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001475 }
1476
1477 /**
Michael Kolbc831b632011-05-11 09:30:34 -07001478 * If this Tab was created through another Tab, then this method returns
1479 * that Tab.
1480 * @return the Tab parent or null
1481 */
1482 public Tab getParent() {
1483 return mParent;
1484 }
1485
1486 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001487 * When a Tab is created through the content of another Tab, then we
1488 * associate the Tabs.
1489 * @param child the Tab that was created from this Tab
1490 */
1491 void addChildTab(Tab child) {
Michael Kolbc831b632011-05-11 09:30:34 -07001492 if (mChildren == null) {
1493 mChildren = new Vector<Tab>();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001494 }
Michael Kolbc831b632011-05-11 09:30:34 -07001495 mChildren.add(child);
1496 child.setParent(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001497 }
1498
Michael Kolbc831b632011-05-11 09:30:34 -07001499 Vector<Tab> getChildren() {
1500 return mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001501 }
1502
1503 void resume() {
1504 if (mMainView != null) {
John Reck56c1fcf2011-08-17 10:15:16 -07001505 setupHwAcceleration(mMainView);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001506 mMainView.onResume();
1507 if (mSubView != null) {
1508 mSubView.onResume();
1509 }
1510 }
1511 }
1512
John Reck56c1fcf2011-08-17 10:15:16 -07001513 private void setupHwAcceleration(View web) {
1514 if (web == null) return;
1515 BrowserSettings settings = BrowserSettings.getInstance();
1516 if (settings.isHardwareAccelerated()) {
1517 web.setLayerType(View.LAYER_TYPE_NONE, null);
1518 } else {
1519 web.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
1520 }
1521 }
1522
Grace Kloba22ac16e2009-10-07 18:00:23 -07001523 void pause() {
1524 if (mMainView != null) {
1525 mMainView.onPause();
1526 if (mSubView != null) {
1527 mSubView.onPause();
1528 }
1529 }
1530 }
1531
1532 void putInForeground() {
John Reck8ee633f2011-08-09 16:00:35 -07001533 if (mInForeground) {
1534 return;
1535 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001536 mInForeground = true;
1537 resume();
Michael Kolb14612442011-06-24 13:06:29 -07001538 Activity activity = mWebViewController.getActivity();
1539 mMainView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001540 if (mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -07001541 mSubView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001542 }
Axesh R. Ajmerac6b5c322015-05-01 11:06:10 -07001543
Leon Scroggins1961ed22010-12-07 15:22:21 -05001544 mWebViewController.bookmarkedStatusHasChanged(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001545 }
1546
1547 void putInBackground() {
John Reck8ee633f2011-08-09 16:00:35 -07001548 if (!mInForeground) {
1549 return;
1550 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001551 mInForeground = false;
1552 pause();
1553 mMainView.setOnCreateContextMenuListener(null);
1554 if (mSubView != null) {
1555 mSubView.setOnCreateContextMenuListener(null);
1556 }
1557 }
1558
Michael Kolb8233fac2010-10-26 16:08:53 -07001559 boolean inForeground() {
1560 return mInForeground;
1561 }
1562
Grace Kloba22ac16e2009-10-07 18:00:23 -07001563 /**
1564 * Return the top window of this tab; either the subwindow if it is not
1565 * null or the main window.
1566 * @return The top window of this tab.
1567 */
1568 WebView getTopWindow() {
1569 if (mSubView != null) {
1570 return mSubView;
1571 }
1572 return mMainView;
1573 }
1574
1575 /**
1576 * Return the main window of this tab. Note: if a tab is freed in the
1577 * background, this can return null. It is only guaranteed to be
1578 * non-null for the current tab.
1579 * @return The main WebView of this tab.
1580 */
1581 WebView getWebView() {
1582 return mMainView;
1583 }
1584
Michael Kolba713ec82010-11-29 17:27:06 -08001585 void setViewContainer(View container) {
1586 mContainer = container;
1587 }
1588
Michael Kolb8233fac2010-10-26 16:08:53 -07001589 View getViewContainer() {
1590 return mContainer;
1591 }
1592
Grace Kloba22ac16e2009-10-07 18:00:23 -07001593 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001594 * Return whether private browsing is enabled for the main window of
1595 * this tab.
1596 * @return True if private browsing is enabled.
1597 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001598 boolean isPrivateBrowsingEnabled() {
John Reck502a3532011-08-16 14:21:46 -07001599 return mCurrentState.mIncognito;
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001600 }
1601
1602 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001603 * Return the subwindow of this tab or null if there is no subwindow.
1604 * @return The subwindow of this tab or null.
1605 */
1606 WebView getSubWebView() {
1607 return mSubView;
1608 }
1609
Michael Kolb1514bb72010-11-22 09:11:48 -08001610 void setSubWebView(WebView subView) {
1611 mSubView = subView;
1612 }
1613
Michael Kolb8233fac2010-10-26 16:08:53 -07001614 View getSubViewContainer() {
1615 return mSubViewContainer;
1616 }
1617
Michael Kolb1514bb72010-11-22 09:11:48 -08001618 void setSubViewContainer(View subViewContainer) {
1619 mSubViewContainer = subViewContainer;
1620 }
1621
Grace Kloba22ac16e2009-10-07 18:00:23 -07001622
1623 /**
1624 * @return The application id string
1625 */
1626 String getAppId() {
1627 return mAppId;
1628 }
1629
1630 /**
1631 * Set the application id string
1632 * @param id
1633 */
1634 void setAppId(String id) {
1635 mAppId = id;
1636 }
1637
Michael Kolbe28b3472011-08-04 16:54:31 -07001638 boolean closeOnBack() {
1639 return mCloseOnBack;
1640 }
1641
1642 void setCloseOnBack(boolean close) {
1643 mCloseOnBack = close;
1644 }
1645
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001646 boolean getDerivedFromIntent() {
1647 return mDerivedFromIntent;
1648 }
1649
1650 void setDerivedFromIntent(boolean derived) {
1651 mDerivedFromIntent = derived;
1652 }
1653
Grace Kloba22ac16e2009-10-07 18:00:23 -07001654 String getUrl() {
John Reck324d4402011-01-11 16:56:42 -08001655 return UrlUtils.filteredUrl(mCurrentState.mUrl);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001656 }
1657
Tarun Nainani8eb00912014-07-17 12:28:32 -07001658
1659 protected void onPageFinished() {
1660 mPageFinished = true;
Ze G Riande2a675c22015-06-03 11:15:24 -07001661 isDistillable();
Tarun Nainani8eb00912014-07-17 12:28:32 -07001662 }
1663
1664 public boolean getPageFinishedStatus() {
1665 return mPageFinished;
1666 }
1667
John Reck49a603c2011-03-03 09:33:05 -08001668 String getOriginalUrl() {
John Reckdb22ec42011-06-29 11:31:24 -07001669 if (mCurrentState.mOriginalUrl == null) {
1670 return getUrl();
John Reck49a603c2011-03-03 09:33:05 -08001671 }
John Reckdb22ec42011-06-29 11:31:24 -07001672 return UrlUtils.filteredUrl(mCurrentState.mOriginalUrl);
John Reck49a603c2011-03-03 09:33:05 -08001673 }
1674
Grace Kloba22ac16e2009-10-07 18:00:23 -07001675 /**
John Reck30c714c2010-12-16 17:30:34 -08001676 * Get the title of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001677 */
1678 String getTitle() {
John Reck30c714c2010-12-16 17:30:34 -08001679 return mCurrentState.mTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001680 }
1681
1682 /**
John Reck30c714c2010-12-16 17:30:34 -08001683 * Get the favicon of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001684 */
1685 Bitmap getFavicon() {
John Reck1cf4b792011-07-26 10:22:22 -07001686 if (mCurrentState.mFavicon != null) {
1687 return mCurrentState.mFavicon;
1688 }
1689 return getDefaultFavicon(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001690 }
1691
Pankaj Garg32e1b942015-06-03 18:13:24 -07001692 public boolean hasFavicon() {
1693 return mCurrentState.mFavicon != null;
1694 }
1695
John Recke969cc52010-12-21 17:24:43 -08001696 public boolean isBookmarkedSite() {
1697 return mCurrentState.mIsBookmarkedSite;
1698 }
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001699
Grace Kloba22ac16e2009-10-07 18:00:23 -07001700 /**
Steve Block08a6f0c2011-10-06 12:12:53 +01001701 * Sets the security state, clears the SSL certificate error and informs
1702 * the controller.
1703 */
Steve Block2466eff2011-10-03 15:33:09 +01001704 private void setSecurityState(SecurityState securityState) {
1705 mCurrentState.mSecurityState = securityState;
1706 mWebViewController.onUpdatedSecurityState(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001707 }
1708
1709 /**
Steve Block2466eff2011-10-03 15:33:09 +01001710 * @return The tab's security state.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001711 */
Steve Block2466eff2011-10-03 15:33:09 +01001712 SecurityState getSecurityState() {
1713 return mCurrentState.mSecurityState;
John Reck30c714c2010-12-16 17:30:34 -08001714 }
1715
1716 int getLoadProgress() {
1717 if (mInPageLoad) {
1718 return mPageLoadProgress;
1719 }
1720 return 100;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001721 }
1722
1723 /**
1724 * @return TRUE if onPageStarted is called while onPageFinished is not
1725 * called yet.
1726 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001727 boolean inPageLoad() {
1728 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001729 }
1730
Grace Kloba22ac16e2009-10-07 18:00:23 -07001731 /**
John Reck1cf4b792011-07-26 10:22:22 -07001732 * @return The Bundle with the tab's state if it can be saved, otherwise null
Grace Kloba22ac16e2009-10-07 18:00:23 -07001733 */
John Reck1cf4b792011-07-26 10:22:22 -07001734 public Bundle saveState() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001735 // If the WebView is null it means we ran low on memory and we already
1736 // stored the saved state in mSavedState.
1737 if (mMainView == null) {
John Reck1cf4b792011-07-26 10:22:22 -07001738 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001739 }
John Reck6c2e2f32011-08-22 13:41:23 -07001740
1741 if (TextUtils.isEmpty(mCurrentState.mUrl)) {
John Reck1cf4b792011-07-26 10:22:22 -07001742 return null;
John Reck24f18262011-06-17 14:47:20 -07001743 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001744
1745 mSavedState = new Bundle();
John Reck6c2e2f32011-08-22 13:41:23 -07001746 WebBackForwardList savedList = mMainView.saveState(mSavedState);
1747 if (savedList == null || savedList.getSize() == 0) {
1748 Log.w(LOGTAG, "Failed to save back/forward list for "
1749 + mCurrentState.mUrl);
1750 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001751
Michael Kolbc831b632011-05-11 09:30:34 -07001752 mSavedState.putLong(ID, mId);
John Reck30c714c2010-12-16 17:30:34 -08001753 mSavedState.putString(CURRURL, mCurrentState.mUrl);
1754 mSavedState.putString(CURRTITLE, mCurrentState.mTitle);
John Reck1cf4b792011-07-26 10:22:22 -07001755 mSavedState.putBoolean(INCOGNITO, mMainView.isPrivateBrowsingEnabled());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001756 if (mAppId != null) {
1757 mSavedState.putString(APPID, mAppId);
1758 }
Michael Kolbe28b3472011-08-04 16:54:31 -07001759 mSavedState.putBoolean(CLOSEFLAG, mCloseOnBack);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001760 // Remember the parent tab so the relationship can be restored.
Michael Kolbc831b632011-05-11 09:30:34 -07001761 if (mParent != null) {
1762 mSavedState.putLong(PARENTTAB, mParent.mId);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001763 }
John Reckb0a86db2011-05-24 14:05:58 -07001764 mSavedState.putBoolean(USERAGENT,
1765 mSettings.hasDesktopUseragent(getWebView()));
John Reck1cf4b792011-07-26 10:22:22 -07001766 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001767 }
1768
1769 /*
1770 * Restore the state of the tab.
1771 */
John Reck1cf4b792011-07-26 10:22:22 -07001772 private void restoreState(Bundle b) {
1773 mSavedState = b;
1774 if (mSavedState == null) {
1775 return;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001776 }
1777 // Restore the internal state even if the WebView fails to restore.
1778 // This will maintain the app id, original url and close-on-exit values.
Michael Kolbc831b632011-05-11 09:30:34 -07001779 mId = b.getLong(ID);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001780 mAppId = b.getString(APPID);
Michael Kolbe28b3472011-08-04 16:54:31 -07001781 mCloseOnBack = b.getBoolean(CLOSEFLAG);
John Reck8b9bb8b2012-03-08 13:19:40 -08001782 restoreUserAgent();
John Reck1cf4b792011-07-26 10:22:22 -07001783 String url = b.getString(CURRURL);
1784 String title = b.getString(CURRTITLE);
1785 boolean incognito = b.getBoolean(INCOGNITO);
1786 mCurrentState = new PageState(mContext, incognito, url, null);
1787 mCurrentState.mTitle = title;
1788 synchronized (Tab.this) {
1789 if (mCapture != null) {
John Reck4eadc342011-10-31 14:04:10 -07001790 DataController.getInstance(mContext).loadThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07001791 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001792 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001793 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04001794
John Reck8b9bb8b2012-03-08 13:19:40 -08001795 private void restoreUserAgent() {
1796 if (mMainView == null || mSavedState == null) {
1797 return;
1798 }
1799 if (mSavedState.getBoolean(USERAGENT)
1800 != mSettings.hasDesktopUseragent(mMainView)) {
1801 mSettings.toggleDesktopUseragent(mMainView);
1802 }
1803 }
1804
Leon Scroggins1961ed22010-12-07 15:22:21 -05001805 public void updateBookmarkedStatus() {
John Recke969cc52010-12-21 17:24:43 -08001806 mDataController.queryBookmarkStatus(getUrl(), mIsBookmarkCallback);
Leon Scroggins1961ed22010-12-07 15:22:21 -05001807 }
1808
John Recke969cc52010-12-21 17:24:43 -08001809 private DataController.OnQueryUrlIsBookmark mIsBookmarkCallback
1810 = new DataController.OnQueryUrlIsBookmark() {
1811 @Override
1812 public void onQueryUrlIsBookmark(String url, boolean isBookmark) {
1813 if (mCurrentState.mUrl.equals(url)) {
1814 mCurrentState.mIsBookmarkedSite = isBookmark;
1815 mWebViewController.bookmarkedStatusHasChanged(Tab.this);
1816 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001817 }
John Recke969cc52010-12-21 17:24:43 -08001818 };
Michael Kolb1acef692011-03-08 14:12:06 -08001819
Michael Kolbeb95db42011-03-03 10:38:40 -08001820 public Bitmap getScreenshot() {
John Reck1cf4b792011-07-26 10:22:22 -07001821 synchronized (Tab.this) {
1822 return mCapture;
1823 }
Michael Kolbeb95db42011-03-03 10:38:40 -08001824 }
1825
John Reck541f55a2011-06-07 16:34:43 -07001826 public boolean isSnapshot() {
John Reck541f55a2011-06-07 16:34:43 -07001827 return false;
1828 }
1829
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001830 private static class SaveCallback implements ValueCallback<String> {
1831 boolean onReceiveValueCalled = false;
1832 private String mPath;
John Reck68234a92012-04-19 15:27:12 -07001833
1834 @Override
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001835 public void onReceiveValue(String path) {
1836 this.onReceiveValueCalled = true;
1837 this.mPath = path;
John Reck68234a92012-04-19 15:27:12 -07001838 synchronized (this) {
1839 notifyAll();
John Reck8cc92352011-07-06 17:41:52 -07001840 }
John Reck541f55a2011-06-07 16:34:43 -07001841 }
John Reck68234a92012-04-19 15:27:12 -07001842
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001843 public String getPath() {
1844 return mPath;
1845 }
John Reck68234a92012-04-19 15:27:12 -07001846 }
1847
1848 /**
1849 * Must be called on the UI thread
1850 */
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001851 public ContentValues createSnapshotValues(Bitmap bm) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001852 WebView web = getWebView();
John Reck68234a92012-04-19 15:27:12 -07001853 if (web == null) return null;
John Reckd8c74522011-06-14 08:45:00 -07001854 ContentValues values = new ContentValues();
1855 values.put(Snapshots.TITLE, mCurrentState.mTitle);
1856 values.put(Snapshots.URL, mCurrentState.mUrl);
John Reck68234a92012-04-19 15:27:12 -07001857 values.put(Snapshots.BACKGROUND, web.getPageBackgroundColor());
John Reck8cc92352011-07-06 17:41:52 -07001858 values.put(Snapshots.DATE_CREATED, System.currentTimeMillis());
1859 values.put(Snapshots.FAVICON, compressBitmap(getFavicon()));
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001860 values.put(Snapshots.THUMBNAIL, compressBitmap(bm));
John Reckd8c74522011-06-14 08:45:00 -07001861 return values;
John Reck541f55a2011-06-07 16:34:43 -07001862 }
1863
John Reck68234a92012-04-19 15:27:12 -07001864 /**
1865 * Probably want to call this on a background thread
1866 */
1867 public boolean saveViewState(ContentValues values) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001868 WebView web = getWebView();
John Reck68234a92012-04-19 15:27:12 -07001869 if (web == null) return false;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001870 String filename = UUID.randomUUID().toString();
John Reck68234a92012-04-19 15:27:12 -07001871 SaveCallback callback = new SaveCallback();
John Reck68234a92012-04-19 15:27:12 -07001872 try {
John Reck68234a92012-04-19 15:27:12 -07001873 synchronized (callback) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001874 web.saveViewState(filename, callback);
1875 callback.wait();
John Reck68234a92012-04-19 15:27:12 -07001876 }
John Reck68234a92012-04-19 15:27:12 -07001877 } catch (Exception e) {
1878 Log.w(LOGTAG, "Failed to save view state", e);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001879 String path = callback.getPath();
1880 if (path != null) {
1881 File file = mContext.getFileStreamPath(path);
1882 if (file.exists() && !file.delete()) {
1883 file.deleteOnExit();
1884 }
John Reck68234a92012-04-19 15:27:12 -07001885 }
1886 return false;
1887 }
Tarun Nainani8eb00912014-07-17 12:28:32 -07001888
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001889 String path = callback.getPath();
Tarun Nainani8eb00912014-07-17 12:28:32 -07001890 // could be that saving of file failed
1891 if (path == null) {
1892 return false;
1893 }
1894
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001895 File savedFile = new File(path);
1896 if (!savedFile.exists()) {
1897 return false;
John Reck68234a92012-04-19 15:27:12 -07001898 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001899 values.put(Snapshots.VIEWSTATE_PATH, path.substring(path.lastIndexOf('/') + 1));
1900 values.put(Snapshots.VIEWSTATE_SIZE, savedFile.length());
John Reck68234a92012-04-19 15:27:12 -07001901 return true;
1902 }
1903
John Reck8cc92352011-07-06 17:41:52 -07001904 public byte[] compressBitmap(Bitmap bitmap) {
1905 if (bitmap == null) {
1906 return null;
1907 }
1908 ByteArrayOutputStream stream = new ByteArrayOutputStream();
1909 bitmap.compress(CompressFormat.PNG, 100, stream);
1910 return stream.toByteArray();
1911 }
1912
John Reck26b18322011-06-21 13:08:58 -07001913 public void loadUrl(String url, Map<String, String> headers) {
1914 if (mMainView != null) {
Michael Kolba53c9892011-10-05 13:31:40 -07001915 mPageLoadProgress = INITIAL_PROGRESS;
Axesh R. Ajmerad04cad12015-08-12 12:02:44 -07001916 mCurrentState = new PageState(
1917 mContext, mMainView.isPrivateBrowsingEnabled(), url, null);
John Reck26b18322011-06-21 13:08:58 -07001918 mMainView.loadUrl(url, headers);
1919 }
1920 }
1921
John Reck38b39652012-06-05 09:22:59 -07001922 public void disableUrlOverridingForLoad() {
1923 mDisableOverrideUrlLoading = true;
1924 }
1925
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001926 private void thumbnailUpdated() {
John Reck8ee633f2011-08-09 16:00:35 -07001927 mHandler.removeMessages(MSG_CAPTURE);
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001928
John Reck8ee633f2011-08-09 16:00:35 -07001929 TabControl tc = mWebViewController.getTabControl();
1930 if (tc != null) {
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001931 OnThumbnailUpdatedListener updateListener = tc.getOnThumbnailUpdatedListener();
John Reck8ee633f2011-08-09 16:00:35 -07001932 if (updateListener != null) {
1933 updateListener.onThumbnailUpdated(this);
1934 }
1935 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001936 }
1937
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001938 protected void capture() {
1939 if (mMainView == null || mCapture == null || !mMainView.isReady() ||
1940 mMainView.getContentWidth() <= 0 || mMainView.getContentHeight() <= 0 ||
1941 !mFirstVisualPixelPainted || mMainView.isShowingCrashView()) {
1942
1943 initCaptureBitmap();
1944 thumbnailUpdated();
1945 return;
1946 }
1947
1948 mMainView.getContentBitmapAsync((float) mCaptureWidth / mMainView.getWidth(), new Rect(),
1949 new ValueCallback<Bitmap>() {
1950 @Override
1951 public void onReceiveValue(Bitmap bitmap) {
1952 if (mCapture == null) {
1953 initCaptureBitmap();
1954 }
1955
1956 if (bitmap == null) {
1957 thumbnailUpdated();
1958 return;
1959 }
1960
1961 Canvas c = new Canvas(mCapture);
1962 mCapture.eraseColor(Color.WHITE);
1963 c.drawBitmap(bitmap, 0, 0, null);
1964
1965 // manually anti-alias the edges for the tilt
1966 c.drawRect(0, 0, 1, mCapture.getHeight(), sAlphaPaint);
1967 c.drawRect(mCapture.getWidth() - 1, 0, mCapture.getWidth(),
1968 mCapture.getHeight(), sAlphaPaint);
1969 c.drawRect(0, 0, mCapture.getWidth(), 1, sAlphaPaint);
1970 c.drawRect(0, mCapture.getHeight() - 1, mCapture.getWidth(),
1971 mCapture.getHeight(), sAlphaPaint);
1972 c.setBitmap(null);
1973
1974 persistThumbnail();
1975 thumbnailUpdated();
1976 }
1977 }
1978 );
John Reck1cf4b792011-07-26 10:22:22 -07001979 }
1980
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001981 @Override
1982 public void onNewPicture(WebView view, Picture picture) {
Michael Kolb9ef259a2011-07-12 15:33:08 -07001983 }
1984
John Reckef654f12011-07-12 16:42:08 -07001985 public boolean canGoBack() {
1986 return mMainView != null ? mMainView.canGoBack() : false;
1987 }
1988
1989 public boolean canGoForward() {
1990 return mMainView != null ? mMainView.canGoForward() : false;
1991 }
1992
1993 public void goBack() {
1994 if (mMainView != null) {
1995 mMainView.goBack();
1996 }
1997 }
1998
1999 public void goForward() {
2000 if (mMainView != null) {
2001 mMainView.goForward();
2002 }
2003 }
2004
John Reck1cf4b792011-07-26 10:22:22 -07002005 protected void persistThumbnail() {
John Reck4eadc342011-10-31 14:04:10 -07002006 DataController.getInstance(mContext).saveThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07002007 }
2008
2009 protected void deleteThumbnail() {
John Reck4eadc342011-10-31 14:04:10 -07002010 DataController.getInstance(mContext).deleteThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07002011 }
2012
John Reck4eadc342011-10-31 14:04:10 -07002013 void updateCaptureFromBlob(byte[] blob) {
John Reck1cf4b792011-07-26 10:22:22 -07002014 synchronized (Tab.this) {
2015 if (mCapture == null) {
2016 return;
2017 }
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002018 ByteBuffer buffer = ByteBuffer.wrap(blob);
Michael Kolbd837a112011-08-09 14:00:18 -07002019 try {
2020 mCapture.copyPixelsFromBuffer(buffer);
2021 } catch (RuntimeException rex) {
2022 Log.e(LOGTAG, "Load capture has mismatched sizes; buffer: "
2023 + buffer.capacity() + " blob: " + blob.length
2024 + "capture: " + mCapture.getByteCount());
2025 throw rex;
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002026 }
John Reck1cf4b792011-07-26 10:22:22 -07002027 }
2028 }
2029
John Reck52be4782011-08-26 15:37:29 -07002030 @Override
2031 public String toString() {
2032 StringBuilder builder = new StringBuilder(100);
2033 builder.append(mId);
2034 builder.append(") has parent: ");
2035 if (getParent() != null) {
2036 builder.append("true[");
2037 builder.append(getParent().getId());
2038 builder.append("]");
2039 } else {
2040 builder.append("false");
2041 }
2042 builder.append(", incog: ");
2043 builder.append(isPrivateBrowsingEnabled());
2044 if (!isPrivateBrowsingEnabled()) {
2045 builder.append(", title: ");
2046 builder.append(getTitle());
2047 builder.append(", url: ");
2048 builder.append(getUrl());
2049 }
2050 return builder.toString();
2051 }
2052
Ze G Riande2a675c22015-06-03 11:15:24 -07002053 // dertermines if the tab contains a dislled page
2054 public boolean isDistilled() {
2055 if (!BrowserCommandLine.hasSwitch("reader-mode")) {
2056 return false;
2057 }
2058 try {
2059 return DomDistillerUtils.isUrlDistilled(getUrl());
2060 } catch (Exception e) {
2061 return false;
2062 }
2063 }
2064
2065 //determines if the tab contains a distillable page
2066 public boolean isDistillable() {
2067 if (!BrowserCommandLine.hasSwitch("reader-mode")) {
2068 mIsDistillable = false;
2069 return mIsDistillable;
2070 }
2071 final ValueCallback<String> onIsDistillable = new ValueCallback<String>() {
2072 @Override
2073 public void onReceiveValue(String str) {
2074 mIsDistillable = Boolean.parseBoolean(str);
2075 }
2076 };
2077
2078 if (isDistilled()) {
2079 mIsDistillable = true;
2080 return mIsDistillable;
2081 }
2082
2083 try {
2084 DomDistillerUtils.isWebViewDistillable(getWebView(), onIsDistillable);
2085 } catch (Exception e) {
2086 mIsDistillable = false;
2087 }
2088
2089 return mIsDistillable;
2090 }
2091
2092 // Function that sets the mIsDistillable variable
2093 public void setIsDistillable(boolean value) {
2094 if (!BrowserCommandLine.hasSwitch("reader-mode")) {
2095 mIsDistillable = false;
2096 }
2097 mIsDistillable = value;
2098 }
2099
2100 // Function that returns the distilled url of the current url
2101 public String getDistilledUrl() {
2102 if (getUrl() != null) {
2103 return DomDistillerUtils.getDistilledUrl(getUrl());
2104 }
2105 return new String();
2106 }
2107
2108 // function that returns the non-distilled version of the current url
2109 public String getNonDistilledUrl() {
2110 if (getUrl() != null) {
2111 return DomDistillerUtils.getOriginalUrlFromDistilledUrl(getUrl());
2112 }
2113 return new String();
2114 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002115}