blob: 8f4db51ec6878b38a2f5c62fc745b14a64aaf0f6 [file] [log] [blame]
Grace Kloba22ac16e2009-10-07 18:00:23 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Bijan Amirzada41242f22014-03-21 12:12:18 -070017package com.android.browser;
Grace Kloba22ac16e2009-10-07 18:00:23 -070018
Michael Kolb8233fac2010-10-26 16:08:53 -070019import android.app.Activity;
Grace Kloba22ac16e2009-10-07 18:00:23 -070020import android.app.AlertDialog;
21import android.content.ContentResolver;
John Reckd8c74522011-06-14 08:45:00 -070022import android.content.ContentValues;
John Reck30c714c2010-12-16 17:30:34 -080023import android.content.Context;
Grace Kloba22ac16e2009-10-07 18:00:23 -070024import android.content.DialogInterface;
Michael Kolbfe251992010-07-08 15:41:55 -070025import android.content.DialogInterface.OnCancelListener;
Pankaj Garg1c13cab2015-05-12 11:52:17 -070026import android.content.res.Configuration;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080027import android.content.res.Resources;
Grace Kloba22ac16e2009-10-07 18:00:23 -070028import android.graphics.Bitmap;
John Reck8cc92352011-07-06 17:41:52 -070029import android.graphics.Bitmap.CompressFormat;
Michael Kolb9ef259a2011-07-12 15:33:08 -070030import android.graphics.BitmapFactory;
31import android.graphics.Canvas;
Michael Kolbc3af0672011-08-09 10:24:41 -070032import android.graphics.Color;
Michael Kolba3194d02011-09-07 11:23:51 -070033import android.graphics.Paint;
Michael Kolb9ef259a2011-07-12 15:33:08 -070034import android.graphics.Picture;
Michael Kolba3194d02011-09-07 11:23:51 -070035import android.graphics.PorterDuff;
36import android.graphics.PorterDuffXfermode;
Tarun Nainaniea28dde2014-08-27 17:25:09 -070037import android.graphics.Rect;
Grace Kloba22ac16e2009-10-07 18:00:23 -070038import android.net.Uri;
39import android.net.http.SslError;
Grace Kloba22ac16e2009-10-07 18:00:23 -070040import android.os.Bundle;
Michael Kolb9ef259a2011-07-12 15:33:08 -070041import android.os.Handler;
Grace Kloba22ac16e2009-10-07 18:00:23 -070042import android.os.Message;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +000043import android.os.SystemClock;
Brian Carlstrom8862c1d2011-06-02 01:05:55 -070044import android.security.KeyChain;
Brian Carlstromaa09cd82011-06-09 16:04:40 -070045import android.security.KeyChainAliasCallback;
John Reck24f18262011-06-17 14:47:20 -070046import android.text.TextUtils;
Grace Kloba22ac16e2009-10-07 18:00:23 -070047import android.util.Log;
48import android.view.KeyEvent;
49import android.view.LayoutInflater;
50import android.view.View;
Grace Kloba50c241e2010-04-20 11:07:50 -070051import android.view.ViewStub;
Ben Murdochc42addf2010-01-28 15:19:59 +000052import android.webkit.ConsoleMessage;
Grace Kloba22ac16e2009-10-07 18:00:23 -070053import android.webkit.URLUtil;
John Reck438bf462011-01-12 18:11:46 -080054import android.webkit.WebResourceResponse;
Grace Kloba22ac16e2009-10-07 18:00:23 -070055import android.webkit.WebStorage;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080056import android.webkit.WebChromeClient.CustomViewCallback;
57import android.webkit.ValueCallback;
Ben Murdoch1d676b62011-01-17 12:54:24 +000058import android.widget.CheckBox;
Ben Murdoch8029a772010-11-16 11:58:21 +000059import android.widget.Toast;
Grace Kloba22ac16e2009-10-07 18:00:23 -070060
Bijan Amirzada41242f22014-03-21 12:12:18 -070061import com.android.browser.TabControl.OnThumbnailUpdatedListener;
62import com.android.browser.homepages.HomeProvider;
63import com.android.browser.mynavigation.MyNavigationUtil;
64import com.android.browser.provider.MyNavigationProvider;
65import com.android.browser.provider.SnapshotProvider.Snapshots;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080066
67import org.codeaurora.swe.BrowserDownloadListener;
68import org.codeaurora.swe.ClientCertRequestHandler;
69import org.codeaurora.swe.HttpAuthHandler;
70import org.codeaurora.swe.SslErrorHandler;
71import org.codeaurora.swe.WebBackForwardList;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080072import org.codeaurora.swe.WebChromeClient;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080073import org.codeaurora.swe.WebView;
74import org.codeaurora.swe.WebView.PictureListener;
Pankaj Garg1c7380d2014-08-27 14:17:12 -070075import org.codeaurora.swe.WebView.CreateWindowParams;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080076import org.codeaurora.swe.WebViewClient;
Pankaj Garg1c13cab2015-05-12 11:52:17 -070077import org.codeaurora.swe.util.Observable;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080078
John Reck541f55a2011-06-07 16:34:43 -070079import java.io.ByteArrayOutputStream;
John Reck2b71d6d2012-04-18 17:42:06 -070080import java.io.File;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080081import java.io.InputStream;
John Reck1cf4b792011-07-26 10:22:22 -070082import java.nio.ByteBuffer;
Michael Kolbfe251992010-07-08 15:41:55 -070083import java.util.Map;
John Reck2b71d6d2012-04-18 17:42:06 -070084import java.util.UUID;
Michael Kolbfe251992010-07-08 15:41:55 -070085import java.util.Vector;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080086import java.sql.Timestamp;
87import java.util.Date;
Michael Kolbfe251992010-07-08 15:41:55 -070088
Grace Kloba22ac16e2009-10-07 18:00:23 -070089/**
90 * Class for maintaining Tabs with a main WebView and a subwindow.
91 */
Michael Kolb9ef259a2011-07-12 15:33:08 -070092class Tab implements PictureListener {
Michael Kolb8233fac2010-10-26 16:08:53 -070093
Grace Kloba22ac16e2009-10-07 18:00:23 -070094 // Log Tag
95 private static final String LOGTAG = "Tab";
Bijan Amirzada41242f22014-03-21 12:12:18 -070096 private static final boolean LOGD_ENABLED = com.android.browser.Browser.LOGD_ENABLED;
Ben Murdochc42addf2010-01-28 15:19:59 +000097 // Special case the logtag for messages for the Console to make it easier to
98 // filter them and match the logtag used for these messages in older versions
99 // of the browser.
100 private static final String CONSOLE_LOGTAG = "browser";
101
Michael Kolb9ef259a2011-07-12 15:33:08 -0700102 private static final int MSG_CAPTURE = 42;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800103 private static final int CAPTURE_DELAY = 1000;
Michael Kolba53c9892011-10-05 13:31:40 -0700104 private static final int INITIAL_PROGRESS = 5;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700105
John Reck1cf4b792011-07-26 10:22:22 -0700106 private static Bitmap sDefaultFavicon;
107
Michael Kolba3194d02011-09-07 11:23:51 -0700108 private static Paint sAlphaPaint = new Paint();
109 static {
110 sAlphaPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
111 sAlphaPaint.setColor(Color.TRANSPARENT);
112 }
113
Steve Block2466eff2011-10-03 15:33:09 +0100114 public enum SecurityState {
Steve Block4895b012011-10-03 16:26:46 +0100115 // The page's main resource does not use SSL. Note that we use this
116 // state irrespective of the SSL authentication state of sub-resources.
Steve Block2466eff2011-10-03 15:33:09 +0100117 SECURITY_STATE_NOT_SECURE,
Steve Block4895b012011-10-03 16:26:46 +0100118 // The page's main resource uses SSL and the certificate is good. The
119 // same is true of all sub-resources.
Steve Block2466eff2011-10-03 15:33:09 +0100120 SECURITY_STATE_SECURE,
Steve Block4895b012011-10-03 16:26:46 +0100121 // The page's main resource uses SSL and the certificate is good, but
122 // some sub-resources either do not use SSL or have problems with their
123 // certificates.
Steve Block2466eff2011-10-03 15:33:09 +0100124 SECURITY_STATE_MIXED,
Steve Block4895b012011-10-03 16:26:46 +0100125 // The page's main resource uses SSL but there is a problem with its
126 // certificate.
127 SECURITY_STATE_BAD_CERTIFICATE,
John Reck30c714c2010-12-16 17:30:34 -0800128 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700129
Michael Kolb14612442011-06-24 13:06:29 -0700130 Context mContext;
John Reckd8c74522011-06-14 08:45:00 -0700131 protected WebViewController mWebViewController;
Michael Kolb8233fac2010-10-26 16:08:53 -0700132
Michael Kolbc831b632011-05-11 09:30:34 -0700133 // The tab ID
John Reckd8c74522011-06-14 08:45:00 -0700134 private long mId = -1;
Michael Kolbc831b632011-05-11 09:30:34 -0700135
Grace Kloba22ac16e2009-10-07 18:00:23 -0700136 // Main WebView wrapper
Michael Kolba713ec82010-11-29 17:27:06 -0800137 private View mContainer;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700138 // Main WebView
139 private WebView mMainView;
140 // Subwindow container
141 private View mSubViewContainer;
142 // Subwindow WebView
143 private WebView mSubView;
144 // Saved bundle for when we are running low on memory. It contains the
145 // information needed to restore the WebView if the user goes back to the
146 // tab.
147 private Bundle mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700148 // Parent Tab. This is the Tab that created this Tab, or null if the Tab was
149 // created by the UI
Michael Kolbc831b632011-05-11 09:30:34 -0700150 private Tab mParent;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700151 // Tab that constructed by this Tab. This is used when this Tab is
152 // destroyed, it clears all mParentTab values in the children.
Michael Kolbc831b632011-05-11 09:30:34 -0700153 private Vector<Tab> mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700154 // If true, the tab is in the foreground of the current activity.
155 private boolean mInForeground;
Michael Kolb8233fac2010-10-26 16:08:53 -0700156 // If true, the tab is in page loading state (after onPageStarted,
157 // before onPageFinsihed)
158 private boolean mInPageLoad;
Tarun Nainani8eb00912014-07-17 12:28:32 -0700159 private boolean mPageFinished;
John Reck38b39652012-06-05 09:22:59 -0700160 private boolean mDisableOverrideUrlLoading;
Pankaj Garg79878492015-04-01 14:48:21 -0700161 private boolean mFirstVisualPixelPainted = false;
John Reck30c714c2010-12-16 17:30:34 -0800162 // The last reported progress of the current page
163 private int mPageLoadProgress;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000164 // The time the load started, used to find load page time
165 private long mLoadStartTime;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700166 // Application identifier used to find tabs that another application wants
167 // to reuse.
168 private String mAppId;
Michael Kolbe28b3472011-08-04 16:54:31 -0700169 // flag to indicate if tab should be closed on back
170 private boolean mCloseOnBack;
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700171 // flag to indicate if the tab was opened from an intent
172 private boolean mDerivedFromIntent = false;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -0500173 // The listener that gets invoked when a download is started from the
174 // mMainView
Selim Gurun0b3d66f2012-08-29 13:08:13 -0700175 private final BrowserDownloadListener mDownloadListener;
John Recke969cc52010-12-21 17:24:43 -0800176 private DataController mDataController;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700177
178 // AsyncTask for downloading touch icons
179 DownloadTouchIcon mTouchIconLoader;
180
John Reck35e9dd62011-04-25 09:01:54 -0700181 private BrowserSettings mSettings;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700182 private int mCaptureWidth;
183 private int mCaptureHeight;
184 private Bitmap mCapture;
185 private Handler mHandler;
Michael Kolb72864272012-05-03 15:42:15 -0700186 private boolean mUpdateThumbnail;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800187 private Timestamp timestamp;
Vivek Sekharf96064b2014-07-28 16:32:34 -0700188 private boolean mFullScreen = false;
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800189 private boolean mReceivedError;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700190
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -0700191 // determine if webview is destroyed to MemoryMonitor
192 private boolean mWebViewDestroyedByMemoryMonitor;
193
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700194 private Observable mFirstPixelObservable;
195 private Observable mTabHistoryUpdateObservable;
196
197 Observable getFirstPixelObservable() {
198 return mFirstPixelObservable;
199 }
200
201 Observable getTabHistoryUpdateObservable() {
202 return mTabHistoryUpdateObservable;
203 }
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -0700204
Mathew Inwood1dd8e822011-08-03 14:34:29 +0100205
John Reck1cf4b792011-07-26 10:22:22 -0700206 private static synchronized Bitmap getDefaultFavicon(Context context) {
207 if (sDefaultFavicon == null) {
208 sDefaultFavicon = BitmapFactory.decodeResource(
Enrico Rosd6efa972014-12-02 19:49:59 -0800209 context.getResources(), R.drawable.ic_deco_favicon_normal);
John Reck1cf4b792011-07-26 10:22:22 -0700210 }
211 return sDefaultFavicon;
212 }
Michael Kolbeb95db42011-03-03 10:38:40 -0800213
John Reck30c714c2010-12-16 17:30:34 -0800214 // All the state needed for a page
John Reckd8c74522011-06-14 08:45:00 -0700215 protected static class PageState {
John Reck30c714c2010-12-16 17:30:34 -0800216 String mUrl;
John Reckdb22ec42011-06-29 11:31:24 -0700217 String mOriginalUrl;
John Reck30c714c2010-12-16 17:30:34 -0800218 String mTitle;
Steve Block2466eff2011-10-03 15:33:09 +0100219 SecurityState mSecurityState;
Steve Block08a6f0c2011-10-06 12:12:53 +0100220 // This is non-null only when mSecurityState is SECURITY_STATE_BAD_CERTIFICATE.
221 SslError mSslCertificateError;
John Reck30c714c2010-12-16 17:30:34 -0800222 Bitmap mFavicon;
Steve Block08a6f0c2011-10-06 12:12:53 +0100223 boolean mIsBookmarkedSite;
224 boolean mIncognito;
John Reck30c714c2010-12-16 17:30:34 -0800225
226 PageState(Context c, boolean incognito) {
John Reck502a3532011-08-16 14:21:46 -0700227 mIncognito = incognito;
228 if (mIncognito) {
Vivek Sekhared791da2015-02-22 12:39:05 -0800229 mOriginalUrl = mUrl = "chrome://incognito";
John Reck30c714c2010-12-16 17:30:34 -0800230 mTitle = c.getString(R.string.new_incognito_tab);
John Reck30c714c2010-12-16 17:30:34 -0800231 } else {
John Reckdb22ec42011-06-29 11:31:24 -0700232 mOriginalUrl = mUrl = "";
John Reck30c714c2010-12-16 17:30:34 -0800233 mTitle = c.getString(R.string.new_tab);
John Reck30c714c2010-12-16 17:30:34 -0800234 }
Steve Block2466eff2011-10-03 15:33:09 +0100235 mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
John Reck30c714c2010-12-16 17:30:34 -0800236 }
237
238 PageState(Context c, boolean incognito, String url, Bitmap favicon) {
John Reck502a3532011-08-16 14:21:46 -0700239 mIncognito = incognito;
John Reckdb22ec42011-06-29 11:31:24 -0700240 mOriginalUrl = mUrl = url;
John Reck30c714c2010-12-16 17:30:34 -0800241 if (URLUtil.isHttpsUrl(url)) {
Steve Block2466eff2011-10-03 15:33:09 +0100242 mSecurityState = SecurityState.SECURITY_STATE_SECURE;
John Reck30c714c2010-12-16 17:30:34 -0800243 } else {
Steve Block2466eff2011-10-03 15:33:09 +0100244 mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
John Reck30c714c2010-12-16 17:30:34 -0800245 }
John Reck1cf4b792011-07-26 10:22:22 -0700246 mFavicon = favicon;
John Reck30c714c2010-12-16 17:30:34 -0800247 }
John Reck1cf4b792011-07-26 10:22:22 -0700248
Grace Kloba22ac16e2009-10-07 18:00:23 -0700249 }
250
John Reck30c714c2010-12-16 17:30:34 -0800251 // The current/loading page's state
John Reckd8c74522011-06-14 08:45:00 -0700252 protected PageState mCurrentState;
John Reck30c714c2010-12-16 17:30:34 -0800253
Grace Kloba22ac16e2009-10-07 18:00:23 -0700254 // Used for saving and restoring each Tab
Michael Kolbc831b632011-05-11 09:30:34 -0700255 static final String ID = "ID";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700256 static final String CURRURL = "currentUrl";
257 static final String CURRTITLE = "currentTitle";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700258 static final String PARENTTAB = "parentTab";
259 static final String APPID = "appid";
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700260 static final String INCOGNITO = "privateBrowsingEnabled";
John Reckb0a86db2011-05-24 14:05:58 -0700261 static final String USERAGENT = "useragent";
Michael Kolbe28b3472011-08-04 16:54:31 -0700262 static final String CLOSEFLAG = "closeOnBack";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700263
Pankaj Garg18186a92015-03-31 14:59:33 -0700264 public void setNetworkAvailable(boolean networkUp) {
265 if (networkUp && mReceivedError && (mMainView != null)) {
266 mMainView.reload();
267 }
268 }
269
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700270 public boolean isFirstVisualPixelPainted() {
271 return mFirstVisualPixelPainted;
272 }
273
274 public int getCaptureIndex(int navIndex) {
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700275 int orientation = mWebViewController.getActivity().
276 getResources().getConfiguration().orientation;
277
278 int orientationBit = (orientation == Configuration.ORIENTATION_LANDSCAPE) ? 0 : 1;
279
Vivek Sekhard0f60402015-06-05 14:07:11 -0700280 int index = orientationBit << 31 | (((int)mId & 0x7f) << 24) | (navIndex & 0xffffff);
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700281 return index;
282 }
283
284 public int getTabIdxFromCaptureIdx(int index) {
285 return (index & 0x7f000000) >> 24;
286 }
287
288 public int getOrientationFromCaptureIdx(int index) {
289 return ((index & 0x80000000) == 0) ? Configuration.ORIENTATION_LANDSCAPE :
290 Configuration.ORIENTATION_PORTRAIT;
291
292 }
293
294 public int getNavIdxFromCaptureIdx(int index) {
295 return (index & 0xffffff);
296 }
297
Grace Kloba22ac16e2009-10-07 18:00:23 -0700298 // -------------------------------------------------------------------------
299 // WebViewClient implementation for the main WebView
300 // -------------------------------------------------------------------------
301
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800302 private final WebViewClient mWebViewClient = new WebViewClient() {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500303 private Message mDontResend;
304 private Message mResend;
Michael Kolb47bd1e42011-09-01 15:25:00 -0700305
306 private boolean providersDiffer(String url, String otherUrl) {
307 Uri uri1 = Uri.parse(url);
308 Uri uri2 = Uri.parse(otherUrl);
309 return !uri1.getEncodedAuthority().equals(uri2.getEncodedAuthority());
310 }
311
Grace Kloba22ac16e2009-10-07 18:00:23 -0700312 @Override
313 public void onPageStarted(WebView view, String url, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700314 mInPageLoad = true;
Tarun Nainani8eb00912014-07-17 12:28:32 -0700315 mPageFinished = false;
Pankaj Garg79878492015-04-01 14:48:21 -0700316 mFirstVisualPixelPainted = false;
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700317 mFirstPixelObservable.set(false);
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800318 mReceivedError = false;
Michael Kolb72864272012-05-03 15:42:15 -0700319 mUpdateThumbnail = true;
Michael Kolba53c9892011-10-05 13:31:40 -0700320 mPageLoadProgress = INITIAL_PROGRESS;
Michael Kolb14612442011-06-24 13:06:29 -0700321 mCurrentState = new PageState(mContext,
John Reck30c714c2010-12-16 17:30:34 -0800322 view.isPrivateBrowsingEnabled(), url, favicon);
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000323 mLoadStartTime = SystemClock.uptimeMillis();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700324
325 // If we start a touch icon load and then load a new page, we don't
326 // want to cancel the current touch icon loader. But, we do want to
327 // create a new one when the touch icon url is known.
328 if (mTouchIconLoader != null) {
329 mTouchIconLoader.mTab = null;
330 mTouchIconLoader = null;
331 }
332
Grace Kloba22ac16e2009-10-07 18:00:23 -0700333 // finally update the UI in the activity if it is in the foreground
John Reck324d4402011-01-11 16:56:42 -0800334 mWebViewController.onPageStarted(Tab.this, view, favicon);
Leon Scroggins4cd97792010-12-03 15:31:56 -0500335
John Recke969cc52010-12-21 17:24:43 -0800336 updateBookmarkedStatus();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700337 }
338
339 @Override
340 public void onPageFinished(WebView view, String url) {
John Reck38b39652012-06-05 09:22:59 -0700341 mDisableOverrideUrlLoading = false;
John Reck5b691842010-11-29 11:21:13 -0800342 if (!isPrivateBrowsingEnabled()) {
343 LogTag.logPageFinishedLoading(
344 url, SystemClock.uptimeMillis() - mLoadStartTime);
345 }
John Reck1cf4b792011-07-26 10:22:22 -0700346 syncCurrentState(view, url);
John Reck324d4402011-01-11 16:56:42 -0800347 mWebViewController.onPageFinished(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700348 }
349
Pankaj Garg79878492015-04-01 14:48:21 -0700350 @Override
351 public void onFirstVisualPixel(WebView view) {
352 mFirstVisualPixelPainted = true;
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700353 mFirstPixelObservable.set(true);
Pankaj Garg79878492015-04-01 14:48:21 -0700354 }
355
Grace Kloba22ac16e2009-10-07 18:00:23 -0700356 // return true if want to hijack the url to let another app to handle it
357 @Override
358 public boolean shouldOverrideUrlLoading(WebView view, String url) {
John Reck38b39652012-06-05 09:22:59 -0700359 if (!mDisableOverrideUrlLoading && mInForeground) {
Michael Kolb18eb3772010-12-10 14:29:51 -0800360 return mWebViewController.shouldOverrideUrlLoading(Tab.this,
361 view, url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700362 } else {
363 return false;
364 }
365 }
366
Vivek Sekharb991edb2014-12-17 18:18:07 -0800367 @Override
368 public boolean shouldDownloadFavicon(WebView view, String url) {
369 return true;
370 }
371
Grace Kloba22ac16e2009-10-07 18:00:23 -0700372 /**
Steve Block2466eff2011-10-03 15:33:09 +0100373 * Updates the security state. This method is called when we discover
374 * another resource to be loaded for this page (for example,
375 * javascript). While we update the security state, we do not update
376 * the lock icon until we are done loading, as it is slightly more
377 * secure this way.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700378 */
379 @Override
380 public void onLoadResource(WebView view, String url) {
381 if (url != null && url.length() > 0) {
382 // It is only if the page claims to be secure that we may have
Steve Block2466eff2011-10-03 15:33:09 +0100383 // to update the security state:
384 if (mCurrentState.mSecurityState == SecurityState.SECURITY_STATE_SECURE) {
385 // If NOT a 'safe' url, change the state to mixed content!
Grace Kloba22ac16e2009-10-07 18:00:23 -0700386 if (!(URLUtil.isHttpsUrl(url) || URLUtil.isDataUrl(url)
387 || URLUtil.isAboutUrl(url))) {
Steve Block2466eff2011-10-03 15:33:09 +0100388 mCurrentState.mSecurityState = SecurityState.SECURITY_STATE_MIXED;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700389 }
390 }
391 }
392 }
393
394 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700395 * Show a dialog informing the user of the network error reported by
396 * WebCore if it is in the foreground.
397 */
398 @Override
399 public void onReceivedError(WebView view, int errorCode,
400 String description, String failingUrl) {
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800401 // Used for the syncCurrentState to use
402 // the failing url instead of using webview url
403 mReceivedError = true;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700404 }
405
406 /**
407 * Check with the user if it is ok to resend POST data as the page they
408 * are trying to navigate to is the result of a POST.
409 */
410 @Override
411 public void onFormResubmission(WebView view, final Message dontResend,
412 final Message resend) {
413 if (!mInForeground) {
414 dontResend.sendToTarget();
415 return;
416 }
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500417 if (mDontResend != null) {
418 Log.w(LOGTAG, "onFormResubmission should not be called again "
419 + "while dialog is still up");
420 dontResend.sendToTarget();
421 return;
422 }
423 mDontResend = dontResend;
424 mResend = resend;
Michael Kolb14612442011-06-24 13:06:29 -0700425 new AlertDialog.Builder(mContext).setTitle(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700426 R.string.browserFrameFormResubmitLabel).setMessage(
427 R.string.browserFrameFormResubmitMessage)
428 .setPositiveButton(R.string.ok,
429 new DialogInterface.OnClickListener() {
430 public void onClick(DialogInterface dialog,
431 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500432 if (mResend != null) {
433 mResend.sendToTarget();
434 mResend = null;
435 mDontResend = null;
436 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700437 }
438 }).setNegativeButton(R.string.cancel,
439 new DialogInterface.OnClickListener() {
440 public void onClick(DialogInterface dialog,
441 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500442 if (mDontResend != null) {
443 mDontResend.sendToTarget();
444 mResend = null;
445 mDontResend = null;
446 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700447 }
448 }).setOnCancelListener(new OnCancelListener() {
449 public void onCancel(DialogInterface dialog) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500450 if (mDontResend != null) {
451 mDontResend.sendToTarget();
452 mResend = null;
453 mDontResend = null;
454 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700455 }
456 }).show();
457 }
458
459 /**
460 * Insert the url into the visited history database.
461 * @param url The url to be inserted.
462 * @param isReload True if this url is being reloaded.
463 * FIXME: Not sure what to do when reloading the page.
464 */
465 @Override
466 public void doUpdateVisitedHistory(WebView view, String url,
467 boolean isReload) {
John Reck324d4402011-01-11 16:56:42 -0800468 mWebViewController.doUpdateVisitedHistory(Tab.this, isReload);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700469 }
470
471 /**
472 * Displays SSL error(s) dialog to the user.
473 */
474 @Override
475 public void onReceivedSslError(final WebView view,
476 final SslErrorHandler handler, final SslError error) {
477 if (!mInForeground) {
478 handler.cancel();
Steve Block2466eff2011-10-03 15:33:09 +0100479 setSecurityState(SecurityState.SECURITY_STATE_NOT_SECURE);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700480 return;
481 }
John Reck35e9dd62011-04-25 09:01:54 -0700482 if (mSettings.showSecurityWarnings()) {
John Reckcb28b2c2011-08-26 17:39:44 -0700483 new AlertDialog.Builder(mContext)
484 .setTitle(R.string.security_warning)
485 .setMessage(R.string.ssl_warnings_header)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200486 .setIconAttribute(android.R.attr.alertDialogIcon)
John Reckcb28b2c2011-08-26 17:39:44 -0700487 .setPositiveButton(R.string.ssl_continue,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700488 new DialogInterface.OnClickListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700489 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700490 public void onClick(DialogInterface dialog,
491 int whichButton) {
492 handler.proceed();
Steve Block4895b012011-10-03 16:26:46 +0100493 handleProceededAfterSslError(error);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700494 }
John Reckcb28b2c2011-08-26 17:39:44 -0700495 })
496 .setNeutralButton(R.string.view_certificate,
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) {
John Reckcb28b2c2011-08-26 17:39:44 -0700501 mWebViewController.showSslCertificateOnError(
502 view, handler, error);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700503 }
John Reckcb28b2c2011-08-26 17:39:44 -0700504 })
505 .setNegativeButton(R.string.ssl_go_back,
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 Reck30c714c2010-12-16 17:30:34 -0800510 dialog.cancel();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700511 }
John Reckcb28b2c2011-08-26 17:39:44 -0700512 })
513 .setOnCancelListener(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700514 new DialogInterface.OnCancelListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700515 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700516 public void onCancel(DialogInterface dialog) {
517 handler.cancel();
Steve Block2466eff2011-10-03 15:33:09 +0100518 setSecurityState(SecurityState.SECURITY_STATE_NOT_SECURE);
John Reck30c714c2010-12-16 17:30:34 -0800519 mWebViewController.onUserCanceledSsl(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700520 }
John Reckcb28b2c2011-08-26 17:39:44 -0700521 })
522 .show();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700523 } else {
524 handler.proceed();
525 }
526 }
527
528 /**
Steve Block4895b012011-10-03 16:26:46 +0100529 * Called when an SSL error occurred while loading a resource, but the
530 * WebView but chose to proceed anyway based on a decision retained
531 * from a previous response to onReceivedSslError(). We update our
532 * security state to reflect this.
533 */
534 @Override
535 public void onProceededAfterSslError(WebView view, SslError error) {
536 handleProceededAfterSslError(error);
537 }
538
539 /**
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700540 * Displays client certificate request to the user.
541 */
542 @Override
543 public void onReceivedClientCertRequest(final WebView view,
544 final ClientCertRequestHandler handler, final String host_and_port) {
545 if (!mInForeground) {
546 handler.ignore();
547 return;
548 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700549 int colon = host_and_port.lastIndexOf(':');
550 String host;
551 int port;
552 if (colon == -1) {
553 host = host_and_port;
554 port = -1;
555 } else {
556 String portString = host_and_port.substring(colon + 1);
557 try {
558 port = Integer.parseInt(portString);
559 host = host_and_port.substring(0, colon);
560 } catch (NumberFormatException e) {
561 host = host_and_port;
562 port = -1;
563 }
564 }
Michael Kolb14612442011-06-24 13:06:29 -0700565 KeyChain.choosePrivateKeyAlias(
566 mWebViewController.getActivity(), new KeyChainAliasCallback() {
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700567 @Override public void alias(String alias) {
568 if (alias == null) {
569 handler.cancel();
570 return;
571 }
Michael Kolb14612442011-06-24 13:06:29 -0700572 new KeyChainLookup(mContext, handler, alias).execute();
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700573 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700574 }, null, null, host, port, null);
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700575 }
576
577 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700578 * Handles an HTTP authentication request.
579 *
580 * @param handler The authentication handler
581 * @param host The host
582 * @param realm The realm
583 */
584 @Override
585 public void onReceivedHttpAuthRequest(WebView view,
586 final HttpAuthHandler handler, final String host,
587 final String realm) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700588 mWebViewController.onReceivedHttpAuthRequest(Tab.this, view, handler, host, realm);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700589 }
590
591 @Override
John Reck438bf462011-01-12 18:11:46 -0800592 public WebResourceResponse shouldInterceptRequest(WebView view,
593 String url) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800594 //intercept if opening a new incognito tab - show the incognito welcome page
Vivek Sekhared791da2015-02-22 12:39:05 -0800595 if (url.startsWith("chrome://incognito")) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800596 Resources resourceHandle = mContext.getResources();
597 InputStream inStream = resourceHandle.openRawResource(
Bijan Amirzada41242f22014-03-21 12:12:18 -0700598 com.android.browser.R.raw.incognito_mode_start_page);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800599 return new WebResourceResponse("text/html", "utf8", inStream);
600 }
kaiyiz6e5b3e02013-08-19 20:02:01 +0800601 WebResourceResponse res;
602 if (MyNavigationUtil.MY_NAVIGATION.equals(url)) {
603 res = MyNavigationProvider.shouldInterceptRequest(mContext, url);
604 } else {
605 res = HomeProvider.shouldInterceptRequest(mContext, url);
606 }
John Reck438bf462011-01-12 18:11:46 -0800607 return res;
608 }
609
610 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700611 public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
612 if (!mInForeground) {
613 return false;
614 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700615 return mWebViewController.shouldOverrideKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700616 }
617
618 @Override
619 public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700620 if (!mInForeground) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700621 return;
622 }
John Reck997b1b72012-04-19 18:08:25 -0700623 if (!mWebViewController.onUnhandledKeyEvent(event)) {
624 super.onUnhandledKeyEvent(view, event);
625 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700626 }
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700627
628 @Override
629 public void beforeNavigation(WebView view, String url) {
630 if (isPrivateBrowsingEnabled()) {
631 return;
632 }
633
634 if (!mFirstVisualPixelPainted) {
635 return;
636 }
637
638 final int idx = view.copyBackForwardList().getCurrentIndex();
639 boolean bitmapExists = view.hasSnapshot(idx);
640
641 int progress = 100;
642 Controller controller = (Controller)mWebViewController;
643 UI ui = controller.getUi();
644 if (ui instanceof BaseUi) {
645 BaseUi baseUi = (BaseUi) ui;
646 TitleBar titleBar = baseUi.getTitleBar();
647 progress = titleBar.getProgressView().getProgressPercent();
648 }
649
650 if (bitmapExists && progress < 85) {
651 return;
652 }
653
654 int index = getCaptureIndex(view.getLastCommittedHistoryIndex());
655 view.captureSnapshot(index , null);
656 }
657
658 @Override
659 public void onHistoryItemCommit(WebView view, int index) {
660 mTabHistoryUpdateObservable.set(index);
661 int maxIdx = view.copyBackForwardList().getSize();
662 int[] ids = view.getSnapshotIds();
663 int currentTabIdx = mWebViewController.getTabControl().getCurrentPosition();
664 for (int id : ids) {
665 if (getTabIdxFromCaptureIdx(id) == currentTabIdx &&
666 getNavIdxFromCaptureIdx(id) >= maxIdx) {
667 view.deleteSnapshot(id);
668 }
669 }
670 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700671 };
672
John Reck1cf4b792011-07-26 10:22:22 -0700673 private void syncCurrentState(WebView view, String url) {
674 // Sync state (in case of stop/timeout)
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800675
676 if (mReceivedError) {
677 mCurrentState.mUrl = url;
678 mCurrentState.mOriginalUrl = url;
679 } else {
680 mCurrentState.mUrl = view.getUrl();
681 mCurrentState.mOriginalUrl = view.getOriginalUrl();
682 mCurrentState.mFavicon = view.getFavicon();
683 }
684
John Reck1cf4b792011-07-26 10:22:22 -0700685 if (mCurrentState.mUrl == null) {
John Reck8bcafc12011-08-29 16:43:02 -0700686 mCurrentState.mUrl = "";
John Reck1cf4b792011-07-26 10:22:22 -0700687 }
John Reck1cf4b792011-07-26 10:22:22 -0700688 mCurrentState.mTitle = view.getTitle();
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800689
690
John Reck1cf4b792011-07-26 10:22:22 -0700691 if (!URLUtil.isHttpsUrl(mCurrentState.mUrl)) {
692 // In case we stop when loading an HTTPS page from an HTTP page
693 // but before a provisional load occurred
Steve Block2466eff2011-10-03 15:33:09 +0100694 mCurrentState.mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
Steve Block08a6f0c2011-10-06 12:12:53 +0100695 mCurrentState.mSslCertificateError = null;
John Reck1cf4b792011-07-26 10:22:22 -0700696 }
John Reck502a3532011-08-16 14:21:46 -0700697 mCurrentState.mIncognito = view.isPrivateBrowsingEnabled();
John Reck1cf4b792011-07-26 10:22:22 -0700698 }
699
Tarun Nainani8eb00912014-07-17 12:28:32 -0700700
701 public boolean isTabFullScreen() {
702 return mFullScreen;
703 }
704
Vivek Sekharf96064b2014-07-28 16:32:34 -0700705 protected void setTabFullscreen(boolean fullScreen) {
Tarun Nainani8eb00912014-07-17 12:28:32 -0700706 Controller controller = (Controller)mWebViewController;
Sudheer Koganti24766882014-10-02 10:58:09 -0700707 controller.getUi().showFullscreen(fullScreen);
Tarun Nainani8eb00912014-07-17 12:28:32 -0700708 mFullScreen = fullScreen;
Vivek Sekharf96064b2014-07-28 16:32:34 -0700709 }
710
Sudheer Koganti24766882014-10-02 10:58:09 -0700711 public boolean exitFullscreen() {
712 if (mFullScreen) {
713 Controller controller = (Controller)mWebViewController;
714 controller.getUi().showFullscreen(false);
715 if (getWebView() != null)
716 getWebView().exitFullscreen();
717 mFullScreen = false;
718 return true;
719 }
720 return false;
721 }
722
723
724
725
Grace Kloba22ac16e2009-10-07 18:00:23 -0700726 // -------------------------------------------------------------------------
727 // WebChromeClient implementation for the main WebView
728 // -------------------------------------------------------------------------
729
730 private final WebChromeClient mWebChromeClient = new WebChromeClient() {
731 // Helper method to create a new tab or sub window.
732 private void createWindow(final boolean dialog, final Message msg) {
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700733 this.createWindow(dialog, msg, null, false);
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700734 }
735
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700736 private void createWindow(final boolean dialog, final Message msg, final String url,
737 final boolean opener_suppressed) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700738 WebView.WebViewTransport transport =
739 (WebView.WebViewTransport) msg.obj;
740 if (dialog) {
741 createSubWindow();
Michael Kolb8233fac2010-10-26 16:08:53 -0700742 mWebViewController.attachSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700743 transport.setWebView(mSubView);
744 } else {
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700745 final Tab newTab = mWebViewController.openTab(url,
John Reck5949c662011-05-27 09:52:29 -0700746 Tab.this, true, true);
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700747 // This is special case for rendering links on a webpage in
748 // a new tab. If opener is suppressed, the WebContents created
749 // by the content layer are not fully initialized. This check
750 // will prevent content layer from overriding WebContents
751 // created by new tab with the uninitialized instance.
752 if (!opener_suppressed) {
753 transport.setWebView(newTab.getWebView());
754 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700755 }
756 msg.sendToTarget();
757 }
758
759 @Override
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700760 public void toggleFullscreenModeForTab(boolean enterFullscreen) {
761 if (mWebViewController instanceof Controller) {
Vivek Sekharf96064b2014-07-28 16:32:34 -0700762 setTabFullscreen(enterFullscreen);
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700763 }
764 }
765
766 @Override
Tarun Nainani8eb00912014-07-17 12:28:32 -0700767 public void onOffsetsForFullscreenChanged(float topControlsOffsetYPix,
768 float contentOffsetYPix,
769 float overdrawBottomHeightPix) {
770 if (mWebViewController instanceof Controller) {
Vivek Sekhar8ee3abb2014-07-14 12:32:05 -0700771 Controller controller = (Controller)mWebViewController;
Tarun Nainani8eb00912014-07-17 12:28:32 -0700772 controller.getUi().translateTitleBar(topControlsOffsetYPix);
Vivek Sekhar8ee3abb2014-07-14 12:32:05 -0700773 }
774 }
775
776 @Override
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700777 public boolean isTabFullScreen() {
Vivek Sekharf96064b2014-07-28 16:32:34 -0700778 return mFullScreen;
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700779 }
780
781 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700782 public boolean onCreateWindow(WebView view, final boolean dialog,
783 final boolean userGesture, final Message resultMsg) {
784 // only allow new window or sub window for the foreground case
785 if (!mInForeground) {
786 return false;
787 }
788 // Short-circuit if we can't create any more tabs or sub windows.
789 if (dialog && mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -0700790 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700791 .setTitle(R.string.too_many_subwindows_dialog_title)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200792 .setIconAttribute(android.R.attr.alertDialogIcon)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700793 .setMessage(R.string.too_many_subwindows_dialog_message)
794 .setPositiveButton(R.string.ok, null)
795 .show();
796 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700797 } else if (!mWebViewController.getTabControl().canCreateNewTab()) {
Michael Kolb14612442011-06-24 13:06:29 -0700798 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700799 .setTitle(R.string.too_many_windows_dialog_title)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200800 .setIconAttribute(android.R.attr.alertDialogIcon)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700801 .setMessage(R.string.too_many_windows_dialog_message)
802 .setPositiveButton(R.string.ok, null)
803 .show();
804 return false;
805 }
806
807 // Short-circuit if this was a user gesture.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800808 if (userGesture || !mSettings.blockPopupWindows()) {
Tarun Nainani4f5137d2015-04-16 17:26:18 -0700809 WebView.WebViewTransport transport =
810 (WebView.WebViewTransport) resultMsg.obj;
811 CreateWindowParams windowParams = transport.getCreateWindowParams();
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700812 if (windowParams.mOpenerSuppressed) {
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700813 createWindow(dialog, resultMsg, windowParams.mURL, true);
814 // This is special case for rendering links on a webpage in
815 // a new tab. If opener is suppressed, the WebContents created
816 // by the content layer are not fully initialized. Returning false
817 // will prevent content layer from overriding WebContents
818 // created by new tab with the uninitialized instance.
819 return false;
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700820 }
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700821
822 createWindow(dialog, resultMsg);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700823 return true;
824 }
825
Tarun Nainani4f5137d2015-04-16 17:26:18 -0700826 createWindow(dialog, resultMsg);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700827 return true;
828 }
829
830 @Override
Patrick Scotteb5061b2009-11-18 15:00:30 -0500831 public void onRequestFocus(WebView view) {
832 if (!mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700833 mWebViewController.switchToTab(Tab.this);
Patrick Scotteb5061b2009-11-18 15:00:30 -0500834 }
835 }
836
837 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700838 public void onCloseWindow(WebView window) {
Michael Kolbc831b632011-05-11 09:30:34 -0700839 if (mParent != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700840 // JavaScript can only close popup window.
841 if (mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700842 mWebViewController.switchToTab(mParent);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700843 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700844 mWebViewController.closeTab(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700845 }
846 }
847
848 @Override
849 public void onProgressChanged(WebView view, int newProgress) {
John Reck30c714c2010-12-16 17:30:34 -0800850 mPageLoadProgress = newProgress;
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800851 if (newProgress == 100) {
852 mInPageLoad = false;
853 }
John Reck30c714c2010-12-16 17:30:34 -0800854 mWebViewController.onProgressChanged(Tab.this);
Michael Kolb72864272012-05-03 15:42:15 -0700855 if (mUpdateThumbnail && newProgress == 100) {
856 mUpdateThumbnail = false;
857 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700858 }
859
860 @Override
Leon Scroggins21d9b902010-03-11 09:33:11 -0500861 public void onReceivedTitle(WebView view, final String title) {
John Reck30c714c2010-12-16 17:30:34 -0800862 mCurrentState.mTitle = title;
Michael Kolb8233fac2010-10-26 16:08:53 -0700863 mWebViewController.onReceivedTitle(Tab.this, title);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700864 }
865
866 @Override
867 public void onReceivedIcon(WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -0800868 mCurrentState.mFavicon = icon;
Michael Kolb8233fac2010-10-26 16:08:53 -0700869 mWebViewController.onFavicon(Tab.this, view, icon);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700870 }
871
872 @Override
873 public void onReceivedTouchIconUrl(WebView view, String url,
874 boolean precomposed) {
Michael Kolb14612442011-06-24 13:06:29 -0700875 final ContentResolver cr = mContext.getContentResolver();
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400876 // Let precomposed icons take precedence over non-composed
877 // icons.
878 if (precomposed && mTouchIconLoader != null) {
879 mTouchIconLoader.cancel(false);
880 mTouchIconLoader = null;
881 }
882 // Have only one async task at a time.
883 if (mTouchIconLoader == null) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700884 mTouchIconLoader = new DownloadTouchIcon(Tab.this,
Michael Kolb14612442011-06-24 13:06:29 -0700885 mContext, cr, view);
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400886 mTouchIconLoader.execute(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700887 }
888 }
889
890 @Override
891 public void onShowCustomView(View view,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800892 CustomViewCallback callback) {
Michael Kolb14612442011-06-24 13:06:29 -0700893 Activity activity = mWebViewController.getActivity();
894 if (activity != null) {
895 onShowCustomView(view, activity.getRequestedOrientation(), callback);
896 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400897 }
898
899 @Override
900 public void onShowCustomView(View view, int requestedOrientation,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800901 CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700902 if (mInForeground) mWebViewController.showCustomView(Tab.this, view,
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400903 requestedOrientation, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700904 }
905
906 @Override
907 public void onHideCustomView() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700908 if (mInForeground) mWebViewController.hideCustomView();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700909 }
910
911 /**
912 * The origin has exceeded its database quota.
913 * @param url the URL that exceeded the quota
914 * @param databaseIdentifier the identifier of the database on which the
915 * transaction that caused the quota overflow was run
916 * @param currentQuota the current quota for the origin.
917 * @param estimatedSize the estimated size of the database.
918 * @param totalUsedQuota is the sum of all origins' quota.
919 * @param quotaUpdater The callback to run when a decision to allow or
920 * deny quota has been made. Don't forget to call this!
921 */
922 @Override
923 public void onExceededDatabaseQuota(String url,
924 String databaseIdentifier, long currentQuota, long estimatedSize,
925 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -0700926 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -0700927 .onExceededDatabaseQuota(url, databaseIdentifier,
928 currentQuota, estimatedSize, totalUsedQuota,
929 quotaUpdater);
930 }
931
932 /**
933 * The Application Cache has exceeded its max size.
934 * @param spaceNeeded is the amount of disk space that would be needed
935 * in order for the last appcache operation to succeed.
936 * @param totalUsedQuota is the sum of all origins' quota.
937 * @param quotaUpdater A callback to inform the WebCore thread that a
938 * new app cache size is available. This callback must always
939 * be executed at some point to ensure that the sleeping
940 * WebCore thread is woken up.
941 */
942 @Override
943 public void onReachedMaxAppCacheSize(long spaceNeeded,
944 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -0700945 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -0700946 .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota,
947 quotaUpdater);
948 }
949
Ben Murdoch65acc352009-11-19 18:16:04 +0000950 /* Adds a JavaScript error message to the system log and if the JS
951 * console is enabled in the about:debug options, to that console
952 * also.
Ben Murdochc42addf2010-01-28 15:19:59 +0000953 * @param consoleMessage the message object.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700954 */
955 @Override
Ben Murdochc42addf2010-01-28 15:19:59 +0000956 public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Jeff Hamilton47654f42010-09-07 09:57:51 -0500957 // Don't log console messages in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -0700958 if (isPrivateBrowsingEnabled()) return true;
Jeff Hamilton47654f42010-09-07 09:57:51 -0500959
Ben Murdochc42addf2010-01-28 15:19:59 +0000960 String message = "Console: " + consoleMessage.message() + " "
961 + consoleMessage.sourceId() + ":"
962 + consoleMessage.lineNumber();
963
964 switch (consoleMessage.messageLevel()) {
965 case TIP:
966 Log.v(CONSOLE_LOGTAG, message);
967 break;
968 case LOG:
969 Log.i(CONSOLE_LOGTAG, message);
970 break;
971 case WARNING:
972 Log.w(CONSOLE_LOGTAG, message);
973 break;
974 case ERROR:
975 Log.e(CONSOLE_LOGTAG, message);
976 break;
977 case DEBUG:
978 Log.d(CONSOLE_LOGTAG, message);
979 break;
980 }
981
982 return true;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700983 }
984
985 /**
986 * Ask the browser for an icon to represent a <video> element.
987 * This icon will be used if the Web page did not specify a poster attribute.
988 * @return Bitmap The icon or null if no such icon is available.
989 */
990 @Override
991 public Bitmap getDefaultVideoPoster() {
992 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700993 return mWebViewController.getDefaultVideoPoster();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700994 }
995 return null;
996 }
997
998 /**
999 * Ask the host application for a custom progress view to show while
1000 * a <video> is loading.
1001 * @return View The progress view.
1002 */
1003 @Override
1004 public View getVideoLoadingProgressView() {
1005 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001006 return mWebViewController.getVideoLoadingProgressView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001007 }
1008 return null;
1009 }
1010
1011 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00001012 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001013 if (mInForeground) {
Ben Murdoch8cad4132012-01-11 10:56:43 +00001014 mWebViewController.openFileChooser(uploadMsg, acceptType, capture);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001015 } else {
1016 uploadMsg.onReceiveValue(null);
1017 }
1018 }
1019
Vivek Sekharb54614f2014-05-01 19:03:37 -07001020 @Override
1021 public void showFileChooser(ValueCallback<String[]> uploadFilePaths, String acceptTypes,
1022 boolean capture) {
1023 if (mInForeground) {
1024 mWebViewController.showFileChooser(uploadFilePaths, acceptTypes, capture);
1025 } else {
1026 uploadFilePaths.onReceiveValue(null);
1027 }
1028 }
1029
Grace Kloba22ac16e2009-10-07 18:00:23 -07001030 /**
1031 * Deliver a list of already-visited URLs
1032 */
1033 @Override
1034 public void getVisitedHistory(final ValueCallback<String[]> callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001035 mWebViewController.getVisitedHistory(callback);
1036 }
Ben Murdoch8029a772010-11-16 11:58:21 +00001037
1038 @Override
1039 public void setupAutoFill(Message message) {
1040 // Prompt the user to set up their profile.
1041 final Message msg = message;
Michael Kolb14612442011-06-24 13:06:29 -07001042 AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
1043 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
Ben Murdoch1d676b62011-01-17 12:54:24 +00001044 Context.LAYOUT_INFLATER_SERVICE);
1045 final View layout = inflater.inflate(R.layout.setup_autofill_dialog, null);
1046
1047 builder.setView(layout)
1048 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1049 @Override
1050 public void onClick(DialogInterface dialog, int id) {
1051 CheckBox disableAutoFill = (CheckBox) layout.findViewById(
1052 R.id.setup_autofill_dialog_disable_autofill);
1053
1054 if (disableAutoFill.isChecked()) {
1055 // Disable autofill and show a toast with how to turn it on again.
John Reck35e9dd62011-04-25 09:01:54 -07001056 mSettings.setAutofillEnabled(false);
Michael Kolb14612442011-06-24 13:06:29 -07001057 Toast.makeText(mContext,
Ben Murdoch1d676b62011-01-17 12:54:24 +00001058 R.string.autofill_setup_dialog_negative_toast,
1059 Toast.LENGTH_LONG).show();
1060 } else {
1061 // Take user to the AutoFill profile editor. When they return,
1062 // we will send the message that we pass here which will trigger
1063 // the form to get filled out with their new profile.
1064 mWebViewController.setupAutoFill(msg);
1065 }
1066 }
1067 })
1068 .setNegativeButton(R.string.cancel, null)
1069 .show();
Ben Murdoch8029a772010-11-16 11:58:21 +00001070 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001071 };
1072
1073 // -------------------------------------------------------------------------
1074 // WebViewClient implementation for the sub window
1075 // -------------------------------------------------------------------------
1076
1077 // Subclass of WebViewClient used in subwindows to notify the main
1078 // WebViewClient of certain WebView activities.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001079 private static class SubWindowClient extends WebViewClient {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001080 // The main WebViewClient.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001081 private final WebViewClient mClient;
Michael Kolb8233fac2010-10-26 16:08:53 -07001082 private final WebViewController mController;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001083
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001084 SubWindowClient(WebViewClient client, WebViewController controller) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001085 mClient = client;
Michael Kolb8233fac2010-10-26 16:08:53 -07001086 mController = controller;
Leon Scroggins III211ba542010-04-19 13:21:13 -04001087 }
1088 @Override
1089 public void onPageStarted(WebView view, String url, Bitmap favicon) {
1090 // Unlike the others, do not call mClient's version, which would
1091 // change the progress bar. However, we do want to remove the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001092 // find or select dialog.
Michael Kolb8233fac2010-10-26 16:08:53 -07001093 mController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001094 }
1095 @Override
1096 public void doUpdateVisitedHistory(WebView view, String url,
1097 boolean isReload) {
1098 mClient.doUpdateVisitedHistory(view, url, isReload);
1099 }
1100 @Override
1101 public boolean shouldOverrideUrlLoading(WebView view, String url) {
1102 return mClient.shouldOverrideUrlLoading(view, url);
1103 }
1104 @Override
1105 public void onReceivedSslError(WebView view, SslErrorHandler handler,
1106 SslError error) {
1107 mClient.onReceivedSslError(view, handler, error);
1108 }
1109 @Override
Brian Carlstrom8862c1d2011-06-02 01:05:55 -07001110 public void onReceivedClientCertRequest(WebView view,
1111 ClientCertRequestHandler handler, String host_and_port) {
1112 mClient.onReceivedClientCertRequest(view, handler, host_and_port);
1113 }
1114 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -07001115 public void onReceivedHttpAuthRequest(WebView view,
1116 HttpAuthHandler handler, String host, String realm) {
1117 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
1118 }
1119 @Override
1120 public void onFormResubmission(WebView view, Message dontResend,
1121 Message resend) {
1122 mClient.onFormResubmission(view, dontResend, resend);
1123 }
1124 @Override
1125 public void onReceivedError(WebView view, int errorCode,
1126 String description, String failingUrl) {
1127 mClient.onReceivedError(view, errorCode, description, failingUrl);
1128 }
1129 @Override
1130 public boolean shouldOverrideKeyEvent(WebView view,
1131 android.view.KeyEvent event) {
1132 return mClient.shouldOverrideKeyEvent(view, event);
1133 }
1134 @Override
1135 public void onUnhandledKeyEvent(WebView view,
1136 android.view.KeyEvent event) {
1137 mClient.onUnhandledKeyEvent(view, event);
1138 }
1139 }
1140
1141 // -------------------------------------------------------------------------
1142 // WebChromeClient implementation for the sub window
1143 // -------------------------------------------------------------------------
1144
1145 private class SubWindowChromeClient extends WebChromeClient {
1146 // The main WebChromeClient.
1147 private final WebChromeClient mClient;
1148
1149 SubWindowChromeClient(WebChromeClient client) {
1150 mClient = client;
1151 }
1152 @Override
1153 public void onProgressChanged(WebView view, int newProgress) {
1154 mClient.onProgressChanged(view, newProgress);
1155 }
1156 @Override
1157 public boolean onCreateWindow(WebView view, boolean dialog,
1158 boolean userGesture, android.os.Message resultMsg) {
1159 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
1160 }
1161 @Override
1162 public void onCloseWindow(WebView window) {
1163 if (window != mSubView) {
1164 Log.e(LOGTAG, "Can't close the window");
1165 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001166 mWebViewController.dismissSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001167 }
1168 }
1169
1170 // -------------------------------------------------------------------------
1171
1172 // Construct a new tab
Michael Kolb7bcafde2011-05-09 13:55:59 -07001173 Tab(WebViewController wvcontroller, WebView w) {
John Reck1cf4b792011-07-26 10:22:22 -07001174 this(wvcontroller, w, null);
1175 }
1176
1177 Tab(WebViewController wvcontroller, Bundle state) {
1178 this(wvcontroller, null, state);
1179 }
1180
1181 Tab(WebViewController wvcontroller, WebView w, Bundle state) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001182 mWebViewController = wvcontroller;
Michael Kolb14612442011-06-24 13:06:29 -07001183 mContext = mWebViewController.getContext();
John Reck35e9dd62011-04-25 09:01:54 -07001184 mSettings = BrowserSettings.getInstance();
Michael Kolb14612442011-06-24 13:06:29 -07001185 mDataController = DataController.getInstance(mContext);
1186 mCurrentState = new PageState(mContext, w != null
John Reck847b5322011-04-14 17:02:18 -07001187 ? w.isPrivateBrowsingEnabled() : false);
Tarun Nainani8084c822014-06-25 13:38:06 -07001188 setTimeStamp();
Michael Kolb8233fac2010-10-26 16:08:53 -07001189 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001190 mInForeground = false;
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001191 mWebViewDestroyedByMemoryMonitor = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001192
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001193 mDownloadListener = new BrowserDownloadListener() {
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001194 public void onDownloadStart(String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001195 String contentDisposition, String mimetype, String referer,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001196 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001197 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001198 mimetype, referer, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001199 }
1200 };
1201
John Reck1cf4b792011-07-26 10:22:22 -07001202 mCaptureWidth = mContext.getResources().getDimensionPixelSize(
1203 R.dimen.tab_thumbnail_width);
1204 mCaptureHeight = mContext.getResources().getDimensionPixelSize(
1205 R.dimen.tab_thumbnail_height);
1206 updateShouldCaptureThumbnails();
1207 restoreState(state);
John Reck52be4782011-08-26 15:37:29 -07001208 if (getId() == -1) {
1209 mId = TabControl.getNextId();
1210 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001211 setWebView(w);
Michael Kolb9ef259a2011-07-12 15:33:08 -07001212 mHandler = new Handler() {
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001213 @Override
Michael Kolb9ef259a2011-07-12 15:33:08 -07001214 public void handleMessage(Message m) {
John Reck1cf4b792011-07-26 10:22:22 -07001215 switch (m.what) {
1216 case MSG_CAPTURE:
1217 capture();
1218 break;
1219 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001220 }
1221 };
Pankaj Garg1c13cab2015-05-12 11:52:17 -07001222
1223 mFirstPixelObservable = new Observable();
1224 mFirstPixelObservable.set(false);
1225 mTabHistoryUpdateObservable = new Observable();
John Reck1cf4b792011-07-26 10:22:22 -07001226 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001227
Michael Kolb72864272012-05-03 15:42:15 -07001228 public boolean shouldUpdateThumbnail() {
1229 return mUpdateThumbnail;
1230 }
1231
Mathew Inwoode09305e2011-09-02 12:03:26 +01001232 /**
1233 * This is used to get a new ID when the tab has been preloaded, before it is displayed and
1234 * added to TabControl. Preloaded tabs can be created before restoreInstanceState, leading
1235 * to overlapping IDs between the preloaded and restored tabs.
1236 */
1237 public void refreshIdAfterPreload() {
1238 mId = TabControl.getNextId();
1239 }
1240
John Reck1cf4b792011-07-26 10:22:22 -07001241 public void updateShouldCaptureThumbnails() {
1242 if (mWebViewController.shouldCaptureThumbnails()) {
1243 synchronized (Tab.this) {
1244 if (mCapture == null) {
1245 mCapture = Bitmap.createBitmap(mCaptureWidth, mCaptureHeight,
1246 Bitmap.Config.RGB_565);
Michael Kolbc3af0672011-08-09 10:24:41 -07001247 mCapture.eraseColor(Color.WHITE);
John Reck1cf4b792011-07-26 10:22:22 -07001248 if (mInForeground) {
1249 postCapture();
1250 }
1251 }
1252 }
1253 } else {
1254 synchronized (Tab.this) {
1255 mCapture = null;
1256 deleteThumbnail();
1257 }
1258 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001259 }
1260
Michael Kolb14612442011-06-24 13:06:29 -07001261 public void setController(WebViewController ctl) {
1262 mWebViewController = ctl;
John Reck1cf4b792011-07-26 10:22:22 -07001263 updateShouldCaptureThumbnails();
Michael Kolb14612442011-06-24 13:06:29 -07001264 }
1265
Michael Kolbc831b632011-05-11 09:30:34 -07001266 public long getId() {
1267 return mId;
1268 }
1269
Michael Kolb91911a22012-01-17 11:21:25 -08001270 void setWebView(WebView w) {
1271 setWebView(w, true);
1272 }
1273
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001274 public boolean isNativeActive(){
1275 if (mMainView == null)
1276 return false;
1277 return true;
1278 }
1279
1280 public void setTimeStamp(){
1281 Date d = new Date();
1282 timestamp = (new Timestamp(d.getTime()));
1283 }
1284
1285 public Timestamp getTimestamp() {
1286 return timestamp;
1287 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001288 /**
1289 * Sets the WebView for this tab, correctly removing the old WebView from
1290 * the container view.
1291 */
Michael Kolb91911a22012-01-17 11:21:25 -08001292 void setWebView(WebView w, boolean restore) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001293 if (mMainView == w) {
1294 return;
1295 }
Michael Kolba713ec82010-11-29 17:27:06 -08001296
Michael Kolba713ec82010-11-29 17:27:06 -08001297 mWebViewController.onSetWebView(this, w);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001298
John Reck1cf4b792011-07-26 10:22:22 -07001299 if (mMainView != null) {
John Reck8ee633f2011-08-09 16:00:35 -07001300 mMainView.setPictureListener(null);
John Reck1cf4b792011-07-26 10:22:22 -07001301 if (w != null) {
1302 syncCurrentState(w, null);
1303 } else {
Panos Thomasa9a5a582014-03-18 19:20:08 -07001304 mCurrentState = new PageState(mContext, mMainView.isPrivateBrowsingEnabled());
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001305
1306 if (mWebViewDestroyedByMemoryMonitor) {
1307 /*
1308 * If tab was destroyed as a result of the MemoryMonitor
1309 * then we need to restore the state properties
1310 * from the old WebView (mMainView)
1311 */
1312 syncCurrentState(mMainView, null);
1313 mWebViewDestroyedByMemoryMonitor = false;
1314 }
John Reck1cf4b792011-07-26 10:22:22 -07001315 }
1316 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001317 // set the new one
1318 mMainView = w;
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001319
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001320 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001321 if (mMainView != null) {
1322 mMainView.setWebViewClient(mWebViewClient);
1323 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001324 // Attach DownloadManager so that downloads can start in an active
1325 // or a non-active window. This can happen when going to a site that
1326 // does a redirect after a period of time. The user could have
1327 // switched to another tab while waiting for the download to start.
1328 mMainView.setDownloadListener(mDownloadListener);
John Reck8ee633f2011-08-09 16:00:35 -07001329 TabControl tc = mWebViewController.getTabControl();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001330 if (tc != null /*&& tc.getOnThumbnailUpdatedListener() != null*/) {
John Reck8ee633f2011-08-09 16:00:35 -07001331 mMainView.setPictureListener(this);
1332 }
Michael Kolb91911a22012-01-17 11:21:25 -08001333 if (restore && (mSavedState != null)) {
John Reck8b9bb8b2012-03-08 13:19:40 -08001334 restoreUserAgent();
John Reck6c2e2f32011-08-22 13:41:23 -07001335 WebBackForwardList restoredState
1336 = mMainView.restoreState(mSavedState);
1337 if (restoredState == null || restoredState.getSize() == 0) {
1338 Log.w(LOGTAG, "Failed to restore WebView state!");
1339 loadUrl(mCurrentState.mOriginalUrl, null);
1340 }
John Reck1cf4b792011-07-26 10:22:22 -07001341 mSavedState = null;
1342 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001343 }
1344 }
1345
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001346 public void destroyThroughMemoryMonitor() {
1347 mWebViewDestroyedByMemoryMonitor = true;
1348 destroy();
1349 }
1350
Grace Kloba22ac16e2009-10-07 18:00:23 -07001351 /**
1352 * Destroy the tab's main WebView and subWindow if any
1353 */
1354 void destroy() {
Tarun Nainani2c1dd7c2014-07-05 16:40:12 -07001355
Matthew Huib7f2e9c2014-04-16 11:12:37 -04001356 if (mPostponeDestroy) {
1357 mShouldDestroy = true;
1358 return;
1359 }
1360 mShouldDestroy = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001361 if (mMainView != null) {
1362 dismissSubWindow();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001363 // save the WebView to call destroy() after detach it from the tab
1364 WebView webView = mMainView;
Pankaj Garg1c13cab2015-05-12 11:52:17 -07001365 if (!mWebViewDestroyedByMemoryMonitor) {
1366 int[] ids = webView.getSnapshotIds();
1367 int currentTabIdx = mWebViewController.getTabControl().getCurrentPosition();
1368 for (int id : ids) {
1369 if (getTabIdxFromCaptureIdx(id) == currentTabIdx) {
1370 webView.deleteSnapshot(id);
1371 }
1372 }
1373 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001374 setWebView(null);
1375 webView.destroy();
1376 }
1377 }
1378
Matthew Huib7f2e9c2014-04-16 11:12:37 -04001379 private boolean mPostponeDestroy = false;
1380 private boolean mShouldDestroy = false;
1381
1382 public void postponeDestroy() {
1383 mPostponeDestroy = true;
1384 }
1385
1386 public void performPostponedDestroy() {
1387 mPostponeDestroy = false;
1388 if (mShouldDestroy) {
1389 destroy();
1390 }
1391 }
1392
Grace Kloba22ac16e2009-10-07 18:00:23 -07001393 /**
1394 * Remove the tab from the parent
1395 */
1396 void removeFromTree() {
1397 // detach the children
Michael Kolbc831b632011-05-11 09:30:34 -07001398 if (mChildren != null) {
1399 for(Tab t : mChildren) {
1400 t.setParent(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001401 }
1402 }
1403 // remove itself from the parent list
Michael Kolbc831b632011-05-11 09:30:34 -07001404 if (mParent != null) {
1405 mParent.mChildren.remove(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001406 }
Tarun Nainani8eb00912014-07-17 12:28:32 -07001407
1408 mCapture = null;
John Reck1cf4b792011-07-26 10:22:22 -07001409 deleteThumbnail();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001410 }
1411
1412 /**
1413 * Create a new subwindow unless a subwindow already exists.
1414 * @return True if a new subwindow was created. False if one already exists.
1415 */
1416 boolean createSubWindow() {
1417 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001418 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001419 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001420 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001421 mSubView.setWebChromeClient(new SubWindowChromeClient(
1422 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001423 // Set a different DownloadListener for the mSubView, since it will
1424 // just need to dismiss the mSubView, rather than close the Tab
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001425 mSubView.setDownloadListener(new BrowserDownloadListener() {
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001426 public void onDownloadStart(String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001427 String contentDisposition, String mimetype, String referer,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001428 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001429 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001430 contentDisposition, mimetype, referer, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001431 if (mSubView.copyBackForwardList().getSize() == 0) {
1432 // This subwindow was opened for the sole purpose of
1433 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001434 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001435 }
1436 }
1437 });
Michael Kolb14612442011-06-24 13:06:29 -07001438 mSubView.setOnCreateContextMenuListener(mWebViewController.getActivity());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001439 return true;
1440 }
1441 return false;
1442 }
1443
1444 /**
1445 * Dismiss the subWindow for the tab.
1446 */
1447 void dismissSubWindow() {
1448 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001449 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001450 mSubView.destroy();
1451 mSubView = null;
1452 mSubViewContainer = null;
1453 }
1454 }
1455
Grace Kloba22ac16e2009-10-07 18:00:23 -07001456
1457 /**
1458 * Set the parent tab of this tab.
1459 */
Michael Kolbc831b632011-05-11 09:30:34 -07001460 void setParent(Tab parent) {
John Reck52be4782011-08-26 15:37:29 -07001461 if (parent == this) {
1462 throw new IllegalStateException("Cannot set parent to self!");
1463 }
Michael Kolbc831b632011-05-11 09:30:34 -07001464 mParent = parent;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001465 // This tab may have been freed due to low memory. If that is the case,
Michael Kolbc831b632011-05-11 09:30:34 -07001466 // the parent tab id is already saved. If we are changing that id
Grace Kloba22ac16e2009-10-07 18:00:23 -07001467 // (most likely due to removing the parent tab) we must update the
Michael Kolbc831b632011-05-11 09:30:34 -07001468 // parent tab id in the saved Bundle.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001469 if (mSavedState != null) {
1470 if (parent == null) {
1471 mSavedState.remove(PARENTTAB);
1472 } else {
Michael Kolbc831b632011-05-11 09:30:34 -07001473 mSavedState.putLong(PARENTTAB, parent.getId());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001474 }
1475 }
John Reckb0a86db2011-05-24 14:05:58 -07001476
1477 // Sync the WebView useragent with the parent
1478 if (parent != null && mSettings.hasDesktopUseragent(parent.getWebView())
1479 != mSettings.hasDesktopUseragent(getWebView())) {
1480 mSettings.toggleDesktopUseragent(getWebView());
1481 }
John Reck52be4782011-08-26 15:37:29 -07001482
1483 if (parent != null && parent.getId() == getId()) {
1484 throw new IllegalStateException("Parent has same ID as child!");
1485 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001486 }
1487
1488 /**
Michael Kolbc831b632011-05-11 09:30:34 -07001489 * If this Tab was created through another Tab, then this method returns
1490 * that Tab.
1491 * @return the Tab parent or null
1492 */
1493 public Tab getParent() {
1494 return mParent;
1495 }
1496
1497 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001498 * When a Tab is created through the content of another Tab, then we
1499 * associate the Tabs.
1500 * @param child the Tab that was created from this Tab
1501 */
1502 void addChildTab(Tab child) {
Michael Kolbc831b632011-05-11 09:30:34 -07001503 if (mChildren == null) {
1504 mChildren = new Vector<Tab>();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001505 }
Michael Kolbc831b632011-05-11 09:30:34 -07001506 mChildren.add(child);
1507 child.setParent(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001508 }
1509
Michael Kolbc831b632011-05-11 09:30:34 -07001510 Vector<Tab> getChildren() {
1511 return mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001512 }
1513
1514 void resume() {
1515 if (mMainView != null) {
John Reck56c1fcf2011-08-17 10:15:16 -07001516 setupHwAcceleration(mMainView);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001517 mMainView.onResume();
1518 if (mSubView != null) {
1519 mSubView.onResume();
1520 }
1521 }
1522 }
1523
John Reck56c1fcf2011-08-17 10:15:16 -07001524 private void setupHwAcceleration(View web) {
1525 if (web == null) return;
1526 BrowserSettings settings = BrowserSettings.getInstance();
1527 if (settings.isHardwareAccelerated()) {
1528 web.setLayerType(View.LAYER_TYPE_NONE, null);
1529 } else {
1530 web.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
1531 }
1532 }
1533
Grace Kloba22ac16e2009-10-07 18:00:23 -07001534 void pause() {
1535 if (mMainView != null) {
1536 mMainView.onPause();
1537 if (mSubView != null) {
1538 mSubView.onPause();
1539 }
1540 }
1541 }
1542
1543 void putInForeground() {
John Reck8ee633f2011-08-09 16:00:35 -07001544 if (mInForeground) {
1545 return;
1546 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001547 mInForeground = true;
1548 resume();
Michael Kolb14612442011-06-24 13:06:29 -07001549 Activity activity = mWebViewController.getActivity();
1550 mMainView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001551 if (mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -07001552 mSubView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001553 }
Axesh R. Ajmerac6b5c322015-05-01 11:06:10 -07001554
Leon Scroggins1961ed22010-12-07 15:22:21 -05001555 mWebViewController.bookmarkedStatusHasChanged(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001556 }
1557
1558 void putInBackground() {
John Reck8ee633f2011-08-09 16:00:35 -07001559 if (!mInForeground) {
1560 return;
1561 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001562 mInForeground = false;
1563 pause();
1564 mMainView.setOnCreateContextMenuListener(null);
1565 if (mSubView != null) {
1566 mSubView.setOnCreateContextMenuListener(null);
1567 }
1568 }
1569
Michael Kolb8233fac2010-10-26 16:08:53 -07001570 boolean inForeground() {
1571 return mInForeground;
1572 }
1573
Grace Kloba22ac16e2009-10-07 18:00:23 -07001574 /**
1575 * Return the top window of this tab; either the subwindow if it is not
1576 * null or the main window.
1577 * @return The top window of this tab.
1578 */
1579 WebView getTopWindow() {
1580 if (mSubView != null) {
1581 return mSubView;
1582 }
1583 return mMainView;
1584 }
1585
1586 /**
1587 * Return the main window of this tab. Note: if a tab is freed in the
1588 * background, this can return null. It is only guaranteed to be
1589 * non-null for the current tab.
1590 * @return The main WebView of this tab.
1591 */
1592 WebView getWebView() {
1593 return mMainView;
1594 }
1595
Michael Kolba713ec82010-11-29 17:27:06 -08001596 void setViewContainer(View container) {
1597 mContainer = container;
1598 }
1599
Michael Kolb8233fac2010-10-26 16:08:53 -07001600 View getViewContainer() {
1601 return mContainer;
1602 }
1603
Grace Kloba22ac16e2009-10-07 18:00:23 -07001604 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001605 * Return whether private browsing is enabled for the main window of
1606 * this tab.
1607 * @return True if private browsing is enabled.
1608 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001609 boolean isPrivateBrowsingEnabled() {
John Reck502a3532011-08-16 14:21:46 -07001610 return mCurrentState.mIncognito;
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001611 }
1612
1613 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001614 * Return the subwindow of this tab or null if there is no subwindow.
1615 * @return The subwindow of this tab or null.
1616 */
1617 WebView getSubWebView() {
1618 return mSubView;
1619 }
1620
Michael Kolb1514bb72010-11-22 09:11:48 -08001621 void setSubWebView(WebView subView) {
1622 mSubView = subView;
1623 }
1624
Michael Kolb8233fac2010-10-26 16:08:53 -07001625 View getSubViewContainer() {
1626 return mSubViewContainer;
1627 }
1628
Michael Kolb1514bb72010-11-22 09:11:48 -08001629 void setSubViewContainer(View subViewContainer) {
1630 mSubViewContainer = subViewContainer;
1631 }
1632
Grace Kloba22ac16e2009-10-07 18:00:23 -07001633
1634 /**
1635 * @return The application id string
1636 */
1637 String getAppId() {
1638 return mAppId;
1639 }
1640
1641 /**
1642 * Set the application id string
1643 * @param id
1644 */
1645 void setAppId(String id) {
1646 mAppId = id;
1647 }
1648
Michael Kolbe28b3472011-08-04 16:54:31 -07001649 boolean closeOnBack() {
1650 return mCloseOnBack;
1651 }
1652
1653 void setCloseOnBack(boolean close) {
1654 mCloseOnBack = close;
1655 }
1656
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001657 boolean getDerivedFromIntent() {
1658 return mDerivedFromIntent;
1659 }
1660
1661 void setDerivedFromIntent(boolean derived) {
1662 mDerivedFromIntent = derived;
1663 }
1664
Grace Kloba22ac16e2009-10-07 18:00:23 -07001665 String getUrl() {
John Reck324d4402011-01-11 16:56:42 -08001666 return UrlUtils.filteredUrl(mCurrentState.mUrl);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001667 }
1668
Tarun Nainani8eb00912014-07-17 12:28:32 -07001669
1670 protected void onPageFinished() {
1671 mPageFinished = true;
1672 }
1673
1674 public boolean getPageFinishedStatus() {
1675 return mPageFinished;
1676 }
1677
John Reck49a603c2011-03-03 09:33:05 -08001678 String getOriginalUrl() {
John Reckdb22ec42011-06-29 11:31:24 -07001679 if (mCurrentState.mOriginalUrl == null) {
1680 return getUrl();
John Reck49a603c2011-03-03 09:33:05 -08001681 }
John Reckdb22ec42011-06-29 11:31:24 -07001682 return UrlUtils.filteredUrl(mCurrentState.mOriginalUrl);
John Reck49a603c2011-03-03 09:33:05 -08001683 }
1684
Grace Kloba22ac16e2009-10-07 18:00:23 -07001685 /**
John Reck30c714c2010-12-16 17:30:34 -08001686 * Get the title of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001687 */
1688 String getTitle() {
John Reck30c714c2010-12-16 17:30:34 -08001689 return mCurrentState.mTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001690 }
1691
1692 /**
John Reck30c714c2010-12-16 17:30:34 -08001693 * Get the favicon of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001694 */
1695 Bitmap getFavicon() {
John Reck1cf4b792011-07-26 10:22:22 -07001696 if (mCurrentState.mFavicon != null) {
1697 return mCurrentState.mFavicon;
1698 }
1699 return getDefaultFavicon(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001700 }
1701
John Recke969cc52010-12-21 17:24:43 -08001702 public boolean isBookmarkedSite() {
1703 return mCurrentState.mIsBookmarkedSite;
1704 }
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001705
Grace Kloba22ac16e2009-10-07 18:00:23 -07001706 /**
Steve Block08a6f0c2011-10-06 12:12:53 +01001707 * Sets the security state, clears the SSL certificate error and informs
1708 * the controller.
1709 */
Steve Block2466eff2011-10-03 15:33:09 +01001710 private void setSecurityState(SecurityState securityState) {
1711 mCurrentState.mSecurityState = securityState;
Steve Block08a6f0c2011-10-06 12:12:53 +01001712 mCurrentState.mSslCertificateError = null;
Steve Block2466eff2011-10-03 15:33:09 +01001713 mWebViewController.onUpdatedSecurityState(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001714 }
1715
1716 /**
Steve Block2466eff2011-10-03 15:33:09 +01001717 * @return The tab's security state.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001718 */
Steve Block2466eff2011-10-03 15:33:09 +01001719 SecurityState getSecurityState() {
1720 return mCurrentState.mSecurityState;
John Reck30c714c2010-12-16 17:30:34 -08001721 }
1722
Steve Block08a6f0c2011-10-06 12:12:53 +01001723 /**
1724 * Gets the SSL certificate error, if any, for the page's main resource.
1725 * This is only non-null when the security state is
1726 * SECURITY_STATE_BAD_CERTIFICATE.
1727 */
1728 SslError getSslCertificateError() {
1729 return mCurrentState.mSslCertificateError;
1730 }
1731
John Reck30c714c2010-12-16 17:30:34 -08001732 int getLoadProgress() {
1733 if (mInPageLoad) {
1734 return mPageLoadProgress;
1735 }
1736 return 100;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001737 }
1738
1739 /**
1740 * @return TRUE if onPageStarted is called while onPageFinished is not
1741 * called yet.
1742 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001743 boolean inPageLoad() {
1744 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001745 }
1746
Grace Kloba22ac16e2009-10-07 18:00:23 -07001747 /**
John Reck1cf4b792011-07-26 10:22:22 -07001748 * @return The Bundle with the tab's state if it can be saved, otherwise null
Grace Kloba22ac16e2009-10-07 18:00:23 -07001749 */
John Reck1cf4b792011-07-26 10:22:22 -07001750 public Bundle saveState() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001751 // If the WebView is null it means we ran low on memory and we already
1752 // stored the saved state in mSavedState.
1753 if (mMainView == null) {
John Reck1cf4b792011-07-26 10:22:22 -07001754 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001755 }
John Reck6c2e2f32011-08-22 13:41:23 -07001756
1757 if (TextUtils.isEmpty(mCurrentState.mUrl)) {
John Reck1cf4b792011-07-26 10:22:22 -07001758 return null;
John Reck24f18262011-06-17 14:47:20 -07001759 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001760
1761 mSavedState = new Bundle();
John Reck6c2e2f32011-08-22 13:41:23 -07001762 WebBackForwardList savedList = mMainView.saveState(mSavedState);
1763 if (savedList == null || savedList.getSize() == 0) {
1764 Log.w(LOGTAG, "Failed to save back/forward list for "
1765 + mCurrentState.mUrl);
1766 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001767
Michael Kolbc831b632011-05-11 09:30:34 -07001768 mSavedState.putLong(ID, mId);
John Reck30c714c2010-12-16 17:30:34 -08001769 mSavedState.putString(CURRURL, mCurrentState.mUrl);
1770 mSavedState.putString(CURRTITLE, mCurrentState.mTitle);
John Reck1cf4b792011-07-26 10:22:22 -07001771 mSavedState.putBoolean(INCOGNITO, mMainView.isPrivateBrowsingEnabled());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001772 if (mAppId != null) {
1773 mSavedState.putString(APPID, mAppId);
1774 }
Michael Kolbe28b3472011-08-04 16:54:31 -07001775 mSavedState.putBoolean(CLOSEFLAG, mCloseOnBack);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001776 // Remember the parent tab so the relationship can be restored.
Michael Kolbc831b632011-05-11 09:30:34 -07001777 if (mParent != null) {
1778 mSavedState.putLong(PARENTTAB, mParent.mId);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001779 }
John Reckb0a86db2011-05-24 14:05:58 -07001780 mSavedState.putBoolean(USERAGENT,
1781 mSettings.hasDesktopUseragent(getWebView()));
John Reck1cf4b792011-07-26 10:22:22 -07001782 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001783 }
1784
1785 /*
1786 * Restore the state of the tab.
1787 */
John Reck1cf4b792011-07-26 10:22:22 -07001788 private void restoreState(Bundle b) {
1789 mSavedState = b;
1790 if (mSavedState == null) {
1791 return;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001792 }
1793 // Restore the internal state even if the WebView fails to restore.
1794 // This will maintain the app id, original url and close-on-exit values.
Michael Kolbc831b632011-05-11 09:30:34 -07001795 mId = b.getLong(ID);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001796 mAppId = b.getString(APPID);
Michael Kolbe28b3472011-08-04 16:54:31 -07001797 mCloseOnBack = b.getBoolean(CLOSEFLAG);
John Reck8b9bb8b2012-03-08 13:19:40 -08001798 restoreUserAgent();
John Reck1cf4b792011-07-26 10:22:22 -07001799 String url = b.getString(CURRURL);
1800 String title = b.getString(CURRTITLE);
1801 boolean incognito = b.getBoolean(INCOGNITO);
1802 mCurrentState = new PageState(mContext, incognito, url, null);
1803 mCurrentState.mTitle = title;
1804 synchronized (Tab.this) {
1805 if (mCapture != null) {
John Reck4eadc342011-10-31 14:04:10 -07001806 DataController.getInstance(mContext).loadThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07001807 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001808 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001809 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04001810
John Reck8b9bb8b2012-03-08 13:19:40 -08001811 private void restoreUserAgent() {
1812 if (mMainView == null || mSavedState == null) {
1813 return;
1814 }
1815 if (mSavedState.getBoolean(USERAGENT)
1816 != mSettings.hasDesktopUseragent(mMainView)) {
1817 mSettings.toggleDesktopUseragent(mMainView);
1818 }
1819 }
1820
Leon Scroggins1961ed22010-12-07 15:22:21 -05001821 public void updateBookmarkedStatus() {
John Recke969cc52010-12-21 17:24:43 -08001822 mDataController.queryBookmarkStatus(getUrl(), mIsBookmarkCallback);
Leon Scroggins1961ed22010-12-07 15:22:21 -05001823 }
1824
John Recke969cc52010-12-21 17:24:43 -08001825 private DataController.OnQueryUrlIsBookmark mIsBookmarkCallback
1826 = new DataController.OnQueryUrlIsBookmark() {
1827 @Override
1828 public void onQueryUrlIsBookmark(String url, boolean isBookmark) {
1829 if (mCurrentState.mUrl.equals(url)) {
1830 mCurrentState.mIsBookmarkedSite = isBookmark;
1831 mWebViewController.bookmarkedStatusHasChanged(Tab.this);
1832 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001833 }
John Recke969cc52010-12-21 17:24:43 -08001834 };
Michael Kolb1acef692011-03-08 14:12:06 -08001835
Michael Kolbeb95db42011-03-03 10:38:40 -08001836 public Bitmap getScreenshot() {
John Reck1cf4b792011-07-26 10:22:22 -07001837 synchronized (Tab.this) {
1838 return mCapture;
1839 }
Michael Kolbeb95db42011-03-03 10:38:40 -08001840 }
1841
John Reck541f55a2011-06-07 16:34:43 -07001842 public boolean isSnapshot() {
John Reck541f55a2011-06-07 16:34:43 -07001843 return false;
1844 }
1845
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001846 private static class SaveCallback implements ValueCallback<String> {
1847 boolean onReceiveValueCalled = false;
1848 private String mPath;
John Reck68234a92012-04-19 15:27:12 -07001849
1850 @Override
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001851 public void onReceiveValue(String path) {
1852 this.onReceiveValueCalled = true;
1853 this.mPath = path;
John Reck68234a92012-04-19 15:27:12 -07001854 synchronized (this) {
1855 notifyAll();
John Reck8cc92352011-07-06 17:41:52 -07001856 }
John Reck541f55a2011-06-07 16:34:43 -07001857 }
John Reck68234a92012-04-19 15:27:12 -07001858
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001859 public String getPath() {
1860 return mPath;
1861 }
John Reck68234a92012-04-19 15:27:12 -07001862 }
1863
1864 /**
1865 * Must be called on the UI thread
1866 */
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001867 public ContentValues createSnapshotValues(Bitmap bm) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001868 WebView web = getWebView();
John Reck68234a92012-04-19 15:27:12 -07001869 if (web == null) return null;
John Reckd8c74522011-06-14 08:45:00 -07001870 ContentValues values = new ContentValues();
1871 values.put(Snapshots.TITLE, mCurrentState.mTitle);
1872 values.put(Snapshots.URL, mCurrentState.mUrl);
John Reck68234a92012-04-19 15:27:12 -07001873 values.put(Snapshots.BACKGROUND, web.getPageBackgroundColor());
John Reck8cc92352011-07-06 17:41:52 -07001874 values.put(Snapshots.DATE_CREATED, System.currentTimeMillis());
1875 values.put(Snapshots.FAVICON, compressBitmap(getFavicon()));
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001876 values.put(Snapshots.THUMBNAIL, compressBitmap(bm));
John Reckd8c74522011-06-14 08:45:00 -07001877 return values;
John Reck541f55a2011-06-07 16:34:43 -07001878 }
1879
John Reck68234a92012-04-19 15:27:12 -07001880 /**
1881 * Probably want to call this on a background thread
1882 */
1883 public boolean saveViewState(ContentValues values) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001884 WebView web = getWebView();
John Reck68234a92012-04-19 15:27:12 -07001885 if (web == null) return false;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001886 String filename = UUID.randomUUID().toString();
John Reck68234a92012-04-19 15:27:12 -07001887 SaveCallback callback = new SaveCallback();
John Reck68234a92012-04-19 15:27:12 -07001888 try {
John Reck68234a92012-04-19 15:27:12 -07001889 synchronized (callback) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001890 web.saveViewState(filename, callback);
1891 callback.wait();
John Reck68234a92012-04-19 15:27:12 -07001892 }
John Reck68234a92012-04-19 15:27:12 -07001893 } catch (Exception e) {
1894 Log.w(LOGTAG, "Failed to save view state", e);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001895 String path = callback.getPath();
1896 if (path != null) {
1897 File file = mContext.getFileStreamPath(path);
1898 if (file.exists() && !file.delete()) {
1899 file.deleteOnExit();
1900 }
John Reck68234a92012-04-19 15:27:12 -07001901 }
1902 return false;
1903 }
Tarun Nainani8eb00912014-07-17 12:28:32 -07001904
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001905 String path = callback.getPath();
Tarun Nainani8eb00912014-07-17 12:28:32 -07001906 // could be that saving of file failed
1907 if (path == null) {
1908 return false;
1909 }
1910
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001911 File savedFile = new File(path);
1912 if (!savedFile.exists()) {
1913 return false;
John Reck68234a92012-04-19 15:27:12 -07001914 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001915 values.put(Snapshots.VIEWSTATE_PATH, path.substring(path.lastIndexOf('/') + 1));
1916 values.put(Snapshots.VIEWSTATE_SIZE, savedFile.length());
John Reck68234a92012-04-19 15:27:12 -07001917 return true;
1918 }
1919
John Reck8cc92352011-07-06 17:41:52 -07001920 public byte[] compressBitmap(Bitmap bitmap) {
1921 if (bitmap == null) {
1922 return null;
1923 }
1924 ByteArrayOutputStream stream = new ByteArrayOutputStream();
1925 bitmap.compress(CompressFormat.PNG, 100, stream);
1926 return stream.toByteArray();
1927 }
1928
John Reck26b18322011-06-21 13:08:58 -07001929 public void loadUrl(String url, Map<String, String> headers) {
1930 if (mMainView != null) {
Michael Kolba53c9892011-10-05 13:31:40 -07001931 mPageLoadProgress = INITIAL_PROGRESS;
Michael Kolb14612442011-06-24 13:06:29 -07001932 mCurrentState = new PageState(mContext, false, url, null);
John Reck26b18322011-06-21 13:08:58 -07001933 mMainView.loadUrl(url, headers);
1934 }
1935 }
1936
John Reck38b39652012-06-05 09:22:59 -07001937 public void disableUrlOverridingForLoad() {
1938 mDisableOverrideUrlLoading = true;
1939 }
1940
Michael Kolb9ef259a2011-07-12 15:33:08 -07001941 protected void capture() {
Vivek Sekhar6bdf6452015-05-12 17:38:45 -07001942 boolean returnEmptyCapture = false;
1943 if (mMainView == null || mCapture == null || !mMainView.isReady())
1944 returnEmptyCapture = true;
John Reck4eadc342011-10-31 14:04:10 -07001945 if (mMainView.getContentWidth() <= 0 || mMainView.getContentHeight() <= 0) {
Vivek Sekhar6bdf6452015-05-12 17:38:45 -07001946 returnEmptyCapture = true;
John Reck4eadc342011-10-31 14:04:10 -07001947 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07001948
Vivek Sekhar6bdf6452015-05-12 17:38:45 -07001949 if (returnEmptyCapture || !mFirstVisualPixelPainted || mMainView.isShowingCrashView()) {
Pankaj Garg79878492015-04-01 14:48:21 -07001950 mCapture = Bitmap.createBitmap(
1951 mCaptureWidth,
1952 mCaptureHeight,
1953 Bitmap.Config.RGB_565);
1954 mCapture.eraseColor(Color.WHITE);
1955
1956 mHandler.removeMessages(MSG_CAPTURE);
1957
1958 TabControl tc = mWebViewController.getTabControl();
1959 if (tc != null) {
1960 OnThumbnailUpdatedListener updateListener
1961 = tc.getOnThumbnailUpdatedListener();
1962 if (updateListener != null) {
1963 updateListener.onThumbnailUpdated(this);
1964 }
1965 }
1966 return;
1967 }
1968
Tarun Nainaniea28dde2014-08-27 17:25:09 -07001969 mMainView
1970 .getContentBitmapAsync(
1971 (float) mCaptureWidth / mMainView.getWidth(),
1972 new Rect(),
1973 new ValueCallback<Bitmap>() {
1974 @Override
1975 public void onReceiveValue(Bitmap bitmap) {
1976 onCaptureCallback(bitmap);
1977 }});
1978 }
1979
1980 private void onCaptureCallback(Bitmap bitmap) {
1981 if (mCapture == null || bitmap == null)
1982 return;
1983
Michael Kolb9ef259a2011-07-12 15:33:08 -07001984 Canvas c = new Canvas(mCapture);
Tarun Nainaniea28dde2014-08-27 17:25:09 -07001985 mCapture.eraseColor(Color.WHITE);
1986 c.drawBitmap(bitmap, 0, 0, null);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001987
Michael Kolba3194d02011-09-07 11:23:51 -07001988 // manually anti-alias the edges for the tilt
1989 c.drawRect(0, 0, 1, mCapture.getHeight(), sAlphaPaint);
1990 c.drawRect(mCapture.getWidth() - 1, 0, mCapture.getWidth(),
1991 mCapture.getHeight(), sAlphaPaint);
1992 c.drawRect(0, 0, mCapture.getWidth(), 1, sAlphaPaint);
1993 c.drawRect(0, mCapture.getHeight() - 1, mCapture.getWidth(),
1994 mCapture.getHeight(), sAlphaPaint);
Dianne Hackborn43cfe8a2011-08-02 16:59:35 -07001995 c.setBitmap(null);
John Reck8ee633f2011-08-09 16:00:35 -07001996 mHandler.removeMessages(MSG_CAPTURE);
John Reck1cf4b792011-07-26 10:22:22 -07001997 persistThumbnail();
John Reck8ee633f2011-08-09 16:00:35 -07001998 TabControl tc = mWebViewController.getTabControl();
1999 if (tc != null) {
2000 OnThumbnailUpdatedListener updateListener
2001 = tc.getOnThumbnailUpdatedListener();
2002 if (updateListener != null) {
2003 updateListener.onThumbnailUpdated(this);
2004 }
2005 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07002006 }
2007
2008 @Override
2009 public void onNewPicture(WebView view, Picture picture) {
John Reck1cf4b792011-07-26 10:22:22 -07002010 postCapture();
2011 }
2012
2013 private void postCapture() {
Michael Kolb9ef259a2011-07-12 15:33:08 -07002014 if (!mHandler.hasMessages(MSG_CAPTURE)) {
2015 mHandler.sendEmptyMessageDelayed(MSG_CAPTURE, CAPTURE_DELAY);
2016 }
2017 }
2018
John Reckef654f12011-07-12 16:42:08 -07002019 public boolean canGoBack() {
2020 return mMainView != null ? mMainView.canGoBack() : false;
2021 }
2022
2023 public boolean canGoForward() {
2024 return mMainView != null ? mMainView.canGoForward() : false;
2025 }
2026
2027 public void goBack() {
2028 if (mMainView != null) {
2029 mMainView.goBack();
2030 }
2031 }
2032
2033 public void goForward() {
2034 if (mMainView != null) {
2035 mMainView.goForward();
2036 }
2037 }
2038
John Reck1cf4b792011-07-26 10:22:22 -07002039 protected void persistThumbnail() {
John Reck4eadc342011-10-31 14:04:10 -07002040 DataController.getInstance(mContext).saveThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07002041 }
2042
2043 protected void deleteThumbnail() {
John Reck4eadc342011-10-31 14:04:10 -07002044 DataController.getInstance(mContext).deleteThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07002045 }
2046
John Reck4eadc342011-10-31 14:04:10 -07002047 void updateCaptureFromBlob(byte[] blob) {
John Reck1cf4b792011-07-26 10:22:22 -07002048 synchronized (Tab.this) {
2049 if (mCapture == null) {
2050 return;
2051 }
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002052 ByteBuffer buffer = ByteBuffer.wrap(blob);
Michael Kolbd837a112011-08-09 14:00:18 -07002053 try {
2054 mCapture.copyPixelsFromBuffer(buffer);
2055 } catch (RuntimeException rex) {
2056 Log.e(LOGTAG, "Load capture has mismatched sizes; buffer: "
2057 + buffer.capacity() + " blob: " + blob.length
2058 + "capture: " + mCapture.getByteCount());
2059 throw rex;
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002060 }
John Reck1cf4b792011-07-26 10:22:22 -07002061 }
2062 }
2063
John Reck52be4782011-08-26 15:37:29 -07002064 @Override
2065 public String toString() {
2066 StringBuilder builder = new StringBuilder(100);
2067 builder.append(mId);
2068 builder.append(") has parent: ");
2069 if (getParent() != null) {
2070 builder.append("true[");
2071 builder.append(getParent().getId());
2072 builder.append("]");
2073 } else {
2074 builder.append("false");
2075 }
2076 builder.append(", incog: ");
2077 builder.append(isPrivateBrowsingEnabled());
2078 if (!isPrivateBrowsingEnabled()) {
2079 builder.append(", title: ");
2080 builder.append(getTitle());
2081 builder.append(", url: ");
2082 builder.append(getUrl());
2083 }
2084 return builder.toString();
2085 }
2086
Steve Block4895b012011-10-03 16:26:46 +01002087 private void handleProceededAfterSslError(SslError error) {
2088 if (error.getUrl().equals(mCurrentState.mUrl)) {
2089 // The security state should currently be SECURITY_STATE_SECURE.
2090 setSecurityState(SecurityState.SECURITY_STATE_BAD_CERTIFICATE);
Steve Block08a6f0c2011-10-06 12:12:53 +01002091 mCurrentState.mSslCertificateError = error;
Steve Block4895b012011-10-03 16:26:46 +01002092 } else if (getSecurityState() == SecurityState.SECURITY_STATE_SECURE) {
Steve Block08a6f0c2011-10-06 12:12:53 +01002093 // The page's main resource is secure and this error is for a
2094 // sub-resource.
Steve Block4895b012011-10-03 16:26:46 +01002095 setSecurityState(SecurityState.SECURITY_STATE_MIXED);
2096 }
2097 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002098}