blob: 8368c3326aea106271deb0382a3bc80e93c263c1 [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 Reck438bf462011-01-12 18:11:46 -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;
John Reck438bf462011-01-12 18:11:46 -080054import android.webkit.WebResourceResponse;
Grace Kloba22ac16e2009-10-07 18:00:23 -070055import android.webkit.WebStorage;
56import android.webkit.WebView;
57import android.webkit.WebViewClient;
Ben Murdoch1d676b62011-01-17 12:54:24 +000058import android.widget.CheckBox;
Grace Kloba22ac16e2009-10-07 18:00:23 -070059import android.widget.LinearLayout;
60import android.widget.TextView;
Ben Murdoch8029a772010-11-16 11:58:21 +000061import android.widget.Toast;
Grace Kloba22ac16e2009-10-07 18:00:23 -070062
Michael Kolbfe251992010-07-08 15:41:55 -070063import java.util.ArrayList;
64import java.util.HashMap;
65import java.util.Iterator;
66import java.util.LinkedList;
67import java.util.Map;
68import java.util.Vector;
69
Grace Kloba22ac16e2009-10-07 18:00:23 -070070/**
71 * Class for maintaining Tabs with a main WebView and a subwindow.
72 */
73class Tab {
Michael Kolb8233fac2010-10-26 16:08:53 -070074
Grace Kloba22ac16e2009-10-07 18:00:23 -070075 // Log Tag
76 private static final String LOGTAG = "Tab";
Ben Murdochc42addf2010-01-28 15:19:59 +000077 // Special case the logtag for messages for the Console to make it easier to
78 // filter them and match the logtag used for these messages in older versions
79 // of the browser.
80 private static final String CONSOLE_LOGTAG = "browser";
81
John Reck30c714c2010-12-16 17:30:34 -080082 public enum LockIcon {
83 LOCK_ICON_UNSECURE,
84 LOCK_ICON_SECURE,
85 LOCK_ICON_MIXED,
86 }
Michael Kolb8233fac2010-10-26 16:08:53 -070087
88 Activity mActivity;
89 private WebViewController mWebViewController;
90
Grace Kloba22ac16e2009-10-07 18:00:23 -070091 // The Geolocation permissions prompt
92 private GeolocationPermissionsPrompt mGeolocationPermissionsPrompt;
93 // Main WebView wrapper
Michael Kolba713ec82010-11-29 17:27:06 -080094 private View mContainer;
Grace Kloba22ac16e2009-10-07 18:00:23 -070095 // Main WebView
96 private WebView mMainView;
97 // Subwindow container
98 private View mSubViewContainer;
99 // Subwindow WebView
100 private WebView mSubView;
101 // Saved bundle for when we are running low on memory. It contains the
102 // information needed to restore the WebView if the user goes back to the
103 // tab.
104 private Bundle mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700105 // Parent Tab. This is the Tab that created this Tab, or null if the Tab was
106 // created by the UI
107 private Tab mParentTab;
108 // Tab that constructed by this Tab. This is used when this Tab is
109 // destroyed, it clears all mParentTab values in the children.
110 private Vector<Tab> mChildTabs;
111 // If true, the tab will be removed when back out of the first page.
112 private boolean mCloseOnExit;
113 // If true, the tab is in the foreground of the current activity.
114 private boolean mInForeground;
Michael Kolb8233fac2010-10-26 16:08:53 -0700115 // If true, the tab is in page loading state (after onPageStarted,
116 // before onPageFinsihed)
117 private boolean mInPageLoad;
John Reck30c714c2010-12-16 17:30:34 -0800118 // The last reported progress of the current page
119 private int mPageLoadProgress;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000120 // The time the load started, used to find load page time
121 private long mLoadStartTime;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700122 // Application identifier used to find tabs that another application wants
123 // to reuse.
124 private String mAppId;
125 // Keep the original url around to avoid killing the old WebView if the url
126 // has not changed.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700127 // Error console for the tab
128 private ErrorConsoleView mErrorConsole;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -0500129 // The listener that gets invoked when a download is started from the
130 // mMainView
131 private final DownloadListener mDownloadListener;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500132 // Listener used to know when we move forward or back in the history list.
133 private final WebBackForwardListClient mWebBackForwardListClient;
John Recke969cc52010-12-21 17:24:43 -0800134 private DataController mDataController;
Patrick Scott92066772011-03-10 08:46:27 -0500135 // State of the auto-login request.
136 private DeviceAccountLogin mDeviceAccountLogin;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700137
138 // AsyncTask for downloading touch icons
139 DownloadTouchIcon mTouchIconLoader;
140
Michael Kolbeb95db42011-03-03 10:38:40 -0800141 private Bitmap mScreenshot;
142
John Reck30c714c2010-12-16 17:30:34 -0800143 // All the state needed for a page
144 private static class PageState {
145 String mUrl;
146 String mTitle;
147 LockIcon mLockIcon;
148 Bitmap mFavicon;
John Recke969cc52010-12-21 17:24:43 -0800149 Boolean mIsBookmarkedSite = false;
John Reck30c714c2010-12-16 17:30:34 -0800150
151 PageState(Context c, boolean incognito) {
152 if (incognito) {
153 mUrl = "browser:incognito";
154 mTitle = c.getString(R.string.new_incognito_tab);
John Reck30c714c2010-12-16 17:30:34 -0800155 } else {
156 mUrl = "";
157 mTitle = c.getString(R.string.new_tab);
John Reck30c714c2010-12-16 17:30:34 -0800158 }
Justin Hodc6cbb72011-01-24 15:14:54 -0800159 mFavicon = BitmapFactory.decodeResource(
160 c.getResources(), R.drawable.app_web_browser_sm);
John Reck30c714c2010-12-16 17:30:34 -0800161 mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
162 }
163
164 PageState(Context c, boolean incognito, String url, Bitmap favicon) {
165 mUrl = url;
166 mTitle = null;
167 if (URLUtil.isHttpsUrl(url)) {
168 mLockIcon = LockIcon.LOCK_ICON_SECURE;
169 } else {
170 mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
171 }
172 if (favicon != null) {
173 mFavicon = favicon;
174 } else {
Justin Hodc6cbb72011-01-24 15:14:54 -0800175 mFavicon = BitmapFactory.decodeResource(
176 c.getResources(), R.drawable.app_web_browser_sm);
John Reck30c714c2010-12-16 17:30:34 -0800177 }
178 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700179 }
180
John Reck30c714c2010-12-16 17:30:34 -0800181 // The current/loading page's state
182 private PageState mCurrentState;
183
Grace Kloba22ac16e2009-10-07 18:00:23 -0700184 // Used for saving and restoring each Tab
John Reck30c714c2010-12-16 17:30:34 -0800185 // TODO: Figure out who uses what and where
186 // Some of these aren't use in this class, and some are only used in
187 // restoring state but not saving it - FIX THIS
Grace Kloba22ac16e2009-10-07 18:00:23 -0700188 static final String WEBVIEW = "webview";
189 static final String NUMTABS = "numTabs";
190 static final String CURRTAB = "currentTab";
191 static final String CURRURL = "currentUrl";
192 static final String CURRTITLE = "currentTitle";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700193 static final String CLOSEONEXIT = "closeonexit";
194 static final String PARENTTAB = "parentTab";
195 static final String APPID = "appid";
196 static final String ORIGINALURL = "originalUrl";
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700197 static final String INCOGNITO = "privateBrowsingEnabled";
Michael Kolbeb95db42011-03-03 10:38:40 -0800198 static final String SCREENSHOT = "screenshot";
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 Kolb11d19782011-03-20 10:17:40 -0700316 mWebViewController.activateVoiceSearchMode(
317 mVoiceSearchData.mLastVoiceSearchTitle,
318 mVoiceSearchData.mVoiceSearchResults);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500319 }
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500320 if (mVoiceSearchData.mVoiceSearchHtmls != null) {
321 // When index was found it was already ensured that it was valid
322 String uriString = mVoiceSearchData.mVoiceSearchHtmls.get(index);
323 if (uriString != null) {
324 Uri dataUri = Uri.parse(uriString);
325 if (RecognizerResultsIntent.URI_SCHEME_INLINE.equals(
326 dataUri.getScheme())) {
327 // If there is only one base URL, use it. If there are
328 // more, there will be one for each index, so use the base
329 // URL corresponding to the index.
330 String baseUrl = mVoiceSearchData.mVoiceSearchBaseUrls.get(
331 mVoiceSearchData.mVoiceSearchBaseUrls.size() > 1 ?
332 index : 0);
333 mVoiceSearchData.mLastVoiceSearchUrl = baseUrl;
334 mMainView.loadDataWithBaseURL(baseUrl,
335 uriString.substring(RecognizerResultsIntent
336 .URI_SCHEME_INLINE.length() + 1), "text/html",
337 "utf-8", baseUrl);
338 return;
339 }
340 }
341 }
Leon Scroggins58d56c62010-01-28 15:12:40 -0500342 mVoiceSearchData.mLastVoiceSearchUrl
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500343 = mVoiceSearchData.mVoiceSearchUrls.get(index);
344 if (null == mVoiceSearchData.mLastVoiceSearchUrl) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700345 mVoiceSearchData.mLastVoiceSearchUrl = UrlUtils.smartUrlFilter(
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500346 mVoiceSearchData.mLastVoiceSearchTitle);
347 }
Leon Scroggins9df94972010-03-08 18:20:35 -0500348 Map<String, String> headers = null;
349 if (mVoiceSearchData.mHeaders != null) {
350 int bundleIndex = mVoiceSearchData.mHeaders.size() == 1 ? 0
351 : index;
352 Bundle bundle = mVoiceSearchData.mHeaders.get(bundleIndex);
353 if (bundle != null && !bundle.isEmpty()) {
354 Iterator<String> iter = bundle.keySet().iterator();
355 headers = new HashMap<String, String>();
356 while (iter.hasNext()) {
357 String key = iter.next();
358 headers.put(key, bundle.getString(key));
359 }
360 }
361 }
362 mMainView.loadUrl(mVoiceSearchData.mLastVoiceSearchUrl, headers);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500363 }
364 /* package */ static class VoiceSearchData {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500365 public VoiceSearchData(ArrayList<String> results,
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500366 ArrayList<String> urls, ArrayList<String> htmls,
367 ArrayList<String> baseUrls) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500368 mVoiceSearchResults = results;
369 mVoiceSearchUrls = urls;
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500370 mVoiceSearchHtmls = htmls;
371 mVoiceSearchBaseUrls = baseUrls;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500372 }
373 /*
374 * ArrayList of suggestions to be displayed when opening the
375 * SearchManager
376 */
377 public ArrayList<String> mVoiceSearchResults;
378 /*
379 * ArrayList of urls, associated with the suggestions in
380 * mVoiceSearchResults.
381 */
382 public ArrayList<String> mVoiceSearchUrls;
383 /*
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500384 * ArrayList holding content to load for each item in
385 * mVoiceSearchResults.
386 */
387 public ArrayList<String> mVoiceSearchHtmls;
388 /*
389 * ArrayList holding base urls for the items in mVoiceSearchResults.
390 * If non null, this will either have the same size as
391 * mVoiceSearchResults or have a size of 1, in which case all will use
392 * the same base url
393 */
394 public ArrayList<String> mVoiceSearchBaseUrls;
395 /*
Leon Scroggins58d56c62010-01-28 15:12:40 -0500396 * The last url provided by voice search. Used for comparison to see if
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500397 * we are going to a page by some method besides voice search.
Leon Scroggins58d56c62010-01-28 15:12:40 -0500398 */
399 public String mLastVoiceSearchUrl;
400 /**
401 * The last title used for voice search. Needed to update the title bar
402 * when switching tabs.
403 */
404 public String mLastVoiceSearchTitle;
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500405 /**
406 * Whether the Intent which turned on voice search mode contained the
407 * String signifying that Google was the source.
408 */
409 public boolean mSourceIsGoogle;
410 /**
Leon Scroggins9df94972010-03-08 18:20:35 -0500411 * List of headers to be passed into the WebView containing location
412 * information
413 */
414 public ArrayList<Bundle> mHeaders;
415 /**
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500416 * The Intent used to invoke voice search. Placed on the
417 * WebHistoryItem so that when coming back to a previous voice search
418 * page we can again activate voice search.
419 */
Leon Scrogginse10dde52010-03-08 19:53:03 -0500420 public Intent mVoiceSearchIntent;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500421 /**
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500422 * String used to identify Google as the source of voice search.
423 */
424 public static String SOURCE_IS_GOOGLE
425 = "android.speech.extras.SOURCE_IS_GOOGLE";
Leon Scroggins58d56c62010-01-28 15:12:40 -0500426 }
427
Grace Kloba22ac16e2009-10-07 18:00:23 -0700428 // Container class for the next error dialog that needs to be displayed
429 private class ErrorDialog {
430 public final int mTitle;
431 public final String mDescription;
432 public final int mError;
433 ErrorDialog(int title, String desc, int error) {
434 mTitle = title;
435 mDescription = desc;
436 mError = error;
437 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700438 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700439
440 private void processNextError() {
441 if (mQueuedErrors == null) {
442 return;
443 }
444 // The first one is currently displayed so just remove it.
445 mQueuedErrors.removeFirst();
446 if (mQueuedErrors.size() == 0) {
447 mQueuedErrors = null;
448 return;
449 }
450 showError(mQueuedErrors.getFirst());
451 }
452
453 private DialogInterface.OnDismissListener mDialogListener =
454 new DialogInterface.OnDismissListener() {
455 public void onDismiss(DialogInterface d) {
456 processNextError();
457 }
458 };
459 private LinkedList<ErrorDialog> mQueuedErrors;
460
461 private void queueError(int err, String desc) {
462 if (mQueuedErrors == null) {
463 mQueuedErrors = new LinkedList<ErrorDialog>();
464 }
465 for (ErrorDialog d : mQueuedErrors) {
466 if (d.mError == err) {
467 // Already saw a similar error, ignore the new one.
468 return;
469 }
470 }
471 ErrorDialog errDialog = new ErrorDialog(
472 err == WebViewClient.ERROR_FILE_NOT_FOUND ?
473 R.string.browserFrameFileErrorLabel :
474 R.string.browserFrameNetworkErrorLabel,
475 desc, err);
476 mQueuedErrors.addLast(errDialog);
477
478 // Show the dialog now if the queue was empty and it is in foreground
479 if (mQueuedErrors.size() == 1 && mInForeground) {
480 showError(errDialog);
481 }
482 }
483
484 private void showError(ErrorDialog errDialog) {
485 if (mInForeground) {
486 AlertDialog d = new AlertDialog.Builder(mActivity)
487 .setTitle(errDialog.mTitle)
488 .setMessage(errDialog.mDescription)
489 .setPositiveButton(R.string.ok, null)
490 .create();
491 d.setOnDismissListener(mDialogListener);
492 d.show();
493 }
494 }
495
496 // -------------------------------------------------------------------------
497 // WebViewClient implementation for the main WebView
498 // -------------------------------------------------------------------------
499
500 private final WebViewClient mWebViewClient = new WebViewClient() {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500501 private Message mDontResend;
502 private Message mResend;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700503 @Override
504 public void onPageStarted(WebView view, String url, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700505 mInPageLoad = true;
John Reck30c714c2010-12-16 17:30:34 -0800506 mPageLoadProgress = 0;
507 mCurrentState = new PageState(mActivity,
508 view.isPrivateBrowsingEnabled(), url, favicon);
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000509 mLoadStartTime = SystemClock.uptimeMillis();
Leon Scroggins58d56c62010-01-28 15:12:40 -0500510 if (mVoiceSearchData != null
511 && !url.equals(mVoiceSearchData.mLastVoiceSearchUrl)) {
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500512 if (mVoiceSearchData.mSourceIsGoogle) {
513 Intent i = new Intent(LoggingEvents.ACTION_LOG_EVENT);
514 i.putExtra(LoggingEvents.EXTRA_FLUSH, true);
515 mActivity.sendBroadcast(i);
516 }
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400517 revertVoiceSearchMode();
Leon Scroggins58d56c62010-01-28 15:12:40 -0500518 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700519
Grace Kloba22ac16e2009-10-07 18:00:23 -0700520
521 // If we start a touch icon load and then load a new page, we don't
522 // want to cancel the current touch icon loader. But, we do want to
523 // create a new one when the touch icon url is known.
524 if (mTouchIconLoader != null) {
525 mTouchIconLoader.mTab = null;
526 mTouchIconLoader = null;
527 }
528
529 // reset the error console
530 if (mErrorConsole != null) {
531 mErrorConsole.clearErrorMessages();
Michael Kolb8233fac2010-10-26 16:08:53 -0700532 if (mWebViewController.shouldShowErrorConsole()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700533 mErrorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
534 }
535 }
536
Patrick Scott92066772011-03-10 08:46:27 -0500537 // Cancel the auto-login process.
538 if (mDeviceAccountLogin != null) {
539 mDeviceAccountLogin.cancel();
540 mDeviceAccountLogin = null;
541 mWebViewController.hideAutoLogin(Tab.this);
542 }
543
Grace Kloba22ac16e2009-10-07 18:00:23 -0700544 // finally update the UI in the activity if it is in the foreground
John Reck324d4402011-01-11 16:56:42 -0800545 mWebViewController.onPageStarted(Tab.this, view, favicon);
Leon Scroggins4cd97792010-12-03 15:31:56 -0500546
John Recke969cc52010-12-21 17:24:43 -0800547 updateBookmarkedStatus();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700548 }
549
550 @Override
551 public void onPageFinished(WebView view, String url) {
John Reck5b691842010-11-29 11:21:13 -0800552 if (!isPrivateBrowsingEnabled()) {
553 LogTag.logPageFinishedLoading(
554 url, SystemClock.uptimeMillis() - mLoadStartTime);
555 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700556 mInPageLoad = false;
John Reck30c714c2010-12-16 17:30:34 -0800557 // Sync state (in case of stop/timeout)
558 mCurrentState.mUrl = view.getUrl();
John Reck6c702ee2011-01-07 09:41:53 -0800559 if (mCurrentState.mUrl == null) {
560 mCurrentState.mUrl = url != null ? url : "";
561 }
John Reck30c714c2010-12-16 17:30:34 -0800562 mCurrentState.mTitle = view.getTitle();
563 mCurrentState.mFavicon = view.getFavicon();
564 if (!URLUtil.isHttpsUrl(mCurrentState.mUrl)) {
565 // In case we stop when loading an HTTPS page from an HTTP page
566 // but before a provisional load occurred
567 mCurrentState.mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
568 }
John Reck324d4402011-01-11 16:56:42 -0800569 mWebViewController.onPageFinished(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700570 }
571
572 // return true if want to hijack the url to let another app to handle it
573 @Override
574 public boolean shouldOverrideUrlLoading(WebView view, String url) {
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400575 if (voiceSearchSourceIsGoogle()) {
576 // This method is called when the user clicks on a link.
577 // VoiceSearchMode is turned off when the user leaves the
578 // Google results page, so at this point the user must be on
579 // that page. If the user clicked a link on that page, assume
580 // that the voice search was effective, and broadcast an Intent
581 // so a receiver can take note of that fact.
582 Intent logIntent = new Intent(LoggingEvents.ACTION_LOG_EVENT);
583 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
584 LoggingEvents.VoiceSearch.RESULT_CLICKED);
585 mActivity.sendBroadcast(logIntent);
586 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700587 if (mInForeground) {
Michael Kolb18eb3772010-12-10 14:29:51 -0800588 return mWebViewController.shouldOverrideUrlLoading(Tab.this,
589 view, url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700590 } else {
591 return false;
592 }
593 }
594
595 /**
596 * Updates the lock icon. This method is called when we discover another
597 * resource to be loaded for this page (for example, javascript). While
598 * we update the icon type, we do not update the lock icon itself until
599 * we are done loading, it is slightly more secure this way.
600 */
601 @Override
602 public void onLoadResource(WebView view, String url) {
603 if (url != null && url.length() > 0) {
604 // It is only if the page claims to be secure that we may have
605 // to update the lock:
John Reck30c714c2010-12-16 17:30:34 -0800606 if (mCurrentState.mLockIcon == LockIcon.LOCK_ICON_SECURE) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700607 // If NOT a 'safe' url, change the lock to mixed content!
608 if (!(URLUtil.isHttpsUrl(url) || URLUtil.isDataUrl(url)
609 || URLUtil.isAboutUrl(url))) {
John Reck30c714c2010-12-16 17:30:34 -0800610 mCurrentState.mLockIcon = LockIcon.LOCK_ICON_MIXED;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700611 }
612 }
613 }
614 }
615
616 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700617 * Show a dialog informing the user of the network error reported by
618 * WebCore if it is in the foreground.
619 */
620 @Override
621 public void onReceivedError(WebView view, int errorCode,
622 String description, String failingUrl) {
623 if (errorCode != WebViewClient.ERROR_HOST_LOOKUP &&
624 errorCode != WebViewClient.ERROR_CONNECT &&
625 errorCode != WebViewClient.ERROR_BAD_URL &&
626 errorCode != WebViewClient.ERROR_UNSUPPORTED_SCHEME &&
627 errorCode != WebViewClient.ERROR_FILE) {
628 queueError(errorCode, description);
629 }
Jeff Hamilton47654f42010-09-07 09:57:51 -0500630
631 // Don't log URLs when in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -0700632 if (!isPrivateBrowsingEnabled()) {
Jeff Hamilton47654f42010-09-07 09:57:51 -0500633 Log.e(LOGTAG, "onReceivedError " + errorCode + " " + failingUrl
634 + " " + description);
635 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700636 }
637
638 /**
639 * Check with the user if it is ok to resend POST data as the page they
640 * are trying to navigate to is the result of a POST.
641 */
642 @Override
643 public void onFormResubmission(WebView view, final Message dontResend,
644 final Message resend) {
645 if (!mInForeground) {
646 dontResend.sendToTarget();
647 return;
648 }
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500649 if (mDontResend != null) {
650 Log.w(LOGTAG, "onFormResubmission should not be called again "
651 + "while dialog is still up");
652 dontResend.sendToTarget();
653 return;
654 }
655 mDontResend = dontResend;
656 mResend = resend;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700657 new AlertDialog.Builder(mActivity).setTitle(
658 R.string.browserFrameFormResubmitLabel).setMessage(
659 R.string.browserFrameFormResubmitMessage)
660 .setPositiveButton(R.string.ok,
661 new DialogInterface.OnClickListener() {
662 public void onClick(DialogInterface dialog,
663 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500664 if (mResend != null) {
665 mResend.sendToTarget();
666 mResend = null;
667 mDontResend = null;
668 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700669 }
670 }).setNegativeButton(R.string.cancel,
671 new DialogInterface.OnClickListener() {
672 public void onClick(DialogInterface dialog,
673 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500674 if (mDontResend != null) {
675 mDontResend.sendToTarget();
676 mResend = null;
677 mDontResend = null;
678 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700679 }
680 }).setOnCancelListener(new OnCancelListener() {
681 public void onCancel(DialogInterface dialog) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500682 if (mDontResend != null) {
683 mDontResend.sendToTarget();
684 mResend = null;
685 mDontResend = null;
686 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700687 }
688 }).show();
689 }
690
691 /**
692 * Insert the url into the visited history database.
693 * @param url The url to be inserted.
694 * @param isReload True if this url is being reloaded.
695 * FIXME: Not sure what to do when reloading the page.
696 */
697 @Override
698 public void doUpdateVisitedHistory(WebView view, String url,
699 boolean isReload) {
John Reck324d4402011-01-11 16:56:42 -0800700 mWebViewController.doUpdateVisitedHistory(Tab.this, isReload);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700701 }
702
703 /**
704 * Displays SSL error(s) dialog to the user.
705 */
706 @Override
707 public void onReceivedSslError(final WebView view,
708 final SslErrorHandler handler, final SslError error) {
709 if (!mInForeground) {
710 handler.cancel();
John Reck30c714c2010-12-16 17:30:34 -0800711 setLockIconType(LockIcon.LOCK_ICON_UNSECURE);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700712 return;
713 }
714 if (BrowserSettings.getInstance().showSecurityWarnings()) {
715 final LayoutInflater factory =
716 LayoutInflater.from(mActivity);
717 final View warningsView =
718 factory.inflate(R.layout.ssl_warnings, null);
719 final LinearLayout placeholder =
720 (LinearLayout)warningsView.findViewById(R.id.placeholder);
721
722 if (error.hasError(SslError.SSL_UNTRUSTED)) {
723 LinearLayout ll = (LinearLayout)factory
724 .inflate(R.layout.ssl_warning, null);
725 ((TextView)ll.findViewById(R.id.warning))
726 .setText(R.string.ssl_untrusted);
727 placeholder.addView(ll);
728 }
729
730 if (error.hasError(SslError.SSL_IDMISMATCH)) {
731 LinearLayout ll = (LinearLayout)factory
732 .inflate(R.layout.ssl_warning, null);
733 ((TextView)ll.findViewById(R.id.warning))
734 .setText(R.string.ssl_mismatch);
735 placeholder.addView(ll);
736 }
737
738 if (error.hasError(SslError.SSL_EXPIRED)) {
739 LinearLayout ll = (LinearLayout)factory
740 .inflate(R.layout.ssl_warning, null);
741 ((TextView)ll.findViewById(R.id.warning))
742 .setText(R.string.ssl_expired);
743 placeholder.addView(ll);
744 }
745
746 if (error.hasError(SslError.SSL_NOTYETVALID)) {
747 LinearLayout ll = (LinearLayout)factory
748 .inflate(R.layout.ssl_warning, null);
749 ((TextView)ll.findViewById(R.id.warning))
750 .setText(R.string.ssl_not_yet_valid);
751 placeholder.addView(ll);
752 }
753
754 new AlertDialog.Builder(mActivity).setTitle(
755 R.string.security_warning).setIcon(
756 android.R.drawable.ic_dialog_alert).setView(
757 warningsView).setPositiveButton(R.string.ssl_continue,
758 new DialogInterface.OnClickListener() {
759 public void onClick(DialogInterface dialog,
760 int whichButton) {
761 handler.proceed();
762 }
763 }).setNeutralButton(R.string.view_certificate,
764 new DialogInterface.OnClickListener() {
765 public void onClick(DialogInterface dialog,
766 int whichButton) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700767 mWebViewController.showSslCertificateOnError(view,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700768 handler, error);
769 }
Ben Murdocha49b8292010-11-16 11:56:04 +0000770 }).setNegativeButton(R.string.ssl_go_back,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700771 new DialogInterface.OnClickListener() {
772 public void onClick(DialogInterface dialog,
773 int whichButton) {
John Reck30c714c2010-12-16 17:30:34 -0800774 dialog.cancel();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700775 }
776 }).setOnCancelListener(
777 new DialogInterface.OnCancelListener() {
778 public void onCancel(DialogInterface dialog) {
779 handler.cancel();
John Reck30c714c2010-12-16 17:30:34 -0800780 setLockIconType(LockIcon.LOCK_ICON_UNSECURE);
781 mWebViewController.onUserCanceledSsl(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700782 }
783 }).show();
784 } else {
785 handler.proceed();
786 }
787 }
788
789 /**
790 * Handles an HTTP authentication request.
791 *
792 * @param handler The authentication handler
793 * @param host The host
794 * @param realm The realm
795 */
796 @Override
797 public void onReceivedHttpAuthRequest(WebView view,
798 final HttpAuthHandler handler, final String host,
799 final String realm) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700800 mWebViewController.onReceivedHttpAuthRequest(Tab.this, view, handler, host, realm);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700801 }
802
803 @Override
John Reck438bf462011-01-12 18:11:46 -0800804 public WebResourceResponse shouldInterceptRequest(WebView view,
805 String url) {
806 WebResourceResponse res = HomeProvider.shouldInterceptRequest(
807 mActivity, url);
808 return res;
809 }
810
811 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700812 public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
813 if (!mInForeground) {
814 return false;
815 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700816 return mWebViewController.shouldOverrideKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700817 }
818
819 @Override
820 public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700821 if (!mInForeground) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700822 return;
823 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700824 mWebViewController.onUnhandledKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700825 }
Patrick Scott92066772011-03-10 08:46:27 -0500826
827 @Override
828 public void onReceivedLoginRequest(WebView view, String realm,
829 String account, String args) {
830 new DeviceAccountLogin(mActivity, view, Tab.this, mWebViewController)
831 .handleLogin(realm, account, args);
832 }
833
Grace Kloba22ac16e2009-10-07 18:00:23 -0700834 };
835
Patrick Scott92066772011-03-10 08:46:27 -0500836 // Called by DeviceAccountLogin when the Tab needs to have the auto-login UI
837 // displayed.
838 void setDeviceAccountLogin(DeviceAccountLogin login) {
839 mDeviceAccountLogin = login;
840 }
841
842 // Returns non-null if the title bar should display the auto-login UI.
843 DeviceAccountLogin getDeviceAccountLogin() {
844 return mDeviceAccountLogin;
845 }
846
Grace Kloba22ac16e2009-10-07 18:00:23 -0700847 // -------------------------------------------------------------------------
848 // WebChromeClient implementation for the main WebView
849 // -------------------------------------------------------------------------
850
851 private final WebChromeClient mWebChromeClient = new WebChromeClient() {
852 // Helper method to create a new tab or sub window.
853 private void createWindow(final boolean dialog, final Message msg) {
854 WebView.WebViewTransport transport =
855 (WebView.WebViewTransport) msg.obj;
856 if (dialog) {
857 createSubWindow();
Michael Kolb8233fac2010-10-26 16:08:53 -0700858 mWebViewController.attachSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700859 transport.setWebView(mSubView);
860 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -0700861 final Tab newTab = mWebViewController.openTabAndShow(
Michael Kolb18eb3772010-12-10 14:29:51 -0800862 Tab.this,
Michael Kolb8233fac2010-10-26 16:08:53 -0700863 IntentHandler.EMPTY_URL_DATA, false, null);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700864 if (newTab != Tab.this) {
865 Tab.this.addChildTab(newTab);
866 }
867 transport.setWebView(newTab.getWebView());
868 }
869 msg.sendToTarget();
870 }
871
872 @Override
873 public boolean onCreateWindow(WebView view, final boolean dialog,
874 final boolean userGesture, final Message resultMsg) {
875 // only allow new window or sub window for the foreground case
876 if (!mInForeground) {
877 return false;
878 }
879 // Short-circuit if we can't create any more tabs or sub windows.
880 if (dialog && mSubView != null) {
881 new AlertDialog.Builder(mActivity)
882 .setTitle(R.string.too_many_subwindows_dialog_title)
883 .setIcon(android.R.drawable.ic_dialog_alert)
884 .setMessage(R.string.too_many_subwindows_dialog_message)
885 .setPositiveButton(R.string.ok, null)
886 .show();
887 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700888 } else if (!mWebViewController.getTabControl().canCreateNewTab()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700889 new AlertDialog.Builder(mActivity)
890 .setTitle(R.string.too_many_windows_dialog_title)
891 .setIcon(android.R.drawable.ic_dialog_alert)
892 .setMessage(R.string.too_many_windows_dialog_message)
893 .setPositiveButton(R.string.ok, null)
894 .show();
895 return false;
896 }
897
898 // Short-circuit if this was a user gesture.
899 if (userGesture) {
900 createWindow(dialog, resultMsg);
901 return true;
902 }
903
904 // Allow the popup and create the appropriate window.
905 final AlertDialog.OnClickListener allowListener =
906 new AlertDialog.OnClickListener() {
907 public void onClick(DialogInterface d,
908 int which) {
909 createWindow(dialog, resultMsg);
910 }
911 };
912
913 // Block the popup by returning a null WebView.
914 final AlertDialog.OnClickListener blockListener =
915 new AlertDialog.OnClickListener() {
916 public void onClick(DialogInterface d, int which) {
917 resultMsg.sendToTarget();
918 }
919 };
920
921 // Build a confirmation dialog to display to the user.
922 final AlertDialog d =
923 new AlertDialog.Builder(mActivity)
924 .setTitle(R.string.attention)
925 .setIcon(android.R.drawable.ic_dialog_alert)
926 .setMessage(R.string.popup_window_attempt)
927 .setPositiveButton(R.string.allow, allowListener)
928 .setNegativeButton(R.string.block, blockListener)
929 .setCancelable(false)
930 .create();
931
932 // Show the confirmation dialog.
933 d.show();
934 return true;
935 }
936
937 @Override
Patrick Scotteb5061b2009-11-18 15:00:30 -0500938 public void onRequestFocus(WebView view) {
939 if (!mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700940 mWebViewController.switchToTab(mWebViewController.getTabControl().getTabIndex(
Patrick Scotteb5061b2009-11-18 15:00:30 -0500941 Tab.this));
942 }
943 }
944
945 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700946 public void onCloseWindow(WebView window) {
947 if (mParentTab != null) {
948 // JavaScript can only close popup window.
949 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700950 mWebViewController.switchToTab(mWebViewController.getTabControl()
Grace Kloba22ac16e2009-10-07 18:00:23 -0700951 .getTabIndex(mParentTab));
952 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700953 mWebViewController.closeTab(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700954 }
955 }
956
957 @Override
958 public void onProgressChanged(WebView view, int newProgress) {
John Reck30c714c2010-12-16 17:30:34 -0800959 mPageLoadProgress = newProgress;
960 mWebViewController.onProgressChanged(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700961 }
962
963 @Override
Leon Scroggins21d9b902010-03-11 09:33:11 -0500964 public void onReceivedTitle(WebView view, final String title) {
John Reck30c714c2010-12-16 17:30:34 -0800965 mCurrentState.mTitle = title;
Michael Kolb8233fac2010-10-26 16:08:53 -0700966 mWebViewController.onReceivedTitle(Tab.this, title);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700967 }
968
969 @Override
970 public void onReceivedIcon(WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -0800971 mCurrentState.mFavicon = icon;
Michael Kolb8233fac2010-10-26 16:08:53 -0700972 mWebViewController.onFavicon(Tab.this, view, icon);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700973 }
974
975 @Override
976 public void onReceivedTouchIconUrl(WebView view, String url,
977 boolean precomposed) {
978 final ContentResolver cr = mActivity.getContentResolver();
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400979 // Let precomposed icons take precedence over non-composed
980 // icons.
981 if (precomposed && mTouchIconLoader != null) {
982 mTouchIconLoader.cancel(false);
983 mTouchIconLoader = null;
984 }
985 // Have only one async task at a time.
986 if (mTouchIconLoader == null) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700987 mTouchIconLoader = new DownloadTouchIcon(Tab.this,
988 mActivity, cr, view);
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400989 mTouchIconLoader.execute(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700990 }
991 }
992
993 @Override
994 public void onShowCustomView(View view,
995 WebChromeClient.CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700996 if (mInForeground) mWebViewController.showCustomView(Tab.this, view,
997 callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700998 }
999
1000 @Override
1001 public void onHideCustomView() {
Michael Kolb8233fac2010-10-26 16:08:53 -07001002 if (mInForeground) mWebViewController.hideCustomView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001003 }
1004
1005 /**
1006 * The origin has exceeded its database quota.
1007 * @param url the URL that exceeded the quota
1008 * @param databaseIdentifier the identifier of the database on which the
1009 * transaction that caused the quota overflow was run
1010 * @param currentQuota the current quota for the origin.
1011 * @param estimatedSize the estimated size of the database.
1012 * @param totalUsedQuota is the sum of all origins' quota.
1013 * @param quotaUpdater The callback to run when a decision to allow or
1014 * deny quota has been made. Don't forget to call this!
1015 */
1016 @Override
1017 public void onExceededDatabaseQuota(String url,
1018 String databaseIdentifier, long currentQuota, long estimatedSize,
1019 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
1020 BrowserSettings.getInstance().getWebStorageSizeManager()
1021 .onExceededDatabaseQuota(url, databaseIdentifier,
1022 currentQuota, estimatedSize, totalUsedQuota,
1023 quotaUpdater);
1024 }
1025
1026 /**
1027 * The Application Cache has exceeded its max size.
1028 * @param spaceNeeded is the amount of disk space that would be needed
1029 * in order for the last appcache operation to succeed.
1030 * @param totalUsedQuota is the sum of all origins' quota.
1031 * @param quotaUpdater A callback to inform the WebCore thread that a
1032 * new app cache size is available. This callback must always
1033 * be executed at some point to ensure that the sleeping
1034 * WebCore thread is woken up.
1035 */
1036 @Override
1037 public void onReachedMaxAppCacheSize(long spaceNeeded,
1038 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
1039 BrowserSettings.getInstance().getWebStorageSizeManager()
1040 .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota,
1041 quotaUpdater);
1042 }
1043
1044 /**
1045 * Instructs the browser to show a prompt to ask the user to set the
1046 * Geolocation permission state for the specified origin.
1047 * @param origin The origin for which Geolocation permissions are
1048 * requested.
1049 * @param callback The callback to call once the user has set the
1050 * Geolocation permission state.
1051 */
1052 @Override
1053 public void onGeolocationPermissionsShowPrompt(String origin,
1054 GeolocationPermissions.Callback callback) {
1055 if (mInForeground) {
Grace Kloba50c241e2010-04-20 11:07:50 -07001056 getGeolocationPermissionsPrompt().show(origin, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001057 }
1058 }
1059
1060 /**
1061 * Instructs the browser to hide the Geolocation permissions prompt.
1062 */
1063 @Override
1064 public void onGeolocationPermissionsHidePrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001065 if (mInForeground && mGeolocationPermissionsPrompt != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001066 mGeolocationPermissionsPrompt.hide();
1067 }
1068 }
1069
Ben Murdoch65acc352009-11-19 18:16:04 +00001070 /* Adds a JavaScript error message to the system log and if the JS
1071 * console is enabled in the about:debug options, to that console
1072 * also.
Ben Murdochc42addf2010-01-28 15:19:59 +00001073 * @param consoleMessage the message object.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001074 */
1075 @Override
Ben Murdochc42addf2010-01-28 15:19:59 +00001076 public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001077 if (mInForeground) {
1078 // call getErrorConsole(true) so it will create one if needed
1079 ErrorConsoleView errorConsole = getErrorConsole(true);
Ben Murdochc42addf2010-01-28 15:19:59 +00001080 errorConsole.addErrorMessage(consoleMessage);
Michael Kolb8233fac2010-10-26 16:08:53 -07001081 if (mWebViewController.shouldShowErrorConsole()
1082 && errorConsole.getShowState() !=
1083 ErrorConsoleView.SHOW_MAXIMIZED) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001084 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
1085 }
1086 }
Ben Murdochc42addf2010-01-28 15:19:59 +00001087
Jeff Hamilton47654f42010-09-07 09:57:51 -05001088 // Don't log console messages in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001089 if (isPrivateBrowsingEnabled()) return true;
Jeff Hamilton47654f42010-09-07 09:57:51 -05001090
Ben Murdochc42addf2010-01-28 15:19:59 +00001091 String message = "Console: " + consoleMessage.message() + " "
1092 + consoleMessage.sourceId() + ":"
1093 + consoleMessage.lineNumber();
1094
1095 switch (consoleMessage.messageLevel()) {
1096 case TIP:
1097 Log.v(CONSOLE_LOGTAG, message);
1098 break;
1099 case LOG:
1100 Log.i(CONSOLE_LOGTAG, message);
1101 break;
1102 case WARNING:
1103 Log.w(CONSOLE_LOGTAG, message);
1104 break;
1105 case ERROR:
1106 Log.e(CONSOLE_LOGTAG, message);
1107 break;
1108 case DEBUG:
1109 Log.d(CONSOLE_LOGTAG, message);
1110 break;
1111 }
1112
1113 return true;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001114 }
1115
1116 /**
1117 * Ask the browser for an icon to represent a <video> element.
1118 * This icon will be used if the Web page did not specify a poster attribute.
1119 * @return Bitmap The icon or null if no such icon is available.
1120 */
1121 @Override
1122 public Bitmap getDefaultVideoPoster() {
1123 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001124 return mWebViewController.getDefaultVideoPoster();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001125 }
1126 return null;
1127 }
1128
1129 /**
1130 * Ask the host application for a custom progress view to show while
1131 * a <video> is loading.
1132 * @return View The progress view.
1133 */
1134 @Override
1135 public View getVideoLoadingProgressView() {
1136 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001137 return mWebViewController.getVideoLoadingProgressView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001138 }
1139 return null;
1140 }
1141
1142 @Override
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01001143 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001144 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001145 mWebViewController.openFileChooser(uploadMsg, acceptType);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001146 } else {
1147 uploadMsg.onReceiveValue(null);
1148 }
1149 }
1150
1151 /**
1152 * Deliver a list of already-visited URLs
1153 */
1154 @Override
1155 public void getVisitedHistory(final ValueCallback<String[]> callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001156 mWebViewController.getVisitedHistory(callback);
1157 }
Ben Murdoch8029a772010-11-16 11:58:21 +00001158
1159 @Override
1160 public void setupAutoFill(Message message) {
1161 // Prompt the user to set up their profile.
1162 final Message msg = message;
1163 AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
Ben Murdoch1d676b62011-01-17 12:54:24 +00001164 LayoutInflater inflater = (LayoutInflater) mActivity.getSystemService(
1165 Context.LAYOUT_INFLATER_SERVICE);
1166 final View layout = inflater.inflate(R.layout.setup_autofill_dialog, null);
1167
1168 builder.setView(layout)
1169 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1170 @Override
1171 public void onClick(DialogInterface dialog, int id) {
1172 CheckBox disableAutoFill = (CheckBox) layout.findViewById(
1173 R.id.setup_autofill_dialog_disable_autofill);
1174
1175 if (disableAutoFill.isChecked()) {
1176 // Disable autofill and show a toast with how to turn it on again.
1177 BrowserSettings s = BrowserSettings.getInstance();
1178 s.addObserver(mMainView.getSettings());
1179 s.disableAutoFill(mActivity);
1180 s.update();
1181 Toast.makeText(mActivity,
1182 R.string.autofill_setup_dialog_negative_toast,
1183 Toast.LENGTH_LONG).show();
1184 } else {
1185 // Take user to the AutoFill profile editor. When they return,
1186 // we will send the message that we pass here which will trigger
1187 // the form to get filled out with their new profile.
1188 mWebViewController.setupAutoFill(msg);
1189 }
1190 }
1191 })
1192 .setNegativeButton(R.string.cancel, null)
1193 .show();
Ben Murdoch8029a772010-11-16 11:58:21 +00001194 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001195 };
1196
1197 // -------------------------------------------------------------------------
1198 // WebViewClient implementation for the sub window
1199 // -------------------------------------------------------------------------
1200
1201 // Subclass of WebViewClient used in subwindows to notify the main
1202 // WebViewClient of certain WebView activities.
1203 private static class SubWindowClient extends WebViewClient {
1204 // The main WebViewClient.
1205 private final WebViewClient mClient;
Michael Kolb8233fac2010-10-26 16:08:53 -07001206 private final WebViewController mController;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001207
Michael Kolb8233fac2010-10-26 16:08:53 -07001208 SubWindowClient(WebViewClient client, WebViewController controller) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001209 mClient = client;
Michael Kolb8233fac2010-10-26 16:08:53 -07001210 mController = controller;
Leon Scroggins III211ba542010-04-19 13:21:13 -04001211 }
1212 @Override
1213 public void onPageStarted(WebView view, String url, Bitmap favicon) {
1214 // Unlike the others, do not call mClient's version, which would
1215 // change the progress bar. However, we do want to remove the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001216 // find or select dialog.
Michael Kolb8233fac2010-10-26 16:08:53 -07001217 mController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001218 }
1219 @Override
1220 public void doUpdateVisitedHistory(WebView view, String url,
1221 boolean isReload) {
1222 mClient.doUpdateVisitedHistory(view, url, isReload);
1223 }
1224 @Override
1225 public boolean shouldOverrideUrlLoading(WebView view, String url) {
1226 return mClient.shouldOverrideUrlLoading(view, url);
1227 }
1228 @Override
1229 public void onReceivedSslError(WebView view, SslErrorHandler handler,
1230 SslError error) {
1231 mClient.onReceivedSslError(view, handler, error);
1232 }
1233 @Override
1234 public void onReceivedHttpAuthRequest(WebView view,
1235 HttpAuthHandler handler, String host, String realm) {
1236 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
1237 }
1238 @Override
1239 public void onFormResubmission(WebView view, Message dontResend,
1240 Message resend) {
1241 mClient.onFormResubmission(view, dontResend, resend);
1242 }
1243 @Override
1244 public void onReceivedError(WebView view, int errorCode,
1245 String description, String failingUrl) {
1246 mClient.onReceivedError(view, errorCode, description, failingUrl);
1247 }
1248 @Override
1249 public boolean shouldOverrideKeyEvent(WebView view,
1250 android.view.KeyEvent event) {
1251 return mClient.shouldOverrideKeyEvent(view, event);
1252 }
1253 @Override
1254 public void onUnhandledKeyEvent(WebView view,
1255 android.view.KeyEvent event) {
1256 mClient.onUnhandledKeyEvent(view, event);
1257 }
1258 }
1259
1260 // -------------------------------------------------------------------------
1261 // WebChromeClient implementation for the sub window
1262 // -------------------------------------------------------------------------
1263
1264 private class SubWindowChromeClient extends WebChromeClient {
1265 // The main WebChromeClient.
1266 private final WebChromeClient mClient;
1267
1268 SubWindowChromeClient(WebChromeClient client) {
1269 mClient = client;
1270 }
1271 @Override
1272 public void onProgressChanged(WebView view, int newProgress) {
1273 mClient.onProgressChanged(view, newProgress);
1274 }
1275 @Override
1276 public boolean onCreateWindow(WebView view, boolean dialog,
1277 boolean userGesture, android.os.Message resultMsg) {
1278 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
1279 }
1280 @Override
1281 public void onCloseWindow(WebView window) {
1282 if (window != mSubView) {
1283 Log.e(LOGTAG, "Can't close the window");
1284 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001285 mWebViewController.dismissSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001286 }
1287 }
1288
1289 // -------------------------------------------------------------------------
1290
Michael Kolb8233fac2010-10-26 16:08:53 -07001291 // TODO temporarily use activity here
1292 // remove later
1293
Grace Kloba22ac16e2009-10-07 18:00:23 -07001294 // Construct a new tab
Michael Kolb8233fac2010-10-26 16:08:53 -07001295 Tab(WebViewController wvcontroller, WebView w, boolean closeOnExit, String appId,
Grace Kloba22ac16e2009-10-07 18:00:23 -07001296 String url) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001297 mWebViewController = wvcontroller;
1298 mActivity = mWebViewController.getActivity();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001299 mCloseOnExit = closeOnExit;
1300 mAppId = appId;
John Recke969cc52010-12-21 17:24:43 -08001301 mDataController = DataController.getInstance(mActivity);
John Reck847b5322011-04-14 17:02:18 -07001302 mCurrentState = new PageState(mActivity, w != null
1303 ? w.isPrivateBrowsingEnabled() : false);
Michael Kolb8233fac2010-10-26 16:08:53 -07001304 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001305 mInForeground = false;
1306
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001307 mDownloadListener = new DownloadListener() {
1308 public void onDownloadStart(String url, String userAgent,
1309 String contentDisposition, String mimetype,
1310 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001311 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001312 mimetype, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001313 }
1314 };
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001315 mWebBackForwardListClient = new WebBackForwardListClient() {
1316 @Override
1317 public void onNewHistoryItem(WebHistoryItem item) {
1318 if (isInVoiceSearchMode()) {
1319 item.setCustomData(mVoiceSearchData.mVoiceSearchIntent);
1320 }
1321 }
1322 @Override
1323 public void onIndexChanged(WebHistoryItem item, int index) {
1324 Object data = item.getCustomData();
1325 if (data != null && data instanceof Intent) {
1326 activateVoiceSearchMode((Intent) data);
1327 }
1328 }
1329 };
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001330
Grace Kloba22ac16e2009-10-07 18:00:23 -07001331 setWebView(w);
1332 }
1333
1334 /**
1335 * Sets the WebView for this tab, correctly removing the old WebView from
1336 * the container view.
1337 */
1338 void setWebView(WebView w) {
1339 if (mMainView == w) {
1340 return;
1341 }
Michael Kolba713ec82010-11-29 17:27:06 -08001342
Grace Kloba22ac16e2009-10-07 18:00:23 -07001343 // If the WebView is changing, the page will be reloaded, so any ongoing
1344 // Geolocation permission requests are void.
Grace Kloba50c241e2010-04-20 11:07:50 -07001345 if (mGeolocationPermissionsPrompt != null) {
1346 mGeolocationPermissionsPrompt.hide();
1347 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001348
Michael Kolba713ec82010-11-29 17:27:06 -08001349 mWebViewController.onSetWebView(this, w);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001350
1351 // set the new one
1352 mMainView = w;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001353 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001354 if (mMainView != null) {
1355 mMainView.setWebViewClient(mWebViewClient);
1356 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001357 // Attach DownloadManager so that downloads can start in an active
1358 // or a non-active window. This can happen when going to a site that
1359 // does a redirect after a period of time. The user could have
1360 // switched to another tab while waiting for the download to start.
1361 mMainView.setDownloadListener(mDownloadListener);
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001362 mMainView.setWebBackForwardListClient(mWebBackForwardListClient);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001363 }
1364 }
1365
1366 /**
1367 * Destroy the tab's main WebView and subWindow if any
1368 */
1369 void destroy() {
1370 if (mMainView != null) {
1371 dismissSubWindow();
1372 BrowserSettings.getInstance().deleteObserver(mMainView.getSettings());
1373 // save the WebView to call destroy() after detach it from the tab
1374 WebView webView = mMainView;
1375 setWebView(null);
1376 webView.destroy();
1377 }
1378 }
1379
1380 /**
1381 * Remove the tab from the parent
1382 */
1383 void removeFromTree() {
1384 // detach the children
1385 if (mChildTabs != null) {
1386 for(Tab t : mChildTabs) {
1387 t.setParentTab(null);
1388 }
1389 }
1390 // remove itself from the parent list
1391 if (mParentTab != null) {
1392 mParentTab.mChildTabs.remove(this);
1393 }
1394 }
1395
1396 /**
1397 * Create a new subwindow unless a subwindow already exists.
1398 * @return True if a new subwindow was created. False if one already exists.
1399 */
1400 boolean createSubWindow() {
1401 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001402 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001403 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001404 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001405 mSubView.setWebChromeClient(new SubWindowChromeClient(
1406 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001407 // Set a different DownloadListener for the mSubView, since it will
1408 // just need to dismiss the mSubView, rather than close the Tab
1409 mSubView.setDownloadListener(new DownloadListener() {
1410 public void onDownloadStart(String url, String userAgent,
1411 String contentDisposition, String mimetype,
1412 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001413 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001414 contentDisposition, mimetype, contentLength);
1415 if (mSubView.copyBackForwardList().getSize() == 0) {
1416 // This subwindow was opened for the sole purpose of
1417 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001418 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001419 }
1420 }
1421 });
Grace Kloba22ac16e2009-10-07 18:00:23 -07001422 mSubView.setOnCreateContextMenuListener(mActivity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001423 return true;
1424 }
1425 return false;
1426 }
1427
1428 /**
1429 * Dismiss the subWindow for the tab.
1430 */
1431 void dismissSubWindow() {
1432 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001433 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001434 BrowserSettings.getInstance().deleteObserver(
1435 mSubView.getSettings());
1436 mSubView.destroy();
1437 mSubView = null;
1438 mSubViewContainer = null;
1439 }
1440 }
1441
Grace Kloba22ac16e2009-10-07 18:00:23 -07001442
1443 /**
1444 * Set the parent tab of this tab.
1445 */
1446 void setParentTab(Tab parent) {
1447 mParentTab = parent;
1448 // This tab may have been freed due to low memory. If that is the case,
1449 // the parent tab index is already saved. If we are changing that index
1450 // (most likely due to removing the parent tab) we must update the
1451 // parent tab index in the saved Bundle.
1452 if (mSavedState != null) {
1453 if (parent == null) {
1454 mSavedState.remove(PARENTTAB);
1455 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -07001456 mSavedState.putInt(PARENTTAB, mWebViewController.getTabControl()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001457 .getTabIndex(parent));
1458 }
1459 }
1460 }
1461
1462 /**
1463 * When a Tab is created through the content of another Tab, then we
1464 * associate the Tabs.
1465 * @param child the Tab that was created from this Tab
1466 */
1467 void addChildTab(Tab child) {
1468 if (mChildTabs == null) {
1469 mChildTabs = new Vector<Tab>();
1470 }
1471 mChildTabs.add(child);
1472 child.setParentTab(this);
1473 }
1474
1475 Vector<Tab> getChildTabs() {
1476 return mChildTabs;
1477 }
1478
1479 void resume() {
1480 if (mMainView != null) {
1481 mMainView.onResume();
1482 if (mSubView != null) {
1483 mSubView.onResume();
1484 }
1485 }
1486 }
1487
1488 void pause() {
1489 if (mMainView != null) {
1490 mMainView.onPause();
1491 if (mSubView != null) {
1492 mSubView.onPause();
1493 }
1494 }
1495 }
1496
1497 void putInForeground() {
1498 mInForeground = true;
1499 resume();
1500 mMainView.setOnCreateContextMenuListener(mActivity);
1501 if (mSubView != null) {
1502 mSubView.setOnCreateContextMenuListener(mActivity);
1503 }
1504 // Show the pending error dialog if the queue is not empty
1505 if (mQueuedErrors != null && mQueuedErrors.size() > 0) {
1506 showError(mQueuedErrors.getFirst());
1507 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001508 mWebViewController.bookmarkedStatusHasChanged(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001509 }
1510
1511 void putInBackground() {
1512 mInForeground = false;
1513 pause();
1514 mMainView.setOnCreateContextMenuListener(null);
1515 if (mSubView != null) {
1516 mSubView.setOnCreateContextMenuListener(null);
1517 }
1518 }
1519
Michael Kolb8233fac2010-10-26 16:08:53 -07001520 boolean inForeground() {
1521 return mInForeground;
1522 }
1523
Grace Kloba22ac16e2009-10-07 18:00:23 -07001524 /**
1525 * Return the top window of this tab; either the subwindow if it is not
1526 * null or the main window.
1527 * @return The top window of this tab.
1528 */
1529 WebView getTopWindow() {
1530 if (mSubView != null) {
1531 return mSubView;
1532 }
1533 return mMainView;
1534 }
1535
1536 /**
1537 * Return the main window of this tab. Note: if a tab is freed in the
1538 * background, this can return null. It is only guaranteed to be
1539 * non-null for the current tab.
1540 * @return The main WebView of this tab.
1541 */
1542 WebView getWebView() {
1543 return mMainView;
1544 }
1545
Michael Kolba713ec82010-11-29 17:27:06 -08001546 void setViewContainer(View container) {
1547 mContainer = container;
1548 }
1549
Michael Kolb8233fac2010-10-26 16:08:53 -07001550 View getViewContainer() {
1551 return mContainer;
1552 }
1553
Grace Kloba22ac16e2009-10-07 18:00:23 -07001554 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001555 * Return whether private browsing is enabled for the main window of
1556 * this tab.
1557 * @return True if private browsing is enabled.
1558 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001559 boolean isPrivateBrowsingEnabled() {
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001560 WebView webView = getWebView();
1561 if (webView == null) {
1562 return false;
1563 }
1564 return webView.isPrivateBrowsingEnabled();
1565 }
1566
1567 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001568 * Return the subwindow of this tab or null if there is no subwindow.
1569 * @return The subwindow of this tab or null.
1570 */
1571 WebView getSubWebView() {
1572 return mSubView;
1573 }
1574
Michael Kolb1514bb72010-11-22 09:11:48 -08001575 void setSubWebView(WebView subView) {
1576 mSubView = subView;
1577 }
1578
Michael Kolb8233fac2010-10-26 16:08:53 -07001579 View getSubViewContainer() {
1580 return mSubViewContainer;
1581 }
1582
Michael Kolb1514bb72010-11-22 09:11:48 -08001583 void setSubViewContainer(View subViewContainer) {
1584 mSubViewContainer = subViewContainer;
1585 }
1586
Grace Kloba22ac16e2009-10-07 18:00:23 -07001587 /**
1588 * @return The geolocation permissions prompt for this tab.
1589 */
1590 GeolocationPermissionsPrompt getGeolocationPermissionsPrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001591 if (mGeolocationPermissionsPrompt == null) {
1592 ViewStub stub = (ViewStub) mContainer
1593 .findViewById(R.id.geolocation_permissions_prompt);
1594 mGeolocationPermissionsPrompt = (GeolocationPermissionsPrompt) stub
1595 .inflate();
1596 mGeolocationPermissionsPrompt.init();
1597 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001598 return mGeolocationPermissionsPrompt;
1599 }
1600
1601 /**
1602 * @return The application id string
1603 */
1604 String getAppId() {
1605 return mAppId;
1606 }
1607
1608 /**
1609 * Set the application id string
1610 * @param id
1611 */
1612 void setAppId(String id) {
1613 mAppId = id;
1614 }
1615
Grace Kloba22ac16e2009-10-07 18:00:23 -07001616 String getUrl() {
John Reck324d4402011-01-11 16:56:42 -08001617 return UrlUtils.filteredUrl(mCurrentState.mUrl);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001618 }
1619
John Reck49a603c2011-03-03 09:33:05 -08001620 String getOriginalUrl() {
1621 if (mMainView == null) {
1622 return "";
1623 }
1624 return UrlUtils.filteredUrl(mMainView.getOriginalUrl());
1625 }
1626
Grace Kloba22ac16e2009-10-07 18:00:23 -07001627 /**
John Reck30c714c2010-12-16 17:30:34 -08001628 * Get the title of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001629 */
1630 String getTitle() {
John Reck30c714c2010-12-16 17:30:34 -08001631 if (mCurrentState.mTitle == null && mInPageLoad) {
1632 return mActivity.getString(R.string.title_bar_loading);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001633 }
John Reck30c714c2010-12-16 17:30:34 -08001634 return mCurrentState.mTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001635 }
1636
1637 /**
John Reck30c714c2010-12-16 17:30:34 -08001638 * Get the favicon of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001639 */
1640 Bitmap getFavicon() {
John Reck30c714c2010-12-16 17:30:34 -08001641 return mCurrentState.mFavicon;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001642 }
1643
John Recke969cc52010-12-21 17:24:43 -08001644 public boolean isBookmarkedSite() {
1645 return mCurrentState.mIsBookmarkedSite;
1646 }
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001647
Grace Kloba22ac16e2009-10-07 18:00:23 -07001648 /**
1649 * Return the tab's error console. Creates the console if createIfNEcessary
1650 * is true and we haven't already created the console.
1651 * @param createIfNecessary Flag to indicate if the console should be
1652 * created if it has not been already.
1653 * @return The tab's error console, or null if one has not been created and
1654 * createIfNecessary is false.
1655 */
1656 ErrorConsoleView getErrorConsole(boolean createIfNecessary) {
1657 if (createIfNecessary && mErrorConsole == null) {
1658 mErrorConsole = new ErrorConsoleView(mActivity);
1659 mErrorConsole.setWebView(mMainView);
1660 }
1661 return mErrorConsole;
1662 }
1663
1664 /**
1665 * If this Tab was created through another Tab, then this method returns
1666 * that Tab.
1667 * @return the Tab parent or null
1668 */
1669 public Tab getParentTab() {
1670 return mParentTab;
1671 }
1672
1673 /**
1674 * Return whether this tab should be closed when it is backing out of the
1675 * first page.
1676 * @return TRUE if this tab should be closed when exit.
1677 */
1678 boolean closeOnExit() {
1679 return mCloseOnExit;
1680 }
1681
John Reck30c714c2010-12-16 17:30:34 -08001682 private void setLockIconType(LockIcon icon) {
1683 mCurrentState.mLockIcon = icon;
1684 mWebViewController.onUpdatedLockIcon(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001685 }
1686
1687 /**
1688 * @return The tab's lock icon type.
1689 */
John Reck30c714c2010-12-16 17:30:34 -08001690 LockIcon getLockIconType() {
1691 return mCurrentState.mLockIcon;
1692 }
1693
1694 int getLoadProgress() {
1695 if (mInPageLoad) {
1696 return mPageLoadProgress;
1697 }
1698 return 100;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001699 }
1700
1701 /**
1702 * @return TRUE if onPageStarted is called while onPageFinished is not
1703 * called yet.
1704 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001705 boolean inPageLoad() {
1706 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001707 }
1708
1709 // force mInLoad to be false. This should only be called before closing the
1710 // tab to ensure BrowserActivity's pauseWebViewTimers() is called correctly.
Michael Kolb8233fac2010-10-26 16:08:53 -07001711 void clearInPageLoad() {
1712 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001713 }
1714
Grace Kloba22ac16e2009-10-07 18:00:23 -07001715 /**
John Reck30c714c2010-12-16 17:30:34 -08001716 * Get the cached saved state bundle.
1717 * @return cached state bundle
Grace Kloba22ac16e2009-10-07 18:00:23 -07001718 */
1719 Bundle getSavedState() {
1720 return mSavedState;
1721 }
1722
1723 /**
1724 * Set the saved state.
1725 */
1726 void setSavedState(Bundle state) {
1727 mSavedState = state;
1728 }
1729
1730 /**
1731 * @return TRUE if succeed in saving the state.
1732 */
1733 boolean saveState() {
1734 // If the WebView is null it means we ran low on memory and we already
1735 // stored the saved state in mSavedState.
1736 if (mMainView == null) {
1737 return mSavedState != null;
1738 }
1739
1740 mSavedState = new Bundle();
1741 final WebBackForwardList list = mMainView.saveState(mSavedState);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001742
1743 // Store some extra info for displaying the tab in the picker.
1744 final WebHistoryItem item = list != null ? list.getCurrentItem() : null;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001745
John Reck30c714c2010-12-16 17:30:34 -08001746 mSavedState.putString(CURRURL, mCurrentState.mUrl);
1747 mSavedState.putString(CURRTITLE, mCurrentState.mTitle);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001748 mSavedState.putBoolean(CLOSEONEXIT, mCloseOnExit);
1749 if (mAppId != null) {
1750 mSavedState.putString(APPID, mAppId);
1751 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001752 // Remember the parent tab so the relationship can be restored.
1753 if (mParentTab != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001754 mSavedState.putInt(PARENTTAB, mWebViewController.getTabControl().getTabIndex(
Grace Kloba22ac16e2009-10-07 18:00:23 -07001755 mParentTab));
1756 }
Michael Kolbeb95db42011-03-03 10:38:40 -08001757 if (mScreenshot != null) {
1758 mSavedState.putParcelable(SCREENSHOT, mScreenshot);
1759 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001760 return true;
1761 }
1762
1763 /*
1764 * Restore the state of the tab.
1765 */
1766 boolean restoreState(Bundle b) {
1767 if (b == null) {
1768 return false;
1769 }
1770 // Restore the internal state even if the WebView fails to restore.
1771 // This will maintain the app id, original url and close-on-exit values.
1772 mSavedState = null;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001773 mCloseOnExit = b.getBoolean(CLOSEONEXIT);
1774 mAppId = b.getString(APPID);
Michael Kolbeb95db42011-03-03 10:38:40 -08001775 mScreenshot = b.getParcelable(SCREENSHOT);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001776
1777 final WebBackForwardList list = mMainView.restoreState(b);
1778 if (list == null) {
1779 return false;
1780 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001781 return true;
1782 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04001783
Leon Scroggins1961ed22010-12-07 15:22:21 -05001784 public void updateBookmarkedStatus() {
John Recke969cc52010-12-21 17:24:43 -08001785 mDataController.queryBookmarkStatus(getUrl(), mIsBookmarkCallback);
Leon Scroggins1961ed22010-12-07 15:22:21 -05001786 }
1787
John Recke969cc52010-12-21 17:24:43 -08001788 private DataController.OnQueryUrlIsBookmark mIsBookmarkCallback
1789 = new DataController.OnQueryUrlIsBookmark() {
1790 @Override
1791 public void onQueryUrlIsBookmark(String url, boolean isBookmark) {
1792 if (mCurrentState.mUrl.equals(url)) {
1793 mCurrentState.mIsBookmarkedSite = isBookmark;
1794 mWebViewController.bookmarkedStatusHasChanged(Tab.this);
1795 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001796 }
John Recke969cc52010-12-21 17:24:43 -08001797 };
Michael Kolb1acef692011-03-08 14:12:06 -08001798
Michael Kolbeb95db42011-03-03 10:38:40 -08001799 public void setScreenshot(Bitmap screenshot) {
1800 mScreenshot = screenshot;
1801 }
1802
1803 public Bitmap getScreenshot() {
1804 return mScreenshot;
1805 }
1806
Grace Kloba22ac16e2009-10-07 18:00:23 -07001807}