blob: 0ba59f406f1ba63df719bd8ba9784162deb1fd73 [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;
Leon Scroggins4cd97792010-12-03 15:31:56 -050028import android.database.Cursor;
29import android.database.sqlite.SQLiteException;
Grace Kloba22ac16e2009-10-07 18:00:23 -070030import android.graphics.Bitmap;
31import android.net.Uri;
32import android.net.http.SslError;
Leon Scroggins4cd97792010-12-03 15:31:56 -050033import android.os.AsyncTask;
Grace Kloba22ac16e2009-10-07 18:00:23 -070034import android.os.Bundle;
35import android.os.Message;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +000036import android.os.SystemClock;
Leon Scroggins4cd97792010-12-03 15:31:56 -050037import android.provider.BrowserContract;
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -050038import android.speech.RecognizerResultsIntent;
Grace Kloba22ac16e2009-10-07 18:00:23 -070039import android.util.Log;
40import android.view.KeyEvent;
41import android.view.LayoutInflater;
42import android.view.View;
Grace Kloba50c241e2010-04-20 11:07:50 -070043import android.view.ViewStub;
Ben Murdochc42addf2010-01-28 15:19:59 +000044import android.webkit.ConsoleMessage;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -050045import android.webkit.DownloadListener;
Grace Kloba22ac16e2009-10-07 18:00:23 -070046import android.webkit.GeolocationPermissions;
47import android.webkit.HttpAuthHandler;
48import android.webkit.SslErrorHandler;
49import android.webkit.URLUtil;
50import android.webkit.ValueCallback;
51import android.webkit.WebBackForwardList;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -050052import android.webkit.WebBackForwardListClient;
Grace Kloba22ac16e2009-10-07 18:00:23 -070053import android.webkit.WebChromeClient;
54import android.webkit.WebHistoryItem;
Grace Kloba22ac16e2009-10-07 18:00:23 -070055import android.webkit.WebStorage;
56import android.webkit.WebView;
57import android.webkit.WebViewClient;
58import android.widget.FrameLayout;
Grace Kloba22ac16e2009-10-07 18:00:23 -070059import android.widget.LinearLayout;
60import android.widget.TextView;
Ben Murdoch8029a772010-11-16 11:58:21 +000061import android.widget.Toast;
Grace Kloba22ac16e2009-10-07 18:00:23 -070062
Michael Kolbfe251992010-07-08 15:41:55 -070063import java.util.ArrayList;
64import java.util.HashMap;
65import java.util.Iterator;
66import java.util.LinkedList;
67import java.util.Map;
68import java.util.Vector;
69
Grace Kloba22ac16e2009-10-07 18:00:23 -070070/**
71 * Class for maintaining Tabs with a main WebView and a subwindow.
72 */
73class Tab {
Michael Kolb8233fac2010-10-26 16:08:53 -070074
Grace Kloba22ac16e2009-10-07 18:00:23 -070075 // Log Tag
76 private static final String LOGTAG = "Tab";
Ben Murdochc42addf2010-01-28 15:19:59 +000077 // Special case the logtag for messages for the Console to make it easier to
78 // filter them and match the logtag used for these messages in older versions
79 // of the browser.
80 private static final String CONSOLE_LOGTAG = "browser";
81
Michael Kolb8233fac2010-10-26 16:08:53 -070082 final static int LOCK_ICON_UNSECURE = 0;
83 final static int LOCK_ICON_SECURE = 1;
84 final static int LOCK_ICON_MIXED = 2;
85
86 Activity mActivity;
87 private WebViewController mWebViewController;
88
Grace Kloba22ac16e2009-10-07 18:00:23 -070089 // The Geolocation permissions prompt
90 private GeolocationPermissionsPrompt mGeolocationPermissionsPrompt;
91 // Main WebView wrapper
Michael Kolba713ec82010-11-29 17:27:06 -080092 private View mContainer;
Grace Kloba22ac16e2009-10-07 18:00:23 -070093 // Main WebView
94 private WebView mMainView;
95 // Subwindow container
96 private View mSubViewContainer;
97 // Subwindow WebView
98 private WebView mSubView;
99 // Saved bundle for when we are running low on memory. It contains the
100 // information needed to restore the WebView if the user goes back to the
101 // tab.
102 private Bundle mSavedState;
103 // Data used when displaying the tab in the picker.
104 private PickerData mPickerData;
105 // Parent Tab. This is the Tab that created this Tab, or null if the Tab was
106 // created by the UI
107 private Tab mParentTab;
108 // Tab that constructed by this Tab. This is used when this Tab is
109 // destroyed, it clears all mParentTab values in the children.
110 private Vector<Tab> mChildTabs;
111 // If true, the tab will be removed when back out of the first page.
112 private boolean mCloseOnExit;
113 // If true, the tab is in the foreground of the current activity.
114 private boolean mInForeground;
Michael Kolb8233fac2010-10-26 16:08:53 -0700115 // If true, the tab is in page loading state (after onPageStarted,
116 // before onPageFinsihed)
117 private boolean mInPageLoad;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000118 // The time the load started, used to find load page time
119 private long mLoadStartTime;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700120 // Application identifier used to find tabs that another application wants
121 // to reuse.
122 private String mAppId;
123 // Keep the original url around to avoid killing the old WebView if the url
124 // has not changed.
125 private String mOriginalUrl;
Michael Kolb8233fac2010-10-26 16:08:53 -0700126 // Hold on to the currently loaded url
127 private String mCurrentUrl;
128 //The currently loaded title
129 private String mCurrentTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700130 // Error console for the tab
131 private ErrorConsoleView mErrorConsole;
132 // the lock icon type and previous lock icon type for the tab
133 private int mLockIconType;
134 private int mPrevLockIconType;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -0500135 // The listener that gets invoked when a download is started from the
136 // mMainView
137 private final DownloadListener mDownloadListener;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500138 // Listener used to know when we move forward or back in the history list.
139 private final WebBackForwardListClient mWebBackForwardListClient;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700140
141 // AsyncTask for downloading touch icons
142 DownloadTouchIcon mTouchIconLoader;
143
144 // Extra saved information for displaying the tab in the picker.
145 private static class PickerData {
146 String mUrl;
147 String mTitle;
148 Bitmap mFavicon;
149 }
150
Leon Scroggins4cd97792010-12-03 15:31:56 -0500151 // Whether or not the currently shown page is a bookmarked site. Will be
152 // out of date when loading a new page until the mBookmarkAsyncTask returns.
153 private boolean mIsBookmarkedSite;
154 // Used to determine whether the current site is bookmarked.
155 private AsyncTask<Void, Void, Boolean> mBookmarkAsyncTask;
156
157 public boolean isBookmarkedSite() { return mIsBookmarkedSite; }
158
Grace Kloba22ac16e2009-10-07 18:00:23 -0700159 // Used for saving and restoring each Tab
160 static final String WEBVIEW = "webview";
161 static final String NUMTABS = "numTabs";
162 static final String CURRTAB = "currentTab";
163 static final String CURRURL = "currentUrl";
164 static final String CURRTITLE = "currentTitle";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700165 static final String CLOSEONEXIT = "closeonexit";
166 static final String PARENTTAB = "parentTab";
167 static final String APPID = "appid";
168 static final String ORIGINALURL = "originalUrl";
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700169 static final String INCOGNITO = "privateBrowsingEnabled";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700170
171 // -------------------------------------------------------------------------
172
Leon Scroggins58d56c62010-01-28 15:12:40 -0500173 /**
174 * Private information regarding the latest voice search. If the Tab is not
175 * in voice search mode, this will be null.
176 */
177 private VoiceSearchData mVoiceSearchData;
178 /**
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400179 * Remove voice search mode from this tab.
180 */
181 public void revertVoiceSearchMode() {
182 if (mVoiceSearchData != null) {
183 mVoiceSearchData = null;
184 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700185 mWebViewController.revertVoiceSearchMode(this);
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400186 }
187 }
188 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700189
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400190 /**
Leon Scroggins58d56c62010-01-28 15:12:40 -0500191 * Return whether the tab is in voice search mode.
192 */
193 public boolean isInVoiceSearchMode() {
194 return mVoiceSearchData != null;
195 }
196 /**
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400197 * Return true if the Tab is in voice search mode and the voice search
198 * Intent came with a String identifying that Google provided the Intent.
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500199 */
200 public boolean voiceSearchSourceIsGoogle() {
201 return mVoiceSearchData != null && mVoiceSearchData.mSourceIsGoogle;
202 }
203 /**
Leon Scroggins58d56c62010-01-28 15:12:40 -0500204 * Get the title to display for the current voice search page. If the Tab
205 * is not in voice search mode, return null.
206 */
207 public String getVoiceDisplayTitle() {
208 if (mVoiceSearchData == null) return null;
209 return mVoiceSearchData.mLastVoiceSearchTitle;
210 }
211 /**
212 * Get the latest array of voice search results, to be passed to the
213 * BrowserProvider. If the Tab is not in voice search mode, return null.
214 */
215 public ArrayList<String> getVoiceSearchResults() {
216 if (mVoiceSearchData == null) return null;
217 return mVoiceSearchData.mVoiceSearchResults;
218 }
219 /**
220 * Activate voice search mode.
221 * @param intent Intent which has the results to use, or an index into the
222 * results when reusing the old results.
223 */
224 /* package */ void activateVoiceSearchMode(Intent intent) {
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500225 int index = 0;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500226 ArrayList<String> results = intent.getStringArrayListExtra(
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -0500227 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_STRINGS);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500228 if (results != null) {
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500229 ArrayList<String> urls = intent.getStringArrayListExtra(
230 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_URLS);
231 ArrayList<String> htmls = intent.getStringArrayListExtra(
232 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_HTML);
233 ArrayList<String> baseUrls = intent.getStringArrayListExtra(
234 RecognizerResultsIntent
235 .EXTRA_VOICE_SEARCH_RESULT_HTML_BASE_URLS);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500236 // This tab is now entering voice search mode for the first time, or
237 // a new voice search was done.
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500238 int size = results.size();
239 if (urls == null || size != urls.size()) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500240 throw new AssertionError("improper extras passed in Intent");
241 }
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500242 if (htmls == null || htmls.size() != size || baseUrls == null ||
243 (baseUrls.size() != size && baseUrls.size() != 1)) {
244 // If either of these arrays are empty/incorrectly sized, ignore
245 // them.
246 htmls = null;
247 baseUrls = null;
248 }
249 mVoiceSearchData = new VoiceSearchData(results, urls, htmls,
250 baseUrls);
Leon Scroggins9df94972010-03-08 18:20:35 -0500251 mVoiceSearchData.mHeaders = intent.getParcelableArrayListExtra(
252 RecognizerResultsIntent
253 .EXTRA_VOICE_SEARCH_RESULT_HTTP_HEADERS);
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500254 mVoiceSearchData.mSourceIsGoogle = intent.getBooleanExtra(
255 VoiceSearchData.SOURCE_IS_GOOGLE, false);
Leon Scroggins2ee4a5a2010-03-15 16:56:57 -0400256 mVoiceSearchData.mVoiceSearchIntent = new Intent(intent);
Leon Scrogginse10dde52010-03-08 19:53:03 -0500257 }
258 String extraData = intent.getStringExtra(
259 SearchManager.EXTRA_DATA_KEY);
260 if (extraData != null) {
261 index = Integer.parseInt(extraData);
262 if (index >= mVoiceSearchData.mVoiceSearchResults.size()) {
263 throw new AssertionError("index must be less than "
264 + "size of mVoiceSearchResults");
265 }
266 if (mVoiceSearchData.mSourceIsGoogle) {
267 Intent logIntent = new Intent(
268 LoggingEvents.ACTION_LOG_EVENT);
269 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
270 LoggingEvents.VoiceSearch.N_BEST_CHOOSE);
271 logIntent.putExtra(
272 LoggingEvents.VoiceSearch.EXTRA_N_BEST_CHOOSE_INDEX,
273 index);
274 mActivity.sendBroadcast(logIntent);
275 }
276 if (mVoiceSearchData.mVoiceSearchIntent != null) {
Leon Scroggins2ee4a5a2010-03-15 16:56:57 -0400277 // Copy the Intent, so that each history item will have its own
278 // Intent, with different (or none) extra data.
279 Intent latest = new Intent(mVoiceSearchData.mVoiceSearchIntent);
280 latest.putExtra(SearchManager.EXTRA_DATA_KEY, extraData);
281 mVoiceSearchData.mVoiceSearchIntent = latest;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500282 }
283 }
284 mVoiceSearchData.mLastVoiceSearchTitle
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500285 = mVoiceSearchData.mVoiceSearchResults.get(index);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500286 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700287 mWebViewController.activateVoiceSearchMode(mVoiceSearchData.mLastVoiceSearchTitle);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500288 }
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500289 if (mVoiceSearchData.mVoiceSearchHtmls != null) {
290 // When index was found it was already ensured that it was valid
291 String uriString = mVoiceSearchData.mVoiceSearchHtmls.get(index);
292 if (uriString != null) {
293 Uri dataUri = Uri.parse(uriString);
294 if (RecognizerResultsIntent.URI_SCHEME_INLINE.equals(
295 dataUri.getScheme())) {
296 // If there is only one base URL, use it. If there are
297 // more, there will be one for each index, so use the base
298 // URL corresponding to the index.
299 String baseUrl = mVoiceSearchData.mVoiceSearchBaseUrls.get(
300 mVoiceSearchData.mVoiceSearchBaseUrls.size() > 1 ?
301 index : 0);
302 mVoiceSearchData.mLastVoiceSearchUrl = baseUrl;
303 mMainView.loadDataWithBaseURL(baseUrl,
304 uriString.substring(RecognizerResultsIntent
305 .URI_SCHEME_INLINE.length() + 1), "text/html",
306 "utf-8", baseUrl);
307 return;
308 }
309 }
310 }
Leon Scroggins58d56c62010-01-28 15:12:40 -0500311 mVoiceSearchData.mLastVoiceSearchUrl
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500312 = mVoiceSearchData.mVoiceSearchUrls.get(index);
313 if (null == mVoiceSearchData.mLastVoiceSearchUrl) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700314 mVoiceSearchData.mLastVoiceSearchUrl = UrlUtils.smartUrlFilter(
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500315 mVoiceSearchData.mLastVoiceSearchTitle);
316 }
Leon Scroggins9df94972010-03-08 18:20:35 -0500317 Map<String, String> headers = null;
318 if (mVoiceSearchData.mHeaders != null) {
319 int bundleIndex = mVoiceSearchData.mHeaders.size() == 1 ? 0
320 : index;
321 Bundle bundle = mVoiceSearchData.mHeaders.get(bundleIndex);
322 if (bundle != null && !bundle.isEmpty()) {
323 Iterator<String> iter = bundle.keySet().iterator();
324 headers = new HashMap<String, String>();
325 while (iter.hasNext()) {
326 String key = iter.next();
327 headers.put(key, bundle.getString(key));
328 }
329 }
330 }
331 mMainView.loadUrl(mVoiceSearchData.mLastVoiceSearchUrl, headers);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500332 }
333 /* package */ static class VoiceSearchData {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500334 public VoiceSearchData(ArrayList<String> results,
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500335 ArrayList<String> urls, ArrayList<String> htmls,
336 ArrayList<String> baseUrls) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500337 mVoiceSearchResults = results;
338 mVoiceSearchUrls = urls;
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500339 mVoiceSearchHtmls = htmls;
340 mVoiceSearchBaseUrls = baseUrls;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500341 }
342 /*
343 * ArrayList of suggestions to be displayed when opening the
344 * SearchManager
345 */
346 public ArrayList<String> mVoiceSearchResults;
347 /*
348 * ArrayList of urls, associated with the suggestions in
349 * mVoiceSearchResults.
350 */
351 public ArrayList<String> mVoiceSearchUrls;
352 /*
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500353 * ArrayList holding content to load for each item in
354 * mVoiceSearchResults.
355 */
356 public ArrayList<String> mVoiceSearchHtmls;
357 /*
358 * ArrayList holding base urls for the items in mVoiceSearchResults.
359 * If non null, this will either have the same size as
360 * mVoiceSearchResults or have a size of 1, in which case all will use
361 * the same base url
362 */
363 public ArrayList<String> mVoiceSearchBaseUrls;
364 /*
Leon Scroggins58d56c62010-01-28 15:12:40 -0500365 * The last url provided by voice search. Used for comparison to see if
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500366 * we are going to a page by some method besides voice search.
Leon Scroggins58d56c62010-01-28 15:12:40 -0500367 */
368 public String mLastVoiceSearchUrl;
369 /**
370 * The last title used for voice search. Needed to update the title bar
371 * when switching tabs.
372 */
373 public String mLastVoiceSearchTitle;
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500374 /**
375 * Whether the Intent which turned on voice search mode contained the
376 * String signifying that Google was the source.
377 */
378 public boolean mSourceIsGoogle;
379 /**
Leon Scroggins9df94972010-03-08 18:20:35 -0500380 * List of headers to be passed into the WebView containing location
381 * information
382 */
383 public ArrayList<Bundle> mHeaders;
384 /**
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500385 * The Intent used to invoke voice search. Placed on the
386 * WebHistoryItem so that when coming back to a previous voice search
387 * page we can again activate voice search.
388 */
Leon Scrogginse10dde52010-03-08 19:53:03 -0500389 public Intent mVoiceSearchIntent;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500390 /**
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500391 * String used to identify Google as the source of voice search.
392 */
393 public static String SOURCE_IS_GOOGLE
394 = "android.speech.extras.SOURCE_IS_GOOGLE";
Leon Scroggins58d56c62010-01-28 15:12:40 -0500395 }
396
Grace Kloba22ac16e2009-10-07 18:00:23 -0700397 // Container class for the next error dialog that needs to be displayed
398 private class ErrorDialog {
399 public final int mTitle;
400 public final String mDescription;
401 public final int mError;
402 ErrorDialog(int title, String desc, int error) {
403 mTitle = title;
404 mDescription = desc;
405 mError = error;
406 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700407 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700408
409 private void processNextError() {
410 if (mQueuedErrors == null) {
411 return;
412 }
413 // The first one is currently displayed so just remove it.
414 mQueuedErrors.removeFirst();
415 if (mQueuedErrors.size() == 0) {
416 mQueuedErrors = null;
417 return;
418 }
419 showError(mQueuedErrors.getFirst());
420 }
421
422 private DialogInterface.OnDismissListener mDialogListener =
423 new DialogInterface.OnDismissListener() {
424 public void onDismiss(DialogInterface d) {
425 processNextError();
426 }
427 };
428 private LinkedList<ErrorDialog> mQueuedErrors;
429
430 private void queueError(int err, String desc) {
431 if (mQueuedErrors == null) {
432 mQueuedErrors = new LinkedList<ErrorDialog>();
433 }
434 for (ErrorDialog d : mQueuedErrors) {
435 if (d.mError == err) {
436 // Already saw a similar error, ignore the new one.
437 return;
438 }
439 }
440 ErrorDialog errDialog = new ErrorDialog(
441 err == WebViewClient.ERROR_FILE_NOT_FOUND ?
442 R.string.browserFrameFileErrorLabel :
443 R.string.browserFrameNetworkErrorLabel,
444 desc, err);
445 mQueuedErrors.addLast(errDialog);
446
447 // Show the dialog now if the queue was empty and it is in foreground
448 if (mQueuedErrors.size() == 1 && mInForeground) {
449 showError(errDialog);
450 }
451 }
452
453 private void showError(ErrorDialog errDialog) {
454 if (mInForeground) {
455 AlertDialog d = new AlertDialog.Builder(mActivity)
456 .setTitle(errDialog.mTitle)
457 .setMessage(errDialog.mDescription)
458 .setPositiveButton(R.string.ok, null)
459 .create();
460 d.setOnDismissListener(mDialogListener);
461 d.show();
462 }
463 }
464
465 // -------------------------------------------------------------------------
466 // WebViewClient implementation for the main WebView
467 // -------------------------------------------------------------------------
468
469 private final WebViewClient mWebViewClient = new WebViewClient() {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500470 private Message mDontResend;
471 private Message mResend;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700472 @Override
473 public void onPageStarted(WebView view, String url, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700474 mInPageLoad = true;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000475 mLoadStartTime = SystemClock.uptimeMillis();
Leon Scroggins58d56c62010-01-28 15:12:40 -0500476 if (mVoiceSearchData != null
477 && !url.equals(mVoiceSearchData.mLastVoiceSearchUrl)) {
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500478 if (mVoiceSearchData.mSourceIsGoogle) {
479 Intent i = new Intent(LoggingEvents.ACTION_LOG_EVENT);
480 i.putExtra(LoggingEvents.EXTRA_FLUSH, true);
481 mActivity.sendBroadcast(i);
482 }
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400483 revertVoiceSearchMode();
Leon Scroggins58d56c62010-01-28 15:12:40 -0500484 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700485
Grace Kloba22ac16e2009-10-07 18:00:23 -0700486
487 // If we start a touch icon load and then load a new page, we don't
488 // want to cancel the current touch icon loader. But, we do want to
489 // create a new one when the touch icon url is known.
490 if (mTouchIconLoader != null) {
491 mTouchIconLoader.mTab = null;
492 mTouchIconLoader = null;
493 }
494
495 // reset the error console
496 if (mErrorConsole != null) {
497 mErrorConsole.clearErrorMessages();
Michael Kolb8233fac2010-10-26 16:08:53 -0700498 if (mWebViewController.shouldShowErrorConsole()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700499 mErrorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
500 }
501 }
502
Grace Kloba22ac16e2009-10-07 18:00:23 -0700503 // finally update the UI in the activity if it is in the foreground
Michael Kolb8233fac2010-10-26 16:08:53 -0700504 mWebViewController.onPageStarted(Tab.this, view, url, favicon);
Leon Scroggins4cd97792010-12-03 15:31:56 -0500505
Leon Scroggins1961ed22010-12-07 15:22:21 -0500506 updateBookmarkedStatusForUrl(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700507 }
508
509 @Override
510 public void onPageFinished(WebView view, String url) {
John Reck5b691842010-11-29 11:21:13 -0800511 if (!isPrivateBrowsingEnabled()) {
512 LogTag.logPageFinishedLoading(
513 url, SystemClock.uptimeMillis() - mLoadStartTime);
514 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700515 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700516
Michael Kolb8233fac2010-10-26 16:08:53 -0700517 mWebViewController.onPageFinished(Tab.this, url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700518 }
519
520 // return true if want to hijack the url to let another app to handle it
521 @Override
522 public boolean shouldOverrideUrlLoading(WebView view, String url) {
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400523 if (voiceSearchSourceIsGoogle()) {
524 // This method is called when the user clicks on a link.
525 // VoiceSearchMode is turned off when the user leaves the
526 // Google results page, so at this point the user must be on
527 // that page. If the user clicked a link on that page, assume
528 // that the voice search was effective, and broadcast an Intent
529 // so a receiver can take note of that fact.
530 Intent logIntent = new Intent(LoggingEvents.ACTION_LOG_EVENT);
531 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
532 LoggingEvents.VoiceSearch.RESULT_CLICKED);
533 mActivity.sendBroadcast(logIntent);
534 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700535 if (mInForeground) {
Michael Kolb18eb3772010-12-10 14:29:51 -0800536 return mWebViewController.shouldOverrideUrlLoading(Tab.this,
537 view, url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700538 } else {
539 return false;
540 }
541 }
542
543 /**
544 * Updates the lock icon. This method is called when we discover another
545 * resource to be loaded for this page (for example, javascript). While
546 * we update the icon type, we do not update the lock icon itself until
547 * we are done loading, it is slightly more secure this way.
548 */
549 @Override
550 public void onLoadResource(WebView view, String url) {
551 if (url != null && url.length() > 0) {
552 // It is only if the page claims to be secure that we may have
553 // to update the lock:
Michael Kolb8233fac2010-10-26 16:08:53 -0700554 if (mLockIconType == LOCK_ICON_SECURE) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700555 // If NOT a 'safe' url, change the lock to mixed content!
556 if (!(URLUtil.isHttpsUrl(url) || URLUtil.isDataUrl(url)
557 || URLUtil.isAboutUrl(url))) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700558 mLockIconType = LOCK_ICON_MIXED;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700559 }
560 }
561 }
562 }
563
564 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700565 * Show a dialog informing the user of the network error reported by
566 * WebCore if it is in the foreground.
567 */
568 @Override
569 public void onReceivedError(WebView view, int errorCode,
570 String description, String failingUrl) {
571 if (errorCode != WebViewClient.ERROR_HOST_LOOKUP &&
572 errorCode != WebViewClient.ERROR_CONNECT &&
573 errorCode != WebViewClient.ERROR_BAD_URL &&
574 errorCode != WebViewClient.ERROR_UNSUPPORTED_SCHEME &&
575 errorCode != WebViewClient.ERROR_FILE) {
576 queueError(errorCode, description);
577 }
Jeff Hamilton47654f42010-09-07 09:57:51 -0500578
579 // Don't log URLs when in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -0700580 if (!isPrivateBrowsingEnabled()) {
Jeff Hamilton47654f42010-09-07 09:57:51 -0500581 Log.e(LOGTAG, "onReceivedError " + errorCode + " " + failingUrl
582 + " " + description);
583 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700584
585 // We need to reset the title after an error if it is in foreground.
586 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700587 mWebViewController.resetTitleAndRevertLockIcon(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700588 }
589 }
590
591 /**
592 * Check with the user if it is ok to resend POST data as the page they
593 * are trying to navigate to is the result of a POST.
594 */
595 @Override
596 public void onFormResubmission(WebView view, final Message dontResend,
597 final Message resend) {
598 if (!mInForeground) {
599 dontResend.sendToTarget();
600 return;
601 }
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500602 if (mDontResend != null) {
603 Log.w(LOGTAG, "onFormResubmission should not be called again "
604 + "while dialog is still up");
605 dontResend.sendToTarget();
606 return;
607 }
608 mDontResend = dontResend;
609 mResend = resend;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700610 new AlertDialog.Builder(mActivity).setTitle(
611 R.string.browserFrameFormResubmitLabel).setMessage(
612 R.string.browserFrameFormResubmitMessage)
613 .setPositiveButton(R.string.ok,
614 new DialogInterface.OnClickListener() {
615 public void onClick(DialogInterface dialog,
616 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500617 if (mResend != null) {
618 mResend.sendToTarget();
619 mResend = null;
620 mDontResend = null;
621 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700622 }
623 }).setNegativeButton(R.string.cancel,
624 new DialogInterface.OnClickListener() {
625 public void onClick(DialogInterface dialog,
626 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500627 if (mDontResend != null) {
628 mDontResend.sendToTarget();
629 mResend = null;
630 mDontResend = null;
631 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700632 }
633 }).setOnCancelListener(new OnCancelListener() {
634 public void onCancel(DialogInterface dialog) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500635 if (mDontResend != null) {
636 mDontResend.sendToTarget();
637 mResend = null;
638 mDontResend = null;
639 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700640 }
641 }).show();
642 }
643
644 /**
645 * Insert the url into the visited history database.
646 * @param url The url to be inserted.
647 * @param isReload True if this url is being reloaded.
648 * FIXME: Not sure what to do when reloading the page.
649 */
650 @Override
651 public void doUpdateVisitedHistory(WebView view, String url,
652 boolean isReload) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700653 mWebViewController.doUpdateVisitedHistory(Tab.this, url, isReload);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700654 }
655
656 /**
657 * Displays SSL error(s) dialog to the user.
658 */
659 @Override
660 public void onReceivedSslError(final WebView view,
661 final SslErrorHandler handler, final SslError error) {
662 if (!mInForeground) {
663 handler.cancel();
664 return;
665 }
666 if (BrowserSettings.getInstance().showSecurityWarnings()) {
667 final LayoutInflater factory =
668 LayoutInflater.from(mActivity);
669 final View warningsView =
670 factory.inflate(R.layout.ssl_warnings, null);
671 final LinearLayout placeholder =
672 (LinearLayout)warningsView.findViewById(R.id.placeholder);
673
674 if (error.hasError(SslError.SSL_UNTRUSTED)) {
675 LinearLayout ll = (LinearLayout)factory
676 .inflate(R.layout.ssl_warning, null);
677 ((TextView)ll.findViewById(R.id.warning))
678 .setText(R.string.ssl_untrusted);
679 placeholder.addView(ll);
680 }
681
682 if (error.hasError(SslError.SSL_IDMISMATCH)) {
683 LinearLayout ll = (LinearLayout)factory
684 .inflate(R.layout.ssl_warning, null);
685 ((TextView)ll.findViewById(R.id.warning))
686 .setText(R.string.ssl_mismatch);
687 placeholder.addView(ll);
688 }
689
690 if (error.hasError(SslError.SSL_EXPIRED)) {
691 LinearLayout ll = (LinearLayout)factory
692 .inflate(R.layout.ssl_warning, null);
693 ((TextView)ll.findViewById(R.id.warning))
694 .setText(R.string.ssl_expired);
695 placeholder.addView(ll);
696 }
697
698 if (error.hasError(SslError.SSL_NOTYETVALID)) {
699 LinearLayout ll = (LinearLayout)factory
700 .inflate(R.layout.ssl_warning, null);
701 ((TextView)ll.findViewById(R.id.warning))
702 .setText(R.string.ssl_not_yet_valid);
703 placeholder.addView(ll);
704 }
705
706 new AlertDialog.Builder(mActivity).setTitle(
707 R.string.security_warning).setIcon(
708 android.R.drawable.ic_dialog_alert).setView(
709 warningsView).setPositiveButton(R.string.ssl_continue,
710 new DialogInterface.OnClickListener() {
711 public void onClick(DialogInterface dialog,
712 int whichButton) {
713 handler.proceed();
714 }
715 }).setNeutralButton(R.string.view_certificate,
716 new DialogInterface.OnClickListener() {
717 public void onClick(DialogInterface dialog,
718 int whichButton) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700719 mWebViewController.showSslCertificateOnError(view,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700720 handler, error);
721 }
Ben Murdocha49b8292010-11-16 11:56:04 +0000722 }).setNegativeButton(R.string.ssl_go_back,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700723 new DialogInterface.OnClickListener() {
724 public void onClick(DialogInterface dialog,
725 int whichButton) {
726 handler.cancel();
Michael Kolb8233fac2010-10-26 16:08:53 -0700727 mWebViewController.resetTitleAndRevertLockIcon(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700728 }
729 }).setOnCancelListener(
730 new DialogInterface.OnCancelListener() {
731 public void onCancel(DialogInterface dialog) {
732 handler.cancel();
Michael Kolb8233fac2010-10-26 16:08:53 -0700733 mWebViewController.resetTitleAndRevertLockIcon(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700734 }
735 }).show();
736 } else {
737 handler.proceed();
738 }
739 }
740
741 /**
742 * Handles an HTTP authentication request.
743 *
744 * @param handler The authentication handler
745 * @param host The host
746 * @param realm The realm
747 */
748 @Override
749 public void onReceivedHttpAuthRequest(WebView view,
750 final HttpAuthHandler handler, final String host,
751 final String realm) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700752 mWebViewController.onReceivedHttpAuthRequest(Tab.this, view, handler, host, realm);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700753 }
754
755 @Override
756 public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
757 if (!mInForeground) {
758 return false;
759 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700760 return mWebViewController.shouldOverrideKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700761 }
762
763 @Override
764 public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700765 if (!mInForeground) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700766 return;
767 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700768 mWebViewController.onUnhandledKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700769 }
770 };
771
772 // -------------------------------------------------------------------------
773 // WebChromeClient implementation for the main WebView
774 // -------------------------------------------------------------------------
775
776 private final WebChromeClient mWebChromeClient = new WebChromeClient() {
777 // Helper method to create a new tab or sub window.
778 private void createWindow(final boolean dialog, final Message msg) {
779 WebView.WebViewTransport transport =
780 (WebView.WebViewTransport) msg.obj;
781 if (dialog) {
782 createSubWindow();
Michael Kolb8233fac2010-10-26 16:08:53 -0700783 mWebViewController.attachSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700784 transport.setWebView(mSubView);
785 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -0700786 final Tab newTab = mWebViewController.openTabAndShow(
Michael Kolb18eb3772010-12-10 14:29:51 -0800787 Tab.this,
Michael Kolb8233fac2010-10-26 16:08:53 -0700788 IntentHandler.EMPTY_URL_DATA, false, null);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700789 if (newTab != Tab.this) {
790 Tab.this.addChildTab(newTab);
791 }
792 transport.setWebView(newTab.getWebView());
793 }
794 msg.sendToTarget();
795 }
796
797 @Override
798 public boolean onCreateWindow(WebView view, final boolean dialog,
799 final boolean userGesture, final Message resultMsg) {
800 // only allow new window or sub window for the foreground case
801 if (!mInForeground) {
802 return false;
803 }
804 // Short-circuit if we can't create any more tabs or sub windows.
805 if (dialog && mSubView != null) {
806 new AlertDialog.Builder(mActivity)
807 .setTitle(R.string.too_many_subwindows_dialog_title)
808 .setIcon(android.R.drawable.ic_dialog_alert)
809 .setMessage(R.string.too_many_subwindows_dialog_message)
810 .setPositiveButton(R.string.ok, null)
811 .show();
812 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700813 } else if (!mWebViewController.getTabControl().canCreateNewTab()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700814 new AlertDialog.Builder(mActivity)
815 .setTitle(R.string.too_many_windows_dialog_title)
816 .setIcon(android.R.drawable.ic_dialog_alert)
817 .setMessage(R.string.too_many_windows_dialog_message)
818 .setPositiveButton(R.string.ok, null)
819 .show();
820 return false;
821 }
822
823 // Short-circuit if this was a user gesture.
824 if (userGesture) {
825 createWindow(dialog, resultMsg);
826 return true;
827 }
828
829 // Allow the popup and create the appropriate window.
830 final AlertDialog.OnClickListener allowListener =
831 new AlertDialog.OnClickListener() {
832 public void onClick(DialogInterface d,
833 int which) {
834 createWindow(dialog, resultMsg);
835 }
836 };
837
838 // Block the popup by returning a null WebView.
839 final AlertDialog.OnClickListener blockListener =
840 new AlertDialog.OnClickListener() {
841 public void onClick(DialogInterface d, int which) {
842 resultMsg.sendToTarget();
843 }
844 };
845
846 // Build a confirmation dialog to display to the user.
847 final AlertDialog d =
848 new AlertDialog.Builder(mActivity)
849 .setTitle(R.string.attention)
850 .setIcon(android.R.drawable.ic_dialog_alert)
851 .setMessage(R.string.popup_window_attempt)
852 .setPositiveButton(R.string.allow, allowListener)
853 .setNegativeButton(R.string.block, blockListener)
854 .setCancelable(false)
855 .create();
856
857 // Show the confirmation dialog.
858 d.show();
859 return true;
860 }
861
862 @Override
Patrick Scotteb5061b2009-11-18 15:00:30 -0500863 public void onRequestFocus(WebView view) {
864 if (!mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700865 mWebViewController.switchToTab(mWebViewController.getTabControl().getTabIndex(
Patrick Scotteb5061b2009-11-18 15:00:30 -0500866 Tab.this));
867 }
868 }
869
870 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700871 public void onCloseWindow(WebView window) {
872 if (mParentTab != null) {
873 // JavaScript can only close popup window.
874 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700875 mWebViewController.switchToTab(mWebViewController.getTabControl()
Grace Kloba22ac16e2009-10-07 18:00:23 -0700876 .getTabIndex(mParentTab));
877 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700878 mWebViewController.closeTab(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700879 }
880 }
881
882 @Override
883 public void onProgressChanged(WebView view, int newProgress) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700884 mWebViewController.onProgressChanged(Tab.this, newProgress);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700885 }
886
887 @Override
Leon Scroggins21d9b902010-03-11 09:33:11 -0500888 public void onReceivedTitle(WebView view, final String title) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700889 mWebViewController.onReceivedTitle(Tab.this, title);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700890 }
891
892 @Override
893 public void onReceivedIcon(WebView view, Bitmap icon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700894 mWebViewController.onFavicon(Tab.this, view, icon);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700895 }
896
897 @Override
898 public void onReceivedTouchIconUrl(WebView view, String url,
899 boolean precomposed) {
900 final ContentResolver cr = mActivity.getContentResolver();
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400901 // Let precomposed icons take precedence over non-composed
902 // icons.
903 if (precomposed && mTouchIconLoader != null) {
904 mTouchIconLoader.cancel(false);
905 mTouchIconLoader = null;
906 }
907 // Have only one async task at a time.
908 if (mTouchIconLoader == null) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700909 mTouchIconLoader = new DownloadTouchIcon(Tab.this,
910 mActivity, cr, view);
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400911 mTouchIconLoader.execute(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700912 }
913 }
914
915 @Override
916 public void onShowCustomView(View view,
917 WebChromeClient.CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700918 if (mInForeground) mWebViewController.showCustomView(Tab.this, view,
919 callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700920 }
921
922 @Override
923 public void onHideCustomView() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700924 if (mInForeground) mWebViewController.hideCustomView();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700925 }
926
927 /**
928 * The origin has exceeded its database quota.
929 * @param url the URL that exceeded the quota
930 * @param databaseIdentifier the identifier of the database on which the
931 * transaction that caused the quota overflow was run
932 * @param currentQuota the current quota for the origin.
933 * @param estimatedSize the estimated size of the database.
934 * @param totalUsedQuota is the sum of all origins' quota.
935 * @param quotaUpdater The callback to run when a decision to allow or
936 * deny quota has been made. Don't forget to call this!
937 */
938 @Override
939 public void onExceededDatabaseQuota(String url,
940 String databaseIdentifier, long currentQuota, long estimatedSize,
941 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
942 BrowserSettings.getInstance().getWebStorageSizeManager()
943 .onExceededDatabaseQuota(url, databaseIdentifier,
944 currentQuota, estimatedSize, totalUsedQuota,
945 quotaUpdater);
946 }
947
948 /**
949 * The Application Cache has exceeded its max size.
950 * @param spaceNeeded is the amount of disk space that would be needed
951 * in order for the last appcache operation to succeed.
952 * @param totalUsedQuota is the sum of all origins' quota.
953 * @param quotaUpdater A callback to inform the WebCore thread that a
954 * new app cache size is available. This callback must always
955 * be executed at some point to ensure that the sleeping
956 * WebCore thread is woken up.
957 */
958 @Override
959 public void onReachedMaxAppCacheSize(long spaceNeeded,
960 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
961 BrowserSettings.getInstance().getWebStorageSizeManager()
962 .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota,
963 quotaUpdater);
964 }
965
966 /**
967 * Instructs the browser to show a prompt to ask the user to set the
968 * Geolocation permission state for the specified origin.
969 * @param origin The origin for which Geolocation permissions are
970 * requested.
971 * @param callback The callback to call once the user has set the
972 * Geolocation permission state.
973 */
974 @Override
975 public void onGeolocationPermissionsShowPrompt(String origin,
976 GeolocationPermissions.Callback callback) {
977 if (mInForeground) {
Grace Kloba50c241e2010-04-20 11:07:50 -0700978 getGeolocationPermissionsPrompt().show(origin, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700979 }
980 }
981
982 /**
983 * Instructs the browser to hide the Geolocation permissions prompt.
984 */
985 @Override
986 public void onGeolocationPermissionsHidePrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -0700987 if (mInForeground && mGeolocationPermissionsPrompt != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700988 mGeolocationPermissionsPrompt.hide();
989 }
990 }
991
Ben Murdoch65acc352009-11-19 18:16:04 +0000992 /* Adds a JavaScript error message to the system log and if the JS
993 * console is enabled in the about:debug options, to that console
994 * also.
Ben Murdochc42addf2010-01-28 15:19:59 +0000995 * @param consoleMessage the message object.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700996 */
997 @Override
Ben Murdochc42addf2010-01-28 15:19:59 +0000998 public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700999 if (mInForeground) {
1000 // call getErrorConsole(true) so it will create one if needed
1001 ErrorConsoleView errorConsole = getErrorConsole(true);
Ben Murdochc42addf2010-01-28 15:19:59 +00001002 errorConsole.addErrorMessage(consoleMessage);
Michael Kolb8233fac2010-10-26 16:08:53 -07001003 if (mWebViewController.shouldShowErrorConsole()
1004 && errorConsole.getShowState() !=
1005 ErrorConsoleView.SHOW_MAXIMIZED) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001006 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
1007 }
1008 }
Ben Murdochc42addf2010-01-28 15:19:59 +00001009
Jeff Hamilton47654f42010-09-07 09:57:51 -05001010 // Don't log console messages in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001011 if (isPrivateBrowsingEnabled()) return true;
Jeff Hamilton47654f42010-09-07 09:57:51 -05001012
Ben Murdochc42addf2010-01-28 15:19:59 +00001013 String message = "Console: " + consoleMessage.message() + " "
1014 + consoleMessage.sourceId() + ":"
1015 + consoleMessage.lineNumber();
1016
1017 switch (consoleMessage.messageLevel()) {
1018 case TIP:
1019 Log.v(CONSOLE_LOGTAG, message);
1020 break;
1021 case LOG:
1022 Log.i(CONSOLE_LOGTAG, message);
1023 break;
1024 case WARNING:
1025 Log.w(CONSOLE_LOGTAG, message);
1026 break;
1027 case ERROR:
1028 Log.e(CONSOLE_LOGTAG, message);
1029 break;
1030 case DEBUG:
1031 Log.d(CONSOLE_LOGTAG, message);
1032 break;
1033 }
1034
1035 return true;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001036 }
1037
1038 /**
1039 * Ask the browser for an icon to represent a <video> element.
1040 * This icon will be used if the Web page did not specify a poster attribute.
1041 * @return Bitmap The icon or null if no such icon is available.
1042 */
1043 @Override
1044 public Bitmap getDefaultVideoPoster() {
1045 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001046 return mWebViewController.getDefaultVideoPoster();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001047 }
1048 return null;
1049 }
1050
1051 /**
1052 * Ask the host application for a custom progress view to show while
1053 * a <video> is loading.
1054 * @return View The progress view.
1055 */
1056 @Override
1057 public View getVideoLoadingProgressView() {
1058 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001059 return mWebViewController.getVideoLoadingProgressView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001060 }
1061 return null;
1062 }
1063
1064 @Override
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01001065 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001066 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001067 mWebViewController.openFileChooser(uploadMsg, acceptType);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001068 } else {
1069 uploadMsg.onReceiveValue(null);
1070 }
1071 }
1072
1073 /**
1074 * Deliver a list of already-visited URLs
1075 */
1076 @Override
1077 public void getVisitedHistory(final ValueCallback<String[]> callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001078 mWebViewController.getVisitedHistory(callback);
1079 }
Ben Murdoch8029a772010-11-16 11:58:21 +00001080
1081 @Override
1082 public void setupAutoFill(Message message) {
1083 // Prompt the user to set up their profile.
1084 final Message msg = message;
1085 AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
1086 builder.setMessage(R.string.autofill_setup_dialog_message)
1087 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1088 @Override
1089 public void onClick(DialogInterface dialog, int id) {
1090 // Take user to the AutoFill profile editor. When they return,
1091 // we will send the message that we pass here which will trigger
1092 // the form to get filled out with their new profile.
1093 mWebViewController.setupAutoFill(msg);
1094 }
1095 })
1096 .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
1097 @Override
1098 public void onClick(DialogInterface dialog, int id) {
1099 // Disable autofill and show a toast with how to turn it on again.
1100 BrowserSettings s = BrowserSettings.getInstance();
1101 s.addObserver(mMainView.getSettings());
1102 s.disableAutoFill(mActivity);
1103 s.update();
1104 Toast.makeText(mActivity, R.string.autofill_setup_dialog_negative_toast,
1105 Toast.LENGTH_LONG).show();
1106 }
1107 }).show();
1108 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001109 };
1110
1111 // -------------------------------------------------------------------------
1112 // WebViewClient implementation for the sub window
1113 // -------------------------------------------------------------------------
1114
1115 // Subclass of WebViewClient used in subwindows to notify the main
1116 // WebViewClient of certain WebView activities.
1117 private static class SubWindowClient extends WebViewClient {
1118 // The main WebViewClient.
1119 private final WebViewClient mClient;
Michael Kolb8233fac2010-10-26 16:08:53 -07001120 private final WebViewController mController;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001121
Michael Kolb8233fac2010-10-26 16:08:53 -07001122 SubWindowClient(WebViewClient client, WebViewController controller) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001123 mClient = client;
Michael Kolb8233fac2010-10-26 16:08:53 -07001124 mController = controller;
Leon Scroggins III211ba542010-04-19 13:21:13 -04001125 }
1126 @Override
1127 public void onPageStarted(WebView view, String url, Bitmap favicon) {
1128 // Unlike the others, do not call mClient's version, which would
1129 // change the progress bar. However, we do want to remove the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001130 // find or select dialog.
Michael Kolb8233fac2010-10-26 16:08:53 -07001131 mController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001132 }
1133 @Override
1134 public void doUpdateVisitedHistory(WebView view, String url,
1135 boolean isReload) {
1136 mClient.doUpdateVisitedHistory(view, url, isReload);
1137 }
1138 @Override
1139 public boolean shouldOverrideUrlLoading(WebView view, String url) {
1140 return mClient.shouldOverrideUrlLoading(view, url);
1141 }
1142 @Override
1143 public void onReceivedSslError(WebView view, SslErrorHandler handler,
1144 SslError error) {
1145 mClient.onReceivedSslError(view, handler, error);
1146 }
1147 @Override
1148 public void onReceivedHttpAuthRequest(WebView view,
1149 HttpAuthHandler handler, String host, String realm) {
1150 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
1151 }
1152 @Override
1153 public void onFormResubmission(WebView view, Message dontResend,
1154 Message resend) {
1155 mClient.onFormResubmission(view, dontResend, resend);
1156 }
1157 @Override
1158 public void onReceivedError(WebView view, int errorCode,
1159 String description, String failingUrl) {
1160 mClient.onReceivedError(view, errorCode, description, failingUrl);
1161 }
1162 @Override
1163 public boolean shouldOverrideKeyEvent(WebView view,
1164 android.view.KeyEvent event) {
1165 return mClient.shouldOverrideKeyEvent(view, event);
1166 }
1167 @Override
1168 public void onUnhandledKeyEvent(WebView view,
1169 android.view.KeyEvent event) {
1170 mClient.onUnhandledKeyEvent(view, event);
1171 }
1172 }
1173
1174 // -------------------------------------------------------------------------
1175 // WebChromeClient implementation for the sub window
1176 // -------------------------------------------------------------------------
1177
1178 private class SubWindowChromeClient extends WebChromeClient {
1179 // The main WebChromeClient.
1180 private final WebChromeClient mClient;
1181
1182 SubWindowChromeClient(WebChromeClient client) {
1183 mClient = client;
1184 }
1185 @Override
1186 public void onProgressChanged(WebView view, int newProgress) {
1187 mClient.onProgressChanged(view, newProgress);
1188 }
1189 @Override
1190 public boolean onCreateWindow(WebView view, boolean dialog,
1191 boolean userGesture, android.os.Message resultMsg) {
1192 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
1193 }
1194 @Override
1195 public void onCloseWindow(WebView window) {
1196 if (window != mSubView) {
1197 Log.e(LOGTAG, "Can't close the window");
1198 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001199 mWebViewController.dismissSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001200 }
1201 }
1202
1203 // -------------------------------------------------------------------------
1204
Michael Kolb8233fac2010-10-26 16:08:53 -07001205 // TODO temporarily use activity here
1206 // remove later
1207
Grace Kloba22ac16e2009-10-07 18:00:23 -07001208 // Construct a new tab
Michael Kolb8233fac2010-10-26 16:08:53 -07001209 Tab(WebViewController wvcontroller, WebView w, boolean closeOnExit, String appId,
Grace Kloba22ac16e2009-10-07 18:00:23 -07001210 String url) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001211 mWebViewController = wvcontroller;
1212 mActivity = mWebViewController.getActivity();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001213 mCloseOnExit = closeOnExit;
1214 mAppId = appId;
1215 mOriginalUrl = url;
Michael Kolb8233fac2010-10-26 16:08:53 -07001216 mLockIconType = LOCK_ICON_UNSECURE;
1217 mPrevLockIconType = LOCK_ICON_UNSECURE;
1218 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001219 mInForeground = false;
1220
Grace Kloba22ac16e2009-10-07 18:00:23 -07001221
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001222 mDownloadListener = new DownloadListener() {
1223 public void onDownloadStart(String url, String userAgent,
1224 String contentDisposition, String mimetype,
1225 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001226 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001227 mimetype, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001228 }
1229 };
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001230 mWebBackForwardListClient = new WebBackForwardListClient() {
1231 @Override
1232 public void onNewHistoryItem(WebHistoryItem item) {
1233 if (isInVoiceSearchMode()) {
1234 item.setCustomData(mVoiceSearchData.mVoiceSearchIntent);
1235 }
1236 }
1237 @Override
1238 public void onIndexChanged(WebHistoryItem item, int index) {
1239 Object data = item.getCustomData();
1240 if (data != null && data instanceof Intent) {
1241 activateVoiceSearchMode((Intent) data);
1242 }
1243 }
1244 };
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001245
Grace Kloba22ac16e2009-10-07 18:00:23 -07001246 setWebView(w);
1247 }
1248
1249 /**
1250 * Sets the WebView for this tab, correctly removing the old WebView from
1251 * the container view.
1252 */
1253 void setWebView(WebView w) {
1254 if (mMainView == w) {
1255 return;
1256 }
Michael Kolba713ec82010-11-29 17:27:06 -08001257
Grace Kloba22ac16e2009-10-07 18:00:23 -07001258 // If the WebView is changing, the page will be reloaded, so any ongoing
1259 // Geolocation permission requests are void.
Grace Kloba50c241e2010-04-20 11:07:50 -07001260 if (mGeolocationPermissionsPrompt != null) {
1261 mGeolocationPermissionsPrompt.hide();
1262 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001263
Michael Kolba713ec82010-11-29 17:27:06 -08001264 mWebViewController.onSetWebView(this, w);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001265
1266 // set the new one
1267 mMainView = w;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001268 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001269 if (mMainView != null) {
1270 mMainView.setWebViewClient(mWebViewClient);
1271 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001272 // Attach DownloadManager so that downloads can start in an active
1273 // or a non-active window. This can happen when going to a site that
1274 // does a redirect after a period of time. The user could have
1275 // switched to another tab while waiting for the download to start.
1276 mMainView.setDownloadListener(mDownloadListener);
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001277 mMainView.setWebBackForwardListClient(mWebBackForwardListClient);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001278 }
1279 }
1280
1281 /**
1282 * Destroy the tab's main WebView and subWindow if any
1283 */
1284 void destroy() {
1285 if (mMainView != null) {
1286 dismissSubWindow();
1287 BrowserSettings.getInstance().deleteObserver(mMainView.getSettings());
1288 // save the WebView to call destroy() after detach it from the tab
1289 WebView webView = mMainView;
1290 setWebView(null);
1291 webView.destroy();
1292 }
1293 }
1294
1295 /**
1296 * Remove the tab from the parent
1297 */
1298 void removeFromTree() {
1299 // detach the children
1300 if (mChildTabs != null) {
1301 for(Tab t : mChildTabs) {
1302 t.setParentTab(null);
1303 }
1304 }
1305 // remove itself from the parent list
1306 if (mParentTab != null) {
1307 mParentTab.mChildTabs.remove(this);
1308 }
1309 }
1310
1311 /**
1312 * Create a new subwindow unless a subwindow already exists.
1313 * @return True if a new subwindow was created. False if one already exists.
1314 */
1315 boolean createSubWindow() {
1316 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001317 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001318 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001319 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001320 mSubView.setWebChromeClient(new SubWindowChromeClient(
1321 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001322 // Set a different DownloadListener for the mSubView, since it will
1323 // just need to dismiss the mSubView, rather than close the Tab
1324 mSubView.setDownloadListener(new DownloadListener() {
1325 public void onDownloadStart(String url, String userAgent,
1326 String contentDisposition, String mimetype,
1327 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001328 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001329 contentDisposition, mimetype, contentLength);
1330 if (mSubView.copyBackForwardList().getSize() == 0) {
1331 // This subwindow was opened for the sole purpose of
1332 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001333 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001334 }
1335 }
1336 });
Grace Kloba22ac16e2009-10-07 18:00:23 -07001337 mSubView.setOnCreateContextMenuListener(mActivity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001338 return true;
1339 }
1340 return false;
1341 }
1342
1343 /**
1344 * Dismiss the subWindow for the tab.
1345 */
1346 void dismissSubWindow() {
1347 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001348 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001349 BrowserSettings.getInstance().deleteObserver(
1350 mSubView.getSettings());
1351 mSubView.destroy();
1352 mSubView = null;
1353 mSubViewContainer = null;
1354 }
1355 }
1356
Grace Kloba22ac16e2009-10-07 18:00:23 -07001357
1358 /**
1359 * Set the parent tab of this tab.
1360 */
1361 void setParentTab(Tab parent) {
1362 mParentTab = parent;
1363 // This tab may have been freed due to low memory. If that is the case,
1364 // the parent tab index is already saved. If we are changing that index
1365 // (most likely due to removing the parent tab) we must update the
1366 // parent tab index in the saved Bundle.
1367 if (mSavedState != null) {
1368 if (parent == null) {
1369 mSavedState.remove(PARENTTAB);
1370 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -07001371 mSavedState.putInt(PARENTTAB, mWebViewController.getTabControl()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001372 .getTabIndex(parent));
1373 }
1374 }
1375 }
1376
1377 /**
1378 * When a Tab is created through the content of another Tab, then we
1379 * associate the Tabs.
1380 * @param child the Tab that was created from this Tab
1381 */
1382 void addChildTab(Tab child) {
1383 if (mChildTabs == null) {
1384 mChildTabs = new Vector<Tab>();
1385 }
1386 mChildTabs.add(child);
1387 child.setParentTab(this);
1388 }
1389
1390 Vector<Tab> getChildTabs() {
1391 return mChildTabs;
1392 }
1393
1394 void resume() {
1395 if (mMainView != null) {
1396 mMainView.onResume();
1397 if (mSubView != null) {
1398 mSubView.onResume();
1399 }
1400 }
1401 }
1402
1403 void pause() {
1404 if (mMainView != null) {
1405 mMainView.onPause();
1406 if (mSubView != null) {
1407 mSubView.onPause();
1408 }
1409 }
1410 }
1411
1412 void putInForeground() {
1413 mInForeground = true;
1414 resume();
1415 mMainView.setOnCreateContextMenuListener(mActivity);
1416 if (mSubView != null) {
1417 mSubView.setOnCreateContextMenuListener(mActivity);
1418 }
1419 // Show the pending error dialog if the queue is not empty
1420 if (mQueuedErrors != null && mQueuedErrors.size() > 0) {
1421 showError(mQueuedErrors.getFirst());
1422 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001423 mWebViewController.bookmarkedStatusHasChanged(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001424 }
1425
1426 void putInBackground() {
1427 mInForeground = false;
1428 pause();
1429 mMainView.setOnCreateContextMenuListener(null);
1430 if (mSubView != null) {
1431 mSubView.setOnCreateContextMenuListener(null);
1432 }
1433 }
1434
Michael Kolb8233fac2010-10-26 16:08:53 -07001435 boolean inForeground() {
1436 return mInForeground;
1437 }
1438
Grace Kloba22ac16e2009-10-07 18:00:23 -07001439 /**
1440 * Return the top window of this tab; either the subwindow if it is not
1441 * null or the main window.
1442 * @return The top window of this tab.
1443 */
1444 WebView getTopWindow() {
1445 if (mSubView != null) {
1446 return mSubView;
1447 }
1448 return mMainView;
1449 }
1450
1451 /**
1452 * Return the main window of this tab. Note: if a tab is freed in the
1453 * background, this can return null. It is only guaranteed to be
1454 * non-null for the current tab.
1455 * @return The main WebView of this tab.
1456 */
1457 WebView getWebView() {
1458 return mMainView;
1459 }
1460
Michael Kolba713ec82010-11-29 17:27:06 -08001461 void setViewContainer(View container) {
1462 mContainer = container;
1463 }
1464
Michael Kolb8233fac2010-10-26 16:08:53 -07001465 View getViewContainer() {
1466 return mContainer;
1467 }
1468
Grace Kloba22ac16e2009-10-07 18:00:23 -07001469 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001470 * Return whether private browsing is enabled for the main window of
1471 * this tab.
1472 * @return True if private browsing is enabled.
1473 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001474 boolean isPrivateBrowsingEnabled() {
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001475 WebView webView = getWebView();
1476 if (webView == null) {
1477 return false;
1478 }
1479 return webView.isPrivateBrowsingEnabled();
1480 }
1481
1482 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001483 * Return the subwindow of this tab or null if there is no subwindow.
1484 * @return The subwindow of this tab or null.
1485 */
1486 WebView getSubWebView() {
1487 return mSubView;
1488 }
1489
Michael Kolb1514bb72010-11-22 09:11:48 -08001490 void setSubWebView(WebView subView) {
1491 mSubView = subView;
1492 }
1493
Michael Kolb8233fac2010-10-26 16:08:53 -07001494 View getSubViewContainer() {
1495 return mSubViewContainer;
1496 }
1497
Michael Kolb1514bb72010-11-22 09:11:48 -08001498 void setSubViewContainer(View subViewContainer) {
1499 mSubViewContainer = subViewContainer;
1500 }
1501
Grace Kloba22ac16e2009-10-07 18:00:23 -07001502 /**
1503 * @return The geolocation permissions prompt for this tab.
1504 */
1505 GeolocationPermissionsPrompt getGeolocationPermissionsPrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001506 if (mGeolocationPermissionsPrompt == null) {
1507 ViewStub stub = (ViewStub) mContainer
1508 .findViewById(R.id.geolocation_permissions_prompt);
1509 mGeolocationPermissionsPrompt = (GeolocationPermissionsPrompt) stub
1510 .inflate();
1511 mGeolocationPermissionsPrompt.init();
1512 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001513 return mGeolocationPermissionsPrompt;
1514 }
1515
1516 /**
1517 * @return The application id string
1518 */
1519 String getAppId() {
1520 return mAppId;
1521 }
1522
1523 /**
1524 * Set the application id string
1525 * @param id
1526 */
1527 void setAppId(String id) {
1528 mAppId = id;
1529 }
1530
1531 /**
1532 * @return The original url associated with this Tab
1533 */
1534 String getOriginalUrl() {
1535 return mOriginalUrl;
1536 }
1537
1538 /**
1539 * Set the original url associated with this tab
1540 */
1541 void setOriginalUrl(String url) {
1542 mOriginalUrl = url;
1543 }
1544
1545 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07001546 * set the title for the tab
1547 */
1548 void setCurrentTitle(String title) {
1549 mCurrentTitle = title;
1550 }
1551
1552 /**
1553 * set url for this tab
1554 * @param url
1555 */
1556 void setCurrentUrl(String url) {
1557 mCurrentUrl = url;
1558 }
1559
1560 String getCurrentTitle() {
1561 return mCurrentTitle;
1562 }
1563
1564 String getCurrentUrl() {
1565 return mCurrentUrl;
1566 }
1567 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001568 * Get the url of this tab. Valid after calling populatePickerData, but
1569 * before calling wipePickerData, or if the webview has been destroyed.
1570 * @return The WebView's url or null.
1571 */
1572 String getUrl() {
1573 if (mPickerData != null) {
1574 return mPickerData.mUrl;
1575 }
1576 return null;
1577 }
1578
1579 /**
1580 * Get the title of this tab. Valid after calling populatePickerData, but
1581 * before calling wipePickerData, or if the webview has been destroyed. If
1582 * the url has no title, use the url instead.
1583 * @return The WebView's title (or url) or null.
1584 */
1585 String getTitle() {
1586 if (mPickerData != null) {
1587 return mPickerData.mTitle;
1588 }
1589 return null;
1590 }
1591
1592 /**
1593 * Get the favicon of this tab. Valid after calling populatePickerData, but
1594 * before calling wipePickerData, or if the webview has been destroyed.
1595 * @return The WebView's favicon or null.
1596 */
1597 Bitmap getFavicon() {
1598 if (mPickerData != null) {
1599 return mPickerData.mFavicon;
1600 }
1601 return null;
1602 }
1603
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001604
Grace Kloba22ac16e2009-10-07 18:00:23 -07001605 /**
1606 * Return the tab's error console. Creates the console if createIfNEcessary
1607 * is true and we haven't already created the console.
1608 * @param createIfNecessary Flag to indicate if the console should be
1609 * created if it has not been already.
1610 * @return The tab's error console, or null if one has not been created and
1611 * createIfNecessary is false.
1612 */
1613 ErrorConsoleView getErrorConsole(boolean createIfNecessary) {
1614 if (createIfNecessary && mErrorConsole == null) {
1615 mErrorConsole = new ErrorConsoleView(mActivity);
1616 mErrorConsole.setWebView(mMainView);
1617 }
1618 return mErrorConsole;
1619 }
1620
1621 /**
1622 * If this Tab was created through another Tab, then this method returns
1623 * that Tab.
1624 * @return the Tab parent or null
1625 */
1626 public Tab getParentTab() {
1627 return mParentTab;
1628 }
1629
1630 /**
1631 * Return whether this tab should be closed when it is backing out of the
1632 * first page.
1633 * @return TRUE if this tab should be closed when exit.
1634 */
1635 boolean closeOnExit() {
1636 return mCloseOnExit;
1637 }
1638
1639 /**
1640 * Saves the current lock-icon state before resetting the lock icon. If we
1641 * have an error, we may need to roll back to the previous state.
1642 */
1643 void resetLockIcon(String url) {
1644 mPrevLockIconType = mLockIconType;
Michael Kolb8233fac2010-10-26 16:08:53 -07001645 mLockIconType = LOCK_ICON_UNSECURE;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001646 if (URLUtil.isHttpsUrl(url)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001647 mLockIconType = LOCK_ICON_SECURE;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001648 }
1649 }
1650
1651 /**
1652 * Reverts the lock-icon state to the last saved state, for example, if we
1653 * had an error, and need to cancel the load.
1654 */
1655 void revertLockIcon() {
1656 mLockIconType = mPrevLockIconType;
1657 }
1658
1659 /**
1660 * @return The tab's lock icon type.
1661 */
1662 int getLockIconType() {
1663 return mLockIconType;
1664 }
1665
1666 /**
1667 * @return TRUE if onPageStarted is called while onPageFinished is not
1668 * called yet.
1669 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001670 boolean inPageLoad() {
1671 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001672 }
1673
1674 // force mInLoad to be false. This should only be called before closing the
1675 // tab to ensure BrowserActivity's pauseWebViewTimers() is called correctly.
Michael Kolb8233fac2010-10-26 16:08:53 -07001676 void clearInPageLoad() {
1677 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001678 }
1679
1680 void populatePickerData() {
1681 if (mMainView == null) {
1682 populatePickerDataFromSavedState();
1683 return;
1684 }
1685
1686 // FIXME: The only place we cared about subwindow was for
1687 // bookmarking (i.e. not when saving state). Was this deliberate?
1688 final WebBackForwardList list = mMainView.copyBackForwardList();
Leon Scroggins70a153b2010-05-10 17:27:26 -04001689 if (list == null) {
1690 Log.w(LOGTAG, "populatePickerData called and WebBackForwardList is null");
1691 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001692 final WebHistoryItem item = list != null ? list.getCurrentItem() : null;
1693 populatePickerData(item);
1694 }
1695
1696 // Populate the picker data using the given history item and the current top
1697 // WebView.
1698 private void populatePickerData(WebHistoryItem item) {
1699 mPickerData = new PickerData();
Leon Scroggins70a153b2010-05-10 17:27:26 -04001700 if (item == null) {
1701 Log.w(LOGTAG, "populatePickerData called with a null WebHistoryItem");
1702 } else {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001703 mPickerData.mUrl = item.getUrl();
1704 mPickerData.mTitle = item.getTitle();
1705 mPickerData.mFavicon = item.getFavicon();
1706 if (mPickerData.mTitle == null) {
1707 mPickerData.mTitle = mPickerData.mUrl;
1708 }
1709 }
1710 }
1711
1712 // Create the PickerData and populate it using the saved state of the tab.
1713 void populatePickerDataFromSavedState() {
1714 if (mSavedState == null) {
1715 return;
1716 }
1717 mPickerData = new PickerData();
1718 mPickerData.mUrl = mSavedState.getString(CURRURL);
1719 mPickerData.mTitle = mSavedState.getString(CURRTITLE);
1720 }
1721
1722 void clearPickerData() {
1723 mPickerData = null;
1724 }
1725
1726 /**
1727 * Get the saved state bundle.
1728 * @return
1729 */
1730 Bundle getSavedState() {
1731 return mSavedState;
1732 }
1733
1734 /**
1735 * Set the saved state.
1736 */
1737 void setSavedState(Bundle state) {
1738 mSavedState = state;
1739 }
1740
1741 /**
1742 * @return TRUE if succeed in saving the state.
1743 */
1744 boolean saveState() {
1745 // If the WebView is null it means we ran low on memory and we already
1746 // stored the saved state in mSavedState.
1747 if (mMainView == null) {
1748 return mSavedState != null;
1749 }
1750
1751 mSavedState = new Bundle();
1752 final WebBackForwardList list = mMainView.saveState(mSavedState);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001753
1754 // Store some extra info for displaying the tab in the picker.
1755 final WebHistoryItem item = list != null ? list.getCurrentItem() : null;
1756 populatePickerData(item);
1757
1758 if (mPickerData.mUrl != null) {
1759 mSavedState.putString(CURRURL, mPickerData.mUrl);
1760 }
1761 if (mPickerData.mTitle != null) {
1762 mSavedState.putString(CURRTITLE, mPickerData.mTitle);
1763 }
1764 mSavedState.putBoolean(CLOSEONEXIT, mCloseOnExit);
1765 if (mAppId != null) {
1766 mSavedState.putString(APPID, mAppId);
1767 }
1768 if (mOriginalUrl != null) {
1769 mSavedState.putString(ORIGINALURL, mOriginalUrl);
1770 }
1771 // Remember the parent tab so the relationship can be restored.
1772 if (mParentTab != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001773 mSavedState.putInt(PARENTTAB, mWebViewController.getTabControl().getTabIndex(
Grace Kloba22ac16e2009-10-07 18:00:23 -07001774 mParentTab));
1775 }
1776 return true;
1777 }
1778
1779 /*
1780 * Restore the state of the tab.
1781 */
1782 boolean restoreState(Bundle b) {
1783 if (b == null) {
1784 return false;
1785 }
1786 // Restore the internal state even if the WebView fails to restore.
1787 // This will maintain the app id, original url and close-on-exit values.
1788 mSavedState = null;
1789 mPickerData = null;
1790 mCloseOnExit = b.getBoolean(CLOSEONEXIT);
1791 mAppId = b.getString(APPID);
1792 mOriginalUrl = b.getString(ORIGINALURL);
1793
1794 final WebBackForwardList list = mMainView.restoreState(b);
1795 if (list == null) {
1796 return false;
1797 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001798 return true;
1799 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04001800
Leon Scroggins1961ed22010-12-07 15:22:21 -05001801 public void updateBookmarkedStatus() {
1802 if (mMainView == null) {
1803 return;
1804 }
1805 String url = mMainView.getUrl();
1806 if (url == null) {
1807 return;
1808 }
1809 updateBookmarkedStatusForUrl(url);
1810 }
1811
1812 /**
1813 * Update mIsBookmarkedSite, using urlInQuestion to compare.
1814 * @param urlInQuestion URL of the current page, to be checked in the
1815 * bookmarks database.
1816 */
1817 private void updateBookmarkedStatusForUrl(final String urlInQuestion) {
1818 if (mBookmarkAsyncTask != null) {
1819 mBookmarkAsyncTask.cancel(true);
1820 }
1821 mBookmarkAsyncTask = new AsyncTask<Void, Void, Boolean>() {
1822 @Override
1823 protected Boolean doInBackground(Void... unused) {
1824 // Check to see if the site is bookmarked
1825 Cursor cursor = null;
1826 try {
1827 cursor = mActivity.getContentResolver().query(
1828 BrowserContract.Bookmarks.CONTENT_URI,
1829 new String[] { BrowserContract.Bookmarks.URL },
1830 BrowserContract.Bookmarks.URL + " == ?",
1831 new String[] { urlInQuestion },
1832 null);
1833 return cursor.moveToFirst();
1834 } catch (SQLiteException e) {
1835 Log.e(LOGTAG, "Error checking for bookmark: " + e);
1836 return false;
1837 } finally {
1838 if (cursor != null) cursor.close();
1839 }
1840 }
1841 @Override
1842 protected void onPostExecute(Boolean isBookmarked) {
1843 if (this == mBookmarkAsyncTask) {
1844 mIsBookmarkedSite = isBookmarked;
1845 mWebViewController.bookmarkedStatusHasChanged(Tab.this);
1846 }
1847 }
1848 };
1849 mBookmarkAsyncTask.execute();
1850 }
1851
Grace Kloba22ac16e2009-10-07 18:00:23 -07001852}