Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.browser; |
| 18 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 19 | import android.app.Activity; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 20 | import android.app.AlertDialog; |
| 21 | import android.content.ContentResolver; |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 22 | import android.content.ContentValues; |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 23 | import android.content.Context; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 24 | import android.content.DialogInterface; |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 25 | import android.content.DialogInterface.OnCancelListener; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 26 | import android.graphics.Bitmap; |
John Reck | 8cc9235 | 2011-07-06 17:41:52 -0700 | [diff] [blame] | 27 | import android.graphics.Bitmap.CompressFormat; |
Michael Kolb | 9ef259a | 2011-07-12 15:33:08 -0700 | [diff] [blame] | 28 | import android.graphics.BitmapFactory; |
| 29 | import android.graphics.Canvas; |
Michael Kolb | c3af067 | 2011-08-09 10:24:41 -0700 | [diff] [blame] | 30 | import android.graphics.Color; |
Michael Kolb | a3194d0 | 2011-09-07 11:23:51 -0700 | [diff] [blame] | 31 | import android.graphics.Paint; |
Michael Kolb | 9ef259a | 2011-07-12 15:33:08 -0700 | [diff] [blame] | 32 | import android.graphics.Picture; |
Michael Kolb | a3194d0 | 2011-09-07 11:23:51 -0700 | [diff] [blame] | 33 | import android.graphics.PorterDuff; |
| 34 | import android.graphics.PorterDuffXfermode; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 35 | import android.net.Uri; |
| 36 | import android.net.http.SslError; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 37 | import android.os.Bundle; |
Michael Kolb | 9ef259a | 2011-07-12 15:33:08 -0700 | [diff] [blame] | 38 | import android.os.Handler; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 39 | import android.os.Message; |
Kristian Monsen | 4dce3bf | 2010-02-02 13:37:09 +0000 | [diff] [blame] | 40 | import android.os.SystemClock; |
Brian Carlstrom | 8862c1d | 2011-06-02 01:05:55 -0700 | [diff] [blame] | 41 | import android.security.KeyChain; |
Brian Carlstrom | aa09cd8 | 2011-06-09 16:04:40 -0700 | [diff] [blame] | 42 | import android.security.KeyChainAliasCallback; |
John Reck | 24f1826 | 2011-06-17 14:47:20 -0700 | [diff] [blame] | 43 | import android.text.TextUtils; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 44 | import android.util.Log; |
| 45 | import android.view.KeyEvent; |
| 46 | import android.view.LayoutInflater; |
| 47 | import android.view.View; |
Grace Kloba | 50c241e | 2010-04-20 11:07:50 -0700 | [diff] [blame] | 48 | import android.view.ViewStub; |
Selim Gurun | 0b3d66f | 2012-08-29 13:08:13 -0700 | [diff] [blame] | 49 | import android.webkit.BrowserDownloadListener; |
Brian Carlstrom | 8862c1d | 2011-06-02 01:05:55 -0700 | [diff] [blame] | 50 | import android.webkit.ClientCertRequestHandler; |
Ben Murdoch | c42addf | 2010-01-28 15:19:59 +0000 | [diff] [blame] | 51 | import android.webkit.ConsoleMessage; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 52 | import android.webkit.GeolocationPermissions; |
| 53 | import android.webkit.HttpAuthHandler; |
| 54 | import android.webkit.SslErrorHandler; |
| 55 | import android.webkit.URLUtil; |
| 56 | import android.webkit.ValueCallback; |
John Reck | 6c2e2f3 | 2011-08-22 13:41:23 -0700 | [diff] [blame] | 57 | import android.webkit.WebBackForwardList; |
Leon Scroggins | 0c75a8e | 2010-03-03 16:40:58 -0500 | [diff] [blame] | 58 | import android.webkit.WebBackForwardListClient; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 59 | import android.webkit.WebChromeClient; |
| 60 | import android.webkit.WebHistoryItem; |
John Reck | 438bf46 | 2011-01-12 18:11:46 -0800 | [diff] [blame] | 61 | import android.webkit.WebResourceResponse; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 62 | import android.webkit.WebStorage; |
| 63 | import android.webkit.WebView; |
Michael Kolb | 9ef259a | 2011-07-12 15:33:08 -0700 | [diff] [blame] | 64 | import android.webkit.WebView.PictureListener; |
John Reck | 2b71d6d | 2012-04-18 17:42:06 -0700 | [diff] [blame] | 65 | import android.webkit.WebViewClassic; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 66 | import android.webkit.WebViewClient; |
Jonathan Dixon | f4ff149 | 2012-06-08 10:15:32 -0700 | [diff] [blame] | 67 | import android.webkit.WebViewClientClassicExt; |
Ben Murdoch | 1d676b6 | 2011-01-17 12:54:24 +0000 | [diff] [blame] | 68 | import android.widget.CheckBox; |
Ben Murdoch | 8029a77 | 2010-11-16 11:58:21 +0000 | [diff] [blame] | 69 | import android.widget.Toast; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 70 | |
John Reck | 8ee633f | 2011-08-09 16:00:35 -0700 | [diff] [blame] | 71 | import com.android.browser.TabControl.OnThumbnailUpdatedListener; |
John Reck | 541f55a | 2011-06-07 16:34:43 -0700 | [diff] [blame] | 72 | import com.android.browser.homepages.HomeProvider; |
John Reck | 8cc9235 | 2011-07-06 17:41:52 -0700 | [diff] [blame] | 73 | import com.android.browser.provider.SnapshotProvider.Snapshots; |
John Reck | 541f55a | 2011-06-07 16:34:43 -0700 | [diff] [blame] | 74 | |
| 75 | import java.io.ByteArrayOutputStream; |
John Reck | 2b71d6d | 2012-04-18 17:42:06 -0700 | [diff] [blame] | 76 | import java.io.File; |
John Reck | 68234a9 | 2012-04-19 15:27:12 -0700 | [diff] [blame] | 77 | import java.io.IOException; |
John Reck | 2b71d6d | 2012-04-18 17:42:06 -0700 | [diff] [blame] | 78 | import java.io.OutputStream; |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 79 | import java.nio.ByteBuffer; |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 80 | import java.util.LinkedList; |
| 81 | import java.util.Map; |
John Reck | 2b71d6d | 2012-04-18 17:42:06 -0700 | [diff] [blame] | 82 | import java.util.UUID; |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 83 | import java.util.Vector; |
Mathew Inwood | a829d55 | 2011-09-02 14:16:25 +0100 | [diff] [blame] | 84 | import java.util.regex.Pattern; |
John Reck | 8cc9235 | 2011-07-06 17:41:52 -0700 | [diff] [blame] | 85 | import java.util.zip.GZIPOutputStream; |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 86 | |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 87 | /** |
| 88 | * Class for maintaining Tabs with a main WebView and a subwindow. |
| 89 | */ |
Michael Kolb | 9ef259a | 2011-07-12 15:33:08 -0700 | [diff] [blame] | 90 | class Tab implements PictureListener { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 91 | |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 92 | // Log Tag |
| 93 | private static final String LOGTAG = "Tab"; |
Mathew Inwood | 1dd8e82 | 2011-08-03 14:34:29 +0100 | [diff] [blame] | 94 | private static final boolean LOGD_ENABLED = com.android.browser.Browser.LOGD_ENABLED; |
Ben Murdoch | c42addf | 2010-01-28 15:19:59 +0000 | [diff] [blame] | 95 | // Special case the logtag for messages for the Console to make it easier to |
| 96 | // filter them and match the logtag used for these messages in older versions |
| 97 | // of the browser. |
| 98 | private static final String CONSOLE_LOGTAG = "browser"; |
| 99 | |
Michael Kolb | 9ef259a | 2011-07-12 15:33:08 -0700 | [diff] [blame] | 100 | private static final int MSG_CAPTURE = 42; |
John Reck | 8ee633f | 2011-08-09 16:00:35 -0700 | [diff] [blame] | 101 | private static final int CAPTURE_DELAY = 100; |
Michael Kolb | a53c989 | 2011-10-05 13:31:40 -0700 | [diff] [blame] | 102 | private static final int INITIAL_PROGRESS = 5; |
Michael Kolb | 9ef259a | 2011-07-12 15:33:08 -0700 | [diff] [blame] | 103 | |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 104 | private static Bitmap sDefaultFavicon; |
| 105 | |
Michael Kolb | a3194d0 | 2011-09-07 11:23:51 -0700 | [diff] [blame] | 106 | private static Paint sAlphaPaint = new Paint(); |
| 107 | static { |
| 108 | sAlphaPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR)); |
| 109 | sAlphaPaint.setColor(Color.TRANSPARENT); |
| 110 | } |
| 111 | |
Steve Block | 2466eff | 2011-10-03 15:33:09 +0100 | [diff] [blame] | 112 | public enum SecurityState { |
Steve Block | 4895b01 | 2011-10-03 16:26:46 +0100 | [diff] [blame] | 113 | // The page's main resource does not use SSL. Note that we use this |
| 114 | // state irrespective of the SSL authentication state of sub-resources. |
Steve Block | 2466eff | 2011-10-03 15:33:09 +0100 | [diff] [blame] | 115 | SECURITY_STATE_NOT_SECURE, |
Steve Block | 4895b01 | 2011-10-03 16:26:46 +0100 | [diff] [blame] | 116 | // The page's main resource uses SSL and the certificate is good. The |
| 117 | // same is true of all sub-resources. |
Steve Block | 2466eff | 2011-10-03 15:33:09 +0100 | [diff] [blame] | 118 | SECURITY_STATE_SECURE, |
Steve Block | 4895b01 | 2011-10-03 16:26:46 +0100 | [diff] [blame] | 119 | // The page's main resource uses SSL and the certificate is good, but |
| 120 | // some sub-resources either do not use SSL or have problems with their |
| 121 | // certificates. |
Steve Block | 2466eff | 2011-10-03 15:33:09 +0100 | [diff] [blame] | 122 | SECURITY_STATE_MIXED, |
Steve Block | 4895b01 | 2011-10-03 16:26:46 +0100 | [diff] [blame] | 123 | // The page's main resource uses SSL but there is a problem with its |
| 124 | // certificate. |
| 125 | SECURITY_STATE_BAD_CERTIFICATE, |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 126 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 127 | |
Michael Kolb | 1461244 | 2011-06-24 13:06:29 -0700 | [diff] [blame] | 128 | Context mContext; |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 129 | protected WebViewController mWebViewController; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 130 | |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 131 | // The tab ID |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 132 | private long mId = -1; |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 133 | |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 134 | // The Geolocation permissions prompt |
| 135 | private GeolocationPermissionsPrompt mGeolocationPermissionsPrompt; |
| 136 | // Main WebView wrapper |
Michael Kolb | a713ec8 | 2010-11-29 17:27:06 -0800 | [diff] [blame] | 137 | private View mContainer; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 138 | // Main WebView |
| 139 | private WebView mMainView; |
| 140 | // Subwindow container |
| 141 | private View mSubViewContainer; |
| 142 | // Subwindow WebView |
| 143 | private WebView mSubView; |
| 144 | // Saved bundle for when we are running low on memory. It contains the |
| 145 | // information needed to restore the WebView if the user goes back to the |
| 146 | // tab. |
| 147 | private Bundle mSavedState; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 148 | // Parent Tab. This is the Tab that created this Tab, or null if the Tab was |
| 149 | // created by the UI |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 150 | private Tab mParent; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 151 | // Tab that constructed by this Tab. This is used when this Tab is |
| 152 | // destroyed, it clears all mParentTab values in the children. |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 153 | private Vector<Tab> mChildren; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 154 | // If true, the tab is in the foreground of the current activity. |
| 155 | private boolean mInForeground; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 156 | // If true, the tab is in page loading state (after onPageStarted, |
| 157 | // before onPageFinsihed) |
| 158 | private boolean mInPageLoad; |
John Reck | 38b3965 | 2012-06-05 09:22:59 -0700 | [diff] [blame] | 159 | private boolean mDisableOverrideUrlLoading; |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 160 | // The last reported progress of the current page |
| 161 | private int mPageLoadProgress; |
Kristian Monsen | 4dce3bf | 2010-02-02 13:37:09 +0000 | [diff] [blame] | 162 | // The time the load started, used to find load page time |
| 163 | private long mLoadStartTime; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 164 | // Application identifier used to find tabs that another application wants |
| 165 | // to reuse. |
| 166 | private String mAppId; |
Michael Kolb | e28b347 | 2011-08-04 16:54:31 -0700 | [diff] [blame] | 167 | // flag to indicate if tab should be closed on back |
| 168 | private boolean mCloseOnBack; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 169 | // Keep the original url around to avoid killing the old WebView if the url |
| 170 | // has not changed. |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 171 | // Error console for the tab |
| 172 | private ErrorConsoleView mErrorConsole; |
Leon Scroggins | dcc5eeb | 2010-02-23 17:26:37 -0500 | [diff] [blame] | 173 | // The listener that gets invoked when a download is started from the |
| 174 | // mMainView |
Selim Gurun | 0b3d66f | 2012-08-29 13:08:13 -0700 | [diff] [blame] | 175 | private final BrowserDownloadListener mDownloadListener; |
Leon Scroggins | 0c75a8e | 2010-03-03 16:40:58 -0500 | [diff] [blame] | 176 | // Listener used to know when we move forward or back in the history list. |
| 177 | private final WebBackForwardListClient mWebBackForwardListClient; |
John Reck | e969cc5 | 2010-12-21 17:24:43 -0800 | [diff] [blame] | 178 | private DataController mDataController; |
Patrick Scott | 9206677 | 2011-03-10 08:46:27 -0500 | [diff] [blame] | 179 | // State of the auto-login request. |
| 180 | private DeviceAccountLogin mDeviceAccountLogin; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 181 | |
| 182 | // AsyncTask for downloading touch icons |
| 183 | DownloadTouchIcon mTouchIconLoader; |
| 184 | |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 185 | private BrowserSettings mSettings; |
Michael Kolb | 9ef259a | 2011-07-12 15:33:08 -0700 | [diff] [blame] | 186 | private int mCaptureWidth; |
| 187 | private int mCaptureHeight; |
| 188 | private Bitmap mCapture; |
| 189 | private Handler mHandler; |
Michael Kolb | 7286427 | 2012-05-03 15:42:15 -0700 | [diff] [blame] | 190 | private boolean mUpdateThumbnail; |
Michael Kolb | 9ef259a | 2011-07-12 15:33:08 -0700 | [diff] [blame] | 191 | |
Mathew Inwood | 1dd8e82 | 2011-08-03 14:34:29 +0100 | [diff] [blame] | 192 | /** |
Mathew Inwood | 9ad1eac | 2011-09-15 11:29:50 +0100 | [diff] [blame] | 193 | * See {@link #clearBackStackWhenItemAdded(String)}. |
Mathew Inwood | 1dd8e82 | 2011-08-03 14:34:29 +0100 | [diff] [blame] | 194 | */ |
Mathew Inwood | a829d55 | 2011-09-02 14:16:25 +0100 | [diff] [blame] | 195 | private Pattern mClearHistoryUrlPattern; |
Mathew Inwood | 1dd8e82 | 2011-08-03 14:34:29 +0100 | [diff] [blame] | 196 | |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 197 | private static synchronized Bitmap getDefaultFavicon(Context context) { |
| 198 | if (sDefaultFavicon == null) { |
| 199 | sDefaultFavicon = BitmapFactory.decodeResource( |
| 200 | context.getResources(), R.drawable.app_web_browser_sm); |
| 201 | } |
| 202 | return sDefaultFavicon; |
| 203 | } |
Michael Kolb | eb95db4 | 2011-03-03 10:38:40 -0800 | [diff] [blame] | 204 | |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 205 | // All the state needed for a page |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 206 | protected static class PageState { |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 207 | String mUrl; |
John Reck | db22ec4 | 2011-06-29 11:31:24 -0700 | [diff] [blame] | 208 | String mOriginalUrl; |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 209 | String mTitle; |
Steve Block | 2466eff | 2011-10-03 15:33:09 +0100 | [diff] [blame] | 210 | SecurityState mSecurityState; |
Steve Block | 08a6f0c | 2011-10-06 12:12:53 +0100 | [diff] [blame] | 211 | // This is non-null only when mSecurityState is SECURITY_STATE_BAD_CERTIFICATE. |
| 212 | SslError mSslCertificateError; |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 213 | Bitmap mFavicon; |
Steve Block | 08a6f0c | 2011-10-06 12:12:53 +0100 | [diff] [blame] | 214 | boolean mIsBookmarkedSite; |
| 215 | boolean mIncognito; |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 216 | |
| 217 | PageState(Context c, boolean incognito) { |
John Reck | 502a353 | 2011-08-16 14:21:46 -0700 | [diff] [blame] | 218 | mIncognito = incognito; |
| 219 | if (mIncognito) { |
John Reck | db22ec4 | 2011-06-29 11:31:24 -0700 | [diff] [blame] | 220 | mOriginalUrl = mUrl = "browser:incognito"; |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 221 | mTitle = c.getString(R.string.new_incognito_tab); |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 222 | } else { |
John Reck | db22ec4 | 2011-06-29 11:31:24 -0700 | [diff] [blame] | 223 | mOriginalUrl = mUrl = ""; |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 224 | mTitle = c.getString(R.string.new_tab); |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 225 | } |
Steve Block | 2466eff | 2011-10-03 15:33:09 +0100 | [diff] [blame] | 226 | mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE; |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | PageState(Context c, boolean incognito, String url, Bitmap favicon) { |
John Reck | 502a353 | 2011-08-16 14:21:46 -0700 | [diff] [blame] | 230 | mIncognito = incognito; |
John Reck | db22ec4 | 2011-06-29 11:31:24 -0700 | [diff] [blame] | 231 | mOriginalUrl = mUrl = url; |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 232 | if (URLUtil.isHttpsUrl(url)) { |
Steve Block | 2466eff | 2011-10-03 15:33:09 +0100 | [diff] [blame] | 233 | mSecurityState = SecurityState.SECURITY_STATE_SECURE; |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 234 | } else { |
Steve Block | 2466eff | 2011-10-03 15:33:09 +0100 | [diff] [blame] | 235 | mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE; |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 236 | } |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 237 | mFavicon = favicon; |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 238 | } |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 239 | |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 240 | } |
| 241 | |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 242 | // The current/loading page's state |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 243 | protected PageState mCurrentState; |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 244 | |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 245 | // Used for saving and restoring each Tab |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 246 | static final String ID = "ID"; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 247 | static final String CURRURL = "currentUrl"; |
| 248 | static final String CURRTITLE = "currentTitle"; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 249 | static final String PARENTTAB = "parentTab"; |
| 250 | static final String APPID = "appid"; |
Elliott Slaughter | 3d6df16 | 2010-08-25 13:17:44 -0700 | [diff] [blame] | 251 | static final String INCOGNITO = "privateBrowsingEnabled"; |
John Reck | b0a86db | 2011-05-24 14:05:58 -0700 | [diff] [blame] | 252 | static final String USERAGENT = "useragent"; |
Michael Kolb | e28b347 | 2011-08-04 16:54:31 -0700 | [diff] [blame] | 253 | static final String CLOSEFLAG = "closeOnBack"; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 254 | |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 255 | // Container class for the next error dialog that needs to be displayed |
| 256 | private class ErrorDialog { |
| 257 | public final int mTitle; |
| 258 | public final String mDescription; |
| 259 | public final int mError; |
| 260 | ErrorDialog(int title, String desc, int error) { |
| 261 | mTitle = title; |
| 262 | mDescription = desc; |
| 263 | mError = error; |
| 264 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 265 | } |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 266 | |
| 267 | private void processNextError() { |
| 268 | if (mQueuedErrors == null) { |
| 269 | return; |
| 270 | } |
| 271 | // The first one is currently displayed so just remove it. |
| 272 | mQueuedErrors.removeFirst(); |
| 273 | if (mQueuedErrors.size() == 0) { |
| 274 | mQueuedErrors = null; |
| 275 | return; |
| 276 | } |
| 277 | showError(mQueuedErrors.getFirst()); |
| 278 | } |
| 279 | |
| 280 | private DialogInterface.OnDismissListener mDialogListener = |
| 281 | new DialogInterface.OnDismissListener() { |
| 282 | public void onDismiss(DialogInterface d) { |
| 283 | processNextError(); |
| 284 | } |
| 285 | }; |
| 286 | private LinkedList<ErrorDialog> mQueuedErrors; |
| 287 | |
| 288 | private void queueError(int err, String desc) { |
| 289 | if (mQueuedErrors == null) { |
| 290 | mQueuedErrors = new LinkedList<ErrorDialog>(); |
| 291 | } |
| 292 | for (ErrorDialog d : mQueuedErrors) { |
| 293 | if (d.mError == err) { |
| 294 | // Already saw a similar error, ignore the new one. |
| 295 | return; |
| 296 | } |
| 297 | } |
| 298 | ErrorDialog errDialog = new ErrorDialog( |
| 299 | err == WebViewClient.ERROR_FILE_NOT_FOUND ? |
| 300 | R.string.browserFrameFileErrorLabel : |
| 301 | R.string.browserFrameNetworkErrorLabel, |
| 302 | desc, err); |
| 303 | mQueuedErrors.addLast(errDialog); |
| 304 | |
| 305 | // Show the dialog now if the queue was empty and it is in foreground |
| 306 | if (mQueuedErrors.size() == 1 && mInForeground) { |
| 307 | showError(errDialog); |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | private void showError(ErrorDialog errDialog) { |
| 312 | if (mInForeground) { |
Michael Kolb | 1461244 | 2011-06-24 13:06:29 -0700 | [diff] [blame] | 313 | AlertDialog d = new AlertDialog.Builder(mContext) |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 314 | .setTitle(errDialog.mTitle) |
| 315 | .setMessage(errDialog.mDescription) |
| 316 | .setPositiveButton(R.string.ok, null) |
| 317 | .create(); |
| 318 | d.setOnDismissListener(mDialogListener); |
| 319 | d.show(); |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | // ------------------------------------------------------------------------- |
| 324 | // WebViewClient implementation for the main WebView |
| 325 | // ------------------------------------------------------------------------- |
| 326 | |
Jonathan Dixon | f4ff149 | 2012-06-08 10:15:32 -0700 | [diff] [blame] | 327 | private final WebViewClientClassicExt mWebViewClient = new WebViewClientClassicExt() { |
Leon Scroggins | 4a64a8a | 2010-03-02 17:57:40 -0500 | [diff] [blame] | 328 | private Message mDontResend; |
| 329 | private Message mResend; |
Michael Kolb | 47bd1e4 | 2011-09-01 15:25:00 -0700 | [diff] [blame] | 330 | |
| 331 | private boolean providersDiffer(String url, String otherUrl) { |
| 332 | Uri uri1 = Uri.parse(url); |
| 333 | Uri uri2 = Uri.parse(otherUrl); |
| 334 | return !uri1.getEncodedAuthority().equals(uri2.getEncodedAuthority()); |
| 335 | } |
| 336 | |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 337 | @Override |
| 338 | public void onPageStarted(WebView view, String url, Bitmap favicon) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 339 | mInPageLoad = true; |
Michael Kolb | 7286427 | 2012-05-03 15:42:15 -0700 | [diff] [blame] | 340 | mUpdateThumbnail = true; |
Michael Kolb | a53c989 | 2011-10-05 13:31:40 -0700 | [diff] [blame] | 341 | mPageLoadProgress = INITIAL_PROGRESS; |
Michael Kolb | 1461244 | 2011-06-24 13:06:29 -0700 | [diff] [blame] | 342 | mCurrentState = new PageState(mContext, |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 343 | view.isPrivateBrowsingEnabled(), url, favicon); |
Kristian Monsen | 4dce3bf | 2010-02-02 13:37:09 +0000 | [diff] [blame] | 344 | mLoadStartTime = SystemClock.uptimeMillis(); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 345 | |
| 346 | // If we start a touch icon load and then load a new page, we don't |
| 347 | // want to cancel the current touch icon loader. But, we do want to |
| 348 | // create a new one when the touch icon url is known. |
| 349 | if (mTouchIconLoader != null) { |
| 350 | mTouchIconLoader.mTab = null; |
| 351 | mTouchIconLoader = null; |
| 352 | } |
| 353 | |
| 354 | // reset the error console |
| 355 | if (mErrorConsole != null) { |
| 356 | mErrorConsole.clearErrorMessages(); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 357 | if (mWebViewController.shouldShowErrorConsole()) { |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 358 | mErrorConsole.showConsole(ErrorConsoleView.SHOW_NONE); |
| 359 | } |
| 360 | } |
| 361 | |
Patrick Scott | 9206677 | 2011-03-10 08:46:27 -0500 | [diff] [blame] | 362 | // Cancel the auto-login process. |
| 363 | if (mDeviceAccountLogin != null) { |
| 364 | mDeviceAccountLogin.cancel(); |
| 365 | mDeviceAccountLogin = null; |
| 366 | mWebViewController.hideAutoLogin(Tab.this); |
| 367 | } |
| 368 | |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 369 | // finally update the UI in the activity if it is in the foreground |
John Reck | 324d440 | 2011-01-11 16:56:42 -0800 | [diff] [blame] | 370 | mWebViewController.onPageStarted(Tab.this, view, favicon); |
Leon Scroggins | 4cd9779 | 2010-12-03 15:31:56 -0500 | [diff] [blame] | 371 | |
John Reck | e969cc5 | 2010-12-21 17:24:43 -0800 | [diff] [blame] | 372 | updateBookmarkedStatus(); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | @Override |
| 376 | public void onPageFinished(WebView view, String url) { |
John Reck | 38b3965 | 2012-06-05 09:22:59 -0700 | [diff] [blame] | 377 | mDisableOverrideUrlLoading = false; |
John Reck | 5b69184 | 2010-11-29 11:21:13 -0800 | [diff] [blame] | 378 | if (!isPrivateBrowsingEnabled()) { |
| 379 | LogTag.logPageFinishedLoading( |
| 380 | url, SystemClock.uptimeMillis() - mLoadStartTime); |
| 381 | } |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 382 | syncCurrentState(view, url); |
John Reck | 324d440 | 2011-01-11 16:56:42 -0800 | [diff] [blame] | 383 | mWebViewController.onPageFinished(Tab.this); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | // return true if want to hijack the url to let another app to handle it |
| 387 | @Override |
| 388 | public boolean shouldOverrideUrlLoading(WebView view, String url) { |
John Reck | 38b3965 | 2012-06-05 09:22:59 -0700 | [diff] [blame] | 389 | if (!mDisableOverrideUrlLoading && mInForeground) { |
Michael Kolb | 18eb377 | 2010-12-10 14:29:51 -0800 | [diff] [blame] | 390 | return mWebViewController.shouldOverrideUrlLoading(Tab.this, |
| 391 | view, url); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 392 | } else { |
| 393 | return false; |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | /** |
Steve Block | 2466eff | 2011-10-03 15:33:09 +0100 | [diff] [blame] | 398 | * Updates the security state. This method is called when we discover |
| 399 | * another resource to be loaded for this page (for example, |
| 400 | * javascript). While we update the security state, we do not update |
| 401 | * the lock icon until we are done loading, as it is slightly more |
| 402 | * secure this way. |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 403 | */ |
| 404 | @Override |
| 405 | public void onLoadResource(WebView view, String url) { |
| 406 | if (url != null && url.length() > 0) { |
| 407 | // It is only if the page claims to be secure that we may have |
Steve Block | 2466eff | 2011-10-03 15:33:09 +0100 | [diff] [blame] | 408 | // to update the security state: |
| 409 | if (mCurrentState.mSecurityState == SecurityState.SECURITY_STATE_SECURE) { |
| 410 | // If NOT a 'safe' url, change the state to mixed content! |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 411 | if (!(URLUtil.isHttpsUrl(url) || URLUtil.isDataUrl(url) |
| 412 | || URLUtil.isAboutUrl(url))) { |
Steve Block | 2466eff | 2011-10-03 15:33:09 +0100 | [diff] [blame] | 413 | mCurrentState.mSecurityState = SecurityState.SECURITY_STATE_MIXED; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 414 | } |
| 415 | } |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | /** |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 420 | * Show a dialog informing the user of the network error reported by |
| 421 | * WebCore if it is in the foreground. |
| 422 | */ |
| 423 | @Override |
| 424 | public void onReceivedError(WebView view, int errorCode, |
| 425 | String description, String failingUrl) { |
| 426 | if (errorCode != WebViewClient.ERROR_HOST_LOOKUP && |
| 427 | errorCode != WebViewClient.ERROR_CONNECT && |
| 428 | errorCode != WebViewClient.ERROR_BAD_URL && |
| 429 | errorCode != WebViewClient.ERROR_UNSUPPORTED_SCHEME && |
| 430 | errorCode != WebViewClient.ERROR_FILE) { |
| 431 | queueError(errorCode, description); |
Jeff Hamilton | 47654f4 | 2010-09-07 09:57:51 -0500 | [diff] [blame] | 432 | |
Selim Gurun | 3da06b8 | 2011-10-10 13:58:12 -0700 | [diff] [blame] | 433 | // Don't log URLs when in private browsing mode |
| 434 | if (!isPrivateBrowsingEnabled()) { |
| 435 | Log.e(LOGTAG, "onReceivedError " + errorCode + " " + failingUrl |
Jeff Hamilton | 47654f4 | 2010-09-07 09:57:51 -0500 | [diff] [blame] | 436 | + " " + description); |
Selim Gurun | 3da06b8 | 2011-10-10 13:58:12 -0700 | [diff] [blame] | 437 | } |
Jeff Hamilton | 47654f4 | 2010-09-07 09:57:51 -0500 | [diff] [blame] | 438 | } |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | /** |
| 442 | * Check with the user if it is ok to resend POST data as the page they |
| 443 | * are trying to navigate to is the result of a POST. |
| 444 | */ |
| 445 | @Override |
| 446 | public void onFormResubmission(WebView view, final Message dontResend, |
| 447 | final Message resend) { |
| 448 | if (!mInForeground) { |
| 449 | dontResend.sendToTarget(); |
| 450 | return; |
| 451 | } |
Leon Scroggins | 4a64a8a | 2010-03-02 17:57:40 -0500 | [diff] [blame] | 452 | if (mDontResend != null) { |
| 453 | Log.w(LOGTAG, "onFormResubmission should not be called again " |
| 454 | + "while dialog is still up"); |
| 455 | dontResend.sendToTarget(); |
| 456 | return; |
| 457 | } |
| 458 | mDontResend = dontResend; |
| 459 | mResend = resend; |
Michael Kolb | 1461244 | 2011-06-24 13:06:29 -0700 | [diff] [blame] | 460 | new AlertDialog.Builder(mContext).setTitle( |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 461 | R.string.browserFrameFormResubmitLabel).setMessage( |
| 462 | R.string.browserFrameFormResubmitMessage) |
| 463 | .setPositiveButton(R.string.ok, |
| 464 | new DialogInterface.OnClickListener() { |
| 465 | public void onClick(DialogInterface dialog, |
| 466 | int which) { |
Leon Scroggins | 4a64a8a | 2010-03-02 17:57:40 -0500 | [diff] [blame] | 467 | if (mResend != null) { |
| 468 | mResend.sendToTarget(); |
| 469 | mResend = null; |
| 470 | mDontResend = null; |
| 471 | } |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 472 | } |
| 473 | }).setNegativeButton(R.string.cancel, |
| 474 | new DialogInterface.OnClickListener() { |
| 475 | public void onClick(DialogInterface dialog, |
| 476 | int which) { |
Leon Scroggins | 4a64a8a | 2010-03-02 17:57:40 -0500 | [diff] [blame] | 477 | if (mDontResend != null) { |
| 478 | mDontResend.sendToTarget(); |
| 479 | mResend = null; |
| 480 | mDontResend = null; |
| 481 | } |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 482 | } |
| 483 | }).setOnCancelListener(new OnCancelListener() { |
| 484 | public void onCancel(DialogInterface dialog) { |
Leon Scroggins | 4a64a8a | 2010-03-02 17:57:40 -0500 | [diff] [blame] | 485 | if (mDontResend != null) { |
| 486 | mDontResend.sendToTarget(); |
| 487 | mResend = null; |
| 488 | mDontResend = null; |
| 489 | } |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 490 | } |
| 491 | }).show(); |
| 492 | } |
| 493 | |
| 494 | /** |
| 495 | * Insert the url into the visited history database. |
| 496 | * @param url The url to be inserted. |
| 497 | * @param isReload True if this url is being reloaded. |
| 498 | * FIXME: Not sure what to do when reloading the page. |
| 499 | */ |
| 500 | @Override |
| 501 | public void doUpdateVisitedHistory(WebView view, String url, |
| 502 | boolean isReload) { |
John Reck | 324d440 | 2011-01-11 16:56:42 -0800 | [diff] [blame] | 503 | mWebViewController.doUpdateVisitedHistory(Tab.this, isReload); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | /** |
| 507 | * Displays SSL error(s) dialog to the user. |
| 508 | */ |
| 509 | @Override |
| 510 | public void onReceivedSslError(final WebView view, |
| 511 | final SslErrorHandler handler, final SslError error) { |
| 512 | if (!mInForeground) { |
| 513 | handler.cancel(); |
Steve Block | 2466eff | 2011-10-03 15:33:09 +0100 | [diff] [blame] | 514 | setSecurityState(SecurityState.SECURITY_STATE_NOT_SECURE); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 515 | return; |
| 516 | } |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 517 | if (mSettings.showSecurityWarnings()) { |
John Reck | cb28b2c | 2011-08-26 17:39:44 -0700 | [diff] [blame] | 518 | new AlertDialog.Builder(mContext) |
| 519 | .setTitle(R.string.security_warning) |
| 520 | .setMessage(R.string.ssl_warnings_header) |
Björn Lundén | 2aa8ba2 | 2012-05-31 23:05:56 +0200 | [diff] [blame] | 521 | .setIconAttribute(android.R.attr.alertDialogIcon) |
John Reck | cb28b2c | 2011-08-26 17:39:44 -0700 | [diff] [blame] | 522 | .setPositiveButton(R.string.ssl_continue, |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 523 | new DialogInterface.OnClickListener() { |
John Reck | cb28b2c | 2011-08-26 17:39:44 -0700 | [diff] [blame] | 524 | @Override |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 525 | public void onClick(DialogInterface dialog, |
| 526 | int whichButton) { |
| 527 | handler.proceed(); |
Steve Block | 4895b01 | 2011-10-03 16:26:46 +0100 | [diff] [blame] | 528 | handleProceededAfterSslError(error); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 529 | } |
John Reck | cb28b2c | 2011-08-26 17:39:44 -0700 | [diff] [blame] | 530 | }) |
| 531 | .setNeutralButton(R.string.view_certificate, |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 532 | new DialogInterface.OnClickListener() { |
John Reck | cb28b2c | 2011-08-26 17:39:44 -0700 | [diff] [blame] | 533 | @Override |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 534 | public void onClick(DialogInterface dialog, |
| 535 | int whichButton) { |
John Reck | cb28b2c | 2011-08-26 17:39:44 -0700 | [diff] [blame] | 536 | mWebViewController.showSslCertificateOnError( |
| 537 | view, handler, error); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 538 | } |
John Reck | cb28b2c | 2011-08-26 17:39:44 -0700 | [diff] [blame] | 539 | }) |
| 540 | .setNegativeButton(R.string.ssl_go_back, |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 541 | new DialogInterface.OnClickListener() { |
John Reck | cb28b2c | 2011-08-26 17:39:44 -0700 | [diff] [blame] | 542 | @Override |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 543 | public void onClick(DialogInterface dialog, |
| 544 | int whichButton) { |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 545 | dialog.cancel(); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 546 | } |
John Reck | cb28b2c | 2011-08-26 17:39:44 -0700 | [diff] [blame] | 547 | }) |
| 548 | .setOnCancelListener( |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 549 | new DialogInterface.OnCancelListener() { |
John Reck | cb28b2c | 2011-08-26 17:39:44 -0700 | [diff] [blame] | 550 | @Override |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 551 | public void onCancel(DialogInterface dialog) { |
| 552 | handler.cancel(); |
Steve Block | 2466eff | 2011-10-03 15:33:09 +0100 | [diff] [blame] | 553 | setSecurityState(SecurityState.SECURITY_STATE_NOT_SECURE); |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 554 | mWebViewController.onUserCanceledSsl(Tab.this); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 555 | } |
John Reck | cb28b2c | 2011-08-26 17:39:44 -0700 | [diff] [blame] | 556 | }) |
| 557 | .show(); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 558 | } else { |
| 559 | handler.proceed(); |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | /** |
Steve Block | 4895b01 | 2011-10-03 16:26:46 +0100 | [diff] [blame] | 564 | * Called when an SSL error occurred while loading a resource, but the |
| 565 | * WebView but chose to proceed anyway based on a decision retained |
| 566 | * from a previous response to onReceivedSslError(). We update our |
| 567 | * security state to reflect this. |
| 568 | */ |
| 569 | @Override |
| 570 | public void onProceededAfterSslError(WebView view, SslError error) { |
| 571 | handleProceededAfterSslError(error); |
| 572 | } |
| 573 | |
| 574 | /** |
Brian Carlstrom | 8862c1d | 2011-06-02 01:05:55 -0700 | [diff] [blame] | 575 | * Displays client certificate request to the user. |
| 576 | */ |
| 577 | @Override |
| 578 | public void onReceivedClientCertRequest(final WebView view, |
| 579 | final ClientCertRequestHandler handler, final String host_and_port) { |
| 580 | if (!mInForeground) { |
| 581 | handler.ignore(); |
| 582 | return; |
| 583 | } |
Brian Carlstrom | 6d85fab | 2011-06-24 14:26:46 -0700 | [diff] [blame] | 584 | int colon = host_and_port.lastIndexOf(':'); |
| 585 | String host; |
| 586 | int port; |
| 587 | if (colon == -1) { |
| 588 | host = host_and_port; |
| 589 | port = -1; |
| 590 | } else { |
| 591 | String portString = host_and_port.substring(colon + 1); |
| 592 | try { |
| 593 | port = Integer.parseInt(portString); |
| 594 | host = host_and_port.substring(0, colon); |
| 595 | } catch (NumberFormatException e) { |
| 596 | host = host_and_port; |
| 597 | port = -1; |
| 598 | } |
| 599 | } |
Michael Kolb | 1461244 | 2011-06-24 13:06:29 -0700 | [diff] [blame] | 600 | KeyChain.choosePrivateKeyAlias( |
| 601 | mWebViewController.getActivity(), new KeyChainAliasCallback() { |
Brian Carlstrom | 8862c1d | 2011-06-02 01:05:55 -0700 | [diff] [blame] | 602 | @Override public void alias(String alias) { |
| 603 | if (alias == null) { |
| 604 | handler.cancel(); |
| 605 | return; |
| 606 | } |
Michael Kolb | 1461244 | 2011-06-24 13:06:29 -0700 | [diff] [blame] | 607 | new KeyChainLookup(mContext, handler, alias).execute(); |
Brian Carlstrom | 8862c1d | 2011-06-02 01:05:55 -0700 | [diff] [blame] | 608 | } |
Brian Carlstrom | 6d85fab | 2011-06-24 14:26:46 -0700 | [diff] [blame] | 609 | }, null, null, host, port, null); |
Brian Carlstrom | 8862c1d | 2011-06-02 01:05:55 -0700 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | /** |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 613 | * Handles an HTTP authentication request. |
| 614 | * |
| 615 | * @param handler The authentication handler |
| 616 | * @param host The host |
| 617 | * @param realm The realm |
| 618 | */ |
| 619 | @Override |
| 620 | public void onReceivedHttpAuthRequest(WebView view, |
| 621 | final HttpAuthHandler handler, final String host, |
| 622 | final String realm) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 623 | mWebViewController.onReceivedHttpAuthRequest(Tab.this, view, handler, host, realm); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | @Override |
John Reck | 438bf46 | 2011-01-12 18:11:46 -0800 | [diff] [blame] | 627 | public WebResourceResponse shouldInterceptRequest(WebView view, |
| 628 | String url) { |
| 629 | WebResourceResponse res = HomeProvider.shouldInterceptRequest( |
Michael Kolb | 1461244 | 2011-06-24 13:06:29 -0700 | [diff] [blame] | 630 | mContext, url); |
John Reck | 438bf46 | 2011-01-12 18:11:46 -0800 | [diff] [blame] | 631 | return res; |
| 632 | } |
| 633 | |
| 634 | @Override |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 635 | public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) { |
| 636 | if (!mInForeground) { |
| 637 | return false; |
| 638 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 639 | return mWebViewController.shouldOverrideKeyEvent(event); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | @Override |
| 643 | public void onUnhandledKeyEvent(WebView view, KeyEvent event) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 644 | if (!mInForeground) { |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 645 | return; |
| 646 | } |
John Reck | 997b1b7 | 2012-04-19 18:08:25 -0700 | [diff] [blame] | 647 | if (!mWebViewController.onUnhandledKeyEvent(event)) { |
| 648 | super.onUnhandledKeyEvent(view, event); |
| 649 | } |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 650 | } |
Patrick Scott | 9206677 | 2011-03-10 08:46:27 -0500 | [diff] [blame] | 651 | |
| 652 | @Override |
| 653 | public void onReceivedLoginRequest(WebView view, String realm, |
| 654 | String account, String args) { |
Michael Kolb | 1461244 | 2011-06-24 13:06:29 -0700 | [diff] [blame] | 655 | new DeviceAccountLogin(mWebViewController.getActivity(), view, Tab.this, mWebViewController) |
Patrick Scott | 9206677 | 2011-03-10 08:46:27 -0500 | [diff] [blame] | 656 | .handleLogin(realm, account, args); |
| 657 | } |
| 658 | |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 659 | }; |
| 660 | |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 661 | private void syncCurrentState(WebView view, String url) { |
| 662 | // Sync state (in case of stop/timeout) |
| 663 | mCurrentState.mUrl = view.getUrl(); |
| 664 | if (mCurrentState.mUrl == null) { |
John Reck | 8bcafc1 | 2011-08-29 16:43:02 -0700 | [diff] [blame] | 665 | mCurrentState.mUrl = ""; |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 666 | } |
| 667 | mCurrentState.mOriginalUrl = view.getOriginalUrl(); |
| 668 | mCurrentState.mTitle = view.getTitle(); |
| 669 | mCurrentState.mFavicon = view.getFavicon(); |
| 670 | if (!URLUtil.isHttpsUrl(mCurrentState.mUrl)) { |
| 671 | // In case we stop when loading an HTTPS page from an HTTP page |
| 672 | // but before a provisional load occurred |
Steve Block | 2466eff | 2011-10-03 15:33:09 +0100 | [diff] [blame] | 673 | mCurrentState.mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE; |
Steve Block | 08a6f0c | 2011-10-06 12:12:53 +0100 | [diff] [blame] | 674 | mCurrentState.mSslCertificateError = null; |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 675 | } |
John Reck | 502a353 | 2011-08-16 14:21:46 -0700 | [diff] [blame] | 676 | mCurrentState.mIncognito = view.isPrivateBrowsingEnabled(); |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 677 | } |
| 678 | |
Patrick Scott | 9206677 | 2011-03-10 08:46:27 -0500 | [diff] [blame] | 679 | // Called by DeviceAccountLogin when the Tab needs to have the auto-login UI |
| 680 | // displayed. |
| 681 | void setDeviceAccountLogin(DeviceAccountLogin login) { |
| 682 | mDeviceAccountLogin = login; |
| 683 | } |
| 684 | |
| 685 | // Returns non-null if the title bar should display the auto-login UI. |
| 686 | DeviceAccountLogin getDeviceAccountLogin() { |
| 687 | return mDeviceAccountLogin; |
| 688 | } |
| 689 | |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 690 | // ------------------------------------------------------------------------- |
| 691 | // WebChromeClient implementation for the main WebView |
| 692 | // ------------------------------------------------------------------------- |
| 693 | |
| 694 | private final WebChromeClient mWebChromeClient = new WebChromeClient() { |
| 695 | // Helper method to create a new tab or sub window. |
| 696 | private void createWindow(final boolean dialog, final Message msg) { |
| 697 | WebView.WebViewTransport transport = |
| 698 | (WebView.WebViewTransport) msg.obj; |
| 699 | if (dialog) { |
| 700 | createSubWindow(); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 701 | mWebViewController.attachSubWindow(Tab.this); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 702 | transport.setWebView(mSubView); |
| 703 | } else { |
Michael Kolb | 7bcafde | 2011-05-09 13:55:59 -0700 | [diff] [blame] | 704 | final Tab newTab = mWebViewController.openTab(null, |
John Reck | 5949c66 | 2011-05-27 09:52:29 -0700 | [diff] [blame] | 705 | Tab.this, true, true); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 706 | transport.setWebView(newTab.getWebView()); |
| 707 | } |
| 708 | msg.sendToTarget(); |
| 709 | } |
| 710 | |
| 711 | @Override |
| 712 | public boolean onCreateWindow(WebView view, final boolean dialog, |
| 713 | final boolean userGesture, final Message resultMsg) { |
| 714 | // only allow new window or sub window for the foreground case |
| 715 | if (!mInForeground) { |
| 716 | return false; |
| 717 | } |
| 718 | // Short-circuit if we can't create any more tabs or sub windows. |
| 719 | if (dialog && mSubView != null) { |
Michael Kolb | 1461244 | 2011-06-24 13:06:29 -0700 | [diff] [blame] | 720 | new AlertDialog.Builder(mContext) |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 721 | .setTitle(R.string.too_many_subwindows_dialog_title) |
Björn Lundén | 2aa8ba2 | 2012-05-31 23:05:56 +0200 | [diff] [blame] | 722 | .setIconAttribute(android.R.attr.alertDialogIcon) |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 723 | .setMessage(R.string.too_many_subwindows_dialog_message) |
| 724 | .setPositiveButton(R.string.ok, null) |
| 725 | .show(); |
| 726 | return false; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 727 | } else if (!mWebViewController.getTabControl().canCreateNewTab()) { |
Michael Kolb | 1461244 | 2011-06-24 13:06:29 -0700 | [diff] [blame] | 728 | new AlertDialog.Builder(mContext) |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 729 | .setTitle(R.string.too_many_windows_dialog_title) |
Björn Lundén | 2aa8ba2 | 2012-05-31 23:05:56 +0200 | [diff] [blame] | 730 | .setIconAttribute(android.R.attr.alertDialogIcon) |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 731 | .setMessage(R.string.too_many_windows_dialog_message) |
| 732 | .setPositiveButton(R.string.ok, null) |
| 733 | .show(); |
| 734 | return false; |
| 735 | } |
| 736 | |
| 737 | // Short-circuit if this was a user gesture. |
| 738 | if (userGesture) { |
| 739 | createWindow(dialog, resultMsg); |
| 740 | return true; |
| 741 | } |
| 742 | |
| 743 | // Allow the popup and create the appropriate window. |
| 744 | final AlertDialog.OnClickListener allowListener = |
| 745 | new AlertDialog.OnClickListener() { |
| 746 | public void onClick(DialogInterface d, |
| 747 | int which) { |
| 748 | createWindow(dialog, resultMsg); |
| 749 | } |
| 750 | }; |
| 751 | |
| 752 | // Block the popup by returning a null WebView. |
| 753 | final AlertDialog.OnClickListener blockListener = |
| 754 | new AlertDialog.OnClickListener() { |
| 755 | public void onClick(DialogInterface d, int which) { |
| 756 | resultMsg.sendToTarget(); |
| 757 | } |
| 758 | }; |
| 759 | |
| 760 | // Build a confirmation dialog to display to the user. |
| 761 | final AlertDialog d = |
Michael Kolb | 1461244 | 2011-06-24 13:06:29 -0700 | [diff] [blame] | 762 | new AlertDialog.Builder(mContext) |
Björn Lundén | 2aa8ba2 | 2012-05-31 23:05:56 +0200 | [diff] [blame] | 763 | .setIconAttribute(android.R.attr.alertDialogIcon) |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 764 | .setMessage(R.string.popup_window_attempt) |
| 765 | .setPositiveButton(R.string.allow, allowListener) |
| 766 | .setNegativeButton(R.string.block, blockListener) |
| 767 | .setCancelable(false) |
| 768 | .create(); |
| 769 | |
| 770 | // Show the confirmation dialog. |
| 771 | d.show(); |
| 772 | return true; |
| 773 | } |
| 774 | |
| 775 | @Override |
Patrick Scott | eb5061b | 2009-11-18 15:00:30 -0500 | [diff] [blame] | 776 | public void onRequestFocus(WebView view) { |
| 777 | if (!mInForeground) { |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 778 | mWebViewController.switchToTab(Tab.this); |
Patrick Scott | eb5061b | 2009-11-18 15:00:30 -0500 | [diff] [blame] | 779 | } |
| 780 | } |
| 781 | |
| 782 | @Override |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 783 | public void onCloseWindow(WebView window) { |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 784 | if (mParent != null) { |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 785 | // JavaScript can only close popup window. |
| 786 | if (mInForeground) { |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 787 | mWebViewController.switchToTab(mParent); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 788 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 789 | mWebViewController.closeTab(Tab.this); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 790 | } |
| 791 | } |
| 792 | |
| 793 | @Override |
| 794 | public void onProgressChanged(WebView view, int newProgress) { |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 795 | mPageLoadProgress = newProgress; |
Michael Kolb | b1fb70c | 2011-11-21 12:38:14 -0800 | [diff] [blame] | 796 | if (newProgress == 100) { |
| 797 | mInPageLoad = false; |
| 798 | } |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 799 | mWebViewController.onProgressChanged(Tab.this); |
Michael Kolb | 7286427 | 2012-05-03 15:42:15 -0700 | [diff] [blame] | 800 | if (mUpdateThumbnail && newProgress == 100) { |
| 801 | mUpdateThumbnail = false; |
| 802 | } |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | @Override |
Leon Scroggins | 21d9b90 | 2010-03-11 09:33:11 -0500 | [diff] [blame] | 806 | public void onReceivedTitle(WebView view, final String title) { |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 807 | mCurrentState.mTitle = title; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 808 | mWebViewController.onReceivedTitle(Tab.this, title); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | @Override |
| 812 | public void onReceivedIcon(WebView view, Bitmap icon) { |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 813 | mCurrentState.mFavicon = icon; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 814 | mWebViewController.onFavicon(Tab.this, view, icon); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 815 | } |
| 816 | |
| 817 | @Override |
| 818 | public void onReceivedTouchIconUrl(WebView view, String url, |
| 819 | boolean precomposed) { |
Michael Kolb | 1461244 | 2011-06-24 13:06:29 -0700 | [diff] [blame] | 820 | final ContentResolver cr = mContext.getContentResolver(); |
Leon Scroggins | c8393d9 | 2010-04-23 14:58:16 -0400 | [diff] [blame] | 821 | // Let precomposed icons take precedence over non-composed |
| 822 | // icons. |
| 823 | if (precomposed && mTouchIconLoader != null) { |
| 824 | mTouchIconLoader.cancel(false); |
| 825 | mTouchIconLoader = null; |
| 826 | } |
| 827 | // Have only one async task at a time. |
| 828 | if (mTouchIconLoader == null) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 829 | mTouchIconLoader = new DownloadTouchIcon(Tab.this, |
Michael Kolb | 1461244 | 2011-06-24 13:06:29 -0700 | [diff] [blame] | 830 | mContext, cr, view); |
Leon Scroggins | c8393d9 | 2010-04-23 14:58:16 -0400 | [diff] [blame] | 831 | mTouchIconLoader.execute(url); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 832 | } |
| 833 | } |
| 834 | |
| 835 | @Override |
| 836 | public void onShowCustomView(View view, |
| 837 | WebChromeClient.CustomViewCallback callback) { |
Michael Kolb | 1461244 | 2011-06-24 13:06:29 -0700 | [diff] [blame] | 838 | Activity activity = mWebViewController.getActivity(); |
| 839 | if (activity != null) { |
| 840 | onShowCustomView(view, activity.getRequestedOrientation(), callback); |
| 841 | } |
Derek Sollenberger | 2d4f1e2 | 2011-06-01 14:50:42 -0400 | [diff] [blame] | 842 | } |
| 843 | |
| 844 | @Override |
| 845 | public void onShowCustomView(View view, int requestedOrientation, |
| 846 | WebChromeClient.CustomViewCallback callback) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 847 | if (mInForeground) mWebViewController.showCustomView(Tab.this, view, |
Derek Sollenberger | 2d4f1e2 | 2011-06-01 14:50:42 -0400 | [diff] [blame] | 848 | requestedOrientation, callback); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | @Override |
| 852 | public void onHideCustomView() { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 853 | if (mInForeground) mWebViewController.hideCustomView(); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 854 | } |
| 855 | |
| 856 | /** |
| 857 | * The origin has exceeded its database quota. |
| 858 | * @param url the URL that exceeded the quota |
| 859 | * @param databaseIdentifier the identifier of the database on which the |
| 860 | * transaction that caused the quota overflow was run |
| 861 | * @param currentQuota the current quota for the origin. |
| 862 | * @param estimatedSize the estimated size of the database. |
| 863 | * @param totalUsedQuota is the sum of all origins' quota. |
| 864 | * @param quotaUpdater The callback to run when a decision to allow or |
| 865 | * deny quota has been made. Don't forget to call this! |
| 866 | */ |
| 867 | @Override |
| 868 | public void onExceededDatabaseQuota(String url, |
| 869 | String databaseIdentifier, long currentQuota, long estimatedSize, |
| 870 | long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) { |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 871 | mSettings.getWebStorageSizeManager() |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 872 | .onExceededDatabaseQuota(url, databaseIdentifier, |
| 873 | currentQuota, estimatedSize, totalUsedQuota, |
| 874 | quotaUpdater); |
| 875 | } |
| 876 | |
| 877 | /** |
| 878 | * The Application Cache has exceeded its max size. |
| 879 | * @param spaceNeeded is the amount of disk space that would be needed |
| 880 | * in order for the last appcache operation to succeed. |
| 881 | * @param totalUsedQuota is the sum of all origins' quota. |
| 882 | * @param quotaUpdater A callback to inform the WebCore thread that a |
| 883 | * new app cache size is available. This callback must always |
| 884 | * be executed at some point to ensure that the sleeping |
| 885 | * WebCore thread is woken up. |
| 886 | */ |
| 887 | @Override |
| 888 | public void onReachedMaxAppCacheSize(long spaceNeeded, |
| 889 | long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) { |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 890 | mSettings.getWebStorageSizeManager() |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 891 | .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota, |
| 892 | quotaUpdater); |
| 893 | } |
| 894 | |
| 895 | /** |
| 896 | * Instructs the browser to show a prompt to ask the user to set the |
| 897 | * Geolocation permission state for the specified origin. |
| 898 | * @param origin The origin for which Geolocation permissions are |
| 899 | * requested. |
| 900 | * @param callback The callback to call once the user has set the |
| 901 | * Geolocation permission state. |
| 902 | */ |
| 903 | @Override |
| 904 | public void onGeolocationPermissionsShowPrompt(String origin, |
| 905 | GeolocationPermissions.Callback callback) { |
| 906 | if (mInForeground) { |
Grace Kloba | 50c241e | 2010-04-20 11:07:50 -0700 | [diff] [blame] | 907 | getGeolocationPermissionsPrompt().show(origin, callback); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 908 | } |
| 909 | } |
| 910 | |
| 911 | /** |
| 912 | * Instructs the browser to hide the Geolocation permissions prompt. |
| 913 | */ |
| 914 | @Override |
| 915 | public void onGeolocationPermissionsHidePrompt() { |
Grace Kloba | 50c241e | 2010-04-20 11:07:50 -0700 | [diff] [blame] | 916 | if (mInForeground && mGeolocationPermissionsPrompt != null) { |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 917 | mGeolocationPermissionsPrompt.hide(); |
| 918 | } |
| 919 | } |
| 920 | |
Ben Murdoch | 65acc35 | 2009-11-19 18:16:04 +0000 | [diff] [blame] | 921 | /* Adds a JavaScript error message to the system log and if the JS |
| 922 | * console is enabled in the about:debug options, to that console |
| 923 | * also. |
Ben Murdoch | c42addf | 2010-01-28 15:19:59 +0000 | [diff] [blame] | 924 | * @param consoleMessage the message object. |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 925 | */ |
| 926 | @Override |
Ben Murdoch | c42addf | 2010-01-28 15:19:59 +0000 | [diff] [blame] | 927 | public boolean onConsoleMessage(ConsoleMessage consoleMessage) { |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 928 | if (mInForeground) { |
| 929 | // call getErrorConsole(true) so it will create one if needed |
| 930 | ErrorConsoleView errorConsole = getErrorConsole(true); |
Ben Murdoch | c42addf | 2010-01-28 15:19:59 +0000 | [diff] [blame] | 931 | errorConsole.addErrorMessage(consoleMessage); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 932 | if (mWebViewController.shouldShowErrorConsole() |
| 933 | && errorConsole.getShowState() != |
| 934 | ErrorConsoleView.SHOW_MAXIMIZED) { |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 935 | errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED); |
| 936 | } |
| 937 | } |
Ben Murdoch | c42addf | 2010-01-28 15:19:59 +0000 | [diff] [blame] | 938 | |
Jeff Hamilton | 47654f4 | 2010-09-07 09:57:51 -0500 | [diff] [blame] | 939 | // Don't log console messages in private browsing mode |
Rob Tsuk | f8bdfce | 2010-10-07 15:41:16 -0700 | [diff] [blame] | 940 | if (isPrivateBrowsingEnabled()) return true; |
Jeff Hamilton | 47654f4 | 2010-09-07 09:57:51 -0500 | [diff] [blame] | 941 | |
Ben Murdoch | c42addf | 2010-01-28 15:19:59 +0000 | [diff] [blame] | 942 | String message = "Console: " + consoleMessage.message() + " " |
| 943 | + consoleMessage.sourceId() + ":" |
| 944 | + consoleMessage.lineNumber(); |
| 945 | |
| 946 | switch (consoleMessage.messageLevel()) { |
| 947 | case TIP: |
| 948 | Log.v(CONSOLE_LOGTAG, message); |
| 949 | break; |
| 950 | case LOG: |
| 951 | Log.i(CONSOLE_LOGTAG, message); |
| 952 | break; |
| 953 | case WARNING: |
| 954 | Log.w(CONSOLE_LOGTAG, message); |
| 955 | break; |
| 956 | case ERROR: |
| 957 | Log.e(CONSOLE_LOGTAG, message); |
| 958 | break; |
| 959 | case DEBUG: |
| 960 | Log.d(CONSOLE_LOGTAG, message); |
| 961 | break; |
| 962 | } |
| 963 | |
| 964 | return true; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 965 | } |
| 966 | |
| 967 | /** |
| 968 | * Ask the browser for an icon to represent a <video> element. |
| 969 | * This icon will be used if the Web page did not specify a poster attribute. |
| 970 | * @return Bitmap The icon or null if no such icon is available. |
| 971 | */ |
| 972 | @Override |
| 973 | public Bitmap getDefaultVideoPoster() { |
| 974 | if (mInForeground) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 975 | return mWebViewController.getDefaultVideoPoster(); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 976 | } |
| 977 | return null; |
| 978 | } |
| 979 | |
| 980 | /** |
| 981 | * Ask the host application for a custom progress view to show while |
| 982 | * a <video> is loading. |
| 983 | * @return View The progress view. |
| 984 | */ |
| 985 | @Override |
| 986 | public View getVideoLoadingProgressView() { |
| 987 | if (mInForeground) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 988 | return mWebViewController.getVideoLoadingProgressView(); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 989 | } |
| 990 | return null; |
| 991 | } |
| 992 | |
| 993 | @Override |
Ben Murdoch | 8cad413 | 2012-01-11 10:56:43 +0000 | [diff] [blame] | 994 | public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) { |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 995 | if (mInForeground) { |
Ben Murdoch | 8cad413 | 2012-01-11 10:56:43 +0000 | [diff] [blame] | 996 | mWebViewController.openFileChooser(uploadMsg, acceptType, capture); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 997 | } else { |
| 998 | uploadMsg.onReceiveValue(null); |
| 999 | } |
| 1000 | } |
| 1001 | |
| 1002 | /** |
| 1003 | * Deliver a list of already-visited URLs |
| 1004 | */ |
| 1005 | @Override |
| 1006 | public void getVisitedHistory(final ValueCallback<String[]> callback) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1007 | mWebViewController.getVisitedHistory(callback); |
| 1008 | } |
Ben Murdoch | 8029a77 | 2010-11-16 11:58:21 +0000 | [diff] [blame] | 1009 | |
| 1010 | @Override |
| 1011 | public void setupAutoFill(Message message) { |
| 1012 | // Prompt the user to set up their profile. |
| 1013 | final Message msg = message; |
Michael Kolb | 1461244 | 2011-06-24 13:06:29 -0700 | [diff] [blame] | 1014 | AlertDialog.Builder builder = new AlertDialog.Builder(mContext); |
| 1015 | LayoutInflater inflater = (LayoutInflater) mContext.getSystemService( |
Ben Murdoch | 1d676b6 | 2011-01-17 12:54:24 +0000 | [diff] [blame] | 1016 | Context.LAYOUT_INFLATER_SERVICE); |
| 1017 | final View layout = inflater.inflate(R.layout.setup_autofill_dialog, null); |
| 1018 | |
| 1019 | builder.setView(layout) |
| 1020 | .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { |
| 1021 | @Override |
| 1022 | public void onClick(DialogInterface dialog, int id) { |
| 1023 | CheckBox disableAutoFill = (CheckBox) layout.findViewById( |
| 1024 | R.id.setup_autofill_dialog_disable_autofill); |
| 1025 | |
| 1026 | if (disableAutoFill.isChecked()) { |
| 1027 | // Disable autofill and show a toast with how to turn it on again. |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 1028 | mSettings.setAutofillEnabled(false); |
Michael Kolb | 1461244 | 2011-06-24 13:06:29 -0700 | [diff] [blame] | 1029 | Toast.makeText(mContext, |
Ben Murdoch | 1d676b6 | 2011-01-17 12:54:24 +0000 | [diff] [blame] | 1030 | R.string.autofill_setup_dialog_negative_toast, |
| 1031 | Toast.LENGTH_LONG).show(); |
| 1032 | } else { |
| 1033 | // Take user to the AutoFill profile editor. When they return, |
| 1034 | // we will send the message that we pass here which will trigger |
| 1035 | // the form to get filled out with their new profile. |
| 1036 | mWebViewController.setupAutoFill(msg); |
| 1037 | } |
| 1038 | } |
| 1039 | }) |
| 1040 | .setNegativeButton(R.string.cancel, null) |
| 1041 | .show(); |
Ben Murdoch | 8029a77 | 2010-11-16 11:58:21 +0000 | [diff] [blame] | 1042 | } |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1043 | }; |
| 1044 | |
| 1045 | // ------------------------------------------------------------------------- |
| 1046 | // WebViewClient implementation for the sub window |
| 1047 | // ------------------------------------------------------------------------- |
| 1048 | |
| 1049 | // Subclass of WebViewClient used in subwindows to notify the main |
| 1050 | // WebViewClient of certain WebView activities. |
Jonathan Dixon | f4ff149 | 2012-06-08 10:15:32 -0700 | [diff] [blame] | 1051 | private static class SubWindowClient extends WebViewClientClassicExt { |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1052 | // The main WebViewClient. |
Jonathan Dixon | f4ff149 | 2012-06-08 10:15:32 -0700 | [diff] [blame] | 1053 | private final WebViewClientClassicExt mClient; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1054 | private final WebViewController mController; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1055 | |
Jonathan Dixon | f4ff149 | 2012-06-08 10:15:32 -0700 | [diff] [blame] | 1056 | SubWindowClient(WebViewClientClassicExt client, WebViewController controller) { |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1057 | mClient = client; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1058 | mController = controller; |
Leon Scroggins III | 211ba54 | 2010-04-19 13:21:13 -0400 | [diff] [blame] | 1059 | } |
| 1060 | @Override |
| 1061 | public void onPageStarted(WebView view, String url, Bitmap favicon) { |
| 1062 | // Unlike the others, do not call mClient's version, which would |
| 1063 | // change the progress bar. However, we do want to remove the |
Cary Clark | 01cfcdd | 2010-06-04 16:36:45 -0400 | [diff] [blame] | 1064 | // find or select dialog. |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1065 | mController.endActionMode(); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1066 | } |
| 1067 | @Override |
| 1068 | public void doUpdateVisitedHistory(WebView view, String url, |
| 1069 | boolean isReload) { |
| 1070 | mClient.doUpdateVisitedHistory(view, url, isReload); |
| 1071 | } |
| 1072 | @Override |
| 1073 | public boolean shouldOverrideUrlLoading(WebView view, String url) { |
| 1074 | return mClient.shouldOverrideUrlLoading(view, url); |
| 1075 | } |
| 1076 | @Override |
| 1077 | public void onReceivedSslError(WebView view, SslErrorHandler handler, |
| 1078 | SslError error) { |
| 1079 | mClient.onReceivedSslError(view, handler, error); |
| 1080 | } |
| 1081 | @Override |
Brian Carlstrom | 8862c1d | 2011-06-02 01:05:55 -0700 | [diff] [blame] | 1082 | public void onReceivedClientCertRequest(WebView view, |
| 1083 | ClientCertRequestHandler handler, String host_and_port) { |
| 1084 | mClient.onReceivedClientCertRequest(view, handler, host_and_port); |
| 1085 | } |
| 1086 | @Override |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1087 | public void onReceivedHttpAuthRequest(WebView view, |
| 1088 | HttpAuthHandler handler, String host, String realm) { |
| 1089 | mClient.onReceivedHttpAuthRequest(view, handler, host, realm); |
| 1090 | } |
| 1091 | @Override |
| 1092 | public void onFormResubmission(WebView view, Message dontResend, |
| 1093 | Message resend) { |
| 1094 | mClient.onFormResubmission(view, dontResend, resend); |
| 1095 | } |
| 1096 | @Override |
| 1097 | public void onReceivedError(WebView view, int errorCode, |
| 1098 | String description, String failingUrl) { |
| 1099 | mClient.onReceivedError(view, errorCode, description, failingUrl); |
| 1100 | } |
| 1101 | @Override |
| 1102 | public boolean shouldOverrideKeyEvent(WebView view, |
| 1103 | android.view.KeyEvent event) { |
| 1104 | return mClient.shouldOverrideKeyEvent(view, event); |
| 1105 | } |
| 1106 | @Override |
| 1107 | public void onUnhandledKeyEvent(WebView view, |
| 1108 | android.view.KeyEvent event) { |
| 1109 | mClient.onUnhandledKeyEvent(view, event); |
| 1110 | } |
| 1111 | } |
| 1112 | |
| 1113 | // ------------------------------------------------------------------------- |
| 1114 | // WebChromeClient implementation for the sub window |
| 1115 | // ------------------------------------------------------------------------- |
| 1116 | |
| 1117 | private class SubWindowChromeClient extends WebChromeClient { |
| 1118 | // The main WebChromeClient. |
| 1119 | private final WebChromeClient mClient; |
| 1120 | |
| 1121 | SubWindowChromeClient(WebChromeClient client) { |
| 1122 | mClient = client; |
| 1123 | } |
| 1124 | @Override |
| 1125 | public void onProgressChanged(WebView view, int newProgress) { |
| 1126 | mClient.onProgressChanged(view, newProgress); |
| 1127 | } |
| 1128 | @Override |
| 1129 | public boolean onCreateWindow(WebView view, boolean dialog, |
| 1130 | boolean userGesture, android.os.Message resultMsg) { |
| 1131 | return mClient.onCreateWindow(view, dialog, userGesture, resultMsg); |
| 1132 | } |
| 1133 | @Override |
| 1134 | public void onCloseWindow(WebView window) { |
| 1135 | if (window != mSubView) { |
| 1136 | Log.e(LOGTAG, "Can't close the window"); |
| 1137 | } |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1138 | mWebViewController.dismissSubWindow(Tab.this); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1139 | } |
| 1140 | } |
| 1141 | |
| 1142 | // ------------------------------------------------------------------------- |
| 1143 | |
| 1144 | // Construct a new tab |
Michael Kolb | 7bcafde | 2011-05-09 13:55:59 -0700 | [diff] [blame] | 1145 | Tab(WebViewController wvcontroller, WebView w) { |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 1146 | this(wvcontroller, w, null); |
| 1147 | } |
| 1148 | |
| 1149 | Tab(WebViewController wvcontroller, Bundle state) { |
| 1150 | this(wvcontroller, null, state); |
| 1151 | } |
| 1152 | |
| 1153 | Tab(WebViewController wvcontroller, WebView w, Bundle state) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1154 | mWebViewController = wvcontroller; |
Michael Kolb | 1461244 | 2011-06-24 13:06:29 -0700 | [diff] [blame] | 1155 | mContext = mWebViewController.getContext(); |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 1156 | mSettings = BrowserSettings.getInstance(); |
Michael Kolb | 1461244 | 2011-06-24 13:06:29 -0700 | [diff] [blame] | 1157 | mDataController = DataController.getInstance(mContext); |
| 1158 | mCurrentState = new PageState(mContext, w != null |
John Reck | 847b532 | 2011-04-14 17:02:18 -0700 | [diff] [blame] | 1159 | ? w.isPrivateBrowsingEnabled() : false); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1160 | mInPageLoad = false; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1161 | mInForeground = false; |
| 1162 | |
Selim Gurun | 0b3d66f | 2012-08-29 13:08:13 -0700 | [diff] [blame] | 1163 | mDownloadListener = new BrowserDownloadListener() { |
Leon Scroggins | dcc5eeb | 2010-02-23 17:26:37 -0500 | [diff] [blame] | 1164 | public void onDownloadStart(String url, String userAgent, |
Selim Gurun | 0b3d66f | 2012-08-29 13:08:13 -0700 | [diff] [blame] | 1165 | String contentDisposition, String mimetype, String referer, |
Leon Scroggins | dcc5eeb | 2010-02-23 17:26:37 -0500 | [diff] [blame] | 1166 | long contentLength) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1167 | mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition, |
Selim Gurun | 0b3d66f | 2012-08-29 13:08:13 -0700 | [diff] [blame] | 1168 | mimetype, referer, contentLength); |
Leon Scroggins | dcc5eeb | 2010-02-23 17:26:37 -0500 | [diff] [blame] | 1169 | } |
| 1170 | }; |
Leon Scroggins | 0c75a8e | 2010-03-03 16:40:58 -0500 | [diff] [blame] | 1171 | mWebBackForwardListClient = new WebBackForwardListClient() { |
| 1172 | @Override |
| 1173 | public void onNewHistoryItem(WebHistoryItem item) { |
Mathew Inwood | a829d55 | 2011-09-02 14:16:25 +0100 | [diff] [blame] | 1174 | if (mClearHistoryUrlPattern != null) { |
| 1175 | boolean match = |
| 1176 | mClearHistoryUrlPattern.matcher(item.getOriginalUrl()).matches(); |
Mathew Inwood | 1dd8e82 | 2011-08-03 14:34:29 +0100 | [diff] [blame] | 1177 | if (LOGD_ENABLED) { |
Mathew Inwood | a829d55 | 2011-09-02 14:16:25 +0100 | [diff] [blame] | 1178 | Log.d(LOGTAG, "onNewHistoryItem: match=" + match + "\n\t" |
| 1179 | + item.getUrl() + "\n\t" |
| 1180 | + mClearHistoryUrlPattern); |
Mathew Inwood | 1dd8e82 | 2011-08-03 14:34:29 +0100 | [diff] [blame] | 1181 | } |
Mathew Inwood | a829d55 | 2011-09-02 14:16:25 +0100 | [diff] [blame] | 1182 | if (match) { |
Mathew Inwood | 1dd8e82 | 2011-08-03 14:34:29 +0100 | [diff] [blame] | 1183 | if (mMainView != null) { |
| 1184 | mMainView.clearHistory(); |
| 1185 | } |
| 1186 | } |
Mathew Inwood | a829d55 | 2011-09-02 14:16:25 +0100 | [diff] [blame] | 1187 | mClearHistoryUrlPattern = null; |
Mathew Inwood | 1dd8e82 | 2011-08-03 14:34:29 +0100 | [diff] [blame] | 1188 | } |
Leon Scroggins | 0c75a8e | 2010-03-03 16:40:58 -0500 | [diff] [blame] | 1189 | } |
Leon Scroggins | 0c75a8e | 2010-03-03 16:40:58 -0500 | [diff] [blame] | 1190 | }; |
Leon Scroggins | dcc5eeb | 2010-02-23 17:26:37 -0500 | [diff] [blame] | 1191 | |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 1192 | mCaptureWidth = mContext.getResources().getDimensionPixelSize( |
| 1193 | R.dimen.tab_thumbnail_width); |
| 1194 | mCaptureHeight = mContext.getResources().getDimensionPixelSize( |
| 1195 | R.dimen.tab_thumbnail_height); |
| 1196 | updateShouldCaptureThumbnails(); |
| 1197 | restoreState(state); |
John Reck | 52be478 | 2011-08-26 15:37:29 -0700 | [diff] [blame] | 1198 | if (getId() == -1) { |
| 1199 | mId = TabControl.getNextId(); |
| 1200 | } |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1201 | setWebView(w); |
Michael Kolb | 9ef259a | 2011-07-12 15:33:08 -0700 | [diff] [blame] | 1202 | mHandler = new Handler() { |
Mathew Inwood | 1dd8e82 | 2011-08-03 14:34:29 +0100 | [diff] [blame] | 1203 | @Override |
Michael Kolb | 9ef259a | 2011-07-12 15:33:08 -0700 | [diff] [blame] | 1204 | public void handleMessage(Message m) { |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 1205 | switch (m.what) { |
| 1206 | case MSG_CAPTURE: |
| 1207 | capture(); |
| 1208 | break; |
| 1209 | } |
Michael Kolb | 9ef259a | 2011-07-12 15:33:08 -0700 | [diff] [blame] | 1210 | } |
| 1211 | }; |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 1212 | } |
Michael Kolb | 9ef259a | 2011-07-12 15:33:08 -0700 | [diff] [blame] | 1213 | |
Michael Kolb | 7286427 | 2012-05-03 15:42:15 -0700 | [diff] [blame] | 1214 | public boolean shouldUpdateThumbnail() { |
| 1215 | return mUpdateThumbnail; |
| 1216 | } |
| 1217 | |
Mathew Inwood | e09305e | 2011-09-02 12:03:26 +0100 | [diff] [blame] | 1218 | /** |
| 1219 | * This is used to get a new ID when the tab has been preloaded, before it is displayed and |
| 1220 | * added to TabControl. Preloaded tabs can be created before restoreInstanceState, leading |
| 1221 | * to overlapping IDs between the preloaded and restored tabs. |
| 1222 | */ |
| 1223 | public void refreshIdAfterPreload() { |
| 1224 | mId = TabControl.getNextId(); |
| 1225 | } |
| 1226 | |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 1227 | public void updateShouldCaptureThumbnails() { |
| 1228 | if (mWebViewController.shouldCaptureThumbnails()) { |
| 1229 | synchronized (Tab.this) { |
| 1230 | if (mCapture == null) { |
| 1231 | mCapture = Bitmap.createBitmap(mCaptureWidth, mCaptureHeight, |
| 1232 | Bitmap.Config.RGB_565); |
Michael Kolb | c3af067 | 2011-08-09 10:24:41 -0700 | [diff] [blame] | 1233 | mCapture.eraseColor(Color.WHITE); |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 1234 | if (mInForeground) { |
| 1235 | postCapture(); |
| 1236 | } |
| 1237 | } |
| 1238 | } |
| 1239 | } else { |
| 1240 | synchronized (Tab.this) { |
| 1241 | mCapture = null; |
| 1242 | deleteThumbnail(); |
| 1243 | } |
| 1244 | } |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1245 | } |
| 1246 | |
Michael Kolb | 1461244 | 2011-06-24 13:06:29 -0700 | [diff] [blame] | 1247 | public void setController(WebViewController ctl) { |
| 1248 | mWebViewController = ctl; |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 1249 | updateShouldCaptureThumbnails(); |
Michael Kolb | 1461244 | 2011-06-24 13:06:29 -0700 | [diff] [blame] | 1250 | } |
| 1251 | |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 1252 | public long getId() { |
| 1253 | return mId; |
| 1254 | } |
| 1255 | |
Michael Kolb | 91911a2 | 2012-01-17 11:21:25 -0800 | [diff] [blame] | 1256 | void setWebView(WebView w) { |
| 1257 | setWebView(w, true); |
| 1258 | } |
| 1259 | |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1260 | /** |
| 1261 | * Sets the WebView for this tab, correctly removing the old WebView from |
| 1262 | * the container view. |
| 1263 | */ |
Michael Kolb | 91911a2 | 2012-01-17 11:21:25 -0800 | [diff] [blame] | 1264 | void setWebView(WebView w, boolean restore) { |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1265 | if (mMainView == w) { |
| 1266 | return; |
| 1267 | } |
Michael Kolb | a713ec8 | 2010-11-29 17:27:06 -0800 | [diff] [blame] | 1268 | |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1269 | // If the WebView is changing, the page will be reloaded, so any ongoing |
| 1270 | // Geolocation permission requests are void. |
Grace Kloba | 50c241e | 2010-04-20 11:07:50 -0700 | [diff] [blame] | 1271 | if (mGeolocationPermissionsPrompt != null) { |
| 1272 | mGeolocationPermissionsPrompt.hide(); |
| 1273 | } |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1274 | |
Michael Kolb | a713ec8 | 2010-11-29 17:27:06 -0800 | [diff] [blame] | 1275 | mWebViewController.onSetWebView(this, w); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1276 | |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 1277 | if (mMainView != null) { |
John Reck | 8ee633f | 2011-08-09 16:00:35 -0700 | [diff] [blame] | 1278 | mMainView.setPictureListener(null); |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 1279 | if (w != null) { |
| 1280 | syncCurrentState(w, null); |
| 1281 | } else { |
| 1282 | mCurrentState = new PageState(mContext, false); |
| 1283 | } |
| 1284 | } |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1285 | // set the new one |
| 1286 | mMainView = w; |
Leon Scroggins | dcc5eeb | 2010-02-23 17:26:37 -0500 | [diff] [blame] | 1287 | // attach the WebViewClient, WebChromeClient and DownloadListener |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1288 | if (mMainView != null) { |
| 1289 | mMainView.setWebViewClient(mWebViewClient); |
| 1290 | mMainView.setWebChromeClient(mWebChromeClient); |
Leon Scroggins | dcc5eeb | 2010-02-23 17:26:37 -0500 | [diff] [blame] | 1291 | // Attach DownloadManager so that downloads can start in an active |
| 1292 | // or a non-active window. This can happen when going to a site that |
| 1293 | // does a redirect after a period of time. The user could have |
| 1294 | // switched to another tab while waiting for the download to start. |
| 1295 | mMainView.setDownloadListener(mDownloadListener); |
Jonathan Dixon | 4d2fcab | 2012-02-24 00:13:06 +0000 | [diff] [blame] | 1296 | getWebViewClassic().setWebBackForwardListClient(mWebBackForwardListClient); |
John Reck | 8ee633f | 2011-08-09 16:00:35 -0700 | [diff] [blame] | 1297 | TabControl tc = mWebViewController.getTabControl(); |
| 1298 | if (tc != null && tc.getOnThumbnailUpdatedListener() != null) { |
| 1299 | mMainView.setPictureListener(this); |
| 1300 | } |
Michael Kolb | 91911a2 | 2012-01-17 11:21:25 -0800 | [diff] [blame] | 1301 | if (restore && (mSavedState != null)) { |
John Reck | 8b9bb8b | 2012-03-08 13:19:40 -0800 | [diff] [blame] | 1302 | restoreUserAgent(); |
John Reck | 6c2e2f3 | 2011-08-22 13:41:23 -0700 | [diff] [blame] | 1303 | WebBackForwardList restoredState |
| 1304 | = mMainView.restoreState(mSavedState); |
| 1305 | if (restoredState == null || restoredState.getSize() == 0) { |
| 1306 | Log.w(LOGTAG, "Failed to restore WebView state!"); |
| 1307 | loadUrl(mCurrentState.mOriginalUrl, null); |
| 1308 | } |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 1309 | mSavedState = null; |
| 1310 | } |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1311 | } |
| 1312 | } |
| 1313 | |
| 1314 | /** |
| 1315 | * Destroy the tab's main WebView and subWindow if any |
| 1316 | */ |
| 1317 | void destroy() { |
| 1318 | if (mMainView != null) { |
| 1319 | dismissSubWindow(); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1320 | // save the WebView to call destroy() after detach it from the tab |
| 1321 | WebView webView = mMainView; |
| 1322 | setWebView(null); |
| 1323 | webView.destroy(); |
| 1324 | } |
| 1325 | } |
| 1326 | |
| 1327 | /** |
| 1328 | * Remove the tab from the parent |
| 1329 | */ |
| 1330 | void removeFromTree() { |
| 1331 | // detach the children |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 1332 | if (mChildren != null) { |
| 1333 | for(Tab t : mChildren) { |
| 1334 | t.setParent(null); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1335 | } |
| 1336 | } |
| 1337 | // remove itself from the parent list |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 1338 | if (mParent != null) { |
| 1339 | mParent.mChildren.remove(this); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1340 | } |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 1341 | deleteThumbnail(); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1342 | } |
| 1343 | |
| 1344 | /** |
| 1345 | * Create a new subwindow unless a subwindow already exists. |
| 1346 | * @return True if a new subwindow was created. False if one already exists. |
| 1347 | */ |
| 1348 | boolean createSubWindow() { |
| 1349 | if (mSubView == null) { |
Michael Kolb | 1514bb7 | 2010-11-22 09:11:48 -0800 | [diff] [blame] | 1350 | mWebViewController.createSubWindow(this); |
Leon Scroggins III | 211ba54 | 2010-04-19 13:21:13 -0400 | [diff] [blame] | 1351 | mSubView.setWebViewClient(new SubWindowClient(mWebViewClient, |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1352 | mWebViewController)); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1353 | mSubView.setWebChromeClient(new SubWindowChromeClient( |
| 1354 | mWebChromeClient)); |
Leon Scroggins | dcc5eeb | 2010-02-23 17:26:37 -0500 | [diff] [blame] | 1355 | // Set a different DownloadListener for the mSubView, since it will |
| 1356 | // just need to dismiss the mSubView, rather than close the Tab |
Selim Gurun | 0b3d66f | 2012-08-29 13:08:13 -0700 | [diff] [blame] | 1357 | mSubView.setDownloadListener(new BrowserDownloadListener() { |
Leon Scroggins | dcc5eeb | 2010-02-23 17:26:37 -0500 | [diff] [blame] | 1358 | public void onDownloadStart(String url, String userAgent, |
Selim Gurun | 0b3d66f | 2012-08-29 13:08:13 -0700 | [diff] [blame] | 1359 | String contentDisposition, String mimetype, String referer, |
Leon Scroggins | dcc5eeb | 2010-02-23 17:26:37 -0500 | [diff] [blame] | 1360 | long contentLength) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1361 | mWebViewController.onDownloadStart(Tab.this, url, userAgent, |
Selim Gurun | 0b3d66f | 2012-08-29 13:08:13 -0700 | [diff] [blame] | 1362 | contentDisposition, mimetype, referer, contentLength); |
Leon Scroggins | dcc5eeb | 2010-02-23 17:26:37 -0500 | [diff] [blame] | 1363 | if (mSubView.copyBackForwardList().getSize() == 0) { |
| 1364 | // This subwindow was opened for the sole purpose of |
| 1365 | // downloading a file. Remove it. |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1366 | mWebViewController.dismissSubWindow(Tab.this); |
Leon Scroggins | dcc5eeb | 2010-02-23 17:26:37 -0500 | [diff] [blame] | 1367 | } |
| 1368 | } |
| 1369 | }); |
Michael Kolb | 1461244 | 2011-06-24 13:06:29 -0700 | [diff] [blame] | 1370 | mSubView.setOnCreateContextMenuListener(mWebViewController.getActivity()); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1371 | return true; |
| 1372 | } |
| 1373 | return false; |
| 1374 | } |
| 1375 | |
| 1376 | /** |
| 1377 | * Dismiss the subWindow for the tab. |
| 1378 | */ |
| 1379 | void dismissSubWindow() { |
| 1380 | if (mSubView != null) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1381 | mWebViewController.endActionMode(); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1382 | mSubView.destroy(); |
| 1383 | mSubView = null; |
| 1384 | mSubViewContainer = null; |
| 1385 | } |
| 1386 | } |
| 1387 | |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1388 | |
| 1389 | /** |
| 1390 | * Set the parent tab of this tab. |
| 1391 | */ |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 1392 | void setParent(Tab parent) { |
John Reck | 52be478 | 2011-08-26 15:37:29 -0700 | [diff] [blame] | 1393 | if (parent == this) { |
| 1394 | throw new IllegalStateException("Cannot set parent to self!"); |
| 1395 | } |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 1396 | mParent = parent; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1397 | // This tab may have been freed due to low memory. If that is the case, |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 1398 | // the parent tab id is already saved. If we are changing that id |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1399 | // (most likely due to removing the parent tab) we must update the |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 1400 | // parent tab id in the saved Bundle. |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1401 | if (mSavedState != null) { |
| 1402 | if (parent == null) { |
| 1403 | mSavedState.remove(PARENTTAB); |
| 1404 | } else { |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 1405 | mSavedState.putLong(PARENTTAB, parent.getId()); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1406 | } |
| 1407 | } |
John Reck | b0a86db | 2011-05-24 14:05:58 -0700 | [diff] [blame] | 1408 | |
| 1409 | // Sync the WebView useragent with the parent |
| 1410 | if (parent != null && mSettings.hasDesktopUseragent(parent.getWebView()) |
| 1411 | != mSettings.hasDesktopUseragent(getWebView())) { |
| 1412 | mSettings.toggleDesktopUseragent(getWebView()); |
| 1413 | } |
John Reck | 52be478 | 2011-08-26 15:37:29 -0700 | [diff] [blame] | 1414 | |
| 1415 | if (parent != null && parent.getId() == getId()) { |
| 1416 | throw new IllegalStateException("Parent has same ID as child!"); |
| 1417 | } |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1418 | } |
| 1419 | |
| 1420 | /** |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 1421 | * If this Tab was created through another Tab, then this method returns |
| 1422 | * that Tab. |
| 1423 | * @return the Tab parent or null |
| 1424 | */ |
| 1425 | public Tab getParent() { |
| 1426 | return mParent; |
| 1427 | } |
| 1428 | |
| 1429 | /** |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1430 | * When a Tab is created through the content of another Tab, then we |
| 1431 | * associate the Tabs. |
| 1432 | * @param child the Tab that was created from this Tab |
| 1433 | */ |
| 1434 | void addChildTab(Tab child) { |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 1435 | if (mChildren == null) { |
| 1436 | mChildren = new Vector<Tab>(); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1437 | } |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 1438 | mChildren.add(child); |
| 1439 | child.setParent(this); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1440 | } |
| 1441 | |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 1442 | Vector<Tab> getChildren() { |
| 1443 | return mChildren; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1444 | } |
| 1445 | |
| 1446 | void resume() { |
| 1447 | if (mMainView != null) { |
John Reck | 56c1fcf | 2011-08-17 10:15:16 -0700 | [diff] [blame] | 1448 | setupHwAcceleration(mMainView); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1449 | mMainView.onResume(); |
| 1450 | if (mSubView != null) { |
| 1451 | mSubView.onResume(); |
| 1452 | } |
| 1453 | } |
| 1454 | } |
| 1455 | |
John Reck | 56c1fcf | 2011-08-17 10:15:16 -0700 | [diff] [blame] | 1456 | private void setupHwAcceleration(View web) { |
| 1457 | if (web == null) return; |
| 1458 | BrowserSettings settings = BrowserSettings.getInstance(); |
| 1459 | if (settings.isHardwareAccelerated()) { |
| 1460 | web.setLayerType(View.LAYER_TYPE_NONE, null); |
| 1461 | } else { |
| 1462 | web.setLayerType(View.LAYER_TYPE_SOFTWARE, null); |
| 1463 | } |
| 1464 | } |
| 1465 | |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1466 | void pause() { |
| 1467 | if (mMainView != null) { |
| 1468 | mMainView.onPause(); |
| 1469 | if (mSubView != null) { |
| 1470 | mSubView.onPause(); |
| 1471 | } |
| 1472 | } |
| 1473 | } |
| 1474 | |
| 1475 | void putInForeground() { |
John Reck | 8ee633f | 2011-08-09 16:00:35 -0700 | [diff] [blame] | 1476 | if (mInForeground) { |
| 1477 | return; |
| 1478 | } |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1479 | mInForeground = true; |
| 1480 | resume(); |
Michael Kolb | 1461244 | 2011-06-24 13:06:29 -0700 | [diff] [blame] | 1481 | Activity activity = mWebViewController.getActivity(); |
| 1482 | mMainView.setOnCreateContextMenuListener(activity); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1483 | if (mSubView != null) { |
Michael Kolb | 1461244 | 2011-06-24 13:06:29 -0700 | [diff] [blame] | 1484 | mSubView.setOnCreateContextMenuListener(activity); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1485 | } |
| 1486 | // Show the pending error dialog if the queue is not empty |
| 1487 | if (mQueuedErrors != null && mQueuedErrors.size() > 0) { |
| 1488 | showError(mQueuedErrors.getFirst()); |
| 1489 | } |
Leon Scroggins | 1961ed2 | 2010-12-07 15:22:21 -0500 | [diff] [blame] | 1490 | mWebViewController.bookmarkedStatusHasChanged(this); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1491 | } |
| 1492 | |
| 1493 | void putInBackground() { |
John Reck | 8ee633f | 2011-08-09 16:00:35 -0700 | [diff] [blame] | 1494 | if (!mInForeground) { |
| 1495 | return; |
| 1496 | } |
John Reck | 52be478 | 2011-08-26 15:37:29 -0700 | [diff] [blame] | 1497 | capture(); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1498 | mInForeground = false; |
| 1499 | pause(); |
| 1500 | mMainView.setOnCreateContextMenuListener(null); |
| 1501 | if (mSubView != null) { |
| 1502 | mSubView.setOnCreateContextMenuListener(null); |
| 1503 | } |
| 1504 | } |
| 1505 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1506 | boolean inForeground() { |
| 1507 | return mInForeground; |
| 1508 | } |
| 1509 | |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1510 | /** |
| 1511 | * Return the top window of this tab; either the subwindow if it is not |
| 1512 | * null or the main window. |
| 1513 | * @return The top window of this tab. |
| 1514 | */ |
| 1515 | WebView getTopWindow() { |
| 1516 | if (mSubView != null) { |
| 1517 | return mSubView; |
| 1518 | } |
| 1519 | return mMainView; |
| 1520 | } |
| 1521 | |
| 1522 | /** |
| 1523 | * Return the main window of this tab. Note: if a tab is freed in the |
| 1524 | * background, this can return null. It is only guaranteed to be |
| 1525 | * non-null for the current tab. |
| 1526 | * @return The main WebView of this tab. |
| 1527 | */ |
| 1528 | WebView getWebView() { |
| 1529 | return mMainView; |
| 1530 | } |
| 1531 | |
Jonathan Dixon | 4d2fcab | 2012-02-24 00:13:06 +0000 | [diff] [blame] | 1532 | /** |
| 1533 | * Return the underlying WebViewClassic implementation. As with getWebView, |
| 1534 | * this maybe null for background tabs. |
| 1535 | * @return The main WebView of this tab. |
| 1536 | */ |
| 1537 | WebViewClassic getWebViewClassic() { |
| 1538 | return WebViewClassic.fromWebView(mMainView); |
| 1539 | } |
| 1540 | |
Michael Kolb | a713ec8 | 2010-11-29 17:27:06 -0800 | [diff] [blame] | 1541 | void setViewContainer(View container) { |
| 1542 | mContainer = container; |
| 1543 | } |
| 1544 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1545 | View getViewContainer() { |
| 1546 | return mContainer; |
| 1547 | } |
| 1548 | |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1549 | /** |
Rob Tsuk | f8bdfce | 2010-10-07 15:41:16 -0700 | [diff] [blame] | 1550 | * Return whether private browsing is enabled for the main window of |
| 1551 | * this tab. |
| 1552 | * @return True if private browsing is enabled. |
| 1553 | */ |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1554 | boolean isPrivateBrowsingEnabled() { |
John Reck | 502a353 | 2011-08-16 14:21:46 -0700 | [diff] [blame] | 1555 | return mCurrentState.mIncognito; |
Rob Tsuk | f8bdfce | 2010-10-07 15:41:16 -0700 | [diff] [blame] | 1556 | } |
| 1557 | |
| 1558 | /** |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1559 | * Return the subwindow of this tab or null if there is no subwindow. |
| 1560 | * @return The subwindow of this tab or null. |
| 1561 | */ |
| 1562 | WebView getSubWebView() { |
| 1563 | return mSubView; |
| 1564 | } |
| 1565 | |
Michael Kolb | 1514bb7 | 2010-11-22 09:11:48 -0800 | [diff] [blame] | 1566 | void setSubWebView(WebView subView) { |
| 1567 | mSubView = subView; |
| 1568 | } |
| 1569 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1570 | View getSubViewContainer() { |
| 1571 | return mSubViewContainer; |
| 1572 | } |
| 1573 | |
Michael Kolb | 1514bb7 | 2010-11-22 09:11:48 -0800 | [diff] [blame] | 1574 | void setSubViewContainer(View subViewContainer) { |
| 1575 | mSubViewContainer = subViewContainer; |
| 1576 | } |
| 1577 | |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1578 | /** |
| 1579 | * @return The geolocation permissions prompt for this tab. |
| 1580 | */ |
| 1581 | GeolocationPermissionsPrompt getGeolocationPermissionsPrompt() { |
Grace Kloba | 50c241e | 2010-04-20 11:07:50 -0700 | [diff] [blame] | 1582 | if (mGeolocationPermissionsPrompt == null) { |
| 1583 | ViewStub stub = (ViewStub) mContainer |
| 1584 | .findViewById(R.id.geolocation_permissions_prompt); |
| 1585 | mGeolocationPermissionsPrompt = (GeolocationPermissionsPrompt) stub |
| 1586 | .inflate(); |
Grace Kloba | 50c241e | 2010-04-20 11:07:50 -0700 | [diff] [blame] | 1587 | } |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1588 | return mGeolocationPermissionsPrompt; |
| 1589 | } |
| 1590 | |
| 1591 | /** |
| 1592 | * @return The application id string |
| 1593 | */ |
| 1594 | String getAppId() { |
| 1595 | return mAppId; |
| 1596 | } |
| 1597 | |
| 1598 | /** |
| 1599 | * Set the application id string |
| 1600 | * @param id |
| 1601 | */ |
| 1602 | void setAppId(String id) { |
| 1603 | mAppId = id; |
| 1604 | } |
| 1605 | |
Michael Kolb | e28b347 | 2011-08-04 16:54:31 -0700 | [diff] [blame] | 1606 | boolean closeOnBack() { |
| 1607 | return mCloseOnBack; |
| 1608 | } |
| 1609 | |
| 1610 | void setCloseOnBack(boolean close) { |
| 1611 | mCloseOnBack = close; |
| 1612 | } |
| 1613 | |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1614 | String getUrl() { |
John Reck | 324d440 | 2011-01-11 16:56:42 -0800 | [diff] [blame] | 1615 | return UrlUtils.filteredUrl(mCurrentState.mUrl); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1616 | } |
| 1617 | |
John Reck | 49a603c | 2011-03-03 09:33:05 -0800 | [diff] [blame] | 1618 | String getOriginalUrl() { |
John Reck | db22ec4 | 2011-06-29 11:31:24 -0700 | [diff] [blame] | 1619 | if (mCurrentState.mOriginalUrl == null) { |
| 1620 | return getUrl(); |
John Reck | 49a603c | 2011-03-03 09:33:05 -0800 | [diff] [blame] | 1621 | } |
John Reck | db22ec4 | 2011-06-29 11:31:24 -0700 | [diff] [blame] | 1622 | return UrlUtils.filteredUrl(mCurrentState.mOriginalUrl); |
John Reck | 49a603c | 2011-03-03 09:33:05 -0800 | [diff] [blame] | 1623 | } |
| 1624 | |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1625 | /** |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 1626 | * Get the title of this tab. |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1627 | */ |
| 1628 | String getTitle() { |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 1629 | if (mCurrentState.mTitle == null && mInPageLoad) { |
Michael Kolb | 1461244 | 2011-06-24 13:06:29 -0700 | [diff] [blame] | 1630 | return mContext.getString(R.string.title_bar_loading); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1631 | } |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 1632 | return mCurrentState.mTitle; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1633 | } |
| 1634 | |
| 1635 | /** |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 1636 | * Get the favicon of this tab. |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1637 | */ |
| 1638 | Bitmap getFavicon() { |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 1639 | if (mCurrentState.mFavicon != null) { |
| 1640 | return mCurrentState.mFavicon; |
| 1641 | } |
| 1642 | return getDefaultFavicon(mContext); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1643 | } |
| 1644 | |
John Reck | e969cc5 | 2010-12-21 17:24:43 -0800 | [diff] [blame] | 1645 | public boolean isBookmarkedSite() { |
| 1646 | return mCurrentState.mIsBookmarkedSite; |
| 1647 | } |
Rob Tsuk | f8bdfce | 2010-10-07 15:41:16 -0700 | [diff] [blame] | 1648 | |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1649 | /** |
| 1650 | * Return the tab's error console. Creates the console if createIfNEcessary |
| 1651 | * is true and we haven't already created the console. |
| 1652 | * @param createIfNecessary Flag to indicate if the console should be |
| 1653 | * created if it has not been already. |
| 1654 | * @return The tab's error console, or null if one has not been created and |
| 1655 | * createIfNecessary is false. |
| 1656 | */ |
| 1657 | ErrorConsoleView getErrorConsole(boolean createIfNecessary) { |
| 1658 | if (createIfNecessary && mErrorConsole == null) { |
Michael Kolb | 1461244 | 2011-06-24 13:06:29 -0700 | [diff] [blame] | 1659 | mErrorConsole = new ErrorConsoleView(mContext); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1660 | mErrorConsole.setWebView(mMainView); |
| 1661 | } |
| 1662 | return mErrorConsole; |
| 1663 | } |
| 1664 | |
Steve Block | 08a6f0c | 2011-10-06 12:12:53 +0100 | [diff] [blame] | 1665 | /** |
| 1666 | * Sets the security state, clears the SSL certificate error and informs |
| 1667 | * the controller. |
| 1668 | */ |
Steve Block | 2466eff | 2011-10-03 15:33:09 +0100 | [diff] [blame] | 1669 | private void setSecurityState(SecurityState securityState) { |
| 1670 | mCurrentState.mSecurityState = securityState; |
Steve Block | 08a6f0c | 2011-10-06 12:12:53 +0100 | [diff] [blame] | 1671 | mCurrentState.mSslCertificateError = null; |
Steve Block | 2466eff | 2011-10-03 15:33:09 +0100 | [diff] [blame] | 1672 | mWebViewController.onUpdatedSecurityState(this); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1673 | } |
| 1674 | |
| 1675 | /** |
Steve Block | 2466eff | 2011-10-03 15:33:09 +0100 | [diff] [blame] | 1676 | * @return The tab's security state. |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1677 | */ |
Steve Block | 2466eff | 2011-10-03 15:33:09 +0100 | [diff] [blame] | 1678 | SecurityState getSecurityState() { |
| 1679 | return mCurrentState.mSecurityState; |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 1680 | } |
| 1681 | |
Steve Block | 08a6f0c | 2011-10-06 12:12:53 +0100 | [diff] [blame] | 1682 | /** |
| 1683 | * Gets the SSL certificate error, if any, for the page's main resource. |
| 1684 | * This is only non-null when the security state is |
| 1685 | * SECURITY_STATE_BAD_CERTIFICATE. |
| 1686 | */ |
| 1687 | SslError getSslCertificateError() { |
| 1688 | return mCurrentState.mSslCertificateError; |
| 1689 | } |
| 1690 | |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 1691 | int getLoadProgress() { |
| 1692 | if (mInPageLoad) { |
| 1693 | return mPageLoadProgress; |
| 1694 | } |
| 1695 | return 100; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1696 | } |
| 1697 | |
| 1698 | /** |
| 1699 | * @return TRUE if onPageStarted is called while onPageFinished is not |
| 1700 | * called yet. |
| 1701 | */ |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1702 | boolean inPageLoad() { |
| 1703 | return mInPageLoad; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1704 | } |
| 1705 | |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1706 | /** |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 1707 | * @return The Bundle with the tab's state if it can be saved, otherwise null |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1708 | */ |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 1709 | public Bundle saveState() { |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1710 | // If the WebView is null it means we ran low on memory and we already |
| 1711 | // stored the saved state in mSavedState. |
| 1712 | if (mMainView == null) { |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 1713 | return mSavedState; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1714 | } |
John Reck | 6c2e2f3 | 2011-08-22 13:41:23 -0700 | [diff] [blame] | 1715 | |
| 1716 | if (TextUtils.isEmpty(mCurrentState.mUrl)) { |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 1717 | return null; |
John Reck | 24f1826 | 2011-06-17 14:47:20 -0700 | [diff] [blame] | 1718 | } |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1719 | |
| 1720 | mSavedState = new Bundle(); |
John Reck | 6c2e2f3 | 2011-08-22 13:41:23 -0700 | [diff] [blame] | 1721 | WebBackForwardList savedList = mMainView.saveState(mSavedState); |
| 1722 | if (savedList == null || savedList.getSize() == 0) { |
| 1723 | Log.w(LOGTAG, "Failed to save back/forward list for " |
| 1724 | + mCurrentState.mUrl); |
| 1725 | } |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1726 | |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 1727 | mSavedState.putLong(ID, mId); |
John Reck | 30c714c | 2010-12-16 17:30:34 -0800 | [diff] [blame] | 1728 | mSavedState.putString(CURRURL, mCurrentState.mUrl); |
| 1729 | mSavedState.putString(CURRTITLE, mCurrentState.mTitle); |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 1730 | mSavedState.putBoolean(INCOGNITO, mMainView.isPrivateBrowsingEnabled()); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1731 | if (mAppId != null) { |
| 1732 | mSavedState.putString(APPID, mAppId); |
| 1733 | } |
Michael Kolb | e28b347 | 2011-08-04 16:54:31 -0700 | [diff] [blame] | 1734 | mSavedState.putBoolean(CLOSEFLAG, mCloseOnBack); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1735 | // Remember the parent tab so the relationship can be restored. |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 1736 | if (mParent != null) { |
| 1737 | mSavedState.putLong(PARENTTAB, mParent.mId); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1738 | } |
John Reck | b0a86db | 2011-05-24 14:05:58 -0700 | [diff] [blame] | 1739 | mSavedState.putBoolean(USERAGENT, |
| 1740 | mSettings.hasDesktopUseragent(getWebView())); |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 1741 | return mSavedState; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1742 | } |
| 1743 | |
| 1744 | /* |
| 1745 | * Restore the state of the tab. |
| 1746 | */ |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 1747 | private void restoreState(Bundle b) { |
| 1748 | mSavedState = b; |
| 1749 | if (mSavedState == null) { |
| 1750 | return; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1751 | } |
| 1752 | // Restore the internal state even if the WebView fails to restore. |
| 1753 | // This will maintain the app id, original url and close-on-exit values. |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 1754 | mId = b.getLong(ID); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1755 | mAppId = b.getString(APPID); |
Michael Kolb | e28b347 | 2011-08-04 16:54:31 -0700 | [diff] [blame] | 1756 | mCloseOnBack = b.getBoolean(CLOSEFLAG); |
John Reck | 8b9bb8b | 2012-03-08 13:19:40 -0800 | [diff] [blame] | 1757 | restoreUserAgent(); |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 1758 | String url = b.getString(CURRURL); |
| 1759 | String title = b.getString(CURRTITLE); |
| 1760 | boolean incognito = b.getBoolean(INCOGNITO); |
| 1761 | mCurrentState = new PageState(mContext, incognito, url, null); |
| 1762 | mCurrentState.mTitle = title; |
| 1763 | synchronized (Tab.this) { |
| 1764 | if (mCapture != null) { |
John Reck | 4eadc34 | 2011-10-31 14:04:10 -0700 | [diff] [blame] | 1765 | DataController.getInstance(mContext).loadThumbnail(this); |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 1766 | } |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1767 | } |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 1768 | } |
Leon Scroggins III | 211ba54 | 2010-04-19 13:21:13 -0400 | [diff] [blame] | 1769 | |
John Reck | 8b9bb8b | 2012-03-08 13:19:40 -0800 | [diff] [blame] | 1770 | private void restoreUserAgent() { |
| 1771 | if (mMainView == null || mSavedState == null) { |
| 1772 | return; |
| 1773 | } |
| 1774 | if (mSavedState.getBoolean(USERAGENT) |
| 1775 | != mSettings.hasDesktopUseragent(mMainView)) { |
| 1776 | mSettings.toggleDesktopUseragent(mMainView); |
| 1777 | } |
| 1778 | } |
| 1779 | |
Leon Scroggins | 1961ed2 | 2010-12-07 15:22:21 -0500 | [diff] [blame] | 1780 | public void updateBookmarkedStatus() { |
John Reck | e969cc5 | 2010-12-21 17:24:43 -0800 | [diff] [blame] | 1781 | mDataController.queryBookmarkStatus(getUrl(), mIsBookmarkCallback); |
Leon Scroggins | 1961ed2 | 2010-12-07 15:22:21 -0500 | [diff] [blame] | 1782 | } |
| 1783 | |
John Reck | e969cc5 | 2010-12-21 17:24:43 -0800 | [diff] [blame] | 1784 | private DataController.OnQueryUrlIsBookmark mIsBookmarkCallback |
| 1785 | = new DataController.OnQueryUrlIsBookmark() { |
| 1786 | @Override |
| 1787 | public void onQueryUrlIsBookmark(String url, boolean isBookmark) { |
| 1788 | if (mCurrentState.mUrl.equals(url)) { |
| 1789 | mCurrentState.mIsBookmarkedSite = isBookmark; |
| 1790 | mWebViewController.bookmarkedStatusHasChanged(Tab.this); |
| 1791 | } |
Leon Scroggins | 1961ed2 | 2010-12-07 15:22:21 -0500 | [diff] [blame] | 1792 | } |
John Reck | e969cc5 | 2010-12-21 17:24:43 -0800 | [diff] [blame] | 1793 | }; |
Michael Kolb | 1acef69 | 2011-03-08 14:12:06 -0800 | [diff] [blame] | 1794 | |
Michael Kolb | eb95db4 | 2011-03-03 10:38:40 -0800 | [diff] [blame] | 1795 | public Bitmap getScreenshot() { |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 1796 | synchronized (Tab.this) { |
| 1797 | return mCapture; |
| 1798 | } |
Michael Kolb | eb95db4 | 2011-03-03 10:38:40 -0800 | [diff] [blame] | 1799 | } |
| 1800 | |
John Reck | 541f55a | 2011-06-07 16:34:43 -0700 | [diff] [blame] | 1801 | public boolean isSnapshot() { |
John Reck | 541f55a | 2011-06-07 16:34:43 -0700 | [diff] [blame] | 1802 | return false; |
| 1803 | } |
| 1804 | |
John Reck | 68234a9 | 2012-04-19 15:27:12 -0700 | [diff] [blame] | 1805 | private static class SaveCallback implements ValueCallback<Boolean> { |
| 1806 | boolean mResult; |
| 1807 | |
| 1808 | @Override |
| 1809 | public void onReceiveValue(Boolean value) { |
| 1810 | mResult = value; |
| 1811 | synchronized (this) { |
| 1812 | notifyAll(); |
John Reck | 8cc9235 | 2011-07-06 17:41:52 -0700 | [diff] [blame] | 1813 | } |
John Reck | 541f55a | 2011-06-07 16:34:43 -0700 | [diff] [blame] | 1814 | } |
John Reck | 68234a9 | 2012-04-19 15:27:12 -0700 | [diff] [blame] | 1815 | |
| 1816 | } |
| 1817 | |
| 1818 | /** |
| 1819 | * Must be called on the UI thread |
| 1820 | */ |
| 1821 | public ContentValues createSnapshotValues() { |
| 1822 | WebViewClassic web = getWebViewClassic(); |
| 1823 | if (web == null) return null; |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 1824 | ContentValues values = new ContentValues(); |
| 1825 | values.put(Snapshots.TITLE, mCurrentState.mTitle); |
| 1826 | values.put(Snapshots.URL, mCurrentState.mUrl); |
John Reck | 68234a9 | 2012-04-19 15:27:12 -0700 | [diff] [blame] | 1827 | values.put(Snapshots.BACKGROUND, web.getPageBackgroundColor()); |
John Reck | 8cc9235 | 2011-07-06 17:41:52 -0700 | [diff] [blame] | 1828 | values.put(Snapshots.DATE_CREATED, System.currentTimeMillis()); |
| 1829 | values.put(Snapshots.FAVICON, compressBitmap(getFavicon())); |
| 1830 | Bitmap screenshot = Controller.createScreenshot(mMainView, |
| 1831 | Controller.getDesiredThumbnailWidth(mContext), |
| 1832 | Controller.getDesiredThumbnailHeight(mContext)); |
| 1833 | values.put(Snapshots.THUMBNAIL, compressBitmap(screenshot)); |
John Reck | d8c7452 | 2011-06-14 08:45:00 -0700 | [diff] [blame] | 1834 | return values; |
John Reck | 541f55a | 2011-06-07 16:34:43 -0700 | [diff] [blame] | 1835 | } |
| 1836 | |
John Reck | 68234a9 | 2012-04-19 15:27:12 -0700 | [diff] [blame] | 1837 | /** |
| 1838 | * Probably want to call this on a background thread |
| 1839 | */ |
| 1840 | public boolean saveViewState(ContentValues values) { |
| 1841 | WebViewClassic web = getWebViewClassic(); |
| 1842 | if (web == null) return false; |
| 1843 | String path = UUID.randomUUID().toString(); |
| 1844 | SaveCallback callback = new SaveCallback(); |
| 1845 | OutputStream outs = null; |
| 1846 | try { |
| 1847 | outs = mContext.openFileOutput(path, Context.MODE_PRIVATE); |
| 1848 | GZIPOutputStream stream = new GZIPOutputStream(outs); |
| 1849 | synchronized (callback) { |
| 1850 | web.saveViewState(stream, callback); |
| 1851 | callback.wait(); |
| 1852 | } |
| 1853 | stream.flush(); |
| 1854 | stream.close(); |
| 1855 | } catch (Exception e) { |
| 1856 | Log.w(LOGTAG, "Failed to save view state", e); |
| 1857 | if (outs != null) { |
| 1858 | try { |
| 1859 | outs.close(); |
| 1860 | } catch (IOException ignore) {} |
| 1861 | } |
| 1862 | File file = mContext.getFileStreamPath(path); |
| 1863 | if (file.exists() && !file.delete()) { |
| 1864 | file.deleteOnExit(); |
| 1865 | } |
| 1866 | return false; |
| 1867 | } |
| 1868 | File savedFile = mContext.getFileStreamPath(path); |
| 1869 | if (!callback.mResult) { |
| 1870 | if (!savedFile.delete()) { |
| 1871 | savedFile.deleteOnExit(); |
| 1872 | } |
| 1873 | return false; |
| 1874 | } |
| 1875 | long size = savedFile.length(); |
| 1876 | values.put(Snapshots.VIEWSTATE_PATH, path); |
| 1877 | values.put(Snapshots.VIEWSTATE_SIZE, size); |
| 1878 | return true; |
| 1879 | } |
| 1880 | |
John Reck | 8cc9235 | 2011-07-06 17:41:52 -0700 | [diff] [blame] | 1881 | public byte[] compressBitmap(Bitmap bitmap) { |
| 1882 | if (bitmap == null) { |
| 1883 | return null; |
| 1884 | } |
| 1885 | ByteArrayOutputStream stream = new ByteArrayOutputStream(); |
| 1886 | bitmap.compress(CompressFormat.PNG, 100, stream); |
| 1887 | return stream.toByteArray(); |
| 1888 | } |
| 1889 | |
John Reck | 26b1832 | 2011-06-21 13:08:58 -0700 | [diff] [blame] | 1890 | public void loadUrl(String url, Map<String, String> headers) { |
| 1891 | if (mMainView != null) { |
Michael Kolb | a53c989 | 2011-10-05 13:31:40 -0700 | [diff] [blame] | 1892 | mPageLoadProgress = INITIAL_PROGRESS; |
| 1893 | mInPageLoad = true; |
Michael Kolb | 1461244 | 2011-06-24 13:06:29 -0700 | [diff] [blame] | 1894 | mCurrentState = new PageState(mContext, false, url, null); |
John Reck | 26b1832 | 2011-06-21 13:08:58 -0700 | [diff] [blame] | 1895 | mWebViewController.onPageStarted(this, mMainView, null); |
| 1896 | mMainView.loadUrl(url, headers); |
| 1897 | } |
| 1898 | } |
| 1899 | |
John Reck | 38b3965 | 2012-06-05 09:22:59 -0700 | [diff] [blame] | 1900 | public void disableUrlOverridingForLoad() { |
| 1901 | mDisableOverrideUrlLoading = true; |
| 1902 | } |
| 1903 | |
Michael Kolb | 9ef259a | 2011-07-12 15:33:08 -0700 | [diff] [blame] | 1904 | protected void capture() { |
| 1905 | if (mMainView == null || mCapture == null) return; |
John Reck | 4eadc34 | 2011-10-31 14:04:10 -0700 | [diff] [blame] | 1906 | if (mMainView.getContentWidth() <= 0 || mMainView.getContentHeight() <= 0) { |
| 1907 | return; |
| 1908 | } |
Michael Kolb | 9ef259a | 2011-07-12 15:33:08 -0700 | [diff] [blame] | 1909 | Canvas c = new Canvas(mCapture); |
| 1910 | final int left = mMainView.getScrollX(); |
| 1911 | final int top = mMainView.getScrollY() + mMainView.getVisibleTitleHeight(); |
Michael Kolb | a3194d0 | 2011-09-07 11:23:51 -0700 | [diff] [blame] | 1912 | int state = c.save(); |
Michael Kolb | 9ef259a | 2011-07-12 15:33:08 -0700 | [diff] [blame] | 1913 | c.translate(-left, -top); |
| 1914 | float scale = mCaptureWidth / (float) mMainView.getWidth(); |
| 1915 | c.scale(scale, scale, left, top); |
John Reck | 8ee633f | 2011-08-09 16:00:35 -0700 | [diff] [blame] | 1916 | if (mMainView instanceof BrowserWebView) { |
| 1917 | ((BrowserWebView)mMainView).drawContent(c); |
| 1918 | } else { |
| 1919 | mMainView.draw(c); |
| 1920 | } |
Michael Kolb | a3194d0 | 2011-09-07 11:23:51 -0700 | [diff] [blame] | 1921 | c.restoreToCount(state); |
| 1922 | // manually anti-alias the edges for the tilt |
| 1923 | c.drawRect(0, 0, 1, mCapture.getHeight(), sAlphaPaint); |
| 1924 | c.drawRect(mCapture.getWidth() - 1, 0, mCapture.getWidth(), |
| 1925 | mCapture.getHeight(), sAlphaPaint); |
| 1926 | c.drawRect(0, 0, mCapture.getWidth(), 1, sAlphaPaint); |
| 1927 | c.drawRect(0, mCapture.getHeight() - 1, mCapture.getWidth(), |
| 1928 | mCapture.getHeight(), sAlphaPaint); |
Dianne Hackborn | 43cfe8a | 2011-08-02 16:59:35 -0700 | [diff] [blame] | 1929 | c.setBitmap(null); |
John Reck | 8ee633f | 2011-08-09 16:00:35 -0700 | [diff] [blame] | 1930 | mHandler.removeMessages(MSG_CAPTURE); |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 1931 | persistThumbnail(); |
John Reck | 8ee633f | 2011-08-09 16:00:35 -0700 | [diff] [blame] | 1932 | TabControl tc = mWebViewController.getTabControl(); |
| 1933 | if (tc != null) { |
| 1934 | OnThumbnailUpdatedListener updateListener |
| 1935 | = tc.getOnThumbnailUpdatedListener(); |
| 1936 | if (updateListener != null) { |
| 1937 | updateListener.onThumbnailUpdated(this); |
| 1938 | } |
| 1939 | } |
Michael Kolb | 9ef259a | 2011-07-12 15:33:08 -0700 | [diff] [blame] | 1940 | } |
| 1941 | |
| 1942 | @Override |
| 1943 | public void onNewPicture(WebView view, Picture picture) { |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 1944 | postCapture(); |
| 1945 | } |
| 1946 | |
| 1947 | private void postCapture() { |
Michael Kolb | 9ef259a | 2011-07-12 15:33:08 -0700 | [diff] [blame] | 1948 | if (!mHandler.hasMessages(MSG_CAPTURE)) { |
| 1949 | mHandler.sendEmptyMessageDelayed(MSG_CAPTURE, CAPTURE_DELAY); |
| 1950 | } |
| 1951 | } |
| 1952 | |
John Reck | ef654f1 | 2011-07-12 16:42:08 -0700 | [diff] [blame] | 1953 | public boolean canGoBack() { |
| 1954 | return mMainView != null ? mMainView.canGoBack() : false; |
| 1955 | } |
| 1956 | |
| 1957 | public boolean canGoForward() { |
| 1958 | return mMainView != null ? mMainView.canGoForward() : false; |
| 1959 | } |
| 1960 | |
| 1961 | public void goBack() { |
| 1962 | if (mMainView != null) { |
| 1963 | mMainView.goBack(); |
| 1964 | } |
| 1965 | } |
| 1966 | |
| 1967 | public void goForward() { |
| 1968 | if (mMainView != null) { |
| 1969 | mMainView.goForward(); |
| 1970 | } |
| 1971 | } |
| 1972 | |
Mathew Inwood | 1dd8e82 | 2011-08-03 14:34:29 +0100 | [diff] [blame] | 1973 | /** |
| 1974 | * Causes the tab back/forward stack to be cleared once, if the given URL is the next URL |
| 1975 | * to be added to the stack. |
| 1976 | * |
| 1977 | * This is used to ensure that preloaded URLs that are not subsequently seen by the user do |
| 1978 | * not appear in the back stack. |
| 1979 | */ |
Mathew Inwood | a829d55 | 2011-09-02 14:16:25 +0100 | [diff] [blame] | 1980 | public void clearBackStackWhenItemAdded(Pattern urlPattern) { |
| 1981 | mClearHistoryUrlPattern = urlPattern; |
Mathew Inwood | 1dd8e82 | 2011-08-03 14:34:29 +0100 | [diff] [blame] | 1982 | } |
| 1983 | |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 1984 | protected void persistThumbnail() { |
John Reck | 4eadc34 | 2011-10-31 14:04:10 -0700 | [diff] [blame] | 1985 | DataController.getInstance(mContext).saveThumbnail(this); |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 1986 | } |
| 1987 | |
| 1988 | protected void deleteThumbnail() { |
John Reck | 4eadc34 | 2011-10-31 14:04:10 -0700 | [diff] [blame] | 1989 | DataController.getInstance(mContext).deleteThumbnail(this); |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 1990 | } |
| 1991 | |
John Reck | 4eadc34 | 2011-10-31 14:04:10 -0700 | [diff] [blame] | 1992 | void updateCaptureFromBlob(byte[] blob) { |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 1993 | synchronized (Tab.this) { |
| 1994 | if (mCapture == null) { |
| 1995 | return; |
| 1996 | } |
Michael Kolb | bd4c00a | 2011-08-02 11:27:12 -0700 | [diff] [blame] | 1997 | ByteBuffer buffer = ByteBuffer.wrap(blob); |
Michael Kolb | d837a11 | 2011-08-09 14:00:18 -0700 | [diff] [blame] | 1998 | try { |
| 1999 | mCapture.copyPixelsFromBuffer(buffer); |
| 2000 | } catch (RuntimeException rex) { |
| 2001 | Log.e(LOGTAG, "Load capture has mismatched sizes; buffer: " |
| 2002 | + buffer.capacity() + " blob: " + blob.length |
| 2003 | + "capture: " + mCapture.getByteCount()); |
| 2004 | throw rex; |
Michael Kolb | bd4c00a | 2011-08-02 11:27:12 -0700 | [diff] [blame] | 2005 | } |
John Reck | 1cf4b79 | 2011-07-26 10:22:22 -0700 | [diff] [blame] | 2006 | } |
| 2007 | } |
| 2008 | |
John Reck | 52be478 | 2011-08-26 15:37:29 -0700 | [diff] [blame] | 2009 | @Override |
| 2010 | public String toString() { |
| 2011 | StringBuilder builder = new StringBuilder(100); |
| 2012 | builder.append(mId); |
| 2013 | builder.append(") has parent: "); |
| 2014 | if (getParent() != null) { |
| 2015 | builder.append("true["); |
| 2016 | builder.append(getParent().getId()); |
| 2017 | builder.append("]"); |
| 2018 | } else { |
| 2019 | builder.append("false"); |
| 2020 | } |
| 2021 | builder.append(", incog: "); |
| 2022 | builder.append(isPrivateBrowsingEnabled()); |
| 2023 | if (!isPrivateBrowsingEnabled()) { |
| 2024 | builder.append(", title: "); |
| 2025 | builder.append(getTitle()); |
| 2026 | builder.append(", url: "); |
| 2027 | builder.append(getUrl()); |
| 2028 | } |
| 2029 | return builder.toString(); |
| 2030 | } |
| 2031 | |
Steve Block | 4895b01 | 2011-10-03 16:26:46 +0100 | [diff] [blame] | 2032 | private void handleProceededAfterSslError(SslError error) { |
| 2033 | if (error.getUrl().equals(mCurrentState.mUrl)) { |
| 2034 | // The security state should currently be SECURITY_STATE_SECURE. |
| 2035 | setSecurityState(SecurityState.SECURITY_STATE_BAD_CERTIFICATE); |
Steve Block | 08a6f0c | 2011-10-06 12:12:53 +0100 | [diff] [blame] | 2036 | mCurrentState.mSslCertificateError = error; |
Steve Block | 4895b01 | 2011-10-03 16:26:46 +0100 | [diff] [blame] | 2037 | } else if (getSecurityState() == SecurityState.SECURITY_STATE_SECURE) { |
Steve Block | 08a6f0c | 2011-10-06 12:12:53 +0100 | [diff] [blame] | 2038 | // The page's main resource is secure and this error is for a |
| 2039 | // sub-resource. |
Steve Block | 4895b01 | 2011-10-03 16:26:46 +0100 | [diff] [blame] | 2040 | setSecurityState(SecurityState.SECURITY_STATE_MIXED); |
| 2041 | } |
| 2042 | } |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 2043 | } |