blob: 3647a2057d239c31a61823d23f8c0c105f928cbf [file] [log] [blame]
Grace Kloba22ac16e2009-10-07 18:00:23 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.browser;
18
Jeff Hamilton8ce956c2010-08-17 11:13:53 -050019import com.android.common.speech.LoggingEvents;
20
Michael Kolb8233fac2010-10-26 16:08:53 -070021import android.app.Activity;
Grace Kloba22ac16e2009-10-07 18:00:23 -070022import android.app.AlertDialog;
Leon Scroggins58d56c62010-01-28 15:12:40 -050023import android.app.SearchManager;
Grace Kloba22ac16e2009-10-07 18:00:23 -070024import android.content.ContentResolver;
Grace Kloba22ac16e2009-10-07 18:00:23 -070025import android.content.DialogInterface;
Michael Kolbfe251992010-07-08 15:41:55 -070026import android.content.DialogInterface.OnCancelListener;
Jeff Hamilton8ce956c2010-08-17 11:13:53 -050027import android.content.Intent;
Grace Kloba22ac16e2009-10-07 18:00:23 -070028import android.graphics.Bitmap;
29import android.net.Uri;
30import android.net.http.SslError;
Grace Kloba22ac16e2009-10-07 18:00:23 -070031import android.os.Bundle;
32import android.os.Message;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +000033import android.os.SystemClock;
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -050034import android.speech.RecognizerResultsIntent;
Grace Kloba22ac16e2009-10-07 18:00:23 -070035import android.util.Log;
36import android.view.KeyEvent;
37import android.view.LayoutInflater;
38import android.view.View;
Grace Kloba50c241e2010-04-20 11:07:50 -070039import android.view.ViewStub;
Ben Murdochc42addf2010-01-28 15:19:59 +000040import android.webkit.ConsoleMessage;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -050041import android.webkit.DownloadListener;
Grace Kloba22ac16e2009-10-07 18:00:23 -070042import android.webkit.GeolocationPermissions;
43import android.webkit.HttpAuthHandler;
44import android.webkit.SslErrorHandler;
45import android.webkit.URLUtil;
46import android.webkit.ValueCallback;
47import android.webkit.WebBackForwardList;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -050048import android.webkit.WebBackForwardListClient;
Grace Kloba22ac16e2009-10-07 18:00:23 -070049import android.webkit.WebChromeClient;
50import android.webkit.WebHistoryItem;
Grace Kloba22ac16e2009-10-07 18:00:23 -070051import android.webkit.WebStorage;
52import android.webkit.WebView;
53import android.webkit.WebViewClient;
54import android.widget.FrameLayout;
Grace Kloba22ac16e2009-10-07 18:00:23 -070055import android.widget.LinearLayout;
56import android.widget.TextView;
Ben Murdoch8029a772010-11-16 11:58:21 +000057import android.widget.Toast;
Grace Kloba22ac16e2009-10-07 18:00:23 -070058
Michael Kolbfe251992010-07-08 15:41:55 -070059import java.util.ArrayList;
60import java.util.HashMap;
61import java.util.Iterator;
62import java.util.LinkedList;
63import java.util.Map;
64import java.util.Vector;
65
Grace Kloba22ac16e2009-10-07 18:00:23 -070066/**
67 * Class for maintaining Tabs with a main WebView and a subwindow.
68 */
69class Tab {
Michael Kolb8233fac2010-10-26 16:08:53 -070070
Grace Kloba22ac16e2009-10-07 18:00:23 -070071 // Log Tag
72 private static final String LOGTAG = "Tab";
Ben Murdochc42addf2010-01-28 15:19:59 +000073 // Special case the logtag for messages for the Console to make it easier to
74 // filter them and match the logtag used for these messages in older versions
75 // of the browser.
76 private static final String CONSOLE_LOGTAG = "browser";
77
Michael Kolb8233fac2010-10-26 16:08:53 -070078 final static int LOCK_ICON_UNSECURE = 0;
79 final static int LOCK_ICON_SECURE = 1;
80 final static int LOCK_ICON_MIXED = 2;
81
82 Activity mActivity;
83 private WebViewController mWebViewController;
84
Grace Kloba22ac16e2009-10-07 18:00:23 -070085 // The Geolocation permissions prompt
86 private GeolocationPermissionsPrompt mGeolocationPermissionsPrompt;
87 // Main WebView wrapper
Leon Scroggins III211ba542010-04-19 13:21:13 -040088 private LinearLayout mContainer;
Grace Kloba22ac16e2009-10-07 18:00:23 -070089 // Main WebView
90 private WebView mMainView;
91 // Subwindow container
92 private View mSubViewContainer;
93 // Subwindow WebView
94 private WebView mSubView;
95 // Saved bundle for when we are running low on memory. It contains the
96 // information needed to restore the WebView if the user goes back to the
97 // tab.
98 private Bundle mSavedState;
99 // Data used when displaying the tab in the picker.
100 private PickerData mPickerData;
101 // Parent Tab. This is the Tab that created this Tab, or null if the Tab was
102 // created by the UI
103 private Tab mParentTab;
104 // Tab that constructed by this Tab. This is used when this Tab is
105 // destroyed, it clears all mParentTab values in the children.
106 private Vector<Tab> mChildTabs;
107 // If true, the tab will be removed when back out of the first page.
108 private boolean mCloseOnExit;
109 // If true, the tab is in the foreground of the current activity.
110 private boolean mInForeground;
Michael Kolb8233fac2010-10-26 16:08:53 -0700111 // If true, the tab is in page loading state (after onPageStarted,
112 // before onPageFinsihed)
113 private boolean mInPageLoad;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000114 // The time the load started, used to find load page time
115 private long mLoadStartTime;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700116 // Application identifier used to find tabs that another application wants
117 // to reuse.
118 private String mAppId;
119 // Keep the original url around to avoid killing the old WebView if the url
120 // has not changed.
121 private String mOriginalUrl;
Michael Kolb8233fac2010-10-26 16:08:53 -0700122 // Hold on to the currently loaded url
123 private String mCurrentUrl;
124 //The currently loaded title
125 private String mCurrentTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700126 // Error console for the tab
127 private ErrorConsoleView mErrorConsole;
128 // the lock icon type and previous lock icon type for the tab
129 private int mLockIconType;
130 private int mPrevLockIconType;
131 // Inflation service for making subwindows.
132 private final LayoutInflater mInflateService;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -0500133 // The listener that gets invoked when a download is started from the
134 // mMainView
135 private final DownloadListener mDownloadListener;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500136 // Listener used to know when we move forward or back in the history list.
137 private final WebBackForwardListClient mWebBackForwardListClient;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700138
139 // AsyncTask for downloading touch icons
140 DownloadTouchIcon mTouchIconLoader;
141
142 // Extra saved information for displaying the tab in the picker.
143 private static class PickerData {
144 String mUrl;
145 String mTitle;
146 Bitmap mFavicon;
147 }
148
149 // Used for saving and restoring each Tab
150 static final String WEBVIEW = "webview";
151 static final String NUMTABS = "numTabs";
152 static final String CURRTAB = "currentTab";
153 static final String CURRURL = "currentUrl";
154 static final String CURRTITLE = "currentTitle";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700155 static final String CLOSEONEXIT = "closeonexit";
156 static final String PARENTTAB = "parentTab";
157 static final String APPID = "appid";
158 static final String ORIGINALURL = "originalUrl";
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700159 static final String INCOGNITO = "privateBrowsingEnabled";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700160
161 // -------------------------------------------------------------------------
162
Leon Scroggins58d56c62010-01-28 15:12:40 -0500163 /**
164 * Private information regarding the latest voice search. If the Tab is not
165 * in voice search mode, this will be null.
166 */
167 private VoiceSearchData mVoiceSearchData;
168 /**
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400169 * Remove voice search mode from this tab.
170 */
171 public void revertVoiceSearchMode() {
172 if (mVoiceSearchData != null) {
173 mVoiceSearchData = null;
174 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700175 mWebViewController.revertVoiceSearchMode(this);
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400176 }
177 }
178 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700179
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400180 /**
Leon Scroggins58d56c62010-01-28 15:12:40 -0500181 * Return whether the tab is in voice search mode.
182 */
183 public boolean isInVoiceSearchMode() {
184 return mVoiceSearchData != null;
185 }
186 /**
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400187 * Return true if the Tab is in voice search mode and the voice search
188 * Intent came with a String identifying that Google provided the Intent.
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500189 */
190 public boolean voiceSearchSourceIsGoogle() {
191 return mVoiceSearchData != null && mVoiceSearchData.mSourceIsGoogle;
192 }
193 /**
Leon Scroggins58d56c62010-01-28 15:12:40 -0500194 * Get the title to display for the current voice search page. If the Tab
195 * is not in voice search mode, return null.
196 */
197 public String getVoiceDisplayTitle() {
198 if (mVoiceSearchData == null) return null;
199 return mVoiceSearchData.mLastVoiceSearchTitle;
200 }
201 /**
202 * Get the latest array of voice search results, to be passed to the
203 * BrowserProvider. If the Tab is not in voice search mode, return null.
204 */
205 public ArrayList<String> getVoiceSearchResults() {
206 if (mVoiceSearchData == null) return null;
207 return mVoiceSearchData.mVoiceSearchResults;
208 }
209 /**
210 * Activate voice search mode.
211 * @param intent Intent which has the results to use, or an index into the
212 * results when reusing the old results.
213 */
214 /* package */ void activateVoiceSearchMode(Intent intent) {
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500215 int index = 0;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500216 ArrayList<String> results = intent.getStringArrayListExtra(
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -0500217 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_STRINGS);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500218 if (results != null) {
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500219 ArrayList<String> urls = intent.getStringArrayListExtra(
220 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_URLS);
221 ArrayList<String> htmls = intent.getStringArrayListExtra(
222 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_HTML);
223 ArrayList<String> baseUrls = intent.getStringArrayListExtra(
224 RecognizerResultsIntent
225 .EXTRA_VOICE_SEARCH_RESULT_HTML_BASE_URLS);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500226 // This tab is now entering voice search mode for the first time, or
227 // a new voice search was done.
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500228 int size = results.size();
229 if (urls == null || size != urls.size()) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500230 throw new AssertionError("improper extras passed in Intent");
231 }
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500232 if (htmls == null || htmls.size() != size || baseUrls == null ||
233 (baseUrls.size() != size && baseUrls.size() != 1)) {
234 // If either of these arrays are empty/incorrectly sized, ignore
235 // them.
236 htmls = null;
237 baseUrls = null;
238 }
239 mVoiceSearchData = new VoiceSearchData(results, urls, htmls,
240 baseUrls);
Leon Scroggins9df94972010-03-08 18:20:35 -0500241 mVoiceSearchData.mHeaders = intent.getParcelableArrayListExtra(
242 RecognizerResultsIntent
243 .EXTRA_VOICE_SEARCH_RESULT_HTTP_HEADERS);
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500244 mVoiceSearchData.mSourceIsGoogle = intent.getBooleanExtra(
245 VoiceSearchData.SOURCE_IS_GOOGLE, false);
Leon Scroggins2ee4a5a2010-03-15 16:56:57 -0400246 mVoiceSearchData.mVoiceSearchIntent = new Intent(intent);
Leon Scrogginse10dde52010-03-08 19:53:03 -0500247 }
248 String extraData = intent.getStringExtra(
249 SearchManager.EXTRA_DATA_KEY);
250 if (extraData != null) {
251 index = Integer.parseInt(extraData);
252 if (index >= mVoiceSearchData.mVoiceSearchResults.size()) {
253 throw new AssertionError("index must be less than "
254 + "size of mVoiceSearchResults");
255 }
256 if (mVoiceSearchData.mSourceIsGoogle) {
257 Intent logIntent = new Intent(
258 LoggingEvents.ACTION_LOG_EVENT);
259 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
260 LoggingEvents.VoiceSearch.N_BEST_CHOOSE);
261 logIntent.putExtra(
262 LoggingEvents.VoiceSearch.EXTRA_N_BEST_CHOOSE_INDEX,
263 index);
264 mActivity.sendBroadcast(logIntent);
265 }
266 if (mVoiceSearchData.mVoiceSearchIntent != null) {
Leon Scroggins2ee4a5a2010-03-15 16:56:57 -0400267 // Copy the Intent, so that each history item will have its own
268 // Intent, with different (or none) extra data.
269 Intent latest = new Intent(mVoiceSearchData.mVoiceSearchIntent);
270 latest.putExtra(SearchManager.EXTRA_DATA_KEY, extraData);
271 mVoiceSearchData.mVoiceSearchIntent = latest;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500272 }
273 }
274 mVoiceSearchData.mLastVoiceSearchTitle
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500275 = mVoiceSearchData.mVoiceSearchResults.get(index);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500276 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700277 mWebViewController.activateVoiceSearchMode(mVoiceSearchData.mLastVoiceSearchTitle);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500278 }
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500279 if (mVoiceSearchData.mVoiceSearchHtmls != null) {
280 // When index was found it was already ensured that it was valid
281 String uriString = mVoiceSearchData.mVoiceSearchHtmls.get(index);
282 if (uriString != null) {
283 Uri dataUri = Uri.parse(uriString);
284 if (RecognizerResultsIntent.URI_SCHEME_INLINE.equals(
285 dataUri.getScheme())) {
286 // If there is only one base URL, use it. If there are
287 // more, there will be one for each index, so use the base
288 // URL corresponding to the index.
289 String baseUrl = mVoiceSearchData.mVoiceSearchBaseUrls.get(
290 mVoiceSearchData.mVoiceSearchBaseUrls.size() > 1 ?
291 index : 0);
292 mVoiceSearchData.mLastVoiceSearchUrl = baseUrl;
293 mMainView.loadDataWithBaseURL(baseUrl,
294 uriString.substring(RecognizerResultsIntent
295 .URI_SCHEME_INLINE.length() + 1), "text/html",
296 "utf-8", baseUrl);
297 return;
298 }
299 }
300 }
Leon Scroggins58d56c62010-01-28 15:12:40 -0500301 mVoiceSearchData.mLastVoiceSearchUrl
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500302 = mVoiceSearchData.mVoiceSearchUrls.get(index);
303 if (null == mVoiceSearchData.mLastVoiceSearchUrl) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700304 mVoiceSearchData.mLastVoiceSearchUrl = UrlUtils.smartUrlFilter(
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500305 mVoiceSearchData.mLastVoiceSearchTitle);
306 }
Leon Scroggins9df94972010-03-08 18:20:35 -0500307 Map<String, String> headers = null;
308 if (mVoiceSearchData.mHeaders != null) {
309 int bundleIndex = mVoiceSearchData.mHeaders.size() == 1 ? 0
310 : index;
311 Bundle bundle = mVoiceSearchData.mHeaders.get(bundleIndex);
312 if (bundle != null && !bundle.isEmpty()) {
313 Iterator<String> iter = bundle.keySet().iterator();
314 headers = new HashMap<String, String>();
315 while (iter.hasNext()) {
316 String key = iter.next();
317 headers.put(key, bundle.getString(key));
318 }
319 }
320 }
321 mMainView.loadUrl(mVoiceSearchData.mLastVoiceSearchUrl, headers);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500322 }
323 /* package */ static class VoiceSearchData {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500324 public VoiceSearchData(ArrayList<String> results,
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500325 ArrayList<String> urls, ArrayList<String> htmls,
326 ArrayList<String> baseUrls) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500327 mVoiceSearchResults = results;
328 mVoiceSearchUrls = urls;
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500329 mVoiceSearchHtmls = htmls;
330 mVoiceSearchBaseUrls = baseUrls;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500331 }
332 /*
333 * ArrayList of suggestions to be displayed when opening the
334 * SearchManager
335 */
336 public ArrayList<String> mVoiceSearchResults;
337 /*
338 * ArrayList of urls, associated with the suggestions in
339 * mVoiceSearchResults.
340 */
341 public ArrayList<String> mVoiceSearchUrls;
342 /*
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500343 * ArrayList holding content to load for each item in
344 * mVoiceSearchResults.
345 */
346 public ArrayList<String> mVoiceSearchHtmls;
347 /*
348 * ArrayList holding base urls for the items in mVoiceSearchResults.
349 * If non null, this will either have the same size as
350 * mVoiceSearchResults or have a size of 1, in which case all will use
351 * the same base url
352 */
353 public ArrayList<String> mVoiceSearchBaseUrls;
354 /*
Leon Scroggins58d56c62010-01-28 15:12:40 -0500355 * The last url provided by voice search. Used for comparison to see if
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500356 * we are going to a page by some method besides voice search.
Leon Scroggins58d56c62010-01-28 15:12:40 -0500357 */
358 public String mLastVoiceSearchUrl;
359 /**
360 * The last title used for voice search. Needed to update the title bar
361 * when switching tabs.
362 */
363 public String mLastVoiceSearchTitle;
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500364 /**
365 * Whether the Intent which turned on voice search mode contained the
366 * String signifying that Google was the source.
367 */
368 public boolean mSourceIsGoogle;
369 /**
Leon Scroggins9df94972010-03-08 18:20:35 -0500370 * List of headers to be passed into the WebView containing location
371 * information
372 */
373 public ArrayList<Bundle> mHeaders;
374 /**
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500375 * The Intent used to invoke voice search. Placed on the
376 * WebHistoryItem so that when coming back to a previous voice search
377 * page we can again activate voice search.
378 */
Leon Scrogginse10dde52010-03-08 19:53:03 -0500379 public Intent mVoiceSearchIntent;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500380 /**
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500381 * String used to identify Google as the source of voice search.
382 */
383 public static String SOURCE_IS_GOOGLE
384 = "android.speech.extras.SOURCE_IS_GOOGLE";
Leon Scroggins58d56c62010-01-28 15:12:40 -0500385 }
386
Grace Kloba22ac16e2009-10-07 18:00:23 -0700387 // Container class for the next error dialog that needs to be displayed
388 private class ErrorDialog {
389 public final int mTitle;
390 public final String mDescription;
391 public final int mError;
392 ErrorDialog(int title, String desc, int error) {
393 mTitle = title;
394 mDescription = desc;
395 mError = error;
396 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700397 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700398
399 private void processNextError() {
400 if (mQueuedErrors == null) {
401 return;
402 }
403 // The first one is currently displayed so just remove it.
404 mQueuedErrors.removeFirst();
405 if (mQueuedErrors.size() == 0) {
406 mQueuedErrors = null;
407 return;
408 }
409 showError(mQueuedErrors.getFirst());
410 }
411
412 private DialogInterface.OnDismissListener mDialogListener =
413 new DialogInterface.OnDismissListener() {
414 public void onDismiss(DialogInterface d) {
415 processNextError();
416 }
417 };
418 private LinkedList<ErrorDialog> mQueuedErrors;
419
420 private void queueError(int err, String desc) {
421 if (mQueuedErrors == null) {
422 mQueuedErrors = new LinkedList<ErrorDialog>();
423 }
424 for (ErrorDialog d : mQueuedErrors) {
425 if (d.mError == err) {
426 // Already saw a similar error, ignore the new one.
427 return;
428 }
429 }
430 ErrorDialog errDialog = new ErrorDialog(
431 err == WebViewClient.ERROR_FILE_NOT_FOUND ?
432 R.string.browserFrameFileErrorLabel :
433 R.string.browserFrameNetworkErrorLabel,
434 desc, err);
435 mQueuedErrors.addLast(errDialog);
436
437 // Show the dialog now if the queue was empty and it is in foreground
438 if (mQueuedErrors.size() == 1 && mInForeground) {
439 showError(errDialog);
440 }
441 }
442
443 private void showError(ErrorDialog errDialog) {
444 if (mInForeground) {
445 AlertDialog d = new AlertDialog.Builder(mActivity)
446 .setTitle(errDialog.mTitle)
447 .setMessage(errDialog.mDescription)
448 .setPositiveButton(R.string.ok, null)
449 .create();
450 d.setOnDismissListener(mDialogListener);
451 d.show();
452 }
453 }
454
455 // -------------------------------------------------------------------------
456 // WebViewClient implementation for the main WebView
457 // -------------------------------------------------------------------------
458
459 private final WebViewClient mWebViewClient = new WebViewClient() {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500460 private Message mDontResend;
461 private Message mResend;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700462 @Override
463 public void onPageStarted(WebView view, String url, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700464 mInPageLoad = true;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000465 mLoadStartTime = SystemClock.uptimeMillis();
Leon Scroggins58d56c62010-01-28 15:12:40 -0500466 if (mVoiceSearchData != null
467 && !url.equals(mVoiceSearchData.mLastVoiceSearchUrl)) {
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500468 if (mVoiceSearchData.mSourceIsGoogle) {
469 Intent i = new Intent(LoggingEvents.ACTION_LOG_EVENT);
470 i.putExtra(LoggingEvents.EXTRA_FLUSH, true);
471 mActivity.sendBroadcast(i);
472 }
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400473 revertVoiceSearchMode();
Leon Scroggins58d56c62010-01-28 15:12:40 -0500474 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700475
Grace Kloba22ac16e2009-10-07 18:00:23 -0700476
477 // If we start a touch icon load and then load a new page, we don't
478 // want to cancel the current touch icon loader. But, we do want to
479 // create a new one when the touch icon url is known.
480 if (mTouchIconLoader != null) {
481 mTouchIconLoader.mTab = null;
482 mTouchIconLoader = null;
483 }
484
485 // reset the error console
486 if (mErrorConsole != null) {
487 mErrorConsole.clearErrorMessages();
Michael Kolb8233fac2010-10-26 16:08:53 -0700488 if (mWebViewController.shouldShowErrorConsole()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700489 mErrorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
490 }
491 }
492
Grace Kloba22ac16e2009-10-07 18:00:23 -0700493
494 // finally update the UI in the activity if it is in the foreground
Michael Kolb8233fac2010-10-26 16:08:53 -0700495 mWebViewController.onPageStarted(Tab.this, view, url, favicon);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700496 }
497
498 @Override
499 public void onPageFinished(WebView view, String url) {
John Reck5b691842010-11-29 11:21:13 -0800500 if (!isPrivateBrowsingEnabled()) {
501 LogTag.logPageFinishedLoading(
502 url, SystemClock.uptimeMillis() - mLoadStartTime);
503 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700504 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700505
Michael Kolb8233fac2010-10-26 16:08:53 -0700506 mWebViewController.onPageFinished(Tab.this, url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700507 }
508
509 // return true if want to hijack the url to let another app to handle it
510 @Override
511 public boolean shouldOverrideUrlLoading(WebView view, String url) {
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400512 if (voiceSearchSourceIsGoogle()) {
513 // This method is called when the user clicks on a link.
514 // VoiceSearchMode is turned off when the user leaves the
515 // Google results page, so at this point the user must be on
516 // that page. If the user clicked a link on that page, assume
517 // that the voice search was effective, and broadcast an Intent
518 // so a receiver can take note of that fact.
519 Intent logIntent = new Intent(LoggingEvents.ACTION_LOG_EVENT);
520 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
521 LoggingEvents.VoiceSearch.RESULT_CLICKED);
522 mActivity.sendBroadcast(logIntent);
523 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700524 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700525 return mWebViewController.shouldOverrideUrlLoading(view, url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700526 } else {
527 return false;
528 }
529 }
530
531 /**
532 * Updates the lock icon. This method is called when we discover another
533 * resource to be loaded for this page (for example, javascript). While
534 * we update the icon type, we do not update the lock icon itself until
535 * we are done loading, it is slightly more secure this way.
536 */
537 @Override
538 public void onLoadResource(WebView view, String url) {
539 if (url != null && url.length() > 0) {
540 // It is only if the page claims to be secure that we may have
541 // to update the lock:
Michael Kolb8233fac2010-10-26 16:08:53 -0700542 if (mLockIconType == LOCK_ICON_SECURE) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700543 // If NOT a 'safe' url, change the lock to mixed content!
544 if (!(URLUtil.isHttpsUrl(url) || URLUtil.isDataUrl(url)
545 || URLUtil.isAboutUrl(url))) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700546 mLockIconType = LOCK_ICON_MIXED;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700547 }
548 }
549 }
550 }
551
552 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700553 * Show a dialog informing the user of the network error reported by
554 * WebCore if it is in the foreground.
555 */
556 @Override
557 public void onReceivedError(WebView view, int errorCode,
558 String description, String failingUrl) {
559 if (errorCode != WebViewClient.ERROR_HOST_LOOKUP &&
560 errorCode != WebViewClient.ERROR_CONNECT &&
561 errorCode != WebViewClient.ERROR_BAD_URL &&
562 errorCode != WebViewClient.ERROR_UNSUPPORTED_SCHEME &&
563 errorCode != WebViewClient.ERROR_FILE) {
564 queueError(errorCode, description);
565 }
Jeff Hamilton47654f42010-09-07 09:57:51 -0500566
567 // Don't log URLs when in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -0700568 if (!isPrivateBrowsingEnabled()) {
Jeff Hamilton47654f42010-09-07 09:57:51 -0500569 Log.e(LOGTAG, "onReceivedError " + errorCode + " " + failingUrl
570 + " " + description);
571 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700572
573 // We need to reset the title after an error if it is in foreground.
574 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700575 mWebViewController.resetTitleAndRevertLockIcon(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700576 }
577 }
578
579 /**
580 * Check with the user if it is ok to resend POST data as the page they
581 * are trying to navigate to is the result of a POST.
582 */
583 @Override
584 public void onFormResubmission(WebView view, final Message dontResend,
585 final Message resend) {
586 if (!mInForeground) {
587 dontResend.sendToTarget();
588 return;
589 }
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500590 if (mDontResend != null) {
591 Log.w(LOGTAG, "onFormResubmission should not be called again "
592 + "while dialog is still up");
593 dontResend.sendToTarget();
594 return;
595 }
596 mDontResend = dontResend;
597 mResend = resend;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700598 new AlertDialog.Builder(mActivity).setTitle(
599 R.string.browserFrameFormResubmitLabel).setMessage(
600 R.string.browserFrameFormResubmitMessage)
601 .setPositiveButton(R.string.ok,
602 new DialogInterface.OnClickListener() {
603 public void onClick(DialogInterface dialog,
604 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500605 if (mResend != null) {
606 mResend.sendToTarget();
607 mResend = null;
608 mDontResend = null;
609 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700610 }
611 }).setNegativeButton(R.string.cancel,
612 new DialogInterface.OnClickListener() {
613 public void onClick(DialogInterface dialog,
614 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500615 if (mDontResend != null) {
616 mDontResend.sendToTarget();
617 mResend = null;
618 mDontResend = null;
619 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700620 }
621 }).setOnCancelListener(new OnCancelListener() {
622 public void onCancel(DialogInterface dialog) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500623 if (mDontResend != null) {
624 mDontResend.sendToTarget();
625 mResend = null;
626 mDontResend = null;
627 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700628 }
629 }).show();
630 }
631
632 /**
633 * Insert the url into the visited history database.
634 * @param url The url to be inserted.
635 * @param isReload True if this url is being reloaded.
636 * FIXME: Not sure what to do when reloading the page.
637 */
638 @Override
639 public void doUpdateVisitedHistory(WebView view, String url,
640 boolean isReload) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700641 mWebViewController.doUpdateVisitedHistory(Tab.this, url, isReload);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700642 }
643
644 /**
645 * Displays SSL error(s) dialog to the user.
646 */
647 @Override
648 public void onReceivedSslError(final WebView view,
649 final SslErrorHandler handler, final SslError error) {
650 if (!mInForeground) {
651 handler.cancel();
652 return;
653 }
654 if (BrowserSettings.getInstance().showSecurityWarnings()) {
655 final LayoutInflater factory =
656 LayoutInflater.from(mActivity);
657 final View warningsView =
658 factory.inflate(R.layout.ssl_warnings, null);
659 final LinearLayout placeholder =
660 (LinearLayout)warningsView.findViewById(R.id.placeholder);
661
662 if (error.hasError(SslError.SSL_UNTRUSTED)) {
663 LinearLayout ll = (LinearLayout)factory
664 .inflate(R.layout.ssl_warning, null);
665 ((TextView)ll.findViewById(R.id.warning))
666 .setText(R.string.ssl_untrusted);
667 placeholder.addView(ll);
668 }
669
670 if (error.hasError(SslError.SSL_IDMISMATCH)) {
671 LinearLayout ll = (LinearLayout)factory
672 .inflate(R.layout.ssl_warning, null);
673 ((TextView)ll.findViewById(R.id.warning))
674 .setText(R.string.ssl_mismatch);
675 placeholder.addView(ll);
676 }
677
678 if (error.hasError(SslError.SSL_EXPIRED)) {
679 LinearLayout ll = (LinearLayout)factory
680 .inflate(R.layout.ssl_warning, null);
681 ((TextView)ll.findViewById(R.id.warning))
682 .setText(R.string.ssl_expired);
683 placeholder.addView(ll);
684 }
685
686 if (error.hasError(SslError.SSL_NOTYETVALID)) {
687 LinearLayout ll = (LinearLayout)factory
688 .inflate(R.layout.ssl_warning, null);
689 ((TextView)ll.findViewById(R.id.warning))
690 .setText(R.string.ssl_not_yet_valid);
691 placeholder.addView(ll);
692 }
693
694 new AlertDialog.Builder(mActivity).setTitle(
695 R.string.security_warning).setIcon(
696 android.R.drawable.ic_dialog_alert).setView(
697 warningsView).setPositiveButton(R.string.ssl_continue,
698 new DialogInterface.OnClickListener() {
699 public void onClick(DialogInterface dialog,
700 int whichButton) {
701 handler.proceed();
702 }
703 }).setNeutralButton(R.string.view_certificate,
704 new DialogInterface.OnClickListener() {
705 public void onClick(DialogInterface dialog,
706 int whichButton) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700707 mWebViewController.showSslCertificateOnError(view,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700708 handler, error);
709 }
Ben Murdocha49b8292010-11-16 11:56:04 +0000710 }).setNegativeButton(R.string.ssl_go_back,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700711 new DialogInterface.OnClickListener() {
712 public void onClick(DialogInterface dialog,
713 int whichButton) {
714 handler.cancel();
Michael Kolb8233fac2010-10-26 16:08:53 -0700715 mWebViewController.resetTitleAndRevertLockIcon(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700716 }
717 }).setOnCancelListener(
718 new DialogInterface.OnCancelListener() {
719 public void onCancel(DialogInterface dialog) {
720 handler.cancel();
Michael Kolb8233fac2010-10-26 16:08:53 -0700721 mWebViewController.resetTitleAndRevertLockIcon(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700722 }
723 }).show();
724 } else {
725 handler.proceed();
726 }
727 }
728
729 /**
730 * Handles an HTTP authentication request.
731 *
732 * @param handler The authentication handler
733 * @param host The host
734 * @param realm The realm
735 */
736 @Override
737 public void onReceivedHttpAuthRequest(WebView view,
738 final HttpAuthHandler handler, final String host,
739 final String realm) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700740 mWebViewController.onReceivedHttpAuthRequest(Tab.this, view, handler, host, realm);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700741 }
742
743 @Override
744 public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
745 if (!mInForeground) {
746 return false;
747 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700748 return mWebViewController.shouldOverrideKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700749 }
750
751 @Override
752 public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700753 if (!mInForeground) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700754 return;
755 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700756 mWebViewController.onUnhandledKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700757 }
758 };
759
760 // -------------------------------------------------------------------------
761 // WebChromeClient implementation for the main WebView
762 // -------------------------------------------------------------------------
763
764 private final WebChromeClient mWebChromeClient = new WebChromeClient() {
765 // Helper method to create a new tab or sub window.
766 private void createWindow(final boolean dialog, final Message msg) {
767 WebView.WebViewTransport transport =
768 (WebView.WebViewTransport) msg.obj;
769 if (dialog) {
770 createSubWindow();
Michael Kolb8233fac2010-10-26 16:08:53 -0700771 mWebViewController.attachSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700772 transport.setWebView(mSubView);
773 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -0700774 final Tab newTab = mWebViewController.openTabAndShow(
775 IntentHandler.EMPTY_URL_DATA, false, null);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700776 if (newTab != Tab.this) {
777 Tab.this.addChildTab(newTab);
778 }
779 transport.setWebView(newTab.getWebView());
780 }
781 msg.sendToTarget();
782 }
783
784 @Override
785 public boolean onCreateWindow(WebView view, final boolean dialog,
786 final boolean userGesture, final Message resultMsg) {
787 // only allow new window or sub window for the foreground case
788 if (!mInForeground) {
789 return false;
790 }
791 // Short-circuit if we can't create any more tabs or sub windows.
792 if (dialog && mSubView != null) {
793 new AlertDialog.Builder(mActivity)
794 .setTitle(R.string.too_many_subwindows_dialog_title)
795 .setIcon(android.R.drawable.ic_dialog_alert)
796 .setMessage(R.string.too_many_subwindows_dialog_message)
797 .setPositiveButton(R.string.ok, null)
798 .show();
799 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700800 } else if (!mWebViewController.getTabControl().canCreateNewTab()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700801 new AlertDialog.Builder(mActivity)
802 .setTitle(R.string.too_many_windows_dialog_title)
803 .setIcon(android.R.drawable.ic_dialog_alert)
804 .setMessage(R.string.too_many_windows_dialog_message)
805 .setPositiveButton(R.string.ok, null)
806 .show();
807 return false;
808 }
809
810 // Short-circuit if this was a user gesture.
811 if (userGesture) {
812 createWindow(dialog, resultMsg);
813 return true;
814 }
815
816 // Allow the popup and create the appropriate window.
817 final AlertDialog.OnClickListener allowListener =
818 new AlertDialog.OnClickListener() {
819 public void onClick(DialogInterface d,
820 int which) {
821 createWindow(dialog, resultMsg);
822 }
823 };
824
825 // Block the popup by returning a null WebView.
826 final AlertDialog.OnClickListener blockListener =
827 new AlertDialog.OnClickListener() {
828 public void onClick(DialogInterface d, int which) {
829 resultMsg.sendToTarget();
830 }
831 };
832
833 // Build a confirmation dialog to display to the user.
834 final AlertDialog d =
835 new AlertDialog.Builder(mActivity)
836 .setTitle(R.string.attention)
837 .setIcon(android.R.drawable.ic_dialog_alert)
838 .setMessage(R.string.popup_window_attempt)
839 .setPositiveButton(R.string.allow, allowListener)
840 .setNegativeButton(R.string.block, blockListener)
841 .setCancelable(false)
842 .create();
843
844 // Show the confirmation dialog.
845 d.show();
846 return true;
847 }
848
849 @Override
Patrick Scotteb5061b2009-11-18 15:00:30 -0500850 public void onRequestFocus(WebView view) {
851 if (!mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700852 mWebViewController.switchToTab(mWebViewController.getTabControl().getTabIndex(
Patrick Scotteb5061b2009-11-18 15:00:30 -0500853 Tab.this));
854 }
855 }
856
857 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700858 public void onCloseWindow(WebView window) {
859 if (mParentTab != null) {
860 // JavaScript can only close popup window.
861 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700862 mWebViewController.switchToTab(mWebViewController.getTabControl()
Grace Kloba22ac16e2009-10-07 18:00:23 -0700863 .getTabIndex(mParentTab));
864 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700865 mWebViewController.closeTab(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700866 }
867 }
868
869 @Override
870 public void onProgressChanged(WebView view, int newProgress) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700871 mWebViewController.onProgressChanged(Tab.this, newProgress);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700872 }
873
874 @Override
Leon Scroggins21d9b902010-03-11 09:33:11 -0500875 public void onReceivedTitle(WebView view, final String title) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700876 mWebViewController.onReceivedTitle(Tab.this, title);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700877 }
878
879 @Override
880 public void onReceivedIcon(WebView view, Bitmap icon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700881 mWebViewController.onFavicon(Tab.this, view, icon);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700882 }
883
884 @Override
885 public void onReceivedTouchIconUrl(WebView view, String url,
886 boolean precomposed) {
887 final ContentResolver cr = mActivity.getContentResolver();
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400888 // Let precomposed icons take precedence over non-composed
889 // icons.
890 if (precomposed && mTouchIconLoader != null) {
891 mTouchIconLoader.cancel(false);
892 mTouchIconLoader = null;
893 }
894 // Have only one async task at a time.
895 if (mTouchIconLoader == null) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700896 mTouchIconLoader = new DownloadTouchIcon(Tab.this,
897 mActivity, cr, view);
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400898 mTouchIconLoader.execute(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700899 }
900 }
901
902 @Override
903 public void onShowCustomView(View view,
904 WebChromeClient.CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700905 if (mInForeground) mWebViewController.showCustomView(Tab.this, view,
906 callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700907 }
908
909 @Override
910 public void onHideCustomView() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700911 if (mInForeground) mWebViewController.hideCustomView();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700912 }
913
914 /**
915 * The origin has exceeded its database quota.
916 * @param url the URL that exceeded the quota
917 * @param databaseIdentifier the identifier of the database on which the
918 * transaction that caused the quota overflow was run
919 * @param currentQuota the current quota for the origin.
920 * @param estimatedSize the estimated size of the database.
921 * @param totalUsedQuota is the sum of all origins' quota.
922 * @param quotaUpdater The callback to run when a decision to allow or
923 * deny quota has been made. Don't forget to call this!
924 */
925 @Override
926 public void onExceededDatabaseQuota(String url,
927 String databaseIdentifier, long currentQuota, long estimatedSize,
928 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
929 BrowserSettings.getInstance().getWebStorageSizeManager()
930 .onExceededDatabaseQuota(url, databaseIdentifier,
931 currentQuota, estimatedSize, totalUsedQuota,
932 quotaUpdater);
933 }
934
935 /**
936 * The Application Cache has exceeded its max size.
937 * @param spaceNeeded is the amount of disk space that would be needed
938 * in order for the last appcache operation to succeed.
939 * @param totalUsedQuota is the sum of all origins' quota.
940 * @param quotaUpdater A callback to inform the WebCore thread that a
941 * new app cache size is available. This callback must always
942 * be executed at some point to ensure that the sleeping
943 * WebCore thread is woken up.
944 */
945 @Override
946 public void onReachedMaxAppCacheSize(long spaceNeeded,
947 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
948 BrowserSettings.getInstance().getWebStorageSizeManager()
949 .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota,
950 quotaUpdater);
951 }
952
953 /**
954 * Instructs the browser to show a prompt to ask the user to set the
955 * Geolocation permission state for the specified origin.
956 * @param origin The origin for which Geolocation permissions are
957 * requested.
958 * @param callback The callback to call once the user has set the
959 * Geolocation permission state.
960 */
961 @Override
962 public void onGeolocationPermissionsShowPrompt(String origin,
963 GeolocationPermissions.Callback callback) {
964 if (mInForeground) {
Grace Kloba50c241e2010-04-20 11:07:50 -0700965 getGeolocationPermissionsPrompt().show(origin, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700966 }
967 }
968
969 /**
970 * Instructs the browser to hide the Geolocation permissions prompt.
971 */
972 @Override
973 public void onGeolocationPermissionsHidePrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -0700974 if (mInForeground && mGeolocationPermissionsPrompt != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700975 mGeolocationPermissionsPrompt.hide();
976 }
977 }
978
Ben Murdoch65acc352009-11-19 18:16:04 +0000979 /* Adds a JavaScript error message to the system log and if the JS
980 * console is enabled in the about:debug options, to that console
981 * also.
Ben Murdochc42addf2010-01-28 15:19:59 +0000982 * @param consoleMessage the message object.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700983 */
984 @Override
Ben Murdochc42addf2010-01-28 15:19:59 +0000985 public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700986 if (mInForeground) {
987 // call getErrorConsole(true) so it will create one if needed
988 ErrorConsoleView errorConsole = getErrorConsole(true);
Ben Murdochc42addf2010-01-28 15:19:59 +0000989 errorConsole.addErrorMessage(consoleMessage);
Michael Kolb8233fac2010-10-26 16:08:53 -0700990 if (mWebViewController.shouldShowErrorConsole()
991 && errorConsole.getShowState() !=
992 ErrorConsoleView.SHOW_MAXIMIZED) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700993 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
994 }
995 }
Ben Murdochc42addf2010-01-28 15:19:59 +0000996
Jeff Hamilton47654f42010-09-07 09:57:51 -0500997 // Don't log console messages in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -0700998 if (isPrivateBrowsingEnabled()) return true;
Jeff Hamilton47654f42010-09-07 09:57:51 -0500999
Ben Murdochc42addf2010-01-28 15:19:59 +00001000 String message = "Console: " + consoleMessage.message() + " "
1001 + consoleMessage.sourceId() + ":"
1002 + consoleMessage.lineNumber();
1003
1004 switch (consoleMessage.messageLevel()) {
1005 case TIP:
1006 Log.v(CONSOLE_LOGTAG, message);
1007 break;
1008 case LOG:
1009 Log.i(CONSOLE_LOGTAG, message);
1010 break;
1011 case WARNING:
1012 Log.w(CONSOLE_LOGTAG, message);
1013 break;
1014 case ERROR:
1015 Log.e(CONSOLE_LOGTAG, message);
1016 break;
1017 case DEBUG:
1018 Log.d(CONSOLE_LOGTAG, message);
1019 break;
1020 }
1021
1022 return true;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001023 }
1024
1025 /**
1026 * Ask the browser for an icon to represent a <video> element.
1027 * This icon will be used if the Web page did not specify a poster attribute.
1028 * @return Bitmap The icon or null if no such icon is available.
1029 */
1030 @Override
1031 public Bitmap getDefaultVideoPoster() {
1032 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001033 return mWebViewController.getDefaultVideoPoster();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001034 }
1035 return null;
1036 }
1037
1038 /**
1039 * Ask the host application for a custom progress view to show while
1040 * a <video> is loading.
1041 * @return View The progress view.
1042 */
1043 @Override
1044 public View getVideoLoadingProgressView() {
1045 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001046 return mWebViewController.getVideoLoadingProgressView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001047 }
1048 return null;
1049 }
1050
1051 @Override
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01001052 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001053 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001054 mWebViewController.openFileChooser(uploadMsg, acceptType);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001055 } else {
1056 uploadMsg.onReceiveValue(null);
1057 }
1058 }
1059
1060 /**
1061 * Deliver a list of already-visited URLs
1062 */
1063 @Override
1064 public void getVisitedHistory(final ValueCallback<String[]> callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001065 mWebViewController.getVisitedHistory(callback);
1066 }
Ben Murdoch8029a772010-11-16 11:58:21 +00001067
1068 @Override
1069 public void setupAutoFill(Message message) {
1070 // Prompt the user to set up their profile.
1071 final Message msg = message;
1072 AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
1073 builder.setMessage(R.string.autofill_setup_dialog_message)
1074 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1075 @Override
1076 public void onClick(DialogInterface dialog, int id) {
1077 // Take user to the AutoFill profile editor. When they return,
1078 // we will send the message that we pass here which will trigger
1079 // the form to get filled out with their new profile.
1080 mWebViewController.setupAutoFill(msg);
1081 }
1082 })
1083 .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
1084 @Override
1085 public void onClick(DialogInterface dialog, int id) {
1086 // Disable autofill and show a toast with how to turn it on again.
1087 BrowserSettings s = BrowserSettings.getInstance();
1088 s.addObserver(mMainView.getSettings());
1089 s.disableAutoFill(mActivity);
1090 s.update();
1091 Toast.makeText(mActivity, R.string.autofill_setup_dialog_negative_toast,
1092 Toast.LENGTH_LONG).show();
1093 }
1094 }).show();
1095 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001096 };
1097
1098 // -------------------------------------------------------------------------
1099 // WebViewClient implementation for the sub window
1100 // -------------------------------------------------------------------------
1101
1102 // Subclass of WebViewClient used in subwindows to notify the main
1103 // WebViewClient of certain WebView activities.
1104 private static class SubWindowClient extends WebViewClient {
1105 // The main WebViewClient.
1106 private final WebViewClient mClient;
Michael Kolb8233fac2010-10-26 16:08:53 -07001107 private final WebViewController mController;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001108
Michael Kolb8233fac2010-10-26 16:08:53 -07001109 SubWindowClient(WebViewClient client, WebViewController controller) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001110 mClient = client;
Michael Kolb8233fac2010-10-26 16:08:53 -07001111 mController = controller;
Leon Scroggins III211ba542010-04-19 13:21:13 -04001112 }
1113 @Override
1114 public void onPageStarted(WebView view, String url, Bitmap favicon) {
1115 // Unlike the others, do not call mClient's version, which would
1116 // change the progress bar. However, we do want to remove the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001117 // find or select dialog.
Michael Kolb8233fac2010-10-26 16:08:53 -07001118 mController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001119 }
1120 @Override
1121 public void doUpdateVisitedHistory(WebView view, String url,
1122 boolean isReload) {
1123 mClient.doUpdateVisitedHistory(view, url, isReload);
1124 }
1125 @Override
1126 public boolean shouldOverrideUrlLoading(WebView view, String url) {
1127 return mClient.shouldOverrideUrlLoading(view, url);
1128 }
1129 @Override
1130 public void onReceivedSslError(WebView view, SslErrorHandler handler,
1131 SslError error) {
1132 mClient.onReceivedSslError(view, handler, error);
1133 }
1134 @Override
1135 public void onReceivedHttpAuthRequest(WebView view,
1136 HttpAuthHandler handler, String host, String realm) {
1137 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
1138 }
1139 @Override
1140 public void onFormResubmission(WebView view, Message dontResend,
1141 Message resend) {
1142 mClient.onFormResubmission(view, dontResend, resend);
1143 }
1144 @Override
1145 public void onReceivedError(WebView view, int errorCode,
1146 String description, String failingUrl) {
1147 mClient.onReceivedError(view, errorCode, description, failingUrl);
1148 }
1149 @Override
1150 public boolean shouldOverrideKeyEvent(WebView view,
1151 android.view.KeyEvent event) {
1152 return mClient.shouldOverrideKeyEvent(view, event);
1153 }
1154 @Override
1155 public void onUnhandledKeyEvent(WebView view,
1156 android.view.KeyEvent event) {
1157 mClient.onUnhandledKeyEvent(view, event);
1158 }
1159 }
1160
1161 // -------------------------------------------------------------------------
1162 // WebChromeClient implementation for the sub window
1163 // -------------------------------------------------------------------------
1164
1165 private class SubWindowChromeClient extends WebChromeClient {
1166 // The main WebChromeClient.
1167 private final WebChromeClient mClient;
1168
1169 SubWindowChromeClient(WebChromeClient client) {
1170 mClient = client;
1171 }
1172 @Override
1173 public void onProgressChanged(WebView view, int newProgress) {
1174 mClient.onProgressChanged(view, newProgress);
1175 }
1176 @Override
1177 public boolean onCreateWindow(WebView view, boolean dialog,
1178 boolean userGesture, android.os.Message resultMsg) {
1179 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
1180 }
1181 @Override
1182 public void onCloseWindow(WebView window) {
1183 if (window != mSubView) {
1184 Log.e(LOGTAG, "Can't close the window");
1185 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001186 mWebViewController.dismissSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001187 }
1188 }
1189
1190 // -------------------------------------------------------------------------
1191
Michael Kolb8233fac2010-10-26 16:08:53 -07001192 // TODO temporarily use activity here
1193 // remove later
1194
Grace Kloba22ac16e2009-10-07 18:00:23 -07001195 // Construct a new tab
Michael Kolb8233fac2010-10-26 16:08:53 -07001196 Tab(WebViewController wvcontroller, WebView w, boolean closeOnExit, String appId,
Grace Kloba22ac16e2009-10-07 18:00:23 -07001197 String url) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001198 mWebViewController = wvcontroller;
1199 mActivity = mWebViewController.getActivity();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001200 mCloseOnExit = closeOnExit;
1201 mAppId = appId;
1202 mOriginalUrl = url;
Michael Kolb8233fac2010-10-26 16:08:53 -07001203 mLockIconType = LOCK_ICON_UNSECURE;
1204 mPrevLockIconType = LOCK_ICON_UNSECURE;
1205 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001206 mInForeground = false;
1207
Michael Kolb8233fac2010-10-26 16:08:53 -07001208 mInflateService = LayoutInflater.from(mActivity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001209
1210 // The tab consists of a container view, which contains the main
1211 // WebView, as well as any other UI elements associated with the tab.
Leon Scroggins III211ba542010-04-19 13:21:13 -04001212 mContainer = (LinearLayout) mInflateService.inflate(R.layout.tab, null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001213
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001214 mDownloadListener = new DownloadListener() {
1215 public void onDownloadStart(String url, String userAgent,
1216 String contentDisposition, String mimetype,
1217 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001218 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001219 mimetype, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001220 }
1221 };
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001222 mWebBackForwardListClient = new WebBackForwardListClient() {
1223 @Override
1224 public void onNewHistoryItem(WebHistoryItem item) {
1225 if (isInVoiceSearchMode()) {
1226 item.setCustomData(mVoiceSearchData.mVoiceSearchIntent);
1227 }
1228 }
1229 @Override
1230 public void onIndexChanged(WebHistoryItem item, int index) {
1231 Object data = item.getCustomData();
1232 if (data != null && data instanceof Intent) {
1233 activateVoiceSearchMode((Intent) data);
1234 }
1235 }
1236 };
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001237
Grace Kloba22ac16e2009-10-07 18:00:23 -07001238 setWebView(w);
1239 }
1240
1241 /**
1242 * Sets the WebView for this tab, correctly removing the old WebView from
1243 * the container view.
1244 */
1245 void setWebView(WebView w) {
1246 if (mMainView == w) {
1247 return;
1248 }
1249 // If the WebView is changing, the page will be reloaded, so any ongoing
1250 // Geolocation permission requests are void.
Grace Kloba50c241e2010-04-20 11:07:50 -07001251 if (mGeolocationPermissionsPrompt != null) {
1252 mGeolocationPermissionsPrompt.hide();
1253 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001254
1255 // Just remove the old one.
1256 FrameLayout wrapper =
1257 (FrameLayout) mContainer.findViewById(R.id.webview_wrapper);
1258 wrapper.removeView(mMainView);
1259
1260 // set the new one
1261 mMainView = w;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001262 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001263 if (mMainView != null) {
1264 mMainView.setWebViewClient(mWebViewClient);
1265 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001266 // Attach DownloadManager so that downloads can start in an active
1267 // or a non-active window. This can happen when going to a site that
1268 // does a redirect after a period of time. The user could have
1269 // switched to another tab while waiting for the download to start.
1270 mMainView.setDownloadListener(mDownloadListener);
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001271 mMainView.setWebBackForwardListClient(mWebBackForwardListClient);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001272 }
1273 }
1274
1275 /**
1276 * Destroy the tab's main WebView and subWindow if any
1277 */
1278 void destroy() {
1279 if (mMainView != null) {
1280 dismissSubWindow();
1281 BrowserSettings.getInstance().deleteObserver(mMainView.getSettings());
1282 // save the WebView to call destroy() after detach it from the tab
1283 WebView webView = mMainView;
1284 setWebView(null);
1285 webView.destroy();
1286 }
1287 }
1288
1289 /**
1290 * Remove the tab from the parent
1291 */
1292 void removeFromTree() {
1293 // detach the children
1294 if (mChildTabs != null) {
1295 for(Tab t : mChildTabs) {
1296 t.setParentTab(null);
1297 }
1298 }
1299 // remove itself from the parent list
1300 if (mParentTab != null) {
1301 mParentTab.mChildTabs.remove(this);
1302 }
1303 }
1304
1305 /**
1306 * Create a new subwindow unless a subwindow already exists.
1307 * @return True if a new subwindow was created. False if one already exists.
1308 */
1309 boolean createSubWindow() {
1310 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001311 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001312 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001313 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001314 mSubView.setWebChromeClient(new SubWindowChromeClient(
1315 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001316 // Set a different DownloadListener for the mSubView, since it will
1317 // just need to dismiss the mSubView, rather than close the Tab
1318 mSubView.setDownloadListener(new DownloadListener() {
1319 public void onDownloadStart(String url, String userAgent,
1320 String contentDisposition, String mimetype,
1321 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001322 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001323 contentDisposition, mimetype, contentLength);
1324 if (mSubView.copyBackForwardList().getSize() == 0) {
1325 // This subwindow was opened for the sole purpose of
1326 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001327 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001328 }
1329 }
1330 });
Grace Kloba22ac16e2009-10-07 18:00:23 -07001331 mSubView.setOnCreateContextMenuListener(mActivity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001332 return true;
1333 }
1334 return false;
1335 }
1336
1337 /**
1338 * Dismiss the subWindow for the tab.
1339 */
1340 void dismissSubWindow() {
1341 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001342 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001343 BrowserSettings.getInstance().deleteObserver(
1344 mSubView.getSettings());
1345 mSubView.destroy();
1346 mSubView = null;
1347 mSubViewContainer = null;
1348 }
1349 }
1350
Grace Kloba22ac16e2009-10-07 18:00:23 -07001351
1352 /**
1353 * Set the parent tab of this tab.
1354 */
1355 void setParentTab(Tab parent) {
1356 mParentTab = parent;
1357 // This tab may have been freed due to low memory. If that is the case,
1358 // the parent tab index is already saved. If we are changing that index
1359 // (most likely due to removing the parent tab) we must update the
1360 // parent tab index in the saved Bundle.
1361 if (mSavedState != null) {
1362 if (parent == null) {
1363 mSavedState.remove(PARENTTAB);
1364 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -07001365 mSavedState.putInt(PARENTTAB, mWebViewController.getTabControl()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001366 .getTabIndex(parent));
1367 }
1368 }
1369 }
1370
1371 /**
1372 * When a Tab is created through the content of another Tab, then we
1373 * associate the Tabs.
1374 * @param child the Tab that was created from this Tab
1375 */
1376 void addChildTab(Tab child) {
1377 if (mChildTabs == null) {
1378 mChildTabs = new Vector<Tab>();
1379 }
1380 mChildTabs.add(child);
1381 child.setParentTab(this);
1382 }
1383
1384 Vector<Tab> getChildTabs() {
1385 return mChildTabs;
1386 }
1387
1388 void resume() {
1389 if (mMainView != null) {
1390 mMainView.onResume();
1391 if (mSubView != null) {
1392 mSubView.onResume();
1393 }
1394 }
1395 }
1396
1397 void pause() {
1398 if (mMainView != null) {
1399 mMainView.onPause();
1400 if (mSubView != null) {
1401 mSubView.onPause();
1402 }
1403 }
1404 }
1405
1406 void putInForeground() {
1407 mInForeground = true;
1408 resume();
1409 mMainView.setOnCreateContextMenuListener(mActivity);
1410 if (mSubView != null) {
1411 mSubView.setOnCreateContextMenuListener(mActivity);
1412 }
1413 // Show the pending error dialog if the queue is not empty
1414 if (mQueuedErrors != null && mQueuedErrors.size() > 0) {
1415 showError(mQueuedErrors.getFirst());
1416 }
1417 }
1418
1419 void putInBackground() {
1420 mInForeground = false;
1421 pause();
1422 mMainView.setOnCreateContextMenuListener(null);
1423 if (mSubView != null) {
1424 mSubView.setOnCreateContextMenuListener(null);
1425 }
1426 }
1427
Michael Kolb8233fac2010-10-26 16:08:53 -07001428 boolean inForeground() {
1429 return mInForeground;
1430 }
1431
Grace Kloba22ac16e2009-10-07 18:00:23 -07001432 /**
1433 * Return the top window of this tab; either the subwindow if it is not
1434 * null or the main window.
1435 * @return The top window of this tab.
1436 */
1437 WebView getTopWindow() {
1438 if (mSubView != null) {
1439 return mSubView;
1440 }
1441 return mMainView;
1442 }
1443
1444 /**
1445 * Return the main window of this tab. Note: if a tab is freed in the
1446 * background, this can return null. It is only guaranteed to be
1447 * non-null for the current tab.
1448 * @return The main WebView of this tab.
1449 */
1450 WebView getWebView() {
1451 return mMainView;
1452 }
1453
Michael Kolb8233fac2010-10-26 16:08:53 -07001454 View getViewContainer() {
1455 return mContainer;
1456 }
1457
Grace Kloba22ac16e2009-10-07 18:00:23 -07001458 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001459 * Return whether private browsing is enabled for the main window of
1460 * this tab.
1461 * @return True if private browsing is enabled.
1462 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001463 boolean isPrivateBrowsingEnabled() {
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001464 WebView webView = getWebView();
1465 if (webView == null) {
1466 return false;
1467 }
1468 return webView.isPrivateBrowsingEnabled();
1469 }
1470
1471 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001472 * Return the subwindow of this tab or null if there is no subwindow.
1473 * @return The subwindow of this tab or null.
1474 */
1475 WebView getSubWebView() {
1476 return mSubView;
1477 }
1478
Michael Kolb1514bb72010-11-22 09:11:48 -08001479 void setSubWebView(WebView subView) {
1480 mSubView = subView;
1481 }
1482
Michael Kolb8233fac2010-10-26 16:08:53 -07001483 View getSubViewContainer() {
1484 return mSubViewContainer;
1485 }
1486
Michael Kolb1514bb72010-11-22 09:11:48 -08001487 void setSubViewContainer(View subViewContainer) {
1488 mSubViewContainer = subViewContainer;
1489 }
1490
Grace Kloba22ac16e2009-10-07 18:00:23 -07001491 /**
1492 * @return The geolocation permissions prompt for this tab.
1493 */
1494 GeolocationPermissionsPrompt getGeolocationPermissionsPrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001495 if (mGeolocationPermissionsPrompt == null) {
1496 ViewStub stub = (ViewStub) mContainer
1497 .findViewById(R.id.geolocation_permissions_prompt);
1498 mGeolocationPermissionsPrompt = (GeolocationPermissionsPrompt) stub
1499 .inflate();
1500 mGeolocationPermissionsPrompt.init();
1501 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001502 return mGeolocationPermissionsPrompt;
1503 }
1504
1505 /**
1506 * @return The application id string
1507 */
1508 String getAppId() {
1509 return mAppId;
1510 }
1511
1512 /**
1513 * Set the application id string
1514 * @param id
1515 */
1516 void setAppId(String id) {
1517 mAppId = id;
1518 }
1519
1520 /**
1521 * @return The original url associated with this Tab
1522 */
1523 String getOriginalUrl() {
1524 return mOriginalUrl;
1525 }
1526
1527 /**
1528 * Set the original url associated with this tab
1529 */
1530 void setOriginalUrl(String url) {
1531 mOriginalUrl = url;
1532 }
1533
1534 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07001535 * set the title for the tab
1536 */
1537 void setCurrentTitle(String title) {
1538 mCurrentTitle = title;
1539 }
1540
1541 /**
1542 * set url for this tab
1543 * @param url
1544 */
1545 void setCurrentUrl(String url) {
1546 mCurrentUrl = url;
1547 }
1548
1549 String getCurrentTitle() {
1550 return mCurrentTitle;
1551 }
1552
1553 String getCurrentUrl() {
1554 return mCurrentUrl;
1555 }
1556 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001557 * Get the url of this tab. Valid after calling populatePickerData, but
1558 * before calling wipePickerData, or if the webview has been destroyed.
1559 * @return The WebView's url or null.
1560 */
1561 String getUrl() {
1562 if (mPickerData != null) {
1563 return mPickerData.mUrl;
1564 }
1565 return null;
1566 }
1567
1568 /**
1569 * Get the title of this tab. Valid after calling populatePickerData, but
1570 * before calling wipePickerData, or if the webview has been destroyed. If
1571 * the url has no title, use the url instead.
1572 * @return The WebView's title (or url) or null.
1573 */
1574 String getTitle() {
1575 if (mPickerData != null) {
1576 return mPickerData.mTitle;
1577 }
1578 return null;
1579 }
1580
1581 /**
1582 * Get the favicon of this tab. Valid after calling populatePickerData, but
1583 * before calling wipePickerData, or if the webview has been destroyed.
1584 * @return The WebView's favicon or null.
1585 */
1586 Bitmap getFavicon() {
1587 if (mPickerData != null) {
1588 return mPickerData.mFavicon;
1589 }
1590 return null;
1591 }
1592
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001593
Grace Kloba22ac16e2009-10-07 18:00:23 -07001594 /**
1595 * Return the tab's error console. Creates the console if createIfNEcessary
1596 * is true and we haven't already created the console.
1597 * @param createIfNecessary Flag to indicate if the console should be
1598 * created if it has not been already.
1599 * @return The tab's error console, or null if one has not been created and
1600 * createIfNecessary is false.
1601 */
1602 ErrorConsoleView getErrorConsole(boolean createIfNecessary) {
1603 if (createIfNecessary && mErrorConsole == null) {
1604 mErrorConsole = new ErrorConsoleView(mActivity);
1605 mErrorConsole.setWebView(mMainView);
1606 }
1607 return mErrorConsole;
1608 }
1609
1610 /**
1611 * If this Tab was created through another Tab, then this method returns
1612 * that Tab.
1613 * @return the Tab parent or null
1614 */
1615 public Tab getParentTab() {
1616 return mParentTab;
1617 }
1618
1619 /**
1620 * Return whether this tab should be closed when it is backing out of the
1621 * first page.
1622 * @return TRUE if this tab should be closed when exit.
1623 */
1624 boolean closeOnExit() {
1625 return mCloseOnExit;
1626 }
1627
1628 /**
1629 * Saves the current lock-icon state before resetting the lock icon. If we
1630 * have an error, we may need to roll back to the previous state.
1631 */
1632 void resetLockIcon(String url) {
1633 mPrevLockIconType = mLockIconType;
Michael Kolb8233fac2010-10-26 16:08:53 -07001634 mLockIconType = LOCK_ICON_UNSECURE;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001635 if (URLUtil.isHttpsUrl(url)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001636 mLockIconType = LOCK_ICON_SECURE;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001637 }
1638 }
1639
1640 /**
1641 * Reverts the lock-icon state to the last saved state, for example, if we
1642 * had an error, and need to cancel the load.
1643 */
1644 void revertLockIcon() {
1645 mLockIconType = mPrevLockIconType;
1646 }
1647
1648 /**
1649 * @return The tab's lock icon type.
1650 */
1651 int getLockIconType() {
1652 return mLockIconType;
1653 }
1654
1655 /**
1656 * @return TRUE if onPageStarted is called while onPageFinished is not
1657 * called yet.
1658 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001659 boolean inPageLoad() {
1660 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001661 }
1662
1663 // force mInLoad to be false. This should only be called before closing the
1664 // tab to ensure BrowserActivity's pauseWebViewTimers() is called correctly.
Michael Kolb8233fac2010-10-26 16:08:53 -07001665 void clearInPageLoad() {
1666 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001667 }
1668
1669 void populatePickerData() {
1670 if (mMainView == null) {
1671 populatePickerDataFromSavedState();
1672 return;
1673 }
1674
1675 // FIXME: The only place we cared about subwindow was for
1676 // bookmarking (i.e. not when saving state). Was this deliberate?
1677 final WebBackForwardList list = mMainView.copyBackForwardList();
Leon Scroggins70a153b2010-05-10 17:27:26 -04001678 if (list == null) {
1679 Log.w(LOGTAG, "populatePickerData called and WebBackForwardList is null");
1680 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001681 final WebHistoryItem item = list != null ? list.getCurrentItem() : null;
1682 populatePickerData(item);
1683 }
1684
1685 // Populate the picker data using the given history item and the current top
1686 // WebView.
1687 private void populatePickerData(WebHistoryItem item) {
1688 mPickerData = new PickerData();
Leon Scroggins70a153b2010-05-10 17:27:26 -04001689 if (item == null) {
1690 Log.w(LOGTAG, "populatePickerData called with a null WebHistoryItem");
1691 } else {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001692 mPickerData.mUrl = item.getUrl();
1693 mPickerData.mTitle = item.getTitle();
1694 mPickerData.mFavicon = item.getFavicon();
1695 if (mPickerData.mTitle == null) {
1696 mPickerData.mTitle = mPickerData.mUrl;
1697 }
1698 }
1699 }
1700
1701 // Create the PickerData and populate it using the saved state of the tab.
1702 void populatePickerDataFromSavedState() {
1703 if (mSavedState == null) {
1704 return;
1705 }
1706 mPickerData = new PickerData();
1707 mPickerData.mUrl = mSavedState.getString(CURRURL);
1708 mPickerData.mTitle = mSavedState.getString(CURRTITLE);
1709 }
1710
1711 void clearPickerData() {
1712 mPickerData = null;
1713 }
1714
1715 /**
1716 * Get the saved state bundle.
1717 * @return
1718 */
1719 Bundle getSavedState() {
1720 return mSavedState;
1721 }
1722
1723 /**
1724 * Set the saved state.
1725 */
1726 void setSavedState(Bundle state) {
1727 mSavedState = state;
1728 }
1729
1730 /**
1731 * @return TRUE if succeed in saving the state.
1732 */
1733 boolean saveState() {
1734 // If the WebView is null it means we ran low on memory and we already
1735 // stored the saved state in mSavedState.
1736 if (mMainView == null) {
1737 return mSavedState != null;
1738 }
1739
1740 mSavedState = new Bundle();
1741 final WebBackForwardList list = mMainView.saveState(mSavedState);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001742
1743 // Store some extra info for displaying the tab in the picker.
1744 final WebHistoryItem item = list != null ? list.getCurrentItem() : null;
1745 populatePickerData(item);
1746
1747 if (mPickerData.mUrl != null) {
1748 mSavedState.putString(CURRURL, mPickerData.mUrl);
1749 }
1750 if (mPickerData.mTitle != null) {
1751 mSavedState.putString(CURRTITLE, mPickerData.mTitle);
1752 }
1753 mSavedState.putBoolean(CLOSEONEXIT, mCloseOnExit);
1754 if (mAppId != null) {
1755 mSavedState.putString(APPID, mAppId);
1756 }
1757 if (mOriginalUrl != null) {
1758 mSavedState.putString(ORIGINALURL, mOriginalUrl);
1759 }
1760 // Remember the parent tab so the relationship can be restored.
1761 if (mParentTab != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001762 mSavedState.putInt(PARENTTAB, mWebViewController.getTabControl().getTabIndex(
Grace Kloba22ac16e2009-10-07 18:00:23 -07001763 mParentTab));
1764 }
1765 return true;
1766 }
1767
1768 /*
1769 * Restore the state of the tab.
1770 */
1771 boolean restoreState(Bundle b) {
1772 if (b == null) {
1773 return false;
1774 }
1775 // Restore the internal state even if the WebView fails to restore.
1776 // This will maintain the app id, original url and close-on-exit values.
1777 mSavedState = null;
1778 mPickerData = null;
1779 mCloseOnExit = b.getBoolean(CLOSEONEXIT);
1780 mAppId = b.getString(APPID);
1781 mOriginalUrl = b.getString(ORIGINALURL);
1782
1783 final WebBackForwardList list = mMainView.restoreState(b);
1784 if (list == null) {
1785 return false;
1786 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001787 return true;
1788 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04001789
Grace Kloba22ac16e2009-10-07 18:00:23 -07001790}