blob: d70b0ef7a57fdc614fd5b9f4c7e684d38b49d217 [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;
Grace Kloba22ac16e2009-10-07 18:00:23 -070029import android.graphics.Bitmap;
John Reck30c714c2010-12-16 17:30:34 -080030import android.graphics.BitmapFactory;
Grace Kloba22ac16e2009-10-07 18:00:23 -070031import android.net.Uri;
32import android.net.http.SslError;
Grace Kloba22ac16e2009-10-07 18:00:23 -070033import android.os.Bundle;
34import android.os.Message;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +000035import android.os.SystemClock;
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -050036import android.speech.RecognizerResultsIntent;
Grace Kloba22ac16e2009-10-07 18:00:23 -070037import android.util.Log;
38import android.view.KeyEvent;
39import android.view.LayoutInflater;
40import android.view.View;
Grace Kloba50c241e2010-04-20 11:07:50 -070041import android.view.ViewStub;
Ben Murdochc42addf2010-01-28 15:19:59 +000042import android.webkit.ConsoleMessage;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -050043import android.webkit.DownloadListener;
Grace Kloba22ac16e2009-10-07 18:00:23 -070044import android.webkit.GeolocationPermissions;
45import android.webkit.HttpAuthHandler;
46import android.webkit.SslErrorHandler;
47import android.webkit.URLUtil;
48import android.webkit.ValueCallback;
49import android.webkit.WebBackForwardList;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -050050import android.webkit.WebBackForwardListClient;
Grace Kloba22ac16e2009-10-07 18:00:23 -070051import android.webkit.WebChromeClient;
52import android.webkit.WebHistoryItem;
Grace Kloba22ac16e2009-10-07 18:00:23 -070053import android.webkit.WebStorage;
54import android.webkit.WebView;
55import android.webkit.WebViewClient;
Grace Kloba22ac16e2009-10-07 18:00:23 -070056import android.widget.LinearLayout;
57import android.widget.TextView;
Ben Murdoch8029a772010-11-16 11:58:21 +000058import android.widget.Toast;
Grace Kloba22ac16e2009-10-07 18:00:23 -070059
Michael Kolbfe251992010-07-08 15:41:55 -070060import java.util.ArrayList;
61import java.util.HashMap;
62import java.util.Iterator;
63import java.util.LinkedList;
64import java.util.Map;
65import java.util.Vector;
66
Grace Kloba22ac16e2009-10-07 18:00:23 -070067/**
68 * Class for maintaining Tabs with a main WebView and a subwindow.
69 */
70class Tab {
Michael Kolb8233fac2010-10-26 16:08:53 -070071
Grace Kloba22ac16e2009-10-07 18:00:23 -070072 // Log Tag
73 private static final String LOGTAG = "Tab";
Ben Murdochc42addf2010-01-28 15:19:59 +000074 // Special case the logtag for messages for the Console to make it easier to
75 // filter them and match the logtag used for these messages in older versions
76 // of the browser.
77 private static final String CONSOLE_LOGTAG = "browser";
78
John Reck30c714c2010-12-16 17:30:34 -080079 public enum LockIcon {
80 LOCK_ICON_UNSECURE,
81 LOCK_ICON_SECURE,
82 LOCK_ICON_MIXED,
83 }
Michael Kolb8233fac2010-10-26 16:08:53 -070084
85 Activity mActivity;
86 private WebViewController mWebViewController;
87
Grace Kloba22ac16e2009-10-07 18:00:23 -070088 // The Geolocation permissions prompt
89 private GeolocationPermissionsPrompt mGeolocationPermissionsPrompt;
90 // Main WebView wrapper
Michael Kolba713ec82010-11-29 17:27:06 -080091 private View mContainer;
Grace Kloba22ac16e2009-10-07 18:00:23 -070092 // Main WebView
93 private WebView mMainView;
94 // Subwindow container
95 private View mSubViewContainer;
96 // Subwindow WebView
97 private WebView mSubView;
98 // Saved bundle for when we are running low on memory. It contains the
99 // information needed to restore the WebView if the user goes back to the
100 // tab.
101 private Bundle mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700102 // Parent Tab. This is the Tab that created this Tab, or null if the Tab was
103 // created by the UI
104 private Tab mParentTab;
105 // Tab that constructed by this Tab. This is used when this Tab is
106 // destroyed, it clears all mParentTab values in the children.
107 private Vector<Tab> mChildTabs;
108 // If true, the tab will be removed when back out of the first page.
109 private boolean mCloseOnExit;
110 // If true, the tab is in the foreground of the current activity.
111 private boolean mInForeground;
Michael Kolb8233fac2010-10-26 16:08:53 -0700112 // If true, the tab is in page loading state (after onPageStarted,
113 // before onPageFinsihed)
114 private boolean mInPageLoad;
John Reck30c714c2010-12-16 17:30:34 -0800115 // The last reported progress of the current page
116 private int mPageLoadProgress;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000117 // The time the load started, used to find load page time
118 private long mLoadStartTime;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700119 // Application identifier used to find tabs that another application wants
120 // to reuse.
121 private String mAppId;
122 // Keep the original url around to avoid killing the old WebView if the url
123 // has not changed.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700124 // Error console for the tab
125 private ErrorConsoleView mErrorConsole;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -0500126 // The listener that gets invoked when a download is started from the
127 // mMainView
128 private final DownloadListener mDownloadListener;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500129 // Listener used to know when we move forward or back in the history list.
130 private final WebBackForwardListClient mWebBackForwardListClient;
John Recke969cc52010-12-21 17:24:43 -0800131 private DataController mDataController;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700132
133 // AsyncTask for downloading touch icons
134 DownloadTouchIcon mTouchIconLoader;
135
John Reck30c714c2010-12-16 17:30:34 -0800136 // All the state needed for a page
137 private static class PageState {
138 String mUrl;
139 String mTitle;
140 LockIcon mLockIcon;
141 Bitmap mFavicon;
John Recke969cc52010-12-21 17:24:43 -0800142 Boolean mIsBookmarkedSite = false;
John Reck30c714c2010-12-16 17:30:34 -0800143
144 PageState(Context c, boolean incognito) {
145 if (incognito) {
146 mUrl = "browser:incognito";
147 mTitle = c.getString(R.string.new_incognito_tab);
148 mFavicon = BitmapFactory.decodeResource(
149 c.getResources(), R.drawable.fav_incognito);
150 } else {
151 mUrl = "";
152 mTitle = c.getString(R.string.new_tab);
153 mFavicon = BitmapFactory.decodeResource(
154 c.getResources(), R.drawable.app_web_browser_sm);
155 }
156 mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
157 }
158
159 PageState(Context c, boolean incognito, String url, Bitmap favicon) {
160 mUrl = url;
161 mTitle = null;
162 if (URLUtil.isHttpsUrl(url)) {
163 mLockIcon = LockIcon.LOCK_ICON_SECURE;
164 } else {
165 mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
166 }
167 if (favicon != null) {
168 mFavicon = favicon;
169 } else {
170 if (incognito) {
171 mFavicon = BitmapFactory.decodeResource(
172 c.getResources(), R.drawable.fav_incognito);
173 } else {
174 mFavicon = BitmapFactory.decodeResource(
175 c.getResources(), R.drawable.app_web_browser_sm);
176 }
177 }
178 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700179 }
180
John Reck30c714c2010-12-16 17:30:34 -0800181 // The current/loading page's state
182 private PageState mCurrentState;
183
Grace Kloba22ac16e2009-10-07 18:00:23 -0700184 // Used for saving and restoring each Tab
John Reck30c714c2010-12-16 17:30:34 -0800185 // TODO: Figure out who uses what and where
186 // Some of these aren't use in this class, and some are only used in
187 // restoring state but not saving it - FIX THIS
Grace Kloba22ac16e2009-10-07 18:00:23 -0700188 static final String WEBVIEW = "webview";
189 static final String NUMTABS = "numTabs";
190 static final String CURRTAB = "currentTab";
191 static final String CURRURL = "currentUrl";
192 static final String CURRTITLE = "currentTitle";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700193 static final String CLOSEONEXIT = "closeonexit";
194 static final String PARENTTAB = "parentTab";
195 static final String APPID = "appid";
196 static final String ORIGINALURL = "originalUrl";
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700197 static final String INCOGNITO = "privateBrowsingEnabled";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700198
199 // -------------------------------------------------------------------------
200
Leon Scroggins58d56c62010-01-28 15:12:40 -0500201 /**
202 * Private information regarding the latest voice search. If the Tab is not
203 * in voice search mode, this will be null.
204 */
205 private VoiceSearchData mVoiceSearchData;
206 /**
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400207 * Remove voice search mode from this tab.
208 */
209 public void revertVoiceSearchMode() {
210 if (mVoiceSearchData != null) {
211 mVoiceSearchData = null;
212 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700213 mWebViewController.revertVoiceSearchMode(this);
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400214 }
215 }
216 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700217
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400218 /**
Leon Scroggins58d56c62010-01-28 15:12:40 -0500219 * Return whether the tab is in voice search mode.
220 */
221 public boolean isInVoiceSearchMode() {
222 return mVoiceSearchData != null;
223 }
224 /**
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400225 * Return true if the Tab is in voice search mode and the voice search
226 * Intent came with a String identifying that Google provided the Intent.
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500227 */
228 public boolean voiceSearchSourceIsGoogle() {
229 return mVoiceSearchData != null && mVoiceSearchData.mSourceIsGoogle;
230 }
231 /**
Leon Scroggins58d56c62010-01-28 15:12:40 -0500232 * Get the title to display for the current voice search page. If the Tab
233 * is not in voice search mode, return null.
234 */
235 public String getVoiceDisplayTitle() {
236 if (mVoiceSearchData == null) return null;
237 return mVoiceSearchData.mLastVoiceSearchTitle;
238 }
239 /**
240 * Get the latest array of voice search results, to be passed to the
241 * BrowserProvider. If the Tab is not in voice search mode, return null.
242 */
243 public ArrayList<String> getVoiceSearchResults() {
244 if (mVoiceSearchData == null) return null;
245 return mVoiceSearchData.mVoiceSearchResults;
246 }
247 /**
248 * Activate voice search mode.
249 * @param intent Intent which has the results to use, or an index into the
250 * results when reusing the old results.
251 */
252 /* package */ void activateVoiceSearchMode(Intent intent) {
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500253 int index = 0;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500254 ArrayList<String> results = intent.getStringArrayListExtra(
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -0500255 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_STRINGS);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500256 if (results != null) {
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500257 ArrayList<String> urls = intent.getStringArrayListExtra(
258 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_URLS);
259 ArrayList<String> htmls = intent.getStringArrayListExtra(
260 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_HTML);
261 ArrayList<String> baseUrls = intent.getStringArrayListExtra(
262 RecognizerResultsIntent
263 .EXTRA_VOICE_SEARCH_RESULT_HTML_BASE_URLS);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500264 // This tab is now entering voice search mode for the first time, or
265 // a new voice search was done.
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500266 int size = results.size();
267 if (urls == null || size != urls.size()) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500268 throw new AssertionError("improper extras passed in Intent");
269 }
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500270 if (htmls == null || htmls.size() != size || baseUrls == null ||
271 (baseUrls.size() != size && baseUrls.size() != 1)) {
272 // If either of these arrays are empty/incorrectly sized, ignore
273 // them.
274 htmls = null;
275 baseUrls = null;
276 }
277 mVoiceSearchData = new VoiceSearchData(results, urls, htmls,
278 baseUrls);
Leon Scroggins9df94972010-03-08 18:20:35 -0500279 mVoiceSearchData.mHeaders = intent.getParcelableArrayListExtra(
280 RecognizerResultsIntent
281 .EXTRA_VOICE_SEARCH_RESULT_HTTP_HEADERS);
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500282 mVoiceSearchData.mSourceIsGoogle = intent.getBooleanExtra(
283 VoiceSearchData.SOURCE_IS_GOOGLE, false);
Leon Scroggins2ee4a5a2010-03-15 16:56:57 -0400284 mVoiceSearchData.mVoiceSearchIntent = new Intent(intent);
Leon Scrogginse10dde52010-03-08 19:53:03 -0500285 }
286 String extraData = intent.getStringExtra(
287 SearchManager.EXTRA_DATA_KEY);
288 if (extraData != null) {
289 index = Integer.parseInt(extraData);
290 if (index >= mVoiceSearchData.mVoiceSearchResults.size()) {
291 throw new AssertionError("index must be less than "
292 + "size of mVoiceSearchResults");
293 }
294 if (mVoiceSearchData.mSourceIsGoogle) {
295 Intent logIntent = new Intent(
296 LoggingEvents.ACTION_LOG_EVENT);
297 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
298 LoggingEvents.VoiceSearch.N_BEST_CHOOSE);
299 logIntent.putExtra(
300 LoggingEvents.VoiceSearch.EXTRA_N_BEST_CHOOSE_INDEX,
301 index);
302 mActivity.sendBroadcast(logIntent);
303 }
304 if (mVoiceSearchData.mVoiceSearchIntent != null) {
Leon Scroggins2ee4a5a2010-03-15 16:56:57 -0400305 // Copy the Intent, so that each history item will have its own
306 // Intent, with different (or none) extra data.
307 Intent latest = new Intent(mVoiceSearchData.mVoiceSearchIntent);
308 latest.putExtra(SearchManager.EXTRA_DATA_KEY, extraData);
309 mVoiceSearchData.mVoiceSearchIntent = latest;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500310 }
311 }
312 mVoiceSearchData.mLastVoiceSearchTitle
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500313 = mVoiceSearchData.mVoiceSearchResults.get(index);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500314 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700315 mWebViewController.activateVoiceSearchMode(mVoiceSearchData.mLastVoiceSearchTitle);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500316 }
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500317 if (mVoiceSearchData.mVoiceSearchHtmls != null) {
318 // When index was found it was already ensured that it was valid
319 String uriString = mVoiceSearchData.mVoiceSearchHtmls.get(index);
320 if (uriString != null) {
321 Uri dataUri = Uri.parse(uriString);
322 if (RecognizerResultsIntent.URI_SCHEME_INLINE.equals(
323 dataUri.getScheme())) {
324 // If there is only one base URL, use it. If there are
325 // more, there will be one for each index, so use the base
326 // URL corresponding to the index.
327 String baseUrl = mVoiceSearchData.mVoiceSearchBaseUrls.get(
328 mVoiceSearchData.mVoiceSearchBaseUrls.size() > 1 ?
329 index : 0);
330 mVoiceSearchData.mLastVoiceSearchUrl = baseUrl;
331 mMainView.loadDataWithBaseURL(baseUrl,
332 uriString.substring(RecognizerResultsIntent
333 .URI_SCHEME_INLINE.length() + 1), "text/html",
334 "utf-8", baseUrl);
335 return;
336 }
337 }
338 }
Leon Scroggins58d56c62010-01-28 15:12:40 -0500339 mVoiceSearchData.mLastVoiceSearchUrl
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500340 = mVoiceSearchData.mVoiceSearchUrls.get(index);
341 if (null == mVoiceSearchData.mLastVoiceSearchUrl) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700342 mVoiceSearchData.mLastVoiceSearchUrl = UrlUtils.smartUrlFilter(
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500343 mVoiceSearchData.mLastVoiceSearchTitle);
344 }
Leon Scroggins9df94972010-03-08 18:20:35 -0500345 Map<String, String> headers = null;
346 if (mVoiceSearchData.mHeaders != null) {
347 int bundleIndex = mVoiceSearchData.mHeaders.size() == 1 ? 0
348 : index;
349 Bundle bundle = mVoiceSearchData.mHeaders.get(bundleIndex);
350 if (bundle != null && !bundle.isEmpty()) {
351 Iterator<String> iter = bundle.keySet().iterator();
352 headers = new HashMap<String, String>();
353 while (iter.hasNext()) {
354 String key = iter.next();
355 headers.put(key, bundle.getString(key));
356 }
357 }
358 }
359 mMainView.loadUrl(mVoiceSearchData.mLastVoiceSearchUrl, headers);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500360 }
361 /* package */ static class VoiceSearchData {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500362 public VoiceSearchData(ArrayList<String> results,
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500363 ArrayList<String> urls, ArrayList<String> htmls,
364 ArrayList<String> baseUrls) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500365 mVoiceSearchResults = results;
366 mVoiceSearchUrls = urls;
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500367 mVoiceSearchHtmls = htmls;
368 mVoiceSearchBaseUrls = baseUrls;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500369 }
370 /*
371 * ArrayList of suggestions to be displayed when opening the
372 * SearchManager
373 */
374 public ArrayList<String> mVoiceSearchResults;
375 /*
376 * ArrayList of urls, associated with the suggestions in
377 * mVoiceSearchResults.
378 */
379 public ArrayList<String> mVoiceSearchUrls;
380 /*
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500381 * ArrayList holding content to load for each item in
382 * mVoiceSearchResults.
383 */
384 public ArrayList<String> mVoiceSearchHtmls;
385 /*
386 * ArrayList holding base urls for the items in mVoiceSearchResults.
387 * If non null, this will either have the same size as
388 * mVoiceSearchResults or have a size of 1, in which case all will use
389 * the same base url
390 */
391 public ArrayList<String> mVoiceSearchBaseUrls;
392 /*
Leon Scroggins58d56c62010-01-28 15:12:40 -0500393 * The last url provided by voice search. Used for comparison to see if
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500394 * we are going to a page by some method besides voice search.
Leon Scroggins58d56c62010-01-28 15:12:40 -0500395 */
396 public String mLastVoiceSearchUrl;
397 /**
398 * The last title used for voice search. Needed to update the title bar
399 * when switching tabs.
400 */
401 public String mLastVoiceSearchTitle;
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500402 /**
403 * Whether the Intent which turned on voice search mode contained the
404 * String signifying that Google was the source.
405 */
406 public boolean mSourceIsGoogle;
407 /**
Leon Scroggins9df94972010-03-08 18:20:35 -0500408 * List of headers to be passed into the WebView containing location
409 * information
410 */
411 public ArrayList<Bundle> mHeaders;
412 /**
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500413 * The Intent used to invoke voice search. Placed on the
414 * WebHistoryItem so that when coming back to a previous voice search
415 * page we can again activate voice search.
416 */
Leon Scrogginse10dde52010-03-08 19:53:03 -0500417 public Intent mVoiceSearchIntent;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500418 /**
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500419 * String used to identify Google as the source of voice search.
420 */
421 public static String SOURCE_IS_GOOGLE
422 = "android.speech.extras.SOURCE_IS_GOOGLE";
Leon Scroggins58d56c62010-01-28 15:12:40 -0500423 }
424
Grace Kloba22ac16e2009-10-07 18:00:23 -0700425 // Container class for the next error dialog that needs to be displayed
426 private class ErrorDialog {
427 public final int mTitle;
428 public final String mDescription;
429 public final int mError;
430 ErrorDialog(int title, String desc, int error) {
431 mTitle = title;
432 mDescription = desc;
433 mError = error;
434 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700435 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700436
437 private void processNextError() {
438 if (mQueuedErrors == null) {
439 return;
440 }
441 // The first one is currently displayed so just remove it.
442 mQueuedErrors.removeFirst();
443 if (mQueuedErrors.size() == 0) {
444 mQueuedErrors = null;
445 return;
446 }
447 showError(mQueuedErrors.getFirst());
448 }
449
450 private DialogInterface.OnDismissListener mDialogListener =
451 new DialogInterface.OnDismissListener() {
452 public void onDismiss(DialogInterface d) {
453 processNextError();
454 }
455 };
456 private LinkedList<ErrorDialog> mQueuedErrors;
457
458 private void queueError(int err, String desc) {
459 if (mQueuedErrors == null) {
460 mQueuedErrors = new LinkedList<ErrorDialog>();
461 }
462 for (ErrorDialog d : mQueuedErrors) {
463 if (d.mError == err) {
464 // Already saw a similar error, ignore the new one.
465 return;
466 }
467 }
468 ErrorDialog errDialog = new ErrorDialog(
469 err == WebViewClient.ERROR_FILE_NOT_FOUND ?
470 R.string.browserFrameFileErrorLabel :
471 R.string.browserFrameNetworkErrorLabel,
472 desc, err);
473 mQueuedErrors.addLast(errDialog);
474
475 // Show the dialog now if the queue was empty and it is in foreground
476 if (mQueuedErrors.size() == 1 && mInForeground) {
477 showError(errDialog);
478 }
479 }
480
481 private void showError(ErrorDialog errDialog) {
482 if (mInForeground) {
483 AlertDialog d = new AlertDialog.Builder(mActivity)
484 .setTitle(errDialog.mTitle)
485 .setMessage(errDialog.mDescription)
486 .setPositiveButton(R.string.ok, null)
487 .create();
488 d.setOnDismissListener(mDialogListener);
489 d.show();
490 }
491 }
492
493 // -------------------------------------------------------------------------
494 // WebViewClient implementation for the main WebView
495 // -------------------------------------------------------------------------
496
497 private final WebViewClient mWebViewClient = new WebViewClient() {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500498 private Message mDontResend;
499 private Message mResend;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700500 @Override
501 public void onPageStarted(WebView view, String url, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700502 mInPageLoad = true;
John Reck30c714c2010-12-16 17:30:34 -0800503 mPageLoadProgress = 0;
504 mCurrentState = new PageState(mActivity,
505 view.isPrivateBrowsingEnabled(), url, favicon);
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000506 mLoadStartTime = SystemClock.uptimeMillis();
Leon Scroggins58d56c62010-01-28 15:12:40 -0500507 if (mVoiceSearchData != null
508 && !url.equals(mVoiceSearchData.mLastVoiceSearchUrl)) {
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500509 if (mVoiceSearchData.mSourceIsGoogle) {
510 Intent i = new Intent(LoggingEvents.ACTION_LOG_EVENT);
511 i.putExtra(LoggingEvents.EXTRA_FLUSH, true);
512 mActivity.sendBroadcast(i);
513 }
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400514 revertVoiceSearchMode();
Leon Scroggins58d56c62010-01-28 15:12:40 -0500515 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700516
Grace Kloba22ac16e2009-10-07 18:00:23 -0700517
518 // If we start a touch icon load and then load a new page, we don't
519 // want to cancel the current touch icon loader. But, we do want to
520 // create a new one when the touch icon url is known.
521 if (mTouchIconLoader != null) {
522 mTouchIconLoader.mTab = null;
523 mTouchIconLoader = null;
524 }
525
526 // reset the error console
527 if (mErrorConsole != null) {
528 mErrorConsole.clearErrorMessages();
Michael Kolb8233fac2010-10-26 16:08:53 -0700529 if (mWebViewController.shouldShowErrorConsole()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700530 mErrorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
531 }
532 }
533
Grace Kloba22ac16e2009-10-07 18:00:23 -0700534 // finally update the UI in the activity if it is in the foreground
John Reck324d4402011-01-11 16:56:42 -0800535 mWebViewController.onPageStarted(Tab.this, view, favicon);
Leon Scroggins4cd97792010-12-03 15:31:56 -0500536
John Recke969cc52010-12-21 17:24:43 -0800537 updateBookmarkedStatus();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700538 }
539
540 @Override
541 public void onPageFinished(WebView view, String url) {
John Reck5b691842010-11-29 11:21:13 -0800542 if (!isPrivateBrowsingEnabled()) {
543 LogTag.logPageFinishedLoading(
544 url, SystemClock.uptimeMillis() - mLoadStartTime);
545 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700546 mInPageLoad = false;
John Reck30c714c2010-12-16 17:30:34 -0800547 // Sync state (in case of stop/timeout)
548 mCurrentState.mUrl = view.getUrl();
John Reck6c702ee2011-01-07 09:41:53 -0800549 if (mCurrentState.mUrl == null) {
550 mCurrentState.mUrl = url != null ? url : "";
551 }
John Reck30c714c2010-12-16 17:30:34 -0800552 mCurrentState.mTitle = view.getTitle();
553 mCurrentState.mFavicon = view.getFavicon();
554 if (!URLUtil.isHttpsUrl(mCurrentState.mUrl)) {
555 // In case we stop when loading an HTTPS page from an HTTP page
556 // but before a provisional load occurred
557 mCurrentState.mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
558 }
John Reck324d4402011-01-11 16:56:42 -0800559 mWebViewController.onPageFinished(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700560 }
561
562 // return true if want to hijack the url to let another app to handle it
563 @Override
564 public boolean shouldOverrideUrlLoading(WebView view, String url) {
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400565 if (voiceSearchSourceIsGoogle()) {
566 // This method is called when the user clicks on a link.
567 // VoiceSearchMode is turned off when the user leaves the
568 // Google results page, so at this point the user must be on
569 // that page. If the user clicked a link on that page, assume
570 // that the voice search was effective, and broadcast an Intent
571 // so a receiver can take note of that fact.
572 Intent logIntent = new Intent(LoggingEvents.ACTION_LOG_EVENT);
573 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
574 LoggingEvents.VoiceSearch.RESULT_CLICKED);
575 mActivity.sendBroadcast(logIntent);
576 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700577 if (mInForeground) {
Michael Kolb18eb3772010-12-10 14:29:51 -0800578 return mWebViewController.shouldOverrideUrlLoading(Tab.this,
579 view, url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700580 } else {
581 return false;
582 }
583 }
584
585 /**
586 * Updates the lock icon. This method is called when we discover another
587 * resource to be loaded for this page (for example, javascript). While
588 * we update the icon type, we do not update the lock icon itself until
589 * we are done loading, it is slightly more secure this way.
590 */
591 @Override
592 public void onLoadResource(WebView view, String url) {
593 if (url != null && url.length() > 0) {
594 // It is only if the page claims to be secure that we may have
595 // to update the lock:
John Reck30c714c2010-12-16 17:30:34 -0800596 if (mCurrentState.mLockIcon == LockIcon.LOCK_ICON_SECURE) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700597 // If NOT a 'safe' url, change the lock to mixed content!
598 if (!(URLUtil.isHttpsUrl(url) || URLUtil.isDataUrl(url)
599 || URLUtil.isAboutUrl(url))) {
John Reck30c714c2010-12-16 17:30:34 -0800600 mCurrentState.mLockIcon = LockIcon.LOCK_ICON_MIXED;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700601 }
602 }
603 }
604 }
605
606 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700607 * Show a dialog informing the user of the network error reported by
608 * WebCore if it is in the foreground.
609 */
610 @Override
611 public void onReceivedError(WebView view, int errorCode,
612 String description, String failingUrl) {
613 if (errorCode != WebViewClient.ERROR_HOST_LOOKUP &&
614 errorCode != WebViewClient.ERROR_CONNECT &&
615 errorCode != WebViewClient.ERROR_BAD_URL &&
616 errorCode != WebViewClient.ERROR_UNSUPPORTED_SCHEME &&
617 errorCode != WebViewClient.ERROR_FILE) {
618 queueError(errorCode, description);
619 }
Jeff Hamilton47654f42010-09-07 09:57:51 -0500620
621 // Don't log URLs when in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -0700622 if (!isPrivateBrowsingEnabled()) {
Jeff Hamilton47654f42010-09-07 09:57:51 -0500623 Log.e(LOGTAG, "onReceivedError " + errorCode + " " + failingUrl
624 + " " + description);
625 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700626 }
627
628 /**
629 * Check with the user if it is ok to resend POST data as the page they
630 * are trying to navigate to is the result of a POST.
631 */
632 @Override
633 public void onFormResubmission(WebView view, final Message dontResend,
634 final Message resend) {
635 if (!mInForeground) {
636 dontResend.sendToTarget();
637 return;
638 }
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500639 if (mDontResend != null) {
640 Log.w(LOGTAG, "onFormResubmission should not be called again "
641 + "while dialog is still up");
642 dontResend.sendToTarget();
643 return;
644 }
645 mDontResend = dontResend;
646 mResend = resend;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700647 new AlertDialog.Builder(mActivity).setTitle(
648 R.string.browserFrameFormResubmitLabel).setMessage(
649 R.string.browserFrameFormResubmitMessage)
650 .setPositiveButton(R.string.ok,
651 new DialogInterface.OnClickListener() {
652 public void onClick(DialogInterface dialog,
653 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500654 if (mResend != null) {
655 mResend.sendToTarget();
656 mResend = null;
657 mDontResend = null;
658 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700659 }
660 }).setNegativeButton(R.string.cancel,
661 new DialogInterface.OnClickListener() {
662 public void onClick(DialogInterface dialog,
663 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500664 if (mDontResend != null) {
665 mDontResend.sendToTarget();
666 mResend = null;
667 mDontResend = null;
668 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700669 }
670 }).setOnCancelListener(new OnCancelListener() {
671 public void onCancel(DialogInterface dialog) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500672 if (mDontResend != null) {
673 mDontResend.sendToTarget();
674 mResend = null;
675 mDontResend = null;
676 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700677 }
678 }).show();
679 }
680
681 /**
682 * Insert the url into the visited history database.
683 * @param url The url to be inserted.
684 * @param isReload True if this url is being reloaded.
685 * FIXME: Not sure what to do when reloading the page.
686 */
687 @Override
688 public void doUpdateVisitedHistory(WebView view, String url,
689 boolean isReload) {
John Reck324d4402011-01-11 16:56:42 -0800690 mWebViewController.doUpdateVisitedHistory(Tab.this, isReload);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700691 }
692
693 /**
694 * Displays SSL error(s) dialog to the user.
695 */
696 @Override
697 public void onReceivedSslError(final WebView view,
698 final SslErrorHandler handler, final SslError error) {
699 if (!mInForeground) {
700 handler.cancel();
John Reck30c714c2010-12-16 17:30:34 -0800701 setLockIconType(LockIcon.LOCK_ICON_UNSECURE);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700702 return;
703 }
704 if (BrowserSettings.getInstance().showSecurityWarnings()) {
705 final LayoutInflater factory =
706 LayoutInflater.from(mActivity);
707 final View warningsView =
708 factory.inflate(R.layout.ssl_warnings, null);
709 final LinearLayout placeholder =
710 (LinearLayout)warningsView.findViewById(R.id.placeholder);
711
712 if (error.hasError(SslError.SSL_UNTRUSTED)) {
713 LinearLayout ll = (LinearLayout)factory
714 .inflate(R.layout.ssl_warning, null);
715 ((TextView)ll.findViewById(R.id.warning))
716 .setText(R.string.ssl_untrusted);
717 placeholder.addView(ll);
718 }
719
720 if (error.hasError(SslError.SSL_IDMISMATCH)) {
721 LinearLayout ll = (LinearLayout)factory
722 .inflate(R.layout.ssl_warning, null);
723 ((TextView)ll.findViewById(R.id.warning))
724 .setText(R.string.ssl_mismatch);
725 placeholder.addView(ll);
726 }
727
728 if (error.hasError(SslError.SSL_EXPIRED)) {
729 LinearLayout ll = (LinearLayout)factory
730 .inflate(R.layout.ssl_warning, null);
731 ((TextView)ll.findViewById(R.id.warning))
732 .setText(R.string.ssl_expired);
733 placeholder.addView(ll);
734 }
735
736 if (error.hasError(SslError.SSL_NOTYETVALID)) {
737 LinearLayout ll = (LinearLayout)factory
738 .inflate(R.layout.ssl_warning, null);
739 ((TextView)ll.findViewById(R.id.warning))
740 .setText(R.string.ssl_not_yet_valid);
741 placeholder.addView(ll);
742 }
743
744 new AlertDialog.Builder(mActivity).setTitle(
745 R.string.security_warning).setIcon(
746 android.R.drawable.ic_dialog_alert).setView(
747 warningsView).setPositiveButton(R.string.ssl_continue,
748 new DialogInterface.OnClickListener() {
749 public void onClick(DialogInterface dialog,
750 int whichButton) {
751 handler.proceed();
752 }
753 }).setNeutralButton(R.string.view_certificate,
754 new DialogInterface.OnClickListener() {
755 public void onClick(DialogInterface dialog,
756 int whichButton) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700757 mWebViewController.showSslCertificateOnError(view,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700758 handler, error);
759 }
Ben Murdocha49b8292010-11-16 11:56:04 +0000760 }).setNegativeButton(R.string.ssl_go_back,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700761 new DialogInterface.OnClickListener() {
762 public void onClick(DialogInterface dialog,
763 int whichButton) {
John Reck30c714c2010-12-16 17:30:34 -0800764 dialog.cancel();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700765 }
766 }).setOnCancelListener(
767 new DialogInterface.OnCancelListener() {
768 public void onCancel(DialogInterface dialog) {
769 handler.cancel();
John Reck30c714c2010-12-16 17:30:34 -0800770 setLockIconType(LockIcon.LOCK_ICON_UNSECURE);
771 mWebViewController.onUserCanceledSsl(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700772 }
773 }).show();
774 } else {
775 handler.proceed();
776 }
777 }
778
779 /**
780 * Handles an HTTP authentication request.
781 *
782 * @param handler The authentication handler
783 * @param host The host
784 * @param realm The realm
785 */
786 @Override
787 public void onReceivedHttpAuthRequest(WebView view,
788 final HttpAuthHandler handler, final String host,
789 final String realm) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700790 mWebViewController.onReceivedHttpAuthRequest(Tab.this, view, handler, host, realm);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700791 }
792
793 @Override
794 public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
795 if (!mInForeground) {
796 return false;
797 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700798 return mWebViewController.shouldOverrideKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700799 }
800
801 @Override
802 public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700803 if (!mInForeground) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700804 return;
805 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700806 mWebViewController.onUnhandledKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700807 }
808 };
809
810 // -------------------------------------------------------------------------
811 // WebChromeClient implementation for the main WebView
812 // -------------------------------------------------------------------------
813
814 private final WebChromeClient mWebChromeClient = new WebChromeClient() {
815 // Helper method to create a new tab or sub window.
816 private void createWindow(final boolean dialog, final Message msg) {
817 WebView.WebViewTransport transport =
818 (WebView.WebViewTransport) msg.obj;
819 if (dialog) {
820 createSubWindow();
Michael Kolb8233fac2010-10-26 16:08:53 -0700821 mWebViewController.attachSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700822 transport.setWebView(mSubView);
823 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -0700824 final Tab newTab = mWebViewController.openTabAndShow(
Michael Kolb18eb3772010-12-10 14:29:51 -0800825 Tab.this,
Michael Kolb8233fac2010-10-26 16:08:53 -0700826 IntentHandler.EMPTY_URL_DATA, false, null);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700827 if (newTab != Tab.this) {
828 Tab.this.addChildTab(newTab);
829 }
830 transport.setWebView(newTab.getWebView());
831 }
832 msg.sendToTarget();
833 }
834
835 @Override
836 public boolean onCreateWindow(WebView view, final boolean dialog,
837 final boolean userGesture, final Message resultMsg) {
838 // only allow new window or sub window for the foreground case
839 if (!mInForeground) {
840 return false;
841 }
842 // Short-circuit if we can't create any more tabs or sub windows.
843 if (dialog && mSubView != null) {
844 new AlertDialog.Builder(mActivity)
845 .setTitle(R.string.too_many_subwindows_dialog_title)
846 .setIcon(android.R.drawable.ic_dialog_alert)
847 .setMessage(R.string.too_many_subwindows_dialog_message)
848 .setPositiveButton(R.string.ok, null)
849 .show();
850 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700851 } else if (!mWebViewController.getTabControl().canCreateNewTab()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700852 new AlertDialog.Builder(mActivity)
853 .setTitle(R.string.too_many_windows_dialog_title)
854 .setIcon(android.R.drawable.ic_dialog_alert)
855 .setMessage(R.string.too_many_windows_dialog_message)
856 .setPositiveButton(R.string.ok, null)
857 .show();
858 return false;
859 }
860
861 // Short-circuit if this was a user gesture.
862 if (userGesture) {
863 createWindow(dialog, resultMsg);
864 return true;
865 }
866
867 // Allow the popup and create the appropriate window.
868 final AlertDialog.OnClickListener allowListener =
869 new AlertDialog.OnClickListener() {
870 public void onClick(DialogInterface d,
871 int which) {
872 createWindow(dialog, resultMsg);
873 }
874 };
875
876 // Block the popup by returning a null WebView.
877 final AlertDialog.OnClickListener blockListener =
878 new AlertDialog.OnClickListener() {
879 public void onClick(DialogInterface d, int which) {
880 resultMsg.sendToTarget();
881 }
882 };
883
884 // Build a confirmation dialog to display to the user.
885 final AlertDialog d =
886 new AlertDialog.Builder(mActivity)
887 .setTitle(R.string.attention)
888 .setIcon(android.R.drawable.ic_dialog_alert)
889 .setMessage(R.string.popup_window_attempt)
890 .setPositiveButton(R.string.allow, allowListener)
891 .setNegativeButton(R.string.block, blockListener)
892 .setCancelable(false)
893 .create();
894
895 // Show the confirmation dialog.
896 d.show();
897 return true;
898 }
899
900 @Override
Patrick Scotteb5061b2009-11-18 15:00:30 -0500901 public void onRequestFocus(WebView view) {
902 if (!mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700903 mWebViewController.switchToTab(mWebViewController.getTabControl().getTabIndex(
Patrick Scotteb5061b2009-11-18 15:00:30 -0500904 Tab.this));
905 }
906 }
907
908 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700909 public void onCloseWindow(WebView window) {
910 if (mParentTab != null) {
911 // JavaScript can only close popup window.
912 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700913 mWebViewController.switchToTab(mWebViewController.getTabControl()
Grace Kloba22ac16e2009-10-07 18:00:23 -0700914 .getTabIndex(mParentTab));
915 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700916 mWebViewController.closeTab(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700917 }
918 }
919
920 @Override
921 public void onProgressChanged(WebView view, int newProgress) {
John Reck30c714c2010-12-16 17:30:34 -0800922 mPageLoadProgress = newProgress;
923 mWebViewController.onProgressChanged(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700924 }
925
926 @Override
Leon Scroggins21d9b902010-03-11 09:33:11 -0500927 public void onReceivedTitle(WebView view, final String title) {
John Reck30c714c2010-12-16 17:30:34 -0800928 mCurrentState.mTitle = title;
Michael Kolb8233fac2010-10-26 16:08:53 -0700929 mWebViewController.onReceivedTitle(Tab.this, title);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700930 }
931
932 @Override
933 public void onReceivedIcon(WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -0800934 mCurrentState.mFavicon = icon;
Michael Kolb8233fac2010-10-26 16:08:53 -0700935 mWebViewController.onFavicon(Tab.this, view, icon);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700936 }
937
938 @Override
939 public void onReceivedTouchIconUrl(WebView view, String url,
940 boolean precomposed) {
941 final ContentResolver cr = mActivity.getContentResolver();
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400942 // Let precomposed icons take precedence over non-composed
943 // icons.
944 if (precomposed && mTouchIconLoader != null) {
945 mTouchIconLoader.cancel(false);
946 mTouchIconLoader = null;
947 }
948 // Have only one async task at a time.
949 if (mTouchIconLoader == null) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700950 mTouchIconLoader = new DownloadTouchIcon(Tab.this,
951 mActivity, cr, view);
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400952 mTouchIconLoader.execute(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700953 }
954 }
955
956 @Override
957 public void onShowCustomView(View view,
958 WebChromeClient.CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700959 if (mInForeground) mWebViewController.showCustomView(Tab.this, view,
960 callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700961 }
962
963 @Override
964 public void onHideCustomView() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700965 if (mInForeground) mWebViewController.hideCustomView();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700966 }
967
968 /**
969 * The origin has exceeded its database quota.
970 * @param url the URL that exceeded the quota
971 * @param databaseIdentifier the identifier of the database on which the
972 * transaction that caused the quota overflow was run
973 * @param currentQuota the current quota for the origin.
974 * @param estimatedSize the estimated size of the database.
975 * @param totalUsedQuota is the sum of all origins' quota.
976 * @param quotaUpdater The callback to run when a decision to allow or
977 * deny quota has been made. Don't forget to call this!
978 */
979 @Override
980 public void onExceededDatabaseQuota(String url,
981 String databaseIdentifier, long currentQuota, long estimatedSize,
982 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
983 BrowserSettings.getInstance().getWebStorageSizeManager()
984 .onExceededDatabaseQuota(url, databaseIdentifier,
985 currentQuota, estimatedSize, totalUsedQuota,
986 quotaUpdater);
987 }
988
989 /**
990 * The Application Cache has exceeded its max size.
991 * @param spaceNeeded is the amount of disk space that would be needed
992 * in order for the last appcache operation to succeed.
993 * @param totalUsedQuota is the sum of all origins' quota.
994 * @param quotaUpdater A callback to inform the WebCore thread that a
995 * new app cache size is available. This callback must always
996 * be executed at some point to ensure that the sleeping
997 * WebCore thread is woken up.
998 */
999 @Override
1000 public void onReachedMaxAppCacheSize(long spaceNeeded,
1001 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
1002 BrowserSettings.getInstance().getWebStorageSizeManager()
1003 .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota,
1004 quotaUpdater);
1005 }
1006
1007 /**
1008 * Instructs the browser to show a prompt to ask the user to set the
1009 * Geolocation permission state for the specified origin.
1010 * @param origin The origin for which Geolocation permissions are
1011 * requested.
1012 * @param callback The callback to call once the user has set the
1013 * Geolocation permission state.
1014 */
1015 @Override
1016 public void onGeolocationPermissionsShowPrompt(String origin,
1017 GeolocationPermissions.Callback callback) {
1018 if (mInForeground) {
Grace Kloba50c241e2010-04-20 11:07:50 -07001019 getGeolocationPermissionsPrompt().show(origin, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001020 }
1021 }
1022
1023 /**
1024 * Instructs the browser to hide the Geolocation permissions prompt.
1025 */
1026 @Override
1027 public void onGeolocationPermissionsHidePrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001028 if (mInForeground && mGeolocationPermissionsPrompt != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001029 mGeolocationPermissionsPrompt.hide();
1030 }
1031 }
1032
Ben Murdoch65acc352009-11-19 18:16:04 +00001033 /* Adds a JavaScript error message to the system log and if the JS
1034 * console is enabled in the about:debug options, to that console
1035 * also.
Ben Murdochc42addf2010-01-28 15:19:59 +00001036 * @param consoleMessage the message object.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001037 */
1038 @Override
Ben Murdochc42addf2010-01-28 15:19:59 +00001039 public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001040 if (mInForeground) {
1041 // call getErrorConsole(true) so it will create one if needed
1042 ErrorConsoleView errorConsole = getErrorConsole(true);
Ben Murdochc42addf2010-01-28 15:19:59 +00001043 errorConsole.addErrorMessage(consoleMessage);
Michael Kolb8233fac2010-10-26 16:08:53 -07001044 if (mWebViewController.shouldShowErrorConsole()
1045 && errorConsole.getShowState() !=
1046 ErrorConsoleView.SHOW_MAXIMIZED) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001047 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
1048 }
1049 }
Ben Murdochc42addf2010-01-28 15:19:59 +00001050
Jeff Hamilton47654f42010-09-07 09:57:51 -05001051 // Don't log console messages in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001052 if (isPrivateBrowsingEnabled()) return true;
Jeff Hamilton47654f42010-09-07 09:57:51 -05001053
Ben Murdochc42addf2010-01-28 15:19:59 +00001054 String message = "Console: " + consoleMessage.message() + " "
1055 + consoleMessage.sourceId() + ":"
1056 + consoleMessage.lineNumber();
1057
1058 switch (consoleMessage.messageLevel()) {
1059 case TIP:
1060 Log.v(CONSOLE_LOGTAG, message);
1061 break;
1062 case LOG:
1063 Log.i(CONSOLE_LOGTAG, message);
1064 break;
1065 case WARNING:
1066 Log.w(CONSOLE_LOGTAG, message);
1067 break;
1068 case ERROR:
1069 Log.e(CONSOLE_LOGTAG, message);
1070 break;
1071 case DEBUG:
1072 Log.d(CONSOLE_LOGTAG, message);
1073 break;
1074 }
1075
1076 return true;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001077 }
1078
1079 /**
1080 * Ask the browser for an icon to represent a <video> element.
1081 * This icon will be used if the Web page did not specify a poster attribute.
1082 * @return Bitmap The icon or null if no such icon is available.
1083 */
1084 @Override
1085 public Bitmap getDefaultVideoPoster() {
1086 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001087 return mWebViewController.getDefaultVideoPoster();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001088 }
1089 return null;
1090 }
1091
1092 /**
1093 * Ask the host application for a custom progress view to show while
1094 * a <video> is loading.
1095 * @return View The progress view.
1096 */
1097 @Override
1098 public View getVideoLoadingProgressView() {
1099 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001100 return mWebViewController.getVideoLoadingProgressView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001101 }
1102 return null;
1103 }
1104
1105 @Override
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01001106 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001107 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001108 mWebViewController.openFileChooser(uploadMsg, acceptType);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001109 } else {
1110 uploadMsg.onReceiveValue(null);
1111 }
1112 }
1113
1114 /**
1115 * Deliver a list of already-visited URLs
1116 */
1117 @Override
1118 public void getVisitedHistory(final ValueCallback<String[]> callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001119 mWebViewController.getVisitedHistory(callback);
1120 }
Ben Murdoch8029a772010-11-16 11:58:21 +00001121
1122 @Override
1123 public void setupAutoFill(Message message) {
1124 // Prompt the user to set up their profile.
1125 final Message msg = message;
1126 AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
1127 builder.setMessage(R.string.autofill_setup_dialog_message)
1128 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1129 @Override
1130 public void onClick(DialogInterface dialog, int id) {
1131 // Take user to the AutoFill profile editor. When they return,
1132 // we will send the message that we pass here which will trigger
1133 // the form to get filled out with their new profile.
1134 mWebViewController.setupAutoFill(msg);
1135 }
1136 })
1137 .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
1138 @Override
1139 public void onClick(DialogInterface dialog, int id) {
1140 // Disable autofill and show a toast with how to turn it on again.
1141 BrowserSettings s = BrowserSettings.getInstance();
1142 s.addObserver(mMainView.getSettings());
1143 s.disableAutoFill(mActivity);
1144 s.update();
1145 Toast.makeText(mActivity, R.string.autofill_setup_dialog_negative_toast,
1146 Toast.LENGTH_LONG).show();
1147 }
1148 }).show();
1149 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001150 };
1151
1152 // -------------------------------------------------------------------------
1153 // WebViewClient implementation for the sub window
1154 // -------------------------------------------------------------------------
1155
1156 // Subclass of WebViewClient used in subwindows to notify the main
1157 // WebViewClient of certain WebView activities.
1158 private static class SubWindowClient extends WebViewClient {
1159 // The main WebViewClient.
1160 private final WebViewClient mClient;
Michael Kolb8233fac2010-10-26 16:08:53 -07001161 private final WebViewController mController;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001162
Michael Kolb8233fac2010-10-26 16:08:53 -07001163 SubWindowClient(WebViewClient client, WebViewController controller) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001164 mClient = client;
Michael Kolb8233fac2010-10-26 16:08:53 -07001165 mController = controller;
Leon Scroggins III211ba542010-04-19 13:21:13 -04001166 }
1167 @Override
1168 public void onPageStarted(WebView view, String url, Bitmap favicon) {
1169 // Unlike the others, do not call mClient's version, which would
1170 // change the progress bar. However, we do want to remove the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001171 // find or select dialog.
Michael Kolb8233fac2010-10-26 16:08:53 -07001172 mController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001173 }
1174 @Override
1175 public void doUpdateVisitedHistory(WebView view, String url,
1176 boolean isReload) {
1177 mClient.doUpdateVisitedHistory(view, url, isReload);
1178 }
1179 @Override
1180 public boolean shouldOverrideUrlLoading(WebView view, String url) {
1181 return mClient.shouldOverrideUrlLoading(view, url);
1182 }
1183 @Override
1184 public void onReceivedSslError(WebView view, SslErrorHandler handler,
1185 SslError error) {
1186 mClient.onReceivedSslError(view, handler, error);
1187 }
1188 @Override
1189 public void onReceivedHttpAuthRequest(WebView view,
1190 HttpAuthHandler handler, String host, String realm) {
1191 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
1192 }
1193 @Override
1194 public void onFormResubmission(WebView view, Message dontResend,
1195 Message resend) {
1196 mClient.onFormResubmission(view, dontResend, resend);
1197 }
1198 @Override
1199 public void onReceivedError(WebView view, int errorCode,
1200 String description, String failingUrl) {
1201 mClient.onReceivedError(view, errorCode, description, failingUrl);
1202 }
1203 @Override
1204 public boolean shouldOverrideKeyEvent(WebView view,
1205 android.view.KeyEvent event) {
1206 return mClient.shouldOverrideKeyEvent(view, event);
1207 }
1208 @Override
1209 public void onUnhandledKeyEvent(WebView view,
1210 android.view.KeyEvent event) {
1211 mClient.onUnhandledKeyEvent(view, event);
1212 }
1213 }
1214
1215 // -------------------------------------------------------------------------
1216 // WebChromeClient implementation for the sub window
1217 // -------------------------------------------------------------------------
1218
1219 private class SubWindowChromeClient extends WebChromeClient {
1220 // The main WebChromeClient.
1221 private final WebChromeClient mClient;
1222
1223 SubWindowChromeClient(WebChromeClient client) {
1224 mClient = client;
1225 }
1226 @Override
1227 public void onProgressChanged(WebView view, int newProgress) {
1228 mClient.onProgressChanged(view, newProgress);
1229 }
1230 @Override
1231 public boolean onCreateWindow(WebView view, boolean dialog,
1232 boolean userGesture, android.os.Message resultMsg) {
1233 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
1234 }
1235 @Override
1236 public void onCloseWindow(WebView window) {
1237 if (window != mSubView) {
1238 Log.e(LOGTAG, "Can't close the window");
1239 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001240 mWebViewController.dismissSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001241 }
1242 }
1243
1244 // -------------------------------------------------------------------------
1245
Michael Kolb8233fac2010-10-26 16:08:53 -07001246 // TODO temporarily use activity here
1247 // remove later
1248
Grace Kloba22ac16e2009-10-07 18:00:23 -07001249 // Construct a new tab
Michael Kolb8233fac2010-10-26 16:08:53 -07001250 Tab(WebViewController wvcontroller, WebView w, boolean closeOnExit, String appId,
Grace Kloba22ac16e2009-10-07 18:00:23 -07001251 String url) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001252 mWebViewController = wvcontroller;
1253 mActivity = mWebViewController.getActivity();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001254 mCloseOnExit = closeOnExit;
1255 mAppId = appId;
John Recke969cc52010-12-21 17:24:43 -08001256 mDataController = DataController.getInstance(mActivity);
John Reck30c714c2010-12-16 17:30:34 -08001257 mCurrentState = new PageState(mActivity, w.isPrivateBrowsingEnabled());
Michael Kolb8233fac2010-10-26 16:08:53 -07001258 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001259 mInForeground = false;
1260
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001261 mDownloadListener = new DownloadListener() {
1262 public void onDownloadStart(String url, String userAgent,
1263 String contentDisposition, String mimetype,
1264 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001265 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001266 mimetype, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001267 }
1268 };
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001269 mWebBackForwardListClient = new WebBackForwardListClient() {
1270 @Override
1271 public void onNewHistoryItem(WebHistoryItem item) {
1272 if (isInVoiceSearchMode()) {
1273 item.setCustomData(mVoiceSearchData.mVoiceSearchIntent);
1274 }
1275 }
1276 @Override
1277 public void onIndexChanged(WebHistoryItem item, int index) {
1278 Object data = item.getCustomData();
1279 if (data != null && data instanceof Intent) {
1280 activateVoiceSearchMode((Intent) data);
1281 }
1282 }
1283 };
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001284
Grace Kloba22ac16e2009-10-07 18:00:23 -07001285 setWebView(w);
1286 }
1287
1288 /**
1289 * Sets the WebView for this tab, correctly removing the old WebView from
1290 * the container view.
1291 */
1292 void setWebView(WebView w) {
1293 if (mMainView == w) {
1294 return;
1295 }
Michael Kolba713ec82010-11-29 17:27:06 -08001296
Grace Kloba22ac16e2009-10-07 18:00:23 -07001297 // If the WebView is changing, the page will be reloaded, so any ongoing
1298 // Geolocation permission requests are void.
Grace Kloba50c241e2010-04-20 11:07:50 -07001299 if (mGeolocationPermissionsPrompt != null) {
1300 mGeolocationPermissionsPrompt.hide();
1301 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001302
Michael Kolba713ec82010-11-29 17:27:06 -08001303 mWebViewController.onSetWebView(this, w);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001304
1305 // set the new one
1306 mMainView = w;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001307 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001308 if (mMainView != null) {
1309 mMainView.setWebViewClient(mWebViewClient);
1310 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001311 // Attach DownloadManager so that downloads can start in an active
1312 // or a non-active window. This can happen when going to a site that
1313 // does a redirect after a period of time. The user could have
1314 // switched to another tab while waiting for the download to start.
1315 mMainView.setDownloadListener(mDownloadListener);
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001316 mMainView.setWebBackForwardListClient(mWebBackForwardListClient);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001317 }
1318 }
1319
1320 /**
1321 * Destroy the tab's main WebView and subWindow if any
1322 */
1323 void destroy() {
1324 if (mMainView != null) {
1325 dismissSubWindow();
1326 BrowserSettings.getInstance().deleteObserver(mMainView.getSettings());
1327 // save the WebView to call destroy() after detach it from the tab
1328 WebView webView = mMainView;
1329 setWebView(null);
1330 webView.destroy();
1331 }
1332 }
1333
1334 /**
1335 * Remove the tab from the parent
1336 */
1337 void removeFromTree() {
1338 // detach the children
1339 if (mChildTabs != null) {
1340 for(Tab t : mChildTabs) {
1341 t.setParentTab(null);
1342 }
1343 }
1344 // remove itself from the parent list
1345 if (mParentTab != null) {
1346 mParentTab.mChildTabs.remove(this);
1347 }
1348 }
1349
1350 /**
1351 * Create a new subwindow unless a subwindow already exists.
1352 * @return True if a new subwindow was created. False if one already exists.
1353 */
1354 boolean createSubWindow() {
1355 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001356 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001357 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001358 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001359 mSubView.setWebChromeClient(new SubWindowChromeClient(
1360 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001361 // Set a different DownloadListener for the mSubView, since it will
1362 // just need to dismiss the mSubView, rather than close the Tab
1363 mSubView.setDownloadListener(new DownloadListener() {
1364 public void onDownloadStart(String url, String userAgent,
1365 String contentDisposition, String mimetype,
1366 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001367 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001368 contentDisposition, mimetype, contentLength);
1369 if (mSubView.copyBackForwardList().getSize() == 0) {
1370 // This subwindow was opened for the sole purpose of
1371 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001372 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001373 }
1374 }
1375 });
Grace Kloba22ac16e2009-10-07 18:00:23 -07001376 mSubView.setOnCreateContextMenuListener(mActivity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001377 return true;
1378 }
1379 return false;
1380 }
1381
1382 /**
1383 * Dismiss the subWindow for the tab.
1384 */
1385 void dismissSubWindow() {
1386 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001387 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001388 BrowserSettings.getInstance().deleteObserver(
1389 mSubView.getSettings());
1390 mSubView.destroy();
1391 mSubView = null;
1392 mSubViewContainer = null;
1393 }
1394 }
1395
Grace Kloba22ac16e2009-10-07 18:00:23 -07001396
1397 /**
1398 * Set the parent tab of this tab.
1399 */
1400 void setParentTab(Tab parent) {
1401 mParentTab = parent;
1402 // This tab may have been freed due to low memory. If that is the case,
1403 // the parent tab index is already saved. If we are changing that index
1404 // (most likely due to removing the parent tab) we must update the
1405 // parent tab index in the saved Bundle.
1406 if (mSavedState != null) {
1407 if (parent == null) {
1408 mSavedState.remove(PARENTTAB);
1409 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -07001410 mSavedState.putInt(PARENTTAB, mWebViewController.getTabControl()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001411 .getTabIndex(parent));
1412 }
1413 }
1414 }
1415
1416 /**
1417 * When a Tab is created through the content of another Tab, then we
1418 * associate the Tabs.
1419 * @param child the Tab that was created from this Tab
1420 */
1421 void addChildTab(Tab child) {
1422 if (mChildTabs == null) {
1423 mChildTabs = new Vector<Tab>();
1424 }
1425 mChildTabs.add(child);
1426 child.setParentTab(this);
1427 }
1428
1429 Vector<Tab> getChildTabs() {
1430 return mChildTabs;
1431 }
1432
1433 void resume() {
1434 if (mMainView != null) {
1435 mMainView.onResume();
1436 if (mSubView != null) {
1437 mSubView.onResume();
1438 }
1439 }
1440 }
1441
1442 void pause() {
1443 if (mMainView != null) {
1444 mMainView.onPause();
1445 if (mSubView != null) {
1446 mSubView.onPause();
1447 }
1448 }
1449 }
1450
1451 void putInForeground() {
1452 mInForeground = true;
1453 resume();
1454 mMainView.setOnCreateContextMenuListener(mActivity);
1455 if (mSubView != null) {
1456 mSubView.setOnCreateContextMenuListener(mActivity);
1457 }
1458 // Show the pending error dialog if the queue is not empty
1459 if (mQueuedErrors != null && mQueuedErrors.size() > 0) {
1460 showError(mQueuedErrors.getFirst());
1461 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001462 mWebViewController.bookmarkedStatusHasChanged(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001463 }
1464
1465 void putInBackground() {
1466 mInForeground = false;
1467 pause();
1468 mMainView.setOnCreateContextMenuListener(null);
1469 if (mSubView != null) {
1470 mSubView.setOnCreateContextMenuListener(null);
1471 }
1472 }
1473
Michael Kolb8233fac2010-10-26 16:08:53 -07001474 boolean inForeground() {
1475 return mInForeground;
1476 }
1477
Grace Kloba22ac16e2009-10-07 18:00:23 -07001478 /**
1479 * Return the top window of this tab; either the subwindow if it is not
1480 * null or the main window.
1481 * @return The top window of this tab.
1482 */
1483 WebView getTopWindow() {
1484 if (mSubView != null) {
1485 return mSubView;
1486 }
1487 return mMainView;
1488 }
1489
1490 /**
1491 * Return the main window of this tab. Note: if a tab is freed in the
1492 * background, this can return null. It is only guaranteed to be
1493 * non-null for the current tab.
1494 * @return The main WebView of this tab.
1495 */
1496 WebView getWebView() {
1497 return mMainView;
1498 }
1499
Michael Kolba713ec82010-11-29 17:27:06 -08001500 void setViewContainer(View container) {
1501 mContainer = container;
1502 }
1503
Michael Kolb8233fac2010-10-26 16:08:53 -07001504 View getViewContainer() {
1505 return mContainer;
1506 }
1507
Grace Kloba22ac16e2009-10-07 18:00:23 -07001508 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001509 * Return whether private browsing is enabled for the main window of
1510 * this tab.
1511 * @return True if private browsing is enabled.
1512 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001513 boolean isPrivateBrowsingEnabled() {
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001514 WebView webView = getWebView();
1515 if (webView == null) {
1516 return false;
1517 }
1518 return webView.isPrivateBrowsingEnabled();
1519 }
1520
1521 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001522 * Return the subwindow of this tab or null if there is no subwindow.
1523 * @return The subwindow of this tab or null.
1524 */
1525 WebView getSubWebView() {
1526 return mSubView;
1527 }
1528
Michael Kolb1514bb72010-11-22 09:11:48 -08001529 void setSubWebView(WebView subView) {
1530 mSubView = subView;
1531 }
1532
Michael Kolb8233fac2010-10-26 16:08:53 -07001533 View getSubViewContainer() {
1534 return mSubViewContainer;
1535 }
1536
Michael Kolb1514bb72010-11-22 09:11:48 -08001537 void setSubViewContainer(View subViewContainer) {
1538 mSubViewContainer = subViewContainer;
1539 }
1540
Grace Kloba22ac16e2009-10-07 18:00:23 -07001541 /**
1542 * @return The geolocation permissions prompt for this tab.
1543 */
1544 GeolocationPermissionsPrompt getGeolocationPermissionsPrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001545 if (mGeolocationPermissionsPrompt == null) {
1546 ViewStub stub = (ViewStub) mContainer
1547 .findViewById(R.id.geolocation_permissions_prompt);
1548 mGeolocationPermissionsPrompt = (GeolocationPermissionsPrompt) stub
1549 .inflate();
1550 mGeolocationPermissionsPrompt.init();
1551 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001552 return mGeolocationPermissionsPrompt;
1553 }
1554
1555 /**
1556 * @return The application id string
1557 */
1558 String getAppId() {
1559 return mAppId;
1560 }
1561
1562 /**
1563 * Set the application id string
1564 * @param id
1565 */
1566 void setAppId(String id) {
1567 mAppId = id;
1568 }
1569
Grace Kloba22ac16e2009-10-07 18:00:23 -07001570 String getUrl() {
John Reck324d4402011-01-11 16:56:42 -08001571 return UrlUtils.filteredUrl(mCurrentState.mUrl);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001572 }
1573
1574 /**
John Reck30c714c2010-12-16 17:30:34 -08001575 * Get the title of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001576 */
1577 String getTitle() {
John Reck30c714c2010-12-16 17:30:34 -08001578 if (mCurrentState.mTitle == null && mInPageLoad) {
1579 return mActivity.getString(R.string.title_bar_loading);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001580 }
John Reck30c714c2010-12-16 17:30:34 -08001581 return mCurrentState.mTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001582 }
1583
1584 /**
John Reck30c714c2010-12-16 17:30:34 -08001585 * Get the favicon of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001586 */
1587 Bitmap getFavicon() {
John Reck30c714c2010-12-16 17:30:34 -08001588 return mCurrentState.mFavicon;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001589 }
1590
John Recke969cc52010-12-21 17:24:43 -08001591 public boolean isBookmarkedSite() {
1592 return mCurrentState.mIsBookmarkedSite;
1593 }
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001594
Grace Kloba22ac16e2009-10-07 18:00:23 -07001595 /**
1596 * Return the tab's error console. Creates the console if createIfNEcessary
1597 * is true and we haven't already created the console.
1598 * @param createIfNecessary Flag to indicate if the console should be
1599 * created if it has not been already.
1600 * @return The tab's error console, or null if one has not been created and
1601 * createIfNecessary is false.
1602 */
1603 ErrorConsoleView getErrorConsole(boolean createIfNecessary) {
1604 if (createIfNecessary && mErrorConsole == null) {
1605 mErrorConsole = new ErrorConsoleView(mActivity);
1606 mErrorConsole.setWebView(mMainView);
1607 }
1608 return mErrorConsole;
1609 }
1610
1611 /**
1612 * If this Tab was created through another Tab, then this method returns
1613 * that Tab.
1614 * @return the Tab parent or null
1615 */
1616 public Tab getParentTab() {
1617 return mParentTab;
1618 }
1619
1620 /**
1621 * Return whether this tab should be closed when it is backing out of the
1622 * first page.
1623 * @return TRUE if this tab should be closed when exit.
1624 */
1625 boolean closeOnExit() {
1626 return mCloseOnExit;
1627 }
1628
John Reck30c714c2010-12-16 17:30:34 -08001629 private void setLockIconType(LockIcon icon) {
1630 mCurrentState.mLockIcon = icon;
1631 mWebViewController.onUpdatedLockIcon(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001632 }
1633
1634 /**
1635 * @return The tab's lock icon type.
1636 */
John Reck30c714c2010-12-16 17:30:34 -08001637 LockIcon getLockIconType() {
1638 return mCurrentState.mLockIcon;
1639 }
1640
1641 int getLoadProgress() {
1642 if (mInPageLoad) {
1643 return mPageLoadProgress;
1644 }
1645 return 100;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001646 }
1647
1648 /**
1649 * @return TRUE if onPageStarted is called while onPageFinished is not
1650 * called yet.
1651 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001652 boolean inPageLoad() {
1653 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001654 }
1655
1656 // force mInLoad to be false. This should only be called before closing the
1657 // tab to ensure BrowserActivity's pauseWebViewTimers() is called correctly.
Michael Kolb8233fac2010-10-26 16:08:53 -07001658 void clearInPageLoad() {
1659 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001660 }
1661
Grace Kloba22ac16e2009-10-07 18:00:23 -07001662 /**
John Reck30c714c2010-12-16 17:30:34 -08001663 * Get the cached saved state bundle.
1664 * @return cached state bundle
Grace Kloba22ac16e2009-10-07 18:00:23 -07001665 */
1666 Bundle getSavedState() {
1667 return mSavedState;
1668 }
1669
1670 /**
1671 * Set the saved state.
1672 */
1673 void setSavedState(Bundle state) {
1674 mSavedState = state;
1675 }
1676
1677 /**
1678 * @return TRUE if succeed in saving the state.
1679 */
1680 boolean saveState() {
1681 // If the WebView is null it means we ran low on memory and we already
1682 // stored the saved state in mSavedState.
1683 if (mMainView == null) {
1684 return mSavedState != null;
1685 }
1686
1687 mSavedState = new Bundle();
1688 final WebBackForwardList list = mMainView.saveState(mSavedState);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001689
1690 // Store some extra info for displaying the tab in the picker.
1691 final WebHistoryItem item = list != null ? list.getCurrentItem() : null;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001692
John Reck30c714c2010-12-16 17:30:34 -08001693 mSavedState.putString(CURRURL, mCurrentState.mUrl);
1694 mSavedState.putString(CURRTITLE, mCurrentState.mTitle);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001695 mSavedState.putBoolean(CLOSEONEXIT, mCloseOnExit);
1696 if (mAppId != null) {
1697 mSavedState.putString(APPID, mAppId);
1698 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001699 // Remember the parent tab so the relationship can be restored.
1700 if (mParentTab != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001701 mSavedState.putInt(PARENTTAB, mWebViewController.getTabControl().getTabIndex(
Grace Kloba22ac16e2009-10-07 18:00:23 -07001702 mParentTab));
1703 }
1704 return true;
1705 }
1706
1707 /*
1708 * Restore the state of the tab.
1709 */
1710 boolean restoreState(Bundle b) {
1711 if (b == null) {
1712 return false;
1713 }
1714 // Restore the internal state even if the WebView fails to restore.
1715 // This will maintain the app id, original url and close-on-exit values.
1716 mSavedState = null;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001717 mCloseOnExit = b.getBoolean(CLOSEONEXIT);
1718 mAppId = b.getString(APPID);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001719
1720 final WebBackForwardList list = mMainView.restoreState(b);
1721 if (list == null) {
1722 return false;
1723 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001724 return true;
1725 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04001726
Leon Scroggins1961ed22010-12-07 15:22:21 -05001727 public void updateBookmarkedStatus() {
John Recke969cc52010-12-21 17:24:43 -08001728 mDataController.queryBookmarkStatus(getUrl(), mIsBookmarkCallback);
Leon Scroggins1961ed22010-12-07 15:22:21 -05001729 }
1730
John Recke969cc52010-12-21 17:24:43 -08001731 private DataController.OnQueryUrlIsBookmark mIsBookmarkCallback
1732 = new DataController.OnQueryUrlIsBookmark() {
1733 @Override
1734 public void onQueryUrlIsBookmark(String url, boolean isBookmark) {
1735 if (mCurrentState.mUrl.equals(url)) {
1736 mCurrentState.mIsBookmarkedSite = isBookmark;
1737 mWebViewController.bookmarkedStatusHasChanged(Tab.this);
1738 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001739 }
John Recke969cc52010-12-21 17:24:43 -08001740 };
Grace Kloba22ac16e2009-10-07 18:00:23 -07001741}