blob: f1444c8f32c875a7656ccae4e5d383a32f6a2488 [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;
611 view.getSnapshotIds(new ValueCallback <List<Integer>>() {
612 @Override
613 public void onReceiveValue(List<Integer> ids) {
614 int currentTabIdx = mWebViewController.getTabControl().getCurrentPosition();
615 for (Integer id : ids) {
616 if (getTabIdxFromCaptureIdx(id) == currentTabIdx &&
617 getNavIdxFromCaptureIdx(id) >= maxIdx) {
618 wv.deleteSnapshot(id);
619 }
620 }
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700621 }
Vivek Sekhar53ef8932015-06-18 16:51:43 -0700622 });
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700623 }
Pankaj Garg62bc7912015-04-14 16:08:59 -0700624
625 @Override
626 public void onKeyboardStateChange(boolean popup) {
Vivek Sekhard4de6162015-07-21 15:01:45 -0700627 boolean keyboardWasShowing = isKeyboardShowing();
Sagar Dhawan33551ff2015-07-08 17:24:44 -0700628 mIsKeyboardUp = popup;
Vivek Sekhard4de6162015-07-21 15:01:45 -0700629 Controller controller = (Controller)mWebViewController;
630 BaseUi ui = (BaseUi) controller.getUi();
631 // lock the title bar
632 if (popup)
633 ui.getTitleBar().showTopControls(true);
634 if (keyboardWasShowing && popup)
635 ui.getTitleBar().enableTopControls(true);
Pankaj Garg62bc7912015-04-14 16:08:59 -0700636 if (BrowserSettings.getInstance().useFullscreen()) {
Pankaj Garg62bc7912015-04-14 16:08:59 -0700637 ui.forceDisableFullscreenMode(popup);
638 }
639 }
Vivek Sekharae3b1792015-08-03 12:26:41 -0700640
641 @Override
642 public void onAttachInterstitialPage(WebView mWebView) {
643 Controller controller = (Controller)mWebViewController;
644 BaseUi ui = (BaseUi) controller.getUi();
645 ui.getTitleBar().showTopControls(false);
646 }
647
648 @Override
649 public void onDetachInterstitialPage(WebView mWebView) {
650 Controller controller = (Controller)mWebViewController;
651 BaseUi ui = (BaseUi) controller.getUi();
652 ui.getTitleBar().enableTopControls(true);
653 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700654 };
655
John Reck1cf4b792011-07-26 10:22:22 -0700656 private void syncCurrentState(WebView view, String url) {
657 // Sync state (in case of stop/timeout)
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800658
Axesh R. Ajmerad04cad12015-08-12 12:02:44 -0700659
660
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800661 if (mReceivedError) {
662 mCurrentState.mUrl = url;
663 mCurrentState.mOriginalUrl = url;
Axesh R. Ajmerad04cad12015-08-12 12:02:44 -0700664 } else if (view.isPrivateBrowsingEnabled() &&
665 !TextUtils.isEmpty(url) &&
666 url.contains(Controller.INCOGNITO_URI)) {
667 mCurrentState.mUrl = mCurrentState.mOriginalUrl = "";
668 }
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -0700669
Axesh R. Ajmerad04cad12015-08-12 12:02:44 -0700670 else {
671 mCurrentState.mUrl = view.getUrl();
672 mCurrentState.mOriginalUrl = view.getOriginalUrl();
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800673 }
674
John Reck1cf4b792011-07-26 10:22:22 -0700675 if (mCurrentState.mUrl == null) {
John Reck8bcafc12011-08-29 16:43:02 -0700676 mCurrentState.mUrl = "";
John Reck1cf4b792011-07-26 10:22:22 -0700677 }
John Reck1cf4b792011-07-26 10:22:22 -0700678 mCurrentState.mTitle = view.getTitle();
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800679
680
John Reck1cf4b792011-07-26 10:22:22 -0700681 if (!URLUtil.isHttpsUrl(mCurrentState.mUrl)) {
682 // In case we stop when loading an HTTPS page from an HTTP page
683 // but before a provisional load occurred
Steve Block2466eff2011-10-03 15:33:09 +0100684 mCurrentState.mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
John Reck1cf4b792011-07-26 10:22:22 -0700685 }
John Reck502a3532011-08-16 14:21:46 -0700686 mCurrentState.mIncognito = view.isPrivateBrowsingEnabled();
John Reck1cf4b792011-07-26 10:22:22 -0700687 }
688
Pankaj Garg21dad562015-07-02 17:17:24 -0700689 public String getTouchIconUrl() {
690 return mTouchIconUrl;
691 }
Tarun Nainani8eb00912014-07-17 12:28:32 -0700692
Sagar Dhawan33551ff2015-07-08 17:24:44 -0700693 public boolean isKeyboardShowing() {
694 Controller controller = (Controller)mWebViewController;
695 return (mIsKeyboardUp || controller.getUi().isEditingUrl());
696 }
697
Tarun Nainani8eb00912014-07-17 12:28:32 -0700698 public boolean isTabFullScreen() {
699 return mFullScreen;
700 }
701
Vivek Sekharf96064b2014-07-28 16:32:34 -0700702 protected void setTabFullscreen(boolean fullScreen) {
Tarun Nainani8eb00912014-07-17 12:28:32 -0700703 Controller controller = (Controller)mWebViewController;
Sudheer Koganti24766882014-10-02 10:58:09 -0700704 controller.getUi().showFullscreen(fullScreen);
Tarun Nainani8eb00912014-07-17 12:28:32 -0700705 mFullScreen = fullScreen;
Vivek Sekharf96064b2014-07-28 16:32:34 -0700706 }
707
Sudheer Koganti24766882014-10-02 10:58:09 -0700708 public boolean exitFullscreen() {
709 if (mFullScreen) {
710 Controller controller = (Controller)mWebViewController;
711 controller.getUi().showFullscreen(false);
712 if (getWebView() != null)
713 getWebView().exitFullscreen();
714 mFullScreen = false;
715 return true;
716 }
717 return false;
718 }
719
720
721
722
Grace Kloba22ac16e2009-10-07 18:00:23 -0700723 // -------------------------------------------------------------------------
724 // WebChromeClient implementation for the main WebView
725 // -------------------------------------------------------------------------
726
727 private final WebChromeClient mWebChromeClient = new WebChromeClient() {
728 // Helper method to create a new tab or sub window.
729 private void createWindow(final boolean dialog, final Message msg) {
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700730 this.createWindow(dialog, msg, null, false);
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700731 }
732
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700733 private void createWindow(final boolean dialog, final Message msg, final String url,
734 final boolean opener_suppressed) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700735 WebView.WebViewTransport transport =
736 (WebView.WebViewTransport) msg.obj;
737 if (dialog) {
738 createSubWindow();
Michael Kolb8233fac2010-10-26 16:08:53 -0700739 mWebViewController.attachSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700740 transport.setWebView(mSubView);
741 } else {
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700742 final Tab newTab = mWebViewController.openTab(url,
John Reck5949c662011-05-27 09:52:29 -0700743 Tab.this, true, true);
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700744 // This is special case for rendering links on a webpage in
745 // a new tab. If opener is suppressed, the WebContents created
746 // by the content layer are not fully initialized. This check
747 // will prevent content layer from overriding WebContents
748 // created by new tab with the uninitialized instance.
749 if (!opener_suppressed) {
750 transport.setWebView(newTab.getWebView());
751 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700752 }
753 msg.sendToTarget();
754 }
755
756 @Override
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700757 public void toggleFullscreenModeForTab(boolean enterFullscreen) {
758 if (mWebViewController instanceof Controller) {
Vivek Sekharf96064b2014-07-28 16:32:34 -0700759 setTabFullscreen(enterFullscreen);
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700760 }
761 }
762
763 @Override
Tarun Nainani8eb00912014-07-17 12:28:32 -0700764 public void onOffsetsForFullscreenChanged(float topControlsOffsetYPix,
765 float contentOffsetYPix,
766 float overdrawBottomHeightPix) {
767 if (mWebViewController instanceof Controller) {
Vivek Sekhar8ee3abb2014-07-14 12:32:05 -0700768 Controller controller = (Controller)mWebViewController;
Tarun Nainani8eb00912014-07-17 12:28:32 -0700769 controller.getUi().translateTitleBar(topControlsOffsetYPix);
Vivek Sekhar11b40062015-06-24 11:49:04 -0700770 // Resize the viewport if top controls is not visible
Vivek Sekhar2ee19a32015-07-02 17:03:57 -0700771 if (mMainView != null &&
772 (topControlsOffsetYPix == 0.0f || contentOffsetYPix == 0.0f))
Vivek Sekhar11b40062015-06-24 11:49:04 -0700773 ((BrowserWebView)mMainView).enableTopControls(
774 (topControlsOffsetYPix == 0.0f) ? true : false);
Vivek Sekhar8ee3abb2014-07-14 12:32:05 -0700775 }
776 }
777
778 @Override
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700779 public boolean isTabFullScreen() {
Vivek Sekharf96064b2014-07-28 16:32:34 -0700780 return mFullScreen;
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700781 }
782
783 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700784 public boolean onCreateWindow(WebView view, final boolean dialog,
785 final boolean userGesture, final Message resultMsg) {
786 // only allow new window or sub window for the foreground case
787 if (!mInForeground) {
788 return false;
789 }
790 // Short-circuit if we can't create any more tabs or sub windows.
791 if (dialog && mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -0700792 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700793 .setTitle(R.string.too_many_subwindows_dialog_title)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200794 .setIconAttribute(android.R.attr.alertDialogIcon)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700795 .setMessage(R.string.too_many_subwindows_dialog_message)
796 .setPositiveButton(R.string.ok, null)
797 .show();
798 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700799 } else if (!mWebViewController.getTabControl().canCreateNewTab()) {
Michael Kolb14612442011-06-24 13:06:29 -0700800 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700801 .setTitle(R.string.too_many_windows_dialog_title)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200802 .setIconAttribute(android.R.attr.alertDialogIcon)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700803 .setMessage(R.string.too_many_windows_dialog_message)
804 .setPositiveButton(R.string.ok, null)
805 .show();
806 return false;
807 }
808
809 // Short-circuit if this was a user gesture.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800810 if (userGesture || !mSettings.blockPopupWindows()) {
Tarun Nainani4f5137d2015-04-16 17:26:18 -0700811 WebView.WebViewTransport transport =
812 (WebView.WebViewTransport) resultMsg.obj;
813 CreateWindowParams windowParams = transport.getCreateWindowParams();
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700814 if (windowParams.mOpenerSuppressed) {
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700815 createWindow(dialog, resultMsg, windowParams.mURL, true);
816 // This is special case for rendering links on a webpage in
817 // a new tab. If opener is suppressed, the WebContents created
818 // by the content layer are not fully initialized. Returning false
819 // will prevent content layer from overriding WebContents
820 // created by new tab with the uninitialized instance.
821 return false;
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700822 }
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700823
824 createWindow(dialog, resultMsg);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700825 return true;
826 }
827
Tarun Nainani4f5137d2015-04-16 17:26:18 -0700828 createWindow(dialog, resultMsg);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700829 return true;
830 }
831
832 @Override
Patrick Scotteb5061b2009-11-18 15:00:30 -0500833 public void onRequestFocus(WebView view) {
834 if (!mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700835 mWebViewController.switchToTab(Tab.this);
Patrick Scotteb5061b2009-11-18 15:00:30 -0500836 }
837 }
838
839 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700840 public void onCloseWindow(WebView window) {
Michael Kolbc831b632011-05-11 09:30:34 -0700841 if (mParent != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700842 // JavaScript can only close popup window.
843 if (mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700844 mWebViewController.switchToTab(mParent);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700845 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700846 mWebViewController.closeTab(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700847 }
848 }
849
850 @Override
851 public void onProgressChanged(WebView view, int newProgress) {
John Reck30c714c2010-12-16 17:30:34 -0800852 mPageLoadProgress = newProgress;
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800853 if (newProgress == 100) {
854 mInPageLoad = false;
855 }
John Reck30c714c2010-12-16 17:30:34 -0800856 mWebViewController.onProgressChanged(Tab.this);
Michael Kolb72864272012-05-03 15:42:15 -0700857 if (mUpdateThumbnail && newProgress == 100) {
858 mUpdateThumbnail = false;
859 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700860 }
861
862 @Override
Leon Scroggins21d9b902010-03-11 09:33:11 -0500863 public void onReceivedTitle(WebView view, final String title) {
John Reck30c714c2010-12-16 17:30:34 -0800864 mCurrentState.mTitle = title;
Michael Kolb8233fac2010-10-26 16:08:53 -0700865 mWebViewController.onReceivedTitle(Tab.this, title);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700866 }
867
868 @Override
869 public void onReceivedIcon(WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -0800870 mCurrentState.mFavicon = icon;
Michael Kolb8233fac2010-10-26 16:08:53 -0700871 mWebViewController.onFavicon(Tab.this, view, icon);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700872 }
873
874 @Override
875 public void onReceivedTouchIconUrl(WebView view, String url,
876 boolean precomposed) {
Michael Kolb14612442011-06-24 13:06:29 -0700877 final ContentResolver cr = mContext.getContentResolver();
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400878 // Let precomposed icons take precedence over non-composed
879 // icons.
880 if (precomposed && mTouchIconLoader != null) {
881 mTouchIconLoader.cancel(false);
882 mTouchIconLoader = null;
883 }
884 // Have only one async task at a time.
885 if (mTouchIconLoader == null) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700886 mTouchIconLoader = new DownloadTouchIcon(Tab.this,
Michael Kolb14612442011-06-24 13:06:29 -0700887 mContext, cr, view);
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400888 mTouchIconLoader.execute(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700889 }
Pankaj Garg21dad562015-07-02 17:17:24 -0700890 mTouchIconUrl = url;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700891 }
892
893 @Override
894 public void onShowCustomView(View view,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800895 CustomViewCallback callback) {
Michael Kolb14612442011-06-24 13:06:29 -0700896 Activity activity = mWebViewController.getActivity();
897 if (activity != null) {
898 onShowCustomView(view, activity.getRequestedOrientation(), callback);
899 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400900 }
901
902 @Override
903 public void onShowCustomView(View view, int requestedOrientation,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800904 CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700905 if (mInForeground) mWebViewController.showCustomView(Tab.this, view,
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400906 requestedOrientation, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700907 }
908
909 @Override
910 public void onHideCustomView() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700911 if (mInForeground) mWebViewController.hideCustomView();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700912 }
913
914 /**
915 * The origin has exceeded its database quota.
916 * @param url the URL that exceeded the quota
917 * @param databaseIdentifier the identifier of the database on which the
918 * transaction that caused the quota overflow was run
919 * @param currentQuota the current quota for the origin.
920 * @param estimatedSize the estimated size of the database.
921 * @param totalUsedQuota is the sum of all origins' quota.
922 * @param quotaUpdater The callback to run when a decision to allow or
923 * deny quota has been made. Don't forget to call this!
924 */
925 @Override
926 public void onExceededDatabaseQuota(String url,
927 String databaseIdentifier, long currentQuota, long estimatedSize,
928 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -0700929 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -0700930 .onExceededDatabaseQuota(url, databaseIdentifier,
931 currentQuota, estimatedSize, totalUsedQuota,
932 quotaUpdater);
933 }
934
935 /**
936 * The Application Cache has exceeded its max size.
937 * @param spaceNeeded is the amount of disk space that would be needed
938 * in order for the last appcache operation to succeed.
939 * @param totalUsedQuota is the sum of all origins' quota.
940 * @param quotaUpdater A callback to inform the WebCore thread that a
941 * new app cache size is available. This callback must always
942 * be executed at some point to ensure that the sleeping
943 * WebCore thread is woken up.
944 */
945 @Override
946 public void onReachedMaxAppCacheSize(long spaceNeeded,
947 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -0700948 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -0700949 .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota,
950 quotaUpdater);
951 }
952
Ben Murdoch65acc352009-11-19 18:16:04 +0000953 /* Adds a JavaScript error message to the system log and if the JS
954 * console is enabled in the about:debug options, to that console
955 * also.
Ben Murdochc42addf2010-01-28 15:19:59 +0000956 * @param consoleMessage the message object.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700957 */
958 @Override
Ben Murdochc42addf2010-01-28 15:19:59 +0000959 public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Jeff Hamilton47654f42010-09-07 09:57:51 -0500960 // Don't log console messages in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -0700961 if (isPrivateBrowsingEnabled()) return true;
Jeff Hamilton47654f42010-09-07 09:57:51 -0500962
Ben Murdochc42addf2010-01-28 15:19:59 +0000963 String message = "Console: " + consoleMessage.message() + " "
964 + consoleMessage.sourceId() + ":"
965 + consoleMessage.lineNumber();
966
967 switch (consoleMessage.messageLevel()) {
968 case TIP:
969 Log.v(CONSOLE_LOGTAG, message);
970 break;
971 case LOG:
972 Log.i(CONSOLE_LOGTAG, message);
973 break;
974 case WARNING:
975 Log.w(CONSOLE_LOGTAG, message);
976 break;
977 case ERROR:
978 Log.e(CONSOLE_LOGTAG, message);
979 break;
980 case DEBUG:
981 Log.d(CONSOLE_LOGTAG, message);
982 break;
983 }
984
985 return true;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700986 }
987
988 /**
989 * Ask the browser for an icon to represent a <video> element.
990 * This icon will be used if the Web page did not specify a poster attribute.
991 * @return Bitmap The icon or null if no such icon is available.
992 */
993 @Override
994 public Bitmap getDefaultVideoPoster() {
995 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700996 return mWebViewController.getDefaultVideoPoster();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700997 }
998 return null;
999 }
1000
1001 /**
1002 * Ask the host application for a custom progress view to show while
1003 * a <video> is loading.
1004 * @return View The progress view.
1005 */
1006 @Override
1007 public View getVideoLoadingProgressView() {
1008 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001009 return mWebViewController.getVideoLoadingProgressView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001010 }
1011 return null;
1012 }
1013
1014 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00001015 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001016 if (mInForeground) {
Ben Murdoch8cad4132012-01-11 10:56:43 +00001017 mWebViewController.openFileChooser(uploadMsg, acceptType, capture);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001018 } else {
1019 uploadMsg.onReceiveValue(null);
1020 }
1021 }
1022
Vivek Sekharb54614f2014-05-01 19:03:37 -07001023 @Override
1024 public void showFileChooser(ValueCallback<String[]> uploadFilePaths, String acceptTypes,
1025 boolean capture) {
1026 if (mInForeground) {
1027 mWebViewController.showFileChooser(uploadFilePaths, acceptTypes, capture);
1028 } else {
1029 uploadFilePaths.onReceiveValue(null);
1030 }
1031 }
1032
Grace Kloba22ac16e2009-10-07 18:00:23 -07001033 /**
1034 * Deliver a list of already-visited URLs
1035 */
1036 @Override
1037 public void getVisitedHistory(final ValueCallback<String[]> callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001038 mWebViewController.getVisitedHistory(callback);
1039 }
Ben Murdoch8029a772010-11-16 11:58:21 +00001040
1041 @Override
1042 public void setupAutoFill(Message message) {
1043 // Prompt the user to set up their profile.
1044 final Message msg = message;
Michael Kolb14612442011-06-24 13:06:29 -07001045 AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
1046 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
Ben Murdoch1d676b62011-01-17 12:54:24 +00001047 Context.LAYOUT_INFLATER_SERVICE);
1048 final View layout = inflater.inflate(R.layout.setup_autofill_dialog, null);
1049
1050 builder.setView(layout)
1051 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1052 @Override
1053 public void onClick(DialogInterface dialog, int id) {
1054 CheckBox disableAutoFill = (CheckBox) layout.findViewById(
1055 R.id.setup_autofill_dialog_disable_autofill);
1056
1057 if (disableAutoFill.isChecked()) {
1058 // Disable autofill and show a toast with how to turn it on again.
John Reck35e9dd62011-04-25 09:01:54 -07001059 mSettings.setAutofillEnabled(false);
Michael Kolb14612442011-06-24 13:06:29 -07001060 Toast.makeText(mContext,
Ben Murdoch1d676b62011-01-17 12:54:24 +00001061 R.string.autofill_setup_dialog_negative_toast,
1062 Toast.LENGTH_LONG).show();
1063 } else {
1064 // Take user to the AutoFill profile editor. When they return,
1065 // we will send the message that we pass here which will trigger
1066 // the form to get filled out with their new profile.
1067 mWebViewController.setupAutoFill(msg);
1068 }
1069 }
1070 })
1071 .setNegativeButton(R.string.cancel, null)
1072 .show();
Ben Murdoch8029a772010-11-16 11:58:21 +00001073 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001074 };
1075
1076 // -------------------------------------------------------------------------
1077 // WebViewClient implementation for the sub window
1078 // -------------------------------------------------------------------------
1079
1080 // Subclass of WebViewClient used in subwindows to notify the main
1081 // WebViewClient of certain WebView activities.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001082 private static class SubWindowClient extends WebViewClient {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001083 // The main WebViewClient.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001084 private final WebViewClient mClient;
Michael Kolb8233fac2010-10-26 16:08:53 -07001085 private final WebViewController mController;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001086
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001087 SubWindowClient(WebViewClient client, WebViewController controller) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001088 mClient = client;
Michael Kolb8233fac2010-10-26 16:08:53 -07001089 mController = controller;
Leon Scroggins III211ba542010-04-19 13:21:13 -04001090 }
1091 @Override
1092 public void onPageStarted(WebView view, String url, Bitmap favicon) {
1093 // Unlike the others, do not call mClient's version, which would
1094 // change the progress bar. However, we do want to remove the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001095 // find or select dialog.
Michael Kolb8233fac2010-10-26 16:08:53 -07001096 mController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001097 }
1098 @Override
1099 public void doUpdateVisitedHistory(WebView view, String url,
1100 boolean isReload) {
1101 mClient.doUpdateVisitedHistory(view, url, isReload);
1102 }
1103 @Override
1104 public boolean shouldOverrideUrlLoading(WebView view, String url) {
1105 return mClient.shouldOverrideUrlLoading(view, url);
1106 }
1107 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -07001108 public void onReceivedHttpAuthRequest(WebView view,
1109 HttpAuthHandler handler, String host, String realm) {
1110 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
1111 }
1112 @Override
1113 public void onFormResubmission(WebView view, Message dontResend,
1114 Message resend) {
1115 mClient.onFormResubmission(view, dontResend, resend);
1116 }
1117 @Override
1118 public void onReceivedError(WebView view, int errorCode,
1119 String description, String failingUrl) {
1120 mClient.onReceivedError(view, errorCode, description, failingUrl);
1121 }
1122 @Override
1123 public boolean shouldOverrideKeyEvent(WebView view,
1124 android.view.KeyEvent event) {
1125 return mClient.shouldOverrideKeyEvent(view, event);
1126 }
1127 @Override
1128 public void onUnhandledKeyEvent(WebView view,
1129 android.view.KeyEvent event) {
1130 mClient.onUnhandledKeyEvent(view, event);
1131 }
1132 }
1133
1134 // -------------------------------------------------------------------------
1135 // WebChromeClient implementation for the sub window
1136 // -------------------------------------------------------------------------
1137
1138 private class SubWindowChromeClient extends WebChromeClient {
1139 // The main WebChromeClient.
1140 private final WebChromeClient mClient;
1141
1142 SubWindowChromeClient(WebChromeClient client) {
1143 mClient = client;
1144 }
1145 @Override
1146 public void onProgressChanged(WebView view, int newProgress) {
1147 mClient.onProgressChanged(view, newProgress);
1148 }
1149 @Override
1150 public boolean onCreateWindow(WebView view, boolean dialog,
1151 boolean userGesture, android.os.Message resultMsg) {
1152 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
1153 }
1154 @Override
1155 public void onCloseWindow(WebView window) {
1156 if (window != mSubView) {
1157 Log.e(LOGTAG, "Can't close the window");
1158 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001159 mWebViewController.dismissSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001160 }
1161 }
1162
1163 // -------------------------------------------------------------------------
1164
1165 // Construct a new tab
Michael Kolb7bcafde2011-05-09 13:55:59 -07001166 Tab(WebViewController wvcontroller, WebView w) {
John Reck1cf4b792011-07-26 10:22:22 -07001167 this(wvcontroller, w, null);
1168 }
1169
1170 Tab(WebViewController wvcontroller, Bundle state) {
1171 this(wvcontroller, null, state);
1172 }
1173
1174 Tab(WebViewController wvcontroller, WebView w, Bundle state) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001175 mWebViewController = wvcontroller;
Michael Kolb14612442011-06-24 13:06:29 -07001176 mContext = mWebViewController.getContext();
John Reck35e9dd62011-04-25 09:01:54 -07001177 mSettings = BrowserSettings.getInstance();
Michael Kolb14612442011-06-24 13:06:29 -07001178 mDataController = DataController.getInstance(mContext);
1179 mCurrentState = new PageState(mContext, w != null
John Reck847b5322011-04-14 17:02:18 -07001180 ? w.isPrivateBrowsingEnabled() : false);
Tarun Nainani8084c822014-06-25 13:38:06 -07001181 setTimeStamp();
Michael Kolb8233fac2010-10-26 16:08:53 -07001182 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001183 mInForeground = false;
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001184 mWebViewDestroyedByMemoryMonitor = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001185
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001186 mDownloadListener = new BrowserDownloadListener() {
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001187 public void onDownloadStart(String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001188 String contentDisposition, String mimetype, String referer,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001189 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001190 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001191 mimetype, referer, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001192 }
1193 };
1194
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001195 mCaptureWidth = mContext.getResources().getDimensionPixelSize(R.dimen.tab_thumbnail_width);
1196 mCaptureHeight =mContext.getResources().getDimensionPixelSize(R.dimen.tab_thumbnail_height);
1197
1198 initCaptureBitmap();
1199
John Reck1cf4b792011-07-26 10:22:22 -07001200 restoreState(state);
John Reck52be4782011-08-26 15:37:29 -07001201 if (getId() == -1) {
1202 mId = TabControl.getNextId();
1203 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001204 setWebView(w);
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -07001205
1206 UI ui = ((Controller)mWebViewController).getUi();
1207 if (ui instanceof BaseUi) {
1208 TitleBar titleBar = ((BaseUi)ui).getTitleBar();
1209 if (titleBar != null) {
1210 NavigationBarBase navBar = titleBar.getNavigationBar();
1211 navBar.showCurrentFavicon(this); // Show the default Favicon while loading a new page
1212 }
1213 }
1214
Michael Kolb9ef259a2011-07-12 15:33:08 -07001215 mHandler = new Handler() {
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001216 @Override
Michael Kolb9ef259a2011-07-12 15:33:08 -07001217 public void handleMessage(Message m) {
John Reck1cf4b792011-07-26 10:22:22 -07001218 switch (m.what) {
1219 case MSG_CAPTURE:
1220 capture();
1221 break;
1222 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001223 }
1224 };
Pankaj Garg1c13cab2015-05-12 11:52:17 -07001225
1226 mFirstPixelObservable = new Observable();
1227 mFirstPixelObservable.set(false);
1228 mTabHistoryUpdateObservable = new Observable();
John Reck1cf4b792011-07-26 10:22:22 -07001229 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001230
Pankaj Gargb4b4f012015-08-04 16:19:14 -07001231 public void initCaptureBitmap() {
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001232 mCapture = Bitmap.createBitmap(mCaptureWidth, mCaptureHeight, Bitmap.Config.RGB_565);
1233 mCapture.eraseColor(Color.WHITE);
Michael Kolb72864272012-05-03 15:42:15 -07001234 }
1235
Mathew Inwoode09305e2011-09-02 12:03:26 +01001236 /**
1237 * This is used to get a new ID when the tab has been preloaded, before it is displayed and
1238 * added to TabControl. Preloaded tabs can be created before restoreInstanceState, leading
1239 * to overlapping IDs between the preloaded and restored tabs.
1240 */
1241 public void refreshIdAfterPreload() {
1242 mId = TabControl.getNextId();
1243 }
1244
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001245 public void setController(WebViewController ctl) {
1246 mWebViewController = ctl;
1247
John Reck1cf4b792011-07-26 10:22:22 -07001248 if (mWebViewController.shouldCaptureThumbnails()) {
1249 synchronized (Tab.this) {
1250 if (mCapture == null) {
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001251 initCaptureBitmap();
1252 if (mInForeground && !mHandler.hasMessages(MSG_CAPTURE)) {
1253 mHandler.sendEmptyMessageDelayed(MSG_CAPTURE, CAPTURE_DELAY);
John Reck1cf4b792011-07-26 10:22:22 -07001254 }
1255 }
1256 }
1257 } else {
1258 synchronized (Tab.this) {
1259 mCapture = null;
1260 deleteThumbnail();
1261 }
1262 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001263 }
1264
Michael Kolbc831b632011-05-11 09:30:34 -07001265 public long getId() {
1266 return mId;
1267 }
1268
Michael Kolb91911a22012-01-17 11:21:25 -08001269 void setWebView(WebView w) {
1270 setWebView(w, true);
1271 }
1272
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001273 public boolean isNativeActive(){
1274 if (mMainView == null)
1275 return false;
1276 return true;
1277 }
1278
1279 public void setTimeStamp(){
1280 Date d = new Date();
1281 timestamp = (new Timestamp(d.getTime()));
1282 }
1283
1284 public Timestamp getTimestamp() {
1285 return timestamp;
1286 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001287 /**
1288 * Sets the WebView for this tab, correctly removing the old WebView from
1289 * the container view.
1290 */
Michael Kolb91911a22012-01-17 11:21:25 -08001291 void setWebView(WebView w, boolean restore) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001292 if (mMainView == w) {
1293 return;
1294 }
Michael Kolba713ec82010-11-29 17:27:06 -08001295
Michael Kolba713ec82010-11-29 17:27:06 -08001296 mWebViewController.onSetWebView(this, w);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001297
John Reck1cf4b792011-07-26 10:22:22 -07001298 if (mMainView != null) {
John Reck8ee633f2011-08-09 16:00:35 -07001299 mMainView.setPictureListener(null);
John Reck1cf4b792011-07-26 10:22:22 -07001300 if (w != null) {
1301 syncCurrentState(w, null);
1302 } else {
Panos Thomasa9a5a582014-03-18 19:20:08 -07001303 mCurrentState = new PageState(mContext, mMainView.isPrivateBrowsingEnabled());
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001304
1305 if (mWebViewDestroyedByMemoryMonitor) {
1306 /*
1307 * If tab was destroyed as a result of the MemoryMonitor
1308 * then we need to restore the state properties
1309 * from the old WebView (mMainView)
1310 */
1311 syncCurrentState(mMainView, null);
1312 mWebViewDestroyedByMemoryMonitor = false;
1313 }
John Reck1cf4b792011-07-26 10:22:22 -07001314 }
1315 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001316 // set the new one
1317 mMainView = w;
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001318
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001319 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001320 if (mMainView != null) {
1321 mMainView.setWebViewClient(mWebViewClient);
1322 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001323 // Attach DownloadManager so that downloads can start in an active
1324 // or a non-active window. This can happen when going to a site that
1325 // does a redirect after a period of time. The user could have
1326 // switched to another tab while waiting for the download to start.
1327 mMainView.setDownloadListener(mDownloadListener);
John Reck8ee633f2011-08-09 16:00:35 -07001328 TabControl tc = mWebViewController.getTabControl();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001329 if (tc != null /*&& tc.getOnThumbnailUpdatedListener() != null*/) {
John Reck8ee633f2011-08-09 16:00:35 -07001330 mMainView.setPictureListener(this);
1331 }
Michael Kolb91911a22012-01-17 11:21:25 -08001332 if (restore && (mSavedState != null)) {
John Reck8b9bb8b2012-03-08 13:19:40 -08001333 restoreUserAgent();
John Reck6c2e2f32011-08-22 13:41:23 -07001334 WebBackForwardList restoredState
1335 = mMainView.restoreState(mSavedState);
1336 if (restoredState == null || restoredState.getSize() == 0) {
1337 Log.w(LOGTAG, "Failed to restore WebView state!");
1338 loadUrl(mCurrentState.mOriginalUrl, null);
1339 }
John Reck1cf4b792011-07-26 10:22:22 -07001340 mSavedState = null;
1341 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001342 }
1343 }
1344
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001345 public void destroyThroughMemoryMonitor() {
1346 mWebViewDestroyedByMemoryMonitor = true;
1347 destroy();
1348 }
1349
Grace Kloba22ac16e2009-10-07 18:00:23 -07001350 /**
1351 * Destroy the tab's main WebView and subWindow if any
1352 */
1353 void destroy() {
Tarun Nainani2c1dd7c2014-07-05 16:40:12 -07001354
Matthew Huib7f2e9c2014-04-16 11:12:37 -04001355 if (mPostponeDestroy) {
1356 mShouldDestroy = true;
1357 return;
1358 }
1359 mShouldDestroy = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001360 if (mMainView != null) {
1361 dismissSubWindow();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001362 // save the WebView to call destroy() after detach it from the tab
Vivek Sekhar53ef8932015-06-18 16:51:43 -07001363 final WebView webView = mMainView;
Vivek Sekhar2ee19a32015-07-02 17:03:57 -07001364 setWebView(null);
Site Mao61b68212015-07-16 10:56:31 -07001365 if (!mWebViewDestroyedByMemoryMonitor && !BaseUi.isUiLowPowerMode()) {
Vivek Sekhar2ee19a32015-07-02 17:03:57 -07001366 // Tabs can be reused with new instance of WebView so delete the snapshots
Vivek Sekhar53ef8932015-06-18 16:51:43 -07001367 webView.getSnapshotIds(new ValueCallback<List<Integer>>() {
1368 @Override
1369 public void onReceiveValue(List<Integer> ids) {
1370 int currentTabIdx = mWebViewController.getTabControl().getCurrentPosition();
1371 for (Integer id : ids) {
1372 if (getTabIdxFromCaptureIdx(id) == currentTabIdx) {
1373 webView.deleteSnapshot(id);
1374 }
1375 }
Vivek Sekhar53ef8932015-06-18 16:51:43 -07001376 webView.destroy();
Pankaj Garg1c13cab2015-05-12 11:52:17 -07001377 }
Vivek Sekhar53ef8932015-06-18 16:51:43 -07001378 });
Vivek Sekhar2ee19a32015-07-02 17:03:57 -07001379 } else {
1380 webView.destroy();
Pankaj Garg1c13cab2015-05-12 11:52:17 -07001381 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001382 }
1383 }
1384
Matthew Huib7f2e9c2014-04-16 11:12:37 -04001385 private boolean mPostponeDestroy = false;
1386 private boolean mShouldDestroy = false;
1387
1388 public void postponeDestroy() {
1389 mPostponeDestroy = true;
1390 }
1391
1392 public void performPostponedDestroy() {
1393 mPostponeDestroy = false;
1394 if (mShouldDestroy) {
1395 destroy();
1396 }
1397 }
1398
Grace Kloba22ac16e2009-10-07 18:00:23 -07001399 /**
1400 * Remove the tab from the parent
1401 */
1402 void removeFromTree() {
1403 // detach the children
Michael Kolbc831b632011-05-11 09:30:34 -07001404 if (mChildren != null) {
1405 for(Tab t : mChildren) {
1406 t.setParent(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001407 }
1408 }
1409 // remove itself from the parent list
Michael Kolbc831b632011-05-11 09:30:34 -07001410 if (mParent != null) {
1411 mParent.mChildren.remove(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001412 }
Tarun Nainani8eb00912014-07-17 12:28:32 -07001413
1414 mCapture = null;
John Reck1cf4b792011-07-26 10:22:22 -07001415 deleteThumbnail();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001416 }
1417
1418 /**
1419 * Create a new subwindow unless a subwindow already exists.
1420 * @return True if a new subwindow was created. False if one already exists.
1421 */
1422 boolean createSubWindow() {
1423 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001424 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001425 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001426 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001427 mSubView.setWebChromeClient(new SubWindowChromeClient(
1428 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001429 // Set a different DownloadListener for the mSubView, since it will
1430 // just need to dismiss the mSubView, rather than close the Tab
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001431 mSubView.setDownloadListener(new BrowserDownloadListener() {
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001432 public void onDownloadStart(String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001433 String contentDisposition, String mimetype, String referer,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001434 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001435 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001436 contentDisposition, mimetype, referer, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001437 if (mSubView.copyBackForwardList().getSize() == 0) {
1438 // This subwindow was opened for the sole purpose of
1439 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001440 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001441 }
1442 }
1443 });
Michael Kolb14612442011-06-24 13:06:29 -07001444 mSubView.setOnCreateContextMenuListener(mWebViewController.getActivity());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001445 return true;
1446 }
1447 return false;
1448 }
1449
1450 /**
1451 * Dismiss the subWindow for the tab.
1452 */
1453 void dismissSubWindow() {
1454 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001455 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001456 mSubView.destroy();
1457 mSubView = null;
1458 mSubViewContainer = null;
1459 }
1460 }
1461
Grace Kloba22ac16e2009-10-07 18:00:23 -07001462
1463 /**
1464 * Set the parent tab of this tab.
1465 */
Michael Kolbc831b632011-05-11 09:30:34 -07001466 void setParent(Tab parent) {
John Reck52be4782011-08-26 15:37:29 -07001467 if (parent == this) {
1468 throw new IllegalStateException("Cannot set parent to self!");
1469 }
Michael Kolbc831b632011-05-11 09:30:34 -07001470 mParent = parent;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001471 // This tab may have been freed due to low memory. If that is the case,
Michael Kolbc831b632011-05-11 09:30:34 -07001472 // the parent tab id is already saved. If we are changing that id
Grace Kloba22ac16e2009-10-07 18:00:23 -07001473 // (most likely due to removing the parent tab) we must update the
Michael Kolbc831b632011-05-11 09:30:34 -07001474 // parent tab id in the saved Bundle.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001475 if (mSavedState != null) {
1476 if (parent == null) {
1477 mSavedState.remove(PARENTTAB);
1478 } else {
Michael Kolbc831b632011-05-11 09:30:34 -07001479 mSavedState.putLong(PARENTTAB, parent.getId());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001480 }
1481 }
John Reckb0a86db2011-05-24 14:05:58 -07001482
1483 // Sync the WebView useragent with the parent
1484 if (parent != null && mSettings.hasDesktopUseragent(parent.getWebView())
1485 != mSettings.hasDesktopUseragent(getWebView())) {
1486 mSettings.toggleDesktopUseragent(getWebView());
1487 }
John Reck52be4782011-08-26 15:37:29 -07001488
1489 if (parent != null && parent.getId() == getId()) {
1490 throw new IllegalStateException("Parent has same ID as child!");
1491 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001492 }
1493
1494 /**
Michael Kolbc831b632011-05-11 09:30:34 -07001495 * If this Tab was created through another Tab, then this method returns
1496 * that Tab.
1497 * @return the Tab parent or null
1498 */
1499 public Tab getParent() {
1500 return mParent;
1501 }
1502
1503 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001504 * When a Tab is created through the content of another Tab, then we
1505 * associate the Tabs.
1506 * @param child the Tab that was created from this Tab
1507 */
1508 void addChildTab(Tab child) {
Michael Kolbc831b632011-05-11 09:30:34 -07001509 if (mChildren == null) {
1510 mChildren = new Vector<Tab>();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001511 }
Michael Kolbc831b632011-05-11 09:30:34 -07001512 mChildren.add(child);
1513 child.setParent(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001514 }
1515
Michael Kolbc831b632011-05-11 09:30:34 -07001516 Vector<Tab> getChildren() {
1517 return mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001518 }
1519
1520 void resume() {
1521 if (mMainView != null) {
John Reck56c1fcf2011-08-17 10:15:16 -07001522 setupHwAcceleration(mMainView);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001523 mMainView.onResume();
1524 if (mSubView != null) {
1525 mSubView.onResume();
1526 }
1527 }
1528 }
1529
John Reck56c1fcf2011-08-17 10:15:16 -07001530 private void setupHwAcceleration(View web) {
1531 if (web == null) return;
1532 BrowserSettings settings = BrowserSettings.getInstance();
1533 if (settings.isHardwareAccelerated()) {
1534 web.setLayerType(View.LAYER_TYPE_NONE, null);
1535 } else {
1536 web.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
1537 }
1538 }
1539
Grace Kloba22ac16e2009-10-07 18:00:23 -07001540 void pause() {
1541 if (mMainView != null) {
1542 mMainView.onPause();
1543 if (mSubView != null) {
1544 mSubView.onPause();
1545 }
1546 }
1547 }
1548
1549 void putInForeground() {
John Reck8ee633f2011-08-09 16:00:35 -07001550 if (mInForeground) {
1551 return;
1552 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001553 mInForeground = true;
1554 resume();
Michael Kolb14612442011-06-24 13:06:29 -07001555 Activity activity = mWebViewController.getActivity();
1556 mMainView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001557 if (mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -07001558 mSubView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001559 }
Axesh R. Ajmerac6b5c322015-05-01 11:06:10 -07001560
Leon Scroggins1961ed22010-12-07 15:22:21 -05001561 mWebViewController.bookmarkedStatusHasChanged(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001562 }
1563
1564 void putInBackground() {
John Reck8ee633f2011-08-09 16:00:35 -07001565 if (!mInForeground) {
1566 return;
1567 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001568 mInForeground = false;
1569 pause();
1570 mMainView.setOnCreateContextMenuListener(null);
1571 if (mSubView != null) {
1572 mSubView.setOnCreateContextMenuListener(null);
1573 }
1574 }
1575
Michael Kolb8233fac2010-10-26 16:08:53 -07001576 boolean inForeground() {
1577 return mInForeground;
1578 }
1579
Grace Kloba22ac16e2009-10-07 18:00:23 -07001580 /**
1581 * Return the top window of this tab; either the subwindow if it is not
1582 * null or the main window.
1583 * @return The top window of this tab.
1584 */
1585 WebView getTopWindow() {
1586 if (mSubView != null) {
1587 return mSubView;
1588 }
1589 return mMainView;
1590 }
1591
1592 /**
1593 * Return the main window of this tab. Note: if a tab is freed in the
1594 * background, this can return null. It is only guaranteed to be
1595 * non-null for the current tab.
1596 * @return The main WebView of this tab.
1597 */
1598 WebView getWebView() {
1599 return mMainView;
1600 }
1601
Michael Kolba713ec82010-11-29 17:27:06 -08001602 void setViewContainer(View container) {
1603 mContainer = container;
1604 }
1605
Michael Kolb8233fac2010-10-26 16:08:53 -07001606 View getViewContainer() {
1607 return mContainer;
1608 }
1609
Grace Kloba22ac16e2009-10-07 18:00:23 -07001610 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001611 * Return whether private browsing is enabled for the main window of
1612 * this tab.
1613 * @return True if private browsing is enabled.
1614 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001615 boolean isPrivateBrowsingEnabled() {
John Reck502a3532011-08-16 14:21:46 -07001616 return mCurrentState.mIncognito;
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001617 }
1618
1619 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001620 * Return the subwindow of this tab or null if there is no subwindow.
1621 * @return The subwindow of this tab or null.
1622 */
1623 WebView getSubWebView() {
1624 return mSubView;
1625 }
1626
Michael Kolb1514bb72010-11-22 09:11:48 -08001627 void setSubWebView(WebView subView) {
1628 mSubView = subView;
1629 }
1630
Michael Kolb8233fac2010-10-26 16:08:53 -07001631 View getSubViewContainer() {
1632 return mSubViewContainer;
1633 }
1634
Michael Kolb1514bb72010-11-22 09:11:48 -08001635 void setSubViewContainer(View subViewContainer) {
1636 mSubViewContainer = subViewContainer;
1637 }
1638
Grace Kloba22ac16e2009-10-07 18:00:23 -07001639
1640 /**
1641 * @return The application id string
1642 */
1643 String getAppId() {
1644 return mAppId;
1645 }
1646
1647 /**
1648 * Set the application id string
1649 * @param id
1650 */
1651 void setAppId(String id) {
1652 mAppId = id;
1653 }
1654
Michael Kolbe28b3472011-08-04 16:54:31 -07001655 boolean closeOnBack() {
1656 return mCloseOnBack;
1657 }
1658
1659 void setCloseOnBack(boolean close) {
1660 mCloseOnBack = close;
1661 }
1662
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001663 boolean getDerivedFromIntent() {
1664 return mDerivedFromIntent;
1665 }
1666
1667 void setDerivedFromIntent(boolean derived) {
1668 mDerivedFromIntent = derived;
1669 }
1670
Grace Kloba22ac16e2009-10-07 18:00:23 -07001671 String getUrl() {
John Reck324d4402011-01-11 16:56:42 -08001672 return UrlUtils.filteredUrl(mCurrentState.mUrl);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001673 }
1674
Tarun Nainani8eb00912014-07-17 12:28:32 -07001675
1676 protected void onPageFinished() {
1677 mPageFinished = true;
Ze G Riande2a675c22015-06-03 11:15:24 -07001678 isDistillable();
Tarun Nainani8eb00912014-07-17 12:28:32 -07001679 }
1680
1681 public boolean getPageFinishedStatus() {
1682 return mPageFinished;
1683 }
1684
John Reck49a603c2011-03-03 09:33:05 -08001685 String getOriginalUrl() {
John Reckdb22ec42011-06-29 11:31:24 -07001686 if (mCurrentState.mOriginalUrl == null) {
1687 return getUrl();
John Reck49a603c2011-03-03 09:33:05 -08001688 }
John Reckdb22ec42011-06-29 11:31:24 -07001689 return UrlUtils.filteredUrl(mCurrentState.mOriginalUrl);
John Reck49a603c2011-03-03 09:33:05 -08001690 }
1691
Grace Kloba22ac16e2009-10-07 18:00:23 -07001692 /**
John Reck30c714c2010-12-16 17:30:34 -08001693 * Get the title of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001694 */
1695 String getTitle() {
John Reck30c714c2010-12-16 17:30:34 -08001696 return mCurrentState.mTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001697 }
1698
1699 /**
John Reck30c714c2010-12-16 17:30:34 -08001700 * Get the favicon of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001701 */
1702 Bitmap getFavicon() {
John Reck1cf4b792011-07-26 10:22:22 -07001703 if (mCurrentState.mFavicon != null) {
1704 return mCurrentState.mFavicon;
1705 }
1706 return getDefaultFavicon(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001707 }
1708
Pankaj Garg32e1b942015-06-03 18:13:24 -07001709 public boolean hasFavicon() {
1710 return mCurrentState.mFavicon != null;
1711 }
1712
John Recke969cc52010-12-21 17:24:43 -08001713 public boolean isBookmarkedSite() {
1714 return mCurrentState.mIsBookmarkedSite;
1715 }
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001716
Grace Kloba22ac16e2009-10-07 18:00:23 -07001717 /**
Steve Block08a6f0c2011-10-06 12:12:53 +01001718 * Sets the security state, clears the SSL certificate error and informs
1719 * the controller.
1720 */
Steve Block2466eff2011-10-03 15:33:09 +01001721 private void setSecurityState(SecurityState securityState) {
1722 mCurrentState.mSecurityState = securityState;
1723 mWebViewController.onUpdatedSecurityState(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001724 }
1725
1726 /**
Steve Block2466eff2011-10-03 15:33:09 +01001727 * @return The tab's security state.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001728 */
Steve Block2466eff2011-10-03 15:33:09 +01001729 SecurityState getSecurityState() {
1730 return mCurrentState.mSecurityState;
John Reck30c714c2010-12-16 17:30:34 -08001731 }
1732
1733 int getLoadProgress() {
1734 if (mInPageLoad) {
1735 return mPageLoadProgress;
1736 }
1737 return 100;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001738 }
1739
1740 /**
1741 * @return TRUE if onPageStarted is called while onPageFinished is not
1742 * called yet.
1743 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001744 boolean inPageLoad() {
1745 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001746 }
1747
Grace Kloba22ac16e2009-10-07 18:00:23 -07001748 /**
John Reck1cf4b792011-07-26 10:22:22 -07001749 * @return The Bundle with the tab's state if it can be saved, otherwise null
Grace Kloba22ac16e2009-10-07 18:00:23 -07001750 */
John Reck1cf4b792011-07-26 10:22:22 -07001751 public Bundle saveState() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001752 // If the WebView is null it means we ran low on memory and we already
1753 // stored the saved state in mSavedState.
1754 if (mMainView == null) {
John Reck1cf4b792011-07-26 10:22:22 -07001755 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001756 }
John Reck6c2e2f32011-08-22 13:41:23 -07001757
1758 if (TextUtils.isEmpty(mCurrentState.mUrl)) {
John Reck1cf4b792011-07-26 10:22:22 -07001759 return null;
John Reck24f18262011-06-17 14:47:20 -07001760 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001761
1762 mSavedState = new Bundle();
John Reck6c2e2f32011-08-22 13:41:23 -07001763 WebBackForwardList savedList = mMainView.saveState(mSavedState);
1764 if (savedList == null || savedList.getSize() == 0) {
1765 Log.w(LOGTAG, "Failed to save back/forward list for "
1766 + mCurrentState.mUrl);
1767 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001768
Michael Kolbc831b632011-05-11 09:30:34 -07001769 mSavedState.putLong(ID, mId);
John Reck30c714c2010-12-16 17:30:34 -08001770 mSavedState.putString(CURRURL, mCurrentState.mUrl);
1771 mSavedState.putString(CURRTITLE, mCurrentState.mTitle);
John Reck1cf4b792011-07-26 10:22:22 -07001772 mSavedState.putBoolean(INCOGNITO, mMainView.isPrivateBrowsingEnabled());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001773 if (mAppId != null) {
1774 mSavedState.putString(APPID, mAppId);
1775 }
Michael Kolbe28b3472011-08-04 16:54:31 -07001776 mSavedState.putBoolean(CLOSEFLAG, mCloseOnBack);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001777 // Remember the parent tab so the relationship can be restored.
Michael Kolbc831b632011-05-11 09:30:34 -07001778 if (mParent != null) {
1779 mSavedState.putLong(PARENTTAB, mParent.mId);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001780 }
John Reckb0a86db2011-05-24 14:05:58 -07001781 mSavedState.putBoolean(USERAGENT,
1782 mSettings.hasDesktopUseragent(getWebView()));
John Reck1cf4b792011-07-26 10:22:22 -07001783 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001784 }
1785
1786 /*
1787 * Restore the state of the tab.
1788 */
John Reck1cf4b792011-07-26 10:22:22 -07001789 private void restoreState(Bundle b) {
1790 mSavedState = b;
1791 if (mSavedState == null) {
1792 return;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001793 }
1794 // Restore the internal state even if the WebView fails to restore.
1795 // This will maintain the app id, original url and close-on-exit values.
Michael Kolbc831b632011-05-11 09:30:34 -07001796 mId = b.getLong(ID);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001797 mAppId = b.getString(APPID);
Michael Kolbe28b3472011-08-04 16:54:31 -07001798 mCloseOnBack = b.getBoolean(CLOSEFLAG);
John Reck8b9bb8b2012-03-08 13:19:40 -08001799 restoreUserAgent();
John Reck1cf4b792011-07-26 10:22:22 -07001800 String url = b.getString(CURRURL);
1801 String title = b.getString(CURRTITLE);
1802 boolean incognito = b.getBoolean(INCOGNITO);
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -07001803 mCurrentState = new PageState(mContext, incognito, url);
John Reck1cf4b792011-07-26 10:22:22 -07001804 mCurrentState.mTitle = title;
1805 synchronized (Tab.this) {
1806 if (mCapture != null) {
John Reck4eadc342011-10-31 14:04:10 -07001807 DataController.getInstance(mContext).loadThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07001808 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001809 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001810 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04001811
John Reck8b9bb8b2012-03-08 13:19:40 -08001812 private void restoreUserAgent() {
1813 if (mMainView == null || mSavedState == null) {
1814 return;
1815 }
1816 if (mSavedState.getBoolean(USERAGENT)
1817 != mSettings.hasDesktopUseragent(mMainView)) {
1818 mSettings.toggleDesktopUseragent(mMainView);
1819 }
1820 }
1821
Leon Scroggins1961ed22010-12-07 15:22:21 -05001822 public void updateBookmarkedStatus() {
John Recke969cc52010-12-21 17:24:43 -08001823 mDataController.queryBookmarkStatus(getUrl(), mIsBookmarkCallback);
Leon Scroggins1961ed22010-12-07 15:22:21 -05001824 }
1825
John Recke969cc52010-12-21 17:24:43 -08001826 private DataController.OnQueryUrlIsBookmark mIsBookmarkCallback
1827 = new DataController.OnQueryUrlIsBookmark() {
1828 @Override
1829 public void onQueryUrlIsBookmark(String url, boolean isBookmark) {
1830 if (mCurrentState.mUrl.equals(url)) {
1831 mCurrentState.mIsBookmarkedSite = isBookmark;
1832 mWebViewController.bookmarkedStatusHasChanged(Tab.this);
1833 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001834 }
John Recke969cc52010-12-21 17:24:43 -08001835 };
Michael Kolb1acef692011-03-08 14:12:06 -08001836
Michael Kolbeb95db42011-03-03 10:38:40 -08001837 public Bitmap getScreenshot() {
John Reck1cf4b792011-07-26 10:22:22 -07001838 synchronized (Tab.this) {
1839 return mCapture;
1840 }
Michael Kolbeb95db42011-03-03 10:38:40 -08001841 }
1842
John Reck541f55a2011-06-07 16:34:43 -07001843 public boolean isSnapshot() {
John Reck541f55a2011-06-07 16:34:43 -07001844 return false;
1845 }
1846
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001847 private static class SaveCallback implements ValueCallback<String> {
1848 boolean onReceiveValueCalled = false;
1849 private String mPath;
John Reck68234a92012-04-19 15:27:12 -07001850
1851 @Override
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001852 public void onReceiveValue(String path) {
1853 this.onReceiveValueCalled = true;
1854 this.mPath = path;
John Reck68234a92012-04-19 15:27:12 -07001855 synchronized (this) {
1856 notifyAll();
John Reck8cc92352011-07-06 17:41:52 -07001857 }
John Reck541f55a2011-06-07 16:34:43 -07001858 }
John Reck68234a92012-04-19 15:27:12 -07001859
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001860 public String getPath() {
1861 return mPath;
1862 }
John Reck68234a92012-04-19 15:27:12 -07001863 }
1864
1865 /**
1866 * Must be called on the UI thread
1867 */
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001868 public ContentValues createSnapshotValues(Bitmap bm) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001869 WebView web = getWebView();
John Reck68234a92012-04-19 15:27:12 -07001870 if (web == null) return null;
John Reckd8c74522011-06-14 08:45:00 -07001871 ContentValues values = new ContentValues();
1872 values.put(Snapshots.TITLE, mCurrentState.mTitle);
1873 values.put(Snapshots.URL, mCurrentState.mUrl);
John Reck68234a92012-04-19 15:27:12 -07001874 values.put(Snapshots.BACKGROUND, web.getPageBackgroundColor());
John Reck8cc92352011-07-06 17:41:52 -07001875 values.put(Snapshots.DATE_CREATED, System.currentTimeMillis());
1876 values.put(Snapshots.FAVICON, compressBitmap(getFavicon()));
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001877 values.put(Snapshots.THUMBNAIL, compressBitmap(bm));
John Reckd8c74522011-06-14 08:45:00 -07001878 return values;
John Reck541f55a2011-06-07 16:34:43 -07001879 }
1880
John Reck68234a92012-04-19 15:27:12 -07001881 /**
1882 * Probably want to call this on a background thread
1883 */
1884 public boolean saveViewState(ContentValues values) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001885 WebView web = getWebView();
John Reck68234a92012-04-19 15:27:12 -07001886 if (web == null) return false;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001887 String filename = UUID.randomUUID().toString();
John Reck68234a92012-04-19 15:27:12 -07001888 SaveCallback callback = new SaveCallback();
John Reck68234a92012-04-19 15:27:12 -07001889 try {
John Reck68234a92012-04-19 15:27:12 -07001890 synchronized (callback) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001891 web.saveViewState(filename, callback);
1892 callback.wait();
John Reck68234a92012-04-19 15:27:12 -07001893 }
John Reck68234a92012-04-19 15:27:12 -07001894 } catch (Exception e) {
1895 Log.w(LOGTAG, "Failed to save view state", e);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001896 String path = callback.getPath();
1897 if (path != null) {
1898 File file = mContext.getFileStreamPath(path);
1899 if (file.exists() && !file.delete()) {
1900 file.deleteOnExit();
1901 }
John Reck68234a92012-04-19 15:27:12 -07001902 }
1903 return false;
1904 }
Tarun Nainani8eb00912014-07-17 12:28:32 -07001905
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001906 String path = callback.getPath();
Tarun Nainani8eb00912014-07-17 12:28:32 -07001907 // could be that saving of file failed
1908 if (path == null) {
1909 return false;
1910 }
1911
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001912 File savedFile = new File(path);
1913 if (!savedFile.exists()) {
1914 return false;
John Reck68234a92012-04-19 15:27:12 -07001915 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001916 values.put(Snapshots.VIEWSTATE_PATH, path.substring(path.lastIndexOf('/') + 1));
1917 values.put(Snapshots.VIEWSTATE_SIZE, savedFile.length());
John Reck68234a92012-04-19 15:27:12 -07001918 return true;
1919 }
1920
John Reck8cc92352011-07-06 17:41:52 -07001921 public byte[] compressBitmap(Bitmap bitmap) {
1922 if (bitmap == null) {
1923 return null;
1924 }
1925 ByteArrayOutputStream stream = new ByteArrayOutputStream();
1926 bitmap.compress(CompressFormat.PNG, 100, stream);
1927 return stream.toByteArray();
1928 }
1929
John Reck26b18322011-06-21 13:08:58 -07001930 public void loadUrl(String url, Map<String, String> headers) {
1931 if (mMainView != null) {
Michael Kolba53c9892011-10-05 13:31:40 -07001932 mPageLoadProgress = INITIAL_PROGRESS;
Axesh R. Ajmerad04cad12015-08-12 12:02:44 -07001933 mCurrentState = new PageState(
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -07001934 mContext, mMainView.isPrivateBrowsingEnabled(), url);
John Reck26b18322011-06-21 13:08:58 -07001935 mMainView.loadUrl(url, headers);
1936 }
1937 }
1938
John Reck38b39652012-06-05 09:22:59 -07001939 public void disableUrlOverridingForLoad() {
1940 mDisableOverrideUrlLoading = true;
1941 }
1942
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001943 private void thumbnailUpdated() {
John Reck8ee633f2011-08-09 16:00:35 -07001944 mHandler.removeMessages(MSG_CAPTURE);
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001945
John Reck8ee633f2011-08-09 16:00:35 -07001946 TabControl tc = mWebViewController.getTabControl();
1947 if (tc != null) {
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001948 OnThumbnailUpdatedListener updateListener = tc.getOnThumbnailUpdatedListener();
John Reck8ee633f2011-08-09 16:00:35 -07001949 if (updateListener != null) {
1950 updateListener.onThumbnailUpdated(this);
1951 }
1952 }
Pankaj Gargf49e0222015-09-01 12:19:13 -07001953
1954 if (mViewportCapture != null) {
1955 mWebViewController.onThumbnailCapture(mViewportCapture);
1956 mViewportCapture.recycle();
1957 mViewportCapture = null;
1958 } else {
1959 mWebViewController.onThumbnailCapture(mCapture);
1960 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001961 }
1962
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001963 protected void capture() {
1964 if (mMainView == null || mCapture == null || !mMainView.isReady() ||
1965 mMainView.getContentWidth() <= 0 || mMainView.getContentHeight() <= 0 ||
1966 !mFirstVisualPixelPainted || mMainView.isShowingCrashView()) {
Pankaj Gargf49e0222015-09-01 12:19:13 -07001967 mViewportCapture = null;
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001968 initCaptureBitmap();
1969 thumbnailUpdated();
1970 return;
1971 }
1972
1973 mMainView.getContentBitmapAsync((float) mCaptureWidth / mMainView.getWidth(), new Rect(),
1974 new ValueCallback<Bitmap>() {
1975 @Override
1976 public void onReceiveValue(Bitmap bitmap) {
Pankaj Gargf49e0222015-09-01 12:19:13 -07001977 mViewportCapture = bitmap;
1978
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001979 if (mCapture == null) {
1980 initCaptureBitmap();
1981 }
1982
1983 if (bitmap == null) {
1984 thumbnailUpdated();
1985 return;
1986 }
1987
1988 Canvas c = new Canvas(mCapture);
1989 mCapture.eraseColor(Color.WHITE);
1990 c.drawBitmap(bitmap, 0, 0, null);
1991
1992 // manually anti-alias the edges for the tilt
1993 c.drawRect(0, 0, 1, mCapture.getHeight(), sAlphaPaint);
1994 c.drawRect(mCapture.getWidth() - 1, 0, mCapture.getWidth(),
1995 mCapture.getHeight(), sAlphaPaint);
1996 c.drawRect(0, 0, mCapture.getWidth(), 1, sAlphaPaint);
1997 c.drawRect(0, mCapture.getHeight() - 1, mCapture.getWidth(),
1998 mCapture.getHeight(), sAlphaPaint);
1999 c.setBitmap(null);
2000
2001 persistThumbnail();
2002 thumbnailUpdated();
2003 }
2004 }
2005 );
John Reck1cf4b792011-07-26 10:22:22 -07002006 }
2007
Pankaj Garg6bedeba2015-06-23 15:47:37 -07002008 @Override
2009 public void onNewPicture(WebView view, Picture picture) {
Michael Kolb9ef259a2011-07-12 15:33:08 -07002010 }
2011
John Reckef654f12011-07-12 16:42:08 -07002012 public boolean canGoBack() {
2013 return mMainView != null ? mMainView.canGoBack() : false;
2014 }
2015
2016 public boolean canGoForward() {
2017 return mMainView != null ? mMainView.canGoForward() : false;
2018 }
2019
2020 public void goBack() {
2021 if (mMainView != null) {
2022 mMainView.goBack();
2023 }
2024 }
2025
2026 public void goForward() {
2027 if (mMainView != null) {
2028 mMainView.goForward();
2029 }
2030 }
2031
John Reck1cf4b792011-07-26 10:22:22 -07002032 protected void persistThumbnail() {
John Reck4eadc342011-10-31 14:04:10 -07002033 DataController.getInstance(mContext).saveThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07002034 }
2035
2036 protected void deleteThumbnail() {
John Reck4eadc342011-10-31 14:04:10 -07002037 DataController.getInstance(mContext).deleteThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07002038 }
2039
John Reck4eadc342011-10-31 14:04:10 -07002040 void updateCaptureFromBlob(byte[] blob) {
John Reck1cf4b792011-07-26 10:22:22 -07002041 synchronized (Tab.this) {
2042 if (mCapture == null) {
2043 return;
2044 }
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002045 ByteBuffer buffer = ByteBuffer.wrap(blob);
Michael Kolbd837a112011-08-09 14:00:18 -07002046 try {
2047 mCapture.copyPixelsFromBuffer(buffer);
2048 } catch (RuntimeException rex) {
2049 Log.e(LOGTAG, "Load capture has mismatched sizes; buffer: "
2050 + buffer.capacity() + " blob: " + blob.length
2051 + "capture: " + mCapture.getByteCount());
2052 throw rex;
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002053 }
John Reck1cf4b792011-07-26 10:22:22 -07002054 }
2055 }
2056
John Reck52be4782011-08-26 15:37:29 -07002057 @Override
2058 public String toString() {
2059 StringBuilder builder = new StringBuilder(100);
2060 builder.append(mId);
2061 builder.append(") has parent: ");
2062 if (getParent() != null) {
2063 builder.append("true[");
2064 builder.append(getParent().getId());
2065 builder.append("]");
2066 } else {
2067 builder.append("false");
2068 }
2069 builder.append(", incog: ");
2070 builder.append(isPrivateBrowsingEnabled());
2071 if (!isPrivateBrowsingEnabled()) {
2072 builder.append(", title: ");
2073 builder.append(getTitle());
2074 builder.append(", url: ");
2075 builder.append(getUrl());
2076 }
2077 return builder.toString();
2078 }
2079
Ze G Riande2a675c22015-06-03 11:15:24 -07002080 // dertermines if the tab contains a dislled page
2081 public boolean isDistilled() {
2082 if (!BrowserCommandLine.hasSwitch("reader-mode")) {
2083 return false;
2084 }
2085 try {
2086 return DomDistillerUtils.isUrlDistilled(getUrl());
2087 } catch (Exception e) {
2088 return false;
2089 }
2090 }
2091
2092 //determines if the tab contains a distillable page
2093 public boolean isDistillable() {
2094 if (!BrowserCommandLine.hasSwitch("reader-mode")) {
2095 mIsDistillable = false;
2096 return mIsDistillable;
2097 }
2098 final ValueCallback<String> onIsDistillable = new ValueCallback<String>() {
2099 @Override
2100 public void onReceiveValue(String str) {
2101 mIsDistillable = Boolean.parseBoolean(str);
2102 }
2103 };
2104
2105 if (isDistilled()) {
2106 mIsDistillable = true;
2107 return mIsDistillable;
2108 }
2109
2110 try {
2111 DomDistillerUtils.isWebViewDistillable(getWebView(), onIsDistillable);
2112 } catch (Exception e) {
2113 mIsDistillable = false;
2114 }
2115
2116 return mIsDistillable;
2117 }
2118
2119 // Function that sets the mIsDistillable variable
2120 public void setIsDistillable(boolean value) {
2121 if (!BrowserCommandLine.hasSwitch("reader-mode")) {
2122 mIsDistillable = false;
2123 }
2124 mIsDistillable = value;
2125 }
2126
2127 // Function that returns the distilled url of the current url
2128 public String getDistilledUrl() {
2129 if (getUrl() != null) {
2130 return DomDistillerUtils.getDistilledUrl(getUrl());
2131 }
2132 return new String();
2133 }
2134
2135 // function that returns the non-distilled version of the current url
2136 public String getNonDistilledUrl() {
2137 if (getUrl() != null) {
2138 return DomDistillerUtils.getOriginalUrlFromDistilledUrl(getUrl());
2139 }
2140 return new String();
2141 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002142}