blob: 83db214e1b61310e7a7daa675d8160fc06bf6492 [file] [log] [blame]
Grace Kloba22ac16e2009-10-07 18:00:23 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.browser;
18
John Reck438bf462011-01-12 18:11:46 -080019import com.android.browser.homepages.HomeProvider;
Jeff Hamilton8ce956c2010-08-17 11:13:53 -050020import com.android.common.speech.LoggingEvents;
21
Michael Kolb8233fac2010-10-26 16:08:53 -070022import android.app.Activity;
Grace Kloba22ac16e2009-10-07 18:00:23 -070023import android.app.AlertDialog;
Leon Scroggins58d56c62010-01-28 15:12:40 -050024import android.app.SearchManager;
Grace Kloba22ac16e2009-10-07 18:00:23 -070025import android.content.ContentResolver;
John Reck30c714c2010-12-16 17:30:34 -080026import android.content.Context;
Grace Kloba22ac16e2009-10-07 18:00:23 -070027import android.content.DialogInterface;
Michael Kolbfe251992010-07-08 15:41:55 -070028import android.content.DialogInterface.OnCancelListener;
Jeff Hamilton8ce956c2010-08-17 11:13:53 -050029import android.content.Intent;
Grace Kloba22ac16e2009-10-07 18:00:23 -070030import android.graphics.Bitmap;
John Reck30c714c2010-12-16 17:30:34 -080031import android.graphics.BitmapFactory;
Grace Kloba22ac16e2009-10-07 18:00:23 -070032import android.net.Uri;
33import android.net.http.SslError;
Grace Kloba22ac16e2009-10-07 18:00:23 -070034import android.os.Bundle;
35import android.os.Message;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +000036import android.os.SystemClock;
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -050037import android.speech.RecognizerResultsIntent;
Grace Kloba22ac16e2009-10-07 18:00:23 -070038import android.util.Log;
39import android.view.KeyEvent;
40import android.view.LayoutInflater;
41import android.view.View;
Grace Kloba50c241e2010-04-20 11:07:50 -070042import android.view.ViewStub;
Ben Murdochc42addf2010-01-28 15:19:59 +000043import android.webkit.ConsoleMessage;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -050044import android.webkit.DownloadListener;
Grace Kloba22ac16e2009-10-07 18:00:23 -070045import android.webkit.GeolocationPermissions;
46import android.webkit.HttpAuthHandler;
47import android.webkit.SslErrorHandler;
48import android.webkit.URLUtil;
49import android.webkit.ValueCallback;
50import android.webkit.WebBackForwardList;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -050051import android.webkit.WebBackForwardListClient;
Grace Kloba22ac16e2009-10-07 18:00:23 -070052import android.webkit.WebChromeClient;
53import android.webkit.WebHistoryItem;
John Reck438bf462011-01-12 18:11:46 -080054import android.webkit.WebResourceResponse;
Grace Kloba22ac16e2009-10-07 18:00:23 -070055import android.webkit.WebStorage;
56import android.webkit.WebView;
57import android.webkit.WebViewClient;
Ben Murdoch1d676b62011-01-17 12:54:24 +000058import android.widget.CheckBox;
Grace Kloba22ac16e2009-10-07 18:00:23 -070059import android.widget.LinearLayout;
60import android.widget.TextView;
Ben Murdoch8029a772010-11-16 11:58:21 +000061import android.widget.Toast;
Grace Kloba22ac16e2009-10-07 18:00:23 -070062
Michael Kolbfe251992010-07-08 15:41:55 -070063import java.util.ArrayList;
64import java.util.HashMap;
65import java.util.Iterator;
66import java.util.LinkedList;
67import java.util.Map;
68import java.util.Vector;
69
Grace Kloba22ac16e2009-10-07 18:00:23 -070070/**
71 * Class for maintaining Tabs with a main WebView and a subwindow.
72 */
73class Tab {
Michael Kolb8233fac2010-10-26 16:08:53 -070074
Grace Kloba22ac16e2009-10-07 18:00:23 -070075 // Log Tag
76 private static final String LOGTAG = "Tab";
Ben Murdochc42addf2010-01-28 15:19:59 +000077 // Special case the logtag for messages for the Console to make it easier to
78 // filter them and match the logtag used for these messages in older versions
79 // of the browser.
80 private static final String CONSOLE_LOGTAG = "browser";
81
John Reck30c714c2010-12-16 17:30:34 -080082 public enum LockIcon {
83 LOCK_ICON_UNSECURE,
84 LOCK_ICON_SECURE,
85 LOCK_ICON_MIXED,
86 }
Michael Kolb8233fac2010-10-26 16:08:53 -070087
88 Activity mActivity;
89 private WebViewController mWebViewController;
90
Grace Kloba22ac16e2009-10-07 18:00:23 -070091 // The Geolocation permissions prompt
92 private GeolocationPermissionsPrompt mGeolocationPermissionsPrompt;
93 // Main WebView wrapper
Michael Kolba713ec82010-11-29 17:27:06 -080094 private View mContainer;
Grace Kloba22ac16e2009-10-07 18:00:23 -070095 // Main WebView
96 private WebView mMainView;
97 // Subwindow container
98 private View mSubViewContainer;
99 // Subwindow WebView
100 private WebView mSubView;
101 // Saved bundle for when we are running low on memory. It contains the
102 // information needed to restore the WebView if the user goes back to the
103 // tab.
104 private Bundle mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700105 // Parent Tab. This is the Tab that created this Tab, or null if the Tab was
106 // created by the UI
107 private Tab mParentTab;
108 // Tab that constructed by this Tab. This is used when this Tab is
109 // destroyed, it clears all mParentTab values in the children.
110 private Vector<Tab> mChildTabs;
111 // If true, the tab will be removed when back out of the first page.
112 private boolean mCloseOnExit;
113 // If true, the tab is in the foreground of the current activity.
114 private boolean mInForeground;
Michael Kolb8233fac2010-10-26 16:08:53 -0700115 // If true, the tab is in page loading state (after onPageStarted,
116 // before onPageFinsihed)
117 private boolean mInPageLoad;
John Reck30c714c2010-12-16 17:30:34 -0800118 // The last reported progress of the current page
119 private int mPageLoadProgress;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000120 // The time the load started, used to find load page time
121 private long mLoadStartTime;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700122 // Application identifier used to find tabs that another application wants
123 // to reuse.
124 private String mAppId;
125 // Keep the original url around to avoid killing the old WebView if the url
126 // has not changed.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700127 // Error console for the tab
128 private ErrorConsoleView mErrorConsole;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -0500129 // The listener that gets invoked when a download is started from the
130 // mMainView
131 private final DownloadListener mDownloadListener;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500132 // Listener used to know when we move forward or back in the history list.
133 private final WebBackForwardListClient mWebBackForwardListClient;
John Recke969cc52010-12-21 17:24:43 -0800134 private DataController mDataController;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700135
136 // AsyncTask for downloading touch icons
137 DownloadTouchIcon mTouchIconLoader;
138
John Reck30c714c2010-12-16 17:30:34 -0800139 // All the state needed for a page
140 private static class PageState {
141 String mUrl;
142 String mTitle;
143 LockIcon mLockIcon;
144 Bitmap mFavicon;
John Recke969cc52010-12-21 17:24:43 -0800145 Boolean mIsBookmarkedSite = false;
John Reck30c714c2010-12-16 17:30:34 -0800146
147 PageState(Context c, boolean incognito) {
148 if (incognito) {
149 mUrl = "browser:incognito";
150 mTitle = c.getString(R.string.new_incognito_tab);
151 mFavicon = BitmapFactory.decodeResource(
152 c.getResources(), R.drawable.fav_incognito);
153 } else {
154 mUrl = "";
155 mTitle = c.getString(R.string.new_tab);
156 mFavicon = BitmapFactory.decodeResource(
157 c.getResources(), R.drawable.app_web_browser_sm);
158 }
159 mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
160 }
161
162 PageState(Context c, boolean incognito, String url, Bitmap favicon) {
163 mUrl = url;
164 mTitle = null;
165 if (URLUtil.isHttpsUrl(url)) {
166 mLockIcon = LockIcon.LOCK_ICON_SECURE;
167 } else {
168 mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
169 }
170 if (favicon != null) {
171 mFavicon = favicon;
172 } else {
173 if (incognito) {
174 mFavicon = BitmapFactory.decodeResource(
175 c.getResources(), R.drawable.fav_incognito);
176 } else {
177 mFavicon = BitmapFactory.decodeResource(
178 c.getResources(), R.drawable.app_web_browser_sm);
179 }
180 }
181 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700182 }
183
John Reck30c714c2010-12-16 17:30:34 -0800184 // The current/loading page's state
185 private PageState mCurrentState;
186
Grace Kloba22ac16e2009-10-07 18:00:23 -0700187 // Used for saving and restoring each Tab
John Reck30c714c2010-12-16 17:30:34 -0800188 // TODO: Figure out who uses what and where
189 // Some of these aren't use in this class, and some are only used in
190 // restoring state but not saving it - FIX THIS
Grace Kloba22ac16e2009-10-07 18:00:23 -0700191 static final String WEBVIEW = "webview";
192 static final String NUMTABS = "numTabs";
193 static final String CURRTAB = "currentTab";
194 static final String CURRURL = "currentUrl";
195 static final String CURRTITLE = "currentTitle";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700196 static final String CLOSEONEXIT = "closeonexit";
197 static final String PARENTTAB = "parentTab";
198 static final String APPID = "appid";
199 static final String ORIGINALURL = "originalUrl";
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700200 static final String INCOGNITO = "privateBrowsingEnabled";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700201
202 // -------------------------------------------------------------------------
203
Leon Scroggins58d56c62010-01-28 15:12:40 -0500204 /**
205 * Private information regarding the latest voice search. If the Tab is not
206 * in voice search mode, this will be null.
207 */
208 private VoiceSearchData mVoiceSearchData;
209 /**
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400210 * Remove voice search mode from this tab.
211 */
212 public void revertVoiceSearchMode() {
213 if (mVoiceSearchData != null) {
214 mVoiceSearchData = null;
215 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700216 mWebViewController.revertVoiceSearchMode(this);
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400217 }
218 }
219 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700220
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400221 /**
Leon Scroggins58d56c62010-01-28 15:12:40 -0500222 * Return whether the tab is in voice search mode.
223 */
224 public boolean isInVoiceSearchMode() {
225 return mVoiceSearchData != null;
226 }
227 /**
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400228 * Return true if the Tab is in voice search mode and the voice search
229 * Intent came with a String identifying that Google provided the Intent.
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500230 */
231 public boolean voiceSearchSourceIsGoogle() {
232 return mVoiceSearchData != null && mVoiceSearchData.mSourceIsGoogle;
233 }
234 /**
Leon Scroggins58d56c62010-01-28 15:12:40 -0500235 * Get the title to display for the current voice search page. If the Tab
236 * is not in voice search mode, return null.
237 */
238 public String getVoiceDisplayTitle() {
239 if (mVoiceSearchData == null) return null;
240 return mVoiceSearchData.mLastVoiceSearchTitle;
241 }
242 /**
243 * Get the latest array of voice search results, to be passed to the
244 * BrowserProvider. If the Tab is not in voice search mode, return null.
245 */
246 public ArrayList<String> getVoiceSearchResults() {
247 if (mVoiceSearchData == null) return null;
248 return mVoiceSearchData.mVoiceSearchResults;
249 }
250 /**
251 * Activate voice search mode.
252 * @param intent Intent which has the results to use, or an index into the
253 * results when reusing the old results.
254 */
255 /* package */ void activateVoiceSearchMode(Intent intent) {
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500256 int index = 0;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500257 ArrayList<String> results = intent.getStringArrayListExtra(
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -0500258 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_STRINGS);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500259 if (results != null) {
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500260 ArrayList<String> urls = intent.getStringArrayListExtra(
261 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_URLS);
262 ArrayList<String> htmls = intent.getStringArrayListExtra(
263 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_HTML);
264 ArrayList<String> baseUrls = intent.getStringArrayListExtra(
265 RecognizerResultsIntent
266 .EXTRA_VOICE_SEARCH_RESULT_HTML_BASE_URLS);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500267 // This tab is now entering voice search mode for the first time, or
268 // a new voice search was done.
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500269 int size = results.size();
270 if (urls == null || size != urls.size()) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500271 throw new AssertionError("improper extras passed in Intent");
272 }
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500273 if (htmls == null || htmls.size() != size || baseUrls == null ||
274 (baseUrls.size() != size && baseUrls.size() != 1)) {
275 // If either of these arrays are empty/incorrectly sized, ignore
276 // them.
277 htmls = null;
278 baseUrls = null;
279 }
280 mVoiceSearchData = new VoiceSearchData(results, urls, htmls,
281 baseUrls);
Leon Scroggins9df94972010-03-08 18:20:35 -0500282 mVoiceSearchData.mHeaders = intent.getParcelableArrayListExtra(
283 RecognizerResultsIntent
284 .EXTRA_VOICE_SEARCH_RESULT_HTTP_HEADERS);
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500285 mVoiceSearchData.mSourceIsGoogle = intent.getBooleanExtra(
286 VoiceSearchData.SOURCE_IS_GOOGLE, false);
Leon Scroggins2ee4a5a2010-03-15 16:56:57 -0400287 mVoiceSearchData.mVoiceSearchIntent = new Intent(intent);
Leon Scrogginse10dde52010-03-08 19:53:03 -0500288 }
289 String extraData = intent.getStringExtra(
290 SearchManager.EXTRA_DATA_KEY);
291 if (extraData != null) {
292 index = Integer.parseInt(extraData);
293 if (index >= mVoiceSearchData.mVoiceSearchResults.size()) {
294 throw new AssertionError("index must be less than "
295 + "size of mVoiceSearchResults");
296 }
297 if (mVoiceSearchData.mSourceIsGoogle) {
298 Intent logIntent = new Intent(
299 LoggingEvents.ACTION_LOG_EVENT);
300 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
301 LoggingEvents.VoiceSearch.N_BEST_CHOOSE);
302 logIntent.putExtra(
303 LoggingEvents.VoiceSearch.EXTRA_N_BEST_CHOOSE_INDEX,
304 index);
305 mActivity.sendBroadcast(logIntent);
306 }
307 if (mVoiceSearchData.mVoiceSearchIntent != null) {
Leon Scroggins2ee4a5a2010-03-15 16:56:57 -0400308 // Copy the Intent, so that each history item will have its own
309 // Intent, with different (or none) extra data.
310 Intent latest = new Intent(mVoiceSearchData.mVoiceSearchIntent);
311 latest.putExtra(SearchManager.EXTRA_DATA_KEY, extraData);
312 mVoiceSearchData.mVoiceSearchIntent = latest;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500313 }
314 }
315 mVoiceSearchData.mLastVoiceSearchTitle
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500316 = mVoiceSearchData.mVoiceSearchResults.get(index);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500317 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700318 mWebViewController.activateVoiceSearchMode(mVoiceSearchData.mLastVoiceSearchTitle);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500319 }
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500320 if (mVoiceSearchData.mVoiceSearchHtmls != null) {
321 // When index was found it was already ensured that it was valid
322 String uriString = mVoiceSearchData.mVoiceSearchHtmls.get(index);
323 if (uriString != null) {
324 Uri dataUri = Uri.parse(uriString);
325 if (RecognizerResultsIntent.URI_SCHEME_INLINE.equals(
326 dataUri.getScheme())) {
327 // If there is only one base URL, use it. If there are
328 // more, there will be one for each index, so use the base
329 // URL corresponding to the index.
330 String baseUrl = mVoiceSearchData.mVoiceSearchBaseUrls.get(
331 mVoiceSearchData.mVoiceSearchBaseUrls.size() > 1 ?
332 index : 0);
333 mVoiceSearchData.mLastVoiceSearchUrl = baseUrl;
334 mMainView.loadDataWithBaseURL(baseUrl,
335 uriString.substring(RecognizerResultsIntent
336 .URI_SCHEME_INLINE.length() + 1), "text/html",
337 "utf-8", baseUrl);
338 return;
339 }
340 }
341 }
Leon Scroggins58d56c62010-01-28 15:12:40 -0500342 mVoiceSearchData.mLastVoiceSearchUrl
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500343 = mVoiceSearchData.mVoiceSearchUrls.get(index);
344 if (null == mVoiceSearchData.mLastVoiceSearchUrl) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700345 mVoiceSearchData.mLastVoiceSearchUrl = UrlUtils.smartUrlFilter(
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500346 mVoiceSearchData.mLastVoiceSearchTitle);
347 }
Leon Scroggins9df94972010-03-08 18:20:35 -0500348 Map<String, String> headers = null;
349 if (mVoiceSearchData.mHeaders != null) {
350 int bundleIndex = mVoiceSearchData.mHeaders.size() == 1 ? 0
351 : index;
352 Bundle bundle = mVoiceSearchData.mHeaders.get(bundleIndex);
353 if (bundle != null && !bundle.isEmpty()) {
354 Iterator<String> iter = bundle.keySet().iterator();
355 headers = new HashMap<String, String>();
356 while (iter.hasNext()) {
357 String key = iter.next();
358 headers.put(key, bundle.getString(key));
359 }
360 }
361 }
362 mMainView.loadUrl(mVoiceSearchData.mLastVoiceSearchUrl, headers);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500363 }
364 /* package */ static class VoiceSearchData {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500365 public VoiceSearchData(ArrayList<String> results,
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500366 ArrayList<String> urls, ArrayList<String> htmls,
367 ArrayList<String> baseUrls) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500368 mVoiceSearchResults = results;
369 mVoiceSearchUrls = urls;
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500370 mVoiceSearchHtmls = htmls;
371 mVoiceSearchBaseUrls = baseUrls;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500372 }
373 /*
374 * ArrayList of suggestions to be displayed when opening the
375 * SearchManager
376 */
377 public ArrayList<String> mVoiceSearchResults;
378 /*
379 * ArrayList of urls, associated with the suggestions in
380 * mVoiceSearchResults.
381 */
382 public ArrayList<String> mVoiceSearchUrls;
383 /*
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500384 * ArrayList holding content to load for each item in
385 * mVoiceSearchResults.
386 */
387 public ArrayList<String> mVoiceSearchHtmls;
388 /*
389 * ArrayList holding base urls for the items in mVoiceSearchResults.
390 * If non null, this will either have the same size as
391 * mVoiceSearchResults or have a size of 1, in which case all will use
392 * the same base url
393 */
394 public ArrayList<String> mVoiceSearchBaseUrls;
395 /*
Leon Scroggins58d56c62010-01-28 15:12:40 -0500396 * The last url provided by voice search. Used for comparison to see if
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500397 * we are going to a page by some method besides voice search.
Leon Scroggins58d56c62010-01-28 15:12:40 -0500398 */
399 public String mLastVoiceSearchUrl;
400 /**
401 * The last title used for voice search. Needed to update the title bar
402 * when switching tabs.
403 */
404 public String mLastVoiceSearchTitle;
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500405 /**
406 * Whether the Intent which turned on voice search mode contained the
407 * String signifying that Google was the source.
408 */
409 public boolean mSourceIsGoogle;
410 /**
Leon Scroggins9df94972010-03-08 18:20:35 -0500411 * List of headers to be passed into the WebView containing location
412 * information
413 */
414 public ArrayList<Bundle> mHeaders;
415 /**
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500416 * The Intent used to invoke voice search. Placed on the
417 * WebHistoryItem so that when coming back to a previous voice search
418 * page we can again activate voice search.
419 */
Leon Scrogginse10dde52010-03-08 19:53:03 -0500420 public Intent mVoiceSearchIntent;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500421 /**
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500422 * String used to identify Google as the source of voice search.
423 */
424 public static String SOURCE_IS_GOOGLE
425 = "android.speech.extras.SOURCE_IS_GOOGLE";
Leon Scroggins58d56c62010-01-28 15:12:40 -0500426 }
427
Grace Kloba22ac16e2009-10-07 18:00:23 -0700428 // Container class for the next error dialog that needs to be displayed
429 private class ErrorDialog {
430 public final int mTitle;
431 public final String mDescription;
432 public final int mError;
433 ErrorDialog(int title, String desc, int error) {
434 mTitle = title;
435 mDescription = desc;
436 mError = error;
437 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700438 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700439
440 private void processNextError() {
441 if (mQueuedErrors == null) {
442 return;
443 }
444 // The first one is currently displayed so just remove it.
445 mQueuedErrors.removeFirst();
446 if (mQueuedErrors.size() == 0) {
447 mQueuedErrors = null;
448 return;
449 }
450 showError(mQueuedErrors.getFirst());
451 }
452
453 private DialogInterface.OnDismissListener mDialogListener =
454 new DialogInterface.OnDismissListener() {
455 public void onDismiss(DialogInterface d) {
456 processNextError();
457 }
458 };
459 private LinkedList<ErrorDialog> mQueuedErrors;
460
461 private void queueError(int err, String desc) {
462 if (mQueuedErrors == null) {
463 mQueuedErrors = new LinkedList<ErrorDialog>();
464 }
465 for (ErrorDialog d : mQueuedErrors) {
466 if (d.mError == err) {
467 // Already saw a similar error, ignore the new one.
468 return;
469 }
470 }
471 ErrorDialog errDialog = new ErrorDialog(
472 err == WebViewClient.ERROR_FILE_NOT_FOUND ?
473 R.string.browserFrameFileErrorLabel :
474 R.string.browserFrameNetworkErrorLabel,
475 desc, err);
476 mQueuedErrors.addLast(errDialog);
477
478 // Show the dialog now if the queue was empty and it is in foreground
479 if (mQueuedErrors.size() == 1 && mInForeground) {
480 showError(errDialog);
481 }
482 }
483
484 private void showError(ErrorDialog errDialog) {
485 if (mInForeground) {
486 AlertDialog d = new AlertDialog.Builder(mActivity)
487 .setTitle(errDialog.mTitle)
488 .setMessage(errDialog.mDescription)
489 .setPositiveButton(R.string.ok, null)
490 .create();
491 d.setOnDismissListener(mDialogListener);
492 d.show();
493 }
494 }
495
496 // -------------------------------------------------------------------------
497 // WebViewClient implementation for the main WebView
498 // -------------------------------------------------------------------------
499
500 private final WebViewClient mWebViewClient = new WebViewClient() {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500501 private Message mDontResend;
502 private Message mResend;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700503 @Override
504 public void onPageStarted(WebView view, String url, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700505 mInPageLoad = true;
John Reck30c714c2010-12-16 17:30:34 -0800506 mPageLoadProgress = 0;
507 mCurrentState = new PageState(mActivity,
508 view.isPrivateBrowsingEnabled(), url, favicon);
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000509 mLoadStartTime = SystemClock.uptimeMillis();
Leon Scroggins58d56c62010-01-28 15:12:40 -0500510 if (mVoiceSearchData != null
511 && !url.equals(mVoiceSearchData.mLastVoiceSearchUrl)) {
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500512 if (mVoiceSearchData.mSourceIsGoogle) {
513 Intent i = new Intent(LoggingEvents.ACTION_LOG_EVENT);
514 i.putExtra(LoggingEvents.EXTRA_FLUSH, true);
515 mActivity.sendBroadcast(i);
516 }
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400517 revertVoiceSearchMode();
Leon Scroggins58d56c62010-01-28 15:12:40 -0500518 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700519
Grace Kloba22ac16e2009-10-07 18:00:23 -0700520
521 // If we start a touch icon load and then load a new page, we don't
522 // want to cancel the current touch icon loader. But, we do want to
523 // create a new one when the touch icon url is known.
524 if (mTouchIconLoader != null) {
525 mTouchIconLoader.mTab = null;
526 mTouchIconLoader = null;
527 }
528
529 // reset the error console
530 if (mErrorConsole != null) {
531 mErrorConsole.clearErrorMessages();
Michael Kolb8233fac2010-10-26 16:08:53 -0700532 if (mWebViewController.shouldShowErrorConsole()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700533 mErrorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
534 }
535 }
536
Grace Kloba22ac16e2009-10-07 18:00:23 -0700537 // finally update the UI in the activity if it is in the foreground
John Reck324d4402011-01-11 16:56:42 -0800538 mWebViewController.onPageStarted(Tab.this, view, favicon);
Leon Scroggins4cd97792010-12-03 15:31:56 -0500539
John Recke969cc52010-12-21 17:24:43 -0800540 updateBookmarkedStatus();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700541 }
542
543 @Override
544 public void onPageFinished(WebView view, String url) {
John Reck5b691842010-11-29 11:21:13 -0800545 if (!isPrivateBrowsingEnabled()) {
546 LogTag.logPageFinishedLoading(
547 url, SystemClock.uptimeMillis() - mLoadStartTime);
548 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700549 mInPageLoad = false;
John Reck30c714c2010-12-16 17:30:34 -0800550 // Sync state (in case of stop/timeout)
551 mCurrentState.mUrl = view.getUrl();
John Reck6c702ee2011-01-07 09:41:53 -0800552 if (mCurrentState.mUrl == null) {
553 mCurrentState.mUrl = url != null ? url : "";
554 }
John Reck30c714c2010-12-16 17:30:34 -0800555 mCurrentState.mTitle = view.getTitle();
556 mCurrentState.mFavicon = view.getFavicon();
557 if (!URLUtil.isHttpsUrl(mCurrentState.mUrl)) {
558 // In case we stop when loading an HTTPS page from an HTTP page
559 // but before a provisional load occurred
560 mCurrentState.mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
561 }
John Reck324d4402011-01-11 16:56:42 -0800562 mWebViewController.onPageFinished(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700563 }
564
565 // return true if want to hijack the url to let another app to handle it
566 @Override
567 public boolean shouldOverrideUrlLoading(WebView view, String url) {
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400568 if (voiceSearchSourceIsGoogle()) {
569 // This method is called when the user clicks on a link.
570 // VoiceSearchMode is turned off when the user leaves the
571 // Google results page, so at this point the user must be on
572 // that page. If the user clicked a link on that page, assume
573 // that the voice search was effective, and broadcast an Intent
574 // so a receiver can take note of that fact.
575 Intent logIntent = new Intent(LoggingEvents.ACTION_LOG_EVENT);
576 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
577 LoggingEvents.VoiceSearch.RESULT_CLICKED);
578 mActivity.sendBroadcast(logIntent);
579 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700580 if (mInForeground) {
Michael Kolb18eb3772010-12-10 14:29:51 -0800581 return mWebViewController.shouldOverrideUrlLoading(Tab.this,
582 view, url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700583 } else {
584 return false;
585 }
586 }
587
588 /**
589 * Updates the lock icon. This method is called when we discover another
590 * resource to be loaded for this page (for example, javascript). While
591 * we update the icon type, we do not update the lock icon itself until
592 * we are done loading, it is slightly more secure this way.
593 */
594 @Override
595 public void onLoadResource(WebView view, String url) {
596 if (url != null && url.length() > 0) {
597 // It is only if the page claims to be secure that we may have
598 // to update the lock:
John Reck30c714c2010-12-16 17:30:34 -0800599 if (mCurrentState.mLockIcon == LockIcon.LOCK_ICON_SECURE) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700600 // If NOT a 'safe' url, change the lock to mixed content!
601 if (!(URLUtil.isHttpsUrl(url) || URLUtil.isDataUrl(url)
602 || URLUtil.isAboutUrl(url))) {
John Reck30c714c2010-12-16 17:30:34 -0800603 mCurrentState.mLockIcon = LockIcon.LOCK_ICON_MIXED;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700604 }
605 }
606 }
607 }
608
609 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700610 * Show a dialog informing the user of the network error reported by
611 * WebCore if it is in the foreground.
612 */
613 @Override
614 public void onReceivedError(WebView view, int errorCode,
615 String description, String failingUrl) {
616 if (errorCode != WebViewClient.ERROR_HOST_LOOKUP &&
617 errorCode != WebViewClient.ERROR_CONNECT &&
618 errorCode != WebViewClient.ERROR_BAD_URL &&
619 errorCode != WebViewClient.ERROR_UNSUPPORTED_SCHEME &&
620 errorCode != WebViewClient.ERROR_FILE) {
621 queueError(errorCode, description);
622 }
Jeff Hamilton47654f42010-09-07 09:57:51 -0500623
624 // Don't log URLs when in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -0700625 if (!isPrivateBrowsingEnabled()) {
Jeff Hamilton47654f42010-09-07 09:57:51 -0500626 Log.e(LOGTAG, "onReceivedError " + errorCode + " " + failingUrl
627 + " " + description);
628 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700629 }
630
631 /**
632 * Check with the user if it is ok to resend POST data as the page they
633 * are trying to navigate to is the result of a POST.
634 */
635 @Override
636 public void onFormResubmission(WebView view, final Message dontResend,
637 final Message resend) {
638 if (!mInForeground) {
639 dontResend.sendToTarget();
640 return;
641 }
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500642 if (mDontResend != null) {
643 Log.w(LOGTAG, "onFormResubmission should not be called again "
644 + "while dialog is still up");
645 dontResend.sendToTarget();
646 return;
647 }
648 mDontResend = dontResend;
649 mResend = resend;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700650 new AlertDialog.Builder(mActivity).setTitle(
651 R.string.browserFrameFormResubmitLabel).setMessage(
652 R.string.browserFrameFormResubmitMessage)
653 .setPositiveButton(R.string.ok,
654 new DialogInterface.OnClickListener() {
655 public void onClick(DialogInterface dialog,
656 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500657 if (mResend != null) {
658 mResend.sendToTarget();
659 mResend = null;
660 mDontResend = null;
661 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700662 }
663 }).setNegativeButton(R.string.cancel,
664 new DialogInterface.OnClickListener() {
665 public void onClick(DialogInterface dialog,
666 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500667 if (mDontResend != null) {
668 mDontResend.sendToTarget();
669 mResend = null;
670 mDontResend = null;
671 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700672 }
673 }).setOnCancelListener(new OnCancelListener() {
674 public void onCancel(DialogInterface dialog) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500675 if (mDontResend != null) {
676 mDontResend.sendToTarget();
677 mResend = null;
678 mDontResend = null;
679 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700680 }
681 }).show();
682 }
683
684 /**
685 * Insert the url into the visited history database.
686 * @param url The url to be inserted.
687 * @param isReload True if this url is being reloaded.
688 * FIXME: Not sure what to do when reloading the page.
689 */
690 @Override
691 public void doUpdateVisitedHistory(WebView view, String url,
692 boolean isReload) {
John Reck324d4402011-01-11 16:56:42 -0800693 mWebViewController.doUpdateVisitedHistory(Tab.this, isReload);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700694 }
695
696 /**
697 * Displays SSL error(s) dialog to the user.
698 */
699 @Override
700 public void onReceivedSslError(final WebView view,
701 final SslErrorHandler handler, final SslError error) {
702 if (!mInForeground) {
703 handler.cancel();
John Reck30c714c2010-12-16 17:30:34 -0800704 setLockIconType(LockIcon.LOCK_ICON_UNSECURE);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700705 return;
706 }
707 if (BrowserSettings.getInstance().showSecurityWarnings()) {
708 final LayoutInflater factory =
709 LayoutInflater.from(mActivity);
710 final View warningsView =
711 factory.inflate(R.layout.ssl_warnings, null);
712 final LinearLayout placeholder =
713 (LinearLayout)warningsView.findViewById(R.id.placeholder);
714
715 if (error.hasError(SslError.SSL_UNTRUSTED)) {
716 LinearLayout ll = (LinearLayout)factory
717 .inflate(R.layout.ssl_warning, null);
718 ((TextView)ll.findViewById(R.id.warning))
719 .setText(R.string.ssl_untrusted);
720 placeholder.addView(ll);
721 }
722
723 if (error.hasError(SslError.SSL_IDMISMATCH)) {
724 LinearLayout ll = (LinearLayout)factory
725 .inflate(R.layout.ssl_warning, null);
726 ((TextView)ll.findViewById(R.id.warning))
727 .setText(R.string.ssl_mismatch);
728 placeholder.addView(ll);
729 }
730
731 if (error.hasError(SslError.SSL_EXPIRED)) {
732 LinearLayout ll = (LinearLayout)factory
733 .inflate(R.layout.ssl_warning, null);
734 ((TextView)ll.findViewById(R.id.warning))
735 .setText(R.string.ssl_expired);
736 placeholder.addView(ll);
737 }
738
739 if (error.hasError(SslError.SSL_NOTYETVALID)) {
740 LinearLayout ll = (LinearLayout)factory
741 .inflate(R.layout.ssl_warning, null);
742 ((TextView)ll.findViewById(R.id.warning))
743 .setText(R.string.ssl_not_yet_valid);
744 placeholder.addView(ll);
745 }
746
747 new AlertDialog.Builder(mActivity).setTitle(
748 R.string.security_warning).setIcon(
749 android.R.drawable.ic_dialog_alert).setView(
750 warningsView).setPositiveButton(R.string.ssl_continue,
751 new DialogInterface.OnClickListener() {
752 public void onClick(DialogInterface dialog,
753 int whichButton) {
754 handler.proceed();
755 }
756 }).setNeutralButton(R.string.view_certificate,
757 new DialogInterface.OnClickListener() {
758 public void onClick(DialogInterface dialog,
759 int whichButton) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700760 mWebViewController.showSslCertificateOnError(view,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700761 handler, error);
762 }
Ben Murdocha49b8292010-11-16 11:56:04 +0000763 }).setNegativeButton(R.string.ssl_go_back,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700764 new DialogInterface.OnClickListener() {
765 public void onClick(DialogInterface dialog,
766 int whichButton) {
John Reck30c714c2010-12-16 17:30:34 -0800767 dialog.cancel();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700768 }
769 }).setOnCancelListener(
770 new DialogInterface.OnCancelListener() {
771 public void onCancel(DialogInterface dialog) {
772 handler.cancel();
John Reck30c714c2010-12-16 17:30:34 -0800773 setLockIconType(LockIcon.LOCK_ICON_UNSECURE);
774 mWebViewController.onUserCanceledSsl(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700775 }
776 }).show();
777 } else {
778 handler.proceed();
779 }
780 }
781
782 /**
783 * Handles an HTTP authentication request.
784 *
785 * @param handler The authentication handler
786 * @param host The host
787 * @param realm The realm
788 */
789 @Override
790 public void onReceivedHttpAuthRequest(WebView view,
791 final HttpAuthHandler handler, final String host,
792 final String realm) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700793 mWebViewController.onReceivedHttpAuthRequest(Tab.this, view, handler, host, realm);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700794 }
795
796 @Override
John Reck438bf462011-01-12 18:11:46 -0800797 public WebResourceResponse shouldInterceptRequest(WebView view,
798 String url) {
799 WebResourceResponse res = HomeProvider.shouldInterceptRequest(
800 mActivity, url);
801 return res;
802 }
803
804 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700805 public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
806 if (!mInForeground) {
807 return false;
808 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700809 return mWebViewController.shouldOverrideKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700810 }
811
812 @Override
813 public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700814 if (!mInForeground) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700815 return;
816 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700817 mWebViewController.onUnhandledKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700818 }
819 };
820
821 // -------------------------------------------------------------------------
822 // WebChromeClient implementation for the main WebView
823 // -------------------------------------------------------------------------
824
825 private final WebChromeClient mWebChromeClient = new WebChromeClient() {
826 // Helper method to create a new tab or sub window.
827 private void createWindow(final boolean dialog, final Message msg) {
828 WebView.WebViewTransport transport =
829 (WebView.WebViewTransport) msg.obj;
830 if (dialog) {
831 createSubWindow();
Michael Kolb8233fac2010-10-26 16:08:53 -0700832 mWebViewController.attachSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700833 transport.setWebView(mSubView);
834 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -0700835 final Tab newTab = mWebViewController.openTabAndShow(
Michael Kolb18eb3772010-12-10 14:29:51 -0800836 Tab.this,
Michael Kolb8233fac2010-10-26 16:08:53 -0700837 IntentHandler.EMPTY_URL_DATA, false, null);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700838 if (newTab != Tab.this) {
839 Tab.this.addChildTab(newTab);
840 }
841 transport.setWebView(newTab.getWebView());
842 }
843 msg.sendToTarget();
844 }
845
846 @Override
847 public boolean onCreateWindow(WebView view, final boolean dialog,
848 final boolean userGesture, final Message resultMsg) {
849 // only allow new window or sub window for the foreground case
850 if (!mInForeground) {
851 return false;
852 }
853 // Short-circuit if we can't create any more tabs or sub windows.
854 if (dialog && mSubView != null) {
855 new AlertDialog.Builder(mActivity)
856 .setTitle(R.string.too_many_subwindows_dialog_title)
857 .setIcon(android.R.drawable.ic_dialog_alert)
858 .setMessage(R.string.too_many_subwindows_dialog_message)
859 .setPositiveButton(R.string.ok, null)
860 .show();
861 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700862 } else if (!mWebViewController.getTabControl().canCreateNewTab()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700863 new AlertDialog.Builder(mActivity)
864 .setTitle(R.string.too_many_windows_dialog_title)
865 .setIcon(android.R.drawable.ic_dialog_alert)
866 .setMessage(R.string.too_many_windows_dialog_message)
867 .setPositiveButton(R.string.ok, null)
868 .show();
869 return false;
870 }
871
872 // Short-circuit if this was a user gesture.
873 if (userGesture) {
874 createWindow(dialog, resultMsg);
875 return true;
876 }
877
878 // Allow the popup and create the appropriate window.
879 final AlertDialog.OnClickListener allowListener =
880 new AlertDialog.OnClickListener() {
881 public void onClick(DialogInterface d,
882 int which) {
883 createWindow(dialog, resultMsg);
884 }
885 };
886
887 // Block the popup by returning a null WebView.
888 final AlertDialog.OnClickListener blockListener =
889 new AlertDialog.OnClickListener() {
890 public void onClick(DialogInterface d, int which) {
891 resultMsg.sendToTarget();
892 }
893 };
894
895 // Build a confirmation dialog to display to the user.
896 final AlertDialog d =
897 new AlertDialog.Builder(mActivity)
898 .setTitle(R.string.attention)
899 .setIcon(android.R.drawable.ic_dialog_alert)
900 .setMessage(R.string.popup_window_attempt)
901 .setPositiveButton(R.string.allow, allowListener)
902 .setNegativeButton(R.string.block, blockListener)
903 .setCancelable(false)
904 .create();
905
906 // Show the confirmation dialog.
907 d.show();
908 return true;
909 }
910
911 @Override
Patrick Scotteb5061b2009-11-18 15:00:30 -0500912 public void onRequestFocus(WebView view) {
913 if (!mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700914 mWebViewController.switchToTab(mWebViewController.getTabControl().getTabIndex(
Patrick Scotteb5061b2009-11-18 15:00:30 -0500915 Tab.this));
916 }
917 }
918
919 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700920 public void onCloseWindow(WebView window) {
921 if (mParentTab != null) {
922 // JavaScript can only close popup window.
923 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700924 mWebViewController.switchToTab(mWebViewController.getTabControl()
Grace Kloba22ac16e2009-10-07 18:00:23 -0700925 .getTabIndex(mParentTab));
926 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700927 mWebViewController.closeTab(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700928 }
929 }
930
931 @Override
932 public void onProgressChanged(WebView view, int newProgress) {
John Reck30c714c2010-12-16 17:30:34 -0800933 mPageLoadProgress = newProgress;
934 mWebViewController.onProgressChanged(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700935 }
936
937 @Override
Leon Scroggins21d9b902010-03-11 09:33:11 -0500938 public void onReceivedTitle(WebView view, final String title) {
John Reck30c714c2010-12-16 17:30:34 -0800939 mCurrentState.mTitle = title;
Michael Kolb8233fac2010-10-26 16:08:53 -0700940 mWebViewController.onReceivedTitle(Tab.this, title);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700941 }
942
943 @Override
944 public void onReceivedIcon(WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -0800945 mCurrentState.mFavicon = icon;
Michael Kolb8233fac2010-10-26 16:08:53 -0700946 mWebViewController.onFavicon(Tab.this, view, icon);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700947 }
948
949 @Override
950 public void onReceivedTouchIconUrl(WebView view, String url,
951 boolean precomposed) {
952 final ContentResolver cr = mActivity.getContentResolver();
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400953 // Let precomposed icons take precedence over non-composed
954 // icons.
955 if (precomposed && mTouchIconLoader != null) {
956 mTouchIconLoader.cancel(false);
957 mTouchIconLoader = null;
958 }
959 // Have only one async task at a time.
960 if (mTouchIconLoader == null) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700961 mTouchIconLoader = new DownloadTouchIcon(Tab.this,
962 mActivity, cr, view);
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400963 mTouchIconLoader.execute(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700964 }
965 }
966
967 @Override
968 public void onShowCustomView(View view,
969 WebChromeClient.CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700970 if (mInForeground) mWebViewController.showCustomView(Tab.this, view,
971 callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700972 }
973
974 @Override
975 public void onHideCustomView() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700976 if (mInForeground) mWebViewController.hideCustomView();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700977 }
978
979 /**
980 * The origin has exceeded its database quota.
981 * @param url the URL that exceeded the quota
982 * @param databaseIdentifier the identifier of the database on which the
983 * transaction that caused the quota overflow was run
984 * @param currentQuota the current quota for the origin.
985 * @param estimatedSize the estimated size of the database.
986 * @param totalUsedQuota is the sum of all origins' quota.
987 * @param quotaUpdater The callback to run when a decision to allow or
988 * deny quota has been made. Don't forget to call this!
989 */
990 @Override
991 public void onExceededDatabaseQuota(String url,
992 String databaseIdentifier, long currentQuota, long estimatedSize,
993 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
994 BrowserSettings.getInstance().getWebStorageSizeManager()
995 .onExceededDatabaseQuota(url, databaseIdentifier,
996 currentQuota, estimatedSize, totalUsedQuota,
997 quotaUpdater);
998 }
999
1000 /**
1001 * The Application Cache has exceeded its max size.
1002 * @param spaceNeeded is the amount of disk space that would be needed
1003 * in order for the last appcache operation to succeed.
1004 * @param totalUsedQuota is the sum of all origins' quota.
1005 * @param quotaUpdater A callback to inform the WebCore thread that a
1006 * new app cache size is available. This callback must always
1007 * be executed at some point to ensure that the sleeping
1008 * WebCore thread is woken up.
1009 */
1010 @Override
1011 public void onReachedMaxAppCacheSize(long spaceNeeded,
1012 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
1013 BrowserSettings.getInstance().getWebStorageSizeManager()
1014 .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota,
1015 quotaUpdater);
1016 }
1017
1018 /**
1019 * Instructs the browser to show a prompt to ask the user to set the
1020 * Geolocation permission state for the specified origin.
1021 * @param origin The origin for which Geolocation permissions are
1022 * requested.
1023 * @param callback The callback to call once the user has set the
1024 * Geolocation permission state.
1025 */
1026 @Override
1027 public void onGeolocationPermissionsShowPrompt(String origin,
1028 GeolocationPermissions.Callback callback) {
1029 if (mInForeground) {
Grace Kloba50c241e2010-04-20 11:07:50 -07001030 getGeolocationPermissionsPrompt().show(origin, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001031 }
1032 }
1033
1034 /**
1035 * Instructs the browser to hide the Geolocation permissions prompt.
1036 */
1037 @Override
1038 public void onGeolocationPermissionsHidePrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001039 if (mInForeground && mGeolocationPermissionsPrompt != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001040 mGeolocationPermissionsPrompt.hide();
1041 }
1042 }
1043
Ben Murdoch65acc352009-11-19 18:16:04 +00001044 /* Adds a JavaScript error message to the system log and if the JS
1045 * console is enabled in the about:debug options, to that console
1046 * also.
Ben Murdochc42addf2010-01-28 15:19:59 +00001047 * @param consoleMessage the message object.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001048 */
1049 @Override
Ben Murdochc42addf2010-01-28 15:19:59 +00001050 public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001051 if (mInForeground) {
1052 // call getErrorConsole(true) so it will create one if needed
1053 ErrorConsoleView errorConsole = getErrorConsole(true);
Ben Murdochc42addf2010-01-28 15:19:59 +00001054 errorConsole.addErrorMessage(consoleMessage);
Michael Kolb8233fac2010-10-26 16:08:53 -07001055 if (mWebViewController.shouldShowErrorConsole()
1056 && errorConsole.getShowState() !=
1057 ErrorConsoleView.SHOW_MAXIMIZED) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001058 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
1059 }
1060 }
Ben Murdochc42addf2010-01-28 15:19:59 +00001061
Jeff Hamilton47654f42010-09-07 09:57:51 -05001062 // Don't log console messages in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001063 if (isPrivateBrowsingEnabled()) return true;
Jeff Hamilton47654f42010-09-07 09:57:51 -05001064
Ben Murdochc42addf2010-01-28 15:19:59 +00001065 String message = "Console: " + consoleMessage.message() + " "
1066 + consoleMessage.sourceId() + ":"
1067 + consoleMessage.lineNumber();
1068
1069 switch (consoleMessage.messageLevel()) {
1070 case TIP:
1071 Log.v(CONSOLE_LOGTAG, message);
1072 break;
1073 case LOG:
1074 Log.i(CONSOLE_LOGTAG, message);
1075 break;
1076 case WARNING:
1077 Log.w(CONSOLE_LOGTAG, message);
1078 break;
1079 case ERROR:
1080 Log.e(CONSOLE_LOGTAG, message);
1081 break;
1082 case DEBUG:
1083 Log.d(CONSOLE_LOGTAG, message);
1084 break;
1085 }
1086
1087 return true;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001088 }
1089
1090 /**
1091 * Ask the browser for an icon to represent a <video> element.
1092 * This icon will be used if the Web page did not specify a poster attribute.
1093 * @return Bitmap The icon or null if no such icon is available.
1094 */
1095 @Override
1096 public Bitmap getDefaultVideoPoster() {
1097 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001098 return mWebViewController.getDefaultVideoPoster();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001099 }
1100 return null;
1101 }
1102
1103 /**
1104 * Ask the host application for a custom progress view to show while
1105 * a <video> is loading.
1106 * @return View The progress view.
1107 */
1108 @Override
1109 public View getVideoLoadingProgressView() {
1110 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001111 return mWebViewController.getVideoLoadingProgressView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001112 }
1113 return null;
1114 }
1115
1116 @Override
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01001117 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001118 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001119 mWebViewController.openFileChooser(uploadMsg, acceptType);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001120 } else {
1121 uploadMsg.onReceiveValue(null);
1122 }
1123 }
1124
1125 /**
1126 * Deliver a list of already-visited URLs
1127 */
1128 @Override
1129 public void getVisitedHistory(final ValueCallback<String[]> callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001130 mWebViewController.getVisitedHistory(callback);
1131 }
Ben Murdoch8029a772010-11-16 11:58:21 +00001132
1133 @Override
1134 public void setupAutoFill(Message message) {
1135 // Prompt the user to set up their profile.
1136 final Message msg = message;
1137 AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
Ben Murdoch1d676b62011-01-17 12:54:24 +00001138 LayoutInflater inflater = (LayoutInflater) mActivity.getSystemService(
1139 Context.LAYOUT_INFLATER_SERVICE);
1140 final View layout = inflater.inflate(R.layout.setup_autofill_dialog, null);
1141
1142 builder.setView(layout)
1143 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1144 @Override
1145 public void onClick(DialogInterface dialog, int id) {
1146 CheckBox disableAutoFill = (CheckBox) layout.findViewById(
1147 R.id.setup_autofill_dialog_disable_autofill);
1148
1149 if (disableAutoFill.isChecked()) {
1150 // Disable autofill and show a toast with how to turn it on again.
1151 BrowserSettings s = BrowserSettings.getInstance();
1152 s.addObserver(mMainView.getSettings());
1153 s.disableAutoFill(mActivity);
1154 s.update();
1155 Toast.makeText(mActivity,
1156 R.string.autofill_setup_dialog_negative_toast,
1157 Toast.LENGTH_LONG).show();
1158 } else {
1159 // Take user to the AutoFill profile editor. When they return,
1160 // we will send the message that we pass here which will trigger
1161 // the form to get filled out with their new profile.
1162 mWebViewController.setupAutoFill(msg);
1163 }
1164 }
1165 })
1166 .setNegativeButton(R.string.cancel, null)
1167 .show();
Ben Murdoch8029a772010-11-16 11:58:21 +00001168 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001169 };
1170
1171 // -------------------------------------------------------------------------
1172 // WebViewClient implementation for the sub window
1173 // -------------------------------------------------------------------------
1174
1175 // Subclass of WebViewClient used in subwindows to notify the main
1176 // WebViewClient of certain WebView activities.
1177 private static class SubWindowClient extends WebViewClient {
1178 // The main WebViewClient.
1179 private final WebViewClient mClient;
Michael Kolb8233fac2010-10-26 16:08:53 -07001180 private final WebViewController mController;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001181
Michael Kolb8233fac2010-10-26 16:08:53 -07001182 SubWindowClient(WebViewClient client, WebViewController controller) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001183 mClient = client;
Michael Kolb8233fac2010-10-26 16:08:53 -07001184 mController = controller;
Leon Scroggins III211ba542010-04-19 13:21:13 -04001185 }
1186 @Override
1187 public void onPageStarted(WebView view, String url, Bitmap favicon) {
1188 // Unlike the others, do not call mClient's version, which would
1189 // change the progress bar. However, we do want to remove the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001190 // find or select dialog.
Michael Kolb8233fac2010-10-26 16:08:53 -07001191 mController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001192 }
1193 @Override
1194 public void doUpdateVisitedHistory(WebView view, String url,
1195 boolean isReload) {
1196 mClient.doUpdateVisitedHistory(view, url, isReload);
1197 }
1198 @Override
1199 public boolean shouldOverrideUrlLoading(WebView view, String url) {
1200 return mClient.shouldOverrideUrlLoading(view, url);
1201 }
1202 @Override
1203 public void onReceivedSslError(WebView view, SslErrorHandler handler,
1204 SslError error) {
1205 mClient.onReceivedSslError(view, handler, error);
1206 }
1207 @Override
1208 public void onReceivedHttpAuthRequest(WebView view,
1209 HttpAuthHandler handler, String host, String realm) {
1210 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
1211 }
1212 @Override
1213 public void onFormResubmission(WebView view, Message dontResend,
1214 Message resend) {
1215 mClient.onFormResubmission(view, dontResend, resend);
1216 }
1217 @Override
1218 public void onReceivedError(WebView view, int errorCode,
1219 String description, String failingUrl) {
1220 mClient.onReceivedError(view, errorCode, description, failingUrl);
1221 }
1222 @Override
1223 public boolean shouldOverrideKeyEvent(WebView view,
1224 android.view.KeyEvent event) {
1225 return mClient.shouldOverrideKeyEvent(view, event);
1226 }
1227 @Override
1228 public void onUnhandledKeyEvent(WebView view,
1229 android.view.KeyEvent event) {
1230 mClient.onUnhandledKeyEvent(view, event);
1231 }
1232 }
1233
1234 // -------------------------------------------------------------------------
1235 // WebChromeClient implementation for the sub window
1236 // -------------------------------------------------------------------------
1237
1238 private class SubWindowChromeClient extends WebChromeClient {
1239 // The main WebChromeClient.
1240 private final WebChromeClient mClient;
1241
1242 SubWindowChromeClient(WebChromeClient client) {
1243 mClient = client;
1244 }
1245 @Override
1246 public void onProgressChanged(WebView view, int newProgress) {
1247 mClient.onProgressChanged(view, newProgress);
1248 }
1249 @Override
1250 public boolean onCreateWindow(WebView view, boolean dialog,
1251 boolean userGesture, android.os.Message resultMsg) {
1252 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
1253 }
1254 @Override
1255 public void onCloseWindow(WebView window) {
1256 if (window != mSubView) {
1257 Log.e(LOGTAG, "Can't close the window");
1258 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001259 mWebViewController.dismissSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001260 }
1261 }
1262
1263 // -------------------------------------------------------------------------
1264
Michael Kolb8233fac2010-10-26 16:08:53 -07001265 // TODO temporarily use activity here
1266 // remove later
1267
Grace Kloba22ac16e2009-10-07 18:00:23 -07001268 // Construct a new tab
Michael Kolb8233fac2010-10-26 16:08:53 -07001269 Tab(WebViewController wvcontroller, WebView w, boolean closeOnExit, String appId,
Grace Kloba22ac16e2009-10-07 18:00:23 -07001270 String url) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001271 mWebViewController = wvcontroller;
1272 mActivity = mWebViewController.getActivity();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001273 mCloseOnExit = closeOnExit;
1274 mAppId = appId;
John Recke969cc52010-12-21 17:24:43 -08001275 mDataController = DataController.getInstance(mActivity);
John Reck30c714c2010-12-16 17:30:34 -08001276 mCurrentState = new PageState(mActivity, w.isPrivateBrowsingEnabled());
Michael Kolb8233fac2010-10-26 16:08:53 -07001277 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001278 mInForeground = false;
1279
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001280 mDownloadListener = new DownloadListener() {
1281 public void onDownloadStart(String url, String userAgent,
1282 String contentDisposition, String mimetype,
1283 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001284 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001285 mimetype, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001286 }
1287 };
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001288 mWebBackForwardListClient = new WebBackForwardListClient() {
1289 @Override
1290 public void onNewHistoryItem(WebHistoryItem item) {
1291 if (isInVoiceSearchMode()) {
1292 item.setCustomData(mVoiceSearchData.mVoiceSearchIntent);
1293 }
1294 }
1295 @Override
1296 public void onIndexChanged(WebHistoryItem item, int index) {
1297 Object data = item.getCustomData();
1298 if (data != null && data instanceof Intent) {
1299 activateVoiceSearchMode((Intent) data);
1300 }
1301 }
1302 };
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001303
Grace Kloba22ac16e2009-10-07 18:00:23 -07001304 setWebView(w);
1305 }
1306
1307 /**
1308 * Sets the WebView for this tab, correctly removing the old WebView from
1309 * the container view.
1310 */
1311 void setWebView(WebView w) {
1312 if (mMainView == w) {
1313 return;
1314 }
Michael Kolba713ec82010-11-29 17:27:06 -08001315
Grace Kloba22ac16e2009-10-07 18:00:23 -07001316 // If the WebView is changing, the page will be reloaded, so any ongoing
1317 // Geolocation permission requests are void.
Grace Kloba50c241e2010-04-20 11:07:50 -07001318 if (mGeolocationPermissionsPrompt != null) {
1319 mGeolocationPermissionsPrompt.hide();
1320 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001321
Michael Kolba713ec82010-11-29 17:27:06 -08001322 mWebViewController.onSetWebView(this, w);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001323
1324 // set the new one
1325 mMainView = w;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001326 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001327 if (mMainView != null) {
1328 mMainView.setWebViewClient(mWebViewClient);
1329 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001330 // Attach DownloadManager so that downloads can start in an active
1331 // or a non-active window. This can happen when going to a site that
1332 // does a redirect after a period of time. The user could have
1333 // switched to another tab while waiting for the download to start.
1334 mMainView.setDownloadListener(mDownloadListener);
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001335 mMainView.setWebBackForwardListClient(mWebBackForwardListClient);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001336 }
1337 }
1338
1339 /**
1340 * Destroy the tab's main WebView and subWindow if any
1341 */
1342 void destroy() {
1343 if (mMainView != null) {
1344 dismissSubWindow();
1345 BrowserSettings.getInstance().deleteObserver(mMainView.getSettings());
1346 // save the WebView to call destroy() after detach it from the tab
1347 WebView webView = mMainView;
1348 setWebView(null);
1349 webView.destroy();
1350 }
1351 }
1352
1353 /**
1354 * Remove the tab from the parent
1355 */
1356 void removeFromTree() {
1357 // detach the children
1358 if (mChildTabs != null) {
1359 for(Tab t : mChildTabs) {
1360 t.setParentTab(null);
1361 }
1362 }
1363 // remove itself from the parent list
1364 if (mParentTab != null) {
1365 mParentTab.mChildTabs.remove(this);
1366 }
1367 }
1368
1369 /**
1370 * Create a new subwindow unless a subwindow already exists.
1371 * @return True if a new subwindow was created. False if one already exists.
1372 */
1373 boolean createSubWindow() {
1374 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001375 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001376 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001377 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001378 mSubView.setWebChromeClient(new SubWindowChromeClient(
1379 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001380 // Set a different DownloadListener for the mSubView, since it will
1381 // just need to dismiss the mSubView, rather than close the Tab
1382 mSubView.setDownloadListener(new DownloadListener() {
1383 public void onDownloadStart(String url, String userAgent,
1384 String contentDisposition, String mimetype,
1385 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001386 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001387 contentDisposition, mimetype, contentLength);
1388 if (mSubView.copyBackForwardList().getSize() == 0) {
1389 // This subwindow was opened for the sole purpose of
1390 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001391 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001392 }
1393 }
1394 });
Grace Kloba22ac16e2009-10-07 18:00:23 -07001395 mSubView.setOnCreateContextMenuListener(mActivity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001396 return true;
1397 }
1398 return false;
1399 }
1400
1401 /**
1402 * Dismiss the subWindow for the tab.
1403 */
1404 void dismissSubWindow() {
1405 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001406 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001407 BrowserSettings.getInstance().deleteObserver(
1408 mSubView.getSettings());
1409 mSubView.destroy();
1410 mSubView = null;
1411 mSubViewContainer = null;
1412 }
1413 }
1414
Grace Kloba22ac16e2009-10-07 18:00:23 -07001415
1416 /**
1417 * Set the parent tab of this tab.
1418 */
1419 void setParentTab(Tab parent) {
1420 mParentTab = parent;
1421 // This tab may have been freed due to low memory. If that is the case,
1422 // the parent tab index is already saved. If we are changing that index
1423 // (most likely due to removing the parent tab) we must update the
1424 // parent tab index in the saved Bundle.
1425 if (mSavedState != null) {
1426 if (parent == null) {
1427 mSavedState.remove(PARENTTAB);
1428 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -07001429 mSavedState.putInt(PARENTTAB, mWebViewController.getTabControl()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001430 .getTabIndex(parent));
1431 }
1432 }
1433 }
1434
1435 /**
1436 * When a Tab is created through the content of another Tab, then we
1437 * associate the Tabs.
1438 * @param child the Tab that was created from this Tab
1439 */
1440 void addChildTab(Tab child) {
1441 if (mChildTabs == null) {
1442 mChildTabs = new Vector<Tab>();
1443 }
1444 mChildTabs.add(child);
1445 child.setParentTab(this);
1446 }
1447
1448 Vector<Tab> getChildTabs() {
1449 return mChildTabs;
1450 }
1451
1452 void resume() {
1453 if (mMainView != null) {
1454 mMainView.onResume();
1455 if (mSubView != null) {
1456 mSubView.onResume();
1457 }
1458 }
1459 }
1460
1461 void pause() {
1462 if (mMainView != null) {
1463 mMainView.onPause();
1464 if (mSubView != null) {
1465 mSubView.onPause();
1466 }
1467 }
1468 }
1469
1470 void putInForeground() {
1471 mInForeground = true;
1472 resume();
1473 mMainView.setOnCreateContextMenuListener(mActivity);
1474 if (mSubView != null) {
1475 mSubView.setOnCreateContextMenuListener(mActivity);
1476 }
1477 // Show the pending error dialog if the queue is not empty
1478 if (mQueuedErrors != null && mQueuedErrors.size() > 0) {
1479 showError(mQueuedErrors.getFirst());
1480 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001481 mWebViewController.bookmarkedStatusHasChanged(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001482 }
1483
1484 void putInBackground() {
1485 mInForeground = false;
1486 pause();
1487 mMainView.setOnCreateContextMenuListener(null);
1488 if (mSubView != null) {
1489 mSubView.setOnCreateContextMenuListener(null);
1490 }
1491 }
1492
Michael Kolb8233fac2010-10-26 16:08:53 -07001493 boolean inForeground() {
1494 return mInForeground;
1495 }
1496
Grace Kloba22ac16e2009-10-07 18:00:23 -07001497 /**
1498 * Return the top window of this tab; either the subwindow if it is not
1499 * null or the main window.
1500 * @return The top window of this tab.
1501 */
1502 WebView getTopWindow() {
1503 if (mSubView != null) {
1504 return mSubView;
1505 }
1506 return mMainView;
1507 }
1508
1509 /**
1510 * Return the main window of this tab. Note: if a tab is freed in the
1511 * background, this can return null. It is only guaranteed to be
1512 * non-null for the current tab.
1513 * @return The main WebView of this tab.
1514 */
1515 WebView getWebView() {
1516 return mMainView;
1517 }
1518
Michael Kolba713ec82010-11-29 17:27:06 -08001519 void setViewContainer(View container) {
1520 mContainer = container;
1521 }
1522
Michael Kolb8233fac2010-10-26 16:08:53 -07001523 View getViewContainer() {
1524 return mContainer;
1525 }
1526
Grace Kloba22ac16e2009-10-07 18:00:23 -07001527 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001528 * Return whether private browsing is enabled for the main window of
1529 * this tab.
1530 * @return True if private browsing is enabled.
1531 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001532 boolean isPrivateBrowsingEnabled() {
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001533 WebView webView = getWebView();
1534 if (webView == null) {
1535 return false;
1536 }
1537 return webView.isPrivateBrowsingEnabled();
1538 }
1539
1540 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001541 * Return the subwindow of this tab or null if there is no subwindow.
1542 * @return The subwindow of this tab or null.
1543 */
1544 WebView getSubWebView() {
1545 return mSubView;
1546 }
1547
Michael Kolb1514bb72010-11-22 09:11:48 -08001548 void setSubWebView(WebView subView) {
1549 mSubView = subView;
1550 }
1551
Michael Kolb8233fac2010-10-26 16:08:53 -07001552 View getSubViewContainer() {
1553 return mSubViewContainer;
1554 }
1555
Michael Kolb1514bb72010-11-22 09:11:48 -08001556 void setSubViewContainer(View subViewContainer) {
1557 mSubViewContainer = subViewContainer;
1558 }
1559
Grace Kloba22ac16e2009-10-07 18:00:23 -07001560 /**
1561 * @return The geolocation permissions prompt for this tab.
1562 */
1563 GeolocationPermissionsPrompt getGeolocationPermissionsPrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001564 if (mGeolocationPermissionsPrompt == null) {
1565 ViewStub stub = (ViewStub) mContainer
1566 .findViewById(R.id.geolocation_permissions_prompt);
1567 mGeolocationPermissionsPrompt = (GeolocationPermissionsPrompt) stub
1568 .inflate();
1569 mGeolocationPermissionsPrompt.init();
1570 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001571 return mGeolocationPermissionsPrompt;
1572 }
1573
1574 /**
1575 * @return The application id string
1576 */
1577 String getAppId() {
1578 return mAppId;
1579 }
1580
1581 /**
1582 * Set the application id string
1583 * @param id
1584 */
1585 void setAppId(String id) {
1586 mAppId = id;
1587 }
1588
Grace Kloba22ac16e2009-10-07 18:00:23 -07001589 String getUrl() {
John Reck324d4402011-01-11 16:56:42 -08001590 return UrlUtils.filteredUrl(mCurrentState.mUrl);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001591 }
1592
1593 /**
John Reck30c714c2010-12-16 17:30:34 -08001594 * Get the title of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001595 */
1596 String getTitle() {
John Reck30c714c2010-12-16 17:30:34 -08001597 if (mCurrentState.mTitle == null && mInPageLoad) {
1598 return mActivity.getString(R.string.title_bar_loading);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001599 }
John Reck30c714c2010-12-16 17:30:34 -08001600 return mCurrentState.mTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001601 }
1602
1603 /**
John Reck30c714c2010-12-16 17:30:34 -08001604 * Get the favicon of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001605 */
1606 Bitmap getFavicon() {
John Reck30c714c2010-12-16 17:30:34 -08001607 return mCurrentState.mFavicon;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001608 }
1609
John Recke969cc52010-12-21 17:24:43 -08001610 public boolean isBookmarkedSite() {
1611 return mCurrentState.mIsBookmarkedSite;
1612 }
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001613
Grace Kloba22ac16e2009-10-07 18:00:23 -07001614 /**
1615 * Return the tab's error console. Creates the console if createIfNEcessary
1616 * is true and we haven't already created the console.
1617 * @param createIfNecessary Flag to indicate if the console should be
1618 * created if it has not been already.
1619 * @return The tab's error console, or null if one has not been created and
1620 * createIfNecessary is false.
1621 */
1622 ErrorConsoleView getErrorConsole(boolean createIfNecessary) {
1623 if (createIfNecessary && mErrorConsole == null) {
1624 mErrorConsole = new ErrorConsoleView(mActivity);
1625 mErrorConsole.setWebView(mMainView);
1626 }
1627 return mErrorConsole;
1628 }
1629
1630 /**
1631 * If this Tab was created through another Tab, then this method returns
1632 * that Tab.
1633 * @return the Tab parent or null
1634 */
1635 public Tab getParentTab() {
1636 return mParentTab;
1637 }
1638
1639 /**
1640 * Return whether this tab should be closed when it is backing out of the
1641 * first page.
1642 * @return TRUE if this tab should be closed when exit.
1643 */
1644 boolean closeOnExit() {
1645 return mCloseOnExit;
1646 }
1647
John Reck30c714c2010-12-16 17:30:34 -08001648 private void setLockIconType(LockIcon icon) {
1649 mCurrentState.mLockIcon = icon;
1650 mWebViewController.onUpdatedLockIcon(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001651 }
1652
1653 /**
1654 * @return The tab's lock icon type.
1655 */
John Reck30c714c2010-12-16 17:30:34 -08001656 LockIcon getLockIconType() {
1657 return mCurrentState.mLockIcon;
1658 }
1659
1660 int getLoadProgress() {
1661 if (mInPageLoad) {
1662 return mPageLoadProgress;
1663 }
1664 return 100;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001665 }
1666
1667 /**
1668 * @return TRUE if onPageStarted is called while onPageFinished is not
1669 * called yet.
1670 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001671 boolean inPageLoad() {
1672 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001673 }
1674
1675 // force mInLoad to be false. This should only be called before closing the
1676 // tab to ensure BrowserActivity's pauseWebViewTimers() is called correctly.
Michael Kolb8233fac2010-10-26 16:08:53 -07001677 void clearInPageLoad() {
1678 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001679 }
1680
Grace Kloba22ac16e2009-10-07 18:00:23 -07001681 /**
John Reck30c714c2010-12-16 17:30:34 -08001682 * Get the cached saved state bundle.
1683 * @return cached state bundle
Grace Kloba22ac16e2009-10-07 18:00:23 -07001684 */
1685 Bundle getSavedState() {
1686 return mSavedState;
1687 }
1688
1689 /**
1690 * Set the saved state.
1691 */
1692 void setSavedState(Bundle state) {
1693 mSavedState = state;
1694 }
1695
1696 /**
1697 * @return TRUE if succeed in saving the state.
1698 */
1699 boolean saveState() {
1700 // If the WebView is null it means we ran low on memory and we already
1701 // stored the saved state in mSavedState.
1702 if (mMainView == null) {
1703 return mSavedState != null;
1704 }
1705
1706 mSavedState = new Bundle();
1707 final WebBackForwardList list = mMainView.saveState(mSavedState);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001708
1709 // Store some extra info for displaying the tab in the picker.
1710 final WebHistoryItem item = list != null ? list.getCurrentItem() : null;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001711
John Reck30c714c2010-12-16 17:30:34 -08001712 mSavedState.putString(CURRURL, mCurrentState.mUrl);
1713 mSavedState.putString(CURRTITLE, mCurrentState.mTitle);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001714 mSavedState.putBoolean(CLOSEONEXIT, mCloseOnExit);
1715 if (mAppId != null) {
1716 mSavedState.putString(APPID, mAppId);
1717 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001718 // Remember the parent tab so the relationship can be restored.
1719 if (mParentTab != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001720 mSavedState.putInt(PARENTTAB, mWebViewController.getTabControl().getTabIndex(
Grace Kloba22ac16e2009-10-07 18:00:23 -07001721 mParentTab));
1722 }
1723 return true;
1724 }
1725
1726 /*
1727 * Restore the state of the tab.
1728 */
1729 boolean restoreState(Bundle b) {
1730 if (b == null) {
1731 return false;
1732 }
1733 // Restore the internal state even if the WebView fails to restore.
1734 // This will maintain the app id, original url and close-on-exit values.
1735 mSavedState = null;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001736 mCloseOnExit = b.getBoolean(CLOSEONEXIT);
1737 mAppId = b.getString(APPID);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001738
1739 final WebBackForwardList list = mMainView.restoreState(b);
1740 if (list == null) {
1741 return false;
1742 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001743 return true;
1744 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04001745
Leon Scroggins1961ed22010-12-07 15:22:21 -05001746 public void updateBookmarkedStatus() {
John Recke969cc52010-12-21 17:24:43 -08001747 mDataController.queryBookmarkStatus(getUrl(), mIsBookmarkCallback);
Leon Scroggins1961ed22010-12-07 15:22:21 -05001748 }
1749
John Recke969cc52010-12-21 17:24:43 -08001750 private DataController.OnQueryUrlIsBookmark mIsBookmarkCallback
1751 = new DataController.OnQueryUrlIsBookmark() {
1752 @Override
1753 public void onQueryUrlIsBookmark(String url, boolean isBookmark) {
1754 if (mCurrentState.mUrl.equals(url)) {
1755 mCurrentState.mIsBookmarkedSite = isBookmark;
1756 mWebViewController.bookmarkedStatusHasChanged(Tab.this);
1757 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001758 }
John Recke969cc52010-12-21 17:24:43 -08001759 };
Grace Kloba22ac16e2009-10-07 18:00:23 -07001760}