blob: 852aa25339f46fffc295bb716e0312e3628d4496 [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
17package com.android.browser;
18
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;
Grace Kloba22ac16e2009-10-07 18:00:23 -070026import android.graphics.Bitmap;
John Reck8cc92352011-07-06 17:41:52 -070027import android.graphics.Bitmap.CompressFormat;
Michael Kolb9ef259a2011-07-12 15:33:08 -070028import android.graphics.BitmapFactory;
29import android.graphics.Canvas;
Michael Kolbc3af0672011-08-09 10:24:41 -070030import android.graphics.Color;
Michael Kolba3194d02011-09-07 11:23:51 -070031import android.graphics.Paint;
Michael Kolb9ef259a2011-07-12 15:33:08 -070032import android.graphics.Picture;
Michael Kolba3194d02011-09-07 11:23:51 -070033import android.graphics.PorterDuff;
34import android.graphics.PorterDuffXfermode;
Grace Kloba22ac16e2009-10-07 18:00:23 -070035import android.net.Uri;
36import android.net.http.SslError;
Grace Kloba22ac16e2009-10-07 18:00:23 -070037import android.os.Bundle;
Michael Kolb9ef259a2011-07-12 15:33:08 -070038import android.os.Handler;
Grace Kloba22ac16e2009-10-07 18:00:23 -070039import android.os.Message;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +000040import android.os.SystemClock;
Brian Carlstrom8862c1d2011-06-02 01:05:55 -070041import android.security.KeyChain;
Brian Carlstromaa09cd82011-06-09 16:04:40 -070042import android.security.KeyChainAliasCallback;
John Reck24f18262011-06-17 14:47:20 -070043import android.text.TextUtils;
Grace Kloba22ac16e2009-10-07 18:00:23 -070044import android.util.Log;
45import android.view.KeyEvent;
46import android.view.LayoutInflater;
47import android.view.View;
Grace Kloba50c241e2010-04-20 11:07:50 -070048import android.view.ViewStub;
Selim Gurun0b3d66f2012-08-29 13:08:13 -070049import android.webkit.BrowserDownloadListener;
Brian Carlstrom8862c1d2011-06-02 01:05:55 -070050import android.webkit.ClientCertRequestHandler;
Ben Murdochc42addf2010-01-28 15:19:59 +000051import android.webkit.ConsoleMessage;
Grace Kloba22ac16e2009-10-07 18:00:23 -070052import android.webkit.GeolocationPermissions;
53import android.webkit.HttpAuthHandler;
54import android.webkit.SslErrorHandler;
55import android.webkit.URLUtil;
56import android.webkit.ValueCallback;
John Reck6c2e2f32011-08-22 13:41:23 -070057import android.webkit.WebBackForwardList;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -050058import android.webkit.WebBackForwardListClient;
Grace Kloba22ac16e2009-10-07 18:00:23 -070059import android.webkit.WebChromeClient;
60import android.webkit.WebHistoryItem;
John Reck438bf462011-01-12 18:11:46 -080061import android.webkit.WebResourceResponse;
Grace Kloba22ac16e2009-10-07 18:00:23 -070062import android.webkit.WebStorage;
63import android.webkit.WebView;
Michael Kolb9ef259a2011-07-12 15:33:08 -070064import android.webkit.WebView.PictureListener;
John Reck2b71d6d2012-04-18 17:42:06 -070065import android.webkit.WebViewClassic;
Grace Kloba22ac16e2009-10-07 18:00:23 -070066import android.webkit.WebViewClient;
Jonathan Dixonf4ff1492012-06-08 10:15:32 -070067import android.webkit.WebViewClientClassicExt;
Ben Murdoch1d676b62011-01-17 12:54:24 +000068import android.widget.CheckBox;
Ben Murdoch8029a772010-11-16 11:58:21 +000069import android.widget.Toast;
Grace Kloba22ac16e2009-10-07 18:00:23 -070070
John Reck8ee633f2011-08-09 16:00:35 -070071import com.android.browser.TabControl.OnThumbnailUpdatedListener;
John Reck541f55a2011-06-07 16:34:43 -070072import com.android.browser.homepages.HomeProvider;
kaiyiz6e5b3e02013-08-19 20:02:01 +080073import com.android.browser.mynavigation.MyNavigationUtil;
74import com.android.browser.provider.MyNavigationProvider;
John Reck8cc92352011-07-06 17:41:52 -070075import com.android.browser.provider.SnapshotProvider.Snapshots;
John Reck541f55a2011-06-07 16:34:43 -070076
77import java.io.ByteArrayOutputStream;
John Reck2b71d6d2012-04-18 17:42:06 -070078import java.io.File;
John Reck68234a92012-04-19 15:27:12 -070079import java.io.IOException;
John Reck2b71d6d2012-04-18 17:42:06 -070080import java.io.OutputStream;
John Reck1cf4b792011-07-26 10:22:22 -070081import java.nio.ByteBuffer;
Michael Kolbfe251992010-07-08 15:41:55 -070082import java.util.LinkedList;
83import java.util.Map;
John Reck2b71d6d2012-04-18 17:42:06 -070084import java.util.UUID;
Michael Kolbfe251992010-07-08 15:41:55 -070085import java.util.Vector;
Mathew Inwooda829d552011-09-02 14:16:25 +010086import java.util.regex.Pattern;
John Reck8cc92352011-07-06 17:41:52 -070087import java.util.zip.GZIPOutputStream;
Michael Kolbfe251992010-07-08 15:41:55 -070088
Grace Kloba22ac16e2009-10-07 18:00:23 -070089/**
90 * Class for maintaining Tabs with a main WebView and a subwindow.
91 */
Michael Kolb9ef259a2011-07-12 15:33:08 -070092class Tab implements PictureListener {
Michael Kolb8233fac2010-10-26 16:08:53 -070093
Grace Kloba22ac16e2009-10-07 18:00:23 -070094 // Log Tag
95 private static final String LOGTAG = "Tab";
Mathew Inwood1dd8e822011-08-03 14:34:29 +010096 private static final boolean LOGD_ENABLED = com.android.browser.Browser.LOGD_ENABLED;
Ben Murdochc42addf2010-01-28 15:19:59 +000097 // Special case the logtag for messages for the Console to make it easier to
98 // filter them and match the logtag used for these messages in older versions
99 // of the browser.
100 private static final String CONSOLE_LOGTAG = "browser";
101
Michael Kolb9ef259a2011-07-12 15:33:08 -0700102 private static final int MSG_CAPTURE = 42;
John Reck8ee633f2011-08-09 16:00:35 -0700103 private static final int CAPTURE_DELAY = 100;
Michael Kolba53c9892011-10-05 13:31:40 -0700104 private static final int INITIAL_PROGRESS = 5;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700105
John Reck1cf4b792011-07-26 10:22:22 -0700106 private static Bitmap sDefaultFavicon;
107
Michael Kolba3194d02011-09-07 11:23:51 -0700108 private static Paint sAlphaPaint = new Paint();
109 static {
110 sAlphaPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
111 sAlphaPaint.setColor(Color.TRANSPARENT);
112 }
113
Steve Block2466eff2011-10-03 15:33:09 +0100114 public enum SecurityState {
Steve Block4895b012011-10-03 16:26:46 +0100115 // The page's main resource does not use SSL. Note that we use this
116 // state irrespective of the SSL authentication state of sub-resources.
Steve Block2466eff2011-10-03 15:33:09 +0100117 SECURITY_STATE_NOT_SECURE,
Steve Block4895b012011-10-03 16:26:46 +0100118 // The page's main resource uses SSL and the certificate is good. The
119 // same is true of all sub-resources.
Steve Block2466eff2011-10-03 15:33:09 +0100120 SECURITY_STATE_SECURE,
Steve Block4895b012011-10-03 16:26:46 +0100121 // The page's main resource uses SSL and the certificate is good, but
122 // some sub-resources either do not use SSL or have problems with their
123 // certificates.
Steve Block2466eff2011-10-03 15:33:09 +0100124 SECURITY_STATE_MIXED,
Steve Block4895b012011-10-03 16:26:46 +0100125 // The page's main resource uses SSL but there is a problem with its
126 // certificate.
127 SECURITY_STATE_BAD_CERTIFICATE,
John Reck30c714c2010-12-16 17:30:34 -0800128 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700129
Michael Kolb14612442011-06-24 13:06:29 -0700130 Context mContext;
John Reckd8c74522011-06-14 08:45:00 -0700131 protected WebViewController mWebViewController;
Michael Kolb8233fac2010-10-26 16:08:53 -0700132
Michael Kolbc831b632011-05-11 09:30:34 -0700133 // The tab ID
John Reckd8c74522011-06-14 08:45:00 -0700134 private long mId = -1;
Michael Kolbc831b632011-05-11 09:30:34 -0700135
Grace Kloba22ac16e2009-10-07 18:00:23 -0700136 // The Geolocation permissions prompt
137 private GeolocationPermissionsPrompt mGeolocationPermissionsPrompt;
138 // Main WebView wrapper
Michael Kolba713ec82010-11-29 17:27:06 -0800139 private View mContainer;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700140 // Main WebView
141 private WebView mMainView;
142 // Subwindow container
143 private View mSubViewContainer;
144 // Subwindow WebView
145 private WebView mSubView;
146 // Saved bundle for when we are running low on memory. It contains the
147 // information needed to restore the WebView if the user goes back to the
148 // tab.
149 private Bundle mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700150 // Parent Tab. This is the Tab that created this Tab, or null if the Tab was
151 // created by the UI
Michael Kolbc831b632011-05-11 09:30:34 -0700152 private Tab mParent;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700153 // Tab that constructed by this Tab. This is used when this Tab is
154 // destroyed, it clears all mParentTab values in the children.
Michael Kolbc831b632011-05-11 09:30:34 -0700155 private Vector<Tab> mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700156 // If true, the tab is in the foreground of the current activity.
157 private boolean mInForeground;
Michael Kolb8233fac2010-10-26 16:08:53 -0700158 // If true, the tab is in page loading state (after onPageStarted,
159 // before onPageFinsihed)
160 private boolean mInPageLoad;
John Reck38b39652012-06-05 09:22:59 -0700161 private boolean mDisableOverrideUrlLoading;
John Reck30c714c2010-12-16 17:30:34 -0800162 // The last reported progress of the current page
163 private int mPageLoadProgress;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000164 // The time the load started, used to find load page time
165 private long mLoadStartTime;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700166 // Application identifier used to find tabs that another application wants
167 // to reuse.
168 private String mAppId;
Michael Kolbe28b3472011-08-04 16:54:31 -0700169 // flag to indicate if tab should be closed on back
170 private boolean mCloseOnBack;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700171 // Keep the original url around to avoid killing the old WebView if the url
172 // has not changed.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700173 // Error console for the tab
174 private ErrorConsoleView mErrorConsole;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -0500175 // The listener that gets invoked when a download is started from the
176 // mMainView
Selim Gurun0b3d66f2012-08-29 13:08:13 -0700177 private final BrowserDownloadListener mDownloadListener;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500178 // Listener used to know when we move forward or back in the history list.
179 private final WebBackForwardListClient mWebBackForwardListClient;
John Recke969cc52010-12-21 17:24:43 -0800180 private DataController mDataController;
Patrick Scott92066772011-03-10 08:46:27 -0500181 // State of the auto-login request.
182 private DeviceAccountLogin mDeviceAccountLogin;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700183
184 // AsyncTask for downloading touch icons
185 DownloadTouchIcon mTouchIconLoader;
186
John Reck35e9dd62011-04-25 09:01:54 -0700187 private BrowserSettings mSettings;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700188 private int mCaptureWidth;
189 private int mCaptureHeight;
190 private Bitmap mCapture;
191 private Handler mHandler;
Michael Kolb72864272012-05-03 15:42:15 -0700192 private boolean mUpdateThumbnail;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700193
Mathew Inwood1dd8e822011-08-03 14:34:29 +0100194 /**
Mathew Inwood9ad1eac2011-09-15 11:29:50 +0100195 * See {@link #clearBackStackWhenItemAdded(String)}.
Mathew Inwood1dd8e822011-08-03 14:34:29 +0100196 */
Mathew Inwooda829d552011-09-02 14:16:25 +0100197 private Pattern mClearHistoryUrlPattern;
Mathew Inwood1dd8e822011-08-03 14:34:29 +0100198
John Reck1cf4b792011-07-26 10:22:22 -0700199 private static synchronized Bitmap getDefaultFavicon(Context context) {
200 if (sDefaultFavicon == null) {
201 sDefaultFavicon = BitmapFactory.decodeResource(
202 context.getResources(), R.drawable.app_web_browser_sm);
203 }
204 return sDefaultFavicon;
205 }
Michael Kolbeb95db42011-03-03 10:38:40 -0800206
John Reck30c714c2010-12-16 17:30:34 -0800207 // All the state needed for a page
John Reckd8c74522011-06-14 08:45:00 -0700208 protected static class PageState {
John Reck30c714c2010-12-16 17:30:34 -0800209 String mUrl;
John Reckdb22ec42011-06-29 11:31:24 -0700210 String mOriginalUrl;
John Reck30c714c2010-12-16 17:30:34 -0800211 String mTitle;
Steve Block2466eff2011-10-03 15:33:09 +0100212 SecurityState mSecurityState;
Steve Block08a6f0c2011-10-06 12:12:53 +0100213 // This is non-null only when mSecurityState is SECURITY_STATE_BAD_CERTIFICATE.
214 SslError mSslCertificateError;
John Reck30c714c2010-12-16 17:30:34 -0800215 Bitmap mFavicon;
Steve Block08a6f0c2011-10-06 12:12:53 +0100216 boolean mIsBookmarkedSite;
217 boolean mIncognito;
John Reck30c714c2010-12-16 17:30:34 -0800218
219 PageState(Context c, boolean incognito) {
John Reck502a3532011-08-16 14:21:46 -0700220 mIncognito = incognito;
221 if (mIncognito) {
John Reckdb22ec42011-06-29 11:31:24 -0700222 mOriginalUrl = mUrl = "browser:incognito";
John Reck30c714c2010-12-16 17:30:34 -0800223 mTitle = c.getString(R.string.new_incognito_tab);
John Reck30c714c2010-12-16 17:30:34 -0800224 } else {
John Reckdb22ec42011-06-29 11:31:24 -0700225 mOriginalUrl = mUrl = "";
John Reck30c714c2010-12-16 17:30:34 -0800226 mTitle = c.getString(R.string.new_tab);
John Reck30c714c2010-12-16 17:30:34 -0800227 }
Steve Block2466eff2011-10-03 15:33:09 +0100228 mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
John Reck30c714c2010-12-16 17:30:34 -0800229 }
230
231 PageState(Context c, boolean incognito, String url, Bitmap favicon) {
John Reck502a3532011-08-16 14:21:46 -0700232 mIncognito = incognito;
John Reckdb22ec42011-06-29 11:31:24 -0700233 mOriginalUrl = mUrl = url;
John Reck30c714c2010-12-16 17:30:34 -0800234 if (URLUtil.isHttpsUrl(url)) {
Steve Block2466eff2011-10-03 15:33:09 +0100235 mSecurityState = SecurityState.SECURITY_STATE_SECURE;
John Reck30c714c2010-12-16 17:30:34 -0800236 } else {
Steve Block2466eff2011-10-03 15:33:09 +0100237 mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
John Reck30c714c2010-12-16 17:30:34 -0800238 }
John Reck1cf4b792011-07-26 10:22:22 -0700239 mFavicon = favicon;
John Reck30c714c2010-12-16 17:30:34 -0800240 }
John Reck1cf4b792011-07-26 10:22:22 -0700241
Grace Kloba22ac16e2009-10-07 18:00:23 -0700242 }
243
John Reck30c714c2010-12-16 17:30:34 -0800244 // The current/loading page's state
John Reckd8c74522011-06-14 08:45:00 -0700245 protected PageState mCurrentState;
John Reck30c714c2010-12-16 17:30:34 -0800246
Grace Kloba22ac16e2009-10-07 18:00:23 -0700247 // Used for saving and restoring each Tab
Michael Kolbc831b632011-05-11 09:30:34 -0700248 static final String ID = "ID";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700249 static final String CURRURL = "currentUrl";
250 static final String CURRTITLE = "currentTitle";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700251 static final String PARENTTAB = "parentTab";
252 static final String APPID = "appid";
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700253 static final String INCOGNITO = "privateBrowsingEnabled";
John Reckb0a86db2011-05-24 14:05:58 -0700254 static final String USERAGENT = "useragent";
Michael Kolbe28b3472011-08-04 16:54:31 -0700255 static final String CLOSEFLAG = "closeOnBack";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700256
Grace Kloba22ac16e2009-10-07 18:00:23 -0700257 // Container class for the next error dialog that needs to be displayed
258 private class ErrorDialog {
259 public final int mTitle;
260 public final String mDescription;
261 public final int mError;
262 ErrorDialog(int title, String desc, int error) {
263 mTitle = title;
264 mDescription = desc;
265 mError = error;
266 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700267 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700268
269 private void processNextError() {
270 if (mQueuedErrors == null) {
271 return;
272 }
273 // The first one is currently displayed so just remove it.
274 mQueuedErrors.removeFirst();
275 if (mQueuedErrors.size() == 0) {
276 mQueuedErrors = null;
277 return;
278 }
279 showError(mQueuedErrors.getFirst());
280 }
281
282 private DialogInterface.OnDismissListener mDialogListener =
283 new DialogInterface.OnDismissListener() {
284 public void onDismiss(DialogInterface d) {
285 processNextError();
286 }
287 };
288 private LinkedList<ErrorDialog> mQueuedErrors;
289
290 private void queueError(int err, String desc) {
291 if (mQueuedErrors == null) {
292 mQueuedErrors = new LinkedList<ErrorDialog>();
293 }
294 for (ErrorDialog d : mQueuedErrors) {
295 if (d.mError == err) {
296 // Already saw a similar error, ignore the new one.
297 return;
298 }
299 }
300 ErrorDialog errDialog = new ErrorDialog(
301 err == WebViewClient.ERROR_FILE_NOT_FOUND ?
302 R.string.browserFrameFileErrorLabel :
303 R.string.browserFrameNetworkErrorLabel,
304 desc, err);
305 mQueuedErrors.addLast(errDialog);
306
307 // Show the dialog now if the queue was empty and it is in foreground
308 if (mQueuedErrors.size() == 1 && mInForeground) {
309 showError(errDialog);
310 }
311 }
312
313 private void showError(ErrorDialog errDialog) {
314 if (mInForeground) {
Michael Kolb14612442011-06-24 13:06:29 -0700315 AlertDialog d = new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700316 .setTitle(errDialog.mTitle)
317 .setMessage(errDialog.mDescription)
318 .setPositiveButton(R.string.ok, null)
319 .create();
320 d.setOnDismissListener(mDialogListener);
321 d.show();
322 }
323 }
324
325 // -------------------------------------------------------------------------
326 // WebViewClient implementation for the main WebView
327 // -------------------------------------------------------------------------
328
Jonathan Dixonf4ff1492012-06-08 10:15:32 -0700329 private final WebViewClientClassicExt mWebViewClient = new WebViewClientClassicExt() {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500330 private Message mDontResend;
331 private Message mResend;
Michael Kolb47bd1e42011-09-01 15:25:00 -0700332
333 private boolean providersDiffer(String url, String otherUrl) {
334 Uri uri1 = Uri.parse(url);
335 Uri uri2 = Uri.parse(otherUrl);
336 return !uri1.getEncodedAuthority().equals(uri2.getEncodedAuthority());
337 }
338
Grace Kloba22ac16e2009-10-07 18:00:23 -0700339 @Override
340 public void onPageStarted(WebView view, String url, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700341 mInPageLoad = true;
Michael Kolb72864272012-05-03 15:42:15 -0700342 mUpdateThumbnail = true;
Michael Kolba53c9892011-10-05 13:31:40 -0700343 mPageLoadProgress = INITIAL_PROGRESS;
Michael Kolb14612442011-06-24 13:06:29 -0700344 mCurrentState = new PageState(mContext,
John Reck30c714c2010-12-16 17:30:34 -0800345 view.isPrivateBrowsingEnabled(), url, favicon);
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000346 mLoadStartTime = SystemClock.uptimeMillis();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700347
348 // If we start a touch icon load and then load a new page, we don't
349 // want to cancel the current touch icon loader. But, we do want to
350 // create a new one when the touch icon url is known.
351 if (mTouchIconLoader != null) {
352 mTouchIconLoader.mTab = null;
353 mTouchIconLoader = null;
354 }
355
356 // reset the error console
357 if (mErrorConsole != null) {
358 mErrorConsole.clearErrorMessages();
Michael Kolb8233fac2010-10-26 16:08:53 -0700359 if (mWebViewController.shouldShowErrorConsole()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700360 mErrorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
361 }
362 }
363
Patrick Scott92066772011-03-10 08:46:27 -0500364 // Cancel the auto-login process.
365 if (mDeviceAccountLogin != null) {
366 mDeviceAccountLogin.cancel();
367 mDeviceAccountLogin = null;
368 mWebViewController.hideAutoLogin(Tab.this);
369 }
370
Grace Kloba22ac16e2009-10-07 18:00:23 -0700371 // finally update the UI in the activity if it is in the foreground
John Reck324d4402011-01-11 16:56:42 -0800372 mWebViewController.onPageStarted(Tab.this, view, favicon);
Leon Scroggins4cd97792010-12-03 15:31:56 -0500373
John Recke969cc52010-12-21 17:24:43 -0800374 updateBookmarkedStatus();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700375 }
376
377 @Override
378 public void onPageFinished(WebView view, String url) {
John Reck38b39652012-06-05 09:22:59 -0700379 mDisableOverrideUrlLoading = false;
John Reck5b691842010-11-29 11:21:13 -0800380 if (!isPrivateBrowsingEnabled()) {
381 LogTag.logPageFinishedLoading(
382 url, SystemClock.uptimeMillis() - mLoadStartTime);
383 }
John Reck1cf4b792011-07-26 10:22:22 -0700384 syncCurrentState(view, url);
John Reck324d4402011-01-11 16:56:42 -0800385 mWebViewController.onPageFinished(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700386 }
387
388 // return true if want to hijack the url to let another app to handle it
389 @Override
390 public boolean shouldOverrideUrlLoading(WebView view, String url) {
John Reck38b39652012-06-05 09:22:59 -0700391 if (!mDisableOverrideUrlLoading && mInForeground) {
Michael Kolb18eb3772010-12-10 14:29:51 -0800392 return mWebViewController.shouldOverrideUrlLoading(Tab.this,
393 view, url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700394 } else {
395 return false;
396 }
397 }
398
399 /**
Steve Block2466eff2011-10-03 15:33:09 +0100400 * Updates the security state. This method is called when we discover
401 * another resource to be loaded for this page (for example,
402 * javascript). While we update the security state, we do not update
403 * the lock icon until we are done loading, as it is slightly more
404 * secure this way.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700405 */
406 @Override
407 public void onLoadResource(WebView view, String url) {
408 if (url != null && url.length() > 0) {
409 // It is only if the page claims to be secure that we may have
Steve Block2466eff2011-10-03 15:33:09 +0100410 // to update the security state:
411 if (mCurrentState.mSecurityState == SecurityState.SECURITY_STATE_SECURE) {
412 // If NOT a 'safe' url, change the state to mixed content!
Grace Kloba22ac16e2009-10-07 18:00:23 -0700413 if (!(URLUtil.isHttpsUrl(url) || URLUtil.isDataUrl(url)
414 || URLUtil.isAboutUrl(url))) {
Steve Block2466eff2011-10-03 15:33:09 +0100415 mCurrentState.mSecurityState = SecurityState.SECURITY_STATE_MIXED;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700416 }
417 }
418 }
419 }
420
421 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700422 * Show a dialog informing the user of the network error reported by
423 * WebCore if it is in the foreground.
424 */
425 @Override
426 public void onReceivedError(WebView view, int errorCode,
427 String description, String failingUrl) {
428 if (errorCode != WebViewClient.ERROR_HOST_LOOKUP &&
429 errorCode != WebViewClient.ERROR_CONNECT &&
430 errorCode != WebViewClient.ERROR_BAD_URL &&
431 errorCode != WebViewClient.ERROR_UNSUPPORTED_SCHEME &&
432 errorCode != WebViewClient.ERROR_FILE) {
433 queueError(errorCode, description);
Jeff Hamilton47654f42010-09-07 09:57:51 -0500434
Selim Gurun3da06b82011-10-10 13:58:12 -0700435 // Don't log URLs when in private browsing mode
436 if (!isPrivateBrowsingEnabled()) {
437 Log.e(LOGTAG, "onReceivedError " + errorCode + " " + failingUrl
Jeff Hamilton47654f42010-09-07 09:57:51 -0500438 + " " + description);
Selim Gurun3da06b82011-10-10 13:58:12 -0700439 }
Jeff Hamilton47654f42010-09-07 09:57:51 -0500440 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700441 }
442
443 /**
444 * Check with the user if it is ok to resend POST data as the page they
445 * are trying to navigate to is the result of a POST.
446 */
447 @Override
448 public void onFormResubmission(WebView view, final Message dontResend,
449 final Message resend) {
450 if (!mInForeground) {
451 dontResend.sendToTarget();
452 return;
453 }
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500454 if (mDontResend != null) {
455 Log.w(LOGTAG, "onFormResubmission should not be called again "
456 + "while dialog is still up");
457 dontResend.sendToTarget();
458 return;
459 }
460 mDontResend = dontResend;
461 mResend = resend;
Michael Kolb14612442011-06-24 13:06:29 -0700462 new AlertDialog.Builder(mContext).setTitle(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700463 R.string.browserFrameFormResubmitLabel).setMessage(
464 R.string.browserFrameFormResubmitMessage)
465 .setPositiveButton(R.string.ok,
466 new DialogInterface.OnClickListener() {
467 public void onClick(DialogInterface dialog,
468 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500469 if (mResend != null) {
470 mResend.sendToTarget();
471 mResend = null;
472 mDontResend = null;
473 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700474 }
475 }).setNegativeButton(R.string.cancel,
476 new DialogInterface.OnClickListener() {
477 public void onClick(DialogInterface dialog,
478 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500479 if (mDontResend != null) {
480 mDontResend.sendToTarget();
481 mResend = null;
482 mDontResend = null;
483 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700484 }
485 }).setOnCancelListener(new OnCancelListener() {
486 public void onCancel(DialogInterface dialog) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500487 if (mDontResend != null) {
488 mDontResend.sendToTarget();
489 mResend = null;
490 mDontResend = null;
491 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700492 }
493 }).show();
494 }
495
496 /**
497 * Insert the url into the visited history database.
498 * @param url The url to be inserted.
499 * @param isReload True if this url is being reloaded.
500 * FIXME: Not sure what to do when reloading the page.
501 */
502 @Override
503 public void doUpdateVisitedHistory(WebView view, String url,
504 boolean isReload) {
John Reck324d4402011-01-11 16:56:42 -0800505 mWebViewController.doUpdateVisitedHistory(Tab.this, isReload);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700506 }
507
508 /**
509 * Displays SSL error(s) dialog to the user.
510 */
511 @Override
512 public void onReceivedSslError(final WebView view,
513 final SslErrorHandler handler, final SslError error) {
514 if (!mInForeground) {
515 handler.cancel();
Steve Block2466eff2011-10-03 15:33:09 +0100516 setSecurityState(SecurityState.SECURITY_STATE_NOT_SECURE);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700517 return;
518 }
John Reck35e9dd62011-04-25 09:01:54 -0700519 if (mSettings.showSecurityWarnings()) {
John Reckcb28b2c2011-08-26 17:39:44 -0700520 new AlertDialog.Builder(mContext)
521 .setTitle(R.string.security_warning)
522 .setMessage(R.string.ssl_warnings_header)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200523 .setIconAttribute(android.R.attr.alertDialogIcon)
John Reckcb28b2c2011-08-26 17:39:44 -0700524 .setPositiveButton(R.string.ssl_continue,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700525 new DialogInterface.OnClickListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700526 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700527 public void onClick(DialogInterface dialog,
528 int whichButton) {
529 handler.proceed();
Steve Block4895b012011-10-03 16:26:46 +0100530 handleProceededAfterSslError(error);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700531 }
John Reckcb28b2c2011-08-26 17:39:44 -0700532 })
533 .setNeutralButton(R.string.view_certificate,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700534 new DialogInterface.OnClickListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700535 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700536 public void onClick(DialogInterface dialog,
537 int whichButton) {
John Reckcb28b2c2011-08-26 17:39:44 -0700538 mWebViewController.showSslCertificateOnError(
539 view, handler, error);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700540 }
John Reckcb28b2c2011-08-26 17:39:44 -0700541 })
542 .setNegativeButton(R.string.ssl_go_back,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700543 new DialogInterface.OnClickListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700544 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700545 public void onClick(DialogInterface dialog,
546 int whichButton) {
John Reck30c714c2010-12-16 17:30:34 -0800547 dialog.cancel();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700548 }
John Reckcb28b2c2011-08-26 17:39:44 -0700549 })
550 .setOnCancelListener(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700551 new DialogInterface.OnCancelListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700552 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700553 public void onCancel(DialogInterface dialog) {
554 handler.cancel();
Steve Block2466eff2011-10-03 15:33:09 +0100555 setSecurityState(SecurityState.SECURITY_STATE_NOT_SECURE);
John Reck30c714c2010-12-16 17:30:34 -0800556 mWebViewController.onUserCanceledSsl(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700557 }
John Reckcb28b2c2011-08-26 17:39:44 -0700558 })
559 .show();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700560 } else {
561 handler.proceed();
562 }
563 }
564
565 /**
Steve Block4895b012011-10-03 16:26:46 +0100566 * Called when an SSL error occurred while loading a resource, but the
567 * WebView but chose to proceed anyway based on a decision retained
568 * from a previous response to onReceivedSslError(). We update our
569 * security state to reflect this.
570 */
571 @Override
572 public void onProceededAfterSslError(WebView view, SslError error) {
573 handleProceededAfterSslError(error);
574 }
575
576 /**
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700577 * Displays client certificate request to the user.
578 */
579 @Override
580 public void onReceivedClientCertRequest(final WebView view,
581 final ClientCertRequestHandler handler, final String host_and_port) {
582 if (!mInForeground) {
583 handler.ignore();
584 return;
585 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700586 int colon = host_and_port.lastIndexOf(':');
587 String host;
588 int port;
589 if (colon == -1) {
590 host = host_and_port;
591 port = -1;
592 } else {
593 String portString = host_and_port.substring(colon + 1);
594 try {
595 port = Integer.parseInt(portString);
596 host = host_and_port.substring(0, colon);
597 } catch (NumberFormatException e) {
598 host = host_and_port;
599 port = -1;
600 }
601 }
Michael Kolb14612442011-06-24 13:06:29 -0700602 KeyChain.choosePrivateKeyAlias(
603 mWebViewController.getActivity(), new KeyChainAliasCallback() {
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700604 @Override public void alias(String alias) {
605 if (alias == null) {
606 handler.cancel();
607 return;
608 }
Michael Kolb14612442011-06-24 13:06:29 -0700609 new KeyChainLookup(mContext, handler, alias).execute();
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700610 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700611 }, null, null, host, port, null);
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700612 }
613
614 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700615 * Handles an HTTP authentication request.
616 *
617 * @param handler The authentication handler
618 * @param host The host
619 * @param realm The realm
620 */
621 @Override
622 public void onReceivedHttpAuthRequest(WebView view,
623 final HttpAuthHandler handler, final String host,
624 final String realm) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700625 mWebViewController.onReceivedHttpAuthRequest(Tab.this, view, handler, host, realm);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700626 }
627
628 @Override
John Reck438bf462011-01-12 18:11:46 -0800629 public WebResourceResponse shouldInterceptRequest(WebView view,
630 String url) {
kaiyiz6e5b3e02013-08-19 20:02:01 +0800631 WebResourceResponse res;
632 if (MyNavigationUtil.MY_NAVIGATION.equals(url)) {
633 res = MyNavigationProvider.shouldInterceptRequest(mContext, url);
634 } else {
635 res = HomeProvider.shouldInterceptRequest(mContext, url);
636 }
John Reck438bf462011-01-12 18:11:46 -0800637 return res;
638 }
639
640 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700641 public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
642 if (!mInForeground) {
643 return false;
644 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700645 return mWebViewController.shouldOverrideKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700646 }
647
648 @Override
649 public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700650 if (!mInForeground) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700651 return;
652 }
John Reck997b1b72012-04-19 18:08:25 -0700653 if (!mWebViewController.onUnhandledKeyEvent(event)) {
654 super.onUnhandledKeyEvent(view, event);
655 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700656 }
Patrick Scott92066772011-03-10 08:46:27 -0500657
658 @Override
659 public void onReceivedLoginRequest(WebView view, String realm,
660 String account, String args) {
Michael Kolb14612442011-06-24 13:06:29 -0700661 new DeviceAccountLogin(mWebViewController.getActivity(), view, Tab.this, mWebViewController)
Patrick Scott92066772011-03-10 08:46:27 -0500662 .handleLogin(realm, account, args);
663 }
664
Grace Kloba22ac16e2009-10-07 18:00:23 -0700665 };
666
John Reck1cf4b792011-07-26 10:22:22 -0700667 private void syncCurrentState(WebView view, String url) {
668 // Sync state (in case of stop/timeout)
669 mCurrentState.mUrl = view.getUrl();
670 if (mCurrentState.mUrl == null) {
John Reck8bcafc12011-08-29 16:43:02 -0700671 mCurrentState.mUrl = "";
John Reck1cf4b792011-07-26 10:22:22 -0700672 }
673 mCurrentState.mOriginalUrl = view.getOriginalUrl();
674 mCurrentState.mTitle = view.getTitle();
675 mCurrentState.mFavicon = view.getFavicon();
676 if (!URLUtil.isHttpsUrl(mCurrentState.mUrl)) {
677 // In case we stop when loading an HTTPS page from an HTTP page
678 // but before a provisional load occurred
Steve Block2466eff2011-10-03 15:33:09 +0100679 mCurrentState.mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
Steve Block08a6f0c2011-10-06 12:12:53 +0100680 mCurrentState.mSslCertificateError = null;
John Reck1cf4b792011-07-26 10:22:22 -0700681 }
John Reck502a3532011-08-16 14:21:46 -0700682 mCurrentState.mIncognito = view.isPrivateBrowsingEnabled();
John Reck1cf4b792011-07-26 10:22:22 -0700683 }
684
Patrick Scott92066772011-03-10 08:46:27 -0500685 // Called by DeviceAccountLogin when the Tab needs to have the auto-login UI
686 // displayed.
687 void setDeviceAccountLogin(DeviceAccountLogin login) {
688 mDeviceAccountLogin = login;
689 }
690
691 // Returns non-null if the title bar should display the auto-login UI.
692 DeviceAccountLogin getDeviceAccountLogin() {
693 return mDeviceAccountLogin;
694 }
695
Grace Kloba22ac16e2009-10-07 18:00:23 -0700696 // -------------------------------------------------------------------------
697 // WebChromeClient implementation for the main WebView
698 // -------------------------------------------------------------------------
699
700 private final WebChromeClient mWebChromeClient = new WebChromeClient() {
701 // Helper method to create a new tab or sub window.
702 private void createWindow(final boolean dialog, final Message msg) {
703 WebView.WebViewTransport transport =
704 (WebView.WebViewTransport) msg.obj;
705 if (dialog) {
706 createSubWindow();
Michael Kolb8233fac2010-10-26 16:08:53 -0700707 mWebViewController.attachSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700708 transport.setWebView(mSubView);
709 } else {
Michael Kolb7bcafde2011-05-09 13:55:59 -0700710 final Tab newTab = mWebViewController.openTab(null,
John Reck5949c662011-05-27 09:52:29 -0700711 Tab.this, true, true);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700712 transport.setWebView(newTab.getWebView());
713 }
714 msg.sendToTarget();
715 }
716
717 @Override
718 public boolean onCreateWindow(WebView view, final boolean dialog,
719 final boolean userGesture, final Message resultMsg) {
720 // only allow new window or sub window for the foreground case
721 if (!mInForeground) {
722 return false;
723 }
724 // Short-circuit if we can't create any more tabs or sub windows.
725 if (dialog && mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -0700726 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700727 .setTitle(R.string.too_many_subwindows_dialog_title)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200728 .setIconAttribute(android.R.attr.alertDialogIcon)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700729 .setMessage(R.string.too_many_subwindows_dialog_message)
730 .setPositiveButton(R.string.ok, null)
731 .show();
732 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700733 } else if (!mWebViewController.getTabControl().canCreateNewTab()) {
Michael Kolb14612442011-06-24 13:06:29 -0700734 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700735 .setTitle(R.string.too_many_windows_dialog_title)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200736 .setIconAttribute(android.R.attr.alertDialogIcon)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700737 .setMessage(R.string.too_many_windows_dialog_message)
738 .setPositiveButton(R.string.ok, null)
739 .show();
740 return false;
741 }
742
743 // Short-circuit if this was a user gesture.
744 if (userGesture) {
745 createWindow(dialog, resultMsg);
746 return true;
747 }
748
749 // Allow the popup and create the appropriate window.
750 final AlertDialog.OnClickListener allowListener =
751 new AlertDialog.OnClickListener() {
752 public void onClick(DialogInterface d,
753 int which) {
754 createWindow(dialog, resultMsg);
755 }
756 };
757
758 // Block the popup by returning a null WebView.
759 final AlertDialog.OnClickListener blockListener =
760 new AlertDialog.OnClickListener() {
761 public void onClick(DialogInterface d, int which) {
762 resultMsg.sendToTarget();
763 }
764 };
765
766 // Build a confirmation dialog to display to the user.
767 final AlertDialog d =
Michael Kolb14612442011-06-24 13:06:29 -0700768 new AlertDialog.Builder(mContext)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200769 .setIconAttribute(android.R.attr.alertDialogIcon)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700770 .setMessage(R.string.popup_window_attempt)
771 .setPositiveButton(R.string.allow, allowListener)
772 .setNegativeButton(R.string.block, blockListener)
773 .setCancelable(false)
774 .create();
775
776 // Show the confirmation dialog.
777 d.show();
778 return true;
779 }
780
781 @Override
Patrick Scotteb5061b2009-11-18 15:00:30 -0500782 public void onRequestFocus(WebView view) {
783 if (!mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700784 mWebViewController.switchToTab(Tab.this);
Patrick Scotteb5061b2009-11-18 15:00:30 -0500785 }
786 }
787
788 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700789 public void onCloseWindow(WebView window) {
Michael Kolbc831b632011-05-11 09:30:34 -0700790 if (mParent != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700791 // JavaScript can only close popup window.
792 if (mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700793 mWebViewController.switchToTab(mParent);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700794 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700795 mWebViewController.closeTab(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700796 }
797 }
798
799 @Override
800 public void onProgressChanged(WebView view, int newProgress) {
John Reck30c714c2010-12-16 17:30:34 -0800801 mPageLoadProgress = newProgress;
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800802 if (newProgress == 100) {
803 mInPageLoad = false;
804 }
John Reck30c714c2010-12-16 17:30:34 -0800805 mWebViewController.onProgressChanged(Tab.this);
Michael Kolb72864272012-05-03 15:42:15 -0700806 if (mUpdateThumbnail && newProgress == 100) {
807 mUpdateThumbnail = false;
808 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700809 }
810
811 @Override
Leon Scroggins21d9b902010-03-11 09:33:11 -0500812 public void onReceivedTitle(WebView view, final String title) {
John Reck30c714c2010-12-16 17:30:34 -0800813 mCurrentState.mTitle = title;
Michael Kolb8233fac2010-10-26 16:08:53 -0700814 mWebViewController.onReceivedTitle(Tab.this, title);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700815 }
816
817 @Override
818 public void onReceivedIcon(WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -0800819 mCurrentState.mFavicon = icon;
Michael Kolb8233fac2010-10-26 16:08:53 -0700820 mWebViewController.onFavicon(Tab.this, view, icon);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700821 }
822
823 @Override
824 public void onReceivedTouchIconUrl(WebView view, String url,
825 boolean precomposed) {
Michael Kolb14612442011-06-24 13:06:29 -0700826 final ContentResolver cr = mContext.getContentResolver();
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400827 // Let precomposed icons take precedence over non-composed
828 // icons.
829 if (precomposed && mTouchIconLoader != null) {
830 mTouchIconLoader.cancel(false);
831 mTouchIconLoader = null;
832 }
833 // Have only one async task at a time.
834 if (mTouchIconLoader == null) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700835 mTouchIconLoader = new DownloadTouchIcon(Tab.this,
Michael Kolb14612442011-06-24 13:06:29 -0700836 mContext, cr, view);
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400837 mTouchIconLoader.execute(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700838 }
839 }
840
841 @Override
842 public void onShowCustomView(View view,
843 WebChromeClient.CustomViewCallback callback) {
Michael Kolb14612442011-06-24 13:06:29 -0700844 Activity activity = mWebViewController.getActivity();
845 if (activity != null) {
846 onShowCustomView(view, activity.getRequestedOrientation(), callback);
847 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400848 }
849
850 @Override
851 public void onShowCustomView(View view, int requestedOrientation,
852 WebChromeClient.CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700853 if (mInForeground) mWebViewController.showCustomView(Tab.this, view,
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400854 requestedOrientation, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700855 }
856
857 @Override
858 public void onHideCustomView() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700859 if (mInForeground) mWebViewController.hideCustomView();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700860 }
861
862 /**
863 * The origin has exceeded its database quota.
864 * @param url the URL that exceeded the quota
865 * @param databaseIdentifier the identifier of the database on which the
866 * transaction that caused the quota overflow was run
867 * @param currentQuota the current quota for the origin.
868 * @param estimatedSize the estimated size of the database.
869 * @param totalUsedQuota is the sum of all origins' quota.
870 * @param quotaUpdater The callback to run when a decision to allow or
871 * deny quota has been made. Don't forget to call this!
872 */
873 @Override
874 public void onExceededDatabaseQuota(String url,
875 String databaseIdentifier, long currentQuota, long estimatedSize,
876 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -0700877 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -0700878 .onExceededDatabaseQuota(url, databaseIdentifier,
879 currentQuota, estimatedSize, totalUsedQuota,
880 quotaUpdater);
881 }
882
883 /**
884 * The Application Cache has exceeded its max size.
885 * @param spaceNeeded is the amount of disk space that would be needed
886 * in order for the last appcache operation to succeed.
887 * @param totalUsedQuota is the sum of all origins' quota.
888 * @param quotaUpdater A callback to inform the WebCore thread that a
889 * new app cache size is available. This callback must always
890 * be executed at some point to ensure that the sleeping
891 * WebCore thread is woken up.
892 */
893 @Override
894 public void onReachedMaxAppCacheSize(long spaceNeeded,
895 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -0700896 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -0700897 .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota,
898 quotaUpdater);
899 }
900
901 /**
902 * Instructs the browser to show a prompt to ask the user to set the
903 * Geolocation permission state for the specified origin.
904 * @param origin The origin for which Geolocation permissions are
905 * requested.
906 * @param callback The callback to call once the user has set the
907 * Geolocation permission state.
908 */
909 @Override
910 public void onGeolocationPermissionsShowPrompt(String origin,
911 GeolocationPermissions.Callback callback) {
912 if (mInForeground) {
Grace Kloba50c241e2010-04-20 11:07:50 -0700913 getGeolocationPermissionsPrompt().show(origin, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700914 }
915 }
916
917 /**
918 * Instructs the browser to hide the Geolocation permissions prompt.
919 */
920 @Override
921 public void onGeolocationPermissionsHidePrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -0700922 if (mInForeground && mGeolocationPermissionsPrompt != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700923 mGeolocationPermissionsPrompt.hide();
924 }
925 }
926
Ben Murdoch65acc352009-11-19 18:16:04 +0000927 /* Adds a JavaScript error message to the system log and if the JS
928 * console is enabled in the about:debug options, to that console
929 * also.
Ben Murdochc42addf2010-01-28 15:19:59 +0000930 * @param consoleMessage the message object.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700931 */
932 @Override
Ben Murdochc42addf2010-01-28 15:19:59 +0000933 public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700934 if (mInForeground) {
935 // call getErrorConsole(true) so it will create one if needed
936 ErrorConsoleView errorConsole = getErrorConsole(true);
Ben Murdochc42addf2010-01-28 15:19:59 +0000937 errorConsole.addErrorMessage(consoleMessage);
Michael Kolb8233fac2010-10-26 16:08:53 -0700938 if (mWebViewController.shouldShowErrorConsole()
939 && errorConsole.getShowState() !=
940 ErrorConsoleView.SHOW_MAXIMIZED) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700941 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
942 }
943 }
Ben Murdochc42addf2010-01-28 15:19:59 +0000944
Jeff Hamilton47654f42010-09-07 09:57:51 -0500945 // Don't log console messages in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -0700946 if (isPrivateBrowsingEnabled()) return true;
Jeff Hamilton47654f42010-09-07 09:57:51 -0500947
Ben Murdochc42addf2010-01-28 15:19:59 +0000948 String message = "Console: " + consoleMessage.message() + " "
949 + consoleMessage.sourceId() + ":"
950 + consoleMessage.lineNumber();
951
952 switch (consoleMessage.messageLevel()) {
953 case TIP:
954 Log.v(CONSOLE_LOGTAG, message);
955 break;
956 case LOG:
957 Log.i(CONSOLE_LOGTAG, message);
958 break;
959 case WARNING:
960 Log.w(CONSOLE_LOGTAG, message);
961 break;
962 case ERROR:
963 Log.e(CONSOLE_LOGTAG, message);
964 break;
965 case DEBUG:
966 Log.d(CONSOLE_LOGTAG, message);
967 break;
968 }
969
970 return true;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700971 }
972
973 /**
974 * Ask the browser for an icon to represent a <video> element.
975 * This icon will be used if the Web page did not specify a poster attribute.
976 * @return Bitmap The icon or null if no such icon is available.
977 */
978 @Override
979 public Bitmap getDefaultVideoPoster() {
980 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700981 return mWebViewController.getDefaultVideoPoster();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700982 }
983 return null;
984 }
985
986 /**
987 * Ask the host application for a custom progress view to show while
988 * a <video> is loading.
989 * @return View The progress view.
990 */
991 @Override
992 public View getVideoLoadingProgressView() {
993 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700994 return mWebViewController.getVideoLoadingProgressView();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700995 }
996 return null;
997 }
998
999 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00001000 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001001 if (mInForeground) {
Ben Murdoch8cad4132012-01-11 10:56:43 +00001002 mWebViewController.openFileChooser(uploadMsg, acceptType, capture);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001003 } else {
1004 uploadMsg.onReceiveValue(null);
1005 }
1006 }
1007
1008 /**
1009 * Deliver a list of already-visited URLs
1010 */
1011 @Override
1012 public void getVisitedHistory(final ValueCallback<String[]> callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001013 mWebViewController.getVisitedHistory(callback);
1014 }
Ben Murdoch8029a772010-11-16 11:58:21 +00001015
1016 @Override
1017 public void setupAutoFill(Message message) {
1018 // Prompt the user to set up their profile.
1019 final Message msg = message;
Michael Kolb14612442011-06-24 13:06:29 -07001020 AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
1021 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
Ben Murdoch1d676b62011-01-17 12:54:24 +00001022 Context.LAYOUT_INFLATER_SERVICE);
1023 final View layout = inflater.inflate(R.layout.setup_autofill_dialog, null);
1024
1025 builder.setView(layout)
1026 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1027 @Override
1028 public void onClick(DialogInterface dialog, int id) {
1029 CheckBox disableAutoFill = (CheckBox) layout.findViewById(
1030 R.id.setup_autofill_dialog_disable_autofill);
1031
1032 if (disableAutoFill.isChecked()) {
1033 // Disable autofill and show a toast with how to turn it on again.
John Reck35e9dd62011-04-25 09:01:54 -07001034 mSettings.setAutofillEnabled(false);
Michael Kolb14612442011-06-24 13:06:29 -07001035 Toast.makeText(mContext,
Ben Murdoch1d676b62011-01-17 12:54:24 +00001036 R.string.autofill_setup_dialog_negative_toast,
1037 Toast.LENGTH_LONG).show();
1038 } else {
1039 // Take user to the AutoFill profile editor. When they return,
1040 // we will send the message that we pass here which will trigger
1041 // the form to get filled out with their new profile.
1042 mWebViewController.setupAutoFill(msg);
1043 }
1044 }
1045 })
1046 .setNegativeButton(R.string.cancel, null)
1047 .show();
Ben Murdoch8029a772010-11-16 11:58:21 +00001048 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001049 };
1050
1051 // -------------------------------------------------------------------------
1052 // WebViewClient implementation for the sub window
1053 // -------------------------------------------------------------------------
1054
1055 // Subclass of WebViewClient used in subwindows to notify the main
1056 // WebViewClient of certain WebView activities.
Jonathan Dixonf4ff1492012-06-08 10:15:32 -07001057 private static class SubWindowClient extends WebViewClientClassicExt {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001058 // The main WebViewClient.
Jonathan Dixonf4ff1492012-06-08 10:15:32 -07001059 private final WebViewClientClassicExt mClient;
Michael Kolb8233fac2010-10-26 16:08:53 -07001060 private final WebViewController mController;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001061
Jonathan Dixonf4ff1492012-06-08 10:15:32 -07001062 SubWindowClient(WebViewClientClassicExt client, WebViewController controller) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001063 mClient = client;
Michael Kolb8233fac2010-10-26 16:08:53 -07001064 mController = controller;
Leon Scroggins III211ba542010-04-19 13:21:13 -04001065 }
1066 @Override
1067 public void onPageStarted(WebView view, String url, Bitmap favicon) {
1068 // Unlike the others, do not call mClient's version, which would
1069 // change the progress bar. However, we do want to remove the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001070 // find or select dialog.
Michael Kolb8233fac2010-10-26 16:08:53 -07001071 mController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001072 }
1073 @Override
1074 public void doUpdateVisitedHistory(WebView view, String url,
1075 boolean isReload) {
1076 mClient.doUpdateVisitedHistory(view, url, isReload);
1077 }
1078 @Override
1079 public boolean shouldOverrideUrlLoading(WebView view, String url) {
1080 return mClient.shouldOverrideUrlLoading(view, url);
1081 }
1082 @Override
1083 public void onReceivedSslError(WebView view, SslErrorHandler handler,
1084 SslError error) {
1085 mClient.onReceivedSslError(view, handler, error);
1086 }
1087 @Override
Brian Carlstrom8862c1d2011-06-02 01:05:55 -07001088 public void onReceivedClientCertRequest(WebView view,
1089 ClientCertRequestHandler handler, String host_and_port) {
1090 mClient.onReceivedClientCertRequest(view, handler, host_and_port);
1091 }
1092 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -07001093 public void onReceivedHttpAuthRequest(WebView view,
1094 HttpAuthHandler handler, String host, String realm) {
1095 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
1096 }
1097 @Override
1098 public void onFormResubmission(WebView view, Message dontResend,
1099 Message resend) {
1100 mClient.onFormResubmission(view, dontResend, resend);
1101 }
1102 @Override
1103 public void onReceivedError(WebView view, int errorCode,
1104 String description, String failingUrl) {
1105 mClient.onReceivedError(view, errorCode, description, failingUrl);
1106 }
1107 @Override
1108 public boolean shouldOverrideKeyEvent(WebView view,
1109 android.view.KeyEvent event) {
1110 return mClient.shouldOverrideKeyEvent(view, event);
1111 }
1112 @Override
1113 public void onUnhandledKeyEvent(WebView view,
1114 android.view.KeyEvent event) {
1115 mClient.onUnhandledKeyEvent(view, event);
1116 }
1117 }
1118
1119 // -------------------------------------------------------------------------
1120 // WebChromeClient implementation for the sub window
1121 // -------------------------------------------------------------------------
1122
1123 private class SubWindowChromeClient extends WebChromeClient {
1124 // The main WebChromeClient.
1125 private final WebChromeClient mClient;
1126
1127 SubWindowChromeClient(WebChromeClient client) {
1128 mClient = client;
1129 }
1130 @Override
1131 public void onProgressChanged(WebView view, int newProgress) {
1132 mClient.onProgressChanged(view, newProgress);
1133 }
1134 @Override
1135 public boolean onCreateWindow(WebView view, boolean dialog,
1136 boolean userGesture, android.os.Message resultMsg) {
1137 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
1138 }
1139 @Override
1140 public void onCloseWindow(WebView window) {
1141 if (window != mSubView) {
1142 Log.e(LOGTAG, "Can't close the window");
1143 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001144 mWebViewController.dismissSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001145 }
1146 }
1147
1148 // -------------------------------------------------------------------------
1149
1150 // Construct a new tab
Michael Kolb7bcafde2011-05-09 13:55:59 -07001151 Tab(WebViewController wvcontroller, WebView w) {
John Reck1cf4b792011-07-26 10:22:22 -07001152 this(wvcontroller, w, null);
1153 }
1154
1155 Tab(WebViewController wvcontroller, Bundle state) {
1156 this(wvcontroller, null, state);
1157 }
1158
1159 Tab(WebViewController wvcontroller, WebView w, Bundle state) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001160 mWebViewController = wvcontroller;
Michael Kolb14612442011-06-24 13:06:29 -07001161 mContext = mWebViewController.getContext();
John Reck35e9dd62011-04-25 09:01:54 -07001162 mSettings = BrowserSettings.getInstance();
Michael Kolb14612442011-06-24 13:06:29 -07001163 mDataController = DataController.getInstance(mContext);
1164 mCurrentState = new PageState(mContext, w != null
John Reck847b5322011-04-14 17:02:18 -07001165 ? w.isPrivateBrowsingEnabled() : false);
Michael Kolb8233fac2010-10-26 16:08:53 -07001166 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001167 mInForeground = false;
1168
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001169 mDownloadListener = new BrowserDownloadListener() {
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001170 public void onDownloadStart(String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001171 String contentDisposition, String mimetype, String referer,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001172 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001173 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001174 mimetype, referer, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001175 }
1176 };
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001177 mWebBackForwardListClient = new WebBackForwardListClient() {
1178 @Override
1179 public void onNewHistoryItem(WebHistoryItem item) {
Mathew Inwooda829d552011-09-02 14:16:25 +01001180 if (mClearHistoryUrlPattern != null) {
1181 boolean match =
1182 mClearHistoryUrlPattern.matcher(item.getOriginalUrl()).matches();
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001183 if (LOGD_ENABLED) {
Mathew Inwooda829d552011-09-02 14:16:25 +01001184 Log.d(LOGTAG, "onNewHistoryItem: match=" + match + "\n\t"
1185 + item.getUrl() + "\n\t"
1186 + mClearHistoryUrlPattern);
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001187 }
Mathew Inwooda829d552011-09-02 14:16:25 +01001188 if (match) {
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001189 if (mMainView != null) {
1190 mMainView.clearHistory();
1191 }
1192 }
Mathew Inwooda829d552011-09-02 14:16:25 +01001193 mClearHistoryUrlPattern = null;
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001194 }
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001195 }
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001196 };
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001197
John Reck1cf4b792011-07-26 10:22:22 -07001198 mCaptureWidth = mContext.getResources().getDimensionPixelSize(
1199 R.dimen.tab_thumbnail_width);
1200 mCaptureHeight = mContext.getResources().getDimensionPixelSize(
1201 R.dimen.tab_thumbnail_height);
1202 updateShouldCaptureThumbnails();
1203 restoreState(state);
John Reck52be4782011-08-26 15:37:29 -07001204 if (getId() == -1) {
1205 mId = TabControl.getNextId();
1206 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001207 setWebView(w);
Michael Kolb9ef259a2011-07-12 15:33:08 -07001208 mHandler = new Handler() {
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001209 @Override
Michael Kolb9ef259a2011-07-12 15:33:08 -07001210 public void handleMessage(Message m) {
John Reck1cf4b792011-07-26 10:22:22 -07001211 switch (m.what) {
1212 case MSG_CAPTURE:
1213 capture();
1214 break;
1215 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001216 }
1217 };
John Reck1cf4b792011-07-26 10:22:22 -07001218 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001219
Michael Kolb72864272012-05-03 15:42:15 -07001220 public boolean shouldUpdateThumbnail() {
1221 return mUpdateThumbnail;
1222 }
1223
Mathew Inwoode09305e2011-09-02 12:03:26 +01001224 /**
1225 * This is used to get a new ID when the tab has been preloaded, before it is displayed and
1226 * added to TabControl. Preloaded tabs can be created before restoreInstanceState, leading
1227 * to overlapping IDs between the preloaded and restored tabs.
1228 */
1229 public void refreshIdAfterPreload() {
1230 mId = TabControl.getNextId();
1231 }
1232
John Reck1cf4b792011-07-26 10:22:22 -07001233 public void updateShouldCaptureThumbnails() {
1234 if (mWebViewController.shouldCaptureThumbnails()) {
1235 synchronized (Tab.this) {
1236 if (mCapture == null) {
1237 mCapture = Bitmap.createBitmap(mCaptureWidth, mCaptureHeight,
1238 Bitmap.Config.RGB_565);
Michael Kolbc3af0672011-08-09 10:24:41 -07001239 mCapture.eraseColor(Color.WHITE);
John Reck1cf4b792011-07-26 10:22:22 -07001240 if (mInForeground) {
1241 postCapture();
1242 }
1243 }
1244 }
1245 } else {
1246 synchronized (Tab.this) {
1247 mCapture = null;
1248 deleteThumbnail();
1249 }
1250 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001251 }
1252
Michael Kolb14612442011-06-24 13:06:29 -07001253 public void setController(WebViewController ctl) {
1254 mWebViewController = ctl;
John Reck1cf4b792011-07-26 10:22:22 -07001255 updateShouldCaptureThumbnails();
Michael Kolb14612442011-06-24 13:06:29 -07001256 }
1257
Michael Kolbc831b632011-05-11 09:30:34 -07001258 public long getId() {
1259 return mId;
1260 }
1261
Michael Kolb91911a22012-01-17 11:21:25 -08001262 void setWebView(WebView w) {
1263 setWebView(w, true);
1264 }
1265
Grace Kloba22ac16e2009-10-07 18:00:23 -07001266 /**
1267 * Sets the WebView for this tab, correctly removing the old WebView from
1268 * the container view.
1269 */
Michael Kolb91911a22012-01-17 11:21:25 -08001270 void setWebView(WebView w, boolean restore) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001271 if (mMainView == w) {
1272 return;
1273 }
Michael Kolba713ec82010-11-29 17:27:06 -08001274
Grace Kloba22ac16e2009-10-07 18:00:23 -07001275 // If the WebView is changing, the page will be reloaded, so any ongoing
1276 // Geolocation permission requests are void.
Grace Kloba50c241e2010-04-20 11:07:50 -07001277 if (mGeolocationPermissionsPrompt != null) {
1278 mGeolocationPermissionsPrompt.hide();
1279 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001280
Michael Kolba713ec82010-11-29 17:27:06 -08001281 mWebViewController.onSetWebView(this, w);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001282
John Reck1cf4b792011-07-26 10:22:22 -07001283 if (mMainView != null) {
John Reck8ee633f2011-08-09 16:00:35 -07001284 mMainView.setPictureListener(null);
John Reck1cf4b792011-07-26 10:22:22 -07001285 if (w != null) {
1286 syncCurrentState(w, null);
1287 } else {
1288 mCurrentState = new PageState(mContext, false);
1289 }
1290 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001291 // set the new one
1292 mMainView = w;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001293 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001294 if (mMainView != null) {
1295 mMainView.setWebViewClient(mWebViewClient);
1296 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001297 // Attach DownloadManager so that downloads can start in an active
1298 // or a non-active window. This can happen when going to a site that
1299 // does a redirect after a period of time. The user could have
1300 // switched to another tab while waiting for the download to start.
1301 mMainView.setDownloadListener(mDownloadListener);
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08001302 if (BrowserWebView.isClassic()) {
1303 getWebViewClassic().setWebBackForwardListClient(mWebBackForwardListClient);
1304 }
John Reck8ee633f2011-08-09 16:00:35 -07001305 TabControl tc = mWebViewController.getTabControl();
1306 if (tc != null && tc.getOnThumbnailUpdatedListener() != null) {
1307 mMainView.setPictureListener(this);
1308 }
Michael Kolb91911a22012-01-17 11:21:25 -08001309 if (restore && (mSavedState != null)) {
John Reck8b9bb8b2012-03-08 13:19:40 -08001310 restoreUserAgent();
John Reck6c2e2f32011-08-22 13:41:23 -07001311 WebBackForwardList restoredState
1312 = mMainView.restoreState(mSavedState);
1313 if (restoredState == null || restoredState.getSize() == 0) {
1314 Log.w(LOGTAG, "Failed to restore WebView state!");
1315 loadUrl(mCurrentState.mOriginalUrl, null);
1316 }
John Reck1cf4b792011-07-26 10:22:22 -07001317 mSavedState = null;
1318 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001319 }
1320 }
1321
1322 /**
1323 * Destroy the tab's main WebView and subWindow if any
1324 */
1325 void destroy() {
1326 if (mMainView != null) {
1327 dismissSubWindow();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001328 // save the WebView to call destroy() after detach it from the tab
1329 WebView webView = mMainView;
1330 setWebView(null);
1331 webView.destroy();
1332 }
1333 }
1334
1335 /**
1336 * Remove the tab from the parent
1337 */
1338 void removeFromTree() {
1339 // detach the children
Michael Kolbc831b632011-05-11 09:30:34 -07001340 if (mChildren != null) {
1341 for(Tab t : mChildren) {
1342 t.setParent(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001343 }
1344 }
1345 // remove itself from the parent list
Michael Kolbc831b632011-05-11 09:30:34 -07001346 if (mParent != null) {
1347 mParent.mChildren.remove(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001348 }
John Reck1cf4b792011-07-26 10:22:22 -07001349 deleteThumbnail();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001350 }
1351
1352 /**
1353 * Create a new subwindow unless a subwindow already exists.
1354 * @return True if a new subwindow was created. False if one already exists.
1355 */
1356 boolean createSubWindow() {
1357 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001358 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001359 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001360 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001361 mSubView.setWebChromeClient(new SubWindowChromeClient(
1362 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001363 // Set a different DownloadListener for the mSubView, since it will
1364 // just need to dismiss the mSubView, rather than close the Tab
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001365 mSubView.setDownloadListener(new BrowserDownloadListener() {
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001366 public void onDownloadStart(String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001367 String contentDisposition, String mimetype, String referer,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001368 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001369 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001370 contentDisposition, mimetype, referer, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001371 if (mSubView.copyBackForwardList().getSize() == 0) {
1372 // This subwindow was opened for the sole purpose of
1373 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001374 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001375 }
1376 }
1377 });
Michael Kolb14612442011-06-24 13:06:29 -07001378 mSubView.setOnCreateContextMenuListener(mWebViewController.getActivity());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001379 return true;
1380 }
1381 return false;
1382 }
1383
1384 /**
1385 * Dismiss the subWindow for the tab.
1386 */
1387 void dismissSubWindow() {
1388 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001389 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001390 mSubView.destroy();
1391 mSubView = null;
1392 mSubViewContainer = null;
1393 }
1394 }
1395
Grace Kloba22ac16e2009-10-07 18:00:23 -07001396
1397 /**
1398 * Set the parent tab of this tab.
1399 */
Michael Kolbc831b632011-05-11 09:30:34 -07001400 void setParent(Tab parent) {
John Reck52be4782011-08-26 15:37:29 -07001401 if (parent == this) {
1402 throw new IllegalStateException("Cannot set parent to self!");
1403 }
Michael Kolbc831b632011-05-11 09:30:34 -07001404 mParent = parent;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001405 // This tab may have been freed due to low memory. If that is the case,
Michael Kolbc831b632011-05-11 09:30:34 -07001406 // the parent tab id is already saved. If we are changing that id
Grace Kloba22ac16e2009-10-07 18:00:23 -07001407 // (most likely due to removing the parent tab) we must update the
Michael Kolbc831b632011-05-11 09:30:34 -07001408 // parent tab id in the saved Bundle.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001409 if (mSavedState != null) {
1410 if (parent == null) {
1411 mSavedState.remove(PARENTTAB);
1412 } else {
Michael Kolbc831b632011-05-11 09:30:34 -07001413 mSavedState.putLong(PARENTTAB, parent.getId());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001414 }
1415 }
John Reckb0a86db2011-05-24 14:05:58 -07001416
1417 // Sync the WebView useragent with the parent
1418 if (parent != null && mSettings.hasDesktopUseragent(parent.getWebView())
1419 != mSettings.hasDesktopUseragent(getWebView())) {
1420 mSettings.toggleDesktopUseragent(getWebView());
1421 }
John Reck52be4782011-08-26 15:37:29 -07001422
1423 if (parent != null && parent.getId() == getId()) {
1424 throw new IllegalStateException("Parent has same ID as child!");
1425 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001426 }
1427
1428 /**
Michael Kolbc831b632011-05-11 09:30:34 -07001429 * If this Tab was created through another Tab, then this method returns
1430 * that Tab.
1431 * @return the Tab parent or null
1432 */
1433 public Tab getParent() {
1434 return mParent;
1435 }
1436
1437 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001438 * When a Tab is created through the content of another Tab, then we
1439 * associate the Tabs.
1440 * @param child the Tab that was created from this Tab
1441 */
1442 void addChildTab(Tab child) {
Michael Kolbc831b632011-05-11 09:30:34 -07001443 if (mChildren == null) {
1444 mChildren = new Vector<Tab>();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001445 }
Michael Kolbc831b632011-05-11 09:30:34 -07001446 mChildren.add(child);
1447 child.setParent(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001448 }
1449
Michael Kolbc831b632011-05-11 09:30:34 -07001450 Vector<Tab> getChildren() {
1451 return mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001452 }
1453
1454 void resume() {
1455 if (mMainView != null) {
John Reck56c1fcf2011-08-17 10:15:16 -07001456 setupHwAcceleration(mMainView);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001457 mMainView.onResume();
1458 if (mSubView != null) {
1459 mSubView.onResume();
1460 }
1461 }
1462 }
1463
John Reck56c1fcf2011-08-17 10:15:16 -07001464 private void setupHwAcceleration(View web) {
1465 if (web == null) return;
1466 BrowserSettings settings = BrowserSettings.getInstance();
1467 if (settings.isHardwareAccelerated()) {
1468 web.setLayerType(View.LAYER_TYPE_NONE, null);
1469 } else {
1470 web.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
1471 }
1472 }
1473
Grace Kloba22ac16e2009-10-07 18:00:23 -07001474 void pause() {
1475 if (mMainView != null) {
1476 mMainView.onPause();
1477 if (mSubView != null) {
1478 mSubView.onPause();
1479 }
1480 }
1481 }
1482
1483 void putInForeground() {
John Reck8ee633f2011-08-09 16:00:35 -07001484 if (mInForeground) {
1485 return;
1486 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001487 mInForeground = true;
1488 resume();
Michael Kolb14612442011-06-24 13:06:29 -07001489 Activity activity = mWebViewController.getActivity();
1490 mMainView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001491 if (mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -07001492 mSubView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001493 }
1494 // Show the pending error dialog if the queue is not empty
1495 if (mQueuedErrors != null && mQueuedErrors.size() > 0) {
1496 showError(mQueuedErrors.getFirst());
1497 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001498 mWebViewController.bookmarkedStatusHasChanged(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001499 }
1500
1501 void putInBackground() {
John Reck8ee633f2011-08-09 16:00:35 -07001502 if (!mInForeground) {
1503 return;
1504 }
John Reck52be4782011-08-26 15:37:29 -07001505 capture();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001506 mInForeground = false;
1507 pause();
1508 mMainView.setOnCreateContextMenuListener(null);
1509 if (mSubView != null) {
1510 mSubView.setOnCreateContextMenuListener(null);
1511 }
1512 }
1513
Michael Kolb8233fac2010-10-26 16:08:53 -07001514 boolean inForeground() {
1515 return mInForeground;
1516 }
1517
Grace Kloba22ac16e2009-10-07 18:00:23 -07001518 /**
1519 * Return the top window of this tab; either the subwindow if it is not
1520 * null or the main window.
1521 * @return The top window of this tab.
1522 */
1523 WebView getTopWindow() {
1524 if (mSubView != null) {
1525 return mSubView;
1526 }
1527 return mMainView;
1528 }
1529
1530 /**
1531 * Return the main window of this tab. Note: if a tab is freed in the
1532 * background, this can return null. It is only guaranteed to be
1533 * non-null for the current tab.
1534 * @return The main WebView of this tab.
1535 */
1536 WebView getWebView() {
1537 return mMainView;
1538 }
1539
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00001540 /**
1541 * Return the underlying WebViewClassic implementation. As with getWebView,
1542 * this maybe null for background tabs.
1543 * @return The main WebView of this tab.
1544 */
1545 WebViewClassic getWebViewClassic() {
Jonathan Dixone1d6dfc2012-12-17 13:39:17 -08001546 if (!BrowserWebView.isClassic()) {
1547 return null;
1548 }
Jonathan Dixon4d2fcab2012-02-24 00:13:06 +00001549 return WebViewClassic.fromWebView(mMainView);
1550 }
1551
Michael Kolba713ec82010-11-29 17:27:06 -08001552 void setViewContainer(View container) {
1553 mContainer = container;
1554 }
1555
Michael Kolb8233fac2010-10-26 16:08:53 -07001556 View getViewContainer() {
1557 return mContainer;
1558 }
1559
Grace Kloba22ac16e2009-10-07 18:00:23 -07001560 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001561 * Return whether private browsing is enabled for the main window of
1562 * this tab.
1563 * @return True if private browsing is enabled.
1564 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001565 boolean isPrivateBrowsingEnabled() {
John Reck502a3532011-08-16 14:21:46 -07001566 return mCurrentState.mIncognito;
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001567 }
1568
1569 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001570 * Return the subwindow of this tab or null if there is no subwindow.
1571 * @return The subwindow of this tab or null.
1572 */
1573 WebView getSubWebView() {
1574 return mSubView;
1575 }
1576
Michael Kolb1514bb72010-11-22 09:11:48 -08001577 void setSubWebView(WebView subView) {
1578 mSubView = subView;
1579 }
1580
Michael Kolb8233fac2010-10-26 16:08:53 -07001581 View getSubViewContainer() {
1582 return mSubViewContainer;
1583 }
1584
Michael Kolb1514bb72010-11-22 09:11:48 -08001585 void setSubViewContainer(View subViewContainer) {
1586 mSubViewContainer = subViewContainer;
1587 }
1588
Grace Kloba22ac16e2009-10-07 18:00:23 -07001589 /**
1590 * @return The geolocation permissions prompt for this tab.
1591 */
1592 GeolocationPermissionsPrompt getGeolocationPermissionsPrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001593 if (mGeolocationPermissionsPrompt == null) {
1594 ViewStub stub = (ViewStub) mContainer
1595 .findViewById(R.id.geolocation_permissions_prompt);
1596 mGeolocationPermissionsPrompt = (GeolocationPermissionsPrompt) stub
1597 .inflate();
Grace Kloba50c241e2010-04-20 11:07:50 -07001598 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001599 return mGeolocationPermissionsPrompt;
1600 }
1601
1602 /**
1603 * @return The application id string
1604 */
1605 String getAppId() {
1606 return mAppId;
1607 }
1608
1609 /**
1610 * Set the application id string
1611 * @param id
1612 */
1613 void setAppId(String id) {
1614 mAppId = id;
1615 }
1616
Michael Kolbe28b3472011-08-04 16:54:31 -07001617 boolean closeOnBack() {
1618 return mCloseOnBack;
1619 }
1620
1621 void setCloseOnBack(boolean close) {
1622 mCloseOnBack = close;
1623 }
1624
Grace Kloba22ac16e2009-10-07 18:00:23 -07001625 String getUrl() {
John Reck324d4402011-01-11 16:56:42 -08001626 return UrlUtils.filteredUrl(mCurrentState.mUrl);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001627 }
1628
John Reck49a603c2011-03-03 09:33:05 -08001629 String getOriginalUrl() {
John Reckdb22ec42011-06-29 11:31:24 -07001630 if (mCurrentState.mOriginalUrl == null) {
1631 return getUrl();
John Reck49a603c2011-03-03 09:33:05 -08001632 }
John Reckdb22ec42011-06-29 11:31:24 -07001633 return UrlUtils.filteredUrl(mCurrentState.mOriginalUrl);
John Reck49a603c2011-03-03 09:33:05 -08001634 }
1635
Grace Kloba22ac16e2009-10-07 18:00:23 -07001636 /**
John Reck30c714c2010-12-16 17:30:34 -08001637 * Get the title of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001638 */
1639 String getTitle() {
John Reck30c714c2010-12-16 17:30:34 -08001640 if (mCurrentState.mTitle == null && mInPageLoad) {
Michael Kolb14612442011-06-24 13:06:29 -07001641 return mContext.getString(R.string.title_bar_loading);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001642 }
John Reck30c714c2010-12-16 17:30:34 -08001643 return mCurrentState.mTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001644 }
1645
1646 /**
John Reck30c714c2010-12-16 17:30:34 -08001647 * Get the favicon of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001648 */
1649 Bitmap getFavicon() {
John Reck1cf4b792011-07-26 10:22:22 -07001650 if (mCurrentState.mFavicon != null) {
1651 return mCurrentState.mFavicon;
1652 }
1653 return getDefaultFavicon(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001654 }
1655
John Recke969cc52010-12-21 17:24:43 -08001656 public boolean isBookmarkedSite() {
1657 return mCurrentState.mIsBookmarkedSite;
1658 }
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001659
Grace Kloba22ac16e2009-10-07 18:00:23 -07001660 /**
1661 * Return the tab's error console. Creates the console if createIfNEcessary
1662 * is true and we haven't already created the console.
1663 * @param createIfNecessary Flag to indicate if the console should be
1664 * created if it has not been already.
1665 * @return The tab's error console, or null if one has not been created and
1666 * createIfNecessary is false.
1667 */
1668 ErrorConsoleView getErrorConsole(boolean createIfNecessary) {
1669 if (createIfNecessary && mErrorConsole == null) {
Michael Kolb14612442011-06-24 13:06:29 -07001670 mErrorConsole = new ErrorConsoleView(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001671 mErrorConsole.setWebView(mMainView);
1672 }
1673 return mErrorConsole;
1674 }
1675
Steve Block08a6f0c2011-10-06 12:12:53 +01001676 /**
1677 * Sets the security state, clears the SSL certificate error and informs
1678 * the controller.
1679 */
Steve Block2466eff2011-10-03 15:33:09 +01001680 private void setSecurityState(SecurityState securityState) {
1681 mCurrentState.mSecurityState = securityState;
Steve Block08a6f0c2011-10-06 12:12:53 +01001682 mCurrentState.mSslCertificateError = null;
Steve Block2466eff2011-10-03 15:33:09 +01001683 mWebViewController.onUpdatedSecurityState(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001684 }
1685
1686 /**
Steve Block2466eff2011-10-03 15:33:09 +01001687 * @return The tab's security state.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001688 */
Steve Block2466eff2011-10-03 15:33:09 +01001689 SecurityState getSecurityState() {
1690 return mCurrentState.mSecurityState;
John Reck30c714c2010-12-16 17:30:34 -08001691 }
1692
Steve Block08a6f0c2011-10-06 12:12:53 +01001693 /**
1694 * Gets the SSL certificate error, if any, for the page's main resource.
1695 * This is only non-null when the security state is
1696 * SECURITY_STATE_BAD_CERTIFICATE.
1697 */
1698 SslError getSslCertificateError() {
1699 return mCurrentState.mSslCertificateError;
1700 }
1701
John Reck30c714c2010-12-16 17:30:34 -08001702 int getLoadProgress() {
1703 if (mInPageLoad) {
1704 return mPageLoadProgress;
1705 }
1706 return 100;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001707 }
1708
1709 /**
1710 * @return TRUE if onPageStarted is called while onPageFinished is not
1711 * called yet.
1712 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001713 boolean inPageLoad() {
1714 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001715 }
1716
Grace Kloba22ac16e2009-10-07 18:00:23 -07001717 /**
John Reck1cf4b792011-07-26 10:22:22 -07001718 * @return The Bundle with the tab's state if it can be saved, otherwise null
Grace Kloba22ac16e2009-10-07 18:00:23 -07001719 */
John Reck1cf4b792011-07-26 10:22:22 -07001720 public Bundle saveState() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001721 // If the WebView is null it means we ran low on memory and we already
1722 // stored the saved state in mSavedState.
1723 if (mMainView == null) {
John Reck1cf4b792011-07-26 10:22:22 -07001724 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001725 }
John Reck6c2e2f32011-08-22 13:41:23 -07001726
1727 if (TextUtils.isEmpty(mCurrentState.mUrl)) {
John Reck1cf4b792011-07-26 10:22:22 -07001728 return null;
John Reck24f18262011-06-17 14:47:20 -07001729 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001730
1731 mSavedState = new Bundle();
John Reck6c2e2f32011-08-22 13:41:23 -07001732 WebBackForwardList savedList = mMainView.saveState(mSavedState);
1733 if (savedList == null || savedList.getSize() == 0) {
1734 Log.w(LOGTAG, "Failed to save back/forward list for "
1735 + mCurrentState.mUrl);
1736 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001737
Michael Kolbc831b632011-05-11 09:30:34 -07001738 mSavedState.putLong(ID, mId);
John Reck30c714c2010-12-16 17:30:34 -08001739 mSavedState.putString(CURRURL, mCurrentState.mUrl);
1740 mSavedState.putString(CURRTITLE, mCurrentState.mTitle);
John Reck1cf4b792011-07-26 10:22:22 -07001741 mSavedState.putBoolean(INCOGNITO, mMainView.isPrivateBrowsingEnabled());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001742 if (mAppId != null) {
1743 mSavedState.putString(APPID, mAppId);
1744 }
Michael Kolbe28b3472011-08-04 16:54:31 -07001745 mSavedState.putBoolean(CLOSEFLAG, mCloseOnBack);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001746 // Remember the parent tab so the relationship can be restored.
Michael Kolbc831b632011-05-11 09:30:34 -07001747 if (mParent != null) {
1748 mSavedState.putLong(PARENTTAB, mParent.mId);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001749 }
John Reckb0a86db2011-05-24 14:05:58 -07001750 mSavedState.putBoolean(USERAGENT,
1751 mSettings.hasDesktopUseragent(getWebView()));
John Reck1cf4b792011-07-26 10:22:22 -07001752 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001753 }
1754
1755 /*
1756 * Restore the state of the tab.
1757 */
John Reck1cf4b792011-07-26 10:22:22 -07001758 private void restoreState(Bundle b) {
1759 mSavedState = b;
1760 if (mSavedState == null) {
1761 return;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001762 }
1763 // Restore the internal state even if the WebView fails to restore.
1764 // This will maintain the app id, original url and close-on-exit values.
Michael Kolbc831b632011-05-11 09:30:34 -07001765 mId = b.getLong(ID);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001766 mAppId = b.getString(APPID);
Michael Kolbe28b3472011-08-04 16:54:31 -07001767 mCloseOnBack = b.getBoolean(CLOSEFLAG);
John Reck8b9bb8b2012-03-08 13:19:40 -08001768 restoreUserAgent();
John Reck1cf4b792011-07-26 10:22:22 -07001769 String url = b.getString(CURRURL);
1770 String title = b.getString(CURRTITLE);
1771 boolean incognito = b.getBoolean(INCOGNITO);
1772 mCurrentState = new PageState(mContext, incognito, url, null);
1773 mCurrentState.mTitle = title;
1774 synchronized (Tab.this) {
1775 if (mCapture != null) {
John Reck4eadc342011-10-31 14:04:10 -07001776 DataController.getInstance(mContext).loadThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07001777 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001778 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001779 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04001780
John Reck8b9bb8b2012-03-08 13:19:40 -08001781 private void restoreUserAgent() {
1782 if (mMainView == null || mSavedState == null) {
1783 return;
1784 }
1785 if (mSavedState.getBoolean(USERAGENT)
1786 != mSettings.hasDesktopUseragent(mMainView)) {
1787 mSettings.toggleDesktopUseragent(mMainView);
1788 }
1789 }
1790
Leon Scroggins1961ed22010-12-07 15:22:21 -05001791 public void updateBookmarkedStatus() {
John Recke969cc52010-12-21 17:24:43 -08001792 mDataController.queryBookmarkStatus(getUrl(), mIsBookmarkCallback);
Leon Scroggins1961ed22010-12-07 15:22:21 -05001793 }
1794
John Recke969cc52010-12-21 17:24:43 -08001795 private DataController.OnQueryUrlIsBookmark mIsBookmarkCallback
1796 = new DataController.OnQueryUrlIsBookmark() {
1797 @Override
1798 public void onQueryUrlIsBookmark(String url, boolean isBookmark) {
1799 if (mCurrentState.mUrl.equals(url)) {
1800 mCurrentState.mIsBookmarkedSite = isBookmark;
1801 mWebViewController.bookmarkedStatusHasChanged(Tab.this);
1802 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001803 }
John Recke969cc52010-12-21 17:24:43 -08001804 };
Michael Kolb1acef692011-03-08 14:12:06 -08001805
Michael Kolbeb95db42011-03-03 10:38:40 -08001806 public Bitmap getScreenshot() {
John Reck1cf4b792011-07-26 10:22:22 -07001807 synchronized (Tab.this) {
1808 return mCapture;
1809 }
Michael Kolbeb95db42011-03-03 10:38:40 -08001810 }
1811
John Reck541f55a2011-06-07 16:34:43 -07001812 public boolean isSnapshot() {
John Reck541f55a2011-06-07 16:34:43 -07001813 return false;
1814 }
1815
John Reck68234a92012-04-19 15:27:12 -07001816 private static class SaveCallback implements ValueCallback<Boolean> {
1817 boolean mResult;
1818
1819 @Override
1820 public void onReceiveValue(Boolean value) {
1821 mResult = value;
1822 synchronized (this) {
1823 notifyAll();
John Reck8cc92352011-07-06 17:41:52 -07001824 }
John Reck541f55a2011-06-07 16:34:43 -07001825 }
John Reck68234a92012-04-19 15:27:12 -07001826
1827 }
1828
1829 /**
1830 * Must be called on the UI thread
1831 */
1832 public ContentValues createSnapshotValues() {
1833 WebViewClassic web = getWebViewClassic();
1834 if (web == null) return null;
John Reckd8c74522011-06-14 08:45:00 -07001835 ContentValues values = new ContentValues();
1836 values.put(Snapshots.TITLE, mCurrentState.mTitle);
1837 values.put(Snapshots.URL, mCurrentState.mUrl);
John Reck68234a92012-04-19 15:27:12 -07001838 values.put(Snapshots.BACKGROUND, web.getPageBackgroundColor());
John Reck8cc92352011-07-06 17:41:52 -07001839 values.put(Snapshots.DATE_CREATED, System.currentTimeMillis());
1840 values.put(Snapshots.FAVICON, compressBitmap(getFavicon()));
1841 Bitmap screenshot = Controller.createScreenshot(mMainView,
1842 Controller.getDesiredThumbnailWidth(mContext),
1843 Controller.getDesiredThumbnailHeight(mContext));
1844 values.put(Snapshots.THUMBNAIL, compressBitmap(screenshot));
John Reckd8c74522011-06-14 08:45:00 -07001845 return values;
John Reck541f55a2011-06-07 16:34:43 -07001846 }
1847
John Reck68234a92012-04-19 15:27:12 -07001848 /**
1849 * Probably want to call this on a background thread
1850 */
1851 public boolean saveViewState(ContentValues values) {
1852 WebViewClassic web = getWebViewClassic();
1853 if (web == null) return false;
1854 String path = UUID.randomUUID().toString();
1855 SaveCallback callback = new SaveCallback();
1856 OutputStream outs = null;
1857 try {
1858 outs = mContext.openFileOutput(path, Context.MODE_PRIVATE);
1859 GZIPOutputStream stream = new GZIPOutputStream(outs);
1860 synchronized (callback) {
1861 web.saveViewState(stream, callback);
1862 callback.wait();
1863 }
1864 stream.flush();
1865 stream.close();
1866 } catch (Exception e) {
1867 Log.w(LOGTAG, "Failed to save view state", e);
1868 if (outs != null) {
1869 try {
1870 outs.close();
1871 } catch (IOException ignore) {}
1872 }
1873 File file = mContext.getFileStreamPath(path);
1874 if (file.exists() && !file.delete()) {
1875 file.deleteOnExit();
1876 }
1877 return false;
1878 }
1879 File savedFile = mContext.getFileStreamPath(path);
1880 if (!callback.mResult) {
1881 if (!savedFile.delete()) {
1882 savedFile.deleteOnExit();
1883 }
1884 return false;
1885 }
1886 long size = savedFile.length();
1887 values.put(Snapshots.VIEWSTATE_PATH, path);
1888 values.put(Snapshots.VIEWSTATE_SIZE, size);
1889 return true;
1890 }
1891
John Reck8cc92352011-07-06 17:41:52 -07001892 public byte[] compressBitmap(Bitmap bitmap) {
1893 if (bitmap == null) {
1894 return null;
1895 }
1896 ByteArrayOutputStream stream = new ByteArrayOutputStream();
1897 bitmap.compress(CompressFormat.PNG, 100, stream);
1898 return stream.toByteArray();
1899 }
1900
John Reck26b18322011-06-21 13:08:58 -07001901 public void loadUrl(String url, Map<String, String> headers) {
1902 if (mMainView != null) {
Michael Kolba53c9892011-10-05 13:31:40 -07001903 mPageLoadProgress = INITIAL_PROGRESS;
1904 mInPageLoad = true;
Michael Kolb14612442011-06-24 13:06:29 -07001905 mCurrentState = new PageState(mContext, false, url, null);
John Reck26b18322011-06-21 13:08:58 -07001906 mWebViewController.onPageStarted(this, mMainView, null);
1907 mMainView.loadUrl(url, headers);
1908 }
1909 }
1910
John Reck38b39652012-06-05 09:22:59 -07001911 public void disableUrlOverridingForLoad() {
1912 mDisableOverrideUrlLoading = true;
1913 }
1914
Michael Kolb9ef259a2011-07-12 15:33:08 -07001915 protected void capture() {
1916 if (mMainView == null || mCapture == null) return;
John Reck4eadc342011-10-31 14:04:10 -07001917 if (mMainView.getContentWidth() <= 0 || mMainView.getContentHeight() <= 0) {
1918 return;
1919 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001920 Canvas c = new Canvas(mCapture);
1921 final int left = mMainView.getScrollX();
1922 final int top = mMainView.getScrollY() + mMainView.getVisibleTitleHeight();
Michael Kolba3194d02011-09-07 11:23:51 -07001923 int state = c.save();
Michael Kolb9ef259a2011-07-12 15:33:08 -07001924 c.translate(-left, -top);
1925 float scale = mCaptureWidth / (float) mMainView.getWidth();
1926 c.scale(scale, scale, left, top);
John Reck8ee633f2011-08-09 16:00:35 -07001927 if (mMainView instanceof BrowserWebView) {
1928 ((BrowserWebView)mMainView).drawContent(c);
1929 } else {
1930 mMainView.draw(c);
1931 }
Michael Kolba3194d02011-09-07 11:23:51 -07001932 c.restoreToCount(state);
1933 // manually anti-alias the edges for the tilt
1934 c.drawRect(0, 0, 1, mCapture.getHeight(), sAlphaPaint);
1935 c.drawRect(mCapture.getWidth() - 1, 0, mCapture.getWidth(),
1936 mCapture.getHeight(), sAlphaPaint);
1937 c.drawRect(0, 0, mCapture.getWidth(), 1, sAlphaPaint);
1938 c.drawRect(0, mCapture.getHeight() - 1, mCapture.getWidth(),
1939 mCapture.getHeight(), sAlphaPaint);
Dianne Hackborn43cfe8a2011-08-02 16:59:35 -07001940 c.setBitmap(null);
John Reck8ee633f2011-08-09 16:00:35 -07001941 mHandler.removeMessages(MSG_CAPTURE);
John Reck1cf4b792011-07-26 10:22:22 -07001942 persistThumbnail();
John Reck8ee633f2011-08-09 16:00:35 -07001943 TabControl tc = mWebViewController.getTabControl();
1944 if (tc != null) {
1945 OnThumbnailUpdatedListener updateListener
1946 = tc.getOnThumbnailUpdatedListener();
1947 if (updateListener != null) {
1948 updateListener.onThumbnailUpdated(this);
1949 }
1950 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001951 }
1952
1953 @Override
1954 public void onNewPicture(WebView view, Picture picture) {
John Reck1cf4b792011-07-26 10:22:22 -07001955 postCapture();
1956 }
1957
1958 private void postCapture() {
Michael Kolb9ef259a2011-07-12 15:33:08 -07001959 if (!mHandler.hasMessages(MSG_CAPTURE)) {
1960 mHandler.sendEmptyMessageDelayed(MSG_CAPTURE, CAPTURE_DELAY);
1961 }
1962 }
1963
John Reckef654f12011-07-12 16:42:08 -07001964 public boolean canGoBack() {
1965 return mMainView != null ? mMainView.canGoBack() : false;
1966 }
1967
1968 public boolean canGoForward() {
1969 return mMainView != null ? mMainView.canGoForward() : false;
1970 }
1971
1972 public void goBack() {
1973 if (mMainView != null) {
1974 mMainView.goBack();
1975 }
1976 }
1977
1978 public void goForward() {
1979 if (mMainView != null) {
1980 mMainView.goForward();
1981 }
1982 }
1983
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001984 /**
1985 * Causes the tab back/forward stack to be cleared once, if the given URL is the next URL
1986 * to be added to the stack.
1987 *
1988 * This is used to ensure that preloaded URLs that are not subsequently seen by the user do
1989 * not appear in the back stack.
1990 */
Mathew Inwooda829d552011-09-02 14:16:25 +01001991 public void clearBackStackWhenItemAdded(Pattern urlPattern) {
1992 mClearHistoryUrlPattern = urlPattern;
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001993 }
1994
John Reck1cf4b792011-07-26 10:22:22 -07001995 protected void persistThumbnail() {
John Reck4eadc342011-10-31 14:04:10 -07001996 DataController.getInstance(mContext).saveThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07001997 }
1998
1999 protected void deleteThumbnail() {
John Reck4eadc342011-10-31 14:04:10 -07002000 DataController.getInstance(mContext).deleteThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07002001 }
2002
John Reck4eadc342011-10-31 14:04:10 -07002003 void updateCaptureFromBlob(byte[] blob) {
John Reck1cf4b792011-07-26 10:22:22 -07002004 synchronized (Tab.this) {
2005 if (mCapture == null) {
2006 return;
2007 }
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002008 ByteBuffer buffer = ByteBuffer.wrap(blob);
Michael Kolbd837a112011-08-09 14:00:18 -07002009 try {
2010 mCapture.copyPixelsFromBuffer(buffer);
2011 } catch (RuntimeException rex) {
2012 Log.e(LOGTAG, "Load capture has mismatched sizes; buffer: "
2013 + buffer.capacity() + " blob: " + blob.length
2014 + "capture: " + mCapture.getByteCount());
2015 throw rex;
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002016 }
John Reck1cf4b792011-07-26 10:22:22 -07002017 }
2018 }
2019
John Reck52be4782011-08-26 15:37:29 -07002020 @Override
2021 public String toString() {
2022 StringBuilder builder = new StringBuilder(100);
2023 builder.append(mId);
2024 builder.append(") has parent: ");
2025 if (getParent() != null) {
2026 builder.append("true[");
2027 builder.append(getParent().getId());
2028 builder.append("]");
2029 } else {
2030 builder.append("false");
2031 }
2032 builder.append(", incog: ");
2033 builder.append(isPrivateBrowsingEnabled());
2034 if (!isPrivateBrowsingEnabled()) {
2035 builder.append(", title: ");
2036 builder.append(getTitle());
2037 builder.append(", url: ");
2038 builder.append(getUrl());
2039 }
2040 return builder.toString();
2041 }
2042
Steve Block4895b012011-10-03 16:26:46 +01002043 private void handleProceededAfterSslError(SslError error) {
2044 if (error.getUrl().equals(mCurrentState.mUrl)) {
2045 // The security state should currently be SECURITY_STATE_SECURE.
2046 setSecurityState(SecurityState.SECURITY_STATE_BAD_CERTIFICATE);
Steve Block08a6f0c2011-10-06 12:12:53 +01002047 mCurrentState.mSslCertificateError = error;
Steve Block4895b012011-10-03 16:26:46 +01002048 } else if (getSecurityState() == SecurityState.SECURITY_STATE_SECURE) {
Steve Block08a6f0c2011-10-06 12:12:53 +01002049 // The page's main resource is secure and this error is for a
2050 // sub-resource.
Steve Block4895b012011-10-03 16:26:46 +01002051 setSecurityState(SecurityState.SECURITY_STATE_MIXED);
2052 }
2053 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002054}