blob: c4edda689f1314703fa527f76e9fc1f83786886e [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
Michael Kolb8233fac2010-10-26 16:08:53 -0700535 mWebViewController.onPageStarted(Tab.this, view, url, 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();
549 mCurrentState.mTitle = view.getTitle();
550 mCurrentState.mFavicon = view.getFavicon();
551 if (!URLUtil.isHttpsUrl(mCurrentState.mUrl)) {
552 // In case we stop when loading an HTTPS page from an HTTP page
553 // but before a provisional load occurred
554 mCurrentState.mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
555 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700556 mWebViewController.onPageFinished(Tab.this, url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700557 }
558
559 // return true if want to hijack the url to let another app to handle it
560 @Override
561 public boolean shouldOverrideUrlLoading(WebView view, String url) {
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400562 if (voiceSearchSourceIsGoogle()) {
563 // This method is called when the user clicks on a link.
564 // VoiceSearchMode is turned off when the user leaves the
565 // Google results page, so at this point the user must be on
566 // that page. If the user clicked a link on that page, assume
567 // that the voice search was effective, and broadcast an Intent
568 // so a receiver can take note of that fact.
569 Intent logIntent = new Intent(LoggingEvents.ACTION_LOG_EVENT);
570 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
571 LoggingEvents.VoiceSearch.RESULT_CLICKED);
572 mActivity.sendBroadcast(logIntent);
573 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700574 if (mInForeground) {
Michael Kolb18eb3772010-12-10 14:29:51 -0800575 return mWebViewController.shouldOverrideUrlLoading(Tab.this,
576 view, url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700577 } else {
578 return false;
579 }
580 }
581
582 /**
583 * Updates the lock icon. This method is called when we discover another
584 * resource to be loaded for this page (for example, javascript). While
585 * we update the icon type, we do not update the lock icon itself until
586 * we are done loading, it is slightly more secure this way.
587 */
588 @Override
589 public void onLoadResource(WebView view, String url) {
590 if (url != null && url.length() > 0) {
591 // It is only if the page claims to be secure that we may have
592 // to update the lock:
John Reck30c714c2010-12-16 17:30:34 -0800593 if (mCurrentState.mLockIcon == LockIcon.LOCK_ICON_SECURE) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700594 // If NOT a 'safe' url, change the lock to mixed content!
595 if (!(URLUtil.isHttpsUrl(url) || URLUtil.isDataUrl(url)
596 || URLUtil.isAboutUrl(url))) {
John Reck30c714c2010-12-16 17:30:34 -0800597 mCurrentState.mLockIcon = LockIcon.LOCK_ICON_MIXED;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700598 }
599 }
600 }
601 }
602
603 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700604 * Show a dialog informing the user of the network error reported by
605 * WebCore if it is in the foreground.
606 */
607 @Override
608 public void onReceivedError(WebView view, int errorCode,
609 String description, String failingUrl) {
610 if (errorCode != WebViewClient.ERROR_HOST_LOOKUP &&
611 errorCode != WebViewClient.ERROR_CONNECT &&
612 errorCode != WebViewClient.ERROR_BAD_URL &&
613 errorCode != WebViewClient.ERROR_UNSUPPORTED_SCHEME &&
614 errorCode != WebViewClient.ERROR_FILE) {
615 queueError(errorCode, description);
616 }
Jeff Hamilton47654f42010-09-07 09:57:51 -0500617
618 // Don't log URLs when in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -0700619 if (!isPrivateBrowsingEnabled()) {
Jeff Hamilton47654f42010-09-07 09:57:51 -0500620 Log.e(LOGTAG, "onReceivedError " + errorCode + " " + failingUrl
621 + " " + description);
622 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700623 }
624
625 /**
626 * Check with the user if it is ok to resend POST data as the page they
627 * are trying to navigate to is the result of a POST.
628 */
629 @Override
630 public void onFormResubmission(WebView view, final Message dontResend,
631 final Message resend) {
632 if (!mInForeground) {
633 dontResend.sendToTarget();
634 return;
635 }
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500636 if (mDontResend != null) {
637 Log.w(LOGTAG, "onFormResubmission should not be called again "
638 + "while dialog is still up");
639 dontResend.sendToTarget();
640 return;
641 }
642 mDontResend = dontResend;
643 mResend = resend;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700644 new AlertDialog.Builder(mActivity).setTitle(
645 R.string.browserFrameFormResubmitLabel).setMessage(
646 R.string.browserFrameFormResubmitMessage)
647 .setPositiveButton(R.string.ok,
648 new DialogInterface.OnClickListener() {
649 public void onClick(DialogInterface dialog,
650 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500651 if (mResend != null) {
652 mResend.sendToTarget();
653 mResend = null;
654 mDontResend = null;
655 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700656 }
657 }).setNegativeButton(R.string.cancel,
658 new DialogInterface.OnClickListener() {
659 public void onClick(DialogInterface dialog,
660 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500661 if (mDontResend != null) {
662 mDontResend.sendToTarget();
663 mResend = null;
664 mDontResend = null;
665 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700666 }
667 }).setOnCancelListener(new OnCancelListener() {
668 public void onCancel(DialogInterface dialog) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500669 if (mDontResend != null) {
670 mDontResend.sendToTarget();
671 mResend = null;
672 mDontResend = null;
673 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700674 }
675 }).show();
676 }
677
678 /**
679 * Insert the url into the visited history database.
680 * @param url The url to be inserted.
681 * @param isReload True if this url is being reloaded.
682 * FIXME: Not sure what to do when reloading the page.
683 */
684 @Override
685 public void doUpdateVisitedHistory(WebView view, String url,
686 boolean isReload) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700687 mWebViewController.doUpdateVisitedHistory(Tab.this, url, isReload);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700688 }
689
690 /**
691 * Displays SSL error(s) dialog to the user.
692 */
693 @Override
694 public void onReceivedSslError(final WebView view,
695 final SslErrorHandler handler, final SslError error) {
696 if (!mInForeground) {
697 handler.cancel();
John Reck30c714c2010-12-16 17:30:34 -0800698 setLockIconType(LockIcon.LOCK_ICON_UNSECURE);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700699 return;
700 }
701 if (BrowserSettings.getInstance().showSecurityWarnings()) {
702 final LayoutInflater factory =
703 LayoutInflater.from(mActivity);
704 final View warningsView =
705 factory.inflate(R.layout.ssl_warnings, null);
706 final LinearLayout placeholder =
707 (LinearLayout)warningsView.findViewById(R.id.placeholder);
708
709 if (error.hasError(SslError.SSL_UNTRUSTED)) {
710 LinearLayout ll = (LinearLayout)factory
711 .inflate(R.layout.ssl_warning, null);
712 ((TextView)ll.findViewById(R.id.warning))
713 .setText(R.string.ssl_untrusted);
714 placeholder.addView(ll);
715 }
716
717 if (error.hasError(SslError.SSL_IDMISMATCH)) {
718 LinearLayout ll = (LinearLayout)factory
719 .inflate(R.layout.ssl_warning, null);
720 ((TextView)ll.findViewById(R.id.warning))
721 .setText(R.string.ssl_mismatch);
722 placeholder.addView(ll);
723 }
724
725 if (error.hasError(SslError.SSL_EXPIRED)) {
726 LinearLayout ll = (LinearLayout)factory
727 .inflate(R.layout.ssl_warning, null);
728 ((TextView)ll.findViewById(R.id.warning))
729 .setText(R.string.ssl_expired);
730 placeholder.addView(ll);
731 }
732
733 if (error.hasError(SslError.SSL_NOTYETVALID)) {
734 LinearLayout ll = (LinearLayout)factory
735 .inflate(R.layout.ssl_warning, null);
736 ((TextView)ll.findViewById(R.id.warning))
737 .setText(R.string.ssl_not_yet_valid);
738 placeholder.addView(ll);
739 }
740
741 new AlertDialog.Builder(mActivity).setTitle(
742 R.string.security_warning).setIcon(
743 android.R.drawable.ic_dialog_alert).setView(
744 warningsView).setPositiveButton(R.string.ssl_continue,
745 new DialogInterface.OnClickListener() {
746 public void onClick(DialogInterface dialog,
747 int whichButton) {
748 handler.proceed();
749 }
750 }).setNeutralButton(R.string.view_certificate,
751 new DialogInterface.OnClickListener() {
752 public void onClick(DialogInterface dialog,
753 int whichButton) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700754 mWebViewController.showSslCertificateOnError(view,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700755 handler, error);
756 }
Ben Murdocha49b8292010-11-16 11:56:04 +0000757 }).setNegativeButton(R.string.ssl_go_back,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700758 new DialogInterface.OnClickListener() {
759 public void onClick(DialogInterface dialog,
760 int whichButton) {
John Reck30c714c2010-12-16 17:30:34 -0800761 dialog.cancel();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700762 }
763 }).setOnCancelListener(
764 new DialogInterface.OnCancelListener() {
765 public void onCancel(DialogInterface dialog) {
766 handler.cancel();
John Reck30c714c2010-12-16 17:30:34 -0800767 setLockIconType(LockIcon.LOCK_ICON_UNSECURE);
768 mWebViewController.onUserCanceledSsl(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700769 }
770 }).show();
771 } else {
772 handler.proceed();
773 }
774 }
775
776 /**
777 * Handles an HTTP authentication request.
778 *
779 * @param handler The authentication handler
780 * @param host The host
781 * @param realm The realm
782 */
783 @Override
784 public void onReceivedHttpAuthRequest(WebView view,
785 final HttpAuthHandler handler, final String host,
786 final String realm) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700787 mWebViewController.onReceivedHttpAuthRequest(Tab.this, view, handler, host, realm);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700788 }
789
790 @Override
791 public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
792 if (!mInForeground) {
793 return false;
794 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700795 return mWebViewController.shouldOverrideKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700796 }
797
798 @Override
799 public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700800 if (!mInForeground) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700801 return;
802 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700803 mWebViewController.onUnhandledKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700804 }
805 };
806
807 // -------------------------------------------------------------------------
808 // WebChromeClient implementation for the main WebView
809 // -------------------------------------------------------------------------
810
811 private final WebChromeClient mWebChromeClient = new WebChromeClient() {
812 // Helper method to create a new tab or sub window.
813 private void createWindow(final boolean dialog, final Message msg) {
814 WebView.WebViewTransport transport =
815 (WebView.WebViewTransport) msg.obj;
816 if (dialog) {
817 createSubWindow();
Michael Kolb8233fac2010-10-26 16:08:53 -0700818 mWebViewController.attachSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700819 transport.setWebView(mSubView);
820 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -0700821 final Tab newTab = mWebViewController.openTabAndShow(
Michael Kolb18eb3772010-12-10 14:29:51 -0800822 Tab.this,
Michael Kolb8233fac2010-10-26 16:08:53 -0700823 IntentHandler.EMPTY_URL_DATA, false, null);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700824 if (newTab != Tab.this) {
825 Tab.this.addChildTab(newTab);
826 }
827 transport.setWebView(newTab.getWebView());
828 }
829 msg.sendToTarget();
830 }
831
832 @Override
833 public boolean onCreateWindow(WebView view, final boolean dialog,
834 final boolean userGesture, final Message resultMsg) {
835 // only allow new window or sub window for the foreground case
836 if (!mInForeground) {
837 return false;
838 }
839 // Short-circuit if we can't create any more tabs or sub windows.
840 if (dialog && mSubView != null) {
841 new AlertDialog.Builder(mActivity)
842 .setTitle(R.string.too_many_subwindows_dialog_title)
843 .setIcon(android.R.drawable.ic_dialog_alert)
844 .setMessage(R.string.too_many_subwindows_dialog_message)
845 .setPositiveButton(R.string.ok, null)
846 .show();
847 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700848 } else if (!mWebViewController.getTabControl().canCreateNewTab()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700849 new AlertDialog.Builder(mActivity)
850 .setTitle(R.string.too_many_windows_dialog_title)
851 .setIcon(android.R.drawable.ic_dialog_alert)
852 .setMessage(R.string.too_many_windows_dialog_message)
853 .setPositiveButton(R.string.ok, null)
854 .show();
855 return false;
856 }
857
858 // Short-circuit if this was a user gesture.
859 if (userGesture) {
860 createWindow(dialog, resultMsg);
861 return true;
862 }
863
864 // Allow the popup and create the appropriate window.
865 final AlertDialog.OnClickListener allowListener =
866 new AlertDialog.OnClickListener() {
867 public void onClick(DialogInterface d,
868 int which) {
869 createWindow(dialog, resultMsg);
870 }
871 };
872
873 // Block the popup by returning a null WebView.
874 final AlertDialog.OnClickListener blockListener =
875 new AlertDialog.OnClickListener() {
876 public void onClick(DialogInterface d, int which) {
877 resultMsg.sendToTarget();
878 }
879 };
880
881 // Build a confirmation dialog to display to the user.
882 final AlertDialog d =
883 new AlertDialog.Builder(mActivity)
884 .setTitle(R.string.attention)
885 .setIcon(android.R.drawable.ic_dialog_alert)
886 .setMessage(R.string.popup_window_attempt)
887 .setPositiveButton(R.string.allow, allowListener)
888 .setNegativeButton(R.string.block, blockListener)
889 .setCancelable(false)
890 .create();
891
892 // Show the confirmation dialog.
893 d.show();
894 return true;
895 }
896
897 @Override
Patrick Scotteb5061b2009-11-18 15:00:30 -0500898 public void onRequestFocus(WebView view) {
899 if (!mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700900 mWebViewController.switchToTab(mWebViewController.getTabControl().getTabIndex(
Patrick Scotteb5061b2009-11-18 15:00:30 -0500901 Tab.this));
902 }
903 }
904
905 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700906 public void onCloseWindow(WebView window) {
907 if (mParentTab != null) {
908 // JavaScript can only close popup window.
909 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700910 mWebViewController.switchToTab(mWebViewController.getTabControl()
Grace Kloba22ac16e2009-10-07 18:00:23 -0700911 .getTabIndex(mParentTab));
912 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700913 mWebViewController.closeTab(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700914 }
915 }
916
917 @Override
918 public void onProgressChanged(WebView view, int newProgress) {
John Reck30c714c2010-12-16 17:30:34 -0800919 mPageLoadProgress = newProgress;
920 mWebViewController.onProgressChanged(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700921 }
922
923 @Override
Leon Scroggins21d9b902010-03-11 09:33:11 -0500924 public void onReceivedTitle(WebView view, final String title) {
John Reck30c714c2010-12-16 17:30:34 -0800925 mCurrentState.mTitle = title;
Michael Kolb8233fac2010-10-26 16:08:53 -0700926 mWebViewController.onReceivedTitle(Tab.this, title);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700927 }
928
929 @Override
930 public void onReceivedIcon(WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -0800931 mCurrentState.mFavicon = icon;
Michael Kolb8233fac2010-10-26 16:08:53 -0700932 mWebViewController.onFavicon(Tab.this, view, icon);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700933 }
934
935 @Override
936 public void onReceivedTouchIconUrl(WebView view, String url,
937 boolean precomposed) {
938 final ContentResolver cr = mActivity.getContentResolver();
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400939 // Let precomposed icons take precedence over non-composed
940 // icons.
941 if (precomposed && mTouchIconLoader != null) {
942 mTouchIconLoader.cancel(false);
943 mTouchIconLoader = null;
944 }
945 // Have only one async task at a time.
946 if (mTouchIconLoader == null) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700947 mTouchIconLoader = new DownloadTouchIcon(Tab.this,
948 mActivity, cr, view);
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400949 mTouchIconLoader.execute(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700950 }
951 }
952
953 @Override
954 public void onShowCustomView(View view,
955 WebChromeClient.CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700956 if (mInForeground) mWebViewController.showCustomView(Tab.this, view,
957 callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700958 }
959
960 @Override
961 public void onHideCustomView() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700962 if (mInForeground) mWebViewController.hideCustomView();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700963 }
964
965 /**
966 * The origin has exceeded its database quota.
967 * @param url the URL that exceeded the quota
968 * @param databaseIdentifier the identifier of the database on which the
969 * transaction that caused the quota overflow was run
970 * @param currentQuota the current quota for the origin.
971 * @param estimatedSize the estimated size of the database.
972 * @param totalUsedQuota is the sum of all origins' quota.
973 * @param quotaUpdater The callback to run when a decision to allow or
974 * deny quota has been made. Don't forget to call this!
975 */
976 @Override
977 public void onExceededDatabaseQuota(String url,
978 String databaseIdentifier, long currentQuota, long estimatedSize,
979 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
980 BrowserSettings.getInstance().getWebStorageSizeManager()
981 .onExceededDatabaseQuota(url, databaseIdentifier,
982 currentQuota, estimatedSize, totalUsedQuota,
983 quotaUpdater);
984 }
985
986 /**
987 * The Application Cache has exceeded its max size.
988 * @param spaceNeeded is the amount of disk space that would be needed
989 * in order for the last appcache operation to succeed.
990 * @param totalUsedQuota is the sum of all origins' quota.
991 * @param quotaUpdater A callback to inform the WebCore thread that a
992 * new app cache size is available. This callback must always
993 * be executed at some point to ensure that the sleeping
994 * WebCore thread is woken up.
995 */
996 @Override
997 public void onReachedMaxAppCacheSize(long spaceNeeded,
998 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
999 BrowserSettings.getInstance().getWebStorageSizeManager()
1000 .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota,
1001 quotaUpdater);
1002 }
1003
1004 /**
1005 * Instructs the browser to show a prompt to ask the user to set the
1006 * Geolocation permission state for the specified origin.
1007 * @param origin The origin for which Geolocation permissions are
1008 * requested.
1009 * @param callback The callback to call once the user has set the
1010 * Geolocation permission state.
1011 */
1012 @Override
1013 public void onGeolocationPermissionsShowPrompt(String origin,
1014 GeolocationPermissions.Callback callback) {
1015 if (mInForeground) {
Grace Kloba50c241e2010-04-20 11:07:50 -07001016 getGeolocationPermissionsPrompt().show(origin, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001017 }
1018 }
1019
1020 /**
1021 * Instructs the browser to hide the Geolocation permissions prompt.
1022 */
1023 @Override
1024 public void onGeolocationPermissionsHidePrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001025 if (mInForeground && mGeolocationPermissionsPrompt != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001026 mGeolocationPermissionsPrompt.hide();
1027 }
1028 }
1029
Ben Murdoch65acc352009-11-19 18:16:04 +00001030 /* Adds a JavaScript error message to the system log and if the JS
1031 * console is enabled in the about:debug options, to that console
1032 * also.
Ben Murdochc42addf2010-01-28 15:19:59 +00001033 * @param consoleMessage the message object.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001034 */
1035 @Override
Ben Murdochc42addf2010-01-28 15:19:59 +00001036 public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001037 if (mInForeground) {
1038 // call getErrorConsole(true) so it will create one if needed
1039 ErrorConsoleView errorConsole = getErrorConsole(true);
Ben Murdochc42addf2010-01-28 15:19:59 +00001040 errorConsole.addErrorMessage(consoleMessage);
Michael Kolb8233fac2010-10-26 16:08:53 -07001041 if (mWebViewController.shouldShowErrorConsole()
1042 && errorConsole.getShowState() !=
1043 ErrorConsoleView.SHOW_MAXIMIZED) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001044 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
1045 }
1046 }
Ben Murdochc42addf2010-01-28 15:19:59 +00001047
Jeff Hamilton47654f42010-09-07 09:57:51 -05001048 // Don't log console messages in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001049 if (isPrivateBrowsingEnabled()) return true;
Jeff Hamilton47654f42010-09-07 09:57:51 -05001050
Ben Murdochc42addf2010-01-28 15:19:59 +00001051 String message = "Console: " + consoleMessage.message() + " "
1052 + consoleMessage.sourceId() + ":"
1053 + consoleMessage.lineNumber();
1054
1055 switch (consoleMessage.messageLevel()) {
1056 case TIP:
1057 Log.v(CONSOLE_LOGTAG, message);
1058 break;
1059 case LOG:
1060 Log.i(CONSOLE_LOGTAG, message);
1061 break;
1062 case WARNING:
1063 Log.w(CONSOLE_LOGTAG, message);
1064 break;
1065 case ERROR:
1066 Log.e(CONSOLE_LOGTAG, message);
1067 break;
1068 case DEBUG:
1069 Log.d(CONSOLE_LOGTAG, message);
1070 break;
1071 }
1072
1073 return true;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001074 }
1075
1076 /**
1077 * Ask the browser for an icon to represent a <video> element.
1078 * This icon will be used if the Web page did not specify a poster attribute.
1079 * @return Bitmap The icon or null if no such icon is available.
1080 */
1081 @Override
1082 public Bitmap getDefaultVideoPoster() {
1083 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001084 return mWebViewController.getDefaultVideoPoster();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001085 }
1086 return null;
1087 }
1088
1089 /**
1090 * Ask the host application for a custom progress view to show while
1091 * a <video> is loading.
1092 * @return View The progress view.
1093 */
1094 @Override
1095 public View getVideoLoadingProgressView() {
1096 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001097 return mWebViewController.getVideoLoadingProgressView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001098 }
1099 return null;
1100 }
1101
1102 @Override
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01001103 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001104 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001105 mWebViewController.openFileChooser(uploadMsg, acceptType);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001106 } else {
1107 uploadMsg.onReceiveValue(null);
1108 }
1109 }
1110
1111 /**
1112 * Deliver a list of already-visited URLs
1113 */
1114 @Override
1115 public void getVisitedHistory(final ValueCallback<String[]> callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001116 mWebViewController.getVisitedHistory(callback);
1117 }
Ben Murdoch8029a772010-11-16 11:58:21 +00001118
1119 @Override
1120 public void setupAutoFill(Message message) {
1121 // Prompt the user to set up their profile.
1122 final Message msg = message;
1123 AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
1124 builder.setMessage(R.string.autofill_setup_dialog_message)
1125 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1126 @Override
1127 public void onClick(DialogInterface dialog, int id) {
1128 // Take user to the AutoFill profile editor. When they return,
1129 // we will send the message that we pass here which will trigger
1130 // the form to get filled out with their new profile.
1131 mWebViewController.setupAutoFill(msg);
1132 }
1133 })
1134 .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
1135 @Override
1136 public void onClick(DialogInterface dialog, int id) {
1137 // Disable autofill and show a toast with how to turn it on again.
1138 BrowserSettings s = BrowserSettings.getInstance();
1139 s.addObserver(mMainView.getSettings());
1140 s.disableAutoFill(mActivity);
1141 s.update();
1142 Toast.makeText(mActivity, R.string.autofill_setup_dialog_negative_toast,
1143 Toast.LENGTH_LONG).show();
1144 }
1145 }).show();
1146 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001147 };
1148
1149 // -------------------------------------------------------------------------
1150 // WebViewClient implementation for the sub window
1151 // -------------------------------------------------------------------------
1152
1153 // Subclass of WebViewClient used in subwindows to notify the main
1154 // WebViewClient of certain WebView activities.
1155 private static class SubWindowClient extends WebViewClient {
1156 // The main WebViewClient.
1157 private final WebViewClient mClient;
Michael Kolb8233fac2010-10-26 16:08:53 -07001158 private final WebViewController mController;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001159
Michael Kolb8233fac2010-10-26 16:08:53 -07001160 SubWindowClient(WebViewClient client, WebViewController controller) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001161 mClient = client;
Michael Kolb8233fac2010-10-26 16:08:53 -07001162 mController = controller;
Leon Scroggins III211ba542010-04-19 13:21:13 -04001163 }
1164 @Override
1165 public void onPageStarted(WebView view, String url, Bitmap favicon) {
1166 // Unlike the others, do not call mClient's version, which would
1167 // change the progress bar. However, we do want to remove the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001168 // find or select dialog.
Michael Kolb8233fac2010-10-26 16:08:53 -07001169 mController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001170 }
1171 @Override
1172 public void doUpdateVisitedHistory(WebView view, String url,
1173 boolean isReload) {
1174 mClient.doUpdateVisitedHistory(view, url, isReload);
1175 }
1176 @Override
1177 public boolean shouldOverrideUrlLoading(WebView view, String url) {
1178 return mClient.shouldOverrideUrlLoading(view, url);
1179 }
1180 @Override
1181 public void onReceivedSslError(WebView view, SslErrorHandler handler,
1182 SslError error) {
1183 mClient.onReceivedSslError(view, handler, error);
1184 }
1185 @Override
1186 public void onReceivedHttpAuthRequest(WebView view,
1187 HttpAuthHandler handler, String host, String realm) {
1188 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
1189 }
1190 @Override
1191 public void onFormResubmission(WebView view, Message dontResend,
1192 Message resend) {
1193 mClient.onFormResubmission(view, dontResend, resend);
1194 }
1195 @Override
1196 public void onReceivedError(WebView view, int errorCode,
1197 String description, String failingUrl) {
1198 mClient.onReceivedError(view, errorCode, description, failingUrl);
1199 }
1200 @Override
1201 public boolean shouldOverrideKeyEvent(WebView view,
1202 android.view.KeyEvent event) {
1203 return mClient.shouldOverrideKeyEvent(view, event);
1204 }
1205 @Override
1206 public void onUnhandledKeyEvent(WebView view,
1207 android.view.KeyEvent event) {
1208 mClient.onUnhandledKeyEvent(view, event);
1209 }
1210 }
1211
1212 // -------------------------------------------------------------------------
1213 // WebChromeClient implementation for the sub window
1214 // -------------------------------------------------------------------------
1215
1216 private class SubWindowChromeClient extends WebChromeClient {
1217 // The main WebChromeClient.
1218 private final WebChromeClient mClient;
1219
1220 SubWindowChromeClient(WebChromeClient client) {
1221 mClient = client;
1222 }
1223 @Override
1224 public void onProgressChanged(WebView view, int newProgress) {
1225 mClient.onProgressChanged(view, newProgress);
1226 }
1227 @Override
1228 public boolean onCreateWindow(WebView view, boolean dialog,
1229 boolean userGesture, android.os.Message resultMsg) {
1230 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
1231 }
1232 @Override
1233 public void onCloseWindow(WebView window) {
1234 if (window != mSubView) {
1235 Log.e(LOGTAG, "Can't close the window");
1236 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001237 mWebViewController.dismissSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001238 }
1239 }
1240
1241 // -------------------------------------------------------------------------
1242
Michael Kolb8233fac2010-10-26 16:08:53 -07001243 // TODO temporarily use activity here
1244 // remove later
1245
Grace Kloba22ac16e2009-10-07 18:00:23 -07001246 // Construct a new tab
Michael Kolb8233fac2010-10-26 16:08:53 -07001247 Tab(WebViewController wvcontroller, WebView w, boolean closeOnExit, String appId,
Grace Kloba22ac16e2009-10-07 18:00:23 -07001248 String url) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001249 mWebViewController = wvcontroller;
1250 mActivity = mWebViewController.getActivity();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001251 mCloseOnExit = closeOnExit;
1252 mAppId = appId;
John Recke969cc52010-12-21 17:24:43 -08001253 mDataController = DataController.getInstance(mActivity);
John Reck30c714c2010-12-16 17:30:34 -08001254 mCurrentState = new PageState(mActivity, w.isPrivateBrowsingEnabled());
Michael Kolb8233fac2010-10-26 16:08:53 -07001255 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001256 mInForeground = false;
1257
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001258 mDownloadListener = new DownloadListener() {
1259 public void onDownloadStart(String url, String userAgent,
1260 String contentDisposition, String mimetype,
1261 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001262 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001263 mimetype, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001264 }
1265 };
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001266 mWebBackForwardListClient = new WebBackForwardListClient() {
1267 @Override
1268 public void onNewHistoryItem(WebHistoryItem item) {
1269 if (isInVoiceSearchMode()) {
1270 item.setCustomData(mVoiceSearchData.mVoiceSearchIntent);
1271 }
1272 }
1273 @Override
1274 public void onIndexChanged(WebHistoryItem item, int index) {
1275 Object data = item.getCustomData();
1276 if (data != null && data instanceof Intent) {
1277 activateVoiceSearchMode((Intent) data);
1278 }
1279 }
1280 };
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001281
Grace Kloba22ac16e2009-10-07 18:00:23 -07001282 setWebView(w);
1283 }
1284
1285 /**
1286 * Sets the WebView for this tab, correctly removing the old WebView from
1287 * the container view.
1288 */
1289 void setWebView(WebView w) {
1290 if (mMainView == w) {
1291 return;
1292 }
Michael Kolba713ec82010-11-29 17:27:06 -08001293
Grace Kloba22ac16e2009-10-07 18:00:23 -07001294 // If the WebView is changing, the page will be reloaded, so any ongoing
1295 // Geolocation permission requests are void.
Grace Kloba50c241e2010-04-20 11:07:50 -07001296 if (mGeolocationPermissionsPrompt != null) {
1297 mGeolocationPermissionsPrompt.hide();
1298 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001299
Michael Kolba713ec82010-11-29 17:27:06 -08001300 mWebViewController.onSetWebView(this, w);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001301
1302 // set the new one
1303 mMainView = w;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001304 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001305 if (mMainView != null) {
1306 mMainView.setWebViewClient(mWebViewClient);
1307 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001308 // Attach DownloadManager so that downloads can start in an active
1309 // or a non-active window. This can happen when going to a site that
1310 // does a redirect after a period of time. The user could have
1311 // switched to another tab while waiting for the download to start.
1312 mMainView.setDownloadListener(mDownloadListener);
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001313 mMainView.setWebBackForwardListClient(mWebBackForwardListClient);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001314 }
1315 }
1316
1317 /**
1318 * Destroy the tab's main WebView and subWindow if any
1319 */
1320 void destroy() {
1321 if (mMainView != null) {
1322 dismissSubWindow();
1323 BrowserSettings.getInstance().deleteObserver(mMainView.getSettings());
1324 // save the WebView to call destroy() after detach it from the tab
1325 WebView webView = mMainView;
1326 setWebView(null);
1327 webView.destroy();
1328 }
1329 }
1330
1331 /**
1332 * Remove the tab from the parent
1333 */
1334 void removeFromTree() {
1335 // detach the children
1336 if (mChildTabs != null) {
1337 for(Tab t : mChildTabs) {
1338 t.setParentTab(null);
1339 }
1340 }
1341 // remove itself from the parent list
1342 if (mParentTab != null) {
1343 mParentTab.mChildTabs.remove(this);
1344 }
1345 }
1346
1347 /**
1348 * Create a new subwindow unless a subwindow already exists.
1349 * @return True if a new subwindow was created. False if one already exists.
1350 */
1351 boolean createSubWindow() {
1352 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001353 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001354 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001355 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001356 mSubView.setWebChromeClient(new SubWindowChromeClient(
1357 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001358 // Set a different DownloadListener for the mSubView, since it will
1359 // just need to dismiss the mSubView, rather than close the Tab
1360 mSubView.setDownloadListener(new DownloadListener() {
1361 public void onDownloadStart(String url, String userAgent,
1362 String contentDisposition, String mimetype,
1363 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001364 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001365 contentDisposition, mimetype, contentLength);
1366 if (mSubView.copyBackForwardList().getSize() == 0) {
1367 // This subwindow was opened for the sole purpose of
1368 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001369 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001370 }
1371 }
1372 });
Grace Kloba22ac16e2009-10-07 18:00:23 -07001373 mSubView.setOnCreateContextMenuListener(mActivity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001374 return true;
1375 }
1376 return false;
1377 }
1378
1379 /**
1380 * Dismiss the subWindow for the tab.
1381 */
1382 void dismissSubWindow() {
1383 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001384 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001385 BrowserSettings.getInstance().deleteObserver(
1386 mSubView.getSettings());
1387 mSubView.destroy();
1388 mSubView = null;
1389 mSubViewContainer = null;
1390 }
1391 }
1392
Grace Kloba22ac16e2009-10-07 18:00:23 -07001393
1394 /**
1395 * Set the parent tab of this tab.
1396 */
1397 void setParentTab(Tab parent) {
1398 mParentTab = parent;
1399 // This tab may have been freed due to low memory. If that is the case,
1400 // the parent tab index is already saved. If we are changing that index
1401 // (most likely due to removing the parent tab) we must update the
1402 // parent tab index in the saved Bundle.
1403 if (mSavedState != null) {
1404 if (parent == null) {
1405 mSavedState.remove(PARENTTAB);
1406 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -07001407 mSavedState.putInt(PARENTTAB, mWebViewController.getTabControl()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001408 .getTabIndex(parent));
1409 }
1410 }
1411 }
1412
1413 /**
1414 * When a Tab is created through the content of another Tab, then we
1415 * associate the Tabs.
1416 * @param child the Tab that was created from this Tab
1417 */
1418 void addChildTab(Tab child) {
1419 if (mChildTabs == null) {
1420 mChildTabs = new Vector<Tab>();
1421 }
1422 mChildTabs.add(child);
1423 child.setParentTab(this);
1424 }
1425
1426 Vector<Tab> getChildTabs() {
1427 return mChildTabs;
1428 }
1429
1430 void resume() {
1431 if (mMainView != null) {
1432 mMainView.onResume();
1433 if (mSubView != null) {
1434 mSubView.onResume();
1435 }
1436 }
1437 }
1438
1439 void pause() {
1440 if (mMainView != null) {
1441 mMainView.onPause();
1442 if (mSubView != null) {
1443 mSubView.onPause();
1444 }
1445 }
1446 }
1447
1448 void putInForeground() {
1449 mInForeground = true;
1450 resume();
1451 mMainView.setOnCreateContextMenuListener(mActivity);
1452 if (mSubView != null) {
1453 mSubView.setOnCreateContextMenuListener(mActivity);
1454 }
1455 // Show the pending error dialog if the queue is not empty
1456 if (mQueuedErrors != null && mQueuedErrors.size() > 0) {
1457 showError(mQueuedErrors.getFirst());
1458 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001459 mWebViewController.bookmarkedStatusHasChanged(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001460 }
1461
1462 void putInBackground() {
1463 mInForeground = false;
1464 pause();
1465 mMainView.setOnCreateContextMenuListener(null);
1466 if (mSubView != null) {
1467 mSubView.setOnCreateContextMenuListener(null);
1468 }
1469 }
1470
Michael Kolb8233fac2010-10-26 16:08:53 -07001471 boolean inForeground() {
1472 return mInForeground;
1473 }
1474
Grace Kloba22ac16e2009-10-07 18:00:23 -07001475 /**
1476 * Return the top window of this tab; either the subwindow if it is not
1477 * null or the main window.
1478 * @return The top window of this tab.
1479 */
1480 WebView getTopWindow() {
1481 if (mSubView != null) {
1482 return mSubView;
1483 }
1484 return mMainView;
1485 }
1486
1487 /**
1488 * Return the main window of this tab. Note: if a tab is freed in the
1489 * background, this can return null. It is only guaranteed to be
1490 * non-null for the current tab.
1491 * @return The main WebView of this tab.
1492 */
1493 WebView getWebView() {
1494 return mMainView;
1495 }
1496
Michael Kolba713ec82010-11-29 17:27:06 -08001497 void setViewContainer(View container) {
1498 mContainer = container;
1499 }
1500
Michael Kolb8233fac2010-10-26 16:08:53 -07001501 View getViewContainer() {
1502 return mContainer;
1503 }
1504
Grace Kloba22ac16e2009-10-07 18:00:23 -07001505 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001506 * Return whether private browsing is enabled for the main window of
1507 * this tab.
1508 * @return True if private browsing is enabled.
1509 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001510 boolean isPrivateBrowsingEnabled() {
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001511 WebView webView = getWebView();
1512 if (webView == null) {
1513 return false;
1514 }
1515 return webView.isPrivateBrowsingEnabled();
1516 }
1517
1518 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001519 * Return the subwindow of this tab or null if there is no subwindow.
1520 * @return The subwindow of this tab or null.
1521 */
1522 WebView getSubWebView() {
1523 return mSubView;
1524 }
1525
Michael Kolb1514bb72010-11-22 09:11:48 -08001526 void setSubWebView(WebView subView) {
1527 mSubView = subView;
1528 }
1529
Michael Kolb8233fac2010-10-26 16:08:53 -07001530 View getSubViewContainer() {
1531 return mSubViewContainer;
1532 }
1533
Michael Kolb1514bb72010-11-22 09:11:48 -08001534 void setSubViewContainer(View subViewContainer) {
1535 mSubViewContainer = subViewContainer;
1536 }
1537
Grace Kloba22ac16e2009-10-07 18:00:23 -07001538 /**
1539 * @return The geolocation permissions prompt for this tab.
1540 */
1541 GeolocationPermissionsPrompt getGeolocationPermissionsPrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001542 if (mGeolocationPermissionsPrompt == null) {
1543 ViewStub stub = (ViewStub) mContainer
1544 .findViewById(R.id.geolocation_permissions_prompt);
1545 mGeolocationPermissionsPrompt = (GeolocationPermissionsPrompt) stub
1546 .inflate();
1547 mGeolocationPermissionsPrompt.init();
1548 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001549 return mGeolocationPermissionsPrompt;
1550 }
1551
1552 /**
1553 * @return The application id string
1554 */
1555 String getAppId() {
1556 return mAppId;
1557 }
1558
1559 /**
1560 * Set the application id string
1561 * @param id
1562 */
1563 void setAppId(String id) {
1564 mAppId = id;
1565 }
1566
Grace Kloba22ac16e2009-10-07 18:00:23 -07001567 String getUrl() {
John Reck30c714c2010-12-16 17:30:34 -08001568 return mCurrentState.mUrl;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001569 }
1570
1571 /**
John Reck30c714c2010-12-16 17:30:34 -08001572 * Get the title of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001573 */
1574 String getTitle() {
John Reck30c714c2010-12-16 17:30:34 -08001575 if (mCurrentState.mTitle == null && mInPageLoad) {
1576 return mActivity.getString(R.string.title_bar_loading);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001577 }
John Reck30c714c2010-12-16 17:30:34 -08001578 return mCurrentState.mTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001579 }
1580
1581 /**
John Reck30c714c2010-12-16 17:30:34 -08001582 * Get the favicon of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001583 */
1584 Bitmap getFavicon() {
John Reck30c714c2010-12-16 17:30:34 -08001585 return mCurrentState.mFavicon;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001586 }
1587
John Recke969cc52010-12-21 17:24:43 -08001588 public boolean isBookmarkedSite() {
1589 return mCurrentState.mIsBookmarkedSite;
1590 }
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001591
Grace Kloba22ac16e2009-10-07 18:00:23 -07001592 /**
1593 * Return the tab's error console. Creates the console if createIfNEcessary
1594 * is true and we haven't already created the console.
1595 * @param createIfNecessary Flag to indicate if the console should be
1596 * created if it has not been already.
1597 * @return The tab's error console, or null if one has not been created and
1598 * createIfNecessary is false.
1599 */
1600 ErrorConsoleView getErrorConsole(boolean createIfNecessary) {
1601 if (createIfNecessary && mErrorConsole == null) {
1602 mErrorConsole = new ErrorConsoleView(mActivity);
1603 mErrorConsole.setWebView(mMainView);
1604 }
1605 return mErrorConsole;
1606 }
1607
1608 /**
1609 * If this Tab was created through another Tab, then this method returns
1610 * that Tab.
1611 * @return the Tab parent or null
1612 */
1613 public Tab getParentTab() {
1614 return mParentTab;
1615 }
1616
1617 /**
1618 * Return whether this tab should be closed when it is backing out of the
1619 * first page.
1620 * @return TRUE if this tab should be closed when exit.
1621 */
1622 boolean closeOnExit() {
1623 return mCloseOnExit;
1624 }
1625
John Reck30c714c2010-12-16 17:30:34 -08001626 private void setLockIconType(LockIcon icon) {
1627 mCurrentState.mLockIcon = icon;
1628 mWebViewController.onUpdatedLockIcon(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001629 }
1630
1631 /**
1632 * @return The tab's lock icon type.
1633 */
John Reck30c714c2010-12-16 17:30:34 -08001634 LockIcon getLockIconType() {
1635 return mCurrentState.mLockIcon;
1636 }
1637
1638 int getLoadProgress() {
1639 if (mInPageLoad) {
1640 return mPageLoadProgress;
1641 }
1642 return 100;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001643 }
1644
1645 /**
1646 * @return TRUE if onPageStarted is called while onPageFinished is not
1647 * called yet.
1648 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001649 boolean inPageLoad() {
1650 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001651 }
1652
1653 // force mInLoad to be false. This should only be called before closing the
1654 // tab to ensure BrowserActivity's pauseWebViewTimers() is called correctly.
Michael Kolb8233fac2010-10-26 16:08:53 -07001655 void clearInPageLoad() {
1656 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001657 }
1658
Grace Kloba22ac16e2009-10-07 18:00:23 -07001659 /**
John Reck30c714c2010-12-16 17:30:34 -08001660 * Get the cached saved state bundle.
1661 * @return cached state bundle
Grace Kloba22ac16e2009-10-07 18:00:23 -07001662 */
1663 Bundle getSavedState() {
1664 return mSavedState;
1665 }
1666
1667 /**
1668 * Set the saved state.
1669 */
1670 void setSavedState(Bundle state) {
1671 mSavedState = state;
1672 }
1673
1674 /**
1675 * @return TRUE if succeed in saving the state.
1676 */
1677 boolean saveState() {
1678 // If the WebView is null it means we ran low on memory and we already
1679 // stored the saved state in mSavedState.
1680 if (mMainView == null) {
1681 return mSavedState != null;
1682 }
1683
1684 mSavedState = new Bundle();
1685 final WebBackForwardList list = mMainView.saveState(mSavedState);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001686
1687 // Store some extra info for displaying the tab in the picker.
1688 final WebHistoryItem item = list != null ? list.getCurrentItem() : null;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001689
John Reck30c714c2010-12-16 17:30:34 -08001690 mSavedState.putString(CURRURL, mCurrentState.mUrl);
1691 mSavedState.putString(CURRTITLE, mCurrentState.mTitle);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001692 mSavedState.putBoolean(CLOSEONEXIT, mCloseOnExit);
1693 if (mAppId != null) {
1694 mSavedState.putString(APPID, mAppId);
1695 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001696 // Remember the parent tab so the relationship can be restored.
1697 if (mParentTab != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001698 mSavedState.putInt(PARENTTAB, mWebViewController.getTabControl().getTabIndex(
Grace Kloba22ac16e2009-10-07 18:00:23 -07001699 mParentTab));
1700 }
1701 return true;
1702 }
1703
1704 /*
1705 * Restore the state of the tab.
1706 */
1707 boolean restoreState(Bundle b) {
1708 if (b == null) {
1709 return false;
1710 }
1711 // Restore the internal state even if the WebView fails to restore.
1712 // This will maintain the app id, original url and close-on-exit values.
1713 mSavedState = null;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001714 mCloseOnExit = b.getBoolean(CLOSEONEXIT);
1715 mAppId = b.getString(APPID);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001716
1717 final WebBackForwardList list = mMainView.restoreState(b);
1718 if (list == null) {
1719 return false;
1720 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001721 return true;
1722 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04001723
Leon Scroggins1961ed22010-12-07 15:22:21 -05001724 public void updateBookmarkedStatus() {
John Recke969cc52010-12-21 17:24:43 -08001725 mDataController.queryBookmarkStatus(getUrl(), mIsBookmarkCallback);
Leon Scroggins1961ed22010-12-07 15:22:21 -05001726 }
1727
John Recke969cc52010-12-21 17:24:43 -08001728 private DataController.OnQueryUrlIsBookmark mIsBookmarkCallback
1729 = new DataController.OnQueryUrlIsBookmark() {
1730 @Override
1731 public void onQueryUrlIsBookmark(String url, boolean isBookmark) {
1732 if (mCurrentState.mUrl.equals(url)) {
1733 mCurrentState.mIsBookmarkedSite = isBookmark;
1734 mWebViewController.bookmarkedStatusHasChanged(Tab.this);
1735 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001736 }
John Recke969cc52010-12-21 17:24:43 -08001737 };
Grace Kloba22ac16e2009-10-07 18:00:23 -07001738}