blob: c9e785fcfdce8cf804fdb8582ab119e5c80d743a [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;
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700178 // flag to indicate if the tab was opened from an intent
179 private boolean mDerivedFromIntent = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700180 // Keep the original url around to avoid killing the old WebView if the url
181 // has not changed.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700182 // Error console for the tab
183 private ErrorConsoleView mErrorConsole;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -0500184 // The listener that gets invoked when a download is started from the
185 // mMainView
Selim Gurun0b3d66f2012-08-29 13:08:13 -0700186 private final BrowserDownloadListener mDownloadListener;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500187 // Listener used to know when we move forward or back in the history list.
188 private final WebBackForwardListClient mWebBackForwardListClient;
John Recke969cc52010-12-21 17:24:43 -0800189 private DataController mDataController;
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700190 //Indicates if a JS interface was created for a specific url
191 private boolean mJsInterfaceEnabled = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700192
193 // AsyncTask for downloading touch icons
194 DownloadTouchIcon mTouchIconLoader;
195
John Reck35e9dd62011-04-25 09:01:54 -0700196 private BrowserSettings mSettings;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700197 private int mCaptureWidth;
198 private int mCaptureHeight;
199 private Bitmap mCapture;
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700200 private Bitmap mScreenShot;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700201 private Handler mHandler;
Michael Kolb72864272012-05-03 15:42:15 -0700202 private boolean mUpdateThumbnail;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800203 private Timestamp timestamp;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700204
Mathew Inwood1dd8e822011-08-03 14:34:29 +0100205 /**
Mathew Inwood9ad1eac2011-09-15 11:29:50 +0100206 * See {@link #clearBackStackWhenItemAdded(String)}.
Mathew Inwood1dd8e822011-08-03 14:34:29 +0100207 */
Mathew Inwooda829d552011-09-02 14:16:25 +0100208 private Pattern mClearHistoryUrlPattern;
Mathew Inwood1dd8e822011-08-03 14:34:29 +0100209
John Reck1cf4b792011-07-26 10:22:22 -0700210 private static synchronized Bitmap getDefaultFavicon(Context context) {
211 if (sDefaultFavicon == null) {
212 sDefaultFavicon = BitmapFactory.decodeResource(
213 context.getResources(), R.drawable.app_web_browser_sm);
214 }
215 return sDefaultFavicon;
216 }
Michael Kolbeb95db42011-03-03 10:38:40 -0800217
John Reck30c714c2010-12-16 17:30:34 -0800218 // All the state needed for a page
John Reckd8c74522011-06-14 08:45:00 -0700219 protected static class PageState {
John Reck30c714c2010-12-16 17:30:34 -0800220 String mUrl;
John Reckdb22ec42011-06-29 11:31:24 -0700221 String mOriginalUrl;
John Reck30c714c2010-12-16 17:30:34 -0800222 String mTitle;
Steve Block2466eff2011-10-03 15:33:09 +0100223 SecurityState mSecurityState;
Steve Block08a6f0c2011-10-06 12:12:53 +0100224 // This is non-null only when mSecurityState is SECURITY_STATE_BAD_CERTIFICATE.
225 SslError mSslCertificateError;
John Reck30c714c2010-12-16 17:30:34 -0800226 Bitmap mFavicon;
Steve Block08a6f0c2011-10-06 12:12:53 +0100227 boolean mIsBookmarkedSite;
228 boolean mIncognito;
John Reck30c714c2010-12-16 17:30:34 -0800229
230 PageState(Context c, boolean incognito) {
John Reck502a3532011-08-16 14:21:46 -0700231 mIncognito = incognito;
232 if (mIncognito) {
John Reckdb22ec42011-06-29 11:31:24 -0700233 mOriginalUrl = mUrl = "browser:incognito";
John Reck30c714c2010-12-16 17:30:34 -0800234 mTitle = c.getString(R.string.new_incognito_tab);
John Reck30c714c2010-12-16 17:30:34 -0800235 } else {
John Reckdb22ec42011-06-29 11:31:24 -0700236 mOriginalUrl = mUrl = "";
John Reck30c714c2010-12-16 17:30:34 -0800237 mTitle = c.getString(R.string.new_tab);
John Reck30c714c2010-12-16 17:30:34 -0800238 }
Steve Block2466eff2011-10-03 15:33:09 +0100239 mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
John Reck30c714c2010-12-16 17:30:34 -0800240 }
241
242 PageState(Context c, boolean incognito, String url, Bitmap favicon) {
John Reck502a3532011-08-16 14:21:46 -0700243 mIncognito = incognito;
John Reckdb22ec42011-06-29 11:31:24 -0700244 mOriginalUrl = mUrl = url;
John Reck30c714c2010-12-16 17:30:34 -0800245 if (URLUtil.isHttpsUrl(url)) {
Steve Block2466eff2011-10-03 15:33:09 +0100246 mSecurityState = SecurityState.SECURITY_STATE_SECURE;
John Reck30c714c2010-12-16 17:30:34 -0800247 } else {
Steve Block2466eff2011-10-03 15:33:09 +0100248 mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
John Reck30c714c2010-12-16 17:30:34 -0800249 }
John Reck1cf4b792011-07-26 10:22:22 -0700250 mFavicon = favicon;
John Reck30c714c2010-12-16 17:30:34 -0800251 }
John Reck1cf4b792011-07-26 10:22:22 -0700252
Grace Kloba22ac16e2009-10-07 18:00:23 -0700253 }
254
John Reck30c714c2010-12-16 17:30:34 -0800255 // The current/loading page's state
John Reckd8c74522011-06-14 08:45:00 -0700256 protected PageState mCurrentState;
John Reck30c714c2010-12-16 17:30:34 -0800257
Grace Kloba22ac16e2009-10-07 18:00:23 -0700258 // Used for saving and restoring each Tab
Michael Kolbc831b632011-05-11 09:30:34 -0700259 static final String ID = "ID";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700260 static final String CURRURL = "currentUrl";
261 static final String CURRTITLE = "currentTitle";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700262 static final String PARENTTAB = "parentTab";
263 static final String APPID = "appid";
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700264 static final String INCOGNITO = "privateBrowsingEnabled";
John Reckb0a86db2011-05-24 14:05:58 -0700265 static final String USERAGENT = "useragent";
Michael Kolbe28b3472011-08-04 16:54:31 -0700266 static final String CLOSEFLAG = "closeOnBack";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700267
Grace Kloba22ac16e2009-10-07 18:00:23 -0700268 // Container class for the next error dialog that needs to be displayed
269 private class ErrorDialog {
270 public final int mTitle;
271 public final String mDescription;
272 public final int mError;
273 ErrorDialog(int title, String desc, int error) {
274 mTitle = title;
275 mDescription = desc;
276 mError = error;
277 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700278 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700279
280 private void processNextError() {
281 if (mQueuedErrors == null) {
282 return;
283 }
284 // The first one is currently displayed so just remove it.
285 mQueuedErrors.removeFirst();
286 if (mQueuedErrors.size() == 0) {
287 mQueuedErrors = null;
288 return;
289 }
290 showError(mQueuedErrors.getFirst());
291 }
292
293 private DialogInterface.OnDismissListener mDialogListener =
294 new DialogInterface.OnDismissListener() {
295 public void onDismiss(DialogInterface d) {
296 processNextError();
297 }
298 };
299 private LinkedList<ErrorDialog> mQueuedErrors;
300
301 private void queueError(int err, String desc) {
302 if (mQueuedErrors == null) {
303 mQueuedErrors = new LinkedList<ErrorDialog>();
304 }
305 for (ErrorDialog d : mQueuedErrors) {
306 if (d.mError == err) {
307 // Already saw a similar error, ignore the new one.
308 return;
309 }
310 }
311 ErrorDialog errDialog = new ErrorDialog(
312 err == WebViewClient.ERROR_FILE_NOT_FOUND ?
313 R.string.browserFrameFileErrorLabel :
314 R.string.browserFrameNetworkErrorLabel,
315 desc, err);
316 mQueuedErrors.addLast(errDialog);
317
318 // Show the dialog now if the queue was empty and it is in foreground
319 if (mQueuedErrors.size() == 1 && mInForeground) {
320 showError(errDialog);
321 }
322 }
323
324 private void showError(ErrorDialog errDialog) {
325 if (mInForeground) {
Michael Kolb14612442011-06-24 13:06:29 -0700326 AlertDialog d = new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700327 .setTitle(errDialog.mTitle)
328 .setMessage(errDialog.mDescription)
329 .setPositiveButton(R.string.ok, null)
330 .create();
331 d.setOnDismissListener(mDialogListener);
332 d.show();
333 }
334 }
335
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800336 protected void replaceCrashView(View view, View container) {
337 if (hasCrashed && (view == mMainView)) {
338 final FrameLayout wrapper = (FrameLayout) container.findViewById(R.id.webview_wrapper);
339 wrapper.removeAllViewsInLayout();
340 wrapper.addView(view);
341 hasCrashed = false;
342 }
343 }
344
345 protected void showCrashView() {
346 if (hasCrashed) {
347 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
348 Context.LAYOUT_INFLATER_SERVICE);
349 final View crashLayout = inflater.inflate(R.layout.browser_tab_crash, null);
350 final FrameLayout wrapper =
351 (FrameLayout) mContainer.findViewById(R.id.webview_wrapper);
352 wrapper.removeAllViewsInLayout();
353 wrapper.addView(crashLayout);
354 mContainer.requestFocus();
355 Button reloadBtn = (Button) crashLayout.findViewById(R.id.browser_crash_reload_btn);
356 reloadBtn.setOnClickListener(new OnClickListener() {
357 @Override
358 public void onClick(View arg0) {
359 replaceCrashView(mMainView, mContainer);
360 mMainView.reload();
361 }
362 });
363 }
364 }
365
Grace Kloba22ac16e2009-10-07 18:00:23 -0700366 // -------------------------------------------------------------------------
367 // WebViewClient implementation for the main WebView
368 // -------------------------------------------------------------------------
369
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800370 private final WebViewClient mWebViewClient = new WebViewClient() {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500371 private Message mDontResend;
372 private Message mResend;
Michael Kolb47bd1e42011-09-01 15:25:00 -0700373
374 private boolean providersDiffer(String url, String otherUrl) {
375 Uri uri1 = Uri.parse(url);
376 Uri uri2 = Uri.parse(otherUrl);
377 return !uri1.getEncodedAuthority().equals(uri2.getEncodedAuthority());
378 }
379
Grace Kloba22ac16e2009-10-07 18:00:23 -0700380 @Override
381 public void onPageStarted(WebView view, String url, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700382 mInPageLoad = true;
Michael Kolb72864272012-05-03 15:42:15 -0700383 mUpdateThumbnail = true;
Michael Kolba53c9892011-10-05 13:31:40 -0700384 mPageLoadProgress = INITIAL_PROGRESS;
Michael Kolb14612442011-06-24 13:06:29 -0700385 mCurrentState = new PageState(mContext,
John Reck30c714c2010-12-16 17:30:34 -0800386 view.isPrivateBrowsingEnabled(), url, favicon);
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000387 mLoadStartTime = SystemClock.uptimeMillis();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700388
389 // If we start a touch icon load and then load a new page, we don't
390 // want to cancel the current touch icon loader. But, we do want to
391 // create a new one when the touch icon url is known.
392 if (mTouchIconLoader != null) {
393 mTouchIconLoader.mTab = null;
394 mTouchIconLoader = null;
395 }
396
397 // reset the error console
398 if (mErrorConsole != null) {
399 mErrorConsole.clearErrorMessages();
Michael Kolb8233fac2010-10-26 16:08:53 -0700400 if (mWebViewController.shouldShowErrorConsole()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700401 mErrorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
402 }
403 }
404
Grace Kloba22ac16e2009-10-07 18:00:23 -0700405 // finally update the UI in the activity if it is in the foreground
John Reck324d4402011-01-11 16:56:42 -0800406 mWebViewController.onPageStarted(Tab.this, view, favicon);
Leon Scroggins4cd97792010-12-03 15:31:56 -0500407
John Recke969cc52010-12-21 17:24:43 -0800408 updateBookmarkedStatus();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700409 }
410
411 @Override
412 public void onPageFinished(WebView view, String url) {
John Reck38b39652012-06-05 09:22:59 -0700413 mDisableOverrideUrlLoading = false;
John Reck5b691842010-11-29 11:21:13 -0800414 if (!isPrivateBrowsingEnabled()) {
415 LogTag.logPageFinishedLoading(
416 url, SystemClock.uptimeMillis() - mLoadStartTime);
417 }
John Reck1cf4b792011-07-26 10:22:22 -0700418 syncCurrentState(view, url);
John Reck324d4402011-01-11 16:56:42 -0800419 mWebViewController.onPageFinished(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700420 }
421
422 // return true if want to hijack the url to let another app to handle it
423 @Override
424 public boolean shouldOverrideUrlLoading(WebView view, String url) {
John Reck38b39652012-06-05 09:22:59 -0700425 if (!mDisableOverrideUrlLoading && mInForeground) {
Michael Kolb18eb3772010-12-10 14:29:51 -0800426 return mWebViewController.shouldOverrideUrlLoading(Tab.this,
427 view, url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700428 } else {
429 return false;
430 }
431 }
432
433 /**
Steve Block2466eff2011-10-03 15:33:09 +0100434 * Updates the security state. This method is called when we discover
435 * another resource to be loaded for this page (for example,
436 * javascript). While we update the security state, we do not update
437 * the lock icon until we are done loading, as it is slightly more
438 * secure this way.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700439 */
440 @Override
441 public void onLoadResource(WebView view, String url) {
442 if (url != null && url.length() > 0) {
443 // It is only if the page claims to be secure that we may have
Steve Block2466eff2011-10-03 15:33:09 +0100444 // to update the security state:
445 if (mCurrentState.mSecurityState == SecurityState.SECURITY_STATE_SECURE) {
446 // If NOT a 'safe' url, change the state to mixed content!
Grace Kloba22ac16e2009-10-07 18:00:23 -0700447 if (!(URLUtil.isHttpsUrl(url) || URLUtil.isDataUrl(url)
448 || URLUtil.isAboutUrl(url))) {
Steve Block2466eff2011-10-03 15:33:09 +0100449 mCurrentState.mSecurityState = SecurityState.SECURITY_STATE_MIXED;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700450 }
451 }
452 }
453 }
454
455 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700456 * Show a dialog informing the user of the network error reported by
457 * WebCore if it is in the foreground.
458 */
459 @Override
460 public void onReceivedError(WebView view, int errorCode,
461 String description, String failingUrl) {
462 if (errorCode != WebViewClient.ERROR_HOST_LOOKUP &&
463 errorCode != WebViewClient.ERROR_CONNECT &&
464 errorCode != WebViewClient.ERROR_BAD_URL &&
465 errorCode != WebViewClient.ERROR_UNSUPPORTED_SCHEME &&
466 errorCode != WebViewClient.ERROR_FILE) {
467 queueError(errorCode, description);
Jeff Hamilton47654f42010-09-07 09:57:51 -0500468
Selim Gurun3da06b82011-10-10 13:58:12 -0700469 // Don't log URLs when in private browsing mode
470 if (!isPrivateBrowsingEnabled()) {
471 Log.e(LOGTAG, "onReceivedError " + errorCode + " " + failingUrl
Jeff Hamilton47654f42010-09-07 09:57:51 -0500472 + " " + description);
Selim Gurun3da06b82011-10-10 13:58:12 -0700473 }
Jeff Hamilton47654f42010-09-07 09:57:51 -0500474 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700475 }
476
477 /**
478 * Check with the user if it is ok to resend POST data as the page they
479 * are trying to navigate to is the result of a POST.
480 */
481 @Override
482 public void onFormResubmission(WebView view, final Message dontResend,
483 final Message resend) {
484 if (!mInForeground) {
485 dontResend.sendToTarget();
486 return;
487 }
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500488 if (mDontResend != null) {
489 Log.w(LOGTAG, "onFormResubmission should not be called again "
490 + "while dialog is still up");
491 dontResend.sendToTarget();
492 return;
493 }
494 mDontResend = dontResend;
495 mResend = resend;
Michael Kolb14612442011-06-24 13:06:29 -0700496 new AlertDialog.Builder(mContext).setTitle(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700497 R.string.browserFrameFormResubmitLabel).setMessage(
498 R.string.browserFrameFormResubmitMessage)
499 .setPositiveButton(R.string.ok,
500 new DialogInterface.OnClickListener() {
501 public void onClick(DialogInterface dialog,
502 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500503 if (mResend != null) {
504 mResend.sendToTarget();
505 mResend = null;
506 mDontResend = null;
507 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700508 }
509 }).setNegativeButton(R.string.cancel,
510 new DialogInterface.OnClickListener() {
511 public void onClick(DialogInterface dialog,
512 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500513 if (mDontResend != null) {
514 mDontResend.sendToTarget();
515 mResend = null;
516 mDontResend = null;
517 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700518 }
519 }).setOnCancelListener(new OnCancelListener() {
520 public void onCancel(DialogInterface dialog) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500521 if (mDontResend != null) {
522 mDontResend.sendToTarget();
523 mResend = null;
524 mDontResend = null;
525 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700526 }
527 }).show();
528 }
529
530 /**
531 * Insert the url into the visited history database.
532 * @param url The url to be inserted.
533 * @param isReload True if this url is being reloaded.
534 * FIXME: Not sure what to do when reloading the page.
535 */
536 @Override
537 public void doUpdateVisitedHistory(WebView view, String url,
538 boolean isReload) {
John Reck324d4402011-01-11 16:56:42 -0800539 mWebViewController.doUpdateVisitedHistory(Tab.this, isReload);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700540 }
541
542 /**
543 * Displays SSL error(s) dialog to the user.
544 */
545 @Override
546 public void onReceivedSslError(final WebView view,
547 final SslErrorHandler handler, final SslError error) {
548 if (!mInForeground) {
549 handler.cancel();
Steve Block2466eff2011-10-03 15:33:09 +0100550 setSecurityState(SecurityState.SECURITY_STATE_NOT_SECURE);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700551 return;
552 }
John Reck35e9dd62011-04-25 09:01:54 -0700553 if (mSettings.showSecurityWarnings()) {
John Reckcb28b2c2011-08-26 17:39:44 -0700554 new AlertDialog.Builder(mContext)
555 .setTitle(R.string.security_warning)
556 .setMessage(R.string.ssl_warnings_header)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200557 .setIconAttribute(android.R.attr.alertDialogIcon)
John Reckcb28b2c2011-08-26 17:39:44 -0700558 .setPositiveButton(R.string.ssl_continue,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700559 new DialogInterface.OnClickListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700560 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700561 public void onClick(DialogInterface dialog,
562 int whichButton) {
563 handler.proceed();
Steve Block4895b012011-10-03 16:26:46 +0100564 handleProceededAfterSslError(error);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700565 }
John Reckcb28b2c2011-08-26 17:39:44 -0700566 })
567 .setNeutralButton(R.string.view_certificate,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700568 new DialogInterface.OnClickListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700569 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700570 public void onClick(DialogInterface dialog,
571 int whichButton) {
John Reckcb28b2c2011-08-26 17:39:44 -0700572 mWebViewController.showSslCertificateOnError(
573 view, handler, error);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700574 }
John Reckcb28b2c2011-08-26 17:39:44 -0700575 })
576 .setNegativeButton(R.string.ssl_go_back,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700577 new DialogInterface.OnClickListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700578 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700579 public void onClick(DialogInterface dialog,
580 int whichButton) {
John Reck30c714c2010-12-16 17:30:34 -0800581 dialog.cancel();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700582 }
John Reckcb28b2c2011-08-26 17:39:44 -0700583 })
584 .setOnCancelListener(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700585 new DialogInterface.OnCancelListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700586 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700587 public void onCancel(DialogInterface dialog) {
588 handler.cancel();
Steve Block2466eff2011-10-03 15:33:09 +0100589 setSecurityState(SecurityState.SECURITY_STATE_NOT_SECURE);
John Reck30c714c2010-12-16 17:30:34 -0800590 mWebViewController.onUserCanceledSsl(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700591 }
John Reckcb28b2c2011-08-26 17:39:44 -0700592 })
593 .show();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700594 } else {
595 handler.proceed();
596 }
597 }
598
599 /**
Steve Block4895b012011-10-03 16:26:46 +0100600 * Called when an SSL error occurred while loading a resource, but the
601 * WebView but chose to proceed anyway based on a decision retained
602 * from a previous response to onReceivedSslError(). We update our
603 * security state to reflect this.
604 */
605 @Override
606 public void onProceededAfterSslError(WebView view, SslError error) {
607 handleProceededAfterSslError(error);
608 }
609
610 /**
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700611 * Displays client certificate request to the user.
612 */
613 @Override
614 public void onReceivedClientCertRequest(final WebView view,
615 final ClientCertRequestHandler handler, final String host_and_port) {
616 if (!mInForeground) {
617 handler.ignore();
618 return;
619 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700620 int colon = host_and_port.lastIndexOf(':');
621 String host;
622 int port;
623 if (colon == -1) {
624 host = host_and_port;
625 port = -1;
626 } else {
627 String portString = host_and_port.substring(colon + 1);
628 try {
629 port = Integer.parseInt(portString);
630 host = host_and_port.substring(0, colon);
631 } catch (NumberFormatException e) {
632 host = host_and_port;
633 port = -1;
634 }
635 }
Michael Kolb14612442011-06-24 13:06:29 -0700636 KeyChain.choosePrivateKeyAlias(
637 mWebViewController.getActivity(), new KeyChainAliasCallback() {
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700638 @Override public void alias(String alias) {
639 if (alias == null) {
640 handler.cancel();
641 return;
642 }
Michael Kolb14612442011-06-24 13:06:29 -0700643 new KeyChainLookup(mContext, handler, alias).execute();
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700644 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700645 }, null, null, host, port, null);
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700646 }
647
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800648 @Override
649 public void onRendererCrash(WebView view, boolean crashedWhileOomProtected) {
650 Log.e(LOGTAG, "Tab Crashed");
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700651 if (mWebViewController.getTabControl().getCurrentTab() == Tab.this) {
652 hasCrashed = true;
653 showCrashView();
654 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800655 }
656
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700657 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700658 * Handles an HTTP authentication request.
659 *
660 * @param handler The authentication handler
661 * @param host The host
662 * @param realm The realm
663 */
664 @Override
665 public void onReceivedHttpAuthRequest(WebView view,
666 final HttpAuthHandler handler, final String host,
667 final String realm) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700668 mWebViewController.onReceivedHttpAuthRequest(Tab.this, view, handler, host, realm);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700669 }
670
671 @Override
John Reck438bf462011-01-12 18:11:46 -0800672 public WebResourceResponse shouldInterceptRequest(WebView view,
673 String url) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800674 //intercept if opening a new incognito tab - show the incognito welcome page
675 if (url.startsWith("browser:incognito")) {
676 Resources resourceHandle = mContext.getResources();
677 InputStream inStream = resourceHandle.openRawResource(
Bijan Amirzada41242f22014-03-21 12:12:18 -0700678 com.android.browser.R.raw.incognito_mode_start_page);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800679 return new WebResourceResponse("text/html", "utf8", inStream);
680 }
kaiyiz6e5b3e02013-08-19 20:02:01 +0800681 WebResourceResponse res;
682 if (MyNavigationUtil.MY_NAVIGATION.equals(url)) {
683 res = MyNavigationProvider.shouldInterceptRequest(mContext, url);
684 } else {
685 res = HomeProvider.shouldInterceptRequest(mContext, url);
686 }
John Reck438bf462011-01-12 18:11:46 -0800687 return res;
688 }
689
690 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700691 public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
692 if (!mInForeground) {
693 return false;
694 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700695 return mWebViewController.shouldOverrideKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700696 }
697
698 @Override
699 public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700700 if (!mInForeground) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700701 return;
702 }
John Reck997b1b72012-04-19 18:08:25 -0700703 if (!mWebViewController.onUnhandledKeyEvent(event)) {
704 super.onUnhandledKeyEvent(view, event);
705 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700706 }
707 };
708
John Reck1cf4b792011-07-26 10:22:22 -0700709 private void syncCurrentState(WebView view, String url) {
710 // Sync state (in case of stop/timeout)
711 mCurrentState.mUrl = view.getUrl();
712 if (mCurrentState.mUrl == null) {
John Reck8bcafc12011-08-29 16:43:02 -0700713 mCurrentState.mUrl = "";
John Reck1cf4b792011-07-26 10:22:22 -0700714 }
715 mCurrentState.mOriginalUrl = view.getOriginalUrl();
716 mCurrentState.mTitle = view.getTitle();
717 mCurrentState.mFavicon = view.getFavicon();
718 if (!URLUtil.isHttpsUrl(mCurrentState.mUrl)) {
719 // In case we stop when loading an HTTPS page from an HTTP page
720 // but before a provisional load occurred
Steve Block2466eff2011-10-03 15:33:09 +0100721 mCurrentState.mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
Steve Block08a6f0c2011-10-06 12:12:53 +0100722 mCurrentState.mSslCertificateError = null;
John Reck1cf4b792011-07-26 10:22:22 -0700723 }
John Reck502a3532011-08-16 14:21:46 -0700724 mCurrentState.mIncognito = view.isPrivateBrowsingEnabled();
John Reck1cf4b792011-07-26 10:22:22 -0700725 }
726
Grace Kloba22ac16e2009-10-07 18:00:23 -0700727 // -------------------------------------------------------------------------
728 // WebChromeClient implementation for the main WebView
729 // -------------------------------------------------------------------------
730
731 private final WebChromeClient mWebChromeClient = new WebChromeClient() {
732 // Helper method to create a new tab or sub window.
733 private void createWindow(final boolean dialog, final Message msg) {
734 WebView.WebViewTransport transport =
735 (WebView.WebViewTransport) msg.obj;
736 if (dialog) {
737 createSubWindow();
Michael Kolb8233fac2010-10-26 16:08:53 -0700738 mWebViewController.attachSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700739 transport.setWebView(mSubView);
740 } else {
Michael Kolb7bcafde2011-05-09 13:55:59 -0700741 final Tab newTab = mWebViewController.openTab(null,
John Reck5949c662011-05-27 09:52:29 -0700742 Tab.this, true, true);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700743 transport.setWebView(newTab.getWebView());
744 }
745 msg.sendToTarget();
746 }
747
748 @Override
749 public boolean onCreateWindow(WebView view, final boolean dialog,
750 final boolean userGesture, final Message resultMsg) {
751 // only allow new window or sub window for the foreground case
752 if (!mInForeground) {
753 return false;
754 }
755 // Short-circuit if we can't create any more tabs or sub windows.
756 if (dialog && mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -0700757 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700758 .setTitle(R.string.too_many_subwindows_dialog_title)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200759 .setIconAttribute(android.R.attr.alertDialogIcon)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700760 .setMessage(R.string.too_many_subwindows_dialog_message)
761 .setPositiveButton(R.string.ok, null)
762 .show();
763 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700764 } else if (!mWebViewController.getTabControl().canCreateNewTab()) {
Michael Kolb14612442011-06-24 13:06:29 -0700765 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700766 .setTitle(R.string.too_many_windows_dialog_title)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200767 .setIconAttribute(android.R.attr.alertDialogIcon)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700768 .setMessage(R.string.too_many_windows_dialog_message)
769 .setPositiveButton(R.string.ok, null)
770 .show();
771 return false;
772 }
773
774 // Short-circuit if this was a user gesture.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800775 if (userGesture || !mSettings.blockPopupWindows()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700776 createWindow(dialog, resultMsg);
777 return true;
778 }
779
780 // Allow the popup and create the appropriate window.
781 final AlertDialog.OnClickListener allowListener =
782 new AlertDialog.OnClickListener() {
783 public void onClick(DialogInterface d,
784 int which) {
785 createWindow(dialog, resultMsg);
786 }
787 };
788
789 // Block the popup by returning a null WebView.
790 final AlertDialog.OnClickListener blockListener =
791 new AlertDialog.OnClickListener() {
792 public void onClick(DialogInterface d, int which) {
793 resultMsg.sendToTarget();
794 }
795 };
796
797 // Build a confirmation dialog to display to the user.
798 final AlertDialog d =
Michael Kolb14612442011-06-24 13:06:29 -0700799 new AlertDialog.Builder(mContext)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200800 .setIconAttribute(android.R.attr.alertDialogIcon)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700801 .setMessage(R.string.popup_window_attempt)
802 .setPositiveButton(R.string.allow, allowListener)
803 .setNegativeButton(R.string.block, blockListener)
804 .setCancelable(false)
805 .create();
806
807 // Show the confirmation dialog.
808 d.show();
809 return true;
810 }
811
812 @Override
Patrick Scotteb5061b2009-11-18 15:00:30 -0500813 public void onRequestFocus(WebView view) {
814 if (!mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700815 mWebViewController.switchToTab(Tab.this);
Patrick Scotteb5061b2009-11-18 15:00:30 -0500816 }
817 }
818
819 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700820 public void onCloseWindow(WebView window) {
Michael Kolbc831b632011-05-11 09:30:34 -0700821 if (mParent != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700822 // JavaScript can only close popup window.
823 if (mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700824 mWebViewController.switchToTab(mParent);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700825 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700826 mWebViewController.closeTab(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700827 }
828 }
829
830 @Override
831 public void onProgressChanged(WebView view, int newProgress) {
John Reck30c714c2010-12-16 17:30:34 -0800832 mPageLoadProgress = newProgress;
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800833 if (newProgress == 100) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800834 Log.i(CONSOLE_LOGTAG, "SWE Pageload Progress = 100");
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800835 mInPageLoad = false;
836 }
John Reck30c714c2010-12-16 17:30:34 -0800837 mWebViewController.onProgressChanged(Tab.this);
Michael Kolb72864272012-05-03 15:42:15 -0700838 if (mUpdateThumbnail && newProgress == 100) {
839 mUpdateThumbnail = false;
840 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700841 }
842
843 @Override
Leon Scroggins21d9b902010-03-11 09:33:11 -0500844 public void onReceivedTitle(WebView view, final String title) {
John Reck30c714c2010-12-16 17:30:34 -0800845 mCurrentState.mTitle = title;
Michael Kolb8233fac2010-10-26 16:08:53 -0700846 mWebViewController.onReceivedTitle(Tab.this, title);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700847 }
848
849 @Override
850 public void onReceivedIcon(WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -0800851 mCurrentState.mFavicon = icon;
Michael Kolb8233fac2010-10-26 16:08:53 -0700852 mWebViewController.onFavicon(Tab.this, view, icon);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700853 }
854
855 @Override
856 public void onReceivedTouchIconUrl(WebView view, String url,
857 boolean precomposed) {
Michael Kolb14612442011-06-24 13:06:29 -0700858 final ContentResolver cr = mContext.getContentResolver();
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400859 // Let precomposed icons take precedence over non-composed
860 // icons.
861 if (precomposed && mTouchIconLoader != null) {
862 mTouchIconLoader.cancel(false);
863 mTouchIconLoader = null;
864 }
865 // Have only one async task at a time.
866 if (mTouchIconLoader == null) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700867 mTouchIconLoader = new DownloadTouchIcon(Tab.this,
Michael Kolb14612442011-06-24 13:06:29 -0700868 mContext, cr, view);
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400869 mTouchIconLoader.execute(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700870 }
871 }
872
873 @Override
874 public void onShowCustomView(View view,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800875 CustomViewCallback callback) {
Michael Kolb14612442011-06-24 13:06:29 -0700876 Activity activity = mWebViewController.getActivity();
877 if (activity != null) {
878 onShowCustomView(view, activity.getRequestedOrientation(), callback);
879 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400880 }
881
882 @Override
883 public void onShowCustomView(View view, int requestedOrientation,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800884 CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700885 if (mInForeground) mWebViewController.showCustomView(Tab.this, view,
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400886 requestedOrientation, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700887 }
888
889 @Override
890 public void onHideCustomView() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700891 if (mInForeground) mWebViewController.hideCustomView();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700892 }
893
894 /**
895 * The origin has exceeded its database quota.
896 * @param url the URL that exceeded the quota
897 * @param databaseIdentifier the identifier of the database on which the
898 * transaction that caused the quota overflow was run
899 * @param currentQuota the current quota for the origin.
900 * @param estimatedSize the estimated size of the database.
901 * @param totalUsedQuota is the sum of all origins' quota.
902 * @param quotaUpdater The callback to run when a decision to allow or
903 * deny quota has been made. Don't forget to call this!
904 */
905 @Override
906 public void onExceededDatabaseQuota(String url,
907 String databaseIdentifier, long currentQuota, long estimatedSize,
908 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -0700909 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -0700910 .onExceededDatabaseQuota(url, databaseIdentifier,
911 currentQuota, estimatedSize, totalUsedQuota,
912 quotaUpdater);
913 }
914
915 /**
916 * The Application Cache has exceeded its max size.
917 * @param spaceNeeded is the amount of disk space that would be needed
918 * in order for the last appcache operation to succeed.
919 * @param totalUsedQuota is the sum of all origins' quota.
920 * @param quotaUpdater A callback to inform the WebCore thread that a
921 * new app cache size is available. This callback must always
922 * be executed at some point to ensure that the sleeping
923 * WebCore thread is woken up.
924 */
925 @Override
926 public void onReachedMaxAppCacheSize(long spaceNeeded,
927 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -0700928 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -0700929 .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota,
930 quotaUpdater);
931 }
932
933 /**
934 * Instructs the browser to show a prompt to ask the user to set the
935 * Geolocation permission state for the specified origin.
936 * @param origin The origin for which Geolocation permissions are
937 * requested.
938 * @param callback The callback to call once the user has set the
939 * Geolocation permission state.
940 */
941 @Override
942 public void onGeolocationPermissionsShowPrompt(String origin,
943 GeolocationPermissions.Callback callback) {
944 if (mInForeground) {
Grace Kloba50c241e2010-04-20 11:07:50 -0700945 getGeolocationPermissionsPrompt().show(origin, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700946 }
947 }
948
949 /**
950 * Instructs the browser to hide the Geolocation permissions prompt.
951 */
952 @Override
953 public void onGeolocationPermissionsHidePrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -0700954 if (mInForeground && mGeolocationPermissionsPrompt != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700955 mGeolocationPermissionsPrompt.hide();
956 }
957 }
958
Ben Murdoch65acc352009-11-19 18:16:04 +0000959 /* Adds a JavaScript error message to the system log and if the JS
960 * console is enabled in the about:debug options, to that console
961 * also.
Ben Murdochc42addf2010-01-28 15:19:59 +0000962 * @param consoleMessage the message object.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700963 */
964 @Override
Ben Murdochc42addf2010-01-28 15:19:59 +0000965 public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700966 if (mInForeground) {
967 // call getErrorConsole(true) so it will create one if needed
968 ErrorConsoleView errorConsole = getErrorConsole(true);
Ben Murdochc42addf2010-01-28 15:19:59 +0000969 errorConsole.addErrorMessage(consoleMessage);
Michael Kolb8233fac2010-10-26 16:08:53 -0700970 if (mWebViewController.shouldShowErrorConsole()
971 && errorConsole.getShowState() !=
972 ErrorConsoleView.SHOW_MAXIMIZED) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700973 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
974 }
975 }
Ben Murdochc42addf2010-01-28 15:19:59 +0000976
Jeff Hamilton47654f42010-09-07 09:57:51 -0500977 // Don't log console messages in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -0700978 if (isPrivateBrowsingEnabled()) return true;
Jeff Hamilton47654f42010-09-07 09:57:51 -0500979
Ben Murdochc42addf2010-01-28 15:19:59 +0000980 String message = "Console: " + consoleMessage.message() + " "
981 + consoleMessage.sourceId() + ":"
982 + consoleMessage.lineNumber();
983
984 switch (consoleMessage.messageLevel()) {
985 case TIP:
986 Log.v(CONSOLE_LOGTAG, message);
987 break;
988 case LOG:
989 Log.i(CONSOLE_LOGTAG, message);
990 break;
991 case WARNING:
992 Log.w(CONSOLE_LOGTAG, message);
993 break;
994 case ERROR:
995 Log.e(CONSOLE_LOGTAG, message);
996 break;
997 case DEBUG:
998 Log.d(CONSOLE_LOGTAG, message);
999 break;
1000 }
1001
1002 return true;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001003 }
1004
1005 /**
1006 * Ask the browser for an icon to represent a <video> element.
1007 * This icon will be used if the Web page did not specify a poster attribute.
1008 * @return Bitmap The icon or null if no such icon is available.
1009 */
1010 @Override
1011 public Bitmap getDefaultVideoPoster() {
1012 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001013 return mWebViewController.getDefaultVideoPoster();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001014 }
1015 return null;
1016 }
1017
1018 /**
1019 * Ask the host application for a custom progress view to show while
1020 * a <video> is loading.
1021 * @return View The progress view.
1022 */
1023 @Override
1024 public View getVideoLoadingProgressView() {
1025 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001026 return mWebViewController.getVideoLoadingProgressView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001027 }
1028 return null;
1029 }
1030
1031 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00001032 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001033 if (mInForeground) {
Ben Murdoch8cad4132012-01-11 10:56:43 +00001034 mWebViewController.openFileChooser(uploadMsg, acceptType, capture);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001035 } else {
1036 uploadMsg.onReceiveValue(null);
1037 }
1038 }
1039
Vivek Sekharb54614f2014-05-01 19:03:37 -07001040 @Override
1041 public void showFileChooser(ValueCallback<String[]> uploadFilePaths, String acceptTypes,
1042 boolean capture) {
1043 if (mInForeground) {
1044 mWebViewController.showFileChooser(uploadFilePaths, acceptTypes, capture);
1045 } else {
1046 uploadFilePaths.onReceiveValue(null);
1047 }
1048 }
1049
Grace Kloba22ac16e2009-10-07 18:00:23 -07001050 /**
1051 * Deliver a list of already-visited URLs
1052 */
1053 @Override
1054 public void getVisitedHistory(final ValueCallback<String[]> callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001055 mWebViewController.getVisitedHistory(callback);
1056 }
Ben Murdoch8029a772010-11-16 11:58:21 +00001057
1058 @Override
1059 public void setupAutoFill(Message message) {
1060 // Prompt the user to set up their profile.
1061 final Message msg = message;
Michael Kolb14612442011-06-24 13:06:29 -07001062 AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
1063 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
Ben Murdoch1d676b62011-01-17 12:54:24 +00001064 Context.LAYOUT_INFLATER_SERVICE);
1065 final View layout = inflater.inflate(R.layout.setup_autofill_dialog, null);
1066
1067 builder.setView(layout)
1068 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1069 @Override
1070 public void onClick(DialogInterface dialog, int id) {
1071 CheckBox disableAutoFill = (CheckBox) layout.findViewById(
1072 R.id.setup_autofill_dialog_disable_autofill);
1073
1074 if (disableAutoFill.isChecked()) {
1075 // Disable autofill and show a toast with how to turn it on again.
John Reck35e9dd62011-04-25 09:01:54 -07001076 mSettings.setAutofillEnabled(false);
Michael Kolb14612442011-06-24 13:06:29 -07001077 Toast.makeText(mContext,
Ben Murdoch1d676b62011-01-17 12:54:24 +00001078 R.string.autofill_setup_dialog_negative_toast,
1079 Toast.LENGTH_LONG).show();
1080 } else {
1081 // Take user to the AutoFill profile editor. When they return,
1082 // we will send the message that we pass here which will trigger
1083 // the form to get filled out with their new profile.
1084 mWebViewController.setupAutoFill(msg);
1085 }
1086 }
1087 })
1088 .setNegativeButton(R.string.cancel, null)
1089 .show();
Ben Murdoch8029a772010-11-16 11:58:21 +00001090 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001091 };
1092
1093 // -------------------------------------------------------------------------
1094 // WebViewClient implementation for the sub window
1095 // -------------------------------------------------------------------------
1096
1097 // Subclass of WebViewClient used in subwindows to notify the main
1098 // WebViewClient of certain WebView activities.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001099 private static class SubWindowClient extends WebViewClient {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001100 // The main WebViewClient.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001101 private final WebViewClient mClient;
Michael Kolb8233fac2010-10-26 16:08:53 -07001102 private final WebViewController mController;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001103
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001104 SubWindowClient(WebViewClient client, WebViewController controller) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001105 mClient = client;
Michael Kolb8233fac2010-10-26 16:08:53 -07001106 mController = controller;
Leon Scroggins III211ba542010-04-19 13:21:13 -04001107 }
1108 @Override
1109 public void onPageStarted(WebView view, String url, Bitmap favicon) {
1110 // Unlike the others, do not call mClient's version, which would
1111 // change the progress bar. However, we do want to remove the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001112 // find or select dialog.
Michael Kolb8233fac2010-10-26 16:08:53 -07001113 mController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001114 }
1115 @Override
1116 public void doUpdateVisitedHistory(WebView view, String url,
1117 boolean isReload) {
1118 mClient.doUpdateVisitedHistory(view, url, isReload);
1119 }
1120 @Override
1121 public boolean shouldOverrideUrlLoading(WebView view, String url) {
1122 return mClient.shouldOverrideUrlLoading(view, url);
1123 }
1124 @Override
1125 public void onReceivedSslError(WebView view, SslErrorHandler handler,
1126 SslError error) {
1127 mClient.onReceivedSslError(view, handler, error);
1128 }
1129 @Override
Brian Carlstrom8862c1d2011-06-02 01:05:55 -07001130 public void onReceivedClientCertRequest(WebView view,
1131 ClientCertRequestHandler handler, String host_and_port) {
1132 mClient.onReceivedClientCertRequest(view, handler, host_and_port);
1133 }
1134 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -07001135 public void onReceivedHttpAuthRequest(WebView view,
1136 HttpAuthHandler handler, String host, String realm) {
1137 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
1138 }
1139 @Override
1140 public void onFormResubmission(WebView view, Message dontResend,
1141 Message resend) {
1142 mClient.onFormResubmission(view, dontResend, resend);
1143 }
1144 @Override
1145 public void onReceivedError(WebView view, int errorCode,
1146 String description, String failingUrl) {
1147 mClient.onReceivedError(view, errorCode, description, failingUrl);
1148 }
1149 @Override
1150 public boolean shouldOverrideKeyEvent(WebView view,
1151 android.view.KeyEvent event) {
1152 return mClient.shouldOverrideKeyEvent(view, event);
1153 }
1154 @Override
1155 public void onUnhandledKeyEvent(WebView view,
1156 android.view.KeyEvent event) {
1157 mClient.onUnhandledKeyEvent(view, event);
1158 }
1159 }
1160
1161 // -------------------------------------------------------------------------
1162 // WebChromeClient implementation for the sub window
1163 // -------------------------------------------------------------------------
1164
1165 private class SubWindowChromeClient extends WebChromeClient {
1166 // The main WebChromeClient.
1167 private final WebChromeClient mClient;
1168
1169 SubWindowChromeClient(WebChromeClient client) {
1170 mClient = client;
1171 }
1172 @Override
1173 public void onProgressChanged(WebView view, int newProgress) {
1174 mClient.onProgressChanged(view, newProgress);
1175 }
1176 @Override
1177 public boolean onCreateWindow(WebView view, boolean dialog,
1178 boolean userGesture, android.os.Message resultMsg) {
1179 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
1180 }
1181 @Override
1182 public void onCloseWindow(WebView window) {
1183 if (window != mSubView) {
1184 Log.e(LOGTAG, "Can't close the window");
1185 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001186 mWebViewController.dismissSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001187 }
1188 }
1189
1190 // -------------------------------------------------------------------------
1191
1192 // Construct a new tab
Michael Kolb7bcafde2011-05-09 13:55:59 -07001193 Tab(WebViewController wvcontroller, WebView w) {
John Reck1cf4b792011-07-26 10:22:22 -07001194 this(wvcontroller, w, null);
1195 }
1196
1197 Tab(WebViewController wvcontroller, Bundle state) {
1198 this(wvcontroller, null, state);
1199 }
1200
1201 Tab(WebViewController wvcontroller, WebView w, Bundle state) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001202 mWebViewController = wvcontroller;
Michael Kolb14612442011-06-24 13:06:29 -07001203 mContext = mWebViewController.getContext();
John Reck35e9dd62011-04-25 09:01:54 -07001204 mSettings = BrowserSettings.getInstance();
Michael Kolb14612442011-06-24 13:06:29 -07001205 mDataController = DataController.getInstance(mContext);
1206 mCurrentState = new PageState(mContext, w != null
John Reck847b5322011-04-14 17:02:18 -07001207 ? w.isPrivateBrowsingEnabled() : false);
Michael Kolb8233fac2010-10-26 16:08:53 -07001208 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001209 mInForeground = false;
1210
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001211 mDownloadListener = new BrowserDownloadListener() {
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001212 public void onDownloadStart(String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001213 String contentDisposition, String mimetype, String referer,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001214 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001215 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001216 mimetype, referer, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001217 }
1218 };
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001219 mWebBackForwardListClient = new WebBackForwardListClient() {
1220 @Override
1221 public void onNewHistoryItem(WebHistoryItem item) {
Mathew Inwooda829d552011-09-02 14:16:25 +01001222 if (mClearHistoryUrlPattern != null) {
1223 boolean match =
1224 mClearHistoryUrlPattern.matcher(item.getOriginalUrl()).matches();
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001225 if (LOGD_ENABLED) {
Mathew Inwooda829d552011-09-02 14:16:25 +01001226 Log.d(LOGTAG, "onNewHistoryItem: match=" + match + "\n\t"
1227 + item.getUrl() + "\n\t"
1228 + mClearHistoryUrlPattern);
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001229 }
Mathew Inwooda829d552011-09-02 14:16:25 +01001230 if (match) {
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001231 if (mMainView != null) {
1232 mMainView.clearHistory();
1233 }
1234 }
Mathew Inwooda829d552011-09-02 14:16:25 +01001235 mClearHistoryUrlPattern = null;
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001236 }
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001237 }
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001238 };
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001239
John Reck1cf4b792011-07-26 10:22:22 -07001240 mCaptureWidth = mContext.getResources().getDimensionPixelSize(
1241 R.dimen.tab_thumbnail_width);
1242 mCaptureHeight = mContext.getResources().getDimensionPixelSize(
1243 R.dimen.tab_thumbnail_height);
1244 updateShouldCaptureThumbnails();
1245 restoreState(state);
John Reck52be4782011-08-26 15:37:29 -07001246 if (getId() == -1) {
1247 mId = TabControl.getNextId();
1248 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001249 setWebView(w);
Michael Kolb9ef259a2011-07-12 15:33:08 -07001250 mHandler = new Handler() {
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001251 @Override
Michael Kolb9ef259a2011-07-12 15:33:08 -07001252 public void handleMessage(Message m) {
John Reck1cf4b792011-07-26 10:22:22 -07001253 switch (m.what) {
1254 case MSG_CAPTURE:
1255 capture();
1256 break;
1257 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001258 }
1259 };
John Reck1cf4b792011-07-26 10:22:22 -07001260 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001261
Michael Kolb72864272012-05-03 15:42:15 -07001262 public boolean shouldUpdateThumbnail() {
1263 return mUpdateThumbnail;
1264 }
1265
Mathew Inwoode09305e2011-09-02 12:03:26 +01001266 /**
1267 * This is used to get a new ID when the tab has been preloaded, before it is displayed and
1268 * added to TabControl. Preloaded tabs can be created before restoreInstanceState, leading
1269 * to overlapping IDs between the preloaded and restored tabs.
1270 */
1271 public void refreshIdAfterPreload() {
1272 mId = TabControl.getNextId();
1273 }
1274
John Reck1cf4b792011-07-26 10:22:22 -07001275 public void updateShouldCaptureThumbnails() {
1276 if (mWebViewController.shouldCaptureThumbnails()) {
1277 synchronized (Tab.this) {
1278 if (mCapture == null) {
1279 mCapture = Bitmap.createBitmap(mCaptureWidth, mCaptureHeight,
1280 Bitmap.Config.RGB_565);
Michael Kolbc3af0672011-08-09 10:24:41 -07001281 mCapture.eraseColor(Color.WHITE);
John Reck1cf4b792011-07-26 10:22:22 -07001282 if (mInForeground) {
1283 postCapture();
1284 }
1285 }
1286 }
1287 } else {
1288 synchronized (Tab.this) {
1289 mCapture = null;
1290 deleteThumbnail();
1291 }
1292 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001293 }
1294
Michael Kolb14612442011-06-24 13:06:29 -07001295 public void setController(WebViewController ctl) {
1296 mWebViewController = ctl;
John Reck1cf4b792011-07-26 10:22:22 -07001297 updateShouldCaptureThumbnails();
Michael Kolb14612442011-06-24 13:06:29 -07001298 }
1299
Michael Kolbc831b632011-05-11 09:30:34 -07001300 public long getId() {
1301 return mId;
1302 }
1303
Michael Kolb91911a22012-01-17 11:21:25 -08001304 void setWebView(WebView w) {
1305 setWebView(w, true);
1306 }
1307
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001308 public boolean isNativeActive(){
1309 if (mMainView == null)
1310 return false;
1311 return true;
1312 }
1313
1314 public void setTimeStamp(){
1315 Date d = new Date();
1316 timestamp = (new Timestamp(d.getTime()));
1317 }
1318
1319 public Timestamp getTimestamp() {
1320 return timestamp;
1321 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001322 /**
1323 * Sets the WebView for this tab, correctly removing the old WebView from
1324 * the container view.
1325 */
Michael Kolb91911a22012-01-17 11:21:25 -08001326 void setWebView(WebView w, boolean restore) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001327 if (mMainView == w) {
1328 return;
1329 }
Michael Kolba713ec82010-11-29 17:27:06 -08001330
Grace Kloba22ac16e2009-10-07 18:00:23 -07001331 // If the WebView is changing, the page will be reloaded, so any ongoing
1332 // Geolocation permission requests are void.
Grace Kloba50c241e2010-04-20 11:07:50 -07001333 if (mGeolocationPermissionsPrompt != null) {
1334 mGeolocationPermissionsPrompt.hide();
1335 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001336
Michael Kolba713ec82010-11-29 17:27:06 -08001337 mWebViewController.onSetWebView(this, w);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001338
John Reck1cf4b792011-07-26 10:22:22 -07001339 if (mMainView != null) {
John Reck8ee633f2011-08-09 16:00:35 -07001340 mMainView.setPictureListener(null);
John Reck1cf4b792011-07-26 10:22:22 -07001341 if (w != null) {
1342 syncCurrentState(w, null);
1343 } else {
Panos Thomasa9a5a582014-03-18 19:20:08 -07001344 mCurrentState = new PageState(mContext, mMainView.isPrivateBrowsingEnabled());
John Reck1cf4b792011-07-26 10:22:22 -07001345 }
1346 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001347 // set the new one
1348 mMainView = w;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001349 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001350 if (mMainView != null) {
1351 mMainView.setWebViewClient(mWebViewClient);
1352 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001353 // Attach DownloadManager so that downloads can start in an active
1354 // or a non-active window. This can happen when going to a site that
1355 // does a redirect after a period of time. The user could have
1356 // switched to another tab while waiting for the download to start.
1357 mMainView.setDownloadListener(mDownloadListener);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001358 getWebView().setWebBackForwardListClient(mWebBackForwardListClient);
John Reck8ee633f2011-08-09 16:00:35 -07001359 TabControl tc = mWebViewController.getTabControl();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001360 if (tc != null /*&& tc.getOnThumbnailUpdatedListener() != null*/) {
John Reck8ee633f2011-08-09 16:00:35 -07001361 mMainView.setPictureListener(this);
1362 }
Michael Kolb91911a22012-01-17 11:21:25 -08001363 if (restore && (mSavedState != null)) {
John Reck8b9bb8b2012-03-08 13:19:40 -08001364 restoreUserAgent();
John Reck6c2e2f32011-08-22 13:41:23 -07001365 WebBackForwardList restoredState
1366 = mMainView.restoreState(mSavedState);
1367 if (restoredState == null || restoredState.getSize() == 0) {
1368 Log.w(LOGTAG, "Failed to restore WebView state!");
1369 loadUrl(mCurrentState.mOriginalUrl, null);
1370 }
John Reck1cf4b792011-07-26 10:22:22 -07001371 mSavedState = null;
1372 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001373 }
1374 }
1375
1376 /**
1377 * Destroy the tab's main WebView and subWindow if any
1378 */
1379 void destroy() {
Matthew Huib7f2e9c2014-04-16 11:12:37 -04001380 if (mPostponeDestroy) {
1381 mShouldDestroy = true;
1382 return;
1383 }
1384 mShouldDestroy = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001385 if (mMainView != null) {
1386 dismissSubWindow();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001387 // save the WebView to call destroy() after detach it from the tab
1388 WebView webView = mMainView;
1389 setWebView(null);
1390 webView.destroy();
1391 }
1392 }
1393
Matthew Huib7f2e9c2014-04-16 11:12:37 -04001394 private boolean mPostponeDestroy = false;
1395 private boolean mShouldDestroy = false;
1396
1397 public void postponeDestroy() {
1398 mPostponeDestroy = true;
1399 }
1400
1401 public void performPostponedDestroy() {
1402 mPostponeDestroy = false;
1403 if (mShouldDestroy) {
1404 destroy();
1405 }
1406 }
1407
Grace Kloba22ac16e2009-10-07 18:00:23 -07001408 /**
1409 * Remove the tab from the parent
1410 */
1411 void removeFromTree() {
1412 // detach the children
Michael Kolbc831b632011-05-11 09:30:34 -07001413 if (mChildren != null) {
1414 for(Tab t : mChildren) {
1415 t.setParent(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001416 }
1417 }
1418 // remove itself from the parent list
Michael Kolbc831b632011-05-11 09:30:34 -07001419 if (mParent != null) {
1420 mParent.mChildren.remove(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001421 }
John Reck1cf4b792011-07-26 10:22:22 -07001422 deleteThumbnail();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001423 }
1424
1425 /**
1426 * Create a new subwindow unless a subwindow already exists.
1427 * @return True if a new subwindow was created. False if one already exists.
1428 */
1429 boolean createSubWindow() {
1430 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001431 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001432 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001433 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001434 mSubView.setWebChromeClient(new SubWindowChromeClient(
1435 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001436 // Set a different DownloadListener for the mSubView, since it will
1437 // just need to dismiss the mSubView, rather than close the Tab
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001438 mSubView.setDownloadListener(new BrowserDownloadListener() {
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001439 public void onDownloadStart(String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001440 String contentDisposition, String mimetype, String referer,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001441 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001442 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001443 contentDisposition, mimetype, referer, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001444 if (mSubView.copyBackForwardList().getSize() == 0) {
1445 // This subwindow was opened for the sole purpose of
1446 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001447 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001448 }
1449 }
1450 });
Michael Kolb14612442011-06-24 13:06:29 -07001451 mSubView.setOnCreateContextMenuListener(mWebViewController.getActivity());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001452 return true;
1453 }
1454 return false;
1455 }
1456
1457 /**
1458 * Dismiss the subWindow for the tab.
1459 */
1460 void dismissSubWindow() {
1461 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001462 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001463 mSubView.destroy();
1464 mSubView = null;
1465 mSubViewContainer = null;
1466 }
1467 }
1468
Grace Kloba22ac16e2009-10-07 18:00:23 -07001469
1470 /**
1471 * Set the parent tab of this tab.
1472 */
Michael Kolbc831b632011-05-11 09:30:34 -07001473 void setParent(Tab parent) {
John Reck52be4782011-08-26 15:37:29 -07001474 if (parent == this) {
1475 throw new IllegalStateException("Cannot set parent to self!");
1476 }
Michael Kolbc831b632011-05-11 09:30:34 -07001477 mParent = parent;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001478 // This tab may have been freed due to low memory. If that is the case,
Michael Kolbc831b632011-05-11 09:30:34 -07001479 // the parent tab id is already saved. If we are changing that id
Grace Kloba22ac16e2009-10-07 18:00:23 -07001480 // (most likely due to removing the parent tab) we must update the
Michael Kolbc831b632011-05-11 09:30:34 -07001481 // parent tab id in the saved Bundle.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001482 if (mSavedState != null) {
1483 if (parent == null) {
1484 mSavedState.remove(PARENTTAB);
1485 } else {
Michael Kolbc831b632011-05-11 09:30:34 -07001486 mSavedState.putLong(PARENTTAB, parent.getId());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001487 }
1488 }
John Reckb0a86db2011-05-24 14:05:58 -07001489
1490 // Sync the WebView useragent with the parent
1491 if (parent != null && mSettings.hasDesktopUseragent(parent.getWebView())
1492 != mSettings.hasDesktopUseragent(getWebView())) {
1493 mSettings.toggleDesktopUseragent(getWebView());
1494 }
John Reck52be4782011-08-26 15:37:29 -07001495
1496 if (parent != null && parent.getId() == getId()) {
1497 throw new IllegalStateException("Parent has same ID as child!");
1498 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001499 }
1500
1501 /**
Michael Kolbc831b632011-05-11 09:30:34 -07001502 * If this Tab was created through another Tab, then this method returns
1503 * that Tab.
1504 * @return the Tab parent or null
1505 */
1506 public Tab getParent() {
1507 return mParent;
1508 }
1509
1510 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001511 * When a Tab is created through the content of another Tab, then we
1512 * associate the Tabs.
1513 * @param child the Tab that was created from this Tab
1514 */
1515 void addChildTab(Tab child) {
Michael Kolbc831b632011-05-11 09:30:34 -07001516 if (mChildren == null) {
1517 mChildren = new Vector<Tab>();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001518 }
Michael Kolbc831b632011-05-11 09:30:34 -07001519 mChildren.add(child);
1520 child.setParent(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001521 }
1522
Michael Kolbc831b632011-05-11 09:30:34 -07001523 Vector<Tab> getChildren() {
1524 return mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001525 }
1526
1527 void resume() {
1528 if (mMainView != null) {
John Reck56c1fcf2011-08-17 10:15:16 -07001529 setupHwAcceleration(mMainView);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001530 mMainView.onResume();
1531 if (mSubView != null) {
1532 mSubView.onResume();
1533 }
1534 }
1535 }
1536
John Reck56c1fcf2011-08-17 10:15:16 -07001537 private void setupHwAcceleration(View web) {
1538 if (web == null) return;
1539 BrowserSettings settings = BrowserSettings.getInstance();
1540 if (settings.isHardwareAccelerated()) {
1541 web.setLayerType(View.LAYER_TYPE_NONE, null);
1542 } else {
1543 web.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
1544 }
1545 }
1546
Grace Kloba22ac16e2009-10-07 18:00:23 -07001547 void pause() {
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001548 capture();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001549 if (mMainView != null) {
1550 mMainView.onPause();
1551 if (mSubView != null) {
1552 mSubView.onPause();
1553 }
1554 }
1555 }
1556
1557 void putInForeground() {
John Reck8ee633f2011-08-09 16:00:35 -07001558 if (mInForeground) {
1559 return;
1560 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001561 mInForeground = true;
1562 resume();
Michael Kolb14612442011-06-24 13:06:29 -07001563 Activity activity = mWebViewController.getActivity();
1564 mMainView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001565 if (mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -07001566 mSubView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001567 }
1568 // Show the pending error dialog if the queue is not empty
1569 if (mQueuedErrors != null && mQueuedErrors.size() > 0) {
1570 showError(mQueuedErrors.getFirst());
1571 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001572 mWebViewController.bookmarkedStatusHasChanged(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001573 }
1574
1575 void putInBackground() {
John Reck8ee633f2011-08-09 16:00:35 -07001576 if (!mInForeground) {
1577 return;
1578 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001579 mInForeground = false;
1580 pause();
1581 mMainView.setOnCreateContextMenuListener(null);
1582 if (mSubView != null) {
1583 mSubView.setOnCreateContextMenuListener(null);
1584 }
1585 }
1586
Michael Kolb8233fac2010-10-26 16:08:53 -07001587 boolean inForeground() {
1588 return mInForeground;
1589 }
1590
Grace Kloba22ac16e2009-10-07 18:00:23 -07001591 /**
1592 * Return the top window of this tab; either the subwindow if it is not
1593 * null or the main window.
1594 * @return The top window of this tab.
1595 */
1596 WebView getTopWindow() {
1597 if (mSubView != null) {
1598 return mSubView;
1599 }
1600 return mMainView;
1601 }
1602
1603 /**
1604 * Return the main window of this tab. Note: if a tab is freed in the
1605 * background, this can return null. It is only guaranteed to be
1606 * non-null for the current tab.
1607 * @return The main WebView of this tab.
1608 */
1609 WebView getWebView() {
1610 return mMainView;
1611 }
1612
Michael Kolba713ec82010-11-29 17:27:06 -08001613 void setViewContainer(View container) {
1614 mContainer = container;
1615 }
1616
Michael Kolb8233fac2010-10-26 16:08:53 -07001617 View getViewContainer() {
1618 return mContainer;
1619 }
1620
Grace Kloba22ac16e2009-10-07 18:00:23 -07001621 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001622 * Return whether private browsing is enabled for the main window of
1623 * this tab.
1624 * @return True if private browsing is enabled.
1625 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001626 boolean isPrivateBrowsingEnabled() {
John Reck502a3532011-08-16 14:21:46 -07001627 return mCurrentState.mIncognito;
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001628 }
1629
1630 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001631 * Return the subwindow of this tab or null if there is no subwindow.
1632 * @return The subwindow of this tab or null.
1633 */
1634 WebView getSubWebView() {
1635 return mSubView;
1636 }
1637
Michael Kolb1514bb72010-11-22 09:11:48 -08001638 void setSubWebView(WebView subView) {
1639 mSubView = subView;
1640 }
1641
Michael Kolb8233fac2010-10-26 16:08:53 -07001642 View getSubViewContainer() {
1643 return mSubViewContainer;
1644 }
1645
Michael Kolb1514bb72010-11-22 09:11:48 -08001646 void setSubViewContainer(View subViewContainer) {
1647 mSubViewContainer = subViewContainer;
1648 }
1649
Grace Kloba22ac16e2009-10-07 18:00:23 -07001650 /**
1651 * @return The geolocation permissions prompt for this tab.
1652 */
1653 GeolocationPermissionsPrompt getGeolocationPermissionsPrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001654 if (mGeolocationPermissionsPrompt == null) {
1655 ViewStub stub = (ViewStub) mContainer
1656 .findViewById(R.id.geolocation_permissions_prompt);
1657 mGeolocationPermissionsPrompt = (GeolocationPermissionsPrompt) stub
1658 .inflate();
Grace Kloba50c241e2010-04-20 11:07:50 -07001659 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001660 return mGeolocationPermissionsPrompt;
1661 }
1662
1663 /**
1664 * @return The application id string
1665 */
1666 String getAppId() {
1667 return mAppId;
1668 }
1669
1670 /**
1671 * Set the application id string
1672 * @param id
1673 */
1674 void setAppId(String id) {
1675 mAppId = id;
1676 }
1677
Michael Kolbe28b3472011-08-04 16:54:31 -07001678 boolean closeOnBack() {
1679 return mCloseOnBack;
1680 }
1681
1682 void setCloseOnBack(boolean close) {
1683 mCloseOnBack = close;
1684 }
1685
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001686 boolean getDerivedFromIntent() {
1687 return mDerivedFromIntent;
1688 }
1689
1690 void setDerivedFromIntent(boolean derived) {
1691 mDerivedFromIntent = derived;
1692 }
1693
Grace Kloba22ac16e2009-10-07 18:00:23 -07001694 String getUrl() {
John Reck324d4402011-01-11 16:56:42 -08001695 return UrlUtils.filteredUrl(mCurrentState.mUrl);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001696 }
1697
John Reck49a603c2011-03-03 09:33:05 -08001698 String getOriginalUrl() {
John Reckdb22ec42011-06-29 11:31:24 -07001699 if (mCurrentState.mOriginalUrl == null) {
1700 return getUrl();
John Reck49a603c2011-03-03 09:33:05 -08001701 }
John Reckdb22ec42011-06-29 11:31:24 -07001702 return UrlUtils.filteredUrl(mCurrentState.mOriginalUrl);
John Reck49a603c2011-03-03 09:33:05 -08001703 }
1704
Grace Kloba22ac16e2009-10-07 18:00:23 -07001705 /**
John Reck30c714c2010-12-16 17:30:34 -08001706 * Get the title of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001707 */
1708 String getTitle() {
John Reck30c714c2010-12-16 17:30:34 -08001709 if (mCurrentState.mTitle == null && mInPageLoad) {
Michael Kolb14612442011-06-24 13:06:29 -07001710 return mContext.getString(R.string.title_bar_loading);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001711 }
John Reck30c714c2010-12-16 17:30:34 -08001712 return mCurrentState.mTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001713 }
1714
1715 /**
John Reck30c714c2010-12-16 17:30:34 -08001716 * Get the favicon of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001717 */
1718 Bitmap getFavicon() {
John Reck1cf4b792011-07-26 10:22:22 -07001719 if (mCurrentState.mFavicon != null) {
1720 return mCurrentState.mFavicon;
1721 }
1722 return getDefaultFavicon(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001723 }
1724
John Recke969cc52010-12-21 17:24:43 -08001725 public boolean isBookmarkedSite() {
1726 return mCurrentState.mIsBookmarkedSite;
1727 }
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001728
Grace Kloba22ac16e2009-10-07 18:00:23 -07001729 /**
1730 * Return the tab's error console. Creates the console if createIfNEcessary
1731 * is true and we haven't already created the console.
1732 * @param createIfNecessary Flag to indicate if the console should be
1733 * created if it has not been already.
1734 * @return The tab's error console, or null if one has not been created and
1735 * createIfNecessary is false.
1736 */
1737 ErrorConsoleView getErrorConsole(boolean createIfNecessary) {
1738 if (createIfNecessary && mErrorConsole == null) {
Michael Kolb14612442011-06-24 13:06:29 -07001739 mErrorConsole = new ErrorConsoleView(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001740 mErrorConsole.setWebView(mMainView);
1741 }
1742 return mErrorConsole;
1743 }
1744
Steve Block08a6f0c2011-10-06 12:12:53 +01001745 /**
1746 * Sets the security state, clears the SSL certificate error and informs
1747 * the controller.
1748 */
Steve Block2466eff2011-10-03 15:33:09 +01001749 private void setSecurityState(SecurityState securityState) {
1750 mCurrentState.mSecurityState = securityState;
Steve Block08a6f0c2011-10-06 12:12:53 +01001751 mCurrentState.mSslCertificateError = null;
Steve Block2466eff2011-10-03 15:33:09 +01001752 mWebViewController.onUpdatedSecurityState(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001753 }
1754
1755 /**
Steve Block2466eff2011-10-03 15:33:09 +01001756 * @return The tab's security state.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001757 */
Steve Block2466eff2011-10-03 15:33:09 +01001758 SecurityState getSecurityState() {
1759 return mCurrentState.mSecurityState;
John Reck30c714c2010-12-16 17:30:34 -08001760 }
1761
Steve Block08a6f0c2011-10-06 12:12:53 +01001762 /**
1763 * Gets the SSL certificate error, if any, for the page's main resource.
1764 * This is only non-null when the security state is
1765 * SECURITY_STATE_BAD_CERTIFICATE.
1766 */
1767 SslError getSslCertificateError() {
1768 return mCurrentState.mSslCertificateError;
1769 }
1770
John Reck30c714c2010-12-16 17:30:34 -08001771 int getLoadProgress() {
1772 if (mInPageLoad) {
1773 return mPageLoadProgress;
1774 }
1775 return 100;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001776 }
1777
1778 /**
1779 * @return TRUE if onPageStarted is called while onPageFinished is not
1780 * called yet.
1781 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001782 boolean inPageLoad() {
1783 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001784 }
1785
Grace Kloba22ac16e2009-10-07 18:00:23 -07001786 /**
John Reck1cf4b792011-07-26 10:22:22 -07001787 * @return The Bundle with the tab's state if it can be saved, otherwise null
Grace Kloba22ac16e2009-10-07 18:00:23 -07001788 */
John Reck1cf4b792011-07-26 10:22:22 -07001789 public Bundle saveState() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001790 // If the WebView is null it means we ran low on memory and we already
1791 // stored the saved state in mSavedState.
1792 if (mMainView == null) {
John Reck1cf4b792011-07-26 10:22:22 -07001793 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001794 }
John Reck6c2e2f32011-08-22 13:41:23 -07001795
1796 if (TextUtils.isEmpty(mCurrentState.mUrl)) {
John Reck1cf4b792011-07-26 10:22:22 -07001797 return null;
John Reck24f18262011-06-17 14:47:20 -07001798 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001799
1800 mSavedState = new Bundle();
John Reck6c2e2f32011-08-22 13:41:23 -07001801 WebBackForwardList savedList = mMainView.saveState(mSavedState);
1802 if (savedList == null || savedList.getSize() == 0) {
1803 Log.w(LOGTAG, "Failed to save back/forward list for "
1804 + mCurrentState.mUrl);
1805 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001806
Michael Kolbc831b632011-05-11 09:30:34 -07001807 mSavedState.putLong(ID, mId);
John Reck30c714c2010-12-16 17:30:34 -08001808 mSavedState.putString(CURRURL, mCurrentState.mUrl);
1809 mSavedState.putString(CURRTITLE, mCurrentState.mTitle);
John Reck1cf4b792011-07-26 10:22:22 -07001810 mSavedState.putBoolean(INCOGNITO, mMainView.isPrivateBrowsingEnabled());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001811 if (mAppId != null) {
1812 mSavedState.putString(APPID, mAppId);
1813 }
Michael Kolbe28b3472011-08-04 16:54:31 -07001814 mSavedState.putBoolean(CLOSEFLAG, mCloseOnBack);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001815 // Remember the parent tab so the relationship can be restored.
Michael Kolbc831b632011-05-11 09:30:34 -07001816 if (mParent != null) {
1817 mSavedState.putLong(PARENTTAB, mParent.mId);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001818 }
John Reckb0a86db2011-05-24 14:05:58 -07001819 mSavedState.putBoolean(USERAGENT,
1820 mSettings.hasDesktopUseragent(getWebView()));
John Reck1cf4b792011-07-26 10:22:22 -07001821 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001822 }
1823
1824 /*
1825 * Restore the state of the tab.
1826 */
John Reck1cf4b792011-07-26 10:22:22 -07001827 private void restoreState(Bundle b) {
1828 mSavedState = b;
1829 if (mSavedState == null) {
1830 return;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001831 }
1832 // Restore the internal state even if the WebView fails to restore.
1833 // This will maintain the app id, original url and close-on-exit values.
Michael Kolbc831b632011-05-11 09:30:34 -07001834 mId = b.getLong(ID);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001835 mAppId = b.getString(APPID);
Michael Kolbe28b3472011-08-04 16:54:31 -07001836 mCloseOnBack = b.getBoolean(CLOSEFLAG);
John Reck8b9bb8b2012-03-08 13:19:40 -08001837 restoreUserAgent();
John Reck1cf4b792011-07-26 10:22:22 -07001838 String url = b.getString(CURRURL);
1839 String title = b.getString(CURRTITLE);
1840 boolean incognito = b.getBoolean(INCOGNITO);
1841 mCurrentState = new PageState(mContext, incognito, url, null);
1842 mCurrentState.mTitle = title;
1843 synchronized (Tab.this) {
1844 if (mCapture != null) {
John Reck4eadc342011-10-31 14:04:10 -07001845 DataController.getInstance(mContext).loadThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07001846 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001847 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001848 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04001849
John Reck8b9bb8b2012-03-08 13:19:40 -08001850 private void restoreUserAgent() {
1851 if (mMainView == null || mSavedState == null) {
1852 return;
1853 }
1854 if (mSavedState.getBoolean(USERAGENT)
1855 != mSettings.hasDesktopUseragent(mMainView)) {
1856 mSettings.toggleDesktopUseragent(mMainView);
1857 }
1858 }
1859
Leon Scroggins1961ed22010-12-07 15:22:21 -05001860 public void updateBookmarkedStatus() {
John Recke969cc52010-12-21 17:24:43 -08001861 mDataController.queryBookmarkStatus(getUrl(), mIsBookmarkCallback);
Leon Scroggins1961ed22010-12-07 15:22:21 -05001862 }
1863
John Recke969cc52010-12-21 17:24:43 -08001864 private DataController.OnQueryUrlIsBookmark mIsBookmarkCallback
1865 = new DataController.OnQueryUrlIsBookmark() {
1866 @Override
1867 public void onQueryUrlIsBookmark(String url, boolean isBookmark) {
1868 if (mCurrentState.mUrl.equals(url)) {
1869 mCurrentState.mIsBookmarkedSite = isBookmark;
1870 mWebViewController.bookmarkedStatusHasChanged(Tab.this);
1871 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001872 }
John Recke969cc52010-12-21 17:24:43 -08001873 };
Michael Kolb1acef692011-03-08 14:12:06 -08001874
Michael Kolbeb95db42011-03-03 10:38:40 -08001875 public Bitmap getScreenshot() {
John Reck1cf4b792011-07-26 10:22:22 -07001876 synchronized (Tab.this) {
1877 return mCapture;
1878 }
Michael Kolbeb95db42011-03-03 10:38:40 -08001879 }
1880
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001881 public Bitmap getFullScreenshot() {
1882 synchronized (Tab.this) {
1883 return mScreenShot;
1884 }
1885 }
1886
John Reck541f55a2011-06-07 16:34:43 -07001887 public boolean isSnapshot() {
John Reck541f55a2011-06-07 16:34:43 -07001888 return false;
1889 }
1890
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001891 private static class SaveCallback implements ValueCallback<String> {
1892 boolean onReceiveValueCalled = false;
1893 private String mPath;
John Reck68234a92012-04-19 15:27:12 -07001894
1895 @Override
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001896 public void onReceiveValue(String path) {
1897 this.onReceiveValueCalled = true;
1898 this.mPath = path;
John Reck68234a92012-04-19 15:27:12 -07001899 synchronized (this) {
1900 notifyAll();
John Reck8cc92352011-07-06 17:41:52 -07001901 }
John Reck541f55a2011-06-07 16:34:43 -07001902 }
John Reck68234a92012-04-19 15:27:12 -07001903
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001904 public String getPath() {
1905 return mPath;
1906 }
John Reck68234a92012-04-19 15:27:12 -07001907 }
1908
1909 /**
1910 * Must be called on the UI thread
1911 */
1912 public ContentValues createSnapshotValues() {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001913 WebView web = getWebView();
John Reck68234a92012-04-19 15:27:12 -07001914 if (web == null) return null;
John Reckd8c74522011-06-14 08:45:00 -07001915 ContentValues values = new ContentValues();
1916 values.put(Snapshots.TITLE, mCurrentState.mTitle);
1917 values.put(Snapshots.URL, mCurrentState.mUrl);
John Reck68234a92012-04-19 15:27:12 -07001918 values.put(Snapshots.BACKGROUND, web.getPageBackgroundColor());
John Reck8cc92352011-07-06 17:41:52 -07001919 values.put(Snapshots.DATE_CREATED, System.currentTimeMillis());
1920 values.put(Snapshots.FAVICON, compressBitmap(getFavicon()));
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001921 Bitmap screenshot = web.getViewportBitmap();
John Reck8cc92352011-07-06 17:41:52 -07001922 values.put(Snapshots.THUMBNAIL, compressBitmap(screenshot));
John Reckd8c74522011-06-14 08:45:00 -07001923 return values;
John Reck541f55a2011-06-07 16:34:43 -07001924 }
1925
John Reck68234a92012-04-19 15:27:12 -07001926 /**
1927 * Probably want to call this on a background thread
1928 */
1929 public boolean saveViewState(ContentValues values) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001930 WebView web = getWebView();
John Reck68234a92012-04-19 15:27:12 -07001931 if (web == null) return false;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001932 String filename = UUID.randomUUID().toString();
John Reck68234a92012-04-19 15:27:12 -07001933 SaveCallback callback = new SaveCallback();
John Reck68234a92012-04-19 15:27:12 -07001934 try {
John Reck68234a92012-04-19 15:27:12 -07001935 synchronized (callback) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001936 web.saveViewState(filename, callback);
1937 callback.wait();
John Reck68234a92012-04-19 15:27:12 -07001938 }
John Reck68234a92012-04-19 15:27:12 -07001939 } catch (Exception e) {
1940 Log.w(LOGTAG, "Failed to save view state", e);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001941 String path = callback.getPath();
1942 if (path != null) {
1943 File file = mContext.getFileStreamPath(path);
1944 if (file.exists() && !file.delete()) {
1945 file.deleteOnExit();
1946 }
John Reck68234a92012-04-19 15:27:12 -07001947 }
1948 return false;
1949 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001950 String path = callback.getPath();
1951 File savedFile = new File(path);
1952 if (!savedFile.exists()) {
1953 return false;
John Reck68234a92012-04-19 15:27:12 -07001954 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001955 values.put(Snapshots.VIEWSTATE_PATH, path.substring(path.lastIndexOf('/') + 1));
1956 values.put(Snapshots.VIEWSTATE_SIZE, savedFile.length());
John Reck68234a92012-04-19 15:27:12 -07001957 return true;
1958 }
1959
John Reck8cc92352011-07-06 17:41:52 -07001960 public byte[] compressBitmap(Bitmap bitmap) {
1961 if (bitmap == null) {
1962 return null;
1963 }
1964 ByteArrayOutputStream stream = new ByteArrayOutputStream();
1965 bitmap.compress(CompressFormat.PNG, 100, stream);
1966 return stream.toByteArray();
1967 }
1968
John Reck26b18322011-06-21 13:08:58 -07001969 public void loadUrl(String url, Map<String, String> headers) {
1970 if (mMainView != null) {
Michael Kolba53c9892011-10-05 13:31:40 -07001971 mPageLoadProgress = INITIAL_PROGRESS;
1972 mInPageLoad = true;
Michael Kolb14612442011-06-24 13:06:29 -07001973 mCurrentState = new PageState(mContext, false, url, null);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001974 handleJsInterface(mMainView, url);
John Reck26b18322011-06-21 13:08:58 -07001975 mWebViewController.onPageStarted(this, mMainView, null);
1976 mMainView.loadUrl(url, headers);
1977 }
1978 }
1979
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001980 public void handleJsInterface(WebView webview, String url){
1981 if (url != null &&
1982 url.equals(mContext.getResources().getString(R.string.homepage_base)) &&
1983 url.startsWith("file:///")) {
1984 mJsInterfaceEnabled = true;
1985 webview.getSettings().setJavaScriptEnabled(true);
1986 webview.addJavascriptInterface(mContext, "default_homepage");
1987 } else {
1988 if (mJsInterfaceEnabled) {
1989 webview.removeJavascriptInterface("default_homepage");
1990 mJsInterfaceEnabled = false;
1991 }
1992 }
1993 }
1994
John Reck38b39652012-06-05 09:22:59 -07001995 public void disableUrlOverridingForLoad() {
1996 mDisableOverrideUrlLoading = true;
1997 }
1998
Michael Kolb9ef259a2011-07-12 15:33:08 -07001999 protected void capture() {
2000 if (mMainView == null || mCapture == null) return;
John Reck4eadc342011-10-31 14:04:10 -07002001 if (mMainView.getContentWidth() <= 0 || mMainView.getContentHeight() <= 0) {
2002 return;
2003 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07002004 Canvas c = new Canvas(mCapture);
Michael Kolba3194d02011-09-07 11:23:51 -07002005 int state = c.save();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002006 float scale = 0;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002007 Bitmap screenShot = mMainView.getViewportBitmap();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002008 mScreenShot = screenShot;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002009 if (screenShot != null) {
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002010 //scale based on device orientation
2011 if (screenShot.getHeight() > screenShot.getWidth()){
2012 scale = (float) mCaptureWidth / screenShot.getWidth();
2013 } else {
2014 scale = (float) mCaptureHeight / screenShot.getHeight();
2015 }
2016 mCapture.eraseColor(Color.WHITE);
2017 c.scale(scale, scale);
2018 c.drawBitmap(screenShot, 0, 0, null);
2019 } else {
2020 final int left = mMainView.getViewScrollX();
2021 final int top = mMainView.getViewScrollY() + mMainView.getVisibleTitleHeight();
2022
2023 if (mMainView.getHeight() > mMainView.getWidth()){
2024 scale = mCaptureWidth / (float) mMainView.getWidth();
2025 } else {
2026 scale = mCaptureHeight / (float) mMainView.getHeight();
2027 }
2028
2029 c.translate(-left, -top);
2030 c.scale(scale, scale, left, top);
2031 if (mMainView instanceof BrowserWebView) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002032 ((BrowserWebView)mMainView).drawContent(c);
2033 } else {
2034 mMainView.draw(c);
2035 }
John Reck8ee633f2011-08-09 16:00:35 -07002036 }
Michael Kolba3194d02011-09-07 11:23:51 -07002037 c.restoreToCount(state);
2038 // manually anti-alias the edges for the tilt
2039 c.drawRect(0, 0, 1, mCapture.getHeight(), sAlphaPaint);
2040 c.drawRect(mCapture.getWidth() - 1, 0, mCapture.getWidth(),
2041 mCapture.getHeight(), sAlphaPaint);
2042 c.drawRect(0, 0, mCapture.getWidth(), 1, sAlphaPaint);
2043 c.drawRect(0, mCapture.getHeight() - 1, mCapture.getWidth(),
2044 mCapture.getHeight(), sAlphaPaint);
Dianne Hackborn43cfe8a2011-08-02 16:59:35 -07002045 c.setBitmap(null);
John Reck8ee633f2011-08-09 16:00:35 -07002046 mHandler.removeMessages(MSG_CAPTURE);
John Reck1cf4b792011-07-26 10:22:22 -07002047 persistThumbnail();
John Reck8ee633f2011-08-09 16:00:35 -07002048 TabControl tc = mWebViewController.getTabControl();
2049 if (tc != null) {
2050 OnThumbnailUpdatedListener updateListener
2051 = tc.getOnThumbnailUpdatedListener();
2052 if (updateListener != null) {
2053 updateListener.onThumbnailUpdated(this);
2054 }
2055 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07002056 }
2057
2058 @Override
2059 public void onNewPicture(WebView view, Picture picture) {
John Reck1cf4b792011-07-26 10:22:22 -07002060 postCapture();
2061 }
2062
2063 private void postCapture() {
Michael Kolb9ef259a2011-07-12 15:33:08 -07002064 if (!mHandler.hasMessages(MSG_CAPTURE)) {
2065 mHandler.sendEmptyMessageDelayed(MSG_CAPTURE, CAPTURE_DELAY);
2066 }
2067 }
2068
John Reckef654f12011-07-12 16:42:08 -07002069 public boolean canGoBack() {
2070 return mMainView != null ? mMainView.canGoBack() : false;
2071 }
2072
2073 public boolean canGoForward() {
2074 return mMainView != null ? mMainView.canGoForward() : false;
2075 }
2076
2077 public void goBack() {
2078 if (mMainView != null) {
2079 mMainView.goBack();
2080 }
2081 }
2082
2083 public void goForward() {
2084 if (mMainView != null) {
2085 mMainView.goForward();
2086 }
2087 }
2088
Mathew Inwood1dd8e822011-08-03 14:34:29 +01002089 /**
2090 * Causes the tab back/forward stack to be cleared once, if the given URL is the next URL
2091 * to be added to the stack.
2092 *
2093 * This is used to ensure that preloaded URLs that are not subsequently seen by the user do
2094 * not appear in the back stack.
2095 */
Mathew Inwooda829d552011-09-02 14:16:25 +01002096 public void clearBackStackWhenItemAdded(Pattern urlPattern) {
2097 mClearHistoryUrlPattern = urlPattern;
Mathew Inwood1dd8e822011-08-03 14:34:29 +01002098 }
2099
John Reck1cf4b792011-07-26 10:22:22 -07002100 protected void persistThumbnail() {
John Reck4eadc342011-10-31 14:04:10 -07002101 DataController.getInstance(mContext).saveThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07002102 }
2103
2104 protected void deleteThumbnail() {
John Reck4eadc342011-10-31 14:04:10 -07002105 DataController.getInstance(mContext).deleteThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07002106 }
2107
John Reck4eadc342011-10-31 14:04:10 -07002108 void updateCaptureFromBlob(byte[] blob) {
John Reck1cf4b792011-07-26 10:22:22 -07002109 synchronized (Tab.this) {
2110 if (mCapture == null) {
2111 return;
2112 }
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002113 ByteBuffer buffer = ByteBuffer.wrap(blob);
Michael Kolbd837a112011-08-09 14:00:18 -07002114 try {
2115 mCapture.copyPixelsFromBuffer(buffer);
2116 } catch (RuntimeException rex) {
2117 Log.e(LOGTAG, "Load capture has mismatched sizes; buffer: "
2118 + buffer.capacity() + " blob: " + blob.length
2119 + "capture: " + mCapture.getByteCount());
2120 throw rex;
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002121 }
John Reck1cf4b792011-07-26 10:22:22 -07002122 }
2123 }
2124
John Reck52be4782011-08-26 15:37:29 -07002125 @Override
2126 public String toString() {
2127 StringBuilder builder = new StringBuilder(100);
2128 builder.append(mId);
2129 builder.append(") has parent: ");
2130 if (getParent() != null) {
2131 builder.append("true[");
2132 builder.append(getParent().getId());
2133 builder.append("]");
2134 } else {
2135 builder.append("false");
2136 }
2137 builder.append(", incog: ");
2138 builder.append(isPrivateBrowsingEnabled());
2139 if (!isPrivateBrowsingEnabled()) {
2140 builder.append(", title: ");
2141 builder.append(getTitle());
2142 builder.append(", url: ");
2143 builder.append(getUrl());
2144 }
2145 return builder.toString();
2146 }
2147
Steve Block4895b012011-10-03 16:26:46 +01002148 private void handleProceededAfterSslError(SslError error) {
2149 if (error.getUrl().equals(mCurrentState.mUrl)) {
2150 // The security state should currently be SECURITY_STATE_SECURE.
2151 setSecurityState(SecurityState.SECURITY_STATE_BAD_CERTIFICATE);
Steve Block08a6f0c2011-10-06 12:12:53 +01002152 mCurrentState.mSslCertificateError = error;
Steve Block4895b012011-10-03 16:26:46 +01002153 } else if (getSecurityState() == SecurityState.SECURITY_STATE_SECURE) {
Steve Block08a6f0c2011-10-06 12:12:53 +01002154 // The page's main resource is secure and this error is for a
2155 // sub-resource.
Steve Block4895b012011-10-03 16:26:46 +01002156 setSecurityState(SecurityState.SECURITY_STATE_MIXED);
2157 }
2158 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002159}