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