blob: 36aae4c55050a40ce9e23317a5d8f7abf9b13698 [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) {
Vivek Sekhard4de6162015-07-21 15:01:45 -0700710 boolean keyboardWasShowing = isKeyboardShowing();
Sagar Dhawan33551ff2015-07-08 17:24:44 -0700711 mIsKeyboardUp = popup;
Vivek Sekhard4de6162015-07-21 15:01:45 -0700712 Controller controller = (Controller)mWebViewController;
713 BaseUi ui = (BaseUi) controller.getUi();
714 // lock the title bar
715 if (popup)
716 ui.getTitleBar().showTopControls(true);
717 if (keyboardWasShowing && popup)
718 ui.getTitleBar().enableTopControls(true);
Pankaj Garg62bc7912015-04-14 16:08:59 -0700719 if (BrowserSettings.getInstance().useFullscreen()) {
Pankaj Garg62bc7912015-04-14 16:08:59 -0700720 ui.forceDisableFullscreenMode(popup);
721 }
722 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700723 };
724
John Reck1cf4b792011-07-26 10:22:22 -0700725 private void syncCurrentState(WebView view, String url) {
726 // Sync state (in case of stop/timeout)
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800727
728 if (mReceivedError) {
729 mCurrentState.mUrl = url;
730 mCurrentState.mOriginalUrl = url;
731 } else {
732 mCurrentState.mUrl = view.getUrl();
733 mCurrentState.mOriginalUrl = view.getOriginalUrl();
734 mCurrentState.mFavicon = view.getFavicon();
735 }
736
John Reck1cf4b792011-07-26 10:22:22 -0700737 if (mCurrentState.mUrl == null) {
John Reck8bcafc12011-08-29 16:43:02 -0700738 mCurrentState.mUrl = "";
John Reck1cf4b792011-07-26 10:22:22 -0700739 }
John Reck1cf4b792011-07-26 10:22:22 -0700740 mCurrentState.mTitle = view.getTitle();
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800741
742
John Reck1cf4b792011-07-26 10:22:22 -0700743 if (!URLUtil.isHttpsUrl(mCurrentState.mUrl)) {
744 // In case we stop when loading an HTTPS page from an HTTP page
745 // but before a provisional load occurred
Steve Block2466eff2011-10-03 15:33:09 +0100746 mCurrentState.mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
Steve Block08a6f0c2011-10-06 12:12:53 +0100747 mCurrentState.mSslCertificateError = null;
John Reck1cf4b792011-07-26 10:22:22 -0700748 }
John Reck502a3532011-08-16 14:21:46 -0700749 mCurrentState.mIncognito = view.isPrivateBrowsingEnabled();
John Reck1cf4b792011-07-26 10:22:22 -0700750 }
751
Pankaj Garg21dad562015-07-02 17:17:24 -0700752 public String getTouchIconUrl() {
753 return mTouchIconUrl;
754 }
Tarun Nainani8eb00912014-07-17 12:28:32 -0700755
Sagar Dhawan33551ff2015-07-08 17:24:44 -0700756 public boolean isKeyboardShowing() {
757 Controller controller = (Controller)mWebViewController;
758 return (mIsKeyboardUp || controller.getUi().isEditingUrl());
759 }
760
Tarun Nainani8eb00912014-07-17 12:28:32 -0700761 public boolean isTabFullScreen() {
762 return mFullScreen;
763 }
764
Vivek Sekharf96064b2014-07-28 16:32:34 -0700765 protected void setTabFullscreen(boolean fullScreen) {
Tarun Nainani8eb00912014-07-17 12:28:32 -0700766 Controller controller = (Controller)mWebViewController;
Sudheer Koganti24766882014-10-02 10:58:09 -0700767 controller.getUi().showFullscreen(fullScreen);
Tarun Nainani8eb00912014-07-17 12:28:32 -0700768 mFullScreen = fullScreen;
Vivek Sekharf96064b2014-07-28 16:32:34 -0700769 }
770
Sudheer Koganti24766882014-10-02 10:58:09 -0700771 public boolean exitFullscreen() {
772 if (mFullScreen) {
773 Controller controller = (Controller)mWebViewController;
774 controller.getUi().showFullscreen(false);
775 if (getWebView() != null)
776 getWebView().exitFullscreen();
777 mFullScreen = false;
778 return true;
779 }
780 return false;
781 }
782
783
784
785
Grace Kloba22ac16e2009-10-07 18:00:23 -0700786 // -------------------------------------------------------------------------
787 // WebChromeClient implementation for the main WebView
788 // -------------------------------------------------------------------------
789
790 private final WebChromeClient mWebChromeClient = new WebChromeClient() {
791 // Helper method to create a new tab or sub window.
792 private void createWindow(final boolean dialog, final Message msg) {
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700793 this.createWindow(dialog, msg, null, false);
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700794 }
795
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700796 private void createWindow(final boolean dialog, final Message msg, final String url,
797 final boolean opener_suppressed) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700798 WebView.WebViewTransport transport =
799 (WebView.WebViewTransport) msg.obj;
800 if (dialog) {
801 createSubWindow();
Michael Kolb8233fac2010-10-26 16:08:53 -0700802 mWebViewController.attachSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700803 transport.setWebView(mSubView);
804 } else {
Pankaj Garg21dad562015-07-02 17:17:24 -0700805 capture();
806
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700807 final Tab newTab = mWebViewController.openTab(url,
John Reck5949c662011-05-27 09:52:29 -0700808 Tab.this, true, true);
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700809 // This is special case for rendering links on a webpage in
810 // a new tab. If opener is suppressed, the WebContents created
811 // by the content layer are not fully initialized. This check
812 // will prevent content layer from overriding WebContents
813 // created by new tab with the uninitialized instance.
814 if (!opener_suppressed) {
815 transport.setWebView(newTab.getWebView());
816 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700817 }
818 msg.sendToTarget();
819 }
820
821 @Override
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700822 public void toggleFullscreenModeForTab(boolean enterFullscreen) {
823 if (mWebViewController instanceof Controller) {
Vivek Sekharf96064b2014-07-28 16:32:34 -0700824 setTabFullscreen(enterFullscreen);
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700825 }
826 }
827
828 @Override
Tarun Nainani8eb00912014-07-17 12:28:32 -0700829 public void onOffsetsForFullscreenChanged(float topControlsOffsetYPix,
830 float contentOffsetYPix,
831 float overdrawBottomHeightPix) {
832 if (mWebViewController instanceof Controller) {
Vivek Sekhar8ee3abb2014-07-14 12:32:05 -0700833 Controller controller = (Controller)mWebViewController;
Tarun Nainani8eb00912014-07-17 12:28:32 -0700834 controller.getUi().translateTitleBar(topControlsOffsetYPix);
Vivek Sekhar11b40062015-06-24 11:49:04 -0700835 // Resize the viewport if top controls is not visible
Vivek Sekhar2ee19a32015-07-02 17:03:57 -0700836 if (mMainView != null &&
837 (topControlsOffsetYPix == 0.0f || contentOffsetYPix == 0.0f))
Vivek Sekhar11b40062015-06-24 11:49:04 -0700838 ((BrowserWebView)mMainView).enableTopControls(
839 (topControlsOffsetYPix == 0.0f) ? true : false);
Vivek Sekhar8ee3abb2014-07-14 12:32:05 -0700840 }
841 }
842
843 @Override
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700844 public boolean isTabFullScreen() {
Vivek Sekharf96064b2014-07-28 16:32:34 -0700845 return mFullScreen;
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700846 }
847
848 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700849 public boolean onCreateWindow(WebView view, final boolean dialog,
850 final boolean userGesture, final Message resultMsg) {
851 // only allow new window or sub window for the foreground case
852 if (!mInForeground) {
853 return false;
854 }
855 // Short-circuit if we can't create any more tabs or sub windows.
856 if (dialog && mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -0700857 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700858 .setTitle(R.string.too_many_subwindows_dialog_title)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200859 .setIconAttribute(android.R.attr.alertDialogIcon)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700860 .setMessage(R.string.too_many_subwindows_dialog_message)
861 .setPositiveButton(R.string.ok, null)
862 .show();
863 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700864 } else if (!mWebViewController.getTabControl().canCreateNewTab()) {
Michael Kolb14612442011-06-24 13:06:29 -0700865 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700866 .setTitle(R.string.too_many_windows_dialog_title)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200867 .setIconAttribute(android.R.attr.alertDialogIcon)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700868 .setMessage(R.string.too_many_windows_dialog_message)
869 .setPositiveButton(R.string.ok, null)
870 .show();
871 return false;
872 }
873
874 // Short-circuit if this was a user gesture.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800875 if (userGesture || !mSettings.blockPopupWindows()) {
Tarun Nainani4f5137d2015-04-16 17:26:18 -0700876 WebView.WebViewTransport transport =
877 (WebView.WebViewTransport) resultMsg.obj;
878 CreateWindowParams windowParams = transport.getCreateWindowParams();
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700879 if (windowParams.mOpenerSuppressed) {
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700880 createWindow(dialog, resultMsg, windowParams.mURL, true);
881 // This is special case for rendering links on a webpage in
882 // a new tab. If opener is suppressed, the WebContents created
883 // by the content layer are not fully initialized. Returning false
884 // will prevent content layer from overriding WebContents
885 // created by new tab with the uninitialized instance.
886 return false;
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700887 }
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700888
889 createWindow(dialog, resultMsg);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700890 return true;
891 }
892
Tarun Nainani4f5137d2015-04-16 17:26:18 -0700893 createWindow(dialog, resultMsg);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700894 return true;
895 }
896
897 @Override
Patrick Scotteb5061b2009-11-18 15:00:30 -0500898 public void onRequestFocus(WebView view) {
899 if (!mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700900 mWebViewController.switchToTab(Tab.this);
Patrick Scotteb5061b2009-11-18 15:00:30 -0500901 }
902 }
903
904 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700905 public void onCloseWindow(WebView window) {
Michael Kolbc831b632011-05-11 09:30:34 -0700906 if (mParent != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700907 // JavaScript can only close popup window.
908 if (mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700909 mWebViewController.switchToTab(mParent);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700910 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700911 mWebViewController.closeTab(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700912 }
913 }
914
915 @Override
916 public void onProgressChanged(WebView view, int newProgress) {
John Reck30c714c2010-12-16 17:30:34 -0800917 mPageLoadProgress = newProgress;
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800918 if (newProgress == 100) {
919 mInPageLoad = false;
920 }
John Reck30c714c2010-12-16 17:30:34 -0800921 mWebViewController.onProgressChanged(Tab.this);
Michael Kolb72864272012-05-03 15:42:15 -0700922 if (mUpdateThumbnail && newProgress == 100) {
923 mUpdateThumbnail = false;
924 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700925 }
926
927 @Override
Leon Scroggins21d9b902010-03-11 09:33:11 -0500928 public void onReceivedTitle(WebView view, final String title) {
John Reck30c714c2010-12-16 17:30:34 -0800929 mCurrentState.mTitle = title;
Michael Kolb8233fac2010-10-26 16:08:53 -0700930 mWebViewController.onReceivedTitle(Tab.this, title);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700931 }
932
933 @Override
934 public void onReceivedIcon(WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -0800935 mCurrentState.mFavicon = icon;
Michael Kolb8233fac2010-10-26 16:08:53 -0700936 mWebViewController.onFavicon(Tab.this, view, icon);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700937 }
938
939 @Override
940 public void onReceivedTouchIconUrl(WebView view, String url,
941 boolean precomposed) {
Michael Kolb14612442011-06-24 13:06:29 -0700942 final ContentResolver cr = mContext.getContentResolver();
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400943 // Let precomposed icons take precedence over non-composed
944 // icons.
945 if (precomposed && mTouchIconLoader != null) {
946 mTouchIconLoader.cancel(false);
947 mTouchIconLoader = null;
948 }
949 // Have only one async task at a time.
950 if (mTouchIconLoader == null) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700951 mTouchIconLoader = new DownloadTouchIcon(Tab.this,
Michael Kolb14612442011-06-24 13:06:29 -0700952 mContext, cr, view);
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400953 mTouchIconLoader.execute(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700954 }
Pankaj Garg21dad562015-07-02 17:17:24 -0700955 mTouchIconUrl = url;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700956 }
957
958 @Override
959 public void onShowCustomView(View view,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800960 CustomViewCallback callback) {
Michael Kolb14612442011-06-24 13:06:29 -0700961 Activity activity = mWebViewController.getActivity();
962 if (activity != null) {
963 onShowCustomView(view, activity.getRequestedOrientation(), callback);
964 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400965 }
966
967 @Override
968 public void onShowCustomView(View view, int requestedOrientation,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800969 CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700970 if (mInForeground) mWebViewController.showCustomView(Tab.this, view,
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400971 requestedOrientation, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700972 }
973
974 @Override
975 public void onHideCustomView() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700976 if (mInForeground) mWebViewController.hideCustomView();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700977 }
978
979 /**
980 * The origin has exceeded its database quota.
981 * @param url the URL that exceeded the quota
982 * @param databaseIdentifier the identifier of the database on which the
983 * transaction that caused the quota overflow was run
984 * @param currentQuota the current quota for the origin.
985 * @param estimatedSize the estimated size of the database.
986 * @param totalUsedQuota is the sum of all origins' quota.
987 * @param quotaUpdater The callback to run when a decision to allow or
988 * deny quota has been made. Don't forget to call this!
989 */
990 @Override
991 public void onExceededDatabaseQuota(String url,
992 String databaseIdentifier, long currentQuota, long estimatedSize,
993 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -0700994 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -0700995 .onExceededDatabaseQuota(url, databaseIdentifier,
996 currentQuota, estimatedSize, totalUsedQuota,
997 quotaUpdater);
998 }
999
1000 /**
1001 * The Application Cache has exceeded its max size.
1002 * @param spaceNeeded is the amount of disk space that would be needed
1003 * in order for the last appcache operation to succeed.
1004 * @param totalUsedQuota is the sum of all origins' quota.
1005 * @param quotaUpdater A callback to inform the WebCore thread that a
1006 * new app cache size is available. This callback must always
1007 * be executed at some point to ensure that the sleeping
1008 * WebCore thread is woken up.
1009 */
1010 @Override
1011 public void onReachedMaxAppCacheSize(long spaceNeeded,
1012 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -07001013 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001014 .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota,
1015 quotaUpdater);
1016 }
1017
Ben Murdoch65acc352009-11-19 18:16:04 +00001018 /* Adds a JavaScript error message to the system log and if the JS
1019 * console is enabled in the about:debug options, to that console
1020 * also.
Ben Murdochc42addf2010-01-28 15:19:59 +00001021 * @param consoleMessage the message object.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001022 */
1023 @Override
Ben Murdochc42addf2010-01-28 15:19:59 +00001024 public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Jeff Hamilton47654f42010-09-07 09:57:51 -05001025 // Don't log console messages in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001026 if (isPrivateBrowsingEnabled()) return true;
Jeff Hamilton47654f42010-09-07 09:57:51 -05001027
Ben Murdochc42addf2010-01-28 15:19:59 +00001028 String message = "Console: " + consoleMessage.message() + " "
1029 + consoleMessage.sourceId() + ":"
1030 + consoleMessage.lineNumber();
1031
1032 switch (consoleMessage.messageLevel()) {
1033 case TIP:
1034 Log.v(CONSOLE_LOGTAG, message);
1035 break;
1036 case LOG:
1037 Log.i(CONSOLE_LOGTAG, message);
1038 break;
1039 case WARNING:
1040 Log.w(CONSOLE_LOGTAG, message);
1041 break;
1042 case ERROR:
1043 Log.e(CONSOLE_LOGTAG, message);
1044 break;
1045 case DEBUG:
1046 Log.d(CONSOLE_LOGTAG, message);
1047 break;
1048 }
1049
1050 return true;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001051 }
1052
1053 /**
1054 * Ask the browser for an icon to represent a <video> element.
1055 * This icon will be used if the Web page did not specify a poster attribute.
1056 * @return Bitmap The icon or null if no such icon is available.
1057 */
1058 @Override
1059 public Bitmap getDefaultVideoPoster() {
1060 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001061 return mWebViewController.getDefaultVideoPoster();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001062 }
1063 return null;
1064 }
1065
1066 /**
1067 * Ask the host application for a custom progress view to show while
1068 * a <video> is loading.
1069 * @return View The progress view.
1070 */
1071 @Override
1072 public View getVideoLoadingProgressView() {
1073 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001074 return mWebViewController.getVideoLoadingProgressView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001075 }
1076 return null;
1077 }
1078
1079 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00001080 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001081 if (mInForeground) {
Ben Murdoch8cad4132012-01-11 10:56:43 +00001082 mWebViewController.openFileChooser(uploadMsg, acceptType, capture);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001083 } else {
1084 uploadMsg.onReceiveValue(null);
1085 }
1086 }
1087
Vivek Sekharb54614f2014-05-01 19:03:37 -07001088 @Override
1089 public void showFileChooser(ValueCallback<String[]> uploadFilePaths, String acceptTypes,
1090 boolean capture) {
1091 if (mInForeground) {
1092 mWebViewController.showFileChooser(uploadFilePaths, acceptTypes, capture);
1093 } else {
1094 uploadFilePaths.onReceiveValue(null);
1095 }
1096 }
1097
Grace Kloba22ac16e2009-10-07 18:00:23 -07001098 /**
1099 * Deliver a list of already-visited URLs
1100 */
1101 @Override
1102 public void getVisitedHistory(final ValueCallback<String[]> callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001103 mWebViewController.getVisitedHistory(callback);
1104 }
Ben Murdoch8029a772010-11-16 11:58:21 +00001105
1106 @Override
1107 public void setupAutoFill(Message message) {
1108 // Prompt the user to set up their profile.
1109 final Message msg = message;
Michael Kolb14612442011-06-24 13:06:29 -07001110 AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
1111 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
Ben Murdoch1d676b62011-01-17 12:54:24 +00001112 Context.LAYOUT_INFLATER_SERVICE);
1113 final View layout = inflater.inflate(R.layout.setup_autofill_dialog, null);
1114
1115 builder.setView(layout)
1116 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1117 @Override
1118 public void onClick(DialogInterface dialog, int id) {
1119 CheckBox disableAutoFill = (CheckBox) layout.findViewById(
1120 R.id.setup_autofill_dialog_disable_autofill);
1121
1122 if (disableAutoFill.isChecked()) {
1123 // Disable autofill and show a toast with how to turn it on again.
John Reck35e9dd62011-04-25 09:01:54 -07001124 mSettings.setAutofillEnabled(false);
Michael Kolb14612442011-06-24 13:06:29 -07001125 Toast.makeText(mContext,
Ben Murdoch1d676b62011-01-17 12:54:24 +00001126 R.string.autofill_setup_dialog_negative_toast,
1127 Toast.LENGTH_LONG).show();
1128 } else {
1129 // Take user to the AutoFill profile editor. When they return,
1130 // we will send the message that we pass here which will trigger
1131 // the form to get filled out with their new profile.
1132 mWebViewController.setupAutoFill(msg);
1133 }
1134 }
1135 })
1136 .setNegativeButton(R.string.cancel, null)
1137 .show();
Ben Murdoch8029a772010-11-16 11:58:21 +00001138 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001139 };
1140
1141 // -------------------------------------------------------------------------
1142 // WebViewClient implementation for the sub window
1143 // -------------------------------------------------------------------------
1144
1145 // Subclass of WebViewClient used in subwindows to notify the main
1146 // WebViewClient of certain WebView activities.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001147 private static class SubWindowClient extends WebViewClient {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001148 // The main WebViewClient.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001149 private final WebViewClient mClient;
Michael Kolb8233fac2010-10-26 16:08:53 -07001150 private final WebViewController mController;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001151
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001152 SubWindowClient(WebViewClient client, WebViewController controller) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001153 mClient = client;
Michael Kolb8233fac2010-10-26 16:08:53 -07001154 mController = controller;
Leon Scroggins III211ba542010-04-19 13:21:13 -04001155 }
1156 @Override
1157 public void onPageStarted(WebView view, String url, Bitmap favicon) {
1158 // Unlike the others, do not call mClient's version, which would
1159 // change the progress bar. However, we do want to remove the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001160 // find or select dialog.
Michael Kolb8233fac2010-10-26 16:08:53 -07001161 mController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001162 }
1163 @Override
1164 public void doUpdateVisitedHistory(WebView view, String url,
1165 boolean isReload) {
1166 mClient.doUpdateVisitedHistory(view, url, isReload);
1167 }
1168 @Override
1169 public boolean shouldOverrideUrlLoading(WebView view, String url) {
1170 return mClient.shouldOverrideUrlLoading(view, url);
1171 }
1172 @Override
1173 public void onReceivedSslError(WebView view, SslErrorHandler handler,
1174 SslError error) {
1175 mClient.onReceivedSslError(view, handler, error);
1176 }
1177 @Override
Brian Carlstrom8862c1d2011-06-02 01:05:55 -07001178 public void onReceivedClientCertRequest(WebView view,
1179 ClientCertRequestHandler handler, String host_and_port) {
1180 mClient.onReceivedClientCertRequest(view, handler, host_and_port);
1181 }
1182 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -07001183 public void onReceivedHttpAuthRequest(WebView view,
1184 HttpAuthHandler handler, String host, String realm) {
1185 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
1186 }
1187 @Override
1188 public void onFormResubmission(WebView view, Message dontResend,
1189 Message resend) {
1190 mClient.onFormResubmission(view, dontResend, resend);
1191 }
1192 @Override
1193 public void onReceivedError(WebView view, int errorCode,
1194 String description, String failingUrl) {
1195 mClient.onReceivedError(view, errorCode, description, failingUrl);
1196 }
1197 @Override
1198 public boolean shouldOverrideKeyEvent(WebView view,
1199 android.view.KeyEvent event) {
1200 return mClient.shouldOverrideKeyEvent(view, event);
1201 }
1202 @Override
1203 public void onUnhandledKeyEvent(WebView view,
1204 android.view.KeyEvent event) {
1205 mClient.onUnhandledKeyEvent(view, event);
1206 }
1207 }
1208
1209 // -------------------------------------------------------------------------
1210 // WebChromeClient implementation for the sub window
1211 // -------------------------------------------------------------------------
1212
1213 private class SubWindowChromeClient extends WebChromeClient {
1214 // The main WebChromeClient.
1215 private final WebChromeClient mClient;
1216
1217 SubWindowChromeClient(WebChromeClient client) {
1218 mClient = client;
1219 }
1220 @Override
1221 public void onProgressChanged(WebView view, int newProgress) {
1222 mClient.onProgressChanged(view, newProgress);
1223 }
1224 @Override
1225 public boolean onCreateWindow(WebView view, boolean dialog,
1226 boolean userGesture, android.os.Message resultMsg) {
1227 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
1228 }
1229 @Override
1230 public void onCloseWindow(WebView window) {
1231 if (window != mSubView) {
1232 Log.e(LOGTAG, "Can't close the window");
1233 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001234 mWebViewController.dismissSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001235 }
1236 }
1237
1238 // -------------------------------------------------------------------------
1239
1240 // Construct a new tab
Michael Kolb7bcafde2011-05-09 13:55:59 -07001241 Tab(WebViewController wvcontroller, WebView w) {
John Reck1cf4b792011-07-26 10:22:22 -07001242 this(wvcontroller, w, null);
1243 }
1244
1245 Tab(WebViewController wvcontroller, Bundle state) {
1246 this(wvcontroller, null, state);
1247 }
1248
1249 Tab(WebViewController wvcontroller, WebView w, Bundle state) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001250 mWebViewController = wvcontroller;
Michael Kolb14612442011-06-24 13:06:29 -07001251 mContext = mWebViewController.getContext();
John Reck35e9dd62011-04-25 09:01:54 -07001252 mSettings = BrowserSettings.getInstance();
Michael Kolb14612442011-06-24 13:06:29 -07001253 mDataController = DataController.getInstance(mContext);
1254 mCurrentState = new PageState(mContext, w != null
John Reck847b5322011-04-14 17:02:18 -07001255 ? w.isPrivateBrowsingEnabled() : false);
Tarun Nainani8084c822014-06-25 13:38:06 -07001256 setTimeStamp();
Michael Kolb8233fac2010-10-26 16:08:53 -07001257 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001258 mInForeground = false;
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001259 mWebViewDestroyedByMemoryMonitor = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001260
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001261 mDownloadListener = new BrowserDownloadListener() {
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001262 public void onDownloadStart(String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001263 String contentDisposition, String mimetype, String referer,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001264 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001265 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001266 mimetype, referer, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001267 }
1268 };
1269
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001270 mCaptureWidth = mContext.getResources().getDimensionPixelSize(R.dimen.tab_thumbnail_width);
1271 mCaptureHeight =mContext.getResources().getDimensionPixelSize(R.dimen.tab_thumbnail_height);
1272
1273 initCaptureBitmap();
1274
John Reck1cf4b792011-07-26 10:22:22 -07001275 restoreState(state);
John Reck52be4782011-08-26 15:37:29 -07001276 if (getId() == -1) {
1277 mId = TabControl.getNextId();
1278 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001279 setWebView(w);
Michael Kolb9ef259a2011-07-12 15:33:08 -07001280 mHandler = new Handler() {
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001281 @Override
Michael Kolb9ef259a2011-07-12 15:33:08 -07001282 public void handleMessage(Message m) {
John Reck1cf4b792011-07-26 10:22:22 -07001283 switch (m.what) {
1284 case MSG_CAPTURE:
1285 capture();
1286 break;
1287 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001288 }
1289 };
Pankaj Garg1c13cab2015-05-12 11:52:17 -07001290
1291 mFirstPixelObservable = new Observable();
1292 mFirstPixelObservable.set(false);
1293 mTabHistoryUpdateObservable = new Observable();
John Reck1cf4b792011-07-26 10:22:22 -07001294 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001295
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001296 private void initCaptureBitmap() {
1297 mCapture = Bitmap.createBitmap(mCaptureWidth, mCaptureHeight, Bitmap.Config.RGB_565);
1298 mCapture.eraseColor(Color.WHITE);
Michael Kolb72864272012-05-03 15:42:15 -07001299 }
1300
Mathew Inwoode09305e2011-09-02 12:03:26 +01001301 /**
1302 * This is used to get a new ID when the tab has been preloaded, before it is displayed and
1303 * added to TabControl. Preloaded tabs can be created before restoreInstanceState, leading
1304 * to overlapping IDs between the preloaded and restored tabs.
1305 */
1306 public void refreshIdAfterPreload() {
1307 mId = TabControl.getNextId();
1308 }
1309
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001310 public void setController(WebViewController ctl) {
1311 mWebViewController = ctl;
1312
John Reck1cf4b792011-07-26 10:22:22 -07001313 if (mWebViewController.shouldCaptureThumbnails()) {
1314 synchronized (Tab.this) {
1315 if (mCapture == null) {
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001316 initCaptureBitmap();
1317 if (mInForeground && !mHandler.hasMessages(MSG_CAPTURE)) {
1318 mHandler.sendEmptyMessageDelayed(MSG_CAPTURE, CAPTURE_DELAY);
John Reck1cf4b792011-07-26 10:22:22 -07001319 }
1320 }
1321 }
1322 } else {
1323 synchronized (Tab.this) {
1324 mCapture = null;
1325 deleteThumbnail();
1326 }
1327 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001328 }
1329
Michael Kolbc831b632011-05-11 09:30:34 -07001330 public long getId() {
1331 return mId;
1332 }
1333
Michael Kolb91911a22012-01-17 11:21:25 -08001334 void setWebView(WebView w) {
1335 setWebView(w, true);
1336 }
1337
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001338 public boolean isNativeActive(){
1339 if (mMainView == null)
1340 return false;
1341 return true;
1342 }
1343
1344 public void setTimeStamp(){
1345 Date d = new Date();
1346 timestamp = (new Timestamp(d.getTime()));
1347 }
1348
1349 public Timestamp getTimestamp() {
1350 return timestamp;
1351 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001352 /**
1353 * Sets the WebView for this tab, correctly removing the old WebView from
1354 * the container view.
1355 */
Michael Kolb91911a22012-01-17 11:21:25 -08001356 void setWebView(WebView w, boolean restore) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001357 if (mMainView == w) {
1358 return;
1359 }
Michael Kolba713ec82010-11-29 17:27:06 -08001360
Michael Kolba713ec82010-11-29 17:27:06 -08001361 mWebViewController.onSetWebView(this, w);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001362
John Reck1cf4b792011-07-26 10:22:22 -07001363 if (mMainView != null) {
John Reck8ee633f2011-08-09 16:00:35 -07001364 mMainView.setPictureListener(null);
John Reck1cf4b792011-07-26 10:22:22 -07001365 if (w != null) {
1366 syncCurrentState(w, null);
1367 } else {
Panos Thomasa9a5a582014-03-18 19:20:08 -07001368 mCurrentState = new PageState(mContext, mMainView.isPrivateBrowsingEnabled());
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001369
1370 if (mWebViewDestroyedByMemoryMonitor) {
1371 /*
1372 * If tab was destroyed as a result of the MemoryMonitor
1373 * then we need to restore the state properties
1374 * from the old WebView (mMainView)
1375 */
1376 syncCurrentState(mMainView, null);
1377 mWebViewDestroyedByMemoryMonitor = false;
1378 }
John Reck1cf4b792011-07-26 10:22:22 -07001379 }
1380 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001381 // set the new one
1382 mMainView = w;
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001383
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001384 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001385 if (mMainView != null) {
1386 mMainView.setWebViewClient(mWebViewClient);
1387 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001388 // Attach DownloadManager so that downloads can start in an active
1389 // or a non-active window. This can happen when going to a site that
1390 // does a redirect after a period of time. The user could have
1391 // switched to another tab while waiting for the download to start.
1392 mMainView.setDownloadListener(mDownloadListener);
John Reck8ee633f2011-08-09 16:00:35 -07001393 TabControl tc = mWebViewController.getTabControl();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001394 if (tc != null /*&& tc.getOnThumbnailUpdatedListener() != null*/) {
John Reck8ee633f2011-08-09 16:00:35 -07001395 mMainView.setPictureListener(this);
1396 }
Michael Kolb91911a22012-01-17 11:21:25 -08001397 if (restore && (mSavedState != null)) {
John Reck8b9bb8b2012-03-08 13:19:40 -08001398 restoreUserAgent();
John Reck6c2e2f32011-08-22 13:41:23 -07001399 WebBackForwardList restoredState
1400 = mMainView.restoreState(mSavedState);
1401 if (restoredState == null || restoredState.getSize() == 0) {
1402 Log.w(LOGTAG, "Failed to restore WebView state!");
1403 loadUrl(mCurrentState.mOriginalUrl, null);
1404 }
John Reck1cf4b792011-07-26 10:22:22 -07001405 mSavedState = null;
1406 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001407 }
1408 }
1409
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001410 public void destroyThroughMemoryMonitor() {
1411 mWebViewDestroyedByMemoryMonitor = true;
1412 destroy();
1413 }
1414
Grace Kloba22ac16e2009-10-07 18:00:23 -07001415 /**
1416 * Destroy the tab's main WebView and subWindow if any
1417 */
1418 void destroy() {
Tarun Nainani2c1dd7c2014-07-05 16:40:12 -07001419
Matthew Huib7f2e9c2014-04-16 11:12:37 -04001420 if (mPostponeDestroy) {
1421 mShouldDestroy = true;
1422 return;
1423 }
1424 mShouldDestroy = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001425 if (mMainView != null) {
1426 dismissSubWindow();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001427 // save the WebView to call destroy() after detach it from the tab
Vivek Sekhar53ef8932015-06-18 16:51:43 -07001428 final WebView webView = mMainView;
Vivek Sekhar2ee19a32015-07-02 17:03:57 -07001429 setWebView(null);
Site Mao61b68212015-07-16 10:56:31 -07001430 if (!mWebViewDestroyedByMemoryMonitor && !BaseUi.isUiLowPowerMode()) {
Vivek Sekhar2ee19a32015-07-02 17:03:57 -07001431 // Tabs can be reused with new instance of WebView so delete the snapshots
Vivek Sekhar53ef8932015-06-18 16:51:43 -07001432 webView.getSnapshotIds(new ValueCallback<List<Integer>>() {
1433 @Override
1434 public void onReceiveValue(List<Integer> ids) {
1435 int currentTabIdx = mWebViewController.getTabControl().getCurrentPosition();
1436 for (Integer id : ids) {
1437 if (getTabIdxFromCaptureIdx(id) == currentTabIdx) {
1438 webView.deleteSnapshot(id);
1439 }
1440 }
Vivek Sekhar53ef8932015-06-18 16:51:43 -07001441 webView.destroy();
Pankaj Garg1c13cab2015-05-12 11:52:17 -07001442 }
Vivek Sekhar53ef8932015-06-18 16:51:43 -07001443 });
Vivek Sekhar2ee19a32015-07-02 17:03:57 -07001444 } else {
1445 webView.destroy();
Pankaj Garg1c13cab2015-05-12 11:52:17 -07001446 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001447 }
1448 }
1449
Matthew Huib7f2e9c2014-04-16 11:12:37 -04001450 private boolean mPostponeDestroy = false;
1451 private boolean mShouldDestroy = false;
1452
1453 public void postponeDestroy() {
1454 mPostponeDestroy = true;
1455 }
1456
1457 public void performPostponedDestroy() {
1458 mPostponeDestroy = false;
1459 if (mShouldDestroy) {
1460 destroy();
1461 }
1462 }
1463
Grace Kloba22ac16e2009-10-07 18:00:23 -07001464 /**
1465 * Remove the tab from the parent
1466 */
1467 void removeFromTree() {
1468 // detach the children
Michael Kolbc831b632011-05-11 09:30:34 -07001469 if (mChildren != null) {
1470 for(Tab t : mChildren) {
1471 t.setParent(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001472 }
1473 }
1474 // remove itself from the parent list
Michael Kolbc831b632011-05-11 09:30:34 -07001475 if (mParent != null) {
1476 mParent.mChildren.remove(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001477 }
Tarun Nainani8eb00912014-07-17 12:28:32 -07001478
1479 mCapture = null;
John Reck1cf4b792011-07-26 10:22:22 -07001480 deleteThumbnail();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001481 }
1482
1483 /**
1484 * Create a new subwindow unless a subwindow already exists.
1485 * @return True if a new subwindow was created. False if one already exists.
1486 */
1487 boolean createSubWindow() {
1488 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001489 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001490 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001491 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001492 mSubView.setWebChromeClient(new SubWindowChromeClient(
1493 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001494 // Set a different DownloadListener for the mSubView, since it will
1495 // just need to dismiss the mSubView, rather than close the Tab
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001496 mSubView.setDownloadListener(new BrowserDownloadListener() {
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001497 public void onDownloadStart(String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001498 String contentDisposition, String mimetype, String referer,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001499 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001500 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001501 contentDisposition, mimetype, referer, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001502 if (mSubView.copyBackForwardList().getSize() == 0) {
1503 // This subwindow was opened for the sole purpose of
1504 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001505 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001506 }
1507 }
1508 });
Michael Kolb14612442011-06-24 13:06:29 -07001509 mSubView.setOnCreateContextMenuListener(mWebViewController.getActivity());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001510 return true;
1511 }
1512 return false;
1513 }
1514
1515 /**
1516 * Dismiss the subWindow for the tab.
1517 */
1518 void dismissSubWindow() {
1519 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001520 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001521 mSubView.destroy();
1522 mSubView = null;
1523 mSubViewContainer = null;
1524 }
1525 }
1526
Grace Kloba22ac16e2009-10-07 18:00:23 -07001527
1528 /**
1529 * Set the parent tab of this tab.
1530 */
Michael Kolbc831b632011-05-11 09:30:34 -07001531 void setParent(Tab parent) {
John Reck52be4782011-08-26 15:37:29 -07001532 if (parent == this) {
1533 throw new IllegalStateException("Cannot set parent to self!");
1534 }
Michael Kolbc831b632011-05-11 09:30:34 -07001535 mParent = parent;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001536 // This tab may have been freed due to low memory. If that is the case,
Michael Kolbc831b632011-05-11 09:30:34 -07001537 // the parent tab id is already saved. If we are changing that id
Grace Kloba22ac16e2009-10-07 18:00:23 -07001538 // (most likely due to removing the parent tab) we must update the
Michael Kolbc831b632011-05-11 09:30:34 -07001539 // parent tab id in the saved Bundle.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001540 if (mSavedState != null) {
1541 if (parent == null) {
1542 mSavedState.remove(PARENTTAB);
1543 } else {
Michael Kolbc831b632011-05-11 09:30:34 -07001544 mSavedState.putLong(PARENTTAB, parent.getId());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001545 }
1546 }
John Reckb0a86db2011-05-24 14:05:58 -07001547
1548 // Sync the WebView useragent with the parent
1549 if (parent != null && mSettings.hasDesktopUseragent(parent.getWebView())
1550 != mSettings.hasDesktopUseragent(getWebView())) {
1551 mSettings.toggleDesktopUseragent(getWebView());
1552 }
John Reck52be4782011-08-26 15:37:29 -07001553
1554 if (parent != null && parent.getId() == getId()) {
1555 throw new IllegalStateException("Parent has same ID as child!");
1556 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001557 }
1558
1559 /**
Michael Kolbc831b632011-05-11 09:30:34 -07001560 * If this Tab was created through another Tab, then this method returns
1561 * that Tab.
1562 * @return the Tab parent or null
1563 */
1564 public Tab getParent() {
1565 return mParent;
1566 }
1567
1568 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001569 * When a Tab is created through the content of another Tab, then we
1570 * associate the Tabs.
1571 * @param child the Tab that was created from this Tab
1572 */
1573 void addChildTab(Tab child) {
Michael Kolbc831b632011-05-11 09:30:34 -07001574 if (mChildren == null) {
1575 mChildren = new Vector<Tab>();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001576 }
Michael Kolbc831b632011-05-11 09:30:34 -07001577 mChildren.add(child);
1578 child.setParent(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001579 }
1580
Michael Kolbc831b632011-05-11 09:30:34 -07001581 Vector<Tab> getChildren() {
1582 return mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001583 }
1584
1585 void resume() {
1586 if (mMainView != null) {
John Reck56c1fcf2011-08-17 10:15:16 -07001587 setupHwAcceleration(mMainView);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001588 mMainView.onResume();
1589 if (mSubView != null) {
1590 mSubView.onResume();
1591 }
1592 }
1593 }
1594
John Reck56c1fcf2011-08-17 10:15:16 -07001595 private void setupHwAcceleration(View web) {
1596 if (web == null) return;
1597 BrowserSettings settings = BrowserSettings.getInstance();
1598 if (settings.isHardwareAccelerated()) {
1599 web.setLayerType(View.LAYER_TYPE_NONE, null);
1600 } else {
1601 web.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
1602 }
1603 }
1604
Grace Kloba22ac16e2009-10-07 18:00:23 -07001605 void pause() {
1606 if (mMainView != null) {
1607 mMainView.onPause();
1608 if (mSubView != null) {
1609 mSubView.onPause();
1610 }
1611 }
1612 }
1613
1614 void putInForeground() {
John Reck8ee633f2011-08-09 16:00:35 -07001615 if (mInForeground) {
1616 return;
1617 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001618 mInForeground = true;
1619 resume();
Michael Kolb14612442011-06-24 13:06:29 -07001620 Activity activity = mWebViewController.getActivity();
1621 mMainView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001622 if (mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -07001623 mSubView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001624 }
Axesh R. Ajmerac6b5c322015-05-01 11:06:10 -07001625
Leon Scroggins1961ed22010-12-07 15:22:21 -05001626 mWebViewController.bookmarkedStatusHasChanged(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001627 }
1628
1629 void putInBackground() {
John Reck8ee633f2011-08-09 16:00:35 -07001630 if (!mInForeground) {
1631 return;
1632 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001633 mInForeground = false;
1634 pause();
1635 mMainView.setOnCreateContextMenuListener(null);
1636 if (mSubView != null) {
1637 mSubView.setOnCreateContextMenuListener(null);
1638 }
1639 }
1640
Michael Kolb8233fac2010-10-26 16:08:53 -07001641 boolean inForeground() {
1642 return mInForeground;
1643 }
1644
Grace Kloba22ac16e2009-10-07 18:00:23 -07001645 /**
1646 * Return the top window of this tab; either the subwindow if it is not
1647 * null or the main window.
1648 * @return The top window of this tab.
1649 */
1650 WebView getTopWindow() {
1651 if (mSubView != null) {
1652 return mSubView;
1653 }
1654 return mMainView;
1655 }
1656
1657 /**
1658 * Return the main window of this tab. Note: if a tab is freed in the
1659 * background, this can return null. It is only guaranteed to be
1660 * non-null for the current tab.
1661 * @return The main WebView of this tab.
1662 */
1663 WebView getWebView() {
1664 return mMainView;
1665 }
1666
Michael Kolba713ec82010-11-29 17:27:06 -08001667 void setViewContainer(View container) {
1668 mContainer = container;
1669 }
1670
Michael Kolb8233fac2010-10-26 16:08:53 -07001671 View getViewContainer() {
1672 return mContainer;
1673 }
1674
Grace Kloba22ac16e2009-10-07 18:00:23 -07001675 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001676 * Return whether private browsing is enabled for the main window of
1677 * this tab.
1678 * @return True if private browsing is enabled.
1679 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001680 boolean isPrivateBrowsingEnabled() {
John Reck502a3532011-08-16 14:21:46 -07001681 return mCurrentState.mIncognito;
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001682 }
1683
1684 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001685 * Return the subwindow of this tab or null if there is no subwindow.
1686 * @return The subwindow of this tab or null.
1687 */
1688 WebView getSubWebView() {
1689 return mSubView;
1690 }
1691
Michael Kolb1514bb72010-11-22 09:11:48 -08001692 void setSubWebView(WebView subView) {
1693 mSubView = subView;
1694 }
1695
Michael Kolb8233fac2010-10-26 16:08:53 -07001696 View getSubViewContainer() {
1697 return mSubViewContainer;
1698 }
1699
Michael Kolb1514bb72010-11-22 09:11:48 -08001700 void setSubViewContainer(View subViewContainer) {
1701 mSubViewContainer = subViewContainer;
1702 }
1703
Grace Kloba22ac16e2009-10-07 18:00:23 -07001704
1705 /**
1706 * @return The application id string
1707 */
1708 String getAppId() {
1709 return mAppId;
1710 }
1711
1712 /**
1713 * Set the application id string
1714 * @param id
1715 */
1716 void setAppId(String id) {
1717 mAppId = id;
1718 }
1719
Michael Kolbe28b3472011-08-04 16:54:31 -07001720 boolean closeOnBack() {
1721 return mCloseOnBack;
1722 }
1723
1724 void setCloseOnBack(boolean close) {
1725 mCloseOnBack = close;
1726 }
1727
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001728 boolean getDerivedFromIntent() {
1729 return mDerivedFromIntent;
1730 }
1731
1732 void setDerivedFromIntent(boolean derived) {
1733 mDerivedFromIntent = derived;
1734 }
1735
Grace Kloba22ac16e2009-10-07 18:00:23 -07001736 String getUrl() {
John Reck324d4402011-01-11 16:56:42 -08001737 return UrlUtils.filteredUrl(mCurrentState.mUrl);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001738 }
1739
Tarun Nainani8eb00912014-07-17 12:28:32 -07001740
1741 protected void onPageFinished() {
1742 mPageFinished = true;
Ze G Riande2a675c22015-06-03 11:15:24 -07001743 isDistillable();
Tarun Nainani8eb00912014-07-17 12:28:32 -07001744 }
1745
1746 public boolean getPageFinishedStatus() {
1747 return mPageFinished;
1748 }
1749
John Reck49a603c2011-03-03 09:33:05 -08001750 String getOriginalUrl() {
John Reckdb22ec42011-06-29 11:31:24 -07001751 if (mCurrentState.mOriginalUrl == null) {
1752 return getUrl();
John Reck49a603c2011-03-03 09:33:05 -08001753 }
John Reckdb22ec42011-06-29 11:31:24 -07001754 return UrlUtils.filteredUrl(mCurrentState.mOriginalUrl);
John Reck49a603c2011-03-03 09:33:05 -08001755 }
1756
Grace Kloba22ac16e2009-10-07 18:00:23 -07001757 /**
John Reck30c714c2010-12-16 17:30:34 -08001758 * Get the title of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001759 */
1760 String getTitle() {
John Reck30c714c2010-12-16 17:30:34 -08001761 return mCurrentState.mTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001762 }
1763
1764 /**
John Reck30c714c2010-12-16 17:30:34 -08001765 * Get the favicon of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001766 */
1767 Bitmap getFavicon() {
John Reck1cf4b792011-07-26 10:22:22 -07001768 if (mCurrentState.mFavicon != null) {
1769 return mCurrentState.mFavicon;
1770 }
1771 return getDefaultFavicon(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001772 }
1773
Pankaj Garg32e1b942015-06-03 18:13:24 -07001774 public boolean hasFavicon() {
1775 return mCurrentState.mFavicon != null;
1776 }
1777
John Recke969cc52010-12-21 17:24:43 -08001778 public boolean isBookmarkedSite() {
1779 return mCurrentState.mIsBookmarkedSite;
1780 }
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001781
Grace Kloba22ac16e2009-10-07 18:00:23 -07001782 /**
Steve Block08a6f0c2011-10-06 12:12:53 +01001783 * Sets the security state, clears the SSL certificate error and informs
1784 * the controller.
1785 */
Steve Block2466eff2011-10-03 15:33:09 +01001786 private void setSecurityState(SecurityState securityState) {
1787 mCurrentState.mSecurityState = securityState;
Steve Block08a6f0c2011-10-06 12:12:53 +01001788 mCurrentState.mSslCertificateError = null;
Steve Block2466eff2011-10-03 15:33:09 +01001789 mWebViewController.onUpdatedSecurityState(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001790 }
1791
1792 /**
Steve Block2466eff2011-10-03 15:33:09 +01001793 * @return The tab's security state.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001794 */
Steve Block2466eff2011-10-03 15:33:09 +01001795 SecurityState getSecurityState() {
1796 return mCurrentState.mSecurityState;
John Reck30c714c2010-12-16 17:30:34 -08001797 }
1798
Steve Block08a6f0c2011-10-06 12:12:53 +01001799 /**
1800 * Gets the SSL certificate error, if any, for the page's main resource.
1801 * This is only non-null when the security state is
1802 * SECURITY_STATE_BAD_CERTIFICATE.
1803 */
1804 SslError getSslCertificateError() {
1805 return mCurrentState.mSslCertificateError;
1806 }
1807
John Reck30c714c2010-12-16 17:30:34 -08001808 int getLoadProgress() {
1809 if (mInPageLoad) {
1810 return mPageLoadProgress;
1811 }
1812 return 100;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001813 }
1814
1815 /**
1816 * @return TRUE if onPageStarted is called while onPageFinished is not
1817 * called yet.
1818 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001819 boolean inPageLoad() {
1820 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001821 }
1822
Grace Kloba22ac16e2009-10-07 18:00:23 -07001823 /**
John Reck1cf4b792011-07-26 10:22:22 -07001824 * @return The Bundle with the tab's state if it can be saved, otherwise null
Grace Kloba22ac16e2009-10-07 18:00:23 -07001825 */
John Reck1cf4b792011-07-26 10:22:22 -07001826 public Bundle saveState() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001827 // If the WebView is null it means we ran low on memory and we already
1828 // stored the saved state in mSavedState.
1829 if (mMainView == null) {
John Reck1cf4b792011-07-26 10:22:22 -07001830 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001831 }
John Reck6c2e2f32011-08-22 13:41:23 -07001832
1833 if (TextUtils.isEmpty(mCurrentState.mUrl)) {
John Reck1cf4b792011-07-26 10:22:22 -07001834 return null;
John Reck24f18262011-06-17 14:47:20 -07001835 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001836
1837 mSavedState = new Bundle();
John Reck6c2e2f32011-08-22 13:41:23 -07001838 WebBackForwardList savedList = mMainView.saveState(mSavedState);
1839 if (savedList == null || savedList.getSize() == 0) {
1840 Log.w(LOGTAG, "Failed to save back/forward list for "
1841 + mCurrentState.mUrl);
1842 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001843
Michael Kolbc831b632011-05-11 09:30:34 -07001844 mSavedState.putLong(ID, mId);
John Reck30c714c2010-12-16 17:30:34 -08001845 mSavedState.putString(CURRURL, mCurrentState.mUrl);
1846 mSavedState.putString(CURRTITLE, mCurrentState.mTitle);
John Reck1cf4b792011-07-26 10:22:22 -07001847 mSavedState.putBoolean(INCOGNITO, mMainView.isPrivateBrowsingEnabled());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001848 if (mAppId != null) {
1849 mSavedState.putString(APPID, mAppId);
1850 }
Michael Kolbe28b3472011-08-04 16:54:31 -07001851 mSavedState.putBoolean(CLOSEFLAG, mCloseOnBack);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001852 // Remember the parent tab so the relationship can be restored.
Michael Kolbc831b632011-05-11 09:30:34 -07001853 if (mParent != null) {
1854 mSavedState.putLong(PARENTTAB, mParent.mId);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001855 }
John Reckb0a86db2011-05-24 14:05:58 -07001856 mSavedState.putBoolean(USERAGENT,
1857 mSettings.hasDesktopUseragent(getWebView()));
John Reck1cf4b792011-07-26 10:22:22 -07001858 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001859 }
1860
1861 /*
1862 * Restore the state of the tab.
1863 */
John Reck1cf4b792011-07-26 10:22:22 -07001864 private void restoreState(Bundle b) {
1865 mSavedState = b;
1866 if (mSavedState == null) {
1867 return;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001868 }
1869 // Restore the internal state even if the WebView fails to restore.
1870 // This will maintain the app id, original url and close-on-exit values.
Michael Kolbc831b632011-05-11 09:30:34 -07001871 mId = b.getLong(ID);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001872 mAppId = b.getString(APPID);
Michael Kolbe28b3472011-08-04 16:54:31 -07001873 mCloseOnBack = b.getBoolean(CLOSEFLAG);
John Reck8b9bb8b2012-03-08 13:19:40 -08001874 restoreUserAgent();
John Reck1cf4b792011-07-26 10:22:22 -07001875 String url = b.getString(CURRURL);
1876 String title = b.getString(CURRTITLE);
1877 boolean incognito = b.getBoolean(INCOGNITO);
1878 mCurrentState = new PageState(mContext, incognito, url, null);
1879 mCurrentState.mTitle = title;
1880 synchronized (Tab.this) {
1881 if (mCapture != null) {
John Reck4eadc342011-10-31 14:04:10 -07001882 DataController.getInstance(mContext).loadThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07001883 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001884 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001885 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04001886
John Reck8b9bb8b2012-03-08 13:19:40 -08001887 private void restoreUserAgent() {
1888 if (mMainView == null || mSavedState == null) {
1889 return;
1890 }
1891 if (mSavedState.getBoolean(USERAGENT)
1892 != mSettings.hasDesktopUseragent(mMainView)) {
1893 mSettings.toggleDesktopUseragent(mMainView);
1894 }
1895 }
1896
Leon Scroggins1961ed22010-12-07 15:22:21 -05001897 public void updateBookmarkedStatus() {
John Recke969cc52010-12-21 17:24:43 -08001898 mDataController.queryBookmarkStatus(getUrl(), mIsBookmarkCallback);
Leon Scroggins1961ed22010-12-07 15:22:21 -05001899 }
1900
John Recke969cc52010-12-21 17:24:43 -08001901 private DataController.OnQueryUrlIsBookmark mIsBookmarkCallback
1902 = new DataController.OnQueryUrlIsBookmark() {
1903 @Override
1904 public void onQueryUrlIsBookmark(String url, boolean isBookmark) {
1905 if (mCurrentState.mUrl.equals(url)) {
1906 mCurrentState.mIsBookmarkedSite = isBookmark;
1907 mWebViewController.bookmarkedStatusHasChanged(Tab.this);
1908 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001909 }
John Recke969cc52010-12-21 17:24:43 -08001910 };
Michael Kolb1acef692011-03-08 14:12:06 -08001911
Michael Kolbeb95db42011-03-03 10:38:40 -08001912 public Bitmap getScreenshot() {
John Reck1cf4b792011-07-26 10:22:22 -07001913 synchronized (Tab.this) {
1914 return mCapture;
1915 }
Michael Kolbeb95db42011-03-03 10:38:40 -08001916 }
1917
John Reck541f55a2011-06-07 16:34:43 -07001918 public boolean isSnapshot() {
John Reck541f55a2011-06-07 16:34:43 -07001919 return false;
1920 }
1921
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001922 private static class SaveCallback implements ValueCallback<String> {
1923 boolean onReceiveValueCalled = false;
1924 private String mPath;
John Reck68234a92012-04-19 15:27:12 -07001925
1926 @Override
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001927 public void onReceiveValue(String path) {
1928 this.onReceiveValueCalled = true;
1929 this.mPath = path;
John Reck68234a92012-04-19 15:27:12 -07001930 synchronized (this) {
1931 notifyAll();
John Reck8cc92352011-07-06 17:41:52 -07001932 }
John Reck541f55a2011-06-07 16:34:43 -07001933 }
John Reck68234a92012-04-19 15:27:12 -07001934
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001935 public String getPath() {
1936 return mPath;
1937 }
John Reck68234a92012-04-19 15:27:12 -07001938 }
1939
1940 /**
1941 * Must be called on the UI thread
1942 */
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001943 public ContentValues createSnapshotValues(Bitmap bm) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001944 WebView web = getWebView();
John Reck68234a92012-04-19 15:27:12 -07001945 if (web == null) return null;
John Reckd8c74522011-06-14 08:45:00 -07001946 ContentValues values = new ContentValues();
1947 values.put(Snapshots.TITLE, mCurrentState.mTitle);
1948 values.put(Snapshots.URL, mCurrentState.mUrl);
John Reck68234a92012-04-19 15:27:12 -07001949 values.put(Snapshots.BACKGROUND, web.getPageBackgroundColor());
John Reck8cc92352011-07-06 17:41:52 -07001950 values.put(Snapshots.DATE_CREATED, System.currentTimeMillis());
1951 values.put(Snapshots.FAVICON, compressBitmap(getFavicon()));
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001952 values.put(Snapshots.THUMBNAIL, compressBitmap(bm));
John Reckd8c74522011-06-14 08:45:00 -07001953 return values;
John Reck541f55a2011-06-07 16:34:43 -07001954 }
1955
John Reck68234a92012-04-19 15:27:12 -07001956 /**
1957 * Probably want to call this on a background thread
1958 */
1959 public boolean saveViewState(ContentValues values) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001960 WebView web = getWebView();
John Reck68234a92012-04-19 15:27:12 -07001961 if (web == null) return false;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001962 String filename = UUID.randomUUID().toString();
John Reck68234a92012-04-19 15:27:12 -07001963 SaveCallback callback = new SaveCallback();
John Reck68234a92012-04-19 15:27:12 -07001964 try {
John Reck68234a92012-04-19 15:27:12 -07001965 synchronized (callback) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001966 web.saveViewState(filename, callback);
1967 callback.wait();
John Reck68234a92012-04-19 15:27:12 -07001968 }
John Reck68234a92012-04-19 15:27:12 -07001969 } catch (Exception e) {
1970 Log.w(LOGTAG, "Failed to save view state", e);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001971 String path = callback.getPath();
1972 if (path != null) {
1973 File file = mContext.getFileStreamPath(path);
1974 if (file.exists() && !file.delete()) {
1975 file.deleteOnExit();
1976 }
John Reck68234a92012-04-19 15:27:12 -07001977 }
1978 return false;
1979 }
Tarun Nainani8eb00912014-07-17 12:28:32 -07001980
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001981 String path = callback.getPath();
Tarun Nainani8eb00912014-07-17 12:28:32 -07001982 // could be that saving of file failed
1983 if (path == null) {
1984 return false;
1985 }
1986
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001987 File savedFile = new File(path);
1988 if (!savedFile.exists()) {
1989 return false;
John Reck68234a92012-04-19 15:27:12 -07001990 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001991 values.put(Snapshots.VIEWSTATE_PATH, path.substring(path.lastIndexOf('/') + 1));
1992 values.put(Snapshots.VIEWSTATE_SIZE, savedFile.length());
John Reck68234a92012-04-19 15:27:12 -07001993 return true;
1994 }
1995
John Reck8cc92352011-07-06 17:41:52 -07001996 public byte[] compressBitmap(Bitmap bitmap) {
1997 if (bitmap == null) {
1998 return null;
1999 }
2000 ByteArrayOutputStream stream = new ByteArrayOutputStream();
2001 bitmap.compress(CompressFormat.PNG, 100, stream);
2002 return stream.toByteArray();
2003 }
2004
John Reck26b18322011-06-21 13:08:58 -07002005 public void loadUrl(String url, Map<String, String> headers) {
2006 if (mMainView != null) {
Michael Kolba53c9892011-10-05 13:31:40 -07002007 mPageLoadProgress = INITIAL_PROGRESS;
Michael Kolb14612442011-06-24 13:06:29 -07002008 mCurrentState = new PageState(mContext, false, url, null);
John Reck26b18322011-06-21 13:08:58 -07002009 mMainView.loadUrl(url, headers);
2010 }
2011 }
2012
John Reck38b39652012-06-05 09:22:59 -07002013 public void disableUrlOverridingForLoad() {
2014 mDisableOverrideUrlLoading = true;
2015 }
2016
Pankaj Garg6bedeba2015-06-23 15:47:37 -07002017 private void thumbnailUpdated() {
John Reck8ee633f2011-08-09 16:00:35 -07002018 mHandler.removeMessages(MSG_CAPTURE);
Pankaj Garg6bedeba2015-06-23 15:47:37 -07002019
John Reck8ee633f2011-08-09 16:00:35 -07002020 TabControl tc = mWebViewController.getTabControl();
2021 if (tc != null) {
Pankaj Garg6bedeba2015-06-23 15:47:37 -07002022 OnThumbnailUpdatedListener updateListener = tc.getOnThumbnailUpdatedListener();
John Reck8ee633f2011-08-09 16:00:35 -07002023 if (updateListener != null) {
2024 updateListener.onThumbnailUpdated(this);
2025 }
2026 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07002027 }
2028
Pankaj Garg6bedeba2015-06-23 15:47:37 -07002029 protected void capture() {
2030 if (mMainView == null || mCapture == null || !mMainView.isReady() ||
2031 mMainView.getContentWidth() <= 0 || mMainView.getContentHeight() <= 0 ||
2032 !mFirstVisualPixelPainted || mMainView.isShowingCrashView()) {
2033
2034 initCaptureBitmap();
2035 thumbnailUpdated();
2036 return;
2037 }
2038
2039 mMainView.getContentBitmapAsync((float) mCaptureWidth / mMainView.getWidth(), new Rect(),
2040 new ValueCallback<Bitmap>() {
2041 @Override
2042 public void onReceiveValue(Bitmap bitmap) {
2043 if (mCapture == null) {
2044 initCaptureBitmap();
2045 }
2046
2047 if (bitmap == null) {
2048 thumbnailUpdated();
2049 return;
2050 }
2051
2052 Canvas c = new Canvas(mCapture);
2053 mCapture.eraseColor(Color.WHITE);
2054 c.drawBitmap(bitmap, 0, 0, null);
2055
2056 // manually anti-alias the edges for the tilt
2057 c.drawRect(0, 0, 1, mCapture.getHeight(), sAlphaPaint);
2058 c.drawRect(mCapture.getWidth() - 1, 0, mCapture.getWidth(),
2059 mCapture.getHeight(), sAlphaPaint);
2060 c.drawRect(0, 0, mCapture.getWidth(), 1, sAlphaPaint);
2061 c.drawRect(0, mCapture.getHeight() - 1, mCapture.getWidth(),
2062 mCapture.getHeight(), sAlphaPaint);
2063 c.setBitmap(null);
2064
2065 persistThumbnail();
2066 thumbnailUpdated();
2067 }
2068 }
2069 );
John Reck1cf4b792011-07-26 10:22:22 -07002070 }
2071
Pankaj Garg6bedeba2015-06-23 15:47:37 -07002072 @Override
2073 public void onNewPicture(WebView view, Picture picture) {
Michael Kolb9ef259a2011-07-12 15:33:08 -07002074 }
2075
John Reckef654f12011-07-12 16:42:08 -07002076 public boolean canGoBack() {
2077 return mMainView != null ? mMainView.canGoBack() : false;
2078 }
2079
2080 public boolean canGoForward() {
2081 return mMainView != null ? mMainView.canGoForward() : false;
2082 }
2083
2084 public void goBack() {
2085 if (mMainView != null) {
2086 mMainView.goBack();
2087 }
2088 }
2089
2090 public void goForward() {
2091 if (mMainView != null) {
2092 mMainView.goForward();
2093 }
2094 }
2095
John Reck1cf4b792011-07-26 10:22:22 -07002096 protected void persistThumbnail() {
John Reck4eadc342011-10-31 14:04:10 -07002097 DataController.getInstance(mContext).saveThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07002098 }
2099
2100 protected void deleteThumbnail() {
John Reck4eadc342011-10-31 14:04:10 -07002101 DataController.getInstance(mContext).deleteThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07002102 }
2103
John Reck4eadc342011-10-31 14:04:10 -07002104 void updateCaptureFromBlob(byte[] blob) {
John Reck1cf4b792011-07-26 10:22:22 -07002105 synchronized (Tab.this) {
2106 if (mCapture == null) {
2107 return;
2108 }
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002109 ByteBuffer buffer = ByteBuffer.wrap(blob);
Michael Kolbd837a112011-08-09 14:00:18 -07002110 try {
2111 mCapture.copyPixelsFromBuffer(buffer);
2112 } catch (RuntimeException rex) {
2113 Log.e(LOGTAG, "Load capture has mismatched sizes; buffer: "
2114 + buffer.capacity() + " blob: " + blob.length
2115 + "capture: " + mCapture.getByteCount());
2116 throw rex;
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002117 }
John Reck1cf4b792011-07-26 10:22:22 -07002118 }
2119 }
2120
John Reck52be4782011-08-26 15:37:29 -07002121 @Override
2122 public String toString() {
2123 StringBuilder builder = new StringBuilder(100);
2124 builder.append(mId);
2125 builder.append(") has parent: ");
2126 if (getParent() != null) {
2127 builder.append("true[");
2128 builder.append(getParent().getId());
2129 builder.append("]");
2130 } else {
2131 builder.append("false");
2132 }
2133 builder.append(", incog: ");
2134 builder.append(isPrivateBrowsingEnabled());
2135 if (!isPrivateBrowsingEnabled()) {
2136 builder.append(", title: ");
2137 builder.append(getTitle());
2138 builder.append(", url: ");
2139 builder.append(getUrl());
2140 }
2141 return builder.toString();
2142 }
2143
Steve Block4895b012011-10-03 16:26:46 +01002144 private void handleProceededAfterSslError(SslError error) {
2145 if (error.getUrl().equals(mCurrentState.mUrl)) {
2146 // The security state should currently be SECURITY_STATE_SECURE.
2147 setSecurityState(SecurityState.SECURITY_STATE_BAD_CERTIFICATE);
Steve Block08a6f0c2011-10-06 12:12:53 +01002148 mCurrentState.mSslCertificateError = error;
Steve Block4895b012011-10-03 16:26:46 +01002149 } else if (getSecurityState() == SecurityState.SECURITY_STATE_SECURE) {
Steve Block08a6f0c2011-10-06 12:12:53 +01002150 // The page's main resource is secure and this error is for a
2151 // sub-resource.
Steve Block4895b012011-10-03 16:26:46 +01002152 setSecurityState(SecurityState.SECURITY_STATE_MIXED);
2153 }
2154 }
Ze G Riande2a675c22015-06-03 11:15:24 -07002155
2156 // dertermines if the tab contains a dislled page
2157 public boolean isDistilled() {
2158 if (!BrowserCommandLine.hasSwitch("reader-mode")) {
2159 return false;
2160 }
2161 try {
2162 return DomDistillerUtils.isUrlDistilled(getUrl());
2163 } catch (Exception e) {
2164 return false;
2165 }
2166 }
2167
2168 //determines if the tab contains a distillable page
2169 public boolean isDistillable() {
2170 if (!BrowserCommandLine.hasSwitch("reader-mode")) {
2171 mIsDistillable = false;
2172 return mIsDistillable;
2173 }
2174 final ValueCallback<String> onIsDistillable = new ValueCallback<String>() {
2175 @Override
2176 public void onReceiveValue(String str) {
2177 mIsDistillable = Boolean.parseBoolean(str);
2178 }
2179 };
2180
2181 if (isDistilled()) {
2182 mIsDistillable = true;
2183 return mIsDistillable;
2184 }
2185
2186 try {
2187 DomDistillerUtils.isWebViewDistillable(getWebView(), onIsDistillable);
2188 } catch (Exception e) {
2189 mIsDistillable = false;
2190 }
2191
2192 return mIsDistillable;
2193 }
2194
2195 // Function that sets the mIsDistillable variable
2196 public void setIsDistillable(boolean value) {
2197 if (!BrowserCommandLine.hasSwitch("reader-mode")) {
2198 mIsDistillable = false;
2199 }
2200 mIsDistillable = value;
2201 }
2202
2203 // Function that returns the distilled url of the current url
2204 public String getDistilledUrl() {
2205 if (getUrl() != null) {
2206 return DomDistillerUtils.getDistilledUrl(getUrl());
2207 }
2208 return new String();
2209 }
2210
2211 // function that returns the non-distilled version of the current url
2212 public String getNonDistilledUrl() {
2213 if (getUrl() != null) {
2214 return DomDistillerUtils.getOriginalUrlFromDistilledUrl(getUrl());
2215 }
2216 return new String();
2217 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002218}