blob: d3ce38c614b95659991e7c804a805f1cc22e7e82 [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
Sagar Dhawanfb79bf42015-08-21 12:37:29 -0700556 // Clear the page state
557 mCurrentState = new PageState(mContext,
558 view.isPrivateBrowsingEnabled(), url);
559
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -0700560 if (ui instanceof BaseUi) {
561 titleBar = ((BaseUi)ui).getTitleBar();
562 if (titleBar != null) {
563 NavigationBarBase navBar = titleBar.getNavigationBar();
564 navBar.showCurrentFavicon(Tab.this); // Show the default Favicon while loading a new page
565 }
566 }
567
Site Mao61b68212015-07-16 10:56:31 -0700568 if (BaseUi.isUiLowPowerMode()) {
Pankaj Garg18aa0a12015-06-22 11:06:12 -0700569 return;
570 }
571
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700572 if (isPrivateBrowsingEnabled()) {
573 return;
574 }
575
576 if (!mFirstVisualPixelPainted) {
577 return;
578 }
579
580 final int idx = view.copyBackForwardList().getCurrentIndex();
581 boolean bitmapExists = view.hasSnapshot(idx);
582
583 int progress = 100;
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -0700584 if (titleBar != null) {
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700585 progress = titleBar.getProgressView().getProgressPercent();
586 }
587
588 if (bitmapExists && progress < 85) {
589 return;
590 }
591
592 int index = getCaptureIndex(view.getLastCommittedHistoryIndex());
Sagar Dhawan49f85cf2015-07-10 16:54:20 -0700593 view.captureSnapshot(index, null);
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700594 }
595
596 @Override
597 public void onHistoryItemCommit(WebView view, int index) {
Site Mao61b68212015-07-16 10:56:31 -0700598 if (BaseUi.isUiLowPowerMode()) {
Pankaj Garg18aa0a12015-06-22 11:06:12 -0700599 return;
600 }
601
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -0700602 // prevent snapshot tab from commiting any history
603 if (isSnapshot()) {
604 return;
605 }
606
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700607 mTabHistoryUpdateObservable.set(index);
Vivek Sekhar53ef8932015-06-18 16:51:43 -0700608 final int maxIdx = view.copyBackForwardList().getSize();
609 final WebView wv = view;
610 view.getSnapshotIds(new ValueCallback <List<Integer>>() {
611 @Override
612 public void onReceiveValue(List<Integer> ids) {
613 int currentTabIdx = mWebViewController.getTabControl().getCurrentPosition();
614 for (Integer id : ids) {
615 if (getTabIdxFromCaptureIdx(id) == currentTabIdx &&
616 getNavIdxFromCaptureIdx(id) >= maxIdx) {
617 wv.deleteSnapshot(id);
618 }
619 }
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700620 }
Vivek Sekhar53ef8932015-06-18 16:51:43 -0700621 });
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700622 }
Pankaj Garg62bc7912015-04-14 16:08:59 -0700623
624 @Override
625 public void onKeyboardStateChange(boolean popup) {
Vivek Sekhard4de6162015-07-21 15:01:45 -0700626 boolean keyboardWasShowing = isKeyboardShowing();
Sagar Dhawan33551ff2015-07-08 17:24:44 -0700627 mIsKeyboardUp = popup;
Vivek Sekhard4de6162015-07-21 15:01:45 -0700628 Controller controller = (Controller)mWebViewController;
629 BaseUi ui = (BaseUi) controller.getUi();
630 // lock the title bar
631 if (popup)
632 ui.getTitleBar().showTopControls(true);
633 if (keyboardWasShowing && popup)
634 ui.getTitleBar().enableTopControls(true);
Pankaj Garg62bc7912015-04-14 16:08:59 -0700635 if (BrowserSettings.getInstance().useFullscreen()) {
Pankaj Garg62bc7912015-04-14 16:08:59 -0700636 ui.forceDisableFullscreenMode(popup);
637 }
638 }
Vivek Sekharae3b1792015-08-03 12:26:41 -0700639
640 @Override
641 public void onAttachInterstitialPage(WebView mWebView) {
642 Controller controller = (Controller)mWebViewController;
643 BaseUi ui = (BaseUi) controller.getUi();
644 ui.getTitleBar().showTopControls(false);
645 }
646
647 @Override
648 public void onDetachInterstitialPage(WebView mWebView) {
649 Controller controller = (Controller)mWebViewController;
650 BaseUi ui = (BaseUi) controller.getUi();
651 ui.getTitleBar().enableTopControls(true);
652 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700653 };
654
John Reck1cf4b792011-07-26 10:22:22 -0700655 private void syncCurrentState(WebView view, String url) {
656 // Sync state (in case of stop/timeout)
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800657
Axesh R. Ajmerad04cad12015-08-12 12:02:44 -0700658
659
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800660 if (mReceivedError) {
661 mCurrentState.mUrl = url;
662 mCurrentState.mOriginalUrl = url;
Axesh R. Ajmerad04cad12015-08-12 12:02:44 -0700663 } else if (view.isPrivateBrowsingEnabled() &&
664 !TextUtils.isEmpty(url) &&
665 url.contains(Controller.INCOGNITO_URI)) {
666 mCurrentState.mUrl = mCurrentState.mOriginalUrl = "";
667 }
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -0700668
Axesh R. Ajmerad04cad12015-08-12 12:02:44 -0700669 else {
670 mCurrentState.mUrl = view.getUrl();
671 mCurrentState.mOriginalUrl = view.getOriginalUrl();
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800672 }
673
John Reck1cf4b792011-07-26 10:22:22 -0700674 if (mCurrentState.mUrl == null) {
John Reck8bcafc12011-08-29 16:43:02 -0700675 mCurrentState.mUrl = "";
John Reck1cf4b792011-07-26 10:22:22 -0700676 }
John Reck1cf4b792011-07-26 10:22:22 -0700677 mCurrentState.mTitle = view.getTitle();
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800678
679
John Reck1cf4b792011-07-26 10:22:22 -0700680 if (!URLUtil.isHttpsUrl(mCurrentState.mUrl)) {
681 // In case we stop when loading an HTTPS page from an HTTP page
682 // but before a provisional load occurred
Steve Block2466eff2011-10-03 15:33:09 +0100683 mCurrentState.mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
John Reck1cf4b792011-07-26 10:22:22 -0700684 }
John Reck502a3532011-08-16 14:21:46 -0700685 mCurrentState.mIncognito = view.isPrivateBrowsingEnabled();
John Reck1cf4b792011-07-26 10:22:22 -0700686 }
687
Pankaj Garg21dad562015-07-02 17:17:24 -0700688 public String getTouchIconUrl() {
689 return mTouchIconUrl;
690 }
Tarun Nainani8eb00912014-07-17 12:28:32 -0700691
Sagar Dhawan33551ff2015-07-08 17:24:44 -0700692 public boolean isKeyboardShowing() {
693 Controller controller = (Controller)mWebViewController;
694 return (mIsKeyboardUp || controller.getUi().isEditingUrl());
695 }
696
Tarun Nainani8eb00912014-07-17 12:28:32 -0700697 public boolean isTabFullScreen() {
698 return mFullScreen;
699 }
700
Vivek Sekharf96064b2014-07-28 16:32:34 -0700701 protected void setTabFullscreen(boolean fullScreen) {
Tarun Nainani8eb00912014-07-17 12:28:32 -0700702 Controller controller = (Controller)mWebViewController;
Sudheer Koganti24766882014-10-02 10:58:09 -0700703 controller.getUi().showFullscreen(fullScreen);
Tarun Nainani8eb00912014-07-17 12:28:32 -0700704 mFullScreen = fullScreen;
Vivek Sekharf96064b2014-07-28 16:32:34 -0700705 }
706
Sudheer Koganti24766882014-10-02 10:58:09 -0700707 public boolean exitFullscreen() {
708 if (mFullScreen) {
709 Controller controller = (Controller)mWebViewController;
710 controller.getUi().showFullscreen(false);
711 if (getWebView() != null)
712 getWebView().exitFullscreen();
713 mFullScreen = false;
714 return true;
715 }
716 return false;
717 }
718
719
720
721
Grace Kloba22ac16e2009-10-07 18:00:23 -0700722 // -------------------------------------------------------------------------
723 // WebChromeClient implementation for the main WebView
724 // -------------------------------------------------------------------------
725
726 private final WebChromeClient mWebChromeClient = new WebChromeClient() {
727 // Helper method to create a new tab or sub window.
728 private void createWindow(final boolean dialog, final Message msg) {
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700729 this.createWindow(dialog, msg, null, false);
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700730 }
731
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700732 private void createWindow(final boolean dialog, final Message msg, final String url,
733 final boolean opener_suppressed) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700734 WebView.WebViewTransport transport =
735 (WebView.WebViewTransport) msg.obj;
736 if (dialog) {
737 createSubWindow();
Michael Kolb8233fac2010-10-26 16:08:53 -0700738 mWebViewController.attachSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700739 transport.setWebView(mSubView);
740 } else {
Pankaj Garg21dad562015-07-02 17:17:24 -0700741 capture();
742
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700743 final Tab newTab = mWebViewController.openTab(url,
John Reck5949c662011-05-27 09:52:29 -0700744 Tab.this, true, true);
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700745 // This is special case for rendering links on a webpage in
746 // a new tab. If opener is suppressed, the WebContents created
747 // by the content layer are not fully initialized. This check
748 // will prevent content layer from overriding WebContents
749 // created by new tab with the uninitialized instance.
750 if (!opener_suppressed) {
751 transport.setWebView(newTab.getWebView());
752 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700753 }
754 msg.sendToTarget();
755 }
756
757 @Override
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700758 public void toggleFullscreenModeForTab(boolean enterFullscreen) {
759 if (mWebViewController instanceof Controller) {
Vivek Sekharf96064b2014-07-28 16:32:34 -0700760 setTabFullscreen(enterFullscreen);
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700761 }
762 }
763
764 @Override
Tarun Nainani8eb00912014-07-17 12:28:32 -0700765 public void onOffsetsForFullscreenChanged(float topControlsOffsetYPix,
766 float contentOffsetYPix,
767 float overdrawBottomHeightPix) {
768 if (mWebViewController instanceof Controller) {
Vivek Sekhar8ee3abb2014-07-14 12:32:05 -0700769 Controller controller = (Controller)mWebViewController;
Tarun Nainani8eb00912014-07-17 12:28:32 -0700770 controller.getUi().translateTitleBar(topControlsOffsetYPix);
Vivek Sekhar11b40062015-06-24 11:49:04 -0700771 // Resize the viewport if top controls is not visible
Vivek Sekhar2ee19a32015-07-02 17:03:57 -0700772 if (mMainView != null &&
773 (topControlsOffsetYPix == 0.0f || contentOffsetYPix == 0.0f))
Vivek Sekhar11b40062015-06-24 11:49:04 -0700774 ((BrowserWebView)mMainView).enableTopControls(
775 (topControlsOffsetYPix == 0.0f) ? true : false);
Vivek Sekhar8ee3abb2014-07-14 12:32:05 -0700776 }
777 }
778
779 @Override
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700780 public boolean isTabFullScreen() {
Vivek Sekharf96064b2014-07-28 16:32:34 -0700781 return mFullScreen;
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700782 }
783
784 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700785 public boolean onCreateWindow(WebView view, final boolean dialog,
786 final boolean userGesture, final Message resultMsg) {
787 // only allow new window or sub window for the foreground case
788 if (!mInForeground) {
789 return false;
790 }
791 // Short-circuit if we can't create any more tabs or sub windows.
792 if (dialog && mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -0700793 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700794 .setTitle(R.string.too_many_subwindows_dialog_title)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200795 .setIconAttribute(android.R.attr.alertDialogIcon)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700796 .setMessage(R.string.too_many_subwindows_dialog_message)
797 .setPositiveButton(R.string.ok, null)
798 .show();
799 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700800 } else if (!mWebViewController.getTabControl().canCreateNewTab()) {
Michael Kolb14612442011-06-24 13:06:29 -0700801 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700802 .setTitle(R.string.too_many_windows_dialog_title)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200803 .setIconAttribute(android.R.attr.alertDialogIcon)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700804 .setMessage(R.string.too_many_windows_dialog_message)
805 .setPositiveButton(R.string.ok, null)
806 .show();
807 return false;
808 }
809
810 // Short-circuit if this was a user gesture.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800811 if (userGesture || !mSettings.blockPopupWindows()) {
Tarun Nainani4f5137d2015-04-16 17:26:18 -0700812 WebView.WebViewTransport transport =
813 (WebView.WebViewTransport) resultMsg.obj;
814 CreateWindowParams windowParams = transport.getCreateWindowParams();
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700815 if (windowParams.mOpenerSuppressed) {
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700816 createWindow(dialog, resultMsg, windowParams.mURL, true);
817 // This is special case for rendering links on a webpage in
818 // a new tab. If opener is suppressed, the WebContents created
819 // by the content layer are not fully initialized. Returning false
820 // will prevent content layer from overriding WebContents
821 // created by new tab with the uninitialized instance.
822 return false;
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700823 }
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700824
825 createWindow(dialog, resultMsg);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700826 return true;
827 }
828
Tarun Nainani4f5137d2015-04-16 17:26:18 -0700829 createWindow(dialog, resultMsg);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700830 return true;
831 }
832
833 @Override
Patrick Scotteb5061b2009-11-18 15:00:30 -0500834 public void onRequestFocus(WebView view) {
835 if (!mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700836 mWebViewController.switchToTab(Tab.this);
Patrick Scotteb5061b2009-11-18 15:00:30 -0500837 }
838 }
839
840 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700841 public void onCloseWindow(WebView window) {
Michael Kolbc831b632011-05-11 09:30:34 -0700842 if (mParent != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700843 // JavaScript can only close popup window.
844 if (mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700845 mWebViewController.switchToTab(mParent);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700846 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700847 mWebViewController.closeTab(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700848 }
849 }
850
851 @Override
852 public void onProgressChanged(WebView view, int newProgress) {
John Reck30c714c2010-12-16 17:30:34 -0800853 mPageLoadProgress = newProgress;
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800854 if (newProgress == 100) {
855 mInPageLoad = false;
856 }
John Reck30c714c2010-12-16 17:30:34 -0800857 mWebViewController.onProgressChanged(Tab.this);
Michael Kolb72864272012-05-03 15:42:15 -0700858 if (mUpdateThumbnail && newProgress == 100) {
859 mUpdateThumbnail = false;
860 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700861 }
862
863 @Override
Leon Scroggins21d9b902010-03-11 09:33:11 -0500864 public void onReceivedTitle(WebView view, final String title) {
John Reck30c714c2010-12-16 17:30:34 -0800865 mCurrentState.mTitle = title;
Michael Kolb8233fac2010-10-26 16:08:53 -0700866 mWebViewController.onReceivedTitle(Tab.this, title);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700867 }
868
869 @Override
870 public void onReceivedIcon(WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -0800871 mCurrentState.mFavicon = icon;
Michael Kolb8233fac2010-10-26 16:08:53 -0700872 mWebViewController.onFavicon(Tab.this, view, icon);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700873 }
874
875 @Override
876 public void onReceivedTouchIconUrl(WebView view, String url,
877 boolean precomposed) {
Michael Kolb14612442011-06-24 13:06:29 -0700878 final ContentResolver cr = mContext.getContentResolver();
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400879 // Let precomposed icons take precedence over non-composed
880 // icons.
881 if (precomposed && mTouchIconLoader != null) {
882 mTouchIconLoader.cancel(false);
883 mTouchIconLoader = null;
884 }
885 // Have only one async task at a time.
886 if (mTouchIconLoader == null) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700887 mTouchIconLoader = new DownloadTouchIcon(Tab.this,
Michael Kolb14612442011-06-24 13:06:29 -0700888 mContext, cr, view);
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400889 mTouchIconLoader.execute(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700890 }
Pankaj Garg21dad562015-07-02 17:17:24 -0700891 mTouchIconUrl = url;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700892 }
893
894 @Override
895 public void onShowCustomView(View view,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800896 CustomViewCallback callback) {
Michael Kolb14612442011-06-24 13:06:29 -0700897 Activity activity = mWebViewController.getActivity();
898 if (activity != null) {
899 onShowCustomView(view, activity.getRequestedOrientation(), callback);
900 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400901 }
902
903 @Override
904 public void onShowCustomView(View view, int requestedOrientation,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800905 CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700906 if (mInForeground) mWebViewController.showCustomView(Tab.this, view,
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400907 requestedOrientation, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700908 }
909
910 @Override
911 public void onHideCustomView() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700912 if (mInForeground) mWebViewController.hideCustomView();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700913 }
914
915 /**
916 * The origin has exceeded its database quota.
917 * @param url the URL that exceeded the quota
918 * @param databaseIdentifier the identifier of the database on which the
919 * transaction that caused the quota overflow was run
920 * @param currentQuota the current quota for the origin.
921 * @param estimatedSize the estimated size of the database.
922 * @param totalUsedQuota is the sum of all origins' quota.
923 * @param quotaUpdater The callback to run when a decision to allow or
924 * deny quota has been made. Don't forget to call this!
925 */
926 @Override
927 public void onExceededDatabaseQuota(String url,
928 String databaseIdentifier, long currentQuota, long estimatedSize,
929 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -0700930 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -0700931 .onExceededDatabaseQuota(url, databaseIdentifier,
932 currentQuota, estimatedSize, totalUsedQuota,
933 quotaUpdater);
934 }
935
936 /**
937 * The Application Cache has exceeded its max size.
938 * @param spaceNeeded is the amount of disk space that would be needed
939 * in order for the last appcache operation to succeed.
940 * @param totalUsedQuota is the sum of all origins' quota.
941 * @param quotaUpdater A callback to inform the WebCore thread that a
942 * new app cache size is available. This callback must always
943 * be executed at some point to ensure that the sleeping
944 * WebCore thread is woken up.
945 */
946 @Override
947 public void onReachedMaxAppCacheSize(long spaceNeeded,
948 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -0700949 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -0700950 .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota,
951 quotaUpdater);
952 }
953
Ben Murdoch65acc352009-11-19 18:16:04 +0000954 /* Adds a JavaScript error message to the system log and if the JS
955 * console is enabled in the about:debug options, to that console
956 * also.
Ben Murdochc42addf2010-01-28 15:19:59 +0000957 * @param consoleMessage the message object.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700958 */
959 @Override
Ben Murdochc42addf2010-01-28 15:19:59 +0000960 public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Jeff Hamilton47654f42010-09-07 09:57:51 -0500961 // Don't log console messages in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -0700962 if (isPrivateBrowsingEnabled()) return true;
Jeff Hamilton47654f42010-09-07 09:57:51 -0500963
Ben Murdochc42addf2010-01-28 15:19:59 +0000964 String message = "Console: " + consoleMessage.message() + " "
965 + consoleMessage.sourceId() + ":"
966 + consoleMessage.lineNumber();
967
968 switch (consoleMessage.messageLevel()) {
969 case TIP:
970 Log.v(CONSOLE_LOGTAG, message);
971 break;
972 case LOG:
973 Log.i(CONSOLE_LOGTAG, message);
974 break;
975 case WARNING:
976 Log.w(CONSOLE_LOGTAG, message);
977 break;
978 case ERROR:
979 Log.e(CONSOLE_LOGTAG, message);
980 break;
981 case DEBUG:
982 Log.d(CONSOLE_LOGTAG, message);
983 break;
984 }
985
986 return true;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700987 }
988
989 /**
990 * Ask the browser for an icon to represent a <video> element.
991 * This icon will be used if the Web page did not specify a poster attribute.
992 * @return Bitmap The icon or null if no such icon is available.
993 */
994 @Override
995 public Bitmap getDefaultVideoPoster() {
996 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700997 return mWebViewController.getDefaultVideoPoster();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700998 }
999 return null;
1000 }
1001
1002 /**
1003 * Ask the host application for a custom progress view to show while
1004 * a <video> is loading.
1005 * @return View The progress view.
1006 */
1007 @Override
1008 public View getVideoLoadingProgressView() {
1009 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001010 return mWebViewController.getVideoLoadingProgressView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001011 }
1012 return null;
1013 }
1014
1015 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00001016 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001017 if (mInForeground) {
Ben Murdoch8cad4132012-01-11 10:56:43 +00001018 mWebViewController.openFileChooser(uploadMsg, acceptType, capture);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001019 } else {
1020 uploadMsg.onReceiveValue(null);
1021 }
1022 }
1023
Vivek Sekharb54614f2014-05-01 19:03:37 -07001024 @Override
1025 public void showFileChooser(ValueCallback<String[]> uploadFilePaths, String acceptTypes,
1026 boolean capture) {
1027 if (mInForeground) {
1028 mWebViewController.showFileChooser(uploadFilePaths, acceptTypes, capture);
1029 } else {
1030 uploadFilePaths.onReceiveValue(null);
1031 }
1032 }
1033
Grace Kloba22ac16e2009-10-07 18:00:23 -07001034 /**
1035 * Deliver a list of already-visited URLs
1036 */
1037 @Override
1038 public void getVisitedHistory(final ValueCallback<String[]> callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001039 mWebViewController.getVisitedHistory(callback);
1040 }
Ben Murdoch8029a772010-11-16 11:58:21 +00001041
1042 @Override
1043 public void setupAutoFill(Message message) {
1044 // Prompt the user to set up their profile.
1045 final Message msg = message;
Michael Kolb14612442011-06-24 13:06:29 -07001046 AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
1047 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
Ben Murdoch1d676b62011-01-17 12:54:24 +00001048 Context.LAYOUT_INFLATER_SERVICE);
1049 final View layout = inflater.inflate(R.layout.setup_autofill_dialog, null);
1050
1051 builder.setView(layout)
1052 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1053 @Override
1054 public void onClick(DialogInterface dialog, int id) {
1055 CheckBox disableAutoFill = (CheckBox) layout.findViewById(
1056 R.id.setup_autofill_dialog_disable_autofill);
1057
1058 if (disableAutoFill.isChecked()) {
1059 // Disable autofill and show a toast with how to turn it on again.
John Reck35e9dd62011-04-25 09:01:54 -07001060 mSettings.setAutofillEnabled(false);
Michael Kolb14612442011-06-24 13:06:29 -07001061 Toast.makeText(mContext,
Ben Murdoch1d676b62011-01-17 12:54:24 +00001062 R.string.autofill_setup_dialog_negative_toast,
1063 Toast.LENGTH_LONG).show();
1064 } else {
1065 // Take user to the AutoFill profile editor. When they return,
1066 // we will send the message that we pass here which will trigger
1067 // the form to get filled out with their new profile.
1068 mWebViewController.setupAutoFill(msg);
1069 }
1070 }
1071 })
1072 .setNegativeButton(R.string.cancel, null)
1073 .show();
Ben Murdoch8029a772010-11-16 11:58:21 +00001074 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001075 };
1076
1077 // -------------------------------------------------------------------------
1078 // WebViewClient implementation for the sub window
1079 // -------------------------------------------------------------------------
1080
1081 // Subclass of WebViewClient used in subwindows to notify the main
1082 // WebViewClient of certain WebView activities.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001083 private static class SubWindowClient extends WebViewClient {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001084 // The main WebViewClient.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001085 private final WebViewClient mClient;
Michael Kolb8233fac2010-10-26 16:08:53 -07001086 private final WebViewController mController;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001087
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001088 SubWindowClient(WebViewClient client, WebViewController controller) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001089 mClient = client;
Michael Kolb8233fac2010-10-26 16:08:53 -07001090 mController = controller;
Leon Scroggins III211ba542010-04-19 13:21:13 -04001091 }
1092 @Override
1093 public void onPageStarted(WebView view, String url, Bitmap favicon) {
1094 // Unlike the others, do not call mClient's version, which would
1095 // change the progress bar. However, we do want to remove the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001096 // find or select dialog.
Michael Kolb8233fac2010-10-26 16:08:53 -07001097 mController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001098 }
1099 @Override
1100 public void doUpdateVisitedHistory(WebView view, String url,
1101 boolean isReload) {
1102 mClient.doUpdateVisitedHistory(view, url, isReload);
1103 }
1104 @Override
1105 public boolean shouldOverrideUrlLoading(WebView view, String url) {
1106 return mClient.shouldOverrideUrlLoading(view, url);
1107 }
1108 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -07001109 public void onReceivedHttpAuthRequest(WebView view,
1110 HttpAuthHandler handler, String host, String realm) {
1111 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
1112 }
1113 @Override
1114 public void onFormResubmission(WebView view, Message dontResend,
1115 Message resend) {
1116 mClient.onFormResubmission(view, dontResend, resend);
1117 }
1118 @Override
1119 public void onReceivedError(WebView view, int errorCode,
1120 String description, String failingUrl) {
1121 mClient.onReceivedError(view, errorCode, description, failingUrl);
1122 }
1123 @Override
1124 public boolean shouldOverrideKeyEvent(WebView view,
1125 android.view.KeyEvent event) {
1126 return mClient.shouldOverrideKeyEvent(view, event);
1127 }
1128 @Override
1129 public void onUnhandledKeyEvent(WebView view,
1130 android.view.KeyEvent event) {
1131 mClient.onUnhandledKeyEvent(view, event);
1132 }
1133 }
1134
1135 // -------------------------------------------------------------------------
1136 // WebChromeClient implementation for the sub window
1137 // -------------------------------------------------------------------------
1138
1139 private class SubWindowChromeClient extends WebChromeClient {
1140 // The main WebChromeClient.
1141 private final WebChromeClient mClient;
1142
1143 SubWindowChromeClient(WebChromeClient client) {
1144 mClient = client;
1145 }
1146 @Override
1147 public void onProgressChanged(WebView view, int newProgress) {
1148 mClient.onProgressChanged(view, newProgress);
1149 }
1150 @Override
1151 public boolean onCreateWindow(WebView view, boolean dialog,
1152 boolean userGesture, android.os.Message resultMsg) {
1153 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
1154 }
1155 @Override
1156 public void onCloseWindow(WebView window) {
1157 if (window != mSubView) {
1158 Log.e(LOGTAG, "Can't close the window");
1159 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001160 mWebViewController.dismissSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001161 }
1162 }
1163
1164 // -------------------------------------------------------------------------
1165
1166 // Construct a new tab
Michael Kolb7bcafde2011-05-09 13:55:59 -07001167 Tab(WebViewController wvcontroller, WebView w) {
John Reck1cf4b792011-07-26 10:22:22 -07001168 this(wvcontroller, w, null);
1169 }
1170
1171 Tab(WebViewController wvcontroller, Bundle state) {
1172 this(wvcontroller, null, state);
1173 }
1174
1175 Tab(WebViewController wvcontroller, WebView w, Bundle state) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001176 mWebViewController = wvcontroller;
Michael Kolb14612442011-06-24 13:06:29 -07001177 mContext = mWebViewController.getContext();
John Reck35e9dd62011-04-25 09:01:54 -07001178 mSettings = BrowserSettings.getInstance();
Michael Kolb14612442011-06-24 13:06:29 -07001179 mDataController = DataController.getInstance(mContext);
1180 mCurrentState = new PageState(mContext, w != null
John Reck847b5322011-04-14 17:02:18 -07001181 ? w.isPrivateBrowsingEnabled() : false);
Tarun Nainani8084c822014-06-25 13:38:06 -07001182 setTimeStamp();
Michael Kolb8233fac2010-10-26 16:08:53 -07001183 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001184 mInForeground = false;
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001185 mWebViewDestroyedByMemoryMonitor = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001186
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001187 mDownloadListener = new BrowserDownloadListener() {
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001188 public void onDownloadStart(String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001189 String contentDisposition, String mimetype, String referer,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001190 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001191 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001192 mimetype, referer, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001193 }
1194 };
1195
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001196 mCaptureWidth = mContext.getResources().getDimensionPixelSize(R.dimen.tab_thumbnail_width);
1197 mCaptureHeight =mContext.getResources().getDimensionPixelSize(R.dimen.tab_thumbnail_height);
1198
1199 initCaptureBitmap();
1200
John Reck1cf4b792011-07-26 10:22:22 -07001201 restoreState(state);
John Reck52be4782011-08-26 15:37:29 -07001202 if (getId() == -1) {
1203 mId = TabControl.getNextId();
1204 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001205 setWebView(w);
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -07001206
1207 UI ui = ((Controller)mWebViewController).getUi();
1208 if (ui instanceof BaseUi) {
1209 TitleBar titleBar = ((BaseUi)ui).getTitleBar();
1210 if (titleBar != null) {
1211 NavigationBarBase navBar = titleBar.getNavigationBar();
1212 navBar.showCurrentFavicon(this); // Show the default Favicon while loading a new page
1213 }
1214 }
1215
Michael Kolb9ef259a2011-07-12 15:33:08 -07001216 mHandler = new Handler() {
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001217 @Override
Michael Kolb9ef259a2011-07-12 15:33:08 -07001218 public void handleMessage(Message m) {
John Reck1cf4b792011-07-26 10:22:22 -07001219 switch (m.what) {
1220 case MSG_CAPTURE:
1221 capture();
1222 break;
1223 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001224 }
1225 };
Pankaj Garg1c13cab2015-05-12 11:52:17 -07001226
1227 mFirstPixelObservable = new Observable();
1228 mFirstPixelObservable.set(false);
1229 mTabHistoryUpdateObservable = new Observable();
John Reck1cf4b792011-07-26 10:22:22 -07001230 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001231
Pankaj Gargb4b4f012015-08-04 16:19:14 -07001232 public void initCaptureBitmap() {
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001233 mCapture = Bitmap.createBitmap(mCaptureWidth, mCaptureHeight, Bitmap.Config.RGB_565);
1234 mCapture.eraseColor(Color.WHITE);
Michael Kolb72864272012-05-03 15:42:15 -07001235 }
1236
Mathew Inwoode09305e2011-09-02 12:03:26 +01001237 /**
1238 * This is used to get a new ID when the tab has been preloaded, before it is displayed and
1239 * added to TabControl. Preloaded tabs can be created before restoreInstanceState, leading
1240 * to overlapping IDs between the preloaded and restored tabs.
1241 */
1242 public void refreshIdAfterPreload() {
1243 mId = TabControl.getNextId();
1244 }
1245
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001246 public void setController(WebViewController ctl) {
1247 mWebViewController = ctl;
1248
John Reck1cf4b792011-07-26 10:22:22 -07001249 if (mWebViewController.shouldCaptureThumbnails()) {
1250 synchronized (Tab.this) {
1251 if (mCapture == null) {
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001252 initCaptureBitmap();
1253 if (mInForeground && !mHandler.hasMessages(MSG_CAPTURE)) {
1254 mHandler.sendEmptyMessageDelayed(MSG_CAPTURE, CAPTURE_DELAY);
John Reck1cf4b792011-07-26 10:22:22 -07001255 }
1256 }
1257 }
1258 } else {
1259 synchronized (Tab.this) {
1260 mCapture = null;
1261 deleteThumbnail();
1262 }
1263 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001264 }
1265
Michael Kolbc831b632011-05-11 09:30:34 -07001266 public long getId() {
1267 return mId;
1268 }
1269
Michael Kolb91911a22012-01-17 11:21:25 -08001270 void setWebView(WebView w) {
1271 setWebView(w, true);
1272 }
1273
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001274 public boolean isNativeActive(){
1275 if (mMainView == null)
1276 return false;
1277 return true;
1278 }
1279
1280 public void setTimeStamp(){
1281 Date d = new Date();
1282 timestamp = (new Timestamp(d.getTime()));
1283 }
1284
1285 public Timestamp getTimestamp() {
1286 return timestamp;
1287 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001288 /**
1289 * Sets the WebView for this tab, correctly removing the old WebView from
1290 * the container view.
1291 */
Michael Kolb91911a22012-01-17 11:21:25 -08001292 void setWebView(WebView w, boolean restore) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001293 if (mMainView == w) {
1294 return;
1295 }
Michael Kolba713ec82010-11-29 17:27:06 -08001296
Michael Kolba713ec82010-11-29 17:27:06 -08001297 mWebViewController.onSetWebView(this, w);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001298
John Reck1cf4b792011-07-26 10:22:22 -07001299 if (mMainView != null) {
John Reck8ee633f2011-08-09 16:00:35 -07001300 mMainView.setPictureListener(null);
John Reck1cf4b792011-07-26 10:22:22 -07001301 if (w != null) {
1302 syncCurrentState(w, null);
1303 } else {
Panos Thomasa9a5a582014-03-18 19:20:08 -07001304 mCurrentState = new PageState(mContext, mMainView.isPrivateBrowsingEnabled());
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001305
1306 if (mWebViewDestroyedByMemoryMonitor) {
1307 /*
1308 * If tab was destroyed as a result of the MemoryMonitor
1309 * then we need to restore the state properties
1310 * from the old WebView (mMainView)
1311 */
1312 syncCurrentState(mMainView, null);
1313 mWebViewDestroyedByMemoryMonitor = false;
1314 }
John Reck1cf4b792011-07-26 10:22:22 -07001315 }
1316 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001317 // set the new one
1318 mMainView = w;
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001319
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001320 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001321 if (mMainView != null) {
1322 mMainView.setWebViewClient(mWebViewClient);
1323 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001324 // Attach DownloadManager so that downloads can start in an active
1325 // or a non-active window. This can happen when going to a site that
1326 // does a redirect after a period of time. The user could have
1327 // switched to another tab while waiting for the download to start.
1328 mMainView.setDownloadListener(mDownloadListener);
John Reck8ee633f2011-08-09 16:00:35 -07001329 TabControl tc = mWebViewController.getTabControl();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001330 if (tc != null /*&& tc.getOnThumbnailUpdatedListener() != null*/) {
John Reck8ee633f2011-08-09 16:00:35 -07001331 mMainView.setPictureListener(this);
1332 }
Michael Kolb91911a22012-01-17 11:21:25 -08001333 if (restore && (mSavedState != null)) {
John Reck8b9bb8b2012-03-08 13:19:40 -08001334 restoreUserAgent();
John Reck6c2e2f32011-08-22 13:41:23 -07001335 WebBackForwardList restoredState
1336 = mMainView.restoreState(mSavedState);
1337 if (restoredState == null || restoredState.getSize() == 0) {
1338 Log.w(LOGTAG, "Failed to restore WebView state!");
1339 loadUrl(mCurrentState.mOriginalUrl, null);
1340 }
John Reck1cf4b792011-07-26 10:22:22 -07001341 mSavedState = null;
1342 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001343 }
1344 }
1345
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001346 public void destroyThroughMemoryMonitor() {
1347 mWebViewDestroyedByMemoryMonitor = true;
1348 destroy();
1349 }
1350
Grace Kloba22ac16e2009-10-07 18:00:23 -07001351 /**
1352 * Destroy the tab's main WebView and subWindow if any
1353 */
1354 void destroy() {
Tarun Nainani2c1dd7c2014-07-05 16:40:12 -07001355
Matthew Huib7f2e9c2014-04-16 11:12:37 -04001356 if (mPostponeDestroy) {
1357 mShouldDestroy = true;
1358 return;
1359 }
1360 mShouldDestroy = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001361 if (mMainView != null) {
1362 dismissSubWindow();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001363 // save the WebView to call destroy() after detach it from the tab
Vivek Sekhar53ef8932015-06-18 16:51:43 -07001364 final WebView webView = mMainView;
Vivek Sekhar2ee19a32015-07-02 17:03:57 -07001365 setWebView(null);
Site Mao61b68212015-07-16 10:56:31 -07001366 if (!mWebViewDestroyedByMemoryMonitor && !BaseUi.isUiLowPowerMode()) {
Vivek Sekhar2ee19a32015-07-02 17:03:57 -07001367 // Tabs can be reused with new instance of WebView so delete the snapshots
Vivek Sekhar53ef8932015-06-18 16:51:43 -07001368 webView.getSnapshotIds(new ValueCallback<List<Integer>>() {
1369 @Override
1370 public void onReceiveValue(List<Integer> ids) {
1371 int currentTabIdx = mWebViewController.getTabControl().getCurrentPosition();
1372 for (Integer id : ids) {
1373 if (getTabIdxFromCaptureIdx(id) == currentTabIdx) {
1374 webView.deleteSnapshot(id);
1375 }
1376 }
Vivek Sekhar53ef8932015-06-18 16:51:43 -07001377 webView.destroy();
Pankaj Garg1c13cab2015-05-12 11:52:17 -07001378 }
Vivek Sekhar53ef8932015-06-18 16:51:43 -07001379 });
Vivek Sekhar2ee19a32015-07-02 17:03:57 -07001380 } else {
1381 webView.destroy();
Pankaj Garg1c13cab2015-05-12 11:52:17 -07001382 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001383 }
1384 }
1385
Matthew Huib7f2e9c2014-04-16 11:12:37 -04001386 private boolean mPostponeDestroy = false;
1387 private boolean mShouldDestroy = false;
1388
1389 public void postponeDestroy() {
1390 mPostponeDestroy = true;
1391 }
1392
1393 public void performPostponedDestroy() {
1394 mPostponeDestroy = false;
1395 if (mShouldDestroy) {
1396 destroy();
1397 }
1398 }
1399
Grace Kloba22ac16e2009-10-07 18:00:23 -07001400 /**
1401 * Remove the tab from the parent
1402 */
1403 void removeFromTree() {
1404 // detach the children
Michael Kolbc831b632011-05-11 09:30:34 -07001405 if (mChildren != null) {
1406 for(Tab t : mChildren) {
1407 t.setParent(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001408 }
1409 }
1410 // remove itself from the parent list
Michael Kolbc831b632011-05-11 09:30:34 -07001411 if (mParent != null) {
1412 mParent.mChildren.remove(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001413 }
Tarun Nainani8eb00912014-07-17 12:28:32 -07001414
1415 mCapture = null;
John Reck1cf4b792011-07-26 10:22:22 -07001416 deleteThumbnail();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001417 }
1418
1419 /**
1420 * Create a new subwindow unless a subwindow already exists.
1421 * @return True if a new subwindow was created. False if one already exists.
1422 */
1423 boolean createSubWindow() {
1424 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001425 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001426 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001427 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001428 mSubView.setWebChromeClient(new SubWindowChromeClient(
1429 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001430 // Set a different DownloadListener for the mSubView, since it will
1431 // just need to dismiss the mSubView, rather than close the Tab
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001432 mSubView.setDownloadListener(new BrowserDownloadListener() {
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001433 public void onDownloadStart(String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001434 String contentDisposition, String mimetype, String referer,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001435 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001436 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001437 contentDisposition, mimetype, referer, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001438 if (mSubView.copyBackForwardList().getSize() == 0) {
1439 // This subwindow was opened for the sole purpose of
1440 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001441 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001442 }
1443 }
1444 });
Michael Kolb14612442011-06-24 13:06:29 -07001445 mSubView.setOnCreateContextMenuListener(mWebViewController.getActivity());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001446 return true;
1447 }
1448 return false;
1449 }
1450
1451 /**
1452 * Dismiss the subWindow for the tab.
1453 */
1454 void dismissSubWindow() {
1455 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001456 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001457 mSubView.destroy();
1458 mSubView = null;
1459 mSubViewContainer = null;
1460 }
1461 }
1462
Grace Kloba22ac16e2009-10-07 18:00:23 -07001463
1464 /**
1465 * Set the parent tab of this tab.
1466 */
Michael Kolbc831b632011-05-11 09:30:34 -07001467 void setParent(Tab parent) {
John Reck52be4782011-08-26 15:37:29 -07001468 if (parent == this) {
1469 throw new IllegalStateException("Cannot set parent to self!");
1470 }
Michael Kolbc831b632011-05-11 09:30:34 -07001471 mParent = parent;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001472 // This tab may have been freed due to low memory. If that is the case,
Michael Kolbc831b632011-05-11 09:30:34 -07001473 // the parent tab id is already saved. If we are changing that id
Grace Kloba22ac16e2009-10-07 18:00:23 -07001474 // (most likely due to removing the parent tab) we must update the
Michael Kolbc831b632011-05-11 09:30:34 -07001475 // parent tab id in the saved Bundle.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001476 if (mSavedState != null) {
1477 if (parent == null) {
1478 mSavedState.remove(PARENTTAB);
1479 } else {
Michael Kolbc831b632011-05-11 09:30:34 -07001480 mSavedState.putLong(PARENTTAB, parent.getId());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001481 }
1482 }
John Reckb0a86db2011-05-24 14:05:58 -07001483
1484 // Sync the WebView useragent with the parent
1485 if (parent != null && mSettings.hasDesktopUseragent(parent.getWebView())
1486 != mSettings.hasDesktopUseragent(getWebView())) {
1487 mSettings.toggleDesktopUseragent(getWebView());
1488 }
John Reck52be4782011-08-26 15:37:29 -07001489
1490 if (parent != null && parent.getId() == getId()) {
1491 throw new IllegalStateException("Parent has same ID as child!");
1492 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001493 }
1494
1495 /**
Michael Kolbc831b632011-05-11 09:30:34 -07001496 * If this Tab was created through another Tab, then this method returns
1497 * that Tab.
1498 * @return the Tab parent or null
1499 */
1500 public Tab getParent() {
1501 return mParent;
1502 }
1503
1504 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001505 * When a Tab is created through the content of another Tab, then we
1506 * associate the Tabs.
1507 * @param child the Tab that was created from this Tab
1508 */
1509 void addChildTab(Tab child) {
Michael Kolbc831b632011-05-11 09:30:34 -07001510 if (mChildren == null) {
1511 mChildren = new Vector<Tab>();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001512 }
Michael Kolbc831b632011-05-11 09:30:34 -07001513 mChildren.add(child);
1514 child.setParent(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001515 }
1516
Michael Kolbc831b632011-05-11 09:30:34 -07001517 Vector<Tab> getChildren() {
1518 return mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001519 }
1520
1521 void resume() {
1522 if (mMainView != null) {
John Reck56c1fcf2011-08-17 10:15:16 -07001523 setupHwAcceleration(mMainView);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001524 mMainView.onResume();
1525 if (mSubView != null) {
1526 mSubView.onResume();
1527 }
1528 }
1529 }
1530
John Reck56c1fcf2011-08-17 10:15:16 -07001531 private void setupHwAcceleration(View web) {
1532 if (web == null) return;
1533 BrowserSettings settings = BrowserSettings.getInstance();
1534 if (settings.isHardwareAccelerated()) {
1535 web.setLayerType(View.LAYER_TYPE_NONE, null);
1536 } else {
1537 web.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
1538 }
1539 }
1540
Grace Kloba22ac16e2009-10-07 18:00:23 -07001541 void pause() {
1542 if (mMainView != null) {
1543 mMainView.onPause();
1544 if (mSubView != null) {
1545 mSubView.onPause();
1546 }
1547 }
1548 }
1549
1550 void putInForeground() {
John Reck8ee633f2011-08-09 16:00:35 -07001551 if (mInForeground) {
1552 return;
1553 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001554 mInForeground = true;
1555 resume();
Michael Kolb14612442011-06-24 13:06:29 -07001556 Activity activity = mWebViewController.getActivity();
1557 mMainView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001558 if (mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -07001559 mSubView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001560 }
Axesh R. Ajmerac6b5c322015-05-01 11:06:10 -07001561
Leon Scroggins1961ed22010-12-07 15:22:21 -05001562 mWebViewController.bookmarkedStatusHasChanged(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001563 }
1564
1565 void putInBackground() {
John Reck8ee633f2011-08-09 16:00:35 -07001566 if (!mInForeground) {
1567 return;
1568 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001569 mInForeground = false;
1570 pause();
1571 mMainView.setOnCreateContextMenuListener(null);
1572 if (mSubView != null) {
1573 mSubView.setOnCreateContextMenuListener(null);
1574 }
1575 }
1576
Michael Kolb8233fac2010-10-26 16:08:53 -07001577 boolean inForeground() {
1578 return mInForeground;
1579 }
1580
Grace Kloba22ac16e2009-10-07 18:00:23 -07001581 /**
1582 * Return the top window of this tab; either the subwindow if it is not
1583 * null or the main window.
1584 * @return The top window of this tab.
1585 */
1586 WebView getTopWindow() {
1587 if (mSubView != null) {
1588 return mSubView;
1589 }
1590 return mMainView;
1591 }
1592
1593 /**
1594 * Return the main window of this tab. Note: if a tab is freed in the
1595 * background, this can return null. It is only guaranteed to be
1596 * non-null for the current tab.
1597 * @return The main WebView of this tab.
1598 */
1599 WebView getWebView() {
1600 return mMainView;
1601 }
1602
Michael Kolba713ec82010-11-29 17:27:06 -08001603 void setViewContainer(View container) {
1604 mContainer = container;
1605 }
1606
Michael Kolb8233fac2010-10-26 16:08:53 -07001607 View getViewContainer() {
1608 return mContainer;
1609 }
1610
Grace Kloba22ac16e2009-10-07 18:00:23 -07001611 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001612 * Return whether private browsing is enabled for the main window of
1613 * this tab.
1614 * @return True if private browsing is enabled.
1615 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001616 boolean isPrivateBrowsingEnabled() {
John Reck502a3532011-08-16 14:21:46 -07001617 return mCurrentState.mIncognito;
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001618 }
1619
1620 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001621 * Return the subwindow of this tab or null if there is no subwindow.
1622 * @return The subwindow of this tab or null.
1623 */
1624 WebView getSubWebView() {
1625 return mSubView;
1626 }
1627
Michael Kolb1514bb72010-11-22 09:11:48 -08001628 void setSubWebView(WebView subView) {
1629 mSubView = subView;
1630 }
1631
Michael Kolb8233fac2010-10-26 16:08:53 -07001632 View getSubViewContainer() {
1633 return mSubViewContainer;
1634 }
1635
Michael Kolb1514bb72010-11-22 09:11:48 -08001636 void setSubViewContainer(View subViewContainer) {
1637 mSubViewContainer = subViewContainer;
1638 }
1639
Grace Kloba22ac16e2009-10-07 18:00:23 -07001640
1641 /**
1642 * @return The application id string
1643 */
1644 String getAppId() {
1645 return mAppId;
1646 }
1647
1648 /**
1649 * Set the application id string
1650 * @param id
1651 */
1652 void setAppId(String id) {
1653 mAppId = id;
1654 }
1655
Michael Kolbe28b3472011-08-04 16:54:31 -07001656 boolean closeOnBack() {
1657 return mCloseOnBack;
1658 }
1659
1660 void setCloseOnBack(boolean close) {
1661 mCloseOnBack = close;
1662 }
1663
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001664 boolean getDerivedFromIntent() {
1665 return mDerivedFromIntent;
1666 }
1667
1668 void setDerivedFromIntent(boolean derived) {
1669 mDerivedFromIntent = derived;
1670 }
1671
Grace Kloba22ac16e2009-10-07 18:00:23 -07001672 String getUrl() {
John Reck324d4402011-01-11 16:56:42 -08001673 return UrlUtils.filteredUrl(mCurrentState.mUrl);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001674 }
1675
Tarun Nainani8eb00912014-07-17 12:28:32 -07001676
1677 protected void onPageFinished() {
1678 mPageFinished = true;
Ze G Riande2a675c22015-06-03 11:15:24 -07001679 isDistillable();
Tarun Nainani8eb00912014-07-17 12:28:32 -07001680 }
1681
1682 public boolean getPageFinishedStatus() {
1683 return mPageFinished;
1684 }
1685
John Reck49a603c2011-03-03 09:33:05 -08001686 String getOriginalUrl() {
John Reckdb22ec42011-06-29 11:31:24 -07001687 if (mCurrentState.mOriginalUrl == null) {
1688 return getUrl();
John Reck49a603c2011-03-03 09:33:05 -08001689 }
John Reckdb22ec42011-06-29 11:31:24 -07001690 return UrlUtils.filteredUrl(mCurrentState.mOriginalUrl);
John Reck49a603c2011-03-03 09:33:05 -08001691 }
1692
Grace Kloba22ac16e2009-10-07 18:00:23 -07001693 /**
John Reck30c714c2010-12-16 17:30:34 -08001694 * Get the title of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001695 */
1696 String getTitle() {
John Reck30c714c2010-12-16 17:30:34 -08001697 return mCurrentState.mTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001698 }
1699
1700 /**
John Reck30c714c2010-12-16 17:30:34 -08001701 * Get the favicon of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001702 */
1703 Bitmap getFavicon() {
John Reck1cf4b792011-07-26 10:22:22 -07001704 if (mCurrentState.mFavicon != null) {
1705 return mCurrentState.mFavicon;
1706 }
1707 return getDefaultFavicon(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001708 }
1709
Pankaj Garg32e1b942015-06-03 18:13:24 -07001710 public boolean hasFavicon() {
1711 return mCurrentState.mFavicon != null;
1712 }
1713
John Recke969cc52010-12-21 17:24:43 -08001714 public boolean isBookmarkedSite() {
1715 return mCurrentState.mIsBookmarkedSite;
1716 }
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001717
Grace Kloba22ac16e2009-10-07 18:00:23 -07001718 /**
Steve Block08a6f0c2011-10-06 12:12:53 +01001719 * Sets the security state, clears the SSL certificate error and informs
1720 * the controller.
1721 */
Steve Block2466eff2011-10-03 15:33:09 +01001722 private void setSecurityState(SecurityState securityState) {
1723 mCurrentState.mSecurityState = securityState;
1724 mWebViewController.onUpdatedSecurityState(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001725 }
1726
1727 /**
Steve Block2466eff2011-10-03 15:33:09 +01001728 * @return The tab's security state.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001729 */
Steve Block2466eff2011-10-03 15:33:09 +01001730 SecurityState getSecurityState() {
1731 return mCurrentState.mSecurityState;
John Reck30c714c2010-12-16 17:30:34 -08001732 }
1733
1734 int getLoadProgress() {
1735 if (mInPageLoad) {
1736 return mPageLoadProgress;
1737 }
1738 return 100;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001739 }
1740
1741 /**
1742 * @return TRUE if onPageStarted is called while onPageFinished is not
1743 * called yet.
1744 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001745 boolean inPageLoad() {
1746 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001747 }
1748
Grace Kloba22ac16e2009-10-07 18:00:23 -07001749 /**
John Reck1cf4b792011-07-26 10:22:22 -07001750 * @return The Bundle with the tab's state if it can be saved, otherwise null
Grace Kloba22ac16e2009-10-07 18:00:23 -07001751 */
John Reck1cf4b792011-07-26 10:22:22 -07001752 public Bundle saveState() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001753 // If the WebView is null it means we ran low on memory and we already
1754 // stored the saved state in mSavedState.
1755 if (mMainView == null) {
John Reck1cf4b792011-07-26 10:22:22 -07001756 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001757 }
John Reck6c2e2f32011-08-22 13:41:23 -07001758
1759 if (TextUtils.isEmpty(mCurrentState.mUrl)) {
John Reck1cf4b792011-07-26 10:22:22 -07001760 return null;
John Reck24f18262011-06-17 14:47:20 -07001761 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001762
1763 mSavedState = new Bundle();
John Reck6c2e2f32011-08-22 13:41:23 -07001764 WebBackForwardList savedList = mMainView.saveState(mSavedState);
1765 if (savedList == null || savedList.getSize() == 0) {
1766 Log.w(LOGTAG, "Failed to save back/forward list for "
1767 + mCurrentState.mUrl);
1768 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001769
Michael Kolbc831b632011-05-11 09:30:34 -07001770 mSavedState.putLong(ID, mId);
John Reck30c714c2010-12-16 17:30:34 -08001771 mSavedState.putString(CURRURL, mCurrentState.mUrl);
1772 mSavedState.putString(CURRTITLE, mCurrentState.mTitle);
John Reck1cf4b792011-07-26 10:22:22 -07001773 mSavedState.putBoolean(INCOGNITO, mMainView.isPrivateBrowsingEnabled());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001774 if (mAppId != null) {
1775 mSavedState.putString(APPID, mAppId);
1776 }
Michael Kolbe28b3472011-08-04 16:54:31 -07001777 mSavedState.putBoolean(CLOSEFLAG, mCloseOnBack);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001778 // Remember the parent tab so the relationship can be restored.
Michael Kolbc831b632011-05-11 09:30:34 -07001779 if (mParent != null) {
1780 mSavedState.putLong(PARENTTAB, mParent.mId);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001781 }
John Reckb0a86db2011-05-24 14:05:58 -07001782 mSavedState.putBoolean(USERAGENT,
1783 mSettings.hasDesktopUseragent(getWebView()));
John Reck1cf4b792011-07-26 10:22:22 -07001784 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001785 }
1786
1787 /*
1788 * Restore the state of the tab.
1789 */
John Reck1cf4b792011-07-26 10:22:22 -07001790 private void restoreState(Bundle b) {
1791 mSavedState = b;
1792 if (mSavedState == null) {
1793 return;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001794 }
1795 // Restore the internal state even if the WebView fails to restore.
1796 // This will maintain the app id, original url and close-on-exit values.
Michael Kolbc831b632011-05-11 09:30:34 -07001797 mId = b.getLong(ID);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001798 mAppId = b.getString(APPID);
Michael Kolbe28b3472011-08-04 16:54:31 -07001799 mCloseOnBack = b.getBoolean(CLOSEFLAG);
John Reck8b9bb8b2012-03-08 13:19:40 -08001800 restoreUserAgent();
John Reck1cf4b792011-07-26 10:22:22 -07001801 String url = b.getString(CURRURL);
1802 String title = b.getString(CURRTITLE);
1803 boolean incognito = b.getBoolean(INCOGNITO);
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -07001804 mCurrentState = new PageState(mContext, incognito, url);
John Reck1cf4b792011-07-26 10:22:22 -07001805 mCurrentState.mTitle = title;
1806 synchronized (Tab.this) {
1807 if (mCapture != null) {
John Reck4eadc342011-10-31 14:04:10 -07001808 DataController.getInstance(mContext).loadThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07001809 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001810 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001811 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04001812
John Reck8b9bb8b2012-03-08 13:19:40 -08001813 private void restoreUserAgent() {
1814 if (mMainView == null || mSavedState == null) {
1815 return;
1816 }
1817 if (mSavedState.getBoolean(USERAGENT)
1818 != mSettings.hasDesktopUseragent(mMainView)) {
1819 mSettings.toggleDesktopUseragent(mMainView);
1820 }
1821 }
1822
Leon Scroggins1961ed22010-12-07 15:22:21 -05001823 public void updateBookmarkedStatus() {
John Recke969cc52010-12-21 17:24:43 -08001824 mDataController.queryBookmarkStatus(getUrl(), mIsBookmarkCallback);
Leon Scroggins1961ed22010-12-07 15:22:21 -05001825 }
1826
John Recke969cc52010-12-21 17:24:43 -08001827 private DataController.OnQueryUrlIsBookmark mIsBookmarkCallback
1828 = new DataController.OnQueryUrlIsBookmark() {
1829 @Override
1830 public void onQueryUrlIsBookmark(String url, boolean isBookmark) {
1831 if (mCurrentState.mUrl.equals(url)) {
1832 mCurrentState.mIsBookmarkedSite = isBookmark;
1833 mWebViewController.bookmarkedStatusHasChanged(Tab.this);
1834 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001835 }
John Recke969cc52010-12-21 17:24:43 -08001836 };
Michael Kolb1acef692011-03-08 14:12:06 -08001837
Michael Kolbeb95db42011-03-03 10:38:40 -08001838 public Bitmap getScreenshot() {
John Reck1cf4b792011-07-26 10:22:22 -07001839 synchronized (Tab.this) {
1840 return mCapture;
1841 }
Michael Kolbeb95db42011-03-03 10:38:40 -08001842 }
1843
John Reck541f55a2011-06-07 16:34:43 -07001844 public boolean isSnapshot() {
John Reck541f55a2011-06-07 16:34:43 -07001845 return false;
1846 }
1847
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001848 private static class SaveCallback implements ValueCallback<String> {
1849 boolean onReceiveValueCalled = false;
1850 private String mPath;
John Reck68234a92012-04-19 15:27:12 -07001851
1852 @Override
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001853 public void onReceiveValue(String path) {
1854 this.onReceiveValueCalled = true;
1855 this.mPath = path;
John Reck68234a92012-04-19 15:27:12 -07001856 synchronized (this) {
1857 notifyAll();
John Reck8cc92352011-07-06 17:41:52 -07001858 }
John Reck541f55a2011-06-07 16:34:43 -07001859 }
John Reck68234a92012-04-19 15:27:12 -07001860
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001861 public String getPath() {
1862 return mPath;
1863 }
John Reck68234a92012-04-19 15:27:12 -07001864 }
1865
1866 /**
1867 * Must be called on the UI thread
1868 */
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001869 public ContentValues createSnapshotValues(Bitmap bm) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001870 WebView web = getWebView();
John Reck68234a92012-04-19 15:27:12 -07001871 if (web == null) return null;
John Reckd8c74522011-06-14 08:45:00 -07001872 ContentValues values = new ContentValues();
1873 values.put(Snapshots.TITLE, mCurrentState.mTitle);
1874 values.put(Snapshots.URL, mCurrentState.mUrl);
John Reck68234a92012-04-19 15:27:12 -07001875 values.put(Snapshots.BACKGROUND, web.getPageBackgroundColor());
John Reck8cc92352011-07-06 17:41:52 -07001876 values.put(Snapshots.DATE_CREATED, System.currentTimeMillis());
1877 values.put(Snapshots.FAVICON, compressBitmap(getFavicon()));
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001878 values.put(Snapshots.THUMBNAIL, compressBitmap(bm));
John Reckd8c74522011-06-14 08:45:00 -07001879 return values;
John Reck541f55a2011-06-07 16:34:43 -07001880 }
1881
John Reck68234a92012-04-19 15:27:12 -07001882 /**
1883 * Probably want to call this on a background thread
1884 */
1885 public boolean saveViewState(ContentValues values) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001886 WebView web = getWebView();
John Reck68234a92012-04-19 15:27:12 -07001887 if (web == null) return false;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001888 String filename = UUID.randomUUID().toString();
John Reck68234a92012-04-19 15:27:12 -07001889 SaveCallback callback = new SaveCallback();
John Reck68234a92012-04-19 15:27:12 -07001890 try {
John Reck68234a92012-04-19 15:27:12 -07001891 synchronized (callback) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001892 web.saveViewState(filename, callback);
1893 callback.wait();
John Reck68234a92012-04-19 15:27:12 -07001894 }
John Reck68234a92012-04-19 15:27:12 -07001895 } catch (Exception e) {
1896 Log.w(LOGTAG, "Failed to save view state", e);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001897 String path = callback.getPath();
1898 if (path != null) {
1899 File file = mContext.getFileStreamPath(path);
1900 if (file.exists() && !file.delete()) {
1901 file.deleteOnExit();
1902 }
John Reck68234a92012-04-19 15:27:12 -07001903 }
1904 return false;
1905 }
Tarun Nainani8eb00912014-07-17 12:28:32 -07001906
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001907 String path = callback.getPath();
Tarun Nainani8eb00912014-07-17 12:28:32 -07001908 // could be that saving of file failed
1909 if (path == null) {
1910 return false;
1911 }
1912
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001913 File savedFile = new File(path);
1914 if (!savedFile.exists()) {
1915 return false;
John Reck68234a92012-04-19 15:27:12 -07001916 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001917 values.put(Snapshots.VIEWSTATE_PATH, path.substring(path.lastIndexOf('/') + 1));
1918 values.put(Snapshots.VIEWSTATE_SIZE, savedFile.length());
John Reck68234a92012-04-19 15:27:12 -07001919 return true;
1920 }
1921
John Reck8cc92352011-07-06 17:41:52 -07001922 public byte[] compressBitmap(Bitmap bitmap) {
1923 if (bitmap == null) {
1924 return null;
1925 }
1926 ByteArrayOutputStream stream = new ByteArrayOutputStream();
1927 bitmap.compress(CompressFormat.PNG, 100, stream);
1928 return stream.toByteArray();
1929 }
1930
John Reck26b18322011-06-21 13:08:58 -07001931 public void loadUrl(String url, Map<String, String> headers) {
1932 if (mMainView != null) {
Michael Kolba53c9892011-10-05 13:31:40 -07001933 mPageLoadProgress = INITIAL_PROGRESS;
Axesh R. Ajmerad04cad12015-08-12 12:02:44 -07001934 mCurrentState = new PageState(
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -07001935 mContext, mMainView.isPrivateBrowsingEnabled(), url);
John Reck26b18322011-06-21 13:08:58 -07001936 mMainView.loadUrl(url, headers);
1937 }
1938 }
1939
John Reck38b39652012-06-05 09:22:59 -07001940 public void disableUrlOverridingForLoad() {
1941 mDisableOverrideUrlLoading = true;
1942 }
1943
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001944 private void thumbnailUpdated() {
John Reck8ee633f2011-08-09 16:00:35 -07001945 mHandler.removeMessages(MSG_CAPTURE);
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001946
John Reck8ee633f2011-08-09 16:00:35 -07001947 TabControl tc = mWebViewController.getTabControl();
1948 if (tc != null) {
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001949 OnThumbnailUpdatedListener updateListener = tc.getOnThumbnailUpdatedListener();
John Reck8ee633f2011-08-09 16:00:35 -07001950 if (updateListener != null) {
1951 updateListener.onThumbnailUpdated(this);
1952 }
1953 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001954 }
1955
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001956 protected void capture() {
1957 if (mMainView == null || mCapture == null || !mMainView.isReady() ||
1958 mMainView.getContentWidth() <= 0 || mMainView.getContentHeight() <= 0 ||
1959 !mFirstVisualPixelPainted || mMainView.isShowingCrashView()) {
1960
1961 initCaptureBitmap();
1962 thumbnailUpdated();
1963 return;
1964 }
1965
1966 mMainView.getContentBitmapAsync((float) mCaptureWidth / mMainView.getWidth(), new Rect(),
1967 new ValueCallback<Bitmap>() {
1968 @Override
1969 public void onReceiveValue(Bitmap bitmap) {
1970 if (mCapture == null) {
1971 initCaptureBitmap();
1972 }
1973
1974 if (bitmap == null) {
1975 thumbnailUpdated();
1976 return;
1977 }
1978
1979 Canvas c = new Canvas(mCapture);
1980 mCapture.eraseColor(Color.WHITE);
1981 c.drawBitmap(bitmap, 0, 0, null);
1982
1983 // manually anti-alias the edges for the tilt
1984 c.drawRect(0, 0, 1, mCapture.getHeight(), sAlphaPaint);
1985 c.drawRect(mCapture.getWidth() - 1, 0, mCapture.getWidth(),
1986 mCapture.getHeight(), sAlphaPaint);
1987 c.drawRect(0, 0, mCapture.getWidth(), 1, sAlphaPaint);
1988 c.drawRect(0, mCapture.getHeight() - 1, mCapture.getWidth(),
1989 mCapture.getHeight(), sAlphaPaint);
1990 c.setBitmap(null);
1991
1992 persistThumbnail();
1993 thumbnailUpdated();
1994 }
1995 }
1996 );
John Reck1cf4b792011-07-26 10:22:22 -07001997 }
1998
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001999 @Override
2000 public void onNewPicture(WebView view, Picture picture) {
Michael Kolb9ef259a2011-07-12 15:33:08 -07002001 }
2002
John Reckef654f12011-07-12 16:42:08 -07002003 public boolean canGoBack() {
2004 return mMainView != null ? mMainView.canGoBack() : false;
2005 }
2006
2007 public boolean canGoForward() {
2008 return mMainView != null ? mMainView.canGoForward() : false;
2009 }
2010
2011 public void goBack() {
2012 if (mMainView != null) {
2013 mMainView.goBack();
2014 }
2015 }
2016
2017 public void goForward() {
2018 if (mMainView != null) {
2019 mMainView.goForward();
2020 }
2021 }
2022
John Reck1cf4b792011-07-26 10:22:22 -07002023 protected void persistThumbnail() {
John Reck4eadc342011-10-31 14:04:10 -07002024 DataController.getInstance(mContext).saveThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07002025 }
2026
2027 protected void deleteThumbnail() {
John Reck4eadc342011-10-31 14:04:10 -07002028 DataController.getInstance(mContext).deleteThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07002029 }
2030
John Reck4eadc342011-10-31 14:04:10 -07002031 void updateCaptureFromBlob(byte[] blob) {
John Reck1cf4b792011-07-26 10:22:22 -07002032 synchronized (Tab.this) {
2033 if (mCapture == null) {
2034 return;
2035 }
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002036 ByteBuffer buffer = ByteBuffer.wrap(blob);
Michael Kolbd837a112011-08-09 14:00:18 -07002037 try {
2038 mCapture.copyPixelsFromBuffer(buffer);
2039 } catch (RuntimeException rex) {
2040 Log.e(LOGTAG, "Load capture has mismatched sizes; buffer: "
2041 + buffer.capacity() + " blob: " + blob.length
2042 + "capture: " + mCapture.getByteCount());
2043 throw rex;
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002044 }
John Reck1cf4b792011-07-26 10:22:22 -07002045 }
2046 }
2047
John Reck52be4782011-08-26 15:37:29 -07002048 @Override
2049 public String toString() {
2050 StringBuilder builder = new StringBuilder(100);
2051 builder.append(mId);
2052 builder.append(") has parent: ");
2053 if (getParent() != null) {
2054 builder.append("true[");
2055 builder.append(getParent().getId());
2056 builder.append("]");
2057 } else {
2058 builder.append("false");
2059 }
2060 builder.append(", incog: ");
2061 builder.append(isPrivateBrowsingEnabled());
2062 if (!isPrivateBrowsingEnabled()) {
2063 builder.append(", title: ");
2064 builder.append(getTitle());
2065 builder.append(", url: ");
2066 builder.append(getUrl());
2067 }
2068 return builder.toString();
2069 }
2070
Ze G Riande2a675c22015-06-03 11:15:24 -07002071 // dertermines if the tab contains a dislled page
2072 public boolean isDistilled() {
2073 if (!BrowserCommandLine.hasSwitch("reader-mode")) {
2074 return false;
2075 }
2076 try {
2077 return DomDistillerUtils.isUrlDistilled(getUrl());
2078 } catch (Exception e) {
2079 return false;
2080 }
2081 }
2082
2083 //determines if the tab contains a distillable page
2084 public boolean isDistillable() {
2085 if (!BrowserCommandLine.hasSwitch("reader-mode")) {
2086 mIsDistillable = false;
2087 return mIsDistillable;
2088 }
2089 final ValueCallback<String> onIsDistillable = new ValueCallback<String>() {
2090 @Override
2091 public void onReceiveValue(String str) {
2092 mIsDistillable = Boolean.parseBoolean(str);
2093 }
2094 };
2095
2096 if (isDistilled()) {
2097 mIsDistillable = true;
2098 return mIsDistillable;
2099 }
2100
2101 try {
2102 DomDistillerUtils.isWebViewDistillable(getWebView(), onIsDistillable);
2103 } catch (Exception e) {
2104 mIsDistillable = false;
2105 }
2106
2107 return mIsDistillable;
2108 }
2109
2110 // Function that sets the mIsDistillable variable
2111 public void setIsDistillable(boolean value) {
2112 if (!BrowserCommandLine.hasSwitch("reader-mode")) {
2113 mIsDistillable = false;
2114 }
2115 mIsDistillable = value;
2116 }
2117
2118 // Function that returns the distilled url of the current url
2119 public String getDistilledUrl() {
2120 if (getUrl() != null) {
2121 return DomDistillerUtils.getDistilledUrl(getUrl());
2122 }
2123 return new String();
2124 }
2125
2126 // function that returns the non-distilled version of the current url
2127 public String getNonDistilledUrl() {
2128 if (getUrl() != null) {
2129 return DomDistillerUtils.getOriginalUrlFromDistilledUrl(getUrl());
2130 }
2131 return new String();
2132 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002133}