blob: 5ef75647ea128ff4d333ebf922bdd7123432a10a [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
John Reckbafe58a2011-01-11 10:26:02 -080019import com.android.browser.homepages.HomeProvider;
Jeff Hamilton8ce956c2010-08-17 11:13:53 -050020import com.android.common.speech.LoggingEvents;
21
Michael Kolb8233fac2010-10-26 16:08:53 -070022import android.app.Activity;
Grace Kloba22ac16e2009-10-07 18:00:23 -070023import android.app.AlertDialog;
Leon Scroggins58d56c62010-01-28 15:12:40 -050024import android.app.SearchManager;
Grace Kloba22ac16e2009-10-07 18:00:23 -070025import android.content.ContentResolver;
John Reck30c714c2010-12-16 17:30:34 -080026import android.content.Context;
Grace Kloba22ac16e2009-10-07 18:00:23 -070027import android.content.DialogInterface;
Michael Kolbfe251992010-07-08 15:41:55 -070028import android.content.DialogInterface.OnCancelListener;
Jeff Hamilton8ce956c2010-08-17 11:13:53 -050029import android.content.Intent;
Grace Kloba22ac16e2009-10-07 18:00:23 -070030import android.graphics.Bitmap;
John Reck30c714c2010-12-16 17:30:34 -080031import android.graphics.BitmapFactory;
Grace Kloba22ac16e2009-10-07 18:00:23 -070032import android.net.Uri;
33import android.net.http.SslError;
Grace Kloba22ac16e2009-10-07 18:00:23 -070034import android.os.Bundle;
35import android.os.Message;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +000036import android.os.SystemClock;
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -050037import android.speech.RecognizerResultsIntent;
Grace Kloba22ac16e2009-10-07 18:00:23 -070038import android.util.Log;
39import android.view.KeyEvent;
40import android.view.LayoutInflater;
41import android.view.View;
Grace Kloba50c241e2010-04-20 11:07:50 -070042import android.view.ViewStub;
Ben Murdochc42addf2010-01-28 15:19:59 +000043import android.webkit.ConsoleMessage;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -050044import android.webkit.DownloadListener;
Grace Kloba22ac16e2009-10-07 18:00:23 -070045import android.webkit.GeolocationPermissions;
46import android.webkit.HttpAuthHandler;
47import android.webkit.SslErrorHandler;
48import android.webkit.URLUtil;
49import android.webkit.ValueCallback;
50import android.webkit.WebBackForwardList;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -050051import android.webkit.WebBackForwardListClient;
Grace Kloba22ac16e2009-10-07 18:00:23 -070052import android.webkit.WebChromeClient;
53import android.webkit.WebHistoryItem;
Grace Kloba22ac16e2009-10-07 18:00:23 -070054import android.webkit.WebStorage;
55import android.webkit.WebView;
56import android.webkit.WebViewClient;
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
Michael Kolb8233fac2010-10-26 16:08:53 -0700536 mWebViewController.onPageStarted(Tab.this, view, url, 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 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700560 mWebViewController.onPageFinished(Tab.this, url);
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) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700691 mWebViewController.doUpdateVisitedHistory(Tab.this, url, 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);
1128 builder.setMessage(R.string.autofill_setup_dialog_message)
1129 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1130 @Override
1131 public void onClick(DialogInterface dialog, int id) {
1132 // Take user to the AutoFill profile editor. When they return,
1133 // we will send the message that we pass here which will trigger
1134 // the form to get filled out with their new profile.
1135 mWebViewController.setupAutoFill(msg);
1136 }
1137 })
1138 .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
1139 @Override
1140 public void onClick(DialogInterface dialog, int id) {
1141 // Disable autofill and show a toast with how to turn it on again.
1142 BrowserSettings s = BrowserSettings.getInstance();
1143 s.addObserver(mMainView.getSettings());
1144 s.disableAutoFill(mActivity);
1145 s.update();
1146 Toast.makeText(mActivity, R.string.autofill_setup_dialog_negative_toast,
1147 Toast.LENGTH_LONG).show();
1148 }
1149 }).show();
1150 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001151 };
1152
1153 // -------------------------------------------------------------------------
1154 // WebViewClient implementation for the sub window
1155 // -------------------------------------------------------------------------
1156
1157 // Subclass of WebViewClient used in subwindows to notify the main
1158 // WebViewClient of certain WebView activities.
1159 private static class SubWindowClient extends WebViewClient {
1160 // The main WebViewClient.
1161 private final WebViewClient mClient;
Michael Kolb8233fac2010-10-26 16:08:53 -07001162 private final WebViewController mController;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001163
Michael Kolb8233fac2010-10-26 16:08:53 -07001164 SubWindowClient(WebViewClient client, WebViewController controller) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001165 mClient = client;
Michael Kolb8233fac2010-10-26 16:08:53 -07001166 mController = controller;
Leon Scroggins III211ba542010-04-19 13:21:13 -04001167 }
1168 @Override
1169 public void onPageStarted(WebView view, String url, Bitmap favicon) {
1170 // Unlike the others, do not call mClient's version, which would
1171 // change the progress bar. However, we do want to remove the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001172 // find or select dialog.
Michael Kolb8233fac2010-10-26 16:08:53 -07001173 mController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001174 }
1175 @Override
1176 public void doUpdateVisitedHistory(WebView view, String url,
1177 boolean isReload) {
1178 mClient.doUpdateVisitedHistory(view, url, isReload);
1179 }
1180 @Override
1181 public boolean shouldOverrideUrlLoading(WebView view, String url) {
1182 return mClient.shouldOverrideUrlLoading(view, url);
1183 }
1184 @Override
1185 public void onReceivedSslError(WebView view, SslErrorHandler handler,
1186 SslError error) {
1187 mClient.onReceivedSslError(view, handler, error);
1188 }
1189 @Override
1190 public void onReceivedHttpAuthRequest(WebView view,
1191 HttpAuthHandler handler, String host, String realm) {
1192 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
1193 }
1194 @Override
1195 public void onFormResubmission(WebView view, Message dontResend,
1196 Message resend) {
1197 mClient.onFormResubmission(view, dontResend, resend);
1198 }
1199 @Override
1200 public void onReceivedError(WebView view, int errorCode,
1201 String description, String failingUrl) {
1202 mClient.onReceivedError(view, errorCode, description, failingUrl);
1203 }
1204 @Override
1205 public boolean shouldOverrideKeyEvent(WebView view,
1206 android.view.KeyEvent event) {
1207 return mClient.shouldOverrideKeyEvent(view, event);
1208 }
1209 @Override
1210 public void onUnhandledKeyEvent(WebView view,
1211 android.view.KeyEvent event) {
1212 mClient.onUnhandledKeyEvent(view, event);
1213 }
1214 }
1215
1216 // -------------------------------------------------------------------------
1217 // WebChromeClient implementation for the sub window
1218 // -------------------------------------------------------------------------
1219
1220 private class SubWindowChromeClient extends WebChromeClient {
1221 // The main WebChromeClient.
1222 private final WebChromeClient mClient;
1223
1224 SubWindowChromeClient(WebChromeClient client) {
1225 mClient = client;
1226 }
1227 @Override
1228 public void onProgressChanged(WebView view, int newProgress) {
1229 mClient.onProgressChanged(view, newProgress);
1230 }
1231 @Override
1232 public boolean onCreateWindow(WebView view, boolean dialog,
1233 boolean userGesture, android.os.Message resultMsg) {
1234 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
1235 }
1236 @Override
1237 public void onCloseWindow(WebView window) {
1238 if (window != mSubView) {
1239 Log.e(LOGTAG, "Can't close the window");
1240 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001241 mWebViewController.dismissSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001242 }
1243 }
1244
1245 // -------------------------------------------------------------------------
1246
Michael Kolb8233fac2010-10-26 16:08:53 -07001247 // TODO temporarily use activity here
1248 // remove later
1249
Grace Kloba22ac16e2009-10-07 18:00:23 -07001250 // Construct a new tab
Michael Kolb8233fac2010-10-26 16:08:53 -07001251 Tab(WebViewController wvcontroller, WebView w, boolean closeOnExit, String appId,
Grace Kloba22ac16e2009-10-07 18:00:23 -07001252 String url) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001253 mWebViewController = wvcontroller;
1254 mActivity = mWebViewController.getActivity();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001255 mCloseOnExit = closeOnExit;
1256 mAppId = appId;
John Recke969cc52010-12-21 17:24:43 -08001257 mDataController = DataController.getInstance(mActivity);
John Reck30c714c2010-12-16 17:30:34 -08001258 mCurrentState = new PageState(mActivity, w.isPrivateBrowsingEnabled());
Michael Kolb8233fac2010-10-26 16:08:53 -07001259 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001260 mInForeground = false;
1261
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001262 mDownloadListener = new DownloadListener() {
1263 public void onDownloadStart(String url, String userAgent,
1264 String contentDisposition, String mimetype,
1265 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001266 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001267 mimetype, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001268 }
1269 };
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001270 mWebBackForwardListClient = new WebBackForwardListClient() {
1271 @Override
1272 public void onNewHistoryItem(WebHistoryItem item) {
1273 if (isInVoiceSearchMode()) {
1274 item.setCustomData(mVoiceSearchData.mVoiceSearchIntent);
1275 }
1276 }
1277 @Override
1278 public void onIndexChanged(WebHistoryItem item, int index) {
1279 Object data = item.getCustomData();
1280 if (data != null && data instanceof Intent) {
1281 activateVoiceSearchMode((Intent) data);
1282 }
1283 }
1284 };
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001285
Grace Kloba22ac16e2009-10-07 18:00:23 -07001286 setWebView(w);
1287 }
1288
1289 /**
1290 * Sets the WebView for this tab, correctly removing the old WebView from
1291 * the container view.
1292 */
1293 void setWebView(WebView w) {
1294 if (mMainView == w) {
1295 return;
1296 }
Michael Kolba713ec82010-11-29 17:27:06 -08001297
Grace Kloba22ac16e2009-10-07 18:00:23 -07001298 // If the WebView is changing, the page will be reloaded, so any ongoing
1299 // Geolocation permission requests are void.
Grace Kloba50c241e2010-04-20 11:07:50 -07001300 if (mGeolocationPermissionsPrompt != null) {
1301 mGeolocationPermissionsPrompt.hide();
1302 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001303
Michael Kolba713ec82010-11-29 17:27:06 -08001304 mWebViewController.onSetWebView(this, w);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001305
1306 // set the new one
1307 mMainView = w;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001308 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001309 if (mMainView != null) {
1310 mMainView.setWebViewClient(mWebViewClient);
1311 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001312 // Attach DownloadManager so that downloads can start in an active
1313 // or a non-active window. This can happen when going to a site that
1314 // does a redirect after a period of time. The user could have
1315 // switched to another tab while waiting for the download to start.
1316 mMainView.setDownloadListener(mDownloadListener);
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001317 mMainView.setWebBackForwardListClient(mWebBackForwardListClient);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001318 }
1319 }
1320
1321 /**
1322 * Destroy the tab's main WebView and subWindow if any
1323 */
1324 void destroy() {
1325 if (mMainView != null) {
1326 dismissSubWindow();
1327 BrowserSettings.getInstance().deleteObserver(mMainView.getSettings());
1328 // save the WebView to call destroy() after detach it from the tab
1329 WebView webView = mMainView;
1330 setWebView(null);
1331 webView.destroy();
1332 }
1333 }
1334
1335 /**
1336 * Remove the tab from the parent
1337 */
1338 void removeFromTree() {
1339 // detach the children
1340 if (mChildTabs != null) {
1341 for(Tab t : mChildTabs) {
1342 t.setParentTab(null);
1343 }
1344 }
1345 // remove itself from the parent list
1346 if (mParentTab != null) {
1347 mParentTab.mChildTabs.remove(this);
1348 }
1349 }
1350
1351 /**
1352 * Create a new subwindow unless a subwindow already exists.
1353 * @return True if a new subwindow was created. False if one already exists.
1354 */
1355 boolean createSubWindow() {
1356 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001357 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001358 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001359 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001360 mSubView.setWebChromeClient(new SubWindowChromeClient(
1361 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001362 // Set a different DownloadListener for the mSubView, since it will
1363 // just need to dismiss the mSubView, rather than close the Tab
1364 mSubView.setDownloadListener(new DownloadListener() {
1365 public void onDownloadStart(String url, String userAgent,
1366 String contentDisposition, String mimetype,
1367 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001368 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001369 contentDisposition, mimetype, contentLength);
1370 if (mSubView.copyBackForwardList().getSize() == 0) {
1371 // This subwindow was opened for the sole purpose of
1372 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001373 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001374 }
1375 }
1376 });
Grace Kloba22ac16e2009-10-07 18:00:23 -07001377 mSubView.setOnCreateContextMenuListener(mActivity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001378 return true;
1379 }
1380 return false;
1381 }
1382
1383 /**
1384 * Dismiss the subWindow for the tab.
1385 */
1386 void dismissSubWindow() {
1387 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001388 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001389 BrowserSettings.getInstance().deleteObserver(
1390 mSubView.getSettings());
1391 mSubView.destroy();
1392 mSubView = null;
1393 mSubViewContainer = null;
1394 }
1395 }
1396
Grace Kloba22ac16e2009-10-07 18:00:23 -07001397
1398 /**
1399 * Set the parent tab of this tab.
1400 */
1401 void setParentTab(Tab parent) {
1402 mParentTab = parent;
1403 // This tab may have been freed due to low memory. If that is the case,
1404 // the parent tab index is already saved. If we are changing that index
1405 // (most likely due to removing the parent tab) we must update the
1406 // parent tab index in the saved Bundle.
1407 if (mSavedState != null) {
1408 if (parent == null) {
1409 mSavedState.remove(PARENTTAB);
1410 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -07001411 mSavedState.putInt(PARENTTAB, mWebViewController.getTabControl()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001412 .getTabIndex(parent));
1413 }
1414 }
1415 }
1416
1417 /**
1418 * When a Tab is created through the content of another Tab, then we
1419 * associate the Tabs.
1420 * @param child the Tab that was created from this Tab
1421 */
1422 void addChildTab(Tab child) {
1423 if (mChildTabs == null) {
1424 mChildTabs = new Vector<Tab>();
1425 }
1426 mChildTabs.add(child);
1427 child.setParentTab(this);
1428 }
1429
1430 Vector<Tab> getChildTabs() {
1431 return mChildTabs;
1432 }
1433
1434 void resume() {
1435 if (mMainView != null) {
1436 mMainView.onResume();
1437 if (mSubView != null) {
1438 mSubView.onResume();
1439 }
1440 }
1441 }
1442
1443 void pause() {
1444 if (mMainView != null) {
1445 mMainView.onPause();
1446 if (mSubView != null) {
1447 mSubView.onPause();
1448 }
1449 }
1450 }
1451
1452 void putInForeground() {
1453 mInForeground = true;
1454 resume();
1455 mMainView.setOnCreateContextMenuListener(mActivity);
1456 if (mSubView != null) {
1457 mSubView.setOnCreateContextMenuListener(mActivity);
1458 }
1459 // Show the pending error dialog if the queue is not empty
1460 if (mQueuedErrors != null && mQueuedErrors.size() > 0) {
1461 showError(mQueuedErrors.getFirst());
1462 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001463 mWebViewController.bookmarkedStatusHasChanged(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001464 }
1465
1466 void putInBackground() {
1467 mInForeground = false;
1468 pause();
1469 mMainView.setOnCreateContextMenuListener(null);
1470 if (mSubView != null) {
1471 mSubView.setOnCreateContextMenuListener(null);
1472 }
1473 }
1474
Michael Kolb8233fac2010-10-26 16:08:53 -07001475 boolean inForeground() {
1476 return mInForeground;
1477 }
1478
Grace Kloba22ac16e2009-10-07 18:00:23 -07001479 /**
1480 * Return the top window of this tab; either the subwindow if it is not
1481 * null or the main window.
1482 * @return The top window of this tab.
1483 */
1484 WebView getTopWindow() {
1485 if (mSubView != null) {
1486 return mSubView;
1487 }
1488 return mMainView;
1489 }
1490
1491 /**
1492 * Return the main window of this tab. Note: if a tab is freed in the
1493 * background, this can return null. It is only guaranteed to be
1494 * non-null for the current tab.
1495 * @return The main WebView of this tab.
1496 */
1497 WebView getWebView() {
1498 return mMainView;
1499 }
1500
Michael Kolba713ec82010-11-29 17:27:06 -08001501 void setViewContainer(View container) {
1502 mContainer = container;
1503 }
1504
Michael Kolb8233fac2010-10-26 16:08:53 -07001505 View getViewContainer() {
1506 return mContainer;
1507 }
1508
Grace Kloba22ac16e2009-10-07 18:00:23 -07001509 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001510 * Return whether private browsing is enabled for the main window of
1511 * this tab.
1512 * @return True if private browsing is enabled.
1513 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001514 boolean isPrivateBrowsingEnabled() {
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001515 WebView webView = getWebView();
1516 if (webView == null) {
1517 return false;
1518 }
1519 return webView.isPrivateBrowsingEnabled();
1520 }
1521
1522 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001523 * Return the subwindow of this tab or null if there is no subwindow.
1524 * @return The subwindow of this tab or null.
1525 */
1526 WebView getSubWebView() {
1527 return mSubView;
1528 }
1529
Michael Kolb1514bb72010-11-22 09:11:48 -08001530 void setSubWebView(WebView subView) {
1531 mSubView = subView;
1532 }
1533
Michael Kolb8233fac2010-10-26 16:08:53 -07001534 View getSubViewContainer() {
1535 return mSubViewContainer;
1536 }
1537
Michael Kolb1514bb72010-11-22 09:11:48 -08001538 void setSubViewContainer(View subViewContainer) {
1539 mSubViewContainer = subViewContainer;
1540 }
1541
Grace Kloba22ac16e2009-10-07 18:00:23 -07001542 /**
1543 * @return The geolocation permissions prompt for this tab.
1544 */
1545 GeolocationPermissionsPrompt getGeolocationPermissionsPrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001546 if (mGeolocationPermissionsPrompt == null) {
1547 ViewStub stub = (ViewStub) mContainer
1548 .findViewById(R.id.geolocation_permissions_prompt);
1549 mGeolocationPermissionsPrompt = (GeolocationPermissionsPrompt) stub
1550 .inflate();
1551 mGeolocationPermissionsPrompt.init();
1552 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001553 return mGeolocationPermissionsPrompt;
1554 }
1555
1556 /**
1557 * @return The application id string
1558 */
1559 String getAppId() {
1560 return mAppId;
1561 }
1562
1563 /**
1564 * Set the application id string
1565 * @param id
1566 */
1567 void setAppId(String id) {
1568 mAppId = id;
1569 }
1570
Grace Kloba22ac16e2009-10-07 18:00:23 -07001571 String getUrl() {
John Reckbafe58a2011-01-11 10:26:02 -08001572 if (HomeProvider.MOST_VISITED.equals(mCurrentState.mUrl)) {
1573 return "";
1574 }
John Reck30c714c2010-12-16 17:30:34 -08001575 return mCurrentState.mUrl;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001576 }
1577
1578 /**
John Reck30c714c2010-12-16 17:30:34 -08001579 * Get the title of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001580 */
1581 String getTitle() {
John Reck30c714c2010-12-16 17:30:34 -08001582 if (mCurrentState.mTitle == null && mInPageLoad) {
1583 return mActivity.getString(R.string.title_bar_loading);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001584 }
John Reck30c714c2010-12-16 17:30:34 -08001585 return mCurrentState.mTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001586 }
1587
1588 /**
John Reck30c714c2010-12-16 17:30:34 -08001589 * Get the favicon of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001590 */
1591 Bitmap getFavicon() {
John Reck30c714c2010-12-16 17:30:34 -08001592 return mCurrentState.mFavicon;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001593 }
1594
John Recke969cc52010-12-21 17:24:43 -08001595 public boolean isBookmarkedSite() {
1596 return mCurrentState.mIsBookmarkedSite;
1597 }
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001598
Grace Kloba22ac16e2009-10-07 18:00:23 -07001599 /**
1600 * Return the tab's error console. Creates the console if createIfNEcessary
1601 * is true and we haven't already created the console.
1602 * @param createIfNecessary Flag to indicate if the console should be
1603 * created if it has not been already.
1604 * @return The tab's error console, or null if one has not been created and
1605 * createIfNecessary is false.
1606 */
1607 ErrorConsoleView getErrorConsole(boolean createIfNecessary) {
1608 if (createIfNecessary && mErrorConsole == null) {
1609 mErrorConsole = new ErrorConsoleView(mActivity);
1610 mErrorConsole.setWebView(mMainView);
1611 }
1612 return mErrorConsole;
1613 }
1614
1615 /**
1616 * If this Tab was created through another Tab, then this method returns
1617 * that Tab.
1618 * @return the Tab parent or null
1619 */
1620 public Tab getParentTab() {
1621 return mParentTab;
1622 }
1623
1624 /**
1625 * Return whether this tab should be closed when it is backing out of the
1626 * first page.
1627 * @return TRUE if this tab should be closed when exit.
1628 */
1629 boolean closeOnExit() {
1630 return mCloseOnExit;
1631 }
1632
John Reck30c714c2010-12-16 17:30:34 -08001633 private void setLockIconType(LockIcon icon) {
1634 mCurrentState.mLockIcon = icon;
1635 mWebViewController.onUpdatedLockIcon(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001636 }
1637
1638 /**
1639 * @return The tab's lock icon type.
1640 */
John Reck30c714c2010-12-16 17:30:34 -08001641 LockIcon getLockIconType() {
1642 return mCurrentState.mLockIcon;
1643 }
1644
1645 int getLoadProgress() {
1646 if (mInPageLoad) {
1647 return mPageLoadProgress;
1648 }
1649 return 100;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001650 }
1651
1652 /**
1653 * @return TRUE if onPageStarted is called while onPageFinished is not
1654 * called yet.
1655 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001656 boolean inPageLoad() {
1657 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001658 }
1659
1660 // force mInLoad to be false. This should only be called before closing the
1661 // tab to ensure BrowserActivity's pauseWebViewTimers() is called correctly.
Michael Kolb8233fac2010-10-26 16:08:53 -07001662 void clearInPageLoad() {
1663 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001664 }
1665
Grace Kloba22ac16e2009-10-07 18:00:23 -07001666 /**
John Reck30c714c2010-12-16 17:30:34 -08001667 * Get the cached saved state bundle.
1668 * @return cached state bundle
Grace Kloba22ac16e2009-10-07 18:00:23 -07001669 */
1670 Bundle getSavedState() {
1671 return mSavedState;
1672 }
1673
1674 /**
1675 * Set the saved state.
1676 */
1677 void setSavedState(Bundle state) {
1678 mSavedState = state;
1679 }
1680
1681 /**
1682 * @return TRUE if succeed in saving the state.
1683 */
1684 boolean saveState() {
1685 // If the WebView is null it means we ran low on memory and we already
1686 // stored the saved state in mSavedState.
1687 if (mMainView == null) {
1688 return mSavedState != null;
1689 }
1690
1691 mSavedState = new Bundle();
1692 final WebBackForwardList list = mMainView.saveState(mSavedState);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001693
1694 // Store some extra info for displaying the tab in the picker.
1695 final WebHistoryItem item = list != null ? list.getCurrentItem() : null;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001696
John Reck30c714c2010-12-16 17:30:34 -08001697 mSavedState.putString(CURRURL, mCurrentState.mUrl);
1698 mSavedState.putString(CURRTITLE, mCurrentState.mTitle);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001699 mSavedState.putBoolean(CLOSEONEXIT, mCloseOnExit);
1700 if (mAppId != null) {
1701 mSavedState.putString(APPID, mAppId);
1702 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001703 // Remember the parent tab so the relationship can be restored.
1704 if (mParentTab != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001705 mSavedState.putInt(PARENTTAB, mWebViewController.getTabControl().getTabIndex(
Grace Kloba22ac16e2009-10-07 18:00:23 -07001706 mParentTab));
1707 }
1708 return true;
1709 }
1710
1711 /*
1712 * Restore the state of the tab.
1713 */
1714 boolean restoreState(Bundle b) {
1715 if (b == null) {
1716 return false;
1717 }
1718 // Restore the internal state even if the WebView fails to restore.
1719 // This will maintain the app id, original url and close-on-exit values.
1720 mSavedState = null;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001721 mCloseOnExit = b.getBoolean(CLOSEONEXIT);
1722 mAppId = b.getString(APPID);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001723
1724 final WebBackForwardList list = mMainView.restoreState(b);
1725 if (list == null) {
1726 return false;
1727 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001728 return true;
1729 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04001730
Leon Scroggins1961ed22010-12-07 15:22:21 -05001731 public void updateBookmarkedStatus() {
John Recke969cc52010-12-21 17:24:43 -08001732 mDataController.queryBookmarkStatus(getUrl(), mIsBookmarkCallback);
Leon Scroggins1961ed22010-12-07 15:22:21 -05001733 }
1734
John Recke969cc52010-12-21 17:24:43 -08001735 private DataController.OnQueryUrlIsBookmark mIsBookmarkCallback
1736 = new DataController.OnQueryUrlIsBookmark() {
1737 @Override
1738 public void onQueryUrlIsBookmark(String url, boolean isBookmark) {
1739 if (mCurrentState.mUrl.equals(url)) {
1740 mCurrentState.mIsBookmarkedSite = isBookmark;
1741 mWebViewController.bookmarkedStatusHasChanged(Tab.this);
1742 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001743 }
John Recke969cc52010-12-21 17:24:43 -08001744 };
Grace Kloba22ac16e2009-10-07 18:00:23 -07001745}