blob: e672e2bb389edf709e6b708a2f706454d47afe09 [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 Reckd8c74522011-06-14 08:45:00 -070023import android.content.ContentValues;
John Reck30c714c2010-12-16 17:30:34 -080024import android.content.Context;
Grace Kloba22ac16e2009-10-07 18:00:23 -070025import android.content.DialogInterface;
Michael Kolbfe251992010-07-08 15:41:55 -070026import android.content.DialogInterface.OnCancelListener;
Jeff Hamilton8ce956c2010-08-17 11:13:53 -050027import android.content.Intent;
Grace Kloba22ac16e2009-10-07 18:00:23 -070028import android.graphics.Bitmap;
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;
Brian Carlstrom8862c1d2011-06-02 01:05:55 -070035import android.security.KeyChain;
Brian Carlstromaa09cd82011-06-09 16:04:40 -070036import android.security.KeyChainAliasCallback;
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -050037import android.speech.RecognizerResultsIntent;
John Reck24f18262011-06-17 14:47:20 -070038import android.text.TextUtils;
Grace Kloba22ac16e2009-10-07 18:00:23 -070039import android.util.Log;
40import android.view.KeyEvent;
41import android.view.LayoutInflater;
42import android.view.View;
Grace Kloba50c241e2010-04-20 11:07:50 -070043import android.view.ViewStub;
Brian Carlstrom8862c1d2011-06-02 01:05:55 -070044import android.webkit.ClientCertRequestHandler;
Ben Murdochc42addf2010-01-28 15:19:59 +000045import android.webkit.ConsoleMessage;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -050046import android.webkit.DownloadListener;
Grace Kloba22ac16e2009-10-07 18:00:23 -070047import android.webkit.GeolocationPermissions;
48import android.webkit.HttpAuthHandler;
49import android.webkit.SslErrorHandler;
50import android.webkit.URLUtil;
51import android.webkit.ValueCallback;
52import android.webkit.WebBackForwardList;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -050053import android.webkit.WebBackForwardListClient;
Grace Kloba22ac16e2009-10-07 18:00:23 -070054import android.webkit.WebChromeClient;
55import android.webkit.WebHistoryItem;
John Reck438bf462011-01-12 18:11:46 -080056import android.webkit.WebResourceResponse;
Grace Kloba22ac16e2009-10-07 18:00:23 -070057import android.webkit.WebStorage;
58import android.webkit.WebView;
59import android.webkit.WebViewClient;
Ben Murdoch1d676b62011-01-17 12:54:24 +000060import android.widget.CheckBox;
Grace Kloba22ac16e2009-10-07 18:00:23 -070061import android.widget.LinearLayout;
62import android.widget.TextView;
Ben Murdoch8029a772010-11-16 11:58:21 +000063import android.widget.Toast;
Grace Kloba22ac16e2009-10-07 18:00:23 -070064
John Reck541f55a2011-06-07 16:34:43 -070065import com.android.browser.homepages.HomeProvider;
John Reckd8c74522011-06-14 08:45:00 -070066import com.android.browser.provider.BrowserProvider2.Snapshots;
John Reck541f55a2011-06-07 16:34:43 -070067import com.android.common.speech.LoggingEvents;
68
69import java.io.ByteArrayOutputStream;
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;
John Reckd8c74522011-06-14 08:45:00 -070096 protected WebViewController mWebViewController;
Michael Kolb8233fac2010-10-26 16:08:53 -070097
Michael Kolbc831b632011-05-11 09:30:34 -070098 // The tab ID
John Reckd8c74522011-06-14 08:45:00 -070099 private long mId = -1;
Michael Kolbc831b632011-05-11 09:30:34 -0700100
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;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700145
146 // AsyncTask for downloading touch icons
147 DownloadTouchIcon mTouchIconLoader;
148
Michael Kolbeb95db42011-03-03 10:38:40 -0800149 private Bitmap mScreenshot;
John Reck35e9dd62011-04-25 09:01:54 -0700150 private BrowserSettings mSettings;
Michael Kolbeb95db42011-03-03 10:38:40 -0800151
John Reck30c714c2010-12-16 17:30:34 -0800152 // All the state needed for a page
John Reckd8c74522011-06-14 08:45:00 -0700153 protected static class PageState {
John Reck30c714c2010-12-16 17:30:34 -0800154 String mUrl;
John Reckdb22ec42011-06-29 11:31:24 -0700155 String mOriginalUrl;
John Reck30c714c2010-12-16 17:30:34 -0800156 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) {
John Reckdb22ec42011-06-29 11:31:24 -0700163 mOriginalUrl = mUrl = "browser:incognito";
John Reck30c714c2010-12-16 17:30:34 -0800164 mTitle = c.getString(R.string.new_incognito_tab);
John Reck30c714c2010-12-16 17:30:34 -0800165 } else {
John Reckdb22ec42011-06-29 11:31:24 -0700166 mOriginalUrl = mUrl = "";
John Reck30c714c2010-12-16 17:30:34 -0800167 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) {
John Reckdb22ec42011-06-29 11:31:24 -0700175 mOriginalUrl = mUrl = url;
John Reck30c714c2010-12-16 17:30:34 -0800176 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
John Reckd8c74522011-06-14 08:45:00 -0700192 protected PageState mCurrentState;
John Reck30c714c2010-12-16 17:30:34 -0800193
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 Reckdb22ec42011-06-29 11:31:24 -0700566 mCurrentState.mOriginalUrl = view.getOriginalUrl();
John Reck30c714c2010-12-16 17:30:34 -0800567 mCurrentState.mTitle = view.getTitle();
568 mCurrentState.mFavicon = view.getFavicon();
569 if (!URLUtil.isHttpsUrl(mCurrentState.mUrl)) {
570 // In case we stop when loading an HTTPS page from an HTTP page
571 // but before a provisional load occurred
572 mCurrentState.mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
573 }
John Reck324d4402011-01-11 16:56:42 -0800574 mWebViewController.onPageFinished(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700575 }
576
577 // return true if want to hijack the url to let another app to handle it
578 @Override
579 public boolean shouldOverrideUrlLoading(WebView view, String url) {
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400580 if (voiceSearchSourceIsGoogle()) {
581 // This method is called when the user clicks on a link.
582 // VoiceSearchMode is turned off when the user leaves the
583 // Google results page, so at this point the user must be on
584 // that page. If the user clicked a link on that page, assume
585 // that the voice search was effective, and broadcast an Intent
586 // so a receiver can take note of that fact.
587 Intent logIntent = new Intent(LoggingEvents.ACTION_LOG_EVENT);
588 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
589 LoggingEvents.VoiceSearch.RESULT_CLICKED);
590 mActivity.sendBroadcast(logIntent);
591 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700592 if (mInForeground) {
Michael Kolb18eb3772010-12-10 14:29:51 -0800593 return mWebViewController.shouldOverrideUrlLoading(Tab.this,
594 view, url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700595 } else {
596 return false;
597 }
598 }
599
600 /**
601 * Updates the lock icon. This method is called when we discover another
602 * resource to be loaded for this page (for example, javascript). While
603 * we update the icon type, we do not update the lock icon itself until
604 * we are done loading, it is slightly more secure this way.
605 */
606 @Override
607 public void onLoadResource(WebView view, String url) {
608 if (url != null && url.length() > 0) {
609 // It is only if the page claims to be secure that we may have
610 // to update the lock:
John Reck30c714c2010-12-16 17:30:34 -0800611 if (mCurrentState.mLockIcon == LockIcon.LOCK_ICON_SECURE) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700612 // If NOT a 'safe' url, change the lock to mixed content!
613 if (!(URLUtil.isHttpsUrl(url) || URLUtil.isDataUrl(url)
614 || URLUtil.isAboutUrl(url))) {
John Reck30c714c2010-12-16 17:30:34 -0800615 mCurrentState.mLockIcon = LockIcon.LOCK_ICON_MIXED;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700616 }
617 }
618 }
619 }
620
621 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700622 * Show a dialog informing the user of the network error reported by
623 * WebCore if it is in the foreground.
624 */
625 @Override
626 public void onReceivedError(WebView view, int errorCode,
627 String description, String failingUrl) {
628 if (errorCode != WebViewClient.ERROR_HOST_LOOKUP &&
629 errorCode != WebViewClient.ERROR_CONNECT &&
630 errorCode != WebViewClient.ERROR_BAD_URL &&
631 errorCode != WebViewClient.ERROR_UNSUPPORTED_SCHEME &&
632 errorCode != WebViewClient.ERROR_FILE) {
633 queueError(errorCode, description);
634 }
Jeff Hamilton47654f42010-09-07 09:57:51 -0500635
636 // Don't log URLs when in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -0700637 if (!isPrivateBrowsingEnabled()) {
Jeff Hamilton47654f42010-09-07 09:57:51 -0500638 Log.e(LOGTAG, "onReceivedError " + errorCode + " " + failingUrl
639 + " " + description);
640 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700641 }
642
643 /**
644 * Check with the user if it is ok to resend POST data as the page they
645 * are trying to navigate to is the result of a POST.
646 */
647 @Override
648 public void onFormResubmission(WebView view, final Message dontResend,
649 final Message resend) {
650 if (!mInForeground) {
651 dontResend.sendToTarget();
652 return;
653 }
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500654 if (mDontResend != null) {
655 Log.w(LOGTAG, "onFormResubmission should not be called again "
656 + "while dialog is still up");
657 dontResend.sendToTarget();
658 return;
659 }
660 mDontResend = dontResend;
661 mResend = resend;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700662 new AlertDialog.Builder(mActivity).setTitle(
663 R.string.browserFrameFormResubmitLabel).setMessage(
664 R.string.browserFrameFormResubmitMessage)
665 .setPositiveButton(R.string.ok,
666 new DialogInterface.OnClickListener() {
667 public void onClick(DialogInterface dialog,
668 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500669 if (mResend != null) {
670 mResend.sendToTarget();
671 mResend = null;
672 mDontResend = null;
673 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700674 }
675 }).setNegativeButton(R.string.cancel,
676 new DialogInterface.OnClickListener() {
677 public void onClick(DialogInterface dialog,
678 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500679 if (mDontResend != null) {
680 mDontResend.sendToTarget();
681 mResend = null;
682 mDontResend = null;
683 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700684 }
685 }).setOnCancelListener(new OnCancelListener() {
686 public void onCancel(DialogInterface dialog) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500687 if (mDontResend != null) {
688 mDontResend.sendToTarget();
689 mResend = null;
690 mDontResend = null;
691 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700692 }
693 }).show();
694 }
695
696 /**
697 * Insert the url into the visited history database.
698 * @param url The url to be inserted.
699 * @param isReload True if this url is being reloaded.
700 * FIXME: Not sure what to do when reloading the page.
701 */
702 @Override
703 public void doUpdateVisitedHistory(WebView view, String url,
704 boolean isReload) {
John Reck324d4402011-01-11 16:56:42 -0800705 mWebViewController.doUpdateVisitedHistory(Tab.this, isReload);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700706 }
707
708 /**
709 * Displays SSL error(s) dialog to the user.
710 */
711 @Override
712 public void onReceivedSslError(final WebView view,
713 final SslErrorHandler handler, final SslError error) {
714 if (!mInForeground) {
715 handler.cancel();
John Reck30c714c2010-12-16 17:30:34 -0800716 setLockIconType(LockIcon.LOCK_ICON_UNSECURE);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700717 return;
718 }
John Reck35e9dd62011-04-25 09:01:54 -0700719 if (mSettings.showSecurityWarnings()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700720 final LayoutInflater factory =
721 LayoutInflater.from(mActivity);
722 final View warningsView =
723 factory.inflate(R.layout.ssl_warnings, null);
724 final LinearLayout placeholder =
725 (LinearLayout)warningsView.findViewById(R.id.placeholder);
726
727 if (error.hasError(SslError.SSL_UNTRUSTED)) {
728 LinearLayout ll = (LinearLayout)factory
729 .inflate(R.layout.ssl_warning, null);
730 ((TextView)ll.findViewById(R.id.warning))
731 .setText(R.string.ssl_untrusted);
732 placeholder.addView(ll);
733 }
734
735 if (error.hasError(SslError.SSL_IDMISMATCH)) {
736 LinearLayout ll = (LinearLayout)factory
737 .inflate(R.layout.ssl_warning, null);
738 ((TextView)ll.findViewById(R.id.warning))
739 .setText(R.string.ssl_mismatch);
740 placeholder.addView(ll);
741 }
742
743 if (error.hasError(SslError.SSL_EXPIRED)) {
744 LinearLayout ll = (LinearLayout)factory
745 .inflate(R.layout.ssl_warning, null);
746 ((TextView)ll.findViewById(R.id.warning))
747 .setText(R.string.ssl_expired);
748 placeholder.addView(ll);
749 }
750
751 if (error.hasError(SslError.SSL_NOTYETVALID)) {
752 LinearLayout ll = (LinearLayout)factory
753 .inflate(R.layout.ssl_warning, null);
754 ((TextView)ll.findViewById(R.id.warning))
755 .setText(R.string.ssl_not_yet_valid);
756 placeholder.addView(ll);
757 }
758
759 new AlertDialog.Builder(mActivity).setTitle(
760 R.string.security_warning).setIcon(
761 android.R.drawable.ic_dialog_alert).setView(
762 warningsView).setPositiveButton(R.string.ssl_continue,
763 new DialogInterface.OnClickListener() {
764 public void onClick(DialogInterface dialog,
765 int whichButton) {
766 handler.proceed();
767 }
768 }).setNeutralButton(R.string.view_certificate,
769 new DialogInterface.OnClickListener() {
770 public void onClick(DialogInterface dialog,
771 int whichButton) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700772 mWebViewController.showSslCertificateOnError(view,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700773 handler, error);
774 }
Ben Murdocha49b8292010-11-16 11:56:04 +0000775 }).setNegativeButton(R.string.ssl_go_back,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700776 new DialogInterface.OnClickListener() {
777 public void onClick(DialogInterface dialog,
778 int whichButton) {
John Reck30c714c2010-12-16 17:30:34 -0800779 dialog.cancel();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700780 }
781 }).setOnCancelListener(
782 new DialogInterface.OnCancelListener() {
783 public void onCancel(DialogInterface dialog) {
784 handler.cancel();
John Reck30c714c2010-12-16 17:30:34 -0800785 setLockIconType(LockIcon.LOCK_ICON_UNSECURE);
786 mWebViewController.onUserCanceledSsl(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700787 }
788 }).show();
789 } else {
790 handler.proceed();
791 }
792 }
793
794 /**
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700795 * Displays client certificate request to the user.
796 */
797 @Override
798 public void onReceivedClientCertRequest(final WebView view,
799 final ClientCertRequestHandler handler, final String host_and_port) {
800 if (!mInForeground) {
801 handler.ignore();
802 return;
803 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700804 int colon = host_and_port.lastIndexOf(':');
805 String host;
806 int port;
807 if (colon == -1) {
808 host = host_and_port;
809 port = -1;
810 } else {
811 String portString = host_and_port.substring(colon + 1);
812 try {
813 port = Integer.parseInt(portString);
814 host = host_and_port.substring(0, colon);
815 } catch (NumberFormatException e) {
816 host = host_and_port;
817 port = -1;
818 }
819 }
Brian Carlstromaa09cd82011-06-09 16:04:40 -0700820 KeyChain.choosePrivateKeyAlias(mActivity, new KeyChainAliasCallback() {
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700821 @Override public void alias(String alias) {
822 if (alias == null) {
823 handler.cancel();
824 return;
825 }
826 new KeyChainLookup(mActivity, handler, alias).execute();
827 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700828 }, null, null, host, port, null);
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700829 }
830
831 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700832 * Handles an HTTP authentication request.
833 *
834 * @param handler The authentication handler
835 * @param host The host
836 * @param realm The realm
837 */
838 @Override
839 public void onReceivedHttpAuthRequest(WebView view,
840 final HttpAuthHandler handler, final String host,
841 final String realm) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700842 mWebViewController.onReceivedHttpAuthRequest(Tab.this, view, handler, host, realm);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700843 }
844
845 @Override
John Reck438bf462011-01-12 18:11:46 -0800846 public WebResourceResponse shouldInterceptRequest(WebView view,
847 String url) {
848 WebResourceResponse res = HomeProvider.shouldInterceptRequest(
849 mActivity, url);
850 return res;
851 }
852
853 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700854 public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
855 if (!mInForeground) {
856 return false;
857 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700858 return mWebViewController.shouldOverrideKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700859 }
860
861 @Override
862 public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700863 if (!mInForeground) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700864 return;
865 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700866 mWebViewController.onUnhandledKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700867 }
Patrick Scott92066772011-03-10 08:46:27 -0500868
869 @Override
870 public void onReceivedLoginRequest(WebView view, String realm,
871 String account, String args) {
872 new DeviceAccountLogin(mActivity, view, Tab.this, mWebViewController)
873 .handleLogin(realm, account, args);
874 }
875
Grace Kloba22ac16e2009-10-07 18:00:23 -0700876 };
877
Patrick Scott92066772011-03-10 08:46:27 -0500878 // Called by DeviceAccountLogin when the Tab needs to have the auto-login UI
879 // displayed.
880 void setDeviceAccountLogin(DeviceAccountLogin login) {
881 mDeviceAccountLogin = login;
882 }
883
884 // Returns non-null if the title bar should display the auto-login UI.
885 DeviceAccountLogin getDeviceAccountLogin() {
886 return mDeviceAccountLogin;
887 }
888
Grace Kloba22ac16e2009-10-07 18:00:23 -0700889 // -------------------------------------------------------------------------
890 // WebChromeClient implementation for the main WebView
891 // -------------------------------------------------------------------------
892
893 private final WebChromeClient mWebChromeClient = new WebChromeClient() {
894 // Helper method to create a new tab or sub window.
895 private void createWindow(final boolean dialog, final Message msg) {
896 WebView.WebViewTransport transport =
897 (WebView.WebViewTransport) msg.obj;
898 if (dialog) {
899 createSubWindow();
Michael Kolb8233fac2010-10-26 16:08:53 -0700900 mWebViewController.attachSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700901 transport.setWebView(mSubView);
902 } else {
Michael Kolb7bcafde2011-05-09 13:55:59 -0700903 final Tab newTab = mWebViewController.openTab(null,
John Reck5949c662011-05-27 09:52:29 -0700904 Tab.this, true, true);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700905 transport.setWebView(newTab.getWebView());
906 }
907 msg.sendToTarget();
908 }
909
910 @Override
911 public boolean onCreateWindow(WebView view, final boolean dialog,
912 final boolean userGesture, final Message resultMsg) {
913 // only allow new window or sub window for the foreground case
914 if (!mInForeground) {
915 return false;
916 }
917 // Short-circuit if we can't create any more tabs or sub windows.
918 if (dialog && mSubView != null) {
919 new AlertDialog.Builder(mActivity)
920 .setTitle(R.string.too_many_subwindows_dialog_title)
921 .setIcon(android.R.drawable.ic_dialog_alert)
922 .setMessage(R.string.too_many_subwindows_dialog_message)
923 .setPositiveButton(R.string.ok, null)
924 .show();
925 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700926 } else if (!mWebViewController.getTabControl().canCreateNewTab()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700927 new AlertDialog.Builder(mActivity)
928 .setTitle(R.string.too_many_windows_dialog_title)
929 .setIcon(android.R.drawable.ic_dialog_alert)
930 .setMessage(R.string.too_many_windows_dialog_message)
931 .setPositiveButton(R.string.ok, null)
932 .show();
933 return false;
934 }
935
936 // Short-circuit if this was a user gesture.
937 if (userGesture) {
938 createWindow(dialog, resultMsg);
939 return true;
940 }
941
942 // Allow the popup and create the appropriate window.
943 final AlertDialog.OnClickListener allowListener =
944 new AlertDialog.OnClickListener() {
945 public void onClick(DialogInterface d,
946 int which) {
947 createWindow(dialog, resultMsg);
948 }
949 };
950
951 // Block the popup by returning a null WebView.
952 final AlertDialog.OnClickListener blockListener =
953 new AlertDialog.OnClickListener() {
954 public void onClick(DialogInterface d, int which) {
955 resultMsg.sendToTarget();
956 }
957 };
958
959 // Build a confirmation dialog to display to the user.
960 final AlertDialog d =
961 new AlertDialog.Builder(mActivity)
962 .setTitle(R.string.attention)
963 .setIcon(android.R.drawable.ic_dialog_alert)
964 .setMessage(R.string.popup_window_attempt)
965 .setPositiveButton(R.string.allow, allowListener)
966 .setNegativeButton(R.string.block, blockListener)
967 .setCancelable(false)
968 .create();
969
970 // Show the confirmation dialog.
971 d.show();
972 return true;
973 }
974
975 @Override
Patrick Scotteb5061b2009-11-18 15:00:30 -0500976 public void onRequestFocus(WebView view) {
977 if (!mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700978 mWebViewController.switchToTab(Tab.this);
Patrick Scotteb5061b2009-11-18 15:00:30 -0500979 }
980 }
981
982 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700983 public void onCloseWindow(WebView window) {
Michael Kolbc831b632011-05-11 09:30:34 -0700984 if (mParent != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700985 // JavaScript can only close popup window.
986 if (mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700987 mWebViewController.switchToTab(mParent);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700988 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700989 mWebViewController.closeTab(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700990 }
991 }
992
993 @Override
994 public void onProgressChanged(WebView view, int newProgress) {
John Reck30c714c2010-12-16 17:30:34 -0800995 mPageLoadProgress = newProgress;
996 mWebViewController.onProgressChanged(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700997 }
998
999 @Override
Leon Scroggins21d9b902010-03-11 09:33:11 -05001000 public void onReceivedTitle(WebView view, final String title) {
John Reck30c714c2010-12-16 17:30:34 -08001001 mCurrentState.mTitle = title;
Michael Kolb8233fac2010-10-26 16:08:53 -07001002 mWebViewController.onReceivedTitle(Tab.this, title);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001003 }
1004
1005 @Override
1006 public void onReceivedIcon(WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -08001007 mCurrentState.mFavicon = icon;
Michael Kolb8233fac2010-10-26 16:08:53 -07001008 mWebViewController.onFavicon(Tab.this, view, icon);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001009 }
1010
1011 @Override
1012 public void onReceivedTouchIconUrl(WebView view, String url,
1013 boolean precomposed) {
1014 final ContentResolver cr = mActivity.getContentResolver();
Leon Scrogginsc8393d92010-04-23 14:58:16 -04001015 // Let precomposed icons take precedence over non-composed
1016 // icons.
1017 if (precomposed && mTouchIconLoader != null) {
1018 mTouchIconLoader.cancel(false);
1019 mTouchIconLoader = null;
1020 }
1021 // Have only one async task at a time.
1022 if (mTouchIconLoader == null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001023 mTouchIconLoader = new DownloadTouchIcon(Tab.this,
1024 mActivity, cr, view);
Leon Scrogginsc8393d92010-04-23 14:58:16 -04001025 mTouchIconLoader.execute(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001026 }
1027 }
1028
1029 @Override
1030 public void onShowCustomView(View view,
1031 WebChromeClient.CustomViewCallback callback) {
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001032 onShowCustomView(view, mActivity.getRequestedOrientation(), callback);
1033 }
1034
1035 @Override
1036 public void onShowCustomView(View view, int requestedOrientation,
1037 WebChromeClient.CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001038 if (mInForeground) mWebViewController.showCustomView(Tab.this, view,
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001039 requestedOrientation, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001040 }
1041
1042 @Override
1043 public void onHideCustomView() {
Michael Kolb8233fac2010-10-26 16:08:53 -07001044 if (mInForeground) mWebViewController.hideCustomView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001045 }
1046
1047 /**
1048 * The origin has exceeded its database quota.
1049 * @param url the URL that exceeded the quota
1050 * @param databaseIdentifier the identifier of the database on which the
1051 * transaction that caused the quota overflow was run
1052 * @param currentQuota the current quota for the origin.
1053 * @param estimatedSize the estimated size of the database.
1054 * @param totalUsedQuota is the sum of all origins' quota.
1055 * @param quotaUpdater The callback to run when a decision to allow or
1056 * deny quota has been made. Don't forget to call this!
1057 */
1058 @Override
1059 public void onExceededDatabaseQuota(String url,
1060 String databaseIdentifier, long currentQuota, long estimatedSize,
1061 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -07001062 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001063 .onExceededDatabaseQuota(url, databaseIdentifier,
1064 currentQuota, estimatedSize, totalUsedQuota,
1065 quotaUpdater);
1066 }
1067
1068 /**
1069 * The Application Cache has exceeded its max size.
1070 * @param spaceNeeded is the amount of disk space that would be needed
1071 * in order for the last appcache operation to succeed.
1072 * @param totalUsedQuota is the sum of all origins' quota.
1073 * @param quotaUpdater A callback to inform the WebCore thread that a
1074 * new app cache size is available. This callback must always
1075 * be executed at some point to ensure that the sleeping
1076 * WebCore thread is woken up.
1077 */
1078 @Override
1079 public void onReachedMaxAppCacheSize(long spaceNeeded,
1080 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -07001081 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001082 .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota,
1083 quotaUpdater);
1084 }
1085
1086 /**
1087 * Instructs the browser to show a prompt to ask the user to set the
1088 * Geolocation permission state for the specified origin.
1089 * @param origin The origin for which Geolocation permissions are
1090 * requested.
1091 * @param callback The callback to call once the user has set the
1092 * Geolocation permission state.
1093 */
1094 @Override
1095 public void onGeolocationPermissionsShowPrompt(String origin,
1096 GeolocationPermissions.Callback callback) {
1097 if (mInForeground) {
Grace Kloba50c241e2010-04-20 11:07:50 -07001098 getGeolocationPermissionsPrompt().show(origin, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001099 }
1100 }
1101
1102 /**
1103 * Instructs the browser to hide the Geolocation permissions prompt.
1104 */
1105 @Override
1106 public void onGeolocationPermissionsHidePrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001107 if (mInForeground && mGeolocationPermissionsPrompt != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001108 mGeolocationPermissionsPrompt.hide();
1109 }
1110 }
1111
Ben Murdoch65acc352009-11-19 18:16:04 +00001112 /* Adds a JavaScript error message to the system log and if the JS
1113 * console is enabled in the about:debug options, to that console
1114 * also.
Ben Murdochc42addf2010-01-28 15:19:59 +00001115 * @param consoleMessage the message object.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001116 */
1117 @Override
Ben Murdochc42addf2010-01-28 15:19:59 +00001118 public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001119 if (mInForeground) {
1120 // call getErrorConsole(true) so it will create one if needed
1121 ErrorConsoleView errorConsole = getErrorConsole(true);
Ben Murdochc42addf2010-01-28 15:19:59 +00001122 errorConsole.addErrorMessage(consoleMessage);
Michael Kolb8233fac2010-10-26 16:08:53 -07001123 if (mWebViewController.shouldShowErrorConsole()
1124 && errorConsole.getShowState() !=
1125 ErrorConsoleView.SHOW_MAXIMIZED) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001126 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
1127 }
1128 }
Ben Murdochc42addf2010-01-28 15:19:59 +00001129
Jeff Hamilton47654f42010-09-07 09:57:51 -05001130 // Don't log console messages in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001131 if (isPrivateBrowsingEnabled()) return true;
Jeff Hamilton47654f42010-09-07 09:57:51 -05001132
Ben Murdochc42addf2010-01-28 15:19:59 +00001133 String message = "Console: " + consoleMessage.message() + " "
1134 + consoleMessage.sourceId() + ":"
1135 + consoleMessage.lineNumber();
1136
1137 switch (consoleMessage.messageLevel()) {
1138 case TIP:
1139 Log.v(CONSOLE_LOGTAG, message);
1140 break;
1141 case LOG:
1142 Log.i(CONSOLE_LOGTAG, message);
1143 break;
1144 case WARNING:
1145 Log.w(CONSOLE_LOGTAG, message);
1146 break;
1147 case ERROR:
1148 Log.e(CONSOLE_LOGTAG, message);
1149 break;
1150 case DEBUG:
1151 Log.d(CONSOLE_LOGTAG, message);
1152 break;
1153 }
1154
1155 return true;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001156 }
1157
1158 /**
1159 * Ask the browser for an icon to represent a <video> element.
1160 * This icon will be used if the Web page did not specify a poster attribute.
1161 * @return Bitmap The icon or null if no such icon is available.
1162 */
1163 @Override
1164 public Bitmap getDefaultVideoPoster() {
1165 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001166 return mWebViewController.getDefaultVideoPoster();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001167 }
1168 return null;
1169 }
1170
1171 /**
1172 * Ask the host application for a custom progress view to show while
1173 * a <video> is loading.
1174 * @return View The progress view.
1175 */
1176 @Override
1177 public View getVideoLoadingProgressView() {
1178 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001179 return mWebViewController.getVideoLoadingProgressView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001180 }
1181 return null;
1182 }
1183
1184 @Override
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01001185 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001186 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001187 mWebViewController.openFileChooser(uploadMsg, acceptType);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001188 } else {
1189 uploadMsg.onReceiveValue(null);
1190 }
1191 }
1192
1193 /**
1194 * Deliver a list of already-visited URLs
1195 */
1196 @Override
1197 public void getVisitedHistory(final ValueCallback<String[]> callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001198 mWebViewController.getVisitedHistory(callback);
1199 }
Ben Murdoch8029a772010-11-16 11:58:21 +00001200
1201 @Override
1202 public void setupAutoFill(Message message) {
1203 // Prompt the user to set up their profile.
1204 final Message msg = message;
1205 AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
Ben Murdoch1d676b62011-01-17 12:54:24 +00001206 LayoutInflater inflater = (LayoutInflater) mActivity.getSystemService(
1207 Context.LAYOUT_INFLATER_SERVICE);
1208 final View layout = inflater.inflate(R.layout.setup_autofill_dialog, null);
1209
1210 builder.setView(layout)
1211 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1212 @Override
1213 public void onClick(DialogInterface dialog, int id) {
1214 CheckBox disableAutoFill = (CheckBox) layout.findViewById(
1215 R.id.setup_autofill_dialog_disable_autofill);
1216
1217 if (disableAutoFill.isChecked()) {
1218 // Disable autofill and show a toast with how to turn it on again.
John Reck35e9dd62011-04-25 09:01:54 -07001219 mSettings.setAutofillEnabled(false);
Ben Murdoch1d676b62011-01-17 12:54:24 +00001220 Toast.makeText(mActivity,
1221 R.string.autofill_setup_dialog_negative_toast,
1222 Toast.LENGTH_LONG).show();
1223 } else {
1224 // Take user to the AutoFill profile editor. When they return,
1225 // we will send the message that we pass here which will trigger
1226 // the form to get filled out with their new profile.
1227 mWebViewController.setupAutoFill(msg);
1228 }
1229 }
1230 })
1231 .setNegativeButton(R.string.cancel, null)
1232 .show();
Ben Murdoch8029a772010-11-16 11:58:21 +00001233 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001234 };
1235
1236 // -------------------------------------------------------------------------
1237 // WebViewClient implementation for the sub window
1238 // -------------------------------------------------------------------------
1239
1240 // Subclass of WebViewClient used in subwindows to notify the main
1241 // WebViewClient of certain WebView activities.
1242 private static class SubWindowClient extends WebViewClient {
1243 // The main WebViewClient.
1244 private final WebViewClient mClient;
Michael Kolb8233fac2010-10-26 16:08:53 -07001245 private final WebViewController mController;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001246
Michael Kolb8233fac2010-10-26 16:08:53 -07001247 SubWindowClient(WebViewClient client, WebViewController controller) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001248 mClient = client;
Michael Kolb8233fac2010-10-26 16:08:53 -07001249 mController = controller;
Leon Scroggins III211ba542010-04-19 13:21:13 -04001250 }
1251 @Override
1252 public void onPageStarted(WebView view, String url, Bitmap favicon) {
1253 // Unlike the others, do not call mClient's version, which would
1254 // change the progress bar. However, we do want to remove the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001255 // find or select dialog.
Michael Kolb8233fac2010-10-26 16:08:53 -07001256 mController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001257 }
1258 @Override
1259 public void doUpdateVisitedHistory(WebView view, String url,
1260 boolean isReload) {
1261 mClient.doUpdateVisitedHistory(view, url, isReload);
1262 }
1263 @Override
1264 public boolean shouldOverrideUrlLoading(WebView view, String url) {
1265 return mClient.shouldOverrideUrlLoading(view, url);
1266 }
1267 @Override
1268 public void onReceivedSslError(WebView view, SslErrorHandler handler,
1269 SslError error) {
1270 mClient.onReceivedSslError(view, handler, error);
1271 }
1272 @Override
Brian Carlstrom8862c1d2011-06-02 01:05:55 -07001273 public void onReceivedClientCertRequest(WebView view,
1274 ClientCertRequestHandler handler, String host_and_port) {
1275 mClient.onReceivedClientCertRequest(view, handler, host_and_port);
1276 }
1277 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -07001278 public void onReceivedHttpAuthRequest(WebView view,
1279 HttpAuthHandler handler, String host, String realm) {
1280 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
1281 }
1282 @Override
1283 public void onFormResubmission(WebView view, Message dontResend,
1284 Message resend) {
1285 mClient.onFormResubmission(view, dontResend, resend);
1286 }
1287 @Override
1288 public void onReceivedError(WebView view, int errorCode,
1289 String description, String failingUrl) {
1290 mClient.onReceivedError(view, errorCode, description, failingUrl);
1291 }
1292 @Override
1293 public boolean shouldOverrideKeyEvent(WebView view,
1294 android.view.KeyEvent event) {
1295 return mClient.shouldOverrideKeyEvent(view, event);
1296 }
1297 @Override
1298 public void onUnhandledKeyEvent(WebView view,
1299 android.view.KeyEvent event) {
1300 mClient.onUnhandledKeyEvent(view, event);
1301 }
1302 }
1303
1304 // -------------------------------------------------------------------------
1305 // WebChromeClient implementation for the sub window
1306 // -------------------------------------------------------------------------
1307
1308 private class SubWindowChromeClient extends WebChromeClient {
1309 // The main WebChromeClient.
1310 private final WebChromeClient mClient;
1311
1312 SubWindowChromeClient(WebChromeClient client) {
1313 mClient = client;
1314 }
1315 @Override
1316 public void onProgressChanged(WebView view, int newProgress) {
1317 mClient.onProgressChanged(view, newProgress);
1318 }
1319 @Override
1320 public boolean onCreateWindow(WebView view, boolean dialog,
1321 boolean userGesture, android.os.Message resultMsg) {
1322 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
1323 }
1324 @Override
1325 public void onCloseWindow(WebView window) {
1326 if (window != mSubView) {
1327 Log.e(LOGTAG, "Can't close the window");
1328 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001329 mWebViewController.dismissSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001330 }
1331 }
1332
1333 // -------------------------------------------------------------------------
1334
Michael Kolb8233fac2010-10-26 16:08:53 -07001335 // TODO temporarily use activity here
1336 // remove later
1337
Grace Kloba22ac16e2009-10-07 18:00:23 -07001338 // Construct a new tab
Michael Kolb7bcafde2011-05-09 13:55:59 -07001339 Tab(WebViewController wvcontroller, WebView w) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001340 mWebViewController = wvcontroller;
1341 mActivity = mWebViewController.getActivity();
John Reck35e9dd62011-04-25 09:01:54 -07001342 mSettings = BrowserSettings.getInstance();
John Recke969cc52010-12-21 17:24:43 -08001343 mDataController = DataController.getInstance(mActivity);
John Reck847b5322011-04-14 17:02:18 -07001344 mCurrentState = new PageState(mActivity, w != null
1345 ? w.isPrivateBrowsingEnabled() : false);
Michael Kolb8233fac2010-10-26 16:08:53 -07001346 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001347 mInForeground = false;
1348
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001349 mDownloadListener = new DownloadListener() {
1350 public void onDownloadStart(String url, String userAgent,
1351 String contentDisposition, String mimetype,
1352 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001353 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001354 mimetype, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001355 }
1356 };
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001357 mWebBackForwardListClient = new WebBackForwardListClient() {
1358 @Override
1359 public void onNewHistoryItem(WebHistoryItem item) {
1360 if (isInVoiceSearchMode()) {
1361 item.setCustomData(mVoiceSearchData.mVoiceSearchIntent);
1362 }
1363 }
1364 @Override
1365 public void onIndexChanged(WebHistoryItem item, int index) {
1366 Object data = item.getCustomData();
1367 if (data != null && data instanceof Intent) {
1368 activateVoiceSearchMode((Intent) data);
1369 }
1370 }
1371 };
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001372
Grace Kloba22ac16e2009-10-07 18:00:23 -07001373 setWebView(w);
1374 }
1375
Michael Kolbc831b632011-05-11 09:30:34 -07001376 public void setId(long id) {
1377 mId = id;
1378 }
1379
1380 public long getId() {
1381 return mId;
1382 }
1383
Grace Kloba22ac16e2009-10-07 18:00:23 -07001384 /**
1385 * Sets the WebView for this tab, correctly removing the old WebView from
1386 * the container view.
1387 */
1388 void setWebView(WebView w) {
1389 if (mMainView == w) {
1390 return;
1391 }
Michael Kolba713ec82010-11-29 17:27:06 -08001392
Grace Kloba22ac16e2009-10-07 18:00:23 -07001393 // If the WebView is changing, the page will be reloaded, so any ongoing
1394 // Geolocation permission requests are void.
Grace Kloba50c241e2010-04-20 11:07:50 -07001395 if (mGeolocationPermissionsPrompt != null) {
1396 mGeolocationPermissionsPrompt.hide();
1397 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001398
Michael Kolba713ec82010-11-29 17:27:06 -08001399 mWebViewController.onSetWebView(this, w);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001400
1401 // set the new one
1402 mMainView = w;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001403 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001404 if (mMainView != null) {
1405 mMainView.setWebViewClient(mWebViewClient);
1406 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001407 // Attach DownloadManager so that downloads can start in an active
1408 // or a non-active window. This can happen when going to a site that
1409 // does a redirect after a period of time. The user could have
1410 // switched to another tab while waiting for the download to start.
1411 mMainView.setDownloadListener(mDownloadListener);
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001412 mMainView.setWebBackForwardListClient(mWebBackForwardListClient);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001413 }
1414 }
1415
1416 /**
1417 * Destroy the tab's main WebView and subWindow if any
1418 */
1419 void destroy() {
1420 if (mMainView != null) {
1421 dismissSubWindow();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001422 // save the WebView to call destroy() after detach it from the tab
1423 WebView webView = mMainView;
1424 setWebView(null);
1425 webView.destroy();
1426 }
1427 }
1428
1429 /**
1430 * Remove the tab from the parent
1431 */
1432 void removeFromTree() {
1433 // detach the children
Michael Kolbc831b632011-05-11 09:30:34 -07001434 if (mChildren != null) {
1435 for(Tab t : mChildren) {
1436 t.setParent(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001437 }
1438 }
1439 // remove itself from the parent list
Michael Kolbc831b632011-05-11 09:30:34 -07001440 if (mParent != null) {
1441 mParent.mChildren.remove(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001442 }
1443 }
1444
1445 /**
1446 * Create a new subwindow unless a subwindow already exists.
1447 * @return True if a new subwindow was created. False if one already exists.
1448 */
1449 boolean createSubWindow() {
1450 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001451 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001452 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001453 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001454 mSubView.setWebChromeClient(new SubWindowChromeClient(
1455 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001456 // Set a different DownloadListener for the mSubView, since it will
1457 // just need to dismiss the mSubView, rather than close the Tab
1458 mSubView.setDownloadListener(new DownloadListener() {
1459 public void onDownloadStart(String url, String userAgent,
1460 String contentDisposition, String mimetype,
1461 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001462 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001463 contentDisposition, mimetype, contentLength);
1464 if (mSubView.copyBackForwardList().getSize() == 0) {
1465 // This subwindow was opened for the sole purpose of
1466 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001467 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001468 }
1469 }
1470 });
Grace Kloba22ac16e2009-10-07 18:00:23 -07001471 mSubView.setOnCreateContextMenuListener(mActivity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001472 return true;
1473 }
1474 return false;
1475 }
1476
1477 /**
1478 * Dismiss the subWindow for the tab.
1479 */
1480 void dismissSubWindow() {
1481 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001482 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001483 mSubView.destroy();
1484 mSubView = null;
1485 mSubViewContainer = null;
1486 }
1487 }
1488
Grace Kloba22ac16e2009-10-07 18:00:23 -07001489
1490 /**
1491 * Set the parent tab of this tab.
1492 */
Michael Kolbc831b632011-05-11 09:30:34 -07001493 void setParent(Tab parent) {
1494 mParent = parent;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001495 // This tab may have been freed due to low memory. If that is the case,
Michael Kolbc831b632011-05-11 09:30:34 -07001496 // the parent tab id is already saved. If we are changing that id
Grace Kloba22ac16e2009-10-07 18:00:23 -07001497 // (most likely due to removing the parent tab) we must update the
Michael Kolbc831b632011-05-11 09:30:34 -07001498 // parent tab id in the saved Bundle.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001499 if (mSavedState != null) {
1500 if (parent == null) {
1501 mSavedState.remove(PARENTTAB);
1502 } else {
Michael Kolbc831b632011-05-11 09:30:34 -07001503 mSavedState.putLong(PARENTTAB, parent.getId());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001504 }
1505 }
John Reckb0a86db2011-05-24 14:05:58 -07001506
1507 // Sync the WebView useragent with the parent
1508 if (parent != null && mSettings.hasDesktopUseragent(parent.getWebView())
1509 != mSettings.hasDesktopUseragent(getWebView())) {
1510 mSettings.toggleDesktopUseragent(getWebView());
1511 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001512 }
1513
1514 /**
Michael Kolbc831b632011-05-11 09:30:34 -07001515 * If this Tab was created through another Tab, then this method returns
1516 * that Tab.
1517 * @return the Tab parent or null
1518 */
1519 public Tab getParent() {
1520 return mParent;
1521 }
1522
1523 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001524 * When a Tab is created through the content of another Tab, then we
1525 * associate the Tabs.
1526 * @param child the Tab that was created from this Tab
1527 */
1528 void addChildTab(Tab child) {
Michael Kolbc831b632011-05-11 09:30:34 -07001529 if (mChildren == null) {
1530 mChildren = new Vector<Tab>();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001531 }
Michael Kolbc831b632011-05-11 09:30:34 -07001532 mChildren.add(child);
1533 child.setParent(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001534 }
1535
Michael Kolbc831b632011-05-11 09:30:34 -07001536 Vector<Tab> getChildren() {
1537 return mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001538 }
1539
1540 void resume() {
1541 if (mMainView != null) {
1542 mMainView.onResume();
1543 if (mSubView != null) {
1544 mSubView.onResume();
1545 }
1546 }
1547 }
1548
1549 void pause() {
1550 if (mMainView != null) {
1551 mMainView.onPause();
1552 if (mSubView != null) {
1553 mSubView.onPause();
1554 }
1555 }
1556 }
1557
1558 void putInForeground() {
1559 mInForeground = true;
1560 resume();
1561 mMainView.setOnCreateContextMenuListener(mActivity);
1562 if (mSubView != null) {
1563 mSubView.setOnCreateContextMenuListener(mActivity);
1564 }
1565 // Show the pending error dialog if the queue is not empty
1566 if (mQueuedErrors != null && mQueuedErrors.size() > 0) {
1567 showError(mQueuedErrors.getFirst());
1568 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001569 mWebViewController.bookmarkedStatusHasChanged(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001570 }
1571
1572 void putInBackground() {
1573 mInForeground = false;
1574 pause();
1575 mMainView.setOnCreateContextMenuListener(null);
1576 if (mSubView != null) {
1577 mSubView.setOnCreateContextMenuListener(null);
1578 }
1579 }
1580
Michael Kolb8233fac2010-10-26 16:08:53 -07001581 boolean inForeground() {
1582 return mInForeground;
1583 }
1584
Grace Kloba22ac16e2009-10-07 18:00:23 -07001585 /**
1586 * Return the top window of this tab; either the subwindow if it is not
1587 * null or the main window.
1588 * @return The top window of this tab.
1589 */
1590 WebView getTopWindow() {
1591 if (mSubView != null) {
1592 return mSubView;
1593 }
1594 return mMainView;
1595 }
1596
1597 /**
1598 * Return the main window of this tab. Note: if a tab is freed in the
1599 * background, this can return null. It is only guaranteed to be
1600 * non-null for the current tab.
1601 * @return The main WebView of this tab.
1602 */
1603 WebView getWebView() {
1604 return mMainView;
1605 }
1606
Michael Kolba713ec82010-11-29 17:27:06 -08001607 void setViewContainer(View container) {
1608 mContainer = container;
1609 }
1610
Michael Kolb8233fac2010-10-26 16:08:53 -07001611 View getViewContainer() {
1612 return mContainer;
1613 }
1614
Grace Kloba22ac16e2009-10-07 18:00:23 -07001615 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001616 * Return whether private browsing is enabled for the main window of
1617 * this tab.
1618 * @return True if private browsing is enabled.
1619 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001620 boolean isPrivateBrowsingEnabled() {
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001621 WebView webView = getWebView();
1622 if (webView == null) {
1623 return false;
1624 }
1625 return webView.isPrivateBrowsingEnabled();
1626 }
1627
1628 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001629 * Return the subwindow of this tab or null if there is no subwindow.
1630 * @return The subwindow of this tab or null.
1631 */
1632 WebView getSubWebView() {
1633 return mSubView;
1634 }
1635
Michael Kolb1514bb72010-11-22 09:11:48 -08001636 void setSubWebView(WebView subView) {
1637 mSubView = subView;
1638 }
1639
Michael Kolb8233fac2010-10-26 16:08:53 -07001640 View getSubViewContainer() {
1641 return mSubViewContainer;
1642 }
1643
Michael Kolb1514bb72010-11-22 09:11:48 -08001644 void setSubViewContainer(View subViewContainer) {
1645 mSubViewContainer = subViewContainer;
1646 }
1647
Grace Kloba22ac16e2009-10-07 18:00:23 -07001648 /**
1649 * @return The geolocation permissions prompt for this tab.
1650 */
1651 GeolocationPermissionsPrompt getGeolocationPermissionsPrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001652 if (mGeolocationPermissionsPrompt == null) {
1653 ViewStub stub = (ViewStub) mContainer
1654 .findViewById(R.id.geolocation_permissions_prompt);
1655 mGeolocationPermissionsPrompt = (GeolocationPermissionsPrompt) stub
1656 .inflate();
1657 mGeolocationPermissionsPrompt.init();
1658 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001659 return mGeolocationPermissionsPrompt;
1660 }
1661
1662 /**
1663 * @return The application id string
1664 */
1665 String getAppId() {
1666 return mAppId;
1667 }
1668
1669 /**
1670 * Set the application id string
1671 * @param id
1672 */
1673 void setAppId(String id) {
1674 mAppId = id;
1675 }
1676
Grace Kloba22ac16e2009-10-07 18:00:23 -07001677 String getUrl() {
John Reck324d4402011-01-11 16:56:42 -08001678 return UrlUtils.filteredUrl(mCurrentState.mUrl);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001679 }
1680
John Reck49a603c2011-03-03 09:33:05 -08001681 String getOriginalUrl() {
John Reckdb22ec42011-06-29 11:31:24 -07001682 if (mCurrentState.mOriginalUrl == null) {
1683 return getUrl();
John Reck49a603c2011-03-03 09:33:05 -08001684 }
John Reckdb22ec42011-06-29 11:31:24 -07001685 return UrlUtils.filteredUrl(mCurrentState.mOriginalUrl);
John Reck49a603c2011-03-03 09:33:05 -08001686 }
1687
Grace Kloba22ac16e2009-10-07 18:00:23 -07001688 /**
John Reck30c714c2010-12-16 17:30:34 -08001689 * Get the title of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001690 */
1691 String getTitle() {
John Reck30c714c2010-12-16 17:30:34 -08001692 if (mCurrentState.mTitle == null && mInPageLoad) {
1693 return mActivity.getString(R.string.title_bar_loading);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001694 }
John Reck30c714c2010-12-16 17:30:34 -08001695 return mCurrentState.mTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001696 }
1697
1698 /**
John Reck30c714c2010-12-16 17:30:34 -08001699 * Get the favicon of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001700 */
1701 Bitmap getFavicon() {
John Reck30c714c2010-12-16 17:30:34 -08001702 return mCurrentState.mFavicon;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001703 }
1704
John Recke969cc52010-12-21 17:24:43 -08001705 public boolean isBookmarkedSite() {
1706 return mCurrentState.mIsBookmarkedSite;
1707 }
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001708
Grace Kloba22ac16e2009-10-07 18:00:23 -07001709 /**
1710 * Return the tab's error console. Creates the console if createIfNEcessary
1711 * is true and we haven't already created the console.
1712 * @param createIfNecessary Flag to indicate if the console should be
1713 * created if it has not been already.
1714 * @return The tab's error console, or null if one has not been created and
1715 * createIfNecessary is false.
1716 */
1717 ErrorConsoleView getErrorConsole(boolean createIfNecessary) {
1718 if (createIfNecessary && mErrorConsole == null) {
1719 mErrorConsole = new ErrorConsoleView(mActivity);
1720 mErrorConsole.setWebView(mMainView);
1721 }
1722 return mErrorConsole;
1723 }
1724
John Reck30c714c2010-12-16 17:30:34 -08001725 private void setLockIconType(LockIcon icon) {
1726 mCurrentState.mLockIcon = icon;
1727 mWebViewController.onUpdatedLockIcon(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001728 }
1729
1730 /**
1731 * @return The tab's lock icon type.
1732 */
John Reck30c714c2010-12-16 17:30:34 -08001733 LockIcon getLockIconType() {
1734 return mCurrentState.mLockIcon;
1735 }
1736
1737 int getLoadProgress() {
1738 if (mInPageLoad) {
1739 return mPageLoadProgress;
1740 }
1741 return 100;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001742 }
1743
1744 /**
1745 * @return TRUE if onPageStarted is called while onPageFinished is not
1746 * called yet.
1747 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001748 boolean inPageLoad() {
1749 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001750 }
1751
1752 // force mInLoad to be false. This should only be called before closing the
1753 // tab to ensure BrowserActivity's pauseWebViewTimers() is called correctly.
Michael Kolb8233fac2010-10-26 16:08:53 -07001754 void clearInPageLoad() {
1755 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001756 }
1757
Grace Kloba22ac16e2009-10-07 18:00:23 -07001758 /**
John Reck30c714c2010-12-16 17:30:34 -08001759 * Get the cached saved state bundle.
1760 * @return cached state bundle
Grace Kloba22ac16e2009-10-07 18:00:23 -07001761 */
1762 Bundle getSavedState() {
1763 return mSavedState;
1764 }
1765
John Reckaed9c542011-05-27 16:08:53 -07001766 Bundle getSavedState(boolean saveImages) {
1767 if (saveImages && mScreenshot != null) {
1768 Bundle b = new Bundle(mSavedState);
1769 b.putParcelable(SCREENSHOT, mScreenshot);
1770 return b;
1771 }
1772 return mSavedState;
1773 }
1774
Grace Kloba22ac16e2009-10-07 18:00:23 -07001775 /**
1776 * Set the saved state.
1777 */
1778 void setSavedState(Bundle state) {
1779 mSavedState = state;
1780 }
1781
1782 /**
1783 * @return TRUE if succeed in saving the state.
1784 */
1785 boolean saveState() {
1786 // If the WebView is null it means we ran low on memory and we already
1787 // stored the saved state in mSavedState.
1788 if (mMainView == null) {
1789 return mSavedState != null;
1790 }
John Reck24f18262011-06-17 14:47:20 -07001791 // If the tab is the homepage or has no URL, don't save it
1792 String homepage = BrowserSettings.getInstance().getHomePage();
1793 if (TextUtils.equals(homepage, mCurrentState.mUrl)
1794 || TextUtils.isEmpty(mCurrentState.mUrl)) {
1795 return false;
1796 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001797
1798 mSavedState = new Bundle();
John Reck541f55a2011-06-07 16:34:43 -07001799 mMainView.saveState(mSavedState);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001800
Michael Kolbc831b632011-05-11 09:30:34 -07001801 mSavedState.putLong(ID, mId);
John Reck30c714c2010-12-16 17:30:34 -08001802 mSavedState.putString(CURRURL, mCurrentState.mUrl);
1803 mSavedState.putString(CURRTITLE, mCurrentState.mTitle);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001804 if (mAppId != null) {
1805 mSavedState.putString(APPID, mAppId);
1806 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001807 // Remember the parent tab so the relationship can be restored.
Michael Kolbc831b632011-05-11 09:30:34 -07001808 if (mParent != null) {
1809 mSavedState.putLong(PARENTTAB, mParent.mId);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001810 }
John Reckb0a86db2011-05-24 14:05:58 -07001811 mSavedState.putBoolean(USERAGENT,
1812 mSettings.hasDesktopUseragent(getWebView()));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001813 return true;
1814 }
1815
1816 /*
1817 * Restore the state of the tab.
1818 */
1819 boolean restoreState(Bundle b) {
1820 if (b == null) {
1821 return false;
1822 }
1823 // Restore the internal state even if the WebView fails to restore.
1824 // This will maintain the app id, original url and close-on-exit values.
1825 mSavedState = null;
Michael Kolbc831b632011-05-11 09:30:34 -07001826 mId = b.getLong(ID);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001827 mAppId = b.getString(APPID);
Michael Kolbeb95db42011-03-03 10:38:40 -08001828 mScreenshot = b.getParcelable(SCREENSHOT);
John Reckb0a86db2011-05-24 14:05:58 -07001829 if (b.getBoolean(USERAGENT)
1830 != mSettings.hasDesktopUseragent(getWebView())) {
1831 mSettings.toggleDesktopUseragent(getWebView());
1832 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001833
1834 final WebBackForwardList list = mMainView.restoreState(b);
1835 if (list == null) {
1836 return false;
1837 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001838 return true;
1839 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04001840
Leon Scroggins1961ed22010-12-07 15:22:21 -05001841 public void updateBookmarkedStatus() {
John Recke969cc52010-12-21 17:24:43 -08001842 mDataController.queryBookmarkStatus(getUrl(), mIsBookmarkCallback);
Leon Scroggins1961ed22010-12-07 15:22:21 -05001843 }
1844
John Recke969cc52010-12-21 17:24:43 -08001845 private DataController.OnQueryUrlIsBookmark mIsBookmarkCallback
1846 = new DataController.OnQueryUrlIsBookmark() {
1847 @Override
1848 public void onQueryUrlIsBookmark(String url, boolean isBookmark) {
1849 if (mCurrentState.mUrl.equals(url)) {
1850 mCurrentState.mIsBookmarkedSite = isBookmark;
1851 mWebViewController.bookmarkedStatusHasChanged(Tab.this);
1852 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001853 }
John Recke969cc52010-12-21 17:24:43 -08001854 };
Michael Kolb1acef692011-03-08 14:12:06 -08001855
Michael Kolbeb95db42011-03-03 10:38:40 -08001856 public void setScreenshot(Bitmap screenshot) {
1857 mScreenshot = screenshot;
1858 }
1859
1860 public Bitmap getScreenshot() {
1861 return mScreenshot;
1862 }
1863
John Reck541f55a2011-06-07 16:34:43 -07001864 public boolean isSnapshot() {
John Reck541f55a2011-06-07 16:34:43 -07001865 return false;
1866 }
1867
John Reckd8c74522011-06-14 08:45:00 -07001868 public ContentValues createSnapshotValues() {
1869 if (mMainView == null) return null;
1870 ByteArrayOutputStream stream = new ByteArrayOutputStream();
1871 if (!mMainView.saveViewState(stream)) {
John Reck541f55a2011-06-07 16:34:43 -07001872 return null;
1873 }
John Reckd8c74522011-06-14 08:45:00 -07001874 byte[] data = stream.toByteArray();
John Reckd8c74522011-06-14 08:45:00 -07001875 ContentValues values = new ContentValues();
1876 values.put(Snapshots.TITLE, mCurrentState.mTitle);
1877 values.put(Snapshots.URL, mCurrentState.mUrl);
1878 values.put(Snapshots.VIEWSTATE, data);
1879 values.put(Snapshots.BACKGROUND, mMainView.getPageBackgroundColor());
1880 return values;
John Reck541f55a2011-06-07 16:34:43 -07001881 }
1882
John Reck26b18322011-06-21 13:08:58 -07001883 public void loadUrl(String url, Map<String, String> headers) {
1884 if (mMainView != null) {
1885 mCurrentState = new PageState(mActivity, false, url, null);
1886 mWebViewController.onPageStarted(this, mMainView, null);
1887 mMainView.loadUrl(url, headers);
1888 }
1889 }
1890
Grace Kloba22ac16e2009-10-07 18:00:23 -07001891}