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 | |
| 17 | package com.android.browser; |
| 18 | |
| 19 | import java.io.File; |
Leon Scroggins | 58d56c6 | 2010-01-28 15:12:40 -0500 | [diff] [blame] | 20 | import java.util.ArrayList; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 21 | import java.util.LinkedList; |
| 22 | import java.util.Vector; |
| 23 | |
| 24 | import android.app.AlertDialog; |
Leon Scroggins | 58d56c6 | 2010-01-28 15:12:40 -0500 | [diff] [blame] | 25 | import android.app.SearchManager; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 26 | import android.content.ContentResolver; |
| 27 | import android.content.ContentValues; |
| 28 | import android.content.DialogInterface; |
| 29 | import android.content.DialogInterface.OnCancelListener; |
Leon Scroggins | 58d56c6 | 2010-01-28 15:12:40 -0500 | [diff] [blame] | 30 | import android.content.Intent; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 31 | import android.database.Cursor; |
| 32 | import android.database.sqlite.SQLiteDatabase; |
| 33 | import android.database.sqlite.SQLiteException; |
| 34 | import android.graphics.Bitmap; |
| 35 | import android.net.Uri; |
| 36 | import android.net.http.SslError; |
| 37 | import android.os.AsyncTask; |
| 38 | import android.os.Bundle; |
| 39 | import android.os.Message; |
Kristian Monsen | 4dce3bf | 2010-02-02 13:37:09 +0000 | [diff] [blame] | 40 | import android.os.SystemClock; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 41 | import android.provider.Browser; |
Leon Scroggins | a1cc3fd | 2010-02-01 16:14:11 -0500 | [diff] [blame] | 42 | import android.speech.RecognizerResultsIntent; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 43 | import android.util.Log; |
| 44 | import android.view.KeyEvent; |
| 45 | import android.view.LayoutInflater; |
| 46 | import android.view.View; |
| 47 | import android.view.ViewGroup; |
| 48 | import android.view.View.OnClickListener; |
| 49 | import android.webkit.CookieSyncManager; |
| 50 | import android.webkit.GeolocationPermissions; |
| 51 | import android.webkit.HttpAuthHandler; |
| 52 | import android.webkit.SslErrorHandler; |
| 53 | import android.webkit.URLUtil; |
| 54 | import android.webkit.ValueCallback; |
| 55 | import android.webkit.WebBackForwardList; |
| 56 | import android.webkit.WebChromeClient; |
| 57 | import android.webkit.WebHistoryItem; |
| 58 | import android.webkit.WebIconDatabase; |
| 59 | import android.webkit.WebStorage; |
| 60 | import android.webkit.WebView; |
| 61 | import android.webkit.WebViewClient; |
| 62 | import android.widget.FrameLayout; |
| 63 | import android.widget.ImageButton; |
| 64 | import android.widget.LinearLayout; |
| 65 | import android.widget.TextView; |
| 66 | |
| 67 | /** |
| 68 | * Class for maintaining Tabs with a main WebView and a subwindow. |
| 69 | */ |
| 70 | class Tab { |
| 71 | // Log Tag |
| 72 | private static final String LOGTAG = "Tab"; |
| 73 | // The Geolocation permissions prompt |
| 74 | private GeolocationPermissionsPrompt mGeolocationPermissionsPrompt; |
| 75 | // Main WebView wrapper |
| 76 | private View mContainer; |
| 77 | // Main WebView |
| 78 | private WebView mMainView; |
| 79 | // Subwindow container |
| 80 | private View mSubViewContainer; |
| 81 | // Subwindow WebView |
| 82 | private WebView mSubView; |
| 83 | // Saved bundle for when we are running low on memory. It contains the |
| 84 | // information needed to restore the WebView if the user goes back to the |
| 85 | // tab. |
| 86 | private Bundle mSavedState; |
| 87 | // Data used when displaying the tab in the picker. |
| 88 | private PickerData mPickerData; |
| 89 | // Parent Tab. This is the Tab that created this Tab, or null if the Tab was |
| 90 | // created by the UI |
| 91 | private Tab mParentTab; |
| 92 | // Tab that constructed by this Tab. This is used when this Tab is |
| 93 | // destroyed, it clears all mParentTab values in the children. |
| 94 | private Vector<Tab> mChildTabs; |
| 95 | // If true, the tab will be removed when back out of the first page. |
| 96 | private boolean mCloseOnExit; |
| 97 | // If true, the tab is in the foreground of the current activity. |
| 98 | private boolean mInForeground; |
| 99 | // If true, the tab is in loading state. |
| 100 | private boolean mInLoad; |
Kristian Monsen | 4dce3bf | 2010-02-02 13:37:09 +0000 | [diff] [blame] | 101 | // The time the load started, used to find load page time |
| 102 | private long mLoadStartTime; |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 103 | // Application identifier used to find tabs that another application wants |
| 104 | // to reuse. |
| 105 | private String mAppId; |
| 106 | // Keep the original url around to avoid killing the old WebView if the url |
| 107 | // has not changed. |
| 108 | private String mOriginalUrl; |
| 109 | // Error console for the tab |
| 110 | private ErrorConsoleView mErrorConsole; |
| 111 | // the lock icon type and previous lock icon type for the tab |
| 112 | private int mLockIconType; |
| 113 | private int mPrevLockIconType; |
| 114 | // Inflation service for making subwindows. |
| 115 | private final LayoutInflater mInflateService; |
| 116 | // The BrowserActivity which owners the Tab |
| 117 | private final BrowserActivity mActivity; |
| 118 | |
| 119 | // AsyncTask for downloading touch icons |
| 120 | DownloadTouchIcon mTouchIconLoader; |
| 121 | |
| 122 | // Extra saved information for displaying the tab in the picker. |
| 123 | private static class PickerData { |
| 124 | String mUrl; |
| 125 | String mTitle; |
| 126 | Bitmap mFavicon; |
| 127 | } |
| 128 | |
| 129 | // Used for saving and restoring each Tab |
| 130 | static final String WEBVIEW = "webview"; |
| 131 | static final String NUMTABS = "numTabs"; |
| 132 | static final String CURRTAB = "currentTab"; |
| 133 | static final String CURRURL = "currentUrl"; |
| 134 | static final String CURRTITLE = "currentTitle"; |
| 135 | static final String CURRPICTURE = "currentPicture"; |
| 136 | static final String CLOSEONEXIT = "closeonexit"; |
| 137 | static final String PARENTTAB = "parentTab"; |
| 138 | static final String APPID = "appid"; |
| 139 | static final String ORIGINALURL = "originalUrl"; |
| 140 | |
| 141 | // ------------------------------------------------------------------------- |
| 142 | |
Leon Scroggins | 58d56c6 | 2010-01-28 15:12:40 -0500 | [diff] [blame] | 143 | /** |
| 144 | * Private information regarding the latest voice search. If the Tab is not |
| 145 | * in voice search mode, this will be null. |
| 146 | */ |
| 147 | private VoiceSearchData mVoiceSearchData; |
| 148 | /** |
| 149 | * Return whether the tab is in voice search mode. |
| 150 | */ |
| 151 | public boolean isInVoiceSearchMode() { |
| 152 | return mVoiceSearchData != null; |
| 153 | } |
| 154 | /** |
| 155 | * Get the title to display for the current voice search page. If the Tab |
| 156 | * is not in voice search mode, return null. |
| 157 | */ |
| 158 | public String getVoiceDisplayTitle() { |
| 159 | if (mVoiceSearchData == null) return null; |
| 160 | return mVoiceSearchData.mLastVoiceSearchTitle; |
| 161 | } |
| 162 | /** |
| 163 | * Get the latest array of voice search results, to be passed to the |
| 164 | * BrowserProvider. If the Tab is not in voice search mode, return null. |
| 165 | */ |
| 166 | public ArrayList<String> getVoiceSearchResults() { |
| 167 | if (mVoiceSearchData == null) return null; |
| 168 | return mVoiceSearchData.mVoiceSearchResults; |
| 169 | } |
| 170 | /** |
| 171 | * Activate voice search mode. |
| 172 | * @param intent Intent which has the results to use, or an index into the |
| 173 | * results when reusing the old results. |
| 174 | */ |
| 175 | /* package */ void activateVoiceSearchMode(Intent intent) { |
Leon Scroggins | 82c1baa | 2010-02-02 16:10:57 -0500 | [diff] [blame^] | 176 | int index = 0; |
Leon Scroggins | 58d56c6 | 2010-01-28 15:12:40 -0500 | [diff] [blame] | 177 | ArrayList<String> results = intent.getStringArrayListExtra( |
Leon Scroggins | a1cc3fd | 2010-02-01 16:14:11 -0500 | [diff] [blame] | 178 | RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_STRINGS); |
Leon Scroggins | 58d56c6 | 2010-01-28 15:12:40 -0500 | [diff] [blame] | 179 | if (results != null) { |
Leon Scroggins | 82c1baa | 2010-02-02 16:10:57 -0500 | [diff] [blame^] | 180 | ArrayList<String> urls = intent.getStringArrayListExtra( |
| 181 | RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_URLS); |
| 182 | ArrayList<String> htmls = intent.getStringArrayListExtra( |
| 183 | RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_HTML); |
| 184 | ArrayList<String> baseUrls = intent.getStringArrayListExtra( |
| 185 | RecognizerResultsIntent |
| 186 | .EXTRA_VOICE_SEARCH_RESULT_HTML_BASE_URLS); |
Leon Scroggins | 58d56c6 | 2010-01-28 15:12:40 -0500 | [diff] [blame] | 187 | // This tab is now entering voice search mode for the first time, or |
| 188 | // a new voice search was done. |
Leon Scroggins | 82c1baa | 2010-02-02 16:10:57 -0500 | [diff] [blame^] | 189 | int size = results.size(); |
| 190 | if (urls == null || size != urls.size()) { |
Leon Scroggins | 58d56c6 | 2010-01-28 15:12:40 -0500 | [diff] [blame] | 191 | throw new AssertionError("improper extras passed in Intent"); |
| 192 | } |
Leon Scroggins | 82c1baa | 2010-02-02 16:10:57 -0500 | [diff] [blame^] | 193 | if (htmls == null || htmls.size() != size || baseUrls == null || |
| 194 | (baseUrls.size() != size && baseUrls.size() != 1)) { |
| 195 | // If either of these arrays are empty/incorrectly sized, ignore |
| 196 | // them. |
| 197 | htmls = null; |
| 198 | baseUrls = null; |
| 199 | } |
| 200 | mVoiceSearchData = new VoiceSearchData(results, urls, htmls, |
| 201 | baseUrls); |
Leon Scroggins | 58d56c6 | 2010-01-28 15:12:40 -0500 | [diff] [blame] | 202 | } else { |
| 203 | String extraData = intent.getStringExtra( |
| 204 | SearchManager.EXTRA_DATA_KEY); |
| 205 | if (extraData != null) { |
Leon Scroggins | 82c1baa | 2010-02-02 16:10:57 -0500 | [diff] [blame^] | 206 | index = Integer.parseInt(extraData); |
| 207 | if (index >= mVoiceSearchData.mVoiceSearchResults.size()) { |
Leon Scroggins | 58d56c6 | 2010-01-28 15:12:40 -0500 | [diff] [blame] | 208 | throw new AssertionError("index must be less than " |
| 209 | + " size of mVoiceSearchResults"); |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | mVoiceSearchData.mLastVoiceSearchTitle |
Leon Scroggins | 82c1baa | 2010-02-02 16:10:57 -0500 | [diff] [blame^] | 214 | = mVoiceSearchData.mVoiceSearchResults.get(index); |
Leon Scroggins | 58d56c6 | 2010-01-28 15:12:40 -0500 | [diff] [blame] | 215 | if (mInForeground) { |
| 216 | mActivity.showVoiceTitleBar(mVoiceSearchData.mLastVoiceSearchTitle); |
| 217 | } |
Leon Scroggins | 82c1baa | 2010-02-02 16:10:57 -0500 | [diff] [blame^] | 218 | if (mVoiceSearchData.mVoiceSearchHtmls != null) { |
| 219 | // When index was found it was already ensured that it was valid |
| 220 | String uriString = mVoiceSearchData.mVoiceSearchHtmls.get(index); |
| 221 | if (uriString != null) { |
| 222 | Uri dataUri = Uri.parse(uriString); |
| 223 | if (RecognizerResultsIntent.URI_SCHEME_INLINE.equals( |
| 224 | dataUri.getScheme())) { |
| 225 | // If there is only one base URL, use it. If there are |
| 226 | // more, there will be one for each index, so use the base |
| 227 | // URL corresponding to the index. |
| 228 | String baseUrl = mVoiceSearchData.mVoiceSearchBaseUrls.get( |
| 229 | mVoiceSearchData.mVoiceSearchBaseUrls.size() > 1 ? |
| 230 | index : 0); |
| 231 | mVoiceSearchData.mLastVoiceSearchUrl = baseUrl; |
| 232 | mMainView.loadDataWithBaseURL(baseUrl, |
| 233 | uriString.substring(RecognizerResultsIntent |
| 234 | .URI_SCHEME_INLINE.length() + 1), "text/html", |
| 235 | "utf-8", baseUrl); |
| 236 | return; |
| 237 | } |
| 238 | } |
| 239 | } |
Leon Scroggins | 58d56c6 | 2010-01-28 15:12:40 -0500 | [diff] [blame] | 240 | mVoiceSearchData.mLastVoiceSearchUrl |
Leon Scroggins | 82c1baa | 2010-02-02 16:10:57 -0500 | [diff] [blame^] | 241 | = mVoiceSearchData.mVoiceSearchUrls.get(index); |
| 242 | if (null == mVoiceSearchData.mLastVoiceSearchUrl) { |
| 243 | mVoiceSearchData.mLastVoiceSearchUrl = mActivity.smartUrlFilter( |
| 244 | mVoiceSearchData.mLastVoiceSearchTitle); |
| 245 | } |
Leon Scroggins | 58d56c6 | 2010-01-28 15:12:40 -0500 | [diff] [blame] | 246 | mMainView.loadUrl(mVoiceSearchData.mLastVoiceSearchUrl); |
| 247 | } |
| 248 | /* package */ static class VoiceSearchData { |
Leon Scroggins | 58d56c6 | 2010-01-28 15:12:40 -0500 | [diff] [blame] | 249 | public VoiceSearchData(ArrayList<String> results, |
Leon Scroggins | 82c1baa | 2010-02-02 16:10:57 -0500 | [diff] [blame^] | 250 | ArrayList<String> urls, ArrayList<String> htmls, |
| 251 | ArrayList<String> baseUrls) { |
Leon Scroggins | 58d56c6 | 2010-01-28 15:12:40 -0500 | [diff] [blame] | 252 | mVoiceSearchResults = results; |
| 253 | mVoiceSearchUrls = urls; |
Leon Scroggins | 82c1baa | 2010-02-02 16:10:57 -0500 | [diff] [blame^] | 254 | mVoiceSearchHtmls = htmls; |
| 255 | mVoiceSearchBaseUrls = baseUrls; |
Leon Scroggins | 58d56c6 | 2010-01-28 15:12:40 -0500 | [diff] [blame] | 256 | } |
| 257 | /* |
| 258 | * ArrayList of suggestions to be displayed when opening the |
| 259 | * SearchManager |
| 260 | */ |
| 261 | public ArrayList<String> mVoiceSearchResults; |
| 262 | /* |
| 263 | * ArrayList of urls, associated with the suggestions in |
| 264 | * mVoiceSearchResults. |
| 265 | */ |
| 266 | public ArrayList<String> mVoiceSearchUrls; |
| 267 | /* |
Leon Scroggins | 82c1baa | 2010-02-02 16:10:57 -0500 | [diff] [blame^] | 268 | * ArrayList holding content to load for each item in |
| 269 | * mVoiceSearchResults. |
| 270 | */ |
| 271 | public ArrayList<String> mVoiceSearchHtmls; |
| 272 | /* |
| 273 | * ArrayList holding base urls for the items in mVoiceSearchResults. |
| 274 | * If non null, this will either have the same size as |
| 275 | * mVoiceSearchResults or have a size of 1, in which case all will use |
| 276 | * the same base url |
| 277 | */ |
| 278 | public ArrayList<String> mVoiceSearchBaseUrls; |
| 279 | /* |
Leon Scroggins | 58d56c6 | 2010-01-28 15:12:40 -0500 | [diff] [blame] | 280 | * The last url provided by voice search. Used for comparison to see if |
Leon Scroggins | 82c1baa | 2010-02-02 16:10:57 -0500 | [diff] [blame^] | 281 | * we are going to a page by some method besides voice search. |
Leon Scroggins | 58d56c6 | 2010-01-28 15:12:40 -0500 | [diff] [blame] | 282 | */ |
| 283 | public String mLastVoiceSearchUrl; |
| 284 | /** |
| 285 | * The last title used for voice search. Needed to update the title bar |
| 286 | * when switching tabs. |
| 287 | */ |
| 288 | public String mLastVoiceSearchTitle; |
Leon Scroggins | 58d56c6 | 2010-01-28 15:12:40 -0500 | [diff] [blame] | 289 | } |
| 290 | |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 291 | // Container class for the next error dialog that needs to be displayed |
| 292 | private class ErrorDialog { |
| 293 | public final int mTitle; |
| 294 | public final String mDescription; |
| 295 | public final int mError; |
| 296 | ErrorDialog(int title, String desc, int error) { |
| 297 | mTitle = title; |
| 298 | mDescription = desc; |
| 299 | mError = error; |
| 300 | } |
| 301 | }; |
| 302 | |
| 303 | private void processNextError() { |
| 304 | if (mQueuedErrors == null) { |
| 305 | return; |
| 306 | } |
| 307 | // The first one is currently displayed so just remove it. |
| 308 | mQueuedErrors.removeFirst(); |
| 309 | if (mQueuedErrors.size() == 0) { |
| 310 | mQueuedErrors = null; |
| 311 | return; |
| 312 | } |
| 313 | showError(mQueuedErrors.getFirst()); |
| 314 | } |
| 315 | |
| 316 | private DialogInterface.OnDismissListener mDialogListener = |
| 317 | new DialogInterface.OnDismissListener() { |
| 318 | public void onDismiss(DialogInterface d) { |
| 319 | processNextError(); |
| 320 | } |
| 321 | }; |
| 322 | private LinkedList<ErrorDialog> mQueuedErrors; |
| 323 | |
| 324 | private void queueError(int err, String desc) { |
| 325 | if (mQueuedErrors == null) { |
| 326 | mQueuedErrors = new LinkedList<ErrorDialog>(); |
| 327 | } |
| 328 | for (ErrorDialog d : mQueuedErrors) { |
| 329 | if (d.mError == err) { |
| 330 | // Already saw a similar error, ignore the new one. |
| 331 | return; |
| 332 | } |
| 333 | } |
| 334 | ErrorDialog errDialog = new ErrorDialog( |
| 335 | err == WebViewClient.ERROR_FILE_NOT_FOUND ? |
| 336 | R.string.browserFrameFileErrorLabel : |
| 337 | R.string.browserFrameNetworkErrorLabel, |
| 338 | desc, err); |
| 339 | mQueuedErrors.addLast(errDialog); |
| 340 | |
| 341 | // Show the dialog now if the queue was empty and it is in foreground |
| 342 | if (mQueuedErrors.size() == 1 && mInForeground) { |
| 343 | showError(errDialog); |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | private void showError(ErrorDialog errDialog) { |
| 348 | if (mInForeground) { |
| 349 | AlertDialog d = new AlertDialog.Builder(mActivity) |
| 350 | .setTitle(errDialog.mTitle) |
| 351 | .setMessage(errDialog.mDescription) |
| 352 | .setPositiveButton(R.string.ok, null) |
| 353 | .create(); |
| 354 | d.setOnDismissListener(mDialogListener); |
| 355 | d.show(); |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | // ------------------------------------------------------------------------- |
| 360 | // WebViewClient implementation for the main WebView |
| 361 | // ------------------------------------------------------------------------- |
| 362 | |
| 363 | private final WebViewClient mWebViewClient = new WebViewClient() { |
| 364 | @Override |
| 365 | public void onPageStarted(WebView view, String url, Bitmap favicon) { |
| 366 | mInLoad = true; |
Kristian Monsen | 4dce3bf | 2010-02-02 13:37:09 +0000 | [diff] [blame] | 367 | mLoadStartTime = SystemClock.uptimeMillis(); |
Leon Scroggins | 58d56c6 | 2010-01-28 15:12:40 -0500 | [diff] [blame] | 368 | if (mVoiceSearchData != null |
| 369 | && !url.equals(mVoiceSearchData.mLastVoiceSearchUrl)) { |
| 370 | mVoiceSearchData = null; |
| 371 | if (mInForeground) { |
| 372 | mActivity.revertVoiceTitleBar(); |
| 373 | } |
| 374 | } |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 375 | |
| 376 | // We've started to load a new page. If there was a pending message |
| 377 | // to save a screenshot then we will now take the new page and save |
| 378 | // an incorrect screenshot. Therefore, remove any pending thumbnail |
| 379 | // messages from the queue. |
| 380 | mActivity.removeMessages(BrowserActivity.UPDATE_BOOKMARK_THUMBNAIL, |
| 381 | view); |
| 382 | |
| 383 | // If we start a touch icon load and then load a new page, we don't |
| 384 | // want to cancel the current touch icon loader. But, we do want to |
| 385 | // create a new one when the touch icon url is known. |
| 386 | if (mTouchIconLoader != null) { |
| 387 | mTouchIconLoader.mTab = null; |
| 388 | mTouchIconLoader = null; |
| 389 | } |
| 390 | |
| 391 | // reset the error console |
| 392 | if (mErrorConsole != null) { |
| 393 | mErrorConsole.clearErrorMessages(); |
| 394 | if (mActivity.shouldShowErrorConsole()) { |
| 395 | mErrorConsole.showConsole(ErrorConsoleView.SHOW_NONE); |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | // update the bookmark database for favicon |
| 400 | if (favicon != null) { |
| 401 | BrowserBookmarksAdapter.updateBookmarkFavicon(mActivity |
| 402 | .getContentResolver(), view.getOriginalUrl(), view |
| 403 | .getUrl(), favicon); |
| 404 | } |
| 405 | |
| 406 | // reset sync timer to avoid sync starts during loading a page |
| 407 | CookieSyncManager.getInstance().resetSync(); |
| 408 | |
| 409 | if (!mActivity.isNetworkUp()) { |
| 410 | view.setNetworkAvailable(false); |
| 411 | } |
| 412 | |
| 413 | // finally update the UI in the activity if it is in the foreground |
| 414 | if (mInForeground) { |
| 415 | mActivity.onPageStarted(view, url, favicon); |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | @Override |
| 420 | public void onPageFinished(WebView view, String url) { |
Kristian Monsen | 4dce3bf | 2010-02-02 13:37:09 +0000 | [diff] [blame] | 421 | LogTag.logPageFinishedLoading( |
| 422 | url, SystemClock.uptimeMillis() - mLoadStartTime); |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 423 | mInLoad = false; |
| 424 | |
| 425 | if (mInForeground && !mActivity.didUserStopLoading() |
| 426 | || !mInForeground) { |
| 427 | // Only update the bookmark screenshot if the user did not |
| 428 | // cancel the load early. |
| 429 | mActivity.postMessage( |
| 430 | BrowserActivity.UPDATE_BOOKMARK_THUMBNAIL, 0, 0, view, |
| 431 | 500); |
| 432 | } |
| 433 | |
| 434 | // finally update the UI in the activity if it is in the foreground |
| 435 | if (mInForeground) { |
| 436 | mActivity.onPageFinished(view, url); |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | // return true if want to hijack the url to let another app to handle it |
| 441 | @Override |
| 442 | public boolean shouldOverrideUrlLoading(WebView view, String url) { |
| 443 | if (mInForeground) { |
| 444 | return mActivity.shouldOverrideUrlLoading(view, url); |
| 445 | } else { |
| 446 | return false; |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | /** |
| 451 | * Updates the lock icon. This method is called when we discover another |
| 452 | * resource to be loaded for this page (for example, javascript). While |
| 453 | * we update the icon type, we do not update the lock icon itself until |
| 454 | * we are done loading, it is slightly more secure this way. |
| 455 | */ |
| 456 | @Override |
| 457 | public void onLoadResource(WebView view, String url) { |
| 458 | if (url != null && url.length() > 0) { |
| 459 | // It is only if the page claims to be secure that we may have |
| 460 | // to update the lock: |
| 461 | if (mLockIconType == BrowserActivity.LOCK_ICON_SECURE) { |
| 462 | // If NOT a 'safe' url, change the lock to mixed content! |
| 463 | if (!(URLUtil.isHttpsUrl(url) || URLUtil.isDataUrl(url) |
| 464 | || URLUtil.isAboutUrl(url))) { |
| 465 | mLockIconType = BrowserActivity.LOCK_ICON_MIXED; |
| 466 | } |
| 467 | } |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | /** |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 472 | * Show a dialog informing the user of the network error reported by |
| 473 | * WebCore if it is in the foreground. |
| 474 | */ |
| 475 | @Override |
| 476 | public void onReceivedError(WebView view, int errorCode, |
| 477 | String description, String failingUrl) { |
| 478 | if (errorCode != WebViewClient.ERROR_HOST_LOOKUP && |
| 479 | errorCode != WebViewClient.ERROR_CONNECT && |
| 480 | errorCode != WebViewClient.ERROR_BAD_URL && |
| 481 | errorCode != WebViewClient.ERROR_UNSUPPORTED_SCHEME && |
| 482 | errorCode != WebViewClient.ERROR_FILE) { |
| 483 | queueError(errorCode, description); |
| 484 | } |
| 485 | Log.e(LOGTAG, "onReceivedError " + errorCode + " " + failingUrl |
| 486 | + " " + description); |
| 487 | |
| 488 | // We need to reset the title after an error if it is in foreground. |
| 489 | if (mInForeground) { |
| 490 | mActivity.resetTitleAndRevertLockIcon(); |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | /** |
| 495 | * Check with the user if it is ok to resend POST data as the page they |
| 496 | * are trying to navigate to is the result of a POST. |
| 497 | */ |
| 498 | @Override |
| 499 | public void onFormResubmission(WebView view, final Message dontResend, |
| 500 | final Message resend) { |
| 501 | if (!mInForeground) { |
| 502 | dontResend.sendToTarget(); |
| 503 | return; |
| 504 | } |
| 505 | new AlertDialog.Builder(mActivity).setTitle( |
| 506 | R.string.browserFrameFormResubmitLabel).setMessage( |
| 507 | R.string.browserFrameFormResubmitMessage) |
| 508 | .setPositiveButton(R.string.ok, |
| 509 | new DialogInterface.OnClickListener() { |
| 510 | public void onClick(DialogInterface dialog, |
| 511 | int which) { |
| 512 | resend.sendToTarget(); |
| 513 | } |
| 514 | }).setNegativeButton(R.string.cancel, |
| 515 | new DialogInterface.OnClickListener() { |
| 516 | public void onClick(DialogInterface dialog, |
| 517 | int which) { |
| 518 | dontResend.sendToTarget(); |
| 519 | } |
| 520 | }).setOnCancelListener(new OnCancelListener() { |
| 521 | public void onCancel(DialogInterface dialog) { |
| 522 | dontResend.sendToTarget(); |
| 523 | } |
| 524 | }).show(); |
| 525 | } |
| 526 | |
| 527 | /** |
| 528 | * Insert the url into the visited history database. |
| 529 | * @param url The url to be inserted. |
| 530 | * @param isReload True if this url is being reloaded. |
| 531 | * FIXME: Not sure what to do when reloading the page. |
| 532 | */ |
| 533 | @Override |
| 534 | public void doUpdateVisitedHistory(WebView view, String url, |
| 535 | boolean isReload) { |
| 536 | if (url.regionMatches(true, 0, "about:", 0, 6)) { |
| 537 | return; |
| 538 | } |
| 539 | // remove "client" before updating it to the history so that it wont |
| 540 | // show up in the auto-complete list. |
| 541 | int index = url.indexOf("client=ms-"); |
| 542 | if (index > 0 && url.contains(".google.")) { |
| 543 | int end = url.indexOf('&', index); |
| 544 | if (end > 0) { |
| 545 | url = url.substring(0, index) |
| 546 | .concat(url.substring(end + 1)); |
| 547 | } else { |
| 548 | // the url.charAt(index-1) should be either '?' or '&' |
| 549 | url = url.substring(0, index-1); |
| 550 | } |
| 551 | } |
| 552 | Browser.updateVisitedHistory(mActivity.getContentResolver(), url, |
| 553 | true); |
| 554 | WebIconDatabase.getInstance().retainIconForPageUrl(url); |
| 555 | } |
| 556 | |
| 557 | /** |
| 558 | * Displays SSL error(s) dialog to the user. |
| 559 | */ |
| 560 | @Override |
| 561 | public void onReceivedSslError(final WebView view, |
| 562 | final SslErrorHandler handler, final SslError error) { |
| 563 | if (!mInForeground) { |
| 564 | handler.cancel(); |
| 565 | return; |
| 566 | } |
| 567 | if (BrowserSettings.getInstance().showSecurityWarnings()) { |
| 568 | final LayoutInflater factory = |
| 569 | LayoutInflater.from(mActivity); |
| 570 | final View warningsView = |
| 571 | factory.inflate(R.layout.ssl_warnings, null); |
| 572 | final LinearLayout placeholder = |
| 573 | (LinearLayout)warningsView.findViewById(R.id.placeholder); |
| 574 | |
| 575 | if (error.hasError(SslError.SSL_UNTRUSTED)) { |
| 576 | LinearLayout ll = (LinearLayout)factory |
| 577 | .inflate(R.layout.ssl_warning, null); |
| 578 | ((TextView)ll.findViewById(R.id.warning)) |
| 579 | .setText(R.string.ssl_untrusted); |
| 580 | placeholder.addView(ll); |
| 581 | } |
| 582 | |
| 583 | if (error.hasError(SslError.SSL_IDMISMATCH)) { |
| 584 | LinearLayout ll = (LinearLayout)factory |
| 585 | .inflate(R.layout.ssl_warning, null); |
| 586 | ((TextView)ll.findViewById(R.id.warning)) |
| 587 | .setText(R.string.ssl_mismatch); |
| 588 | placeholder.addView(ll); |
| 589 | } |
| 590 | |
| 591 | if (error.hasError(SslError.SSL_EXPIRED)) { |
| 592 | LinearLayout ll = (LinearLayout)factory |
| 593 | .inflate(R.layout.ssl_warning, null); |
| 594 | ((TextView)ll.findViewById(R.id.warning)) |
| 595 | .setText(R.string.ssl_expired); |
| 596 | placeholder.addView(ll); |
| 597 | } |
| 598 | |
| 599 | if (error.hasError(SslError.SSL_NOTYETVALID)) { |
| 600 | LinearLayout ll = (LinearLayout)factory |
| 601 | .inflate(R.layout.ssl_warning, null); |
| 602 | ((TextView)ll.findViewById(R.id.warning)) |
| 603 | .setText(R.string.ssl_not_yet_valid); |
| 604 | placeholder.addView(ll); |
| 605 | } |
| 606 | |
| 607 | new AlertDialog.Builder(mActivity).setTitle( |
| 608 | R.string.security_warning).setIcon( |
| 609 | android.R.drawable.ic_dialog_alert).setView( |
| 610 | warningsView).setPositiveButton(R.string.ssl_continue, |
| 611 | new DialogInterface.OnClickListener() { |
| 612 | public void onClick(DialogInterface dialog, |
| 613 | int whichButton) { |
| 614 | handler.proceed(); |
| 615 | } |
| 616 | }).setNeutralButton(R.string.view_certificate, |
| 617 | new DialogInterface.OnClickListener() { |
| 618 | public void onClick(DialogInterface dialog, |
| 619 | int whichButton) { |
| 620 | mActivity.showSSLCertificateOnError(view, |
| 621 | handler, error); |
| 622 | } |
| 623 | }).setNegativeButton(R.string.cancel, |
| 624 | new DialogInterface.OnClickListener() { |
| 625 | public void onClick(DialogInterface dialog, |
| 626 | int whichButton) { |
| 627 | handler.cancel(); |
| 628 | mActivity.resetTitleAndRevertLockIcon(); |
| 629 | } |
| 630 | }).setOnCancelListener( |
| 631 | new DialogInterface.OnCancelListener() { |
| 632 | public void onCancel(DialogInterface dialog) { |
| 633 | handler.cancel(); |
| 634 | mActivity.resetTitleAndRevertLockIcon(); |
| 635 | } |
| 636 | }).show(); |
| 637 | } else { |
| 638 | handler.proceed(); |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | /** |
| 643 | * Handles an HTTP authentication request. |
| 644 | * |
| 645 | * @param handler The authentication handler |
| 646 | * @param host The host |
| 647 | * @param realm The realm |
| 648 | */ |
| 649 | @Override |
| 650 | public void onReceivedHttpAuthRequest(WebView view, |
| 651 | final HttpAuthHandler handler, final String host, |
| 652 | final String realm) { |
| 653 | String username = null; |
| 654 | String password = null; |
| 655 | |
| 656 | boolean reuseHttpAuthUsernamePassword = handler |
| 657 | .useHttpAuthUsernamePassword(); |
| 658 | |
| 659 | if (reuseHttpAuthUsernamePassword && mMainView != null) { |
| 660 | String[] credentials = mMainView.getHttpAuthUsernamePassword( |
| 661 | host, realm); |
| 662 | if (credentials != null && credentials.length == 2) { |
| 663 | username = credentials[0]; |
| 664 | password = credentials[1]; |
| 665 | } |
| 666 | } |
| 667 | |
| 668 | if (username != null && password != null) { |
| 669 | handler.proceed(username, password); |
| 670 | } else { |
| 671 | if (mInForeground) { |
| 672 | mActivity.showHttpAuthentication(handler, host, realm, |
| 673 | null, null, null, 0); |
| 674 | } else { |
| 675 | handler.cancel(); |
| 676 | } |
| 677 | } |
| 678 | } |
| 679 | |
| 680 | @Override |
| 681 | public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) { |
| 682 | if (!mInForeground) { |
| 683 | return false; |
| 684 | } |
| 685 | if (mActivity.isMenuDown()) { |
| 686 | // only check shortcut key when MENU is held |
| 687 | return mActivity.getWindow().isShortcutKey(event.getKeyCode(), |
| 688 | event); |
| 689 | } else { |
| 690 | return false; |
| 691 | } |
| 692 | } |
| 693 | |
| 694 | @Override |
| 695 | public void onUnhandledKeyEvent(WebView view, KeyEvent event) { |
| 696 | if (!mInForeground) { |
| 697 | return; |
| 698 | } |
| 699 | if (event.isDown()) { |
| 700 | mActivity.onKeyDown(event.getKeyCode(), event); |
| 701 | } else { |
| 702 | mActivity.onKeyUp(event.getKeyCode(), event); |
| 703 | } |
| 704 | } |
| 705 | }; |
| 706 | |
| 707 | // ------------------------------------------------------------------------- |
| 708 | // WebChromeClient implementation for the main WebView |
| 709 | // ------------------------------------------------------------------------- |
| 710 | |
| 711 | private final WebChromeClient mWebChromeClient = new WebChromeClient() { |
| 712 | // Helper method to create a new tab or sub window. |
| 713 | private void createWindow(final boolean dialog, final Message msg) { |
| 714 | WebView.WebViewTransport transport = |
| 715 | (WebView.WebViewTransport) msg.obj; |
| 716 | if (dialog) { |
| 717 | createSubWindow(); |
| 718 | mActivity.attachSubWindow(Tab.this); |
| 719 | transport.setWebView(mSubView); |
| 720 | } else { |
| 721 | final Tab newTab = mActivity.openTabAndShow( |
| 722 | BrowserActivity.EMPTY_URL_DATA, false, null); |
| 723 | if (newTab != Tab.this) { |
| 724 | Tab.this.addChildTab(newTab); |
| 725 | } |
| 726 | transport.setWebView(newTab.getWebView()); |
| 727 | } |
| 728 | msg.sendToTarget(); |
| 729 | } |
| 730 | |
| 731 | @Override |
| 732 | public boolean onCreateWindow(WebView view, final boolean dialog, |
| 733 | final boolean userGesture, final Message resultMsg) { |
| 734 | // only allow new window or sub window for the foreground case |
| 735 | if (!mInForeground) { |
| 736 | return false; |
| 737 | } |
| 738 | // Short-circuit if we can't create any more tabs or sub windows. |
| 739 | if (dialog && mSubView != null) { |
| 740 | new AlertDialog.Builder(mActivity) |
| 741 | .setTitle(R.string.too_many_subwindows_dialog_title) |
| 742 | .setIcon(android.R.drawable.ic_dialog_alert) |
| 743 | .setMessage(R.string.too_many_subwindows_dialog_message) |
| 744 | .setPositiveButton(R.string.ok, null) |
| 745 | .show(); |
| 746 | return false; |
| 747 | } else if (!mActivity.getTabControl().canCreateNewTab()) { |
| 748 | new AlertDialog.Builder(mActivity) |
| 749 | .setTitle(R.string.too_many_windows_dialog_title) |
| 750 | .setIcon(android.R.drawable.ic_dialog_alert) |
| 751 | .setMessage(R.string.too_many_windows_dialog_message) |
| 752 | .setPositiveButton(R.string.ok, null) |
| 753 | .show(); |
| 754 | return false; |
| 755 | } |
| 756 | |
| 757 | // Short-circuit if this was a user gesture. |
| 758 | if (userGesture) { |
| 759 | createWindow(dialog, resultMsg); |
| 760 | return true; |
| 761 | } |
| 762 | |
| 763 | // Allow the popup and create the appropriate window. |
| 764 | final AlertDialog.OnClickListener allowListener = |
| 765 | new AlertDialog.OnClickListener() { |
| 766 | public void onClick(DialogInterface d, |
| 767 | int which) { |
| 768 | createWindow(dialog, resultMsg); |
| 769 | } |
| 770 | }; |
| 771 | |
| 772 | // Block the popup by returning a null WebView. |
| 773 | final AlertDialog.OnClickListener blockListener = |
| 774 | new AlertDialog.OnClickListener() { |
| 775 | public void onClick(DialogInterface d, int which) { |
| 776 | resultMsg.sendToTarget(); |
| 777 | } |
| 778 | }; |
| 779 | |
| 780 | // Build a confirmation dialog to display to the user. |
| 781 | final AlertDialog d = |
| 782 | new AlertDialog.Builder(mActivity) |
| 783 | .setTitle(R.string.attention) |
| 784 | .setIcon(android.R.drawable.ic_dialog_alert) |
| 785 | .setMessage(R.string.popup_window_attempt) |
| 786 | .setPositiveButton(R.string.allow, allowListener) |
| 787 | .setNegativeButton(R.string.block, blockListener) |
| 788 | .setCancelable(false) |
| 789 | .create(); |
| 790 | |
| 791 | // Show the confirmation dialog. |
| 792 | d.show(); |
| 793 | return true; |
| 794 | } |
| 795 | |
| 796 | @Override |
Patrick Scott | eb5061b | 2009-11-18 15:00:30 -0500 | [diff] [blame] | 797 | public void onRequestFocus(WebView view) { |
| 798 | if (!mInForeground) { |
| 799 | mActivity.switchToTab(mActivity.getTabControl().getTabIndex( |
| 800 | Tab.this)); |
| 801 | } |
| 802 | } |
| 803 | |
| 804 | @Override |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 805 | public void onCloseWindow(WebView window) { |
| 806 | if (mParentTab != null) { |
| 807 | // JavaScript can only close popup window. |
| 808 | if (mInForeground) { |
| 809 | mActivity.switchToTab(mActivity.getTabControl() |
| 810 | .getTabIndex(mParentTab)); |
| 811 | } |
| 812 | mActivity.closeTab(Tab.this); |
| 813 | } |
| 814 | } |
| 815 | |
| 816 | @Override |
| 817 | public void onProgressChanged(WebView view, int newProgress) { |
| 818 | if (newProgress == 100) { |
| 819 | // sync cookies and cache promptly here. |
| 820 | CookieSyncManager.getInstance().sync(); |
| 821 | } |
| 822 | if (mInForeground) { |
| 823 | mActivity.onProgressChanged(view, newProgress); |
| 824 | } |
| 825 | } |
| 826 | |
| 827 | @Override |
| 828 | public void onReceivedTitle(WebView view, String title) { |
| 829 | String url = view.getUrl(); |
| 830 | if (mInForeground) { |
| 831 | // here, if url is null, we want to reset the title |
| 832 | mActivity.setUrlTitle(url, title); |
| 833 | } |
| 834 | if (url == null || |
| 835 | url.length() >= SQLiteDatabase.SQLITE_MAX_LIKE_PATTERN_LENGTH) { |
| 836 | return; |
| 837 | } |
| 838 | // See if we can find the current url in our history database and |
| 839 | // add the new title to it. |
| 840 | if (url.startsWith("http://www.")) { |
| 841 | url = url.substring(11); |
| 842 | } else if (url.startsWith("http://")) { |
| 843 | url = url.substring(4); |
| 844 | } |
| 845 | try { |
| 846 | final ContentResolver cr = mActivity.getContentResolver(); |
| 847 | url = "%" + url; |
| 848 | String [] selArgs = new String[] { url }; |
| 849 | String where = Browser.BookmarkColumns.URL + " LIKE ? AND " |
| 850 | + Browser.BookmarkColumns.BOOKMARK + " = 0"; |
| 851 | Cursor c = cr.query(Browser.BOOKMARKS_URI, |
| 852 | Browser.HISTORY_PROJECTION, where, selArgs, null); |
| 853 | if (c.moveToFirst()) { |
| 854 | // Current implementation of database only has one entry per |
| 855 | // url. |
| 856 | ContentValues map = new ContentValues(); |
| 857 | map.put(Browser.BookmarkColumns.TITLE, title); |
| 858 | cr.update(Browser.BOOKMARKS_URI, map, "_id = " |
| 859 | + c.getInt(0), null); |
| 860 | } |
| 861 | c.close(); |
| 862 | } catch (IllegalStateException e) { |
| 863 | Log.e(LOGTAG, "Tab onReceived title", e); |
| 864 | } catch (SQLiteException ex) { |
| 865 | Log.e(LOGTAG, "onReceivedTitle() caught SQLiteException: ", ex); |
| 866 | } |
| 867 | } |
| 868 | |
| 869 | @Override |
| 870 | public void onReceivedIcon(WebView view, Bitmap icon) { |
| 871 | if (icon != null) { |
| 872 | BrowserBookmarksAdapter.updateBookmarkFavicon(mActivity |
| 873 | .getContentResolver(), view.getOriginalUrl(), view |
| 874 | .getUrl(), icon); |
| 875 | } |
| 876 | if (mInForeground) { |
| 877 | mActivity.setFavicon(icon); |
| 878 | } |
| 879 | } |
| 880 | |
| 881 | @Override |
| 882 | public void onReceivedTouchIconUrl(WebView view, String url, |
| 883 | boolean precomposed) { |
| 884 | final ContentResolver cr = mActivity.getContentResolver(); |
| 885 | final Cursor c = BrowserBookmarksAdapter.queryBookmarksForUrl(cr, |
| 886 | view.getOriginalUrl(), view.getUrl(), true); |
| 887 | if (c != null) { |
| 888 | if (c.getCount() > 0) { |
| 889 | // Let precomposed icons take precedence over non-composed |
| 890 | // icons. |
| 891 | if (precomposed && mTouchIconLoader != null) { |
| 892 | mTouchIconLoader.cancel(false); |
| 893 | mTouchIconLoader = null; |
| 894 | } |
| 895 | // Have only one async task at a time. |
| 896 | if (mTouchIconLoader == null) { |
| 897 | mTouchIconLoader = new DownloadTouchIcon(Tab.this, cr, |
| 898 | c, view); |
| 899 | mTouchIconLoader.execute(url); |
| 900 | } |
| 901 | } else { |
| 902 | c.close(); |
| 903 | } |
| 904 | } |
| 905 | } |
| 906 | |
| 907 | @Override |
| 908 | public void onShowCustomView(View view, |
| 909 | WebChromeClient.CustomViewCallback callback) { |
| 910 | if (mInForeground) mActivity.onShowCustomView(view, callback); |
| 911 | } |
| 912 | |
| 913 | @Override |
| 914 | public void onHideCustomView() { |
| 915 | if (mInForeground) mActivity.onHideCustomView(); |
| 916 | } |
| 917 | |
| 918 | /** |
| 919 | * The origin has exceeded its database quota. |
| 920 | * @param url the URL that exceeded the quota |
| 921 | * @param databaseIdentifier the identifier of the database on which the |
| 922 | * transaction that caused the quota overflow was run |
| 923 | * @param currentQuota the current quota for the origin. |
| 924 | * @param estimatedSize the estimated size of the database. |
| 925 | * @param totalUsedQuota is the sum of all origins' quota. |
| 926 | * @param quotaUpdater The callback to run when a decision to allow or |
| 927 | * deny quota has been made. Don't forget to call this! |
| 928 | */ |
| 929 | @Override |
| 930 | public void onExceededDatabaseQuota(String url, |
| 931 | String databaseIdentifier, long currentQuota, long estimatedSize, |
| 932 | long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) { |
| 933 | BrowserSettings.getInstance().getWebStorageSizeManager() |
| 934 | .onExceededDatabaseQuota(url, databaseIdentifier, |
| 935 | currentQuota, estimatedSize, totalUsedQuota, |
| 936 | quotaUpdater); |
| 937 | } |
| 938 | |
| 939 | /** |
| 940 | * The Application Cache has exceeded its max size. |
| 941 | * @param spaceNeeded is the amount of disk space that would be needed |
| 942 | * in order for the last appcache operation to succeed. |
| 943 | * @param totalUsedQuota is the sum of all origins' quota. |
| 944 | * @param quotaUpdater A callback to inform the WebCore thread that a |
| 945 | * new app cache size is available. This callback must always |
| 946 | * be executed at some point to ensure that the sleeping |
| 947 | * WebCore thread is woken up. |
| 948 | */ |
| 949 | @Override |
| 950 | public void onReachedMaxAppCacheSize(long spaceNeeded, |
| 951 | long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) { |
| 952 | BrowserSettings.getInstance().getWebStorageSizeManager() |
| 953 | .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota, |
| 954 | quotaUpdater); |
| 955 | } |
| 956 | |
| 957 | /** |
| 958 | * Instructs the browser to show a prompt to ask the user to set the |
| 959 | * Geolocation permission state for the specified origin. |
| 960 | * @param origin The origin for which Geolocation permissions are |
| 961 | * requested. |
| 962 | * @param callback The callback to call once the user has set the |
| 963 | * Geolocation permission state. |
| 964 | */ |
| 965 | @Override |
| 966 | public void onGeolocationPermissionsShowPrompt(String origin, |
| 967 | GeolocationPermissions.Callback callback) { |
| 968 | if (mInForeground) { |
| 969 | mGeolocationPermissionsPrompt.show(origin, callback); |
| 970 | } |
| 971 | } |
| 972 | |
| 973 | /** |
| 974 | * Instructs the browser to hide the Geolocation permissions prompt. |
| 975 | */ |
| 976 | @Override |
| 977 | public void onGeolocationPermissionsHidePrompt() { |
| 978 | if (mInForeground) { |
| 979 | mGeolocationPermissionsPrompt.hide(); |
| 980 | } |
| 981 | } |
| 982 | |
Ben Murdoch | 65acc35 | 2009-11-19 18:16:04 +0000 | [diff] [blame] | 983 | /* Adds a JavaScript error message to the system log and if the JS |
| 984 | * console is enabled in the about:debug options, to that console |
| 985 | * also. |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 986 | * @param message The error message to report. |
| 987 | * @param lineNumber The line number of the error. |
| 988 | * @param sourceID The name of the source file that caused the error. |
| 989 | */ |
| 990 | @Override |
Ben Murdoch | 65acc35 | 2009-11-19 18:16:04 +0000 | [diff] [blame] | 991 | public void onConsoleMessage(String message, int lineNumber, |
Grace Kloba | 22ac16e | 2009-10-07 18:00:23 -0700 | [diff] [blame] | 992 | String sourceID) { |
| 993 | if (mInForeground) { |
| 994 | // call getErrorConsole(true) so it will create one if needed |
| 995 | ErrorConsoleView errorConsole = getErrorConsole(true); |
| 996 | errorConsole.addErrorMessage(message, sourceID, lineNumber); |
| 997 | if (mActivity.shouldShowErrorConsole() |
| 998 | && errorConsole.getShowState() != ErrorConsoleView.SHOW_MAXIMIZED) { |
| 999 | errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED); |
| 1000 | } |
| 1001 | } |
| 1002 | Log.w(LOGTAG, "Console: " + message + " " + sourceID + ":" |
| 1003 | + lineNumber); |
| 1004 | } |
| 1005 | |
| 1006 | /** |
| 1007 | * Ask the browser for an icon to represent a <video> element. |
| 1008 | * This icon will be used if the Web page did not specify a poster attribute. |
| 1009 | * @return Bitmap The icon or null if no such icon is available. |
| 1010 | */ |
| 1011 | @Override |
| 1012 | public Bitmap getDefaultVideoPoster() { |
| 1013 | if (mInForeground) { |
| 1014 | return mActivity.getDefaultVideoPoster(); |
| 1015 | } |
| 1016 | return null; |
| 1017 | } |
| 1018 | |
| 1019 | /** |
| 1020 | * Ask the host application for a custom progress view to show while |
| 1021 | * a <video> is loading. |
| 1022 | * @return View The progress view. |
| 1023 | */ |
| 1024 | @Override |
| 1025 | public View getVideoLoadingProgressView() { |
| 1026 | if (mInForeground) { |
| 1027 | return mActivity.getVideoLoadingProgressView(); |
| 1028 | } |
| 1029 | return null; |
| 1030 | } |
| 1031 | |
| 1032 | @Override |
| 1033 | public void openFileChooser(ValueCallback<Uri> uploadMsg) { |
| 1034 | if (mInForeground) { |
| 1035 | mActivity.openFileChooser(uploadMsg); |
| 1036 | } else { |
| 1037 | uploadMsg.onReceiveValue(null); |
| 1038 | } |
| 1039 | } |
| 1040 | |
| 1041 | /** |
| 1042 | * Deliver a list of already-visited URLs |
| 1043 | */ |
| 1044 | @Override |
| 1045 | public void getVisitedHistory(final ValueCallback<String[]> callback) { |
| 1046 | AsyncTask<Void, Void, String[]> task = new AsyncTask<Void, Void, String[]>() { |
| 1047 | public String[] doInBackground(Void... unused) { |
| 1048 | return Browser.getVisitedHistory(mActivity |
| 1049 | .getContentResolver()); |
| 1050 | } |
| 1051 | public void onPostExecute(String[] result) { |
| 1052 | callback.onReceiveValue(result); |
| 1053 | }; |
| 1054 | }; |
| 1055 | task.execute(); |
| 1056 | }; |
| 1057 | }; |
| 1058 | |
| 1059 | // ------------------------------------------------------------------------- |
| 1060 | // WebViewClient implementation for the sub window |
| 1061 | // ------------------------------------------------------------------------- |
| 1062 | |
| 1063 | // Subclass of WebViewClient used in subwindows to notify the main |
| 1064 | // WebViewClient of certain WebView activities. |
| 1065 | private static class SubWindowClient extends WebViewClient { |
| 1066 | // The main WebViewClient. |
| 1067 | private final WebViewClient mClient; |
| 1068 | |
| 1069 | SubWindowClient(WebViewClient client) { |
| 1070 | mClient = client; |
| 1071 | } |
| 1072 | @Override |
| 1073 | public void doUpdateVisitedHistory(WebView view, String url, |
| 1074 | boolean isReload) { |
| 1075 | mClient.doUpdateVisitedHistory(view, url, isReload); |
| 1076 | } |
| 1077 | @Override |
| 1078 | public boolean shouldOverrideUrlLoading(WebView view, String url) { |
| 1079 | return mClient.shouldOverrideUrlLoading(view, url); |
| 1080 | } |
| 1081 | @Override |
| 1082 | public void onReceivedSslError(WebView view, SslErrorHandler handler, |
| 1083 | SslError error) { |
| 1084 | mClient.onReceivedSslError(view, handler, error); |
| 1085 | } |
| 1086 | @Override |
| 1087 | public void onReceivedHttpAuthRequest(WebView view, |
| 1088 | HttpAuthHandler handler, String host, String realm) { |
| 1089 | mClient.onReceivedHttpAuthRequest(view, handler, host, realm); |
| 1090 | } |
| 1091 | @Override |
| 1092 | public void onFormResubmission(WebView view, Message dontResend, |
| 1093 | Message resend) { |
| 1094 | mClient.onFormResubmission(view, dontResend, resend); |
| 1095 | } |
| 1096 | @Override |
| 1097 | public void onReceivedError(WebView view, int errorCode, |
| 1098 | String description, String failingUrl) { |
| 1099 | mClient.onReceivedError(view, errorCode, description, failingUrl); |
| 1100 | } |
| 1101 | @Override |
| 1102 | public boolean shouldOverrideKeyEvent(WebView view, |
| 1103 | android.view.KeyEvent event) { |
| 1104 | return mClient.shouldOverrideKeyEvent(view, event); |
| 1105 | } |
| 1106 | @Override |
| 1107 | public void onUnhandledKeyEvent(WebView view, |
| 1108 | android.view.KeyEvent event) { |
| 1109 | mClient.onUnhandledKeyEvent(view, event); |
| 1110 | } |
| 1111 | } |
| 1112 | |
| 1113 | // ------------------------------------------------------------------------- |
| 1114 | // WebChromeClient implementation for the sub window |
| 1115 | // ------------------------------------------------------------------------- |
| 1116 | |
| 1117 | private class SubWindowChromeClient extends WebChromeClient { |
| 1118 | // The main WebChromeClient. |
| 1119 | private final WebChromeClient mClient; |
| 1120 | |
| 1121 | SubWindowChromeClient(WebChromeClient client) { |
| 1122 | mClient = client; |
| 1123 | } |
| 1124 | @Override |
| 1125 | public void onProgressChanged(WebView view, int newProgress) { |
| 1126 | mClient.onProgressChanged(view, newProgress); |
| 1127 | } |
| 1128 | @Override |
| 1129 | public boolean onCreateWindow(WebView view, boolean dialog, |
| 1130 | boolean userGesture, android.os.Message resultMsg) { |
| 1131 | return mClient.onCreateWindow(view, dialog, userGesture, resultMsg); |
| 1132 | } |
| 1133 | @Override |
| 1134 | public void onCloseWindow(WebView window) { |
| 1135 | if (window != mSubView) { |
| 1136 | Log.e(LOGTAG, "Can't close the window"); |
| 1137 | } |
| 1138 | mActivity.dismissSubWindow(Tab.this); |
| 1139 | } |
| 1140 | } |
| 1141 | |
| 1142 | // ------------------------------------------------------------------------- |
| 1143 | |
| 1144 | // Construct a new tab |
| 1145 | Tab(BrowserActivity activity, WebView w, boolean closeOnExit, String appId, |
| 1146 | String url) { |
| 1147 | mActivity = activity; |
| 1148 | mCloseOnExit = closeOnExit; |
| 1149 | mAppId = appId; |
| 1150 | mOriginalUrl = url; |
| 1151 | mLockIconType = BrowserActivity.LOCK_ICON_UNSECURE; |
| 1152 | mPrevLockIconType = BrowserActivity.LOCK_ICON_UNSECURE; |
| 1153 | mInLoad = false; |
| 1154 | mInForeground = false; |
| 1155 | |
| 1156 | mInflateService = LayoutInflater.from(activity); |
| 1157 | |
| 1158 | // The tab consists of a container view, which contains the main |
| 1159 | // WebView, as well as any other UI elements associated with the tab. |
| 1160 | mContainer = mInflateService.inflate(R.layout.tab, null); |
| 1161 | |
| 1162 | mGeolocationPermissionsPrompt = |
| 1163 | (GeolocationPermissionsPrompt) mContainer.findViewById( |
| 1164 | R.id.geolocation_permissions_prompt); |
| 1165 | |
| 1166 | setWebView(w); |
| 1167 | } |
| 1168 | |
| 1169 | /** |
| 1170 | * Sets the WebView for this tab, correctly removing the old WebView from |
| 1171 | * the container view. |
| 1172 | */ |
| 1173 | void setWebView(WebView w) { |
| 1174 | if (mMainView == w) { |
| 1175 | return; |
| 1176 | } |
| 1177 | // If the WebView is changing, the page will be reloaded, so any ongoing |
| 1178 | // Geolocation permission requests are void. |
| 1179 | mGeolocationPermissionsPrompt.hide(); |
| 1180 | |
| 1181 | // Just remove the old one. |
| 1182 | FrameLayout wrapper = |
| 1183 | (FrameLayout) mContainer.findViewById(R.id.webview_wrapper); |
| 1184 | wrapper.removeView(mMainView); |
| 1185 | |
| 1186 | // set the new one |
| 1187 | mMainView = w; |
| 1188 | // attached the WebViewClient and WebChromeClient |
| 1189 | if (mMainView != null) { |
| 1190 | mMainView.setWebViewClient(mWebViewClient); |
| 1191 | mMainView.setWebChromeClient(mWebChromeClient); |
| 1192 | } |
| 1193 | } |
| 1194 | |
| 1195 | /** |
| 1196 | * Destroy the tab's main WebView and subWindow if any |
| 1197 | */ |
| 1198 | void destroy() { |
| 1199 | if (mMainView != null) { |
| 1200 | dismissSubWindow(); |
| 1201 | BrowserSettings.getInstance().deleteObserver(mMainView.getSettings()); |
| 1202 | // save the WebView to call destroy() after detach it from the tab |
| 1203 | WebView webView = mMainView; |
| 1204 | setWebView(null); |
| 1205 | webView.destroy(); |
| 1206 | } |
| 1207 | } |
| 1208 | |
| 1209 | /** |
| 1210 | * Remove the tab from the parent |
| 1211 | */ |
| 1212 | void removeFromTree() { |
| 1213 | // detach the children |
| 1214 | if (mChildTabs != null) { |
| 1215 | for(Tab t : mChildTabs) { |
| 1216 | t.setParentTab(null); |
| 1217 | } |
| 1218 | } |
| 1219 | // remove itself from the parent list |
| 1220 | if (mParentTab != null) { |
| 1221 | mParentTab.mChildTabs.remove(this); |
| 1222 | } |
| 1223 | } |
| 1224 | |
| 1225 | /** |
| 1226 | * Create a new subwindow unless a subwindow already exists. |
| 1227 | * @return True if a new subwindow was created. False if one already exists. |
| 1228 | */ |
| 1229 | boolean createSubWindow() { |
| 1230 | if (mSubView == null) { |
| 1231 | mSubViewContainer = mInflateService.inflate( |
| 1232 | R.layout.browser_subwindow, null); |
| 1233 | mSubView = (WebView) mSubViewContainer.findViewById(R.id.webview); |
| 1234 | // use trackball directly |
| 1235 | mSubView.setMapTrackballToArrowKeys(false); |
| 1236 | mSubView.setWebViewClient(new SubWindowClient(mWebViewClient)); |
| 1237 | mSubView.setWebChromeClient(new SubWindowChromeClient( |
| 1238 | mWebChromeClient)); |
| 1239 | mSubView.setDownloadListener(mActivity); |
| 1240 | mSubView.setOnCreateContextMenuListener(mActivity); |
| 1241 | final BrowserSettings s = BrowserSettings.getInstance(); |
| 1242 | s.addObserver(mSubView.getSettings()).update(s, null); |
| 1243 | final ImageButton cancel = (ImageButton) mSubViewContainer |
| 1244 | .findViewById(R.id.subwindow_close); |
| 1245 | cancel.setOnClickListener(new OnClickListener() { |
| 1246 | public void onClick(View v) { |
| 1247 | mSubView.getWebChromeClient().onCloseWindow(mSubView); |
| 1248 | } |
| 1249 | }); |
| 1250 | return true; |
| 1251 | } |
| 1252 | return false; |
| 1253 | } |
| 1254 | |
| 1255 | /** |
| 1256 | * Dismiss the subWindow for the tab. |
| 1257 | */ |
| 1258 | void dismissSubWindow() { |
| 1259 | if (mSubView != null) { |
| 1260 | BrowserSettings.getInstance().deleteObserver( |
| 1261 | mSubView.getSettings()); |
| 1262 | mSubView.destroy(); |
| 1263 | mSubView = null; |
| 1264 | mSubViewContainer = null; |
| 1265 | } |
| 1266 | } |
| 1267 | |
| 1268 | /** |
| 1269 | * Attach the sub window to the content view. |
| 1270 | */ |
| 1271 | void attachSubWindow(ViewGroup content) { |
| 1272 | if (mSubView != null) { |
| 1273 | content.addView(mSubViewContainer, |
| 1274 | BrowserActivity.COVER_SCREEN_PARAMS); |
| 1275 | } |
| 1276 | } |
| 1277 | |
| 1278 | /** |
| 1279 | * Remove the sub window from the content view. |
| 1280 | */ |
| 1281 | void removeSubWindow(ViewGroup content) { |
| 1282 | if (mSubView != null) { |
| 1283 | content.removeView(mSubViewContainer); |
| 1284 | } |
| 1285 | } |
| 1286 | |
| 1287 | /** |
| 1288 | * This method attaches both the WebView and any sub window to the |
| 1289 | * given content view. |
| 1290 | */ |
| 1291 | void attachTabToContentView(ViewGroup content) { |
| 1292 | if (mMainView == null) { |
| 1293 | return; |
| 1294 | } |
| 1295 | |
| 1296 | // Attach the WebView to the container and then attach the |
| 1297 | // container to the content view. |
| 1298 | FrameLayout wrapper = |
| 1299 | (FrameLayout) mContainer.findViewById(R.id.webview_wrapper); |
| 1300 | wrapper.addView(mMainView); |
| 1301 | content.addView(mContainer, BrowserActivity.COVER_SCREEN_PARAMS); |
| 1302 | attachSubWindow(content); |
| 1303 | } |
| 1304 | |
| 1305 | /** |
| 1306 | * Remove the WebView and any sub window from the given content view. |
| 1307 | */ |
| 1308 | void removeTabFromContentView(ViewGroup content) { |
| 1309 | if (mMainView == null) { |
| 1310 | return; |
| 1311 | } |
| 1312 | |
| 1313 | // Remove the container from the content and then remove the |
| 1314 | // WebView from the container. This will trigger a focus change |
| 1315 | // needed by WebView. |
| 1316 | FrameLayout wrapper = |
| 1317 | (FrameLayout) mContainer.findViewById(R.id.webview_wrapper); |
| 1318 | wrapper.removeView(mMainView); |
| 1319 | content.removeView(mContainer); |
| 1320 | removeSubWindow(content); |
| 1321 | } |
| 1322 | |
| 1323 | /** |
| 1324 | * Set the parent tab of this tab. |
| 1325 | */ |
| 1326 | void setParentTab(Tab parent) { |
| 1327 | mParentTab = parent; |
| 1328 | // This tab may have been freed due to low memory. If that is the case, |
| 1329 | // the parent tab index is already saved. If we are changing that index |
| 1330 | // (most likely due to removing the parent tab) we must update the |
| 1331 | // parent tab index in the saved Bundle. |
| 1332 | if (mSavedState != null) { |
| 1333 | if (parent == null) { |
| 1334 | mSavedState.remove(PARENTTAB); |
| 1335 | } else { |
| 1336 | mSavedState.putInt(PARENTTAB, mActivity.getTabControl() |
| 1337 | .getTabIndex(parent)); |
| 1338 | } |
| 1339 | } |
| 1340 | } |
| 1341 | |
| 1342 | /** |
| 1343 | * When a Tab is created through the content of another Tab, then we |
| 1344 | * associate the Tabs. |
| 1345 | * @param child the Tab that was created from this Tab |
| 1346 | */ |
| 1347 | void addChildTab(Tab child) { |
| 1348 | if (mChildTabs == null) { |
| 1349 | mChildTabs = new Vector<Tab>(); |
| 1350 | } |
| 1351 | mChildTabs.add(child); |
| 1352 | child.setParentTab(this); |
| 1353 | } |
| 1354 | |
| 1355 | Vector<Tab> getChildTabs() { |
| 1356 | return mChildTabs; |
| 1357 | } |
| 1358 | |
| 1359 | void resume() { |
| 1360 | if (mMainView != null) { |
| 1361 | mMainView.onResume(); |
| 1362 | if (mSubView != null) { |
| 1363 | mSubView.onResume(); |
| 1364 | } |
| 1365 | } |
| 1366 | } |
| 1367 | |
| 1368 | void pause() { |
| 1369 | if (mMainView != null) { |
| 1370 | mMainView.onPause(); |
| 1371 | if (mSubView != null) { |
| 1372 | mSubView.onPause(); |
| 1373 | } |
| 1374 | } |
| 1375 | } |
| 1376 | |
| 1377 | void putInForeground() { |
| 1378 | mInForeground = true; |
| 1379 | resume(); |
| 1380 | mMainView.setOnCreateContextMenuListener(mActivity); |
| 1381 | if (mSubView != null) { |
| 1382 | mSubView.setOnCreateContextMenuListener(mActivity); |
| 1383 | } |
| 1384 | // Show the pending error dialog if the queue is not empty |
| 1385 | if (mQueuedErrors != null && mQueuedErrors.size() > 0) { |
| 1386 | showError(mQueuedErrors.getFirst()); |
| 1387 | } |
| 1388 | } |
| 1389 | |
| 1390 | void putInBackground() { |
| 1391 | mInForeground = false; |
| 1392 | pause(); |
| 1393 | mMainView.setOnCreateContextMenuListener(null); |
| 1394 | if (mSubView != null) { |
| 1395 | mSubView.setOnCreateContextMenuListener(null); |
| 1396 | } |
| 1397 | } |
| 1398 | |
| 1399 | /** |
| 1400 | * Return the top window of this tab; either the subwindow if it is not |
| 1401 | * null or the main window. |
| 1402 | * @return The top window of this tab. |
| 1403 | */ |
| 1404 | WebView getTopWindow() { |
| 1405 | if (mSubView != null) { |
| 1406 | return mSubView; |
| 1407 | } |
| 1408 | return mMainView; |
| 1409 | } |
| 1410 | |
| 1411 | /** |
| 1412 | * Return the main window of this tab. Note: if a tab is freed in the |
| 1413 | * background, this can return null. It is only guaranteed to be |
| 1414 | * non-null for the current tab. |
| 1415 | * @return The main WebView of this tab. |
| 1416 | */ |
| 1417 | WebView getWebView() { |
| 1418 | return mMainView; |
| 1419 | } |
| 1420 | |
| 1421 | /** |
| 1422 | * Return the subwindow of this tab or null if there is no subwindow. |
| 1423 | * @return The subwindow of this tab or null. |
| 1424 | */ |
| 1425 | WebView getSubWebView() { |
| 1426 | return mSubView; |
| 1427 | } |
| 1428 | |
| 1429 | /** |
| 1430 | * @return The geolocation permissions prompt for this tab. |
| 1431 | */ |
| 1432 | GeolocationPermissionsPrompt getGeolocationPermissionsPrompt() { |
| 1433 | return mGeolocationPermissionsPrompt; |
| 1434 | } |
| 1435 | |
| 1436 | /** |
| 1437 | * @return The application id string |
| 1438 | */ |
| 1439 | String getAppId() { |
| 1440 | return mAppId; |
| 1441 | } |
| 1442 | |
| 1443 | /** |
| 1444 | * Set the application id string |
| 1445 | * @param id |
| 1446 | */ |
| 1447 | void setAppId(String id) { |
| 1448 | mAppId = id; |
| 1449 | } |
| 1450 | |
| 1451 | /** |
| 1452 | * @return The original url associated with this Tab |
| 1453 | */ |
| 1454 | String getOriginalUrl() { |
| 1455 | return mOriginalUrl; |
| 1456 | } |
| 1457 | |
| 1458 | /** |
| 1459 | * Set the original url associated with this tab |
| 1460 | */ |
| 1461 | void setOriginalUrl(String url) { |
| 1462 | mOriginalUrl = url; |
| 1463 | } |
| 1464 | |
| 1465 | /** |
| 1466 | * Get the url of this tab. Valid after calling populatePickerData, but |
| 1467 | * before calling wipePickerData, or if the webview has been destroyed. |
| 1468 | * @return The WebView's url or null. |
| 1469 | */ |
| 1470 | String getUrl() { |
| 1471 | if (mPickerData != null) { |
| 1472 | return mPickerData.mUrl; |
| 1473 | } |
| 1474 | return null; |
| 1475 | } |
| 1476 | |
| 1477 | /** |
| 1478 | * Get the title of this tab. Valid after calling populatePickerData, but |
| 1479 | * before calling wipePickerData, or if the webview has been destroyed. If |
| 1480 | * the url has no title, use the url instead. |
| 1481 | * @return The WebView's title (or url) or null. |
| 1482 | */ |
| 1483 | String getTitle() { |
| 1484 | if (mPickerData != null) { |
| 1485 | return mPickerData.mTitle; |
| 1486 | } |
| 1487 | return null; |
| 1488 | } |
| 1489 | |
| 1490 | /** |
| 1491 | * Get the favicon of this tab. Valid after calling populatePickerData, but |
| 1492 | * before calling wipePickerData, or if the webview has been destroyed. |
| 1493 | * @return The WebView's favicon or null. |
| 1494 | */ |
| 1495 | Bitmap getFavicon() { |
| 1496 | if (mPickerData != null) { |
| 1497 | return mPickerData.mFavicon; |
| 1498 | } |
| 1499 | return null; |
| 1500 | } |
| 1501 | |
| 1502 | /** |
| 1503 | * Return the tab's error console. Creates the console if createIfNEcessary |
| 1504 | * is true and we haven't already created the console. |
| 1505 | * @param createIfNecessary Flag to indicate if the console should be |
| 1506 | * created if it has not been already. |
| 1507 | * @return The tab's error console, or null if one has not been created and |
| 1508 | * createIfNecessary is false. |
| 1509 | */ |
| 1510 | ErrorConsoleView getErrorConsole(boolean createIfNecessary) { |
| 1511 | if (createIfNecessary && mErrorConsole == null) { |
| 1512 | mErrorConsole = new ErrorConsoleView(mActivity); |
| 1513 | mErrorConsole.setWebView(mMainView); |
| 1514 | } |
| 1515 | return mErrorConsole; |
| 1516 | } |
| 1517 | |
| 1518 | /** |
| 1519 | * If this Tab was created through another Tab, then this method returns |
| 1520 | * that Tab. |
| 1521 | * @return the Tab parent or null |
| 1522 | */ |
| 1523 | public Tab getParentTab() { |
| 1524 | return mParentTab; |
| 1525 | } |
| 1526 | |
| 1527 | /** |
| 1528 | * Return whether this tab should be closed when it is backing out of the |
| 1529 | * first page. |
| 1530 | * @return TRUE if this tab should be closed when exit. |
| 1531 | */ |
| 1532 | boolean closeOnExit() { |
| 1533 | return mCloseOnExit; |
| 1534 | } |
| 1535 | |
| 1536 | /** |
| 1537 | * Saves the current lock-icon state before resetting the lock icon. If we |
| 1538 | * have an error, we may need to roll back to the previous state. |
| 1539 | */ |
| 1540 | void resetLockIcon(String url) { |
| 1541 | mPrevLockIconType = mLockIconType; |
| 1542 | mLockIconType = BrowserActivity.LOCK_ICON_UNSECURE; |
| 1543 | if (URLUtil.isHttpsUrl(url)) { |
| 1544 | mLockIconType = BrowserActivity.LOCK_ICON_SECURE; |
| 1545 | } |
| 1546 | } |
| 1547 | |
| 1548 | /** |
| 1549 | * Reverts the lock-icon state to the last saved state, for example, if we |
| 1550 | * had an error, and need to cancel the load. |
| 1551 | */ |
| 1552 | void revertLockIcon() { |
| 1553 | mLockIconType = mPrevLockIconType; |
| 1554 | } |
| 1555 | |
| 1556 | /** |
| 1557 | * @return The tab's lock icon type. |
| 1558 | */ |
| 1559 | int getLockIconType() { |
| 1560 | return mLockIconType; |
| 1561 | } |
| 1562 | |
| 1563 | /** |
| 1564 | * @return TRUE if onPageStarted is called while onPageFinished is not |
| 1565 | * called yet. |
| 1566 | */ |
| 1567 | boolean inLoad() { |
| 1568 | return mInLoad; |
| 1569 | } |
| 1570 | |
| 1571 | // force mInLoad to be false. This should only be called before closing the |
| 1572 | // tab to ensure BrowserActivity's pauseWebViewTimers() is called correctly. |
| 1573 | void clearInLoad() { |
| 1574 | mInLoad = false; |
| 1575 | } |
| 1576 | |
| 1577 | void populatePickerData() { |
| 1578 | if (mMainView == null) { |
| 1579 | populatePickerDataFromSavedState(); |
| 1580 | return; |
| 1581 | } |
| 1582 | |
| 1583 | // FIXME: The only place we cared about subwindow was for |
| 1584 | // bookmarking (i.e. not when saving state). Was this deliberate? |
| 1585 | final WebBackForwardList list = mMainView.copyBackForwardList(); |
| 1586 | final WebHistoryItem item = list != null ? list.getCurrentItem() : null; |
| 1587 | populatePickerData(item); |
| 1588 | } |
| 1589 | |
| 1590 | // Populate the picker data using the given history item and the current top |
| 1591 | // WebView. |
| 1592 | private void populatePickerData(WebHistoryItem item) { |
| 1593 | mPickerData = new PickerData(); |
| 1594 | if (item != null) { |
| 1595 | mPickerData.mUrl = item.getUrl(); |
| 1596 | mPickerData.mTitle = item.getTitle(); |
| 1597 | mPickerData.mFavicon = item.getFavicon(); |
| 1598 | if (mPickerData.mTitle == null) { |
| 1599 | mPickerData.mTitle = mPickerData.mUrl; |
| 1600 | } |
| 1601 | } |
| 1602 | } |
| 1603 | |
| 1604 | // Create the PickerData and populate it using the saved state of the tab. |
| 1605 | void populatePickerDataFromSavedState() { |
| 1606 | if (mSavedState == null) { |
| 1607 | return; |
| 1608 | } |
| 1609 | mPickerData = new PickerData(); |
| 1610 | mPickerData.mUrl = mSavedState.getString(CURRURL); |
| 1611 | mPickerData.mTitle = mSavedState.getString(CURRTITLE); |
| 1612 | } |
| 1613 | |
| 1614 | void clearPickerData() { |
| 1615 | mPickerData = null; |
| 1616 | } |
| 1617 | |
| 1618 | /** |
| 1619 | * Get the saved state bundle. |
| 1620 | * @return |
| 1621 | */ |
| 1622 | Bundle getSavedState() { |
| 1623 | return mSavedState; |
| 1624 | } |
| 1625 | |
| 1626 | /** |
| 1627 | * Set the saved state. |
| 1628 | */ |
| 1629 | void setSavedState(Bundle state) { |
| 1630 | mSavedState = state; |
| 1631 | } |
| 1632 | |
| 1633 | /** |
| 1634 | * @return TRUE if succeed in saving the state. |
| 1635 | */ |
| 1636 | boolean saveState() { |
| 1637 | // If the WebView is null it means we ran low on memory and we already |
| 1638 | // stored the saved state in mSavedState. |
| 1639 | if (mMainView == null) { |
| 1640 | return mSavedState != null; |
| 1641 | } |
| 1642 | |
| 1643 | mSavedState = new Bundle(); |
| 1644 | final WebBackForwardList list = mMainView.saveState(mSavedState); |
| 1645 | if (list != null) { |
| 1646 | final File f = new File(mActivity.getTabControl().getThumbnailDir(), |
| 1647 | mMainView.hashCode() + "_pic.save"); |
| 1648 | if (mMainView.savePicture(mSavedState, f)) { |
| 1649 | mSavedState.putString(CURRPICTURE, f.getPath()); |
| 1650 | } |
| 1651 | } |
| 1652 | |
| 1653 | // Store some extra info for displaying the tab in the picker. |
| 1654 | final WebHistoryItem item = list != null ? list.getCurrentItem() : null; |
| 1655 | populatePickerData(item); |
| 1656 | |
| 1657 | if (mPickerData.mUrl != null) { |
| 1658 | mSavedState.putString(CURRURL, mPickerData.mUrl); |
| 1659 | } |
| 1660 | if (mPickerData.mTitle != null) { |
| 1661 | mSavedState.putString(CURRTITLE, mPickerData.mTitle); |
| 1662 | } |
| 1663 | mSavedState.putBoolean(CLOSEONEXIT, mCloseOnExit); |
| 1664 | if (mAppId != null) { |
| 1665 | mSavedState.putString(APPID, mAppId); |
| 1666 | } |
| 1667 | if (mOriginalUrl != null) { |
| 1668 | mSavedState.putString(ORIGINALURL, mOriginalUrl); |
| 1669 | } |
| 1670 | // Remember the parent tab so the relationship can be restored. |
| 1671 | if (mParentTab != null) { |
| 1672 | mSavedState.putInt(PARENTTAB, mActivity.getTabControl().getTabIndex( |
| 1673 | mParentTab)); |
| 1674 | } |
| 1675 | return true; |
| 1676 | } |
| 1677 | |
| 1678 | /* |
| 1679 | * Restore the state of the tab. |
| 1680 | */ |
| 1681 | boolean restoreState(Bundle b) { |
| 1682 | if (b == null) { |
| 1683 | return false; |
| 1684 | } |
| 1685 | // Restore the internal state even if the WebView fails to restore. |
| 1686 | // This will maintain the app id, original url and close-on-exit values. |
| 1687 | mSavedState = null; |
| 1688 | mPickerData = null; |
| 1689 | mCloseOnExit = b.getBoolean(CLOSEONEXIT); |
| 1690 | mAppId = b.getString(APPID); |
| 1691 | mOriginalUrl = b.getString(ORIGINALURL); |
| 1692 | |
| 1693 | final WebBackForwardList list = mMainView.restoreState(b); |
| 1694 | if (list == null) { |
| 1695 | return false; |
| 1696 | } |
| 1697 | if (b.containsKey(CURRPICTURE)) { |
| 1698 | final File f = new File(b.getString(CURRPICTURE)); |
| 1699 | mMainView.restorePicture(b, f); |
| 1700 | f.delete(); |
| 1701 | } |
| 1702 | return true; |
| 1703 | } |
| 1704 | } |