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