blob: f5f72e7eef3ea8c8e3cb1808dfac00dfffb26348 [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;
Pankaj Gargf49e0222015-09-01 12:19:13 -0700185 private Bitmap mViewportCapture;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700186 private Handler mHandler;
Michael Kolb72864272012-05-03 15:42:15 -0700187 private boolean mUpdateThumbnail;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800188 private Timestamp timestamp;
Vivek Sekharf96064b2014-07-28 16:32:34 -0700189 private boolean mFullScreen = false;
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800190 private boolean mReceivedError;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700191
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -0700192 // determine if webview is destroyed to MemoryMonitor
193 private boolean mWebViewDestroyedByMemoryMonitor;
194
Pankaj Garg21dad562015-07-02 17:17:24 -0700195 private String mTouchIconUrl;
196
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700197 private Observable mFirstPixelObservable;
198 private Observable mTabHistoryUpdateObservable;
199
200 Observable getFirstPixelObservable() {
201 return mFirstPixelObservable;
202 }
203
204 Observable getTabHistoryUpdateObservable() {
205 return mTabHistoryUpdateObservable;
206 }
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -0700207
Ze G Riande2a675c22015-06-03 11:15:24 -0700208 // dertermines if the tab contains a disllable page
209 private boolean mIsDistillable = false;
Mathew Inwood1dd8e822011-08-03 14:34:29 +0100210
John Reck1cf4b792011-07-26 10:22:22 -0700211 private static synchronized Bitmap getDefaultFavicon(Context context) {
212 if (sDefaultFavicon == null) {
213 sDefaultFavicon = BitmapFactory.decodeResource(
Enrico Rosd6efa972014-12-02 19:49:59 -0800214 context.getResources(), R.drawable.ic_deco_favicon_normal);
John Reck1cf4b792011-07-26 10:22:22 -0700215 }
216 return sDefaultFavicon;
217 }
Michael Kolbeb95db42011-03-03 10:38:40 -0800218
John Reck30c714c2010-12-16 17:30:34 -0800219 // All the state needed for a page
John Reckd8c74522011-06-14 08:45:00 -0700220 protected static class PageState {
John Reck30c714c2010-12-16 17:30:34 -0800221 String mUrl;
John Reckdb22ec42011-06-29 11:31:24 -0700222 String mOriginalUrl;
John Reck30c714c2010-12-16 17:30:34 -0800223 String mTitle;
Steve Block08a6f0c2011-10-06 12:12:53 +0100224 // This is non-null only when mSecurityState is SECURITY_STATE_BAD_CERTIFICATE.
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -0700225 SecurityState mSecurityState;
226 // This is non-null only when onReceivedIcon is called or SnapshotTab restores it.
John Reck30c714c2010-12-16 17:30:34 -0800227 Bitmap mFavicon;
Steve Block08a6f0c2011-10-06 12:12:53 +0100228 boolean mIsBookmarkedSite;
229 boolean mIncognito;
John Reck30c714c2010-12-16 17:30:34 -0800230
231 PageState(Context c, boolean incognito) {
John Reck502a3532011-08-16 14:21:46 -0700232 mIncognito = incognito;
Axesh R. Ajmerad04cad12015-08-12 12:02:44 -0700233 mOriginalUrl = mUrl = "";
John Reck502a3532011-08-16 14:21:46 -0700234 if (mIncognito) {
John Reck30c714c2010-12-16 17:30:34 -0800235 mTitle = c.getString(R.string.new_incognito_tab);
John Reck30c714c2010-12-16 17:30:34 -0800236 } else {
John Reck30c714c2010-12-16 17:30:34 -0800237 mTitle = c.getString(R.string.new_tab);
John Reck30c714c2010-12-16 17:30:34 -0800238 }
Steve Block2466eff2011-10-03 15:33:09 +0100239 mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
John Reck30c714c2010-12-16 17:30:34 -0800240 }
241
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -0700242 PageState(Context c, boolean incognito, String url) {
John Reck502a3532011-08-16 14:21:46 -0700243 mIncognito = incognito;
Axesh R. Ajmerad04cad12015-08-12 12:02:44 -0700244 if (mIncognito)
245 mOriginalUrl = mUrl = "";
246 else
247 mOriginalUrl = mUrl = url;
Pankaj Garg96d0ccd2015-07-30 16:49:47 -0700248 mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
John Reck30c714c2010-12-16 17:30:34 -0800249 }
John Reck1cf4b792011-07-26 10:22:22 -0700250
Grace Kloba22ac16e2009-10-07 18:00:23 -0700251 }
252
John Reck30c714c2010-12-16 17:30:34 -0800253 // The current/loading page's state
John Reckd8c74522011-06-14 08:45:00 -0700254 protected PageState mCurrentState;
John Reck30c714c2010-12-16 17:30:34 -0800255
Grace Kloba22ac16e2009-10-07 18:00:23 -0700256 // Used for saving and restoring each Tab
Michael Kolbc831b632011-05-11 09:30:34 -0700257 static final String ID = "ID";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700258 static final String CURRURL = "currentUrl";
259 static final String CURRTITLE = "currentTitle";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700260 static final String PARENTTAB = "parentTab";
261 static final String APPID = "appid";
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700262 static final String INCOGNITO = "privateBrowsingEnabled";
John Reckb0a86db2011-05-24 14:05:58 -0700263 static final String USERAGENT = "useragent";
Michael Kolbe28b3472011-08-04 16:54:31 -0700264 static final String CLOSEFLAG = "closeOnBack";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700265
Pankaj Garg18186a92015-03-31 14:59:33 -0700266 public void setNetworkAvailable(boolean networkUp) {
267 if (networkUp && mReceivedError && (mMainView != null)) {
268 mMainView.reload();
269 }
270 }
271
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700272 public boolean isFirstVisualPixelPainted() {
273 return mFirstVisualPixelPainted;
274 }
275
276 public int getCaptureIndex(int navIndex) {
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700277 int orientation = mWebViewController.getActivity().
278 getResources().getConfiguration().orientation;
279
280 int orientationBit = (orientation == Configuration.ORIENTATION_LANDSCAPE) ? 0 : 1;
281
Vivek Sekhard0f60402015-06-05 14:07:11 -0700282 int index = orientationBit << 31 | (((int)mId & 0x7f) << 24) | (navIndex & 0xffffff);
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700283 return index;
284 }
285
286 public int getTabIdxFromCaptureIdx(int index) {
287 return (index & 0x7f000000) >> 24;
288 }
289
290 public int getOrientationFromCaptureIdx(int index) {
291 return ((index & 0x80000000) == 0) ? Configuration.ORIENTATION_LANDSCAPE :
292 Configuration.ORIENTATION_PORTRAIT;
293
294 }
295
296 public int getNavIdxFromCaptureIdx(int index) {
297 return (index & 0xffffff);
298 }
299
Pankaj Garg96d0ccd2015-07-30 16:49:47 -0700300 public static SecurityState getWebViewSecurityState(WebView view) {
301 switch (view.getSecurityLevel()) {
302 case WebView.SecurityLevel.EV_SECURE:
303 case WebView.SecurityLevel.SECURE:
304 return SecurityState.SECURITY_STATE_SECURE;
305 case WebView.SecurityLevel.SECURITY_ERROR:
306 return SecurityState.SECURITY_STATE_BAD_CERTIFICATE;
307 case WebView.SecurityLevel.SECURITY_POLICY_WARNING:
308 case WebView.SecurityLevel.SECURITY_WARNING:
309 return SecurityState.SECURITY_STATE_MIXED;
310 }
311 return SecurityState.SECURITY_STATE_NOT_SECURE;
312 }
313
Grace Kloba22ac16e2009-10-07 18:00:23 -0700314 // -------------------------------------------------------------------------
315 // WebViewClient implementation for the main WebView
316 // -------------------------------------------------------------------------
317
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800318 private final WebViewClient mWebViewClient = new WebViewClient() {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500319 private Message mDontResend;
320 private Message mResend;
Michael Kolb47bd1e42011-09-01 15:25:00 -0700321
322 private boolean providersDiffer(String url, String otherUrl) {
323 Uri uri1 = Uri.parse(url);
324 Uri uri2 = Uri.parse(otherUrl);
325 return !uri1.getEncodedAuthority().equals(uri2.getEncodedAuthority());
326 }
327
Grace Kloba22ac16e2009-10-07 18:00:23 -0700328 @Override
329 public void onPageStarted(WebView view, String url, Bitmap favicon) {
Ze G Riande2a675c22015-06-03 11:15:24 -0700330 setIsDistillable(false);
Michael Kolb8233fac2010-10-26 16:08:53 -0700331 mInPageLoad = true;
Tarun Nainani8eb00912014-07-17 12:28:32 -0700332 mPageFinished = false;
Pankaj Garg79878492015-04-01 14:48:21 -0700333 mFirstVisualPixelPainted = false;
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700334 mFirstPixelObservable.set(false);
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800335 mReceivedError = false;
Michael Kolb72864272012-05-03 15:42:15 -0700336 mUpdateThumbnail = true;
Michael Kolba53c9892011-10-05 13:31:40 -0700337 mPageLoadProgress = INITIAL_PROGRESS;
Michael Kolb14612442011-06-24 13:06:29 -0700338 mCurrentState = new PageState(mContext,
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -0700339 view.isPrivateBrowsingEnabled(), url);
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000340 mLoadStartTime = SystemClock.uptimeMillis();
Pankaj Garg62bc7912015-04-14 16:08:59 -0700341 // Need re-enable FullScreenMode on Page navigation if needed
342 if (BrowserSettings.getInstance().useFullscreen()){
343 Controller controller = (Controller) mWebViewController;
344 BaseUi ui = (BaseUi) controller.getUi();
345 ui.forceDisableFullscreenMode(false);
346 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700347 // If we start a touch icon load and then load a new page, we don't
348 // want to cancel the current touch icon loader. But, we do want to
349 // create a new one when the touch icon url is known.
350 if (mTouchIconLoader != null) {
351 mTouchIconLoader.mTab = null;
352 mTouchIconLoader = null;
353 }
354
Grace Kloba22ac16e2009-10-07 18:00:23 -0700355 // finally update the UI in the activity if it is in the foreground
John Reck324d4402011-01-11 16:56:42 -0800356 mWebViewController.onPageStarted(Tab.this, view, favicon);
Leon Scroggins4cd97792010-12-03 15:31:56 -0500357
John Recke969cc52010-12-21 17:24:43 -0800358 updateBookmarkedStatus();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700359 }
360
361 @Override
362 public void onPageFinished(WebView view, String url) {
John Reck38b39652012-06-05 09:22:59 -0700363 mDisableOverrideUrlLoading = false;
John Reck5b691842010-11-29 11:21:13 -0800364 if (!isPrivateBrowsingEnabled()) {
365 LogTag.logPageFinishedLoading(
366 url, SystemClock.uptimeMillis() - mLoadStartTime);
367 }
John Reck1cf4b792011-07-26 10:22:22 -0700368 syncCurrentState(view, url);
John Reck324d4402011-01-11 16:56:42 -0800369 mWebViewController.onPageFinished(Tab.this);
Pankaj Garg96d0ccd2015-07-30 16:49:47 -0700370 setSecurityState(getWebViewSecurityState(view));
Grace Kloba22ac16e2009-10-07 18:00:23 -0700371 }
372
Pankaj Garg79878492015-04-01 14:48:21 -0700373 @Override
374 public void onFirstVisualPixel(WebView view) {
375 mFirstVisualPixelPainted = true;
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700376 mFirstPixelObservable.set(true);
Pankaj Garg79878492015-04-01 14:48:21 -0700377 }
378
Grace Kloba22ac16e2009-10-07 18:00:23 -0700379 // return true if want to hijack the url to let another app to handle it
380 @Override
381 public boolean shouldOverrideUrlLoading(WebView view, String url) {
John Reck38b39652012-06-05 09:22:59 -0700382 if (!mDisableOverrideUrlLoading && mInForeground) {
Michael Kolb18eb3772010-12-10 14:29:51 -0800383 return mWebViewController.shouldOverrideUrlLoading(Tab.this,
384 view, url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700385 } else {
386 return false;
387 }
388 }
389
Vivek Sekharb991edb2014-12-17 18:18:07 -0800390 @Override
391 public boolean shouldDownloadFavicon(WebView view, String url) {
392 return true;
393 }
394
Grace Kloba22ac16e2009-10-07 18:00:23 -0700395 /**
Steve Block2466eff2011-10-03 15:33:09 +0100396 * Updates the security state. This method is called when we discover
397 * another resource to be loaded for this page (for example,
398 * javascript). While we update the security state, we do not update
399 * the lock icon until we are done loading, as it is slightly more
400 * secure this way.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700401 */
402 @Override
403 public void onLoadResource(WebView view, String url) {
404 if (url != null && url.length() > 0) {
405 // It is only if the page claims to be secure that we may have
Steve Block2466eff2011-10-03 15:33:09 +0100406 // to update the security state:
407 if (mCurrentState.mSecurityState == SecurityState.SECURITY_STATE_SECURE) {
408 // If NOT a 'safe' url, change the state to mixed content!
Grace Kloba22ac16e2009-10-07 18:00:23 -0700409 if (!(URLUtil.isHttpsUrl(url) || URLUtil.isDataUrl(url)
410 || URLUtil.isAboutUrl(url))) {
Steve Block2466eff2011-10-03 15:33:09 +0100411 mCurrentState.mSecurityState = SecurityState.SECURITY_STATE_MIXED;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700412 }
413 }
414 }
415 }
416
417 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700418 * Show a dialog informing the user of the network error reported by
419 * WebCore if it is in the foreground.
420 */
421 @Override
422 public void onReceivedError(WebView view, int errorCode,
423 String description, String failingUrl) {
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800424 // Used for the syncCurrentState to use
425 // the failing url instead of using webview url
426 mReceivedError = true;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700427 }
428
429 /**
430 * Check with the user if it is ok to resend POST data as the page they
431 * are trying to navigate to is the result of a POST.
432 */
433 @Override
434 public void onFormResubmission(WebView view, final Message dontResend,
435 final Message resend) {
436 if (!mInForeground) {
437 dontResend.sendToTarget();
438 return;
439 }
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500440 if (mDontResend != null) {
441 Log.w(LOGTAG, "onFormResubmission should not be called again "
442 + "while dialog is still up");
443 dontResend.sendToTarget();
444 return;
445 }
446 mDontResend = dontResend;
447 mResend = resend;
Michael Kolb14612442011-06-24 13:06:29 -0700448 new AlertDialog.Builder(mContext).setTitle(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700449 R.string.browserFrameFormResubmitLabel).setMessage(
450 R.string.browserFrameFormResubmitMessage)
451 .setPositiveButton(R.string.ok,
452 new DialogInterface.OnClickListener() {
453 public void onClick(DialogInterface dialog,
454 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500455 if (mResend != null) {
456 mResend.sendToTarget();
457 mResend = null;
458 mDontResend = null;
459 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700460 }
461 }).setNegativeButton(R.string.cancel,
462 new DialogInterface.OnClickListener() {
463 public void onClick(DialogInterface dialog,
464 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500465 if (mDontResend != null) {
466 mDontResend.sendToTarget();
467 mResend = null;
468 mDontResend = null;
469 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700470 }
471 }).setOnCancelListener(new OnCancelListener() {
472 public void onCancel(DialogInterface dialog) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500473 if (mDontResend != null) {
474 mDontResend.sendToTarget();
475 mResend = null;
476 mDontResend = null;
477 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700478 }
479 }).show();
480 }
481
482 /**
483 * Insert the url into the visited history database.
484 * @param url The url to be inserted.
485 * @param isReload True if this url is being reloaded.
486 * FIXME: Not sure what to do when reloading the page.
487 */
488 @Override
489 public void doUpdateVisitedHistory(WebView view, String url,
490 boolean isReload) {
John Reck324d4402011-01-11 16:56:42 -0800491 mWebViewController.doUpdateVisitedHistory(Tab.this, isReload);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700492 }
493
494 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700495 * Handles an HTTP authentication request.
496 *
497 * @param handler The authentication handler
498 * @param host The host
499 * @param realm The realm
500 */
501 @Override
502 public void onReceivedHttpAuthRequest(WebView view,
503 final HttpAuthHandler handler, final String host,
504 final String realm) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700505 mWebViewController.onReceivedHttpAuthRequest(Tab.this, view, handler, host, realm);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700506 }
507
508 @Override
John Reck438bf462011-01-12 18:11:46 -0800509 public WebResourceResponse shouldInterceptRequest(WebView view,
510 String url) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800511 //intercept if opening a new incognito tab - show the incognito welcome page
Axesh R. Ajmerad04cad12015-08-12 12:02:44 -0700512
513 // show only incognito content and webview has private
514 // and cannot go back(only supported if explicit from UI )
515 if (view.isPrivateBrowsingEnabled() &&
516 !view.canGoBack() &&
517 url.startsWith(Controller.INCOGNITO_URI)) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800518 Resources resourceHandle = mContext.getResources();
519 InputStream inStream = resourceHandle.openRawResource(
Bijan Amirzada41242f22014-03-21 12:12:18 -0700520 com.android.browser.R.raw.incognito_mode_start_page);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800521 return new WebResourceResponse("text/html", "utf8", inStream);
522 }
kaiyiz6e5b3e02013-08-19 20:02:01 +0800523 WebResourceResponse res;
524 if (MyNavigationUtil.MY_NAVIGATION.equals(url)) {
525 res = MyNavigationProvider.shouldInterceptRequest(mContext, url);
526 } else {
527 res = HomeProvider.shouldInterceptRequest(mContext, url);
528 }
John Reck438bf462011-01-12 18:11:46 -0800529 return res;
530 }
531
532 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700533 public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
534 if (!mInForeground) {
535 return false;
536 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700537 return mWebViewController.shouldOverrideKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700538 }
539
540 @Override
541 public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700542 if (!mInForeground) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700543 return;
544 }
John Reck997b1b72012-04-19 18:08:25 -0700545 if (!mWebViewController.onUnhandledKeyEvent(event)) {
546 super.onUnhandledKeyEvent(view, event);
547 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700548 }
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700549
550 @Override
551 public void beforeNavigation(WebView view, String url) {
Pankaj Garg21dad562015-07-02 17:17:24 -0700552 mTouchIconUrl = null;
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -0700553 TitleBar titleBar = null;
554 Controller controller = (Controller)mWebViewController;
555 UI ui = controller.getUi();
556
Sagar Dhawanfb79bf42015-08-21 12:37:29 -0700557 // Clear the page state
558 mCurrentState = new PageState(mContext,
559 view.isPrivateBrowsingEnabled(), url);
560
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -0700561 if (ui instanceof BaseUi) {
562 titleBar = ((BaseUi)ui).getTitleBar();
563 if (titleBar != null) {
564 NavigationBarBase navBar = titleBar.getNavigationBar();
565 navBar.showCurrentFavicon(Tab.this); // Show the default Favicon while loading a new page
566 }
567 }
568
Site Mao61b68212015-07-16 10:56:31 -0700569 if (BaseUi.isUiLowPowerMode()) {
Pankaj Garg18aa0a12015-06-22 11:06:12 -0700570 return;
571 }
572
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700573 if (isPrivateBrowsingEnabled()) {
574 return;
575 }
576
577 if (!mFirstVisualPixelPainted) {
578 return;
579 }
580
581 final int idx = view.copyBackForwardList().getCurrentIndex();
582 boolean bitmapExists = view.hasSnapshot(idx);
583
584 int progress = 100;
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -0700585 if (titleBar != null) {
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700586 progress = titleBar.getProgressView().getProgressPercent();
587 }
588
589 if (bitmapExists && progress < 85) {
590 return;
591 }
592
593 int index = getCaptureIndex(view.getLastCommittedHistoryIndex());
Sagar Dhawan49f85cf2015-07-10 16:54:20 -0700594 view.captureSnapshot(index, null);
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700595 }
596
597 @Override
598 public void onHistoryItemCommit(WebView view, int index) {
Site Mao61b68212015-07-16 10:56:31 -0700599 if (BaseUi.isUiLowPowerMode()) {
Pankaj Garg18aa0a12015-06-22 11:06:12 -0700600 return;
601 }
602
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -0700603 // prevent snapshot tab from commiting any history
604 if (isSnapshot()) {
605 return;
606 }
607
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700608 mTabHistoryUpdateObservable.set(index);
Vivek Sekhar53ef8932015-06-18 16:51:43 -0700609 final int maxIdx = view.copyBackForwardList().getSize();
610 final WebView wv = view;
Pankaj Garg036365b2015-09-14 11:21:19 -0700611 final int currIdx = index;
612 final int currentTabIdx = (int) mWebViewController.getTabControl().
613 getCurrentTab().getId();
Vivek Sekhar53ef8932015-06-18 16:51:43 -0700614 view.getSnapshotIds(new ValueCallback <List<Integer>>() {
615 @Override
616 public void onReceiveValue(List<Integer> ids) {
Vivek Sekhar53ef8932015-06-18 16:51:43 -0700617 for (Integer id : ids) {
Pankaj Garg036365b2015-09-14 11:21:19 -0700618 int tabIdx = getTabIdxFromCaptureIdx(id);
619 int navIdx = getNavIdxFromCaptureIdx(id);
620 if (tabIdx == currentTabIdx && (navIdx >= maxIdx || navIdx == currIdx)) {
Vivek Sekhar53ef8932015-06-18 16:51:43 -0700621 wv.deleteSnapshot(id);
622 }
623 }
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700624 }
Vivek Sekhar53ef8932015-06-18 16:51:43 -0700625 });
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700626 }
Pankaj Garg62bc7912015-04-14 16:08:59 -0700627
628 @Override
629 public void onKeyboardStateChange(boolean popup) {
Vivek Sekhard4de6162015-07-21 15:01:45 -0700630 boolean keyboardWasShowing = isKeyboardShowing();
Sagar Dhawan33551ff2015-07-08 17:24:44 -0700631 mIsKeyboardUp = popup;
Vivek Sekhard4de6162015-07-21 15:01:45 -0700632 Controller controller = (Controller)mWebViewController;
633 BaseUi ui = (BaseUi) controller.getUi();
634 // lock the title bar
635 if (popup)
636 ui.getTitleBar().showTopControls(true);
637 if (keyboardWasShowing && popup)
638 ui.getTitleBar().enableTopControls(true);
Pankaj Garg62bc7912015-04-14 16:08:59 -0700639 if (BrowserSettings.getInstance().useFullscreen()) {
Pankaj Garg62bc7912015-04-14 16:08:59 -0700640 ui.forceDisableFullscreenMode(popup);
641 }
642 }
Vivek Sekharae3b1792015-08-03 12:26:41 -0700643
644 @Override
645 public void onAttachInterstitialPage(WebView mWebView) {
646 Controller controller = (Controller)mWebViewController;
647 BaseUi ui = (BaseUi) controller.getUi();
648 ui.getTitleBar().showTopControls(false);
649 }
650
651 @Override
652 public void onDetachInterstitialPage(WebView mWebView) {
653 Controller controller = (Controller)mWebViewController;
654 BaseUi ui = (BaseUi) controller.getUi();
655 ui.getTitleBar().enableTopControls(true);
656 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700657 };
658
John Reck1cf4b792011-07-26 10:22:22 -0700659 private void syncCurrentState(WebView view, String url) {
660 // Sync state (in case of stop/timeout)
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800661
Axesh R. Ajmerad04cad12015-08-12 12:02:44 -0700662
663
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800664 if (mReceivedError) {
665 mCurrentState.mUrl = url;
666 mCurrentState.mOriginalUrl = url;
Axesh R. Ajmerad04cad12015-08-12 12:02:44 -0700667 } else if (view.isPrivateBrowsingEnabled() &&
668 !TextUtils.isEmpty(url) &&
669 url.contains(Controller.INCOGNITO_URI)) {
670 mCurrentState.mUrl = mCurrentState.mOriginalUrl = "";
671 }
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -0700672
Axesh R. Ajmerad04cad12015-08-12 12:02:44 -0700673 else {
674 mCurrentState.mUrl = view.getUrl();
675 mCurrentState.mOriginalUrl = view.getOriginalUrl();
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800676 }
677
John Reck1cf4b792011-07-26 10:22:22 -0700678 if (mCurrentState.mUrl == null) {
John Reck8bcafc12011-08-29 16:43:02 -0700679 mCurrentState.mUrl = "";
John Reck1cf4b792011-07-26 10:22:22 -0700680 }
John Reck1cf4b792011-07-26 10:22:22 -0700681 mCurrentState.mTitle = view.getTitle();
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800682
683
John Reck1cf4b792011-07-26 10:22:22 -0700684 if (!URLUtil.isHttpsUrl(mCurrentState.mUrl)) {
685 // In case we stop when loading an HTTPS page from an HTTP page
686 // but before a provisional load occurred
Steve Block2466eff2011-10-03 15:33:09 +0100687 mCurrentState.mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
John Reck1cf4b792011-07-26 10:22:22 -0700688 }
John Reck502a3532011-08-16 14:21:46 -0700689 mCurrentState.mIncognito = view.isPrivateBrowsingEnabled();
John Reck1cf4b792011-07-26 10:22:22 -0700690 }
691
Pankaj Garg21dad562015-07-02 17:17:24 -0700692 public String getTouchIconUrl() {
693 return mTouchIconUrl;
694 }
Tarun Nainani8eb00912014-07-17 12:28:32 -0700695
Sagar Dhawan33551ff2015-07-08 17:24:44 -0700696 public boolean isKeyboardShowing() {
697 Controller controller = (Controller)mWebViewController;
698 return (mIsKeyboardUp || controller.getUi().isEditingUrl());
699 }
700
Tarun Nainani8eb00912014-07-17 12:28:32 -0700701 public boolean isTabFullScreen() {
702 return mFullScreen;
703 }
704
Vivek Sekharf96064b2014-07-28 16:32:34 -0700705 protected void setTabFullscreen(boolean fullScreen) {
Tarun Nainani8eb00912014-07-17 12:28:32 -0700706 Controller controller = (Controller)mWebViewController;
Sudheer Koganti24766882014-10-02 10:58:09 -0700707 controller.getUi().showFullscreen(fullScreen);
Tarun Nainani8eb00912014-07-17 12:28:32 -0700708 mFullScreen = fullScreen;
Vivek Sekharf96064b2014-07-28 16:32:34 -0700709 }
710
Sudheer Koganti24766882014-10-02 10:58:09 -0700711 public boolean exitFullscreen() {
712 if (mFullScreen) {
713 Controller controller = (Controller)mWebViewController;
714 controller.getUi().showFullscreen(false);
715 if (getWebView() != null)
716 getWebView().exitFullscreen();
717 mFullScreen = false;
718 return true;
719 }
720 return false;
721 }
722
723
724
725
Grace Kloba22ac16e2009-10-07 18:00:23 -0700726 // -------------------------------------------------------------------------
727 // WebChromeClient implementation for the main WebView
728 // -------------------------------------------------------------------------
729
730 private final WebChromeClient mWebChromeClient = new WebChromeClient() {
731 // Helper method to create a new tab or sub window.
732 private void createWindow(final boolean dialog, final Message msg) {
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700733 this.createWindow(dialog, msg, null, false);
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700734 }
735
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700736 private void createWindow(final boolean dialog, final Message msg, final String url,
737 final boolean opener_suppressed) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700738 WebView.WebViewTransport transport =
739 (WebView.WebViewTransport) msg.obj;
740 if (dialog) {
741 createSubWindow();
Michael Kolb8233fac2010-10-26 16:08:53 -0700742 mWebViewController.attachSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700743 transport.setWebView(mSubView);
744 } else {
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700745 final Tab newTab = mWebViewController.openTab(url,
John Reck5949c662011-05-27 09:52:29 -0700746 Tab.this, true, true);
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700747 // This is special case for rendering links on a webpage in
748 // a new tab. If opener is suppressed, the WebContents created
749 // by the content layer are not fully initialized. This check
750 // will prevent content layer from overriding WebContents
751 // created by new tab with the uninitialized instance.
752 if (!opener_suppressed) {
753 transport.setWebView(newTab.getWebView());
754 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700755 }
756 msg.sendToTarget();
757 }
758
759 @Override
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700760 public void toggleFullscreenModeForTab(boolean enterFullscreen) {
761 if (mWebViewController instanceof Controller) {
Vivek Sekharf96064b2014-07-28 16:32:34 -0700762 setTabFullscreen(enterFullscreen);
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700763 }
764 }
765
766 @Override
Tarun Nainani8eb00912014-07-17 12:28:32 -0700767 public void onOffsetsForFullscreenChanged(float topControlsOffsetYPix,
768 float contentOffsetYPix,
769 float overdrawBottomHeightPix) {
770 if (mWebViewController instanceof Controller) {
Vivek Sekhar8ee3abb2014-07-14 12:32:05 -0700771 Controller controller = (Controller)mWebViewController;
Tarun Nainani8eb00912014-07-17 12:28:32 -0700772 controller.getUi().translateTitleBar(topControlsOffsetYPix);
Vivek Sekhar11b40062015-06-24 11:49:04 -0700773 // Resize the viewport if top controls is not visible
Vivek Sekhar2ee19a32015-07-02 17:03:57 -0700774 if (mMainView != null &&
775 (topControlsOffsetYPix == 0.0f || contentOffsetYPix == 0.0f))
Vivek Sekhar11b40062015-06-24 11:49:04 -0700776 ((BrowserWebView)mMainView).enableTopControls(
777 (topControlsOffsetYPix == 0.0f) ? true : false);
Vivek Sekhar8ee3abb2014-07-14 12:32:05 -0700778 }
779 }
780
781 @Override
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700782 public boolean isTabFullScreen() {
Vivek Sekharf96064b2014-07-28 16:32:34 -0700783 return mFullScreen;
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700784 }
785
786 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700787 public boolean onCreateWindow(WebView view, final boolean dialog,
788 final boolean userGesture, final Message resultMsg) {
789 // only allow new window or sub window for the foreground case
790 if (!mInForeground) {
791 return false;
792 }
793 // Short-circuit if we can't create any more tabs or sub windows.
794 if (dialog && mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -0700795 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700796 .setTitle(R.string.too_many_subwindows_dialog_title)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200797 .setIconAttribute(android.R.attr.alertDialogIcon)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700798 .setMessage(R.string.too_many_subwindows_dialog_message)
799 .setPositiveButton(R.string.ok, null)
800 .show();
801 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700802 } else if (!mWebViewController.getTabControl().canCreateNewTab()) {
Michael Kolb14612442011-06-24 13:06:29 -0700803 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700804 .setTitle(R.string.too_many_windows_dialog_title)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200805 .setIconAttribute(android.R.attr.alertDialogIcon)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700806 .setMessage(R.string.too_many_windows_dialog_message)
807 .setPositiveButton(R.string.ok, null)
808 .show();
809 return false;
810 }
811
812 // Short-circuit if this was a user gesture.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800813 if (userGesture || !mSettings.blockPopupWindows()) {
Tarun Nainani4f5137d2015-04-16 17:26:18 -0700814 WebView.WebViewTransport transport =
815 (WebView.WebViewTransport) resultMsg.obj;
816 CreateWindowParams windowParams = transport.getCreateWindowParams();
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700817 if (windowParams.mOpenerSuppressed) {
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700818 createWindow(dialog, resultMsg, windowParams.mURL, true);
819 // This is special case for rendering links on a webpage in
820 // a new tab. If opener is suppressed, the WebContents created
821 // by the content layer are not fully initialized. Returning false
822 // will prevent content layer from overriding WebContents
823 // created by new tab with the uninitialized instance.
824 return false;
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700825 }
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700826
827 createWindow(dialog, resultMsg);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700828 return true;
829 }
830
Tarun Nainani4f5137d2015-04-16 17:26:18 -0700831 createWindow(dialog, resultMsg);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700832 return true;
833 }
834
835 @Override
Patrick Scotteb5061b2009-11-18 15:00:30 -0500836 public void onRequestFocus(WebView view) {
837 if (!mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700838 mWebViewController.switchToTab(Tab.this);
Patrick Scotteb5061b2009-11-18 15:00:30 -0500839 }
840 }
841
842 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700843 public void onCloseWindow(WebView window) {
Michael Kolbc831b632011-05-11 09:30:34 -0700844 if (mParent != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700845 // JavaScript can only close popup window.
846 if (mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700847 mWebViewController.switchToTab(mParent);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700848 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700849 mWebViewController.closeTab(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700850 }
851 }
852
853 @Override
854 public void onProgressChanged(WebView view, int newProgress) {
John Reck30c714c2010-12-16 17:30:34 -0800855 mPageLoadProgress = newProgress;
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800856 if (newProgress == 100) {
857 mInPageLoad = false;
858 }
John Reck30c714c2010-12-16 17:30:34 -0800859 mWebViewController.onProgressChanged(Tab.this);
Michael Kolb72864272012-05-03 15:42:15 -0700860 if (mUpdateThumbnail && newProgress == 100) {
861 mUpdateThumbnail = false;
862 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700863 }
864
865 @Override
Leon Scroggins21d9b902010-03-11 09:33:11 -0500866 public void onReceivedTitle(WebView view, final String title) {
John Reck30c714c2010-12-16 17:30:34 -0800867 mCurrentState.mTitle = title;
Michael Kolb8233fac2010-10-26 16:08:53 -0700868 mWebViewController.onReceivedTitle(Tab.this, title);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700869 }
870
871 @Override
872 public void onReceivedIcon(WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -0800873 mCurrentState.mFavicon = icon;
Michael Kolb8233fac2010-10-26 16:08:53 -0700874 mWebViewController.onFavicon(Tab.this, view, icon);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700875 }
876
877 @Override
878 public void onReceivedTouchIconUrl(WebView view, String url,
879 boolean precomposed) {
Michael Kolb14612442011-06-24 13:06:29 -0700880 final ContentResolver cr = mContext.getContentResolver();
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400881 // Let precomposed icons take precedence over non-composed
882 // icons.
883 if (precomposed && mTouchIconLoader != null) {
884 mTouchIconLoader.cancel(false);
885 mTouchIconLoader = null;
886 }
887 // Have only one async task at a time.
888 if (mTouchIconLoader == null) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700889 mTouchIconLoader = new DownloadTouchIcon(Tab.this,
Michael Kolb14612442011-06-24 13:06:29 -0700890 mContext, cr, view);
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400891 mTouchIconLoader.execute(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700892 }
Pankaj Garg21dad562015-07-02 17:17:24 -0700893 mTouchIconUrl = url;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700894 }
895
896 @Override
897 public void onShowCustomView(View view,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800898 CustomViewCallback callback) {
Michael Kolb14612442011-06-24 13:06:29 -0700899 Activity activity = mWebViewController.getActivity();
900 if (activity != null) {
901 onShowCustomView(view, activity.getRequestedOrientation(), callback);
902 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400903 }
904
905 @Override
906 public void onShowCustomView(View view, int requestedOrientation,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800907 CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700908 if (mInForeground) mWebViewController.showCustomView(Tab.this, view,
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400909 requestedOrientation, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700910 }
911
912 @Override
913 public void onHideCustomView() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700914 if (mInForeground) mWebViewController.hideCustomView();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700915 }
916
917 /**
918 * The origin has exceeded its database quota.
919 * @param url the URL that exceeded the quota
920 * @param databaseIdentifier the identifier of the database on which the
921 * transaction that caused the quota overflow was run
922 * @param currentQuota the current quota for the origin.
923 * @param estimatedSize the estimated size of the database.
924 * @param totalUsedQuota is the sum of all origins' quota.
925 * @param quotaUpdater The callback to run when a decision to allow or
926 * deny quota has been made. Don't forget to call this!
927 */
928 @Override
929 public void onExceededDatabaseQuota(String url,
930 String databaseIdentifier, long currentQuota, long estimatedSize,
931 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -0700932 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -0700933 .onExceededDatabaseQuota(url, databaseIdentifier,
934 currentQuota, estimatedSize, totalUsedQuota,
935 quotaUpdater);
936 }
937
938 /**
939 * The Application Cache has exceeded its max size.
940 * @param spaceNeeded is the amount of disk space that would be needed
941 * in order for the last appcache operation to succeed.
942 * @param totalUsedQuota is the sum of all origins' quota.
943 * @param quotaUpdater A callback to inform the WebCore thread that a
944 * new app cache size is available. This callback must always
945 * be executed at some point to ensure that the sleeping
946 * WebCore thread is woken up.
947 */
948 @Override
949 public void onReachedMaxAppCacheSize(long spaceNeeded,
950 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -0700951 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -0700952 .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota,
953 quotaUpdater);
954 }
955
Ben Murdoch65acc352009-11-19 18:16:04 +0000956 /* Adds a JavaScript error message to the system log and if the JS
957 * console is enabled in the about:debug options, to that console
958 * also.
Ben Murdochc42addf2010-01-28 15:19:59 +0000959 * @param consoleMessage the message object.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700960 */
961 @Override
Ben Murdochc42addf2010-01-28 15:19:59 +0000962 public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Jeff Hamilton47654f42010-09-07 09:57:51 -0500963 // Don't log console messages in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -0700964 if (isPrivateBrowsingEnabled()) return true;
Jeff Hamilton47654f42010-09-07 09:57:51 -0500965
Ben Murdochc42addf2010-01-28 15:19:59 +0000966 String message = "Console: " + consoleMessage.message() + " "
967 + consoleMessage.sourceId() + ":"
968 + consoleMessage.lineNumber();
969
970 switch (consoleMessage.messageLevel()) {
971 case TIP:
972 Log.v(CONSOLE_LOGTAG, message);
973 break;
974 case LOG:
975 Log.i(CONSOLE_LOGTAG, message);
976 break;
977 case WARNING:
978 Log.w(CONSOLE_LOGTAG, message);
979 break;
980 case ERROR:
981 Log.e(CONSOLE_LOGTAG, message);
982 break;
983 case DEBUG:
984 Log.d(CONSOLE_LOGTAG, message);
985 break;
986 }
987
988 return true;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700989 }
990
991 /**
992 * Ask the browser for an icon to represent a <video> element.
993 * This icon will be used if the Web page did not specify a poster attribute.
994 * @return Bitmap The icon or null if no such icon is available.
995 */
996 @Override
997 public Bitmap getDefaultVideoPoster() {
998 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700999 return mWebViewController.getDefaultVideoPoster();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001000 }
1001 return null;
1002 }
1003
1004 /**
1005 * Ask the host application for a custom progress view to show while
1006 * a <video> is loading.
1007 * @return View The progress view.
1008 */
1009 @Override
1010 public View getVideoLoadingProgressView() {
1011 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001012 return mWebViewController.getVideoLoadingProgressView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001013 }
1014 return null;
1015 }
1016
1017 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00001018 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001019 if (mInForeground) {
Ben Murdoch8cad4132012-01-11 10:56:43 +00001020 mWebViewController.openFileChooser(uploadMsg, acceptType, capture);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001021 } else {
1022 uploadMsg.onReceiveValue(null);
1023 }
1024 }
1025
Vivek Sekharb54614f2014-05-01 19:03:37 -07001026 @Override
1027 public void showFileChooser(ValueCallback<String[]> uploadFilePaths, String acceptTypes,
1028 boolean capture) {
1029 if (mInForeground) {
1030 mWebViewController.showFileChooser(uploadFilePaths, acceptTypes, capture);
1031 } else {
1032 uploadFilePaths.onReceiveValue(null);
1033 }
1034 }
1035
Grace Kloba22ac16e2009-10-07 18:00:23 -07001036 /**
1037 * Deliver a list of already-visited URLs
1038 */
1039 @Override
1040 public void getVisitedHistory(final ValueCallback<String[]> callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001041 mWebViewController.getVisitedHistory(callback);
1042 }
Ben Murdoch8029a772010-11-16 11:58:21 +00001043
1044 @Override
1045 public void setupAutoFill(Message message) {
1046 // Prompt the user to set up their profile.
1047 final Message msg = message;
Michael Kolb14612442011-06-24 13:06:29 -07001048 AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
1049 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
Ben Murdoch1d676b62011-01-17 12:54:24 +00001050 Context.LAYOUT_INFLATER_SERVICE);
1051 final View layout = inflater.inflate(R.layout.setup_autofill_dialog, null);
1052
1053 builder.setView(layout)
1054 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1055 @Override
1056 public void onClick(DialogInterface dialog, int id) {
1057 CheckBox disableAutoFill = (CheckBox) layout.findViewById(
1058 R.id.setup_autofill_dialog_disable_autofill);
1059
1060 if (disableAutoFill.isChecked()) {
1061 // Disable autofill and show a toast with how to turn it on again.
John Reck35e9dd62011-04-25 09:01:54 -07001062 mSettings.setAutofillEnabled(false);
Michael Kolb14612442011-06-24 13:06:29 -07001063 Toast.makeText(mContext,
Ben Murdoch1d676b62011-01-17 12:54:24 +00001064 R.string.autofill_setup_dialog_negative_toast,
1065 Toast.LENGTH_LONG).show();
1066 } else {
1067 // Take user to the AutoFill profile editor. When they return,
1068 // we will send the message that we pass here which will trigger
1069 // the form to get filled out with their new profile.
1070 mWebViewController.setupAutoFill(msg);
1071 }
1072 }
1073 })
1074 .setNegativeButton(R.string.cancel, null)
1075 .show();
Ben Murdoch8029a772010-11-16 11:58:21 +00001076 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001077 };
1078
1079 // -------------------------------------------------------------------------
1080 // WebViewClient implementation for the sub window
1081 // -------------------------------------------------------------------------
1082
1083 // Subclass of WebViewClient used in subwindows to notify the main
1084 // WebViewClient of certain WebView activities.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001085 private static class SubWindowClient extends WebViewClient {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001086 // The main WebViewClient.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001087 private final WebViewClient mClient;
Michael Kolb8233fac2010-10-26 16:08:53 -07001088 private final WebViewController mController;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001089
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001090 SubWindowClient(WebViewClient client, WebViewController controller) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001091 mClient = client;
Michael Kolb8233fac2010-10-26 16:08:53 -07001092 mController = controller;
Leon Scroggins III211ba542010-04-19 13:21:13 -04001093 }
1094 @Override
1095 public void onPageStarted(WebView view, String url, Bitmap favicon) {
1096 // Unlike the others, do not call mClient's version, which would
1097 // change the progress bar. However, we do want to remove the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001098 // find or select dialog.
Michael Kolb8233fac2010-10-26 16:08:53 -07001099 mController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001100 }
1101 @Override
1102 public void doUpdateVisitedHistory(WebView view, String url,
1103 boolean isReload) {
1104 mClient.doUpdateVisitedHistory(view, url, isReload);
1105 }
1106 @Override
1107 public boolean shouldOverrideUrlLoading(WebView view, String url) {
1108 return mClient.shouldOverrideUrlLoading(view, url);
1109 }
1110 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -07001111 public void onReceivedHttpAuthRequest(WebView view,
1112 HttpAuthHandler handler, String host, String realm) {
1113 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
1114 }
1115 @Override
1116 public void onFormResubmission(WebView view, Message dontResend,
1117 Message resend) {
1118 mClient.onFormResubmission(view, dontResend, resend);
1119 }
1120 @Override
1121 public void onReceivedError(WebView view, int errorCode,
1122 String description, String failingUrl) {
1123 mClient.onReceivedError(view, errorCode, description, failingUrl);
1124 }
1125 @Override
1126 public boolean shouldOverrideKeyEvent(WebView view,
1127 android.view.KeyEvent event) {
1128 return mClient.shouldOverrideKeyEvent(view, event);
1129 }
1130 @Override
1131 public void onUnhandledKeyEvent(WebView view,
1132 android.view.KeyEvent event) {
1133 mClient.onUnhandledKeyEvent(view, event);
1134 }
1135 }
1136
1137 // -------------------------------------------------------------------------
1138 // WebChromeClient implementation for the sub window
1139 // -------------------------------------------------------------------------
1140
1141 private class SubWindowChromeClient extends WebChromeClient {
1142 // The main WebChromeClient.
1143 private final WebChromeClient mClient;
1144
1145 SubWindowChromeClient(WebChromeClient client) {
1146 mClient = client;
1147 }
1148 @Override
1149 public void onProgressChanged(WebView view, int newProgress) {
1150 mClient.onProgressChanged(view, newProgress);
1151 }
1152 @Override
1153 public boolean onCreateWindow(WebView view, boolean dialog,
1154 boolean userGesture, android.os.Message resultMsg) {
1155 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
1156 }
1157 @Override
1158 public void onCloseWindow(WebView window) {
1159 if (window != mSubView) {
1160 Log.e(LOGTAG, "Can't close the window");
1161 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001162 mWebViewController.dismissSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001163 }
1164 }
1165
1166 // -------------------------------------------------------------------------
1167
1168 // Construct a new tab
Michael Kolb7bcafde2011-05-09 13:55:59 -07001169 Tab(WebViewController wvcontroller, WebView w) {
John Reck1cf4b792011-07-26 10:22:22 -07001170 this(wvcontroller, w, null);
1171 }
1172
1173 Tab(WebViewController wvcontroller, Bundle state) {
1174 this(wvcontroller, null, state);
1175 }
1176
1177 Tab(WebViewController wvcontroller, WebView w, Bundle state) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001178 mWebViewController = wvcontroller;
Michael Kolb14612442011-06-24 13:06:29 -07001179 mContext = mWebViewController.getContext();
John Reck35e9dd62011-04-25 09:01:54 -07001180 mSettings = BrowserSettings.getInstance();
Michael Kolb14612442011-06-24 13:06:29 -07001181 mDataController = DataController.getInstance(mContext);
1182 mCurrentState = new PageState(mContext, w != null
John Reck847b5322011-04-14 17:02:18 -07001183 ? w.isPrivateBrowsingEnabled() : false);
Tarun Nainani8084c822014-06-25 13:38:06 -07001184 setTimeStamp();
Michael Kolb8233fac2010-10-26 16:08:53 -07001185 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001186 mInForeground = false;
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001187 mWebViewDestroyedByMemoryMonitor = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001188
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001189 mDownloadListener = new BrowserDownloadListener() {
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001190 public void onDownloadStart(String url, String userAgent,
Pankaj Garg5762b362015-11-02 07:57:06 -08001191 String contentDisposition, String mimetype, String referer, String auth,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001192 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001193 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
Pankaj Garg5762b362015-11-02 07:57:06 -08001194 mimetype, referer, auth, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001195 }
1196 };
1197
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001198 mCaptureWidth = mContext.getResources().getDimensionPixelSize(R.dimen.tab_thumbnail_width);
1199 mCaptureHeight =mContext.getResources().getDimensionPixelSize(R.dimen.tab_thumbnail_height);
1200
1201 initCaptureBitmap();
1202
John Reck1cf4b792011-07-26 10:22:22 -07001203 restoreState(state);
John Reck52be4782011-08-26 15:37:29 -07001204 if (getId() == -1) {
1205 mId = TabControl.getNextId();
1206 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001207 setWebView(w);
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -07001208
1209 UI ui = ((Controller)mWebViewController).getUi();
1210 if (ui instanceof BaseUi) {
1211 TitleBar titleBar = ((BaseUi)ui).getTitleBar();
1212 if (titleBar != null) {
1213 NavigationBarBase navBar = titleBar.getNavigationBar();
1214 navBar.showCurrentFavicon(this); // Show the default Favicon while loading a new page
1215 }
1216 }
1217
Michael Kolb9ef259a2011-07-12 15:33:08 -07001218 mHandler = new Handler() {
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001219 @Override
Michael Kolb9ef259a2011-07-12 15:33:08 -07001220 public void handleMessage(Message m) {
John Reck1cf4b792011-07-26 10:22:22 -07001221 switch (m.what) {
1222 case MSG_CAPTURE:
1223 capture();
1224 break;
1225 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001226 }
1227 };
Pankaj Garg1c13cab2015-05-12 11:52:17 -07001228
1229 mFirstPixelObservable = new Observable();
1230 mFirstPixelObservable.set(false);
1231 mTabHistoryUpdateObservable = new Observable();
John Reck1cf4b792011-07-26 10:22:22 -07001232 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001233
Pankaj Gargb4b4f012015-08-04 16:19:14 -07001234 public void initCaptureBitmap() {
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001235 mCapture = Bitmap.createBitmap(mCaptureWidth, mCaptureHeight, Bitmap.Config.RGB_565);
1236 mCapture.eraseColor(Color.WHITE);
Michael Kolb72864272012-05-03 15:42:15 -07001237 }
1238
Mathew Inwoode09305e2011-09-02 12:03:26 +01001239 /**
1240 * This is used to get a new ID when the tab has been preloaded, before it is displayed and
1241 * added to TabControl. Preloaded tabs can be created before restoreInstanceState, leading
1242 * to overlapping IDs between the preloaded and restored tabs.
1243 */
1244 public void refreshIdAfterPreload() {
1245 mId = TabControl.getNextId();
1246 }
1247
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001248 public void setController(WebViewController ctl) {
1249 mWebViewController = ctl;
1250
John Reck1cf4b792011-07-26 10:22:22 -07001251 if (mWebViewController.shouldCaptureThumbnails()) {
1252 synchronized (Tab.this) {
1253 if (mCapture == null) {
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001254 initCaptureBitmap();
1255 if (mInForeground && !mHandler.hasMessages(MSG_CAPTURE)) {
1256 mHandler.sendEmptyMessageDelayed(MSG_CAPTURE, CAPTURE_DELAY);
John Reck1cf4b792011-07-26 10:22:22 -07001257 }
1258 }
1259 }
1260 } else {
1261 synchronized (Tab.this) {
1262 mCapture = null;
1263 deleteThumbnail();
1264 }
1265 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001266 }
1267
Michael Kolbc831b632011-05-11 09:30:34 -07001268 public long getId() {
1269 return mId;
1270 }
1271
Michael Kolb91911a22012-01-17 11:21:25 -08001272 void setWebView(WebView w) {
1273 setWebView(w, true);
1274 }
1275
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001276 public boolean isNativeActive(){
1277 if (mMainView == null)
1278 return false;
1279 return true;
1280 }
1281
1282 public void setTimeStamp(){
1283 Date d = new Date();
1284 timestamp = (new Timestamp(d.getTime()));
1285 }
1286
1287 public Timestamp getTimestamp() {
1288 return timestamp;
1289 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001290 /**
1291 * Sets the WebView for this tab, correctly removing the old WebView from
1292 * the container view.
1293 */
Michael Kolb91911a22012-01-17 11:21:25 -08001294 void setWebView(WebView w, boolean restore) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001295 if (mMainView == w) {
1296 return;
1297 }
Michael Kolba713ec82010-11-29 17:27:06 -08001298
Michael Kolba713ec82010-11-29 17:27:06 -08001299 mWebViewController.onSetWebView(this, w);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001300
John Reck1cf4b792011-07-26 10:22:22 -07001301 if (mMainView != null) {
John Reck8ee633f2011-08-09 16:00:35 -07001302 mMainView.setPictureListener(null);
John Reck1cf4b792011-07-26 10:22:22 -07001303 if (w != null) {
1304 syncCurrentState(w, null);
1305 } else {
Panos Thomasa9a5a582014-03-18 19:20:08 -07001306 mCurrentState = new PageState(mContext, mMainView.isPrivateBrowsingEnabled());
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001307
1308 if (mWebViewDestroyedByMemoryMonitor) {
1309 /*
1310 * If tab was destroyed as a result of the MemoryMonitor
1311 * then we need to restore the state properties
1312 * from the old WebView (mMainView)
1313 */
1314 syncCurrentState(mMainView, null);
1315 mWebViewDestroyedByMemoryMonitor = false;
1316 }
John Reck1cf4b792011-07-26 10:22:22 -07001317 }
1318 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001319 // set the new one
1320 mMainView = w;
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001321
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001322 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001323 if (mMainView != null) {
1324 mMainView.setWebViewClient(mWebViewClient);
1325 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001326 // Attach DownloadManager so that downloads can start in an active
1327 // or a non-active window. This can happen when going to a site that
1328 // does a redirect after a period of time. The user could have
1329 // switched to another tab while waiting for the download to start.
1330 mMainView.setDownloadListener(mDownloadListener);
John Reck8ee633f2011-08-09 16:00:35 -07001331 TabControl tc = mWebViewController.getTabControl();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001332 if (tc != null /*&& tc.getOnThumbnailUpdatedListener() != null*/) {
John Reck8ee633f2011-08-09 16:00:35 -07001333 mMainView.setPictureListener(this);
1334 }
Michael Kolb91911a22012-01-17 11:21:25 -08001335 if (restore && (mSavedState != null)) {
John Reck8b9bb8b2012-03-08 13:19:40 -08001336 restoreUserAgent();
John Reck6c2e2f32011-08-22 13:41:23 -07001337 WebBackForwardList restoredState
1338 = mMainView.restoreState(mSavedState);
1339 if (restoredState == null || restoredState.getSize() == 0) {
1340 Log.w(LOGTAG, "Failed to restore WebView state!");
1341 loadUrl(mCurrentState.mOriginalUrl, null);
1342 }
John Reck1cf4b792011-07-26 10:22:22 -07001343 mSavedState = null;
1344 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001345 }
1346 }
1347
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001348 public void destroyThroughMemoryMonitor() {
1349 mWebViewDestroyedByMemoryMonitor = true;
1350 destroy();
1351 }
1352
Grace Kloba22ac16e2009-10-07 18:00:23 -07001353 /**
1354 * Destroy the tab's main WebView and subWindow if any
1355 */
1356 void destroy() {
Tarun Nainani2c1dd7c2014-07-05 16:40:12 -07001357
Matthew Huib7f2e9c2014-04-16 11:12:37 -04001358 if (mPostponeDestroy) {
1359 mShouldDestroy = true;
1360 return;
1361 }
1362 mShouldDestroy = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001363 if (mMainView != null) {
1364 dismissSubWindow();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001365 // save the WebView to call destroy() after detach it from the tab
Vivek Sekhar53ef8932015-06-18 16:51:43 -07001366 final WebView webView = mMainView;
Vivek Sekhar2ee19a32015-07-02 17:03:57 -07001367 setWebView(null);
Site Mao61b68212015-07-16 10:56:31 -07001368 if (!mWebViewDestroyedByMemoryMonitor && !BaseUi.isUiLowPowerMode()) {
Pankaj Garg036365b2015-09-14 11:21:19 -07001369 final int destroyedTabIdx = (int) mId;
Vivek Sekhar2ee19a32015-07-02 17:03:57 -07001370 // Tabs can be reused with new instance of WebView so delete the snapshots
Vivek Sekhar53ef8932015-06-18 16:51:43 -07001371 webView.getSnapshotIds(new ValueCallback<List<Integer>>() {
1372 @Override
1373 public void onReceiveValue(List<Integer> ids) {
Vivek Sekhar53ef8932015-06-18 16:51:43 -07001374 for (Integer id : ids) {
Pankaj Garg036365b2015-09-14 11:21:19 -07001375 if (getTabIdxFromCaptureIdx(id) == destroyedTabIdx) {
Vivek Sekhar53ef8932015-06-18 16:51:43 -07001376 webView.deleteSnapshot(id);
1377 }
1378 }
Vivek Sekhar53ef8932015-06-18 16:51:43 -07001379 webView.destroy();
Pankaj Garg1c13cab2015-05-12 11:52:17 -07001380 }
Vivek Sekhar53ef8932015-06-18 16:51:43 -07001381 });
Vivek Sekhar2ee19a32015-07-02 17:03:57 -07001382 } else {
1383 webView.destroy();
Pankaj Garg1c13cab2015-05-12 11:52:17 -07001384 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001385 }
1386 }
1387
Matthew Huib7f2e9c2014-04-16 11:12:37 -04001388 private boolean mPostponeDestroy = false;
1389 private boolean mShouldDestroy = false;
1390
1391 public void postponeDestroy() {
1392 mPostponeDestroy = true;
1393 }
1394
1395 public void performPostponedDestroy() {
1396 mPostponeDestroy = false;
1397 if (mShouldDestroy) {
1398 destroy();
1399 }
1400 }
1401
Grace Kloba22ac16e2009-10-07 18:00:23 -07001402 /**
1403 * Remove the tab from the parent
1404 */
1405 void removeFromTree() {
1406 // detach the children
Michael Kolbc831b632011-05-11 09:30:34 -07001407 if (mChildren != null) {
1408 for(Tab t : mChildren) {
1409 t.setParent(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001410 }
1411 }
1412 // remove itself from the parent list
Michael Kolbc831b632011-05-11 09:30:34 -07001413 if (mParent != null) {
1414 mParent.mChildren.remove(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001415 }
Tarun Nainani8eb00912014-07-17 12:28:32 -07001416
1417 mCapture = null;
John Reck1cf4b792011-07-26 10:22:22 -07001418 deleteThumbnail();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001419 }
1420
1421 /**
1422 * Create a new subwindow unless a subwindow already exists.
1423 * @return True if a new subwindow was created. False if one already exists.
1424 */
1425 boolean createSubWindow() {
1426 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001427 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001428 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001429 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001430 mSubView.setWebChromeClient(new SubWindowChromeClient(
1431 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001432 // Set a different DownloadListener for the mSubView, since it will
1433 // just need to dismiss the mSubView, rather than close the Tab
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001434 mSubView.setDownloadListener(new BrowserDownloadListener() {
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001435 public void onDownloadStart(String url, String userAgent,
Pankaj Garg5762b362015-11-02 07:57:06 -08001436 String contentDisposition, String mimetype, String referer, String auth,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001437 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001438 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Pankaj Garg5762b362015-11-02 07:57:06 -08001439 contentDisposition, mimetype, referer, auth, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001440 if (mSubView.copyBackForwardList().getSize() == 0) {
1441 // This subwindow was opened for the sole purpose of
1442 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001443 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001444 }
1445 }
1446 });
Michael Kolb14612442011-06-24 13:06:29 -07001447 mSubView.setOnCreateContextMenuListener(mWebViewController.getActivity());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001448 return true;
1449 }
1450 return false;
1451 }
1452
1453 /**
1454 * Dismiss the subWindow for the tab.
1455 */
1456 void dismissSubWindow() {
1457 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001458 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001459 mSubView.destroy();
1460 mSubView = null;
1461 mSubViewContainer = null;
1462 }
1463 }
1464
Grace Kloba22ac16e2009-10-07 18:00:23 -07001465
1466 /**
1467 * Set the parent tab of this tab.
1468 */
Michael Kolbc831b632011-05-11 09:30:34 -07001469 void setParent(Tab parent) {
John Reck52be4782011-08-26 15:37:29 -07001470 if (parent == this) {
1471 throw new IllegalStateException("Cannot set parent to self!");
1472 }
Michael Kolbc831b632011-05-11 09:30:34 -07001473 mParent = parent;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001474 // This tab may have been freed due to low memory. If that is the case,
Michael Kolbc831b632011-05-11 09:30:34 -07001475 // the parent tab id is already saved. If we are changing that id
Grace Kloba22ac16e2009-10-07 18:00:23 -07001476 // (most likely due to removing the parent tab) we must update the
Michael Kolbc831b632011-05-11 09:30:34 -07001477 // parent tab id in the saved Bundle.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001478 if (mSavedState != null) {
1479 if (parent == null) {
1480 mSavedState.remove(PARENTTAB);
1481 } else {
Michael Kolbc831b632011-05-11 09:30:34 -07001482 mSavedState.putLong(PARENTTAB, parent.getId());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001483 }
1484 }
John Reckb0a86db2011-05-24 14:05:58 -07001485
1486 // Sync the WebView useragent with the parent
1487 if (parent != null && mSettings.hasDesktopUseragent(parent.getWebView())
1488 != mSettings.hasDesktopUseragent(getWebView())) {
1489 mSettings.toggleDesktopUseragent(getWebView());
1490 }
John Reck52be4782011-08-26 15:37:29 -07001491
1492 if (parent != null && parent.getId() == getId()) {
1493 throw new IllegalStateException("Parent has same ID as child!");
1494 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001495 }
1496
1497 /**
Michael Kolbc831b632011-05-11 09:30:34 -07001498 * If this Tab was created through another Tab, then this method returns
1499 * that Tab.
1500 * @return the Tab parent or null
1501 */
1502 public Tab getParent() {
1503 return mParent;
1504 }
1505
1506 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001507 * When a Tab is created through the content of another Tab, then we
1508 * associate the Tabs.
1509 * @param child the Tab that was created from this Tab
1510 */
1511 void addChildTab(Tab child) {
Michael Kolbc831b632011-05-11 09:30:34 -07001512 if (mChildren == null) {
1513 mChildren = new Vector<Tab>();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001514 }
Michael Kolbc831b632011-05-11 09:30:34 -07001515 mChildren.add(child);
1516 child.setParent(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001517 }
1518
Michael Kolbc831b632011-05-11 09:30:34 -07001519 Vector<Tab> getChildren() {
1520 return mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001521 }
1522
1523 void resume() {
1524 if (mMainView != null) {
John Reck56c1fcf2011-08-17 10:15:16 -07001525 setupHwAcceleration(mMainView);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001526 mMainView.onResume();
1527 if (mSubView != null) {
1528 mSubView.onResume();
1529 }
1530 }
1531 }
1532
John Reck56c1fcf2011-08-17 10:15:16 -07001533 private void setupHwAcceleration(View web) {
1534 if (web == null) return;
1535 BrowserSettings settings = BrowserSettings.getInstance();
1536 if (settings.isHardwareAccelerated()) {
1537 web.setLayerType(View.LAYER_TYPE_NONE, null);
1538 } else {
1539 web.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
1540 }
1541 }
1542
Grace Kloba22ac16e2009-10-07 18:00:23 -07001543 void pause() {
1544 if (mMainView != null) {
1545 mMainView.onPause();
1546 if (mSubView != null) {
1547 mSubView.onPause();
1548 }
1549 }
1550 }
1551
1552 void putInForeground() {
John Reck8ee633f2011-08-09 16:00:35 -07001553 if (mInForeground) {
1554 return;
1555 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001556 mInForeground = true;
1557 resume();
Michael Kolb14612442011-06-24 13:06:29 -07001558 Activity activity = mWebViewController.getActivity();
1559 mMainView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001560 if (mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -07001561 mSubView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001562 }
Axesh R. Ajmerac6b5c322015-05-01 11:06:10 -07001563
Leon Scroggins1961ed22010-12-07 15:22:21 -05001564 mWebViewController.bookmarkedStatusHasChanged(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001565 }
1566
1567 void putInBackground() {
John Reck8ee633f2011-08-09 16:00:35 -07001568 if (!mInForeground) {
1569 return;
1570 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001571 mInForeground = false;
1572 pause();
1573 mMainView.setOnCreateContextMenuListener(null);
1574 if (mSubView != null) {
1575 mSubView.setOnCreateContextMenuListener(null);
1576 }
1577 }
1578
Michael Kolb8233fac2010-10-26 16:08:53 -07001579 boolean inForeground() {
1580 return mInForeground;
1581 }
1582
Grace Kloba22ac16e2009-10-07 18:00:23 -07001583 /**
1584 * Return the top window of this tab; either the subwindow if it is not
1585 * null or the main window.
1586 * @return The top window of this tab.
1587 */
1588 WebView getTopWindow() {
1589 if (mSubView != null) {
1590 return mSubView;
1591 }
1592 return mMainView;
1593 }
1594
1595 /**
1596 * Return the main window of this tab. Note: if a tab is freed in the
1597 * background, this can return null. It is only guaranteed to be
1598 * non-null for the current tab.
1599 * @return The main WebView of this tab.
1600 */
1601 WebView getWebView() {
1602 return mMainView;
1603 }
1604
Michael Kolba713ec82010-11-29 17:27:06 -08001605 void setViewContainer(View container) {
1606 mContainer = container;
1607 }
1608
Michael Kolb8233fac2010-10-26 16:08:53 -07001609 View getViewContainer() {
1610 return mContainer;
1611 }
1612
Grace Kloba22ac16e2009-10-07 18:00:23 -07001613 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001614 * Return whether private browsing is enabled for the main window of
1615 * this tab.
1616 * @return True if private browsing is enabled.
1617 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001618 boolean isPrivateBrowsingEnabled() {
John Reck502a3532011-08-16 14:21:46 -07001619 return mCurrentState.mIncognito;
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001620 }
1621
1622 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001623 * Return the subwindow of this tab or null if there is no subwindow.
1624 * @return The subwindow of this tab or null.
1625 */
1626 WebView getSubWebView() {
1627 return mSubView;
1628 }
1629
Michael Kolb1514bb72010-11-22 09:11:48 -08001630 void setSubWebView(WebView subView) {
1631 mSubView = subView;
1632 }
1633
Michael Kolb8233fac2010-10-26 16:08:53 -07001634 View getSubViewContainer() {
1635 return mSubViewContainer;
1636 }
1637
Michael Kolb1514bb72010-11-22 09:11:48 -08001638 void setSubViewContainer(View subViewContainer) {
1639 mSubViewContainer = subViewContainer;
1640 }
1641
Grace Kloba22ac16e2009-10-07 18:00:23 -07001642
1643 /**
1644 * @return The application id string
1645 */
1646 String getAppId() {
1647 return mAppId;
1648 }
1649
1650 /**
1651 * Set the application id string
1652 * @param id
1653 */
1654 void setAppId(String id) {
1655 mAppId = id;
1656 }
1657
Michael Kolbe28b3472011-08-04 16:54:31 -07001658 boolean closeOnBack() {
1659 return mCloseOnBack;
1660 }
1661
1662 void setCloseOnBack(boolean close) {
1663 mCloseOnBack = close;
1664 }
1665
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001666 boolean getDerivedFromIntent() {
1667 return mDerivedFromIntent;
1668 }
1669
1670 void setDerivedFromIntent(boolean derived) {
1671 mDerivedFromIntent = derived;
1672 }
1673
Grace Kloba22ac16e2009-10-07 18:00:23 -07001674 String getUrl() {
John Reck324d4402011-01-11 16:56:42 -08001675 return UrlUtils.filteredUrl(mCurrentState.mUrl);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001676 }
1677
Tarun Nainani8eb00912014-07-17 12:28:32 -07001678
1679 protected void onPageFinished() {
1680 mPageFinished = true;
Ze G Riande2a675c22015-06-03 11:15:24 -07001681 isDistillable();
Tarun Nainani8eb00912014-07-17 12:28:32 -07001682 }
1683
1684 public boolean getPageFinishedStatus() {
1685 return mPageFinished;
1686 }
1687
John Reck49a603c2011-03-03 09:33:05 -08001688 String getOriginalUrl() {
John Reckdb22ec42011-06-29 11:31:24 -07001689 if (mCurrentState.mOriginalUrl == null) {
1690 return getUrl();
John Reck49a603c2011-03-03 09:33:05 -08001691 }
John Reckdb22ec42011-06-29 11:31:24 -07001692 return UrlUtils.filteredUrl(mCurrentState.mOriginalUrl);
John Reck49a603c2011-03-03 09:33:05 -08001693 }
1694
Grace Kloba22ac16e2009-10-07 18:00:23 -07001695 /**
John Reck30c714c2010-12-16 17:30:34 -08001696 * Get the title of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001697 */
1698 String getTitle() {
John Reck30c714c2010-12-16 17:30:34 -08001699 return mCurrentState.mTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001700 }
1701
1702 /**
John Reck30c714c2010-12-16 17:30:34 -08001703 * Get the favicon of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001704 */
1705 Bitmap getFavicon() {
John Reck1cf4b792011-07-26 10:22:22 -07001706 if (mCurrentState.mFavicon != null) {
1707 return mCurrentState.mFavicon;
1708 }
1709 return getDefaultFavicon(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001710 }
1711
Pankaj Garg32e1b942015-06-03 18:13:24 -07001712 public boolean hasFavicon() {
1713 return mCurrentState.mFavicon != null;
1714 }
1715
John Recke969cc52010-12-21 17:24:43 -08001716 public boolean isBookmarkedSite() {
1717 return mCurrentState.mIsBookmarkedSite;
1718 }
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001719
Grace Kloba22ac16e2009-10-07 18:00:23 -07001720 /**
Steve Block08a6f0c2011-10-06 12:12:53 +01001721 * Sets the security state, clears the SSL certificate error and informs
1722 * the controller.
1723 */
Steve Block2466eff2011-10-03 15:33:09 +01001724 private void setSecurityState(SecurityState securityState) {
1725 mCurrentState.mSecurityState = securityState;
1726 mWebViewController.onUpdatedSecurityState(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001727 }
1728
1729 /**
Steve Block2466eff2011-10-03 15:33:09 +01001730 * @return The tab's security state.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001731 */
Steve Block2466eff2011-10-03 15:33:09 +01001732 SecurityState getSecurityState() {
1733 return mCurrentState.mSecurityState;
John Reck30c714c2010-12-16 17:30:34 -08001734 }
1735
1736 int getLoadProgress() {
1737 if (mInPageLoad) {
1738 return mPageLoadProgress;
1739 }
1740 return 100;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001741 }
1742
1743 /**
1744 * @return TRUE if onPageStarted is called while onPageFinished is not
1745 * called yet.
1746 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001747 boolean inPageLoad() {
1748 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001749 }
1750
Grace Kloba22ac16e2009-10-07 18:00:23 -07001751 /**
John Reck1cf4b792011-07-26 10:22:22 -07001752 * @return The Bundle with the tab's state if it can be saved, otherwise null
Grace Kloba22ac16e2009-10-07 18:00:23 -07001753 */
John Reck1cf4b792011-07-26 10:22:22 -07001754 public Bundle saveState() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001755 // If the WebView is null it means we ran low on memory and we already
1756 // stored the saved state in mSavedState.
1757 if (mMainView == null) {
John Reck1cf4b792011-07-26 10:22:22 -07001758 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001759 }
John Reck6c2e2f32011-08-22 13:41:23 -07001760
1761 if (TextUtils.isEmpty(mCurrentState.mUrl)) {
John Reck1cf4b792011-07-26 10:22:22 -07001762 return null;
John Reck24f18262011-06-17 14:47:20 -07001763 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001764
1765 mSavedState = new Bundle();
John Reck6c2e2f32011-08-22 13:41:23 -07001766 WebBackForwardList savedList = mMainView.saveState(mSavedState);
1767 if (savedList == null || savedList.getSize() == 0) {
1768 Log.w(LOGTAG, "Failed to save back/forward list for "
1769 + mCurrentState.mUrl);
1770 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001771
Michael Kolbc831b632011-05-11 09:30:34 -07001772 mSavedState.putLong(ID, mId);
John Reck30c714c2010-12-16 17:30:34 -08001773 mSavedState.putString(CURRURL, mCurrentState.mUrl);
1774 mSavedState.putString(CURRTITLE, mCurrentState.mTitle);
John Reck1cf4b792011-07-26 10:22:22 -07001775 mSavedState.putBoolean(INCOGNITO, mMainView.isPrivateBrowsingEnabled());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001776 if (mAppId != null) {
1777 mSavedState.putString(APPID, mAppId);
1778 }
Michael Kolbe28b3472011-08-04 16:54:31 -07001779 mSavedState.putBoolean(CLOSEFLAG, mCloseOnBack);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001780 // Remember the parent tab so the relationship can be restored.
Michael Kolbc831b632011-05-11 09:30:34 -07001781 if (mParent != null) {
1782 mSavedState.putLong(PARENTTAB, mParent.mId);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001783 }
John Reckb0a86db2011-05-24 14:05:58 -07001784 mSavedState.putBoolean(USERAGENT,
1785 mSettings.hasDesktopUseragent(getWebView()));
John Reck1cf4b792011-07-26 10:22:22 -07001786 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001787 }
1788
1789 /*
1790 * Restore the state of the tab.
1791 */
John Reck1cf4b792011-07-26 10:22:22 -07001792 private void restoreState(Bundle b) {
1793 mSavedState = b;
1794 if (mSavedState == null) {
1795 return;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001796 }
1797 // Restore the internal state even if the WebView fails to restore.
1798 // This will maintain the app id, original url and close-on-exit values.
Michael Kolbc831b632011-05-11 09:30:34 -07001799 mId = b.getLong(ID);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001800 mAppId = b.getString(APPID);
Michael Kolbe28b3472011-08-04 16:54:31 -07001801 mCloseOnBack = b.getBoolean(CLOSEFLAG);
John Reck8b9bb8b2012-03-08 13:19:40 -08001802 restoreUserAgent();
John Reck1cf4b792011-07-26 10:22:22 -07001803 String url = b.getString(CURRURL);
1804 String title = b.getString(CURRTITLE);
1805 boolean incognito = b.getBoolean(INCOGNITO);
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -07001806 mCurrentState = new PageState(mContext, incognito, url);
John Reck1cf4b792011-07-26 10:22:22 -07001807 mCurrentState.mTitle = title;
1808 synchronized (Tab.this) {
1809 if (mCapture != null) {
John Reck4eadc342011-10-31 14:04:10 -07001810 DataController.getInstance(mContext).loadThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07001811 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001812 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001813 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04001814
John Reck8b9bb8b2012-03-08 13:19:40 -08001815 private void restoreUserAgent() {
1816 if (mMainView == null || mSavedState == null) {
1817 return;
1818 }
1819 if (mSavedState.getBoolean(USERAGENT)
1820 != mSettings.hasDesktopUseragent(mMainView)) {
1821 mSettings.toggleDesktopUseragent(mMainView);
1822 }
1823 }
1824
Leon Scroggins1961ed22010-12-07 15:22:21 -05001825 public void updateBookmarkedStatus() {
John Recke969cc52010-12-21 17:24:43 -08001826 mDataController.queryBookmarkStatus(getUrl(), mIsBookmarkCallback);
Leon Scroggins1961ed22010-12-07 15:22:21 -05001827 }
1828
John Recke969cc52010-12-21 17:24:43 -08001829 private DataController.OnQueryUrlIsBookmark mIsBookmarkCallback
1830 = new DataController.OnQueryUrlIsBookmark() {
1831 @Override
1832 public void onQueryUrlIsBookmark(String url, boolean isBookmark) {
Karthikeyan Periasamy66c06a12015-10-21 18:04:14 -07001833 if (TextUtils.isEmpty(mCurrentState.mUrl) || mCurrentState.mUrl.equals(url)) {
John Recke969cc52010-12-21 17:24:43 -08001834 mCurrentState.mIsBookmarkedSite = isBookmark;
1835 mWebViewController.bookmarkedStatusHasChanged(Tab.this);
1836 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001837 }
John Recke969cc52010-12-21 17:24:43 -08001838 };
Michael Kolb1acef692011-03-08 14:12:06 -08001839
Michael Kolbeb95db42011-03-03 10:38:40 -08001840 public Bitmap getScreenshot() {
John Reck1cf4b792011-07-26 10:22:22 -07001841 synchronized (Tab.this) {
1842 return mCapture;
1843 }
Michael Kolbeb95db42011-03-03 10:38:40 -08001844 }
1845
John Reck541f55a2011-06-07 16:34:43 -07001846 public boolean isSnapshot() {
John Reck541f55a2011-06-07 16:34:43 -07001847 return false;
1848 }
1849
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001850 private static class SaveCallback implements ValueCallback<String> {
1851 boolean onReceiveValueCalled = false;
1852 private String mPath;
John Reck68234a92012-04-19 15:27:12 -07001853
1854 @Override
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001855 public void onReceiveValue(String path) {
1856 this.onReceiveValueCalled = true;
1857 this.mPath = path;
John Reck68234a92012-04-19 15:27:12 -07001858 synchronized (this) {
1859 notifyAll();
John Reck8cc92352011-07-06 17:41:52 -07001860 }
John Reck541f55a2011-06-07 16:34:43 -07001861 }
John Reck68234a92012-04-19 15:27:12 -07001862
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001863 public String getPath() {
1864 return mPath;
1865 }
John Reck68234a92012-04-19 15:27:12 -07001866 }
1867
1868 /**
1869 * Must be called on the UI thread
1870 */
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001871 public ContentValues createSnapshotValues(Bitmap bm) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001872 WebView web = getWebView();
John Reck68234a92012-04-19 15:27:12 -07001873 if (web == null) return null;
John Reckd8c74522011-06-14 08:45:00 -07001874 ContentValues values = new ContentValues();
1875 values.put(Snapshots.TITLE, mCurrentState.mTitle);
1876 values.put(Snapshots.URL, mCurrentState.mUrl);
John Reck68234a92012-04-19 15:27:12 -07001877 values.put(Snapshots.BACKGROUND, web.getPageBackgroundColor());
John Reck8cc92352011-07-06 17:41:52 -07001878 values.put(Snapshots.DATE_CREATED, System.currentTimeMillis());
1879 values.put(Snapshots.FAVICON, compressBitmap(getFavicon()));
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001880 values.put(Snapshots.THUMBNAIL, compressBitmap(bm));
John Reckd8c74522011-06-14 08:45:00 -07001881 return values;
John Reck541f55a2011-06-07 16:34:43 -07001882 }
1883
John Reck68234a92012-04-19 15:27:12 -07001884 /**
1885 * Probably want to call this on a background thread
1886 */
1887 public boolean saveViewState(ContentValues values) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001888 WebView web = getWebView();
John Reck68234a92012-04-19 15:27:12 -07001889 if (web == null) return false;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001890 String filename = UUID.randomUUID().toString();
John Reck68234a92012-04-19 15:27:12 -07001891 SaveCallback callback = new SaveCallback();
John Reck68234a92012-04-19 15:27:12 -07001892 try {
John Reck68234a92012-04-19 15:27:12 -07001893 synchronized (callback) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001894 web.saveViewState(filename, callback);
1895 callback.wait();
John Reck68234a92012-04-19 15:27:12 -07001896 }
John Reck68234a92012-04-19 15:27:12 -07001897 } catch (Exception e) {
1898 Log.w(LOGTAG, "Failed to save view state", e);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001899 String path = callback.getPath();
1900 if (path != null) {
1901 File file = mContext.getFileStreamPath(path);
1902 if (file.exists() && !file.delete()) {
1903 file.deleteOnExit();
1904 }
John Reck68234a92012-04-19 15:27:12 -07001905 }
1906 return false;
1907 }
Tarun Nainani8eb00912014-07-17 12:28:32 -07001908
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001909 String path = callback.getPath();
Tarun Nainani8eb00912014-07-17 12:28:32 -07001910 // could be that saving of file failed
1911 if (path == null) {
1912 return false;
1913 }
1914
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001915 File savedFile = new File(path);
1916 if (!savedFile.exists()) {
1917 return false;
John Reck68234a92012-04-19 15:27:12 -07001918 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001919 values.put(Snapshots.VIEWSTATE_PATH, path.substring(path.lastIndexOf('/') + 1));
1920 values.put(Snapshots.VIEWSTATE_SIZE, savedFile.length());
John Reck68234a92012-04-19 15:27:12 -07001921 return true;
1922 }
1923
John Reck8cc92352011-07-06 17:41:52 -07001924 public byte[] compressBitmap(Bitmap bitmap) {
1925 if (bitmap == null) {
1926 return null;
1927 }
1928 ByteArrayOutputStream stream = new ByteArrayOutputStream();
1929 bitmap.compress(CompressFormat.PNG, 100, stream);
1930 return stream.toByteArray();
1931 }
1932
John Reck26b18322011-06-21 13:08:58 -07001933 public void loadUrl(String url, Map<String, String> headers) {
1934 if (mMainView != null) {
Michael Kolba53c9892011-10-05 13:31:40 -07001935 mPageLoadProgress = INITIAL_PROGRESS;
Axesh R. Ajmerad04cad12015-08-12 12:02:44 -07001936 mCurrentState = new PageState(
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -07001937 mContext, mMainView.isPrivateBrowsingEnabled(), url);
John Reck26b18322011-06-21 13:08:58 -07001938 mMainView.loadUrl(url, headers);
1939 }
1940 }
1941
John Reck38b39652012-06-05 09:22:59 -07001942 public void disableUrlOverridingForLoad() {
1943 mDisableOverrideUrlLoading = true;
1944 }
1945
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001946 private void thumbnailUpdated() {
John Reck8ee633f2011-08-09 16:00:35 -07001947 mHandler.removeMessages(MSG_CAPTURE);
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001948
John Reck8ee633f2011-08-09 16:00:35 -07001949 TabControl tc = mWebViewController.getTabControl();
1950 if (tc != null) {
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001951 OnThumbnailUpdatedListener updateListener = tc.getOnThumbnailUpdatedListener();
John Reck8ee633f2011-08-09 16:00:35 -07001952 if (updateListener != null) {
1953 updateListener.onThumbnailUpdated(this);
1954 }
1955 }
Pankaj Gargf49e0222015-09-01 12:19:13 -07001956
1957 if (mViewportCapture != null) {
1958 mWebViewController.onThumbnailCapture(mViewportCapture);
1959 mViewportCapture.recycle();
1960 mViewportCapture = null;
1961 } else {
1962 mWebViewController.onThumbnailCapture(mCapture);
1963 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001964 }
1965
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001966 protected void capture() {
1967 if (mMainView == null || mCapture == null || !mMainView.isReady() ||
1968 mMainView.getContentWidth() <= 0 || mMainView.getContentHeight() <= 0 ||
1969 !mFirstVisualPixelPainted || mMainView.isShowingCrashView()) {
Pankaj Gargf49e0222015-09-01 12:19:13 -07001970 mViewportCapture = null;
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001971 initCaptureBitmap();
1972 thumbnailUpdated();
1973 return;
1974 }
1975
1976 mMainView.getContentBitmapAsync((float) mCaptureWidth / mMainView.getWidth(), new Rect(),
1977 new ValueCallback<Bitmap>() {
1978 @Override
1979 public void onReceiveValue(Bitmap bitmap) {
Pankaj Gargf49e0222015-09-01 12:19:13 -07001980 mViewportCapture = bitmap;
1981
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001982 if (mCapture == null) {
1983 initCaptureBitmap();
1984 }
1985
1986 if (bitmap == null) {
1987 thumbnailUpdated();
1988 return;
1989 }
1990
1991 Canvas c = new Canvas(mCapture);
1992 mCapture.eraseColor(Color.WHITE);
1993 c.drawBitmap(bitmap, 0, 0, null);
1994
1995 // manually anti-alias the edges for the tilt
1996 c.drawRect(0, 0, 1, mCapture.getHeight(), sAlphaPaint);
1997 c.drawRect(mCapture.getWidth() - 1, 0, mCapture.getWidth(),
1998 mCapture.getHeight(), sAlphaPaint);
1999 c.drawRect(0, 0, mCapture.getWidth(), 1, sAlphaPaint);
2000 c.drawRect(0, mCapture.getHeight() - 1, mCapture.getWidth(),
2001 mCapture.getHeight(), sAlphaPaint);
2002 c.setBitmap(null);
2003
2004 persistThumbnail();
2005 thumbnailUpdated();
2006 }
2007 }
2008 );
John Reck1cf4b792011-07-26 10:22:22 -07002009 }
2010
Pankaj Garg6bedeba2015-06-23 15:47:37 -07002011 @Override
2012 public void onNewPicture(WebView view, Picture picture) {
Michael Kolb9ef259a2011-07-12 15:33:08 -07002013 }
2014
John Reckef654f12011-07-12 16:42:08 -07002015 public boolean canGoBack() {
2016 return mMainView != null ? mMainView.canGoBack() : false;
2017 }
2018
2019 public boolean canGoForward() {
2020 return mMainView != null ? mMainView.canGoForward() : false;
2021 }
2022
2023 public void goBack() {
2024 if (mMainView != null) {
2025 mMainView.goBack();
2026 }
2027 }
2028
2029 public void goForward() {
2030 if (mMainView != null) {
2031 mMainView.goForward();
2032 }
2033 }
2034
John Reck1cf4b792011-07-26 10:22:22 -07002035 protected void persistThumbnail() {
John Reck4eadc342011-10-31 14:04:10 -07002036 DataController.getInstance(mContext).saveThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07002037 }
2038
2039 protected void deleteThumbnail() {
John Reck4eadc342011-10-31 14:04:10 -07002040 DataController.getInstance(mContext).deleteThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07002041 }
2042
John Reck4eadc342011-10-31 14:04:10 -07002043 void updateCaptureFromBlob(byte[] blob) {
John Reck1cf4b792011-07-26 10:22:22 -07002044 synchronized (Tab.this) {
2045 if (mCapture == null) {
2046 return;
2047 }
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002048 ByteBuffer buffer = ByteBuffer.wrap(blob);
Michael Kolbd837a112011-08-09 14:00:18 -07002049 try {
2050 mCapture.copyPixelsFromBuffer(buffer);
2051 } catch (RuntimeException rex) {
2052 Log.e(LOGTAG, "Load capture has mismatched sizes; buffer: "
2053 + buffer.capacity() + " blob: " + blob.length
2054 + "capture: " + mCapture.getByteCount());
2055 throw rex;
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002056 }
John Reck1cf4b792011-07-26 10:22:22 -07002057 }
2058 }
2059
John Reck52be4782011-08-26 15:37:29 -07002060 @Override
2061 public String toString() {
2062 StringBuilder builder = new StringBuilder(100);
2063 builder.append(mId);
2064 builder.append(") has parent: ");
2065 if (getParent() != null) {
2066 builder.append("true[");
2067 builder.append(getParent().getId());
2068 builder.append("]");
2069 } else {
2070 builder.append("false");
2071 }
2072 builder.append(", incog: ");
2073 builder.append(isPrivateBrowsingEnabled());
2074 if (!isPrivateBrowsingEnabled()) {
2075 builder.append(", title: ");
2076 builder.append(getTitle());
2077 builder.append(", url: ");
2078 builder.append(getUrl());
2079 }
2080 return builder.toString();
2081 }
2082
Ze G Riande2a675c22015-06-03 11:15:24 -07002083 // dertermines if the tab contains a dislled page
2084 public boolean isDistilled() {
2085 if (!BrowserCommandLine.hasSwitch("reader-mode")) {
2086 return false;
2087 }
2088 try {
2089 return DomDistillerUtils.isUrlDistilled(getUrl());
2090 } catch (Exception e) {
2091 return false;
2092 }
2093 }
2094
2095 //determines if the tab contains a distillable page
2096 public boolean isDistillable() {
2097 if (!BrowserCommandLine.hasSwitch("reader-mode")) {
2098 mIsDistillable = false;
2099 return mIsDistillable;
2100 }
2101 final ValueCallback<String> onIsDistillable = new ValueCallback<String>() {
2102 @Override
2103 public void onReceiveValue(String str) {
2104 mIsDistillable = Boolean.parseBoolean(str);
2105 }
2106 };
2107
2108 if (isDistilled()) {
2109 mIsDistillable = true;
2110 return mIsDistillable;
2111 }
2112
2113 try {
2114 DomDistillerUtils.isWebViewDistillable(getWebView(), onIsDistillable);
2115 } catch (Exception e) {
2116 mIsDistillable = false;
2117 }
2118
2119 return mIsDistillable;
2120 }
2121
2122 // Function that sets the mIsDistillable variable
2123 public void setIsDistillable(boolean value) {
2124 if (!BrowserCommandLine.hasSwitch("reader-mode")) {
2125 mIsDistillable = false;
2126 }
2127 mIsDistillable = value;
2128 }
2129
2130 // Function that returns the distilled url of the current url
2131 public String getDistilledUrl() {
2132 if (getUrl() != null) {
2133 return DomDistillerUtils.getDistilledUrl(getUrl());
2134 }
2135 return new String();
2136 }
2137
2138 // function that returns the non-distilled version of the current url
2139 public String getNonDistilledUrl() {
2140 if (getUrl() != null) {
2141 return DomDistillerUtils.getOriginalUrlFromDistilledUrl(getUrl());
2142 }
2143 return new String();
2144 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002145}