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