blob: 15ebd5e7982ef4255387c5e3ac7829cfff010074 [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;
Tarun Nainaniea28dde2014-08-27 17:25:09 -070036import android.graphics.Rect;
Grace Kloba22ac16e2009-10-07 18:00:23 -070037import android.net.Uri;
38import android.net.http.SslError;
Grace Kloba22ac16e2009-10-07 18:00:23 -070039import android.os.Bundle;
Michael Kolb9ef259a2011-07-12 15:33:08 -070040import android.os.Handler;
Grace Kloba22ac16e2009-10-07 18:00:23 -070041import android.os.Message;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +000042import android.os.SystemClock;
Brian Carlstrom8862c1d2011-06-02 01:05:55 -070043import android.security.KeyChain;
Brian Carlstromaa09cd82011-06-09 16:04:40 -070044import android.security.KeyChainAliasCallback;
John Reck24f18262011-06-17 14:47:20 -070045import android.text.TextUtils;
Grace Kloba22ac16e2009-10-07 18:00:23 -070046import android.util.Log;
47import android.view.KeyEvent;
48import android.view.LayoutInflater;
49import android.view.View;
Grace Kloba50c241e2010-04-20 11:07:50 -070050import android.view.ViewStub;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080051import android.view.View.OnClickListener;
Ben Murdochc42addf2010-01-28 15:19:59 +000052import android.webkit.ConsoleMessage;
Grace Kloba22ac16e2009-10-07 18:00:23 -070053import android.webkit.GeolocationPermissions;
Grace Kloba22ac16e2009-10-07 18:00:23 -070054import android.webkit.URLUtil;
John Reck438bf462011-01-12 18:11:46 -080055import android.webkit.WebResourceResponse;
Grace Kloba22ac16e2009-10-07 18:00:23 -070056import android.webkit.WebStorage;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080057import android.webkit.WebChromeClient.CustomViewCallback;
58import android.webkit.ValueCallback;
Ben Murdoch1d676b62011-01-17 12:54:24 +000059import android.widget.CheckBox;
Ben Murdoch8029a772010-11-16 11:58:21 +000060import android.widget.Toast;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080061import android.widget.FrameLayout;
62import android.widget.Button;
Grace Kloba22ac16e2009-10-07 18:00:23 -070063
Bijan Amirzada41242f22014-03-21 12:12:18 -070064import com.android.browser.R;
65import com.android.browser.TabControl.OnThumbnailUpdatedListener;
66import com.android.browser.homepages.HomeProvider;
67import com.android.browser.mynavigation.MyNavigationUtil;
68import com.android.browser.provider.MyNavigationProvider;
69import com.android.browser.provider.SnapshotProvider.Snapshots;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080070
71import org.codeaurora.swe.BrowserDownloadListener;
72import org.codeaurora.swe.ClientCertRequestHandler;
73import org.codeaurora.swe.HttpAuthHandler;
74import org.codeaurora.swe.SslErrorHandler;
75import org.codeaurora.swe.WebBackForwardList;
76import org.codeaurora.swe.WebBackForwardListClient;
77import org.codeaurora.swe.WebChromeClient;
78import org.codeaurora.swe.WebHistoryItem;
79import org.codeaurora.swe.WebView;
80import org.codeaurora.swe.WebView.PictureListener;
81import org.codeaurora.swe.WebViewClient;
82
John Reck541f55a2011-06-07 16:34:43 -070083
84import java.io.ByteArrayOutputStream;
John Reck2b71d6d2012-04-18 17:42:06 -070085import java.io.File;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080086import java.io.InputStream;
John Reck1cf4b792011-07-26 10:22:22 -070087import java.nio.ByteBuffer;
Michael Kolbfe251992010-07-08 15:41:55 -070088import java.util.LinkedList;
89import java.util.Map;
John Reck2b71d6d2012-04-18 17:42:06 -070090import java.util.UUID;
Michael Kolbfe251992010-07-08 15:41:55 -070091import java.util.Vector;
Mathew Inwooda829d552011-09-02 14:16:25 +010092import java.util.regex.Pattern;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080093import java.sql.Timestamp;
94import java.util.Date;
Michael Kolbfe251992010-07-08 15:41:55 -070095
Grace Kloba22ac16e2009-10-07 18:00:23 -070096/**
97 * Class for maintaining Tabs with a main WebView and a subwindow.
98 */
Michael Kolb9ef259a2011-07-12 15:33:08 -070099class Tab implements PictureListener {
Michael Kolb8233fac2010-10-26 16:08:53 -0700100
Grace Kloba22ac16e2009-10-07 18:00:23 -0700101 // Log Tag
102 private static final String LOGTAG = "Tab";
Bijan Amirzada41242f22014-03-21 12:12:18 -0700103 private static final boolean LOGD_ENABLED = com.android.browser.Browser.LOGD_ENABLED;
Ben Murdochc42addf2010-01-28 15:19:59 +0000104 // Special case the logtag for messages for the Console to make it easier to
105 // filter them and match the logtag used for these messages in older versions
106 // of the browser.
107 private static final String CONSOLE_LOGTAG = "browser";
108
Michael Kolb9ef259a2011-07-12 15:33:08 -0700109 private static final int MSG_CAPTURE = 42;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800110 private static final int CAPTURE_DELAY = 1000;
Michael Kolba53c9892011-10-05 13:31:40 -0700111 private static final int INITIAL_PROGRESS = 5;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700112
John Reck1cf4b792011-07-26 10:22:22 -0700113 private static Bitmap sDefaultFavicon;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800114 protected boolean hasCrashed = false;
John Reck1cf4b792011-07-26 10:22:22 -0700115
Michael Kolba3194d02011-09-07 11:23:51 -0700116 private static Paint sAlphaPaint = new Paint();
117 static {
118 sAlphaPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
119 sAlphaPaint.setColor(Color.TRANSPARENT);
120 }
121
Steve Block2466eff2011-10-03 15:33:09 +0100122 public enum SecurityState {
Steve Block4895b012011-10-03 16:26:46 +0100123 // The page's main resource does not use SSL. Note that we use this
124 // state irrespective of the SSL authentication state of sub-resources.
Steve Block2466eff2011-10-03 15:33:09 +0100125 SECURITY_STATE_NOT_SECURE,
Steve Block4895b012011-10-03 16:26:46 +0100126 // The page's main resource uses SSL and the certificate is good. The
127 // same is true of all sub-resources.
Steve Block2466eff2011-10-03 15:33:09 +0100128 SECURITY_STATE_SECURE,
Steve Block4895b012011-10-03 16:26:46 +0100129 // The page's main resource uses SSL and the certificate is good, but
130 // some sub-resources either do not use SSL or have problems with their
131 // certificates.
Steve Block2466eff2011-10-03 15:33:09 +0100132 SECURITY_STATE_MIXED,
Steve Block4895b012011-10-03 16:26:46 +0100133 // The page's main resource uses SSL but there is a problem with its
134 // certificate.
135 SECURITY_STATE_BAD_CERTIFICATE,
John Reck30c714c2010-12-16 17:30:34 -0800136 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700137
Michael Kolb14612442011-06-24 13:06:29 -0700138 Context mContext;
John Reckd8c74522011-06-14 08:45:00 -0700139 protected WebViewController mWebViewController;
Michael Kolb8233fac2010-10-26 16:08:53 -0700140
Michael Kolbc831b632011-05-11 09:30:34 -0700141 // The tab ID
John Reckd8c74522011-06-14 08:45:00 -0700142 private long mId = -1;
Michael Kolbc831b632011-05-11 09:30:34 -0700143
Grace Kloba22ac16e2009-10-07 18:00:23 -0700144 // The Geolocation permissions prompt
145 private GeolocationPermissionsPrompt mGeolocationPermissionsPrompt;
146 // Main WebView wrapper
Michael Kolba713ec82010-11-29 17:27:06 -0800147 private View mContainer;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700148 // Main WebView
149 private WebView mMainView;
150 // Subwindow container
151 private View mSubViewContainer;
152 // Subwindow WebView
153 private WebView mSubView;
154 // Saved bundle for when we are running low on memory. It contains the
155 // information needed to restore the WebView if the user goes back to the
156 // tab.
157 private Bundle mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700158 // Parent Tab. This is the Tab that created this Tab, or null if the Tab was
159 // created by the UI
Michael Kolbc831b632011-05-11 09:30:34 -0700160 private Tab mParent;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700161 // Tab that constructed by this Tab. This is used when this Tab is
162 // destroyed, it clears all mParentTab values in the children.
Michael Kolbc831b632011-05-11 09:30:34 -0700163 private Vector<Tab> mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700164 // If true, the tab is in the foreground of the current activity.
165 private boolean mInForeground;
Michael Kolb8233fac2010-10-26 16:08:53 -0700166 // If true, the tab is in page loading state (after onPageStarted,
167 // before onPageFinsihed)
168 private boolean mInPageLoad;
Tarun Nainani8eb00912014-07-17 12:28:32 -0700169 private boolean mPageFinished;
John Reck38b39652012-06-05 09:22:59 -0700170 private boolean mDisableOverrideUrlLoading;
John Reck30c714c2010-12-16 17:30:34 -0800171 // The last reported progress of the current page
172 private int mPageLoadProgress;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000173 // The time the load started, used to find load page time
174 private long mLoadStartTime;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700175 // Application identifier used to find tabs that another application wants
176 // to reuse.
177 private String mAppId;
Michael Kolbe28b3472011-08-04 16:54:31 -0700178 // flag to indicate if tab should be closed on back
179 private boolean mCloseOnBack;
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700180 // flag to indicate if the tab was opened from an intent
181 private boolean mDerivedFromIntent = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700182 // Keep the original url around to avoid killing the old WebView if the url
183 // has not changed.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700184 // Error console for the tab
185 private ErrorConsoleView mErrorConsole;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -0500186 // The listener that gets invoked when a download is started from the
187 // mMainView
Selim Gurun0b3d66f2012-08-29 13:08:13 -0700188 private final BrowserDownloadListener mDownloadListener;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500189 // Listener used to know when we move forward or back in the history list.
190 private final WebBackForwardListClient mWebBackForwardListClient;
John Recke969cc52010-12-21 17:24:43 -0800191 private DataController mDataController;
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700192 //Indicates if a JS interface was created for a specific url
193 private boolean mJsInterfaceEnabled = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700194
195 // AsyncTask for downloading touch icons
196 DownloadTouchIcon mTouchIconLoader;
197
John Reck35e9dd62011-04-25 09:01:54 -0700198 private BrowserSettings mSettings;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700199 private int mCaptureWidth;
200 private int mCaptureHeight;
201 private Bitmap mCapture;
202 private Handler mHandler;
Michael Kolb72864272012-05-03 15:42:15 -0700203 private boolean mUpdateThumbnail;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800204 private Timestamp timestamp;
Vivek Sekharf96064b2014-07-28 16:32:34 -0700205 private boolean mFullScreen = false;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700206
Mathew Inwood1dd8e822011-08-03 14:34:29 +0100207 /**
Mathew Inwood9ad1eac2011-09-15 11:29:50 +0100208 * See {@link #clearBackStackWhenItemAdded(String)}.
Mathew Inwood1dd8e822011-08-03 14:34:29 +0100209 */
Mathew Inwooda829d552011-09-02 14:16:25 +0100210 private Pattern mClearHistoryUrlPattern;
Mathew Inwood1dd8e822011-08-03 14:34:29 +0100211
John Reck1cf4b792011-07-26 10:22:22 -0700212 private static synchronized Bitmap getDefaultFavicon(Context context) {
213 if (sDefaultFavicon == null) {
214 sDefaultFavicon = BitmapFactory.decodeResource(
215 context.getResources(), R.drawable.app_web_browser_sm);
216 }
217 return sDefaultFavicon;
218 }
Michael Kolbeb95db42011-03-03 10:38:40 -0800219
John Reck30c714c2010-12-16 17:30:34 -0800220 // All the state needed for a page
John Reckd8c74522011-06-14 08:45:00 -0700221 protected static class PageState {
John Reck30c714c2010-12-16 17:30:34 -0800222 String mUrl;
John Reckdb22ec42011-06-29 11:31:24 -0700223 String mOriginalUrl;
John Reck30c714c2010-12-16 17:30:34 -0800224 String mTitle;
Steve Block2466eff2011-10-03 15:33:09 +0100225 SecurityState mSecurityState;
Steve Block08a6f0c2011-10-06 12:12:53 +0100226 // This is non-null only when mSecurityState is SECURITY_STATE_BAD_CERTIFICATE.
227 SslError mSslCertificateError;
John Reck30c714c2010-12-16 17:30:34 -0800228 Bitmap mFavicon;
Steve Block08a6f0c2011-10-06 12:12:53 +0100229 boolean mIsBookmarkedSite;
230 boolean mIncognito;
John Reck30c714c2010-12-16 17:30:34 -0800231
232 PageState(Context c, boolean incognito) {
John Reck502a3532011-08-16 14:21:46 -0700233 mIncognito = incognito;
234 if (mIncognito) {
John Reckdb22ec42011-06-29 11:31:24 -0700235 mOriginalUrl = mUrl = "browser:incognito";
John Reck30c714c2010-12-16 17:30:34 -0800236 mTitle = c.getString(R.string.new_incognito_tab);
John Reck30c714c2010-12-16 17:30:34 -0800237 } else {
John Reckdb22ec42011-06-29 11:31:24 -0700238 mOriginalUrl = mUrl = "";
John Reck30c714c2010-12-16 17:30:34 -0800239 mTitle = c.getString(R.string.new_tab);
John Reck30c714c2010-12-16 17:30:34 -0800240 }
Steve Block2466eff2011-10-03 15:33:09 +0100241 mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
John Reck30c714c2010-12-16 17:30:34 -0800242 }
243
244 PageState(Context c, boolean incognito, String url, Bitmap favicon) {
John Reck502a3532011-08-16 14:21:46 -0700245 mIncognito = incognito;
John Reckdb22ec42011-06-29 11:31:24 -0700246 mOriginalUrl = mUrl = url;
John Reck30c714c2010-12-16 17:30:34 -0800247 if (URLUtil.isHttpsUrl(url)) {
Steve Block2466eff2011-10-03 15:33:09 +0100248 mSecurityState = SecurityState.SECURITY_STATE_SECURE;
John Reck30c714c2010-12-16 17:30:34 -0800249 } else {
Steve Block2466eff2011-10-03 15:33:09 +0100250 mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
John Reck30c714c2010-12-16 17:30:34 -0800251 }
John Reck1cf4b792011-07-26 10:22:22 -0700252 mFavicon = favicon;
John Reck30c714c2010-12-16 17:30:34 -0800253 }
John Reck1cf4b792011-07-26 10:22:22 -0700254
Grace Kloba22ac16e2009-10-07 18:00:23 -0700255 }
256
John Reck30c714c2010-12-16 17:30:34 -0800257 // The current/loading page's state
John Reckd8c74522011-06-14 08:45:00 -0700258 protected PageState mCurrentState;
John Reck30c714c2010-12-16 17:30:34 -0800259
Grace Kloba22ac16e2009-10-07 18:00:23 -0700260 // Used for saving and restoring each Tab
Michael Kolbc831b632011-05-11 09:30:34 -0700261 static final String ID = "ID";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700262 static final String CURRURL = "currentUrl";
263 static final String CURRTITLE = "currentTitle";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700264 static final String PARENTTAB = "parentTab";
265 static final String APPID = "appid";
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700266 static final String INCOGNITO = "privateBrowsingEnabled";
John Reckb0a86db2011-05-24 14:05:58 -0700267 static final String USERAGENT = "useragent";
Michael Kolbe28b3472011-08-04 16:54:31 -0700268 static final String CLOSEFLAG = "closeOnBack";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700269
Grace Kloba22ac16e2009-10-07 18:00:23 -0700270 // Container class for the next error dialog that needs to be displayed
271 private class ErrorDialog {
272 public final int mTitle;
273 public final String mDescription;
274 public final int mError;
275 ErrorDialog(int title, String desc, int error) {
276 mTitle = title;
277 mDescription = desc;
278 mError = error;
279 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700280 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700281
282 private void processNextError() {
283 if (mQueuedErrors == null) {
284 return;
285 }
286 // The first one is currently displayed so just remove it.
287 mQueuedErrors.removeFirst();
288 if (mQueuedErrors.size() == 0) {
289 mQueuedErrors = null;
290 return;
291 }
292 showError(mQueuedErrors.getFirst());
293 }
294
295 private DialogInterface.OnDismissListener mDialogListener =
296 new DialogInterface.OnDismissListener() {
297 public void onDismiss(DialogInterface d) {
298 processNextError();
299 }
300 };
301 private LinkedList<ErrorDialog> mQueuedErrors;
302
303 private void queueError(int err, String desc) {
304 if (mQueuedErrors == null) {
305 mQueuedErrors = new LinkedList<ErrorDialog>();
306 }
307 for (ErrorDialog d : mQueuedErrors) {
308 if (d.mError == err) {
309 // Already saw a similar error, ignore the new one.
310 return;
311 }
312 }
313 ErrorDialog errDialog = new ErrorDialog(
314 err == WebViewClient.ERROR_FILE_NOT_FOUND ?
315 R.string.browserFrameFileErrorLabel :
316 R.string.browserFrameNetworkErrorLabel,
317 desc, err);
318 mQueuedErrors.addLast(errDialog);
319
320 // Show the dialog now if the queue was empty and it is in foreground
321 if (mQueuedErrors.size() == 1 && mInForeground) {
322 showError(errDialog);
323 }
324 }
325
326 private void showError(ErrorDialog errDialog) {
327 if (mInForeground) {
Michael Kolb14612442011-06-24 13:06:29 -0700328 AlertDialog d = new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700329 .setTitle(errDialog.mTitle)
330 .setMessage(errDialog.mDescription)
331 .setPositiveButton(R.string.ok, null)
332 .create();
333 d.setOnDismissListener(mDialogListener);
334 d.show();
335 }
336 }
337
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800338 protected void replaceCrashView(View view, View container) {
339 if (hasCrashed && (view == mMainView)) {
340 final FrameLayout wrapper = (FrameLayout) container.findViewById(R.id.webview_wrapper);
341 wrapper.removeAllViewsInLayout();
342 wrapper.addView(view);
343 hasCrashed = false;
344 }
345 }
346
347 protected void showCrashView() {
348 if (hasCrashed) {
349 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
350 Context.LAYOUT_INFLATER_SERVICE);
351 final View crashLayout = inflater.inflate(R.layout.browser_tab_crash, null);
352 final FrameLayout wrapper =
353 (FrameLayout) mContainer.findViewById(R.id.webview_wrapper);
354 wrapper.removeAllViewsInLayout();
355 wrapper.addView(crashLayout);
356 mContainer.requestFocus();
357 Button reloadBtn = (Button) crashLayout.findViewById(R.id.browser_crash_reload_btn);
358 reloadBtn.setOnClickListener(new OnClickListener() {
359 @Override
360 public void onClick(View arg0) {
361 replaceCrashView(mMainView, mContainer);
362 mMainView.reload();
363 }
364 });
365 }
366 }
367
Grace Kloba22ac16e2009-10-07 18:00:23 -0700368 // -------------------------------------------------------------------------
369 // WebViewClient implementation for the main WebView
370 // -------------------------------------------------------------------------
371
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800372 private final WebViewClient mWebViewClient = new WebViewClient() {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500373 private Message mDontResend;
374 private Message mResend;
Michael Kolb47bd1e42011-09-01 15:25:00 -0700375
376 private boolean providersDiffer(String url, String otherUrl) {
377 Uri uri1 = Uri.parse(url);
378 Uri uri2 = Uri.parse(otherUrl);
379 return !uri1.getEncodedAuthority().equals(uri2.getEncodedAuthority());
380 }
381
Grace Kloba22ac16e2009-10-07 18:00:23 -0700382 @Override
383 public void onPageStarted(WebView view, String url, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700384 mInPageLoad = true;
Tarun Nainani8eb00912014-07-17 12:28:32 -0700385 mPageFinished = false;
Michael Kolb72864272012-05-03 15:42:15 -0700386 mUpdateThumbnail = true;
Michael Kolba53c9892011-10-05 13:31:40 -0700387 mPageLoadProgress = INITIAL_PROGRESS;
Michael Kolb14612442011-06-24 13:06:29 -0700388 mCurrentState = new PageState(mContext,
John Reck30c714c2010-12-16 17:30:34 -0800389 view.isPrivateBrowsingEnabled(), url, favicon);
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000390 mLoadStartTime = SystemClock.uptimeMillis();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700391
392 // If we start a touch icon load and then load a new page, we don't
393 // want to cancel the current touch icon loader. But, we do want to
394 // create a new one when the touch icon url is known.
395 if (mTouchIconLoader != null) {
396 mTouchIconLoader.mTab = null;
397 mTouchIconLoader = null;
398 }
399
400 // reset the error console
401 if (mErrorConsole != null) {
402 mErrorConsole.clearErrorMessages();
Michael Kolb8233fac2010-10-26 16:08:53 -0700403 if (mWebViewController.shouldShowErrorConsole()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700404 mErrorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
405 }
406 }
407
Grace Kloba22ac16e2009-10-07 18:00:23 -0700408 // finally update the UI in the activity if it is in the foreground
John Reck324d4402011-01-11 16:56:42 -0800409 mWebViewController.onPageStarted(Tab.this, view, favicon);
Leon Scroggins4cd97792010-12-03 15:31:56 -0500410
John Recke969cc52010-12-21 17:24:43 -0800411 updateBookmarkedStatus();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700412 }
413
414 @Override
415 public void onPageFinished(WebView view, String url) {
John Reck38b39652012-06-05 09:22:59 -0700416 mDisableOverrideUrlLoading = false;
John Reck5b691842010-11-29 11:21:13 -0800417 if (!isPrivateBrowsingEnabled()) {
418 LogTag.logPageFinishedLoading(
419 url, SystemClock.uptimeMillis() - mLoadStartTime);
420 }
John Reck1cf4b792011-07-26 10:22:22 -0700421 syncCurrentState(view, url);
John Reck324d4402011-01-11 16:56:42 -0800422 mWebViewController.onPageFinished(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700423 }
424
425 // return true if want to hijack the url to let another app to handle it
426 @Override
427 public boolean shouldOverrideUrlLoading(WebView view, String url) {
John Reck38b39652012-06-05 09:22:59 -0700428 if (!mDisableOverrideUrlLoading && mInForeground) {
Michael Kolb18eb3772010-12-10 14:29:51 -0800429 return mWebViewController.shouldOverrideUrlLoading(Tab.this,
430 view, url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700431 } else {
432 return false;
433 }
434 }
435
436 /**
Steve Block2466eff2011-10-03 15:33:09 +0100437 * Updates the security state. This method is called when we discover
438 * another resource to be loaded for this page (for example,
439 * javascript). While we update the security state, we do not update
440 * the lock icon until we are done loading, as it is slightly more
441 * secure this way.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700442 */
443 @Override
444 public void onLoadResource(WebView view, String url) {
445 if (url != null && url.length() > 0) {
446 // It is only if the page claims to be secure that we may have
Steve Block2466eff2011-10-03 15:33:09 +0100447 // to update the security state:
448 if (mCurrentState.mSecurityState == SecurityState.SECURITY_STATE_SECURE) {
449 // If NOT a 'safe' url, change the state to mixed content!
Grace Kloba22ac16e2009-10-07 18:00:23 -0700450 if (!(URLUtil.isHttpsUrl(url) || URLUtil.isDataUrl(url)
451 || URLUtil.isAboutUrl(url))) {
Steve Block2466eff2011-10-03 15:33:09 +0100452 mCurrentState.mSecurityState = SecurityState.SECURITY_STATE_MIXED;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700453 }
454 }
455 }
456 }
457
458 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700459 * Show a dialog informing the user of the network error reported by
460 * WebCore if it is in the foreground.
461 */
462 @Override
463 public void onReceivedError(WebView view, int errorCode,
464 String description, String failingUrl) {
465 if (errorCode != WebViewClient.ERROR_HOST_LOOKUP &&
466 errorCode != WebViewClient.ERROR_CONNECT &&
467 errorCode != WebViewClient.ERROR_BAD_URL &&
468 errorCode != WebViewClient.ERROR_UNSUPPORTED_SCHEME &&
469 errorCode != WebViewClient.ERROR_FILE) {
470 queueError(errorCode, description);
Jeff Hamilton47654f42010-09-07 09:57:51 -0500471
Selim Gurun3da06b82011-10-10 13:58:12 -0700472 // Don't log URLs when in private browsing mode
473 if (!isPrivateBrowsingEnabled()) {
474 Log.e(LOGTAG, "onReceivedError " + errorCode + " " + failingUrl
Jeff Hamilton47654f42010-09-07 09:57:51 -0500475 + " " + description);
Selim Gurun3da06b82011-10-10 13:58:12 -0700476 }
Jeff Hamilton47654f42010-09-07 09:57:51 -0500477 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700478 }
479
480 /**
481 * Check with the user if it is ok to resend POST data as the page they
482 * are trying to navigate to is the result of a POST.
483 */
484 @Override
485 public void onFormResubmission(WebView view, final Message dontResend,
486 final Message resend) {
487 if (!mInForeground) {
488 dontResend.sendToTarget();
489 return;
490 }
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500491 if (mDontResend != null) {
492 Log.w(LOGTAG, "onFormResubmission should not be called again "
493 + "while dialog is still up");
494 dontResend.sendToTarget();
495 return;
496 }
497 mDontResend = dontResend;
498 mResend = resend;
Michael Kolb14612442011-06-24 13:06:29 -0700499 new AlertDialog.Builder(mContext).setTitle(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700500 R.string.browserFrameFormResubmitLabel).setMessage(
501 R.string.browserFrameFormResubmitMessage)
502 .setPositiveButton(R.string.ok,
503 new DialogInterface.OnClickListener() {
504 public void onClick(DialogInterface dialog,
505 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500506 if (mResend != null) {
507 mResend.sendToTarget();
508 mResend = null;
509 mDontResend = null;
510 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700511 }
512 }).setNegativeButton(R.string.cancel,
513 new DialogInterface.OnClickListener() {
514 public void onClick(DialogInterface dialog,
515 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500516 if (mDontResend != null) {
517 mDontResend.sendToTarget();
518 mResend = null;
519 mDontResend = null;
520 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700521 }
522 }).setOnCancelListener(new OnCancelListener() {
523 public void onCancel(DialogInterface dialog) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500524 if (mDontResend != null) {
525 mDontResend.sendToTarget();
526 mResend = null;
527 mDontResend = null;
528 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700529 }
530 }).show();
531 }
532
533 /**
534 * Insert the url into the visited history database.
535 * @param url The url to be inserted.
536 * @param isReload True if this url is being reloaded.
537 * FIXME: Not sure what to do when reloading the page.
538 */
539 @Override
540 public void doUpdateVisitedHistory(WebView view, String url,
541 boolean isReload) {
John Reck324d4402011-01-11 16:56:42 -0800542 mWebViewController.doUpdateVisitedHistory(Tab.this, isReload);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700543 }
544
545 /**
546 * Displays SSL error(s) dialog to the user.
547 */
548 @Override
549 public void onReceivedSslError(final WebView view,
550 final SslErrorHandler handler, final SslError error) {
551 if (!mInForeground) {
552 handler.cancel();
Steve Block2466eff2011-10-03 15:33:09 +0100553 setSecurityState(SecurityState.SECURITY_STATE_NOT_SECURE);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700554 return;
555 }
John Reck35e9dd62011-04-25 09:01:54 -0700556 if (mSettings.showSecurityWarnings()) {
John Reckcb28b2c2011-08-26 17:39:44 -0700557 new AlertDialog.Builder(mContext)
558 .setTitle(R.string.security_warning)
559 .setMessage(R.string.ssl_warnings_header)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200560 .setIconAttribute(android.R.attr.alertDialogIcon)
John Reckcb28b2c2011-08-26 17:39:44 -0700561 .setPositiveButton(R.string.ssl_continue,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700562 new DialogInterface.OnClickListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700563 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700564 public void onClick(DialogInterface dialog,
565 int whichButton) {
566 handler.proceed();
Steve Block4895b012011-10-03 16:26:46 +0100567 handleProceededAfterSslError(error);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700568 }
John Reckcb28b2c2011-08-26 17:39:44 -0700569 })
570 .setNeutralButton(R.string.view_certificate,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700571 new DialogInterface.OnClickListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700572 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700573 public void onClick(DialogInterface dialog,
574 int whichButton) {
John Reckcb28b2c2011-08-26 17:39:44 -0700575 mWebViewController.showSslCertificateOnError(
576 view, handler, error);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700577 }
John Reckcb28b2c2011-08-26 17:39:44 -0700578 })
579 .setNegativeButton(R.string.ssl_go_back,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700580 new DialogInterface.OnClickListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700581 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700582 public void onClick(DialogInterface dialog,
583 int whichButton) {
John Reck30c714c2010-12-16 17:30:34 -0800584 dialog.cancel();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700585 }
John Reckcb28b2c2011-08-26 17:39:44 -0700586 })
587 .setOnCancelListener(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700588 new DialogInterface.OnCancelListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700589 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700590 public void onCancel(DialogInterface dialog) {
591 handler.cancel();
Steve Block2466eff2011-10-03 15:33:09 +0100592 setSecurityState(SecurityState.SECURITY_STATE_NOT_SECURE);
John Reck30c714c2010-12-16 17:30:34 -0800593 mWebViewController.onUserCanceledSsl(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700594 }
John Reckcb28b2c2011-08-26 17:39:44 -0700595 })
596 .show();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700597 } else {
598 handler.proceed();
599 }
600 }
601
602 /**
Steve Block4895b012011-10-03 16:26:46 +0100603 * Called when an SSL error occurred while loading a resource, but the
604 * WebView but chose to proceed anyway based on a decision retained
605 * from a previous response to onReceivedSslError(). We update our
606 * security state to reflect this.
607 */
608 @Override
609 public void onProceededAfterSslError(WebView view, SslError error) {
610 handleProceededAfterSslError(error);
611 }
612
613 /**
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700614 * Displays client certificate request to the user.
615 */
616 @Override
617 public void onReceivedClientCertRequest(final WebView view,
618 final ClientCertRequestHandler handler, final String host_and_port) {
619 if (!mInForeground) {
620 handler.ignore();
621 return;
622 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700623 int colon = host_and_port.lastIndexOf(':');
624 String host;
625 int port;
626 if (colon == -1) {
627 host = host_and_port;
628 port = -1;
629 } else {
630 String portString = host_and_port.substring(colon + 1);
631 try {
632 port = Integer.parseInt(portString);
633 host = host_and_port.substring(0, colon);
634 } catch (NumberFormatException e) {
635 host = host_and_port;
636 port = -1;
637 }
638 }
Michael Kolb14612442011-06-24 13:06:29 -0700639 KeyChain.choosePrivateKeyAlias(
640 mWebViewController.getActivity(), new KeyChainAliasCallback() {
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700641 @Override public void alias(String alias) {
642 if (alias == null) {
643 handler.cancel();
644 return;
645 }
Michael Kolb14612442011-06-24 13:06:29 -0700646 new KeyChainLookup(mContext, handler, alias).execute();
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700647 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700648 }, null, null, host, port, null);
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700649 }
650
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800651 @Override
652 public void onRendererCrash(WebView view, boolean crashedWhileOomProtected) {
653 Log.e(LOGTAG, "Tab Crashed");
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700654 if (mWebViewController.getTabControl().getCurrentTab() == Tab.this) {
655 hasCrashed = true;
656 showCrashView();
657 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800658 }
659
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700660 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700661 * Handles an HTTP authentication request.
662 *
663 * @param handler The authentication handler
664 * @param host The host
665 * @param realm The realm
666 */
667 @Override
668 public void onReceivedHttpAuthRequest(WebView view,
669 final HttpAuthHandler handler, final String host,
670 final String realm) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700671 mWebViewController.onReceivedHttpAuthRequest(Tab.this, view, handler, host, realm);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700672 }
673
674 @Override
John Reck438bf462011-01-12 18:11:46 -0800675 public WebResourceResponse shouldInterceptRequest(WebView view,
676 String url) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800677 //intercept if opening a new incognito tab - show the incognito welcome page
678 if (url.startsWith("browser:incognito")) {
679 Resources resourceHandle = mContext.getResources();
680 InputStream inStream = resourceHandle.openRawResource(
Bijan Amirzada41242f22014-03-21 12:12:18 -0700681 com.android.browser.R.raw.incognito_mode_start_page);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800682 return new WebResourceResponse("text/html", "utf8", inStream);
683 }
kaiyiz6e5b3e02013-08-19 20:02:01 +0800684 WebResourceResponse res;
685 if (MyNavigationUtil.MY_NAVIGATION.equals(url)) {
686 res = MyNavigationProvider.shouldInterceptRequest(mContext, url);
687 } else {
688 res = HomeProvider.shouldInterceptRequest(mContext, url);
689 }
John Reck438bf462011-01-12 18:11:46 -0800690 return res;
691 }
692
693 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700694 public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
695 if (!mInForeground) {
696 return false;
697 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700698 return mWebViewController.shouldOverrideKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700699 }
700
701 @Override
702 public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700703 if (!mInForeground) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700704 return;
705 }
John Reck997b1b72012-04-19 18:08:25 -0700706 if (!mWebViewController.onUnhandledKeyEvent(event)) {
707 super.onUnhandledKeyEvent(view, event);
708 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700709 }
710 };
711
John Reck1cf4b792011-07-26 10:22:22 -0700712 private void syncCurrentState(WebView view, String url) {
713 // Sync state (in case of stop/timeout)
714 mCurrentState.mUrl = view.getUrl();
715 if (mCurrentState.mUrl == null) {
John Reck8bcafc12011-08-29 16:43:02 -0700716 mCurrentState.mUrl = "";
John Reck1cf4b792011-07-26 10:22:22 -0700717 }
718 mCurrentState.mOriginalUrl = view.getOriginalUrl();
719 mCurrentState.mTitle = view.getTitle();
720 mCurrentState.mFavicon = view.getFavicon();
721 if (!URLUtil.isHttpsUrl(mCurrentState.mUrl)) {
722 // In case we stop when loading an HTTPS page from an HTTP page
723 // but before a provisional load occurred
Steve Block2466eff2011-10-03 15:33:09 +0100724 mCurrentState.mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
Steve Block08a6f0c2011-10-06 12:12:53 +0100725 mCurrentState.mSslCertificateError = null;
John Reck1cf4b792011-07-26 10:22:22 -0700726 }
John Reck502a3532011-08-16 14:21:46 -0700727 mCurrentState.mIncognito = view.isPrivateBrowsingEnabled();
John Reck1cf4b792011-07-26 10:22:22 -0700728 }
729
Tarun Nainani8eb00912014-07-17 12:28:32 -0700730
731 public boolean isTabFullScreen() {
732 return mFullScreen;
733 }
734
Vivek Sekharf96064b2014-07-28 16:32:34 -0700735 protected void setTabFullscreen(boolean fullScreen) {
Tarun Nainani8eb00912014-07-17 12:28:32 -0700736 Controller controller = (Controller)mWebViewController;
737
738 if (!mSettings.useFullscreen())
739 controller.getUi().setFullscreen(fullScreen);
740
741 if (getWebView() != null) {
742 if (fullScreen)
743 getWebView().updateTopControls(true, false, true);
744 else if (!mSettings.useQuickControls())
745 getWebView().updateTopControls(true, true, true);
Vivek Sekharf96064b2014-07-28 16:32:34 -0700746 }
Tarun Nainani8eb00912014-07-17 12:28:32 -0700747
748 mFullScreen = fullScreen;
Vivek Sekharf96064b2014-07-28 16:32:34 -0700749 }
750
Grace Kloba22ac16e2009-10-07 18:00:23 -0700751 // -------------------------------------------------------------------------
752 // WebChromeClient implementation for the main WebView
753 // -------------------------------------------------------------------------
754
755 private final WebChromeClient mWebChromeClient = new WebChromeClient() {
756 // Helper method to create a new tab or sub window.
757 private void createWindow(final boolean dialog, final Message msg) {
758 WebView.WebViewTransport transport =
759 (WebView.WebViewTransport) msg.obj;
760 if (dialog) {
761 createSubWindow();
Michael Kolb8233fac2010-10-26 16:08:53 -0700762 mWebViewController.attachSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700763 transport.setWebView(mSubView);
764 } else {
Michael Kolb7bcafde2011-05-09 13:55:59 -0700765 final Tab newTab = mWebViewController.openTab(null,
John Reck5949c662011-05-27 09:52:29 -0700766 Tab.this, true, true);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700767 transport.setWebView(newTab.getWebView());
768 }
769 msg.sendToTarget();
770 }
771
772 @Override
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700773 public void toggleFullscreenModeForTab(boolean enterFullscreen) {
774 if (mWebViewController instanceof Controller) {
Vivek Sekharf96064b2014-07-28 16:32:34 -0700775 setTabFullscreen(enterFullscreen);
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700776 }
777 }
778
779 @Override
Tarun Nainani8eb00912014-07-17 12:28:32 -0700780 public void onOffsetsForFullscreenChanged(float topControlsOffsetYPix,
781 float contentOffsetYPix,
782 float overdrawBottomHeightPix) {
783 if (mWebViewController instanceof Controller) {
Vivek Sekhar8ee3abb2014-07-14 12:32:05 -0700784 Controller controller = (Controller)mWebViewController;
Tarun Nainani8eb00912014-07-17 12:28:32 -0700785 controller.getUi().translateTitleBar(topControlsOffsetYPix);
Vivek Sekhar8ee3abb2014-07-14 12:32:05 -0700786 }
787 }
788
789 @Override
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700790 public boolean isTabFullScreen() {
Vivek Sekharf96064b2014-07-28 16:32:34 -0700791 return mFullScreen;
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700792 }
793
794 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700795 public boolean onCreateWindow(WebView view, final boolean dialog,
796 final boolean userGesture, final Message resultMsg) {
797 // only allow new window or sub window for the foreground case
798 if (!mInForeground) {
799 return false;
800 }
801 // Short-circuit if we can't create any more tabs or sub windows.
802 if (dialog && mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -0700803 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700804 .setTitle(R.string.too_many_subwindows_dialog_title)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200805 .setIconAttribute(android.R.attr.alertDialogIcon)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700806 .setMessage(R.string.too_many_subwindows_dialog_message)
807 .setPositiveButton(R.string.ok, null)
808 .show();
809 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700810 } else if (!mWebViewController.getTabControl().canCreateNewTab()) {
Michael Kolb14612442011-06-24 13:06:29 -0700811 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700812 .setTitle(R.string.too_many_windows_dialog_title)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200813 .setIconAttribute(android.R.attr.alertDialogIcon)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700814 .setMessage(R.string.too_many_windows_dialog_message)
815 .setPositiveButton(R.string.ok, null)
816 .show();
817 return false;
818 }
819
820 // Short-circuit if this was a user gesture.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800821 if (userGesture || !mSettings.blockPopupWindows()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700822 createWindow(dialog, resultMsg);
823 return true;
824 }
825
826 // Allow the popup and create the appropriate window.
827 final AlertDialog.OnClickListener allowListener =
828 new AlertDialog.OnClickListener() {
829 public void onClick(DialogInterface d,
830 int which) {
831 createWindow(dialog, resultMsg);
832 }
833 };
834
835 // Block the popup by returning a null WebView.
836 final AlertDialog.OnClickListener blockListener =
837 new AlertDialog.OnClickListener() {
838 public void onClick(DialogInterface d, int which) {
839 resultMsg.sendToTarget();
840 }
841 };
842
843 // Build a confirmation dialog to display to the user.
844 final AlertDialog d =
Michael Kolb14612442011-06-24 13:06:29 -0700845 new AlertDialog.Builder(mContext)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200846 .setIconAttribute(android.R.attr.alertDialogIcon)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700847 .setMessage(R.string.popup_window_attempt)
848 .setPositiveButton(R.string.allow, allowListener)
849 .setNegativeButton(R.string.block, blockListener)
850 .setCancelable(false)
851 .create();
852
853 // Show the confirmation dialog.
854 d.show();
855 return true;
856 }
857
858 @Override
Patrick Scotteb5061b2009-11-18 15:00:30 -0500859 public void onRequestFocus(WebView view) {
860 if (!mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700861 mWebViewController.switchToTab(Tab.this);
Patrick Scotteb5061b2009-11-18 15:00:30 -0500862 }
863 }
864
865 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700866 public void onCloseWindow(WebView window) {
Michael Kolbc831b632011-05-11 09:30:34 -0700867 if (mParent != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700868 // JavaScript can only close popup window.
869 if (mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700870 mWebViewController.switchToTab(mParent);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700871 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700872 mWebViewController.closeTab(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700873 }
874 }
875
876 @Override
877 public void onProgressChanged(WebView view, int newProgress) {
John Reck30c714c2010-12-16 17:30:34 -0800878 mPageLoadProgress = newProgress;
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800879 if (newProgress == 100) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800880 Log.i(CONSOLE_LOGTAG, "SWE Pageload Progress = 100");
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800881 mInPageLoad = false;
882 }
John Reck30c714c2010-12-16 17:30:34 -0800883 mWebViewController.onProgressChanged(Tab.this);
Michael Kolb72864272012-05-03 15:42:15 -0700884 if (mUpdateThumbnail && newProgress == 100) {
885 mUpdateThumbnail = false;
886 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700887 }
888
889 @Override
Leon Scroggins21d9b902010-03-11 09:33:11 -0500890 public void onReceivedTitle(WebView view, final String title) {
John Reck30c714c2010-12-16 17:30:34 -0800891 mCurrentState.mTitle = title;
Michael Kolb8233fac2010-10-26 16:08:53 -0700892 mWebViewController.onReceivedTitle(Tab.this, title);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700893 }
894
895 @Override
896 public void onReceivedIcon(WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -0800897 mCurrentState.mFavicon = icon;
Michael Kolb8233fac2010-10-26 16:08:53 -0700898 mWebViewController.onFavicon(Tab.this, view, icon);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700899 }
900
901 @Override
902 public void onReceivedTouchIconUrl(WebView view, String url,
903 boolean precomposed) {
Michael Kolb14612442011-06-24 13:06:29 -0700904 final ContentResolver cr = mContext.getContentResolver();
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400905 // Let precomposed icons take precedence over non-composed
906 // icons.
907 if (precomposed && mTouchIconLoader != null) {
908 mTouchIconLoader.cancel(false);
909 mTouchIconLoader = null;
910 }
911 // Have only one async task at a time.
912 if (mTouchIconLoader == null) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700913 mTouchIconLoader = new DownloadTouchIcon(Tab.this,
Michael Kolb14612442011-06-24 13:06:29 -0700914 mContext, cr, view);
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400915 mTouchIconLoader.execute(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700916 }
917 }
918
919 @Override
920 public void onShowCustomView(View view,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800921 CustomViewCallback callback) {
Michael Kolb14612442011-06-24 13:06:29 -0700922 Activity activity = mWebViewController.getActivity();
923 if (activity != null) {
924 onShowCustomView(view, activity.getRequestedOrientation(), callback);
925 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400926 }
927
928 @Override
929 public void onShowCustomView(View view, int requestedOrientation,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800930 CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700931 if (mInForeground) mWebViewController.showCustomView(Tab.this, view,
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400932 requestedOrientation, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700933 }
934
935 @Override
936 public void onHideCustomView() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700937 if (mInForeground) mWebViewController.hideCustomView();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700938 }
939
940 /**
941 * The origin has exceeded its database quota.
942 * @param url the URL that exceeded the quota
943 * @param databaseIdentifier the identifier of the database on which the
944 * transaction that caused the quota overflow was run
945 * @param currentQuota the current quota for the origin.
946 * @param estimatedSize the estimated size of the database.
947 * @param totalUsedQuota is the sum of all origins' quota.
948 * @param quotaUpdater The callback to run when a decision to allow or
949 * deny quota has been made. Don't forget to call this!
950 */
951 @Override
952 public void onExceededDatabaseQuota(String url,
953 String databaseIdentifier, long currentQuota, long estimatedSize,
954 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -0700955 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -0700956 .onExceededDatabaseQuota(url, databaseIdentifier,
957 currentQuota, estimatedSize, totalUsedQuota,
958 quotaUpdater);
959 }
960
961 /**
962 * The Application Cache has exceeded its max size.
963 * @param spaceNeeded is the amount of disk space that would be needed
964 * in order for the last appcache operation to succeed.
965 * @param totalUsedQuota is the sum of all origins' quota.
966 * @param quotaUpdater A callback to inform the WebCore thread that a
967 * new app cache size is available. This callback must always
968 * be executed at some point to ensure that the sleeping
969 * WebCore thread is woken up.
970 */
971 @Override
972 public void onReachedMaxAppCacheSize(long spaceNeeded,
973 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -0700974 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -0700975 .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota,
976 quotaUpdater);
977 }
978
979 /**
980 * Instructs the browser to show a prompt to ask the user to set the
981 * Geolocation permission state for the specified origin.
982 * @param origin The origin for which Geolocation permissions are
983 * requested.
984 * @param callback The callback to call once the user has set the
985 * Geolocation permission state.
986 */
987 @Override
988 public void onGeolocationPermissionsShowPrompt(String origin,
989 GeolocationPermissions.Callback callback) {
990 if (mInForeground) {
Grace Kloba50c241e2010-04-20 11:07:50 -0700991 getGeolocationPermissionsPrompt().show(origin, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700992 }
993 }
994
995 /**
996 * Instructs the browser to hide the Geolocation permissions prompt.
997 */
998 @Override
999 public void onGeolocationPermissionsHidePrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001000 if (mInForeground && mGeolocationPermissionsPrompt != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001001 mGeolocationPermissionsPrompt.hide();
1002 }
1003 }
1004
Ben Murdoch65acc352009-11-19 18:16:04 +00001005 /* Adds a JavaScript error message to the system log and if the JS
1006 * console is enabled in the about:debug options, to that console
1007 * also.
Ben Murdochc42addf2010-01-28 15:19:59 +00001008 * @param consoleMessage the message object.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001009 */
1010 @Override
Ben Murdochc42addf2010-01-28 15:19:59 +00001011 public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001012 if (mInForeground) {
1013 // call getErrorConsole(true) so it will create one if needed
1014 ErrorConsoleView errorConsole = getErrorConsole(true);
Ben Murdochc42addf2010-01-28 15:19:59 +00001015 errorConsole.addErrorMessage(consoleMessage);
Michael Kolb8233fac2010-10-26 16:08:53 -07001016 if (mWebViewController.shouldShowErrorConsole()
1017 && errorConsole.getShowState() !=
1018 ErrorConsoleView.SHOW_MAXIMIZED) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001019 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
1020 }
1021 }
Ben Murdochc42addf2010-01-28 15:19:59 +00001022
Jeff Hamilton47654f42010-09-07 09:57:51 -05001023 // Don't log console messages in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001024 if (isPrivateBrowsingEnabled()) return true;
Jeff Hamilton47654f42010-09-07 09:57:51 -05001025
Ben Murdochc42addf2010-01-28 15:19:59 +00001026 String message = "Console: " + consoleMessage.message() + " "
1027 + consoleMessage.sourceId() + ":"
1028 + consoleMessage.lineNumber();
1029
1030 switch (consoleMessage.messageLevel()) {
1031 case TIP:
1032 Log.v(CONSOLE_LOGTAG, message);
1033 break;
1034 case LOG:
1035 Log.i(CONSOLE_LOGTAG, message);
1036 break;
1037 case WARNING:
1038 Log.w(CONSOLE_LOGTAG, message);
1039 break;
1040 case ERROR:
1041 Log.e(CONSOLE_LOGTAG, message);
1042 break;
1043 case DEBUG:
1044 Log.d(CONSOLE_LOGTAG, message);
1045 break;
1046 }
1047
1048 return true;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001049 }
1050
1051 /**
1052 * Ask the browser for an icon to represent a <video> element.
1053 * This icon will be used if the Web page did not specify a poster attribute.
1054 * @return Bitmap The icon or null if no such icon is available.
1055 */
1056 @Override
1057 public Bitmap getDefaultVideoPoster() {
1058 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001059 return mWebViewController.getDefaultVideoPoster();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001060 }
1061 return null;
1062 }
1063
1064 /**
1065 * Ask the host application for a custom progress view to show while
1066 * a <video> is loading.
1067 * @return View The progress view.
1068 */
1069 @Override
1070 public View getVideoLoadingProgressView() {
1071 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001072 return mWebViewController.getVideoLoadingProgressView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001073 }
1074 return null;
1075 }
1076
1077 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00001078 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001079 if (mInForeground) {
Ben Murdoch8cad4132012-01-11 10:56:43 +00001080 mWebViewController.openFileChooser(uploadMsg, acceptType, capture);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001081 } else {
1082 uploadMsg.onReceiveValue(null);
1083 }
1084 }
1085
Vivek Sekharb54614f2014-05-01 19:03:37 -07001086 @Override
1087 public void showFileChooser(ValueCallback<String[]> uploadFilePaths, String acceptTypes,
1088 boolean capture) {
1089 if (mInForeground) {
1090 mWebViewController.showFileChooser(uploadFilePaths, acceptTypes, capture);
1091 } else {
1092 uploadFilePaths.onReceiveValue(null);
1093 }
1094 }
1095
Grace Kloba22ac16e2009-10-07 18:00:23 -07001096 /**
1097 * Deliver a list of already-visited URLs
1098 */
1099 @Override
1100 public void getVisitedHistory(final ValueCallback<String[]> callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001101 mWebViewController.getVisitedHistory(callback);
1102 }
Ben Murdoch8029a772010-11-16 11:58:21 +00001103
1104 @Override
1105 public void setupAutoFill(Message message) {
1106 // Prompt the user to set up their profile.
1107 final Message msg = message;
Michael Kolb14612442011-06-24 13:06:29 -07001108 AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
1109 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
Ben Murdoch1d676b62011-01-17 12:54:24 +00001110 Context.LAYOUT_INFLATER_SERVICE);
1111 final View layout = inflater.inflate(R.layout.setup_autofill_dialog, null);
1112
1113 builder.setView(layout)
1114 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1115 @Override
1116 public void onClick(DialogInterface dialog, int id) {
1117 CheckBox disableAutoFill = (CheckBox) layout.findViewById(
1118 R.id.setup_autofill_dialog_disable_autofill);
1119
1120 if (disableAutoFill.isChecked()) {
1121 // Disable autofill and show a toast with how to turn it on again.
John Reck35e9dd62011-04-25 09:01:54 -07001122 mSettings.setAutofillEnabled(false);
Michael Kolb14612442011-06-24 13:06:29 -07001123 Toast.makeText(mContext,
Ben Murdoch1d676b62011-01-17 12:54:24 +00001124 R.string.autofill_setup_dialog_negative_toast,
1125 Toast.LENGTH_LONG).show();
1126 } else {
1127 // Take user to the AutoFill profile editor. When they return,
1128 // we will send the message that we pass here which will trigger
1129 // the form to get filled out with their new profile.
1130 mWebViewController.setupAutoFill(msg);
1131 }
1132 }
1133 })
1134 .setNegativeButton(R.string.cancel, null)
1135 .show();
Ben Murdoch8029a772010-11-16 11:58:21 +00001136 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001137 };
1138
1139 // -------------------------------------------------------------------------
1140 // WebViewClient implementation for the sub window
1141 // -------------------------------------------------------------------------
1142
1143 // Subclass of WebViewClient used in subwindows to notify the main
1144 // WebViewClient of certain WebView activities.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001145 private static class SubWindowClient extends WebViewClient {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001146 // The main WebViewClient.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001147 private final WebViewClient mClient;
Michael Kolb8233fac2010-10-26 16:08:53 -07001148 private final WebViewController mController;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001149
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001150 SubWindowClient(WebViewClient client, WebViewController controller) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001151 mClient = client;
Michael Kolb8233fac2010-10-26 16:08:53 -07001152 mController = controller;
Leon Scroggins III211ba542010-04-19 13:21:13 -04001153 }
1154 @Override
1155 public void onPageStarted(WebView view, String url, Bitmap favicon) {
1156 // Unlike the others, do not call mClient's version, which would
1157 // change the progress bar. However, we do want to remove the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001158 // find or select dialog.
Michael Kolb8233fac2010-10-26 16:08:53 -07001159 mController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001160 }
1161 @Override
1162 public void doUpdateVisitedHistory(WebView view, String url,
1163 boolean isReload) {
1164 mClient.doUpdateVisitedHistory(view, url, isReload);
1165 }
1166 @Override
1167 public boolean shouldOverrideUrlLoading(WebView view, String url) {
1168 return mClient.shouldOverrideUrlLoading(view, url);
1169 }
1170 @Override
1171 public void onReceivedSslError(WebView view, SslErrorHandler handler,
1172 SslError error) {
1173 mClient.onReceivedSslError(view, handler, error);
1174 }
1175 @Override
Brian Carlstrom8862c1d2011-06-02 01:05:55 -07001176 public void onReceivedClientCertRequest(WebView view,
1177 ClientCertRequestHandler handler, String host_and_port) {
1178 mClient.onReceivedClientCertRequest(view, handler, host_and_port);
1179 }
1180 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -07001181 public void onReceivedHttpAuthRequest(WebView view,
1182 HttpAuthHandler handler, String host, String realm) {
1183 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
1184 }
1185 @Override
1186 public void onFormResubmission(WebView view, Message dontResend,
1187 Message resend) {
1188 mClient.onFormResubmission(view, dontResend, resend);
1189 }
1190 @Override
1191 public void onReceivedError(WebView view, int errorCode,
1192 String description, String failingUrl) {
1193 mClient.onReceivedError(view, errorCode, description, failingUrl);
1194 }
1195 @Override
1196 public boolean shouldOverrideKeyEvent(WebView view,
1197 android.view.KeyEvent event) {
1198 return mClient.shouldOverrideKeyEvent(view, event);
1199 }
1200 @Override
1201 public void onUnhandledKeyEvent(WebView view,
1202 android.view.KeyEvent event) {
1203 mClient.onUnhandledKeyEvent(view, event);
1204 }
1205 }
1206
1207 // -------------------------------------------------------------------------
1208 // WebChromeClient implementation for the sub window
1209 // -------------------------------------------------------------------------
1210
1211 private class SubWindowChromeClient extends WebChromeClient {
1212 // The main WebChromeClient.
1213 private final WebChromeClient mClient;
1214
1215 SubWindowChromeClient(WebChromeClient client) {
1216 mClient = client;
1217 }
1218 @Override
1219 public void onProgressChanged(WebView view, int newProgress) {
1220 mClient.onProgressChanged(view, newProgress);
1221 }
1222 @Override
1223 public boolean onCreateWindow(WebView view, boolean dialog,
1224 boolean userGesture, android.os.Message resultMsg) {
1225 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
1226 }
1227 @Override
1228 public void onCloseWindow(WebView window) {
1229 if (window != mSubView) {
1230 Log.e(LOGTAG, "Can't close the window");
1231 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001232 mWebViewController.dismissSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001233 }
1234 }
1235
1236 // -------------------------------------------------------------------------
1237
1238 // Construct a new tab
Michael Kolb7bcafde2011-05-09 13:55:59 -07001239 Tab(WebViewController wvcontroller, WebView w) {
John Reck1cf4b792011-07-26 10:22:22 -07001240 this(wvcontroller, w, null);
1241 }
1242
1243 Tab(WebViewController wvcontroller, Bundle state) {
1244 this(wvcontroller, null, state);
1245 }
1246
1247 Tab(WebViewController wvcontroller, WebView w, Bundle state) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001248 mWebViewController = wvcontroller;
Michael Kolb14612442011-06-24 13:06:29 -07001249 mContext = mWebViewController.getContext();
John Reck35e9dd62011-04-25 09:01:54 -07001250 mSettings = BrowserSettings.getInstance();
Michael Kolb14612442011-06-24 13:06:29 -07001251 mDataController = DataController.getInstance(mContext);
1252 mCurrentState = new PageState(mContext, w != null
John Reck847b5322011-04-14 17:02:18 -07001253 ? w.isPrivateBrowsingEnabled() : false);
Tarun Nainani8084c822014-06-25 13:38:06 -07001254 setTimeStamp();
Michael Kolb8233fac2010-10-26 16:08:53 -07001255 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001256 mInForeground = false;
1257
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001258 mDownloadListener = new BrowserDownloadListener() {
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001259 public void onDownloadStart(String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001260 String contentDisposition, String mimetype, String referer,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001261 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001262 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001263 mimetype, referer, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001264 }
1265 };
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001266 mWebBackForwardListClient = new WebBackForwardListClient() {
1267 @Override
1268 public void onNewHistoryItem(WebHistoryItem item) {
Mathew Inwooda829d552011-09-02 14:16:25 +01001269 if (mClearHistoryUrlPattern != null) {
1270 boolean match =
1271 mClearHistoryUrlPattern.matcher(item.getOriginalUrl()).matches();
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001272 if (LOGD_ENABLED) {
Mathew Inwooda829d552011-09-02 14:16:25 +01001273 Log.d(LOGTAG, "onNewHistoryItem: match=" + match + "\n\t"
1274 + item.getUrl() + "\n\t"
1275 + mClearHistoryUrlPattern);
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001276 }
Mathew Inwooda829d552011-09-02 14:16:25 +01001277 if (match) {
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001278 if (mMainView != null) {
1279 mMainView.clearHistory();
1280 }
1281 }
Mathew Inwooda829d552011-09-02 14:16:25 +01001282 mClearHistoryUrlPattern = null;
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001283 }
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001284 }
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001285 };
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001286
John Reck1cf4b792011-07-26 10:22:22 -07001287 mCaptureWidth = mContext.getResources().getDimensionPixelSize(
1288 R.dimen.tab_thumbnail_width);
1289 mCaptureHeight = mContext.getResources().getDimensionPixelSize(
1290 R.dimen.tab_thumbnail_height);
1291 updateShouldCaptureThumbnails();
1292 restoreState(state);
John Reck52be4782011-08-26 15:37:29 -07001293 if (getId() == -1) {
1294 mId = TabControl.getNextId();
1295 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001296 setWebView(w);
Michael Kolb9ef259a2011-07-12 15:33:08 -07001297 mHandler = new Handler() {
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001298 @Override
Michael Kolb9ef259a2011-07-12 15:33:08 -07001299 public void handleMessage(Message m) {
John Reck1cf4b792011-07-26 10:22:22 -07001300 switch (m.what) {
1301 case MSG_CAPTURE:
1302 capture();
1303 break;
1304 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001305 }
1306 };
John Reck1cf4b792011-07-26 10:22:22 -07001307 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001308
Michael Kolb72864272012-05-03 15:42:15 -07001309 public boolean shouldUpdateThumbnail() {
1310 return mUpdateThumbnail;
1311 }
1312
Mathew Inwoode09305e2011-09-02 12:03:26 +01001313 /**
1314 * This is used to get a new ID when the tab has been preloaded, before it is displayed and
1315 * added to TabControl. Preloaded tabs can be created before restoreInstanceState, leading
1316 * to overlapping IDs between the preloaded and restored tabs.
1317 */
1318 public void refreshIdAfterPreload() {
1319 mId = TabControl.getNextId();
1320 }
1321
John Reck1cf4b792011-07-26 10:22:22 -07001322 public void updateShouldCaptureThumbnails() {
1323 if (mWebViewController.shouldCaptureThumbnails()) {
1324 synchronized (Tab.this) {
1325 if (mCapture == null) {
1326 mCapture = Bitmap.createBitmap(mCaptureWidth, mCaptureHeight,
1327 Bitmap.Config.RGB_565);
Michael Kolbc3af0672011-08-09 10:24:41 -07001328 mCapture.eraseColor(Color.WHITE);
John Reck1cf4b792011-07-26 10:22:22 -07001329 if (mInForeground) {
1330 postCapture();
1331 }
1332 }
1333 }
1334 } else {
1335 synchronized (Tab.this) {
1336 mCapture = null;
1337 deleteThumbnail();
1338 }
1339 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001340 }
1341
Michael Kolb14612442011-06-24 13:06:29 -07001342 public void setController(WebViewController ctl) {
1343 mWebViewController = ctl;
John Reck1cf4b792011-07-26 10:22:22 -07001344 updateShouldCaptureThumbnails();
Michael Kolb14612442011-06-24 13:06:29 -07001345 }
1346
Michael Kolbc831b632011-05-11 09:30:34 -07001347 public long getId() {
1348 return mId;
1349 }
1350
Michael Kolb91911a22012-01-17 11:21:25 -08001351 void setWebView(WebView w) {
1352 setWebView(w, true);
1353 }
1354
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001355 public boolean isNativeActive(){
1356 if (mMainView == null)
1357 return false;
1358 return true;
1359 }
1360
1361 public void setTimeStamp(){
1362 Date d = new Date();
1363 timestamp = (new Timestamp(d.getTime()));
1364 }
1365
1366 public Timestamp getTimestamp() {
1367 return timestamp;
1368 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001369 /**
1370 * Sets the WebView for this tab, correctly removing the old WebView from
1371 * the container view.
1372 */
Michael Kolb91911a22012-01-17 11:21:25 -08001373 void setWebView(WebView w, boolean restore) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001374 if (mMainView == w) {
1375 return;
1376 }
Michael Kolba713ec82010-11-29 17:27:06 -08001377
Grace Kloba22ac16e2009-10-07 18:00:23 -07001378 // If the WebView is changing, the page will be reloaded, so any ongoing
1379 // Geolocation permission requests are void.
Grace Kloba50c241e2010-04-20 11:07:50 -07001380 if (mGeolocationPermissionsPrompt != null) {
1381 mGeolocationPermissionsPrompt.hide();
1382 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001383
Michael Kolba713ec82010-11-29 17:27:06 -08001384 mWebViewController.onSetWebView(this, w);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001385
John Reck1cf4b792011-07-26 10:22:22 -07001386 if (mMainView != null) {
John Reck8ee633f2011-08-09 16:00:35 -07001387 mMainView.setPictureListener(null);
John Reck1cf4b792011-07-26 10:22:22 -07001388 if (w != null) {
1389 syncCurrentState(w, null);
1390 } else {
Panos Thomasa9a5a582014-03-18 19:20:08 -07001391 mCurrentState = new PageState(mContext, mMainView.isPrivateBrowsingEnabled());
John Reck1cf4b792011-07-26 10:22:22 -07001392 }
1393 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001394 // set the new one
1395 mMainView = w;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001396 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001397 if (mMainView != null) {
1398 mMainView.setWebViewClient(mWebViewClient);
1399 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001400 // Attach DownloadManager so that downloads can start in an active
1401 // or a non-active window. This can happen when going to a site that
1402 // does a redirect after a period of time. The user could have
1403 // switched to another tab while waiting for the download to start.
1404 mMainView.setDownloadListener(mDownloadListener);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001405 getWebView().setWebBackForwardListClient(mWebBackForwardListClient);
John Reck8ee633f2011-08-09 16:00:35 -07001406 TabControl tc = mWebViewController.getTabControl();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001407 if (tc != null /*&& tc.getOnThumbnailUpdatedListener() != null*/) {
John Reck8ee633f2011-08-09 16:00:35 -07001408 mMainView.setPictureListener(this);
1409 }
Michael Kolb91911a22012-01-17 11:21:25 -08001410 if (restore && (mSavedState != null)) {
John Reck8b9bb8b2012-03-08 13:19:40 -08001411 restoreUserAgent();
John Reck6c2e2f32011-08-22 13:41:23 -07001412 WebBackForwardList restoredState
1413 = mMainView.restoreState(mSavedState);
1414 if (restoredState == null || restoredState.getSize() == 0) {
1415 Log.w(LOGTAG, "Failed to restore WebView state!");
1416 loadUrl(mCurrentState.mOriginalUrl, null);
1417 }
John Reck1cf4b792011-07-26 10:22:22 -07001418 mSavedState = null;
1419 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001420 }
1421 }
1422
1423 /**
1424 * Destroy the tab's main WebView and subWindow if any
1425 */
1426 void destroy() {
Tarun Nainani2c1dd7c2014-07-05 16:40:12 -07001427
Matthew Huib7f2e9c2014-04-16 11:12:37 -04001428 if (mPostponeDestroy) {
1429 mShouldDestroy = true;
1430 return;
1431 }
1432 mShouldDestroy = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001433 if (mMainView != null) {
1434 dismissSubWindow();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001435 // save the WebView to call destroy() after detach it from the tab
1436 WebView webView = mMainView;
1437 setWebView(null);
1438 webView.destroy();
1439 }
1440 }
1441
Matthew Huib7f2e9c2014-04-16 11:12:37 -04001442 private boolean mPostponeDestroy = false;
1443 private boolean mShouldDestroy = false;
1444
1445 public void postponeDestroy() {
1446 mPostponeDestroy = true;
1447 }
1448
1449 public void performPostponedDestroy() {
1450 mPostponeDestroy = false;
1451 if (mShouldDestroy) {
1452 destroy();
1453 }
1454 }
1455
Grace Kloba22ac16e2009-10-07 18:00:23 -07001456 /**
1457 * Remove the tab from the parent
1458 */
1459 void removeFromTree() {
1460 // detach the children
Michael Kolbc831b632011-05-11 09:30:34 -07001461 if (mChildren != null) {
1462 for(Tab t : mChildren) {
1463 t.setParent(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001464 }
1465 }
1466 // remove itself from the parent list
Michael Kolbc831b632011-05-11 09:30:34 -07001467 if (mParent != null) {
1468 mParent.mChildren.remove(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001469 }
Tarun Nainani8eb00912014-07-17 12:28:32 -07001470
1471 mCapture = null;
John Reck1cf4b792011-07-26 10:22:22 -07001472 deleteThumbnail();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001473 }
1474
1475 /**
1476 * Create a new subwindow unless a subwindow already exists.
1477 * @return True if a new subwindow was created. False if one already exists.
1478 */
1479 boolean createSubWindow() {
1480 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001481 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001482 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001483 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001484 mSubView.setWebChromeClient(new SubWindowChromeClient(
1485 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001486 // Set a different DownloadListener for the mSubView, since it will
1487 // just need to dismiss the mSubView, rather than close the Tab
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001488 mSubView.setDownloadListener(new BrowserDownloadListener() {
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001489 public void onDownloadStart(String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001490 String contentDisposition, String mimetype, String referer,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001491 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001492 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001493 contentDisposition, mimetype, referer, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001494 if (mSubView.copyBackForwardList().getSize() == 0) {
1495 // This subwindow was opened for the sole purpose of
1496 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001497 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001498 }
1499 }
1500 });
Michael Kolb14612442011-06-24 13:06:29 -07001501 mSubView.setOnCreateContextMenuListener(mWebViewController.getActivity());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001502 return true;
1503 }
1504 return false;
1505 }
1506
1507 /**
1508 * Dismiss the subWindow for the tab.
1509 */
1510 void dismissSubWindow() {
1511 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001512 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001513 mSubView.destroy();
1514 mSubView = null;
1515 mSubViewContainer = null;
1516 }
1517 }
1518
Grace Kloba22ac16e2009-10-07 18:00:23 -07001519
1520 /**
1521 * Set the parent tab of this tab.
1522 */
Michael Kolbc831b632011-05-11 09:30:34 -07001523 void setParent(Tab parent) {
John Reck52be4782011-08-26 15:37:29 -07001524 if (parent == this) {
1525 throw new IllegalStateException("Cannot set parent to self!");
1526 }
Michael Kolbc831b632011-05-11 09:30:34 -07001527 mParent = parent;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001528 // This tab may have been freed due to low memory. If that is the case,
Michael Kolbc831b632011-05-11 09:30:34 -07001529 // the parent tab id is already saved. If we are changing that id
Grace Kloba22ac16e2009-10-07 18:00:23 -07001530 // (most likely due to removing the parent tab) we must update the
Michael Kolbc831b632011-05-11 09:30:34 -07001531 // parent tab id in the saved Bundle.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001532 if (mSavedState != null) {
1533 if (parent == null) {
1534 mSavedState.remove(PARENTTAB);
1535 } else {
Michael Kolbc831b632011-05-11 09:30:34 -07001536 mSavedState.putLong(PARENTTAB, parent.getId());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001537 }
1538 }
John Reckb0a86db2011-05-24 14:05:58 -07001539
1540 // Sync the WebView useragent with the parent
1541 if (parent != null && mSettings.hasDesktopUseragent(parent.getWebView())
1542 != mSettings.hasDesktopUseragent(getWebView())) {
1543 mSettings.toggleDesktopUseragent(getWebView());
1544 }
John Reck52be4782011-08-26 15:37:29 -07001545
1546 if (parent != null && parent.getId() == getId()) {
1547 throw new IllegalStateException("Parent has same ID as child!");
1548 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001549 }
1550
1551 /**
Michael Kolbc831b632011-05-11 09:30:34 -07001552 * If this Tab was created through another Tab, then this method returns
1553 * that Tab.
1554 * @return the Tab parent or null
1555 */
1556 public Tab getParent() {
1557 return mParent;
1558 }
1559
1560 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001561 * When a Tab is created through the content of another Tab, then we
1562 * associate the Tabs.
1563 * @param child the Tab that was created from this Tab
1564 */
1565 void addChildTab(Tab child) {
Michael Kolbc831b632011-05-11 09:30:34 -07001566 if (mChildren == null) {
1567 mChildren = new Vector<Tab>();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001568 }
Michael Kolbc831b632011-05-11 09:30:34 -07001569 mChildren.add(child);
1570 child.setParent(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001571 }
1572
Michael Kolbc831b632011-05-11 09:30:34 -07001573 Vector<Tab> getChildren() {
1574 return mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001575 }
1576
1577 void resume() {
1578 if (mMainView != null) {
John Reck56c1fcf2011-08-17 10:15:16 -07001579 setupHwAcceleration(mMainView);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001580 mMainView.onResume();
1581 if (mSubView != null) {
1582 mSubView.onResume();
1583 }
1584 }
1585 }
1586
John Reck56c1fcf2011-08-17 10:15:16 -07001587 private void setupHwAcceleration(View web) {
1588 if (web == null) return;
1589 BrowserSettings settings = BrowserSettings.getInstance();
1590 if (settings.isHardwareAccelerated()) {
1591 web.setLayerType(View.LAYER_TYPE_NONE, null);
1592 } else {
1593 web.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
1594 }
1595 }
1596
Grace Kloba22ac16e2009-10-07 18:00:23 -07001597 void pause() {
1598 if (mMainView != null) {
1599 mMainView.onPause();
1600 if (mSubView != null) {
1601 mSubView.onPause();
1602 }
1603 }
1604 }
1605
1606 void putInForeground() {
John Reck8ee633f2011-08-09 16:00:35 -07001607 if (mInForeground) {
1608 return;
1609 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001610 mInForeground = true;
1611 resume();
Michael Kolb14612442011-06-24 13:06:29 -07001612 Activity activity = mWebViewController.getActivity();
1613 mMainView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001614 if (mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -07001615 mSubView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001616 }
1617 // Show the pending error dialog if the queue is not empty
1618 if (mQueuedErrors != null && mQueuedErrors.size() > 0) {
1619 showError(mQueuedErrors.getFirst());
1620 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001621 mWebViewController.bookmarkedStatusHasChanged(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001622 }
1623
1624 void putInBackground() {
John Reck8ee633f2011-08-09 16:00:35 -07001625 if (!mInForeground) {
1626 return;
1627 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001628 mInForeground = false;
1629 pause();
1630 mMainView.setOnCreateContextMenuListener(null);
1631 if (mSubView != null) {
1632 mSubView.setOnCreateContextMenuListener(null);
1633 }
1634 }
1635
Michael Kolb8233fac2010-10-26 16:08:53 -07001636 boolean inForeground() {
1637 return mInForeground;
1638 }
1639
Grace Kloba22ac16e2009-10-07 18:00:23 -07001640 /**
1641 * Return the top window of this tab; either the subwindow if it is not
1642 * null or the main window.
1643 * @return The top window of this tab.
1644 */
1645 WebView getTopWindow() {
1646 if (mSubView != null) {
1647 return mSubView;
1648 }
1649 return mMainView;
1650 }
1651
1652 /**
1653 * Return the main window of this tab. Note: if a tab is freed in the
1654 * background, this can return null. It is only guaranteed to be
1655 * non-null for the current tab.
1656 * @return The main WebView of this tab.
1657 */
1658 WebView getWebView() {
1659 return mMainView;
1660 }
1661
Michael Kolba713ec82010-11-29 17:27:06 -08001662 void setViewContainer(View container) {
1663 mContainer = container;
1664 }
1665
Michael Kolb8233fac2010-10-26 16:08:53 -07001666 View getViewContainer() {
1667 return mContainer;
1668 }
1669
Grace Kloba22ac16e2009-10-07 18:00:23 -07001670 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001671 * Return whether private browsing is enabled for the main window of
1672 * this tab.
1673 * @return True if private browsing is enabled.
1674 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001675 boolean isPrivateBrowsingEnabled() {
John Reck502a3532011-08-16 14:21:46 -07001676 return mCurrentState.mIncognito;
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001677 }
1678
1679 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001680 * Return the subwindow of this tab or null if there is no subwindow.
1681 * @return The subwindow of this tab or null.
1682 */
1683 WebView getSubWebView() {
1684 return mSubView;
1685 }
1686
Michael Kolb1514bb72010-11-22 09:11:48 -08001687 void setSubWebView(WebView subView) {
1688 mSubView = subView;
1689 }
1690
Michael Kolb8233fac2010-10-26 16:08:53 -07001691 View getSubViewContainer() {
1692 return mSubViewContainer;
1693 }
1694
Michael Kolb1514bb72010-11-22 09:11:48 -08001695 void setSubViewContainer(View subViewContainer) {
1696 mSubViewContainer = subViewContainer;
1697 }
1698
Grace Kloba22ac16e2009-10-07 18:00:23 -07001699 /**
1700 * @return The geolocation permissions prompt for this tab.
1701 */
1702 GeolocationPermissionsPrompt getGeolocationPermissionsPrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001703 if (mGeolocationPermissionsPrompt == null) {
1704 ViewStub stub = (ViewStub) mContainer
1705 .findViewById(R.id.geolocation_permissions_prompt);
1706 mGeolocationPermissionsPrompt = (GeolocationPermissionsPrompt) stub
1707 .inflate();
Grace Kloba50c241e2010-04-20 11:07:50 -07001708 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001709 return mGeolocationPermissionsPrompt;
1710 }
1711
1712 /**
1713 * @return The application id string
1714 */
1715 String getAppId() {
1716 return mAppId;
1717 }
1718
1719 /**
1720 * Set the application id string
1721 * @param id
1722 */
1723 void setAppId(String id) {
1724 mAppId = id;
1725 }
1726
Michael Kolbe28b3472011-08-04 16:54:31 -07001727 boolean closeOnBack() {
1728 return mCloseOnBack;
1729 }
1730
1731 void setCloseOnBack(boolean close) {
1732 mCloseOnBack = close;
1733 }
1734
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001735 boolean getDerivedFromIntent() {
1736 return mDerivedFromIntent;
1737 }
1738
1739 void setDerivedFromIntent(boolean derived) {
1740 mDerivedFromIntent = derived;
1741 }
1742
Grace Kloba22ac16e2009-10-07 18:00:23 -07001743 String getUrl() {
John Reck324d4402011-01-11 16:56:42 -08001744 return UrlUtils.filteredUrl(mCurrentState.mUrl);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001745 }
1746
Tarun Nainani8eb00912014-07-17 12:28:32 -07001747
1748 protected void onPageFinished() {
1749 mPageFinished = true;
1750 }
1751
1752 public boolean getPageFinishedStatus() {
1753 return mPageFinished;
1754 }
1755
John Reck49a603c2011-03-03 09:33:05 -08001756 String getOriginalUrl() {
John Reckdb22ec42011-06-29 11:31:24 -07001757 if (mCurrentState.mOriginalUrl == null) {
1758 return getUrl();
John Reck49a603c2011-03-03 09:33:05 -08001759 }
John Reckdb22ec42011-06-29 11:31:24 -07001760 return UrlUtils.filteredUrl(mCurrentState.mOriginalUrl);
John Reck49a603c2011-03-03 09:33:05 -08001761 }
1762
Grace Kloba22ac16e2009-10-07 18:00:23 -07001763 /**
John Reck30c714c2010-12-16 17:30:34 -08001764 * Get the title of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001765 */
1766 String getTitle() {
John Reck30c714c2010-12-16 17:30:34 -08001767 if (mCurrentState.mTitle == null && mInPageLoad) {
Michael Kolb14612442011-06-24 13:06:29 -07001768 return mContext.getString(R.string.title_bar_loading);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001769 }
John Reck30c714c2010-12-16 17:30:34 -08001770 return mCurrentState.mTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001771 }
1772
1773 /**
John Reck30c714c2010-12-16 17:30:34 -08001774 * Get the favicon of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001775 */
1776 Bitmap getFavicon() {
John Reck1cf4b792011-07-26 10:22:22 -07001777 if (mCurrentState.mFavicon != null) {
1778 return mCurrentState.mFavicon;
1779 }
1780 return getDefaultFavicon(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001781 }
1782
John Recke969cc52010-12-21 17:24:43 -08001783 public boolean isBookmarkedSite() {
1784 return mCurrentState.mIsBookmarkedSite;
1785 }
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001786
Grace Kloba22ac16e2009-10-07 18:00:23 -07001787 /**
1788 * Return the tab's error console. Creates the console if createIfNEcessary
1789 * is true and we haven't already created the console.
1790 * @param createIfNecessary Flag to indicate if the console should be
1791 * created if it has not been already.
1792 * @return The tab's error console, or null if one has not been created and
1793 * createIfNecessary is false.
1794 */
1795 ErrorConsoleView getErrorConsole(boolean createIfNecessary) {
1796 if (createIfNecessary && mErrorConsole == null) {
Michael Kolb14612442011-06-24 13:06:29 -07001797 mErrorConsole = new ErrorConsoleView(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001798 mErrorConsole.setWebView(mMainView);
1799 }
1800 return mErrorConsole;
1801 }
1802
Steve Block08a6f0c2011-10-06 12:12:53 +01001803 /**
1804 * Sets the security state, clears the SSL certificate error and informs
1805 * the controller.
1806 */
Steve Block2466eff2011-10-03 15:33:09 +01001807 private void setSecurityState(SecurityState securityState) {
1808 mCurrentState.mSecurityState = securityState;
Steve Block08a6f0c2011-10-06 12:12:53 +01001809 mCurrentState.mSslCertificateError = null;
Steve Block2466eff2011-10-03 15:33:09 +01001810 mWebViewController.onUpdatedSecurityState(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001811 }
1812
1813 /**
Steve Block2466eff2011-10-03 15:33:09 +01001814 * @return The tab's security state.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001815 */
Steve Block2466eff2011-10-03 15:33:09 +01001816 SecurityState getSecurityState() {
1817 return mCurrentState.mSecurityState;
John Reck30c714c2010-12-16 17:30:34 -08001818 }
1819
Steve Block08a6f0c2011-10-06 12:12:53 +01001820 /**
1821 * Gets the SSL certificate error, if any, for the page's main resource.
1822 * This is only non-null when the security state is
1823 * SECURITY_STATE_BAD_CERTIFICATE.
1824 */
1825 SslError getSslCertificateError() {
1826 return mCurrentState.mSslCertificateError;
1827 }
1828
John Reck30c714c2010-12-16 17:30:34 -08001829 int getLoadProgress() {
1830 if (mInPageLoad) {
1831 return mPageLoadProgress;
1832 }
1833 return 100;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001834 }
1835
1836 /**
1837 * @return TRUE if onPageStarted is called while onPageFinished is not
1838 * called yet.
1839 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001840 boolean inPageLoad() {
1841 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001842 }
1843
Grace Kloba22ac16e2009-10-07 18:00:23 -07001844 /**
John Reck1cf4b792011-07-26 10:22:22 -07001845 * @return The Bundle with the tab's state if it can be saved, otherwise null
Grace Kloba22ac16e2009-10-07 18:00:23 -07001846 */
John Reck1cf4b792011-07-26 10:22:22 -07001847 public Bundle saveState() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001848 // If the WebView is null it means we ran low on memory and we already
1849 // stored the saved state in mSavedState.
1850 if (mMainView == null) {
John Reck1cf4b792011-07-26 10:22:22 -07001851 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001852 }
John Reck6c2e2f32011-08-22 13:41:23 -07001853
1854 if (TextUtils.isEmpty(mCurrentState.mUrl)) {
John Reck1cf4b792011-07-26 10:22:22 -07001855 return null;
John Reck24f18262011-06-17 14:47:20 -07001856 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001857
1858 mSavedState = new Bundle();
John Reck6c2e2f32011-08-22 13:41:23 -07001859 WebBackForwardList savedList = mMainView.saveState(mSavedState);
1860 if (savedList == null || savedList.getSize() == 0) {
1861 Log.w(LOGTAG, "Failed to save back/forward list for "
1862 + mCurrentState.mUrl);
1863 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001864
Michael Kolbc831b632011-05-11 09:30:34 -07001865 mSavedState.putLong(ID, mId);
John Reck30c714c2010-12-16 17:30:34 -08001866 mSavedState.putString(CURRURL, mCurrentState.mUrl);
1867 mSavedState.putString(CURRTITLE, mCurrentState.mTitle);
John Reck1cf4b792011-07-26 10:22:22 -07001868 mSavedState.putBoolean(INCOGNITO, mMainView.isPrivateBrowsingEnabled());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001869 if (mAppId != null) {
1870 mSavedState.putString(APPID, mAppId);
1871 }
Michael Kolbe28b3472011-08-04 16:54:31 -07001872 mSavedState.putBoolean(CLOSEFLAG, mCloseOnBack);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001873 // Remember the parent tab so the relationship can be restored.
Michael Kolbc831b632011-05-11 09:30:34 -07001874 if (mParent != null) {
1875 mSavedState.putLong(PARENTTAB, mParent.mId);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001876 }
John Reckb0a86db2011-05-24 14:05:58 -07001877 mSavedState.putBoolean(USERAGENT,
1878 mSettings.hasDesktopUseragent(getWebView()));
John Reck1cf4b792011-07-26 10:22:22 -07001879 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001880 }
1881
1882 /*
1883 * Restore the state of the tab.
1884 */
John Reck1cf4b792011-07-26 10:22:22 -07001885 private void restoreState(Bundle b) {
1886 mSavedState = b;
1887 if (mSavedState == null) {
1888 return;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001889 }
1890 // Restore the internal state even if the WebView fails to restore.
1891 // This will maintain the app id, original url and close-on-exit values.
Michael Kolbc831b632011-05-11 09:30:34 -07001892 mId = b.getLong(ID);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001893 mAppId = b.getString(APPID);
Michael Kolbe28b3472011-08-04 16:54:31 -07001894 mCloseOnBack = b.getBoolean(CLOSEFLAG);
John Reck8b9bb8b2012-03-08 13:19:40 -08001895 restoreUserAgent();
John Reck1cf4b792011-07-26 10:22:22 -07001896 String url = b.getString(CURRURL);
1897 String title = b.getString(CURRTITLE);
1898 boolean incognito = b.getBoolean(INCOGNITO);
1899 mCurrentState = new PageState(mContext, incognito, url, null);
1900 mCurrentState.mTitle = title;
1901 synchronized (Tab.this) {
1902 if (mCapture != null) {
John Reck4eadc342011-10-31 14:04:10 -07001903 DataController.getInstance(mContext).loadThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07001904 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001905 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001906 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04001907
John Reck8b9bb8b2012-03-08 13:19:40 -08001908 private void restoreUserAgent() {
1909 if (mMainView == null || mSavedState == null) {
1910 return;
1911 }
1912 if (mSavedState.getBoolean(USERAGENT)
1913 != mSettings.hasDesktopUseragent(mMainView)) {
1914 mSettings.toggleDesktopUseragent(mMainView);
1915 }
1916 }
1917
Leon Scroggins1961ed22010-12-07 15:22:21 -05001918 public void updateBookmarkedStatus() {
John Recke969cc52010-12-21 17:24:43 -08001919 mDataController.queryBookmarkStatus(getUrl(), mIsBookmarkCallback);
Leon Scroggins1961ed22010-12-07 15:22:21 -05001920 }
1921
John Recke969cc52010-12-21 17:24:43 -08001922 private DataController.OnQueryUrlIsBookmark mIsBookmarkCallback
1923 = new DataController.OnQueryUrlIsBookmark() {
1924 @Override
1925 public void onQueryUrlIsBookmark(String url, boolean isBookmark) {
1926 if (mCurrentState.mUrl.equals(url)) {
1927 mCurrentState.mIsBookmarkedSite = isBookmark;
1928 mWebViewController.bookmarkedStatusHasChanged(Tab.this);
1929 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001930 }
John Recke969cc52010-12-21 17:24:43 -08001931 };
Michael Kolb1acef692011-03-08 14:12:06 -08001932
Michael Kolbeb95db42011-03-03 10:38:40 -08001933 public Bitmap getScreenshot() {
John Reck1cf4b792011-07-26 10:22:22 -07001934 synchronized (Tab.this) {
1935 return mCapture;
1936 }
Michael Kolbeb95db42011-03-03 10:38:40 -08001937 }
1938
John Reck541f55a2011-06-07 16:34:43 -07001939 public boolean isSnapshot() {
John Reck541f55a2011-06-07 16:34:43 -07001940 return false;
1941 }
1942
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001943 private static class SaveCallback implements ValueCallback<String> {
1944 boolean onReceiveValueCalled = false;
1945 private String mPath;
John Reck68234a92012-04-19 15:27:12 -07001946
1947 @Override
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001948 public void onReceiveValue(String path) {
1949 this.onReceiveValueCalled = true;
1950 this.mPath = path;
John Reck68234a92012-04-19 15:27:12 -07001951 synchronized (this) {
1952 notifyAll();
John Reck8cc92352011-07-06 17:41:52 -07001953 }
John Reck541f55a2011-06-07 16:34:43 -07001954 }
John Reck68234a92012-04-19 15:27:12 -07001955
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001956 public String getPath() {
1957 return mPath;
1958 }
John Reck68234a92012-04-19 15:27:12 -07001959 }
1960
1961 /**
1962 * Must be called on the UI thread
1963 */
1964 public ContentValues createSnapshotValues() {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001965 WebView web = getWebView();
John Reck68234a92012-04-19 15:27:12 -07001966 if (web == null) return null;
John Reckd8c74522011-06-14 08:45:00 -07001967 ContentValues values = new ContentValues();
1968 values.put(Snapshots.TITLE, mCurrentState.mTitle);
1969 values.put(Snapshots.URL, mCurrentState.mUrl);
John Reck68234a92012-04-19 15:27:12 -07001970 values.put(Snapshots.BACKGROUND, web.getPageBackgroundColor());
John Reck8cc92352011-07-06 17:41:52 -07001971 values.put(Snapshots.DATE_CREATED, System.currentTimeMillis());
1972 values.put(Snapshots.FAVICON, compressBitmap(getFavicon()));
Tarun Nainani8eb00912014-07-17 12:28:32 -07001973 Bitmap screenshot = Controller.createScreenshot(web,
1974 Controller.getDesiredThumbnailWidth(mWebViewController.getActivity()),
1975 Controller.getDesiredThumbnailHeight(mWebViewController.getActivity()));
1976 values.put(Snapshots.THUMBNAIL, compressBitmap(screenshot));
John Reckd8c74522011-06-14 08:45:00 -07001977 return values;
John Reck541f55a2011-06-07 16:34:43 -07001978 }
1979
John Reck68234a92012-04-19 15:27:12 -07001980 /**
1981 * Probably want to call this on a background thread
1982 */
1983 public boolean saveViewState(ContentValues values) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001984 WebView web = getWebView();
John Reck68234a92012-04-19 15:27:12 -07001985 if (web == null) return false;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001986 String filename = UUID.randomUUID().toString();
John Reck68234a92012-04-19 15:27:12 -07001987 SaveCallback callback = new SaveCallback();
John Reck68234a92012-04-19 15:27:12 -07001988 try {
John Reck68234a92012-04-19 15:27:12 -07001989 synchronized (callback) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001990 web.saveViewState(filename, callback);
1991 callback.wait();
John Reck68234a92012-04-19 15:27:12 -07001992 }
John Reck68234a92012-04-19 15:27:12 -07001993 } catch (Exception e) {
1994 Log.w(LOGTAG, "Failed to save view state", e);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001995 String path = callback.getPath();
1996 if (path != null) {
1997 File file = mContext.getFileStreamPath(path);
1998 if (file.exists() && !file.delete()) {
1999 file.deleteOnExit();
2000 }
John Reck68234a92012-04-19 15:27:12 -07002001 }
2002 return false;
2003 }
Tarun Nainani8eb00912014-07-17 12:28:32 -07002004
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002005 String path = callback.getPath();
Tarun Nainani8eb00912014-07-17 12:28:32 -07002006 // could be that saving of file failed
2007 if (path == null) {
2008 return false;
2009 }
2010
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002011 File savedFile = new File(path);
2012 if (!savedFile.exists()) {
2013 return false;
John Reck68234a92012-04-19 15:27:12 -07002014 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002015 values.put(Snapshots.VIEWSTATE_PATH, path.substring(path.lastIndexOf('/') + 1));
2016 values.put(Snapshots.VIEWSTATE_SIZE, savedFile.length());
John Reck68234a92012-04-19 15:27:12 -07002017 return true;
2018 }
2019
John Reck8cc92352011-07-06 17:41:52 -07002020 public byte[] compressBitmap(Bitmap bitmap) {
2021 if (bitmap == null) {
2022 return null;
2023 }
2024 ByteArrayOutputStream stream = new ByteArrayOutputStream();
2025 bitmap.compress(CompressFormat.PNG, 100, stream);
2026 return stream.toByteArray();
2027 }
2028
John Reck26b18322011-06-21 13:08:58 -07002029 public void loadUrl(String url, Map<String, String> headers) {
2030 if (mMainView != null) {
Michael Kolba53c9892011-10-05 13:31:40 -07002031 mPageLoadProgress = INITIAL_PROGRESS;
Michael Kolb14612442011-06-24 13:06:29 -07002032 mCurrentState = new PageState(mContext, false, url, null);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002033 handleJsInterface(mMainView, url);
John Reck26b18322011-06-21 13:08:58 -07002034 mMainView.loadUrl(url, headers);
2035 }
2036 }
2037
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002038 public void handleJsInterface(WebView webview, String url){
2039 if (url != null &&
2040 url.equals(mContext.getResources().getString(R.string.homepage_base)) &&
2041 url.startsWith("file:///")) {
2042 mJsInterfaceEnabled = true;
2043 webview.getSettings().setJavaScriptEnabled(true);
2044 webview.addJavascriptInterface(mContext, "default_homepage");
2045 } else {
2046 if (mJsInterfaceEnabled) {
2047 webview.removeJavascriptInterface("default_homepage");
2048 mJsInterfaceEnabled = false;
2049 }
2050 }
2051 }
2052
John Reck38b39652012-06-05 09:22:59 -07002053 public void disableUrlOverridingForLoad() {
2054 mDisableOverrideUrlLoading = true;
2055 }
2056
Michael Kolb9ef259a2011-07-12 15:33:08 -07002057 protected void capture() {
2058 if (mMainView == null || mCapture == null) return;
John Reck4eadc342011-10-31 14:04:10 -07002059 if (mMainView.getContentWidth() <= 0 || mMainView.getContentHeight() <= 0) {
2060 return;
2061 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002062
2063 mMainView
2064 .getContentBitmapAsync(
2065 (float) mCaptureWidth / mMainView.getWidth(),
2066 new Rect(),
2067 new ValueCallback<Bitmap>() {
2068 @Override
2069 public void onReceiveValue(Bitmap bitmap) {
2070 onCaptureCallback(bitmap);
2071 }});
2072 }
2073
2074 private void onCaptureCallback(Bitmap bitmap) {
2075 if (mCapture == null || bitmap == null)
2076 return;
2077
Michael Kolb9ef259a2011-07-12 15:33:08 -07002078 Canvas c = new Canvas(mCapture);
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002079 mCapture.eraseColor(Color.WHITE);
2080 c.drawBitmap(bitmap, 0, 0, null);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002081
Michael Kolba3194d02011-09-07 11:23:51 -07002082 // manually anti-alias the edges for the tilt
2083 c.drawRect(0, 0, 1, mCapture.getHeight(), sAlphaPaint);
2084 c.drawRect(mCapture.getWidth() - 1, 0, mCapture.getWidth(),
2085 mCapture.getHeight(), sAlphaPaint);
2086 c.drawRect(0, 0, mCapture.getWidth(), 1, sAlphaPaint);
2087 c.drawRect(0, mCapture.getHeight() - 1, mCapture.getWidth(),
2088 mCapture.getHeight(), sAlphaPaint);
Dianne Hackborn43cfe8a2011-08-02 16:59:35 -07002089 c.setBitmap(null);
John Reck8ee633f2011-08-09 16:00:35 -07002090 mHandler.removeMessages(MSG_CAPTURE);
John Reck1cf4b792011-07-26 10:22:22 -07002091 persistThumbnail();
John Reck8ee633f2011-08-09 16:00:35 -07002092 TabControl tc = mWebViewController.getTabControl();
2093 if (tc != null) {
2094 OnThumbnailUpdatedListener updateListener
2095 = tc.getOnThumbnailUpdatedListener();
2096 if (updateListener != null) {
2097 updateListener.onThumbnailUpdated(this);
2098 }
2099 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07002100 }
2101
2102 @Override
2103 public void onNewPicture(WebView view, Picture picture) {
John Reck1cf4b792011-07-26 10:22:22 -07002104 postCapture();
2105 }
2106
2107 private void postCapture() {
Michael Kolb9ef259a2011-07-12 15:33:08 -07002108 if (!mHandler.hasMessages(MSG_CAPTURE)) {
2109 mHandler.sendEmptyMessageDelayed(MSG_CAPTURE, CAPTURE_DELAY);
2110 }
2111 }
2112
John Reckef654f12011-07-12 16:42:08 -07002113 public boolean canGoBack() {
2114 return mMainView != null ? mMainView.canGoBack() : false;
2115 }
2116
2117 public boolean canGoForward() {
2118 return mMainView != null ? mMainView.canGoForward() : false;
2119 }
2120
2121 public void goBack() {
2122 if (mMainView != null) {
2123 mMainView.goBack();
2124 }
2125 }
2126
2127 public void goForward() {
2128 if (mMainView != null) {
2129 mMainView.goForward();
2130 }
2131 }
2132
Mathew Inwood1dd8e822011-08-03 14:34:29 +01002133 /**
2134 * Causes the tab back/forward stack to be cleared once, if the given URL is the next URL
2135 * to be added to the stack.
2136 *
2137 * This is used to ensure that preloaded URLs that are not subsequently seen by the user do
2138 * not appear in the back stack.
2139 */
Mathew Inwooda829d552011-09-02 14:16:25 +01002140 public void clearBackStackWhenItemAdded(Pattern urlPattern) {
2141 mClearHistoryUrlPattern = urlPattern;
Mathew Inwood1dd8e822011-08-03 14:34:29 +01002142 }
2143
John Reck1cf4b792011-07-26 10:22:22 -07002144 protected void persistThumbnail() {
John Reck4eadc342011-10-31 14:04:10 -07002145 DataController.getInstance(mContext).saveThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07002146 }
2147
2148 protected void deleteThumbnail() {
John Reck4eadc342011-10-31 14:04:10 -07002149 DataController.getInstance(mContext).deleteThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07002150 }
2151
John Reck4eadc342011-10-31 14:04:10 -07002152 void updateCaptureFromBlob(byte[] blob) {
John Reck1cf4b792011-07-26 10:22:22 -07002153 synchronized (Tab.this) {
2154 if (mCapture == null) {
2155 return;
2156 }
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002157 ByteBuffer buffer = ByteBuffer.wrap(blob);
Michael Kolbd837a112011-08-09 14:00:18 -07002158 try {
2159 mCapture.copyPixelsFromBuffer(buffer);
2160 } catch (RuntimeException rex) {
2161 Log.e(LOGTAG, "Load capture has mismatched sizes; buffer: "
2162 + buffer.capacity() + " blob: " + blob.length
2163 + "capture: " + mCapture.getByteCount());
2164 throw rex;
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002165 }
John Reck1cf4b792011-07-26 10:22:22 -07002166 }
2167 }
2168
John Reck52be4782011-08-26 15:37:29 -07002169 @Override
2170 public String toString() {
2171 StringBuilder builder = new StringBuilder(100);
2172 builder.append(mId);
2173 builder.append(") has parent: ");
2174 if (getParent() != null) {
2175 builder.append("true[");
2176 builder.append(getParent().getId());
2177 builder.append("]");
2178 } else {
2179 builder.append("false");
2180 }
2181 builder.append(", incog: ");
2182 builder.append(isPrivateBrowsingEnabled());
2183 if (!isPrivateBrowsingEnabled()) {
2184 builder.append(", title: ");
2185 builder.append(getTitle());
2186 builder.append(", url: ");
2187 builder.append(getUrl());
2188 }
2189 return builder.toString();
2190 }
2191
Steve Block4895b012011-10-03 16:26:46 +01002192 private void handleProceededAfterSslError(SslError error) {
2193 if (error.getUrl().equals(mCurrentState.mUrl)) {
2194 // The security state should currently be SECURITY_STATE_SECURE.
2195 setSecurityState(SecurityState.SECURITY_STATE_BAD_CERTIFICATE);
Steve Block08a6f0c2011-10-06 12:12:53 +01002196 mCurrentState.mSslCertificateError = error;
Steve Block4895b012011-10-03 16:26:46 +01002197 } else if (getSecurityState() == SecurityState.SECURITY_STATE_SECURE) {
Steve Block08a6f0c2011-10-06 12:12:53 +01002198 // The page's main resource is secure and this error is for a
2199 // sub-resource.
Steve Block4895b012011-10-03 16:26:46 +01002200 setSecurityState(SecurityState.SECURITY_STATE_MIXED);
2201 }
2202 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002203}