blob: 6d36d9c9f389b87d7490d75ea8db76001ce4da06 [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;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080026import android.content.res.Resources;
Grace Kloba22ac16e2009-10-07 18:00:23 -070027import android.graphics.Bitmap;
John Reck8cc92352011-07-06 17:41:52 -070028import android.graphics.Bitmap.CompressFormat;
Michael Kolb9ef259a2011-07-12 15:33:08 -070029import android.graphics.BitmapFactory;
30import android.graphics.Canvas;
Michael Kolbc3af0672011-08-09 10:24:41 -070031import android.graphics.Color;
Michael Kolba3194d02011-09-07 11:23:51 -070032import android.graphics.Paint;
Michael Kolb9ef259a2011-07-12 15:33:08 -070033import android.graphics.Picture;
Michael Kolba3194d02011-09-07 11:23:51 -070034import android.graphics.PorterDuff;
35import android.graphics.PorterDuffXfermode;
Grace Kloba22ac16e2009-10-07 18:00:23 -070036import android.net.Uri;
37import android.net.http.SslError;
Grace Kloba22ac16e2009-10-07 18:00:23 -070038import android.os.Bundle;
Michael Kolb9ef259a2011-07-12 15:33:08 -070039import android.os.Handler;
Grace Kloba22ac16e2009-10-07 18:00:23 -070040import android.os.Message;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +000041import android.os.SystemClock;
Brian Carlstrom8862c1d2011-06-02 01:05:55 -070042import android.security.KeyChain;
Brian Carlstromaa09cd82011-06-09 16:04:40 -070043import android.security.KeyChainAliasCallback;
John Reck24f18262011-06-17 14:47:20 -070044import android.text.TextUtils;
Grace Kloba22ac16e2009-10-07 18:00:23 -070045import android.util.Log;
46import android.view.KeyEvent;
47import android.view.LayoutInflater;
48import android.view.View;
Grace Kloba50c241e2010-04-20 11:07:50 -070049import android.view.ViewStub;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080050import android.view.View.OnClickListener;
Ben Murdochc42addf2010-01-28 15:19:59 +000051import android.webkit.ConsoleMessage;
Grace Kloba22ac16e2009-10-07 18:00:23 -070052import android.webkit.GeolocationPermissions;
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;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080060import android.widget.FrameLayout;
61import android.widget.Button;
Grace Kloba22ac16e2009-10-07 18:00:23 -070062
Bijan Amirzada41242f22014-03-21 12:12:18 -070063import com.android.browser.R;
64import com.android.browser.TabControl.OnThumbnailUpdatedListener;
65import com.android.browser.homepages.HomeProvider;
66import com.android.browser.mynavigation.MyNavigationUtil;
67import com.android.browser.provider.MyNavigationProvider;
68import com.android.browser.provider.SnapshotProvider.Snapshots;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080069
70import org.codeaurora.swe.BrowserDownloadListener;
71import org.codeaurora.swe.ClientCertRequestHandler;
72import org.codeaurora.swe.HttpAuthHandler;
73import org.codeaurora.swe.SslErrorHandler;
74import org.codeaurora.swe.WebBackForwardList;
75import org.codeaurora.swe.WebBackForwardListClient;
76import org.codeaurora.swe.WebChromeClient;
77import org.codeaurora.swe.WebHistoryItem;
78import org.codeaurora.swe.WebView;
79import org.codeaurora.swe.WebView.PictureListener;
80import org.codeaurora.swe.WebViewClient;
81
John Reck541f55a2011-06-07 16:34:43 -070082
83import java.io.ByteArrayOutputStream;
John Reck2b71d6d2012-04-18 17:42:06 -070084import java.io.File;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080085import java.io.InputStream;
John Reck1cf4b792011-07-26 10:22:22 -070086import java.nio.ByteBuffer;
Michael Kolbfe251992010-07-08 15:41:55 -070087import java.util.LinkedList;
88import java.util.Map;
John Reck2b71d6d2012-04-18 17:42:06 -070089import java.util.UUID;
Michael Kolbfe251992010-07-08 15:41:55 -070090import java.util.Vector;
Mathew Inwooda829d552011-09-02 14:16:25 +010091import java.util.regex.Pattern;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080092import java.sql.Timestamp;
93import java.util.Date;
Michael Kolbfe251992010-07-08 15:41:55 -070094
Grace Kloba22ac16e2009-10-07 18:00:23 -070095/**
96 * Class for maintaining Tabs with a main WebView and a subwindow.
97 */
Michael Kolb9ef259a2011-07-12 15:33:08 -070098class Tab implements PictureListener {
Michael Kolb8233fac2010-10-26 16:08:53 -070099
Grace Kloba22ac16e2009-10-07 18:00:23 -0700100 // Log Tag
101 private static final String LOGTAG = "Tab";
Bijan Amirzada41242f22014-03-21 12:12:18 -0700102 private static final boolean LOGD_ENABLED = com.android.browser.Browser.LOGD_ENABLED;
Ben Murdochc42addf2010-01-28 15:19:59 +0000103 // Special case the logtag for messages for the Console to make it easier to
104 // filter them and match the logtag used for these messages in older versions
105 // of the browser.
106 private static final String CONSOLE_LOGTAG = "browser";
107
Michael Kolb9ef259a2011-07-12 15:33:08 -0700108 private static final int MSG_CAPTURE = 42;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800109 private static final int CAPTURE_DELAY = 1000;
Michael Kolba53c9892011-10-05 13:31:40 -0700110 private static final int INITIAL_PROGRESS = 5;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700111
John Reck1cf4b792011-07-26 10:22:22 -0700112 private static Bitmap sDefaultFavicon;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800113 protected boolean hasCrashed = false;
John Reck1cf4b792011-07-26 10:22:22 -0700114
Michael Kolba3194d02011-09-07 11:23:51 -0700115 private static Paint sAlphaPaint = new Paint();
116 static {
117 sAlphaPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
118 sAlphaPaint.setColor(Color.TRANSPARENT);
119 }
120
Steve Block2466eff2011-10-03 15:33:09 +0100121 public enum SecurityState {
Steve Block4895b012011-10-03 16:26:46 +0100122 // The page's main resource does not use SSL. Note that we use this
123 // state irrespective of the SSL authentication state of sub-resources.
Steve Block2466eff2011-10-03 15:33:09 +0100124 SECURITY_STATE_NOT_SECURE,
Steve Block4895b012011-10-03 16:26:46 +0100125 // The page's main resource uses SSL and the certificate is good. The
126 // same is true of all sub-resources.
Steve Block2466eff2011-10-03 15:33:09 +0100127 SECURITY_STATE_SECURE,
Steve Block4895b012011-10-03 16:26:46 +0100128 // The page's main resource uses SSL and the certificate is good, but
129 // some sub-resources either do not use SSL or have problems with their
130 // certificates.
Steve Block2466eff2011-10-03 15:33:09 +0100131 SECURITY_STATE_MIXED,
Steve Block4895b012011-10-03 16:26:46 +0100132 // The page's main resource uses SSL but there is a problem with its
133 // certificate.
134 SECURITY_STATE_BAD_CERTIFICATE,
John Reck30c714c2010-12-16 17:30:34 -0800135 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700136
Michael Kolb14612442011-06-24 13:06:29 -0700137 Context mContext;
John Reckd8c74522011-06-14 08:45:00 -0700138 protected WebViewController mWebViewController;
Michael Kolb8233fac2010-10-26 16:08:53 -0700139
Michael Kolbc831b632011-05-11 09:30:34 -0700140 // The tab ID
John Reckd8c74522011-06-14 08:45:00 -0700141 private long mId = -1;
Michael Kolbc831b632011-05-11 09:30:34 -0700142
Grace Kloba22ac16e2009-10-07 18:00:23 -0700143 // The Geolocation permissions prompt
144 private GeolocationPermissionsPrompt mGeolocationPermissionsPrompt;
145 // Main WebView wrapper
Michael Kolba713ec82010-11-29 17:27:06 -0800146 private View mContainer;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700147 // Main WebView
148 private WebView mMainView;
149 // Subwindow container
150 private View mSubViewContainer;
151 // Subwindow WebView
152 private WebView mSubView;
153 // Saved bundle for when we are running low on memory. It contains the
154 // information needed to restore the WebView if the user goes back to the
155 // tab.
156 private Bundle mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700157 // Parent Tab. This is the Tab that created this Tab, or null if the Tab was
158 // created by the UI
Michael Kolbc831b632011-05-11 09:30:34 -0700159 private Tab mParent;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700160 // Tab that constructed by this Tab. This is used when this Tab is
161 // destroyed, it clears all mParentTab values in the children.
Michael Kolbc831b632011-05-11 09:30:34 -0700162 private Vector<Tab> mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700163 // If true, the tab is in the foreground of the current activity.
164 private boolean mInForeground;
Michael Kolb8233fac2010-10-26 16:08:53 -0700165 // If true, the tab is in page loading state (after onPageStarted,
166 // before onPageFinsihed)
167 private boolean mInPageLoad;
John Reck38b39652012-06-05 09:22:59 -0700168 private boolean mDisableOverrideUrlLoading;
John Reck30c714c2010-12-16 17:30:34 -0800169 // The last reported progress of the current page
170 private int mPageLoadProgress;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000171 // The time the load started, used to find load page time
172 private long mLoadStartTime;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700173 // Application identifier used to find tabs that another application wants
174 // to reuse.
175 private String mAppId;
Michael Kolbe28b3472011-08-04 16:54:31 -0700176 // flag to indicate if tab should be closed on back
177 private boolean mCloseOnBack;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700178 // Keep the original url around to avoid killing the old WebView if the url
179 // has not changed.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700180 // Error console for the tab
181 private ErrorConsoleView mErrorConsole;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -0500182 // The listener that gets invoked when a download is started from the
183 // mMainView
Selim Gurun0b3d66f2012-08-29 13:08:13 -0700184 private final BrowserDownloadListener mDownloadListener;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500185 // Listener used to know when we move forward or back in the history list.
186 private final WebBackForwardListClient mWebBackForwardListClient;
John Recke969cc52010-12-21 17:24:43 -0800187 private DataController mDataController;
Patrick Scott92066772011-03-10 08:46:27 -0500188 // State of the auto-login request.
189 private DeviceAccountLogin mDeviceAccountLogin;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700190
191 // AsyncTask for downloading touch icons
192 DownloadTouchIcon mTouchIconLoader;
193
John Reck35e9dd62011-04-25 09:01:54 -0700194 private BrowserSettings mSettings;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700195 private int mCaptureWidth;
196 private int mCaptureHeight;
197 private Bitmap mCapture;
198 private Handler mHandler;
Michael Kolb72864272012-05-03 15:42:15 -0700199 private boolean mUpdateThumbnail;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800200 private Timestamp timestamp;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700201
Mathew Inwood1dd8e822011-08-03 14:34:29 +0100202 /**
Mathew Inwood9ad1eac2011-09-15 11:29:50 +0100203 * See {@link #clearBackStackWhenItemAdded(String)}.
Mathew Inwood1dd8e822011-08-03 14:34:29 +0100204 */
Mathew Inwooda829d552011-09-02 14:16:25 +0100205 private Pattern mClearHistoryUrlPattern;
Mathew Inwood1dd8e822011-08-03 14:34:29 +0100206
John Reck1cf4b792011-07-26 10:22:22 -0700207 private static synchronized Bitmap getDefaultFavicon(Context context) {
208 if (sDefaultFavicon == null) {
209 sDefaultFavicon = BitmapFactory.decodeResource(
210 context.getResources(), R.drawable.app_web_browser_sm);
211 }
212 return sDefaultFavicon;
213 }
Michael Kolbeb95db42011-03-03 10:38:40 -0800214
John Reck30c714c2010-12-16 17:30:34 -0800215 // All the state needed for a page
John Reckd8c74522011-06-14 08:45:00 -0700216 protected static class PageState {
John Reck30c714c2010-12-16 17:30:34 -0800217 String mUrl;
John Reckdb22ec42011-06-29 11:31:24 -0700218 String mOriginalUrl;
John Reck30c714c2010-12-16 17:30:34 -0800219 String mTitle;
Steve Block2466eff2011-10-03 15:33:09 +0100220 SecurityState mSecurityState;
Steve Block08a6f0c2011-10-06 12:12:53 +0100221 // This is non-null only when mSecurityState is SECURITY_STATE_BAD_CERTIFICATE.
222 SslError mSslCertificateError;
John Reck30c714c2010-12-16 17:30:34 -0800223 Bitmap mFavicon;
Steve Block08a6f0c2011-10-06 12:12:53 +0100224 boolean mIsBookmarkedSite;
225 boolean mIncognito;
John Reck30c714c2010-12-16 17:30:34 -0800226
227 PageState(Context c, boolean incognito) {
John Reck502a3532011-08-16 14:21:46 -0700228 mIncognito = incognito;
229 if (mIncognito) {
John Reckdb22ec42011-06-29 11:31:24 -0700230 mOriginalUrl = mUrl = "browser:incognito";
John Reck30c714c2010-12-16 17:30:34 -0800231 mTitle = c.getString(R.string.new_incognito_tab);
John Reck30c714c2010-12-16 17:30:34 -0800232 } else {
John Reckdb22ec42011-06-29 11:31:24 -0700233 mOriginalUrl = mUrl = "";
John Reck30c714c2010-12-16 17:30:34 -0800234 mTitle = c.getString(R.string.new_tab);
John Reck30c714c2010-12-16 17:30:34 -0800235 }
Steve Block2466eff2011-10-03 15:33:09 +0100236 mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
John Reck30c714c2010-12-16 17:30:34 -0800237 }
238
239 PageState(Context c, boolean incognito, String url, Bitmap favicon) {
John Reck502a3532011-08-16 14:21:46 -0700240 mIncognito = incognito;
John Reckdb22ec42011-06-29 11:31:24 -0700241 mOriginalUrl = mUrl = url;
John Reck30c714c2010-12-16 17:30:34 -0800242 if (URLUtil.isHttpsUrl(url)) {
Steve Block2466eff2011-10-03 15:33:09 +0100243 mSecurityState = SecurityState.SECURITY_STATE_SECURE;
John Reck30c714c2010-12-16 17:30:34 -0800244 } else {
Steve Block2466eff2011-10-03 15:33:09 +0100245 mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
John Reck30c714c2010-12-16 17:30:34 -0800246 }
John Reck1cf4b792011-07-26 10:22:22 -0700247 mFavicon = favicon;
John Reck30c714c2010-12-16 17:30:34 -0800248 }
John Reck1cf4b792011-07-26 10:22:22 -0700249
Grace Kloba22ac16e2009-10-07 18:00:23 -0700250 }
251
John Reck30c714c2010-12-16 17:30:34 -0800252 // The current/loading page's state
John Reckd8c74522011-06-14 08:45:00 -0700253 protected PageState mCurrentState;
John Reck30c714c2010-12-16 17:30:34 -0800254
Grace Kloba22ac16e2009-10-07 18:00:23 -0700255 // Used for saving and restoring each Tab
Michael Kolbc831b632011-05-11 09:30:34 -0700256 static final String ID = "ID";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700257 static final String CURRURL = "currentUrl";
258 static final String CURRTITLE = "currentTitle";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700259 static final String PARENTTAB = "parentTab";
260 static final String APPID = "appid";
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700261 static final String INCOGNITO = "privateBrowsingEnabled";
John Reckb0a86db2011-05-24 14:05:58 -0700262 static final String USERAGENT = "useragent";
Michael Kolbe28b3472011-08-04 16:54:31 -0700263 static final String CLOSEFLAG = "closeOnBack";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700264
Grace Kloba22ac16e2009-10-07 18:00:23 -0700265 // Container class for the next error dialog that needs to be displayed
266 private class ErrorDialog {
267 public final int mTitle;
268 public final String mDescription;
269 public final int mError;
270 ErrorDialog(int title, String desc, int error) {
271 mTitle = title;
272 mDescription = desc;
273 mError = error;
274 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700275 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700276
277 private void processNextError() {
278 if (mQueuedErrors == null) {
279 return;
280 }
281 // The first one is currently displayed so just remove it.
282 mQueuedErrors.removeFirst();
283 if (mQueuedErrors.size() == 0) {
284 mQueuedErrors = null;
285 return;
286 }
287 showError(mQueuedErrors.getFirst());
288 }
289
290 private DialogInterface.OnDismissListener mDialogListener =
291 new DialogInterface.OnDismissListener() {
292 public void onDismiss(DialogInterface d) {
293 processNextError();
294 }
295 };
296 private LinkedList<ErrorDialog> mQueuedErrors;
297
298 private void queueError(int err, String desc) {
299 if (mQueuedErrors == null) {
300 mQueuedErrors = new LinkedList<ErrorDialog>();
301 }
302 for (ErrorDialog d : mQueuedErrors) {
303 if (d.mError == err) {
304 // Already saw a similar error, ignore the new one.
305 return;
306 }
307 }
308 ErrorDialog errDialog = new ErrorDialog(
309 err == WebViewClient.ERROR_FILE_NOT_FOUND ?
310 R.string.browserFrameFileErrorLabel :
311 R.string.browserFrameNetworkErrorLabel,
312 desc, err);
313 mQueuedErrors.addLast(errDialog);
314
315 // Show the dialog now if the queue was empty and it is in foreground
316 if (mQueuedErrors.size() == 1 && mInForeground) {
317 showError(errDialog);
318 }
319 }
320
321 private void showError(ErrorDialog errDialog) {
322 if (mInForeground) {
Michael Kolb14612442011-06-24 13:06:29 -0700323 AlertDialog d = new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700324 .setTitle(errDialog.mTitle)
325 .setMessage(errDialog.mDescription)
326 .setPositiveButton(R.string.ok, null)
327 .create();
328 d.setOnDismissListener(mDialogListener);
329 d.show();
330 }
331 }
332
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800333 protected void replaceCrashView(View view, View container) {
334 if (hasCrashed && (view == mMainView)) {
335 final FrameLayout wrapper = (FrameLayout) container.findViewById(R.id.webview_wrapper);
336 wrapper.removeAllViewsInLayout();
337 wrapper.addView(view);
338 hasCrashed = false;
339 }
340 }
341
342 protected void showCrashView() {
343 if (hasCrashed) {
344 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
345 Context.LAYOUT_INFLATER_SERVICE);
346 final View crashLayout = inflater.inflate(R.layout.browser_tab_crash, null);
347 final FrameLayout wrapper =
348 (FrameLayout) mContainer.findViewById(R.id.webview_wrapper);
349 wrapper.removeAllViewsInLayout();
350 wrapper.addView(crashLayout);
351 mContainer.requestFocus();
352 Button reloadBtn = (Button) crashLayout.findViewById(R.id.browser_crash_reload_btn);
353 reloadBtn.setOnClickListener(new OnClickListener() {
354 @Override
355 public void onClick(View arg0) {
356 replaceCrashView(mMainView, mContainer);
357 mMainView.reload();
358 }
359 });
360 }
361 }
362
Grace Kloba22ac16e2009-10-07 18:00:23 -0700363 // -------------------------------------------------------------------------
364 // WebViewClient implementation for the main WebView
365 // -------------------------------------------------------------------------
366
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800367 private final WebViewClient mWebViewClient = new WebViewClient() {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500368 private Message mDontResend;
369 private Message mResend;
Michael Kolb47bd1e42011-09-01 15:25:00 -0700370
371 private boolean providersDiffer(String url, String otherUrl) {
372 Uri uri1 = Uri.parse(url);
373 Uri uri2 = Uri.parse(otherUrl);
374 return !uri1.getEncodedAuthority().equals(uri2.getEncodedAuthority());
375 }
376
Grace Kloba22ac16e2009-10-07 18:00:23 -0700377 @Override
378 public void onPageStarted(WebView view, String url, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700379 mInPageLoad = true;
Michael Kolb72864272012-05-03 15:42:15 -0700380 mUpdateThumbnail = true;
Michael Kolba53c9892011-10-05 13:31:40 -0700381 mPageLoadProgress = INITIAL_PROGRESS;
Michael Kolb14612442011-06-24 13:06:29 -0700382 mCurrentState = new PageState(mContext,
John Reck30c714c2010-12-16 17:30:34 -0800383 view.isPrivateBrowsingEnabled(), url, favicon);
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000384 mLoadStartTime = SystemClock.uptimeMillis();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700385
386 // If we start a touch icon load and then load a new page, we don't
387 // want to cancel the current touch icon loader. But, we do want to
388 // create a new one when the touch icon url is known.
389 if (mTouchIconLoader != null) {
390 mTouchIconLoader.mTab = null;
391 mTouchIconLoader = null;
392 }
393
394 // reset the error console
395 if (mErrorConsole != null) {
396 mErrorConsole.clearErrorMessages();
Michael Kolb8233fac2010-10-26 16:08:53 -0700397 if (mWebViewController.shouldShowErrorConsole()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700398 mErrorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
399 }
400 }
401
Patrick Scott92066772011-03-10 08:46:27 -0500402 // Cancel the auto-login process.
403 if (mDeviceAccountLogin != null) {
404 mDeviceAccountLogin.cancel();
405 mDeviceAccountLogin = null;
406 mWebViewController.hideAutoLogin(Tab.this);
407 }
408
Grace Kloba22ac16e2009-10-07 18:00:23 -0700409 // finally update the UI in the activity if it is in the foreground
John Reck324d4402011-01-11 16:56:42 -0800410 mWebViewController.onPageStarted(Tab.this, view, favicon);
Leon Scroggins4cd97792010-12-03 15:31:56 -0500411
John Recke969cc52010-12-21 17:24:43 -0800412 updateBookmarkedStatus();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700413 }
414
415 @Override
416 public void onPageFinished(WebView view, String url) {
John Reck38b39652012-06-05 09:22:59 -0700417 mDisableOverrideUrlLoading = false;
John Reck5b691842010-11-29 11:21:13 -0800418 if (!isPrivateBrowsingEnabled()) {
419 LogTag.logPageFinishedLoading(
420 url, SystemClock.uptimeMillis() - mLoadStartTime);
421 }
John Reck1cf4b792011-07-26 10:22:22 -0700422 syncCurrentState(view, url);
John Reck324d4402011-01-11 16:56:42 -0800423 mWebViewController.onPageFinished(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700424 }
425
426 // return true if want to hijack the url to let another app to handle it
427 @Override
428 public boolean shouldOverrideUrlLoading(WebView view, String url) {
John Reck38b39652012-06-05 09:22:59 -0700429 if (!mDisableOverrideUrlLoading && mInForeground) {
Michael Kolb18eb3772010-12-10 14:29:51 -0800430 return mWebViewController.shouldOverrideUrlLoading(Tab.this,
431 view, url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700432 } else {
433 return false;
434 }
435 }
436
437 /**
Steve Block2466eff2011-10-03 15:33:09 +0100438 * Updates the security state. This method is called when we discover
439 * another resource to be loaded for this page (for example,
440 * javascript). While we update the security state, we do not update
441 * the lock icon until we are done loading, as it is slightly more
442 * secure this way.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700443 */
444 @Override
445 public void onLoadResource(WebView view, String url) {
446 if (url != null && url.length() > 0) {
447 // It is only if the page claims to be secure that we may have
Steve Block2466eff2011-10-03 15:33:09 +0100448 // to update the security state:
449 if (mCurrentState.mSecurityState == SecurityState.SECURITY_STATE_SECURE) {
450 // If NOT a 'safe' url, change the state to mixed content!
Grace Kloba22ac16e2009-10-07 18:00:23 -0700451 if (!(URLUtil.isHttpsUrl(url) || URLUtil.isDataUrl(url)
452 || URLUtil.isAboutUrl(url))) {
Steve Block2466eff2011-10-03 15:33:09 +0100453 mCurrentState.mSecurityState = SecurityState.SECURITY_STATE_MIXED;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700454 }
455 }
456 }
457 }
458
459 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700460 * Show a dialog informing the user of the network error reported by
461 * WebCore if it is in the foreground.
462 */
463 @Override
464 public void onReceivedError(WebView view, int errorCode,
465 String description, String failingUrl) {
466 if (errorCode != WebViewClient.ERROR_HOST_LOOKUP &&
467 errorCode != WebViewClient.ERROR_CONNECT &&
468 errorCode != WebViewClient.ERROR_BAD_URL &&
469 errorCode != WebViewClient.ERROR_UNSUPPORTED_SCHEME &&
470 errorCode != WebViewClient.ERROR_FILE) {
471 queueError(errorCode, description);
Jeff Hamilton47654f42010-09-07 09:57:51 -0500472
Selim Gurun3da06b82011-10-10 13:58:12 -0700473 // Don't log URLs when in private browsing mode
474 if (!isPrivateBrowsingEnabled()) {
475 Log.e(LOGTAG, "onReceivedError " + errorCode + " " + failingUrl
Jeff Hamilton47654f42010-09-07 09:57:51 -0500476 + " " + description);
Selim Gurun3da06b82011-10-10 13:58:12 -0700477 }
Jeff Hamilton47654f42010-09-07 09:57:51 -0500478 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700479 }
480
481 /**
482 * Check with the user if it is ok to resend POST data as the page they
483 * are trying to navigate to is the result of a POST.
484 */
485 @Override
486 public void onFormResubmission(WebView view, final Message dontResend,
487 final Message resend) {
488 if (!mInForeground) {
489 dontResend.sendToTarget();
490 return;
491 }
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500492 if (mDontResend != null) {
493 Log.w(LOGTAG, "onFormResubmission should not be called again "
494 + "while dialog is still up");
495 dontResend.sendToTarget();
496 return;
497 }
498 mDontResend = dontResend;
499 mResend = resend;
Michael Kolb14612442011-06-24 13:06:29 -0700500 new AlertDialog.Builder(mContext).setTitle(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700501 R.string.browserFrameFormResubmitLabel).setMessage(
502 R.string.browserFrameFormResubmitMessage)
503 .setPositiveButton(R.string.ok,
504 new DialogInterface.OnClickListener() {
505 public void onClick(DialogInterface dialog,
506 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500507 if (mResend != null) {
508 mResend.sendToTarget();
509 mResend = null;
510 mDontResend = null;
511 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700512 }
513 }).setNegativeButton(R.string.cancel,
514 new DialogInterface.OnClickListener() {
515 public void onClick(DialogInterface dialog,
516 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500517 if (mDontResend != null) {
518 mDontResend.sendToTarget();
519 mResend = null;
520 mDontResend = null;
521 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700522 }
523 }).setOnCancelListener(new OnCancelListener() {
524 public void onCancel(DialogInterface dialog) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500525 if (mDontResend != null) {
526 mDontResend.sendToTarget();
527 mResend = null;
528 mDontResend = null;
529 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700530 }
531 }).show();
532 }
533
534 /**
535 * Insert the url into the visited history database.
536 * @param url The url to be inserted.
537 * @param isReload True if this url is being reloaded.
538 * FIXME: Not sure what to do when reloading the page.
539 */
540 @Override
541 public void doUpdateVisitedHistory(WebView view, String url,
542 boolean isReload) {
John Reck324d4402011-01-11 16:56:42 -0800543 mWebViewController.doUpdateVisitedHistory(Tab.this, isReload);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700544 }
545
546 /**
547 * Displays SSL error(s) dialog to the user.
548 */
549 @Override
550 public void onReceivedSslError(final WebView view,
551 final SslErrorHandler handler, final SslError error) {
552 if (!mInForeground) {
553 handler.cancel();
Steve Block2466eff2011-10-03 15:33:09 +0100554 setSecurityState(SecurityState.SECURITY_STATE_NOT_SECURE);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700555 return;
556 }
John Reck35e9dd62011-04-25 09:01:54 -0700557 if (mSettings.showSecurityWarnings()) {
John Reckcb28b2c2011-08-26 17:39:44 -0700558 new AlertDialog.Builder(mContext)
559 .setTitle(R.string.security_warning)
560 .setMessage(R.string.ssl_warnings_header)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200561 .setIconAttribute(android.R.attr.alertDialogIcon)
John Reckcb28b2c2011-08-26 17:39:44 -0700562 .setPositiveButton(R.string.ssl_continue,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700563 new DialogInterface.OnClickListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700564 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700565 public void onClick(DialogInterface dialog,
566 int whichButton) {
567 handler.proceed();
Steve Block4895b012011-10-03 16:26:46 +0100568 handleProceededAfterSslError(error);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700569 }
John Reckcb28b2c2011-08-26 17:39:44 -0700570 })
571 .setNeutralButton(R.string.view_certificate,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700572 new DialogInterface.OnClickListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700573 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700574 public void onClick(DialogInterface dialog,
575 int whichButton) {
John Reckcb28b2c2011-08-26 17:39:44 -0700576 mWebViewController.showSslCertificateOnError(
577 view, handler, error);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700578 }
John Reckcb28b2c2011-08-26 17:39:44 -0700579 })
580 .setNegativeButton(R.string.ssl_go_back,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700581 new DialogInterface.OnClickListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700582 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700583 public void onClick(DialogInterface dialog,
584 int whichButton) {
John Reck30c714c2010-12-16 17:30:34 -0800585 dialog.cancel();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700586 }
John Reckcb28b2c2011-08-26 17:39:44 -0700587 })
588 .setOnCancelListener(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700589 new DialogInterface.OnCancelListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700590 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700591 public void onCancel(DialogInterface dialog) {
592 handler.cancel();
Steve Block2466eff2011-10-03 15:33:09 +0100593 setSecurityState(SecurityState.SECURITY_STATE_NOT_SECURE);
John Reck30c714c2010-12-16 17:30:34 -0800594 mWebViewController.onUserCanceledSsl(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700595 }
John Reckcb28b2c2011-08-26 17:39:44 -0700596 })
597 .show();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700598 } else {
599 handler.proceed();
600 }
601 }
602
603 /**
Steve Block4895b012011-10-03 16:26:46 +0100604 * Called when an SSL error occurred while loading a resource, but the
605 * WebView but chose to proceed anyway based on a decision retained
606 * from a previous response to onReceivedSslError(). We update our
607 * security state to reflect this.
608 */
609 @Override
610 public void onProceededAfterSslError(WebView view, SslError error) {
611 handleProceededAfterSslError(error);
612 }
613
614 /**
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700615 * Displays client certificate request to the user.
616 */
617 @Override
618 public void onReceivedClientCertRequest(final WebView view,
619 final ClientCertRequestHandler handler, final String host_and_port) {
620 if (!mInForeground) {
621 handler.ignore();
622 return;
623 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700624 int colon = host_and_port.lastIndexOf(':');
625 String host;
626 int port;
627 if (colon == -1) {
628 host = host_and_port;
629 port = -1;
630 } else {
631 String portString = host_and_port.substring(colon + 1);
632 try {
633 port = Integer.parseInt(portString);
634 host = host_and_port.substring(0, colon);
635 } catch (NumberFormatException e) {
636 host = host_and_port;
637 port = -1;
638 }
639 }
Michael Kolb14612442011-06-24 13:06:29 -0700640 KeyChain.choosePrivateKeyAlias(
641 mWebViewController.getActivity(), new KeyChainAliasCallback() {
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700642 @Override public void alias(String alias) {
643 if (alias == null) {
644 handler.cancel();
645 return;
646 }
Michael Kolb14612442011-06-24 13:06:29 -0700647 new KeyChainLookup(mContext, handler, alias).execute();
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700648 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700649 }, null, null, host, port, null);
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700650 }
651
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800652 @Override
653 public void onRendererCrash(WebView view, boolean crashedWhileOomProtected) {
654 Log.e(LOGTAG, "Tab Crashed");
655 hasCrashed = true;
656 showCrashView();
657 }
658
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700659 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700660 * Handles an HTTP authentication request.
661 *
662 * @param handler The authentication handler
663 * @param host The host
664 * @param realm The realm
665 */
666 @Override
667 public void onReceivedHttpAuthRequest(WebView view,
668 final HttpAuthHandler handler, final String host,
669 final String realm) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700670 mWebViewController.onReceivedHttpAuthRequest(Tab.this, view, handler, host, realm);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700671 }
672
673 @Override
John Reck438bf462011-01-12 18:11:46 -0800674 public WebResourceResponse shouldInterceptRequest(WebView view,
675 String url) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800676 //intercept if opening a new incognito tab - show the incognito welcome page
677 if (url.startsWith("browser:incognito")) {
678 Resources resourceHandle = mContext.getResources();
679 InputStream inStream = resourceHandle.openRawResource(
Bijan Amirzada41242f22014-03-21 12:12:18 -0700680 com.android.browser.R.raw.incognito_mode_start_page);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800681 return new WebResourceResponse("text/html", "utf8", inStream);
682 }
kaiyiz6e5b3e02013-08-19 20:02:01 +0800683 WebResourceResponse res;
684 if (MyNavigationUtil.MY_NAVIGATION.equals(url)) {
685 res = MyNavigationProvider.shouldInterceptRequest(mContext, url);
686 } else {
687 res = HomeProvider.shouldInterceptRequest(mContext, url);
688 }
John Reck438bf462011-01-12 18:11:46 -0800689 return res;
690 }
691
692 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700693 public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
694 if (!mInForeground) {
695 return false;
696 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700697 return mWebViewController.shouldOverrideKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700698 }
699
700 @Override
701 public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700702 if (!mInForeground) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700703 return;
704 }
John Reck997b1b72012-04-19 18:08:25 -0700705 if (!mWebViewController.onUnhandledKeyEvent(event)) {
706 super.onUnhandledKeyEvent(view, event);
707 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700708 }
Patrick Scott92066772011-03-10 08:46:27 -0500709
710 @Override
711 public void onReceivedLoginRequest(WebView view, String realm,
712 String account, String args) {
Michael Kolb14612442011-06-24 13:06:29 -0700713 new DeviceAccountLogin(mWebViewController.getActivity(), view, Tab.this, mWebViewController)
Patrick Scott92066772011-03-10 08:46:27 -0500714 .handleLogin(realm, account, args);
715 }
716
Grace Kloba22ac16e2009-10-07 18:00:23 -0700717 };
718
John Reck1cf4b792011-07-26 10:22:22 -0700719 private void syncCurrentState(WebView view, String url) {
720 // Sync state (in case of stop/timeout)
721 mCurrentState.mUrl = view.getUrl();
722 if (mCurrentState.mUrl == null) {
John Reck8bcafc12011-08-29 16:43:02 -0700723 mCurrentState.mUrl = "";
John Reck1cf4b792011-07-26 10:22:22 -0700724 }
725 mCurrentState.mOriginalUrl = view.getOriginalUrl();
726 mCurrentState.mTitle = view.getTitle();
727 mCurrentState.mFavicon = view.getFavicon();
728 if (!URLUtil.isHttpsUrl(mCurrentState.mUrl)) {
729 // In case we stop when loading an HTTPS page from an HTTP page
730 // but before a provisional load occurred
Steve Block2466eff2011-10-03 15:33:09 +0100731 mCurrentState.mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
Steve Block08a6f0c2011-10-06 12:12:53 +0100732 mCurrentState.mSslCertificateError = null;
John Reck1cf4b792011-07-26 10:22:22 -0700733 }
John Reck502a3532011-08-16 14:21:46 -0700734 mCurrentState.mIncognito = view.isPrivateBrowsingEnabled();
John Reck1cf4b792011-07-26 10:22:22 -0700735 }
736
Patrick Scott92066772011-03-10 08:46:27 -0500737 // Called by DeviceAccountLogin when the Tab needs to have the auto-login UI
738 // displayed.
739 void setDeviceAccountLogin(DeviceAccountLogin login) {
740 mDeviceAccountLogin = login;
741 }
742
743 // Returns non-null if the title bar should display the auto-login UI.
744 DeviceAccountLogin getDeviceAccountLogin() {
745 return mDeviceAccountLogin;
746 }
747
Grace Kloba22ac16e2009-10-07 18:00:23 -0700748 // -------------------------------------------------------------------------
749 // WebChromeClient implementation for the main WebView
750 // -------------------------------------------------------------------------
751
752 private final WebChromeClient mWebChromeClient = new WebChromeClient() {
753 // Helper method to create a new tab or sub window.
754 private void createWindow(final boolean dialog, final Message msg) {
755 WebView.WebViewTransport transport =
756 (WebView.WebViewTransport) msg.obj;
757 if (dialog) {
758 createSubWindow();
Michael Kolb8233fac2010-10-26 16:08:53 -0700759 mWebViewController.attachSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700760 transport.setWebView(mSubView);
761 } else {
Michael Kolb7bcafde2011-05-09 13:55:59 -0700762 final Tab newTab = mWebViewController.openTab(null,
John Reck5949c662011-05-27 09:52:29 -0700763 Tab.this, true, true);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700764 transport.setWebView(newTab.getWebView());
765 }
766 msg.sendToTarget();
767 }
768
769 @Override
770 public boolean onCreateWindow(WebView view, final boolean dialog,
771 final boolean userGesture, final Message resultMsg) {
772 // only allow new window or sub window for the foreground case
773 if (!mInForeground) {
774 return false;
775 }
776 // Short-circuit if we can't create any more tabs or sub windows.
777 if (dialog && mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -0700778 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700779 .setTitle(R.string.too_many_subwindows_dialog_title)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200780 .setIconAttribute(android.R.attr.alertDialogIcon)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700781 .setMessage(R.string.too_many_subwindows_dialog_message)
782 .setPositiveButton(R.string.ok, null)
783 .show();
784 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700785 } else if (!mWebViewController.getTabControl().canCreateNewTab()) {
Michael Kolb14612442011-06-24 13:06:29 -0700786 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700787 .setTitle(R.string.too_many_windows_dialog_title)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200788 .setIconAttribute(android.R.attr.alertDialogIcon)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700789 .setMessage(R.string.too_many_windows_dialog_message)
790 .setPositiveButton(R.string.ok, null)
791 .show();
792 return false;
793 }
794
795 // Short-circuit if this was a user gesture.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800796 if (userGesture || !mSettings.blockPopupWindows()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700797 createWindow(dialog, resultMsg);
798 return true;
799 }
800
801 // Allow the popup and create the appropriate window.
802 final AlertDialog.OnClickListener allowListener =
803 new AlertDialog.OnClickListener() {
804 public void onClick(DialogInterface d,
805 int which) {
806 createWindow(dialog, resultMsg);
807 }
808 };
809
810 // Block the popup by returning a null WebView.
811 final AlertDialog.OnClickListener blockListener =
812 new AlertDialog.OnClickListener() {
813 public void onClick(DialogInterface d, int which) {
814 resultMsg.sendToTarget();
815 }
816 };
817
818 // Build a confirmation dialog to display to the user.
819 final AlertDialog d =
Michael Kolb14612442011-06-24 13:06:29 -0700820 new AlertDialog.Builder(mContext)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200821 .setIconAttribute(android.R.attr.alertDialogIcon)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700822 .setMessage(R.string.popup_window_attempt)
823 .setPositiveButton(R.string.allow, allowListener)
824 .setNegativeButton(R.string.block, blockListener)
825 .setCancelable(false)
826 .create();
827
828 // Show the confirmation dialog.
829 d.show();
830 return true;
831 }
832
833 @Override
Patrick Scotteb5061b2009-11-18 15:00:30 -0500834 public void onRequestFocus(WebView view) {
835 if (!mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700836 mWebViewController.switchToTab(Tab.this);
Patrick Scotteb5061b2009-11-18 15:00:30 -0500837 }
838 }
839
840 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700841 public void onCloseWindow(WebView window) {
Michael Kolbc831b632011-05-11 09:30:34 -0700842 if (mParent != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700843 // JavaScript can only close popup window.
844 if (mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700845 mWebViewController.switchToTab(mParent);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700846 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700847 mWebViewController.closeTab(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700848 }
849 }
850
851 @Override
852 public void onProgressChanged(WebView view, int newProgress) {
John Reck30c714c2010-12-16 17:30:34 -0800853 mPageLoadProgress = newProgress;
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800854 if (newProgress == 100) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800855 Log.i(CONSOLE_LOGTAG, "SWE Pageload Progress = 100");
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800856 mInPageLoad = false;
857 }
John Reck30c714c2010-12-16 17:30:34 -0800858 mWebViewController.onProgressChanged(Tab.this);
Michael Kolb72864272012-05-03 15:42:15 -0700859 if (mUpdateThumbnail && newProgress == 100) {
860 mUpdateThumbnail = false;
861 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700862 }
863
864 @Override
Leon Scroggins21d9b902010-03-11 09:33:11 -0500865 public void onReceivedTitle(WebView view, final String title) {
John Reck30c714c2010-12-16 17:30:34 -0800866 mCurrentState.mTitle = title;
Michael Kolb8233fac2010-10-26 16:08:53 -0700867 mWebViewController.onReceivedTitle(Tab.this, title);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700868 }
869
870 @Override
871 public void onReceivedIcon(WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -0800872 mCurrentState.mFavicon = icon;
Michael Kolb8233fac2010-10-26 16:08:53 -0700873 mWebViewController.onFavicon(Tab.this, view, icon);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700874 }
875
876 @Override
877 public void onReceivedTouchIconUrl(WebView view, String url,
878 boolean precomposed) {
Michael Kolb14612442011-06-24 13:06:29 -0700879 final ContentResolver cr = mContext.getContentResolver();
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400880 // Let precomposed icons take precedence over non-composed
881 // icons.
882 if (precomposed && mTouchIconLoader != null) {
883 mTouchIconLoader.cancel(false);
884 mTouchIconLoader = null;
885 }
886 // Have only one async task at a time.
887 if (mTouchIconLoader == null) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700888 mTouchIconLoader = new DownloadTouchIcon(Tab.this,
Michael Kolb14612442011-06-24 13:06:29 -0700889 mContext, cr, view);
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400890 mTouchIconLoader.execute(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700891 }
892 }
893
894 @Override
895 public void onShowCustomView(View view,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800896 CustomViewCallback callback) {
Michael Kolb14612442011-06-24 13:06:29 -0700897 Activity activity = mWebViewController.getActivity();
898 if (activity != null) {
899 onShowCustomView(view, activity.getRequestedOrientation(), callback);
900 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400901 }
902
903 @Override
904 public void onShowCustomView(View view, int requestedOrientation,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800905 CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700906 if (mInForeground) mWebViewController.showCustomView(Tab.this, view,
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400907 requestedOrientation, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700908 }
909
910 @Override
911 public void onHideCustomView() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700912 if (mInForeground) mWebViewController.hideCustomView();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700913 }
914
915 /**
916 * The origin has exceeded its database quota.
917 * @param url the URL that exceeded the quota
918 * @param databaseIdentifier the identifier of the database on which the
919 * transaction that caused the quota overflow was run
920 * @param currentQuota the current quota for the origin.
921 * @param estimatedSize the estimated size of the database.
922 * @param totalUsedQuota is the sum of all origins' quota.
923 * @param quotaUpdater The callback to run when a decision to allow or
924 * deny quota has been made. Don't forget to call this!
925 */
926 @Override
927 public void onExceededDatabaseQuota(String url,
928 String databaseIdentifier, long currentQuota, long estimatedSize,
929 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -0700930 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -0700931 .onExceededDatabaseQuota(url, databaseIdentifier,
932 currentQuota, estimatedSize, totalUsedQuota,
933 quotaUpdater);
934 }
935
936 /**
937 * The Application Cache has exceeded its max size.
938 * @param spaceNeeded is the amount of disk space that would be needed
939 * in order for the last appcache operation to succeed.
940 * @param totalUsedQuota is the sum of all origins' quota.
941 * @param quotaUpdater A callback to inform the WebCore thread that a
942 * new app cache size is available. This callback must always
943 * be executed at some point to ensure that the sleeping
944 * WebCore thread is woken up.
945 */
946 @Override
947 public void onReachedMaxAppCacheSize(long spaceNeeded,
948 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -0700949 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -0700950 .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota,
951 quotaUpdater);
952 }
953
954 /**
955 * Instructs the browser to show a prompt to ask the user to set the
956 * Geolocation permission state for the specified origin.
957 * @param origin The origin for which Geolocation permissions are
958 * requested.
959 * @param callback The callback to call once the user has set the
960 * Geolocation permission state.
961 */
962 @Override
963 public void onGeolocationPermissionsShowPrompt(String origin,
964 GeolocationPermissions.Callback callback) {
965 if (mInForeground) {
Grace Kloba50c241e2010-04-20 11:07:50 -0700966 getGeolocationPermissionsPrompt().show(origin, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700967 }
968 }
969
970 /**
971 * Instructs the browser to hide the Geolocation permissions prompt.
972 */
973 @Override
974 public void onGeolocationPermissionsHidePrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -0700975 if (mInForeground && mGeolocationPermissionsPrompt != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700976 mGeolocationPermissionsPrompt.hide();
977 }
978 }
979
Ben Murdoch65acc352009-11-19 18:16:04 +0000980 /* Adds a JavaScript error message to the system log and if the JS
981 * console is enabled in the about:debug options, to that console
982 * also.
Ben Murdochc42addf2010-01-28 15:19:59 +0000983 * @param consoleMessage the message object.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700984 */
985 @Override
Ben Murdochc42addf2010-01-28 15:19:59 +0000986 public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700987 if (mInForeground) {
988 // call getErrorConsole(true) so it will create one if needed
989 ErrorConsoleView errorConsole = getErrorConsole(true);
Ben Murdochc42addf2010-01-28 15:19:59 +0000990 errorConsole.addErrorMessage(consoleMessage);
Michael Kolb8233fac2010-10-26 16:08:53 -0700991 if (mWebViewController.shouldShowErrorConsole()
992 && errorConsole.getShowState() !=
993 ErrorConsoleView.SHOW_MAXIMIZED) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700994 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
995 }
996 }
Ben Murdochc42addf2010-01-28 15:19:59 +0000997
Jeff Hamilton47654f42010-09-07 09:57:51 -0500998 // Don't log console messages in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -0700999 if (isPrivateBrowsingEnabled()) return true;
Jeff Hamilton47654f42010-09-07 09:57:51 -05001000
Ben Murdochc42addf2010-01-28 15:19:59 +00001001 String message = "Console: " + consoleMessage.message() + " "
1002 + consoleMessage.sourceId() + ":"
1003 + consoleMessage.lineNumber();
1004
1005 switch (consoleMessage.messageLevel()) {
1006 case TIP:
1007 Log.v(CONSOLE_LOGTAG, message);
1008 break;
1009 case LOG:
1010 Log.i(CONSOLE_LOGTAG, message);
1011 break;
1012 case WARNING:
1013 Log.w(CONSOLE_LOGTAG, message);
1014 break;
1015 case ERROR:
1016 Log.e(CONSOLE_LOGTAG, message);
1017 break;
1018 case DEBUG:
1019 Log.d(CONSOLE_LOGTAG, message);
1020 break;
1021 }
1022
1023 return true;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001024 }
1025
1026 /**
1027 * Ask the browser for an icon to represent a <video> element.
1028 * This icon will be used if the Web page did not specify a poster attribute.
1029 * @return Bitmap The icon or null if no such icon is available.
1030 */
1031 @Override
1032 public Bitmap getDefaultVideoPoster() {
1033 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001034 return mWebViewController.getDefaultVideoPoster();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001035 }
1036 return null;
1037 }
1038
1039 /**
1040 * Ask the host application for a custom progress view to show while
1041 * a <video> is loading.
1042 * @return View The progress view.
1043 */
1044 @Override
1045 public View getVideoLoadingProgressView() {
1046 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001047 return mWebViewController.getVideoLoadingProgressView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001048 }
1049 return null;
1050 }
1051
1052 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00001053 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001054 if (mInForeground) {
Ben Murdoch8cad4132012-01-11 10:56:43 +00001055 mWebViewController.openFileChooser(uploadMsg, acceptType, capture);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001056 } else {
1057 uploadMsg.onReceiveValue(null);
1058 }
1059 }
1060
1061 /**
1062 * Deliver a list of already-visited URLs
1063 */
1064 @Override
1065 public void getVisitedHistory(final ValueCallback<String[]> callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001066 mWebViewController.getVisitedHistory(callback);
1067 }
Ben Murdoch8029a772010-11-16 11:58:21 +00001068
1069 @Override
1070 public void setupAutoFill(Message message) {
1071 // Prompt the user to set up their profile.
1072 final Message msg = message;
Michael Kolb14612442011-06-24 13:06:29 -07001073 AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
1074 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
Ben Murdoch1d676b62011-01-17 12:54:24 +00001075 Context.LAYOUT_INFLATER_SERVICE);
1076 final View layout = inflater.inflate(R.layout.setup_autofill_dialog, null);
1077
1078 builder.setView(layout)
1079 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1080 @Override
1081 public void onClick(DialogInterface dialog, int id) {
1082 CheckBox disableAutoFill = (CheckBox) layout.findViewById(
1083 R.id.setup_autofill_dialog_disable_autofill);
1084
1085 if (disableAutoFill.isChecked()) {
1086 // Disable autofill and show a toast with how to turn it on again.
John Reck35e9dd62011-04-25 09:01:54 -07001087 mSettings.setAutofillEnabled(false);
Michael Kolb14612442011-06-24 13:06:29 -07001088 Toast.makeText(mContext,
Ben Murdoch1d676b62011-01-17 12:54:24 +00001089 R.string.autofill_setup_dialog_negative_toast,
1090 Toast.LENGTH_LONG).show();
1091 } else {
1092 // Take user to the AutoFill profile editor. When they return,
1093 // we will send the message that we pass here which will trigger
1094 // the form to get filled out with their new profile.
1095 mWebViewController.setupAutoFill(msg);
1096 }
1097 }
1098 })
1099 .setNegativeButton(R.string.cancel, null)
1100 .show();
Ben Murdoch8029a772010-11-16 11:58:21 +00001101 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001102 };
1103
1104 // -------------------------------------------------------------------------
1105 // WebViewClient implementation for the sub window
1106 // -------------------------------------------------------------------------
1107
1108 // Subclass of WebViewClient used in subwindows to notify the main
1109 // WebViewClient of certain WebView activities.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001110 private static class SubWindowClient extends WebViewClient {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001111 // The main WebViewClient.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001112 private final WebViewClient mClient;
Michael Kolb8233fac2010-10-26 16:08:53 -07001113 private final WebViewController mController;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001114
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001115 SubWindowClient(WebViewClient client, WebViewController controller) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001116 mClient = client;
Michael Kolb8233fac2010-10-26 16:08:53 -07001117 mController = controller;
Leon Scroggins III211ba542010-04-19 13:21:13 -04001118 }
1119 @Override
1120 public void onPageStarted(WebView view, String url, Bitmap favicon) {
1121 // Unlike the others, do not call mClient's version, which would
1122 // change the progress bar. However, we do want to remove the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001123 // find or select dialog.
Michael Kolb8233fac2010-10-26 16:08:53 -07001124 mController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001125 }
1126 @Override
1127 public void doUpdateVisitedHistory(WebView view, String url,
1128 boolean isReload) {
1129 mClient.doUpdateVisitedHistory(view, url, isReload);
1130 }
1131 @Override
1132 public boolean shouldOverrideUrlLoading(WebView view, String url) {
1133 return mClient.shouldOverrideUrlLoading(view, url);
1134 }
1135 @Override
1136 public void onReceivedSslError(WebView view, SslErrorHandler handler,
1137 SslError error) {
1138 mClient.onReceivedSslError(view, handler, error);
1139 }
1140 @Override
Brian Carlstrom8862c1d2011-06-02 01:05:55 -07001141 public void onReceivedClientCertRequest(WebView view,
1142 ClientCertRequestHandler handler, String host_and_port) {
1143 mClient.onReceivedClientCertRequest(view, handler, host_and_port);
1144 }
1145 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -07001146 public void onReceivedHttpAuthRequest(WebView view,
1147 HttpAuthHandler handler, String host, String realm) {
1148 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
1149 }
1150 @Override
1151 public void onFormResubmission(WebView view, Message dontResend,
1152 Message resend) {
1153 mClient.onFormResubmission(view, dontResend, resend);
1154 }
1155 @Override
1156 public void onReceivedError(WebView view, int errorCode,
1157 String description, String failingUrl) {
1158 mClient.onReceivedError(view, errorCode, description, failingUrl);
1159 }
1160 @Override
1161 public boolean shouldOverrideKeyEvent(WebView view,
1162 android.view.KeyEvent event) {
1163 return mClient.shouldOverrideKeyEvent(view, event);
1164 }
1165 @Override
1166 public void onUnhandledKeyEvent(WebView view,
1167 android.view.KeyEvent event) {
1168 mClient.onUnhandledKeyEvent(view, event);
1169 }
1170 }
1171
1172 // -------------------------------------------------------------------------
1173 // WebChromeClient implementation for the sub window
1174 // -------------------------------------------------------------------------
1175
1176 private class SubWindowChromeClient extends WebChromeClient {
1177 // The main WebChromeClient.
1178 private final WebChromeClient mClient;
1179
1180 SubWindowChromeClient(WebChromeClient client) {
1181 mClient = client;
1182 }
1183 @Override
1184 public void onProgressChanged(WebView view, int newProgress) {
1185 mClient.onProgressChanged(view, newProgress);
1186 }
1187 @Override
1188 public boolean onCreateWindow(WebView view, boolean dialog,
1189 boolean userGesture, android.os.Message resultMsg) {
1190 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
1191 }
1192 @Override
1193 public void onCloseWindow(WebView window) {
1194 if (window != mSubView) {
1195 Log.e(LOGTAG, "Can't close the window");
1196 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001197 mWebViewController.dismissSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001198 }
1199 }
1200
1201 // -------------------------------------------------------------------------
1202
1203 // Construct a new tab
Michael Kolb7bcafde2011-05-09 13:55:59 -07001204 Tab(WebViewController wvcontroller, WebView w) {
John Reck1cf4b792011-07-26 10:22:22 -07001205 this(wvcontroller, w, null);
1206 }
1207
1208 Tab(WebViewController wvcontroller, Bundle state) {
1209 this(wvcontroller, null, state);
1210 }
1211
1212 Tab(WebViewController wvcontroller, WebView w, Bundle state) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001213 mWebViewController = wvcontroller;
Michael Kolb14612442011-06-24 13:06:29 -07001214 mContext = mWebViewController.getContext();
John Reck35e9dd62011-04-25 09:01:54 -07001215 mSettings = BrowserSettings.getInstance();
Michael Kolb14612442011-06-24 13:06:29 -07001216 mDataController = DataController.getInstance(mContext);
1217 mCurrentState = new PageState(mContext, w != null
John Reck847b5322011-04-14 17:02:18 -07001218 ? w.isPrivateBrowsingEnabled() : false);
Michael Kolb8233fac2010-10-26 16:08:53 -07001219 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001220 mInForeground = false;
1221
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001222 mDownloadListener = new BrowserDownloadListener() {
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001223 public void onDownloadStart(String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001224 String contentDisposition, String mimetype, String referer,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001225 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001226 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001227 mimetype, referer, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001228 }
1229 };
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001230 mWebBackForwardListClient = new WebBackForwardListClient() {
1231 @Override
1232 public void onNewHistoryItem(WebHistoryItem item) {
Mathew Inwooda829d552011-09-02 14:16:25 +01001233 if (mClearHistoryUrlPattern != null) {
1234 boolean match =
1235 mClearHistoryUrlPattern.matcher(item.getOriginalUrl()).matches();
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001236 if (LOGD_ENABLED) {
Mathew Inwooda829d552011-09-02 14:16:25 +01001237 Log.d(LOGTAG, "onNewHistoryItem: match=" + match + "\n\t"
1238 + item.getUrl() + "\n\t"
1239 + mClearHistoryUrlPattern);
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001240 }
Mathew Inwooda829d552011-09-02 14:16:25 +01001241 if (match) {
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001242 if (mMainView != null) {
1243 mMainView.clearHistory();
1244 }
1245 }
Mathew Inwooda829d552011-09-02 14:16:25 +01001246 mClearHistoryUrlPattern = null;
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001247 }
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001248 }
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001249 };
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001250
John Reck1cf4b792011-07-26 10:22:22 -07001251 mCaptureWidth = mContext.getResources().getDimensionPixelSize(
1252 R.dimen.tab_thumbnail_width);
1253 mCaptureHeight = mContext.getResources().getDimensionPixelSize(
1254 R.dimen.tab_thumbnail_height);
1255 updateShouldCaptureThumbnails();
1256 restoreState(state);
John Reck52be4782011-08-26 15:37:29 -07001257 if (getId() == -1) {
1258 mId = TabControl.getNextId();
1259 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001260 setWebView(w);
Michael Kolb9ef259a2011-07-12 15:33:08 -07001261 mHandler = new Handler() {
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001262 @Override
Michael Kolb9ef259a2011-07-12 15:33:08 -07001263 public void handleMessage(Message m) {
John Reck1cf4b792011-07-26 10:22:22 -07001264 switch (m.what) {
1265 case MSG_CAPTURE:
1266 capture();
1267 break;
1268 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001269 }
1270 };
John Reck1cf4b792011-07-26 10:22:22 -07001271 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001272
Michael Kolb72864272012-05-03 15:42:15 -07001273 public boolean shouldUpdateThumbnail() {
1274 return mUpdateThumbnail;
1275 }
1276
Mathew Inwoode09305e2011-09-02 12:03:26 +01001277 /**
1278 * This is used to get a new ID when the tab has been preloaded, before it is displayed and
1279 * added to TabControl. Preloaded tabs can be created before restoreInstanceState, leading
1280 * to overlapping IDs between the preloaded and restored tabs.
1281 */
1282 public void refreshIdAfterPreload() {
1283 mId = TabControl.getNextId();
1284 }
1285
John Reck1cf4b792011-07-26 10:22:22 -07001286 public void updateShouldCaptureThumbnails() {
1287 if (mWebViewController.shouldCaptureThumbnails()) {
1288 synchronized (Tab.this) {
1289 if (mCapture == null) {
1290 mCapture = Bitmap.createBitmap(mCaptureWidth, mCaptureHeight,
1291 Bitmap.Config.RGB_565);
Michael Kolbc3af0672011-08-09 10:24:41 -07001292 mCapture.eraseColor(Color.WHITE);
John Reck1cf4b792011-07-26 10:22:22 -07001293 if (mInForeground) {
1294 postCapture();
1295 }
1296 }
1297 }
1298 } else {
1299 synchronized (Tab.this) {
1300 mCapture = null;
1301 deleteThumbnail();
1302 }
1303 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001304 }
1305
Michael Kolb14612442011-06-24 13:06:29 -07001306 public void setController(WebViewController ctl) {
1307 mWebViewController = ctl;
John Reck1cf4b792011-07-26 10:22:22 -07001308 updateShouldCaptureThumbnails();
Michael Kolb14612442011-06-24 13:06:29 -07001309 }
1310
Michael Kolbc831b632011-05-11 09:30:34 -07001311 public long getId() {
1312 return mId;
1313 }
1314
Michael Kolb91911a22012-01-17 11:21:25 -08001315 void setWebView(WebView w) {
1316 setWebView(w, true);
1317 }
1318
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001319 public boolean isNativeActive(){
1320 if (mMainView == null)
1321 return false;
1322 return true;
1323 }
1324
1325 public void setTimeStamp(){
1326 Date d = new Date();
1327 timestamp = (new Timestamp(d.getTime()));
1328 }
1329
1330 public Timestamp getTimestamp() {
1331 return timestamp;
1332 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001333 /**
1334 * Sets the WebView for this tab, correctly removing the old WebView from
1335 * the container view.
1336 */
Michael Kolb91911a22012-01-17 11:21:25 -08001337 void setWebView(WebView w, boolean restore) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001338 if (mMainView == w) {
1339 return;
1340 }
Michael Kolba713ec82010-11-29 17:27:06 -08001341
Grace Kloba22ac16e2009-10-07 18:00:23 -07001342 // If the WebView is changing, the page will be reloaded, so any ongoing
1343 // Geolocation permission requests are void.
Grace Kloba50c241e2010-04-20 11:07:50 -07001344 if (mGeolocationPermissionsPrompt != null) {
1345 mGeolocationPermissionsPrompt.hide();
1346 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001347
Michael Kolba713ec82010-11-29 17:27:06 -08001348 mWebViewController.onSetWebView(this, w);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001349
John Reck1cf4b792011-07-26 10:22:22 -07001350 if (mMainView != null) {
John Reck8ee633f2011-08-09 16:00:35 -07001351 mMainView.setPictureListener(null);
John Reck1cf4b792011-07-26 10:22:22 -07001352 if (w != null) {
1353 syncCurrentState(w, null);
1354 } else {
Panos Thomasa9a5a582014-03-18 19:20:08 -07001355 mCurrentState = new PageState(mContext, mMainView.isPrivateBrowsingEnabled());
John Reck1cf4b792011-07-26 10:22:22 -07001356 }
1357 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001358 // set the new one
1359 mMainView = w;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001360 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001361 if (mMainView != null) {
1362 mMainView.setWebViewClient(mWebViewClient);
1363 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001364 // Attach DownloadManager so that downloads can start in an active
1365 // or a non-active window. This can happen when going to a site that
1366 // does a redirect after a period of time. The user could have
1367 // switched to another tab while waiting for the download to start.
1368 mMainView.setDownloadListener(mDownloadListener);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001369 getWebView().setWebBackForwardListClient(mWebBackForwardListClient);
John Reck8ee633f2011-08-09 16:00:35 -07001370 TabControl tc = mWebViewController.getTabControl();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001371 if (tc != null /*&& tc.getOnThumbnailUpdatedListener() != null*/) {
John Reck8ee633f2011-08-09 16:00:35 -07001372 mMainView.setPictureListener(this);
1373 }
Michael Kolb91911a22012-01-17 11:21:25 -08001374 if (restore && (mSavedState != null)) {
John Reck8b9bb8b2012-03-08 13:19:40 -08001375 restoreUserAgent();
John Reck6c2e2f32011-08-22 13:41:23 -07001376 WebBackForwardList restoredState
1377 = mMainView.restoreState(mSavedState);
1378 if (restoredState == null || restoredState.getSize() == 0) {
1379 Log.w(LOGTAG, "Failed to restore WebView state!");
1380 loadUrl(mCurrentState.mOriginalUrl, null);
1381 }
John Reck1cf4b792011-07-26 10:22:22 -07001382 mSavedState = null;
1383 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001384 }
1385 }
1386
1387 /**
1388 * Destroy the tab's main WebView and subWindow if any
1389 */
1390 void destroy() {
1391 if (mMainView != null) {
1392 dismissSubWindow();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001393 // save the WebView to call destroy() after detach it from the tab
1394 WebView webView = mMainView;
1395 setWebView(null);
1396 webView.destroy();
1397 }
1398 }
1399
1400 /**
1401 * Remove the tab from the parent
1402 */
1403 void removeFromTree() {
1404 // detach the children
Michael Kolbc831b632011-05-11 09:30:34 -07001405 if (mChildren != null) {
1406 for(Tab t : mChildren) {
1407 t.setParent(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001408 }
1409 }
1410 // remove itself from the parent list
Michael Kolbc831b632011-05-11 09:30:34 -07001411 if (mParent != null) {
1412 mParent.mChildren.remove(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001413 }
John Reck1cf4b792011-07-26 10:22:22 -07001414 deleteThumbnail();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001415 }
1416
1417 /**
1418 * Create a new subwindow unless a subwindow already exists.
1419 * @return True if a new subwindow was created. False if one already exists.
1420 */
1421 boolean createSubWindow() {
1422 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001423 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001424 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001425 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001426 mSubView.setWebChromeClient(new SubWindowChromeClient(
1427 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001428 // Set a different DownloadListener for the mSubView, since it will
1429 // just need to dismiss the mSubView, rather than close the Tab
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001430 mSubView.setDownloadListener(new BrowserDownloadListener() {
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001431 public void onDownloadStart(String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001432 String contentDisposition, String mimetype, String referer,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001433 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001434 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001435 contentDisposition, mimetype, referer, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001436 if (mSubView.copyBackForwardList().getSize() == 0) {
1437 // This subwindow was opened for the sole purpose of
1438 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001439 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001440 }
1441 }
1442 });
Michael Kolb14612442011-06-24 13:06:29 -07001443 mSubView.setOnCreateContextMenuListener(mWebViewController.getActivity());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001444 return true;
1445 }
1446 return false;
1447 }
1448
1449 /**
1450 * Dismiss the subWindow for the tab.
1451 */
1452 void dismissSubWindow() {
1453 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001454 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001455 mSubView.destroy();
1456 mSubView = null;
1457 mSubViewContainer = null;
1458 }
1459 }
1460
Grace Kloba22ac16e2009-10-07 18:00:23 -07001461
1462 /**
1463 * Set the parent tab of this tab.
1464 */
Michael Kolbc831b632011-05-11 09:30:34 -07001465 void setParent(Tab parent) {
John Reck52be4782011-08-26 15:37:29 -07001466 if (parent == this) {
1467 throw new IllegalStateException("Cannot set parent to self!");
1468 }
Michael Kolbc831b632011-05-11 09:30:34 -07001469 mParent = parent;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001470 // This tab may have been freed due to low memory. If that is the case,
Michael Kolbc831b632011-05-11 09:30:34 -07001471 // the parent tab id is already saved. If we are changing that id
Grace Kloba22ac16e2009-10-07 18:00:23 -07001472 // (most likely due to removing the parent tab) we must update the
Michael Kolbc831b632011-05-11 09:30:34 -07001473 // parent tab id in the saved Bundle.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001474 if (mSavedState != null) {
1475 if (parent == null) {
1476 mSavedState.remove(PARENTTAB);
1477 } else {
Michael Kolbc831b632011-05-11 09:30:34 -07001478 mSavedState.putLong(PARENTTAB, parent.getId());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001479 }
1480 }
John Reckb0a86db2011-05-24 14:05:58 -07001481
1482 // Sync the WebView useragent with the parent
1483 if (parent != null && mSettings.hasDesktopUseragent(parent.getWebView())
1484 != mSettings.hasDesktopUseragent(getWebView())) {
1485 mSettings.toggleDesktopUseragent(getWebView());
1486 }
John Reck52be4782011-08-26 15:37:29 -07001487
1488 if (parent != null && parent.getId() == getId()) {
1489 throw new IllegalStateException("Parent has same ID as child!");
1490 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001491 }
1492
1493 /**
Michael Kolbc831b632011-05-11 09:30:34 -07001494 * If this Tab was created through another Tab, then this method returns
1495 * that Tab.
1496 * @return the Tab parent or null
1497 */
1498 public Tab getParent() {
1499 return mParent;
1500 }
1501
1502 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001503 * When a Tab is created through the content of another Tab, then we
1504 * associate the Tabs.
1505 * @param child the Tab that was created from this Tab
1506 */
1507 void addChildTab(Tab child) {
Michael Kolbc831b632011-05-11 09:30:34 -07001508 if (mChildren == null) {
1509 mChildren = new Vector<Tab>();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001510 }
Michael Kolbc831b632011-05-11 09:30:34 -07001511 mChildren.add(child);
1512 child.setParent(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001513 }
1514
Michael Kolbc831b632011-05-11 09:30:34 -07001515 Vector<Tab> getChildren() {
1516 return mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001517 }
1518
1519 void resume() {
1520 if (mMainView != null) {
John Reck56c1fcf2011-08-17 10:15:16 -07001521 setupHwAcceleration(mMainView);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001522 mMainView.onResume();
1523 if (mSubView != null) {
1524 mSubView.onResume();
1525 }
1526 }
1527 }
1528
John Reck56c1fcf2011-08-17 10:15:16 -07001529 private void setupHwAcceleration(View web) {
1530 if (web == null) return;
1531 BrowserSettings settings = BrowserSettings.getInstance();
1532 if (settings.isHardwareAccelerated()) {
1533 web.setLayerType(View.LAYER_TYPE_NONE, null);
1534 } else {
1535 web.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
1536 }
1537 }
1538
Grace Kloba22ac16e2009-10-07 18:00:23 -07001539 void pause() {
1540 if (mMainView != null) {
1541 mMainView.onPause();
1542 if (mSubView != null) {
1543 mSubView.onPause();
1544 }
1545 }
1546 }
1547
1548 void putInForeground() {
John Reck8ee633f2011-08-09 16:00:35 -07001549 if (mInForeground) {
1550 return;
1551 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001552 mInForeground = true;
1553 resume();
Michael Kolb14612442011-06-24 13:06:29 -07001554 Activity activity = mWebViewController.getActivity();
1555 mMainView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001556 if (mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -07001557 mSubView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001558 }
1559 // Show the pending error dialog if the queue is not empty
1560 if (mQueuedErrors != null && mQueuedErrors.size() > 0) {
1561 showError(mQueuedErrors.getFirst());
1562 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001563 mWebViewController.bookmarkedStatusHasChanged(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001564 }
1565
1566 void putInBackground() {
John Reck8ee633f2011-08-09 16:00:35 -07001567 if (!mInForeground) {
1568 return;
1569 }
John Reck52be4782011-08-26 15:37:29 -07001570 capture();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001571 mInForeground = false;
1572 pause();
1573 mMainView.setOnCreateContextMenuListener(null);
1574 if (mSubView != null) {
1575 mSubView.setOnCreateContextMenuListener(null);
1576 }
1577 }
1578
Michael Kolb8233fac2010-10-26 16:08:53 -07001579 boolean inForeground() {
1580 return mInForeground;
1581 }
1582
Grace Kloba22ac16e2009-10-07 18:00:23 -07001583 /**
1584 * Return the top window of this tab; either the subwindow if it is not
1585 * null or the main window.
1586 * @return The top window of this tab.
1587 */
1588 WebView getTopWindow() {
1589 if (mSubView != null) {
1590 return mSubView;
1591 }
1592 return mMainView;
1593 }
1594
1595 /**
1596 * Return the main window of this tab. Note: if a tab is freed in the
1597 * background, this can return null. It is only guaranteed to be
1598 * non-null for the current tab.
1599 * @return The main WebView of this tab.
1600 */
1601 WebView getWebView() {
1602 return mMainView;
1603 }
1604
Michael Kolba713ec82010-11-29 17:27:06 -08001605 void setViewContainer(View container) {
1606 mContainer = container;
1607 }
1608
Michael Kolb8233fac2010-10-26 16:08:53 -07001609 View getViewContainer() {
1610 return mContainer;
1611 }
1612
Grace Kloba22ac16e2009-10-07 18:00:23 -07001613 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001614 * Return whether private browsing is enabled for the main window of
1615 * this tab.
1616 * @return True if private browsing is enabled.
1617 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001618 boolean isPrivateBrowsingEnabled() {
John Reck502a3532011-08-16 14:21:46 -07001619 return mCurrentState.mIncognito;
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001620 }
1621
1622 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001623 * Return the subwindow of this tab or null if there is no subwindow.
1624 * @return The subwindow of this tab or null.
1625 */
1626 WebView getSubWebView() {
1627 return mSubView;
1628 }
1629
Michael Kolb1514bb72010-11-22 09:11:48 -08001630 void setSubWebView(WebView subView) {
1631 mSubView = subView;
1632 }
1633
Michael Kolb8233fac2010-10-26 16:08:53 -07001634 View getSubViewContainer() {
1635 return mSubViewContainer;
1636 }
1637
Michael Kolb1514bb72010-11-22 09:11:48 -08001638 void setSubViewContainer(View subViewContainer) {
1639 mSubViewContainer = subViewContainer;
1640 }
1641
Grace Kloba22ac16e2009-10-07 18:00:23 -07001642 /**
1643 * @return The geolocation permissions prompt for this tab.
1644 */
1645 GeolocationPermissionsPrompt getGeolocationPermissionsPrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001646 if (mGeolocationPermissionsPrompt == null) {
1647 ViewStub stub = (ViewStub) mContainer
1648 .findViewById(R.id.geolocation_permissions_prompt);
1649 mGeolocationPermissionsPrompt = (GeolocationPermissionsPrompt) stub
1650 .inflate();
Grace Kloba50c241e2010-04-20 11:07:50 -07001651 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001652 return mGeolocationPermissionsPrompt;
1653 }
1654
1655 /**
1656 * @return The application id string
1657 */
1658 String getAppId() {
1659 return mAppId;
1660 }
1661
1662 /**
1663 * Set the application id string
1664 * @param id
1665 */
1666 void setAppId(String id) {
1667 mAppId = id;
1668 }
1669
Michael Kolbe28b3472011-08-04 16:54:31 -07001670 boolean closeOnBack() {
1671 return mCloseOnBack;
1672 }
1673
1674 void setCloseOnBack(boolean close) {
1675 mCloseOnBack = close;
1676 }
1677
Grace Kloba22ac16e2009-10-07 18:00:23 -07001678 String getUrl() {
John Reck324d4402011-01-11 16:56:42 -08001679 return UrlUtils.filteredUrl(mCurrentState.mUrl);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001680 }
1681
John Reck49a603c2011-03-03 09:33:05 -08001682 String getOriginalUrl() {
John Reckdb22ec42011-06-29 11:31:24 -07001683 if (mCurrentState.mOriginalUrl == null) {
1684 return getUrl();
John Reck49a603c2011-03-03 09:33:05 -08001685 }
John Reckdb22ec42011-06-29 11:31:24 -07001686 return UrlUtils.filteredUrl(mCurrentState.mOriginalUrl);
John Reck49a603c2011-03-03 09:33:05 -08001687 }
1688
Grace Kloba22ac16e2009-10-07 18:00:23 -07001689 /**
John Reck30c714c2010-12-16 17:30:34 -08001690 * Get the title of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001691 */
1692 String getTitle() {
John Reck30c714c2010-12-16 17:30:34 -08001693 if (mCurrentState.mTitle == null && mInPageLoad) {
Michael Kolb14612442011-06-24 13:06:29 -07001694 return mContext.getString(R.string.title_bar_loading);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001695 }
John Reck30c714c2010-12-16 17:30:34 -08001696 return mCurrentState.mTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001697 }
1698
1699 /**
John Reck30c714c2010-12-16 17:30:34 -08001700 * Get the favicon of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001701 */
1702 Bitmap getFavicon() {
John Reck1cf4b792011-07-26 10:22:22 -07001703 if (mCurrentState.mFavicon != null) {
1704 return mCurrentState.mFavicon;
1705 }
1706 return getDefaultFavicon(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001707 }
1708
John Recke969cc52010-12-21 17:24:43 -08001709 public boolean isBookmarkedSite() {
1710 return mCurrentState.mIsBookmarkedSite;
1711 }
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001712
Grace Kloba22ac16e2009-10-07 18:00:23 -07001713 /**
1714 * Return the tab's error console. Creates the console if createIfNEcessary
1715 * is true and we haven't already created the console.
1716 * @param createIfNecessary Flag to indicate if the console should be
1717 * created if it has not been already.
1718 * @return The tab's error console, or null if one has not been created and
1719 * createIfNecessary is false.
1720 */
1721 ErrorConsoleView getErrorConsole(boolean createIfNecessary) {
1722 if (createIfNecessary && mErrorConsole == null) {
Michael Kolb14612442011-06-24 13:06:29 -07001723 mErrorConsole = new ErrorConsoleView(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001724 mErrorConsole.setWebView(mMainView);
1725 }
1726 return mErrorConsole;
1727 }
1728
Steve Block08a6f0c2011-10-06 12:12:53 +01001729 /**
1730 * Sets the security state, clears the SSL certificate error and informs
1731 * the controller.
1732 */
Steve Block2466eff2011-10-03 15:33:09 +01001733 private void setSecurityState(SecurityState securityState) {
1734 mCurrentState.mSecurityState = securityState;
Steve Block08a6f0c2011-10-06 12:12:53 +01001735 mCurrentState.mSslCertificateError = null;
Steve Block2466eff2011-10-03 15:33:09 +01001736 mWebViewController.onUpdatedSecurityState(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001737 }
1738
1739 /**
Steve Block2466eff2011-10-03 15:33:09 +01001740 * @return The tab's security state.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001741 */
Steve Block2466eff2011-10-03 15:33:09 +01001742 SecurityState getSecurityState() {
1743 return mCurrentState.mSecurityState;
John Reck30c714c2010-12-16 17:30:34 -08001744 }
1745
Steve Block08a6f0c2011-10-06 12:12:53 +01001746 /**
1747 * Gets the SSL certificate error, if any, for the page's main resource.
1748 * This is only non-null when the security state is
1749 * SECURITY_STATE_BAD_CERTIFICATE.
1750 */
1751 SslError getSslCertificateError() {
1752 return mCurrentState.mSslCertificateError;
1753 }
1754
John Reck30c714c2010-12-16 17:30:34 -08001755 int getLoadProgress() {
1756 if (mInPageLoad) {
1757 return mPageLoadProgress;
1758 }
1759 return 100;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001760 }
1761
1762 /**
1763 * @return TRUE if onPageStarted is called while onPageFinished is not
1764 * called yet.
1765 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001766 boolean inPageLoad() {
1767 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001768 }
1769
Grace Kloba22ac16e2009-10-07 18:00:23 -07001770 /**
John Reck1cf4b792011-07-26 10:22:22 -07001771 * @return The Bundle with the tab's state if it can be saved, otherwise null
Grace Kloba22ac16e2009-10-07 18:00:23 -07001772 */
John Reck1cf4b792011-07-26 10:22:22 -07001773 public Bundle saveState() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001774 // If the WebView is null it means we ran low on memory and we already
1775 // stored the saved state in mSavedState.
1776 if (mMainView == null) {
John Reck1cf4b792011-07-26 10:22:22 -07001777 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001778 }
John Reck6c2e2f32011-08-22 13:41:23 -07001779
1780 if (TextUtils.isEmpty(mCurrentState.mUrl)) {
John Reck1cf4b792011-07-26 10:22:22 -07001781 return null;
John Reck24f18262011-06-17 14:47:20 -07001782 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001783
1784 mSavedState = new Bundle();
John Reck6c2e2f32011-08-22 13:41:23 -07001785 WebBackForwardList savedList = mMainView.saveState(mSavedState);
1786 if (savedList == null || savedList.getSize() == 0) {
1787 Log.w(LOGTAG, "Failed to save back/forward list for "
1788 + mCurrentState.mUrl);
1789 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001790
Michael Kolbc831b632011-05-11 09:30:34 -07001791 mSavedState.putLong(ID, mId);
John Reck30c714c2010-12-16 17:30:34 -08001792 mSavedState.putString(CURRURL, mCurrentState.mUrl);
1793 mSavedState.putString(CURRTITLE, mCurrentState.mTitle);
John Reck1cf4b792011-07-26 10:22:22 -07001794 mSavedState.putBoolean(INCOGNITO, mMainView.isPrivateBrowsingEnabled());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001795 if (mAppId != null) {
1796 mSavedState.putString(APPID, mAppId);
1797 }
Michael Kolbe28b3472011-08-04 16:54:31 -07001798 mSavedState.putBoolean(CLOSEFLAG, mCloseOnBack);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001799 // Remember the parent tab so the relationship can be restored.
Michael Kolbc831b632011-05-11 09:30:34 -07001800 if (mParent != null) {
1801 mSavedState.putLong(PARENTTAB, mParent.mId);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001802 }
John Reckb0a86db2011-05-24 14:05:58 -07001803 mSavedState.putBoolean(USERAGENT,
1804 mSettings.hasDesktopUseragent(getWebView()));
John Reck1cf4b792011-07-26 10:22:22 -07001805 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001806 }
1807
1808 /*
1809 * Restore the state of the tab.
1810 */
John Reck1cf4b792011-07-26 10:22:22 -07001811 private void restoreState(Bundle b) {
1812 mSavedState = b;
1813 if (mSavedState == null) {
1814 return;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001815 }
1816 // Restore the internal state even if the WebView fails to restore.
1817 // This will maintain the app id, original url and close-on-exit values.
Michael Kolbc831b632011-05-11 09:30:34 -07001818 mId = b.getLong(ID);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001819 mAppId = b.getString(APPID);
Michael Kolbe28b3472011-08-04 16:54:31 -07001820 mCloseOnBack = b.getBoolean(CLOSEFLAG);
John Reck8b9bb8b2012-03-08 13:19:40 -08001821 restoreUserAgent();
John Reck1cf4b792011-07-26 10:22:22 -07001822 String url = b.getString(CURRURL);
1823 String title = b.getString(CURRTITLE);
1824 boolean incognito = b.getBoolean(INCOGNITO);
1825 mCurrentState = new PageState(mContext, incognito, url, null);
1826 mCurrentState.mTitle = title;
1827 synchronized (Tab.this) {
1828 if (mCapture != null) {
John Reck4eadc342011-10-31 14:04:10 -07001829 DataController.getInstance(mContext).loadThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07001830 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001831 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001832 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04001833
John Reck8b9bb8b2012-03-08 13:19:40 -08001834 private void restoreUserAgent() {
1835 if (mMainView == null || mSavedState == null) {
1836 return;
1837 }
1838 if (mSavedState.getBoolean(USERAGENT)
1839 != mSettings.hasDesktopUseragent(mMainView)) {
1840 mSettings.toggleDesktopUseragent(mMainView);
1841 }
1842 }
1843
Leon Scroggins1961ed22010-12-07 15:22:21 -05001844 public void updateBookmarkedStatus() {
John Recke969cc52010-12-21 17:24:43 -08001845 mDataController.queryBookmarkStatus(getUrl(), mIsBookmarkCallback);
Leon Scroggins1961ed22010-12-07 15:22:21 -05001846 }
1847
John Recke969cc52010-12-21 17:24:43 -08001848 private DataController.OnQueryUrlIsBookmark mIsBookmarkCallback
1849 = new DataController.OnQueryUrlIsBookmark() {
1850 @Override
1851 public void onQueryUrlIsBookmark(String url, boolean isBookmark) {
1852 if (mCurrentState.mUrl.equals(url)) {
1853 mCurrentState.mIsBookmarkedSite = isBookmark;
1854 mWebViewController.bookmarkedStatusHasChanged(Tab.this);
1855 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001856 }
John Recke969cc52010-12-21 17:24:43 -08001857 };
Michael Kolb1acef692011-03-08 14:12:06 -08001858
Michael Kolbeb95db42011-03-03 10:38:40 -08001859 public Bitmap getScreenshot() {
John Reck1cf4b792011-07-26 10:22:22 -07001860 synchronized (Tab.this) {
1861 return mCapture;
1862 }
Michael Kolbeb95db42011-03-03 10:38:40 -08001863 }
1864
John Reck541f55a2011-06-07 16:34:43 -07001865 public boolean isSnapshot() {
John Reck541f55a2011-06-07 16:34:43 -07001866 return false;
1867 }
1868
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001869 private static class SaveCallback implements ValueCallback<String> {
1870 boolean onReceiveValueCalled = false;
1871 private String mPath;
John Reck68234a92012-04-19 15:27:12 -07001872
1873 @Override
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001874 public void onReceiveValue(String path) {
1875 this.onReceiveValueCalled = true;
1876 this.mPath = path;
John Reck68234a92012-04-19 15:27:12 -07001877 synchronized (this) {
1878 notifyAll();
John Reck8cc92352011-07-06 17:41:52 -07001879 }
John Reck541f55a2011-06-07 16:34:43 -07001880 }
John Reck68234a92012-04-19 15:27:12 -07001881
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001882 public String getPath() {
1883 return mPath;
1884 }
John Reck68234a92012-04-19 15:27:12 -07001885 }
1886
1887 /**
1888 * Must be called on the UI thread
1889 */
1890 public ContentValues createSnapshotValues() {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001891 WebView web = getWebView();
John Reck68234a92012-04-19 15:27:12 -07001892 if (web == null) return null;
John Reckd8c74522011-06-14 08:45:00 -07001893 ContentValues values = new ContentValues();
1894 values.put(Snapshots.TITLE, mCurrentState.mTitle);
1895 values.put(Snapshots.URL, mCurrentState.mUrl);
John Reck68234a92012-04-19 15:27:12 -07001896 values.put(Snapshots.BACKGROUND, web.getPageBackgroundColor());
John Reck8cc92352011-07-06 17:41:52 -07001897 values.put(Snapshots.DATE_CREATED, System.currentTimeMillis());
1898 values.put(Snapshots.FAVICON, compressBitmap(getFavicon()));
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001899 Bitmap screenshot = web.getViewportBitmap();
John Reck8cc92352011-07-06 17:41:52 -07001900 values.put(Snapshots.THUMBNAIL, compressBitmap(screenshot));
John Reckd8c74522011-06-14 08:45:00 -07001901 return values;
John Reck541f55a2011-06-07 16:34:43 -07001902 }
1903
John Reck68234a92012-04-19 15:27:12 -07001904 /**
1905 * Probably want to call this on a background thread
1906 */
1907 public boolean saveViewState(ContentValues values) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001908 WebView web = getWebView();
John Reck68234a92012-04-19 15:27:12 -07001909 if (web == null) return false;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001910 String filename = UUID.randomUUID().toString();
John Reck68234a92012-04-19 15:27:12 -07001911 SaveCallback callback = new SaveCallback();
John Reck68234a92012-04-19 15:27:12 -07001912 try {
John Reck68234a92012-04-19 15:27:12 -07001913 synchronized (callback) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001914 web.saveViewState(filename, callback);
1915 callback.wait();
John Reck68234a92012-04-19 15:27:12 -07001916 }
John Reck68234a92012-04-19 15:27:12 -07001917 } catch (Exception e) {
1918 Log.w(LOGTAG, "Failed to save view state", e);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001919 String path = callback.getPath();
1920 if (path != null) {
1921 File file = mContext.getFileStreamPath(path);
1922 if (file.exists() && !file.delete()) {
1923 file.deleteOnExit();
1924 }
John Reck68234a92012-04-19 15:27:12 -07001925 }
1926 return false;
1927 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001928 String path = callback.getPath();
1929 File savedFile = new File(path);
1930 if (!savedFile.exists()) {
1931 return false;
John Reck68234a92012-04-19 15:27:12 -07001932 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001933 values.put(Snapshots.VIEWSTATE_PATH, path.substring(path.lastIndexOf('/') + 1));
1934 values.put(Snapshots.VIEWSTATE_SIZE, savedFile.length());
John Reck68234a92012-04-19 15:27:12 -07001935 return true;
1936 }
1937
John Reck8cc92352011-07-06 17:41:52 -07001938 public byte[] compressBitmap(Bitmap bitmap) {
1939 if (bitmap == null) {
1940 return null;
1941 }
1942 ByteArrayOutputStream stream = new ByteArrayOutputStream();
1943 bitmap.compress(CompressFormat.PNG, 100, stream);
1944 return stream.toByteArray();
1945 }
1946
John Reck26b18322011-06-21 13:08:58 -07001947 public void loadUrl(String url, Map<String, String> headers) {
1948 if (mMainView != null) {
Michael Kolba53c9892011-10-05 13:31:40 -07001949 mPageLoadProgress = INITIAL_PROGRESS;
1950 mInPageLoad = true;
Michael Kolb14612442011-06-24 13:06:29 -07001951 mCurrentState = new PageState(mContext, false, url, null);
John Reck26b18322011-06-21 13:08:58 -07001952 mWebViewController.onPageStarted(this, mMainView, null);
1953 mMainView.loadUrl(url, headers);
1954 }
1955 }
1956
John Reck38b39652012-06-05 09:22:59 -07001957 public void disableUrlOverridingForLoad() {
1958 mDisableOverrideUrlLoading = true;
1959 }
1960
Michael Kolb9ef259a2011-07-12 15:33:08 -07001961 protected void capture() {
1962 if (mMainView == null || mCapture == null) return;
John Reck4eadc342011-10-31 14:04:10 -07001963 if (mMainView.getContentWidth() <= 0 || mMainView.getContentHeight() <= 0) {
1964 return;
1965 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001966 Canvas c = new Canvas(mCapture);
Michael Kolba3194d02011-09-07 11:23:51 -07001967 int state = c.save();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001968 Bitmap screenShot = mMainView.getViewportBitmap();
1969 if (screenShot != null) {
1970 mCapture.eraseColor(Color.WHITE);
1971 float scale = (float) mCaptureWidth / screenShot.getWidth();
1972 c.scale(scale, scale);
1973 c.drawBitmap(screenShot, 0, 0, null);
1974 } else {
1975 final int left = mMainView.getViewScrollX();
1976 final int top = mMainView.getViewScrollY() + mMainView.getVisibleTitleHeight();
1977 c.translate(-left, -top);
1978 float scale = mCaptureWidth / (float) mMainView.getWidth();
1979 c.scale(scale, scale, left, top);
1980 if (mMainView instanceof BrowserWebView) {
1981 ((BrowserWebView)mMainView).drawContent(c);
1982 } else {
1983 mMainView.draw(c);
1984 }
John Reck8ee633f2011-08-09 16:00:35 -07001985 }
Michael Kolba3194d02011-09-07 11:23:51 -07001986 c.restoreToCount(state);
1987 // manually anti-alias the edges for the tilt
1988 c.drawRect(0, 0, 1, mCapture.getHeight(), sAlphaPaint);
1989 c.drawRect(mCapture.getWidth() - 1, 0, mCapture.getWidth(),
1990 mCapture.getHeight(), sAlphaPaint);
1991 c.drawRect(0, 0, mCapture.getWidth(), 1, sAlphaPaint);
1992 c.drawRect(0, mCapture.getHeight() - 1, mCapture.getWidth(),
1993 mCapture.getHeight(), sAlphaPaint);
Dianne Hackborn43cfe8a2011-08-02 16:59:35 -07001994 c.setBitmap(null);
John Reck8ee633f2011-08-09 16:00:35 -07001995 mHandler.removeMessages(MSG_CAPTURE);
John Reck1cf4b792011-07-26 10:22:22 -07001996 persistThumbnail();
John Reck8ee633f2011-08-09 16:00:35 -07001997 TabControl tc = mWebViewController.getTabControl();
1998 if (tc != null) {
1999 OnThumbnailUpdatedListener updateListener
2000 = tc.getOnThumbnailUpdatedListener();
2001 if (updateListener != null) {
2002 updateListener.onThumbnailUpdated(this);
2003 }
2004 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07002005 }
2006
2007 @Override
2008 public void onNewPicture(WebView view, Picture picture) {
John Reck1cf4b792011-07-26 10:22:22 -07002009 postCapture();
2010 }
2011
2012 private void postCapture() {
Michael Kolb9ef259a2011-07-12 15:33:08 -07002013 if (!mHandler.hasMessages(MSG_CAPTURE)) {
2014 mHandler.sendEmptyMessageDelayed(MSG_CAPTURE, CAPTURE_DELAY);
2015 }
2016 }
2017
John Reckef654f12011-07-12 16:42:08 -07002018 public boolean canGoBack() {
2019 return mMainView != null ? mMainView.canGoBack() : false;
2020 }
2021
2022 public boolean canGoForward() {
2023 return mMainView != null ? mMainView.canGoForward() : false;
2024 }
2025
2026 public void goBack() {
2027 if (mMainView != null) {
2028 mMainView.goBack();
2029 }
2030 }
2031
2032 public void goForward() {
2033 if (mMainView != null) {
2034 mMainView.goForward();
2035 }
2036 }
2037
Mathew Inwood1dd8e822011-08-03 14:34:29 +01002038 /**
2039 * Causes the tab back/forward stack to be cleared once, if the given URL is the next URL
2040 * to be added to the stack.
2041 *
2042 * This is used to ensure that preloaded URLs that are not subsequently seen by the user do
2043 * not appear in the back stack.
2044 */
Mathew Inwooda829d552011-09-02 14:16:25 +01002045 public void clearBackStackWhenItemAdded(Pattern urlPattern) {
2046 mClearHistoryUrlPattern = urlPattern;
Mathew Inwood1dd8e822011-08-03 14:34:29 +01002047 }
2048
John Reck1cf4b792011-07-26 10:22:22 -07002049 protected void persistThumbnail() {
John Reck4eadc342011-10-31 14:04:10 -07002050 DataController.getInstance(mContext).saveThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07002051 }
2052
2053 protected void deleteThumbnail() {
John Reck4eadc342011-10-31 14:04:10 -07002054 DataController.getInstance(mContext).deleteThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07002055 }
2056
John Reck4eadc342011-10-31 14:04:10 -07002057 void updateCaptureFromBlob(byte[] blob) {
John Reck1cf4b792011-07-26 10:22:22 -07002058 synchronized (Tab.this) {
2059 if (mCapture == null) {
2060 return;
2061 }
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002062 ByteBuffer buffer = ByteBuffer.wrap(blob);
Michael Kolbd837a112011-08-09 14:00:18 -07002063 try {
2064 mCapture.copyPixelsFromBuffer(buffer);
2065 } catch (RuntimeException rex) {
2066 Log.e(LOGTAG, "Load capture has mismatched sizes; buffer: "
2067 + buffer.capacity() + " blob: " + blob.length
2068 + "capture: " + mCapture.getByteCount());
2069 throw rex;
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002070 }
John Reck1cf4b792011-07-26 10:22:22 -07002071 }
2072 }
2073
John Reck52be4782011-08-26 15:37:29 -07002074 @Override
2075 public String toString() {
2076 StringBuilder builder = new StringBuilder(100);
2077 builder.append(mId);
2078 builder.append(") has parent: ");
2079 if (getParent() != null) {
2080 builder.append("true[");
2081 builder.append(getParent().getId());
2082 builder.append("]");
2083 } else {
2084 builder.append("false");
2085 }
2086 builder.append(", incog: ");
2087 builder.append(isPrivateBrowsingEnabled());
2088 if (!isPrivateBrowsingEnabled()) {
2089 builder.append(", title: ");
2090 builder.append(getTitle());
2091 builder.append(", url: ");
2092 builder.append(getUrl());
2093 }
2094 return builder.toString();
2095 }
2096
Steve Block4895b012011-10-03 16:26:46 +01002097 private void handleProceededAfterSslError(SslError error) {
2098 if (error.getUrl().equals(mCurrentState.mUrl)) {
2099 // The security state should currently be SECURITY_STATE_SECURE.
2100 setSecurityState(SecurityState.SECURITY_STATE_BAD_CERTIFICATE);
Steve Block08a6f0c2011-10-06 12:12:53 +01002101 mCurrentState.mSslCertificateError = error;
Steve Block4895b012011-10-03 16:26:46 +01002102 } else if (getSecurityState() == SecurityState.SECURITY_STATE_SECURE) {
Steve Block08a6f0c2011-10-06 12:12:53 +01002103 // The page's main resource is secure and this error is for a
2104 // sub-resource.
Steve Block4895b012011-10-03 16:26:46 +01002105 setSecurityState(SecurityState.SECURITY_STATE_MIXED);
2106 }
2107 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002108}