blob: e1dd1ca0ba5dbb29e832c16f9aaa8f4d1c91beb7 [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";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700194
195 // -------------------------------------------------------------------------
196
Leon Scroggins58d56c62010-01-28 15:12:40 -0500197 /**
198 * Private information regarding the latest voice search. If the Tab is not
199 * in voice search mode, this will be null.
200 */
201 private VoiceSearchData mVoiceSearchData;
202 /**
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400203 * Remove voice search mode from this tab.
204 */
205 public void revertVoiceSearchMode() {
206 if (mVoiceSearchData != null) {
207 mVoiceSearchData = null;
208 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700209 mWebViewController.revertVoiceSearchMode(this);
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400210 }
211 }
212 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700213
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400214 /**
Leon Scroggins58d56c62010-01-28 15:12:40 -0500215 * Return whether the tab is in voice search mode.
216 */
217 public boolean isInVoiceSearchMode() {
218 return mVoiceSearchData != null;
219 }
220 /**
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400221 * Return true if the Tab is in voice search mode and the voice search
222 * Intent came with a String identifying that Google provided the Intent.
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500223 */
224 public boolean voiceSearchSourceIsGoogle() {
225 return mVoiceSearchData != null && mVoiceSearchData.mSourceIsGoogle;
226 }
227 /**
Leon Scroggins58d56c62010-01-28 15:12:40 -0500228 * Get the title to display for the current voice search page. If the Tab
229 * is not in voice search mode, return null.
230 */
231 public String getVoiceDisplayTitle() {
232 if (mVoiceSearchData == null) return null;
233 return mVoiceSearchData.mLastVoiceSearchTitle;
234 }
235 /**
236 * Get the latest array of voice search results, to be passed to the
237 * BrowserProvider. If the Tab is not in voice search mode, return null.
238 */
239 public ArrayList<String> getVoiceSearchResults() {
240 if (mVoiceSearchData == null) return null;
241 return mVoiceSearchData.mVoiceSearchResults;
242 }
243 /**
244 * Activate voice search mode.
245 * @param intent Intent which has the results to use, or an index into the
246 * results when reusing the old results.
247 */
248 /* package */ void activateVoiceSearchMode(Intent intent) {
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500249 int index = 0;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500250 ArrayList<String> results = intent.getStringArrayListExtra(
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -0500251 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_STRINGS);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500252 if (results != null) {
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500253 ArrayList<String> urls = intent.getStringArrayListExtra(
254 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_URLS);
255 ArrayList<String> htmls = intent.getStringArrayListExtra(
256 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_HTML);
257 ArrayList<String> baseUrls = intent.getStringArrayListExtra(
258 RecognizerResultsIntent
259 .EXTRA_VOICE_SEARCH_RESULT_HTML_BASE_URLS);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500260 // This tab is now entering voice search mode for the first time, or
261 // a new voice search was done.
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500262 int size = results.size();
263 if (urls == null || size != urls.size()) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500264 throw new AssertionError("improper extras passed in Intent");
265 }
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500266 if (htmls == null || htmls.size() != size || baseUrls == null ||
267 (baseUrls.size() != size && baseUrls.size() != 1)) {
268 // If either of these arrays are empty/incorrectly sized, ignore
269 // them.
270 htmls = null;
271 baseUrls = null;
272 }
273 mVoiceSearchData = new VoiceSearchData(results, urls, htmls,
274 baseUrls);
Leon Scroggins9df94972010-03-08 18:20:35 -0500275 mVoiceSearchData.mHeaders = intent.getParcelableArrayListExtra(
276 RecognizerResultsIntent
277 .EXTRA_VOICE_SEARCH_RESULT_HTTP_HEADERS);
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500278 mVoiceSearchData.mSourceIsGoogle = intent.getBooleanExtra(
279 VoiceSearchData.SOURCE_IS_GOOGLE, false);
Leon Scroggins2ee4a5a2010-03-15 16:56:57 -0400280 mVoiceSearchData.mVoiceSearchIntent = new Intent(intent);
Leon Scrogginse10dde52010-03-08 19:53:03 -0500281 }
282 String extraData = intent.getStringExtra(
283 SearchManager.EXTRA_DATA_KEY);
284 if (extraData != null) {
285 index = Integer.parseInt(extraData);
286 if (index >= mVoiceSearchData.mVoiceSearchResults.size()) {
287 throw new AssertionError("index must be less than "
288 + "size of mVoiceSearchResults");
289 }
290 if (mVoiceSearchData.mSourceIsGoogle) {
291 Intent logIntent = new Intent(
292 LoggingEvents.ACTION_LOG_EVENT);
293 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
294 LoggingEvents.VoiceSearch.N_BEST_CHOOSE);
295 logIntent.putExtra(
296 LoggingEvents.VoiceSearch.EXTRA_N_BEST_CHOOSE_INDEX,
297 index);
298 mActivity.sendBroadcast(logIntent);
299 }
300 if (mVoiceSearchData.mVoiceSearchIntent != null) {
Leon Scroggins2ee4a5a2010-03-15 16:56:57 -0400301 // Copy the Intent, so that each history item will have its own
302 // Intent, with different (or none) extra data.
303 Intent latest = new Intent(mVoiceSearchData.mVoiceSearchIntent);
304 latest.putExtra(SearchManager.EXTRA_DATA_KEY, extraData);
305 mVoiceSearchData.mVoiceSearchIntent = latest;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500306 }
307 }
308 mVoiceSearchData.mLastVoiceSearchTitle
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500309 = mVoiceSearchData.mVoiceSearchResults.get(index);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500310 if (mInForeground) {
Michael Kolb11d19782011-03-20 10:17:40 -0700311 mWebViewController.activateVoiceSearchMode(
312 mVoiceSearchData.mLastVoiceSearchTitle,
313 mVoiceSearchData.mVoiceSearchResults);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500314 }
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500315 if (mVoiceSearchData.mVoiceSearchHtmls != null) {
316 // When index was found it was already ensured that it was valid
317 String uriString = mVoiceSearchData.mVoiceSearchHtmls.get(index);
318 if (uriString != null) {
319 Uri dataUri = Uri.parse(uriString);
320 if (RecognizerResultsIntent.URI_SCHEME_INLINE.equals(
321 dataUri.getScheme())) {
322 // If there is only one base URL, use it. If there are
323 // more, there will be one for each index, so use the base
324 // URL corresponding to the index.
325 String baseUrl = mVoiceSearchData.mVoiceSearchBaseUrls.get(
326 mVoiceSearchData.mVoiceSearchBaseUrls.size() > 1 ?
327 index : 0);
328 mVoiceSearchData.mLastVoiceSearchUrl = baseUrl;
329 mMainView.loadDataWithBaseURL(baseUrl,
330 uriString.substring(RecognizerResultsIntent
331 .URI_SCHEME_INLINE.length() + 1), "text/html",
332 "utf-8", baseUrl);
333 return;
334 }
335 }
336 }
Leon Scroggins58d56c62010-01-28 15:12:40 -0500337 mVoiceSearchData.mLastVoiceSearchUrl
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500338 = mVoiceSearchData.mVoiceSearchUrls.get(index);
339 if (null == mVoiceSearchData.mLastVoiceSearchUrl) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700340 mVoiceSearchData.mLastVoiceSearchUrl = UrlUtils.smartUrlFilter(
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500341 mVoiceSearchData.mLastVoiceSearchTitle);
342 }
Leon Scroggins9df94972010-03-08 18:20:35 -0500343 Map<String, String> headers = null;
344 if (mVoiceSearchData.mHeaders != null) {
345 int bundleIndex = mVoiceSearchData.mHeaders.size() == 1 ? 0
346 : index;
347 Bundle bundle = mVoiceSearchData.mHeaders.get(bundleIndex);
348 if (bundle != null && !bundle.isEmpty()) {
349 Iterator<String> iter = bundle.keySet().iterator();
350 headers = new HashMap<String, String>();
351 while (iter.hasNext()) {
352 String key = iter.next();
353 headers.put(key, bundle.getString(key));
354 }
355 }
356 }
357 mMainView.loadUrl(mVoiceSearchData.mLastVoiceSearchUrl, headers);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500358 }
359 /* package */ static class VoiceSearchData {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500360 public VoiceSearchData(ArrayList<String> results,
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500361 ArrayList<String> urls, ArrayList<String> htmls,
362 ArrayList<String> baseUrls) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500363 mVoiceSearchResults = results;
364 mVoiceSearchUrls = urls;
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500365 mVoiceSearchHtmls = htmls;
366 mVoiceSearchBaseUrls = baseUrls;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500367 }
368 /*
369 * ArrayList of suggestions to be displayed when opening the
370 * SearchManager
371 */
372 public ArrayList<String> mVoiceSearchResults;
373 /*
374 * ArrayList of urls, associated with the suggestions in
375 * mVoiceSearchResults.
376 */
377 public ArrayList<String> mVoiceSearchUrls;
378 /*
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500379 * ArrayList holding content to load for each item in
380 * mVoiceSearchResults.
381 */
382 public ArrayList<String> mVoiceSearchHtmls;
383 /*
384 * ArrayList holding base urls for the items in mVoiceSearchResults.
385 * If non null, this will either have the same size as
386 * mVoiceSearchResults or have a size of 1, in which case all will use
387 * the same base url
388 */
389 public ArrayList<String> mVoiceSearchBaseUrls;
390 /*
Leon Scroggins58d56c62010-01-28 15:12:40 -0500391 * The last url provided by voice search. Used for comparison to see if
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500392 * we are going to a page by some method besides voice search.
Leon Scroggins58d56c62010-01-28 15:12:40 -0500393 */
394 public String mLastVoiceSearchUrl;
395 /**
396 * The last title used for voice search. Needed to update the title bar
397 * when switching tabs.
398 */
399 public String mLastVoiceSearchTitle;
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500400 /**
401 * Whether the Intent which turned on voice search mode contained the
402 * String signifying that Google was the source.
403 */
404 public boolean mSourceIsGoogle;
405 /**
Leon Scroggins9df94972010-03-08 18:20:35 -0500406 * List of headers to be passed into the WebView containing location
407 * information
408 */
409 public ArrayList<Bundle> mHeaders;
410 /**
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500411 * The Intent used to invoke voice search. Placed on the
412 * WebHistoryItem so that when coming back to a previous voice search
413 * page we can again activate voice search.
414 */
Leon Scrogginse10dde52010-03-08 19:53:03 -0500415 public Intent mVoiceSearchIntent;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500416 /**
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500417 * String used to identify Google as the source of voice search.
418 */
419 public static String SOURCE_IS_GOOGLE
420 = "android.speech.extras.SOURCE_IS_GOOGLE";
Leon Scroggins58d56c62010-01-28 15:12:40 -0500421 }
422
Grace Kloba22ac16e2009-10-07 18:00:23 -0700423 // Container class for the next error dialog that needs to be displayed
424 private class ErrorDialog {
425 public final int mTitle;
426 public final String mDescription;
427 public final int mError;
428 ErrorDialog(int title, String desc, int error) {
429 mTitle = title;
430 mDescription = desc;
431 mError = error;
432 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700433 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700434
435 private void processNextError() {
436 if (mQueuedErrors == null) {
437 return;
438 }
439 // The first one is currently displayed so just remove it.
440 mQueuedErrors.removeFirst();
441 if (mQueuedErrors.size() == 0) {
442 mQueuedErrors = null;
443 return;
444 }
445 showError(mQueuedErrors.getFirst());
446 }
447
448 private DialogInterface.OnDismissListener mDialogListener =
449 new DialogInterface.OnDismissListener() {
450 public void onDismiss(DialogInterface d) {
451 processNextError();
452 }
453 };
454 private LinkedList<ErrorDialog> mQueuedErrors;
455
456 private void queueError(int err, String desc) {
457 if (mQueuedErrors == null) {
458 mQueuedErrors = new LinkedList<ErrorDialog>();
459 }
460 for (ErrorDialog d : mQueuedErrors) {
461 if (d.mError == err) {
462 // Already saw a similar error, ignore the new one.
463 return;
464 }
465 }
466 ErrorDialog errDialog = new ErrorDialog(
467 err == WebViewClient.ERROR_FILE_NOT_FOUND ?
468 R.string.browserFrameFileErrorLabel :
469 R.string.browserFrameNetworkErrorLabel,
470 desc, err);
471 mQueuedErrors.addLast(errDialog);
472
473 // Show the dialog now if the queue was empty and it is in foreground
474 if (mQueuedErrors.size() == 1 && mInForeground) {
475 showError(errDialog);
476 }
477 }
478
479 private void showError(ErrorDialog errDialog) {
480 if (mInForeground) {
481 AlertDialog d = new AlertDialog.Builder(mActivity)
482 .setTitle(errDialog.mTitle)
483 .setMessage(errDialog.mDescription)
484 .setPositiveButton(R.string.ok, null)
485 .create();
486 d.setOnDismissListener(mDialogListener);
487 d.show();
488 }
489 }
490
491 // -------------------------------------------------------------------------
492 // WebViewClient implementation for the main WebView
493 // -------------------------------------------------------------------------
494
495 private final WebViewClient mWebViewClient = new WebViewClient() {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500496 private Message mDontResend;
497 private Message mResend;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700498 @Override
499 public void onPageStarted(WebView view, String url, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700500 mInPageLoad = true;
John Reck30c714c2010-12-16 17:30:34 -0800501 mPageLoadProgress = 0;
502 mCurrentState = new PageState(mActivity,
503 view.isPrivateBrowsingEnabled(), url, favicon);
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000504 mLoadStartTime = SystemClock.uptimeMillis();
Leon Scroggins58d56c62010-01-28 15:12:40 -0500505 if (mVoiceSearchData != null
506 && !url.equals(mVoiceSearchData.mLastVoiceSearchUrl)) {
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500507 if (mVoiceSearchData.mSourceIsGoogle) {
508 Intent i = new Intent(LoggingEvents.ACTION_LOG_EVENT);
509 i.putExtra(LoggingEvents.EXTRA_FLUSH, true);
510 mActivity.sendBroadcast(i);
511 }
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400512 revertVoiceSearchMode();
Leon Scroggins58d56c62010-01-28 15:12:40 -0500513 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700514
Grace Kloba22ac16e2009-10-07 18:00:23 -0700515
516 // If we start a touch icon load and then load a new page, we don't
517 // want to cancel the current touch icon loader. But, we do want to
518 // create a new one when the touch icon url is known.
519 if (mTouchIconLoader != null) {
520 mTouchIconLoader.mTab = null;
521 mTouchIconLoader = null;
522 }
523
524 // reset the error console
525 if (mErrorConsole != null) {
526 mErrorConsole.clearErrorMessages();
Michael Kolb8233fac2010-10-26 16:08:53 -0700527 if (mWebViewController.shouldShowErrorConsole()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700528 mErrorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
529 }
530 }
531
Patrick Scott92066772011-03-10 08:46:27 -0500532 // Cancel the auto-login process.
533 if (mDeviceAccountLogin != null) {
534 mDeviceAccountLogin.cancel();
535 mDeviceAccountLogin = null;
536 mWebViewController.hideAutoLogin(Tab.this);
537 }
538
Grace Kloba22ac16e2009-10-07 18:00:23 -0700539 // finally update the UI in the activity if it is in the foreground
John Reck324d4402011-01-11 16:56:42 -0800540 mWebViewController.onPageStarted(Tab.this, view, favicon);
Leon Scroggins4cd97792010-12-03 15:31:56 -0500541
John Recke969cc52010-12-21 17:24:43 -0800542 updateBookmarkedStatus();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700543 }
544
545 @Override
546 public void onPageFinished(WebView view, String url) {
John Reck5b691842010-11-29 11:21:13 -0800547 if (!isPrivateBrowsingEnabled()) {
548 LogTag.logPageFinishedLoading(
549 url, SystemClock.uptimeMillis() - mLoadStartTime);
550 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700551 mInPageLoad = false;
John Reck30c714c2010-12-16 17:30:34 -0800552 // Sync state (in case of stop/timeout)
553 mCurrentState.mUrl = view.getUrl();
John Reck6c702ee2011-01-07 09:41:53 -0800554 if (mCurrentState.mUrl == null) {
555 mCurrentState.mUrl = url != null ? url : "";
556 }
John Reck30c714c2010-12-16 17:30:34 -0800557 mCurrentState.mTitle = view.getTitle();
558 mCurrentState.mFavicon = view.getFavicon();
559 if (!URLUtil.isHttpsUrl(mCurrentState.mUrl)) {
560 // In case we stop when loading an HTTPS page from an HTTP page
561 // but before a provisional load occurred
562 mCurrentState.mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
563 }
John Reck324d4402011-01-11 16:56:42 -0800564 mWebViewController.onPageFinished(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700565 }
566
567 // return true if want to hijack the url to let another app to handle it
568 @Override
569 public boolean shouldOverrideUrlLoading(WebView view, String url) {
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400570 if (voiceSearchSourceIsGoogle()) {
571 // This method is called when the user clicks on a link.
572 // VoiceSearchMode is turned off when the user leaves the
573 // Google results page, so at this point the user must be on
574 // that page. If the user clicked a link on that page, assume
575 // that the voice search was effective, and broadcast an Intent
576 // so a receiver can take note of that fact.
577 Intent logIntent = new Intent(LoggingEvents.ACTION_LOG_EVENT);
578 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
579 LoggingEvents.VoiceSearch.RESULT_CLICKED);
580 mActivity.sendBroadcast(logIntent);
581 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700582 if (mInForeground) {
Michael Kolb18eb3772010-12-10 14:29:51 -0800583 return mWebViewController.shouldOverrideUrlLoading(Tab.this,
584 view, url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700585 } else {
586 return false;
587 }
588 }
589
590 /**
591 * Updates the lock icon. This method is called when we discover another
592 * resource to be loaded for this page (for example, javascript). While
593 * we update the icon type, we do not update the lock icon itself until
594 * we are done loading, it is slightly more secure this way.
595 */
596 @Override
597 public void onLoadResource(WebView view, String url) {
598 if (url != null && url.length() > 0) {
599 // It is only if the page claims to be secure that we may have
600 // to update the lock:
John Reck30c714c2010-12-16 17:30:34 -0800601 if (mCurrentState.mLockIcon == LockIcon.LOCK_ICON_SECURE) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700602 // If NOT a 'safe' url, change the lock to mixed content!
603 if (!(URLUtil.isHttpsUrl(url) || URLUtil.isDataUrl(url)
604 || URLUtil.isAboutUrl(url))) {
John Reck30c714c2010-12-16 17:30:34 -0800605 mCurrentState.mLockIcon = LockIcon.LOCK_ICON_MIXED;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700606 }
607 }
608 }
609 }
610
611 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700612 * Show a dialog informing the user of the network error reported by
613 * WebCore if it is in the foreground.
614 */
615 @Override
616 public void onReceivedError(WebView view, int errorCode,
617 String description, String failingUrl) {
618 if (errorCode != WebViewClient.ERROR_HOST_LOOKUP &&
619 errorCode != WebViewClient.ERROR_CONNECT &&
620 errorCode != WebViewClient.ERROR_BAD_URL &&
621 errorCode != WebViewClient.ERROR_UNSUPPORTED_SCHEME &&
622 errorCode != WebViewClient.ERROR_FILE) {
623 queueError(errorCode, description);
624 }
Jeff Hamilton47654f42010-09-07 09:57:51 -0500625
626 // Don't log URLs when in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -0700627 if (!isPrivateBrowsingEnabled()) {
Jeff Hamilton47654f42010-09-07 09:57:51 -0500628 Log.e(LOGTAG, "onReceivedError " + errorCode + " " + failingUrl
629 + " " + description);
630 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700631 }
632
633 /**
634 * Check with the user if it is ok to resend POST data as the page they
635 * are trying to navigate to is the result of a POST.
636 */
637 @Override
638 public void onFormResubmission(WebView view, final Message dontResend,
639 final Message resend) {
640 if (!mInForeground) {
641 dontResend.sendToTarget();
642 return;
643 }
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500644 if (mDontResend != null) {
645 Log.w(LOGTAG, "onFormResubmission should not be called again "
646 + "while dialog is still up");
647 dontResend.sendToTarget();
648 return;
649 }
650 mDontResend = dontResend;
651 mResend = resend;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700652 new AlertDialog.Builder(mActivity).setTitle(
653 R.string.browserFrameFormResubmitLabel).setMessage(
654 R.string.browserFrameFormResubmitMessage)
655 .setPositiveButton(R.string.ok,
656 new DialogInterface.OnClickListener() {
657 public void onClick(DialogInterface dialog,
658 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500659 if (mResend != null) {
660 mResend.sendToTarget();
661 mResend = null;
662 mDontResend = null;
663 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700664 }
665 }).setNegativeButton(R.string.cancel,
666 new DialogInterface.OnClickListener() {
667 public void onClick(DialogInterface dialog,
668 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500669 if (mDontResend != null) {
670 mDontResend.sendToTarget();
671 mResend = null;
672 mDontResend = null;
673 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700674 }
675 }).setOnCancelListener(new OnCancelListener() {
676 public void onCancel(DialogInterface dialog) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500677 if (mDontResend != null) {
678 mDontResend.sendToTarget();
679 mResend = null;
680 mDontResend = null;
681 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700682 }
683 }).show();
684 }
685
686 /**
687 * Insert the url into the visited history database.
688 * @param url The url to be inserted.
689 * @param isReload True if this url is being reloaded.
690 * FIXME: Not sure what to do when reloading the page.
691 */
692 @Override
693 public void doUpdateVisitedHistory(WebView view, String url,
694 boolean isReload) {
John Reck324d4402011-01-11 16:56:42 -0800695 mWebViewController.doUpdateVisitedHistory(Tab.this, isReload);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700696 }
697
698 /**
699 * Displays SSL error(s) dialog to the user.
700 */
701 @Override
702 public void onReceivedSslError(final WebView view,
703 final SslErrorHandler handler, final SslError error) {
704 if (!mInForeground) {
705 handler.cancel();
John Reck30c714c2010-12-16 17:30:34 -0800706 setLockIconType(LockIcon.LOCK_ICON_UNSECURE);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700707 return;
708 }
John Reck35e9dd62011-04-25 09:01:54 -0700709 if (mSettings.showSecurityWarnings()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700710 final LayoutInflater factory =
711 LayoutInflater.from(mActivity);
712 final View warningsView =
713 factory.inflate(R.layout.ssl_warnings, null);
714 final LinearLayout placeholder =
715 (LinearLayout)warningsView.findViewById(R.id.placeholder);
716
717 if (error.hasError(SslError.SSL_UNTRUSTED)) {
718 LinearLayout ll = (LinearLayout)factory
719 .inflate(R.layout.ssl_warning, null);
720 ((TextView)ll.findViewById(R.id.warning))
721 .setText(R.string.ssl_untrusted);
722 placeholder.addView(ll);
723 }
724
725 if (error.hasError(SslError.SSL_IDMISMATCH)) {
726 LinearLayout ll = (LinearLayout)factory
727 .inflate(R.layout.ssl_warning, null);
728 ((TextView)ll.findViewById(R.id.warning))
729 .setText(R.string.ssl_mismatch);
730 placeholder.addView(ll);
731 }
732
733 if (error.hasError(SslError.SSL_EXPIRED)) {
734 LinearLayout ll = (LinearLayout)factory
735 .inflate(R.layout.ssl_warning, null);
736 ((TextView)ll.findViewById(R.id.warning))
737 .setText(R.string.ssl_expired);
738 placeholder.addView(ll);
739 }
740
741 if (error.hasError(SslError.SSL_NOTYETVALID)) {
742 LinearLayout ll = (LinearLayout)factory
743 .inflate(R.layout.ssl_warning, null);
744 ((TextView)ll.findViewById(R.id.warning))
745 .setText(R.string.ssl_not_yet_valid);
746 placeholder.addView(ll);
747 }
748
749 new AlertDialog.Builder(mActivity).setTitle(
750 R.string.security_warning).setIcon(
751 android.R.drawable.ic_dialog_alert).setView(
752 warningsView).setPositiveButton(R.string.ssl_continue,
753 new DialogInterface.OnClickListener() {
754 public void onClick(DialogInterface dialog,
755 int whichButton) {
756 handler.proceed();
757 }
758 }).setNeutralButton(R.string.view_certificate,
759 new DialogInterface.OnClickListener() {
760 public void onClick(DialogInterface dialog,
761 int whichButton) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700762 mWebViewController.showSslCertificateOnError(view,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700763 handler, error);
764 }
Ben Murdocha49b8292010-11-16 11:56:04 +0000765 }).setNegativeButton(R.string.ssl_go_back,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700766 new DialogInterface.OnClickListener() {
767 public void onClick(DialogInterface dialog,
768 int whichButton) {
John Reck30c714c2010-12-16 17:30:34 -0800769 dialog.cancel();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700770 }
771 }).setOnCancelListener(
772 new DialogInterface.OnCancelListener() {
773 public void onCancel(DialogInterface dialog) {
774 handler.cancel();
John Reck30c714c2010-12-16 17:30:34 -0800775 setLockIconType(LockIcon.LOCK_ICON_UNSECURE);
776 mWebViewController.onUserCanceledSsl(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700777 }
778 }).show();
779 } else {
780 handler.proceed();
781 }
782 }
783
784 /**
785 * Handles an HTTP authentication request.
786 *
787 * @param handler The authentication handler
788 * @param host The host
789 * @param realm The realm
790 */
791 @Override
792 public void onReceivedHttpAuthRequest(WebView view,
793 final HttpAuthHandler handler, final String host,
794 final String realm) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700795 mWebViewController.onReceivedHttpAuthRequest(Tab.this, view, handler, host, realm);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700796 }
797
798 @Override
John Reck438bf462011-01-12 18:11:46 -0800799 public WebResourceResponse shouldInterceptRequest(WebView view,
800 String url) {
801 WebResourceResponse res = HomeProvider.shouldInterceptRequest(
802 mActivity, url);
803 return res;
804 }
805
806 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700807 public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
808 if (!mInForeground) {
809 return false;
810 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700811 return mWebViewController.shouldOverrideKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700812 }
813
814 @Override
815 public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700816 if (!mInForeground) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700817 return;
818 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700819 mWebViewController.onUnhandledKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700820 }
Patrick Scott92066772011-03-10 08:46:27 -0500821
822 @Override
823 public void onReceivedLoginRequest(WebView view, String realm,
824 String account, String args) {
825 new DeviceAccountLogin(mActivity, view, Tab.this, mWebViewController)
826 .handleLogin(realm, account, args);
827 }
828
Grace Kloba22ac16e2009-10-07 18:00:23 -0700829 };
830
Patrick Scott92066772011-03-10 08:46:27 -0500831 // Called by DeviceAccountLogin when the Tab needs to have the auto-login UI
832 // displayed.
833 void setDeviceAccountLogin(DeviceAccountLogin login) {
834 mDeviceAccountLogin = login;
835 }
836
837 // Returns non-null if the title bar should display the auto-login UI.
838 DeviceAccountLogin getDeviceAccountLogin() {
839 return mDeviceAccountLogin;
840 }
841
Grace Kloba22ac16e2009-10-07 18:00:23 -0700842 // -------------------------------------------------------------------------
843 // WebChromeClient implementation for the main WebView
844 // -------------------------------------------------------------------------
845
846 private final WebChromeClient mWebChromeClient = new WebChromeClient() {
847 // Helper method to create a new tab or sub window.
848 private void createWindow(final boolean dialog, final Message msg) {
849 WebView.WebViewTransport transport =
850 (WebView.WebViewTransport) msg.obj;
851 if (dialog) {
852 createSubWindow();
Michael Kolb8233fac2010-10-26 16:08:53 -0700853 mWebViewController.attachSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700854 transport.setWebView(mSubView);
855 } else {
Michael Kolb7bcafde2011-05-09 13:55:59 -0700856 final Tab newTab = mWebViewController.openTab(null,
857 Tab.this.isPrivateBrowsingEnabled(),
858 true, true);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700859 if (newTab != Tab.this) {
860 Tab.this.addChildTab(newTab);
861 }
862 transport.setWebView(newTab.getWebView());
863 }
864 msg.sendToTarget();
865 }
866
867 @Override
868 public boolean onCreateWindow(WebView view, final boolean dialog,
869 final boolean userGesture, final Message resultMsg) {
870 // only allow new window or sub window for the foreground case
871 if (!mInForeground) {
872 return false;
873 }
874 // Short-circuit if we can't create any more tabs or sub windows.
875 if (dialog && mSubView != null) {
876 new AlertDialog.Builder(mActivity)
877 .setTitle(R.string.too_many_subwindows_dialog_title)
878 .setIcon(android.R.drawable.ic_dialog_alert)
879 .setMessage(R.string.too_many_subwindows_dialog_message)
880 .setPositiveButton(R.string.ok, null)
881 .show();
882 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700883 } else if (!mWebViewController.getTabControl().canCreateNewTab()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700884 new AlertDialog.Builder(mActivity)
885 .setTitle(R.string.too_many_windows_dialog_title)
886 .setIcon(android.R.drawable.ic_dialog_alert)
887 .setMessage(R.string.too_many_windows_dialog_message)
888 .setPositiveButton(R.string.ok, null)
889 .show();
890 return false;
891 }
892
893 // Short-circuit if this was a user gesture.
894 if (userGesture) {
895 createWindow(dialog, resultMsg);
896 return true;
897 }
898
899 // Allow the popup and create the appropriate window.
900 final AlertDialog.OnClickListener allowListener =
901 new AlertDialog.OnClickListener() {
902 public void onClick(DialogInterface d,
903 int which) {
904 createWindow(dialog, resultMsg);
905 }
906 };
907
908 // Block the popup by returning a null WebView.
909 final AlertDialog.OnClickListener blockListener =
910 new AlertDialog.OnClickListener() {
911 public void onClick(DialogInterface d, int which) {
912 resultMsg.sendToTarget();
913 }
914 };
915
916 // Build a confirmation dialog to display to the user.
917 final AlertDialog d =
918 new AlertDialog.Builder(mActivity)
919 .setTitle(R.string.attention)
920 .setIcon(android.R.drawable.ic_dialog_alert)
921 .setMessage(R.string.popup_window_attempt)
922 .setPositiveButton(R.string.allow, allowListener)
923 .setNegativeButton(R.string.block, blockListener)
924 .setCancelable(false)
925 .create();
926
927 // Show the confirmation dialog.
928 d.show();
929 return true;
930 }
931
932 @Override
Patrick Scotteb5061b2009-11-18 15:00:30 -0500933 public void onRequestFocus(WebView view) {
934 if (!mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700935 mWebViewController.switchToTab(Tab.this);
Patrick Scotteb5061b2009-11-18 15:00:30 -0500936 }
937 }
938
939 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700940 public void onCloseWindow(WebView window) {
Michael Kolbc831b632011-05-11 09:30:34 -0700941 if (mParent != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700942 // JavaScript can only close popup window.
943 if (mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700944 mWebViewController.switchToTab(mParent);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700945 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700946 mWebViewController.closeTab(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700947 }
948 }
949
950 @Override
951 public void onProgressChanged(WebView view, int newProgress) {
John Reck30c714c2010-12-16 17:30:34 -0800952 mPageLoadProgress = newProgress;
953 mWebViewController.onProgressChanged(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700954 }
955
956 @Override
Leon Scroggins21d9b902010-03-11 09:33:11 -0500957 public void onReceivedTitle(WebView view, final String title) {
John Reck30c714c2010-12-16 17:30:34 -0800958 mCurrentState.mTitle = title;
Michael Kolb8233fac2010-10-26 16:08:53 -0700959 mWebViewController.onReceivedTitle(Tab.this, title);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700960 }
961
962 @Override
963 public void onReceivedIcon(WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -0800964 mCurrentState.mFavicon = icon;
Michael Kolb8233fac2010-10-26 16:08:53 -0700965 mWebViewController.onFavicon(Tab.this, view, icon);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700966 }
967
968 @Override
969 public void onReceivedTouchIconUrl(WebView view, String url,
970 boolean precomposed) {
971 final ContentResolver cr = mActivity.getContentResolver();
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400972 // Let precomposed icons take precedence over non-composed
973 // icons.
974 if (precomposed && mTouchIconLoader != null) {
975 mTouchIconLoader.cancel(false);
976 mTouchIconLoader = null;
977 }
978 // Have only one async task at a time.
979 if (mTouchIconLoader == null) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700980 mTouchIconLoader = new DownloadTouchIcon(Tab.this,
981 mActivity, cr, view);
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400982 mTouchIconLoader.execute(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700983 }
984 }
985
986 @Override
987 public void onShowCustomView(View view,
988 WebChromeClient.CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700989 if (mInForeground) mWebViewController.showCustomView(Tab.this, view,
990 callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700991 }
992
993 @Override
994 public void onHideCustomView() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700995 if (mInForeground) mWebViewController.hideCustomView();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700996 }
997
998 /**
999 * The origin has exceeded its database quota.
1000 * @param url the URL that exceeded the quota
1001 * @param databaseIdentifier the identifier of the database on which the
1002 * transaction that caused the quota overflow was run
1003 * @param currentQuota the current quota for the origin.
1004 * @param estimatedSize the estimated size of the database.
1005 * @param totalUsedQuota is the sum of all origins' quota.
1006 * @param quotaUpdater The callback to run when a decision to allow or
1007 * deny quota has been made. Don't forget to call this!
1008 */
1009 @Override
1010 public void onExceededDatabaseQuota(String url,
1011 String databaseIdentifier, long currentQuota, long estimatedSize,
1012 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -07001013 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001014 .onExceededDatabaseQuota(url, databaseIdentifier,
1015 currentQuota, estimatedSize, totalUsedQuota,
1016 quotaUpdater);
1017 }
1018
1019 /**
1020 * The Application Cache has exceeded its max size.
1021 * @param spaceNeeded is the amount of disk space that would be needed
1022 * in order for the last appcache operation to succeed.
1023 * @param totalUsedQuota is the sum of all origins' quota.
1024 * @param quotaUpdater A callback to inform the WebCore thread that a
1025 * new app cache size is available. This callback must always
1026 * be executed at some point to ensure that the sleeping
1027 * WebCore thread is woken up.
1028 */
1029 @Override
1030 public void onReachedMaxAppCacheSize(long spaceNeeded,
1031 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -07001032 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001033 .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota,
1034 quotaUpdater);
1035 }
1036
1037 /**
1038 * Instructs the browser to show a prompt to ask the user to set the
1039 * Geolocation permission state for the specified origin.
1040 * @param origin The origin for which Geolocation permissions are
1041 * requested.
1042 * @param callback The callback to call once the user has set the
1043 * Geolocation permission state.
1044 */
1045 @Override
1046 public void onGeolocationPermissionsShowPrompt(String origin,
1047 GeolocationPermissions.Callback callback) {
1048 if (mInForeground) {
Grace Kloba50c241e2010-04-20 11:07:50 -07001049 getGeolocationPermissionsPrompt().show(origin, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001050 }
1051 }
1052
1053 /**
1054 * Instructs the browser to hide the Geolocation permissions prompt.
1055 */
1056 @Override
1057 public void onGeolocationPermissionsHidePrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001058 if (mInForeground && mGeolocationPermissionsPrompt != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001059 mGeolocationPermissionsPrompt.hide();
1060 }
1061 }
1062
Ben Murdoch65acc352009-11-19 18:16:04 +00001063 /* Adds a JavaScript error message to the system log and if the JS
1064 * console is enabled in the about:debug options, to that console
1065 * also.
Ben Murdochc42addf2010-01-28 15:19:59 +00001066 * @param consoleMessage the message object.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001067 */
1068 @Override
Ben Murdochc42addf2010-01-28 15:19:59 +00001069 public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001070 if (mInForeground) {
1071 // call getErrorConsole(true) so it will create one if needed
1072 ErrorConsoleView errorConsole = getErrorConsole(true);
Ben Murdochc42addf2010-01-28 15:19:59 +00001073 errorConsole.addErrorMessage(consoleMessage);
Michael Kolb8233fac2010-10-26 16:08:53 -07001074 if (mWebViewController.shouldShowErrorConsole()
1075 && errorConsole.getShowState() !=
1076 ErrorConsoleView.SHOW_MAXIMIZED) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001077 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
1078 }
1079 }
Ben Murdochc42addf2010-01-28 15:19:59 +00001080
Jeff Hamilton47654f42010-09-07 09:57:51 -05001081 // Don't log console messages in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001082 if (isPrivateBrowsingEnabled()) return true;
Jeff Hamilton47654f42010-09-07 09:57:51 -05001083
Ben Murdochc42addf2010-01-28 15:19:59 +00001084 String message = "Console: " + consoleMessage.message() + " "
1085 + consoleMessage.sourceId() + ":"
1086 + consoleMessage.lineNumber();
1087
1088 switch (consoleMessage.messageLevel()) {
1089 case TIP:
1090 Log.v(CONSOLE_LOGTAG, message);
1091 break;
1092 case LOG:
1093 Log.i(CONSOLE_LOGTAG, message);
1094 break;
1095 case WARNING:
1096 Log.w(CONSOLE_LOGTAG, message);
1097 break;
1098 case ERROR:
1099 Log.e(CONSOLE_LOGTAG, message);
1100 break;
1101 case DEBUG:
1102 Log.d(CONSOLE_LOGTAG, message);
1103 break;
1104 }
1105
1106 return true;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001107 }
1108
1109 /**
1110 * Ask the browser for an icon to represent a <video> element.
1111 * This icon will be used if the Web page did not specify a poster attribute.
1112 * @return Bitmap The icon or null if no such icon is available.
1113 */
1114 @Override
1115 public Bitmap getDefaultVideoPoster() {
1116 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001117 return mWebViewController.getDefaultVideoPoster();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001118 }
1119 return null;
1120 }
1121
1122 /**
1123 * Ask the host application for a custom progress view to show while
1124 * a <video> is loading.
1125 * @return View The progress view.
1126 */
1127 @Override
1128 public View getVideoLoadingProgressView() {
1129 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001130 return mWebViewController.getVideoLoadingProgressView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001131 }
1132 return null;
1133 }
1134
1135 @Override
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01001136 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001137 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001138 mWebViewController.openFileChooser(uploadMsg, acceptType);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001139 } else {
1140 uploadMsg.onReceiveValue(null);
1141 }
1142 }
1143
1144 /**
1145 * Deliver a list of already-visited URLs
1146 */
1147 @Override
1148 public void getVisitedHistory(final ValueCallback<String[]> callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001149 mWebViewController.getVisitedHistory(callback);
1150 }
Ben Murdoch8029a772010-11-16 11:58:21 +00001151
1152 @Override
1153 public void setupAutoFill(Message message) {
1154 // Prompt the user to set up their profile.
1155 final Message msg = message;
1156 AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
Ben Murdoch1d676b62011-01-17 12:54:24 +00001157 LayoutInflater inflater = (LayoutInflater) mActivity.getSystemService(
1158 Context.LAYOUT_INFLATER_SERVICE);
1159 final View layout = inflater.inflate(R.layout.setup_autofill_dialog, null);
1160
1161 builder.setView(layout)
1162 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1163 @Override
1164 public void onClick(DialogInterface dialog, int id) {
1165 CheckBox disableAutoFill = (CheckBox) layout.findViewById(
1166 R.id.setup_autofill_dialog_disable_autofill);
1167
1168 if (disableAutoFill.isChecked()) {
1169 // Disable autofill and show a toast with how to turn it on again.
John Reck35e9dd62011-04-25 09:01:54 -07001170 mSettings.setAutofillEnabled(false);
Ben Murdoch1d676b62011-01-17 12:54:24 +00001171 Toast.makeText(mActivity,
1172 R.string.autofill_setup_dialog_negative_toast,
1173 Toast.LENGTH_LONG).show();
1174 } else {
1175 // Take user to the AutoFill profile editor. When they return,
1176 // we will send the message that we pass here which will trigger
1177 // the form to get filled out with their new profile.
1178 mWebViewController.setupAutoFill(msg);
1179 }
1180 }
1181 })
1182 .setNegativeButton(R.string.cancel, null)
1183 .show();
Ben Murdoch8029a772010-11-16 11:58:21 +00001184 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001185 };
1186
1187 // -------------------------------------------------------------------------
1188 // WebViewClient implementation for the sub window
1189 // -------------------------------------------------------------------------
1190
1191 // Subclass of WebViewClient used in subwindows to notify the main
1192 // WebViewClient of certain WebView activities.
1193 private static class SubWindowClient extends WebViewClient {
1194 // The main WebViewClient.
1195 private final WebViewClient mClient;
Michael Kolb8233fac2010-10-26 16:08:53 -07001196 private final WebViewController mController;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001197
Michael Kolb8233fac2010-10-26 16:08:53 -07001198 SubWindowClient(WebViewClient client, WebViewController controller) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001199 mClient = client;
Michael Kolb8233fac2010-10-26 16:08:53 -07001200 mController = controller;
Leon Scroggins III211ba542010-04-19 13:21:13 -04001201 }
1202 @Override
1203 public void onPageStarted(WebView view, String url, Bitmap favicon) {
1204 // Unlike the others, do not call mClient's version, which would
1205 // change the progress bar. However, we do want to remove the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001206 // find or select dialog.
Michael Kolb8233fac2010-10-26 16:08:53 -07001207 mController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001208 }
1209 @Override
1210 public void doUpdateVisitedHistory(WebView view, String url,
1211 boolean isReload) {
1212 mClient.doUpdateVisitedHistory(view, url, isReload);
1213 }
1214 @Override
1215 public boolean shouldOverrideUrlLoading(WebView view, String url) {
1216 return mClient.shouldOverrideUrlLoading(view, url);
1217 }
1218 @Override
1219 public void onReceivedSslError(WebView view, SslErrorHandler handler,
1220 SslError error) {
1221 mClient.onReceivedSslError(view, handler, error);
1222 }
1223 @Override
1224 public void onReceivedHttpAuthRequest(WebView view,
1225 HttpAuthHandler handler, String host, String realm) {
1226 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
1227 }
1228 @Override
1229 public void onFormResubmission(WebView view, Message dontResend,
1230 Message resend) {
1231 mClient.onFormResubmission(view, dontResend, resend);
1232 }
1233 @Override
1234 public void onReceivedError(WebView view, int errorCode,
1235 String description, String failingUrl) {
1236 mClient.onReceivedError(view, errorCode, description, failingUrl);
1237 }
1238 @Override
1239 public boolean shouldOverrideKeyEvent(WebView view,
1240 android.view.KeyEvent event) {
1241 return mClient.shouldOverrideKeyEvent(view, event);
1242 }
1243 @Override
1244 public void onUnhandledKeyEvent(WebView view,
1245 android.view.KeyEvent event) {
1246 mClient.onUnhandledKeyEvent(view, event);
1247 }
1248 }
1249
1250 // -------------------------------------------------------------------------
1251 // WebChromeClient implementation for the sub window
1252 // -------------------------------------------------------------------------
1253
1254 private class SubWindowChromeClient extends WebChromeClient {
1255 // The main WebChromeClient.
1256 private final WebChromeClient mClient;
1257
1258 SubWindowChromeClient(WebChromeClient client) {
1259 mClient = client;
1260 }
1261 @Override
1262 public void onProgressChanged(WebView view, int newProgress) {
1263 mClient.onProgressChanged(view, newProgress);
1264 }
1265 @Override
1266 public boolean onCreateWindow(WebView view, boolean dialog,
1267 boolean userGesture, android.os.Message resultMsg) {
1268 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
1269 }
1270 @Override
1271 public void onCloseWindow(WebView window) {
1272 if (window != mSubView) {
1273 Log.e(LOGTAG, "Can't close the window");
1274 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001275 mWebViewController.dismissSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001276 }
1277 }
1278
1279 // -------------------------------------------------------------------------
1280
Michael Kolb8233fac2010-10-26 16:08:53 -07001281 // TODO temporarily use activity here
1282 // remove later
1283
Grace Kloba22ac16e2009-10-07 18:00:23 -07001284 // Construct a new tab
Michael Kolb7bcafde2011-05-09 13:55:59 -07001285 Tab(WebViewController wvcontroller, WebView w) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001286 mWebViewController = wvcontroller;
1287 mActivity = mWebViewController.getActivity();
John Reck35e9dd62011-04-25 09:01:54 -07001288 mSettings = BrowserSettings.getInstance();
John Recke969cc52010-12-21 17:24:43 -08001289 mDataController = DataController.getInstance(mActivity);
John Reck847b5322011-04-14 17:02:18 -07001290 mCurrentState = new PageState(mActivity, w != null
1291 ? w.isPrivateBrowsingEnabled() : false);
Michael Kolb8233fac2010-10-26 16:08:53 -07001292 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001293 mInForeground = false;
1294
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001295 mDownloadListener = new DownloadListener() {
1296 public void onDownloadStart(String url, String userAgent,
1297 String contentDisposition, String mimetype,
1298 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001299 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001300 mimetype, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001301 }
1302 };
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001303 mWebBackForwardListClient = new WebBackForwardListClient() {
1304 @Override
1305 public void onNewHistoryItem(WebHistoryItem item) {
1306 if (isInVoiceSearchMode()) {
1307 item.setCustomData(mVoiceSearchData.mVoiceSearchIntent);
1308 }
1309 }
1310 @Override
1311 public void onIndexChanged(WebHistoryItem item, int index) {
1312 Object data = item.getCustomData();
1313 if (data != null && data instanceof Intent) {
1314 activateVoiceSearchMode((Intent) data);
1315 }
1316 }
1317 };
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001318
Grace Kloba22ac16e2009-10-07 18:00:23 -07001319 setWebView(w);
1320 }
1321
Michael Kolbc831b632011-05-11 09:30:34 -07001322 public void setId(long id) {
1323 mId = id;
1324 }
1325
1326 public long getId() {
1327 return mId;
1328 }
1329
Grace Kloba22ac16e2009-10-07 18:00:23 -07001330 /**
1331 * Sets the WebView for this tab, correctly removing the old WebView from
1332 * the container view.
1333 */
1334 void setWebView(WebView w) {
1335 if (mMainView == w) {
1336 return;
1337 }
Michael Kolba713ec82010-11-29 17:27:06 -08001338
Grace Kloba22ac16e2009-10-07 18:00:23 -07001339 // If the WebView is changing, the page will be reloaded, so any ongoing
1340 // Geolocation permission requests are void.
Grace Kloba50c241e2010-04-20 11:07:50 -07001341 if (mGeolocationPermissionsPrompt != null) {
1342 mGeolocationPermissionsPrompt.hide();
1343 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001344
Michael Kolba713ec82010-11-29 17:27:06 -08001345 mWebViewController.onSetWebView(this, w);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001346
1347 // set the new one
1348 mMainView = w;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001349 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001350 if (mMainView != null) {
1351 mMainView.setWebViewClient(mWebViewClient);
1352 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001353 // Attach DownloadManager so that downloads can start in an active
1354 // or a non-active window. This can happen when going to a site that
1355 // does a redirect after a period of time. The user could have
1356 // switched to another tab while waiting for the download to start.
1357 mMainView.setDownloadListener(mDownloadListener);
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001358 mMainView.setWebBackForwardListClient(mWebBackForwardListClient);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001359 }
1360 }
1361
1362 /**
1363 * Destroy the tab's main WebView and subWindow if any
1364 */
1365 void destroy() {
1366 if (mMainView != null) {
1367 dismissSubWindow();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001368 // save the WebView to call destroy() after detach it from the tab
1369 WebView webView = mMainView;
1370 setWebView(null);
1371 webView.destroy();
1372 }
1373 }
1374
1375 /**
1376 * Remove the tab from the parent
1377 */
1378 void removeFromTree() {
1379 // detach the children
Michael Kolbc831b632011-05-11 09:30:34 -07001380 if (mChildren != null) {
1381 for(Tab t : mChildren) {
1382 t.setParent(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001383 }
1384 }
1385 // remove itself from the parent list
Michael Kolbc831b632011-05-11 09:30:34 -07001386 if (mParent != null) {
1387 mParent.mChildren.remove(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001388 }
1389 }
1390
1391 /**
1392 * Create a new subwindow unless a subwindow already exists.
1393 * @return True if a new subwindow was created. False if one already exists.
1394 */
1395 boolean createSubWindow() {
1396 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001397 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001398 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001399 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001400 mSubView.setWebChromeClient(new SubWindowChromeClient(
1401 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001402 // Set a different DownloadListener for the mSubView, since it will
1403 // just need to dismiss the mSubView, rather than close the Tab
1404 mSubView.setDownloadListener(new DownloadListener() {
1405 public void onDownloadStart(String url, String userAgent,
1406 String contentDisposition, String mimetype,
1407 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001408 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001409 contentDisposition, mimetype, contentLength);
1410 if (mSubView.copyBackForwardList().getSize() == 0) {
1411 // This subwindow was opened for the sole purpose of
1412 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001413 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001414 }
1415 }
1416 });
Grace Kloba22ac16e2009-10-07 18:00:23 -07001417 mSubView.setOnCreateContextMenuListener(mActivity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001418 return true;
1419 }
1420 return false;
1421 }
1422
1423 /**
1424 * Dismiss the subWindow for the tab.
1425 */
1426 void dismissSubWindow() {
1427 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001428 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001429 mSubView.destroy();
1430 mSubView = null;
1431 mSubViewContainer = null;
1432 }
1433 }
1434
Grace Kloba22ac16e2009-10-07 18:00:23 -07001435
1436 /**
1437 * Set the parent tab of this tab.
1438 */
Michael Kolbc831b632011-05-11 09:30:34 -07001439 void setParent(Tab parent) {
1440 mParent = parent;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001441 // This tab may have been freed due to low memory. If that is the case,
Michael Kolbc831b632011-05-11 09:30:34 -07001442 // the parent tab id is already saved. If we are changing that id
Grace Kloba22ac16e2009-10-07 18:00:23 -07001443 // (most likely due to removing the parent tab) we must update the
Michael Kolbc831b632011-05-11 09:30:34 -07001444 // parent tab id in the saved Bundle.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001445 if (mSavedState != null) {
1446 if (parent == null) {
1447 mSavedState.remove(PARENTTAB);
1448 } else {
Michael Kolbc831b632011-05-11 09:30:34 -07001449 mSavedState.putLong(PARENTTAB, parent.getId());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001450 }
1451 }
1452 }
1453
1454 /**
Michael Kolbc831b632011-05-11 09:30:34 -07001455 * If this Tab was created through another Tab, then this method returns
1456 * that Tab.
1457 * @return the Tab parent or null
1458 */
1459 public Tab getParent() {
1460 return mParent;
1461 }
1462
1463 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001464 * When a Tab is created through the content of another Tab, then we
1465 * associate the Tabs.
1466 * @param child the Tab that was created from this Tab
1467 */
1468 void addChildTab(Tab child) {
Michael Kolbc831b632011-05-11 09:30:34 -07001469 if (mChildren == null) {
1470 mChildren = new Vector<Tab>();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001471 }
Michael Kolbc831b632011-05-11 09:30:34 -07001472 mChildren.add(child);
1473 child.setParent(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001474 }
1475
Michael Kolbc831b632011-05-11 09:30:34 -07001476 Vector<Tab> getChildren() {
1477 return mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001478 }
1479
1480 void resume() {
1481 if (mMainView != null) {
1482 mMainView.onResume();
1483 if (mSubView != null) {
1484 mSubView.onResume();
1485 }
1486 }
1487 }
1488
1489 void pause() {
1490 if (mMainView != null) {
1491 mMainView.onPause();
1492 if (mSubView != null) {
1493 mSubView.onPause();
1494 }
1495 }
1496 }
1497
1498 void putInForeground() {
1499 mInForeground = true;
1500 resume();
1501 mMainView.setOnCreateContextMenuListener(mActivity);
1502 if (mSubView != null) {
1503 mSubView.setOnCreateContextMenuListener(mActivity);
1504 }
1505 // Show the pending error dialog if the queue is not empty
1506 if (mQueuedErrors != null && mQueuedErrors.size() > 0) {
1507 showError(mQueuedErrors.getFirst());
1508 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001509 mWebViewController.bookmarkedStatusHasChanged(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001510 }
1511
1512 void putInBackground() {
1513 mInForeground = false;
1514 pause();
1515 mMainView.setOnCreateContextMenuListener(null);
1516 if (mSubView != null) {
1517 mSubView.setOnCreateContextMenuListener(null);
1518 }
1519 }
1520
Michael Kolb8233fac2010-10-26 16:08:53 -07001521 boolean inForeground() {
1522 return mInForeground;
1523 }
1524
Grace Kloba22ac16e2009-10-07 18:00:23 -07001525 /**
1526 * Return the top window of this tab; either the subwindow if it is not
1527 * null or the main window.
1528 * @return The top window of this tab.
1529 */
1530 WebView getTopWindow() {
1531 if (mSubView != null) {
1532 return mSubView;
1533 }
1534 return mMainView;
1535 }
1536
1537 /**
1538 * Return the main window of this tab. Note: if a tab is freed in the
1539 * background, this can return null. It is only guaranteed to be
1540 * non-null for the current tab.
1541 * @return The main WebView of this tab.
1542 */
1543 WebView getWebView() {
1544 return mMainView;
1545 }
1546
Michael Kolba713ec82010-11-29 17:27:06 -08001547 void setViewContainer(View container) {
1548 mContainer = container;
1549 }
1550
Michael Kolb8233fac2010-10-26 16:08:53 -07001551 View getViewContainer() {
1552 return mContainer;
1553 }
1554
Grace Kloba22ac16e2009-10-07 18:00:23 -07001555 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001556 * Return whether private browsing is enabled for the main window of
1557 * this tab.
1558 * @return True if private browsing is enabled.
1559 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001560 boolean isPrivateBrowsingEnabled() {
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001561 WebView webView = getWebView();
1562 if (webView == null) {
1563 return false;
1564 }
1565 return webView.isPrivateBrowsingEnabled();
1566 }
1567
1568 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001569 * Return the subwindow of this tab or null if there is no subwindow.
1570 * @return The subwindow of this tab or null.
1571 */
1572 WebView getSubWebView() {
1573 return mSubView;
1574 }
1575
Michael Kolb1514bb72010-11-22 09:11:48 -08001576 void setSubWebView(WebView subView) {
1577 mSubView = subView;
1578 }
1579
Michael Kolb8233fac2010-10-26 16:08:53 -07001580 View getSubViewContainer() {
1581 return mSubViewContainer;
1582 }
1583
Michael Kolb1514bb72010-11-22 09:11:48 -08001584 void setSubViewContainer(View subViewContainer) {
1585 mSubViewContainer = subViewContainer;
1586 }
1587
Grace Kloba22ac16e2009-10-07 18:00:23 -07001588 /**
1589 * @return The geolocation permissions prompt for this tab.
1590 */
1591 GeolocationPermissionsPrompt getGeolocationPermissionsPrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001592 if (mGeolocationPermissionsPrompt == null) {
1593 ViewStub stub = (ViewStub) mContainer
1594 .findViewById(R.id.geolocation_permissions_prompt);
1595 mGeolocationPermissionsPrompt = (GeolocationPermissionsPrompt) stub
1596 .inflate();
1597 mGeolocationPermissionsPrompt.init();
1598 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001599 return mGeolocationPermissionsPrompt;
1600 }
1601
1602 /**
1603 * @return The application id string
1604 */
1605 String getAppId() {
1606 return mAppId;
1607 }
1608
1609 /**
1610 * Set the application id string
1611 * @param id
1612 */
1613 void setAppId(String id) {
1614 mAppId = id;
1615 }
1616
Grace Kloba22ac16e2009-10-07 18:00:23 -07001617 String getUrl() {
John Reck324d4402011-01-11 16:56:42 -08001618 return UrlUtils.filteredUrl(mCurrentState.mUrl);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001619 }
1620
John Reck49a603c2011-03-03 09:33:05 -08001621 String getOriginalUrl() {
1622 if (mMainView == null) {
1623 return "";
1624 }
1625 return UrlUtils.filteredUrl(mMainView.getOriginalUrl());
1626 }
1627
Grace Kloba22ac16e2009-10-07 18:00:23 -07001628 /**
John Reck30c714c2010-12-16 17:30:34 -08001629 * Get the title of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001630 */
1631 String getTitle() {
John Reck30c714c2010-12-16 17:30:34 -08001632 if (mCurrentState.mTitle == null && mInPageLoad) {
1633 return mActivity.getString(R.string.title_bar_loading);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001634 }
John Reck30c714c2010-12-16 17:30:34 -08001635 return mCurrentState.mTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001636 }
1637
1638 /**
John Reck30c714c2010-12-16 17:30:34 -08001639 * Get the favicon of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001640 */
1641 Bitmap getFavicon() {
John Reck30c714c2010-12-16 17:30:34 -08001642 return mCurrentState.mFavicon;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001643 }
1644
John Recke969cc52010-12-21 17:24:43 -08001645 public boolean isBookmarkedSite() {
1646 return mCurrentState.mIsBookmarkedSite;
1647 }
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001648
Grace Kloba22ac16e2009-10-07 18:00:23 -07001649 /**
1650 * Return the tab's error console. Creates the console if createIfNEcessary
1651 * is true and we haven't already created the console.
1652 * @param createIfNecessary Flag to indicate if the console should be
1653 * created if it has not been already.
1654 * @return The tab's error console, or null if one has not been created and
1655 * createIfNecessary is false.
1656 */
1657 ErrorConsoleView getErrorConsole(boolean createIfNecessary) {
1658 if (createIfNecessary && mErrorConsole == null) {
1659 mErrorConsole = new ErrorConsoleView(mActivity);
1660 mErrorConsole.setWebView(mMainView);
1661 }
1662 return mErrorConsole;
1663 }
1664
John Reck30c714c2010-12-16 17:30:34 -08001665 private void setLockIconType(LockIcon icon) {
1666 mCurrentState.mLockIcon = icon;
1667 mWebViewController.onUpdatedLockIcon(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001668 }
1669
1670 /**
1671 * @return The tab's lock icon type.
1672 */
John Reck30c714c2010-12-16 17:30:34 -08001673 LockIcon getLockIconType() {
1674 return mCurrentState.mLockIcon;
1675 }
1676
1677 int getLoadProgress() {
1678 if (mInPageLoad) {
1679 return mPageLoadProgress;
1680 }
1681 return 100;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001682 }
1683
1684 /**
1685 * @return TRUE if onPageStarted is called while onPageFinished is not
1686 * called yet.
1687 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001688 boolean inPageLoad() {
1689 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001690 }
1691
1692 // force mInLoad to be false. This should only be called before closing the
1693 // tab to ensure BrowserActivity's pauseWebViewTimers() is called correctly.
Michael Kolb8233fac2010-10-26 16:08:53 -07001694 void clearInPageLoad() {
1695 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001696 }
1697
Grace Kloba22ac16e2009-10-07 18:00:23 -07001698 /**
John Reck30c714c2010-12-16 17:30:34 -08001699 * Get the cached saved state bundle.
1700 * @return cached state bundle
Grace Kloba22ac16e2009-10-07 18:00:23 -07001701 */
1702 Bundle getSavedState() {
1703 return mSavedState;
1704 }
1705
1706 /**
1707 * Set the saved state.
1708 */
1709 void setSavedState(Bundle state) {
1710 mSavedState = state;
1711 }
1712
1713 /**
1714 * @return TRUE if succeed in saving the state.
1715 */
1716 boolean saveState() {
1717 // If the WebView is null it means we ran low on memory and we already
1718 // stored the saved state in mSavedState.
1719 if (mMainView == null) {
1720 return mSavedState != null;
1721 }
1722
1723 mSavedState = new Bundle();
1724 final WebBackForwardList list = mMainView.saveState(mSavedState);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001725
1726 // Store some extra info for displaying the tab in the picker.
1727 final WebHistoryItem item = list != null ? list.getCurrentItem() : null;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001728
Michael Kolbc831b632011-05-11 09:30:34 -07001729 mSavedState.putLong(ID, mId);
John Reck30c714c2010-12-16 17:30:34 -08001730 mSavedState.putString(CURRURL, mCurrentState.mUrl);
1731 mSavedState.putString(CURRTITLE, mCurrentState.mTitle);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001732 if (mAppId != null) {
1733 mSavedState.putString(APPID, mAppId);
1734 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001735 // Remember the parent tab so the relationship can be restored.
Michael Kolbc831b632011-05-11 09:30:34 -07001736 if (mParent != null) {
1737 mSavedState.putLong(PARENTTAB, mParent.mId);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001738 }
Michael Kolbeb95db42011-03-03 10:38:40 -08001739 if (mScreenshot != null) {
1740 mSavedState.putParcelable(SCREENSHOT, mScreenshot);
1741 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001742 return true;
1743 }
1744
1745 /*
1746 * Restore the state of the tab.
1747 */
1748 boolean restoreState(Bundle b) {
1749 if (b == null) {
1750 return false;
1751 }
1752 // Restore the internal state even if the WebView fails to restore.
1753 // This will maintain the app id, original url and close-on-exit values.
1754 mSavedState = null;
Michael Kolbc831b632011-05-11 09:30:34 -07001755 mId = b.getLong(ID);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001756 mAppId = b.getString(APPID);
Michael Kolbeb95db42011-03-03 10:38:40 -08001757 mScreenshot = b.getParcelable(SCREENSHOT);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001758
1759 final WebBackForwardList list = mMainView.restoreState(b);
1760 if (list == null) {
1761 return false;
1762 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001763 return true;
1764 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04001765
Leon Scroggins1961ed22010-12-07 15:22:21 -05001766 public void updateBookmarkedStatus() {
John Recke969cc52010-12-21 17:24:43 -08001767 mDataController.queryBookmarkStatus(getUrl(), mIsBookmarkCallback);
Leon Scroggins1961ed22010-12-07 15:22:21 -05001768 }
1769
John Recke969cc52010-12-21 17:24:43 -08001770 private DataController.OnQueryUrlIsBookmark mIsBookmarkCallback
1771 = new DataController.OnQueryUrlIsBookmark() {
1772 @Override
1773 public void onQueryUrlIsBookmark(String url, boolean isBookmark) {
1774 if (mCurrentState.mUrl.equals(url)) {
1775 mCurrentState.mIsBookmarkedSite = isBookmark;
1776 mWebViewController.bookmarkedStatusHasChanged(Tab.this);
1777 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001778 }
John Recke969cc52010-12-21 17:24:43 -08001779 };
Michael Kolb1acef692011-03-08 14:12:06 -08001780
Michael Kolbeb95db42011-03-03 10:38:40 -08001781 public void setScreenshot(Bitmap screenshot) {
1782 mScreenshot = screenshot;
1783 }
1784
1785 public Bitmap getScreenshot() {
1786 return mScreenshot;
1787 }
1788
Grace Kloba22ac16e2009-10-07 18:00:23 -07001789}