blob: 4097334c14d5887feffc13fcb79ae21d3228f93d [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,
John Reck5949c662011-05-27 09:52:29 -0700858 Tab.this, true, true);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700859 transport.setWebView(newTab.getWebView());
860 }
861 msg.sendToTarget();
862 }
863
864 @Override
865 public boolean onCreateWindow(WebView view, final boolean dialog,
866 final boolean userGesture, final Message resultMsg) {
867 // only allow new window or sub window for the foreground case
868 if (!mInForeground) {
869 return false;
870 }
871 // Short-circuit if we can't create any more tabs or sub windows.
872 if (dialog && mSubView != null) {
873 new AlertDialog.Builder(mActivity)
874 .setTitle(R.string.too_many_subwindows_dialog_title)
875 .setIcon(android.R.drawable.ic_dialog_alert)
876 .setMessage(R.string.too_many_subwindows_dialog_message)
877 .setPositiveButton(R.string.ok, null)
878 .show();
879 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700880 } else if (!mWebViewController.getTabControl().canCreateNewTab()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700881 new AlertDialog.Builder(mActivity)
882 .setTitle(R.string.too_many_windows_dialog_title)
883 .setIcon(android.R.drawable.ic_dialog_alert)
884 .setMessage(R.string.too_many_windows_dialog_message)
885 .setPositiveButton(R.string.ok, null)
886 .show();
887 return false;
888 }
889
890 // Short-circuit if this was a user gesture.
891 if (userGesture) {
892 createWindow(dialog, resultMsg);
893 return true;
894 }
895
896 // Allow the popup and create the appropriate window.
897 final AlertDialog.OnClickListener allowListener =
898 new AlertDialog.OnClickListener() {
899 public void onClick(DialogInterface d,
900 int which) {
901 createWindow(dialog, resultMsg);
902 }
903 };
904
905 // Block the popup by returning a null WebView.
906 final AlertDialog.OnClickListener blockListener =
907 new AlertDialog.OnClickListener() {
908 public void onClick(DialogInterface d, int which) {
909 resultMsg.sendToTarget();
910 }
911 };
912
913 // Build a confirmation dialog to display to the user.
914 final AlertDialog d =
915 new AlertDialog.Builder(mActivity)
916 .setTitle(R.string.attention)
917 .setIcon(android.R.drawable.ic_dialog_alert)
918 .setMessage(R.string.popup_window_attempt)
919 .setPositiveButton(R.string.allow, allowListener)
920 .setNegativeButton(R.string.block, blockListener)
921 .setCancelable(false)
922 .create();
923
924 // Show the confirmation dialog.
925 d.show();
926 return true;
927 }
928
929 @Override
Patrick Scotteb5061b2009-11-18 15:00:30 -0500930 public void onRequestFocus(WebView view) {
931 if (!mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700932 mWebViewController.switchToTab(Tab.this);
Patrick Scotteb5061b2009-11-18 15:00:30 -0500933 }
934 }
935
936 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700937 public void onCloseWindow(WebView window) {
Michael Kolbc831b632011-05-11 09:30:34 -0700938 if (mParent != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700939 // JavaScript can only close popup window.
940 if (mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700941 mWebViewController.switchToTab(mParent);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700942 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700943 mWebViewController.closeTab(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700944 }
945 }
946
947 @Override
948 public void onProgressChanged(WebView view, int newProgress) {
John Reck30c714c2010-12-16 17:30:34 -0800949 mPageLoadProgress = newProgress;
950 mWebViewController.onProgressChanged(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700951 }
952
953 @Override
Leon Scroggins21d9b902010-03-11 09:33:11 -0500954 public void onReceivedTitle(WebView view, final String title) {
John Reck30c714c2010-12-16 17:30:34 -0800955 mCurrentState.mTitle = title;
Michael Kolb8233fac2010-10-26 16:08:53 -0700956 mWebViewController.onReceivedTitle(Tab.this, title);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700957 }
958
959 @Override
960 public void onReceivedIcon(WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -0800961 mCurrentState.mFavicon = icon;
Michael Kolb8233fac2010-10-26 16:08:53 -0700962 mWebViewController.onFavicon(Tab.this, view, icon);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700963 }
964
965 @Override
966 public void onReceivedTouchIconUrl(WebView view, String url,
967 boolean precomposed) {
968 final ContentResolver cr = mActivity.getContentResolver();
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400969 // Let precomposed icons take precedence over non-composed
970 // icons.
971 if (precomposed && mTouchIconLoader != null) {
972 mTouchIconLoader.cancel(false);
973 mTouchIconLoader = null;
974 }
975 // Have only one async task at a time.
976 if (mTouchIconLoader == null) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700977 mTouchIconLoader = new DownloadTouchIcon(Tab.this,
978 mActivity, cr, view);
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400979 mTouchIconLoader.execute(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700980 }
981 }
982
983 @Override
984 public void onShowCustomView(View view,
985 WebChromeClient.CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700986 if (mInForeground) mWebViewController.showCustomView(Tab.this, view,
987 callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700988 }
989
990 @Override
991 public void onHideCustomView() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700992 if (mInForeground) mWebViewController.hideCustomView();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700993 }
994
995 /**
996 * The origin has exceeded its database quota.
997 * @param url the URL that exceeded the quota
998 * @param databaseIdentifier the identifier of the database on which the
999 * transaction that caused the quota overflow was run
1000 * @param currentQuota the current quota for the origin.
1001 * @param estimatedSize the estimated size of the database.
1002 * @param totalUsedQuota is the sum of all origins' quota.
1003 * @param quotaUpdater The callback to run when a decision to allow or
1004 * deny quota has been made. Don't forget to call this!
1005 */
1006 @Override
1007 public void onExceededDatabaseQuota(String url,
1008 String databaseIdentifier, long currentQuota, long estimatedSize,
1009 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -07001010 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001011 .onExceededDatabaseQuota(url, databaseIdentifier,
1012 currentQuota, estimatedSize, totalUsedQuota,
1013 quotaUpdater);
1014 }
1015
1016 /**
1017 * The Application Cache has exceeded its max size.
1018 * @param spaceNeeded is the amount of disk space that would be needed
1019 * in order for the last appcache operation to succeed.
1020 * @param totalUsedQuota is the sum of all origins' quota.
1021 * @param quotaUpdater A callback to inform the WebCore thread that a
1022 * new app cache size is available. This callback must always
1023 * be executed at some point to ensure that the sleeping
1024 * WebCore thread is woken up.
1025 */
1026 @Override
1027 public void onReachedMaxAppCacheSize(long spaceNeeded,
1028 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -07001029 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001030 .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota,
1031 quotaUpdater);
1032 }
1033
1034 /**
1035 * Instructs the browser to show a prompt to ask the user to set the
1036 * Geolocation permission state for the specified origin.
1037 * @param origin The origin for which Geolocation permissions are
1038 * requested.
1039 * @param callback The callback to call once the user has set the
1040 * Geolocation permission state.
1041 */
1042 @Override
1043 public void onGeolocationPermissionsShowPrompt(String origin,
1044 GeolocationPermissions.Callback callback) {
1045 if (mInForeground) {
Grace Kloba50c241e2010-04-20 11:07:50 -07001046 getGeolocationPermissionsPrompt().show(origin, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001047 }
1048 }
1049
1050 /**
1051 * Instructs the browser to hide the Geolocation permissions prompt.
1052 */
1053 @Override
1054 public void onGeolocationPermissionsHidePrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001055 if (mInForeground && mGeolocationPermissionsPrompt != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001056 mGeolocationPermissionsPrompt.hide();
1057 }
1058 }
1059
Ben Murdoch65acc352009-11-19 18:16:04 +00001060 /* Adds a JavaScript error message to the system log and if the JS
1061 * console is enabled in the about:debug options, to that console
1062 * also.
Ben Murdochc42addf2010-01-28 15:19:59 +00001063 * @param consoleMessage the message object.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001064 */
1065 @Override
Ben Murdochc42addf2010-01-28 15:19:59 +00001066 public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001067 if (mInForeground) {
1068 // call getErrorConsole(true) so it will create one if needed
1069 ErrorConsoleView errorConsole = getErrorConsole(true);
Ben Murdochc42addf2010-01-28 15:19:59 +00001070 errorConsole.addErrorMessage(consoleMessage);
Michael Kolb8233fac2010-10-26 16:08:53 -07001071 if (mWebViewController.shouldShowErrorConsole()
1072 && errorConsole.getShowState() !=
1073 ErrorConsoleView.SHOW_MAXIMIZED) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001074 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
1075 }
1076 }
Ben Murdochc42addf2010-01-28 15:19:59 +00001077
Jeff Hamilton47654f42010-09-07 09:57:51 -05001078 // Don't log console messages in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001079 if (isPrivateBrowsingEnabled()) return true;
Jeff Hamilton47654f42010-09-07 09:57:51 -05001080
Ben Murdochc42addf2010-01-28 15:19:59 +00001081 String message = "Console: " + consoleMessage.message() + " "
1082 + consoleMessage.sourceId() + ":"
1083 + consoleMessage.lineNumber();
1084
1085 switch (consoleMessage.messageLevel()) {
1086 case TIP:
1087 Log.v(CONSOLE_LOGTAG, message);
1088 break;
1089 case LOG:
1090 Log.i(CONSOLE_LOGTAG, message);
1091 break;
1092 case WARNING:
1093 Log.w(CONSOLE_LOGTAG, message);
1094 break;
1095 case ERROR:
1096 Log.e(CONSOLE_LOGTAG, message);
1097 break;
1098 case DEBUG:
1099 Log.d(CONSOLE_LOGTAG, message);
1100 break;
1101 }
1102
1103 return true;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001104 }
1105
1106 /**
1107 * Ask the browser for an icon to represent a <video> element.
1108 * This icon will be used if the Web page did not specify a poster attribute.
1109 * @return Bitmap The icon or null if no such icon is available.
1110 */
1111 @Override
1112 public Bitmap getDefaultVideoPoster() {
1113 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001114 return mWebViewController.getDefaultVideoPoster();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001115 }
1116 return null;
1117 }
1118
1119 /**
1120 * Ask the host application for a custom progress view to show while
1121 * a <video> is loading.
1122 * @return View The progress view.
1123 */
1124 @Override
1125 public View getVideoLoadingProgressView() {
1126 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001127 return mWebViewController.getVideoLoadingProgressView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001128 }
1129 return null;
1130 }
1131
1132 @Override
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01001133 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001134 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001135 mWebViewController.openFileChooser(uploadMsg, acceptType);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001136 } else {
1137 uploadMsg.onReceiveValue(null);
1138 }
1139 }
1140
1141 /**
1142 * Deliver a list of already-visited URLs
1143 */
1144 @Override
1145 public void getVisitedHistory(final ValueCallback<String[]> callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001146 mWebViewController.getVisitedHistory(callback);
1147 }
Ben Murdoch8029a772010-11-16 11:58:21 +00001148
1149 @Override
1150 public void setupAutoFill(Message message) {
1151 // Prompt the user to set up their profile.
1152 final Message msg = message;
1153 AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
Ben Murdoch1d676b62011-01-17 12:54:24 +00001154 LayoutInflater inflater = (LayoutInflater) mActivity.getSystemService(
1155 Context.LAYOUT_INFLATER_SERVICE);
1156 final View layout = inflater.inflate(R.layout.setup_autofill_dialog, null);
1157
1158 builder.setView(layout)
1159 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1160 @Override
1161 public void onClick(DialogInterface dialog, int id) {
1162 CheckBox disableAutoFill = (CheckBox) layout.findViewById(
1163 R.id.setup_autofill_dialog_disable_autofill);
1164
1165 if (disableAutoFill.isChecked()) {
1166 // Disable autofill and show a toast with how to turn it on again.
John Reck35e9dd62011-04-25 09:01:54 -07001167 mSettings.setAutofillEnabled(false);
Ben Murdoch1d676b62011-01-17 12:54:24 +00001168 Toast.makeText(mActivity,
1169 R.string.autofill_setup_dialog_negative_toast,
1170 Toast.LENGTH_LONG).show();
1171 } else {
1172 // Take user to the AutoFill profile editor. When they return,
1173 // we will send the message that we pass here which will trigger
1174 // the form to get filled out with their new profile.
1175 mWebViewController.setupAutoFill(msg);
1176 }
1177 }
1178 })
1179 .setNegativeButton(R.string.cancel, null)
1180 .show();
Ben Murdoch8029a772010-11-16 11:58:21 +00001181 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001182 };
1183
1184 // -------------------------------------------------------------------------
1185 // WebViewClient implementation for the sub window
1186 // -------------------------------------------------------------------------
1187
1188 // Subclass of WebViewClient used in subwindows to notify the main
1189 // WebViewClient of certain WebView activities.
1190 private static class SubWindowClient extends WebViewClient {
1191 // The main WebViewClient.
1192 private final WebViewClient mClient;
Michael Kolb8233fac2010-10-26 16:08:53 -07001193 private final WebViewController mController;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001194
Michael Kolb8233fac2010-10-26 16:08:53 -07001195 SubWindowClient(WebViewClient client, WebViewController controller) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001196 mClient = client;
Michael Kolb8233fac2010-10-26 16:08:53 -07001197 mController = controller;
Leon Scroggins III211ba542010-04-19 13:21:13 -04001198 }
1199 @Override
1200 public void onPageStarted(WebView view, String url, Bitmap favicon) {
1201 // Unlike the others, do not call mClient's version, which would
1202 // change the progress bar. However, we do want to remove the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001203 // find or select dialog.
Michael Kolb8233fac2010-10-26 16:08:53 -07001204 mController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001205 }
1206 @Override
1207 public void doUpdateVisitedHistory(WebView view, String url,
1208 boolean isReload) {
1209 mClient.doUpdateVisitedHistory(view, url, isReload);
1210 }
1211 @Override
1212 public boolean shouldOverrideUrlLoading(WebView view, String url) {
1213 return mClient.shouldOverrideUrlLoading(view, url);
1214 }
1215 @Override
1216 public void onReceivedSslError(WebView view, SslErrorHandler handler,
1217 SslError error) {
1218 mClient.onReceivedSslError(view, handler, error);
1219 }
1220 @Override
1221 public void onReceivedHttpAuthRequest(WebView view,
1222 HttpAuthHandler handler, String host, String realm) {
1223 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
1224 }
1225 @Override
1226 public void onFormResubmission(WebView view, Message dontResend,
1227 Message resend) {
1228 mClient.onFormResubmission(view, dontResend, resend);
1229 }
1230 @Override
1231 public void onReceivedError(WebView view, int errorCode,
1232 String description, String failingUrl) {
1233 mClient.onReceivedError(view, errorCode, description, failingUrl);
1234 }
1235 @Override
1236 public boolean shouldOverrideKeyEvent(WebView view,
1237 android.view.KeyEvent event) {
1238 return mClient.shouldOverrideKeyEvent(view, event);
1239 }
1240 @Override
1241 public void onUnhandledKeyEvent(WebView view,
1242 android.view.KeyEvent event) {
1243 mClient.onUnhandledKeyEvent(view, event);
1244 }
1245 }
1246
1247 // -------------------------------------------------------------------------
1248 // WebChromeClient implementation for the sub window
1249 // -------------------------------------------------------------------------
1250
1251 private class SubWindowChromeClient extends WebChromeClient {
1252 // The main WebChromeClient.
1253 private final WebChromeClient mClient;
1254
1255 SubWindowChromeClient(WebChromeClient client) {
1256 mClient = client;
1257 }
1258 @Override
1259 public void onProgressChanged(WebView view, int newProgress) {
1260 mClient.onProgressChanged(view, newProgress);
1261 }
1262 @Override
1263 public boolean onCreateWindow(WebView view, boolean dialog,
1264 boolean userGesture, android.os.Message resultMsg) {
1265 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
1266 }
1267 @Override
1268 public void onCloseWindow(WebView window) {
1269 if (window != mSubView) {
1270 Log.e(LOGTAG, "Can't close the window");
1271 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001272 mWebViewController.dismissSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001273 }
1274 }
1275
1276 // -------------------------------------------------------------------------
1277
Michael Kolb8233fac2010-10-26 16:08:53 -07001278 // TODO temporarily use activity here
1279 // remove later
1280
Grace Kloba22ac16e2009-10-07 18:00:23 -07001281 // Construct a new tab
Michael Kolb7bcafde2011-05-09 13:55:59 -07001282 Tab(WebViewController wvcontroller, WebView w) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001283 mWebViewController = wvcontroller;
1284 mActivity = mWebViewController.getActivity();
John Reck35e9dd62011-04-25 09:01:54 -07001285 mSettings = BrowserSettings.getInstance();
John Recke969cc52010-12-21 17:24:43 -08001286 mDataController = DataController.getInstance(mActivity);
John Reck847b5322011-04-14 17:02:18 -07001287 mCurrentState = new PageState(mActivity, w != null
1288 ? w.isPrivateBrowsingEnabled() : false);
Michael Kolb8233fac2010-10-26 16:08:53 -07001289 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001290 mInForeground = false;
1291
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001292 mDownloadListener = new DownloadListener() {
1293 public void onDownloadStart(String url, String userAgent,
1294 String contentDisposition, String mimetype,
1295 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001296 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001297 mimetype, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001298 }
1299 };
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001300 mWebBackForwardListClient = new WebBackForwardListClient() {
1301 @Override
1302 public void onNewHistoryItem(WebHistoryItem item) {
1303 if (isInVoiceSearchMode()) {
1304 item.setCustomData(mVoiceSearchData.mVoiceSearchIntent);
1305 }
1306 }
1307 @Override
1308 public void onIndexChanged(WebHistoryItem item, int index) {
1309 Object data = item.getCustomData();
1310 if (data != null && data instanceof Intent) {
1311 activateVoiceSearchMode((Intent) data);
1312 }
1313 }
1314 };
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001315
Grace Kloba22ac16e2009-10-07 18:00:23 -07001316 setWebView(w);
1317 }
1318
Michael Kolbc831b632011-05-11 09:30:34 -07001319 public void setId(long id) {
1320 mId = id;
1321 }
1322
1323 public long getId() {
1324 return mId;
1325 }
1326
Grace Kloba22ac16e2009-10-07 18:00:23 -07001327 /**
1328 * Sets the WebView for this tab, correctly removing the old WebView from
1329 * the container view.
1330 */
1331 void setWebView(WebView w) {
1332 if (mMainView == w) {
1333 return;
1334 }
Michael Kolba713ec82010-11-29 17:27:06 -08001335
Grace Kloba22ac16e2009-10-07 18:00:23 -07001336 // If the WebView is changing, the page will be reloaded, so any ongoing
1337 // Geolocation permission requests are void.
Grace Kloba50c241e2010-04-20 11:07:50 -07001338 if (mGeolocationPermissionsPrompt != null) {
1339 mGeolocationPermissionsPrompt.hide();
1340 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001341
Michael Kolba713ec82010-11-29 17:27:06 -08001342 mWebViewController.onSetWebView(this, w);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001343
1344 // set the new one
1345 mMainView = w;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001346 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001347 if (mMainView != null) {
1348 mMainView.setWebViewClient(mWebViewClient);
1349 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001350 // Attach DownloadManager so that downloads can start in an active
1351 // or a non-active window. This can happen when going to a site that
1352 // does a redirect after a period of time. The user could have
1353 // switched to another tab while waiting for the download to start.
1354 mMainView.setDownloadListener(mDownloadListener);
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001355 mMainView.setWebBackForwardListClient(mWebBackForwardListClient);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001356 }
1357 }
1358
1359 /**
1360 * Destroy the tab's main WebView and subWindow if any
1361 */
1362 void destroy() {
1363 if (mMainView != null) {
1364 dismissSubWindow();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001365 // save the WebView to call destroy() after detach it from the tab
1366 WebView webView = mMainView;
1367 setWebView(null);
1368 webView.destroy();
1369 }
1370 }
1371
1372 /**
1373 * Remove the tab from the parent
1374 */
1375 void removeFromTree() {
1376 // detach the children
Michael Kolbc831b632011-05-11 09:30:34 -07001377 if (mChildren != null) {
1378 for(Tab t : mChildren) {
1379 t.setParent(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001380 }
1381 }
1382 // remove itself from the parent list
Michael Kolbc831b632011-05-11 09:30:34 -07001383 if (mParent != null) {
1384 mParent.mChildren.remove(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001385 }
1386 }
1387
1388 /**
1389 * Create a new subwindow unless a subwindow already exists.
1390 * @return True if a new subwindow was created. False if one already exists.
1391 */
1392 boolean createSubWindow() {
1393 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001394 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001395 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001396 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001397 mSubView.setWebChromeClient(new SubWindowChromeClient(
1398 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001399 // Set a different DownloadListener for the mSubView, since it will
1400 // just need to dismiss the mSubView, rather than close the Tab
1401 mSubView.setDownloadListener(new DownloadListener() {
1402 public void onDownloadStart(String url, String userAgent,
1403 String contentDisposition, String mimetype,
1404 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001405 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001406 contentDisposition, mimetype, contentLength);
1407 if (mSubView.copyBackForwardList().getSize() == 0) {
1408 // This subwindow was opened for the sole purpose of
1409 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001410 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001411 }
1412 }
1413 });
Grace Kloba22ac16e2009-10-07 18:00:23 -07001414 mSubView.setOnCreateContextMenuListener(mActivity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001415 return true;
1416 }
1417 return false;
1418 }
1419
1420 /**
1421 * Dismiss the subWindow for the tab.
1422 */
1423 void dismissSubWindow() {
1424 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001425 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001426 mSubView.destroy();
1427 mSubView = null;
1428 mSubViewContainer = null;
1429 }
1430 }
1431
Grace Kloba22ac16e2009-10-07 18:00:23 -07001432
1433 /**
1434 * Set the parent tab of this tab.
1435 */
Michael Kolbc831b632011-05-11 09:30:34 -07001436 void setParent(Tab parent) {
1437 mParent = parent;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001438 // This tab may have been freed due to low memory. If that is the case,
Michael Kolbc831b632011-05-11 09:30:34 -07001439 // the parent tab id is already saved. If we are changing that id
Grace Kloba22ac16e2009-10-07 18:00:23 -07001440 // (most likely due to removing the parent tab) we must update the
Michael Kolbc831b632011-05-11 09:30:34 -07001441 // parent tab id in the saved Bundle.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001442 if (mSavedState != null) {
1443 if (parent == null) {
1444 mSavedState.remove(PARENTTAB);
1445 } else {
Michael Kolbc831b632011-05-11 09:30:34 -07001446 mSavedState.putLong(PARENTTAB, parent.getId());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001447 }
1448 }
John Reckb0a86db2011-05-24 14:05:58 -07001449
1450 // Sync the WebView useragent with the parent
1451 if (parent != null && mSettings.hasDesktopUseragent(parent.getWebView())
1452 != mSettings.hasDesktopUseragent(getWebView())) {
1453 mSettings.toggleDesktopUseragent(getWebView());
1454 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001455 }
1456
1457 /**
Michael Kolbc831b632011-05-11 09:30:34 -07001458 * If this Tab was created through another Tab, then this method returns
1459 * that Tab.
1460 * @return the Tab parent or null
1461 */
1462 public Tab getParent() {
1463 return mParent;
1464 }
1465
1466 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001467 * When a Tab is created through the content of another Tab, then we
1468 * associate the Tabs.
1469 * @param child the Tab that was created from this Tab
1470 */
1471 void addChildTab(Tab child) {
Michael Kolbc831b632011-05-11 09:30:34 -07001472 if (mChildren == null) {
1473 mChildren = new Vector<Tab>();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001474 }
Michael Kolbc831b632011-05-11 09:30:34 -07001475 mChildren.add(child);
1476 child.setParent(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001477 }
1478
Michael Kolbc831b632011-05-11 09:30:34 -07001479 Vector<Tab> getChildren() {
1480 return mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001481 }
1482
1483 void resume() {
1484 if (mMainView != null) {
1485 mMainView.onResume();
1486 if (mSubView != null) {
1487 mSubView.onResume();
1488 }
1489 }
1490 }
1491
1492 void pause() {
1493 if (mMainView != null) {
1494 mMainView.onPause();
1495 if (mSubView != null) {
1496 mSubView.onPause();
1497 }
1498 }
1499 }
1500
1501 void putInForeground() {
1502 mInForeground = true;
1503 resume();
1504 mMainView.setOnCreateContextMenuListener(mActivity);
1505 if (mSubView != null) {
1506 mSubView.setOnCreateContextMenuListener(mActivity);
1507 }
1508 // Show the pending error dialog if the queue is not empty
1509 if (mQueuedErrors != null && mQueuedErrors.size() > 0) {
1510 showError(mQueuedErrors.getFirst());
1511 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001512 mWebViewController.bookmarkedStatusHasChanged(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001513 }
1514
1515 void putInBackground() {
1516 mInForeground = false;
1517 pause();
1518 mMainView.setOnCreateContextMenuListener(null);
1519 if (mSubView != null) {
1520 mSubView.setOnCreateContextMenuListener(null);
1521 }
1522 }
1523
Michael Kolb8233fac2010-10-26 16:08:53 -07001524 boolean inForeground() {
1525 return mInForeground;
1526 }
1527
Grace Kloba22ac16e2009-10-07 18:00:23 -07001528 /**
1529 * Return the top window of this tab; either the subwindow if it is not
1530 * null or the main window.
1531 * @return The top window of this tab.
1532 */
1533 WebView getTopWindow() {
1534 if (mSubView != null) {
1535 return mSubView;
1536 }
1537 return mMainView;
1538 }
1539
1540 /**
1541 * Return the main window of this tab. Note: if a tab is freed in the
1542 * background, this can return null. It is only guaranteed to be
1543 * non-null for the current tab.
1544 * @return The main WebView of this tab.
1545 */
1546 WebView getWebView() {
1547 return mMainView;
1548 }
1549
Michael Kolba713ec82010-11-29 17:27:06 -08001550 void setViewContainer(View container) {
1551 mContainer = container;
1552 }
1553
Michael Kolb8233fac2010-10-26 16:08:53 -07001554 View getViewContainer() {
1555 return mContainer;
1556 }
1557
Grace Kloba22ac16e2009-10-07 18:00:23 -07001558 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001559 * Return whether private browsing is enabled for the main window of
1560 * this tab.
1561 * @return True if private browsing is enabled.
1562 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001563 boolean isPrivateBrowsingEnabled() {
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001564 WebView webView = getWebView();
1565 if (webView == null) {
1566 return false;
1567 }
1568 return webView.isPrivateBrowsingEnabled();
1569 }
1570
1571 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001572 * Return the subwindow of this tab or null if there is no subwindow.
1573 * @return The subwindow of this tab or null.
1574 */
1575 WebView getSubWebView() {
1576 return mSubView;
1577 }
1578
Michael Kolb1514bb72010-11-22 09:11:48 -08001579 void setSubWebView(WebView subView) {
1580 mSubView = subView;
1581 }
1582
Michael Kolb8233fac2010-10-26 16:08:53 -07001583 View getSubViewContainer() {
1584 return mSubViewContainer;
1585 }
1586
Michael Kolb1514bb72010-11-22 09:11:48 -08001587 void setSubViewContainer(View subViewContainer) {
1588 mSubViewContainer = subViewContainer;
1589 }
1590
Grace Kloba22ac16e2009-10-07 18:00:23 -07001591 /**
1592 * @return The geolocation permissions prompt for this tab.
1593 */
1594 GeolocationPermissionsPrompt getGeolocationPermissionsPrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001595 if (mGeolocationPermissionsPrompt == null) {
1596 ViewStub stub = (ViewStub) mContainer
1597 .findViewById(R.id.geolocation_permissions_prompt);
1598 mGeolocationPermissionsPrompt = (GeolocationPermissionsPrompt) stub
1599 .inflate();
1600 mGeolocationPermissionsPrompt.init();
1601 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001602 return mGeolocationPermissionsPrompt;
1603 }
1604
1605 /**
1606 * @return The application id string
1607 */
1608 String getAppId() {
1609 return mAppId;
1610 }
1611
1612 /**
1613 * Set the application id string
1614 * @param id
1615 */
1616 void setAppId(String id) {
1617 mAppId = id;
1618 }
1619
Grace Kloba22ac16e2009-10-07 18:00:23 -07001620 String getUrl() {
John Reck324d4402011-01-11 16:56:42 -08001621 return UrlUtils.filteredUrl(mCurrentState.mUrl);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001622 }
1623
John Reck49a603c2011-03-03 09:33:05 -08001624 String getOriginalUrl() {
1625 if (mMainView == null) {
1626 return "";
1627 }
1628 return UrlUtils.filteredUrl(mMainView.getOriginalUrl());
1629 }
1630
Grace Kloba22ac16e2009-10-07 18:00:23 -07001631 /**
John Reck30c714c2010-12-16 17:30:34 -08001632 * Get the title of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001633 */
1634 String getTitle() {
John Reck30c714c2010-12-16 17:30:34 -08001635 if (mCurrentState.mTitle == null && mInPageLoad) {
1636 return mActivity.getString(R.string.title_bar_loading);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001637 }
John Reck30c714c2010-12-16 17:30:34 -08001638 return mCurrentState.mTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001639 }
1640
1641 /**
John Reck30c714c2010-12-16 17:30:34 -08001642 * Get the favicon of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001643 */
1644 Bitmap getFavicon() {
John Reck30c714c2010-12-16 17:30:34 -08001645 return mCurrentState.mFavicon;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001646 }
1647
John Recke969cc52010-12-21 17:24:43 -08001648 public boolean isBookmarkedSite() {
1649 return mCurrentState.mIsBookmarkedSite;
1650 }
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001651
Grace Kloba22ac16e2009-10-07 18:00:23 -07001652 /**
1653 * Return the tab's error console. Creates the console if createIfNEcessary
1654 * is true and we haven't already created the console.
1655 * @param createIfNecessary Flag to indicate if the console should be
1656 * created if it has not been already.
1657 * @return The tab's error console, or null if one has not been created and
1658 * createIfNecessary is false.
1659 */
1660 ErrorConsoleView getErrorConsole(boolean createIfNecessary) {
1661 if (createIfNecessary && mErrorConsole == null) {
1662 mErrorConsole = new ErrorConsoleView(mActivity);
1663 mErrorConsole.setWebView(mMainView);
1664 }
1665 return mErrorConsole;
1666 }
1667
John Reck30c714c2010-12-16 17:30:34 -08001668 private void setLockIconType(LockIcon icon) {
1669 mCurrentState.mLockIcon = icon;
1670 mWebViewController.onUpdatedLockIcon(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001671 }
1672
1673 /**
1674 * @return The tab's lock icon type.
1675 */
John Reck30c714c2010-12-16 17:30:34 -08001676 LockIcon getLockIconType() {
1677 return mCurrentState.mLockIcon;
1678 }
1679
1680 int getLoadProgress() {
1681 if (mInPageLoad) {
1682 return mPageLoadProgress;
1683 }
1684 return 100;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001685 }
1686
1687 /**
1688 * @return TRUE if onPageStarted is called while onPageFinished is not
1689 * called yet.
1690 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001691 boolean inPageLoad() {
1692 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001693 }
1694
1695 // force mInLoad to be false. This should only be called before closing the
1696 // tab to ensure BrowserActivity's pauseWebViewTimers() is called correctly.
Michael Kolb8233fac2010-10-26 16:08:53 -07001697 void clearInPageLoad() {
1698 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001699 }
1700
Grace Kloba22ac16e2009-10-07 18:00:23 -07001701 /**
John Reck30c714c2010-12-16 17:30:34 -08001702 * Get the cached saved state bundle.
1703 * @return cached state bundle
Grace Kloba22ac16e2009-10-07 18:00:23 -07001704 */
1705 Bundle getSavedState() {
1706 return mSavedState;
1707 }
1708
1709 /**
1710 * Set the saved state.
1711 */
1712 void setSavedState(Bundle state) {
1713 mSavedState = state;
1714 }
1715
1716 /**
1717 * @return TRUE if succeed in saving the state.
1718 */
1719 boolean saveState() {
1720 // If the WebView is null it means we ran low on memory and we already
1721 // stored the saved state in mSavedState.
1722 if (mMainView == null) {
1723 return mSavedState != null;
1724 }
1725
1726 mSavedState = new Bundle();
1727 final WebBackForwardList list = mMainView.saveState(mSavedState);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001728
1729 // Store some extra info for displaying the tab in the picker.
1730 final WebHistoryItem item = list != null ? list.getCurrentItem() : null;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001731
Michael Kolbc831b632011-05-11 09:30:34 -07001732 mSavedState.putLong(ID, mId);
John Reck30c714c2010-12-16 17:30:34 -08001733 mSavedState.putString(CURRURL, mCurrentState.mUrl);
1734 mSavedState.putString(CURRTITLE, mCurrentState.mTitle);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001735 if (mAppId != null) {
1736 mSavedState.putString(APPID, mAppId);
1737 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001738 // Remember the parent tab so the relationship can be restored.
Michael Kolbc831b632011-05-11 09:30:34 -07001739 if (mParent != null) {
1740 mSavedState.putLong(PARENTTAB, mParent.mId);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001741 }
Michael Kolbeb95db42011-03-03 10:38:40 -08001742 if (mScreenshot != null) {
1743 mSavedState.putParcelable(SCREENSHOT, mScreenshot);
1744 }
John Reckb0a86db2011-05-24 14:05:58 -07001745 mSavedState.putBoolean(USERAGENT,
1746 mSettings.hasDesktopUseragent(getWebView()));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001747 return true;
1748 }
1749
1750 /*
1751 * Restore the state of the tab.
1752 */
1753 boolean restoreState(Bundle b) {
1754 if (b == null) {
1755 return false;
1756 }
1757 // Restore the internal state even if the WebView fails to restore.
1758 // This will maintain the app id, original url and close-on-exit values.
1759 mSavedState = null;
Michael Kolbc831b632011-05-11 09:30:34 -07001760 mId = b.getLong(ID);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001761 mAppId = b.getString(APPID);
Michael Kolbeb95db42011-03-03 10:38:40 -08001762 mScreenshot = b.getParcelable(SCREENSHOT);
John Reckb0a86db2011-05-24 14:05:58 -07001763 if (b.getBoolean(USERAGENT)
1764 != mSettings.hasDesktopUseragent(getWebView())) {
1765 mSettings.toggleDesktopUseragent(getWebView());
1766 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001767
1768 final WebBackForwardList list = mMainView.restoreState(b);
1769 if (list == null) {
1770 return false;
1771 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001772 return true;
1773 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04001774
Leon Scroggins1961ed22010-12-07 15:22:21 -05001775 public void updateBookmarkedStatus() {
John Recke969cc52010-12-21 17:24:43 -08001776 mDataController.queryBookmarkStatus(getUrl(), mIsBookmarkCallback);
Leon Scroggins1961ed22010-12-07 15:22:21 -05001777 }
1778
John Recke969cc52010-12-21 17:24:43 -08001779 private DataController.OnQueryUrlIsBookmark mIsBookmarkCallback
1780 = new DataController.OnQueryUrlIsBookmark() {
1781 @Override
1782 public void onQueryUrlIsBookmark(String url, boolean isBookmark) {
1783 if (mCurrentState.mUrl.equals(url)) {
1784 mCurrentState.mIsBookmarkedSite = isBookmark;
1785 mWebViewController.bookmarkedStatusHasChanged(Tab.this);
1786 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001787 }
John Recke969cc52010-12-21 17:24:43 -08001788 };
Michael Kolb1acef692011-03-08 14:12:06 -08001789
Michael Kolbeb95db42011-03-03 10:38:40 -08001790 public void setScreenshot(Bitmap screenshot) {
1791 mScreenshot = screenshot;
1792 }
1793
1794 public Bitmap getScreenshot() {
1795 return mScreenshot;
1796 }
1797
Grace Kloba22ac16e2009-10-07 18:00:23 -07001798}