blob: 8a3bc274820197180bd24ba721ff629408354ccd [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
Michael Kolba713ec82010-11-29 17:27:06 -080088 private View 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;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -0500131 // The listener that gets invoked when a download is started from the
132 // mMainView
133 private final DownloadListener mDownloadListener;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500134 // Listener used to know when we move forward or back in the history list.
135 private final WebBackForwardListClient mWebBackForwardListClient;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700136
137 // AsyncTask for downloading touch icons
138 DownloadTouchIcon mTouchIconLoader;
139
140 // Extra saved information for displaying the tab in the picker.
141 private static class PickerData {
142 String mUrl;
143 String mTitle;
144 Bitmap mFavicon;
145 }
146
147 // Used for saving and restoring each Tab
148 static final String WEBVIEW = "webview";
149 static final String NUMTABS = "numTabs";
150 static final String CURRTAB = "currentTab";
151 static final String CURRURL = "currentUrl";
152 static final String CURRTITLE = "currentTitle";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700153 static final String CLOSEONEXIT = "closeonexit";
154 static final String PARENTTAB = "parentTab";
155 static final String APPID = "appid";
156 static final String ORIGINALURL = "originalUrl";
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700157 static final String INCOGNITO = "privateBrowsingEnabled";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700158
159 // -------------------------------------------------------------------------
160
Leon Scroggins58d56c62010-01-28 15:12:40 -0500161 /**
162 * Private information regarding the latest voice search. If the Tab is not
163 * in voice search mode, this will be null.
164 */
165 private VoiceSearchData mVoiceSearchData;
166 /**
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400167 * Remove voice search mode from this tab.
168 */
169 public void revertVoiceSearchMode() {
170 if (mVoiceSearchData != null) {
171 mVoiceSearchData = null;
172 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700173 mWebViewController.revertVoiceSearchMode(this);
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400174 }
175 }
176 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700177
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400178 /**
Leon Scroggins58d56c62010-01-28 15:12:40 -0500179 * Return whether the tab is in voice search mode.
180 */
181 public boolean isInVoiceSearchMode() {
182 return mVoiceSearchData != null;
183 }
184 /**
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400185 * Return true if the Tab is in voice search mode and the voice search
186 * Intent came with a String identifying that Google provided the Intent.
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500187 */
188 public boolean voiceSearchSourceIsGoogle() {
189 return mVoiceSearchData != null && mVoiceSearchData.mSourceIsGoogle;
190 }
191 /**
Leon Scroggins58d56c62010-01-28 15:12:40 -0500192 * Get the title to display for the current voice search page. If the Tab
193 * is not in voice search mode, return null.
194 */
195 public String getVoiceDisplayTitle() {
196 if (mVoiceSearchData == null) return null;
197 return mVoiceSearchData.mLastVoiceSearchTitle;
198 }
199 /**
200 * Get the latest array of voice search results, to be passed to the
201 * BrowserProvider. If the Tab is not in voice search mode, return null.
202 */
203 public ArrayList<String> getVoiceSearchResults() {
204 if (mVoiceSearchData == null) return null;
205 return mVoiceSearchData.mVoiceSearchResults;
206 }
207 /**
208 * Activate voice search mode.
209 * @param intent Intent which has the results to use, or an index into the
210 * results when reusing the old results.
211 */
212 /* package */ void activateVoiceSearchMode(Intent intent) {
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500213 int index = 0;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500214 ArrayList<String> results = intent.getStringArrayListExtra(
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -0500215 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_STRINGS);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500216 if (results != null) {
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500217 ArrayList<String> urls = intent.getStringArrayListExtra(
218 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_URLS);
219 ArrayList<String> htmls = intent.getStringArrayListExtra(
220 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_HTML);
221 ArrayList<String> baseUrls = intent.getStringArrayListExtra(
222 RecognizerResultsIntent
223 .EXTRA_VOICE_SEARCH_RESULT_HTML_BASE_URLS);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500224 // This tab is now entering voice search mode for the first time, or
225 // a new voice search was done.
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500226 int size = results.size();
227 if (urls == null || size != urls.size()) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500228 throw new AssertionError("improper extras passed in Intent");
229 }
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500230 if (htmls == null || htmls.size() != size || baseUrls == null ||
231 (baseUrls.size() != size && baseUrls.size() != 1)) {
232 // If either of these arrays are empty/incorrectly sized, ignore
233 // them.
234 htmls = null;
235 baseUrls = null;
236 }
237 mVoiceSearchData = new VoiceSearchData(results, urls, htmls,
238 baseUrls);
Leon Scroggins9df94972010-03-08 18:20:35 -0500239 mVoiceSearchData.mHeaders = intent.getParcelableArrayListExtra(
240 RecognizerResultsIntent
241 .EXTRA_VOICE_SEARCH_RESULT_HTTP_HEADERS);
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500242 mVoiceSearchData.mSourceIsGoogle = intent.getBooleanExtra(
243 VoiceSearchData.SOURCE_IS_GOOGLE, false);
Leon Scroggins2ee4a5a2010-03-15 16:56:57 -0400244 mVoiceSearchData.mVoiceSearchIntent = new Intent(intent);
Leon Scrogginse10dde52010-03-08 19:53:03 -0500245 }
246 String extraData = intent.getStringExtra(
247 SearchManager.EXTRA_DATA_KEY);
248 if (extraData != null) {
249 index = Integer.parseInt(extraData);
250 if (index >= mVoiceSearchData.mVoiceSearchResults.size()) {
251 throw new AssertionError("index must be less than "
252 + "size of mVoiceSearchResults");
253 }
254 if (mVoiceSearchData.mSourceIsGoogle) {
255 Intent logIntent = new Intent(
256 LoggingEvents.ACTION_LOG_EVENT);
257 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
258 LoggingEvents.VoiceSearch.N_BEST_CHOOSE);
259 logIntent.putExtra(
260 LoggingEvents.VoiceSearch.EXTRA_N_BEST_CHOOSE_INDEX,
261 index);
262 mActivity.sendBroadcast(logIntent);
263 }
264 if (mVoiceSearchData.mVoiceSearchIntent != null) {
Leon Scroggins2ee4a5a2010-03-15 16:56:57 -0400265 // Copy the Intent, so that each history item will have its own
266 // Intent, with different (or none) extra data.
267 Intent latest = new Intent(mVoiceSearchData.mVoiceSearchIntent);
268 latest.putExtra(SearchManager.EXTRA_DATA_KEY, extraData);
269 mVoiceSearchData.mVoiceSearchIntent = latest;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500270 }
271 }
272 mVoiceSearchData.mLastVoiceSearchTitle
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500273 = mVoiceSearchData.mVoiceSearchResults.get(index);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500274 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700275 mWebViewController.activateVoiceSearchMode(mVoiceSearchData.mLastVoiceSearchTitle);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500276 }
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500277 if (mVoiceSearchData.mVoiceSearchHtmls != null) {
278 // When index was found it was already ensured that it was valid
279 String uriString = mVoiceSearchData.mVoiceSearchHtmls.get(index);
280 if (uriString != null) {
281 Uri dataUri = Uri.parse(uriString);
282 if (RecognizerResultsIntent.URI_SCHEME_INLINE.equals(
283 dataUri.getScheme())) {
284 // If there is only one base URL, use it. If there are
285 // more, there will be one for each index, so use the base
286 // URL corresponding to the index.
287 String baseUrl = mVoiceSearchData.mVoiceSearchBaseUrls.get(
288 mVoiceSearchData.mVoiceSearchBaseUrls.size() > 1 ?
289 index : 0);
290 mVoiceSearchData.mLastVoiceSearchUrl = baseUrl;
291 mMainView.loadDataWithBaseURL(baseUrl,
292 uriString.substring(RecognizerResultsIntent
293 .URI_SCHEME_INLINE.length() + 1), "text/html",
294 "utf-8", baseUrl);
295 return;
296 }
297 }
298 }
Leon Scroggins58d56c62010-01-28 15:12:40 -0500299 mVoiceSearchData.mLastVoiceSearchUrl
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500300 = mVoiceSearchData.mVoiceSearchUrls.get(index);
301 if (null == mVoiceSearchData.mLastVoiceSearchUrl) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700302 mVoiceSearchData.mLastVoiceSearchUrl = UrlUtils.smartUrlFilter(
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500303 mVoiceSearchData.mLastVoiceSearchTitle);
304 }
Leon Scroggins9df94972010-03-08 18:20:35 -0500305 Map<String, String> headers = null;
306 if (mVoiceSearchData.mHeaders != null) {
307 int bundleIndex = mVoiceSearchData.mHeaders.size() == 1 ? 0
308 : index;
309 Bundle bundle = mVoiceSearchData.mHeaders.get(bundleIndex);
310 if (bundle != null && !bundle.isEmpty()) {
311 Iterator<String> iter = bundle.keySet().iterator();
312 headers = new HashMap<String, String>();
313 while (iter.hasNext()) {
314 String key = iter.next();
315 headers.put(key, bundle.getString(key));
316 }
317 }
318 }
319 mMainView.loadUrl(mVoiceSearchData.mLastVoiceSearchUrl, headers);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500320 }
321 /* package */ static class VoiceSearchData {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500322 public VoiceSearchData(ArrayList<String> results,
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500323 ArrayList<String> urls, ArrayList<String> htmls,
324 ArrayList<String> baseUrls) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500325 mVoiceSearchResults = results;
326 mVoiceSearchUrls = urls;
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500327 mVoiceSearchHtmls = htmls;
328 mVoiceSearchBaseUrls = baseUrls;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500329 }
330 /*
331 * ArrayList of suggestions to be displayed when opening the
332 * SearchManager
333 */
334 public ArrayList<String> mVoiceSearchResults;
335 /*
336 * ArrayList of urls, associated with the suggestions in
337 * mVoiceSearchResults.
338 */
339 public ArrayList<String> mVoiceSearchUrls;
340 /*
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500341 * ArrayList holding content to load for each item in
342 * mVoiceSearchResults.
343 */
344 public ArrayList<String> mVoiceSearchHtmls;
345 /*
346 * ArrayList holding base urls for the items in mVoiceSearchResults.
347 * If non null, this will either have the same size as
348 * mVoiceSearchResults or have a size of 1, in which case all will use
349 * the same base url
350 */
351 public ArrayList<String> mVoiceSearchBaseUrls;
352 /*
Leon Scroggins58d56c62010-01-28 15:12:40 -0500353 * The last url provided by voice search. Used for comparison to see if
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500354 * we are going to a page by some method besides voice search.
Leon Scroggins58d56c62010-01-28 15:12:40 -0500355 */
356 public String mLastVoiceSearchUrl;
357 /**
358 * The last title used for voice search. Needed to update the title bar
359 * when switching tabs.
360 */
361 public String mLastVoiceSearchTitle;
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500362 /**
363 * Whether the Intent which turned on voice search mode contained the
364 * String signifying that Google was the source.
365 */
366 public boolean mSourceIsGoogle;
367 /**
Leon Scroggins9df94972010-03-08 18:20:35 -0500368 * List of headers to be passed into the WebView containing location
369 * information
370 */
371 public ArrayList<Bundle> mHeaders;
372 /**
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500373 * The Intent used to invoke voice search. Placed on the
374 * WebHistoryItem so that when coming back to a previous voice search
375 * page we can again activate voice search.
376 */
Leon Scrogginse10dde52010-03-08 19:53:03 -0500377 public Intent mVoiceSearchIntent;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500378 /**
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500379 * String used to identify Google as the source of voice search.
380 */
381 public static String SOURCE_IS_GOOGLE
382 = "android.speech.extras.SOURCE_IS_GOOGLE";
Leon Scroggins58d56c62010-01-28 15:12:40 -0500383 }
384
Grace Kloba22ac16e2009-10-07 18:00:23 -0700385 // Container class for the next error dialog that needs to be displayed
386 private class ErrorDialog {
387 public final int mTitle;
388 public final String mDescription;
389 public final int mError;
390 ErrorDialog(int title, String desc, int error) {
391 mTitle = title;
392 mDescription = desc;
393 mError = error;
394 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700395 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700396
397 private void processNextError() {
398 if (mQueuedErrors == null) {
399 return;
400 }
401 // The first one is currently displayed so just remove it.
402 mQueuedErrors.removeFirst();
403 if (mQueuedErrors.size() == 0) {
404 mQueuedErrors = null;
405 return;
406 }
407 showError(mQueuedErrors.getFirst());
408 }
409
410 private DialogInterface.OnDismissListener mDialogListener =
411 new DialogInterface.OnDismissListener() {
412 public void onDismiss(DialogInterface d) {
413 processNextError();
414 }
415 };
416 private LinkedList<ErrorDialog> mQueuedErrors;
417
418 private void queueError(int err, String desc) {
419 if (mQueuedErrors == null) {
420 mQueuedErrors = new LinkedList<ErrorDialog>();
421 }
422 for (ErrorDialog d : mQueuedErrors) {
423 if (d.mError == err) {
424 // Already saw a similar error, ignore the new one.
425 return;
426 }
427 }
428 ErrorDialog errDialog = new ErrorDialog(
429 err == WebViewClient.ERROR_FILE_NOT_FOUND ?
430 R.string.browserFrameFileErrorLabel :
431 R.string.browserFrameNetworkErrorLabel,
432 desc, err);
433 mQueuedErrors.addLast(errDialog);
434
435 // Show the dialog now if the queue was empty and it is in foreground
436 if (mQueuedErrors.size() == 1 && mInForeground) {
437 showError(errDialog);
438 }
439 }
440
441 private void showError(ErrorDialog errDialog) {
442 if (mInForeground) {
443 AlertDialog d = new AlertDialog.Builder(mActivity)
444 .setTitle(errDialog.mTitle)
445 .setMessage(errDialog.mDescription)
446 .setPositiveButton(R.string.ok, null)
447 .create();
448 d.setOnDismissListener(mDialogListener);
449 d.show();
450 }
451 }
452
453 // -------------------------------------------------------------------------
454 // WebViewClient implementation for the main WebView
455 // -------------------------------------------------------------------------
456
457 private final WebViewClient mWebViewClient = new WebViewClient() {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500458 private Message mDontResend;
459 private Message mResend;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700460 @Override
461 public void onPageStarted(WebView view, String url, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700462 mInPageLoad = true;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000463 mLoadStartTime = SystemClock.uptimeMillis();
Leon Scroggins58d56c62010-01-28 15:12:40 -0500464 if (mVoiceSearchData != null
465 && !url.equals(mVoiceSearchData.mLastVoiceSearchUrl)) {
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500466 if (mVoiceSearchData.mSourceIsGoogle) {
467 Intent i = new Intent(LoggingEvents.ACTION_LOG_EVENT);
468 i.putExtra(LoggingEvents.EXTRA_FLUSH, true);
469 mActivity.sendBroadcast(i);
470 }
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400471 revertVoiceSearchMode();
Leon Scroggins58d56c62010-01-28 15:12:40 -0500472 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700473
Grace Kloba22ac16e2009-10-07 18:00:23 -0700474
475 // If we start a touch icon load and then load a new page, we don't
476 // want to cancel the current touch icon loader. But, we do want to
477 // create a new one when the touch icon url is known.
478 if (mTouchIconLoader != null) {
479 mTouchIconLoader.mTab = null;
480 mTouchIconLoader = null;
481 }
482
483 // reset the error console
484 if (mErrorConsole != null) {
485 mErrorConsole.clearErrorMessages();
Michael Kolb8233fac2010-10-26 16:08:53 -0700486 if (mWebViewController.shouldShowErrorConsole()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700487 mErrorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
488 }
489 }
490
Grace Kloba22ac16e2009-10-07 18:00:23 -0700491
492 // finally update the UI in the activity if it is in the foreground
Michael Kolb8233fac2010-10-26 16:08:53 -0700493 mWebViewController.onPageStarted(Tab.this, view, url, favicon);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700494 }
495
496 @Override
497 public void onPageFinished(WebView view, String url) {
John Reck5b691842010-11-29 11:21:13 -0800498 if (!isPrivateBrowsingEnabled()) {
499 LogTag.logPageFinishedLoading(
500 url, SystemClock.uptimeMillis() - mLoadStartTime);
501 }
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
Grace Kloba22ac16e2009-10-07 18:00:23 -07001206
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001207 mDownloadListener = new DownloadListener() {
1208 public void onDownloadStart(String url, String userAgent,
1209 String contentDisposition, String mimetype,
1210 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001211 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001212 mimetype, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001213 }
1214 };
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001215 mWebBackForwardListClient = new WebBackForwardListClient() {
1216 @Override
1217 public void onNewHistoryItem(WebHistoryItem item) {
1218 if (isInVoiceSearchMode()) {
1219 item.setCustomData(mVoiceSearchData.mVoiceSearchIntent);
1220 }
1221 }
1222 @Override
1223 public void onIndexChanged(WebHistoryItem item, int index) {
1224 Object data = item.getCustomData();
1225 if (data != null && data instanceof Intent) {
1226 activateVoiceSearchMode((Intent) data);
1227 }
1228 }
1229 };
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001230
Grace Kloba22ac16e2009-10-07 18:00:23 -07001231 setWebView(w);
1232 }
1233
1234 /**
1235 * Sets the WebView for this tab, correctly removing the old WebView from
1236 * the container view.
1237 */
1238 void setWebView(WebView w) {
1239 if (mMainView == w) {
1240 return;
1241 }
Michael Kolba713ec82010-11-29 17:27:06 -08001242
Grace Kloba22ac16e2009-10-07 18:00:23 -07001243 // If the WebView is changing, the page will be reloaded, so any ongoing
1244 // Geolocation permission requests are void.
Grace Kloba50c241e2010-04-20 11:07:50 -07001245 if (mGeolocationPermissionsPrompt != null) {
1246 mGeolocationPermissionsPrompt.hide();
1247 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001248
Michael Kolba713ec82010-11-29 17:27:06 -08001249 mWebViewController.onSetWebView(this, w);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001250
1251 // set the new one
1252 mMainView = w;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001253 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001254 if (mMainView != null) {
1255 mMainView.setWebViewClient(mWebViewClient);
1256 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001257 // Attach DownloadManager so that downloads can start in an active
1258 // or a non-active window. This can happen when going to a site that
1259 // does a redirect after a period of time. The user could have
1260 // switched to another tab while waiting for the download to start.
1261 mMainView.setDownloadListener(mDownloadListener);
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001262 mMainView.setWebBackForwardListClient(mWebBackForwardListClient);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001263 }
1264 }
1265
1266 /**
1267 * Destroy the tab's main WebView and subWindow if any
1268 */
1269 void destroy() {
1270 if (mMainView != null) {
1271 dismissSubWindow();
1272 BrowserSettings.getInstance().deleteObserver(mMainView.getSettings());
1273 // save the WebView to call destroy() after detach it from the tab
1274 WebView webView = mMainView;
1275 setWebView(null);
1276 webView.destroy();
1277 }
1278 }
1279
1280 /**
1281 * Remove the tab from the parent
1282 */
1283 void removeFromTree() {
1284 // detach the children
1285 if (mChildTabs != null) {
1286 for(Tab t : mChildTabs) {
1287 t.setParentTab(null);
1288 }
1289 }
1290 // remove itself from the parent list
1291 if (mParentTab != null) {
1292 mParentTab.mChildTabs.remove(this);
1293 }
1294 }
1295
1296 /**
1297 * Create a new subwindow unless a subwindow already exists.
1298 * @return True if a new subwindow was created. False if one already exists.
1299 */
1300 boolean createSubWindow() {
1301 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001302 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001303 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001304 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001305 mSubView.setWebChromeClient(new SubWindowChromeClient(
1306 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001307 // Set a different DownloadListener for the mSubView, since it will
1308 // just need to dismiss the mSubView, rather than close the Tab
1309 mSubView.setDownloadListener(new DownloadListener() {
1310 public void onDownloadStart(String url, String userAgent,
1311 String contentDisposition, String mimetype,
1312 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001313 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001314 contentDisposition, mimetype, contentLength);
1315 if (mSubView.copyBackForwardList().getSize() == 0) {
1316 // This subwindow was opened for the sole purpose of
1317 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001318 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001319 }
1320 }
1321 });
Grace Kloba22ac16e2009-10-07 18:00:23 -07001322 mSubView.setOnCreateContextMenuListener(mActivity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001323 return true;
1324 }
1325 return false;
1326 }
1327
1328 /**
1329 * Dismiss the subWindow for the tab.
1330 */
1331 void dismissSubWindow() {
1332 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001333 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001334 BrowserSettings.getInstance().deleteObserver(
1335 mSubView.getSettings());
1336 mSubView.destroy();
1337 mSubView = null;
1338 mSubViewContainer = null;
1339 }
1340 }
1341
Grace Kloba22ac16e2009-10-07 18:00:23 -07001342
1343 /**
1344 * Set the parent tab of this tab.
1345 */
1346 void setParentTab(Tab parent) {
1347 mParentTab = parent;
1348 // This tab may have been freed due to low memory. If that is the case,
1349 // the parent tab index is already saved. If we are changing that index
1350 // (most likely due to removing the parent tab) we must update the
1351 // parent tab index in the saved Bundle.
1352 if (mSavedState != null) {
1353 if (parent == null) {
1354 mSavedState.remove(PARENTTAB);
1355 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -07001356 mSavedState.putInt(PARENTTAB, mWebViewController.getTabControl()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001357 .getTabIndex(parent));
1358 }
1359 }
1360 }
1361
1362 /**
1363 * When a Tab is created through the content of another Tab, then we
1364 * associate the Tabs.
1365 * @param child the Tab that was created from this Tab
1366 */
1367 void addChildTab(Tab child) {
1368 if (mChildTabs == null) {
1369 mChildTabs = new Vector<Tab>();
1370 }
1371 mChildTabs.add(child);
1372 child.setParentTab(this);
1373 }
1374
1375 Vector<Tab> getChildTabs() {
1376 return mChildTabs;
1377 }
1378
1379 void resume() {
1380 if (mMainView != null) {
1381 mMainView.onResume();
1382 if (mSubView != null) {
1383 mSubView.onResume();
1384 }
1385 }
1386 }
1387
1388 void pause() {
1389 if (mMainView != null) {
1390 mMainView.onPause();
1391 if (mSubView != null) {
1392 mSubView.onPause();
1393 }
1394 }
1395 }
1396
1397 void putInForeground() {
1398 mInForeground = true;
1399 resume();
1400 mMainView.setOnCreateContextMenuListener(mActivity);
1401 if (mSubView != null) {
1402 mSubView.setOnCreateContextMenuListener(mActivity);
1403 }
1404 // Show the pending error dialog if the queue is not empty
1405 if (mQueuedErrors != null && mQueuedErrors.size() > 0) {
1406 showError(mQueuedErrors.getFirst());
1407 }
1408 }
1409
1410 void putInBackground() {
1411 mInForeground = false;
1412 pause();
1413 mMainView.setOnCreateContextMenuListener(null);
1414 if (mSubView != null) {
1415 mSubView.setOnCreateContextMenuListener(null);
1416 }
1417 }
1418
Michael Kolb8233fac2010-10-26 16:08:53 -07001419 boolean inForeground() {
1420 return mInForeground;
1421 }
1422
Grace Kloba22ac16e2009-10-07 18:00:23 -07001423 /**
1424 * Return the top window of this tab; either the subwindow if it is not
1425 * null or the main window.
1426 * @return The top window of this tab.
1427 */
1428 WebView getTopWindow() {
1429 if (mSubView != null) {
1430 return mSubView;
1431 }
1432 return mMainView;
1433 }
1434
1435 /**
1436 * Return the main window of this tab. Note: if a tab is freed in the
1437 * background, this can return null. It is only guaranteed to be
1438 * non-null for the current tab.
1439 * @return The main WebView of this tab.
1440 */
1441 WebView getWebView() {
1442 return mMainView;
1443 }
1444
Michael Kolba713ec82010-11-29 17:27:06 -08001445 void setViewContainer(View container) {
1446 mContainer = container;
1447 }
1448
Michael Kolb8233fac2010-10-26 16:08:53 -07001449 View getViewContainer() {
1450 return mContainer;
1451 }
1452
Grace Kloba22ac16e2009-10-07 18:00:23 -07001453 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001454 * Return whether private browsing is enabled for the main window of
1455 * this tab.
1456 * @return True if private browsing is enabled.
1457 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001458 boolean isPrivateBrowsingEnabled() {
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001459 WebView webView = getWebView();
1460 if (webView == null) {
1461 return false;
1462 }
1463 return webView.isPrivateBrowsingEnabled();
1464 }
1465
1466 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001467 * Return the subwindow of this tab or null if there is no subwindow.
1468 * @return The subwindow of this tab or null.
1469 */
1470 WebView getSubWebView() {
1471 return mSubView;
1472 }
1473
Michael Kolb1514bb72010-11-22 09:11:48 -08001474 void setSubWebView(WebView subView) {
1475 mSubView = subView;
1476 }
1477
Michael Kolb8233fac2010-10-26 16:08:53 -07001478 View getSubViewContainer() {
1479 return mSubViewContainer;
1480 }
1481
Michael Kolb1514bb72010-11-22 09:11:48 -08001482 void setSubViewContainer(View subViewContainer) {
1483 mSubViewContainer = subViewContainer;
1484 }
1485
Grace Kloba22ac16e2009-10-07 18:00:23 -07001486 /**
1487 * @return The geolocation permissions prompt for this tab.
1488 */
1489 GeolocationPermissionsPrompt getGeolocationPermissionsPrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001490 if (mGeolocationPermissionsPrompt == null) {
1491 ViewStub stub = (ViewStub) mContainer
1492 .findViewById(R.id.geolocation_permissions_prompt);
1493 mGeolocationPermissionsPrompt = (GeolocationPermissionsPrompt) stub
1494 .inflate();
1495 mGeolocationPermissionsPrompt.init();
1496 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001497 return mGeolocationPermissionsPrompt;
1498 }
1499
1500 /**
1501 * @return The application id string
1502 */
1503 String getAppId() {
1504 return mAppId;
1505 }
1506
1507 /**
1508 * Set the application id string
1509 * @param id
1510 */
1511 void setAppId(String id) {
1512 mAppId = id;
1513 }
1514
1515 /**
1516 * @return The original url associated with this Tab
1517 */
1518 String getOriginalUrl() {
1519 return mOriginalUrl;
1520 }
1521
1522 /**
1523 * Set the original url associated with this tab
1524 */
1525 void setOriginalUrl(String url) {
1526 mOriginalUrl = url;
1527 }
1528
1529 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07001530 * set the title for the tab
1531 */
1532 void setCurrentTitle(String title) {
1533 mCurrentTitle = title;
1534 }
1535
1536 /**
1537 * set url for this tab
1538 * @param url
1539 */
1540 void setCurrentUrl(String url) {
1541 mCurrentUrl = url;
1542 }
1543
1544 String getCurrentTitle() {
1545 return mCurrentTitle;
1546 }
1547
1548 String getCurrentUrl() {
1549 return mCurrentUrl;
1550 }
1551 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001552 * Get the url of this tab. Valid after calling populatePickerData, but
1553 * before calling wipePickerData, or if the webview has been destroyed.
1554 * @return The WebView's url or null.
1555 */
1556 String getUrl() {
1557 if (mPickerData != null) {
1558 return mPickerData.mUrl;
1559 }
1560 return null;
1561 }
1562
1563 /**
1564 * Get the title of this tab. Valid after calling populatePickerData, but
1565 * before calling wipePickerData, or if the webview has been destroyed. If
1566 * the url has no title, use the url instead.
1567 * @return The WebView's title (or url) or null.
1568 */
1569 String getTitle() {
1570 if (mPickerData != null) {
1571 return mPickerData.mTitle;
1572 }
1573 return null;
1574 }
1575
1576 /**
1577 * Get the favicon of this tab. Valid after calling populatePickerData, but
1578 * before calling wipePickerData, or if the webview has been destroyed.
1579 * @return The WebView's favicon or null.
1580 */
1581 Bitmap getFavicon() {
1582 if (mPickerData != null) {
1583 return mPickerData.mFavicon;
1584 }
1585 return null;
1586 }
1587
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001588
Grace Kloba22ac16e2009-10-07 18:00:23 -07001589 /**
1590 * Return the tab's error console. Creates the console if createIfNEcessary
1591 * is true and we haven't already created the console.
1592 * @param createIfNecessary Flag to indicate if the console should be
1593 * created if it has not been already.
1594 * @return The tab's error console, or null if one has not been created and
1595 * createIfNecessary is false.
1596 */
1597 ErrorConsoleView getErrorConsole(boolean createIfNecessary) {
1598 if (createIfNecessary && mErrorConsole == null) {
1599 mErrorConsole = new ErrorConsoleView(mActivity);
1600 mErrorConsole.setWebView(mMainView);
1601 }
1602 return mErrorConsole;
1603 }
1604
1605 /**
1606 * If this Tab was created through another Tab, then this method returns
1607 * that Tab.
1608 * @return the Tab parent or null
1609 */
1610 public Tab getParentTab() {
1611 return mParentTab;
1612 }
1613
1614 /**
1615 * Return whether this tab should be closed when it is backing out of the
1616 * first page.
1617 * @return TRUE if this tab should be closed when exit.
1618 */
1619 boolean closeOnExit() {
1620 return mCloseOnExit;
1621 }
1622
1623 /**
1624 * Saves the current lock-icon state before resetting the lock icon. If we
1625 * have an error, we may need to roll back to the previous state.
1626 */
1627 void resetLockIcon(String url) {
1628 mPrevLockIconType = mLockIconType;
Michael Kolb8233fac2010-10-26 16:08:53 -07001629 mLockIconType = LOCK_ICON_UNSECURE;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001630 if (URLUtil.isHttpsUrl(url)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001631 mLockIconType = LOCK_ICON_SECURE;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001632 }
1633 }
1634
1635 /**
1636 * Reverts the lock-icon state to the last saved state, for example, if we
1637 * had an error, and need to cancel the load.
1638 */
1639 void revertLockIcon() {
1640 mLockIconType = mPrevLockIconType;
1641 }
1642
1643 /**
1644 * @return The tab's lock icon type.
1645 */
1646 int getLockIconType() {
1647 return mLockIconType;
1648 }
1649
1650 /**
1651 * @return TRUE if onPageStarted is called while onPageFinished is not
1652 * called yet.
1653 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001654 boolean inPageLoad() {
1655 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001656 }
1657
1658 // force mInLoad to be false. This should only be called before closing the
1659 // tab to ensure BrowserActivity's pauseWebViewTimers() is called correctly.
Michael Kolb8233fac2010-10-26 16:08:53 -07001660 void clearInPageLoad() {
1661 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001662 }
1663
1664 void populatePickerData() {
1665 if (mMainView == null) {
1666 populatePickerDataFromSavedState();
1667 return;
1668 }
1669
1670 // FIXME: The only place we cared about subwindow was for
1671 // bookmarking (i.e. not when saving state). Was this deliberate?
1672 final WebBackForwardList list = mMainView.copyBackForwardList();
Leon Scroggins70a153b2010-05-10 17:27:26 -04001673 if (list == null) {
1674 Log.w(LOGTAG, "populatePickerData called and WebBackForwardList is null");
1675 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001676 final WebHistoryItem item = list != null ? list.getCurrentItem() : null;
1677 populatePickerData(item);
1678 }
1679
1680 // Populate the picker data using the given history item and the current top
1681 // WebView.
1682 private void populatePickerData(WebHistoryItem item) {
1683 mPickerData = new PickerData();
Leon Scroggins70a153b2010-05-10 17:27:26 -04001684 if (item == null) {
1685 Log.w(LOGTAG, "populatePickerData called with a null WebHistoryItem");
1686 } else {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001687 mPickerData.mUrl = item.getUrl();
1688 mPickerData.mTitle = item.getTitle();
1689 mPickerData.mFavicon = item.getFavicon();
1690 if (mPickerData.mTitle == null) {
1691 mPickerData.mTitle = mPickerData.mUrl;
1692 }
1693 }
1694 }
1695
1696 // Create the PickerData and populate it using the saved state of the tab.
1697 void populatePickerDataFromSavedState() {
1698 if (mSavedState == null) {
1699 return;
1700 }
1701 mPickerData = new PickerData();
1702 mPickerData.mUrl = mSavedState.getString(CURRURL);
1703 mPickerData.mTitle = mSavedState.getString(CURRTITLE);
1704 }
1705
1706 void clearPickerData() {
1707 mPickerData = null;
1708 }
1709
1710 /**
1711 * Get the saved state bundle.
1712 * @return
1713 */
1714 Bundle getSavedState() {
1715 return mSavedState;
1716 }
1717
1718 /**
1719 * Set the saved state.
1720 */
1721 void setSavedState(Bundle state) {
1722 mSavedState = state;
1723 }
1724
1725 /**
1726 * @return TRUE if succeed in saving the state.
1727 */
1728 boolean saveState() {
1729 // If the WebView is null it means we ran low on memory and we already
1730 // stored the saved state in mSavedState.
1731 if (mMainView == null) {
1732 return mSavedState != null;
1733 }
1734
1735 mSavedState = new Bundle();
1736 final WebBackForwardList list = mMainView.saveState(mSavedState);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001737
1738 // Store some extra info for displaying the tab in the picker.
1739 final WebHistoryItem item = list != null ? list.getCurrentItem() : null;
1740 populatePickerData(item);
1741
1742 if (mPickerData.mUrl != null) {
1743 mSavedState.putString(CURRURL, mPickerData.mUrl);
1744 }
1745 if (mPickerData.mTitle != null) {
1746 mSavedState.putString(CURRTITLE, mPickerData.mTitle);
1747 }
1748 mSavedState.putBoolean(CLOSEONEXIT, mCloseOnExit);
1749 if (mAppId != null) {
1750 mSavedState.putString(APPID, mAppId);
1751 }
1752 if (mOriginalUrl != null) {
1753 mSavedState.putString(ORIGINALURL, mOriginalUrl);
1754 }
1755 // Remember the parent tab so the relationship can be restored.
1756 if (mParentTab != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001757 mSavedState.putInt(PARENTTAB, mWebViewController.getTabControl().getTabIndex(
Grace Kloba22ac16e2009-10-07 18:00:23 -07001758 mParentTab));
1759 }
1760 return true;
1761 }
1762
1763 /*
1764 * Restore the state of the tab.
1765 */
1766 boolean restoreState(Bundle b) {
1767 if (b == null) {
1768 return false;
1769 }
1770 // Restore the internal state even if the WebView fails to restore.
1771 // This will maintain the app id, original url and close-on-exit values.
1772 mSavedState = null;
1773 mPickerData = null;
1774 mCloseOnExit = b.getBoolean(CLOSEONEXIT);
1775 mAppId = b.getString(APPID);
1776 mOriginalUrl = b.getString(ORIGINALURL);
1777
1778 final WebBackForwardList list = mMainView.restoreState(b);
1779 if (list == null) {
1780 return false;
1781 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001782 return true;
1783 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04001784
Grace Kloba22ac16e2009-10-07 18:00:23 -07001785}