blob: 1b0b68c423d7d630436d7a89a01a3f3b59999ce4 [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
Vivek Sekharb54614f2014-05-01 19:03:37 -07001033 @Override
1034 public void showFileChooser(ValueCallback<String[]> uploadFilePaths, String acceptTypes,
1035 boolean capture) {
1036 if (mInForeground) {
1037 mWebViewController.showFileChooser(uploadFilePaths, acceptTypes, capture);
1038 } else {
1039 uploadFilePaths.onReceiveValue(null);
1040 }
1041 }
1042
Grace Kloba22ac16e2009-10-07 18:00:23 -07001043 /**
1044 * Deliver a list of already-visited URLs
1045 */
1046 @Override
1047 public void getVisitedHistory(final ValueCallback<String[]> callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001048 mWebViewController.getVisitedHistory(callback);
1049 }
Ben Murdoch8029a772010-11-16 11:58:21 +00001050
1051 @Override
1052 public void setupAutoFill(Message message) {
1053 // Prompt the user to set up their profile.
1054 final Message msg = message;
Michael Kolb14612442011-06-24 13:06:29 -07001055 AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
1056 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
Ben Murdoch1d676b62011-01-17 12:54:24 +00001057 Context.LAYOUT_INFLATER_SERVICE);
1058 final View layout = inflater.inflate(R.layout.setup_autofill_dialog, null);
1059
1060 builder.setView(layout)
1061 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1062 @Override
1063 public void onClick(DialogInterface dialog, int id) {
1064 CheckBox disableAutoFill = (CheckBox) layout.findViewById(
1065 R.id.setup_autofill_dialog_disable_autofill);
1066
1067 if (disableAutoFill.isChecked()) {
1068 // Disable autofill and show a toast with how to turn it on again.
John Reck35e9dd62011-04-25 09:01:54 -07001069 mSettings.setAutofillEnabled(false);
Michael Kolb14612442011-06-24 13:06:29 -07001070 Toast.makeText(mContext,
Ben Murdoch1d676b62011-01-17 12:54:24 +00001071 R.string.autofill_setup_dialog_negative_toast,
1072 Toast.LENGTH_LONG).show();
1073 } else {
1074 // Take user to the AutoFill profile editor. When they return,
1075 // we will send the message that we pass here which will trigger
1076 // the form to get filled out with their new profile.
1077 mWebViewController.setupAutoFill(msg);
1078 }
1079 }
1080 })
1081 .setNegativeButton(R.string.cancel, null)
1082 .show();
Ben Murdoch8029a772010-11-16 11:58:21 +00001083 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001084 };
1085
1086 // -------------------------------------------------------------------------
1087 // WebViewClient implementation for the sub window
1088 // -------------------------------------------------------------------------
1089
1090 // Subclass of WebViewClient used in subwindows to notify the main
1091 // WebViewClient of certain WebView activities.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001092 private static class SubWindowClient extends WebViewClient {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001093 // The main WebViewClient.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001094 private final WebViewClient mClient;
Michael Kolb8233fac2010-10-26 16:08:53 -07001095 private final WebViewController mController;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001096
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001097 SubWindowClient(WebViewClient client, WebViewController controller) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001098 mClient = client;
Michael Kolb8233fac2010-10-26 16:08:53 -07001099 mController = controller;
Leon Scroggins III211ba542010-04-19 13:21:13 -04001100 }
1101 @Override
1102 public void onPageStarted(WebView view, String url, Bitmap favicon) {
1103 // Unlike the others, do not call mClient's version, which would
1104 // change the progress bar. However, we do want to remove the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001105 // find or select dialog.
Michael Kolb8233fac2010-10-26 16:08:53 -07001106 mController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001107 }
1108 @Override
1109 public void doUpdateVisitedHistory(WebView view, String url,
1110 boolean isReload) {
1111 mClient.doUpdateVisitedHistory(view, url, isReload);
1112 }
1113 @Override
1114 public boolean shouldOverrideUrlLoading(WebView view, String url) {
1115 return mClient.shouldOverrideUrlLoading(view, url);
1116 }
1117 @Override
1118 public void onReceivedSslError(WebView view, SslErrorHandler handler,
1119 SslError error) {
1120 mClient.onReceivedSslError(view, handler, error);
1121 }
1122 @Override
Brian Carlstrom8862c1d2011-06-02 01:05:55 -07001123 public void onReceivedClientCertRequest(WebView view,
1124 ClientCertRequestHandler handler, String host_and_port) {
1125 mClient.onReceivedClientCertRequest(view, handler, host_and_port);
1126 }
1127 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -07001128 public void onReceivedHttpAuthRequest(WebView view,
1129 HttpAuthHandler handler, String host, String realm) {
1130 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
1131 }
1132 @Override
1133 public void onFormResubmission(WebView view, Message dontResend,
1134 Message resend) {
1135 mClient.onFormResubmission(view, dontResend, resend);
1136 }
1137 @Override
1138 public void onReceivedError(WebView view, int errorCode,
1139 String description, String failingUrl) {
1140 mClient.onReceivedError(view, errorCode, description, failingUrl);
1141 }
1142 @Override
1143 public boolean shouldOverrideKeyEvent(WebView view,
1144 android.view.KeyEvent event) {
1145 return mClient.shouldOverrideKeyEvent(view, event);
1146 }
1147 @Override
1148 public void onUnhandledKeyEvent(WebView view,
1149 android.view.KeyEvent event) {
1150 mClient.onUnhandledKeyEvent(view, event);
1151 }
1152 }
1153
1154 // -------------------------------------------------------------------------
1155 // WebChromeClient implementation for the sub window
1156 // -------------------------------------------------------------------------
1157
1158 private class SubWindowChromeClient extends WebChromeClient {
1159 // The main WebChromeClient.
1160 private final WebChromeClient mClient;
1161
1162 SubWindowChromeClient(WebChromeClient client) {
1163 mClient = client;
1164 }
1165 @Override
1166 public void onProgressChanged(WebView view, int newProgress) {
1167 mClient.onProgressChanged(view, newProgress);
1168 }
1169 @Override
1170 public boolean onCreateWindow(WebView view, boolean dialog,
1171 boolean userGesture, android.os.Message resultMsg) {
1172 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
1173 }
1174 @Override
1175 public void onCloseWindow(WebView window) {
1176 if (window != mSubView) {
1177 Log.e(LOGTAG, "Can't close the window");
1178 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001179 mWebViewController.dismissSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001180 }
1181 }
1182
1183 // -------------------------------------------------------------------------
1184
1185 // Construct a new tab
Michael Kolb7bcafde2011-05-09 13:55:59 -07001186 Tab(WebViewController wvcontroller, WebView w) {
John Reck1cf4b792011-07-26 10:22:22 -07001187 this(wvcontroller, w, null);
1188 }
1189
1190 Tab(WebViewController wvcontroller, Bundle state) {
1191 this(wvcontroller, null, state);
1192 }
1193
1194 Tab(WebViewController wvcontroller, WebView w, Bundle state) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001195 mWebViewController = wvcontroller;
Michael Kolb14612442011-06-24 13:06:29 -07001196 mContext = mWebViewController.getContext();
John Reck35e9dd62011-04-25 09:01:54 -07001197 mSettings = BrowserSettings.getInstance();
Michael Kolb14612442011-06-24 13:06:29 -07001198 mDataController = DataController.getInstance(mContext);
1199 mCurrentState = new PageState(mContext, w != null
John Reck847b5322011-04-14 17:02:18 -07001200 ? w.isPrivateBrowsingEnabled() : false);
Michael Kolb8233fac2010-10-26 16:08:53 -07001201 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001202 mInForeground = false;
1203
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001204 mDownloadListener = new BrowserDownloadListener() {
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001205 public void onDownloadStart(String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001206 String contentDisposition, String mimetype, String referer,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001207 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001208 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001209 mimetype, referer, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001210 }
1211 };
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001212 mWebBackForwardListClient = new WebBackForwardListClient() {
1213 @Override
1214 public void onNewHistoryItem(WebHistoryItem item) {
Mathew Inwooda829d552011-09-02 14:16:25 +01001215 if (mClearHistoryUrlPattern != null) {
1216 boolean match =
1217 mClearHistoryUrlPattern.matcher(item.getOriginalUrl()).matches();
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001218 if (LOGD_ENABLED) {
Mathew Inwooda829d552011-09-02 14:16:25 +01001219 Log.d(LOGTAG, "onNewHistoryItem: match=" + match + "\n\t"
1220 + item.getUrl() + "\n\t"
1221 + mClearHistoryUrlPattern);
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001222 }
Mathew Inwooda829d552011-09-02 14:16:25 +01001223 if (match) {
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001224 if (mMainView != null) {
1225 mMainView.clearHistory();
1226 }
1227 }
Mathew Inwooda829d552011-09-02 14:16:25 +01001228 mClearHistoryUrlPattern = null;
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001229 }
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001230 }
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001231 };
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001232
John Reck1cf4b792011-07-26 10:22:22 -07001233 mCaptureWidth = mContext.getResources().getDimensionPixelSize(
1234 R.dimen.tab_thumbnail_width);
1235 mCaptureHeight = mContext.getResources().getDimensionPixelSize(
1236 R.dimen.tab_thumbnail_height);
1237 updateShouldCaptureThumbnails();
1238 restoreState(state);
John Reck52be4782011-08-26 15:37:29 -07001239 if (getId() == -1) {
1240 mId = TabControl.getNextId();
1241 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001242 setWebView(w);
Michael Kolb9ef259a2011-07-12 15:33:08 -07001243 mHandler = new Handler() {
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001244 @Override
Michael Kolb9ef259a2011-07-12 15:33:08 -07001245 public void handleMessage(Message m) {
John Reck1cf4b792011-07-26 10:22:22 -07001246 switch (m.what) {
1247 case MSG_CAPTURE:
1248 capture();
1249 break;
1250 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001251 }
1252 };
John Reck1cf4b792011-07-26 10:22:22 -07001253 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001254
Michael Kolb72864272012-05-03 15:42:15 -07001255 public boolean shouldUpdateThumbnail() {
1256 return mUpdateThumbnail;
1257 }
1258
Mathew Inwoode09305e2011-09-02 12:03:26 +01001259 /**
1260 * This is used to get a new ID when the tab has been preloaded, before it is displayed and
1261 * added to TabControl. Preloaded tabs can be created before restoreInstanceState, leading
1262 * to overlapping IDs between the preloaded and restored tabs.
1263 */
1264 public void refreshIdAfterPreload() {
1265 mId = TabControl.getNextId();
1266 }
1267
John Reck1cf4b792011-07-26 10:22:22 -07001268 public void updateShouldCaptureThumbnails() {
1269 if (mWebViewController.shouldCaptureThumbnails()) {
1270 synchronized (Tab.this) {
1271 if (mCapture == null) {
1272 mCapture = Bitmap.createBitmap(mCaptureWidth, mCaptureHeight,
1273 Bitmap.Config.RGB_565);
Michael Kolbc3af0672011-08-09 10:24:41 -07001274 mCapture.eraseColor(Color.WHITE);
John Reck1cf4b792011-07-26 10:22:22 -07001275 if (mInForeground) {
1276 postCapture();
1277 }
1278 }
1279 }
1280 } else {
1281 synchronized (Tab.this) {
1282 mCapture = null;
1283 deleteThumbnail();
1284 }
1285 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001286 }
1287
Michael Kolb14612442011-06-24 13:06:29 -07001288 public void setController(WebViewController ctl) {
1289 mWebViewController = ctl;
John Reck1cf4b792011-07-26 10:22:22 -07001290 updateShouldCaptureThumbnails();
Michael Kolb14612442011-06-24 13:06:29 -07001291 }
1292
Michael Kolbc831b632011-05-11 09:30:34 -07001293 public long getId() {
1294 return mId;
1295 }
1296
Michael Kolb91911a22012-01-17 11:21:25 -08001297 void setWebView(WebView w) {
1298 setWebView(w, true);
1299 }
1300
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001301 public boolean isNativeActive(){
1302 if (mMainView == null)
1303 return false;
1304 return true;
1305 }
1306
1307 public void setTimeStamp(){
1308 Date d = new Date();
1309 timestamp = (new Timestamp(d.getTime()));
1310 }
1311
1312 public Timestamp getTimestamp() {
1313 return timestamp;
1314 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001315 /**
1316 * Sets the WebView for this tab, correctly removing the old WebView from
1317 * the container view.
1318 */
Michael Kolb91911a22012-01-17 11:21:25 -08001319 void setWebView(WebView w, boolean restore) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001320 if (mMainView == w) {
1321 return;
1322 }
Michael Kolba713ec82010-11-29 17:27:06 -08001323
Grace Kloba22ac16e2009-10-07 18:00:23 -07001324 // If the WebView is changing, the page will be reloaded, so any ongoing
1325 // Geolocation permission requests are void.
Grace Kloba50c241e2010-04-20 11:07:50 -07001326 if (mGeolocationPermissionsPrompt != null) {
1327 mGeolocationPermissionsPrompt.hide();
1328 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001329
Michael Kolba713ec82010-11-29 17:27:06 -08001330 mWebViewController.onSetWebView(this, w);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001331
John Reck1cf4b792011-07-26 10:22:22 -07001332 if (mMainView != null) {
John Reck8ee633f2011-08-09 16:00:35 -07001333 mMainView.setPictureListener(null);
John Reck1cf4b792011-07-26 10:22:22 -07001334 if (w != null) {
1335 syncCurrentState(w, null);
1336 } else {
Panos Thomasa9a5a582014-03-18 19:20:08 -07001337 mCurrentState = new PageState(mContext, mMainView.isPrivateBrowsingEnabled());
John Reck1cf4b792011-07-26 10:22:22 -07001338 }
1339 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001340 // set the new one
1341 mMainView = w;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001342 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001343 if (mMainView != null) {
1344 mMainView.setWebViewClient(mWebViewClient);
1345 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001346 // Attach DownloadManager so that downloads can start in an active
1347 // or a non-active window. This can happen when going to a site that
1348 // does a redirect after a period of time. The user could have
1349 // switched to another tab while waiting for the download to start.
1350 mMainView.setDownloadListener(mDownloadListener);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001351 getWebView().setWebBackForwardListClient(mWebBackForwardListClient);
John Reck8ee633f2011-08-09 16:00:35 -07001352 TabControl tc = mWebViewController.getTabControl();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001353 if (tc != null /*&& tc.getOnThumbnailUpdatedListener() != null*/) {
John Reck8ee633f2011-08-09 16:00:35 -07001354 mMainView.setPictureListener(this);
1355 }
Michael Kolb91911a22012-01-17 11:21:25 -08001356 if (restore && (mSavedState != null)) {
John Reck8b9bb8b2012-03-08 13:19:40 -08001357 restoreUserAgent();
John Reck6c2e2f32011-08-22 13:41:23 -07001358 WebBackForwardList restoredState
1359 = mMainView.restoreState(mSavedState);
1360 if (restoredState == null || restoredState.getSize() == 0) {
1361 Log.w(LOGTAG, "Failed to restore WebView state!");
1362 loadUrl(mCurrentState.mOriginalUrl, null);
1363 }
John Reck1cf4b792011-07-26 10:22:22 -07001364 mSavedState = null;
1365 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001366 }
1367 }
1368
1369 /**
1370 * Destroy the tab's main WebView and subWindow if any
1371 */
1372 void destroy() {
Matthew Huib7f2e9c2014-04-16 11:12:37 -04001373 if (mPostponeDestroy) {
1374 mShouldDestroy = true;
1375 return;
1376 }
1377 mShouldDestroy = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001378 if (mMainView != null) {
1379 dismissSubWindow();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001380 // save the WebView to call destroy() after detach it from the tab
1381 WebView webView = mMainView;
1382 setWebView(null);
1383 webView.destroy();
1384 }
1385 }
1386
Matthew Huib7f2e9c2014-04-16 11:12:37 -04001387 private boolean mPostponeDestroy = false;
1388 private boolean mShouldDestroy = false;
1389
1390 public void postponeDestroy() {
1391 mPostponeDestroy = true;
1392 }
1393
1394 public void performPostponedDestroy() {
1395 mPostponeDestroy = false;
1396 if (mShouldDestroy) {
1397 destroy();
1398 }
1399 }
1400
Grace Kloba22ac16e2009-10-07 18:00:23 -07001401 /**
1402 * Remove the tab from the parent
1403 */
1404 void removeFromTree() {
1405 // detach the children
Michael Kolbc831b632011-05-11 09:30:34 -07001406 if (mChildren != null) {
1407 for(Tab t : mChildren) {
1408 t.setParent(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001409 }
1410 }
1411 // remove itself from the parent list
Michael Kolbc831b632011-05-11 09:30:34 -07001412 if (mParent != null) {
1413 mParent.mChildren.remove(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001414 }
John Reck1cf4b792011-07-26 10:22:22 -07001415 deleteThumbnail();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001416 }
1417
1418 /**
1419 * Create a new subwindow unless a subwindow already exists.
1420 * @return True if a new subwindow was created. False if one already exists.
1421 */
1422 boolean createSubWindow() {
1423 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001424 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001425 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001426 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001427 mSubView.setWebChromeClient(new SubWindowChromeClient(
1428 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001429 // Set a different DownloadListener for the mSubView, since it will
1430 // just need to dismiss the mSubView, rather than close the Tab
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001431 mSubView.setDownloadListener(new BrowserDownloadListener() {
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001432 public void onDownloadStart(String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001433 String contentDisposition, String mimetype, String referer,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001434 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001435 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001436 contentDisposition, mimetype, referer, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001437 if (mSubView.copyBackForwardList().getSize() == 0) {
1438 // This subwindow was opened for the sole purpose of
1439 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001440 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001441 }
1442 }
1443 });
Michael Kolb14612442011-06-24 13:06:29 -07001444 mSubView.setOnCreateContextMenuListener(mWebViewController.getActivity());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001445 return true;
1446 }
1447 return false;
1448 }
1449
1450 /**
1451 * Dismiss the subWindow for the tab.
1452 */
1453 void dismissSubWindow() {
1454 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001455 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001456 mSubView.destroy();
1457 mSubView = null;
1458 mSubViewContainer = null;
1459 }
1460 }
1461
Grace Kloba22ac16e2009-10-07 18:00:23 -07001462
1463 /**
1464 * Set the parent tab of this tab.
1465 */
Michael Kolbc831b632011-05-11 09:30:34 -07001466 void setParent(Tab parent) {
John Reck52be4782011-08-26 15:37:29 -07001467 if (parent == this) {
1468 throw new IllegalStateException("Cannot set parent to self!");
1469 }
Michael Kolbc831b632011-05-11 09:30:34 -07001470 mParent = parent;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001471 // This tab may have been freed due to low memory. If that is the case,
Michael Kolbc831b632011-05-11 09:30:34 -07001472 // the parent tab id is already saved. If we are changing that id
Grace Kloba22ac16e2009-10-07 18:00:23 -07001473 // (most likely due to removing the parent tab) we must update the
Michael Kolbc831b632011-05-11 09:30:34 -07001474 // parent tab id in the saved Bundle.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001475 if (mSavedState != null) {
1476 if (parent == null) {
1477 mSavedState.remove(PARENTTAB);
1478 } else {
Michael Kolbc831b632011-05-11 09:30:34 -07001479 mSavedState.putLong(PARENTTAB, parent.getId());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001480 }
1481 }
John Reckb0a86db2011-05-24 14:05:58 -07001482
1483 // Sync the WebView useragent with the parent
1484 if (parent != null && mSettings.hasDesktopUseragent(parent.getWebView())
1485 != mSettings.hasDesktopUseragent(getWebView())) {
1486 mSettings.toggleDesktopUseragent(getWebView());
1487 }
John Reck52be4782011-08-26 15:37:29 -07001488
1489 if (parent != null && parent.getId() == getId()) {
1490 throw new IllegalStateException("Parent has same ID as child!");
1491 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001492 }
1493
1494 /**
Michael Kolbc831b632011-05-11 09:30:34 -07001495 * If this Tab was created through another Tab, then this method returns
1496 * that Tab.
1497 * @return the Tab parent or null
1498 */
1499 public Tab getParent() {
1500 return mParent;
1501 }
1502
1503 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001504 * When a Tab is created through the content of another Tab, then we
1505 * associate the Tabs.
1506 * @param child the Tab that was created from this Tab
1507 */
1508 void addChildTab(Tab child) {
Michael Kolbc831b632011-05-11 09:30:34 -07001509 if (mChildren == null) {
1510 mChildren = new Vector<Tab>();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001511 }
Michael Kolbc831b632011-05-11 09:30:34 -07001512 mChildren.add(child);
1513 child.setParent(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001514 }
1515
Michael Kolbc831b632011-05-11 09:30:34 -07001516 Vector<Tab> getChildren() {
1517 return mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001518 }
1519
1520 void resume() {
1521 if (mMainView != null) {
John Reck56c1fcf2011-08-17 10:15:16 -07001522 setupHwAcceleration(mMainView);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001523 mMainView.onResume();
1524 if (mSubView != null) {
1525 mSubView.onResume();
1526 }
1527 }
1528 }
1529
John Reck56c1fcf2011-08-17 10:15:16 -07001530 private void setupHwAcceleration(View web) {
1531 if (web == null) return;
1532 BrowserSettings settings = BrowserSettings.getInstance();
1533 if (settings.isHardwareAccelerated()) {
1534 web.setLayerType(View.LAYER_TYPE_NONE, null);
1535 } else {
1536 web.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
1537 }
1538 }
1539
Grace Kloba22ac16e2009-10-07 18:00:23 -07001540 void pause() {
1541 if (mMainView != null) {
1542 mMainView.onPause();
1543 if (mSubView != null) {
1544 mSubView.onPause();
1545 }
1546 }
1547 }
1548
1549 void putInForeground() {
John Reck8ee633f2011-08-09 16:00:35 -07001550 if (mInForeground) {
1551 return;
1552 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001553 mInForeground = true;
1554 resume();
Michael Kolb14612442011-06-24 13:06:29 -07001555 Activity activity = mWebViewController.getActivity();
1556 mMainView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001557 if (mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -07001558 mSubView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001559 }
1560 // Show the pending error dialog if the queue is not empty
1561 if (mQueuedErrors != null && mQueuedErrors.size() > 0) {
1562 showError(mQueuedErrors.getFirst());
1563 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001564 mWebViewController.bookmarkedStatusHasChanged(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001565 }
1566
1567 void putInBackground() {
John Reck8ee633f2011-08-09 16:00:35 -07001568 if (!mInForeground) {
1569 return;
1570 }
John Reck52be4782011-08-26 15:37:29 -07001571 capture();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001572 mInForeground = false;
1573 pause();
1574 mMainView.setOnCreateContextMenuListener(null);
1575 if (mSubView != null) {
1576 mSubView.setOnCreateContextMenuListener(null);
1577 }
1578 }
1579
Michael Kolb8233fac2010-10-26 16:08:53 -07001580 boolean inForeground() {
1581 return mInForeground;
1582 }
1583
Grace Kloba22ac16e2009-10-07 18:00:23 -07001584 /**
1585 * Return the top window of this tab; either the subwindow if it is not
1586 * null or the main window.
1587 * @return The top window of this tab.
1588 */
1589 WebView getTopWindow() {
1590 if (mSubView != null) {
1591 return mSubView;
1592 }
1593 return mMainView;
1594 }
1595
1596 /**
1597 * Return the main window of this tab. Note: if a tab is freed in the
1598 * background, this can return null. It is only guaranteed to be
1599 * non-null for the current tab.
1600 * @return The main WebView of this tab.
1601 */
1602 WebView getWebView() {
1603 return mMainView;
1604 }
1605
Michael Kolba713ec82010-11-29 17:27:06 -08001606 void setViewContainer(View container) {
1607 mContainer = container;
1608 }
1609
Michael Kolb8233fac2010-10-26 16:08:53 -07001610 View getViewContainer() {
1611 return mContainer;
1612 }
1613
Grace Kloba22ac16e2009-10-07 18:00:23 -07001614 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001615 * Return whether private browsing is enabled for the main window of
1616 * this tab.
1617 * @return True if private browsing is enabled.
1618 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001619 boolean isPrivateBrowsingEnabled() {
John Reck502a3532011-08-16 14:21:46 -07001620 return mCurrentState.mIncognito;
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001621 }
1622
1623 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001624 * Return the subwindow of this tab or null if there is no subwindow.
1625 * @return The subwindow of this tab or null.
1626 */
1627 WebView getSubWebView() {
1628 return mSubView;
1629 }
1630
Michael Kolb1514bb72010-11-22 09:11:48 -08001631 void setSubWebView(WebView subView) {
1632 mSubView = subView;
1633 }
1634
Michael Kolb8233fac2010-10-26 16:08:53 -07001635 View getSubViewContainer() {
1636 return mSubViewContainer;
1637 }
1638
Michael Kolb1514bb72010-11-22 09:11:48 -08001639 void setSubViewContainer(View subViewContainer) {
1640 mSubViewContainer = subViewContainer;
1641 }
1642
Grace Kloba22ac16e2009-10-07 18:00:23 -07001643 /**
1644 * @return The geolocation permissions prompt for this tab.
1645 */
1646 GeolocationPermissionsPrompt getGeolocationPermissionsPrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001647 if (mGeolocationPermissionsPrompt == null) {
1648 ViewStub stub = (ViewStub) mContainer
1649 .findViewById(R.id.geolocation_permissions_prompt);
1650 mGeolocationPermissionsPrompt = (GeolocationPermissionsPrompt) stub
1651 .inflate();
Grace Kloba50c241e2010-04-20 11:07:50 -07001652 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001653 return mGeolocationPermissionsPrompt;
1654 }
1655
1656 /**
1657 * @return The application id string
1658 */
1659 String getAppId() {
1660 return mAppId;
1661 }
1662
1663 /**
1664 * Set the application id string
1665 * @param id
1666 */
1667 void setAppId(String id) {
1668 mAppId = id;
1669 }
1670
Michael Kolbe28b3472011-08-04 16:54:31 -07001671 boolean closeOnBack() {
1672 return mCloseOnBack;
1673 }
1674
1675 void setCloseOnBack(boolean close) {
1676 mCloseOnBack = close;
1677 }
1678
Grace Kloba22ac16e2009-10-07 18:00:23 -07001679 String getUrl() {
John Reck324d4402011-01-11 16:56:42 -08001680 return UrlUtils.filteredUrl(mCurrentState.mUrl);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001681 }
1682
John Reck49a603c2011-03-03 09:33:05 -08001683 String getOriginalUrl() {
John Reckdb22ec42011-06-29 11:31:24 -07001684 if (mCurrentState.mOriginalUrl == null) {
1685 return getUrl();
John Reck49a603c2011-03-03 09:33:05 -08001686 }
John Reckdb22ec42011-06-29 11:31:24 -07001687 return UrlUtils.filteredUrl(mCurrentState.mOriginalUrl);
John Reck49a603c2011-03-03 09:33:05 -08001688 }
1689
Grace Kloba22ac16e2009-10-07 18:00:23 -07001690 /**
John Reck30c714c2010-12-16 17:30:34 -08001691 * Get the title of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001692 */
1693 String getTitle() {
John Reck30c714c2010-12-16 17:30:34 -08001694 if (mCurrentState.mTitle == null && mInPageLoad) {
Michael Kolb14612442011-06-24 13:06:29 -07001695 return mContext.getString(R.string.title_bar_loading);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001696 }
John Reck30c714c2010-12-16 17:30:34 -08001697 return mCurrentState.mTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001698 }
1699
1700 /**
John Reck30c714c2010-12-16 17:30:34 -08001701 * Get the favicon of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001702 */
1703 Bitmap getFavicon() {
John Reck1cf4b792011-07-26 10:22:22 -07001704 if (mCurrentState.mFavicon != null) {
1705 return mCurrentState.mFavicon;
1706 }
1707 return getDefaultFavicon(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001708 }
1709
John Recke969cc52010-12-21 17:24:43 -08001710 public boolean isBookmarkedSite() {
1711 return mCurrentState.mIsBookmarkedSite;
1712 }
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001713
Grace Kloba22ac16e2009-10-07 18:00:23 -07001714 /**
1715 * Return the tab's error console. Creates the console if createIfNEcessary
1716 * is true and we haven't already created the console.
1717 * @param createIfNecessary Flag to indicate if the console should be
1718 * created if it has not been already.
1719 * @return The tab's error console, or null if one has not been created and
1720 * createIfNecessary is false.
1721 */
1722 ErrorConsoleView getErrorConsole(boolean createIfNecessary) {
1723 if (createIfNecessary && mErrorConsole == null) {
Michael Kolb14612442011-06-24 13:06:29 -07001724 mErrorConsole = new ErrorConsoleView(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001725 mErrorConsole.setWebView(mMainView);
1726 }
1727 return mErrorConsole;
1728 }
1729
Steve Block08a6f0c2011-10-06 12:12:53 +01001730 /**
1731 * Sets the security state, clears the SSL certificate error and informs
1732 * the controller.
1733 */
Steve Block2466eff2011-10-03 15:33:09 +01001734 private void setSecurityState(SecurityState securityState) {
1735 mCurrentState.mSecurityState = securityState;
Steve Block08a6f0c2011-10-06 12:12:53 +01001736 mCurrentState.mSslCertificateError = null;
Steve Block2466eff2011-10-03 15:33:09 +01001737 mWebViewController.onUpdatedSecurityState(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001738 }
1739
1740 /**
Steve Block2466eff2011-10-03 15:33:09 +01001741 * @return The tab's security state.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001742 */
Steve Block2466eff2011-10-03 15:33:09 +01001743 SecurityState getSecurityState() {
1744 return mCurrentState.mSecurityState;
John Reck30c714c2010-12-16 17:30:34 -08001745 }
1746
Steve Block08a6f0c2011-10-06 12:12:53 +01001747 /**
1748 * Gets the SSL certificate error, if any, for the page's main resource.
1749 * This is only non-null when the security state is
1750 * SECURITY_STATE_BAD_CERTIFICATE.
1751 */
1752 SslError getSslCertificateError() {
1753 return mCurrentState.mSslCertificateError;
1754 }
1755
John Reck30c714c2010-12-16 17:30:34 -08001756 int getLoadProgress() {
1757 if (mInPageLoad) {
1758 return mPageLoadProgress;
1759 }
1760 return 100;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001761 }
1762
1763 /**
1764 * @return TRUE if onPageStarted is called while onPageFinished is not
1765 * called yet.
1766 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001767 boolean inPageLoad() {
1768 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001769 }
1770
Grace Kloba22ac16e2009-10-07 18:00:23 -07001771 /**
John Reck1cf4b792011-07-26 10:22:22 -07001772 * @return The Bundle with the tab's state if it can be saved, otherwise null
Grace Kloba22ac16e2009-10-07 18:00:23 -07001773 */
John Reck1cf4b792011-07-26 10:22:22 -07001774 public Bundle saveState() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001775 // If the WebView is null it means we ran low on memory and we already
1776 // stored the saved state in mSavedState.
1777 if (mMainView == null) {
John Reck1cf4b792011-07-26 10:22:22 -07001778 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001779 }
John Reck6c2e2f32011-08-22 13:41:23 -07001780
1781 if (TextUtils.isEmpty(mCurrentState.mUrl)) {
John Reck1cf4b792011-07-26 10:22:22 -07001782 return null;
John Reck24f18262011-06-17 14:47:20 -07001783 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001784
1785 mSavedState = new Bundle();
John Reck6c2e2f32011-08-22 13:41:23 -07001786 WebBackForwardList savedList = mMainView.saveState(mSavedState);
1787 if (savedList == null || savedList.getSize() == 0) {
1788 Log.w(LOGTAG, "Failed to save back/forward list for "
1789 + mCurrentState.mUrl);
1790 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001791
Michael Kolbc831b632011-05-11 09:30:34 -07001792 mSavedState.putLong(ID, mId);
John Reck30c714c2010-12-16 17:30:34 -08001793 mSavedState.putString(CURRURL, mCurrentState.mUrl);
1794 mSavedState.putString(CURRTITLE, mCurrentState.mTitle);
John Reck1cf4b792011-07-26 10:22:22 -07001795 mSavedState.putBoolean(INCOGNITO, mMainView.isPrivateBrowsingEnabled());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001796 if (mAppId != null) {
1797 mSavedState.putString(APPID, mAppId);
1798 }
Michael Kolbe28b3472011-08-04 16:54:31 -07001799 mSavedState.putBoolean(CLOSEFLAG, mCloseOnBack);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001800 // Remember the parent tab so the relationship can be restored.
Michael Kolbc831b632011-05-11 09:30:34 -07001801 if (mParent != null) {
1802 mSavedState.putLong(PARENTTAB, mParent.mId);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001803 }
John Reckb0a86db2011-05-24 14:05:58 -07001804 mSavedState.putBoolean(USERAGENT,
1805 mSettings.hasDesktopUseragent(getWebView()));
John Reck1cf4b792011-07-26 10:22:22 -07001806 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001807 }
1808
1809 /*
1810 * Restore the state of the tab.
1811 */
John Reck1cf4b792011-07-26 10:22:22 -07001812 private void restoreState(Bundle b) {
1813 mSavedState = b;
1814 if (mSavedState == null) {
1815 return;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001816 }
1817 // Restore the internal state even if the WebView fails to restore.
1818 // This will maintain the app id, original url and close-on-exit values.
Michael Kolbc831b632011-05-11 09:30:34 -07001819 mId = b.getLong(ID);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001820 mAppId = b.getString(APPID);
Michael Kolbe28b3472011-08-04 16:54:31 -07001821 mCloseOnBack = b.getBoolean(CLOSEFLAG);
John Reck8b9bb8b2012-03-08 13:19:40 -08001822 restoreUserAgent();
John Reck1cf4b792011-07-26 10:22:22 -07001823 String url = b.getString(CURRURL);
1824 String title = b.getString(CURRTITLE);
1825 boolean incognito = b.getBoolean(INCOGNITO);
1826 mCurrentState = new PageState(mContext, incognito, url, null);
1827 mCurrentState.mTitle = title;
1828 synchronized (Tab.this) {
1829 if (mCapture != null) {
John Reck4eadc342011-10-31 14:04:10 -07001830 DataController.getInstance(mContext).loadThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07001831 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001832 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001833 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04001834
John Reck8b9bb8b2012-03-08 13:19:40 -08001835 private void restoreUserAgent() {
1836 if (mMainView == null || mSavedState == null) {
1837 return;
1838 }
1839 if (mSavedState.getBoolean(USERAGENT)
1840 != mSettings.hasDesktopUseragent(mMainView)) {
1841 mSettings.toggleDesktopUseragent(mMainView);
1842 }
1843 }
1844
Leon Scroggins1961ed22010-12-07 15:22:21 -05001845 public void updateBookmarkedStatus() {
John Recke969cc52010-12-21 17:24:43 -08001846 mDataController.queryBookmarkStatus(getUrl(), mIsBookmarkCallback);
Leon Scroggins1961ed22010-12-07 15:22:21 -05001847 }
1848
John Recke969cc52010-12-21 17:24:43 -08001849 private DataController.OnQueryUrlIsBookmark mIsBookmarkCallback
1850 = new DataController.OnQueryUrlIsBookmark() {
1851 @Override
1852 public void onQueryUrlIsBookmark(String url, boolean isBookmark) {
1853 if (mCurrentState.mUrl.equals(url)) {
1854 mCurrentState.mIsBookmarkedSite = isBookmark;
1855 mWebViewController.bookmarkedStatusHasChanged(Tab.this);
1856 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001857 }
John Recke969cc52010-12-21 17:24:43 -08001858 };
Michael Kolb1acef692011-03-08 14:12:06 -08001859
Michael Kolbeb95db42011-03-03 10:38:40 -08001860 public Bitmap getScreenshot() {
John Reck1cf4b792011-07-26 10:22:22 -07001861 synchronized (Tab.this) {
1862 return mCapture;
1863 }
Michael Kolbeb95db42011-03-03 10:38:40 -08001864 }
1865
John Reck541f55a2011-06-07 16:34:43 -07001866 public boolean isSnapshot() {
John Reck541f55a2011-06-07 16:34:43 -07001867 return false;
1868 }
1869
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001870 private static class SaveCallback implements ValueCallback<String> {
1871 boolean onReceiveValueCalled = false;
1872 private String mPath;
John Reck68234a92012-04-19 15:27:12 -07001873
1874 @Override
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001875 public void onReceiveValue(String path) {
1876 this.onReceiveValueCalled = true;
1877 this.mPath = path;
John Reck68234a92012-04-19 15:27:12 -07001878 synchronized (this) {
1879 notifyAll();
John Reck8cc92352011-07-06 17:41:52 -07001880 }
John Reck541f55a2011-06-07 16:34:43 -07001881 }
John Reck68234a92012-04-19 15:27:12 -07001882
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001883 public String getPath() {
1884 return mPath;
1885 }
John Reck68234a92012-04-19 15:27:12 -07001886 }
1887
1888 /**
1889 * Must be called on the UI thread
1890 */
1891 public ContentValues createSnapshotValues() {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001892 WebView web = getWebView();
John Reck68234a92012-04-19 15:27:12 -07001893 if (web == null) return null;
John Reckd8c74522011-06-14 08:45:00 -07001894 ContentValues values = new ContentValues();
1895 values.put(Snapshots.TITLE, mCurrentState.mTitle);
1896 values.put(Snapshots.URL, mCurrentState.mUrl);
John Reck68234a92012-04-19 15:27:12 -07001897 values.put(Snapshots.BACKGROUND, web.getPageBackgroundColor());
John Reck8cc92352011-07-06 17:41:52 -07001898 values.put(Snapshots.DATE_CREATED, System.currentTimeMillis());
1899 values.put(Snapshots.FAVICON, compressBitmap(getFavicon()));
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001900 Bitmap screenshot = web.getViewportBitmap();
John Reck8cc92352011-07-06 17:41:52 -07001901 values.put(Snapshots.THUMBNAIL, compressBitmap(screenshot));
John Reckd8c74522011-06-14 08:45:00 -07001902 return values;
John Reck541f55a2011-06-07 16:34:43 -07001903 }
1904
John Reck68234a92012-04-19 15:27:12 -07001905 /**
1906 * Probably want to call this on a background thread
1907 */
1908 public boolean saveViewState(ContentValues values) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001909 WebView web = getWebView();
John Reck68234a92012-04-19 15:27:12 -07001910 if (web == null) return false;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001911 String filename = UUID.randomUUID().toString();
John Reck68234a92012-04-19 15:27:12 -07001912 SaveCallback callback = new SaveCallback();
John Reck68234a92012-04-19 15:27:12 -07001913 try {
John Reck68234a92012-04-19 15:27:12 -07001914 synchronized (callback) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001915 web.saveViewState(filename, callback);
1916 callback.wait();
John Reck68234a92012-04-19 15:27:12 -07001917 }
John Reck68234a92012-04-19 15:27:12 -07001918 } catch (Exception e) {
1919 Log.w(LOGTAG, "Failed to save view state", e);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001920 String path = callback.getPath();
1921 if (path != null) {
1922 File file = mContext.getFileStreamPath(path);
1923 if (file.exists() && !file.delete()) {
1924 file.deleteOnExit();
1925 }
John Reck68234a92012-04-19 15:27:12 -07001926 }
1927 return false;
1928 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001929 String path = callback.getPath();
1930 File savedFile = new File(path);
1931 if (!savedFile.exists()) {
1932 return false;
John Reck68234a92012-04-19 15:27:12 -07001933 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001934 values.put(Snapshots.VIEWSTATE_PATH, path.substring(path.lastIndexOf('/') + 1));
1935 values.put(Snapshots.VIEWSTATE_SIZE, savedFile.length());
John Reck68234a92012-04-19 15:27:12 -07001936 return true;
1937 }
1938
John Reck8cc92352011-07-06 17:41:52 -07001939 public byte[] compressBitmap(Bitmap bitmap) {
1940 if (bitmap == null) {
1941 return null;
1942 }
1943 ByteArrayOutputStream stream = new ByteArrayOutputStream();
1944 bitmap.compress(CompressFormat.PNG, 100, stream);
1945 return stream.toByteArray();
1946 }
1947
John Reck26b18322011-06-21 13:08:58 -07001948 public void loadUrl(String url, Map<String, String> headers) {
1949 if (mMainView != null) {
Michael Kolba53c9892011-10-05 13:31:40 -07001950 mPageLoadProgress = INITIAL_PROGRESS;
1951 mInPageLoad = true;
Michael Kolb14612442011-06-24 13:06:29 -07001952 mCurrentState = new PageState(mContext, false, url, null);
John Reck26b18322011-06-21 13:08:58 -07001953 mWebViewController.onPageStarted(this, mMainView, null);
1954 mMainView.loadUrl(url, headers);
1955 }
1956 }
1957
John Reck38b39652012-06-05 09:22:59 -07001958 public void disableUrlOverridingForLoad() {
1959 mDisableOverrideUrlLoading = true;
1960 }
1961
Michael Kolb9ef259a2011-07-12 15:33:08 -07001962 protected void capture() {
1963 if (mMainView == null || mCapture == null) return;
John Reck4eadc342011-10-31 14:04:10 -07001964 if (mMainView.getContentWidth() <= 0 || mMainView.getContentHeight() <= 0) {
1965 return;
1966 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001967 Canvas c = new Canvas(mCapture);
Michael Kolba3194d02011-09-07 11:23:51 -07001968 int state = c.save();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001969 Bitmap screenShot = mMainView.getViewportBitmap();
1970 if (screenShot != null) {
1971 mCapture.eraseColor(Color.WHITE);
1972 float scale = (float) mCaptureWidth / screenShot.getWidth();
1973 c.scale(scale, scale);
1974 c.drawBitmap(screenShot, 0, 0, null);
1975 } else {
1976 final int left = mMainView.getViewScrollX();
1977 final int top = mMainView.getViewScrollY() + mMainView.getVisibleTitleHeight();
1978 c.translate(-left, -top);
1979 float scale = mCaptureWidth / (float) mMainView.getWidth();
1980 c.scale(scale, scale, left, top);
1981 if (mMainView instanceof BrowserWebView) {
1982 ((BrowserWebView)mMainView).drawContent(c);
1983 } else {
1984 mMainView.draw(c);
1985 }
John Reck8ee633f2011-08-09 16:00:35 -07001986 }
Michael Kolba3194d02011-09-07 11:23:51 -07001987 c.restoreToCount(state);
1988 // manually anti-alias the edges for the tilt
1989 c.drawRect(0, 0, 1, mCapture.getHeight(), sAlphaPaint);
1990 c.drawRect(mCapture.getWidth() - 1, 0, mCapture.getWidth(),
1991 mCapture.getHeight(), sAlphaPaint);
1992 c.drawRect(0, 0, mCapture.getWidth(), 1, sAlphaPaint);
1993 c.drawRect(0, mCapture.getHeight() - 1, mCapture.getWidth(),
1994 mCapture.getHeight(), sAlphaPaint);
Dianne Hackborn43cfe8a2011-08-02 16:59:35 -07001995 c.setBitmap(null);
John Reck8ee633f2011-08-09 16:00:35 -07001996 mHandler.removeMessages(MSG_CAPTURE);
John Reck1cf4b792011-07-26 10:22:22 -07001997 persistThumbnail();
John Reck8ee633f2011-08-09 16:00:35 -07001998 TabControl tc = mWebViewController.getTabControl();
1999 if (tc != null) {
2000 OnThumbnailUpdatedListener updateListener
2001 = tc.getOnThumbnailUpdatedListener();
2002 if (updateListener != null) {
2003 updateListener.onThumbnailUpdated(this);
2004 }
2005 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07002006 }
2007
2008 @Override
2009 public void onNewPicture(WebView view, Picture picture) {
John Reck1cf4b792011-07-26 10:22:22 -07002010 postCapture();
2011 }
2012
2013 private void postCapture() {
Michael Kolb9ef259a2011-07-12 15:33:08 -07002014 if (!mHandler.hasMessages(MSG_CAPTURE)) {
2015 mHandler.sendEmptyMessageDelayed(MSG_CAPTURE, CAPTURE_DELAY);
2016 }
2017 }
2018
John Reckef654f12011-07-12 16:42:08 -07002019 public boolean canGoBack() {
2020 return mMainView != null ? mMainView.canGoBack() : false;
2021 }
2022
2023 public boolean canGoForward() {
2024 return mMainView != null ? mMainView.canGoForward() : false;
2025 }
2026
2027 public void goBack() {
2028 if (mMainView != null) {
2029 mMainView.goBack();
2030 }
2031 }
2032
2033 public void goForward() {
2034 if (mMainView != null) {
2035 mMainView.goForward();
2036 }
2037 }
2038
Mathew Inwood1dd8e822011-08-03 14:34:29 +01002039 /**
2040 * Causes the tab back/forward stack to be cleared once, if the given URL is the next URL
2041 * to be added to the stack.
2042 *
2043 * This is used to ensure that preloaded URLs that are not subsequently seen by the user do
2044 * not appear in the back stack.
2045 */
Mathew Inwooda829d552011-09-02 14:16:25 +01002046 public void clearBackStackWhenItemAdded(Pattern urlPattern) {
2047 mClearHistoryUrlPattern = urlPattern;
Mathew Inwood1dd8e822011-08-03 14:34:29 +01002048 }
2049
John Reck1cf4b792011-07-26 10:22:22 -07002050 protected void persistThumbnail() {
John Reck4eadc342011-10-31 14:04:10 -07002051 DataController.getInstance(mContext).saveThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07002052 }
2053
2054 protected void deleteThumbnail() {
John Reck4eadc342011-10-31 14:04:10 -07002055 DataController.getInstance(mContext).deleteThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07002056 }
2057
John Reck4eadc342011-10-31 14:04:10 -07002058 void updateCaptureFromBlob(byte[] blob) {
John Reck1cf4b792011-07-26 10:22:22 -07002059 synchronized (Tab.this) {
2060 if (mCapture == null) {
2061 return;
2062 }
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002063 ByteBuffer buffer = ByteBuffer.wrap(blob);
Michael Kolbd837a112011-08-09 14:00:18 -07002064 try {
2065 mCapture.copyPixelsFromBuffer(buffer);
2066 } catch (RuntimeException rex) {
2067 Log.e(LOGTAG, "Load capture has mismatched sizes; buffer: "
2068 + buffer.capacity() + " blob: " + blob.length
2069 + "capture: " + mCapture.getByteCount());
2070 throw rex;
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002071 }
John Reck1cf4b792011-07-26 10:22:22 -07002072 }
2073 }
2074
John Reck52be4782011-08-26 15:37:29 -07002075 @Override
2076 public String toString() {
2077 StringBuilder builder = new StringBuilder(100);
2078 builder.append(mId);
2079 builder.append(") has parent: ");
2080 if (getParent() != null) {
2081 builder.append("true[");
2082 builder.append(getParent().getId());
2083 builder.append("]");
2084 } else {
2085 builder.append("false");
2086 }
2087 builder.append(", incog: ");
2088 builder.append(isPrivateBrowsingEnabled());
2089 if (!isPrivateBrowsingEnabled()) {
2090 builder.append(", title: ");
2091 builder.append(getTitle());
2092 builder.append(", url: ");
2093 builder.append(getUrl());
2094 }
2095 return builder.toString();
2096 }
2097
Steve Block4895b012011-10-03 16:26:46 +01002098 private void handleProceededAfterSslError(SslError error) {
2099 if (error.getUrl().equals(mCurrentState.mUrl)) {
2100 // The security state should currently be SECURITY_STATE_SECURE.
2101 setSecurityState(SecurityState.SECURITY_STATE_BAD_CERTIFICATE);
Steve Block08a6f0c2011-10-06 12:12:53 +01002102 mCurrentState.mSslCertificateError = error;
Steve Block4895b012011-10-03 16:26:46 +01002103 } else if (getSecurityState() == SecurityState.SECURITY_STATE_SECURE) {
Steve Block08a6f0c2011-10-06 12:12:53 +01002104 // The page's main resource is secure and this error is for a
2105 // sub-resource.
Steve Block4895b012011-10-03 16:26:46 +01002106 setSecurityState(SecurityState.SECURITY_STATE_MIXED);
2107 }
2108 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002109}