blob: 8121ba4dddb0e809ffc703d725478184023497bb [file] [log] [blame]
Grace Kloba22ac16e2009-10-07 18:00:23 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Bijan Amirzada41242f22014-03-21 12:12:18 -070017package com.android.browser;
Grace Kloba22ac16e2009-10-07 18:00:23 -070018
Michael Kolb8233fac2010-10-26 16:08:53 -070019import android.app.Activity;
Grace Kloba22ac16e2009-10-07 18:00:23 -070020import android.app.AlertDialog;
21import android.content.ContentResolver;
John Reckd8c74522011-06-14 08:45:00 -070022import android.content.ContentValues;
John Reck30c714c2010-12-16 17:30:34 -080023import android.content.Context;
Grace Kloba22ac16e2009-10-07 18:00:23 -070024import android.content.DialogInterface;
Michael Kolbfe251992010-07-08 15:41:55 -070025import android.content.DialogInterface.OnCancelListener;
Pankaj Garg1c13cab2015-05-12 11:52:17 -070026import android.content.res.Configuration;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080027import android.content.res.Resources;
Grace Kloba22ac16e2009-10-07 18:00:23 -070028import android.graphics.Bitmap;
John Reck8cc92352011-07-06 17:41:52 -070029import android.graphics.Bitmap.CompressFormat;
Michael Kolb9ef259a2011-07-12 15:33:08 -070030import android.graphics.BitmapFactory;
31import android.graphics.Canvas;
Michael Kolbc3af0672011-08-09 10:24:41 -070032import android.graphics.Color;
Michael Kolba3194d02011-09-07 11:23:51 -070033import android.graphics.Paint;
Michael Kolb9ef259a2011-07-12 15:33:08 -070034import android.graphics.Picture;
Michael Kolba3194d02011-09-07 11:23:51 -070035import android.graphics.PorterDuff;
36import android.graphics.PorterDuffXfermode;
Tarun Nainaniea28dde2014-08-27 17:25:09 -070037import android.graphics.Rect;
Grace Kloba22ac16e2009-10-07 18:00:23 -070038import android.net.Uri;
Grace Kloba22ac16e2009-10-07 18:00:23 -070039import android.os.Bundle;
Michael Kolb9ef259a2011-07-12 15:33:08 -070040import android.os.Handler;
Grace Kloba22ac16e2009-10-07 18:00:23 -070041import android.os.Message;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +000042import android.os.SystemClock;
Brian Carlstrom8862c1d2011-06-02 01:05:55 -070043import android.security.KeyChain;
Brian Carlstromaa09cd82011-06-09 16:04:40 -070044import android.security.KeyChainAliasCallback;
John Reck24f18262011-06-17 14:47:20 -070045import android.text.TextUtils;
Grace Kloba22ac16e2009-10-07 18:00:23 -070046import android.util.Log;
47import android.view.KeyEvent;
48import android.view.LayoutInflater;
49import android.view.View;
Ben Murdochc42addf2010-01-28 15:19:59 +000050import android.webkit.ConsoleMessage;
Grace Kloba22ac16e2009-10-07 18:00:23 -070051import android.webkit.URLUtil;
John Reck438bf462011-01-12 18:11:46 -080052import android.webkit.WebResourceResponse;
Grace Kloba22ac16e2009-10-07 18:00:23 -070053import android.webkit.WebStorage;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080054import android.webkit.WebChromeClient.CustomViewCallback;
55import android.webkit.ValueCallback;
Ben Murdoch1d676b62011-01-17 12:54:24 +000056import android.widget.CheckBox;
Ben Murdoch8029a772010-11-16 11:58:21 +000057import android.widget.Toast;
Grace Kloba22ac16e2009-10-07 18:00:23 -070058
Bijan Amirzada41242f22014-03-21 12:12:18 -070059import com.android.browser.TabControl.OnThumbnailUpdatedListener;
60import com.android.browser.homepages.HomeProvider;
61import com.android.browser.mynavigation.MyNavigationUtil;
62import com.android.browser.provider.MyNavigationProvider;
63import com.android.browser.provider.SnapshotProvider.Snapshots;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080064
Pankaj Garg18aa0a12015-06-22 11:06:12 -070065import org.codeaurora.swe.BrowserCommandLine;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080066import org.codeaurora.swe.BrowserDownloadListener;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080067import org.codeaurora.swe.HttpAuthHandler;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080068import org.codeaurora.swe.WebBackForwardList;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080069import org.codeaurora.swe.WebChromeClient;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080070import org.codeaurora.swe.WebView;
71import org.codeaurora.swe.WebView.PictureListener;
Pankaj Garg1c7380d2014-08-27 14:17:12 -070072import org.codeaurora.swe.WebView.CreateWindowParams;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080073import org.codeaurora.swe.WebViewClient;
Pankaj Garg1c13cab2015-05-12 11:52:17 -070074import org.codeaurora.swe.util.Observable;
Ze G Riande2a675c22015-06-03 11:15:24 -070075import org.codeaurora.swe.DomDistillerUtils;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080076
John Reck541f55a2011-06-07 16:34:43 -070077import java.io.ByteArrayOutputStream;
John Reck2b71d6d2012-04-18 17:42:06 -070078import java.io.File;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080079import java.io.InputStream;
John Reck1cf4b792011-07-26 10:22:22 -070080import java.nio.ByteBuffer;
Michael Kolbfe251992010-07-08 15:41:55 -070081import java.util.Map;
John Reck2b71d6d2012-04-18 17:42:06 -070082import java.util.UUID;
Michael Kolbfe251992010-07-08 15:41:55 -070083import java.util.Vector;
Vivek Sekhar53ef8932015-06-18 16:51:43 -070084import java.util.List;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080085import java.sql.Timestamp;
86import java.util.Date;
Michael Kolbfe251992010-07-08 15:41:55 -070087
Grace Kloba22ac16e2009-10-07 18:00:23 -070088/**
89 * Class for maintaining Tabs with a main WebView and a subwindow.
90 */
Michael Kolb9ef259a2011-07-12 15:33:08 -070091class Tab implements PictureListener {
Michael Kolb8233fac2010-10-26 16:08:53 -070092
Grace Kloba22ac16e2009-10-07 18:00:23 -070093 // Log Tag
94 private static final String LOGTAG = "Tab";
Bijan Amirzada41242f22014-03-21 12:12:18 -070095 private static final boolean LOGD_ENABLED = com.android.browser.Browser.LOGD_ENABLED;
Ben Murdochc42addf2010-01-28 15:19:59 +000096 // Special case the logtag for messages for the Console to make it easier to
97 // filter them and match the logtag used for these messages in older versions
98 // of the browser.
99 private static final String CONSOLE_LOGTAG = "browser";
100
Michael Kolb9ef259a2011-07-12 15:33:08 -0700101 private static final int MSG_CAPTURE = 42;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800102 private static final int CAPTURE_DELAY = 1000;
Michael Kolba53c9892011-10-05 13:31:40 -0700103 private static final int INITIAL_PROGRESS = 5;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700104
John Reck1cf4b792011-07-26 10:22:22 -0700105 private static Bitmap sDefaultFavicon;
Sagar Dhawan33551ff2015-07-08 17:24:44 -0700106 private boolean mIsKeyboardUp = false;
John Reck1cf4b792011-07-26 10:22:22 -0700107
Michael Kolba3194d02011-09-07 11:23:51 -0700108 private static Paint sAlphaPaint = new Paint();
109 static {
110 sAlphaPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
111 sAlphaPaint.setColor(Color.TRANSPARENT);
112 }
113
Steve Block2466eff2011-10-03 15:33:09 +0100114 public enum SecurityState {
Steve Block4895b012011-10-03 16:26:46 +0100115 // The page's main resource does not use SSL. Note that we use this
116 // state irrespective of the SSL authentication state of sub-resources.
Steve Block2466eff2011-10-03 15:33:09 +0100117 SECURITY_STATE_NOT_SECURE,
Steve Block4895b012011-10-03 16:26:46 +0100118 // The page's main resource uses SSL and the certificate is good. The
119 // same is true of all sub-resources.
Steve Block2466eff2011-10-03 15:33:09 +0100120 SECURITY_STATE_SECURE,
Steve Block4895b012011-10-03 16:26:46 +0100121 // The page's main resource uses SSL and the certificate is good, but
122 // some sub-resources either do not use SSL or have problems with their
123 // certificates.
Steve Block2466eff2011-10-03 15:33:09 +0100124 SECURITY_STATE_MIXED,
Steve Block4895b012011-10-03 16:26:46 +0100125 // The page's main resource uses SSL but there is a problem with its
126 // certificate.
127 SECURITY_STATE_BAD_CERTIFICATE,
John Reck30c714c2010-12-16 17:30:34 -0800128 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700129
Michael Kolb14612442011-06-24 13:06:29 -0700130 Context mContext;
John Reckd8c74522011-06-14 08:45:00 -0700131 protected WebViewController mWebViewController;
Michael Kolb8233fac2010-10-26 16:08:53 -0700132
Michael Kolbc831b632011-05-11 09:30:34 -0700133 // The tab ID
John Reckd8c74522011-06-14 08:45:00 -0700134 private long mId = -1;
Michael Kolbc831b632011-05-11 09:30:34 -0700135
Grace Kloba22ac16e2009-10-07 18:00:23 -0700136 // Main WebView wrapper
Michael Kolba713ec82010-11-29 17:27:06 -0800137 private View mContainer;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700138 // Main WebView
139 private WebView mMainView;
140 // Subwindow container
141 private View mSubViewContainer;
142 // Subwindow WebView
143 private WebView mSubView;
144 // Saved bundle for when we are running low on memory. It contains the
145 // information needed to restore the WebView if the user goes back to the
146 // tab.
147 private Bundle mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700148 // Parent Tab. This is the Tab that created this Tab, or null if the Tab was
149 // created by the UI
Michael Kolbc831b632011-05-11 09:30:34 -0700150 private Tab mParent;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700151 // Tab that constructed by this Tab. This is used when this Tab is
152 // destroyed, it clears all mParentTab values in the children.
Michael Kolbc831b632011-05-11 09:30:34 -0700153 private Vector<Tab> mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700154 // If true, the tab is in the foreground of the current activity.
155 private boolean mInForeground;
Michael Kolb8233fac2010-10-26 16:08:53 -0700156 // If true, the tab is in page loading state (after onPageStarted,
157 // before onPageFinsihed)
158 private boolean mInPageLoad;
Tarun Nainani8eb00912014-07-17 12:28:32 -0700159 private boolean mPageFinished;
John Reck38b39652012-06-05 09:22:59 -0700160 private boolean mDisableOverrideUrlLoading;
Pankaj Garg79878492015-04-01 14:48:21 -0700161 private boolean mFirstVisualPixelPainted = false;
John Reck30c714c2010-12-16 17:30:34 -0800162 // The last reported progress of the current page
163 private int mPageLoadProgress;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000164 // The time the load started, used to find load page time
165 private long mLoadStartTime;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700166 // Application identifier used to find tabs that another application wants
167 // to reuse.
168 private String mAppId;
Michael Kolbe28b3472011-08-04 16:54:31 -0700169 // flag to indicate if tab should be closed on back
170 private boolean mCloseOnBack;
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700171 // flag to indicate if the tab was opened from an intent
172 private boolean mDerivedFromIntent = false;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -0500173 // The listener that gets invoked when a download is started from the
174 // mMainView
Selim Gurun0b3d66f2012-08-29 13:08:13 -0700175 private final BrowserDownloadListener mDownloadListener;
John Recke969cc52010-12-21 17:24:43 -0800176 private DataController mDataController;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700177
178 // AsyncTask for downloading touch icons
179 DownloadTouchIcon mTouchIconLoader;
180
John Reck35e9dd62011-04-25 09:01:54 -0700181 private BrowserSettings mSettings;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700182 private int mCaptureWidth;
183 private int mCaptureHeight;
184 private Bitmap mCapture;
185 private Handler mHandler;
Michael Kolb72864272012-05-03 15:42:15 -0700186 private boolean mUpdateThumbnail;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800187 private Timestamp timestamp;
Vivek Sekharf96064b2014-07-28 16:32:34 -0700188 private boolean mFullScreen = false;
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800189 private boolean mReceivedError;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700190
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -0700191 // determine if webview is destroyed to MemoryMonitor
192 private boolean mWebViewDestroyedByMemoryMonitor;
193
Pankaj Garg21dad562015-07-02 17:17:24 -0700194 private String mTouchIconUrl;
195
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700196 private Observable mFirstPixelObservable;
197 private Observable mTabHistoryUpdateObservable;
198
199 Observable getFirstPixelObservable() {
200 return mFirstPixelObservable;
201 }
202
203 Observable getTabHistoryUpdateObservable() {
204 return mTabHistoryUpdateObservable;
205 }
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -0700206
Ze G Riande2a675c22015-06-03 11:15:24 -0700207 // dertermines if the tab contains a disllable page
208 private boolean mIsDistillable = false;
Mathew Inwood1dd8e822011-08-03 14:34:29 +0100209
John Reck1cf4b792011-07-26 10:22:22 -0700210 private static synchronized Bitmap getDefaultFavicon(Context context) {
211 if (sDefaultFavicon == null) {
212 sDefaultFavicon = BitmapFactory.decodeResource(
Enrico Rosd6efa972014-12-02 19:49:59 -0800213 context.getResources(), R.drawable.ic_deco_favicon_normal);
John Reck1cf4b792011-07-26 10:22:22 -0700214 }
215 return sDefaultFavicon;
216 }
Michael Kolbeb95db42011-03-03 10:38:40 -0800217
John Reck30c714c2010-12-16 17:30:34 -0800218 // All the state needed for a page
John Reckd8c74522011-06-14 08:45:00 -0700219 protected static class PageState {
John Reck30c714c2010-12-16 17:30:34 -0800220 String mUrl;
John Reckdb22ec42011-06-29 11:31:24 -0700221 String mOriginalUrl;
John Reck30c714c2010-12-16 17:30:34 -0800222 String mTitle;
Steve Block08a6f0c2011-10-06 12:12:53 +0100223 // This is non-null only when mSecurityState is SECURITY_STATE_BAD_CERTIFICATE.
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -0700224 SecurityState mSecurityState;
225 // This is non-null only when onReceivedIcon is called or SnapshotTab restores it.
John Reck30c714c2010-12-16 17:30:34 -0800226 Bitmap mFavicon;
Steve Block08a6f0c2011-10-06 12:12:53 +0100227 boolean mIsBookmarkedSite;
228 boolean mIncognito;
John Reck30c714c2010-12-16 17:30:34 -0800229
230 PageState(Context c, boolean incognito) {
John Reck502a3532011-08-16 14:21:46 -0700231 mIncognito = incognito;
Axesh R. Ajmerad04cad12015-08-12 12:02:44 -0700232 mOriginalUrl = mUrl = "";
John Reck502a3532011-08-16 14:21:46 -0700233 if (mIncognito) {
John Reck30c714c2010-12-16 17:30:34 -0800234 mTitle = c.getString(R.string.new_incognito_tab);
John Reck30c714c2010-12-16 17:30:34 -0800235 } else {
John Reck30c714c2010-12-16 17:30:34 -0800236 mTitle = c.getString(R.string.new_tab);
John Reck30c714c2010-12-16 17:30:34 -0800237 }
Steve Block2466eff2011-10-03 15:33:09 +0100238 mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
John Reck30c714c2010-12-16 17:30:34 -0800239 }
240
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -0700241 PageState(Context c, boolean incognito, String url) {
John Reck502a3532011-08-16 14:21:46 -0700242 mIncognito = incognito;
Axesh R. Ajmerad04cad12015-08-12 12:02:44 -0700243 if (mIncognito)
244 mOriginalUrl = mUrl = "";
245 else
246 mOriginalUrl = mUrl = url;
Pankaj Garg96d0ccd2015-07-30 16:49:47 -0700247 mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
John Reck30c714c2010-12-16 17:30:34 -0800248 }
John Reck1cf4b792011-07-26 10:22:22 -0700249
Grace Kloba22ac16e2009-10-07 18:00:23 -0700250 }
251
John Reck30c714c2010-12-16 17:30:34 -0800252 // The current/loading page's state
John Reckd8c74522011-06-14 08:45:00 -0700253 protected PageState mCurrentState;
John Reck30c714c2010-12-16 17:30:34 -0800254
Grace Kloba22ac16e2009-10-07 18:00:23 -0700255 // Used for saving and restoring each Tab
Michael Kolbc831b632011-05-11 09:30:34 -0700256 static final String ID = "ID";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700257 static final String CURRURL = "currentUrl";
258 static final String CURRTITLE = "currentTitle";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700259 static final String PARENTTAB = "parentTab";
260 static final String APPID = "appid";
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700261 static final String INCOGNITO = "privateBrowsingEnabled";
John Reckb0a86db2011-05-24 14:05:58 -0700262 static final String USERAGENT = "useragent";
Michael Kolbe28b3472011-08-04 16:54:31 -0700263 static final String CLOSEFLAG = "closeOnBack";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700264
Pankaj Garg18186a92015-03-31 14:59:33 -0700265 public void setNetworkAvailable(boolean networkUp) {
266 if (networkUp && mReceivedError && (mMainView != null)) {
267 mMainView.reload();
268 }
269 }
270
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700271 public boolean isFirstVisualPixelPainted() {
272 return mFirstVisualPixelPainted;
273 }
274
275 public int getCaptureIndex(int navIndex) {
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700276 int orientation = mWebViewController.getActivity().
277 getResources().getConfiguration().orientation;
278
279 int orientationBit = (orientation == Configuration.ORIENTATION_LANDSCAPE) ? 0 : 1;
280
Vivek Sekhard0f60402015-06-05 14:07:11 -0700281 int index = orientationBit << 31 | (((int)mId & 0x7f) << 24) | (navIndex & 0xffffff);
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700282 return index;
283 }
284
285 public int getTabIdxFromCaptureIdx(int index) {
286 return (index & 0x7f000000) >> 24;
287 }
288
289 public int getOrientationFromCaptureIdx(int index) {
290 return ((index & 0x80000000) == 0) ? Configuration.ORIENTATION_LANDSCAPE :
291 Configuration.ORIENTATION_PORTRAIT;
292
293 }
294
295 public int getNavIdxFromCaptureIdx(int index) {
296 return (index & 0xffffff);
297 }
298
Pankaj Garg96d0ccd2015-07-30 16:49:47 -0700299 public static SecurityState getWebViewSecurityState(WebView view) {
300 switch (view.getSecurityLevel()) {
301 case WebView.SecurityLevel.EV_SECURE:
302 case WebView.SecurityLevel.SECURE:
303 return SecurityState.SECURITY_STATE_SECURE;
304 case WebView.SecurityLevel.SECURITY_ERROR:
305 return SecurityState.SECURITY_STATE_BAD_CERTIFICATE;
306 case WebView.SecurityLevel.SECURITY_POLICY_WARNING:
307 case WebView.SecurityLevel.SECURITY_WARNING:
308 return SecurityState.SECURITY_STATE_MIXED;
309 }
310 return SecurityState.SECURITY_STATE_NOT_SECURE;
311 }
312
Grace Kloba22ac16e2009-10-07 18:00:23 -0700313 // -------------------------------------------------------------------------
314 // WebViewClient implementation for the main WebView
315 // -------------------------------------------------------------------------
316
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800317 private final WebViewClient mWebViewClient = new WebViewClient() {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500318 private Message mDontResend;
319 private Message mResend;
Michael Kolb47bd1e42011-09-01 15:25:00 -0700320
321 private boolean providersDiffer(String url, String otherUrl) {
322 Uri uri1 = Uri.parse(url);
323 Uri uri2 = Uri.parse(otherUrl);
324 return !uri1.getEncodedAuthority().equals(uri2.getEncodedAuthority());
325 }
326
Grace Kloba22ac16e2009-10-07 18:00:23 -0700327 @Override
328 public void onPageStarted(WebView view, String url, Bitmap favicon) {
Ze G Riande2a675c22015-06-03 11:15:24 -0700329 setIsDistillable(false);
Michael Kolb8233fac2010-10-26 16:08:53 -0700330 mInPageLoad = true;
Tarun Nainani8eb00912014-07-17 12:28:32 -0700331 mPageFinished = false;
Pankaj Garg79878492015-04-01 14:48:21 -0700332 mFirstVisualPixelPainted = false;
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700333 mFirstPixelObservable.set(false);
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800334 mReceivedError = false;
Michael Kolb72864272012-05-03 15:42:15 -0700335 mUpdateThumbnail = true;
Michael Kolba53c9892011-10-05 13:31:40 -0700336 mPageLoadProgress = INITIAL_PROGRESS;
Michael Kolb14612442011-06-24 13:06:29 -0700337 mCurrentState = new PageState(mContext,
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -0700338 view.isPrivateBrowsingEnabled(), url);
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000339 mLoadStartTime = SystemClock.uptimeMillis();
Pankaj Garg62bc7912015-04-14 16:08:59 -0700340 // Need re-enable FullScreenMode on Page navigation if needed
341 if (BrowserSettings.getInstance().useFullscreen()){
342 Controller controller = (Controller) mWebViewController;
343 BaseUi ui = (BaseUi) controller.getUi();
344 ui.forceDisableFullscreenMode(false);
345 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700346 // If we start a touch icon load and then load a new page, we don't
347 // want to cancel the current touch icon loader. But, we do want to
348 // create a new one when the touch icon url is known.
349 if (mTouchIconLoader != null) {
350 mTouchIconLoader.mTab = null;
351 mTouchIconLoader = null;
352 }
353
Grace Kloba22ac16e2009-10-07 18:00:23 -0700354 // finally update the UI in the activity if it is in the foreground
John Reck324d4402011-01-11 16:56:42 -0800355 mWebViewController.onPageStarted(Tab.this, view, favicon);
Leon Scroggins4cd97792010-12-03 15:31:56 -0500356
John Recke969cc52010-12-21 17:24:43 -0800357 updateBookmarkedStatus();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700358 }
359
360 @Override
361 public void onPageFinished(WebView view, String url) {
John Reck38b39652012-06-05 09:22:59 -0700362 mDisableOverrideUrlLoading = false;
John Reck5b691842010-11-29 11:21:13 -0800363 if (!isPrivateBrowsingEnabled()) {
364 LogTag.logPageFinishedLoading(
365 url, SystemClock.uptimeMillis() - mLoadStartTime);
366 }
John Reck1cf4b792011-07-26 10:22:22 -0700367 syncCurrentState(view, url);
John Reck324d4402011-01-11 16:56:42 -0800368 mWebViewController.onPageFinished(Tab.this);
Pankaj Garg96d0ccd2015-07-30 16:49:47 -0700369 setSecurityState(getWebViewSecurityState(view));
Grace Kloba22ac16e2009-10-07 18:00:23 -0700370 }
371
Pankaj Garg79878492015-04-01 14:48:21 -0700372 @Override
373 public void onFirstVisualPixel(WebView view) {
374 mFirstVisualPixelPainted = true;
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700375 mFirstPixelObservable.set(true);
Pankaj Garg79878492015-04-01 14:48:21 -0700376 }
377
Grace Kloba22ac16e2009-10-07 18:00:23 -0700378 // return true if want to hijack the url to let another app to handle it
379 @Override
380 public boolean shouldOverrideUrlLoading(WebView view, String url) {
John Reck38b39652012-06-05 09:22:59 -0700381 if (!mDisableOverrideUrlLoading && mInForeground) {
Michael Kolb18eb3772010-12-10 14:29:51 -0800382 return mWebViewController.shouldOverrideUrlLoading(Tab.this,
383 view, url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700384 } else {
385 return false;
386 }
387 }
388
Vivek Sekharb991edb2014-12-17 18:18:07 -0800389 @Override
390 public boolean shouldDownloadFavicon(WebView view, String url) {
391 return true;
392 }
393
Grace Kloba22ac16e2009-10-07 18:00:23 -0700394 /**
Steve Block2466eff2011-10-03 15:33:09 +0100395 * Updates the security state. This method is called when we discover
396 * another resource to be loaded for this page (for example,
397 * javascript). While we update the security state, we do not update
398 * the lock icon until we are done loading, as it is slightly more
399 * secure this way.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700400 */
401 @Override
402 public void onLoadResource(WebView view, String url) {
403 if (url != null && url.length() > 0) {
404 // It is only if the page claims to be secure that we may have
Steve Block2466eff2011-10-03 15:33:09 +0100405 // to update the security state:
406 if (mCurrentState.mSecurityState == SecurityState.SECURITY_STATE_SECURE) {
407 // If NOT a 'safe' url, change the state to mixed content!
Grace Kloba22ac16e2009-10-07 18:00:23 -0700408 if (!(URLUtil.isHttpsUrl(url) || URLUtil.isDataUrl(url)
409 || URLUtil.isAboutUrl(url))) {
Steve Block2466eff2011-10-03 15:33:09 +0100410 mCurrentState.mSecurityState = SecurityState.SECURITY_STATE_MIXED;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700411 }
412 }
413 }
414 }
415
416 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700417 * Show a dialog informing the user of the network error reported by
418 * WebCore if it is in the foreground.
419 */
420 @Override
421 public void onReceivedError(WebView view, int errorCode,
422 String description, String failingUrl) {
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800423 // Used for the syncCurrentState to use
424 // the failing url instead of using webview url
425 mReceivedError = true;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700426 }
427
428 /**
429 * Check with the user if it is ok to resend POST data as the page they
430 * are trying to navigate to is the result of a POST.
431 */
432 @Override
433 public void onFormResubmission(WebView view, final Message dontResend,
434 final Message resend) {
435 if (!mInForeground) {
436 dontResend.sendToTarget();
437 return;
438 }
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500439 if (mDontResend != null) {
440 Log.w(LOGTAG, "onFormResubmission should not be called again "
441 + "while dialog is still up");
442 dontResend.sendToTarget();
443 return;
444 }
445 mDontResend = dontResend;
446 mResend = resend;
Michael Kolb14612442011-06-24 13:06:29 -0700447 new AlertDialog.Builder(mContext).setTitle(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700448 R.string.browserFrameFormResubmitLabel).setMessage(
449 R.string.browserFrameFormResubmitMessage)
450 .setPositiveButton(R.string.ok,
451 new DialogInterface.OnClickListener() {
452 public void onClick(DialogInterface dialog,
453 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500454 if (mResend != null) {
455 mResend.sendToTarget();
456 mResend = null;
457 mDontResend = null;
458 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700459 }
460 }).setNegativeButton(R.string.cancel,
461 new DialogInterface.OnClickListener() {
462 public void onClick(DialogInterface dialog,
463 int which) {
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 }).setOnCancelListener(new OnCancelListener() {
471 public void onCancel(DialogInterface dialog) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500472 if (mDontResend != null) {
473 mDontResend.sendToTarget();
474 mResend = null;
475 mDontResend = null;
476 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700477 }
478 }).show();
479 }
480
481 /**
482 * Insert the url into the visited history database.
483 * @param url The url to be inserted.
484 * @param isReload True if this url is being reloaded.
485 * FIXME: Not sure what to do when reloading the page.
486 */
487 @Override
488 public void doUpdateVisitedHistory(WebView view, String url,
489 boolean isReload) {
John Reck324d4402011-01-11 16:56:42 -0800490 mWebViewController.doUpdateVisitedHistory(Tab.this, isReload);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700491 }
492
493 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700494 * Handles an HTTP authentication request.
495 *
496 * @param handler The authentication handler
497 * @param host The host
498 * @param realm The realm
499 */
500 @Override
501 public void onReceivedHttpAuthRequest(WebView view,
502 final HttpAuthHandler handler, final String host,
503 final String realm) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700504 mWebViewController.onReceivedHttpAuthRequest(Tab.this, view, handler, host, realm);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700505 }
506
507 @Override
John Reck438bf462011-01-12 18:11:46 -0800508 public WebResourceResponse shouldInterceptRequest(WebView view,
509 String url) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800510 //intercept if opening a new incognito tab - show the incognito welcome page
Axesh R. Ajmerad04cad12015-08-12 12:02:44 -0700511
512 // show only incognito content and webview has private
513 // and cannot go back(only supported if explicit from UI )
514 if (view.isPrivateBrowsingEnabled() &&
515 !view.canGoBack() &&
516 url.startsWith(Controller.INCOGNITO_URI)) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800517 Resources resourceHandle = mContext.getResources();
518 InputStream inStream = resourceHandle.openRawResource(
Bijan Amirzada41242f22014-03-21 12:12:18 -0700519 com.android.browser.R.raw.incognito_mode_start_page);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800520 return new WebResourceResponse("text/html", "utf8", inStream);
521 }
kaiyiz6e5b3e02013-08-19 20:02:01 +0800522 WebResourceResponse res;
523 if (MyNavigationUtil.MY_NAVIGATION.equals(url)) {
524 res = MyNavigationProvider.shouldInterceptRequest(mContext, url);
525 } else {
526 res = HomeProvider.shouldInterceptRequest(mContext, url);
527 }
John Reck438bf462011-01-12 18:11:46 -0800528 return res;
529 }
530
531 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700532 public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
533 if (!mInForeground) {
534 return false;
535 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700536 return mWebViewController.shouldOverrideKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700537 }
538
539 @Override
540 public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700541 if (!mInForeground) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700542 return;
543 }
John Reck997b1b72012-04-19 18:08:25 -0700544 if (!mWebViewController.onUnhandledKeyEvent(event)) {
545 super.onUnhandledKeyEvent(view, event);
546 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700547 }
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700548
549 @Override
550 public void beforeNavigation(WebView view, String url) {
Pankaj Garg21dad562015-07-02 17:17:24 -0700551 mTouchIconUrl = null;
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -0700552 TitleBar titleBar = null;
553 Controller controller = (Controller)mWebViewController;
554 UI ui = controller.getUi();
555
556 if (ui instanceof BaseUi) {
557 titleBar = ((BaseUi)ui).getTitleBar();
558 if (titleBar != null) {
559 NavigationBarBase navBar = titleBar.getNavigationBar();
560 navBar.showCurrentFavicon(Tab.this); // Show the default Favicon while loading a new page
561 }
562 }
563
Site Mao61b68212015-07-16 10:56:31 -0700564 if (BaseUi.isUiLowPowerMode()) {
Pankaj Garg18aa0a12015-06-22 11:06:12 -0700565 return;
566 }
567
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700568 if (isPrivateBrowsingEnabled()) {
569 return;
570 }
571
572 if (!mFirstVisualPixelPainted) {
573 return;
574 }
575
576 final int idx = view.copyBackForwardList().getCurrentIndex();
577 boolean bitmapExists = view.hasSnapshot(idx);
578
579 int progress = 100;
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -0700580 if (titleBar != null) {
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700581 progress = titleBar.getProgressView().getProgressPercent();
582 }
583
584 if (bitmapExists && progress < 85) {
585 return;
586 }
587
588 int index = getCaptureIndex(view.getLastCommittedHistoryIndex());
Sagar Dhawan49f85cf2015-07-10 16:54:20 -0700589 view.captureSnapshot(index, null);
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700590 }
591
592 @Override
593 public void onHistoryItemCommit(WebView view, int index) {
Site Mao61b68212015-07-16 10:56:31 -0700594 if (BaseUi.isUiLowPowerMode()) {
Pankaj Garg18aa0a12015-06-22 11:06:12 -0700595 return;
596 }
597
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -0700598 // prevent snapshot tab from commiting any history
599 if (isSnapshot()) {
600 return;
601 }
602
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700603 mTabHistoryUpdateObservable.set(index);
Vivek Sekhar53ef8932015-06-18 16:51:43 -0700604 final int maxIdx = view.copyBackForwardList().getSize();
605 final WebView wv = view;
606 view.getSnapshotIds(new ValueCallback <List<Integer>>() {
607 @Override
608 public void onReceiveValue(List<Integer> ids) {
609 int currentTabIdx = mWebViewController.getTabControl().getCurrentPosition();
610 for (Integer id : ids) {
611 if (getTabIdxFromCaptureIdx(id) == currentTabIdx &&
612 getNavIdxFromCaptureIdx(id) >= maxIdx) {
613 wv.deleteSnapshot(id);
614 }
615 }
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700616 }
Vivek Sekhar53ef8932015-06-18 16:51:43 -0700617 });
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700618 }
Pankaj Garg62bc7912015-04-14 16:08:59 -0700619
620 @Override
621 public void onKeyboardStateChange(boolean popup) {
Vivek Sekhard4de6162015-07-21 15:01:45 -0700622 boolean keyboardWasShowing = isKeyboardShowing();
Sagar Dhawan33551ff2015-07-08 17:24:44 -0700623 mIsKeyboardUp = popup;
Vivek Sekhard4de6162015-07-21 15:01:45 -0700624 Controller controller = (Controller)mWebViewController;
625 BaseUi ui = (BaseUi) controller.getUi();
626 // lock the title bar
627 if (popup)
628 ui.getTitleBar().showTopControls(true);
629 if (keyboardWasShowing && popup)
630 ui.getTitleBar().enableTopControls(true);
Pankaj Garg62bc7912015-04-14 16:08:59 -0700631 if (BrowserSettings.getInstance().useFullscreen()) {
Pankaj Garg62bc7912015-04-14 16:08:59 -0700632 ui.forceDisableFullscreenMode(popup);
633 }
634 }
Vivek Sekharae3b1792015-08-03 12:26:41 -0700635
636 @Override
637 public void onAttachInterstitialPage(WebView mWebView) {
638 Controller controller = (Controller)mWebViewController;
639 BaseUi ui = (BaseUi) controller.getUi();
640 ui.getTitleBar().showTopControls(false);
641 }
642
643 @Override
644 public void onDetachInterstitialPage(WebView mWebView) {
645 Controller controller = (Controller)mWebViewController;
646 BaseUi ui = (BaseUi) controller.getUi();
647 ui.getTitleBar().enableTopControls(true);
648 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700649 };
650
John Reck1cf4b792011-07-26 10:22:22 -0700651 private void syncCurrentState(WebView view, String url) {
652 // Sync state (in case of stop/timeout)
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800653
Axesh R. Ajmerad04cad12015-08-12 12:02:44 -0700654
655
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800656 if (mReceivedError) {
657 mCurrentState.mUrl = url;
658 mCurrentState.mOriginalUrl = url;
Axesh R. Ajmerad04cad12015-08-12 12:02:44 -0700659 } else if (view.isPrivateBrowsingEnabled() &&
660 !TextUtils.isEmpty(url) &&
661 url.contains(Controller.INCOGNITO_URI)) {
662 mCurrentState.mUrl = mCurrentState.mOriginalUrl = "";
663 }
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -0700664
Axesh R. Ajmerad04cad12015-08-12 12:02:44 -0700665 else {
666 mCurrentState.mUrl = view.getUrl();
667 mCurrentState.mOriginalUrl = view.getOriginalUrl();
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800668 }
669
John Reck1cf4b792011-07-26 10:22:22 -0700670 if (mCurrentState.mUrl == null) {
John Reck8bcafc12011-08-29 16:43:02 -0700671 mCurrentState.mUrl = "";
John Reck1cf4b792011-07-26 10:22:22 -0700672 }
John Reck1cf4b792011-07-26 10:22:22 -0700673 mCurrentState.mTitle = view.getTitle();
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800674
675
John Reck1cf4b792011-07-26 10:22:22 -0700676 if (!URLUtil.isHttpsUrl(mCurrentState.mUrl)) {
677 // In case we stop when loading an HTTPS page from an HTTP page
678 // but before a provisional load occurred
Steve Block2466eff2011-10-03 15:33:09 +0100679 mCurrentState.mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
John Reck1cf4b792011-07-26 10:22:22 -0700680 }
John Reck502a3532011-08-16 14:21:46 -0700681 mCurrentState.mIncognito = view.isPrivateBrowsingEnabled();
John Reck1cf4b792011-07-26 10:22:22 -0700682 }
683
Pankaj Garg21dad562015-07-02 17:17:24 -0700684 public String getTouchIconUrl() {
685 return mTouchIconUrl;
686 }
Tarun Nainani8eb00912014-07-17 12:28:32 -0700687
Sagar Dhawan33551ff2015-07-08 17:24:44 -0700688 public boolean isKeyboardShowing() {
689 Controller controller = (Controller)mWebViewController;
690 return (mIsKeyboardUp || controller.getUi().isEditingUrl());
691 }
692
Tarun Nainani8eb00912014-07-17 12:28:32 -0700693 public boolean isTabFullScreen() {
694 return mFullScreen;
695 }
696
Vivek Sekharf96064b2014-07-28 16:32:34 -0700697 protected void setTabFullscreen(boolean fullScreen) {
Tarun Nainani8eb00912014-07-17 12:28:32 -0700698 Controller controller = (Controller)mWebViewController;
Sudheer Koganti24766882014-10-02 10:58:09 -0700699 controller.getUi().showFullscreen(fullScreen);
Tarun Nainani8eb00912014-07-17 12:28:32 -0700700 mFullScreen = fullScreen;
Vivek Sekharf96064b2014-07-28 16:32:34 -0700701 }
702
Sudheer Koganti24766882014-10-02 10:58:09 -0700703 public boolean exitFullscreen() {
704 if (mFullScreen) {
705 Controller controller = (Controller)mWebViewController;
706 controller.getUi().showFullscreen(false);
707 if (getWebView() != null)
708 getWebView().exitFullscreen();
709 mFullScreen = false;
710 return true;
711 }
712 return false;
713 }
714
715
716
717
Grace Kloba22ac16e2009-10-07 18:00:23 -0700718 // -------------------------------------------------------------------------
719 // WebChromeClient implementation for the main WebView
720 // -------------------------------------------------------------------------
721
722 private final WebChromeClient mWebChromeClient = new WebChromeClient() {
723 // Helper method to create a new tab or sub window.
724 private void createWindow(final boolean dialog, final Message msg) {
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700725 this.createWindow(dialog, msg, null, false);
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700726 }
727
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700728 private void createWindow(final boolean dialog, final Message msg, final String url,
729 final boolean opener_suppressed) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700730 WebView.WebViewTransport transport =
731 (WebView.WebViewTransport) msg.obj;
732 if (dialog) {
733 createSubWindow();
Michael Kolb8233fac2010-10-26 16:08:53 -0700734 mWebViewController.attachSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700735 transport.setWebView(mSubView);
736 } else {
Pankaj Garg21dad562015-07-02 17:17:24 -0700737 capture();
738
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700739 final Tab newTab = mWebViewController.openTab(url,
John Reck5949c662011-05-27 09:52:29 -0700740 Tab.this, true, true);
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700741 // This is special case for rendering links on a webpage in
742 // a new tab. If opener is suppressed, the WebContents created
743 // by the content layer are not fully initialized. This check
744 // will prevent content layer from overriding WebContents
745 // created by new tab with the uninitialized instance.
746 if (!opener_suppressed) {
747 transport.setWebView(newTab.getWebView());
748 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700749 }
750 msg.sendToTarget();
751 }
752
753 @Override
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700754 public void toggleFullscreenModeForTab(boolean enterFullscreen) {
755 if (mWebViewController instanceof Controller) {
Vivek Sekharf96064b2014-07-28 16:32:34 -0700756 setTabFullscreen(enterFullscreen);
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700757 }
758 }
759
760 @Override
Tarun Nainani8eb00912014-07-17 12:28:32 -0700761 public void onOffsetsForFullscreenChanged(float topControlsOffsetYPix,
762 float contentOffsetYPix,
763 float overdrawBottomHeightPix) {
764 if (mWebViewController instanceof Controller) {
Vivek Sekhar8ee3abb2014-07-14 12:32:05 -0700765 Controller controller = (Controller)mWebViewController;
Tarun Nainani8eb00912014-07-17 12:28:32 -0700766 controller.getUi().translateTitleBar(topControlsOffsetYPix);
Vivek Sekhar11b40062015-06-24 11:49:04 -0700767 // Resize the viewport if top controls is not visible
Vivek Sekhar2ee19a32015-07-02 17:03:57 -0700768 if (mMainView != null &&
769 (topControlsOffsetYPix == 0.0f || contentOffsetYPix == 0.0f))
Vivek Sekhar11b40062015-06-24 11:49:04 -0700770 ((BrowserWebView)mMainView).enableTopControls(
771 (topControlsOffsetYPix == 0.0f) ? true : false);
Vivek Sekhar8ee3abb2014-07-14 12:32:05 -0700772 }
773 }
774
775 @Override
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700776 public boolean isTabFullScreen() {
Vivek Sekharf96064b2014-07-28 16:32:34 -0700777 return mFullScreen;
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700778 }
779
780 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700781 public boolean onCreateWindow(WebView view, final boolean dialog,
782 final boolean userGesture, final Message resultMsg) {
783 // only allow new window or sub window for the foreground case
784 if (!mInForeground) {
785 return false;
786 }
787 // Short-circuit if we can't create any more tabs or sub windows.
788 if (dialog && mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -0700789 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700790 .setTitle(R.string.too_many_subwindows_dialog_title)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200791 .setIconAttribute(android.R.attr.alertDialogIcon)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700792 .setMessage(R.string.too_many_subwindows_dialog_message)
793 .setPositiveButton(R.string.ok, null)
794 .show();
795 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700796 } else if (!mWebViewController.getTabControl().canCreateNewTab()) {
Michael Kolb14612442011-06-24 13:06:29 -0700797 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700798 .setTitle(R.string.too_many_windows_dialog_title)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200799 .setIconAttribute(android.R.attr.alertDialogIcon)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700800 .setMessage(R.string.too_many_windows_dialog_message)
801 .setPositiveButton(R.string.ok, null)
802 .show();
803 return false;
804 }
805
806 // Short-circuit if this was a user gesture.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800807 if (userGesture || !mSettings.blockPopupWindows()) {
Tarun Nainani4f5137d2015-04-16 17:26:18 -0700808 WebView.WebViewTransport transport =
809 (WebView.WebViewTransport) resultMsg.obj;
810 CreateWindowParams windowParams = transport.getCreateWindowParams();
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700811 if (windowParams.mOpenerSuppressed) {
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700812 createWindow(dialog, resultMsg, windowParams.mURL, true);
813 // This is special case for rendering links on a webpage in
814 // a new tab. If opener is suppressed, the WebContents created
815 // by the content layer are not fully initialized. Returning false
816 // will prevent content layer from overriding WebContents
817 // created by new tab with the uninitialized instance.
818 return false;
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700819 }
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700820
821 createWindow(dialog, resultMsg);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700822 return true;
823 }
824
Tarun Nainani4f5137d2015-04-16 17:26:18 -0700825 createWindow(dialog, resultMsg);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700826 return true;
827 }
828
829 @Override
Patrick Scotteb5061b2009-11-18 15:00:30 -0500830 public void onRequestFocus(WebView view) {
831 if (!mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700832 mWebViewController.switchToTab(Tab.this);
Patrick Scotteb5061b2009-11-18 15:00:30 -0500833 }
834 }
835
836 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700837 public void onCloseWindow(WebView window) {
Michael Kolbc831b632011-05-11 09:30:34 -0700838 if (mParent != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700839 // JavaScript can only close popup window.
840 if (mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700841 mWebViewController.switchToTab(mParent);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700842 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700843 mWebViewController.closeTab(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700844 }
845 }
846
847 @Override
848 public void onProgressChanged(WebView view, int newProgress) {
John Reck30c714c2010-12-16 17:30:34 -0800849 mPageLoadProgress = newProgress;
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800850 if (newProgress == 100) {
851 mInPageLoad = false;
852 }
John Reck30c714c2010-12-16 17:30:34 -0800853 mWebViewController.onProgressChanged(Tab.this);
Michael Kolb72864272012-05-03 15:42:15 -0700854 if (mUpdateThumbnail && newProgress == 100) {
855 mUpdateThumbnail = false;
856 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700857 }
858
859 @Override
Leon Scroggins21d9b902010-03-11 09:33:11 -0500860 public void onReceivedTitle(WebView view, final String title) {
John Reck30c714c2010-12-16 17:30:34 -0800861 mCurrentState.mTitle = title;
Michael Kolb8233fac2010-10-26 16:08:53 -0700862 mWebViewController.onReceivedTitle(Tab.this, title);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700863 }
864
865 @Override
866 public void onReceivedIcon(WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -0800867 mCurrentState.mFavicon = icon;
Michael Kolb8233fac2010-10-26 16:08:53 -0700868 mWebViewController.onFavicon(Tab.this, view, icon);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700869 }
870
871 @Override
872 public void onReceivedTouchIconUrl(WebView view, String url,
873 boolean precomposed) {
Michael Kolb14612442011-06-24 13:06:29 -0700874 final ContentResolver cr = mContext.getContentResolver();
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400875 // Let precomposed icons take precedence over non-composed
876 // icons.
877 if (precomposed && mTouchIconLoader != null) {
878 mTouchIconLoader.cancel(false);
879 mTouchIconLoader = null;
880 }
881 // Have only one async task at a time.
882 if (mTouchIconLoader == null) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700883 mTouchIconLoader = new DownloadTouchIcon(Tab.this,
Michael Kolb14612442011-06-24 13:06:29 -0700884 mContext, cr, view);
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400885 mTouchIconLoader.execute(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700886 }
Pankaj Garg21dad562015-07-02 17:17:24 -0700887 mTouchIconUrl = url;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700888 }
889
890 @Override
891 public void onShowCustomView(View view,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800892 CustomViewCallback callback) {
Michael Kolb14612442011-06-24 13:06:29 -0700893 Activity activity = mWebViewController.getActivity();
894 if (activity != null) {
895 onShowCustomView(view, activity.getRequestedOrientation(), callback);
896 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400897 }
898
899 @Override
900 public void onShowCustomView(View view, int requestedOrientation,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800901 CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700902 if (mInForeground) mWebViewController.showCustomView(Tab.this, view,
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400903 requestedOrientation, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700904 }
905
906 @Override
907 public void onHideCustomView() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700908 if (mInForeground) mWebViewController.hideCustomView();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700909 }
910
911 /**
912 * The origin has exceeded its database quota.
913 * @param url the URL that exceeded the quota
914 * @param databaseIdentifier the identifier of the database on which the
915 * transaction that caused the quota overflow was run
916 * @param currentQuota the current quota for the origin.
917 * @param estimatedSize the estimated size of the database.
918 * @param totalUsedQuota is the sum of all origins' quota.
919 * @param quotaUpdater The callback to run when a decision to allow or
920 * deny quota has been made. Don't forget to call this!
921 */
922 @Override
923 public void onExceededDatabaseQuota(String url,
924 String databaseIdentifier, long currentQuota, long estimatedSize,
925 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -0700926 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -0700927 .onExceededDatabaseQuota(url, databaseIdentifier,
928 currentQuota, estimatedSize, totalUsedQuota,
929 quotaUpdater);
930 }
931
932 /**
933 * The Application Cache has exceeded its max size.
934 * @param spaceNeeded is the amount of disk space that would be needed
935 * in order for the last appcache operation to succeed.
936 * @param totalUsedQuota is the sum of all origins' quota.
937 * @param quotaUpdater A callback to inform the WebCore thread that a
938 * new app cache size is available. This callback must always
939 * be executed at some point to ensure that the sleeping
940 * WebCore thread is woken up.
941 */
942 @Override
943 public void onReachedMaxAppCacheSize(long spaceNeeded,
944 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -0700945 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -0700946 .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota,
947 quotaUpdater);
948 }
949
Ben Murdoch65acc352009-11-19 18:16:04 +0000950 /* Adds a JavaScript error message to the system log and if the JS
951 * console is enabled in the about:debug options, to that console
952 * also.
Ben Murdochc42addf2010-01-28 15:19:59 +0000953 * @param consoleMessage the message object.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700954 */
955 @Override
Ben Murdochc42addf2010-01-28 15:19:59 +0000956 public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Jeff Hamilton47654f42010-09-07 09:57:51 -0500957 // Don't log console messages in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -0700958 if (isPrivateBrowsingEnabled()) return true;
Jeff Hamilton47654f42010-09-07 09:57:51 -0500959
Ben Murdochc42addf2010-01-28 15:19:59 +0000960 String message = "Console: " + consoleMessage.message() + " "
961 + consoleMessage.sourceId() + ":"
962 + consoleMessage.lineNumber();
963
964 switch (consoleMessage.messageLevel()) {
965 case TIP:
966 Log.v(CONSOLE_LOGTAG, message);
967 break;
968 case LOG:
969 Log.i(CONSOLE_LOGTAG, message);
970 break;
971 case WARNING:
972 Log.w(CONSOLE_LOGTAG, message);
973 break;
974 case ERROR:
975 Log.e(CONSOLE_LOGTAG, message);
976 break;
977 case DEBUG:
978 Log.d(CONSOLE_LOGTAG, message);
979 break;
980 }
981
982 return true;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700983 }
984
985 /**
986 * Ask the browser for an icon to represent a <video> element.
987 * This icon will be used if the Web page did not specify a poster attribute.
988 * @return Bitmap The icon or null if no such icon is available.
989 */
990 @Override
991 public Bitmap getDefaultVideoPoster() {
992 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700993 return mWebViewController.getDefaultVideoPoster();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700994 }
995 return null;
996 }
997
998 /**
999 * Ask the host application for a custom progress view to show while
1000 * a <video> is loading.
1001 * @return View The progress view.
1002 */
1003 @Override
1004 public View getVideoLoadingProgressView() {
1005 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001006 return mWebViewController.getVideoLoadingProgressView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001007 }
1008 return null;
1009 }
1010
1011 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00001012 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001013 if (mInForeground) {
Ben Murdoch8cad4132012-01-11 10:56:43 +00001014 mWebViewController.openFileChooser(uploadMsg, acceptType, capture);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001015 } else {
1016 uploadMsg.onReceiveValue(null);
1017 }
1018 }
1019
Vivek Sekharb54614f2014-05-01 19:03:37 -07001020 @Override
1021 public void showFileChooser(ValueCallback<String[]> uploadFilePaths, String acceptTypes,
1022 boolean capture) {
1023 if (mInForeground) {
1024 mWebViewController.showFileChooser(uploadFilePaths, acceptTypes, capture);
1025 } else {
1026 uploadFilePaths.onReceiveValue(null);
1027 }
1028 }
1029
Grace Kloba22ac16e2009-10-07 18:00:23 -07001030 /**
1031 * Deliver a list of already-visited URLs
1032 */
1033 @Override
1034 public void getVisitedHistory(final ValueCallback<String[]> callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001035 mWebViewController.getVisitedHistory(callback);
1036 }
Ben Murdoch8029a772010-11-16 11:58:21 +00001037
1038 @Override
1039 public void setupAutoFill(Message message) {
1040 // Prompt the user to set up their profile.
1041 final Message msg = message;
Michael Kolb14612442011-06-24 13:06:29 -07001042 AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
1043 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
Ben Murdoch1d676b62011-01-17 12:54:24 +00001044 Context.LAYOUT_INFLATER_SERVICE);
1045 final View layout = inflater.inflate(R.layout.setup_autofill_dialog, null);
1046
1047 builder.setView(layout)
1048 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1049 @Override
1050 public void onClick(DialogInterface dialog, int id) {
1051 CheckBox disableAutoFill = (CheckBox) layout.findViewById(
1052 R.id.setup_autofill_dialog_disable_autofill);
1053
1054 if (disableAutoFill.isChecked()) {
1055 // Disable autofill and show a toast with how to turn it on again.
John Reck35e9dd62011-04-25 09:01:54 -07001056 mSettings.setAutofillEnabled(false);
Michael Kolb14612442011-06-24 13:06:29 -07001057 Toast.makeText(mContext,
Ben Murdoch1d676b62011-01-17 12:54:24 +00001058 R.string.autofill_setup_dialog_negative_toast,
1059 Toast.LENGTH_LONG).show();
1060 } else {
1061 // Take user to the AutoFill profile editor. When they return,
1062 // we will send the message that we pass here which will trigger
1063 // the form to get filled out with their new profile.
1064 mWebViewController.setupAutoFill(msg);
1065 }
1066 }
1067 })
1068 .setNegativeButton(R.string.cancel, null)
1069 .show();
Ben Murdoch8029a772010-11-16 11:58:21 +00001070 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001071 };
1072
1073 // -------------------------------------------------------------------------
1074 // WebViewClient implementation for the sub window
1075 // -------------------------------------------------------------------------
1076
1077 // Subclass of WebViewClient used in subwindows to notify the main
1078 // WebViewClient of certain WebView activities.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001079 private static class SubWindowClient extends WebViewClient {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001080 // The main WebViewClient.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001081 private final WebViewClient mClient;
Michael Kolb8233fac2010-10-26 16:08:53 -07001082 private final WebViewController mController;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001083
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001084 SubWindowClient(WebViewClient client, WebViewController controller) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001085 mClient = client;
Michael Kolb8233fac2010-10-26 16:08:53 -07001086 mController = controller;
Leon Scroggins III211ba542010-04-19 13:21:13 -04001087 }
1088 @Override
1089 public void onPageStarted(WebView view, String url, Bitmap favicon) {
1090 // Unlike the others, do not call mClient's version, which would
1091 // change the progress bar. However, we do want to remove the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001092 // find or select dialog.
Michael Kolb8233fac2010-10-26 16:08:53 -07001093 mController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001094 }
1095 @Override
1096 public void doUpdateVisitedHistory(WebView view, String url,
1097 boolean isReload) {
1098 mClient.doUpdateVisitedHistory(view, url, isReload);
1099 }
1100 @Override
1101 public boolean shouldOverrideUrlLoading(WebView view, String url) {
1102 return mClient.shouldOverrideUrlLoading(view, url);
1103 }
1104 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -07001105 public void onReceivedHttpAuthRequest(WebView view,
1106 HttpAuthHandler handler, String host, String realm) {
1107 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
1108 }
1109 @Override
1110 public void onFormResubmission(WebView view, Message dontResend,
1111 Message resend) {
1112 mClient.onFormResubmission(view, dontResend, resend);
1113 }
1114 @Override
1115 public void onReceivedError(WebView view, int errorCode,
1116 String description, String failingUrl) {
1117 mClient.onReceivedError(view, errorCode, description, failingUrl);
1118 }
1119 @Override
1120 public boolean shouldOverrideKeyEvent(WebView view,
1121 android.view.KeyEvent event) {
1122 return mClient.shouldOverrideKeyEvent(view, event);
1123 }
1124 @Override
1125 public void onUnhandledKeyEvent(WebView view,
1126 android.view.KeyEvent event) {
1127 mClient.onUnhandledKeyEvent(view, event);
1128 }
1129 }
1130
1131 // -------------------------------------------------------------------------
1132 // WebChromeClient implementation for the sub window
1133 // -------------------------------------------------------------------------
1134
1135 private class SubWindowChromeClient extends WebChromeClient {
1136 // The main WebChromeClient.
1137 private final WebChromeClient mClient;
1138
1139 SubWindowChromeClient(WebChromeClient client) {
1140 mClient = client;
1141 }
1142 @Override
1143 public void onProgressChanged(WebView view, int newProgress) {
1144 mClient.onProgressChanged(view, newProgress);
1145 }
1146 @Override
1147 public boolean onCreateWindow(WebView view, boolean dialog,
1148 boolean userGesture, android.os.Message resultMsg) {
1149 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
1150 }
1151 @Override
1152 public void onCloseWindow(WebView window) {
1153 if (window != mSubView) {
1154 Log.e(LOGTAG, "Can't close the window");
1155 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001156 mWebViewController.dismissSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001157 }
1158 }
1159
1160 // -------------------------------------------------------------------------
1161
1162 // Construct a new tab
Michael Kolb7bcafde2011-05-09 13:55:59 -07001163 Tab(WebViewController wvcontroller, WebView w) {
John Reck1cf4b792011-07-26 10:22:22 -07001164 this(wvcontroller, w, null);
1165 }
1166
1167 Tab(WebViewController wvcontroller, Bundle state) {
1168 this(wvcontroller, null, state);
1169 }
1170
1171 Tab(WebViewController wvcontroller, WebView w, Bundle state) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001172 mWebViewController = wvcontroller;
Michael Kolb14612442011-06-24 13:06:29 -07001173 mContext = mWebViewController.getContext();
John Reck35e9dd62011-04-25 09:01:54 -07001174 mSettings = BrowserSettings.getInstance();
Michael Kolb14612442011-06-24 13:06:29 -07001175 mDataController = DataController.getInstance(mContext);
1176 mCurrentState = new PageState(mContext, w != null
John Reck847b5322011-04-14 17:02:18 -07001177 ? w.isPrivateBrowsingEnabled() : false);
Tarun Nainani8084c822014-06-25 13:38:06 -07001178 setTimeStamp();
Michael Kolb8233fac2010-10-26 16:08:53 -07001179 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001180 mInForeground = false;
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001181 mWebViewDestroyedByMemoryMonitor = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001182
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001183 mDownloadListener = new BrowserDownloadListener() {
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001184 public void onDownloadStart(String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001185 String contentDisposition, String mimetype, String referer,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001186 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001187 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001188 mimetype, referer, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001189 }
1190 };
1191
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001192 mCaptureWidth = mContext.getResources().getDimensionPixelSize(R.dimen.tab_thumbnail_width);
1193 mCaptureHeight =mContext.getResources().getDimensionPixelSize(R.dimen.tab_thumbnail_height);
1194
1195 initCaptureBitmap();
1196
John Reck1cf4b792011-07-26 10:22:22 -07001197 restoreState(state);
John Reck52be4782011-08-26 15:37:29 -07001198 if (getId() == -1) {
1199 mId = TabControl.getNextId();
1200 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001201 setWebView(w);
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -07001202
1203 UI ui = ((Controller)mWebViewController).getUi();
1204 if (ui instanceof BaseUi) {
1205 TitleBar titleBar = ((BaseUi)ui).getTitleBar();
1206 if (titleBar != null) {
1207 NavigationBarBase navBar = titleBar.getNavigationBar();
1208 navBar.showCurrentFavicon(this); // Show the default Favicon while loading a new page
1209 }
1210 }
1211
Michael Kolb9ef259a2011-07-12 15:33:08 -07001212 mHandler = new Handler() {
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001213 @Override
Michael Kolb9ef259a2011-07-12 15:33:08 -07001214 public void handleMessage(Message m) {
John Reck1cf4b792011-07-26 10:22:22 -07001215 switch (m.what) {
1216 case MSG_CAPTURE:
1217 capture();
1218 break;
1219 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001220 }
1221 };
Pankaj Garg1c13cab2015-05-12 11:52:17 -07001222
1223 mFirstPixelObservable = new Observable();
1224 mFirstPixelObservable.set(false);
1225 mTabHistoryUpdateObservable = new Observable();
John Reck1cf4b792011-07-26 10:22:22 -07001226 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001227
Pankaj Gargb4b4f012015-08-04 16:19:14 -07001228 public void initCaptureBitmap() {
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001229 mCapture = Bitmap.createBitmap(mCaptureWidth, mCaptureHeight, Bitmap.Config.RGB_565);
1230 mCapture.eraseColor(Color.WHITE);
Michael Kolb72864272012-05-03 15:42:15 -07001231 }
1232
Mathew Inwoode09305e2011-09-02 12:03:26 +01001233 /**
1234 * This is used to get a new ID when the tab has been preloaded, before it is displayed and
1235 * added to TabControl. Preloaded tabs can be created before restoreInstanceState, leading
1236 * to overlapping IDs between the preloaded and restored tabs.
1237 */
1238 public void refreshIdAfterPreload() {
1239 mId = TabControl.getNextId();
1240 }
1241
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001242 public void setController(WebViewController ctl) {
1243 mWebViewController = ctl;
1244
John Reck1cf4b792011-07-26 10:22:22 -07001245 if (mWebViewController.shouldCaptureThumbnails()) {
1246 synchronized (Tab.this) {
1247 if (mCapture == null) {
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001248 initCaptureBitmap();
1249 if (mInForeground && !mHandler.hasMessages(MSG_CAPTURE)) {
1250 mHandler.sendEmptyMessageDelayed(MSG_CAPTURE, CAPTURE_DELAY);
John Reck1cf4b792011-07-26 10:22:22 -07001251 }
1252 }
1253 }
1254 } else {
1255 synchronized (Tab.this) {
1256 mCapture = null;
1257 deleteThumbnail();
1258 }
1259 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001260 }
1261
Michael Kolbc831b632011-05-11 09:30:34 -07001262 public long getId() {
1263 return mId;
1264 }
1265
Michael Kolb91911a22012-01-17 11:21:25 -08001266 void setWebView(WebView w) {
1267 setWebView(w, true);
1268 }
1269
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001270 public boolean isNativeActive(){
1271 if (mMainView == null)
1272 return false;
1273 return true;
1274 }
1275
1276 public void setTimeStamp(){
1277 Date d = new Date();
1278 timestamp = (new Timestamp(d.getTime()));
1279 }
1280
1281 public Timestamp getTimestamp() {
1282 return timestamp;
1283 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001284 /**
1285 * Sets the WebView for this tab, correctly removing the old WebView from
1286 * the container view.
1287 */
Michael Kolb91911a22012-01-17 11:21:25 -08001288 void setWebView(WebView w, boolean restore) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001289 if (mMainView == w) {
1290 return;
1291 }
Michael Kolba713ec82010-11-29 17:27:06 -08001292
Michael Kolba713ec82010-11-29 17:27:06 -08001293 mWebViewController.onSetWebView(this, w);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001294
John Reck1cf4b792011-07-26 10:22:22 -07001295 if (mMainView != null) {
John Reck8ee633f2011-08-09 16:00:35 -07001296 mMainView.setPictureListener(null);
John Reck1cf4b792011-07-26 10:22:22 -07001297 if (w != null) {
1298 syncCurrentState(w, null);
1299 } else {
Panos Thomasa9a5a582014-03-18 19:20:08 -07001300 mCurrentState = new PageState(mContext, mMainView.isPrivateBrowsingEnabled());
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001301
1302 if (mWebViewDestroyedByMemoryMonitor) {
1303 /*
1304 * If tab was destroyed as a result of the MemoryMonitor
1305 * then we need to restore the state properties
1306 * from the old WebView (mMainView)
1307 */
1308 syncCurrentState(mMainView, null);
1309 mWebViewDestroyedByMemoryMonitor = false;
1310 }
John Reck1cf4b792011-07-26 10:22:22 -07001311 }
1312 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001313 // set the new one
1314 mMainView = w;
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001315
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001316 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001317 if (mMainView != null) {
1318 mMainView.setWebViewClient(mWebViewClient);
1319 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001320 // Attach DownloadManager so that downloads can start in an active
1321 // or a non-active window. This can happen when going to a site that
1322 // does a redirect after a period of time. The user could have
1323 // switched to another tab while waiting for the download to start.
1324 mMainView.setDownloadListener(mDownloadListener);
John Reck8ee633f2011-08-09 16:00:35 -07001325 TabControl tc = mWebViewController.getTabControl();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001326 if (tc != null /*&& tc.getOnThumbnailUpdatedListener() != null*/) {
John Reck8ee633f2011-08-09 16:00:35 -07001327 mMainView.setPictureListener(this);
1328 }
Michael Kolb91911a22012-01-17 11:21:25 -08001329 if (restore && (mSavedState != null)) {
John Reck8b9bb8b2012-03-08 13:19:40 -08001330 restoreUserAgent();
John Reck6c2e2f32011-08-22 13:41:23 -07001331 WebBackForwardList restoredState
1332 = mMainView.restoreState(mSavedState);
1333 if (restoredState == null || restoredState.getSize() == 0) {
1334 Log.w(LOGTAG, "Failed to restore WebView state!");
1335 loadUrl(mCurrentState.mOriginalUrl, null);
1336 }
John Reck1cf4b792011-07-26 10:22:22 -07001337 mSavedState = null;
1338 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001339 }
1340 }
1341
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001342 public void destroyThroughMemoryMonitor() {
1343 mWebViewDestroyedByMemoryMonitor = true;
1344 destroy();
1345 }
1346
Grace Kloba22ac16e2009-10-07 18:00:23 -07001347 /**
1348 * Destroy the tab's main WebView and subWindow if any
1349 */
1350 void destroy() {
Tarun Nainani2c1dd7c2014-07-05 16:40:12 -07001351
Matthew Huib7f2e9c2014-04-16 11:12:37 -04001352 if (mPostponeDestroy) {
1353 mShouldDestroy = true;
1354 return;
1355 }
1356 mShouldDestroy = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001357 if (mMainView != null) {
1358 dismissSubWindow();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001359 // save the WebView to call destroy() after detach it from the tab
Vivek Sekhar53ef8932015-06-18 16:51:43 -07001360 final WebView webView = mMainView;
Vivek Sekhar2ee19a32015-07-02 17:03:57 -07001361 setWebView(null);
Site Mao61b68212015-07-16 10:56:31 -07001362 if (!mWebViewDestroyedByMemoryMonitor && !BaseUi.isUiLowPowerMode()) {
Vivek Sekhar2ee19a32015-07-02 17:03:57 -07001363 // Tabs can be reused with new instance of WebView so delete the snapshots
Vivek Sekhar53ef8932015-06-18 16:51:43 -07001364 webView.getSnapshotIds(new ValueCallback<List<Integer>>() {
1365 @Override
1366 public void onReceiveValue(List<Integer> ids) {
1367 int currentTabIdx = mWebViewController.getTabControl().getCurrentPosition();
1368 for (Integer id : ids) {
1369 if (getTabIdxFromCaptureIdx(id) == currentTabIdx) {
1370 webView.deleteSnapshot(id);
1371 }
1372 }
Vivek Sekhar53ef8932015-06-18 16:51:43 -07001373 webView.destroy();
Pankaj Garg1c13cab2015-05-12 11:52:17 -07001374 }
Vivek Sekhar53ef8932015-06-18 16:51:43 -07001375 });
Vivek Sekhar2ee19a32015-07-02 17:03:57 -07001376 } else {
1377 webView.destroy();
Pankaj Garg1c13cab2015-05-12 11:52:17 -07001378 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001379 }
1380 }
1381
Matthew Huib7f2e9c2014-04-16 11:12:37 -04001382 private boolean mPostponeDestroy = false;
1383 private boolean mShouldDestroy = false;
1384
1385 public void postponeDestroy() {
1386 mPostponeDestroy = true;
1387 }
1388
1389 public void performPostponedDestroy() {
1390 mPostponeDestroy = false;
1391 if (mShouldDestroy) {
1392 destroy();
1393 }
1394 }
1395
Grace Kloba22ac16e2009-10-07 18:00:23 -07001396 /**
1397 * Remove the tab from the parent
1398 */
1399 void removeFromTree() {
1400 // detach the children
Michael Kolbc831b632011-05-11 09:30:34 -07001401 if (mChildren != null) {
1402 for(Tab t : mChildren) {
1403 t.setParent(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001404 }
1405 }
1406 // remove itself from the parent list
Michael Kolbc831b632011-05-11 09:30:34 -07001407 if (mParent != null) {
1408 mParent.mChildren.remove(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001409 }
Tarun Nainani8eb00912014-07-17 12:28:32 -07001410
1411 mCapture = null;
John Reck1cf4b792011-07-26 10:22:22 -07001412 deleteThumbnail();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001413 }
1414
1415 /**
1416 * Create a new subwindow unless a subwindow already exists.
1417 * @return True if a new subwindow was created. False if one already exists.
1418 */
1419 boolean createSubWindow() {
1420 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001421 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001422 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001423 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001424 mSubView.setWebChromeClient(new SubWindowChromeClient(
1425 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001426 // Set a different DownloadListener for the mSubView, since it will
1427 // just need to dismiss the mSubView, rather than close the Tab
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001428 mSubView.setDownloadListener(new BrowserDownloadListener() {
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001429 public void onDownloadStart(String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001430 String contentDisposition, String mimetype, String referer,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001431 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001432 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001433 contentDisposition, mimetype, referer, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001434 if (mSubView.copyBackForwardList().getSize() == 0) {
1435 // This subwindow was opened for the sole purpose of
1436 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001437 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001438 }
1439 }
1440 });
Michael Kolb14612442011-06-24 13:06:29 -07001441 mSubView.setOnCreateContextMenuListener(mWebViewController.getActivity());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001442 return true;
1443 }
1444 return false;
1445 }
1446
1447 /**
1448 * Dismiss the subWindow for the tab.
1449 */
1450 void dismissSubWindow() {
1451 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001452 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001453 mSubView.destroy();
1454 mSubView = null;
1455 mSubViewContainer = null;
1456 }
1457 }
1458
Grace Kloba22ac16e2009-10-07 18:00:23 -07001459
1460 /**
1461 * Set the parent tab of this tab.
1462 */
Michael Kolbc831b632011-05-11 09:30:34 -07001463 void setParent(Tab parent) {
John Reck52be4782011-08-26 15:37:29 -07001464 if (parent == this) {
1465 throw new IllegalStateException("Cannot set parent to self!");
1466 }
Michael Kolbc831b632011-05-11 09:30:34 -07001467 mParent = parent;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001468 // This tab may have been freed due to low memory. If that is the case,
Michael Kolbc831b632011-05-11 09:30:34 -07001469 // the parent tab id is already saved. If we are changing that id
Grace Kloba22ac16e2009-10-07 18:00:23 -07001470 // (most likely due to removing the parent tab) we must update the
Michael Kolbc831b632011-05-11 09:30:34 -07001471 // parent tab id in the saved Bundle.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001472 if (mSavedState != null) {
1473 if (parent == null) {
1474 mSavedState.remove(PARENTTAB);
1475 } else {
Michael Kolbc831b632011-05-11 09:30:34 -07001476 mSavedState.putLong(PARENTTAB, parent.getId());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001477 }
1478 }
John Reckb0a86db2011-05-24 14:05:58 -07001479
1480 // Sync the WebView useragent with the parent
1481 if (parent != null && mSettings.hasDesktopUseragent(parent.getWebView())
1482 != mSettings.hasDesktopUseragent(getWebView())) {
1483 mSettings.toggleDesktopUseragent(getWebView());
1484 }
John Reck52be4782011-08-26 15:37:29 -07001485
1486 if (parent != null && parent.getId() == getId()) {
1487 throw new IllegalStateException("Parent has same ID as child!");
1488 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001489 }
1490
1491 /**
Michael Kolbc831b632011-05-11 09:30:34 -07001492 * If this Tab was created through another Tab, then this method returns
1493 * that Tab.
1494 * @return the Tab parent or null
1495 */
1496 public Tab getParent() {
1497 return mParent;
1498 }
1499
1500 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001501 * When a Tab is created through the content of another Tab, then we
1502 * associate the Tabs.
1503 * @param child the Tab that was created from this Tab
1504 */
1505 void addChildTab(Tab child) {
Michael Kolbc831b632011-05-11 09:30:34 -07001506 if (mChildren == null) {
1507 mChildren = new Vector<Tab>();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001508 }
Michael Kolbc831b632011-05-11 09:30:34 -07001509 mChildren.add(child);
1510 child.setParent(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001511 }
1512
Michael Kolbc831b632011-05-11 09:30:34 -07001513 Vector<Tab> getChildren() {
1514 return mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001515 }
1516
1517 void resume() {
1518 if (mMainView != null) {
John Reck56c1fcf2011-08-17 10:15:16 -07001519 setupHwAcceleration(mMainView);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001520 mMainView.onResume();
1521 if (mSubView != null) {
1522 mSubView.onResume();
1523 }
1524 }
1525 }
1526
John Reck56c1fcf2011-08-17 10:15:16 -07001527 private void setupHwAcceleration(View web) {
1528 if (web == null) return;
1529 BrowserSettings settings = BrowserSettings.getInstance();
1530 if (settings.isHardwareAccelerated()) {
1531 web.setLayerType(View.LAYER_TYPE_NONE, null);
1532 } else {
1533 web.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
1534 }
1535 }
1536
Grace Kloba22ac16e2009-10-07 18:00:23 -07001537 void pause() {
1538 if (mMainView != null) {
1539 mMainView.onPause();
1540 if (mSubView != null) {
1541 mSubView.onPause();
1542 }
1543 }
1544 }
1545
1546 void putInForeground() {
John Reck8ee633f2011-08-09 16:00:35 -07001547 if (mInForeground) {
1548 return;
1549 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001550 mInForeground = true;
1551 resume();
Michael Kolb14612442011-06-24 13:06:29 -07001552 Activity activity = mWebViewController.getActivity();
1553 mMainView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001554 if (mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -07001555 mSubView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001556 }
Axesh R. Ajmerac6b5c322015-05-01 11:06:10 -07001557
Leon Scroggins1961ed22010-12-07 15:22:21 -05001558 mWebViewController.bookmarkedStatusHasChanged(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001559 }
1560
1561 void putInBackground() {
John Reck8ee633f2011-08-09 16:00:35 -07001562 if (!mInForeground) {
1563 return;
1564 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001565 mInForeground = false;
1566 pause();
1567 mMainView.setOnCreateContextMenuListener(null);
1568 if (mSubView != null) {
1569 mSubView.setOnCreateContextMenuListener(null);
1570 }
1571 }
1572
Michael Kolb8233fac2010-10-26 16:08:53 -07001573 boolean inForeground() {
1574 return mInForeground;
1575 }
1576
Grace Kloba22ac16e2009-10-07 18:00:23 -07001577 /**
1578 * Return the top window of this tab; either the subwindow if it is not
1579 * null or the main window.
1580 * @return The top window of this tab.
1581 */
1582 WebView getTopWindow() {
1583 if (mSubView != null) {
1584 return mSubView;
1585 }
1586 return mMainView;
1587 }
1588
1589 /**
1590 * Return the main window of this tab. Note: if a tab is freed in the
1591 * background, this can return null. It is only guaranteed to be
1592 * non-null for the current tab.
1593 * @return The main WebView of this tab.
1594 */
1595 WebView getWebView() {
1596 return mMainView;
1597 }
1598
Michael Kolba713ec82010-11-29 17:27:06 -08001599 void setViewContainer(View container) {
1600 mContainer = container;
1601 }
1602
Michael Kolb8233fac2010-10-26 16:08:53 -07001603 View getViewContainer() {
1604 return mContainer;
1605 }
1606
Grace Kloba22ac16e2009-10-07 18:00:23 -07001607 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001608 * Return whether private browsing is enabled for the main window of
1609 * this tab.
1610 * @return True if private browsing is enabled.
1611 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001612 boolean isPrivateBrowsingEnabled() {
John Reck502a3532011-08-16 14:21:46 -07001613 return mCurrentState.mIncognito;
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001614 }
1615
1616 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001617 * Return the subwindow of this tab or null if there is no subwindow.
1618 * @return The subwindow of this tab or null.
1619 */
1620 WebView getSubWebView() {
1621 return mSubView;
1622 }
1623
Michael Kolb1514bb72010-11-22 09:11:48 -08001624 void setSubWebView(WebView subView) {
1625 mSubView = subView;
1626 }
1627
Michael Kolb8233fac2010-10-26 16:08:53 -07001628 View getSubViewContainer() {
1629 return mSubViewContainer;
1630 }
1631
Michael Kolb1514bb72010-11-22 09:11:48 -08001632 void setSubViewContainer(View subViewContainer) {
1633 mSubViewContainer = subViewContainer;
1634 }
1635
Grace Kloba22ac16e2009-10-07 18:00:23 -07001636
1637 /**
1638 * @return The application id string
1639 */
1640 String getAppId() {
1641 return mAppId;
1642 }
1643
1644 /**
1645 * Set the application id string
1646 * @param id
1647 */
1648 void setAppId(String id) {
1649 mAppId = id;
1650 }
1651
Michael Kolbe28b3472011-08-04 16:54:31 -07001652 boolean closeOnBack() {
1653 return mCloseOnBack;
1654 }
1655
1656 void setCloseOnBack(boolean close) {
1657 mCloseOnBack = close;
1658 }
1659
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001660 boolean getDerivedFromIntent() {
1661 return mDerivedFromIntent;
1662 }
1663
1664 void setDerivedFromIntent(boolean derived) {
1665 mDerivedFromIntent = derived;
1666 }
1667
Grace Kloba22ac16e2009-10-07 18:00:23 -07001668 String getUrl() {
John Reck324d4402011-01-11 16:56:42 -08001669 return UrlUtils.filteredUrl(mCurrentState.mUrl);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001670 }
1671
Tarun Nainani8eb00912014-07-17 12:28:32 -07001672
1673 protected void onPageFinished() {
1674 mPageFinished = true;
Ze G Riande2a675c22015-06-03 11:15:24 -07001675 isDistillable();
Tarun Nainani8eb00912014-07-17 12:28:32 -07001676 }
1677
1678 public boolean getPageFinishedStatus() {
1679 return mPageFinished;
1680 }
1681
John Reck49a603c2011-03-03 09:33:05 -08001682 String getOriginalUrl() {
John Reckdb22ec42011-06-29 11:31:24 -07001683 if (mCurrentState.mOriginalUrl == null) {
1684 return getUrl();
John Reck49a603c2011-03-03 09:33:05 -08001685 }
John Reckdb22ec42011-06-29 11:31:24 -07001686 return UrlUtils.filteredUrl(mCurrentState.mOriginalUrl);
John Reck49a603c2011-03-03 09:33:05 -08001687 }
1688
Grace Kloba22ac16e2009-10-07 18:00:23 -07001689 /**
John Reck30c714c2010-12-16 17:30:34 -08001690 * Get the title of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001691 */
1692 String getTitle() {
John Reck30c714c2010-12-16 17:30:34 -08001693 return mCurrentState.mTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001694 }
1695
1696 /**
John Reck30c714c2010-12-16 17:30:34 -08001697 * Get the favicon of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001698 */
1699 Bitmap getFavicon() {
John Reck1cf4b792011-07-26 10:22:22 -07001700 if (mCurrentState.mFavicon != null) {
1701 return mCurrentState.mFavicon;
1702 }
1703 return getDefaultFavicon(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001704 }
1705
Pankaj Garg32e1b942015-06-03 18:13:24 -07001706 public boolean hasFavicon() {
1707 return mCurrentState.mFavicon != null;
1708 }
1709
John Recke969cc52010-12-21 17:24:43 -08001710 public boolean isBookmarkedSite() {
1711 return mCurrentState.mIsBookmarkedSite;
1712 }
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001713
Grace Kloba22ac16e2009-10-07 18:00:23 -07001714 /**
Steve Block08a6f0c2011-10-06 12:12:53 +01001715 * Sets the security state, clears the SSL certificate error and informs
1716 * the controller.
1717 */
Steve Block2466eff2011-10-03 15:33:09 +01001718 private void setSecurityState(SecurityState securityState) {
1719 mCurrentState.mSecurityState = securityState;
1720 mWebViewController.onUpdatedSecurityState(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001721 }
1722
1723 /**
Steve Block2466eff2011-10-03 15:33:09 +01001724 * @return The tab's security state.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001725 */
Steve Block2466eff2011-10-03 15:33:09 +01001726 SecurityState getSecurityState() {
1727 return mCurrentState.mSecurityState;
John Reck30c714c2010-12-16 17:30:34 -08001728 }
1729
1730 int getLoadProgress() {
1731 if (mInPageLoad) {
1732 return mPageLoadProgress;
1733 }
1734 return 100;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001735 }
1736
1737 /**
1738 * @return TRUE if onPageStarted is called while onPageFinished is not
1739 * called yet.
1740 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001741 boolean inPageLoad() {
1742 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001743 }
1744
Grace Kloba22ac16e2009-10-07 18:00:23 -07001745 /**
John Reck1cf4b792011-07-26 10:22:22 -07001746 * @return The Bundle with the tab's state if it can be saved, otherwise null
Grace Kloba22ac16e2009-10-07 18:00:23 -07001747 */
John Reck1cf4b792011-07-26 10:22:22 -07001748 public Bundle saveState() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001749 // If the WebView is null it means we ran low on memory and we already
1750 // stored the saved state in mSavedState.
1751 if (mMainView == null) {
John Reck1cf4b792011-07-26 10:22:22 -07001752 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001753 }
John Reck6c2e2f32011-08-22 13:41:23 -07001754
1755 if (TextUtils.isEmpty(mCurrentState.mUrl)) {
John Reck1cf4b792011-07-26 10:22:22 -07001756 return null;
John Reck24f18262011-06-17 14:47:20 -07001757 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001758
1759 mSavedState = new Bundle();
John Reck6c2e2f32011-08-22 13:41:23 -07001760 WebBackForwardList savedList = mMainView.saveState(mSavedState);
1761 if (savedList == null || savedList.getSize() == 0) {
1762 Log.w(LOGTAG, "Failed to save back/forward list for "
1763 + mCurrentState.mUrl);
1764 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001765
Michael Kolbc831b632011-05-11 09:30:34 -07001766 mSavedState.putLong(ID, mId);
John Reck30c714c2010-12-16 17:30:34 -08001767 mSavedState.putString(CURRURL, mCurrentState.mUrl);
1768 mSavedState.putString(CURRTITLE, mCurrentState.mTitle);
John Reck1cf4b792011-07-26 10:22:22 -07001769 mSavedState.putBoolean(INCOGNITO, mMainView.isPrivateBrowsingEnabled());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001770 if (mAppId != null) {
1771 mSavedState.putString(APPID, mAppId);
1772 }
Michael Kolbe28b3472011-08-04 16:54:31 -07001773 mSavedState.putBoolean(CLOSEFLAG, mCloseOnBack);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001774 // Remember the parent tab so the relationship can be restored.
Michael Kolbc831b632011-05-11 09:30:34 -07001775 if (mParent != null) {
1776 mSavedState.putLong(PARENTTAB, mParent.mId);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001777 }
John Reckb0a86db2011-05-24 14:05:58 -07001778 mSavedState.putBoolean(USERAGENT,
1779 mSettings.hasDesktopUseragent(getWebView()));
John Reck1cf4b792011-07-26 10:22:22 -07001780 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001781 }
1782
1783 /*
1784 * Restore the state of the tab.
1785 */
John Reck1cf4b792011-07-26 10:22:22 -07001786 private void restoreState(Bundle b) {
1787 mSavedState = b;
1788 if (mSavedState == null) {
1789 return;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001790 }
1791 // Restore the internal state even if the WebView fails to restore.
1792 // This will maintain the app id, original url and close-on-exit values.
Michael Kolbc831b632011-05-11 09:30:34 -07001793 mId = b.getLong(ID);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001794 mAppId = b.getString(APPID);
Michael Kolbe28b3472011-08-04 16:54:31 -07001795 mCloseOnBack = b.getBoolean(CLOSEFLAG);
John Reck8b9bb8b2012-03-08 13:19:40 -08001796 restoreUserAgent();
John Reck1cf4b792011-07-26 10:22:22 -07001797 String url = b.getString(CURRURL);
1798 String title = b.getString(CURRTITLE);
1799 boolean incognito = b.getBoolean(INCOGNITO);
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -07001800 mCurrentState = new PageState(mContext, incognito, url);
John Reck1cf4b792011-07-26 10:22:22 -07001801 mCurrentState.mTitle = title;
1802 synchronized (Tab.this) {
1803 if (mCapture != null) {
John Reck4eadc342011-10-31 14:04:10 -07001804 DataController.getInstance(mContext).loadThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07001805 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001806 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001807 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04001808
John Reck8b9bb8b2012-03-08 13:19:40 -08001809 private void restoreUserAgent() {
1810 if (mMainView == null || mSavedState == null) {
1811 return;
1812 }
1813 if (mSavedState.getBoolean(USERAGENT)
1814 != mSettings.hasDesktopUseragent(mMainView)) {
1815 mSettings.toggleDesktopUseragent(mMainView);
1816 }
1817 }
1818
Leon Scroggins1961ed22010-12-07 15:22:21 -05001819 public void updateBookmarkedStatus() {
John Recke969cc52010-12-21 17:24:43 -08001820 mDataController.queryBookmarkStatus(getUrl(), mIsBookmarkCallback);
Leon Scroggins1961ed22010-12-07 15:22:21 -05001821 }
1822
John Recke969cc52010-12-21 17:24:43 -08001823 private DataController.OnQueryUrlIsBookmark mIsBookmarkCallback
1824 = new DataController.OnQueryUrlIsBookmark() {
1825 @Override
1826 public void onQueryUrlIsBookmark(String url, boolean isBookmark) {
1827 if (mCurrentState.mUrl.equals(url)) {
1828 mCurrentState.mIsBookmarkedSite = isBookmark;
1829 mWebViewController.bookmarkedStatusHasChanged(Tab.this);
1830 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001831 }
John Recke969cc52010-12-21 17:24:43 -08001832 };
Michael Kolb1acef692011-03-08 14:12:06 -08001833
Michael Kolbeb95db42011-03-03 10:38:40 -08001834 public Bitmap getScreenshot() {
John Reck1cf4b792011-07-26 10:22:22 -07001835 synchronized (Tab.this) {
1836 return mCapture;
1837 }
Michael Kolbeb95db42011-03-03 10:38:40 -08001838 }
1839
John Reck541f55a2011-06-07 16:34:43 -07001840 public boolean isSnapshot() {
John Reck541f55a2011-06-07 16:34:43 -07001841 return false;
1842 }
1843
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001844 private static class SaveCallback implements ValueCallback<String> {
1845 boolean onReceiveValueCalled = false;
1846 private String mPath;
John Reck68234a92012-04-19 15:27:12 -07001847
1848 @Override
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001849 public void onReceiveValue(String path) {
1850 this.onReceiveValueCalled = true;
1851 this.mPath = path;
John Reck68234a92012-04-19 15:27:12 -07001852 synchronized (this) {
1853 notifyAll();
John Reck8cc92352011-07-06 17:41:52 -07001854 }
John Reck541f55a2011-06-07 16:34:43 -07001855 }
John Reck68234a92012-04-19 15:27:12 -07001856
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001857 public String getPath() {
1858 return mPath;
1859 }
John Reck68234a92012-04-19 15:27:12 -07001860 }
1861
1862 /**
1863 * Must be called on the UI thread
1864 */
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001865 public ContentValues createSnapshotValues(Bitmap bm) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001866 WebView web = getWebView();
John Reck68234a92012-04-19 15:27:12 -07001867 if (web == null) return null;
John Reckd8c74522011-06-14 08:45:00 -07001868 ContentValues values = new ContentValues();
1869 values.put(Snapshots.TITLE, mCurrentState.mTitle);
1870 values.put(Snapshots.URL, mCurrentState.mUrl);
John Reck68234a92012-04-19 15:27:12 -07001871 values.put(Snapshots.BACKGROUND, web.getPageBackgroundColor());
John Reck8cc92352011-07-06 17:41:52 -07001872 values.put(Snapshots.DATE_CREATED, System.currentTimeMillis());
1873 values.put(Snapshots.FAVICON, compressBitmap(getFavicon()));
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001874 values.put(Snapshots.THUMBNAIL, compressBitmap(bm));
John Reckd8c74522011-06-14 08:45:00 -07001875 return values;
John Reck541f55a2011-06-07 16:34:43 -07001876 }
1877
John Reck68234a92012-04-19 15:27:12 -07001878 /**
1879 * Probably want to call this on a background thread
1880 */
1881 public boolean saveViewState(ContentValues values) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001882 WebView web = getWebView();
John Reck68234a92012-04-19 15:27:12 -07001883 if (web == null) return false;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001884 String filename = UUID.randomUUID().toString();
John Reck68234a92012-04-19 15:27:12 -07001885 SaveCallback callback = new SaveCallback();
John Reck68234a92012-04-19 15:27:12 -07001886 try {
John Reck68234a92012-04-19 15:27:12 -07001887 synchronized (callback) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001888 web.saveViewState(filename, callback);
1889 callback.wait();
John Reck68234a92012-04-19 15:27:12 -07001890 }
John Reck68234a92012-04-19 15:27:12 -07001891 } catch (Exception e) {
1892 Log.w(LOGTAG, "Failed to save view state", e);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001893 String path = callback.getPath();
1894 if (path != null) {
1895 File file = mContext.getFileStreamPath(path);
1896 if (file.exists() && !file.delete()) {
1897 file.deleteOnExit();
1898 }
John Reck68234a92012-04-19 15:27:12 -07001899 }
1900 return false;
1901 }
Tarun Nainani8eb00912014-07-17 12:28:32 -07001902
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001903 String path = callback.getPath();
Tarun Nainani8eb00912014-07-17 12:28:32 -07001904 // could be that saving of file failed
1905 if (path == null) {
1906 return false;
1907 }
1908
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001909 File savedFile = new File(path);
1910 if (!savedFile.exists()) {
1911 return false;
John Reck68234a92012-04-19 15:27:12 -07001912 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001913 values.put(Snapshots.VIEWSTATE_PATH, path.substring(path.lastIndexOf('/') + 1));
1914 values.put(Snapshots.VIEWSTATE_SIZE, savedFile.length());
John Reck68234a92012-04-19 15:27:12 -07001915 return true;
1916 }
1917
John Reck8cc92352011-07-06 17:41:52 -07001918 public byte[] compressBitmap(Bitmap bitmap) {
1919 if (bitmap == null) {
1920 return null;
1921 }
1922 ByteArrayOutputStream stream = new ByteArrayOutputStream();
1923 bitmap.compress(CompressFormat.PNG, 100, stream);
1924 return stream.toByteArray();
1925 }
1926
John Reck26b18322011-06-21 13:08:58 -07001927 public void loadUrl(String url, Map<String, String> headers) {
1928 if (mMainView != null) {
Michael Kolba53c9892011-10-05 13:31:40 -07001929 mPageLoadProgress = INITIAL_PROGRESS;
Axesh R. Ajmerad04cad12015-08-12 12:02:44 -07001930 mCurrentState = new PageState(
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -07001931 mContext, mMainView.isPrivateBrowsingEnabled(), url);
John Reck26b18322011-06-21 13:08:58 -07001932 mMainView.loadUrl(url, headers);
1933 }
1934 }
1935
John Reck38b39652012-06-05 09:22:59 -07001936 public void disableUrlOverridingForLoad() {
1937 mDisableOverrideUrlLoading = true;
1938 }
1939
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001940 private void thumbnailUpdated() {
John Reck8ee633f2011-08-09 16:00:35 -07001941 mHandler.removeMessages(MSG_CAPTURE);
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001942
John Reck8ee633f2011-08-09 16:00:35 -07001943 TabControl tc = mWebViewController.getTabControl();
1944 if (tc != null) {
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001945 OnThumbnailUpdatedListener updateListener = tc.getOnThumbnailUpdatedListener();
John Reck8ee633f2011-08-09 16:00:35 -07001946 if (updateListener != null) {
1947 updateListener.onThumbnailUpdated(this);
1948 }
1949 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001950 }
1951
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001952 protected void capture() {
1953 if (mMainView == null || mCapture == null || !mMainView.isReady() ||
1954 mMainView.getContentWidth() <= 0 || mMainView.getContentHeight() <= 0 ||
1955 !mFirstVisualPixelPainted || mMainView.isShowingCrashView()) {
1956
1957 initCaptureBitmap();
1958 thumbnailUpdated();
1959 return;
1960 }
1961
1962 mMainView.getContentBitmapAsync((float) mCaptureWidth / mMainView.getWidth(), new Rect(),
1963 new ValueCallback<Bitmap>() {
1964 @Override
1965 public void onReceiveValue(Bitmap bitmap) {
1966 if (mCapture == null) {
1967 initCaptureBitmap();
1968 }
1969
1970 if (bitmap == null) {
1971 thumbnailUpdated();
1972 return;
1973 }
1974
1975 Canvas c = new Canvas(mCapture);
1976 mCapture.eraseColor(Color.WHITE);
1977 c.drawBitmap(bitmap, 0, 0, null);
1978
1979 // manually anti-alias the edges for the tilt
1980 c.drawRect(0, 0, 1, mCapture.getHeight(), sAlphaPaint);
1981 c.drawRect(mCapture.getWidth() - 1, 0, mCapture.getWidth(),
1982 mCapture.getHeight(), sAlphaPaint);
1983 c.drawRect(0, 0, mCapture.getWidth(), 1, sAlphaPaint);
1984 c.drawRect(0, mCapture.getHeight() - 1, mCapture.getWidth(),
1985 mCapture.getHeight(), sAlphaPaint);
1986 c.setBitmap(null);
1987
1988 persistThumbnail();
1989 thumbnailUpdated();
1990 }
1991 }
1992 );
John Reck1cf4b792011-07-26 10:22:22 -07001993 }
1994
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001995 @Override
1996 public void onNewPicture(WebView view, Picture picture) {
Michael Kolb9ef259a2011-07-12 15:33:08 -07001997 }
1998
John Reckef654f12011-07-12 16:42:08 -07001999 public boolean canGoBack() {
2000 return mMainView != null ? mMainView.canGoBack() : false;
2001 }
2002
2003 public boolean canGoForward() {
2004 return mMainView != null ? mMainView.canGoForward() : false;
2005 }
2006
2007 public void goBack() {
2008 if (mMainView != null) {
2009 mMainView.goBack();
2010 }
2011 }
2012
2013 public void goForward() {
2014 if (mMainView != null) {
2015 mMainView.goForward();
2016 }
2017 }
2018
John Reck1cf4b792011-07-26 10:22:22 -07002019 protected void persistThumbnail() {
John Reck4eadc342011-10-31 14:04:10 -07002020 DataController.getInstance(mContext).saveThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07002021 }
2022
2023 protected void deleteThumbnail() {
John Reck4eadc342011-10-31 14:04:10 -07002024 DataController.getInstance(mContext).deleteThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07002025 }
2026
John Reck4eadc342011-10-31 14:04:10 -07002027 void updateCaptureFromBlob(byte[] blob) {
John Reck1cf4b792011-07-26 10:22:22 -07002028 synchronized (Tab.this) {
2029 if (mCapture == null) {
2030 return;
2031 }
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002032 ByteBuffer buffer = ByteBuffer.wrap(blob);
Michael Kolbd837a112011-08-09 14:00:18 -07002033 try {
2034 mCapture.copyPixelsFromBuffer(buffer);
2035 } catch (RuntimeException rex) {
2036 Log.e(LOGTAG, "Load capture has mismatched sizes; buffer: "
2037 + buffer.capacity() + " blob: " + blob.length
2038 + "capture: " + mCapture.getByteCount());
2039 throw rex;
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002040 }
John Reck1cf4b792011-07-26 10:22:22 -07002041 }
2042 }
2043
John Reck52be4782011-08-26 15:37:29 -07002044 @Override
2045 public String toString() {
2046 StringBuilder builder = new StringBuilder(100);
2047 builder.append(mId);
2048 builder.append(") has parent: ");
2049 if (getParent() != null) {
2050 builder.append("true[");
2051 builder.append(getParent().getId());
2052 builder.append("]");
2053 } else {
2054 builder.append("false");
2055 }
2056 builder.append(", incog: ");
2057 builder.append(isPrivateBrowsingEnabled());
2058 if (!isPrivateBrowsingEnabled()) {
2059 builder.append(", title: ");
2060 builder.append(getTitle());
2061 builder.append(", url: ");
2062 builder.append(getUrl());
2063 }
2064 return builder.toString();
2065 }
2066
Ze G Riande2a675c22015-06-03 11:15:24 -07002067 // dertermines if the tab contains a dislled page
2068 public boolean isDistilled() {
2069 if (!BrowserCommandLine.hasSwitch("reader-mode")) {
2070 return false;
2071 }
2072 try {
2073 return DomDistillerUtils.isUrlDistilled(getUrl());
2074 } catch (Exception e) {
2075 return false;
2076 }
2077 }
2078
2079 //determines if the tab contains a distillable page
2080 public boolean isDistillable() {
2081 if (!BrowserCommandLine.hasSwitch("reader-mode")) {
2082 mIsDistillable = false;
2083 return mIsDistillable;
2084 }
2085 final ValueCallback<String> onIsDistillable = new ValueCallback<String>() {
2086 @Override
2087 public void onReceiveValue(String str) {
2088 mIsDistillable = Boolean.parseBoolean(str);
2089 }
2090 };
2091
2092 if (isDistilled()) {
2093 mIsDistillable = true;
2094 return mIsDistillable;
2095 }
2096
2097 try {
2098 DomDistillerUtils.isWebViewDistillable(getWebView(), onIsDistillable);
2099 } catch (Exception e) {
2100 mIsDistillable = false;
2101 }
2102
2103 return mIsDistillable;
2104 }
2105
2106 // Function that sets the mIsDistillable variable
2107 public void setIsDistillable(boolean value) {
2108 if (!BrowserCommandLine.hasSwitch("reader-mode")) {
2109 mIsDistillable = false;
2110 }
2111 mIsDistillable = value;
2112 }
2113
2114 // Function that returns the distilled url of the current url
2115 public String getDistilledUrl() {
2116 if (getUrl() != null) {
2117 return DomDistillerUtils.getDistilledUrl(getUrl());
2118 }
2119 return new String();
2120 }
2121
2122 // function that returns the non-distilled version of the current url
2123 public String getNonDistilledUrl() {
2124 if (getUrl() != null) {
2125 return DomDistillerUtils.getOriginalUrlFromDistilledUrl(getUrl());
2126 }
2127 return new String();
2128 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002129}