blob: 1642568c64dfc5fc6f0355ee2475c0c8241970dc [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) {
Axesh R. Ajmera1f998ae2015-04-21 14:16:41 -0700237 mOriginalUrl = mUrl = Controller.INCOGNITO_URI;
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
Axesh R. Ajmera1f998ae2015-04-21 14:16:41 -0700610
611 // show only incognito content and webview has private
612 // and cannot go back(only supported if explicit from UI )
613 if (view.isPrivateBrowsingEnabled() &&
614 !view.canGoBack() &&
615 url.startsWith(Controller.INCOGNITO_URI) ) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800616 Resources resourceHandle = mContext.getResources();
617 InputStream inStream = resourceHandle.openRawResource(
Bijan Amirzada41242f22014-03-21 12:12:18 -0700618 com.android.browser.R.raw.incognito_mode_start_page);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800619 return new WebResourceResponse("text/html", "utf8", inStream);
620 }
kaiyiz6e5b3e02013-08-19 20:02:01 +0800621 WebResourceResponse res;
622 if (MyNavigationUtil.MY_NAVIGATION.equals(url)) {
623 res = MyNavigationProvider.shouldInterceptRequest(mContext, url);
624 } else {
625 res = HomeProvider.shouldInterceptRequest(mContext, url);
626 }
John Reck438bf462011-01-12 18:11:46 -0800627 return res;
628 }
629
630 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700631 public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
632 if (!mInForeground) {
633 return false;
634 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700635 return mWebViewController.shouldOverrideKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700636 }
637
638 @Override
639 public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700640 if (!mInForeground) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700641 return;
642 }
John Reck997b1b72012-04-19 18:08:25 -0700643 if (!mWebViewController.onUnhandledKeyEvent(event)) {
644 super.onUnhandledKeyEvent(view, event);
645 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700646 }
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700647
648 @Override
649 public void beforeNavigation(WebView view, String url) {
Pankaj Garg21dad562015-07-02 17:17:24 -0700650 mTouchIconUrl = null;
Site Mao61b68212015-07-16 10:56:31 -0700651 if (BaseUi.isUiLowPowerMode()) {
Pankaj Garg18aa0a12015-06-22 11:06:12 -0700652 return;
653 }
654
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700655 if (isPrivateBrowsingEnabled()) {
656 return;
657 }
658
659 if (!mFirstVisualPixelPainted) {
660 return;
661 }
662
Pankaj Garg8ee61462015-07-29 18:17:24 -0700663 if (view.getUrl().equals(url)) {
664 return;
665 }
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -0700666
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700667 final int idx = view.copyBackForwardList().getCurrentIndex();
668 boolean bitmapExists = view.hasSnapshot(idx);
669
670 int progress = 100;
671 Controller controller = (Controller)mWebViewController;
672 UI ui = controller.getUi();
673 if (ui instanceof BaseUi) {
674 BaseUi baseUi = (BaseUi) ui;
675 TitleBar titleBar = baseUi.getTitleBar();
676 progress = titleBar.getProgressView().getProgressPercent();
677 }
678
679 if (bitmapExists && progress < 85) {
680 return;
681 }
682
683 int index = getCaptureIndex(view.getLastCommittedHistoryIndex());
Sagar Dhawan49f85cf2015-07-10 16:54:20 -0700684 view.captureSnapshot(index, null);
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700685 }
686
687 @Override
688 public void onHistoryItemCommit(WebView view, int index) {
Site Mao61b68212015-07-16 10:56:31 -0700689 if (BaseUi.isUiLowPowerMode()) {
Pankaj Garg18aa0a12015-06-22 11:06:12 -0700690 return;
691 }
692
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -0700693 // prevent snapshot tab from commiting any history
694 if (isSnapshot()) {
695 return;
696 }
697
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700698 mTabHistoryUpdateObservable.set(index);
Vivek Sekhar53ef8932015-06-18 16:51:43 -0700699 final int maxIdx = view.copyBackForwardList().getSize();
700 final WebView wv = view;
701 view.getSnapshotIds(new ValueCallback <List<Integer>>() {
702 @Override
703 public void onReceiveValue(List<Integer> ids) {
704 int currentTabIdx = mWebViewController.getTabControl().getCurrentPosition();
705 for (Integer id : ids) {
706 if (getTabIdxFromCaptureIdx(id) == currentTabIdx &&
707 getNavIdxFromCaptureIdx(id) >= maxIdx) {
708 wv.deleteSnapshot(id);
709 }
710 }
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700711 }
Vivek Sekhar53ef8932015-06-18 16:51:43 -0700712 });
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700713 }
Pankaj Garg62bc7912015-04-14 16:08:59 -0700714
715 @Override
716 public void onKeyboardStateChange(boolean popup) {
Vivek Sekhard4de6162015-07-21 15:01:45 -0700717 boolean keyboardWasShowing = isKeyboardShowing();
Sagar Dhawan33551ff2015-07-08 17:24:44 -0700718 mIsKeyboardUp = popup;
Vivek Sekhard4de6162015-07-21 15:01:45 -0700719 Controller controller = (Controller)mWebViewController;
720 BaseUi ui = (BaseUi) controller.getUi();
721 // lock the title bar
722 if (popup)
723 ui.getTitleBar().showTopControls(true);
724 if (keyboardWasShowing && popup)
725 ui.getTitleBar().enableTopControls(true);
Pankaj Garg62bc7912015-04-14 16:08:59 -0700726 if (BrowserSettings.getInstance().useFullscreen()) {
Pankaj Garg62bc7912015-04-14 16:08:59 -0700727 ui.forceDisableFullscreenMode(popup);
728 }
729 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700730 };
731
John Reck1cf4b792011-07-26 10:22:22 -0700732 private void syncCurrentState(WebView view, String url) {
733 // Sync state (in case of stop/timeout)
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800734
735 if (mReceivedError) {
736 mCurrentState.mUrl = url;
737 mCurrentState.mOriginalUrl = url;
738 } else {
739 mCurrentState.mUrl = view.getUrl();
740 mCurrentState.mOriginalUrl = view.getOriginalUrl();
741 mCurrentState.mFavicon = view.getFavicon();
742 }
743
John Reck1cf4b792011-07-26 10:22:22 -0700744 if (mCurrentState.mUrl == null) {
John Reck8bcafc12011-08-29 16:43:02 -0700745 mCurrentState.mUrl = "";
John Reck1cf4b792011-07-26 10:22:22 -0700746 }
John Reck1cf4b792011-07-26 10:22:22 -0700747 mCurrentState.mTitle = view.getTitle();
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800748
749
John Reck1cf4b792011-07-26 10:22:22 -0700750 if (!URLUtil.isHttpsUrl(mCurrentState.mUrl)) {
751 // In case we stop when loading an HTTPS page from an HTTP page
752 // but before a provisional load occurred
Steve Block2466eff2011-10-03 15:33:09 +0100753 mCurrentState.mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
Steve Block08a6f0c2011-10-06 12:12:53 +0100754 mCurrentState.mSslCertificateError = null;
John Reck1cf4b792011-07-26 10:22:22 -0700755 }
John Reck502a3532011-08-16 14:21:46 -0700756 mCurrentState.mIncognito = view.isPrivateBrowsingEnabled();
John Reck1cf4b792011-07-26 10:22:22 -0700757 }
758
Pankaj Garg21dad562015-07-02 17:17:24 -0700759 public String getTouchIconUrl() {
760 return mTouchIconUrl;
761 }
Tarun Nainani8eb00912014-07-17 12:28:32 -0700762
Sagar Dhawan33551ff2015-07-08 17:24:44 -0700763 public boolean isKeyboardShowing() {
764 Controller controller = (Controller)mWebViewController;
765 return (mIsKeyboardUp || controller.getUi().isEditingUrl());
766 }
767
Tarun Nainani8eb00912014-07-17 12:28:32 -0700768 public boolean isTabFullScreen() {
769 return mFullScreen;
770 }
771
Vivek Sekharf96064b2014-07-28 16:32:34 -0700772 protected void setTabFullscreen(boolean fullScreen) {
Tarun Nainani8eb00912014-07-17 12:28:32 -0700773 Controller controller = (Controller)mWebViewController;
Sudheer Koganti24766882014-10-02 10:58:09 -0700774 controller.getUi().showFullscreen(fullScreen);
Tarun Nainani8eb00912014-07-17 12:28:32 -0700775 mFullScreen = fullScreen;
Vivek Sekharf96064b2014-07-28 16:32:34 -0700776 }
777
Sudheer Koganti24766882014-10-02 10:58:09 -0700778 public boolean exitFullscreen() {
779 if (mFullScreen) {
780 Controller controller = (Controller)mWebViewController;
781 controller.getUi().showFullscreen(false);
782 if (getWebView() != null)
783 getWebView().exitFullscreen();
784 mFullScreen = false;
785 return true;
786 }
787 return false;
788 }
789
790
791
792
Grace Kloba22ac16e2009-10-07 18:00:23 -0700793 // -------------------------------------------------------------------------
794 // WebChromeClient implementation for the main WebView
795 // -------------------------------------------------------------------------
796
797 private final WebChromeClient mWebChromeClient = new WebChromeClient() {
798 // Helper method to create a new tab or sub window.
799 private void createWindow(final boolean dialog, final Message msg) {
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700800 this.createWindow(dialog, msg, null, false);
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700801 }
802
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700803 private void createWindow(final boolean dialog, final Message msg, final String url,
804 final boolean opener_suppressed) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700805 WebView.WebViewTransport transport =
806 (WebView.WebViewTransport) msg.obj;
807 if (dialog) {
808 createSubWindow();
Michael Kolb8233fac2010-10-26 16:08:53 -0700809 mWebViewController.attachSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700810 transport.setWebView(mSubView);
811 } else {
Pankaj Garg21dad562015-07-02 17:17:24 -0700812 capture();
813
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700814 final Tab newTab = mWebViewController.openTab(url,
John Reck5949c662011-05-27 09:52:29 -0700815 Tab.this, true, true);
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700816 // This is special case for rendering links on a webpage in
817 // a new tab. If opener is suppressed, the WebContents created
818 // by the content layer are not fully initialized. This check
819 // will prevent content layer from overriding WebContents
820 // created by new tab with the uninitialized instance.
821 if (!opener_suppressed) {
822 transport.setWebView(newTab.getWebView());
823 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700824 }
825 msg.sendToTarget();
826 }
827
828 @Override
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700829 public void toggleFullscreenModeForTab(boolean enterFullscreen) {
830 if (mWebViewController instanceof Controller) {
Vivek Sekharf96064b2014-07-28 16:32:34 -0700831 setTabFullscreen(enterFullscreen);
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700832 }
833 }
834
835 @Override
Tarun Nainani8eb00912014-07-17 12:28:32 -0700836 public void onOffsetsForFullscreenChanged(float topControlsOffsetYPix,
837 float contentOffsetYPix,
838 float overdrawBottomHeightPix) {
839 if (mWebViewController instanceof Controller) {
Vivek Sekhar8ee3abb2014-07-14 12:32:05 -0700840 Controller controller = (Controller)mWebViewController;
Tarun Nainani8eb00912014-07-17 12:28:32 -0700841 controller.getUi().translateTitleBar(topControlsOffsetYPix);
Vivek Sekhar11b40062015-06-24 11:49:04 -0700842 // Resize the viewport if top controls is not visible
Vivek Sekhar2ee19a32015-07-02 17:03:57 -0700843 if (mMainView != null &&
844 (topControlsOffsetYPix == 0.0f || contentOffsetYPix == 0.0f))
Vivek Sekhar11b40062015-06-24 11:49:04 -0700845 ((BrowserWebView)mMainView).enableTopControls(
846 (topControlsOffsetYPix == 0.0f) ? true : false);
Vivek Sekhar8ee3abb2014-07-14 12:32:05 -0700847 }
848 }
849
850 @Override
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700851 public boolean isTabFullScreen() {
Vivek Sekharf96064b2014-07-28 16:32:34 -0700852 return mFullScreen;
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700853 }
854
855 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700856 public boolean onCreateWindow(WebView view, final boolean dialog,
857 final boolean userGesture, final Message resultMsg) {
858 // only allow new window or sub window for the foreground case
859 if (!mInForeground) {
860 return false;
861 }
862 // Short-circuit if we can't create any more tabs or sub windows.
863 if (dialog && mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -0700864 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700865 .setTitle(R.string.too_many_subwindows_dialog_title)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200866 .setIconAttribute(android.R.attr.alertDialogIcon)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700867 .setMessage(R.string.too_many_subwindows_dialog_message)
868 .setPositiveButton(R.string.ok, null)
869 .show();
870 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700871 } else if (!mWebViewController.getTabControl().canCreateNewTab()) {
Michael Kolb14612442011-06-24 13:06:29 -0700872 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700873 .setTitle(R.string.too_many_windows_dialog_title)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200874 .setIconAttribute(android.R.attr.alertDialogIcon)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700875 .setMessage(R.string.too_many_windows_dialog_message)
876 .setPositiveButton(R.string.ok, null)
877 .show();
878 return false;
879 }
880
881 // Short-circuit if this was a user gesture.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800882 if (userGesture || !mSettings.blockPopupWindows()) {
Tarun Nainani4f5137d2015-04-16 17:26:18 -0700883 WebView.WebViewTransport transport =
884 (WebView.WebViewTransport) resultMsg.obj;
885 CreateWindowParams windowParams = transport.getCreateWindowParams();
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700886 if (windowParams.mOpenerSuppressed) {
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700887 createWindow(dialog, resultMsg, windowParams.mURL, true);
888 // This is special case for rendering links on a webpage in
889 // a new tab. If opener is suppressed, the WebContents created
890 // by the content layer are not fully initialized. Returning false
891 // will prevent content layer from overriding WebContents
892 // created by new tab with the uninitialized instance.
893 return false;
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700894 }
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700895
896 createWindow(dialog, resultMsg);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700897 return true;
898 }
899
Tarun Nainani4f5137d2015-04-16 17:26:18 -0700900 createWindow(dialog, resultMsg);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700901 return true;
902 }
903
904 @Override
Patrick Scotteb5061b2009-11-18 15:00:30 -0500905 public void onRequestFocus(WebView view) {
906 if (!mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700907 mWebViewController.switchToTab(Tab.this);
Patrick Scotteb5061b2009-11-18 15:00:30 -0500908 }
909 }
910
911 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700912 public void onCloseWindow(WebView window) {
Michael Kolbc831b632011-05-11 09:30:34 -0700913 if (mParent != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700914 // JavaScript can only close popup window.
915 if (mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700916 mWebViewController.switchToTab(mParent);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700917 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700918 mWebViewController.closeTab(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700919 }
920 }
921
922 @Override
923 public void onProgressChanged(WebView view, int newProgress) {
John Reck30c714c2010-12-16 17:30:34 -0800924 mPageLoadProgress = newProgress;
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800925 if (newProgress == 100) {
926 mInPageLoad = false;
927 }
John Reck30c714c2010-12-16 17:30:34 -0800928 mWebViewController.onProgressChanged(Tab.this);
Michael Kolb72864272012-05-03 15:42:15 -0700929 if (mUpdateThumbnail && newProgress == 100) {
930 mUpdateThumbnail = false;
931 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700932 }
933
934 @Override
Leon Scroggins21d9b902010-03-11 09:33:11 -0500935 public void onReceivedTitle(WebView view, final String title) {
John Reck30c714c2010-12-16 17:30:34 -0800936 mCurrentState.mTitle = title;
Michael Kolb8233fac2010-10-26 16:08:53 -0700937 mWebViewController.onReceivedTitle(Tab.this, title);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700938 }
939
940 @Override
941 public void onReceivedIcon(WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -0800942 mCurrentState.mFavicon = icon;
Michael Kolb8233fac2010-10-26 16:08:53 -0700943 mWebViewController.onFavicon(Tab.this, view, icon);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700944 }
945
946 @Override
947 public void onReceivedTouchIconUrl(WebView view, String url,
948 boolean precomposed) {
Michael Kolb14612442011-06-24 13:06:29 -0700949 final ContentResolver cr = mContext.getContentResolver();
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400950 // Let precomposed icons take precedence over non-composed
951 // icons.
952 if (precomposed && mTouchIconLoader != null) {
953 mTouchIconLoader.cancel(false);
954 mTouchIconLoader = null;
955 }
956 // Have only one async task at a time.
957 if (mTouchIconLoader == null) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700958 mTouchIconLoader = new DownloadTouchIcon(Tab.this,
Michael Kolb14612442011-06-24 13:06:29 -0700959 mContext, cr, view);
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400960 mTouchIconLoader.execute(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700961 }
Pankaj Garg21dad562015-07-02 17:17:24 -0700962 mTouchIconUrl = url;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700963 }
964
965 @Override
966 public void onShowCustomView(View view,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800967 CustomViewCallback callback) {
Michael Kolb14612442011-06-24 13:06:29 -0700968 Activity activity = mWebViewController.getActivity();
969 if (activity != null) {
970 onShowCustomView(view, activity.getRequestedOrientation(), callback);
971 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400972 }
973
974 @Override
975 public void onShowCustomView(View view, int requestedOrientation,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800976 CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700977 if (mInForeground) mWebViewController.showCustomView(Tab.this, view,
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400978 requestedOrientation, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700979 }
980
981 @Override
982 public void onHideCustomView() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700983 if (mInForeground) mWebViewController.hideCustomView();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700984 }
985
986 /**
987 * The origin has exceeded its database quota.
988 * @param url the URL that exceeded the quota
989 * @param databaseIdentifier the identifier of the database on which the
990 * transaction that caused the quota overflow was run
991 * @param currentQuota the current quota for the origin.
992 * @param estimatedSize the estimated size of the database.
993 * @param totalUsedQuota is the sum of all origins' quota.
994 * @param quotaUpdater The callback to run when a decision to allow or
995 * deny quota has been made. Don't forget to call this!
996 */
997 @Override
998 public void onExceededDatabaseQuota(String url,
999 String databaseIdentifier, long currentQuota, long estimatedSize,
1000 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -07001001 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001002 .onExceededDatabaseQuota(url, databaseIdentifier,
1003 currentQuota, estimatedSize, totalUsedQuota,
1004 quotaUpdater);
1005 }
1006
1007 /**
1008 * The Application Cache has exceeded its max size.
1009 * @param spaceNeeded is the amount of disk space that would be needed
1010 * in order for the last appcache operation to succeed.
1011 * @param totalUsedQuota is the sum of all origins' quota.
1012 * @param quotaUpdater A callback to inform the WebCore thread that a
1013 * new app cache size is available. This callback must always
1014 * be executed at some point to ensure that the sleeping
1015 * WebCore thread is woken up.
1016 */
1017 @Override
1018 public void onReachedMaxAppCacheSize(long spaceNeeded,
1019 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -07001020 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001021 .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota,
1022 quotaUpdater);
1023 }
1024
Ben Murdoch65acc352009-11-19 18:16:04 +00001025 /* Adds a JavaScript error message to the system log and if the JS
1026 * console is enabled in the about:debug options, to that console
1027 * also.
Ben Murdochc42addf2010-01-28 15:19:59 +00001028 * @param consoleMessage the message object.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001029 */
1030 @Override
Ben Murdochc42addf2010-01-28 15:19:59 +00001031 public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Jeff Hamilton47654f42010-09-07 09:57:51 -05001032 // Don't log console messages in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001033 if (isPrivateBrowsingEnabled()) return true;
Jeff Hamilton47654f42010-09-07 09:57:51 -05001034
Ben Murdochc42addf2010-01-28 15:19:59 +00001035 String message = "Console: " + consoleMessage.message() + " "
1036 + consoleMessage.sourceId() + ":"
1037 + consoleMessage.lineNumber();
1038
1039 switch (consoleMessage.messageLevel()) {
1040 case TIP:
1041 Log.v(CONSOLE_LOGTAG, message);
1042 break;
1043 case LOG:
1044 Log.i(CONSOLE_LOGTAG, message);
1045 break;
1046 case WARNING:
1047 Log.w(CONSOLE_LOGTAG, message);
1048 break;
1049 case ERROR:
1050 Log.e(CONSOLE_LOGTAG, message);
1051 break;
1052 case DEBUG:
1053 Log.d(CONSOLE_LOGTAG, message);
1054 break;
1055 }
1056
1057 return true;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001058 }
1059
1060 /**
1061 * Ask the browser for an icon to represent a <video> element.
1062 * This icon will be used if the Web page did not specify a poster attribute.
1063 * @return Bitmap The icon or null if no such icon is available.
1064 */
1065 @Override
1066 public Bitmap getDefaultVideoPoster() {
1067 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001068 return mWebViewController.getDefaultVideoPoster();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001069 }
1070 return null;
1071 }
1072
1073 /**
1074 * Ask the host application for a custom progress view to show while
1075 * a <video> is loading.
1076 * @return View The progress view.
1077 */
1078 @Override
1079 public View getVideoLoadingProgressView() {
1080 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001081 return mWebViewController.getVideoLoadingProgressView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001082 }
1083 return null;
1084 }
1085
1086 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00001087 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001088 if (mInForeground) {
Ben Murdoch8cad4132012-01-11 10:56:43 +00001089 mWebViewController.openFileChooser(uploadMsg, acceptType, capture);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001090 } else {
1091 uploadMsg.onReceiveValue(null);
1092 }
1093 }
1094
Vivek Sekharb54614f2014-05-01 19:03:37 -07001095 @Override
1096 public void showFileChooser(ValueCallback<String[]> uploadFilePaths, String acceptTypes,
1097 boolean capture) {
1098 if (mInForeground) {
1099 mWebViewController.showFileChooser(uploadFilePaths, acceptTypes, capture);
1100 } else {
1101 uploadFilePaths.onReceiveValue(null);
1102 }
1103 }
1104
Grace Kloba22ac16e2009-10-07 18:00:23 -07001105 /**
1106 * Deliver a list of already-visited URLs
1107 */
1108 @Override
1109 public void getVisitedHistory(final ValueCallback<String[]> callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001110 mWebViewController.getVisitedHistory(callback);
1111 }
Ben Murdoch8029a772010-11-16 11:58:21 +00001112
1113 @Override
1114 public void setupAutoFill(Message message) {
1115 // Prompt the user to set up their profile.
1116 final Message msg = message;
Michael Kolb14612442011-06-24 13:06:29 -07001117 AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
1118 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
Ben Murdoch1d676b62011-01-17 12:54:24 +00001119 Context.LAYOUT_INFLATER_SERVICE);
1120 final View layout = inflater.inflate(R.layout.setup_autofill_dialog, null);
1121
1122 builder.setView(layout)
1123 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1124 @Override
1125 public void onClick(DialogInterface dialog, int id) {
1126 CheckBox disableAutoFill = (CheckBox) layout.findViewById(
1127 R.id.setup_autofill_dialog_disable_autofill);
1128
1129 if (disableAutoFill.isChecked()) {
1130 // Disable autofill and show a toast with how to turn it on again.
John Reck35e9dd62011-04-25 09:01:54 -07001131 mSettings.setAutofillEnabled(false);
Michael Kolb14612442011-06-24 13:06:29 -07001132 Toast.makeText(mContext,
Ben Murdoch1d676b62011-01-17 12:54:24 +00001133 R.string.autofill_setup_dialog_negative_toast,
1134 Toast.LENGTH_LONG).show();
1135 } else {
1136 // Take user to the AutoFill profile editor. When they return,
1137 // we will send the message that we pass here which will trigger
1138 // the form to get filled out with their new profile.
1139 mWebViewController.setupAutoFill(msg);
1140 }
1141 }
1142 })
1143 .setNegativeButton(R.string.cancel, null)
1144 .show();
Ben Murdoch8029a772010-11-16 11:58:21 +00001145 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001146 };
1147
1148 // -------------------------------------------------------------------------
1149 // WebViewClient implementation for the sub window
1150 // -------------------------------------------------------------------------
1151
1152 // Subclass of WebViewClient used in subwindows to notify the main
1153 // WebViewClient of certain WebView activities.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001154 private static class SubWindowClient extends WebViewClient {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001155 // The main WebViewClient.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001156 private final WebViewClient mClient;
Michael Kolb8233fac2010-10-26 16:08:53 -07001157 private final WebViewController mController;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001158
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001159 SubWindowClient(WebViewClient client, WebViewController controller) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001160 mClient = client;
Michael Kolb8233fac2010-10-26 16:08:53 -07001161 mController = controller;
Leon Scroggins III211ba542010-04-19 13:21:13 -04001162 }
1163 @Override
1164 public void onPageStarted(WebView view, String url, Bitmap favicon) {
1165 // Unlike the others, do not call mClient's version, which would
1166 // change the progress bar. However, we do want to remove the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001167 // find or select dialog.
Michael Kolb8233fac2010-10-26 16:08:53 -07001168 mController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001169 }
1170 @Override
1171 public void doUpdateVisitedHistory(WebView view, String url,
1172 boolean isReload) {
1173 mClient.doUpdateVisitedHistory(view, url, isReload);
1174 }
1175 @Override
1176 public boolean shouldOverrideUrlLoading(WebView view, String url) {
1177 return mClient.shouldOverrideUrlLoading(view, url);
1178 }
1179 @Override
1180 public void onReceivedSslError(WebView view, SslErrorHandler handler,
1181 SslError error) {
1182 mClient.onReceivedSslError(view, handler, error);
1183 }
1184 @Override
Brian Carlstrom8862c1d2011-06-02 01:05:55 -07001185 public void onReceivedClientCertRequest(WebView view,
1186 ClientCertRequestHandler handler, String host_and_port) {
1187 mClient.onReceivedClientCertRequest(view, handler, host_and_port);
1188 }
1189 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -07001190 public void onReceivedHttpAuthRequest(WebView view,
1191 HttpAuthHandler handler, String host, String realm) {
1192 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
1193 }
1194 @Override
1195 public void onFormResubmission(WebView view, Message dontResend,
1196 Message resend) {
1197 mClient.onFormResubmission(view, dontResend, resend);
1198 }
1199 @Override
1200 public void onReceivedError(WebView view, int errorCode,
1201 String description, String failingUrl) {
1202 mClient.onReceivedError(view, errorCode, description, failingUrl);
1203 }
1204 @Override
1205 public boolean shouldOverrideKeyEvent(WebView view,
1206 android.view.KeyEvent event) {
1207 return mClient.shouldOverrideKeyEvent(view, event);
1208 }
1209 @Override
1210 public void onUnhandledKeyEvent(WebView view,
1211 android.view.KeyEvent event) {
1212 mClient.onUnhandledKeyEvent(view, event);
1213 }
1214 }
1215
1216 // -------------------------------------------------------------------------
1217 // WebChromeClient implementation for the sub window
1218 // -------------------------------------------------------------------------
1219
1220 private class SubWindowChromeClient extends WebChromeClient {
1221 // The main WebChromeClient.
1222 private final WebChromeClient mClient;
1223
1224 SubWindowChromeClient(WebChromeClient client) {
1225 mClient = client;
1226 }
1227 @Override
1228 public void onProgressChanged(WebView view, int newProgress) {
1229 mClient.onProgressChanged(view, newProgress);
1230 }
1231 @Override
1232 public boolean onCreateWindow(WebView view, boolean dialog,
1233 boolean userGesture, android.os.Message resultMsg) {
1234 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
1235 }
1236 @Override
1237 public void onCloseWindow(WebView window) {
1238 if (window != mSubView) {
1239 Log.e(LOGTAG, "Can't close the window");
1240 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001241 mWebViewController.dismissSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001242 }
1243 }
1244
1245 // -------------------------------------------------------------------------
1246
1247 // Construct a new tab
Michael Kolb7bcafde2011-05-09 13:55:59 -07001248 Tab(WebViewController wvcontroller, WebView w) {
John Reck1cf4b792011-07-26 10:22:22 -07001249 this(wvcontroller, w, null);
1250 }
1251
1252 Tab(WebViewController wvcontroller, Bundle state) {
1253 this(wvcontroller, null, state);
1254 }
1255
1256 Tab(WebViewController wvcontroller, WebView w, Bundle state) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001257 mWebViewController = wvcontroller;
Michael Kolb14612442011-06-24 13:06:29 -07001258 mContext = mWebViewController.getContext();
John Reck35e9dd62011-04-25 09:01:54 -07001259 mSettings = BrowserSettings.getInstance();
Michael Kolb14612442011-06-24 13:06:29 -07001260 mDataController = DataController.getInstance(mContext);
1261 mCurrentState = new PageState(mContext, w != null
John Reck847b5322011-04-14 17:02:18 -07001262 ? w.isPrivateBrowsingEnabled() : false);
Tarun Nainani8084c822014-06-25 13:38:06 -07001263 setTimeStamp();
Michael Kolb8233fac2010-10-26 16:08:53 -07001264 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001265 mInForeground = false;
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001266 mWebViewDestroyedByMemoryMonitor = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001267
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001268 mDownloadListener = new BrowserDownloadListener() {
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001269 public void onDownloadStart(String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001270 String contentDisposition, String mimetype, String referer,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001271 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001272 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001273 mimetype, referer, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001274 }
1275 };
1276
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001277 mCaptureWidth = mContext.getResources().getDimensionPixelSize(R.dimen.tab_thumbnail_width);
1278 mCaptureHeight =mContext.getResources().getDimensionPixelSize(R.dimen.tab_thumbnail_height);
1279
1280 initCaptureBitmap();
1281
John Reck1cf4b792011-07-26 10:22:22 -07001282 restoreState(state);
John Reck52be4782011-08-26 15:37:29 -07001283 if (getId() == -1) {
1284 mId = TabControl.getNextId();
1285 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001286 setWebView(w);
Michael Kolb9ef259a2011-07-12 15:33:08 -07001287 mHandler = new Handler() {
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001288 @Override
Michael Kolb9ef259a2011-07-12 15:33:08 -07001289 public void handleMessage(Message m) {
John Reck1cf4b792011-07-26 10:22:22 -07001290 switch (m.what) {
1291 case MSG_CAPTURE:
1292 capture();
1293 break;
1294 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001295 }
1296 };
Pankaj Garg1c13cab2015-05-12 11:52:17 -07001297
1298 mFirstPixelObservable = new Observable();
1299 mFirstPixelObservable.set(false);
1300 mTabHistoryUpdateObservable = new Observable();
John Reck1cf4b792011-07-26 10:22:22 -07001301 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001302
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001303 private void initCaptureBitmap() {
1304 mCapture = Bitmap.createBitmap(mCaptureWidth, mCaptureHeight, Bitmap.Config.RGB_565);
1305 mCapture.eraseColor(Color.WHITE);
Michael Kolb72864272012-05-03 15:42:15 -07001306 }
1307
Mathew Inwoode09305e2011-09-02 12:03:26 +01001308 /**
1309 * This is used to get a new ID when the tab has been preloaded, before it is displayed and
1310 * added to TabControl. Preloaded tabs can be created before restoreInstanceState, leading
1311 * to overlapping IDs between the preloaded and restored tabs.
1312 */
1313 public void refreshIdAfterPreload() {
1314 mId = TabControl.getNextId();
1315 }
1316
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001317 public void setController(WebViewController ctl) {
1318 mWebViewController = ctl;
1319
John Reck1cf4b792011-07-26 10:22:22 -07001320 if (mWebViewController.shouldCaptureThumbnails()) {
1321 synchronized (Tab.this) {
1322 if (mCapture == null) {
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001323 initCaptureBitmap();
1324 if (mInForeground && !mHandler.hasMessages(MSG_CAPTURE)) {
1325 mHandler.sendEmptyMessageDelayed(MSG_CAPTURE, CAPTURE_DELAY);
John Reck1cf4b792011-07-26 10:22:22 -07001326 }
1327 }
1328 }
1329 } else {
1330 synchronized (Tab.this) {
1331 mCapture = null;
1332 deleteThumbnail();
1333 }
1334 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001335 }
1336
Michael Kolbc831b632011-05-11 09:30:34 -07001337 public long getId() {
1338 return mId;
1339 }
1340
Michael Kolb91911a22012-01-17 11:21:25 -08001341 void setWebView(WebView w) {
1342 setWebView(w, true);
1343 }
1344
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001345 public boolean isNativeActive(){
1346 if (mMainView == null)
1347 return false;
1348 return true;
1349 }
1350
1351 public void setTimeStamp(){
1352 Date d = new Date();
1353 timestamp = (new Timestamp(d.getTime()));
1354 }
1355
1356 public Timestamp getTimestamp() {
1357 return timestamp;
1358 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001359 /**
1360 * Sets the WebView for this tab, correctly removing the old WebView from
1361 * the container view.
1362 */
Michael Kolb91911a22012-01-17 11:21:25 -08001363 void setWebView(WebView w, boolean restore) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001364 if (mMainView == w) {
1365 return;
1366 }
Michael Kolba713ec82010-11-29 17:27:06 -08001367
Michael Kolba713ec82010-11-29 17:27:06 -08001368 mWebViewController.onSetWebView(this, w);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001369
John Reck1cf4b792011-07-26 10:22:22 -07001370 if (mMainView != null) {
John Reck8ee633f2011-08-09 16:00:35 -07001371 mMainView.setPictureListener(null);
John Reck1cf4b792011-07-26 10:22:22 -07001372 if (w != null) {
1373 syncCurrentState(w, null);
1374 } else {
Panos Thomasa9a5a582014-03-18 19:20:08 -07001375 mCurrentState = new PageState(mContext, mMainView.isPrivateBrowsingEnabled());
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001376
1377 if (mWebViewDestroyedByMemoryMonitor) {
1378 /*
1379 * If tab was destroyed as a result of the MemoryMonitor
1380 * then we need to restore the state properties
1381 * from the old WebView (mMainView)
1382 */
1383 syncCurrentState(mMainView, null);
1384 mWebViewDestroyedByMemoryMonitor = false;
1385 }
John Reck1cf4b792011-07-26 10:22:22 -07001386 }
1387 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001388 // set the new one
1389 mMainView = w;
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001390
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001391 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001392 if (mMainView != null) {
1393 mMainView.setWebViewClient(mWebViewClient);
1394 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001395 // Attach DownloadManager so that downloads can start in an active
1396 // or a non-active window. This can happen when going to a site that
1397 // does a redirect after a period of time. The user could have
1398 // switched to another tab while waiting for the download to start.
1399 mMainView.setDownloadListener(mDownloadListener);
John Reck8ee633f2011-08-09 16:00:35 -07001400 TabControl tc = mWebViewController.getTabControl();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001401 if (tc != null /*&& tc.getOnThumbnailUpdatedListener() != null*/) {
John Reck8ee633f2011-08-09 16:00:35 -07001402 mMainView.setPictureListener(this);
1403 }
Michael Kolb91911a22012-01-17 11:21:25 -08001404 if (restore && (mSavedState != null)) {
John Reck8b9bb8b2012-03-08 13:19:40 -08001405 restoreUserAgent();
John Reck6c2e2f32011-08-22 13:41:23 -07001406 WebBackForwardList restoredState
1407 = mMainView.restoreState(mSavedState);
1408 if (restoredState == null || restoredState.getSize() == 0) {
1409 Log.w(LOGTAG, "Failed to restore WebView state!");
1410 loadUrl(mCurrentState.mOriginalUrl, null);
1411 }
John Reck1cf4b792011-07-26 10:22:22 -07001412 mSavedState = null;
1413 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001414 }
1415 }
1416
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001417 public void destroyThroughMemoryMonitor() {
1418 mWebViewDestroyedByMemoryMonitor = true;
1419 destroy();
1420 }
1421
Grace Kloba22ac16e2009-10-07 18:00:23 -07001422 /**
1423 * Destroy the tab's main WebView and subWindow if any
1424 */
1425 void destroy() {
Tarun Nainani2c1dd7c2014-07-05 16:40:12 -07001426
Matthew Huib7f2e9c2014-04-16 11:12:37 -04001427 if (mPostponeDestroy) {
1428 mShouldDestroy = true;
1429 return;
1430 }
1431 mShouldDestroy = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001432 if (mMainView != null) {
1433 dismissSubWindow();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001434 // save the WebView to call destroy() after detach it from the tab
Vivek Sekhar53ef8932015-06-18 16:51:43 -07001435 final WebView webView = mMainView;
Vivek Sekhar2ee19a32015-07-02 17:03:57 -07001436 setWebView(null);
Site Mao61b68212015-07-16 10:56:31 -07001437 if (!mWebViewDestroyedByMemoryMonitor && !BaseUi.isUiLowPowerMode()) {
Vivek Sekhar2ee19a32015-07-02 17:03:57 -07001438 // Tabs can be reused with new instance of WebView so delete the snapshots
Vivek Sekhar53ef8932015-06-18 16:51:43 -07001439 webView.getSnapshotIds(new ValueCallback<List<Integer>>() {
1440 @Override
1441 public void onReceiveValue(List<Integer> ids) {
1442 int currentTabIdx = mWebViewController.getTabControl().getCurrentPosition();
1443 for (Integer id : ids) {
1444 if (getTabIdxFromCaptureIdx(id) == currentTabIdx) {
1445 webView.deleteSnapshot(id);
1446 }
1447 }
Vivek Sekhar53ef8932015-06-18 16:51:43 -07001448 webView.destroy();
Pankaj Garg1c13cab2015-05-12 11:52:17 -07001449 }
Vivek Sekhar53ef8932015-06-18 16:51:43 -07001450 });
Vivek Sekhar2ee19a32015-07-02 17:03:57 -07001451 } else {
1452 webView.destroy();
Pankaj Garg1c13cab2015-05-12 11:52:17 -07001453 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001454 }
1455 }
1456
Matthew Huib7f2e9c2014-04-16 11:12:37 -04001457 private boolean mPostponeDestroy = false;
1458 private boolean mShouldDestroy = false;
1459
1460 public void postponeDestroy() {
1461 mPostponeDestroy = true;
1462 }
1463
1464 public void performPostponedDestroy() {
1465 mPostponeDestroy = false;
1466 if (mShouldDestroy) {
1467 destroy();
1468 }
1469 }
1470
Grace Kloba22ac16e2009-10-07 18:00:23 -07001471 /**
1472 * Remove the tab from the parent
1473 */
1474 void removeFromTree() {
1475 // detach the children
Michael Kolbc831b632011-05-11 09:30:34 -07001476 if (mChildren != null) {
1477 for(Tab t : mChildren) {
1478 t.setParent(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001479 }
1480 }
1481 // remove itself from the parent list
Michael Kolbc831b632011-05-11 09:30:34 -07001482 if (mParent != null) {
1483 mParent.mChildren.remove(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001484 }
Tarun Nainani8eb00912014-07-17 12:28:32 -07001485
1486 mCapture = null;
John Reck1cf4b792011-07-26 10:22:22 -07001487 deleteThumbnail();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001488 }
1489
1490 /**
1491 * Create a new subwindow unless a subwindow already exists.
1492 * @return True if a new subwindow was created. False if one already exists.
1493 */
1494 boolean createSubWindow() {
1495 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001496 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001497 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001498 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001499 mSubView.setWebChromeClient(new SubWindowChromeClient(
1500 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001501 // Set a different DownloadListener for the mSubView, since it will
1502 // just need to dismiss the mSubView, rather than close the Tab
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001503 mSubView.setDownloadListener(new BrowserDownloadListener() {
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001504 public void onDownloadStart(String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001505 String contentDisposition, String mimetype, String referer,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001506 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001507 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001508 contentDisposition, mimetype, referer, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001509 if (mSubView.copyBackForwardList().getSize() == 0) {
1510 // This subwindow was opened for the sole purpose of
1511 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001512 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001513 }
1514 }
1515 });
Michael Kolb14612442011-06-24 13:06:29 -07001516 mSubView.setOnCreateContextMenuListener(mWebViewController.getActivity());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001517 return true;
1518 }
1519 return false;
1520 }
1521
1522 /**
1523 * Dismiss the subWindow for the tab.
1524 */
1525 void dismissSubWindow() {
1526 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001527 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001528 mSubView.destroy();
1529 mSubView = null;
1530 mSubViewContainer = null;
1531 }
1532 }
1533
Grace Kloba22ac16e2009-10-07 18:00:23 -07001534
1535 /**
1536 * Set the parent tab of this tab.
1537 */
Michael Kolbc831b632011-05-11 09:30:34 -07001538 void setParent(Tab parent) {
John Reck52be4782011-08-26 15:37:29 -07001539 if (parent == this) {
1540 throw new IllegalStateException("Cannot set parent to self!");
1541 }
Michael Kolbc831b632011-05-11 09:30:34 -07001542 mParent = parent;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001543 // This tab may have been freed due to low memory. If that is the case,
Michael Kolbc831b632011-05-11 09:30:34 -07001544 // the parent tab id is already saved. If we are changing that id
Grace Kloba22ac16e2009-10-07 18:00:23 -07001545 // (most likely due to removing the parent tab) we must update the
Michael Kolbc831b632011-05-11 09:30:34 -07001546 // parent tab id in the saved Bundle.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001547 if (mSavedState != null) {
1548 if (parent == null) {
1549 mSavedState.remove(PARENTTAB);
1550 } else {
Michael Kolbc831b632011-05-11 09:30:34 -07001551 mSavedState.putLong(PARENTTAB, parent.getId());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001552 }
1553 }
John Reckb0a86db2011-05-24 14:05:58 -07001554
1555 // Sync the WebView useragent with the parent
1556 if (parent != null && mSettings.hasDesktopUseragent(parent.getWebView())
1557 != mSettings.hasDesktopUseragent(getWebView())) {
1558 mSettings.toggleDesktopUseragent(getWebView());
1559 }
John Reck52be4782011-08-26 15:37:29 -07001560
1561 if (parent != null && parent.getId() == getId()) {
1562 throw new IllegalStateException("Parent has same ID as child!");
1563 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001564 }
1565
1566 /**
Michael Kolbc831b632011-05-11 09:30:34 -07001567 * If this Tab was created through another Tab, then this method returns
1568 * that Tab.
1569 * @return the Tab parent or null
1570 */
1571 public Tab getParent() {
1572 return mParent;
1573 }
1574
1575 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001576 * When a Tab is created through the content of another Tab, then we
1577 * associate the Tabs.
1578 * @param child the Tab that was created from this Tab
1579 */
1580 void addChildTab(Tab child) {
Michael Kolbc831b632011-05-11 09:30:34 -07001581 if (mChildren == null) {
1582 mChildren = new Vector<Tab>();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001583 }
Michael Kolbc831b632011-05-11 09:30:34 -07001584 mChildren.add(child);
1585 child.setParent(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001586 }
1587
Michael Kolbc831b632011-05-11 09:30:34 -07001588 Vector<Tab> getChildren() {
1589 return mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001590 }
1591
1592 void resume() {
1593 if (mMainView != null) {
John Reck56c1fcf2011-08-17 10:15:16 -07001594 setupHwAcceleration(mMainView);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001595 mMainView.onResume();
1596 if (mSubView != null) {
1597 mSubView.onResume();
1598 }
1599 }
1600 }
1601
John Reck56c1fcf2011-08-17 10:15:16 -07001602 private void setupHwAcceleration(View web) {
1603 if (web == null) return;
1604 BrowserSettings settings = BrowserSettings.getInstance();
1605 if (settings.isHardwareAccelerated()) {
1606 web.setLayerType(View.LAYER_TYPE_NONE, null);
1607 } else {
1608 web.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
1609 }
1610 }
1611
Grace Kloba22ac16e2009-10-07 18:00:23 -07001612 void pause() {
1613 if (mMainView != null) {
1614 mMainView.onPause();
1615 if (mSubView != null) {
1616 mSubView.onPause();
1617 }
1618 }
1619 }
1620
1621 void putInForeground() {
John Reck8ee633f2011-08-09 16:00:35 -07001622 if (mInForeground) {
1623 return;
1624 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001625 mInForeground = true;
1626 resume();
Michael Kolb14612442011-06-24 13:06:29 -07001627 Activity activity = mWebViewController.getActivity();
1628 mMainView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001629 if (mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -07001630 mSubView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001631 }
Axesh R. Ajmerac6b5c322015-05-01 11:06:10 -07001632
Leon Scroggins1961ed22010-12-07 15:22:21 -05001633 mWebViewController.bookmarkedStatusHasChanged(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001634 }
1635
1636 void putInBackground() {
John Reck8ee633f2011-08-09 16:00:35 -07001637 if (!mInForeground) {
1638 return;
1639 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001640 mInForeground = false;
1641 pause();
1642 mMainView.setOnCreateContextMenuListener(null);
1643 if (mSubView != null) {
1644 mSubView.setOnCreateContextMenuListener(null);
1645 }
1646 }
1647
Michael Kolb8233fac2010-10-26 16:08:53 -07001648 boolean inForeground() {
1649 return mInForeground;
1650 }
1651
Grace Kloba22ac16e2009-10-07 18:00:23 -07001652 /**
1653 * Return the top window of this tab; either the subwindow if it is not
1654 * null or the main window.
1655 * @return The top window of this tab.
1656 */
1657 WebView getTopWindow() {
1658 if (mSubView != null) {
1659 return mSubView;
1660 }
1661 return mMainView;
1662 }
1663
1664 /**
1665 * Return the main window of this tab. Note: if a tab is freed in the
1666 * background, this can return null. It is only guaranteed to be
1667 * non-null for the current tab.
1668 * @return The main WebView of this tab.
1669 */
1670 WebView getWebView() {
1671 return mMainView;
1672 }
1673
Michael Kolba713ec82010-11-29 17:27:06 -08001674 void setViewContainer(View container) {
1675 mContainer = container;
1676 }
1677
Michael Kolb8233fac2010-10-26 16:08:53 -07001678 View getViewContainer() {
1679 return mContainer;
1680 }
1681
Grace Kloba22ac16e2009-10-07 18:00:23 -07001682 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001683 * Return whether private browsing is enabled for the main window of
1684 * this tab.
1685 * @return True if private browsing is enabled.
1686 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001687 boolean isPrivateBrowsingEnabled() {
John Reck502a3532011-08-16 14:21:46 -07001688 return mCurrentState.mIncognito;
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001689 }
1690
1691 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001692 * Return the subwindow of this tab or null if there is no subwindow.
1693 * @return The subwindow of this tab or null.
1694 */
1695 WebView getSubWebView() {
1696 return mSubView;
1697 }
1698
Michael Kolb1514bb72010-11-22 09:11:48 -08001699 void setSubWebView(WebView subView) {
1700 mSubView = subView;
1701 }
1702
Michael Kolb8233fac2010-10-26 16:08:53 -07001703 View getSubViewContainer() {
1704 return mSubViewContainer;
1705 }
1706
Michael Kolb1514bb72010-11-22 09:11:48 -08001707 void setSubViewContainer(View subViewContainer) {
1708 mSubViewContainer = subViewContainer;
1709 }
1710
Grace Kloba22ac16e2009-10-07 18:00:23 -07001711
1712 /**
1713 * @return The application id string
1714 */
1715 String getAppId() {
1716 return mAppId;
1717 }
1718
1719 /**
1720 * Set the application id string
1721 * @param id
1722 */
1723 void setAppId(String id) {
1724 mAppId = id;
1725 }
1726
Michael Kolbe28b3472011-08-04 16:54:31 -07001727 boolean closeOnBack() {
1728 return mCloseOnBack;
1729 }
1730
1731 void setCloseOnBack(boolean close) {
1732 mCloseOnBack = close;
1733 }
1734
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001735 boolean getDerivedFromIntent() {
1736 return mDerivedFromIntent;
1737 }
1738
1739 void setDerivedFromIntent(boolean derived) {
1740 mDerivedFromIntent = derived;
1741 }
1742
Grace Kloba22ac16e2009-10-07 18:00:23 -07001743 String getUrl() {
John Reck324d4402011-01-11 16:56:42 -08001744 return UrlUtils.filteredUrl(mCurrentState.mUrl);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001745 }
1746
Tarun Nainani8eb00912014-07-17 12:28:32 -07001747
1748 protected void onPageFinished() {
1749 mPageFinished = true;
Ze G Riande2a675c22015-06-03 11:15:24 -07001750 isDistillable();
Tarun Nainani8eb00912014-07-17 12:28:32 -07001751 }
1752
1753 public boolean getPageFinishedStatus() {
1754 return mPageFinished;
1755 }
1756
John Reck49a603c2011-03-03 09:33:05 -08001757 String getOriginalUrl() {
John Reckdb22ec42011-06-29 11:31:24 -07001758 if (mCurrentState.mOriginalUrl == null) {
1759 return getUrl();
John Reck49a603c2011-03-03 09:33:05 -08001760 }
John Reckdb22ec42011-06-29 11:31:24 -07001761 return UrlUtils.filteredUrl(mCurrentState.mOriginalUrl);
John Reck49a603c2011-03-03 09:33:05 -08001762 }
1763
Grace Kloba22ac16e2009-10-07 18:00:23 -07001764 /**
John Reck30c714c2010-12-16 17:30:34 -08001765 * Get the title of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001766 */
1767 String getTitle() {
John Reck30c714c2010-12-16 17:30:34 -08001768 return mCurrentState.mTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001769 }
1770
1771 /**
John Reck30c714c2010-12-16 17:30:34 -08001772 * Get the favicon of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001773 */
1774 Bitmap getFavicon() {
John Reck1cf4b792011-07-26 10:22:22 -07001775 if (mCurrentState.mFavicon != null) {
1776 return mCurrentState.mFavicon;
1777 }
1778 return getDefaultFavicon(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001779 }
1780
Pankaj Garg32e1b942015-06-03 18:13:24 -07001781 public boolean hasFavicon() {
1782 return mCurrentState.mFavicon != null;
1783 }
1784
John Recke969cc52010-12-21 17:24:43 -08001785 public boolean isBookmarkedSite() {
1786 return mCurrentState.mIsBookmarkedSite;
1787 }
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001788
Grace Kloba22ac16e2009-10-07 18:00:23 -07001789 /**
Steve Block08a6f0c2011-10-06 12:12:53 +01001790 * Sets the security state, clears the SSL certificate error and informs
1791 * the controller.
1792 */
Steve Block2466eff2011-10-03 15:33:09 +01001793 private void setSecurityState(SecurityState securityState) {
1794 mCurrentState.mSecurityState = securityState;
Steve Block08a6f0c2011-10-06 12:12:53 +01001795 mCurrentState.mSslCertificateError = null;
Steve Block2466eff2011-10-03 15:33:09 +01001796 mWebViewController.onUpdatedSecurityState(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001797 }
1798
1799 /**
Steve Block2466eff2011-10-03 15:33:09 +01001800 * @return The tab's security state.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001801 */
Steve Block2466eff2011-10-03 15:33:09 +01001802 SecurityState getSecurityState() {
1803 return mCurrentState.mSecurityState;
John Reck30c714c2010-12-16 17:30:34 -08001804 }
1805
Steve Block08a6f0c2011-10-06 12:12:53 +01001806 /**
1807 * Gets the SSL certificate error, if any, for the page's main resource.
1808 * This is only non-null when the security state is
1809 * SECURITY_STATE_BAD_CERTIFICATE.
1810 */
1811 SslError getSslCertificateError() {
1812 return mCurrentState.mSslCertificateError;
1813 }
1814
John Reck30c714c2010-12-16 17:30:34 -08001815 int getLoadProgress() {
1816 if (mInPageLoad) {
1817 return mPageLoadProgress;
1818 }
1819 return 100;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001820 }
1821
1822 /**
1823 * @return TRUE if onPageStarted is called while onPageFinished is not
1824 * called yet.
1825 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001826 boolean inPageLoad() {
1827 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001828 }
1829
Grace Kloba22ac16e2009-10-07 18:00:23 -07001830 /**
John Reck1cf4b792011-07-26 10:22:22 -07001831 * @return The Bundle with the tab's state if it can be saved, otherwise null
Grace Kloba22ac16e2009-10-07 18:00:23 -07001832 */
John Reck1cf4b792011-07-26 10:22:22 -07001833 public Bundle saveState() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001834 // If the WebView is null it means we ran low on memory and we already
1835 // stored the saved state in mSavedState.
1836 if (mMainView == null) {
John Reck1cf4b792011-07-26 10:22:22 -07001837 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001838 }
John Reck6c2e2f32011-08-22 13:41:23 -07001839
1840 if (TextUtils.isEmpty(mCurrentState.mUrl)) {
John Reck1cf4b792011-07-26 10:22:22 -07001841 return null;
John Reck24f18262011-06-17 14:47:20 -07001842 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001843
1844 mSavedState = new Bundle();
John Reck6c2e2f32011-08-22 13:41:23 -07001845 WebBackForwardList savedList = mMainView.saveState(mSavedState);
1846 if (savedList == null || savedList.getSize() == 0) {
1847 Log.w(LOGTAG, "Failed to save back/forward list for "
1848 + mCurrentState.mUrl);
1849 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001850
Michael Kolbc831b632011-05-11 09:30:34 -07001851 mSavedState.putLong(ID, mId);
John Reck30c714c2010-12-16 17:30:34 -08001852 mSavedState.putString(CURRURL, mCurrentState.mUrl);
1853 mSavedState.putString(CURRTITLE, mCurrentState.mTitle);
John Reck1cf4b792011-07-26 10:22:22 -07001854 mSavedState.putBoolean(INCOGNITO, mMainView.isPrivateBrowsingEnabled());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001855 if (mAppId != null) {
1856 mSavedState.putString(APPID, mAppId);
1857 }
Michael Kolbe28b3472011-08-04 16:54:31 -07001858 mSavedState.putBoolean(CLOSEFLAG, mCloseOnBack);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001859 // Remember the parent tab so the relationship can be restored.
Michael Kolbc831b632011-05-11 09:30:34 -07001860 if (mParent != null) {
1861 mSavedState.putLong(PARENTTAB, mParent.mId);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001862 }
John Reckb0a86db2011-05-24 14:05:58 -07001863 mSavedState.putBoolean(USERAGENT,
1864 mSettings.hasDesktopUseragent(getWebView()));
John Reck1cf4b792011-07-26 10:22:22 -07001865 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001866 }
1867
1868 /*
1869 * Restore the state of the tab.
1870 */
John Reck1cf4b792011-07-26 10:22:22 -07001871 private void restoreState(Bundle b) {
1872 mSavedState = b;
1873 if (mSavedState == null) {
1874 return;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001875 }
1876 // Restore the internal state even if the WebView fails to restore.
1877 // This will maintain the app id, original url and close-on-exit values.
Michael Kolbc831b632011-05-11 09:30:34 -07001878 mId = b.getLong(ID);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001879 mAppId = b.getString(APPID);
Michael Kolbe28b3472011-08-04 16:54:31 -07001880 mCloseOnBack = b.getBoolean(CLOSEFLAG);
John Reck8b9bb8b2012-03-08 13:19:40 -08001881 restoreUserAgent();
John Reck1cf4b792011-07-26 10:22:22 -07001882 String url = b.getString(CURRURL);
1883 String title = b.getString(CURRTITLE);
1884 boolean incognito = b.getBoolean(INCOGNITO);
1885 mCurrentState = new PageState(mContext, incognito, url, null);
1886 mCurrentState.mTitle = title;
1887 synchronized (Tab.this) {
1888 if (mCapture != null) {
John Reck4eadc342011-10-31 14:04:10 -07001889 DataController.getInstance(mContext).loadThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07001890 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001891 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001892 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04001893
John Reck8b9bb8b2012-03-08 13:19:40 -08001894 private void restoreUserAgent() {
1895 if (mMainView == null || mSavedState == null) {
1896 return;
1897 }
1898 if (mSavedState.getBoolean(USERAGENT)
1899 != mSettings.hasDesktopUseragent(mMainView)) {
1900 mSettings.toggleDesktopUseragent(mMainView);
1901 }
1902 }
1903
Leon Scroggins1961ed22010-12-07 15:22:21 -05001904 public void updateBookmarkedStatus() {
John Recke969cc52010-12-21 17:24:43 -08001905 mDataController.queryBookmarkStatus(getUrl(), mIsBookmarkCallback);
Leon Scroggins1961ed22010-12-07 15:22:21 -05001906 }
1907
John Recke969cc52010-12-21 17:24:43 -08001908 private DataController.OnQueryUrlIsBookmark mIsBookmarkCallback
1909 = new DataController.OnQueryUrlIsBookmark() {
1910 @Override
1911 public void onQueryUrlIsBookmark(String url, boolean isBookmark) {
1912 if (mCurrentState.mUrl.equals(url)) {
1913 mCurrentState.mIsBookmarkedSite = isBookmark;
1914 mWebViewController.bookmarkedStatusHasChanged(Tab.this);
1915 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001916 }
John Recke969cc52010-12-21 17:24:43 -08001917 };
Michael Kolb1acef692011-03-08 14:12:06 -08001918
Michael Kolbeb95db42011-03-03 10:38:40 -08001919 public Bitmap getScreenshot() {
John Reck1cf4b792011-07-26 10:22:22 -07001920 synchronized (Tab.this) {
1921 return mCapture;
1922 }
Michael Kolbeb95db42011-03-03 10:38:40 -08001923 }
1924
John Reck541f55a2011-06-07 16:34:43 -07001925 public boolean isSnapshot() {
John Reck541f55a2011-06-07 16:34:43 -07001926 return false;
1927 }
1928
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001929 private static class SaveCallback implements ValueCallback<String> {
1930 boolean onReceiveValueCalled = false;
1931 private String mPath;
John Reck68234a92012-04-19 15:27:12 -07001932
1933 @Override
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001934 public void onReceiveValue(String path) {
1935 this.onReceiveValueCalled = true;
1936 this.mPath = path;
John Reck68234a92012-04-19 15:27:12 -07001937 synchronized (this) {
1938 notifyAll();
John Reck8cc92352011-07-06 17:41:52 -07001939 }
John Reck541f55a2011-06-07 16:34:43 -07001940 }
John Reck68234a92012-04-19 15:27:12 -07001941
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001942 public String getPath() {
1943 return mPath;
1944 }
John Reck68234a92012-04-19 15:27:12 -07001945 }
1946
1947 /**
1948 * Must be called on the UI thread
1949 */
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001950 public ContentValues createSnapshotValues(Bitmap bm) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001951 WebView web = getWebView();
John Reck68234a92012-04-19 15:27:12 -07001952 if (web == null) return null;
John Reckd8c74522011-06-14 08:45:00 -07001953 ContentValues values = new ContentValues();
1954 values.put(Snapshots.TITLE, mCurrentState.mTitle);
1955 values.put(Snapshots.URL, mCurrentState.mUrl);
John Reck68234a92012-04-19 15:27:12 -07001956 values.put(Snapshots.BACKGROUND, web.getPageBackgroundColor());
John Reck8cc92352011-07-06 17:41:52 -07001957 values.put(Snapshots.DATE_CREATED, System.currentTimeMillis());
1958 values.put(Snapshots.FAVICON, compressBitmap(getFavicon()));
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001959 values.put(Snapshots.THUMBNAIL, compressBitmap(bm));
John Reckd8c74522011-06-14 08:45:00 -07001960 return values;
John Reck541f55a2011-06-07 16:34:43 -07001961 }
1962
John Reck68234a92012-04-19 15:27:12 -07001963 /**
1964 * Probably want to call this on a background thread
1965 */
1966 public boolean saveViewState(ContentValues values) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001967 WebView web = getWebView();
John Reck68234a92012-04-19 15:27:12 -07001968 if (web == null) return false;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001969 String filename = UUID.randomUUID().toString();
John Reck68234a92012-04-19 15:27:12 -07001970 SaveCallback callback = new SaveCallback();
John Reck68234a92012-04-19 15:27:12 -07001971 try {
John Reck68234a92012-04-19 15:27:12 -07001972 synchronized (callback) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001973 web.saveViewState(filename, callback);
1974 callback.wait();
John Reck68234a92012-04-19 15:27:12 -07001975 }
John Reck68234a92012-04-19 15:27:12 -07001976 } catch (Exception e) {
1977 Log.w(LOGTAG, "Failed to save view state", e);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001978 String path = callback.getPath();
1979 if (path != null) {
1980 File file = mContext.getFileStreamPath(path);
1981 if (file.exists() && !file.delete()) {
1982 file.deleteOnExit();
1983 }
John Reck68234a92012-04-19 15:27:12 -07001984 }
1985 return false;
1986 }
Tarun Nainani8eb00912014-07-17 12:28:32 -07001987
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001988 String path = callback.getPath();
Tarun Nainani8eb00912014-07-17 12:28:32 -07001989 // could be that saving of file failed
1990 if (path == null) {
1991 return false;
1992 }
1993
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001994 File savedFile = new File(path);
1995 if (!savedFile.exists()) {
1996 return false;
John Reck68234a92012-04-19 15:27:12 -07001997 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001998 values.put(Snapshots.VIEWSTATE_PATH, path.substring(path.lastIndexOf('/') + 1));
1999 values.put(Snapshots.VIEWSTATE_SIZE, savedFile.length());
John Reck68234a92012-04-19 15:27:12 -07002000 return true;
2001 }
2002
John Reck8cc92352011-07-06 17:41:52 -07002003 public byte[] compressBitmap(Bitmap bitmap) {
2004 if (bitmap == null) {
2005 return null;
2006 }
2007 ByteArrayOutputStream stream = new ByteArrayOutputStream();
2008 bitmap.compress(CompressFormat.PNG, 100, stream);
2009 return stream.toByteArray();
2010 }
2011
John Reck26b18322011-06-21 13:08:58 -07002012 public void loadUrl(String url, Map<String, String> headers) {
2013 if (mMainView != null) {
Michael Kolba53c9892011-10-05 13:31:40 -07002014 mPageLoadProgress = INITIAL_PROGRESS;
Michael Kolb14612442011-06-24 13:06:29 -07002015 mCurrentState = new PageState(mContext, false, url, null);
John Reck26b18322011-06-21 13:08:58 -07002016 mMainView.loadUrl(url, headers);
2017 }
2018 }
2019
John Reck38b39652012-06-05 09:22:59 -07002020 public void disableUrlOverridingForLoad() {
2021 mDisableOverrideUrlLoading = true;
2022 }
2023
Pankaj Garg6bedeba2015-06-23 15:47:37 -07002024 private void thumbnailUpdated() {
John Reck8ee633f2011-08-09 16:00:35 -07002025 mHandler.removeMessages(MSG_CAPTURE);
Pankaj Garg6bedeba2015-06-23 15:47:37 -07002026
John Reck8ee633f2011-08-09 16:00:35 -07002027 TabControl tc = mWebViewController.getTabControl();
2028 if (tc != null) {
Pankaj Garg6bedeba2015-06-23 15:47:37 -07002029 OnThumbnailUpdatedListener updateListener = tc.getOnThumbnailUpdatedListener();
John Reck8ee633f2011-08-09 16:00:35 -07002030 if (updateListener != null) {
2031 updateListener.onThumbnailUpdated(this);
2032 }
2033 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07002034 }
2035
Pankaj Garg6bedeba2015-06-23 15:47:37 -07002036 protected void capture() {
2037 if (mMainView == null || mCapture == null || !mMainView.isReady() ||
2038 mMainView.getContentWidth() <= 0 || mMainView.getContentHeight() <= 0 ||
2039 !mFirstVisualPixelPainted || mMainView.isShowingCrashView()) {
2040
2041 initCaptureBitmap();
2042 thumbnailUpdated();
2043 return;
2044 }
2045
2046 mMainView.getContentBitmapAsync((float) mCaptureWidth / mMainView.getWidth(), new Rect(),
2047 new ValueCallback<Bitmap>() {
2048 @Override
2049 public void onReceiveValue(Bitmap bitmap) {
2050 if (mCapture == null) {
2051 initCaptureBitmap();
2052 }
2053
2054 if (bitmap == null) {
2055 thumbnailUpdated();
2056 return;
2057 }
2058
2059 Canvas c = new Canvas(mCapture);
2060 mCapture.eraseColor(Color.WHITE);
2061 c.drawBitmap(bitmap, 0, 0, null);
2062
2063 // manually anti-alias the edges for the tilt
2064 c.drawRect(0, 0, 1, mCapture.getHeight(), sAlphaPaint);
2065 c.drawRect(mCapture.getWidth() - 1, 0, mCapture.getWidth(),
2066 mCapture.getHeight(), sAlphaPaint);
2067 c.drawRect(0, 0, mCapture.getWidth(), 1, sAlphaPaint);
2068 c.drawRect(0, mCapture.getHeight() - 1, mCapture.getWidth(),
2069 mCapture.getHeight(), sAlphaPaint);
2070 c.setBitmap(null);
2071
2072 persistThumbnail();
2073 thumbnailUpdated();
2074 }
2075 }
2076 );
John Reck1cf4b792011-07-26 10:22:22 -07002077 }
2078
Pankaj Garg6bedeba2015-06-23 15:47:37 -07002079 @Override
2080 public void onNewPicture(WebView view, Picture picture) {
Michael Kolb9ef259a2011-07-12 15:33:08 -07002081 }
2082
John Reckef654f12011-07-12 16:42:08 -07002083 public boolean canGoBack() {
2084 return mMainView != null ? mMainView.canGoBack() : false;
2085 }
2086
2087 public boolean canGoForward() {
2088 return mMainView != null ? mMainView.canGoForward() : false;
2089 }
2090
2091 public void goBack() {
2092 if (mMainView != null) {
2093 mMainView.goBack();
2094 }
2095 }
2096
2097 public void goForward() {
2098 if (mMainView != null) {
2099 mMainView.goForward();
2100 }
2101 }
2102
John Reck1cf4b792011-07-26 10:22:22 -07002103 protected void persistThumbnail() {
John Reck4eadc342011-10-31 14:04:10 -07002104 DataController.getInstance(mContext).saveThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07002105 }
2106
2107 protected void deleteThumbnail() {
John Reck4eadc342011-10-31 14:04:10 -07002108 DataController.getInstance(mContext).deleteThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07002109 }
2110
John Reck4eadc342011-10-31 14:04:10 -07002111 void updateCaptureFromBlob(byte[] blob) {
John Reck1cf4b792011-07-26 10:22:22 -07002112 synchronized (Tab.this) {
2113 if (mCapture == null) {
2114 return;
2115 }
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002116 ByteBuffer buffer = ByteBuffer.wrap(blob);
Michael Kolbd837a112011-08-09 14:00:18 -07002117 try {
2118 mCapture.copyPixelsFromBuffer(buffer);
2119 } catch (RuntimeException rex) {
2120 Log.e(LOGTAG, "Load capture has mismatched sizes; buffer: "
2121 + buffer.capacity() + " blob: " + blob.length
2122 + "capture: " + mCapture.getByteCount());
2123 throw rex;
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002124 }
John Reck1cf4b792011-07-26 10:22:22 -07002125 }
2126 }
2127
John Reck52be4782011-08-26 15:37:29 -07002128 @Override
2129 public String toString() {
2130 StringBuilder builder = new StringBuilder(100);
2131 builder.append(mId);
2132 builder.append(") has parent: ");
2133 if (getParent() != null) {
2134 builder.append("true[");
2135 builder.append(getParent().getId());
2136 builder.append("]");
2137 } else {
2138 builder.append("false");
2139 }
2140 builder.append(", incog: ");
2141 builder.append(isPrivateBrowsingEnabled());
2142 if (!isPrivateBrowsingEnabled()) {
2143 builder.append(", title: ");
2144 builder.append(getTitle());
2145 builder.append(", url: ");
2146 builder.append(getUrl());
2147 }
2148 return builder.toString();
2149 }
2150
Steve Block4895b012011-10-03 16:26:46 +01002151 private void handleProceededAfterSslError(SslError error) {
2152 if (error.getUrl().equals(mCurrentState.mUrl)) {
2153 // The security state should currently be SECURITY_STATE_SECURE.
2154 setSecurityState(SecurityState.SECURITY_STATE_BAD_CERTIFICATE);
Steve Block08a6f0c2011-10-06 12:12:53 +01002155 mCurrentState.mSslCertificateError = error;
Steve Block4895b012011-10-03 16:26:46 +01002156 } else if (getSecurityState() == SecurityState.SECURITY_STATE_SECURE) {
Steve Block08a6f0c2011-10-06 12:12:53 +01002157 // The page's main resource is secure and this error is for a
2158 // sub-resource.
Steve Block4895b012011-10-03 16:26:46 +01002159 setSecurityState(SecurityState.SECURITY_STATE_MIXED);
2160 }
2161 }
Ze G Riande2a675c22015-06-03 11:15:24 -07002162
2163 // dertermines if the tab contains a dislled page
2164 public boolean isDistilled() {
2165 if (!BrowserCommandLine.hasSwitch("reader-mode")) {
2166 return false;
2167 }
2168 try {
2169 return DomDistillerUtils.isUrlDistilled(getUrl());
2170 } catch (Exception e) {
2171 return false;
2172 }
2173 }
2174
2175 //determines if the tab contains a distillable page
2176 public boolean isDistillable() {
2177 if (!BrowserCommandLine.hasSwitch("reader-mode")) {
2178 mIsDistillable = false;
2179 return mIsDistillable;
2180 }
2181 final ValueCallback<String> onIsDistillable = new ValueCallback<String>() {
2182 @Override
2183 public void onReceiveValue(String str) {
2184 mIsDistillable = Boolean.parseBoolean(str);
2185 }
2186 };
2187
2188 if (isDistilled()) {
2189 mIsDistillable = true;
2190 return mIsDistillable;
2191 }
2192
2193 try {
2194 DomDistillerUtils.isWebViewDistillable(getWebView(), onIsDistillable);
2195 } catch (Exception e) {
2196 mIsDistillable = false;
2197 }
2198
2199 return mIsDistillable;
2200 }
2201
2202 // Function that sets the mIsDistillable variable
2203 public void setIsDistillable(boolean value) {
2204 if (!BrowserCommandLine.hasSwitch("reader-mode")) {
2205 mIsDistillable = false;
2206 }
2207 mIsDistillable = value;
2208 }
2209
2210 // Function that returns the distilled url of the current url
2211 public String getDistilledUrl() {
2212 if (getUrl() != null) {
2213 return DomDistillerUtils.getDistilledUrl(getUrl());
2214 }
2215 return new String();
2216 }
2217
2218 // function that returns the non-distilled version of the current url
2219 public String getNonDistilledUrl() {
2220 if (getUrl() != null) {
2221 return DomDistillerUtils.getOriginalUrlFromDistilledUrl(getUrl());
2222 }
2223 return new String();
2224 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002225}