blob: 8962475940ba08bf7ac61165c1817fdd45ae97e6 [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
Michael Kolbc831b632011-05-11 09:30:34 -070091 // The tab ID
92 private long mId;
93
Grace Kloba22ac16e2009-10-07 18:00:23 -070094 // The Geolocation permissions prompt
95 private GeolocationPermissionsPrompt mGeolocationPermissionsPrompt;
96 // Main WebView wrapper
Michael Kolba713ec82010-11-29 17:27:06 -080097 private View mContainer;
Grace Kloba22ac16e2009-10-07 18:00:23 -070098 // Main WebView
99 private WebView mMainView;
100 // Subwindow container
101 private View mSubViewContainer;
102 // Subwindow WebView
103 private WebView mSubView;
104 // Saved bundle for when we are running low on memory. It contains the
105 // information needed to restore the WebView if the user goes back to the
106 // tab.
107 private Bundle mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700108 // Parent Tab. This is the Tab that created this Tab, or null if the Tab was
109 // created by the UI
Michael Kolbc831b632011-05-11 09:30:34 -0700110 private Tab mParent;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700111 // Tab that constructed by this Tab. This is used when this Tab is
112 // destroyed, it clears all mParentTab values in the children.
Michael Kolbc831b632011-05-11 09:30:34 -0700113 private Vector<Tab> mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700114 // If true, the tab is in the foreground of the current activity.
115 private boolean mInForeground;
Michael Kolb8233fac2010-10-26 16:08:53 -0700116 // If true, the tab is in page loading state (after onPageStarted,
117 // before onPageFinsihed)
118 private boolean mInPageLoad;
John Reck30c714c2010-12-16 17:30:34 -0800119 // The last reported progress of the current page
120 private int mPageLoadProgress;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000121 // The time the load started, used to find load page time
122 private long mLoadStartTime;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700123 // Application identifier used to find tabs that another application wants
124 // to reuse.
125 private String mAppId;
126 // Keep the original url around to avoid killing the old WebView if the url
127 // has not changed.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700128 // Error console for the tab
129 private ErrorConsoleView mErrorConsole;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -0500130 // The listener that gets invoked when a download is started from the
131 // mMainView
132 private final DownloadListener mDownloadListener;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500133 // Listener used to know when we move forward or back in the history list.
134 private final WebBackForwardListClient mWebBackForwardListClient;
John Recke969cc52010-12-21 17:24:43 -0800135 private DataController mDataController;
Patrick Scott92066772011-03-10 08:46:27 -0500136 // State of the auto-login request.
137 private DeviceAccountLogin mDeviceAccountLogin;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700138
139 // AsyncTask for downloading touch icons
140 DownloadTouchIcon mTouchIconLoader;
141
Michael Kolbeb95db42011-03-03 10:38:40 -0800142 private Bitmap mScreenshot;
John Reck35e9dd62011-04-25 09:01:54 -0700143 private BrowserSettings mSettings;
Michael Kolbeb95db42011-03-03 10:38:40 -0800144
John Reck30c714c2010-12-16 17:30:34 -0800145 // All the state needed for a page
146 private static class PageState {
147 String mUrl;
148 String mTitle;
149 LockIcon mLockIcon;
150 Bitmap mFavicon;
John Recke969cc52010-12-21 17:24:43 -0800151 Boolean mIsBookmarkedSite = false;
John Reck30c714c2010-12-16 17:30:34 -0800152
153 PageState(Context c, boolean incognito) {
154 if (incognito) {
155 mUrl = "browser:incognito";
156 mTitle = c.getString(R.string.new_incognito_tab);
John Reck30c714c2010-12-16 17:30:34 -0800157 } else {
158 mUrl = "";
159 mTitle = c.getString(R.string.new_tab);
John Reck30c714c2010-12-16 17:30:34 -0800160 }
Justin Hodc6cbb72011-01-24 15:14:54 -0800161 mFavicon = BitmapFactory.decodeResource(
162 c.getResources(), R.drawable.app_web_browser_sm);
John Reck30c714c2010-12-16 17:30:34 -0800163 mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
164 }
165
166 PageState(Context c, boolean incognito, String url, Bitmap favicon) {
167 mUrl = url;
168 mTitle = null;
169 if (URLUtil.isHttpsUrl(url)) {
170 mLockIcon = LockIcon.LOCK_ICON_SECURE;
171 } else {
172 mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
173 }
174 if (favicon != null) {
175 mFavicon = favicon;
176 } else {
Justin Hodc6cbb72011-01-24 15:14:54 -0800177 mFavicon = BitmapFactory.decodeResource(
178 c.getResources(), R.drawable.app_web_browser_sm);
John Reck30c714c2010-12-16 17:30:34 -0800179 }
180 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700181 }
182
John Reck30c714c2010-12-16 17:30:34 -0800183 // The current/loading page's state
184 private PageState mCurrentState;
185
Grace Kloba22ac16e2009-10-07 18:00:23 -0700186 // Used for saving and restoring each Tab
Michael Kolbc831b632011-05-11 09:30:34 -0700187 static final String ID = "ID";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700188 static final String CURRURL = "currentUrl";
189 static final String CURRTITLE = "currentTitle";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700190 static final String PARENTTAB = "parentTab";
191 static final String APPID = "appid";
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700192 static final String INCOGNITO = "privateBrowsingEnabled";
Michael Kolbeb95db42011-03-03 10:38:40 -0800193 static final String SCREENSHOT = "screenshot";
John Reckb0a86db2011-05-24 14:05:58 -0700194 static final String USERAGENT = "useragent";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700195
196 // -------------------------------------------------------------------------
197
Leon Scroggins58d56c62010-01-28 15:12:40 -0500198 /**
199 * Private information regarding the latest voice search. If the Tab is not
200 * in voice search mode, this will be null.
201 */
202 private VoiceSearchData mVoiceSearchData;
203 /**
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400204 * Remove voice search mode from this tab.
205 */
206 public void revertVoiceSearchMode() {
207 if (mVoiceSearchData != null) {
208 mVoiceSearchData = null;
209 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700210 mWebViewController.revertVoiceSearchMode(this);
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400211 }
212 }
213 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700214
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400215 /**
Leon Scroggins58d56c62010-01-28 15:12:40 -0500216 * Return whether the tab is in voice search mode.
217 */
218 public boolean isInVoiceSearchMode() {
219 return mVoiceSearchData != null;
220 }
221 /**
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400222 * Return true if the Tab is in voice search mode and the voice search
223 * Intent came with a String identifying that Google provided the Intent.
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500224 */
225 public boolean voiceSearchSourceIsGoogle() {
226 return mVoiceSearchData != null && mVoiceSearchData.mSourceIsGoogle;
227 }
228 /**
Leon Scroggins58d56c62010-01-28 15:12:40 -0500229 * Get the title to display for the current voice search page. If the Tab
230 * is not in voice search mode, return null.
231 */
232 public String getVoiceDisplayTitle() {
233 if (mVoiceSearchData == null) return null;
234 return mVoiceSearchData.mLastVoiceSearchTitle;
235 }
236 /**
237 * Get the latest array of voice search results, to be passed to the
238 * BrowserProvider. If the Tab is not in voice search mode, return null.
239 */
240 public ArrayList<String> getVoiceSearchResults() {
241 if (mVoiceSearchData == null) return null;
242 return mVoiceSearchData.mVoiceSearchResults;
243 }
244 /**
245 * Activate voice search mode.
246 * @param intent Intent which has the results to use, or an index into the
247 * results when reusing the old results.
248 */
249 /* package */ void activateVoiceSearchMode(Intent intent) {
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500250 int index = 0;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500251 ArrayList<String> results = intent.getStringArrayListExtra(
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -0500252 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_STRINGS);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500253 if (results != null) {
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500254 ArrayList<String> urls = intent.getStringArrayListExtra(
255 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_URLS);
256 ArrayList<String> htmls = intent.getStringArrayListExtra(
257 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_HTML);
258 ArrayList<String> baseUrls = intent.getStringArrayListExtra(
259 RecognizerResultsIntent
260 .EXTRA_VOICE_SEARCH_RESULT_HTML_BASE_URLS);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500261 // This tab is now entering voice search mode for the first time, or
262 // a new voice search was done.
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500263 int size = results.size();
264 if (urls == null || size != urls.size()) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500265 throw new AssertionError("improper extras passed in Intent");
266 }
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500267 if (htmls == null || htmls.size() != size || baseUrls == null ||
268 (baseUrls.size() != size && baseUrls.size() != 1)) {
269 // If either of these arrays are empty/incorrectly sized, ignore
270 // them.
271 htmls = null;
272 baseUrls = null;
273 }
274 mVoiceSearchData = new VoiceSearchData(results, urls, htmls,
275 baseUrls);
Leon Scroggins9df94972010-03-08 18:20:35 -0500276 mVoiceSearchData.mHeaders = intent.getParcelableArrayListExtra(
277 RecognizerResultsIntent
278 .EXTRA_VOICE_SEARCH_RESULT_HTTP_HEADERS);
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500279 mVoiceSearchData.mSourceIsGoogle = intent.getBooleanExtra(
280 VoiceSearchData.SOURCE_IS_GOOGLE, false);
Leon Scroggins2ee4a5a2010-03-15 16:56:57 -0400281 mVoiceSearchData.mVoiceSearchIntent = new Intent(intent);
Leon Scrogginse10dde52010-03-08 19:53:03 -0500282 }
283 String extraData = intent.getStringExtra(
284 SearchManager.EXTRA_DATA_KEY);
285 if (extraData != null) {
286 index = Integer.parseInt(extraData);
287 if (index >= mVoiceSearchData.mVoiceSearchResults.size()) {
288 throw new AssertionError("index must be less than "
289 + "size of mVoiceSearchResults");
290 }
291 if (mVoiceSearchData.mSourceIsGoogle) {
292 Intent logIntent = new Intent(
293 LoggingEvents.ACTION_LOG_EVENT);
294 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
295 LoggingEvents.VoiceSearch.N_BEST_CHOOSE);
296 logIntent.putExtra(
297 LoggingEvents.VoiceSearch.EXTRA_N_BEST_CHOOSE_INDEX,
298 index);
299 mActivity.sendBroadcast(logIntent);
300 }
301 if (mVoiceSearchData.mVoiceSearchIntent != null) {
Leon Scroggins2ee4a5a2010-03-15 16:56:57 -0400302 // Copy the Intent, so that each history item will have its own
303 // Intent, with different (or none) extra data.
304 Intent latest = new Intent(mVoiceSearchData.mVoiceSearchIntent);
305 latest.putExtra(SearchManager.EXTRA_DATA_KEY, extraData);
306 mVoiceSearchData.mVoiceSearchIntent = latest;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500307 }
308 }
309 mVoiceSearchData.mLastVoiceSearchTitle
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500310 = mVoiceSearchData.mVoiceSearchResults.get(index);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500311 if (mInForeground) {
Michael Kolb11d19782011-03-20 10:17:40 -0700312 mWebViewController.activateVoiceSearchMode(
313 mVoiceSearchData.mLastVoiceSearchTitle,
314 mVoiceSearchData.mVoiceSearchResults);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500315 }
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500316 if (mVoiceSearchData.mVoiceSearchHtmls != null) {
317 // When index was found it was already ensured that it was valid
318 String uriString = mVoiceSearchData.mVoiceSearchHtmls.get(index);
319 if (uriString != null) {
320 Uri dataUri = Uri.parse(uriString);
321 if (RecognizerResultsIntent.URI_SCHEME_INLINE.equals(
322 dataUri.getScheme())) {
323 // If there is only one base URL, use it. If there are
324 // more, there will be one for each index, so use the base
325 // URL corresponding to the index.
326 String baseUrl = mVoiceSearchData.mVoiceSearchBaseUrls.get(
327 mVoiceSearchData.mVoiceSearchBaseUrls.size() > 1 ?
328 index : 0);
329 mVoiceSearchData.mLastVoiceSearchUrl = baseUrl;
330 mMainView.loadDataWithBaseURL(baseUrl,
331 uriString.substring(RecognizerResultsIntent
332 .URI_SCHEME_INLINE.length() + 1), "text/html",
333 "utf-8", baseUrl);
334 return;
335 }
336 }
337 }
Leon Scroggins58d56c62010-01-28 15:12:40 -0500338 mVoiceSearchData.mLastVoiceSearchUrl
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500339 = mVoiceSearchData.mVoiceSearchUrls.get(index);
340 if (null == mVoiceSearchData.mLastVoiceSearchUrl) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700341 mVoiceSearchData.mLastVoiceSearchUrl = UrlUtils.smartUrlFilter(
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500342 mVoiceSearchData.mLastVoiceSearchTitle);
343 }
Leon Scroggins9df94972010-03-08 18:20:35 -0500344 Map<String, String> headers = null;
345 if (mVoiceSearchData.mHeaders != null) {
346 int bundleIndex = mVoiceSearchData.mHeaders.size() == 1 ? 0
347 : index;
348 Bundle bundle = mVoiceSearchData.mHeaders.get(bundleIndex);
349 if (bundle != null && !bundle.isEmpty()) {
350 Iterator<String> iter = bundle.keySet().iterator();
351 headers = new HashMap<String, String>();
352 while (iter.hasNext()) {
353 String key = iter.next();
354 headers.put(key, bundle.getString(key));
355 }
356 }
357 }
358 mMainView.loadUrl(mVoiceSearchData.mLastVoiceSearchUrl, headers);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500359 }
360 /* package */ static class VoiceSearchData {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500361 public VoiceSearchData(ArrayList<String> results,
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500362 ArrayList<String> urls, ArrayList<String> htmls,
363 ArrayList<String> baseUrls) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500364 mVoiceSearchResults = results;
365 mVoiceSearchUrls = urls;
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500366 mVoiceSearchHtmls = htmls;
367 mVoiceSearchBaseUrls = baseUrls;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500368 }
369 /*
370 * ArrayList of suggestions to be displayed when opening the
371 * SearchManager
372 */
373 public ArrayList<String> mVoiceSearchResults;
374 /*
375 * ArrayList of urls, associated with the suggestions in
376 * mVoiceSearchResults.
377 */
378 public ArrayList<String> mVoiceSearchUrls;
379 /*
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500380 * ArrayList holding content to load for each item in
381 * mVoiceSearchResults.
382 */
383 public ArrayList<String> mVoiceSearchHtmls;
384 /*
385 * ArrayList holding base urls for the items in mVoiceSearchResults.
386 * If non null, this will either have the same size as
387 * mVoiceSearchResults or have a size of 1, in which case all will use
388 * the same base url
389 */
390 public ArrayList<String> mVoiceSearchBaseUrls;
391 /*
Leon Scroggins58d56c62010-01-28 15:12:40 -0500392 * The last url provided by voice search. Used for comparison to see if
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500393 * we are going to a page by some method besides voice search.
Leon Scroggins58d56c62010-01-28 15:12:40 -0500394 */
395 public String mLastVoiceSearchUrl;
396 /**
397 * The last title used for voice search. Needed to update the title bar
398 * when switching tabs.
399 */
400 public String mLastVoiceSearchTitle;
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500401 /**
402 * Whether the Intent which turned on voice search mode contained the
403 * String signifying that Google was the source.
404 */
405 public boolean mSourceIsGoogle;
406 /**
Leon Scroggins9df94972010-03-08 18:20:35 -0500407 * List of headers to be passed into the WebView containing location
408 * information
409 */
410 public ArrayList<Bundle> mHeaders;
411 /**
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500412 * The Intent used to invoke voice search. Placed on the
413 * WebHistoryItem so that when coming back to a previous voice search
414 * page we can again activate voice search.
415 */
Leon Scrogginse10dde52010-03-08 19:53:03 -0500416 public Intent mVoiceSearchIntent;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500417 /**
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500418 * String used to identify Google as the source of voice search.
419 */
420 public static String SOURCE_IS_GOOGLE
421 = "android.speech.extras.SOURCE_IS_GOOGLE";
Leon Scroggins58d56c62010-01-28 15:12:40 -0500422 }
423
Grace Kloba22ac16e2009-10-07 18:00:23 -0700424 // Container class for the next error dialog that needs to be displayed
425 private class ErrorDialog {
426 public final int mTitle;
427 public final String mDescription;
428 public final int mError;
429 ErrorDialog(int title, String desc, int error) {
430 mTitle = title;
431 mDescription = desc;
432 mError = error;
433 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700434 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700435
436 private void processNextError() {
437 if (mQueuedErrors == null) {
438 return;
439 }
440 // The first one is currently displayed so just remove it.
441 mQueuedErrors.removeFirst();
442 if (mQueuedErrors.size() == 0) {
443 mQueuedErrors = null;
444 return;
445 }
446 showError(mQueuedErrors.getFirst());
447 }
448
449 private DialogInterface.OnDismissListener mDialogListener =
450 new DialogInterface.OnDismissListener() {
451 public void onDismiss(DialogInterface d) {
452 processNextError();
453 }
454 };
455 private LinkedList<ErrorDialog> mQueuedErrors;
456
457 private void queueError(int err, String desc) {
458 if (mQueuedErrors == null) {
459 mQueuedErrors = new LinkedList<ErrorDialog>();
460 }
461 for (ErrorDialog d : mQueuedErrors) {
462 if (d.mError == err) {
463 // Already saw a similar error, ignore the new one.
464 return;
465 }
466 }
467 ErrorDialog errDialog = new ErrorDialog(
468 err == WebViewClient.ERROR_FILE_NOT_FOUND ?
469 R.string.browserFrameFileErrorLabel :
470 R.string.browserFrameNetworkErrorLabel,
471 desc, err);
472 mQueuedErrors.addLast(errDialog);
473
474 // Show the dialog now if the queue was empty and it is in foreground
475 if (mQueuedErrors.size() == 1 && mInForeground) {
476 showError(errDialog);
477 }
478 }
479
480 private void showError(ErrorDialog errDialog) {
481 if (mInForeground) {
482 AlertDialog d = new AlertDialog.Builder(mActivity)
483 .setTitle(errDialog.mTitle)
484 .setMessage(errDialog.mDescription)
485 .setPositiveButton(R.string.ok, null)
486 .create();
487 d.setOnDismissListener(mDialogListener);
488 d.show();
489 }
490 }
491
492 // -------------------------------------------------------------------------
493 // WebViewClient implementation for the main WebView
494 // -------------------------------------------------------------------------
495
496 private final WebViewClient mWebViewClient = new WebViewClient() {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500497 private Message mDontResend;
498 private Message mResend;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700499 @Override
500 public void onPageStarted(WebView view, String url, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700501 mInPageLoad = true;
John Reck30c714c2010-12-16 17:30:34 -0800502 mPageLoadProgress = 0;
503 mCurrentState = new PageState(mActivity,
504 view.isPrivateBrowsingEnabled(), url, favicon);
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000505 mLoadStartTime = SystemClock.uptimeMillis();
Leon Scroggins58d56c62010-01-28 15:12:40 -0500506 if (mVoiceSearchData != null
507 && !url.equals(mVoiceSearchData.mLastVoiceSearchUrl)) {
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500508 if (mVoiceSearchData.mSourceIsGoogle) {
509 Intent i = new Intent(LoggingEvents.ACTION_LOG_EVENT);
510 i.putExtra(LoggingEvents.EXTRA_FLUSH, true);
511 mActivity.sendBroadcast(i);
512 }
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400513 revertVoiceSearchMode();
Leon Scroggins58d56c62010-01-28 15:12:40 -0500514 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700515
Grace Kloba22ac16e2009-10-07 18:00:23 -0700516
517 // If we start a touch icon load and then load a new page, we don't
518 // want to cancel the current touch icon loader. But, we do want to
519 // create a new one when the touch icon url is known.
520 if (mTouchIconLoader != null) {
521 mTouchIconLoader.mTab = null;
522 mTouchIconLoader = null;
523 }
524
525 // reset the error console
526 if (mErrorConsole != null) {
527 mErrorConsole.clearErrorMessages();
Michael Kolb8233fac2010-10-26 16:08:53 -0700528 if (mWebViewController.shouldShowErrorConsole()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700529 mErrorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
530 }
531 }
532
Patrick Scott92066772011-03-10 08:46:27 -0500533 // Cancel the auto-login process.
534 if (mDeviceAccountLogin != null) {
535 mDeviceAccountLogin.cancel();
536 mDeviceAccountLogin = null;
537 mWebViewController.hideAutoLogin(Tab.this);
538 }
539
Grace Kloba22ac16e2009-10-07 18:00:23 -0700540 // finally update the UI in the activity if it is in the foreground
John Reck324d4402011-01-11 16:56:42 -0800541 mWebViewController.onPageStarted(Tab.this, view, favicon);
Leon Scroggins4cd97792010-12-03 15:31:56 -0500542
John Recke969cc52010-12-21 17:24:43 -0800543 updateBookmarkedStatus();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700544 }
545
546 @Override
547 public void onPageFinished(WebView view, String url) {
John Reck5b691842010-11-29 11:21:13 -0800548 if (!isPrivateBrowsingEnabled()) {
549 LogTag.logPageFinishedLoading(
550 url, SystemClock.uptimeMillis() - mLoadStartTime);
551 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700552 mInPageLoad = false;
John Reck30c714c2010-12-16 17:30:34 -0800553 // Sync state (in case of stop/timeout)
554 mCurrentState.mUrl = view.getUrl();
John Reck6c702ee2011-01-07 09:41:53 -0800555 if (mCurrentState.mUrl == null) {
556 mCurrentState.mUrl = url != null ? url : "";
557 }
John Reck30c714c2010-12-16 17:30:34 -0800558 mCurrentState.mTitle = view.getTitle();
559 mCurrentState.mFavicon = view.getFavicon();
560 if (!URLUtil.isHttpsUrl(mCurrentState.mUrl)) {
561 // In case we stop when loading an HTTPS page from an HTTP page
562 // but before a provisional load occurred
563 mCurrentState.mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
564 }
John Reck324d4402011-01-11 16:56:42 -0800565 mWebViewController.onPageFinished(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700566 }
567
568 // return true if want to hijack the url to let another app to handle it
569 @Override
570 public boolean shouldOverrideUrlLoading(WebView view, String url) {
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400571 if (voiceSearchSourceIsGoogle()) {
572 // This method is called when the user clicks on a link.
573 // VoiceSearchMode is turned off when the user leaves the
574 // Google results page, so at this point the user must be on
575 // that page. If the user clicked a link on that page, assume
576 // that the voice search was effective, and broadcast an Intent
577 // so a receiver can take note of that fact.
578 Intent logIntent = new Intent(LoggingEvents.ACTION_LOG_EVENT);
579 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
580 LoggingEvents.VoiceSearch.RESULT_CLICKED);
581 mActivity.sendBroadcast(logIntent);
582 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700583 if (mInForeground) {
Michael Kolb18eb3772010-12-10 14:29:51 -0800584 return mWebViewController.shouldOverrideUrlLoading(Tab.this,
585 view, url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700586 } else {
587 return false;
588 }
589 }
590
591 /**
592 * Updates the lock icon. This method is called when we discover another
593 * resource to be loaded for this page (for example, javascript). While
594 * we update the icon type, we do not update the lock icon itself until
595 * we are done loading, it is slightly more secure this way.
596 */
597 @Override
598 public void onLoadResource(WebView view, String url) {
599 if (url != null && url.length() > 0) {
600 // It is only if the page claims to be secure that we may have
601 // to update the lock:
John Reck30c714c2010-12-16 17:30:34 -0800602 if (mCurrentState.mLockIcon == LockIcon.LOCK_ICON_SECURE) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700603 // If NOT a 'safe' url, change the lock to mixed content!
604 if (!(URLUtil.isHttpsUrl(url) || URLUtil.isDataUrl(url)
605 || URLUtil.isAboutUrl(url))) {
John Reck30c714c2010-12-16 17:30:34 -0800606 mCurrentState.mLockIcon = LockIcon.LOCK_ICON_MIXED;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700607 }
608 }
609 }
610 }
611
612 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700613 * Show a dialog informing the user of the network error reported by
614 * WebCore if it is in the foreground.
615 */
616 @Override
617 public void onReceivedError(WebView view, int errorCode,
618 String description, String failingUrl) {
619 if (errorCode != WebViewClient.ERROR_HOST_LOOKUP &&
620 errorCode != WebViewClient.ERROR_CONNECT &&
621 errorCode != WebViewClient.ERROR_BAD_URL &&
622 errorCode != WebViewClient.ERROR_UNSUPPORTED_SCHEME &&
623 errorCode != WebViewClient.ERROR_FILE) {
624 queueError(errorCode, description);
625 }
Jeff Hamilton47654f42010-09-07 09:57:51 -0500626
627 // Don't log URLs when in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -0700628 if (!isPrivateBrowsingEnabled()) {
Jeff Hamilton47654f42010-09-07 09:57:51 -0500629 Log.e(LOGTAG, "onReceivedError " + errorCode + " " + failingUrl
630 + " " + description);
631 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700632 }
633
634 /**
635 * Check with the user if it is ok to resend POST data as the page they
636 * are trying to navigate to is the result of a POST.
637 */
638 @Override
639 public void onFormResubmission(WebView view, final Message dontResend,
640 final Message resend) {
641 if (!mInForeground) {
642 dontResend.sendToTarget();
643 return;
644 }
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500645 if (mDontResend != null) {
646 Log.w(LOGTAG, "onFormResubmission should not be called again "
647 + "while dialog is still up");
648 dontResend.sendToTarget();
649 return;
650 }
651 mDontResend = dontResend;
652 mResend = resend;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700653 new AlertDialog.Builder(mActivity).setTitle(
654 R.string.browserFrameFormResubmitLabel).setMessage(
655 R.string.browserFrameFormResubmitMessage)
656 .setPositiveButton(R.string.ok,
657 new DialogInterface.OnClickListener() {
658 public void onClick(DialogInterface dialog,
659 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500660 if (mResend != null) {
661 mResend.sendToTarget();
662 mResend = null;
663 mDontResend = null;
664 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700665 }
666 }).setNegativeButton(R.string.cancel,
667 new DialogInterface.OnClickListener() {
668 public void onClick(DialogInterface dialog,
669 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500670 if (mDontResend != null) {
671 mDontResend.sendToTarget();
672 mResend = null;
673 mDontResend = null;
674 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700675 }
676 }).setOnCancelListener(new OnCancelListener() {
677 public void onCancel(DialogInterface dialog) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500678 if (mDontResend != null) {
679 mDontResend.sendToTarget();
680 mResend = null;
681 mDontResend = null;
682 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700683 }
684 }).show();
685 }
686
687 /**
688 * Insert the url into the visited history database.
689 * @param url The url to be inserted.
690 * @param isReload True if this url is being reloaded.
691 * FIXME: Not sure what to do when reloading the page.
692 */
693 @Override
694 public void doUpdateVisitedHistory(WebView view, String url,
695 boolean isReload) {
John Reck324d4402011-01-11 16:56:42 -0800696 mWebViewController.doUpdateVisitedHistory(Tab.this, isReload);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700697 }
698
699 /**
700 * Displays SSL error(s) dialog to the user.
701 */
702 @Override
703 public void onReceivedSslError(final WebView view,
704 final SslErrorHandler handler, final SslError error) {
705 if (!mInForeground) {
706 handler.cancel();
John Reck30c714c2010-12-16 17:30:34 -0800707 setLockIconType(LockIcon.LOCK_ICON_UNSECURE);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700708 return;
709 }
John Reck35e9dd62011-04-25 09:01:54 -0700710 if (mSettings.showSecurityWarnings()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700711 final LayoutInflater factory =
712 LayoutInflater.from(mActivity);
713 final View warningsView =
714 factory.inflate(R.layout.ssl_warnings, null);
715 final LinearLayout placeholder =
716 (LinearLayout)warningsView.findViewById(R.id.placeholder);
717
718 if (error.hasError(SslError.SSL_UNTRUSTED)) {
719 LinearLayout ll = (LinearLayout)factory
720 .inflate(R.layout.ssl_warning, null);
721 ((TextView)ll.findViewById(R.id.warning))
722 .setText(R.string.ssl_untrusted);
723 placeholder.addView(ll);
724 }
725
726 if (error.hasError(SslError.SSL_IDMISMATCH)) {
727 LinearLayout ll = (LinearLayout)factory
728 .inflate(R.layout.ssl_warning, null);
729 ((TextView)ll.findViewById(R.id.warning))
730 .setText(R.string.ssl_mismatch);
731 placeholder.addView(ll);
732 }
733
734 if (error.hasError(SslError.SSL_EXPIRED)) {
735 LinearLayout ll = (LinearLayout)factory
736 .inflate(R.layout.ssl_warning, null);
737 ((TextView)ll.findViewById(R.id.warning))
738 .setText(R.string.ssl_expired);
739 placeholder.addView(ll);
740 }
741
742 if (error.hasError(SslError.SSL_NOTYETVALID)) {
743 LinearLayout ll = (LinearLayout)factory
744 .inflate(R.layout.ssl_warning, null);
745 ((TextView)ll.findViewById(R.id.warning))
746 .setText(R.string.ssl_not_yet_valid);
747 placeholder.addView(ll);
748 }
749
750 new AlertDialog.Builder(mActivity).setTitle(
751 R.string.security_warning).setIcon(
752 android.R.drawable.ic_dialog_alert).setView(
753 warningsView).setPositiveButton(R.string.ssl_continue,
754 new DialogInterface.OnClickListener() {
755 public void onClick(DialogInterface dialog,
756 int whichButton) {
757 handler.proceed();
758 }
759 }).setNeutralButton(R.string.view_certificate,
760 new DialogInterface.OnClickListener() {
761 public void onClick(DialogInterface dialog,
762 int whichButton) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700763 mWebViewController.showSslCertificateOnError(view,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700764 handler, error);
765 }
Ben Murdocha49b8292010-11-16 11:56:04 +0000766 }).setNegativeButton(R.string.ssl_go_back,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700767 new DialogInterface.OnClickListener() {
768 public void onClick(DialogInterface dialog,
769 int whichButton) {
John Reck30c714c2010-12-16 17:30:34 -0800770 dialog.cancel();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700771 }
772 }).setOnCancelListener(
773 new DialogInterface.OnCancelListener() {
774 public void onCancel(DialogInterface dialog) {
775 handler.cancel();
John Reck30c714c2010-12-16 17:30:34 -0800776 setLockIconType(LockIcon.LOCK_ICON_UNSECURE);
777 mWebViewController.onUserCanceledSsl(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700778 }
779 }).show();
780 } else {
781 handler.proceed();
782 }
783 }
784
785 /**
786 * Handles an HTTP authentication request.
787 *
788 * @param handler The authentication handler
789 * @param host The host
790 * @param realm The realm
791 */
792 @Override
793 public void onReceivedHttpAuthRequest(WebView view,
794 final HttpAuthHandler handler, final String host,
795 final String realm) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700796 mWebViewController.onReceivedHttpAuthRequest(Tab.this, view, handler, host, realm);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700797 }
798
799 @Override
John Reck438bf462011-01-12 18:11:46 -0800800 public WebResourceResponse shouldInterceptRequest(WebView view,
801 String url) {
802 WebResourceResponse res = HomeProvider.shouldInterceptRequest(
803 mActivity, url);
804 return res;
805 }
806
807 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700808 public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
809 if (!mInForeground) {
810 return false;
811 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700812 return mWebViewController.shouldOverrideKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700813 }
814
815 @Override
816 public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700817 if (!mInForeground) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700818 return;
819 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700820 mWebViewController.onUnhandledKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700821 }
Patrick Scott92066772011-03-10 08:46:27 -0500822
823 @Override
824 public void onReceivedLoginRequest(WebView view, String realm,
825 String account, String args) {
826 new DeviceAccountLogin(mActivity, view, Tab.this, mWebViewController)
827 .handleLogin(realm, account, args);
828 }
829
Grace Kloba22ac16e2009-10-07 18:00:23 -0700830 };
831
Patrick Scott92066772011-03-10 08:46:27 -0500832 // Called by DeviceAccountLogin when the Tab needs to have the auto-login UI
833 // displayed.
834 void setDeviceAccountLogin(DeviceAccountLogin login) {
835 mDeviceAccountLogin = login;
836 }
837
838 // Returns non-null if the title bar should display the auto-login UI.
839 DeviceAccountLogin getDeviceAccountLogin() {
840 return mDeviceAccountLogin;
841 }
842
Grace Kloba22ac16e2009-10-07 18:00:23 -0700843 // -------------------------------------------------------------------------
844 // WebChromeClient implementation for the main WebView
845 // -------------------------------------------------------------------------
846
847 private final WebChromeClient mWebChromeClient = new WebChromeClient() {
848 // Helper method to create a new tab or sub window.
849 private void createWindow(final boolean dialog, final Message msg) {
850 WebView.WebViewTransport transport =
851 (WebView.WebViewTransport) msg.obj;
852 if (dialog) {
853 createSubWindow();
Michael Kolb8233fac2010-10-26 16:08:53 -0700854 mWebViewController.attachSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700855 transport.setWebView(mSubView);
856 } else {
Michael Kolb7bcafde2011-05-09 13:55:59 -0700857 final Tab newTab = mWebViewController.openTab(null,
858 Tab.this.isPrivateBrowsingEnabled(),
859 true, true);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700860 if (newTab != Tab.this) {
861 Tab.this.addChildTab(newTab);
862 }
863 transport.setWebView(newTab.getWebView());
864 }
865 msg.sendToTarget();
866 }
867
868 @Override
869 public boolean onCreateWindow(WebView view, final boolean dialog,
870 final boolean userGesture, final Message resultMsg) {
871 // only allow new window or sub window for the foreground case
872 if (!mInForeground) {
873 return false;
874 }
875 // Short-circuit if we can't create any more tabs or sub windows.
876 if (dialog && mSubView != null) {
877 new AlertDialog.Builder(mActivity)
878 .setTitle(R.string.too_many_subwindows_dialog_title)
879 .setIcon(android.R.drawable.ic_dialog_alert)
880 .setMessage(R.string.too_many_subwindows_dialog_message)
881 .setPositiveButton(R.string.ok, null)
882 .show();
883 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700884 } else if (!mWebViewController.getTabControl().canCreateNewTab()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700885 new AlertDialog.Builder(mActivity)
886 .setTitle(R.string.too_many_windows_dialog_title)
887 .setIcon(android.R.drawable.ic_dialog_alert)
888 .setMessage(R.string.too_many_windows_dialog_message)
889 .setPositiveButton(R.string.ok, null)
890 .show();
891 return false;
892 }
893
894 // Short-circuit if this was a user gesture.
895 if (userGesture) {
896 createWindow(dialog, resultMsg);
897 return true;
898 }
899
900 // Allow the popup and create the appropriate window.
901 final AlertDialog.OnClickListener allowListener =
902 new AlertDialog.OnClickListener() {
903 public void onClick(DialogInterface d,
904 int which) {
905 createWindow(dialog, resultMsg);
906 }
907 };
908
909 // Block the popup by returning a null WebView.
910 final AlertDialog.OnClickListener blockListener =
911 new AlertDialog.OnClickListener() {
912 public void onClick(DialogInterface d, int which) {
913 resultMsg.sendToTarget();
914 }
915 };
916
917 // Build a confirmation dialog to display to the user.
918 final AlertDialog d =
919 new AlertDialog.Builder(mActivity)
920 .setTitle(R.string.attention)
921 .setIcon(android.R.drawable.ic_dialog_alert)
922 .setMessage(R.string.popup_window_attempt)
923 .setPositiveButton(R.string.allow, allowListener)
924 .setNegativeButton(R.string.block, blockListener)
925 .setCancelable(false)
926 .create();
927
928 // Show the confirmation dialog.
929 d.show();
930 return true;
931 }
932
933 @Override
Patrick Scotteb5061b2009-11-18 15:00:30 -0500934 public void onRequestFocus(WebView view) {
935 if (!mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700936 mWebViewController.switchToTab(Tab.this);
Patrick Scotteb5061b2009-11-18 15:00:30 -0500937 }
938 }
939
940 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700941 public void onCloseWindow(WebView window) {
Michael Kolbc831b632011-05-11 09:30:34 -0700942 if (mParent != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700943 // JavaScript can only close popup window.
944 if (mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700945 mWebViewController.switchToTab(mParent);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700946 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700947 mWebViewController.closeTab(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700948 }
949 }
950
951 @Override
952 public void onProgressChanged(WebView view, int newProgress) {
John Reck30c714c2010-12-16 17:30:34 -0800953 mPageLoadProgress = newProgress;
954 mWebViewController.onProgressChanged(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700955 }
956
957 @Override
Leon Scroggins21d9b902010-03-11 09:33:11 -0500958 public void onReceivedTitle(WebView view, final String title) {
John Reck30c714c2010-12-16 17:30:34 -0800959 mCurrentState.mTitle = title;
Michael Kolb8233fac2010-10-26 16:08:53 -0700960 mWebViewController.onReceivedTitle(Tab.this, title);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700961 }
962
963 @Override
964 public void onReceivedIcon(WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -0800965 mCurrentState.mFavicon = icon;
Michael Kolb8233fac2010-10-26 16:08:53 -0700966 mWebViewController.onFavicon(Tab.this, view, icon);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700967 }
968
969 @Override
970 public void onReceivedTouchIconUrl(WebView view, String url,
971 boolean precomposed) {
972 final ContentResolver cr = mActivity.getContentResolver();
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400973 // Let precomposed icons take precedence over non-composed
974 // icons.
975 if (precomposed && mTouchIconLoader != null) {
976 mTouchIconLoader.cancel(false);
977 mTouchIconLoader = null;
978 }
979 // Have only one async task at a time.
980 if (mTouchIconLoader == null) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700981 mTouchIconLoader = new DownloadTouchIcon(Tab.this,
982 mActivity, cr, view);
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400983 mTouchIconLoader.execute(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700984 }
985 }
986
987 @Override
988 public void onShowCustomView(View view,
989 WebChromeClient.CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700990 if (mInForeground) mWebViewController.showCustomView(Tab.this, view,
991 callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700992 }
993
994 @Override
995 public void onHideCustomView() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700996 if (mInForeground) mWebViewController.hideCustomView();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700997 }
998
999 /**
1000 * The origin has exceeded its database quota.
1001 * @param url the URL that exceeded the quota
1002 * @param databaseIdentifier the identifier of the database on which the
1003 * transaction that caused the quota overflow was run
1004 * @param currentQuota the current quota for the origin.
1005 * @param estimatedSize the estimated size of the database.
1006 * @param totalUsedQuota is the sum of all origins' quota.
1007 * @param quotaUpdater The callback to run when a decision to allow or
1008 * deny quota has been made. Don't forget to call this!
1009 */
1010 @Override
1011 public void onExceededDatabaseQuota(String url,
1012 String databaseIdentifier, long currentQuota, long estimatedSize,
1013 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -07001014 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001015 .onExceededDatabaseQuota(url, databaseIdentifier,
1016 currentQuota, estimatedSize, totalUsedQuota,
1017 quotaUpdater);
1018 }
1019
1020 /**
1021 * The Application Cache has exceeded its max size.
1022 * @param spaceNeeded is the amount of disk space that would be needed
1023 * in order for the last appcache operation to succeed.
1024 * @param totalUsedQuota is the sum of all origins' quota.
1025 * @param quotaUpdater A callback to inform the WebCore thread that a
1026 * new app cache size is available. This callback must always
1027 * be executed at some point to ensure that the sleeping
1028 * WebCore thread is woken up.
1029 */
1030 @Override
1031 public void onReachedMaxAppCacheSize(long spaceNeeded,
1032 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -07001033 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001034 .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota,
1035 quotaUpdater);
1036 }
1037
1038 /**
1039 * Instructs the browser to show a prompt to ask the user to set the
1040 * Geolocation permission state for the specified origin.
1041 * @param origin The origin for which Geolocation permissions are
1042 * requested.
1043 * @param callback The callback to call once the user has set the
1044 * Geolocation permission state.
1045 */
1046 @Override
1047 public void onGeolocationPermissionsShowPrompt(String origin,
1048 GeolocationPermissions.Callback callback) {
1049 if (mInForeground) {
Grace Kloba50c241e2010-04-20 11:07:50 -07001050 getGeolocationPermissionsPrompt().show(origin, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001051 }
1052 }
1053
1054 /**
1055 * Instructs the browser to hide the Geolocation permissions prompt.
1056 */
1057 @Override
1058 public void onGeolocationPermissionsHidePrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001059 if (mInForeground && mGeolocationPermissionsPrompt != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001060 mGeolocationPermissionsPrompt.hide();
1061 }
1062 }
1063
Ben Murdoch65acc352009-11-19 18:16:04 +00001064 /* Adds a JavaScript error message to the system log and if the JS
1065 * console is enabled in the about:debug options, to that console
1066 * also.
Ben Murdochc42addf2010-01-28 15:19:59 +00001067 * @param consoleMessage the message object.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001068 */
1069 @Override
Ben Murdochc42addf2010-01-28 15:19:59 +00001070 public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001071 if (mInForeground) {
1072 // call getErrorConsole(true) so it will create one if needed
1073 ErrorConsoleView errorConsole = getErrorConsole(true);
Ben Murdochc42addf2010-01-28 15:19:59 +00001074 errorConsole.addErrorMessage(consoleMessage);
Michael Kolb8233fac2010-10-26 16:08:53 -07001075 if (mWebViewController.shouldShowErrorConsole()
1076 && errorConsole.getShowState() !=
1077 ErrorConsoleView.SHOW_MAXIMIZED) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001078 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
1079 }
1080 }
Ben Murdochc42addf2010-01-28 15:19:59 +00001081
Jeff Hamilton47654f42010-09-07 09:57:51 -05001082 // Don't log console messages in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001083 if (isPrivateBrowsingEnabled()) return true;
Jeff Hamilton47654f42010-09-07 09:57:51 -05001084
Ben Murdochc42addf2010-01-28 15:19:59 +00001085 String message = "Console: " + consoleMessage.message() + " "
1086 + consoleMessage.sourceId() + ":"
1087 + consoleMessage.lineNumber();
1088
1089 switch (consoleMessage.messageLevel()) {
1090 case TIP:
1091 Log.v(CONSOLE_LOGTAG, message);
1092 break;
1093 case LOG:
1094 Log.i(CONSOLE_LOGTAG, message);
1095 break;
1096 case WARNING:
1097 Log.w(CONSOLE_LOGTAG, message);
1098 break;
1099 case ERROR:
1100 Log.e(CONSOLE_LOGTAG, message);
1101 break;
1102 case DEBUG:
1103 Log.d(CONSOLE_LOGTAG, message);
1104 break;
1105 }
1106
1107 return true;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001108 }
1109
1110 /**
1111 * Ask the browser for an icon to represent a <video> element.
1112 * This icon will be used if the Web page did not specify a poster attribute.
1113 * @return Bitmap The icon or null if no such icon is available.
1114 */
1115 @Override
1116 public Bitmap getDefaultVideoPoster() {
1117 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001118 return mWebViewController.getDefaultVideoPoster();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001119 }
1120 return null;
1121 }
1122
1123 /**
1124 * Ask the host application for a custom progress view to show while
1125 * a <video> is loading.
1126 * @return View The progress view.
1127 */
1128 @Override
1129 public View getVideoLoadingProgressView() {
1130 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001131 return mWebViewController.getVideoLoadingProgressView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001132 }
1133 return null;
1134 }
1135
1136 @Override
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01001137 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001138 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001139 mWebViewController.openFileChooser(uploadMsg, acceptType);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001140 } else {
1141 uploadMsg.onReceiveValue(null);
1142 }
1143 }
1144
1145 /**
1146 * Deliver a list of already-visited URLs
1147 */
1148 @Override
1149 public void getVisitedHistory(final ValueCallback<String[]> callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001150 mWebViewController.getVisitedHistory(callback);
1151 }
Ben Murdoch8029a772010-11-16 11:58:21 +00001152
1153 @Override
1154 public void setupAutoFill(Message message) {
1155 // Prompt the user to set up their profile.
1156 final Message msg = message;
1157 AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
Ben Murdoch1d676b62011-01-17 12:54:24 +00001158 LayoutInflater inflater = (LayoutInflater) mActivity.getSystemService(
1159 Context.LAYOUT_INFLATER_SERVICE);
1160 final View layout = inflater.inflate(R.layout.setup_autofill_dialog, null);
1161
1162 builder.setView(layout)
1163 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1164 @Override
1165 public void onClick(DialogInterface dialog, int id) {
1166 CheckBox disableAutoFill = (CheckBox) layout.findViewById(
1167 R.id.setup_autofill_dialog_disable_autofill);
1168
1169 if (disableAutoFill.isChecked()) {
1170 // Disable autofill and show a toast with how to turn it on again.
John Reck35e9dd62011-04-25 09:01:54 -07001171 mSettings.setAutofillEnabled(false);
Ben Murdoch1d676b62011-01-17 12:54:24 +00001172 Toast.makeText(mActivity,
1173 R.string.autofill_setup_dialog_negative_toast,
1174 Toast.LENGTH_LONG).show();
1175 } else {
1176 // Take user to the AutoFill profile editor. When they return,
1177 // we will send the message that we pass here which will trigger
1178 // the form to get filled out with their new profile.
1179 mWebViewController.setupAutoFill(msg);
1180 }
1181 }
1182 })
1183 .setNegativeButton(R.string.cancel, null)
1184 .show();
Ben Murdoch8029a772010-11-16 11:58:21 +00001185 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001186 };
1187
1188 // -------------------------------------------------------------------------
1189 // WebViewClient implementation for the sub window
1190 // -------------------------------------------------------------------------
1191
1192 // Subclass of WebViewClient used in subwindows to notify the main
1193 // WebViewClient of certain WebView activities.
1194 private static class SubWindowClient extends WebViewClient {
1195 // The main WebViewClient.
1196 private final WebViewClient mClient;
Michael Kolb8233fac2010-10-26 16:08:53 -07001197 private final WebViewController mController;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001198
Michael Kolb8233fac2010-10-26 16:08:53 -07001199 SubWindowClient(WebViewClient client, WebViewController controller) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001200 mClient = client;
Michael Kolb8233fac2010-10-26 16:08:53 -07001201 mController = controller;
Leon Scroggins III211ba542010-04-19 13:21:13 -04001202 }
1203 @Override
1204 public void onPageStarted(WebView view, String url, Bitmap favicon) {
1205 // Unlike the others, do not call mClient's version, which would
1206 // change the progress bar. However, we do want to remove the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001207 // find or select dialog.
Michael Kolb8233fac2010-10-26 16:08:53 -07001208 mController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001209 }
1210 @Override
1211 public void doUpdateVisitedHistory(WebView view, String url,
1212 boolean isReload) {
1213 mClient.doUpdateVisitedHistory(view, url, isReload);
1214 }
1215 @Override
1216 public boolean shouldOverrideUrlLoading(WebView view, String url) {
1217 return mClient.shouldOverrideUrlLoading(view, url);
1218 }
1219 @Override
1220 public void onReceivedSslError(WebView view, SslErrorHandler handler,
1221 SslError error) {
1222 mClient.onReceivedSslError(view, handler, error);
1223 }
1224 @Override
1225 public void onReceivedHttpAuthRequest(WebView view,
1226 HttpAuthHandler handler, String host, String realm) {
1227 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
1228 }
1229 @Override
1230 public void onFormResubmission(WebView view, Message dontResend,
1231 Message resend) {
1232 mClient.onFormResubmission(view, dontResend, resend);
1233 }
1234 @Override
1235 public void onReceivedError(WebView view, int errorCode,
1236 String description, String failingUrl) {
1237 mClient.onReceivedError(view, errorCode, description, failingUrl);
1238 }
1239 @Override
1240 public boolean shouldOverrideKeyEvent(WebView view,
1241 android.view.KeyEvent event) {
1242 return mClient.shouldOverrideKeyEvent(view, event);
1243 }
1244 @Override
1245 public void onUnhandledKeyEvent(WebView view,
1246 android.view.KeyEvent event) {
1247 mClient.onUnhandledKeyEvent(view, event);
1248 }
1249 }
1250
1251 // -------------------------------------------------------------------------
1252 // WebChromeClient implementation for the sub window
1253 // -------------------------------------------------------------------------
1254
1255 private class SubWindowChromeClient extends WebChromeClient {
1256 // The main WebChromeClient.
1257 private final WebChromeClient mClient;
1258
1259 SubWindowChromeClient(WebChromeClient client) {
1260 mClient = client;
1261 }
1262 @Override
1263 public void onProgressChanged(WebView view, int newProgress) {
1264 mClient.onProgressChanged(view, newProgress);
1265 }
1266 @Override
1267 public boolean onCreateWindow(WebView view, boolean dialog,
1268 boolean userGesture, android.os.Message resultMsg) {
1269 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
1270 }
1271 @Override
1272 public void onCloseWindow(WebView window) {
1273 if (window != mSubView) {
1274 Log.e(LOGTAG, "Can't close the window");
1275 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001276 mWebViewController.dismissSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001277 }
1278 }
1279
1280 // -------------------------------------------------------------------------
1281
Michael Kolb8233fac2010-10-26 16:08:53 -07001282 // TODO temporarily use activity here
1283 // remove later
1284
Grace Kloba22ac16e2009-10-07 18:00:23 -07001285 // Construct a new tab
Michael Kolb7bcafde2011-05-09 13:55:59 -07001286 Tab(WebViewController wvcontroller, WebView w) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001287 mWebViewController = wvcontroller;
1288 mActivity = mWebViewController.getActivity();
John Reck35e9dd62011-04-25 09:01:54 -07001289 mSettings = BrowserSettings.getInstance();
John Recke969cc52010-12-21 17:24:43 -08001290 mDataController = DataController.getInstance(mActivity);
John Reck847b5322011-04-14 17:02:18 -07001291 mCurrentState = new PageState(mActivity, w != null
1292 ? w.isPrivateBrowsingEnabled() : false);
Michael Kolb8233fac2010-10-26 16:08:53 -07001293 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001294 mInForeground = false;
1295
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001296 mDownloadListener = new DownloadListener() {
1297 public void onDownloadStart(String url, String userAgent,
1298 String contentDisposition, String mimetype,
1299 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001300 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001301 mimetype, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001302 }
1303 };
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001304 mWebBackForwardListClient = new WebBackForwardListClient() {
1305 @Override
1306 public void onNewHistoryItem(WebHistoryItem item) {
1307 if (isInVoiceSearchMode()) {
1308 item.setCustomData(mVoiceSearchData.mVoiceSearchIntent);
1309 }
1310 }
1311 @Override
1312 public void onIndexChanged(WebHistoryItem item, int index) {
1313 Object data = item.getCustomData();
1314 if (data != null && data instanceof Intent) {
1315 activateVoiceSearchMode((Intent) data);
1316 }
1317 }
1318 };
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001319
Grace Kloba22ac16e2009-10-07 18:00:23 -07001320 setWebView(w);
1321 }
1322
Michael Kolbc831b632011-05-11 09:30:34 -07001323 public void setId(long id) {
1324 mId = id;
1325 }
1326
1327 public long getId() {
1328 return mId;
1329 }
1330
Grace Kloba22ac16e2009-10-07 18:00:23 -07001331 /**
1332 * Sets the WebView for this tab, correctly removing the old WebView from
1333 * the container view.
1334 */
1335 void setWebView(WebView w) {
1336 if (mMainView == w) {
1337 return;
1338 }
Michael Kolba713ec82010-11-29 17:27:06 -08001339
Grace Kloba22ac16e2009-10-07 18:00:23 -07001340 // If the WebView is changing, the page will be reloaded, so any ongoing
1341 // Geolocation permission requests are void.
Grace Kloba50c241e2010-04-20 11:07:50 -07001342 if (mGeolocationPermissionsPrompt != null) {
1343 mGeolocationPermissionsPrompt.hide();
1344 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001345
Michael Kolba713ec82010-11-29 17:27:06 -08001346 mWebViewController.onSetWebView(this, w);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001347
1348 // set the new one
1349 mMainView = w;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001350 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001351 if (mMainView != null) {
1352 mMainView.setWebViewClient(mWebViewClient);
1353 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001354 // Attach DownloadManager so that downloads can start in an active
1355 // or a non-active window. This can happen when going to a site that
1356 // does a redirect after a period of time. The user could have
1357 // switched to another tab while waiting for the download to start.
1358 mMainView.setDownloadListener(mDownloadListener);
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001359 mMainView.setWebBackForwardListClient(mWebBackForwardListClient);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001360 }
1361 }
1362
1363 /**
1364 * Destroy the tab's main WebView and subWindow if any
1365 */
1366 void destroy() {
1367 if (mMainView != null) {
1368 dismissSubWindow();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001369 // save the WebView to call destroy() after detach it from the tab
1370 WebView webView = mMainView;
1371 setWebView(null);
1372 webView.destroy();
1373 }
1374 }
1375
1376 /**
1377 * Remove the tab from the parent
1378 */
1379 void removeFromTree() {
1380 // detach the children
Michael Kolbc831b632011-05-11 09:30:34 -07001381 if (mChildren != null) {
1382 for(Tab t : mChildren) {
1383 t.setParent(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001384 }
1385 }
1386 // remove itself from the parent list
Michael Kolbc831b632011-05-11 09:30:34 -07001387 if (mParent != null) {
1388 mParent.mChildren.remove(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001389 }
1390 }
1391
1392 /**
1393 * Create a new subwindow unless a subwindow already exists.
1394 * @return True if a new subwindow was created. False if one already exists.
1395 */
1396 boolean createSubWindow() {
1397 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001398 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001399 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001400 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001401 mSubView.setWebChromeClient(new SubWindowChromeClient(
1402 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001403 // Set a different DownloadListener for the mSubView, since it will
1404 // just need to dismiss the mSubView, rather than close the Tab
1405 mSubView.setDownloadListener(new DownloadListener() {
1406 public void onDownloadStart(String url, String userAgent,
1407 String contentDisposition, String mimetype,
1408 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001409 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001410 contentDisposition, mimetype, contentLength);
1411 if (mSubView.copyBackForwardList().getSize() == 0) {
1412 // This subwindow was opened for the sole purpose of
1413 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001414 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001415 }
1416 }
1417 });
Grace Kloba22ac16e2009-10-07 18:00:23 -07001418 mSubView.setOnCreateContextMenuListener(mActivity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001419 return true;
1420 }
1421 return false;
1422 }
1423
1424 /**
1425 * Dismiss the subWindow for the tab.
1426 */
1427 void dismissSubWindow() {
1428 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001429 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001430 mSubView.destroy();
1431 mSubView = null;
1432 mSubViewContainer = null;
1433 }
1434 }
1435
Grace Kloba22ac16e2009-10-07 18:00:23 -07001436
1437 /**
1438 * Set the parent tab of this tab.
1439 */
Michael Kolbc831b632011-05-11 09:30:34 -07001440 void setParent(Tab parent) {
1441 mParent = parent;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001442 // This tab may have been freed due to low memory. If that is the case,
Michael Kolbc831b632011-05-11 09:30:34 -07001443 // the parent tab id is already saved. If we are changing that id
Grace Kloba22ac16e2009-10-07 18:00:23 -07001444 // (most likely due to removing the parent tab) we must update the
Michael Kolbc831b632011-05-11 09:30:34 -07001445 // parent tab id in the saved Bundle.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001446 if (mSavedState != null) {
1447 if (parent == null) {
1448 mSavedState.remove(PARENTTAB);
1449 } else {
Michael Kolbc831b632011-05-11 09:30:34 -07001450 mSavedState.putLong(PARENTTAB, parent.getId());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001451 }
1452 }
John Reckb0a86db2011-05-24 14:05:58 -07001453
1454 // Sync the WebView useragent with the parent
1455 if (parent != null && mSettings.hasDesktopUseragent(parent.getWebView())
1456 != mSettings.hasDesktopUseragent(getWebView())) {
1457 mSettings.toggleDesktopUseragent(getWebView());
1458 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001459 }
1460
1461 /**
Michael Kolbc831b632011-05-11 09:30:34 -07001462 * If this Tab was created through another Tab, then this method returns
1463 * that Tab.
1464 * @return the Tab parent or null
1465 */
1466 public Tab getParent() {
1467 return mParent;
1468 }
1469
1470 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001471 * When a Tab is created through the content of another Tab, then we
1472 * associate the Tabs.
1473 * @param child the Tab that was created from this Tab
1474 */
1475 void addChildTab(Tab child) {
Michael Kolbc831b632011-05-11 09:30:34 -07001476 if (mChildren == null) {
1477 mChildren = new Vector<Tab>();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001478 }
Michael Kolbc831b632011-05-11 09:30:34 -07001479 mChildren.add(child);
1480 child.setParent(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001481 }
1482
Michael Kolbc831b632011-05-11 09:30:34 -07001483 Vector<Tab> getChildren() {
1484 return mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001485 }
1486
1487 void resume() {
1488 if (mMainView != null) {
1489 mMainView.onResume();
1490 if (mSubView != null) {
1491 mSubView.onResume();
1492 }
1493 }
1494 }
1495
1496 void pause() {
1497 if (mMainView != null) {
1498 mMainView.onPause();
1499 if (mSubView != null) {
1500 mSubView.onPause();
1501 }
1502 }
1503 }
1504
1505 void putInForeground() {
1506 mInForeground = true;
1507 resume();
1508 mMainView.setOnCreateContextMenuListener(mActivity);
1509 if (mSubView != null) {
1510 mSubView.setOnCreateContextMenuListener(mActivity);
1511 }
1512 // Show the pending error dialog if the queue is not empty
1513 if (mQueuedErrors != null && mQueuedErrors.size() > 0) {
1514 showError(mQueuedErrors.getFirst());
1515 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001516 mWebViewController.bookmarkedStatusHasChanged(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001517 }
1518
1519 void putInBackground() {
1520 mInForeground = false;
1521 pause();
1522 mMainView.setOnCreateContextMenuListener(null);
1523 if (mSubView != null) {
1524 mSubView.setOnCreateContextMenuListener(null);
1525 }
1526 }
1527
Michael Kolb8233fac2010-10-26 16:08:53 -07001528 boolean inForeground() {
1529 return mInForeground;
1530 }
1531
Grace Kloba22ac16e2009-10-07 18:00:23 -07001532 /**
1533 * Return the top window of this tab; either the subwindow if it is not
1534 * null or the main window.
1535 * @return The top window of this tab.
1536 */
1537 WebView getTopWindow() {
1538 if (mSubView != null) {
1539 return mSubView;
1540 }
1541 return mMainView;
1542 }
1543
1544 /**
1545 * Return the main window of this tab. Note: if a tab is freed in the
1546 * background, this can return null. It is only guaranteed to be
1547 * non-null for the current tab.
1548 * @return The main WebView of this tab.
1549 */
1550 WebView getWebView() {
1551 return mMainView;
1552 }
1553
Michael Kolba713ec82010-11-29 17:27:06 -08001554 void setViewContainer(View container) {
1555 mContainer = container;
1556 }
1557
Michael Kolb8233fac2010-10-26 16:08:53 -07001558 View getViewContainer() {
1559 return mContainer;
1560 }
1561
Grace Kloba22ac16e2009-10-07 18:00:23 -07001562 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001563 * Return whether private browsing is enabled for the main window of
1564 * this tab.
1565 * @return True if private browsing is enabled.
1566 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001567 boolean isPrivateBrowsingEnabled() {
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001568 WebView webView = getWebView();
1569 if (webView == null) {
1570 return false;
1571 }
1572 return webView.isPrivateBrowsingEnabled();
1573 }
1574
1575 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001576 * Return the subwindow of this tab or null if there is no subwindow.
1577 * @return The subwindow of this tab or null.
1578 */
1579 WebView getSubWebView() {
1580 return mSubView;
1581 }
1582
Michael Kolb1514bb72010-11-22 09:11:48 -08001583 void setSubWebView(WebView subView) {
1584 mSubView = subView;
1585 }
1586
Michael Kolb8233fac2010-10-26 16:08:53 -07001587 View getSubViewContainer() {
1588 return mSubViewContainer;
1589 }
1590
Michael Kolb1514bb72010-11-22 09:11:48 -08001591 void setSubViewContainer(View subViewContainer) {
1592 mSubViewContainer = subViewContainer;
1593 }
1594
Grace Kloba22ac16e2009-10-07 18:00:23 -07001595 /**
1596 * @return The geolocation permissions prompt for this tab.
1597 */
1598 GeolocationPermissionsPrompt getGeolocationPermissionsPrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001599 if (mGeolocationPermissionsPrompt == null) {
1600 ViewStub stub = (ViewStub) mContainer
1601 .findViewById(R.id.geolocation_permissions_prompt);
1602 mGeolocationPermissionsPrompt = (GeolocationPermissionsPrompt) stub
1603 .inflate();
1604 mGeolocationPermissionsPrompt.init();
1605 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001606 return mGeolocationPermissionsPrompt;
1607 }
1608
1609 /**
1610 * @return The application id string
1611 */
1612 String getAppId() {
1613 return mAppId;
1614 }
1615
1616 /**
1617 * Set the application id string
1618 * @param id
1619 */
1620 void setAppId(String id) {
1621 mAppId = id;
1622 }
1623
Grace Kloba22ac16e2009-10-07 18:00:23 -07001624 String getUrl() {
John Reck324d4402011-01-11 16:56:42 -08001625 return UrlUtils.filteredUrl(mCurrentState.mUrl);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001626 }
1627
John Reck49a603c2011-03-03 09:33:05 -08001628 String getOriginalUrl() {
1629 if (mMainView == null) {
1630 return "";
1631 }
1632 return UrlUtils.filteredUrl(mMainView.getOriginalUrl());
1633 }
1634
Grace Kloba22ac16e2009-10-07 18:00:23 -07001635 /**
John Reck30c714c2010-12-16 17:30:34 -08001636 * Get the title of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001637 */
1638 String getTitle() {
John Reck30c714c2010-12-16 17:30:34 -08001639 if (mCurrentState.mTitle == null && mInPageLoad) {
1640 return mActivity.getString(R.string.title_bar_loading);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001641 }
John Reck30c714c2010-12-16 17:30:34 -08001642 return mCurrentState.mTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001643 }
1644
1645 /**
John Reck30c714c2010-12-16 17:30:34 -08001646 * Get the favicon of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001647 */
1648 Bitmap getFavicon() {
John Reck30c714c2010-12-16 17:30:34 -08001649 return mCurrentState.mFavicon;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001650 }
1651
John Recke969cc52010-12-21 17:24:43 -08001652 public boolean isBookmarkedSite() {
1653 return mCurrentState.mIsBookmarkedSite;
1654 }
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001655
Grace Kloba22ac16e2009-10-07 18:00:23 -07001656 /**
1657 * Return the tab's error console. Creates the console if createIfNEcessary
1658 * is true and we haven't already created the console.
1659 * @param createIfNecessary Flag to indicate if the console should be
1660 * created if it has not been already.
1661 * @return The tab's error console, or null if one has not been created and
1662 * createIfNecessary is false.
1663 */
1664 ErrorConsoleView getErrorConsole(boolean createIfNecessary) {
1665 if (createIfNecessary && mErrorConsole == null) {
1666 mErrorConsole = new ErrorConsoleView(mActivity);
1667 mErrorConsole.setWebView(mMainView);
1668 }
1669 return mErrorConsole;
1670 }
1671
John Reck30c714c2010-12-16 17:30:34 -08001672 private void setLockIconType(LockIcon icon) {
1673 mCurrentState.mLockIcon = icon;
1674 mWebViewController.onUpdatedLockIcon(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001675 }
1676
1677 /**
1678 * @return The tab's lock icon type.
1679 */
John Reck30c714c2010-12-16 17:30:34 -08001680 LockIcon getLockIconType() {
1681 return mCurrentState.mLockIcon;
1682 }
1683
1684 int getLoadProgress() {
1685 if (mInPageLoad) {
1686 return mPageLoadProgress;
1687 }
1688 return 100;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001689 }
1690
1691 /**
1692 * @return TRUE if onPageStarted is called while onPageFinished is not
1693 * called yet.
1694 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001695 boolean inPageLoad() {
1696 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001697 }
1698
1699 // force mInLoad to be false. This should only be called before closing the
1700 // tab to ensure BrowserActivity's pauseWebViewTimers() is called correctly.
Michael Kolb8233fac2010-10-26 16:08:53 -07001701 void clearInPageLoad() {
1702 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001703 }
1704
Grace Kloba22ac16e2009-10-07 18:00:23 -07001705 /**
John Reck30c714c2010-12-16 17:30:34 -08001706 * Get the cached saved state bundle.
1707 * @return cached state bundle
Grace Kloba22ac16e2009-10-07 18:00:23 -07001708 */
1709 Bundle getSavedState() {
1710 return mSavedState;
1711 }
1712
1713 /**
1714 * Set the saved state.
1715 */
1716 void setSavedState(Bundle state) {
1717 mSavedState = state;
1718 }
1719
1720 /**
1721 * @return TRUE if succeed in saving the state.
1722 */
1723 boolean saveState() {
1724 // If the WebView is null it means we ran low on memory and we already
1725 // stored the saved state in mSavedState.
1726 if (mMainView == null) {
1727 return mSavedState != null;
1728 }
1729
1730 mSavedState = new Bundle();
1731 final WebBackForwardList list = mMainView.saveState(mSavedState);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001732
1733 // Store some extra info for displaying the tab in the picker.
1734 final WebHistoryItem item = list != null ? list.getCurrentItem() : null;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001735
Michael Kolbc831b632011-05-11 09:30:34 -07001736 mSavedState.putLong(ID, mId);
John Reck30c714c2010-12-16 17:30:34 -08001737 mSavedState.putString(CURRURL, mCurrentState.mUrl);
1738 mSavedState.putString(CURRTITLE, mCurrentState.mTitle);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001739 if (mAppId != null) {
1740 mSavedState.putString(APPID, mAppId);
1741 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001742 // Remember the parent tab so the relationship can be restored.
Michael Kolbc831b632011-05-11 09:30:34 -07001743 if (mParent != null) {
1744 mSavedState.putLong(PARENTTAB, mParent.mId);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001745 }
Michael Kolbeb95db42011-03-03 10:38:40 -08001746 if (mScreenshot != null) {
1747 mSavedState.putParcelable(SCREENSHOT, mScreenshot);
1748 }
John Reckb0a86db2011-05-24 14:05:58 -07001749 mSavedState.putBoolean(USERAGENT,
1750 mSettings.hasDesktopUseragent(getWebView()));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001751 return true;
1752 }
1753
1754 /*
1755 * Restore the state of the tab.
1756 */
1757 boolean restoreState(Bundle b) {
1758 if (b == null) {
1759 return false;
1760 }
1761 // Restore the internal state even if the WebView fails to restore.
1762 // This will maintain the app id, original url and close-on-exit values.
1763 mSavedState = null;
Michael Kolbc831b632011-05-11 09:30:34 -07001764 mId = b.getLong(ID);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001765 mAppId = b.getString(APPID);
Michael Kolbeb95db42011-03-03 10:38:40 -08001766 mScreenshot = b.getParcelable(SCREENSHOT);
John Reckb0a86db2011-05-24 14:05:58 -07001767 if (b.getBoolean(USERAGENT)
1768 != mSettings.hasDesktopUseragent(getWebView())) {
1769 mSettings.toggleDesktopUseragent(getWebView());
1770 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001771
1772 final WebBackForwardList list = mMainView.restoreState(b);
1773 if (list == null) {
1774 return false;
1775 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001776 return true;
1777 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04001778
Leon Scroggins1961ed22010-12-07 15:22:21 -05001779 public void updateBookmarkedStatus() {
John Recke969cc52010-12-21 17:24:43 -08001780 mDataController.queryBookmarkStatus(getUrl(), mIsBookmarkCallback);
Leon Scroggins1961ed22010-12-07 15:22:21 -05001781 }
1782
John Recke969cc52010-12-21 17:24:43 -08001783 private DataController.OnQueryUrlIsBookmark mIsBookmarkCallback
1784 = new DataController.OnQueryUrlIsBookmark() {
1785 @Override
1786 public void onQueryUrlIsBookmark(String url, boolean isBookmark) {
1787 if (mCurrentState.mUrl.equals(url)) {
1788 mCurrentState.mIsBookmarkedSite = isBookmark;
1789 mWebViewController.bookmarkedStatusHasChanged(Tab.this);
1790 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001791 }
John Recke969cc52010-12-21 17:24:43 -08001792 };
Michael Kolb1acef692011-03-08 14:12:06 -08001793
Michael Kolbeb95db42011-03-03 10:38:40 -08001794 public void setScreenshot(Bitmap screenshot) {
1795 mScreenshot = screenshot;
1796 }
1797
1798 public Bitmap getScreenshot() {
1799 return mScreenshot;
1800 }
1801
Grace Kloba22ac16e2009-10-07 18:00:23 -07001802}