blob: 4ca8937e124da94f17b3fb44a3a4ad1135aff6c6 [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.GeolocationPermissions;
Grace Kloba22ac16e2009-10-07 18:00:23 -070054import android.webkit.URLUtil;
John Reck438bf462011-01-12 18:11:46 -080055import android.webkit.WebResourceResponse;
Grace Kloba22ac16e2009-10-07 18:00:23 -070056import android.webkit.WebStorage;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080057import android.webkit.WebChromeClient.CustomViewCallback;
58import android.webkit.ValueCallback;
Ben Murdoch1d676b62011-01-17 12:54:24 +000059import android.widget.CheckBox;
Ben Murdoch8029a772010-11-16 11:58:21 +000060import android.widget.Toast;
Grace Kloba22ac16e2009-10-07 18:00:23 -070061
Bijan Amirzada41242f22014-03-21 12:12:18 -070062import com.android.browser.TabControl.OnThumbnailUpdatedListener;
63import com.android.browser.homepages.HomeProvider;
64import com.android.browser.mynavigation.MyNavigationUtil;
65import com.android.browser.provider.MyNavigationProvider;
66import com.android.browser.provider.SnapshotProvider.Snapshots;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080067
68import 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;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080087import java.sql.Timestamp;
88import java.util.Date;
Michael Kolbfe251992010-07-08 15:41:55 -070089
Grace Kloba22ac16e2009-10-07 18:00:23 -070090/**
91 * Class for maintaining Tabs with a main WebView and a subwindow.
92 */
Michael Kolb9ef259a2011-07-12 15:33:08 -070093class Tab implements PictureListener {
Michael Kolb8233fac2010-10-26 16:08:53 -070094
Grace Kloba22ac16e2009-10-07 18:00:23 -070095 // Log Tag
96 private static final String LOGTAG = "Tab";
Bijan Amirzada41242f22014-03-21 12:12:18 -070097 private static final boolean LOGD_ENABLED = com.android.browser.Browser.LOGD_ENABLED;
Ben Murdochc42addf2010-01-28 15:19:59 +000098 // Special case the logtag for messages for the Console to make it easier to
99 // filter them and match the logtag used for these messages in older versions
100 // of the browser.
101 private static final String CONSOLE_LOGTAG = "browser";
102
Michael Kolb9ef259a2011-07-12 15:33:08 -0700103 private static final int MSG_CAPTURE = 42;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800104 private static final int CAPTURE_DELAY = 1000;
Michael Kolba53c9892011-10-05 13:31:40 -0700105 private static final int INITIAL_PROGRESS = 5;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700106
John Reck1cf4b792011-07-26 10:22:22 -0700107 private static Bitmap sDefaultFavicon;
108
Michael Kolba3194d02011-09-07 11:23:51 -0700109 private static Paint sAlphaPaint = new Paint();
110 static {
111 sAlphaPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
112 sAlphaPaint.setColor(Color.TRANSPARENT);
113 }
114
Steve Block2466eff2011-10-03 15:33:09 +0100115 public enum SecurityState {
Steve Block4895b012011-10-03 16:26:46 +0100116 // The page's main resource does not use SSL. Note that we use this
117 // state irrespective of the SSL authentication state of sub-resources.
Steve Block2466eff2011-10-03 15:33:09 +0100118 SECURITY_STATE_NOT_SECURE,
Steve Block4895b012011-10-03 16:26:46 +0100119 // The page's main resource uses SSL and the certificate is good. The
120 // same is true of all sub-resources.
Steve Block2466eff2011-10-03 15:33:09 +0100121 SECURITY_STATE_SECURE,
Steve Block4895b012011-10-03 16:26:46 +0100122 // The page's main resource uses SSL and the certificate is good, but
123 // some sub-resources either do not use SSL or have problems with their
124 // certificates.
Steve Block2466eff2011-10-03 15:33:09 +0100125 SECURITY_STATE_MIXED,
Steve Block4895b012011-10-03 16:26:46 +0100126 // The page's main resource uses SSL but there is a problem with its
127 // certificate.
128 SECURITY_STATE_BAD_CERTIFICATE,
John Reck30c714c2010-12-16 17:30:34 -0800129 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700130
Michael Kolb14612442011-06-24 13:06:29 -0700131 Context mContext;
John Reckd8c74522011-06-14 08:45:00 -0700132 protected WebViewController mWebViewController;
Michael Kolb8233fac2010-10-26 16:08:53 -0700133
Michael Kolbc831b632011-05-11 09:30:34 -0700134 // The tab ID
John Reckd8c74522011-06-14 08:45:00 -0700135 private long mId = -1;
Michael Kolbc831b632011-05-11 09:30:34 -0700136
Grace Kloba22ac16e2009-10-07 18:00:23 -0700137 // The Geolocation permissions prompt
138 private GeolocationPermissionsPrompt mGeolocationPermissionsPrompt;
139 // Main WebView wrapper
Michael Kolba713ec82010-11-29 17:27:06 -0800140 private View mContainer;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700141 // Main WebView
142 private WebView mMainView;
143 // Subwindow container
144 private View mSubViewContainer;
145 // Subwindow WebView
146 private WebView mSubView;
147 // Saved bundle for when we are running low on memory. It contains the
148 // information needed to restore the WebView if the user goes back to the
149 // tab.
150 private Bundle mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700151 // Parent Tab. This is the Tab that created this Tab, or null if the Tab was
152 // created by the UI
Michael Kolbc831b632011-05-11 09:30:34 -0700153 private Tab mParent;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700154 // Tab that constructed by this Tab. This is used when this Tab is
155 // destroyed, it clears all mParentTab values in the children.
Michael Kolbc831b632011-05-11 09:30:34 -0700156 private Vector<Tab> mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700157 // If true, the tab is in the foreground of the current activity.
158 private boolean mInForeground;
Michael Kolb8233fac2010-10-26 16:08:53 -0700159 // If true, the tab is in page loading state (after onPageStarted,
160 // before onPageFinsihed)
161 private boolean mInPageLoad;
Tarun Nainani8eb00912014-07-17 12:28:32 -0700162 private boolean mPageFinished;
John Reck38b39652012-06-05 09:22:59 -0700163 private boolean mDisableOverrideUrlLoading;
Pankaj Garg79878492015-04-01 14:48:21 -0700164 private boolean mFirstVisualPixelPainted = false;
John Reck30c714c2010-12-16 17:30:34 -0800165 // The last reported progress of the current page
166 private int mPageLoadProgress;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000167 // The time the load started, used to find load page time
168 private long mLoadStartTime;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700169 // Application identifier used to find tabs that another application wants
170 // to reuse.
171 private String mAppId;
Michael Kolbe28b3472011-08-04 16:54:31 -0700172 // flag to indicate if tab should be closed on back
173 private boolean mCloseOnBack;
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700174 // flag to indicate if the tab was opened from an intent
175 private boolean mDerivedFromIntent = false;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -0500176 // The listener that gets invoked when a download is started from the
177 // mMainView
Selim Gurun0b3d66f2012-08-29 13:08:13 -0700178 private final BrowserDownloadListener mDownloadListener;
John Recke969cc52010-12-21 17:24:43 -0800179 private DataController mDataController;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700180
181 // AsyncTask for downloading touch icons
182 DownloadTouchIcon mTouchIconLoader;
183
John Reck35e9dd62011-04-25 09:01:54 -0700184 private BrowserSettings mSettings;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700185 private int mCaptureWidth;
186 private int mCaptureHeight;
187 private Bitmap mCapture;
188 private Handler mHandler;
Michael Kolb72864272012-05-03 15:42:15 -0700189 private boolean mUpdateThumbnail;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800190 private Timestamp timestamp;
Vivek Sekharf96064b2014-07-28 16:32:34 -0700191 private boolean mFullScreen = false;
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800192 private boolean mReceivedError;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700193
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -0700194 // determine if webview is destroyed to MemoryMonitor
195 private boolean mWebViewDestroyedByMemoryMonitor;
196
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700197 private Observable mFirstPixelObservable;
198 private Observable mTabHistoryUpdateObservable;
199
200 Observable getFirstPixelObservable() {
201 return mFirstPixelObservable;
202 }
203
204 Observable getTabHistoryUpdateObservable() {
205 return mTabHistoryUpdateObservable;
206 }
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -0700207
Mathew Inwood1dd8e822011-08-03 14:34:29 +0100208
John Reck1cf4b792011-07-26 10:22:22 -0700209 private static synchronized Bitmap getDefaultFavicon(Context context) {
210 if (sDefaultFavicon == null) {
211 sDefaultFavicon = BitmapFactory.decodeResource(
Enrico Rosd6efa972014-12-02 19:49:59 -0800212 context.getResources(), R.drawable.ic_deco_favicon_normal);
John Reck1cf4b792011-07-26 10:22:22 -0700213 }
214 return sDefaultFavicon;
215 }
Michael Kolbeb95db42011-03-03 10:38:40 -0800216
John Reck30c714c2010-12-16 17:30:34 -0800217 // All the state needed for a page
John Reckd8c74522011-06-14 08:45:00 -0700218 protected static class PageState {
John Reck30c714c2010-12-16 17:30:34 -0800219 String mUrl;
John Reckdb22ec42011-06-29 11:31:24 -0700220 String mOriginalUrl;
John Reck30c714c2010-12-16 17:30:34 -0800221 String mTitle;
Steve Block2466eff2011-10-03 15:33:09 +0100222 SecurityState mSecurityState;
Steve Block08a6f0c2011-10-06 12:12:53 +0100223 // This is non-null only when mSecurityState is SECURITY_STATE_BAD_CERTIFICATE.
224 SslError mSslCertificateError;
John Reck30c714c2010-12-16 17:30:34 -0800225 Bitmap mFavicon;
Steve Block08a6f0c2011-10-06 12:12:53 +0100226 boolean mIsBookmarkedSite;
227 boolean mIncognito;
John Reck30c714c2010-12-16 17:30:34 -0800228
229 PageState(Context c, boolean incognito) {
John Reck502a3532011-08-16 14:21:46 -0700230 mIncognito = incognito;
231 if (mIncognito) {
Vivek Sekhared791da2015-02-22 12:39:05 -0800232 mOriginalUrl = mUrl = "chrome://incognito";
John Reck30c714c2010-12-16 17:30:34 -0800233 mTitle = c.getString(R.string.new_incognito_tab);
John Reck30c714c2010-12-16 17:30:34 -0800234 } else {
John Reckdb22ec42011-06-29 11:31:24 -0700235 mOriginalUrl = mUrl = "";
John Reck30c714c2010-12-16 17:30:34 -0800236 mTitle = c.getString(R.string.new_tab);
John Reck30c714c2010-12-16 17:30:34 -0800237 }
Steve Block2466eff2011-10-03 15:33:09 +0100238 mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
John Reck30c714c2010-12-16 17:30:34 -0800239 }
240
241 PageState(Context c, boolean incognito, String url, Bitmap favicon) {
John Reck502a3532011-08-16 14:21:46 -0700242 mIncognito = incognito;
John Reckdb22ec42011-06-29 11:31:24 -0700243 mOriginalUrl = mUrl = url;
John Reck30c714c2010-12-16 17:30:34 -0800244 if (URLUtil.isHttpsUrl(url)) {
Steve Block2466eff2011-10-03 15:33:09 +0100245 mSecurityState = SecurityState.SECURITY_STATE_SECURE;
John Reck30c714c2010-12-16 17:30:34 -0800246 } else {
Steve Block2466eff2011-10-03 15:33:09 +0100247 mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
John Reck30c714c2010-12-16 17:30:34 -0800248 }
John Reck1cf4b792011-07-26 10:22:22 -0700249 mFavicon = favicon;
John Reck30c714c2010-12-16 17:30:34 -0800250 }
John Reck1cf4b792011-07-26 10:22:22 -0700251
Grace Kloba22ac16e2009-10-07 18:00:23 -0700252 }
253
John Reck30c714c2010-12-16 17:30:34 -0800254 // The current/loading page's state
John Reckd8c74522011-06-14 08:45:00 -0700255 protected PageState mCurrentState;
John Reck30c714c2010-12-16 17:30:34 -0800256
Grace Kloba22ac16e2009-10-07 18:00:23 -0700257 // Used for saving and restoring each Tab
Michael Kolbc831b632011-05-11 09:30:34 -0700258 static final String ID = "ID";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700259 static final String CURRURL = "currentUrl";
260 static final String CURRTITLE = "currentTitle";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700261 static final String PARENTTAB = "parentTab";
262 static final String APPID = "appid";
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700263 static final String INCOGNITO = "privateBrowsingEnabled";
John Reckb0a86db2011-05-24 14:05:58 -0700264 static final String USERAGENT = "useragent";
Michael Kolbe28b3472011-08-04 16:54:31 -0700265 static final String CLOSEFLAG = "closeOnBack";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700266
Pankaj Garg18186a92015-03-31 14:59:33 -0700267 public void setNetworkAvailable(boolean networkUp) {
268 if (networkUp && mReceivedError && (mMainView != null)) {
269 mMainView.reload();
270 }
271 }
272
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700273 public boolean isFirstVisualPixelPainted() {
274 return mFirstVisualPixelPainted;
275 }
276
277 public int getCaptureIndex(int navIndex) {
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700278 int orientation = mWebViewController.getActivity().
279 getResources().getConfiguration().orientation;
280
281 int orientationBit = (orientation == Configuration.ORIENTATION_LANDSCAPE) ? 0 : 1;
282
Vivek Sekhard0f60402015-06-05 14:07:11 -0700283 int index = orientationBit << 31 | (((int)mId & 0x7f) << 24) | (navIndex & 0xffffff);
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700284 return index;
285 }
286
287 public int getTabIdxFromCaptureIdx(int index) {
288 return (index & 0x7f000000) >> 24;
289 }
290
291 public int getOrientationFromCaptureIdx(int index) {
292 return ((index & 0x80000000) == 0) ? Configuration.ORIENTATION_LANDSCAPE :
293 Configuration.ORIENTATION_PORTRAIT;
294
295 }
296
297 public int getNavIdxFromCaptureIdx(int index) {
298 return (index & 0xffffff);
299 }
300
Grace Kloba22ac16e2009-10-07 18:00:23 -0700301 // -------------------------------------------------------------------------
302 // WebViewClient implementation for the main WebView
303 // -------------------------------------------------------------------------
304
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800305 private final WebViewClient mWebViewClient = new WebViewClient() {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500306 private Message mDontResend;
307 private Message mResend;
Michael Kolb47bd1e42011-09-01 15:25:00 -0700308
309 private boolean providersDiffer(String url, String otherUrl) {
310 Uri uri1 = Uri.parse(url);
311 Uri uri2 = Uri.parse(otherUrl);
312 return !uri1.getEncodedAuthority().equals(uri2.getEncodedAuthority());
313 }
314
Grace Kloba22ac16e2009-10-07 18:00:23 -0700315 @Override
316 public void onPageStarted(WebView view, String url, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700317 mInPageLoad = true;
Tarun Nainani8eb00912014-07-17 12:28:32 -0700318 mPageFinished = false;
Pankaj Garg79878492015-04-01 14:48:21 -0700319 mFirstVisualPixelPainted = false;
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700320 mFirstPixelObservable.set(false);
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800321 mReceivedError = false;
Michael Kolb72864272012-05-03 15:42:15 -0700322 mUpdateThumbnail = true;
Michael Kolba53c9892011-10-05 13:31:40 -0700323 mPageLoadProgress = INITIAL_PROGRESS;
Michael Kolb14612442011-06-24 13:06:29 -0700324 mCurrentState = new PageState(mContext,
John Reck30c714c2010-12-16 17:30:34 -0800325 view.isPrivateBrowsingEnabled(), url, favicon);
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000326 mLoadStartTime = SystemClock.uptimeMillis();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700327
328 // If we start a touch icon load and then load a new page, we don't
329 // want to cancel the current touch icon loader. But, we do want to
330 // create a new one when the touch icon url is known.
331 if (mTouchIconLoader != null) {
332 mTouchIconLoader.mTab = null;
333 mTouchIconLoader = null;
334 }
335
Panos Thomasb298aad2014-10-22 12:24:21 -0700336 // Loading a new page voids any ongoing Geolocation permission
337 // requests.
338 if (mGeolocationPermissionsPrompt != null) {
339 mGeolocationPermissionsPrompt.dismiss();
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) {
639 if (isPrivateBrowsingEnabled()) {
640 return;
641 }
642
643 if (!mFirstVisualPixelPainted) {
644 return;
645 }
646
647 final int idx = view.copyBackForwardList().getCurrentIndex();
648 boolean bitmapExists = view.hasSnapshot(idx);
649
650 int progress = 100;
651 Controller controller = (Controller)mWebViewController;
652 UI ui = controller.getUi();
653 if (ui instanceof BaseUi) {
654 BaseUi baseUi = (BaseUi) ui;
655 TitleBar titleBar = baseUi.getTitleBar();
656 progress = titleBar.getProgressView().getProgressPercent();
657 }
658
659 if (bitmapExists && progress < 85) {
660 return;
661 }
662
663 int index = getCaptureIndex(view.getLastCommittedHistoryIndex());
664 view.captureSnapshot(index , null);
665 }
666
667 @Override
668 public void onHistoryItemCommit(WebView view, int index) {
669 mTabHistoryUpdateObservable.set(index);
670 int maxIdx = view.copyBackForwardList().getSize();
671 int[] ids = view.getSnapshotIds();
672 int currentTabIdx = mWebViewController.getTabControl().getCurrentPosition();
673 for (int id : ids) {
674 if (getTabIdxFromCaptureIdx(id) == currentTabIdx &&
675 getNavIdxFromCaptureIdx(id) >= maxIdx) {
676 view.deleteSnapshot(id);
677 }
678 }
679 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700680 };
681
John Reck1cf4b792011-07-26 10:22:22 -0700682 private void syncCurrentState(WebView view, String url) {
683 // Sync state (in case of stop/timeout)
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800684
685 if (mReceivedError) {
686 mCurrentState.mUrl = url;
687 mCurrentState.mOriginalUrl = url;
688 } else {
689 mCurrentState.mUrl = view.getUrl();
690 mCurrentState.mOriginalUrl = view.getOriginalUrl();
691 mCurrentState.mFavicon = view.getFavicon();
692 }
693
John Reck1cf4b792011-07-26 10:22:22 -0700694 if (mCurrentState.mUrl == null) {
John Reck8bcafc12011-08-29 16:43:02 -0700695 mCurrentState.mUrl = "";
John Reck1cf4b792011-07-26 10:22:22 -0700696 }
John Reck1cf4b792011-07-26 10:22:22 -0700697 mCurrentState.mTitle = view.getTitle();
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800698
699
John Reck1cf4b792011-07-26 10:22:22 -0700700 if (!URLUtil.isHttpsUrl(mCurrentState.mUrl)) {
701 // In case we stop when loading an HTTPS page from an HTTP page
702 // but before a provisional load occurred
Steve Block2466eff2011-10-03 15:33:09 +0100703 mCurrentState.mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
Steve Block08a6f0c2011-10-06 12:12:53 +0100704 mCurrentState.mSslCertificateError = null;
John Reck1cf4b792011-07-26 10:22:22 -0700705 }
John Reck502a3532011-08-16 14:21:46 -0700706 mCurrentState.mIncognito = view.isPrivateBrowsingEnabled();
John Reck1cf4b792011-07-26 10:22:22 -0700707 }
708
Tarun Nainani8eb00912014-07-17 12:28:32 -0700709
710 public boolean isTabFullScreen() {
711 return mFullScreen;
712 }
713
Vivek Sekharf96064b2014-07-28 16:32:34 -0700714 protected void setTabFullscreen(boolean fullScreen) {
Tarun Nainani8eb00912014-07-17 12:28:32 -0700715 Controller controller = (Controller)mWebViewController;
Sudheer Koganti24766882014-10-02 10:58:09 -0700716 controller.getUi().showFullscreen(fullScreen);
Tarun Nainani8eb00912014-07-17 12:28:32 -0700717 mFullScreen = fullScreen;
Vivek Sekharf96064b2014-07-28 16:32:34 -0700718 }
719
Sudheer Koganti24766882014-10-02 10:58:09 -0700720 public boolean exitFullscreen() {
721 if (mFullScreen) {
722 Controller controller = (Controller)mWebViewController;
723 controller.getUi().showFullscreen(false);
724 if (getWebView() != null)
725 getWebView().exitFullscreen();
726 mFullScreen = false;
727 return true;
728 }
729 return false;
730 }
731
732
733
734
Grace Kloba22ac16e2009-10-07 18:00:23 -0700735 // -------------------------------------------------------------------------
736 // WebChromeClient implementation for the main WebView
737 // -------------------------------------------------------------------------
738
739 private final WebChromeClient mWebChromeClient = new WebChromeClient() {
740 // Helper method to create a new tab or sub window.
741 private void createWindow(final boolean dialog, final Message msg) {
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700742 this.createWindow(dialog, msg, null, false);
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700743 }
744
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700745 private void createWindow(final boolean dialog, final Message msg, final String url,
746 final boolean opener_suppressed) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700747 WebView.WebViewTransport transport =
748 (WebView.WebViewTransport) msg.obj;
749 if (dialog) {
750 createSubWindow();
Michael Kolb8233fac2010-10-26 16:08:53 -0700751 mWebViewController.attachSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700752 transport.setWebView(mSubView);
753 } else {
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700754 final Tab newTab = mWebViewController.openTab(url,
John Reck5949c662011-05-27 09:52:29 -0700755 Tab.this, true, true);
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700756 // This is special case for rendering links on a webpage in
757 // a new tab. If opener is suppressed, the WebContents created
758 // by the content layer are not fully initialized. This check
759 // will prevent content layer from overriding WebContents
760 // created by new tab with the uninitialized instance.
761 if (!opener_suppressed) {
762 transport.setWebView(newTab.getWebView());
763 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700764 }
765 msg.sendToTarget();
766 }
767
768 @Override
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700769 public void toggleFullscreenModeForTab(boolean enterFullscreen) {
770 if (mWebViewController instanceof Controller) {
Vivek Sekharf96064b2014-07-28 16:32:34 -0700771 setTabFullscreen(enterFullscreen);
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700772 }
773 }
774
775 @Override
Tarun Nainani8eb00912014-07-17 12:28:32 -0700776 public void onOffsetsForFullscreenChanged(float topControlsOffsetYPix,
777 float contentOffsetYPix,
778 float overdrawBottomHeightPix) {
779 if (mWebViewController instanceof Controller) {
Vivek Sekhar8ee3abb2014-07-14 12:32:05 -0700780 Controller controller = (Controller)mWebViewController;
Tarun Nainani8eb00912014-07-17 12:28:32 -0700781 controller.getUi().translateTitleBar(topControlsOffsetYPix);
Vivek Sekhar8ee3abb2014-07-14 12:32:05 -0700782 }
783 }
784
785 @Override
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700786 public boolean isTabFullScreen() {
Vivek Sekharf96064b2014-07-28 16:32:34 -0700787 return mFullScreen;
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700788 }
789
790 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700791 public boolean onCreateWindow(WebView view, final boolean dialog,
792 final boolean userGesture, final Message resultMsg) {
793 // only allow new window or sub window for the foreground case
794 if (!mInForeground) {
795 return false;
796 }
797 // Short-circuit if we can't create any more tabs or sub windows.
798 if (dialog && mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -0700799 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700800 .setTitle(R.string.too_many_subwindows_dialog_title)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200801 .setIconAttribute(android.R.attr.alertDialogIcon)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700802 .setMessage(R.string.too_many_subwindows_dialog_message)
803 .setPositiveButton(R.string.ok, null)
804 .show();
805 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700806 } else if (!mWebViewController.getTabControl().canCreateNewTab()) {
Michael Kolb14612442011-06-24 13:06:29 -0700807 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700808 .setTitle(R.string.too_many_windows_dialog_title)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200809 .setIconAttribute(android.R.attr.alertDialogIcon)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700810 .setMessage(R.string.too_many_windows_dialog_message)
811 .setPositiveButton(R.string.ok, null)
812 .show();
813 return false;
814 }
815
816 // Short-circuit if this was a user gesture.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800817 if (userGesture || !mSettings.blockPopupWindows()) {
Tarun Nainani4f5137d2015-04-16 17:26:18 -0700818 WebView.WebViewTransport transport =
819 (WebView.WebViewTransport) resultMsg.obj;
820 CreateWindowParams windowParams = transport.getCreateWindowParams();
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700821 if (windowParams.mOpenerSuppressed) {
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700822 createWindow(dialog, resultMsg, windowParams.mURL, true);
823 // This is special case for rendering links on a webpage in
824 // a new tab. If opener is suppressed, the WebContents created
825 // by the content layer are not fully initialized. Returning false
826 // will prevent content layer from overriding WebContents
827 // created by new tab with the uninitialized instance.
828 return false;
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700829 }
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700830
831 createWindow(dialog, resultMsg);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700832 return true;
833 }
834
Tarun Nainani4f5137d2015-04-16 17:26:18 -0700835 createWindow(dialog, resultMsg);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700836 return true;
837 }
838
839 @Override
Patrick Scotteb5061b2009-11-18 15:00:30 -0500840 public void onRequestFocus(WebView view) {
841 if (!mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700842 mWebViewController.switchToTab(Tab.this);
Patrick Scotteb5061b2009-11-18 15:00:30 -0500843 }
844 }
845
846 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700847 public void onCloseWindow(WebView window) {
Michael Kolbc831b632011-05-11 09:30:34 -0700848 if (mParent != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700849 // JavaScript can only close popup window.
850 if (mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700851 mWebViewController.switchToTab(mParent);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700852 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700853 mWebViewController.closeTab(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700854 }
855 }
856
857 @Override
858 public void onProgressChanged(WebView view, int newProgress) {
John Reck30c714c2010-12-16 17:30:34 -0800859 mPageLoadProgress = newProgress;
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800860 if (newProgress == 100) {
861 mInPageLoad = false;
862 }
John Reck30c714c2010-12-16 17:30:34 -0800863 mWebViewController.onProgressChanged(Tab.this);
Michael Kolb72864272012-05-03 15:42:15 -0700864 if (mUpdateThumbnail && newProgress == 100) {
865 mUpdateThumbnail = false;
866 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700867 }
868
869 @Override
Leon Scroggins21d9b902010-03-11 09:33:11 -0500870 public void onReceivedTitle(WebView view, final String title) {
John Reck30c714c2010-12-16 17:30:34 -0800871 mCurrentState.mTitle = title;
Michael Kolb8233fac2010-10-26 16:08:53 -0700872 mWebViewController.onReceivedTitle(Tab.this, title);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700873 }
874
875 @Override
876 public void onReceivedIcon(WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -0800877 mCurrentState.mFavicon = icon;
Michael Kolb8233fac2010-10-26 16:08:53 -0700878 mWebViewController.onFavicon(Tab.this, view, icon);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700879 }
880
881 @Override
882 public void onReceivedTouchIconUrl(WebView view, String url,
883 boolean precomposed) {
Michael Kolb14612442011-06-24 13:06:29 -0700884 final ContentResolver cr = mContext.getContentResolver();
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400885 // Let precomposed icons take precedence over non-composed
886 // icons.
887 if (precomposed && mTouchIconLoader != null) {
888 mTouchIconLoader.cancel(false);
889 mTouchIconLoader = null;
890 }
891 // Have only one async task at a time.
892 if (mTouchIconLoader == null) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700893 mTouchIconLoader = new DownloadTouchIcon(Tab.this,
Michael Kolb14612442011-06-24 13:06:29 -0700894 mContext, cr, view);
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400895 mTouchIconLoader.execute(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700896 }
897 }
898
899 @Override
900 public void onShowCustomView(View view,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800901 CustomViewCallback callback) {
Michael Kolb14612442011-06-24 13:06:29 -0700902 Activity activity = mWebViewController.getActivity();
903 if (activity != null) {
904 onShowCustomView(view, activity.getRequestedOrientation(), callback);
905 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400906 }
907
908 @Override
909 public void onShowCustomView(View view, int requestedOrientation,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800910 CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700911 if (mInForeground) mWebViewController.showCustomView(Tab.this, view,
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400912 requestedOrientation, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700913 }
914
915 @Override
916 public void onHideCustomView() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700917 if (mInForeground) mWebViewController.hideCustomView();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700918 }
919
920 /**
921 * The origin has exceeded its database quota.
922 * @param url the URL that exceeded the quota
923 * @param databaseIdentifier the identifier of the database on which the
924 * transaction that caused the quota overflow was run
925 * @param currentQuota the current quota for the origin.
926 * @param estimatedSize the estimated size of the database.
927 * @param totalUsedQuota is the sum of all origins' quota.
928 * @param quotaUpdater The callback to run when a decision to allow or
929 * deny quota has been made. Don't forget to call this!
930 */
931 @Override
932 public void onExceededDatabaseQuota(String url,
933 String databaseIdentifier, long currentQuota, long estimatedSize,
934 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -0700935 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -0700936 .onExceededDatabaseQuota(url, databaseIdentifier,
937 currentQuota, estimatedSize, totalUsedQuota,
938 quotaUpdater);
939 }
940
941 /**
942 * The Application Cache has exceeded its max size.
943 * @param spaceNeeded is the amount of disk space that would be needed
944 * in order for the last appcache operation to succeed.
945 * @param totalUsedQuota is the sum of all origins' quota.
946 * @param quotaUpdater A callback to inform the WebCore thread that a
947 * new app cache size is available. This callback must always
948 * be executed at some point to ensure that the sleeping
949 * WebCore thread is woken up.
950 */
951 @Override
952 public void onReachedMaxAppCacheSize(long spaceNeeded,
953 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -0700954 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -0700955 .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota,
956 quotaUpdater);
957 }
958
959 /**
960 * Instructs the browser to show a prompt to ask the user to set the
961 * Geolocation permission state for the specified origin.
962 * @param origin The origin for which Geolocation permissions are
963 * requested.
964 * @param callback The callback to call once the user has set the
965 * Geolocation permission state.
966 */
967 @Override
968 public void onGeolocationPermissionsShowPrompt(String origin,
969 GeolocationPermissions.Callback callback) {
970 if (mInForeground) {
Grace Kloba50c241e2010-04-20 11:07:50 -0700971 getGeolocationPermissionsPrompt().show(origin, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700972 }
973 }
974
975 /**
976 * Instructs the browser to hide the Geolocation permissions prompt.
977 */
978 @Override
979 public void onGeolocationPermissionsHidePrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -0700980 if (mInForeground && mGeolocationPermissionsPrompt != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700981 mGeolocationPermissionsPrompt.hide();
982 }
983 }
984
Ben Murdoch65acc352009-11-19 18:16:04 +0000985 /* Adds a JavaScript error message to the system log and if the JS
986 * console is enabled in the about:debug options, to that console
987 * also.
Ben Murdochc42addf2010-01-28 15:19:59 +0000988 * @param consoleMessage the message object.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700989 */
990 @Override
Ben Murdochc42addf2010-01-28 15:19:59 +0000991 public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Jeff Hamilton47654f42010-09-07 09:57:51 -0500992 // Don't log console messages in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -0700993 if (isPrivateBrowsingEnabled()) return true;
Jeff Hamilton47654f42010-09-07 09:57:51 -0500994
Ben Murdochc42addf2010-01-28 15:19:59 +0000995 String message = "Console: " + consoleMessage.message() + " "
996 + consoleMessage.sourceId() + ":"
997 + consoleMessage.lineNumber();
998
999 switch (consoleMessage.messageLevel()) {
1000 case TIP:
1001 Log.v(CONSOLE_LOGTAG, message);
1002 break;
1003 case LOG:
1004 Log.i(CONSOLE_LOGTAG, message);
1005 break;
1006 case WARNING:
1007 Log.w(CONSOLE_LOGTAG, message);
1008 break;
1009 case ERROR:
1010 Log.e(CONSOLE_LOGTAG, message);
1011 break;
1012 case DEBUG:
1013 Log.d(CONSOLE_LOGTAG, message);
1014 break;
1015 }
1016
1017 return true;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001018 }
1019
1020 /**
1021 * Ask the browser for an icon to represent a <video> element.
1022 * This icon will be used if the Web page did not specify a poster attribute.
1023 * @return Bitmap The icon or null if no such icon is available.
1024 */
1025 @Override
1026 public Bitmap getDefaultVideoPoster() {
1027 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001028 return mWebViewController.getDefaultVideoPoster();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001029 }
1030 return null;
1031 }
1032
1033 /**
1034 * Ask the host application for a custom progress view to show while
1035 * a <video> is loading.
1036 * @return View The progress view.
1037 */
1038 @Override
1039 public View getVideoLoadingProgressView() {
1040 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001041 return mWebViewController.getVideoLoadingProgressView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001042 }
1043 return null;
1044 }
1045
1046 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00001047 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001048 if (mInForeground) {
Ben Murdoch8cad4132012-01-11 10:56:43 +00001049 mWebViewController.openFileChooser(uploadMsg, acceptType, capture);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001050 } else {
1051 uploadMsg.onReceiveValue(null);
1052 }
1053 }
1054
Vivek Sekharb54614f2014-05-01 19:03:37 -07001055 @Override
1056 public void showFileChooser(ValueCallback<String[]> uploadFilePaths, String acceptTypes,
1057 boolean capture) {
1058 if (mInForeground) {
1059 mWebViewController.showFileChooser(uploadFilePaths, acceptTypes, capture);
1060 } else {
1061 uploadFilePaths.onReceiveValue(null);
1062 }
1063 }
1064
Grace Kloba22ac16e2009-10-07 18:00:23 -07001065 /**
1066 * Deliver a list of already-visited URLs
1067 */
1068 @Override
1069 public void getVisitedHistory(final ValueCallback<String[]> callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001070 mWebViewController.getVisitedHistory(callback);
1071 }
Ben Murdoch8029a772010-11-16 11:58:21 +00001072
1073 @Override
1074 public void setupAutoFill(Message message) {
1075 // Prompt the user to set up their profile.
1076 final Message msg = message;
Michael Kolb14612442011-06-24 13:06:29 -07001077 AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
1078 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
Ben Murdoch1d676b62011-01-17 12:54:24 +00001079 Context.LAYOUT_INFLATER_SERVICE);
1080 final View layout = inflater.inflate(R.layout.setup_autofill_dialog, null);
1081
1082 builder.setView(layout)
1083 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1084 @Override
1085 public void onClick(DialogInterface dialog, int id) {
1086 CheckBox disableAutoFill = (CheckBox) layout.findViewById(
1087 R.id.setup_autofill_dialog_disable_autofill);
1088
1089 if (disableAutoFill.isChecked()) {
1090 // Disable autofill and show a toast with how to turn it on again.
John Reck35e9dd62011-04-25 09:01:54 -07001091 mSettings.setAutofillEnabled(false);
Michael Kolb14612442011-06-24 13:06:29 -07001092 Toast.makeText(mContext,
Ben Murdoch1d676b62011-01-17 12:54:24 +00001093 R.string.autofill_setup_dialog_negative_toast,
1094 Toast.LENGTH_LONG).show();
1095 } else {
1096 // Take user to the AutoFill profile editor. When they return,
1097 // we will send the message that we pass here which will trigger
1098 // the form to get filled out with their new profile.
1099 mWebViewController.setupAutoFill(msg);
1100 }
1101 }
1102 })
1103 .setNegativeButton(R.string.cancel, null)
1104 .show();
Ben Murdoch8029a772010-11-16 11:58:21 +00001105 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001106 };
1107
1108 // -------------------------------------------------------------------------
1109 // WebViewClient implementation for the sub window
1110 // -------------------------------------------------------------------------
1111
1112 // Subclass of WebViewClient used in subwindows to notify the main
1113 // WebViewClient of certain WebView activities.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001114 private static class SubWindowClient extends WebViewClient {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001115 // The main WebViewClient.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001116 private final WebViewClient mClient;
Michael Kolb8233fac2010-10-26 16:08:53 -07001117 private final WebViewController mController;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001118
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001119 SubWindowClient(WebViewClient client, WebViewController controller) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001120 mClient = client;
Michael Kolb8233fac2010-10-26 16:08:53 -07001121 mController = controller;
Leon Scroggins III211ba542010-04-19 13:21:13 -04001122 }
1123 @Override
1124 public void onPageStarted(WebView view, String url, Bitmap favicon) {
1125 // Unlike the others, do not call mClient's version, which would
1126 // change the progress bar. However, we do want to remove the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001127 // find or select dialog.
Michael Kolb8233fac2010-10-26 16:08:53 -07001128 mController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001129 }
1130 @Override
1131 public void doUpdateVisitedHistory(WebView view, String url,
1132 boolean isReload) {
1133 mClient.doUpdateVisitedHistory(view, url, isReload);
1134 }
1135 @Override
1136 public boolean shouldOverrideUrlLoading(WebView view, String url) {
1137 return mClient.shouldOverrideUrlLoading(view, url);
1138 }
1139 @Override
1140 public void onReceivedSslError(WebView view, SslErrorHandler handler,
1141 SslError error) {
1142 mClient.onReceivedSslError(view, handler, error);
1143 }
1144 @Override
Brian Carlstrom8862c1d2011-06-02 01:05:55 -07001145 public void onReceivedClientCertRequest(WebView view,
1146 ClientCertRequestHandler handler, String host_and_port) {
1147 mClient.onReceivedClientCertRequest(view, handler, host_and_port);
1148 }
1149 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -07001150 public void onReceivedHttpAuthRequest(WebView view,
1151 HttpAuthHandler handler, String host, String realm) {
1152 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
1153 }
1154 @Override
1155 public void onFormResubmission(WebView view, Message dontResend,
1156 Message resend) {
1157 mClient.onFormResubmission(view, dontResend, resend);
1158 }
1159 @Override
1160 public void onReceivedError(WebView view, int errorCode,
1161 String description, String failingUrl) {
1162 mClient.onReceivedError(view, errorCode, description, failingUrl);
1163 }
1164 @Override
1165 public boolean shouldOverrideKeyEvent(WebView view,
1166 android.view.KeyEvent event) {
1167 return mClient.shouldOverrideKeyEvent(view, event);
1168 }
1169 @Override
1170 public void onUnhandledKeyEvent(WebView view,
1171 android.view.KeyEvent event) {
1172 mClient.onUnhandledKeyEvent(view, event);
1173 }
1174 }
1175
1176 // -------------------------------------------------------------------------
1177 // WebChromeClient implementation for the sub window
1178 // -------------------------------------------------------------------------
1179
1180 private class SubWindowChromeClient extends WebChromeClient {
1181 // The main WebChromeClient.
1182 private final WebChromeClient mClient;
1183
1184 SubWindowChromeClient(WebChromeClient client) {
1185 mClient = client;
1186 }
1187 @Override
1188 public void onProgressChanged(WebView view, int newProgress) {
1189 mClient.onProgressChanged(view, newProgress);
1190 }
1191 @Override
1192 public boolean onCreateWindow(WebView view, boolean dialog,
1193 boolean userGesture, android.os.Message resultMsg) {
1194 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
1195 }
1196 @Override
1197 public void onCloseWindow(WebView window) {
1198 if (window != mSubView) {
1199 Log.e(LOGTAG, "Can't close the window");
1200 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001201 mWebViewController.dismissSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001202 }
1203 }
1204
1205 // -------------------------------------------------------------------------
1206
1207 // Construct a new tab
Michael Kolb7bcafde2011-05-09 13:55:59 -07001208 Tab(WebViewController wvcontroller, WebView w) {
John Reck1cf4b792011-07-26 10:22:22 -07001209 this(wvcontroller, w, null);
1210 }
1211
1212 Tab(WebViewController wvcontroller, Bundle state) {
1213 this(wvcontroller, null, state);
1214 }
1215
1216 Tab(WebViewController wvcontroller, WebView w, Bundle state) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001217 mWebViewController = wvcontroller;
Michael Kolb14612442011-06-24 13:06:29 -07001218 mContext = mWebViewController.getContext();
John Reck35e9dd62011-04-25 09:01:54 -07001219 mSettings = BrowserSettings.getInstance();
Michael Kolb14612442011-06-24 13:06:29 -07001220 mDataController = DataController.getInstance(mContext);
1221 mCurrentState = new PageState(mContext, w != null
John Reck847b5322011-04-14 17:02:18 -07001222 ? w.isPrivateBrowsingEnabled() : false);
Tarun Nainani8084c822014-06-25 13:38:06 -07001223 setTimeStamp();
Michael Kolb8233fac2010-10-26 16:08:53 -07001224 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001225 mInForeground = false;
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001226 mWebViewDestroyedByMemoryMonitor = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001227
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001228 mDownloadListener = new BrowserDownloadListener() {
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001229 public void onDownloadStart(String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001230 String contentDisposition, String mimetype, String referer,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001231 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001232 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001233 mimetype, referer, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001234 }
1235 };
1236
John Reck1cf4b792011-07-26 10:22:22 -07001237 mCaptureWidth = mContext.getResources().getDimensionPixelSize(
1238 R.dimen.tab_thumbnail_width);
1239 mCaptureHeight = mContext.getResources().getDimensionPixelSize(
1240 R.dimen.tab_thumbnail_height);
1241 updateShouldCaptureThumbnails();
1242 restoreState(state);
John Reck52be4782011-08-26 15:37:29 -07001243 if (getId() == -1) {
1244 mId = TabControl.getNextId();
1245 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001246 setWebView(w);
Michael Kolb9ef259a2011-07-12 15:33:08 -07001247 mHandler = new Handler() {
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001248 @Override
Michael Kolb9ef259a2011-07-12 15:33:08 -07001249 public void handleMessage(Message m) {
John Reck1cf4b792011-07-26 10:22:22 -07001250 switch (m.what) {
1251 case MSG_CAPTURE:
1252 capture();
1253 break;
1254 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001255 }
1256 };
Pankaj Garg1c13cab2015-05-12 11:52:17 -07001257
1258 mFirstPixelObservable = new Observable();
1259 mFirstPixelObservable.set(false);
1260 mTabHistoryUpdateObservable = new Observable();
John Reck1cf4b792011-07-26 10:22:22 -07001261 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001262
Michael Kolb72864272012-05-03 15:42:15 -07001263 public boolean shouldUpdateThumbnail() {
1264 return mUpdateThumbnail;
1265 }
1266
Mathew Inwoode09305e2011-09-02 12:03:26 +01001267 /**
1268 * This is used to get a new ID when the tab has been preloaded, before it is displayed and
1269 * added to TabControl. Preloaded tabs can be created before restoreInstanceState, leading
1270 * to overlapping IDs between the preloaded and restored tabs.
1271 */
1272 public void refreshIdAfterPreload() {
1273 mId = TabControl.getNextId();
1274 }
1275
John Reck1cf4b792011-07-26 10:22:22 -07001276 public void updateShouldCaptureThumbnails() {
1277 if (mWebViewController.shouldCaptureThumbnails()) {
1278 synchronized (Tab.this) {
1279 if (mCapture == null) {
1280 mCapture = Bitmap.createBitmap(mCaptureWidth, mCaptureHeight,
1281 Bitmap.Config.RGB_565);
Michael Kolbc3af0672011-08-09 10:24:41 -07001282 mCapture.eraseColor(Color.WHITE);
John Reck1cf4b792011-07-26 10:22:22 -07001283 if (mInForeground) {
1284 postCapture();
1285 }
1286 }
1287 }
1288 } else {
1289 synchronized (Tab.this) {
1290 mCapture = null;
1291 deleteThumbnail();
1292 }
1293 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001294 }
1295
Michael Kolb14612442011-06-24 13:06:29 -07001296 public void setController(WebViewController ctl) {
1297 mWebViewController = ctl;
John Reck1cf4b792011-07-26 10:22:22 -07001298 updateShouldCaptureThumbnails();
Michael Kolb14612442011-06-24 13:06:29 -07001299 }
1300
Michael Kolbc831b632011-05-11 09:30:34 -07001301 public long getId() {
1302 return mId;
1303 }
1304
Michael Kolb91911a22012-01-17 11:21:25 -08001305 void setWebView(WebView w) {
1306 setWebView(w, true);
1307 }
1308
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001309 public boolean isNativeActive(){
1310 if (mMainView == null)
1311 return false;
1312 return true;
1313 }
1314
1315 public void setTimeStamp(){
1316 Date d = new Date();
1317 timestamp = (new Timestamp(d.getTime()));
1318 }
1319
1320 public Timestamp getTimestamp() {
1321 return timestamp;
1322 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001323 /**
1324 * Sets the WebView for this tab, correctly removing the old WebView from
1325 * the container view.
1326 */
Michael Kolb91911a22012-01-17 11:21:25 -08001327 void setWebView(WebView w, boolean restore) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001328 if (mMainView == w) {
1329 return;
1330 }
Michael Kolba713ec82010-11-29 17:27:06 -08001331
Grace Kloba22ac16e2009-10-07 18:00:23 -07001332 // If the WebView is changing, the page will be reloaded, so any ongoing
1333 // Geolocation permission requests are void.
Grace Kloba50c241e2010-04-20 11:07:50 -07001334 if (mGeolocationPermissionsPrompt != null) {
1335 mGeolocationPermissionsPrompt.hide();
1336 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001337
Michael Kolba713ec82010-11-29 17:27:06 -08001338 mWebViewController.onSetWebView(this, w);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001339
John Reck1cf4b792011-07-26 10:22:22 -07001340 if (mMainView != null) {
John Reck8ee633f2011-08-09 16:00:35 -07001341 mMainView.setPictureListener(null);
John Reck1cf4b792011-07-26 10:22:22 -07001342 if (w != null) {
1343 syncCurrentState(w, null);
1344 } else {
Panos Thomasa9a5a582014-03-18 19:20:08 -07001345 mCurrentState = new PageState(mContext, mMainView.isPrivateBrowsingEnabled());
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001346
1347 if (mWebViewDestroyedByMemoryMonitor) {
1348 /*
1349 * If tab was destroyed as a result of the MemoryMonitor
1350 * then we need to restore the state properties
1351 * from the old WebView (mMainView)
1352 */
1353 syncCurrentState(mMainView, null);
1354 mWebViewDestroyedByMemoryMonitor = false;
1355 }
John Reck1cf4b792011-07-26 10:22:22 -07001356 }
1357 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001358 // set the new one
1359 mMainView = w;
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001360
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001361 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001362 if (mMainView != null) {
1363 mMainView.setWebViewClient(mWebViewClient);
1364 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001365 // Attach DownloadManager so that downloads can start in an active
1366 // or a non-active window. This can happen when going to a site that
1367 // does a redirect after a period of time. The user could have
1368 // switched to another tab while waiting for the download to start.
1369 mMainView.setDownloadListener(mDownloadListener);
John Reck8ee633f2011-08-09 16:00:35 -07001370 TabControl tc = mWebViewController.getTabControl();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001371 if (tc != null /*&& tc.getOnThumbnailUpdatedListener() != null*/) {
John Reck8ee633f2011-08-09 16:00:35 -07001372 mMainView.setPictureListener(this);
1373 }
Michael Kolb91911a22012-01-17 11:21:25 -08001374 if (restore && (mSavedState != null)) {
John Reck8b9bb8b2012-03-08 13:19:40 -08001375 restoreUserAgent();
John Reck6c2e2f32011-08-22 13:41:23 -07001376 WebBackForwardList restoredState
1377 = mMainView.restoreState(mSavedState);
1378 if (restoredState == null || restoredState.getSize() == 0) {
1379 Log.w(LOGTAG, "Failed to restore WebView state!");
1380 loadUrl(mCurrentState.mOriginalUrl, null);
1381 }
John Reck1cf4b792011-07-26 10:22:22 -07001382 mSavedState = null;
1383 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001384 }
1385 }
1386
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001387 public void destroyThroughMemoryMonitor() {
1388 mWebViewDestroyedByMemoryMonitor = true;
1389 destroy();
1390 }
1391
Grace Kloba22ac16e2009-10-07 18:00:23 -07001392 /**
1393 * Destroy the tab's main WebView and subWindow if any
1394 */
1395 void destroy() {
Tarun Nainani2c1dd7c2014-07-05 16:40:12 -07001396
Matthew Huib7f2e9c2014-04-16 11:12:37 -04001397 if (mPostponeDestroy) {
1398 mShouldDestroy = true;
1399 return;
1400 }
1401 mShouldDestroy = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001402 if (mMainView != null) {
1403 dismissSubWindow();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001404 // save the WebView to call destroy() after detach it from the tab
1405 WebView webView = mMainView;
Pankaj Garg1c13cab2015-05-12 11:52:17 -07001406 if (!mWebViewDestroyedByMemoryMonitor) {
1407 int[] ids = webView.getSnapshotIds();
1408 int currentTabIdx = mWebViewController.getTabControl().getCurrentPosition();
1409 for (int id : ids) {
1410 if (getTabIdxFromCaptureIdx(id) == currentTabIdx) {
1411 webView.deleteSnapshot(id);
1412 }
1413 }
1414 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001415 setWebView(null);
1416 webView.destroy();
1417 }
1418 }
1419
Matthew Huib7f2e9c2014-04-16 11:12:37 -04001420 private boolean mPostponeDestroy = false;
1421 private boolean mShouldDestroy = false;
1422
1423 public void postponeDestroy() {
1424 mPostponeDestroy = true;
1425 }
1426
1427 public void performPostponedDestroy() {
1428 mPostponeDestroy = false;
1429 if (mShouldDestroy) {
1430 destroy();
1431 }
1432 }
1433
Grace Kloba22ac16e2009-10-07 18:00:23 -07001434 /**
1435 * Remove the tab from the parent
1436 */
1437 void removeFromTree() {
1438 // detach the children
Michael Kolbc831b632011-05-11 09:30:34 -07001439 if (mChildren != null) {
1440 for(Tab t : mChildren) {
1441 t.setParent(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001442 }
1443 }
1444 // remove itself from the parent list
Michael Kolbc831b632011-05-11 09:30:34 -07001445 if (mParent != null) {
1446 mParent.mChildren.remove(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001447 }
Tarun Nainani8eb00912014-07-17 12:28:32 -07001448
1449 mCapture = null;
John Reck1cf4b792011-07-26 10:22:22 -07001450 deleteThumbnail();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001451 }
1452
1453 /**
1454 * Create a new subwindow unless a subwindow already exists.
1455 * @return True if a new subwindow was created. False if one already exists.
1456 */
1457 boolean createSubWindow() {
1458 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001459 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001460 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001461 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001462 mSubView.setWebChromeClient(new SubWindowChromeClient(
1463 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001464 // Set a different DownloadListener for the mSubView, since it will
1465 // just need to dismiss the mSubView, rather than close the Tab
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001466 mSubView.setDownloadListener(new BrowserDownloadListener() {
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001467 public void onDownloadStart(String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001468 String contentDisposition, String mimetype, String referer,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001469 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001470 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001471 contentDisposition, mimetype, referer, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001472 if (mSubView.copyBackForwardList().getSize() == 0) {
1473 // This subwindow was opened for the sole purpose of
1474 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001475 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001476 }
1477 }
1478 });
Michael Kolb14612442011-06-24 13:06:29 -07001479 mSubView.setOnCreateContextMenuListener(mWebViewController.getActivity());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001480 return true;
1481 }
1482 return false;
1483 }
1484
1485 /**
1486 * Dismiss the subWindow for the tab.
1487 */
1488 void dismissSubWindow() {
1489 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001490 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001491 mSubView.destroy();
1492 mSubView = null;
1493 mSubViewContainer = null;
1494 }
1495 }
1496
Grace Kloba22ac16e2009-10-07 18:00:23 -07001497
1498 /**
1499 * Set the parent tab of this tab.
1500 */
Michael Kolbc831b632011-05-11 09:30:34 -07001501 void setParent(Tab parent) {
John Reck52be4782011-08-26 15:37:29 -07001502 if (parent == this) {
1503 throw new IllegalStateException("Cannot set parent to self!");
1504 }
Michael Kolbc831b632011-05-11 09:30:34 -07001505 mParent = parent;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001506 // This tab may have been freed due to low memory. If that is the case,
Michael Kolbc831b632011-05-11 09:30:34 -07001507 // the parent tab id is already saved. If we are changing that id
Grace Kloba22ac16e2009-10-07 18:00:23 -07001508 // (most likely due to removing the parent tab) we must update the
Michael Kolbc831b632011-05-11 09:30:34 -07001509 // parent tab id in the saved Bundle.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001510 if (mSavedState != null) {
1511 if (parent == null) {
1512 mSavedState.remove(PARENTTAB);
1513 } else {
Michael Kolbc831b632011-05-11 09:30:34 -07001514 mSavedState.putLong(PARENTTAB, parent.getId());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001515 }
1516 }
John Reckb0a86db2011-05-24 14:05:58 -07001517
1518 // Sync the WebView useragent with the parent
1519 if (parent != null && mSettings.hasDesktopUseragent(parent.getWebView())
1520 != mSettings.hasDesktopUseragent(getWebView())) {
1521 mSettings.toggleDesktopUseragent(getWebView());
1522 }
John Reck52be4782011-08-26 15:37:29 -07001523
1524 if (parent != null && parent.getId() == getId()) {
1525 throw new IllegalStateException("Parent has same ID as child!");
1526 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001527 }
1528
1529 /**
Michael Kolbc831b632011-05-11 09:30:34 -07001530 * If this Tab was created through another Tab, then this method returns
1531 * that Tab.
1532 * @return the Tab parent or null
1533 */
1534 public Tab getParent() {
1535 return mParent;
1536 }
1537
1538 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001539 * When a Tab is created through the content of another Tab, then we
1540 * associate the Tabs.
1541 * @param child the Tab that was created from this Tab
1542 */
1543 void addChildTab(Tab child) {
Michael Kolbc831b632011-05-11 09:30:34 -07001544 if (mChildren == null) {
1545 mChildren = new Vector<Tab>();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001546 }
Michael Kolbc831b632011-05-11 09:30:34 -07001547 mChildren.add(child);
1548 child.setParent(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001549 }
1550
Michael Kolbc831b632011-05-11 09:30:34 -07001551 Vector<Tab> getChildren() {
1552 return mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001553 }
1554
1555 void resume() {
1556 if (mMainView != null) {
John Reck56c1fcf2011-08-17 10:15:16 -07001557 setupHwAcceleration(mMainView);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001558 mMainView.onResume();
1559 if (mSubView != null) {
1560 mSubView.onResume();
1561 }
1562 }
1563 }
1564
John Reck56c1fcf2011-08-17 10:15:16 -07001565 private void setupHwAcceleration(View web) {
1566 if (web == null) return;
1567 BrowserSettings settings = BrowserSettings.getInstance();
1568 if (settings.isHardwareAccelerated()) {
1569 web.setLayerType(View.LAYER_TYPE_NONE, null);
1570 } else {
1571 web.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
1572 }
1573 }
1574
Grace Kloba22ac16e2009-10-07 18:00:23 -07001575 void pause() {
1576 if (mMainView != null) {
1577 mMainView.onPause();
1578 if (mSubView != null) {
1579 mSubView.onPause();
1580 }
1581 }
1582 }
1583
1584 void putInForeground() {
John Reck8ee633f2011-08-09 16:00:35 -07001585 if (mInForeground) {
1586 return;
1587 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001588 mInForeground = true;
1589 resume();
Michael Kolb14612442011-06-24 13:06:29 -07001590 Activity activity = mWebViewController.getActivity();
1591 mMainView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001592 if (mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -07001593 mSubView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001594 }
Axesh R. Ajmerac6b5c322015-05-01 11:06:10 -07001595
Leon Scroggins1961ed22010-12-07 15:22:21 -05001596 mWebViewController.bookmarkedStatusHasChanged(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001597 }
1598
1599 void putInBackground() {
John Reck8ee633f2011-08-09 16:00:35 -07001600 if (!mInForeground) {
1601 return;
1602 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001603 mInForeground = false;
1604 pause();
1605 mMainView.setOnCreateContextMenuListener(null);
1606 if (mSubView != null) {
1607 mSubView.setOnCreateContextMenuListener(null);
1608 }
1609 }
1610
Michael Kolb8233fac2010-10-26 16:08:53 -07001611 boolean inForeground() {
1612 return mInForeground;
1613 }
1614
Grace Kloba22ac16e2009-10-07 18:00:23 -07001615 /**
1616 * Return the top window of this tab; either the subwindow if it is not
1617 * null or the main window.
1618 * @return The top window of this tab.
1619 */
1620 WebView getTopWindow() {
1621 if (mSubView != null) {
1622 return mSubView;
1623 }
1624 return mMainView;
1625 }
1626
1627 /**
1628 * Return the main window of this tab. Note: if a tab is freed in the
1629 * background, this can return null. It is only guaranteed to be
1630 * non-null for the current tab.
1631 * @return The main WebView of this tab.
1632 */
1633 WebView getWebView() {
1634 return mMainView;
1635 }
1636
Michael Kolba713ec82010-11-29 17:27:06 -08001637 void setViewContainer(View container) {
1638 mContainer = container;
1639 }
1640
Michael Kolb8233fac2010-10-26 16:08:53 -07001641 View getViewContainer() {
1642 return mContainer;
1643 }
1644
Grace Kloba22ac16e2009-10-07 18:00:23 -07001645 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001646 * Return whether private browsing is enabled for the main window of
1647 * this tab.
1648 * @return True if private browsing is enabled.
1649 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001650 boolean isPrivateBrowsingEnabled() {
John Reck502a3532011-08-16 14:21:46 -07001651 return mCurrentState.mIncognito;
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001652 }
1653
1654 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001655 * Return the subwindow of this tab or null if there is no subwindow.
1656 * @return The subwindow of this tab or null.
1657 */
1658 WebView getSubWebView() {
1659 return mSubView;
1660 }
1661
Michael Kolb1514bb72010-11-22 09:11:48 -08001662 void setSubWebView(WebView subView) {
1663 mSubView = subView;
1664 }
1665
Michael Kolb8233fac2010-10-26 16:08:53 -07001666 View getSubViewContainer() {
1667 return mSubViewContainer;
1668 }
1669
Michael Kolb1514bb72010-11-22 09:11:48 -08001670 void setSubViewContainer(View subViewContainer) {
1671 mSubViewContainer = subViewContainer;
1672 }
1673
Grace Kloba22ac16e2009-10-07 18:00:23 -07001674 /**
1675 * @return The geolocation permissions prompt for this tab.
1676 */
1677 GeolocationPermissionsPrompt getGeolocationPermissionsPrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001678 if (mGeolocationPermissionsPrompt == null) {
1679 ViewStub stub = (ViewStub) mContainer
1680 .findViewById(R.id.geolocation_permissions_prompt);
1681 mGeolocationPermissionsPrompt = (GeolocationPermissionsPrompt) stub
1682 .inflate();
Panos Thomasb298aad2014-10-22 12:24:21 -07001683 mGeolocationPermissionsPrompt.init(mCurrentState.mIncognito);
Grace Kloba50c241e2010-04-20 11:07:50 -07001684 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001685 return mGeolocationPermissionsPrompt;
1686 }
1687
1688 /**
1689 * @return The application id string
1690 */
1691 String getAppId() {
1692 return mAppId;
1693 }
1694
1695 /**
1696 * Set the application id string
1697 * @param id
1698 */
1699 void setAppId(String id) {
1700 mAppId = id;
1701 }
1702
Michael Kolbe28b3472011-08-04 16:54:31 -07001703 boolean closeOnBack() {
1704 return mCloseOnBack;
1705 }
1706
1707 void setCloseOnBack(boolean close) {
1708 mCloseOnBack = close;
1709 }
1710
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001711 boolean getDerivedFromIntent() {
1712 return mDerivedFromIntent;
1713 }
1714
1715 void setDerivedFromIntent(boolean derived) {
1716 mDerivedFromIntent = derived;
1717 }
1718
Grace Kloba22ac16e2009-10-07 18:00:23 -07001719 String getUrl() {
John Reck324d4402011-01-11 16:56:42 -08001720 return UrlUtils.filteredUrl(mCurrentState.mUrl);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001721 }
1722
Tarun Nainani8eb00912014-07-17 12:28:32 -07001723
1724 protected void onPageFinished() {
1725 mPageFinished = true;
1726 }
1727
1728 public boolean getPageFinishedStatus() {
1729 return mPageFinished;
1730 }
1731
John Reck49a603c2011-03-03 09:33:05 -08001732 String getOriginalUrl() {
Vivek Sekhar361065a2014-11-25 15:34:35 -08001733 if (mMainView != null)
1734 return mMainView.getOriginalUrl();
John Reckdb22ec42011-06-29 11:31:24 -07001735 if (mCurrentState.mOriginalUrl == null) {
1736 return getUrl();
John Reck49a603c2011-03-03 09:33:05 -08001737 }
John Reckdb22ec42011-06-29 11:31:24 -07001738 return UrlUtils.filteredUrl(mCurrentState.mOriginalUrl);
John Reck49a603c2011-03-03 09:33:05 -08001739 }
1740
Grace Kloba22ac16e2009-10-07 18:00:23 -07001741 /**
John Reck30c714c2010-12-16 17:30:34 -08001742 * Get the title of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001743 */
1744 String getTitle() {
John Reck30c714c2010-12-16 17:30:34 -08001745 return mCurrentState.mTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001746 }
1747
1748 /**
John Reck30c714c2010-12-16 17:30:34 -08001749 * Get the favicon of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001750 */
1751 Bitmap getFavicon() {
John Reck1cf4b792011-07-26 10:22:22 -07001752 if (mCurrentState.mFavicon != null) {
1753 return mCurrentState.mFavicon;
1754 }
1755 return getDefaultFavicon(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001756 }
1757
John Recke969cc52010-12-21 17:24:43 -08001758 public boolean isBookmarkedSite() {
1759 return mCurrentState.mIsBookmarkedSite;
1760 }
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001761
Grace Kloba22ac16e2009-10-07 18:00:23 -07001762 /**
Steve Block08a6f0c2011-10-06 12:12:53 +01001763 * Sets the security state, clears the SSL certificate error and informs
1764 * the controller.
1765 */
Steve Block2466eff2011-10-03 15:33:09 +01001766 private void setSecurityState(SecurityState securityState) {
1767 mCurrentState.mSecurityState = securityState;
Steve Block08a6f0c2011-10-06 12:12:53 +01001768 mCurrentState.mSslCertificateError = null;
Steve Block2466eff2011-10-03 15:33:09 +01001769 mWebViewController.onUpdatedSecurityState(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001770 }
1771
1772 /**
Steve Block2466eff2011-10-03 15:33:09 +01001773 * @return The tab's security state.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001774 */
Steve Block2466eff2011-10-03 15:33:09 +01001775 SecurityState getSecurityState() {
1776 return mCurrentState.mSecurityState;
John Reck30c714c2010-12-16 17:30:34 -08001777 }
1778
Steve Block08a6f0c2011-10-06 12:12:53 +01001779 /**
1780 * Gets the SSL certificate error, if any, for the page's main resource.
1781 * This is only non-null when the security state is
1782 * SECURITY_STATE_BAD_CERTIFICATE.
1783 */
1784 SslError getSslCertificateError() {
1785 return mCurrentState.mSslCertificateError;
1786 }
1787
John Reck30c714c2010-12-16 17:30:34 -08001788 int getLoadProgress() {
1789 if (mInPageLoad) {
1790 return mPageLoadProgress;
1791 }
1792 return 100;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001793 }
1794
1795 /**
1796 * @return TRUE if onPageStarted is called while onPageFinished is not
1797 * called yet.
1798 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001799 boolean inPageLoad() {
1800 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001801 }
1802
Grace Kloba22ac16e2009-10-07 18:00:23 -07001803 /**
John Reck1cf4b792011-07-26 10:22:22 -07001804 * @return The Bundle with the tab's state if it can be saved, otherwise null
Grace Kloba22ac16e2009-10-07 18:00:23 -07001805 */
John Reck1cf4b792011-07-26 10:22:22 -07001806 public Bundle saveState() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001807 // If the WebView is null it means we ran low on memory and we already
1808 // stored the saved state in mSavedState.
1809 if (mMainView == null) {
John Reck1cf4b792011-07-26 10:22:22 -07001810 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001811 }
John Reck6c2e2f32011-08-22 13:41:23 -07001812
1813 if (TextUtils.isEmpty(mCurrentState.mUrl)) {
John Reck1cf4b792011-07-26 10:22:22 -07001814 return null;
John Reck24f18262011-06-17 14:47:20 -07001815 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001816
1817 mSavedState = new Bundle();
John Reck6c2e2f32011-08-22 13:41:23 -07001818 WebBackForwardList savedList = mMainView.saveState(mSavedState);
1819 if (savedList == null || savedList.getSize() == 0) {
1820 Log.w(LOGTAG, "Failed to save back/forward list for "
1821 + mCurrentState.mUrl);
1822 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001823
Michael Kolbc831b632011-05-11 09:30:34 -07001824 mSavedState.putLong(ID, mId);
John Reck30c714c2010-12-16 17:30:34 -08001825 mSavedState.putString(CURRURL, mCurrentState.mUrl);
1826 mSavedState.putString(CURRTITLE, mCurrentState.mTitle);
John Reck1cf4b792011-07-26 10:22:22 -07001827 mSavedState.putBoolean(INCOGNITO, mMainView.isPrivateBrowsingEnabled());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001828 if (mAppId != null) {
1829 mSavedState.putString(APPID, mAppId);
1830 }
Michael Kolbe28b3472011-08-04 16:54:31 -07001831 mSavedState.putBoolean(CLOSEFLAG, mCloseOnBack);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001832 // Remember the parent tab so the relationship can be restored.
Michael Kolbc831b632011-05-11 09:30:34 -07001833 if (mParent != null) {
1834 mSavedState.putLong(PARENTTAB, mParent.mId);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001835 }
John Reckb0a86db2011-05-24 14:05:58 -07001836 mSavedState.putBoolean(USERAGENT,
1837 mSettings.hasDesktopUseragent(getWebView()));
John Reck1cf4b792011-07-26 10:22:22 -07001838 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001839 }
1840
1841 /*
1842 * Restore the state of the tab.
1843 */
John Reck1cf4b792011-07-26 10:22:22 -07001844 private void restoreState(Bundle b) {
1845 mSavedState = b;
1846 if (mSavedState == null) {
1847 return;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001848 }
1849 // Restore the internal state even if the WebView fails to restore.
1850 // This will maintain the app id, original url and close-on-exit values.
Michael Kolbc831b632011-05-11 09:30:34 -07001851 mId = b.getLong(ID);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001852 mAppId = b.getString(APPID);
Michael Kolbe28b3472011-08-04 16:54:31 -07001853 mCloseOnBack = b.getBoolean(CLOSEFLAG);
John Reck8b9bb8b2012-03-08 13:19:40 -08001854 restoreUserAgent();
John Reck1cf4b792011-07-26 10:22:22 -07001855 String url = b.getString(CURRURL);
1856 String title = b.getString(CURRTITLE);
1857 boolean incognito = b.getBoolean(INCOGNITO);
1858 mCurrentState = new PageState(mContext, incognito, url, null);
1859 mCurrentState.mTitle = title;
1860 synchronized (Tab.this) {
1861 if (mCapture != null) {
John Reck4eadc342011-10-31 14:04:10 -07001862 DataController.getInstance(mContext).loadThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07001863 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001864 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001865 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04001866
John Reck8b9bb8b2012-03-08 13:19:40 -08001867 private void restoreUserAgent() {
1868 if (mMainView == null || mSavedState == null) {
1869 return;
1870 }
1871 if (mSavedState.getBoolean(USERAGENT)
1872 != mSettings.hasDesktopUseragent(mMainView)) {
1873 mSettings.toggleDesktopUseragent(mMainView);
1874 }
1875 }
1876
Leon Scroggins1961ed22010-12-07 15:22:21 -05001877 public void updateBookmarkedStatus() {
John Recke969cc52010-12-21 17:24:43 -08001878 mDataController.queryBookmarkStatus(getUrl(), mIsBookmarkCallback);
Leon Scroggins1961ed22010-12-07 15:22:21 -05001879 }
1880
John Recke969cc52010-12-21 17:24:43 -08001881 private DataController.OnQueryUrlIsBookmark mIsBookmarkCallback
1882 = new DataController.OnQueryUrlIsBookmark() {
1883 @Override
1884 public void onQueryUrlIsBookmark(String url, boolean isBookmark) {
1885 if (mCurrentState.mUrl.equals(url)) {
1886 mCurrentState.mIsBookmarkedSite = isBookmark;
1887 mWebViewController.bookmarkedStatusHasChanged(Tab.this);
1888 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001889 }
John Recke969cc52010-12-21 17:24:43 -08001890 };
Michael Kolb1acef692011-03-08 14:12:06 -08001891
Michael Kolbeb95db42011-03-03 10:38:40 -08001892 public Bitmap getScreenshot() {
John Reck1cf4b792011-07-26 10:22:22 -07001893 synchronized (Tab.this) {
1894 return mCapture;
1895 }
Michael Kolbeb95db42011-03-03 10:38:40 -08001896 }
1897
John Reck541f55a2011-06-07 16:34:43 -07001898 public boolean isSnapshot() {
John Reck541f55a2011-06-07 16:34:43 -07001899 return false;
1900 }
1901
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001902 private static class SaveCallback implements ValueCallback<String> {
1903 boolean onReceiveValueCalled = false;
1904 private String mPath;
John Reck68234a92012-04-19 15:27:12 -07001905
1906 @Override
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001907 public void onReceiveValue(String path) {
1908 this.onReceiveValueCalled = true;
1909 this.mPath = path;
John Reck68234a92012-04-19 15:27:12 -07001910 synchronized (this) {
1911 notifyAll();
John Reck8cc92352011-07-06 17:41:52 -07001912 }
John Reck541f55a2011-06-07 16:34:43 -07001913 }
John Reck68234a92012-04-19 15:27:12 -07001914
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001915 public String getPath() {
1916 return mPath;
1917 }
John Reck68234a92012-04-19 15:27:12 -07001918 }
1919
1920 /**
1921 * Must be called on the UI thread
1922 */
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001923 public ContentValues createSnapshotValues(Bitmap bm) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001924 WebView web = getWebView();
John Reck68234a92012-04-19 15:27:12 -07001925 if (web == null) return null;
John Reckd8c74522011-06-14 08:45:00 -07001926 ContentValues values = new ContentValues();
1927 values.put(Snapshots.TITLE, mCurrentState.mTitle);
1928 values.put(Snapshots.URL, mCurrentState.mUrl);
John Reck68234a92012-04-19 15:27:12 -07001929 values.put(Snapshots.BACKGROUND, web.getPageBackgroundColor());
John Reck8cc92352011-07-06 17:41:52 -07001930 values.put(Snapshots.DATE_CREATED, System.currentTimeMillis());
1931 values.put(Snapshots.FAVICON, compressBitmap(getFavicon()));
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001932 values.put(Snapshots.THUMBNAIL, compressBitmap(bm));
John Reckd8c74522011-06-14 08:45:00 -07001933 return values;
John Reck541f55a2011-06-07 16:34:43 -07001934 }
1935
John Reck68234a92012-04-19 15:27:12 -07001936 /**
1937 * Probably want to call this on a background thread
1938 */
1939 public boolean saveViewState(ContentValues values) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001940 WebView web = getWebView();
John Reck68234a92012-04-19 15:27:12 -07001941 if (web == null) return false;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001942 String filename = UUID.randomUUID().toString();
John Reck68234a92012-04-19 15:27:12 -07001943 SaveCallback callback = new SaveCallback();
John Reck68234a92012-04-19 15:27:12 -07001944 try {
John Reck68234a92012-04-19 15:27:12 -07001945 synchronized (callback) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001946 web.saveViewState(filename, callback);
1947 callback.wait();
John Reck68234a92012-04-19 15:27:12 -07001948 }
John Reck68234a92012-04-19 15:27:12 -07001949 } catch (Exception e) {
1950 Log.w(LOGTAG, "Failed to save view state", e);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001951 String path = callback.getPath();
1952 if (path != null) {
1953 File file = mContext.getFileStreamPath(path);
1954 if (file.exists() && !file.delete()) {
1955 file.deleteOnExit();
1956 }
John Reck68234a92012-04-19 15:27:12 -07001957 }
1958 return false;
1959 }
Tarun Nainani8eb00912014-07-17 12:28:32 -07001960
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001961 String path = callback.getPath();
Tarun Nainani8eb00912014-07-17 12:28:32 -07001962 // could be that saving of file failed
1963 if (path == null) {
1964 return false;
1965 }
1966
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001967 File savedFile = new File(path);
1968 if (!savedFile.exists()) {
1969 return false;
John Reck68234a92012-04-19 15:27:12 -07001970 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001971 values.put(Snapshots.VIEWSTATE_PATH, path.substring(path.lastIndexOf('/') + 1));
1972 values.put(Snapshots.VIEWSTATE_SIZE, savedFile.length());
John Reck68234a92012-04-19 15:27:12 -07001973 return true;
1974 }
1975
John Reck8cc92352011-07-06 17:41:52 -07001976 public byte[] compressBitmap(Bitmap bitmap) {
1977 if (bitmap == null) {
1978 return null;
1979 }
1980 ByteArrayOutputStream stream = new ByteArrayOutputStream();
1981 bitmap.compress(CompressFormat.PNG, 100, stream);
1982 return stream.toByteArray();
1983 }
1984
John Reck26b18322011-06-21 13:08:58 -07001985 public void loadUrl(String url, Map<String, String> headers) {
1986 if (mMainView != null) {
Michael Kolba53c9892011-10-05 13:31:40 -07001987 mPageLoadProgress = INITIAL_PROGRESS;
Michael Kolb14612442011-06-24 13:06:29 -07001988 mCurrentState = new PageState(mContext, false, url, null);
John Reck26b18322011-06-21 13:08:58 -07001989 mMainView.loadUrl(url, headers);
1990 }
1991 }
1992
John Reck38b39652012-06-05 09:22:59 -07001993 public void disableUrlOverridingForLoad() {
1994 mDisableOverrideUrlLoading = true;
1995 }
1996
Michael Kolb9ef259a2011-07-12 15:33:08 -07001997 protected void capture() {
Vivek Sekhar6bdf6452015-05-12 17:38:45 -07001998 boolean returnEmptyCapture = false;
1999 if (mMainView == null || mCapture == null || !mMainView.isReady())
2000 returnEmptyCapture = true;
John Reck4eadc342011-10-31 14:04:10 -07002001 if (mMainView.getContentWidth() <= 0 || mMainView.getContentHeight() <= 0) {
Vivek Sekhar6bdf6452015-05-12 17:38:45 -07002002 returnEmptyCapture = true;
John Reck4eadc342011-10-31 14:04:10 -07002003 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002004
Vivek Sekhar6bdf6452015-05-12 17:38:45 -07002005 if (returnEmptyCapture || !mFirstVisualPixelPainted || mMainView.isShowingCrashView()) {
Pankaj Garg79878492015-04-01 14:48:21 -07002006 mCapture = Bitmap.createBitmap(
2007 mCaptureWidth,
2008 mCaptureHeight,
2009 Bitmap.Config.RGB_565);
2010 mCapture.eraseColor(Color.WHITE);
2011
2012 mHandler.removeMessages(MSG_CAPTURE);
2013
2014 TabControl tc = mWebViewController.getTabControl();
2015 if (tc != null) {
2016 OnThumbnailUpdatedListener updateListener
2017 = tc.getOnThumbnailUpdatedListener();
2018 if (updateListener != null) {
2019 updateListener.onThumbnailUpdated(this);
2020 }
2021 }
2022 return;
2023 }
2024
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002025 mMainView
2026 .getContentBitmapAsync(
2027 (float) mCaptureWidth / mMainView.getWidth(),
2028 new Rect(),
2029 new ValueCallback<Bitmap>() {
2030 @Override
2031 public void onReceiveValue(Bitmap bitmap) {
2032 onCaptureCallback(bitmap);
2033 }});
2034 }
2035
2036 private void onCaptureCallback(Bitmap bitmap) {
2037 if (mCapture == null || bitmap == null)
2038 return;
2039
Michael Kolb9ef259a2011-07-12 15:33:08 -07002040 Canvas c = new Canvas(mCapture);
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002041 mCapture.eraseColor(Color.WHITE);
2042 c.drawBitmap(bitmap, 0, 0, null);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002043
Michael Kolba3194d02011-09-07 11:23:51 -07002044 // manually anti-alias the edges for the tilt
2045 c.drawRect(0, 0, 1, mCapture.getHeight(), sAlphaPaint);
2046 c.drawRect(mCapture.getWidth() - 1, 0, mCapture.getWidth(),
2047 mCapture.getHeight(), sAlphaPaint);
2048 c.drawRect(0, 0, mCapture.getWidth(), 1, sAlphaPaint);
2049 c.drawRect(0, mCapture.getHeight() - 1, mCapture.getWidth(),
2050 mCapture.getHeight(), sAlphaPaint);
Dianne Hackborn43cfe8a2011-08-02 16:59:35 -07002051 c.setBitmap(null);
John Reck8ee633f2011-08-09 16:00:35 -07002052 mHandler.removeMessages(MSG_CAPTURE);
John Reck1cf4b792011-07-26 10:22:22 -07002053 persistThumbnail();
John Reck8ee633f2011-08-09 16:00:35 -07002054 TabControl tc = mWebViewController.getTabControl();
2055 if (tc != null) {
2056 OnThumbnailUpdatedListener updateListener
2057 = tc.getOnThumbnailUpdatedListener();
2058 if (updateListener != null) {
2059 updateListener.onThumbnailUpdated(this);
2060 }
2061 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07002062 }
2063
2064 @Override
2065 public void onNewPicture(WebView view, Picture picture) {
John Reck1cf4b792011-07-26 10:22:22 -07002066 postCapture();
2067 }
2068
2069 private void postCapture() {
Michael Kolb9ef259a2011-07-12 15:33:08 -07002070 if (!mHandler.hasMessages(MSG_CAPTURE)) {
2071 mHandler.sendEmptyMessageDelayed(MSG_CAPTURE, CAPTURE_DELAY);
2072 }
2073 }
2074
John Reckef654f12011-07-12 16:42:08 -07002075 public boolean canGoBack() {
2076 return mMainView != null ? mMainView.canGoBack() : false;
2077 }
2078
2079 public boolean canGoForward() {
2080 return mMainView != null ? mMainView.canGoForward() : false;
2081 }
2082
2083 public void goBack() {
2084 if (mMainView != null) {
2085 mMainView.goBack();
2086 }
2087 }
2088
2089 public void goForward() {
2090 if (mMainView != null) {
2091 mMainView.goForward();
2092 }
2093 }
2094
John Reck1cf4b792011-07-26 10:22:22 -07002095 protected void persistThumbnail() {
John Reck4eadc342011-10-31 14:04:10 -07002096 DataController.getInstance(mContext).saveThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07002097 }
2098
2099 protected void deleteThumbnail() {
John Reck4eadc342011-10-31 14:04:10 -07002100 DataController.getInstance(mContext).deleteThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07002101 }
2102
John Reck4eadc342011-10-31 14:04:10 -07002103 void updateCaptureFromBlob(byte[] blob) {
John Reck1cf4b792011-07-26 10:22:22 -07002104 synchronized (Tab.this) {
2105 if (mCapture == null) {
2106 return;
2107 }
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002108 ByteBuffer buffer = ByteBuffer.wrap(blob);
Michael Kolbd837a112011-08-09 14:00:18 -07002109 try {
2110 mCapture.copyPixelsFromBuffer(buffer);
2111 } catch (RuntimeException rex) {
2112 Log.e(LOGTAG, "Load capture has mismatched sizes; buffer: "
2113 + buffer.capacity() + " blob: " + blob.length
2114 + "capture: " + mCapture.getByteCount());
2115 throw rex;
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002116 }
John Reck1cf4b792011-07-26 10:22:22 -07002117 }
2118 }
2119
John Reck52be4782011-08-26 15:37:29 -07002120 @Override
2121 public String toString() {
2122 StringBuilder builder = new StringBuilder(100);
2123 builder.append(mId);
2124 builder.append(") has parent: ");
2125 if (getParent() != null) {
2126 builder.append("true[");
2127 builder.append(getParent().getId());
2128 builder.append("]");
2129 } else {
2130 builder.append("false");
2131 }
2132 builder.append(", incog: ");
2133 builder.append(isPrivateBrowsingEnabled());
2134 if (!isPrivateBrowsingEnabled()) {
2135 builder.append(", title: ");
2136 builder.append(getTitle());
2137 builder.append(", url: ");
2138 builder.append(getUrl());
2139 }
2140 return builder.toString();
2141 }
2142
Steve Block4895b012011-10-03 16:26:46 +01002143 private void handleProceededAfterSslError(SslError error) {
2144 if (error.getUrl().equals(mCurrentState.mUrl)) {
2145 // The security state should currently be SECURITY_STATE_SECURE.
2146 setSecurityState(SecurityState.SECURITY_STATE_BAD_CERTIFICATE);
Steve Block08a6f0c2011-10-06 12:12:53 +01002147 mCurrentState.mSslCertificateError = error;
Steve Block4895b012011-10-03 16:26:46 +01002148 } else if (getSecurityState() == SecurityState.SECURITY_STATE_SECURE) {
Steve Block08a6f0c2011-10-06 12:12:53 +01002149 // The page's main resource is secure and this error is for a
2150 // sub-resource.
Steve Block4895b012011-10-03 16:26:46 +01002151 setSecurityState(SecurityState.SECURITY_STATE_MIXED);
2152 }
2153 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002154}