blob: ef4c3806b8859fccb501068f1047da711aa7a111 [file] [log] [blame]
Grace Kloba22ac16e2009-10-07 18:00:23 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Bijan Amirzada41242f22014-03-21 12:12:18 -070017package com.android.browser;
Grace Kloba22ac16e2009-10-07 18:00:23 -070018
Michael Kolb8233fac2010-10-26 16:08:53 -070019import android.app.Activity;
Grace Kloba22ac16e2009-10-07 18:00:23 -070020import android.app.AlertDialog;
21import android.content.ContentResolver;
John Reckd8c74522011-06-14 08:45:00 -070022import android.content.ContentValues;
John Reck30c714c2010-12-16 17:30:34 -080023import android.content.Context;
Grace Kloba22ac16e2009-10-07 18:00:23 -070024import android.content.DialogInterface;
Michael Kolbfe251992010-07-08 15:41:55 -070025import android.content.DialogInterface.OnCancelListener;
Pankaj Garg1c13cab2015-05-12 11:52:17 -070026import android.content.res.Configuration;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080027import android.content.res.Resources;
Grace Kloba22ac16e2009-10-07 18:00:23 -070028import android.graphics.Bitmap;
John Reck8cc92352011-07-06 17:41:52 -070029import android.graphics.Bitmap.CompressFormat;
Michael Kolb9ef259a2011-07-12 15:33:08 -070030import android.graphics.BitmapFactory;
31import android.graphics.Canvas;
Michael Kolbc3af0672011-08-09 10:24:41 -070032import android.graphics.Color;
Michael Kolba3194d02011-09-07 11:23:51 -070033import android.graphics.Paint;
Michael Kolb9ef259a2011-07-12 15:33:08 -070034import android.graphics.Picture;
Michael Kolba3194d02011-09-07 11:23:51 -070035import android.graphics.PorterDuff;
36import android.graphics.PorterDuffXfermode;
Tarun Nainaniea28dde2014-08-27 17:25:09 -070037import android.graphics.Rect;
Grace Kloba22ac16e2009-10-07 18:00:23 -070038import android.net.Uri;
39import android.net.http.SslError;
Grace Kloba22ac16e2009-10-07 18:00:23 -070040import android.os.Bundle;
Michael Kolb9ef259a2011-07-12 15:33:08 -070041import android.os.Handler;
Grace Kloba22ac16e2009-10-07 18:00:23 -070042import android.os.Message;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +000043import android.os.SystemClock;
Brian Carlstrom8862c1d2011-06-02 01:05:55 -070044import android.security.KeyChain;
Brian Carlstromaa09cd82011-06-09 16:04:40 -070045import android.security.KeyChainAliasCallback;
John Reck24f18262011-06-17 14:47:20 -070046import android.text.TextUtils;
Grace Kloba22ac16e2009-10-07 18:00:23 -070047import android.util.Log;
48import android.view.KeyEvent;
49import android.view.LayoutInflater;
50import android.view.View;
Grace Kloba50c241e2010-04-20 11:07:50 -070051import android.view.ViewStub;
Ben Murdochc42addf2010-01-28 15:19:59 +000052import android.webkit.ConsoleMessage;
Grace Kloba22ac16e2009-10-07 18:00:23 -070053import android.webkit.URLUtil;
John Reck438bf462011-01-12 18:11:46 -080054import android.webkit.WebResourceResponse;
Grace Kloba22ac16e2009-10-07 18:00:23 -070055import android.webkit.WebStorage;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080056import android.webkit.WebChromeClient.CustomViewCallback;
57import android.webkit.ValueCallback;
Ben Murdoch1d676b62011-01-17 12:54:24 +000058import android.widget.CheckBox;
Ben Murdoch8029a772010-11-16 11:58:21 +000059import android.widget.Toast;
Grace Kloba22ac16e2009-10-07 18:00:23 -070060
Bijan Amirzada41242f22014-03-21 12:12:18 -070061import com.android.browser.TabControl.OnThumbnailUpdatedListener;
62import com.android.browser.homepages.HomeProvider;
63import com.android.browser.mynavigation.MyNavigationUtil;
64import com.android.browser.provider.MyNavigationProvider;
65import com.android.browser.provider.SnapshotProvider.Snapshots;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080066
Pankaj Garg18aa0a12015-06-22 11:06:12 -070067import org.codeaurora.swe.BrowserCommandLine;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080068import org.codeaurora.swe.BrowserDownloadListener;
69import org.codeaurora.swe.ClientCertRequestHandler;
70import org.codeaurora.swe.HttpAuthHandler;
71import org.codeaurora.swe.SslErrorHandler;
72import org.codeaurora.swe.WebBackForwardList;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080073import org.codeaurora.swe.WebChromeClient;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080074import org.codeaurora.swe.WebView;
75import org.codeaurora.swe.WebView.PictureListener;
Pankaj Garg1c7380d2014-08-27 14:17:12 -070076import org.codeaurora.swe.WebView.CreateWindowParams;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080077import org.codeaurora.swe.WebViewClient;
Pankaj Garg1c13cab2015-05-12 11:52:17 -070078import org.codeaurora.swe.util.Observable;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080079
John Reck541f55a2011-06-07 16:34:43 -070080import java.io.ByteArrayOutputStream;
John Reck2b71d6d2012-04-18 17:42:06 -070081import java.io.File;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080082import java.io.InputStream;
John Reck1cf4b792011-07-26 10:22:22 -070083import java.nio.ByteBuffer;
Michael Kolbfe251992010-07-08 15:41:55 -070084import java.util.Map;
John Reck2b71d6d2012-04-18 17:42:06 -070085import java.util.UUID;
Michael Kolbfe251992010-07-08 15:41:55 -070086import java.util.Vector;
Vivek Sekhar53ef8932015-06-18 16:51:43 -070087import java.util.List;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080088import java.sql.Timestamp;
89import java.util.Date;
Michael Kolbfe251992010-07-08 15:41:55 -070090
Grace Kloba22ac16e2009-10-07 18:00:23 -070091/**
92 * Class for maintaining Tabs with a main WebView and a subwindow.
93 */
Michael Kolb9ef259a2011-07-12 15:33:08 -070094class Tab implements PictureListener {
Michael Kolb8233fac2010-10-26 16:08:53 -070095
Grace Kloba22ac16e2009-10-07 18:00:23 -070096 // Log Tag
97 private static final String LOGTAG = "Tab";
Bijan Amirzada41242f22014-03-21 12:12:18 -070098 private static final boolean LOGD_ENABLED = com.android.browser.Browser.LOGD_ENABLED;
Ben Murdochc42addf2010-01-28 15:19:59 +000099 // Special case the logtag for messages for the Console to make it easier to
100 // filter them and match the logtag used for these messages in older versions
101 // of the browser.
102 private static final String CONSOLE_LOGTAG = "browser";
103
Michael Kolb9ef259a2011-07-12 15:33:08 -0700104 private static final int MSG_CAPTURE = 42;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800105 private static final int CAPTURE_DELAY = 1000;
Michael Kolba53c9892011-10-05 13:31:40 -0700106 private static final int INITIAL_PROGRESS = 5;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700107
John Reck1cf4b792011-07-26 10:22:22 -0700108 private static Bitmap sDefaultFavicon;
109
Michael Kolba3194d02011-09-07 11:23:51 -0700110 private static Paint sAlphaPaint = new Paint();
111 static {
112 sAlphaPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
113 sAlphaPaint.setColor(Color.TRANSPARENT);
114 }
115
Steve Block2466eff2011-10-03 15:33:09 +0100116 public enum SecurityState {
Steve Block4895b012011-10-03 16:26:46 +0100117 // The page's main resource does not use SSL. Note that we use this
118 // state irrespective of the SSL authentication state of sub-resources.
Steve Block2466eff2011-10-03 15:33:09 +0100119 SECURITY_STATE_NOT_SECURE,
Steve Block4895b012011-10-03 16:26:46 +0100120 // The page's main resource uses SSL and the certificate is good. The
121 // same is true of all sub-resources.
Steve Block2466eff2011-10-03 15:33:09 +0100122 SECURITY_STATE_SECURE,
Steve Block4895b012011-10-03 16:26:46 +0100123 // The page's main resource uses SSL and the certificate is good, but
124 // some sub-resources either do not use SSL or have problems with their
125 // certificates.
Steve Block2466eff2011-10-03 15:33:09 +0100126 SECURITY_STATE_MIXED,
Steve Block4895b012011-10-03 16:26:46 +0100127 // The page's main resource uses SSL but there is a problem with its
128 // certificate.
129 SECURITY_STATE_BAD_CERTIFICATE,
John Reck30c714c2010-12-16 17:30:34 -0800130 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700131
Michael Kolb14612442011-06-24 13:06:29 -0700132 Context mContext;
John Reckd8c74522011-06-14 08:45:00 -0700133 protected WebViewController mWebViewController;
Michael Kolb8233fac2010-10-26 16:08:53 -0700134
Michael Kolbc831b632011-05-11 09:30:34 -0700135 // The tab ID
John Reckd8c74522011-06-14 08:45:00 -0700136 private long mId = -1;
Michael Kolbc831b632011-05-11 09:30:34 -0700137
Grace Kloba22ac16e2009-10-07 18:00:23 -0700138 // Main WebView wrapper
Michael Kolba713ec82010-11-29 17:27:06 -0800139 private View mContainer;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700140 // Main WebView
141 private WebView mMainView;
142 // Subwindow container
143 private View mSubViewContainer;
144 // Subwindow WebView
145 private WebView mSubView;
146 // Saved bundle for when we are running low on memory. It contains the
147 // information needed to restore the WebView if the user goes back to the
148 // tab.
149 private Bundle mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700150 // Parent Tab. This is the Tab that created this Tab, or null if the Tab was
151 // created by the UI
Michael Kolbc831b632011-05-11 09:30:34 -0700152 private Tab mParent;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700153 // Tab that constructed by this Tab. This is used when this Tab is
154 // destroyed, it clears all mParentTab values in the children.
Michael Kolbc831b632011-05-11 09:30:34 -0700155 private Vector<Tab> mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700156 // If true, the tab is in the foreground of the current activity.
157 private boolean mInForeground;
Michael Kolb8233fac2010-10-26 16:08:53 -0700158 // If true, the tab is in page loading state (after onPageStarted,
159 // before onPageFinsihed)
160 private boolean mInPageLoad;
Tarun Nainani8eb00912014-07-17 12:28:32 -0700161 private boolean mPageFinished;
John Reck38b39652012-06-05 09:22:59 -0700162 private boolean mDisableOverrideUrlLoading;
Pankaj Garg79878492015-04-01 14:48:21 -0700163 private boolean mFirstVisualPixelPainted = false;
John Reck30c714c2010-12-16 17:30:34 -0800164 // The last reported progress of the current page
165 private int mPageLoadProgress;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000166 // The time the load started, used to find load page time
167 private long mLoadStartTime;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700168 // Application identifier used to find tabs that another application wants
169 // to reuse.
170 private String mAppId;
Michael Kolbe28b3472011-08-04 16:54:31 -0700171 // flag to indicate if tab should be closed on back
172 private boolean mCloseOnBack;
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700173 // flag to indicate if the tab was opened from an intent
174 private boolean mDerivedFromIntent = false;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -0500175 // The listener that gets invoked when a download is started from the
176 // mMainView
Selim Gurun0b3d66f2012-08-29 13:08:13 -0700177 private final BrowserDownloadListener mDownloadListener;
John Recke969cc52010-12-21 17:24:43 -0800178 private DataController mDataController;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700179
180 // AsyncTask for downloading touch icons
181 DownloadTouchIcon mTouchIconLoader;
182
John Reck35e9dd62011-04-25 09:01:54 -0700183 private BrowserSettings mSettings;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700184 private int mCaptureWidth;
185 private int mCaptureHeight;
186 private Bitmap mCapture;
187 private Handler mHandler;
Michael Kolb72864272012-05-03 15:42:15 -0700188 private boolean mUpdateThumbnail;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800189 private Timestamp timestamp;
Vivek Sekharf96064b2014-07-28 16:32:34 -0700190 private boolean mFullScreen = false;
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800191 private boolean mReceivedError;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700192
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -0700193 // determine if webview is destroyed to MemoryMonitor
194 private boolean mWebViewDestroyedByMemoryMonitor;
195
Pankaj Garg21dad562015-07-02 17:17:24 -0700196 private String mTouchIconUrl;
197
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700198 private Observable mFirstPixelObservable;
199 private Observable mTabHistoryUpdateObservable;
200
201 Observable getFirstPixelObservable() {
202 return mFirstPixelObservable;
203 }
204
205 Observable getTabHistoryUpdateObservable() {
206 return mTabHistoryUpdateObservable;
207 }
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -0700208
Mathew Inwood1dd8e822011-08-03 14:34:29 +0100209
John Reck1cf4b792011-07-26 10:22:22 -0700210 private static synchronized Bitmap getDefaultFavicon(Context context) {
211 if (sDefaultFavicon == null) {
212 sDefaultFavicon = BitmapFactory.decodeResource(
Enrico Rosd6efa972014-12-02 19:49:59 -0800213 context.getResources(), R.drawable.ic_deco_favicon_normal);
John Reck1cf4b792011-07-26 10:22:22 -0700214 }
215 return sDefaultFavicon;
216 }
Michael Kolbeb95db42011-03-03 10:38:40 -0800217
John Reck30c714c2010-12-16 17:30:34 -0800218 // All the state needed for a page
John Reckd8c74522011-06-14 08:45:00 -0700219 protected static class PageState {
John Reck30c714c2010-12-16 17:30:34 -0800220 String mUrl;
John Reckdb22ec42011-06-29 11:31:24 -0700221 String mOriginalUrl;
John Reck30c714c2010-12-16 17:30:34 -0800222 String mTitle;
Steve Block2466eff2011-10-03 15:33:09 +0100223 SecurityState mSecurityState;
Steve Block08a6f0c2011-10-06 12:12:53 +0100224 // This is non-null only when mSecurityState is SECURITY_STATE_BAD_CERTIFICATE.
225 SslError mSslCertificateError;
John Reck30c714c2010-12-16 17:30:34 -0800226 Bitmap mFavicon;
Steve Block08a6f0c2011-10-06 12:12:53 +0100227 boolean mIsBookmarkedSite;
228 boolean mIncognito;
John Reck30c714c2010-12-16 17:30:34 -0800229
230 PageState(Context c, boolean incognito) {
John Reck502a3532011-08-16 14:21:46 -0700231 mIncognito = incognito;
232 if (mIncognito) {
Vivek Sekhared791da2015-02-22 12:39:05 -0800233 mOriginalUrl = mUrl = "chrome://incognito";
John Reck30c714c2010-12-16 17:30:34 -0800234 mTitle = c.getString(R.string.new_incognito_tab);
John Reck30c714c2010-12-16 17:30:34 -0800235 } else {
John Reckdb22ec42011-06-29 11:31:24 -0700236 mOriginalUrl = mUrl = "";
John Reck30c714c2010-12-16 17:30:34 -0800237 mTitle = c.getString(R.string.new_tab);
John Reck30c714c2010-12-16 17:30:34 -0800238 }
Steve Block2466eff2011-10-03 15:33:09 +0100239 mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
John Reck30c714c2010-12-16 17:30:34 -0800240 }
241
242 PageState(Context c, boolean incognito, String url, Bitmap favicon) {
John Reck502a3532011-08-16 14:21:46 -0700243 mIncognito = incognito;
John Reckdb22ec42011-06-29 11:31:24 -0700244 mOriginalUrl = mUrl = url;
John Reck30c714c2010-12-16 17:30:34 -0800245 if (URLUtil.isHttpsUrl(url)) {
Steve Block2466eff2011-10-03 15:33:09 +0100246 mSecurityState = SecurityState.SECURITY_STATE_SECURE;
John Reck30c714c2010-12-16 17:30:34 -0800247 } else {
Steve Block2466eff2011-10-03 15:33:09 +0100248 mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
John Reck30c714c2010-12-16 17:30:34 -0800249 }
John Reck1cf4b792011-07-26 10:22:22 -0700250 mFavicon = favicon;
John Reck30c714c2010-12-16 17:30:34 -0800251 }
John Reck1cf4b792011-07-26 10:22:22 -0700252
Grace Kloba22ac16e2009-10-07 18:00:23 -0700253 }
254
John Reck30c714c2010-12-16 17:30:34 -0800255 // The current/loading page's state
John Reckd8c74522011-06-14 08:45:00 -0700256 protected PageState mCurrentState;
John Reck30c714c2010-12-16 17:30:34 -0800257
Grace Kloba22ac16e2009-10-07 18:00:23 -0700258 // Used for saving and restoring each Tab
Michael Kolbc831b632011-05-11 09:30:34 -0700259 static final String ID = "ID";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700260 static final String CURRURL = "currentUrl";
261 static final String CURRTITLE = "currentTitle";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700262 static final String PARENTTAB = "parentTab";
263 static final String APPID = "appid";
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700264 static final String INCOGNITO = "privateBrowsingEnabled";
John Reckb0a86db2011-05-24 14:05:58 -0700265 static final String USERAGENT = "useragent";
Michael Kolbe28b3472011-08-04 16:54:31 -0700266 static final String CLOSEFLAG = "closeOnBack";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700267
Pankaj Garg18186a92015-03-31 14:59:33 -0700268 public void setNetworkAvailable(boolean networkUp) {
269 if (networkUp && mReceivedError && (mMainView != null)) {
270 mMainView.reload();
271 }
272 }
273
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700274 public boolean isFirstVisualPixelPainted() {
275 return mFirstVisualPixelPainted;
276 }
277
278 public int getCaptureIndex(int navIndex) {
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700279 int orientation = mWebViewController.getActivity().
280 getResources().getConfiguration().orientation;
281
282 int orientationBit = (orientation == Configuration.ORIENTATION_LANDSCAPE) ? 0 : 1;
283
Vivek Sekhard0f60402015-06-05 14:07:11 -0700284 int index = orientationBit << 31 | (((int)mId & 0x7f) << 24) | (navIndex & 0xffffff);
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700285 return index;
286 }
287
288 public int getTabIdxFromCaptureIdx(int index) {
289 return (index & 0x7f000000) >> 24;
290 }
291
292 public int getOrientationFromCaptureIdx(int index) {
293 return ((index & 0x80000000) == 0) ? Configuration.ORIENTATION_LANDSCAPE :
294 Configuration.ORIENTATION_PORTRAIT;
295
296 }
297
298 public int getNavIdxFromCaptureIdx(int index) {
299 return (index & 0xffffff);
300 }
301
Grace Kloba22ac16e2009-10-07 18:00:23 -0700302 // -------------------------------------------------------------------------
303 // WebViewClient implementation for the main WebView
304 // -------------------------------------------------------------------------
305
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800306 private final WebViewClient mWebViewClient = new WebViewClient() {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500307 private Message mDontResend;
308 private Message mResend;
Michael Kolb47bd1e42011-09-01 15:25:00 -0700309
310 private boolean providersDiffer(String url, String otherUrl) {
311 Uri uri1 = Uri.parse(url);
312 Uri uri2 = Uri.parse(otherUrl);
313 return !uri1.getEncodedAuthority().equals(uri2.getEncodedAuthority());
314 }
315
Grace Kloba22ac16e2009-10-07 18:00:23 -0700316 @Override
317 public void onPageStarted(WebView view, String url, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700318 mInPageLoad = true;
Tarun Nainani8eb00912014-07-17 12:28:32 -0700319 mPageFinished = false;
Pankaj Garg79878492015-04-01 14:48:21 -0700320 mFirstVisualPixelPainted = false;
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700321 mFirstPixelObservable.set(false);
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800322 mReceivedError = false;
Michael Kolb72864272012-05-03 15:42:15 -0700323 mUpdateThumbnail = true;
Michael Kolba53c9892011-10-05 13:31:40 -0700324 mPageLoadProgress = INITIAL_PROGRESS;
Michael Kolb14612442011-06-24 13:06:29 -0700325 mCurrentState = new PageState(mContext,
John Reck30c714c2010-12-16 17:30:34 -0800326 view.isPrivateBrowsingEnabled(), url, favicon);
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000327 mLoadStartTime = SystemClock.uptimeMillis();
Pankaj Garg62bc7912015-04-14 16:08:59 -0700328 // Need re-enable FullScreenMode on Page navigation if needed
329 if (BrowserSettings.getInstance().useFullscreen()){
330 Controller controller = (Controller) mWebViewController;
331 BaseUi ui = (BaseUi) controller.getUi();
332 ui.forceDisableFullscreenMode(false);
333 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700334 // If we start a touch icon load and then load a new page, we don't
335 // want to cancel the current touch icon loader. But, we do want to
336 // create a new one when the touch icon url is known.
337 if (mTouchIconLoader != null) {
338 mTouchIconLoader.mTab = null;
339 mTouchIconLoader = null;
340 }
341
Grace Kloba22ac16e2009-10-07 18:00:23 -0700342 // finally update the UI in the activity if it is in the foreground
John Reck324d4402011-01-11 16:56:42 -0800343 mWebViewController.onPageStarted(Tab.this, view, favicon);
Leon Scroggins4cd97792010-12-03 15:31:56 -0500344
John Recke969cc52010-12-21 17:24:43 -0800345 updateBookmarkedStatus();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700346 }
347
348 @Override
349 public void onPageFinished(WebView view, String url) {
John Reck38b39652012-06-05 09:22:59 -0700350 mDisableOverrideUrlLoading = false;
John Reck5b691842010-11-29 11:21:13 -0800351 if (!isPrivateBrowsingEnabled()) {
352 LogTag.logPageFinishedLoading(
353 url, SystemClock.uptimeMillis() - mLoadStartTime);
354 }
John Reck1cf4b792011-07-26 10:22:22 -0700355 syncCurrentState(view, url);
John Reck324d4402011-01-11 16:56:42 -0800356 mWebViewController.onPageFinished(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700357 }
358
Pankaj Garg79878492015-04-01 14:48:21 -0700359 @Override
360 public void onFirstVisualPixel(WebView view) {
361 mFirstVisualPixelPainted = true;
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700362 mFirstPixelObservable.set(true);
Pankaj Garg79878492015-04-01 14:48:21 -0700363 }
364
Grace Kloba22ac16e2009-10-07 18:00:23 -0700365 // return true if want to hijack the url to let another app to handle it
366 @Override
367 public boolean shouldOverrideUrlLoading(WebView view, String url) {
John Reck38b39652012-06-05 09:22:59 -0700368 if (!mDisableOverrideUrlLoading && mInForeground) {
Michael Kolb18eb3772010-12-10 14:29:51 -0800369 return mWebViewController.shouldOverrideUrlLoading(Tab.this,
370 view, url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700371 } else {
372 return false;
373 }
374 }
375
Vivek Sekharb991edb2014-12-17 18:18:07 -0800376 @Override
377 public boolean shouldDownloadFavicon(WebView view, String url) {
378 return true;
379 }
380
Grace Kloba22ac16e2009-10-07 18:00:23 -0700381 /**
Steve Block2466eff2011-10-03 15:33:09 +0100382 * Updates the security state. This method is called when we discover
383 * another resource to be loaded for this page (for example,
384 * javascript). While we update the security state, we do not update
385 * the lock icon until we are done loading, as it is slightly more
386 * secure this way.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700387 */
388 @Override
389 public void onLoadResource(WebView view, String url) {
390 if (url != null && url.length() > 0) {
391 // It is only if the page claims to be secure that we may have
Steve Block2466eff2011-10-03 15:33:09 +0100392 // to update the security state:
393 if (mCurrentState.mSecurityState == SecurityState.SECURITY_STATE_SECURE) {
394 // If NOT a 'safe' url, change the state to mixed content!
Grace Kloba22ac16e2009-10-07 18:00:23 -0700395 if (!(URLUtil.isHttpsUrl(url) || URLUtil.isDataUrl(url)
396 || URLUtil.isAboutUrl(url))) {
Steve Block2466eff2011-10-03 15:33:09 +0100397 mCurrentState.mSecurityState = SecurityState.SECURITY_STATE_MIXED;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700398 }
399 }
400 }
401 }
402
403 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700404 * Show a dialog informing the user of the network error reported by
405 * WebCore if it is in the foreground.
406 */
407 @Override
408 public void onReceivedError(WebView view, int errorCode,
409 String description, String failingUrl) {
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800410 // Used for the syncCurrentState to use
411 // the failing url instead of using webview url
412 mReceivedError = true;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700413 }
414
415 /**
416 * Check with the user if it is ok to resend POST data as the page they
417 * are trying to navigate to is the result of a POST.
418 */
419 @Override
420 public void onFormResubmission(WebView view, final Message dontResend,
421 final Message resend) {
422 if (!mInForeground) {
423 dontResend.sendToTarget();
424 return;
425 }
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500426 if (mDontResend != null) {
427 Log.w(LOGTAG, "onFormResubmission should not be called again "
428 + "while dialog is still up");
429 dontResend.sendToTarget();
430 return;
431 }
432 mDontResend = dontResend;
433 mResend = resend;
Michael Kolb14612442011-06-24 13:06:29 -0700434 new AlertDialog.Builder(mContext).setTitle(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700435 R.string.browserFrameFormResubmitLabel).setMessage(
436 R.string.browserFrameFormResubmitMessage)
437 .setPositiveButton(R.string.ok,
438 new DialogInterface.OnClickListener() {
439 public void onClick(DialogInterface dialog,
440 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500441 if (mResend != null) {
442 mResend.sendToTarget();
443 mResend = null;
444 mDontResend = null;
445 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700446 }
447 }).setNegativeButton(R.string.cancel,
448 new DialogInterface.OnClickListener() {
449 public void onClick(DialogInterface dialog,
450 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500451 if (mDontResend != null) {
452 mDontResend.sendToTarget();
453 mResend = null;
454 mDontResend = null;
455 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700456 }
457 }).setOnCancelListener(new OnCancelListener() {
458 public void onCancel(DialogInterface dialog) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500459 if (mDontResend != null) {
460 mDontResend.sendToTarget();
461 mResend = null;
462 mDontResend = null;
463 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700464 }
465 }).show();
466 }
467
468 /**
469 * Insert the url into the visited history database.
470 * @param url The url to be inserted.
471 * @param isReload True if this url is being reloaded.
472 * FIXME: Not sure what to do when reloading the page.
473 */
474 @Override
475 public void doUpdateVisitedHistory(WebView view, String url,
476 boolean isReload) {
John Reck324d4402011-01-11 16:56:42 -0800477 mWebViewController.doUpdateVisitedHistory(Tab.this, isReload);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700478 }
479
480 /**
481 * Displays SSL error(s) dialog to the user.
482 */
483 @Override
484 public void onReceivedSslError(final WebView view,
485 final SslErrorHandler handler, final SslError error) {
486 if (!mInForeground) {
487 handler.cancel();
Steve Block2466eff2011-10-03 15:33:09 +0100488 setSecurityState(SecurityState.SECURITY_STATE_NOT_SECURE);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700489 return;
490 }
John Reck35e9dd62011-04-25 09:01:54 -0700491 if (mSettings.showSecurityWarnings()) {
John Reckcb28b2c2011-08-26 17:39:44 -0700492 new AlertDialog.Builder(mContext)
493 .setTitle(R.string.security_warning)
494 .setMessage(R.string.ssl_warnings_header)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200495 .setIconAttribute(android.R.attr.alertDialogIcon)
John Reckcb28b2c2011-08-26 17:39:44 -0700496 .setPositiveButton(R.string.ssl_continue,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700497 new DialogInterface.OnClickListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700498 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700499 public void onClick(DialogInterface dialog,
500 int whichButton) {
501 handler.proceed();
Steve Block4895b012011-10-03 16:26:46 +0100502 handleProceededAfterSslError(error);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700503 }
John Reckcb28b2c2011-08-26 17:39:44 -0700504 })
505 .setNeutralButton(R.string.view_certificate,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700506 new DialogInterface.OnClickListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700507 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700508 public void onClick(DialogInterface dialog,
509 int whichButton) {
John Reckcb28b2c2011-08-26 17:39:44 -0700510 mWebViewController.showSslCertificateOnError(
511 view, handler, error);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700512 }
John Reckcb28b2c2011-08-26 17:39:44 -0700513 })
514 .setNegativeButton(R.string.ssl_go_back,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700515 new DialogInterface.OnClickListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700516 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700517 public void onClick(DialogInterface dialog,
518 int whichButton) {
John Reck30c714c2010-12-16 17:30:34 -0800519 dialog.cancel();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700520 }
John Reckcb28b2c2011-08-26 17:39:44 -0700521 })
522 .setOnCancelListener(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700523 new DialogInterface.OnCancelListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700524 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700525 public void onCancel(DialogInterface dialog) {
526 handler.cancel();
Steve Block2466eff2011-10-03 15:33:09 +0100527 setSecurityState(SecurityState.SECURITY_STATE_NOT_SECURE);
John Reck30c714c2010-12-16 17:30:34 -0800528 mWebViewController.onUserCanceledSsl(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700529 }
John Reckcb28b2c2011-08-26 17:39:44 -0700530 })
531 .show();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700532 } else {
533 handler.proceed();
534 }
535 }
536
537 /**
Steve Block4895b012011-10-03 16:26:46 +0100538 * Called when an SSL error occurred while loading a resource, but the
539 * WebView but chose to proceed anyway based on a decision retained
540 * from a previous response to onReceivedSslError(). We update our
541 * security state to reflect this.
542 */
543 @Override
544 public void onProceededAfterSslError(WebView view, SslError error) {
545 handleProceededAfterSslError(error);
546 }
547
548 /**
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700549 * Displays client certificate request to the user.
550 */
551 @Override
552 public void onReceivedClientCertRequest(final WebView view,
553 final ClientCertRequestHandler handler, final String host_and_port) {
554 if (!mInForeground) {
555 handler.ignore();
556 return;
557 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700558 int colon = host_and_port.lastIndexOf(':');
559 String host;
560 int port;
561 if (colon == -1) {
562 host = host_and_port;
563 port = -1;
564 } else {
565 String portString = host_and_port.substring(colon + 1);
566 try {
567 port = Integer.parseInt(portString);
568 host = host_and_port.substring(0, colon);
569 } catch (NumberFormatException e) {
570 host = host_and_port;
571 port = -1;
572 }
573 }
Michael Kolb14612442011-06-24 13:06:29 -0700574 KeyChain.choosePrivateKeyAlias(
575 mWebViewController.getActivity(), new KeyChainAliasCallback() {
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700576 @Override public void alias(String alias) {
577 if (alias == null) {
578 handler.cancel();
579 return;
580 }
Michael Kolb14612442011-06-24 13:06:29 -0700581 new KeyChainLookup(mContext, handler, alias).execute();
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700582 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700583 }, null, null, host, port, null);
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700584 }
585
586 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700587 * Handles an HTTP authentication request.
588 *
589 * @param handler The authentication handler
590 * @param host The host
591 * @param realm The realm
592 */
593 @Override
594 public void onReceivedHttpAuthRequest(WebView view,
595 final HttpAuthHandler handler, final String host,
596 final String realm) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700597 mWebViewController.onReceivedHttpAuthRequest(Tab.this, view, handler, host, realm);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700598 }
599
600 @Override
John Reck438bf462011-01-12 18:11:46 -0800601 public WebResourceResponse shouldInterceptRequest(WebView view,
602 String url) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800603 //intercept if opening a new incognito tab - show the incognito welcome page
Vivek Sekhared791da2015-02-22 12:39:05 -0800604 if (url.startsWith("chrome://incognito")) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800605 Resources resourceHandle = mContext.getResources();
606 InputStream inStream = resourceHandle.openRawResource(
Bijan Amirzada41242f22014-03-21 12:12:18 -0700607 com.android.browser.R.raw.incognito_mode_start_page);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800608 return new WebResourceResponse("text/html", "utf8", inStream);
609 }
kaiyiz6e5b3e02013-08-19 20:02:01 +0800610 WebResourceResponse res;
611 if (MyNavigationUtil.MY_NAVIGATION.equals(url)) {
612 res = MyNavigationProvider.shouldInterceptRequest(mContext, url);
613 } else {
614 res = HomeProvider.shouldInterceptRequest(mContext, url);
615 }
John Reck438bf462011-01-12 18:11:46 -0800616 return res;
617 }
618
619 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700620 public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
621 if (!mInForeground) {
622 return false;
623 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700624 return mWebViewController.shouldOverrideKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700625 }
626
627 @Override
628 public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700629 if (!mInForeground) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700630 return;
631 }
John Reck997b1b72012-04-19 18:08:25 -0700632 if (!mWebViewController.onUnhandledKeyEvent(event)) {
633 super.onUnhandledKeyEvent(view, event);
634 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700635 }
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700636
637 @Override
638 public void beforeNavigation(WebView view, String url) {
Pankaj Garg21dad562015-07-02 17:17:24 -0700639 mTouchIconUrl = null;
Pankaj Garg18aa0a12015-06-22 11:06:12 -0700640 if (BrowserCommandLine.hasSwitch("ui-low-power-mode")) {
641 return;
642 }
643
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700644 if (isPrivateBrowsingEnabled()) {
645 return;
646 }
647
648 if (!mFirstVisualPixelPainted) {
649 return;
650 }
651
652 final int idx = view.copyBackForwardList().getCurrentIndex();
653 boolean bitmapExists = view.hasSnapshot(idx);
654
655 int progress = 100;
656 Controller controller = (Controller)mWebViewController;
657 UI ui = controller.getUi();
658 if (ui instanceof BaseUi) {
659 BaseUi baseUi = (BaseUi) ui;
660 TitleBar titleBar = baseUi.getTitleBar();
661 progress = titleBar.getProgressView().getProgressPercent();
662 }
663
664 if (bitmapExists && progress < 85) {
665 return;
666 }
667
668 int index = getCaptureIndex(view.getLastCommittedHistoryIndex());
669 view.captureSnapshot(index , null);
670 }
671
672 @Override
673 public void onHistoryItemCommit(WebView view, int index) {
Pankaj Garg18aa0a12015-06-22 11:06:12 -0700674 if (BrowserCommandLine.hasSwitch("ui-low-power-mode")) {
675 return;
676 }
677
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700678 mTabHistoryUpdateObservable.set(index);
Vivek Sekhar53ef8932015-06-18 16:51:43 -0700679 final int maxIdx = view.copyBackForwardList().getSize();
680 final WebView wv = view;
681 view.getSnapshotIds(new ValueCallback <List<Integer>>() {
682 @Override
683 public void onReceiveValue(List<Integer> ids) {
684 int currentTabIdx = mWebViewController.getTabControl().getCurrentPosition();
685 for (Integer id : ids) {
686 if (getTabIdxFromCaptureIdx(id) == currentTabIdx &&
687 getNavIdxFromCaptureIdx(id) >= maxIdx) {
688 wv.deleteSnapshot(id);
689 }
690 }
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700691 }
Vivek Sekhar53ef8932015-06-18 16:51:43 -0700692 });
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700693 }
Pankaj Garg62bc7912015-04-14 16:08:59 -0700694
695 @Override
696 public void onKeyboardStateChange(boolean popup) {
697 if (BrowserSettings.getInstance().useFullscreen()) {
698 Controller controller = (Controller) mWebViewController;
699 BaseUi ui = (BaseUi) controller.getUi();
700 ui.forceDisableFullscreenMode(popup);
701 }
702 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700703 };
704
John Reck1cf4b792011-07-26 10:22:22 -0700705 private void syncCurrentState(WebView view, String url) {
706 // Sync state (in case of stop/timeout)
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800707
708 if (mReceivedError) {
709 mCurrentState.mUrl = url;
710 mCurrentState.mOriginalUrl = url;
711 } else {
712 mCurrentState.mUrl = view.getUrl();
713 mCurrentState.mOriginalUrl = view.getOriginalUrl();
714 mCurrentState.mFavicon = view.getFavicon();
715 }
716
John Reck1cf4b792011-07-26 10:22:22 -0700717 if (mCurrentState.mUrl == null) {
John Reck8bcafc12011-08-29 16:43:02 -0700718 mCurrentState.mUrl = "";
John Reck1cf4b792011-07-26 10:22:22 -0700719 }
John Reck1cf4b792011-07-26 10:22:22 -0700720 mCurrentState.mTitle = view.getTitle();
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800721
722
John Reck1cf4b792011-07-26 10:22:22 -0700723 if (!URLUtil.isHttpsUrl(mCurrentState.mUrl)) {
724 // In case we stop when loading an HTTPS page from an HTTP page
725 // but before a provisional load occurred
Steve Block2466eff2011-10-03 15:33:09 +0100726 mCurrentState.mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
Steve Block08a6f0c2011-10-06 12:12:53 +0100727 mCurrentState.mSslCertificateError = null;
John Reck1cf4b792011-07-26 10:22:22 -0700728 }
John Reck502a3532011-08-16 14:21:46 -0700729 mCurrentState.mIncognito = view.isPrivateBrowsingEnabled();
John Reck1cf4b792011-07-26 10:22:22 -0700730 }
731
Pankaj Garg21dad562015-07-02 17:17:24 -0700732 public String getTouchIconUrl() {
733 return mTouchIconUrl;
734 }
Tarun Nainani8eb00912014-07-17 12:28:32 -0700735
736 public boolean isTabFullScreen() {
737 return mFullScreen;
738 }
739
Vivek Sekharf96064b2014-07-28 16:32:34 -0700740 protected void setTabFullscreen(boolean fullScreen) {
Tarun Nainani8eb00912014-07-17 12:28:32 -0700741 Controller controller = (Controller)mWebViewController;
Sudheer Koganti24766882014-10-02 10:58:09 -0700742 controller.getUi().showFullscreen(fullScreen);
Tarun Nainani8eb00912014-07-17 12:28:32 -0700743 mFullScreen = fullScreen;
Vivek Sekharf96064b2014-07-28 16:32:34 -0700744 }
745
Sudheer Koganti24766882014-10-02 10:58:09 -0700746 public boolean exitFullscreen() {
747 if (mFullScreen) {
748 Controller controller = (Controller)mWebViewController;
749 controller.getUi().showFullscreen(false);
750 if (getWebView() != null)
751 getWebView().exitFullscreen();
752 mFullScreen = false;
753 return true;
754 }
755 return false;
756 }
757
758
759
760
Grace Kloba22ac16e2009-10-07 18:00:23 -0700761 // -------------------------------------------------------------------------
762 // WebChromeClient implementation for the main WebView
763 // -------------------------------------------------------------------------
764
765 private final WebChromeClient mWebChromeClient = new WebChromeClient() {
766 // Helper method to create a new tab or sub window.
767 private void createWindow(final boolean dialog, final Message msg) {
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700768 this.createWindow(dialog, msg, null, false);
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700769 }
770
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700771 private void createWindow(final boolean dialog, final Message msg, final String url,
772 final boolean opener_suppressed) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700773 WebView.WebViewTransport transport =
774 (WebView.WebViewTransport) msg.obj;
775 if (dialog) {
776 createSubWindow();
Michael Kolb8233fac2010-10-26 16:08:53 -0700777 mWebViewController.attachSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700778 transport.setWebView(mSubView);
779 } else {
Pankaj Garg21dad562015-07-02 17:17:24 -0700780 capture();
781
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700782 final Tab newTab = mWebViewController.openTab(url,
John Reck5949c662011-05-27 09:52:29 -0700783 Tab.this, true, true);
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700784 // This is special case for rendering links on a webpage in
785 // a new tab. If opener is suppressed, the WebContents created
786 // by the content layer are not fully initialized. This check
787 // will prevent content layer from overriding WebContents
788 // created by new tab with the uninitialized instance.
789 if (!opener_suppressed) {
790 transport.setWebView(newTab.getWebView());
791 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700792 }
793 msg.sendToTarget();
794 }
795
796 @Override
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700797 public void toggleFullscreenModeForTab(boolean enterFullscreen) {
798 if (mWebViewController instanceof Controller) {
Vivek Sekharf96064b2014-07-28 16:32:34 -0700799 setTabFullscreen(enterFullscreen);
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700800 }
801 }
802
803 @Override
Tarun Nainani8eb00912014-07-17 12:28:32 -0700804 public void onOffsetsForFullscreenChanged(float topControlsOffsetYPix,
805 float contentOffsetYPix,
806 float overdrawBottomHeightPix) {
807 if (mWebViewController instanceof Controller) {
Vivek Sekhar8ee3abb2014-07-14 12:32:05 -0700808 Controller controller = (Controller)mWebViewController;
Tarun Nainani8eb00912014-07-17 12:28:32 -0700809 controller.getUi().translateTitleBar(topControlsOffsetYPix);
Vivek Sekhar11b40062015-06-24 11:49:04 -0700810 // Resize the viewport if top controls is not visible
Vivek Sekhar2ee19a32015-07-02 17:03:57 -0700811 if (mMainView != null &&
812 (topControlsOffsetYPix == 0.0f || contentOffsetYPix == 0.0f))
Vivek Sekhar11b40062015-06-24 11:49:04 -0700813 ((BrowserWebView)mMainView).enableTopControls(
814 (topControlsOffsetYPix == 0.0f) ? true : false);
Vivek Sekhar8ee3abb2014-07-14 12:32:05 -0700815 }
816 }
817
818 @Override
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700819 public boolean isTabFullScreen() {
Vivek Sekharf96064b2014-07-28 16:32:34 -0700820 return mFullScreen;
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700821 }
822
823 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700824 public boolean onCreateWindow(WebView view, final boolean dialog,
825 final boolean userGesture, final Message resultMsg) {
826 // only allow new window or sub window for the foreground case
827 if (!mInForeground) {
828 return false;
829 }
830 // Short-circuit if we can't create any more tabs or sub windows.
831 if (dialog && mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -0700832 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700833 .setTitle(R.string.too_many_subwindows_dialog_title)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200834 .setIconAttribute(android.R.attr.alertDialogIcon)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700835 .setMessage(R.string.too_many_subwindows_dialog_message)
836 .setPositiveButton(R.string.ok, null)
837 .show();
838 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700839 } else if (!mWebViewController.getTabControl().canCreateNewTab()) {
Michael Kolb14612442011-06-24 13:06:29 -0700840 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700841 .setTitle(R.string.too_many_windows_dialog_title)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200842 .setIconAttribute(android.R.attr.alertDialogIcon)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700843 .setMessage(R.string.too_many_windows_dialog_message)
844 .setPositiveButton(R.string.ok, null)
845 .show();
846 return false;
847 }
848
849 // Short-circuit if this was a user gesture.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800850 if (userGesture || !mSettings.blockPopupWindows()) {
Tarun Nainani4f5137d2015-04-16 17:26:18 -0700851 WebView.WebViewTransport transport =
852 (WebView.WebViewTransport) resultMsg.obj;
853 CreateWindowParams windowParams = transport.getCreateWindowParams();
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700854 if (windowParams.mOpenerSuppressed) {
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700855 createWindow(dialog, resultMsg, windowParams.mURL, true);
856 // This is special case for rendering links on a webpage in
857 // a new tab. If opener is suppressed, the WebContents created
858 // by the content layer are not fully initialized. Returning false
859 // will prevent content layer from overriding WebContents
860 // created by new tab with the uninitialized instance.
861 return false;
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700862 }
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700863
864 createWindow(dialog, resultMsg);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700865 return true;
866 }
867
Tarun Nainani4f5137d2015-04-16 17:26:18 -0700868 createWindow(dialog, resultMsg);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700869 return true;
870 }
871
872 @Override
Patrick Scotteb5061b2009-11-18 15:00:30 -0500873 public void onRequestFocus(WebView view) {
874 if (!mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700875 mWebViewController.switchToTab(Tab.this);
Patrick Scotteb5061b2009-11-18 15:00:30 -0500876 }
877 }
878
879 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700880 public void onCloseWindow(WebView window) {
Michael Kolbc831b632011-05-11 09:30:34 -0700881 if (mParent != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700882 // JavaScript can only close popup window.
883 if (mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700884 mWebViewController.switchToTab(mParent);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700885 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700886 mWebViewController.closeTab(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700887 }
888 }
889
890 @Override
891 public void onProgressChanged(WebView view, int newProgress) {
John Reck30c714c2010-12-16 17:30:34 -0800892 mPageLoadProgress = newProgress;
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800893 if (newProgress == 100) {
894 mInPageLoad = false;
895 }
John Reck30c714c2010-12-16 17:30:34 -0800896 mWebViewController.onProgressChanged(Tab.this);
Michael Kolb72864272012-05-03 15:42:15 -0700897 if (mUpdateThumbnail && newProgress == 100) {
898 mUpdateThumbnail = false;
899 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700900 }
901
902 @Override
Leon Scroggins21d9b902010-03-11 09:33:11 -0500903 public void onReceivedTitle(WebView view, final String title) {
John Reck30c714c2010-12-16 17:30:34 -0800904 mCurrentState.mTitle = title;
Michael Kolb8233fac2010-10-26 16:08:53 -0700905 mWebViewController.onReceivedTitle(Tab.this, title);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700906 }
907
908 @Override
909 public void onReceivedIcon(WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -0800910 mCurrentState.mFavicon = icon;
Michael Kolb8233fac2010-10-26 16:08:53 -0700911 mWebViewController.onFavicon(Tab.this, view, icon);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700912 }
913
914 @Override
915 public void onReceivedTouchIconUrl(WebView view, String url,
916 boolean precomposed) {
Michael Kolb14612442011-06-24 13:06:29 -0700917 final ContentResolver cr = mContext.getContentResolver();
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400918 // Let precomposed icons take precedence over non-composed
919 // icons.
920 if (precomposed && mTouchIconLoader != null) {
921 mTouchIconLoader.cancel(false);
922 mTouchIconLoader = null;
923 }
924 // Have only one async task at a time.
925 if (mTouchIconLoader == null) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700926 mTouchIconLoader = new DownloadTouchIcon(Tab.this,
Michael Kolb14612442011-06-24 13:06:29 -0700927 mContext, cr, view);
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400928 mTouchIconLoader.execute(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700929 }
Pankaj Garg21dad562015-07-02 17:17:24 -0700930 mTouchIconUrl = url;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700931 }
932
933 @Override
934 public void onShowCustomView(View view,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800935 CustomViewCallback callback) {
Michael Kolb14612442011-06-24 13:06:29 -0700936 Activity activity = mWebViewController.getActivity();
937 if (activity != null) {
938 onShowCustomView(view, activity.getRequestedOrientation(), callback);
939 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400940 }
941
942 @Override
943 public void onShowCustomView(View view, int requestedOrientation,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800944 CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700945 if (mInForeground) mWebViewController.showCustomView(Tab.this, view,
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400946 requestedOrientation, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700947 }
948
949 @Override
950 public void onHideCustomView() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700951 if (mInForeground) mWebViewController.hideCustomView();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700952 }
953
954 /**
955 * The origin has exceeded its database quota.
956 * @param url the URL that exceeded the quota
957 * @param databaseIdentifier the identifier of the database on which the
958 * transaction that caused the quota overflow was run
959 * @param currentQuota the current quota for the origin.
960 * @param estimatedSize the estimated size of the database.
961 * @param totalUsedQuota is the sum of all origins' quota.
962 * @param quotaUpdater The callback to run when a decision to allow or
963 * deny quota has been made. Don't forget to call this!
964 */
965 @Override
966 public void onExceededDatabaseQuota(String url,
967 String databaseIdentifier, long currentQuota, long estimatedSize,
968 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -0700969 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -0700970 .onExceededDatabaseQuota(url, databaseIdentifier,
971 currentQuota, estimatedSize, totalUsedQuota,
972 quotaUpdater);
973 }
974
975 /**
976 * The Application Cache has exceeded its max size.
977 * @param spaceNeeded is the amount of disk space that would be needed
978 * in order for the last appcache operation to succeed.
979 * @param totalUsedQuota is the sum of all origins' quota.
980 * @param quotaUpdater A callback to inform the WebCore thread that a
981 * new app cache size is available. This callback must always
982 * be executed at some point to ensure that the sleeping
983 * WebCore thread is woken up.
984 */
985 @Override
986 public void onReachedMaxAppCacheSize(long spaceNeeded,
987 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -0700988 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -0700989 .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota,
990 quotaUpdater);
991 }
992
Ben Murdoch65acc352009-11-19 18:16:04 +0000993 /* Adds a JavaScript error message to the system log and if the JS
994 * console is enabled in the about:debug options, to that console
995 * also.
Ben Murdochc42addf2010-01-28 15:19:59 +0000996 * @param consoleMessage the message object.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700997 */
998 @Override
Ben Murdochc42addf2010-01-28 15:19:59 +0000999 public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Jeff Hamilton47654f42010-09-07 09:57:51 -05001000 // Don't log console messages in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001001 if (isPrivateBrowsingEnabled()) return true;
Jeff Hamilton47654f42010-09-07 09:57:51 -05001002
Ben Murdochc42addf2010-01-28 15:19:59 +00001003 String message = "Console: " + consoleMessage.message() + " "
1004 + consoleMessage.sourceId() + ":"
1005 + consoleMessage.lineNumber();
1006
1007 switch (consoleMessage.messageLevel()) {
1008 case TIP:
1009 Log.v(CONSOLE_LOGTAG, message);
1010 break;
1011 case LOG:
1012 Log.i(CONSOLE_LOGTAG, message);
1013 break;
1014 case WARNING:
1015 Log.w(CONSOLE_LOGTAG, message);
1016 break;
1017 case ERROR:
1018 Log.e(CONSOLE_LOGTAG, message);
1019 break;
1020 case DEBUG:
1021 Log.d(CONSOLE_LOGTAG, message);
1022 break;
1023 }
1024
1025 return true;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001026 }
1027
1028 /**
1029 * Ask the browser for an icon to represent a <video> element.
1030 * This icon will be used if the Web page did not specify a poster attribute.
1031 * @return Bitmap The icon or null if no such icon is available.
1032 */
1033 @Override
1034 public Bitmap getDefaultVideoPoster() {
1035 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001036 return mWebViewController.getDefaultVideoPoster();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001037 }
1038 return null;
1039 }
1040
1041 /**
1042 * Ask the host application for a custom progress view to show while
1043 * a <video> is loading.
1044 * @return View The progress view.
1045 */
1046 @Override
1047 public View getVideoLoadingProgressView() {
1048 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001049 return mWebViewController.getVideoLoadingProgressView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001050 }
1051 return null;
1052 }
1053
1054 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00001055 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001056 if (mInForeground) {
Ben Murdoch8cad4132012-01-11 10:56:43 +00001057 mWebViewController.openFileChooser(uploadMsg, acceptType, capture);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001058 } else {
1059 uploadMsg.onReceiveValue(null);
1060 }
1061 }
1062
Vivek Sekharb54614f2014-05-01 19:03:37 -07001063 @Override
1064 public void showFileChooser(ValueCallback<String[]> uploadFilePaths, String acceptTypes,
1065 boolean capture) {
1066 if (mInForeground) {
1067 mWebViewController.showFileChooser(uploadFilePaths, acceptTypes, capture);
1068 } else {
1069 uploadFilePaths.onReceiveValue(null);
1070 }
1071 }
1072
Grace Kloba22ac16e2009-10-07 18:00:23 -07001073 /**
1074 * Deliver a list of already-visited URLs
1075 */
1076 @Override
1077 public void getVisitedHistory(final ValueCallback<String[]> callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001078 mWebViewController.getVisitedHistory(callback);
1079 }
Ben Murdoch8029a772010-11-16 11:58:21 +00001080
1081 @Override
1082 public void setupAutoFill(Message message) {
1083 // Prompt the user to set up their profile.
1084 final Message msg = message;
Michael Kolb14612442011-06-24 13:06:29 -07001085 AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
1086 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
Ben Murdoch1d676b62011-01-17 12:54:24 +00001087 Context.LAYOUT_INFLATER_SERVICE);
1088 final View layout = inflater.inflate(R.layout.setup_autofill_dialog, null);
1089
1090 builder.setView(layout)
1091 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1092 @Override
1093 public void onClick(DialogInterface dialog, int id) {
1094 CheckBox disableAutoFill = (CheckBox) layout.findViewById(
1095 R.id.setup_autofill_dialog_disable_autofill);
1096
1097 if (disableAutoFill.isChecked()) {
1098 // Disable autofill and show a toast with how to turn it on again.
John Reck35e9dd62011-04-25 09:01:54 -07001099 mSettings.setAutofillEnabled(false);
Michael Kolb14612442011-06-24 13:06:29 -07001100 Toast.makeText(mContext,
Ben Murdoch1d676b62011-01-17 12:54:24 +00001101 R.string.autofill_setup_dialog_negative_toast,
1102 Toast.LENGTH_LONG).show();
1103 } else {
1104 // Take user to the AutoFill profile editor. When they return,
1105 // we will send the message that we pass here which will trigger
1106 // the form to get filled out with their new profile.
1107 mWebViewController.setupAutoFill(msg);
1108 }
1109 }
1110 })
1111 .setNegativeButton(R.string.cancel, null)
1112 .show();
Ben Murdoch8029a772010-11-16 11:58:21 +00001113 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001114 };
1115
1116 // -------------------------------------------------------------------------
1117 // WebViewClient implementation for the sub window
1118 // -------------------------------------------------------------------------
1119
1120 // Subclass of WebViewClient used in subwindows to notify the main
1121 // WebViewClient of certain WebView activities.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001122 private static class SubWindowClient extends WebViewClient {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001123 // The main WebViewClient.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001124 private final WebViewClient mClient;
Michael Kolb8233fac2010-10-26 16:08:53 -07001125 private final WebViewController mController;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001126
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001127 SubWindowClient(WebViewClient client, WebViewController controller) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001128 mClient = client;
Michael Kolb8233fac2010-10-26 16:08:53 -07001129 mController = controller;
Leon Scroggins III211ba542010-04-19 13:21:13 -04001130 }
1131 @Override
1132 public void onPageStarted(WebView view, String url, Bitmap favicon) {
1133 // Unlike the others, do not call mClient's version, which would
1134 // change the progress bar. However, we do want to remove the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001135 // find or select dialog.
Michael Kolb8233fac2010-10-26 16:08:53 -07001136 mController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001137 }
1138 @Override
1139 public void doUpdateVisitedHistory(WebView view, String url,
1140 boolean isReload) {
1141 mClient.doUpdateVisitedHistory(view, url, isReload);
1142 }
1143 @Override
1144 public boolean shouldOverrideUrlLoading(WebView view, String url) {
1145 return mClient.shouldOverrideUrlLoading(view, url);
1146 }
1147 @Override
1148 public void onReceivedSslError(WebView view, SslErrorHandler handler,
1149 SslError error) {
1150 mClient.onReceivedSslError(view, handler, error);
1151 }
1152 @Override
Brian Carlstrom8862c1d2011-06-02 01:05:55 -07001153 public void onReceivedClientCertRequest(WebView view,
1154 ClientCertRequestHandler handler, String host_and_port) {
1155 mClient.onReceivedClientCertRequest(view, handler, host_and_port);
1156 }
1157 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -07001158 public void onReceivedHttpAuthRequest(WebView view,
1159 HttpAuthHandler handler, String host, String realm) {
1160 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
1161 }
1162 @Override
1163 public void onFormResubmission(WebView view, Message dontResend,
1164 Message resend) {
1165 mClient.onFormResubmission(view, dontResend, resend);
1166 }
1167 @Override
1168 public void onReceivedError(WebView view, int errorCode,
1169 String description, String failingUrl) {
1170 mClient.onReceivedError(view, errorCode, description, failingUrl);
1171 }
1172 @Override
1173 public boolean shouldOverrideKeyEvent(WebView view,
1174 android.view.KeyEvent event) {
1175 return mClient.shouldOverrideKeyEvent(view, event);
1176 }
1177 @Override
1178 public void onUnhandledKeyEvent(WebView view,
1179 android.view.KeyEvent event) {
1180 mClient.onUnhandledKeyEvent(view, event);
1181 }
1182 }
1183
1184 // -------------------------------------------------------------------------
1185 // WebChromeClient implementation for the sub window
1186 // -------------------------------------------------------------------------
1187
1188 private class SubWindowChromeClient extends WebChromeClient {
1189 // The main WebChromeClient.
1190 private final WebChromeClient mClient;
1191
1192 SubWindowChromeClient(WebChromeClient client) {
1193 mClient = client;
1194 }
1195 @Override
1196 public void onProgressChanged(WebView view, int newProgress) {
1197 mClient.onProgressChanged(view, newProgress);
1198 }
1199 @Override
1200 public boolean onCreateWindow(WebView view, boolean dialog,
1201 boolean userGesture, android.os.Message resultMsg) {
1202 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
1203 }
1204 @Override
1205 public void onCloseWindow(WebView window) {
1206 if (window != mSubView) {
1207 Log.e(LOGTAG, "Can't close the window");
1208 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001209 mWebViewController.dismissSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001210 }
1211 }
1212
1213 // -------------------------------------------------------------------------
1214
1215 // Construct a new tab
Michael Kolb7bcafde2011-05-09 13:55:59 -07001216 Tab(WebViewController wvcontroller, WebView w) {
John Reck1cf4b792011-07-26 10:22:22 -07001217 this(wvcontroller, w, null);
1218 }
1219
1220 Tab(WebViewController wvcontroller, Bundle state) {
1221 this(wvcontroller, null, state);
1222 }
1223
1224 Tab(WebViewController wvcontroller, WebView w, Bundle state) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001225 mWebViewController = wvcontroller;
Michael Kolb14612442011-06-24 13:06:29 -07001226 mContext = mWebViewController.getContext();
John Reck35e9dd62011-04-25 09:01:54 -07001227 mSettings = BrowserSettings.getInstance();
Michael Kolb14612442011-06-24 13:06:29 -07001228 mDataController = DataController.getInstance(mContext);
1229 mCurrentState = new PageState(mContext, w != null
John Reck847b5322011-04-14 17:02:18 -07001230 ? w.isPrivateBrowsingEnabled() : false);
Tarun Nainani8084c822014-06-25 13:38:06 -07001231 setTimeStamp();
Michael Kolb8233fac2010-10-26 16:08:53 -07001232 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001233 mInForeground = false;
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001234 mWebViewDestroyedByMemoryMonitor = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001235
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001236 mDownloadListener = new BrowserDownloadListener() {
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001237 public void onDownloadStart(String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001238 String contentDisposition, String mimetype, String referer,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001239 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001240 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001241 mimetype, referer, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001242 }
1243 };
1244
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001245 mCaptureWidth = mContext.getResources().getDimensionPixelSize(R.dimen.tab_thumbnail_width);
1246 mCaptureHeight =mContext.getResources().getDimensionPixelSize(R.dimen.tab_thumbnail_height);
1247
1248 initCaptureBitmap();
1249
John Reck1cf4b792011-07-26 10:22:22 -07001250 restoreState(state);
John Reck52be4782011-08-26 15:37:29 -07001251 if (getId() == -1) {
1252 mId = TabControl.getNextId();
1253 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001254 setWebView(w);
Michael Kolb9ef259a2011-07-12 15:33:08 -07001255 mHandler = new Handler() {
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001256 @Override
Michael Kolb9ef259a2011-07-12 15:33:08 -07001257 public void handleMessage(Message m) {
John Reck1cf4b792011-07-26 10:22:22 -07001258 switch (m.what) {
1259 case MSG_CAPTURE:
1260 capture();
1261 break;
1262 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001263 }
1264 };
Pankaj Garg1c13cab2015-05-12 11:52:17 -07001265
1266 mFirstPixelObservable = new Observable();
1267 mFirstPixelObservable.set(false);
1268 mTabHistoryUpdateObservable = new Observable();
John Reck1cf4b792011-07-26 10:22:22 -07001269 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001270
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001271 private void initCaptureBitmap() {
1272 mCapture = Bitmap.createBitmap(mCaptureWidth, mCaptureHeight, Bitmap.Config.RGB_565);
1273 mCapture.eraseColor(Color.WHITE);
Michael Kolb72864272012-05-03 15:42:15 -07001274 }
1275
Mathew Inwoode09305e2011-09-02 12:03:26 +01001276 /**
1277 * This is used to get a new ID when the tab has been preloaded, before it is displayed and
1278 * added to TabControl. Preloaded tabs can be created before restoreInstanceState, leading
1279 * to overlapping IDs between the preloaded and restored tabs.
1280 */
1281 public void refreshIdAfterPreload() {
1282 mId = TabControl.getNextId();
1283 }
1284
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001285 public void setController(WebViewController ctl) {
1286 mWebViewController = ctl;
1287
John Reck1cf4b792011-07-26 10:22:22 -07001288 if (mWebViewController.shouldCaptureThumbnails()) {
1289 synchronized (Tab.this) {
1290 if (mCapture == null) {
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001291 initCaptureBitmap();
1292 if (mInForeground && !mHandler.hasMessages(MSG_CAPTURE)) {
1293 mHandler.sendEmptyMessageDelayed(MSG_CAPTURE, CAPTURE_DELAY);
John Reck1cf4b792011-07-26 10:22:22 -07001294 }
1295 }
1296 }
1297 } else {
1298 synchronized (Tab.this) {
1299 mCapture = null;
1300 deleteThumbnail();
1301 }
1302 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001303 }
1304
Michael Kolbc831b632011-05-11 09:30:34 -07001305 public long getId() {
1306 return mId;
1307 }
1308
Michael Kolb91911a22012-01-17 11:21:25 -08001309 void setWebView(WebView w) {
1310 setWebView(w, true);
1311 }
1312
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001313 public boolean isNativeActive(){
1314 if (mMainView == null)
1315 return false;
1316 return true;
1317 }
1318
1319 public void setTimeStamp(){
1320 Date d = new Date();
1321 timestamp = (new Timestamp(d.getTime()));
1322 }
1323
1324 public Timestamp getTimestamp() {
1325 return timestamp;
1326 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001327 /**
1328 * Sets the WebView for this tab, correctly removing the old WebView from
1329 * the container view.
1330 */
Michael Kolb91911a22012-01-17 11:21:25 -08001331 void setWebView(WebView w, boolean restore) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001332 if (mMainView == w) {
1333 return;
1334 }
Michael Kolba713ec82010-11-29 17:27:06 -08001335
Michael Kolba713ec82010-11-29 17:27:06 -08001336 mWebViewController.onSetWebView(this, w);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001337
John Reck1cf4b792011-07-26 10:22:22 -07001338 if (mMainView != null) {
John Reck8ee633f2011-08-09 16:00:35 -07001339 mMainView.setPictureListener(null);
John Reck1cf4b792011-07-26 10:22:22 -07001340 if (w != null) {
1341 syncCurrentState(w, null);
1342 } else {
Panos Thomasa9a5a582014-03-18 19:20:08 -07001343 mCurrentState = new PageState(mContext, mMainView.isPrivateBrowsingEnabled());
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001344
1345 if (mWebViewDestroyedByMemoryMonitor) {
1346 /*
1347 * If tab was destroyed as a result of the MemoryMonitor
1348 * then we need to restore the state properties
1349 * from the old WebView (mMainView)
1350 */
1351 syncCurrentState(mMainView, null);
1352 mWebViewDestroyedByMemoryMonitor = false;
1353 }
John Reck1cf4b792011-07-26 10:22:22 -07001354 }
1355 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001356 // set the new one
1357 mMainView = w;
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001358
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001359 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001360 if (mMainView != null) {
1361 mMainView.setWebViewClient(mWebViewClient);
1362 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001363 // Attach DownloadManager so that downloads can start in an active
1364 // or a non-active window. This can happen when going to a site that
1365 // does a redirect after a period of time. The user could have
1366 // switched to another tab while waiting for the download to start.
1367 mMainView.setDownloadListener(mDownloadListener);
John Reck8ee633f2011-08-09 16:00:35 -07001368 TabControl tc = mWebViewController.getTabControl();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001369 if (tc != null /*&& tc.getOnThumbnailUpdatedListener() != null*/) {
John Reck8ee633f2011-08-09 16:00:35 -07001370 mMainView.setPictureListener(this);
1371 }
Michael Kolb91911a22012-01-17 11:21:25 -08001372 if (restore && (mSavedState != null)) {
John Reck8b9bb8b2012-03-08 13:19:40 -08001373 restoreUserAgent();
John Reck6c2e2f32011-08-22 13:41:23 -07001374 WebBackForwardList restoredState
1375 = mMainView.restoreState(mSavedState);
1376 if (restoredState == null || restoredState.getSize() == 0) {
1377 Log.w(LOGTAG, "Failed to restore WebView state!");
1378 loadUrl(mCurrentState.mOriginalUrl, null);
1379 }
John Reck1cf4b792011-07-26 10:22:22 -07001380 mSavedState = null;
1381 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001382 }
1383 }
1384
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001385 public void destroyThroughMemoryMonitor() {
1386 mWebViewDestroyedByMemoryMonitor = true;
1387 destroy();
1388 }
1389
Grace Kloba22ac16e2009-10-07 18:00:23 -07001390 /**
1391 * Destroy the tab's main WebView and subWindow if any
1392 */
1393 void destroy() {
Tarun Nainani2c1dd7c2014-07-05 16:40:12 -07001394
Matthew Huib7f2e9c2014-04-16 11:12:37 -04001395 if (mPostponeDestroy) {
1396 mShouldDestroy = true;
1397 return;
1398 }
1399 mShouldDestroy = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001400 if (mMainView != null) {
1401 dismissSubWindow();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001402 // save the WebView to call destroy() after detach it from the tab
Vivek Sekhar53ef8932015-06-18 16:51:43 -07001403 final WebView webView = mMainView;
Vivek Sekhar2ee19a32015-07-02 17:03:57 -07001404 setWebView(null);
Pankaj Garg18aa0a12015-06-22 11:06:12 -07001405 if (!mWebViewDestroyedByMemoryMonitor &&
1406 !BrowserCommandLine.hasSwitch("ui-low-power-mode")) {
Vivek Sekhar2ee19a32015-07-02 17:03:57 -07001407 // Tabs can be reused with new instance of WebView so delete the snapshots
Vivek Sekhar53ef8932015-06-18 16:51:43 -07001408 webView.getSnapshotIds(new ValueCallback<List<Integer>>() {
1409 @Override
1410 public void onReceiveValue(List<Integer> ids) {
1411 int currentTabIdx = mWebViewController.getTabControl().getCurrentPosition();
1412 for (Integer id : ids) {
1413 if (getTabIdxFromCaptureIdx(id) == currentTabIdx) {
1414 webView.deleteSnapshot(id);
1415 }
1416 }
Vivek Sekhar53ef8932015-06-18 16:51:43 -07001417 webView.destroy();
Pankaj Garg1c13cab2015-05-12 11:52:17 -07001418 }
Vivek Sekhar53ef8932015-06-18 16:51:43 -07001419 });
Vivek Sekhar2ee19a32015-07-02 17:03:57 -07001420 } else {
1421 webView.destroy();
Pankaj Garg1c13cab2015-05-12 11:52:17 -07001422 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001423 }
1424 }
1425
Matthew Huib7f2e9c2014-04-16 11:12:37 -04001426 private boolean mPostponeDestroy = false;
1427 private boolean mShouldDestroy = false;
1428
1429 public void postponeDestroy() {
1430 mPostponeDestroy = true;
1431 }
1432
1433 public void performPostponedDestroy() {
1434 mPostponeDestroy = false;
1435 if (mShouldDestroy) {
1436 destroy();
1437 }
1438 }
1439
Grace Kloba22ac16e2009-10-07 18:00:23 -07001440 /**
1441 * Remove the tab from the parent
1442 */
1443 void removeFromTree() {
1444 // detach the children
Michael Kolbc831b632011-05-11 09:30:34 -07001445 if (mChildren != null) {
1446 for(Tab t : mChildren) {
1447 t.setParent(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001448 }
1449 }
1450 // remove itself from the parent list
Michael Kolbc831b632011-05-11 09:30:34 -07001451 if (mParent != null) {
1452 mParent.mChildren.remove(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001453 }
Tarun Nainani8eb00912014-07-17 12:28:32 -07001454
1455 mCapture = null;
John Reck1cf4b792011-07-26 10:22:22 -07001456 deleteThumbnail();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001457 }
1458
1459 /**
1460 * Create a new subwindow unless a subwindow already exists.
1461 * @return True if a new subwindow was created. False if one already exists.
1462 */
1463 boolean createSubWindow() {
1464 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001465 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001466 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001467 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001468 mSubView.setWebChromeClient(new SubWindowChromeClient(
1469 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001470 // Set a different DownloadListener for the mSubView, since it will
1471 // just need to dismiss the mSubView, rather than close the Tab
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001472 mSubView.setDownloadListener(new BrowserDownloadListener() {
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001473 public void onDownloadStart(String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001474 String contentDisposition, String mimetype, String referer,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001475 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001476 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001477 contentDisposition, mimetype, referer, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001478 if (mSubView.copyBackForwardList().getSize() == 0) {
1479 // This subwindow was opened for the sole purpose of
1480 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001481 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001482 }
1483 }
1484 });
Michael Kolb14612442011-06-24 13:06:29 -07001485 mSubView.setOnCreateContextMenuListener(mWebViewController.getActivity());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001486 return true;
1487 }
1488 return false;
1489 }
1490
1491 /**
1492 * Dismiss the subWindow for the tab.
1493 */
1494 void dismissSubWindow() {
1495 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001496 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001497 mSubView.destroy();
1498 mSubView = null;
1499 mSubViewContainer = null;
1500 }
1501 }
1502
Grace Kloba22ac16e2009-10-07 18:00:23 -07001503
1504 /**
1505 * Set the parent tab of this tab.
1506 */
Michael Kolbc831b632011-05-11 09:30:34 -07001507 void setParent(Tab parent) {
John Reck52be4782011-08-26 15:37:29 -07001508 if (parent == this) {
1509 throw new IllegalStateException("Cannot set parent to self!");
1510 }
Michael Kolbc831b632011-05-11 09:30:34 -07001511 mParent = parent;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001512 // This tab may have been freed due to low memory. If that is the case,
Michael Kolbc831b632011-05-11 09:30:34 -07001513 // the parent tab id is already saved. If we are changing that id
Grace Kloba22ac16e2009-10-07 18:00:23 -07001514 // (most likely due to removing the parent tab) we must update the
Michael Kolbc831b632011-05-11 09:30:34 -07001515 // parent tab id in the saved Bundle.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001516 if (mSavedState != null) {
1517 if (parent == null) {
1518 mSavedState.remove(PARENTTAB);
1519 } else {
Michael Kolbc831b632011-05-11 09:30:34 -07001520 mSavedState.putLong(PARENTTAB, parent.getId());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001521 }
1522 }
John Reckb0a86db2011-05-24 14:05:58 -07001523
1524 // Sync the WebView useragent with the parent
1525 if (parent != null && mSettings.hasDesktopUseragent(parent.getWebView())
1526 != mSettings.hasDesktopUseragent(getWebView())) {
1527 mSettings.toggleDesktopUseragent(getWebView());
1528 }
John Reck52be4782011-08-26 15:37:29 -07001529
1530 if (parent != null && parent.getId() == getId()) {
1531 throw new IllegalStateException("Parent has same ID as child!");
1532 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001533 }
1534
1535 /**
Michael Kolbc831b632011-05-11 09:30:34 -07001536 * If this Tab was created through another Tab, then this method returns
1537 * that Tab.
1538 * @return the Tab parent or null
1539 */
1540 public Tab getParent() {
1541 return mParent;
1542 }
1543
1544 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001545 * When a Tab is created through the content of another Tab, then we
1546 * associate the Tabs.
1547 * @param child the Tab that was created from this Tab
1548 */
1549 void addChildTab(Tab child) {
Michael Kolbc831b632011-05-11 09:30:34 -07001550 if (mChildren == null) {
1551 mChildren = new Vector<Tab>();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001552 }
Michael Kolbc831b632011-05-11 09:30:34 -07001553 mChildren.add(child);
1554 child.setParent(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001555 }
1556
Michael Kolbc831b632011-05-11 09:30:34 -07001557 Vector<Tab> getChildren() {
1558 return mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001559 }
1560
1561 void resume() {
1562 if (mMainView != null) {
John Reck56c1fcf2011-08-17 10:15:16 -07001563 setupHwAcceleration(mMainView);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001564 mMainView.onResume();
1565 if (mSubView != null) {
1566 mSubView.onResume();
1567 }
1568 }
1569 }
1570
John Reck56c1fcf2011-08-17 10:15:16 -07001571 private void setupHwAcceleration(View web) {
1572 if (web == null) return;
1573 BrowserSettings settings = BrowserSettings.getInstance();
1574 if (settings.isHardwareAccelerated()) {
1575 web.setLayerType(View.LAYER_TYPE_NONE, null);
1576 } else {
1577 web.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
1578 }
1579 }
1580
Grace Kloba22ac16e2009-10-07 18:00:23 -07001581 void pause() {
1582 if (mMainView != null) {
1583 mMainView.onPause();
1584 if (mSubView != null) {
1585 mSubView.onPause();
1586 }
1587 }
1588 }
1589
1590 void putInForeground() {
John Reck8ee633f2011-08-09 16:00:35 -07001591 if (mInForeground) {
1592 return;
1593 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001594 mInForeground = true;
1595 resume();
Michael Kolb14612442011-06-24 13:06:29 -07001596 Activity activity = mWebViewController.getActivity();
1597 mMainView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001598 if (mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -07001599 mSubView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001600 }
Axesh R. Ajmerac6b5c322015-05-01 11:06:10 -07001601
Leon Scroggins1961ed22010-12-07 15:22:21 -05001602 mWebViewController.bookmarkedStatusHasChanged(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001603 }
1604
1605 void putInBackground() {
John Reck8ee633f2011-08-09 16:00:35 -07001606 if (!mInForeground) {
1607 return;
1608 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001609 mInForeground = false;
1610 pause();
1611 mMainView.setOnCreateContextMenuListener(null);
1612 if (mSubView != null) {
1613 mSubView.setOnCreateContextMenuListener(null);
1614 }
1615 }
1616
Michael Kolb8233fac2010-10-26 16:08:53 -07001617 boolean inForeground() {
1618 return mInForeground;
1619 }
1620
Grace Kloba22ac16e2009-10-07 18:00:23 -07001621 /**
1622 * Return the top window of this tab; either the subwindow if it is not
1623 * null or the main window.
1624 * @return The top window of this tab.
1625 */
1626 WebView getTopWindow() {
1627 if (mSubView != null) {
1628 return mSubView;
1629 }
1630 return mMainView;
1631 }
1632
1633 /**
1634 * Return the main window of this tab. Note: if a tab is freed in the
1635 * background, this can return null. It is only guaranteed to be
1636 * non-null for the current tab.
1637 * @return The main WebView of this tab.
1638 */
1639 WebView getWebView() {
1640 return mMainView;
1641 }
1642
Michael Kolba713ec82010-11-29 17:27:06 -08001643 void setViewContainer(View container) {
1644 mContainer = container;
1645 }
1646
Michael Kolb8233fac2010-10-26 16:08:53 -07001647 View getViewContainer() {
1648 return mContainer;
1649 }
1650
Grace Kloba22ac16e2009-10-07 18:00:23 -07001651 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001652 * Return whether private browsing is enabled for the main window of
1653 * this tab.
1654 * @return True if private browsing is enabled.
1655 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001656 boolean isPrivateBrowsingEnabled() {
John Reck502a3532011-08-16 14:21:46 -07001657 return mCurrentState.mIncognito;
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001658 }
1659
1660 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001661 * Return the subwindow of this tab or null if there is no subwindow.
1662 * @return The subwindow of this tab or null.
1663 */
1664 WebView getSubWebView() {
1665 return mSubView;
1666 }
1667
Michael Kolb1514bb72010-11-22 09:11:48 -08001668 void setSubWebView(WebView subView) {
1669 mSubView = subView;
1670 }
1671
Michael Kolb8233fac2010-10-26 16:08:53 -07001672 View getSubViewContainer() {
1673 return mSubViewContainer;
1674 }
1675
Michael Kolb1514bb72010-11-22 09:11:48 -08001676 void setSubViewContainer(View subViewContainer) {
1677 mSubViewContainer = subViewContainer;
1678 }
1679
Grace Kloba22ac16e2009-10-07 18:00:23 -07001680
1681 /**
1682 * @return The application id string
1683 */
1684 String getAppId() {
1685 return mAppId;
1686 }
1687
1688 /**
1689 * Set the application id string
1690 * @param id
1691 */
1692 void setAppId(String id) {
1693 mAppId = id;
1694 }
1695
Michael Kolbe28b3472011-08-04 16:54:31 -07001696 boolean closeOnBack() {
1697 return mCloseOnBack;
1698 }
1699
1700 void setCloseOnBack(boolean close) {
1701 mCloseOnBack = close;
1702 }
1703
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001704 boolean getDerivedFromIntent() {
1705 return mDerivedFromIntent;
1706 }
1707
1708 void setDerivedFromIntent(boolean derived) {
1709 mDerivedFromIntent = derived;
1710 }
1711
Grace Kloba22ac16e2009-10-07 18:00:23 -07001712 String getUrl() {
John Reck324d4402011-01-11 16:56:42 -08001713 return UrlUtils.filteredUrl(mCurrentState.mUrl);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001714 }
1715
Tarun Nainani8eb00912014-07-17 12:28:32 -07001716
1717 protected void onPageFinished() {
1718 mPageFinished = true;
1719 }
1720
1721 public boolean getPageFinishedStatus() {
1722 return mPageFinished;
1723 }
1724
John Reck49a603c2011-03-03 09:33:05 -08001725 String getOriginalUrl() {
John Reckdb22ec42011-06-29 11:31:24 -07001726 if (mCurrentState.mOriginalUrl == null) {
1727 return getUrl();
John Reck49a603c2011-03-03 09:33:05 -08001728 }
John Reckdb22ec42011-06-29 11:31:24 -07001729 return UrlUtils.filteredUrl(mCurrentState.mOriginalUrl);
John Reck49a603c2011-03-03 09:33:05 -08001730 }
1731
Grace Kloba22ac16e2009-10-07 18:00:23 -07001732 /**
John Reck30c714c2010-12-16 17:30:34 -08001733 * Get the title of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001734 */
1735 String getTitle() {
John Reck30c714c2010-12-16 17:30:34 -08001736 return mCurrentState.mTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001737 }
1738
1739 /**
John Reck30c714c2010-12-16 17:30:34 -08001740 * Get the favicon of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001741 */
1742 Bitmap getFavicon() {
John Reck1cf4b792011-07-26 10:22:22 -07001743 if (mCurrentState.mFavicon != null) {
1744 return mCurrentState.mFavicon;
1745 }
1746 return getDefaultFavicon(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001747 }
1748
Pankaj Garg32e1b942015-06-03 18:13:24 -07001749 public boolean hasFavicon() {
1750 return mCurrentState.mFavicon != null;
1751 }
1752
John Recke969cc52010-12-21 17:24:43 -08001753 public boolean isBookmarkedSite() {
1754 return mCurrentState.mIsBookmarkedSite;
1755 }
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001756
Grace Kloba22ac16e2009-10-07 18:00:23 -07001757 /**
Steve Block08a6f0c2011-10-06 12:12:53 +01001758 * Sets the security state, clears the SSL certificate error and informs
1759 * the controller.
1760 */
Steve Block2466eff2011-10-03 15:33:09 +01001761 private void setSecurityState(SecurityState securityState) {
1762 mCurrentState.mSecurityState = securityState;
Steve Block08a6f0c2011-10-06 12:12:53 +01001763 mCurrentState.mSslCertificateError = null;
Steve Block2466eff2011-10-03 15:33:09 +01001764 mWebViewController.onUpdatedSecurityState(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001765 }
1766
1767 /**
Steve Block2466eff2011-10-03 15:33:09 +01001768 * @return The tab's security state.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001769 */
Steve Block2466eff2011-10-03 15:33:09 +01001770 SecurityState getSecurityState() {
1771 return mCurrentState.mSecurityState;
John Reck30c714c2010-12-16 17:30:34 -08001772 }
1773
Steve Block08a6f0c2011-10-06 12:12:53 +01001774 /**
1775 * Gets the SSL certificate error, if any, for the page's main resource.
1776 * This is only non-null when the security state is
1777 * SECURITY_STATE_BAD_CERTIFICATE.
1778 */
1779 SslError getSslCertificateError() {
1780 return mCurrentState.mSslCertificateError;
1781 }
1782
John Reck30c714c2010-12-16 17:30:34 -08001783 int getLoadProgress() {
1784 if (mInPageLoad) {
1785 return mPageLoadProgress;
1786 }
1787 return 100;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001788 }
1789
1790 /**
1791 * @return TRUE if onPageStarted is called while onPageFinished is not
1792 * called yet.
1793 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001794 boolean inPageLoad() {
1795 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001796 }
1797
Grace Kloba22ac16e2009-10-07 18:00:23 -07001798 /**
John Reck1cf4b792011-07-26 10:22:22 -07001799 * @return The Bundle with the tab's state if it can be saved, otherwise null
Grace Kloba22ac16e2009-10-07 18:00:23 -07001800 */
John Reck1cf4b792011-07-26 10:22:22 -07001801 public Bundle saveState() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001802 // If the WebView is null it means we ran low on memory and we already
1803 // stored the saved state in mSavedState.
1804 if (mMainView == null) {
John Reck1cf4b792011-07-26 10:22:22 -07001805 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001806 }
John Reck6c2e2f32011-08-22 13:41:23 -07001807
1808 if (TextUtils.isEmpty(mCurrentState.mUrl)) {
John Reck1cf4b792011-07-26 10:22:22 -07001809 return null;
John Reck24f18262011-06-17 14:47:20 -07001810 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001811
1812 mSavedState = new Bundle();
John Reck6c2e2f32011-08-22 13:41:23 -07001813 WebBackForwardList savedList = mMainView.saveState(mSavedState);
1814 if (savedList == null || savedList.getSize() == 0) {
1815 Log.w(LOGTAG, "Failed to save back/forward list for "
1816 + mCurrentState.mUrl);
1817 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001818
Michael Kolbc831b632011-05-11 09:30:34 -07001819 mSavedState.putLong(ID, mId);
John Reck30c714c2010-12-16 17:30:34 -08001820 mSavedState.putString(CURRURL, mCurrentState.mUrl);
1821 mSavedState.putString(CURRTITLE, mCurrentState.mTitle);
John Reck1cf4b792011-07-26 10:22:22 -07001822 mSavedState.putBoolean(INCOGNITO, mMainView.isPrivateBrowsingEnabled());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001823 if (mAppId != null) {
1824 mSavedState.putString(APPID, mAppId);
1825 }
Michael Kolbe28b3472011-08-04 16:54:31 -07001826 mSavedState.putBoolean(CLOSEFLAG, mCloseOnBack);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001827 // Remember the parent tab so the relationship can be restored.
Michael Kolbc831b632011-05-11 09:30:34 -07001828 if (mParent != null) {
1829 mSavedState.putLong(PARENTTAB, mParent.mId);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001830 }
John Reckb0a86db2011-05-24 14:05:58 -07001831 mSavedState.putBoolean(USERAGENT,
1832 mSettings.hasDesktopUseragent(getWebView()));
John Reck1cf4b792011-07-26 10:22:22 -07001833 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001834 }
1835
1836 /*
1837 * Restore the state of the tab.
1838 */
John Reck1cf4b792011-07-26 10:22:22 -07001839 private void restoreState(Bundle b) {
1840 mSavedState = b;
1841 if (mSavedState == null) {
1842 return;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001843 }
1844 // Restore the internal state even if the WebView fails to restore.
1845 // This will maintain the app id, original url and close-on-exit values.
Michael Kolbc831b632011-05-11 09:30:34 -07001846 mId = b.getLong(ID);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001847 mAppId = b.getString(APPID);
Michael Kolbe28b3472011-08-04 16:54:31 -07001848 mCloseOnBack = b.getBoolean(CLOSEFLAG);
John Reck8b9bb8b2012-03-08 13:19:40 -08001849 restoreUserAgent();
John Reck1cf4b792011-07-26 10:22:22 -07001850 String url = b.getString(CURRURL);
1851 String title = b.getString(CURRTITLE);
1852 boolean incognito = b.getBoolean(INCOGNITO);
1853 mCurrentState = new PageState(mContext, incognito, url, null);
1854 mCurrentState.mTitle = title;
1855 synchronized (Tab.this) {
1856 if (mCapture != null) {
John Reck4eadc342011-10-31 14:04:10 -07001857 DataController.getInstance(mContext).loadThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07001858 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001859 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001860 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04001861
John Reck8b9bb8b2012-03-08 13:19:40 -08001862 private void restoreUserAgent() {
1863 if (mMainView == null || mSavedState == null) {
1864 return;
1865 }
1866 if (mSavedState.getBoolean(USERAGENT)
1867 != mSettings.hasDesktopUseragent(mMainView)) {
1868 mSettings.toggleDesktopUseragent(mMainView);
1869 }
1870 }
1871
Leon Scroggins1961ed22010-12-07 15:22:21 -05001872 public void updateBookmarkedStatus() {
John Recke969cc52010-12-21 17:24:43 -08001873 mDataController.queryBookmarkStatus(getUrl(), mIsBookmarkCallback);
Leon Scroggins1961ed22010-12-07 15:22:21 -05001874 }
1875
John Recke969cc52010-12-21 17:24:43 -08001876 private DataController.OnQueryUrlIsBookmark mIsBookmarkCallback
1877 = new DataController.OnQueryUrlIsBookmark() {
1878 @Override
1879 public void onQueryUrlIsBookmark(String url, boolean isBookmark) {
1880 if (mCurrentState.mUrl.equals(url)) {
1881 mCurrentState.mIsBookmarkedSite = isBookmark;
1882 mWebViewController.bookmarkedStatusHasChanged(Tab.this);
1883 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001884 }
John Recke969cc52010-12-21 17:24:43 -08001885 };
Michael Kolb1acef692011-03-08 14:12:06 -08001886
Michael Kolbeb95db42011-03-03 10:38:40 -08001887 public Bitmap getScreenshot() {
John Reck1cf4b792011-07-26 10:22:22 -07001888 synchronized (Tab.this) {
1889 return mCapture;
1890 }
Michael Kolbeb95db42011-03-03 10:38:40 -08001891 }
1892
John Reck541f55a2011-06-07 16:34:43 -07001893 public boolean isSnapshot() {
John Reck541f55a2011-06-07 16:34:43 -07001894 return false;
1895 }
1896
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001897 private static class SaveCallback implements ValueCallback<String> {
1898 boolean onReceiveValueCalled = false;
1899 private String mPath;
John Reck68234a92012-04-19 15:27:12 -07001900
1901 @Override
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001902 public void onReceiveValue(String path) {
1903 this.onReceiveValueCalled = true;
1904 this.mPath = path;
John Reck68234a92012-04-19 15:27:12 -07001905 synchronized (this) {
1906 notifyAll();
John Reck8cc92352011-07-06 17:41:52 -07001907 }
John Reck541f55a2011-06-07 16:34:43 -07001908 }
John Reck68234a92012-04-19 15:27:12 -07001909
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001910 public String getPath() {
1911 return mPath;
1912 }
John Reck68234a92012-04-19 15:27:12 -07001913 }
1914
1915 /**
1916 * Must be called on the UI thread
1917 */
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001918 public ContentValues createSnapshotValues(Bitmap bm) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001919 WebView web = getWebView();
John Reck68234a92012-04-19 15:27:12 -07001920 if (web == null) return null;
John Reckd8c74522011-06-14 08:45:00 -07001921 ContentValues values = new ContentValues();
1922 values.put(Snapshots.TITLE, mCurrentState.mTitle);
1923 values.put(Snapshots.URL, mCurrentState.mUrl);
John Reck68234a92012-04-19 15:27:12 -07001924 values.put(Snapshots.BACKGROUND, web.getPageBackgroundColor());
John Reck8cc92352011-07-06 17:41:52 -07001925 values.put(Snapshots.DATE_CREATED, System.currentTimeMillis());
1926 values.put(Snapshots.FAVICON, compressBitmap(getFavicon()));
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001927 values.put(Snapshots.THUMBNAIL, compressBitmap(bm));
John Reckd8c74522011-06-14 08:45:00 -07001928 return values;
John Reck541f55a2011-06-07 16:34:43 -07001929 }
1930
John Reck68234a92012-04-19 15:27:12 -07001931 /**
1932 * Probably want to call this on a background thread
1933 */
1934 public boolean saveViewState(ContentValues values) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001935 WebView web = getWebView();
John Reck68234a92012-04-19 15:27:12 -07001936 if (web == null) return false;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001937 String filename = UUID.randomUUID().toString();
John Reck68234a92012-04-19 15:27:12 -07001938 SaveCallback callback = new SaveCallback();
John Reck68234a92012-04-19 15:27:12 -07001939 try {
John Reck68234a92012-04-19 15:27:12 -07001940 synchronized (callback) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001941 web.saveViewState(filename, callback);
1942 callback.wait();
John Reck68234a92012-04-19 15:27:12 -07001943 }
John Reck68234a92012-04-19 15:27:12 -07001944 } catch (Exception e) {
1945 Log.w(LOGTAG, "Failed to save view state", e);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001946 String path = callback.getPath();
1947 if (path != null) {
1948 File file = mContext.getFileStreamPath(path);
1949 if (file.exists() && !file.delete()) {
1950 file.deleteOnExit();
1951 }
John Reck68234a92012-04-19 15:27:12 -07001952 }
1953 return false;
1954 }
Tarun Nainani8eb00912014-07-17 12:28:32 -07001955
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001956 String path = callback.getPath();
Tarun Nainani8eb00912014-07-17 12:28:32 -07001957 // could be that saving of file failed
1958 if (path == null) {
1959 return false;
1960 }
1961
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001962 File savedFile = new File(path);
1963 if (!savedFile.exists()) {
1964 return false;
John Reck68234a92012-04-19 15:27:12 -07001965 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001966 values.put(Snapshots.VIEWSTATE_PATH, path.substring(path.lastIndexOf('/') + 1));
1967 values.put(Snapshots.VIEWSTATE_SIZE, savedFile.length());
John Reck68234a92012-04-19 15:27:12 -07001968 return true;
1969 }
1970
John Reck8cc92352011-07-06 17:41:52 -07001971 public byte[] compressBitmap(Bitmap bitmap) {
1972 if (bitmap == null) {
1973 return null;
1974 }
1975 ByteArrayOutputStream stream = new ByteArrayOutputStream();
1976 bitmap.compress(CompressFormat.PNG, 100, stream);
1977 return stream.toByteArray();
1978 }
1979
John Reck26b18322011-06-21 13:08:58 -07001980 public void loadUrl(String url, Map<String, String> headers) {
1981 if (mMainView != null) {
Michael Kolba53c9892011-10-05 13:31:40 -07001982 mPageLoadProgress = INITIAL_PROGRESS;
Michael Kolb14612442011-06-24 13:06:29 -07001983 mCurrentState = new PageState(mContext, false, url, null);
John Reck26b18322011-06-21 13:08:58 -07001984 mMainView.loadUrl(url, headers);
1985 }
1986 }
1987
John Reck38b39652012-06-05 09:22:59 -07001988 public void disableUrlOverridingForLoad() {
1989 mDisableOverrideUrlLoading = true;
1990 }
1991
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001992 private void thumbnailUpdated() {
John Reck8ee633f2011-08-09 16:00:35 -07001993 mHandler.removeMessages(MSG_CAPTURE);
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001994
John Reck8ee633f2011-08-09 16:00:35 -07001995 TabControl tc = mWebViewController.getTabControl();
1996 if (tc != null) {
Pankaj Garg6bedeba2015-06-23 15:47:37 -07001997 OnThumbnailUpdatedListener updateListener = tc.getOnThumbnailUpdatedListener();
John Reck8ee633f2011-08-09 16:00:35 -07001998 if (updateListener != null) {
1999 updateListener.onThumbnailUpdated(this);
2000 }
2001 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07002002 }
2003
Pankaj Garg6bedeba2015-06-23 15:47:37 -07002004 protected void capture() {
2005 if (mMainView == null || mCapture == null || !mMainView.isReady() ||
2006 mMainView.getContentWidth() <= 0 || mMainView.getContentHeight() <= 0 ||
2007 !mFirstVisualPixelPainted || mMainView.isShowingCrashView()) {
2008
2009 initCaptureBitmap();
2010 thumbnailUpdated();
2011 return;
2012 }
2013
2014 mMainView.getContentBitmapAsync((float) mCaptureWidth / mMainView.getWidth(), new Rect(),
2015 new ValueCallback<Bitmap>() {
2016 @Override
2017 public void onReceiveValue(Bitmap bitmap) {
2018 if (mCapture == null) {
2019 initCaptureBitmap();
2020 }
2021
2022 if (bitmap == null) {
2023 thumbnailUpdated();
2024 return;
2025 }
2026
2027 Canvas c = new Canvas(mCapture);
2028 mCapture.eraseColor(Color.WHITE);
2029 c.drawBitmap(bitmap, 0, 0, null);
2030
2031 // manually anti-alias the edges for the tilt
2032 c.drawRect(0, 0, 1, mCapture.getHeight(), sAlphaPaint);
2033 c.drawRect(mCapture.getWidth() - 1, 0, mCapture.getWidth(),
2034 mCapture.getHeight(), sAlphaPaint);
2035 c.drawRect(0, 0, mCapture.getWidth(), 1, sAlphaPaint);
2036 c.drawRect(0, mCapture.getHeight() - 1, mCapture.getWidth(),
2037 mCapture.getHeight(), sAlphaPaint);
2038 c.setBitmap(null);
2039
2040 persistThumbnail();
2041 thumbnailUpdated();
2042 }
2043 }
2044 );
John Reck1cf4b792011-07-26 10:22:22 -07002045 }
2046
Pankaj Garg6bedeba2015-06-23 15:47:37 -07002047 @Override
2048 public void onNewPicture(WebView view, Picture picture) {
Michael Kolb9ef259a2011-07-12 15:33:08 -07002049 }
2050
John Reckef654f12011-07-12 16:42:08 -07002051 public boolean canGoBack() {
2052 return mMainView != null ? mMainView.canGoBack() : false;
2053 }
2054
2055 public boolean canGoForward() {
2056 return mMainView != null ? mMainView.canGoForward() : false;
2057 }
2058
2059 public void goBack() {
2060 if (mMainView != null) {
2061 mMainView.goBack();
2062 }
2063 }
2064
2065 public void goForward() {
2066 if (mMainView != null) {
2067 mMainView.goForward();
2068 }
2069 }
2070
John Reck1cf4b792011-07-26 10:22:22 -07002071 protected void persistThumbnail() {
John Reck4eadc342011-10-31 14:04:10 -07002072 DataController.getInstance(mContext).saveThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07002073 }
2074
2075 protected void deleteThumbnail() {
John Reck4eadc342011-10-31 14:04:10 -07002076 DataController.getInstance(mContext).deleteThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07002077 }
2078
John Reck4eadc342011-10-31 14:04:10 -07002079 void updateCaptureFromBlob(byte[] blob) {
John Reck1cf4b792011-07-26 10:22:22 -07002080 synchronized (Tab.this) {
2081 if (mCapture == null) {
2082 return;
2083 }
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002084 ByteBuffer buffer = ByteBuffer.wrap(blob);
Michael Kolbd837a112011-08-09 14:00:18 -07002085 try {
2086 mCapture.copyPixelsFromBuffer(buffer);
2087 } catch (RuntimeException rex) {
2088 Log.e(LOGTAG, "Load capture has mismatched sizes; buffer: "
2089 + buffer.capacity() + " blob: " + blob.length
2090 + "capture: " + mCapture.getByteCount());
2091 throw rex;
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002092 }
John Reck1cf4b792011-07-26 10:22:22 -07002093 }
2094 }
2095
John Reck52be4782011-08-26 15:37:29 -07002096 @Override
2097 public String toString() {
2098 StringBuilder builder = new StringBuilder(100);
2099 builder.append(mId);
2100 builder.append(") has parent: ");
2101 if (getParent() != null) {
2102 builder.append("true[");
2103 builder.append(getParent().getId());
2104 builder.append("]");
2105 } else {
2106 builder.append("false");
2107 }
2108 builder.append(", incog: ");
2109 builder.append(isPrivateBrowsingEnabled());
2110 if (!isPrivateBrowsingEnabled()) {
2111 builder.append(", title: ");
2112 builder.append(getTitle());
2113 builder.append(", url: ");
2114 builder.append(getUrl());
2115 }
2116 return builder.toString();
2117 }
2118
Steve Block4895b012011-10-03 16:26:46 +01002119 private void handleProceededAfterSslError(SslError error) {
2120 if (error.getUrl().equals(mCurrentState.mUrl)) {
2121 // The security state should currently be SECURITY_STATE_SECURE.
2122 setSecurityState(SecurityState.SECURITY_STATE_BAD_CERTIFICATE);
Steve Block08a6f0c2011-10-06 12:12:53 +01002123 mCurrentState.mSslCertificateError = error;
Steve Block4895b012011-10-03 16:26:46 +01002124 } else if (getSecurityState() == SecurityState.SECURITY_STATE_SECURE) {
Steve Block08a6f0c2011-10-06 12:12:53 +01002125 // The page's main resource is secure and this error is for a
2126 // sub-resource.
Steve Block4895b012011-10-03 16:26:46 +01002127 setSecurityState(SecurityState.SECURITY_STATE_MIXED);
2128 }
2129 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002130}