blob: 5b536f86fe8ec190f92ca38e199439f2c83b03b0 [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 Kolb8233fac2010-10-26 16:08:53 -0700536 return mWebViewController.shouldOverrideUrlLoading(view, url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700537 } else {
538 return false;
539 }
540 }
541
542 /**
543 * Updates the lock icon. This method is called when we discover another
544 * resource to be loaded for this page (for example, javascript). While
545 * we update the icon type, we do not update the lock icon itself until
546 * we are done loading, it is slightly more secure this way.
547 */
548 @Override
549 public void onLoadResource(WebView view, String url) {
550 if (url != null && url.length() > 0) {
551 // It is only if the page claims to be secure that we may have
552 // to update the lock:
Michael Kolb8233fac2010-10-26 16:08:53 -0700553 if (mLockIconType == LOCK_ICON_SECURE) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700554 // If NOT a 'safe' url, change the lock to mixed content!
555 if (!(URLUtil.isHttpsUrl(url) || URLUtil.isDataUrl(url)
556 || URLUtil.isAboutUrl(url))) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700557 mLockIconType = LOCK_ICON_MIXED;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700558 }
559 }
560 }
561 }
562
563 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700564 * Show a dialog informing the user of the network error reported by
565 * WebCore if it is in the foreground.
566 */
567 @Override
568 public void onReceivedError(WebView view, int errorCode,
569 String description, String failingUrl) {
570 if (errorCode != WebViewClient.ERROR_HOST_LOOKUP &&
571 errorCode != WebViewClient.ERROR_CONNECT &&
572 errorCode != WebViewClient.ERROR_BAD_URL &&
573 errorCode != WebViewClient.ERROR_UNSUPPORTED_SCHEME &&
574 errorCode != WebViewClient.ERROR_FILE) {
575 queueError(errorCode, description);
576 }
Jeff Hamilton47654f42010-09-07 09:57:51 -0500577
578 // Don't log URLs when in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -0700579 if (!isPrivateBrowsingEnabled()) {
Jeff Hamilton47654f42010-09-07 09:57:51 -0500580 Log.e(LOGTAG, "onReceivedError " + errorCode + " " + failingUrl
581 + " " + description);
582 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700583
584 // We need to reset the title after an error if it is in foreground.
585 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700586 mWebViewController.resetTitleAndRevertLockIcon(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700587 }
588 }
589
590 /**
591 * Check with the user if it is ok to resend POST data as the page they
592 * are trying to navigate to is the result of a POST.
593 */
594 @Override
595 public void onFormResubmission(WebView view, final Message dontResend,
596 final Message resend) {
597 if (!mInForeground) {
598 dontResend.sendToTarget();
599 return;
600 }
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500601 if (mDontResend != null) {
602 Log.w(LOGTAG, "onFormResubmission should not be called again "
603 + "while dialog is still up");
604 dontResend.sendToTarget();
605 return;
606 }
607 mDontResend = dontResend;
608 mResend = resend;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700609 new AlertDialog.Builder(mActivity).setTitle(
610 R.string.browserFrameFormResubmitLabel).setMessage(
611 R.string.browserFrameFormResubmitMessage)
612 .setPositiveButton(R.string.ok,
613 new DialogInterface.OnClickListener() {
614 public void onClick(DialogInterface dialog,
615 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500616 if (mResend != null) {
617 mResend.sendToTarget();
618 mResend = null;
619 mDontResend = null;
620 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700621 }
622 }).setNegativeButton(R.string.cancel,
623 new DialogInterface.OnClickListener() {
624 public void onClick(DialogInterface dialog,
625 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500626 if (mDontResend != null) {
627 mDontResend.sendToTarget();
628 mResend = null;
629 mDontResend = null;
630 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700631 }
632 }).setOnCancelListener(new OnCancelListener() {
633 public void onCancel(DialogInterface dialog) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500634 if (mDontResend != null) {
635 mDontResend.sendToTarget();
636 mResend = null;
637 mDontResend = null;
638 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700639 }
640 }).show();
641 }
642
643 /**
644 * Insert the url into the visited history database.
645 * @param url The url to be inserted.
646 * @param isReload True if this url is being reloaded.
647 * FIXME: Not sure what to do when reloading the page.
648 */
649 @Override
650 public void doUpdateVisitedHistory(WebView view, String url,
651 boolean isReload) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700652 mWebViewController.doUpdateVisitedHistory(Tab.this, url, isReload);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700653 }
654
655 /**
656 * Displays SSL error(s) dialog to the user.
657 */
658 @Override
659 public void onReceivedSslError(final WebView view,
660 final SslErrorHandler handler, final SslError error) {
661 if (!mInForeground) {
662 handler.cancel();
663 return;
664 }
665 if (BrowserSettings.getInstance().showSecurityWarnings()) {
666 final LayoutInflater factory =
667 LayoutInflater.from(mActivity);
668 final View warningsView =
669 factory.inflate(R.layout.ssl_warnings, null);
670 final LinearLayout placeholder =
671 (LinearLayout)warningsView.findViewById(R.id.placeholder);
672
673 if (error.hasError(SslError.SSL_UNTRUSTED)) {
674 LinearLayout ll = (LinearLayout)factory
675 .inflate(R.layout.ssl_warning, null);
676 ((TextView)ll.findViewById(R.id.warning))
677 .setText(R.string.ssl_untrusted);
678 placeholder.addView(ll);
679 }
680
681 if (error.hasError(SslError.SSL_IDMISMATCH)) {
682 LinearLayout ll = (LinearLayout)factory
683 .inflate(R.layout.ssl_warning, null);
684 ((TextView)ll.findViewById(R.id.warning))
685 .setText(R.string.ssl_mismatch);
686 placeholder.addView(ll);
687 }
688
689 if (error.hasError(SslError.SSL_EXPIRED)) {
690 LinearLayout ll = (LinearLayout)factory
691 .inflate(R.layout.ssl_warning, null);
692 ((TextView)ll.findViewById(R.id.warning))
693 .setText(R.string.ssl_expired);
694 placeholder.addView(ll);
695 }
696
697 if (error.hasError(SslError.SSL_NOTYETVALID)) {
698 LinearLayout ll = (LinearLayout)factory
699 .inflate(R.layout.ssl_warning, null);
700 ((TextView)ll.findViewById(R.id.warning))
701 .setText(R.string.ssl_not_yet_valid);
702 placeholder.addView(ll);
703 }
704
705 new AlertDialog.Builder(mActivity).setTitle(
706 R.string.security_warning).setIcon(
707 android.R.drawable.ic_dialog_alert).setView(
708 warningsView).setPositiveButton(R.string.ssl_continue,
709 new DialogInterface.OnClickListener() {
710 public void onClick(DialogInterface dialog,
711 int whichButton) {
712 handler.proceed();
713 }
714 }).setNeutralButton(R.string.view_certificate,
715 new DialogInterface.OnClickListener() {
716 public void onClick(DialogInterface dialog,
717 int whichButton) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700718 mWebViewController.showSslCertificateOnError(view,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700719 handler, error);
720 }
Ben Murdocha49b8292010-11-16 11:56:04 +0000721 }).setNegativeButton(R.string.ssl_go_back,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700722 new DialogInterface.OnClickListener() {
723 public void onClick(DialogInterface dialog,
724 int whichButton) {
725 handler.cancel();
Michael Kolb8233fac2010-10-26 16:08:53 -0700726 mWebViewController.resetTitleAndRevertLockIcon(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700727 }
728 }).setOnCancelListener(
729 new DialogInterface.OnCancelListener() {
730 public void onCancel(DialogInterface dialog) {
731 handler.cancel();
Michael Kolb8233fac2010-10-26 16:08:53 -0700732 mWebViewController.resetTitleAndRevertLockIcon(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700733 }
734 }).show();
735 } else {
736 handler.proceed();
737 }
738 }
739
740 /**
741 * Handles an HTTP authentication request.
742 *
743 * @param handler The authentication handler
744 * @param host The host
745 * @param realm The realm
746 */
747 @Override
748 public void onReceivedHttpAuthRequest(WebView view,
749 final HttpAuthHandler handler, final String host,
750 final String realm) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700751 mWebViewController.onReceivedHttpAuthRequest(Tab.this, view, handler, host, realm);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700752 }
753
754 @Override
755 public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
756 if (!mInForeground) {
757 return false;
758 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700759 return mWebViewController.shouldOverrideKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700760 }
761
762 @Override
763 public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700764 if (!mInForeground) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700765 return;
766 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700767 mWebViewController.onUnhandledKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700768 }
769 };
770
771 // -------------------------------------------------------------------------
772 // WebChromeClient implementation for the main WebView
773 // -------------------------------------------------------------------------
774
775 private final WebChromeClient mWebChromeClient = new WebChromeClient() {
776 // Helper method to create a new tab or sub window.
777 private void createWindow(final boolean dialog, final Message msg) {
778 WebView.WebViewTransport transport =
779 (WebView.WebViewTransport) msg.obj;
780 if (dialog) {
781 createSubWindow();
Michael Kolb8233fac2010-10-26 16:08:53 -0700782 mWebViewController.attachSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700783 transport.setWebView(mSubView);
784 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -0700785 final Tab newTab = mWebViewController.openTabAndShow(
786 IntentHandler.EMPTY_URL_DATA, false, null);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700787 if (newTab != Tab.this) {
788 Tab.this.addChildTab(newTab);
789 }
790 transport.setWebView(newTab.getWebView());
791 }
792 msg.sendToTarget();
793 }
794
795 @Override
796 public boolean onCreateWindow(WebView view, final boolean dialog,
797 final boolean userGesture, final Message resultMsg) {
798 // only allow new window or sub window for the foreground case
799 if (!mInForeground) {
800 return false;
801 }
802 // Short-circuit if we can't create any more tabs or sub windows.
803 if (dialog && mSubView != null) {
804 new AlertDialog.Builder(mActivity)
805 .setTitle(R.string.too_many_subwindows_dialog_title)
806 .setIcon(android.R.drawable.ic_dialog_alert)
807 .setMessage(R.string.too_many_subwindows_dialog_message)
808 .setPositiveButton(R.string.ok, null)
809 .show();
810 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700811 } else if (!mWebViewController.getTabControl().canCreateNewTab()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700812 new AlertDialog.Builder(mActivity)
813 .setTitle(R.string.too_many_windows_dialog_title)
814 .setIcon(android.R.drawable.ic_dialog_alert)
815 .setMessage(R.string.too_many_windows_dialog_message)
816 .setPositiveButton(R.string.ok, null)
817 .show();
818 return false;
819 }
820
821 // Short-circuit if this was a user gesture.
822 if (userGesture) {
823 createWindow(dialog, resultMsg);
824 return true;
825 }
826
827 // Allow the popup and create the appropriate window.
828 final AlertDialog.OnClickListener allowListener =
829 new AlertDialog.OnClickListener() {
830 public void onClick(DialogInterface d,
831 int which) {
832 createWindow(dialog, resultMsg);
833 }
834 };
835
836 // Block the popup by returning a null WebView.
837 final AlertDialog.OnClickListener blockListener =
838 new AlertDialog.OnClickListener() {
839 public void onClick(DialogInterface d, int which) {
840 resultMsg.sendToTarget();
841 }
842 };
843
844 // Build a confirmation dialog to display to the user.
845 final AlertDialog d =
846 new AlertDialog.Builder(mActivity)
847 .setTitle(R.string.attention)
848 .setIcon(android.R.drawable.ic_dialog_alert)
849 .setMessage(R.string.popup_window_attempt)
850 .setPositiveButton(R.string.allow, allowListener)
851 .setNegativeButton(R.string.block, blockListener)
852 .setCancelable(false)
853 .create();
854
855 // Show the confirmation dialog.
856 d.show();
857 return true;
858 }
859
860 @Override
Patrick Scotteb5061b2009-11-18 15:00:30 -0500861 public void onRequestFocus(WebView view) {
862 if (!mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700863 mWebViewController.switchToTab(mWebViewController.getTabControl().getTabIndex(
Patrick Scotteb5061b2009-11-18 15:00:30 -0500864 Tab.this));
865 }
866 }
867
868 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700869 public void onCloseWindow(WebView window) {
870 if (mParentTab != null) {
871 // JavaScript can only close popup window.
872 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700873 mWebViewController.switchToTab(mWebViewController.getTabControl()
Grace Kloba22ac16e2009-10-07 18:00:23 -0700874 .getTabIndex(mParentTab));
875 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700876 mWebViewController.closeTab(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700877 }
878 }
879
880 @Override
881 public void onProgressChanged(WebView view, int newProgress) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700882 mWebViewController.onProgressChanged(Tab.this, newProgress);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700883 }
884
885 @Override
Leon Scroggins21d9b902010-03-11 09:33:11 -0500886 public void onReceivedTitle(WebView view, final String title) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700887 mWebViewController.onReceivedTitle(Tab.this, title);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700888 }
889
890 @Override
891 public void onReceivedIcon(WebView view, Bitmap icon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700892 mWebViewController.onFavicon(Tab.this, view, icon);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700893 }
894
895 @Override
896 public void onReceivedTouchIconUrl(WebView view, String url,
897 boolean precomposed) {
898 final ContentResolver cr = mActivity.getContentResolver();
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400899 // Let precomposed icons take precedence over non-composed
900 // icons.
901 if (precomposed && mTouchIconLoader != null) {
902 mTouchIconLoader.cancel(false);
903 mTouchIconLoader = null;
904 }
905 // Have only one async task at a time.
906 if (mTouchIconLoader == null) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700907 mTouchIconLoader = new DownloadTouchIcon(Tab.this,
908 mActivity, cr, view);
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400909 mTouchIconLoader.execute(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700910 }
911 }
912
913 @Override
914 public void onShowCustomView(View view,
915 WebChromeClient.CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700916 if (mInForeground) mWebViewController.showCustomView(Tab.this, view,
917 callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700918 }
919
920 @Override
921 public void onHideCustomView() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700922 if (mInForeground) mWebViewController.hideCustomView();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700923 }
924
925 /**
926 * The origin has exceeded its database quota.
927 * @param url the URL that exceeded the quota
928 * @param databaseIdentifier the identifier of the database on which the
929 * transaction that caused the quota overflow was run
930 * @param currentQuota the current quota for the origin.
931 * @param estimatedSize the estimated size of the database.
932 * @param totalUsedQuota is the sum of all origins' quota.
933 * @param quotaUpdater The callback to run when a decision to allow or
934 * deny quota has been made. Don't forget to call this!
935 */
936 @Override
937 public void onExceededDatabaseQuota(String url,
938 String databaseIdentifier, long currentQuota, long estimatedSize,
939 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
940 BrowserSettings.getInstance().getWebStorageSizeManager()
941 .onExceededDatabaseQuota(url, databaseIdentifier,
942 currentQuota, estimatedSize, totalUsedQuota,
943 quotaUpdater);
944 }
945
946 /**
947 * The Application Cache has exceeded its max size.
948 * @param spaceNeeded is the amount of disk space that would be needed
949 * in order for the last appcache operation to succeed.
950 * @param totalUsedQuota is the sum of all origins' quota.
951 * @param quotaUpdater A callback to inform the WebCore thread that a
952 * new app cache size is available. This callback must always
953 * be executed at some point to ensure that the sleeping
954 * WebCore thread is woken up.
955 */
956 @Override
957 public void onReachedMaxAppCacheSize(long spaceNeeded,
958 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
959 BrowserSettings.getInstance().getWebStorageSizeManager()
960 .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota,
961 quotaUpdater);
962 }
963
964 /**
965 * Instructs the browser to show a prompt to ask the user to set the
966 * Geolocation permission state for the specified origin.
967 * @param origin The origin for which Geolocation permissions are
968 * requested.
969 * @param callback The callback to call once the user has set the
970 * Geolocation permission state.
971 */
972 @Override
973 public void onGeolocationPermissionsShowPrompt(String origin,
974 GeolocationPermissions.Callback callback) {
975 if (mInForeground) {
Grace Kloba50c241e2010-04-20 11:07:50 -0700976 getGeolocationPermissionsPrompt().show(origin, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700977 }
978 }
979
980 /**
981 * Instructs the browser to hide the Geolocation permissions prompt.
982 */
983 @Override
984 public void onGeolocationPermissionsHidePrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -0700985 if (mInForeground && mGeolocationPermissionsPrompt != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700986 mGeolocationPermissionsPrompt.hide();
987 }
988 }
989
Ben Murdoch65acc352009-11-19 18:16:04 +0000990 /* Adds a JavaScript error message to the system log and if the JS
991 * console is enabled in the about:debug options, to that console
992 * also.
Ben Murdochc42addf2010-01-28 15:19:59 +0000993 * @param consoleMessage the message object.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700994 */
995 @Override
Ben Murdochc42addf2010-01-28 15:19:59 +0000996 public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700997 if (mInForeground) {
998 // call getErrorConsole(true) so it will create one if needed
999 ErrorConsoleView errorConsole = getErrorConsole(true);
Ben Murdochc42addf2010-01-28 15:19:59 +00001000 errorConsole.addErrorMessage(consoleMessage);
Michael Kolb8233fac2010-10-26 16:08:53 -07001001 if (mWebViewController.shouldShowErrorConsole()
1002 && errorConsole.getShowState() !=
1003 ErrorConsoleView.SHOW_MAXIMIZED) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001004 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
1005 }
1006 }
Ben Murdochc42addf2010-01-28 15:19:59 +00001007
Jeff Hamilton47654f42010-09-07 09:57:51 -05001008 // Don't log console messages in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001009 if (isPrivateBrowsingEnabled()) return true;
Jeff Hamilton47654f42010-09-07 09:57:51 -05001010
Ben Murdochc42addf2010-01-28 15:19:59 +00001011 String message = "Console: " + consoleMessage.message() + " "
1012 + consoleMessage.sourceId() + ":"
1013 + consoleMessage.lineNumber();
1014
1015 switch (consoleMessage.messageLevel()) {
1016 case TIP:
1017 Log.v(CONSOLE_LOGTAG, message);
1018 break;
1019 case LOG:
1020 Log.i(CONSOLE_LOGTAG, message);
1021 break;
1022 case WARNING:
1023 Log.w(CONSOLE_LOGTAG, message);
1024 break;
1025 case ERROR:
1026 Log.e(CONSOLE_LOGTAG, message);
1027 break;
1028 case DEBUG:
1029 Log.d(CONSOLE_LOGTAG, message);
1030 break;
1031 }
1032
1033 return true;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001034 }
1035
1036 /**
1037 * Ask the browser for an icon to represent a <video> element.
1038 * This icon will be used if the Web page did not specify a poster attribute.
1039 * @return Bitmap The icon or null if no such icon is available.
1040 */
1041 @Override
1042 public Bitmap getDefaultVideoPoster() {
1043 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001044 return mWebViewController.getDefaultVideoPoster();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001045 }
1046 return null;
1047 }
1048
1049 /**
1050 * Ask the host application for a custom progress view to show while
1051 * a <video> is loading.
1052 * @return View The progress view.
1053 */
1054 @Override
1055 public View getVideoLoadingProgressView() {
1056 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001057 return mWebViewController.getVideoLoadingProgressView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001058 }
1059 return null;
1060 }
1061
1062 @Override
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01001063 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001064 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001065 mWebViewController.openFileChooser(uploadMsg, acceptType);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001066 } else {
1067 uploadMsg.onReceiveValue(null);
1068 }
1069 }
1070
1071 /**
1072 * Deliver a list of already-visited URLs
1073 */
1074 @Override
1075 public void getVisitedHistory(final ValueCallback<String[]> callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001076 mWebViewController.getVisitedHistory(callback);
1077 }
Ben Murdoch8029a772010-11-16 11:58:21 +00001078
1079 @Override
1080 public void setupAutoFill(Message message) {
1081 // Prompt the user to set up their profile.
1082 final Message msg = message;
1083 AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
1084 builder.setMessage(R.string.autofill_setup_dialog_message)
1085 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1086 @Override
1087 public void onClick(DialogInterface dialog, int id) {
1088 // Take user to the AutoFill profile editor. When they return,
1089 // we will send the message that we pass here which will trigger
1090 // the form to get filled out with their new profile.
1091 mWebViewController.setupAutoFill(msg);
1092 }
1093 })
1094 .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
1095 @Override
1096 public void onClick(DialogInterface dialog, int id) {
1097 // Disable autofill and show a toast with how to turn it on again.
1098 BrowserSettings s = BrowserSettings.getInstance();
1099 s.addObserver(mMainView.getSettings());
1100 s.disableAutoFill(mActivity);
1101 s.update();
1102 Toast.makeText(mActivity, R.string.autofill_setup_dialog_negative_toast,
1103 Toast.LENGTH_LONG).show();
1104 }
1105 }).show();
1106 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001107 };
1108
1109 // -------------------------------------------------------------------------
1110 // WebViewClient implementation for the sub window
1111 // -------------------------------------------------------------------------
1112
1113 // Subclass of WebViewClient used in subwindows to notify the main
1114 // WebViewClient of certain WebView activities.
1115 private static class SubWindowClient extends WebViewClient {
1116 // The main WebViewClient.
1117 private final WebViewClient mClient;
Michael Kolb8233fac2010-10-26 16:08:53 -07001118 private final WebViewController mController;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001119
Michael Kolb8233fac2010-10-26 16:08:53 -07001120 SubWindowClient(WebViewClient client, WebViewController controller) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001121 mClient = client;
Michael Kolb8233fac2010-10-26 16:08:53 -07001122 mController = controller;
Leon Scroggins III211ba542010-04-19 13:21:13 -04001123 }
1124 @Override
1125 public void onPageStarted(WebView view, String url, Bitmap favicon) {
1126 // Unlike the others, do not call mClient's version, which would
1127 // change the progress bar. However, we do want to remove the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001128 // find or select dialog.
Michael Kolb8233fac2010-10-26 16:08:53 -07001129 mController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001130 }
1131 @Override
1132 public void doUpdateVisitedHistory(WebView view, String url,
1133 boolean isReload) {
1134 mClient.doUpdateVisitedHistory(view, url, isReload);
1135 }
1136 @Override
1137 public boolean shouldOverrideUrlLoading(WebView view, String url) {
1138 return mClient.shouldOverrideUrlLoading(view, url);
1139 }
1140 @Override
1141 public void onReceivedSslError(WebView view, SslErrorHandler handler,
1142 SslError error) {
1143 mClient.onReceivedSslError(view, handler, error);
1144 }
1145 @Override
1146 public void onReceivedHttpAuthRequest(WebView view,
1147 HttpAuthHandler handler, String host, String realm) {
1148 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
1149 }
1150 @Override
1151 public void onFormResubmission(WebView view, Message dontResend,
1152 Message resend) {
1153 mClient.onFormResubmission(view, dontResend, resend);
1154 }
1155 @Override
1156 public void onReceivedError(WebView view, int errorCode,
1157 String description, String failingUrl) {
1158 mClient.onReceivedError(view, errorCode, description, failingUrl);
1159 }
1160 @Override
1161 public boolean shouldOverrideKeyEvent(WebView view,
1162 android.view.KeyEvent event) {
1163 return mClient.shouldOverrideKeyEvent(view, event);
1164 }
1165 @Override
1166 public void onUnhandledKeyEvent(WebView view,
1167 android.view.KeyEvent event) {
1168 mClient.onUnhandledKeyEvent(view, event);
1169 }
1170 }
1171
1172 // -------------------------------------------------------------------------
1173 // WebChromeClient implementation for the sub window
1174 // -------------------------------------------------------------------------
1175
1176 private class SubWindowChromeClient extends WebChromeClient {
1177 // The main WebChromeClient.
1178 private final WebChromeClient mClient;
1179
1180 SubWindowChromeClient(WebChromeClient client) {
1181 mClient = client;
1182 }
1183 @Override
1184 public void onProgressChanged(WebView view, int newProgress) {
1185 mClient.onProgressChanged(view, newProgress);
1186 }
1187 @Override
1188 public boolean onCreateWindow(WebView view, boolean dialog,
1189 boolean userGesture, android.os.Message resultMsg) {
1190 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
1191 }
1192 @Override
1193 public void onCloseWindow(WebView window) {
1194 if (window != mSubView) {
1195 Log.e(LOGTAG, "Can't close the window");
1196 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001197 mWebViewController.dismissSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001198 }
1199 }
1200
1201 // -------------------------------------------------------------------------
1202
Michael Kolb8233fac2010-10-26 16:08:53 -07001203 // TODO temporarily use activity here
1204 // remove later
1205
Grace Kloba22ac16e2009-10-07 18:00:23 -07001206 // Construct a new tab
Michael Kolb8233fac2010-10-26 16:08:53 -07001207 Tab(WebViewController wvcontroller, WebView w, boolean closeOnExit, String appId,
Grace Kloba22ac16e2009-10-07 18:00:23 -07001208 String url) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001209 mWebViewController = wvcontroller;
1210 mActivity = mWebViewController.getActivity();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001211 mCloseOnExit = closeOnExit;
1212 mAppId = appId;
1213 mOriginalUrl = url;
Michael Kolb8233fac2010-10-26 16:08:53 -07001214 mLockIconType = LOCK_ICON_UNSECURE;
1215 mPrevLockIconType = LOCK_ICON_UNSECURE;
1216 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001217 mInForeground = false;
1218
Grace Kloba22ac16e2009-10-07 18:00:23 -07001219
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001220 mDownloadListener = new DownloadListener() {
1221 public void onDownloadStart(String url, String userAgent,
1222 String contentDisposition, String mimetype,
1223 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001224 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001225 mimetype, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001226 }
1227 };
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001228 mWebBackForwardListClient = new WebBackForwardListClient() {
1229 @Override
1230 public void onNewHistoryItem(WebHistoryItem item) {
1231 if (isInVoiceSearchMode()) {
1232 item.setCustomData(mVoiceSearchData.mVoiceSearchIntent);
1233 }
1234 }
1235 @Override
1236 public void onIndexChanged(WebHistoryItem item, int index) {
1237 Object data = item.getCustomData();
1238 if (data != null && data instanceof Intent) {
1239 activateVoiceSearchMode((Intent) data);
1240 }
1241 }
1242 };
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001243
Grace Kloba22ac16e2009-10-07 18:00:23 -07001244 setWebView(w);
1245 }
1246
1247 /**
1248 * Sets the WebView for this tab, correctly removing the old WebView from
1249 * the container view.
1250 */
1251 void setWebView(WebView w) {
1252 if (mMainView == w) {
1253 return;
1254 }
Michael Kolba713ec82010-11-29 17:27:06 -08001255
Grace Kloba22ac16e2009-10-07 18:00:23 -07001256 // If the WebView is changing, the page will be reloaded, so any ongoing
1257 // Geolocation permission requests are void.
Grace Kloba50c241e2010-04-20 11:07:50 -07001258 if (mGeolocationPermissionsPrompt != null) {
1259 mGeolocationPermissionsPrompt.hide();
1260 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001261
Michael Kolba713ec82010-11-29 17:27:06 -08001262 mWebViewController.onSetWebView(this, w);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001263
1264 // set the new one
1265 mMainView = w;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001266 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001267 if (mMainView != null) {
1268 mMainView.setWebViewClient(mWebViewClient);
1269 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001270 // Attach DownloadManager so that downloads can start in an active
1271 // or a non-active window. This can happen when going to a site that
1272 // does a redirect after a period of time. The user could have
1273 // switched to another tab while waiting for the download to start.
1274 mMainView.setDownloadListener(mDownloadListener);
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001275 mMainView.setWebBackForwardListClient(mWebBackForwardListClient);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001276 }
1277 }
1278
1279 /**
1280 * Destroy the tab's main WebView and subWindow if any
1281 */
1282 void destroy() {
1283 if (mMainView != null) {
1284 dismissSubWindow();
1285 BrowserSettings.getInstance().deleteObserver(mMainView.getSettings());
1286 // save the WebView to call destroy() after detach it from the tab
1287 WebView webView = mMainView;
1288 setWebView(null);
1289 webView.destroy();
1290 }
1291 }
1292
1293 /**
1294 * Remove the tab from the parent
1295 */
1296 void removeFromTree() {
1297 // detach the children
1298 if (mChildTabs != null) {
1299 for(Tab t : mChildTabs) {
1300 t.setParentTab(null);
1301 }
1302 }
1303 // remove itself from the parent list
1304 if (mParentTab != null) {
1305 mParentTab.mChildTabs.remove(this);
1306 }
1307 }
1308
1309 /**
1310 * Create a new subwindow unless a subwindow already exists.
1311 * @return True if a new subwindow was created. False if one already exists.
1312 */
1313 boolean createSubWindow() {
1314 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001315 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001316 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001317 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001318 mSubView.setWebChromeClient(new SubWindowChromeClient(
1319 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001320 // Set a different DownloadListener for the mSubView, since it will
1321 // just need to dismiss the mSubView, rather than close the Tab
1322 mSubView.setDownloadListener(new DownloadListener() {
1323 public void onDownloadStart(String url, String userAgent,
1324 String contentDisposition, String mimetype,
1325 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001326 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001327 contentDisposition, mimetype, contentLength);
1328 if (mSubView.copyBackForwardList().getSize() == 0) {
1329 // This subwindow was opened for the sole purpose of
1330 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001331 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001332 }
1333 }
1334 });
Grace Kloba22ac16e2009-10-07 18:00:23 -07001335 mSubView.setOnCreateContextMenuListener(mActivity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001336 return true;
1337 }
1338 return false;
1339 }
1340
1341 /**
1342 * Dismiss the subWindow for the tab.
1343 */
1344 void dismissSubWindow() {
1345 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001346 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001347 BrowserSettings.getInstance().deleteObserver(
1348 mSubView.getSettings());
1349 mSubView.destroy();
1350 mSubView = null;
1351 mSubViewContainer = null;
1352 }
1353 }
1354
Grace Kloba22ac16e2009-10-07 18:00:23 -07001355
1356 /**
1357 * Set the parent tab of this tab.
1358 */
1359 void setParentTab(Tab parent) {
1360 mParentTab = parent;
1361 // This tab may have been freed due to low memory. If that is the case,
1362 // the parent tab index is already saved. If we are changing that index
1363 // (most likely due to removing the parent tab) we must update the
1364 // parent tab index in the saved Bundle.
1365 if (mSavedState != null) {
1366 if (parent == null) {
1367 mSavedState.remove(PARENTTAB);
1368 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -07001369 mSavedState.putInt(PARENTTAB, mWebViewController.getTabControl()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001370 .getTabIndex(parent));
1371 }
1372 }
1373 }
1374
1375 /**
1376 * When a Tab is created through the content of another Tab, then we
1377 * associate the Tabs.
1378 * @param child the Tab that was created from this Tab
1379 */
1380 void addChildTab(Tab child) {
1381 if (mChildTabs == null) {
1382 mChildTabs = new Vector<Tab>();
1383 }
1384 mChildTabs.add(child);
1385 child.setParentTab(this);
1386 }
1387
1388 Vector<Tab> getChildTabs() {
1389 return mChildTabs;
1390 }
1391
1392 void resume() {
1393 if (mMainView != null) {
1394 mMainView.onResume();
1395 if (mSubView != null) {
1396 mSubView.onResume();
1397 }
1398 }
1399 }
1400
1401 void pause() {
1402 if (mMainView != null) {
1403 mMainView.onPause();
1404 if (mSubView != null) {
1405 mSubView.onPause();
1406 }
1407 }
1408 }
1409
1410 void putInForeground() {
1411 mInForeground = true;
1412 resume();
1413 mMainView.setOnCreateContextMenuListener(mActivity);
1414 if (mSubView != null) {
1415 mSubView.setOnCreateContextMenuListener(mActivity);
1416 }
1417 // Show the pending error dialog if the queue is not empty
1418 if (mQueuedErrors != null && mQueuedErrors.size() > 0) {
1419 showError(mQueuedErrors.getFirst());
1420 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001421 mWebViewController.bookmarkedStatusHasChanged(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001422 }
1423
1424 void putInBackground() {
1425 mInForeground = false;
1426 pause();
1427 mMainView.setOnCreateContextMenuListener(null);
1428 if (mSubView != null) {
1429 mSubView.setOnCreateContextMenuListener(null);
1430 }
1431 }
1432
Michael Kolb8233fac2010-10-26 16:08:53 -07001433 boolean inForeground() {
1434 return mInForeground;
1435 }
1436
Grace Kloba22ac16e2009-10-07 18:00:23 -07001437 /**
1438 * Return the top window of this tab; either the subwindow if it is not
1439 * null or the main window.
1440 * @return The top window of this tab.
1441 */
1442 WebView getTopWindow() {
1443 if (mSubView != null) {
1444 return mSubView;
1445 }
1446 return mMainView;
1447 }
1448
1449 /**
1450 * Return the main window of this tab. Note: if a tab is freed in the
1451 * background, this can return null. It is only guaranteed to be
1452 * non-null for the current tab.
1453 * @return The main WebView of this tab.
1454 */
1455 WebView getWebView() {
1456 return mMainView;
1457 }
1458
Michael Kolba713ec82010-11-29 17:27:06 -08001459 void setViewContainer(View container) {
1460 mContainer = container;
1461 }
1462
Michael Kolb8233fac2010-10-26 16:08:53 -07001463 View getViewContainer() {
1464 return mContainer;
1465 }
1466
Grace Kloba22ac16e2009-10-07 18:00:23 -07001467 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001468 * Return whether private browsing is enabled for the main window of
1469 * this tab.
1470 * @return True if private browsing is enabled.
1471 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001472 boolean isPrivateBrowsingEnabled() {
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001473 WebView webView = getWebView();
1474 if (webView == null) {
1475 return false;
1476 }
1477 return webView.isPrivateBrowsingEnabled();
1478 }
1479
1480 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001481 * Return the subwindow of this tab or null if there is no subwindow.
1482 * @return The subwindow of this tab or null.
1483 */
1484 WebView getSubWebView() {
1485 return mSubView;
1486 }
1487
Michael Kolb1514bb72010-11-22 09:11:48 -08001488 void setSubWebView(WebView subView) {
1489 mSubView = subView;
1490 }
1491
Michael Kolb8233fac2010-10-26 16:08:53 -07001492 View getSubViewContainer() {
1493 return mSubViewContainer;
1494 }
1495
Michael Kolb1514bb72010-11-22 09:11:48 -08001496 void setSubViewContainer(View subViewContainer) {
1497 mSubViewContainer = subViewContainer;
1498 }
1499
Grace Kloba22ac16e2009-10-07 18:00:23 -07001500 /**
1501 * @return The geolocation permissions prompt for this tab.
1502 */
1503 GeolocationPermissionsPrompt getGeolocationPermissionsPrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001504 if (mGeolocationPermissionsPrompt == null) {
1505 ViewStub stub = (ViewStub) mContainer
1506 .findViewById(R.id.geolocation_permissions_prompt);
1507 mGeolocationPermissionsPrompt = (GeolocationPermissionsPrompt) stub
1508 .inflate();
1509 mGeolocationPermissionsPrompt.init();
1510 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001511 return mGeolocationPermissionsPrompt;
1512 }
1513
1514 /**
1515 * @return The application id string
1516 */
1517 String getAppId() {
1518 return mAppId;
1519 }
1520
1521 /**
1522 * Set the application id string
1523 * @param id
1524 */
1525 void setAppId(String id) {
1526 mAppId = id;
1527 }
1528
1529 /**
1530 * @return The original url associated with this Tab
1531 */
1532 String getOriginalUrl() {
1533 return mOriginalUrl;
1534 }
1535
1536 /**
1537 * Set the original url associated with this tab
1538 */
1539 void setOriginalUrl(String url) {
1540 mOriginalUrl = url;
1541 }
1542
1543 /**
Michael Kolb8233fac2010-10-26 16:08:53 -07001544 * set the title for the tab
1545 */
1546 void setCurrentTitle(String title) {
1547 mCurrentTitle = title;
1548 }
1549
1550 /**
1551 * set url for this tab
1552 * @param url
1553 */
1554 void setCurrentUrl(String url) {
1555 mCurrentUrl = url;
1556 }
1557
1558 String getCurrentTitle() {
1559 return mCurrentTitle;
1560 }
1561
1562 String getCurrentUrl() {
1563 return mCurrentUrl;
1564 }
1565 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001566 * Get the url of this tab. Valid after calling populatePickerData, but
1567 * before calling wipePickerData, or if the webview has been destroyed.
1568 * @return The WebView's url or null.
1569 */
1570 String getUrl() {
1571 if (mPickerData != null) {
1572 return mPickerData.mUrl;
1573 }
1574 return null;
1575 }
1576
1577 /**
1578 * Get the title of this tab. Valid after calling populatePickerData, but
1579 * before calling wipePickerData, or if the webview has been destroyed. If
1580 * the url has no title, use the url instead.
1581 * @return The WebView's title (or url) or null.
1582 */
1583 String getTitle() {
1584 if (mPickerData != null) {
1585 return mPickerData.mTitle;
1586 }
1587 return null;
1588 }
1589
1590 /**
1591 * Get the favicon of this tab. Valid after calling populatePickerData, but
1592 * before calling wipePickerData, or if the webview has been destroyed.
1593 * @return The WebView's favicon or null.
1594 */
1595 Bitmap getFavicon() {
1596 if (mPickerData != null) {
1597 return mPickerData.mFavicon;
1598 }
1599 return null;
1600 }
1601
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001602
Grace Kloba22ac16e2009-10-07 18:00:23 -07001603 /**
1604 * Return the tab's error console. Creates the console if createIfNEcessary
1605 * is true and we haven't already created the console.
1606 * @param createIfNecessary Flag to indicate if the console should be
1607 * created if it has not been already.
1608 * @return The tab's error console, or null if one has not been created and
1609 * createIfNecessary is false.
1610 */
1611 ErrorConsoleView getErrorConsole(boolean createIfNecessary) {
1612 if (createIfNecessary && mErrorConsole == null) {
1613 mErrorConsole = new ErrorConsoleView(mActivity);
1614 mErrorConsole.setWebView(mMainView);
1615 }
1616 return mErrorConsole;
1617 }
1618
1619 /**
1620 * If this Tab was created through another Tab, then this method returns
1621 * that Tab.
1622 * @return the Tab parent or null
1623 */
1624 public Tab getParentTab() {
1625 return mParentTab;
1626 }
1627
1628 /**
1629 * Return whether this tab should be closed when it is backing out of the
1630 * first page.
1631 * @return TRUE if this tab should be closed when exit.
1632 */
1633 boolean closeOnExit() {
1634 return mCloseOnExit;
1635 }
1636
1637 /**
1638 * Saves the current lock-icon state before resetting the lock icon. If we
1639 * have an error, we may need to roll back to the previous state.
1640 */
1641 void resetLockIcon(String url) {
1642 mPrevLockIconType = mLockIconType;
Michael Kolb8233fac2010-10-26 16:08:53 -07001643 mLockIconType = LOCK_ICON_UNSECURE;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001644 if (URLUtil.isHttpsUrl(url)) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001645 mLockIconType = LOCK_ICON_SECURE;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001646 }
1647 }
1648
1649 /**
1650 * Reverts the lock-icon state to the last saved state, for example, if we
1651 * had an error, and need to cancel the load.
1652 */
1653 void revertLockIcon() {
1654 mLockIconType = mPrevLockIconType;
1655 }
1656
1657 /**
1658 * @return The tab's lock icon type.
1659 */
1660 int getLockIconType() {
1661 return mLockIconType;
1662 }
1663
1664 /**
1665 * @return TRUE if onPageStarted is called while onPageFinished is not
1666 * called yet.
1667 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001668 boolean inPageLoad() {
1669 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001670 }
1671
1672 // force mInLoad to be false. This should only be called before closing the
1673 // tab to ensure BrowserActivity's pauseWebViewTimers() is called correctly.
Michael Kolb8233fac2010-10-26 16:08:53 -07001674 void clearInPageLoad() {
1675 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001676 }
1677
1678 void populatePickerData() {
1679 if (mMainView == null) {
1680 populatePickerDataFromSavedState();
1681 return;
1682 }
1683
1684 // FIXME: The only place we cared about subwindow was for
1685 // bookmarking (i.e. not when saving state). Was this deliberate?
1686 final WebBackForwardList list = mMainView.copyBackForwardList();
Leon Scroggins70a153b2010-05-10 17:27:26 -04001687 if (list == null) {
1688 Log.w(LOGTAG, "populatePickerData called and WebBackForwardList is null");
1689 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001690 final WebHistoryItem item = list != null ? list.getCurrentItem() : null;
1691 populatePickerData(item);
1692 }
1693
1694 // Populate the picker data using the given history item and the current top
1695 // WebView.
1696 private void populatePickerData(WebHistoryItem item) {
1697 mPickerData = new PickerData();
Leon Scroggins70a153b2010-05-10 17:27:26 -04001698 if (item == null) {
1699 Log.w(LOGTAG, "populatePickerData called with a null WebHistoryItem");
1700 } else {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001701 mPickerData.mUrl = item.getUrl();
1702 mPickerData.mTitle = item.getTitle();
1703 mPickerData.mFavicon = item.getFavicon();
1704 if (mPickerData.mTitle == null) {
1705 mPickerData.mTitle = mPickerData.mUrl;
1706 }
1707 }
1708 }
1709
1710 // Create the PickerData and populate it using the saved state of the tab.
1711 void populatePickerDataFromSavedState() {
1712 if (mSavedState == null) {
1713 return;
1714 }
1715 mPickerData = new PickerData();
1716 mPickerData.mUrl = mSavedState.getString(CURRURL);
1717 mPickerData.mTitle = mSavedState.getString(CURRTITLE);
1718 }
1719
1720 void clearPickerData() {
1721 mPickerData = null;
1722 }
1723
1724 /**
1725 * Get the saved state bundle.
1726 * @return
1727 */
1728 Bundle getSavedState() {
1729 return mSavedState;
1730 }
1731
1732 /**
1733 * Set the saved state.
1734 */
1735 void setSavedState(Bundle state) {
1736 mSavedState = state;
1737 }
1738
1739 /**
1740 * @return TRUE if succeed in saving the state.
1741 */
1742 boolean saveState() {
1743 // If the WebView is null it means we ran low on memory and we already
1744 // stored the saved state in mSavedState.
1745 if (mMainView == null) {
1746 return mSavedState != null;
1747 }
1748
1749 mSavedState = new Bundle();
1750 final WebBackForwardList list = mMainView.saveState(mSavedState);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001751
1752 // Store some extra info for displaying the tab in the picker.
1753 final WebHistoryItem item = list != null ? list.getCurrentItem() : null;
1754 populatePickerData(item);
1755
1756 if (mPickerData.mUrl != null) {
1757 mSavedState.putString(CURRURL, mPickerData.mUrl);
1758 }
1759 if (mPickerData.mTitle != null) {
1760 mSavedState.putString(CURRTITLE, mPickerData.mTitle);
1761 }
1762 mSavedState.putBoolean(CLOSEONEXIT, mCloseOnExit);
1763 if (mAppId != null) {
1764 mSavedState.putString(APPID, mAppId);
1765 }
1766 if (mOriginalUrl != null) {
1767 mSavedState.putString(ORIGINALURL, mOriginalUrl);
1768 }
1769 // Remember the parent tab so the relationship can be restored.
1770 if (mParentTab != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001771 mSavedState.putInt(PARENTTAB, mWebViewController.getTabControl().getTabIndex(
Grace Kloba22ac16e2009-10-07 18:00:23 -07001772 mParentTab));
1773 }
1774 return true;
1775 }
1776
1777 /*
1778 * Restore the state of the tab.
1779 */
1780 boolean restoreState(Bundle b) {
1781 if (b == null) {
1782 return false;
1783 }
1784 // Restore the internal state even if the WebView fails to restore.
1785 // This will maintain the app id, original url and close-on-exit values.
1786 mSavedState = null;
1787 mPickerData = null;
1788 mCloseOnExit = b.getBoolean(CLOSEONEXIT);
1789 mAppId = b.getString(APPID);
1790 mOriginalUrl = b.getString(ORIGINALURL);
1791
1792 final WebBackForwardList list = mMainView.restoreState(b);
1793 if (list == null) {
1794 return false;
1795 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001796 return true;
1797 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04001798
Leon Scroggins1961ed22010-12-07 15:22:21 -05001799 public void updateBookmarkedStatus() {
1800 if (mMainView == null) {
1801 return;
1802 }
1803 String url = mMainView.getUrl();
1804 if (url == null) {
1805 return;
1806 }
1807 updateBookmarkedStatusForUrl(url);
1808 }
1809
1810 /**
1811 * Update mIsBookmarkedSite, using urlInQuestion to compare.
1812 * @param urlInQuestion URL of the current page, to be checked in the
1813 * bookmarks database.
1814 */
1815 private void updateBookmarkedStatusForUrl(final String urlInQuestion) {
1816 if (mBookmarkAsyncTask != null) {
1817 mBookmarkAsyncTask.cancel(true);
1818 }
1819 mBookmarkAsyncTask = new AsyncTask<Void, Void, Boolean>() {
1820 @Override
1821 protected Boolean doInBackground(Void... unused) {
1822 // Check to see if the site is bookmarked
1823 Cursor cursor = null;
1824 try {
1825 cursor = mActivity.getContentResolver().query(
1826 BrowserContract.Bookmarks.CONTENT_URI,
1827 new String[] { BrowserContract.Bookmarks.URL },
1828 BrowserContract.Bookmarks.URL + " == ?",
1829 new String[] { urlInQuestion },
1830 null);
1831 return cursor.moveToFirst();
1832 } catch (SQLiteException e) {
1833 Log.e(LOGTAG, "Error checking for bookmark: " + e);
1834 return false;
1835 } finally {
1836 if (cursor != null) cursor.close();
1837 }
1838 }
1839 @Override
1840 protected void onPostExecute(Boolean isBookmarked) {
1841 if (this == mBookmarkAsyncTask) {
1842 mIsBookmarkedSite = isBookmarked;
1843 mWebViewController.bookmarkedStatusHasChanged(Tab.this);
1844 }
1845 }
1846 };
1847 mBookmarkAsyncTask.execute();
1848 }
1849
Grace Kloba22ac16e2009-10-07 18:00:23 -07001850}