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