blob: 0347ef5960ea54722368de600a03619656b735e8 [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;
Ben Murdoch1d676b62011-01-17 12:54:24 +000056import android.widget.CheckBox;
Grace Kloba22ac16e2009-10-07 18:00:23 -070057import android.widget.LinearLayout;
58import android.widget.TextView;
Ben Murdoch8029a772010-11-16 11:58:21 +000059import android.widget.Toast;
Grace Kloba22ac16e2009-10-07 18:00:23 -070060
Michael Kolbfe251992010-07-08 15:41:55 -070061import java.util.ArrayList;
62import java.util.HashMap;
63import java.util.Iterator;
64import java.util.LinkedList;
65import java.util.Map;
66import java.util.Vector;
67
Grace Kloba22ac16e2009-10-07 18:00:23 -070068/**
69 * Class for maintaining Tabs with a main WebView and a subwindow.
70 */
71class Tab {
Michael Kolb8233fac2010-10-26 16:08:53 -070072
Grace Kloba22ac16e2009-10-07 18:00:23 -070073 // Log Tag
74 private static final String LOGTAG = "Tab";
Ben Murdochc42addf2010-01-28 15:19:59 +000075 // Special case the logtag for messages for the Console to make it easier to
76 // filter them and match the logtag used for these messages in older versions
77 // of the browser.
78 private static final String CONSOLE_LOGTAG = "browser";
79
John Reck30c714c2010-12-16 17:30:34 -080080 public enum LockIcon {
81 LOCK_ICON_UNSECURE,
82 LOCK_ICON_SECURE,
83 LOCK_ICON_MIXED,
84 }
Michael Kolb8233fac2010-10-26 16:08:53 -070085
86 Activity mActivity;
87 private WebViewController mWebViewController;
88
Grace Kloba22ac16e2009-10-07 18:00:23 -070089 // The Geolocation permissions prompt
90 private GeolocationPermissionsPrompt mGeolocationPermissionsPrompt;
91 // Main WebView wrapper
Michael Kolba713ec82010-11-29 17:27:06 -080092 private View mContainer;
Grace Kloba22ac16e2009-10-07 18:00:23 -070093 // Main WebView
94 private WebView mMainView;
95 // Subwindow container
96 private View mSubViewContainer;
97 // Subwindow WebView
98 private WebView mSubView;
99 // Saved bundle for when we are running low on memory. It contains the
100 // information needed to restore the WebView if the user goes back to the
101 // tab.
102 private Bundle mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700103 // Parent Tab. This is the Tab that created this Tab, or null if the Tab was
104 // created by the UI
105 private Tab mParentTab;
106 // Tab that constructed by this Tab. This is used when this Tab is
107 // destroyed, it clears all mParentTab values in the children.
108 private Vector<Tab> mChildTabs;
109 // If true, the tab will be removed when back out of the first page.
110 private boolean mCloseOnExit;
111 // If true, the tab is in the foreground of the current activity.
112 private boolean mInForeground;
Michael Kolb8233fac2010-10-26 16:08:53 -0700113 // If true, the tab is in page loading state (after onPageStarted,
114 // before onPageFinsihed)
115 private boolean mInPageLoad;
John Reck30c714c2010-12-16 17:30:34 -0800116 // The last reported progress of the current page
117 private int mPageLoadProgress;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000118 // The time the load started, used to find load page time
119 private long mLoadStartTime;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700120 // Application identifier used to find tabs that another application wants
121 // to reuse.
122 private String mAppId;
123 // Keep the original url around to avoid killing the old WebView if the url
124 // has not changed.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700125 // Error console for the tab
126 private ErrorConsoleView mErrorConsole;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -0500127 // The listener that gets invoked when a download is started from the
128 // mMainView
129 private final DownloadListener mDownloadListener;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500130 // Listener used to know when we move forward or back in the history list.
131 private final WebBackForwardListClient mWebBackForwardListClient;
John Recke969cc52010-12-21 17:24:43 -0800132 private DataController mDataController;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700133
134 // AsyncTask for downloading touch icons
135 DownloadTouchIcon mTouchIconLoader;
136
John Reck30c714c2010-12-16 17:30:34 -0800137 // All the state needed for a page
138 private static class PageState {
139 String mUrl;
140 String mTitle;
141 LockIcon mLockIcon;
142 Bitmap mFavicon;
John Recke969cc52010-12-21 17:24:43 -0800143 Boolean mIsBookmarkedSite = false;
John Reck30c714c2010-12-16 17:30:34 -0800144
145 PageState(Context c, boolean incognito) {
146 if (incognito) {
147 mUrl = "browser:incognito";
148 mTitle = c.getString(R.string.new_incognito_tab);
149 mFavicon = BitmapFactory.decodeResource(
150 c.getResources(), R.drawable.fav_incognito);
151 } else {
152 mUrl = "";
153 mTitle = c.getString(R.string.new_tab);
154 mFavicon = BitmapFactory.decodeResource(
155 c.getResources(), R.drawable.app_web_browser_sm);
156 }
157 mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
158 }
159
160 PageState(Context c, boolean incognito, String url, Bitmap favicon) {
161 mUrl = url;
162 mTitle = null;
163 if (URLUtil.isHttpsUrl(url)) {
164 mLockIcon = LockIcon.LOCK_ICON_SECURE;
165 } else {
166 mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
167 }
168 if (favicon != null) {
169 mFavicon = favicon;
170 } else {
171 if (incognito) {
172 mFavicon = BitmapFactory.decodeResource(
173 c.getResources(), R.drawable.fav_incognito);
174 } else {
175 mFavicon = BitmapFactory.decodeResource(
176 c.getResources(), R.drawable.app_web_browser_sm);
177 }
178 }
179 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700180 }
181
John Reck30c714c2010-12-16 17:30:34 -0800182 // The current/loading page's state
183 private PageState mCurrentState;
184
Grace Kloba22ac16e2009-10-07 18:00:23 -0700185 // Used for saving and restoring each Tab
John Reck30c714c2010-12-16 17:30:34 -0800186 // TODO: Figure out who uses what and where
187 // Some of these aren't use in this class, and some are only used in
188 // restoring state but not saving it - FIX THIS
Grace Kloba22ac16e2009-10-07 18:00:23 -0700189 static final String WEBVIEW = "webview";
190 static final String NUMTABS = "numTabs";
191 static final String CURRTAB = "currentTab";
192 static final String CURRURL = "currentUrl";
193 static final String CURRTITLE = "currentTitle";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700194 static final String CLOSEONEXIT = "closeonexit";
195 static final String PARENTTAB = "parentTab";
196 static final String APPID = "appid";
197 static final String ORIGINALURL = "originalUrl";
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700198 static final String INCOGNITO = "privateBrowsingEnabled";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700199
200 // -------------------------------------------------------------------------
201
Leon Scroggins58d56c62010-01-28 15:12:40 -0500202 /**
203 * Private information regarding the latest voice search. If the Tab is not
204 * in voice search mode, this will be null.
205 */
206 private VoiceSearchData mVoiceSearchData;
207 /**
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400208 * Remove voice search mode from this tab.
209 */
210 public void revertVoiceSearchMode() {
211 if (mVoiceSearchData != null) {
212 mVoiceSearchData = null;
213 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700214 mWebViewController.revertVoiceSearchMode(this);
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400215 }
216 }
217 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700218
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400219 /**
Leon Scroggins58d56c62010-01-28 15:12:40 -0500220 * Return whether the tab is in voice search mode.
221 */
222 public boolean isInVoiceSearchMode() {
223 return mVoiceSearchData != null;
224 }
225 /**
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400226 * Return true if the Tab is in voice search mode and the voice search
227 * Intent came with a String identifying that Google provided the Intent.
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500228 */
229 public boolean voiceSearchSourceIsGoogle() {
230 return mVoiceSearchData != null && mVoiceSearchData.mSourceIsGoogle;
231 }
232 /**
Leon Scroggins58d56c62010-01-28 15:12:40 -0500233 * Get the title to display for the current voice search page. If the Tab
234 * is not in voice search mode, return null.
235 */
236 public String getVoiceDisplayTitle() {
237 if (mVoiceSearchData == null) return null;
238 return mVoiceSearchData.mLastVoiceSearchTitle;
239 }
240 /**
241 * Get the latest array of voice search results, to be passed to the
242 * BrowserProvider. If the Tab is not in voice search mode, return null.
243 */
244 public ArrayList<String> getVoiceSearchResults() {
245 if (mVoiceSearchData == null) return null;
246 return mVoiceSearchData.mVoiceSearchResults;
247 }
248 /**
249 * Activate voice search mode.
250 * @param intent Intent which has the results to use, or an index into the
251 * results when reusing the old results.
252 */
253 /* package */ void activateVoiceSearchMode(Intent intent) {
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500254 int index = 0;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500255 ArrayList<String> results = intent.getStringArrayListExtra(
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -0500256 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_STRINGS);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500257 if (results != null) {
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500258 ArrayList<String> urls = intent.getStringArrayListExtra(
259 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_URLS);
260 ArrayList<String> htmls = intent.getStringArrayListExtra(
261 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_HTML);
262 ArrayList<String> baseUrls = intent.getStringArrayListExtra(
263 RecognizerResultsIntent
264 .EXTRA_VOICE_SEARCH_RESULT_HTML_BASE_URLS);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500265 // This tab is now entering voice search mode for the first time, or
266 // a new voice search was done.
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500267 int size = results.size();
268 if (urls == null || size != urls.size()) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500269 throw new AssertionError("improper extras passed in Intent");
270 }
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500271 if (htmls == null || htmls.size() != size || baseUrls == null ||
272 (baseUrls.size() != size && baseUrls.size() != 1)) {
273 // If either of these arrays are empty/incorrectly sized, ignore
274 // them.
275 htmls = null;
276 baseUrls = null;
277 }
278 mVoiceSearchData = new VoiceSearchData(results, urls, htmls,
279 baseUrls);
Leon Scroggins9df94972010-03-08 18:20:35 -0500280 mVoiceSearchData.mHeaders = intent.getParcelableArrayListExtra(
281 RecognizerResultsIntent
282 .EXTRA_VOICE_SEARCH_RESULT_HTTP_HEADERS);
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500283 mVoiceSearchData.mSourceIsGoogle = intent.getBooleanExtra(
284 VoiceSearchData.SOURCE_IS_GOOGLE, false);
Leon Scroggins2ee4a5a2010-03-15 16:56:57 -0400285 mVoiceSearchData.mVoiceSearchIntent = new Intent(intent);
Leon Scrogginse10dde52010-03-08 19:53:03 -0500286 }
287 String extraData = intent.getStringExtra(
288 SearchManager.EXTRA_DATA_KEY);
289 if (extraData != null) {
290 index = Integer.parseInt(extraData);
291 if (index >= mVoiceSearchData.mVoiceSearchResults.size()) {
292 throw new AssertionError("index must be less than "
293 + "size of mVoiceSearchResults");
294 }
295 if (mVoiceSearchData.mSourceIsGoogle) {
296 Intent logIntent = new Intent(
297 LoggingEvents.ACTION_LOG_EVENT);
298 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
299 LoggingEvents.VoiceSearch.N_BEST_CHOOSE);
300 logIntent.putExtra(
301 LoggingEvents.VoiceSearch.EXTRA_N_BEST_CHOOSE_INDEX,
302 index);
303 mActivity.sendBroadcast(logIntent);
304 }
305 if (mVoiceSearchData.mVoiceSearchIntent != null) {
Leon Scroggins2ee4a5a2010-03-15 16:56:57 -0400306 // Copy the Intent, so that each history item will have its own
307 // Intent, with different (or none) extra data.
308 Intent latest = new Intent(mVoiceSearchData.mVoiceSearchIntent);
309 latest.putExtra(SearchManager.EXTRA_DATA_KEY, extraData);
310 mVoiceSearchData.mVoiceSearchIntent = latest;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500311 }
312 }
313 mVoiceSearchData.mLastVoiceSearchTitle
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500314 = mVoiceSearchData.mVoiceSearchResults.get(index);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500315 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700316 mWebViewController.activateVoiceSearchMode(mVoiceSearchData.mLastVoiceSearchTitle);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500317 }
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500318 if (mVoiceSearchData.mVoiceSearchHtmls != null) {
319 // When index was found it was already ensured that it was valid
320 String uriString = mVoiceSearchData.mVoiceSearchHtmls.get(index);
321 if (uriString != null) {
322 Uri dataUri = Uri.parse(uriString);
323 if (RecognizerResultsIntent.URI_SCHEME_INLINE.equals(
324 dataUri.getScheme())) {
325 // If there is only one base URL, use it. If there are
326 // more, there will be one for each index, so use the base
327 // URL corresponding to the index.
328 String baseUrl = mVoiceSearchData.mVoiceSearchBaseUrls.get(
329 mVoiceSearchData.mVoiceSearchBaseUrls.size() > 1 ?
330 index : 0);
331 mVoiceSearchData.mLastVoiceSearchUrl = baseUrl;
332 mMainView.loadDataWithBaseURL(baseUrl,
333 uriString.substring(RecognizerResultsIntent
334 .URI_SCHEME_INLINE.length() + 1), "text/html",
335 "utf-8", baseUrl);
336 return;
337 }
338 }
339 }
Leon Scroggins58d56c62010-01-28 15:12:40 -0500340 mVoiceSearchData.mLastVoiceSearchUrl
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500341 = mVoiceSearchData.mVoiceSearchUrls.get(index);
342 if (null == mVoiceSearchData.mLastVoiceSearchUrl) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700343 mVoiceSearchData.mLastVoiceSearchUrl = UrlUtils.smartUrlFilter(
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500344 mVoiceSearchData.mLastVoiceSearchTitle);
345 }
Leon Scroggins9df94972010-03-08 18:20:35 -0500346 Map<String, String> headers = null;
347 if (mVoiceSearchData.mHeaders != null) {
348 int bundleIndex = mVoiceSearchData.mHeaders.size() == 1 ? 0
349 : index;
350 Bundle bundle = mVoiceSearchData.mHeaders.get(bundleIndex);
351 if (bundle != null && !bundle.isEmpty()) {
352 Iterator<String> iter = bundle.keySet().iterator();
353 headers = new HashMap<String, String>();
354 while (iter.hasNext()) {
355 String key = iter.next();
356 headers.put(key, bundle.getString(key));
357 }
358 }
359 }
360 mMainView.loadUrl(mVoiceSearchData.mLastVoiceSearchUrl, headers);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500361 }
362 /* package */ static class VoiceSearchData {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500363 public VoiceSearchData(ArrayList<String> results,
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500364 ArrayList<String> urls, ArrayList<String> htmls,
365 ArrayList<String> baseUrls) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500366 mVoiceSearchResults = results;
367 mVoiceSearchUrls = urls;
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500368 mVoiceSearchHtmls = htmls;
369 mVoiceSearchBaseUrls = baseUrls;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500370 }
371 /*
372 * ArrayList of suggestions to be displayed when opening the
373 * SearchManager
374 */
375 public ArrayList<String> mVoiceSearchResults;
376 /*
377 * ArrayList of urls, associated with the suggestions in
378 * mVoiceSearchResults.
379 */
380 public ArrayList<String> mVoiceSearchUrls;
381 /*
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500382 * ArrayList holding content to load for each item in
383 * mVoiceSearchResults.
384 */
385 public ArrayList<String> mVoiceSearchHtmls;
386 /*
387 * ArrayList holding base urls for the items in mVoiceSearchResults.
388 * If non null, this will either have the same size as
389 * mVoiceSearchResults or have a size of 1, in which case all will use
390 * the same base url
391 */
392 public ArrayList<String> mVoiceSearchBaseUrls;
393 /*
Leon Scroggins58d56c62010-01-28 15:12:40 -0500394 * The last url provided by voice search. Used for comparison to see if
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500395 * we are going to a page by some method besides voice search.
Leon Scroggins58d56c62010-01-28 15:12:40 -0500396 */
397 public String mLastVoiceSearchUrl;
398 /**
399 * The last title used for voice search. Needed to update the title bar
400 * when switching tabs.
401 */
402 public String mLastVoiceSearchTitle;
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500403 /**
404 * Whether the Intent which turned on voice search mode contained the
405 * String signifying that Google was the source.
406 */
407 public boolean mSourceIsGoogle;
408 /**
Leon Scroggins9df94972010-03-08 18:20:35 -0500409 * List of headers to be passed into the WebView containing location
410 * information
411 */
412 public ArrayList<Bundle> mHeaders;
413 /**
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500414 * The Intent used to invoke voice search. Placed on the
415 * WebHistoryItem so that when coming back to a previous voice search
416 * page we can again activate voice search.
417 */
Leon Scrogginse10dde52010-03-08 19:53:03 -0500418 public Intent mVoiceSearchIntent;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500419 /**
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500420 * String used to identify Google as the source of voice search.
421 */
422 public static String SOURCE_IS_GOOGLE
423 = "android.speech.extras.SOURCE_IS_GOOGLE";
Leon Scroggins58d56c62010-01-28 15:12:40 -0500424 }
425
Grace Kloba22ac16e2009-10-07 18:00:23 -0700426 // Container class for the next error dialog that needs to be displayed
427 private class ErrorDialog {
428 public final int mTitle;
429 public final String mDescription;
430 public final int mError;
431 ErrorDialog(int title, String desc, int error) {
432 mTitle = title;
433 mDescription = desc;
434 mError = error;
435 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700436 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700437
438 private void processNextError() {
439 if (mQueuedErrors == null) {
440 return;
441 }
442 // The first one is currently displayed so just remove it.
443 mQueuedErrors.removeFirst();
444 if (mQueuedErrors.size() == 0) {
445 mQueuedErrors = null;
446 return;
447 }
448 showError(mQueuedErrors.getFirst());
449 }
450
451 private DialogInterface.OnDismissListener mDialogListener =
452 new DialogInterface.OnDismissListener() {
453 public void onDismiss(DialogInterface d) {
454 processNextError();
455 }
456 };
457 private LinkedList<ErrorDialog> mQueuedErrors;
458
459 private void queueError(int err, String desc) {
460 if (mQueuedErrors == null) {
461 mQueuedErrors = new LinkedList<ErrorDialog>();
462 }
463 for (ErrorDialog d : mQueuedErrors) {
464 if (d.mError == err) {
465 // Already saw a similar error, ignore the new one.
466 return;
467 }
468 }
469 ErrorDialog errDialog = new ErrorDialog(
470 err == WebViewClient.ERROR_FILE_NOT_FOUND ?
471 R.string.browserFrameFileErrorLabel :
472 R.string.browserFrameNetworkErrorLabel,
473 desc, err);
474 mQueuedErrors.addLast(errDialog);
475
476 // Show the dialog now if the queue was empty and it is in foreground
477 if (mQueuedErrors.size() == 1 && mInForeground) {
478 showError(errDialog);
479 }
480 }
481
482 private void showError(ErrorDialog errDialog) {
483 if (mInForeground) {
484 AlertDialog d = new AlertDialog.Builder(mActivity)
485 .setTitle(errDialog.mTitle)
486 .setMessage(errDialog.mDescription)
487 .setPositiveButton(R.string.ok, null)
488 .create();
489 d.setOnDismissListener(mDialogListener);
490 d.show();
491 }
492 }
493
494 // -------------------------------------------------------------------------
495 // WebViewClient implementation for the main WebView
496 // -------------------------------------------------------------------------
497
498 private final WebViewClient mWebViewClient = new WebViewClient() {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500499 private Message mDontResend;
500 private Message mResend;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700501 @Override
502 public void onPageStarted(WebView view, String url, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700503 mInPageLoad = true;
John Reck30c714c2010-12-16 17:30:34 -0800504 mPageLoadProgress = 0;
505 mCurrentState = new PageState(mActivity,
506 view.isPrivateBrowsingEnabled(), url, favicon);
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000507 mLoadStartTime = SystemClock.uptimeMillis();
Leon Scroggins58d56c62010-01-28 15:12:40 -0500508 if (mVoiceSearchData != null
509 && !url.equals(mVoiceSearchData.mLastVoiceSearchUrl)) {
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500510 if (mVoiceSearchData.mSourceIsGoogle) {
511 Intent i = new Intent(LoggingEvents.ACTION_LOG_EVENT);
512 i.putExtra(LoggingEvents.EXTRA_FLUSH, true);
513 mActivity.sendBroadcast(i);
514 }
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400515 revertVoiceSearchMode();
Leon Scroggins58d56c62010-01-28 15:12:40 -0500516 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700517
Grace Kloba22ac16e2009-10-07 18:00:23 -0700518
519 // If we start a touch icon load and then load a new page, we don't
520 // want to cancel the current touch icon loader. But, we do want to
521 // create a new one when the touch icon url is known.
522 if (mTouchIconLoader != null) {
523 mTouchIconLoader.mTab = null;
524 mTouchIconLoader = null;
525 }
526
527 // reset the error console
528 if (mErrorConsole != null) {
529 mErrorConsole.clearErrorMessages();
Michael Kolb8233fac2010-10-26 16:08:53 -0700530 if (mWebViewController.shouldShowErrorConsole()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700531 mErrorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
532 }
533 }
534
Grace Kloba22ac16e2009-10-07 18:00:23 -0700535 // finally update the UI in the activity if it is in the foreground
John Reck324d4402011-01-11 16:56:42 -0800536 mWebViewController.onPageStarted(Tab.this, view, favicon);
Leon Scroggins4cd97792010-12-03 15:31:56 -0500537
John Recke969cc52010-12-21 17:24:43 -0800538 updateBookmarkedStatus();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700539 }
540
541 @Override
542 public void onPageFinished(WebView view, String url) {
John Reck5b691842010-11-29 11:21:13 -0800543 if (!isPrivateBrowsingEnabled()) {
544 LogTag.logPageFinishedLoading(
545 url, SystemClock.uptimeMillis() - mLoadStartTime);
546 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700547 mInPageLoad = false;
John Reck30c714c2010-12-16 17:30:34 -0800548 // Sync state (in case of stop/timeout)
549 mCurrentState.mUrl = view.getUrl();
John Reck6c702ee2011-01-07 09:41:53 -0800550 if (mCurrentState.mUrl == null) {
551 mCurrentState.mUrl = url != null ? url : "";
552 }
John Reck30c714c2010-12-16 17:30:34 -0800553 mCurrentState.mTitle = view.getTitle();
554 mCurrentState.mFavicon = view.getFavicon();
555 if (!URLUtil.isHttpsUrl(mCurrentState.mUrl)) {
556 // In case we stop when loading an HTTPS page from an HTTP page
557 // but before a provisional load occurred
558 mCurrentState.mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
559 }
John Reck324d4402011-01-11 16:56:42 -0800560 mWebViewController.onPageFinished(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700561 }
562
563 // return true if want to hijack the url to let another app to handle it
564 @Override
565 public boolean shouldOverrideUrlLoading(WebView view, String url) {
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400566 if (voiceSearchSourceIsGoogle()) {
567 // This method is called when the user clicks on a link.
568 // VoiceSearchMode is turned off when the user leaves the
569 // Google results page, so at this point the user must be on
570 // that page. If the user clicked a link on that page, assume
571 // that the voice search was effective, and broadcast an Intent
572 // so a receiver can take note of that fact.
573 Intent logIntent = new Intent(LoggingEvents.ACTION_LOG_EVENT);
574 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
575 LoggingEvents.VoiceSearch.RESULT_CLICKED);
576 mActivity.sendBroadcast(logIntent);
577 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700578 if (mInForeground) {
Michael Kolb18eb3772010-12-10 14:29:51 -0800579 return mWebViewController.shouldOverrideUrlLoading(Tab.this,
580 view, url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700581 } else {
582 return false;
583 }
584 }
585
586 /**
587 * Updates the lock icon. This method is called when we discover another
588 * resource to be loaded for this page (for example, javascript). While
589 * we update the icon type, we do not update the lock icon itself until
590 * we are done loading, it is slightly more secure this way.
591 */
592 @Override
593 public void onLoadResource(WebView view, String url) {
594 if (url != null && url.length() > 0) {
595 // It is only if the page claims to be secure that we may have
596 // to update the lock:
John Reck30c714c2010-12-16 17:30:34 -0800597 if (mCurrentState.mLockIcon == LockIcon.LOCK_ICON_SECURE) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700598 // If NOT a 'safe' url, change the lock to mixed content!
599 if (!(URLUtil.isHttpsUrl(url) || URLUtil.isDataUrl(url)
600 || URLUtil.isAboutUrl(url))) {
John Reck30c714c2010-12-16 17:30:34 -0800601 mCurrentState.mLockIcon = LockIcon.LOCK_ICON_MIXED;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700602 }
603 }
604 }
605 }
606
607 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700608 * Show a dialog informing the user of the network error reported by
609 * WebCore if it is in the foreground.
610 */
611 @Override
612 public void onReceivedError(WebView view, int errorCode,
613 String description, String failingUrl) {
614 if (errorCode != WebViewClient.ERROR_HOST_LOOKUP &&
615 errorCode != WebViewClient.ERROR_CONNECT &&
616 errorCode != WebViewClient.ERROR_BAD_URL &&
617 errorCode != WebViewClient.ERROR_UNSUPPORTED_SCHEME &&
618 errorCode != WebViewClient.ERROR_FILE) {
619 queueError(errorCode, description);
620 }
Jeff Hamilton47654f42010-09-07 09:57:51 -0500621
622 // Don't log URLs when in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -0700623 if (!isPrivateBrowsingEnabled()) {
Jeff Hamilton47654f42010-09-07 09:57:51 -0500624 Log.e(LOGTAG, "onReceivedError " + errorCode + " " + failingUrl
625 + " " + description);
626 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700627 }
628
629 /**
630 * Check with the user if it is ok to resend POST data as the page they
631 * are trying to navigate to is the result of a POST.
632 */
633 @Override
634 public void onFormResubmission(WebView view, final Message dontResend,
635 final Message resend) {
636 if (!mInForeground) {
637 dontResend.sendToTarget();
638 return;
639 }
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500640 if (mDontResend != null) {
641 Log.w(LOGTAG, "onFormResubmission should not be called again "
642 + "while dialog is still up");
643 dontResend.sendToTarget();
644 return;
645 }
646 mDontResend = dontResend;
647 mResend = resend;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700648 new AlertDialog.Builder(mActivity).setTitle(
649 R.string.browserFrameFormResubmitLabel).setMessage(
650 R.string.browserFrameFormResubmitMessage)
651 .setPositiveButton(R.string.ok,
652 new DialogInterface.OnClickListener() {
653 public void onClick(DialogInterface dialog,
654 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500655 if (mResend != null) {
656 mResend.sendToTarget();
657 mResend = null;
658 mDontResend = null;
659 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700660 }
661 }).setNegativeButton(R.string.cancel,
662 new DialogInterface.OnClickListener() {
663 public void onClick(DialogInterface dialog,
664 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500665 if (mDontResend != null) {
666 mDontResend.sendToTarget();
667 mResend = null;
668 mDontResend = null;
669 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700670 }
671 }).setOnCancelListener(new OnCancelListener() {
672 public void onCancel(DialogInterface dialog) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500673 if (mDontResend != null) {
674 mDontResend.sendToTarget();
675 mResend = null;
676 mDontResend = null;
677 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700678 }
679 }).show();
680 }
681
682 /**
683 * Insert the url into the visited history database.
684 * @param url The url to be inserted.
685 * @param isReload True if this url is being reloaded.
686 * FIXME: Not sure what to do when reloading the page.
687 */
688 @Override
689 public void doUpdateVisitedHistory(WebView view, String url,
690 boolean isReload) {
John Reck324d4402011-01-11 16:56:42 -0800691 mWebViewController.doUpdateVisitedHistory(Tab.this, isReload);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700692 }
693
694 /**
695 * Displays SSL error(s) dialog to the user.
696 */
697 @Override
698 public void onReceivedSslError(final WebView view,
699 final SslErrorHandler handler, final SslError error) {
700 if (!mInForeground) {
701 handler.cancel();
John Reck30c714c2010-12-16 17:30:34 -0800702 setLockIconType(LockIcon.LOCK_ICON_UNSECURE);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700703 return;
704 }
705 if (BrowserSettings.getInstance().showSecurityWarnings()) {
706 final LayoutInflater factory =
707 LayoutInflater.from(mActivity);
708 final View warningsView =
709 factory.inflate(R.layout.ssl_warnings, null);
710 final LinearLayout placeholder =
711 (LinearLayout)warningsView.findViewById(R.id.placeholder);
712
713 if (error.hasError(SslError.SSL_UNTRUSTED)) {
714 LinearLayout ll = (LinearLayout)factory
715 .inflate(R.layout.ssl_warning, null);
716 ((TextView)ll.findViewById(R.id.warning))
717 .setText(R.string.ssl_untrusted);
718 placeholder.addView(ll);
719 }
720
721 if (error.hasError(SslError.SSL_IDMISMATCH)) {
722 LinearLayout ll = (LinearLayout)factory
723 .inflate(R.layout.ssl_warning, null);
724 ((TextView)ll.findViewById(R.id.warning))
725 .setText(R.string.ssl_mismatch);
726 placeholder.addView(ll);
727 }
728
729 if (error.hasError(SslError.SSL_EXPIRED)) {
730 LinearLayout ll = (LinearLayout)factory
731 .inflate(R.layout.ssl_warning, null);
732 ((TextView)ll.findViewById(R.id.warning))
733 .setText(R.string.ssl_expired);
734 placeholder.addView(ll);
735 }
736
737 if (error.hasError(SslError.SSL_NOTYETVALID)) {
738 LinearLayout ll = (LinearLayout)factory
739 .inflate(R.layout.ssl_warning, null);
740 ((TextView)ll.findViewById(R.id.warning))
741 .setText(R.string.ssl_not_yet_valid);
742 placeholder.addView(ll);
743 }
744
745 new AlertDialog.Builder(mActivity).setTitle(
746 R.string.security_warning).setIcon(
747 android.R.drawable.ic_dialog_alert).setView(
748 warningsView).setPositiveButton(R.string.ssl_continue,
749 new DialogInterface.OnClickListener() {
750 public void onClick(DialogInterface dialog,
751 int whichButton) {
752 handler.proceed();
753 }
754 }).setNeutralButton(R.string.view_certificate,
755 new DialogInterface.OnClickListener() {
756 public void onClick(DialogInterface dialog,
757 int whichButton) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700758 mWebViewController.showSslCertificateOnError(view,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700759 handler, error);
760 }
Ben Murdocha49b8292010-11-16 11:56:04 +0000761 }).setNegativeButton(R.string.ssl_go_back,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700762 new DialogInterface.OnClickListener() {
763 public void onClick(DialogInterface dialog,
764 int whichButton) {
John Reck30c714c2010-12-16 17:30:34 -0800765 dialog.cancel();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700766 }
767 }).setOnCancelListener(
768 new DialogInterface.OnCancelListener() {
769 public void onCancel(DialogInterface dialog) {
770 handler.cancel();
John Reck30c714c2010-12-16 17:30:34 -0800771 setLockIconType(LockIcon.LOCK_ICON_UNSECURE);
772 mWebViewController.onUserCanceledSsl(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700773 }
774 }).show();
775 } else {
776 handler.proceed();
777 }
778 }
779
780 /**
781 * Handles an HTTP authentication request.
782 *
783 * @param handler The authentication handler
784 * @param host The host
785 * @param realm The realm
786 */
787 @Override
788 public void onReceivedHttpAuthRequest(WebView view,
789 final HttpAuthHandler handler, final String host,
790 final String realm) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700791 mWebViewController.onReceivedHttpAuthRequest(Tab.this, view, handler, host, realm);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700792 }
793
794 @Override
795 public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
796 if (!mInForeground) {
797 return false;
798 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700799 return mWebViewController.shouldOverrideKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700800 }
801
802 @Override
803 public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700804 if (!mInForeground) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700805 return;
806 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700807 mWebViewController.onUnhandledKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700808 }
809 };
810
811 // -------------------------------------------------------------------------
812 // WebChromeClient implementation for the main WebView
813 // -------------------------------------------------------------------------
814
815 private final WebChromeClient mWebChromeClient = new WebChromeClient() {
816 // Helper method to create a new tab or sub window.
817 private void createWindow(final boolean dialog, final Message msg) {
818 WebView.WebViewTransport transport =
819 (WebView.WebViewTransport) msg.obj;
820 if (dialog) {
821 createSubWindow();
Michael Kolb8233fac2010-10-26 16:08:53 -0700822 mWebViewController.attachSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700823 transport.setWebView(mSubView);
824 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -0700825 final Tab newTab = mWebViewController.openTabAndShow(
Michael Kolb18eb3772010-12-10 14:29:51 -0800826 Tab.this,
Michael Kolb8233fac2010-10-26 16:08:53 -0700827 IntentHandler.EMPTY_URL_DATA, false, null);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700828 if (newTab != Tab.this) {
829 Tab.this.addChildTab(newTab);
830 }
831 transport.setWebView(newTab.getWebView());
832 }
833 msg.sendToTarget();
834 }
835
836 @Override
837 public boolean onCreateWindow(WebView view, final boolean dialog,
838 final boolean userGesture, final Message resultMsg) {
839 // only allow new window or sub window for the foreground case
840 if (!mInForeground) {
841 return false;
842 }
843 // Short-circuit if we can't create any more tabs or sub windows.
844 if (dialog && mSubView != null) {
845 new AlertDialog.Builder(mActivity)
846 .setTitle(R.string.too_many_subwindows_dialog_title)
847 .setIcon(android.R.drawable.ic_dialog_alert)
848 .setMessage(R.string.too_many_subwindows_dialog_message)
849 .setPositiveButton(R.string.ok, null)
850 .show();
851 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700852 } else if (!mWebViewController.getTabControl().canCreateNewTab()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700853 new AlertDialog.Builder(mActivity)
854 .setTitle(R.string.too_many_windows_dialog_title)
855 .setIcon(android.R.drawable.ic_dialog_alert)
856 .setMessage(R.string.too_many_windows_dialog_message)
857 .setPositiveButton(R.string.ok, null)
858 .show();
859 return false;
860 }
861
862 // Short-circuit if this was a user gesture.
863 if (userGesture) {
864 createWindow(dialog, resultMsg);
865 return true;
866 }
867
868 // Allow the popup and create the appropriate window.
869 final AlertDialog.OnClickListener allowListener =
870 new AlertDialog.OnClickListener() {
871 public void onClick(DialogInterface d,
872 int which) {
873 createWindow(dialog, resultMsg);
874 }
875 };
876
877 // Block the popup by returning a null WebView.
878 final AlertDialog.OnClickListener blockListener =
879 new AlertDialog.OnClickListener() {
880 public void onClick(DialogInterface d, int which) {
881 resultMsg.sendToTarget();
882 }
883 };
884
885 // Build a confirmation dialog to display to the user.
886 final AlertDialog d =
887 new AlertDialog.Builder(mActivity)
888 .setTitle(R.string.attention)
889 .setIcon(android.R.drawable.ic_dialog_alert)
890 .setMessage(R.string.popup_window_attempt)
891 .setPositiveButton(R.string.allow, allowListener)
892 .setNegativeButton(R.string.block, blockListener)
893 .setCancelable(false)
894 .create();
895
896 // Show the confirmation dialog.
897 d.show();
898 return true;
899 }
900
901 @Override
Patrick Scotteb5061b2009-11-18 15:00:30 -0500902 public void onRequestFocus(WebView view) {
903 if (!mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700904 mWebViewController.switchToTab(mWebViewController.getTabControl().getTabIndex(
Patrick Scotteb5061b2009-11-18 15:00:30 -0500905 Tab.this));
906 }
907 }
908
909 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700910 public void onCloseWindow(WebView window) {
911 if (mParentTab != null) {
912 // JavaScript can only close popup window.
913 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700914 mWebViewController.switchToTab(mWebViewController.getTabControl()
Grace Kloba22ac16e2009-10-07 18:00:23 -0700915 .getTabIndex(mParentTab));
916 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700917 mWebViewController.closeTab(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700918 }
919 }
920
921 @Override
922 public void onProgressChanged(WebView view, int newProgress) {
John Reck30c714c2010-12-16 17:30:34 -0800923 mPageLoadProgress = newProgress;
924 mWebViewController.onProgressChanged(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700925 }
926
927 @Override
Leon Scroggins21d9b902010-03-11 09:33:11 -0500928 public void onReceivedTitle(WebView view, final String title) {
John Reck30c714c2010-12-16 17:30:34 -0800929 mCurrentState.mTitle = title;
Michael Kolb8233fac2010-10-26 16:08:53 -0700930 mWebViewController.onReceivedTitle(Tab.this, title);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700931 }
932
933 @Override
934 public void onReceivedIcon(WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -0800935 mCurrentState.mFavicon = icon;
Michael Kolb8233fac2010-10-26 16:08:53 -0700936 mWebViewController.onFavicon(Tab.this, view, icon);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700937 }
938
939 @Override
940 public void onReceivedTouchIconUrl(WebView view, String url,
941 boolean precomposed) {
942 final ContentResolver cr = mActivity.getContentResolver();
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400943 // Let precomposed icons take precedence over non-composed
944 // icons.
945 if (precomposed && mTouchIconLoader != null) {
946 mTouchIconLoader.cancel(false);
947 mTouchIconLoader = null;
948 }
949 // Have only one async task at a time.
950 if (mTouchIconLoader == null) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700951 mTouchIconLoader = new DownloadTouchIcon(Tab.this,
952 mActivity, cr, view);
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400953 mTouchIconLoader.execute(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700954 }
955 }
956
957 @Override
958 public void onShowCustomView(View view,
959 WebChromeClient.CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700960 if (mInForeground) mWebViewController.showCustomView(Tab.this, view,
961 callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700962 }
963
964 @Override
965 public void onHideCustomView() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700966 if (mInForeground) mWebViewController.hideCustomView();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700967 }
968
969 /**
970 * The origin has exceeded its database quota.
971 * @param url the URL that exceeded the quota
972 * @param databaseIdentifier the identifier of the database on which the
973 * transaction that caused the quota overflow was run
974 * @param currentQuota the current quota for the origin.
975 * @param estimatedSize the estimated size of the database.
976 * @param totalUsedQuota is the sum of all origins' quota.
977 * @param quotaUpdater The callback to run when a decision to allow or
978 * deny quota has been made. Don't forget to call this!
979 */
980 @Override
981 public void onExceededDatabaseQuota(String url,
982 String databaseIdentifier, long currentQuota, long estimatedSize,
983 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
984 BrowserSettings.getInstance().getWebStorageSizeManager()
985 .onExceededDatabaseQuota(url, databaseIdentifier,
986 currentQuota, estimatedSize, totalUsedQuota,
987 quotaUpdater);
988 }
989
990 /**
991 * The Application Cache has exceeded its max size.
992 * @param spaceNeeded is the amount of disk space that would be needed
993 * in order for the last appcache operation to succeed.
994 * @param totalUsedQuota is the sum of all origins' quota.
995 * @param quotaUpdater A callback to inform the WebCore thread that a
996 * new app cache size is available. This callback must always
997 * be executed at some point to ensure that the sleeping
998 * WebCore thread is woken up.
999 */
1000 @Override
1001 public void onReachedMaxAppCacheSize(long spaceNeeded,
1002 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
1003 BrowserSettings.getInstance().getWebStorageSizeManager()
1004 .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota,
1005 quotaUpdater);
1006 }
1007
1008 /**
1009 * Instructs the browser to show a prompt to ask the user to set the
1010 * Geolocation permission state for the specified origin.
1011 * @param origin The origin for which Geolocation permissions are
1012 * requested.
1013 * @param callback The callback to call once the user has set the
1014 * Geolocation permission state.
1015 */
1016 @Override
1017 public void onGeolocationPermissionsShowPrompt(String origin,
1018 GeolocationPermissions.Callback callback) {
1019 if (mInForeground) {
Grace Kloba50c241e2010-04-20 11:07:50 -07001020 getGeolocationPermissionsPrompt().show(origin, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001021 }
1022 }
1023
1024 /**
1025 * Instructs the browser to hide the Geolocation permissions prompt.
1026 */
1027 @Override
1028 public void onGeolocationPermissionsHidePrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001029 if (mInForeground && mGeolocationPermissionsPrompt != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001030 mGeolocationPermissionsPrompt.hide();
1031 }
1032 }
1033
Ben Murdoch65acc352009-11-19 18:16:04 +00001034 /* Adds a JavaScript error message to the system log and if the JS
1035 * console is enabled in the about:debug options, to that console
1036 * also.
Ben Murdochc42addf2010-01-28 15:19:59 +00001037 * @param consoleMessage the message object.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001038 */
1039 @Override
Ben Murdochc42addf2010-01-28 15:19:59 +00001040 public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001041 if (mInForeground) {
1042 // call getErrorConsole(true) so it will create one if needed
1043 ErrorConsoleView errorConsole = getErrorConsole(true);
Ben Murdochc42addf2010-01-28 15:19:59 +00001044 errorConsole.addErrorMessage(consoleMessage);
Michael Kolb8233fac2010-10-26 16:08:53 -07001045 if (mWebViewController.shouldShowErrorConsole()
1046 && errorConsole.getShowState() !=
1047 ErrorConsoleView.SHOW_MAXIMIZED) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001048 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
1049 }
1050 }
Ben Murdochc42addf2010-01-28 15:19:59 +00001051
Jeff Hamilton47654f42010-09-07 09:57:51 -05001052 // Don't log console messages in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001053 if (isPrivateBrowsingEnabled()) return true;
Jeff Hamilton47654f42010-09-07 09:57:51 -05001054
Ben Murdochc42addf2010-01-28 15:19:59 +00001055 String message = "Console: " + consoleMessage.message() + " "
1056 + consoleMessage.sourceId() + ":"
1057 + consoleMessage.lineNumber();
1058
1059 switch (consoleMessage.messageLevel()) {
1060 case TIP:
1061 Log.v(CONSOLE_LOGTAG, message);
1062 break;
1063 case LOG:
1064 Log.i(CONSOLE_LOGTAG, message);
1065 break;
1066 case WARNING:
1067 Log.w(CONSOLE_LOGTAG, message);
1068 break;
1069 case ERROR:
1070 Log.e(CONSOLE_LOGTAG, message);
1071 break;
1072 case DEBUG:
1073 Log.d(CONSOLE_LOGTAG, message);
1074 break;
1075 }
1076
1077 return true;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001078 }
1079
1080 /**
1081 * Ask the browser for an icon to represent a <video> element.
1082 * This icon will be used if the Web page did not specify a poster attribute.
1083 * @return Bitmap The icon or null if no such icon is available.
1084 */
1085 @Override
1086 public Bitmap getDefaultVideoPoster() {
1087 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001088 return mWebViewController.getDefaultVideoPoster();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001089 }
1090 return null;
1091 }
1092
1093 /**
1094 * Ask the host application for a custom progress view to show while
1095 * a <video> is loading.
1096 * @return View The progress view.
1097 */
1098 @Override
1099 public View getVideoLoadingProgressView() {
1100 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001101 return mWebViewController.getVideoLoadingProgressView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001102 }
1103 return null;
1104 }
1105
1106 @Override
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01001107 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001108 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001109 mWebViewController.openFileChooser(uploadMsg, acceptType);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001110 } else {
1111 uploadMsg.onReceiveValue(null);
1112 }
1113 }
1114
1115 /**
1116 * Deliver a list of already-visited URLs
1117 */
1118 @Override
1119 public void getVisitedHistory(final ValueCallback<String[]> callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001120 mWebViewController.getVisitedHistory(callback);
1121 }
Ben Murdoch8029a772010-11-16 11:58:21 +00001122
1123 @Override
1124 public void setupAutoFill(Message message) {
1125 // Prompt the user to set up their profile.
1126 final Message msg = message;
1127 AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
Ben Murdoch1d676b62011-01-17 12:54:24 +00001128 LayoutInflater inflater = (LayoutInflater) mActivity.getSystemService(
1129 Context.LAYOUT_INFLATER_SERVICE);
1130 final View layout = inflater.inflate(R.layout.setup_autofill_dialog, null);
1131
1132 builder.setView(layout)
1133 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1134 @Override
1135 public void onClick(DialogInterface dialog, int id) {
1136 CheckBox disableAutoFill = (CheckBox) layout.findViewById(
1137 R.id.setup_autofill_dialog_disable_autofill);
1138
1139 if (disableAutoFill.isChecked()) {
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,
1146 R.string.autofill_setup_dialog_negative_toast,
1147 Toast.LENGTH_LONG).show();
1148 } else {
1149 // Take user to the AutoFill profile editor. When they return,
1150 // we will send the message that we pass here which will trigger
1151 // the form to get filled out with their new profile.
1152 mWebViewController.setupAutoFill(msg);
1153 }
1154 }
1155 })
1156 .setNegativeButton(R.string.cancel, null)
1157 .show();
Ben Murdoch8029a772010-11-16 11:58:21 +00001158 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001159 };
1160
1161 // -------------------------------------------------------------------------
1162 // WebViewClient implementation for the sub window
1163 // -------------------------------------------------------------------------
1164
1165 // Subclass of WebViewClient used in subwindows to notify the main
1166 // WebViewClient of certain WebView activities.
1167 private static class SubWindowClient extends WebViewClient {
1168 // The main WebViewClient.
1169 private final WebViewClient mClient;
Michael Kolb8233fac2010-10-26 16:08:53 -07001170 private final WebViewController mController;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001171
Michael Kolb8233fac2010-10-26 16:08:53 -07001172 SubWindowClient(WebViewClient client, WebViewController controller) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001173 mClient = client;
Michael Kolb8233fac2010-10-26 16:08:53 -07001174 mController = controller;
Leon Scroggins III211ba542010-04-19 13:21:13 -04001175 }
1176 @Override
1177 public void onPageStarted(WebView view, String url, Bitmap favicon) {
1178 // Unlike the others, do not call mClient's version, which would
1179 // change the progress bar. However, we do want to remove the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001180 // find or select dialog.
Michael Kolb8233fac2010-10-26 16:08:53 -07001181 mController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001182 }
1183 @Override
1184 public void doUpdateVisitedHistory(WebView view, String url,
1185 boolean isReload) {
1186 mClient.doUpdateVisitedHistory(view, url, isReload);
1187 }
1188 @Override
1189 public boolean shouldOverrideUrlLoading(WebView view, String url) {
1190 return mClient.shouldOverrideUrlLoading(view, url);
1191 }
1192 @Override
1193 public void onReceivedSslError(WebView view, SslErrorHandler handler,
1194 SslError error) {
1195 mClient.onReceivedSslError(view, handler, error);
1196 }
1197 @Override
1198 public void onReceivedHttpAuthRequest(WebView view,
1199 HttpAuthHandler handler, String host, String realm) {
1200 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
1201 }
1202 @Override
1203 public void onFormResubmission(WebView view, Message dontResend,
1204 Message resend) {
1205 mClient.onFormResubmission(view, dontResend, resend);
1206 }
1207 @Override
1208 public void onReceivedError(WebView view, int errorCode,
1209 String description, String failingUrl) {
1210 mClient.onReceivedError(view, errorCode, description, failingUrl);
1211 }
1212 @Override
1213 public boolean shouldOverrideKeyEvent(WebView view,
1214 android.view.KeyEvent event) {
1215 return mClient.shouldOverrideKeyEvent(view, event);
1216 }
1217 @Override
1218 public void onUnhandledKeyEvent(WebView view,
1219 android.view.KeyEvent event) {
1220 mClient.onUnhandledKeyEvent(view, event);
1221 }
1222 }
1223
1224 // -------------------------------------------------------------------------
1225 // WebChromeClient implementation for the sub window
1226 // -------------------------------------------------------------------------
1227
1228 private class SubWindowChromeClient extends WebChromeClient {
1229 // The main WebChromeClient.
1230 private final WebChromeClient mClient;
1231
1232 SubWindowChromeClient(WebChromeClient client) {
1233 mClient = client;
1234 }
1235 @Override
1236 public void onProgressChanged(WebView view, int newProgress) {
1237 mClient.onProgressChanged(view, newProgress);
1238 }
1239 @Override
1240 public boolean onCreateWindow(WebView view, boolean dialog,
1241 boolean userGesture, android.os.Message resultMsg) {
1242 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
1243 }
1244 @Override
1245 public void onCloseWindow(WebView window) {
1246 if (window != mSubView) {
1247 Log.e(LOGTAG, "Can't close the window");
1248 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001249 mWebViewController.dismissSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001250 }
1251 }
1252
1253 // -------------------------------------------------------------------------
1254
Michael Kolb8233fac2010-10-26 16:08:53 -07001255 // TODO temporarily use activity here
1256 // remove later
1257
Grace Kloba22ac16e2009-10-07 18:00:23 -07001258 // Construct a new tab
Michael Kolb8233fac2010-10-26 16:08:53 -07001259 Tab(WebViewController wvcontroller, WebView w, boolean closeOnExit, String appId,
Grace Kloba22ac16e2009-10-07 18:00:23 -07001260 String url) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001261 mWebViewController = wvcontroller;
1262 mActivity = mWebViewController.getActivity();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001263 mCloseOnExit = closeOnExit;
1264 mAppId = appId;
John Recke969cc52010-12-21 17:24:43 -08001265 mDataController = DataController.getInstance(mActivity);
John Reck30c714c2010-12-16 17:30:34 -08001266 mCurrentState = new PageState(mActivity, w.isPrivateBrowsingEnabled());
Michael Kolb8233fac2010-10-26 16:08:53 -07001267 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001268 mInForeground = false;
1269
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001270 mDownloadListener = new DownloadListener() {
1271 public void onDownloadStart(String url, String userAgent,
1272 String contentDisposition, String mimetype,
1273 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001274 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001275 mimetype, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001276 }
1277 };
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001278 mWebBackForwardListClient = new WebBackForwardListClient() {
1279 @Override
1280 public void onNewHistoryItem(WebHistoryItem item) {
1281 if (isInVoiceSearchMode()) {
1282 item.setCustomData(mVoiceSearchData.mVoiceSearchIntent);
1283 }
1284 }
1285 @Override
1286 public void onIndexChanged(WebHistoryItem item, int index) {
1287 Object data = item.getCustomData();
1288 if (data != null && data instanceof Intent) {
1289 activateVoiceSearchMode((Intent) data);
1290 }
1291 }
1292 };
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001293
Grace Kloba22ac16e2009-10-07 18:00:23 -07001294 setWebView(w);
1295 }
1296
1297 /**
1298 * Sets the WebView for this tab, correctly removing the old WebView from
1299 * the container view.
1300 */
1301 void setWebView(WebView w) {
1302 if (mMainView == w) {
1303 return;
1304 }
Michael Kolba713ec82010-11-29 17:27:06 -08001305
Grace Kloba22ac16e2009-10-07 18:00:23 -07001306 // If the WebView is changing, the page will be reloaded, so any ongoing
1307 // Geolocation permission requests are void.
Grace Kloba50c241e2010-04-20 11:07:50 -07001308 if (mGeolocationPermissionsPrompt != null) {
1309 mGeolocationPermissionsPrompt.hide();
1310 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001311
Michael Kolba713ec82010-11-29 17:27:06 -08001312 mWebViewController.onSetWebView(this, w);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001313
1314 // set the new one
1315 mMainView = w;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001316 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001317 if (mMainView != null) {
1318 mMainView.setWebViewClient(mWebViewClient);
1319 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001320 // Attach DownloadManager so that downloads can start in an active
1321 // or a non-active window. This can happen when going to a site that
1322 // does a redirect after a period of time. The user could have
1323 // switched to another tab while waiting for the download to start.
1324 mMainView.setDownloadListener(mDownloadListener);
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001325 mMainView.setWebBackForwardListClient(mWebBackForwardListClient);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001326 }
1327 }
1328
1329 /**
1330 * Destroy the tab's main WebView and subWindow if any
1331 */
1332 void destroy() {
1333 if (mMainView != null) {
1334 dismissSubWindow();
1335 BrowserSettings.getInstance().deleteObserver(mMainView.getSettings());
1336 // save the WebView to call destroy() after detach it from the tab
1337 WebView webView = mMainView;
1338 setWebView(null);
1339 webView.destroy();
1340 }
1341 }
1342
1343 /**
1344 * Remove the tab from the parent
1345 */
1346 void removeFromTree() {
1347 // detach the children
1348 if (mChildTabs != null) {
1349 for(Tab t : mChildTabs) {
1350 t.setParentTab(null);
1351 }
1352 }
1353 // remove itself from the parent list
1354 if (mParentTab != null) {
1355 mParentTab.mChildTabs.remove(this);
1356 }
1357 }
1358
1359 /**
1360 * Create a new subwindow unless a subwindow already exists.
1361 * @return True if a new subwindow was created. False if one already exists.
1362 */
1363 boolean createSubWindow() {
1364 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001365 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001366 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001367 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001368 mSubView.setWebChromeClient(new SubWindowChromeClient(
1369 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001370 // Set a different DownloadListener for the mSubView, since it will
1371 // just need to dismiss the mSubView, rather than close the Tab
1372 mSubView.setDownloadListener(new DownloadListener() {
1373 public void onDownloadStart(String url, String userAgent,
1374 String contentDisposition, String mimetype,
1375 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001376 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001377 contentDisposition, mimetype, contentLength);
1378 if (mSubView.copyBackForwardList().getSize() == 0) {
1379 // This subwindow was opened for the sole purpose of
1380 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001381 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001382 }
1383 }
1384 });
Grace Kloba22ac16e2009-10-07 18:00:23 -07001385 mSubView.setOnCreateContextMenuListener(mActivity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001386 return true;
1387 }
1388 return false;
1389 }
1390
1391 /**
1392 * Dismiss the subWindow for the tab.
1393 */
1394 void dismissSubWindow() {
1395 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001396 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001397 BrowserSettings.getInstance().deleteObserver(
1398 mSubView.getSettings());
1399 mSubView.destroy();
1400 mSubView = null;
1401 mSubViewContainer = null;
1402 }
1403 }
1404
Grace Kloba22ac16e2009-10-07 18:00:23 -07001405
1406 /**
1407 * Set the parent tab of this tab.
1408 */
1409 void setParentTab(Tab parent) {
1410 mParentTab = parent;
1411 // This tab may have been freed due to low memory. If that is the case,
1412 // the parent tab index is already saved. If we are changing that index
1413 // (most likely due to removing the parent tab) we must update the
1414 // parent tab index in the saved Bundle.
1415 if (mSavedState != null) {
1416 if (parent == null) {
1417 mSavedState.remove(PARENTTAB);
1418 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -07001419 mSavedState.putInt(PARENTTAB, mWebViewController.getTabControl()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001420 .getTabIndex(parent));
1421 }
1422 }
1423 }
1424
1425 /**
1426 * When a Tab is created through the content of another Tab, then we
1427 * associate the Tabs.
1428 * @param child the Tab that was created from this Tab
1429 */
1430 void addChildTab(Tab child) {
1431 if (mChildTabs == null) {
1432 mChildTabs = new Vector<Tab>();
1433 }
1434 mChildTabs.add(child);
1435 child.setParentTab(this);
1436 }
1437
1438 Vector<Tab> getChildTabs() {
1439 return mChildTabs;
1440 }
1441
1442 void resume() {
1443 if (mMainView != null) {
1444 mMainView.onResume();
1445 if (mSubView != null) {
1446 mSubView.onResume();
1447 }
1448 }
1449 }
1450
1451 void pause() {
1452 if (mMainView != null) {
1453 mMainView.onPause();
1454 if (mSubView != null) {
1455 mSubView.onPause();
1456 }
1457 }
1458 }
1459
1460 void putInForeground() {
1461 mInForeground = true;
1462 resume();
1463 mMainView.setOnCreateContextMenuListener(mActivity);
1464 if (mSubView != null) {
1465 mSubView.setOnCreateContextMenuListener(mActivity);
1466 }
1467 // Show the pending error dialog if the queue is not empty
1468 if (mQueuedErrors != null && mQueuedErrors.size() > 0) {
1469 showError(mQueuedErrors.getFirst());
1470 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001471 mWebViewController.bookmarkedStatusHasChanged(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001472 }
1473
1474 void putInBackground() {
1475 mInForeground = false;
1476 pause();
1477 mMainView.setOnCreateContextMenuListener(null);
1478 if (mSubView != null) {
1479 mSubView.setOnCreateContextMenuListener(null);
1480 }
1481 }
1482
Michael Kolb8233fac2010-10-26 16:08:53 -07001483 boolean inForeground() {
1484 return mInForeground;
1485 }
1486
Grace Kloba22ac16e2009-10-07 18:00:23 -07001487 /**
1488 * Return the top window of this tab; either the subwindow if it is not
1489 * null or the main window.
1490 * @return The top window of this tab.
1491 */
1492 WebView getTopWindow() {
1493 if (mSubView != null) {
1494 return mSubView;
1495 }
1496 return mMainView;
1497 }
1498
1499 /**
1500 * Return the main window of this tab. Note: if a tab is freed in the
1501 * background, this can return null. It is only guaranteed to be
1502 * non-null for the current tab.
1503 * @return The main WebView of this tab.
1504 */
1505 WebView getWebView() {
1506 return mMainView;
1507 }
1508
Michael Kolba713ec82010-11-29 17:27:06 -08001509 void setViewContainer(View container) {
1510 mContainer = container;
1511 }
1512
Michael Kolb8233fac2010-10-26 16:08:53 -07001513 View getViewContainer() {
1514 return mContainer;
1515 }
1516
Grace Kloba22ac16e2009-10-07 18:00:23 -07001517 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001518 * Return whether private browsing is enabled for the main window of
1519 * this tab.
1520 * @return True if private browsing is enabled.
1521 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001522 boolean isPrivateBrowsingEnabled() {
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001523 WebView webView = getWebView();
1524 if (webView == null) {
1525 return false;
1526 }
1527 return webView.isPrivateBrowsingEnabled();
1528 }
1529
1530 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001531 * Return the subwindow of this tab or null if there is no subwindow.
1532 * @return The subwindow of this tab or null.
1533 */
1534 WebView getSubWebView() {
1535 return mSubView;
1536 }
1537
Michael Kolb1514bb72010-11-22 09:11:48 -08001538 void setSubWebView(WebView subView) {
1539 mSubView = subView;
1540 }
1541
Michael Kolb8233fac2010-10-26 16:08:53 -07001542 View getSubViewContainer() {
1543 return mSubViewContainer;
1544 }
1545
Michael Kolb1514bb72010-11-22 09:11:48 -08001546 void setSubViewContainer(View subViewContainer) {
1547 mSubViewContainer = subViewContainer;
1548 }
1549
Grace Kloba22ac16e2009-10-07 18:00:23 -07001550 /**
1551 * @return The geolocation permissions prompt for this tab.
1552 */
1553 GeolocationPermissionsPrompt getGeolocationPermissionsPrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001554 if (mGeolocationPermissionsPrompt == null) {
1555 ViewStub stub = (ViewStub) mContainer
1556 .findViewById(R.id.geolocation_permissions_prompt);
1557 mGeolocationPermissionsPrompt = (GeolocationPermissionsPrompt) stub
1558 .inflate();
1559 mGeolocationPermissionsPrompt.init();
1560 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001561 return mGeolocationPermissionsPrompt;
1562 }
1563
1564 /**
1565 * @return The application id string
1566 */
1567 String getAppId() {
1568 return mAppId;
1569 }
1570
1571 /**
1572 * Set the application id string
1573 * @param id
1574 */
1575 void setAppId(String id) {
1576 mAppId = id;
1577 }
1578
Grace Kloba22ac16e2009-10-07 18:00:23 -07001579 String getUrl() {
John Reck324d4402011-01-11 16:56:42 -08001580 return UrlUtils.filteredUrl(mCurrentState.mUrl);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001581 }
1582
1583 /**
John Reck30c714c2010-12-16 17:30:34 -08001584 * Get the title of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001585 */
1586 String getTitle() {
John Reck30c714c2010-12-16 17:30:34 -08001587 if (mCurrentState.mTitle == null && mInPageLoad) {
1588 return mActivity.getString(R.string.title_bar_loading);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001589 }
John Reck30c714c2010-12-16 17:30:34 -08001590 return mCurrentState.mTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001591 }
1592
1593 /**
John Reck30c714c2010-12-16 17:30:34 -08001594 * Get the favicon of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001595 */
1596 Bitmap getFavicon() {
John Reck30c714c2010-12-16 17:30:34 -08001597 return mCurrentState.mFavicon;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001598 }
1599
John Recke969cc52010-12-21 17:24:43 -08001600 public boolean isBookmarkedSite() {
1601 return mCurrentState.mIsBookmarkedSite;
1602 }
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001603
Grace Kloba22ac16e2009-10-07 18:00:23 -07001604 /**
1605 * Return the tab's error console. Creates the console if createIfNEcessary
1606 * is true and we haven't already created the console.
1607 * @param createIfNecessary Flag to indicate if the console should be
1608 * created if it has not been already.
1609 * @return The tab's error console, or null if one has not been created and
1610 * createIfNecessary is false.
1611 */
1612 ErrorConsoleView getErrorConsole(boolean createIfNecessary) {
1613 if (createIfNecessary && mErrorConsole == null) {
1614 mErrorConsole = new ErrorConsoleView(mActivity);
1615 mErrorConsole.setWebView(mMainView);
1616 }
1617 return mErrorConsole;
1618 }
1619
1620 /**
1621 * If this Tab was created through another Tab, then this method returns
1622 * that Tab.
1623 * @return the Tab parent or null
1624 */
1625 public Tab getParentTab() {
1626 return mParentTab;
1627 }
1628
1629 /**
1630 * Return whether this tab should be closed when it is backing out of the
1631 * first page.
1632 * @return TRUE if this tab should be closed when exit.
1633 */
1634 boolean closeOnExit() {
1635 return mCloseOnExit;
1636 }
1637
John Reck30c714c2010-12-16 17:30:34 -08001638 private void setLockIconType(LockIcon icon) {
1639 mCurrentState.mLockIcon = icon;
1640 mWebViewController.onUpdatedLockIcon(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001641 }
1642
1643 /**
1644 * @return The tab's lock icon type.
1645 */
John Reck30c714c2010-12-16 17:30:34 -08001646 LockIcon getLockIconType() {
1647 return mCurrentState.mLockIcon;
1648 }
1649
1650 int getLoadProgress() {
1651 if (mInPageLoad) {
1652 return mPageLoadProgress;
1653 }
1654 return 100;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001655 }
1656
1657 /**
1658 * @return TRUE if onPageStarted is called while onPageFinished is not
1659 * called yet.
1660 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001661 boolean inPageLoad() {
1662 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001663 }
1664
1665 // force mInLoad to be false. This should only be called before closing the
1666 // tab to ensure BrowserActivity's pauseWebViewTimers() is called correctly.
Michael Kolb8233fac2010-10-26 16:08:53 -07001667 void clearInPageLoad() {
1668 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001669 }
1670
Grace Kloba22ac16e2009-10-07 18:00:23 -07001671 /**
John Reck30c714c2010-12-16 17:30:34 -08001672 * Get the cached saved state bundle.
1673 * @return cached state bundle
Grace Kloba22ac16e2009-10-07 18:00:23 -07001674 */
1675 Bundle getSavedState() {
1676 return mSavedState;
1677 }
1678
1679 /**
1680 * Set the saved state.
1681 */
1682 void setSavedState(Bundle state) {
1683 mSavedState = state;
1684 }
1685
1686 /**
1687 * @return TRUE if succeed in saving the state.
1688 */
1689 boolean saveState() {
1690 // If the WebView is null it means we ran low on memory and we already
1691 // stored the saved state in mSavedState.
1692 if (mMainView == null) {
1693 return mSavedState != null;
1694 }
1695
1696 mSavedState = new Bundle();
1697 final WebBackForwardList list = mMainView.saveState(mSavedState);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001698
1699 // Store some extra info for displaying the tab in the picker.
1700 final WebHistoryItem item = list != null ? list.getCurrentItem() : null;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001701
John Reck30c714c2010-12-16 17:30:34 -08001702 mSavedState.putString(CURRURL, mCurrentState.mUrl);
1703 mSavedState.putString(CURRTITLE, mCurrentState.mTitle);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001704 mSavedState.putBoolean(CLOSEONEXIT, mCloseOnExit);
1705 if (mAppId != null) {
1706 mSavedState.putString(APPID, mAppId);
1707 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001708 // Remember the parent tab so the relationship can be restored.
1709 if (mParentTab != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001710 mSavedState.putInt(PARENTTAB, mWebViewController.getTabControl().getTabIndex(
Grace Kloba22ac16e2009-10-07 18:00:23 -07001711 mParentTab));
1712 }
1713 return true;
1714 }
1715
1716 /*
1717 * Restore the state of the tab.
1718 */
1719 boolean restoreState(Bundle b) {
1720 if (b == null) {
1721 return false;
1722 }
1723 // Restore the internal state even if the WebView fails to restore.
1724 // This will maintain the app id, original url and close-on-exit values.
1725 mSavedState = null;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001726 mCloseOnExit = b.getBoolean(CLOSEONEXIT);
1727 mAppId = b.getString(APPID);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001728
1729 final WebBackForwardList list = mMainView.restoreState(b);
1730 if (list == null) {
1731 return false;
1732 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001733 return true;
1734 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04001735
Leon Scroggins1961ed22010-12-07 15:22:21 -05001736 public void updateBookmarkedStatus() {
John Recke969cc52010-12-21 17:24:43 -08001737 mDataController.queryBookmarkStatus(getUrl(), mIsBookmarkCallback);
Leon Scroggins1961ed22010-12-07 15:22:21 -05001738 }
1739
John Recke969cc52010-12-21 17:24:43 -08001740 private DataController.OnQueryUrlIsBookmark mIsBookmarkCallback
1741 = new DataController.OnQueryUrlIsBookmark() {
1742 @Override
1743 public void onQueryUrlIsBookmark(String url, boolean isBookmark) {
1744 if (mCurrentState.mUrl.equals(url)) {
1745 mCurrentState.mIsBookmarkedSite = isBookmark;
1746 mWebViewController.bookmarkedStatusHasChanged(Tab.this);
1747 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001748 }
John Recke969cc52010-12-21 17:24:43 -08001749 };
Grace Kloba22ac16e2009-10-07 18:00:23 -07001750}