blob: 95c7850961cea5691b3a8d09604ce25ec5dc26a9 [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
Michael Kolb8233fac2010-10-26 16:08:53 -070019import android.app.Activity;
Grace Kloba22ac16e2009-10-07 18:00:23 -070020import android.app.AlertDialog;
Leon Scroggins58d56c62010-01-28 15:12:40 -050021import android.app.SearchManager;
Grace Kloba22ac16e2009-10-07 18:00:23 -070022import android.content.ContentResolver;
John Reck30c714c2010-12-16 17:30:34 -080023import android.content.Context;
Grace Kloba22ac16e2009-10-07 18:00:23 -070024import android.content.DialogInterface;
Michael Kolbfe251992010-07-08 15:41:55 -070025import android.content.DialogInterface.OnCancelListener;
Jeff Hamilton8ce956c2010-08-17 11:13:53 -050026import android.content.Intent;
Grace Kloba22ac16e2009-10-07 18:00:23 -070027import android.graphics.Bitmap;
John Reck541f55a2011-06-07 16:34:43 -070028import android.graphics.Bitmap.CompressFormat;
John Reck30c714c2010-12-16 17:30:34 -080029import android.graphics.BitmapFactory;
Grace Kloba22ac16e2009-10-07 18:00:23 -070030import android.net.Uri;
31import android.net.http.SslError;
Grace Kloba22ac16e2009-10-07 18:00:23 -070032import android.os.Bundle;
33import android.os.Message;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +000034import android.os.SystemClock;
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -050035import android.speech.RecognizerResultsIntent;
Grace Kloba22ac16e2009-10-07 18:00:23 -070036import android.util.Log;
37import android.view.KeyEvent;
38import android.view.LayoutInflater;
39import android.view.View;
Grace Kloba50c241e2010-04-20 11:07:50 -070040import android.view.ViewStub;
Ben Murdochc42addf2010-01-28 15:19:59 +000041import android.webkit.ConsoleMessage;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -050042import android.webkit.DownloadListener;
Grace Kloba22ac16e2009-10-07 18:00:23 -070043import android.webkit.GeolocationPermissions;
44import android.webkit.HttpAuthHandler;
45import android.webkit.SslErrorHandler;
46import android.webkit.URLUtil;
47import android.webkit.ValueCallback;
48import android.webkit.WebBackForwardList;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -050049import android.webkit.WebBackForwardListClient;
Grace Kloba22ac16e2009-10-07 18:00:23 -070050import android.webkit.WebChromeClient;
51import android.webkit.WebHistoryItem;
John Reck438bf462011-01-12 18:11:46 -080052import android.webkit.WebResourceResponse;
Grace Kloba22ac16e2009-10-07 18:00:23 -070053import android.webkit.WebStorage;
54import android.webkit.WebView;
55import android.webkit.WebViewClient;
Ben Murdoch1d676b62011-01-17 12:54:24 +000056import android.widget.CheckBox;
Grace Kloba22ac16e2009-10-07 18:00:23 -070057import android.widget.LinearLayout;
58import android.widget.TextView;
Ben Murdoch8029a772010-11-16 11:58:21 +000059import android.widget.Toast;
Grace Kloba22ac16e2009-10-07 18:00:23 -070060
John Reck541f55a2011-06-07 16:34:43 -070061import com.android.browser.homepages.HomeProvider;
62import com.android.common.speech.LoggingEvents;
63
64import java.io.ByteArrayOutputStream;
65import java.io.DataInputStream;
66import java.io.DataOutputStream;
67import java.io.IOException;
68import java.io.InputStream;
69import java.io.OutputStream;
Michael Kolbfe251992010-07-08 15:41:55 -070070import java.util.ArrayList;
71import java.util.HashMap;
72import java.util.Iterator;
73import java.util.LinkedList;
74import java.util.Map;
75import java.util.Vector;
76
Grace Kloba22ac16e2009-10-07 18:00:23 -070077/**
78 * Class for maintaining Tabs with a main WebView and a subwindow.
79 */
80class Tab {
Michael Kolb8233fac2010-10-26 16:08:53 -070081
Grace Kloba22ac16e2009-10-07 18:00:23 -070082 // Log Tag
83 private static final String LOGTAG = "Tab";
Ben Murdochc42addf2010-01-28 15:19:59 +000084 // Special case the logtag for messages for the Console to make it easier to
85 // filter them and match the logtag used for these messages in older versions
86 // of the browser.
87 private static final String CONSOLE_LOGTAG = "browser";
88
John Reck30c714c2010-12-16 17:30:34 -080089 public enum LockIcon {
90 LOCK_ICON_UNSECURE,
91 LOCK_ICON_SECURE,
92 LOCK_ICON_MIXED,
93 }
Michael Kolb8233fac2010-10-26 16:08:53 -070094
95 Activity mActivity;
96 private WebViewController mWebViewController;
97
Michael Kolbc831b632011-05-11 09:30:34 -070098 // The tab ID
99 private long mId;
100
Grace Kloba22ac16e2009-10-07 18:00:23 -0700101 // The Geolocation permissions prompt
102 private GeolocationPermissionsPrompt mGeolocationPermissionsPrompt;
103 // Main WebView wrapper
Michael Kolba713ec82010-11-29 17:27:06 -0800104 private View mContainer;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700105 // Main WebView
106 private WebView mMainView;
107 // Subwindow container
108 private View mSubViewContainer;
109 // Subwindow WebView
110 private WebView mSubView;
111 // Saved bundle for when we are running low on memory. It contains the
112 // information needed to restore the WebView if the user goes back to the
113 // tab.
114 private Bundle mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700115 // Parent Tab. This is the Tab that created this Tab, or null if the Tab was
116 // created by the UI
Michael Kolbc831b632011-05-11 09:30:34 -0700117 private Tab mParent;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700118 // Tab that constructed by this Tab. This is used when this Tab is
119 // destroyed, it clears all mParentTab values in the children.
Michael Kolbc831b632011-05-11 09:30:34 -0700120 private Vector<Tab> mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700121 // If true, the tab is in the foreground of the current activity.
122 private boolean mInForeground;
Michael Kolb8233fac2010-10-26 16:08:53 -0700123 // If true, the tab is in page loading state (after onPageStarted,
124 // before onPageFinsihed)
125 private boolean mInPageLoad;
John Reck30c714c2010-12-16 17:30:34 -0800126 // The last reported progress of the current page
127 private int mPageLoadProgress;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000128 // The time the load started, used to find load page time
129 private long mLoadStartTime;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700130 // Application identifier used to find tabs that another application wants
131 // to reuse.
132 private String mAppId;
133 // Keep the original url around to avoid killing the old WebView if the url
134 // has not changed.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700135 // Error console for the tab
136 private ErrorConsoleView mErrorConsole;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -0500137 // The listener that gets invoked when a download is started from the
138 // mMainView
139 private final DownloadListener mDownloadListener;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500140 // Listener used to know when we move forward or back in the history list.
141 private final WebBackForwardListClient mWebBackForwardListClient;
John Recke969cc52010-12-21 17:24:43 -0800142 private DataController mDataController;
Patrick Scott92066772011-03-10 08:46:27 -0500143 // State of the auto-login request.
144 private DeviceAccountLogin mDeviceAccountLogin;
John Reck541f55a2011-06-07 16:34:43 -0700145 private boolean mIsSnapshot = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700146
147 // AsyncTask for downloading touch icons
148 DownloadTouchIcon mTouchIconLoader;
149
Michael Kolbeb95db42011-03-03 10:38:40 -0800150 private Bitmap mScreenshot;
John Reck35e9dd62011-04-25 09:01:54 -0700151 private BrowserSettings mSettings;
Michael Kolbeb95db42011-03-03 10:38:40 -0800152
John Reck30c714c2010-12-16 17:30:34 -0800153 // All the state needed for a page
154 private static class PageState {
155 String mUrl;
156 String mTitle;
157 LockIcon mLockIcon;
158 Bitmap mFavicon;
John Recke969cc52010-12-21 17:24:43 -0800159 Boolean mIsBookmarkedSite = false;
John Reck30c714c2010-12-16 17:30:34 -0800160
161 PageState(Context c, boolean incognito) {
162 if (incognito) {
163 mUrl = "browser:incognito";
164 mTitle = c.getString(R.string.new_incognito_tab);
John Reck30c714c2010-12-16 17:30:34 -0800165 } else {
166 mUrl = "";
167 mTitle = c.getString(R.string.new_tab);
John Reck30c714c2010-12-16 17:30:34 -0800168 }
Justin Hodc6cbb72011-01-24 15:14:54 -0800169 mFavicon = BitmapFactory.decodeResource(
170 c.getResources(), R.drawable.app_web_browser_sm);
John Reck30c714c2010-12-16 17:30:34 -0800171 mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
172 }
173
174 PageState(Context c, boolean incognito, String url, Bitmap favicon) {
175 mUrl = url;
176 mTitle = null;
177 if (URLUtil.isHttpsUrl(url)) {
178 mLockIcon = LockIcon.LOCK_ICON_SECURE;
179 } else {
180 mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
181 }
182 if (favicon != null) {
183 mFavicon = favicon;
184 } else {
Justin Hodc6cbb72011-01-24 15:14:54 -0800185 mFavicon = BitmapFactory.decodeResource(
186 c.getResources(), R.drawable.app_web_browser_sm);
John Reck30c714c2010-12-16 17:30:34 -0800187 }
188 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700189 }
190
John Reck30c714c2010-12-16 17:30:34 -0800191 // The current/loading page's state
192 private PageState mCurrentState;
193
Grace Kloba22ac16e2009-10-07 18:00:23 -0700194 // Used for saving and restoring each Tab
Michael Kolbc831b632011-05-11 09:30:34 -0700195 static final String ID = "ID";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700196 static final String CURRURL = "currentUrl";
197 static final String CURRTITLE = "currentTitle";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700198 static final String PARENTTAB = "parentTab";
199 static final String APPID = "appid";
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700200 static final String INCOGNITO = "privateBrowsingEnabled";
Michael Kolbeb95db42011-03-03 10:38:40 -0800201 static final String SCREENSHOT = "screenshot";
John Reckb0a86db2011-05-24 14:05:58 -0700202 static final String USERAGENT = "useragent";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700203
204 // -------------------------------------------------------------------------
205
Leon Scroggins58d56c62010-01-28 15:12:40 -0500206 /**
207 * Private information regarding the latest voice search. If the Tab is not
208 * in voice search mode, this will be null.
209 */
210 private VoiceSearchData mVoiceSearchData;
211 /**
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400212 * Remove voice search mode from this tab.
213 */
214 public void revertVoiceSearchMode() {
215 if (mVoiceSearchData != null) {
216 mVoiceSearchData = null;
217 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700218 mWebViewController.revertVoiceSearchMode(this);
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400219 }
220 }
221 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700222
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400223 /**
Leon Scroggins58d56c62010-01-28 15:12:40 -0500224 * Return whether the tab is in voice search mode.
225 */
226 public boolean isInVoiceSearchMode() {
227 return mVoiceSearchData != null;
228 }
229 /**
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400230 * Return true if the Tab is in voice search mode and the voice search
231 * Intent came with a String identifying that Google provided the Intent.
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500232 */
233 public boolean voiceSearchSourceIsGoogle() {
234 return mVoiceSearchData != null && mVoiceSearchData.mSourceIsGoogle;
235 }
236 /**
Leon Scroggins58d56c62010-01-28 15:12:40 -0500237 * Get the title to display for the current voice search page. If the Tab
238 * is not in voice search mode, return null.
239 */
240 public String getVoiceDisplayTitle() {
241 if (mVoiceSearchData == null) return null;
242 return mVoiceSearchData.mLastVoiceSearchTitle;
243 }
244 /**
245 * Get the latest array of voice search results, to be passed to the
246 * BrowserProvider. If the Tab is not in voice search mode, return null.
247 */
248 public ArrayList<String> getVoiceSearchResults() {
249 if (mVoiceSearchData == null) return null;
250 return mVoiceSearchData.mVoiceSearchResults;
251 }
252 /**
253 * Activate voice search mode.
254 * @param intent Intent which has the results to use, or an index into the
255 * results when reusing the old results.
256 */
257 /* package */ void activateVoiceSearchMode(Intent intent) {
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500258 int index = 0;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500259 ArrayList<String> results = intent.getStringArrayListExtra(
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -0500260 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_STRINGS);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500261 if (results != null) {
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500262 ArrayList<String> urls = intent.getStringArrayListExtra(
263 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_URLS);
264 ArrayList<String> htmls = intent.getStringArrayListExtra(
265 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_HTML);
266 ArrayList<String> baseUrls = intent.getStringArrayListExtra(
267 RecognizerResultsIntent
268 .EXTRA_VOICE_SEARCH_RESULT_HTML_BASE_URLS);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500269 // This tab is now entering voice search mode for the first time, or
270 // a new voice search was done.
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500271 int size = results.size();
272 if (urls == null || size != urls.size()) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500273 throw new AssertionError("improper extras passed in Intent");
274 }
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500275 if (htmls == null || htmls.size() != size || baseUrls == null ||
276 (baseUrls.size() != size && baseUrls.size() != 1)) {
277 // If either of these arrays are empty/incorrectly sized, ignore
278 // them.
279 htmls = null;
280 baseUrls = null;
281 }
282 mVoiceSearchData = new VoiceSearchData(results, urls, htmls,
283 baseUrls);
Leon Scroggins9df94972010-03-08 18:20:35 -0500284 mVoiceSearchData.mHeaders = intent.getParcelableArrayListExtra(
285 RecognizerResultsIntent
286 .EXTRA_VOICE_SEARCH_RESULT_HTTP_HEADERS);
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500287 mVoiceSearchData.mSourceIsGoogle = intent.getBooleanExtra(
288 VoiceSearchData.SOURCE_IS_GOOGLE, false);
Leon Scroggins2ee4a5a2010-03-15 16:56:57 -0400289 mVoiceSearchData.mVoiceSearchIntent = new Intent(intent);
Leon Scrogginse10dde52010-03-08 19:53:03 -0500290 }
291 String extraData = intent.getStringExtra(
292 SearchManager.EXTRA_DATA_KEY);
293 if (extraData != null) {
294 index = Integer.parseInt(extraData);
295 if (index >= mVoiceSearchData.mVoiceSearchResults.size()) {
296 throw new AssertionError("index must be less than "
297 + "size of mVoiceSearchResults");
298 }
299 if (mVoiceSearchData.mSourceIsGoogle) {
300 Intent logIntent = new Intent(
301 LoggingEvents.ACTION_LOG_EVENT);
302 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
303 LoggingEvents.VoiceSearch.N_BEST_CHOOSE);
304 logIntent.putExtra(
305 LoggingEvents.VoiceSearch.EXTRA_N_BEST_CHOOSE_INDEX,
306 index);
307 mActivity.sendBroadcast(logIntent);
308 }
309 if (mVoiceSearchData.mVoiceSearchIntent != null) {
Leon Scroggins2ee4a5a2010-03-15 16:56:57 -0400310 // Copy the Intent, so that each history item will have its own
311 // Intent, with different (or none) extra data.
312 Intent latest = new Intent(mVoiceSearchData.mVoiceSearchIntent);
313 latest.putExtra(SearchManager.EXTRA_DATA_KEY, extraData);
314 mVoiceSearchData.mVoiceSearchIntent = latest;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500315 }
316 }
317 mVoiceSearchData.mLastVoiceSearchTitle
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500318 = mVoiceSearchData.mVoiceSearchResults.get(index);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500319 if (mInForeground) {
Michael Kolb11d19782011-03-20 10:17:40 -0700320 mWebViewController.activateVoiceSearchMode(
321 mVoiceSearchData.mLastVoiceSearchTitle,
322 mVoiceSearchData.mVoiceSearchResults);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500323 }
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500324 if (mVoiceSearchData.mVoiceSearchHtmls != null) {
325 // When index was found it was already ensured that it was valid
326 String uriString = mVoiceSearchData.mVoiceSearchHtmls.get(index);
327 if (uriString != null) {
328 Uri dataUri = Uri.parse(uriString);
329 if (RecognizerResultsIntent.URI_SCHEME_INLINE.equals(
330 dataUri.getScheme())) {
331 // If there is only one base URL, use it. If there are
332 // more, there will be one for each index, so use the base
333 // URL corresponding to the index.
334 String baseUrl = mVoiceSearchData.mVoiceSearchBaseUrls.get(
335 mVoiceSearchData.mVoiceSearchBaseUrls.size() > 1 ?
336 index : 0);
337 mVoiceSearchData.mLastVoiceSearchUrl = baseUrl;
338 mMainView.loadDataWithBaseURL(baseUrl,
339 uriString.substring(RecognizerResultsIntent
340 .URI_SCHEME_INLINE.length() + 1), "text/html",
341 "utf-8", baseUrl);
342 return;
343 }
344 }
345 }
Leon Scroggins58d56c62010-01-28 15:12:40 -0500346 mVoiceSearchData.mLastVoiceSearchUrl
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500347 = mVoiceSearchData.mVoiceSearchUrls.get(index);
348 if (null == mVoiceSearchData.mLastVoiceSearchUrl) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700349 mVoiceSearchData.mLastVoiceSearchUrl = UrlUtils.smartUrlFilter(
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500350 mVoiceSearchData.mLastVoiceSearchTitle);
351 }
Leon Scroggins9df94972010-03-08 18:20:35 -0500352 Map<String, String> headers = null;
353 if (mVoiceSearchData.mHeaders != null) {
354 int bundleIndex = mVoiceSearchData.mHeaders.size() == 1 ? 0
355 : index;
356 Bundle bundle = mVoiceSearchData.mHeaders.get(bundleIndex);
357 if (bundle != null && !bundle.isEmpty()) {
358 Iterator<String> iter = bundle.keySet().iterator();
359 headers = new HashMap<String, String>();
360 while (iter.hasNext()) {
361 String key = iter.next();
362 headers.put(key, bundle.getString(key));
363 }
364 }
365 }
366 mMainView.loadUrl(mVoiceSearchData.mLastVoiceSearchUrl, headers);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500367 }
368 /* package */ static class VoiceSearchData {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500369 public VoiceSearchData(ArrayList<String> results,
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500370 ArrayList<String> urls, ArrayList<String> htmls,
371 ArrayList<String> baseUrls) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500372 mVoiceSearchResults = results;
373 mVoiceSearchUrls = urls;
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500374 mVoiceSearchHtmls = htmls;
375 mVoiceSearchBaseUrls = baseUrls;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500376 }
377 /*
378 * ArrayList of suggestions to be displayed when opening the
379 * SearchManager
380 */
381 public ArrayList<String> mVoiceSearchResults;
382 /*
383 * ArrayList of urls, associated with the suggestions in
384 * mVoiceSearchResults.
385 */
386 public ArrayList<String> mVoiceSearchUrls;
387 /*
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500388 * ArrayList holding content to load for each item in
389 * mVoiceSearchResults.
390 */
391 public ArrayList<String> mVoiceSearchHtmls;
392 /*
393 * ArrayList holding base urls for the items in mVoiceSearchResults.
394 * If non null, this will either have the same size as
395 * mVoiceSearchResults or have a size of 1, in which case all will use
396 * the same base url
397 */
398 public ArrayList<String> mVoiceSearchBaseUrls;
399 /*
Leon Scroggins58d56c62010-01-28 15:12:40 -0500400 * The last url provided by voice search. Used for comparison to see if
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500401 * we are going to a page by some method besides voice search.
Leon Scroggins58d56c62010-01-28 15:12:40 -0500402 */
403 public String mLastVoiceSearchUrl;
404 /**
405 * The last title used for voice search. Needed to update the title bar
406 * when switching tabs.
407 */
408 public String mLastVoiceSearchTitle;
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500409 /**
410 * Whether the Intent which turned on voice search mode contained the
411 * String signifying that Google was the source.
412 */
413 public boolean mSourceIsGoogle;
414 /**
Leon Scroggins9df94972010-03-08 18:20:35 -0500415 * List of headers to be passed into the WebView containing location
416 * information
417 */
418 public ArrayList<Bundle> mHeaders;
419 /**
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500420 * The Intent used to invoke voice search. Placed on the
421 * WebHistoryItem so that when coming back to a previous voice search
422 * page we can again activate voice search.
423 */
Leon Scrogginse10dde52010-03-08 19:53:03 -0500424 public Intent mVoiceSearchIntent;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500425 /**
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500426 * String used to identify Google as the source of voice search.
427 */
428 public static String SOURCE_IS_GOOGLE
429 = "android.speech.extras.SOURCE_IS_GOOGLE";
Leon Scroggins58d56c62010-01-28 15:12:40 -0500430 }
431
Grace Kloba22ac16e2009-10-07 18:00:23 -0700432 // Container class for the next error dialog that needs to be displayed
433 private class ErrorDialog {
434 public final int mTitle;
435 public final String mDescription;
436 public final int mError;
437 ErrorDialog(int title, String desc, int error) {
438 mTitle = title;
439 mDescription = desc;
440 mError = error;
441 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700442 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700443
444 private void processNextError() {
445 if (mQueuedErrors == null) {
446 return;
447 }
448 // The first one is currently displayed so just remove it.
449 mQueuedErrors.removeFirst();
450 if (mQueuedErrors.size() == 0) {
451 mQueuedErrors = null;
452 return;
453 }
454 showError(mQueuedErrors.getFirst());
455 }
456
457 private DialogInterface.OnDismissListener mDialogListener =
458 new DialogInterface.OnDismissListener() {
459 public void onDismiss(DialogInterface d) {
460 processNextError();
461 }
462 };
463 private LinkedList<ErrorDialog> mQueuedErrors;
464
465 private void queueError(int err, String desc) {
466 if (mQueuedErrors == null) {
467 mQueuedErrors = new LinkedList<ErrorDialog>();
468 }
469 for (ErrorDialog d : mQueuedErrors) {
470 if (d.mError == err) {
471 // Already saw a similar error, ignore the new one.
472 return;
473 }
474 }
475 ErrorDialog errDialog = new ErrorDialog(
476 err == WebViewClient.ERROR_FILE_NOT_FOUND ?
477 R.string.browserFrameFileErrorLabel :
478 R.string.browserFrameNetworkErrorLabel,
479 desc, err);
480 mQueuedErrors.addLast(errDialog);
481
482 // Show the dialog now if the queue was empty and it is in foreground
483 if (mQueuedErrors.size() == 1 && mInForeground) {
484 showError(errDialog);
485 }
486 }
487
488 private void showError(ErrorDialog errDialog) {
489 if (mInForeground) {
490 AlertDialog d = new AlertDialog.Builder(mActivity)
491 .setTitle(errDialog.mTitle)
492 .setMessage(errDialog.mDescription)
493 .setPositiveButton(R.string.ok, null)
494 .create();
495 d.setOnDismissListener(mDialogListener);
496 d.show();
497 }
498 }
499
500 // -------------------------------------------------------------------------
501 // WebViewClient implementation for the main WebView
502 // -------------------------------------------------------------------------
503
504 private final WebViewClient mWebViewClient = new WebViewClient() {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500505 private Message mDontResend;
506 private Message mResend;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700507 @Override
508 public void onPageStarted(WebView view, String url, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700509 mInPageLoad = true;
John Reck30c714c2010-12-16 17:30:34 -0800510 mPageLoadProgress = 0;
511 mCurrentState = new PageState(mActivity,
512 view.isPrivateBrowsingEnabled(), url, favicon);
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000513 mLoadStartTime = SystemClock.uptimeMillis();
Leon Scroggins58d56c62010-01-28 15:12:40 -0500514 if (mVoiceSearchData != null
515 && !url.equals(mVoiceSearchData.mLastVoiceSearchUrl)) {
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500516 if (mVoiceSearchData.mSourceIsGoogle) {
517 Intent i = new Intent(LoggingEvents.ACTION_LOG_EVENT);
518 i.putExtra(LoggingEvents.EXTRA_FLUSH, true);
519 mActivity.sendBroadcast(i);
520 }
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400521 revertVoiceSearchMode();
Leon Scroggins58d56c62010-01-28 15:12:40 -0500522 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700523
Grace Kloba22ac16e2009-10-07 18:00:23 -0700524
525 // If we start a touch icon load and then load a new page, we don't
526 // want to cancel the current touch icon loader. But, we do want to
527 // create a new one when the touch icon url is known.
528 if (mTouchIconLoader != null) {
529 mTouchIconLoader.mTab = null;
530 mTouchIconLoader = null;
531 }
532
533 // reset the error console
534 if (mErrorConsole != null) {
535 mErrorConsole.clearErrorMessages();
Michael Kolb8233fac2010-10-26 16:08:53 -0700536 if (mWebViewController.shouldShowErrorConsole()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700537 mErrorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
538 }
539 }
540
Patrick Scott92066772011-03-10 08:46:27 -0500541 // Cancel the auto-login process.
542 if (mDeviceAccountLogin != null) {
543 mDeviceAccountLogin.cancel();
544 mDeviceAccountLogin = null;
545 mWebViewController.hideAutoLogin(Tab.this);
546 }
547
Grace Kloba22ac16e2009-10-07 18:00:23 -0700548 // finally update the UI in the activity if it is in the foreground
John Reck324d4402011-01-11 16:56:42 -0800549 mWebViewController.onPageStarted(Tab.this, view, favicon);
Leon Scroggins4cd97792010-12-03 15:31:56 -0500550
John Recke969cc52010-12-21 17:24:43 -0800551 updateBookmarkedStatus();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700552 }
553
554 @Override
555 public void onPageFinished(WebView view, String url) {
John Reck5b691842010-11-29 11:21:13 -0800556 if (!isPrivateBrowsingEnabled()) {
557 LogTag.logPageFinishedLoading(
558 url, SystemClock.uptimeMillis() - mLoadStartTime);
559 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700560 mInPageLoad = false;
John Reck30c714c2010-12-16 17:30:34 -0800561 // Sync state (in case of stop/timeout)
562 mCurrentState.mUrl = view.getUrl();
John Reck6c702ee2011-01-07 09:41:53 -0800563 if (mCurrentState.mUrl == null) {
564 mCurrentState.mUrl = url != null ? url : "";
565 }
John Reck30c714c2010-12-16 17:30:34 -0800566 mCurrentState.mTitle = view.getTitle();
567 mCurrentState.mFavicon = view.getFavicon();
568 if (!URLUtil.isHttpsUrl(mCurrentState.mUrl)) {
569 // In case we stop when loading an HTTPS page from an HTTP page
570 // but before a provisional load occurred
571 mCurrentState.mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
572 }
John Reck324d4402011-01-11 16:56:42 -0800573 mWebViewController.onPageFinished(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700574 }
575
576 // return true if want to hijack the url to let another app to handle it
577 @Override
578 public boolean shouldOverrideUrlLoading(WebView view, String url) {
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400579 if (voiceSearchSourceIsGoogle()) {
580 // This method is called when the user clicks on a link.
581 // VoiceSearchMode is turned off when the user leaves the
582 // Google results page, so at this point the user must be on
583 // that page. If the user clicked a link on that page, assume
584 // that the voice search was effective, and broadcast an Intent
585 // so a receiver can take note of that fact.
586 Intent logIntent = new Intent(LoggingEvents.ACTION_LOG_EVENT);
587 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
588 LoggingEvents.VoiceSearch.RESULT_CLICKED);
589 mActivity.sendBroadcast(logIntent);
590 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700591 if (mInForeground) {
Michael Kolb18eb3772010-12-10 14:29:51 -0800592 return mWebViewController.shouldOverrideUrlLoading(Tab.this,
593 view, url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700594 } else {
595 return false;
596 }
597 }
598
599 /**
600 * Updates the lock icon. This method is called when we discover another
601 * resource to be loaded for this page (for example, javascript). While
602 * we update the icon type, we do not update the lock icon itself until
603 * we are done loading, it is slightly more secure this way.
604 */
605 @Override
606 public void onLoadResource(WebView view, String url) {
607 if (url != null && url.length() > 0) {
608 // It is only if the page claims to be secure that we may have
609 // to update the lock:
John Reck30c714c2010-12-16 17:30:34 -0800610 if (mCurrentState.mLockIcon == LockIcon.LOCK_ICON_SECURE) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700611 // If NOT a 'safe' url, change the lock to mixed content!
612 if (!(URLUtil.isHttpsUrl(url) || URLUtil.isDataUrl(url)
613 || URLUtil.isAboutUrl(url))) {
John Reck30c714c2010-12-16 17:30:34 -0800614 mCurrentState.mLockIcon = LockIcon.LOCK_ICON_MIXED;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700615 }
616 }
617 }
618 }
619
620 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700621 * Show a dialog informing the user of the network error reported by
622 * WebCore if it is in the foreground.
623 */
624 @Override
625 public void onReceivedError(WebView view, int errorCode,
626 String description, String failingUrl) {
627 if (errorCode != WebViewClient.ERROR_HOST_LOOKUP &&
628 errorCode != WebViewClient.ERROR_CONNECT &&
629 errorCode != WebViewClient.ERROR_BAD_URL &&
630 errorCode != WebViewClient.ERROR_UNSUPPORTED_SCHEME &&
631 errorCode != WebViewClient.ERROR_FILE) {
632 queueError(errorCode, description);
633 }
Jeff Hamilton47654f42010-09-07 09:57:51 -0500634
635 // Don't log URLs when in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -0700636 if (!isPrivateBrowsingEnabled()) {
Jeff Hamilton47654f42010-09-07 09:57:51 -0500637 Log.e(LOGTAG, "onReceivedError " + errorCode + " " + failingUrl
638 + " " + description);
639 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700640 }
641
642 /**
643 * Check with the user if it is ok to resend POST data as the page they
644 * are trying to navigate to is the result of a POST.
645 */
646 @Override
647 public void onFormResubmission(WebView view, final Message dontResend,
648 final Message resend) {
649 if (!mInForeground) {
650 dontResend.sendToTarget();
651 return;
652 }
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500653 if (mDontResend != null) {
654 Log.w(LOGTAG, "onFormResubmission should not be called again "
655 + "while dialog is still up");
656 dontResend.sendToTarget();
657 return;
658 }
659 mDontResend = dontResend;
660 mResend = resend;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700661 new AlertDialog.Builder(mActivity).setTitle(
662 R.string.browserFrameFormResubmitLabel).setMessage(
663 R.string.browserFrameFormResubmitMessage)
664 .setPositiveButton(R.string.ok,
665 new DialogInterface.OnClickListener() {
666 public void onClick(DialogInterface dialog,
667 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500668 if (mResend != null) {
669 mResend.sendToTarget();
670 mResend = null;
671 mDontResend = null;
672 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700673 }
674 }).setNegativeButton(R.string.cancel,
675 new DialogInterface.OnClickListener() {
676 public void onClick(DialogInterface dialog,
677 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500678 if (mDontResend != null) {
679 mDontResend.sendToTarget();
680 mResend = null;
681 mDontResend = null;
682 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700683 }
684 }).setOnCancelListener(new OnCancelListener() {
685 public void onCancel(DialogInterface dialog) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500686 if (mDontResend != null) {
687 mDontResend.sendToTarget();
688 mResend = null;
689 mDontResend = null;
690 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700691 }
692 }).show();
693 }
694
695 /**
696 * Insert the url into the visited history database.
697 * @param url The url to be inserted.
698 * @param isReload True if this url is being reloaded.
699 * FIXME: Not sure what to do when reloading the page.
700 */
701 @Override
702 public void doUpdateVisitedHistory(WebView view, String url,
703 boolean isReload) {
John Reck324d4402011-01-11 16:56:42 -0800704 mWebViewController.doUpdateVisitedHistory(Tab.this, isReload);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700705 }
706
707 /**
708 * Displays SSL error(s) dialog to the user.
709 */
710 @Override
711 public void onReceivedSslError(final WebView view,
712 final SslErrorHandler handler, final SslError error) {
713 if (!mInForeground) {
714 handler.cancel();
John Reck30c714c2010-12-16 17:30:34 -0800715 setLockIconType(LockIcon.LOCK_ICON_UNSECURE);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700716 return;
717 }
John Reck35e9dd62011-04-25 09:01:54 -0700718 if (mSettings.showSecurityWarnings()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700719 final LayoutInflater factory =
720 LayoutInflater.from(mActivity);
721 final View warningsView =
722 factory.inflate(R.layout.ssl_warnings, null);
723 final LinearLayout placeholder =
724 (LinearLayout)warningsView.findViewById(R.id.placeholder);
725
726 if (error.hasError(SslError.SSL_UNTRUSTED)) {
727 LinearLayout ll = (LinearLayout)factory
728 .inflate(R.layout.ssl_warning, null);
729 ((TextView)ll.findViewById(R.id.warning))
730 .setText(R.string.ssl_untrusted);
731 placeholder.addView(ll);
732 }
733
734 if (error.hasError(SslError.SSL_IDMISMATCH)) {
735 LinearLayout ll = (LinearLayout)factory
736 .inflate(R.layout.ssl_warning, null);
737 ((TextView)ll.findViewById(R.id.warning))
738 .setText(R.string.ssl_mismatch);
739 placeholder.addView(ll);
740 }
741
742 if (error.hasError(SslError.SSL_EXPIRED)) {
743 LinearLayout ll = (LinearLayout)factory
744 .inflate(R.layout.ssl_warning, null);
745 ((TextView)ll.findViewById(R.id.warning))
746 .setText(R.string.ssl_expired);
747 placeholder.addView(ll);
748 }
749
750 if (error.hasError(SslError.SSL_NOTYETVALID)) {
751 LinearLayout ll = (LinearLayout)factory
752 .inflate(R.layout.ssl_warning, null);
753 ((TextView)ll.findViewById(R.id.warning))
754 .setText(R.string.ssl_not_yet_valid);
755 placeholder.addView(ll);
756 }
757
758 new AlertDialog.Builder(mActivity).setTitle(
759 R.string.security_warning).setIcon(
760 android.R.drawable.ic_dialog_alert).setView(
761 warningsView).setPositiveButton(R.string.ssl_continue,
762 new DialogInterface.OnClickListener() {
763 public void onClick(DialogInterface dialog,
764 int whichButton) {
765 handler.proceed();
766 }
767 }).setNeutralButton(R.string.view_certificate,
768 new DialogInterface.OnClickListener() {
769 public void onClick(DialogInterface dialog,
770 int whichButton) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700771 mWebViewController.showSslCertificateOnError(view,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700772 handler, error);
773 }
Ben Murdocha49b8292010-11-16 11:56:04 +0000774 }).setNegativeButton(R.string.ssl_go_back,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700775 new DialogInterface.OnClickListener() {
776 public void onClick(DialogInterface dialog,
777 int whichButton) {
John Reck30c714c2010-12-16 17:30:34 -0800778 dialog.cancel();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700779 }
780 }).setOnCancelListener(
781 new DialogInterface.OnCancelListener() {
782 public void onCancel(DialogInterface dialog) {
783 handler.cancel();
John Reck30c714c2010-12-16 17:30:34 -0800784 setLockIconType(LockIcon.LOCK_ICON_UNSECURE);
785 mWebViewController.onUserCanceledSsl(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700786 }
787 }).show();
788 } else {
789 handler.proceed();
790 }
791 }
792
793 /**
794 * Handles an HTTP authentication request.
795 *
796 * @param handler The authentication handler
797 * @param host The host
798 * @param realm The realm
799 */
800 @Override
801 public void onReceivedHttpAuthRequest(WebView view,
802 final HttpAuthHandler handler, final String host,
803 final String realm) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700804 mWebViewController.onReceivedHttpAuthRequest(Tab.this, view, handler, host, realm);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700805 }
806
807 @Override
John Reck438bf462011-01-12 18:11:46 -0800808 public WebResourceResponse shouldInterceptRequest(WebView view,
809 String url) {
810 WebResourceResponse res = HomeProvider.shouldInterceptRequest(
811 mActivity, url);
812 return res;
813 }
814
815 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700816 public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
817 if (!mInForeground) {
818 return false;
819 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700820 return mWebViewController.shouldOverrideKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700821 }
822
823 @Override
824 public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700825 if (!mInForeground) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700826 return;
827 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700828 mWebViewController.onUnhandledKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700829 }
Patrick Scott92066772011-03-10 08:46:27 -0500830
831 @Override
832 public void onReceivedLoginRequest(WebView view, String realm,
833 String account, String args) {
834 new DeviceAccountLogin(mActivity, view, Tab.this, mWebViewController)
835 .handleLogin(realm, account, args);
836 }
837
Grace Kloba22ac16e2009-10-07 18:00:23 -0700838 };
839
Patrick Scott92066772011-03-10 08:46:27 -0500840 // Called by DeviceAccountLogin when the Tab needs to have the auto-login UI
841 // displayed.
842 void setDeviceAccountLogin(DeviceAccountLogin login) {
843 mDeviceAccountLogin = login;
844 }
845
846 // Returns non-null if the title bar should display the auto-login UI.
847 DeviceAccountLogin getDeviceAccountLogin() {
848 return mDeviceAccountLogin;
849 }
850
Grace Kloba22ac16e2009-10-07 18:00:23 -0700851 // -------------------------------------------------------------------------
852 // WebChromeClient implementation for the main WebView
853 // -------------------------------------------------------------------------
854
855 private final WebChromeClient mWebChromeClient = new WebChromeClient() {
856 // Helper method to create a new tab or sub window.
857 private void createWindow(final boolean dialog, final Message msg) {
858 WebView.WebViewTransport transport =
859 (WebView.WebViewTransport) msg.obj;
860 if (dialog) {
861 createSubWindow();
Michael Kolb8233fac2010-10-26 16:08:53 -0700862 mWebViewController.attachSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700863 transport.setWebView(mSubView);
864 } else {
Michael Kolb7bcafde2011-05-09 13:55:59 -0700865 final Tab newTab = mWebViewController.openTab(null,
John Reck5949c662011-05-27 09:52:29 -0700866 Tab.this, true, true);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700867 transport.setWebView(newTab.getWebView());
868 }
869 msg.sendToTarget();
870 }
871
872 @Override
873 public boolean onCreateWindow(WebView view, final boolean dialog,
874 final boolean userGesture, final Message resultMsg) {
875 // only allow new window or sub window for the foreground case
876 if (!mInForeground) {
877 return false;
878 }
879 // Short-circuit if we can't create any more tabs or sub windows.
880 if (dialog && mSubView != null) {
881 new AlertDialog.Builder(mActivity)
882 .setTitle(R.string.too_many_subwindows_dialog_title)
883 .setIcon(android.R.drawable.ic_dialog_alert)
884 .setMessage(R.string.too_many_subwindows_dialog_message)
885 .setPositiveButton(R.string.ok, null)
886 .show();
887 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700888 } else if (!mWebViewController.getTabControl().canCreateNewTab()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700889 new AlertDialog.Builder(mActivity)
890 .setTitle(R.string.too_many_windows_dialog_title)
891 .setIcon(android.R.drawable.ic_dialog_alert)
892 .setMessage(R.string.too_many_windows_dialog_message)
893 .setPositiveButton(R.string.ok, null)
894 .show();
895 return false;
896 }
897
898 // Short-circuit if this was a user gesture.
899 if (userGesture) {
900 createWindow(dialog, resultMsg);
901 return true;
902 }
903
904 // Allow the popup and create the appropriate window.
905 final AlertDialog.OnClickListener allowListener =
906 new AlertDialog.OnClickListener() {
907 public void onClick(DialogInterface d,
908 int which) {
909 createWindow(dialog, resultMsg);
910 }
911 };
912
913 // Block the popup by returning a null WebView.
914 final AlertDialog.OnClickListener blockListener =
915 new AlertDialog.OnClickListener() {
916 public void onClick(DialogInterface d, int which) {
917 resultMsg.sendToTarget();
918 }
919 };
920
921 // Build a confirmation dialog to display to the user.
922 final AlertDialog d =
923 new AlertDialog.Builder(mActivity)
924 .setTitle(R.string.attention)
925 .setIcon(android.R.drawable.ic_dialog_alert)
926 .setMessage(R.string.popup_window_attempt)
927 .setPositiveButton(R.string.allow, allowListener)
928 .setNegativeButton(R.string.block, blockListener)
929 .setCancelable(false)
930 .create();
931
932 // Show the confirmation dialog.
933 d.show();
934 return true;
935 }
936
937 @Override
Patrick Scotteb5061b2009-11-18 15:00:30 -0500938 public void onRequestFocus(WebView view) {
939 if (!mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700940 mWebViewController.switchToTab(Tab.this);
Patrick Scotteb5061b2009-11-18 15:00:30 -0500941 }
942 }
943
944 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700945 public void onCloseWindow(WebView window) {
Michael Kolbc831b632011-05-11 09:30:34 -0700946 if (mParent != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700947 // JavaScript can only close popup window.
948 if (mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700949 mWebViewController.switchToTab(mParent);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700950 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700951 mWebViewController.closeTab(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700952 }
953 }
954
955 @Override
956 public void onProgressChanged(WebView view, int newProgress) {
John Reck30c714c2010-12-16 17:30:34 -0800957 mPageLoadProgress = newProgress;
958 mWebViewController.onProgressChanged(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700959 }
960
961 @Override
Leon Scroggins21d9b902010-03-11 09:33:11 -0500962 public void onReceivedTitle(WebView view, final String title) {
John Reck30c714c2010-12-16 17:30:34 -0800963 mCurrentState.mTitle = title;
Michael Kolb8233fac2010-10-26 16:08:53 -0700964 mWebViewController.onReceivedTitle(Tab.this, title);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700965 }
966
967 @Override
968 public void onReceivedIcon(WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -0800969 mCurrentState.mFavicon = icon;
Michael Kolb8233fac2010-10-26 16:08:53 -0700970 mWebViewController.onFavicon(Tab.this, view, icon);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700971 }
972
973 @Override
974 public void onReceivedTouchIconUrl(WebView view, String url,
975 boolean precomposed) {
976 final ContentResolver cr = mActivity.getContentResolver();
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400977 // Let precomposed icons take precedence over non-composed
978 // icons.
979 if (precomposed && mTouchIconLoader != null) {
980 mTouchIconLoader.cancel(false);
981 mTouchIconLoader = null;
982 }
983 // Have only one async task at a time.
984 if (mTouchIconLoader == null) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700985 mTouchIconLoader = new DownloadTouchIcon(Tab.this,
986 mActivity, cr, view);
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400987 mTouchIconLoader.execute(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700988 }
989 }
990
991 @Override
992 public void onShowCustomView(View view,
993 WebChromeClient.CustomViewCallback callback) {
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -0400994 onShowCustomView(view, mActivity.getRequestedOrientation(), callback);
995 }
996
997 @Override
998 public void onShowCustomView(View view, int requestedOrientation,
999 WebChromeClient.CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001000 if (mInForeground) mWebViewController.showCustomView(Tab.this, view,
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001001 requestedOrientation, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001002 }
1003
1004 @Override
1005 public void onHideCustomView() {
Michael Kolb8233fac2010-10-26 16:08:53 -07001006 if (mInForeground) mWebViewController.hideCustomView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001007 }
1008
1009 /**
1010 * The origin has exceeded its database quota.
1011 * @param url the URL that exceeded the quota
1012 * @param databaseIdentifier the identifier of the database on which the
1013 * transaction that caused the quota overflow was run
1014 * @param currentQuota the current quota for the origin.
1015 * @param estimatedSize the estimated size of the database.
1016 * @param totalUsedQuota is the sum of all origins' quota.
1017 * @param quotaUpdater The callback to run when a decision to allow or
1018 * deny quota has been made. Don't forget to call this!
1019 */
1020 @Override
1021 public void onExceededDatabaseQuota(String url,
1022 String databaseIdentifier, long currentQuota, long estimatedSize,
1023 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -07001024 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001025 .onExceededDatabaseQuota(url, databaseIdentifier,
1026 currentQuota, estimatedSize, totalUsedQuota,
1027 quotaUpdater);
1028 }
1029
1030 /**
1031 * The Application Cache has exceeded its max size.
1032 * @param spaceNeeded is the amount of disk space that would be needed
1033 * in order for the last appcache operation to succeed.
1034 * @param totalUsedQuota is the sum of all origins' quota.
1035 * @param quotaUpdater A callback to inform the WebCore thread that a
1036 * new app cache size is available. This callback must always
1037 * be executed at some point to ensure that the sleeping
1038 * WebCore thread is woken up.
1039 */
1040 @Override
1041 public void onReachedMaxAppCacheSize(long spaceNeeded,
1042 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -07001043 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001044 .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota,
1045 quotaUpdater);
1046 }
1047
1048 /**
1049 * Instructs the browser to show a prompt to ask the user to set the
1050 * Geolocation permission state for the specified origin.
1051 * @param origin The origin for which Geolocation permissions are
1052 * requested.
1053 * @param callback The callback to call once the user has set the
1054 * Geolocation permission state.
1055 */
1056 @Override
1057 public void onGeolocationPermissionsShowPrompt(String origin,
1058 GeolocationPermissions.Callback callback) {
1059 if (mInForeground) {
Grace Kloba50c241e2010-04-20 11:07:50 -07001060 getGeolocationPermissionsPrompt().show(origin, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001061 }
1062 }
1063
1064 /**
1065 * Instructs the browser to hide the Geolocation permissions prompt.
1066 */
1067 @Override
1068 public void onGeolocationPermissionsHidePrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001069 if (mInForeground && mGeolocationPermissionsPrompt != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001070 mGeolocationPermissionsPrompt.hide();
1071 }
1072 }
1073
Ben Murdoch65acc352009-11-19 18:16:04 +00001074 /* Adds a JavaScript error message to the system log and if the JS
1075 * console is enabled in the about:debug options, to that console
1076 * also.
Ben Murdochc42addf2010-01-28 15:19:59 +00001077 * @param consoleMessage the message object.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001078 */
1079 @Override
Ben Murdochc42addf2010-01-28 15:19:59 +00001080 public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001081 if (mInForeground) {
1082 // call getErrorConsole(true) so it will create one if needed
1083 ErrorConsoleView errorConsole = getErrorConsole(true);
Ben Murdochc42addf2010-01-28 15:19:59 +00001084 errorConsole.addErrorMessage(consoleMessage);
Michael Kolb8233fac2010-10-26 16:08:53 -07001085 if (mWebViewController.shouldShowErrorConsole()
1086 && errorConsole.getShowState() !=
1087 ErrorConsoleView.SHOW_MAXIMIZED) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001088 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
1089 }
1090 }
Ben Murdochc42addf2010-01-28 15:19:59 +00001091
Jeff Hamilton47654f42010-09-07 09:57:51 -05001092 // Don't log console messages in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001093 if (isPrivateBrowsingEnabled()) return true;
Jeff Hamilton47654f42010-09-07 09:57:51 -05001094
Ben Murdochc42addf2010-01-28 15:19:59 +00001095 String message = "Console: " + consoleMessage.message() + " "
1096 + consoleMessage.sourceId() + ":"
1097 + consoleMessage.lineNumber();
1098
1099 switch (consoleMessage.messageLevel()) {
1100 case TIP:
1101 Log.v(CONSOLE_LOGTAG, message);
1102 break;
1103 case LOG:
1104 Log.i(CONSOLE_LOGTAG, message);
1105 break;
1106 case WARNING:
1107 Log.w(CONSOLE_LOGTAG, message);
1108 break;
1109 case ERROR:
1110 Log.e(CONSOLE_LOGTAG, message);
1111 break;
1112 case DEBUG:
1113 Log.d(CONSOLE_LOGTAG, message);
1114 break;
1115 }
1116
1117 return true;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001118 }
1119
1120 /**
1121 * Ask the browser for an icon to represent a <video> element.
1122 * This icon will be used if the Web page did not specify a poster attribute.
1123 * @return Bitmap The icon or null if no such icon is available.
1124 */
1125 @Override
1126 public Bitmap getDefaultVideoPoster() {
1127 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001128 return mWebViewController.getDefaultVideoPoster();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001129 }
1130 return null;
1131 }
1132
1133 /**
1134 * Ask the host application for a custom progress view to show while
1135 * a <video> is loading.
1136 * @return View The progress view.
1137 */
1138 @Override
1139 public View getVideoLoadingProgressView() {
1140 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001141 return mWebViewController.getVideoLoadingProgressView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001142 }
1143 return null;
1144 }
1145
1146 @Override
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01001147 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001148 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001149 mWebViewController.openFileChooser(uploadMsg, acceptType);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001150 } else {
1151 uploadMsg.onReceiveValue(null);
1152 }
1153 }
1154
1155 /**
1156 * Deliver a list of already-visited URLs
1157 */
1158 @Override
1159 public void getVisitedHistory(final ValueCallback<String[]> callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001160 mWebViewController.getVisitedHistory(callback);
1161 }
Ben Murdoch8029a772010-11-16 11:58:21 +00001162
1163 @Override
1164 public void setupAutoFill(Message message) {
1165 // Prompt the user to set up their profile.
1166 final Message msg = message;
1167 AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
Ben Murdoch1d676b62011-01-17 12:54:24 +00001168 LayoutInflater inflater = (LayoutInflater) mActivity.getSystemService(
1169 Context.LAYOUT_INFLATER_SERVICE);
1170 final View layout = inflater.inflate(R.layout.setup_autofill_dialog, null);
1171
1172 builder.setView(layout)
1173 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1174 @Override
1175 public void onClick(DialogInterface dialog, int id) {
1176 CheckBox disableAutoFill = (CheckBox) layout.findViewById(
1177 R.id.setup_autofill_dialog_disable_autofill);
1178
1179 if (disableAutoFill.isChecked()) {
1180 // Disable autofill and show a toast with how to turn it on again.
John Reck35e9dd62011-04-25 09:01:54 -07001181 mSettings.setAutofillEnabled(false);
Ben Murdoch1d676b62011-01-17 12:54:24 +00001182 Toast.makeText(mActivity,
1183 R.string.autofill_setup_dialog_negative_toast,
1184 Toast.LENGTH_LONG).show();
1185 } else {
1186 // Take user to the AutoFill profile editor. When they return,
1187 // we will send the message that we pass here which will trigger
1188 // the form to get filled out with their new profile.
1189 mWebViewController.setupAutoFill(msg);
1190 }
1191 }
1192 })
1193 .setNegativeButton(R.string.cancel, null)
1194 .show();
Ben Murdoch8029a772010-11-16 11:58:21 +00001195 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001196 };
1197
1198 // -------------------------------------------------------------------------
1199 // WebViewClient implementation for the sub window
1200 // -------------------------------------------------------------------------
1201
1202 // Subclass of WebViewClient used in subwindows to notify the main
1203 // WebViewClient of certain WebView activities.
1204 private static class SubWindowClient extends WebViewClient {
1205 // The main WebViewClient.
1206 private final WebViewClient mClient;
Michael Kolb8233fac2010-10-26 16:08:53 -07001207 private final WebViewController mController;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001208
Michael Kolb8233fac2010-10-26 16:08:53 -07001209 SubWindowClient(WebViewClient client, WebViewController controller) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001210 mClient = client;
Michael Kolb8233fac2010-10-26 16:08:53 -07001211 mController = controller;
Leon Scroggins III211ba542010-04-19 13:21:13 -04001212 }
1213 @Override
1214 public void onPageStarted(WebView view, String url, Bitmap favicon) {
1215 // Unlike the others, do not call mClient's version, which would
1216 // change the progress bar. However, we do want to remove the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001217 // find or select dialog.
Michael Kolb8233fac2010-10-26 16:08:53 -07001218 mController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001219 }
1220 @Override
1221 public void doUpdateVisitedHistory(WebView view, String url,
1222 boolean isReload) {
1223 mClient.doUpdateVisitedHistory(view, url, isReload);
1224 }
1225 @Override
1226 public boolean shouldOverrideUrlLoading(WebView view, String url) {
1227 return mClient.shouldOverrideUrlLoading(view, url);
1228 }
1229 @Override
1230 public void onReceivedSslError(WebView view, SslErrorHandler handler,
1231 SslError error) {
1232 mClient.onReceivedSslError(view, handler, error);
1233 }
1234 @Override
1235 public void onReceivedHttpAuthRequest(WebView view,
1236 HttpAuthHandler handler, String host, String realm) {
1237 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
1238 }
1239 @Override
1240 public void onFormResubmission(WebView view, Message dontResend,
1241 Message resend) {
1242 mClient.onFormResubmission(view, dontResend, resend);
1243 }
1244 @Override
1245 public void onReceivedError(WebView view, int errorCode,
1246 String description, String failingUrl) {
1247 mClient.onReceivedError(view, errorCode, description, failingUrl);
1248 }
1249 @Override
1250 public boolean shouldOverrideKeyEvent(WebView view,
1251 android.view.KeyEvent event) {
1252 return mClient.shouldOverrideKeyEvent(view, event);
1253 }
1254 @Override
1255 public void onUnhandledKeyEvent(WebView view,
1256 android.view.KeyEvent event) {
1257 mClient.onUnhandledKeyEvent(view, event);
1258 }
1259 }
1260
1261 // -------------------------------------------------------------------------
1262 // WebChromeClient implementation for the sub window
1263 // -------------------------------------------------------------------------
1264
1265 private class SubWindowChromeClient extends WebChromeClient {
1266 // The main WebChromeClient.
1267 private final WebChromeClient mClient;
1268
1269 SubWindowChromeClient(WebChromeClient client) {
1270 mClient = client;
1271 }
1272 @Override
1273 public void onProgressChanged(WebView view, int newProgress) {
1274 mClient.onProgressChanged(view, newProgress);
1275 }
1276 @Override
1277 public boolean onCreateWindow(WebView view, boolean dialog,
1278 boolean userGesture, android.os.Message resultMsg) {
1279 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
1280 }
1281 @Override
1282 public void onCloseWindow(WebView window) {
1283 if (window != mSubView) {
1284 Log.e(LOGTAG, "Can't close the window");
1285 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001286 mWebViewController.dismissSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001287 }
1288 }
1289
1290 // -------------------------------------------------------------------------
1291
Michael Kolb8233fac2010-10-26 16:08:53 -07001292 // TODO temporarily use activity here
1293 // remove later
1294
Grace Kloba22ac16e2009-10-07 18:00:23 -07001295 // Construct a new tab
Michael Kolb7bcafde2011-05-09 13:55:59 -07001296 Tab(WebViewController wvcontroller, WebView w) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001297 mWebViewController = wvcontroller;
1298 mActivity = mWebViewController.getActivity();
John Reck35e9dd62011-04-25 09:01:54 -07001299 mSettings = BrowserSettings.getInstance();
John Recke969cc52010-12-21 17:24:43 -08001300 mDataController = DataController.getInstance(mActivity);
John Reck847b5322011-04-14 17:02:18 -07001301 mCurrentState = new PageState(mActivity, w != null
1302 ? w.isPrivateBrowsingEnabled() : false);
Michael Kolb8233fac2010-10-26 16:08:53 -07001303 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001304 mInForeground = false;
1305
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001306 mDownloadListener = new DownloadListener() {
1307 public void onDownloadStart(String url, String userAgent,
1308 String contentDisposition, String mimetype,
1309 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001310 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001311 mimetype, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001312 }
1313 };
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001314 mWebBackForwardListClient = new WebBackForwardListClient() {
1315 @Override
1316 public void onNewHistoryItem(WebHistoryItem item) {
1317 if (isInVoiceSearchMode()) {
1318 item.setCustomData(mVoiceSearchData.mVoiceSearchIntent);
1319 }
1320 }
1321 @Override
1322 public void onIndexChanged(WebHistoryItem item, int index) {
1323 Object data = item.getCustomData();
1324 if (data != null && data instanceof Intent) {
1325 activateVoiceSearchMode((Intent) data);
1326 }
1327 }
1328 };
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001329
Grace Kloba22ac16e2009-10-07 18:00:23 -07001330 setWebView(w);
1331 }
1332
Michael Kolbc831b632011-05-11 09:30:34 -07001333 public void setId(long id) {
1334 mId = id;
1335 }
1336
1337 public long getId() {
1338 return mId;
1339 }
1340
Grace Kloba22ac16e2009-10-07 18:00:23 -07001341 /**
1342 * Sets the WebView for this tab, correctly removing the old WebView from
1343 * the container view.
1344 */
1345 void setWebView(WebView w) {
1346 if (mMainView == w) {
1347 return;
1348 }
Michael Kolba713ec82010-11-29 17:27:06 -08001349
Grace Kloba22ac16e2009-10-07 18:00:23 -07001350 // If the WebView is changing, the page will be reloaded, so any ongoing
1351 // Geolocation permission requests are void.
Grace Kloba50c241e2010-04-20 11:07:50 -07001352 if (mGeolocationPermissionsPrompt != null) {
1353 mGeolocationPermissionsPrompt.hide();
1354 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001355
Michael Kolba713ec82010-11-29 17:27:06 -08001356 mWebViewController.onSetWebView(this, w);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001357
1358 // set the new one
1359 mMainView = w;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001360 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001361 if (mMainView != null) {
1362 mMainView.setWebViewClient(mWebViewClient);
1363 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001364 // Attach DownloadManager so that downloads can start in an active
1365 // or a non-active window. This can happen when going to a site that
1366 // does a redirect after a period of time. The user could have
1367 // switched to another tab while waiting for the download to start.
1368 mMainView.setDownloadListener(mDownloadListener);
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001369 mMainView.setWebBackForwardListClient(mWebBackForwardListClient);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001370 }
1371 }
1372
1373 /**
1374 * Destroy the tab's main WebView and subWindow if any
1375 */
1376 void destroy() {
1377 if (mMainView != null) {
1378 dismissSubWindow();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001379 // save the WebView to call destroy() after detach it from the tab
1380 WebView webView = mMainView;
1381 setWebView(null);
1382 webView.destroy();
1383 }
1384 }
1385
1386 /**
1387 * Remove the tab from the parent
1388 */
1389 void removeFromTree() {
1390 // detach the children
Michael Kolbc831b632011-05-11 09:30:34 -07001391 if (mChildren != null) {
1392 for(Tab t : mChildren) {
1393 t.setParent(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001394 }
1395 }
1396 // remove itself from the parent list
Michael Kolbc831b632011-05-11 09:30:34 -07001397 if (mParent != null) {
1398 mParent.mChildren.remove(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001399 }
1400 }
1401
1402 /**
1403 * Create a new subwindow unless a subwindow already exists.
1404 * @return True if a new subwindow was created. False if one already exists.
1405 */
1406 boolean createSubWindow() {
1407 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001408 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001409 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001410 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001411 mSubView.setWebChromeClient(new SubWindowChromeClient(
1412 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001413 // Set a different DownloadListener for the mSubView, since it will
1414 // just need to dismiss the mSubView, rather than close the Tab
1415 mSubView.setDownloadListener(new DownloadListener() {
1416 public void onDownloadStart(String url, String userAgent,
1417 String contentDisposition, String mimetype,
1418 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001419 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001420 contentDisposition, mimetype, contentLength);
1421 if (mSubView.copyBackForwardList().getSize() == 0) {
1422 // This subwindow was opened for the sole purpose of
1423 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001424 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001425 }
1426 }
1427 });
Grace Kloba22ac16e2009-10-07 18:00:23 -07001428 mSubView.setOnCreateContextMenuListener(mActivity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001429 return true;
1430 }
1431 return false;
1432 }
1433
1434 /**
1435 * Dismiss the subWindow for the tab.
1436 */
1437 void dismissSubWindow() {
1438 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001439 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001440 mSubView.destroy();
1441 mSubView = null;
1442 mSubViewContainer = null;
1443 }
1444 }
1445
Grace Kloba22ac16e2009-10-07 18:00:23 -07001446
1447 /**
1448 * Set the parent tab of this tab.
1449 */
Michael Kolbc831b632011-05-11 09:30:34 -07001450 void setParent(Tab parent) {
1451 mParent = parent;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001452 // This tab may have been freed due to low memory. If that is the case,
Michael Kolbc831b632011-05-11 09:30:34 -07001453 // the parent tab id is already saved. If we are changing that id
Grace Kloba22ac16e2009-10-07 18:00:23 -07001454 // (most likely due to removing the parent tab) we must update the
Michael Kolbc831b632011-05-11 09:30:34 -07001455 // parent tab id in the saved Bundle.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001456 if (mSavedState != null) {
1457 if (parent == null) {
1458 mSavedState.remove(PARENTTAB);
1459 } else {
Michael Kolbc831b632011-05-11 09:30:34 -07001460 mSavedState.putLong(PARENTTAB, parent.getId());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001461 }
1462 }
John Reckb0a86db2011-05-24 14:05:58 -07001463
1464 // Sync the WebView useragent with the parent
1465 if (parent != null && mSettings.hasDesktopUseragent(parent.getWebView())
1466 != mSettings.hasDesktopUseragent(getWebView())) {
1467 mSettings.toggleDesktopUseragent(getWebView());
1468 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001469 }
1470
1471 /**
Michael Kolbc831b632011-05-11 09:30:34 -07001472 * If this Tab was created through another Tab, then this method returns
1473 * that Tab.
1474 * @return the Tab parent or null
1475 */
1476 public Tab getParent() {
1477 return mParent;
1478 }
1479
1480 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001481 * When a Tab is created through the content of another Tab, then we
1482 * associate the Tabs.
1483 * @param child the Tab that was created from this Tab
1484 */
1485 void addChildTab(Tab child) {
John Reck541f55a2011-06-07 16:34:43 -07001486 if (mIsSnapshot) {
1487 throw new IllegalStateException(
1488 "Snapshot tabs cannot have child tabs!");
1489 }
Michael Kolbc831b632011-05-11 09:30:34 -07001490 if (mChildren == null) {
1491 mChildren = new Vector<Tab>();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001492 }
Michael Kolbc831b632011-05-11 09:30:34 -07001493 mChildren.add(child);
1494 child.setParent(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001495 }
1496
Michael Kolbc831b632011-05-11 09:30:34 -07001497 Vector<Tab> getChildren() {
1498 return mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001499 }
1500
1501 void resume() {
1502 if (mMainView != null) {
1503 mMainView.onResume();
1504 if (mSubView != null) {
1505 mSubView.onResume();
1506 }
1507 }
1508 }
1509
1510 void pause() {
1511 if (mMainView != null) {
1512 mMainView.onPause();
1513 if (mSubView != null) {
1514 mSubView.onPause();
1515 }
1516 }
1517 }
1518
1519 void putInForeground() {
1520 mInForeground = true;
1521 resume();
1522 mMainView.setOnCreateContextMenuListener(mActivity);
1523 if (mSubView != null) {
1524 mSubView.setOnCreateContextMenuListener(mActivity);
1525 }
1526 // Show the pending error dialog if the queue is not empty
1527 if (mQueuedErrors != null && mQueuedErrors.size() > 0) {
1528 showError(mQueuedErrors.getFirst());
1529 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001530 mWebViewController.bookmarkedStatusHasChanged(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001531 }
1532
1533 void putInBackground() {
1534 mInForeground = false;
1535 pause();
1536 mMainView.setOnCreateContextMenuListener(null);
1537 if (mSubView != null) {
1538 mSubView.setOnCreateContextMenuListener(null);
1539 }
1540 }
1541
Michael Kolb8233fac2010-10-26 16:08:53 -07001542 boolean inForeground() {
1543 return mInForeground;
1544 }
1545
Grace Kloba22ac16e2009-10-07 18:00:23 -07001546 /**
1547 * Return the top window of this tab; either the subwindow if it is not
1548 * null or the main window.
1549 * @return The top window of this tab.
1550 */
1551 WebView getTopWindow() {
1552 if (mSubView != null) {
1553 return mSubView;
1554 }
1555 return mMainView;
1556 }
1557
1558 /**
1559 * Return the main window of this tab. Note: if a tab is freed in the
1560 * background, this can return null. It is only guaranteed to be
1561 * non-null for the current tab.
1562 * @return The main WebView of this tab.
1563 */
1564 WebView getWebView() {
1565 return mMainView;
1566 }
1567
Michael Kolba713ec82010-11-29 17:27:06 -08001568 void setViewContainer(View container) {
1569 mContainer = container;
1570 }
1571
Michael Kolb8233fac2010-10-26 16:08:53 -07001572 View getViewContainer() {
1573 return mContainer;
1574 }
1575
Grace Kloba22ac16e2009-10-07 18:00:23 -07001576 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001577 * Return whether private browsing is enabled for the main window of
1578 * this tab.
1579 * @return True if private browsing is enabled.
1580 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001581 boolean isPrivateBrowsingEnabled() {
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001582 WebView webView = getWebView();
1583 if (webView == null) {
1584 return false;
1585 }
1586 return webView.isPrivateBrowsingEnabled();
1587 }
1588
1589 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001590 * Return the subwindow of this tab or null if there is no subwindow.
1591 * @return The subwindow of this tab or null.
1592 */
1593 WebView getSubWebView() {
1594 return mSubView;
1595 }
1596
Michael Kolb1514bb72010-11-22 09:11:48 -08001597 void setSubWebView(WebView subView) {
1598 mSubView = subView;
1599 }
1600
Michael Kolb8233fac2010-10-26 16:08:53 -07001601 View getSubViewContainer() {
1602 return mSubViewContainer;
1603 }
1604
Michael Kolb1514bb72010-11-22 09:11:48 -08001605 void setSubViewContainer(View subViewContainer) {
1606 mSubViewContainer = subViewContainer;
1607 }
1608
Grace Kloba22ac16e2009-10-07 18:00:23 -07001609 /**
1610 * @return The geolocation permissions prompt for this tab.
1611 */
1612 GeolocationPermissionsPrompt getGeolocationPermissionsPrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001613 if (mGeolocationPermissionsPrompt == null) {
1614 ViewStub stub = (ViewStub) mContainer
1615 .findViewById(R.id.geolocation_permissions_prompt);
1616 mGeolocationPermissionsPrompt = (GeolocationPermissionsPrompt) stub
1617 .inflate();
1618 mGeolocationPermissionsPrompt.init();
1619 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001620 return mGeolocationPermissionsPrompt;
1621 }
1622
1623 /**
1624 * @return The application id string
1625 */
1626 String getAppId() {
1627 return mAppId;
1628 }
1629
1630 /**
1631 * Set the application id string
1632 * @param id
1633 */
1634 void setAppId(String id) {
1635 mAppId = id;
1636 }
1637
Grace Kloba22ac16e2009-10-07 18:00:23 -07001638 String getUrl() {
John Reck324d4402011-01-11 16:56:42 -08001639 return UrlUtils.filteredUrl(mCurrentState.mUrl);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001640 }
1641
John Reck49a603c2011-03-03 09:33:05 -08001642 String getOriginalUrl() {
1643 if (mMainView == null) {
1644 return "";
1645 }
1646 return UrlUtils.filteredUrl(mMainView.getOriginalUrl());
1647 }
1648
Grace Kloba22ac16e2009-10-07 18:00:23 -07001649 /**
John Reck30c714c2010-12-16 17:30:34 -08001650 * Get the title of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001651 */
1652 String getTitle() {
John Reck30c714c2010-12-16 17:30:34 -08001653 if (mCurrentState.mTitle == null && mInPageLoad) {
1654 return mActivity.getString(R.string.title_bar_loading);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001655 }
John Reck30c714c2010-12-16 17:30:34 -08001656 return mCurrentState.mTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001657 }
1658
1659 /**
John Reck30c714c2010-12-16 17:30:34 -08001660 * Get the favicon of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001661 */
1662 Bitmap getFavicon() {
John Reck30c714c2010-12-16 17:30:34 -08001663 return mCurrentState.mFavicon;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001664 }
1665
John Recke969cc52010-12-21 17:24:43 -08001666 public boolean isBookmarkedSite() {
1667 return mCurrentState.mIsBookmarkedSite;
1668 }
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001669
Grace Kloba22ac16e2009-10-07 18:00:23 -07001670 /**
1671 * Return the tab's error console. Creates the console if createIfNEcessary
1672 * is true and we haven't already created the console.
1673 * @param createIfNecessary Flag to indicate if the console should be
1674 * created if it has not been already.
1675 * @return The tab's error console, or null if one has not been created and
1676 * createIfNecessary is false.
1677 */
1678 ErrorConsoleView getErrorConsole(boolean createIfNecessary) {
1679 if (createIfNecessary && mErrorConsole == null) {
1680 mErrorConsole = new ErrorConsoleView(mActivity);
1681 mErrorConsole.setWebView(mMainView);
1682 }
1683 return mErrorConsole;
1684 }
1685
John Reck30c714c2010-12-16 17:30:34 -08001686 private void setLockIconType(LockIcon icon) {
1687 mCurrentState.mLockIcon = icon;
1688 mWebViewController.onUpdatedLockIcon(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001689 }
1690
1691 /**
1692 * @return The tab's lock icon type.
1693 */
John Reck30c714c2010-12-16 17:30:34 -08001694 LockIcon getLockIconType() {
1695 return mCurrentState.mLockIcon;
1696 }
1697
1698 int getLoadProgress() {
1699 if (mInPageLoad) {
1700 return mPageLoadProgress;
1701 }
1702 return 100;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001703 }
1704
1705 /**
1706 * @return TRUE if onPageStarted is called while onPageFinished is not
1707 * called yet.
1708 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001709 boolean inPageLoad() {
1710 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001711 }
1712
1713 // force mInLoad to be false. This should only be called before closing the
1714 // tab to ensure BrowserActivity's pauseWebViewTimers() is called correctly.
Michael Kolb8233fac2010-10-26 16:08:53 -07001715 void clearInPageLoad() {
1716 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001717 }
1718
Grace Kloba22ac16e2009-10-07 18:00:23 -07001719 /**
John Reck30c714c2010-12-16 17:30:34 -08001720 * Get the cached saved state bundle.
1721 * @return cached state bundle
Grace Kloba22ac16e2009-10-07 18:00:23 -07001722 */
1723 Bundle getSavedState() {
1724 return mSavedState;
1725 }
1726
John Reckaed9c542011-05-27 16:08:53 -07001727 Bundle getSavedState(boolean saveImages) {
1728 if (saveImages && mScreenshot != null) {
1729 Bundle b = new Bundle(mSavedState);
1730 b.putParcelable(SCREENSHOT, mScreenshot);
1731 return b;
1732 }
1733 return mSavedState;
1734 }
1735
Grace Kloba22ac16e2009-10-07 18:00:23 -07001736 /**
1737 * Set the saved state.
1738 */
1739 void setSavedState(Bundle state) {
1740 mSavedState = state;
1741 }
1742
1743 /**
1744 * @return TRUE if succeed in saving the state.
1745 */
1746 boolean saveState() {
1747 // If the WebView is null it means we ran low on memory and we already
1748 // stored the saved state in mSavedState.
1749 if (mMainView == null) {
1750 return mSavedState != null;
1751 }
1752
1753 mSavedState = new Bundle();
John Reck541f55a2011-06-07 16:34:43 -07001754 mMainView.saveState(mSavedState);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001755
Michael Kolbc831b632011-05-11 09:30:34 -07001756 mSavedState.putLong(ID, mId);
John Reck30c714c2010-12-16 17:30:34 -08001757 mSavedState.putString(CURRURL, mCurrentState.mUrl);
1758 mSavedState.putString(CURRTITLE, mCurrentState.mTitle);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001759 if (mAppId != null) {
1760 mSavedState.putString(APPID, mAppId);
1761 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001762 // Remember the parent tab so the relationship can be restored.
Michael Kolbc831b632011-05-11 09:30:34 -07001763 if (mParent != null) {
1764 mSavedState.putLong(PARENTTAB, mParent.mId);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001765 }
John Reckb0a86db2011-05-24 14:05:58 -07001766 mSavedState.putBoolean(USERAGENT,
1767 mSettings.hasDesktopUseragent(getWebView()));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001768 return true;
1769 }
1770
1771 /*
1772 * Restore the state of the tab.
1773 */
1774 boolean restoreState(Bundle b) {
1775 if (b == null) {
1776 return false;
1777 }
1778 // Restore the internal state even if the WebView fails to restore.
1779 // This will maintain the app id, original url and close-on-exit values.
1780 mSavedState = null;
Michael Kolbc831b632011-05-11 09:30:34 -07001781 mId = b.getLong(ID);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001782 mAppId = b.getString(APPID);
Michael Kolbeb95db42011-03-03 10:38:40 -08001783 mScreenshot = b.getParcelable(SCREENSHOT);
John Reckb0a86db2011-05-24 14:05:58 -07001784 if (b.getBoolean(USERAGENT)
1785 != mSettings.hasDesktopUseragent(getWebView())) {
1786 mSettings.toggleDesktopUseragent(getWebView());
1787 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001788
1789 final WebBackForwardList list = mMainView.restoreState(b);
1790 if (list == null) {
1791 return false;
1792 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001793 return true;
1794 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04001795
Leon Scroggins1961ed22010-12-07 15:22:21 -05001796 public void updateBookmarkedStatus() {
John Recke969cc52010-12-21 17:24:43 -08001797 mDataController.queryBookmarkStatus(getUrl(), mIsBookmarkCallback);
Leon Scroggins1961ed22010-12-07 15:22:21 -05001798 }
1799
John Recke969cc52010-12-21 17:24:43 -08001800 private DataController.OnQueryUrlIsBookmark mIsBookmarkCallback
1801 = new DataController.OnQueryUrlIsBookmark() {
1802 @Override
1803 public void onQueryUrlIsBookmark(String url, boolean isBookmark) {
1804 if (mCurrentState.mUrl.equals(url)) {
1805 mCurrentState.mIsBookmarkedSite = isBookmark;
1806 mWebViewController.bookmarkedStatusHasChanged(Tab.this);
1807 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001808 }
John Recke969cc52010-12-21 17:24:43 -08001809 };
Michael Kolb1acef692011-03-08 14:12:06 -08001810
Michael Kolbeb95db42011-03-03 10:38:40 -08001811 public void setScreenshot(Bitmap screenshot) {
1812 mScreenshot = screenshot;
1813 }
1814
1815 public Bitmap getScreenshot() {
1816 return mScreenshot;
1817 }
1818
John Reck541f55a2011-06-07 16:34:43 -07001819 public boolean isSnapshot() {
1820 return mIsSnapshot;
1821 }
1822
1823 public boolean loadSnapshot(InputStream rstream) {
1824 if (rstream == null) {
1825 mIsSnapshot = false;
1826 if (mMainView != null) {
1827 mMainView.clearViewState();
1828 }
1829 return true;
1830 }
1831 DataInputStream stream = new DataInputStream(rstream);
1832 if (!readTabInfo(stream)) {
1833 return false;
1834 }
1835 if (!mMainView.loadViewState(stream)) {
1836 return false;
1837 }
1838 mIsSnapshot = true;
1839 return true;
1840 }
1841
1842 public boolean saveSnapshot(OutputStream rstream) {
1843 if (rstream == null) return false;
1844 if (mMainView == null) return false;
1845 DataOutputStream stream = new DataOutputStream(rstream);
1846 if (saveTabInfo(stream)) {
1847 return mMainView.saveViewState(stream);
1848 }
1849 return false;
1850 }
1851
1852 private boolean readTabInfo(DataInputStream stream) {
1853 try {
1854 PageState state = new PageState(mActivity, false);
1855 state.mTitle = stream.readUTF();
1856 if (state.mTitle.length() == 0) {
1857 state.mTitle = null;
1858 }
1859 state.mUrl = stream.readUTF();
1860 int faviconLen = stream.readInt();
1861 if (faviconLen > 0) {
1862 byte[] data = new byte[faviconLen];
1863 int read = stream.read(data);
1864 if (read != faviconLen) {
1865 throw new IOException("Read didn't match expected len!"
1866 + " Expected: " + faviconLen
1867 + " Got: " + read);
1868 }
1869 state.mFavicon = BitmapFactory.decodeByteArray(data, 0, data.length);
1870 }
1871 mCurrentState = state;
1872 return true;
1873 } catch (IOException e) {
1874 return false;
1875 }
1876 }
1877
1878 private boolean saveTabInfo(DataOutputStream stream) {
1879 try {
1880 // mTitle might be null, but writeUTF doesn't handle that
1881 String title = mCurrentState.mTitle;
1882 stream.writeUTF(title != null ? title : "");
1883 // mUrl is never null
1884 stream.writeUTF(mCurrentState.mUrl);
1885 byte[] compressedPixels = compressFavicon();
1886 if (compressedPixels == null) {
1887 stream.writeInt(-1);
1888 } else {
1889 stream.writeInt(compressedPixels.length);
1890 stream.write(compressedPixels);
1891 }
1892 return true;
1893 } catch (Exception e) {
1894 Log.w(LOGTAG, "Failed to saveTabInfo", e);
1895 return false;
1896 }
1897 }
1898
1899 private byte[] compressFavicon() {
1900 Bitmap favicon = mCurrentState.mFavicon;
1901 if (favicon == null) {
1902 return null;
1903 }
1904 ByteArrayOutputStream stream = new ByteArrayOutputStream();
1905 byte[] data = null;
1906 try {
1907 favicon.compress(CompressFormat.PNG, 100, stream);
1908 data = stream.toByteArray();
1909 stream.close();
1910 } catch (IOException e) {
1911 // Will return null below then
1912 }
1913 return data;
1914 }
1915
Grace Kloba22ac16e2009-10-07 18:00:23 -07001916}