blob: 89f567b8a86c926809e018d66616c8ae8a687dd4 [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;
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;
Grace Kloba22ac16e2009-10-07 18:00:23 -070038import android.util.Log;
39import android.view.KeyEvent;
40import android.view.LayoutInflater;
41import android.view.View;
Grace Kloba50c241e2010-04-20 11:07:50 -070042import android.view.ViewStub;
Brian Carlstrom8862c1d2011-06-02 01:05:55 -070043import android.webkit.ClientCertRequestHandler;
Ben Murdochc42addf2010-01-28 15:19:59 +000044import android.webkit.ConsoleMessage;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -050045import android.webkit.DownloadListener;
Grace Kloba22ac16e2009-10-07 18:00:23 -070046import android.webkit.GeolocationPermissions;
47import android.webkit.HttpAuthHandler;
48import android.webkit.SslErrorHandler;
49import android.webkit.URLUtil;
50import android.webkit.ValueCallback;
51import android.webkit.WebBackForwardList;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -050052import android.webkit.WebBackForwardListClient;
Grace Kloba22ac16e2009-10-07 18:00:23 -070053import android.webkit.WebChromeClient;
54import android.webkit.WebHistoryItem;
John Reck438bf462011-01-12 18:11:46 -080055import android.webkit.WebResourceResponse;
Grace Kloba22ac16e2009-10-07 18:00:23 -070056import android.webkit.WebStorage;
57import android.webkit.WebView;
58import android.webkit.WebViewClient;
Ben Murdoch1d676b62011-01-17 12:54:24 +000059import android.widget.CheckBox;
Grace Kloba22ac16e2009-10-07 18:00:23 -070060import android.widget.LinearLayout;
61import android.widget.TextView;
Ben Murdoch8029a772010-11-16 11:58:21 +000062import android.widget.Toast;
Grace Kloba22ac16e2009-10-07 18:00:23 -070063
John Reck541f55a2011-06-07 16:34:43 -070064import com.android.browser.homepages.HomeProvider;
65import com.android.common.speech.LoggingEvents;
66
67import java.io.ByteArrayOutputStream;
68import java.io.DataInputStream;
69import java.io.DataOutputStream;
70import java.io.IOException;
71import java.io.InputStream;
72import java.io.OutputStream;
Michael Kolbfe251992010-07-08 15:41:55 -070073import java.util.ArrayList;
74import java.util.HashMap;
75import java.util.Iterator;
76import java.util.LinkedList;
77import java.util.Map;
78import java.util.Vector;
79
Grace Kloba22ac16e2009-10-07 18:00:23 -070080/**
81 * Class for maintaining Tabs with a main WebView and a subwindow.
82 */
83class Tab {
Michael Kolb8233fac2010-10-26 16:08:53 -070084
Grace Kloba22ac16e2009-10-07 18:00:23 -070085 // Log Tag
86 private static final String LOGTAG = "Tab";
Ben Murdochc42addf2010-01-28 15:19:59 +000087 // Special case the logtag for messages for the Console to make it easier to
88 // filter them and match the logtag used for these messages in older versions
89 // of the browser.
90 private static final String CONSOLE_LOGTAG = "browser";
91
John Reck30c714c2010-12-16 17:30:34 -080092 public enum LockIcon {
93 LOCK_ICON_UNSECURE,
94 LOCK_ICON_SECURE,
95 LOCK_ICON_MIXED,
96 }
Michael Kolb8233fac2010-10-26 16:08:53 -070097
98 Activity mActivity;
99 private WebViewController mWebViewController;
100
Michael Kolbc831b632011-05-11 09:30:34 -0700101 // The tab ID
102 private long mId;
103
Grace Kloba22ac16e2009-10-07 18:00:23 -0700104 // The Geolocation permissions prompt
105 private GeolocationPermissionsPrompt mGeolocationPermissionsPrompt;
106 // Main WebView wrapper
Michael Kolba713ec82010-11-29 17:27:06 -0800107 private View mContainer;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700108 // Main WebView
109 private WebView mMainView;
110 // Subwindow container
111 private View mSubViewContainer;
112 // Subwindow WebView
113 private WebView mSubView;
114 // Saved bundle for when we are running low on memory. It contains the
115 // information needed to restore the WebView if the user goes back to the
116 // tab.
117 private Bundle mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700118 // Parent Tab. This is the Tab that created this Tab, or null if the Tab was
119 // created by the UI
Michael Kolbc831b632011-05-11 09:30:34 -0700120 private Tab mParent;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700121 // Tab that constructed by this Tab. This is used when this Tab is
122 // destroyed, it clears all mParentTab values in the children.
Michael Kolbc831b632011-05-11 09:30:34 -0700123 private Vector<Tab> mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700124 // If true, the tab is in the foreground of the current activity.
125 private boolean mInForeground;
Michael Kolb8233fac2010-10-26 16:08:53 -0700126 // If true, the tab is in page loading state (after onPageStarted,
127 // before onPageFinsihed)
128 private boolean mInPageLoad;
John Reck30c714c2010-12-16 17:30:34 -0800129 // The last reported progress of the current page
130 private int mPageLoadProgress;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000131 // The time the load started, used to find load page time
132 private long mLoadStartTime;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700133 // Application identifier used to find tabs that another application wants
134 // to reuse.
135 private String mAppId;
136 // Keep the original url around to avoid killing the old WebView if the url
137 // has not changed.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700138 // Error console for the tab
139 private ErrorConsoleView mErrorConsole;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -0500140 // The listener that gets invoked when a download is started from the
141 // mMainView
142 private final DownloadListener mDownloadListener;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500143 // Listener used to know when we move forward or back in the history list.
144 private final WebBackForwardListClient mWebBackForwardListClient;
John Recke969cc52010-12-21 17:24:43 -0800145 private DataController mDataController;
Patrick Scott92066772011-03-10 08:46:27 -0500146 // State of the auto-login request.
147 private DeviceAccountLogin mDeviceAccountLogin;
John Reck541f55a2011-06-07 16:34:43 -0700148 private boolean mIsSnapshot = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700149
150 // AsyncTask for downloading touch icons
151 DownloadTouchIcon mTouchIconLoader;
152
Michael Kolbeb95db42011-03-03 10:38:40 -0800153 private Bitmap mScreenshot;
John Reck35e9dd62011-04-25 09:01:54 -0700154 private BrowserSettings mSettings;
Michael Kolbeb95db42011-03-03 10:38:40 -0800155
John Reck30c714c2010-12-16 17:30:34 -0800156 // All the state needed for a page
157 private static class PageState {
158 String mUrl;
159 String mTitle;
160 LockIcon mLockIcon;
161 Bitmap mFavicon;
John Recke969cc52010-12-21 17:24:43 -0800162 Boolean mIsBookmarkedSite = false;
John Reck30c714c2010-12-16 17:30:34 -0800163
164 PageState(Context c, boolean incognito) {
165 if (incognito) {
166 mUrl = "browser:incognito";
167 mTitle = c.getString(R.string.new_incognito_tab);
John Reck30c714c2010-12-16 17:30:34 -0800168 } else {
169 mUrl = "";
170 mTitle = c.getString(R.string.new_tab);
John Reck30c714c2010-12-16 17:30:34 -0800171 }
Justin Hodc6cbb72011-01-24 15:14:54 -0800172 mFavicon = BitmapFactory.decodeResource(
173 c.getResources(), R.drawable.app_web_browser_sm);
John Reck30c714c2010-12-16 17:30:34 -0800174 mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
175 }
176
177 PageState(Context c, boolean incognito, String url, Bitmap favicon) {
178 mUrl = url;
179 mTitle = null;
180 if (URLUtil.isHttpsUrl(url)) {
181 mLockIcon = LockIcon.LOCK_ICON_SECURE;
182 } else {
183 mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
184 }
185 if (favicon != null) {
186 mFavicon = favicon;
187 } else {
Justin Hodc6cbb72011-01-24 15:14:54 -0800188 mFavicon = BitmapFactory.decodeResource(
189 c.getResources(), R.drawable.app_web_browser_sm);
John Reck30c714c2010-12-16 17:30:34 -0800190 }
191 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700192 }
193
John Reck30c714c2010-12-16 17:30:34 -0800194 // The current/loading page's state
195 private PageState mCurrentState;
196
Grace Kloba22ac16e2009-10-07 18:00:23 -0700197 // Used for saving and restoring each Tab
Michael Kolbc831b632011-05-11 09:30:34 -0700198 static final String ID = "ID";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700199 static final String CURRURL = "currentUrl";
200 static final String CURRTITLE = "currentTitle";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700201 static final String PARENTTAB = "parentTab";
202 static final String APPID = "appid";
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700203 static final String INCOGNITO = "privateBrowsingEnabled";
Michael Kolbeb95db42011-03-03 10:38:40 -0800204 static final String SCREENSHOT = "screenshot";
John Reckb0a86db2011-05-24 14:05:58 -0700205 static final String USERAGENT = "useragent";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700206
207 // -------------------------------------------------------------------------
208
Leon Scroggins58d56c62010-01-28 15:12:40 -0500209 /**
210 * Private information regarding the latest voice search. If the Tab is not
211 * in voice search mode, this will be null.
212 */
213 private VoiceSearchData mVoiceSearchData;
214 /**
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400215 * Remove voice search mode from this tab.
216 */
217 public void revertVoiceSearchMode() {
218 if (mVoiceSearchData != null) {
219 mVoiceSearchData = null;
220 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700221 mWebViewController.revertVoiceSearchMode(this);
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400222 }
223 }
224 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700225
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400226 /**
Leon Scroggins58d56c62010-01-28 15:12:40 -0500227 * Return whether the tab is in voice search mode.
228 */
229 public boolean isInVoiceSearchMode() {
230 return mVoiceSearchData != null;
231 }
232 /**
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400233 * Return true if the Tab is in voice search mode and the voice search
234 * Intent came with a String identifying that Google provided the Intent.
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500235 */
236 public boolean voiceSearchSourceIsGoogle() {
237 return mVoiceSearchData != null && mVoiceSearchData.mSourceIsGoogle;
238 }
239 /**
Leon Scroggins58d56c62010-01-28 15:12:40 -0500240 * Get the title to display for the current voice search page. If the Tab
241 * is not in voice search mode, return null.
242 */
243 public String getVoiceDisplayTitle() {
244 if (mVoiceSearchData == null) return null;
245 return mVoiceSearchData.mLastVoiceSearchTitle;
246 }
247 /**
248 * Get the latest array of voice search results, to be passed to the
249 * BrowserProvider. If the Tab is not in voice search mode, return null.
250 */
251 public ArrayList<String> getVoiceSearchResults() {
252 if (mVoiceSearchData == null) return null;
253 return mVoiceSearchData.mVoiceSearchResults;
254 }
255 /**
256 * Activate voice search mode.
257 * @param intent Intent which has the results to use, or an index into the
258 * results when reusing the old results.
259 */
260 /* package */ void activateVoiceSearchMode(Intent intent) {
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500261 int index = 0;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500262 ArrayList<String> results = intent.getStringArrayListExtra(
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -0500263 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_STRINGS);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500264 if (results != null) {
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500265 ArrayList<String> urls = intent.getStringArrayListExtra(
266 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_URLS);
267 ArrayList<String> htmls = intent.getStringArrayListExtra(
268 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_HTML);
269 ArrayList<String> baseUrls = intent.getStringArrayListExtra(
270 RecognizerResultsIntent
271 .EXTRA_VOICE_SEARCH_RESULT_HTML_BASE_URLS);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500272 // This tab is now entering voice search mode for the first time, or
273 // a new voice search was done.
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500274 int size = results.size();
275 if (urls == null || size != urls.size()) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500276 throw new AssertionError("improper extras passed in Intent");
277 }
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500278 if (htmls == null || htmls.size() != size || baseUrls == null ||
279 (baseUrls.size() != size && baseUrls.size() != 1)) {
280 // If either of these arrays are empty/incorrectly sized, ignore
281 // them.
282 htmls = null;
283 baseUrls = null;
284 }
285 mVoiceSearchData = new VoiceSearchData(results, urls, htmls,
286 baseUrls);
Leon Scroggins9df94972010-03-08 18:20:35 -0500287 mVoiceSearchData.mHeaders = intent.getParcelableArrayListExtra(
288 RecognizerResultsIntent
289 .EXTRA_VOICE_SEARCH_RESULT_HTTP_HEADERS);
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500290 mVoiceSearchData.mSourceIsGoogle = intent.getBooleanExtra(
291 VoiceSearchData.SOURCE_IS_GOOGLE, false);
Leon Scroggins2ee4a5a2010-03-15 16:56:57 -0400292 mVoiceSearchData.mVoiceSearchIntent = new Intent(intent);
Leon Scrogginse10dde52010-03-08 19:53:03 -0500293 }
294 String extraData = intent.getStringExtra(
295 SearchManager.EXTRA_DATA_KEY);
296 if (extraData != null) {
297 index = Integer.parseInt(extraData);
298 if (index >= mVoiceSearchData.mVoiceSearchResults.size()) {
299 throw new AssertionError("index must be less than "
300 + "size of mVoiceSearchResults");
301 }
302 if (mVoiceSearchData.mSourceIsGoogle) {
303 Intent logIntent = new Intent(
304 LoggingEvents.ACTION_LOG_EVENT);
305 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
306 LoggingEvents.VoiceSearch.N_BEST_CHOOSE);
307 logIntent.putExtra(
308 LoggingEvents.VoiceSearch.EXTRA_N_BEST_CHOOSE_INDEX,
309 index);
310 mActivity.sendBroadcast(logIntent);
311 }
312 if (mVoiceSearchData.mVoiceSearchIntent != null) {
Leon Scroggins2ee4a5a2010-03-15 16:56:57 -0400313 // Copy the Intent, so that each history item will have its own
314 // Intent, with different (or none) extra data.
315 Intent latest = new Intent(mVoiceSearchData.mVoiceSearchIntent);
316 latest.putExtra(SearchManager.EXTRA_DATA_KEY, extraData);
317 mVoiceSearchData.mVoiceSearchIntent = latest;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500318 }
319 }
320 mVoiceSearchData.mLastVoiceSearchTitle
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500321 = mVoiceSearchData.mVoiceSearchResults.get(index);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500322 if (mInForeground) {
Michael Kolb11d19782011-03-20 10:17:40 -0700323 mWebViewController.activateVoiceSearchMode(
324 mVoiceSearchData.mLastVoiceSearchTitle,
325 mVoiceSearchData.mVoiceSearchResults);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500326 }
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500327 if (mVoiceSearchData.mVoiceSearchHtmls != null) {
328 // When index was found it was already ensured that it was valid
329 String uriString = mVoiceSearchData.mVoiceSearchHtmls.get(index);
330 if (uriString != null) {
331 Uri dataUri = Uri.parse(uriString);
332 if (RecognizerResultsIntent.URI_SCHEME_INLINE.equals(
333 dataUri.getScheme())) {
334 // If there is only one base URL, use it. If there are
335 // more, there will be one for each index, so use the base
336 // URL corresponding to the index.
337 String baseUrl = mVoiceSearchData.mVoiceSearchBaseUrls.get(
338 mVoiceSearchData.mVoiceSearchBaseUrls.size() > 1 ?
339 index : 0);
340 mVoiceSearchData.mLastVoiceSearchUrl = baseUrl;
341 mMainView.loadDataWithBaseURL(baseUrl,
342 uriString.substring(RecognizerResultsIntent
343 .URI_SCHEME_INLINE.length() + 1), "text/html",
344 "utf-8", baseUrl);
345 return;
346 }
347 }
348 }
Leon Scroggins58d56c62010-01-28 15:12:40 -0500349 mVoiceSearchData.mLastVoiceSearchUrl
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500350 = mVoiceSearchData.mVoiceSearchUrls.get(index);
351 if (null == mVoiceSearchData.mLastVoiceSearchUrl) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700352 mVoiceSearchData.mLastVoiceSearchUrl = UrlUtils.smartUrlFilter(
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500353 mVoiceSearchData.mLastVoiceSearchTitle);
354 }
Leon Scroggins9df94972010-03-08 18:20:35 -0500355 Map<String, String> headers = null;
356 if (mVoiceSearchData.mHeaders != null) {
357 int bundleIndex = mVoiceSearchData.mHeaders.size() == 1 ? 0
358 : index;
359 Bundle bundle = mVoiceSearchData.mHeaders.get(bundleIndex);
360 if (bundle != null && !bundle.isEmpty()) {
361 Iterator<String> iter = bundle.keySet().iterator();
362 headers = new HashMap<String, String>();
363 while (iter.hasNext()) {
364 String key = iter.next();
365 headers.put(key, bundle.getString(key));
366 }
367 }
368 }
369 mMainView.loadUrl(mVoiceSearchData.mLastVoiceSearchUrl, headers);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500370 }
371 /* package */ static class VoiceSearchData {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500372 public VoiceSearchData(ArrayList<String> results,
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500373 ArrayList<String> urls, ArrayList<String> htmls,
374 ArrayList<String> baseUrls) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500375 mVoiceSearchResults = results;
376 mVoiceSearchUrls = urls;
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500377 mVoiceSearchHtmls = htmls;
378 mVoiceSearchBaseUrls = baseUrls;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500379 }
380 /*
381 * ArrayList of suggestions to be displayed when opening the
382 * SearchManager
383 */
384 public ArrayList<String> mVoiceSearchResults;
385 /*
386 * ArrayList of urls, associated with the suggestions in
387 * mVoiceSearchResults.
388 */
389 public ArrayList<String> mVoiceSearchUrls;
390 /*
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500391 * ArrayList holding content to load for each item in
392 * mVoiceSearchResults.
393 */
394 public ArrayList<String> mVoiceSearchHtmls;
395 /*
396 * ArrayList holding base urls for the items in mVoiceSearchResults.
397 * If non null, this will either have the same size as
398 * mVoiceSearchResults or have a size of 1, in which case all will use
399 * the same base url
400 */
401 public ArrayList<String> mVoiceSearchBaseUrls;
402 /*
Leon Scroggins58d56c62010-01-28 15:12:40 -0500403 * The last url provided by voice search. Used for comparison to see if
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500404 * we are going to a page by some method besides voice search.
Leon Scroggins58d56c62010-01-28 15:12:40 -0500405 */
406 public String mLastVoiceSearchUrl;
407 /**
408 * The last title used for voice search. Needed to update the title bar
409 * when switching tabs.
410 */
411 public String mLastVoiceSearchTitle;
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500412 /**
413 * Whether the Intent which turned on voice search mode contained the
414 * String signifying that Google was the source.
415 */
416 public boolean mSourceIsGoogle;
417 /**
Leon Scroggins9df94972010-03-08 18:20:35 -0500418 * List of headers to be passed into the WebView containing location
419 * information
420 */
421 public ArrayList<Bundle> mHeaders;
422 /**
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500423 * The Intent used to invoke voice search. Placed on the
424 * WebHistoryItem so that when coming back to a previous voice search
425 * page we can again activate voice search.
426 */
Leon Scrogginse10dde52010-03-08 19:53:03 -0500427 public Intent mVoiceSearchIntent;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500428 /**
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500429 * String used to identify Google as the source of voice search.
430 */
431 public static String SOURCE_IS_GOOGLE
432 = "android.speech.extras.SOURCE_IS_GOOGLE";
Leon Scroggins58d56c62010-01-28 15:12:40 -0500433 }
434
Grace Kloba22ac16e2009-10-07 18:00:23 -0700435 // Container class for the next error dialog that needs to be displayed
436 private class ErrorDialog {
437 public final int mTitle;
438 public final String mDescription;
439 public final int mError;
440 ErrorDialog(int title, String desc, int error) {
441 mTitle = title;
442 mDescription = desc;
443 mError = error;
444 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700445 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700446
447 private void processNextError() {
448 if (mQueuedErrors == null) {
449 return;
450 }
451 // The first one is currently displayed so just remove it.
452 mQueuedErrors.removeFirst();
453 if (mQueuedErrors.size() == 0) {
454 mQueuedErrors = null;
455 return;
456 }
457 showError(mQueuedErrors.getFirst());
458 }
459
460 private DialogInterface.OnDismissListener mDialogListener =
461 new DialogInterface.OnDismissListener() {
462 public void onDismiss(DialogInterface d) {
463 processNextError();
464 }
465 };
466 private LinkedList<ErrorDialog> mQueuedErrors;
467
468 private void queueError(int err, String desc) {
469 if (mQueuedErrors == null) {
470 mQueuedErrors = new LinkedList<ErrorDialog>();
471 }
472 for (ErrorDialog d : mQueuedErrors) {
473 if (d.mError == err) {
474 // Already saw a similar error, ignore the new one.
475 return;
476 }
477 }
478 ErrorDialog errDialog = new ErrorDialog(
479 err == WebViewClient.ERROR_FILE_NOT_FOUND ?
480 R.string.browserFrameFileErrorLabel :
481 R.string.browserFrameNetworkErrorLabel,
482 desc, err);
483 mQueuedErrors.addLast(errDialog);
484
485 // Show the dialog now if the queue was empty and it is in foreground
486 if (mQueuedErrors.size() == 1 && mInForeground) {
487 showError(errDialog);
488 }
489 }
490
491 private void showError(ErrorDialog errDialog) {
492 if (mInForeground) {
493 AlertDialog d = new AlertDialog.Builder(mActivity)
494 .setTitle(errDialog.mTitle)
495 .setMessage(errDialog.mDescription)
496 .setPositiveButton(R.string.ok, null)
497 .create();
498 d.setOnDismissListener(mDialogListener);
499 d.show();
500 }
501 }
502
503 // -------------------------------------------------------------------------
504 // WebViewClient implementation for the main WebView
505 // -------------------------------------------------------------------------
506
507 private final WebViewClient mWebViewClient = new WebViewClient() {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500508 private Message mDontResend;
509 private Message mResend;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700510 @Override
511 public void onPageStarted(WebView view, String url, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700512 mInPageLoad = true;
John Reck30c714c2010-12-16 17:30:34 -0800513 mPageLoadProgress = 0;
514 mCurrentState = new PageState(mActivity,
515 view.isPrivateBrowsingEnabled(), url, favicon);
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000516 mLoadStartTime = SystemClock.uptimeMillis();
Leon Scroggins58d56c62010-01-28 15:12:40 -0500517 if (mVoiceSearchData != null
518 && !url.equals(mVoiceSearchData.mLastVoiceSearchUrl)) {
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500519 if (mVoiceSearchData.mSourceIsGoogle) {
520 Intent i = new Intent(LoggingEvents.ACTION_LOG_EVENT);
521 i.putExtra(LoggingEvents.EXTRA_FLUSH, true);
522 mActivity.sendBroadcast(i);
523 }
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400524 revertVoiceSearchMode();
Leon Scroggins58d56c62010-01-28 15:12:40 -0500525 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700526
Grace Kloba22ac16e2009-10-07 18:00:23 -0700527
528 // If we start a touch icon load and then load a new page, we don't
529 // want to cancel the current touch icon loader. But, we do want to
530 // create a new one when the touch icon url is known.
531 if (mTouchIconLoader != null) {
532 mTouchIconLoader.mTab = null;
533 mTouchIconLoader = null;
534 }
535
536 // reset the error console
537 if (mErrorConsole != null) {
538 mErrorConsole.clearErrorMessages();
Michael Kolb8233fac2010-10-26 16:08:53 -0700539 if (mWebViewController.shouldShowErrorConsole()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700540 mErrorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
541 }
542 }
543
Patrick Scott92066772011-03-10 08:46:27 -0500544 // Cancel the auto-login process.
545 if (mDeviceAccountLogin != null) {
546 mDeviceAccountLogin.cancel();
547 mDeviceAccountLogin = null;
548 mWebViewController.hideAutoLogin(Tab.this);
549 }
550
Grace Kloba22ac16e2009-10-07 18:00:23 -0700551 // finally update the UI in the activity if it is in the foreground
John Reck324d4402011-01-11 16:56:42 -0800552 mWebViewController.onPageStarted(Tab.this, view, favicon);
Leon Scroggins4cd97792010-12-03 15:31:56 -0500553
John Recke969cc52010-12-21 17:24:43 -0800554 updateBookmarkedStatus();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700555 }
556
557 @Override
558 public void onPageFinished(WebView view, String url) {
John Reck5b691842010-11-29 11:21:13 -0800559 if (!isPrivateBrowsingEnabled()) {
560 LogTag.logPageFinishedLoading(
561 url, SystemClock.uptimeMillis() - mLoadStartTime);
562 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700563 mInPageLoad = false;
John Reck30c714c2010-12-16 17:30:34 -0800564 // Sync state (in case of stop/timeout)
565 mCurrentState.mUrl = view.getUrl();
John Reck6c702ee2011-01-07 09:41:53 -0800566 if (mCurrentState.mUrl == null) {
567 mCurrentState.mUrl = url != null ? url : "";
568 }
John Reck30c714c2010-12-16 17:30:34 -0800569 mCurrentState.mTitle = view.getTitle();
570 mCurrentState.mFavicon = view.getFavicon();
571 if (!URLUtil.isHttpsUrl(mCurrentState.mUrl)) {
572 // In case we stop when loading an HTTPS page from an HTTP page
573 // but before a provisional load occurred
574 mCurrentState.mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
575 }
John Reck324d4402011-01-11 16:56:42 -0800576 mWebViewController.onPageFinished(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700577 }
578
579 // return true if want to hijack the url to let another app to handle it
580 @Override
581 public boolean shouldOverrideUrlLoading(WebView view, String url) {
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400582 if (voiceSearchSourceIsGoogle()) {
583 // This method is called when the user clicks on a link.
584 // VoiceSearchMode is turned off when the user leaves the
585 // Google results page, so at this point the user must be on
586 // that page. If the user clicked a link on that page, assume
587 // that the voice search was effective, and broadcast an Intent
588 // so a receiver can take note of that fact.
589 Intent logIntent = new Intent(LoggingEvents.ACTION_LOG_EVENT);
590 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
591 LoggingEvents.VoiceSearch.RESULT_CLICKED);
592 mActivity.sendBroadcast(logIntent);
593 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700594 if (mInForeground) {
Michael Kolb18eb3772010-12-10 14:29:51 -0800595 return mWebViewController.shouldOverrideUrlLoading(Tab.this,
596 view, url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700597 } else {
598 return false;
599 }
600 }
601
602 /**
603 * Updates the lock icon. This method is called when we discover another
604 * resource to be loaded for this page (for example, javascript). While
605 * we update the icon type, we do not update the lock icon itself until
606 * we are done loading, it is slightly more secure this way.
607 */
608 @Override
609 public void onLoadResource(WebView view, String url) {
610 if (url != null && url.length() > 0) {
611 // It is only if the page claims to be secure that we may have
612 // to update the lock:
John Reck30c714c2010-12-16 17:30:34 -0800613 if (mCurrentState.mLockIcon == LockIcon.LOCK_ICON_SECURE) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700614 // If NOT a 'safe' url, change the lock to mixed content!
615 if (!(URLUtil.isHttpsUrl(url) || URLUtil.isDataUrl(url)
616 || URLUtil.isAboutUrl(url))) {
John Reck30c714c2010-12-16 17:30:34 -0800617 mCurrentState.mLockIcon = LockIcon.LOCK_ICON_MIXED;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700618 }
619 }
620 }
621 }
622
623 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700624 * Show a dialog informing the user of the network error reported by
625 * WebCore if it is in the foreground.
626 */
627 @Override
628 public void onReceivedError(WebView view, int errorCode,
629 String description, String failingUrl) {
630 if (errorCode != WebViewClient.ERROR_HOST_LOOKUP &&
631 errorCode != WebViewClient.ERROR_CONNECT &&
632 errorCode != WebViewClient.ERROR_BAD_URL &&
633 errorCode != WebViewClient.ERROR_UNSUPPORTED_SCHEME &&
634 errorCode != WebViewClient.ERROR_FILE) {
635 queueError(errorCode, description);
636 }
Jeff Hamilton47654f42010-09-07 09:57:51 -0500637
638 // Don't log URLs when in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -0700639 if (!isPrivateBrowsingEnabled()) {
Jeff Hamilton47654f42010-09-07 09:57:51 -0500640 Log.e(LOGTAG, "onReceivedError " + errorCode + " " + failingUrl
641 + " " + description);
642 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700643 }
644
645 /**
646 * Check with the user if it is ok to resend POST data as the page they
647 * are trying to navigate to is the result of a POST.
648 */
649 @Override
650 public void onFormResubmission(WebView view, final Message dontResend,
651 final Message resend) {
652 if (!mInForeground) {
653 dontResend.sendToTarget();
654 return;
655 }
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500656 if (mDontResend != null) {
657 Log.w(LOGTAG, "onFormResubmission should not be called again "
658 + "while dialog is still up");
659 dontResend.sendToTarget();
660 return;
661 }
662 mDontResend = dontResend;
663 mResend = resend;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700664 new AlertDialog.Builder(mActivity).setTitle(
665 R.string.browserFrameFormResubmitLabel).setMessage(
666 R.string.browserFrameFormResubmitMessage)
667 .setPositiveButton(R.string.ok,
668 new DialogInterface.OnClickListener() {
669 public void onClick(DialogInterface dialog,
670 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500671 if (mResend != null) {
672 mResend.sendToTarget();
673 mResend = null;
674 mDontResend = null;
675 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700676 }
677 }).setNegativeButton(R.string.cancel,
678 new DialogInterface.OnClickListener() {
679 public void onClick(DialogInterface dialog,
680 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500681 if (mDontResend != null) {
682 mDontResend.sendToTarget();
683 mResend = null;
684 mDontResend = null;
685 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700686 }
687 }).setOnCancelListener(new OnCancelListener() {
688 public void onCancel(DialogInterface dialog) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500689 if (mDontResend != null) {
690 mDontResend.sendToTarget();
691 mResend = null;
692 mDontResend = null;
693 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700694 }
695 }).show();
696 }
697
698 /**
699 * Insert the url into the visited history database.
700 * @param url The url to be inserted.
701 * @param isReload True if this url is being reloaded.
702 * FIXME: Not sure what to do when reloading the page.
703 */
704 @Override
705 public void doUpdateVisitedHistory(WebView view, String url,
706 boolean isReload) {
John Reck324d4402011-01-11 16:56:42 -0800707 mWebViewController.doUpdateVisitedHistory(Tab.this, isReload);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700708 }
709
710 /**
711 * Displays SSL error(s) dialog to the user.
712 */
713 @Override
714 public void onReceivedSslError(final WebView view,
715 final SslErrorHandler handler, final SslError error) {
716 if (!mInForeground) {
717 handler.cancel();
John Reck30c714c2010-12-16 17:30:34 -0800718 setLockIconType(LockIcon.LOCK_ICON_UNSECURE);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700719 return;
720 }
John Reck35e9dd62011-04-25 09:01:54 -0700721 if (mSettings.showSecurityWarnings()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700722 final LayoutInflater factory =
723 LayoutInflater.from(mActivity);
724 final View warningsView =
725 factory.inflate(R.layout.ssl_warnings, null);
726 final LinearLayout placeholder =
727 (LinearLayout)warningsView.findViewById(R.id.placeholder);
728
729 if (error.hasError(SslError.SSL_UNTRUSTED)) {
730 LinearLayout ll = (LinearLayout)factory
731 .inflate(R.layout.ssl_warning, null);
732 ((TextView)ll.findViewById(R.id.warning))
733 .setText(R.string.ssl_untrusted);
734 placeholder.addView(ll);
735 }
736
737 if (error.hasError(SslError.SSL_IDMISMATCH)) {
738 LinearLayout ll = (LinearLayout)factory
739 .inflate(R.layout.ssl_warning, null);
740 ((TextView)ll.findViewById(R.id.warning))
741 .setText(R.string.ssl_mismatch);
742 placeholder.addView(ll);
743 }
744
745 if (error.hasError(SslError.SSL_EXPIRED)) {
746 LinearLayout ll = (LinearLayout)factory
747 .inflate(R.layout.ssl_warning, null);
748 ((TextView)ll.findViewById(R.id.warning))
749 .setText(R.string.ssl_expired);
750 placeholder.addView(ll);
751 }
752
753 if (error.hasError(SslError.SSL_NOTYETVALID)) {
754 LinearLayout ll = (LinearLayout)factory
755 .inflate(R.layout.ssl_warning, null);
756 ((TextView)ll.findViewById(R.id.warning))
757 .setText(R.string.ssl_not_yet_valid);
758 placeholder.addView(ll);
759 }
760
761 new AlertDialog.Builder(mActivity).setTitle(
762 R.string.security_warning).setIcon(
763 android.R.drawable.ic_dialog_alert).setView(
764 warningsView).setPositiveButton(R.string.ssl_continue,
765 new DialogInterface.OnClickListener() {
766 public void onClick(DialogInterface dialog,
767 int whichButton) {
768 handler.proceed();
769 }
770 }).setNeutralButton(R.string.view_certificate,
771 new DialogInterface.OnClickListener() {
772 public void onClick(DialogInterface dialog,
773 int whichButton) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700774 mWebViewController.showSslCertificateOnError(view,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700775 handler, error);
776 }
Ben Murdocha49b8292010-11-16 11:56:04 +0000777 }).setNegativeButton(R.string.ssl_go_back,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700778 new DialogInterface.OnClickListener() {
779 public void onClick(DialogInterface dialog,
780 int whichButton) {
John Reck30c714c2010-12-16 17:30:34 -0800781 dialog.cancel();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700782 }
783 }).setOnCancelListener(
784 new DialogInterface.OnCancelListener() {
785 public void onCancel(DialogInterface dialog) {
786 handler.cancel();
John Reck30c714c2010-12-16 17:30:34 -0800787 setLockIconType(LockIcon.LOCK_ICON_UNSECURE);
788 mWebViewController.onUserCanceledSsl(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700789 }
790 }).show();
791 } else {
792 handler.proceed();
793 }
794 }
795
796 /**
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700797 * Displays client certificate request to the user.
798 */
799 @Override
800 public void onReceivedClientCertRequest(final WebView view,
801 final ClientCertRequestHandler handler, final String host_and_port) {
802 if (!mInForeground) {
803 handler.ignore();
804 return;
805 }
Brian Carlstromaa09cd82011-06-09 16:04:40 -0700806 KeyChain.choosePrivateKeyAlias(mActivity, new KeyChainAliasCallback() {
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700807 @Override public void alias(String alias) {
808 if (alias == null) {
809 handler.cancel();
810 return;
811 }
812 new KeyChainLookup(mActivity, handler, alias).execute();
813 }
Brian Carlstromaa09cd82011-06-09 16:04:40 -0700814 }, null, null, null, -1);
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700815 }
816
817 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700818 * Handles an HTTP authentication request.
819 *
820 * @param handler The authentication handler
821 * @param host The host
822 * @param realm The realm
823 */
824 @Override
825 public void onReceivedHttpAuthRequest(WebView view,
826 final HttpAuthHandler handler, final String host,
827 final String realm) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700828 mWebViewController.onReceivedHttpAuthRequest(Tab.this, view, handler, host, realm);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700829 }
830
831 @Override
John Reck438bf462011-01-12 18:11:46 -0800832 public WebResourceResponse shouldInterceptRequest(WebView view,
833 String url) {
834 WebResourceResponse res = HomeProvider.shouldInterceptRequest(
835 mActivity, url);
836 return res;
837 }
838
839 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700840 public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
841 if (!mInForeground) {
842 return false;
843 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700844 return mWebViewController.shouldOverrideKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700845 }
846
847 @Override
848 public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700849 if (!mInForeground) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700850 return;
851 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700852 mWebViewController.onUnhandledKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700853 }
Patrick Scott92066772011-03-10 08:46:27 -0500854
855 @Override
856 public void onReceivedLoginRequest(WebView view, String realm,
857 String account, String args) {
858 new DeviceAccountLogin(mActivity, view, Tab.this, mWebViewController)
859 .handleLogin(realm, account, args);
860 }
861
Grace Kloba22ac16e2009-10-07 18:00:23 -0700862 };
863
Patrick Scott92066772011-03-10 08:46:27 -0500864 // Called by DeviceAccountLogin when the Tab needs to have the auto-login UI
865 // displayed.
866 void setDeviceAccountLogin(DeviceAccountLogin login) {
867 mDeviceAccountLogin = login;
868 }
869
870 // Returns non-null if the title bar should display the auto-login UI.
871 DeviceAccountLogin getDeviceAccountLogin() {
872 return mDeviceAccountLogin;
873 }
874
Grace Kloba22ac16e2009-10-07 18:00:23 -0700875 // -------------------------------------------------------------------------
876 // WebChromeClient implementation for the main WebView
877 // -------------------------------------------------------------------------
878
879 private final WebChromeClient mWebChromeClient = new WebChromeClient() {
880 // Helper method to create a new tab or sub window.
881 private void createWindow(final boolean dialog, final Message msg) {
882 WebView.WebViewTransport transport =
883 (WebView.WebViewTransport) msg.obj;
884 if (dialog) {
885 createSubWindow();
Michael Kolb8233fac2010-10-26 16:08:53 -0700886 mWebViewController.attachSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700887 transport.setWebView(mSubView);
888 } else {
Michael Kolb7bcafde2011-05-09 13:55:59 -0700889 final Tab newTab = mWebViewController.openTab(null,
John Reck5949c662011-05-27 09:52:29 -0700890 Tab.this, true, true);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700891 transport.setWebView(newTab.getWebView());
892 }
893 msg.sendToTarget();
894 }
895
896 @Override
897 public boolean onCreateWindow(WebView view, final boolean dialog,
898 final boolean userGesture, final Message resultMsg) {
899 // only allow new window or sub window for the foreground case
900 if (!mInForeground) {
901 return false;
902 }
903 // Short-circuit if we can't create any more tabs or sub windows.
904 if (dialog && mSubView != null) {
905 new AlertDialog.Builder(mActivity)
906 .setTitle(R.string.too_many_subwindows_dialog_title)
907 .setIcon(android.R.drawable.ic_dialog_alert)
908 .setMessage(R.string.too_many_subwindows_dialog_message)
909 .setPositiveButton(R.string.ok, null)
910 .show();
911 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700912 } else if (!mWebViewController.getTabControl().canCreateNewTab()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700913 new AlertDialog.Builder(mActivity)
914 .setTitle(R.string.too_many_windows_dialog_title)
915 .setIcon(android.R.drawable.ic_dialog_alert)
916 .setMessage(R.string.too_many_windows_dialog_message)
917 .setPositiveButton(R.string.ok, null)
918 .show();
919 return false;
920 }
921
922 // Short-circuit if this was a user gesture.
923 if (userGesture) {
924 createWindow(dialog, resultMsg);
925 return true;
926 }
927
928 // Allow the popup and create the appropriate window.
929 final AlertDialog.OnClickListener allowListener =
930 new AlertDialog.OnClickListener() {
931 public void onClick(DialogInterface d,
932 int which) {
933 createWindow(dialog, resultMsg);
934 }
935 };
936
937 // Block the popup by returning a null WebView.
938 final AlertDialog.OnClickListener blockListener =
939 new AlertDialog.OnClickListener() {
940 public void onClick(DialogInterface d, int which) {
941 resultMsg.sendToTarget();
942 }
943 };
944
945 // Build a confirmation dialog to display to the user.
946 final AlertDialog d =
947 new AlertDialog.Builder(mActivity)
948 .setTitle(R.string.attention)
949 .setIcon(android.R.drawable.ic_dialog_alert)
950 .setMessage(R.string.popup_window_attempt)
951 .setPositiveButton(R.string.allow, allowListener)
952 .setNegativeButton(R.string.block, blockListener)
953 .setCancelable(false)
954 .create();
955
956 // Show the confirmation dialog.
957 d.show();
958 return true;
959 }
960
961 @Override
Patrick Scotteb5061b2009-11-18 15:00:30 -0500962 public void onRequestFocus(WebView view) {
963 if (!mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700964 mWebViewController.switchToTab(Tab.this);
Patrick Scotteb5061b2009-11-18 15:00:30 -0500965 }
966 }
967
968 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700969 public void onCloseWindow(WebView window) {
Michael Kolbc831b632011-05-11 09:30:34 -0700970 if (mParent != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700971 // JavaScript can only close popup window.
972 if (mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700973 mWebViewController.switchToTab(mParent);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700974 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700975 mWebViewController.closeTab(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700976 }
977 }
978
979 @Override
980 public void onProgressChanged(WebView view, int newProgress) {
John Reck30c714c2010-12-16 17:30:34 -0800981 mPageLoadProgress = newProgress;
982 mWebViewController.onProgressChanged(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700983 }
984
985 @Override
Leon Scroggins21d9b902010-03-11 09:33:11 -0500986 public void onReceivedTitle(WebView view, final String title) {
John Reck30c714c2010-12-16 17:30:34 -0800987 mCurrentState.mTitle = title;
Michael Kolb8233fac2010-10-26 16:08:53 -0700988 mWebViewController.onReceivedTitle(Tab.this, title);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700989 }
990
991 @Override
992 public void onReceivedIcon(WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -0800993 mCurrentState.mFavicon = icon;
Michael Kolb8233fac2010-10-26 16:08:53 -0700994 mWebViewController.onFavicon(Tab.this, view, icon);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700995 }
996
997 @Override
998 public void onReceivedTouchIconUrl(WebView view, String url,
999 boolean precomposed) {
1000 final ContentResolver cr = mActivity.getContentResolver();
Leon Scrogginsc8393d92010-04-23 14:58:16 -04001001 // Let precomposed icons take precedence over non-composed
1002 // icons.
1003 if (precomposed && mTouchIconLoader != null) {
1004 mTouchIconLoader.cancel(false);
1005 mTouchIconLoader = null;
1006 }
1007 // Have only one async task at a time.
1008 if (mTouchIconLoader == null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001009 mTouchIconLoader = new DownloadTouchIcon(Tab.this,
1010 mActivity, cr, view);
Leon Scrogginsc8393d92010-04-23 14:58:16 -04001011 mTouchIconLoader.execute(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001012 }
1013 }
1014
1015 @Override
1016 public void onShowCustomView(View view,
1017 WebChromeClient.CustomViewCallback callback) {
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001018 onShowCustomView(view, mActivity.getRequestedOrientation(), callback);
1019 }
1020
1021 @Override
1022 public void onShowCustomView(View view, int requestedOrientation,
1023 WebChromeClient.CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001024 if (mInForeground) mWebViewController.showCustomView(Tab.this, view,
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001025 requestedOrientation, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001026 }
1027
1028 @Override
1029 public void onHideCustomView() {
Michael Kolb8233fac2010-10-26 16:08:53 -07001030 if (mInForeground) mWebViewController.hideCustomView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001031 }
1032
1033 /**
1034 * The origin has exceeded its database quota.
1035 * @param url the URL that exceeded the quota
1036 * @param databaseIdentifier the identifier of the database on which the
1037 * transaction that caused the quota overflow was run
1038 * @param currentQuota the current quota for the origin.
1039 * @param estimatedSize the estimated size of the database.
1040 * @param totalUsedQuota is the sum of all origins' quota.
1041 * @param quotaUpdater The callback to run when a decision to allow or
1042 * deny quota has been made. Don't forget to call this!
1043 */
1044 @Override
1045 public void onExceededDatabaseQuota(String url,
1046 String databaseIdentifier, long currentQuota, long estimatedSize,
1047 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -07001048 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001049 .onExceededDatabaseQuota(url, databaseIdentifier,
1050 currentQuota, estimatedSize, totalUsedQuota,
1051 quotaUpdater);
1052 }
1053
1054 /**
1055 * The Application Cache has exceeded its max size.
1056 * @param spaceNeeded is the amount of disk space that would be needed
1057 * in order for the last appcache operation to succeed.
1058 * @param totalUsedQuota is the sum of all origins' quota.
1059 * @param quotaUpdater A callback to inform the WebCore thread that a
1060 * new app cache size is available. This callback must always
1061 * be executed at some point to ensure that the sleeping
1062 * WebCore thread is woken up.
1063 */
1064 @Override
1065 public void onReachedMaxAppCacheSize(long spaceNeeded,
1066 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -07001067 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001068 .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota,
1069 quotaUpdater);
1070 }
1071
1072 /**
1073 * Instructs the browser to show a prompt to ask the user to set the
1074 * Geolocation permission state for the specified origin.
1075 * @param origin The origin for which Geolocation permissions are
1076 * requested.
1077 * @param callback The callback to call once the user has set the
1078 * Geolocation permission state.
1079 */
1080 @Override
1081 public void onGeolocationPermissionsShowPrompt(String origin,
1082 GeolocationPermissions.Callback callback) {
1083 if (mInForeground) {
Grace Kloba50c241e2010-04-20 11:07:50 -07001084 getGeolocationPermissionsPrompt().show(origin, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001085 }
1086 }
1087
1088 /**
1089 * Instructs the browser to hide the Geolocation permissions prompt.
1090 */
1091 @Override
1092 public void onGeolocationPermissionsHidePrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001093 if (mInForeground && mGeolocationPermissionsPrompt != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001094 mGeolocationPermissionsPrompt.hide();
1095 }
1096 }
1097
Ben Murdoch65acc352009-11-19 18:16:04 +00001098 /* Adds a JavaScript error message to the system log and if the JS
1099 * console is enabled in the about:debug options, to that console
1100 * also.
Ben Murdochc42addf2010-01-28 15:19:59 +00001101 * @param consoleMessage the message object.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001102 */
1103 @Override
Ben Murdochc42addf2010-01-28 15:19:59 +00001104 public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001105 if (mInForeground) {
1106 // call getErrorConsole(true) so it will create one if needed
1107 ErrorConsoleView errorConsole = getErrorConsole(true);
Ben Murdochc42addf2010-01-28 15:19:59 +00001108 errorConsole.addErrorMessage(consoleMessage);
Michael Kolb8233fac2010-10-26 16:08:53 -07001109 if (mWebViewController.shouldShowErrorConsole()
1110 && errorConsole.getShowState() !=
1111 ErrorConsoleView.SHOW_MAXIMIZED) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001112 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
1113 }
1114 }
Ben Murdochc42addf2010-01-28 15:19:59 +00001115
Jeff Hamilton47654f42010-09-07 09:57:51 -05001116 // Don't log console messages in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001117 if (isPrivateBrowsingEnabled()) return true;
Jeff Hamilton47654f42010-09-07 09:57:51 -05001118
Ben Murdochc42addf2010-01-28 15:19:59 +00001119 String message = "Console: " + consoleMessage.message() + " "
1120 + consoleMessage.sourceId() + ":"
1121 + consoleMessage.lineNumber();
1122
1123 switch (consoleMessage.messageLevel()) {
1124 case TIP:
1125 Log.v(CONSOLE_LOGTAG, message);
1126 break;
1127 case LOG:
1128 Log.i(CONSOLE_LOGTAG, message);
1129 break;
1130 case WARNING:
1131 Log.w(CONSOLE_LOGTAG, message);
1132 break;
1133 case ERROR:
1134 Log.e(CONSOLE_LOGTAG, message);
1135 break;
1136 case DEBUG:
1137 Log.d(CONSOLE_LOGTAG, message);
1138 break;
1139 }
1140
1141 return true;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001142 }
1143
1144 /**
1145 * Ask the browser for an icon to represent a <video> element.
1146 * This icon will be used if the Web page did not specify a poster attribute.
1147 * @return Bitmap The icon or null if no such icon is available.
1148 */
1149 @Override
1150 public Bitmap getDefaultVideoPoster() {
1151 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001152 return mWebViewController.getDefaultVideoPoster();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001153 }
1154 return null;
1155 }
1156
1157 /**
1158 * Ask the host application for a custom progress view to show while
1159 * a <video> is loading.
1160 * @return View The progress view.
1161 */
1162 @Override
1163 public View getVideoLoadingProgressView() {
1164 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001165 return mWebViewController.getVideoLoadingProgressView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001166 }
1167 return null;
1168 }
1169
1170 @Override
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01001171 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001172 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001173 mWebViewController.openFileChooser(uploadMsg, acceptType);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001174 } else {
1175 uploadMsg.onReceiveValue(null);
1176 }
1177 }
1178
1179 /**
1180 * Deliver a list of already-visited URLs
1181 */
1182 @Override
1183 public void getVisitedHistory(final ValueCallback<String[]> callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001184 mWebViewController.getVisitedHistory(callback);
1185 }
Ben Murdoch8029a772010-11-16 11:58:21 +00001186
1187 @Override
1188 public void setupAutoFill(Message message) {
1189 // Prompt the user to set up their profile.
1190 final Message msg = message;
1191 AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
Ben Murdoch1d676b62011-01-17 12:54:24 +00001192 LayoutInflater inflater = (LayoutInflater) mActivity.getSystemService(
1193 Context.LAYOUT_INFLATER_SERVICE);
1194 final View layout = inflater.inflate(R.layout.setup_autofill_dialog, null);
1195
1196 builder.setView(layout)
1197 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1198 @Override
1199 public void onClick(DialogInterface dialog, int id) {
1200 CheckBox disableAutoFill = (CheckBox) layout.findViewById(
1201 R.id.setup_autofill_dialog_disable_autofill);
1202
1203 if (disableAutoFill.isChecked()) {
1204 // Disable autofill and show a toast with how to turn it on again.
John Reck35e9dd62011-04-25 09:01:54 -07001205 mSettings.setAutofillEnabled(false);
Ben Murdoch1d676b62011-01-17 12:54:24 +00001206 Toast.makeText(mActivity,
1207 R.string.autofill_setup_dialog_negative_toast,
1208 Toast.LENGTH_LONG).show();
1209 } else {
1210 // Take user to the AutoFill profile editor. When they return,
1211 // we will send the message that we pass here which will trigger
1212 // the form to get filled out with their new profile.
1213 mWebViewController.setupAutoFill(msg);
1214 }
1215 }
1216 })
1217 .setNegativeButton(R.string.cancel, null)
1218 .show();
Ben Murdoch8029a772010-11-16 11:58:21 +00001219 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001220 };
1221
1222 // -------------------------------------------------------------------------
1223 // WebViewClient implementation for the sub window
1224 // -------------------------------------------------------------------------
1225
1226 // Subclass of WebViewClient used in subwindows to notify the main
1227 // WebViewClient of certain WebView activities.
1228 private static class SubWindowClient extends WebViewClient {
1229 // The main WebViewClient.
1230 private final WebViewClient mClient;
Michael Kolb8233fac2010-10-26 16:08:53 -07001231 private final WebViewController mController;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001232
Michael Kolb8233fac2010-10-26 16:08:53 -07001233 SubWindowClient(WebViewClient client, WebViewController controller) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001234 mClient = client;
Michael Kolb8233fac2010-10-26 16:08:53 -07001235 mController = controller;
Leon Scroggins III211ba542010-04-19 13:21:13 -04001236 }
1237 @Override
1238 public void onPageStarted(WebView view, String url, Bitmap favicon) {
1239 // Unlike the others, do not call mClient's version, which would
1240 // change the progress bar. However, we do want to remove the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001241 // find or select dialog.
Michael Kolb8233fac2010-10-26 16:08:53 -07001242 mController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001243 }
1244 @Override
1245 public void doUpdateVisitedHistory(WebView view, String url,
1246 boolean isReload) {
1247 mClient.doUpdateVisitedHistory(view, url, isReload);
1248 }
1249 @Override
1250 public boolean shouldOverrideUrlLoading(WebView view, String url) {
1251 return mClient.shouldOverrideUrlLoading(view, url);
1252 }
1253 @Override
1254 public void onReceivedSslError(WebView view, SslErrorHandler handler,
1255 SslError error) {
1256 mClient.onReceivedSslError(view, handler, error);
1257 }
1258 @Override
Brian Carlstrom8862c1d2011-06-02 01:05:55 -07001259 public void onReceivedClientCertRequest(WebView view,
1260 ClientCertRequestHandler handler, String host_and_port) {
1261 mClient.onReceivedClientCertRequest(view, handler, host_and_port);
1262 }
1263 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -07001264 public void onReceivedHttpAuthRequest(WebView view,
1265 HttpAuthHandler handler, String host, String realm) {
1266 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
1267 }
1268 @Override
1269 public void onFormResubmission(WebView view, Message dontResend,
1270 Message resend) {
1271 mClient.onFormResubmission(view, dontResend, resend);
1272 }
1273 @Override
1274 public void onReceivedError(WebView view, int errorCode,
1275 String description, String failingUrl) {
1276 mClient.onReceivedError(view, errorCode, description, failingUrl);
1277 }
1278 @Override
1279 public boolean shouldOverrideKeyEvent(WebView view,
1280 android.view.KeyEvent event) {
1281 return mClient.shouldOverrideKeyEvent(view, event);
1282 }
1283 @Override
1284 public void onUnhandledKeyEvent(WebView view,
1285 android.view.KeyEvent event) {
1286 mClient.onUnhandledKeyEvent(view, event);
1287 }
1288 }
1289
1290 // -------------------------------------------------------------------------
1291 // WebChromeClient implementation for the sub window
1292 // -------------------------------------------------------------------------
1293
1294 private class SubWindowChromeClient extends WebChromeClient {
1295 // The main WebChromeClient.
1296 private final WebChromeClient mClient;
1297
1298 SubWindowChromeClient(WebChromeClient client) {
1299 mClient = client;
1300 }
1301 @Override
1302 public void onProgressChanged(WebView view, int newProgress) {
1303 mClient.onProgressChanged(view, newProgress);
1304 }
1305 @Override
1306 public boolean onCreateWindow(WebView view, boolean dialog,
1307 boolean userGesture, android.os.Message resultMsg) {
1308 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
1309 }
1310 @Override
1311 public void onCloseWindow(WebView window) {
1312 if (window != mSubView) {
1313 Log.e(LOGTAG, "Can't close the window");
1314 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001315 mWebViewController.dismissSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001316 }
1317 }
1318
1319 // -------------------------------------------------------------------------
1320
Michael Kolb8233fac2010-10-26 16:08:53 -07001321 // TODO temporarily use activity here
1322 // remove later
1323
Grace Kloba22ac16e2009-10-07 18:00:23 -07001324 // Construct a new tab
Michael Kolb7bcafde2011-05-09 13:55:59 -07001325 Tab(WebViewController wvcontroller, WebView w) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001326 mWebViewController = wvcontroller;
1327 mActivity = mWebViewController.getActivity();
John Reck35e9dd62011-04-25 09:01:54 -07001328 mSettings = BrowserSettings.getInstance();
John Recke969cc52010-12-21 17:24:43 -08001329 mDataController = DataController.getInstance(mActivity);
John Reck847b5322011-04-14 17:02:18 -07001330 mCurrentState = new PageState(mActivity, w != null
1331 ? w.isPrivateBrowsingEnabled() : false);
Michael Kolb8233fac2010-10-26 16:08:53 -07001332 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001333 mInForeground = false;
1334
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001335 mDownloadListener = new DownloadListener() {
1336 public void onDownloadStart(String url, String userAgent,
1337 String contentDisposition, String mimetype,
1338 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001339 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001340 mimetype, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001341 }
1342 };
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001343 mWebBackForwardListClient = new WebBackForwardListClient() {
1344 @Override
1345 public void onNewHistoryItem(WebHistoryItem item) {
1346 if (isInVoiceSearchMode()) {
1347 item.setCustomData(mVoiceSearchData.mVoiceSearchIntent);
1348 }
1349 }
1350 @Override
1351 public void onIndexChanged(WebHistoryItem item, int index) {
1352 Object data = item.getCustomData();
1353 if (data != null && data instanceof Intent) {
1354 activateVoiceSearchMode((Intent) data);
1355 }
1356 }
1357 };
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001358
Grace Kloba22ac16e2009-10-07 18:00:23 -07001359 setWebView(w);
1360 }
1361
Michael Kolbc831b632011-05-11 09:30:34 -07001362 public void setId(long id) {
1363 mId = id;
1364 }
1365
1366 public long getId() {
1367 return mId;
1368 }
1369
Grace Kloba22ac16e2009-10-07 18:00:23 -07001370 /**
1371 * Sets the WebView for this tab, correctly removing the old WebView from
1372 * the container view.
1373 */
1374 void setWebView(WebView w) {
1375 if (mMainView == w) {
1376 return;
1377 }
Michael Kolba713ec82010-11-29 17:27:06 -08001378
Grace Kloba22ac16e2009-10-07 18:00:23 -07001379 // If the WebView is changing, the page will be reloaded, so any ongoing
1380 // Geolocation permission requests are void.
Grace Kloba50c241e2010-04-20 11:07:50 -07001381 if (mGeolocationPermissionsPrompt != null) {
1382 mGeolocationPermissionsPrompt.hide();
1383 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001384
Michael Kolba713ec82010-11-29 17:27:06 -08001385 mWebViewController.onSetWebView(this, w);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001386
1387 // set the new one
1388 mMainView = w;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001389 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001390 if (mMainView != null) {
1391 mMainView.setWebViewClient(mWebViewClient);
1392 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001393 // Attach DownloadManager so that downloads can start in an active
1394 // or a non-active window. This can happen when going to a site that
1395 // does a redirect after a period of time. The user could have
1396 // switched to another tab while waiting for the download to start.
1397 mMainView.setDownloadListener(mDownloadListener);
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001398 mMainView.setWebBackForwardListClient(mWebBackForwardListClient);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001399 }
1400 }
1401
1402 /**
1403 * Destroy the tab's main WebView and subWindow if any
1404 */
1405 void destroy() {
1406 if (mMainView != null) {
1407 dismissSubWindow();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001408 // save the WebView to call destroy() after detach it from the tab
1409 WebView webView = mMainView;
1410 setWebView(null);
1411 webView.destroy();
1412 }
1413 }
1414
1415 /**
1416 * Remove the tab from the parent
1417 */
1418 void removeFromTree() {
1419 // detach the children
Michael Kolbc831b632011-05-11 09:30:34 -07001420 if (mChildren != null) {
1421 for(Tab t : mChildren) {
1422 t.setParent(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001423 }
1424 }
1425 // remove itself from the parent list
Michael Kolbc831b632011-05-11 09:30:34 -07001426 if (mParent != null) {
1427 mParent.mChildren.remove(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001428 }
1429 }
1430
1431 /**
1432 * Create a new subwindow unless a subwindow already exists.
1433 * @return True if a new subwindow was created. False if one already exists.
1434 */
1435 boolean createSubWindow() {
1436 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001437 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001438 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001439 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001440 mSubView.setWebChromeClient(new SubWindowChromeClient(
1441 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001442 // Set a different DownloadListener for the mSubView, since it will
1443 // just need to dismiss the mSubView, rather than close the Tab
1444 mSubView.setDownloadListener(new DownloadListener() {
1445 public void onDownloadStart(String url, String userAgent,
1446 String contentDisposition, String mimetype,
1447 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001448 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001449 contentDisposition, mimetype, contentLength);
1450 if (mSubView.copyBackForwardList().getSize() == 0) {
1451 // This subwindow was opened for the sole purpose of
1452 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001453 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001454 }
1455 }
1456 });
Grace Kloba22ac16e2009-10-07 18:00:23 -07001457 mSubView.setOnCreateContextMenuListener(mActivity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001458 return true;
1459 }
1460 return false;
1461 }
1462
1463 /**
1464 * Dismiss the subWindow for the tab.
1465 */
1466 void dismissSubWindow() {
1467 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001468 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001469 mSubView.destroy();
1470 mSubView = null;
1471 mSubViewContainer = null;
1472 }
1473 }
1474
Grace Kloba22ac16e2009-10-07 18:00:23 -07001475
1476 /**
1477 * Set the parent tab of this tab.
1478 */
Michael Kolbc831b632011-05-11 09:30:34 -07001479 void setParent(Tab parent) {
1480 mParent = parent;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001481 // This tab may have been freed due to low memory. If that is the case,
Michael Kolbc831b632011-05-11 09:30:34 -07001482 // the parent tab id is already saved. If we are changing that id
Grace Kloba22ac16e2009-10-07 18:00:23 -07001483 // (most likely due to removing the parent tab) we must update the
Michael Kolbc831b632011-05-11 09:30:34 -07001484 // parent tab id in the saved Bundle.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001485 if (mSavedState != null) {
1486 if (parent == null) {
1487 mSavedState.remove(PARENTTAB);
1488 } else {
Michael Kolbc831b632011-05-11 09:30:34 -07001489 mSavedState.putLong(PARENTTAB, parent.getId());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001490 }
1491 }
John Reckb0a86db2011-05-24 14:05:58 -07001492
1493 // Sync the WebView useragent with the parent
1494 if (parent != null && mSettings.hasDesktopUseragent(parent.getWebView())
1495 != mSettings.hasDesktopUseragent(getWebView())) {
1496 mSettings.toggleDesktopUseragent(getWebView());
1497 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001498 }
1499
1500 /**
Michael Kolbc831b632011-05-11 09:30:34 -07001501 * If this Tab was created through another Tab, then this method returns
1502 * that Tab.
1503 * @return the Tab parent or null
1504 */
1505 public Tab getParent() {
1506 return mParent;
1507 }
1508
1509 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001510 * When a Tab is created through the content of another Tab, then we
1511 * associate the Tabs.
1512 * @param child the Tab that was created from this Tab
1513 */
1514 void addChildTab(Tab child) {
John Reck541f55a2011-06-07 16:34:43 -07001515 if (mIsSnapshot) {
1516 throw new IllegalStateException(
1517 "Snapshot tabs cannot have child tabs!");
1518 }
Michael Kolbc831b632011-05-11 09:30:34 -07001519 if (mChildren == null) {
1520 mChildren = new Vector<Tab>();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001521 }
Michael Kolbc831b632011-05-11 09:30:34 -07001522 mChildren.add(child);
1523 child.setParent(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001524 }
1525
Michael Kolbc831b632011-05-11 09:30:34 -07001526 Vector<Tab> getChildren() {
1527 return mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001528 }
1529
1530 void resume() {
1531 if (mMainView != null) {
1532 mMainView.onResume();
1533 if (mSubView != null) {
1534 mSubView.onResume();
1535 }
1536 }
1537 }
1538
1539 void pause() {
1540 if (mMainView != null) {
1541 mMainView.onPause();
1542 if (mSubView != null) {
1543 mSubView.onPause();
1544 }
1545 }
1546 }
1547
1548 void putInForeground() {
1549 mInForeground = true;
1550 resume();
1551 mMainView.setOnCreateContextMenuListener(mActivity);
1552 if (mSubView != null) {
1553 mSubView.setOnCreateContextMenuListener(mActivity);
1554 }
1555 // Show the pending error dialog if the queue is not empty
1556 if (mQueuedErrors != null && mQueuedErrors.size() > 0) {
1557 showError(mQueuedErrors.getFirst());
1558 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001559 mWebViewController.bookmarkedStatusHasChanged(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001560 }
1561
1562 void putInBackground() {
1563 mInForeground = false;
1564 pause();
1565 mMainView.setOnCreateContextMenuListener(null);
1566 if (mSubView != null) {
1567 mSubView.setOnCreateContextMenuListener(null);
1568 }
1569 }
1570
Michael Kolb8233fac2010-10-26 16:08:53 -07001571 boolean inForeground() {
1572 return mInForeground;
1573 }
1574
Grace Kloba22ac16e2009-10-07 18:00:23 -07001575 /**
1576 * Return the top window of this tab; either the subwindow if it is not
1577 * null or the main window.
1578 * @return The top window of this tab.
1579 */
1580 WebView getTopWindow() {
1581 if (mSubView != null) {
1582 return mSubView;
1583 }
1584 return mMainView;
1585 }
1586
1587 /**
1588 * Return the main window of this tab. Note: if a tab is freed in the
1589 * background, this can return null. It is only guaranteed to be
1590 * non-null for the current tab.
1591 * @return The main WebView of this tab.
1592 */
1593 WebView getWebView() {
1594 return mMainView;
1595 }
1596
Michael Kolba713ec82010-11-29 17:27:06 -08001597 void setViewContainer(View container) {
1598 mContainer = container;
1599 }
1600
Michael Kolb8233fac2010-10-26 16:08:53 -07001601 View getViewContainer() {
1602 return mContainer;
1603 }
1604
Grace Kloba22ac16e2009-10-07 18:00:23 -07001605 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001606 * Return whether private browsing is enabled for the main window of
1607 * this tab.
1608 * @return True if private browsing is enabled.
1609 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001610 boolean isPrivateBrowsingEnabled() {
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001611 WebView webView = getWebView();
1612 if (webView == null) {
1613 return false;
1614 }
1615 return webView.isPrivateBrowsingEnabled();
1616 }
1617
1618 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001619 * Return the subwindow of this tab or null if there is no subwindow.
1620 * @return The subwindow of this tab or null.
1621 */
1622 WebView getSubWebView() {
1623 return mSubView;
1624 }
1625
Michael Kolb1514bb72010-11-22 09:11:48 -08001626 void setSubWebView(WebView subView) {
1627 mSubView = subView;
1628 }
1629
Michael Kolb8233fac2010-10-26 16:08:53 -07001630 View getSubViewContainer() {
1631 return mSubViewContainer;
1632 }
1633
Michael Kolb1514bb72010-11-22 09:11:48 -08001634 void setSubViewContainer(View subViewContainer) {
1635 mSubViewContainer = subViewContainer;
1636 }
1637
Grace Kloba22ac16e2009-10-07 18:00:23 -07001638 /**
1639 * @return The geolocation permissions prompt for this tab.
1640 */
1641 GeolocationPermissionsPrompt getGeolocationPermissionsPrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001642 if (mGeolocationPermissionsPrompt == null) {
1643 ViewStub stub = (ViewStub) mContainer
1644 .findViewById(R.id.geolocation_permissions_prompt);
1645 mGeolocationPermissionsPrompt = (GeolocationPermissionsPrompt) stub
1646 .inflate();
1647 mGeolocationPermissionsPrompt.init();
1648 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001649 return mGeolocationPermissionsPrompt;
1650 }
1651
1652 /**
1653 * @return The application id string
1654 */
1655 String getAppId() {
1656 return mAppId;
1657 }
1658
1659 /**
1660 * Set the application id string
1661 * @param id
1662 */
1663 void setAppId(String id) {
1664 mAppId = id;
1665 }
1666
Grace Kloba22ac16e2009-10-07 18:00:23 -07001667 String getUrl() {
John Reck324d4402011-01-11 16:56:42 -08001668 return UrlUtils.filteredUrl(mCurrentState.mUrl);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001669 }
1670
John Reck49a603c2011-03-03 09:33:05 -08001671 String getOriginalUrl() {
1672 if (mMainView == null) {
1673 return "";
1674 }
1675 return UrlUtils.filteredUrl(mMainView.getOriginalUrl());
1676 }
1677
Grace Kloba22ac16e2009-10-07 18:00:23 -07001678 /**
John Reck30c714c2010-12-16 17:30:34 -08001679 * Get the title of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001680 */
1681 String getTitle() {
John Reck30c714c2010-12-16 17:30:34 -08001682 if (mCurrentState.mTitle == null && mInPageLoad) {
1683 return mActivity.getString(R.string.title_bar_loading);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001684 }
John Reck30c714c2010-12-16 17:30:34 -08001685 return mCurrentState.mTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001686 }
1687
1688 /**
John Reck30c714c2010-12-16 17:30:34 -08001689 * Get the favicon of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001690 */
1691 Bitmap getFavicon() {
John Reck30c714c2010-12-16 17:30:34 -08001692 return mCurrentState.mFavicon;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001693 }
1694
John Recke969cc52010-12-21 17:24:43 -08001695 public boolean isBookmarkedSite() {
1696 return mCurrentState.mIsBookmarkedSite;
1697 }
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001698
Grace Kloba22ac16e2009-10-07 18:00:23 -07001699 /**
1700 * Return the tab's error console. Creates the console if createIfNEcessary
1701 * is true and we haven't already created the console.
1702 * @param createIfNecessary Flag to indicate if the console should be
1703 * created if it has not been already.
1704 * @return The tab's error console, or null if one has not been created and
1705 * createIfNecessary is false.
1706 */
1707 ErrorConsoleView getErrorConsole(boolean createIfNecessary) {
1708 if (createIfNecessary && mErrorConsole == null) {
1709 mErrorConsole = new ErrorConsoleView(mActivity);
1710 mErrorConsole.setWebView(mMainView);
1711 }
1712 return mErrorConsole;
1713 }
1714
John Reck30c714c2010-12-16 17:30:34 -08001715 private void setLockIconType(LockIcon icon) {
1716 mCurrentState.mLockIcon = icon;
1717 mWebViewController.onUpdatedLockIcon(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001718 }
1719
1720 /**
1721 * @return The tab's lock icon type.
1722 */
John Reck30c714c2010-12-16 17:30:34 -08001723 LockIcon getLockIconType() {
1724 return mCurrentState.mLockIcon;
1725 }
1726
1727 int getLoadProgress() {
1728 if (mInPageLoad) {
1729 return mPageLoadProgress;
1730 }
1731 return 100;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001732 }
1733
1734 /**
1735 * @return TRUE if onPageStarted is called while onPageFinished is not
1736 * called yet.
1737 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001738 boolean inPageLoad() {
1739 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001740 }
1741
1742 // force mInLoad to be false. This should only be called before closing the
1743 // tab to ensure BrowserActivity's pauseWebViewTimers() is called correctly.
Michael Kolb8233fac2010-10-26 16:08:53 -07001744 void clearInPageLoad() {
1745 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001746 }
1747
Grace Kloba22ac16e2009-10-07 18:00:23 -07001748 /**
John Reck30c714c2010-12-16 17:30:34 -08001749 * Get the cached saved state bundle.
1750 * @return cached state bundle
Grace Kloba22ac16e2009-10-07 18:00:23 -07001751 */
1752 Bundle getSavedState() {
1753 return mSavedState;
1754 }
1755
John Reckaed9c542011-05-27 16:08:53 -07001756 Bundle getSavedState(boolean saveImages) {
1757 if (saveImages && mScreenshot != null) {
1758 Bundle b = new Bundle(mSavedState);
1759 b.putParcelable(SCREENSHOT, mScreenshot);
1760 return b;
1761 }
1762 return mSavedState;
1763 }
1764
Grace Kloba22ac16e2009-10-07 18:00:23 -07001765 /**
1766 * Set the saved state.
1767 */
1768 void setSavedState(Bundle state) {
1769 mSavedState = state;
1770 }
1771
1772 /**
1773 * @return TRUE if succeed in saving the state.
1774 */
1775 boolean saveState() {
1776 // If the WebView is null it means we ran low on memory and we already
1777 // stored the saved state in mSavedState.
1778 if (mMainView == null) {
1779 return mSavedState != null;
1780 }
1781
1782 mSavedState = new Bundle();
John Reck541f55a2011-06-07 16:34:43 -07001783 mMainView.saveState(mSavedState);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001784
Michael Kolbc831b632011-05-11 09:30:34 -07001785 mSavedState.putLong(ID, mId);
John Reck30c714c2010-12-16 17:30:34 -08001786 mSavedState.putString(CURRURL, mCurrentState.mUrl);
1787 mSavedState.putString(CURRTITLE, mCurrentState.mTitle);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001788 if (mAppId != null) {
1789 mSavedState.putString(APPID, mAppId);
1790 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001791 // Remember the parent tab so the relationship can be restored.
Michael Kolbc831b632011-05-11 09:30:34 -07001792 if (mParent != null) {
1793 mSavedState.putLong(PARENTTAB, mParent.mId);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001794 }
John Reckb0a86db2011-05-24 14:05:58 -07001795 mSavedState.putBoolean(USERAGENT,
1796 mSettings.hasDesktopUseragent(getWebView()));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001797 return true;
1798 }
1799
1800 /*
1801 * Restore the state of the tab.
1802 */
1803 boolean restoreState(Bundle b) {
1804 if (b == null) {
1805 return false;
1806 }
1807 // Restore the internal state even if the WebView fails to restore.
1808 // This will maintain the app id, original url and close-on-exit values.
1809 mSavedState = null;
Michael Kolbc831b632011-05-11 09:30:34 -07001810 mId = b.getLong(ID);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001811 mAppId = b.getString(APPID);
Michael Kolbeb95db42011-03-03 10:38:40 -08001812 mScreenshot = b.getParcelable(SCREENSHOT);
John Reckb0a86db2011-05-24 14:05:58 -07001813 if (b.getBoolean(USERAGENT)
1814 != mSettings.hasDesktopUseragent(getWebView())) {
1815 mSettings.toggleDesktopUseragent(getWebView());
1816 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001817
1818 final WebBackForwardList list = mMainView.restoreState(b);
1819 if (list == null) {
1820 return false;
1821 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001822 return true;
1823 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04001824
Leon Scroggins1961ed22010-12-07 15:22:21 -05001825 public void updateBookmarkedStatus() {
John Recke969cc52010-12-21 17:24:43 -08001826 mDataController.queryBookmarkStatus(getUrl(), mIsBookmarkCallback);
Leon Scroggins1961ed22010-12-07 15:22:21 -05001827 }
1828
John Recke969cc52010-12-21 17:24:43 -08001829 private DataController.OnQueryUrlIsBookmark mIsBookmarkCallback
1830 = new DataController.OnQueryUrlIsBookmark() {
1831 @Override
1832 public void onQueryUrlIsBookmark(String url, boolean isBookmark) {
1833 if (mCurrentState.mUrl.equals(url)) {
1834 mCurrentState.mIsBookmarkedSite = isBookmark;
1835 mWebViewController.bookmarkedStatusHasChanged(Tab.this);
1836 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001837 }
John Recke969cc52010-12-21 17:24:43 -08001838 };
Michael Kolb1acef692011-03-08 14:12:06 -08001839
Michael Kolbeb95db42011-03-03 10:38:40 -08001840 public void setScreenshot(Bitmap screenshot) {
1841 mScreenshot = screenshot;
1842 }
1843
1844 public Bitmap getScreenshot() {
1845 return mScreenshot;
1846 }
1847
John Reck541f55a2011-06-07 16:34:43 -07001848 public boolean isSnapshot() {
1849 return mIsSnapshot;
1850 }
1851
1852 public boolean loadSnapshot(InputStream rstream) {
1853 if (rstream == null) {
1854 mIsSnapshot = false;
1855 if (mMainView != null) {
1856 mMainView.clearViewState();
1857 }
1858 return true;
1859 }
1860 DataInputStream stream = new DataInputStream(rstream);
1861 if (!readTabInfo(stream)) {
1862 return false;
1863 }
1864 if (!mMainView.loadViewState(stream)) {
1865 return false;
1866 }
1867 mIsSnapshot = true;
1868 return true;
1869 }
1870
1871 public boolean saveSnapshot(OutputStream rstream) {
1872 if (rstream == null) return false;
1873 if (mMainView == null) return false;
1874 DataOutputStream stream = new DataOutputStream(rstream);
1875 if (saveTabInfo(stream)) {
1876 return mMainView.saveViewState(stream);
1877 }
1878 return false;
1879 }
1880
1881 private boolean readTabInfo(DataInputStream stream) {
1882 try {
1883 PageState state = new PageState(mActivity, false);
1884 state.mTitle = stream.readUTF();
1885 if (state.mTitle.length() == 0) {
1886 state.mTitle = null;
1887 }
1888 state.mUrl = stream.readUTF();
1889 int faviconLen = stream.readInt();
1890 if (faviconLen > 0) {
1891 byte[] data = new byte[faviconLen];
1892 int read = stream.read(data);
1893 if (read != faviconLen) {
1894 throw new IOException("Read didn't match expected len!"
1895 + " Expected: " + faviconLen
1896 + " Got: " + read);
1897 }
1898 state.mFavicon = BitmapFactory.decodeByteArray(data, 0, data.length);
1899 }
1900 mCurrentState = state;
1901 return true;
1902 } catch (IOException e) {
1903 return false;
1904 }
1905 }
1906
1907 private boolean saveTabInfo(DataOutputStream stream) {
1908 try {
1909 // mTitle might be null, but writeUTF doesn't handle that
1910 String title = mCurrentState.mTitle;
1911 stream.writeUTF(title != null ? title : "");
1912 // mUrl is never null
1913 stream.writeUTF(mCurrentState.mUrl);
1914 byte[] compressedPixels = compressFavicon();
1915 if (compressedPixels == null) {
1916 stream.writeInt(-1);
1917 } else {
1918 stream.writeInt(compressedPixels.length);
1919 stream.write(compressedPixels);
1920 }
1921 return true;
1922 } catch (Exception e) {
1923 Log.w(LOGTAG, "Failed to saveTabInfo", e);
1924 return false;
1925 }
1926 }
1927
1928 private byte[] compressFavicon() {
1929 Bitmap favicon = mCurrentState.mFavicon;
1930 if (favicon == null) {
1931 return null;
1932 }
1933 ByteArrayOutputStream stream = new ByteArrayOutputStream();
1934 byte[] data = null;
1935 try {
1936 favicon.compress(CompressFormat.PNG, 100, stream);
1937 data = stream.toByteArray();
1938 stream.close();
1939 } catch (IOException e) {
1940 // Will return null below then
1941 }
1942 return data;
1943 }
1944
Grace Kloba22ac16e2009-10-07 18:00:23 -07001945}