blob: 26f9152b54f012d0f300235fee0e461310b3f128 [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;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700188
189 // AsyncTask for downloading touch icons
190 DownloadTouchIcon mTouchIconLoader;
191
John Reck35e9dd62011-04-25 09:01:54 -0700192 private BrowserSettings mSettings;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700193 private int mCaptureWidth;
194 private int mCaptureHeight;
195 private Bitmap mCapture;
196 private Handler mHandler;
Michael Kolb72864272012-05-03 15:42:15 -0700197 private boolean mUpdateThumbnail;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800198 private Timestamp timestamp;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700199
Mathew Inwood1dd8e822011-08-03 14:34:29 +0100200 /**
Mathew Inwood9ad1eac2011-09-15 11:29:50 +0100201 * See {@link #clearBackStackWhenItemAdded(String)}.
Mathew Inwood1dd8e822011-08-03 14:34:29 +0100202 */
Mathew Inwooda829d552011-09-02 14:16:25 +0100203 private Pattern mClearHistoryUrlPattern;
Mathew Inwood1dd8e822011-08-03 14:34:29 +0100204
John Reck1cf4b792011-07-26 10:22:22 -0700205 private static synchronized Bitmap getDefaultFavicon(Context context) {
206 if (sDefaultFavicon == null) {
207 sDefaultFavicon = BitmapFactory.decodeResource(
208 context.getResources(), R.drawable.app_web_browser_sm);
209 }
210 return sDefaultFavicon;
211 }
Michael Kolbeb95db42011-03-03 10:38:40 -0800212
John Reck30c714c2010-12-16 17:30:34 -0800213 // All the state needed for a page
John Reckd8c74522011-06-14 08:45:00 -0700214 protected static class PageState {
John Reck30c714c2010-12-16 17:30:34 -0800215 String mUrl;
John Reckdb22ec42011-06-29 11:31:24 -0700216 String mOriginalUrl;
John Reck30c714c2010-12-16 17:30:34 -0800217 String mTitle;
Steve Block2466eff2011-10-03 15:33:09 +0100218 SecurityState mSecurityState;
Steve Block08a6f0c2011-10-06 12:12:53 +0100219 // This is non-null only when mSecurityState is SECURITY_STATE_BAD_CERTIFICATE.
220 SslError mSslCertificateError;
John Reck30c714c2010-12-16 17:30:34 -0800221 Bitmap mFavicon;
Steve Block08a6f0c2011-10-06 12:12:53 +0100222 boolean mIsBookmarkedSite;
223 boolean mIncognito;
John Reck30c714c2010-12-16 17:30:34 -0800224
225 PageState(Context c, boolean incognito) {
John Reck502a3532011-08-16 14:21:46 -0700226 mIncognito = incognito;
227 if (mIncognito) {
John Reckdb22ec42011-06-29 11:31:24 -0700228 mOriginalUrl = mUrl = "browser:incognito";
John Reck30c714c2010-12-16 17:30:34 -0800229 mTitle = c.getString(R.string.new_incognito_tab);
John Reck30c714c2010-12-16 17:30:34 -0800230 } else {
John Reckdb22ec42011-06-29 11:31:24 -0700231 mOriginalUrl = mUrl = "";
John Reck30c714c2010-12-16 17:30:34 -0800232 mTitle = c.getString(R.string.new_tab);
John Reck30c714c2010-12-16 17:30:34 -0800233 }
Steve Block2466eff2011-10-03 15:33:09 +0100234 mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
John Reck30c714c2010-12-16 17:30:34 -0800235 }
236
237 PageState(Context c, boolean incognito, String url, Bitmap favicon) {
John Reck502a3532011-08-16 14:21:46 -0700238 mIncognito = incognito;
John Reckdb22ec42011-06-29 11:31:24 -0700239 mOriginalUrl = mUrl = url;
John Reck30c714c2010-12-16 17:30:34 -0800240 if (URLUtil.isHttpsUrl(url)) {
Steve Block2466eff2011-10-03 15:33:09 +0100241 mSecurityState = SecurityState.SECURITY_STATE_SECURE;
John Reck30c714c2010-12-16 17:30:34 -0800242 } else {
Steve Block2466eff2011-10-03 15:33:09 +0100243 mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
John Reck30c714c2010-12-16 17:30:34 -0800244 }
John Reck1cf4b792011-07-26 10:22:22 -0700245 mFavicon = favicon;
John Reck30c714c2010-12-16 17:30:34 -0800246 }
John Reck1cf4b792011-07-26 10:22:22 -0700247
Grace Kloba22ac16e2009-10-07 18:00:23 -0700248 }
249
John Reck30c714c2010-12-16 17:30:34 -0800250 // The current/loading page's state
John Reckd8c74522011-06-14 08:45:00 -0700251 protected PageState mCurrentState;
John Reck30c714c2010-12-16 17:30:34 -0800252
Grace Kloba22ac16e2009-10-07 18:00:23 -0700253 // Used for saving and restoring each Tab
Michael Kolbc831b632011-05-11 09:30:34 -0700254 static final String ID = "ID";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700255 static final String CURRURL = "currentUrl";
256 static final String CURRTITLE = "currentTitle";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700257 static final String PARENTTAB = "parentTab";
258 static final String APPID = "appid";
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700259 static final String INCOGNITO = "privateBrowsingEnabled";
John Reckb0a86db2011-05-24 14:05:58 -0700260 static final String USERAGENT = "useragent";
Michael Kolbe28b3472011-08-04 16:54:31 -0700261 static final String CLOSEFLAG = "closeOnBack";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700262
Grace Kloba22ac16e2009-10-07 18:00:23 -0700263 // Container class for the next error dialog that needs to be displayed
264 private class ErrorDialog {
265 public final int mTitle;
266 public final String mDescription;
267 public final int mError;
268 ErrorDialog(int title, String desc, int error) {
269 mTitle = title;
270 mDescription = desc;
271 mError = error;
272 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700273 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700274
275 private void processNextError() {
276 if (mQueuedErrors == null) {
277 return;
278 }
279 // The first one is currently displayed so just remove it.
280 mQueuedErrors.removeFirst();
281 if (mQueuedErrors.size() == 0) {
282 mQueuedErrors = null;
283 return;
284 }
285 showError(mQueuedErrors.getFirst());
286 }
287
288 private DialogInterface.OnDismissListener mDialogListener =
289 new DialogInterface.OnDismissListener() {
290 public void onDismiss(DialogInterface d) {
291 processNextError();
292 }
293 };
294 private LinkedList<ErrorDialog> mQueuedErrors;
295
296 private void queueError(int err, String desc) {
297 if (mQueuedErrors == null) {
298 mQueuedErrors = new LinkedList<ErrorDialog>();
299 }
300 for (ErrorDialog d : mQueuedErrors) {
301 if (d.mError == err) {
302 // Already saw a similar error, ignore the new one.
303 return;
304 }
305 }
306 ErrorDialog errDialog = new ErrorDialog(
307 err == WebViewClient.ERROR_FILE_NOT_FOUND ?
308 R.string.browserFrameFileErrorLabel :
309 R.string.browserFrameNetworkErrorLabel,
310 desc, err);
311 mQueuedErrors.addLast(errDialog);
312
313 // Show the dialog now if the queue was empty and it is in foreground
314 if (mQueuedErrors.size() == 1 && mInForeground) {
315 showError(errDialog);
316 }
317 }
318
319 private void showError(ErrorDialog errDialog) {
320 if (mInForeground) {
Michael Kolb14612442011-06-24 13:06:29 -0700321 AlertDialog d = new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700322 .setTitle(errDialog.mTitle)
323 .setMessage(errDialog.mDescription)
324 .setPositiveButton(R.string.ok, null)
325 .create();
326 d.setOnDismissListener(mDialogListener);
327 d.show();
328 }
329 }
330
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800331 protected void replaceCrashView(View view, View container) {
332 if (hasCrashed && (view == mMainView)) {
333 final FrameLayout wrapper = (FrameLayout) container.findViewById(R.id.webview_wrapper);
334 wrapper.removeAllViewsInLayout();
335 wrapper.addView(view);
336 hasCrashed = false;
337 }
338 }
339
340 protected void showCrashView() {
341 if (hasCrashed) {
342 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
343 Context.LAYOUT_INFLATER_SERVICE);
344 final View crashLayout = inflater.inflate(R.layout.browser_tab_crash, null);
345 final FrameLayout wrapper =
346 (FrameLayout) mContainer.findViewById(R.id.webview_wrapper);
347 wrapper.removeAllViewsInLayout();
348 wrapper.addView(crashLayout);
349 mContainer.requestFocus();
350 Button reloadBtn = (Button) crashLayout.findViewById(R.id.browser_crash_reload_btn);
351 reloadBtn.setOnClickListener(new OnClickListener() {
352 @Override
353 public void onClick(View arg0) {
354 replaceCrashView(mMainView, mContainer);
355 mMainView.reload();
356 }
357 });
358 }
359 }
360
Grace Kloba22ac16e2009-10-07 18:00:23 -0700361 // -------------------------------------------------------------------------
362 // WebViewClient implementation for the main WebView
363 // -------------------------------------------------------------------------
364
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800365 private final WebViewClient mWebViewClient = new WebViewClient() {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500366 private Message mDontResend;
367 private Message mResend;
Michael Kolb47bd1e42011-09-01 15:25:00 -0700368
369 private boolean providersDiffer(String url, String otherUrl) {
370 Uri uri1 = Uri.parse(url);
371 Uri uri2 = Uri.parse(otherUrl);
372 return !uri1.getEncodedAuthority().equals(uri2.getEncodedAuthority());
373 }
374
Grace Kloba22ac16e2009-10-07 18:00:23 -0700375 @Override
376 public void onPageStarted(WebView view, String url, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700377 mInPageLoad = true;
Michael Kolb72864272012-05-03 15:42:15 -0700378 mUpdateThumbnail = true;
Michael Kolba53c9892011-10-05 13:31:40 -0700379 mPageLoadProgress = INITIAL_PROGRESS;
Michael Kolb14612442011-06-24 13:06:29 -0700380 mCurrentState = new PageState(mContext,
John Reck30c714c2010-12-16 17:30:34 -0800381 view.isPrivateBrowsingEnabled(), url, favicon);
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000382 mLoadStartTime = SystemClock.uptimeMillis();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700383
384 // If we start a touch icon load and then load a new page, we don't
385 // want to cancel the current touch icon loader. But, we do want to
386 // create a new one when the touch icon url is known.
387 if (mTouchIconLoader != null) {
388 mTouchIconLoader.mTab = null;
389 mTouchIconLoader = null;
390 }
391
392 // reset the error console
393 if (mErrorConsole != null) {
394 mErrorConsole.clearErrorMessages();
Michael Kolb8233fac2010-10-26 16:08:53 -0700395 if (mWebViewController.shouldShowErrorConsole()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700396 mErrorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
397 }
398 }
399
Grace Kloba22ac16e2009-10-07 18:00:23 -0700400 // finally update the UI in the activity if it is in the foreground
John Reck324d4402011-01-11 16:56:42 -0800401 mWebViewController.onPageStarted(Tab.this, view, favicon);
Leon Scroggins4cd97792010-12-03 15:31:56 -0500402
John Recke969cc52010-12-21 17:24:43 -0800403 updateBookmarkedStatus();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700404 }
405
406 @Override
407 public void onPageFinished(WebView view, String url) {
John Reck38b39652012-06-05 09:22:59 -0700408 mDisableOverrideUrlLoading = false;
John Reck5b691842010-11-29 11:21:13 -0800409 if (!isPrivateBrowsingEnabled()) {
410 LogTag.logPageFinishedLoading(
411 url, SystemClock.uptimeMillis() - mLoadStartTime);
412 }
John Reck1cf4b792011-07-26 10:22:22 -0700413 syncCurrentState(view, url);
John Reck324d4402011-01-11 16:56:42 -0800414 mWebViewController.onPageFinished(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700415 }
416
417 // return true if want to hijack the url to let another app to handle it
418 @Override
419 public boolean shouldOverrideUrlLoading(WebView view, String url) {
John Reck38b39652012-06-05 09:22:59 -0700420 if (!mDisableOverrideUrlLoading && mInForeground) {
Michael Kolb18eb3772010-12-10 14:29:51 -0800421 return mWebViewController.shouldOverrideUrlLoading(Tab.this,
422 view, url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700423 } else {
424 return false;
425 }
426 }
427
428 /**
Steve Block2466eff2011-10-03 15:33:09 +0100429 * Updates the security state. This method is called when we discover
430 * another resource to be loaded for this page (for example,
431 * javascript). While we update the security state, we do not update
432 * the lock icon until we are done loading, as it is slightly more
433 * secure this way.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700434 */
435 @Override
436 public void onLoadResource(WebView view, String url) {
437 if (url != null && url.length() > 0) {
438 // It is only if the page claims to be secure that we may have
Steve Block2466eff2011-10-03 15:33:09 +0100439 // to update the security state:
440 if (mCurrentState.mSecurityState == SecurityState.SECURITY_STATE_SECURE) {
441 // If NOT a 'safe' url, change the state to mixed content!
Grace Kloba22ac16e2009-10-07 18:00:23 -0700442 if (!(URLUtil.isHttpsUrl(url) || URLUtil.isDataUrl(url)
443 || URLUtil.isAboutUrl(url))) {
Steve Block2466eff2011-10-03 15:33:09 +0100444 mCurrentState.mSecurityState = SecurityState.SECURITY_STATE_MIXED;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700445 }
446 }
447 }
448 }
449
450 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700451 * Show a dialog informing the user of the network error reported by
452 * WebCore if it is in the foreground.
453 */
454 @Override
455 public void onReceivedError(WebView view, int errorCode,
456 String description, String failingUrl) {
457 if (errorCode != WebViewClient.ERROR_HOST_LOOKUP &&
458 errorCode != WebViewClient.ERROR_CONNECT &&
459 errorCode != WebViewClient.ERROR_BAD_URL &&
460 errorCode != WebViewClient.ERROR_UNSUPPORTED_SCHEME &&
461 errorCode != WebViewClient.ERROR_FILE) {
462 queueError(errorCode, description);
Jeff Hamilton47654f42010-09-07 09:57:51 -0500463
Selim Gurun3da06b82011-10-10 13:58:12 -0700464 // Don't log URLs when in private browsing mode
465 if (!isPrivateBrowsingEnabled()) {
466 Log.e(LOGTAG, "onReceivedError " + errorCode + " " + failingUrl
Jeff Hamilton47654f42010-09-07 09:57:51 -0500467 + " " + description);
Selim Gurun3da06b82011-10-10 13:58:12 -0700468 }
Jeff Hamilton47654f42010-09-07 09:57:51 -0500469 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700470 }
471
472 /**
473 * Check with the user if it is ok to resend POST data as the page they
474 * are trying to navigate to is the result of a POST.
475 */
476 @Override
477 public void onFormResubmission(WebView view, final Message dontResend,
478 final Message resend) {
479 if (!mInForeground) {
480 dontResend.sendToTarget();
481 return;
482 }
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500483 if (mDontResend != null) {
484 Log.w(LOGTAG, "onFormResubmission should not be called again "
485 + "while dialog is still up");
486 dontResend.sendToTarget();
487 return;
488 }
489 mDontResend = dontResend;
490 mResend = resend;
Michael Kolb14612442011-06-24 13:06:29 -0700491 new AlertDialog.Builder(mContext).setTitle(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700492 R.string.browserFrameFormResubmitLabel).setMessage(
493 R.string.browserFrameFormResubmitMessage)
494 .setPositiveButton(R.string.ok,
495 new DialogInterface.OnClickListener() {
496 public void onClick(DialogInterface dialog,
497 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500498 if (mResend != null) {
499 mResend.sendToTarget();
500 mResend = null;
501 mDontResend = null;
502 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700503 }
504 }).setNegativeButton(R.string.cancel,
505 new DialogInterface.OnClickListener() {
506 public void onClick(DialogInterface dialog,
507 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500508 if (mDontResend != null) {
509 mDontResend.sendToTarget();
510 mResend = null;
511 mDontResend = null;
512 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700513 }
514 }).setOnCancelListener(new OnCancelListener() {
515 public void onCancel(DialogInterface dialog) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500516 if (mDontResend != null) {
517 mDontResend.sendToTarget();
518 mResend = null;
519 mDontResend = null;
520 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700521 }
522 }).show();
523 }
524
525 /**
526 * Insert the url into the visited history database.
527 * @param url The url to be inserted.
528 * @param isReload True if this url is being reloaded.
529 * FIXME: Not sure what to do when reloading the page.
530 */
531 @Override
532 public void doUpdateVisitedHistory(WebView view, String url,
533 boolean isReload) {
John Reck324d4402011-01-11 16:56:42 -0800534 mWebViewController.doUpdateVisitedHistory(Tab.this, isReload);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700535 }
536
537 /**
538 * Displays SSL error(s) dialog to the user.
539 */
540 @Override
541 public void onReceivedSslError(final WebView view,
542 final SslErrorHandler handler, final SslError error) {
543 if (!mInForeground) {
544 handler.cancel();
Steve Block2466eff2011-10-03 15:33:09 +0100545 setSecurityState(SecurityState.SECURITY_STATE_NOT_SECURE);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700546 return;
547 }
John Reck35e9dd62011-04-25 09:01:54 -0700548 if (mSettings.showSecurityWarnings()) {
John Reckcb28b2c2011-08-26 17:39:44 -0700549 new AlertDialog.Builder(mContext)
550 .setTitle(R.string.security_warning)
551 .setMessage(R.string.ssl_warnings_header)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200552 .setIconAttribute(android.R.attr.alertDialogIcon)
John Reckcb28b2c2011-08-26 17:39:44 -0700553 .setPositiveButton(R.string.ssl_continue,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700554 new DialogInterface.OnClickListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700555 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700556 public void onClick(DialogInterface dialog,
557 int whichButton) {
558 handler.proceed();
Steve Block4895b012011-10-03 16:26:46 +0100559 handleProceededAfterSslError(error);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700560 }
John Reckcb28b2c2011-08-26 17:39:44 -0700561 })
562 .setNeutralButton(R.string.view_certificate,
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) {
John Reckcb28b2c2011-08-26 17:39:44 -0700567 mWebViewController.showSslCertificateOnError(
568 view, handler, error);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700569 }
John Reckcb28b2c2011-08-26 17:39:44 -0700570 })
571 .setNegativeButton(R.string.ssl_go_back,
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 Reck30c714c2010-12-16 17:30:34 -0800576 dialog.cancel();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700577 }
John Reckcb28b2c2011-08-26 17:39:44 -0700578 })
579 .setOnCancelListener(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700580 new DialogInterface.OnCancelListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700581 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700582 public void onCancel(DialogInterface dialog) {
583 handler.cancel();
Steve Block2466eff2011-10-03 15:33:09 +0100584 setSecurityState(SecurityState.SECURITY_STATE_NOT_SECURE);
John Reck30c714c2010-12-16 17:30:34 -0800585 mWebViewController.onUserCanceledSsl(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700586 }
John Reckcb28b2c2011-08-26 17:39:44 -0700587 })
588 .show();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700589 } else {
590 handler.proceed();
591 }
592 }
593
594 /**
Steve Block4895b012011-10-03 16:26:46 +0100595 * Called when an SSL error occurred while loading a resource, but the
596 * WebView but chose to proceed anyway based on a decision retained
597 * from a previous response to onReceivedSslError(). We update our
598 * security state to reflect this.
599 */
600 @Override
601 public void onProceededAfterSslError(WebView view, SslError error) {
602 handleProceededAfterSslError(error);
603 }
604
605 /**
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700606 * Displays client certificate request to the user.
607 */
608 @Override
609 public void onReceivedClientCertRequest(final WebView view,
610 final ClientCertRequestHandler handler, final String host_and_port) {
611 if (!mInForeground) {
612 handler.ignore();
613 return;
614 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700615 int colon = host_and_port.lastIndexOf(':');
616 String host;
617 int port;
618 if (colon == -1) {
619 host = host_and_port;
620 port = -1;
621 } else {
622 String portString = host_and_port.substring(colon + 1);
623 try {
624 port = Integer.parseInt(portString);
625 host = host_and_port.substring(0, colon);
626 } catch (NumberFormatException e) {
627 host = host_and_port;
628 port = -1;
629 }
630 }
Michael Kolb14612442011-06-24 13:06:29 -0700631 KeyChain.choosePrivateKeyAlias(
632 mWebViewController.getActivity(), new KeyChainAliasCallback() {
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700633 @Override public void alias(String alias) {
634 if (alias == null) {
635 handler.cancel();
636 return;
637 }
Michael Kolb14612442011-06-24 13:06:29 -0700638 new KeyChainLookup(mContext, handler, alias).execute();
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700639 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700640 }, null, null, host, port, null);
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700641 }
642
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800643 @Override
644 public void onRendererCrash(WebView view, boolean crashedWhileOomProtected) {
645 Log.e(LOGTAG, "Tab Crashed");
646 hasCrashed = true;
647 showCrashView();
648 }
649
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700650 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700651 * Handles an HTTP authentication request.
652 *
653 * @param handler The authentication handler
654 * @param host The host
655 * @param realm The realm
656 */
657 @Override
658 public void onReceivedHttpAuthRequest(WebView view,
659 final HttpAuthHandler handler, final String host,
660 final String realm) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700661 mWebViewController.onReceivedHttpAuthRequest(Tab.this, view, handler, host, realm);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700662 }
663
664 @Override
John Reck438bf462011-01-12 18:11:46 -0800665 public WebResourceResponse shouldInterceptRequest(WebView view,
666 String url) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800667 //intercept if opening a new incognito tab - show the incognito welcome page
668 if (url.startsWith("browser:incognito")) {
669 Resources resourceHandle = mContext.getResources();
670 InputStream inStream = resourceHandle.openRawResource(
Bijan Amirzada41242f22014-03-21 12:12:18 -0700671 com.android.browser.R.raw.incognito_mode_start_page);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800672 return new WebResourceResponse("text/html", "utf8", inStream);
673 }
kaiyiz6e5b3e02013-08-19 20:02:01 +0800674 WebResourceResponse res;
675 if (MyNavigationUtil.MY_NAVIGATION.equals(url)) {
676 res = MyNavigationProvider.shouldInterceptRequest(mContext, url);
677 } else {
678 res = HomeProvider.shouldInterceptRequest(mContext, url);
679 }
John Reck438bf462011-01-12 18:11:46 -0800680 return res;
681 }
682
683 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700684 public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
685 if (!mInForeground) {
686 return false;
687 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700688 return mWebViewController.shouldOverrideKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700689 }
690
691 @Override
692 public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700693 if (!mInForeground) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700694 return;
695 }
John Reck997b1b72012-04-19 18:08:25 -0700696 if (!mWebViewController.onUnhandledKeyEvent(event)) {
697 super.onUnhandledKeyEvent(view, event);
698 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700699 }
700 };
701
John Reck1cf4b792011-07-26 10:22:22 -0700702 private void syncCurrentState(WebView view, String url) {
703 // Sync state (in case of stop/timeout)
704 mCurrentState.mUrl = view.getUrl();
705 if (mCurrentState.mUrl == null) {
John Reck8bcafc12011-08-29 16:43:02 -0700706 mCurrentState.mUrl = "";
John Reck1cf4b792011-07-26 10:22:22 -0700707 }
708 mCurrentState.mOriginalUrl = view.getOriginalUrl();
709 mCurrentState.mTitle = view.getTitle();
710 mCurrentState.mFavicon = view.getFavicon();
711 if (!URLUtil.isHttpsUrl(mCurrentState.mUrl)) {
712 // In case we stop when loading an HTTPS page from an HTTP page
713 // but before a provisional load occurred
Steve Block2466eff2011-10-03 15:33:09 +0100714 mCurrentState.mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
Steve Block08a6f0c2011-10-06 12:12:53 +0100715 mCurrentState.mSslCertificateError = null;
John Reck1cf4b792011-07-26 10:22:22 -0700716 }
John Reck502a3532011-08-16 14:21:46 -0700717 mCurrentState.mIncognito = view.isPrivateBrowsingEnabled();
John Reck1cf4b792011-07-26 10:22:22 -0700718 }
719
Grace Kloba22ac16e2009-10-07 18:00:23 -0700720 // -------------------------------------------------------------------------
721 // WebChromeClient implementation for the main WebView
722 // -------------------------------------------------------------------------
723
724 private final WebChromeClient mWebChromeClient = new WebChromeClient() {
725 // Helper method to create a new tab or sub window.
726 private void createWindow(final boolean dialog, final Message msg) {
727 WebView.WebViewTransport transport =
728 (WebView.WebViewTransport) msg.obj;
729 if (dialog) {
730 createSubWindow();
Michael Kolb8233fac2010-10-26 16:08:53 -0700731 mWebViewController.attachSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700732 transport.setWebView(mSubView);
733 } else {
Michael Kolb7bcafde2011-05-09 13:55:59 -0700734 final Tab newTab = mWebViewController.openTab(null,
John Reck5949c662011-05-27 09:52:29 -0700735 Tab.this, true, true);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700736 transport.setWebView(newTab.getWebView());
737 }
738 msg.sendToTarget();
739 }
740
741 @Override
742 public boolean onCreateWindow(WebView view, final boolean dialog,
743 final boolean userGesture, final Message resultMsg) {
744 // only allow new window or sub window for the foreground case
745 if (!mInForeground) {
746 return false;
747 }
748 // Short-circuit if we can't create any more tabs or sub windows.
749 if (dialog && mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -0700750 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700751 .setTitle(R.string.too_many_subwindows_dialog_title)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200752 .setIconAttribute(android.R.attr.alertDialogIcon)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700753 .setMessage(R.string.too_many_subwindows_dialog_message)
754 .setPositiveButton(R.string.ok, null)
755 .show();
756 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700757 } else if (!mWebViewController.getTabControl().canCreateNewTab()) {
Michael Kolb14612442011-06-24 13:06:29 -0700758 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700759 .setTitle(R.string.too_many_windows_dialog_title)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200760 .setIconAttribute(android.R.attr.alertDialogIcon)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700761 .setMessage(R.string.too_many_windows_dialog_message)
762 .setPositiveButton(R.string.ok, null)
763 .show();
764 return false;
765 }
766
767 // Short-circuit if this was a user gesture.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800768 if (userGesture || !mSettings.blockPopupWindows()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700769 createWindow(dialog, resultMsg);
770 return true;
771 }
772
773 // Allow the popup and create the appropriate window.
774 final AlertDialog.OnClickListener allowListener =
775 new AlertDialog.OnClickListener() {
776 public void onClick(DialogInterface d,
777 int which) {
778 createWindow(dialog, resultMsg);
779 }
780 };
781
782 // Block the popup by returning a null WebView.
783 final AlertDialog.OnClickListener blockListener =
784 new AlertDialog.OnClickListener() {
785 public void onClick(DialogInterface d, int which) {
786 resultMsg.sendToTarget();
787 }
788 };
789
790 // Build a confirmation dialog to display to the user.
791 final AlertDialog d =
Michael Kolb14612442011-06-24 13:06:29 -0700792 new AlertDialog.Builder(mContext)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200793 .setIconAttribute(android.R.attr.alertDialogIcon)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700794 .setMessage(R.string.popup_window_attempt)
795 .setPositiveButton(R.string.allow, allowListener)
796 .setNegativeButton(R.string.block, blockListener)
797 .setCancelable(false)
798 .create();
799
800 // Show the confirmation dialog.
801 d.show();
802 return true;
803 }
804
805 @Override
Patrick Scotteb5061b2009-11-18 15:00:30 -0500806 public void onRequestFocus(WebView view) {
807 if (!mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700808 mWebViewController.switchToTab(Tab.this);
Patrick Scotteb5061b2009-11-18 15:00:30 -0500809 }
810 }
811
812 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700813 public void onCloseWindow(WebView window) {
Michael Kolbc831b632011-05-11 09:30:34 -0700814 if (mParent != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700815 // JavaScript can only close popup window.
816 if (mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700817 mWebViewController.switchToTab(mParent);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700818 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700819 mWebViewController.closeTab(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700820 }
821 }
822
823 @Override
824 public void onProgressChanged(WebView view, int newProgress) {
John Reck30c714c2010-12-16 17:30:34 -0800825 mPageLoadProgress = newProgress;
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800826 if (newProgress == 100) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800827 Log.i(CONSOLE_LOGTAG, "SWE Pageload Progress = 100");
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800828 mInPageLoad = false;
829 }
John Reck30c714c2010-12-16 17:30:34 -0800830 mWebViewController.onProgressChanged(Tab.this);
Michael Kolb72864272012-05-03 15:42:15 -0700831 if (mUpdateThumbnail && newProgress == 100) {
832 mUpdateThumbnail = false;
833 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700834 }
835
836 @Override
Leon Scroggins21d9b902010-03-11 09:33:11 -0500837 public void onReceivedTitle(WebView view, final String title) {
John Reck30c714c2010-12-16 17:30:34 -0800838 mCurrentState.mTitle = title;
Michael Kolb8233fac2010-10-26 16:08:53 -0700839 mWebViewController.onReceivedTitle(Tab.this, title);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700840 }
841
842 @Override
843 public void onReceivedIcon(WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -0800844 mCurrentState.mFavicon = icon;
Michael Kolb8233fac2010-10-26 16:08:53 -0700845 mWebViewController.onFavicon(Tab.this, view, icon);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700846 }
847
848 @Override
849 public void onReceivedTouchIconUrl(WebView view, String url,
850 boolean precomposed) {
Michael Kolb14612442011-06-24 13:06:29 -0700851 final ContentResolver cr = mContext.getContentResolver();
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400852 // Let precomposed icons take precedence over non-composed
853 // icons.
854 if (precomposed && mTouchIconLoader != null) {
855 mTouchIconLoader.cancel(false);
856 mTouchIconLoader = null;
857 }
858 // Have only one async task at a time.
859 if (mTouchIconLoader == null) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700860 mTouchIconLoader = new DownloadTouchIcon(Tab.this,
Michael Kolb14612442011-06-24 13:06:29 -0700861 mContext, cr, view);
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400862 mTouchIconLoader.execute(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700863 }
864 }
865
866 @Override
867 public void onShowCustomView(View view,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800868 CustomViewCallback callback) {
Michael Kolb14612442011-06-24 13:06:29 -0700869 Activity activity = mWebViewController.getActivity();
870 if (activity != null) {
871 onShowCustomView(view, activity.getRequestedOrientation(), callback);
872 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400873 }
874
875 @Override
876 public void onShowCustomView(View view, int requestedOrientation,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800877 CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700878 if (mInForeground) mWebViewController.showCustomView(Tab.this, view,
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400879 requestedOrientation, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700880 }
881
882 @Override
883 public void onHideCustomView() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700884 if (mInForeground) mWebViewController.hideCustomView();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700885 }
886
887 /**
888 * The origin has exceeded its database quota.
889 * @param url the URL that exceeded the quota
890 * @param databaseIdentifier the identifier of the database on which the
891 * transaction that caused the quota overflow was run
892 * @param currentQuota the current quota for the origin.
893 * @param estimatedSize the estimated size of the database.
894 * @param totalUsedQuota is the sum of all origins' quota.
895 * @param quotaUpdater The callback to run when a decision to allow or
896 * deny quota has been made. Don't forget to call this!
897 */
898 @Override
899 public void onExceededDatabaseQuota(String url,
900 String databaseIdentifier, long currentQuota, long estimatedSize,
901 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -0700902 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -0700903 .onExceededDatabaseQuota(url, databaseIdentifier,
904 currentQuota, estimatedSize, totalUsedQuota,
905 quotaUpdater);
906 }
907
908 /**
909 * The Application Cache has exceeded its max size.
910 * @param spaceNeeded is the amount of disk space that would be needed
911 * in order for the last appcache operation to succeed.
912 * @param totalUsedQuota is the sum of all origins' quota.
913 * @param quotaUpdater A callback to inform the WebCore thread that a
914 * new app cache size is available. This callback must always
915 * be executed at some point to ensure that the sleeping
916 * WebCore thread is woken up.
917 */
918 @Override
919 public void onReachedMaxAppCacheSize(long spaceNeeded,
920 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -0700921 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -0700922 .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota,
923 quotaUpdater);
924 }
925
926 /**
927 * Instructs the browser to show a prompt to ask the user to set the
928 * Geolocation permission state for the specified origin.
929 * @param origin The origin for which Geolocation permissions are
930 * requested.
931 * @param callback The callback to call once the user has set the
932 * Geolocation permission state.
933 */
934 @Override
935 public void onGeolocationPermissionsShowPrompt(String origin,
936 GeolocationPermissions.Callback callback) {
937 if (mInForeground) {
Grace Kloba50c241e2010-04-20 11:07:50 -0700938 getGeolocationPermissionsPrompt().show(origin, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700939 }
940 }
941
942 /**
943 * Instructs the browser to hide the Geolocation permissions prompt.
944 */
945 @Override
946 public void onGeolocationPermissionsHidePrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -0700947 if (mInForeground && mGeolocationPermissionsPrompt != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700948 mGeolocationPermissionsPrompt.hide();
949 }
950 }
951
Ben Murdoch65acc352009-11-19 18:16:04 +0000952 /* Adds a JavaScript error message to the system log and if the JS
953 * console is enabled in the about:debug options, to that console
954 * also.
Ben Murdochc42addf2010-01-28 15:19:59 +0000955 * @param consoleMessage the message object.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700956 */
957 @Override
Ben Murdochc42addf2010-01-28 15:19:59 +0000958 public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700959 if (mInForeground) {
960 // call getErrorConsole(true) so it will create one if needed
961 ErrorConsoleView errorConsole = getErrorConsole(true);
Ben Murdochc42addf2010-01-28 15:19:59 +0000962 errorConsole.addErrorMessage(consoleMessage);
Michael Kolb8233fac2010-10-26 16:08:53 -0700963 if (mWebViewController.shouldShowErrorConsole()
964 && errorConsole.getShowState() !=
965 ErrorConsoleView.SHOW_MAXIMIZED) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700966 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
967 }
968 }
Ben Murdochc42addf2010-01-28 15:19:59 +0000969
Jeff Hamilton47654f42010-09-07 09:57:51 -0500970 // Don't log console messages in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -0700971 if (isPrivateBrowsingEnabled()) return true;
Jeff Hamilton47654f42010-09-07 09:57:51 -0500972
Ben Murdochc42addf2010-01-28 15:19:59 +0000973 String message = "Console: " + consoleMessage.message() + " "
974 + consoleMessage.sourceId() + ":"
975 + consoleMessage.lineNumber();
976
977 switch (consoleMessage.messageLevel()) {
978 case TIP:
979 Log.v(CONSOLE_LOGTAG, message);
980 break;
981 case LOG:
982 Log.i(CONSOLE_LOGTAG, message);
983 break;
984 case WARNING:
985 Log.w(CONSOLE_LOGTAG, message);
986 break;
987 case ERROR:
988 Log.e(CONSOLE_LOGTAG, message);
989 break;
990 case DEBUG:
991 Log.d(CONSOLE_LOGTAG, message);
992 break;
993 }
994
995 return true;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700996 }
997
998 /**
999 * Ask the browser for an icon to represent a <video> element.
1000 * This icon will be used if the Web page did not specify a poster attribute.
1001 * @return Bitmap The icon or null if no such icon is available.
1002 */
1003 @Override
1004 public Bitmap getDefaultVideoPoster() {
1005 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001006 return mWebViewController.getDefaultVideoPoster();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001007 }
1008 return null;
1009 }
1010
1011 /**
1012 * Ask the host application for a custom progress view to show while
1013 * a <video> is loading.
1014 * @return View The progress view.
1015 */
1016 @Override
1017 public View getVideoLoadingProgressView() {
1018 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001019 return mWebViewController.getVideoLoadingProgressView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001020 }
1021 return null;
1022 }
1023
1024 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00001025 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001026 if (mInForeground) {
Ben Murdoch8cad4132012-01-11 10:56:43 +00001027 mWebViewController.openFileChooser(uploadMsg, acceptType, capture);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001028 } else {
1029 uploadMsg.onReceiveValue(null);
1030 }
1031 }
1032
1033 /**
1034 * Deliver a list of already-visited URLs
1035 */
1036 @Override
1037 public void getVisitedHistory(final ValueCallback<String[]> callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001038 mWebViewController.getVisitedHistory(callback);
1039 }
Ben Murdoch8029a772010-11-16 11:58:21 +00001040
1041 @Override
1042 public void setupAutoFill(Message message) {
1043 // Prompt the user to set up their profile.
1044 final Message msg = message;
Michael Kolb14612442011-06-24 13:06:29 -07001045 AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
1046 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
Ben Murdoch1d676b62011-01-17 12:54:24 +00001047 Context.LAYOUT_INFLATER_SERVICE);
1048 final View layout = inflater.inflate(R.layout.setup_autofill_dialog, null);
1049
1050 builder.setView(layout)
1051 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1052 @Override
1053 public void onClick(DialogInterface dialog, int id) {
1054 CheckBox disableAutoFill = (CheckBox) layout.findViewById(
1055 R.id.setup_autofill_dialog_disable_autofill);
1056
1057 if (disableAutoFill.isChecked()) {
1058 // Disable autofill and show a toast with how to turn it on again.
John Reck35e9dd62011-04-25 09:01:54 -07001059 mSettings.setAutofillEnabled(false);
Michael Kolb14612442011-06-24 13:06:29 -07001060 Toast.makeText(mContext,
Ben Murdoch1d676b62011-01-17 12:54:24 +00001061 R.string.autofill_setup_dialog_negative_toast,
1062 Toast.LENGTH_LONG).show();
1063 } else {
1064 // Take user to the AutoFill profile editor. When they return,
1065 // we will send the message that we pass here which will trigger
1066 // the form to get filled out with their new profile.
1067 mWebViewController.setupAutoFill(msg);
1068 }
1069 }
1070 })
1071 .setNegativeButton(R.string.cancel, null)
1072 .show();
Ben Murdoch8029a772010-11-16 11:58:21 +00001073 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001074 };
1075
1076 // -------------------------------------------------------------------------
1077 // WebViewClient implementation for the sub window
1078 // -------------------------------------------------------------------------
1079
1080 // Subclass of WebViewClient used in subwindows to notify the main
1081 // WebViewClient of certain WebView activities.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001082 private static class SubWindowClient extends WebViewClient {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001083 // The main WebViewClient.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001084 private final WebViewClient mClient;
Michael Kolb8233fac2010-10-26 16:08:53 -07001085 private final WebViewController mController;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001086
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001087 SubWindowClient(WebViewClient client, WebViewController controller) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001088 mClient = client;
Michael Kolb8233fac2010-10-26 16:08:53 -07001089 mController = controller;
Leon Scroggins III211ba542010-04-19 13:21:13 -04001090 }
1091 @Override
1092 public void onPageStarted(WebView view, String url, Bitmap favicon) {
1093 // Unlike the others, do not call mClient's version, which would
1094 // change the progress bar. However, we do want to remove the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001095 // find or select dialog.
Michael Kolb8233fac2010-10-26 16:08:53 -07001096 mController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001097 }
1098 @Override
1099 public void doUpdateVisitedHistory(WebView view, String url,
1100 boolean isReload) {
1101 mClient.doUpdateVisitedHistory(view, url, isReload);
1102 }
1103 @Override
1104 public boolean shouldOverrideUrlLoading(WebView view, String url) {
1105 return mClient.shouldOverrideUrlLoading(view, url);
1106 }
1107 @Override
1108 public void onReceivedSslError(WebView view, SslErrorHandler handler,
1109 SslError error) {
1110 mClient.onReceivedSslError(view, handler, error);
1111 }
1112 @Override
Brian Carlstrom8862c1d2011-06-02 01:05:55 -07001113 public void onReceivedClientCertRequest(WebView view,
1114 ClientCertRequestHandler handler, String host_and_port) {
1115 mClient.onReceivedClientCertRequest(view, handler, host_and_port);
1116 }
1117 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -07001118 public void onReceivedHttpAuthRequest(WebView view,
1119 HttpAuthHandler handler, String host, String realm) {
1120 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
1121 }
1122 @Override
1123 public void onFormResubmission(WebView view, Message dontResend,
1124 Message resend) {
1125 mClient.onFormResubmission(view, dontResend, resend);
1126 }
1127 @Override
1128 public void onReceivedError(WebView view, int errorCode,
1129 String description, String failingUrl) {
1130 mClient.onReceivedError(view, errorCode, description, failingUrl);
1131 }
1132 @Override
1133 public boolean shouldOverrideKeyEvent(WebView view,
1134 android.view.KeyEvent event) {
1135 return mClient.shouldOverrideKeyEvent(view, event);
1136 }
1137 @Override
1138 public void onUnhandledKeyEvent(WebView view,
1139 android.view.KeyEvent event) {
1140 mClient.onUnhandledKeyEvent(view, event);
1141 }
1142 }
1143
1144 // -------------------------------------------------------------------------
1145 // WebChromeClient implementation for the sub window
1146 // -------------------------------------------------------------------------
1147
1148 private class SubWindowChromeClient extends WebChromeClient {
1149 // The main WebChromeClient.
1150 private final WebChromeClient mClient;
1151
1152 SubWindowChromeClient(WebChromeClient client) {
1153 mClient = client;
1154 }
1155 @Override
1156 public void onProgressChanged(WebView view, int newProgress) {
1157 mClient.onProgressChanged(view, newProgress);
1158 }
1159 @Override
1160 public boolean onCreateWindow(WebView view, boolean dialog,
1161 boolean userGesture, android.os.Message resultMsg) {
1162 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
1163 }
1164 @Override
1165 public void onCloseWindow(WebView window) {
1166 if (window != mSubView) {
1167 Log.e(LOGTAG, "Can't close the window");
1168 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001169 mWebViewController.dismissSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001170 }
1171 }
1172
1173 // -------------------------------------------------------------------------
1174
1175 // Construct a new tab
Michael Kolb7bcafde2011-05-09 13:55:59 -07001176 Tab(WebViewController wvcontroller, WebView w) {
John Reck1cf4b792011-07-26 10:22:22 -07001177 this(wvcontroller, w, null);
1178 }
1179
1180 Tab(WebViewController wvcontroller, Bundle state) {
1181 this(wvcontroller, null, state);
1182 }
1183
1184 Tab(WebViewController wvcontroller, WebView w, Bundle state) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001185 mWebViewController = wvcontroller;
Michael Kolb14612442011-06-24 13:06:29 -07001186 mContext = mWebViewController.getContext();
John Reck35e9dd62011-04-25 09:01:54 -07001187 mSettings = BrowserSettings.getInstance();
Michael Kolb14612442011-06-24 13:06:29 -07001188 mDataController = DataController.getInstance(mContext);
1189 mCurrentState = new PageState(mContext, w != null
John Reck847b5322011-04-14 17:02:18 -07001190 ? w.isPrivateBrowsingEnabled() : false);
Michael Kolb8233fac2010-10-26 16:08:53 -07001191 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001192 mInForeground = false;
1193
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001194 mDownloadListener = new BrowserDownloadListener() {
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001195 public void onDownloadStart(String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001196 String contentDisposition, String mimetype, String referer,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001197 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001198 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001199 mimetype, referer, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001200 }
1201 };
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001202 mWebBackForwardListClient = new WebBackForwardListClient() {
1203 @Override
1204 public void onNewHistoryItem(WebHistoryItem item) {
Mathew Inwooda829d552011-09-02 14:16:25 +01001205 if (mClearHistoryUrlPattern != null) {
1206 boolean match =
1207 mClearHistoryUrlPattern.matcher(item.getOriginalUrl()).matches();
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001208 if (LOGD_ENABLED) {
Mathew Inwooda829d552011-09-02 14:16:25 +01001209 Log.d(LOGTAG, "onNewHistoryItem: match=" + match + "\n\t"
1210 + item.getUrl() + "\n\t"
1211 + mClearHistoryUrlPattern);
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001212 }
Mathew Inwooda829d552011-09-02 14:16:25 +01001213 if (match) {
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001214 if (mMainView != null) {
1215 mMainView.clearHistory();
1216 }
1217 }
Mathew Inwooda829d552011-09-02 14:16:25 +01001218 mClearHistoryUrlPattern = null;
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001219 }
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001220 }
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001221 };
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001222
John Reck1cf4b792011-07-26 10:22:22 -07001223 mCaptureWidth = mContext.getResources().getDimensionPixelSize(
1224 R.dimen.tab_thumbnail_width);
1225 mCaptureHeight = mContext.getResources().getDimensionPixelSize(
1226 R.dimen.tab_thumbnail_height);
1227 updateShouldCaptureThumbnails();
1228 restoreState(state);
John Reck52be4782011-08-26 15:37:29 -07001229 if (getId() == -1) {
1230 mId = TabControl.getNextId();
1231 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001232 setWebView(w);
Michael Kolb9ef259a2011-07-12 15:33:08 -07001233 mHandler = new Handler() {
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001234 @Override
Michael Kolb9ef259a2011-07-12 15:33:08 -07001235 public void handleMessage(Message m) {
John Reck1cf4b792011-07-26 10:22:22 -07001236 switch (m.what) {
1237 case MSG_CAPTURE:
1238 capture();
1239 break;
1240 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001241 }
1242 };
John Reck1cf4b792011-07-26 10:22:22 -07001243 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001244
Michael Kolb72864272012-05-03 15:42:15 -07001245 public boolean shouldUpdateThumbnail() {
1246 return mUpdateThumbnail;
1247 }
1248
Mathew Inwoode09305e2011-09-02 12:03:26 +01001249 /**
1250 * This is used to get a new ID when the tab has been preloaded, before it is displayed and
1251 * added to TabControl. Preloaded tabs can be created before restoreInstanceState, leading
1252 * to overlapping IDs between the preloaded and restored tabs.
1253 */
1254 public void refreshIdAfterPreload() {
1255 mId = TabControl.getNextId();
1256 }
1257
John Reck1cf4b792011-07-26 10:22:22 -07001258 public void updateShouldCaptureThumbnails() {
1259 if (mWebViewController.shouldCaptureThumbnails()) {
1260 synchronized (Tab.this) {
1261 if (mCapture == null) {
1262 mCapture = Bitmap.createBitmap(mCaptureWidth, mCaptureHeight,
1263 Bitmap.Config.RGB_565);
Michael Kolbc3af0672011-08-09 10:24:41 -07001264 mCapture.eraseColor(Color.WHITE);
John Reck1cf4b792011-07-26 10:22:22 -07001265 if (mInForeground) {
1266 postCapture();
1267 }
1268 }
1269 }
1270 } else {
1271 synchronized (Tab.this) {
1272 mCapture = null;
1273 deleteThumbnail();
1274 }
1275 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001276 }
1277
Michael Kolb14612442011-06-24 13:06:29 -07001278 public void setController(WebViewController ctl) {
1279 mWebViewController = ctl;
John Reck1cf4b792011-07-26 10:22:22 -07001280 updateShouldCaptureThumbnails();
Michael Kolb14612442011-06-24 13:06:29 -07001281 }
1282
Michael Kolbc831b632011-05-11 09:30:34 -07001283 public long getId() {
1284 return mId;
1285 }
1286
Michael Kolb91911a22012-01-17 11:21:25 -08001287 void setWebView(WebView w) {
1288 setWebView(w, true);
1289 }
1290
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001291 public boolean isNativeActive(){
1292 if (mMainView == null)
1293 return false;
1294 return true;
1295 }
1296
1297 public void setTimeStamp(){
1298 Date d = new Date();
1299 timestamp = (new Timestamp(d.getTime()));
1300 }
1301
1302 public Timestamp getTimestamp() {
1303 return timestamp;
1304 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001305 /**
1306 * Sets the WebView for this tab, correctly removing the old WebView from
1307 * the container view.
1308 */
Michael Kolb91911a22012-01-17 11:21:25 -08001309 void setWebView(WebView w, boolean restore) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001310 if (mMainView == w) {
1311 return;
1312 }
Michael Kolba713ec82010-11-29 17:27:06 -08001313
Grace Kloba22ac16e2009-10-07 18:00:23 -07001314 // If the WebView is changing, the page will be reloaded, so any ongoing
1315 // Geolocation permission requests are void.
Grace Kloba50c241e2010-04-20 11:07:50 -07001316 if (mGeolocationPermissionsPrompt != null) {
1317 mGeolocationPermissionsPrompt.hide();
1318 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001319
Michael Kolba713ec82010-11-29 17:27:06 -08001320 mWebViewController.onSetWebView(this, w);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001321
John Reck1cf4b792011-07-26 10:22:22 -07001322 if (mMainView != null) {
John Reck8ee633f2011-08-09 16:00:35 -07001323 mMainView.setPictureListener(null);
John Reck1cf4b792011-07-26 10:22:22 -07001324 if (w != null) {
1325 syncCurrentState(w, null);
1326 } else {
Panos Thomasa9a5a582014-03-18 19:20:08 -07001327 mCurrentState = new PageState(mContext, mMainView.isPrivateBrowsingEnabled());
John Reck1cf4b792011-07-26 10:22:22 -07001328 }
1329 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001330 // set the new one
1331 mMainView = w;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001332 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001333 if (mMainView != null) {
1334 mMainView.setWebViewClient(mWebViewClient);
1335 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001336 // Attach DownloadManager so that downloads can start in an active
1337 // or a non-active window. This can happen when going to a site that
1338 // does a redirect after a period of time. The user could have
1339 // switched to another tab while waiting for the download to start.
1340 mMainView.setDownloadListener(mDownloadListener);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001341 getWebView().setWebBackForwardListClient(mWebBackForwardListClient);
John Reck8ee633f2011-08-09 16:00:35 -07001342 TabControl tc = mWebViewController.getTabControl();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001343 if (tc != null /*&& tc.getOnThumbnailUpdatedListener() != null*/) {
John Reck8ee633f2011-08-09 16:00:35 -07001344 mMainView.setPictureListener(this);
1345 }
Michael Kolb91911a22012-01-17 11:21:25 -08001346 if (restore && (mSavedState != null)) {
John Reck8b9bb8b2012-03-08 13:19:40 -08001347 restoreUserAgent();
John Reck6c2e2f32011-08-22 13:41:23 -07001348 WebBackForwardList restoredState
1349 = mMainView.restoreState(mSavedState);
1350 if (restoredState == null || restoredState.getSize() == 0) {
1351 Log.w(LOGTAG, "Failed to restore WebView state!");
1352 loadUrl(mCurrentState.mOriginalUrl, null);
1353 }
John Reck1cf4b792011-07-26 10:22:22 -07001354 mSavedState = null;
1355 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001356 }
1357 }
1358
1359 /**
1360 * Destroy the tab's main WebView and subWindow if any
1361 */
1362 void destroy() {
Matthew Huib7f2e9c2014-04-16 11:12:37 -04001363 if (mPostponeDestroy) {
1364 mShouldDestroy = true;
1365 return;
1366 }
1367 mShouldDestroy = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001368 if (mMainView != null) {
1369 dismissSubWindow();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001370 // save the WebView to call destroy() after detach it from the tab
1371 WebView webView = mMainView;
1372 setWebView(null);
1373 webView.destroy();
1374 }
1375 }
1376
Matthew Huib7f2e9c2014-04-16 11:12:37 -04001377 private boolean mPostponeDestroy = false;
1378 private boolean mShouldDestroy = false;
1379
1380 public void postponeDestroy() {
1381 mPostponeDestroy = true;
1382 }
1383
1384 public void performPostponedDestroy() {
1385 mPostponeDestroy = false;
1386 if (mShouldDestroy) {
1387 destroy();
1388 }
1389 }
1390
Grace Kloba22ac16e2009-10-07 18:00:23 -07001391 /**
1392 * Remove the tab from the parent
1393 */
1394 void removeFromTree() {
1395 // detach the children
Michael Kolbc831b632011-05-11 09:30:34 -07001396 if (mChildren != null) {
1397 for(Tab t : mChildren) {
1398 t.setParent(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001399 }
1400 }
1401 // remove itself from the parent list
Michael Kolbc831b632011-05-11 09:30:34 -07001402 if (mParent != null) {
1403 mParent.mChildren.remove(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001404 }
John Reck1cf4b792011-07-26 10:22:22 -07001405 deleteThumbnail();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001406 }
1407
1408 /**
1409 * Create a new subwindow unless a subwindow already exists.
1410 * @return True if a new subwindow was created. False if one already exists.
1411 */
1412 boolean createSubWindow() {
1413 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001414 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001415 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001416 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001417 mSubView.setWebChromeClient(new SubWindowChromeClient(
1418 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001419 // Set a different DownloadListener for the mSubView, since it will
1420 // just need to dismiss the mSubView, rather than close the Tab
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001421 mSubView.setDownloadListener(new BrowserDownloadListener() {
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001422 public void onDownloadStart(String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001423 String contentDisposition, String mimetype, String referer,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001424 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001425 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001426 contentDisposition, mimetype, referer, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001427 if (mSubView.copyBackForwardList().getSize() == 0) {
1428 // This subwindow was opened for the sole purpose of
1429 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001430 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001431 }
1432 }
1433 });
Michael Kolb14612442011-06-24 13:06:29 -07001434 mSubView.setOnCreateContextMenuListener(mWebViewController.getActivity());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001435 return true;
1436 }
1437 return false;
1438 }
1439
1440 /**
1441 * Dismiss the subWindow for the tab.
1442 */
1443 void dismissSubWindow() {
1444 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001445 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001446 mSubView.destroy();
1447 mSubView = null;
1448 mSubViewContainer = null;
1449 }
1450 }
1451
Grace Kloba22ac16e2009-10-07 18:00:23 -07001452
1453 /**
1454 * Set the parent tab of this tab.
1455 */
Michael Kolbc831b632011-05-11 09:30:34 -07001456 void setParent(Tab parent) {
John Reck52be4782011-08-26 15:37:29 -07001457 if (parent == this) {
1458 throw new IllegalStateException("Cannot set parent to self!");
1459 }
Michael Kolbc831b632011-05-11 09:30:34 -07001460 mParent = parent;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001461 // This tab may have been freed due to low memory. If that is the case,
Michael Kolbc831b632011-05-11 09:30:34 -07001462 // the parent tab id is already saved. If we are changing that id
Grace Kloba22ac16e2009-10-07 18:00:23 -07001463 // (most likely due to removing the parent tab) we must update the
Michael Kolbc831b632011-05-11 09:30:34 -07001464 // parent tab id in the saved Bundle.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001465 if (mSavedState != null) {
1466 if (parent == null) {
1467 mSavedState.remove(PARENTTAB);
1468 } else {
Michael Kolbc831b632011-05-11 09:30:34 -07001469 mSavedState.putLong(PARENTTAB, parent.getId());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001470 }
1471 }
John Reckb0a86db2011-05-24 14:05:58 -07001472
1473 // Sync the WebView useragent with the parent
1474 if (parent != null && mSettings.hasDesktopUseragent(parent.getWebView())
1475 != mSettings.hasDesktopUseragent(getWebView())) {
1476 mSettings.toggleDesktopUseragent(getWebView());
1477 }
John Reck52be4782011-08-26 15:37:29 -07001478
1479 if (parent != null && parent.getId() == getId()) {
1480 throw new IllegalStateException("Parent has same ID as child!");
1481 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001482 }
1483
1484 /**
Michael Kolbc831b632011-05-11 09:30:34 -07001485 * If this Tab was created through another Tab, then this method returns
1486 * that Tab.
1487 * @return the Tab parent or null
1488 */
1489 public Tab getParent() {
1490 return mParent;
1491 }
1492
1493 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001494 * When a Tab is created through the content of another Tab, then we
1495 * associate the Tabs.
1496 * @param child the Tab that was created from this Tab
1497 */
1498 void addChildTab(Tab child) {
Michael Kolbc831b632011-05-11 09:30:34 -07001499 if (mChildren == null) {
1500 mChildren = new Vector<Tab>();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001501 }
Michael Kolbc831b632011-05-11 09:30:34 -07001502 mChildren.add(child);
1503 child.setParent(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001504 }
1505
Michael Kolbc831b632011-05-11 09:30:34 -07001506 Vector<Tab> getChildren() {
1507 return mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001508 }
1509
1510 void resume() {
1511 if (mMainView != null) {
John Reck56c1fcf2011-08-17 10:15:16 -07001512 setupHwAcceleration(mMainView);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001513 mMainView.onResume();
1514 if (mSubView != null) {
1515 mSubView.onResume();
1516 }
1517 }
1518 }
1519
John Reck56c1fcf2011-08-17 10:15:16 -07001520 private void setupHwAcceleration(View web) {
1521 if (web == null) return;
1522 BrowserSettings settings = BrowserSettings.getInstance();
1523 if (settings.isHardwareAccelerated()) {
1524 web.setLayerType(View.LAYER_TYPE_NONE, null);
1525 } else {
1526 web.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
1527 }
1528 }
1529
Grace Kloba22ac16e2009-10-07 18:00:23 -07001530 void pause() {
1531 if (mMainView != null) {
1532 mMainView.onPause();
1533 if (mSubView != null) {
1534 mSubView.onPause();
1535 }
1536 }
1537 }
1538
1539 void putInForeground() {
John Reck8ee633f2011-08-09 16:00:35 -07001540 if (mInForeground) {
1541 return;
1542 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001543 mInForeground = true;
1544 resume();
Michael Kolb14612442011-06-24 13:06:29 -07001545 Activity activity = mWebViewController.getActivity();
1546 mMainView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001547 if (mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -07001548 mSubView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001549 }
1550 // Show the pending error dialog if the queue is not empty
1551 if (mQueuedErrors != null && mQueuedErrors.size() > 0) {
1552 showError(mQueuedErrors.getFirst());
1553 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001554 mWebViewController.bookmarkedStatusHasChanged(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001555 }
1556
1557 void putInBackground() {
John Reck8ee633f2011-08-09 16:00:35 -07001558 if (!mInForeground) {
1559 return;
1560 }
John Reck52be4782011-08-26 15:37:29 -07001561 capture();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001562 mInForeground = false;
1563 pause();
1564 mMainView.setOnCreateContextMenuListener(null);
1565 if (mSubView != null) {
1566 mSubView.setOnCreateContextMenuListener(null);
1567 }
1568 }
1569
Michael Kolb8233fac2010-10-26 16:08:53 -07001570 boolean inForeground() {
1571 return mInForeground;
1572 }
1573
Grace Kloba22ac16e2009-10-07 18:00:23 -07001574 /**
1575 * Return the top window of this tab; either the subwindow if it is not
1576 * null or the main window.
1577 * @return The top window of this tab.
1578 */
1579 WebView getTopWindow() {
1580 if (mSubView != null) {
1581 return mSubView;
1582 }
1583 return mMainView;
1584 }
1585
1586 /**
1587 * Return the main window of this tab. Note: if a tab is freed in the
1588 * background, this can return null. It is only guaranteed to be
1589 * non-null for the current tab.
1590 * @return The main WebView of this tab.
1591 */
1592 WebView getWebView() {
1593 return mMainView;
1594 }
1595
Michael Kolba713ec82010-11-29 17:27:06 -08001596 void setViewContainer(View container) {
1597 mContainer = container;
1598 }
1599
Michael Kolb8233fac2010-10-26 16:08:53 -07001600 View getViewContainer() {
1601 return mContainer;
1602 }
1603
Grace Kloba22ac16e2009-10-07 18:00:23 -07001604 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001605 * Return whether private browsing is enabled for the main window of
1606 * this tab.
1607 * @return True if private browsing is enabled.
1608 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001609 boolean isPrivateBrowsingEnabled() {
John Reck502a3532011-08-16 14:21:46 -07001610 return mCurrentState.mIncognito;
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001611 }
1612
1613 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001614 * Return the subwindow of this tab or null if there is no subwindow.
1615 * @return The subwindow of this tab or null.
1616 */
1617 WebView getSubWebView() {
1618 return mSubView;
1619 }
1620
Michael Kolb1514bb72010-11-22 09:11:48 -08001621 void setSubWebView(WebView subView) {
1622 mSubView = subView;
1623 }
1624
Michael Kolb8233fac2010-10-26 16:08:53 -07001625 View getSubViewContainer() {
1626 return mSubViewContainer;
1627 }
1628
Michael Kolb1514bb72010-11-22 09:11:48 -08001629 void setSubViewContainer(View subViewContainer) {
1630 mSubViewContainer = subViewContainer;
1631 }
1632
Grace Kloba22ac16e2009-10-07 18:00:23 -07001633 /**
1634 * @return The geolocation permissions prompt for this tab.
1635 */
1636 GeolocationPermissionsPrompt getGeolocationPermissionsPrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001637 if (mGeolocationPermissionsPrompt == null) {
1638 ViewStub stub = (ViewStub) mContainer
1639 .findViewById(R.id.geolocation_permissions_prompt);
1640 mGeolocationPermissionsPrompt = (GeolocationPermissionsPrompt) stub
1641 .inflate();
Grace Kloba50c241e2010-04-20 11:07:50 -07001642 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001643 return mGeolocationPermissionsPrompt;
1644 }
1645
1646 /**
1647 * @return The application id string
1648 */
1649 String getAppId() {
1650 return mAppId;
1651 }
1652
1653 /**
1654 * Set the application id string
1655 * @param id
1656 */
1657 void setAppId(String id) {
1658 mAppId = id;
1659 }
1660
Michael Kolbe28b3472011-08-04 16:54:31 -07001661 boolean closeOnBack() {
1662 return mCloseOnBack;
1663 }
1664
1665 void setCloseOnBack(boolean close) {
1666 mCloseOnBack = close;
1667 }
1668
Grace Kloba22ac16e2009-10-07 18:00:23 -07001669 String getUrl() {
John Reck324d4402011-01-11 16:56:42 -08001670 return UrlUtils.filteredUrl(mCurrentState.mUrl);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001671 }
1672
John Reck49a603c2011-03-03 09:33:05 -08001673 String getOriginalUrl() {
John Reckdb22ec42011-06-29 11:31:24 -07001674 if (mCurrentState.mOriginalUrl == null) {
1675 return getUrl();
John Reck49a603c2011-03-03 09:33:05 -08001676 }
John Reckdb22ec42011-06-29 11:31:24 -07001677 return UrlUtils.filteredUrl(mCurrentState.mOriginalUrl);
John Reck49a603c2011-03-03 09:33:05 -08001678 }
1679
Grace Kloba22ac16e2009-10-07 18:00:23 -07001680 /**
John Reck30c714c2010-12-16 17:30:34 -08001681 * Get the title of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001682 */
1683 String getTitle() {
John Reck30c714c2010-12-16 17:30:34 -08001684 if (mCurrentState.mTitle == null && mInPageLoad) {
Michael Kolb14612442011-06-24 13:06:29 -07001685 return mContext.getString(R.string.title_bar_loading);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001686 }
John Reck30c714c2010-12-16 17:30:34 -08001687 return mCurrentState.mTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001688 }
1689
1690 /**
John Reck30c714c2010-12-16 17:30:34 -08001691 * Get the favicon of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001692 */
1693 Bitmap getFavicon() {
John Reck1cf4b792011-07-26 10:22:22 -07001694 if (mCurrentState.mFavicon != null) {
1695 return mCurrentState.mFavicon;
1696 }
1697 return getDefaultFavicon(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001698 }
1699
John Recke969cc52010-12-21 17:24:43 -08001700 public boolean isBookmarkedSite() {
1701 return mCurrentState.mIsBookmarkedSite;
1702 }
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001703
Grace Kloba22ac16e2009-10-07 18:00:23 -07001704 /**
1705 * Return the tab's error console. Creates the console if createIfNEcessary
1706 * is true and we haven't already created the console.
1707 * @param createIfNecessary Flag to indicate if the console should be
1708 * created if it has not been already.
1709 * @return The tab's error console, or null if one has not been created and
1710 * createIfNecessary is false.
1711 */
1712 ErrorConsoleView getErrorConsole(boolean createIfNecessary) {
1713 if (createIfNecessary && mErrorConsole == null) {
Michael Kolb14612442011-06-24 13:06:29 -07001714 mErrorConsole = new ErrorConsoleView(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001715 mErrorConsole.setWebView(mMainView);
1716 }
1717 return mErrorConsole;
1718 }
1719
Steve Block08a6f0c2011-10-06 12:12:53 +01001720 /**
1721 * Sets the security state, clears the SSL certificate error and informs
1722 * the controller.
1723 */
Steve Block2466eff2011-10-03 15:33:09 +01001724 private void setSecurityState(SecurityState securityState) {
1725 mCurrentState.mSecurityState = securityState;
Steve Block08a6f0c2011-10-06 12:12:53 +01001726 mCurrentState.mSslCertificateError = null;
Steve Block2466eff2011-10-03 15:33:09 +01001727 mWebViewController.onUpdatedSecurityState(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001728 }
1729
1730 /**
Steve Block2466eff2011-10-03 15:33:09 +01001731 * @return The tab's security state.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001732 */
Steve Block2466eff2011-10-03 15:33:09 +01001733 SecurityState getSecurityState() {
1734 return mCurrentState.mSecurityState;
John Reck30c714c2010-12-16 17:30:34 -08001735 }
1736
Steve Block08a6f0c2011-10-06 12:12:53 +01001737 /**
1738 * Gets the SSL certificate error, if any, for the page's main resource.
1739 * This is only non-null when the security state is
1740 * SECURITY_STATE_BAD_CERTIFICATE.
1741 */
1742 SslError getSslCertificateError() {
1743 return mCurrentState.mSslCertificateError;
1744 }
1745
John Reck30c714c2010-12-16 17:30:34 -08001746 int getLoadProgress() {
1747 if (mInPageLoad) {
1748 return mPageLoadProgress;
1749 }
1750 return 100;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001751 }
1752
1753 /**
1754 * @return TRUE if onPageStarted is called while onPageFinished is not
1755 * called yet.
1756 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001757 boolean inPageLoad() {
1758 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001759 }
1760
Grace Kloba22ac16e2009-10-07 18:00:23 -07001761 /**
John Reck1cf4b792011-07-26 10:22:22 -07001762 * @return The Bundle with the tab's state if it can be saved, otherwise null
Grace Kloba22ac16e2009-10-07 18:00:23 -07001763 */
John Reck1cf4b792011-07-26 10:22:22 -07001764 public Bundle saveState() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001765 // If the WebView is null it means we ran low on memory and we already
1766 // stored the saved state in mSavedState.
1767 if (mMainView == null) {
John Reck1cf4b792011-07-26 10:22:22 -07001768 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001769 }
John Reck6c2e2f32011-08-22 13:41:23 -07001770
1771 if (TextUtils.isEmpty(mCurrentState.mUrl)) {
John Reck1cf4b792011-07-26 10:22:22 -07001772 return null;
John Reck24f18262011-06-17 14:47:20 -07001773 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001774
1775 mSavedState = new Bundle();
John Reck6c2e2f32011-08-22 13:41:23 -07001776 WebBackForwardList savedList = mMainView.saveState(mSavedState);
1777 if (savedList == null || savedList.getSize() == 0) {
1778 Log.w(LOGTAG, "Failed to save back/forward list for "
1779 + mCurrentState.mUrl);
1780 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001781
Michael Kolbc831b632011-05-11 09:30:34 -07001782 mSavedState.putLong(ID, mId);
John Reck30c714c2010-12-16 17:30:34 -08001783 mSavedState.putString(CURRURL, mCurrentState.mUrl);
1784 mSavedState.putString(CURRTITLE, mCurrentState.mTitle);
John Reck1cf4b792011-07-26 10:22:22 -07001785 mSavedState.putBoolean(INCOGNITO, mMainView.isPrivateBrowsingEnabled());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001786 if (mAppId != null) {
1787 mSavedState.putString(APPID, mAppId);
1788 }
Michael Kolbe28b3472011-08-04 16:54:31 -07001789 mSavedState.putBoolean(CLOSEFLAG, mCloseOnBack);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001790 // Remember the parent tab so the relationship can be restored.
Michael Kolbc831b632011-05-11 09:30:34 -07001791 if (mParent != null) {
1792 mSavedState.putLong(PARENTTAB, mParent.mId);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001793 }
John Reckb0a86db2011-05-24 14:05:58 -07001794 mSavedState.putBoolean(USERAGENT,
1795 mSettings.hasDesktopUseragent(getWebView()));
John Reck1cf4b792011-07-26 10:22:22 -07001796 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001797 }
1798
1799 /*
1800 * Restore the state of the tab.
1801 */
John Reck1cf4b792011-07-26 10:22:22 -07001802 private void restoreState(Bundle b) {
1803 mSavedState = b;
1804 if (mSavedState == null) {
1805 return;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001806 }
1807 // Restore the internal state even if the WebView fails to restore.
1808 // This will maintain the app id, original url and close-on-exit values.
Michael Kolbc831b632011-05-11 09:30:34 -07001809 mId = b.getLong(ID);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001810 mAppId = b.getString(APPID);
Michael Kolbe28b3472011-08-04 16:54:31 -07001811 mCloseOnBack = b.getBoolean(CLOSEFLAG);
John Reck8b9bb8b2012-03-08 13:19:40 -08001812 restoreUserAgent();
John Reck1cf4b792011-07-26 10:22:22 -07001813 String url = b.getString(CURRURL);
1814 String title = b.getString(CURRTITLE);
1815 boolean incognito = b.getBoolean(INCOGNITO);
1816 mCurrentState = new PageState(mContext, incognito, url, null);
1817 mCurrentState.mTitle = title;
1818 synchronized (Tab.this) {
1819 if (mCapture != null) {
John Reck4eadc342011-10-31 14:04:10 -07001820 DataController.getInstance(mContext).loadThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07001821 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001822 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001823 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04001824
John Reck8b9bb8b2012-03-08 13:19:40 -08001825 private void restoreUserAgent() {
1826 if (mMainView == null || mSavedState == null) {
1827 return;
1828 }
1829 if (mSavedState.getBoolean(USERAGENT)
1830 != mSettings.hasDesktopUseragent(mMainView)) {
1831 mSettings.toggleDesktopUseragent(mMainView);
1832 }
1833 }
1834
Leon Scroggins1961ed22010-12-07 15:22:21 -05001835 public void updateBookmarkedStatus() {
John Recke969cc52010-12-21 17:24:43 -08001836 mDataController.queryBookmarkStatus(getUrl(), mIsBookmarkCallback);
Leon Scroggins1961ed22010-12-07 15:22:21 -05001837 }
1838
John Recke969cc52010-12-21 17:24:43 -08001839 private DataController.OnQueryUrlIsBookmark mIsBookmarkCallback
1840 = new DataController.OnQueryUrlIsBookmark() {
1841 @Override
1842 public void onQueryUrlIsBookmark(String url, boolean isBookmark) {
1843 if (mCurrentState.mUrl.equals(url)) {
1844 mCurrentState.mIsBookmarkedSite = isBookmark;
1845 mWebViewController.bookmarkedStatusHasChanged(Tab.this);
1846 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001847 }
John Recke969cc52010-12-21 17:24:43 -08001848 };
Michael Kolb1acef692011-03-08 14:12:06 -08001849
Michael Kolbeb95db42011-03-03 10:38:40 -08001850 public Bitmap getScreenshot() {
John Reck1cf4b792011-07-26 10:22:22 -07001851 synchronized (Tab.this) {
1852 return mCapture;
1853 }
Michael Kolbeb95db42011-03-03 10:38:40 -08001854 }
1855
John Reck541f55a2011-06-07 16:34:43 -07001856 public boolean isSnapshot() {
John Reck541f55a2011-06-07 16:34:43 -07001857 return false;
1858 }
1859
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001860 private static class SaveCallback implements ValueCallback<String> {
1861 boolean onReceiveValueCalled = false;
1862 private String mPath;
John Reck68234a92012-04-19 15:27:12 -07001863
1864 @Override
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001865 public void onReceiveValue(String path) {
1866 this.onReceiveValueCalled = true;
1867 this.mPath = path;
John Reck68234a92012-04-19 15:27:12 -07001868 synchronized (this) {
1869 notifyAll();
John Reck8cc92352011-07-06 17:41:52 -07001870 }
John Reck541f55a2011-06-07 16:34:43 -07001871 }
John Reck68234a92012-04-19 15:27:12 -07001872
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001873 public String getPath() {
1874 return mPath;
1875 }
John Reck68234a92012-04-19 15:27:12 -07001876 }
1877
1878 /**
1879 * Must be called on the UI thread
1880 */
1881 public ContentValues createSnapshotValues() {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001882 WebView web = getWebView();
John Reck68234a92012-04-19 15:27:12 -07001883 if (web == null) return null;
John Reckd8c74522011-06-14 08:45:00 -07001884 ContentValues values = new ContentValues();
1885 values.put(Snapshots.TITLE, mCurrentState.mTitle);
1886 values.put(Snapshots.URL, mCurrentState.mUrl);
John Reck68234a92012-04-19 15:27:12 -07001887 values.put(Snapshots.BACKGROUND, web.getPageBackgroundColor());
John Reck8cc92352011-07-06 17:41:52 -07001888 values.put(Snapshots.DATE_CREATED, System.currentTimeMillis());
1889 values.put(Snapshots.FAVICON, compressBitmap(getFavicon()));
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001890 Bitmap screenshot = web.getViewportBitmap();
John Reck8cc92352011-07-06 17:41:52 -07001891 values.put(Snapshots.THUMBNAIL, compressBitmap(screenshot));
John Reckd8c74522011-06-14 08:45:00 -07001892 return values;
John Reck541f55a2011-06-07 16:34:43 -07001893 }
1894
John Reck68234a92012-04-19 15:27:12 -07001895 /**
1896 * Probably want to call this on a background thread
1897 */
1898 public boolean saveViewState(ContentValues values) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001899 WebView web = getWebView();
John Reck68234a92012-04-19 15:27:12 -07001900 if (web == null) return false;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001901 String filename = UUID.randomUUID().toString();
John Reck68234a92012-04-19 15:27:12 -07001902 SaveCallback callback = new SaveCallback();
John Reck68234a92012-04-19 15:27:12 -07001903 try {
John Reck68234a92012-04-19 15:27:12 -07001904 synchronized (callback) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001905 web.saveViewState(filename, callback);
1906 callback.wait();
John Reck68234a92012-04-19 15:27:12 -07001907 }
John Reck68234a92012-04-19 15:27:12 -07001908 } catch (Exception e) {
1909 Log.w(LOGTAG, "Failed to save view state", e);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001910 String path = callback.getPath();
1911 if (path != null) {
1912 File file = mContext.getFileStreamPath(path);
1913 if (file.exists() && !file.delete()) {
1914 file.deleteOnExit();
1915 }
John Reck68234a92012-04-19 15:27:12 -07001916 }
1917 return false;
1918 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001919 String path = callback.getPath();
1920 File savedFile = new File(path);
1921 if (!savedFile.exists()) {
1922 return false;
John Reck68234a92012-04-19 15:27:12 -07001923 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001924 values.put(Snapshots.VIEWSTATE_PATH, path.substring(path.lastIndexOf('/') + 1));
1925 values.put(Snapshots.VIEWSTATE_SIZE, savedFile.length());
John Reck68234a92012-04-19 15:27:12 -07001926 return true;
1927 }
1928
John Reck8cc92352011-07-06 17:41:52 -07001929 public byte[] compressBitmap(Bitmap bitmap) {
1930 if (bitmap == null) {
1931 return null;
1932 }
1933 ByteArrayOutputStream stream = new ByteArrayOutputStream();
1934 bitmap.compress(CompressFormat.PNG, 100, stream);
1935 return stream.toByteArray();
1936 }
1937
John Reck26b18322011-06-21 13:08:58 -07001938 public void loadUrl(String url, Map<String, String> headers) {
1939 if (mMainView != null) {
Michael Kolba53c9892011-10-05 13:31:40 -07001940 mPageLoadProgress = INITIAL_PROGRESS;
1941 mInPageLoad = true;
Michael Kolb14612442011-06-24 13:06:29 -07001942 mCurrentState = new PageState(mContext, false, url, null);
John Reck26b18322011-06-21 13:08:58 -07001943 mWebViewController.onPageStarted(this, mMainView, null);
1944 mMainView.loadUrl(url, headers);
1945 }
1946 }
1947
John Reck38b39652012-06-05 09:22:59 -07001948 public void disableUrlOverridingForLoad() {
1949 mDisableOverrideUrlLoading = true;
1950 }
1951
Michael Kolb9ef259a2011-07-12 15:33:08 -07001952 protected void capture() {
1953 if (mMainView == null || mCapture == null) return;
John Reck4eadc342011-10-31 14:04:10 -07001954 if (mMainView.getContentWidth() <= 0 || mMainView.getContentHeight() <= 0) {
1955 return;
1956 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001957 Canvas c = new Canvas(mCapture);
Michael Kolba3194d02011-09-07 11:23:51 -07001958 int state = c.save();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001959 Bitmap screenShot = mMainView.getViewportBitmap();
1960 if (screenShot != null) {
1961 mCapture.eraseColor(Color.WHITE);
1962 float scale = (float) mCaptureWidth / screenShot.getWidth();
1963 c.scale(scale, scale);
1964 c.drawBitmap(screenShot, 0, 0, null);
1965 } else {
1966 final int left = mMainView.getViewScrollX();
1967 final int top = mMainView.getViewScrollY() + mMainView.getVisibleTitleHeight();
1968 c.translate(-left, -top);
1969 float scale = mCaptureWidth / (float) mMainView.getWidth();
1970 c.scale(scale, scale, left, top);
1971 if (mMainView instanceof BrowserWebView) {
1972 ((BrowserWebView)mMainView).drawContent(c);
1973 } else {
1974 mMainView.draw(c);
1975 }
John Reck8ee633f2011-08-09 16:00:35 -07001976 }
Michael Kolba3194d02011-09-07 11:23:51 -07001977 c.restoreToCount(state);
1978 // manually anti-alias the edges for the tilt
1979 c.drawRect(0, 0, 1, mCapture.getHeight(), sAlphaPaint);
1980 c.drawRect(mCapture.getWidth() - 1, 0, mCapture.getWidth(),
1981 mCapture.getHeight(), sAlphaPaint);
1982 c.drawRect(0, 0, mCapture.getWidth(), 1, sAlphaPaint);
1983 c.drawRect(0, mCapture.getHeight() - 1, mCapture.getWidth(),
1984 mCapture.getHeight(), sAlphaPaint);
Dianne Hackborn43cfe8a2011-08-02 16:59:35 -07001985 c.setBitmap(null);
John Reck8ee633f2011-08-09 16:00:35 -07001986 mHandler.removeMessages(MSG_CAPTURE);
John Reck1cf4b792011-07-26 10:22:22 -07001987 persistThumbnail();
John Reck8ee633f2011-08-09 16:00:35 -07001988 TabControl tc = mWebViewController.getTabControl();
1989 if (tc != null) {
1990 OnThumbnailUpdatedListener updateListener
1991 = tc.getOnThumbnailUpdatedListener();
1992 if (updateListener != null) {
1993 updateListener.onThumbnailUpdated(this);
1994 }
1995 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001996 }
1997
1998 @Override
1999 public void onNewPicture(WebView view, Picture picture) {
John Reck1cf4b792011-07-26 10:22:22 -07002000 postCapture();
2001 }
2002
2003 private void postCapture() {
Michael Kolb9ef259a2011-07-12 15:33:08 -07002004 if (!mHandler.hasMessages(MSG_CAPTURE)) {
2005 mHandler.sendEmptyMessageDelayed(MSG_CAPTURE, CAPTURE_DELAY);
2006 }
2007 }
2008
John Reckef654f12011-07-12 16:42:08 -07002009 public boolean canGoBack() {
2010 return mMainView != null ? mMainView.canGoBack() : false;
2011 }
2012
2013 public boolean canGoForward() {
2014 return mMainView != null ? mMainView.canGoForward() : false;
2015 }
2016
2017 public void goBack() {
2018 if (mMainView != null) {
2019 mMainView.goBack();
2020 }
2021 }
2022
2023 public void goForward() {
2024 if (mMainView != null) {
2025 mMainView.goForward();
2026 }
2027 }
2028
Mathew Inwood1dd8e822011-08-03 14:34:29 +01002029 /**
2030 * Causes the tab back/forward stack to be cleared once, if the given URL is the next URL
2031 * to be added to the stack.
2032 *
2033 * This is used to ensure that preloaded URLs that are not subsequently seen by the user do
2034 * not appear in the back stack.
2035 */
Mathew Inwooda829d552011-09-02 14:16:25 +01002036 public void clearBackStackWhenItemAdded(Pattern urlPattern) {
2037 mClearHistoryUrlPattern = urlPattern;
Mathew Inwood1dd8e822011-08-03 14:34:29 +01002038 }
2039
John Reck1cf4b792011-07-26 10:22:22 -07002040 protected void persistThumbnail() {
John Reck4eadc342011-10-31 14:04:10 -07002041 DataController.getInstance(mContext).saveThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07002042 }
2043
2044 protected void deleteThumbnail() {
John Reck4eadc342011-10-31 14:04:10 -07002045 DataController.getInstance(mContext).deleteThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07002046 }
2047
John Reck4eadc342011-10-31 14:04:10 -07002048 void updateCaptureFromBlob(byte[] blob) {
John Reck1cf4b792011-07-26 10:22:22 -07002049 synchronized (Tab.this) {
2050 if (mCapture == null) {
2051 return;
2052 }
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002053 ByteBuffer buffer = ByteBuffer.wrap(blob);
Michael Kolbd837a112011-08-09 14:00:18 -07002054 try {
2055 mCapture.copyPixelsFromBuffer(buffer);
2056 } catch (RuntimeException rex) {
2057 Log.e(LOGTAG, "Load capture has mismatched sizes; buffer: "
2058 + buffer.capacity() + " blob: " + blob.length
2059 + "capture: " + mCapture.getByteCount());
2060 throw rex;
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002061 }
John Reck1cf4b792011-07-26 10:22:22 -07002062 }
2063 }
2064
John Reck52be4782011-08-26 15:37:29 -07002065 @Override
2066 public String toString() {
2067 StringBuilder builder = new StringBuilder(100);
2068 builder.append(mId);
2069 builder.append(") has parent: ");
2070 if (getParent() != null) {
2071 builder.append("true[");
2072 builder.append(getParent().getId());
2073 builder.append("]");
2074 } else {
2075 builder.append("false");
2076 }
2077 builder.append(", incog: ");
2078 builder.append(isPrivateBrowsingEnabled());
2079 if (!isPrivateBrowsingEnabled()) {
2080 builder.append(", title: ");
2081 builder.append(getTitle());
2082 builder.append(", url: ");
2083 builder.append(getUrl());
2084 }
2085 return builder.toString();
2086 }
2087
Steve Block4895b012011-10-03 16:26:46 +01002088 private void handleProceededAfterSslError(SslError error) {
2089 if (error.getUrl().equals(mCurrentState.mUrl)) {
2090 // The security state should currently be SECURITY_STATE_SECURE.
2091 setSecurityState(SecurityState.SECURITY_STATE_BAD_CERTIFICATE);
Steve Block08a6f0c2011-10-06 12:12:53 +01002092 mCurrentState.mSslCertificateError = error;
Steve Block4895b012011-10-03 16:26:46 +01002093 } else if (getSecurityState() == SecurityState.SECURITY_STATE_SECURE) {
Steve Block08a6f0c2011-10-06 12:12:53 +01002094 // The page's main resource is secure and this error is for a
2095 // sub-resource.
Steve Block4895b012011-10-03 16:26:46 +01002096 setSecurityState(SecurityState.SECURITY_STATE_MIXED);
2097 }
2098 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002099}