blob: 3fc6eb55764b19b81fcc7cc14ecdb99f16d68ba1 [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;
39import android.net.http.SslError;
Grace Kloba22ac16e2009-10-07 18:00:23 -070040import android.os.Bundle;
Michael Kolb9ef259a2011-07-12 15:33:08 -070041import android.os.Handler;
Grace Kloba22ac16e2009-10-07 18:00:23 -070042import android.os.Message;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +000043import android.os.SystemClock;
Brian Carlstrom8862c1d2011-06-02 01:05:55 -070044import android.security.KeyChain;
Brian Carlstromaa09cd82011-06-09 16:04:40 -070045import android.security.KeyChainAliasCallback;
John Reck24f18262011-06-17 14:47:20 -070046import android.text.TextUtils;
Grace Kloba22ac16e2009-10-07 18:00:23 -070047import android.util.Log;
48import android.view.KeyEvent;
49import android.view.LayoutInflater;
50import android.view.View;
Grace Kloba50c241e2010-04-20 11:07:50 -070051import android.view.ViewStub;
Ben Murdochc42addf2010-01-28 15:19:59 +000052import android.webkit.ConsoleMessage;
Grace Kloba22ac16e2009-10-07 18:00:23 -070053import android.webkit.URLUtil;
John Reck438bf462011-01-12 18:11:46 -080054import android.webkit.WebResourceResponse;
Grace Kloba22ac16e2009-10-07 18:00:23 -070055import android.webkit.WebStorage;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080056import android.webkit.WebChromeClient.CustomViewCallback;
57import android.webkit.ValueCallback;
Ben Murdoch1d676b62011-01-17 12:54:24 +000058import android.widget.CheckBox;
Ben Murdoch8029a772010-11-16 11:58:21 +000059import android.widget.Toast;
Grace Kloba22ac16e2009-10-07 18:00:23 -070060
Bijan Amirzada41242f22014-03-21 12:12:18 -070061import com.android.browser.TabControl.OnThumbnailUpdatedListener;
62import com.android.browser.homepages.HomeProvider;
63import com.android.browser.mynavigation.MyNavigationUtil;
64import com.android.browser.provider.MyNavigationProvider;
65import com.android.browser.provider.SnapshotProvider.Snapshots;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080066
Pankaj Garg18aa0a12015-06-22 11:06:12 -070067import org.codeaurora.swe.BrowserCommandLine;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080068import org.codeaurora.swe.BrowserDownloadListener;
69import org.codeaurora.swe.ClientCertRequestHandler;
70import org.codeaurora.swe.HttpAuthHandler;
71import org.codeaurora.swe.SslErrorHandler;
72import org.codeaurora.swe.WebBackForwardList;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080073import org.codeaurora.swe.WebChromeClient;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080074import org.codeaurora.swe.WebView;
75import org.codeaurora.swe.WebView.PictureListener;
Pankaj Garg1c7380d2014-08-27 14:17:12 -070076import org.codeaurora.swe.WebView.CreateWindowParams;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080077import org.codeaurora.swe.WebViewClient;
Pankaj Garg1c13cab2015-05-12 11:52:17 -070078import org.codeaurora.swe.util.Observable;
Ze G Riande2a675c22015-06-03 11:15:24 -070079import org.codeaurora.swe.DomDistillerUtils;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080080
John Reck541f55a2011-06-07 16:34:43 -070081import java.io.ByteArrayOutputStream;
John Reck2b71d6d2012-04-18 17:42:06 -070082import java.io.File;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080083import java.io.InputStream;
John Reck1cf4b792011-07-26 10:22:22 -070084import java.nio.ByteBuffer;
Michael Kolbfe251992010-07-08 15:41:55 -070085import java.util.Map;
John Reck2b71d6d2012-04-18 17:42:06 -070086import java.util.UUID;
Michael Kolbfe251992010-07-08 15:41:55 -070087import java.util.Vector;
Vivek Sekhar53ef8932015-06-18 16:51:43 -070088import java.util.List;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080089import java.sql.Timestamp;
90import java.util.Date;
Michael Kolbfe251992010-07-08 15:41:55 -070091
Grace Kloba22ac16e2009-10-07 18:00:23 -070092/**
93 * Class for maintaining Tabs with a main WebView and a subwindow.
94 */
Michael Kolb9ef259a2011-07-12 15:33:08 -070095class Tab implements PictureListener {
Michael Kolb8233fac2010-10-26 16:08:53 -070096
Grace Kloba22ac16e2009-10-07 18:00:23 -070097 // Log Tag
98 private static final String LOGTAG = "Tab";
Bijan Amirzada41242f22014-03-21 12:12:18 -070099 private static final boolean LOGD_ENABLED = com.android.browser.Browser.LOGD_ENABLED;
Ben Murdochc42addf2010-01-28 15:19:59 +0000100 // Special case the logtag for messages for the Console to make it easier to
101 // filter them and match the logtag used for these messages in older versions
102 // of the browser.
103 private static final String CONSOLE_LOGTAG = "browser";
104
Michael Kolb9ef259a2011-07-12 15:33:08 -0700105 private static final int MSG_CAPTURE = 42;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800106 private static final int CAPTURE_DELAY = 1000;
Michael Kolba53c9892011-10-05 13:31:40 -0700107 private static final int INITIAL_PROGRESS = 5;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700108
John Reck1cf4b792011-07-26 10:22:22 -0700109 private static Bitmap sDefaultFavicon;
Sagar Dhawan33551ff2015-07-08 17:24:44 -0700110 private boolean mIsKeyboardUp = false;
John Reck1cf4b792011-07-26 10:22:22 -0700111
Michael Kolba3194d02011-09-07 11:23:51 -0700112 private static Paint sAlphaPaint = new Paint();
113 static {
114 sAlphaPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
115 sAlphaPaint.setColor(Color.TRANSPARENT);
116 }
117
Steve Block2466eff2011-10-03 15:33:09 +0100118 public enum SecurityState {
Steve Block4895b012011-10-03 16:26:46 +0100119 // The page's main resource does not use SSL. Note that we use this
120 // state irrespective of the SSL authentication state of sub-resources.
Steve Block2466eff2011-10-03 15:33:09 +0100121 SECURITY_STATE_NOT_SECURE,
Steve Block4895b012011-10-03 16:26:46 +0100122 // The page's main resource uses SSL and the certificate is good. The
123 // same is true of all sub-resources.
Steve Block2466eff2011-10-03 15:33:09 +0100124 SECURITY_STATE_SECURE,
Steve Block4895b012011-10-03 16:26:46 +0100125 // The page's main resource uses SSL and the certificate is good, but
126 // some sub-resources either do not use SSL or have problems with their
127 // certificates.
Steve Block2466eff2011-10-03 15:33:09 +0100128 SECURITY_STATE_MIXED,
Steve Block4895b012011-10-03 16:26:46 +0100129 // The page's main resource uses SSL but there is a problem with its
130 // certificate.
131 SECURITY_STATE_BAD_CERTIFICATE,
John Reck30c714c2010-12-16 17:30:34 -0800132 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700133
Michael Kolb14612442011-06-24 13:06:29 -0700134 Context mContext;
John Reckd8c74522011-06-14 08:45:00 -0700135 protected WebViewController mWebViewController;
Michael Kolb8233fac2010-10-26 16:08:53 -0700136
Michael Kolbc831b632011-05-11 09:30:34 -0700137 // The tab ID
John Reckd8c74522011-06-14 08:45:00 -0700138 private long mId = -1;
Michael Kolbc831b632011-05-11 09:30:34 -0700139
Grace Kloba22ac16e2009-10-07 18:00:23 -0700140 // Main WebView wrapper
Michael Kolba713ec82010-11-29 17:27:06 -0800141 private View mContainer;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700142 // Main WebView
143 private WebView mMainView;
144 // Subwindow container
145 private View mSubViewContainer;
146 // Subwindow WebView
147 private WebView mSubView;
148 // Saved bundle for when we are running low on memory. It contains the
149 // information needed to restore the WebView if the user goes back to the
150 // tab.
151 private Bundle mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700152 // Parent Tab. This is the Tab that created this Tab, or null if the Tab was
153 // created by the UI
Michael Kolbc831b632011-05-11 09:30:34 -0700154 private Tab mParent;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700155 // Tab that constructed by this Tab. This is used when this Tab is
156 // destroyed, it clears all mParentTab values in the children.
Michael Kolbc831b632011-05-11 09:30:34 -0700157 private Vector<Tab> mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700158 // If true, the tab is in the foreground of the current activity.
159 private boolean mInForeground;
Michael Kolb8233fac2010-10-26 16:08:53 -0700160 // If true, the tab is in page loading state (after onPageStarted,
161 // before onPageFinsihed)
162 private boolean mInPageLoad;
Tarun Nainani8eb00912014-07-17 12:28:32 -0700163 private boolean mPageFinished;
John Reck38b39652012-06-05 09:22:59 -0700164 private boolean mDisableOverrideUrlLoading;
Pankaj Garg79878492015-04-01 14:48:21 -0700165 private boolean mFirstVisualPixelPainted = false;
John Reck30c714c2010-12-16 17:30:34 -0800166 // The last reported progress of the current page
167 private int mPageLoadProgress;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000168 // The time the load started, used to find load page time
169 private long mLoadStartTime;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700170 // Application identifier used to find tabs that another application wants
171 // to reuse.
172 private String mAppId;
Michael Kolbe28b3472011-08-04 16:54:31 -0700173 // flag to indicate if tab should be closed on back
174 private boolean mCloseOnBack;
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700175 // flag to indicate if the tab was opened from an intent
176 private boolean mDerivedFromIntent = false;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -0500177 // The listener that gets invoked when a download is started from the
178 // mMainView
Selim Gurun0b3d66f2012-08-29 13:08:13 -0700179 private final BrowserDownloadListener mDownloadListener;
John Recke969cc52010-12-21 17:24:43 -0800180 private DataController mDataController;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700181
182 // AsyncTask for downloading touch icons
183 DownloadTouchIcon mTouchIconLoader;
184
John Reck35e9dd62011-04-25 09:01:54 -0700185 private BrowserSettings mSettings;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700186 private int mCaptureWidth;
187 private int mCaptureHeight;
188 private Bitmap mCapture;
189 private Handler mHandler;
Michael Kolb72864272012-05-03 15:42:15 -0700190 private boolean mUpdateThumbnail;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800191 private Timestamp timestamp;
Vivek Sekharf96064b2014-07-28 16:32:34 -0700192 private boolean mFullScreen = false;
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800193 private boolean mReceivedError;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700194
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -0700195 // determine if webview is destroyed to MemoryMonitor
196 private boolean mWebViewDestroyedByMemoryMonitor;
197
Pankaj Garg21dad562015-07-02 17:17:24 -0700198 private String mTouchIconUrl;
199
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700200 private Observable mFirstPixelObservable;
201 private Observable mTabHistoryUpdateObservable;
202
203 Observable getFirstPixelObservable() {
204 return mFirstPixelObservable;
205 }
206
207 Observable getTabHistoryUpdateObservable() {
208 return mTabHistoryUpdateObservable;
209 }
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -0700210
Ze G Riande2a675c22015-06-03 11:15:24 -0700211 // dertermines if the tab contains a disllable page
212 private boolean mIsDistillable = false;
Mathew Inwood1dd8e822011-08-03 14:34:29 +0100213
John Reck1cf4b792011-07-26 10:22:22 -0700214 private static synchronized Bitmap getDefaultFavicon(Context context) {
215 if (sDefaultFavicon == null) {
216 sDefaultFavicon = BitmapFactory.decodeResource(
Enrico Rosd6efa972014-12-02 19:49:59 -0800217 context.getResources(), R.drawable.ic_deco_favicon_normal);
John Reck1cf4b792011-07-26 10:22:22 -0700218 }
219 return sDefaultFavicon;
220 }
Michael Kolbeb95db42011-03-03 10:38:40 -0800221
John Reck30c714c2010-12-16 17:30:34 -0800222 // All the state needed for a page
John Reckd8c74522011-06-14 08:45:00 -0700223 protected static class PageState {
John Reck30c714c2010-12-16 17:30:34 -0800224 String mUrl;
John Reckdb22ec42011-06-29 11:31:24 -0700225 String mOriginalUrl;
John Reck30c714c2010-12-16 17:30:34 -0800226 String mTitle;
Steve Block2466eff2011-10-03 15:33:09 +0100227 SecurityState mSecurityState;
Steve Block08a6f0c2011-10-06 12:12:53 +0100228 // This is non-null only when mSecurityState is SECURITY_STATE_BAD_CERTIFICATE.
229 SslError mSslCertificateError;
John Reck30c714c2010-12-16 17:30:34 -0800230 Bitmap mFavicon;
Steve Block08a6f0c2011-10-06 12:12:53 +0100231 boolean mIsBookmarkedSite;
232 boolean mIncognito;
John Reck30c714c2010-12-16 17:30:34 -0800233
234 PageState(Context c, boolean incognito) {
John Reck502a3532011-08-16 14:21:46 -0700235 mIncognito = incognito;
236 if (mIncognito) {
Vivek Sekhared791da2015-02-22 12:39:05 -0800237 mOriginalUrl = mUrl = "chrome://incognito";
John Reck30c714c2010-12-16 17:30:34 -0800238 mTitle = c.getString(R.string.new_incognito_tab);
John Reck30c714c2010-12-16 17:30:34 -0800239 } else {
John Reckdb22ec42011-06-29 11:31:24 -0700240 mOriginalUrl = mUrl = "";
John Reck30c714c2010-12-16 17:30:34 -0800241 mTitle = c.getString(R.string.new_tab);
John Reck30c714c2010-12-16 17:30:34 -0800242 }
Steve Block2466eff2011-10-03 15:33:09 +0100243 mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
John Reck30c714c2010-12-16 17:30:34 -0800244 }
245
246 PageState(Context c, boolean incognito, String url, Bitmap favicon) {
John Reck502a3532011-08-16 14:21:46 -0700247 mIncognito = incognito;
John Reckdb22ec42011-06-29 11:31:24 -0700248 mOriginalUrl = mUrl = url;
John Reck30c714c2010-12-16 17:30:34 -0800249 if (URLUtil.isHttpsUrl(url)) {
Steve Block2466eff2011-10-03 15:33:09 +0100250 mSecurityState = SecurityState.SECURITY_STATE_SECURE;
John Reck30c714c2010-12-16 17:30:34 -0800251 } else {
Steve Block2466eff2011-10-03 15:33:09 +0100252 mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
John Reck30c714c2010-12-16 17:30:34 -0800253 }
John Reck1cf4b792011-07-26 10:22:22 -0700254 mFavicon = favicon;
John Reck30c714c2010-12-16 17:30:34 -0800255 }
John Reck1cf4b792011-07-26 10:22:22 -0700256
Grace Kloba22ac16e2009-10-07 18:00:23 -0700257 }
258
John Reck30c714c2010-12-16 17:30:34 -0800259 // The current/loading page's state
John Reckd8c74522011-06-14 08:45:00 -0700260 protected PageState mCurrentState;
John Reck30c714c2010-12-16 17:30:34 -0800261
Grace Kloba22ac16e2009-10-07 18:00:23 -0700262 // Used for saving and restoring each Tab
Michael Kolbc831b632011-05-11 09:30:34 -0700263 static final String ID = "ID";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700264 static final String CURRURL = "currentUrl";
265 static final String CURRTITLE = "currentTitle";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700266 static final String PARENTTAB = "parentTab";
267 static final String APPID = "appid";
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700268 static final String INCOGNITO = "privateBrowsingEnabled";
John Reckb0a86db2011-05-24 14:05:58 -0700269 static final String USERAGENT = "useragent";
Michael Kolbe28b3472011-08-04 16:54:31 -0700270 static final String CLOSEFLAG = "closeOnBack";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700271
Pankaj Garg18186a92015-03-31 14:59:33 -0700272 public void setNetworkAvailable(boolean networkUp) {
273 if (networkUp && mReceivedError && (mMainView != null)) {
274 mMainView.reload();
275 }
276 }
277
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700278 public boolean isFirstVisualPixelPainted() {
279 return mFirstVisualPixelPainted;
280 }
281
282 public int getCaptureIndex(int navIndex) {
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700283 int orientation = mWebViewController.getActivity().
284 getResources().getConfiguration().orientation;
285
286 int orientationBit = (orientation == Configuration.ORIENTATION_LANDSCAPE) ? 0 : 1;
287
Vivek Sekhard0f60402015-06-05 14:07:11 -0700288 int index = orientationBit << 31 | (((int)mId & 0x7f) << 24) | (navIndex & 0xffffff);
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700289 return index;
290 }
291
292 public int getTabIdxFromCaptureIdx(int index) {
293 return (index & 0x7f000000) >> 24;
294 }
295
296 public int getOrientationFromCaptureIdx(int index) {
297 return ((index & 0x80000000) == 0) ? Configuration.ORIENTATION_LANDSCAPE :
298 Configuration.ORIENTATION_PORTRAIT;
299
300 }
301
302 public int getNavIdxFromCaptureIdx(int index) {
303 return (index & 0xffffff);
304 }
305
Grace Kloba22ac16e2009-10-07 18:00:23 -0700306 // -------------------------------------------------------------------------
307 // WebViewClient implementation for the main WebView
308 // -------------------------------------------------------------------------
309
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800310 private final WebViewClient mWebViewClient = new WebViewClient() {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500311 private Message mDontResend;
312 private Message mResend;
Michael Kolb47bd1e42011-09-01 15:25:00 -0700313
314 private boolean providersDiffer(String url, String otherUrl) {
315 Uri uri1 = Uri.parse(url);
316 Uri uri2 = Uri.parse(otherUrl);
317 return !uri1.getEncodedAuthority().equals(uri2.getEncodedAuthority());
318 }
319
Grace Kloba22ac16e2009-10-07 18:00:23 -0700320 @Override
321 public void onPageStarted(WebView view, String url, Bitmap favicon) {
Ze G Riande2a675c22015-06-03 11:15:24 -0700322 setIsDistillable(false);
Michael Kolb8233fac2010-10-26 16:08:53 -0700323 mInPageLoad = true;
Tarun Nainani8eb00912014-07-17 12:28:32 -0700324 mPageFinished = false;
Pankaj Garg79878492015-04-01 14:48:21 -0700325 mFirstVisualPixelPainted = false;
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700326 mFirstPixelObservable.set(false);
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800327 mReceivedError = false;
Michael Kolb72864272012-05-03 15:42:15 -0700328 mUpdateThumbnail = true;
Michael Kolba53c9892011-10-05 13:31:40 -0700329 mPageLoadProgress = INITIAL_PROGRESS;
Michael Kolb14612442011-06-24 13:06:29 -0700330 mCurrentState = new PageState(mContext,
John Reck30c714c2010-12-16 17:30:34 -0800331 view.isPrivateBrowsingEnabled(), url, favicon);
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000332 mLoadStartTime = SystemClock.uptimeMillis();
Pankaj Garg62bc7912015-04-14 16:08:59 -0700333 // Need re-enable FullScreenMode on Page navigation if needed
334 if (BrowserSettings.getInstance().useFullscreen()){
335 Controller controller = (Controller) mWebViewController;
336 BaseUi ui = (BaseUi) controller.getUi();
337 ui.forceDisableFullscreenMode(false);
338 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700339 // If we start a touch icon load and then load a new page, we don't
340 // want to cancel the current touch icon loader. But, we do want to
341 // create a new one when the touch icon url is known.
342 if (mTouchIconLoader != null) {
343 mTouchIconLoader.mTab = null;
344 mTouchIconLoader = null;
345 }
346
Grace Kloba22ac16e2009-10-07 18:00:23 -0700347 // finally update the UI in the activity if it is in the foreground
John Reck324d4402011-01-11 16:56:42 -0800348 mWebViewController.onPageStarted(Tab.this, view, favicon);
Leon Scroggins4cd97792010-12-03 15:31:56 -0500349
John Recke969cc52010-12-21 17:24:43 -0800350 updateBookmarkedStatus();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700351 }
352
353 @Override
354 public void onPageFinished(WebView view, String url) {
John Reck38b39652012-06-05 09:22:59 -0700355 mDisableOverrideUrlLoading = false;
John Reck5b691842010-11-29 11:21:13 -0800356 if (!isPrivateBrowsingEnabled()) {
357 LogTag.logPageFinishedLoading(
358 url, SystemClock.uptimeMillis() - mLoadStartTime);
359 }
John Reck1cf4b792011-07-26 10:22:22 -0700360 syncCurrentState(view, url);
John Reck324d4402011-01-11 16:56:42 -0800361 mWebViewController.onPageFinished(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700362 }
363
Pankaj Garg79878492015-04-01 14:48:21 -0700364 @Override
365 public void onFirstVisualPixel(WebView view) {
366 mFirstVisualPixelPainted = true;
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700367 mFirstPixelObservable.set(true);
Pankaj Garg79878492015-04-01 14:48:21 -0700368 }
369
Grace Kloba22ac16e2009-10-07 18:00:23 -0700370 // return true if want to hijack the url to let another app to handle it
371 @Override
372 public boolean shouldOverrideUrlLoading(WebView view, String url) {
John Reck38b39652012-06-05 09:22:59 -0700373 if (!mDisableOverrideUrlLoading && mInForeground) {
Michael Kolb18eb3772010-12-10 14:29:51 -0800374 return mWebViewController.shouldOverrideUrlLoading(Tab.this,
375 view, url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700376 } else {
377 return false;
378 }
379 }
380
Vivek Sekharb991edb2014-12-17 18:18:07 -0800381 @Override
382 public boolean shouldDownloadFavicon(WebView view, String url) {
383 return true;
384 }
385
Grace Kloba22ac16e2009-10-07 18:00:23 -0700386 /**
Steve Block2466eff2011-10-03 15:33:09 +0100387 * Updates the security state. This method is called when we discover
388 * another resource to be loaded for this page (for example,
389 * javascript). While we update the security state, we do not update
390 * the lock icon until we are done loading, as it is slightly more
391 * secure this way.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700392 */
393 @Override
394 public void onLoadResource(WebView view, String url) {
395 if (url != null && url.length() > 0) {
396 // It is only if the page claims to be secure that we may have
Steve Block2466eff2011-10-03 15:33:09 +0100397 // to update the security state:
398 if (mCurrentState.mSecurityState == SecurityState.SECURITY_STATE_SECURE) {
399 // If NOT a 'safe' url, change the state to mixed content!
Grace Kloba22ac16e2009-10-07 18:00:23 -0700400 if (!(URLUtil.isHttpsUrl(url) || URLUtil.isDataUrl(url)
401 || URLUtil.isAboutUrl(url))) {
Steve Block2466eff2011-10-03 15:33:09 +0100402 mCurrentState.mSecurityState = SecurityState.SECURITY_STATE_MIXED;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700403 }
404 }
405 }
406 }
407
408 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700409 * Show a dialog informing the user of the network error reported by
410 * WebCore if it is in the foreground.
411 */
412 @Override
413 public void onReceivedError(WebView view, int errorCode,
414 String description, String failingUrl) {
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800415 // Used for the syncCurrentState to use
416 // the failing url instead of using webview url
417 mReceivedError = true;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700418 }
419
420 /**
421 * Check with the user if it is ok to resend POST data as the page they
422 * are trying to navigate to is the result of a POST.
423 */
424 @Override
425 public void onFormResubmission(WebView view, final Message dontResend,
426 final Message resend) {
427 if (!mInForeground) {
428 dontResend.sendToTarget();
429 return;
430 }
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500431 if (mDontResend != null) {
432 Log.w(LOGTAG, "onFormResubmission should not be called again "
433 + "while dialog is still up");
434 dontResend.sendToTarget();
435 return;
436 }
437 mDontResend = dontResend;
438 mResend = resend;
Michael Kolb14612442011-06-24 13:06:29 -0700439 new AlertDialog.Builder(mContext).setTitle(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700440 R.string.browserFrameFormResubmitLabel).setMessage(
441 R.string.browserFrameFormResubmitMessage)
442 .setPositiveButton(R.string.ok,
443 new DialogInterface.OnClickListener() {
444 public void onClick(DialogInterface dialog,
445 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500446 if (mResend != null) {
447 mResend.sendToTarget();
448 mResend = null;
449 mDontResend = null;
450 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700451 }
452 }).setNegativeButton(R.string.cancel,
453 new DialogInterface.OnClickListener() {
454 public void onClick(DialogInterface dialog,
455 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500456 if (mDontResend != null) {
457 mDontResend.sendToTarget();
458 mResend = null;
459 mDontResend = null;
460 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700461 }
462 }).setOnCancelListener(new OnCancelListener() {
463 public void onCancel(DialogInterface dialog) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500464 if (mDontResend != null) {
465 mDontResend.sendToTarget();
466 mResend = null;
467 mDontResend = null;
468 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700469 }
470 }).show();
471 }
472
473 /**
474 * Insert the url into the visited history database.
475 * @param url The url to be inserted.
476 * @param isReload True if this url is being reloaded.
477 * FIXME: Not sure what to do when reloading the page.
478 */
479 @Override
480 public void doUpdateVisitedHistory(WebView view, String url,
481 boolean isReload) {
John Reck324d4402011-01-11 16:56:42 -0800482 mWebViewController.doUpdateVisitedHistory(Tab.this, isReload);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700483 }
484
485 /**
486 * Displays SSL error(s) dialog to the user.
487 */
488 @Override
489 public void onReceivedSslError(final WebView view,
490 final SslErrorHandler handler, final SslError error) {
491 if (!mInForeground) {
492 handler.cancel();
Steve Block2466eff2011-10-03 15:33:09 +0100493 setSecurityState(SecurityState.SECURITY_STATE_NOT_SECURE);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700494 return;
495 }
John Reck35e9dd62011-04-25 09:01:54 -0700496 if (mSettings.showSecurityWarnings()) {
John Reckcb28b2c2011-08-26 17:39:44 -0700497 new AlertDialog.Builder(mContext)
498 .setTitle(R.string.security_warning)
499 .setMessage(R.string.ssl_warnings_header)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200500 .setIconAttribute(android.R.attr.alertDialogIcon)
John Reckcb28b2c2011-08-26 17:39:44 -0700501 .setPositiveButton(R.string.ssl_continue,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700502 new DialogInterface.OnClickListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700503 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700504 public void onClick(DialogInterface dialog,
505 int whichButton) {
506 handler.proceed();
Steve Block4895b012011-10-03 16:26:46 +0100507 handleProceededAfterSslError(error);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700508 }
John Reckcb28b2c2011-08-26 17:39:44 -0700509 })
510 .setNeutralButton(R.string.view_certificate,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700511 new DialogInterface.OnClickListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700512 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700513 public void onClick(DialogInterface dialog,
514 int whichButton) {
John Reckcb28b2c2011-08-26 17:39:44 -0700515 mWebViewController.showSslCertificateOnError(
516 view, handler, error);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700517 }
John Reckcb28b2c2011-08-26 17:39:44 -0700518 })
519 .setNegativeButton(R.string.ssl_go_back,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700520 new DialogInterface.OnClickListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700521 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700522 public void onClick(DialogInterface dialog,
523 int whichButton) {
John Reck30c714c2010-12-16 17:30:34 -0800524 dialog.cancel();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700525 }
John Reckcb28b2c2011-08-26 17:39:44 -0700526 })
527 .setOnCancelListener(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700528 new DialogInterface.OnCancelListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700529 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700530 public void onCancel(DialogInterface dialog) {
531 handler.cancel();
Steve Block2466eff2011-10-03 15:33:09 +0100532 setSecurityState(SecurityState.SECURITY_STATE_NOT_SECURE);
John Reck30c714c2010-12-16 17:30:34 -0800533 mWebViewController.onUserCanceledSsl(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700534 }
John Reckcb28b2c2011-08-26 17:39:44 -0700535 })
536 .show();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700537 } else {
538 handler.proceed();
539 }
540 }
541
542 /**
Steve Block4895b012011-10-03 16:26:46 +0100543 * Called when an SSL error occurred while loading a resource, but the
544 * WebView but chose to proceed anyway based on a decision retained
545 * from a previous response to onReceivedSslError(). We update our
546 * security state to reflect this.
547 */
548 @Override
549 public void onProceededAfterSslError(WebView view, SslError error) {
550 handleProceededAfterSslError(error);
551 }
552
553 /**
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700554 * Displays client certificate request to the user.
555 */
556 @Override
557 public void onReceivedClientCertRequest(final WebView view,
558 final ClientCertRequestHandler handler, final String host_and_port) {
559 if (!mInForeground) {
560 handler.ignore();
561 return;
562 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700563 int colon = host_and_port.lastIndexOf(':');
564 String host;
565 int port;
566 if (colon == -1) {
567 host = host_and_port;
568 port = -1;
569 } else {
570 String portString = host_and_port.substring(colon + 1);
571 try {
572 port = Integer.parseInt(portString);
573 host = host_and_port.substring(0, colon);
574 } catch (NumberFormatException e) {
575 host = host_and_port;
576 port = -1;
577 }
578 }
Michael Kolb14612442011-06-24 13:06:29 -0700579 KeyChain.choosePrivateKeyAlias(
580 mWebViewController.getActivity(), new KeyChainAliasCallback() {
Sagar Dhawan49f85cf2015-07-10 16:54:20 -0700581 @Override
582 public void alias(String alias) {
583 if (alias == null) {
584 handler.cancel();
585 return;
586 }
587 new KeyChainLookup(mContext, handler, alias).execute();
588 }
589 }, null, null, host, port, null);
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700590 }
591
592 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700593 * Handles an HTTP authentication request.
594 *
595 * @param handler The authentication handler
596 * @param host The host
597 * @param realm The realm
598 */
599 @Override
600 public void onReceivedHttpAuthRequest(WebView view,
601 final HttpAuthHandler handler, final String host,
602 final String realm) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700603 mWebViewController.onReceivedHttpAuthRequest(Tab.this, view, handler, host, realm);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700604 }
605
606 @Override
John Reck438bf462011-01-12 18:11:46 -0800607 public WebResourceResponse shouldInterceptRequest(WebView view,
608 String url) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800609 //intercept if opening a new incognito tab - show the incognito welcome page
Vivek Sekhared791da2015-02-22 12:39:05 -0800610 if (url.startsWith("chrome://incognito")) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800611 Resources resourceHandle = mContext.getResources();
612 InputStream inStream = resourceHandle.openRawResource(
Bijan Amirzada41242f22014-03-21 12:12:18 -0700613 com.android.browser.R.raw.incognito_mode_start_page);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800614 return new WebResourceResponse("text/html", "utf8", inStream);
615 }
kaiyiz6e5b3e02013-08-19 20:02:01 +0800616 WebResourceResponse res;
617 if (MyNavigationUtil.MY_NAVIGATION.equals(url)) {
618 res = MyNavigationProvider.shouldInterceptRequest(mContext, url);
619 } else {
620 res = HomeProvider.shouldInterceptRequest(mContext, url);
621 }
John Reck438bf462011-01-12 18:11:46 -0800622 return res;
623 }
624
625 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700626 public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
627 if (!mInForeground) {
628 return false;
629 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700630 return mWebViewController.shouldOverrideKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700631 }
632
633 @Override
634 public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700635 if (!mInForeground) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700636 return;
637 }
John Reck997b1b72012-04-19 18:08:25 -0700638 if (!mWebViewController.onUnhandledKeyEvent(event)) {
639 super.onUnhandledKeyEvent(view, event);
640 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700641 }
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700642
643 @Override
644 public void beforeNavigation(WebView view, String url) {
Pankaj Garg21dad562015-07-02 17:17:24 -0700645 mTouchIconUrl = null;
Site Mao61b68212015-07-16 10:56:31 -0700646 if (BaseUi.isUiLowPowerMode()) {
Pankaj Garg18aa0a12015-06-22 11:06:12 -0700647 return;
648 }
649
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700650 if (isPrivateBrowsingEnabled()) {
651 return;
652 }
653
654 if (!mFirstVisualPixelPainted) {
655 return;
656 }
657
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -0700658
659
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700660 final int idx = view.copyBackForwardList().getCurrentIndex();
661 boolean bitmapExists = view.hasSnapshot(idx);
662
663 int progress = 100;
664 Controller controller = (Controller)mWebViewController;
665 UI ui = controller.getUi();
666 if (ui instanceof BaseUi) {
667 BaseUi baseUi = (BaseUi) ui;
668 TitleBar titleBar = baseUi.getTitleBar();
669 progress = titleBar.getProgressView().getProgressPercent();
670 }
671
672 if (bitmapExists && progress < 85) {
673 return;
674 }
675
676 int index = getCaptureIndex(view.getLastCommittedHistoryIndex());
Sagar Dhawan49f85cf2015-07-10 16:54:20 -0700677 view.captureSnapshot(index, null);
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700678 }
679
680 @Override
681 public void onHistoryItemCommit(WebView view, int index) {
Site Mao61b68212015-07-16 10:56:31 -0700682 if (BaseUi.isUiLowPowerMode()) {
Pankaj Garg18aa0a12015-06-22 11:06:12 -0700683 return;
684 }
685
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -0700686 // prevent snapshot tab from commiting any history
687 if (isSnapshot()) {
688 return;
689 }
690
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700691 mTabHistoryUpdateObservable.set(index);
Vivek Sekhar53ef8932015-06-18 16:51:43 -0700692 final int maxIdx = view.copyBackForwardList().getSize();
693 final WebView wv = view;
694 view.getSnapshotIds(new ValueCallback <List<Integer>>() {
695 @Override
696 public void onReceiveValue(List<Integer> ids) {
697 int currentTabIdx = mWebViewController.getTabControl().getCurrentPosition();
698 for (Integer id : ids) {
699 if (getTabIdxFromCaptureIdx(id) == currentTabIdx &&
700 getNavIdxFromCaptureIdx(id) >= maxIdx) {
701 wv.deleteSnapshot(id);
702 }
703 }
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700704 }
Vivek Sekhar53ef8932015-06-18 16:51:43 -0700705 });
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700706 }
Pankaj Garg62bc7912015-04-14 16:08:59 -0700707
708 @Override
709 public void onKeyboardStateChange(boolean popup) {
Sagar Dhawan33551ff2015-07-08 17:24:44 -0700710 mIsKeyboardUp = popup;
Pankaj Garg62bc7912015-04-14 16:08:59 -0700711 if (BrowserSettings.getInstance().useFullscreen()) {
712 Controller controller = (Controller) mWebViewController;
713 BaseUi ui = (BaseUi) controller.getUi();
714 ui.forceDisableFullscreenMode(popup);
715 }
716 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700717 };
718
John Reck1cf4b792011-07-26 10:22:22 -0700719 private void syncCurrentState(WebView view, String url) {
720 // Sync state (in case of stop/timeout)
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800721
722 if (mReceivedError) {
723 mCurrentState.mUrl = url;
724 mCurrentState.mOriginalUrl = url;
725 } else {
726 mCurrentState.mUrl = view.getUrl();
727 mCurrentState.mOriginalUrl = view.getOriginalUrl();
728 mCurrentState.mFavicon = view.getFavicon();
729 }
730
John Reck1cf4b792011-07-26 10:22:22 -0700731 if (mCurrentState.mUrl == null) {
John Reck8bcafc12011-08-29 16:43:02 -0700732 mCurrentState.mUrl = "";
John Reck1cf4b792011-07-26 10:22:22 -0700733 }
John Reck1cf4b792011-07-26 10:22:22 -0700734 mCurrentState.mTitle = view.getTitle();
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800735
736
John Reck1cf4b792011-07-26 10:22:22 -0700737 if (!URLUtil.isHttpsUrl(mCurrentState.mUrl)) {
738 // In case we stop when loading an HTTPS page from an HTTP page
739 // but before a provisional load occurred
Steve Block2466eff2011-10-03 15:33:09 +0100740 mCurrentState.mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
Steve Block08a6f0c2011-10-06 12:12:53 +0100741 mCurrentState.mSslCertificateError = null;
John Reck1cf4b792011-07-26 10:22:22 -0700742 }
John Reck502a3532011-08-16 14:21:46 -0700743 mCurrentState.mIncognito = view.isPrivateBrowsingEnabled();
John Reck1cf4b792011-07-26 10:22:22 -0700744 }
745
Pankaj Garg21dad562015-07-02 17:17:24 -0700746 public String getTouchIconUrl() {
747 return mTouchIconUrl;
748 }
Tarun Nainani8eb00912014-07-17 12:28:32 -0700749
Sagar Dhawan33551ff2015-07-08 17:24:44 -0700750 public boolean isKeyboardShowing() {
751 Controller controller = (Controller)mWebViewController;
752 return (mIsKeyboardUp || controller.getUi().isEditingUrl());
753 }
754
Tarun Nainani8eb00912014-07-17 12:28:32 -0700755 public boolean isTabFullScreen() {
756 return mFullScreen;
757 }
758
Vivek Sekharf96064b2014-07-28 16:32:34 -0700759 protected void setTabFullscreen(boolean fullScreen) {
Tarun Nainani8eb00912014-07-17 12:28:32 -0700760 Controller controller = (Controller)mWebViewController;
Sudheer Koganti24766882014-10-02 10:58:09 -0700761 controller.getUi().showFullscreen(fullScreen);
Tarun Nainani8eb00912014-07-17 12:28:32 -0700762 mFullScreen = fullScreen;
Vivek Sekharf96064b2014-07-28 16:32:34 -0700763 }
764
Sudheer Koganti24766882014-10-02 10:58:09 -0700765 public boolean exitFullscreen() {
766 if (mFullScreen) {
767 Controller controller = (Controller)mWebViewController;
768 controller.getUi().showFullscreen(false);
769 if (getWebView() != null)
770 getWebView().exitFullscreen();
771 mFullScreen = false;
772 return true;
773 }
774 return false;
775 }
776
777
778
779
Grace Kloba22ac16e2009-10-07 18:00:23 -0700780 // -------------------------------------------------------------------------
781 // WebChromeClient implementation for the main WebView
782 // -------------------------------------------------------------------------
783
784 private final WebChromeClient mWebChromeClient = new WebChromeClient() {
785 // Helper method to create a new tab or sub window.
786 private void createWindow(final boolean dialog, final Message msg) {
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700787 this.createWindow(dialog, msg, null, false);
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700788 }
789
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700790 private void createWindow(final boolean dialog, final Message msg, final String url,
791 final boolean opener_suppressed) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700792 WebView.WebViewTransport transport =
793 (WebView.WebViewTransport) msg.obj;
794 if (dialog) {
795 createSubWindow();
Michael Kolb8233fac2010-10-26 16:08:53 -0700796 mWebViewController.attachSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700797 transport.setWebView(mSubView);
798 } else {
Pankaj Garg21dad562015-07-02 17:17:24 -0700799 capture();
800
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700801 final Tab newTab = mWebViewController.openTab(url,
John Reck5949c662011-05-27 09:52:29 -0700802 Tab.this, true, true);
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700803 // This is special case for rendering links on a webpage in
804 // a new tab. If opener is suppressed, the WebContents created
805 // by the content layer are not fully initialized. This check
806 // will prevent content layer from overriding WebContents
807 // created by new tab with the uninitialized instance.
808 if (!opener_suppressed) {
809 transport.setWebView(newTab.getWebView());
810 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700811 }
812 msg.sendToTarget();
813 }
814
815 @Override
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700816 public void toggleFullscreenModeForTab(boolean enterFullscreen) {
817 if (mWebViewController instanceof Controller) {
Vivek Sekharf96064b2014-07-28 16:32:34 -0700818 setTabFullscreen(enterFullscreen);
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700819 }
820 }
821
822 @Override
Tarun Nainani8eb00912014-07-17 12:28:32 -0700823 public void onOffsetsForFullscreenChanged(float topControlsOffsetYPix,
824 float contentOffsetYPix,
825 float overdrawBottomHeightPix) {
826 if (mWebViewController instanceof Controller) {
Vivek Sekhar8ee3abb2014-07-14 12:32:05 -0700827 Controller controller = (Controller)mWebViewController;
Tarun Nainani8eb00912014-07-17 12:28:32 -0700828 controller.getUi().translateTitleBar(topControlsOffsetYPix);
Vivek Sekhar11b40062015-06-24 11:49:04 -0700829 // Resize the viewport if top controls is not visible
Vivek Sekhar2ee19a32015-07-02 17:03:57 -0700830 if (mMainView != null &&
831 (topControlsOffsetYPix == 0.0f || contentOffsetYPix == 0.0f))
Vivek Sekhar11b40062015-06-24 11:49:04 -0700832 ((BrowserWebView)mMainView).enableTopControls(
833 (topControlsOffsetYPix == 0.0f) ? true : false);
Vivek Sekhar8ee3abb2014-07-14 12:32:05 -0700834 }
835 }
836
837 @Override
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700838 public boolean isTabFullScreen() {
Vivek Sekharf96064b2014-07-28 16:32:34 -0700839 return mFullScreen;
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700840 }
841
842 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700843 public boolean onCreateWindow(WebView view, final boolean dialog,
844 final boolean userGesture, final Message resultMsg) {
845 // only allow new window or sub window for the foreground case
846 if (!mInForeground) {
847 return false;
848 }
849 // Short-circuit if we can't create any more tabs or sub windows.
850 if (dialog && mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -0700851 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700852 .setTitle(R.string.too_many_subwindows_dialog_title)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200853 .setIconAttribute(android.R.attr.alertDialogIcon)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700854 .setMessage(R.string.too_many_subwindows_dialog_message)
855 .setPositiveButton(R.string.ok, null)
856 .show();
857 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700858 } else if (!mWebViewController.getTabControl().canCreateNewTab()) {
Michael Kolb14612442011-06-24 13:06:29 -0700859 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700860 .setTitle(R.string.too_many_windows_dialog_title)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200861 .setIconAttribute(android.R.attr.alertDialogIcon)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700862 .setMessage(R.string.too_many_windows_dialog_message)
863 .setPositiveButton(R.string.ok, null)
864 .show();
865 return false;
866 }
867
868 // Short-circuit if this was a user gesture.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800869 if (userGesture || !mSettings.blockPopupWindows()) {
Tarun Nainani4f5137d2015-04-16 17:26:18 -0700870 WebView.WebViewTransport transport =
871 (WebView.WebViewTransport) resultMsg.obj;
872 CreateWindowParams windowParams = transport.getCreateWindowParams();
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700873 if (windowParams.mOpenerSuppressed) {
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700874 createWindow(dialog, resultMsg, windowParams.mURL, true);
875 // This is special case for rendering links on a webpage in
876 // a new tab. If opener is suppressed, the WebContents created
877 // by the content layer are not fully initialized. Returning false
878 // will prevent content layer from overriding WebContents
879 // created by new tab with the uninitialized instance.
880 return false;
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700881 }
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700882
883 createWindow(dialog, resultMsg);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700884 return true;
885 }
886
Tarun Nainani4f5137d2015-04-16 17:26:18 -0700887 createWindow(dialog, resultMsg);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700888 return true;
889 }
890
891 @Override
Patrick Scotteb5061b2009-11-18 15:00:30 -0500892 public void onRequestFocus(WebView view) {
893 if (!mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700894 mWebViewController.switchToTab(Tab.this);
Patrick Scotteb5061b2009-11-18 15:00:30 -0500895 }
896 }
897
898 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700899 public void onCloseWindow(WebView window) {
Michael Kolbc831b632011-05-11 09:30:34 -0700900 if (mParent != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700901 // JavaScript can only close popup window.
902 if (mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700903 mWebViewController.switchToTab(mParent);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700904 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700905 mWebViewController.closeTab(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700906 }
907 }
908
909 @Override
910 public void onProgressChanged(WebView view, int newProgress) {
John Reck30c714c2010-12-16 17:30:34 -0800911 mPageLoadProgress = newProgress;
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800912 if (newProgress == 100) {
913 mInPageLoad = false;
914 }
John Reck30c714c2010-12-16 17:30:34 -0800915 mWebViewController.onProgressChanged(Tab.this);
Michael Kolb72864272012-05-03 15:42:15 -0700916 if (mUpdateThumbnail && newProgress == 100) {
917 mUpdateThumbnail = false;
918 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700919 }
920
921 @Override
Leon Scroggins21d9b902010-03-11 09:33:11 -0500922 public void onReceivedTitle(WebView view, final String title) {
John Reck30c714c2010-12-16 17:30:34 -0800923 mCurrentState.mTitle = title;
Michael Kolb8233fac2010-10-26 16:08:53 -0700924 mWebViewController.onReceivedTitle(Tab.this, title);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700925 }
926
927 @Override
928 public void onReceivedIcon(WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -0800929 mCurrentState.mFavicon = icon;
Michael Kolb8233fac2010-10-26 16:08:53 -0700930 mWebViewController.onFavicon(Tab.this, view, icon);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700931 }
932
933 @Override
934 public void onReceivedTouchIconUrl(WebView view, String url,
935 boolean precomposed) {
Michael Kolb14612442011-06-24 13:06:29 -0700936 final ContentResolver cr = mContext.getContentResolver();
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400937 // Let precomposed icons take precedence over non-composed
938 // icons.
939 if (precomposed && mTouchIconLoader != null) {
940 mTouchIconLoader.cancel(false);
941 mTouchIconLoader = null;
942 }
943 // Have only one async task at a time.
944 if (mTouchIconLoader == null) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700945 mTouchIconLoader = new DownloadTouchIcon(Tab.this,
Michael Kolb14612442011-06-24 13:06:29 -0700946 mContext, cr, view);
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400947 mTouchIconLoader.execute(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700948 }
Pankaj Garg21dad562015-07-02 17:17:24 -0700949 mTouchIconUrl = url;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700950 }
951
952 @Override
953 public void onShowCustomView(View view,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800954 CustomViewCallback callback) {
Michael Kolb14612442011-06-24 13:06:29 -0700955 Activity activity = mWebViewController.getActivity();
956 if (activity != null) {
957 onShowCustomView(view, activity.getRequestedOrientation(), callback);
958 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400959 }
960
961 @Override
962 public void onShowCustomView(View view, int requestedOrientation,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800963 CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700964 if (mInForeground) mWebViewController.showCustomView(Tab.this, view,
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400965 requestedOrientation, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700966 }
967
968 @Override
969 public void onHideCustomView() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700970 if (mInForeground) mWebViewController.hideCustomView();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700971 }
972
973 /**
974 * The origin has exceeded its database quota.
975 * @param url the URL that exceeded the quota
976 * @param databaseIdentifier the identifier of the database on which the
977 * transaction that caused the quota overflow was run
978 * @param currentQuota the current quota for the origin.
979 * @param estimatedSize the estimated size of the database.
980 * @param totalUsedQuota is the sum of all origins' quota.
981 * @param quotaUpdater The callback to run when a decision to allow or
982 * deny quota has been made. Don't forget to call this!
983 */
984 @Override
985 public void onExceededDatabaseQuota(String url,
986 String databaseIdentifier, long currentQuota, long estimatedSize,
987 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -0700988 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -0700989 .onExceededDatabaseQuota(url, databaseIdentifier,
990 currentQuota, estimatedSize, totalUsedQuota,
991 quotaUpdater);
992 }
993
994 /**
995 * The Application Cache has exceeded its max size.
996 * @param spaceNeeded is the amount of disk space that would be needed
997 * in order for the last appcache operation to succeed.
998 * @param totalUsedQuota is the sum of all origins' quota.
999 * @param quotaUpdater A callback to inform the WebCore thread that a
1000 * new app cache size is available. This callback must always
1001 * be executed at some point to ensure that the sleeping
1002 * WebCore thread is woken up.
1003 */
1004 @Override
1005 public void onReachedMaxAppCacheSize(long spaceNeeded,
1006 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -07001007 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001008 .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota,
1009 quotaUpdater);
1010 }
1011
Ben Murdoch65acc352009-11-19 18:16:04 +00001012 /* Adds a JavaScript error message to the system log and if the JS
1013 * console is enabled in the about:debug options, to that console
1014 * also.
Ben Murdochc42addf2010-01-28 15:19:59 +00001015 * @param consoleMessage the message object.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001016 */
1017 @Override
Ben Murdochc42addf2010-01-28 15:19:59 +00001018 public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Jeff Hamilton47654f42010-09-07 09:57:51 -05001019 // Don't log console messages in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001020 if (isPrivateBrowsingEnabled()) return true;
Jeff Hamilton47654f42010-09-07 09:57:51 -05001021
Ben Murdochc42addf2010-01-28 15:19:59 +00001022 String message = "Console: " + consoleMessage.message() + " "
1023 + consoleMessage.sourceId() + ":"
1024 + consoleMessage.lineNumber();
1025
1026 switch (consoleMessage.messageLevel()) {
1027 case TIP:
1028 Log.v(CONSOLE_LOGTAG, message);
1029 break;
1030 case LOG:
1031 Log.i(CONSOLE_LOGTAG, message);
1032 break;
1033 case WARNING:
1034 Log.w(CONSOLE_LOGTAG, message);
1035 break;
1036 case ERROR:
1037 Log.e(CONSOLE_LOGTAG, message);
1038 break;
1039 case DEBUG:
1040 Log.d(CONSOLE_LOGTAG, message);
1041 break;
1042 }
1043
1044 return true;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001045 }
1046
1047 /**
1048 * Ask the browser for an icon to represent a <video> element.
1049 * This icon will be used if the Web page did not specify a poster attribute.
1050 * @return Bitmap The icon or null if no such icon is available.
1051 */
1052 @Override
1053 public Bitmap getDefaultVideoPoster() {
1054 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001055 return mWebViewController.getDefaultVideoPoster();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001056 }
1057 return null;
1058 }
1059
1060 /**
1061 * Ask the host application for a custom progress view to show while
1062 * a <video> is loading.
1063 * @return View The progress view.
1064 */
1065 @Override
1066 public View getVideoLoadingProgressView() {
1067 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001068 return mWebViewController.getVideoLoadingProgressView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001069 }
1070 return null;
1071 }
1072
1073 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00001074 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001075 if (mInForeground) {
Ben Murdoch8cad4132012-01-11 10:56:43 +00001076 mWebViewController.openFileChooser(uploadMsg, acceptType, capture);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001077 } else {
1078 uploadMsg.onReceiveValue(null);
1079 }
1080 }
1081
Vivek Sekharb54614f2014-05-01 19:03:37 -07001082 @Override
1083 public void showFileChooser(ValueCallback<String[]> uploadFilePaths, String acceptTypes,
1084 boolean capture) {
1085 if (mInForeground) {
1086 mWebViewController.showFileChooser(uploadFilePaths, acceptTypes, capture);
1087 } else {
1088 uploadFilePaths.onReceiveValue(null);
1089 }
1090 }
1091
Grace Kloba22ac16e2009-10-07 18:00:23 -07001092 /**
1093 * Deliver a list of already-visited URLs
1094 */
1095 @Override
1096 public void getVisitedHistory(final ValueCallback<String[]> callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001097 mWebViewController.getVisitedHistory(callback);
1098 }
Ben Murdoch8029a772010-11-16 11:58:21 +00001099
1100 @Override
1101 public void setupAutoFill(Message message) {
1102 // Prompt the user to set up their profile.
1103 final Message msg = message;
Michael Kolb14612442011-06-24 13:06:29 -07001104 AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
1105 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
Ben Murdoch1d676b62011-01-17 12:54:24 +00001106 Context.LAYOUT_INFLATER_SERVICE);
1107 final View layout = inflater.inflate(R.layout.setup_autofill_dialog, null);
1108
1109 builder.setView(layout)
1110 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1111 @Override
1112 public void onClick(DialogInterface dialog, int id) {
1113 CheckBox disableAutoFill = (CheckBox) layout.findViewById(
1114 R.id.setup_autofill_dialog_disable_autofill);
1115
1116 if (disableAutoFill.isChecked()) {
1117 // Disable autofill and show a toast with how to turn it on again.
John Reck35e9dd62011-04-25 09:01:54 -07001118 mSettings.setAutofillEnabled(false);
Michael Kolb14612442011-06-24 13:06:29 -07001119 Toast.makeText(mContext,
Ben Murdoch1d676b62011-01-17 12:54:24 +00001120 R.string.autofill_setup_dialog_negative_toast,
1121 Toast.LENGTH_LONG).show();
1122 } else {
1123 // Take user to the AutoFill profile editor. When they return,
1124 // we will send the message that we pass here which will trigger
1125 // the form to get filled out with their new profile.
1126 mWebViewController.setupAutoFill(msg);
1127 }
1128 }
1129 })
1130 .setNegativeButton(R.string.cancel, null)
1131 .show();
Ben Murdoch8029a772010-11-16 11:58:21 +00001132 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001133 };
1134
1135 // -------------------------------------------------------------------------
1136 // WebViewClient implementation for the sub window
1137 // -------------------------------------------------------------------------
1138
1139 // Subclass of WebViewClient used in subwindows to notify the main
1140 // WebViewClient of certain WebView activities.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001141 private static class SubWindowClient extends WebViewClient {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001142 // The main WebViewClient.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001143 private final WebViewClient mClient;
Michael Kolb8233fac2010-10-26 16:08:53 -07001144 private final WebViewController mController;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001145
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001146 SubWindowClient(WebViewClient client, WebViewController controller) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001147 mClient = client;
Michael Kolb8233fac2010-10-26 16:08:53 -07001148 mController = controller;
Leon Scroggins III211ba542010-04-19 13:21:13 -04001149 }
1150 @Override
1151 public void onPageStarted(WebView view, String url, Bitmap favicon) {
1152 // Unlike the others, do not call mClient's version, which would
1153 // change the progress bar. However, we do want to remove the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001154 // find or select dialog.
Michael Kolb8233fac2010-10-26 16:08:53 -07001155 mController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001156 }
1157 @Override
1158 public void doUpdateVisitedHistory(WebView view, String url,
1159 boolean isReload) {
1160 mClient.doUpdateVisitedHistory(view, url, isReload);
1161 }
1162 @Override
1163 public boolean shouldOverrideUrlLoading(WebView view, String url) {
1164 return mClient.shouldOverrideUrlLoading(view, url);
1165 }
1166 @Override
1167 public void onReceivedSslError(WebView view, SslErrorHandler handler,
1168 SslError error) {
1169 mClient.onReceivedSslError(view, handler, error);
1170 }
1171 @Override
Brian Carlstrom8862c1d2011-06-02 01:05:55 -07001172 public void onReceivedClientCertRequest(WebView view,
1173 ClientCertRequestHandler handler, String host_and_port) {
1174 mClient.onReceivedClientCertRequest(view, handler, host_and_port);
1175 }
1176 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -07001177 public void onReceivedHttpAuthRequest(WebView view,
1178 HttpAuthHandler handler, String host, String realm) {
1179 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
1180 }
1181 @Override
1182 public void onFormResubmission(WebView view, Message dontResend,
1183 Message resend) {
1184 mClient.onFormResubmission(view, dontResend, resend);
1185 }
1186 @Override
1187 public void onReceivedError(WebView view, int errorCode,
1188 String description, String failingUrl) {
1189 mClient.onReceivedError(view, errorCode, description, failingUrl);
1190 }
1191 @Override
1192 public boolean shouldOverrideKeyEvent(WebView view,
1193 android.view.KeyEvent event) {
1194 return mClient.shouldOverrideKeyEvent(view, event);
1195 }
1196 @Override
1197 public void onUnhandledKeyEvent(WebView view,
1198 android.view.KeyEvent event) {
1199 mClient.onUnhandledKeyEvent(view, event);
1200 }
1201 }
1202
1203 // -------------------------------------------------------------------------
1204 // WebChromeClient implementation for the sub window
1205 // -------------------------------------------------------------------------
1206
1207 private class SubWindowChromeClient extends WebChromeClient {
1208 // The main WebChromeClient.
1209 private final WebChromeClient mClient;
1210
1211 SubWindowChromeClient(WebChromeClient client) {
1212 mClient = client;
1213 }
1214 @Override
1215 public void onProgressChanged(WebView view, int newProgress) {
1216 mClient.onProgressChanged(view, newProgress);
1217 }
1218 @Override
1219 public boolean onCreateWindow(WebView view, boolean dialog,
1220 boolean userGesture, android.os.Message resultMsg) {
1221 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
1222 }
1223 @Override
1224 public void onCloseWindow(WebView window) {
1225 if (window != mSubView) {
1226 Log.e(LOGTAG, "Can't close the window");
1227 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001228 mWebViewController.dismissSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001229 }
1230 }
1231
1232 // -------------------------------------------------------------------------
1233
1234 // Construct a new tab
Michael Kolb7bcafde2011-05-09 13:55:59 -07001235 Tab(WebViewController wvcontroller, WebView w) {
John Reck1cf4b792011-07-26 10:22:22 -07001236 this(wvcontroller, w, null);
1237 }
1238
1239 Tab(WebViewController wvcontroller, Bundle state) {
1240 this(wvcontroller, null, state);
1241 }
1242
1243 Tab(WebViewController wvcontroller, WebView w, Bundle state) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001244 mWebViewController = wvcontroller;
Michael Kolb14612442011-06-24 13:06:29 -07001245 mContext = mWebViewController.getContext();
John Reck35e9dd62011-04-25 09:01:54 -07001246 mSettings = BrowserSettings.getInstance();
Michael Kolb14612442011-06-24 13:06:29 -07001247 mDataController = DataController.getInstance(mContext);
1248 mCurrentState = new PageState(mContext, w != null
John Reck847b5322011-04-14 17:02:18 -07001249 ? w.isPrivateBrowsingEnabled() : false);
Tarun Nainani8084c822014-06-25 13:38:06 -07001250 setTimeStamp();
Michael Kolb8233fac2010-10-26 16:08:53 -07001251 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001252 mInForeground = false;
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001253 mWebViewDestroyedByMemoryMonitor = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001254
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001255 mDownloadListener = new BrowserDownloadListener() {
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001256 public void onDownloadStart(String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001257 String contentDisposition, String mimetype, String referer,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001258 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001259 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001260 mimetype, referer, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001261 }
1262 };
1263
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001264 mCaptureWidth = mContext.getResources().getDimensionPixelSize(R.dimen.tab_thumbnail_width);
1265 mCaptureHeight =mContext.getResources().getDimensionPixelSize(R.dimen.tab_thumbnail_height);
1266
1267 initCaptureBitmap();
1268
John Reck1cf4b792011-07-26 10:22:22 -07001269 restoreState(state);
John Reck52be4782011-08-26 15:37:29 -07001270 if (getId() == -1) {
1271 mId = TabControl.getNextId();
1272 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001273 setWebView(w);
Michael Kolb9ef259a2011-07-12 15:33:08 -07001274 mHandler = new Handler() {
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001275 @Override
Michael Kolb9ef259a2011-07-12 15:33:08 -07001276 public void handleMessage(Message m) {
John Reck1cf4b792011-07-26 10:22:22 -07001277 switch (m.what) {
1278 case MSG_CAPTURE:
1279 capture();
1280 break;
1281 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001282 }
1283 };
Pankaj Garg1c13cab2015-05-12 11:52:17 -07001284
1285 mFirstPixelObservable = new Observable();
1286 mFirstPixelObservable.set(false);
1287 mTabHistoryUpdateObservable = new Observable();
John Reck1cf4b792011-07-26 10:22:22 -07001288 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001289
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001290 private void initCaptureBitmap() {
1291 mCapture = Bitmap.createBitmap(mCaptureWidth, mCaptureHeight, Bitmap.Config.RGB_565);
1292 mCapture.eraseColor(Color.WHITE);
Michael Kolb72864272012-05-03 15:42:15 -07001293 }
1294
Mathew Inwoode09305e2011-09-02 12:03:26 +01001295 /**
1296 * This is used to get a new ID when the tab has been preloaded, before it is displayed and
1297 * added to TabControl. Preloaded tabs can be created before restoreInstanceState, leading
1298 * to overlapping IDs between the preloaded and restored tabs.
1299 */
1300 public void refreshIdAfterPreload() {
1301 mId = TabControl.getNextId();
1302 }
1303
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001304 public void setController(WebViewController ctl) {
1305 mWebViewController = ctl;
1306
John Reck1cf4b792011-07-26 10:22:22 -07001307 if (mWebViewController.shouldCaptureThumbnails()) {
1308 synchronized (Tab.this) {
1309 if (mCapture == null) {
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001310 initCaptureBitmap();
1311 if (mInForeground && !mHandler.hasMessages(MSG_CAPTURE)) {
1312 mHandler.sendEmptyMessageDelayed(MSG_CAPTURE, CAPTURE_DELAY);
John Reck1cf4b792011-07-26 10:22:22 -07001313 }
1314 }
1315 }
1316 } else {
1317 synchronized (Tab.this) {
1318 mCapture = null;
1319 deleteThumbnail();
1320 }
1321 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001322 }
1323
Michael Kolbc831b632011-05-11 09:30:34 -07001324 public long getId() {
1325 return mId;
1326 }
1327
Michael Kolb91911a22012-01-17 11:21:25 -08001328 void setWebView(WebView w) {
1329 setWebView(w, true);
1330 }
1331
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001332 public boolean isNativeActive(){
1333 if (mMainView == null)
1334 return false;
1335 return true;
1336 }
1337
1338 public void setTimeStamp(){
1339 Date d = new Date();
1340 timestamp = (new Timestamp(d.getTime()));
1341 }
1342
1343 public Timestamp getTimestamp() {
1344 return timestamp;
1345 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001346 /**
1347 * Sets the WebView for this tab, correctly removing the old WebView from
1348 * the container view.
1349 */
Michael Kolb91911a22012-01-17 11:21:25 -08001350 void setWebView(WebView w, boolean restore) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001351 if (mMainView == w) {
1352 return;
1353 }
Michael Kolba713ec82010-11-29 17:27:06 -08001354
Michael Kolba713ec82010-11-29 17:27:06 -08001355 mWebViewController.onSetWebView(this, w);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001356
John Reck1cf4b792011-07-26 10:22:22 -07001357 if (mMainView != null) {
John Reck8ee633f2011-08-09 16:00:35 -07001358 mMainView.setPictureListener(null);
John Reck1cf4b792011-07-26 10:22:22 -07001359 if (w != null) {
1360 syncCurrentState(w, null);
1361 } else {
Panos Thomasa9a5a582014-03-18 19:20:08 -07001362 mCurrentState = new PageState(mContext, mMainView.isPrivateBrowsingEnabled());
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001363
1364 if (mWebViewDestroyedByMemoryMonitor) {
1365 /*
1366 * If tab was destroyed as a result of the MemoryMonitor
1367 * then we need to restore the state properties
1368 * from the old WebView (mMainView)
1369 */
1370 syncCurrentState(mMainView, null);
1371 mWebViewDestroyedByMemoryMonitor = false;
1372 }
John Reck1cf4b792011-07-26 10:22:22 -07001373 }
1374 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001375 // set the new one
1376 mMainView = w;
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001377
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001378 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001379 if (mMainView != null) {
1380 mMainView.setWebViewClient(mWebViewClient);
1381 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001382 // Attach DownloadManager so that downloads can start in an active
1383 // or a non-active window. This can happen when going to a site that
1384 // does a redirect after a period of time. The user could have
1385 // switched to another tab while waiting for the download to start.
1386 mMainView.setDownloadListener(mDownloadListener);
John Reck8ee633f2011-08-09 16:00:35 -07001387 TabControl tc = mWebViewController.getTabControl();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001388 if (tc != null /*&& tc.getOnThumbnailUpdatedListener() != null*/) {
John Reck8ee633f2011-08-09 16:00:35 -07001389 mMainView.setPictureListener(this);
1390 }
Michael Kolb91911a22012-01-17 11:21:25 -08001391 if (restore && (mSavedState != null)) {
John Reck8b9bb8b2012-03-08 13:19:40 -08001392 restoreUserAgent();
John Reck6c2e2f32011-08-22 13:41:23 -07001393 WebBackForwardList restoredState
1394 = mMainView.restoreState(mSavedState);
1395 if (restoredState == null || restoredState.getSize() == 0) {
1396 Log.w(LOGTAG, "Failed to restore WebView state!");
1397 loadUrl(mCurrentState.mOriginalUrl, null);
1398 }
John Reck1cf4b792011-07-26 10:22:22 -07001399 mSavedState = null;
1400 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001401 }
1402 }
1403
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001404 public void destroyThroughMemoryMonitor() {
1405 mWebViewDestroyedByMemoryMonitor = true;
1406 destroy();
1407 }
1408
Grace Kloba22ac16e2009-10-07 18:00:23 -07001409 /**
1410 * Destroy the tab's main WebView and subWindow if any
1411 */
1412 void destroy() {
Tarun Nainani2c1dd7c2014-07-05 16:40:12 -07001413
Matthew Huib7f2e9c2014-04-16 11:12:37 -04001414 if (mPostponeDestroy) {
1415 mShouldDestroy = true;
1416 return;
1417 }
1418 mShouldDestroy = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001419 if (mMainView != null) {
1420 dismissSubWindow();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001421 // save the WebView to call destroy() after detach it from the tab
Vivek Sekhar53ef8932015-06-18 16:51:43 -07001422 final WebView webView = mMainView;
Vivek Sekhar2ee19a32015-07-02 17:03:57 -07001423 setWebView(null);
Site Mao61b68212015-07-16 10:56:31 -07001424 if (!mWebViewDestroyedByMemoryMonitor && !BaseUi.isUiLowPowerMode()) {
Vivek Sekhar2ee19a32015-07-02 17:03:57 -07001425 // Tabs can be reused with new instance of WebView so delete the snapshots
Vivek Sekhar53ef8932015-06-18 16:51:43 -07001426 webView.getSnapshotIds(new ValueCallback<List<Integer>>() {
1427 @Override
1428 public void onReceiveValue(List<Integer> ids) {
1429 int currentTabIdx = mWebViewController.getTabControl().getCurrentPosition();
1430 for (Integer id : ids) {
1431 if (getTabIdxFromCaptureIdx(id) == currentTabIdx) {
1432 webView.deleteSnapshot(id);
1433 }
1434 }
Vivek Sekhar53ef8932015-06-18 16:51:43 -07001435 webView.destroy();
Pankaj Garg1c13cab2015-05-12 11:52:17 -07001436 }
Vivek Sekhar53ef8932015-06-18 16:51:43 -07001437 });
Vivek Sekhar2ee19a32015-07-02 17:03:57 -07001438 } else {
1439 webView.destroy();
Pankaj Garg1c13cab2015-05-12 11:52:17 -07001440 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001441 }
1442 }
1443
Matthew Huib7f2e9c2014-04-16 11:12:37 -04001444 private boolean mPostponeDestroy = false;
1445 private boolean mShouldDestroy = false;
1446
1447 public void postponeDestroy() {
1448 mPostponeDestroy = true;
1449 }
1450
1451 public void performPostponedDestroy() {
1452 mPostponeDestroy = false;
1453 if (mShouldDestroy) {
1454 destroy();
1455 }
1456 }
1457
Grace Kloba22ac16e2009-10-07 18:00:23 -07001458 /**
1459 * Remove the tab from the parent
1460 */
1461 void removeFromTree() {
1462 // detach the children
Michael Kolbc831b632011-05-11 09:30:34 -07001463 if (mChildren != null) {
1464 for(Tab t : mChildren) {
1465 t.setParent(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001466 }
1467 }
1468 // remove itself from the parent list
Michael Kolbc831b632011-05-11 09:30:34 -07001469 if (mParent != null) {
1470 mParent.mChildren.remove(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001471 }
Tarun Nainani8eb00912014-07-17 12:28:32 -07001472
1473 mCapture = null;
John Reck1cf4b792011-07-26 10:22:22 -07001474 deleteThumbnail();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001475 }
1476
1477 /**
1478 * Create a new subwindow unless a subwindow already exists.
1479 * @return True if a new subwindow was created. False if one already exists.
1480 */
1481 boolean createSubWindow() {
1482 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001483 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001484 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001485 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001486 mSubView.setWebChromeClient(new SubWindowChromeClient(
1487 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001488 // Set a different DownloadListener for the mSubView, since it will
1489 // just need to dismiss the mSubView, rather than close the Tab
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001490 mSubView.setDownloadListener(new BrowserDownloadListener() {
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001491 public void onDownloadStart(String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001492 String contentDisposition, String mimetype, String referer,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001493 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001494 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001495 contentDisposition, mimetype, referer, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001496 if (mSubView.copyBackForwardList().getSize() == 0) {
1497 // This subwindow was opened for the sole purpose of
1498 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001499 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001500 }
1501 }
1502 });
Michael Kolb14612442011-06-24 13:06:29 -07001503 mSubView.setOnCreateContextMenuListener(mWebViewController.getActivity());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001504 return true;
1505 }
1506 return false;
1507 }
1508
1509 /**
1510 * Dismiss the subWindow for the tab.
1511 */
1512 void dismissSubWindow() {
1513 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001514 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001515 mSubView.destroy();
1516 mSubView = null;
1517 mSubViewContainer = null;
1518 }
1519 }
1520
Grace Kloba22ac16e2009-10-07 18:00:23 -07001521
1522 /**
1523 * Set the parent tab of this tab.
1524 */
Michael Kolbc831b632011-05-11 09:30:34 -07001525 void setParent(Tab parent) {
John Reck52be4782011-08-26 15:37:29 -07001526 if (parent == this) {
1527 throw new IllegalStateException("Cannot set parent to self!");
1528 }
Michael Kolbc831b632011-05-11 09:30:34 -07001529 mParent = parent;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001530 // This tab may have been freed due to low memory. If that is the case,
Michael Kolbc831b632011-05-11 09:30:34 -07001531 // the parent tab id is already saved. If we are changing that id
Grace Kloba22ac16e2009-10-07 18:00:23 -07001532 // (most likely due to removing the parent tab) we must update the
Michael Kolbc831b632011-05-11 09:30:34 -07001533 // parent tab id in the saved Bundle.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001534 if (mSavedState != null) {
1535 if (parent == null) {
1536 mSavedState.remove(PARENTTAB);
1537 } else {
Michael Kolbc831b632011-05-11 09:30:34 -07001538 mSavedState.putLong(PARENTTAB, parent.getId());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001539 }
1540 }
John Reckb0a86db2011-05-24 14:05:58 -07001541
1542 // Sync the WebView useragent with the parent
1543 if (parent != null && mSettings.hasDesktopUseragent(parent.getWebView())
1544 != mSettings.hasDesktopUseragent(getWebView())) {
1545 mSettings.toggleDesktopUseragent(getWebView());
1546 }
John Reck52be4782011-08-26 15:37:29 -07001547
1548 if (parent != null && parent.getId() == getId()) {
1549 throw new IllegalStateException("Parent has same ID as child!");
1550 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001551 }
1552
1553 /**
Michael Kolbc831b632011-05-11 09:30:34 -07001554 * If this Tab was created through another Tab, then this method returns
1555 * that Tab.
1556 * @return the Tab parent or null
1557 */
1558 public Tab getParent() {
1559 return mParent;
1560 }
1561
1562 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001563 * When a Tab is created through the content of another Tab, then we
1564 * associate the Tabs.
1565 * @param child the Tab that was created from this Tab
1566 */
1567 void addChildTab(Tab child) {
Michael Kolbc831b632011-05-11 09:30:34 -07001568 if (mChildren == null) {
1569 mChildren = new Vector<Tab>();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001570 }
Michael Kolbc831b632011-05-11 09:30:34 -07001571 mChildren.add(child);
1572 child.setParent(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001573 }
1574
Michael Kolbc831b632011-05-11 09:30:34 -07001575 Vector<Tab> getChildren() {
1576 return mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001577 }
1578
1579 void resume() {
1580 if (mMainView != null) {
John Reck56c1fcf2011-08-17 10:15:16 -07001581 setupHwAcceleration(mMainView);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001582 mMainView.onResume();
1583 if (mSubView != null) {
1584 mSubView.onResume();
1585 }
1586 }
1587 }
1588
John Reck56c1fcf2011-08-17 10:15:16 -07001589 private void setupHwAcceleration(View web) {
1590 if (web == null) return;
1591 BrowserSettings settings = BrowserSettings.getInstance();
1592 if (settings.isHardwareAccelerated()) {
1593 web.setLayerType(View.LAYER_TYPE_NONE, null);
1594 } else {
1595 web.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
1596 }
1597 }
1598
Grace Kloba22ac16e2009-10-07 18:00:23 -07001599 void pause() {
1600 if (mMainView != null) {
1601 mMainView.onPause();
1602 if (mSubView != null) {
1603 mSubView.onPause();
1604 }
1605 }
1606 }
1607
1608 void putInForeground() {
John Reck8ee633f2011-08-09 16:00:35 -07001609 if (mInForeground) {
1610 return;
1611 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001612 mInForeground = true;
1613 resume();
Michael Kolb14612442011-06-24 13:06:29 -07001614 Activity activity = mWebViewController.getActivity();
1615 mMainView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001616 if (mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -07001617 mSubView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001618 }
Axesh R. Ajmerac6b5c322015-05-01 11:06:10 -07001619
Leon Scroggins1961ed22010-12-07 15:22:21 -05001620 mWebViewController.bookmarkedStatusHasChanged(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001621 }
1622
1623 void putInBackground() {
John Reck8ee633f2011-08-09 16:00:35 -07001624 if (!mInForeground) {
1625 return;
1626 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001627 mInForeground = false;
1628 pause();
1629 mMainView.setOnCreateContextMenuListener(null);
1630 if (mSubView != null) {
1631 mSubView.setOnCreateContextMenuListener(null);
1632 }
1633 }
1634
Michael Kolb8233fac2010-10-26 16:08:53 -07001635 boolean inForeground() {
1636 return mInForeground;
1637 }
1638
Grace Kloba22ac16e2009-10-07 18:00:23 -07001639 /**
1640 * Return the top window of this tab; either the subwindow if it is not
1641 * null or the main window.
1642 * @return The top window of this tab.
1643 */
1644 WebView getTopWindow() {
1645 if (mSubView != null) {
1646 return mSubView;
1647 }
1648 return mMainView;
1649 }
1650
1651 /**
1652 * Return the main window of this tab. Note: if a tab is freed in the
1653 * background, this can return null. It is only guaranteed to be
1654 * non-null for the current tab.
1655 * @return The main WebView of this tab.
1656 */
1657 WebView getWebView() {
1658 return mMainView;
1659 }
1660
Michael Kolba713ec82010-11-29 17:27:06 -08001661 void setViewContainer(View container) {
1662 mContainer = container;
1663 }
1664
Michael Kolb8233fac2010-10-26 16:08:53 -07001665 View getViewContainer() {
1666 return mContainer;
1667 }
1668
Grace Kloba22ac16e2009-10-07 18:00:23 -07001669 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001670 * Return whether private browsing is enabled for the main window of
1671 * this tab.
1672 * @return True if private browsing is enabled.
1673 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001674 boolean isPrivateBrowsingEnabled() {
John Reck502a3532011-08-16 14:21:46 -07001675 return mCurrentState.mIncognito;
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001676 }
1677
1678 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001679 * Return the subwindow of this tab or null if there is no subwindow.
1680 * @return The subwindow of this tab or null.
1681 */
1682 WebView getSubWebView() {
1683 return mSubView;
1684 }
1685
Michael Kolb1514bb72010-11-22 09:11:48 -08001686 void setSubWebView(WebView subView) {
1687 mSubView = subView;
1688 }
1689
Michael Kolb8233fac2010-10-26 16:08:53 -07001690 View getSubViewContainer() {
1691 return mSubViewContainer;
1692 }
1693
Michael Kolb1514bb72010-11-22 09:11:48 -08001694 void setSubViewContainer(View subViewContainer) {
1695 mSubViewContainer = subViewContainer;
1696 }
1697
Grace Kloba22ac16e2009-10-07 18:00:23 -07001698
1699 /**
1700 * @return The application id string
1701 */
1702 String getAppId() {
1703 return mAppId;
1704 }
1705
1706 /**
1707 * Set the application id string
1708 * @param id
1709 */
1710 void setAppId(String id) {
1711 mAppId = id;
1712 }
1713
Michael Kolbe28b3472011-08-04 16:54:31 -07001714 boolean closeOnBack() {
1715 return mCloseOnBack;
1716 }
1717
1718 void setCloseOnBack(boolean close) {
1719 mCloseOnBack = close;
1720 }
1721
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001722 boolean getDerivedFromIntent() {
1723 return mDerivedFromIntent;
1724 }
1725
1726 void setDerivedFromIntent(boolean derived) {
1727 mDerivedFromIntent = derived;
1728 }
1729
Grace Kloba22ac16e2009-10-07 18:00:23 -07001730 String getUrl() {
John Reck324d4402011-01-11 16:56:42 -08001731 return UrlUtils.filteredUrl(mCurrentState.mUrl);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001732 }
1733
Tarun Nainani8eb00912014-07-17 12:28:32 -07001734
1735 protected void onPageFinished() {
1736 mPageFinished = true;
Ze G Riande2a675c22015-06-03 11:15:24 -07001737 isDistillable();
Tarun Nainani8eb00912014-07-17 12:28:32 -07001738 }
1739
1740 public boolean getPageFinishedStatus() {
1741 return mPageFinished;
1742 }
1743
John Reck49a603c2011-03-03 09:33:05 -08001744 String getOriginalUrl() {
John Reckdb22ec42011-06-29 11:31:24 -07001745 if (mCurrentState.mOriginalUrl == null) {
1746 return getUrl();
John Reck49a603c2011-03-03 09:33:05 -08001747 }
John Reckdb22ec42011-06-29 11:31:24 -07001748 return UrlUtils.filteredUrl(mCurrentState.mOriginalUrl);
John Reck49a603c2011-03-03 09:33:05 -08001749 }
1750
Grace Kloba22ac16e2009-10-07 18:00:23 -07001751 /**
John Reck30c714c2010-12-16 17:30:34 -08001752 * Get the title of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001753 */
1754 String getTitle() {
John Reck30c714c2010-12-16 17:30:34 -08001755 return mCurrentState.mTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001756 }
1757
1758 /**
John Reck30c714c2010-12-16 17:30:34 -08001759 * Get the favicon of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001760 */
1761 Bitmap getFavicon() {
John Reck1cf4b792011-07-26 10:22:22 -07001762 if (mCurrentState.mFavicon != null) {
1763 return mCurrentState.mFavicon;
1764 }
1765 return getDefaultFavicon(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001766 }
1767
Pankaj Garg32e1b942015-06-03 18:13:24 -07001768 public boolean hasFavicon() {
1769 return mCurrentState.mFavicon != null;
1770 }
1771
John Recke969cc52010-12-21 17:24:43 -08001772 public boolean isBookmarkedSite() {
1773 return mCurrentState.mIsBookmarkedSite;
1774 }
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001775
Grace Kloba22ac16e2009-10-07 18:00:23 -07001776 /**
Steve Block08a6f0c2011-10-06 12:12:53 +01001777 * Sets the security state, clears the SSL certificate error and informs
1778 * the controller.
1779 */
Steve Block2466eff2011-10-03 15:33:09 +01001780 private void setSecurityState(SecurityState securityState) {
1781 mCurrentState.mSecurityState = securityState;
Steve Block08a6f0c2011-10-06 12:12:53 +01001782 mCurrentState.mSslCertificateError = null;
Steve Block2466eff2011-10-03 15:33:09 +01001783 mWebViewController.onUpdatedSecurityState(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001784 }
1785
1786 /**
Steve Block2466eff2011-10-03 15:33:09 +01001787 * @return The tab's security state.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001788 */
Steve Block2466eff2011-10-03 15:33:09 +01001789 SecurityState getSecurityState() {
1790 return mCurrentState.mSecurityState;
John Reck30c714c2010-12-16 17:30:34 -08001791 }
1792
Steve Block08a6f0c2011-10-06 12:12:53 +01001793 /**
1794 * Gets the SSL certificate error, if any, for the page's main resource.
1795 * This is only non-null when the security state is
1796 * SECURITY_STATE_BAD_CERTIFICATE.
1797 */
1798 SslError getSslCertificateError() {
1799 return mCurrentState.mSslCertificateError;
1800 }
1801
John Reck30c714c2010-12-16 17:30:34 -08001802 int getLoadProgress() {
1803 if (mInPageLoad) {
1804 return mPageLoadProgress;
1805 }
1806 return 100;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001807 }
1808
1809 /**
1810 * @return TRUE if onPageStarted is called while onPageFinished is not
1811 * called yet.
1812 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001813 boolean inPageLoad() {
1814 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001815 }
1816
Grace Kloba22ac16e2009-10-07 18:00:23 -07001817 /**
John Reck1cf4b792011-07-26 10:22:22 -07001818 * @return The Bundle with the tab's state if it can be saved, otherwise null
Grace Kloba22ac16e2009-10-07 18:00:23 -07001819 */
John Reck1cf4b792011-07-26 10:22:22 -07001820 public Bundle saveState() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001821 // If the WebView is null it means we ran low on memory and we already
1822 // stored the saved state in mSavedState.
1823 if (mMainView == null) {
John Reck1cf4b792011-07-26 10:22:22 -07001824 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001825 }
John Reck6c2e2f32011-08-22 13:41:23 -07001826
1827 if (TextUtils.isEmpty(mCurrentState.mUrl)) {
John Reck1cf4b792011-07-26 10:22:22 -07001828 return null;
John Reck24f18262011-06-17 14:47:20 -07001829 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001830
1831 mSavedState = new Bundle();
John Reck6c2e2f32011-08-22 13:41:23 -07001832 WebBackForwardList savedList = mMainView.saveState(mSavedState);
1833 if (savedList == null || savedList.getSize() == 0) {
1834 Log.w(LOGTAG, "Failed to save back/forward list for "
1835 + mCurrentState.mUrl);
1836 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001837
Michael Kolbc831b632011-05-11 09:30:34 -07001838 mSavedState.putLong(ID, mId);
John Reck30c714c2010-12-16 17:30:34 -08001839 mSavedState.putString(CURRURL, mCurrentState.mUrl);
1840 mSavedState.putString(CURRTITLE, mCurrentState.mTitle);
John Reck1cf4b792011-07-26 10:22:22 -07001841 mSavedState.putBoolean(INCOGNITO, mMainView.isPrivateBrowsingEnabled());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001842 if (mAppId != null) {
1843 mSavedState.putString(APPID, mAppId);
1844 }
Michael Kolbe28b3472011-08-04 16:54:31 -07001845 mSavedState.putBoolean(CLOSEFLAG, mCloseOnBack);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001846 // Remember the parent tab so the relationship can be restored.
Michael Kolbc831b632011-05-11 09:30:34 -07001847 if (mParent != null) {
1848 mSavedState.putLong(PARENTTAB, mParent.mId);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001849 }
John Reckb0a86db2011-05-24 14:05:58 -07001850 mSavedState.putBoolean(USERAGENT,
1851 mSettings.hasDesktopUseragent(getWebView()));
John Reck1cf4b792011-07-26 10:22:22 -07001852 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001853 }
1854
1855 /*
1856 * Restore the state of the tab.
1857 */
John Reck1cf4b792011-07-26 10:22:22 -07001858 private void restoreState(Bundle b) {
1859 mSavedState = b;
1860 if (mSavedState == null) {
1861 return;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001862 }
1863 // Restore the internal state even if the WebView fails to restore.
1864 // This will maintain the app id, original url and close-on-exit values.
Michael Kolbc831b632011-05-11 09:30:34 -07001865 mId = b.getLong(ID);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001866 mAppId = b.getString(APPID);
Michael Kolbe28b3472011-08-04 16:54:31 -07001867 mCloseOnBack = b.getBoolean(CLOSEFLAG);
John Reck8b9bb8b2012-03-08 13:19:40 -08001868 restoreUserAgent();
John Reck1cf4b792011-07-26 10:22:22 -07001869 String url = b.getString(CURRURL);
1870 String title = b.getString(CURRTITLE);
1871 boolean incognito = b.getBoolean(INCOGNITO);
1872 mCurrentState = new PageState(mContext, incognito, url, null);
1873 mCurrentState.mTitle = title;
1874 synchronized (Tab.this) {
1875 if (mCapture != null) {
John Reck4eadc342011-10-31 14:04:10 -07001876 DataController.getInstance(mContext).loadThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07001877 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001878 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001879 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04001880
John Reck8b9bb8b2012-03-08 13:19:40 -08001881 private void restoreUserAgent() {
1882 if (mMainView == null || mSavedState == null) {
1883 return;
1884 }
1885 if (mSavedState.getBoolean(USERAGENT)
1886 != mSettings.hasDesktopUseragent(mMainView)) {
1887 mSettings.toggleDesktopUseragent(mMainView);
1888 }
1889 }
1890
Leon Scroggins1961ed22010-12-07 15:22:21 -05001891 public void updateBookmarkedStatus() {
John Recke969cc52010-12-21 17:24:43 -08001892 mDataController.queryBookmarkStatus(getUrl(), mIsBookmarkCallback);
Leon Scroggins1961ed22010-12-07 15:22:21 -05001893 }
1894
John Recke969cc52010-12-21 17:24:43 -08001895 private DataController.OnQueryUrlIsBookmark mIsBookmarkCallback
1896 = new DataController.OnQueryUrlIsBookmark() {
1897 @Override
1898 public void onQueryUrlIsBookmark(String url, boolean isBookmark) {
1899 if (mCurrentState.mUrl.equals(url)) {
1900 mCurrentState.mIsBookmarkedSite = isBookmark;
1901 mWebViewController.bookmarkedStatusHasChanged(Tab.this);
1902 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001903 }
John Recke969cc52010-12-21 17:24:43 -08001904 };
Michael Kolb1acef692011-03-08 14:12:06 -08001905
Michael Kolbeb95db42011-03-03 10:38:40 -08001906 public Bitmap getScreenshot() {
John Reck1cf4b792011-07-26 10:22:22 -07001907 synchronized (Tab.this) {
1908 return mCapture;
1909 }
Michael Kolbeb95db42011-03-03 10:38:40 -08001910 }
1911
John Reck541f55a2011-06-07 16:34:43 -07001912 public boolean isSnapshot() {
John Reck541f55a2011-06-07 16:34:43 -07001913 return false;
1914 }
1915
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001916 private static class SaveCallback implements ValueCallback<String> {
1917 boolean onReceiveValueCalled = false;
1918 private String mPath;
John Reck68234a92012-04-19 15:27:12 -07001919
1920 @Override
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001921 public void onReceiveValue(String path) {
1922 this.onReceiveValueCalled = true;
1923 this.mPath = path;
John Reck68234a92012-04-19 15:27:12 -07001924 synchronized (this) {
1925 notifyAll();
John Reck8cc92352011-07-06 17:41:52 -07001926 }
John Reck541f55a2011-06-07 16:34:43 -07001927 }
John Reck68234a92012-04-19 15:27:12 -07001928
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001929 public String getPath() {
1930 return mPath;
1931 }
John Reck68234a92012-04-19 15:27:12 -07001932 }
1933
1934 /**
1935 * Must be called on the UI thread
1936 */
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001937 public ContentValues createSnapshotValues(Bitmap bm) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001938 WebView web = getWebView();
John Reck68234a92012-04-19 15:27:12 -07001939 if (web == null) return null;
John Reckd8c74522011-06-14 08:45:00 -07001940 ContentValues values = new ContentValues();
1941 values.put(Snapshots.TITLE, mCurrentState.mTitle);
1942 values.put(Snapshots.URL, mCurrentState.mUrl);
John Reck68234a92012-04-19 15:27:12 -07001943 values.put(Snapshots.BACKGROUND, web.getPageBackgroundColor());
John Reck8cc92352011-07-06 17:41:52 -07001944 values.put(Snapshots.DATE_CREATED, System.currentTimeMillis());
1945 values.put(Snapshots.FAVICON, compressBitmap(getFavicon()));
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001946 values.put(Snapshots.THUMBNAIL, compressBitmap(bm));
John Reckd8c74522011-06-14 08:45:00 -07001947 return values;
John Reck541f55a2011-06-07 16:34:43 -07001948 }
1949
John Reck68234a92012-04-19 15:27:12 -07001950 /**
1951 * Probably want to call this on a background thread
1952 */
1953 public boolean saveViewState(ContentValues values) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001954 WebView web = getWebView();
John Reck68234a92012-04-19 15:27:12 -07001955 if (web == null) return false;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001956 String filename = UUID.randomUUID().toString();
John Reck68234a92012-04-19 15:27:12 -07001957 SaveCallback callback = new SaveCallback();
John Reck68234a92012-04-19 15:27:12 -07001958 try {
John Reck68234a92012-04-19 15:27:12 -07001959 synchronized (callback) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001960 web.saveViewState(filename, callback);
1961 callback.wait();
John Reck68234a92012-04-19 15:27:12 -07001962 }
John Reck68234a92012-04-19 15:27:12 -07001963 } catch (Exception e) {
1964 Log.w(LOGTAG, "Failed to save view state", e);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001965 String path = callback.getPath();
1966 if (path != null) {
1967 File file = mContext.getFileStreamPath(path);
1968 if (file.exists() && !file.delete()) {
1969 file.deleteOnExit();
1970 }
John Reck68234a92012-04-19 15:27:12 -07001971 }
1972 return false;
1973 }
Tarun Nainani8eb00912014-07-17 12:28:32 -07001974
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001975 String path = callback.getPath();
Tarun Nainani8eb00912014-07-17 12:28:32 -07001976 // could be that saving of file failed
1977 if (path == null) {
1978 return false;
1979 }
1980
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001981 File savedFile = new File(path);
1982 if (!savedFile.exists()) {
1983 return false;
John Reck68234a92012-04-19 15:27:12 -07001984 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001985 values.put(Snapshots.VIEWSTATE_PATH, path.substring(path.lastIndexOf('/') + 1));
1986 values.put(Snapshots.VIEWSTATE_SIZE, savedFile.length());
John Reck68234a92012-04-19 15:27:12 -07001987 return true;
1988 }
1989
John Reck8cc92352011-07-06 17:41:52 -07001990 public byte[] compressBitmap(Bitmap bitmap) {
1991 if (bitmap == null) {
1992 return null;
1993 }
1994 ByteArrayOutputStream stream = new ByteArrayOutputStream();
1995 bitmap.compress(CompressFormat.PNG, 100, stream);
1996 return stream.toByteArray();
1997 }
1998
John Reck26b18322011-06-21 13:08:58 -07001999 public void loadUrl(String url, Map<String, String> headers) {
2000 if (mMainView != null) {
Michael Kolba53c9892011-10-05 13:31:40 -07002001 mPageLoadProgress = INITIAL_PROGRESS;
Michael Kolb14612442011-06-24 13:06:29 -07002002 mCurrentState = new PageState(mContext, false, url, null);
John Reck26b18322011-06-21 13:08:58 -07002003 mMainView.loadUrl(url, headers);
2004 }
2005 }
2006
John Reck38b39652012-06-05 09:22:59 -07002007 public void disableUrlOverridingForLoad() {
2008 mDisableOverrideUrlLoading = true;
2009 }
2010
Pankaj Garg6bedeba2015-06-23 15:47:37 -07002011 private void thumbnailUpdated() {
John Reck8ee633f2011-08-09 16:00:35 -07002012 mHandler.removeMessages(MSG_CAPTURE);
Pankaj Garg6bedeba2015-06-23 15:47:37 -07002013
John Reck8ee633f2011-08-09 16:00:35 -07002014 TabControl tc = mWebViewController.getTabControl();
2015 if (tc != null) {
Pankaj Garg6bedeba2015-06-23 15:47:37 -07002016 OnThumbnailUpdatedListener updateListener = tc.getOnThumbnailUpdatedListener();
John Reck8ee633f2011-08-09 16:00:35 -07002017 if (updateListener != null) {
2018 updateListener.onThumbnailUpdated(this);
2019 }
2020 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07002021 }
2022
Pankaj Garg6bedeba2015-06-23 15:47:37 -07002023 protected void capture() {
2024 if (mMainView == null || mCapture == null || !mMainView.isReady() ||
2025 mMainView.getContentWidth() <= 0 || mMainView.getContentHeight() <= 0 ||
2026 !mFirstVisualPixelPainted || mMainView.isShowingCrashView()) {
2027
2028 initCaptureBitmap();
2029 thumbnailUpdated();
2030 return;
2031 }
2032
2033 mMainView.getContentBitmapAsync((float) mCaptureWidth / mMainView.getWidth(), new Rect(),
2034 new ValueCallback<Bitmap>() {
2035 @Override
2036 public void onReceiveValue(Bitmap bitmap) {
2037 if (mCapture == null) {
2038 initCaptureBitmap();
2039 }
2040
2041 if (bitmap == null) {
2042 thumbnailUpdated();
2043 return;
2044 }
2045
2046 Canvas c = new Canvas(mCapture);
2047 mCapture.eraseColor(Color.WHITE);
2048 c.drawBitmap(bitmap, 0, 0, null);
2049
2050 // manually anti-alias the edges for the tilt
2051 c.drawRect(0, 0, 1, mCapture.getHeight(), sAlphaPaint);
2052 c.drawRect(mCapture.getWidth() - 1, 0, mCapture.getWidth(),
2053 mCapture.getHeight(), sAlphaPaint);
2054 c.drawRect(0, 0, mCapture.getWidth(), 1, sAlphaPaint);
2055 c.drawRect(0, mCapture.getHeight() - 1, mCapture.getWidth(),
2056 mCapture.getHeight(), sAlphaPaint);
2057 c.setBitmap(null);
2058
2059 persistThumbnail();
2060 thumbnailUpdated();
2061 }
2062 }
2063 );
John Reck1cf4b792011-07-26 10:22:22 -07002064 }
2065
Pankaj Garg6bedeba2015-06-23 15:47:37 -07002066 @Override
2067 public void onNewPicture(WebView view, Picture picture) {
Michael Kolb9ef259a2011-07-12 15:33:08 -07002068 }
2069
John Reckef654f12011-07-12 16:42:08 -07002070 public boolean canGoBack() {
2071 return mMainView != null ? mMainView.canGoBack() : false;
2072 }
2073
2074 public boolean canGoForward() {
2075 return mMainView != null ? mMainView.canGoForward() : false;
2076 }
2077
2078 public void goBack() {
2079 if (mMainView != null) {
2080 mMainView.goBack();
2081 }
2082 }
2083
2084 public void goForward() {
2085 if (mMainView != null) {
2086 mMainView.goForward();
2087 }
2088 }
2089
John Reck1cf4b792011-07-26 10:22:22 -07002090 protected void persistThumbnail() {
John Reck4eadc342011-10-31 14:04:10 -07002091 DataController.getInstance(mContext).saveThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07002092 }
2093
2094 protected void deleteThumbnail() {
John Reck4eadc342011-10-31 14:04:10 -07002095 DataController.getInstance(mContext).deleteThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07002096 }
2097
John Reck4eadc342011-10-31 14:04:10 -07002098 void updateCaptureFromBlob(byte[] blob) {
John Reck1cf4b792011-07-26 10:22:22 -07002099 synchronized (Tab.this) {
2100 if (mCapture == null) {
2101 return;
2102 }
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002103 ByteBuffer buffer = ByteBuffer.wrap(blob);
Michael Kolbd837a112011-08-09 14:00:18 -07002104 try {
2105 mCapture.copyPixelsFromBuffer(buffer);
2106 } catch (RuntimeException rex) {
2107 Log.e(LOGTAG, "Load capture has mismatched sizes; buffer: "
2108 + buffer.capacity() + " blob: " + blob.length
2109 + "capture: " + mCapture.getByteCount());
2110 throw rex;
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002111 }
John Reck1cf4b792011-07-26 10:22:22 -07002112 }
2113 }
2114
John Reck52be4782011-08-26 15:37:29 -07002115 @Override
2116 public String toString() {
2117 StringBuilder builder = new StringBuilder(100);
2118 builder.append(mId);
2119 builder.append(") has parent: ");
2120 if (getParent() != null) {
2121 builder.append("true[");
2122 builder.append(getParent().getId());
2123 builder.append("]");
2124 } else {
2125 builder.append("false");
2126 }
2127 builder.append(", incog: ");
2128 builder.append(isPrivateBrowsingEnabled());
2129 if (!isPrivateBrowsingEnabled()) {
2130 builder.append(", title: ");
2131 builder.append(getTitle());
2132 builder.append(", url: ");
2133 builder.append(getUrl());
2134 }
2135 return builder.toString();
2136 }
2137
Steve Block4895b012011-10-03 16:26:46 +01002138 private void handleProceededAfterSslError(SslError error) {
2139 if (error.getUrl().equals(mCurrentState.mUrl)) {
2140 // The security state should currently be SECURITY_STATE_SECURE.
2141 setSecurityState(SecurityState.SECURITY_STATE_BAD_CERTIFICATE);
Steve Block08a6f0c2011-10-06 12:12:53 +01002142 mCurrentState.mSslCertificateError = error;
Steve Block4895b012011-10-03 16:26:46 +01002143 } else if (getSecurityState() == SecurityState.SECURITY_STATE_SECURE) {
Steve Block08a6f0c2011-10-06 12:12:53 +01002144 // The page's main resource is secure and this error is for a
2145 // sub-resource.
Steve Block4895b012011-10-03 16:26:46 +01002146 setSecurityState(SecurityState.SECURITY_STATE_MIXED);
2147 }
2148 }
Ze G Riande2a675c22015-06-03 11:15:24 -07002149
2150 // dertermines if the tab contains a dislled page
2151 public boolean isDistilled() {
2152 if (!BrowserCommandLine.hasSwitch("reader-mode")) {
2153 return false;
2154 }
2155 try {
2156 return DomDistillerUtils.isUrlDistilled(getUrl());
2157 } catch (Exception e) {
2158 return false;
2159 }
2160 }
2161
2162 //determines if the tab contains a distillable page
2163 public boolean isDistillable() {
2164 if (!BrowserCommandLine.hasSwitch("reader-mode")) {
2165 mIsDistillable = false;
2166 return mIsDistillable;
2167 }
2168 final ValueCallback<String> onIsDistillable = new ValueCallback<String>() {
2169 @Override
2170 public void onReceiveValue(String str) {
2171 mIsDistillable = Boolean.parseBoolean(str);
2172 }
2173 };
2174
2175 if (isDistilled()) {
2176 mIsDistillable = true;
2177 return mIsDistillable;
2178 }
2179
2180 try {
2181 DomDistillerUtils.isWebViewDistillable(getWebView(), onIsDistillable);
2182 } catch (Exception e) {
2183 mIsDistillable = false;
2184 }
2185
2186 return mIsDistillable;
2187 }
2188
2189 // Function that sets the mIsDistillable variable
2190 public void setIsDistillable(boolean value) {
2191 if (!BrowserCommandLine.hasSwitch("reader-mode")) {
2192 mIsDistillable = false;
2193 }
2194 mIsDistillable = value;
2195 }
2196
2197 // Function that returns the distilled url of the current url
2198 public String getDistilledUrl() {
2199 if (getUrl() != null) {
2200 return DomDistillerUtils.getDistilledUrl(getUrl());
2201 }
2202 return new String();
2203 }
2204
2205 // function that returns the non-distilled version of the current url
2206 public String getNonDistilledUrl() {
2207 if (getUrl() != null) {
2208 return DomDistillerUtils.getOriginalUrlFromDistilledUrl(getUrl());
2209 }
2210 return new String();
2211 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002212}