blob: f533f6c192ea4a7d9c84aca824a980679eb5bce0 [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;
67import org.codeaurora.swe.ClientCertRequestHandler;
68import org.codeaurora.swe.HttpAuthHandler;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080069import org.codeaurora.swe.WebBackForwardList;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080070import org.codeaurora.swe.WebChromeClient;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080071import org.codeaurora.swe.WebView;
72import org.codeaurora.swe.WebView.PictureListener;
Pankaj Garg1c7380d2014-08-27 14:17:12 -070073import org.codeaurora.swe.WebView.CreateWindowParams;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080074import org.codeaurora.swe.WebViewClient;
Pankaj Garg1c13cab2015-05-12 11:52:17 -070075import org.codeaurora.swe.util.Observable;
Ze G Riande2a675c22015-06-03 11:15:24 -070076import org.codeaurora.swe.DomDistillerUtils;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080077
John Reck541f55a2011-06-07 16:34:43 -070078import java.io.ByteArrayOutputStream;
John Reck2b71d6d2012-04-18 17:42:06 -070079import java.io.File;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080080import java.io.InputStream;
John Reck1cf4b792011-07-26 10:22:22 -070081import java.nio.ByteBuffer;
Michael Kolbfe251992010-07-08 15:41:55 -070082import java.util.Map;
John Reck2b71d6d2012-04-18 17:42:06 -070083import java.util.UUID;
Michael Kolbfe251992010-07-08 15:41:55 -070084import java.util.Vector;
Vivek Sekhar53ef8932015-06-18 16:51:43 -070085import java.util.List;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080086import java.sql.Timestamp;
87import java.util.Date;
Michael Kolbfe251992010-07-08 15:41:55 -070088
Grace Kloba22ac16e2009-10-07 18:00:23 -070089/**
90 * Class for maintaining Tabs with a main WebView and a subwindow.
91 */
Michael Kolb9ef259a2011-07-12 15:33:08 -070092class Tab implements PictureListener {
Michael Kolb8233fac2010-10-26 16:08:53 -070093
Grace Kloba22ac16e2009-10-07 18:00:23 -070094 // Log Tag
95 private static final String LOGTAG = "Tab";
Bijan Amirzada41242f22014-03-21 12:12:18 -070096 private static final boolean LOGD_ENABLED = com.android.browser.Browser.LOGD_ENABLED;
Ben Murdochc42addf2010-01-28 15:19:59 +000097 // Special case the logtag for messages for the Console to make it easier to
98 // filter them and match the logtag used for these messages in older versions
99 // of the browser.
100 private static final String CONSOLE_LOGTAG = "browser";
101
Michael Kolb9ef259a2011-07-12 15:33:08 -0700102 private static final int MSG_CAPTURE = 42;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800103 private static final int CAPTURE_DELAY = 1000;
Michael Kolba53c9892011-10-05 13:31:40 -0700104 private static final int INITIAL_PROGRESS = 5;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700105
John Reck1cf4b792011-07-26 10:22:22 -0700106 private static Bitmap sDefaultFavicon;
Sagar Dhawan33551ff2015-07-08 17:24:44 -0700107 private boolean mIsKeyboardUp = false;
John Reck1cf4b792011-07-26 10:22:22 -0700108
Michael Kolba3194d02011-09-07 11:23:51 -0700109 private static Paint sAlphaPaint = new Paint();
110 static {
111 sAlphaPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
112 sAlphaPaint.setColor(Color.TRANSPARENT);
113 }
114
Steve Block2466eff2011-10-03 15:33:09 +0100115 public enum SecurityState {
Steve Block4895b012011-10-03 16:26:46 +0100116 // The page's main resource does not use SSL. Note that we use this
117 // state irrespective of the SSL authentication state of sub-resources.
Steve Block2466eff2011-10-03 15:33:09 +0100118 SECURITY_STATE_NOT_SECURE,
Steve Block4895b012011-10-03 16:26:46 +0100119 // The page's main resource uses SSL and the certificate is good. The
120 // same is true of all sub-resources.
Steve Block2466eff2011-10-03 15:33:09 +0100121 SECURITY_STATE_SECURE,
Steve Block4895b012011-10-03 16:26:46 +0100122 // The page's main resource uses SSL and the certificate is good, but
123 // some sub-resources either do not use SSL or have problems with their
124 // certificates.
Steve Block2466eff2011-10-03 15:33:09 +0100125 SECURITY_STATE_MIXED,
Steve Block4895b012011-10-03 16:26:46 +0100126 // The page's main resource uses SSL but there is a problem with its
127 // certificate.
128 SECURITY_STATE_BAD_CERTIFICATE,
John Reck30c714c2010-12-16 17:30:34 -0800129 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700130
Michael Kolb14612442011-06-24 13:06:29 -0700131 Context mContext;
John Reckd8c74522011-06-14 08:45:00 -0700132 protected WebViewController mWebViewController;
Michael Kolb8233fac2010-10-26 16:08:53 -0700133
Michael Kolbc831b632011-05-11 09:30:34 -0700134 // The tab ID
John Reckd8c74522011-06-14 08:45:00 -0700135 private long mId = -1;
Michael Kolbc831b632011-05-11 09:30:34 -0700136
Grace Kloba22ac16e2009-10-07 18:00:23 -0700137 // Main WebView wrapper
Michael Kolba713ec82010-11-29 17:27:06 -0800138 private View mContainer;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700139 // Main WebView
140 private WebView mMainView;
141 // Subwindow container
142 private View mSubViewContainer;
143 // Subwindow WebView
144 private WebView mSubView;
145 // Saved bundle for when we are running low on memory. It contains the
146 // information needed to restore the WebView if the user goes back to the
147 // tab.
148 private Bundle mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700149 // Parent Tab. This is the Tab that created this Tab, or null if the Tab was
150 // created by the UI
Michael Kolbc831b632011-05-11 09:30:34 -0700151 private Tab mParent;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700152 // Tab that constructed by this Tab. This is used when this Tab is
153 // destroyed, it clears all mParentTab values in the children.
Michael Kolbc831b632011-05-11 09:30:34 -0700154 private Vector<Tab> mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700155 // If true, the tab is in the foreground of the current activity.
156 private boolean mInForeground;
Michael Kolb8233fac2010-10-26 16:08:53 -0700157 // If true, the tab is in page loading state (after onPageStarted,
158 // before onPageFinsihed)
159 private boolean mInPageLoad;
Tarun Nainani8eb00912014-07-17 12:28:32 -0700160 private boolean mPageFinished;
John Reck38b39652012-06-05 09:22:59 -0700161 private boolean mDisableOverrideUrlLoading;
Pankaj Garg79878492015-04-01 14:48:21 -0700162 private boolean mFirstVisualPixelPainted = false;
John Reck30c714c2010-12-16 17:30:34 -0800163 // The last reported progress of the current page
164 private int mPageLoadProgress;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000165 // The time the load started, used to find load page time
166 private long mLoadStartTime;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700167 // Application identifier used to find tabs that another application wants
168 // to reuse.
169 private String mAppId;
Michael Kolbe28b3472011-08-04 16:54:31 -0700170 // flag to indicate if tab should be closed on back
171 private boolean mCloseOnBack;
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700172 // flag to indicate if the tab was opened from an intent
173 private boolean mDerivedFromIntent = false;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -0500174 // The listener that gets invoked when a download is started from the
175 // mMainView
Selim Gurun0b3d66f2012-08-29 13:08:13 -0700176 private final BrowserDownloadListener mDownloadListener;
John Recke969cc52010-12-21 17:24:43 -0800177 private DataController mDataController;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700178
179 // AsyncTask for downloading touch icons
180 DownloadTouchIcon mTouchIconLoader;
181
John Reck35e9dd62011-04-25 09:01:54 -0700182 private BrowserSettings mSettings;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700183 private int mCaptureWidth;
184 private int mCaptureHeight;
185 private Bitmap mCapture;
186 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 Block2466eff2011-10-03 15:33:09 +0100224 SecurityState mSecurityState;
Steve Block08a6f0c2011-10-06 12:12:53 +0100225 // This is non-null only when mSecurityState is SECURITY_STATE_BAD_CERTIFICATE.
John Reck30c714c2010-12-16 17:30:34 -0800226 Bitmap mFavicon;
Steve Block08a6f0c2011-10-06 12:12:53 +0100227 boolean mIsBookmarkedSite;
228 boolean mIncognito;
John Reck30c714c2010-12-16 17:30:34 -0800229
230 PageState(Context c, boolean incognito) {
John Reck502a3532011-08-16 14:21:46 -0700231 mIncognito = incognito;
232 if (mIncognito) {
Axesh R. Ajmera1f998ae2015-04-21 14:16:41 -0700233 mOriginalUrl = mUrl = Controller.INCOGNITO_URI;
John Reck30c714c2010-12-16 17:30:34 -0800234 mTitle = c.getString(R.string.new_incognito_tab);
John Reck30c714c2010-12-16 17:30:34 -0800235 } else {
John Reckdb22ec42011-06-29 11:31:24 -0700236 mOriginalUrl = mUrl = "";
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
242 PageState(Context c, boolean incognito, String url, Bitmap favicon) {
John Reck502a3532011-08-16 14:21:46 -0700243 mIncognito = incognito;
John Reckdb22ec42011-06-29 11:31:24 -0700244 mOriginalUrl = mUrl = url;
Pankaj Garg96d0ccd2015-07-30 16:49:47 -0700245 mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
John Reck1cf4b792011-07-26 10:22:22 -0700246 mFavicon = favicon;
John Reck30c714c2010-12-16 17:30:34 -0800247 }
John Reck1cf4b792011-07-26 10:22:22 -0700248
Grace Kloba22ac16e2009-10-07 18:00:23 -0700249 }
250
John Reck30c714c2010-12-16 17:30:34 -0800251 // The current/loading page's state
John Reckd8c74522011-06-14 08:45:00 -0700252 protected PageState mCurrentState;
John Reck30c714c2010-12-16 17:30:34 -0800253
Grace Kloba22ac16e2009-10-07 18:00:23 -0700254 // Used for saving and restoring each Tab
Michael Kolbc831b632011-05-11 09:30:34 -0700255 static final String ID = "ID";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700256 static final String CURRURL = "currentUrl";
257 static final String CURRTITLE = "currentTitle";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700258 static final String PARENTTAB = "parentTab";
259 static final String APPID = "appid";
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700260 static final String INCOGNITO = "privateBrowsingEnabled";
John Reckb0a86db2011-05-24 14:05:58 -0700261 static final String USERAGENT = "useragent";
Michael Kolbe28b3472011-08-04 16:54:31 -0700262 static final String CLOSEFLAG = "closeOnBack";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700263
Pankaj Garg18186a92015-03-31 14:59:33 -0700264 public void setNetworkAvailable(boolean networkUp) {
265 if (networkUp && mReceivedError && (mMainView != null)) {
266 mMainView.reload();
267 }
268 }
269
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700270 public boolean isFirstVisualPixelPainted() {
271 return mFirstVisualPixelPainted;
272 }
273
274 public int getCaptureIndex(int navIndex) {
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700275 int orientation = mWebViewController.getActivity().
276 getResources().getConfiguration().orientation;
277
278 int orientationBit = (orientation == Configuration.ORIENTATION_LANDSCAPE) ? 0 : 1;
279
Vivek Sekhard0f60402015-06-05 14:07:11 -0700280 int index = orientationBit << 31 | (((int)mId & 0x7f) << 24) | (navIndex & 0xffffff);
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700281 return index;
282 }
283
284 public int getTabIdxFromCaptureIdx(int index) {
285 return (index & 0x7f000000) >> 24;
286 }
287
288 public int getOrientationFromCaptureIdx(int index) {
289 return ((index & 0x80000000) == 0) ? Configuration.ORIENTATION_LANDSCAPE :
290 Configuration.ORIENTATION_PORTRAIT;
291
292 }
293
294 public int getNavIdxFromCaptureIdx(int index) {
295 return (index & 0xffffff);
296 }
297
Pankaj Garg96d0ccd2015-07-30 16:49:47 -0700298 public static SecurityState getWebViewSecurityState(WebView view) {
299 switch (view.getSecurityLevel()) {
300 case WebView.SecurityLevel.EV_SECURE:
301 case WebView.SecurityLevel.SECURE:
302 return SecurityState.SECURITY_STATE_SECURE;
303 case WebView.SecurityLevel.SECURITY_ERROR:
304 return SecurityState.SECURITY_STATE_BAD_CERTIFICATE;
305 case WebView.SecurityLevel.SECURITY_POLICY_WARNING:
306 case WebView.SecurityLevel.SECURITY_WARNING:
307 return SecurityState.SECURITY_STATE_MIXED;
308 }
309 return SecurityState.SECURITY_STATE_NOT_SECURE;
310 }
311
Grace Kloba22ac16e2009-10-07 18:00:23 -0700312 // -------------------------------------------------------------------------
313 // WebViewClient implementation for the main WebView
314 // -------------------------------------------------------------------------
315
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800316 private final WebViewClient mWebViewClient = new WebViewClient() {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500317 private Message mDontResend;
318 private Message mResend;
Michael Kolb47bd1e42011-09-01 15:25:00 -0700319
320 private boolean providersDiffer(String url, String otherUrl) {
321 Uri uri1 = Uri.parse(url);
322 Uri uri2 = Uri.parse(otherUrl);
323 return !uri1.getEncodedAuthority().equals(uri2.getEncodedAuthority());
324 }
325
Grace Kloba22ac16e2009-10-07 18:00:23 -0700326 @Override
327 public void onPageStarted(WebView view, String url, Bitmap favicon) {
Ze G Riande2a675c22015-06-03 11:15:24 -0700328 setIsDistillable(false);
Michael Kolb8233fac2010-10-26 16:08:53 -0700329 mInPageLoad = true;
Tarun Nainani8eb00912014-07-17 12:28:32 -0700330 mPageFinished = false;
Pankaj Garg79878492015-04-01 14:48:21 -0700331 mFirstVisualPixelPainted = false;
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700332 mFirstPixelObservable.set(false);
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800333 mReceivedError = false;
Michael Kolb72864272012-05-03 15:42:15 -0700334 mUpdateThumbnail = true;
Michael Kolba53c9892011-10-05 13:31:40 -0700335 mPageLoadProgress = INITIAL_PROGRESS;
Michael Kolb14612442011-06-24 13:06:29 -0700336 mCurrentState = new PageState(mContext,
John Reck30c714c2010-12-16 17:30:34 -0800337 view.isPrivateBrowsingEnabled(), url, favicon);
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000338 mLoadStartTime = SystemClock.uptimeMillis();
Pankaj Garg62bc7912015-04-14 16:08:59 -0700339 // Need re-enable FullScreenMode on Page navigation if needed
340 if (BrowserSettings.getInstance().useFullscreen()){
341 Controller controller = (Controller) mWebViewController;
342 BaseUi ui = (BaseUi) controller.getUi();
343 ui.forceDisableFullscreenMode(false);
344 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700345 // If we start a touch icon load and then load a new page, we don't
346 // want to cancel the current touch icon loader. But, we do want to
347 // create a new one when the touch icon url is known.
348 if (mTouchIconLoader != null) {
349 mTouchIconLoader.mTab = null;
350 mTouchIconLoader = null;
351 }
352
Grace Kloba22ac16e2009-10-07 18:00:23 -0700353 // finally update the UI in the activity if it is in the foreground
John Reck324d4402011-01-11 16:56:42 -0800354 mWebViewController.onPageStarted(Tab.this, view, favicon);
Leon Scroggins4cd97792010-12-03 15:31:56 -0500355
John Recke969cc52010-12-21 17:24:43 -0800356 updateBookmarkedStatus();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700357 }
358
359 @Override
360 public void onPageFinished(WebView view, String url) {
John Reck38b39652012-06-05 09:22:59 -0700361 mDisableOverrideUrlLoading = false;
John Reck5b691842010-11-29 11:21:13 -0800362 if (!isPrivateBrowsingEnabled()) {
363 LogTag.logPageFinishedLoading(
364 url, SystemClock.uptimeMillis() - mLoadStartTime);
365 }
John Reck1cf4b792011-07-26 10:22:22 -0700366 syncCurrentState(view, url);
John Reck324d4402011-01-11 16:56:42 -0800367 mWebViewController.onPageFinished(Tab.this);
Pankaj Garg96d0ccd2015-07-30 16:49:47 -0700368 setSecurityState(getWebViewSecurityState(view));
Grace Kloba22ac16e2009-10-07 18:00:23 -0700369 }
370
Pankaj Garg79878492015-04-01 14:48:21 -0700371 @Override
372 public void onFirstVisualPixel(WebView view) {
373 mFirstVisualPixelPainted = true;
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700374 mFirstPixelObservable.set(true);
Pankaj Garg79878492015-04-01 14:48:21 -0700375 }
376
Grace Kloba22ac16e2009-10-07 18:00:23 -0700377 // return true if want to hijack the url to let another app to handle it
378 @Override
379 public boolean shouldOverrideUrlLoading(WebView view, String url) {
John Reck38b39652012-06-05 09:22:59 -0700380 if (!mDisableOverrideUrlLoading && mInForeground) {
Michael Kolb18eb3772010-12-10 14:29:51 -0800381 return mWebViewController.shouldOverrideUrlLoading(Tab.this,
382 view, url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700383 } else {
384 return false;
385 }
386 }
387
Vivek Sekharb991edb2014-12-17 18:18:07 -0800388 @Override
389 public boolean shouldDownloadFavicon(WebView view, String url) {
390 return true;
391 }
392
Grace Kloba22ac16e2009-10-07 18:00:23 -0700393 /**
Steve Block2466eff2011-10-03 15:33:09 +0100394 * Updates the security state. This method is called when we discover
395 * another resource to be loaded for this page (for example,
396 * javascript). While we update the security state, we do not update
397 * the lock icon until we are done loading, as it is slightly more
398 * secure this way.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700399 */
400 @Override
401 public void onLoadResource(WebView view, String url) {
402 if (url != null && url.length() > 0) {
403 // It is only if the page claims to be secure that we may have
Steve Block2466eff2011-10-03 15:33:09 +0100404 // to update the security state:
405 if (mCurrentState.mSecurityState == SecurityState.SECURITY_STATE_SECURE) {
406 // If NOT a 'safe' url, change the state to mixed content!
Grace Kloba22ac16e2009-10-07 18:00:23 -0700407 if (!(URLUtil.isHttpsUrl(url) || URLUtil.isDataUrl(url)
408 || URLUtil.isAboutUrl(url))) {
Steve Block2466eff2011-10-03 15:33:09 +0100409 mCurrentState.mSecurityState = SecurityState.SECURITY_STATE_MIXED;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700410 }
411 }
412 }
413 }
414
415 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700416 * Show a dialog informing the user of the network error reported by
417 * WebCore if it is in the foreground.
418 */
419 @Override
420 public void onReceivedError(WebView view, int errorCode,
421 String description, String failingUrl) {
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800422 // Used for the syncCurrentState to use
423 // the failing url instead of using webview url
424 mReceivedError = true;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700425 }
426
427 /**
428 * Check with the user if it is ok to resend POST data as the page they
429 * are trying to navigate to is the result of a POST.
430 */
431 @Override
432 public void onFormResubmission(WebView view, final Message dontResend,
433 final Message resend) {
434 if (!mInForeground) {
435 dontResend.sendToTarget();
436 return;
437 }
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500438 if (mDontResend != null) {
439 Log.w(LOGTAG, "onFormResubmission should not be called again "
440 + "while dialog is still up");
441 dontResend.sendToTarget();
442 return;
443 }
444 mDontResend = dontResend;
445 mResend = resend;
Michael Kolb14612442011-06-24 13:06:29 -0700446 new AlertDialog.Builder(mContext).setTitle(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700447 R.string.browserFrameFormResubmitLabel).setMessage(
448 R.string.browserFrameFormResubmitMessage)
449 .setPositiveButton(R.string.ok,
450 new DialogInterface.OnClickListener() {
451 public void onClick(DialogInterface dialog,
452 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500453 if (mResend != null) {
454 mResend.sendToTarget();
455 mResend = null;
456 mDontResend = null;
457 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700458 }
459 }).setNegativeButton(R.string.cancel,
460 new DialogInterface.OnClickListener() {
461 public void onClick(DialogInterface dialog,
462 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500463 if (mDontResend != null) {
464 mDontResend.sendToTarget();
465 mResend = null;
466 mDontResend = null;
467 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700468 }
469 }).setOnCancelListener(new OnCancelListener() {
470 public void onCancel(DialogInterface dialog) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500471 if (mDontResend != null) {
472 mDontResend.sendToTarget();
473 mResend = null;
474 mDontResend = null;
475 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700476 }
477 }).show();
478 }
479
480 /**
481 * Insert the url into the visited history database.
482 * @param url The url to be inserted.
483 * @param isReload True if this url is being reloaded.
484 * FIXME: Not sure what to do when reloading the page.
485 */
486 @Override
487 public void doUpdateVisitedHistory(WebView view, String url,
488 boolean isReload) {
John Reck324d4402011-01-11 16:56:42 -0800489 mWebViewController.doUpdateVisitedHistory(Tab.this, isReload);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700490 }
491
492 /**
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700493 * Displays client certificate request to the user.
494 */
495 @Override
496 public void onReceivedClientCertRequest(final WebView view,
497 final ClientCertRequestHandler handler, final String host_and_port) {
498 if (!mInForeground) {
499 handler.ignore();
500 return;
501 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700502 int colon = host_and_port.lastIndexOf(':');
503 String host;
504 int port;
505 if (colon == -1) {
506 host = host_and_port;
507 port = -1;
508 } else {
509 String portString = host_and_port.substring(colon + 1);
510 try {
511 port = Integer.parseInt(portString);
512 host = host_and_port.substring(0, colon);
513 } catch (NumberFormatException e) {
514 host = host_and_port;
515 port = -1;
516 }
517 }
Michael Kolb14612442011-06-24 13:06:29 -0700518 KeyChain.choosePrivateKeyAlias(
519 mWebViewController.getActivity(), new KeyChainAliasCallback() {
Sagar Dhawan49f85cf2015-07-10 16:54:20 -0700520 @Override
521 public void alias(String alias) {
522 if (alias == null) {
523 handler.cancel();
524 return;
525 }
526 new KeyChainLookup(mContext, handler, alias).execute();
527 }
528 }, null, null, host, port, null);
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700529 }
530
531 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700532 * Handles an HTTP authentication request.
533 *
534 * @param handler The authentication handler
535 * @param host The host
536 * @param realm The realm
537 */
538 @Override
539 public void onReceivedHttpAuthRequest(WebView view,
540 final HttpAuthHandler handler, final String host,
541 final String realm) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700542 mWebViewController.onReceivedHttpAuthRequest(Tab.this, view, handler, host, realm);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700543 }
544
545 @Override
John Reck438bf462011-01-12 18:11:46 -0800546 public WebResourceResponse shouldInterceptRequest(WebView view,
547 String url) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800548 //intercept if opening a new incognito tab - show the incognito welcome page
Axesh R. Ajmera1f998ae2015-04-21 14:16:41 -0700549
550 // show only incognito content and webview has private
551 // and cannot go back(only supported if explicit from UI )
552 if (view.isPrivateBrowsingEnabled() &&
553 !view.canGoBack() &&
554 url.startsWith(Controller.INCOGNITO_URI) ) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800555 Resources resourceHandle = mContext.getResources();
556 InputStream inStream = resourceHandle.openRawResource(
Bijan Amirzada41242f22014-03-21 12:12:18 -0700557 com.android.browser.R.raw.incognito_mode_start_page);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800558 return new WebResourceResponse("text/html", "utf8", inStream);
559 }
kaiyiz6e5b3e02013-08-19 20:02:01 +0800560 WebResourceResponse res;
561 if (MyNavigationUtil.MY_NAVIGATION.equals(url)) {
562 res = MyNavigationProvider.shouldInterceptRequest(mContext, url);
563 } else {
564 res = HomeProvider.shouldInterceptRequest(mContext, url);
565 }
John Reck438bf462011-01-12 18:11:46 -0800566 return res;
567 }
568
569 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700570 public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
571 if (!mInForeground) {
572 return false;
573 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700574 return mWebViewController.shouldOverrideKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700575 }
576
577 @Override
578 public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700579 if (!mInForeground) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700580 return;
581 }
John Reck997b1b72012-04-19 18:08:25 -0700582 if (!mWebViewController.onUnhandledKeyEvent(event)) {
583 super.onUnhandledKeyEvent(view, event);
584 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700585 }
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700586
587 @Override
588 public void beforeNavigation(WebView view, String url) {
Pankaj Garg21dad562015-07-02 17:17:24 -0700589 mTouchIconUrl = null;
Site Mao61b68212015-07-16 10:56:31 -0700590 if (BaseUi.isUiLowPowerMode()) {
Pankaj Garg18aa0a12015-06-22 11:06:12 -0700591 return;
592 }
593
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700594 if (isPrivateBrowsingEnabled()) {
595 return;
596 }
597
598 if (!mFirstVisualPixelPainted) {
599 return;
600 }
601
Pankaj Garg8ee61462015-07-29 18:17:24 -0700602 if (view.getUrl().equals(url)) {
603 return;
604 }
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -0700605
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700606 final int idx = view.copyBackForwardList().getCurrentIndex();
607 boolean bitmapExists = view.hasSnapshot(idx);
608
609 int progress = 100;
610 Controller controller = (Controller)mWebViewController;
611 UI ui = controller.getUi();
612 if (ui instanceof BaseUi) {
613 BaseUi baseUi = (BaseUi) ui;
614 TitleBar titleBar = baseUi.getTitleBar();
615 progress = titleBar.getProgressView().getProgressPercent();
616 }
617
618 if (bitmapExists && progress < 85) {
619 return;
620 }
621
622 int index = getCaptureIndex(view.getLastCommittedHistoryIndex());
Sagar Dhawan49f85cf2015-07-10 16:54:20 -0700623 view.captureSnapshot(index, null);
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700624 }
625
626 @Override
627 public void onHistoryItemCommit(WebView view, int index) {
Site Mao61b68212015-07-16 10:56:31 -0700628 if (BaseUi.isUiLowPowerMode()) {
Pankaj Garg18aa0a12015-06-22 11:06:12 -0700629 return;
630 }
631
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -0700632 // prevent snapshot tab from commiting any history
633 if (isSnapshot()) {
634 return;
635 }
636
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700637 mTabHistoryUpdateObservable.set(index);
Vivek Sekhar53ef8932015-06-18 16:51:43 -0700638 final int maxIdx = view.copyBackForwardList().getSize();
639 final WebView wv = view;
640 view.getSnapshotIds(new ValueCallback <List<Integer>>() {
641 @Override
642 public void onReceiveValue(List<Integer> ids) {
643 int currentTabIdx = mWebViewController.getTabControl().getCurrentPosition();
644 for (Integer id : ids) {
645 if (getTabIdxFromCaptureIdx(id) == currentTabIdx &&
646 getNavIdxFromCaptureIdx(id) >= maxIdx) {
647 wv.deleteSnapshot(id);
648 }
649 }
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700650 }
Vivek Sekhar53ef8932015-06-18 16:51:43 -0700651 });
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700652 }
Pankaj Garg62bc7912015-04-14 16:08:59 -0700653
654 @Override
655 public void onKeyboardStateChange(boolean popup) {
Vivek Sekhard4de6162015-07-21 15:01:45 -0700656 boolean keyboardWasShowing = isKeyboardShowing();
Sagar Dhawan33551ff2015-07-08 17:24:44 -0700657 mIsKeyboardUp = popup;
Vivek Sekhard4de6162015-07-21 15:01:45 -0700658 Controller controller = (Controller)mWebViewController;
659 BaseUi ui = (BaseUi) controller.getUi();
660 // lock the title bar
661 if (popup)
662 ui.getTitleBar().showTopControls(true);
663 if (keyboardWasShowing && popup)
664 ui.getTitleBar().enableTopControls(true);
Pankaj Garg62bc7912015-04-14 16:08:59 -0700665 if (BrowserSettings.getInstance().useFullscreen()) {
Pankaj Garg62bc7912015-04-14 16:08:59 -0700666 ui.forceDisableFullscreenMode(popup);
667 }
668 }
Vivek Sekharae3b1792015-08-03 12:26:41 -0700669
670 @Override
671 public void onAttachInterstitialPage(WebView mWebView) {
672 Controller controller = (Controller)mWebViewController;
673 BaseUi ui = (BaseUi) controller.getUi();
674 ui.getTitleBar().showTopControls(false);
675 }
676
677 @Override
678 public void onDetachInterstitialPage(WebView mWebView) {
679 Controller controller = (Controller)mWebViewController;
680 BaseUi ui = (BaseUi) controller.getUi();
681 ui.getTitleBar().enableTopControls(true);
682 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700683 };
684
John Reck1cf4b792011-07-26 10:22:22 -0700685 private void syncCurrentState(WebView view, String url) {
686 // Sync state (in case of stop/timeout)
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800687
688 if (mReceivedError) {
689 mCurrentState.mUrl = url;
690 mCurrentState.mOriginalUrl = url;
691 } else {
692 mCurrentState.mUrl = view.getUrl();
693 mCurrentState.mOriginalUrl = view.getOriginalUrl();
694 mCurrentState.mFavicon = view.getFavicon();
695 }
696
John Reck1cf4b792011-07-26 10:22:22 -0700697 if (mCurrentState.mUrl == null) {
John Reck8bcafc12011-08-29 16:43:02 -0700698 mCurrentState.mUrl = "";
John Reck1cf4b792011-07-26 10:22:22 -0700699 }
John Reck1cf4b792011-07-26 10:22:22 -0700700 mCurrentState.mTitle = view.getTitle();
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800701
702
John Reck1cf4b792011-07-26 10:22:22 -0700703 if (!URLUtil.isHttpsUrl(mCurrentState.mUrl)) {
704 // In case we stop when loading an HTTPS page from an HTTP page
705 // but before a provisional load occurred
Steve Block2466eff2011-10-03 15:33:09 +0100706 mCurrentState.mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
John Reck1cf4b792011-07-26 10:22:22 -0700707 }
John Reck502a3532011-08-16 14:21:46 -0700708 mCurrentState.mIncognito = view.isPrivateBrowsingEnabled();
John Reck1cf4b792011-07-26 10:22:22 -0700709 }
710
Pankaj Garg21dad562015-07-02 17:17:24 -0700711 public String getTouchIconUrl() {
712 return mTouchIconUrl;
713 }
Tarun Nainani8eb00912014-07-17 12:28:32 -0700714
Sagar Dhawan33551ff2015-07-08 17:24:44 -0700715 public boolean isKeyboardShowing() {
716 Controller controller = (Controller)mWebViewController;
717 return (mIsKeyboardUp || controller.getUi().isEditingUrl());
718 }
719
Tarun Nainani8eb00912014-07-17 12:28:32 -0700720 public boolean isTabFullScreen() {
721 return mFullScreen;
722 }
723
Vivek Sekharf96064b2014-07-28 16:32:34 -0700724 protected void setTabFullscreen(boolean fullScreen) {
Tarun Nainani8eb00912014-07-17 12:28:32 -0700725 Controller controller = (Controller)mWebViewController;
Sudheer Koganti24766882014-10-02 10:58:09 -0700726 controller.getUi().showFullscreen(fullScreen);
Tarun Nainani8eb00912014-07-17 12:28:32 -0700727 mFullScreen = fullScreen;
Vivek Sekharf96064b2014-07-28 16:32:34 -0700728 }
729
Sudheer Koganti24766882014-10-02 10:58:09 -0700730 public boolean exitFullscreen() {
731 if (mFullScreen) {
732 Controller controller = (Controller)mWebViewController;
733 controller.getUi().showFullscreen(false);
734 if (getWebView() != null)
735 getWebView().exitFullscreen();
736 mFullScreen = false;
737 return true;
738 }
739 return false;
740 }
741
742
743
744
Grace Kloba22ac16e2009-10-07 18:00:23 -0700745 // -------------------------------------------------------------------------
746 // WebChromeClient implementation for the main WebView
747 // -------------------------------------------------------------------------
748
749 private final WebChromeClient mWebChromeClient = new WebChromeClient() {
750 // Helper method to create a new tab or sub window.
751 private void createWindow(final boolean dialog, final Message msg) {
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700752 this.createWindow(dialog, msg, null, false);
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700753 }
754
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700755 private void createWindow(final boolean dialog, final Message msg, final String url,
756 final boolean opener_suppressed) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700757 WebView.WebViewTransport transport =
758 (WebView.WebViewTransport) msg.obj;
759 if (dialog) {
760 createSubWindow();
Michael Kolb8233fac2010-10-26 16:08:53 -0700761 mWebViewController.attachSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700762 transport.setWebView(mSubView);
763 } else {
Pankaj Garg21dad562015-07-02 17:17:24 -0700764 capture();
765
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700766 final Tab newTab = mWebViewController.openTab(url,
John Reck5949c662011-05-27 09:52:29 -0700767 Tab.this, true, true);
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700768 // This is special case for rendering links on a webpage in
769 // a new tab. If opener is suppressed, the WebContents created
770 // by the content layer are not fully initialized. This check
771 // will prevent content layer from overriding WebContents
772 // created by new tab with the uninitialized instance.
773 if (!opener_suppressed) {
774 transport.setWebView(newTab.getWebView());
775 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700776 }
777 msg.sendToTarget();
778 }
779
780 @Override
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700781 public void toggleFullscreenModeForTab(boolean enterFullscreen) {
782 if (mWebViewController instanceof Controller) {
Vivek Sekharf96064b2014-07-28 16:32:34 -0700783 setTabFullscreen(enterFullscreen);
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700784 }
785 }
786
787 @Override
Tarun Nainani8eb00912014-07-17 12:28:32 -0700788 public void onOffsetsForFullscreenChanged(float topControlsOffsetYPix,
789 float contentOffsetYPix,
790 float overdrawBottomHeightPix) {
791 if (mWebViewController instanceof Controller) {
Vivek Sekhar8ee3abb2014-07-14 12:32:05 -0700792 Controller controller = (Controller)mWebViewController;
Tarun Nainani8eb00912014-07-17 12:28:32 -0700793 controller.getUi().translateTitleBar(topControlsOffsetYPix);
Vivek Sekhar11b40062015-06-24 11:49:04 -0700794 // Resize the viewport if top controls is not visible
Vivek Sekhar2ee19a32015-07-02 17:03:57 -0700795 if (mMainView != null &&
796 (topControlsOffsetYPix == 0.0f || contentOffsetYPix == 0.0f))
Vivek Sekhar11b40062015-06-24 11:49:04 -0700797 ((BrowserWebView)mMainView).enableTopControls(
798 (topControlsOffsetYPix == 0.0f) ? true : false);
Vivek Sekhar8ee3abb2014-07-14 12:32:05 -0700799 }
800 }
801
802 @Override
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700803 public boolean isTabFullScreen() {
Vivek Sekharf96064b2014-07-28 16:32:34 -0700804 return mFullScreen;
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700805 }
806
807 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700808 public boolean onCreateWindow(WebView view, final boolean dialog,
809 final boolean userGesture, final Message resultMsg) {
810 // only allow new window or sub window for the foreground case
811 if (!mInForeground) {
812 return false;
813 }
814 // Short-circuit if we can't create any more tabs or sub windows.
815 if (dialog && mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -0700816 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700817 .setTitle(R.string.too_many_subwindows_dialog_title)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200818 .setIconAttribute(android.R.attr.alertDialogIcon)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700819 .setMessage(R.string.too_many_subwindows_dialog_message)
820 .setPositiveButton(R.string.ok, null)
821 .show();
822 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700823 } else if (!mWebViewController.getTabControl().canCreateNewTab()) {
Michael Kolb14612442011-06-24 13:06:29 -0700824 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700825 .setTitle(R.string.too_many_windows_dialog_title)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200826 .setIconAttribute(android.R.attr.alertDialogIcon)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700827 .setMessage(R.string.too_many_windows_dialog_message)
828 .setPositiveButton(R.string.ok, null)
829 .show();
830 return false;
831 }
832
833 // Short-circuit if this was a user gesture.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800834 if (userGesture || !mSettings.blockPopupWindows()) {
Tarun Nainani4f5137d2015-04-16 17:26:18 -0700835 WebView.WebViewTransport transport =
836 (WebView.WebViewTransport) resultMsg.obj;
837 CreateWindowParams windowParams = transport.getCreateWindowParams();
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700838 if (windowParams.mOpenerSuppressed) {
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700839 createWindow(dialog, resultMsg, windowParams.mURL, true);
840 // This is special case for rendering links on a webpage in
841 // a new tab. If opener is suppressed, the WebContents created
842 // by the content layer are not fully initialized. Returning false
843 // will prevent content layer from overriding WebContents
844 // created by new tab with the uninitialized instance.
845 return false;
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700846 }
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700847
848 createWindow(dialog, resultMsg);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700849 return true;
850 }
851
Tarun Nainani4f5137d2015-04-16 17:26:18 -0700852 createWindow(dialog, resultMsg);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700853 return true;
854 }
855
856 @Override
Patrick Scotteb5061b2009-11-18 15:00:30 -0500857 public void onRequestFocus(WebView view) {
858 if (!mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700859 mWebViewController.switchToTab(Tab.this);
Patrick Scotteb5061b2009-11-18 15:00:30 -0500860 }
861 }
862
863 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700864 public void onCloseWindow(WebView window) {
Michael Kolbc831b632011-05-11 09:30:34 -0700865 if (mParent != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700866 // JavaScript can only close popup window.
867 if (mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700868 mWebViewController.switchToTab(mParent);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700869 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700870 mWebViewController.closeTab(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700871 }
872 }
873
874 @Override
875 public void onProgressChanged(WebView view, int newProgress) {
John Reck30c714c2010-12-16 17:30:34 -0800876 mPageLoadProgress = newProgress;
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800877 if (newProgress == 100) {
878 mInPageLoad = false;
879 }
John Reck30c714c2010-12-16 17:30:34 -0800880 mWebViewController.onProgressChanged(Tab.this);
Michael Kolb72864272012-05-03 15:42:15 -0700881 if (mUpdateThumbnail && newProgress == 100) {
882 mUpdateThumbnail = false;
883 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700884 }
885
886 @Override
Leon Scroggins21d9b902010-03-11 09:33:11 -0500887 public void onReceivedTitle(WebView view, final String title) {
John Reck30c714c2010-12-16 17:30:34 -0800888 mCurrentState.mTitle = title;
Michael Kolb8233fac2010-10-26 16:08:53 -0700889 mWebViewController.onReceivedTitle(Tab.this, title);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700890 }
891
892 @Override
893 public void onReceivedIcon(WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -0800894 mCurrentState.mFavicon = icon;
Michael Kolb8233fac2010-10-26 16:08:53 -0700895 mWebViewController.onFavicon(Tab.this, view, icon);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700896 }
897
898 @Override
899 public void onReceivedTouchIconUrl(WebView view, String url,
900 boolean precomposed) {
Michael Kolb14612442011-06-24 13:06:29 -0700901 final ContentResolver cr = mContext.getContentResolver();
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400902 // Let precomposed icons take precedence over non-composed
903 // icons.
904 if (precomposed && mTouchIconLoader != null) {
905 mTouchIconLoader.cancel(false);
906 mTouchIconLoader = null;
907 }
908 // Have only one async task at a time.
909 if (mTouchIconLoader == null) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700910 mTouchIconLoader = new DownloadTouchIcon(Tab.this,
Michael Kolb14612442011-06-24 13:06:29 -0700911 mContext, cr, view);
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400912 mTouchIconLoader.execute(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700913 }
Pankaj Garg21dad562015-07-02 17:17:24 -0700914 mTouchIconUrl = url;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700915 }
916
917 @Override
918 public void onShowCustomView(View view,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800919 CustomViewCallback callback) {
Michael Kolb14612442011-06-24 13:06:29 -0700920 Activity activity = mWebViewController.getActivity();
921 if (activity != null) {
922 onShowCustomView(view, activity.getRequestedOrientation(), callback);
923 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400924 }
925
926 @Override
927 public void onShowCustomView(View view, int requestedOrientation,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800928 CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700929 if (mInForeground) mWebViewController.showCustomView(Tab.this, view,
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400930 requestedOrientation, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700931 }
932
933 @Override
934 public void onHideCustomView() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700935 if (mInForeground) mWebViewController.hideCustomView();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700936 }
937
938 /**
939 * The origin has exceeded its database quota.
940 * @param url the URL that exceeded the quota
941 * @param databaseIdentifier the identifier of the database on which the
942 * transaction that caused the quota overflow was run
943 * @param currentQuota the current quota for the origin.
944 * @param estimatedSize the estimated size of the database.
945 * @param totalUsedQuota is the sum of all origins' quota.
946 * @param quotaUpdater The callback to run when a decision to allow or
947 * deny quota has been made. Don't forget to call this!
948 */
949 @Override
950 public void onExceededDatabaseQuota(String url,
951 String databaseIdentifier, long currentQuota, long estimatedSize,
952 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -0700953 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -0700954 .onExceededDatabaseQuota(url, databaseIdentifier,
955 currentQuota, estimatedSize, totalUsedQuota,
956 quotaUpdater);
957 }
958
959 /**
960 * The Application Cache has exceeded its max size.
961 * @param spaceNeeded is the amount of disk space that would be needed
962 * in order for the last appcache operation to succeed.
963 * @param totalUsedQuota is the sum of all origins' quota.
964 * @param quotaUpdater A callback to inform the WebCore thread that a
965 * new app cache size is available. This callback must always
966 * be executed at some point to ensure that the sleeping
967 * WebCore thread is woken up.
968 */
969 @Override
970 public void onReachedMaxAppCacheSize(long spaceNeeded,
971 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -0700972 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -0700973 .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota,
974 quotaUpdater);
975 }
976
Ben Murdoch65acc352009-11-19 18:16:04 +0000977 /* Adds a JavaScript error message to the system log and if the JS
978 * console is enabled in the about:debug options, to that console
979 * also.
Ben Murdochc42addf2010-01-28 15:19:59 +0000980 * @param consoleMessage the message object.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700981 */
982 @Override
Ben Murdochc42addf2010-01-28 15:19:59 +0000983 public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Jeff Hamilton47654f42010-09-07 09:57:51 -0500984 // Don't log console messages in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -0700985 if (isPrivateBrowsingEnabled()) return true;
Jeff Hamilton47654f42010-09-07 09:57:51 -0500986
Ben Murdochc42addf2010-01-28 15:19:59 +0000987 String message = "Console: " + consoleMessage.message() + " "
988 + consoleMessage.sourceId() + ":"
989 + consoleMessage.lineNumber();
990
991 switch (consoleMessage.messageLevel()) {
992 case TIP:
993 Log.v(CONSOLE_LOGTAG, message);
994 break;
995 case LOG:
996 Log.i(CONSOLE_LOGTAG, message);
997 break;
998 case WARNING:
999 Log.w(CONSOLE_LOGTAG, message);
1000 break;
1001 case ERROR:
1002 Log.e(CONSOLE_LOGTAG, message);
1003 break;
1004 case DEBUG:
1005 Log.d(CONSOLE_LOGTAG, message);
1006 break;
1007 }
1008
1009 return true;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001010 }
1011
1012 /**
1013 * Ask the browser for an icon to represent a <video> element.
1014 * This icon will be used if the Web page did not specify a poster attribute.
1015 * @return Bitmap The icon or null if no such icon is available.
1016 */
1017 @Override
1018 public Bitmap getDefaultVideoPoster() {
1019 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001020 return mWebViewController.getDefaultVideoPoster();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001021 }
1022 return null;
1023 }
1024
1025 /**
1026 * Ask the host application for a custom progress view to show while
1027 * a <video> is loading.
1028 * @return View The progress view.
1029 */
1030 @Override
1031 public View getVideoLoadingProgressView() {
1032 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001033 return mWebViewController.getVideoLoadingProgressView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001034 }
1035 return null;
1036 }
1037
1038 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00001039 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001040 if (mInForeground) {
Ben Murdoch8cad4132012-01-11 10:56:43 +00001041 mWebViewController.openFileChooser(uploadMsg, acceptType, capture);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001042 } else {
1043 uploadMsg.onReceiveValue(null);
1044 }
1045 }
1046
Vivek Sekharb54614f2014-05-01 19:03:37 -07001047 @Override
1048 public void showFileChooser(ValueCallback<String[]> uploadFilePaths, String acceptTypes,
1049 boolean capture) {
1050 if (mInForeground) {
1051 mWebViewController.showFileChooser(uploadFilePaths, acceptTypes, capture);
1052 } else {
1053 uploadFilePaths.onReceiveValue(null);
1054 }
1055 }
1056
Grace Kloba22ac16e2009-10-07 18:00:23 -07001057 /**
1058 * Deliver a list of already-visited URLs
1059 */
1060 @Override
1061 public void getVisitedHistory(final ValueCallback<String[]> callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001062 mWebViewController.getVisitedHistory(callback);
1063 }
Ben Murdoch8029a772010-11-16 11:58:21 +00001064
1065 @Override
1066 public void setupAutoFill(Message message) {
1067 // Prompt the user to set up their profile.
1068 final Message msg = message;
Michael Kolb14612442011-06-24 13:06:29 -07001069 AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
1070 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
Ben Murdoch1d676b62011-01-17 12:54:24 +00001071 Context.LAYOUT_INFLATER_SERVICE);
1072 final View layout = inflater.inflate(R.layout.setup_autofill_dialog, null);
1073
1074 builder.setView(layout)
1075 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1076 @Override
1077 public void onClick(DialogInterface dialog, int id) {
1078 CheckBox disableAutoFill = (CheckBox) layout.findViewById(
1079 R.id.setup_autofill_dialog_disable_autofill);
1080
1081 if (disableAutoFill.isChecked()) {
1082 // Disable autofill and show a toast with how to turn it on again.
John Reck35e9dd62011-04-25 09:01:54 -07001083 mSettings.setAutofillEnabled(false);
Michael Kolb14612442011-06-24 13:06:29 -07001084 Toast.makeText(mContext,
Ben Murdoch1d676b62011-01-17 12:54:24 +00001085 R.string.autofill_setup_dialog_negative_toast,
1086 Toast.LENGTH_LONG).show();
1087 } else {
1088 // Take user to the AutoFill profile editor. When they return,
1089 // we will send the message that we pass here which will trigger
1090 // the form to get filled out with their new profile.
1091 mWebViewController.setupAutoFill(msg);
1092 }
1093 }
1094 })
1095 .setNegativeButton(R.string.cancel, null)
1096 .show();
Ben Murdoch8029a772010-11-16 11:58:21 +00001097 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001098 };
1099
1100 // -------------------------------------------------------------------------
1101 // WebViewClient implementation for the sub window
1102 // -------------------------------------------------------------------------
1103
1104 // Subclass of WebViewClient used in subwindows to notify the main
1105 // WebViewClient of certain WebView activities.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001106 private static class SubWindowClient extends WebViewClient {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001107 // The main WebViewClient.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001108 private final WebViewClient mClient;
Michael Kolb8233fac2010-10-26 16:08:53 -07001109 private final WebViewController mController;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001110
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001111 SubWindowClient(WebViewClient client, WebViewController controller) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001112 mClient = client;
Michael Kolb8233fac2010-10-26 16:08:53 -07001113 mController = controller;
Leon Scroggins III211ba542010-04-19 13:21:13 -04001114 }
1115 @Override
1116 public void onPageStarted(WebView view, String url, Bitmap favicon) {
1117 // Unlike the others, do not call mClient's version, which would
1118 // change the progress bar. However, we do want to remove the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001119 // find or select dialog.
Michael Kolb8233fac2010-10-26 16:08:53 -07001120 mController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001121 }
1122 @Override
1123 public void doUpdateVisitedHistory(WebView view, String url,
1124 boolean isReload) {
1125 mClient.doUpdateVisitedHistory(view, url, isReload);
1126 }
1127 @Override
1128 public boolean shouldOverrideUrlLoading(WebView view, String url) {
1129 return mClient.shouldOverrideUrlLoading(view, url);
1130 }
1131 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -07001132 public void onReceivedHttpAuthRequest(WebView view,
1133 HttpAuthHandler handler, String host, String realm) {
1134 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
1135 }
1136 @Override
1137 public void onFormResubmission(WebView view, Message dontResend,
1138 Message resend) {
1139 mClient.onFormResubmission(view, dontResend, resend);
1140 }
1141 @Override
1142 public void onReceivedError(WebView view, int errorCode,
1143 String description, String failingUrl) {
1144 mClient.onReceivedError(view, errorCode, description, failingUrl);
1145 }
1146 @Override
1147 public boolean shouldOverrideKeyEvent(WebView view,
1148 android.view.KeyEvent event) {
1149 return mClient.shouldOverrideKeyEvent(view, event);
1150 }
1151 @Override
1152 public void onUnhandledKeyEvent(WebView view,
1153 android.view.KeyEvent event) {
1154 mClient.onUnhandledKeyEvent(view, event);
1155 }
1156 }
1157
1158 // -------------------------------------------------------------------------
1159 // WebChromeClient implementation for the sub window
1160 // -------------------------------------------------------------------------
1161
1162 private class SubWindowChromeClient extends WebChromeClient {
1163 // The main WebChromeClient.
1164 private final WebChromeClient mClient;
1165
1166 SubWindowChromeClient(WebChromeClient client) {
1167 mClient = client;
1168 }
1169 @Override
1170 public void onProgressChanged(WebView view, int newProgress) {
1171 mClient.onProgressChanged(view, newProgress);
1172 }
1173 @Override
1174 public boolean onCreateWindow(WebView view, boolean dialog,
1175 boolean userGesture, android.os.Message resultMsg) {
1176 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
1177 }
1178 @Override
1179 public void onCloseWindow(WebView window) {
1180 if (window != mSubView) {
1181 Log.e(LOGTAG, "Can't close the window");
1182 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001183 mWebViewController.dismissSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001184 }
1185 }
1186
1187 // -------------------------------------------------------------------------
1188
1189 // Construct a new tab
Michael Kolb7bcafde2011-05-09 13:55:59 -07001190 Tab(WebViewController wvcontroller, WebView w) {
John Reck1cf4b792011-07-26 10:22:22 -07001191 this(wvcontroller, w, null);
1192 }
1193
1194 Tab(WebViewController wvcontroller, Bundle state) {
1195 this(wvcontroller, null, state);
1196 }
1197
1198 Tab(WebViewController wvcontroller, WebView w, Bundle state) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001199 mWebViewController = wvcontroller;
Michael Kolb14612442011-06-24 13:06:29 -07001200 mContext = mWebViewController.getContext();
John Reck35e9dd62011-04-25 09:01:54 -07001201 mSettings = BrowserSettings.getInstance();
Michael Kolb14612442011-06-24 13:06:29 -07001202 mDataController = DataController.getInstance(mContext);
1203 mCurrentState = new PageState(mContext, w != null
John Reck847b5322011-04-14 17:02:18 -07001204 ? w.isPrivateBrowsingEnabled() : false);
Tarun Nainani8084c822014-06-25 13:38:06 -07001205 setTimeStamp();
Michael Kolb8233fac2010-10-26 16:08:53 -07001206 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001207 mInForeground = false;
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001208 mWebViewDestroyedByMemoryMonitor = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001209
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001210 mDownloadListener = new BrowserDownloadListener() {
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001211 public void onDownloadStart(String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001212 String contentDisposition, String mimetype, String referer,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001213 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001214 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001215 mimetype, referer, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001216 }
1217 };
1218
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001219 mCaptureWidth = mContext.getResources().getDimensionPixelSize(R.dimen.tab_thumbnail_width);
1220 mCaptureHeight =mContext.getResources().getDimensionPixelSize(R.dimen.tab_thumbnail_height);
1221
1222 initCaptureBitmap();
1223
John Reck1cf4b792011-07-26 10:22:22 -07001224 restoreState(state);
John Reck52be4782011-08-26 15:37:29 -07001225 if (getId() == -1) {
1226 mId = TabControl.getNextId();
1227 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001228 setWebView(w);
Michael Kolb9ef259a2011-07-12 15:33:08 -07001229 mHandler = new Handler() {
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001230 @Override
Michael Kolb9ef259a2011-07-12 15:33:08 -07001231 public void handleMessage(Message m) {
John Reck1cf4b792011-07-26 10:22:22 -07001232 switch (m.what) {
1233 case MSG_CAPTURE:
1234 capture();
1235 break;
1236 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001237 }
1238 };
Pankaj Garg1c13cab2015-05-12 11:52:17 -07001239
1240 mFirstPixelObservable = new Observable();
1241 mFirstPixelObservable.set(false);
1242 mTabHistoryUpdateObservable = new Observable();
John Reck1cf4b792011-07-26 10:22:22 -07001243 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001244
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001245 private void initCaptureBitmap() {
1246 mCapture = Bitmap.createBitmap(mCaptureWidth, mCaptureHeight, Bitmap.Config.RGB_565);
1247 mCapture.eraseColor(Color.WHITE);
Michael Kolb72864272012-05-03 15:42:15 -07001248 }
1249
Mathew Inwoode09305e2011-09-02 12:03:26 +01001250 /**
1251 * This is used to get a new ID when the tab has been preloaded, before it is displayed and
1252 * added to TabControl. Preloaded tabs can be created before restoreInstanceState, leading
1253 * to overlapping IDs between the preloaded and restored tabs.
1254 */
1255 public void refreshIdAfterPreload() {
1256 mId = TabControl.getNextId();
1257 }
1258
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001259 public void setController(WebViewController ctl) {
1260 mWebViewController = ctl;
1261
John Reck1cf4b792011-07-26 10:22:22 -07001262 if (mWebViewController.shouldCaptureThumbnails()) {
1263 synchronized (Tab.this) {
1264 if (mCapture == null) {
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001265 initCaptureBitmap();
1266 if (mInForeground && !mHandler.hasMessages(MSG_CAPTURE)) {
1267 mHandler.sendEmptyMessageDelayed(MSG_CAPTURE, CAPTURE_DELAY);
John Reck1cf4b792011-07-26 10:22:22 -07001268 }
1269 }
1270 }
1271 } else {
1272 synchronized (Tab.this) {
1273 mCapture = null;
1274 deleteThumbnail();
1275 }
1276 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001277 }
1278
Michael Kolbc831b632011-05-11 09:30:34 -07001279 public long getId() {
1280 return mId;
1281 }
1282
Michael Kolb91911a22012-01-17 11:21:25 -08001283 void setWebView(WebView w) {
1284 setWebView(w, true);
1285 }
1286
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001287 public boolean isNativeActive(){
1288 if (mMainView == null)
1289 return false;
1290 return true;
1291 }
1292
1293 public void setTimeStamp(){
1294 Date d = new Date();
1295 timestamp = (new Timestamp(d.getTime()));
1296 }
1297
1298 public Timestamp getTimestamp() {
1299 return timestamp;
1300 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001301 /**
1302 * Sets the WebView for this tab, correctly removing the old WebView from
1303 * the container view.
1304 */
Michael Kolb91911a22012-01-17 11:21:25 -08001305 void setWebView(WebView w, boolean restore) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001306 if (mMainView == w) {
1307 return;
1308 }
Michael Kolba713ec82010-11-29 17:27:06 -08001309
Michael Kolba713ec82010-11-29 17:27:06 -08001310 mWebViewController.onSetWebView(this, w);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001311
John Reck1cf4b792011-07-26 10:22:22 -07001312 if (mMainView != null) {
John Reck8ee633f2011-08-09 16:00:35 -07001313 mMainView.setPictureListener(null);
John Reck1cf4b792011-07-26 10:22:22 -07001314 if (w != null) {
1315 syncCurrentState(w, null);
1316 } else {
Panos Thomasa9a5a582014-03-18 19:20:08 -07001317 mCurrentState = new PageState(mContext, mMainView.isPrivateBrowsingEnabled());
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001318
1319 if (mWebViewDestroyedByMemoryMonitor) {
1320 /*
1321 * If tab was destroyed as a result of the MemoryMonitor
1322 * then we need to restore the state properties
1323 * from the old WebView (mMainView)
1324 */
1325 syncCurrentState(mMainView, null);
1326 mWebViewDestroyedByMemoryMonitor = false;
1327 }
John Reck1cf4b792011-07-26 10:22:22 -07001328 }
1329 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001330 // set the new one
1331 mMainView = w;
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001332
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001333 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001334 if (mMainView != null) {
1335 mMainView.setWebViewClient(mWebViewClient);
1336 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001337 // Attach DownloadManager so that downloads can start in an active
1338 // or a non-active window. This can happen when going to a site that
1339 // does a redirect after a period of time. The user could have
1340 // switched to another tab while waiting for the download to start.
1341 mMainView.setDownloadListener(mDownloadListener);
John Reck8ee633f2011-08-09 16:00:35 -07001342 TabControl tc = mWebViewController.getTabControl();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001343 if (tc != null /*&& tc.getOnThumbnailUpdatedListener() != null*/) {
John Reck8ee633f2011-08-09 16:00:35 -07001344 mMainView.setPictureListener(this);
1345 }
Michael Kolb91911a22012-01-17 11:21:25 -08001346 if (restore && (mSavedState != null)) {
John Reck8b9bb8b2012-03-08 13:19:40 -08001347 restoreUserAgent();
John Reck6c2e2f32011-08-22 13:41:23 -07001348 WebBackForwardList restoredState
1349 = mMainView.restoreState(mSavedState);
1350 if (restoredState == null || restoredState.getSize() == 0) {
1351 Log.w(LOGTAG, "Failed to restore WebView state!");
1352 loadUrl(mCurrentState.mOriginalUrl, null);
1353 }
John Reck1cf4b792011-07-26 10:22:22 -07001354 mSavedState = null;
1355 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001356 }
1357 }
1358
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001359 public void destroyThroughMemoryMonitor() {
1360 mWebViewDestroyedByMemoryMonitor = true;
1361 destroy();
1362 }
1363
Grace Kloba22ac16e2009-10-07 18:00:23 -07001364 /**
1365 * Destroy the tab's main WebView and subWindow if any
1366 */
1367 void destroy() {
Tarun Nainani2c1dd7c2014-07-05 16:40:12 -07001368
Matthew Huib7f2e9c2014-04-16 11:12:37 -04001369 if (mPostponeDestroy) {
1370 mShouldDestroy = true;
1371 return;
1372 }
1373 mShouldDestroy = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001374 if (mMainView != null) {
1375 dismissSubWindow();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001376 // save the WebView to call destroy() after detach it from the tab
Vivek Sekhar53ef8932015-06-18 16:51:43 -07001377 final WebView webView = mMainView;
Vivek Sekhar2ee19a32015-07-02 17:03:57 -07001378 setWebView(null);
Site Mao61b68212015-07-16 10:56:31 -07001379 if (!mWebViewDestroyedByMemoryMonitor && !BaseUi.isUiLowPowerMode()) {
Vivek Sekhar2ee19a32015-07-02 17:03:57 -07001380 // Tabs can be reused with new instance of WebView so delete the snapshots
Vivek Sekhar53ef8932015-06-18 16:51:43 -07001381 webView.getSnapshotIds(new ValueCallback<List<Integer>>() {
1382 @Override
1383 public void onReceiveValue(List<Integer> ids) {
1384 int currentTabIdx = mWebViewController.getTabControl().getCurrentPosition();
1385 for (Integer id : ids) {
1386 if (getTabIdxFromCaptureIdx(id) == currentTabIdx) {
1387 webView.deleteSnapshot(id);
1388 }
1389 }
Vivek Sekhar53ef8932015-06-18 16:51:43 -07001390 webView.destroy();
Pankaj Garg1c13cab2015-05-12 11:52:17 -07001391 }
Vivek Sekhar53ef8932015-06-18 16:51:43 -07001392 });
Vivek Sekhar2ee19a32015-07-02 17:03:57 -07001393 } else {
1394 webView.destroy();
Pankaj Garg1c13cab2015-05-12 11:52:17 -07001395 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001396 }
1397 }
1398
Matthew Huib7f2e9c2014-04-16 11:12:37 -04001399 private boolean mPostponeDestroy = false;
1400 private boolean mShouldDestroy = false;
1401
1402 public void postponeDestroy() {
1403 mPostponeDestroy = true;
1404 }
1405
1406 public void performPostponedDestroy() {
1407 mPostponeDestroy = false;
1408 if (mShouldDestroy) {
1409 destroy();
1410 }
1411 }
1412
Grace Kloba22ac16e2009-10-07 18:00:23 -07001413 /**
1414 * Remove the tab from the parent
1415 */
1416 void removeFromTree() {
1417 // detach the children
Michael Kolbc831b632011-05-11 09:30:34 -07001418 if (mChildren != null) {
1419 for(Tab t : mChildren) {
1420 t.setParent(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001421 }
1422 }
1423 // remove itself from the parent list
Michael Kolbc831b632011-05-11 09:30:34 -07001424 if (mParent != null) {
1425 mParent.mChildren.remove(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001426 }
Tarun Nainani8eb00912014-07-17 12:28:32 -07001427
1428 mCapture = null;
John Reck1cf4b792011-07-26 10:22:22 -07001429 deleteThumbnail();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001430 }
1431
1432 /**
1433 * Create a new subwindow unless a subwindow already exists.
1434 * @return True if a new subwindow was created. False if one already exists.
1435 */
1436 boolean createSubWindow() {
1437 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001438 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001439 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001440 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001441 mSubView.setWebChromeClient(new SubWindowChromeClient(
1442 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001443 // Set a different DownloadListener for the mSubView, since it will
1444 // just need to dismiss the mSubView, rather than close the Tab
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001445 mSubView.setDownloadListener(new BrowserDownloadListener() {
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001446 public void onDownloadStart(String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001447 String contentDisposition, String mimetype, String referer,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001448 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001449 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001450 contentDisposition, mimetype, referer, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001451 if (mSubView.copyBackForwardList().getSize() == 0) {
1452 // This subwindow was opened for the sole purpose of
1453 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001454 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001455 }
1456 }
1457 });
Michael Kolb14612442011-06-24 13:06:29 -07001458 mSubView.setOnCreateContextMenuListener(mWebViewController.getActivity());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001459 return true;
1460 }
1461 return false;
1462 }
1463
1464 /**
1465 * Dismiss the subWindow for the tab.
1466 */
1467 void dismissSubWindow() {
1468 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001469 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001470 mSubView.destroy();
1471 mSubView = null;
1472 mSubViewContainer = null;
1473 }
1474 }
1475
Grace Kloba22ac16e2009-10-07 18:00:23 -07001476
1477 /**
1478 * Set the parent tab of this tab.
1479 */
Michael Kolbc831b632011-05-11 09:30:34 -07001480 void setParent(Tab parent) {
John Reck52be4782011-08-26 15:37:29 -07001481 if (parent == this) {
1482 throw new IllegalStateException("Cannot set parent to self!");
1483 }
Michael Kolbc831b632011-05-11 09:30:34 -07001484 mParent = parent;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001485 // This tab may have been freed due to low memory. If that is the case,
Michael Kolbc831b632011-05-11 09:30:34 -07001486 // the parent tab id is already saved. If we are changing that id
Grace Kloba22ac16e2009-10-07 18:00:23 -07001487 // (most likely due to removing the parent tab) we must update the
Michael Kolbc831b632011-05-11 09:30:34 -07001488 // parent tab id in the saved Bundle.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001489 if (mSavedState != null) {
1490 if (parent == null) {
1491 mSavedState.remove(PARENTTAB);
1492 } else {
Michael Kolbc831b632011-05-11 09:30:34 -07001493 mSavedState.putLong(PARENTTAB, parent.getId());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001494 }
1495 }
John Reckb0a86db2011-05-24 14:05:58 -07001496
1497 // Sync the WebView useragent with the parent
1498 if (parent != null && mSettings.hasDesktopUseragent(parent.getWebView())
1499 != mSettings.hasDesktopUseragent(getWebView())) {
1500 mSettings.toggleDesktopUseragent(getWebView());
1501 }
John Reck52be4782011-08-26 15:37:29 -07001502
1503 if (parent != null && parent.getId() == getId()) {
1504 throw new IllegalStateException("Parent has same ID as child!");
1505 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001506 }
1507
1508 /**
Michael Kolbc831b632011-05-11 09:30:34 -07001509 * If this Tab was created through another Tab, then this method returns
1510 * that Tab.
1511 * @return the Tab parent or null
1512 */
1513 public Tab getParent() {
1514 return mParent;
1515 }
1516
1517 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001518 * When a Tab is created through the content of another Tab, then we
1519 * associate the Tabs.
1520 * @param child the Tab that was created from this Tab
1521 */
1522 void addChildTab(Tab child) {
Michael Kolbc831b632011-05-11 09:30:34 -07001523 if (mChildren == null) {
1524 mChildren = new Vector<Tab>();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001525 }
Michael Kolbc831b632011-05-11 09:30:34 -07001526 mChildren.add(child);
1527 child.setParent(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001528 }
1529
Michael Kolbc831b632011-05-11 09:30:34 -07001530 Vector<Tab> getChildren() {
1531 return mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001532 }
1533
1534 void resume() {
1535 if (mMainView != null) {
John Reck56c1fcf2011-08-17 10:15:16 -07001536 setupHwAcceleration(mMainView);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001537 mMainView.onResume();
1538 if (mSubView != null) {
1539 mSubView.onResume();
1540 }
1541 }
1542 }
1543
John Reck56c1fcf2011-08-17 10:15:16 -07001544 private void setupHwAcceleration(View web) {
1545 if (web == null) return;
1546 BrowserSettings settings = BrowserSettings.getInstance();
1547 if (settings.isHardwareAccelerated()) {
1548 web.setLayerType(View.LAYER_TYPE_NONE, null);
1549 } else {
1550 web.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
1551 }
1552 }
1553
Grace Kloba22ac16e2009-10-07 18:00:23 -07001554 void pause() {
1555 if (mMainView != null) {
1556 mMainView.onPause();
1557 if (mSubView != null) {
1558 mSubView.onPause();
1559 }
1560 }
1561 }
1562
1563 void putInForeground() {
John Reck8ee633f2011-08-09 16:00:35 -07001564 if (mInForeground) {
1565 return;
1566 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001567 mInForeground = true;
1568 resume();
Michael Kolb14612442011-06-24 13:06:29 -07001569 Activity activity = mWebViewController.getActivity();
1570 mMainView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001571 if (mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -07001572 mSubView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001573 }
Axesh R. Ajmerac6b5c322015-05-01 11:06:10 -07001574
Leon Scroggins1961ed22010-12-07 15:22:21 -05001575 mWebViewController.bookmarkedStatusHasChanged(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001576 }
1577
1578 void putInBackground() {
John Reck8ee633f2011-08-09 16:00:35 -07001579 if (!mInForeground) {
1580 return;
1581 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001582 mInForeground = false;
1583 pause();
1584 mMainView.setOnCreateContextMenuListener(null);
1585 if (mSubView != null) {
1586 mSubView.setOnCreateContextMenuListener(null);
1587 }
1588 }
1589
Michael Kolb8233fac2010-10-26 16:08:53 -07001590 boolean inForeground() {
1591 return mInForeground;
1592 }
1593
Grace Kloba22ac16e2009-10-07 18:00:23 -07001594 /**
1595 * Return the top window of this tab; either the subwindow if it is not
1596 * null or the main window.
1597 * @return The top window of this tab.
1598 */
1599 WebView getTopWindow() {
1600 if (mSubView != null) {
1601 return mSubView;
1602 }
1603 return mMainView;
1604 }
1605
1606 /**
1607 * Return the main window of this tab. Note: if a tab is freed in the
1608 * background, this can return null. It is only guaranteed to be
1609 * non-null for the current tab.
1610 * @return The main WebView of this tab.
1611 */
1612 WebView getWebView() {
1613 return mMainView;
1614 }
1615
Michael Kolba713ec82010-11-29 17:27:06 -08001616 void setViewContainer(View container) {
1617 mContainer = container;
1618 }
1619
Michael Kolb8233fac2010-10-26 16:08:53 -07001620 View getViewContainer() {
1621 return mContainer;
1622 }
1623
Grace Kloba22ac16e2009-10-07 18:00:23 -07001624 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001625 * Return whether private browsing is enabled for the main window of
1626 * this tab.
1627 * @return True if private browsing is enabled.
1628 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001629 boolean isPrivateBrowsingEnabled() {
John Reck502a3532011-08-16 14:21:46 -07001630 return mCurrentState.mIncognito;
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001631 }
1632
1633 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001634 * Return the subwindow of this tab or null if there is no subwindow.
1635 * @return The subwindow of this tab or null.
1636 */
1637 WebView getSubWebView() {
1638 return mSubView;
1639 }
1640
Michael Kolb1514bb72010-11-22 09:11:48 -08001641 void setSubWebView(WebView subView) {
1642 mSubView = subView;
1643 }
1644
Michael Kolb8233fac2010-10-26 16:08:53 -07001645 View getSubViewContainer() {
1646 return mSubViewContainer;
1647 }
1648
Michael Kolb1514bb72010-11-22 09:11:48 -08001649 void setSubViewContainer(View subViewContainer) {
1650 mSubViewContainer = subViewContainer;
1651 }
1652
Grace Kloba22ac16e2009-10-07 18:00:23 -07001653
1654 /**
1655 * @return The application id string
1656 */
1657 String getAppId() {
1658 return mAppId;
1659 }
1660
1661 /**
1662 * Set the application id string
1663 * @param id
1664 */
1665 void setAppId(String id) {
1666 mAppId = id;
1667 }
1668
Michael Kolbe28b3472011-08-04 16:54:31 -07001669 boolean closeOnBack() {
1670 return mCloseOnBack;
1671 }
1672
1673 void setCloseOnBack(boolean close) {
1674 mCloseOnBack = close;
1675 }
1676
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001677 boolean getDerivedFromIntent() {
1678 return mDerivedFromIntent;
1679 }
1680
1681 void setDerivedFromIntent(boolean derived) {
1682 mDerivedFromIntent = derived;
1683 }
1684
Grace Kloba22ac16e2009-10-07 18:00:23 -07001685 String getUrl() {
John Reck324d4402011-01-11 16:56:42 -08001686 return UrlUtils.filteredUrl(mCurrentState.mUrl);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001687 }
1688
Tarun Nainani8eb00912014-07-17 12:28:32 -07001689
1690 protected void onPageFinished() {
1691 mPageFinished = true;
Ze G Riande2a675c22015-06-03 11:15:24 -07001692 isDistillable();
Tarun Nainani8eb00912014-07-17 12:28:32 -07001693 }
1694
1695 public boolean getPageFinishedStatus() {
1696 return mPageFinished;
1697 }
1698
John Reck49a603c2011-03-03 09:33:05 -08001699 String getOriginalUrl() {
John Reckdb22ec42011-06-29 11:31:24 -07001700 if (mCurrentState.mOriginalUrl == null) {
1701 return getUrl();
John Reck49a603c2011-03-03 09:33:05 -08001702 }
John Reckdb22ec42011-06-29 11:31:24 -07001703 return UrlUtils.filteredUrl(mCurrentState.mOriginalUrl);
John Reck49a603c2011-03-03 09:33:05 -08001704 }
1705
Grace Kloba22ac16e2009-10-07 18:00:23 -07001706 /**
John Reck30c714c2010-12-16 17:30:34 -08001707 * Get the title of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001708 */
1709 String getTitle() {
John Reck30c714c2010-12-16 17:30:34 -08001710 return mCurrentState.mTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001711 }
1712
1713 /**
John Reck30c714c2010-12-16 17:30:34 -08001714 * Get the favicon of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001715 */
1716 Bitmap getFavicon() {
John Reck1cf4b792011-07-26 10:22:22 -07001717 if (mCurrentState.mFavicon != null) {
1718 return mCurrentState.mFavicon;
1719 }
1720 return getDefaultFavicon(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001721 }
1722
Pankaj Garg32e1b942015-06-03 18:13:24 -07001723 public boolean hasFavicon() {
1724 return mCurrentState.mFavicon != null;
1725 }
1726
John Recke969cc52010-12-21 17:24:43 -08001727 public boolean isBookmarkedSite() {
1728 return mCurrentState.mIsBookmarkedSite;
1729 }
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001730
Grace Kloba22ac16e2009-10-07 18:00:23 -07001731 /**
Steve Block08a6f0c2011-10-06 12:12:53 +01001732 * Sets the security state, clears the SSL certificate error and informs
1733 * the controller.
1734 */
Steve Block2466eff2011-10-03 15:33:09 +01001735 private void setSecurityState(SecurityState securityState) {
1736 mCurrentState.mSecurityState = securityState;
1737 mWebViewController.onUpdatedSecurityState(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001738 }
1739
1740 /**
Steve Block2466eff2011-10-03 15:33:09 +01001741 * @return The tab's security state.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001742 */
Steve Block2466eff2011-10-03 15:33:09 +01001743 SecurityState getSecurityState() {
1744 return mCurrentState.mSecurityState;
John Reck30c714c2010-12-16 17:30:34 -08001745 }
1746
1747 int getLoadProgress() {
1748 if (mInPageLoad) {
1749 return mPageLoadProgress;
1750 }
1751 return 100;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001752 }
1753
1754 /**
1755 * @return TRUE if onPageStarted is called while onPageFinished is not
1756 * called yet.
1757 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001758 boolean inPageLoad() {
1759 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001760 }
1761
Grace Kloba22ac16e2009-10-07 18:00:23 -07001762 /**
John Reck1cf4b792011-07-26 10:22:22 -07001763 * @return The Bundle with the tab's state if it can be saved, otherwise null
Grace Kloba22ac16e2009-10-07 18:00:23 -07001764 */
John Reck1cf4b792011-07-26 10:22:22 -07001765 public Bundle saveState() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001766 // If the WebView is null it means we ran low on memory and we already
1767 // stored the saved state in mSavedState.
1768 if (mMainView == null) {
John Reck1cf4b792011-07-26 10:22:22 -07001769 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001770 }
John Reck6c2e2f32011-08-22 13:41:23 -07001771
1772 if (TextUtils.isEmpty(mCurrentState.mUrl)) {
John Reck1cf4b792011-07-26 10:22:22 -07001773 return null;
John Reck24f18262011-06-17 14:47:20 -07001774 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001775
1776 mSavedState = new Bundle();
John Reck6c2e2f32011-08-22 13:41:23 -07001777 WebBackForwardList savedList = mMainView.saveState(mSavedState);
1778 if (savedList == null || savedList.getSize() == 0) {
1779 Log.w(LOGTAG, "Failed to save back/forward list for "
1780 + mCurrentState.mUrl);
1781 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001782
Michael Kolbc831b632011-05-11 09:30:34 -07001783 mSavedState.putLong(ID, mId);
John Reck30c714c2010-12-16 17:30:34 -08001784 mSavedState.putString(CURRURL, mCurrentState.mUrl);
1785 mSavedState.putString(CURRTITLE, mCurrentState.mTitle);
John Reck1cf4b792011-07-26 10:22:22 -07001786 mSavedState.putBoolean(INCOGNITO, mMainView.isPrivateBrowsingEnabled());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001787 if (mAppId != null) {
1788 mSavedState.putString(APPID, mAppId);
1789 }
Michael Kolbe28b3472011-08-04 16:54:31 -07001790 mSavedState.putBoolean(CLOSEFLAG, mCloseOnBack);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001791 // Remember the parent tab so the relationship can be restored.
Michael Kolbc831b632011-05-11 09:30:34 -07001792 if (mParent != null) {
1793 mSavedState.putLong(PARENTTAB, mParent.mId);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001794 }
John Reckb0a86db2011-05-24 14:05:58 -07001795 mSavedState.putBoolean(USERAGENT,
1796 mSettings.hasDesktopUseragent(getWebView()));
John Reck1cf4b792011-07-26 10:22:22 -07001797 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001798 }
1799
1800 /*
1801 * Restore the state of the tab.
1802 */
John Reck1cf4b792011-07-26 10:22:22 -07001803 private void restoreState(Bundle b) {
1804 mSavedState = b;
1805 if (mSavedState == null) {
1806 return;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001807 }
1808 // Restore the internal state even if the WebView fails to restore.
1809 // This will maintain the app id, original url and close-on-exit values.
Michael Kolbc831b632011-05-11 09:30:34 -07001810 mId = b.getLong(ID);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001811 mAppId = b.getString(APPID);
Michael Kolbe28b3472011-08-04 16:54:31 -07001812 mCloseOnBack = b.getBoolean(CLOSEFLAG);
John Reck8b9bb8b2012-03-08 13:19:40 -08001813 restoreUserAgent();
John Reck1cf4b792011-07-26 10:22:22 -07001814 String url = b.getString(CURRURL);
1815 String title = b.getString(CURRTITLE);
1816 boolean incognito = b.getBoolean(INCOGNITO);
1817 mCurrentState = new PageState(mContext, incognito, url, null);
1818 mCurrentState.mTitle = title;
1819 synchronized (Tab.this) {
1820 if (mCapture != null) {
John Reck4eadc342011-10-31 14:04:10 -07001821 DataController.getInstance(mContext).loadThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07001822 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001823 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001824 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04001825
John Reck8b9bb8b2012-03-08 13:19:40 -08001826 private void restoreUserAgent() {
1827 if (mMainView == null || mSavedState == null) {
1828 return;
1829 }
1830 if (mSavedState.getBoolean(USERAGENT)
1831 != mSettings.hasDesktopUseragent(mMainView)) {
1832 mSettings.toggleDesktopUseragent(mMainView);
1833 }
1834 }
1835
Leon Scroggins1961ed22010-12-07 15:22:21 -05001836 public void updateBookmarkedStatus() {
John Recke969cc52010-12-21 17:24:43 -08001837 mDataController.queryBookmarkStatus(getUrl(), mIsBookmarkCallback);
Leon Scroggins1961ed22010-12-07 15:22:21 -05001838 }
1839
John Recke969cc52010-12-21 17:24:43 -08001840 private DataController.OnQueryUrlIsBookmark mIsBookmarkCallback
1841 = new DataController.OnQueryUrlIsBookmark() {
1842 @Override
1843 public void onQueryUrlIsBookmark(String url, boolean isBookmark) {
1844 if (mCurrentState.mUrl.equals(url)) {
1845 mCurrentState.mIsBookmarkedSite = isBookmark;
1846 mWebViewController.bookmarkedStatusHasChanged(Tab.this);
1847 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001848 }
John Recke969cc52010-12-21 17:24:43 -08001849 };
Michael Kolb1acef692011-03-08 14:12:06 -08001850
Michael Kolbeb95db42011-03-03 10:38:40 -08001851 public Bitmap getScreenshot() {
John Reck1cf4b792011-07-26 10:22:22 -07001852 synchronized (Tab.this) {
1853 return mCapture;
1854 }
Michael Kolbeb95db42011-03-03 10:38:40 -08001855 }
1856
John Reck541f55a2011-06-07 16:34:43 -07001857 public boolean isSnapshot() {
John Reck541f55a2011-06-07 16:34:43 -07001858 return false;
1859 }
1860
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001861 private static class SaveCallback implements ValueCallback<String> {
1862 boolean onReceiveValueCalled = false;
1863 private String mPath;
John Reck68234a92012-04-19 15:27:12 -07001864
1865 @Override
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001866 public void onReceiveValue(String path) {
1867 this.onReceiveValueCalled = true;
1868 this.mPath = path;
John Reck68234a92012-04-19 15:27:12 -07001869 synchronized (this) {
1870 notifyAll();
John Reck8cc92352011-07-06 17:41:52 -07001871 }
John Reck541f55a2011-06-07 16:34:43 -07001872 }
John Reck68234a92012-04-19 15:27:12 -07001873
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001874 public String getPath() {
1875 return mPath;
1876 }
John Reck68234a92012-04-19 15:27:12 -07001877 }
1878
1879 /**
1880 * Must be called on the UI thread
1881 */
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001882 public ContentValues createSnapshotValues(Bitmap bm) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001883 WebView web = getWebView();
John Reck68234a92012-04-19 15:27:12 -07001884 if (web == null) return null;
John Reckd8c74522011-06-14 08:45:00 -07001885 ContentValues values = new ContentValues();
1886 values.put(Snapshots.TITLE, mCurrentState.mTitle);
1887 values.put(Snapshots.URL, mCurrentState.mUrl);
John Reck68234a92012-04-19 15:27:12 -07001888 values.put(Snapshots.BACKGROUND, web.getPageBackgroundColor());
John Reck8cc92352011-07-06 17:41:52 -07001889 values.put(Snapshots.DATE_CREATED, System.currentTimeMillis());
1890 values.put(Snapshots.FAVICON, compressBitmap(getFavicon()));
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001891 values.put(Snapshots.THUMBNAIL, compressBitmap(bm));
John Reckd8c74522011-06-14 08:45:00 -07001892 return values;
John Reck541f55a2011-06-07 16:34:43 -07001893 }
1894
John Reck68234a92012-04-19 15:27:12 -07001895 /**
1896 * Probably want to call this on a background thread
1897 */
1898 public boolean saveViewState(ContentValues values) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001899 WebView web = getWebView();
John Reck68234a92012-04-19 15:27:12 -07001900 if (web == null) return false;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001901 String filename = UUID.randomUUID().toString();
John Reck68234a92012-04-19 15:27:12 -07001902 SaveCallback callback = new SaveCallback();
John Reck68234a92012-04-19 15:27:12 -07001903 try {
John Reck68234a92012-04-19 15:27:12 -07001904 synchronized (callback) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001905 web.saveViewState(filename, callback);
1906 callback.wait();
John Reck68234a92012-04-19 15:27:12 -07001907 }
John Reck68234a92012-04-19 15:27:12 -07001908 } catch (Exception e) {
1909 Log.w(LOGTAG, "Failed to save view state", e);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001910 String path = callback.getPath();
1911 if (path != null) {
1912 File file = mContext.getFileStreamPath(path);
1913 if (file.exists() && !file.delete()) {
1914 file.deleteOnExit();
1915 }
John Reck68234a92012-04-19 15:27:12 -07001916 }
1917 return false;
1918 }
Tarun Nainani8eb00912014-07-17 12:28:32 -07001919
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001920 String path = callback.getPath();
Tarun Nainani8eb00912014-07-17 12:28:32 -07001921 // could be that saving of file failed
1922 if (path == null) {
1923 return false;
1924 }
1925
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001926 File savedFile = new File(path);
1927 if (!savedFile.exists()) {
1928 return false;
John Reck68234a92012-04-19 15:27:12 -07001929 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001930 values.put(Snapshots.VIEWSTATE_PATH, path.substring(path.lastIndexOf('/') + 1));
1931 values.put(Snapshots.VIEWSTATE_SIZE, savedFile.length());
John Reck68234a92012-04-19 15:27:12 -07001932 return true;
1933 }
1934
John Reck8cc92352011-07-06 17:41:52 -07001935 public byte[] compressBitmap(Bitmap bitmap) {
1936 if (bitmap == null) {
1937 return null;
1938 }
1939 ByteArrayOutputStream stream = new ByteArrayOutputStream();
1940 bitmap.compress(CompressFormat.PNG, 100, stream);
1941 return stream.toByteArray();
1942 }
1943
John Reck26b18322011-06-21 13:08:58 -07001944 public void loadUrl(String url, Map<String, String> headers) {
1945 if (mMainView != null) {
Michael Kolba53c9892011-10-05 13:31:40 -07001946 mPageLoadProgress = INITIAL_PROGRESS;
Michael Kolb14612442011-06-24 13:06:29 -07001947 mCurrentState = new PageState(mContext, false, url, null);
John Reck26b18322011-06-21 13:08:58 -07001948 mMainView.loadUrl(url, headers);
1949 }
1950 }
1951
John Reck38b39652012-06-05 09:22:59 -07001952 public void disableUrlOverridingForLoad() {
1953 mDisableOverrideUrlLoading = true;
1954 }
1955
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001956 private void thumbnailUpdated() {
John Reck8ee633f2011-08-09 16:00:35 -07001957 mHandler.removeMessages(MSG_CAPTURE);
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001958
John Reck8ee633f2011-08-09 16:00:35 -07001959 TabControl tc = mWebViewController.getTabControl();
1960 if (tc != null) {
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001961 OnThumbnailUpdatedListener updateListener = tc.getOnThumbnailUpdatedListener();
John Reck8ee633f2011-08-09 16:00:35 -07001962 if (updateListener != null) {
1963 updateListener.onThumbnailUpdated(this);
1964 }
1965 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001966 }
1967
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001968 protected void capture() {
1969 if (mMainView == null || mCapture == null || !mMainView.isReady() ||
1970 mMainView.getContentWidth() <= 0 || mMainView.getContentHeight() <= 0 ||
1971 !mFirstVisualPixelPainted || mMainView.isShowingCrashView()) {
1972
1973 initCaptureBitmap();
1974 thumbnailUpdated();
1975 return;
1976 }
1977
1978 mMainView.getContentBitmapAsync((float) mCaptureWidth / mMainView.getWidth(), new Rect(),
1979 new ValueCallback<Bitmap>() {
1980 @Override
1981 public void onReceiveValue(Bitmap bitmap) {
1982 if (mCapture == null) {
1983 initCaptureBitmap();
1984 }
1985
1986 if (bitmap == null) {
1987 thumbnailUpdated();
1988 return;
1989 }
1990
1991 Canvas c = new Canvas(mCapture);
1992 mCapture.eraseColor(Color.WHITE);
1993 c.drawBitmap(bitmap, 0, 0, null);
1994
1995 // manually anti-alias the edges for the tilt
1996 c.drawRect(0, 0, 1, mCapture.getHeight(), sAlphaPaint);
1997 c.drawRect(mCapture.getWidth() - 1, 0, mCapture.getWidth(),
1998 mCapture.getHeight(), sAlphaPaint);
1999 c.drawRect(0, 0, mCapture.getWidth(), 1, sAlphaPaint);
2000 c.drawRect(0, mCapture.getHeight() - 1, mCapture.getWidth(),
2001 mCapture.getHeight(), sAlphaPaint);
2002 c.setBitmap(null);
2003
2004 persistThumbnail();
2005 thumbnailUpdated();
2006 }
2007 }
2008 );
John Reck1cf4b792011-07-26 10:22:22 -07002009 }
2010
Pankaj Garg6bedeba2015-06-23 15:47:37 -07002011 @Override
2012 public void onNewPicture(WebView view, Picture picture) {
Michael Kolb9ef259a2011-07-12 15:33:08 -07002013 }
2014
John Reckef654f12011-07-12 16:42:08 -07002015 public boolean canGoBack() {
2016 return mMainView != null ? mMainView.canGoBack() : false;
2017 }
2018
2019 public boolean canGoForward() {
2020 return mMainView != null ? mMainView.canGoForward() : false;
2021 }
2022
2023 public void goBack() {
2024 if (mMainView != null) {
2025 mMainView.goBack();
2026 }
2027 }
2028
2029 public void goForward() {
2030 if (mMainView != null) {
2031 mMainView.goForward();
2032 }
2033 }
2034
John Reck1cf4b792011-07-26 10:22:22 -07002035 protected void persistThumbnail() {
John Reck4eadc342011-10-31 14:04:10 -07002036 DataController.getInstance(mContext).saveThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07002037 }
2038
2039 protected void deleteThumbnail() {
John Reck4eadc342011-10-31 14:04:10 -07002040 DataController.getInstance(mContext).deleteThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07002041 }
2042
John Reck4eadc342011-10-31 14:04:10 -07002043 void updateCaptureFromBlob(byte[] blob) {
John Reck1cf4b792011-07-26 10:22:22 -07002044 synchronized (Tab.this) {
2045 if (mCapture == null) {
2046 return;
2047 }
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002048 ByteBuffer buffer = ByteBuffer.wrap(blob);
Michael Kolbd837a112011-08-09 14:00:18 -07002049 try {
2050 mCapture.copyPixelsFromBuffer(buffer);
2051 } catch (RuntimeException rex) {
2052 Log.e(LOGTAG, "Load capture has mismatched sizes; buffer: "
2053 + buffer.capacity() + " blob: " + blob.length
2054 + "capture: " + mCapture.getByteCount());
2055 throw rex;
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002056 }
John Reck1cf4b792011-07-26 10:22:22 -07002057 }
2058 }
2059
John Reck52be4782011-08-26 15:37:29 -07002060 @Override
2061 public String toString() {
2062 StringBuilder builder = new StringBuilder(100);
2063 builder.append(mId);
2064 builder.append(") has parent: ");
2065 if (getParent() != null) {
2066 builder.append("true[");
2067 builder.append(getParent().getId());
2068 builder.append("]");
2069 } else {
2070 builder.append("false");
2071 }
2072 builder.append(", incog: ");
2073 builder.append(isPrivateBrowsingEnabled());
2074 if (!isPrivateBrowsingEnabled()) {
2075 builder.append(", title: ");
2076 builder.append(getTitle());
2077 builder.append(", url: ");
2078 builder.append(getUrl());
2079 }
2080 return builder.toString();
2081 }
2082
Ze G Riande2a675c22015-06-03 11:15:24 -07002083 // dertermines if the tab contains a dislled page
2084 public boolean isDistilled() {
2085 if (!BrowserCommandLine.hasSwitch("reader-mode")) {
2086 return false;
2087 }
2088 try {
2089 return DomDistillerUtils.isUrlDistilled(getUrl());
2090 } catch (Exception e) {
2091 return false;
2092 }
2093 }
2094
2095 //determines if the tab contains a distillable page
2096 public boolean isDistillable() {
2097 if (!BrowserCommandLine.hasSwitch("reader-mode")) {
2098 mIsDistillable = false;
2099 return mIsDistillable;
2100 }
2101 final ValueCallback<String> onIsDistillable = new ValueCallback<String>() {
2102 @Override
2103 public void onReceiveValue(String str) {
2104 mIsDistillable = Boolean.parseBoolean(str);
2105 }
2106 };
2107
2108 if (isDistilled()) {
2109 mIsDistillable = true;
2110 return mIsDistillable;
2111 }
2112
2113 try {
2114 DomDistillerUtils.isWebViewDistillable(getWebView(), onIsDistillable);
2115 } catch (Exception e) {
2116 mIsDistillable = false;
2117 }
2118
2119 return mIsDistillable;
2120 }
2121
2122 // Function that sets the mIsDistillable variable
2123 public void setIsDistillable(boolean value) {
2124 if (!BrowserCommandLine.hasSwitch("reader-mode")) {
2125 mIsDistillable = false;
2126 }
2127 mIsDistillable = value;
2128 }
2129
2130 // Function that returns the distilled url of the current url
2131 public String getDistilledUrl() {
2132 if (getUrl() != null) {
2133 return DomDistillerUtils.getDistilledUrl(getUrl());
2134 }
2135 return new String();
2136 }
2137
2138 // function that returns the non-distilled version of the current url
2139 public String getNonDistilledUrl() {
2140 if (getUrl() != null) {
2141 return DomDistillerUtils.getOriginalUrlFromDistilledUrl(getUrl());
2142 }
2143 return new String();
2144 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002145}