blob: e18cb2ac805a9ae90b952ee918b880a5e729ae52 [file] [log] [blame]
Grace Kloba22ac16e2009-10-07 18:00:23 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Bijan Amirzada41242f22014-03-21 12:12:18 -070017package com.android.browser;
Grace Kloba22ac16e2009-10-07 18:00:23 -070018
Michael Kolb8233fac2010-10-26 16:08:53 -070019import android.app.Activity;
Grace Kloba22ac16e2009-10-07 18:00:23 -070020import android.app.AlertDialog;
21import android.content.ContentResolver;
John Reckd8c74522011-06-14 08:45:00 -070022import android.content.ContentValues;
John Reck30c714c2010-12-16 17:30:34 -080023import android.content.Context;
Grace Kloba22ac16e2009-10-07 18:00:23 -070024import android.content.DialogInterface;
Michael Kolbfe251992010-07-08 15:41:55 -070025import android.content.DialogInterface.OnCancelListener;
Pankaj Garg1c13cab2015-05-12 11:52:17 -070026import android.content.res.Configuration;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080027import android.content.res.Resources;
Grace Kloba22ac16e2009-10-07 18:00:23 -070028import android.graphics.Bitmap;
John Reck8cc92352011-07-06 17:41:52 -070029import android.graphics.Bitmap.CompressFormat;
Michael Kolb9ef259a2011-07-12 15:33:08 -070030import android.graphics.BitmapFactory;
31import android.graphics.Canvas;
Michael Kolbc3af0672011-08-09 10:24:41 -070032import android.graphics.Color;
Michael Kolba3194d02011-09-07 11:23:51 -070033import android.graphics.Paint;
Michael Kolb9ef259a2011-07-12 15:33:08 -070034import android.graphics.Picture;
Michael Kolba3194d02011-09-07 11:23:51 -070035import android.graphics.PorterDuff;
36import android.graphics.PorterDuffXfermode;
Tarun Nainaniea28dde2014-08-27 17:25:09 -070037import android.graphics.Rect;
Grace Kloba22ac16e2009-10-07 18:00:23 -070038import android.net.Uri;
Grace Kloba22ac16e2009-10-07 18:00:23 -070039import android.os.Bundle;
Michael Kolb9ef259a2011-07-12 15:33:08 -070040import android.os.Handler;
Grace Kloba22ac16e2009-10-07 18:00:23 -070041import android.os.Message;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +000042import android.os.SystemClock;
Brian Carlstrom8862c1d2011-06-02 01:05:55 -070043import android.security.KeyChain;
Brian Carlstromaa09cd82011-06-09 16:04:40 -070044import android.security.KeyChainAliasCallback;
John Reck24f18262011-06-17 14:47:20 -070045import android.text.TextUtils;
Grace Kloba22ac16e2009-10-07 18:00:23 -070046import android.util.Log;
47import android.view.KeyEvent;
48import android.view.LayoutInflater;
49import android.view.View;
Ben Murdochc42addf2010-01-28 15:19:59 +000050import android.webkit.ConsoleMessage;
Grace Kloba22ac16e2009-10-07 18:00:23 -070051import android.webkit.URLUtil;
John Reck438bf462011-01-12 18:11:46 -080052import android.webkit.WebResourceResponse;
Grace Kloba22ac16e2009-10-07 18:00:23 -070053import android.webkit.WebStorage;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080054import android.webkit.WebChromeClient.CustomViewCallback;
55import android.webkit.ValueCallback;
Ben Murdoch1d676b62011-01-17 12:54:24 +000056import android.widget.CheckBox;
Ben Murdoch8029a772010-11-16 11:58:21 +000057import android.widget.Toast;
Grace Kloba22ac16e2009-10-07 18:00:23 -070058
Bijan Amirzada41242f22014-03-21 12:12:18 -070059import com.android.browser.TabControl.OnThumbnailUpdatedListener;
60import com.android.browser.homepages.HomeProvider;
61import com.android.browser.mynavigation.MyNavigationUtil;
62import com.android.browser.provider.MyNavigationProvider;
63import com.android.browser.provider.SnapshotProvider.Snapshots;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080064
Pankaj Garg18aa0a12015-06-22 11:06:12 -070065import org.codeaurora.swe.BrowserCommandLine;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080066import org.codeaurora.swe.BrowserDownloadListener;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080067import org.codeaurora.swe.HttpAuthHandler;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080068import org.codeaurora.swe.WebBackForwardList;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080069import org.codeaurora.swe.WebChromeClient;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080070import org.codeaurora.swe.WebView;
71import org.codeaurora.swe.WebView.PictureListener;
Pankaj Garg1c7380d2014-08-27 14:17:12 -070072import org.codeaurora.swe.WebView.CreateWindowParams;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080073import org.codeaurora.swe.WebViewClient;
Pankaj Garg1c13cab2015-05-12 11:52:17 -070074import org.codeaurora.swe.util.Observable;
Ze G Riande2a675c22015-06-03 11:15:24 -070075import org.codeaurora.swe.DomDistillerUtils;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080076
John Reck541f55a2011-06-07 16:34:43 -070077import java.io.ByteArrayOutputStream;
John Reck2b71d6d2012-04-18 17:42:06 -070078import java.io.File;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080079import java.io.InputStream;
John Reck1cf4b792011-07-26 10:22:22 -070080import java.nio.ByteBuffer;
Michael Kolbfe251992010-07-08 15:41:55 -070081import java.util.Map;
John Reck2b71d6d2012-04-18 17:42:06 -070082import java.util.UUID;
Michael Kolbfe251992010-07-08 15:41:55 -070083import java.util.Vector;
Vivek Sekhar53ef8932015-06-18 16:51:43 -070084import java.util.List;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080085import java.sql.Timestamp;
86import java.util.Date;
Michael Kolbfe251992010-07-08 15:41:55 -070087
Grace Kloba22ac16e2009-10-07 18:00:23 -070088/**
89 * Class for maintaining Tabs with a main WebView and a subwindow.
90 */
Michael Kolb9ef259a2011-07-12 15:33:08 -070091class Tab implements PictureListener {
Michael Kolb8233fac2010-10-26 16:08:53 -070092
Grace Kloba22ac16e2009-10-07 18:00:23 -070093 // Log Tag
94 private static final String LOGTAG = "Tab";
Bijan Amirzada41242f22014-03-21 12:12:18 -070095 private static final boolean LOGD_ENABLED = com.android.browser.Browser.LOGD_ENABLED;
Ben Murdochc42addf2010-01-28 15:19:59 +000096 // Special case the logtag for messages for the Console to make it easier to
97 // filter them and match the logtag used for these messages in older versions
98 // of the browser.
99 private static final String CONSOLE_LOGTAG = "browser";
100
Michael Kolb9ef259a2011-07-12 15:33:08 -0700101 private static final int MSG_CAPTURE = 42;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800102 private static final int CAPTURE_DELAY = 1000;
Michael Kolba53c9892011-10-05 13:31:40 -0700103 private static final int INITIAL_PROGRESS = 5;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700104
John Reck1cf4b792011-07-26 10:22:22 -0700105 private static Bitmap sDefaultFavicon;
Sagar Dhawan33551ff2015-07-08 17:24:44 -0700106 private boolean mIsKeyboardUp = false;
John Reck1cf4b792011-07-26 10:22:22 -0700107
Michael Kolba3194d02011-09-07 11:23:51 -0700108 private static Paint sAlphaPaint = new Paint();
109 static {
110 sAlphaPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
111 sAlphaPaint.setColor(Color.TRANSPARENT);
112 }
113
Steve Block2466eff2011-10-03 15:33:09 +0100114 public enum SecurityState {
Steve Block4895b012011-10-03 16:26:46 +0100115 // The page's main resource does not use SSL. Note that we use this
116 // state irrespective of the SSL authentication state of sub-resources.
Steve Block2466eff2011-10-03 15:33:09 +0100117 SECURITY_STATE_NOT_SECURE,
Steve Block4895b012011-10-03 16:26:46 +0100118 // The page's main resource uses SSL and the certificate is good. The
119 // same is true of all sub-resources.
Steve Block2466eff2011-10-03 15:33:09 +0100120 SECURITY_STATE_SECURE,
Steve Block4895b012011-10-03 16:26:46 +0100121 // The page's main resource uses SSL and the certificate is good, but
122 // some sub-resources either do not use SSL or have problems with their
123 // certificates.
Steve Block2466eff2011-10-03 15:33:09 +0100124 SECURITY_STATE_MIXED,
Steve Block4895b012011-10-03 16:26:46 +0100125 // The page's main resource uses SSL but there is a problem with its
126 // certificate.
127 SECURITY_STATE_BAD_CERTIFICATE,
John Reck30c714c2010-12-16 17:30:34 -0800128 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700129
Michael Kolb14612442011-06-24 13:06:29 -0700130 Context mContext;
John Reckd8c74522011-06-14 08:45:00 -0700131 protected WebViewController mWebViewController;
Michael Kolb8233fac2010-10-26 16:08:53 -0700132
Michael Kolbc831b632011-05-11 09:30:34 -0700133 // The tab ID
John Reckd8c74522011-06-14 08:45:00 -0700134 private long mId = -1;
Michael Kolbc831b632011-05-11 09:30:34 -0700135
Grace Kloba22ac16e2009-10-07 18:00:23 -0700136 // Main WebView wrapper
Michael Kolba713ec82010-11-29 17:27:06 -0800137 private View mContainer;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700138 // Main WebView
139 private WebView mMainView;
140 // Subwindow container
141 private View mSubViewContainer;
142 // Subwindow WebView
143 private WebView mSubView;
144 // Saved bundle for when we are running low on memory. It contains the
145 // information needed to restore the WebView if the user goes back to the
146 // tab.
147 private Bundle mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700148 // Parent Tab. This is the Tab that created this Tab, or null if the Tab was
149 // created by the UI
Michael Kolbc831b632011-05-11 09:30:34 -0700150 private Tab mParent;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700151 // Tab that constructed by this Tab. This is used when this Tab is
152 // destroyed, it clears all mParentTab values in the children.
Michael Kolbc831b632011-05-11 09:30:34 -0700153 private Vector<Tab> mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700154 // If true, the tab is in the foreground of the current activity.
155 private boolean mInForeground;
Michael Kolb8233fac2010-10-26 16:08:53 -0700156 // If true, the tab is in page loading state (after onPageStarted,
157 // before onPageFinsihed)
158 private boolean mInPageLoad;
Tarun Nainani8eb00912014-07-17 12:28:32 -0700159 private boolean mPageFinished;
John Reck38b39652012-06-05 09:22:59 -0700160 private boolean mDisableOverrideUrlLoading;
Pankaj Garg79878492015-04-01 14:48:21 -0700161 private boolean mFirstVisualPixelPainted = false;
John Reck30c714c2010-12-16 17:30:34 -0800162 // The last reported progress of the current page
163 private int mPageLoadProgress;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000164 // The time the load started, used to find load page time
165 private long mLoadStartTime;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700166 // Application identifier used to find tabs that another application wants
167 // to reuse.
168 private String mAppId;
Michael Kolbe28b3472011-08-04 16:54:31 -0700169 // flag to indicate if tab should be closed on back
170 private boolean mCloseOnBack;
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700171 // flag to indicate if the tab was opened from an intent
172 private boolean mDerivedFromIntent = false;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -0500173 // The listener that gets invoked when a download is started from the
174 // mMainView
Selim Gurun0b3d66f2012-08-29 13:08:13 -0700175 private final BrowserDownloadListener mDownloadListener;
John Recke969cc52010-12-21 17:24:43 -0800176 private DataController mDataController;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700177
178 // AsyncTask for downloading touch icons
179 DownloadTouchIcon mTouchIconLoader;
180
John Reck35e9dd62011-04-25 09:01:54 -0700181 private BrowserSettings mSettings;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700182 private int mCaptureWidth;
183 private int mCaptureHeight;
184 private Bitmap mCapture;
185 private Handler mHandler;
Michael Kolb72864272012-05-03 15:42:15 -0700186 private boolean mUpdateThumbnail;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800187 private Timestamp timestamp;
Vivek Sekharf96064b2014-07-28 16:32:34 -0700188 private boolean mFullScreen = false;
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800189 private boolean mReceivedError;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700190
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -0700191 // determine if webview is destroyed to MemoryMonitor
192 private boolean mWebViewDestroyedByMemoryMonitor;
193
Pankaj Garg21dad562015-07-02 17:17:24 -0700194 private String mTouchIconUrl;
195
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700196 private Observable mFirstPixelObservable;
197 private Observable mTabHistoryUpdateObservable;
198
199 Observable getFirstPixelObservable() {
200 return mFirstPixelObservable;
201 }
202
203 Observable getTabHistoryUpdateObservable() {
204 return mTabHistoryUpdateObservable;
205 }
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -0700206
Ze G Riande2a675c22015-06-03 11:15:24 -0700207 // dertermines if the tab contains a disllable page
208 private boolean mIsDistillable = false;
Mathew Inwood1dd8e822011-08-03 14:34:29 +0100209
John Reck1cf4b792011-07-26 10:22:22 -0700210 private static synchronized Bitmap getDefaultFavicon(Context context) {
211 if (sDefaultFavicon == null) {
212 sDefaultFavicon = BitmapFactory.decodeResource(
Enrico Rosd6efa972014-12-02 19:49:59 -0800213 context.getResources(), R.drawable.ic_deco_favicon_normal);
John Reck1cf4b792011-07-26 10:22:22 -0700214 }
215 return sDefaultFavicon;
216 }
Michael Kolbeb95db42011-03-03 10:38:40 -0800217
John Reck30c714c2010-12-16 17:30:34 -0800218 // All the state needed for a page
John Reckd8c74522011-06-14 08:45:00 -0700219 protected static class PageState {
John Reck30c714c2010-12-16 17:30:34 -0800220 String mUrl;
John Reckdb22ec42011-06-29 11:31:24 -0700221 String mOriginalUrl;
John Reck30c714c2010-12-16 17:30:34 -0800222 String mTitle;
Steve Block2466eff2011-10-03 15:33:09 +0100223 SecurityState mSecurityState;
Steve Block08a6f0c2011-10-06 12:12:53 +0100224 // This is non-null only when mSecurityState is SECURITY_STATE_BAD_CERTIFICATE.
John Reck30c714c2010-12-16 17:30:34 -0800225 Bitmap mFavicon;
Steve Block08a6f0c2011-10-06 12:12:53 +0100226 boolean mIsBookmarkedSite;
227 boolean mIncognito;
John Reck30c714c2010-12-16 17:30:34 -0800228
229 PageState(Context c, boolean incognito) {
John Reck502a3532011-08-16 14:21:46 -0700230 mIncognito = incognito;
231 if (mIncognito) {
Axesh R. Ajmera1f998ae2015-04-21 14:16:41 -0700232 mOriginalUrl = mUrl = Controller.INCOGNITO_URI;
John Reck30c714c2010-12-16 17:30:34 -0800233 mTitle = c.getString(R.string.new_incognito_tab);
John Reck30c714c2010-12-16 17:30:34 -0800234 } else {
John Reckdb22ec42011-06-29 11:31:24 -0700235 mOriginalUrl = mUrl = "";
John Reck30c714c2010-12-16 17:30:34 -0800236 mTitle = c.getString(R.string.new_tab);
John Reck30c714c2010-12-16 17:30:34 -0800237 }
Steve Block2466eff2011-10-03 15:33:09 +0100238 mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
John Reck30c714c2010-12-16 17:30:34 -0800239 }
240
241 PageState(Context c, boolean incognito, String url, Bitmap favicon) {
John Reck502a3532011-08-16 14:21:46 -0700242 mIncognito = incognito;
John Reckdb22ec42011-06-29 11:31:24 -0700243 mOriginalUrl = mUrl = url;
Pankaj Garg96d0ccd2015-07-30 16:49:47 -0700244 mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
John Reck1cf4b792011-07-26 10:22:22 -0700245 mFavicon = favicon;
John Reck30c714c2010-12-16 17:30:34 -0800246 }
John Reck1cf4b792011-07-26 10:22:22 -0700247
Grace Kloba22ac16e2009-10-07 18:00:23 -0700248 }
249
John Reck30c714c2010-12-16 17:30:34 -0800250 // The current/loading page's state
John Reckd8c74522011-06-14 08:45:00 -0700251 protected PageState mCurrentState;
John Reck30c714c2010-12-16 17:30:34 -0800252
Grace Kloba22ac16e2009-10-07 18:00:23 -0700253 // Used for saving and restoring each Tab
Michael Kolbc831b632011-05-11 09:30:34 -0700254 static final String ID = "ID";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700255 static final String CURRURL = "currentUrl";
256 static final String CURRTITLE = "currentTitle";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700257 static final String PARENTTAB = "parentTab";
258 static final String APPID = "appid";
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700259 static final String INCOGNITO = "privateBrowsingEnabled";
John Reckb0a86db2011-05-24 14:05:58 -0700260 static final String USERAGENT = "useragent";
Michael Kolbe28b3472011-08-04 16:54:31 -0700261 static final String CLOSEFLAG = "closeOnBack";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700262
Pankaj Garg18186a92015-03-31 14:59:33 -0700263 public void setNetworkAvailable(boolean networkUp) {
264 if (networkUp && mReceivedError && (mMainView != null)) {
265 mMainView.reload();
266 }
267 }
268
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700269 public boolean isFirstVisualPixelPainted() {
270 return mFirstVisualPixelPainted;
271 }
272
273 public int getCaptureIndex(int navIndex) {
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700274 int orientation = mWebViewController.getActivity().
275 getResources().getConfiguration().orientation;
276
277 int orientationBit = (orientation == Configuration.ORIENTATION_LANDSCAPE) ? 0 : 1;
278
Vivek Sekhard0f60402015-06-05 14:07:11 -0700279 int index = orientationBit << 31 | (((int)mId & 0x7f) << 24) | (navIndex & 0xffffff);
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700280 return index;
281 }
282
283 public int getTabIdxFromCaptureIdx(int index) {
284 return (index & 0x7f000000) >> 24;
285 }
286
287 public int getOrientationFromCaptureIdx(int index) {
288 return ((index & 0x80000000) == 0) ? Configuration.ORIENTATION_LANDSCAPE :
289 Configuration.ORIENTATION_PORTRAIT;
290
291 }
292
293 public int getNavIdxFromCaptureIdx(int index) {
294 return (index & 0xffffff);
295 }
296
Pankaj Garg96d0ccd2015-07-30 16:49:47 -0700297 public static SecurityState getWebViewSecurityState(WebView view) {
298 switch (view.getSecurityLevel()) {
299 case WebView.SecurityLevel.EV_SECURE:
300 case WebView.SecurityLevel.SECURE:
301 return SecurityState.SECURITY_STATE_SECURE;
302 case WebView.SecurityLevel.SECURITY_ERROR:
303 return SecurityState.SECURITY_STATE_BAD_CERTIFICATE;
304 case WebView.SecurityLevel.SECURITY_POLICY_WARNING:
305 case WebView.SecurityLevel.SECURITY_WARNING:
306 return SecurityState.SECURITY_STATE_MIXED;
307 }
308 return SecurityState.SECURITY_STATE_NOT_SECURE;
309 }
310
Grace Kloba22ac16e2009-10-07 18:00:23 -0700311 // -------------------------------------------------------------------------
312 // WebViewClient implementation for the main WebView
313 // -------------------------------------------------------------------------
314
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800315 private final WebViewClient mWebViewClient = new WebViewClient() {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500316 private Message mDontResend;
317 private Message mResend;
Michael Kolb47bd1e42011-09-01 15:25:00 -0700318
319 private boolean providersDiffer(String url, String otherUrl) {
320 Uri uri1 = Uri.parse(url);
321 Uri uri2 = Uri.parse(otherUrl);
322 return !uri1.getEncodedAuthority().equals(uri2.getEncodedAuthority());
323 }
324
Grace Kloba22ac16e2009-10-07 18:00:23 -0700325 @Override
326 public void onPageStarted(WebView view, String url, Bitmap favicon) {
Ze G Riande2a675c22015-06-03 11:15:24 -0700327 setIsDistillable(false);
Michael Kolb8233fac2010-10-26 16:08:53 -0700328 mInPageLoad = true;
Tarun Nainani8eb00912014-07-17 12:28:32 -0700329 mPageFinished = false;
Pankaj Garg79878492015-04-01 14:48:21 -0700330 mFirstVisualPixelPainted = false;
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700331 mFirstPixelObservable.set(false);
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800332 mReceivedError = false;
Michael Kolb72864272012-05-03 15:42:15 -0700333 mUpdateThumbnail = true;
Michael Kolba53c9892011-10-05 13:31:40 -0700334 mPageLoadProgress = INITIAL_PROGRESS;
Michael Kolb14612442011-06-24 13:06:29 -0700335 mCurrentState = new PageState(mContext,
John Reck30c714c2010-12-16 17:30:34 -0800336 view.isPrivateBrowsingEnabled(), url, favicon);
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000337 mLoadStartTime = SystemClock.uptimeMillis();
Pankaj Garg62bc7912015-04-14 16:08:59 -0700338 // Need re-enable FullScreenMode on Page navigation if needed
339 if (BrowserSettings.getInstance().useFullscreen()){
340 Controller controller = (Controller) mWebViewController;
341 BaseUi ui = (BaseUi) controller.getUi();
342 ui.forceDisableFullscreenMode(false);
343 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700344 // If we start a touch icon load and then load a new page, we don't
345 // want to cancel the current touch icon loader. But, we do want to
346 // create a new one when the touch icon url is known.
347 if (mTouchIconLoader != null) {
348 mTouchIconLoader.mTab = null;
349 mTouchIconLoader = null;
350 }
351
Grace Kloba22ac16e2009-10-07 18:00:23 -0700352 // finally update the UI in the activity if it is in the foreground
John Reck324d4402011-01-11 16:56:42 -0800353 mWebViewController.onPageStarted(Tab.this, view, favicon);
Leon Scroggins4cd97792010-12-03 15:31:56 -0500354
John Recke969cc52010-12-21 17:24:43 -0800355 updateBookmarkedStatus();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700356 }
357
358 @Override
359 public void onPageFinished(WebView view, String url) {
John Reck38b39652012-06-05 09:22:59 -0700360 mDisableOverrideUrlLoading = false;
John Reck5b691842010-11-29 11:21:13 -0800361 if (!isPrivateBrowsingEnabled()) {
362 LogTag.logPageFinishedLoading(
363 url, SystemClock.uptimeMillis() - mLoadStartTime);
364 }
John Reck1cf4b792011-07-26 10:22:22 -0700365 syncCurrentState(view, url);
John Reck324d4402011-01-11 16:56:42 -0800366 mWebViewController.onPageFinished(Tab.this);
Pankaj Garg96d0ccd2015-07-30 16:49:47 -0700367 setSecurityState(getWebViewSecurityState(view));
Grace Kloba22ac16e2009-10-07 18:00:23 -0700368 }
369
Pankaj Garg79878492015-04-01 14:48:21 -0700370 @Override
371 public void onFirstVisualPixel(WebView view) {
372 mFirstVisualPixelPainted = true;
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700373 mFirstPixelObservable.set(true);
Pankaj Garg79878492015-04-01 14:48:21 -0700374 }
375
Grace Kloba22ac16e2009-10-07 18:00:23 -0700376 // return true if want to hijack the url to let another app to handle it
377 @Override
378 public boolean shouldOverrideUrlLoading(WebView view, String url) {
John Reck38b39652012-06-05 09:22:59 -0700379 if (!mDisableOverrideUrlLoading && mInForeground) {
Michael Kolb18eb3772010-12-10 14:29:51 -0800380 return mWebViewController.shouldOverrideUrlLoading(Tab.this,
381 view, url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700382 } else {
383 return false;
384 }
385 }
386
Vivek Sekharb991edb2014-12-17 18:18:07 -0800387 @Override
388 public boolean shouldDownloadFavicon(WebView view, String url) {
389 return true;
390 }
391
Grace Kloba22ac16e2009-10-07 18:00:23 -0700392 /**
Steve Block2466eff2011-10-03 15:33:09 +0100393 * Updates the security state. This method is called when we discover
394 * another resource to be loaded for this page (for example,
395 * javascript). While we update the security state, we do not update
396 * the lock icon until we are done loading, as it is slightly more
397 * secure this way.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700398 */
399 @Override
400 public void onLoadResource(WebView view, String url) {
401 if (url != null && url.length() > 0) {
402 // It is only if the page claims to be secure that we may have
Steve Block2466eff2011-10-03 15:33:09 +0100403 // to update the security state:
404 if (mCurrentState.mSecurityState == SecurityState.SECURITY_STATE_SECURE) {
405 // If NOT a 'safe' url, change the state to mixed content!
Grace Kloba22ac16e2009-10-07 18:00:23 -0700406 if (!(URLUtil.isHttpsUrl(url) || URLUtil.isDataUrl(url)
407 || URLUtil.isAboutUrl(url))) {
Steve Block2466eff2011-10-03 15:33:09 +0100408 mCurrentState.mSecurityState = SecurityState.SECURITY_STATE_MIXED;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700409 }
410 }
411 }
412 }
413
414 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700415 * Show a dialog informing the user of the network error reported by
416 * WebCore if it is in the foreground.
417 */
418 @Override
419 public void onReceivedError(WebView view, int errorCode,
420 String description, String failingUrl) {
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800421 // Used for the syncCurrentState to use
422 // the failing url instead of using webview url
423 mReceivedError = true;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700424 }
425
426 /**
427 * Check with the user if it is ok to resend POST data as the page they
428 * are trying to navigate to is the result of a POST.
429 */
430 @Override
431 public void onFormResubmission(WebView view, final Message dontResend,
432 final Message resend) {
433 if (!mInForeground) {
434 dontResend.sendToTarget();
435 return;
436 }
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500437 if (mDontResend != null) {
438 Log.w(LOGTAG, "onFormResubmission should not be called again "
439 + "while dialog is still up");
440 dontResend.sendToTarget();
441 return;
442 }
443 mDontResend = dontResend;
444 mResend = resend;
Michael Kolb14612442011-06-24 13:06:29 -0700445 new AlertDialog.Builder(mContext).setTitle(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700446 R.string.browserFrameFormResubmitLabel).setMessage(
447 R.string.browserFrameFormResubmitMessage)
448 .setPositiveButton(R.string.ok,
449 new DialogInterface.OnClickListener() {
450 public void onClick(DialogInterface dialog,
451 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500452 if (mResend != null) {
453 mResend.sendToTarget();
454 mResend = null;
455 mDontResend = null;
456 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700457 }
458 }).setNegativeButton(R.string.cancel,
459 new DialogInterface.OnClickListener() {
460 public void onClick(DialogInterface dialog,
461 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500462 if (mDontResend != null) {
463 mDontResend.sendToTarget();
464 mResend = null;
465 mDontResend = null;
466 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700467 }
468 }).setOnCancelListener(new OnCancelListener() {
469 public void onCancel(DialogInterface dialog) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500470 if (mDontResend != null) {
471 mDontResend.sendToTarget();
472 mResend = null;
473 mDontResend = null;
474 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700475 }
476 }).show();
477 }
478
479 /**
480 * Insert the url into the visited history database.
481 * @param url The url to be inserted.
482 * @param isReload True if this url is being reloaded.
483 * FIXME: Not sure what to do when reloading the page.
484 */
485 @Override
486 public void doUpdateVisitedHistory(WebView view, String url,
487 boolean isReload) {
John Reck324d4402011-01-11 16:56:42 -0800488 mWebViewController.doUpdateVisitedHistory(Tab.this, isReload);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700489 }
490
491 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700492 * Handles an HTTP authentication request.
493 *
494 * @param handler The authentication handler
495 * @param host The host
496 * @param realm The realm
497 */
498 @Override
499 public void onReceivedHttpAuthRequest(WebView view,
500 final HttpAuthHandler handler, final String host,
501 final String realm) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700502 mWebViewController.onReceivedHttpAuthRequest(Tab.this, view, handler, host, realm);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700503 }
504
505 @Override
John Reck438bf462011-01-12 18:11:46 -0800506 public WebResourceResponse shouldInterceptRequest(WebView view,
507 String url) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800508 //intercept if opening a new incognito tab - show the incognito welcome page
Axesh R. Ajmera1f998ae2015-04-21 14:16:41 -0700509
510 // show only incognito content and webview has private
511 // and cannot go back(only supported if explicit from UI )
512 if (view.isPrivateBrowsingEnabled() &&
513 !view.canGoBack() &&
514 url.startsWith(Controller.INCOGNITO_URI) ) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800515 Resources resourceHandle = mContext.getResources();
516 InputStream inStream = resourceHandle.openRawResource(
Bijan Amirzada41242f22014-03-21 12:12:18 -0700517 com.android.browser.R.raw.incognito_mode_start_page);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800518 return new WebResourceResponse("text/html", "utf8", inStream);
519 }
kaiyiz6e5b3e02013-08-19 20:02:01 +0800520 WebResourceResponse res;
521 if (MyNavigationUtil.MY_NAVIGATION.equals(url)) {
522 res = MyNavigationProvider.shouldInterceptRequest(mContext, url);
523 } else {
524 res = HomeProvider.shouldInterceptRequest(mContext, url);
525 }
John Reck438bf462011-01-12 18:11:46 -0800526 return res;
527 }
528
529 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700530 public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
531 if (!mInForeground) {
532 return false;
533 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700534 return mWebViewController.shouldOverrideKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700535 }
536
537 @Override
538 public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700539 if (!mInForeground) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700540 return;
541 }
John Reck997b1b72012-04-19 18:08:25 -0700542 if (!mWebViewController.onUnhandledKeyEvent(event)) {
543 super.onUnhandledKeyEvent(view, event);
544 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700545 }
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700546
547 @Override
548 public void beforeNavigation(WebView view, String url) {
Pankaj Garg21dad562015-07-02 17:17:24 -0700549 mTouchIconUrl = null;
Site Mao61b68212015-07-16 10:56:31 -0700550 if (BaseUi.isUiLowPowerMode()) {
Pankaj Garg18aa0a12015-06-22 11:06:12 -0700551 return;
552 }
553
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700554 if (isPrivateBrowsingEnabled()) {
555 return;
556 }
557
558 if (!mFirstVisualPixelPainted) {
559 return;
560 }
561
Pankaj Garg8ee61462015-07-29 18:17:24 -0700562 if (view.getUrl().equals(url)) {
563 return;
564 }
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -0700565
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700566 final int idx = view.copyBackForwardList().getCurrentIndex();
567 boolean bitmapExists = view.hasSnapshot(idx);
568
569 int progress = 100;
570 Controller controller = (Controller)mWebViewController;
571 UI ui = controller.getUi();
572 if (ui instanceof BaseUi) {
573 BaseUi baseUi = (BaseUi) ui;
574 TitleBar titleBar = baseUi.getTitleBar();
575 progress = titleBar.getProgressView().getProgressPercent();
576 }
577
578 if (bitmapExists && progress < 85) {
579 return;
580 }
581
582 int index = getCaptureIndex(view.getLastCommittedHistoryIndex());
Sagar Dhawan49f85cf2015-07-10 16:54:20 -0700583 view.captureSnapshot(index, null);
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700584 }
585
586 @Override
587 public void onHistoryItemCommit(WebView view, int index) {
Site Mao61b68212015-07-16 10:56:31 -0700588 if (BaseUi.isUiLowPowerMode()) {
Pankaj Garg18aa0a12015-06-22 11:06:12 -0700589 return;
590 }
591
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -0700592 // prevent snapshot tab from commiting any history
593 if (isSnapshot()) {
594 return;
595 }
596
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700597 mTabHistoryUpdateObservable.set(index);
Vivek Sekhar53ef8932015-06-18 16:51:43 -0700598 final int maxIdx = view.copyBackForwardList().getSize();
599 final WebView wv = view;
600 view.getSnapshotIds(new ValueCallback <List<Integer>>() {
601 @Override
602 public void onReceiveValue(List<Integer> ids) {
603 int currentTabIdx = mWebViewController.getTabControl().getCurrentPosition();
604 for (Integer id : ids) {
605 if (getTabIdxFromCaptureIdx(id) == currentTabIdx &&
606 getNavIdxFromCaptureIdx(id) >= maxIdx) {
607 wv.deleteSnapshot(id);
608 }
609 }
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700610 }
Vivek Sekhar53ef8932015-06-18 16:51:43 -0700611 });
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700612 }
Pankaj Garg62bc7912015-04-14 16:08:59 -0700613
614 @Override
615 public void onKeyboardStateChange(boolean popup) {
Vivek Sekhard4de6162015-07-21 15:01:45 -0700616 boolean keyboardWasShowing = isKeyboardShowing();
Sagar Dhawan33551ff2015-07-08 17:24:44 -0700617 mIsKeyboardUp = popup;
Vivek Sekhard4de6162015-07-21 15:01:45 -0700618 Controller controller = (Controller)mWebViewController;
619 BaseUi ui = (BaseUi) controller.getUi();
620 // lock the title bar
621 if (popup)
622 ui.getTitleBar().showTopControls(true);
623 if (keyboardWasShowing && popup)
624 ui.getTitleBar().enableTopControls(true);
Pankaj Garg62bc7912015-04-14 16:08:59 -0700625 if (BrowserSettings.getInstance().useFullscreen()) {
Pankaj Garg62bc7912015-04-14 16:08:59 -0700626 ui.forceDisableFullscreenMode(popup);
627 }
628 }
Vivek Sekharae3b1792015-08-03 12:26:41 -0700629
630 @Override
631 public void onAttachInterstitialPage(WebView mWebView) {
632 Controller controller = (Controller)mWebViewController;
633 BaseUi ui = (BaseUi) controller.getUi();
634 ui.getTitleBar().showTopControls(false);
635 }
636
637 @Override
638 public void onDetachInterstitialPage(WebView mWebView) {
639 Controller controller = (Controller)mWebViewController;
640 BaseUi ui = (BaseUi) controller.getUi();
641 ui.getTitleBar().enableTopControls(true);
642 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700643 };
644
John Reck1cf4b792011-07-26 10:22:22 -0700645 private void syncCurrentState(WebView view, String url) {
646 // Sync state (in case of stop/timeout)
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800647
648 if (mReceivedError) {
649 mCurrentState.mUrl = url;
650 mCurrentState.mOriginalUrl = url;
651 } else {
652 mCurrentState.mUrl = view.getUrl();
653 mCurrentState.mOriginalUrl = view.getOriginalUrl();
654 mCurrentState.mFavicon = view.getFavicon();
655 }
656
John Reck1cf4b792011-07-26 10:22:22 -0700657 if (mCurrentState.mUrl == null) {
John Reck8bcafc12011-08-29 16:43:02 -0700658 mCurrentState.mUrl = "";
John Reck1cf4b792011-07-26 10:22:22 -0700659 }
John Reck1cf4b792011-07-26 10:22:22 -0700660 mCurrentState.mTitle = view.getTitle();
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800661
662
John Reck1cf4b792011-07-26 10:22:22 -0700663 if (!URLUtil.isHttpsUrl(mCurrentState.mUrl)) {
664 // In case we stop when loading an HTTPS page from an HTTP page
665 // but before a provisional load occurred
Steve Block2466eff2011-10-03 15:33:09 +0100666 mCurrentState.mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
John Reck1cf4b792011-07-26 10:22:22 -0700667 }
John Reck502a3532011-08-16 14:21:46 -0700668 mCurrentState.mIncognito = view.isPrivateBrowsingEnabled();
John Reck1cf4b792011-07-26 10:22:22 -0700669 }
670
Pankaj Garg21dad562015-07-02 17:17:24 -0700671 public String getTouchIconUrl() {
672 return mTouchIconUrl;
673 }
Tarun Nainani8eb00912014-07-17 12:28:32 -0700674
Sagar Dhawan33551ff2015-07-08 17:24:44 -0700675 public boolean isKeyboardShowing() {
676 Controller controller = (Controller)mWebViewController;
677 return (mIsKeyboardUp || controller.getUi().isEditingUrl());
678 }
679
Tarun Nainani8eb00912014-07-17 12:28:32 -0700680 public boolean isTabFullScreen() {
681 return mFullScreen;
682 }
683
Vivek Sekharf96064b2014-07-28 16:32:34 -0700684 protected void setTabFullscreen(boolean fullScreen) {
Tarun Nainani8eb00912014-07-17 12:28:32 -0700685 Controller controller = (Controller)mWebViewController;
Sudheer Koganti24766882014-10-02 10:58:09 -0700686 controller.getUi().showFullscreen(fullScreen);
Tarun Nainani8eb00912014-07-17 12:28:32 -0700687 mFullScreen = fullScreen;
Vivek Sekharf96064b2014-07-28 16:32:34 -0700688 }
689
Sudheer Koganti24766882014-10-02 10:58:09 -0700690 public boolean exitFullscreen() {
691 if (mFullScreen) {
692 Controller controller = (Controller)mWebViewController;
693 controller.getUi().showFullscreen(false);
694 if (getWebView() != null)
695 getWebView().exitFullscreen();
696 mFullScreen = false;
697 return true;
698 }
699 return false;
700 }
701
702
703
704
Grace Kloba22ac16e2009-10-07 18:00:23 -0700705 // -------------------------------------------------------------------------
706 // WebChromeClient implementation for the main WebView
707 // -------------------------------------------------------------------------
708
709 private final WebChromeClient mWebChromeClient = new WebChromeClient() {
710 // Helper method to create a new tab or sub window.
711 private void createWindow(final boolean dialog, final Message msg) {
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700712 this.createWindow(dialog, msg, null, false);
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700713 }
714
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700715 private void createWindow(final boolean dialog, final Message msg, final String url,
716 final boolean opener_suppressed) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700717 WebView.WebViewTransport transport =
718 (WebView.WebViewTransport) msg.obj;
719 if (dialog) {
720 createSubWindow();
Michael Kolb8233fac2010-10-26 16:08:53 -0700721 mWebViewController.attachSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700722 transport.setWebView(mSubView);
723 } else {
Pankaj Garg21dad562015-07-02 17:17:24 -0700724 capture();
725
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700726 final Tab newTab = mWebViewController.openTab(url,
John Reck5949c662011-05-27 09:52:29 -0700727 Tab.this, true, true);
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700728 // This is special case for rendering links on a webpage in
729 // a new tab. If opener is suppressed, the WebContents created
730 // by the content layer are not fully initialized. This check
731 // will prevent content layer from overriding WebContents
732 // created by new tab with the uninitialized instance.
733 if (!opener_suppressed) {
734 transport.setWebView(newTab.getWebView());
735 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700736 }
737 msg.sendToTarget();
738 }
739
740 @Override
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700741 public void toggleFullscreenModeForTab(boolean enterFullscreen) {
742 if (mWebViewController instanceof Controller) {
Vivek Sekharf96064b2014-07-28 16:32:34 -0700743 setTabFullscreen(enterFullscreen);
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700744 }
745 }
746
747 @Override
Tarun Nainani8eb00912014-07-17 12:28:32 -0700748 public void onOffsetsForFullscreenChanged(float topControlsOffsetYPix,
749 float contentOffsetYPix,
750 float overdrawBottomHeightPix) {
751 if (mWebViewController instanceof Controller) {
Vivek Sekhar8ee3abb2014-07-14 12:32:05 -0700752 Controller controller = (Controller)mWebViewController;
Tarun Nainani8eb00912014-07-17 12:28:32 -0700753 controller.getUi().translateTitleBar(topControlsOffsetYPix);
Vivek Sekhar11b40062015-06-24 11:49:04 -0700754 // Resize the viewport if top controls is not visible
Vivek Sekhar2ee19a32015-07-02 17:03:57 -0700755 if (mMainView != null &&
756 (topControlsOffsetYPix == 0.0f || contentOffsetYPix == 0.0f))
Vivek Sekhar11b40062015-06-24 11:49:04 -0700757 ((BrowserWebView)mMainView).enableTopControls(
758 (topControlsOffsetYPix == 0.0f) ? true : false);
Vivek Sekhar8ee3abb2014-07-14 12:32:05 -0700759 }
760 }
761
762 @Override
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700763 public boolean isTabFullScreen() {
Vivek Sekharf96064b2014-07-28 16:32:34 -0700764 return mFullScreen;
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700765 }
766
767 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700768 public boolean onCreateWindow(WebView view, final boolean dialog,
769 final boolean userGesture, final Message resultMsg) {
770 // only allow new window or sub window for the foreground case
771 if (!mInForeground) {
772 return false;
773 }
774 // Short-circuit if we can't create any more tabs or sub windows.
775 if (dialog && mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -0700776 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700777 .setTitle(R.string.too_many_subwindows_dialog_title)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200778 .setIconAttribute(android.R.attr.alertDialogIcon)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700779 .setMessage(R.string.too_many_subwindows_dialog_message)
780 .setPositiveButton(R.string.ok, null)
781 .show();
782 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700783 } else if (!mWebViewController.getTabControl().canCreateNewTab()) {
Michael Kolb14612442011-06-24 13:06:29 -0700784 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700785 .setTitle(R.string.too_many_windows_dialog_title)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200786 .setIconAttribute(android.R.attr.alertDialogIcon)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700787 .setMessage(R.string.too_many_windows_dialog_message)
788 .setPositiveButton(R.string.ok, null)
789 .show();
790 return false;
791 }
792
793 // Short-circuit if this was a user gesture.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800794 if (userGesture || !mSettings.blockPopupWindows()) {
Tarun Nainani4f5137d2015-04-16 17:26:18 -0700795 WebView.WebViewTransport transport =
796 (WebView.WebViewTransport) resultMsg.obj;
797 CreateWindowParams windowParams = transport.getCreateWindowParams();
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700798 if (windowParams.mOpenerSuppressed) {
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700799 createWindow(dialog, resultMsg, windowParams.mURL, true);
800 // This is special case for rendering links on a webpage in
801 // a new tab. If opener is suppressed, the WebContents created
802 // by the content layer are not fully initialized. Returning false
803 // will prevent content layer from overriding WebContents
804 // created by new tab with the uninitialized instance.
805 return false;
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700806 }
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700807
808 createWindow(dialog, resultMsg);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700809 return true;
810 }
811
Tarun Nainani4f5137d2015-04-16 17:26:18 -0700812 createWindow(dialog, resultMsg);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700813 return true;
814 }
815
816 @Override
Patrick Scotteb5061b2009-11-18 15:00:30 -0500817 public void onRequestFocus(WebView view) {
818 if (!mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700819 mWebViewController.switchToTab(Tab.this);
Patrick Scotteb5061b2009-11-18 15:00:30 -0500820 }
821 }
822
823 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700824 public void onCloseWindow(WebView window) {
Michael Kolbc831b632011-05-11 09:30:34 -0700825 if (mParent != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700826 // JavaScript can only close popup window.
827 if (mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700828 mWebViewController.switchToTab(mParent);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700829 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700830 mWebViewController.closeTab(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700831 }
832 }
833
834 @Override
835 public void onProgressChanged(WebView view, int newProgress) {
John Reck30c714c2010-12-16 17:30:34 -0800836 mPageLoadProgress = newProgress;
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800837 if (newProgress == 100) {
838 mInPageLoad = false;
839 }
John Reck30c714c2010-12-16 17:30:34 -0800840 mWebViewController.onProgressChanged(Tab.this);
Michael Kolb72864272012-05-03 15:42:15 -0700841 if (mUpdateThumbnail && newProgress == 100) {
842 mUpdateThumbnail = false;
843 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700844 }
845
846 @Override
Leon Scroggins21d9b902010-03-11 09:33:11 -0500847 public void onReceivedTitle(WebView view, final String title) {
John Reck30c714c2010-12-16 17:30:34 -0800848 mCurrentState.mTitle = title;
Michael Kolb8233fac2010-10-26 16:08:53 -0700849 mWebViewController.onReceivedTitle(Tab.this, title);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700850 }
851
852 @Override
853 public void onReceivedIcon(WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -0800854 mCurrentState.mFavicon = icon;
Michael Kolb8233fac2010-10-26 16:08:53 -0700855 mWebViewController.onFavicon(Tab.this, view, icon);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700856 }
857
858 @Override
859 public void onReceivedTouchIconUrl(WebView view, String url,
860 boolean precomposed) {
Michael Kolb14612442011-06-24 13:06:29 -0700861 final ContentResolver cr = mContext.getContentResolver();
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400862 // Let precomposed icons take precedence over non-composed
863 // icons.
864 if (precomposed && mTouchIconLoader != null) {
865 mTouchIconLoader.cancel(false);
866 mTouchIconLoader = null;
867 }
868 // Have only one async task at a time.
869 if (mTouchIconLoader == null) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700870 mTouchIconLoader = new DownloadTouchIcon(Tab.this,
Michael Kolb14612442011-06-24 13:06:29 -0700871 mContext, cr, view);
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400872 mTouchIconLoader.execute(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700873 }
Pankaj Garg21dad562015-07-02 17:17:24 -0700874 mTouchIconUrl = url;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700875 }
876
877 @Override
878 public void onShowCustomView(View view,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800879 CustomViewCallback callback) {
Michael Kolb14612442011-06-24 13:06:29 -0700880 Activity activity = mWebViewController.getActivity();
881 if (activity != null) {
882 onShowCustomView(view, activity.getRequestedOrientation(), callback);
883 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400884 }
885
886 @Override
887 public void onShowCustomView(View view, int requestedOrientation,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800888 CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700889 if (mInForeground) mWebViewController.showCustomView(Tab.this, view,
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400890 requestedOrientation, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700891 }
892
893 @Override
894 public void onHideCustomView() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700895 if (mInForeground) mWebViewController.hideCustomView();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700896 }
897
898 /**
899 * The origin has exceeded its database quota.
900 * @param url the URL that exceeded the quota
901 * @param databaseIdentifier the identifier of the database on which the
902 * transaction that caused the quota overflow was run
903 * @param currentQuota the current quota for the origin.
904 * @param estimatedSize the estimated size of the database.
905 * @param totalUsedQuota is the sum of all origins' quota.
906 * @param quotaUpdater The callback to run when a decision to allow or
907 * deny quota has been made. Don't forget to call this!
908 */
909 @Override
910 public void onExceededDatabaseQuota(String url,
911 String databaseIdentifier, long currentQuota, long estimatedSize,
912 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -0700913 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -0700914 .onExceededDatabaseQuota(url, databaseIdentifier,
915 currentQuota, estimatedSize, totalUsedQuota,
916 quotaUpdater);
917 }
918
919 /**
920 * The Application Cache has exceeded its max size.
921 * @param spaceNeeded is the amount of disk space that would be needed
922 * in order for the last appcache operation to succeed.
923 * @param totalUsedQuota is the sum of all origins' quota.
924 * @param quotaUpdater A callback to inform the WebCore thread that a
925 * new app cache size is available. This callback must always
926 * be executed at some point to ensure that the sleeping
927 * WebCore thread is woken up.
928 */
929 @Override
930 public void onReachedMaxAppCacheSize(long spaceNeeded,
931 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -0700932 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -0700933 .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota,
934 quotaUpdater);
935 }
936
Ben Murdoch65acc352009-11-19 18:16:04 +0000937 /* Adds a JavaScript error message to the system log and if the JS
938 * console is enabled in the about:debug options, to that console
939 * also.
Ben Murdochc42addf2010-01-28 15:19:59 +0000940 * @param consoleMessage the message object.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700941 */
942 @Override
Ben Murdochc42addf2010-01-28 15:19:59 +0000943 public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Jeff Hamilton47654f42010-09-07 09:57:51 -0500944 // Don't log console messages in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -0700945 if (isPrivateBrowsingEnabled()) return true;
Jeff Hamilton47654f42010-09-07 09:57:51 -0500946
Ben Murdochc42addf2010-01-28 15:19:59 +0000947 String message = "Console: " + consoleMessage.message() + " "
948 + consoleMessage.sourceId() + ":"
949 + consoleMessage.lineNumber();
950
951 switch (consoleMessage.messageLevel()) {
952 case TIP:
953 Log.v(CONSOLE_LOGTAG, message);
954 break;
955 case LOG:
956 Log.i(CONSOLE_LOGTAG, message);
957 break;
958 case WARNING:
959 Log.w(CONSOLE_LOGTAG, message);
960 break;
961 case ERROR:
962 Log.e(CONSOLE_LOGTAG, message);
963 break;
964 case DEBUG:
965 Log.d(CONSOLE_LOGTAG, message);
966 break;
967 }
968
969 return true;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700970 }
971
972 /**
973 * Ask the browser for an icon to represent a <video> element.
974 * This icon will be used if the Web page did not specify a poster attribute.
975 * @return Bitmap The icon or null if no such icon is available.
976 */
977 @Override
978 public Bitmap getDefaultVideoPoster() {
979 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700980 return mWebViewController.getDefaultVideoPoster();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700981 }
982 return null;
983 }
984
985 /**
986 * Ask the host application for a custom progress view to show while
987 * a <video> is loading.
988 * @return View The progress view.
989 */
990 @Override
991 public View getVideoLoadingProgressView() {
992 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700993 return mWebViewController.getVideoLoadingProgressView();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700994 }
995 return null;
996 }
997
998 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +0000999 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001000 if (mInForeground) {
Ben Murdoch8cad4132012-01-11 10:56:43 +00001001 mWebViewController.openFileChooser(uploadMsg, acceptType, capture);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001002 } else {
1003 uploadMsg.onReceiveValue(null);
1004 }
1005 }
1006
Vivek Sekharb54614f2014-05-01 19:03:37 -07001007 @Override
1008 public void showFileChooser(ValueCallback<String[]> uploadFilePaths, String acceptTypes,
1009 boolean capture) {
1010 if (mInForeground) {
1011 mWebViewController.showFileChooser(uploadFilePaths, acceptTypes, capture);
1012 } else {
1013 uploadFilePaths.onReceiveValue(null);
1014 }
1015 }
1016
Grace Kloba22ac16e2009-10-07 18:00:23 -07001017 /**
1018 * Deliver a list of already-visited URLs
1019 */
1020 @Override
1021 public void getVisitedHistory(final ValueCallback<String[]> callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001022 mWebViewController.getVisitedHistory(callback);
1023 }
Ben Murdoch8029a772010-11-16 11:58:21 +00001024
1025 @Override
1026 public void setupAutoFill(Message message) {
1027 // Prompt the user to set up their profile.
1028 final Message msg = message;
Michael Kolb14612442011-06-24 13:06:29 -07001029 AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
1030 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
Ben Murdoch1d676b62011-01-17 12:54:24 +00001031 Context.LAYOUT_INFLATER_SERVICE);
1032 final View layout = inflater.inflate(R.layout.setup_autofill_dialog, null);
1033
1034 builder.setView(layout)
1035 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1036 @Override
1037 public void onClick(DialogInterface dialog, int id) {
1038 CheckBox disableAutoFill = (CheckBox) layout.findViewById(
1039 R.id.setup_autofill_dialog_disable_autofill);
1040
1041 if (disableAutoFill.isChecked()) {
1042 // Disable autofill and show a toast with how to turn it on again.
John Reck35e9dd62011-04-25 09:01:54 -07001043 mSettings.setAutofillEnabled(false);
Michael Kolb14612442011-06-24 13:06:29 -07001044 Toast.makeText(mContext,
Ben Murdoch1d676b62011-01-17 12:54:24 +00001045 R.string.autofill_setup_dialog_negative_toast,
1046 Toast.LENGTH_LONG).show();
1047 } else {
1048 // Take user to the AutoFill profile editor. When they return,
1049 // we will send the message that we pass here which will trigger
1050 // the form to get filled out with their new profile.
1051 mWebViewController.setupAutoFill(msg);
1052 }
1053 }
1054 })
1055 .setNegativeButton(R.string.cancel, null)
1056 .show();
Ben Murdoch8029a772010-11-16 11:58:21 +00001057 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001058 };
1059
1060 // -------------------------------------------------------------------------
1061 // WebViewClient implementation for the sub window
1062 // -------------------------------------------------------------------------
1063
1064 // Subclass of WebViewClient used in subwindows to notify the main
1065 // WebViewClient of certain WebView activities.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001066 private static class SubWindowClient extends WebViewClient {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001067 // The main WebViewClient.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001068 private final WebViewClient mClient;
Michael Kolb8233fac2010-10-26 16:08:53 -07001069 private final WebViewController mController;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001070
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001071 SubWindowClient(WebViewClient client, WebViewController controller) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001072 mClient = client;
Michael Kolb8233fac2010-10-26 16:08:53 -07001073 mController = controller;
Leon Scroggins III211ba542010-04-19 13:21:13 -04001074 }
1075 @Override
1076 public void onPageStarted(WebView view, String url, Bitmap favicon) {
1077 // Unlike the others, do not call mClient's version, which would
1078 // change the progress bar. However, we do want to remove the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001079 // find or select dialog.
Michael Kolb8233fac2010-10-26 16:08:53 -07001080 mController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001081 }
1082 @Override
1083 public void doUpdateVisitedHistory(WebView view, String url,
1084 boolean isReload) {
1085 mClient.doUpdateVisitedHistory(view, url, isReload);
1086 }
1087 @Override
1088 public boolean shouldOverrideUrlLoading(WebView view, String url) {
1089 return mClient.shouldOverrideUrlLoading(view, url);
1090 }
1091 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -07001092 public void onReceivedHttpAuthRequest(WebView view,
1093 HttpAuthHandler handler, String host, String realm) {
1094 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
1095 }
1096 @Override
1097 public void onFormResubmission(WebView view, Message dontResend,
1098 Message resend) {
1099 mClient.onFormResubmission(view, dontResend, resend);
1100 }
1101 @Override
1102 public void onReceivedError(WebView view, int errorCode,
1103 String description, String failingUrl) {
1104 mClient.onReceivedError(view, errorCode, description, failingUrl);
1105 }
1106 @Override
1107 public boolean shouldOverrideKeyEvent(WebView view,
1108 android.view.KeyEvent event) {
1109 return mClient.shouldOverrideKeyEvent(view, event);
1110 }
1111 @Override
1112 public void onUnhandledKeyEvent(WebView view,
1113 android.view.KeyEvent event) {
1114 mClient.onUnhandledKeyEvent(view, event);
1115 }
1116 }
1117
1118 // -------------------------------------------------------------------------
1119 // WebChromeClient implementation for the sub window
1120 // -------------------------------------------------------------------------
1121
1122 private class SubWindowChromeClient extends WebChromeClient {
1123 // The main WebChromeClient.
1124 private final WebChromeClient mClient;
1125
1126 SubWindowChromeClient(WebChromeClient client) {
1127 mClient = client;
1128 }
1129 @Override
1130 public void onProgressChanged(WebView view, int newProgress) {
1131 mClient.onProgressChanged(view, newProgress);
1132 }
1133 @Override
1134 public boolean onCreateWindow(WebView view, boolean dialog,
1135 boolean userGesture, android.os.Message resultMsg) {
1136 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
1137 }
1138 @Override
1139 public void onCloseWindow(WebView window) {
1140 if (window != mSubView) {
1141 Log.e(LOGTAG, "Can't close the window");
1142 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001143 mWebViewController.dismissSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001144 }
1145 }
1146
1147 // -------------------------------------------------------------------------
1148
1149 // Construct a new tab
Michael Kolb7bcafde2011-05-09 13:55:59 -07001150 Tab(WebViewController wvcontroller, WebView w) {
John Reck1cf4b792011-07-26 10:22:22 -07001151 this(wvcontroller, w, null);
1152 }
1153
1154 Tab(WebViewController wvcontroller, Bundle state) {
1155 this(wvcontroller, null, state);
1156 }
1157
1158 Tab(WebViewController wvcontroller, WebView w, Bundle state) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001159 mWebViewController = wvcontroller;
Michael Kolb14612442011-06-24 13:06:29 -07001160 mContext = mWebViewController.getContext();
John Reck35e9dd62011-04-25 09:01:54 -07001161 mSettings = BrowserSettings.getInstance();
Michael Kolb14612442011-06-24 13:06:29 -07001162 mDataController = DataController.getInstance(mContext);
1163 mCurrentState = new PageState(mContext, w != null
John Reck847b5322011-04-14 17:02:18 -07001164 ? w.isPrivateBrowsingEnabled() : false);
Tarun Nainani8084c822014-06-25 13:38:06 -07001165 setTimeStamp();
Michael Kolb8233fac2010-10-26 16:08:53 -07001166 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001167 mInForeground = false;
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001168 mWebViewDestroyedByMemoryMonitor = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001169
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001170 mDownloadListener = new BrowserDownloadListener() {
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001171 public void onDownloadStart(String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001172 String contentDisposition, String mimetype, String referer,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001173 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001174 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001175 mimetype, referer, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001176 }
1177 };
1178
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001179 mCaptureWidth = mContext.getResources().getDimensionPixelSize(R.dimen.tab_thumbnail_width);
1180 mCaptureHeight =mContext.getResources().getDimensionPixelSize(R.dimen.tab_thumbnail_height);
1181
1182 initCaptureBitmap();
1183
John Reck1cf4b792011-07-26 10:22:22 -07001184 restoreState(state);
John Reck52be4782011-08-26 15:37:29 -07001185 if (getId() == -1) {
1186 mId = TabControl.getNextId();
1187 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001188 setWebView(w);
Michael Kolb9ef259a2011-07-12 15:33:08 -07001189 mHandler = new Handler() {
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001190 @Override
Michael Kolb9ef259a2011-07-12 15:33:08 -07001191 public void handleMessage(Message m) {
John Reck1cf4b792011-07-26 10:22:22 -07001192 switch (m.what) {
1193 case MSG_CAPTURE:
1194 capture();
1195 break;
1196 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001197 }
1198 };
Pankaj Garg1c13cab2015-05-12 11:52:17 -07001199
1200 mFirstPixelObservable = new Observable();
1201 mFirstPixelObservable.set(false);
1202 mTabHistoryUpdateObservable = new Observable();
John Reck1cf4b792011-07-26 10:22:22 -07001203 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001204
Pankaj Gargb4b4f012015-08-04 16:19:14 -07001205 public void initCaptureBitmap() {
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001206 mCapture = Bitmap.createBitmap(mCaptureWidth, mCaptureHeight, Bitmap.Config.RGB_565);
1207 mCapture.eraseColor(Color.WHITE);
Michael Kolb72864272012-05-03 15:42:15 -07001208 }
1209
Mathew Inwoode09305e2011-09-02 12:03:26 +01001210 /**
1211 * This is used to get a new ID when the tab has been preloaded, before it is displayed and
1212 * added to TabControl. Preloaded tabs can be created before restoreInstanceState, leading
1213 * to overlapping IDs between the preloaded and restored tabs.
1214 */
1215 public void refreshIdAfterPreload() {
1216 mId = TabControl.getNextId();
1217 }
1218
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001219 public void setController(WebViewController ctl) {
1220 mWebViewController = ctl;
1221
John Reck1cf4b792011-07-26 10:22:22 -07001222 if (mWebViewController.shouldCaptureThumbnails()) {
1223 synchronized (Tab.this) {
1224 if (mCapture == null) {
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001225 initCaptureBitmap();
1226 if (mInForeground && !mHandler.hasMessages(MSG_CAPTURE)) {
1227 mHandler.sendEmptyMessageDelayed(MSG_CAPTURE, CAPTURE_DELAY);
John Reck1cf4b792011-07-26 10:22:22 -07001228 }
1229 }
1230 }
1231 } else {
1232 synchronized (Tab.this) {
1233 mCapture = null;
1234 deleteThumbnail();
1235 }
1236 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001237 }
1238
Michael Kolbc831b632011-05-11 09:30:34 -07001239 public long getId() {
1240 return mId;
1241 }
1242
Michael Kolb91911a22012-01-17 11:21:25 -08001243 void setWebView(WebView w) {
1244 setWebView(w, true);
1245 }
1246
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001247 public boolean isNativeActive(){
1248 if (mMainView == null)
1249 return false;
1250 return true;
1251 }
1252
1253 public void setTimeStamp(){
1254 Date d = new Date();
1255 timestamp = (new Timestamp(d.getTime()));
1256 }
1257
1258 public Timestamp getTimestamp() {
1259 return timestamp;
1260 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001261 /**
1262 * Sets the WebView for this tab, correctly removing the old WebView from
1263 * the container view.
1264 */
Michael Kolb91911a22012-01-17 11:21:25 -08001265 void setWebView(WebView w, boolean restore) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001266 if (mMainView == w) {
1267 return;
1268 }
Michael Kolba713ec82010-11-29 17:27:06 -08001269
Michael Kolba713ec82010-11-29 17:27:06 -08001270 mWebViewController.onSetWebView(this, w);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001271
John Reck1cf4b792011-07-26 10:22:22 -07001272 if (mMainView != null) {
John Reck8ee633f2011-08-09 16:00:35 -07001273 mMainView.setPictureListener(null);
John Reck1cf4b792011-07-26 10:22:22 -07001274 if (w != null) {
1275 syncCurrentState(w, null);
1276 } else {
Panos Thomasa9a5a582014-03-18 19:20:08 -07001277 mCurrentState = new PageState(mContext, mMainView.isPrivateBrowsingEnabled());
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001278
1279 if (mWebViewDestroyedByMemoryMonitor) {
1280 /*
1281 * If tab was destroyed as a result of the MemoryMonitor
1282 * then we need to restore the state properties
1283 * from the old WebView (mMainView)
1284 */
1285 syncCurrentState(mMainView, null);
1286 mWebViewDestroyedByMemoryMonitor = false;
1287 }
John Reck1cf4b792011-07-26 10:22:22 -07001288 }
1289 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001290 // set the new one
1291 mMainView = w;
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001292
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001293 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001294 if (mMainView != null) {
1295 mMainView.setWebViewClient(mWebViewClient);
1296 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001297 // Attach DownloadManager so that downloads can start in an active
1298 // or a non-active window. This can happen when going to a site that
1299 // does a redirect after a period of time. The user could have
1300 // switched to another tab while waiting for the download to start.
1301 mMainView.setDownloadListener(mDownloadListener);
John Reck8ee633f2011-08-09 16:00:35 -07001302 TabControl tc = mWebViewController.getTabControl();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001303 if (tc != null /*&& tc.getOnThumbnailUpdatedListener() != null*/) {
John Reck8ee633f2011-08-09 16:00:35 -07001304 mMainView.setPictureListener(this);
1305 }
Michael Kolb91911a22012-01-17 11:21:25 -08001306 if (restore && (mSavedState != null)) {
John Reck8b9bb8b2012-03-08 13:19:40 -08001307 restoreUserAgent();
John Reck6c2e2f32011-08-22 13:41:23 -07001308 WebBackForwardList restoredState
1309 = mMainView.restoreState(mSavedState);
1310 if (restoredState == null || restoredState.getSize() == 0) {
1311 Log.w(LOGTAG, "Failed to restore WebView state!");
1312 loadUrl(mCurrentState.mOriginalUrl, null);
1313 }
John Reck1cf4b792011-07-26 10:22:22 -07001314 mSavedState = null;
1315 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001316 }
1317 }
1318
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001319 public void destroyThroughMemoryMonitor() {
1320 mWebViewDestroyedByMemoryMonitor = true;
1321 destroy();
1322 }
1323
Grace Kloba22ac16e2009-10-07 18:00:23 -07001324 /**
1325 * Destroy the tab's main WebView and subWindow if any
1326 */
1327 void destroy() {
Tarun Nainani2c1dd7c2014-07-05 16:40:12 -07001328
Matthew Huib7f2e9c2014-04-16 11:12:37 -04001329 if (mPostponeDestroy) {
1330 mShouldDestroy = true;
1331 return;
1332 }
1333 mShouldDestroy = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001334 if (mMainView != null) {
1335 dismissSubWindow();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001336 // save the WebView to call destroy() after detach it from the tab
Vivek Sekhar53ef8932015-06-18 16:51:43 -07001337 final WebView webView = mMainView;
Vivek Sekhar2ee19a32015-07-02 17:03:57 -07001338 setWebView(null);
Site Mao61b68212015-07-16 10:56:31 -07001339 if (!mWebViewDestroyedByMemoryMonitor && !BaseUi.isUiLowPowerMode()) {
Vivek Sekhar2ee19a32015-07-02 17:03:57 -07001340 // Tabs can be reused with new instance of WebView so delete the snapshots
Vivek Sekhar53ef8932015-06-18 16:51:43 -07001341 webView.getSnapshotIds(new ValueCallback<List<Integer>>() {
1342 @Override
1343 public void onReceiveValue(List<Integer> ids) {
1344 int currentTabIdx = mWebViewController.getTabControl().getCurrentPosition();
1345 for (Integer id : ids) {
1346 if (getTabIdxFromCaptureIdx(id) == currentTabIdx) {
1347 webView.deleteSnapshot(id);
1348 }
1349 }
Vivek Sekhar53ef8932015-06-18 16:51:43 -07001350 webView.destroy();
Pankaj Garg1c13cab2015-05-12 11:52:17 -07001351 }
Vivek Sekhar53ef8932015-06-18 16:51:43 -07001352 });
Vivek Sekhar2ee19a32015-07-02 17:03:57 -07001353 } else {
1354 webView.destroy();
Pankaj Garg1c13cab2015-05-12 11:52:17 -07001355 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001356 }
1357 }
1358
Matthew Huib7f2e9c2014-04-16 11:12:37 -04001359 private boolean mPostponeDestroy = false;
1360 private boolean mShouldDestroy = false;
1361
1362 public void postponeDestroy() {
1363 mPostponeDestroy = true;
1364 }
1365
1366 public void performPostponedDestroy() {
1367 mPostponeDestroy = false;
1368 if (mShouldDestroy) {
1369 destroy();
1370 }
1371 }
1372
Grace Kloba22ac16e2009-10-07 18:00:23 -07001373 /**
1374 * Remove the tab from the parent
1375 */
1376 void removeFromTree() {
1377 // detach the children
Michael Kolbc831b632011-05-11 09:30:34 -07001378 if (mChildren != null) {
1379 for(Tab t : mChildren) {
1380 t.setParent(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001381 }
1382 }
1383 // remove itself from the parent list
Michael Kolbc831b632011-05-11 09:30:34 -07001384 if (mParent != null) {
1385 mParent.mChildren.remove(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001386 }
Tarun Nainani8eb00912014-07-17 12:28:32 -07001387
1388 mCapture = null;
John Reck1cf4b792011-07-26 10:22:22 -07001389 deleteThumbnail();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001390 }
1391
1392 /**
1393 * Create a new subwindow unless a subwindow already exists.
1394 * @return True if a new subwindow was created. False if one already exists.
1395 */
1396 boolean createSubWindow() {
1397 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001398 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001399 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001400 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001401 mSubView.setWebChromeClient(new SubWindowChromeClient(
1402 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001403 // Set a different DownloadListener for the mSubView, since it will
1404 // just need to dismiss the mSubView, rather than close the Tab
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001405 mSubView.setDownloadListener(new BrowserDownloadListener() {
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001406 public void onDownloadStart(String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001407 String contentDisposition, String mimetype, String referer,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001408 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001409 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001410 contentDisposition, mimetype, referer, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001411 if (mSubView.copyBackForwardList().getSize() == 0) {
1412 // This subwindow was opened for the sole purpose of
1413 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001414 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001415 }
1416 }
1417 });
Michael Kolb14612442011-06-24 13:06:29 -07001418 mSubView.setOnCreateContextMenuListener(mWebViewController.getActivity());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001419 return true;
1420 }
1421 return false;
1422 }
1423
1424 /**
1425 * Dismiss the subWindow for the tab.
1426 */
1427 void dismissSubWindow() {
1428 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001429 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001430 mSubView.destroy();
1431 mSubView = null;
1432 mSubViewContainer = null;
1433 }
1434 }
1435
Grace Kloba22ac16e2009-10-07 18:00:23 -07001436
1437 /**
1438 * Set the parent tab of this tab.
1439 */
Michael Kolbc831b632011-05-11 09:30:34 -07001440 void setParent(Tab parent) {
John Reck52be4782011-08-26 15:37:29 -07001441 if (parent == this) {
1442 throw new IllegalStateException("Cannot set parent to self!");
1443 }
Michael Kolbc831b632011-05-11 09:30:34 -07001444 mParent = parent;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001445 // This tab may have been freed due to low memory. If that is the case,
Michael Kolbc831b632011-05-11 09:30:34 -07001446 // the parent tab id is already saved. If we are changing that id
Grace Kloba22ac16e2009-10-07 18:00:23 -07001447 // (most likely due to removing the parent tab) we must update the
Michael Kolbc831b632011-05-11 09:30:34 -07001448 // parent tab id in the saved Bundle.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001449 if (mSavedState != null) {
1450 if (parent == null) {
1451 mSavedState.remove(PARENTTAB);
1452 } else {
Michael Kolbc831b632011-05-11 09:30:34 -07001453 mSavedState.putLong(PARENTTAB, parent.getId());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001454 }
1455 }
John Reckb0a86db2011-05-24 14:05:58 -07001456
1457 // Sync the WebView useragent with the parent
1458 if (parent != null && mSettings.hasDesktopUseragent(parent.getWebView())
1459 != mSettings.hasDesktopUseragent(getWebView())) {
1460 mSettings.toggleDesktopUseragent(getWebView());
1461 }
John Reck52be4782011-08-26 15:37:29 -07001462
1463 if (parent != null && parent.getId() == getId()) {
1464 throw new IllegalStateException("Parent has same ID as child!");
1465 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001466 }
1467
1468 /**
Michael Kolbc831b632011-05-11 09:30:34 -07001469 * If this Tab was created through another Tab, then this method returns
1470 * that Tab.
1471 * @return the Tab parent or null
1472 */
1473 public Tab getParent() {
1474 return mParent;
1475 }
1476
1477 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001478 * When a Tab is created through the content of another Tab, then we
1479 * associate the Tabs.
1480 * @param child the Tab that was created from this Tab
1481 */
1482 void addChildTab(Tab child) {
Michael Kolbc831b632011-05-11 09:30:34 -07001483 if (mChildren == null) {
1484 mChildren = new Vector<Tab>();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001485 }
Michael Kolbc831b632011-05-11 09:30:34 -07001486 mChildren.add(child);
1487 child.setParent(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001488 }
1489
Michael Kolbc831b632011-05-11 09:30:34 -07001490 Vector<Tab> getChildren() {
1491 return mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001492 }
1493
1494 void resume() {
1495 if (mMainView != null) {
John Reck56c1fcf2011-08-17 10:15:16 -07001496 setupHwAcceleration(mMainView);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001497 mMainView.onResume();
1498 if (mSubView != null) {
1499 mSubView.onResume();
1500 }
1501 }
1502 }
1503
John Reck56c1fcf2011-08-17 10:15:16 -07001504 private void setupHwAcceleration(View web) {
1505 if (web == null) return;
1506 BrowserSettings settings = BrowserSettings.getInstance();
1507 if (settings.isHardwareAccelerated()) {
1508 web.setLayerType(View.LAYER_TYPE_NONE, null);
1509 } else {
1510 web.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
1511 }
1512 }
1513
Grace Kloba22ac16e2009-10-07 18:00:23 -07001514 void pause() {
1515 if (mMainView != null) {
1516 mMainView.onPause();
1517 if (mSubView != null) {
1518 mSubView.onPause();
1519 }
1520 }
1521 }
1522
1523 void putInForeground() {
John Reck8ee633f2011-08-09 16:00:35 -07001524 if (mInForeground) {
1525 return;
1526 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001527 mInForeground = true;
1528 resume();
Michael Kolb14612442011-06-24 13:06:29 -07001529 Activity activity = mWebViewController.getActivity();
1530 mMainView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001531 if (mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -07001532 mSubView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001533 }
Axesh R. Ajmerac6b5c322015-05-01 11:06:10 -07001534
Leon Scroggins1961ed22010-12-07 15:22:21 -05001535 mWebViewController.bookmarkedStatusHasChanged(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001536 }
1537
1538 void putInBackground() {
John Reck8ee633f2011-08-09 16:00:35 -07001539 if (!mInForeground) {
1540 return;
1541 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001542 mInForeground = false;
1543 pause();
1544 mMainView.setOnCreateContextMenuListener(null);
1545 if (mSubView != null) {
1546 mSubView.setOnCreateContextMenuListener(null);
1547 }
1548 }
1549
Michael Kolb8233fac2010-10-26 16:08:53 -07001550 boolean inForeground() {
1551 return mInForeground;
1552 }
1553
Grace Kloba22ac16e2009-10-07 18:00:23 -07001554 /**
1555 * Return the top window of this tab; either the subwindow if it is not
1556 * null or the main window.
1557 * @return The top window of this tab.
1558 */
1559 WebView getTopWindow() {
1560 if (mSubView != null) {
1561 return mSubView;
1562 }
1563 return mMainView;
1564 }
1565
1566 /**
1567 * Return the main window of this tab. Note: if a tab is freed in the
1568 * background, this can return null. It is only guaranteed to be
1569 * non-null for the current tab.
1570 * @return The main WebView of this tab.
1571 */
1572 WebView getWebView() {
1573 return mMainView;
1574 }
1575
Michael Kolba713ec82010-11-29 17:27:06 -08001576 void setViewContainer(View container) {
1577 mContainer = container;
1578 }
1579
Michael Kolb8233fac2010-10-26 16:08:53 -07001580 View getViewContainer() {
1581 return mContainer;
1582 }
1583
Grace Kloba22ac16e2009-10-07 18:00:23 -07001584 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001585 * Return whether private browsing is enabled for the main window of
1586 * this tab.
1587 * @return True if private browsing is enabled.
1588 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001589 boolean isPrivateBrowsingEnabled() {
John Reck502a3532011-08-16 14:21:46 -07001590 return mCurrentState.mIncognito;
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001591 }
1592
1593 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001594 * Return the subwindow of this tab or null if there is no subwindow.
1595 * @return The subwindow of this tab or null.
1596 */
1597 WebView getSubWebView() {
1598 return mSubView;
1599 }
1600
Michael Kolb1514bb72010-11-22 09:11:48 -08001601 void setSubWebView(WebView subView) {
1602 mSubView = subView;
1603 }
1604
Michael Kolb8233fac2010-10-26 16:08:53 -07001605 View getSubViewContainer() {
1606 return mSubViewContainer;
1607 }
1608
Michael Kolb1514bb72010-11-22 09:11:48 -08001609 void setSubViewContainer(View subViewContainer) {
1610 mSubViewContainer = subViewContainer;
1611 }
1612
Grace Kloba22ac16e2009-10-07 18:00:23 -07001613
1614 /**
1615 * @return The application id string
1616 */
1617 String getAppId() {
1618 return mAppId;
1619 }
1620
1621 /**
1622 * Set the application id string
1623 * @param id
1624 */
1625 void setAppId(String id) {
1626 mAppId = id;
1627 }
1628
Michael Kolbe28b3472011-08-04 16:54:31 -07001629 boolean closeOnBack() {
1630 return mCloseOnBack;
1631 }
1632
1633 void setCloseOnBack(boolean close) {
1634 mCloseOnBack = close;
1635 }
1636
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001637 boolean getDerivedFromIntent() {
1638 return mDerivedFromIntent;
1639 }
1640
1641 void setDerivedFromIntent(boolean derived) {
1642 mDerivedFromIntent = derived;
1643 }
1644
Grace Kloba22ac16e2009-10-07 18:00:23 -07001645 String getUrl() {
John Reck324d4402011-01-11 16:56:42 -08001646 return UrlUtils.filteredUrl(mCurrentState.mUrl);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001647 }
1648
Tarun Nainani8eb00912014-07-17 12:28:32 -07001649
1650 protected void onPageFinished() {
1651 mPageFinished = true;
Ze G Riande2a675c22015-06-03 11:15:24 -07001652 isDistillable();
Tarun Nainani8eb00912014-07-17 12:28:32 -07001653 }
1654
1655 public boolean getPageFinishedStatus() {
1656 return mPageFinished;
1657 }
1658
John Reck49a603c2011-03-03 09:33:05 -08001659 String getOriginalUrl() {
John Reckdb22ec42011-06-29 11:31:24 -07001660 if (mCurrentState.mOriginalUrl == null) {
1661 return getUrl();
John Reck49a603c2011-03-03 09:33:05 -08001662 }
John Reckdb22ec42011-06-29 11:31:24 -07001663 return UrlUtils.filteredUrl(mCurrentState.mOriginalUrl);
John Reck49a603c2011-03-03 09:33:05 -08001664 }
1665
Grace Kloba22ac16e2009-10-07 18:00:23 -07001666 /**
John Reck30c714c2010-12-16 17:30:34 -08001667 * Get the title of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001668 */
1669 String getTitle() {
John Reck30c714c2010-12-16 17:30:34 -08001670 return mCurrentState.mTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001671 }
1672
1673 /**
John Reck30c714c2010-12-16 17:30:34 -08001674 * Get the favicon of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001675 */
1676 Bitmap getFavicon() {
John Reck1cf4b792011-07-26 10:22:22 -07001677 if (mCurrentState.mFavicon != null) {
1678 return mCurrentState.mFavicon;
1679 }
1680 return getDefaultFavicon(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001681 }
1682
Pankaj Garg32e1b942015-06-03 18:13:24 -07001683 public boolean hasFavicon() {
1684 return mCurrentState.mFavicon != null;
1685 }
1686
John Recke969cc52010-12-21 17:24:43 -08001687 public boolean isBookmarkedSite() {
1688 return mCurrentState.mIsBookmarkedSite;
1689 }
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001690
Grace Kloba22ac16e2009-10-07 18:00:23 -07001691 /**
Steve Block08a6f0c2011-10-06 12:12:53 +01001692 * Sets the security state, clears the SSL certificate error and informs
1693 * the controller.
1694 */
Steve Block2466eff2011-10-03 15:33:09 +01001695 private void setSecurityState(SecurityState securityState) {
1696 mCurrentState.mSecurityState = securityState;
1697 mWebViewController.onUpdatedSecurityState(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001698 }
1699
1700 /**
Steve Block2466eff2011-10-03 15:33:09 +01001701 * @return The tab's security state.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001702 */
Steve Block2466eff2011-10-03 15:33:09 +01001703 SecurityState getSecurityState() {
1704 return mCurrentState.mSecurityState;
John Reck30c714c2010-12-16 17:30:34 -08001705 }
1706
1707 int getLoadProgress() {
1708 if (mInPageLoad) {
1709 return mPageLoadProgress;
1710 }
1711 return 100;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001712 }
1713
1714 /**
1715 * @return TRUE if onPageStarted is called while onPageFinished is not
1716 * called yet.
1717 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001718 boolean inPageLoad() {
1719 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001720 }
1721
Grace Kloba22ac16e2009-10-07 18:00:23 -07001722 /**
John Reck1cf4b792011-07-26 10:22:22 -07001723 * @return The Bundle with the tab's state if it can be saved, otherwise null
Grace Kloba22ac16e2009-10-07 18:00:23 -07001724 */
John Reck1cf4b792011-07-26 10:22:22 -07001725 public Bundle saveState() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001726 // If the WebView is null it means we ran low on memory and we already
1727 // stored the saved state in mSavedState.
1728 if (mMainView == null) {
John Reck1cf4b792011-07-26 10:22:22 -07001729 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001730 }
John Reck6c2e2f32011-08-22 13:41:23 -07001731
1732 if (TextUtils.isEmpty(mCurrentState.mUrl)) {
John Reck1cf4b792011-07-26 10:22:22 -07001733 return null;
John Reck24f18262011-06-17 14:47:20 -07001734 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001735
1736 mSavedState = new Bundle();
John Reck6c2e2f32011-08-22 13:41:23 -07001737 WebBackForwardList savedList = mMainView.saveState(mSavedState);
1738 if (savedList == null || savedList.getSize() == 0) {
1739 Log.w(LOGTAG, "Failed to save back/forward list for "
1740 + mCurrentState.mUrl);
1741 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001742
Michael Kolbc831b632011-05-11 09:30:34 -07001743 mSavedState.putLong(ID, mId);
John Reck30c714c2010-12-16 17:30:34 -08001744 mSavedState.putString(CURRURL, mCurrentState.mUrl);
1745 mSavedState.putString(CURRTITLE, mCurrentState.mTitle);
John Reck1cf4b792011-07-26 10:22:22 -07001746 mSavedState.putBoolean(INCOGNITO, mMainView.isPrivateBrowsingEnabled());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001747 if (mAppId != null) {
1748 mSavedState.putString(APPID, mAppId);
1749 }
Michael Kolbe28b3472011-08-04 16:54:31 -07001750 mSavedState.putBoolean(CLOSEFLAG, mCloseOnBack);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001751 // Remember the parent tab so the relationship can be restored.
Michael Kolbc831b632011-05-11 09:30:34 -07001752 if (mParent != null) {
1753 mSavedState.putLong(PARENTTAB, mParent.mId);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001754 }
John Reckb0a86db2011-05-24 14:05:58 -07001755 mSavedState.putBoolean(USERAGENT,
1756 mSettings.hasDesktopUseragent(getWebView()));
John Reck1cf4b792011-07-26 10:22:22 -07001757 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001758 }
1759
1760 /*
1761 * Restore the state of the tab.
1762 */
John Reck1cf4b792011-07-26 10:22:22 -07001763 private void restoreState(Bundle b) {
1764 mSavedState = b;
1765 if (mSavedState == null) {
1766 return;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001767 }
1768 // Restore the internal state even if the WebView fails to restore.
1769 // This will maintain the app id, original url and close-on-exit values.
Michael Kolbc831b632011-05-11 09:30:34 -07001770 mId = b.getLong(ID);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001771 mAppId = b.getString(APPID);
Michael Kolbe28b3472011-08-04 16:54:31 -07001772 mCloseOnBack = b.getBoolean(CLOSEFLAG);
John Reck8b9bb8b2012-03-08 13:19:40 -08001773 restoreUserAgent();
John Reck1cf4b792011-07-26 10:22:22 -07001774 String url = b.getString(CURRURL);
1775 String title = b.getString(CURRTITLE);
1776 boolean incognito = b.getBoolean(INCOGNITO);
1777 mCurrentState = new PageState(mContext, incognito, url, null);
1778 mCurrentState.mTitle = title;
1779 synchronized (Tab.this) {
1780 if (mCapture != null) {
John Reck4eadc342011-10-31 14:04:10 -07001781 DataController.getInstance(mContext).loadThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07001782 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001783 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001784 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04001785
John Reck8b9bb8b2012-03-08 13:19:40 -08001786 private void restoreUserAgent() {
1787 if (mMainView == null || mSavedState == null) {
1788 return;
1789 }
1790 if (mSavedState.getBoolean(USERAGENT)
1791 != mSettings.hasDesktopUseragent(mMainView)) {
1792 mSettings.toggleDesktopUseragent(mMainView);
1793 }
1794 }
1795
Leon Scroggins1961ed22010-12-07 15:22:21 -05001796 public void updateBookmarkedStatus() {
John Recke969cc52010-12-21 17:24:43 -08001797 mDataController.queryBookmarkStatus(getUrl(), mIsBookmarkCallback);
Leon Scroggins1961ed22010-12-07 15:22:21 -05001798 }
1799
John Recke969cc52010-12-21 17:24:43 -08001800 private DataController.OnQueryUrlIsBookmark mIsBookmarkCallback
1801 = new DataController.OnQueryUrlIsBookmark() {
1802 @Override
1803 public void onQueryUrlIsBookmark(String url, boolean isBookmark) {
1804 if (mCurrentState.mUrl.equals(url)) {
1805 mCurrentState.mIsBookmarkedSite = isBookmark;
1806 mWebViewController.bookmarkedStatusHasChanged(Tab.this);
1807 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001808 }
John Recke969cc52010-12-21 17:24:43 -08001809 };
Michael Kolb1acef692011-03-08 14:12:06 -08001810
Michael Kolbeb95db42011-03-03 10:38:40 -08001811 public Bitmap getScreenshot() {
John Reck1cf4b792011-07-26 10:22:22 -07001812 synchronized (Tab.this) {
1813 return mCapture;
1814 }
Michael Kolbeb95db42011-03-03 10:38:40 -08001815 }
1816
John Reck541f55a2011-06-07 16:34:43 -07001817 public boolean isSnapshot() {
John Reck541f55a2011-06-07 16:34:43 -07001818 return false;
1819 }
1820
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001821 private static class SaveCallback implements ValueCallback<String> {
1822 boolean onReceiveValueCalled = false;
1823 private String mPath;
John Reck68234a92012-04-19 15:27:12 -07001824
1825 @Override
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001826 public void onReceiveValue(String path) {
1827 this.onReceiveValueCalled = true;
1828 this.mPath = path;
John Reck68234a92012-04-19 15:27:12 -07001829 synchronized (this) {
1830 notifyAll();
John Reck8cc92352011-07-06 17:41:52 -07001831 }
John Reck541f55a2011-06-07 16:34:43 -07001832 }
John Reck68234a92012-04-19 15:27:12 -07001833
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001834 public String getPath() {
1835 return mPath;
1836 }
John Reck68234a92012-04-19 15:27:12 -07001837 }
1838
1839 /**
1840 * Must be called on the UI thread
1841 */
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001842 public ContentValues createSnapshotValues(Bitmap bm) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001843 WebView web = getWebView();
John Reck68234a92012-04-19 15:27:12 -07001844 if (web == null) return null;
John Reckd8c74522011-06-14 08:45:00 -07001845 ContentValues values = new ContentValues();
1846 values.put(Snapshots.TITLE, mCurrentState.mTitle);
1847 values.put(Snapshots.URL, mCurrentState.mUrl);
John Reck68234a92012-04-19 15:27:12 -07001848 values.put(Snapshots.BACKGROUND, web.getPageBackgroundColor());
John Reck8cc92352011-07-06 17:41:52 -07001849 values.put(Snapshots.DATE_CREATED, System.currentTimeMillis());
1850 values.put(Snapshots.FAVICON, compressBitmap(getFavicon()));
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001851 values.put(Snapshots.THUMBNAIL, compressBitmap(bm));
John Reckd8c74522011-06-14 08:45:00 -07001852 return values;
John Reck541f55a2011-06-07 16:34:43 -07001853 }
1854
John Reck68234a92012-04-19 15:27:12 -07001855 /**
1856 * Probably want to call this on a background thread
1857 */
1858 public boolean saveViewState(ContentValues values) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001859 WebView web = getWebView();
John Reck68234a92012-04-19 15:27:12 -07001860 if (web == null) return false;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001861 String filename = UUID.randomUUID().toString();
John Reck68234a92012-04-19 15:27:12 -07001862 SaveCallback callback = new SaveCallback();
John Reck68234a92012-04-19 15:27:12 -07001863 try {
John Reck68234a92012-04-19 15:27:12 -07001864 synchronized (callback) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001865 web.saveViewState(filename, callback);
1866 callback.wait();
John Reck68234a92012-04-19 15:27:12 -07001867 }
John Reck68234a92012-04-19 15:27:12 -07001868 } catch (Exception e) {
1869 Log.w(LOGTAG, "Failed to save view state", e);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001870 String path = callback.getPath();
1871 if (path != null) {
1872 File file = mContext.getFileStreamPath(path);
1873 if (file.exists() && !file.delete()) {
1874 file.deleteOnExit();
1875 }
John Reck68234a92012-04-19 15:27:12 -07001876 }
1877 return false;
1878 }
Tarun Nainani8eb00912014-07-17 12:28:32 -07001879
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001880 String path = callback.getPath();
Tarun Nainani8eb00912014-07-17 12:28:32 -07001881 // could be that saving of file failed
1882 if (path == null) {
1883 return false;
1884 }
1885
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001886 File savedFile = new File(path);
1887 if (!savedFile.exists()) {
1888 return false;
John Reck68234a92012-04-19 15:27:12 -07001889 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001890 values.put(Snapshots.VIEWSTATE_PATH, path.substring(path.lastIndexOf('/') + 1));
1891 values.put(Snapshots.VIEWSTATE_SIZE, savedFile.length());
John Reck68234a92012-04-19 15:27:12 -07001892 return true;
1893 }
1894
John Reck8cc92352011-07-06 17:41:52 -07001895 public byte[] compressBitmap(Bitmap bitmap) {
1896 if (bitmap == null) {
1897 return null;
1898 }
1899 ByteArrayOutputStream stream = new ByteArrayOutputStream();
1900 bitmap.compress(CompressFormat.PNG, 100, stream);
1901 return stream.toByteArray();
1902 }
1903
John Reck26b18322011-06-21 13:08:58 -07001904 public void loadUrl(String url, Map<String, String> headers) {
1905 if (mMainView != null) {
Michael Kolba53c9892011-10-05 13:31:40 -07001906 mPageLoadProgress = INITIAL_PROGRESS;
Michael Kolb14612442011-06-24 13:06:29 -07001907 mCurrentState = new PageState(mContext, false, url, null);
John Reck26b18322011-06-21 13:08:58 -07001908 mMainView.loadUrl(url, headers);
1909 }
1910 }
1911
John Reck38b39652012-06-05 09:22:59 -07001912 public void disableUrlOverridingForLoad() {
1913 mDisableOverrideUrlLoading = true;
1914 }
1915
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001916 private void thumbnailUpdated() {
John Reck8ee633f2011-08-09 16:00:35 -07001917 mHandler.removeMessages(MSG_CAPTURE);
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001918
John Reck8ee633f2011-08-09 16:00:35 -07001919 TabControl tc = mWebViewController.getTabControl();
1920 if (tc != null) {
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001921 OnThumbnailUpdatedListener updateListener = tc.getOnThumbnailUpdatedListener();
John Reck8ee633f2011-08-09 16:00:35 -07001922 if (updateListener != null) {
1923 updateListener.onThumbnailUpdated(this);
1924 }
1925 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001926 }
1927
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001928 protected void capture() {
1929 if (mMainView == null || mCapture == null || !mMainView.isReady() ||
1930 mMainView.getContentWidth() <= 0 || mMainView.getContentHeight() <= 0 ||
1931 !mFirstVisualPixelPainted || mMainView.isShowingCrashView()) {
1932
1933 initCaptureBitmap();
1934 thumbnailUpdated();
1935 return;
1936 }
1937
1938 mMainView.getContentBitmapAsync((float) mCaptureWidth / mMainView.getWidth(), new Rect(),
1939 new ValueCallback<Bitmap>() {
1940 @Override
1941 public void onReceiveValue(Bitmap bitmap) {
1942 if (mCapture == null) {
1943 initCaptureBitmap();
1944 }
1945
1946 if (bitmap == null) {
1947 thumbnailUpdated();
1948 return;
1949 }
1950
1951 Canvas c = new Canvas(mCapture);
1952 mCapture.eraseColor(Color.WHITE);
1953 c.drawBitmap(bitmap, 0, 0, null);
1954
1955 // manually anti-alias the edges for the tilt
1956 c.drawRect(0, 0, 1, mCapture.getHeight(), sAlphaPaint);
1957 c.drawRect(mCapture.getWidth() - 1, 0, mCapture.getWidth(),
1958 mCapture.getHeight(), sAlphaPaint);
1959 c.drawRect(0, 0, mCapture.getWidth(), 1, sAlphaPaint);
1960 c.drawRect(0, mCapture.getHeight() - 1, mCapture.getWidth(),
1961 mCapture.getHeight(), sAlphaPaint);
1962 c.setBitmap(null);
1963
1964 persistThumbnail();
1965 thumbnailUpdated();
1966 }
1967 }
1968 );
John Reck1cf4b792011-07-26 10:22:22 -07001969 }
1970
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001971 @Override
1972 public void onNewPicture(WebView view, Picture picture) {
Michael Kolb9ef259a2011-07-12 15:33:08 -07001973 }
1974
John Reckef654f12011-07-12 16:42:08 -07001975 public boolean canGoBack() {
1976 return mMainView != null ? mMainView.canGoBack() : false;
1977 }
1978
1979 public boolean canGoForward() {
1980 return mMainView != null ? mMainView.canGoForward() : false;
1981 }
1982
1983 public void goBack() {
1984 if (mMainView != null) {
1985 mMainView.goBack();
1986 }
1987 }
1988
1989 public void goForward() {
1990 if (mMainView != null) {
1991 mMainView.goForward();
1992 }
1993 }
1994
John Reck1cf4b792011-07-26 10:22:22 -07001995 protected void persistThumbnail() {
John Reck4eadc342011-10-31 14:04:10 -07001996 DataController.getInstance(mContext).saveThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07001997 }
1998
1999 protected void deleteThumbnail() {
John Reck4eadc342011-10-31 14:04:10 -07002000 DataController.getInstance(mContext).deleteThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07002001 }
2002
John Reck4eadc342011-10-31 14:04:10 -07002003 void updateCaptureFromBlob(byte[] blob) {
John Reck1cf4b792011-07-26 10:22:22 -07002004 synchronized (Tab.this) {
2005 if (mCapture == null) {
2006 return;
2007 }
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002008 ByteBuffer buffer = ByteBuffer.wrap(blob);
Michael Kolbd837a112011-08-09 14:00:18 -07002009 try {
2010 mCapture.copyPixelsFromBuffer(buffer);
2011 } catch (RuntimeException rex) {
2012 Log.e(LOGTAG, "Load capture has mismatched sizes; buffer: "
2013 + buffer.capacity() + " blob: " + blob.length
2014 + "capture: " + mCapture.getByteCount());
2015 throw rex;
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002016 }
John Reck1cf4b792011-07-26 10:22:22 -07002017 }
2018 }
2019
John Reck52be4782011-08-26 15:37:29 -07002020 @Override
2021 public String toString() {
2022 StringBuilder builder = new StringBuilder(100);
2023 builder.append(mId);
2024 builder.append(") has parent: ");
2025 if (getParent() != null) {
2026 builder.append("true[");
2027 builder.append(getParent().getId());
2028 builder.append("]");
2029 } else {
2030 builder.append("false");
2031 }
2032 builder.append(", incog: ");
2033 builder.append(isPrivateBrowsingEnabled());
2034 if (!isPrivateBrowsingEnabled()) {
2035 builder.append(", title: ");
2036 builder.append(getTitle());
2037 builder.append(", url: ");
2038 builder.append(getUrl());
2039 }
2040 return builder.toString();
2041 }
2042
Ze G Riande2a675c22015-06-03 11:15:24 -07002043 // dertermines if the tab contains a dislled page
2044 public boolean isDistilled() {
2045 if (!BrowserCommandLine.hasSwitch("reader-mode")) {
2046 return false;
2047 }
2048 try {
2049 return DomDistillerUtils.isUrlDistilled(getUrl());
2050 } catch (Exception e) {
2051 return false;
2052 }
2053 }
2054
2055 //determines if the tab contains a distillable page
2056 public boolean isDistillable() {
2057 if (!BrowserCommandLine.hasSwitch("reader-mode")) {
2058 mIsDistillable = false;
2059 return mIsDistillable;
2060 }
2061 final ValueCallback<String> onIsDistillable = new ValueCallback<String>() {
2062 @Override
2063 public void onReceiveValue(String str) {
2064 mIsDistillable = Boolean.parseBoolean(str);
2065 }
2066 };
2067
2068 if (isDistilled()) {
2069 mIsDistillable = true;
2070 return mIsDistillable;
2071 }
2072
2073 try {
2074 DomDistillerUtils.isWebViewDistillable(getWebView(), onIsDistillable);
2075 } catch (Exception e) {
2076 mIsDistillable = false;
2077 }
2078
2079 return mIsDistillable;
2080 }
2081
2082 // Function that sets the mIsDistillable variable
2083 public void setIsDistillable(boolean value) {
2084 if (!BrowserCommandLine.hasSwitch("reader-mode")) {
2085 mIsDistillable = false;
2086 }
2087 mIsDistillable = value;
2088 }
2089
2090 // Function that returns the distilled url of the current url
2091 public String getDistilledUrl() {
2092 if (getUrl() != null) {
2093 return DomDistillerUtils.getDistilledUrl(getUrl());
2094 }
2095 return new String();
2096 }
2097
2098 // function that returns the non-distilled version of the current url
2099 public String getNonDistilledUrl() {
2100 if (getUrl() != null) {
2101 return DomDistillerUtils.getOriginalUrlFromDistilledUrl(getUrl());
2102 }
2103 return new String();
2104 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002105}