blob: a048c2da9a40b19d591624a711e1637b4b40c31d [file] [log] [blame]
Grace Kloba22ac16e2009-10-07 18:00:23 -07001/*
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
17package com.android.browser;
18
Jeff Hamilton8ce956c2010-08-17 11:13:53 -050019import com.android.common.speech.LoggingEvents;
20
Michael Kolb8233fac2010-10-26 16:08:53 -070021import android.app.Activity;
Grace Kloba22ac16e2009-10-07 18:00:23 -070022import android.app.AlertDialog;
Leon Scroggins58d56c62010-01-28 15:12:40 -050023import android.app.SearchManager;
Grace Kloba22ac16e2009-10-07 18:00:23 -070024import android.content.ContentResolver;
Grace Kloba22ac16e2009-10-07 18:00:23 -070025import android.content.DialogInterface;
Michael Kolbfe251992010-07-08 15:41:55 -070026import android.content.DialogInterface.OnCancelListener;
Jeff Hamilton8ce956c2010-08-17 11:13:53 -050027import android.content.Intent;
Grace Kloba22ac16e2009-10-07 18:00:23 -070028import android.graphics.Bitmap;
29import android.net.Uri;
30import android.net.http.SslError;
Grace Kloba22ac16e2009-10-07 18:00:23 -070031import android.os.Bundle;
32import android.os.Message;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +000033import android.os.SystemClock;
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -050034import android.speech.RecognizerResultsIntent;
Grace Kloba22ac16e2009-10-07 18:00:23 -070035import android.util.Log;
36import android.view.KeyEvent;
37import android.view.LayoutInflater;
38import android.view.View;
Grace Kloba50c241e2010-04-20 11:07:50 -070039import android.view.ViewStub;
Ben Murdochc42addf2010-01-28 15:19:59 +000040import android.webkit.ConsoleMessage;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -050041import android.webkit.DownloadListener;
Grace Kloba22ac16e2009-10-07 18:00:23 -070042import android.webkit.GeolocationPermissions;
43import android.webkit.HttpAuthHandler;
44import android.webkit.SslErrorHandler;
45import android.webkit.URLUtil;
46import android.webkit.ValueCallback;
47import android.webkit.WebBackForwardList;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -050048import android.webkit.WebBackForwardListClient;
Grace Kloba22ac16e2009-10-07 18:00:23 -070049import android.webkit.WebChromeClient;
50import android.webkit.WebHistoryItem;
Grace Kloba22ac16e2009-10-07 18:00:23 -070051import android.webkit.WebStorage;
52import android.webkit.WebView;
53import android.webkit.WebViewClient;
54import android.widget.FrameLayout;
Grace Kloba22ac16e2009-10-07 18:00:23 -070055import android.widget.LinearLayout;
56import android.widget.TextView;
Ben Murdoch8029a772010-11-16 11:58:21 +000057import android.widget.Toast;
Grace Kloba22ac16e2009-10-07 18:00:23 -070058
Michael Kolbfe251992010-07-08 15:41:55 -070059import java.util.ArrayList;
60import java.util.HashMap;
61import java.util.Iterator;
62import java.util.LinkedList;
63import java.util.Map;
64import java.util.Vector;
65
Grace Kloba22ac16e2009-10-07 18:00:23 -070066/**
67 * Class for maintaining Tabs with a main WebView and a subwindow.
68 */
69class Tab {
Michael Kolb8233fac2010-10-26 16:08:53 -070070
Grace Kloba22ac16e2009-10-07 18:00:23 -070071 // Log Tag
72 private static final String LOGTAG = "Tab";
Ben Murdochc42addf2010-01-28 15:19:59 +000073 // Special case the logtag for messages for the Console to make it easier to
74 // filter them and match the logtag used for these messages in older versions
75 // of the browser.
76 private static final String CONSOLE_LOGTAG = "browser";
77
Michael Kolb8233fac2010-10-26 16:08:53 -070078 final static int LOCK_ICON_UNSECURE = 0;
79 final static int LOCK_ICON_SECURE = 1;
80 final static int LOCK_ICON_MIXED = 2;
81
82 Activity mActivity;
83 private WebViewController mWebViewController;
84
Grace Kloba22ac16e2009-10-07 18:00:23 -070085 // The Geolocation permissions prompt
86 private GeolocationPermissionsPrompt mGeolocationPermissionsPrompt;
87 // Main WebView wrapper
Leon Scroggins III211ba542010-04-19 13:21:13 -040088 private LinearLayout mContainer;
Grace Kloba22ac16e2009-10-07 18:00:23 -070089 // Main WebView
90 private WebView mMainView;
91 // Subwindow container
92 private View mSubViewContainer;
93 // Subwindow WebView
94 private WebView mSubView;
95 // Saved bundle for when we are running low on memory. It contains the
96 // information needed to restore the WebView if the user goes back to the
97 // tab.
98 private Bundle mSavedState;
99 // Data used when displaying the tab in the picker.
100 private PickerData mPickerData;
101 // Parent Tab. This is the Tab that created this Tab, or null if the Tab was
102 // created by the UI
103 private Tab mParentTab;
104 // Tab that constructed by this Tab. This is used when this Tab is
105 // destroyed, it clears all mParentTab values in the children.
106 private Vector<Tab> mChildTabs;
107 // If true, the tab will be removed when back out of the first page.
108 private boolean mCloseOnExit;
109 // If true, the tab is in the foreground of the current activity.
110 private boolean mInForeground;
Michael Kolb8233fac2010-10-26 16:08:53 -0700111 // If true, the tab is in page loading state (after onPageStarted,
112 // before onPageFinsihed)
113 private boolean mInPageLoad;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000114 // The time the load started, used to find load page time
115 private long mLoadStartTime;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700116 // Application identifier used to find tabs that another application wants
117 // to reuse.
118 private String mAppId;
119 // Keep the original url around to avoid killing the old WebView if the url
120 // has not changed.
121 private String mOriginalUrl;
Michael Kolb8233fac2010-10-26 16:08:53 -0700122 // Hold on to the currently loaded url
123 private String mCurrentUrl;
124 //The currently loaded title
125 private String mCurrentTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700126 // Error console for the tab
127 private ErrorConsoleView mErrorConsole;
128 // the lock icon type and previous lock icon type for the tab
129 private int mLockIconType;
130 private int mPrevLockIconType;
131 // Inflation service for making subwindows.
132 private final LayoutInflater mInflateService;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -0500133 // The listener that gets invoked when a download is started from the
134 // mMainView
135 private final DownloadListener mDownloadListener;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500136 // Listener used to know when we move forward or back in the history list.
137 private final WebBackForwardListClient mWebBackForwardListClient;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700138
139 // AsyncTask for downloading touch icons
140 DownloadTouchIcon mTouchIconLoader;
141
142 // Extra saved information for displaying the tab in the picker.
143 private static class PickerData {
144 String mUrl;
145 String mTitle;
146 Bitmap mFavicon;
147 }
148
149 // Used for saving and restoring each Tab
150 static final String WEBVIEW = "webview";
151 static final String NUMTABS = "numTabs";
152 static final String CURRTAB = "currentTab";
153 static final String CURRURL = "currentUrl";
154 static final String CURRTITLE = "currentTitle";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700155 static final String CLOSEONEXIT = "closeonexit";
156 static final String PARENTTAB = "parentTab";
157 static final String APPID = "appid";
158 static final String ORIGINALURL = "originalUrl";
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700159 static final String INCOGNITO = "privateBrowsingEnabled";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700160
161 // -------------------------------------------------------------------------
162
Leon Scroggins58d56c62010-01-28 15:12:40 -0500163 /**
164 * Private information regarding the latest voice search. If the Tab is not
165 * in voice search mode, this will be null.
166 */
167 private VoiceSearchData mVoiceSearchData;
168 /**
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400169 * Remove voice search mode from this tab.
170 */
171 public void revertVoiceSearchMode() {
172 if (mVoiceSearchData != null) {
173 mVoiceSearchData = null;
174 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700175 mWebViewController.revertVoiceSearchMode(this);
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400176 }
177 }
178 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700179
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400180 /**
Leon Scroggins58d56c62010-01-28 15:12:40 -0500181 * Return whether the tab is in voice search mode.
182 */
183 public boolean isInVoiceSearchMode() {
184 return mVoiceSearchData != null;
185 }
186 /**
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400187 * Return true if the Tab is in voice search mode and the voice search
188 * Intent came with a String identifying that Google provided the Intent.
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500189 */
190 public boolean voiceSearchSourceIsGoogle() {
191 return mVoiceSearchData != null && mVoiceSearchData.mSourceIsGoogle;
192 }
193 /**
Leon Scroggins58d56c62010-01-28 15:12:40 -0500194 * Get the title to display for the current voice search page. If the Tab
195 * is not in voice search mode, return null.
196 */
197 public String getVoiceDisplayTitle() {
198 if (mVoiceSearchData == null) return null;
199 return mVoiceSearchData.mLastVoiceSearchTitle;
200 }
201 /**
202 * Get the latest array of voice search results, to be passed to the
203 * BrowserProvider. If the Tab is not in voice search mode, return null.
204 */
205 public ArrayList<String> getVoiceSearchResults() {
206 if (mVoiceSearchData == null) return null;
207 return mVoiceSearchData.mVoiceSearchResults;
208 }
209 /**
210 * Activate voice search mode.
211 * @param intent Intent which has the results to use, or an index into the
212 * results when reusing the old results.
213 */
214 /* package */ void activateVoiceSearchMode(Intent intent) {
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500215 int index = 0;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500216 ArrayList<String> results = intent.getStringArrayListExtra(
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -0500217 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_STRINGS);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500218 if (results != null) {
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500219 ArrayList<String> urls = intent.getStringArrayListExtra(
220 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_URLS);
221 ArrayList<String> htmls = intent.getStringArrayListExtra(
222 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_HTML);
223 ArrayList<String> baseUrls = intent.getStringArrayListExtra(
224 RecognizerResultsIntent
225 .EXTRA_VOICE_SEARCH_RESULT_HTML_BASE_URLS);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500226 // This tab is now entering voice search mode for the first time, or
227 // a new voice search was done.
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500228 int size = results.size();
229 if (urls == null || size != urls.size()) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500230 throw new AssertionError("improper extras passed in Intent");
231 }
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500232 if (htmls == null || htmls.size() != size || baseUrls == null ||
233 (baseUrls.size() != size && baseUrls.size() != 1)) {
234 // If either of these arrays are empty/incorrectly sized, ignore
235 // them.
236 htmls = null;
237 baseUrls = null;
238 }
239 mVoiceSearchData = new VoiceSearchData(results, urls, htmls,
240 baseUrls);
Leon Scroggins9df94972010-03-08 18:20:35 -0500241 mVoiceSearchData.mHeaders = intent.getParcelableArrayListExtra(
242 RecognizerResultsIntent
243 .EXTRA_VOICE_SEARCH_RESULT_HTTP_HEADERS);
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500244 mVoiceSearchData.mSourceIsGoogle = intent.getBooleanExtra(
245 VoiceSearchData.SOURCE_IS_GOOGLE, false);
Leon Scroggins2ee4a5a2010-03-15 16:56:57 -0400246 mVoiceSearchData.mVoiceSearchIntent = new Intent(intent);
Leon Scrogginse10dde52010-03-08 19:53:03 -0500247 }
248 String extraData = intent.getStringExtra(
249 SearchManager.EXTRA_DATA_KEY);
250 if (extraData != null) {
251 index = Integer.parseInt(extraData);
252 if (index >= mVoiceSearchData.mVoiceSearchResults.size()) {
253 throw new AssertionError("index must be less than "
254 + "size of mVoiceSearchResults");
255 }
256 if (mVoiceSearchData.mSourceIsGoogle) {
257 Intent logIntent = new Intent(
258 LoggingEvents.ACTION_LOG_EVENT);
259 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
260 LoggingEvents.VoiceSearch.N_BEST_CHOOSE);
261 logIntent.putExtra(
262 LoggingEvents.VoiceSearch.EXTRA_N_BEST_CHOOSE_INDEX,
263 index);
264 mActivity.sendBroadcast(logIntent);
265 }
266 if (mVoiceSearchData.mVoiceSearchIntent != null) {
Leon Scroggins2ee4a5a2010-03-15 16:56:57 -0400267 // Copy the Intent, so that each history item will have its own
268 // Intent, with different (or none) extra data.
269 Intent latest = new Intent(mVoiceSearchData.mVoiceSearchIntent);
270 latest.putExtra(SearchManager.EXTRA_DATA_KEY, extraData);
271 mVoiceSearchData.mVoiceSearchIntent = latest;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500272 }
273 }
274 mVoiceSearchData.mLastVoiceSearchTitle
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500275 = mVoiceSearchData.mVoiceSearchResults.get(index);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500276 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700277 mWebViewController.activateVoiceSearchMode(mVoiceSearchData.mLastVoiceSearchTitle);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500278 }
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500279 if (mVoiceSearchData.mVoiceSearchHtmls != null) {
280 // When index was found it was already ensured that it was valid
281 String uriString = mVoiceSearchData.mVoiceSearchHtmls.get(index);
282 if (uriString != null) {
283 Uri dataUri = Uri.parse(uriString);
284 if (RecognizerResultsIntent.URI_SCHEME_INLINE.equals(
285 dataUri.getScheme())) {
286 // If there is only one base URL, use it. If there are
287 // more, there will be one for each index, so use the base
288 // URL corresponding to the index.
289 String baseUrl = mVoiceSearchData.mVoiceSearchBaseUrls.get(
290 mVoiceSearchData.mVoiceSearchBaseUrls.size() > 1 ?
291 index : 0);
292 mVoiceSearchData.mLastVoiceSearchUrl = baseUrl;
293 mMainView.loadDataWithBaseURL(baseUrl,
294 uriString.substring(RecognizerResultsIntent
295 .URI_SCHEME_INLINE.length() + 1), "text/html",
296 "utf-8", baseUrl);
297 return;
298 }
299 }
300 }
Leon Scroggins58d56c62010-01-28 15:12:40 -0500301 mVoiceSearchData.mLastVoiceSearchUrl
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500302 = mVoiceSearchData.mVoiceSearchUrls.get(index);
303 if (null == mVoiceSearchData.mLastVoiceSearchUrl) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700304 mVoiceSearchData.mLastVoiceSearchUrl = UrlUtils.smartUrlFilter(
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500305 mVoiceSearchData.mLastVoiceSearchTitle);
306 }
Leon Scroggins9df94972010-03-08 18:20:35 -0500307 Map<String, String> headers = null;
308 if (mVoiceSearchData.mHeaders != null) {
309 int bundleIndex = mVoiceSearchData.mHeaders.size() == 1 ? 0
310 : index;
311 Bundle bundle = mVoiceSearchData.mHeaders.get(bundleIndex);
312 if (bundle != null && !bundle.isEmpty()) {
313 Iterator<String> iter = bundle.keySet().iterator();
314 headers = new HashMap<String, String>();
315 while (iter.hasNext()) {
316 String key = iter.next();
317 headers.put(key, bundle.getString(key));
318 }
319 }
320 }
321 mMainView.loadUrl(mVoiceSearchData.mLastVoiceSearchUrl, headers);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500322 }
323 /* package */ static class VoiceSearchData {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500324 public VoiceSearchData(ArrayList<String> results,
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500325 ArrayList<String> urls, ArrayList<String> htmls,
326 ArrayList<String> baseUrls) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500327 mVoiceSearchResults = results;
328 mVoiceSearchUrls = urls;
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500329 mVoiceSearchHtmls = htmls;
330 mVoiceSearchBaseUrls = baseUrls;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500331 }
332 /*
333 * ArrayList of suggestions to be displayed when opening the
334 * SearchManager
335 */
336 public ArrayList<String> mVoiceSearchResults;
337 /*
338 * ArrayList of urls, associated with the suggestions in
339 * mVoiceSearchResults.
340 */
341 public ArrayList<String> mVoiceSearchUrls;
342 /*
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500343 * ArrayList holding content to load for each item in
344 * mVoiceSearchResults.
345 */
346 public ArrayList<String> mVoiceSearchHtmls;
347 /*
348 * ArrayList holding base urls for the items in mVoiceSearchResults.
349 * If non null, this will either have the same size as
350 * mVoiceSearchResults or have a size of 1, in which case all will use
351 * the same base url
352 */
353 public ArrayList<String> mVoiceSearchBaseUrls;
354 /*
Leon Scroggins58d56c62010-01-28 15:12:40 -0500355 * The last url provided by voice search. Used for comparison to see if
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500356 * we are going to a page by some method besides voice search.
Leon Scroggins58d56c62010-01-28 15:12:40 -0500357 */
358 public String mLastVoiceSearchUrl;
359 /**
360 * The last title used for voice search. Needed to update the title bar
361 * when switching tabs.
362 */
363 public String mLastVoiceSearchTitle;
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500364 /**
365 * Whether the Intent which turned on voice search mode contained the
366 * String signifying that Google was the source.
367 */
368 public boolean mSourceIsGoogle;
369 /**
Leon Scroggins9df94972010-03-08 18:20:35 -0500370 * List of headers to be passed into the WebView containing location
371 * information
372 */
373 public ArrayList<Bundle> mHeaders;
374 /**
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500375 * The Intent used to invoke voice search. Placed on the
376 * WebHistoryItem so that when coming back to a previous voice search
377 * page we can again activate voice search.
378 */
Leon Scrogginse10dde52010-03-08 19:53:03 -0500379 public Intent mVoiceSearchIntent;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500380 /**
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500381 * String used to identify Google as the source of voice search.
382 */
383 public static String SOURCE_IS_GOOGLE
384 = "android.speech.extras.SOURCE_IS_GOOGLE";
Leon Scroggins58d56c62010-01-28 15:12:40 -0500385 }
386
Grace Kloba22ac16e2009-10-07 18:00:23 -0700387 // Container class for the next error dialog that needs to be displayed
388 private class ErrorDialog {
389 public final int mTitle;
390 public final String mDescription;
391 public final int mError;
392 ErrorDialog(int title, String desc, int error) {
393 mTitle = title;
394 mDescription = desc;
395 mError = error;
396 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700397 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700398
399 private void processNextError() {
400 if (mQueuedErrors == null) {
401 return;
402 }
403 // The first one is currently displayed so just remove it.
404 mQueuedErrors.removeFirst();
405 if (mQueuedErrors.size() == 0) {
406 mQueuedErrors = null;
407 return;
408 }
409 showError(mQueuedErrors.getFirst());
410 }
411
412 private DialogInterface.OnDismissListener mDialogListener =
413 new DialogInterface.OnDismissListener() {
414 public void onDismiss(DialogInterface d) {
415 processNextError();
416 }
417 };
418 private LinkedList<ErrorDialog> mQueuedErrors;
419
420 private void queueError(int err, String desc) {
421 if (mQueuedErrors == null) {
422 mQueuedErrors = new LinkedList<ErrorDialog>();
423 }
424 for (ErrorDialog d : mQueuedErrors) {
425 if (d.mError == err) {
426 // Already saw a similar error, ignore the new one.
427 return;
428 }
429 }
430 ErrorDialog errDialog = new ErrorDialog(
431 err == WebViewClient.ERROR_FILE_NOT_FOUND ?
432 R.string.browserFrameFileErrorLabel :
433 R.string.browserFrameNetworkErrorLabel,
434 desc, err);
435 mQueuedErrors.addLast(errDialog);
436
437 // Show the dialog now if the queue was empty and it is in foreground
438 if (mQueuedErrors.size() == 1 && mInForeground) {
439 showError(errDialog);
440 }
441 }
442
443 private void showError(ErrorDialog errDialog) {
444 if (mInForeground) {
445 AlertDialog d = new AlertDialog.Builder(mActivity)
446 .setTitle(errDialog.mTitle)
447 .setMessage(errDialog.mDescription)
448 .setPositiveButton(R.string.ok, null)
449 .create();
450 d.setOnDismissListener(mDialogListener);
451 d.show();
452 }
453 }
454
455 // -------------------------------------------------------------------------
456 // WebViewClient implementation for the main WebView
457 // -------------------------------------------------------------------------
458
459 private final WebViewClient mWebViewClient = new WebViewClient() {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500460 private Message mDontResend;
461 private Message mResend;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700462 @Override
463 public void onPageStarted(WebView view, String url, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700464 mInPageLoad = true;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000465 mLoadStartTime = SystemClock.uptimeMillis();
Leon Scroggins58d56c62010-01-28 15:12:40 -0500466 if (mVoiceSearchData != null
467 && !url.equals(mVoiceSearchData.mLastVoiceSearchUrl)) {
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500468 if (mVoiceSearchData.mSourceIsGoogle) {
469 Intent i = new Intent(LoggingEvents.ACTION_LOG_EVENT);
470 i.putExtra(LoggingEvents.EXTRA_FLUSH, true);
471 mActivity.sendBroadcast(i);
472 }
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400473 revertVoiceSearchMode();
Leon Scroggins58d56c62010-01-28 15:12:40 -0500474 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700475
Grace Kloba22ac16e2009-10-07 18:00:23 -0700476
477 // If we start a touch icon load and then load a new page, we don't
478 // want to cancel the current touch icon loader. But, we do want to
479 // create a new one when the touch icon url is known.
480 if (mTouchIconLoader != null) {
481 mTouchIconLoader.mTab = null;
482 mTouchIconLoader = null;
483 }
484
485 // reset the error console
486 if (mErrorConsole != null) {
487 mErrorConsole.clearErrorMessages();
Michael Kolb8233fac2010-10-26 16:08:53 -0700488 if (mWebViewController.shouldShowErrorConsole()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700489 mErrorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
490 }
491 }
492
Grace Kloba22ac16e2009-10-07 18:00:23 -0700493
494 // finally update the UI in the activity if it is in the foreground
Michael Kolb8233fac2010-10-26 16:08:53 -0700495 mWebViewController.onPageStarted(Tab.this, view, url, favicon);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700496 }
497
498 @Override
499 public void onPageFinished(WebView view, String url) {
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000500 LogTag.logPageFinishedLoading(
501 url, SystemClock.uptimeMillis() - mLoadStartTime);
Michael Kolb8233fac2010-10-26 16:08:53 -0700502 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700503
Michael Kolb8233fac2010-10-26 16:08:53 -0700504 mWebViewController.onPageFinished(Tab.this, url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700505 }
506
507 // return true if want to hijack the url to let another app to handle it
508 @Override
509 public boolean shouldOverrideUrlLoading(WebView view, String url) {
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400510 if (voiceSearchSourceIsGoogle()) {
511 // This method is called when the user clicks on a link.
512 // VoiceSearchMode is turned off when the user leaves the
513 // Google results page, so at this point the user must be on
514 // that page. If the user clicked a link on that page, assume
515 // that the voice search was effective, and broadcast an Intent
516 // so a receiver can take note of that fact.
517 Intent logIntent = new Intent(LoggingEvents.ACTION_LOG_EVENT);
518 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
519 LoggingEvents.VoiceSearch.RESULT_CLICKED);
520 mActivity.sendBroadcast(logIntent);
521 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700522 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700523 return mWebViewController.shouldOverrideUrlLoading(view, url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700524 } else {
525 return false;
526 }
527 }
528
529 /**
530 * Updates the lock icon. This method is called when we discover another
531 * resource to be loaded for this page (for example, javascript). While
532 * we update the icon type, we do not update the lock icon itself until
533 * we are done loading, it is slightly more secure this way.
534 */
535 @Override
536 public void onLoadResource(WebView view, String url) {
537 if (url != null && url.length() > 0) {
538 // It is only if the page claims to be secure that we may have
539 // to update the lock:
Michael Kolb8233fac2010-10-26 16:08:53 -0700540 if (mLockIconType == LOCK_ICON_SECURE) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700541 // If NOT a 'safe' url, change the lock to mixed content!
542 if (!(URLUtil.isHttpsUrl(url) || URLUtil.isDataUrl(url)
543 || URLUtil.isAboutUrl(url))) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700544 mLockIconType = LOCK_ICON_MIXED;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700545 }
546 }
547 }
548 }
549
550 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700551 * Show a dialog informing the user of the network error reported by
552 * WebCore if it is in the foreground.
553 */
554 @Override
555 public void onReceivedError(WebView view, int errorCode,
556 String description, String failingUrl) {
557 if (errorCode != WebViewClient.ERROR_HOST_LOOKUP &&
558 errorCode != WebViewClient.ERROR_CONNECT &&
559 errorCode != WebViewClient.ERROR_BAD_URL &&
560 errorCode != WebViewClient.ERROR_UNSUPPORTED_SCHEME &&
561 errorCode != WebViewClient.ERROR_FILE) {
562 queueError(errorCode, description);
563 }
Jeff Hamilton47654f42010-09-07 09:57:51 -0500564
565 // Don't log URLs when in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -0700566 if (!isPrivateBrowsingEnabled()) {
Jeff Hamilton47654f42010-09-07 09:57:51 -0500567 Log.e(LOGTAG, "onReceivedError " + errorCode + " " + failingUrl
568 + " " + description);
569 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700570
571 // We need to reset the title after an error if it is in foreground.
572 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700573 mWebViewController.resetTitleAndRevertLockIcon(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700574 }
575 }
576
577 /**
578 * Check with the user if it is ok to resend POST data as the page they
579 * are trying to navigate to is the result of a POST.
580 */
581 @Override
582 public void onFormResubmission(WebView view, final Message dontResend,
583 final Message resend) {
584 if (!mInForeground) {
585 dontResend.sendToTarget();
586 return;
587 }
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500588 if (mDontResend != null) {
589 Log.w(LOGTAG, "onFormResubmission should not be called again "
590 + "while dialog is still up");
591 dontResend.sendToTarget();
592 return;
593 }
594 mDontResend = dontResend;
595 mResend = resend;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700596 new AlertDialog.Builder(mActivity).setTitle(
597 R.string.browserFrameFormResubmitLabel).setMessage(
598 R.string.browserFrameFormResubmitMessage)
599 .setPositiveButton(R.string.ok,
600 new DialogInterface.OnClickListener() {
601 public void onClick(DialogInterface dialog,
602 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500603 if (mResend != null) {
604 mResend.sendToTarget();
605 mResend = null;
606 mDontResend = null;
607 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700608 }
609 }).setNegativeButton(R.string.cancel,
610 new DialogInterface.OnClickListener() {
611 public void onClick(DialogInterface dialog,
612 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500613 if (mDontResend != null) {
614 mDontResend.sendToTarget();
615 mResend = null;
616 mDontResend = null;
617 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700618 }
619 }).setOnCancelListener(new OnCancelListener() {
620 public void onCancel(DialogInterface dialog) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500621 if (mDontResend != null) {
622 mDontResend.sendToTarget();
623 mResend = null;
624 mDontResend = null;
625 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700626 }
627 }).show();
628 }
629
630 /**
631 * Insert the url into the visited history database.
632 * @param url The url to be inserted.
633 * @param isReload True if this url is being reloaded.
634 * FIXME: Not sure what to do when reloading the page.
635 */
636 @Override
637 public void doUpdateVisitedHistory(WebView view, String url,
638 boolean isReload) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700639 mWebViewController.doUpdateVisitedHistory(Tab.this, url, isReload);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700640 }
641
642 /**
643 * Displays SSL error(s) dialog to the user.
644 */
645 @Override
646 public void onReceivedSslError(final WebView view,
647 final SslErrorHandler handler, final SslError error) {
648 if (!mInForeground) {
649 handler.cancel();
650 return;
651 }
652 if (BrowserSettings.getInstance().showSecurityWarnings()) {
653 final LayoutInflater factory =
654 LayoutInflater.from(mActivity);
655 final View warningsView =
656 factory.inflate(R.layout.ssl_warnings, null);
657 final LinearLayout placeholder =
658 (LinearLayout)warningsView.findViewById(R.id.placeholder);
659
660 if (error.hasError(SslError.SSL_UNTRUSTED)) {
661 LinearLayout ll = (LinearLayout)factory
662 .inflate(R.layout.ssl_warning, null);
663 ((TextView)ll.findViewById(R.id.warning))
664 .setText(R.string.ssl_untrusted);
665 placeholder.addView(ll);
666 }
667
668 if (error.hasError(SslError.SSL_IDMISMATCH)) {
669 LinearLayout ll = (LinearLayout)factory
670 .inflate(R.layout.ssl_warning, null);
671 ((TextView)ll.findViewById(R.id.warning))
672 .setText(R.string.ssl_mismatch);
673 placeholder.addView(ll);
674 }
675
676 if (error.hasError(SslError.SSL_EXPIRED)) {
677 LinearLayout ll = (LinearLayout)factory
678 .inflate(R.layout.ssl_warning, null);
679 ((TextView)ll.findViewById(R.id.warning))
680 .setText(R.string.ssl_expired);
681 placeholder.addView(ll);
682 }
683
684 if (error.hasError(SslError.SSL_NOTYETVALID)) {
685 LinearLayout ll = (LinearLayout)factory
686 .inflate(R.layout.ssl_warning, null);
687 ((TextView)ll.findViewById(R.id.warning))
688 .setText(R.string.ssl_not_yet_valid);
689 placeholder.addView(ll);
690 }
691
692 new AlertDialog.Builder(mActivity).setTitle(
693 R.string.security_warning).setIcon(
694 android.R.drawable.ic_dialog_alert).setView(
695 warningsView).setPositiveButton(R.string.ssl_continue,
696 new DialogInterface.OnClickListener() {
697 public void onClick(DialogInterface dialog,
698 int whichButton) {
699 handler.proceed();
700 }
701 }).setNeutralButton(R.string.view_certificate,
702 new DialogInterface.OnClickListener() {
703 public void onClick(DialogInterface dialog,
704 int whichButton) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700705 mWebViewController.showSslCertificateOnError(view,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700706 handler, error);
707 }
Ben Murdocha49b8292010-11-16 11:56:04 +0000708 }).setNegativeButton(R.string.ssl_go_back,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700709 new DialogInterface.OnClickListener() {
710 public void onClick(DialogInterface dialog,
711 int whichButton) {
712 handler.cancel();
Michael Kolb8233fac2010-10-26 16:08:53 -0700713 mWebViewController.resetTitleAndRevertLockIcon(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700714 }
715 }).setOnCancelListener(
716 new DialogInterface.OnCancelListener() {
717 public void onCancel(DialogInterface dialog) {
718 handler.cancel();
Michael Kolb8233fac2010-10-26 16:08:53 -0700719 mWebViewController.resetTitleAndRevertLockIcon(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700720 }
721 }).show();
722 } else {
723 handler.proceed();
724 }
725 }
726
727 /**
728 * Handles an HTTP authentication request.
729 *
730 * @param handler The authentication handler
731 * @param host The host
732 * @param realm The realm
733 */
734 @Override
735 public void onReceivedHttpAuthRequest(WebView view,
736 final HttpAuthHandler handler, final String host,
737 final String realm) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700738 mWebViewController.onReceivedHttpAuthRequest(Tab.this, view, handler, host, realm);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700739 }
740
741 @Override
742 public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
743 if (!mInForeground) {
744 return false;
745 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700746 return mWebViewController.shouldOverrideKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700747 }
748
749 @Override
750 public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700751 if (!mInForeground) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700752 return;
753 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700754 mWebViewController.onUnhandledKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700755 }
756 };
757
758 // -------------------------------------------------------------------------
759 // WebChromeClient implementation for the main WebView
760 // -------------------------------------------------------------------------
761
762 private final WebChromeClient mWebChromeClient = new WebChromeClient() {
763 // Helper method to create a new tab or sub window.
764 private void createWindow(final boolean dialog, final Message msg) {
765 WebView.WebViewTransport transport =
766 (WebView.WebViewTransport) msg.obj;
767 if (dialog) {
768 createSubWindow();
Michael Kolb8233fac2010-10-26 16:08:53 -0700769 mWebViewController.attachSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700770 transport.setWebView(mSubView);
771 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -0700772 final Tab newTab = mWebViewController.openTabAndShow(
773 IntentHandler.EMPTY_URL_DATA, false, null);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700774 if (newTab != Tab.this) {
775 Tab.this.addChildTab(newTab);
776 }
777 transport.setWebView(newTab.getWebView());
778 }
779 msg.sendToTarget();
780 }
781
782 @Override
783 public boolean onCreateWindow(WebView view, final boolean dialog,
784 final boolean userGesture, final Message resultMsg) {
785 // only allow new window or sub window for the foreground case
786 if (!mInForeground) {
787 return false;
788 }
789 // Short-circuit if we can't create any more tabs or sub windows.
790 if (dialog && mSubView != null) {
791 new AlertDialog.Builder(mActivity)
792 .setTitle(R.string.too_many_subwindows_dialog_title)
793 .setIcon(android.R.drawable.ic_dialog_alert)
794 .setMessage(R.string.too_many_subwindows_dialog_message)
795 .setPositiveButton(R.string.ok, null)
796 .show();
797 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700798 } else if (!mWebViewController.getTabControl().canCreateNewTab()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700799 new AlertDialog.Builder(mActivity)
800 .setTitle(R.string.too_many_windows_dialog_title)
801 .setIcon(android.R.drawable.ic_dialog_alert)
802 .setMessage(R.string.too_many_windows_dialog_message)
803 .setPositiveButton(R.string.ok, null)
804 .show();
805 return false;
806 }
807
808 // Short-circuit if this was a user gesture.
809 if (userGesture) {
810 createWindow(dialog, resultMsg);
811 return true;
812 }
813
814 // Allow the popup and create the appropriate window.
815 final AlertDialog.OnClickListener allowListener =
816 new AlertDialog.OnClickListener() {
817 public void onClick(DialogInterface d,
818 int which) {
819 createWindow(dialog, resultMsg);
820 }
821 };
822
823 // Block the popup by returning a null WebView.
824 final AlertDialog.OnClickListener blockListener =
825 new AlertDialog.OnClickListener() {
826 public void onClick(DialogInterface d, int which) {
827 resultMsg.sendToTarget();
828 }
829 };
830
831 // Build a confirmation dialog to display to the user.
832 final AlertDialog d =
833 new AlertDialog.Builder(mActivity)
834 .setTitle(R.string.attention)
835 .setIcon(android.R.drawable.ic_dialog_alert)
836 .setMessage(R.string.popup_window_attempt)
837 .setPositiveButton(R.string.allow, allowListener)
838 .setNegativeButton(R.string.block, blockListener)
839 .setCancelable(false)
840 .create();
841
842 // Show the confirmation dialog.
843 d.show();
844 return true;
845 }
846
847 @Override
Patrick Scotteb5061b2009-11-18 15:00:30 -0500848 public void onRequestFocus(WebView view) {
849 if (!mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700850 mWebViewController.switchToTab(mWebViewController.getTabControl().getTabIndex(
Patrick Scotteb5061b2009-11-18 15:00:30 -0500851 Tab.this));
852 }
853 }
854
855 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700856 public void onCloseWindow(WebView window) {
857 if (mParentTab != null) {
858 // JavaScript can only close popup window.
859 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700860 mWebViewController.switchToTab(mWebViewController.getTabControl()
Grace Kloba22ac16e2009-10-07 18:00:23 -0700861 .getTabIndex(mParentTab));
862 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700863 mWebViewController.closeTab(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700864 }
865 }
866
867 @Override
868 public void onProgressChanged(WebView view, int newProgress) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700869 mWebViewController.onProgressChanged(Tab.this, newProgress);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700870 }
871
872 @Override
Leon Scroggins21d9b902010-03-11 09:33:11 -0500873 public void onReceivedTitle(WebView view, final String title) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700874 mWebViewController.onReceivedTitle(Tab.this, title);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700875 }
876
877 @Override
878 public void onReceivedIcon(WebView view, Bitmap icon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700879 mWebViewController.onFavicon(Tab.this, view, icon);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700880 }
881
882 @Override
883 public void onReceivedTouchIconUrl(WebView view, String url,
884 boolean precomposed) {
885 final ContentResolver cr = mActivity.getContentResolver();
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400886 // Let precomposed icons take precedence over non-composed
887 // icons.
888 if (precomposed && mTouchIconLoader != null) {
889 mTouchIconLoader.cancel(false);
890 mTouchIconLoader = null;
891 }
892 // Have only one async task at a time.
893 if (mTouchIconLoader == null) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700894 mTouchIconLoader = new DownloadTouchIcon(Tab.this,
895 mActivity, cr, view);
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400896 mTouchIconLoader.execute(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700897 }
898 }
899
900 @Override
901 public void onShowCustomView(View view,
902 WebChromeClient.CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700903 if (mInForeground) mWebViewController.showCustomView(Tab.this, view,
904 callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700905 }
906
907 @Override
908 public void onHideCustomView() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700909 if (mInForeground) mWebViewController.hideCustomView();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700910 }
911
912 /**
913 * The origin has exceeded its database quota.
914 * @param url the URL that exceeded the quota
915 * @param databaseIdentifier the identifier of the database on which the
916 * transaction that caused the quota overflow was run
917 * @param currentQuota the current quota for the origin.
918 * @param estimatedSize the estimated size of the database.
919 * @param totalUsedQuota is the sum of all origins' quota.
920 * @param quotaUpdater The callback to run when a decision to allow or
921 * deny quota has been made. Don't forget to call this!
922 */
923 @Override
924 public void onExceededDatabaseQuota(String url,
925 String databaseIdentifier, long currentQuota, long estimatedSize,
926 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
927 BrowserSettings.getInstance().getWebStorageSizeManager()
928 .onExceededDatabaseQuota(url, databaseIdentifier,
929 currentQuota, estimatedSize, totalUsedQuota,
930 quotaUpdater);
931 }
932
933 /**
934 * The Application Cache has exceeded its max size.
935 * @param spaceNeeded is the amount of disk space that would be needed
936 * in order for the last appcache operation to succeed.
937 * @param totalUsedQuota is the sum of all origins' quota.
938 * @param quotaUpdater A callback to inform the WebCore thread that a
939 * new app cache size is available. This callback must always
940 * be executed at some point to ensure that the sleeping
941 * WebCore thread is woken up.
942 */
943 @Override
944 public void onReachedMaxAppCacheSize(long spaceNeeded,
945 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
946 BrowserSettings.getInstance().getWebStorageSizeManager()
947 .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota,
948 quotaUpdater);
949 }
950
951 /**
952 * Instructs the browser to show a prompt to ask the user to set the
953 * Geolocation permission state for the specified origin.
954 * @param origin The origin for which Geolocation permissions are
955 * requested.
956 * @param callback The callback to call once the user has set the
957 * Geolocation permission state.
958 */
959 @Override
960 public void onGeolocationPermissionsShowPrompt(String origin,
961 GeolocationPermissions.Callback callback) {
962 if (mInForeground) {
Grace Kloba50c241e2010-04-20 11:07:50 -0700963 getGeolocationPermissionsPrompt().show(origin, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700964 }
965 }
966
967 /**
968 * Instructs the browser to hide the Geolocation permissions prompt.
969 */
970 @Override
971 public void onGeolocationPermissionsHidePrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -0700972 if (mInForeground && mGeolocationPermissionsPrompt != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700973 mGeolocationPermissionsPrompt.hide();
974 }
975 }
976
Ben Murdoch65acc352009-11-19 18:16:04 +0000977 /* Adds a JavaScript error message to the system log and if the JS
978 * console is enabled in the about:debug options, to that console
979 * also.
Ben Murdochc42addf2010-01-28 15:19:59 +0000980 * @param consoleMessage the message object.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700981 */
982 @Override
Ben Murdochc42addf2010-01-28 15:19:59 +0000983 public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700984 if (mInForeground) {
985 // call getErrorConsole(true) so it will create one if needed
986 ErrorConsoleView errorConsole = getErrorConsole(true);
Ben Murdochc42addf2010-01-28 15:19:59 +0000987 errorConsole.addErrorMessage(consoleMessage);
Michael Kolb8233fac2010-10-26 16:08:53 -0700988 if (mWebViewController.shouldShowErrorConsole()
989 && errorConsole.getShowState() !=
990 ErrorConsoleView.SHOW_MAXIMIZED) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700991 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
992 }
993 }
Ben Murdochc42addf2010-01-28 15:19:59 +0000994
Jeff Hamilton47654f42010-09-07 09:57:51 -0500995 // Don't log console messages in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -0700996 if (isPrivateBrowsingEnabled()) return true;
Jeff Hamilton47654f42010-09-07 09:57:51 -0500997
Ben Murdochc42addf2010-01-28 15:19:59 +0000998 String message = "Console: " + consoleMessage.message() + " "
999 + consoleMessage.sourceId() + ":"
1000 + consoleMessage.lineNumber();
1001
1002 switch (consoleMessage.messageLevel()) {
1003 case TIP:
1004 Log.v(CONSOLE_LOGTAG, message);
1005 break;
1006 case LOG:
1007 Log.i(CONSOLE_LOGTAG, message);
1008 break;
1009 case WARNING:
1010 Log.w(CONSOLE_LOGTAG, message);
1011 break;
1012 case ERROR:
1013 Log.e(CONSOLE_LOGTAG, message);
1014 break;
1015 case DEBUG:
1016 Log.d(CONSOLE_LOGTAG, message);
1017 break;
1018 }
1019
1020 return true;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001021 }
1022
1023 /**
1024 * Ask the browser for an icon to represent a <video> element.
1025 * This icon will be used if the Web page did not specify a poster attribute.
1026 * @return Bitmap The icon or null if no such icon is available.
1027 */
1028 @Override
1029 public Bitmap getDefaultVideoPoster() {
1030 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001031 return mWebViewController.getDefaultVideoPoster();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001032 }
1033 return null;
1034 }
1035
1036 /**
1037 * Ask the host application for a custom progress view to show while
1038 * a <video> is loading.
1039 * @return View The progress view.
1040 */
1041 @Override
1042 public View getVideoLoadingProgressView() {
1043 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001044 return mWebViewController.getVideoLoadingProgressView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001045 }
1046 return null;
1047 }
1048
1049 @Override
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01001050 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001051 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001052 mWebViewController.openFileChooser(uploadMsg, acceptType);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001053 } else {
1054 uploadMsg.onReceiveValue(null);
1055 }
1056 }
1057
1058 /**
1059 * Deliver a list of already-visited URLs
1060 */
1061 @Override
1062 public void getVisitedHistory(final ValueCallback<String[]> callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001063 mWebViewController.getVisitedHistory(callback);
1064 }
Ben Murdoch8029a772010-11-16 11:58:21 +00001065
1066 @Override
1067 public void setupAutoFill(Message message) {
1068 // Prompt the user to set up their profile.
1069 final Message msg = message;
1070 AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
1071 builder.setMessage(R.string.autofill_setup_dialog_message)
1072 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1073 @Override
1074 public void onClick(DialogInterface dialog, int id) {
1075 // Take user to the AutoFill profile editor. When they return,
1076 // we will send the message that we pass here which will trigger
1077 // the form to get filled out with their new profile.
1078 mWebViewController.setupAutoFill(msg);
1079 }
1080 })
1081 .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
1082 @Override
1083 public void onClick(DialogInterface dialog, int id) {
1084 // Disable autofill and show a toast with how to turn it on again.
1085 BrowserSettings s = BrowserSettings.getInstance();
1086 s.addObserver(mMainView.getSettings());
1087 s.disableAutoFill(mActivity);
1088 s.update();
1089 Toast.makeText(mActivity, R.string.autofill_setup_dialog_negative_toast,
1090 Toast.LENGTH_LONG).show();
1091 }
1092 }).show();
1093 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001094 };
1095
1096 // -------------------------------------------------------------------------
1097 // WebViewClient implementation for the sub window
1098 // -------------------------------------------------------------------------
1099
1100 // Subclass of WebViewClient used in subwindows to notify the main
1101 // WebViewClient of certain WebView activities.
1102 private static class SubWindowClient extends WebViewClient {
1103 // The main WebViewClient.
1104 private final WebViewClient mClient;
Michael Kolb8233fac2010-10-26 16:08:53 -07001105 private final WebViewController mController;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001106
Michael Kolb8233fac2010-10-26 16:08:53 -07001107 SubWindowClient(WebViewClient client, WebViewController controller) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001108 mClient = client;
Michael Kolb8233fac2010-10-26 16:08:53 -07001109 mController = controller;
Leon Scroggins III211ba542010-04-19 13:21:13 -04001110 }
1111 @Override
1112 public void onPageStarted(WebView view, String url, Bitmap favicon) {
1113 // Unlike the others, do not call mClient's version, which would
1114 // change the progress bar. However, we do want to remove the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001115 // find or select dialog.
Michael Kolb8233fac2010-10-26 16:08:53 -07001116 mController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001117 }
1118 @Override
1119 public void doUpdateVisitedHistory(WebView view, String url,
1120 boolean isReload) {
1121 mClient.doUpdateVisitedHistory(view, url, isReload);
1122 }
1123 @Override
1124 public boolean shouldOverrideUrlLoading(WebView view, String url) {
1125 return mClient.shouldOverrideUrlLoading(view, url);
1126 }
1127 @Override
1128 public void onReceivedSslError(WebView view, SslErrorHandler handler,
1129 SslError error) {
1130 mClient.onReceivedSslError(view, handler, error);
1131 }
1132 @Override
1133 public void onReceivedHttpAuthRequest(WebView view,
1134 HttpAuthHandler handler, String host, String realm) {
1135 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
1136 }
1137 @Override
1138 public void onFormResubmission(WebView view, Message dontResend,
1139 Message resend) {
1140 mClient.onFormResubmission(view, dontResend, resend);
1141 }
1142 @Override
1143 public void onReceivedError(WebView view, int errorCode,
1144 String description, String failingUrl) {
1145 mClient.onReceivedError(view, errorCode, description, failingUrl);
1146 }
1147 @Override
1148 public boolean shouldOverrideKeyEvent(WebView view,
1149 android.view.KeyEvent event) {
1150 return mClient.shouldOverrideKeyEvent(view, event);
1151 }
1152 @Override
1153 public void onUnhandledKeyEvent(WebView view,
1154 android.view.KeyEvent event) {
1155 mClient.onUnhandledKeyEvent(view, event);
1156 }
1157 }
1158
1159 // -------------------------------------------------------------------------
1160 // WebChromeClient implementation for the sub window
1161 // -------------------------------------------------------------------------
1162
1163 private class SubWindowChromeClient extends WebChromeClient {
1164 // The main WebChromeClient.
1165 private final WebChromeClient mClient;
1166
1167 SubWindowChromeClient(WebChromeClient client) {
1168 mClient = client;
1169 }
1170 @Override
1171 public void onProgressChanged(WebView view, int newProgress) {
1172 mClient.onProgressChanged(view, newProgress);
1173 }
1174 @Override
1175 public boolean onCreateWindow(WebView view, boolean dialog,
1176 boolean userGesture, android.os.Message resultMsg) {
1177 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
1178 }
1179 @Override
1180 public void onCloseWindow(WebView window) {
1181 if (window != mSubView) {
1182 Log.e(LOGTAG, "Can't close the window");
1183 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001184 mWebViewController.dismissSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001185 }
1186 }
1187
1188 // -------------------------------------------------------------------------
1189
Michael Kolb8233fac2010-10-26 16:08:53 -07001190 // TODO temporarily use activity here
1191 // remove later
1192
Grace Kloba22ac16e2009-10-07 18:00:23 -07001193 // Construct a new tab
Michael Kolb8233fac2010-10-26 16:08:53 -07001194 Tab(WebViewController wvcontroller, WebView w, boolean closeOnExit, String appId,
Grace Kloba22ac16e2009-10-07 18:00:23 -07001195 String url) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001196 mWebViewController = wvcontroller;
1197 mActivity = mWebViewController.getActivity();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001198 mCloseOnExit = closeOnExit;
1199 mAppId = appId;
1200 mOriginalUrl = url;
Michael Kolb8233fac2010-10-26 16:08:53 -07001201 mLockIconType = LOCK_ICON_UNSECURE;
1202 mPrevLockIconType = LOCK_ICON_UNSECURE;
1203 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001204 mInForeground = false;
1205
Michael Kolb8233fac2010-10-26 16:08:53 -07001206 mInflateService = LayoutInflater.from(mActivity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001207
1208 // The tab consists of a container view, which contains the main
1209 // WebView, as well as any other UI elements associated with the tab.
Leon Scroggins III211ba542010-04-19 13:21:13 -04001210 mContainer = (LinearLayout) mInflateService.inflate(R.layout.tab, null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001211
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001212 mDownloadListener = new DownloadListener() {
1213 public void onDownloadStart(String url, String userAgent,
1214 String contentDisposition, String mimetype,
1215 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001216 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001217 mimetype, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001218 }
1219 };
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001220 mWebBackForwardListClient = new WebBackForwardListClient() {
1221 @Override
1222 public void onNewHistoryItem(WebHistoryItem item) {
1223 if (isInVoiceSearchMode()) {
1224 item.setCustomData(mVoiceSearchData.mVoiceSearchIntent);
1225 }
1226 }
1227 @Override
1228 public void onIndexChanged(WebHistoryItem item, int index) {
1229 Object data = item.getCustomData();
1230 if (data != null && data instanceof Intent) {
1231 activateVoiceSearchMode((Intent) data);
1232 }
1233 }
1234 };
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001235
Grace Kloba22ac16e2009-10-07 18:00:23 -07001236 setWebView(w);
1237 }
1238
1239 /**
1240 * Sets the WebView for this tab, correctly removing the old WebView from
1241 * the container view.
1242 */
1243 void setWebView(WebView w) {
1244 if (mMainView == w) {
1245 return;
1246 }
1247 // If the WebView is changing, the page will be reloaded, so any ongoing
1248 // Geolocation permission requests are void.
Grace Kloba50c241e2010-04-20 11:07:50 -07001249 if (mGeolocationPermissionsPrompt != null) {
1250 mGeolocationPermissionsPrompt.hide();
1251 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001252
1253 // Just remove the old one.
1254 FrameLayout wrapper =
1255 (FrameLayout) mContainer.findViewById(R.id.webview_wrapper);
1256 wrapper.removeView(mMainView);
1257
1258 // set the new one
1259 mMainView = w;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001260 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001261 if (mMainView != null) {
1262 mMainView.setWebViewClient(mWebViewClient);
1263 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001264 // Attach DownloadManager so that downloads can start in an active
1265 // or a non-active window. This can happen when going to a site that
1266 // does a redirect after a period of time. The user could have
1267 // switched to another tab while waiting for the download to start.
1268 mMainView.setDownloadListener(mDownloadListener);
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001269 mMainView.setWebBackForwardListClient(mWebBackForwardListClient);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001270 }
1271 }
1272
1273 /**
1274 * Destroy the tab's main WebView and subWindow if any
1275 */
1276 void destroy() {
1277 if (mMainView != null) {
1278 dismissSubWindow();
1279 BrowserSettings.getInstance().deleteObserver(mMainView.getSettings());
1280 // save the WebView to call destroy() after detach it from the tab
1281 WebView webView = mMainView;
1282 setWebView(null);
1283 webView.destroy();
1284 }
1285 }
1286
1287 /**
1288 * Remove the tab from the parent
1289 */
1290 void removeFromTree() {
1291 // detach the children
1292 if (mChildTabs != null) {
1293 for(Tab t : mChildTabs) {
1294 t.setParentTab(null);
1295 }
1296 }
1297 // remove itself from the parent list
1298 if (mParentTab != null) {
1299 mParentTab.mChildTabs.remove(this);
1300 }
1301 }
1302
1303 /**
1304 * Create a new subwindow unless a subwindow already exists.
1305 * @return True if a new subwindow was created. False if one already exists.
1306 */
1307 boolean createSubWindow() {
1308 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001309 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001310 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001311 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001312 mSubView.setWebChromeClient(new SubWindowChromeClient(
1313 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001314 // Set a different DownloadListener for the mSubView, since it will
1315 // just need to dismiss the mSubView, rather than close the Tab
1316 mSubView.setDownloadListener(new DownloadListener() {
1317 public void onDownloadStart(String url, String userAgent,
1318 String contentDisposition, String mimetype,
1319 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001320 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001321 contentDisposition, mimetype, contentLength);
1322 if (mSubView.copyBackForwardList().getSize() == 0) {
1323 // This subwindow was opened for the sole purpose of
1324 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001325 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001326 }
1327 }
1328 });
Grace Kloba22ac16e2009-10-07 18:00:23 -07001329 mSubView.setOnCreateContextMenuListener(mActivity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001330 return true;
1331 }
1332 return false;
1333 }
1334
1335 /**
1336 * Dismiss the subWindow for the tab.
1337 */
1338 void dismissSubWindow() {
1339 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001340 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001341 BrowserSettings.getInstance().deleteObserver(
1342 mSubView.getSettings());
1343 mSubView.destroy();
1344 mSubView = null;
1345 mSubViewContainer = null;
1346 }
1347 }
1348
Grace Kloba22ac16e2009-10-07 18:00:23 -07001349
1350 /**
1351 * Set the parent tab of this tab.
1352 */
1353 void setParentTab(Tab parent) {
1354 mParentTab = parent;
1355 // This tab may have been freed due to low memory. If that is the case,
1356 // the parent tab index is already saved. If we are changing that index
1357 // (most likely due to removing the parent tab) we must update the
1358 // parent tab index in the saved Bundle.
1359 if (mSavedState != null) {
1360 if (parent == null) {
1361 mSavedState.remove(PARENTTAB);
1362 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -07001363 mSavedState.putInt(PARENTTAB, mWebViewController.getTabControl()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001364 .getTabIndex(parent));
1365 }
1366 }
1367 }
1368
1369 /**
1370 * When a Tab is created through the content of another Tab, then we
1371 * associate the Tabs.
1372 * @param child the Tab that was created from this Tab
1373 */
1374 void addChildTab(Tab child) {
1375 if (mChildTabs == null) {
1376 mChildTabs = new Vector<Tab>();
1377 }
1378 mChildTabs.add(child);
1379 child.setParentTab(this);
1380 }
1381
1382 Vector<Tab> getChildTabs() {
1383 return mChildTabs;
1384 }
1385
1386 void resume() {
1387 if (mMainView != null) {
1388 mMainView.onResume();
1389 if (mSubView != null) {
1390 mSubView.onResume();
1391 }
1392 }
1393 }
1394
1395 void pause() {
1396 if (mMainView != null) {
1397 mMainView.onPause();
1398 if (mSubView != null) {
1399 mSubView.onPause();
1400 }
1401 }
1402 }
1403
1404 void putInForeground() {
1405 mInForeground = true;
1406 resume();
1407 mMainView.setOnCreateContextMenuListener(mActivity);
1408 if (mSubView != null) {
1409 mSubView.setOnCreateContextMenuListener(mActivity);
1410 }
1411 // Show the pending error dialog if the queue is not empty
1412 if (mQueuedErrors != null && mQueuedErrors.size() > 0) {
1413 showError(mQueuedErrors.getFirst());
1414 }
1415 }
1416
1417 void putInBackground() {
1418 mInForeground = false;
1419 pause();
1420 mMainView.setOnCreateContextMenuListener(null);
1421 if (mSubView != null) {
1422 mSubView.setOnCreateContextMenuListener(null);
1423 }
1424 }
1425
Michael Kolb8233fac2010-10-26 16:08:53 -07001426 boolean inForeground() {
1427 return mInForeground;
1428 }
1429
Grace Kloba22ac16e2009-10-07 18:00:23 -07001430 /**
1431 * Return the top window of this tab; either the subwindow if it is not
1432 * null or the main window.
1433 * @return The top window of this tab.
1434 */
1435 WebView getTopWindow() {
1436 if (mSubView != null) {
1437 return mSubView;
1438 }
1439 return mMainView;
1440 }
1441
1442 /**
1443 * Return the main window of this tab. Note: if a tab is freed in the
1444 * background, this can return null. It is only guaranteed to be
1445 * non-null for the current tab.
1446 * @return The main WebView of this tab.
1447 */
1448 WebView getWebView() {
1449 return mMainView;
1450 }
1451
Michael Kolb8233fac2010-10-26 16:08:53 -07001452 View getViewContainer() {
1453 return mContainer;
1454 }
1455
Grace Kloba22ac16e2009-10-07 18:00:23 -07001456 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001457 * Return whether private browsing is enabled for the main window of
1458 * this tab.
1459 * @return True if private browsing is enabled.
1460 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001461 boolean isPrivateBrowsingEnabled() {
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001462 WebView webView = getWebView();
1463 if (webView == null) {
1464 return false;
1465 }
1466 return webView.isPrivateBrowsingEnabled();
1467 }
1468
1469 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001470 * Return the subwindow of this tab or null if there is no subwindow.
1471 * @return The subwindow of this tab or null.
1472 */
1473 WebView getSubWebView() {
1474 return mSubView;
1475 }
1476
Michael Kolb1514bb72010-11-22 09:11:48 -08001477 void setSubWebView(WebView subView) {
1478 mSubView = subView;
1479 }
1480
Michael Kolb8233fac2010-10-26 16:08:53 -07001481 View getSubViewContainer() {
1482 return mSubViewContainer;
1483 }
1484
Michael Kolb1514bb72010-11-22 09:11:48 -08001485 void setSubViewContainer(View subViewContainer) {
1486 mSubViewContainer = subViewContainer;
1487 }
1488
Grace Kloba22ac16e2009-10-07 18:00:23 -07001489 /**
1490 * @return The geolocation permissions prompt for this tab.
1491 */
1492 GeolocationPermissionsPrompt getGeolocationPermissionsPrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001493 if (mGeolocationPermissionsPrompt == null) {
1494 ViewStub stub = (ViewStub) mContainer
1495 .findViewById(R.id.geolocation_permissions_prompt);
1496 mGeolocationPermissionsPrompt = (GeolocationPermissionsPrompt) stub
1497 .inflate();
1498 mGeolocationPermissionsPrompt.init();
1499 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001500 return mGeolocationPermissionsPrompt;
1501 }
1502
1503 /**
1504 * @return The application id string
1505 */
1506 String getAppId() {
1507 return mAppId;
1508 }
1509
1510 /**
1511 * Set the application id string
1512 * @param id
1513 */
1514 void setAppId(String id) {
1515 mAppId = id;
1516 }
1517
1518 /**
1519 * @return The original url associated with this Tab
1520 */
1521 String getOriginalUrl() {
1522 return mOriginalUrl;
1523 }
1524
1525 /**
1526 * Set the original url associated with this tab
1527 */
1528 void setOriginalUrl(String url) {
1529 mOriginalUrl = url;
1530 }
1531
1532 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07001533 * set the title for the tab
1534 */
1535 void setCurrentTitle(String title) {
1536 mCurrentTitle = title;
1537 }
1538
1539 /**
1540 * set url for this tab
1541 * @param url
1542 */
1543 void setCurrentUrl(String url) {
1544 mCurrentUrl = url;
1545 }
1546
1547 String getCurrentTitle() {
1548 return mCurrentTitle;
1549 }
1550
1551 String getCurrentUrl() {
1552 return mCurrentUrl;
1553 }
1554 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001555 * Get the url of this tab. Valid after calling populatePickerData, but
1556 * before calling wipePickerData, or if the webview has been destroyed.
1557 * @return The WebView's url or null.
1558 */
1559 String getUrl() {
1560 if (mPickerData != null) {
1561 return mPickerData.mUrl;
1562 }
1563 return null;
1564 }
1565
1566 /**
1567 * Get the title of this tab. Valid after calling populatePickerData, but
1568 * before calling wipePickerData, or if the webview has been destroyed. If
1569 * the url has no title, use the url instead.
1570 * @return The WebView's title (or url) or null.
1571 */
1572 String getTitle() {
1573 if (mPickerData != null) {
1574 return mPickerData.mTitle;
1575 }
1576 return null;
1577 }
1578
1579 /**
1580 * Get the favicon of this tab. Valid after calling populatePickerData, but
1581 * before calling wipePickerData, or if the webview has been destroyed.
1582 * @return The WebView's favicon or null.
1583 */
1584 Bitmap getFavicon() {
1585 if (mPickerData != null) {
1586 return mPickerData.mFavicon;
1587 }
1588 return null;
1589 }
1590
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001591
Grace Kloba22ac16e2009-10-07 18:00:23 -07001592 /**
1593 * Return the tab's error console. Creates the console if createIfNEcessary
1594 * is true and we haven't already created the console.
1595 * @param createIfNecessary Flag to indicate if the console should be
1596 * created if it has not been already.
1597 * @return The tab's error console, or null if one has not been created and
1598 * createIfNecessary is false.
1599 */
1600 ErrorConsoleView getErrorConsole(boolean createIfNecessary) {
1601 if (createIfNecessary && mErrorConsole == null) {
1602 mErrorConsole = new ErrorConsoleView(mActivity);
1603 mErrorConsole.setWebView(mMainView);
1604 }
1605 return mErrorConsole;
1606 }
1607
1608 /**
1609 * If this Tab was created through another Tab, then this method returns
1610 * that Tab.
1611 * @return the Tab parent or null
1612 */
1613 public Tab getParentTab() {
1614 return mParentTab;
1615 }
1616
1617 /**
1618 * Return whether this tab should be closed when it is backing out of the
1619 * first page.
1620 * @return TRUE if this tab should be closed when exit.
1621 */
1622 boolean closeOnExit() {
1623 return mCloseOnExit;
1624 }
1625
1626 /**
1627 * Saves the current lock-icon state before resetting the lock icon. If we
1628 * have an error, we may need to roll back to the previous state.
1629 */
1630 void resetLockIcon(String url) {
1631 mPrevLockIconType = mLockIconType;
Michael Kolb8233fac2010-10-26 16:08:53 -07001632 mLockIconType = LOCK_ICON_UNSECURE;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001633 if (URLUtil.isHttpsUrl(url)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001634 mLockIconType = LOCK_ICON_SECURE;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001635 }
1636 }
1637
1638 /**
1639 * Reverts the lock-icon state to the last saved state, for example, if we
1640 * had an error, and need to cancel the load.
1641 */
1642 void revertLockIcon() {
1643 mLockIconType = mPrevLockIconType;
1644 }
1645
1646 /**
1647 * @return The tab's lock icon type.
1648 */
1649 int getLockIconType() {
1650 return mLockIconType;
1651 }
1652
1653 /**
1654 * @return TRUE if onPageStarted is called while onPageFinished is not
1655 * called yet.
1656 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001657 boolean inPageLoad() {
1658 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001659 }
1660
1661 // force mInLoad to be false. This should only be called before closing the
1662 // tab to ensure BrowserActivity's pauseWebViewTimers() is called correctly.
Michael Kolb8233fac2010-10-26 16:08:53 -07001663 void clearInPageLoad() {
1664 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001665 }
1666
1667 void populatePickerData() {
1668 if (mMainView == null) {
1669 populatePickerDataFromSavedState();
1670 return;
1671 }
1672
1673 // FIXME: The only place we cared about subwindow was for
1674 // bookmarking (i.e. not when saving state). Was this deliberate?
1675 final WebBackForwardList list = mMainView.copyBackForwardList();
Leon Scroggins70a153b2010-05-10 17:27:26 -04001676 if (list == null) {
1677 Log.w(LOGTAG, "populatePickerData called and WebBackForwardList is null");
1678 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001679 final WebHistoryItem item = list != null ? list.getCurrentItem() : null;
1680 populatePickerData(item);
1681 }
1682
1683 // Populate the picker data using the given history item and the current top
1684 // WebView.
1685 private void populatePickerData(WebHistoryItem item) {
1686 mPickerData = new PickerData();
Leon Scroggins70a153b2010-05-10 17:27:26 -04001687 if (item == null) {
1688 Log.w(LOGTAG, "populatePickerData called with a null WebHistoryItem");
1689 } else {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001690 mPickerData.mUrl = item.getUrl();
1691 mPickerData.mTitle = item.getTitle();
1692 mPickerData.mFavicon = item.getFavicon();
1693 if (mPickerData.mTitle == null) {
1694 mPickerData.mTitle = mPickerData.mUrl;
1695 }
1696 }
1697 }
1698
1699 // Create the PickerData and populate it using the saved state of the tab.
1700 void populatePickerDataFromSavedState() {
1701 if (mSavedState == null) {
1702 return;
1703 }
1704 mPickerData = new PickerData();
1705 mPickerData.mUrl = mSavedState.getString(CURRURL);
1706 mPickerData.mTitle = mSavedState.getString(CURRTITLE);
1707 }
1708
1709 void clearPickerData() {
1710 mPickerData = null;
1711 }
1712
1713 /**
1714 * Get the saved state bundle.
1715 * @return
1716 */
1717 Bundle getSavedState() {
1718 return mSavedState;
1719 }
1720
1721 /**
1722 * Set the saved state.
1723 */
1724 void setSavedState(Bundle state) {
1725 mSavedState = state;
1726 }
1727
1728 /**
1729 * @return TRUE if succeed in saving the state.
1730 */
1731 boolean saveState() {
1732 // If the WebView is null it means we ran low on memory and we already
1733 // stored the saved state in mSavedState.
1734 if (mMainView == null) {
1735 return mSavedState != null;
1736 }
1737
1738 mSavedState = new Bundle();
1739 final WebBackForwardList list = mMainView.saveState(mSavedState);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001740
1741 // Store some extra info for displaying the tab in the picker.
1742 final WebHistoryItem item = list != null ? list.getCurrentItem() : null;
1743 populatePickerData(item);
1744
1745 if (mPickerData.mUrl != null) {
1746 mSavedState.putString(CURRURL, mPickerData.mUrl);
1747 }
1748 if (mPickerData.mTitle != null) {
1749 mSavedState.putString(CURRTITLE, mPickerData.mTitle);
1750 }
1751 mSavedState.putBoolean(CLOSEONEXIT, mCloseOnExit);
1752 if (mAppId != null) {
1753 mSavedState.putString(APPID, mAppId);
1754 }
1755 if (mOriginalUrl != null) {
1756 mSavedState.putString(ORIGINALURL, mOriginalUrl);
1757 }
1758 // Remember the parent tab so the relationship can be restored.
1759 if (mParentTab != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001760 mSavedState.putInt(PARENTTAB, mWebViewController.getTabControl().getTabIndex(
Grace Kloba22ac16e2009-10-07 18:00:23 -07001761 mParentTab));
1762 }
1763 return true;
1764 }
1765
1766 /*
1767 * Restore the state of the tab.
1768 */
1769 boolean restoreState(Bundle b) {
1770 if (b == null) {
1771 return false;
1772 }
1773 // Restore the internal state even if the WebView fails to restore.
1774 // This will maintain the app id, original url and close-on-exit values.
1775 mSavedState = null;
1776 mPickerData = null;
1777 mCloseOnExit = b.getBoolean(CLOSEONEXIT);
1778 mAppId = b.getString(APPID);
1779 mOriginalUrl = b.getString(ORIGINALURL);
1780
1781 final WebBackForwardList list = mMainView.restoreState(b);
1782 if (list == null) {
1783 return false;
1784 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001785 return true;
1786 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04001787
Grace Kloba22ac16e2009-10-07 18:00:23 -07001788}