blob: 0a2320550207bd2d929c9bcf60892e89f2b6d204 [file] [log] [blame]
Grace Kloba22ac16e2009-10-07 18:00:23 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Bijan Amirzada41242f22014-03-21 12:12:18 -070017package com.android.browser;
Grace Kloba22ac16e2009-10-07 18:00:23 -070018
Michael Kolb8233fac2010-10-26 16:08:53 -070019import android.app.Activity;
Grace Kloba22ac16e2009-10-07 18:00:23 -070020import android.app.AlertDialog;
21import android.content.ContentResolver;
John Reckd8c74522011-06-14 08:45:00 -070022import android.content.ContentValues;
John Reck30c714c2010-12-16 17:30:34 -080023import android.content.Context;
Grace Kloba22ac16e2009-10-07 18:00:23 -070024import android.content.DialogInterface;
Michael Kolbfe251992010-07-08 15:41:55 -070025import android.content.DialogInterface.OnCancelListener;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080026import android.content.res.Resources;
Grace Kloba22ac16e2009-10-07 18:00:23 -070027import android.graphics.Bitmap;
John Reck8cc92352011-07-06 17:41:52 -070028import android.graphics.Bitmap.CompressFormat;
Michael Kolb9ef259a2011-07-12 15:33:08 -070029import android.graphics.BitmapFactory;
30import android.graphics.Canvas;
Michael Kolbc3af0672011-08-09 10:24:41 -070031import android.graphics.Color;
Michael Kolba3194d02011-09-07 11:23:51 -070032import android.graphics.Paint;
Michael Kolb9ef259a2011-07-12 15:33:08 -070033import android.graphics.Picture;
Michael Kolba3194d02011-09-07 11:23:51 -070034import android.graphics.PorterDuff;
35import android.graphics.PorterDuffXfermode;
Tarun Nainaniea28dde2014-08-27 17:25:09 -070036import android.graphics.Rect;
Grace Kloba22ac16e2009-10-07 18:00:23 -070037import android.net.Uri;
38import android.net.http.SslError;
Grace Kloba22ac16e2009-10-07 18:00:23 -070039import android.os.Bundle;
Michael Kolb9ef259a2011-07-12 15:33:08 -070040import android.os.Handler;
Grace Kloba22ac16e2009-10-07 18:00:23 -070041import android.os.Message;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +000042import android.os.SystemClock;
Brian Carlstrom8862c1d2011-06-02 01:05:55 -070043import android.security.KeyChain;
Brian Carlstromaa09cd82011-06-09 16:04:40 -070044import android.security.KeyChainAliasCallback;
John Reck24f18262011-06-17 14:47:20 -070045import android.text.TextUtils;
Grace Kloba22ac16e2009-10-07 18:00:23 -070046import android.util.Log;
47import android.view.KeyEvent;
48import android.view.LayoutInflater;
49import android.view.View;
Grace Kloba50c241e2010-04-20 11:07:50 -070050import android.view.ViewStub;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080051import android.view.View.OnClickListener;
Ben Murdochc42addf2010-01-28 15:19:59 +000052import android.webkit.ConsoleMessage;
Grace Kloba22ac16e2009-10-07 18:00:23 -070053import android.webkit.GeolocationPermissions;
Grace Kloba22ac16e2009-10-07 18:00:23 -070054import android.webkit.URLUtil;
John Reck438bf462011-01-12 18:11:46 -080055import android.webkit.WebResourceResponse;
Grace Kloba22ac16e2009-10-07 18:00:23 -070056import android.webkit.WebStorage;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080057import android.webkit.WebChromeClient.CustomViewCallback;
58import android.webkit.ValueCallback;
Ben Murdoch1d676b62011-01-17 12:54:24 +000059import android.widget.CheckBox;
Ben Murdoch8029a772010-11-16 11:58:21 +000060import android.widget.Toast;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080061import android.widget.FrameLayout;
62import android.widget.Button;
Grace Kloba22ac16e2009-10-07 18:00:23 -070063
Bijan Amirzada41242f22014-03-21 12:12:18 -070064import com.android.browser.TabControl.OnThumbnailUpdatedListener;
65import com.android.browser.homepages.HomeProvider;
66import com.android.browser.mynavigation.MyNavigationUtil;
67import com.android.browser.provider.MyNavigationProvider;
68import com.android.browser.provider.SnapshotProvider.Snapshots;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080069
70import org.codeaurora.swe.BrowserDownloadListener;
71import org.codeaurora.swe.ClientCertRequestHandler;
72import org.codeaurora.swe.HttpAuthHandler;
73import org.codeaurora.swe.SslErrorHandler;
74import org.codeaurora.swe.WebBackForwardList;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080075import org.codeaurora.swe.WebChromeClient;
76import org.codeaurora.swe.WebHistoryItem;
77import org.codeaurora.swe.WebView;
78import org.codeaurora.swe.WebView.PictureListener;
Pankaj Garg1c7380d2014-08-27 14:17:12 -070079import org.codeaurora.swe.WebView.CreateWindowParams;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080080import org.codeaurora.swe.WebViewClient;
81
John Reck541f55a2011-06-07 16:34:43 -070082import java.io.ByteArrayOutputStream;
John Reck2b71d6d2012-04-18 17:42:06 -070083import java.io.File;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080084import java.io.InputStream;
John Reck1cf4b792011-07-26 10:22:22 -070085import java.nio.ByteBuffer;
Michael Kolbfe251992010-07-08 15:41:55 -070086import java.util.LinkedList;
87import java.util.Map;
John Reck2b71d6d2012-04-18 17:42:06 -070088import java.util.UUID;
Michael Kolbfe251992010-07-08 15:41:55 -070089import java.util.Vector;
Mathew Inwooda829d552011-09-02 14:16:25 +010090import java.util.regex.Pattern;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080091import java.sql.Timestamp;
92import java.util.Date;
Michael Kolbfe251992010-07-08 15:41:55 -070093
Grace Kloba22ac16e2009-10-07 18:00:23 -070094/**
95 * Class for maintaining Tabs with a main WebView and a subwindow.
96 */
Michael Kolb9ef259a2011-07-12 15:33:08 -070097class Tab implements PictureListener {
Michael Kolb8233fac2010-10-26 16:08:53 -070098
Grace Kloba22ac16e2009-10-07 18:00:23 -070099 // Log Tag
100 private static final String LOGTAG = "Tab";
Bijan Amirzada41242f22014-03-21 12:12:18 -0700101 private static final boolean LOGD_ENABLED = com.android.browser.Browser.LOGD_ENABLED;
Ben Murdochc42addf2010-01-28 15:19:59 +0000102 // Special case the logtag for messages for the Console to make it easier to
103 // filter them and match the logtag used for these messages in older versions
104 // of the browser.
105 private static final String CONSOLE_LOGTAG = "browser";
106
Michael Kolb9ef259a2011-07-12 15:33:08 -0700107 private static final int MSG_CAPTURE = 42;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800108 private static final int CAPTURE_DELAY = 1000;
Michael Kolba53c9892011-10-05 13:31:40 -0700109 private static final int INITIAL_PROGRESS = 5;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700110
John Reck1cf4b792011-07-26 10:22:22 -0700111 private static Bitmap sDefaultFavicon;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800112 protected boolean hasCrashed = false;
John Reck1cf4b792011-07-26 10:22:22 -0700113
Michael Kolba3194d02011-09-07 11:23:51 -0700114 private static Paint sAlphaPaint = new Paint();
115 static {
116 sAlphaPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
117 sAlphaPaint.setColor(Color.TRANSPARENT);
118 }
119
Steve Block2466eff2011-10-03 15:33:09 +0100120 public enum SecurityState {
Steve Block4895b012011-10-03 16:26:46 +0100121 // The page's main resource does not use SSL. Note that we use this
122 // state irrespective of the SSL authentication state of sub-resources.
Steve Block2466eff2011-10-03 15:33:09 +0100123 SECURITY_STATE_NOT_SECURE,
Steve Block4895b012011-10-03 16:26:46 +0100124 // The page's main resource uses SSL and the certificate is good. The
125 // same is true of all sub-resources.
Steve Block2466eff2011-10-03 15:33:09 +0100126 SECURITY_STATE_SECURE,
Steve Block4895b012011-10-03 16:26:46 +0100127 // The page's main resource uses SSL and the certificate is good, but
128 // some sub-resources either do not use SSL or have problems with their
129 // certificates.
Steve Block2466eff2011-10-03 15:33:09 +0100130 SECURITY_STATE_MIXED,
Steve Block4895b012011-10-03 16:26:46 +0100131 // The page's main resource uses SSL but there is a problem with its
132 // certificate.
133 SECURITY_STATE_BAD_CERTIFICATE,
John Reck30c714c2010-12-16 17:30:34 -0800134 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700135
Michael Kolb14612442011-06-24 13:06:29 -0700136 Context mContext;
John Reckd8c74522011-06-14 08:45:00 -0700137 protected WebViewController mWebViewController;
Michael Kolb8233fac2010-10-26 16:08:53 -0700138
Michael Kolbc831b632011-05-11 09:30:34 -0700139 // The tab ID
John Reckd8c74522011-06-14 08:45:00 -0700140 private long mId = -1;
Michael Kolbc831b632011-05-11 09:30:34 -0700141
Grace Kloba22ac16e2009-10-07 18:00:23 -0700142 // The Geolocation permissions prompt
143 private GeolocationPermissionsPrompt mGeolocationPermissionsPrompt;
144 // Main WebView wrapper
Michael Kolba713ec82010-11-29 17:27:06 -0800145 private View mContainer;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700146 // Main WebView
147 private WebView mMainView;
148 // Subwindow container
149 private View mSubViewContainer;
150 // Subwindow WebView
151 private WebView mSubView;
152 // Saved bundle for when we are running low on memory. It contains the
153 // information needed to restore the WebView if the user goes back to the
154 // tab.
155 private Bundle mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700156 // Parent Tab. This is the Tab that created this Tab, or null if the Tab was
157 // created by the UI
Michael Kolbc831b632011-05-11 09:30:34 -0700158 private Tab mParent;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700159 // Tab that constructed by this Tab. This is used when this Tab is
160 // destroyed, it clears all mParentTab values in the children.
Michael Kolbc831b632011-05-11 09:30:34 -0700161 private Vector<Tab> mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700162 // If true, the tab is in the foreground of the current activity.
163 private boolean mInForeground;
Michael Kolb8233fac2010-10-26 16:08:53 -0700164 // If true, the tab is in page loading state (after onPageStarted,
165 // before onPageFinsihed)
166 private boolean mInPageLoad;
Tarun Nainani8eb00912014-07-17 12:28:32 -0700167 private boolean mPageFinished;
John Reck38b39652012-06-05 09:22:59 -0700168 private boolean mDisableOverrideUrlLoading;
John Reck30c714c2010-12-16 17:30:34 -0800169 // The last reported progress of the current page
170 private int mPageLoadProgress;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000171 // The time the load started, used to find load page time
172 private long mLoadStartTime;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700173 // Application identifier used to find tabs that another application wants
174 // to reuse.
175 private String mAppId;
Michael Kolbe28b3472011-08-04 16:54:31 -0700176 // flag to indicate if tab should be closed on back
177 private boolean mCloseOnBack;
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700178 // flag to indicate if the tab was opened from an intent
179 private boolean mDerivedFromIntent = false;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -0500180 // The listener that gets invoked when a download is started from the
181 // mMainView
Selim Gurun0b3d66f2012-08-29 13:08:13 -0700182 private final BrowserDownloadListener mDownloadListener;
John Recke969cc52010-12-21 17:24:43 -0800183 private DataController mDataController;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700184
185 // AsyncTask for downloading touch icons
186 DownloadTouchIcon mTouchIconLoader;
187
John Reck35e9dd62011-04-25 09:01:54 -0700188 private BrowserSettings mSettings;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700189 private int mCaptureWidth;
190 private int mCaptureHeight;
191 private Bitmap mCapture;
192 private Handler mHandler;
Michael Kolb72864272012-05-03 15:42:15 -0700193 private boolean mUpdateThumbnail;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800194 private Timestamp timestamp;
Vivek Sekharf96064b2014-07-28 16:32:34 -0700195 private boolean mFullScreen = false;
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800196 private boolean mReceivedError;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700197
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(
Enrico Rosd6efa972014-12-02 19:49:59 -0800202 context.getResources(), R.drawable.ic_deco_favicon_normal);
John Reck1cf4b792011-07-26 10:22:22 -0700203 }
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) {
Vivek Sekhared791da2015-02-22 12:39:05 -0800222 mOriginalUrl = mUrl = "chrome://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
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800325 protected void replaceCrashView(View view, View container) {
326 if (hasCrashed && (view == mMainView)) {
327 final FrameLayout wrapper = (FrameLayout) container.findViewById(R.id.webview_wrapper);
328 wrapper.removeAllViewsInLayout();
329 wrapper.addView(view);
330 hasCrashed = false;
331 }
Vivek Sekhar2868b8d2014-12-03 17:22:50 -0800332 mMainView.reload();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800333 }
334
335 protected void showCrashView() {
336 if (hasCrashed) {
337 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
338 Context.LAYOUT_INFLATER_SERVICE);
339 final View crashLayout = inflater.inflate(R.layout.browser_tab_crash, null);
340 final FrameLayout wrapper =
341 (FrameLayout) mContainer.findViewById(R.id.webview_wrapper);
342 wrapper.removeAllViewsInLayout();
343 wrapper.addView(crashLayout);
344 mContainer.requestFocus();
345 Button reloadBtn = (Button) crashLayout.findViewById(R.id.browser_crash_reload_btn);
346 reloadBtn.setOnClickListener(new OnClickListener() {
347 @Override
348 public void onClick(View arg0) {
349 replaceCrashView(mMainView, mContainer);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800350 }
351 });
352 }
353 }
354
Grace Kloba22ac16e2009-10-07 18:00:23 -0700355 // -------------------------------------------------------------------------
356 // WebViewClient implementation for the main WebView
357 // -------------------------------------------------------------------------
358
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800359 private final WebViewClient mWebViewClient = new WebViewClient() {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500360 private Message mDontResend;
361 private Message mResend;
Michael Kolb47bd1e42011-09-01 15:25:00 -0700362
363 private boolean providersDiffer(String url, String otherUrl) {
364 Uri uri1 = Uri.parse(url);
365 Uri uri2 = Uri.parse(otherUrl);
366 return !uri1.getEncodedAuthority().equals(uri2.getEncodedAuthority());
367 }
368
Grace Kloba22ac16e2009-10-07 18:00:23 -0700369 @Override
370 public void onPageStarted(WebView view, String url, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700371 mInPageLoad = true;
Tarun Nainani8eb00912014-07-17 12:28:32 -0700372 mPageFinished = false;
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800373 mReceivedError = false;
Michael Kolb72864272012-05-03 15:42:15 -0700374 mUpdateThumbnail = true;
Michael Kolba53c9892011-10-05 13:31:40 -0700375 mPageLoadProgress = INITIAL_PROGRESS;
Michael Kolb14612442011-06-24 13:06:29 -0700376 mCurrentState = new PageState(mContext,
John Reck30c714c2010-12-16 17:30:34 -0800377 view.isPrivateBrowsingEnabled(), url, favicon);
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000378 mLoadStartTime = SystemClock.uptimeMillis();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700379
380 // If we start a touch icon load and then load a new page, we don't
381 // want to cancel the current touch icon loader. But, we do want to
382 // create a new one when the touch icon url is known.
383 if (mTouchIconLoader != null) {
384 mTouchIconLoader.mTab = null;
385 mTouchIconLoader = null;
386 }
387
Panos Thomasb298aad2014-10-22 12:24:21 -0700388 // Loading a new page voids any ongoing Geolocation permission
389 // requests.
390 if (mGeolocationPermissionsPrompt != null) {
391 mGeolocationPermissionsPrompt.dismiss();
392 }
393
Grace Kloba22ac16e2009-10-07 18:00:23 -0700394 // finally update the UI in the activity if it is in the foreground
John Reck324d4402011-01-11 16:56:42 -0800395 mWebViewController.onPageStarted(Tab.this, view, favicon);
Leon Scroggins4cd97792010-12-03 15:31:56 -0500396
John Recke969cc52010-12-21 17:24:43 -0800397 updateBookmarkedStatus();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700398 }
399
400 @Override
401 public void onPageFinished(WebView view, String url) {
John Reck38b39652012-06-05 09:22:59 -0700402 mDisableOverrideUrlLoading = false;
John Reck5b691842010-11-29 11:21:13 -0800403 if (!isPrivateBrowsingEnabled()) {
404 LogTag.logPageFinishedLoading(
405 url, SystemClock.uptimeMillis() - mLoadStartTime);
406 }
John Reck1cf4b792011-07-26 10:22:22 -0700407 syncCurrentState(view, url);
John Reck324d4402011-01-11 16:56:42 -0800408 mWebViewController.onPageFinished(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700409 }
410
411 // return true if want to hijack the url to let another app to handle it
412 @Override
413 public boolean shouldOverrideUrlLoading(WebView view, String url) {
John Reck38b39652012-06-05 09:22:59 -0700414 if (!mDisableOverrideUrlLoading && mInForeground) {
Michael Kolb18eb3772010-12-10 14:29:51 -0800415 return mWebViewController.shouldOverrideUrlLoading(Tab.this,
416 view, url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700417 } else {
418 return false;
419 }
420 }
421
Vivek Sekharb991edb2014-12-17 18:18:07 -0800422 @Override
423 public boolean shouldDownloadFavicon(WebView view, String url) {
424 return true;
425 }
426
Grace Kloba22ac16e2009-10-07 18:00:23 -0700427 /**
Steve Block2466eff2011-10-03 15:33:09 +0100428 * Updates the security state. This method is called when we discover
429 * another resource to be loaded for this page (for example,
430 * javascript). While we update the security state, we do not update
431 * the lock icon until we are done loading, as it is slightly more
432 * secure this way.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700433 */
434 @Override
435 public void onLoadResource(WebView view, String url) {
436 if (url != null && url.length() > 0) {
437 // It is only if the page claims to be secure that we may have
Steve Block2466eff2011-10-03 15:33:09 +0100438 // to update the security state:
439 if (mCurrentState.mSecurityState == SecurityState.SECURITY_STATE_SECURE) {
440 // If NOT a 'safe' url, change the state to mixed content!
Grace Kloba22ac16e2009-10-07 18:00:23 -0700441 if (!(URLUtil.isHttpsUrl(url) || URLUtil.isDataUrl(url)
442 || URLUtil.isAboutUrl(url))) {
Steve Block2466eff2011-10-03 15:33:09 +0100443 mCurrentState.mSecurityState = SecurityState.SECURITY_STATE_MIXED;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700444 }
445 }
446 }
447 }
448
449 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700450 * Show a dialog informing the user of the network error reported by
451 * WebCore if it is in the foreground.
452 */
453 @Override
454 public void onReceivedError(WebView view, int errorCode,
455 String description, String failingUrl) {
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800456 // Used for the syncCurrentState to use
457 // the failing url instead of using webview url
458 mReceivedError = true;
459
Grace Kloba22ac16e2009-10-07 18:00:23 -0700460 if (errorCode != WebViewClient.ERROR_HOST_LOOKUP &&
461 errorCode != WebViewClient.ERROR_CONNECT &&
462 errorCode != WebViewClient.ERROR_BAD_URL &&
463 errorCode != WebViewClient.ERROR_UNSUPPORTED_SCHEME &&
464 errorCode != WebViewClient.ERROR_FILE) {
465 queueError(errorCode, description);
Jeff Hamilton47654f42010-09-07 09:57:51 -0500466
Selim Gurun3da06b82011-10-10 13:58:12 -0700467 // Don't log URLs when in private browsing mode
468 if (!isPrivateBrowsingEnabled()) {
469 Log.e(LOGTAG, "onReceivedError " + errorCode + " " + failingUrl
Jeff Hamilton47654f42010-09-07 09:57:51 -0500470 + " " + description);
Selim Gurun3da06b82011-10-10 13:58:12 -0700471 }
Jeff Hamilton47654f42010-09-07 09:57:51 -0500472 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700473 }
474
475 /**
476 * Check with the user if it is ok to resend POST data as the page they
477 * are trying to navigate to is the result of a POST.
478 */
479 @Override
480 public void onFormResubmission(WebView view, final Message dontResend,
481 final Message resend) {
482 if (!mInForeground) {
483 dontResend.sendToTarget();
484 return;
485 }
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500486 if (mDontResend != null) {
487 Log.w(LOGTAG, "onFormResubmission should not be called again "
488 + "while dialog is still up");
489 dontResend.sendToTarget();
490 return;
491 }
492 mDontResend = dontResend;
493 mResend = resend;
Michael Kolb14612442011-06-24 13:06:29 -0700494 new AlertDialog.Builder(mContext).setTitle(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700495 R.string.browserFrameFormResubmitLabel).setMessage(
496 R.string.browserFrameFormResubmitMessage)
497 .setPositiveButton(R.string.ok,
498 new DialogInterface.OnClickListener() {
499 public void onClick(DialogInterface dialog,
500 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500501 if (mResend != null) {
502 mResend.sendToTarget();
503 mResend = null;
504 mDontResend = null;
505 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700506 }
507 }).setNegativeButton(R.string.cancel,
508 new DialogInterface.OnClickListener() {
509 public void onClick(DialogInterface dialog,
510 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500511 if (mDontResend != null) {
512 mDontResend.sendToTarget();
513 mResend = null;
514 mDontResend = null;
515 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700516 }
517 }).setOnCancelListener(new OnCancelListener() {
518 public void onCancel(DialogInterface dialog) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500519 if (mDontResend != null) {
520 mDontResend.sendToTarget();
521 mResend = null;
522 mDontResend = null;
523 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700524 }
525 }).show();
526 }
527
528 /**
529 * Insert the url into the visited history database.
530 * @param url The url to be inserted.
531 * @param isReload True if this url is being reloaded.
532 * FIXME: Not sure what to do when reloading the page.
533 */
534 @Override
535 public void doUpdateVisitedHistory(WebView view, String url,
536 boolean isReload) {
John Reck324d4402011-01-11 16:56:42 -0800537 mWebViewController.doUpdateVisitedHistory(Tab.this, isReload);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700538 }
539
540 /**
541 * Displays SSL error(s) dialog to the user.
542 */
543 @Override
544 public void onReceivedSslError(final WebView view,
545 final SslErrorHandler handler, final SslError error) {
546 if (!mInForeground) {
547 handler.cancel();
Steve Block2466eff2011-10-03 15:33:09 +0100548 setSecurityState(SecurityState.SECURITY_STATE_NOT_SECURE);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700549 return;
550 }
John Reck35e9dd62011-04-25 09:01:54 -0700551 if (mSettings.showSecurityWarnings()) {
John Reckcb28b2c2011-08-26 17:39:44 -0700552 new AlertDialog.Builder(mContext)
553 .setTitle(R.string.security_warning)
554 .setMessage(R.string.ssl_warnings_header)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200555 .setIconAttribute(android.R.attr.alertDialogIcon)
John Reckcb28b2c2011-08-26 17:39:44 -0700556 .setPositiveButton(R.string.ssl_continue,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700557 new DialogInterface.OnClickListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700558 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700559 public void onClick(DialogInterface dialog,
560 int whichButton) {
561 handler.proceed();
Steve Block4895b012011-10-03 16:26:46 +0100562 handleProceededAfterSslError(error);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700563 }
John Reckcb28b2c2011-08-26 17:39:44 -0700564 })
565 .setNeutralButton(R.string.view_certificate,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700566 new DialogInterface.OnClickListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700567 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700568 public void onClick(DialogInterface dialog,
569 int whichButton) {
John Reckcb28b2c2011-08-26 17:39:44 -0700570 mWebViewController.showSslCertificateOnError(
571 view, handler, error);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700572 }
John Reckcb28b2c2011-08-26 17:39:44 -0700573 })
574 .setNegativeButton(R.string.ssl_go_back,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700575 new DialogInterface.OnClickListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700576 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700577 public void onClick(DialogInterface dialog,
578 int whichButton) {
John Reck30c714c2010-12-16 17:30:34 -0800579 dialog.cancel();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700580 }
John Reckcb28b2c2011-08-26 17:39:44 -0700581 })
582 .setOnCancelListener(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700583 new DialogInterface.OnCancelListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700584 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700585 public void onCancel(DialogInterface dialog) {
586 handler.cancel();
Steve Block2466eff2011-10-03 15:33:09 +0100587 setSecurityState(SecurityState.SECURITY_STATE_NOT_SECURE);
John Reck30c714c2010-12-16 17:30:34 -0800588 mWebViewController.onUserCanceledSsl(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700589 }
John Reckcb28b2c2011-08-26 17:39:44 -0700590 })
591 .show();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700592 } else {
593 handler.proceed();
594 }
595 }
596
597 /**
Steve Block4895b012011-10-03 16:26:46 +0100598 * Called when an SSL error occurred while loading a resource, but the
599 * WebView but chose to proceed anyway based on a decision retained
600 * from a previous response to onReceivedSslError(). We update our
601 * security state to reflect this.
602 */
603 @Override
604 public void onProceededAfterSslError(WebView view, SslError error) {
605 handleProceededAfterSslError(error);
606 }
607
608 /**
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700609 * Displays client certificate request to the user.
610 */
611 @Override
612 public void onReceivedClientCertRequest(final WebView view,
613 final ClientCertRequestHandler handler, final String host_and_port) {
614 if (!mInForeground) {
615 handler.ignore();
616 return;
617 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700618 int colon = host_and_port.lastIndexOf(':');
619 String host;
620 int port;
621 if (colon == -1) {
622 host = host_and_port;
623 port = -1;
624 } else {
625 String portString = host_and_port.substring(colon + 1);
626 try {
627 port = Integer.parseInt(portString);
628 host = host_and_port.substring(0, colon);
629 } catch (NumberFormatException e) {
630 host = host_and_port;
631 port = -1;
632 }
633 }
Michael Kolb14612442011-06-24 13:06:29 -0700634 KeyChain.choosePrivateKeyAlias(
635 mWebViewController.getActivity(), new KeyChainAliasCallback() {
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700636 @Override public void alias(String alias) {
637 if (alias == null) {
638 handler.cancel();
639 return;
640 }
Michael Kolb14612442011-06-24 13:06:29 -0700641 new KeyChainLookup(mContext, handler, alias).execute();
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700642 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700643 }, null, null, host, port, null);
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700644 }
645
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800646 @Override
647 public void onRendererCrash(WebView view, boolean crashedWhileOomProtected) {
648 Log.e(LOGTAG, "Tab Crashed");
Axesh R. Ajmera2e241242014-05-19 15:53:38 -0700649 if (mWebViewController.getTabControl().getCurrentTab() == Tab.this) {
650 hasCrashed = true;
651 showCrashView();
652 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800653 }
654
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700655 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700656 * Handles an HTTP authentication request.
657 *
658 * @param handler The authentication handler
659 * @param host The host
660 * @param realm The realm
661 */
662 @Override
663 public void onReceivedHttpAuthRequest(WebView view,
664 final HttpAuthHandler handler, final String host,
665 final String realm) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700666 mWebViewController.onReceivedHttpAuthRequest(Tab.this, view, handler, host, realm);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700667 }
668
669 @Override
John Reck438bf462011-01-12 18:11:46 -0800670 public WebResourceResponse shouldInterceptRequest(WebView view,
671 String url) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800672 //intercept if opening a new incognito tab - show the incognito welcome page
Vivek Sekhared791da2015-02-22 12:39:05 -0800673 if (url.startsWith("chrome://incognito")) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800674 Resources resourceHandle = mContext.getResources();
675 InputStream inStream = resourceHandle.openRawResource(
Bijan Amirzada41242f22014-03-21 12:12:18 -0700676 com.android.browser.R.raw.incognito_mode_start_page);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800677 return new WebResourceResponse("text/html", "utf8", inStream);
678 }
kaiyiz6e5b3e02013-08-19 20:02:01 +0800679 WebResourceResponse res;
680 if (MyNavigationUtil.MY_NAVIGATION.equals(url)) {
681 res = MyNavigationProvider.shouldInterceptRequest(mContext, url);
682 } else {
683 res = HomeProvider.shouldInterceptRequest(mContext, url);
684 }
John Reck438bf462011-01-12 18:11:46 -0800685 return res;
686 }
687
688 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700689 public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
690 if (!mInForeground) {
691 return false;
692 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700693 return mWebViewController.shouldOverrideKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700694 }
695
696 @Override
697 public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700698 if (!mInForeground) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700699 return;
700 }
John Reck997b1b72012-04-19 18:08:25 -0700701 if (!mWebViewController.onUnhandledKeyEvent(event)) {
702 super.onUnhandledKeyEvent(view, event);
703 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700704 }
Vivek Sekhared791da2015-02-22 12:39:05 -0800705 //SWE-download-file
706 @Override
707 public void onDownloadStart(String url,
708 String userAgent,
709 String contentDisposition,
710 String mimeType,
711 long contentLength) {
712 if (mDownloadListener != null) {
713 if (mDownloadListener instanceof BrowserDownloadListener) {
714 ((BrowserDownloadListener) mDownloadListener).onDownloadStart(url,
715 userAgent, contentDisposition, mimeType, contentLength);
716 } else {
717 mDownloadListener.onDownloadStart(url, userAgent,
718 contentDisposition, mimeType, contentLength);
719 }
720 }
721 }
722 //SWE-download-file
Grace Kloba22ac16e2009-10-07 18:00:23 -0700723 };
724
John Reck1cf4b792011-07-26 10:22:22 -0700725 private void syncCurrentState(WebView view, String url) {
726 // Sync state (in case of stop/timeout)
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800727
728 if (mReceivedError) {
729 mCurrentState.mUrl = url;
730 mCurrentState.mOriginalUrl = url;
731 } else {
732 mCurrentState.mUrl = view.getUrl();
733 mCurrentState.mOriginalUrl = view.getOriginalUrl();
734 mCurrentState.mFavicon = view.getFavicon();
735 }
736
John Reck1cf4b792011-07-26 10:22:22 -0700737 if (mCurrentState.mUrl == null) {
John Reck8bcafc12011-08-29 16:43:02 -0700738 mCurrentState.mUrl = "";
John Reck1cf4b792011-07-26 10:22:22 -0700739 }
John Reck1cf4b792011-07-26 10:22:22 -0700740 mCurrentState.mTitle = view.getTitle();
Axesh R. Ajmera57120962014-11-13 13:37:24 -0800741
742
John Reck1cf4b792011-07-26 10:22:22 -0700743 if (!URLUtil.isHttpsUrl(mCurrentState.mUrl)) {
744 // In case we stop when loading an HTTPS page from an HTTP page
745 // but before a provisional load occurred
Steve Block2466eff2011-10-03 15:33:09 +0100746 mCurrentState.mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
Steve Block08a6f0c2011-10-06 12:12:53 +0100747 mCurrentState.mSslCertificateError = null;
John Reck1cf4b792011-07-26 10:22:22 -0700748 }
John Reck502a3532011-08-16 14:21:46 -0700749 mCurrentState.mIncognito = view.isPrivateBrowsingEnabled();
John Reck1cf4b792011-07-26 10:22:22 -0700750 }
751
Tarun Nainani8eb00912014-07-17 12:28:32 -0700752
753 public boolean isTabFullScreen() {
754 return mFullScreen;
755 }
756
Vivek Sekharf96064b2014-07-28 16:32:34 -0700757 protected void setTabFullscreen(boolean fullScreen) {
Tarun Nainani8eb00912014-07-17 12:28:32 -0700758 Controller controller = (Controller)mWebViewController;
Sudheer Koganti24766882014-10-02 10:58:09 -0700759 controller.getUi().showFullscreen(fullScreen);
Tarun Nainani8eb00912014-07-17 12:28:32 -0700760 mFullScreen = fullScreen;
Vivek Sekharf96064b2014-07-28 16:32:34 -0700761 }
762
Sudheer Koganti24766882014-10-02 10:58:09 -0700763 public boolean exitFullscreen() {
764 if (mFullScreen) {
765 Controller controller = (Controller)mWebViewController;
766 controller.getUi().showFullscreen(false);
767 if (getWebView() != null)
768 getWebView().exitFullscreen();
769 mFullScreen = false;
770 return true;
771 }
772 return false;
773 }
774
775
776
777
Grace Kloba22ac16e2009-10-07 18:00:23 -0700778 // -------------------------------------------------------------------------
779 // WebChromeClient implementation for the main WebView
780 // -------------------------------------------------------------------------
781
782 private final WebChromeClient mWebChromeClient = new WebChromeClient() {
783 // Helper method to create a new tab or sub window.
784 private void createWindow(final boolean dialog, final Message msg) {
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700785 this.createWindow(dialog, msg, null, false);
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700786 }
787
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700788 private void createWindow(final boolean dialog, final Message msg, final String url,
789 final boolean opener_suppressed) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700790 WebView.WebViewTransport transport =
791 (WebView.WebViewTransport) msg.obj;
792 if (dialog) {
793 createSubWindow();
Michael Kolb8233fac2010-10-26 16:08:53 -0700794 mWebViewController.attachSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700795 transport.setWebView(mSubView);
796 } else {
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700797 final Tab newTab = mWebViewController.openTab(url,
John Reck5949c662011-05-27 09:52:29 -0700798 Tab.this, true, true);
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700799 // This is special case for rendering links on a webpage in
800 // a new tab. If opener is suppressed, the WebContents created
801 // by the content layer are not fully initialized. This check
802 // will prevent content layer from overriding WebContents
803 // created by new tab with the uninitialized instance.
804 if (!opener_suppressed) {
805 transport.setWebView(newTab.getWebView());
806 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700807 }
808 msg.sendToTarget();
809 }
810
811 @Override
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700812 public void toggleFullscreenModeForTab(boolean enterFullscreen) {
813 if (mWebViewController instanceof Controller) {
Vivek Sekharf96064b2014-07-28 16:32:34 -0700814 setTabFullscreen(enterFullscreen);
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700815 }
816 }
817
818 @Override
Tarun Nainani8eb00912014-07-17 12:28:32 -0700819 public void onOffsetsForFullscreenChanged(float topControlsOffsetYPix,
820 float contentOffsetYPix,
821 float overdrawBottomHeightPix) {
822 if (mWebViewController instanceof Controller) {
Vivek Sekhar8ee3abb2014-07-14 12:32:05 -0700823 Controller controller = (Controller)mWebViewController;
Tarun Nainani8eb00912014-07-17 12:28:32 -0700824 controller.getUi().translateTitleBar(topControlsOffsetYPix);
Vivek Sekhar8ee3abb2014-07-14 12:32:05 -0700825 }
826 }
827
828 @Override
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700829 public boolean isTabFullScreen() {
Vivek Sekharf96064b2014-07-28 16:32:34 -0700830 return mFullScreen;
Vivek Sekhar13ad9b92014-06-16 15:49:54 -0700831 }
832
833 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700834 public boolean onCreateWindow(WebView view, final boolean dialog,
835 final boolean userGesture, final Message resultMsg) {
836 // only allow new window or sub window for the foreground case
837 if (!mInForeground) {
838 return false;
839 }
840 // Short-circuit if we can't create any more tabs or sub windows.
841 if (dialog && mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -0700842 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700843 .setTitle(R.string.too_many_subwindows_dialog_title)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200844 .setIconAttribute(android.R.attr.alertDialogIcon)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700845 .setMessage(R.string.too_many_subwindows_dialog_message)
846 .setPositiveButton(R.string.ok, null)
847 .show();
848 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700849 } else if (!mWebViewController.getTabControl().canCreateNewTab()) {
Michael Kolb14612442011-06-24 13:06:29 -0700850 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700851 .setTitle(R.string.too_many_windows_dialog_title)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200852 .setIconAttribute(android.R.attr.alertDialogIcon)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700853 .setMessage(R.string.too_many_windows_dialog_message)
854 .setPositiveButton(R.string.ok, null)
855 .show();
856 return false;
857 }
858
859 // Short-circuit if this was a user gesture.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800860 if (userGesture || !mSettings.blockPopupWindows()) {
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700861 CreateWindowParams windowParams = view.getCreateWindowParams();
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700862 if (windowParams.mOpenerSuppressed) {
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700863 createWindow(dialog, resultMsg, windowParams.mURL, true);
864 // This is special case for rendering links on a webpage in
865 // a new tab. If opener is suppressed, the WebContents created
866 // by the content layer are not fully initialized. Returning false
867 // will prevent content layer from overriding WebContents
868 // created by new tab with the uninitialized instance.
869 return false;
Pankaj Garg1c7380d2014-08-27 14:17:12 -0700870 }
Pankaj Garg0c73c7c2014-09-23 15:07:22 -0700871
872 createWindow(dialog, resultMsg);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700873 return true;
874 }
875
876 // Allow the popup and create the appropriate window.
877 final AlertDialog.OnClickListener allowListener =
878 new AlertDialog.OnClickListener() {
879 public void onClick(DialogInterface d,
880 int which) {
881 createWindow(dialog, resultMsg);
882 }
883 };
884
885 // Block the popup by returning a null WebView.
886 final AlertDialog.OnClickListener blockListener =
887 new AlertDialog.OnClickListener() {
888 public void onClick(DialogInterface d, int which) {
889 resultMsg.sendToTarget();
890 }
891 };
892
893 // Build a confirmation dialog to display to the user.
894 final AlertDialog d =
Michael Kolb14612442011-06-24 13:06:29 -0700895 new AlertDialog.Builder(mContext)
Björn Lundén2aa8ba22012-05-31 23:05:56 +0200896 .setIconAttribute(android.R.attr.alertDialogIcon)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700897 .setMessage(R.string.popup_window_attempt)
898 .setPositiveButton(R.string.allow, allowListener)
899 .setNegativeButton(R.string.block, blockListener)
900 .setCancelable(false)
901 .create();
902
903 // Show the confirmation dialog.
904 d.show();
905 return true;
906 }
907
908 @Override
Patrick Scotteb5061b2009-11-18 15:00:30 -0500909 public void onRequestFocus(WebView view) {
910 if (!mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700911 mWebViewController.switchToTab(Tab.this);
Patrick Scotteb5061b2009-11-18 15:00:30 -0500912 }
913 }
914
915 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700916 public void onCloseWindow(WebView window) {
Michael Kolbc831b632011-05-11 09:30:34 -0700917 if (mParent != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700918 // JavaScript can only close popup window.
919 if (mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700920 mWebViewController.switchToTab(mParent);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700921 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700922 mWebViewController.closeTab(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700923 }
924 }
925
926 @Override
927 public void onProgressChanged(WebView view, int newProgress) {
John Reck30c714c2010-12-16 17:30:34 -0800928 mPageLoadProgress = newProgress;
Michael Kolbb1fb70c2011-11-21 12:38:14 -0800929 if (newProgress == 100) {
930 mInPageLoad = false;
931 }
John Reck30c714c2010-12-16 17:30:34 -0800932 mWebViewController.onProgressChanged(Tab.this);
Michael Kolb72864272012-05-03 15:42:15 -0700933 if (mUpdateThumbnail && newProgress == 100) {
934 mUpdateThumbnail = false;
935 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700936 }
937
938 @Override
Leon Scroggins21d9b902010-03-11 09:33:11 -0500939 public void onReceivedTitle(WebView view, final String title) {
John Reck30c714c2010-12-16 17:30:34 -0800940 mCurrentState.mTitle = title;
Michael Kolb8233fac2010-10-26 16:08:53 -0700941 mWebViewController.onReceivedTitle(Tab.this, title);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700942 }
943
944 @Override
945 public void onReceivedIcon(WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -0800946 mCurrentState.mFavicon = icon;
Michael Kolb8233fac2010-10-26 16:08:53 -0700947 mWebViewController.onFavicon(Tab.this, view, icon);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700948 }
949
950 @Override
951 public void onReceivedTouchIconUrl(WebView view, String url,
952 boolean precomposed) {
Michael Kolb14612442011-06-24 13:06:29 -0700953 final ContentResolver cr = mContext.getContentResolver();
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400954 // Let precomposed icons take precedence over non-composed
955 // icons.
956 if (precomposed && mTouchIconLoader != null) {
957 mTouchIconLoader.cancel(false);
958 mTouchIconLoader = null;
959 }
960 // Have only one async task at a time.
961 if (mTouchIconLoader == null) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700962 mTouchIconLoader = new DownloadTouchIcon(Tab.this,
Michael Kolb14612442011-06-24 13:06:29 -0700963 mContext, cr, view);
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400964 mTouchIconLoader.execute(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700965 }
966 }
967
968 @Override
969 public void onShowCustomView(View view,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800970 CustomViewCallback callback) {
Michael Kolb14612442011-06-24 13:06:29 -0700971 Activity activity = mWebViewController.getActivity();
972 if (activity != null) {
973 onShowCustomView(view, activity.getRequestedOrientation(), callback);
974 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400975 }
976
977 @Override
978 public void onShowCustomView(View view, int requestedOrientation,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800979 CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700980 if (mInForeground) mWebViewController.showCustomView(Tab.this, view,
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400981 requestedOrientation, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700982 }
983
984 @Override
985 public void onHideCustomView() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700986 if (mInForeground) mWebViewController.hideCustomView();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700987 }
988
989 /**
990 * The origin has exceeded its database quota.
991 * @param url the URL that exceeded the quota
992 * @param databaseIdentifier the identifier of the database on which the
993 * transaction that caused the quota overflow was run
994 * @param currentQuota the current quota for the origin.
995 * @param estimatedSize the estimated size of the database.
996 * @param totalUsedQuota is the sum of all origins' quota.
997 * @param quotaUpdater The callback to run when a decision to allow or
998 * deny quota has been made. Don't forget to call this!
999 */
1000 @Override
1001 public void onExceededDatabaseQuota(String url,
1002 String databaseIdentifier, long currentQuota, long estimatedSize,
1003 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -07001004 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001005 .onExceededDatabaseQuota(url, databaseIdentifier,
1006 currentQuota, estimatedSize, totalUsedQuota,
1007 quotaUpdater);
1008 }
1009
1010 /**
1011 * The Application Cache has exceeded its max size.
1012 * @param spaceNeeded is the amount of disk space that would be needed
1013 * in order for the last appcache operation to succeed.
1014 * @param totalUsedQuota is the sum of all origins' quota.
1015 * @param quotaUpdater A callback to inform the WebCore thread that a
1016 * new app cache size is available. This callback must always
1017 * be executed at some point to ensure that the sleeping
1018 * WebCore thread is woken up.
1019 */
1020 @Override
1021 public void onReachedMaxAppCacheSize(long spaceNeeded,
1022 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -07001023 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001024 .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota,
1025 quotaUpdater);
1026 }
1027
1028 /**
1029 * Instructs the browser to show a prompt to ask the user to set the
1030 * Geolocation permission state for the specified origin.
1031 * @param origin The origin for which Geolocation permissions are
1032 * requested.
1033 * @param callback The callback to call once the user has set the
1034 * Geolocation permission state.
1035 */
1036 @Override
1037 public void onGeolocationPermissionsShowPrompt(String origin,
1038 GeolocationPermissions.Callback callback) {
1039 if (mInForeground) {
Grace Kloba50c241e2010-04-20 11:07:50 -07001040 getGeolocationPermissionsPrompt().show(origin, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001041 }
1042 }
1043
1044 /**
1045 * Instructs the browser to hide the Geolocation permissions prompt.
1046 */
1047 @Override
1048 public void onGeolocationPermissionsHidePrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001049 if (mInForeground && mGeolocationPermissionsPrompt != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001050 mGeolocationPermissionsPrompt.hide();
1051 }
1052 }
1053
Ben Murdoch65acc352009-11-19 18:16:04 +00001054 /* Adds a JavaScript error message to the system log and if the JS
1055 * console is enabled in the about:debug options, to that console
1056 * also.
Ben Murdochc42addf2010-01-28 15:19:59 +00001057 * @param consoleMessage the message object.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001058 */
1059 @Override
Ben Murdochc42addf2010-01-28 15:19:59 +00001060 public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Jeff Hamilton47654f42010-09-07 09:57:51 -05001061 // Don't log console messages in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001062 if (isPrivateBrowsingEnabled()) return true;
Jeff Hamilton47654f42010-09-07 09:57:51 -05001063
Ben Murdochc42addf2010-01-28 15:19:59 +00001064 String message = "Console: " + consoleMessage.message() + " "
1065 + consoleMessage.sourceId() + ":"
1066 + consoleMessage.lineNumber();
1067
1068 switch (consoleMessage.messageLevel()) {
1069 case TIP:
1070 Log.v(CONSOLE_LOGTAG, message);
1071 break;
1072 case LOG:
1073 Log.i(CONSOLE_LOGTAG, message);
1074 break;
1075 case WARNING:
1076 Log.w(CONSOLE_LOGTAG, message);
1077 break;
1078 case ERROR:
1079 Log.e(CONSOLE_LOGTAG, message);
1080 break;
1081 case DEBUG:
1082 Log.d(CONSOLE_LOGTAG, message);
1083 break;
1084 }
1085
1086 return true;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001087 }
1088
1089 /**
1090 * Ask the browser for an icon to represent a <video> element.
1091 * This icon will be used if the Web page did not specify a poster attribute.
1092 * @return Bitmap The icon or null if no such icon is available.
1093 */
1094 @Override
1095 public Bitmap getDefaultVideoPoster() {
1096 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001097 return mWebViewController.getDefaultVideoPoster();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001098 }
1099 return null;
1100 }
1101
1102 /**
1103 * Ask the host application for a custom progress view to show while
1104 * a <video> is loading.
1105 * @return View The progress view.
1106 */
1107 @Override
1108 public View getVideoLoadingProgressView() {
1109 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001110 return mWebViewController.getVideoLoadingProgressView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001111 }
1112 return null;
1113 }
1114
1115 @Override
Ben Murdoch8cad4132012-01-11 10:56:43 +00001116 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001117 if (mInForeground) {
Ben Murdoch8cad4132012-01-11 10:56:43 +00001118 mWebViewController.openFileChooser(uploadMsg, acceptType, capture);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001119 } else {
1120 uploadMsg.onReceiveValue(null);
1121 }
1122 }
1123
Vivek Sekharb54614f2014-05-01 19:03:37 -07001124 @Override
1125 public void showFileChooser(ValueCallback<String[]> uploadFilePaths, String acceptTypes,
1126 boolean capture) {
1127 if (mInForeground) {
1128 mWebViewController.showFileChooser(uploadFilePaths, acceptTypes, capture);
1129 } else {
1130 uploadFilePaths.onReceiveValue(null);
1131 }
1132 }
1133
Grace Kloba22ac16e2009-10-07 18:00:23 -07001134 /**
1135 * Deliver a list of already-visited URLs
1136 */
1137 @Override
1138 public void getVisitedHistory(final ValueCallback<String[]> callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001139 mWebViewController.getVisitedHistory(callback);
1140 }
Ben Murdoch8029a772010-11-16 11:58:21 +00001141
1142 @Override
1143 public void setupAutoFill(Message message) {
1144 // Prompt the user to set up their profile.
1145 final Message msg = message;
Michael Kolb14612442011-06-24 13:06:29 -07001146 AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
1147 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
Ben Murdoch1d676b62011-01-17 12:54:24 +00001148 Context.LAYOUT_INFLATER_SERVICE);
1149 final View layout = inflater.inflate(R.layout.setup_autofill_dialog, null);
1150
1151 builder.setView(layout)
1152 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1153 @Override
1154 public void onClick(DialogInterface dialog, int id) {
1155 CheckBox disableAutoFill = (CheckBox) layout.findViewById(
1156 R.id.setup_autofill_dialog_disable_autofill);
1157
1158 if (disableAutoFill.isChecked()) {
1159 // Disable autofill and show a toast with how to turn it on again.
John Reck35e9dd62011-04-25 09:01:54 -07001160 mSettings.setAutofillEnabled(false);
Michael Kolb14612442011-06-24 13:06:29 -07001161 Toast.makeText(mContext,
Ben Murdoch1d676b62011-01-17 12:54:24 +00001162 R.string.autofill_setup_dialog_negative_toast,
1163 Toast.LENGTH_LONG).show();
1164 } else {
1165 // Take user to the AutoFill profile editor. When they return,
1166 // we will send the message that we pass here which will trigger
1167 // the form to get filled out with their new profile.
1168 mWebViewController.setupAutoFill(msg);
1169 }
1170 }
1171 })
1172 .setNegativeButton(R.string.cancel, null)
1173 .show();
Ben Murdoch8029a772010-11-16 11:58:21 +00001174 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001175 };
1176
1177 // -------------------------------------------------------------------------
1178 // WebViewClient implementation for the sub window
1179 // -------------------------------------------------------------------------
1180
1181 // Subclass of WebViewClient used in subwindows to notify the main
1182 // WebViewClient of certain WebView activities.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001183 private static class SubWindowClient extends WebViewClient {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001184 // The main WebViewClient.
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001185 private final WebViewClient mClient;
Michael Kolb8233fac2010-10-26 16:08:53 -07001186 private final WebViewController mController;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001187
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001188 SubWindowClient(WebViewClient client, WebViewController controller) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001189 mClient = client;
Michael Kolb8233fac2010-10-26 16:08:53 -07001190 mController = controller;
Leon Scroggins III211ba542010-04-19 13:21:13 -04001191 }
1192 @Override
1193 public void onPageStarted(WebView view, String url, Bitmap favicon) {
1194 // Unlike the others, do not call mClient's version, which would
1195 // change the progress bar. However, we do want to remove the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001196 // find or select dialog.
Michael Kolb8233fac2010-10-26 16:08:53 -07001197 mController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001198 }
1199 @Override
1200 public void doUpdateVisitedHistory(WebView view, String url,
1201 boolean isReload) {
1202 mClient.doUpdateVisitedHistory(view, url, isReload);
1203 }
1204 @Override
1205 public boolean shouldOverrideUrlLoading(WebView view, String url) {
1206 return mClient.shouldOverrideUrlLoading(view, url);
1207 }
1208 @Override
1209 public void onReceivedSslError(WebView view, SslErrorHandler handler,
1210 SslError error) {
1211 mClient.onReceivedSslError(view, handler, error);
1212 }
1213 @Override
Brian Carlstrom8862c1d2011-06-02 01:05:55 -07001214 public void onReceivedClientCertRequest(WebView view,
1215 ClientCertRequestHandler handler, String host_and_port) {
1216 mClient.onReceivedClientCertRequest(view, handler, host_and_port);
1217 }
1218 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -07001219 public void onReceivedHttpAuthRequest(WebView view,
1220 HttpAuthHandler handler, String host, String realm) {
1221 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
1222 }
1223 @Override
1224 public void onFormResubmission(WebView view, Message dontResend,
1225 Message resend) {
1226 mClient.onFormResubmission(view, dontResend, resend);
1227 }
1228 @Override
1229 public void onReceivedError(WebView view, int errorCode,
1230 String description, String failingUrl) {
1231 mClient.onReceivedError(view, errorCode, description, failingUrl);
1232 }
1233 @Override
1234 public boolean shouldOverrideKeyEvent(WebView view,
1235 android.view.KeyEvent event) {
1236 return mClient.shouldOverrideKeyEvent(view, event);
1237 }
1238 @Override
1239 public void onUnhandledKeyEvent(WebView view,
1240 android.view.KeyEvent event) {
1241 mClient.onUnhandledKeyEvent(view, event);
1242 }
1243 }
1244
1245 // -------------------------------------------------------------------------
1246 // WebChromeClient implementation for the sub window
1247 // -------------------------------------------------------------------------
1248
1249 private class SubWindowChromeClient extends WebChromeClient {
1250 // The main WebChromeClient.
1251 private final WebChromeClient mClient;
1252
1253 SubWindowChromeClient(WebChromeClient client) {
1254 mClient = client;
1255 }
1256 @Override
1257 public void onProgressChanged(WebView view, int newProgress) {
1258 mClient.onProgressChanged(view, newProgress);
1259 }
1260 @Override
1261 public boolean onCreateWindow(WebView view, boolean dialog,
1262 boolean userGesture, android.os.Message resultMsg) {
1263 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
1264 }
1265 @Override
1266 public void onCloseWindow(WebView window) {
1267 if (window != mSubView) {
1268 Log.e(LOGTAG, "Can't close the window");
1269 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001270 mWebViewController.dismissSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001271 }
1272 }
1273
1274 // -------------------------------------------------------------------------
1275
1276 // Construct a new tab
Michael Kolb7bcafde2011-05-09 13:55:59 -07001277 Tab(WebViewController wvcontroller, WebView w) {
John Reck1cf4b792011-07-26 10:22:22 -07001278 this(wvcontroller, w, null);
1279 }
1280
1281 Tab(WebViewController wvcontroller, Bundle state) {
1282 this(wvcontroller, null, state);
1283 }
1284
1285 Tab(WebViewController wvcontroller, WebView w, Bundle state) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001286 mWebViewController = wvcontroller;
Michael Kolb14612442011-06-24 13:06:29 -07001287 mContext = mWebViewController.getContext();
John Reck35e9dd62011-04-25 09:01:54 -07001288 mSettings = BrowserSettings.getInstance();
Michael Kolb14612442011-06-24 13:06:29 -07001289 mDataController = DataController.getInstance(mContext);
1290 mCurrentState = new PageState(mContext, w != null
John Reck847b5322011-04-14 17:02:18 -07001291 ? w.isPrivateBrowsingEnabled() : false);
Tarun Nainani8084c822014-06-25 13:38:06 -07001292 setTimeStamp();
Michael Kolb8233fac2010-10-26 16:08:53 -07001293 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001294 mInForeground = false;
1295
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001296 mDownloadListener = new BrowserDownloadListener() {
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001297 public void onDownloadStart(String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001298 String contentDisposition, String mimetype, String referer,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001299 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001300 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001301 mimetype, referer, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001302 }
1303 };
1304
John Reck1cf4b792011-07-26 10:22:22 -07001305 mCaptureWidth = mContext.getResources().getDimensionPixelSize(
1306 R.dimen.tab_thumbnail_width);
1307 mCaptureHeight = mContext.getResources().getDimensionPixelSize(
1308 R.dimen.tab_thumbnail_height);
1309 updateShouldCaptureThumbnails();
1310 restoreState(state);
John Reck52be4782011-08-26 15:37:29 -07001311 if (getId() == -1) {
1312 mId = TabControl.getNextId();
1313 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001314 setWebView(w);
Michael Kolb9ef259a2011-07-12 15:33:08 -07001315 mHandler = new Handler() {
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001316 @Override
Michael Kolb9ef259a2011-07-12 15:33:08 -07001317 public void handleMessage(Message m) {
John Reck1cf4b792011-07-26 10:22:22 -07001318 switch (m.what) {
1319 case MSG_CAPTURE:
1320 capture();
1321 break;
1322 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001323 }
1324 };
John Reck1cf4b792011-07-26 10:22:22 -07001325 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001326
Michael Kolb72864272012-05-03 15:42:15 -07001327 public boolean shouldUpdateThumbnail() {
1328 return mUpdateThumbnail;
1329 }
1330
Mathew Inwoode09305e2011-09-02 12:03:26 +01001331 /**
1332 * This is used to get a new ID when the tab has been preloaded, before it is displayed and
1333 * added to TabControl. Preloaded tabs can be created before restoreInstanceState, leading
1334 * to overlapping IDs between the preloaded and restored tabs.
1335 */
1336 public void refreshIdAfterPreload() {
1337 mId = TabControl.getNextId();
1338 }
1339
John Reck1cf4b792011-07-26 10:22:22 -07001340 public void updateShouldCaptureThumbnails() {
1341 if (mWebViewController.shouldCaptureThumbnails()) {
1342 synchronized (Tab.this) {
1343 if (mCapture == null) {
1344 mCapture = Bitmap.createBitmap(mCaptureWidth, mCaptureHeight,
1345 Bitmap.Config.RGB_565);
Michael Kolbc3af0672011-08-09 10:24:41 -07001346 mCapture.eraseColor(Color.WHITE);
John Reck1cf4b792011-07-26 10:22:22 -07001347 if (mInForeground) {
1348 postCapture();
1349 }
1350 }
1351 }
1352 } else {
1353 synchronized (Tab.this) {
1354 mCapture = null;
1355 deleteThumbnail();
1356 }
1357 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001358 }
1359
Michael Kolb14612442011-06-24 13:06:29 -07001360 public void setController(WebViewController ctl) {
1361 mWebViewController = ctl;
John Reck1cf4b792011-07-26 10:22:22 -07001362 updateShouldCaptureThumbnails();
Michael Kolb14612442011-06-24 13:06:29 -07001363 }
1364
Michael Kolbc831b632011-05-11 09:30:34 -07001365 public long getId() {
1366 return mId;
1367 }
1368
Michael Kolb91911a22012-01-17 11:21:25 -08001369 void setWebView(WebView w) {
1370 setWebView(w, true);
1371 }
1372
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001373 public boolean isNativeActive(){
1374 if (mMainView == null)
1375 return false;
1376 return true;
1377 }
1378
1379 public void setTimeStamp(){
1380 Date d = new Date();
1381 timestamp = (new Timestamp(d.getTime()));
1382 }
1383
1384 public Timestamp getTimestamp() {
1385 return timestamp;
1386 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001387 /**
1388 * Sets the WebView for this tab, correctly removing the old WebView from
1389 * the container view.
1390 */
Michael Kolb91911a22012-01-17 11:21:25 -08001391 void setWebView(WebView w, boolean restore) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001392 if (mMainView == w) {
1393 return;
1394 }
Michael Kolba713ec82010-11-29 17:27:06 -08001395
Grace Kloba22ac16e2009-10-07 18:00:23 -07001396 // If the WebView is changing, the page will be reloaded, so any ongoing
1397 // Geolocation permission requests are void.
Grace Kloba50c241e2010-04-20 11:07:50 -07001398 if (mGeolocationPermissionsPrompt != null) {
1399 mGeolocationPermissionsPrompt.hide();
1400 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001401
Michael Kolba713ec82010-11-29 17:27:06 -08001402 mWebViewController.onSetWebView(this, w);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001403
John Reck1cf4b792011-07-26 10:22:22 -07001404 if (mMainView != null) {
John Reck8ee633f2011-08-09 16:00:35 -07001405 mMainView.setPictureListener(null);
John Reck1cf4b792011-07-26 10:22:22 -07001406 if (w != null) {
1407 syncCurrentState(w, null);
1408 } else {
Panos Thomasa9a5a582014-03-18 19:20:08 -07001409 mCurrentState = new PageState(mContext, mMainView.isPrivateBrowsingEnabled());
John Reck1cf4b792011-07-26 10:22:22 -07001410 }
1411 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001412 // set the new one
1413 mMainView = w;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001414 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001415 if (mMainView != null) {
1416 mMainView.setWebViewClient(mWebViewClient);
1417 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001418 // Attach DownloadManager so that downloads can start in an active
1419 // or a non-active window. This can happen when going to a site that
1420 // does a redirect after a period of time. The user could have
1421 // switched to another tab while waiting for the download to start.
1422 mMainView.setDownloadListener(mDownloadListener);
John Reck8ee633f2011-08-09 16:00:35 -07001423 TabControl tc = mWebViewController.getTabControl();
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001424 if (tc != null /*&& tc.getOnThumbnailUpdatedListener() != null*/) {
John Reck8ee633f2011-08-09 16:00:35 -07001425 mMainView.setPictureListener(this);
1426 }
Michael Kolb91911a22012-01-17 11:21:25 -08001427 if (restore && (mSavedState != null)) {
John Reck8b9bb8b2012-03-08 13:19:40 -08001428 restoreUserAgent();
John Reck6c2e2f32011-08-22 13:41:23 -07001429 WebBackForwardList restoredState
1430 = mMainView.restoreState(mSavedState);
1431 if (restoredState == null || restoredState.getSize() == 0) {
1432 Log.w(LOGTAG, "Failed to restore WebView state!");
1433 loadUrl(mCurrentState.mOriginalUrl, null);
1434 }
John Reck1cf4b792011-07-26 10:22:22 -07001435 mSavedState = null;
1436 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001437 }
1438 }
1439
1440 /**
1441 * Destroy the tab's main WebView and subWindow if any
1442 */
1443 void destroy() {
Tarun Nainani2c1dd7c2014-07-05 16:40:12 -07001444
Matthew Huib7f2e9c2014-04-16 11:12:37 -04001445 if (mPostponeDestroy) {
1446 mShouldDestroy = true;
1447 return;
1448 }
1449 mShouldDestroy = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001450 if (mMainView != null) {
1451 dismissSubWindow();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001452 // save the WebView to call destroy() after detach it from the tab
1453 WebView webView = mMainView;
1454 setWebView(null);
1455 webView.destroy();
1456 }
1457 }
1458
Matthew Huib7f2e9c2014-04-16 11:12:37 -04001459 private boolean mPostponeDestroy = false;
1460 private boolean mShouldDestroy = false;
1461
1462 public void postponeDestroy() {
1463 mPostponeDestroy = true;
1464 }
1465
1466 public void performPostponedDestroy() {
1467 mPostponeDestroy = false;
1468 if (mShouldDestroy) {
1469 destroy();
1470 }
1471 }
1472
Grace Kloba22ac16e2009-10-07 18:00:23 -07001473 /**
1474 * Remove the tab from the parent
1475 */
1476 void removeFromTree() {
1477 // detach the children
Michael Kolbc831b632011-05-11 09:30:34 -07001478 if (mChildren != null) {
1479 for(Tab t : mChildren) {
1480 t.setParent(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001481 }
1482 }
1483 // remove itself from the parent list
Michael Kolbc831b632011-05-11 09:30:34 -07001484 if (mParent != null) {
1485 mParent.mChildren.remove(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001486 }
Tarun Nainani8eb00912014-07-17 12:28:32 -07001487
1488 mCapture = null;
John Reck1cf4b792011-07-26 10:22:22 -07001489 deleteThumbnail();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001490 }
1491
1492 /**
1493 * Create a new subwindow unless a subwindow already exists.
1494 * @return True if a new subwindow was created. False if one already exists.
1495 */
1496 boolean createSubWindow() {
1497 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001498 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001499 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001500 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001501 mSubView.setWebChromeClient(new SubWindowChromeClient(
1502 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001503 // Set a different DownloadListener for the mSubView, since it will
1504 // just need to dismiss the mSubView, rather than close the Tab
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001505 mSubView.setDownloadListener(new BrowserDownloadListener() {
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001506 public void onDownloadStart(String url, String userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001507 String contentDisposition, String mimetype, String referer,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001508 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001509 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Selim Gurun0b3d66f2012-08-29 13:08:13 -07001510 contentDisposition, mimetype, referer, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001511 if (mSubView.copyBackForwardList().getSize() == 0) {
1512 // This subwindow was opened for the sole purpose of
1513 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001514 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001515 }
1516 }
1517 });
Michael Kolb14612442011-06-24 13:06:29 -07001518 mSubView.setOnCreateContextMenuListener(mWebViewController.getActivity());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001519 return true;
1520 }
1521 return false;
1522 }
1523
1524 /**
1525 * Dismiss the subWindow for the tab.
1526 */
1527 void dismissSubWindow() {
1528 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001529 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001530 mSubView.destroy();
1531 mSubView = null;
1532 mSubViewContainer = null;
1533 }
1534 }
1535
Grace Kloba22ac16e2009-10-07 18:00:23 -07001536
1537 /**
1538 * Set the parent tab of this tab.
1539 */
Michael Kolbc831b632011-05-11 09:30:34 -07001540 void setParent(Tab parent) {
John Reck52be4782011-08-26 15:37:29 -07001541 if (parent == this) {
1542 throw new IllegalStateException("Cannot set parent to self!");
1543 }
Michael Kolbc831b632011-05-11 09:30:34 -07001544 mParent = parent;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001545 // This tab may have been freed due to low memory. If that is the case,
Michael Kolbc831b632011-05-11 09:30:34 -07001546 // the parent tab id is already saved. If we are changing that id
Grace Kloba22ac16e2009-10-07 18:00:23 -07001547 // (most likely due to removing the parent tab) we must update the
Michael Kolbc831b632011-05-11 09:30:34 -07001548 // parent tab id in the saved Bundle.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001549 if (mSavedState != null) {
1550 if (parent == null) {
1551 mSavedState.remove(PARENTTAB);
1552 } else {
Michael Kolbc831b632011-05-11 09:30:34 -07001553 mSavedState.putLong(PARENTTAB, parent.getId());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001554 }
1555 }
John Reckb0a86db2011-05-24 14:05:58 -07001556
1557 // Sync the WebView useragent with the parent
1558 if (parent != null && mSettings.hasDesktopUseragent(parent.getWebView())
1559 != mSettings.hasDesktopUseragent(getWebView())) {
1560 mSettings.toggleDesktopUseragent(getWebView());
1561 }
John Reck52be4782011-08-26 15:37:29 -07001562
1563 if (parent != null && parent.getId() == getId()) {
1564 throw new IllegalStateException("Parent has same ID as child!");
1565 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001566 }
1567
1568 /**
Michael Kolbc831b632011-05-11 09:30:34 -07001569 * If this Tab was created through another Tab, then this method returns
1570 * that Tab.
1571 * @return the Tab parent or null
1572 */
1573 public Tab getParent() {
1574 return mParent;
1575 }
1576
1577 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001578 * When a Tab is created through the content of another Tab, then we
1579 * associate the Tabs.
1580 * @param child the Tab that was created from this Tab
1581 */
1582 void addChildTab(Tab child) {
Michael Kolbc831b632011-05-11 09:30:34 -07001583 if (mChildren == null) {
1584 mChildren = new Vector<Tab>();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001585 }
Michael Kolbc831b632011-05-11 09:30:34 -07001586 mChildren.add(child);
1587 child.setParent(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001588 }
1589
Michael Kolbc831b632011-05-11 09:30:34 -07001590 Vector<Tab> getChildren() {
1591 return mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001592 }
1593
1594 void resume() {
1595 if (mMainView != null) {
Pankaj Garg60221af2014-09-23 17:57:50 -07001596 if (mMainView.hasCrashed()) {
1597 // Reload if render process has crashed. This is done here so that
1598 // setFocus call sends wasShown message to correct render process.
Vivek Sekhared791da2015-02-22 12:39:05 -08001599 mMainView.setNeedsReload(true);
Pankaj Garg60221af2014-09-23 17:57:50 -07001600 }
John Reck56c1fcf2011-08-17 10:15:16 -07001601 setupHwAcceleration(mMainView);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001602 mMainView.onResume();
1603 if (mSubView != null) {
1604 mSubView.onResume();
1605 }
1606 }
1607 }
1608
John Reck56c1fcf2011-08-17 10:15:16 -07001609 private void setupHwAcceleration(View web) {
1610 if (web == null) return;
1611 BrowserSettings settings = BrowserSettings.getInstance();
1612 if (settings.isHardwareAccelerated()) {
1613 web.setLayerType(View.LAYER_TYPE_NONE, null);
1614 } else {
1615 web.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
1616 }
1617 }
1618
Grace Kloba22ac16e2009-10-07 18:00:23 -07001619 void pause() {
1620 if (mMainView != null) {
1621 mMainView.onPause();
1622 if (mSubView != null) {
1623 mSubView.onPause();
1624 }
1625 }
1626 }
1627
1628 void putInForeground() {
John Reck8ee633f2011-08-09 16:00:35 -07001629 if (mInForeground) {
1630 return;
1631 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001632 mInForeground = true;
1633 resume();
Michael Kolb14612442011-06-24 13:06:29 -07001634 Activity activity = mWebViewController.getActivity();
1635 mMainView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001636 if (mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -07001637 mSubView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001638 }
1639 // Show the pending error dialog if the queue is not empty
1640 if (mQueuedErrors != null && mQueuedErrors.size() > 0) {
1641 showError(mQueuedErrors.getFirst());
1642 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001643 mWebViewController.bookmarkedStatusHasChanged(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001644 }
1645
1646 void putInBackground() {
John Reck8ee633f2011-08-09 16:00:35 -07001647 if (!mInForeground) {
1648 return;
1649 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001650 mInForeground = false;
1651 pause();
1652 mMainView.setOnCreateContextMenuListener(null);
1653 if (mSubView != null) {
1654 mSubView.setOnCreateContextMenuListener(null);
1655 }
1656 }
1657
Michael Kolb8233fac2010-10-26 16:08:53 -07001658 boolean inForeground() {
1659 return mInForeground;
1660 }
1661
Grace Kloba22ac16e2009-10-07 18:00:23 -07001662 /**
1663 * Return the top window of this tab; either the subwindow if it is not
1664 * null or the main window.
1665 * @return The top window of this tab.
1666 */
1667 WebView getTopWindow() {
1668 if (mSubView != null) {
1669 return mSubView;
1670 }
1671 return mMainView;
1672 }
1673
1674 /**
1675 * Return the main window of this tab. Note: if a tab is freed in the
1676 * background, this can return null. It is only guaranteed to be
1677 * non-null for the current tab.
1678 * @return The main WebView of this tab.
1679 */
1680 WebView getWebView() {
1681 return mMainView;
1682 }
1683
Michael Kolba713ec82010-11-29 17:27:06 -08001684 void setViewContainer(View container) {
1685 mContainer = container;
1686 }
1687
Michael Kolb8233fac2010-10-26 16:08:53 -07001688 View getViewContainer() {
1689 return mContainer;
1690 }
1691
Grace Kloba22ac16e2009-10-07 18:00:23 -07001692 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001693 * Return whether private browsing is enabled for the main window of
1694 * this tab.
1695 * @return True if private browsing is enabled.
1696 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001697 boolean isPrivateBrowsingEnabled() {
John Reck502a3532011-08-16 14:21:46 -07001698 return mCurrentState.mIncognito;
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001699 }
1700
1701 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001702 * Return the subwindow of this tab or null if there is no subwindow.
1703 * @return The subwindow of this tab or null.
1704 */
1705 WebView getSubWebView() {
1706 return mSubView;
1707 }
1708
Michael Kolb1514bb72010-11-22 09:11:48 -08001709 void setSubWebView(WebView subView) {
1710 mSubView = subView;
1711 }
1712
Michael Kolb8233fac2010-10-26 16:08:53 -07001713 View getSubViewContainer() {
1714 return mSubViewContainer;
1715 }
1716
Michael Kolb1514bb72010-11-22 09:11:48 -08001717 void setSubViewContainer(View subViewContainer) {
1718 mSubViewContainer = subViewContainer;
1719 }
1720
Grace Kloba22ac16e2009-10-07 18:00:23 -07001721 /**
1722 * @return The geolocation permissions prompt for this tab.
1723 */
1724 GeolocationPermissionsPrompt getGeolocationPermissionsPrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001725 if (mGeolocationPermissionsPrompt == null) {
1726 ViewStub stub = (ViewStub) mContainer
1727 .findViewById(R.id.geolocation_permissions_prompt);
1728 mGeolocationPermissionsPrompt = (GeolocationPermissionsPrompt) stub
1729 .inflate();
Panos Thomasb298aad2014-10-22 12:24:21 -07001730 mGeolocationPermissionsPrompt.init(mCurrentState.mIncognito);
Grace Kloba50c241e2010-04-20 11:07:50 -07001731 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001732 return mGeolocationPermissionsPrompt;
1733 }
1734
1735 /**
1736 * @return The application id string
1737 */
1738 String getAppId() {
1739 return mAppId;
1740 }
1741
1742 /**
1743 * Set the application id string
1744 * @param id
1745 */
1746 void setAppId(String id) {
1747 mAppId = id;
1748 }
1749
Michael Kolbe28b3472011-08-04 16:54:31 -07001750 boolean closeOnBack() {
1751 return mCloseOnBack;
1752 }
1753
1754 void setCloseOnBack(boolean close) {
1755 mCloseOnBack = close;
1756 }
1757
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07001758 boolean getDerivedFromIntent() {
1759 return mDerivedFromIntent;
1760 }
1761
1762 void setDerivedFromIntent(boolean derived) {
1763 mDerivedFromIntent = derived;
1764 }
1765
Grace Kloba22ac16e2009-10-07 18:00:23 -07001766 String getUrl() {
John Reck324d4402011-01-11 16:56:42 -08001767 return UrlUtils.filteredUrl(mCurrentState.mUrl);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001768 }
1769
Tarun Nainani8eb00912014-07-17 12:28:32 -07001770
1771 protected void onPageFinished() {
1772 mPageFinished = true;
1773 }
1774
1775 public boolean getPageFinishedStatus() {
1776 return mPageFinished;
1777 }
1778
John Reck49a603c2011-03-03 09:33:05 -08001779 String getOriginalUrl() {
Vivek Sekhar361065a2014-11-25 15:34:35 -08001780 if (mMainView != null)
1781 return mMainView.getOriginalUrl();
John Reckdb22ec42011-06-29 11:31:24 -07001782 if (mCurrentState.mOriginalUrl == null) {
1783 return getUrl();
John Reck49a603c2011-03-03 09:33:05 -08001784 }
John Reckdb22ec42011-06-29 11:31:24 -07001785 return UrlUtils.filteredUrl(mCurrentState.mOriginalUrl);
John Reck49a603c2011-03-03 09:33:05 -08001786 }
1787
Grace Kloba22ac16e2009-10-07 18:00:23 -07001788 /**
John Reck30c714c2010-12-16 17:30:34 -08001789 * Get the title of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001790 */
1791 String getTitle() {
John Reck30c714c2010-12-16 17:30:34 -08001792 return mCurrentState.mTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001793 }
1794
1795 /**
John Reck30c714c2010-12-16 17:30:34 -08001796 * Get the favicon of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001797 */
1798 Bitmap getFavicon() {
John Reck1cf4b792011-07-26 10:22:22 -07001799 if (mCurrentState.mFavicon != null) {
1800 return mCurrentState.mFavicon;
1801 }
1802 return getDefaultFavicon(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001803 }
1804
John Recke969cc52010-12-21 17:24:43 -08001805 public boolean isBookmarkedSite() {
1806 return mCurrentState.mIsBookmarkedSite;
1807 }
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001808
Grace Kloba22ac16e2009-10-07 18:00:23 -07001809 /**
Steve Block08a6f0c2011-10-06 12:12:53 +01001810 * Sets the security state, clears the SSL certificate error and informs
1811 * the controller.
1812 */
Steve Block2466eff2011-10-03 15:33:09 +01001813 private void setSecurityState(SecurityState securityState) {
1814 mCurrentState.mSecurityState = securityState;
Steve Block08a6f0c2011-10-06 12:12:53 +01001815 mCurrentState.mSslCertificateError = null;
Steve Block2466eff2011-10-03 15:33:09 +01001816 mWebViewController.onUpdatedSecurityState(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001817 }
1818
1819 /**
Steve Block2466eff2011-10-03 15:33:09 +01001820 * @return The tab's security state.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001821 */
Steve Block2466eff2011-10-03 15:33:09 +01001822 SecurityState getSecurityState() {
1823 return mCurrentState.mSecurityState;
John Reck30c714c2010-12-16 17:30:34 -08001824 }
1825
Steve Block08a6f0c2011-10-06 12:12:53 +01001826 /**
1827 * Gets the SSL certificate error, if any, for the page's main resource.
1828 * This is only non-null when the security state is
1829 * SECURITY_STATE_BAD_CERTIFICATE.
1830 */
1831 SslError getSslCertificateError() {
1832 return mCurrentState.mSslCertificateError;
1833 }
1834
John Reck30c714c2010-12-16 17:30:34 -08001835 int getLoadProgress() {
1836 if (mInPageLoad) {
1837 return mPageLoadProgress;
1838 }
1839 return 100;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001840 }
1841
1842 /**
1843 * @return TRUE if onPageStarted is called while onPageFinished is not
1844 * called yet.
1845 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001846 boolean inPageLoad() {
1847 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001848 }
1849
Grace Kloba22ac16e2009-10-07 18:00:23 -07001850 /**
John Reck1cf4b792011-07-26 10:22:22 -07001851 * @return The Bundle with the tab's state if it can be saved, otherwise null
Grace Kloba22ac16e2009-10-07 18:00:23 -07001852 */
John Reck1cf4b792011-07-26 10:22:22 -07001853 public Bundle saveState() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001854 // If the WebView is null it means we ran low on memory and we already
1855 // stored the saved state in mSavedState.
1856 if (mMainView == null) {
John Reck1cf4b792011-07-26 10:22:22 -07001857 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001858 }
John Reck6c2e2f32011-08-22 13:41:23 -07001859
1860 if (TextUtils.isEmpty(mCurrentState.mUrl)) {
John Reck1cf4b792011-07-26 10:22:22 -07001861 return null;
John Reck24f18262011-06-17 14:47:20 -07001862 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001863
1864 mSavedState = new Bundle();
John Reck6c2e2f32011-08-22 13:41:23 -07001865 WebBackForwardList savedList = mMainView.saveState(mSavedState);
1866 if (savedList == null || savedList.getSize() == 0) {
1867 Log.w(LOGTAG, "Failed to save back/forward list for "
1868 + mCurrentState.mUrl);
1869 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001870
Michael Kolbc831b632011-05-11 09:30:34 -07001871 mSavedState.putLong(ID, mId);
John Reck30c714c2010-12-16 17:30:34 -08001872 mSavedState.putString(CURRURL, mCurrentState.mUrl);
1873 mSavedState.putString(CURRTITLE, mCurrentState.mTitle);
John Reck1cf4b792011-07-26 10:22:22 -07001874 mSavedState.putBoolean(INCOGNITO, mMainView.isPrivateBrowsingEnabled());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001875 if (mAppId != null) {
1876 mSavedState.putString(APPID, mAppId);
1877 }
Michael Kolbe28b3472011-08-04 16:54:31 -07001878 mSavedState.putBoolean(CLOSEFLAG, mCloseOnBack);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001879 // Remember the parent tab so the relationship can be restored.
Michael Kolbc831b632011-05-11 09:30:34 -07001880 if (mParent != null) {
1881 mSavedState.putLong(PARENTTAB, mParent.mId);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001882 }
John Reckb0a86db2011-05-24 14:05:58 -07001883 mSavedState.putBoolean(USERAGENT,
1884 mSettings.hasDesktopUseragent(getWebView()));
John Reck1cf4b792011-07-26 10:22:22 -07001885 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001886 }
1887
1888 /*
1889 * Restore the state of the tab.
1890 */
John Reck1cf4b792011-07-26 10:22:22 -07001891 private void restoreState(Bundle b) {
1892 mSavedState = b;
1893 if (mSavedState == null) {
1894 return;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001895 }
1896 // Restore the internal state even if the WebView fails to restore.
1897 // This will maintain the app id, original url and close-on-exit values.
Michael Kolbc831b632011-05-11 09:30:34 -07001898 mId = b.getLong(ID);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001899 mAppId = b.getString(APPID);
Michael Kolbe28b3472011-08-04 16:54:31 -07001900 mCloseOnBack = b.getBoolean(CLOSEFLAG);
John Reck8b9bb8b2012-03-08 13:19:40 -08001901 restoreUserAgent();
John Reck1cf4b792011-07-26 10:22:22 -07001902 String url = b.getString(CURRURL);
1903 String title = b.getString(CURRTITLE);
1904 boolean incognito = b.getBoolean(INCOGNITO);
1905 mCurrentState = new PageState(mContext, incognito, url, null);
1906 mCurrentState.mTitle = title;
1907 synchronized (Tab.this) {
1908 if (mCapture != null) {
John Reck4eadc342011-10-31 14:04:10 -07001909 DataController.getInstance(mContext).loadThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07001910 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001911 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001912 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04001913
John Reck8b9bb8b2012-03-08 13:19:40 -08001914 private void restoreUserAgent() {
1915 if (mMainView == null || mSavedState == null) {
1916 return;
1917 }
1918 if (mSavedState.getBoolean(USERAGENT)
1919 != mSettings.hasDesktopUseragent(mMainView)) {
1920 mSettings.toggleDesktopUseragent(mMainView);
1921 }
1922 }
1923
Leon Scroggins1961ed22010-12-07 15:22:21 -05001924 public void updateBookmarkedStatus() {
John Recke969cc52010-12-21 17:24:43 -08001925 mDataController.queryBookmarkStatus(getUrl(), mIsBookmarkCallback);
Leon Scroggins1961ed22010-12-07 15:22:21 -05001926 }
1927
John Recke969cc52010-12-21 17:24:43 -08001928 private DataController.OnQueryUrlIsBookmark mIsBookmarkCallback
1929 = new DataController.OnQueryUrlIsBookmark() {
1930 @Override
1931 public void onQueryUrlIsBookmark(String url, boolean isBookmark) {
1932 if (mCurrentState.mUrl.equals(url)) {
1933 mCurrentState.mIsBookmarkedSite = isBookmark;
1934 mWebViewController.bookmarkedStatusHasChanged(Tab.this);
1935 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001936 }
John Recke969cc52010-12-21 17:24:43 -08001937 };
Michael Kolb1acef692011-03-08 14:12:06 -08001938
Michael Kolbeb95db42011-03-03 10:38:40 -08001939 public Bitmap getScreenshot() {
John Reck1cf4b792011-07-26 10:22:22 -07001940 synchronized (Tab.this) {
1941 return mCapture;
1942 }
Michael Kolbeb95db42011-03-03 10:38:40 -08001943 }
1944
John Reck541f55a2011-06-07 16:34:43 -07001945 public boolean isSnapshot() {
John Reck541f55a2011-06-07 16:34:43 -07001946 return false;
1947 }
1948
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001949 private static class SaveCallback implements ValueCallback<String> {
1950 boolean onReceiveValueCalled = false;
1951 private String mPath;
John Reck68234a92012-04-19 15:27:12 -07001952
1953 @Override
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001954 public void onReceiveValue(String path) {
1955 this.onReceiveValueCalled = true;
1956 this.mPath = path;
John Reck68234a92012-04-19 15:27:12 -07001957 synchronized (this) {
1958 notifyAll();
John Reck8cc92352011-07-06 17:41:52 -07001959 }
John Reck541f55a2011-06-07 16:34:43 -07001960 }
John Reck68234a92012-04-19 15:27:12 -07001961
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001962 public String getPath() {
1963 return mPath;
1964 }
John Reck68234a92012-04-19 15:27:12 -07001965 }
1966
1967 /**
1968 * Must be called on the UI thread
1969 */
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001970 public ContentValues createSnapshotValues(Bitmap bm) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001971 WebView web = getWebView();
John Reck68234a92012-04-19 15:27:12 -07001972 if (web == null) return null;
John Reckd8c74522011-06-14 08:45:00 -07001973 ContentValues values = new ContentValues();
1974 values.put(Snapshots.TITLE, mCurrentState.mTitle);
1975 values.put(Snapshots.URL, mCurrentState.mUrl);
John Reck68234a92012-04-19 15:27:12 -07001976 values.put(Snapshots.BACKGROUND, web.getPageBackgroundColor());
John Reck8cc92352011-07-06 17:41:52 -07001977 values.put(Snapshots.DATE_CREATED, System.currentTimeMillis());
1978 values.put(Snapshots.FAVICON, compressBitmap(getFavicon()));
Vivek Sekhar46c3ec02014-10-09 17:28:57 -07001979 values.put(Snapshots.THUMBNAIL, compressBitmap(bm));
John Reckd8c74522011-06-14 08:45:00 -07001980 return values;
John Reck541f55a2011-06-07 16:34:43 -07001981 }
1982
John Reck68234a92012-04-19 15:27:12 -07001983 /**
1984 * Probably want to call this on a background thread
1985 */
1986 public boolean saveViewState(ContentValues values) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001987 WebView web = getWebView();
John Reck68234a92012-04-19 15:27:12 -07001988 if (web == null) return false;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001989 String filename = UUID.randomUUID().toString();
John Reck68234a92012-04-19 15:27:12 -07001990 SaveCallback callback = new SaveCallback();
John Reck68234a92012-04-19 15:27:12 -07001991 try {
John Reck68234a92012-04-19 15:27:12 -07001992 synchronized (callback) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001993 web.saveViewState(filename, callback);
1994 callback.wait();
John Reck68234a92012-04-19 15:27:12 -07001995 }
John Reck68234a92012-04-19 15:27:12 -07001996 } catch (Exception e) {
1997 Log.w(LOGTAG, "Failed to save view state", e);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08001998 String path = callback.getPath();
1999 if (path != null) {
2000 File file = mContext.getFileStreamPath(path);
2001 if (file.exists() && !file.delete()) {
2002 file.deleteOnExit();
2003 }
John Reck68234a92012-04-19 15:27:12 -07002004 }
2005 return false;
2006 }
Tarun Nainani8eb00912014-07-17 12:28:32 -07002007
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002008 String path = callback.getPath();
Tarun Nainani8eb00912014-07-17 12:28:32 -07002009 // could be that saving of file failed
2010 if (path == null) {
2011 return false;
2012 }
2013
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002014 File savedFile = new File(path);
2015 if (!savedFile.exists()) {
2016 return false;
John Reck68234a92012-04-19 15:27:12 -07002017 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -08002018 values.put(Snapshots.VIEWSTATE_PATH, path.substring(path.lastIndexOf('/') + 1));
2019 values.put(Snapshots.VIEWSTATE_SIZE, savedFile.length());
John Reck68234a92012-04-19 15:27:12 -07002020 return true;
2021 }
2022
John Reck8cc92352011-07-06 17:41:52 -07002023 public byte[] compressBitmap(Bitmap bitmap) {
2024 if (bitmap == null) {
2025 return null;
2026 }
2027 ByteArrayOutputStream stream = new ByteArrayOutputStream();
2028 bitmap.compress(CompressFormat.PNG, 100, stream);
2029 return stream.toByteArray();
2030 }
2031
John Reck26b18322011-06-21 13:08:58 -07002032 public void loadUrl(String url, Map<String, String> headers) {
2033 if (mMainView != null) {
Michael Kolba53c9892011-10-05 13:31:40 -07002034 mPageLoadProgress = INITIAL_PROGRESS;
Michael Kolb14612442011-06-24 13:06:29 -07002035 mCurrentState = new PageState(mContext, false, url, null);
John Reck26b18322011-06-21 13:08:58 -07002036 mMainView.loadUrl(url, headers);
2037 }
2038 }
2039
John Reck38b39652012-06-05 09:22:59 -07002040 public void disableUrlOverridingForLoad() {
2041 mDisableOverrideUrlLoading = true;
2042 }
2043
Michael Kolb9ef259a2011-07-12 15:33:08 -07002044 protected void capture() {
2045 if (mMainView == null || mCapture == null) return;
John Reck4eadc342011-10-31 14:04:10 -07002046 if (mMainView.getContentWidth() <= 0 || mMainView.getContentHeight() <= 0) {
2047 return;
2048 }
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002049
2050 mMainView
2051 .getContentBitmapAsync(
2052 (float) mCaptureWidth / mMainView.getWidth(),
2053 new Rect(),
2054 new ValueCallback<Bitmap>() {
2055 @Override
2056 public void onReceiveValue(Bitmap bitmap) {
2057 onCaptureCallback(bitmap);
2058 }});
2059 }
2060
2061 private void onCaptureCallback(Bitmap bitmap) {
2062 if (mCapture == null || bitmap == null)
2063 return;
2064
Michael Kolb9ef259a2011-07-12 15:33:08 -07002065 Canvas c = new Canvas(mCapture);
Tarun Nainaniea28dde2014-08-27 17:25:09 -07002066 mCapture.eraseColor(Color.WHITE);
2067 c.drawBitmap(bitmap, 0, 0, null);
Axesh R. Ajmera2e241242014-05-19 15:53:38 -07002068
Michael Kolba3194d02011-09-07 11:23:51 -07002069 // manually anti-alias the edges for the tilt
2070 c.drawRect(0, 0, 1, mCapture.getHeight(), sAlphaPaint);
2071 c.drawRect(mCapture.getWidth() - 1, 0, mCapture.getWidth(),
2072 mCapture.getHeight(), sAlphaPaint);
2073 c.drawRect(0, 0, mCapture.getWidth(), 1, sAlphaPaint);
2074 c.drawRect(0, mCapture.getHeight() - 1, mCapture.getWidth(),
2075 mCapture.getHeight(), sAlphaPaint);
Dianne Hackborn43cfe8a2011-08-02 16:59:35 -07002076 c.setBitmap(null);
John Reck8ee633f2011-08-09 16:00:35 -07002077 mHandler.removeMessages(MSG_CAPTURE);
John Reck1cf4b792011-07-26 10:22:22 -07002078 persistThumbnail();
John Reck8ee633f2011-08-09 16:00:35 -07002079 TabControl tc = mWebViewController.getTabControl();
2080 if (tc != null) {
2081 OnThumbnailUpdatedListener updateListener
2082 = tc.getOnThumbnailUpdatedListener();
2083 if (updateListener != null) {
2084 updateListener.onThumbnailUpdated(this);
2085 }
2086 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07002087 }
2088
2089 @Override
2090 public void onNewPicture(WebView view, Picture picture) {
John Reck1cf4b792011-07-26 10:22:22 -07002091 postCapture();
2092 }
2093
2094 private void postCapture() {
Michael Kolb9ef259a2011-07-12 15:33:08 -07002095 if (!mHandler.hasMessages(MSG_CAPTURE)) {
2096 mHandler.sendEmptyMessageDelayed(MSG_CAPTURE, CAPTURE_DELAY);
2097 }
2098 }
2099
John Reckef654f12011-07-12 16:42:08 -07002100 public boolean canGoBack() {
2101 return mMainView != null ? mMainView.canGoBack() : false;
2102 }
2103
2104 public boolean canGoForward() {
2105 return mMainView != null ? mMainView.canGoForward() : false;
2106 }
2107
2108 public void goBack() {
2109 if (mMainView != null) {
2110 mMainView.goBack();
2111 }
2112 }
2113
2114 public void goForward() {
2115 if (mMainView != null) {
2116 mMainView.goForward();
2117 }
2118 }
2119
John Reck1cf4b792011-07-26 10:22:22 -07002120 protected void persistThumbnail() {
John Reck4eadc342011-10-31 14:04:10 -07002121 DataController.getInstance(mContext).saveThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07002122 }
2123
2124 protected void deleteThumbnail() {
John Reck4eadc342011-10-31 14:04:10 -07002125 DataController.getInstance(mContext).deleteThumbnail(this);
John Reck1cf4b792011-07-26 10:22:22 -07002126 }
2127
John Reck4eadc342011-10-31 14:04:10 -07002128 void updateCaptureFromBlob(byte[] blob) {
John Reck1cf4b792011-07-26 10:22:22 -07002129 synchronized (Tab.this) {
2130 if (mCapture == null) {
2131 return;
2132 }
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002133 ByteBuffer buffer = ByteBuffer.wrap(blob);
Michael Kolbd837a112011-08-09 14:00:18 -07002134 try {
2135 mCapture.copyPixelsFromBuffer(buffer);
2136 } catch (RuntimeException rex) {
2137 Log.e(LOGTAG, "Load capture has mismatched sizes; buffer: "
2138 + buffer.capacity() + " blob: " + blob.length
2139 + "capture: " + mCapture.getByteCount());
2140 throw rex;
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002141 }
John Reck1cf4b792011-07-26 10:22:22 -07002142 }
2143 }
2144
John Reck52be4782011-08-26 15:37:29 -07002145 @Override
2146 public String toString() {
2147 StringBuilder builder = new StringBuilder(100);
2148 builder.append(mId);
2149 builder.append(") has parent: ");
2150 if (getParent() != null) {
2151 builder.append("true[");
2152 builder.append(getParent().getId());
2153 builder.append("]");
2154 } else {
2155 builder.append("false");
2156 }
2157 builder.append(", incog: ");
2158 builder.append(isPrivateBrowsingEnabled());
2159 if (!isPrivateBrowsingEnabled()) {
2160 builder.append(", title: ");
2161 builder.append(getTitle());
2162 builder.append(", url: ");
2163 builder.append(getUrl());
2164 }
2165 return builder.toString();
2166 }
2167
Steve Block4895b012011-10-03 16:26:46 +01002168 private void handleProceededAfterSslError(SslError error) {
2169 if (error.getUrl().equals(mCurrentState.mUrl)) {
2170 // The security state should currently be SECURITY_STATE_SECURE.
2171 setSecurityState(SecurityState.SECURITY_STATE_BAD_CERTIFICATE);
Steve Block08a6f0c2011-10-06 12:12:53 +01002172 mCurrentState.mSslCertificateError = error;
Steve Block4895b012011-10-03 16:26:46 +01002173 } else if (getSecurityState() == SecurityState.SECURITY_STATE_SECURE) {
Steve Block08a6f0c2011-10-06 12:12:53 +01002174 // The page's main resource is secure and this error is for a
2175 // sub-resource.
Steve Block4895b012011-10-03 16:26:46 +01002176 setSecurityState(SecurityState.SECURITY_STATE_MIXED);
2177 }
2178 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002179}