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