blob: d0a27b91b446e96b43dbaa064d3a5c468ad41610 [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
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700749 public void toggleFullscreenModeForTab(boolean enterFullscreen) {
750 if (mWebViewController instanceof Controller) {
751 Controller controller = (Controller)mWebViewController;
752 controller.getUi().setTabFullscreen(enterFullscreen);
753 }
754 }
755
756 @Override
Vivek Sekhar8ee3abb2014-07-14 12:32:05 -0700757 public void onOffsetsForFullscreenChanged(
758 float topControlsOffsetYPix, float contentOffsetYPix, float overdrawBottomHeightPix) {
759 boolean hide_title_on_scroll =
760 mContext.getResources().getBoolean(R.bool.hide_title_on_scroll);
761 if (mWebViewController instanceof Controller && hide_title_on_scroll) {
762 Controller controller = (Controller)mWebViewController;
763 mMainView.setTranslationY(contentOffsetYPix);
764 controller.getUi().transalateTitleBar(topControlsOffsetYPix);
765 }
766 }
767
768 @Override
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700769 public boolean isTabFullScreen() {
770 if (mWebViewController instanceof Controller) {
771 Controller controller = (Controller)mWebViewController;
772 return controller.getUi().isTabFullScreen();
773 } else {
774 return false;
775 }
776 }
777
778 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700779 public boolean onCreateWindow(WebView view, final boolean dialog,
780 final boolean userGesture, final Message resultMsg) {
781 // only allow new window or sub window for the foreground case
782 if (!mInForeground) {
783 return false;
784 }
785 // Short-circuit if we can't create any more tabs or sub windows.
786 if (dialog && mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -0700787 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700788 .setTitle(R.string.too_many_subwindows_dialog_title)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200789 .setIconAttribute(android.R.attr.alertDialogIcon)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700790 .setMessage(R.string.too_many_subwindows_dialog_message)
791 .setPositiveButton(R.string.ok, null)
792 .show();
793 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700794 } else if (!mWebViewController.getTabControl().canCreateNewTab()) {
Michael Kolb14612442011-06-24 13:06:29 -0700795 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700796 .setTitle(R.string.too_many_windows_dialog_title)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200797 .setIconAttribute(android.R.attr.alertDialogIcon)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700798 .setMessage(R.string.too_many_windows_dialog_message)
799 .setPositiveButton(R.string.ok, null)
800 .show();
801 return false;
802 }
803
804 // Short-circuit if this was a user gesture.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800805 if (userGesture || !mSettings.blockPopupWindows()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700806 createWindow(dialog, resultMsg);
807 return true;
808 }
809
810 // Allow the popup and create the appropriate window.
811 final AlertDialog.OnClickListener allowListener =
812 new AlertDialog.OnClickListener() {
813 public void onClick(DialogInterface d,
814 int which) {
815 createWindow(dialog, resultMsg);
816 }
817 };
818
819 // Block the popup by returning a null WebView.
820 final AlertDialog.OnClickListener blockListener =
821 new AlertDialog.OnClickListener() {
822 public void onClick(DialogInterface d, int which) {
823 resultMsg.sendToTarget();
824 }
825 };
826
827 // Build a confirmation dialog to display to the user.
828 final AlertDialog d =
Michael Kolb14612442011-06-24 13:06:29 -0700829 new AlertDialog.Builder(mContext)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200830 .setIconAttribute(android.R.attr.alertDialogIcon)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700831 .setMessage(R.string.popup_window_attempt)
832 .setPositiveButton(R.string.allow, allowListener)
833 .setNegativeButton(R.string.block, blockListener)
834 .setCancelable(false)
835 .create();
836
837 // Show the confirmation dialog.
838 d.show();
839 return true;
840 }
841
842 @Override
Patrick Scotteb5061b2009-11-18 15:00:30 -0500843 public void onRequestFocus(WebView view) {
844 if (!mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700845 mWebViewController.switchToTab(Tab.this);
Patrick Scotteb5061b2009-11-18 15:00:30 -0500846 }
847 }
848
849 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700850 public void onCloseWindow(WebView window) {
Michael Kolbc831b632011-05-11 09:30:34 -0700851 if (mParent != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700852 // JavaScript can only close popup window.
853 if (mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700854 mWebViewController.switchToTab(mParent);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700855 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700856 mWebViewController.closeTab(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700857 }
858 }
859
860 @Override
861 public void onProgressChanged(WebView view, int newProgress) {
John Reck30c714c2010-12-16 17:30:34 -0800862 mPageLoadProgress = newProgress;
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800863 if (newProgress == 100) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800864 Log.i(CONSOLE_LOGTAG, "SWE Pageload Progress = 100");
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800865 mInPageLoad = false;
866 }
John Reck30c714c2010-12-16 17:30:34 -0800867 mWebViewController.onProgressChanged(Tab.this);
Michael Kolb72864272012-05-03 15:42:15 -0700868 if (mUpdateThumbnail && newProgress == 100) {
869 mUpdateThumbnail = false;
870 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700871 }
872
873 @Override
Leon Scroggins21d9b902010-03-11 09:33:11 -0500874 public void onReceivedTitle(WebView view, final String title) {
John Reck30c714c2010-12-16 17:30:34 -0800875 mCurrentState.mTitle = title;
Michael Kolb8233fac2010-10-26 16:08:53 -0700876 mWebViewController.onReceivedTitle(Tab.this, title);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700877 }
878
879 @Override
880 public void onReceivedIcon(WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -0800881 mCurrentState.mFavicon = icon;
Michael Kolb8233fac2010-10-26 16:08:53 -0700882 mWebViewController.onFavicon(Tab.this, view, icon);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700883 }
884
885 @Override
886 public void onReceivedTouchIconUrl(WebView view, String url,
887 boolean precomposed) {
Michael Kolb14612442011-06-24 13:06:29 -0700888 final ContentResolver cr = mContext.getContentResolver();
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400889 // Let precomposed icons take precedence over non-composed
890 // icons.
891 if (precomposed && mTouchIconLoader != null) {
892 mTouchIconLoader.cancel(false);
893 mTouchIconLoader = null;
894 }
895 // Have only one async task at a time.
896 if (mTouchIconLoader == null) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700897 mTouchIconLoader = new DownloadTouchIcon(Tab.this,
Michael Kolb14612442011-06-24 13:06:29 -0700898 mContext, cr, view);
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400899 mTouchIconLoader.execute(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700900 }
901 }
902
903 @Override
904 public void onShowCustomView(View view,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800905 CustomViewCallback callback) {
Michael Kolb14612442011-06-24 13:06:29 -0700906 Activity activity = mWebViewController.getActivity();
907 if (activity != null) {
908 onShowCustomView(view, activity.getRequestedOrientation(), callback);
909 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400910 }
911
912 @Override
913 public void onShowCustomView(View view, int requestedOrientation,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800914 CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700915 if (mInForeground) mWebViewController.showCustomView(Tab.this, view,
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400916 requestedOrientation, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700917 }
918
919 @Override
920 public void onHideCustomView() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700921 if (mInForeground) mWebViewController.hideCustomView();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700922 }
923
924 /**
925 * The origin has exceeded its database quota.
926 * @param url the URL that exceeded the quota
927 * @param databaseIdentifier the identifier of the database on which the
928 * transaction that caused the quota overflow was run
929 * @param currentQuota the current quota for the origin.
930 * @param estimatedSize the estimated size of the database.
931 * @param totalUsedQuota is the sum of all origins' quota.
932 * @param quotaUpdater The callback to run when a decision to allow or
933 * deny quota has been made. Don't forget to call this!
934 */
935 @Override
936 public void onExceededDatabaseQuota(String url,
937 String databaseIdentifier, long currentQuota, long estimatedSize,
938 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -0700939 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -0700940 .onExceededDatabaseQuota(url, databaseIdentifier,
941 currentQuota, estimatedSize, totalUsedQuota,
942 quotaUpdater);
943 }
944
945 /**
946 * The Application Cache has exceeded its max size.
947 * @param spaceNeeded is the amount of disk space that would be needed
948 * in order for the last appcache operation to succeed.
949 * @param totalUsedQuota is the sum of all origins' quota.
950 * @param quotaUpdater A callback to inform the WebCore thread that a
951 * new app cache size is available. This callback must always
952 * be executed at some point to ensure that the sleeping
953 * WebCore thread is woken up.
954 */
955 @Override
956 public void onReachedMaxAppCacheSize(long spaceNeeded,
957 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -0700958 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -0700959 .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota,
960 quotaUpdater);
961 }
962
963 /**
964 * Instructs the browser to show a prompt to ask the user to set the
965 * Geolocation permission state for the specified origin.
966 * @param origin The origin for which Geolocation permissions are
967 * requested.
968 * @param callback The callback to call once the user has set the
969 * Geolocation permission state.
970 */
971 @Override
972 public void onGeolocationPermissionsShowPrompt(String origin,
973 GeolocationPermissions.Callback callback) {
974 if (mInForeground) {
Grace Kloba50c241e2010-04-20 11:07:50 -0700975 getGeolocationPermissionsPrompt().show(origin, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700976 }
977 }
978
979 /**
980 * Instructs the browser to hide the Geolocation permissions prompt.
981 */
982 @Override
983 public void onGeolocationPermissionsHidePrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -0700984 if (mInForeground && mGeolocationPermissionsPrompt != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700985 mGeolocationPermissionsPrompt.hide();
986 }
987 }
988
Ben Murdoch65acc352009-11-19 18:16:04 +0000989 /* Adds a JavaScript error message to the system log and if the JS
990 * console is enabled in the about:debug options, to that console
991 * also.
Ben Murdochc42addf2010-01-28 15:19:59 +0000992 * @param consoleMessage the message object.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700993 */
994 @Override
Ben Murdochc42addf2010-01-28 15:19:59 +0000995 public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700996 if (mInForeground) {
997 // call getErrorConsole(true) so it will create one if needed
998 ErrorConsoleView errorConsole = getErrorConsole(true);
Ben Murdochc42addf2010-01-28 15:19:59 +0000999 errorConsole.addErrorMessage(consoleMessage);
Michael Kolb8233fac2010-10-26 16:08:53 -07001000 if (mWebViewController.shouldShowErrorConsole()
1001 && errorConsole.getShowState() !=
1002 ErrorConsoleView.SHOW_MAXIMIZED) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001003 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
1004 }
1005 }
Ben Murdochc42addf2010-01-28 15:19:59 +00001006
Jeff Hamilton47654f42010-09-07 09:57:51 -05001007 // Don't log console messages in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001008 if (isPrivateBrowsingEnabled()) return true;
Jeff Hamilton47654f42010-09-07 09:57:51 -05001009
Ben Murdochc42addf2010-01-28 15:19:59 +00001010 String message = "Console: " + consoleMessage.message() + " "
1011 + consoleMessage.sourceId() + ":"
1012 + consoleMessage.lineNumber();
1013
1014 switch (consoleMessage.messageLevel()) {
1015 case TIP:
1016 Log.v(CONSOLE_LOGTAG, message);
1017 break;
1018 case LOG:
1019 Log.i(CONSOLE_LOGTAG, message);
1020 break;
1021 case WARNING:
1022 Log.w(CONSOLE_LOGTAG, message);
1023 break;
1024 case ERROR:
1025 Log.e(CONSOLE_LOGTAG, message);
1026 break;
1027 case DEBUG:
1028 Log.d(CONSOLE_LOGTAG, message);
1029 break;
1030 }
1031
1032 return true;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001033 }
1034
1035 /**
1036 * Ask the browser for an icon to represent a <video> element.
1037 * This icon will be used if the Web page did not specify a poster attribute.
1038 * @return Bitmap The icon or null if no such icon is available.
1039 */
1040 @Override
1041 public Bitmap getDefaultVideoPoster() {
1042 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001043 return mWebViewController.getDefaultVideoPoster();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001044 }
1045 return null;
1046 }
1047
1048 /**
1049 * Ask the host application for a custom progress view to show while
1050 * a <video> is loading.
1051 * @return View The progress view.
1052 */
1053 @Override
1054 public View getVideoLoadingProgressView() {
1055 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001056 return mWebViewController.getVideoLoadingProgressView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001057 }
1058 return null;
1059 }
1060
1061 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00001062 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001063 if (mInForeground) {
Ben Murdoch8cad4132012-01-11 10:56:43 +00001064 mWebViewController.openFileChooser(uploadMsg, acceptType, capture);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001065 } else {
1066 uploadMsg.onReceiveValue(null);
1067 }
1068 }
1069
Vivek Sekharb54614f2014-05-01 19:03:37 -07001070 @Override
1071 public void showFileChooser(ValueCallback<String[]> uploadFilePaths, String acceptTypes,
1072 boolean capture) {
1073 if (mInForeground) {
1074 mWebViewController.showFileChooser(uploadFilePaths, acceptTypes, capture);
1075 } else {
1076 uploadFilePaths.onReceiveValue(null);
1077 }
1078 }
1079
Grace Kloba22ac16e2009-10-07 18:00:23 -07001080 /**
1081 * Deliver a list of already-visited URLs
1082 */
1083 @Override
1084 public void getVisitedHistory(final ValueCallback<String[]> callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001085 mWebViewController.getVisitedHistory(callback);
1086 }
Ben Murdoch8029a772010-11-16 11:58:21 +00001087
1088 @Override
1089 public void setupAutoFill(Message message) {
1090 // Prompt the user to set up their profile.
1091 final Message msg = message;
Michael Kolb14612442011-06-24 13:06:29 -07001092 AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
1093 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
Ben Murdoch1d676b62011-01-17 12:54:24 +00001094 Context.LAYOUT_INFLATER_SERVICE);
1095 final View layout = inflater.inflate(R.layout.setup_autofill_dialog, null);
1096
1097 builder.setView(layout)
1098 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1099 @Override
1100 public void onClick(DialogInterface dialog, int id) {
1101 CheckBox disableAutoFill = (CheckBox) layout.findViewById(
1102 R.id.setup_autofill_dialog_disable_autofill);
1103
1104 if (disableAutoFill.isChecked()) {
1105 // Disable autofill and show a toast with how to turn it on again.
John Reck35e9dd62011-04-25 09:01:54 -07001106 mSettings.setAutofillEnabled(false);
Michael Kolb14612442011-06-24 13:06:29 -07001107 Toast.makeText(mContext,
Ben Murdoch1d676b62011-01-17 12:54:24 +00001108 R.string.autofill_setup_dialog_negative_toast,
1109 Toast.LENGTH_LONG).show();
1110 } else {
1111 // Take user to the AutoFill profile editor. When they return,
1112 // we will send the message that we pass here which will trigger
1113 // the form to get filled out with their new profile.
1114 mWebViewController.setupAutoFill(msg);
1115 }
1116 }
1117 })
1118 .setNegativeButton(R.string.cancel, null)
1119 .show();
Ben Murdoch8029a772010-11-16 11:58:21 +00001120 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001121 };
1122
1123 // -------------------------------------------------------------------------
1124 // WebViewClient implementation for the sub window
1125 // -------------------------------------------------------------------------
1126
1127 // Subclass of WebViewClient used in subwindows to notify the main
1128 // WebViewClient of certain WebView activities.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001129 private static class SubWindowClient extends WebViewClient {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001130 // The main WebViewClient.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001131 private final WebViewClient mClient;
Michael Kolb8233fac2010-10-26 16:08:53 -07001132 private final WebViewController mController;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001133
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001134 SubWindowClient(WebViewClient client, WebViewController controller) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001135 mClient = client;
Michael Kolb8233fac2010-10-26 16:08:53 -07001136 mController = controller;
Leon Scroggins III211ba542010-04-19 13:21:13 -04001137 }
1138 @Override
1139 public void onPageStarted(WebView view, String url, Bitmap favicon) {
1140 // Unlike the others, do not call mClient's version, which would
1141 // change the progress bar. However, we do want to remove the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001142 // find or select dialog.
Michael Kolb8233fac2010-10-26 16:08:53 -07001143 mController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001144 }
1145 @Override
1146 public void doUpdateVisitedHistory(WebView view, String url,
1147 boolean isReload) {
1148 mClient.doUpdateVisitedHistory(view, url, isReload);
1149 }
1150 @Override
1151 public boolean shouldOverrideUrlLoading(WebView view, String url) {
1152 return mClient.shouldOverrideUrlLoading(view, url);
1153 }
1154 @Override
1155 public void onReceivedSslError(WebView view, SslErrorHandler handler,
1156 SslError error) {
1157 mClient.onReceivedSslError(view, handler, error);
1158 }
1159 @Override
Brian Carlstrom8862c1d2011-06-02 01:05:55 -07001160 public void onReceivedClientCertRequest(WebView view,
1161 ClientCertRequestHandler handler, String host_and_port) {
1162 mClient.onReceivedClientCertRequest(view, handler, host_and_port);
1163 }
1164 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -07001165 public void onReceivedHttpAuthRequest(WebView view,
1166 HttpAuthHandler handler, String host, String realm) {
1167 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
1168 }
1169 @Override
1170 public void onFormResubmission(WebView view, Message dontResend,
1171 Message resend) {
1172 mClient.onFormResubmission(view, dontResend, resend);
1173 }
1174 @Override
1175 public void onReceivedError(WebView view, int errorCode,
1176 String description, String failingUrl) {
1177 mClient.onReceivedError(view, errorCode, description, failingUrl);
1178 }
1179 @Override
1180 public boolean shouldOverrideKeyEvent(WebView view,
1181 android.view.KeyEvent event) {
1182 return mClient.shouldOverrideKeyEvent(view, event);
1183 }
1184 @Override
1185 public void onUnhandledKeyEvent(WebView view,
1186 android.view.KeyEvent event) {
1187 mClient.onUnhandledKeyEvent(view, event);
1188 }
1189 }
1190
1191 // -------------------------------------------------------------------------
1192 // WebChromeClient implementation for the sub window
1193 // -------------------------------------------------------------------------
1194
1195 private class SubWindowChromeClient extends WebChromeClient {
1196 // The main WebChromeClient.
1197 private final WebChromeClient mClient;
1198
1199 SubWindowChromeClient(WebChromeClient client) {
1200 mClient = client;
1201 }
1202 @Override
1203 public void onProgressChanged(WebView view, int newProgress) {
1204 mClient.onProgressChanged(view, newProgress);
1205 }
1206 @Override
1207 public boolean onCreateWindow(WebView view, boolean dialog,
1208 boolean userGesture, android.os.Message resultMsg) {
1209 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
1210 }
1211 @Override
1212 public void onCloseWindow(WebView window) {
1213 if (window != mSubView) {
1214 Log.e(LOGTAG, "Can't close the window");
1215 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001216 mWebViewController.dismissSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001217 }
1218 }
1219
1220 // -------------------------------------------------------------------------
1221
1222 // Construct a new tab
Michael Kolb7bcafde2011-05-09 13:55:59 -07001223 Tab(WebViewController wvcontroller, WebView w) {
John Reck1cf4b792011-07-26 10:22:22 -07001224 this(wvcontroller, w, null);
1225 }
1226
1227 Tab(WebViewController wvcontroller, Bundle state) {
1228 this(wvcontroller, null, state);
1229 }
1230
1231 Tab(WebViewController wvcontroller, WebView w, Bundle state) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001232 mWebViewController = wvcontroller;
Michael Kolb14612442011-06-24 13:06:29 -07001233 mContext = mWebViewController.getContext();
John Reck35e9dd62011-04-25 09:01:54 -07001234 mSettings = BrowserSettings.getInstance();
Michael Kolb14612442011-06-24 13:06:29 -07001235 mDataController = DataController.getInstance(mContext);
1236 mCurrentState = new PageState(mContext, w != null
John Reck847b5322011-04-14 17:02:18 -07001237 ? w.isPrivateBrowsingEnabled() : false);
Tarun Nainani8084c822014-06-25 13:38:06 -07001238 setTimeStamp();
Michael Kolb8233fac2010-10-26 16:08:53 -07001239 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001240 mInForeground = false;
1241
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001242 mDownloadListener = new BrowserDownloadListener() {
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001243 public void onDownloadStart(String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001244 String contentDisposition, String mimetype, String referer,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001245 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001246 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001247 mimetype, referer, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001248 }
1249 };
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001250 mWebBackForwardListClient = new WebBackForwardListClient() {
1251 @Override
1252 public void onNewHistoryItem(WebHistoryItem item) {
Mathew Inwooda829d552011-09-02 14:16:25 +01001253 if (mClearHistoryUrlPattern != null) {
1254 boolean match =
1255 mClearHistoryUrlPattern.matcher(item.getOriginalUrl()).matches();
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001256 if (LOGD_ENABLED) {
Mathew Inwooda829d552011-09-02 14:16:25 +01001257 Log.d(LOGTAG, "onNewHistoryItem: match=" + match + "\n\t"
1258 + item.getUrl() + "\n\t"
1259 + mClearHistoryUrlPattern);
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001260 }
Mathew Inwooda829d552011-09-02 14:16:25 +01001261 if (match) {
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001262 if (mMainView != null) {
1263 mMainView.clearHistory();
1264 }
1265 }
Mathew Inwooda829d552011-09-02 14:16:25 +01001266 mClearHistoryUrlPattern = null;
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001267 }
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001268 }
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001269 };
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001270
John Reck1cf4b792011-07-26 10:22:22 -07001271 mCaptureWidth = mContext.getResources().getDimensionPixelSize(
1272 R.dimen.tab_thumbnail_width);
1273 mCaptureHeight = mContext.getResources().getDimensionPixelSize(
1274 R.dimen.tab_thumbnail_height);
1275 updateShouldCaptureThumbnails();
1276 restoreState(state);
John Reck52be4782011-08-26 15:37:29 -07001277 if (getId() == -1) {
1278 mId = TabControl.getNextId();
1279 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001280 setWebView(w);
Michael Kolb9ef259a2011-07-12 15:33:08 -07001281 mHandler = new Handler() {
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001282 @Override
Michael Kolb9ef259a2011-07-12 15:33:08 -07001283 public void handleMessage(Message m) {
John Reck1cf4b792011-07-26 10:22:22 -07001284 switch (m.what) {
1285 case MSG_CAPTURE:
1286 capture();
1287 break;
1288 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001289 }
1290 };
John Reck1cf4b792011-07-26 10:22:22 -07001291 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001292
Michael Kolb72864272012-05-03 15:42:15 -07001293 public boolean shouldUpdateThumbnail() {
1294 return mUpdateThumbnail;
1295 }
1296
Mathew Inwoode09305e2011-09-02 12:03:26 +01001297 /**
1298 * This is used to get a new ID when the tab has been preloaded, before it is displayed and
1299 * added to TabControl. Preloaded tabs can be created before restoreInstanceState, leading
1300 * to overlapping IDs between the preloaded and restored tabs.
1301 */
1302 public void refreshIdAfterPreload() {
1303 mId = TabControl.getNextId();
1304 }
1305
John Reck1cf4b792011-07-26 10:22:22 -07001306 public void updateShouldCaptureThumbnails() {
1307 if (mWebViewController.shouldCaptureThumbnails()) {
1308 synchronized (Tab.this) {
1309 if (mCapture == null) {
1310 mCapture = Bitmap.createBitmap(mCaptureWidth, mCaptureHeight,
1311 Bitmap.Config.RGB_565);
Michael Kolbc3af0672011-08-09 10:24:41 -07001312 mCapture.eraseColor(Color.WHITE);
John Reck1cf4b792011-07-26 10:22:22 -07001313 if (mInForeground) {
1314 postCapture();
1315 }
1316 }
1317 }
1318 } else {
1319 synchronized (Tab.this) {
1320 mCapture = null;
1321 deleteThumbnail();
1322 }
1323 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001324 }
1325
Michael Kolb14612442011-06-24 13:06:29 -07001326 public void setController(WebViewController ctl) {
1327 mWebViewController = ctl;
John Reck1cf4b792011-07-26 10:22:22 -07001328 updateShouldCaptureThumbnails();
Michael Kolb14612442011-06-24 13:06:29 -07001329 }
1330
Michael Kolbc831b632011-05-11 09:30:34 -07001331 public long getId() {
1332 return mId;
1333 }
1334
Michael Kolb91911a22012-01-17 11:21:25 -08001335 void setWebView(WebView w) {
1336 setWebView(w, true);
1337 }
1338
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001339 public boolean isNativeActive(){
1340 if (mMainView == null)
1341 return false;
1342 return true;
1343 }
1344
1345 public void setTimeStamp(){
1346 Date d = new Date();
1347 timestamp = (new Timestamp(d.getTime()));
1348 }
1349
1350 public Timestamp getTimestamp() {
1351 return timestamp;
1352 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001353 /**
1354 * Sets the WebView for this tab, correctly removing the old WebView from
1355 * the container view.
1356 */
Michael Kolb91911a22012-01-17 11:21:25 -08001357 void setWebView(WebView w, boolean restore) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001358 if (mMainView == w) {
1359 return;
1360 }
Michael Kolba713ec82010-11-29 17:27:06 -08001361
Grace Kloba22ac16e2009-10-07 18:00:23 -07001362 // If the WebView is changing, the page will be reloaded, so any ongoing
1363 // Geolocation permission requests are void.
Grace Kloba50c241e2010-04-20 11:07:50 -07001364 if (mGeolocationPermissionsPrompt != null) {
1365 mGeolocationPermissionsPrompt.hide();
1366 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001367
Michael Kolba713ec82010-11-29 17:27:06 -08001368 mWebViewController.onSetWebView(this, w);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001369
John Reck1cf4b792011-07-26 10:22:22 -07001370 if (mMainView != null) {
John Reck8ee633f2011-08-09 16:00:35 -07001371 mMainView.setPictureListener(null);
John Reck1cf4b792011-07-26 10:22:22 -07001372 if (w != null) {
1373 syncCurrentState(w, null);
1374 } else {
Panos Thomasa9a5a582014-03-18 19:20:08 -07001375 mCurrentState = new PageState(mContext, mMainView.isPrivateBrowsingEnabled());
John Reck1cf4b792011-07-26 10:22:22 -07001376 }
1377 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001378 // set the new one
1379 mMainView = w;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001380 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001381 if (mMainView != null) {
1382 mMainView.setWebViewClient(mWebViewClient);
1383 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001384 // Attach DownloadManager so that downloads can start in an active
1385 // or a non-active window. This can happen when going to a site that
1386 // does a redirect after a period of time. The user could have
1387 // switched to another tab while waiting for the download to start.
1388 mMainView.setDownloadListener(mDownloadListener);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001389 getWebView().setWebBackForwardListClient(mWebBackForwardListClient);
John Reck8ee633f2011-08-09 16:00:35 -07001390 TabControl tc = mWebViewController.getTabControl();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001391 if (tc != null /*&& tc.getOnThumbnailUpdatedListener() != null*/) {
John Reck8ee633f2011-08-09 16:00:35 -07001392 mMainView.setPictureListener(this);
1393 }
Michael Kolb91911a22012-01-17 11:21:25 -08001394 if (restore && (mSavedState != null)) {
John Reck8b9bb8b2012-03-08 13:19:40 -08001395 restoreUserAgent();
John Reck6c2e2f32011-08-22 13:41:23 -07001396 WebBackForwardList restoredState
1397 = mMainView.restoreState(mSavedState);
1398 if (restoredState == null || restoredState.getSize() == 0) {
1399 Log.w(LOGTAG, "Failed to restore WebView state!");
1400 loadUrl(mCurrentState.mOriginalUrl, null);
1401 }
John Reck1cf4b792011-07-26 10:22:22 -07001402 mSavedState = null;
1403 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001404 }
1405 }
1406
1407 /**
1408 * Destroy the tab's main WebView and subWindow if any
1409 */
1410 void destroy() {
Tarun Nainani2c1dd7c2014-07-05 16:40:12 -07001411
Matthew Huib7f2e9c2014-04-16 11:12:37 -04001412 if (mPostponeDestroy) {
1413 mShouldDestroy = true;
1414 return;
1415 }
1416 mShouldDestroy = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001417 if (mMainView != null) {
1418 dismissSubWindow();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001419 // save the WebView to call destroy() after detach it from the tab
1420 WebView webView = mMainView;
1421 setWebView(null);
1422 webView.destroy();
1423 }
1424 }
1425
Matthew Huib7f2e9c2014-04-16 11:12:37 -04001426 private boolean mPostponeDestroy = false;
1427 private boolean mShouldDestroy = false;
1428
1429 public void postponeDestroy() {
1430 mPostponeDestroy = true;
1431 }
1432
1433 public void performPostponedDestroy() {
1434 mPostponeDestroy = false;
1435 if (mShouldDestroy) {
1436 destroy();
1437 }
1438 }
1439
Grace Kloba22ac16e2009-10-07 18:00:23 -07001440 /**
1441 * Remove the tab from the parent
1442 */
1443 void removeFromTree() {
1444 // detach the children
Michael Kolbc831b632011-05-11 09:30:34 -07001445 if (mChildren != null) {
1446 for(Tab t : mChildren) {
1447 t.setParent(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001448 }
1449 }
1450 // remove itself from the parent list
Michael Kolbc831b632011-05-11 09:30:34 -07001451 if (mParent != null) {
1452 mParent.mChildren.remove(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001453 }
Tarun Nainani2c1dd7c2014-07-05 16:40:12 -07001454 if (mScreenShot != null) {
1455 mScreenShot.recycle();
1456 mScreenShot = null;
1457 }
1458 if (mCapture != null ) {
1459 mCapture.recycle();
1460 mCapture = null;
1461 }
John Reck1cf4b792011-07-26 10:22:22 -07001462 deleteThumbnail();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001463 }
1464
1465 /**
1466 * Create a new subwindow unless a subwindow already exists.
1467 * @return True if a new subwindow was created. False if one already exists.
1468 */
1469 boolean createSubWindow() {
1470 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001471 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001472 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001473 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001474 mSubView.setWebChromeClient(new SubWindowChromeClient(
1475 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001476 // Set a different DownloadListener for the mSubView, since it will
1477 // just need to dismiss the mSubView, rather than close the Tab
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001478 mSubView.setDownloadListener(new BrowserDownloadListener() {
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001479 public void onDownloadStart(String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001480 String contentDisposition, String mimetype, String referer,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001481 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001482 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001483 contentDisposition, mimetype, referer, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001484 if (mSubView.copyBackForwardList().getSize() == 0) {
1485 // This subwindow was opened for the sole purpose of
1486 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001487 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001488 }
1489 }
1490 });
Michael Kolb14612442011-06-24 13:06:29 -07001491 mSubView.setOnCreateContextMenuListener(mWebViewController.getActivity());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001492 return true;
1493 }
1494 return false;
1495 }
1496
1497 /**
1498 * Dismiss the subWindow for the tab.
1499 */
1500 void dismissSubWindow() {
1501 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001502 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001503 mSubView.destroy();
1504 mSubView = null;
1505 mSubViewContainer = null;
1506 }
1507 }
1508
Grace Kloba22ac16e2009-10-07 18:00:23 -07001509
1510 /**
1511 * Set the parent tab of this tab.
1512 */
Michael Kolbc831b632011-05-11 09:30:34 -07001513 void setParent(Tab parent) {
John Reck52be4782011-08-26 15:37:29 -07001514 if (parent == this) {
1515 throw new IllegalStateException("Cannot set parent to self!");
1516 }
Michael Kolbc831b632011-05-11 09:30:34 -07001517 mParent = parent;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001518 // This tab may have been freed due to low memory. If that is the case,
Michael Kolbc831b632011-05-11 09:30:34 -07001519 // the parent tab id is already saved. If we are changing that id
Grace Kloba22ac16e2009-10-07 18:00:23 -07001520 // (most likely due to removing the parent tab) we must update the
Michael Kolbc831b632011-05-11 09:30:34 -07001521 // parent tab id in the saved Bundle.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001522 if (mSavedState != null) {
1523 if (parent == null) {
1524 mSavedState.remove(PARENTTAB);
1525 } else {
Michael Kolbc831b632011-05-11 09:30:34 -07001526 mSavedState.putLong(PARENTTAB, parent.getId());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001527 }
1528 }
John Reckb0a86db2011-05-24 14:05:58 -07001529
1530 // Sync the WebView useragent with the parent
1531 if (parent != null && mSettings.hasDesktopUseragent(parent.getWebView())
1532 != mSettings.hasDesktopUseragent(getWebView())) {
1533 mSettings.toggleDesktopUseragent(getWebView());
1534 }
John Reck52be4782011-08-26 15:37:29 -07001535
1536 if (parent != null && parent.getId() == getId()) {
1537 throw new IllegalStateException("Parent has same ID as child!");
1538 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001539 }
1540
1541 /**
Michael Kolbc831b632011-05-11 09:30:34 -07001542 * If this Tab was created through another Tab, then this method returns
1543 * that Tab.
1544 * @return the Tab parent or null
1545 */
1546 public Tab getParent() {
1547 return mParent;
1548 }
1549
1550 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001551 * When a Tab is created through the content of another Tab, then we
1552 * associate the Tabs.
1553 * @param child the Tab that was created from this Tab
1554 */
1555 void addChildTab(Tab child) {
Michael Kolbc831b632011-05-11 09:30:34 -07001556 if (mChildren == null) {
1557 mChildren = new Vector<Tab>();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001558 }
Michael Kolbc831b632011-05-11 09:30:34 -07001559 mChildren.add(child);
1560 child.setParent(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001561 }
1562
Michael Kolbc831b632011-05-11 09:30:34 -07001563 Vector<Tab> getChildren() {
1564 return mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001565 }
1566
1567 void resume() {
1568 if (mMainView != null) {
John Reck56c1fcf2011-08-17 10:15:16 -07001569 setupHwAcceleration(mMainView);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001570 mMainView.onResume();
1571 if (mSubView != null) {
1572 mSubView.onResume();
1573 }
1574 }
1575 }
1576
John Reck56c1fcf2011-08-17 10:15:16 -07001577 private void setupHwAcceleration(View web) {
1578 if (web == null) return;
1579 BrowserSettings settings = BrowserSettings.getInstance();
1580 if (settings.isHardwareAccelerated()) {
1581 web.setLayerType(View.LAYER_TYPE_NONE, null);
1582 } else {
1583 web.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
1584 }
1585 }
1586
Grace Kloba22ac16e2009-10-07 18:00:23 -07001587 void pause() {
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001588 capture();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001589 if (mMainView != null) {
1590 mMainView.onPause();
1591 if (mSubView != null) {
1592 mSubView.onPause();
1593 }
1594 }
1595 }
1596
1597 void putInForeground() {
John Reck8ee633f2011-08-09 16:00:35 -07001598 if (mInForeground) {
1599 return;
1600 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001601 mInForeground = true;
1602 resume();
Michael Kolb14612442011-06-24 13:06:29 -07001603 Activity activity = mWebViewController.getActivity();
1604 mMainView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001605 if (mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -07001606 mSubView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001607 }
1608 // Show the pending error dialog if the queue is not empty
1609 if (mQueuedErrors != null && mQueuedErrors.size() > 0) {
1610 showError(mQueuedErrors.getFirst());
1611 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001612 mWebViewController.bookmarkedStatusHasChanged(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001613 }
1614
1615 void putInBackground() {
John Reck8ee633f2011-08-09 16:00:35 -07001616 if (!mInForeground) {
1617 return;
1618 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001619 mInForeground = false;
1620 pause();
1621 mMainView.setOnCreateContextMenuListener(null);
1622 if (mSubView != null) {
1623 mSubView.setOnCreateContextMenuListener(null);
1624 }
1625 }
1626
Michael Kolb8233fac2010-10-26 16:08:53 -07001627 boolean inForeground() {
1628 return mInForeground;
1629 }
1630
Grace Kloba22ac16e2009-10-07 18:00:23 -07001631 /**
1632 * Return the top window of this tab; either the subwindow if it is not
1633 * null or the main window.
1634 * @return The top window of this tab.
1635 */
1636 WebView getTopWindow() {
1637 if (mSubView != null) {
1638 return mSubView;
1639 }
1640 return mMainView;
1641 }
1642
1643 /**
1644 * Return the main window of this tab. Note: if a tab is freed in the
1645 * background, this can return null. It is only guaranteed to be
1646 * non-null for the current tab.
1647 * @return The main WebView of this tab.
1648 */
1649 WebView getWebView() {
1650 return mMainView;
1651 }
1652
Michael Kolba713ec82010-11-29 17:27:06 -08001653 void setViewContainer(View container) {
1654 mContainer = container;
1655 }
1656
Michael Kolb8233fac2010-10-26 16:08:53 -07001657 View getViewContainer() {
1658 return mContainer;
1659 }
1660
Grace Kloba22ac16e2009-10-07 18:00:23 -07001661 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001662 * Return whether private browsing is enabled for the main window of
1663 * this tab.
1664 * @return True if private browsing is enabled.
1665 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001666 boolean isPrivateBrowsingEnabled() {
John Reck502a3532011-08-16 14:21:46 -07001667 return mCurrentState.mIncognito;
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001668 }
1669
1670 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001671 * Return the subwindow of this tab or null if there is no subwindow.
1672 * @return The subwindow of this tab or null.
1673 */
1674 WebView getSubWebView() {
1675 return mSubView;
1676 }
1677
Michael Kolb1514bb72010-11-22 09:11:48 -08001678 void setSubWebView(WebView subView) {
1679 mSubView = subView;
1680 }
1681
Michael Kolb8233fac2010-10-26 16:08:53 -07001682 View getSubViewContainer() {
1683 return mSubViewContainer;
1684 }
1685
Michael Kolb1514bb72010-11-22 09:11:48 -08001686 void setSubViewContainer(View subViewContainer) {
1687 mSubViewContainer = subViewContainer;
1688 }
1689
Grace Kloba22ac16e2009-10-07 18:00:23 -07001690 /**
1691 * @return The geolocation permissions prompt for this tab.
1692 */
1693 GeolocationPermissionsPrompt getGeolocationPermissionsPrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001694 if (mGeolocationPermissionsPrompt == null) {
1695 ViewStub stub = (ViewStub) mContainer
1696 .findViewById(R.id.geolocation_permissions_prompt);
1697 mGeolocationPermissionsPrompt = (GeolocationPermissionsPrompt) stub
1698 .inflate();
Grace Kloba50c241e2010-04-20 11:07:50 -07001699 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001700 return mGeolocationPermissionsPrompt;
1701 }
1702
1703 /**
1704 * @return The application id string
1705 */
1706 String getAppId() {
1707 return mAppId;
1708 }
1709
1710 /**
1711 * Set the application id string
1712 * @param id
1713 */
1714 void setAppId(String id) {
1715 mAppId = id;
1716 }
1717
Michael Kolbe28b3472011-08-04 16:54:31 -07001718 boolean closeOnBack() {
1719 return mCloseOnBack;
1720 }
1721
1722 void setCloseOnBack(boolean close) {
1723 mCloseOnBack = close;
1724 }
1725
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001726 boolean getDerivedFromIntent() {
1727 return mDerivedFromIntent;
1728 }
1729
1730 void setDerivedFromIntent(boolean derived) {
1731 mDerivedFromIntent = derived;
1732 }
1733
Grace Kloba22ac16e2009-10-07 18:00:23 -07001734 String getUrl() {
John Reck324d4402011-01-11 16:56:42 -08001735 return UrlUtils.filteredUrl(mCurrentState.mUrl);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001736 }
1737
John Reck49a603c2011-03-03 09:33:05 -08001738 String getOriginalUrl() {
John Reckdb22ec42011-06-29 11:31:24 -07001739 if (mCurrentState.mOriginalUrl == null) {
1740 return getUrl();
John Reck49a603c2011-03-03 09:33:05 -08001741 }
John Reckdb22ec42011-06-29 11:31:24 -07001742 return UrlUtils.filteredUrl(mCurrentState.mOriginalUrl);
John Reck49a603c2011-03-03 09:33:05 -08001743 }
1744
Grace Kloba22ac16e2009-10-07 18:00:23 -07001745 /**
John Reck30c714c2010-12-16 17:30:34 -08001746 * Get the title of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001747 */
1748 String getTitle() {
John Reck30c714c2010-12-16 17:30:34 -08001749 if (mCurrentState.mTitle == null && mInPageLoad) {
Michael Kolb14612442011-06-24 13:06:29 -07001750 return mContext.getString(R.string.title_bar_loading);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001751 }
John Reck30c714c2010-12-16 17:30:34 -08001752 return mCurrentState.mTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001753 }
1754
1755 /**
John Reck30c714c2010-12-16 17:30:34 -08001756 * Get the favicon of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001757 */
1758 Bitmap getFavicon() {
John Reck1cf4b792011-07-26 10:22:22 -07001759 if (mCurrentState.mFavicon != null) {
1760 return mCurrentState.mFavicon;
1761 }
1762 return getDefaultFavicon(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001763 }
1764
John Recke969cc52010-12-21 17:24:43 -08001765 public boolean isBookmarkedSite() {
1766 return mCurrentState.mIsBookmarkedSite;
1767 }
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001768
Grace Kloba22ac16e2009-10-07 18:00:23 -07001769 /**
1770 * Return the tab's error console. Creates the console if createIfNEcessary
1771 * is true and we haven't already created the console.
1772 * @param createIfNecessary Flag to indicate if the console should be
1773 * created if it has not been already.
1774 * @return The tab's error console, or null if one has not been created and
1775 * createIfNecessary is false.
1776 */
1777 ErrorConsoleView getErrorConsole(boolean createIfNecessary) {
1778 if (createIfNecessary && mErrorConsole == null) {
Michael Kolb14612442011-06-24 13:06:29 -07001779 mErrorConsole = new ErrorConsoleView(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001780 mErrorConsole.setWebView(mMainView);
1781 }
1782 return mErrorConsole;
1783 }
1784
Steve Block08a6f0c2011-10-06 12:12:53 +01001785 /**
1786 * Sets the security state, clears the SSL certificate error and informs
1787 * the controller.
1788 */
Steve Block2466eff2011-10-03 15:33:09 +01001789 private void setSecurityState(SecurityState securityState) {
1790 mCurrentState.mSecurityState = securityState;
Steve Block08a6f0c2011-10-06 12:12:53 +01001791 mCurrentState.mSslCertificateError = null;
Steve Block2466eff2011-10-03 15:33:09 +01001792 mWebViewController.onUpdatedSecurityState(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001793 }
1794
1795 /**
Steve Block2466eff2011-10-03 15:33:09 +01001796 * @return The tab's security state.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001797 */
Steve Block2466eff2011-10-03 15:33:09 +01001798 SecurityState getSecurityState() {
1799 return mCurrentState.mSecurityState;
John Reck30c714c2010-12-16 17:30:34 -08001800 }
1801
Steve Block08a6f0c2011-10-06 12:12:53 +01001802 /**
1803 * Gets the SSL certificate error, if any, for the page's main resource.
1804 * This is only non-null when the security state is
1805 * SECURITY_STATE_BAD_CERTIFICATE.
1806 */
1807 SslError getSslCertificateError() {
1808 return mCurrentState.mSslCertificateError;
1809 }
1810
John Reck30c714c2010-12-16 17:30:34 -08001811 int getLoadProgress() {
1812 if (mInPageLoad) {
1813 return mPageLoadProgress;
1814 }
1815 return 100;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001816 }
1817
1818 /**
1819 * @return TRUE if onPageStarted is called while onPageFinished is not
1820 * called yet.
1821 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001822 boolean inPageLoad() {
1823 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001824 }
1825
Grace Kloba22ac16e2009-10-07 18:00:23 -07001826 /**
John Reck1cf4b792011-07-26 10:22:22 -07001827 * @return The Bundle with the tab's state if it can be saved, otherwise null
Grace Kloba22ac16e2009-10-07 18:00:23 -07001828 */
John Reck1cf4b792011-07-26 10:22:22 -07001829 public Bundle saveState() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001830 // If the WebView is null it means we ran low on memory and we already
1831 // stored the saved state in mSavedState.
1832 if (mMainView == null) {
John Reck1cf4b792011-07-26 10:22:22 -07001833 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001834 }
John Reck6c2e2f32011-08-22 13:41:23 -07001835
1836 if (TextUtils.isEmpty(mCurrentState.mUrl)) {
John Reck1cf4b792011-07-26 10:22:22 -07001837 return null;
John Reck24f18262011-06-17 14:47:20 -07001838 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001839
1840 mSavedState = new Bundle();
John Reck6c2e2f32011-08-22 13:41:23 -07001841 WebBackForwardList savedList = mMainView.saveState(mSavedState);
1842 if (savedList == null || savedList.getSize() == 0) {
1843 Log.w(LOGTAG, "Failed to save back/forward list for "
1844 + mCurrentState.mUrl);
1845 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001846
Michael Kolbc831b632011-05-11 09:30:34 -07001847 mSavedState.putLong(ID, mId);
John Reck30c714c2010-12-16 17:30:34 -08001848 mSavedState.putString(CURRURL, mCurrentState.mUrl);
1849 mSavedState.putString(CURRTITLE, mCurrentState.mTitle);
John Reck1cf4b792011-07-26 10:22:22 -07001850 mSavedState.putBoolean(INCOGNITO, mMainView.isPrivateBrowsingEnabled());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001851 if (mAppId != null) {
1852 mSavedState.putString(APPID, mAppId);
1853 }
Michael Kolbe28b3472011-08-04 16:54:31 -07001854 mSavedState.putBoolean(CLOSEFLAG, mCloseOnBack);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001855 // Remember the parent tab so the relationship can be restored.
Michael Kolbc831b632011-05-11 09:30:34 -07001856 if (mParent != null) {
1857 mSavedState.putLong(PARENTTAB, mParent.mId);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001858 }
John Reckb0a86db2011-05-24 14:05:58 -07001859 mSavedState.putBoolean(USERAGENT,
1860 mSettings.hasDesktopUseragent(getWebView()));
John Reck1cf4b792011-07-26 10:22:22 -07001861 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001862 }
1863
1864 /*
1865 * Restore the state of the tab.
1866 */
John Reck1cf4b792011-07-26 10:22:22 -07001867 private void restoreState(Bundle b) {
1868 mSavedState = b;
1869 if (mSavedState == null) {
1870 return;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001871 }
1872 // Restore the internal state even if the WebView fails to restore.
1873 // This will maintain the app id, original url and close-on-exit values.
Michael Kolbc831b632011-05-11 09:30:34 -07001874 mId = b.getLong(ID);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001875 mAppId = b.getString(APPID);
Michael Kolbe28b3472011-08-04 16:54:31 -07001876 mCloseOnBack = b.getBoolean(CLOSEFLAG);
John Reck8b9bb8b2012-03-08 13:19:40 -08001877 restoreUserAgent();
John Reck1cf4b792011-07-26 10:22:22 -07001878 String url = b.getString(CURRURL);
1879 String title = b.getString(CURRTITLE);
1880 boolean incognito = b.getBoolean(INCOGNITO);
1881 mCurrentState = new PageState(mContext, incognito, url, null);
1882 mCurrentState.mTitle = title;
1883 synchronized (Tab.this) {
1884 if (mCapture != null) {
John Reck4eadc342011-10-31 14:04:10 -07001885 DataController.getInstance(mContext).loadThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07001886 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001887 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001888 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04001889
John Reck8b9bb8b2012-03-08 13:19:40 -08001890 private void restoreUserAgent() {
1891 if (mMainView == null || mSavedState == null) {
1892 return;
1893 }
1894 if (mSavedState.getBoolean(USERAGENT)
1895 != mSettings.hasDesktopUseragent(mMainView)) {
1896 mSettings.toggleDesktopUseragent(mMainView);
1897 }
1898 }
1899
Leon Scroggins1961ed22010-12-07 15:22:21 -05001900 public void updateBookmarkedStatus() {
John Recke969cc52010-12-21 17:24:43 -08001901 mDataController.queryBookmarkStatus(getUrl(), mIsBookmarkCallback);
Leon Scroggins1961ed22010-12-07 15:22:21 -05001902 }
1903
John Recke969cc52010-12-21 17:24:43 -08001904 private DataController.OnQueryUrlIsBookmark mIsBookmarkCallback
1905 = new DataController.OnQueryUrlIsBookmark() {
1906 @Override
1907 public void onQueryUrlIsBookmark(String url, boolean isBookmark) {
1908 if (mCurrentState.mUrl.equals(url)) {
1909 mCurrentState.mIsBookmarkedSite = isBookmark;
1910 mWebViewController.bookmarkedStatusHasChanged(Tab.this);
1911 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001912 }
John Recke969cc52010-12-21 17:24:43 -08001913 };
Michael Kolb1acef692011-03-08 14:12:06 -08001914
Michael Kolbeb95db42011-03-03 10:38:40 -08001915 public Bitmap getScreenshot() {
John Reck1cf4b792011-07-26 10:22:22 -07001916 synchronized (Tab.this) {
1917 return mCapture;
1918 }
Michael Kolbeb95db42011-03-03 10:38:40 -08001919 }
1920
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001921 public Bitmap getFullScreenshot() {
1922 synchronized (Tab.this) {
1923 return mScreenShot;
1924 }
1925 }
1926
John Reck541f55a2011-06-07 16:34:43 -07001927 public boolean isSnapshot() {
John Reck541f55a2011-06-07 16:34:43 -07001928 return false;
1929 }
1930
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001931 private static class SaveCallback implements ValueCallback<String> {
1932 boolean onReceiveValueCalled = false;
1933 private String mPath;
John Reck68234a92012-04-19 15:27:12 -07001934
1935 @Override
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001936 public void onReceiveValue(String path) {
1937 this.onReceiveValueCalled = true;
1938 this.mPath = path;
John Reck68234a92012-04-19 15:27:12 -07001939 synchronized (this) {
1940 notifyAll();
John Reck8cc92352011-07-06 17:41:52 -07001941 }
John Reck541f55a2011-06-07 16:34:43 -07001942 }
John Reck68234a92012-04-19 15:27:12 -07001943
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001944 public String getPath() {
1945 return mPath;
1946 }
John Reck68234a92012-04-19 15:27:12 -07001947 }
1948
1949 /**
1950 * Must be called on the UI thread
1951 */
1952 public ContentValues createSnapshotValues() {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001953 WebView web = getWebView();
John Reck68234a92012-04-19 15:27:12 -07001954 if (web == null) return null;
John Reckd8c74522011-06-14 08:45:00 -07001955 ContentValues values = new ContentValues();
1956 values.put(Snapshots.TITLE, mCurrentState.mTitle);
1957 values.put(Snapshots.URL, mCurrentState.mUrl);
John Reck68234a92012-04-19 15:27:12 -07001958 values.put(Snapshots.BACKGROUND, web.getPageBackgroundColor());
John Reck8cc92352011-07-06 17:41:52 -07001959 values.put(Snapshots.DATE_CREATED, System.currentTimeMillis());
1960 values.put(Snapshots.FAVICON, compressBitmap(getFavicon()));
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001961 Bitmap screenshot = web.getViewportBitmap();
John Reck8cc92352011-07-06 17:41:52 -07001962 values.put(Snapshots.THUMBNAIL, compressBitmap(screenshot));
John Reckd8c74522011-06-14 08:45:00 -07001963 return values;
John Reck541f55a2011-06-07 16:34:43 -07001964 }
1965
John Reck68234a92012-04-19 15:27:12 -07001966 /**
1967 * Probably want to call this on a background thread
1968 */
1969 public boolean saveViewState(ContentValues values) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001970 WebView web = getWebView();
John Reck68234a92012-04-19 15:27:12 -07001971 if (web == null) return false;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001972 String filename = UUID.randomUUID().toString();
John Reck68234a92012-04-19 15:27:12 -07001973 SaveCallback callback = new SaveCallback();
John Reck68234a92012-04-19 15:27:12 -07001974 try {
John Reck68234a92012-04-19 15:27:12 -07001975 synchronized (callback) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001976 web.saveViewState(filename, callback);
1977 callback.wait();
John Reck68234a92012-04-19 15:27:12 -07001978 }
John Reck68234a92012-04-19 15:27:12 -07001979 } catch (Exception e) {
1980 Log.w(LOGTAG, "Failed to save view state", e);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001981 String path = callback.getPath();
1982 if (path != null) {
1983 File file = mContext.getFileStreamPath(path);
1984 if (file.exists() && !file.delete()) {
1985 file.deleteOnExit();
1986 }
John Reck68234a92012-04-19 15:27:12 -07001987 }
1988 return false;
1989 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001990 String path = callback.getPath();
1991 File savedFile = new File(path);
1992 if (!savedFile.exists()) {
1993 return false;
John Reck68234a92012-04-19 15:27:12 -07001994 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001995 values.put(Snapshots.VIEWSTATE_PATH, path.substring(path.lastIndexOf('/') + 1));
1996 values.put(Snapshots.VIEWSTATE_SIZE, savedFile.length());
John Reck68234a92012-04-19 15:27:12 -07001997 return true;
1998 }
1999
John Reck8cc92352011-07-06 17:41:52 -07002000 public byte[] compressBitmap(Bitmap bitmap) {
2001 if (bitmap == null) {
2002 return null;
2003 }
2004 ByteArrayOutputStream stream = new ByteArrayOutputStream();
2005 bitmap.compress(CompressFormat.PNG, 100, stream);
2006 return stream.toByteArray();
2007 }
2008
John Reck26b18322011-06-21 13:08:58 -07002009 public void loadUrl(String url, Map<String, String> headers) {
2010 if (mMainView != null) {
Michael Kolba53c9892011-10-05 13:31:40 -07002011 mPageLoadProgress = INITIAL_PROGRESS;
2012 mInPageLoad = true;
Michael Kolb14612442011-06-24 13:06:29 -07002013 mCurrentState = new PageState(mContext, false, url, null);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002014 handleJsInterface(mMainView, url);
John Reck26b18322011-06-21 13:08:58 -07002015 mMainView.loadUrl(url, headers);
2016 }
2017 }
2018
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002019 public void handleJsInterface(WebView webview, String url){
2020 if (url != null &&
2021 url.equals(mContext.getResources().getString(R.string.homepage_base)) &&
2022 url.startsWith("file:///")) {
2023 mJsInterfaceEnabled = true;
2024 webview.getSettings().setJavaScriptEnabled(true);
2025 webview.addJavascriptInterface(mContext, "default_homepage");
2026 } else {
2027 if (mJsInterfaceEnabled) {
2028 webview.removeJavascriptInterface("default_homepage");
2029 mJsInterfaceEnabled = false;
2030 }
2031 }
2032 }
2033
John Reck38b39652012-06-05 09:22:59 -07002034 public void disableUrlOverridingForLoad() {
2035 mDisableOverrideUrlLoading = true;
2036 }
2037
Michael Kolb9ef259a2011-07-12 15:33:08 -07002038 protected void capture() {
2039 if (mMainView == null || mCapture == null) return;
John Reck4eadc342011-10-31 14:04:10 -07002040 if (mMainView.getContentWidth() <= 0 || mMainView.getContentHeight() <= 0) {
2041 return;
2042 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07002043 Canvas c = new Canvas(mCapture);
Michael Kolba3194d02011-09-07 11:23:51 -07002044 int state = c.save();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002045 float scale = 0;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002046 Bitmap screenShot = mMainView.getViewportBitmap();
Vivek Sekhar0ab52512014-07-02 14:51:32 -07002047 if (mScreenShot != null) {
2048 mScreenShot.recycle();
2049 mScreenShot = null;
2050 }
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002051 mScreenShot = screenShot;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002052 if (screenShot != null) {
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002053 //scale based on device orientation
2054 if (screenShot.getHeight() > screenShot.getWidth()){
2055 scale = (float) mCaptureWidth / screenShot.getWidth();
2056 } else {
2057 scale = (float) mCaptureHeight / screenShot.getHeight();
2058 }
2059 mCapture.eraseColor(Color.WHITE);
2060 c.scale(scale, scale);
2061 c.drawBitmap(screenShot, 0, 0, null);
2062 } else {
2063 final int left = mMainView.getViewScrollX();
2064 final int top = mMainView.getViewScrollY() + mMainView.getVisibleTitleHeight();
2065
2066 if (mMainView.getHeight() > mMainView.getWidth()){
2067 scale = mCaptureWidth / (float) mMainView.getWidth();
2068 } else {
2069 scale = mCaptureHeight / (float) mMainView.getHeight();
2070 }
2071
2072 c.translate(-left, -top);
2073 c.scale(scale, scale, left, top);
2074 if (mMainView instanceof BrowserWebView) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002075 ((BrowserWebView)mMainView).drawContent(c);
2076 } else {
2077 mMainView.draw(c);
2078 }
John Reck8ee633f2011-08-09 16:00:35 -07002079 }
Michael Kolba3194d02011-09-07 11:23:51 -07002080 c.restoreToCount(state);
2081 // manually anti-alias the edges for the tilt
2082 c.drawRect(0, 0, 1, mCapture.getHeight(), sAlphaPaint);
2083 c.drawRect(mCapture.getWidth() - 1, 0, mCapture.getWidth(),
2084 mCapture.getHeight(), sAlphaPaint);
2085 c.drawRect(0, 0, mCapture.getWidth(), 1, sAlphaPaint);
2086 c.drawRect(0, mCapture.getHeight() - 1, mCapture.getWidth(),
2087 mCapture.getHeight(), sAlphaPaint);
Dianne Hackborn43cfe8a2011-08-02 16:59:35 -07002088 c.setBitmap(null);
John Reck8ee633f2011-08-09 16:00:35 -07002089 mHandler.removeMessages(MSG_CAPTURE);
John Reck1cf4b792011-07-26 10:22:22 -07002090 persistThumbnail();
John Reck8ee633f2011-08-09 16:00:35 -07002091 TabControl tc = mWebViewController.getTabControl();
2092 if (tc != null) {
2093 OnThumbnailUpdatedListener updateListener
2094 = tc.getOnThumbnailUpdatedListener();
2095 if (updateListener != null) {
2096 updateListener.onThumbnailUpdated(this);
2097 }
2098 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07002099 }
2100
2101 @Override
2102 public void onNewPicture(WebView view, Picture picture) {
John Reck1cf4b792011-07-26 10:22:22 -07002103 postCapture();
2104 }
2105
2106 private void postCapture() {
Michael Kolb9ef259a2011-07-12 15:33:08 -07002107 if (!mHandler.hasMessages(MSG_CAPTURE)) {
2108 mHandler.sendEmptyMessageDelayed(MSG_CAPTURE, CAPTURE_DELAY);
2109 }
2110 }
2111
John Reckef654f12011-07-12 16:42:08 -07002112 public boolean canGoBack() {
2113 return mMainView != null ? mMainView.canGoBack() : false;
2114 }
2115
2116 public boolean canGoForward() {
2117 return mMainView != null ? mMainView.canGoForward() : false;
2118 }
2119
2120 public void goBack() {
2121 if (mMainView != null) {
2122 mMainView.goBack();
2123 }
2124 }
2125
2126 public void goForward() {
2127 if (mMainView != null) {
2128 mMainView.goForward();
2129 }
2130 }
2131
Mathew Inwood1dd8e822011-08-03 14:34:29 +01002132 /**
2133 * Causes the tab back/forward stack to be cleared once, if the given URL is the next URL
2134 * to be added to the stack.
2135 *
2136 * This is used to ensure that preloaded URLs that are not subsequently seen by the user do
2137 * not appear in the back stack.
2138 */
Mathew Inwooda829d552011-09-02 14:16:25 +01002139 public void clearBackStackWhenItemAdded(Pattern urlPattern) {
2140 mClearHistoryUrlPattern = urlPattern;
Mathew Inwood1dd8e822011-08-03 14:34:29 +01002141 }
2142
John Reck1cf4b792011-07-26 10:22:22 -07002143 protected void persistThumbnail() {
John Reck4eadc342011-10-31 14:04:10 -07002144 DataController.getInstance(mContext).saveThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07002145 }
2146
2147 protected void deleteThumbnail() {
John Reck4eadc342011-10-31 14:04:10 -07002148 DataController.getInstance(mContext).deleteThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07002149 }
2150
John Reck4eadc342011-10-31 14:04:10 -07002151 void updateCaptureFromBlob(byte[] blob) {
John Reck1cf4b792011-07-26 10:22:22 -07002152 synchronized (Tab.this) {
2153 if (mCapture == null) {
2154 return;
2155 }
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002156 ByteBuffer buffer = ByteBuffer.wrap(blob);
Michael Kolbd837a112011-08-09 14:00:18 -07002157 try {
2158 mCapture.copyPixelsFromBuffer(buffer);
2159 } catch (RuntimeException rex) {
2160 Log.e(LOGTAG, "Load capture has mismatched sizes; buffer: "
2161 + buffer.capacity() + " blob: " + blob.length
2162 + "capture: " + mCapture.getByteCount());
2163 throw rex;
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002164 }
John Reck1cf4b792011-07-26 10:22:22 -07002165 }
2166 }
2167
John Reck52be4782011-08-26 15:37:29 -07002168 @Override
2169 public String toString() {
2170 StringBuilder builder = new StringBuilder(100);
2171 builder.append(mId);
2172 builder.append(") has parent: ");
2173 if (getParent() != null) {
2174 builder.append("true[");
2175 builder.append(getParent().getId());
2176 builder.append("]");
2177 } else {
2178 builder.append("false");
2179 }
2180 builder.append(", incog: ");
2181 builder.append(isPrivateBrowsingEnabled());
2182 if (!isPrivateBrowsingEnabled()) {
2183 builder.append(", title: ");
2184 builder.append(getTitle());
2185 builder.append(", url: ");
2186 builder.append(getUrl());
2187 }
2188 return builder.toString();
2189 }
2190
Steve Block4895b012011-10-03 16:26:46 +01002191 private void handleProceededAfterSslError(SslError error) {
2192 if (error.getUrl().equals(mCurrentState.mUrl)) {
2193 // The security state should currently be SECURITY_STATE_SECURE.
2194 setSecurityState(SecurityState.SECURITY_STATE_BAD_CERTIFICATE);
Steve Block08a6f0c2011-10-06 12:12:53 +01002195 mCurrentState.mSslCertificateError = error;
Steve Block4895b012011-10-03 16:26:46 +01002196 } else if (getSecurityState() == SecurityState.SECURITY_STATE_SECURE) {
Steve Block08a6f0c2011-10-06 12:12:53 +01002197 // The page's main resource is secure and this error is for a
2198 // sub-resource.
Steve Block4895b012011-10-03 16:26:46 +01002199 setSecurityState(SecurityState.SECURITY_STATE_MIXED);
2200 }
2201 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002202}