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