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