blob: 2b91c960cfbcf0d5f61643bf88175a048ed26455 [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
757 public boolean isTabFullScreen() {
758 if (mWebViewController instanceof Controller) {
759 Controller controller = (Controller)mWebViewController;
760 return controller.getUi().isTabFullScreen();
761 } else {
762 return false;
763 }
764 }
765
766 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700767 public boolean onCreateWindow(WebView view, final boolean dialog,
768 final boolean userGesture, final Message resultMsg) {
769 // only allow new window or sub window for the foreground case
770 if (!mInForeground) {
771 return false;
772 }
773 // Short-circuit if we can't create any more tabs or sub windows.
774 if (dialog && mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -0700775 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700776 .setTitle(R.string.too_many_subwindows_dialog_title)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200777 .setIconAttribute(android.R.attr.alertDialogIcon)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700778 .setMessage(R.string.too_many_subwindows_dialog_message)
779 .setPositiveButton(R.string.ok, null)
780 .show();
781 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700782 } else if (!mWebViewController.getTabControl().canCreateNewTab()) {
Michael Kolb14612442011-06-24 13:06:29 -0700783 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700784 .setTitle(R.string.too_many_windows_dialog_title)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200785 .setIconAttribute(android.R.attr.alertDialogIcon)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700786 .setMessage(R.string.too_many_windows_dialog_message)
787 .setPositiveButton(R.string.ok, null)
788 .show();
789 return false;
790 }
791
792 // Short-circuit if this was a user gesture.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800793 if (userGesture || !mSettings.blockPopupWindows()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700794 createWindow(dialog, resultMsg);
795 return true;
796 }
797
798 // Allow the popup and create the appropriate window.
799 final AlertDialog.OnClickListener allowListener =
800 new AlertDialog.OnClickListener() {
801 public void onClick(DialogInterface d,
802 int which) {
803 createWindow(dialog, resultMsg);
804 }
805 };
806
807 // Block the popup by returning a null WebView.
808 final AlertDialog.OnClickListener blockListener =
809 new AlertDialog.OnClickListener() {
810 public void onClick(DialogInterface d, int which) {
811 resultMsg.sendToTarget();
812 }
813 };
814
815 // Build a confirmation dialog to display to the user.
816 final AlertDialog d =
Michael Kolb14612442011-06-24 13:06:29 -0700817 new AlertDialog.Builder(mContext)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200818 .setIconAttribute(android.R.attr.alertDialogIcon)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700819 .setMessage(R.string.popup_window_attempt)
820 .setPositiveButton(R.string.allow, allowListener)
821 .setNegativeButton(R.string.block, blockListener)
822 .setCancelable(false)
823 .create();
824
825 // Show the confirmation dialog.
826 d.show();
827 return true;
828 }
829
830 @Override
Patrick Scotteb5061b2009-11-18 15:00:30 -0500831 public void onRequestFocus(WebView view) {
832 if (!mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700833 mWebViewController.switchToTab(Tab.this);
Patrick Scotteb5061b2009-11-18 15:00:30 -0500834 }
835 }
836
837 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700838 public void onCloseWindow(WebView window) {
Michael Kolbc831b632011-05-11 09:30:34 -0700839 if (mParent != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700840 // JavaScript can only close popup window.
841 if (mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700842 mWebViewController.switchToTab(mParent);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700843 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700844 mWebViewController.closeTab(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700845 }
846 }
847
848 @Override
849 public void onProgressChanged(WebView view, int newProgress) {
John Reck30c714c2010-12-16 17:30:34 -0800850 mPageLoadProgress = newProgress;
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800851 if (newProgress == 100) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800852 Log.i(CONSOLE_LOGTAG, "SWE Pageload Progress = 100");
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800853 mInPageLoad = false;
854 }
John Reck30c714c2010-12-16 17:30:34 -0800855 mWebViewController.onProgressChanged(Tab.this);
Michael Kolb72864272012-05-03 15:42:15 -0700856 if (mUpdateThumbnail && newProgress == 100) {
857 mUpdateThumbnail = false;
858 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700859 }
860
861 @Override
Leon Scroggins21d9b902010-03-11 09:33:11 -0500862 public void onReceivedTitle(WebView view, final String title) {
John Reck30c714c2010-12-16 17:30:34 -0800863 mCurrentState.mTitle = title;
Michael Kolb8233fac2010-10-26 16:08:53 -0700864 mWebViewController.onReceivedTitle(Tab.this, title);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700865 }
866
867 @Override
868 public void onReceivedIcon(WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -0800869 mCurrentState.mFavicon = icon;
Michael Kolb8233fac2010-10-26 16:08:53 -0700870 mWebViewController.onFavicon(Tab.this, view, icon);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700871 }
872
873 @Override
874 public void onReceivedTouchIconUrl(WebView view, String url,
875 boolean precomposed) {
Michael Kolb14612442011-06-24 13:06:29 -0700876 final ContentResolver cr = mContext.getContentResolver();
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400877 // Let precomposed icons take precedence over non-composed
878 // icons.
879 if (precomposed && mTouchIconLoader != null) {
880 mTouchIconLoader.cancel(false);
881 mTouchIconLoader = null;
882 }
883 // Have only one async task at a time.
884 if (mTouchIconLoader == null) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700885 mTouchIconLoader = new DownloadTouchIcon(Tab.this,
Michael Kolb14612442011-06-24 13:06:29 -0700886 mContext, cr, view);
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400887 mTouchIconLoader.execute(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700888 }
889 }
890
891 @Override
892 public void onShowCustomView(View view,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800893 CustomViewCallback callback) {
Michael Kolb14612442011-06-24 13:06:29 -0700894 Activity activity = mWebViewController.getActivity();
895 if (activity != null) {
896 onShowCustomView(view, activity.getRequestedOrientation(), callback);
897 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400898 }
899
900 @Override
901 public void onShowCustomView(View view, int requestedOrientation,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800902 CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700903 if (mInForeground) mWebViewController.showCustomView(Tab.this, view,
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400904 requestedOrientation, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700905 }
906
907 @Override
908 public void onHideCustomView() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700909 if (mInForeground) mWebViewController.hideCustomView();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700910 }
911
912 /**
913 * The origin has exceeded its database quota.
914 * @param url the URL that exceeded the quota
915 * @param databaseIdentifier the identifier of the database on which the
916 * transaction that caused the quota overflow was run
917 * @param currentQuota the current quota for the origin.
918 * @param estimatedSize the estimated size of the database.
919 * @param totalUsedQuota is the sum of all origins' quota.
920 * @param quotaUpdater The callback to run when a decision to allow or
921 * deny quota has been made. Don't forget to call this!
922 */
923 @Override
924 public void onExceededDatabaseQuota(String url,
925 String databaseIdentifier, long currentQuota, long estimatedSize,
926 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -0700927 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -0700928 .onExceededDatabaseQuota(url, databaseIdentifier,
929 currentQuota, estimatedSize, totalUsedQuota,
930 quotaUpdater);
931 }
932
933 /**
934 * The Application Cache has exceeded its max size.
935 * @param spaceNeeded is the amount of disk space that would be needed
936 * in order for the last appcache operation to succeed.
937 * @param totalUsedQuota is the sum of all origins' quota.
938 * @param quotaUpdater A callback to inform the WebCore thread that a
939 * new app cache size is available. This callback must always
940 * be executed at some point to ensure that the sleeping
941 * WebCore thread is woken up.
942 */
943 @Override
944 public void onReachedMaxAppCacheSize(long spaceNeeded,
945 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -0700946 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -0700947 .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota,
948 quotaUpdater);
949 }
950
951 /**
952 * Instructs the browser to show a prompt to ask the user to set the
953 * Geolocation permission state for the specified origin.
954 * @param origin The origin for which Geolocation permissions are
955 * requested.
956 * @param callback The callback to call once the user has set the
957 * Geolocation permission state.
958 */
959 @Override
960 public void onGeolocationPermissionsShowPrompt(String origin,
961 GeolocationPermissions.Callback callback) {
962 if (mInForeground) {
Grace Kloba50c241e2010-04-20 11:07:50 -0700963 getGeolocationPermissionsPrompt().show(origin, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700964 }
965 }
966
967 /**
968 * Instructs the browser to hide the Geolocation permissions prompt.
969 */
970 @Override
971 public void onGeolocationPermissionsHidePrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -0700972 if (mInForeground && mGeolocationPermissionsPrompt != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700973 mGeolocationPermissionsPrompt.hide();
974 }
975 }
976
Ben Murdoch65acc352009-11-19 18:16:04 +0000977 /* Adds a JavaScript error message to the system log and if the JS
978 * console is enabled in the about:debug options, to that console
979 * also.
Ben Murdochc42addf2010-01-28 15:19:59 +0000980 * @param consoleMessage the message object.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700981 */
982 @Override
Ben Murdochc42addf2010-01-28 15:19:59 +0000983 public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700984 if (mInForeground) {
985 // call getErrorConsole(true) so it will create one if needed
986 ErrorConsoleView errorConsole = getErrorConsole(true);
Ben Murdochc42addf2010-01-28 15:19:59 +0000987 errorConsole.addErrorMessage(consoleMessage);
Michael Kolb8233fac2010-10-26 16:08:53 -0700988 if (mWebViewController.shouldShowErrorConsole()
989 && errorConsole.getShowState() !=
990 ErrorConsoleView.SHOW_MAXIMIZED) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700991 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
992 }
993 }
Ben Murdochc42addf2010-01-28 15:19:59 +0000994
Jeff Hamilton47654f42010-09-07 09:57:51 -0500995 // Don't log console messages in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -0700996 if (isPrivateBrowsingEnabled()) return true;
Jeff Hamilton47654f42010-09-07 09:57:51 -0500997
Ben Murdochc42addf2010-01-28 15:19:59 +0000998 String message = "Console: " + consoleMessage.message() + " "
999 + consoleMessage.sourceId() + ":"
1000 + consoleMessage.lineNumber();
1001
1002 switch (consoleMessage.messageLevel()) {
1003 case TIP:
1004 Log.v(CONSOLE_LOGTAG, message);
1005 break;
1006 case LOG:
1007 Log.i(CONSOLE_LOGTAG, message);
1008 break;
1009 case WARNING:
1010 Log.w(CONSOLE_LOGTAG, message);
1011 break;
1012 case ERROR:
1013 Log.e(CONSOLE_LOGTAG, message);
1014 break;
1015 case DEBUG:
1016 Log.d(CONSOLE_LOGTAG, message);
1017 break;
1018 }
1019
1020 return true;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001021 }
1022
1023 /**
1024 * Ask the browser for an icon to represent a <video> element.
1025 * This icon will be used if the Web page did not specify a poster attribute.
1026 * @return Bitmap The icon or null if no such icon is available.
1027 */
1028 @Override
1029 public Bitmap getDefaultVideoPoster() {
1030 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001031 return mWebViewController.getDefaultVideoPoster();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001032 }
1033 return null;
1034 }
1035
1036 /**
1037 * Ask the host application for a custom progress view to show while
1038 * a <video> is loading.
1039 * @return View The progress view.
1040 */
1041 @Override
1042 public View getVideoLoadingProgressView() {
1043 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001044 return mWebViewController.getVideoLoadingProgressView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001045 }
1046 return null;
1047 }
1048
1049 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00001050 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001051 if (mInForeground) {
Ben Murdoch8cad4132012-01-11 10:56:43 +00001052 mWebViewController.openFileChooser(uploadMsg, acceptType, capture);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001053 } else {
1054 uploadMsg.onReceiveValue(null);
1055 }
1056 }
1057
Vivek Sekharb54614f2014-05-01 19:03:37 -07001058 @Override
1059 public void showFileChooser(ValueCallback<String[]> uploadFilePaths, String acceptTypes,
1060 boolean capture) {
1061 if (mInForeground) {
1062 mWebViewController.showFileChooser(uploadFilePaths, acceptTypes, capture);
1063 } else {
1064 uploadFilePaths.onReceiveValue(null);
1065 }
1066 }
1067
Grace Kloba22ac16e2009-10-07 18:00:23 -07001068 /**
1069 * Deliver a list of already-visited URLs
1070 */
1071 @Override
1072 public void getVisitedHistory(final ValueCallback<String[]> callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001073 mWebViewController.getVisitedHistory(callback);
1074 }
Ben Murdoch8029a772010-11-16 11:58:21 +00001075
1076 @Override
1077 public void setupAutoFill(Message message) {
1078 // Prompt the user to set up their profile.
1079 final Message msg = message;
Michael Kolb14612442011-06-24 13:06:29 -07001080 AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
1081 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
Ben Murdoch1d676b62011-01-17 12:54:24 +00001082 Context.LAYOUT_INFLATER_SERVICE);
1083 final View layout = inflater.inflate(R.layout.setup_autofill_dialog, null);
1084
1085 builder.setView(layout)
1086 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1087 @Override
1088 public void onClick(DialogInterface dialog, int id) {
1089 CheckBox disableAutoFill = (CheckBox) layout.findViewById(
1090 R.id.setup_autofill_dialog_disable_autofill);
1091
1092 if (disableAutoFill.isChecked()) {
1093 // Disable autofill and show a toast with how to turn it on again.
John Reck35e9dd62011-04-25 09:01:54 -07001094 mSettings.setAutofillEnabled(false);
Michael Kolb14612442011-06-24 13:06:29 -07001095 Toast.makeText(mContext,
Ben Murdoch1d676b62011-01-17 12:54:24 +00001096 R.string.autofill_setup_dialog_negative_toast,
1097 Toast.LENGTH_LONG).show();
1098 } else {
1099 // Take user to the AutoFill profile editor. When they return,
1100 // we will send the message that we pass here which will trigger
1101 // the form to get filled out with their new profile.
1102 mWebViewController.setupAutoFill(msg);
1103 }
1104 }
1105 })
1106 .setNegativeButton(R.string.cancel, null)
1107 .show();
Ben Murdoch8029a772010-11-16 11:58:21 +00001108 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001109 };
1110
1111 // -------------------------------------------------------------------------
1112 // WebViewClient implementation for the sub window
1113 // -------------------------------------------------------------------------
1114
1115 // Subclass of WebViewClient used in subwindows to notify the main
1116 // WebViewClient of certain WebView activities.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001117 private static class SubWindowClient extends WebViewClient {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001118 // The main WebViewClient.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001119 private final WebViewClient mClient;
Michael Kolb8233fac2010-10-26 16:08:53 -07001120 private final WebViewController mController;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001121
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001122 SubWindowClient(WebViewClient client, WebViewController controller) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001123 mClient = client;
Michael Kolb8233fac2010-10-26 16:08:53 -07001124 mController = controller;
Leon Scroggins III211ba542010-04-19 13:21:13 -04001125 }
1126 @Override
1127 public void onPageStarted(WebView view, String url, Bitmap favicon) {
1128 // Unlike the others, do not call mClient's version, which would
1129 // change the progress bar. However, we do want to remove the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001130 // find or select dialog.
Michael Kolb8233fac2010-10-26 16:08:53 -07001131 mController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001132 }
1133 @Override
1134 public void doUpdateVisitedHistory(WebView view, String url,
1135 boolean isReload) {
1136 mClient.doUpdateVisitedHistory(view, url, isReload);
1137 }
1138 @Override
1139 public boolean shouldOverrideUrlLoading(WebView view, String url) {
1140 return mClient.shouldOverrideUrlLoading(view, url);
1141 }
1142 @Override
1143 public void onReceivedSslError(WebView view, SslErrorHandler handler,
1144 SslError error) {
1145 mClient.onReceivedSslError(view, handler, error);
1146 }
1147 @Override
Brian Carlstrom8862c1d2011-06-02 01:05:55 -07001148 public void onReceivedClientCertRequest(WebView view,
1149 ClientCertRequestHandler handler, String host_and_port) {
1150 mClient.onReceivedClientCertRequest(view, handler, host_and_port);
1151 }
1152 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -07001153 public void onReceivedHttpAuthRequest(WebView view,
1154 HttpAuthHandler handler, String host, String realm) {
1155 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
1156 }
1157 @Override
1158 public void onFormResubmission(WebView view, Message dontResend,
1159 Message resend) {
1160 mClient.onFormResubmission(view, dontResend, resend);
1161 }
1162 @Override
1163 public void onReceivedError(WebView view, int errorCode,
1164 String description, String failingUrl) {
1165 mClient.onReceivedError(view, errorCode, description, failingUrl);
1166 }
1167 @Override
1168 public boolean shouldOverrideKeyEvent(WebView view,
1169 android.view.KeyEvent event) {
1170 return mClient.shouldOverrideKeyEvent(view, event);
1171 }
1172 @Override
1173 public void onUnhandledKeyEvent(WebView view,
1174 android.view.KeyEvent event) {
1175 mClient.onUnhandledKeyEvent(view, event);
1176 }
1177 }
1178
1179 // -------------------------------------------------------------------------
1180 // WebChromeClient implementation for the sub window
1181 // -------------------------------------------------------------------------
1182
1183 private class SubWindowChromeClient extends WebChromeClient {
1184 // The main WebChromeClient.
1185 private final WebChromeClient mClient;
1186
1187 SubWindowChromeClient(WebChromeClient client) {
1188 mClient = client;
1189 }
1190 @Override
1191 public void onProgressChanged(WebView view, int newProgress) {
1192 mClient.onProgressChanged(view, newProgress);
1193 }
1194 @Override
1195 public boolean onCreateWindow(WebView view, boolean dialog,
1196 boolean userGesture, android.os.Message resultMsg) {
1197 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
1198 }
1199 @Override
1200 public void onCloseWindow(WebView window) {
1201 if (window != mSubView) {
1202 Log.e(LOGTAG, "Can't close the window");
1203 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001204 mWebViewController.dismissSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001205 }
1206 }
1207
1208 // -------------------------------------------------------------------------
1209
1210 // Construct a new tab
Michael Kolb7bcafde2011-05-09 13:55:59 -07001211 Tab(WebViewController wvcontroller, WebView w) {
John Reck1cf4b792011-07-26 10:22:22 -07001212 this(wvcontroller, w, null);
1213 }
1214
1215 Tab(WebViewController wvcontroller, Bundle state) {
1216 this(wvcontroller, null, state);
1217 }
1218
1219 Tab(WebViewController wvcontroller, WebView w, Bundle state) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001220 mWebViewController = wvcontroller;
Michael Kolb14612442011-06-24 13:06:29 -07001221 mContext = mWebViewController.getContext();
John Reck35e9dd62011-04-25 09:01:54 -07001222 mSettings = BrowserSettings.getInstance();
Michael Kolb14612442011-06-24 13:06:29 -07001223 mDataController = DataController.getInstance(mContext);
1224 mCurrentState = new PageState(mContext, w != null
John Reck847b5322011-04-14 17:02:18 -07001225 ? w.isPrivateBrowsingEnabled() : false);
Tarun Nainani8084c822014-06-25 13:38:06 -07001226 setTimeStamp();
Michael Kolb8233fac2010-10-26 16:08:53 -07001227 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001228 mInForeground = false;
1229
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001230 mDownloadListener = new BrowserDownloadListener() {
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001231 public void onDownloadStart(String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001232 String contentDisposition, String mimetype, String referer,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001233 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001234 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001235 mimetype, referer, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001236 }
1237 };
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001238 mWebBackForwardListClient = new WebBackForwardListClient() {
1239 @Override
1240 public void onNewHistoryItem(WebHistoryItem item) {
Mathew Inwooda829d552011-09-02 14:16:25 +01001241 if (mClearHistoryUrlPattern != null) {
1242 boolean match =
1243 mClearHistoryUrlPattern.matcher(item.getOriginalUrl()).matches();
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001244 if (LOGD_ENABLED) {
Mathew Inwooda829d552011-09-02 14:16:25 +01001245 Log.d(LOGTAG, "onNewHistoryItem: match=" + match + "\n\t"
1246 + item.getUrl() + "\n\t"
1247 + mClearHistoryUrlPattern);
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001248 }
Mathew Inwooda829d552011-09-02 14:16:25 +01001249 if (match) {
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001250 if (mMainView != null) {
1251 mMainView.clearHistory();
1252 }
1253 }
Mathew Inwooda829d552011-09-02 14:16:25 +01001254 mClearHistoryUrlPattern = null;
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001255 }
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001256 }
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001257 };
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001258
John Reck1cf4b792011-07-26 10:22:22 -07001259 mCaptureWidth = mContext.getResources().getDimensionPixelSize(
1260 R.dimen.tab_thumbnail_width);
1261 mCaptureHeight = mContext.getResources().getDimensionPixelSize(
1262 R.dimen.tab_thumbnail_height);
1263 updateShouldCaptureThumbnails();
1264 restoreState(state);
John Reck52be4782011-08-26 15:37:29 -07001265 if (getId() == -1) {
1266 mId = TabControl.getNextId();
1267 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001268 setWebView(w);
Michael Kolb9ef259a2011-07-12 15:33:08 -07001269 mHandler = new Handler() {
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001270 @Override
Michael Kolb9ef259a2011-07-12 15:33:08 -07001271 public void handleMessage(Message m) {
John Reck1cf4b792011-07-26 10:22:22 -07001272 switch (m.what) {
1273 case MSG_CAPTURE:
1274 capture();
1275 break;
1276 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001277 }
1278 };
John Reck1cf4b792011-07-26 10:22:22 -07001279 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001280
Michael Kolb72864272012-05-03 15:42:15 -07001281 public boolean shouldUpdateThumbnail() {
1282 return mUpdateThumbnail;
1283 }
1284
Mathew Inwoode09305e2011-09-02 12:03:26 +01001285 /**
1286 * This is used to get a new ID when the tab has been preloaded, before it is displayed and
1287 * added to TabControl. Preloaded tabs can be created before restoreInstanceState, leading
1288 * to overlapping IDs between the preloaded and restored tabs.
1289 */
1290 public void refreshIdAfterPreload() {
1291 mId = TabControl.getNextId();
1292 }
1293
John Reck1cf4b792011-07-26 10:22:22 -07001294 public void updateShouldCaptureThumbnails() {
1295 if (mWebViewController.shouldCaptureThumbnails()) {
1296 synchronized (Tab.this) {
1297 if (mCapture == null) {
1298 mCapture = Bitmap.createBitmap(mCaptureWidth, mCaptureHeight,
1299 Bitmap.Config.RGB_565);
Michael Kolbc3af0672011-08-09 10:24:41 -07001300 mCapture.eraseColor(Color.WHITE);
John Reck1cf4b792011-07-26 10:22:22 -07001301 if (mInForeground) {
1302 postCapture();
1303 }
1304 }
1305 }
1306 } else {
1307 synchronized (Tab.this) {
1308 mCapture = null;
1309 deleteThumbnail();
1310 }
1311 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001312 }
1313
Michael Kolb14612442011-06-24 13:06:29 -07001314 public void setController(WebViewController ctl) {
1315 mWebViewController = ctl;
John Reck1cf4b792011-07-26 10:22:22 -07001316 updateShouldCaptureThumbnails();
Michael Kolb14612442011-06-24 13:06:29 -07001317 }
1318
Michael Kolbc831b632011-05-11 09:30:34 -07001319 public long getId() {
1320 return mId;
1321 }
1322
Michael Kolb91911a22012-01-17 11:21:25 -08001323 void setWebView(WebView w) {
1324 setWebView(w, true);
1325 }
1326
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001327 public boolean isNativeActive(){
1328 if (mMainView == null)
1329 return false;
1330 return true;
1331 }
1332
1333 public void setTimeStamp(){
1334 Date d = new Date();
1335 timestamp = (new Timestamp(d.getTime()));
1336 }
1337
1338 public Timestamp getTimestamp() {
1339 return timestamp;
1340 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001341 /**
1342 * Sets the WebView for this tab, correctly removing the old WebView from
1343 * the container view.
1344 */
Michael Kolb91911a22012-01-17 11:21:25 -08001345 void setWebView(WebView w, boolean restore) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001346 if (mMainView == w) {
1347 return;
1348 }
Michael Kolba713ec82010-11-29 17:27:06 -08001349
Grace Kloba22ac16e2009-10-07 18:00:23 -07001350 // If the WebView is changing, the page will be reloaded, so any ongoing
1351 // Geolocation permission requests are void.
Grace Kloba50c241e2010-04-20 11:07:50 -07001352 if (mGeolocationPermissionsPrompt != null) {
1353 mGeolocationPermissionsPrompt.hide();
1354 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001355
Michael Kolba713ec82010-11-29 17:27:06 -08001356 mWebViewController.onSetWebView(this, w);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001357
John Reck1cf4b792011-07-26 10:22:22 -07001358 if (mMainView != null) {
John Reck8ee633f2011-08-09 16:00:35 -07001359 mMainView.setPictureListener(null);
John Reck1cf4b792011-07-26 10:22:22 -07001360 if (w != null) {
1361 syncCurrentState(w, null);
1362 } else {
Panos Thomasa9a5a582014-03-18 19:20:08 -07001363 mCurrentState = new PageState(mContext, mMainView.isPrivateBrowsingEnabled());
John Reck1cf4b792011-07-26 10:22:22 -07001364 }
1365 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001366 // set the new one
1367 mMainView = w;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001368 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001369 if (mMainView != null) {
1370 mMainView.setWebViewClient(mWebViewClient);
1371 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001372 // Attach DownloadManager so that downloads can start in an active
1373 // or a non-active window. This can happen when going to a site that
1374 // does a redirect after a period of time. The user could have
1375 // switched to another tab while waiting for the download to start.
1376 mMainView.setDownloadListener(mDownloadListener);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001377 getWebView().setWebBackForwardListClient(mWebBackForwardListClient);
John Reck8ee633f2011-08-09 16:00:35 -07001378 TabControl tc = mWebViewController.getTabControl();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001379 if (tc != null /*&& tc.getOnThumbnailUpdatedListener() != null*/) {
John Reck8ee633f2011-08-09 16:00:35 -07001380 mMainView.setPictureListener(this);
1381 }
Michael Kolb91911a22012-01-17 11:21:25 -08001382 if (restore && (mSavedState != null)) {
John Reck8b9bb8b2012-03-08 13:19:40 -08001383 restoreUserAgent();
John Reck6c2e2f32011-08-22 13:41:23 -07001384 WebBackForwardList restoredState
1385 = mMainView.restoreState(mSavedState);
1386 if (restoredState == null || restoredState.getSize() == 0) {
1387 Log.w(LOGTAG, "Failed to restore WebView state!");
1388 loadUrl(mCurrentState.mOriginalUrl, null);
1389 }
John Reck1cf4b792011-07-26 10:22:22 -07001390 mSavedState = null;
1391 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001392 }
1393 }
1394
1395 /**
1396 * Destroy the tab's main WebView and subWindow if any
1397 */
1398 void destroy() {
Tarun Nainani2c1dd7c2014-07-05 16:40:12 -07001399
Matthew Huib7f2e9c2014-04-16 11:12:37 -04001400 if (mPostponeDestroy) {
1401 mShouldDestroy = true;
1402 return;
1403 }
1404 mShouldDestroy = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001405 if (mMainView != null) {
1406 dismissSubWindow();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001407 // save the WebView to call destroy() after detach it from the tab
1408 WebView webView = mMainView;
1409 setWebView(null);
1410 webView.destroy();
1411 }
1412 }
1413
Matthew Huib7f2e9c2014-04-16 11:12:37 -04001414 private boolean mPostponeDestroy = false;
1415 private boolean mShouldDestroy = false;
1416
1417 public void postponeDestroy() {
1418 mPostponeDestroy = true;
1419 }
1420
1421 public void performPostponedDestroy() {
1422 mPostponeDestroy = false;
1423 if (mShouldDestroy) {
1424 destroy();
1425 }
1426 }
1427
Grace Kloba22ac16e2009-10-07 18:00:23 -07001428 /**
1429 * Remove the tab from the parent
1430 */
1431 void removeFromTree() {
1432 // detach the children
Michael Kolbc831b632011-05-11 09:30:34 -07001433 if (mChildren != null) {
1434 for(Tab t : mChildren) {
1435 t.setParent(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001436 }
1437 }
1438 // remove itself from the parent list
Michael Kolbc831b632011-05-11 09:30:34 -07001439 if (mParent != null) {
1440 mParent.mChildren.remove(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001441 }
Tarun Nainani2c1dd7c2014-07-05 16:40:12 -07001442 if (mScreenShot != null) {
1443 mScreenShot.recycle();
1444 mScreenShot = null;
1445 }
1446 if (mCapture != null ) {
1447 mCapture.recycle();
1448 mCapture = null;
1449 }
John Reck1cf4b792011-07-26 10:22:22 -07001450 deleteThumbnail();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001451 }
1452
1453 /**
1454 * Create a new subwindow unless a subwindow already exists.
1455 * @return True if a new subwindow was created. False if one already exists.
1456 */
1457 boolean createSubWindow() {
1458 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001459 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001460 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001461 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001462 mSubView.setWebChromeClient(new SubWindowChromeClient(
1463 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001464 // Set a different DownloadListener for the mSubView, since it will
1465 // just need to dismiss the mSubView, rather than close the Tab
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001466 mSubView.setDownloadListener(new BrowserDownloadListener() {
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001467 public void onDownloadStart(String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001468 String contentDisposition, String mimetype, String referer,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001469 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001470 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001471 contentDisposition, mimetype, referer, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001472 if (mSubView.copyBackForwardList().getSize() == 0) {
1473 // This subwindow was opened for the sole purpose of
1474 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001475 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001476 }
1477 }
1478 });
Michael Kolb14612442011-06-24 13:06:29 -07001479 mSubView.setOnCreateContextMenuListener(mWebViewController.getActivity());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001480 return true;
1481 }
1482 return false;
1483 }
1484
1485 /**
1486 * Dismiss the subWindow for the tab.
1487 */
1488 void dismissSubWindow() {
1489 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001490 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001491 mSubView.destroy();
1492 mSubView = null;
1493 mSubViewContainer = null;
1494 }
1495 }
1496
Grace Kloba22ac16e2009-10-07 18:00:23 -07001497
1498 /**
1499 * Set the parent tab of this tab.
1500 */
Michael Kolbc831b632011-05-11 09:30:34 -07001501 void setParent(Tab parent) {
John Reck52be4782011-08-26 15:37:29 -07001502 if (parent == this) {
1503 throw new IllegalStateException("Cannot set parent to self!");
1504 }
Michael Kolbc831b632011-05-11 09:30:34 -07001505 mParent = parent;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001506 // This tab may have been freed due to low memory. If that is the case,
Michael Kolbc831b632011-05-11 09:30:34 -07001507 // the parent tab id is already saved. If we are changing that id
Grace Kloba22ac16e2009-10-07 18:00:23 -07001508 // (most likely due to removing the parent tab) we must update the
Michael Kolbc831b632011-05-11 09:30:34 -07001509 // parent tab id in the saved Bundle.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001510 if (mSavedState != null) {
1511 if (parent == null) {
1512 mSavedState.remove(PARENTTAB);
1513 } else {
Michael Kolbc831b632011-05-11 09:30:34 -07001514 mSavedState.putLong(PARENTTAB, parent.getId());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001515 }
1516 }
John Reckb0a86db2011-05-24 14:05:58 -07001517
1518 // Sync the WebView useragent with the parent
1519 if (parent != null && mSettings.hasDesktopUseragent(parent.getWebView())
1520 != mSettings.hasDesktopUseragent(getWebView())) {
1521 mSettings.toggleDesktopUseragent(getWebView());
1522 }
John Reck52be4782011-08-26 15:37:29 -07001523
1524 if (parent != null && parent.getId() == getId()) {
1525 throw new IllegalStateException("Parent has same ID as child!");
1526 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001527 }
1528
1529 /**
Michael Kolbc831b632011-05-11 09:30:34 -07001530 * If this Tab was created through another Tab, then this method returns
1531 * that Tab.
1532 * @return the Tab parent or null
1533 */
1534 public Tab getParent() {
1535 return mParent;
1536 }
1537
1538 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001539 * When a Tab is created through the content of another Tab, then we
1540 * associate the Tabs.
1541 * @param child the Tab that was created from this Tab
1542 */
1543 void addChildTab(Tab child) {
Michael Kolbc831b632011-05-11 09:30:34 -07001544 if (mChildren == null) {
1545 mChildren = new Vector<Tab>();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001546 }
Michael Kolbc831b632011-05-11 09:30:34 -07001547 mChildren.add(child);
1548 child.setParent(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001549 }
1550
Michael Kolbc831b632011-05-11 09:30:34 -07001551 Vector<Tab> getChildren() {
1552 return mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001553 }
1554
1555 void resume() {
1556 if (mMainView != null) {
John Reck56c1fcf2011-08-17 10:15:16 -07001557 setupHwAcceleration(mMainView);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001558 mMainView.onResume();
1559 if (mSubView != null) {
1560 mSubView.onResume();
1561 }
1562 }
1563 }
1564
John Reck56c1fcf2011-08-17 10:15:16 -07001565 private void setupHwAcceleration(View web) {
1566 if (web == null) return;
1567 BrowserSettings settings = BrowserSettings.getInstance();
1568 if (settings.isHardwareAccelerated()) {
1569 web.setLayerType(View.LAYER_TYPE_NONE, null);
1570 } else {
1571 web.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
1572 }
1573 }
1574
Grace Kloba22ac16e2009-10-07 18:00:23 -07001575 void pause() {
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001576 capture();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001577 if (mMainView != null) {
1578 mMainView.onPause();
1579 if (mSubView != null) {
1580 mSubView.onPause();
1581 }
1582 }
1583 }
1584
1585 void putInForeground() {
John Reck8ee633f2011-08-09 16:00:35 -07001586 if (mInForeground) {
1587 return;
1588 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001589 mInForeground = true;
1590 resume();
Michael Kolb14612442011-06-24 13:06:29 -07001591 Activity activity = mWebViewController.getActivity();
1592 mMainView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001593 if (mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -07001594 mSubView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001595 }
1596 // Show the pending error dialog if the queue is not empty
1597 if (mQueuedErrors != null && mQueuedErrors.size() > 0) {
1598 showError(mQueuedErrors.getFirst());
1599 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001600 mWebViewController.bookmarkedStatusHasChanged(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001601 }
1602
1603 void putInBackground() {
John Reck8ee633f2011-08-09 16:00:35 -07001604 if (!mInForeground) {
1605 return;
1606 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001607 mInForeground = false;
1608 pause();
1609 mMainView.setOnCreateContextMenuListener(null);
1610 if (mSubView != null) {
1611 mSubView.setOnCreateContextMenuListener(null);
1612 }
1613 }
1614
Michael Kolb8233fac2010-10-26 16:08:53 -07001615 boolean inForeground() {
1616 return mInForeground;
1617 }
1618
Grace Kloba22ac16e2009-10-07 18:00:23 -07001619 /**
1620 * Return the top window of this tab; either the subwindow if it is not
1621 * null or the main window.
1622 * @return The top window of this tab.
1623 */
1624 WebView getTopWindow() {
1625 if (mSubView != null) {
1626 return mSubView;
1627 }
1628 return mMainView;
1629 }
1630
1631 /**
1632 * Return the main window of this tab. Note: if a tab is freed in the
1633 * background, this can return null. It is only guaranteed to be
1634 * non-null for the current tab.
1635 * @return The main WebView of this tab.
1636 */
1637 WebView getWebView() {
1638 return mMainView;
1639 }
1640
Michael Kolba713ec82010-11-29 17:27:06 -08001641 void setViewContainer(View container) {
1642 mContainer = container;
1643 }
1644
Michael Kolb8233fac2010-10-26 16:08:53 -07001645 View getViewContainer() {
1646 return mContainer;
1647 }
1648
Grace Kloba22ac16e2009-10-07 18:00:23 -07001649 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001650 * Return whether private browsing is enabled for the main window of
1651 * this tab.
1652 * @return True if private browsing is enabled.
1653 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001654 boolean isPrivateBrowsingEnabled() {
John Reck502a3532011-08-16 14:21:46 -07001655 return mCurrentState.mIncognito;
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001656 }
1657
1658 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001659 * Return the subwindow of this tab or null if there is no subwindow.
1660 * @return The subwindow of this tab or null.
1661 */
1662 WebView getSubWebView() {
1663 return mSubView;
1664 }
1665
Michael Kolb1514bb72010-11-22 09:11:48 -08001666 void setSubWebView(WebView subView) {
1667 mSubView = subView;
1668 }
1669
Michael Kolb8233fac2010-10-26 16:08:53 -07001670 View getSubViewContainer() {
1671 return mSubViewContainer;
1672 }
1673
Michael Kolb1514bb72010-11-22 09:11:48 -08001674 void setSubViewContainer(View subViewContainer) {
1675 mSubViewContainer = subViewContainer;
1676 }
1677
Grace Kloba22ac16e2009-10-07 18:00:23 -07001678 /**
1679 * @return The geolocation permissions prompt for this tab.
1680 */
1681 GeolocationPermissionsPrompt getGeolocationPermissionsPrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001682 if (mGeolocationPermissionsPrompt == null) {
1683 ViewStub stub = (ViewStub) mContainer
1684 .findViewById(R.id.geolocation_permissions_prompt);
1685 mGeolocationPermissionsPrompt = (GeolocationPermissionsPrompt) stub
1686 .inflate();
Grace Kloba50c241e2010-04-20 11:07:50 -07001687 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001688 return mGeolocationPermissionsPrompt;
1689 }
1690
1691 /**
1692 * @return The application id string
1693 */
1694 String getAppId() {
1695 return mAppId;
1696 }
1697
1698 /**
1699 * Set the application id string
1700 * @param id
1701 */
1702 void setAppId(String id) {
1703 mAppId = id;
1704 }
1705
Michael Kolbe28b3472011-08-04 16:54:31 -07001706 boolean closeOnBack() {
1707 return mCloseOnBack;
1708 }
1709
1710 void setCloseOnBack(boolean close) {
1711 mCloseOnBack = close;
1712 }
1713
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001714 boolean getDerivedFromIntent() {
1715 return mDerivedFromIntent;
1716 }
1717
1718 void setDerivedFromIntent(boolean derived) {
1719 mDerivedFromIntent = derived;
1720 }
1721
Grace Kloba22ac16e2009-10-07 18:00:23 -07001722 String getUrl() {
John Reck324d4402011-01-11 16:56:42 -08001723 return UrlUtils.filteredUrl(mCurrentState.mUrl);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001724 }
1725
John Reck49a603c2011-03-03 09:33:05 -08001726 String getOriginalUrl() {
John Reckdb22ec42011-06-29 11:31:24 -07001727 if (mCurrentState.mOriginalUrl == null) {
1728 return getUrl();
John Reck49a603c2011-03-03 09:33:05 -08001729 }
John Reckdb22ec42011-06-29 11:31:24 -07001730 return UrlUtils.filteredUrl(mCurrentState.mOriginalUrl);
John Reck49a603c2011-03-03 09:33:05 -08001731 }
1732
Grace Kloba22ac16e2009-10-07 18:00:23 -07001733 /**
John Reck30c714c2010-12-16 17:30:34 -08001734 * Get the title of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001735 */
1736 String getTitle() {
John Reck30c714c2010-12-16 17:30:34 -08001737 if (mCurrentState.mTitle == null && mInPageLoad) {
Michael Kolb14612442011-06-24 13:06:29 -07001738 return mContext.getString(R.string.title_bar_loading);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001739 }
John Reck30c714c2010-12-16 17:30:34 -08001740 return mCurrentState.mTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001741 }
1742
1743 /**
John Reck30c714c2010-12-16 17:30:34 -08001744 * Get the favicon of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001745 */
1746 Bitmap getFavicon() {
John Reck1cf4b792011-07-26 10:22:22 -07001747 if (mCurrentState.mFavicon != null) {
1748 return mCurrentState.mFavicon;
1749 }
1750 return getDefaultFavicon(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001751 }
1752
John Recke969cc52010-12-21 17:24:43 -08001753 public boolean isBookmarkedSite() {
1754 return mCurrentState.mIsBookmarkedSite;
1755 }
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001756
Grace Kloba22ac16e2009-10-07 18:00:23 -07001757 /**
1758 * Return the tab's error console. Creates the console if createIfNEcessary
1759 * is true and we haven't already created the console.
1760 * @param createIfNecessary Flag to indicate if the console should be
1761 * created if it has not been already.
1762 * @return The tab's error console, or null if one has not been created and
1763 * createIfNecessary is false.
1764 */
1765 ErrorConsoleView getErrorConsole(boolean createIfNecessary) {
1766 if (createIfNecessary && mErrorConsole == null) {
Michael Kolb14612442011-06-24 13:06:29 -07001767 mErrorConsole = new ErrorConsoleView(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001768 mErrorConsole.setWebView(mMainView);
1769 }
1770 return mErrorConsole;
1771 }
1772
Steve Block08a6f0c2011-10-06 12:12:53 +01001773 /**
1774 * Sets the security state, clears the SSL certificate error and informs
1775 * the controller.
1776 */
Steve Block2466eff2011-10-03 15:33:09 +01001777 private void setSecurityState(SecurityState securityState) {
1778 mCurrentState.mSecurityState = securityState;
Steve Block08a6f0c2011-10-06 12:12:53 +01001779 mCurrentState.mSslCertificateError = null;
Steve Block2466eff2011-10-03 15:33:09 +01001780 mWebViewController.onUpdatedSecurityState(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001781 }
1782
1783 /**
Steve Block2466eff2011-10-03 15:33:09 +01001784 * @return The tab's security state.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001785 */
Steve Block2466eff2011-10-03 15:33:09 +01001786 SecurityState getSecurityState() {
1787 return mCurrentState.mSecurityState;
John Reck30c714c2010-12-16 17:30:34 -08001788 }
1789
Steve Block08a6f0c2011-10-06 12:12:53 +01001790 /**
1791 * Gets the SSL certificate error, if any, for the page's main resource.
1792 * This is only non-null when the security state is
1793 * SECURITY_STATE_BAD_CERTIFICATE.
1794 */
1795 SslError getSslCertificateError() {
1796 return mCurrentState.mSslCertificateError;
1797 }
1798
John Reck30c714c2010-12-16 17:30:34 -08001799 int getLoadProgress() {
1800 if (mInPageLoad) {
1801 return mPageLoadProgress;
1802 }
1803 return 100;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001804 }
1805
1806 /**
1807 * @return TRUE if onPageStarted is called while onPageFinished is not
1808 * called yet.
1809 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001810 boolean inPageLoad() {
1811 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001812 }
1813
Grace Kloba22ac16e2009-10-07 18:00:23 -07001814 /**
John Reck1cf4b792011-07-26 10:22:22 -07001815 * @return The Bundle with the tab's state if it can be saved, otherwise null
Grace Kloba22ac16e2009-10-07 18:00:23 -07001816 */
John Reck1cf4b792011-07-26 10:22:22 -07001817 public Bundle saveState() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001818 // If the WebView is null it means we ran low on memory and we already
1819 // stored the saved state in mSavedState.
1820 if (mMainView == null) {
John Reck1cf4b792011-07-26 10:22:22 -07001821 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001822 }
John Reck6c2e2f32011-08-22 13:41:23 -07001823
1824 if (TextUtils.isEmpty(mCurrentState.mUrl)) {
John Reck1cf4b792011-07-26 10:22:22 -07001825 return null;
John Reck24f18262011-06-17 14:47:20 -07001826 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001827
1828 mSavedState = new Bundle();
John Reck6c2e2f32011-08-22 13:41:23 -07001829 WebBackForwardList savedList = mMainView.saveState(mSavedState);
1830 if (savedList == null || savedList.getSize() == 0) {
1831 Log.w(LOGTAG, "Failed to save back/forward list for "
1832 + mCurrentState.mUrl);
1833 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001834
Michael Kolbc831b632011-05-11 09:30:34 -07001835 mSavedState.putLong(ID, mId);
John Reck30c714c2010-12-16 17:30:34 -08001836 mSavedState.putString(CURRURL, mCurrentState.mUrl);
1837 mSavedState.putString(CURRTITLE, mCurrentState.mTitle);
John Reck1cf4b792011-07-26 10:22:22 -07001838 mSavedState.putBoolean(INCOGNITO, mMainView.isPrivateBrowsingEnabled());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001839 if (mAppId != null) {
1840 mSavedState.putString(APPID, mAppId);
1841 }
Michael Kolbe28b3472011-08-04 16:54:31 -07001842 mSavedState.putBoolean(CLOSEFLAG, mCloseOnBack);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001843 // Remember the parent tab so the relationship can be restored.
Michael Kolbc831b632011-05-11 09:30:34 -07001844 if (mParent != null) {
1845 mSavedState.putLong(PARENTTAB, mParent.mId);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001846 }
John Reckb0a86db2011-05-24 14:05:58 -07001847 mSavedState.putBoolean(USERAGENT,
1848 mSettings.hasDesktopUseragent(getWebView()));
John Reck1cf4b792011-07-26 10:22:22 -07001849 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001850 }
1851
1852 /*
1853 * Restore the state of the tab.
1854 */
John Reck1cf4b792011-07-26 10:22:22 -07001855 private void restoreState(Bundle b) {
1856 mSavedState = b;
1857 if (mSavedState == null) {
1858 return;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001859 }
1860 // Restore the internal state even if the WebView fails to restore.
1861 // This will maintain the app id, original url and close-on-exit values.
Michael Kolbc831b632011-05-11 09:30:34 -07001862 mId = b.getLong(ID);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001863 mAppId = b.getString(APPID);
Michael Kolbe28b3472011-08-04 16:54:31 -07001864 mCloseOnBack = b.getBoolean(CLOSEFLAG);
John Reck8b9bb8b2012-03-08 13:19:40 -08001865 restoreUserAgent();
John Reck1cf4b792011-07-26 10:22:22 -07001866 String url = b.getString(CURRURL);
1867 String title = b.getString(CURRTITLE);
1868 boolean incognito = b.getBoolean(INCOGNITO);
1869 mCurrentState = new PageState(mContext, incognito, url, null);
1870 mCurrentState.mTitle = title;
1871 synchronized (Tab.this) {
1872 if (mCapture != null) {
John Reck4eadc342011-10-31 14:04:10 -07001873 DataController.getInstance(mContext).loadThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07001874 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001875 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001876 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04001877
John Reck8b9bb8b2012-03-08 13:19:40 -08001878 private void restoreUserAgent() {
1879 if (mMainView == null || mSavedState == null) {
1880 return;
1881 }
1882 if (mSavedState.getBoolean(USERAGENT)
1883 != mSettings.hasDesktopUseragent(mMainView)) {
1884 mSettings.toggleDesktopUseragent(mMainView);
1885 }
1886 }
1887
Leon Scroggins1961ed22010-12-07 15:22:21 -05001888 public void updateBookmarkedStatus() {
John Recke969cc52010-12-21 17:24:43 -08001889 mDataController.queryBookmarkStatus(getUrl(), mIsBookmarkCallback);
Leon Scroggins1961ed22010-12-07 15:22:21 -05001890 }
1891
John Recke969cc52010-12-21 17:24:43 -08001892 private DataController.OnQueryUrlIsBookmark mIsBookmarkCallback
1893 = new DataController.OnQueryUrlIsBookmark() {
1894 @Override
1895 public void onQueryUrlIsBookmark(String url, boolean isBookmark) {
1896 if (mCurrentState.mUrl.equals(url)) {
1897 mCurrentState.mIsBookmarkedSite = isBookmark;
1898 mWebViewController.bookmarkedStatusHasChanged(Tab.this);
1899 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001900 }
John Recke969cc52010-12-21 17:24:43 -08001901 };
Michael Kolb1acef692011-03-08 14:12:06 -08001902
Michael Kolbeb95db42011-03-03 10:38:40 -08001903 public Bitmap getScreenshot() {
John Reck1cf4b792011-07-26 10:22:22 -07001904 synchronized (Tab.this) {
1905 return mCapture;
1906 }
Michael Kolbeb95db42011-03-03 10:38:40 -08001907 }
1908
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001909 public Bitmap getFullScreenshot() {
1910 synchronized (Tab.this) {
1911 return mScreenShot;
1912 }
1913 }
1914
John Reck541f55a2011-06-07 16:34:43 -07001915 public boolean isSnapshot() {
John Reck541f55a2011-06-07 16:34:43 -07001916 return false;
1917 }
1918
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001919 private static class SaveCallback implements ValueCallback<String> {
1920 boolean onReceiveValueCalled = false;
1921 private String mPath;
John Reck68234a92012-04-19 15:27:12 -07001922
1923 @Override
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001924 public void onReceiveValue(String path) {
1925 this.onReceiveValueCalled = true;
1926 this.mPath = path;
John Reck68234a92012-04-19 15:27:12 -07001927 synchronized (this) {
1928 notifyAll();
John Reck8cc92352011-07-06 17:41:52 -07001929 }
John Reck541f55a2011-06-07 16:34:43 -07001930 }
John Reck68234a92012-04-19 15:27:12 -07001931
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001932 public String getPath() {
1933 return mPath;
1934 }
John Reck68234a92012-04-19 15:27:12 -07001935 }
1936
1937 /**
1938 * Must be called on the UI thread
1939 */
1940 public ContentValues createSnapshotValues() {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001941 WebView web = getWebView();
John Reck68234a92012-04-19 15:27:12 -07001942 if (web == null) return null;
John Reckd8c74522011-06-14 08:45:00 -07001943 ContentValues values = new ContentValues();
1944 values.put(Snapshots.TITLE, mCurrentState.mTitle);
1945 values.put(Snapshots.URL, mCurrentState.mUrl);
John Reck68234a92012-04-19 15:27:12 -07001946 values.put(Snapshots.BACKGROUND, web.getPageBackgroundColor());
John Reck8cc92352011-07-06 17:41:52 -07001947 values.put(Snapshots.DATE_CREATED, System.currentTimeMillis());
1948 values.put(Snapshots.FAVICON, compressBitmap(getFavicon()));
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001949 Bitmap screenshot = web.getViewportBitmap();
John Reck8cc92352011-07-06 17:41:52 -07001950 values.put(Snapshots.THUMBNAIL, compressBitmap(screenshot));
John Reckd8c74522011-06-14 08:45:00 -07001951 return values;
John Reck541f55a2011-06-07 16:34:43 -07001952 }
1953
John Reck68234a92012-04-19 15:27:12 -07001954 /**
1955 * Probably want to call this on a background thread
1956 */
1957 public boolean saveViewState(ContentValues values) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001958 WebView web = getWebView();
John Reck68234a92012-04-19 15:27:12 -07001959 if (web == null) return false;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001960 String filename = UUID.randomUUID().toString();
John Reck68234a92012-04-19 15:27:12 -07001961 SaveCallback callback = new SaveCallback();
John Reck68234a92012-04-19 15:27:12 -07001962 try {
John Reck68234a92012-04-19 15:27:12 -07001963 synchronized (callback) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001964 web.saveViewState(filename, callback);
1965 callback.wait();
John Reck68234a92012-04-19 15:27:12 -07001966 }
John Reck68234a92012-04-19 15:27:12 -07001967 } catch (Exception e) {
1968 Log.w(LOGTAG, "Failed to save view state", e);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001969 String path = callback.getPath();
1970 if (path != null) {
1971 File file = mContext.getFileStreamPath(path);
1972 if (file.exists() && !file.delete()) {
1973 file.deleteOnExit();
1974 }
John Reck68234a92012-04-19 15:27:12 -07001975 }
1976 return false;
1977 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001978 String path = callback.getPath();
1979 File savedFile = new File(path);
1980 if (!savedFile.exists()) {
1981 return false;
John Reck68234a92012-04-19 15:27:12 -07001982 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001983 values.put(Snapshots.VIEWSTATE_PATH, path.substring(path.lastIndexOf('/') + 1));
1984 values.put(Snapshots.VIEWSTATE_SIZE, savedFile.length());
John Reck68234a92012-04-19 15:27:12 -07001985 return true;
1986 }
1987
John Reck8cc92352011-07-06 17:41:52 -07001988 public byte[] compressBitmap(Bitmap bitmap) {
1989 if (bitmap == null) {
1990 return null;
1991 }
1992 ByteArrayOutputStream stream = new ByteArrayOutputStream();
1993 bitmap.compress(CompressFormat.PNG, 100, stream);
1994 return stream.toByteArray();
1995 }
1996
John Reck26b18322011-06-21 13:08:58 -07001997 public void loadUrl(String url, Map<String, String> headers) {
1998 if (mMainView != null) {
Michael Kolba53c9892011-10-05 13:31:40 -07001999 mPageLoadProgress = INITIAL_PROGRESS;
2000 mInPageLoad = true;
Michael Kolb14612442011-06-24 13:06:29 -07002001 mCurrentState = new PageState(mContext, false, url, null);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002002 handleJsInterface(mMainView, url);
John Reck26b18322011-06-21 13:08:58 -07002003 mMainView.loadUrl(url, headers);
2004 }
2005 }
2006
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002007 public void handleJsInterface(WebView webview, String url){
2008 if (url != null &&
2009 url.equals(mContext.getResources().getString(R.string.homepage_base)) &&
2010 url.startsWith("file:///")) {
2011 mJsInterfaceEnabled = true;
2012 webview.getSettings().setJavaScriptEnabled(true);
2013 webview.addJavascriptInterface(mContext, "default_homepage");
2014 } else {
2015 if (mJsInterfaceEnabled) {
2016 webview.removeJavascriptInterface("default_homepage");
2017 mJsInterfaceEnabled = false;
2018 }
2019 }
2020 }
2021
John Reck38b39652012-06-05 09:22:59 -07002022 public void disableUrlOverridingForLoad() {
2023 mDisableOverrideUrlLoading = true;
2024 }
2025
Michael Kolb9ef259a2011-07-12 15:33:08 -07002026 protected void capture() {
2027 if (mMainView == null || mCapture == null) return;
John Reck4eadc342011-10-31 14:04:10 -07002028 if (mMainView.getContentWidth() <= 0 || mMainView.getContentHeight() <= 0) {
2029 return;
2030 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07002031 Canvas c = new Canvas(mCapture);
Michael Kolba3194d02011-09-07 11:23:51 -07002032 int state = c.save();
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002033 float scale = 0;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002034 Bitmap screenShot = mMainView.getViewportBitmap();
Vivek Sekhar0ab52512014-07-02 14:51:32 -07002035 if (mScreenShot != null) {
2036 mScreenShot.recycle();
2037 mScreenShot = null;
2038 }
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002039 mScreenShot = screenShot;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002040 if (screenShot != null) {
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002041 //scale based on device orientation
2042 if (screenShot.getHeight() > screenShot.getWidth()){
2043 scale = (float) mCaptureWidth / screenShot.getWidth();
2044 } else {
2045 scale = (float) mCaptureHeight / screenShot.getHeight();
2046 }
2047 mCapture.eraseColor(Color.WHITE);
2048 c.scale(scale, scale);
2049 c.drawBitmap(screenShot, 0, 0, null);
2050 } else {
2051 final int left = mMainView.getViewScrollX();
2052 final int top = mMainView.getViewScrollY() + mMainView.getVisibleTitleHeight();
2053
2054 if (mMainView.getHeight() > mMainView.getWidth()){
2055 scale = mCaptureWidth / (float) mMainView.getWidth();
2056 } else {
2057 scale = mCaptureHeight / (float) mMainView.getHeight();
2058 }
2059
2060 c.translate(-left, -top);
2061 c.scale(scale, scale, left, top);
2062 if (mMainView instanceof BrowserWebView) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002063 ((BrowserWebView)mMainView).drawContent(c);
2064 } else {
2065 mMainView.draw(c);
2066 }
John Reck8ee633f2011-08-09 16:00:35 -07002067 }
Michael Kolba3194d02011-09-07 11:23:51 -07002068 c.restoreToCount(state);
2069 // manually anti-alias the edges for the tilt
2070 c.drawRect(0, 0, 1, mCapture.getHeight(), sAlphaPaint);
2071 c.drawRect(mCapture.getWidth() - 1, 0, mCapture.getWidth(),
2072 mCapture.getHeight(), sAlphaPaint);
2073 c.drawRect(0, 0, mCapture.getWidth(), 1, sAlphaPaint);
2074 c.drawRect(0, mCapture.getHeight() - 1, mCapture.getWidth(),
2075 mCapture.getHeight(), sAlphaPaint);
Dianne Hackborn43cfe8a2011-08-02 16:59:35 -07002076 c.setBitmap(null);
John Reck8ee633f2011-08-09 16:00:35 -07002077 mHandler.removeMessages(MSG_CAPTURE);
John Reck1cf4b792011-07-26 10:22:22 -07002078 persistThumbnail();
John Reck8ee633f2011-08-09 16:00:35 -07002079 TabControl tc = mWebViewController.getTabControl();
2080 if (tc != null) {
2081 OnThumbnailUpdatedListener updateListener
2082 = tc.getOnThumbnailUpdatedListener();
2083 if (updateListener != null) {
2084 updateListener.onThumbnailUpdated(this);
2085 }
2086 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07002087 }
2088
2089 @Override
2090 public void onNewPicture(WebView view, Picture picture) {
John Reck1cf4b792011-07-26 10:22:22 -07002091 postCapture();
2092 }
2093
2094 private void postCapture() {
Michael Kolb9ef259a2011-07-12 15:33:08 -07002095 if (!mHandler.hasMessages(MSG_CAPTURE)) {
2096 mHandler.sendEmptyMessageDelayed(MSG_CAPTURE, CAPTURE_DELAY);
2097 }
2098 }
2099
John Reckef654f12011-07-12 16:42:08 -07002100 public boolean canGoBack() {
2101 return mMainView != null ? mMainView.canGoBack() : false;
2102 }
2103
2104 public boolean canGoForward() {
2105 return mMainView != null ? mMainView.canGoForward() : false;
2106 }
2107
2108 public void goBack() {
2109 if (mMainView != null) {
2110 mMainView.goBack();
2111 }
2112 }
2113
2114 public void goForward() {
2115 if (mMainView != null) {
2116 mMainView.goForward();
2117 }
2118 }
2119
Mathew Inwood1dd8e822011-08-03 14:34:29 +01002120 /**
2121 * Causes the tab back/forward stack to be cleared once, if the given URL is the next URL
2122 * to be added to the stack.
2123 *
2124 * This is used to ensure that preloaded URLs that are not subsequently seen by the user do
2125 * not appear in the back stack.
2126 */
Mathew Inwooda829d552011-09-02 14:16:25 +01002127 public void clearBackStackWhenItemAdded(Pattern urlPattern) {
2128 mClearHistoryUrlPattern = urlPattern;
Mathew Inwood1dd8e822011-08-03 14:34:29 +01002129 }
2130
John Reck1cf4b792011-07-26 10:22:22 -07002131 protected void persistThumbnail() {
John Reck4eadc342011-10-31 14:04:10 -07002132 DataController.getInstance(mContext).saveThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07002133 }
2134
2135 protected void deleteThumbnail() {
John Reck4eadc342011-10-31 14:04:10 -07002136 DataController.getInstance(mContext).deleteThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07002137 }
2138
John Reck4eadc342011-10-31 14:04:10 -07002139 void updateCaptureFromBlob(byte[] blob) {
John Reck1cf4b792011-07-26 10:22:22 -07002140 synchronized (Tab.this) {
2141 if (mCapture == null) {
2142 return;
2143 }
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002144 ByteBuffer buffer = ByteBuffer.wrap(blob);
Michael Kolbd837a112011-08-09 14:00:18 -07002145 try {
2146 mCapture.copyPixelsFromBuffer(buffer);
2147 } catch (RuntimeException rex) {
2148 Log.e(LOGTAG, "Load capture has mismatched sizes; buffer: "
2149 + buffer.capacity() + " blob: " + blob.length
2150 + "capture: " + mCapture.getByteCount());
2151 throw rex;
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002152 }
John Reck1cf4b792011-07-26 10:22:22 -07002153 }
2154 }
2155
John Reck52be4782011-08-26 15:37:29 -07002156 @Override
2157 public String toString() {
2158 StringBuilder builder = new StringBuilder(100);
2159 builder.append(mId);
2160 builder.append(") has parent: ");
2161 if (getParent() != null) {
2162 builder.append("true[");
2163 builder.append(getParent().getId());
2164 builder.append("]");
2165 } else {
2166 builder.append("false");
2167 }
2168 builder.append(", incog: ");
2169 builder.append(isPrivateBrowsingEnabled());
2170 if (!isPrivateBrowsingEnabled()) {
2171 builder.append(", title: ");
2172 builder.append(getTitle());
2173 builder.append(", url: ");
2174 builder.append(getUrl());
2175 }
2176 return builder.toString();
2177 }
2178
Steve Block4895b012011-10-03 16:26:46 +01002179 private void handleProceededAfterSslError(SslError error) {
2180 if (error.getUrl().equals(mCurrentState.mUrl)) {
2181 // The security state should currently be SECURITY_STATE_SECURE.
2182 setSecurityState(SecurityState.SECURITY_STATE_BAD_CERTIFICATE);
Steve Block08a6f0c2011-10-06 12:12:53 +01002183 mCurrentState.mSslCertificateError = error;
Steve Block4895b012011-10-03 16:26:46 +01002184 } else if (getSecurityState() == SecurityState.SECURITY_STATE_SECURE) {
Steve Block08a6f0c2011-10-06 12:12:53 +01002185 // The page's main resource is secure and this error is for a
2186 // sub-resource.
Steve Block4895b012011-10-03 16:26:46 +01002187 setSecurityState(SecurityState.SECURITY_STATE_MIXED);
2188 }
2189 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002190}