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