blob: 491c260dc145f93d8f85c977a930850f02537147 [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;
John Reck30c714c2010-12-16 17:30:34 -080025import android.content.Context;
Grace Kloba22ac16e2009-10-07 18:00:23 -070026import android.content.DialogInterface;
Michael Kolbfe251992010-07-08 15:41:55 -070027import android.content.DialogInterface.OnCancelListener;
Jeff Hamilton8ce956c2010-08-17 11:13:53 -050028import android.content.Intent;
Leon Scroggins4cd97792010-12-03 15:31:56 -050029import android.database.Cursor;
30import android.database.sqlite.SQLiteException;
Grace Kloba22ac16e2009-10-07 18:00:23 -070031import android.graphics.Bitmap;
John Reck30c714c2010-12-16 17:30:34 -080032import android.graphics.BitmapFactory;
Grace Kloba22ac16e2009-10-07 18:00:23 -070033import android.net.Uri;
34import android.net.http.SslError;
Leon Scroggins4cd97792010-12-03 15:31:56 -050035import android.os.AsyncTask;
Grace Kloba22ac16e2009-10-07 18:00:23 -070036import android.os.Bundle;
37import android.os.Message;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +000038import android.os.SystemClock;
Leon Scroggins4cd97792010-12-03 15:31:56 -050039import android.provider.BrowserContract;
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -050040import android.speech.RecognizerResultsIntent;
Grace Kloba22ac16e2009-10-07 18:00:23 -070041import android.util.Log;
42import android.view.KeyEvent;
43import android.view.LayoutInflater;
44import android.view.View;
Grace Kloba50c241e2010-04-20 11:07:50 -070045import android.view.ViewStub;
Ben Murdochc42addf2010-01-28 15:19:59 +000046import android.webkit.ConsoleMessage;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -050047import android.webkit.DownloadListener;
Grace Kloba22ac16e2009-10-07 18:00:23 -070048import android.webkit.GeolocationPermissions;
49import android.webkit.HttpAuthHandler;
50import android.webkit.SslErrorHandler;
51import android.webkit.URLUtil;
52import android.webkit.ValueCallback;
53import android.webkit.WebBackForwardList;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -050054import android.webkit.WebBackForwardListClient;
Grace Kloba22ac16e2009-10-07 18:00:23 -070055import android.webkit.WebChromeClient;
56import android.webkit.WebHistoryItem;
Grace Kloba22ac16e2009-10-07 18:00:23 -070057import android.webkit.WebStorage;
58import android.webkit.WebView;
59import android.webkit.WebViewClient;
Grace Kloba22ac16e2009-10-07 18:00:23 -070060import android.widget.LinearLayout;
61import android.widget.TextView;
Ben Murdoch8029a772010-11-16 11:58:21 +000062import android.widget.Toast;
Grace Kloba22ac16e2009-10-07 18:00:23 -070063
Michael Kolbfe251992010-07-08 15:41:55 -070064import java.util.ArrayList;
65import java.util.HashMap;
66import java.util.Iterator;
67import java.util.LinkedList;
68import java.util.Map;
69import java.util.Vector;
70
Grace Kloba22ac16e2009-10-07 18:00:23 -070071/**
72 * Class for maintaining Tabs with a main WebView and a subwindow.
73 */
74class Tab {
Michael Kolb8233fac2010-10-26 16:08:53 -070075
Grace Kloba22ac16e2009-10-07 18:00:23 -070076 // Log Tag
77 private static final String LOGTAG = "Tab";
Ben Murdochc42addf2010-01-28 15:19:59 +000078 // Special case the logtag for messages for the Console to make it easier to
79 // filter them and match the logtag used for these messages in older versions
80 // of the browser.
81 private static final String CONSOLE_LOGTAG = "browser";
82
John Reck30c714c2010-12-16 17:30:34 -080083 public enum LockIcon {
84 LOCK_ICON_UNSECURE,
85 LOCK_ICON_SECURE,
86 LOCK_ICON_MIXED,
87 }
Michael Kolb8233fac2010-10-26 16:08:53 -070088
89 Activity mActivity;
90 private WebViewController mWebViewController;
91
Grace Kloba22ac16e2009-10-07 18:00:23 -070092 // The Geolocation permissions prompt
93 private GeolocationPermissionsPrompt mGeolocationPermissionsPrompt;
94 // Main WebView wrapper
Michael Kolba713ec82010-11-29 17:27:06 -080095 private View mContainer;
Grace Kloba22ac16e2009-10-07 18:00:23 -070096 // Main WebView
97 private WebView mMainView;
98 // Subwindow container
99 private View mSubViewContainer;
100 // Subwindow WebView
101 private WebView mSubView;
102 // Saved bundle for when we are running low on memory. It contains the
103 // information needed to restore the WebView if the user goes back to the
104 // tab.
105 private Bundle mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700106 // Parent Tab. This is the Tab that created this Tab, or null if the Tab was
107 // created by the UI
108 private Tab mParentTab;
109 // Tab that constructed by this Tab. This is used when this Tab is
110 // destroyed, it clears all mParentTab values in the children.
111 private Vector<Tab> mChildTabs;
112 // If true, the tab will be removed when back out of the first page.
113 private boolean mCloseOnExit;
114 // If true, the tab is in the foreground of the current activity.
115 private boolean mInForeground;
Michael Kolb8233fac2010-10-26 16:08:53 -0700116 // If true, the tab is in page loading state (after onPageStarted,
117 // before onPageFinsihed)
118 private boolean mInPageLoad;
John Reck30c714c2010-12-16 17:30:34 -0800119 // The last reported progress of the current page
120 private int mPageLoadProgress;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000121 // The time the load started, used to find load page time
122 private long mLoadStartTime;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700123 // Application identifier used to find tabs that another application wants
124 // to reuse.
125 private String mAppId;
126 // Keep the original url around to avoid killing the old WebView if the url
127 // has not changed.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700128 // Error console for the tab
129 private ErrorConsoleView mErrorConsole;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -0500130 // The listener that gets invoked when a download is started from the
131 // mMainView
132 private final DownloadListener mDownloadListener;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500133 // Listener used to know when we move forward or back in the history list.
134 private final WebBackForwardListClient mWebBackForwardListClient;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700135
136 // AsyncTask for downloading touch icons
137 DownloadTouchIcon mTouchIconLoader;
138
John Reck30c714c2010-12-16 17:30:34 -0800139 // All the state needed for a page
140 private static class PageState {
141 String mUrl;
142 String mTitle;
143 LockIcon mLockIcon;
144 Bitmap mFavicon;
145
146 PageState(Context c, boolean incognito) {
147 if (incognito) {
148 mUrl = "browser:incognito";
149 mTitle = c.getString(R.string.new_incognito_tab);
150 mFavicon = BitmapFactory.decodeResource(
151 c.getResources(), R.drawable.fav_incognito);
152 } else {
153 mUrl = "";
154 mTitle = c.getString(R.string.new_tab);
155 mFavicon = BitmapFactory.decodeResource(
156 c.getResources(), R.drawable.app_web_browser_sm);
157 }
158 mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
159 }
160
161 PageState(Context c, boolean incognito, String url, Bitmap favicon) {
162 mUrl = url;
163 mTitle = null;
164 if (URLUtil.isHttpsUrl(url)) {
165 mLockIcon = LockIcon.LOCK_ICON_SECURE;
166 } else {
167 mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
168 }
169 if (favicon != null) {
170 mFavicon = favicon;
171 } else {
172 if (incognito) {
173 mFavicon = BitmapFactory.decodeResource(
174 c.getResources(), R.drawable.fav_incognito);
175 } else {
176 mFavicon = BitmapFactory.decodeResource(
177 c.getResources(), R.drawable.app_web_browser_sm);
178 }
179 }
180 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700181 }
182
John Reck30c714c2010-12-16 17:30:34 -0800183 // The current/loading page's state
184 private PageState mCurrentState;
185
Leon Scroggins4cd97792010-12-03 15:31:56 -0500186 // Whether or not the currently shown page is a bookmarked site. Will be
187 // out of date when loading a new page until the mBookmarkAsyncTask returns.
188 private boolean mIsBookmarkedSite;
189 // Used to determine whether the current site is bookmarked.
190 private AsyncTask<Void, Void, Boolean> mBookmarkAsyncTask;
191
192 public boolean isBookmarkedSite() { return mIsBookmarkedSite; }
193
Grace Kloba22ac16e2009-10-07 18:00:23 -0700194 // Used for saving and restoring each Tab
John Reck30c714c2010-12-16 17:30:34 -0800195 // TODO: Figure out who uses what and where
196 // Some of these aren't use in this class, and some are only used in
197 // restoring state but not saving it - FIX THIS
Grace Kloba22ac16e2009-10-07 18:00:23 -0700198 static final String WEBVIEW = "webview";
199 static final String NUMTABS = "numTabs";
200 static final String CURRTAB = "currentTab";
201 static final String CURRURL = "currentUrl";
202 static final String CURRTITLE = "currentTitle";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700203 static final String CLOSEONEXIT = "closeonexit";
204 static final String PARENTTAB = "parentTab";
205 static final String APPID = "appid";
206 static final String ORIGINALURL = "originalUrl";
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700207 static final String INCOGNITO = "privateBrowsingEnabled";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700208
209 // -------------------------------------------------------------------------
210
Leon Scroggins58d56c62010-01-28 15:12:40 -0500211 /**
212 * Private information regarding the latest voice search. If the Tab is not
213 * in voice search mode, this will be null.
214 */
215 private VoiceSearchData mVoiceSearchData;
216 /**
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400217 * Remove voice search mode from this tab.
218 */
219 public void revertVoiceSearchMode() {
220 if (mVoiceSearchData != null) {
221 mVoiceSearchData = null;
222 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700223 mWebViewController.revertVoiceSearchMode(this);
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400224 }
225 }
226 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700227
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400228 /**
Leon Scroggins58d56c62010-01-28 15:12:40 -0500229 * Return whether the tab is in voice search mode.
230 */
231 public boolean isInVoiceSearchMode() {
232 return mVoiceSearchData != null;
233 }
234 /**
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400235 * Return true if the Tab is in voice search mode and the voice search
236 * Intent came with a String identifying that Google provided the Intent.
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500237 */
238 public boolean voiceSearchSourceIsGoogle() {
239 return mVoiceSearchData != null && mVoiceSearchData.mSourceIsGoogle;
240 }
241 /**
Leon Scroggins58d56c62010-01-28 15:12:40 -0500242 * Get the title to display for the current voice search page. If the Tab
243 * is not in voice search mode, return null.
244 */
245 public String getVoiceDisplayTitle() {
246 if (mVoiceSearchData == null) return null;
247 return mVoiceSearchData.mLastVoiceSearchTitle;
248 }
249 /**
250 * Get the latest array of voice search results, to be passed to the
251 * BrowserProvider. If the Tab is not in voice search mode, return null.
252 */
253 public ArrayList<String> getVoiceSearchResults() {
254 if (mVoiceSearchData == null) return null;
255 return mVoiceSearchData.mVoiceSearchResults;
256 }
257 /**
258 * Activate voice search mode.
259 * @param intent Intent which has the results to use, or an index into the
260 * results when reusing the old results.
261 */
262 /* package */ void activateVoiceSearchMode(Intent intent) {
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500263 int index = 0;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500264 ArrayList<String> results = intent.getStringArrayListExtra(
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -0500265 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_STRINGS);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500266 if (results != null) {
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500267 ArrayList<String> urls = intent.getStringArrayListExtra(
268 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_URLS);
269 ArrayList<String> htmls = intent.getStringArrayListExtra(
270 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_HTML);
271 ArrayList<String> baseUrls = intent.getStringArrayListExtra(
272 RecognizerResultsIntent
273 .EXTRA_VOICE_SEARCH_RESULT_HTML_BASE_URLS);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500274 // This tab is now entering voice search mode for the first time, or
275 // a new voice search was done.
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500276 int size = results.size();
277 if (urls == null || size != urls.size()) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500278 throw new AssertionError("improper extras passed in Intent");
279 }
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500280 if (htmls == null || htmls.size() != size || baseUrls == null ||
281 (baseUrls.size() != size && baseUrls.size() != 1)) {
282 // If either of these arrays are empty/incorrectly sized, ignore
283 // them.
284 htmls = null;
285 baseUrls = null;
286 }
287 mVoiceSearchData = new VoiceSearchData(results, urls, htmls,
288 baseUrls);
Leon Scroggins9df94972010-03-08 18:20:35 -0500289 mVoiceSearchData.mHeaders = intent.getParcelableArrayListExtra(
290 RecognizerResultsIntent
291 .EXTRA_VOICE_SEARCH_RESULT_HTTP_HEADERS);
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500292 mVoiceSearchData.mSourceIsGoogle = intent.getBooleanExtra(
293 VoiceSearchData.SOURCE_IS_GOOGLE, false);
Leon Scroggins2ee4a5a2010-03-15 16:56:57 -0400294 mVoiceSearchData.mVoiceSearchIntent = new Intent(intent);
Leon Scrogginse10dde52010-03-08 19:53:03 -0500295 }
296 String extraData = intent.getStringExtra(
297 SearchManager.EXTRA_DATA_KEY);
298 if (extraData != null) {
299 index = Integer.parseInt(extraData);
300 if (index >= mVoiceSearchData.mVoiceSearchResults.size()) {
301 throw new AssertionError("index must be less than "
302 + "size of mVoiceSearchResults");
303 }
304 if (mVoiceSearchData.mSourceIsGoogle) {
305 Intent logIntent = new Intent(
306 LoggingEvents.ACTION_LOG_EVENT);
307 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
308 LoggingEvents.VoiceSearch.N_BEST_CHOOSE);
309 logIntent.putExtra(
310 LoggingEvents.VoiceSearch.EXTRA_N_BEST_CHOOSE_INDEX,
311 index);
312 mActivity.sendBroadcast(logIntent);
313 }
314 if (mVoiceSearchData.mVoiceSearchIntent != null) {
Leon Scroggins2ee4a5a2010-03-15 16:56:57 -0400315 // Copy the Intent, so that each history item will have its own
316 // Intent, with different (or none) extra data.
317 Intent latest = new Intent(mVoiceSearchData.mVoiceSearchIntent);
318 latest.putExtra(SearchManager.EXTRA_DATA_KEY, extraData);
319 mVoiceSearchData.mVoiceSearchIntent = latest;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500320 }
321 }
322 mVoiceSearchData.mLastVoiceSearchTitle
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500323 = mVoiceSearchData.mVoiceSearchResults.get(index);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500324 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700325 mWebViewController.activateVoiceSearchMode(mVoiceSearchData.mLastVoiceSearchTitle);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500326 }
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500327 if (mVoiceSearchData.mVoiceSearchHtmls != null) {
328 // When index was found it was already ensured that it was valid
329 String uriString = mVoiceSearchData.mVoiceSearchHtmls.get(index);
330 if (uriString != null) {
331 Uri dataUri = Uri.parse(uriString);
332 if (RecognizerResultsIntent.URI_SCHEME_INLINE.equals(
333 dataUri.getScheme())) {
334 // If there is only one base URL, use it. If there are
335 // more, there will be one for each index, so use the base
336 // URL corresponding to the index.
337 String baseUrl = mVoiceSearchData.mVoiceSearchBaseUrls.get(
338 mVoiceSearchData.mVoiceSearchBaseUrls.size() > 1 ?
339 index : 0);
340 mVoiceSearchData.mLastVoiceSearchUrl = baseUrl;
341 mMainView.loadDataWithBaseURL(baseUrl,
342 uriString.substring(RecognizerResultsIntent
343 .URI_SCHEME_INLINE.length() + 1), "text/html",
344 "utf-8", baseUrl);
345 return;
346 }
347 }
348 }
Leon Scroggins58d56c62010-01-28 15:12:40 -0500349 mVoiceSearchData.mLastVoiceSearchUrl
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500350 = mVoiceSearchData.mVoiceSearchUrls.get(index);
351 if (null == mVoiceSearchData.mLastVoiceSearchUrl) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700352 mVoiceSearchData.mLastVoiceSearchUrl = UrlUtils.smartUrlFilter(
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500353 mVoiceSearchData.mLastVoiceSearchTitle);
354 }
Leon Scroggins9df94972010-03-08 18:20:35 -0500355 Map<String, String> headers = null;
356 if (mVoiceSearchData.mHeaders != null) {
357 int bundleIndex = mVoiceSearchData.mHeaders.size() == 1 ? 0
358 : index;
359 Bundle bundle = mVoiceSearchData.mHeaders.get(bundleIndex);
360 if (bundle != null && !bundle.isEmpty()) {
361 Iterator<String> iter = bundle.keySet().iterator();
362 headers = new HashMap<String, String>();
363 while (iter.hasNext()) {
364 String key = iter.next();
365 headers.put(key, bundle.getString(key));
366 }
367 }
368 }
369 mMainView.loadUrl(mVoiceSearchData.mLastVoiceSearchUrl, headers);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500370 }
371 /* package */ static class VoiceSearchData {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500372 public VoiceSearchData(ArrayList<String> results,
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500373 ArrayList<String> urls, ArrayList<String> htmls,
374 ArrayList<String> baseUrls) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500375 mVoiceSearchResults = results;
376 mVoiceSearchUrls = urls;
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500377 mVoiceSearchHtmls = htmls;
378 mVoiceSearchBaseUrls = baseUrls;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500379 }
380 /*
381 * ArrayList of suggestions to be displayed when opening the
382 * SearchManager
383 */
384 public ArrayList<String> mVoiceSearchResults;
385 /*
386 * ArrayList of urls, associated with the suggestions in
387 * mVoiceSearchResults.
388 */
389 public ArrayList<String> mVoiceSearchUrls;
390 /*
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500391 * ArrayList holding content to load for each item in
392 * mVoiceSearchResults.
393 */
394 public ArrayList<String> mVoiceSearchHtmls;
395 /*
396 * ArrayList holding base urls for the items in mVoiceSearchResults.
397 * If non null, this will either have the same size as
398 * mVoiceSearchResults or have a size of 1, in which case all will use
399 * the same base url
400 */
401 public ArrayList<String> mVoiceSearchBaseUrls;
402 /*
Leon Scroggins58d56c62010-01-28 15:12:40 -0500403 * The last url provided by voice search. Used for comparison to see if
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500404 * we are going to a page by some method besides voice search.
Leon Scroggins58d56c62010-01-28 15:12:40 -0500405 */
406 public String mLastVoiceSearchUrl;
407 /**
408 * The last title used for voice search. Needed to update the title bar
409 * when switching tabs.
410 */
411 public String mLastVoiceSearchTitle;
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500412 /**
413 * Whether the Intent which turned on voice search mode contained the
414 * String signifying that Google was the source.
415 */
416 public boolean mSourceIsGoogle;
417 /**
Leon Scroggins9df94972010-03-08 18:20:35 -0500418 * List of headers to be passed into the WebView containing location
419 * information
420 */
421 public ArrayList<Bundle> mHeaders;
422 /**
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500423 * The Intent used to invoke voice search. Placed on the
424 * WebHistoryItem so that when coming back to a previous voice search
425 * page we can again activate voice search.
426 */
Leon Scrogginse10dde52010-03-08 19:53:03 -0500427 public Intent mVoiceSearchIntent;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500428 /**
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500429 * String used to identify Google as the source of voice search.
430 */
431 public static String SOURCE_IS_GOOGLE
432 = "android.speech.extras.SOURCE_IS_GOOGLE";
Leon Scroggins58d56c62010-01-28 15:12:40 -0500433 }
434
Grace Kloba22ac16e2009-10-07 18:00:23 -0700435 // Container class for the next error dialog that needs to be displayed
436 private class ErrorDialog {
437 public final int mTitle;
438 public final String mDescription;
439 public final int mError;
440 ErrorDialog(int title, String desc, int error) {
441 mTitle = title;
442 mDescription = desc;
443 mError = error;
444 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700445 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700446
447 private void processNextError() {
448 if (mQueuedErrors == null) {
449 return;
450 }
451 // The first one is currently displayed so just remove it.
452 mQueuedErrors.removeFirst();
453 if (mQueuedErrors.size() == 0) {
454 mQueuedErrors = null;
455 return;
456 }
457 showError(mQueuedErrors.getFirst());
458 }
459
460 private DialogInterface.OnDismissListener mDialogListener =
461 new DialogInterface.OnDismissListener() {
462 public void onDismiss(DialogInterface d) {
463 processNextError();
464 }
465 };
466 private LinkedList<ErrorDialog> mQueuedErrors;
467
468 private void queueError(int err, String desc) {
469 if (mQueuedErrors == null) {
470 mQueuedErrors = new LinkedList<ErrorDialog>();
471 }
472 for (ErrorDialog d : mQueuedErrors) {
473 if (d.mError == err) {
474 // Already saw a similar error, ignore the new one.
475 return;
476 }
477 }
478 ErrorDialog errDialog = new ErrorDialog(
479 err == WebViewClient.ERROR_FILE_NOT_FOUND ?
480 R.string.browserFrameFileErrorLabel :
481 R.string.browserFrameNetworkErrorLabel,
482 desc, err);
483 mQueuedErrors.addLast(errDialog);
484
485 // Show the dialog now if the queue was empty and it is in foreground
486 if (mQueuedErrors.size() == 1 && mInForeground) {
487 showError(errDialog);
488 }
489 }
490
491 private void showError(ErrorDialog errDialog) {
492 if (mInForeground) {
493 AlertDialog d = new AlertDialog.Builder(mActivity)
494 .setTitle(errDialog.mTitle)
495 .setMessage(errDialog.mDescription)
496 .setPositiveButton(R.string.ok, null)
497 .create();
498 d.setOnDismissListener(mDialogListener);
499 d.show();
500 }
501 }
502
503 // -------------------------------------------------------------------------
504 // WebViewClient implementation for the main WebView
505 // -------------------------------------------------------------------------
506
507 private final WebViewClient mWebViewClient = new WebViewClient() {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500508 private Message mDontResend;
509 private Message mResend;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700510 @Override
511 public void onPageStarted(WebView view, String url, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700512 mInPageLoad = true;
John Reck30c714c2010-12-16 17:30:34 -0800513 mPageLoadProgress = 0;
514 mCurrentState = new PageState(mActivity,
515 view.isPrivateBrowsingEnabled(), url, favicon);
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000516 mLoadStartTime = SystemClock.uptimeMillis();
Leon Scroggins58d56c62010-01-28 15:12:40 -0500517 if (mVoiceSearchData != null
518 && !url.equals(mVoiceSearchData.mLastVoiceSearchUrl)) {
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500519 if (mVoiceSearchData.mSourceIsGoogle) {
520 Intent i = new Intent(LoggingEvents.ACTION_LOG_EVENT);
521 i.putExtra(LoggingEvents.EXTRA_FLUSH, true);
522 mActivity.sendBroadcast(i);
523 }
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400524 revertVoiceSearchMode();
Leon Scroggins58d56c62010-01-28 15:12:40 -0500525 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700526
Grace Kloba22ac16e2009-10-07 18:00:23 -0700527
528 // If we start a touch icon load and then load a new page, we don't
529 // want to cancel the current touch icon loader. But, we do want to
530 // create a new one when the touch icon url is known.
531 if (mTouchIconLoader != null) {
532 mTouchIconLoader.mTab = null;
533 mTouchIconLoader = null;
534 }
535
536 // reset the error console
537 if (mErrorConsole != null) {
538 mErrorConsole.clearErrorMessages();
Michael Kolb8233fac2010-10-26 16:08:53 -0700539 if (mWebViewController.shouldShowErrorConsole()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700540 mErrorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
541 }
542 }
543
Grace Kloba22ac16e2009-10-07 18:00:23 -0700544 // finally update the UI in the activity if it is in the foreground
Michael Kolb8233fac2010-10-26 16:08:53 -0700545 mWebViewController.onPageStarted(Tab.this, view, url, favicon);
Leon Scroggins4cd97792010-12-03 15:31:56 -0500546
Leon Scroggins1961ed22010-12-07 15:22:21 -0500547 updateBookmarkedStatusForUrl(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700548 }
549
550 @Override
551 public void onPageFinished(WebView view, String url) {
John Reck5b691842010-11-29 11:21:13 -0800552 if (!isPrivateBrowsingEnabled()) {
553 LogTag.logPageFinishedLoading(
554 url, SystemClock.uptimeMillis() - mLoadStartTime);
555 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700556 mInPageLoad = false;
John Reck30c714c2010-12-16 17:30:34 -0800557 // Sync state (in case of stop/timeout)
558 mCurrentState.mUrl = view.getUrl();
559 mCurrentState.mTitle = view.getTitle();
560 mCurrentState.mFavicon = view.getFavicon();
561 if (!URLUtil.isHttpsUrl(mCurrentState.mUrl)) {
562 // In case we stop when loading an HTTPS page from an HTTP page
563 // but before a provisional load occurred
564 mCurrentState.mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
565 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700566 mWebViewController.onPageFinished(Tab.this, url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700567 }
568
569 // return true if want to hijack the url to let another app to handle it
570 @Override
571 public boolean shouldOverrideUrlLoading(WebView view, String url) {
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400572 if (voiceSearchSourceIsGoogle()) {
573 // This method is called when the user clicks on a link.
574 // VoiceSearchMode is turned off when the user leaves the
575 // Google results page, so at this point the user must be on
576 // that page. If the user clicked a link on that page, assume
577 // that the voice search was effective, and broadcast an Intent
578 // so a receiver can take note of that fact.
579 Intent logIntent = new Intent(LoggingEvents.ACTION_LOG_EVENT);
580 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
581 LoggingEvents.VoiceSearch.RESULT_CLICKED);
582 mActivity.sendBroadcast(logIntent);
583 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700584 if (mInForeground) {
Michael Kolb18eb3772010-12-10 14:29:51 -0800585 return mWebViewController.shouldOverrideUrlLoading(Tab.this,
586 view, url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700587 } else {
588 return false;
589 }
590 }
591
592 /**
593 * Updates the lock icon. This method is called when we discover another
594 * resource to be loaded for this page (for example, javascript). While
595 * we update the icon type, we do not update the lock icon itself until
596 * we are done loading, it is slightly more secure this way.
597 */
598 @Override
599 public void onLoadResource(WebView view, String url) {
600 if (url != null && url.length() > 0) {
601 // It is only if the page claims to be secure that we may have
602 // to update the lock:
John Reck30c714c2010-12-16 17:30:34 -0800603 if (mCurrentState.mLockIcon == LockIcon.LOCK_ICON_SECURE) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700604 // If NOT a 'safe' url, change the lock to mixed content!
605 if (!(URLUtil.isHttpsUrl(url) || URLUtil.isDataUrl(url)
606 || URLUtil.isAboutUrl(url))) {
John Reck30c714c2010-12-16 17:30:34 -0800607 mCurrentState.mLockIcon = LockIcon.LOCK_ICON_MIXED;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700608 }
609 }
610 }
611 }
612
613 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700614 * Show a dialog informing the user of the network error reported by
615 * WebCore if it is in the foreground.
616 */
617 @Override
618 public void onReceivedError(WebView view, int errorCode,
619 String description, String failingUrl) {
620 if (errorCode != WebViewClient.ERROR_HOST_LOOKUP &&
621 errorCode != WebViewClient.ERROR_CONNECT &&
622 errorCode != WebViewClient.ERROR_BAD_URL &&
623 errorCode != WebViewClient.ERROR_UNSUPPORTED_SCHEME &&
624 errorCode != WebViewClient.ERROR_FILE) {
625 queueError(errorCode, description);
626 }
Jeff Hamilton47654f42010-09-07 09:57:51 -0500627
628 // Don't log URLs when in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -0700629 if (!isPrivateBrowsingEnabled()) {
Jeff Hamilton47654f42010-09-07 09:57:51 -0500630 Log.e(LOGTAG, "onReceivedError " + errorCode + " " + failingUrl
631 + " " + description);
632 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700633 }
634
635 /**
636 * Check with the user if it is ok to resend POST data as the page they
637 * are trying to navigate to is the result of a POST.
638 */
639 @Override
640 public void onFormResubmission(WebView view, final Message dontResend,
641 final Message resend) {
642 if (!mInForeground) {
643 dontResend.sendToTarget();
644 return;
645 }
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500646 if (mDontResend != null) {
647 Log.w(LOGTAG, "onFormResubmission should not be called again "
648 + "while dialog is still up");
649 dontResend.sendToTarget();
650 return;
651 }
652 mDontResend = dontResend;
653 mResend = resend;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700654 new AlertDialog.Builder(mActivity).setTitle(
655 R.string.browserFrameFormResubmitLabel).setMessage(
656 R.string.browserFrameFormResubmitMessage)
657 .setPositiveButton(R.string.ok,
658 new DialogInterface.OnClickListener() {
659 public void onClick(DialogInterface dialog,
660 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500661 if (mResend != null) {
662 mResend.sendToTarget();
663 mResend = null;
664 mDontResend = null;
665 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700666 }
667 }).setNegativeButton(R.string.cancel,
668 new DialogInterface.OnClickListener() {
669 public void onClick(DialogInterface dialog,
670 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500671 if (mDontResend != null) {
672 mDontResend.sendToTarget();
673 mResend = null;
674 mDontResend = null;
675 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700676 }
677 }).setOnCancelListener(new OnCancelListener() {
678 public void onCancel(DialogInterface dialog) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500679 if (mDontResend != null) {
680 mDontResend.sendToTarget();
681 mResend = null;
682 mDontResend = null;
683 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700684 }
685 }).show();
686 }
687
688 /**
689 * Insert the url into the visited history database.
690 * @param url The url to be inserted.
691 * @param isReload True if this url is being reloaded.
692 * FIXME: Not sure what to do when reloading the page.
693 */
694 @Override
695 public void doUpdateVisitedHistory(WebView view, String url,
696 boolean isReload) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700697 mWebViewController.doUpdateVisitedHistory(Tab.this, url, isReload);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700698 }
699
700 /**
701 * Displays SSL error(s) dialog to the user.
702 */
703 @Override
704 public void onReceivedSslError(final WebView view,
705 final SslErrorHandler handler, final SslError error) {
706 if (!mInForeground) {
707 handler.cancel();
John Reck30c714c2010-12-16 17:30:34 -0800708 setLockIconType(LockIcon.LOCK_ICON_UNSECURE);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700709 return;
710 }
711 if (BrowserSettings.getInstance().showSecurityWarnings()) {
712 final LayoutInflater factory =
713 LayoutInflater.from(mActivity);
714 final View warningsView =
715 factory.inflate(R.layout.ssl_warnings, null);
716 final LinearLayout placeholder =
717 (LinearLayout)warningsView.findViewById(R.id.placeholder);
718
719 if (error.hasError(SslError.SSL_UNTRUSTED)) {
720 LinearLayout ll = (LinearLayout)factory
721 .inflate(R.layout.ssl_warning, null);
722 ((TextView)ll.findViewById(R.id.warning))
723 .setText(R.string.ssl_untrusted);
724 placeholder.addView(ll);
725 }
726
727 if (error.hasError(SslError.SSL_IDMISMATCH)) {
728 LinearLayout ll = (LinearLayout)factory
729 .inflate(R.layout.ssl_warning, null);
730 ((TextView)ll.findViewById(R.id.warning))
731 .setText(R.string.ssl_mismatch);
732 placeholder.addView(ll);
733 }
734
735 if (error.hasError(SslError.SSL_EXPIRED)) {
736 LinearLayout ll = (LinearLayout)factory
737 .inflate(R.layout.ssl_warning, null);
738 ((TextView)ll.findViewById(R.id.warning))
739 .setText(R.string.ssl_expired);
740 placeholder.addView(ll);
741 }
742
743 if (error.hasError(SslError.SSL_NOTYETVALID)) {
744 LinearLayout ll = (LinearLayout)factory
745 .inflate(R.layout.ssl_warning, null);
746 ((TextView)ll.findViewById(R.id.warning))
747 .setText(R.string.ssl_not_yet_valid);
748 placeholder.addView(ll);
749 }
750
751 new AlertDialog.Builder(mActivity).setTitle(
752 R.string.security_warning).setIcon(
753 android.R.drawable.ic_dialog_alert).setView(
754 warningsView).setPositiveButton(R.string.ssl_continue,
755 new DialogInterface.OnClickListener() {
756 public void onClick(DialogInterface dialog,
757 int whichButton) {
758 handler.proceed();
759 }
760 }).setNeutralButton(R.string.view_certificate,
761 new DialogInterface.OnClickListener() {
762 public void onClick(DialogInterface dialog,
763 int whichButton) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700764 mWebViewController.showSslCertificateOnError(view,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700765 handler, error);
766 }
Ben Murdocha49b8292010-11-16 11:56:04 +0000767 }).setNegativeButton(R.string.ssl_go_back,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700768 new DialogInterface.OnClickListener() {
769 public void onClick(DialogInterface dialog,
770 int whichButton) {
John Reck30c714c2010-12-16 17:30:34 -0800771 dialog.cancel();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700772 }
773 }).setOnCancelListener(
774 new DialogInterface.OnCancelListener() {
775 public void onCancel(DialogInterface dialog) {
776 handler.cancel();
John Reck30c714c2010-12-16 17:30:34 -0800777 setLockIconType(LockIcon.LOCK_ICON_UNSECURE);
778 mWebViewController.onUserCanceledSsl(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700779 }
780 }).show();
781 } else {
782 handler.proceed();
783 }
784 }
785
786 /**
787 * Handles an HTTP authentication request.
788 *
789 * @param handler The authentication handler
790 * @param host The host
791 * @param realm The realm
792 */
793 @Override
794 public void onReceivedHttpAuthRequest(WebView view,
795 final HttpAuthHandler handler, final String host,
796 final String realm) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700797 mWebViewController.onReceivedHttpAuthRequest(Tab.this, view, handler, host, realm);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700798 }
799
800 @Override
801 public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
802 if (!mInForeground) {
803 return false;
804 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700805 return mWebViewController.shouldOverrideKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700806 }
807
808 @Override
809 public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700810 if (!mInForeground) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700811 return;
812 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700813 mWebViewController.onUnhandledKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700814 }
815 };
816
817 // -------------------------------------------------------------------------
818 // WebChromeClient implementation for the main WebView
819 // -------------------------------------------------------------------------
820
821 private final WebChromeClient mWebChromeClient = new WebChromeClient() {
822 // Helper method to create a new tab or sub window.
823 private void createWindow(final boolean dialog, final Message msg) {
824 WebView.WebViewTransport transport =
825 (WebView.WebViewTransport) msg.obj;
826 if (dialog) {
827 createSubWindow();
Michael Kolb8233fac2010-10-26 16:08:53 -0700828 mWebViewController.attachSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700829 transport.setWebView(mSubView);
830 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -0700831 final Tab newTab = mWebViewController.openTabAndShow(
Michael Kolb18eb3772010-12-10 14:29:51 -0800832 Tab.this,
Michael Kolb8233fac2010-10-26 16:08:53 -0700833 IntentHandler.EMPTY_URL_DATA, false, null);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700834 if (newTab != Tab.this) {
835 Tab.this.addChildTab(newTab);
836 }
837 transport.setWebView(newTab.getWebView());
838 }
839 msg.sendToTarget();
840 }
841
842 @Override
843 public boolean onCreateWindow(WebView view, final boolean dialog,
844 final boolean userGesture, final Message resultMsg) {
845 // only allow new window or sub window for the foreground case
846 if (!mInForeground) {
847 return false;
848 }
849 // Short-circuit if we can't create any more tabs or sub windows.
850 if (dialog && mSubView != null) {
851 new AlertDialog.Builder(mActivity)
852 .setTitle(R.string.too_many_subwindows_dialog_title)
853 .setIcon(android.R.drawable.ic_dialog_alert)
854 .setMessage(R.string.too_many_subwindows_dialog_message)
855 .setPositiveButton(R.string.ok, null)
856 .show();
857 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700858 } else if (!mWebViewController.getTabControl().canCreateNewTab()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700859 new AlertDialog.Builder(mActivity)
860 .setTitle(R.string.too_many_windows_dialog_title)
861 .setIcon(android.R.drawable.ic_dialog_alert)
862 .setMessage(R.string.too_many_windows_dialog_message)
863 .setPositiveButton(R.string.ok, null)
864 .show();
865 return false;
866 }
867
868 // Short-circuit if this was a user gesture.
869 if (userGesture) {
870 createWindow(dialog, resultMsg);
871 return true;
872 }
873
874 // Allow the popup and create the appropriate window.
875 final AlertDialog.OnClickListener allowListener =
876 new AlertDialog.OnClickListener() {
877 public void onClick(DialogInterface d,
878 int which) {
879 createWindow(dialog, resultMsg);
880 }
881 };
882
883 // Block the popup by returning a null WebView.
884 final AlertDialog.OnClickListener blockListener =
885 new AlertDialog.OnClickListener() {
886 public void onClick(DialogInterface d, int which) {
887 resultMsg.sendToTarget();
888 }
889 };
890
891 // Build a confirmation dialog to display to the user.
892 final AlertDialog d =
893 new AlertDialog.Builder(mActivity)
894 .setTitle(R.string.attention)
895 .setIcon(android.R.drawable.ic_dialog_alert)
896 .setMessage(R.string.popup_window_attempt)
897 .setPositiveButton(R.string.allow, allowListener)
898 .setNegativeButton(R.string.block, blockListener)
899 .setCancelable(false)
900 .create();
901
902 // Show the confirmation dialog.
903 d.show();
904 return true;
905 }
906
907 @Override
Patrick Scotteb5061b2009-11-18 15:00:30 -0500908 public void onRequestFocus(WebView view) {
909 if (!mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700910 mWebViewController.switchToTab(mWebViewController.getTabControl().getTabIndex(
Patrick Scotteb5061b2009-11-18 15:00:30 -0500911 Tab.this));
912 }
913 }
914
915 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700916 public void onCloseWindow(WebView window) {
917 if (mParentTab != null) {
918 // JavaScript can only close popup window.
919 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700920 mWebViewController.switchToTab(mWebViewController.getTabControl()
Grace Kloba22ac16e2009-10-07 18:00:23 -0700921 .getTabIndex(mParentTab));
922 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700923 mWebViewController.closeTab(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700924 }
925 }
926
927 @Override
928 public void onProgressChanged(WebView view, int newProgress) {
John Reck30c714c2010-12-16 17:30:34 -0800929 mPageLoadProgress = newProgress;
930 mWebViewController.onProgressChanged(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700931 }
932
933 @Override
Leon Scroggins21d9b902010-03-11 09:33:11 -0500934 public void onReceivedTitle(WebView view, final String title) {
John Reck30c714c2010-12-16 17:30:34 -0800935 mCurrentState.mTitle = title;
Michael Kolb8233fac2010-10-26 16:08:53 -0700936 mWebViewController.onReceivedTitle(Tab.this, title);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700937 }
938
939 @Override
940 public void onReceivedIcon(WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -0800941 mCurrentState.mFavicon = icon;
Michael Kolb8233fac2010-10-26 16:08:53 -0700942 mWebViewController.onFavicon(Tab.this, view, icon);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700943 }
944
945 @Override
946 public void onReceivedTouchIconUrl(WebView view, String url,
947 boolean precomposed) {
948 final ContentResolver cr = mActivity.getContentResolver();
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400949 // Let precomposed icons take precedence over non-composed
950 // icons.
951 if (precomposed && mTouchIconLoader != null) {
952 mTouchIconLoader.cancel(false);
953 mTouchIconLoader = null;
954 }
955 // Have only one async task at a time.
956 if (mTouchIconLoader == null) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700957 mTouchIconLoader = new DownloadTouchIcon(Tab.this,
958 mActivity, cr, view);
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400959 mTouchIconLoader.execute(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700960 }
961 }
962
963 @Override
964 public void onShowCustomView(View view,
965 WebChromeClient.CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700966 if (mInForeground) mWebViewController.showCustomView(Tab.this, view,
967 callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700968 }
969
970 @Override
971 public void onHideCustomView() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700972 if (mInForeground) mWebViewController.hideCustomView();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700973 }
974
975 /**
976 * The origin has exceeded its database quota.
977 * @param url the URL that exceeded the quota
978 * @param databaseIdentifier the identifier of the database on which the
979 * transaction that caused the quota overflow was run
980 * @param currentQuota the current quota for the origin.
981 * @param estimatedSize the estimated size of the database.
982 * @param totalUsedQuota is the sum of all origins' quota.
983 * @param quotaUpdater The callback to run when a decision to allow or
984 * deny quota has been made. Don't forget to call this!
985 */
986 @Override
987 public void onExceededDatabaseQuota(String url,
988 String databaseIdentifier, long currentQuota, long estimatedSize,
989 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
990 BrowserSettings.getInstance().getWebStorageSizeManager()
991 .onExceededDatabaseQuota(url, databaseIdentifier,
992 currentQuota, estimatedSize, totalUsedQuota,
993 quotaUpdater);
994 }
995
996 /**
997 * The Application Cache has exceeded its max size.
998 * @param spaceNeeded is the amount of disk space that would be needed
999 * in order for the last appcache operation to succeed.
1000 * @param totalUsedQuota is the sum of all origins' quota.
1001 * @param quotaUpdater A callback to inform the WebCore thread that a
1002 * new app cache size is available. This callback must always
1003 * be executed at some point to ensure that the sleeping
1004 * WebCore thread is woken up.
1005 */
1006 @Override
1007 public void onReachedMaxAppCacheSize(long spaceNeeded,
1008 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
1009 BrowserSettings.getInstance().getWebStorageSizeManager()
1010 .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota,
1011 quotaUpdater);
1012 }
1013
1014 /**
1015 * Instructs the browser to show a prompt to ask the user to set the
1016 * Geolocation permission state for the specified origin.
1017 * @param origin The origin for which Geolocation permissions are
1018 * requested.
1019 * @param callback The callback to call once the user has set the
1020 * Geolocation permission state.
1021 */
1022 @Override
1023 public void onGeolocationPermissionsShowPrompt(String origin,
1024 GeolocationPermissions.Callback callback) {
1025 if (mInForeground) {
Grace Kloba50c241e2010-04-20 11:07:50 -07001026 getGeolocationPermissionsPrompt().show(origin, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001027 }
1028 }
1029
1030 /**
1031 * Instructs the browser to hide the Geolocation permissions prompt.
1032 */
1033 @Override
1034 public void onGeolocationPermissionsHidePrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001035 if (mInForeground && mGeolocationPermissionsPrompt != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001036 mGeolocationPermissionsPrompt.hide();
1037 }
1038 }
1039
Ben Murdoch65acc352009-11-19 18:16:04 +00001040 /* Adds a JavaScript error message to the system log and if the JS
1041 * console is enabled in the about:debug options, to that console
1042 * also.
Ben Murdochc42addf2010-01-28 15:19:59 +00001043 * @param consoleMessage the message object.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001044 */
1045 @Override
Ben Murdochc42addf2010-01-28 15:19:59 +00001046 public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001047 if (mInForeground) {
1048 // call getErrorConsole(true) so it will create one if needed
1049 ErrorConsoleView errorConsole = getErrorConsole(true);
Ben Murdochc42addf2010-01-28 15:19:59 +00001050 errorConsole.addErrorMessage(consoleMessage);
Michael Kolb8233fac2010-10-26 16:08:53 -07001051 if (mWebViewController.shouldShowErrorConsole()
1052 && errorConsole.getShowState() !=
1053 ErrorConsoleView.SHOW_MAXIMIZED) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001054 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
1055 }
1056 }
Ben Murdochc42addf2010-01-28 15:19:59 +00001057
Jeff Hamilton47654f42010-09-07 09:57:51 -05001058 // Don't log console messages in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001059 if (isPrivateBrowsingEnabled()) return true;
Jeff Hamilton47654f42010-09-07 09:57:51 -05001060
Ben Murdochc42addf2010-01-28 15:19:59 +00001061 String message = "Console: " + consoleMessage.message() + " "
1062 + consoleMessage.sourceId() + ":"
1063 + consoleMessage.lineNumber();
1064
1065 switch (consoleMessage.messageLevel()) {
1066 case TIP:
1067 Log.v(CONSOLE_LOGTAG, message);
1068 break;
1069 case LOG:
1070 Log.i(CONSOLE_LOGTAG, message);
1071 break;
1072 case WARNING:
1073 Log.w(CONSOLE_LOGTAG, message);
1074 break;
1075 case ERROR:
1076 Log.e(CONSOLE_LOGTAG, message);
1077 break;
1078 case DEBUG:
1079 Log.d(CONSOLE_LOGTAG, message);
1080 break;
1081 }
1082
1083 return true;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001084 }
1085
1086 /**
1087 * Ask the browser for an icon to represent a <video> element.
1088 * This icon will be used if the Web page did not specify a poster attribute.
1089 * @return Bitmap The icon or null if no such icon is available.
1090 */
1091 @Override
1092 public Bitmap getDefaultVideoPoster() {
1093 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001094 return mWebViewController.getDefaultVideoPoster();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001095 }
1096 return null;
1097 }
1098
1099 /**
1100 * Ask the host application for a custom progress view to show while
1101 * a <video> is loading.
1102 * @return View The progress view.
1103 */
1104 @Override
1105 public View getVideoLoadingProgressView() {
1106 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001107 return mWebViewController.getVideoLoadingProgressView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001108 }
1109 return null;
1110 }
1111
1112 @Override
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01001113 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001114 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001115 mWebViewController.openFileChooser(uploadMsg, acceptType);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001116 } else {
1117 uploadMsg.onReceiveValue(null);
1118 }
1119 }
1120
1121 /**
1122 * Deliver a list of already-visited URLs
1123 */
1124 @Override
1125 public void getVisitedHistory(final ValueCallback<String[]> callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001126 mWebViewController.getVisitedHistory(callback);
1127 }
Ben Murdoch8029a772010-11-16 11:58:21 +00001128
1129 @Override
1130 public void setupAutoFill(Message message) {
1131 // Prompt the user to set up their profile.
1132 final Message msg = message;
1133 AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
1134 builder.setMessage(R.string.autofill_setup_dialog_message)
1135 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1136 @Override
1137 public void onClick(DialogInterface dialog, int id) {
1138 // Take user to the AutoFill profile editor. When they return,
1139 // we will send the message that we pass here which will trigger
1140 // the form to get filled out with their new profile.
1141 mWebViewController.setupAutoFill(msg);
1142 }
1143 })
1144 .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
1145 @Override
1146 public void onClick(DialogInterface dialog, int id) {
1147 // Disable autofill and show a toast with how to turn it on again.
1148 BrowserSettings s = BrowserSettings.getInstance();
1149 s.addObserver(mMainView.getSettings());
1150 s.disableAutoFill(mActivity);
1151 s.update();
1152 Toast.makeText(mActivity, R.string.autofill_setup_dialog_negative_toast,
1153 Toast.LENGTH_LONG).show();
1154 }
1155 }).show();
1156 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001157 };
1158
1159 // -------------------------------------------------------------------------
1160 // WebViewClient implementation for the sub window
1161 // -------------------------------------------------------------------------
1162
1163 // Subclass of WebViewClient used in subwindows to notify the main
1164 // WebViewClient of certain WebView activities.
1165 private static class SubWindowClient extends WebViewClient {
1166 // The main WebViewClient.
1167 private final WebViewClient mClient;
Michael Kolb8233fac2010-10-26 16:08:53 -07001168 private final WebViewController mController;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001169
Michael Kolb8233fac2010-10-26 16:08:53 -07001170 SubWindowClient(WebViewClient client, WebViewController controller) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001171 mClient = client;
Michael Kolb8233fac2010-10-26 16:08:53 -07001172 mController = controller;
Leon Scroggins III211ba542010-04-19 13:21:13 -04001173 }
1174 @Override
1175 public void onPageStarted(WebView view, String url, Bitmap favicon) {
1176 // Unlike the others, do not call mClient's version, which would
1177 // change the progress bar. However, we do want to remove the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001178 // find or select dialog.
Michael Kolb8233fac2010-10-26 16:08:53 -07001179 mController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001180 }
1181 @Override
1182 public void doUpdateVisitedHistory(WebView view, String url,
1183 boolean isReload) {
1184 mClient.doUpdateVisitedHistory(view, url, isReload);
1185 }
1186 @Override
1187 public boolean shouldOverrideUrlLoading(WebView view, String url) {
1188 return mClient.shouldOverrideUrlLoading(view, url);
1189 }
1190 @Override
1191 public void onReceivedSslError(WebView view, SslErrorHandler handler,
1192 SslError error) {
1193 mClient.onReceivedSslError(view, handler, error);
1194 }
1195 @Override
1196 public void onReceivedHttpAuthRequest(WebView view,
1197 HttpAuthHandler handler, String host, String realm) {
1198 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
1199 }
1200 @Override
1201 public void onFormResubmission(WebView view, Message dontResend,
1202 Message resend) {
1203 mClient.onFormResubmission(view, dontResend, resend);
1204 }
1205 @Override
1206 public void onReceivedError(WebView view, int errorCode,
1207 String description, String failingUrl) {
1208 mClient.onReceivedError(view, errorCode, description, failingUrl);
1209 }
1210 @Override
1211 public boolean shouldOverrideKeyEvent(WebView view,
1212 android.view.KeyEvent event) {
1213 return mClient.shouldOverrideKeyEvent(view, event);
1214 }
1215 @Override
1216 public void onUnhandledKeyEvent(WebView view,
1217 android.view.KeyEvent event) {
1218 mClient.onUnhandledKeyEvent(view, event);
1219 }
1220 }
1221
1222 // -------------------------------------------------------------------------
1223 // WebChromeClient implementation for the sub window
1224 // -------------------------------------------------------------------------
1225
1226 private class SubWindowChromeClient extends WebChromeClient {
1227 // The main WebChromeClient.
1228 private final WebChromeClient mClient;
1229
1230 SubWindowChromeClient(WebChromeClient client) {
1231 mClient = client;
1232 }
1233 @Override
1234 public void onProgressChanged(WebView view, int newProgress) {
1235 mClient.onProgressChanged(view, newProgress);
1236 }
1237 @Override
1238 public boolean onCreateWindow(WebView view, boolean dialog,
1239 boolean userGesture, android.os.Message resultMsg) {
1240 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
1241 }
1242 @Override
1243 public void onCloseWindow(WebView window) {
1244 if (window != mSubView) {
1245 Log.e(LOGTAG, "Can't close the window");
1246 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001247 mWebViewController.dismissSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001248 }
1249 }
1250
1251 // -------------------------------------------------------------------------
1252
Michael Kolb8233fac2010-10-26 16:08:53 -07001253 // TODO temporarily use activity here
1254 // remove later
1255
Grace Kloba22ac16e2009-10-07 18:00:23 -07001256 // Construct a new tab
Michael Kolb8233fac2010-10-26 16:08:53 -07001257 Tab(WebViewController wvcontroller, WebView w, boolean closeOnExit, String appId,
Grace Kloba22ac16e2009-10-07 18:00:23 -07001258 String url) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001259 mWebViewController = wvcontroller;
1260 mActivity = mWebViewController.getActivity();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001261 mCloseOnExit = closeOnExit;
1262 mAppId = appId;
John Reck30c714c2010-12-16 17:30:34 -08001263 mCurrentState = new PageState(mActivity, w.isPrivateBrowsingEnabled());
Michael Kolb8233fac2010-10-26 16:08:53 -07001264 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001265 mInForeground = false;
1266
Grace Kloba22ac16e2009-10-07 18:00:23 -07001267
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001268 mDownloadListener = new DownloadListener() {
1269 public void onDownloadStart(String url, String userAgent,
1270 String contentDisposition, String mimetype,
1271 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001272 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001273 mimetype, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001274 }
1275 };
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001276 mWebBackForwardListClient = new WebBackForwardListClient() {
1277 @Override
1278 public void onNewHistoryItem(WebHistoryItem item) {
1279 if (isInVoiceSearchMode()) {
1280 item.setCustomData(mVoiceSearchData.mVoiceSearchIntent);
1281 }
1282 }
1283 @Override
1284 public void onIndexChanged(WebHistoryItem item, int index) {
1285 Object data = item.getCustomData();
1286 if (data != null && data instanceof Intent) {
1287 activateVoiceSearchMode((Intent) data);
1288 }
1289 }
1290 };
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001291
Grace Kloba22ac16e2009-10-07 18:00:23 -07001292 setWebView(w);
1293 }
1294
1295 /**
1296 * Sets the WebView for this tab, correctly removing the old WebView from
1297 * the container view.
1298 */
1299 void setWebView(WebView w) {
1300 if (mMainView == w) {
1301 return;
1302 }
Michael Kolba713ec82010-11-29 17:27:06 -08001303
Grace Kloba22ac16e2009-10-07 18:00:23 -07001304 // If the WebView is changing, the page will be reloaded, so any ongoing
1305 // Geolocation permission requests are void.
Grace Kloba50c241e2010-04-20 11:07:50 -07001306 if (mGeolocationPermissionsPrompt != null) {
1307 mGeolocationPermissionsPrompt.hide();
1308 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001309
Michael Kolba713ec82010-11-29 17:27:06 -08001310 mWebViewController.onSetWebView(this, w);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001311
1312 // set the new one
1313 mMainView = w;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001314 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001315 if (mMainView != null) {
1316 mMainView.setWebViewClient(mWebViewClient);
1317 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001318 // Attach DownloadManager so that downloads can start in an active
1319 // or a non-active window. This can happen when going to a site that
1320 // does a redirect after a period of time. The user could have
1321 // switched to another tab while waiting for the download to start.
1322 mMainView.setDownloadListener(mDownloadListener);
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001323 mMainView.setWebBackForwardListClient(mWebBackForwardListClient);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001324 }
1325 }
1326
1327 /**
1328 * Destroy the tab's main WebView and subWindow if any
1329 */
1330 void destroy() {
1331 if (mMainView != null) {
1332 dismissSubWindow();
1333 BrowserSettings.getInstance().deleteObserver(mMainView.getSettings());
1334 // save the WebView to call destroy() after detach it from the tab
1335 WebView webView = mMainView;
1336 setWebView(null);
1337 webView.destroy();
1338 }
1339 }
1340
1341 /**
1342 * Remove the tab from the parent
1343 */
1344 void removeFromTree() {
1345 // detach the children
1346 if (mChildTabs != null) {
1347 for(Tab t : mChildTabs) {
1348 t.setParentTab(null);
1349 }
1350 }
1351 // remove itself from the parent list
1352 if (mParentTab != null) {
1353 mParentTab.mChildTabs.remove(this);
1354 }
1355 }
1356
1357 /**
1358 * Create a new subwindow unless a subwindow already exists.
1359 * @return True if a new subwindow was created. False if one already exists.
1360 */
1361 boolean createSubWindow() {
1362 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001363 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001364 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001365 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001366 mSubView.setWebChromeClient(new SubWindowChromeClient(
1367 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001368 // Set a different DownloadListener for the mSubView, since it will
1369 // just need to dismiss the mSubView, rather than close the Tab
1370 mSubView.setDownloadListener(new DownloadListener() {
1371 public void onDownloadStart(String url, String userAgent,
1372 String contentDisposition, String mimetype,
1373 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001374 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001375 contentDisposition, mimetype, contentLength);
1376 if (mSubView.copyBackForwardList().getSize() == 0) {
1377 // This subwindow was opened for the sole purpose of
1378 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001379 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001380 }
1381 }
1382 });
Grace Kloba22ac16e2009-10-07 18:00:23 -07001383 mSubView.setOnCreateContextMenuListener(mActivity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001384 return true;
1385 }
1386 return false;
1387 }
1388
1389 /**
1390 * Dismiss the subWindow for the tab.
1391 */
1392 void dismissSubWindow() {
1393 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001394 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001395 BrowserSettings.getInstance().deleteObserver(
1396 mSubView.getSettings());
1397 mSubView.destroy();
1398 mSubView = null;
1399 mSubViewContainer = null;
1400 }
1401 }
1402
Grace Kloba22ac16e2009-10-07 18:00:23 -07001403
1404 /**
1405 * Set the parent tab of this tab.
1406 */
1407 void setParentTab(Tab parent) {
1408 mParentTab = parent;
1409 // This tab may have been freed due to low memory. If that is the case,
1410 // the parent tab index is already saved. If we are changing that index
1411 // (most likely due to removing the parent tab) we must update the
1412 // parent tab index in the saved Bundle.
1413 if (mSavedState != null) {
1414 if (parent == null) {
1415 mSavedState.remove(PARENTTAB);
1416 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -07001417 mSavedState.putInt(PARENTTAB, mWebViewController.getTabControl()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001418 .getTabIndex(parent));
1419 }
1420 }
1421 }
1422
1423 /**
1424 * When a Tab is created through the content of another Tab, then we
1425 * associate the Tabs.
1426 * @param child the Tab that was created from this Tab
1427 */
1428 void addChildTab(Tab child) {
1429 if (mChildTabs == null) {
1430 mChildTabs = new Vector<Tab>();
1431 }
1432 mChildTabs.add(child);
1433 child.setParentTab(this);
1434 }
1435
1436 Vector<Tab> getChildTabs() {
1437 return mChildTabs;
1438 }
1439
1440 void resume() {
1441 if (mMainView != null) {
1442 mMainView.onResume();
1443 if (mSubView != null) {
1444 mSubView.onResume();
1445 }
1446 }
1447 }
1448
1449 void pause() {
1450 if (mMainView != null) {
1451 mMainView.onPause();
1452 if (mSubView != null) {
1453 mSubView.onPause();
1454 }
1455 }
1456 }
1457
1458 void putInForeground() {
1459 mInForeground = true;
1460 resume();
1461 mMainView.setOnCreateContextMenuListener(mActivity);
1462 if (mSubView != null) {
1463 mSubView.setOnCreateContextMenuListener(mActivity);
1464 }
1465 // Show the pending error dialog if the queue is not empty
1466 if (mQueuedErrors != null && mQueuedErrors.size() > 0) {
1467 showError(mQueuedErrors.getFirst());
1468 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001469 mWebViewController.bookmarkedStatusHasChanged(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001470 }
1471
1472 void putInBackground() {
1473 mInForeground = false;
1474 pause();
1475 mMainView.setOnCreateContextMenuListener(null);
1476 if (mSubView != null) {
1477 mSubView.setOnCreateContextMenuListener(null);
1478 }
1479 }
1480
Michael Kolb8233fac2010-10-26 16:08:53 -07001481 boolean inForeground() {
1482 return mInForeground;
1483 }
1484
Grace Kloba22ac16e2009-10-07 18:00:23 -07001485 /**
1486 * Return the top window of this tab; either the subwindow if it is not
1487 * null or the main window.
1488 * @return The top window of this tab.
1489 */
1490 WebView getTopWindow() {
1491 if (mSubView != null) {
1492 return mSubView;
1493 }
1494 return mMainView;
1495 }
1496
1497 /**
1498 * Return the main window of this tab. Note: if a tab is freed in the
1499 * background, this can return null. It is only guaranteed to be
1500 * non-null for the current tab.
1501 * @return The main WebView of this tab.
1502 */
1503 WebView getWebView() {
1504 return mMainView;
1505 }
1506
Michael Kolba713ec82010-11-29 17:27:06 -08001507 void setViewContainer(View container) {
1508 mContainer = container;
1509 }
1510
Michael Kolb8233fac2010-10-26 16:08:53 -07001511 View getViewContainer() {
1512 return mContainer;
1513 }
1514
Grace Kloba22ac16e2009-10-07 18:00:23 -07001515 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001516 * Return whether private browsing is enabled for the main window of
1517 * this tab.
1518 * @return True if private browsing is enabled.
1519 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001520 boolean isPrivateBrowsingEnabled() {
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001521 WebView webView = getWebView();
1522 if (webView == null) {
1523 return false;
1524 }
1525 return webView.isPrivateBrowsingEnabled();
1526 }
1527
1528 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001529 * Return the subwindow of this tab or null if there is no subwindow.
1530 * @return The subwindow of this tab or null.
1531 */
1532 WebView getSubWebView() {
1533 return mSubView;
1534 }
1535
Michael Kolb1514bb72010-11-22 09:11:48 -08001536 void setSubWebView(WebView subView) {
1537 mSubView = subView;
1538 }
1539
Michael Kolb8233fac2010-10-26 16:08:53 -07001540 View getSubViewContainer() {
1541 return mSubViewContainer;
1542 }
1543
Michael Kolb1514bb72010-11-22 09:11:48 -08001544 void setSubViewContainer(View subViewContainer) {
1545 mSubViewContainer = subViewContainer;
1546 }
1547
Grace Kloba22ac16e2009-10-07 18:00:23 -07001548 /**
1549 * @return The geolocation permissions prompt for this tab.
1550 */
1551 GeolocationPermissionsPrompt getGeolocationPermissionsPrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001552 if (mGeolocationPermissionsPrompt == null) {
1553 ViewStub stub = (ViewStub) mContainer
1554 .findViewById(R.id.geolocation_permissions_prompt);
1555 mGeolocationPermissionsPrompt = (GeolocationPermissionsPrompt) stub
1556 .inflate();
1557 mGeolocationPermissionsPrompt.init();
1558 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001559 return mGeolocationPermissionsPrompt;
1560 }
1561
1562 /**
1563 * @return The application id string
1564 */
1565 String getAppId() {
1566 return mAppId;
1567 }
1568
1569 /**
1570 * Set the application id string
1571 * @param id
1572 */
1573 void setAppId(String id) {
1574 mAppId = id;
1575 }
1576
Grace Kloba22ac16e2009-10-07 18:00:23 -07001577 String getUrl() {
John Reck30c714c2010-12-16 17:30:34 -08001578 return mCurrentState.mUrl;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001579 }
1580
1581 /**
John Reck30c714c2010-12-16 17:30:34 -08001582 * Get the title of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001583 */
1584 String getTitle() {
John Reck30c714c2010-12-16 17:30:34 -08001585 if (mCurrentState.mTitle == null && mInPageLoad) {
1586 return mActivity.getString(R.string.title_bar_loading);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001587 }
John Reck30c714c2010-12-16 17:30:34 -08001588 return mCurrentState.mTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001589 }
1590
1591 /**
John Reck30c714c2010-12-16 17:30:34 -08001592 * Get the favicon of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001593 */
1594 Bitmap getFavicon() {
John Reck30c714c2010-12-16 17:30:34 -08001595 return mCurrentState.mFavicon;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001596 }
1597
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001598
Grace Kloba22ac16e2009-10-07 18:00:23 -07001599 /**
1600 * Return the tab's error console. Creates the console if createIfNEcessary
1601 * is true and we haven't already created the console.
1602 * @param createIfNecessary Flag to indicate if the console should be
1603 * created if it has not been already.
1604 * @return The tab's error console, or null if one has not been created and
1605 * createIfNecessary is false.
1606 */
1607 ErrorConsoleView getErrorConsole(boolean createIfNecessary) {
1608 if (createIfNecessary && mErrorConsole == null) {
1609 mErrorConsole = new ErrorConsoleView(mActivity);
1610 mErrorConsole.setWebView(mMainView);
1611 }
1612 return mErrorConsole;
1613 }
1614
1615 /**
1616 * If this Tab was created through another Tab, then this method returns
1617 * that Tab.
1618 * @return the Tab parent or null
1619 */
1620 public Tab getParentTab() {
1621 return mParentTab;
1622 }
1623
1624 /**
1625 * Return whether this tab should be closed when it is backing out of the
1626 * first page.
1627 * @return TRUE if this tab should be closed when exit.
1628 */
1629 boolean closeOnExit() {
1630 return mCloseOnExit;
1631 }
1632
John Reck30c714c2010-12-16 17:30:34 -08001633 private void setLockIconType(LockIcon icon) {
1634 mCurrentState.mLockIcon = icon;
1635 mWebViewController.onUpdatedLockIcon(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001636 }
1637
1638 /**
1639 * @return The tab's lock icon type.
1640 */
John Reck30c714c2010-12-16 17:30:34 -08001641 LockIcon getLockIconType() {
1642 return mCurrentState.mLockIcon;
1643 }
1644
1645 int getLoadProgress() {
1646 if (mInPageLoad) {
1647 return mPageLoadProgress;
1648 }
1649 return 100;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001650 }
1651
1652 /**
1653 * @return TRUE if onPageStarted is called while onPageFinished is not
1654 * called yet.
1655 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001656 boolean inPageLoad() {
1657 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001658 }
1659
1660 // force mInLoad to be false. This should only be called before closing the
1661 // tab to ensure BrowserActivity's pauseWebViewTimers() is called correctly.
Michael Kolb8233fac2010-10-26 16:08:53 -07001662 void clearInPageLoad() {
1663 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001664 }
1665
Grace Kloba22ac16e2009-10-07 18:00:23 -07001666 /**
John Reck30c714c2010-12-16 17:30:34 -08001667 * Get the cached saved state bundle.
1668 * @return cached state bundle
Grace Kloba22ac16e2009-10-07 18:00:23 -07001669 */
1670 Bundle getSavedState() {
1671 return mSavedState;
1672 }
1673
1674 /**
1675 * Set the saved state.
1676 */
1677 void setSavedState(Bundle state) {
1678 mSavedState = state;
1679 }
1680
1681 /**
1682 * @return TRUE if succeed in saving the state.
1683 */
1684 boolean saveState() {
1685 // If the WebView is null it means we ran low on memory and we already
1686 // stored the saved state in mSavedState.
1687 if (mMainView == null) {
1688 return mSavedState != null;
1689 }
1690
1691 mSavedState = new Bundle();
1692 final WebBackForwardList list = mMainView.saveState(mSavedState);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001693
1694 // Store some extra info for displaying the tab in the picker.
1695 final WebHistoryItem item = list != null ? list.getCurrentItem() : null;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001696
John Reck30c714c2010-12-16 17:30:34 -08001697 mSavedState.putString(CURRURL, mCurrentState.mUrl);
1698 mSavedState.putString(CURRTITLE, mCurrentState.mTitle);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001699 mSavedState.putBoolean(CLOSEONEXIT, mCloseOnExit);
1700 if (mAppId != null) {
1701 mSavedState.putString(APPID, mAppId);
1702 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001703 // Remember the parent tab so the relationship can be restored.
1704 if (mParentTab != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001705 mSavedState.putInt(PARENTTAB, mWebViewController.getTabControl().getTabIndex(
Grace Kloba22ac16e2009-10-07 18:00:23 -07001706 mParentTab));
1707 }
1708 return true;
1709 }
1710
1711 /*
1712 * Restore the state of the tab.
1713 */
1714 boolean restoreState(Bundle b) {
1715 if (b == null) {
1716 return false;
1717 }
1718 // Restore the internal state even if the WebView fails to restore.
1719 // This will maintain the app id, original url and close-on-exit values.
1720 mSavedState = null;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001721 mCloseOnExit = b.getBoolean(CLOSEONEXIT);
1722 mAppId = b.getString(APPID);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001723
1724 final WebBackForwardList list = mMainView.restoreState(b);
1725 if (list == null) {
1726 return false;
1727 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001728 return true;
1729 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04001730
Leon Scroggins1961ed22010-12-07 15:22:21 -05001731 public void updateBookmarkedStatus() {
1732 if (mMainView == null) {
1733 return;
1734 }
1735 String url = mMainView.getUrl();
1736 if (url == null) {
1737 return;
1738 }
1739 updateBookmarkedStatusForUrl(url);
1740 }
1741
1742 /**
1743 * Update mIsBookmarkedSite, using urlInQuestion to compare.
1744 * @param urlInQuestion URL of the current page, to be checked in the
1745 * bookmarks database.
1746 */
1747 private void updateBookmarkedStatusForUrl(final String urlInQuestion) {
1748 if (mBookmarkAsyncTask != null) {
1749 mBookmarkAsyncTask.cancel(true);
1750 }
1751 mBookmarkAsyncTask = new AsyncTask<Void, Void, Boolean>() {
1752 @Override
1753 protected Boolean doInBackground(Void... unused) {
1754 // Check to see if the site is bookmarked
1755 Cursor cursor = null;
1756 try {
1757 cursor = mActivity.getContentResolver().query(
1758 BrowserContract.Bookmarks.CONTENT_URI,
1759 new String[] { BrowserContract.Bookmarks.URL },
1760 BrowserContract.Bookmarks.URL + " == ?",
1761 new String[] { urlInQuestion },
1762 null);
1763 return cursor.moveToFirst();
1764 } catch (SQLiteException e) {
1765 Log.e(LOGTAG, "Error checking for bookmark: " + e);
1766 return false;
1767 } finally {
1768 if (cursor != null) cursor.close();
1769 }
1770 }
1771 @Override
1772 protected void onPostExecute(Boolean isBookmarked) {
1773 if (this == mBookmarkAsyncTask) {
1774 mIsBookmarkedSite = isBookmarked;
1775 mWebViewController.bookmarkedStatusHasChanged(Tab.this);
1776 }
1777 }
1778 };
1779 mBookmarkAsyncTask.execute();
1780 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001781}