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