blob: 1bcf4a915f21a79246a9d1531f3f27f9c34831f1 [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();
Pankaj Garg62bc7912015-04-14 16:08:59 -0700324 // Need re-enable FullScreenMode on Page navigation if needed
325 if (BrowserSettings.getInstance().useFullscreen()){
326 Controller controller = (Controller) mWebViewController;
327 BaseUi ui = (BaseUi) controller.getUi();
328 ui.forceDisableFullscreenMode(false);
329 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700330 // If we start a touch icon load and then load a new page, we don't
331 // want to cancel the current touch icon loader. But, we do want to
332 // create a new one when the touch icon url is known.
333 if (mTouchIconLoader != null) {
334 mTouchIconLoader.mTab = null;
335 mTouchIconLoader = null;
336 }
337
Grace Kloba22ac16e2009-10-07 18:00:23 -0700338 // finally update the UI in the activity if it is in the foreground
John Reck324d4402011-01-11 16:56:42 -0800339 mWebViewController.onPageStarted(Tab.this, view, favicon);
Leon Scroggins4cd97792010-12-03 15:31:56 -0500340
John Recke969cc52010-12-21 17:24:43 -0800341 updateBookmarkedStatus();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700342 }
343
344 @Override
345 public void onPageFinished(WebView view, String url) {
John Reck38b39652012-06-05 09:22:59 -0700346 mDisableOverrideUrlLoading = false;
John Reck5b691842010-11-29 11:21:13 -0800347 if (!isPrivateBrowsingEnabled()) {
348 LogTag.logPageFinishedLoading(
349 url, SystemClock.uptimeMillis() - mLoadStartTime);
350 }
John Reck1cf4b792011-07-26 10:22:22 -0700351 syncCurrentState(view, url);
John Reck324d4402011-01-11 16:56:42 -0800352 mWebViewController.onPageFinished(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700353 }
354
Pankaj Garg79878492015-04-01 14:48:21 -0700355 @Override
356 public void onFirstVisualPixel(WebView view) {
357 mFirstVisualPixelPainted = true;
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700358 mFirstPixelObservable.set(true);
Pankaj Garg79878492015-04-01 14:48:21 -0700359 }
360
Grace Kloba22ac16e2009-10-07 18:00:23 -0700361 // return true if want to hijack the url to let another app to handle it
362 @Override
363 public boolean shouldOverrideUrlLoading(WebView view, String url) {
John Reck38b39652012-06-05 09:22:59 -0700364 if (!mDisableOverrideUrlLoading && mInForeground) {
Michael Kolb18eb3772010-12-10 14:29:51 -0800365 return mWebViewController.shouldOverrideUrlLoading(Tab.this,
366 view, url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700367 } else {
368 return false;
369 }
370 }
371
Vivek Sekharb991edb2014-12-17 18:18:07 -0800372 @Override
373 public boolean shouldDownloadFavicon(WebView view, String url) {
374 return true;
375 }
376
Grace Kloba22ac16e2009-10-07 18:00:23 -0700377 /**
Steve Block2466eff2011-10-03 15:33:09 +0100378 * Updates the security state. This method is called when we discover
379 * another resource to be loaded for this page (for example,
380 * javascript). While we update the security state, we do not update
381 * the lock icon until we are done loading, as it is slightly more
382 * secure this way.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700383 */
384 @Override
385 public void onLoadResource(WebView view, String url) {
386 if (url != null && url.length() > 0) {
387 // It is only if the page claims to be secure that we may have
Steve Block2466eff2011-10-03 15:33:09 +0100388 // to update the security state:
389 if (mCurrentState.mSecurityState == SecurityState.SECURITY_STATE_SECURE) {
390 // If NOT a 'safe' url, change the state to mixed content!
Grace Kloba22ac16e2009-10-07 18:00:23 -0700391 if (!(URLUtil.isHttpsUrl(url) || URLUtil.isDataUrl(url)
392 || URLUtil.isAboutUrl(url))) {
Steve Block2466eff2011-10-03 15:33:09 +0100393 mCurrentState.mSecurityState = SecurityState.SECURITY_STATE_MIXED;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700394 }
395 }
396 }
397 }
398
399 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700400 * Show a dialog informing the user of the network error reported by
401 * WebCore if it is in the foreground.
402 */
403 @Override
404 public void onReceivedError(WebView view, int errorCode,
405 String description, String failingUrl) {
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800406 // Used for the syncCurrentState to use
407 // the failing url instead of using webview url
408 mReceivedError = true;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700409 }
410
411 /**
412 * Check with the user if it is ok to resend POST data as the page they
413 * are trying to navigate to is the result of a POST.
414 */
415 @Override
416 public void onFormResubmission(WebView view, final Message dontResend,
417 final Message resend) {
418 if (!mInForeground) {
419 dontResend.sendToTarget();
420 return;
421 }
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500422 if (mDontResend != null) {
423 Log.w(LOGTAG, "onFormResubmission should not be called again "
424 + "while dialog is still up");
425 dontResend.sendToTarget();
426 return;
427 }
428 mDontResend = dontResend;
429 mResend = resend;
Michael Kolb14612442011-06-24 13:06:29 -0700430 new AlertDialog.Builder(mContext).setTitle(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700431 R.string.browserFrameFormResubmitLabel).setMessage(
432 R.string.browserFrameFormResubmitMessage)
433 .setPositiveButton(R.string.ok,
434 new DialogInterface.OnClickListener() {
435 public void onClick(DialogInterface dialog,
436 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500437 if (mResend != null) {
438 mResend.sendToTarget();
439 mResend = null;
440 mDontResend = null;
441 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700442 }
443 }).setNegativeButton(R.string.cancel,
444 new DialogInterface.OnClickListener() {
445 public void onClick(DialogInterface dialog,
446 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500447 if (mDontResend != null) {
448 mDontResend.sendToTarget();
449 mResend = null;
450 mDontResend = null;
451 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700452 }
453 }).setOnCancelListener(new OnCancelListener() {
454 public void onCancel(DialogInterface dialog) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500455 if (mDontResend != null) {
456 mDontResend.sendToTarget();
457 mResend = null;
458 mDontResend = null;
459 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700460 }
461 }).show();
462 }
463
464 /**
465 * Insert the url into the visited history database.
466 * @param url The url to be inserted.
467 * @param isReload True if this url is being reloaded.
468 * FIXME: Not sure what to do when reloading the page.
469 */
470 @Override
471 public void doUpdateVisitedHistory(WebView view, String url,
472 boolean isReload) {
John Reck324d4402011-01-11 16:56:42 -0800473 mWebViewController.doUpdateVisitedHistory(Tab.this, isReload);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700474 }
475
476 /**
477 * Displays SSL error(s) dialog to the user.
478 */
479 @Override
480 public void onReceivedSslError(final WebView view,
481 final SslErrorHandler handler, final SslError error) {
482 if (!mInForeground) {
483 handler.cancel();
Steve Block2466eff2011-10-03 15:33:09 +0100484 setSecurityState(SecurityState.SECURITY_STATE_NOT_SECURE);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700485 return;
486 }
John Reck35e9dd62011-04-25 09:01:54 -0700487 if (mSettings.showSecurityWarnings()) {
John Reckcb28b2c2011-08-26 17:39:44 -0700488 new AlertDialog.Builder(mContext)
489 .setTitle(R.string.security_warning)
490 .setMessage(R.string.ssl_warnings_header)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200491 .setIconAttribute(android.R.attr.alertDialogIcon)
John Reckcb28b2c2011-08-26 17:39:44 -0700492 .setPositiveButton(R.string.ssl_continue,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700493 new DialogInterface.OnClickListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700494 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700495 public void onClick(DialogInterface dialog,
496 int whichButton) {
497 handler.proceed();
Steve Block4895b012011-10-03 16:26:46 +0100498 handleProceededAfterSslError(error);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700499 }
John Reckcb28b2c2011-08-26 17:39:44 -0700500 })
501 .setNeutralButton(R.string.view_certificate,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700502 new DialogInterface.OnClickListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700503 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700504 public void onClick(DialogInterface dialog,
505 int whichButton) {
John Reckcb28b2c2011-08-26 17:39:44 -0700506 mWebViewController.showSslCertificateOnError(
507 view, handler, error);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700508 }
John Reckcb28b2c2011-08-26 17:39:44 -0700509 })
510 .setNegativeButton(R.string.ssl_go_back,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700511 new DialogInterface.OnClickListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700512 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700513 public void onClick(DialogInterface dialog,
514 int whichButton) {
John Reck30c714c2010-12-16 17:30:34 -0800515 dialog.cancel();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700516 }
John Reckcb28b2c2011-08-26 17:39:44 -0700517 })
518 .setOnCancelListener(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700519 new DialogInterface.OnCancelListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700520 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700521 public void onCancel(DialogInterface dialog) {
522 handler.cancel();
Steve Block2466eff2011-10-03 15:33:09 +0100523 setSecurityState(SecurityState.SECURITY_STATE_NOT_SECURE);
John Reck30c714c2010-12-16 17:30:34 -0800524 mWebViewController.onUserCanceledSsl(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700525 }
John Reckcb28b2c2011-08-26 17:39:44 -0700526 })
527 .show();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700528 } else {
529 handler.proceed();
530 }
531 }
532
533 /**
Steve Block4895b012011-10-03 16:26:46 +0100534 * Called when an SSL error occurred while loading a resource, but the
535 * WebView but chose to proceed anyway based on a decision retained
536 * from a previous response to onReceivedSslError(). We update our
537 * security state to reflect this.
538 */
539 @Override
540 public void onProceededAfterSslError(WebView view, SslError error) {
541 handleProceededAfterSslError(error);
542 }
543
544 /**
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700545 * Displays client certificate request to the user.
546 */
547 @Override
548 public void onReceivedClientCertRequest(final WebView view,
549 final ClientCertRequestHandler handler, final String host_and_port) {
550 if (!mInForeground) {
551 handler.ignore();
552 return;
553 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700554 int colon = host_and_port.lastIndexOf(':');
555 String host;
556 int port;
557 if (colon == -1) {
558 host = host_and_port;
559 port = -1;
560 } else {
561 String portString = host_and_port.substring(colon + 1);
562 try {
563 port = Integer.parseInt(portString);
564 host = host_and_port.substring(0, colon);
565 } catch (NumberFormatException e) {
566 host = host_and_port;
567 port = -1;
568 }
569 }
Michael Kolb14612442011-06-24 13:06:29 -0700570 KeyChain.choosePrivateKeyAlias(
571 mWebViewController.getActivity(), new KeyChainAliasCallback() {
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700572 @Override public void alias(String alias) {
573 if (alias == null) {
574 handler.cancel();
575 return;
576 }
Michael Kolb14612442011-06-24 13:06:29 -0700577 new KeyChainLookup(mContext, handler, alias).execute();
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700578 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700579 }, null, null, host, port, null);
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700580 }
581
582 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700583 * Handles an HTTP authentication request.
584 *
585 * @param handler The authentication handler
586 * @param host The host
587 * @param realm The realm
588 */
589 @Override
590 public void onReceivedHttpAuthRequest(WebView view,
591 final HttpAuthHandler handler, final String host,
592 final String realm) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700593 mWebViewController.onReceivedHttpAuthRequest(Tab.this, view, handler, host, realm);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700594 }
595
596 @Override
John Reck438bf462011-01-12 18:11:46 -0800597 public WebResourceResponse shouldInterceptRequest(WebView view,
598 String url) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800599 //intercept if opening a new incognito tab - show the incognito welcome page
Vivek Sekhared791da2015-02-22 12:39:05 -0800600 if (url.startsWith("chrome://incognito")) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800601 Resources resourceHandle = mContext.getResources();
602 InputStream inStream = resourceHandle.openRawResource(
Bijan Amirzada41242f22014-03-21 12:12:18 -0700603 com.android.browser.R.raw.incognito_mode_start_page);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800604 return new WebResourceResponse("text/html", "utf8", inStream);
605 }
kaiyiz6e5b3e02013-08-19 20:02:01 +0800606 WebResourceResponse res;
607 if (MyNavigationUtil.MY_NAVIGATION.equals(url)) {
608 res = MyNavigationProvider.shouldInterceptRequest(mContext, url);
609 } else {
610 res = HomeProvider.shouldInterceptRequest(mContext, url);
611 }
John Reck438bf462011-01-12 18:11:46 -0800612 return res;
613 }
614
615 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700616 public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
617 if (!mInForeground) {
618 return false;
619 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700620 return mWebViewController.shouldOverrideKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700621 }
622
623 @Override
624 public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700625 if (!mInForeground) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700626 return;
627 }
John Reck997b1b72012-04-19 18:08:25 -0700628 if (!mWebViewController.onUnhandledKeyEvent(event)) {
629 super.onUnhandledKeyEvent(view, event);
630 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700631 }
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700632
633 @Override
634 public void beforeNavigation(WebView view, String url) {
635 if (isPrivateBrowsingEnabled()) {
636 return;
637 }
638
639 if (!mFirstVisualPixelPainted) {
640 return;
641 }
642
643 final int idx = view.copyBackForwardList().getCurrentIndex();
644 boolean bitmapExists = view.hasSnapshot(idx);
645
646 int progress = 100;
647 Controller controller = (Controller)mWebViewController;
648 UI ui = controller.getUi();
649 if (ui instanceof BaseUi) {
650 BaseUi baseUi = (BaseUi) ui;
651 TitleBar titleBar = baseUi.getTitleBar();
652 progress = titleBar.getProgressView().getProgressPercent();
653 }
654
655 if (bitmapExists && progress < 85) {
656 return;
657 }
658
659 int index = getCaptureIndex(view.getLastCommittedHistoryIndex());
660 view.captureSnapshot(index , null);
661 }
662
663 @Override
664 public void onHistoryItemCommit(WebView view, int index) {
665 mTabHistoryUpdateObservable.set(index);
666 int maxIdx = view.copyBackForwardList().getSize();
667 int[] ids = view.getSnapshotIds();
668 int currentTabIdx = mWebViewController.getTabControl().getCurrentPosition();
669 for (int id : ids) {
670 if (getTabIdxFromCaptureIdx(id) == currentTabIdx &&
671 getNavIdxFromCaptureIdx(id) >= maxIdx) {
672 view.deleteSnapshot(id);
673 }
674 }
675 }
Pankaj Garg62bc7912015-04-14 16:08:59 -0700676
677 @Override
678 public void onKeyboardStateChange(boolean popup) {
679 if (BrowserSettings.getInstance().useFullscreen()) {
680 Controller controller = (Controller) mWebViewController;
681 BaseUi ui = (BaseUi) controller.getUi();
682 ui.forceDisableFullscreenMode(popup);
683 }
684 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700685 };
686
John Reck1cf4b792011-07-26 10:22:22 -0700687 private void syncCurrentState(WebView view, String url) {
688 // Sync state (in case of stop/timeout)
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800689
690 if (mReceivedError) {
691 mCurrentState.mUrl = url;
692 mCurrentState.mOriginalUrl = url;
693 } else {
694 mCurrentState.mUrl = view.getUrl();
695 mCurrentState.mOriginalUrl = view.getOriginalUrl();
696 mCurrentState.mFavicon = view.getFavicon();
697 }
698
John Reck1cf4b792011-07-26 10:22:22 -0700699 if (mCurrentState.mUrl == null) {
John Reck8bcafc12011-08-29 16:43:02 -0700700 mCurrentState.mUrl = "";
John Reck1cf4b792011-07-26 10:22:22 -0700701 }
John Reck1cf4b792011-07-26 10:22:22 -0700702 mCurrentState.mTitle = view.getTitle();
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800703
704
John Reck1cf4b792011-07-26 10:22:22 -0700705 if (!URLUtil.isHttpsUrl(mCurrentState.mUrl)) {
706 // In case we stop when loading an HTTPS page from an HTTP page
707 // but before a provisional load occurred
Steve Block2466eff2011-10-03 15:33:09 +0100708 mCurrentState.mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
Steve Block08a6f0c2011-10-06 12:12:53 +0100709 mCurrentState.mSslCertificateError = null;
John Reck1cf4b792011-07-26 10:22:22 -0700710 }
John Reck502a3532011-08-16 14:21:46 -0700711 mCurrentState.mIncognito = view.isPrivateBrowsingEnabled();
John Reck1cf4b792011-07-26 10:22:22 -0700712 }
713
Tarun Nainani8eb00912014-07-17 12:28:32 -0700714
715 public boolean isTabFullScreen() {
716 return mFullScreen;
717 }
718
Vivek Sekharf96064b2014-07-28 16:32:34 -0700719 protected void setTabFullscreen(boolean fullScreen) {
Tarun Nainani8eb00912014-07-17 12:28:32 -0700720 Controller controller = (Controller)mWebViewController;
Sudheer Koganti24766882014-10-02 10:58:09 -0700721 controller.getUi().showFullscreen(fullScreen);
Tarun Nainani8eb00912014-07-17 12:28:32 -0700722 mFullScreen = fullScreen;
Vivek Sekharf96064b2014-07-28 16:32:34 -0700723 }
724
Sudheer Koganti24766882014-10-02 10:58:09 -0700725 public boolean exitFullscreen() {
726 if (mFullScreen) {
727 Controller controller = (Controller)mWebViewController;
728 controller.getUi().showFullscreen(false);
729 if (getWebView() != null)
730 getWebView().exitFullscreen();
731 mFullScreen = false;
732 return true;
733 }
734 return false;
735 }
736
737
738
739
Grace Kloba22ac16e2009-10-07 18:00:23 -0700740 // -------------------------------------------------------------------------
741 // WebChromeClient implementation for the main WebView
742 // -------------------------------------------------------------------------
743
744 private final WebChromeClient mWebChromeClient = new WebChromeClient() {
745 // Helper method to create a new tab or sub window.
746 private void createWindow(final boolean dialog, final Message msg) {
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700747 this.createWindow(dialog, msg, null, false);
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700748 }
749
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700750 private void createWindow(final boolean dialog, final Message msg, final String url,
751 final boolean opener_suppressed) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700752 WebView.WebViewTransport transport =
753 (WebView.WebViewTransport) msg.obj;
754 if (dialog) {
755 createSubWindow();
Michael Kolb8233fac2010-10-26 16:08:53 -0700756 mWebViewController.attachSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700757 transport.setWebView(mSubView);
758 } else {
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700759 final Tab newTab = mWebViewController.openTab(url,
John Reck5949c662011-05-27 09:52:29 -0700760 Tab.this, true, true);
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700761 // This is special case for rendering links on a webpage in
762 // a new tab. If opener is suppressed, the WebContents created
763 // by the content layer are not fully initialized. This check
764 // will prevent content layer from overriding WebContents
765 // created by new tab with the uninitialized instance.
766 if (!opener_suppressed) {
767 transport.setWebView(newTab.getWebView());
768 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700769 }
770 msg.sendToTarget();
771 }
772
773 @Override
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700774 public void toggleFullscreenModeForTab(boolean enterFullscreen) {
775 if (mWebViewController instanceof Controller) {
Vivek Sekharf96064b2014-07-28 16:32:34 -0700776 setTabFullscreen(enterFullscreen);
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700777 }
778 }
779
780 @Override
Tarun Nainani8eb00912014-07-17 12:28:32 -0700781 public void onOffsetsForFullscreenChanged(float topControlsOffsetYPix,
782 float contentOffsetYPix,
783 float overdrawBottomHeightPix) {
784 if (mWebViewController instanceof Controller) {
Vivek Sekhar8ee3abb2014-07-14 12:32:05 -0700785 Controller controller = (Controller)mWebViewController;
Tarun Nainani8eb00912014-07-17 12:28:32 -0700786 controller.getUi().translateTitleBar(topControlsOffsetYPix);
Vivek Sekhar8ee3abb2014-07-14 12:32:05 -0700787 }
788 }
789
790 @Override
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700791 public boolean isTabFullScreen() {
Vivek Sekharf96064b2014-07-28 16:32:34 -0700792 return mFullScreen;
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700793 }
794
795 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700796 public boolean onCreateWindow(WebView view, final boolean dialog,
797 final boolean userGesture, final Message resultMsg) {
798 // only allow new window or sub window for the foreground case
799 if (!mInForeground) {
800 return false;
801 }
802 // Short-circuit if we can't create any more tabs or sub windows.
803 if (dialog && mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -0700804 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700805 .setTitle(R.string.too_many_subwindows_dialog_title)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200806 .setIconAttribute(android.R.attr.alertDialogIcon)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700807 .setMessage(R.string.too_many_subwindows_dialog_message)
808 .setPositiveButton(R.string.ok, null)
809 .show();
810 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700811 } else if (!mWebViewController.getTabControl().canCreateNewTab()) {
Michael Kolb14612442011-06-24 13:06:29 -0700812 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700813 .setTitle(R.string.too_many_windows_dialog_title)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200814 .setIconAttribute(android.R.attr.alertDialogIcon)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700815 .setMessage(R.string.too_many_windows_dialog_message)
816 .setPositiveButton(R.string.ok, null)
817 .show();
818 return false;
819 }
820
821 // Short-circuit if this was a user gesture.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800822 if (userGesture || !mSettings.blockPopupWindows()) {
Tarun Nainani4f5137d2015-04-16 17:26:18 -0700823 WebView.WebViewTransport transport =
824 (WebView.WebViewTransport) resultMsg.obj;
825 CreateWindowParams windowParams = transport.getCreateWindowParams();
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700826 if (windowParams.mOpenerSuppressed) {
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700827 createWindow(dialog, resultMsg, windowParams.mURL, true);
828 // This is special case for rendering links on a webpage in
829 // a new tab. If opener is suppressed, the WebContents created
830 // by the content layer are not fully initialized. Returning false
831 // will prevent content layer from overriding WebContents
832 // created by new tab with the uninitialized instance.
833 return false;
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700834 }
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700835
836 createWindow(dialog, resultMsg);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700837 return true;
838 }
839
Tarun Nainani4f5137d2015-04-16 17:26:18 -0700840 createWindow(dialog, resultMsg);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700841 return true;
842 }
843
844 @Override
Patrick Scotteb5061b2009-11-18 15:00:30 -0500845 public void onRequestFocus(WebView view) {
846 if (!mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700847 mWebViewController.switchToTab(Tab.this);
Patrick Scotteb5061b2009-11-18 15:00:30 -0500848 }
849 }
850
851 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700852 public void onCloseWindow(WebView window) {
Michael Kolbc831b632011-05-11 09:30:34 -0700853 if (mParent != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700854 // JavaScript can only close popup window.
855 if (mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700856 mWebViewController.switchToTab(mParent);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700857 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700858 mWebViewController.closeTab(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700859 }
860 }
861
862 @Override
863 public void onProgressChanged(WebView view, int newProgress) {
John Reck30c714c2010-12-16 17:30:34 -0800864 mPageLoadProgress = newProgress;
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800865 if (newProgress == 100) {
866 mInPageLoad = false;
867 }
John Reck30c714c2010-12-16 17:30:34 -0800868 mWebViewController.onProgressChanged(Tab.this);
Michael Kolb72864272012-05-03 15:42:15 -0700869 if (mUpdateThumbnail && newProgress == 100) {
870 mUpdateThumbnail = false;
871 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700872 }
873
874 @Override
Leon Scroggins21d9b902010-03-11 09:33:11 -0500875 public void onReceivedTitle(WebView view, final String title) {
John Reck30c714c2010-12-16 17:30:34 -0800876 mCurrentState.mTitle = title;
Michael Kolb8233fac2010-10-26 16:08:53 -0700877 mWebViewController.onReceivedTitle(Tab.this, title);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700878 }
879
880 @Override
881 public void onReceivedIcon(WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -0800882 mCurrentState.mFavicon = icon;
Michael Kolb8233fac2010-10-26 16:08:53 -0700883 mWebViewController.onFavicon(Tab.this, view, icon);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700884 }
885
886 @Override
887 public void onReceivedTouchIconUrl(WebView view, String url,
888 boolean precomposed) {
Michael Kolb14612442011-06-24 13:06:29 -0700889 final ContentResolver cr = mContext.getContentResolver();
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400890 // Let precomposed icons take precedence over non-composed
891 // icons.
892 if (precomposed && mTouchIconLoader != null) {
893 mTouchIconLoader.cancel(false);
894 mTouchIconLoader = null;
895 }
896 // Have only one async task at a time.
897 if (mTouchIconLoader == null) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700898 mTouchIconLoader = new DownloadTouchIcon(Tab.this,
Michael Kolb14612442011-06-24 13:06:29 -0700899 mContext, cr, view);
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400900 mTouchIconLoader.execute(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700901 }
902 }
903
904 @Override
905 public void onShowCustomView(View view,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800906 CustomViewCallback callback) {
Michael Kolb14612442011-06-24 13:06:29 -0700907 Activity activity = mWebViewController.getActivity();
908 if (activity != null) {
909 onShowCustomView(view, activity.getRequestedOrientation(), callback);
910 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400911 }
912
913 @Override
914 public void onShowCustomView(View view, int requestedOrientation,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800915 CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700916 if (mInForeground) mWebViewController.showCustomView(Tab.this, view,
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400917 requestedOrientation, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700918 }
919
920 @Override
921 public void onHideCustomView() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700922 if (mInForeground) mWebViewController.hideCustomView();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700923 }
924
925 /**
926 * The origin has exceeded its database quota.
927 * @param url the URL that exceeded the quota
928 * @param databaseIdentifier the identifier of the database on which the
929 * transaction that caused the quota overflow was run
930 * @param currentQuota the current quota for the origin.
931 * @param estimatedSize the estimated size of the database.
932 * @param totalUsedQuota is the sum of all origins' quota.
933 * @param quotaUpdater The callback to run when a decision to allow or
934 * deny quota has been made. Don't forget to call this!
935 */
936 @Override
937 public void onExceededDatabaseQuota(String url,
938 String databaseIdentifier, long currentQuota, long estimatedSize,
939 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -0700940 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -0700941 .onExceededDatabaseQuota(url, databaseIdentifier,
942 currentQuota, estimatedSize, totalUsedQuota,
943 quotaUpdater);
944 }
945
946 /**
947 * The Application Cache has exceeded its max size.
948 * @param spaceNeeded is the amount of disk space that would be needed
949 * in order for the last appcache operation to succeed.
950 * @param totalUsedQuota is the sum of all origins' quota.
951 * @param quotaUpdater A callback to inform the WebCore thread that a
952 * new app cache size is available. This callback must always
953 * be executed at some point to ensure that the sleeping
954 * WebCore thread is woken up.
955 */
956 @Override
957 public void onReachedMaxAppCacheSize(long spaceNeeded,
958 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -0700959 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -0700960 .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota,
961 quotaUpdater);
962 }
963
Ben Murdoch65acc352009-11-19 18:16:04 +0000964 /* Adds a JavaScript error message to the system log and if the JS
965 * console is enabled in the about:debug options, to that console
966 * also.
Ben Murdochc42addf2010-01-28 15:19:59 +0000967 * @param consoleMessage the message object.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700968 */
969 @Override
Ben Murdochc42addf2010-01-28 15:19:59 +0000970 public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Jeff Hamilton47654f42010-09-07 09:57:51 -0500971 // Don't log console messages in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -0700972 if (isPrivateBrowsingEnabled()) return true;
Jeff Hamilton47654f42010-09-07 09:57:51 -0500973
Ben Murdochc42addf2010-01-28 15:19:59 +0000974 String message = "Console: " + consoleMessage.message() + " "
975 + consoleMessage.sourceId() + ":"
976 + consoleMessage.lineNumber();
977
978 switch (consoleMessage.messageLevel()) {
979 case TIP:
980 Log.v(CONSOLE_LOGTAG, message);
981 break;
982 case LOG:
983 Log.i(CONSOLE_LOGTAG, message);
984 break;
985 case WARNING:
986 Log.w(CONSOLE_LOGTAG, message);
987 break;
988 case ERROR:
989 Log.e(CONSOLE_LOGTAG, message);
990 break;
991 case DEBUG:
992 Log.d(CONSOLE_LOGTAG, message);
993 break;
994 }
995
996 return true;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700997 }
998
999 /**
1000 * Ask the browser for an icon to represent a <video> element.
1001 * This icon will be used if the Web page did not specify a poster attribute.
1002 * @return Bitmap The icon or null if no such icon is available.
1003 */
1004 @Override
1005 public Bitmap getDefaultVideoPoster() {
1006 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001007 return mWebViewController.getDefaultVideoPoster();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001008 }
1009 return null;
1010 }
1011
1012 /**
1013 * Ask the host application for a custom progress view to show while
1014 * a <video> is loading.
1015 * @return View The progress view.
1016 */
1017 @Override
1018 public View getVideoLoadingProgressView() {
1019 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001020 return mWebViewController.getVideoLoadingProgressView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001021 }
1022 return null;
1023 }
1024
1025 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00001026 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001027 if (mInForeground) {
Ben Murdoch8cad4132012-01-11 10:56:43 +00001028 mWebViewController.openFileChooser(uploadMsg, acceptType, capture);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001029 } else {
1030 uploadMsg.onReceiveValue(null);
1031 }
1032 }
1033
Vivek Sekharb54614f2014-05-01 19:03:37 -07001034 @Override
1035 public void showFileChooser(ValueCallback<String[]> uploadFilePaths, String acceptTypes,
1036 boolean capture) {
1037 if (mInForeground) {
1038 mWebViewController.showFileChooser(uploadFilePaths, acceptTypes, capture);
1039 } else {
1040 uploadFilePaths.onReceiveValue(null);
1041 }
1042 }
1043
Grace Kloba22ac16e2009-10-07 18:00:23 -07001044 /**
1045 * Deliver a list of already-visited URLs
1046 */
1047 @Override
1048 public void getVisitedHistory(final ValueCallback<String[]> callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001049 mWebViewController.getVisitedHistory(callback);
1050 }
Ben Murdoch8029a772010-11-16 11:58:21 +00001051
1052 @Override
1053 public void setupAutoFill(Message message) {
1054 // Prompt the user to set up their profile.
1055 final Message msg = message;
Michael Kolb14612442011-06-24 13:06:29 -07001056 AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
1057 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
Ben Murdoch1d676b62011-01-17 12:54:24 +00001058 Context.LAYOUT_INFLATER_SERVICE);
1059 final View layout = inflater.inflate(R.layout.setup_autofill_dialog, null);
1060
1061 builder.setView(layout)
1062 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1063 @Override
1064 public void onClick(DialogInterface dialog, int id) {
1065 CheckBox disableAutoFill = (CheckBox) layout.findViewById(
1066 R.id.setup_autofill_dialog_disable_autofill);
1067
1068 if (disableAutoFill.isChecked()) {
1069 // Disable autofill and show a toast with how to turn it on again.
John Reck35e9dd62011-04-25 09:01:54 -07001070 mSettings.setAutofillEnabled(false);
Michael Kolb14612442011-06-24 13:06:29 -07001071 Toast.makeText(mContext,
Ben Murdoch1d676b62011-01-17 12:54:24 +00001072 R.string.autofill_setup_dialog_negative_toast,
1073 Toast.LENGTH_LONG).show();
1074 } else {
1075 // Take user to the AutoFill profile editor. When they return,
1076 // we will send the message that we pass here which will trigger
1077 // the form to get filled out with their new profile.
1078 mWebViewController.setupAutoFill(msg);
1079 }
1080 }
1081 })
1082 .setNegativeButton(R.string.cancel, null)
1083 .show();
Ben Murdoch8029a772010-11-16 11:58:21 +00001084 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001085 };
1086
1087 // -------------------------------------------------------------------------
1088 // WebViewClient implementation for the sub window
1089 // -------------------------------------------------------------------------
1090
1091 // Subclass of WebViewClient used in subwindows to notify the main
1092 // WebViewClient of certain WebView activities.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001093 private static class SubWindowClient extends WebViewClient {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001094 // The main WebViewClient.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001095 private final WebViewClient mClient;
Michael Kolb8233fac2010-10-26 16:08:53 -07001096 private final WebViewController mController;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001097
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001098 SubWindowClient(WebViewClient client, WebViewController controller) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001099 mClient = client;
Michael Kolb8233fac2010-10-26 16:08:53 -07001100 mController = controller;
Leon Scroggins III211ba542010-04-19 13:21:13 -04001101 }
1102 @Override
1103 public void onPageStarted(WebView view, String url, Bitmap favicon) {
1104 // Unlike the others, do not call mClient's version, which would
1105 // change the progress bar. However, we do want to remove the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001106 // find or select dialog.
Michael Kolb8233fac2010-10-26 16:08:53 -07001107 mController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001108 }
1109 @Override
1110 public void doUpdateVisitedHistory(WebView view, String url,
1111 boolean isReload) {
1112 mClient.doUpdateVisitedHistory(view, url, isReload);
1113 }
1114 @Override
1115 public boolean shouldOverrideUrlLoading(WebView view, String url) {
1116 return mClient.shouldOverrideUrlLoading(view, url);
1117 }
1118 @Override
1119 public void onReceivedSslError(WebView view, SslErrorHandler handler,
1120 SslError error) {
1121 mClient.onReceivedSslError(view, handler, error);
1122 }
1123 @Override
Brian Carlstrom8862c1d2011-06-02 01:05:55 -07001124 public void onReceivedClientCertRequest(WebView view,
1125 ClientCertRequestHandler handler, String host_and_port) {
1126 mClient.onReceivedClientCertRequest(view, handler, host_and_port);
1127 }
1128 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -07001129 public void onReceivedHttpAuthRequest(WebView view,
1130 HttpAuthHandler handler, String host, String realm) {
1131 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
1132 }
1133 @Override
1134 public void onFormResubmission(WebView view, Message dontResend,
1135 Message resend) {
1136 mClient.onFormResubmission(view, dontResend, resend);
1137 }
1138 @Override
1139 public void onReceivedError(WebView view, int errorCode,
1140 String description, String failingUrl) {
1141 mClient.onReceivedError(view, errorCode, description, failingUrl);
1142 }
1143 @Override
1144 public boolean shouldOverrideKeyEvent(WebView view,
1145 android.view.KeyEvent event) {
1146 return mClient.shouldOverrideKeyEvent(view, event);
1147 }
1148 @Override
1149 public void onUnhandledKeyEvent(WebView view,
1150 android.view.KeyEvent event) {
1151 mClient.onUnhandledKeyEvent(view, event);
1152 }
1153 }
1154
1155 // -------------------------------------------------------------------------
1156 // WebChromeClient implementation for the sub window
1157 // -------------------------------------------------------------------------
1158
1159 private class SubWindowChromeClient extends WebChromeClient {
1160 // The main WebChromeClient.
1161 private final WebChromeClient mClient;
1162
1163 SubWindowChromeClient(WebChromeClient client) {
1164 mClient = client;
1165 }
1166 @Override
1167 public void onProgressChanged(WebView view, int newProgress) {
1168 mClient.onProgressChanged(view, newProgress);
1169 }
1170 @Override
1171 public boolean onCreateWindow(WebView view, boolean dialog,
1172 boolean userGesture, android.os.Message resultMsg) {
1173 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
1174 }
1175 @Override
1176 public void onCloseWindow(WebView window) {
1177 if (window != mSubView) {
1178 Log.e(LOGTAG, "Can't close the window");
1179 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001180 mWebViewController.dismissSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001181 }
1182 }
1183
1184 // -------------------------------------------------------------------------
1185
1186 // Construct a new tab
Michael Kolb7bcafde2011-05-09 13:55:59 -07001187 Tab(WebViewController wvcontroller, WebView w) {
John Reck1cf4b792011-07-26 10:22:22 -07001188 this(wvcontroller, w, null);
1189 }
1190
1191 Tab(WebViewController wvcontroller, Bundle state) {
1192 this(wvcontroller, null, state);
1193 }
1194
1195 Tab(WebViewController wvcontroller, WebView w, Bundle state) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001196 mWebViewController = wvcontroller;
Michael Kolb14612442011-06-24 13:06:29 -07001197 mContext = mWebViewController.getContext();
John Reck35e9dd62011-04-25 09:01:54 -07001198 mSettings = BrowserSettings.getInstance();
Michael Kolb14612442011-06-24 13:06:29 -07001199 mDataController = DataController.getInstance(mContext);
1200 mCurrentState = new PageState(mContext, w != null
John Reck847b5322011-04-14 17:02:18 -07001201 ? w.isPrivateBrowsingEnabled() : false);
Tarun Nainani8084c822014-06-25 13:38:06 -07001202 setTimeStamp();
Michael Kolb8233fac2010-10-26 16:08:53 -07001203 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001204 mInForeground = false;
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001205 mWebViewDestroyedByMemoryMonitor = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001206
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001207 mDownloadListener = new BrowserDownloadListener() {
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001208 public void onDownloadStart(String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001209 String contentDisposition, String mimetype, String referer,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001210 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001211 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001212 mimetype, referer, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001213 }
1214 };
1215
John Reck1cf4b792011-07-26 10:22:22 -07001216 mCaptureWidth = mContext.getResources().getDimensionPixelSize(
1217 R.dimen.tab_thumbnail_width);
1218 mCaptureHeight = mContext.getResources().getDimensionPixelSize(
1219 R.dimen.tab_thumbnail_height);
1220 updateShouldCaptureThumbnails();
1221 restoreState(state);
John Reck52be4782011-08-26 15:37:29 -07001222 if (getId() == -1) {
1223 mId = TabControl.getNextId();
1224 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001225 setWebView(w);
Michael Kolb9ef259a2011-07-12 15:33:08 -07001226 mHandler = new Handler() {
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001227 @Override
Michael Kolb9ef259a2011-07-12 15:33:08 -07001228 public void handleMessage(Message m) {
John Reck1cf4b792011-07-26 10:22:22 -07001229 switch (m.what) {
1230 case MSG_CAPTURE:
1231 capture();
1232 break;
1233 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001234 }
1235 };
Pankaj Garg1c13cab2015-05-12 11:52:17 -07001236
1237 mFirstPixelObservable = new Observable();
1238 mFirstPixelObservable.set(false);
1239 mTabHistoryUpdateObservable = new Observable();
John Reck1cf4b792011-07-26 10:22:22 -07001240 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001241
Michael Kolb72864272012-05-03 15:42:15 -07001242 public boolean shouldUpdateThumbnail() {
1243 return mUpdateThumbnail;
1244 }
1245
Mathew Inwoode09305e2011-09-02 12:03:26 +01001246 /**
1247 * This is used to get a new ID when the tab has been preloaded, before it is displayed and
1248 * added to TabControl. Preloaded tabs can be created before restoreInstanceState, leading
1249 * to overlapping IDs between the preloaded and restored tabs.
1250 */
1251 public void refreshIdAfterPreload() {
1252 mId = TabControl.getNextId();
1253 }
1254
John Reck1cf4b792011-07-26 10:22:22 -07001255 public void updateShouldCaptureThumbnails() {
1256 if (mWebViewController.shouldCaptureThumbnails()) {
1257 synchronized (Tab.this) {
1258 if (mCapture == null) {
1259 mCapture = Bitmap.createBitmap(mCaptureWidth, mCaptureHeight,
1260 Bitmap.Config.RGB_565);
Michael Kolbc3af0672011-08-09 10:24:41 -07001261 mCapture.eraseColor(Color.WHITE);
John Reck1cf4b792011-07-26 10:22:22 -07001262 if (mInForeground) {
1263 postCapture();
1264 }
1265 }
1266 }
1267 } else {
1268 synchronized (Tab.this) {
1269 mCapture = null;
1270 deleteThumbnail();
1271 }
1272 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001273 }
1274
Michael Kolb14612442011-06-24 13:06:29 -07001275 public void setController(WebViewController ctl) {
1276 mWebViewController = ctl;
John Reck1cf4b792011-07-26 10:22:22 -07001277 updateShouldCaptureThumbnails();
Michael Kolb14612442011-06-24 13:06:29 -07001278 }
1279
Michael Kolbc831b632011-05-11 09:30:34 -07001280 public long getId() {
1281 return mId;
1282 }
1283
Michael Kolb91911a22012-01-17 11:21:25 -08001284 void setWebView(WebView w) {
1285 setWebView(w, true);
1286 }
1287
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001288 public boolean isNativeActive(){
1289 if (mMainView == null)
1290 return false;
1291 return true;
1292 }
1293
1294 public void setTimeStamp(){
1295 Date d = new Date();
1296 timestamp = (new Timestamp(d.getTime()));
1297 }
1298
1299 public Timestamp getTimestamp() {
1300 return timestamp;
1301 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001302 /**
1303 * Sets the WebView for this tab, correctly removing the old WebView from
1304 * the container view.
1305 */
Michael Kolb91911a22012-01-17 11:21:25 -08001306 void setWebView(WebView w, boolean restore) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001307 if (mMainView == w) {
1308 return;
1309 }
Michael Kolba713ec82010-11-29 17:27:06 -08001310
Michael Kolba713ec82010-11-29 17:27:06 -08001311 mWebViewController.onSetWebView(this, w);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001312
John Reck1cf4b792011-07-26 10:22:22 -07001313 if (mMainView != null) {
John Reck8ee633f2011-08-09 16:00:35 -07001314 mMainView.setPictureListener(null);
John Reck1cf4b792011-07-26 10:22:22 -07001315 if (w != null) {
1316 syncCurrentState(w, null);
1317 } else {
Panos Thomasa9a5a582014-03-18 19:20:08 -07001318 mCurrentState = new PageState(mContext, mMainView.isPrivateBrowsingEnabled());
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001319
1320 if (mWebViewDestroyedByMemoryMonitor) {
1321 /*
1322 * If tab was destroyed as a result of the MemoryMonitor
1323 * then we need to restore the state properties
1324 * from the old WebView (mMainView)
1325 */
1326 syncCurrentState(mMainView, null);
1327 mWebViewDestroyedByMemoryMonitor = false;
1328 }
John Reck1cf4b792011-07-26 10:22:22 -07001329 }
1330 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001331 // set the new one
1332 mMainView = w;
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001333
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001334 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001335 if (mMainView != null) {
1336 mMainView.setWebViewClient(mWebViewClient);
1337 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001338 // Attach DownloadManager so that downloads can start in an active
1339 // or a non-active window. This can happen when going to a site that
1340 // does a redirect after a period of time. The user could have
1341 // switched to another tab while waiting for the download to start.
1342 mMainView.setDownloadListener(mDownloadListener);
John Reck8ee633f2011-08-09 16:00:35 -07001343 TabControl tc = mWebViewController.getTabControl();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001344 if (tc != null /*&& tc.getOnThumbnailUpdatedListener() != null*/) {
John Reck8ee633f2011-08-09 16:00:35 -07001345 mMainView.setPictureListener(this);
1346 }
Michael Kolb91911a22012-01-17 11:21:25 -08001347 if (restore && (mSavedState != null)) {
John Reck8b9bb8b2012-03-08 13:19:40 -08001348 restoreUserAgent();
John Reck6c2e2f32011-08-22 13:41:23 -07001349 WebBackForwardList restoredState
1350 = mMainView.restoreState(mSavedState);
1351 if (restoredState == null || restoredState.getSize() == 0) {
1352 Log.w(LOGTAG, "Failed to restore WebView state!");
1353 loadUrl(mCurrentState.mOriginalUrl, null);
1354 }
John Reck1cf4b792011-07-26 10:22:22 -07001355 mSavedState = null;
1356 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001357 }
1358 }
1359
Axesh R. Ajmera2fa0ba12015-03-17 18:18:36 -07001360 public void destroyThroughMemoryMonitor() {
1361 mWebViewDestroyedByMemoryMonitor = true;
1362 destroy();
1363 }
1364
Grace Kloba22ac16e2009-10-07 18:00:23 -07001365 /**
1366 * Destroy the tab's main WebView and subWindow if any
1367 */
1368 void destroy() {
Tarun Nainani2c1dd7c2014-07-05 16:40:12 -07001369
Matthew Huib7f2e9c2014-04-16 11:12:37 -04001370 if (mPostponeDestroy) {
1371 mShouldDestroy = true;
1372 return;
1373 }
1374 mShouldDestroy = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001375 if (mMainView != null) {
1376 dismissSubWindow();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001377 // save the WebView to call destroy() after detach it from the tab
1378 WebView webView = mMainView;
Pankaj Garg1c13cab2015-05-12 11:52:17 -07001379 if (!mWebViewDestroyedByMemoryMonitor) {
1380 int[] ids = webView.getSnapshotIds();
1381 int currentTabIdx = mWebViewController.getTabControl().getCurrentPosition();
1382 for (int id : ids) {
1383 if (getTabIdxFromCaptureIdx(id) == currentTabIdx) {
1384 webView.deleteSnapshot(id);
1385 }
1386 }
1387 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001388 setWebView(null);
1389 webView.destroy();
1390 }
1391 }
1392
Matthew Huib7f2e9c2014-04-16 11:12:37 -04001393 private boolean mPostponeDestroy = false;
1394 private boolean mShouldDestroy = false;
1395
1396 public void postponeDestroy() {
1397 mPostponeDestroy = true;
1398 }
1399
1400 public void performPostponedDestroy() {
1401 mPostponeDestroy = false;
1402 if (mShouldDestroy) {
1403 destroy();
1404 }
1405 }
1406
Grace Kloba22ac16e2009-10-07 18:00:23 -07001407 /**
1408 * Remove the tab from the parent
1409 */
1410 void removeFromTree() {
1411 // detach the children
Michael Kolbc831b632011-05-11 09:30:34 -07001412 if (mChildren != null) {
1413 for(Tab t : mChildren) {
1414 t.setParent(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001415 }
1416 }
1417 // remove itself from the parent list
Michael Kolbc831b632011-05-11 09:30:34 -07001418 if (mParent != null) {
1419 mParent.mChildren.remove(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001420 }
Tarun Nainani8eb00912014-07-17 12:28:32 -07001421
1422 mCapture = null;
John Reck1cf4b792011-07-26 10:22:22 -07001423 deleteThumbnail();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001424 }
1425
1426 /**
1427 * Create a new subwindow unless a subwindow already exists.
1428 * @return True if a new subwindow was created. False if one already exists.
1429 */
1430 boolean createSubWindow() {
1431 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001432 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001433 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001434 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001435 mSubView.setWebChromeClient(new SubWindowChromeClient(
1436 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001437 // Set a different DownloadListener for the mSubView, since it will
1438 // just need to dismiss the mSubView, rather than close the Tab
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001439 mSubView.setDownloadListener(new BrowserDownloadListener() {
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001440 public void onDownloadStart(String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001441 String contentDisposition, String mimetype, String referer,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001442 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001443 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001444 contentDisposition, mimetype, referer, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001445 if (mSubView.copyBackForwardList().getSize() == 0) {
1446 // This subwindow was opened for the sole purpose of
1447 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001448 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001449 }
1450 }
1451 });
Michael Kolb14612442011-06-24 13:06:29 -07001452 mSubView.setOnCreateContextMenuListener(mWebViewController.getActivity());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001453 return true;
1454 }
1455 return false;
1456 }
1457
1458 /**
1459 * Dismiss the subWindow for the tab.
1460 */
1461 void dismissSubWindow() {
1462 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001463 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001464 mSubView.destroy();
1465 mSubView = null;
1466 mSubViewContainer = null;
1467 }
1468 }
1469
Grace Kloba22ac16e2009-10-07 18:00:23 -07001470
1471 /**
1472 * Set the parent tab of this tab.
1473 */
Michael Kolbc831b632011-05-11 09:30:34 -07001474 void setParent(Tab parent) {
John Reck52be4782011-08-26 15:37:29 -07001475 if (parent == this) {
1476 throw new IllegalStateException("Cannot set parent to self!");
1477 }
Michael Kolbc831b632011-05-11 09:30:34 -07001478 mParent = parent;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001479 // This tab may have been freed due to low memory. If that is the case,
Michael Kolbc831b632011-05-11 09:30:34 -07001480 // the parent tab id is already saved. If we are changing that id
Grace Kloba22ac16e2009-10-07 18:00:23 -07001481 // (most likely due to removing the parent tab) we must update the
Michael Kolbc831b632011-05-11 09:30:34 -07001482 // parent tab id in the saved Bundle.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001483 if (mSavedState != null) {
1484 if (parent == null) {
1485 mSavedState.remove(PARENTTAB);
1486 } else {
Michael Kolbc831b632011-05-11 09:30:34 -07001487 mSavedState.putLong(PARENTTAB, parent.getId());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001488 }
1489 }
John Reckb0a86db2011-05-24 14:05:58 -07001490
1491 // Sync the WebView useragent with the parent
1492 if (parent != null && mSettings.hasDesktopUseragent(parent.getWebView())
1493 != mSettings.hasDesktopUseragent(getWebView())) {
1494 mSettings.toggleDesktopUseragent(getWebView());
1495 }
John Reck52be4782011-08-26 15:37:29 -07001496
1497 if (parent != null && parent.getId() == getId()) {
1498 throw new IllegalStateException("Parent has same ID as child!");
1499 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001500 }
1501
1502 /**
Michael Kolbc831b632011-05-11 09:30:34 -07001503 * If this Tab was created through another Tab, then this method returns
1504 * that Tab.
1505 * @return the Tab parent or null
1506 */
1507 public Tab getParent() {
1508 return mParent;
1509 }
1510
1511 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001512 * When a Tab is created through the content of another Tab, then we
1513 * associate the Tabs.
1514 * @param child the Tab that was created from this Tab
1515 */
1516 void addChildTab(Tab child) {
Michael Kolbc831b632011-05-11 09:30:34 -07001517 if (mChildren == null) {
1518 mChildren = new Vector<Tab>();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001519 }
Michael Kolbc831b632011-05-11 09:30:34 -07001520 mChildren.add(child);
1521 child.setParent(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001522 }
1523
Michael Kolbc831b632011-05-11 09:30:34 -07001524 Vector<Tab> getChildren() {
1525 return mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001526 }
1527
1528 void resume() {
1529 if (mMainView != null) {
John Reck56c1fcf2011-08-17 10:15:16 -07001530 setupHwAcceleration(mMainView);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001531 mMainView.onResume();
1532 if (mSubView != null) {
1533 mSubView.onResume();
1534 }
1535 }
1536 }
1537
John Reck56c1fcf2011-08-17 10:15:16 -07001538 private void setupHwAcceleration(View web) {
1539 if (web == null) return;
1540 BrowserSettings settings = BrowserSettings.getInstance();
1541 if (settings.isHardwareAccelerated()) {
1542 web.setLayerType(View.LAYER_TYPE_NONE, null);
1543 } else {
1544 web.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
1545 }
1546 }
1547
Grace Kloba22ac16e2009-10-07 18:00:23 -07001548 void pause() {
1549 if (mMainView != null) {
1550 mMainView.onPause();
1551 if (mSubView != null) {
1552 mSubView.onPause();
1553 }
1554 }
1555 }
1556
1557 void putInForeground() {
John Reck8ee633f2011-08-09 16:00:35 -07001558 if (mInForeground) {
1559 return;
1560 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001561 mInForeground = true;
1562 resume();
Michael Kolb14612442011-06-24 13:06:29 -07001563 Activity activity = mWebViewController.getActivity();
1564 mMainView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001565 if (mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -07001566 mSubView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001567 }
Axesh R. Ajmerac6b5c322015-05-01 11:06:10 -07001568
Leon Scroggins1961ed22010-12-07 15:22:21 -05001569 mWebViewController.bookmarkedStatusHasChanged(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001570 }
1571
1572 void putInBackground() {
John Reck8ee633f2011-08-09 16:00:35 -07001573 if (!mInForeground) {
1574 return;
1575 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001576 mInForeground = false;
1577 pause();
1578 mMainView.setOnCreateContextMenuListener(null);
1579 if (mSubView != null) {
1580 mSubView.setOnCreateContextMenuListener(null);
1581 }
1582 }
1583
Michael Kolb8233fac2010-10-26 16:08:53 -07001584 boolean inForeground() {
1585 return mInForeground;
1586 }
1587
Grace Kloba22ac16e2009-10-07 18:00:23 -07001588 /**
1589 * Return the top window of this tab; either the subwindow if it is not
1590 * null or the main window.
1591 * @return The top window of this tab.
1592 */
1593 WebView getTopWindow() {
1594 if (mSubView != null) {
1595 return mSubView;
1596 }
1597 return mMainView;
1598 }
1599
1600 /**
1601 * Return the main window of this tab. Note: if a tab is freed in the
1602 * background, this can return null. It is only guaranteed to be
1603 * non-null for the current tab.
1604 * @return The main WebView of this tab.
1605 */
1606 WebView getWebView() {
1607 return mMainView;
1608 }
1609
Michael Kolba713ec82010-11-29 17:27:06 -08001610 void setViewContainer(View container) {
1611 mContainer = container;
1612 }
1613
Michael Kolb8233fac2010-10-26 16:08:53 -07001614 View getViewContainer() {
1615 return mContainer;
1616 }
1617
Grace Kloba22ac16e2009-10-07 18:00:23 -07001618 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001619 * Return whether private browsing is enabled for the main window of
1620 * this tab.
1621 * @return True if private browsing is enabled.
1622 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001623 boolean isPrivateBrowsingEnabled() {
John Reck502a3532011-08-16 14:21:46 -07001624 return mCurrentState.mIncognito;
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001625 }
1626
1627 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001628 * Return the subwindow of this tab or null if there is no subwindow.
1629 * @return The subwindow of this tab or null.
1630 */
1631 WebView getSubWebView() {
1632 return mSubView;
1633 }
1634
Michael Kolb1514bb72010-11-22 09:11:48 -08001635 void setSubWebView(WebView subView) {
1636 mSubView = subView;
1637 }
1638
Michael Kolb8233fac2010-10-26 16:08:53 -07001639 View getSubViewContainer() {
1640 return mSubViewContainer;
1641 }
1642
Michael Kolb1514bb72010-11-22 09:11:48 -08001643 void setSubViewContainer(View subViewContainer) {
1644 mSubViewContainer = subViewContainer;
1645 }
1646
Grace Kloba22ac16e2009-10-07 18:00:23 -07001647
1648 /**
1649 * @return The application id string
1650 */
1651 String getAppId() {
1652 return mAppId;
1653 }
1654
1655 /**
1656 * Set the application id string
1657 * @param id
1658 */
1659 void setAppId(String id) {
1660 mAppId = id;
1661 }
1662
Michael Kolbe28b3472011-08-04 16:54:31 -07001663 boolean closeOnBack() {
1664 return mCloseOnBack;
1665 }
1666
1667 void setCloseOnBack(boolean close) {
1668 mCloseOnBack = close;
1669 }
1670
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001671 boolean getDerivedFromIntent() {
1672 return mDerivedFromIntent;
1673 }
1674
1675 void setDerivedFromIntent(boolean derived) {
1676 mDerivedFromIntent = derived;
1677 }
1678
Grace Kloba22ac16e2009-10-07 18:00:23 -07001679 String getUrl() {
John Reck324d4402011-01-11 16:56:42 -08001680 return UrlUtils.filteredUrl(mCurrentState.mUrl);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001681 }
1682
Tarun Nainani8eb00912014-07-17 12:28:32 -07001683
1684 protected void onPageFinished() {
1685 mPageFinished = true;
1686 }
1687
1688 public boolean getPageFinishedStatus() {
1689 return mPageFinished;
1690 }
1691
John Reck49a603c2011-03-03 09:33:05 -08001692 String getOriginalUrl() {
John Reckdb22ec42011-06-29 11:31:24 -07001693 if (mCurrentState.mOriginalUrl == null) {
1694 return getUrl();
John Reck49a603c2011-03-03 09:33:05 -08001695 }
John Reckdb22ec42011-06-29 11:31:24 -07001696 return UrlUtils.filteredUrl(mCurrentState.mOriginalUrl);
John Reck49a603c2011-03-03 09:33:05 -08001697 }
1698
Grace Kloba22ac16e2009-10-07 18:00:23 -07001699 /**
John Reck30c714c2010-12-16 17:30:34 -08001700 * Get the title of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001701 */
1702 String getTitle() {
John Reck30c714c2010-12-16 17:30:34 -08001703 return mCurrentState.mTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001704 }
1705
1706 /**
John Reck30c714c2010-12-16 17:30:34 -08001707 * Get the favicon of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001708 */
1709 Bitmap getFavicon() {
John Reck1cf4b792011-07-26 10:22:22 -07001710 if (mCurrentState.mFavicon != null) {
1711 return mCurrentState.mFavicon;
1712 }
1713 return getDefaultFavicon(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001714 }
1715
John Recke969cc52010-12-21 17:24:43 -08001716 public boolean isBookmarkedSite() {
1717 return mCurrentState.mIsBookmarkedSite;
1718 }
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001719
Grace Kloba22ac16e2009-10-07 18:00:23 -07001720 /**
Steve Block08a6f0c2011-10-06 12:12:53 +01001721 * Sets the security state, clears the SSL certificate error and informs
1722 * the controller.
1723 */
Steve Block2466eff2011-10-03 15:33:09 +01001724 private void setSecurityState(SecurityState securityState) {
1725 mCurrentState.mSecurityState = securityState;
Steve Block08a6f0c2011-10-06 12:12:53 +01001726 mCurrentState.mSslCertificateError = null;
Steve Block2466eff2011-10-03 15:33:09 +01001727 mWebViewController.onUpdatedSecurityState(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001728 }
1729
1730 /**
Steve Block2466eff2011-10-03 15:33:09 +01001731 * @return The tab's security state.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001732 */
Steve Block2466eff2011-10-03 15:33:09 +01001733 SecurityState getSecurityState() {
1734 return mCurrentState.mSecurityState;
John Reck30c714c2010-12-16 17:30:34 -08001735 }
1736
Steve Block08a6f0c2011-10-06 12:12:53 +01001737 /**
1738 * Gets the SSL certificate error, if any, for the page's main resource.
1739 * This is only non-null when the security state is
1740 * SECURITY_STATE_BAD_CERTIFICATE.
1741 */
1742 SslError getSslCertificateError() {
1743 return mCurrentState.mSslCertificateError;
1744 }
1745
John Reck30c714c2010-12-16 17:30:34 -08001746 int getLoadProgress() {
1747 if (mInPageLoad) {
1748 return mPageLoadProgress;
1749 }
1750 return 100;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001751 }
1752
1753 /**
1754 * @return TRUE if onPageStarted is called while onPageFinished is not
1755 * called yet.
1756 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001757 boolean inPageLoad() {
1758 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001759 }
1760
Grace Kloba22ac16e2009-10-07 18:00:23 -07001761 /**
John Reck1cf4b792011-07-26 10:22:22 -07001762 * @return The Bundle with the tab's state if it can be saved, otherwise null
Grace Kloba22ac16e2009-10-07 18:00:23 -07001763 */
John Reck1cf4b792011-07-26 10:22:22 -07001764 public Bundle saveState() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001765 // If the WebView is null it means we ran low on memory and we already
1766 // stored the saved state in mSavedState.
1767 if (mMainView == null) {
John Reck1cf4b792011-07-26 10:22:22 -07001768 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001769 }
John Reck6c2e2f32011-08-22 13:41:23 -07001770
1771 if (TextUtils.isEmpty(mCurrentState.mUrl)) {
John Reck1cf4b792011-07-26 10:22:22 -07001772 return null;
John Reck24f18262011-06-17 14:47:20 -07001773 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001774
1775 mSavedState = new Bundle();
John Reck6c2e2f32011-08-22 13:41:23 -07001776 WebBackForwardList savedList = mMainView.saveState(mSavedState);
1777 if (savedList == null || savedList.getSize() == 0) {
1778 Log.w(LOGTAG, "Failed to save back/forward list for "
1779 + mCurrentState.mUrl);
1780 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001781
Michael Kolbc831b632011-05-11 09:30:34 -07001782 mSavedState.putLong(ID, mId);
John Reck30c714c2010-12-16 17:30:34 -08001783 mSavedState.putString(CURRURL, mCurrentState.mUrl);
1784 mSavedState.putString(CURRTITLE, mCurrentState.mTitle);
John Reck1cf4b792011-07-26 10:22:22 -07001785 mSavedState.putBoolean(INCOGNITO, mMainView.isPrivateBrowsingEnabled());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001786 if (mAppId != null) {
1787 mSavedState.putString(APPID, mAppId);
1788 }
Michael Kolbe28b3472011-08-04 16:54:31 -07001789 mSavedState.putBoolean(CLOSEFLAG, mCloseOnBack);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001790 // Remember the parent tab so the relationship can be restored.
Michael Kolbc831b632011-05-11 09:30:34 -07001791 if (mParent != null) {
1792 mSavedState.putLong(PARENTTAB, mParent.mId);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001793 }
John Reckb0a86db2011-05-24 14:05:58 -07001794 mSavedState.putBoolean(USERAGENT,
1795 mSettings.hasDesktopUseragent(getWebView()));
John Reck1cf4b792011-07-26 10:22:22 -07001796 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001797 }
1798
1799 /*
1800 * Restore the state of the tab.
1801 */
John Reck1cf4b792011-07-26 10:22:22 -07001802 private void restoreState(Bundle b) {
1803 mSavedState = b;
1804 if (mSavedState == null) {
1805 return;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001806 }
1807 // Restore the internal state even if the WebView fails to restore.
1808 // This will maintain the app id, original url and close-on-exit values.
Michael Kolbc831b632011-05-11 09:30:34 -07001809 mId = b.getLong(ID);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001810 mAppId = b.getString(APPID);
Michael Kolbe28b3472011-08-04 16:54:31 -07001811 mCloseOnBack = b.getBoolean(CLOSEFLAG);
John Reck8b9bb8b2012-03-08 13:19:40 -08001812 restoreUserAgent();
John Reck1cf4b792011-07-26 10:22:22 -07001813 String url = b.getString(CURRURL);
1814 String title = b.getString(CURRTITLE);
1815 boolean incognito = b.getBoolean(INCOGNITO);
1816 mCurrentState = new PageState(mContext, incognito, url, null);
1817 mCurrentState.mTitle = title;
1818 synchronized (Tab.this) {
1819 if (mCapture != null) {
John Reck4eadc342011-10-31 14:04:10 -07001820 DataController.getInstance(mContext).loadThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07001821 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001822 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001823 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04001824
John Reck8b9bb8b2012-03-08 13:19:40 -08001825 private void restoreUserAgent() {
1826 if (mMainView == null || mSavedState == null) {
1827 return;
1828 }
1829 if (mSavedState.getBoolean(USERAGENT)
1830 != mSettings.hasDesktopUseragent(mMainView)) {
1831 mSettings.toggleDesktopUseragent(mMainView);
1832 }
1833 }
1834
Leon Scroggins1961ed22010-12-07 15:22:21 -05001835 public void updateBookmarkedStatus() {
John Recke969cc52010-12-21 17:24:43 -08001836 mDataController.queryBookmarkStatus(getUrl(), mIsBookmarkCallback);
Leon Scroggins1961ed22010-12-07 15:22:21 -05001837 }
1838
John Recke969cc52010-12-21 17:24:43 -08001839 private DataController.OnQueryUrlIsBookmark mIsBookmarkCallback
1840 = new DataController.OnQueryUrlIsBookmark() {
1841 @Override
1842 public void onQueryUrlIsBookmark(String url, boolean isBookmark) {
1843 if (mCurrentState.mUrl.equals(url)) {
1844 mCurrentState.mIsBookmarkedSite = isBookmark;
1845 mWebViewController.bookmarkedStatusHasChanged(Tab.this);
1846 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001847 }
John Recke969cc52010-12-21 17:24:43 -08001848 };
Michael Kolb1acef692011-03-08 14:12:06 -08001849
Michael Kolbeb95db42011-03-03 10:38:40 -08001850 public Bitmap getScreenshot() {
John Reck1cf4b792011-07-26 10:22:22 -07001851 synchronized (Tab.this) {
1852 return mCapture;
1853 }
Michael Kolbeb95db42011-03-03 10:38:40 -08001854 }
1855
John Reck541f55a2011-06-07 16:34:43 -07001856 public boolean isSnapshot() {
John Reck541f55a2011-06-07 16:34:43 -07001857 return false;
1858 }
1859
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001860 private static class SaveCallback implements ValueCallback<String> {
1861 boolean onReceiveValueCalled = false;
1862 private String mPath;
John Reck68234a92012-04-19 15:27:12 -07001863
1864 @Override
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001865 public void onReceiveValue(String path) {
1866 this.onReceiveValueCalled = true;
1867 this.mPath = path;
John Reck68234a92012-04-19 15:27:12 -07001868 synchronized (this) {
1869 notifyAll();
John Reck8cc92352011-07-06 17:41:52 -07001870 }
John Reck541f55a2011-06-07 16:34:43 -07001871 }
John Reck68234a92012-04-19 15:27:12 -07001872
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001873 public String getPath() {
1874 return mPath;
1875 }
John Reck68234a92012-04-19 15:27:12 -07001876 }
1877
1878 /**
1879 * Must be called on the UI thread
1880 */
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001881 public ContentValues createSnapshotValues(Bitmap bm) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001882 WebView web = getWebView();
John Reck68234a92012-04-19 15:27:12 -07001883 if (web == null) return null;
John Reckd8c74522011-06-14 08:45:00 -07001884 ContentValues values = new ContentValues();
1885 values.put(Snapshots.TITLE, mCurrentState.mTitle);
1886 values.put(Snapshots.URL, mCurrentState.mUrl);
John Reck68234a92012-04-19 15:27:12 -07001887 values.put(Snapshots.BACKGROUND, web.getPageBackgroundColor());
John Reck8cc92352011-07-06 17:41:52 -07001888 values.put(Snapshots.DATE_CREATED, System.currentTimeMillis());
1889 values.put(Snapshots.FAVICON, compressBitmap(getFavicon()));
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001890 values.put(Snapshots.THUMBNAIL, compressBitmap(bm));
John Reckd8c74522011-06-14 08:45:00 -07001891 return values;
John Reck541f55a2011-06-07 16:34:43 -07001892 }
1893
John Reck68234a92012-04-19 15:27:12 -07001894 /**
1895 * Probably want to call this on a background thread
1896 */
1897 public boolean saveViewState(ContentValues values) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001898 WebView web = getWebView();
John Reck68234a92012-04-19 15:27:12 -07001899 if (web == null) return false;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001900 String filename = UUID.randomUUID().toString();
John Reck68234a92012-04-19 15:27:12 -07001901 SaveCallback callback = new SaveCallback();
John Reck68234a92012-04-19 15:27:12 -07001902 try {
John Reck68234a92012-04-19 15:27:12 -07001903 synchronized (callback) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001904 web.saveViewState(filename, callback);
1905 callback.wait();
John Reck68234a92012-04-19 15:27:12 -07001906 }
John Reck68234a92012-04-19 15:27:12 -07001907 } catch (Exception e) {
1908 Log.w(LOGTAG, "Failed to save view state", e);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001909 String path = callback.getPath();
1910 if (path != null) {
1911 File file = mContext.getFileStreamPath(path);
1912 if (file.exists() && !file.delete()) {
1913 file.deleteOnExit();
1914 }
John Reck68234a92012-04-19 15:27:12 -07001915 }
1916 return false;
1917 }
Tarun Nainani8eb00912014-07-17 12:28:32 -07001918
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001919 String path = callback.getPath();
Tarun Nainani8eb00912014-07-17 12:28:32 -07001920 // could be that saving of file failed
1921 if (path == null) {
1922 return false;
1923 }
1924
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001925 File savedFile = new File(path);
1926 if (!savedFile.exists()) {
1927 return false;
John Reck68234a92012-04-19 15:27:12 -07001928 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001929 values.put(Snapshots.VIEWSTATE_PATH, path.substring(path.lastIndexOf('/') + 1));
1930 values.put(Snapshots.VIEWSTATE_SIZE, savedFile.length());
John Reck68234a92012-04-19 15:27:12 -07001931 return true;
1932 }
1933
John Reck8cc92352011-07-06 17:41:52 -07001934 public byte[] compressBitmap(Bitmap bitmap) {
1935 if (bitmap == null) {
1936 return null;
1937 }
1938 ByteArrayOutputStream stream = new ByteArrayOutputStream();
1939 bitmap.compress(CompressFormat.PNG, 100, stream);
1940 return stream.toByteArray();
1941 }
1942
John Reck26b18322011-06-21 13:08:58 -07001943 public void loadUrl(String url, Map<String, String> headers) {
1944 if (mMainView != null) {
Michael Kolba53c9892011-10-05 13:31:40 -07001945 mPageLoadProgress = INITIAL_PROGRESS;
Michael Kolb14612442011-06-24 13:06:29 -07001946 mCurrentState = new PageState(mContext, false, url, null);
John Reck26b18322011-06-21 13:08:58 -07001947 mMainView.loadUrl(url, headers);
1948 }
1949 }
1950
John Reck38b39652012-06-05 09:22:59 -07001951 public void disableUrlOverridingForLoad() {
1952 mDisableOverrideUrlLoading = true;
1953 }
1954
Michael Kolb9ef259a2011-07-12 15:33:08 -07001955 protected void capture() {
Vivek Sekhar6bdf6452015-05-12 17:38:45 -07001956 boolean returnEmptyCapture = false;
1957 if (mMainView == null || mCapture == null || !mMainView.isReady())
1958 returnEmptyCapture = true;
John Reck4eadc342011-10-31 14:04:10 -07001959 if (mMainView.getContentWidth() <= 0 || mMainView.getContentHeight() <= 0) {
Vivek Sekhar6bdf6452015-05-12 17:38:45 -07001960 returnEmptyCapture = true;
John Reck4eadc342011-10-31 14:04:10 -07001961 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07001962
Vivek Sekhar6bdf6452015-05-12 17:38:45 -07001963 if (returnEmptyCapture || !mFirstVisualPixelPainted || mMainView.isShowingCrashView()) {
Pankaj Garg79878492015-04-01 14:48:21 -07001964 mCapture = Bitmap.createBitmap(
1965 mCaptureWidth,
1966 mCaptureHeight,
1967 Bitmap.Config.RGB_565);
1968 mCapture.eraseColor(Color.WHITE);
1969
1970 mHandler.removeMessages(MSG_CAPTURE);
1971
1972 TabControl tc = mWebViewController.getTabControl();
1973 if (tc != null) {
1974 OnThumbnailUpdatedListener updateListener
1975 = tc.getOnThumbnailUpdatedListener();
1976 if (updateListener != null) {
1977 updateListener.onThumbnailUpdated(this);
1978 }
1979 }
1980 return;
1981 }
1982
Tarun Nainaniea28dde2014-08-27 17:25:09 -07001983 mMainView
1984 .getContentBitmapAsync(
1985 (float) mCaptureWidth / mMainView.getWidth(),
1986 new Rect(),
1987 new ValueCallback<Bitmap>() {
1988 @Override
1989 public void onReceiveValue(Bitmap bitmap) {
1990 onCaptureCallback(bitmap);
1991 }});
1992 }
1993
1994 private void onCaptureCallback(Bitmap bitmap) {
1995 if (mCapture == null || bitmap == null)
1996 return;
1997
Michael Kolb9ef259a2011-07-12 15:33:08 -07001998 Canvas c = new Canvas(mCapture);
Tarun Nainaniea28dde2014-08-27 17:25:09 -07001999 mCapture.eraseColor(Color.WHITE);
2000 c.drawBitmap(bitmap, 0, 0, null);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002001
Michael Kolba3194d02011-09-07 11:23:51 -07002002 // manually anti-alias the edges for the tilt
2003 c.drawRect(0, 0, 1, mCapture.getHeight(), sAlphaPaint);
2004 c.drawRect(mCapture.getWidth() - 1, 0, mCapture.getWidth(),
2005 mCapture.getHeight(), sAlphaPaint);
2006 c.drawRect(0, 0, mCapture.getWidth(), 1, sAlphaPaint);
2007 c.drawRect(0, mCapture.getHeight() - 1, mCapture.getWidth(),
2008 mCapture.getHeight(), sAlphaPaint);
Dianne Hackborn43cfe8a2011-08-02 16:59:35 -07002009 c.setBitmap(null);
John Reck8ee633f2011-08-09 16:00:35 -07002010 mHandler.removeMessages(MSG_CAPTURE);
John Reck1cf4b792011-07-26 10:22:22 -07002011 persistThumbnail();
John Reck8ee633f2011-08-09 16:00:35 -07002012 TabControl tc = mWebViewController.getTabControl();
2013 if (tc != null) {
2014 OnThumbnailUpdatedListener updateListener
2015 = tc.getOnThumbnailUpdatedListener();
2016 if (updateListener != null) {
2017 updateListener.onThumbnailUpdated(this);
2018 }
2019 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07002020 }
2021
2022 @Override
2023 public void onNewPicture(WebView view, Picture picture) {
John Reck1cf4b792011-07-26 10:22:22 -07002024 postCapture();
2025 }
2026
2027 private void postCapture() {
Michael Kolb9ef259a2011-07-12 15:33:08 -07002028 if (!mHandler.hasMessages(MSG_CAPTURE)) {
2029 mHandler.sendEmptyMessageDelayed(MSG_CAPTURE, CAPTURE_DELAY);
2030 }
2031 }
2032
John Reckef654f12011-07-12 16:42:08 -07002033 public boolean canGoBack() {
2034 return mMainView != null ? mMainView.canGoBack() : false;
2035 }
2036
2037 public boolean canGoForward() {
2038 return mMainView != null ? mMainView.canGoForward() : false;
2039 }
2040
2041 public void goBack() {
2042 if (mMainView != null) {
2043 mMainView.goBack();
2044 }
2045 }
2046
2047 public void goForward() {
2048 if (mMainView != null) {
2049 mMainView.goForward();
2050 }
2051 }
2052
John Reck1cf4b792011-07-26 10:22:22 -07002053 protected void persistThumbnail() {
John Reck4eadc342011-10-31 14:04:10 -07002054 DataController.getInstance(mContext).saveThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07002055 }
2056
2057 protected void deleteThumbnail() {
John Reck4eadc342011-10-31 14:04:10 -07002058 DataController.getInstance(mContext).deleteThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07002059 }
2060
John Reck4eadc342011-10-31 14:04:10 -07002061 void updateCaptureFromBlob(byte[] blob) {
John Reck1cf4b792011-07-26 10:22:22 -07002062 synchronized (Tab.this) {
2063 if (mCapture == null) {
2064 return;
2065 }
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002066 ByteBuffer buffer = ByteBuffer.wrap(blob);
Michael Kolbd837a112011-08-09 14:00:18 -07002067 try {
2068 mCapture.copyPixelsFromBuffer(buffer);
2069 } catch (RuntimeException rex) {
2070 Log.e(LOGTAG, "Load capture has mismatched sizes; buffer: "
2071 + buffer.capacity() + " blob: " + blob.length
2072 + "capture: " + mCapture.getByteCount());
2073 throw rex;
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002074 }
John Reck1cf4b792011-07-26 10:22:22 -07002075 }
2076 }
2077
John Reck52be4782011-08-26 15:37:29 -07002078 @Override
2079 public String toString() {
2080 StringBuilder builder = new StringBuilder(100);
2081 builder.append(mId);
2082 builder.append(") has parent: ");
2083 if (getParent() != null) {
2084 builder.append("true[");
2085 builder.append(getParent().getId());
2086 builder.append("]");
2087 } else {
2088 builder.append("false");
2089 }
2090 builder.append(", incog: ");
2091 builder.append(isPrivateBrowsingEnabled());
2092 if (!isPrivateBrowsingEnabled()) {
2093 builder.append(", title: ");
2094 builder.append(getTitle());
2095 builder.append(", url: ");
2096 builder.append(getUrl());
2097 }
2098 return builder.toString();
2099 }
2100
Steve Block4895b012011-10-03 16:26:46 +01002101 private void handleProceededAfterSslError(SslError error) {
2102 if (error.getUrl().equals(mCurrentState.mUrl)) {
2103 // The security state should currently be SECURITY_STATE_SECURE.
2104 setSecurityState(SecurityState.SECURITY_STATE_BAD_CERTIFICATE);
Steve Block08a6f0c2011-10-06 12:12:53 +01002105 mCurrentState.mSslCertificateError = error;
Steve Block4895b012011-10-03 16:26:46 +01002106 } else if (getSecurityState() == SecurityState.SECURITY_STATE_SECURE) {
Steve Block08a6f0c2011-10-06 12:12:53 +01002107 // The page's main resource is secure and this error is for a
2108 // sub-resource.
Steve Block4895b012011-10-03 16:26:46 +01002109 setSecurityState(SecurityState.SECURITY_STATE_MIXED);
2110 }
2111 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002112}