blob: b12b31775e671bc6026e9d785b0d22533e761abb [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;
57
Michael Kolbfe251992010-07-08 15:41:55 -070058import java.util.ArrayList;
59import java.util.HashMap;
60import java.util.Iterator;
61import java.util.LinkedList;
62import java.util.Map;
63import java.util.Vector;
64
Grace Kloba22ac16e2009-10-07 18:00:23 -070065/**
66 * Class for maintaining Tabs with a main WebView and a subwindow.
67 */
68class Tab {
Michael Kolb8233fac2010-10-26 16:08:53 -070069
Grace Kloba22ac16e2009-10-07 18:00:23 -070070 // Log Tag
71 private static final String LOGTAG = "Tab";
Ben Murdochc42addf2010-01-28 15:19:59 +000072 // Special case the logtag for messages for the Console to make it easier to
73 // filter them and match the logtag used for these messages in older versions
74 // of the browser.
75 private static final String CONSOLE_LOGTAG = "browser";
76
Michael Kolb8233fac2010-10-26 16:08:53 -070077 final static int LOCK_ICON_UNSECURE = 0;
78 final static int LOCK_ICON_SECURE = 1;
79 final static int LOCK_ICON_MIXED = 2;
80
81 Activity mActivity;
82 private WebViewController mWebViewController;
83
Grace Kloba22ac16e2009-10-07 18:00:23 -070084 // The Geolocation permissions prompt
85 private GeolocationPermissionsPrompt mGeolocationPermissionsPrompt;
86 // Main WebView wrapper
Leon Scroggins III211ba542010-04-19 13:21:13 -040087 private LinearLayout mContainer;
Grace Kloba22ac16e2009-10-07 18:00:23 -070088 // Main WebView
89 private WebView mMainView;
90 // Subwindow container
91 private View mSubViewContainer;
92 // Subwindow WebView
93 private WebView mSubView;
94 // Saved bundle for when we are running low on memory. It contains the
95 // information needed to restore the WebView if the user goes back to the
96 // tab.
97 private Bundle mSavedState;
98 // Data used when displaying the tab in the picker.
99 private PickerData mPickerData;
100 // Parent Tab. This is the Tab that created this Tab, or null if the Tab was
101 // created by the UI
102 private Tab mParentTab;
103 // Tab that constructed by this Tab. This is used when this Tab is
104 // destroyed, it clears all mParentTab values in the children.
105 private Vector<Tab> mChildTabs;
106 // If true, the tab will be removed when back out of the first page.
107 private boolean mCloseOnExit;
108 // If true, the tab is in the foreground of the current activity.
109 private boolean mInForeground;
Michael Kolb8233fac2010-10-26 16:08:53 -0700110 // If true, the tab is in page loading state (after onPageStarted,
111 // before onPageFinsihed)
112 private boolean mInPageLoad;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000113 // The time the load started, used to find load page time
114 private long mLoadStartTime;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700115 // Application identifier used to find tabs that another application wants
116 // to reuse.
117 private String mAppId;
118 // Keep the original url around to avoid killing the old WebView if the url
119 // has not changed.
120 private String mOriginalUrl;
Michael Kolb8233fac2010-10-26 16:08:53 -0700121 // Hold on to the currently loaded url
122 private String mCurrentUrl;
123 //The currently loaded title
124 private String mCurrentTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700125 // Error console for the tab
126 private ErrorConsoleView mErrorConsole;
127 // the lock icon type and previous lock icon type for the tab
128 private int mLockIconType;
129 private int mPrevLockIconType;
130 // Inflation service for making subwindows.
131 private final LayoutInflater mInflateService;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -0500132 // The listener that gets invoked when a download is started from the
133 // mMainView
134 private final DownloadListener mDownloadListener;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500135 // Listener used to know when we move forward or back in the history list.
136 private final WebBackForwardListClient mWebBackForwardListClient;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700137
138 // AsyncTask for downloading touch icons
139 DownloadTouchIcon mTouchIconLoader;
140
141 // Extra saved information for displaying the tab in the picker.
142 private static class PickerData {
143 String mUrl;
144 String mTitle;
145 Bitmap mFavicon;
146 }
147
148 // Used for saving and restoring each Tab
149 static final String WEBVIEW = "webview";
150 static final String NUMTABS = "numTabs";
151 static final String CURRTAB = "currentTab";
152 static final String CURRURL = "currentUrl";
153 static final String CURRTITLE = "currentTitle";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700154 static final String CLOSEONEXIT = "closeonexit";
155 static final String PARENTTAB = "parentTab";
156 static final String APPID = "appid";
157 static final String ORIGINALURL = "originalUrl";
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700158 static final String INCOGNITO = "privateBrowsingEnabled";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700159
160 // -------------------------------------------------------------------------
161
Leon Scroggins58d56c62010-01-28 15:12:40 -0500162 /**
163 * Private information regarding the latest voice search. If the Tab is not
164 * in voice search mode, this will be null.
165 */
166 private VoiceSearchData mVoiceSearchData;
167 /**
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400168 * Remove voice search mode from this tab.
169 */
170 public void revertVoiceSearchMode() {
171 if (mVoiceSearchData != null) {
172 mVoiceSearchData = null;
173 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700174 mWebViewController.revertVoiceSearchMode(this);
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400175 }
176 }
177 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700178
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400179 /**
Leon Scroggins58d56c62010-01-28 15:12:40 -0500180 * Return whether the tab is in voice search mode.
181 */
182 public boolean isInVoiceSearchMode() {
183 return mVoiceSearchData != null;
184 }
185 /**
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400186 * Return true if the Tab is in voice search mode and the voice search
187 * Intent came with a String identifying that Google provided the Intent.
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500188 */
189 public boolean voiceSearchSourceIsGoogle() {
190 return mVoiceSearchData != null && mVoiceSearchData.mSourceIsGoogle;
191 }
192 /**
Leon Scroggins58d56c62010-01-28 15:12:40 -0500193 * Get the title to display for the current voice search page. If the Tab
194 * is not in voice search mode, return null.
195 */
196 public String getVoiceDisplayTitle() {
197 if (mVoiceSearchData == null) return null;
198 return mVoiceSearchData.mLastVoiceSearchTitle;
199 }
200 /**
201 * Get the latest array of voice search results, to be passed to the
202 * BrowserProvider. If the Tab is not in voice search mode, return null.
203 */
204 public ArrayList<String> getVoiceSearchResults() {
205 if (mVoiceSearchData == null) return null;
206 return mVoiceSearchData.mVoiceSearchResults;
207 }
208 /**
209 * Activate voice search mode.
210 * @param intent Intent which has the results to use, or an index into the
211 * results when reusing the old results.
212 */
213 /* package */ void activateVoiceSearchMode(Intent intent) {
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500214 int index = 0;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500215 ArrayList<String> results = intent.getStringArrayListExtra(
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -0500216 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_STRINGS);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500217 if (results != null) {
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500218 ArrayList<String> urls = intent.getStringArrayListExtra(
219 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_URLS);
220 ArrayList<String> htmls = intent.getStringArrayListExtra(
221 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_HTML);
222 ArrayList<String> baseUrls = intent.getStringArrayListExtra(
223 RecognizerResultsIntent
224 .EXTRA_VOICE_SEARCH_RESULT_HTML_BASE_URLS);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500225 // This tab is now entering voice search mode for the first time, or
226 // a new voice search was done.
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500227 int size = results.size();
228 if (urls == null || size != urls.size()) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500229 throw new AssertionError("improper extras passed in Intent");
230 }
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500231 if (htmls == null || htmls.size() != size || baseUrls == null ||
232 (baseUrls.size() != size && baseUrls.size() != 1)) {
233 // If either of these arrays are empty/incorrectly sized, ignore
234 // them.
235 htmls = null;
236 baseUrls = null;
237 }
238 mVoiceSearchData = new VoiceSearchData(results, urls, htmls,
239 baseUrls);
Leon Scroggins9df94972010-03-08 18:20:35 -0500240 mVoiceSearchData.mHeaders = intent.getParcelableArrayListExtra(
241 RecognizerResultsIntent
242 .EXTRA_VOICE_SEARCH_RESULT_HTTP_HEADERS);
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500243 mVoiceSearchData.mSourceIsGoogle = intent.getBooleanExtra(
244 VoiceSearchData.SOURCE_IS_GOOGLE, false);
Leon Scroggins2ee4a5a2010-03-15 16:56:57 -0400245 mVoiceSearchData.mVoiceSearchIntent = new Intent(intent);
Leon Scrogginse10dde52010-03-08 19:53:03 -0500246 }
247 String extraData = intent.getStringExtra(
248 SearchManager.EXTRA_DATA_KEY);
249 if (extraData != null) {
250 index = Integer.parseInt(extraData);
251 if (index >= mVoiceSearchData.mVoiceSearchResults.size()) {
252 throw new AssertionError("index must be less than "
253 + "size of mVoiceSearchResults");
254 }
255 if (mVoiceSearchData.mSourceIsGoogle) {
256 Intent logIntent = new Intent(
257 LoggingEvents.ACTION_LOG_EVENT);
258 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
259 LoggingEvents.VoiceSearch.N_BEST_CHOOSE);
260 logIntent.putExtra(
261 LoggingEvents.VoiceSearch.EXTRA_N_BEST_CHOOSE_INDEX,
262 index);
263 mActivity.sendBroadcast(logIntent);
264 }
265 if (mVoiceSearchData.mVoiceSearchIntent != null) {
Leon Scroggins2ee4a5a2010-03-15 16:56:57 -0400266 // Copy the Intent, so that each history item will have its own
267 // Intent, with different (or none) extra data.
268 Intent latest = new Intent(mVoiceSearchData.mVoiceSearchIntent);
269 latest.putExtra(SearchManager.EXTRA_DATA_KEY, extraData);
270 mVoiceSearchData.mVoiceSearchIntent = latest;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500271 }
272 }
273 mVoiceSearchData.mLastVoiceSearchTitle
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500274 = mVoiceSearchData.mVoiceSearchResults.get(index);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500275 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700276 mWebViewController.activateVoiceSearchMode(mVoiceSearchData.mLastVoiceSearchTitle);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500277 }
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500278 if (mVoiceSearchData.mVoiceSearchHtmls != null) {
279 // When index was found it was already ensured that it was valid
280 String uriString = mVoiceSearchData.mVoiceSearchHtmls.get(index);
281 if (uriString != null) {
282 Uri dataUri = Uri.parse(uriString);
283 if (RecognizerResultsIntent.URI_SCHEME_INLINE.equals(
284 dataUri.getScheme())) {
285 // If there is only one base URL, use it. If there are
286 // more, there will be one for each index, so use the base
287 // URL corresponding to the index.
288 String baseUrl = mVoiceSearchData.mVoiceSearchBaseUrls.get(
289 mVoiceSearchData.mVoiceSearchBaseUrls.size() > 1 ?
290 index : 0);
291 mVoiceSearchData.mLastVoiceSearchUrl = baseUrl;
292 mMainView.loadDataWithBaseURL(baseUrl,
293 uriString.substring(RecognizerResultsIntent
294 .URI_SCHEME_INLINE.length() + 1), "text/html",
295 "utf-8", baseUrl);
296 return;
297 }
298 }
299 }
Leon Scroggins58d56c62010-01-28 15:12:40 -0500300 mVoiceSearchData.mLastVoiceSearchUrl
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500301 = mVoiceSearchData.mVoiceSearchUrls.get(index);
302 if (null == mVoiceSearchData.mLastVoiceSearchUrl) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700303 mVoiceSearchData.mLastVoiceSearchUrl = UrlUtils.smartUrlFilter(
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500304 mVoiceSearchData.mLastVoiceSearchTitle);
305 }
Leon Scroggins9df94972010-03-08 18:20:35 -0500306 Map<String, String> headers = null;
307 if (mVoiceSearchData.mHeaders != null) {
308 int bundleIndex = mVoiceSearchData.mHeaders.size() == 1 ? 0
309 : index;
310 Bundle bundle = mVoiceSearchData.mHeaders.get(bundleIndex);
311 if (bundle != null && !bundle.isEmpty()) {
312 Iterator<String> iter = bundle.keySet().iterator();
313 headers = new HashMap<String, String>();
314 while (iter.hasNext()) {
315 String key = iter.next();
316 headers.put(key, bundle.getString(key));
317 }
318 }
319 }
320 mMainView.loadUrl(mVoiceSearchData.mLastVoiceSearchUrl, headers);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500321 }
322 /* package */ static class VoiceSearchData {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500323 public VoiceSearchData(ArrayList<String> results,
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500324 ArrayList<String> urls, ArrayList<String> htmls,
325 ArrayList<String> baseUrls) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500326 mVoiceSearchResults = results;
327 mVoiceSearchUrls = urls;
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500328 mVoiceSearchHtmls = htmls;
329 mVoiceSearchBaseUrls = baseUrls;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500330 }
331 /*
332 * ArrayList of suggestions to be displayed when opening the
333 * SearchManager
334 */
335 public ArrayList<String> mVoiceSearchResults;
336 /*
337 * ArrayList of urls, associated with the suggestions in
338 * mVoiceSearchResults.
339 */
340 public ArrayList<String> mVoiceSearchUrls;
341 /*
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500342 * ArrayList holding content to load for each item in
343 * mVoiceSearchResults.
344 */
345 public ArrayList<String> mVoiceSearchHtmls;
346 /*
347 * ArrayList holding base urls for the items in mVoiceSearchResults.
348 * If non null, this will either have the same size as
349 * mVoiceSearchResults or have a size of 1, in which case all will use
350 * the same base url
351 */
352 public ArrayList<String> mVoiceSearchBaseUrls;
353 /*
Leon Scroggins58d56c62010-01-28 15:12:40 -0500354 * The last url provided by voice search. Used for comparison to see if
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500355 * we are going to a page by some method besides voice search.
Leon Scroggins58d56c62010-01-28 15:12:40 -0500356 */
357 public String mLastVoiceSearchUrl;
358 /**
359 * The last title used for voice search. Needed to update the title bar
360 * when switching tabs.
361 */
362 public String mLastVoiceSearchTitle;
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500363 /**
364 * Whether the Intent which turned on voice search mode contained the
365 * String signifying that Google was the source.
366 */
367 public boolean mSourceIsGoogle;
368 /**
Leon Scroggins9df94972010-03-08 18:20:35 -0500369 * List of headers to be passed into the WebView containing location
370 * information
371 */
372 public ArrayList<Bundle> mHeaders;
373 /**
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500374 * The Intent used to invoke voice search. Placed on the
375 * WebHistoryItem so that when coming back to a previous voice search
376 * page we can again activate voice search.
377 */
Leon Scrogginse10dde52010-03-08 19:53:03 -0500378 public Intent mVoiceSearchIntent;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500379 /**
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500380 * String used to identify Google as the source of voice search.
381 */
382 public static String SOURCE_IS_GOOGLE
383 = "android.speech.extras.SOURCE_IS_GOOGLE";
Leon Scroggins58d56c62010-01-28 15:12:40 -0500384 }
385
Grace Kloba22ac16e2009-10-07 18:00:23 -0700386 // Container class for the next error dialog that needs to be displayed
387 private class ErrorDialog {
388 public final int mTitle;
389 public final String mDescription;
390 public final int mError;
391 ErrorDialog(int title, String desc, int error) {
392 mTitle = title;
393 mDescription = desc;
394 mError = error;
395 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700396 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700397
398 private void processNextError() {
399 if (mQueuedErrors == null) {
400 return;
401 }
402 // The first one is currently displayed so just remove it.
403 mQueuedErrors.removeFirst();
404 if (mQueuedErrors.size() == 0) {
405 mQueuedErrors = null;
406 return;
407 }
408 showError(mQueuedErrors.getFirst());
409 }
410
411 private DialogInterface.OnDismissListener mDialogListener =
412 new DialogInterface.OnDismissListener() {
413 public void onDismiss(DialogInterface d) {
414 processNextError();
415 }
416 };
417 private LinkedList<ErrorDialog> mQueuedErrors;
418
419 private void queueError(int err, String desc) {
420 if (mQueuedErrors == null) {
421 mQueuedErrors = new LinkedList<ErrorDialog>();
422 }
423 for (ErrorDialog d : mQueuedErrors) {
424 if (d.mError == err) {
425 // Already saw a similar error, ignore the new one.
426 return;
427 }
428 }
429 ErrorDialog errDialog = new ErrorDialog(
430 err == WebViewClient.ERROR_FILE_NOT_FOUND ?
431 R.string.browserFrameFileErrorLabel :
432 R.string.browserFrameNetworkErrorLabel,
433 desc, err);
434 mQueuedErrors.addLast(errDialog);
435
436 // Show the dialog now if the queue was empty and it is in foreground
437 if (mQueuedErrors.size() == 1 && mInForeground) {
438 showError(errDialog);
439 }
440 }
441
442 private void showError(ErrorDialog errDialog) {
443 if (mInForeground) {
444 AlertDialog d = new AlertDialog.Builder(mActivity)
445 .setTitle(errDialog.mTitle)
446 .setMessage(errDialog.mDescription)
447 .setPositiveButton(R.string.ok, null)
448 .create();
449 d.setOnDismissListener(mDialogListener);
450 d.show();
451 }
452 }
453
454 // -------------------------------------------------------------------------
455 // WebViewClient implementation for the main WebView
456 // -------------------------------------------------------------------------
457
458 private final WebViewClient mWebViewClient = new WebViewClient() {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500459 private Message mDontResend;
460 private Message mResend;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700461 @Override
462 public void onPageStarted(WebView view, String url, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700463 mInPageLoad = true;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000464 mLoadStartTime = SystemClock.uptimeMillis();
Leon Scroggins58d56c62010-01-28 15:12:40 -0500465 if (mVoiceSearchData != null
466 && !url.equals(mVoiceSearchData.mLastVoiceSearchUrl)) {
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500467 if (mVoiceSearchData.mSourceIsGoogle) {
468 Intent i = new Intent(LoggingEvents.ACTION_LOG_EVENT);
469 i.putExtra(LoggingEvents.EXTRA_FLUSH, true);
470 mActivity.sendBroadcast(i);
471 }
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400472 revertVoiceSearchMode();
Leon Scroggins58d56c62010-01-28 15:12:40 -0500473 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700474
Grace Kloba22ac16e2009-10-07 18:00:23 -0700475
476 // If we start a touch icon load and then load a new page, we don't
477 // want to cancel the current touch icon loader. But, we do want to
478 // create a new one when the touch icon url is known.
479 if (mTouchIconLoader != null) {
480 mTouchIconLoader.mTab = null;
481 mTouchIconLoader = null;
482 }
483
484 // reset the error console
485 if (mErrorConsole != null) {
486 mErrorConsole.clearErrorMessages();
Michael Kolb8233fac2010-10-26 16:08:53 -0700487 if (mWebViewController.shouldShowErrorConsole()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700488 mErrorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
489 }
490 }
491
Grace Kloba22ac16e2009-10-07 18:00:23 -0700492
493 // finally update the UI in the activity if it is in the foreground
Michael Kolb8233fac2010-10-26 16:08:53 -0700494 mWebViewController.onPageStarted(Tab.this, view, url, favicon);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700495 }
496
497 @Override
498 public void onPageFinished(WebView view, String url) {
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000499 LogTag.logPageFinishedLoading(
500 url, SystemClock.uptimeMillis() - mLoadStartTime);
Michael Kolb8233fac2010-10-26 16:08:53 -0700501 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700502
Michael Kolb8233fac2010-10-26 16:08:53 -0700503 mWebViewController.onPageFinished(Tab.this, url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700504 }
505
506 // return true if want to hijack the url to let another app to handle it
507 @Override
508 public boolean shouldOverrideUrlLoading(WebView view, String url) {
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400509 if (voiceSearchSourceIsGoogle()) {
510 // This method is called when the user clicks on a link.
511 // VoiceSearchMode is turned off when the user leaves the
512 // Google results page, so at this point the user must be on
513 // that page. If the user clicked a link on that page, assume
514 // that the voice search was effective, and broadcast an Intent
515 // so a receiver can take note of that fact.
516 Intent logIntent = new Intent(LoggingEvents.ACTION_LOG_EVENT);
517 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
518 LoggingEvents.VoiceSearch.RESULT_CLICKED);
519 mActivity.sendBroadcast(logIntent);
520 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700521 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700522 return mWebViewController.shouldOverrideUrlLoading(view, url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700523 } else {
524 return false;
525 }
526 }
527
528 /**
529 * Updates the lock icon. This method is called when we discover another
530 * resource to be loaded for this page (for example, javascript). While
531 * we update the icon type, we do not update the lock icon itself until
532 * we are done loading, it is slightly more secure this way.
533 */
534 @Override
535 public void onLoadResource(WebView view, String url) {
536 if (url != null && url.length() > 0) {
537 // It is only if the page claims to be secure that we may have
538 // to update the lock:
Michael Kolb8233fac2010-10-26 16:08:53 -0700539 if (mLockIconType == LOCK_ICON_SECURE) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700540 // If NOT a 'safe' url, change the lock to mixed content!
541 if (!(URLUtil.isHttpsUrl(url) || URLUtil.isDataUrl(url)
542 || URLUtil.isAboutUrl(url))) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700543 mLockIconType = LOCK_ICON_MIXED;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700544 }
545 }
546 }
547 }
548
549 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700550 * Show a dialog informing the user of the network error reported by
551 * WebCore if it is in the foreground.
552 */
553 @Override
554 public void onReceivedError(WebView view, int errorCode,
555 String description, String failingUrl) {
556 if (errorCode != WebViewClient.ERROR_HOST_LOOKUP &&
557 errorCode != WebViewClient.ERROR_CONNECT &&
558 errorCode != WebViewClient.ERROR_BAD_URL &&
559 errorCode != WebViewClient.ERROR_UNSUPPORTED_SCHEME &&
560 errorCode != WebViewClient.ERROR_FILE) {
561 queueError(errorCode, description);
562 }
Jeff Hamilton47654f42010-09-07 09:57:51 -0500563
564 // Don't log URLs when in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -0700565 if (!isPrivateBrowsingEnabled()) {
Jeff Hamilton47654f42010-09-07 09:57:51 -0500566 Log.e(LOGTAG, "onReceivedError " + errorCode + " " + failingUrl
567 + " " + description);
568 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700569
570 // We need to reset the title after an error if it is in foreground.
571 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700572 mWebViewController.resetTitleAndRevertLockIcon(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700573 }
574 }
575
576 /**
577 * Check with the user if it is ok to resend POST data as the page they
578 * are trying to navigate to is the result of a POST.
579 */
580 @Override
581 public void onFormResubmission(WebView view, final Message dontResend,
582 final Message resend) {
583 if (!mInForeground) {
584 dontResend.sendToTarget();
585 return;
586 }
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500587 if (mDontResend != null) {
588 Log.w(LOGTAG, "onFormResubmission should not be called again "
589 + "while dialog is still up");
590 dontResend.sendToTarget();
591 return;
592 }
593 mDontResend = dontResend;
594 mResend = resend;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700595 new AlertDialog.Builder(mActivity).setTitle(
596 R.string.browserFrameFormResubmitLabel).setMessage(
597 R.string.browserFrameFormResubmitMessage)
598 .setPositiveButton(R.string.ok,
599 new DialogInterface.OnClickListener() {
600 public void onClick(DialogInterface dialog,
601 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500602 if (mResend != null) {
603 mResend.sendToTarget();
604 mResend = null;
605 mDontResend = null;
606 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700607 }
608 }).setNegativeButton(R.string.cancel,
609 new DialogInterface.OnClickListener() {
610 public void onClick(DialogInterface dialog,
611 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500612 if (mDontResend != null) {
613 mDontResend.sendToTarget();
614 mResend = null;
615 mDontResend = null;
616 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700617 }
618 }).setOnCancelListener(new OnCancelListener() {
619 public void onCancel(DialogInterface dialog) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500620 if (mDontResend != null) {
621 mDontResend.sendToTarget();
622 mResend = null;
623 mDontResend = null;
624 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700625 }
626 }).show();
627 }
628
629 /**
630 * Insert the url into the visited history database.
631 * @param url The url to be inserted.
632 * @param isReload True if this url is being reloaded.
633 * FIXME: Not sure what to do when reloading the page.
634 */
635 @Override
636 public void doUpdateVisitedHistory(WebView view, String url,
637 boolean isReload) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700638 mWebViewController.doUpdateVisitedHistory(Tab.this, url, isReload);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700639 }
640
641 /**
642 * Displays SSL error(s) dialog to the user.
643 */
644 @Override
645 public void onReceivedSslError(final WebView view,
646 final SslErrorHandler handler, final SslError error) {
647 if (!mInForeground) {
648 handler.cancel();
649 return;
650 }
651 if (BrowserSettings.getInstance().showSecurityWarnings()) {
652 final LayoutInflater factory =
653 LayoutInflater.from(mActivity);
654 final View warningsView =
655 factory.inflate(R.layout.ssl_warnings, null);
656 final LinearLayout placeholder =
657 (LinearLayout)warningsView.findViewById(R.id.placeholder);
658
659 if (error.hasError(SslError.SSL_UNTRUSTED)) {
660 LinearLayout ll = (LinearLayout)factory
661 .inflate(R.layout.ssl_warning, null);
662 ((TextView)ll.findViewById(R.id.warning))
663 .setText(R.string.ssl_untrusted);
664 placeholder.addView(ll);
665 }
666
667 if (error.hasError(SslError.SSL_IDMISMATCH)) {
668 LinearLayout ll = (LinearLayout)factory
669 .inflate(R.layout.ssl_warning, null);
670 ((TextView)ll.findViewById(R.id.warning))
671 .setText(R.string.ssl_mismatch);
672 placeholder.addView(ll);
673 }
674
675 if (error.hasError(SslError.SSL_EXPIRED)) {
676 LinearLayout ll = (LinearLayout)factory
677 .inflate(R.layout.ssl_warning, null);
678 ((TextView)ll.findViewById(R.id.warning))
679 .setText(R.string.ssl_expired);
680 placeholder.addView(ll);
681 }
682
683 if (error.hasError(SslError.SSL_NOTYETVALID)) {
684 LinearLayout ll = (LinearLayout)factory
685 .inflate(R.layout.ssl_warning, null);
686 ((TextView)ll.findViewById(R.id.warning))
687 .setText(R.string.ssl_not_yet_valid);
688 placeholder.addView(ll);
689 }
690
691 new AlertDialog.Builder(mActivity).setTitle(
692 R.string.security_warning).setIcon(
693 android.R.drawable.ic_dialog_alert).setView(
694 warningsView).setPositiveButton(R.string.ssl_continue,
695 new DialogInterface.OnClickListener() {
696 public void onClick(DialogInterface dialog,
697 int whichButton) {
698 handler.proceed();
699 }
700 }).setNeutralButton(R.string.view_certificate,
701 new DialogInterface.OnClickListener() {
702 public void onClick(DialogInterface dialog,
703 int whichButton) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700704 mWebViewController.showSslCertificateOnError(view,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700705 handler, error);
706 }
707 }).setNegativeButton(R.string.cancel,
708 new DialogInterface.OnClickListener() {
709 public void onClick(DialogInterface dialog,
710 int whichButton) {
711 handler.cancel();
Michael Kolb8233fac2010-10-26 16:08:53 -0700712 mWebViewController.resetTitleAndRevertLockIcon(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700713 }
714 }).setOnCancelListener(
715 new DialogInterface.OnCancelListener() {
716 public void onCancel(DialogInterface dialog) {
717 handler.cancel();
Michael Kolb8233fac2010-10-26 16:08:53 -0700718 mWebViewController.resetTitleAndRevertLockIcon(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700719 }
720 }).show();
721 } else {
722 handler.proceed();
723 }
724 }
725
726 /**
727 * Handles an HTTP authentication request.
728 *
729 * @param handler The authentication handler
730 * @param host The host
731 * @param realm The realm
732 */
733 @Override
734 public void onReceivedHttpAuthRequest(WebView view,
735 final HttpAuthHandler handler, final String host,
736 final String realm) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700737 mWebViewController.onReceivedHttpAuthRequest(Tab.this, view, handler, host, realm);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700738 }
739
740 @Override
741 public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
742 if (!mInForeground) {
743 return false;
744 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700745 return mWebViewController.shouldOverrideKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700746 }
747
748 @Override
749 public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700750 if (!mInForeground) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700751 return;
752 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700753 mWebViewController.onUnhandledKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700754 }
755 };
756
757 // -------------------------------------------------------------------------
758 // WebChromeClient implementation for the main WebView
759 // -------------------------------------------------------------------------
760
761 private final WebChromeClient mWebChromeClient = new WebChromeClient() {
762 // Helper method to create a new tab or sub window.
763 private void createWindow(final boolean dialog, final Message msg) {
764 WebView.WebViewTransport transport =
765 (WebView.WebViewTransport) msg.obj;
766 if (dialog) {
767 createSubWindow();
Michael Kolb8233fac2010-10-26 16:08:53 -0700768 mWebViewController.attachSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700769 transport.setWebView(mSubView);
770 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -0700771 final Tab newTab = mWebViewController.openTabAndShow(
772 IntentHandler.EMPTY_URL_DATA, false, null);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700773 if (newTab != Tab.this) {
774 Tab.this.addChildTab(newTab);
775 }
776 transport.setWebView(newTab.getWebView());
777 }
778 msg.sendToTarget();
779 }
780
781 @Override
782 public boolean onCreateWindow(WebView view, final boolean dialog,
783 final boolean userGesture, final Message resultMsg) {
784 // only allow new window or sub window for the foreground case
785 if (!mInForeground) {
786 return false;
787 }
788 // Short-circuit if we can't create any more tabs or sub windows.
789 if (dialog && mSubView != null) {
790 new AlertDialog.Builder(mActivity)
791 .setTitle(R.string.too_many_subwindows_dialog_title)
792 .setIcon(android.R.drawable.ic_dialog_alert)
793 .setMessage(R.string.too_many_subwindows_dialog_message)
794 .setPositiveButton(R.string.ok, null)
795 .show();
796 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700797 } else if (!mWebViewController.getTabControl().canCreateNewTab()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700798 new AlertDialog.Builder(mActivity)
799 .setTitle(R.string.too_many_windows_dialog_title)
800 .setIcon(android.R.drawable.ic_dialog_alert)
801 .setMessage(R.string.too_many_windows_dialog_message)
802 .setPositiveButton(R.string.ok, null)
803 .show();
804 return false;
805 }
806
807 // Short-circuit if this was a user gesture.
808 if (userGesture) {
809 createWindow(dialog, resultMsg);
810 return true;
811 }
812
813 // Allow the popup and create the appropriate window.
814 final AlertDialog.OnClickListener allowListener =
815 new AlertDialog.OnClickListener() {
816 public void onClick(DialogInterface d,
817 int which) {
818 createWindow(dialog, resultMsg);
819 }
820 };
821
822 // Block the popup by returning a null WebView.
823 final AlertDialog.OnClickListener blockListener =
824 new AlertDialog.OnClickListener() {
825 public void onClick(DialogInterface d, int which) {
826 resultMsg.sendToTarget();
827 }
828 };
829
830 // Build a confirmation dialog to display to the user.
831 final AlertDialog d =
832 new AlertDialog.Builder(mActivity)
833 .setTitle(R.string.attention)
834 .setIcon(android.R.drawable.ic_dialog_alert)
835 .setMessage(R.string.popup_window_attempt)
836 .setPositiveButton(R.string.allow, allowListener)
837 .setNegativeButton(R.string.block, blockListener)
838 .setCancelable(false)
839 .create();
840
841 // Show the confirmation dialog.
842 d.show();
843 return true;
844 }
845
846 @Override
Patrick Scotteb5061b2009-11-18 15:00:30 -0500847 public void onRequestFocus(WebView view) {
848 if (!mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700849 mWebViewController.switchToTab(mWebViewController.getTabControl().getTabIndex(
Patrick Scotteb5061b2009-11-18 15:00:30 -0500850 Tab.this));
851 }
852 }
853
854 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700855 public void onCloseWindow(WebView window) {
856 if (mParentTab != null) {
857 // JavaScript can only close popup window.
858 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700859 mWebViewController.switchToTab(mWebViewController.getTabControl()
Grace Kloba22ac16e2009-10-07 18:00:23 -0700860 .getTabIndex(mParentTab));
861 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700862 mWebViewController.closeTab(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700863 }
864 }
865
866 @Override
867 public void onProgressChanged(WebView view, int newProgress) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700868 mWebViewController.onProgressChanged(Tab.this, newProgress);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700869 }
870
871 @Override
Leon Scroggins21d9b902010-03-11 09:33:11 -0500872 public void onReceivedTitle(WebView view, final String title) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700873 mWebViewController.onReceivedTitle(Tab.this, title);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700874 }
875
876 @Override
877 public void onReceivedIcon(WebView view, Bitmap icon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700878 mWebViewController.onFavicon(Tab.this, view, icon);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700879 }
880
881 @Override
882 public void onReceivedTouchIconUrl(WebView view, String url,
883 boolean precomposed) {
884 final ContentResolver cr = mActivity.getContentResolver();
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400885 // Let precomposed icons take precedence over non-composed
886 // icons.
887 if (precomposed && mTouchIconLoader != null) {
888 mTouchIconLoader.cancel(false);
889 mTouchIconLoader = null;
890 }
891 // Have only one async task at a time.
892 if (mTouchIconLoader == null) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700893 mTouchIconLoader = new DownloadTouchIcon(Tab.this,
894 mActivity, cr, view);
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400895 mTouchIconLoader.execute(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700896 }
897 }
898
899 @Override
900 public void onShowCustomView(View view,
901 WebChromeClient.CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700902 if (mInForeground) mWebViewController.showCustomView(Tab.this, view,
903 callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700904 }
905
906 @Override
907 public void onHideCustomView() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700908 if (mInForeground) mWebViewController.hideCustomView();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700909 }
910
911 /**
912 * The origin has exceeded its database quota.
913 * @param url the URL that exceeded the quota
914 * @param databaseIdentifier the identifier of the database on which the
915 * transaction that caused the quota overflow was run
916 * @param currentQuota the current quota for the origin.
917 * @param estimatedSize the estimated size of the database.
918 * @param totalUsedQuota is the sum of all origins' quota.
919 * @param quotaUpdater The callback to run when a decision to allow or
920 * deny quota has been made. Don't forget to call this!
921 */
922 @Override
923 public void onExceededDatabaseQuota(String url,
924 String databaseIdentifier, long currentQuota, long estimatedSize,
925 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
926 BrowserSettings.getInstance().getWebStorageSizeManager()
927 .onExceededDatabaseQuota(url, databaseIdentifier,
928 currentQuota, estimatedSize, totalUsedQuota,
929 quotaUpdater);
930 }
931
932 /**
933 * The Application Cache has exceeded its max size.
934 * @param spaceNeeded is the amount of disk space that would be needed
935 * in order for the last appcache operation to succeed.
936 * @param totalUsedQuota is the sum of all origins' quota.
937 * @param quotaUpdater A callback to inform the WebCore thread that a
938 * new app cache size is available. This callback must always
939 * be executed at some point to ensure that the sleeping
940 * WebCore thread is woken up.
941 */
942 @Override
943 public void onReachedMaxAppCacheSize(long spaceNeeded,
944 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
945 BrowserSettings.getInstance().getWebStorageSizeManager()
946 .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota,
947 quotaUpdater);
948 }
949
950 /**
951 * Instructs the browser to show a prompt to ask the user to set the
952 * Geolocation permission state for the specified origin.
953 * @param origin The origin for which Geolocation permissions are
954 * requested.
955 * @param callback The callback to call once the user has set the
956 * Geolocation permission state.
957 */
958 @Override
959 public void onGeolocationPermissionsShowPrompt(String origin,
960 GeolocationPermissions.Callback callback) {
961 if (mInForeground) {
Grace Kloba50c241e2010-04-20 11:07:50 -0700962 getGeolocationPermissionsPrompt().show(origin, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700963 }
964 }
965
966 /**
967 * Instructs the browser to hide the Geolocation permissions prompt.
968 */
969 @Override
970 public void onGeolocationPermissionsHidePrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -0700971 if (mInForeground && mGeolocationPermissionsPrompt != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700972 mGeolocationPermissionsPrompt.hide();
973 }
974 }
975
Ben Murdoch65acc352009-11-19 18:16:04 +0000976 /* Adds a JavaScript error message to the system log and if the JS
977 * console is enabled in the about:debug options, to that console
978 * also.
Ben Murdochc42addf2010-01-28 15:19:59 +0000979 * @param consoleMessage the message object.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700980 */
981 @Override
Ben Murdochc42addf2010-01-28 15:19:59 +0000982 public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700983 if (mInForeground) {
984 // call getErrorConsole(true) so it will create one if needed
985 ErrorConsoleView errorConsole = getErrorConsole(true);
Ben Murdochc42addf2010-01-28 15:19:59 +0000986 errorConsole.addErrorMessage(consoleMessage);
Michael Kolb8233fac2010-10-26 16:08:53 -0700987 if (mWebViewController.shouldShowErrorConsole()
988 && errorConsole.getShowState() !=
989 ErrorConsoleView.SHOW_MAXIMIZED) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700990 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
991 }
992 }
Ben Murdochc42addf2010-01-28 15:19:59 +0000993
Jeff Hamilton47654f42010-09-07 09:57:51 -0500994 // Don't log console messages in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -0700995 if (isPrivateBrowsingEnabled()) return true;
Jeff Hamilton47654f42010-09-07 09:57:51 -0500996
Ben Murdochc42addf2010-01-28 15:19:59 +0000997 String message = "Console: " + consoleMessage.message() + " "
998 + consoleMessage.sourceId() + ":"
999 + consoleMessage.lineNumber();
1000
1001 switch (consoleMessage.messageLevel()) {
1002 case TIP:
1003 Log.v(CONSOLE_LOGTAG, message);
1004 break;
1005 case LOG:
1006 Log.i(CONSOLE_LOGTAG, message);
1007 break;
1008 case WARNING:
1009 Log.w(CONSOLE_LOGTAG, message);
1010 break;
1011 case ERROR:
1012 Log.e(CONSOLE_LOGTAG, message);
1013 break;
1014 case DEBUG:
1015 Log.d(CONSOLE_LOGTAG, message);
1016 break;
1017 }
1018
1019 return true;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001020 }
1021
1022 /**
1023 * Ask the browser for an icon to represent a <video> element.
1024 * This icon will be used if the Web page did not specify a poster attribute.
1025 * @return Bitmap The icon or null if no such icon is available.
1026 */
1027 @Override
1028 public Bitmap getDefaultVideoPoster() {
1029 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001030 return mWebViewController.getDefaultVideoPoster();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001031 }
1032 return null;
1033 }
1034
1035 /**
1036 * Ask the host application for a custom progress view to show while
1037 * a <video> is loading.
1038 * @return View The progress view.
1039 */
1040 @Override
1041 public View getVideoLoadingProgressView() {
1042 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001043 return mWebViewController.getVideoLoadingProgressView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001044 }
1045 return null;
1046 }
1047
1048 @Override
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01001049 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001050 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001051 mWebViewController.openFileChooser(uploadMsg, acceptType);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001052 } else {
1053 uploadMsg.onReceiveValue(null);
1054 }
1055 }
1056
1057 /**
1058 * Deliver a list of already-visited URLs
1059 */
1060 @Override
1061 public void getVisitedHistory(final ValueCallback<String[]> callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001062 mWebViewController.getVisitedHistory(callback);
1063 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001064 };
1065
1066 // -------------------------------------------------------------------------
1067 // WebViewClient implementation for the sub window
1068 // -------------------------------------------------------------------------
1069
1070 // Subclass of WebViewClient used in subwindows to notify the main
1071 // WebViewClient of certain WebView activities.
1072 private static class SubWindowClient extends WebViewClient {
1073 // The main WebViewClient.
1074 private final WebViewClient mClient;
Michael Kolb8233fac2010-10-26 16:08:53 -07001075 private final WebViewController mController;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001076
Michael Kolb8233fac2010-10-26 16:08:53 -07001077 SubWindowClient(WebViewClient client, WebViewController controller) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001078 mClient = client;
Michael Kolb8233fac2010-10-26 16:08:53 -07001079 mController = controller;
Leon Scroggins III211ba542010-04-19 13:21:13 -04001080 }
1081 @Override
1082 public void onPageStarted(WebView view, String url, Bitmap favicon) {
1083 // Unlike the others, do not call mClient's version, which would
1084 // change the progress bar. However, we do want to remove the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001085 // find or select dialog.
Michael Kolb8233fac2010-10-26 16:08:53 -07001086 mController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001087 }
1088 @Override
1089 public void doUpdateVisitedHistory(WebView view, String url,
1090 boolean isReload) {
1091 mClient.doUpdateVisitedHistory(view, url, isReload);
1092 }
1093 @Override
1094 public boolean shouldOverrideUrlLoading(WebView view, String url) {
1095 return mClient.shouldOverrideUrlLoading(view, url);
1096 }
1097 @Override
1098 public void onReceivedSslError(WebView view, SslErrorHandler handler,
1099 SslError error) {
1100 mClient.onReceivedSslError(view, handler, error);
1101 }
1102 @Override
1103 public void onReceivedHttpAuthRequest(WebView view,
1104 HttpAuthHandler handler, String host, String realm) {
1105 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
1106 }
1107 @Override
1108 public void onFormResubmission(WebView view, Message dontResend,
1109 Message resend) {
1110 mClient.onFormResubmission(view, dontResend, resend);
1111 }
1112 @Override
1113 public void onReceivedError(WebView view, int errorCode,
1114 String description, String failingUrl) {
1115 mClient.onReceivedError(view, errorCode, description, failingUrl);
1116 }
1117 @Override
1118 public boolean shouldOverrideKeyEvent(WebView view,
1119 android.view.KeyEvent event) {
1120 return mClient.shouldOverrideKeyEvent(view, event);
1121 }
1122 @Override
1123 public void onUnhandledKeyEvent(WebView view,
1124 android.view.KeyEvent event) {
1125 mClient.onUnhandledKeyEvent(view, event);
1126 }
1127 }
1128
1129 // -------------------------------------------------------------------------
1130 // WebChromeClient implementation for the sub window
1131 // -------------------------------------------------------------------------
1132
1133 private class SubWindowChromeClient extends WebChromeClient {
1134 // The main WebChromeClient.
1135 private final WebChromeClient mClient;
1136
1137 SubWindowChromeClient(WebChromeClient client) {
1138 mClient = client;
1139 }
1140 @Override
1141 public void onProgressChanged(WebView view, int newProgress) {
1142 mClient.onProgressChanged(view, newProgress);
1143 }
1144 @Override
1145 public boolean onCreateWindow(WebView view, boolean dialog,
1146 boolean userGesture, android.os.Message resultMsg) {
1147 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
1148 }
1149 @Override
1150 public void onCloseWindow(WebView window) {
1151 if (window != mSubView) {
1152 Log.e(LOGTAG, "Can't close the window");
1153 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001154 mWebViewController.dismissSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001155 }
1156 }
1157
1158 // -------------------------------------------------------------------------
1159
Michael Kolb8233fac2010-10-26 16:08:53 -07001160 // TODO temporarily use activity here
1161 // remove later
1162
Grace Kloba22ac16e2009-10-07 18:00:23 -07001163 // Construct a new tab
Michael Kolb8233fac2010-10-26 16:08:53 -07001164 Tab(WebViewController wvcontroller, WebView w, boolean closeOnExit, String appId,
Grace Kloba22ac16e2009-10-07 18:00:23 -07001165 String url) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001166 mWebViewController = wvcontroller;
1167 mActivity = mWebViewController.getActivity();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001168 mCloseOnExit = closeOnExit;
1169 mAppId = appId;
1170 mOriginalUrl = url;
Michael Kolb8233fac2010-10-26 16:08:53 -07001171 mLockIconType = LOCK_ICON_UNSECURE;
1172 mPrevLockIconType = LOCK_ICON_UNSECURE;
1173 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001174 mInForeground = false;
1175
Michael Kolb8233fac2010-10-26 16:08:53 -07001176 mInflateService = LayoutInflater.from(mActivity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001177
1178 // The tab consists of a container view, which contains the main
1179 // WebView, as well as any other UI elements associated with the tab.
Leon Scroggins III211ba542010-04-19 13:21:13 -04001180 mContainer = (LinearLayout) mInflateService.inflate(R.layout.tab, null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001181
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001182 mDownloadListener = new DownloadListener() {
1183 public void onDownloadStart(String url, String userAgent,
1184 String contentDisposition, String mimetype,
1185 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001186 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001187 mimetype, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001188 }
1189 };
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001190 mWebBackForwardListClient = new WebBackForwardListClient() {
1191 @Override
1192 public void onNewHistoryItem(WebHistoryItem item) {
1193 if (isInVoiceSearchMode()) {
1194 item.setCustomData(mVoiceSearchData.mVoiceSearchIntent);
1195 }
1196 }
1197 @Override
1198 public void onIndexChanged(WebHistoryItem item, int index) {
1199 Object data = item.getCustomData();
1200 if (data != null && data instanceof Intent) {
1201 activateVoiceSearchMode((Intent) data);
1202 }
1203 }
1204 };
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001205
Grace Kloba22ac16e2009-10-07 18:00:23 -07001206 setWebView(w);
1207 }
1208
1209 /**
1210 * Sets the WebView for this tab, correctly removing the old WebView from
1211 * the container view.
1212 */
1213 void setWebView(WebView w) {
1214 if (mMainView == w) {
1215 return;
1216 }
1217 // If the WebView is changing, the page will be reloaded, so any ongoing
1218 // Geolocation permission requests are void.
Grace Kloba50c241e2010-04-20 11:07:50 -07001219 if (mGeolocationPermissionsPrompt != null) {
1220 mGeolocationPermissionsPrompt.hide();
1221 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001222
1223 // Just remove the old one.
1224 FrameLayout wrapper =
1225 (FrameLayout) mContainer.findViewById(R.id.webview_wrapper);
1226 wrapper.removeView(mMainView);
1227
1228 // set the new one
1229 mMainView = w;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001230 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001231 if (mMainView != null) {
1232 mMainView.setWebViewClient(mWebViewClient);
1233 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001234 // Attach DownloadManager so that downloads can start in an active
1235 // or a non-active window. This can happen when going to a site that
1236 // does a redirect after a period of time. The user could have
1237 // switched to another tab while waiting for the download to start.
1238 mMainView.setDownloadListener(mDownloadListener);
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001239 mMainView.setWebBackForwardListClient(mWebBackForwardListClient);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001240 }
1241 }
1242
1243 /**
1244 * Destroy the tab's main WebView and subWindow if any
1245 */
1246 void destroy() {
1247 if (mMainView != null) {
1248 dismissSubWindow();
1249 BrowserSettings.getInstance().deleteObserver(mMainView.getSettings());
1250 // save the WebView to call destroy() after detach it from the tab
1251 WebView webView = mMainView;
1252 setWebView(null);
1253 webView.destroy();
1254 }
1255 }
1256
1257 /**
1258 * Remove the tab from the parent
1259 */
1260 void removeFromTree() {
1261 // detach the children
1262 if (mChildTabs != null) {
1263 for(Tab t : mChildTabs) {
1264 t.setParentTab(null);
1265 }
1266 }
1267 // remove itself from the parent list
1268 if (mParentTab != null) {
1269 mParentTab.mChildTabs.remove(this);
1270 }
1271 }
1272
1273 /**
1274 * Create a new subwindow unless a subwindow already exists.
1275 * @return True if a new subwindow was created. False if one already exists.
1276 */
1277 boolean createSubWindow() {
1278 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001279 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001280 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001281 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001282 mSubView.setWebChromeClient(new SubWindowChromeClient(
1283 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001284 // Set a different DownloadListener for the mSubView, since it will
1285 // just need to dismiss the mSubView, rather than close the Tab
1286 mSubView.setDownloadListener(new DownloadListener() {
1287 public void onDownloadStart(String url, String userAgent,
1288 String contentDisposition, String mimetype,
1289 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001290 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001291 contentDisposition, mimetype, contentLength);
1292 if (mSubView.copyBackForwardList().getSize() == 0) {
1293 // This subwindow was opened for the sole purpose of
1294 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001295 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001296 }
1297 }
1298 });
Grace Kloba22ac16e2009-10-07 18:00:23 -07001299 mSubView.setOnCreateContextMenuListener(mActivity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001300 return true;
1301 }
1302 return false;
1303 }
1304
1305 /**
1306 * Dismiss the subWindow for the tab.
1307 */
1308 void dismissSubWindow() {
1309 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001310 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001311 BrowserSettings.getInstance().deleteObserver(
1312 mSubView.getSettings());
1313 mSubView.destroy();
1314 mSubView = null;
1315 mSubViewContainer = null;
1316 }
1317 }
1318
Grace Kloba22ac16e2009-10-07 18:00:23 -07001319
1320 /**
1321 * Set the parent tab of this tab.
1322 */
1323 void setParentTab(Tab parent) {
1324 mParentTab = parent;
1325 // This tab may have been freed due to low memory. If that is the case,
1326 // the parent tab index is already saved. If we are changing that index
1327 // (most likely due to removing the parent tab) we must update the
1328 // parent tab index in the saved Bundle.
1329 if (mSavedState != null) {
1330 if (parent == null) {
1331 mSavedState.remove(PARENTTAB);
1332 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -07001333 mSavedState.putInt(PARENTTAB, mWebViewController.getTabControl()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001334 .getTabIndex(parent));
1335 }
1336 }
1337 }
1338
1339 /**
1340 * When a Tab is created through the content of another Tab, then we
1341 * associate the Tabs.
1342 * @param child the Tab that was created from this Tab
1343 */
1344 void addChildTab(Tab child) {
1345 if (mChildTabs == null) {
1346 mChildTabs = new Vector<Tab>();
1347 }
1348 mChildTabs.add(child);
1349 child.setParentTab(this);
1350 }
1351
1352 Vector<Tab> getChildTabs() {
1353 return mChildTabs;
1354 }
1355
1356 void resume() {
1357 if (mMainView != null) {
1358 mMainView.onResume();
1359 if (mSubView != null) {
1360 mSubView.onResume();
1361 }
1362 }
1363 }
1364
1365 void pause() {
1366 if (mMainView != null) {
1367 mMainView.onPause();
1368 if (mSubView != null) {
1369 mSubView.onPause();
1370 }
1371 }
1372 }
1373
1374 void putInForeground() {
1375 mInForeground = true;
1376 resume();
1377 mMainView.setOnCreateContextMenuListener(mActivity);
1378 if (mSubView != null) {
1379 mSubView.setOnCreateContextMenuListener(mActivity);
1380 }
1381 // Show the pending error dialog if the queue is not empty
1382 if (mQueuedErrors != null && mQueuedErrors.size() > 0) {
1383 showError(mQueuedErrors.getFirst());
1384 }
1385 }
1386
1387 void putInBackground() {
1388 mInForeground = false;
1389 pause();
1390 mMainView.setOnCreateContextMenuListener(null);
1391 if (mSubView != null) {
1392 mSubView.setOnCreateContextMenuListener(null);
1393 }
1394 }
1395
Michael Kolb8233fac2010-10-26 16:08:53 -07001396 boolean inForeground() {
1397 return mInForeground;
1398 }
1399
Grace Kloba22ac16e2009-10-07 18:00:23 -07001400 /**
1401 * Return the top window of this tab; either the subwindow if it is not
1402 * null or the main window.
1403 * @return The top window of this tab.
1404 */
1405 WebView getTopWindow() {
1406 if (mSubView != null) {
1407 return mSubView;
1408 }
1409 return mMainView;
1410 }
1411
1412 /**
1413 * Return the main window of this tab. Note: if a tab is freed in the
1414 * background, this can return null. It is only guaranteed to be
1415 * non-null for the current tab.
1416 * @return The main WebView of this tab.
1417 */
1418 WebView getWebView() {
1419 return mMainView;
1420 }
1421
Michael Kolb8233fac2010-10-26 16:08:53 -07001422 View getViewContainer() {
1423 return mContainer;
1424 }
1425
Grace Kloba22ac16e2009-10-07 18:00:23 -07001426 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001427 * Return whether private browsing is enabled for the main window of
1428 * this tab.
1429 * @return True if private browsing is enabled.
1430 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001431 boolean isPrivateBrowsingEnabled() {
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001432 WebView webView = getWebView();
1433 if (webView == null) {
1434 return false;
1435 }
1436 return webView.isPrivateBrowsingEnabled();
1437 }
1438
1439 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001440 * Return the subwindow of this tab or null if there is no subwindow.
1441 * @return The subwindow of this tab or null.
1442 */
1443 WebView getSubWebView() {
1444 return mSubView;
1445 }
1446
Michael Kolb1514bb72010-11-22 09:11:48 -08001447 void setSubWebView(WebView subView) {
1448 mSubView = subView;
1449 }
1450
Michael Kolb8233fac2010-10-26 16:08:53 -07001451 View getSubViewContainer() {
1452 return mSubViewContainer;
1453 }
1454
Michael Kolb1514bb72010-11-22 09:11:48 -08001455 void setSubViewContainer(View subViewContainer) {
1456 mSubViewContainer = subViewContainer;
1457 }
1458
Grace Kloba22ac16e2009-10-07 18:00:23 -07001459 /**
1460 * @return The geolocation permissions prompt for this tab.
1461 */
1462 GeolocationPermissionsPrompt getGeolocationPermissionsPrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001463 if (mGeolocationPermissionsPrompt == null) {
1464 ViewStub stub = (ViewStub) mContainer
1465 .findViewById(R.id.geolocation_permissions_prompt);
1466 mGeolocationPermissionsPrompt = (GeolocationPermissionsPrompt) stub
1467 .inflate();
1468 mGeolocationPermissionsPrompt.init();
1469 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001470 return mGeolocationPermissionsPrompt;
1471 }
1472
1473 /**
1474 * @return The application id string
1475 */
1476 String getAppId() {
1477 return mAppId;
1478 }
1479
1480 /**
1481 * Set the application id string
1482 * @param id
1483 */
1484 void setAppId(String id) {
1485 mAppId = id;
1486 }
1487
1488 /**
1489 * @return The original url associated with this Tab
1490 */
1491 String getOriginalUrl() {
1492 return mOriginalUrl;
1493 }
1494
1495 /**
1496 * Set the original url associated with this tab
1497 */
1498 void setOriginalUrl(String url) {
1499 mOriginalUrl = url;
1500 }
1501
1502 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07001503 * set the title for the tab
1504 */
1505 void setCurrentTitle(String title) {
1506 mCurrentTitle = title;
1507 }
1508
1509 /**
1510 * set url for this tab
1511 * @param url
1512 */
1513 void setCurrentUrl(String url) {
1514 mCurrentUrl = url;
1515 }
1516
1517 String getCurrentTitle() {
1518 return mCurrentTitle;
1519 }
1520
1521 String getCurrentUrl() {
1522 return mCurrentUrl;
1523 }
1524 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001525 * Get the url of this tab. Valid after calling populatePickerData, but
1526 * before calling wipePickerData, or if the webview has been destroyed.
1527 * @return The WebView's url or null.
1528 */
1529 String getUrl() {
1530 if (mPickerData != null) {
1531 return mPickerData.mUrl;
1532 }
1533 return null;
1534 }
1535
1536 /**
1537 * Get the title of this tab. Valid after calling populatePickerData, but
1538 * before calling wipePickerData, or if the webview has been destroyed. If
1539 * the url has no title, use the url instead.
1540 * @return The WebView's title (or url) or null.
1541 */
1542 String getTitle() {
1543 if (mPickerData != null) {
1544 return mPickerData.mTitle;
1545 }
1546 return null;
1547 }
1548
1549 /**
1550 * Get the favicon of this tab. Valid after calling populatePickerData, but
1551 * before calling wipePickerData, or if the webview has been destroyed.
1552 * @return The WebView's favicon or null.
1553 */
1554 Bitmap getFavicon() {
1555 if (mPickerData != null) {
1556 return mPickerData.mFavicon;
1557 }
1558 return null;
1559 }
1560
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001561
Grace Kloba22ac16e2009-10-07 18:00:23 -07001562 /**
1563 * Return the tab's error console. Creates the console if createIfNEcessary
1564 * is true and we haven't already created the console.
1565 * @param createIfNecessary Flag to indicate if the console should be
1566 * created if it has not been already.
1567 * @return The tab's error console, or null if one has not been created and
1568 * createIfNecessary is false.
1569 */
1570 ErrorConsoleView getErrorConsole(boolean createIfNecessary) {
1571 if (createIfNecessary && mErrorConsole == null) {
1572 mErrorConsole = new ErrorConsoleView(mActivity);
1573 mErrorConsole.setWebView(mMainView);
1574 }
1575 return mErrorConsole;
1576 }
1577
1578 /**
1579 * If this Tab was created through another Tab, then this method returns
1580 * that Tab.
1581 * @return the Tab parent or null
1582 */
1583 public Tab getParentTab() {
1584 return mParentTab;
1585 }
1586
1587 /**
1588 * Return whether this tab should be closed when it is backing out of the
1589 * first page.
1590 * @return TRUE if this tab should be closed when exit.
1591 */
1592 boolean closeOnExit() {
1593 return mCloseOnExit;
1594 }
1595
1596 /**
1597 * Saves the current lock-icon state before resetting the lock icon. If we
1598 * have an error, we may need to roll back to the previous state.
1599 */
1600 void resetLockIcon(String url) {
1601 mPrevLockIconType = mLockIconType;
Michael Kolb8233fac2010-10-26 16:08:53 -07001602 mLockIconType = LOCK_ICON_UNSECURE;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001603 if (URLUtil.isHttpsUrl(url)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001604 mLockIconType = LOCK_ICON_SECURE;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001605 }
1606 }
1607
1608 /**
1609 * Reverts the lock-icon state to the last saved state, for example, if we
1610 * had an error, and need to cancel the load.
1611 */
1612 void revertLockIcon() {
1613 mLockIconType = mPrevLockIconType;
1614 }
1615
1616 /**
1617 * @return The tab's lock icon type.
1618 */
1619 int getLockIconType() {
1620 return mLockIconType;
1621 }
1622
1623 /**
1624 * @return TRUE if onPageStarted is called while onPageFinished is not
1625 * called yet.
1626 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001627 boolean inPageLoad() {
1628 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001629 }
1630
1631 // force mInLoad to be false. This should only be called before closing the
1632 // tab to ensure BrowserActivity's pauseWebViewTimers() is called correctly.
Michael Kolb8233fac2010-10-26 16:08:53 -07001633 void clearInPageLoad() {
1634 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001635 }
1636
1637 void populatePickerData() {
1638 if (mMainView == null) {
1639 populatePickerDataFromSavedState();
1640 return;
1641 }
1642
1643 // FIXME: The only place we cared about subwindow was for
1644 // bookmarking (i.e. not when saving state). Was this deliberate?
1645 final WebBackForwardList list = mMainView.copyBackForwardList();
Leon Scroggins70a153b2010-05-10 17:27:26 -04001646 if (list == null) {
1647 Log.w(LOGTAG, "populatePickerData called and WebBackForwardList is null");
1648 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001649 final WebHistoryItem item = list != null ? list.getCurrentItem() : null;
1650 populatePickerData(item);
1651 }
1652
1653 // Populate the picker data using the given history item and the current top
1654 // WebView.
1655 private void populatePickerData(WebHistoryItem item) {
1656 mPickerData = new PickerData();
Leon Scroggins70a153b2010-05-10 17:27:26 -04001657 if (item == null) {
1658 Log.w(LOGTAG, "populatePickerData called with a null WebHistoryItem");
1659 } else {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001660 mPickerData.mUrl = item.getUrl();
1661 mPickerData.mTitle = item.getTitle();
1662 mPickerData.mFavicon = item.getFavicon();
1663 if (mPickerData.mTitle == null) {
1664 mPickerData.mTitle = mPickerData.mUrl;
1665 }
1666 }
1667 }
1668
1669 // Create the PickerData and populate it using the saved state of the tab.
1670 void populatePickerDataFromSavedState() {
1671 if (mSavedState == null) {
1672 return;
1673 }
1674 mPickerData = new PickerData();
1675 mPickerData.mUrl = mSavedState.getString(CURRURL);
1676 mPickerData.mTitle = mSavedState.getString(CURRTITLE);
1677 }
1678
1679 void clearPickerData() {
1680 mPickerData = null;
1681 }
1682
1683 /**
1684 * Get the saved state bundle.
1685 * @return
1686 */
1687 Bundle getSavedState() {
1688 return mSavedState;
1689 }
1690
1691 /**
1692 * Set the saved state.
1693 */
1694 void setSavedState(Bundle state) {
1695 mSavedState = state;
1696 }
1697
1698 /**
1699 * @return TRUE if succeed in saving the state.
1700 */
1701 boolean saveState() {
1702 // If the WebView is null it means we ran low on memory and we already
1703 // stored the saved state in mSavedState.
1704 if (mMainView == null) {
1705 return mSavedState != null;
1706 }
1707
1708 mSavedState = new Bundle();
1709 final WebBackForwardList list = mMainView.saveState(mSavedState);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001710
1711 // Store some extra info for displaying the tab in the picker.
1712 final WebHistoryItem item = list != null ? list.getCurrentItem() : null;
1713 populatePickerData(item);
1714
1715 if (mPickerData.mUrl != null) {
1716 mSavedState.putString(CURRURL, mPickerData.mUrl);
1717 }
1718 if (mPickerData.mTitle != null) {
1719 mSavedState.putString(CURRTITLE, mPickerData.mTitle);
1720 }
1721 mSavedState.putBoolean(CLOSEONEXIT, mCloseOnExit);
1722 if (mAppId != null) {
1723 mSavedState.putString(APPID, mAppId);
1724 }
1725 if (mOriginalUrl != null) {
1726 mSavedState.putString(ORIGINALURL, mOriginalUrl);
1727 }
1728 // Remember the parent tab so the relationship can be restored.
1729 if (mParentTab != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001730 mSavedState.putInt(PARENTTAB, mWebViewController.getTabControl().getTabIndex(
Grace Kloba22ac16e2009-10-07 18:00:23 -07001731 mParentTab));
1732 }
1733 return true;
1734 }
1735
1736 /*
1737 * Restore the state of the tab.
1738 */
1739 boolean restoreState(Bundle b) {
1740 if (b == null) {
1741 return false;
1742 }
1743 // Restore the internal state even if the WebView fails to restore.
1744 // This will maintain the app id, original url and close-on-exit values.
1745 mSavedState = null;
1746 mPickerData = null;
1747 mCloseOnExit = b.getBoolean(CLOSEONEXIT);
1748 mAppId = b.getString(APPID);
1749 mOriginalUrl = b.getString(ORIGINALURL);
1750
1751 final WebBackForwardList list = mMainView.restoreState(b);
1752 if (list == null) {
1753 return false;
1754 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001755 return true;
1756 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04001757
Grace Kloba22ac16e2009-10-07 18:00:23 -07001758}