blob: 6919300fad32125da801d4229056b39a8e841fb6 [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;
Pankaj Gargf49e0222015-09-01 12:19:13 -0700185 private Bitmap mViewportCapture;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700186 private Handler mHandler;
Michael Kolb72864272012-05-03 15:42:15 -0700187 private boolean mUpdateThumbnail;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800188 private Timestamp timestamp;
Vivek Sekharf96064b2014-07-28 16:32:34 -0700189 private boolean mFullScreen = false;
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800190 private boolean mReceivedError;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700191
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -0700192 // determine if webview is destroyed to MemoryMonitor
193 private boolean mWebViewDestroyedByMemoryMonitor;
194
Vivek Sekhar0c609cd2015-12-10 16:00:05 -0800195 // Tab started initally in background
196 private boolean mBackgroundTab;
197
Pankaj Garg21dad562015-07-02 17:17:24 -0700198 private String mTouchIconUrl;
199
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700200 private Observable mFirstPixelObservable;
201 private Observable mTabHistoryUpdateObservable;
202
203 Observable getFirstPixelObservable() {
204 return mFirstPixelObservable;
205 }
206
207 Observable getTabHistoryUpdateObservable() {
208 return mTabHistoryUpdateObservable;
209 }
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -0700210
Ze G Riande2a675c22015-06-03 11:15:24 -0700211 // dertermines if the tab contains a disllable page
212 private boolean mIsDistillable = false;
Mathew Inwood1dd8e822011-08-03 14:34:29 +0100213
John Reck1cf4b792011-07-26 10:22:22 -0700214 private static synchronized Bitmap getDefaultFavicon(Context context) {
215 if (sDefaultFavicon == null) {
216 sDefaultFavicon = BitmapFactory.decodeResource(
Enrico Rosd6efa972014-12-02 19:49:59 -0800217 context.getResources(), R.drawable.ic_deco_favicon_normal);
John Reck1cf4b792011-07-26 10:22:22 -0700218 }
219 return sDefaultFavicon;
220 }
Michael Kolbeb95db42011-03-03 10:38:40 -0800221
John Reck30c714c2010-12-16 17:30:34 -0800222 // All the state needed for a page
John Reckd8c74522011-06-14 08:45:00 -0700223 protected static class PageState {
John Reck30c714c2010-12-16 17:30:34 -0800224 String mUrl;
John Reckdb22ec42011-06-29 11:31:24 -0700225 String mOriginalUrl;
John Reck30c714c2010-12-16 17:30:34 -0800226 String mTitle;
Steve Block08a6f0c2011-10-06 12:12:53 +0100227 // This is non-null only when mSecurityState is SECURITY_STATE_BAD_CERTIFICATE.
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -0700228 SecurityState mSecurityState;
229 // This is non-null only when onReceivedIcon is called or SnapshotTab restores it.
John Reck30c714c2010-12-16 17:30:34 -0800230 Bitmap mFavicon;
Steve Block08a6f0c2011-10-06 12:12:53 +0100231 boolean mIsBookmarkedSite;
232 boolean mIncognito;
John Reck30c714c2010-12-16 17:30:34 -0800233
234 PageState(Context c, boolean incognito) {
John Reck502a3532011-08-16 14:21:46 -0700235 mIncognito = incognito;
Axesh R. Ajmerad04cad12015-08-12 12:02:44 -0700236 mOriginalUrl = mUrl = "";
John Reck502a3532011-08-16 14:21:46 -0700237 if (mIncognito) {
John Reck30c714c2010-12-16 17:30:34 -0800238 mTitle = c.getString(R.string.new_incognito_tab);
John Reck30c714c2010-12-16 17:30:34 -0800239 } else {
John Reck30c714c2010-12-16 17:30:34 -0800240 mTitle = c.getString(R.string.new_tab);
John Reck30c714c2010-12-16 17:30:34 -0800241 }
Steve Block2466eff2011-10-03 15:33:09 +0100242 mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
John Reck30c714c2010-12-16 17:30:34 -0800243 }
244
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -0700245 PageState(Context c, boolean incognito, String url) {
John Reck502a3532011-08-16 14:21:46 -0700246 mIncognito = incognito;
Axesh R. Ajmerad04cad12015-08-12 12:02:44 -0700247 if (mIncognito)
248 mOriginalUrl = mUrl = "";
249 else
250 mOriginalUrl = mUrl = url;
Pankaj Garg96d0ccd2015-07-30 16:49:47 -0700251 mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
John Reck30c714c2010-12-16 17:30:34 -0800252 }
John Reck1cf4b792011-07-26 10:22:22 -0700253
Grace Kloba22ac16e2009-10-07 18:00:23 -0700254 }
255
John Reck30c714c2010-12-16 17:30:34 -0800256 // The current/loading page's state
John Reckd8c74522011-06-14 08:45:00 -0700257 protected PageState mCurrentState;
John Reck30c714c2010-12-16 17:30:34 -0800258
Grace Kloba22ac16e2009-10-07 18:00:23 -0700259 // Used for saving and restoring each Tab
Michael Kolbc831b632011-05-11 09:30:34 -0700260 static final String ID = "ID";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700261 static final String CURRURL = "currentUrl";
262 static final String CURRTITLE = "currentTitle";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700263 static final String PARENTTAB = "parentTab";
264 static final String APPID = "appid";
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700265 static final String INCOGNITO = "privateBrowsingEnabled";
John Reckb0a86db2011-05-24 14:05:58 -0700266 static final String USERAGENT = "useragent";
Michael Kolbe28b3472011-08-04 16:54:31 -0700267 static final String CLOSEFLAG = "closeOnBack";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700268
Pankaj Garg18186a92015-03-31 14:59:33 -0700269 public void setNetworkAvailable(boolean networkUp) {
270 if (networkUp && mReceivedError && (mMainView != null)) {
271 mMainView.reload();
272 }
273 }
274
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700275 public boolean isFirstVisualPixelPainted() {
276 return mFirstVisualPixelPainted;
277 }
278
279 public int getCaptureIndex(int navIndex) {
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700280 int orientation = mWebViewController.getActivity().
281 getResources().getConfiguration().orientation;
282
283 int orientationBit = (orientation == Configuration.ORIENTATION_LANDSCAPE) ? 0 : 1;
284
Vivek Sekhard0f60402015-06-05 14:07:11 -0700285 int index = orientationBit << 31 | (((int)mId & 0x7f) << 24) | (navIndex & 0xffffff);
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700286 return index;
287 }
288
289 public int getTabIdxFromCaptureIdx(int index) {
290 return (index & 0x7f000000) >> 24;
291 }
292
293 public int getOrientationFromCaptureIdx(int index) {
294 return ((index & 0x80000000) == 0) ? Configuration.ORIENTATION_LANDSCAPE :
295 Configuration.ORIENTATION_PORTRAIT;
296
297 }
298
299 public int getNavIdxFromCaptureIdx(int index) {
300 return (index & 0xffffff);
301 }
302
Pankaj Garg96d0ccd2015-07-30 16:49:47 -0700303 public static SecurityState getWebViewSecurityState(WebView view) {
304 switch (view.getSecurityLevel()) {
305 case WebView.SecurityLevel.EV_SECURE:
306 case WebView.SecurityLevel.SECURE:
307 return SecurityState.SECURITY_STATE_SECURE;
308 case WebView.SecurityLevel.SECURITY_ERROR:
309 return SecurityState.SECURITY_STATE_BAD_CERTIFICATE;
310 case WebView.SecurityLevel.SECURITY_POLICY_WARNING:
311 case WebView.SecurityLevel.SECURITY_WARNING:
312 return SecurityState.SECURITY_STATE_MIXED;
313 }
314 return SecurityState.SECURITY_STATE_NOT_SECURE;
315 }
316
Grace Kloba22ac16e2009-10-07 18:00:23 -0700317 // -------------------------------------------------------------------------
318 // WebViewClient implementation for the main WebView
319 // -------------------------------------------------------------------------
320
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800321 private final WebViewClient mWebViewClient = new WebViewClient() {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500322 private Message mDontResend;
323 private Message mResend;
Michael Kolb47bd1e42011-09-01 15:25:00 -0700324
325 private boolean providersDiffer(String url, String otherUrl) {
326 Uri uri1 = Uri.parse(url);
327 Uri uri2 = Uri.parse(otherUrl);
328 return !uri1.getEncodedAuthority().equals(uri2.getEncodedAuthority());
329 }
330
Grace Kloba22ac16e2009-10-07 18:00:23 -0700331 @Override
332 public void onPageStarted(WebView view, String url, Bitmap favicon) {
Ze G Riande2a675c22015-06-03 11:15:24 -0700333 setIsDistillable(false);
Vivek Sekhar0c609cd2015-12-10 16:00:05 -0800334 mBackgroundTab = false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700335 mInPageLoad = true;
Tarun Nainani8eb00912014-07-17 12:28:32 -0700336 mPageFinished = false;
Pankaj Garg79878492015-04-01 14:48:21 -0700337 mFirstVisualPixelPainted = false;
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700338 mFirstPixelObservable.set(false);
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800339 mReceivedError = false;
Michael Kolb72864272012-05-03 15:42:15 -0700340 mUpdateThumbnail = true;
Michael Kolba53c9892011-10-05 13:31:40 -0700341 mPageLoadProgress = INITIAL_PROGRESS;
Michael Kolb14612442011-06-24 13:06:29 -0700342 mCurrentState = new PageState(mContext,
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -0700343 view.isPrivateBrowsingEnabled(), url);
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000344 mLoadStartTime = SystemClock.uptimeMillis();
Pankaj Garg62bc7912015-04-14 16:08:59 -0700345 // Need re-enable FullScreenMode on Page navigation if needed
346 if (BrowserSettings.getInstance().useFullscreen()){
347 Controller controller = (Controller) mWebViewController;
348 BaseUi ui = (BaseUi) controller.getUi();
349 ui.forceDisableFullscreenMode(false);
350 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700351 // If we start a touch icon load and then load a new page, we don't
352 // want to cancel the current touch icon loader. But, we do want to
353 // create a new one when the touch icon url is known.
354 if (mTouchIconLoader != null) {
355 mTouchIconLoader.mTab = null;
356 mTouchIconLoader = null;
357 }
358
Grace Kloba22ac16e2009-10-07 18:00:23 -0700359 // finally update the UI in the activity if it is in the foreground
John Reck324d4402011-01-11 16:56:42 -0800360 mWebViewController.onPageStarted(Tab.this, view, favicon);
Leon Scroggins4cd97792010-12-03 15:31:56 -0500361
John Recke969cc52010-12-21 17:24:43 -0800362 updateBookmarkedStatus();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700363 }
364
365 @Override
366 public void onPageFinished(WebView view, String url) {
John Reck38b39652012-06-05 09:22:59 -0700367 mDisableOverrideUrlLoading = false;
John Reck5b691842010-11-29 11:21:13 -0800368 if (!isPrivateBrowsingEnabled()) {
369 LogTag.logPageFinishedLoading(
370 url, SystemClock.uptimeMillis() - mLoadStartTime);
371 }
John Reck1cf4b792011-07-26 10:22:22 -0700372 syncCurrentState(view, url);
John Reck324d4402011-01-11 16:56:42 -0800373 mWebViewController.onPageFinished(Tab.this);
Pankaj Garg96d0ccd2015-07-30 16:49:47 -0700374 setSecurityState(getWebViewSecurityState(view));
Grace Kloba22ac16e2009-10-07 18:00:23 -0700375 }
376
Pankaj Garg79878492015-04-01 14:48:21 -0700377 @Override
378 public void onFirstVisualPixel(WebView view) {
379 mFirstVisualPixelPainted = true;
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700380 mFirstPixelObservable.set(true);
Pankaj Garg79878492015-04-01 14:48:21 -0700381 }
382
Grace Kloba22ac16e2009-10-07 18:00:23 -0700383 // return true if want to hijack the url to let another app to handle it
384 @Override
385 public boolean shouldOverrideUrlLoading(WebView view, String url) {
John Reck38b39652012-06-05 09:22:59 -0700386 if (!mDisableOverrideUrlLoading && mInForeground) {
Michael Kolb18eb3772010-12-10 14:29:51 -0800387 return mWebViewController.shouldOverrideUrlLoading(Tab.this,
388 view, url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700389 } else {
390 return false;
391 }
392 }
393
Vivek Sekharb991edb2014-12-17 18:18:07 -0800394 @Override
395 public boolean shouldDownloadFavicon(WebView view, String url) {
396 return true;
397 }
398
Grace Kloba22ac16e2009-10-07 18:00:23 -0700399 /**
Steve Block2466eff2011-10-03 15:33:09 +0100400 * Updates the security state. This method is called when we discover
401 * another resource to be loaded for this page (for example,
402 * javascript). While we update the security state, we do not update
403 * the lock icon until we are done loading, as it is slightly more
404 * secure this way.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700405 */
406 @Override
407 public void onLoadResource(WebView view, String url) {
408 if (url != null && url.length() > 0) {
409 // It is only if the page claims to be secure that we may have
Steve Block2466eff2011-10-03 15:33:09 +0100410 // to update the security state:
411 if (mCurrentState.mSecurityState == SecurityState.SECURITY_STATE_SECURE) {
412 // If NOT a 'safe' url, change the state to mixed content!
Grace Kloba22ac16e2009-10-07 18:00:23 -0700413 if (!(URLUtil.isHttpsUrl(url) || URLUtil.isDataUrl(url)
414 || URLUtil.isAboutUrl(url))) {
Steve Block2466eff2011-10-03 15:33:09 +0100415 mCurrentState.mSecurityState = SecurityState.SECURITY_STATE_MIXED;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700416 }
417 }
418 }
419 }
420
421 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700422 * Show a dialog informing the user of the network error reported by
423 * WebCore if it is in the foreground.
424 */
425 @Override
426 public void onReceivedError(WebView view, int errorCode,
427 String description, String failingUrl) {
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800428 // Used for the syncCurrentState to use
429 // the failing url instead of using webview url
430 mReceivedError = true;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700431 }
432
433 /**
434 * Check with the user if it is ok to resend POST data as the page they
435 * are trying to navigate to is the result of a POST.
436 */
437 @Override
438 public void onFormResubmission(WebView view, final Message dontResend,
439 final Message resend) {
440 if (!mInForeground) {
441 dontResend.sendToTarget();
442 return;
443 }
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500444 if (mDontResend != null) {
445 Log.w(LOGTAG, "onFormResubmission should not be called again "
446 + "while dialog is still up");
447 dontResend.sendToTarget();
448 return;
449 }
450 mDontResend = dontResend;
451 mResend = resend;
Michael Kolb14612442011-06-24 13:06:29 -0700452 new AlertDialog.Builder(mContext).setTitle(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700453 R.string.browserFrameFormResubmitLabel).setMessage(
454 R.string.browserFrameFormResubmitMessage)
455 .setPositiveButton(R.string.ok,
456 new DialogInterface.OnClickListener() {
457 public void onClick(DialogInterface dialog,
458 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500459 if (mResend != null) {
460 mResend.sendToTarget();
461 mResend = null;
462 mDontResend = null;
463 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700464 }
465 }).setNegativeButton(R.string.cancel,
466 new DialogInterface.OnClickListener() {
467 public void onClick(DialogInterface dialog,
468 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500469 if (mDontResend != null) {
470 mDontResend.sendToTarget();
471 mResend = null;
472 mDontResend = null;
473 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700474 }
475 }).setOnCancelListener(new OnCancelListener() {
476 public void onCancel(DialogInterface dialog) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500477 if (mDontResend != null) {
478 mDontResend.sendToTarget();
479 mResend = null;
480 mDontResend = null;
481 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700482 }
483 }).show();
484 }
485
486 /**
487 * Insert the url into the visited history database.
488 * @param url The url to be inserted.
489 * @param isReload True if this url is being reloaded.
490 * FIXME: Not sure what to do when reloading the page.
491 */
492 @Override
493 public void doUpdateVisitedHistory(WebView view, String url,
494 boolean isReload) {
John Reck324d4402011-01-11 16:56:42 -0800495 mWebViewController.doUpdateVisitedHistory(Tab.this, isReload);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700496 }
497
498 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700499 * Handles an HTTP authentication request.
500 *
501 * @param handler The authentication handler
502 * @param host The host
503 * @param realm The realm
504 */
505 @Override
506 public void onReceivedHttpAuthRequest(WebView view,
507 final HttpAuthHandler handler, final String host,
508 final String realm) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700509 mWebViewController.onReceivedHttpAuthRequest(Tab.this, view, handler, host, realm);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700510 }
511
512 @Override
John Reck438bf462011-01-12 18:11:46 -0800513 public WebResourceResponse shouldInterceptRequest(WebView view,
514 String url) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800515 //intercept if opening a new incognito tab - show the incognito welcome page
Axesh R. Ajmerad04cad12015-08-12 12:02:44 -0700516
517 // show only incognito content and webview has private
518 // and cannot go back(only supported if explicit from UI )
519 if (view.isPrivateBrowsingEnabled() &&
520 !view.canGoBack() &&
521 url.startsWith(Controller.INCOGNITO_URI)) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800522 Resources resourceHandle = mContext.getResources();
523 InputStream inStream = resourceHandle.openRawResource(
Bijan Amirzada41242f22014-03-21 12:12:18 -0700524 com.android.browser.R.raw.incognito_mode_start_page);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800525 return new WebResourceResponse("text/html", "utf8", inStream);
526 }
kaiyiz6e5b3e02013-08-19 20:02:01 +0800527 WebResourceResponse res;
528 if (MyNavigationUtil.MY_NAVIGATION.equals(url)) {
529 res = MyNavigationProvider.shouldInterceptRequest(mContext, url);
530 } else {
531 res = HomeProvider.shouldInterceptRequest(mContext, url);
532 }
John Reck438bf462011-01-12 18:11:46 -0800533 return res;
534 }
535
536 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700537 public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
538 if (!mInForeground) {
539 return false;
540 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700541 return mWebViewController.shouldOverrideKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700542 }
543
544 @Override
545 public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700546 if (!mInForeground) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700547 return;
548 }
John Reck997b1b72012-04-19 18:08:25 -0700549 if (!mWebViewController.onUnhandledKeyEvent(event)) {
550 super.onUnhandledKeyEvent(view, event);
551 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700552 }
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700553
554 @Override
555 public void beforeNavigation(WebView view, String url) {
Pankaj Garg21dad562015-07-02 17:17:24 -0700556 mTouchIconUrl = null;
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -0700557 TitleBar titleBar = null;
558 Controller controller = (Controller)mWebViewController;
559 UI ui = controller.getUi();
560
Sagar Dhawanfb79bf42015-08-21 12:37:29 -0700561 // Clear the page state
562 mCurrentState = new PageState(mContext,
563 view.isPrivateBrowsingEnabled(), url);
564
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -0700565 if (ui instanceof BaseUi) {
566 titleBar = ((BaseUi)ui).getTitleBar();
567 if (titleBar != null) {
568 NavigationBarBase navBar = titleBar.getNavigationBar();
569 navBar.showCurrentFavicon(Tab.this); // Show the default Favicon while loading a new page
570 }
571 }
572
Site Mao61b68212015-07-16 10:56:31 -0700573 if (BaseUi.isUiLowPowerMode()) {
Pankaj Garg18aa0a12015-06-22 11:06:12 -0700574 return;
575 }
576
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700577 if (isPrivateBrowsingEnabled()) {
578 return;
579 }
580
581 if (!mFirstVisualPixelPainted) {
582 return;
583 }
584
585 final int idx = view.copyBackForwardList().getCurrentIndex();
586 boolean bitmapExists = view.hasSnapshot(idx);
587
588 int progress = 100;
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -0700589 if (titleBar != null) {
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700590 progress = titleBar.getProgressView().getProgressPercent();
591 }
592
593 if (bitmapExists && progress < 85) {
594 return;
595 }
596
597 int index = getCaptureIndex(view.getLastCommittedHistoryIndex());
Sagar Dhawan49f85cf2015-07-10 16:54:20 -0700598 view.captureSnapshot(index, null);
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700599 }
600
601 @Override
602 public void onHistoryItemCommit(WebView view, int index) {
Site Mao61b68212015-07-16 10:56:31 -0700603 if (BaseUi.isUiLowPowerMode()) {
Pankaj Garg18aa0a12015-06-22 11:06:12 -0700604 return;
605 }
606
Axesh R. Ajmera4a6838a2015-07-23 15:08:14 -0700607 // prevent snapshot tab from commiting any history
608 if (isSnapshot()) {
609 return;
610 }
611
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700612 mTabHistoryUpdateObservable.set(index);
Vivek Sekhar53ef8932015-06-18 16:51:43 -0700613 final int maxIdx = view.copyBackForwardList().getSize();
614 final WebView wv = view;
Pankaj Garg036365b2015-09-14 11:21:19 -0700615 final int currIdx = index;
616 final int currentTabIdx = (int) mWebViewController.getTabControl().
617 getCurrentTab().getId();
Vivek Sekhar53ef8932015-06-18 16:51:43 -0700618 view.getSnapshotIds(new ValueCallback <List<Integer>>() {
619 @Override
620 public void onReceiveValue(List<Integer> ids) {
Vivek Sekhar53ef8932015-06-18 16:51:43 -0700621 for (Integer id : ids) {
Pankaj Garg036365b2015-09-14 11:21:19 -0700622 int tabIdx = getTabIdxFromCaptureIdx(id);
623 int navIdx = getNavIdxFromCaptureIdx(id);
624 if (tabIdx == currentTabIdx && (navIdx >= maxIdx || navIdx == currIdx)) {
Vivek Sekhar53ef8932015-06-18 16:51:43 -0700625 wv.deleteSnapshot(id);
626 }
627 }
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700628 }
Vivek Sekhar53ef8932015-06-18 16:51:43 -0700629 });
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700630 }
Pankaj Garg62bc7912015-04-14 16:08:59 -0700631
632 @Override
633 public void onKeyboardStateChange(boolean popup) {
Vivek Sekhard4de6162015-07-21 15:01:45 -0700634 boolean keyboardWasShowing = isKeyboardShowing();
Sagar Dhawan33551ff2015-07-08 17:24:44 -0700635 mIsKeyboardUp = popup;
Vivek Sekhard4de6162015-07-21 15:01:45 -0700636 Controller controller = (Controller)mWebViewController;
637 BaseUi ui = (BaseUi) controller.getUi();
638 // lock the title bar
639 if (popup)
640 ui.getTitleBar().showTopControls(true);
641 if (keyboardWasShowing && popup)
642 ui.getTitleBar().enableTopControls(true);
Pankaj Garg62bc7912015-04-14 16:08:59 -0700643 if (BrowserSettings.getInstance().useFullscreen()) {
Pankaj Garg62bc7912015-04-14 16:08:59 -0700644 ui.forceDisableFullscreenMode(popup);
645 }
646 }
Vivek Sekharae3b1792015-08-03 12:26:41 -0700647
648 @Override
649 public void onAttachInterstitialPage(WebView mWebView) {
650 Controller controller = (Controller)mWebViewController;
651 BaseUi ui = (BaseUi) controller.getUi();
652 ui.getTitleBar().showTopControls(false);
653 }
654
655 @Override
656 public void onDetachInterstitialPage(WebView mWebView) {
657 Controller controller = (Controller)mWebViewController;
658 BaseUi ui = (BaseUi) controller.getUi();
659 ui.getTitleBar().enableTopControls(true);
660 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700661 };
662
John Reck1cf4b792011-07-26 10:22:22 -0700663 private void syncCurrentState(WebView view, String url) {
664 // Sync state (in case of stop/timeout)
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800665
Axesh R. Ajmerad04cad12015-08-12 12:02:44 -0700666
667
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800668 if (mReceivedError) {
669 mCurrentState.mUrl = url;
670 mCurrentState.mOriginalUrl = url;
Axesh R. Ajmerad04cad12015-08-12 12:02:44 -0700671 } else if (view.isPrivateBrowsingEnabled() &&
672 !TextUtils.isEmpty(url) &&
673 url.contains(Controller.INCOGNITO_URI)) {
Vivek Sekharb826fba2015-12-09 11:27:20 -0800674 mCurrentState.mUrl = mCurrentState.mOriginalUrl = Controller.INCOGNITO_URI;
Axesh R. Ajmerad04cad12015-08-12 12:02:44 -0700675 }
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -0700676
Axesh R. Ajmerad04cad12015-08-12 12:02:44 -0700677 else {
678 mCurrentState.mUrl = view.getUrl();
679 mCurrentState.mOriginalUrl = view.getOriginalUrl();
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800680 }
681
John Reck1cf4b792011-07-26 10:22:22 -0700682 if (mCurrentState.mUrl == null) {
John Reck8bcafc12011-08-29 16:43:02 -0700683 mCurrentState.mUrl = "";
John Reck1cf4b792011-07-26 10:22:22 -0700684 }
John Reck1cf4b792011-07-26 10:22:22 -0700685 mCurrentState.mTitle = view.getTitle();
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800686
687
John Reck1cf4b792011-07-26 10:22:22 -0700688 if (!URLUtil.isHttpsUrl(mCurrentState.mUrl)) {
689 // In case we stop when loading an HTTPS page from an HTTP page
690 // but before a provisional load occurred
Steve Block2466eff2011-10-03 15:33:09 +0100691 mCurrentState.mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
John Reck1cf4b792011-07-26 10:22:22 -0700692 }
John Reck502a3532011-08-16 14:21:46 -0700693 mCurrentState.mIncognito = view.isPrivateBrowsingEnabled();
John Reck1cf4b792011-07-26 10:22:22 -0700694 }
695
Pankaj Garg21dad562015-07-02 17:17:24 -0700696 public String getTouchIconUrl() {
697 return mTouchIconUrl;
698 }
Tarun Nainani8eb00912014-07-17 12:28:32 -0700699
Sagar Dhawan33551ff2015-07-08 17:24:44 -0700700 public boolean isKeyboardShowing() {
701 Controller controller = (Controller)mWebViewController;
702 return (mIsKeyboardUp || controller.getUi().isEditingUrl());
703 }
704
Tarun Nainani8eb00912014-07-17 12:28:32 -0700705 public boolean isTabFullScreen() {
706 return mFullScreen;
707 }
708
Vivek Sekharf96064b2014-07-28 16:32:34 -0700709 protected void setTabFullscreen(boolean fullScreen) {
Tarun Nainani8eb00912014-07-17 12:28:32 -0700710 Controller controller = (Controller)mWebViewController;
Sudheer Koganti24766882014-10-02 10:58:09 -0700711 controller.getUi().showFullscreen(fullScreen);
Tarun Nainani8eb00912014-07-17 12:28:32 -0700712 mFullScreen = fullScreen;
Vivek Sekharf96064b2014-07-28 16:32:34 -0700713 }
714
Sudheer Koganti24766882014-10-02 10:58:09 -0700715 public boolean exitFullscreen() {
716 if (mFullScreen) {
717 Controller controller = (Controller)mWebViewController;
718 controller.getUi().showFullscreen(false);
719 if (getWebView() != null)
720 getWebView().exitFullscreen();
721 mFullScreen = false;
722 return true;
723 }
724 return false;
725 }
726
727
728
729
Grace Kloba22ac16e2009-10-07 18:00:23 -0700730 // -------------------------------------------------------------------------
731 // WebChromeClient implementation for the main WebView
732 // -------------------------------------------------------------------------
733
734 private final WebChromeClient mWebChromeClient = new WebChromeClient() {
735 // Helper method to create a new tab or sub window.
736 private void createWindow(final boolean dialog, final Message msg) {
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700737 this.createWindow(dialog, msg, null, false);
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700738 }
739
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700740 private void createWindow(final boolean dialog, final Message msg, final String url,
741 final boolean opener_suppressed) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700742 WebView.WebViewTransport transport =
743 (WebView.WebViewTransport) msg.obj;
744 if (dialog) {
745 createSubWindow();
Michael Kolb8233fac2010-10-26 16:08:53 -0700746 mWebViewController.attachSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700747 transport.setWebView(mSubView);
748 } else {
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700749 final Tab newTab = mWebViewController.openTab(url,
John Reck5949c662011-05-27 09:52:29 -0700750 Tab.this, true, true);
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700751 // This is special case for rendering links on a webpage in
752 // a new tab. If opener is suppressed, the WebContents created
753 // by the content layer are not fully initialized. This check
754 // will prevent content layer from overriding WebContents
755 // created by new tab with the uninitialized instance.
756 if (!opener_suppressed) {
757 transport.setWebView(newTab.getWebView());
758 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700759 }
760 msg.sendToTarget();
761 }
762
763 @Override
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700764 public void toggleFullscreenModeForTab(boolean enterFullscreen) {
765 if (mWebViewController instanceof Controller) {
Vivek Sekharf96064b2014-07-28 16:32:34 -0700766 setTabFullscreen(enterFullscreen);
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700767 }
768 }
769
770 @Override
Tarun Nainani8eb00912014-07-17 12:28:32 -0700771 public void onOffsetsForFullscreenChanged(float topControlsOffsetYPix,
772 float contentOffsetYPix,
773 float overdrawBottomHeightPix) {
774 if (mWebViewController instanceof Controller) {
Vivek Sekhar8ee3abb2014-07-14 12:32:05 -0700775 Controller controller = (Controller)mWebViewController;
Tarun Nainani8eb00912014-07-17 12:28:32 -0700776 controller.getUi().translateTitleBar(topControlsOffsetYPix);
Vivek Sekhar11b40062015-06-24 11:49:04 -0700777 // Resize the viewport if top controls is not visible
Vivek Sekhar2ee19a32015-07-02 17:03:57 -0700778 if (mMainView != null &&
779 (topControlsOffsetYPix == 0.0f || contentOffsetYPix == 0.0f))
Vivek Sekhar11b40062015-06-24 11:49:04 -0700780 ((BrowserWebView)mMainView).enableTopControls(
781 (topControlsOffsetYPix == 0.0f) ? true : false);
Vivek Sekhar8ee3abb2014-07-14 12:32:05 -0700782 }
783 }
784
785 @Override
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700786 public boolean isTabFullScreen() {
Vivek Sekharf96064b2014-07-28 16:32:34 -0700787 return mFullScreen;
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700788 }
789
790 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700791 public boolean onCreateWindow(WebView view, final boolean dialog,
792 final boolean userGesture, final Message resultMsg) {
793 // only allow new window or sub window for the foreground case
794 if (!mInForeground) {
795 return false;
796 }
797 // Short-circuit if we can't create any more tabs or sub windows.
798 if (dialog && mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -0700799 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700800 .setTitle(R.string.too_many_subwindows_dialog_title)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200801 .setIconAttribute(android.R.attr.alertDialogIcon)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700802 .setMessage(R.string.too_many_subwindows_dialog_message)
803 .setPositiveButton(R.string.ok, null)
804 .show();
805 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700806 } else if (!mWebViewController.getTabControl().canCreateNewTab()) {
Michael Kolb14612442011-06-24 13:06:29 -0700807 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700808 .setTitle(R.string.too_many_windows_dialog_title)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200809 .setIconAttribute(android.R.attr.alertDialogIcon)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700810 .setMessage(R.string.too_many_windows_dialog_message)
811 .setPositiveButton(R.string.ok, null)
812 .show();
813 return false;
814 }
815
816 // Short-circuit if this was a user gesture.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800817 if (userGesture || !mSettings.blockPopupWindows()) {
Tarun Nainani4f5137d2015-04-16 17:26:18 -0700818 WebView.WebViewTransport transport =
819 (WebView.WebViewTransport) resultMsg.obj;
820 CreateWindowParams windowParams = transport.getCreateWindowParams();
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700821 if (windowParams.mOpenerSuppressed) {
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700822 createWindow(dialog, resultMsg, windowParams.mURL, true);
823 // This is special case for rendering links on a webpage in
824 // a new tab. If opener is suppressed, the WebContents created
825 // by the content layer are not fully initialized. Returning false
826 // will prevent content layer from overriding WebContents
827 // created by new tab with the uninitialized instance.
828 return false;
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700829 }
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700830
831 createWindow(dialog, resultMsg);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700832 return true;
833 }
834
Tarun Nainani4f5137d2015-04-16 17:26:18 -0700835 createWindow(dialog, resultMsg);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700836 return true;
837 }
838
839 @Override
Patrick Scotteb5061b2009-11-18 15:00:30 -0500840 public void onRequestFocus(WebView view) {
841 if (!mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700842 mWebViewController.switchToTab(Tab.this);
Patrick Scotteb5061b2009-11-18 15:00:30 -0500843 }
844 }
845
846 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700847 public void onCloseWindow(WebView window) {
Michael Kolbc831b632011-05-11 09:30:34 -0700848 if (mParent != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700849 // JavaScript can only close popup window.
850 if (mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700851 mWebViewController.switchToTab(mParent);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700852 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700853 mWebViewController.closeTab(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700854 }
855 }
856
857 @Override
858 public void onProgressChanged(WebView view, int newProgress) {
John Reck30c714c2010-12-16 17:30:34 -0800859 mPageLoadProgress = newProgress;
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800860 if (newProgress == 100) {
861 mInPageLoad = false;
862 }
John Reck30c714c2010-12-16 17:30:34 -0800863 mWebViewController.onProgressChanged(Tab.this);
Michael Kolb72864272012-05-03 15:42:15 -0700864 if (mUpdateThumbnail && newProgress == 100) {
865 mUpdateThumbnail = false;
866 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700867 }
868
869 @Override
Leon Scroggins21d9b902010-03-11 09:33:11 -0500870 public void onReceivedTitle(WebView view, final String title) {
John Reck30c714c2010-12-16 17:30:34 -0800871 mCurrentState.mTitle = title;
Michael Kolb8233fac2010-10-26 16:08:53 -0700872 mWebViewController.onReceivedTitle(Tab.this, title);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700873 }
874
875 @Override
876 public void onReceivedIcon(WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -0800877 mCurrentState.mFavicon = icon;
Michael Kolb8233fac2010-10-26 16:08:53 -0700878 mWebViewController.onFavicon(Tab.this, view, icon);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700879 }
880
881 @Override
882 public void onReceivedTouchIconUrl(WebView view, String url,
883 boolean precomposed) {
Michael Kolb14612442011-06-24 13:06:29 -0700884 final ContentResolver cr = mContext.getContentResolver();
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400885 // Let precomposed icons take precedence over non-composed
886 // icons.
887 if (precomposed && mTouchIconLoader != null) {
888 mTouchIconLoader.cancel(false);
889 mTouchIconLoader = null;
890 }
891 // Have only one async task at a time.
892 if (mTouchIconLoader == null) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700893 mTouchIconLoader = new DownloadTouchIcon(Tab.this,
Michael Kolb14612442011-06-24 13:06:29 -0700894 mContext, cr, view);
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400895 mTouchIconLoader.execute(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700896 }
Pankaj Garg21dad562015-07-02 17:17:24 -0700897 mTouchIconUrl = url;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700898 }
899
900 @Override
901 public void onShowCustomView(View view,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800902 CustomViewCallback callback) {
Michael Kolb14612442011-06-24 13:06:29 -0700903 Activity activity = mWebViewController.getActivity();
904 if (activity != null) {
905 onShowCustomView(view, activity.getRequestedOrientation(), callback);
906 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400907 }
908
909 @Override
910 public void onShowCustomView(View view, int requestedOrientation,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800911 CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700912 if (mInForeground) mWebViewController.showCustomView(Tab.this, view,
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400913 requestedOrientation, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700914 }
915
916 @Override
917 public void onHideCustomView() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700918 if (mInForeground) mWebViewController.hideCustomView();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700919 }
920
921 /**
922 * The origin has exceeded its database quota.
923 * @param url the URL that exceeded the quota
924 * @param databaseIdentifier the identifier of the database on which the
925 * transaction that caused the quota overflow was run
926 * @param currentQuota the current quota for the origin.
927 * @param estimatedSize the estimated size of the database.
928 * @param totalUsedQuota is the sum of all origins' quota.
929 * @param quotaUpdater The callback to run when a decision to allow or
930 * deny quota has been made. Don't forget to call this!
931 */
932 @Override
933 public void onExceededDatabaseQuota(String url,
934 String databaseIdentifier, long currentQuota, long estimatedSize,
935 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -0700936 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -0700937 .onExceededDatabaseQuota(url, databaseIdentifier,
938 currentQuota, estimatedSize, totalUsedQuota,
939 quotaUpdater);
940 }
941
942 /**
943 * The Application Cache has exceeded its max size.
944 * @param spaceNeeded is the amount of disk space that would be needed
945 * in order for the last appcache operation to succeed.
946 * @param totalUsedQuota is the sum of all origins' quota.
947 * @param quotaUpdater A callback to inform the WebCore thread that a
948 * new app cache size is available. This callback must always
949 * be executed at some point to ensure that the sleeping
950 * WebCore thread is woken up.
951 */
952 @Override
953 public void onReachedMaxAppCacheSize(long spaceNeeded,
954 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -0700955 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -0700956 .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota,
957 quotaUpdater);
958 }
959
Ben Murdoch65acc352009-11-19 18:16:04 +0000960 /* Adds a JavaScript error message to the system log and if the JS
961 * console is enabled in the about:debug options, to that console
962 * also.
Ben Murdochc42addf2010-01-28 15:19:59 +0000963 * @param consoleMessage the message object.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700964 */
965 @Override
Ben Murdochc42addf2010-01-28 15:19:59 +0000966 public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Jeff Hamilton47654f42010-09-07 09:57:51 -0500967 // Don't log console messages in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -0700968 if (isPrivateBrowsingEnabled()) return true;
Jeff Hamilton47654f42010-09-07 09:57:51 -0500969
Ben Murdochc42addf2010-01-28 15:19:59 +0000970 String message = "Console: " + consoleMessage.message() + " "
971 + consoleMessage.sourceId() + ":"
972 + consoleMessage.lineNumber();
973
974 switch (consoleMessage.messageLevel()) {
975 case TIP:
976 Log.v(CONSOLE_LOGTAG, message);
977 break;
978 case LOG:
979 Log.i(CONSOLE_LOGTAG, message);
980 break;
981 case WARNING:
982 Log.w(CONSOLE_LOGTAG, message);
983 break;
984 case ERROR:
985 Log.e(CONSOLE_LOGTAG, message);
986 break;
987 case DEBUG:
988 Log.d(CONSOLE_LOGTAG, message);
989 break;
990 }
991
992 return true;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700993 }
994
995 /**
996 * Ask the browser for an icon to represent a <video> element.
997 * This icon will be used if the Web page did not specify a poster attribute.
998 * @return Bitmap The icon or null if no such icon is available.
999 */
1000 @Override
1001 public Bitmap getDefaultVideoPoster() {
1002 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001003 return mWebViewController.getDefaultVideoPoster();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001004 }
1005 return null;
1006 }
1007
1008 /**
1009 * Ask the host application for a custom progress view to show while
1010 * a <video> is loading.
1011 * @return View The progress view.
1012 */
1013 @Override
1014 public View getVideoLoadingProgressView() {
1015 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001016 return mWebViewController.getVideoLoadingProgressView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001017 }
1018 return null;
1019 }
1020
1021 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00001022 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001023 if (mInForeground) {
Ben Murdoch8cad4132012-01-11 10:56:43 +00001024 mWebViewController.openFileChooser(uploadMsg, acceptType, capture);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001025 } else {
1026 uploadMsg.onReceiveValue(null);
1027 }
1028 }
1029
Vivek Sekharb54614f2014-05-01 19:03:37 -07001030 @Override
1031 public void showFileChooser(ValueCallback<String[]> uploadFilePaths, String acceptTypes,
1032 boolean capture) {
1033 if (mInForeground) {
1034 mWebViewController.showFileChooser(uploadFilePaths, acceptTypes, capture);
1035 } else {
1036 uploadFilePaths.onReceiveValue(null);
1037 }
1038 }
1039
Grace Kloba22ac16e2009-10-07 18:00:23 -07001040 /**
1041 * Deliver a list of already-visited URLs
1042 */
1043 @Override
1044 public void getVisitedHistory(final ValueCallback<String[]> callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001045 mWebViewController.getVisitedHistory(callback);
1046 }
Ben Murdoch8029a772010-11-16 11:58:21 +00001047
1048 @Override
1049 public void setupAutoFill(Message message) {
1050 // Prompt the user to set up their profile.
1051 final Message msg = message;
Michael Kolb14612442011-06-24 13:06:29 -07001052 AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
1053 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
Ben Murdoch1d676b62011-01-17 12:54:24 +00001054 Context.LAYOUT_INFLATER_SERVICE);
1055 final View layout = inflater.inflate(R.layout.setup_autofill_dialog, null);
1056
1057 builder.setView(layout)
1058 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1059 @Override
1060 public void onClick(DialogInterface dialog, int id) {
1061 CheckBox disableAutoFill = (CheckBox) layout.findViewById(
1062 R.id.setup_autofill_dialog_disable_autofill);
1063
1064 if (disableAutoFill.isChecked()) {
1065 // Disable autofill and show a toast with how to turn it on again.
John Reck35e9dd62011-04-25 09:01:54 -07001066 mSettings.setAutofillEnabled(false);
Michael Kolb14612442011-06-24 13:06:29 -07001067 Toast.makeText(mContext,
Ben Murdoch1d676b62011-01-17 12:54:24 +00001068 R.string.autofill_setup_dialog_negative_toast,
1069 Toast.LENGTH_LONG).show();
1070 } else {
1071 // Take user to the AutoFill profile editor. When they return,
1072 // we will send the message that we pass here which will trigger
1073 // the form to get filled out with their new profile.
1074 mWebViewController.setupAutoFill(msg);
1075 }
1076 }
1077 })
1078 .setNegativeButton(R.string.cancel, null)
1079 .show();
Ben Murdoch8029a772010-11-16 11:58:21 +00001080 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001081 };
1082
1083 // -------------------------------------------------------------------------
1084 // WebViewClient implementation for the sub window
1085 // -------------------------------------------------------------------------
1086
1087 // Subclass of WebViewClient used in subwindows to notify the main
1088 // WebViewClient of certain WebView activities.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001089 private static class SubWindowClient extends WebViewClient {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001090 // The main WebViewClient.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001091 private final WebViewClient mClient;
Michael Kolb8233fac2010-10-26 16:08:53 -07001092 private final WebViewController mController;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001093
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001094 SubWindowClient(WebViewClient client, WebViewController controller) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001095 mClient = client;
Michael Kolb8233fac2010-10-26 16:08:53 -07001096 mController = controller;
Leon Scroggins III211ba542010-04-19 13:21:13 -04001097 }
1098 @Override
1099 public void onPageStarted(WebView view, String url, Bitmap favicon) {
1100 // Unlike the others, do not call mClient's version, which would
1101 // change the progress bar. However, we do want to remove the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001102 // find or select dialog.
Michael Kolb8233fac2010-10-26 16:08:53 -07001103 mController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001104 }
1105 @Override
1106 public void doUpdateVisitedHistory(WebView view, String url,
1107 boolean isReload) {
1108 mClient.doUpdateVisitedHistory(view, url, isReload);
1109 }
1110 @Override
1111 public boolean shouldOverrideUrlLoading(WebView view, String url) {
1112 return mClient.shouldOverrideUrlLoading(view, url);
1113 }
1114 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -07001115 public void onReceivedHttpAuthRequest(WebView view,
1116 HttpAuthHandler handler, String host, String realm) {
1117 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
1118 }
1119 @Override
1120 public void onFormResubmission(WebView view, Message dontResend,
1121 Message resend) {
1122 mClient.onFormResubmission(view, dontResend, resend);
1123 }
1124 @Override
1125 public void onReceivedError(WebView view, int errorCode,
1126 String description, String failingUrl) {
1127 mClient.onReceivedError(view, errorCode, description, failingUrl);
1128 }
1129 @Override
1130 public boolean shouldOverrideKeyEvent(WebView view,
1131 android.view.KeyEvent event) {
1132 return mClient.shouldOverrideKeyEvent(view, event);
1133 }
1134 @Override
1135 public void onUnhandledKeyEvent(WebView view,
1136 android.view.KeyEvent event) {
1137 mClient.onUnhandledKeyEvent(view, event);
1138 }
1139 }
1140
1141 // -------------------------------------------------------------------------
1142 // WebChromeClient implementation for the sub window
1143 // -------------------------------------------------------------------------
1144
1145 private class SubWindowChromeClient extends WebChromeClient {
1146 // The main WebChromeClient.
1147 private final WebChromeClient mClient;
1148
1149 SubWindowChromeClient(WebChromeClient client) {
1150 mClient = client;
1151 }
1152 @Override
1153 public void onProgressChanged(WebView view, int newProgress) {
1154 mClient.onProgressChanged(view, newProgress);
1155 }
1156 @Override
1157 public boolean onCreateWindow(WebView view, boolean dialog,
1158 boolean userGesture, android.os.Message resultMsg) {
1159 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
1160 }
1161 @Override
1162 public void onCloseWindow(WebView window) {
1163 if (window != mSubView) {
1164 Log.e(LOGTAG, "Can't close the window");
1165 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001166 mWebViewController.dismissSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001167 }
1168 }
1169
1170 // -------------------------------------------------------------------------
1171
1172 // Construct a new tab
Michael Kolb7bcafde2011-05-09 13:55:59 -07001173 Tab(WebViewController wvcontroller, WebView w) {
John Reck1cf4b792011-07-26 10:22:22 -07001174 this(wvcontroller, w, null);
1175 }
1176
1177 Tab(WebViewController wvcontroller, Bundle state) {
1178 this(wvcontroller, null, state);
1179 }
1180
1181 Tab(WebViewController wvcontroller, WebView w, Bundle state) {
Vivek Sekhar0c609cd2015-12-10 16:00:05 -08001182 this(wvcontroller, null, state, false);
1183 }
1184
1185 Tab(WebViewController wvcontroller, WebView w, Bundle state, boolean backgroundTab) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001186 mWebViewController = wvcontroller;
Michael Kolb14612442011-06-24 13:06:29 -07001187 mContext = mWebViewController.getContext();
John Reck35e9dd62011-04-25 09:01:54 -07001188 mSettings = BrowserSettings.getInstance();
Michael Kolb14612442011-06-24 13:06:29 -07001189 mDataController = DataController.getInstance(mContext);
1190 mCurrentState = new PageState(mContext, w != null
John Reck847b5322011-04-14 17:02:18 -07001191 ? w.isPrivateBrowsingEnabled() : false);
Tarun Nainani8084c822014-06-25 13:38:06 -07001192 setTimeStamp();
Michael Kolb8233fac2010-10-26 16:08:53 -07001193 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001194 mInForeground = false;
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001195 mWebViewDestroyedByMemoryMonitor = false;
Vivek Sekhar0c609cd2015-12-10 16:00:05 -08001196 mBackgroundTab = backgroundTab;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001197
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001198 mDownloadListener = new BrowserDownloadListener() {
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001199 public void onDownloadStart(String url, String userAgent,
Pankaj Garg5762b362015-11-02 07:57:06 -08001200 String contentDisposition, String mimetype, String referer, String auth,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001201 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001202 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
Pankaj Garg5762b362015-11-02 07:57:06 -08001203 mimetype, referer, auth, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001204 }
1205 };
1206
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001207 mCaptureWidth = mContext.getResources().getDimensionPixelSize(R.dimen.tab_thumbnail_width);
1208 mCaptureHeight =mContext.getResources().getDimensionPixelSize(R.dimen.tab_thumbnail_height);
1209
1210 initCaptureBitmap();
1211
John Reck1cf4b792011-07-26 10:22:22 -07001212 restoreState(state);
John Reck52be4782011-08-26 15:37:29 -07001213 if (getId() == -1) {
1214 mId = TabControl.getNextId();
1215 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001216 setWebView(w);
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -07001217
1218 UI ui = ((Controller)mWebViewController).getUi();
1219 if (ui instanceof BaseUi) {
1220 TitleBar titleBar = ((BaseUi)ui).getTitleBar();
1221 if (titleBar != null) {
1222 NavigationBarBase navBar = titleBar.getNavigationBar();
1223 navBar.showCurrentFavicon(this); // Show the default Favicon while loading a new page
1224 }
1225 }
1226
Michael Kolb9ef259a2011-07-12 15:33:08 -07001227 mHandler = new Handler() {
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001228 @Override
Michael Kolb9ef259a2011-07-12 15:33:08 -07001229 public void handleMessage(Message m) {
John Reck1cf4b792011-07-26 10:22:22 -07001230 switch (m.what) {
1231 case MSG_CAPTURE:
1232 capture();
1233 break;
1234 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001235 }
1236 };
Pankaj Garg1c13cab2015-05-12 11:52:17 -07001237
1238 mFirstPixelObservable = new Observable();
1239 mFirstPixelObservable.set(false);
1240 mTabHistoryUpdateObservable = new Observable();
John Reck1cf4b792011-07-26 10:22:22 -07001241 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001242
Pankaj Gargb4b4f012015-08-04 16:19:14 -07001243 public void initCaptureBitmap() {
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001244 mCapture = Bitmap.createBitmap(mCaptureWidth, mCaptureHeight, Bitmap.Config.RGB_565);
1245 mCapture.eraseColor(Color.WHITE);
Michael Kolb72864272012-05-03 15:42:15 -07001246 }
1247
Mathew Inwoode09305e2011-09-02 12:03:26 +01001248 /**
1249 * This is used to get a new ID when the tab has been preloaded, before it is displayed and
1250 * added to TabControl. Preloaded tabs can be created before restoreInstanceState, leading
1251 * to overlapping IDs between the preloaded and restored tabs.
1252 */
1253 public void refreshIdAfterPreload() {
1254 mId = TabControl.getNextId();
1255 }
1256
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001257 public void setController(WebViewController ctl) {
1258 mWebViewController = ctl;
1259
John Reck1cf4b792011-07-26 10:22:22 -07001260 if (mWebViewController.shouldCaptureThumbnails()) {
1261 synchronized (Tab.this) {
1262 if (mCapture == null) {
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001263 initCaptureBitmap();
1264 if (mInForeground && !mHandler.hasMessages(MSG_CAPTURE)) {
1265 mHandler.sendEmptyMessageDelayed(MSG_CAPTURE, CAPTURE_DELAY);
John Reck1cf4b792011-07-26 10:22:22 -07001266 }
1267 }
1268 }
1269 } else {
1270 synchronized (Tab.this) {
1271 mCapture = null;
1272 deleteThumbnail();
1273 }
1274 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001275 }
1276
Michael Kolbc831b632011-05-11 09:30:34 -07001277 public long getId() {
1278 return mId;
1279 }
1280
Michael Kolb91911a22012-01-17 11:21:25 -08001281 void setWebView(WebView w) {
1282 setWebView(w, true);
1283 }
1284
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001285 public boolean isNativeActive(){
1286 if (mMainView == null)
1287 return false;
1288 return true;
1289 }
1290
1291 public void setTimeStamp(){
1292 Date d = new Date();
1293 timestamp = (new Timestamp(d.getTime()));
1294 }
1295
1296 public Timestamp getTimestamp() {
1297 return timestamp;
1298 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001299 /**
1300 * Sets the WebView for this tab, correctly removing the old WebView from
1301 * the container view.
1302 */
Michael Kolb91911a22012-01-17 11:21:25 -08001303 void setWebView(WebView w, boolean restore) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001304 if (mMainView == w) {
1305 return;
1306 }
Michael Kolba713ec82010-11-29 17:27:06 -08001307
Michael Kolba713ec82010-11-29 17:27:06 -08001308 mWebViewController.onSetWebView(this, w);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001309
John Reck1cf4b792011-07-26 10:22:22 -07001310 if (mMainView != null) {
John Reck8ee633f2011-08-09 16:00:35 -07001311 mMainView.setPictureListener(null);
John Reck1cf4b792011-07-26 10:22:22 -07001312 if (w != null) {
1313 syncCurrentState(w, null);
Vivek Sekhar0c609cd2015-12-10 16:00:05 -08001314 } else if(!mWebViewDestroyedByMemoryMonitor) {
1315 mCurrentState = new PageState(mContext,
1316 mMainView.isPrivateBrowsingEnabled());
John Reck1cf4b792011-07-26 10:22:22 -07001317 }
1318 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001319 // set the new one
1320 mMainView = w;
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001321
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001322 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001323 if (mMainView != null) {
1324 mMainView.setWebViewClient(mWebViewClient);
1325 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001326 // Attach DownloadManager so that downloads can start in an active
1327 // or a non-active window. This can happen when going to a site that
1328 // does a redirect after a period of time. The user could have
1329 // switched to another tab while waiting for the download to start.
1330 mMainView.setDownloadListener(mDownloadListener);
John Reck8ee633f2011-08-09 16:00:35 -07001331 TabControl tc = mWebViewController.getTabControl();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001332 if (tc != null /*&& tc.getOnThumbnailUpdatedListener() != null*/) {
John Reck8ee633f2011-08-09 16:00:35 -07001333 mMainView.setPictureListener(this);
1334 }
Michael Kolb91911a22012-01-17 11:21:25 -08001335 if (restore && (mSavedState != null)) {
John Reck8b9bb8b2012-03-08 13:19:40 -08001336 restoreUserAgent();
John Reck6c2e2f32011-08-22 13:41:23 -07001337 WebBackForwardList restoredState
1338 = mMainView.restoreState(mSavedState);
1339 if (restoredState == null || restoredState.getSize() == 0) {
1340 Log.w(LOGTAG, "Failed to restore WebView state!");
1341 loadUrl(mCurrentState.mOriginalUrl, null);
1342 }
Vivek Sekhar0c609cd2015-12-10 16:00:05 -08001343 mWebViewDestroyedByMemoryMonitor = false;
John Reck1cf4b792011-07-26 10:22:22 -07001344 mSavedState = null;
Vivek Sekhar0c609cd2015-12-10 16:00:05 -08001345 } else if(restore && mBackgroundTab && mWebViewDestroyedByMemoryMonitor) {
1346 loadUrl(mCurrentState.mOriginalUrl, null);
1347 mWebViewDestroyedByMemoryMonitor = false;
John Reck1cf4b792011-07-26 10:22:22 -07001348 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001349 }
1350 }
1351
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001352 public void destroyThroughMemoryMonitor() {
1353 mWebViewDestroyedByMemoryMonitor = true;
1354 destroy();
1355 }
1356
Grace Kloba22ac16e2009-10-07 18:00:23 -07001357 /**
1358 * Destroy the tab's main WebView and subWindow if any
1359 */
1360 void destroy() {
Tarun Nainani2c1dd7c2014-07-05 16:40:12 -07001361
Matthew Huib7f2e9c2014-04-16 11:12:37 -04001362 if (mPostponeDestroy) {
1363 mShouldDestroy = true;
1364 return;
1365 }
1366 mShouldDestroy = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001367 if (mMainView != null) {
1368 dismissSubWindow();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001369 // save the WebView to call destroy() after detach it from the tab
Vivek Sekhar53ef8932015-06-18 16:51:43 -07001370 final WebView webView = mMainView;
Vivek Sekhar2ee19a32015-07-02 17:03:57 -07001371 setWebView(null);
Site Mao61b68212015-07-16 10:56:31 -07001372 if (!mWebViewDestroyedByMemoryMonitor && !BaseUi.isUiLowPowerMode()) {
Pankaj Garg036365b2015-09-14 11:21:19 -07001373 final int destroyedTabIdx = (int) mId;
Vivek Sekhar2ee19a32015-07-02 17:03:57 -07001374 // Tabs can be reused with new instance of WebView so delete the snapshots
Vivek Sekhar53ef8932015-06-18 16:51:43 -07001375 webView.getSnapshotIds(new ValueCallback<List<Integer>>() {
1376 @Override
1377 public void onReceiveValue(List<Integer> ids) {
Vivek Sekhar53ef8932015-06-18 16:51:43 -07001378 for (Integer id : ids) {
Pankaj Garg036365b2015-09-14 11:21:19 -07001379 if (getTabIdxFromCaptureIdx(id) == destroyedTabIdx) {
Vivek Sekhar53ef8932015-06-18 16:51:43 -07001380 webView.deleteSnapshot(id);
1381 }
1382 }
Vivek Sekhar53ef8932015-06-18 16:51:43 -07001383 webView.destroy();
Pankaj Garg1c13cab2015-05-12 11:52:17 -07001384 }
Vivek Sekhar53ef8932015-06-18 16:51:43 -07001385 });
Vivek Sekhar2ee19a32015-07-02 17:03:57 -07001386 } else {
1387 webView.destroy();
Pankaj Garg1c13cab2015-05-12 11:52:17 -07001388 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001389 }
1390 }
1391
Matthew Huib7f2e9c2014-04-16 11:12:37 -04001392 private boolean mPostponeDestroy = false;
1393 private boolean mShouldDestroy = false;
1394
1395 public void postponeDestroy() {
1396 mPostponeDestroy = true;
1397 }
1398
1399 public void performPostponedDestroy() {
1400 mPostponeDestroy = false;
1401 if (mShouldDestroy) {
1402 destroy();
1403 }
1404 }
1405
Grace Kloba22ac16e2009-10-07 18:00:23 -07001406 /**
1407 * Remove the tab from the parent
1408 */
1409 void removeFromTree() {
1410 // detach the children
Michael Kolbc831b632011-05-11 09:30:34 -07001411 if (mChildren != null) {
1412 for(Tab t : mChildren) {
1413 t.setParent(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001414 }
1415 }
1416 // remove itself from the parent list
Michael Kolbc831b632011-05-11 09:30:34 -07001417 if (mParent != null) {
1418 mParent.mChildren.remove(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001419 }
Tarun Nainani8eb00912014-07-17 12:28:32 -07001420
1421 mCapture = null;
John Reck1cf4b792011-07-26 10:22:22 -07001422 deleteThumbnail();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001423 }
1424
1425 /**
1426 * Create a new subwindow unless a subwindow already exists.
1427 * @return True if a new subwindow was created. False if one already exists.
1428 */
1429 boolean createSubWindow() {
1430 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001431 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001432 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001433 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001434 mSubView.setWebChromeClient(new SubWindowChromeClient(
1435 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001436 // Set a different DownloadListener for the mSubView, since it will
1437 // just need to dismiss the mSubView, rather than close the Tab
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001438 mSubView.setDownloadListener(new BrowserDownloadListener() {
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001439 public void onDownloadStart(String url, String userAgent,
Pankaj Garg5762b362015-11-02 07:57:06 -08001440 String contentDisposition, String mimetype, String referer, String auth,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001441 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001442 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Pankaj Garg5762b362015-11-02 07:57:06 -08001443 contentDisposition, mimetype, referer, auth, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001444 if (mSubView.copyBackForwardList().getSize() == 0) {
1445 // This subwindow was opened for the sole purpose of
1446 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001447 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001448 }
1449 }
1450 });
Michael Kolb14612442011-06-24 13:06:29 -07001451 mSubView.setOnCreateContextMenuListener(mWebViewController.getActivity());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001452 return true;
1453 }
1454 return false;
1455 }
1456
1457 /**
1458 * Dismiss the subWindow for the tab.
1459 */
1460 void dismissSubWindow() {
1461 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001462 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001463 mSubView.destroy();
1464 mSubView = null;
1465 mSubViewContainer = null;
1466 }
1467 }
1468
Grace Kloba22ac16e2009-10-07 18:00:23 -07001469
1470 /**
1471 * Set the parent tab of this tab.
1472 */
Michael Kolbc831b632011-05-11 09:30:34 -07001473 void setParent(Tab parent) {
John Reck52be4782011-08-26 15:37:29 -07001474 if (parent == this) {
1475 throw new IllegalStateException("Cannot set parent to self!");
1476 }
Michael Kolbc831b632011-05-11 09:30:34 -07001477 mParent = parent;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001478 // This tab may have been freed due to low memory. If that is the case,
Michael Kolbc831b632011-05-11 09:30:34 -07001479 // the parent tab id is already saved. If we are changing that id
Grace Kloba22ac16e2009-10-07 18:00:23 -07001480 // (most likely due to removing the parent tab) we must update the
Michael Kolbc831b632011-05-11 09:30:34 -07001481 // parent tab id in the saved Bundle.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001482 if (mSavedState != null) {
1483 if (parent == null) {
1484 mSavedState.remove(PARENTTAB);
1485 } else {
Michael Kolbc831b632011-05-11 09:30:34 -07001486 mSavedState.putLong(PARENTTAB, parent.getId());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001487 }
1488 }
John Reckb0a86db2011-05-24 14:05:58 -07001489
1490 // Sync the WebView useragent with the parent
1491 if (parent != null && mSettings.hasDesktopUseragent(parent.getWebView())
1492 != mSettings.hasDesktopUseragent(getWebView())) {
1493 mSettings.toggleDesktopUseragent(getWebView());
1494 }
John Reck52be4782011-08-26 15:37:29 -07001495
1496 if (parent != null && parent.getId() == getId()) {
1497 throw new IllegalStateException("Parent has same ID as child!");
1498 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001499 }
1500
1501 /**
Michael Kolbc831b632011-05-11 09:30:34 -07001502 * If this Tab was created through another Tab, then this method returns
1503 * that Tab.
1504 * @return the Tab parent or null
1505 */
1506 public Tab getParent() {
1507 return mParent;
1508 }
1509
1510 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001511 * When a Tab is created through the content of another Tab, then we
1512 * associate the Tabs.
1513 * @param child the Tab that was created from this Tab
1514 */
1515 void addChildTab(Tab child) {
Michael Kolbc831b632011-05-11 09:30:34 -07001516 if (mChildren == null) {
1517 mChildren = new Vector<Tab>();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001518 }
Michael Kolbc831b632011-05-11 09:30:34 -07001519 mChildren.add(child);
1520 child.setParent(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001521 }
1522
Michael Kolbc831b632011-05-11 09:30:34 -07001523 Vector<Tab> getChildren() {
1524 return mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001525 }
1526
1527 void resume() {
1528 if (mMainView != null) {
John Reck56c1fcf2011-08-17 10:15:16 -07001529 setupHwAcceleration(mMainView);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001530 mMainView.onResume();
1531 if (mSubView != null) {
1532 mSubView.onResume();
1533 }
1534 }
1535 }
1536
John Reck56c1fcf2011-08-17 10:15:16 -07001537 private void setupHwAcceleration(View web) {
1538 if (web == null) return;
1539 BrowserSettings settings = BrowserSettings.getInstance();
1540 if (settings.isHardwareAccelerated()) {
1541 web.setLayerType(View.LAYER_TYPE_NONE, null);
1542 } else {
1543 web.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
1544 }
1545 }
1546
Grace Kloba22ac16e2009-10-07 18:00:23 -07001547 void pause() {
1548 if (mMainView != null) {
1549 mMainView.onPause();
1550 if (mSubView != null) {
1551 mSubView.onPause();
1552 }
1553 }
1554 }
1555
1556 void putInForeground() {
John Reck8ee633f2011-08-09 16:00:35 -07001557 if (mInForeground) {
1558 return;
1559 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001560 mInForeground = true;
1561 resume();
Michael Kolb14612442011-06-24 13:06:29 -07001562 Activity activity = mWebViewController.getActivity();
1563 mMainView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001564 if (mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -07001565 mSubView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001566 }
Axesh R. Ajmerac6b5c322015-05-01 11:06:10 -07001567
Leon Scroggins1961ed22010-12-07 15:22:21 -05001568 mWebViewController.bookmarkedStatusHasChanged(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001569 }
1570
1571 void putInBackground() {
John Reck8ee633f2011-08-09 16:00:35 -07001572 if (!mInForeground) {
1573 return;
1574 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001575 mInForeground = false;
1576 pause();
1577 mMainView.setOnCreateContextMenuListener(null);
1578 if (mSubView != null) {
1579 mSubView.setOnCreateContextMenuListener(null);
1580 }
1581 }
1582
Michael Kolb8233fac2010-10-26 16:08:53 -07001583 boolean inForeground() {
1584 return mInForeground;
1585 }
1586
Grace Kloba22ac16e2009-10-07 18:00:23 -07001587 /**
1588 * Return the top window of this tab; either the subwindow if it is not
1589 * null or the main window.
1590 * @return The top window of this tab.
1591 */
1592 WebView getTopWindow() {
1593 if (mSubView != null) {
1594 return mSubView;
1595 }
1596 return mMainView;
1597 }
1598
1599 /**
1600 * Return the main window of this tab. Note: if a tab is freed in the
1601 * background, this can return null. It is only guaranteed to be
1602 * non-null for the current tab.
1603 * @return The main WebView of this tab.
1604 */
1605 WebView getWebView() {
1606 return mMainView;
1607 }
1608
Michael Kolba713ec82010-11-29 17:27:06 -08001609 void setViewContainer(View container) {
1610 mContainer = container;
1611 }
1612
Michael Kolb8233fac2010-10-26 16:08:53 -07001613 View getViewContainer() {
1614 return mContainer;
1615 }
1616
Grace Kloba22ac16e2009-10-07 18:00:23 -07001617 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001618 * Return whether private browsing is enabled for the main window of
1619 * this tab.
1620 * @return True if private browsing is enabled.
1621 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001622 boolean isPrivateBrowsingEnabled() {
John Reck502a3532011-08-16 14:21:46 -07001623 return mCurrentState.mIncognito;
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001624 }
1625
1626 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001627 * Return the subwindow of this tab or null if there is no subwindow.
1628 * @return The subwindow of this tab or null.
1629 */
1630 WebView getSubWebView() {
1631 return mSubView;
1632 }
1633
Michael Kolb1514bb72010-11-22 09:11:48 -08001634 void setSubWebView(WebView subView) {
1635 mSubView = subView;
1636 }
1637
Michael Kolb8233fac2010-10-26 16:08:53 -07001638 View getSubViewContainer() {
1639 return mSubViewContainer;
1640 }
1641
Michael Kolb1514bb72010-11-22 09:11:48 -08001642 void setSubViewContainer(View subViewContainer) {
1643 mSubViewContainer = subViewContainer;
1644 }
1645
Grace Kloba22ac16e2009-10-07 18:00:23 -07001646
1647 /**
1648 * @return The application id string
1649 */
1650 String getAppId() {
1651 return mAppId;
1652 }
1653
1654 /**
1655 * Set the application id string
1656 * @param id
1657 */
1658 void setAppId(String id) {
1659 mAppId = id;
1660 }
1661
Michael Kolbe28b3472011-08-04 16:54:31 -07001662 boolean closeOnBack() {
1663 return mCloseOnBack;
1664 }
1665
1666 void setCloseOnBack(boolean close) {
1667 mCloseOnBack = close;
1668 }
1669
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001670 boolean getDerivedFromIntent() {
1671 return mDerivedFromIntent;
1672 }
1673
1674 void setDerivedFromIntent(boolean derived) {
1675 mDerivedFromIntent = derived;
1676 }
1677
Grace Kloba22ac16e2009-10-07 18:00:23 -07001678 String getUrl() {
John Reck324d4402011-01-11 16:56:42 -08001679 return UrlUtils.filteredUrl(mCurrentState.mUrl);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001680 }
1681
Tarun Nainani8eb00912014-07-17 12:28:32 -07001682
1683 protected void onPageFinished() {
1684 mPageFinished = true;
Ze G Riande2a675c22015-06-03 11:15:24 -07001685 isDistillable();
Tarun Nainani8eb00912014-07-17 12:28:32 -07001686 }
1687
1688 public boolean getPageFinishedStatus() {
1689 return mPageFinished;
1690 }
1691
John Reck49a603c2011-03-03 09:33:05 -08001692 String getOriginalUrl() {
John Reckdb22ec42011-06-29 11:31:24 -07001693 if (mCurrentState.mOriginalUrl == null) {
1694 return getUrl();
John Reck49a603c2011-03-03 09:33:05 -08001695 }
John Reckdb22ec42011-06-29 11:31:24 -07001696 return UrlUtils.filteredUrl(mCurrentState.mOriginalUrl);
John Reck49a603c2011-03-03 09:33:05 -08001697 }
1698
Grace Kloba22ac16e2009-10-07 18:00:23 -07001699 /**
John Reck30c714c2010-12-16 17:30:34 -08001700 * Get the title of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001701 */
1702 String getTitle() {
John Reck30c714c2010-12-16 17:30:34 -08001703 return mCurrentState.mTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001704 }
1705
1706 /**
John Reck30c714c2010-12-16 17:30:34 -08001707 * Get the favicon of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001708 */
1709 Bitmap getFavicon() {
John Reck1cf4b792011-07-26 10:22:22 -07001710 if (mCurrentState.mFavicon != null) {
1711 return mCurrentState.mFavicon;
1712 }
1713 return getDefaultFavicon(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001714 }
1715
Pankaj Garg32e1b942015-06-03 18:13:24 -07001716 public boolean hasFavicon() {
1717 return mCurrentState.mFavicon != null;
1718 }
1719
John Recke969cc52010-12-21 17:24:43 -08001720 public boolean isBookmarkedSite() {
1721 return mCurrentState.mIsBookmarkedSite;
1722 }
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001723
Grace Kloba22ac16e2009-10-07 18:00:23 -07001724 /**
Steve Block08a6f0c2011-10-06 12:12:53 +01001725 * Sets the security state, clears the SSL certificate error and informs
1726 * the controller.
1727 */
Steve Block2466eff2011-10-03 15:33:09 +01001728 private void setSecurityState(SecurityState securityState) {
1729 mCurrentState.mSecurityState = securityState;
1730 mWebViewController.onUpdatedSecurityState(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001731 }
1732
1733 /**
Steve Block2466eff2011-10-03 15:33:09 +01001734 * @return The tab's security state.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001735 */
Steve Block2466eff2011-10-03 15:33:09 +01001736 SecurityState getSecurityState() {
1737 return mCurrentState.mSecurityState;
John Reck30c714c2010-12-16 17:30:34 -08001738 }
1739
1740 int getLoadProgress() {
1741 if (mInPageLoad) {
1742 return mPageLoadProgress;
1743 }
1744 return 100;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001745 }
1746
1747 /**
1748 * @return TRUE if onPageStarted is called while onPageFinished is not
1749 * called yet.
1750 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001751 boolean inPageLoad() {
1752 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001753 }
1754
Grace Kloba22ac16e2009-10-07 18:00:23 -07001755 /**
John Reck1cf4b792011-07-26 10:22:22 -07001756 * @return The Bundle with the tab's state if it can be saved, otherwise null
Grace Kloba22ac16e2009-10-07 18:00:23 -07001757 */
John Reck1cf4b792011-07-26 10:22:22 -07001758 public Bundle saveState() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001759 // If the WebView is null it means we ran low on memory and we already
1760 // stored the saved state in mSavedState.
1761 if (mMainView == null) {
John Reck1cf4b792011-07-26 10:22:22 -07001762 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001763 }
John Reck6c2e2f32011-08-22 13:41:23 -07001764
1765 if (TextUtils.isEmpty(mCurrentState.mUrl)) {
John Reck1cf4b792011-07-26 10:22:22 -07001766 return null;
John Reck24f18262011-06-17 14:47:20 -07001767 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001768
1769 mSavedState = new Bundle();
John Reck6c2e2f32011-08-22 13:41:23 -07001770 WebBackForwardList savedList = mMainView.saveState(mSavedState);
1771 if (savedList == null || savedList.getSize() == 0) {
1772 Log.w(LOGTAG, "Failed to save back/forward list for "
1773 + mCurrentState.mUrl);
1774 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001775
Michael Kolbc831b632011-05-11 09:30:34 -07001776 mSavedState.putLong(ID, mId);
John Reck30c714c2010-12-16 17:30:34 -08001777 mSavedState.putString(CURRURL, mCurrentState.mUrl);
1778 mSavedState.putString(CURRTITLE, mCurrentState.mTitle);
John Reck1cf4b792011-07-26 10:22:22 -07001779 mSavedState.putBoolean(INCOGNITO, mMainView.isPrivateBrowsingEnabled());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001780 if (mAppId != null) {
1781 mSavedState.putString(APPID, mAppId);
1782 }
Michael Kolbe28b3472011-08-04 16:54:31 -07001783 mSavedState.putBoolean(CLOSEFLAG, mCloseOnBack);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001784 // Remember the parent tab so the relationship can be restored.
Michael Kolbc831b632011-05-11 09:30:34 -07001785 if (mParent != null) {
1786 mSavedState.putLong(PARENTTAB, mParent.mId);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001787 }
John Reckb0a86db2011-05-24 14:05:58 -07001788 mSavedState.putBoolean(USERAGENT,
1789 mSettings.hasDesktopUseragent(getWebView()));
John Reck1cf4b792011-07-26 10:22:22 -07001790 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001791 }
1792
1793 /*
1794 * Restore the state of the tab.
1795 */
John Reck1cf4b792011-07-26 10:22:22 -07001796 private void restoreState(Bundle b) {
1797 mSavedState = b;
1798 if (mSavedState == null) {
1799 return;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001800 }
1801 // Restore the internal state even if the WebView fails to restore.
1802 // This will maintain the app id, original url and close-on-exit values.
Michael Kolbc831b632011-05-11 09:30:34 -07001803 mId = b.getLong(ID);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001804 mAppId = b.getString(APPID);
Michael Kolbe28b3472011-08-04 16:54:31 -07001805 mCloseOnBack = b.getBoolean(CLOSEFLAG);
John Reck8b9bb8b2012-03-08 13:19:40 -08001806 restoreUserAgent();
John Reck1cf4b792011-07-26 10:22:22 -07001807 String url = b.getString(CURRURL);
1808 String title = b.getString(CURRTITLE);
1809 boolean incognito = b.getBoolean(INCOGNITO);
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -07001810 mCurrentState = new PageState(mContext, incognito, url);
John Reck1cf4b792011-07-26 10:22:22 -07001811 mCurrentState.mTitle = title;
1812 synchronized (Tab.this) {
1813 if (mCapture != null) {
John Reck4eadc342011-10-31 14:04:10 -07001814 DataController.getInstance(mContext).loadThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07001815 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001816 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001817 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04001818
John Reck8b9bb8b2012-03-08 13:19:40 -08001819 private void restoreUserAgent() {
1820 if (mMainView == null || mSavedState == null) {
1821 return;
1822 }
1823 if (mSavedState.getBoolean(USERAGENT)
1824 != mSettings.hasDesktopUseragent(mMainView)) {
1825 mSettings.toggleDesktopUseragent(mMainView);
1826 }
1827 }
1828
Leon Scroggins1961ed22010-12-07 15:22:21 -05001829 public void updateBookmarkedStatus() {
John Recke969cc52010-12-21 17:24:43 -08001830 mDataController.queryBookmarkStatus(getUrl(), mIsBookmarkCallback);
Leon Scroggins1961ed22010-12-07 15:22:21 -05001831 }
1832
John Recke969cc52010-12-21 17:24:43 -08001833 private DataController.OnQueryUrlIsBookmark mIsBookmarkCallback
1834 = new DataController.OnQueryUrlIsBookmark() {
1835 @Override
1836 public void onQueryUrlIsBookmark(String url, boolean isBookmark) {
Karthikeyan Periasamy66c06a12015-10-21 18:04:14 -07001837 if (TextUtils.isEmpty(mCurrentState.mUrl) || mCurrentState.mUrl.equals(url)) {
John Recke969cc52010-12-21 17:24:43 -08001838 mCurrentState.mIsBookmarkedSite = isBookmark;
1839 mWebViewController.bookmarkedStatusHasChanged(Tab.this);
1840 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001841 }
John Recke969cc52010-12-21 17:24:43 -08001842 };
Michael Kolb1acef692011-03-08 14:12:06 -08001843
Michael Kolbeb95db42011-03-03 10:38:40 -08001844 public Bitmap getScreenshot() {
John Reck1cf4b792011-07-26 10:22:22 -07001845 synchronized (Tab.this) {
1846 return mCapture;
1847 }
Michael Kolbeb95db42011-03-03 10:38:40 -08001848 }
1849
John Reck541f55a2011-06-07 16:34:43 -07001850 public boolean isSnapshot() {
John Reck541f55a2011-06-07 16:34:43 -07001851 return false;
1852 }
1853
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001854 private static class SaveCallback implements ValueCallback<String> {
1855 boolean onReceiveValueCalled = false;
1856 private String mPath;
John Reck68234a92012-04-19 15:27:12 -07001857
1858 @Override
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001859 public void onReceiveValue(String path) {
1860 this.onReceiveValueCalled = true;
1861 this.mPath = path;
John Reck68234a92012-04-19 15:27:12 -07001862 synchronized (this) {
1863 notifyAll();
John Reck8cc92352011-07-06 17:41:52 -07001864 }
John Reck541f55a2011-06-07 16:34:43 -07001865 }
John Reck68234a92012-04-19 15:27:12 -07001866
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001867 public String getPath() {
1868 return mPath;
1869 }
John Reck68234a92012-04-19 15:27:12 -07001870 }
1871
1872 /**
1873 * Must be called on the UI thread
1874 */
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001875 public ContentValues createSnapshotValues(Bitmap bm) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001876 WebView web = getWebView();
John Reck68234a92012-04-19 15:27:12 -07001877 if (web == null) return null;
John Reckd8c74522011-06-14 08:45:00 -07001878 ContentValues values = new ContentValues();
1879 values.put(Snapshots.TITLE, mCurrentState.mTitle);
1880 values.put(Snapshots.URL, mCurrentState.mUrl);
John Reck68234a92012-04-19 15:27:12 -07001881 values.put(Snapshots.BACKGROUND, web.getPageBackgroundColor());
John Reck8cc92352011-07-06 17:41:52 -07001882 values.put(Snapshots.DATE_CREATED, System.currentTimeMillis());
1883 values.put(Snapshots.FAVICON, compressBitmap(getFavicon()));
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001884 values.put(Snapshots.THUMBNAIL, compressBitmap(bm));
John Reckd8c74522011-06-14 08:45:00 -07001885 return values;
John Reck541f55a2011-06-07 16:34:43 -07001886 }
1887
John Reck68234a92012-04-19 15:27:12 -07001888 /**
1889 * Probably want to call this on a background thread
1890 */
1891 public boolean saveViewState(ContentValues values) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001892 WebView web = getWebView();
John Reck68234a92012-04-19 15:27:12 -07001893 if (web == null) return false;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001894 String filename = UUID.randomUUID().toString();
John Reck68234a92012-04-19 15:27:12 -07001895 SaveCallback callback = new SaveCallback();
John Reck68234a92012-04-19 15:27:12 -07001896 try {
John Reck68234a92012-04-19 15:27:12 -07001897 synchronized (callback) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001898 web.saveViewState(filename, callback);
1899 callback.wait();
John Reck68234a92012-04-19 15:27:12 -07001900 }
John Reck68234a92012-04-19 15:27:12 -07001901 } catch (Exception e) {
1902 Log.w(LOGTAG, "Failed to save view state", e);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001903 String path = callback.getPath();
1904 if (path != null) {
1905 File file = mContext.getFileStreamPath(path);
1906 if (file.exists() && !file.delete()) {
1907 file.deleteOnExit();
1908 }
John Reck68234a92012-04-19 15:27:12 -07001909 }
1910 return false;
1911 }
Tarun Nainani8eb00912014-07-17 12:28:32 -07001912
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001913 String path = callback.getPath();
Tarun Nainani8eb00912014-07-17 12:28:32 -07001914 // could be that saving of file failed
1915 if (path == null) {
1916 return false;
1917 }
1918
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001919 File savedFile = new File(path);
1920 if (!savedFile.exists()) {
1921 return false;
John Reck68234a92012-04-19 15:27:12 -07001922 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001923 values.put(Snapshots.VIEWSTATE_PATH, path.substring(path.lastIndexOf('/') + 1));
1924 values.put(Snapshots.VIEWSTATE_SIZE, savedFile.length());
John Reck68234a92012-04-19 15:27:12 -07001925 return true;
1926 }
1927
John Reck8cc92352011-07-06 17:41:52 -07001928 public byte[] compressBitmap(Bitmap bitmap) {
1929 if (bitmap == null) {
1930 return null;
1931 }
1932 ByteArrayOutputStream stream = new ByteArrayOutputStream();
1933 bitmap.compress(CompressFormat.PNG, 100, stream);
1934 return stream.toByteArray();
1935 }
1936
John Reck26b18322011-06-21 13:08:58 -07001937 public void loadUrl(String url, Map<String, String> headers) {
1938 if (mMainView != null) {
Michael Kolba53c9892011-10-05 13:31:40 -07001939 mPageLoadProgress = INITIAL_PROGRESS;
Axesh R. Ajmerad04cad12015-08-12 12:02:44 -07001940 mCurrentState = new PageState(
Sagar Dhawanca9ecfb2015-08-10 17:27:58 -07001941 mContext, mMainView.isPrivateBrowsingEnabled(), url);
John Reck26b18322011-06-21 13:08:58 -07001942 mMainView.loadUrl(url, headers);
1943 }
1944 }
1945
John Reck38b39652012-06-05 09:22:59 -07001946 public void disableUrlOverridingForLoad() {
1947 mDisableOverrideUrlLoading = true;
1948 }
1949
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001950 private void thumbnailUpdated() {
John Reck8ee633f2011-08-09 16:00:35 -07001951 mHandler.removeMessages(MSG_CAPTURE);
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001952
John Reck8ee633f2011-08-09 16:00:35 -07001953 TabControl tc = mWebViewController.getTabControl();
1954 if (tc != null) {
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001955 OnThumbnailUpdatedListener updateListener = tc.getOnThumbnailUpdatedListener();
John Reck8ee633f2011-08-09 16:00:35 -07001956 if (updateListener != null) {
1957 updateListener.onThumbnailUpdated(this);
1958 }
1959 }
Pankaj Gargf49e0222015-09-01 12:19:13 -07001960
1961 if (mViewportCapture != null) {
1962 mWebViewController.onThumbnailCapture(mViewportCapture);
1963 mViewportCapture.recycle();
1964 mViewportCapture = null;
1965 } else {
1966 mWebViewController.onThumbnailCapture(mCapture);
1967 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001968 }
1969
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001970 protected void capture() {
1971 if (mMainView == null || mCapture == null || !mMainView.isReady() ||
1972 mMainView.getContentWidth() <= 0 || mMainView.getContentHeight() <= 0 ||
1973 !mFirstVisualPixelPainted || mMainView.isShowingCrashView()) {
Pankaj Gargf49e0222015-09-01 12:19:13 -07001974 mViewportCapture = null;
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001975 initCaptureBitmap();
1976 thumbnailUpdated();
1977 return;
1978 }
1979
1980 mMainView.getContentBitmapAsync((float) mCaptureWidth / mMainView.getWidth(), new Rect(),
1981 new ValueCallback<Bitmap>() {
1982 @Override
1983 public void onReceiveValue(Bitmap bitmap) {
Pankaj Gargf49e0222015-09-01 12:19:13 -07001984 mViewportCapture = bitmap;
1985
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001986 if (mCapture == null) {
1987 initCaptureBitmap();
1988 }
1989
1990 if (bitmap == null) {
1991 thumbnailUpdated();
1992 return;
1993 }
1994
1995 Canvas c = new Canvas(mCapture);
1996 mCapture.eraseColor(Color.WHITE);
1997 c.drawBitmap(bitmap, 0, 0, null);
1998
1999 // manually anti-alias the edges for the tilt
2000 c.drawRect(0, 0, 1, mCapture.getHeight(), sAlphaPaint);
2001 c.drawRect(mCapture.getWidth() - 1, 0, mCapture.getWidth(),
2002 mCapture.getHeight(), sAlphaPaint);
2003 c.drawRect(0, 0, mCapture.getWidth(), 1, sAlphaPaint);
2004 c.drawRect(0, mCapture.getHeight() - 1, mCapture.getWidth(),
2005 mCapture.getHeight(), sAlphaPaint);
2006 c.setBitmap(null);
2007
2008 persistThumbnail();
2009 thumbnailUpdated();
2010 }
2011 }
2012 );
John Reck1cf4b792011-07-26 10:22:22 -07002013 }
2014
Pankaj Garg6bedeba2015-06-23 15:47:37 -07002015 @Override
2016 public void onNewPicture(WebView view, Picture picture) {
Michael Kolb9ef259a2011-07-12 15:33:08 -07002017 }
2018
John Reckef654f12011-07-12 16:42:08 -07002019 public boolean canGoBack() {
2020 return mMainView != null ? mMainView.canGoBack() : false;
2021 }
2022
2023 public boolean canGoForward() {
2024 return mMainView != null ? mMainView.canGoForward() : false;
2025 }
2026
2027 public void goBack() {
2028 if (mMainView != null) {
2029 mMainView.goBack();
2030 }
2031 }
2032
2033 public void goForward() {
2034 if (mMainView != null) {
2035 mMainView.goForward();
2036 }
2037 }
2038
John Reck1cf4b792011-07-26 10:22:22 -07002039 protected void persistThumbnail() {
John Reck4eadc342011-10-31 14:04:10 -07002040 DataController.getInstance(mContext).saveThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07002041 }
2042
2043 protected void deleteThumbnail() {
John Reck4eadc342011-10-31 14:04:10 -07002044 DataController.getInstance(mContext).deleteThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07002045 }
2046
John Reck4eadc342011-10-31 14:04:10 -07002047 void updateCaptureFromBlob(byte[] blob) {
John Reck1cf4b792011-07-26 10:22:22 -07002048 synchronized (Tab.this) {
2049 if (mCapture == null) {
2050 return;
2051 }
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002052 ByteBuffer buffer = ByteBuffer.wrap(blob);
Michael Kolbd837a112011-08-09 14:00:18 -07002053 try {
2054 mCapture.copyPixelsFromBuffer(buffer);
2055 } catch (RuntimeException rex) {
2056 Log.e(LOGTAG, "Load capture has mismatched sizes; buffer: "
2057 + buffer.capacity() + " blob: " + blob.length
2058 + "capture: " + mCapture.getByteCount());
2059 throw rex;
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002060 }
John Reck1cf4b792011-07-26 10:22:22 -07002061 }
2062 }
2063
John Reck52be4782011-08-26 15:37:29 -07002064 @Override
2065 public String toString() {
2066 StringBuilder builder = new StringBuilder(100);
2067 builder.append(mId);
2068 builder.append(") has parent: ");
2069 if (getParent() != null) {
2070 builder.append("true[");
2071 builder.append(getParent().getId());
2072 builder.append("]");
2073 } else {
2074 builder.append("false");
2075 }
2076 builder.append(", incog: ");
2077 builder.append(isPrivateBrowsingEnabled());
2078 if (!isPrivateBrowsingEnabled()) {
2079 builder.append(", title: ");
2080 builder.append(getTitle());
2081 builder.append(", url: ");
2082 builder.append(getUrl());
2083 }
2084 return builder.toString();
2085 }
2086
Ze G Riande2a675c22015-06-03 11:15:24 -07002087 // dertermines if the tab contains a dislled page
2088 public boolean isDistilled() {
2089 if (!BrowserCommandLine.hasSwitch("reader-mode")) {
2090 return false;
2091 }
2092 try {
2093 return DomDistillerUtils.isUrlDistilled(getUrl());
2094 } catch (Exception e) {
2095 return false;
2096 }
2097 }
2098
2099 //determines if the tab contains a distillable page
2100 public boolean isDistillable() {
2101 if (!BrowserCommandLine.hasSwitch("reader-mode")) {
2102 mIsDistillable = false;
2103 return mIsDistillable;
2104 }
2105 final ValueCallback<String> onIsDistillable = new ValueCallback<String>() {
2106 @Override
2107 public void onReceiveValue(String str) {
2108 mIsDistillable = Boolean.parseBoolean(str);
2109 }
2110 };
2111
2112 if (isDistilled()) {
2113 mIsDistillable = true;
2114 return mIsDistillable;
2115 }
2116
2117 try {
2118 DomDistillerUtils.isWebViewDistillable(getWebView(), onIsDistillable);
2119 } catch (Exception e) {
2120 mIsDistillable = false;
2121 }
2122
2123 return mIsDistillable;
2124 }
2125
2126 // Function that sets the mIsDistillable variable
2127 public void setIsDistillable(boolean value) {
2128 if (!BrowserCommandLine.hasSwitch("reader-mode")) {
2129 mIsDistillable = false;
2130 }
2131 mIsDistillable = value;
2132 }
2133
2134 // Function that returns the distilled url of the current url
2135 public String getDistilledUrl() {
2136 if (getUrl() != null) {
2137 return DomDistillerUtils.getDistilledUrl(getUrl());
2138 }
2139 return new String();
2140 }
2141
2142 // function that returns the non-distilled version of the current url
2143 public String getNonDistilledUrl() {
2144 if (getUrl() != null) {
2145 return DomDistillerUtils.getOriginalUrlFromDistilledUrl(getUrl());
2146 }
2147 return new String();
2148 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002149}