blob: c78b562ef3795eebffa2e8835bca8ca94ca5d06f [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;
155 String mTitle;
156 LockIcon mLockIcon;
157 Bitmap mFavicon;
John Recke969cc52010-12-21 17:24:43 -0800158 Boolean mIsBookmarkedSite = false;
John Reck30c714c2010-12-16 17:30:34 -0800159
160 PageState(Context c, boolean incognito) {
161 if (incognito) {
162 mUrl = "browser:incognito";
163 mTitle = c.getString(R.string.new_incognito_tab);
John Reck30c714c2010-12-16 17:30:34 -0800164 } else {
165 mUrl = "";
166 mTitle = c.getString(R.string.new_tab);
John Reck30c714c2010-12-16 17:30:34 -0800167 }
Justin Hodc6cbb72011-01-24 15:14:54 -0800168 mFavicon = BitmapFactory.decodeResource(
169 c.getResources(), R.drawable.app_web_browser_sm);
John Reck30c714c2010-12-16 17:30:34 -0800170 mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
171 }
172
173 PageState(Context c, boolean incognito, String url, Bitmap favicon) {
174 mUrl = url;
175 mTitle = null;
176 if (URLUtil.isHttpsUrl(url)) {
177 mLockIcon = LockIcon.LOCK_ICON_SECURE;
178 } else {
179 mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
180 }
181 if (favicon != null) {
182 mFavicon = favicon;
183 } else {
Justin Hodc6cbb72011-01-24 15:14:54 -0800184 mFavicon = BitmapFactory.decodeResource(
185 c.getResources(), R.drawable.app_web_browser_sm);
John Reck30c714c2010-12-16 17:30:34 -0800186 }
187 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700188 }
189
John Reck30c714c2010-12-16 17:30:34 -0800190 // The current/loading page's state
John Reckd8c74522011-06-14 08:45:00 -0700191 protected PageState mCurrentState;
John Reck30c714c2010-12-16 17:30:34 -0800192
Grace Kloba22ac16e2009-10-07 18:00:23 -0700193 // Used for saving and restoring each Tab
Michael Kolbc831b632011-05-11 09:30:34 -0700194 static final String ID = "ID";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700195 static final String CURRURL = "currentUrl";
196 static final String CURRTITLE = "currentTitle";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700197 static final String PARENTTAB = "parentTab";
198 static final String APPID = "appid";
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700199 static final String INCOGNITO = "privateBrowsingEnabled";
Michael Kolbeb95db42011-03-03 10:38:40 -0800200 static final String SCREENSHOT = "screenshot";
John Reckb0a86db2011-05-24 14:05:58 -0700201 static final String USERAGENT = "useragent";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700202
203 // -------------------------------------------------------------------------
204
Leon Scroggins58d56c62010-01-28 15:12:40 -0500205 /**
206 * Private information regarding the latest voice search. If the Tab is not
207 * in voice search mode, this will be null.
208 */
209 private VoiceSearchData mVoiceSearchData;
210 /**
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400211 * Remove voice search mode from this tab.
212 */
213 public void revertVoiceSearchMode() {
214 if (mVoiceSearchData != null) {
215 mVoiceSearchData = null;
216 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700217 mWebViewController.revertVoiceSearchMode(this);
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400218 }
219 }
220 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700221
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400222 /**
Leon Scroggins58d56c62010-01-28 15:12:40 -0500223 * Return whether the tab is in voice search mode.
224 */
225 public boolean isInVoiceSearchMode() {
226 return mVoiceSearchData != null;
227 }
228 /**
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400229 * Return true if the Tab is in voice search mode and the voice search
230 * Intent came with a String identifying that Google provided the Intent.
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500231 */
232 public boolean voiceSearchSourceIsGoogle() {
233 return mVoiceSearchData != null && mVoiceSearchData.mSourceIsGoogle;
234 }
235 /**
Leon Scroggins58d56c62010-01-28 15:12:40 -0500236 * Get the title to display for the current voice search page. If the Tab
237 * is not in voice search mode, return null.
238 */
239 public String getVoiceDisplayTitle() {
240 if (mVoiceSearchData == null) return null;
241 return mVoiceSearchData.mLastVoiceSearchTitle;
242 }
243 /**
244 * Get the latest array of voice search results, to be passed to the
245 * BrowserProvider. If the Tab is not in voice search mode, return null.
246 */
247 public ArrayList<String> getVoiceSearchResults() {
248 if (mVoiceSearchData == null) return null;
249 return mVoiceSearchData.mVoiceSearchResults;
250 }
251 /**
252 * Activate voice search mode.
253 * @param intent Intent which has the results to use, or an index into the
254 * results when reusing the old results.
255 */
256 /* package */ void activateVoiceSearchMode(Intent intent) {
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500257 int index = 0;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500258 ArrayList<String> results = intent.getStringArrayListExtra(
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -0500259 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_STRINGS);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500260 if (results != null) {
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500261 ArrayList<String> urls = intent.getStringArrayListExtra(
262 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_URLS);
263 ArrayList<String> htmls = intent.getStringArrayListExtra(
264 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_HTML);
265 ArrayList<String> baseUrls = intent.getStringArrayListExtra(
266 RecognizerResultsIntent
267 .EXTRA_VOICE_SEARCH_RESULT_HTML_BASE_URLS);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500268 // This tab is now entering voice search mode for the first time, or
269 // a new voice search was done.
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500270 int size = results.size();
271 if (urls == null || size != urls.size()) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500272 throw new AssertionError("improper extras passed in Intent");
273 }
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500274 if (htmls == null || htmls.size() != size || baseUrls == null ||
275 (baseUrls.size() != size && baseUrls.size() != 1)) {
276 // If either of these arrays are empty/incorrectly sized, ignore
277 // them.
278 htmls = null;
279 baseUrls = null;
280 }
281 mVoiceSearchData = new VoiceSearchData(results, urls, htmls,
282 baseUrls);
Leon Scroggins9df94972010-03-08 18:20:35 -0500283 mVoiceSearchData.mHeaders = intent.getParcelableArrayListExtra(
284 RecognizerResultsIntent
285 .EXTRA_VOICE_SEARCH_RESULT_HTTP_HEADERS);
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500286 mVoiceSearchData.mSourceIsGoogle = intent.getBooleanExtra(
287 VoiceSearchData.SOURCE_IS_GOOGLE, false);
Leon Scroggins2ee4a5a2010-03-15 16:56:57 -0400288 mVoiceSearchData.mVoiceSearchIntent = new Intent(intent);
Leon Scrogginse10dde52010-03-08 19:53:03 -0500289 }
290 String extraData = intent.getStringExtra(
291 SearchManager.EXTRA_DATA_KEY);
292 if (extraData != null) {
293 index = Integer.parseInt(extraData);
294 if (index >= mVoiceSearchData.mVoiceSearchResults.size()) {
295 throw new AssertionError("index must be less than "
296 + "size of mVoiceSearchResults");
297 }
298 if (mVoiceSearchData.mSourceIsGoogle) {
299 Intent logIntent = new Intent(
300 LoggingEvents.ACTION_LOG_EVENT);
301 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
302 LoggingEvents.VoiceSearch.N_BEST_CHOOSE);
303 logIntent.putExtra(
304 LoggingEvents.VoiceSearch.EXTRA_N_BEST_CHOOSE_INDEX,
305 index);
306 mActivity.sendBroadcast(logIntent);
307 }
308 if (mVoiceSearchData.mVoiceSearchIntent != null) {
Leon Scroggins2ee4a5a2010-03-15 16:56:57 -0400309 // Copy the Intent, so that each history item will have its own
310 // Intent, with different (or none) extra data.
311 Intent latest = new Intent(mVoiceSearchData.mVoiceSearchIntent);
312 latest.putExtra(SearchManager.EXTRA_DATA_KEY, extraData);
313 mVoiceSearchData.mVoiceSearchIntent = latest;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500314 }
315 }
316 mVoiceSearchData.mLastVoiceSearchTitle
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500317 = mVoiceSearchData.mVoiceSearchResults.get(index);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500318 if (mInForeground) {
Michael Kolb11d19782011-03-20 10:17:40 -0700319 mWebViewController.activateVoiceSearchMode(
320 mVoiceSearchData.mLastVoiceSearchTitle,
321 mVoiceSearchData.mVoiceSearchResults);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500322 }
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500323 if (mVoiceSearchData.mVoiceSearchHtmls != null) {
324 // When index was found it was already ensured that it was valid
325 String uriString = mVoiceSearchData.mVoiceSearchHtmls.get(index);
326 if (uriString != null) {
327 Uri dataUri = Uri.parse(uriString);
328 if (RecognizerResultsIntent.URI_SCHEME_INLINE.equals(
329 dataUri.getScheme())) {
330 // If there is only one base URL, use it. If there are
331 // more, there will be one for each index, so use the base
332 // URL corresponding to the index.
333 String baseUrl = mVoiceSearchData.mVoiceSearchBaseUrls.get(
334 mVoiceSearchData.mVoiceSearchBaseUrls.size() > 1 ?
335 index : 0);
336 mVoiceSearchData.mLastVoiceSearchUrl = baseUrl;
337 mMainView.loadDataWithBaseURL(baseUrl,
338 uriString.substring(RecognizerResultsIntent
339 .URI_SCHEME_INLINE.length() + 1), "text/html",
340 "utf-8", baseUrl);
341 return;
342 }
343 }
344 }
Leon Scroggins58d56c62010-01-28 15:12:40 -0500345 mVoiceSearchData.mLastVoiceSearchUrl
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500346 = mVoiceSearchData.mVoiceSearchUrls.get(index);
347 if (null == mVoiceSearchData.mLastVoiceSearchUrl) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700348 mVoiceSearchData.mLastVoiceSearchUrl = UrlUtils.smartUrlFilter(
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500349 mVoiceSearchData.mLastVoiceSearchTitle);
350 }
Leon Scroggins9df94972010-03-08 18:20:35 -0500351 Map<String, String> headers = null;
352 if (mVoiceSearchData.mHeaders != null) {
353 int bundleIndex = mVoiceSearchData.mHeaders.size() == 1 ? 0
354 : index;
355 Bundle bundle = mVoiceSearchData.mHeaders.get(bundleIndex);
356 if (bundle != null && !bundle.isEmpty()) {
357 Iterator<String> iter = bundle.keySet().iterator();
358 headers = new HashMap<String, String>();
359 while (iter.hasNext()) {
360 String key = iter.next();
361 headers.put(key, bundle.getString(key));
362 }
363 }
364 }
365 mMainView.loadUrl(mVoiceSearchData.mLastVoiceSearchUrl, headers);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500366 }
367 /* package */ static class VoiceSearchData {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500368 public VoiceSearchData(ArrayList<String> results,
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500369 ArrayList<String> urls, ArrayList<String> htmls,
370 ArrayList<String> baseUrls) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500371 mVoiceSearchResults = results;
372 mVoiceSearchUrls = urls;
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500373 mVoiceSearchHtmls = htmls;
374 mVoiceSearchBaseUrls = baseUrls;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500375 }
376 /*
377 * ArrayList of suggestions to be displayed when opening the
378 * SearchManager
379 */
380 public ArrayList<String> mVoiceSearchResults;
381 /*
382 * ArrayList of urls, associated with the suggestions in
383 * mVoiceSearchResults.
384 */
385 public ArrayList<String> mVoiceSearchUrls;
386 /*
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500387 * ArrayList holding content to load for each item in
388 * mVoiceSearchResults.
389 */
390 public ArrayList<String> mVoiceSearchHtmls;
391 /*
392 * ArrayList holding base urls for the items in mVoiceSearchResults.
393 * If non null, this will either have the same size as
394 * mVoiceSearchResults or have a size of 1, in which case all will use
395 * the same base url
396 */
397 public ArrayList<String> mVoiceSearchBaseUrls;
398 /*
Leon Scroggins58d56c62010-01-28 15:12:40 -0500399 * The last url provided by voice search. Used for comparison to see if
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500400 * we are going to a page by some method besides voice search.
Leon Scroggins58d56c62010-01-28 15:12:40 -0500401 */
402 public String mLastVoiceSearchUrl;
403 /**
404 * The last title used for voice search. Needed to update the title bar
405 * when switching tabs.
406 */
407 public String mLastVoiceSearchTitle;
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500408 /**
409 * Whether the Intent which turned on voice search mode contained the
410 * String signifying that Google was the source.
411 */
412 public boolean mSourceIsGoogle;
413 /**
Leon Scroggins9df94972010-03-08 18:20:35 -0500414 * List of headers to be passed into the WebView containing location
415 * information
416 */
417 public ArrayList<Bundle> mHeaders;
418 /**
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500419 * The Intent used to invoke voice search. Placed on the
420 * WebHistoryItem so that when coming back to a previous voice search
421 * page we can again activate voice search.
422 */
Leon Scrogginse10dde52010-03-08 19:53:03 -0500423 public Intent mVoiceSearchIntent;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500424 /**
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500425 * String used to identify Google as the source of voice search.
426 */
427 public static String SOURCE_IS_GOOGLE
428 = "android.speech.extras.SOURCE_IS_GOOGLE";
Leon Scroggins58d56c62010-01-28 15:12:40 -0500429 }
430
Grace Kloba22ac16e2009-10-07 18:00:23 -0700431 // Container class for the next error dialog that needs to be displayed
432 private class ErrorDialog {
433 public final int mTitle;
434 public final String mDescription;
435 public final int mError;
436 ErrorDialog(int title, String desc, int error) {
437 mTitle = title;
438 mDescription = desc;
439 mError = error;
440 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700441 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700442
443 private void processNextError() {
444 if (mQueuedErrors == null) {
445 return;
446 }
447 // The first one is currently displayed so just remove it.
448 mQueuedErrors.removeFirst();
449 if (mQueuedErrors.size() == 0) {
450 mQueuedErrors = null;
451 return;
452 }
453 showError(mQueuedErrors.getFirst());
454 }
455
456 private DialogInterface.OnDismissListener mDialogListener =
457 new DialogInterface.OnDismissListener() {
458 public void onDismiss(DialogInterface d) {
459 processNextError();
460 }
461 };
462 private LinkedList<ErrorDialog> mQueuedErrors;
463
464 private void queueError(int err, String desc) {
465 if (mQueuedErrors == null) {
466 mQueuedErrors = new LinkedList<ErrorDialog>();
467 }
468 for (ErrorDialog d : mQueuedErrors) {
469 if (d.mError == err) {
470 // Already saw a similar error, ignore the new one.
471 return;
472 }
473 }
474 ErrorDialog errDialog = new ErrorDialog(
475 err == WebViewClient.ERROR_FILE_NOT_FOUND ?
476 R.string.browserFrameFileErrorLabel :
477 R.string.browserFrameNetworkErrorLabel,
478 desc, err);
479 mQueuedErrors.addLast(errDialog);
480
481 // Show the dialog now if the queue was empty and it is in foreground
482 if (mQueuedErrors.size() == 1 && mInForeground) {
483 showError(errDialog);
484 }
485 }
486
487 private void showError(ErrorDialog errDialog) {
488 if (mInForeground) {
489 AlertDialog d = new AlertDialog.Builder(mActivity)
490 .setTitle(errDialog.mTitle)
491 .setMessage(errDialog.mDescription)
492 .setPositiveButton(R.string.ok, null)
493 .create();
494 d.setOnDismissListener(mDialogListener);
495 d.show();
496 }
497 }
498
499 // -------------------------------------------------------------------------
500 // WebViewClient implementation for the main WebView
501 // -------------------------------------------------------------------------
502
503 private final WebViewClient mWebViewClient = new WebViewClient() {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500504 private Message mDontResend;
505 private Message mResend;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700506 @Override
507 public void onPageStarted(WebView view, String url, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700508 mInPageLoad = true;
John Reck30c714c2010-12-16 17:30:34 -0800509 mPageLoadProgress = 0;
510 mCurrentState = new PageState(mActivity,
511 view.isPrivateBrowsingEnabled(), url, favicon);
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000512 mLoadStartTime = SystemClock.uptimeMillis();
Leon Scroggins58d56c62010-01-28 15:12:40 -0500513 if (mVoiceSearchData != null
514 && !url.equals(mVoiceSearchData.mLastVoiceSearchUrl)) {
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500515 if (mVoiceSearchData.mSourceIsGoogle) {
516 Intent i = new Intent(LoggingEvents.ACTION_LOG_EVENT);
517 i.putExtra(LoggingEvents.EXTRA_FLUSH, true);
518 mActivity.sendBroadcast(i);
519 }
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400520 revertVoiceSearchMode();
Leon Scroggins58d56c62010-01-28 15:12:40 -0500521 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700522
Grace Kloba22ac16e2009-10-07 18:00:23 -0700523
524 // If we start a touch icon load and then load a new page, we don't
525 // want to cancel the current touch icon loader. But, we do want to
526 // create a new one when the touch icon url is known.
527 if (mTouchIconLoader != null) {
528 mTouchIconLoader.mTab = null;
529 mTouchIconLoader = null;
530 }
531
532 // reset the error console
533 if (mErrorConsole != null) {
534 mErrorConsole.clearErrorMessages();
Michael Kolb8233fac2010-10-26 16:08:53 -0700535 if (mWebViewController.shouldShowErrorConsole()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700536 mErrorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
537 }
538 }
539
Patrick Scott92066772011-03-10 08:46:27 -0500540 // Cancel the auto-login process.
541 if (mDeviceAccountLogin != null) {
542 mDeviceAccountLogin.cancel();
543 mDeviceAccountLogin = null;
544 mWebViewController.hideAutoLogin(Tab.this);
545 }
546
Grace Kloba22ac16e2009-10-07 18:00:23 -0700547 // finally update the UI in the activity if it is in the foreground
John Reck324d4402011-01-11 16:56:42 -0800548 mWebViewController.onPageStarted(Tab.this, view, favicon);
Leon Scroggins4cd97792010-12-03 15:31:56 -0500549
John Recke969cc52010-12-21 17:24:43 -0800550 updateBookmarkedStatus();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700551 }
552
553 @Override
554 public void onPageFinished(WebView view, String url) {
John Reck5b691842010-11-29 11:21:13 -0800555 if (!isPrivateBrowsingEnabled()) {
556 LogTag.logPageFinishedLoading(
557 url, SystemClock.uptimeMillis() - mLoadStartTime);
558 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700559 mInPageLoad = false;
John Reck30c714c2010-12-16 17:30:34 -0800560 // Sync state (in case of stop/timeout)
561 mCurrentState.mUrl = view.getUrl();
John Reck6c702ee2011-01-07 09:41:53 -0800562 if (mCurrentState.mUrl == null) {
563 mCurrentState.mUrl = url != null ? url : "";
564 }
John Reck30c714c2010-12-16 17:30:34 -0800565 mCurrentState.mTitle = view.getTitle();
566 mCurrentState.mFavicon = view.getFavicon();
567 if (!URLUtil.isHttpsUrl(mCurrentState.mUrl)) {
568 // In case we stop when loading an HTTPS page from an HTTP page
569 // but before a provisional load occurred
570 mCurrentState.mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
571 }
John Reck324d4402011-01-11 16:56:42 -0800572 mWebViewController.onPageFinished(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700573 }
574
575 // return true if want to hijack the url to let another app to handle it
576 @Override
577 public boolean shouldOverrideUrlLoading(WebView view, String url) {
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400578 if (voiceSearchSourceIsGoogle()) {
579 // This method is called when the user clicks on a link.
580 // VoiceSearchMode is turned off when the user leaves the
581 // Google results page, so at this point the user must be on
582 // that page. If the user clicked a link on that page, assume
583 // that the voice search was effective, and broadcast an Intent
584 // so a receiver can take note of that fact.
585 Intent logIntent = new Intent(LoggingEvents.ACTION_LOG_EVENT);
586 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
587 LoggingEvents.VoiceSearch.RESULT_CLICKED);
588 mActivity.sendBroadcast(logIntent);
589 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700590 if (mInForeground) {
Michael Kolb18eb3772010-12-10 14:29:51 -0800591 return mWebViewController.shouldOverrideUrlLoading(Tab.this,
592 view, url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700593 } else {
594 return false;
595 }
596 }
597
598 /**
599 * Updates the lock icon. This method is called when we discover another
600 * resource to be loaded for this page (for example, javascript). While
601 * we update the icon type, we do not update the lock icon itself until
602 * we are done loading, it is slightly more secure this way.
603 */
604 @Override
605 public void onLoadResource(WebView view, String url) {
606 if (url != null && url.length() > 0) {
607 // It is only if the page claims to be secure that we may have
608 // to update the lock:
John Reck30c714c2010-12-16 17:30:34 -0800609 if (mCurrentState.mLockIcon == LockIcon.LOCK_ICON_SECURE) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700610 // If NOT a 'safe' url, change the lock to mixed content!
611 if (!(URLUtil.isHttpsUrl(url) || URLUtil.isDataUrl(url)
612 || URLUtil.isAboutUrl(url))) {
John Reck30c714c2010-12-16 17:30:34 -0800613 mCurrentState.mLockIcon = LockIcon.LOCK_ICON_MIXED;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700614 }
615 }
616 }
617 }
618
619 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700620 * Show a dialog informing the user of the network error reported by
621 * WebCore if it is in the foreground.
622 */
623 @Override
624 public void onReceivedError(WebView view, int errorCode,
625 String description, String failingUrl) {
626 if (errorCode != WebViewClient.ERROR_HOST_LOOKUP &&
627 errorCode != WebViewClient.ERROR_CONNECT &&
628 errorCode != WebViewClient.ERROR_BAD_URL &&
629 errorCode != WebViewClient.ERROR_UNSUPPORTED_SCHEME &&
630 errorCode != WebViewClient.ERROR_FILE) {
631 queueError(errorCode, description);
632 }
Jeff Hamilton47654f42010-09-07 09:57:51 -0500633
634 // Don't log URLs when in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -0700635 if (!isPrivateBrowsingEnabled()) {
Jeff Hamilton47654f42010-09-07 09:57:51 -0500636 Log.e(LOGTAG, "onReceivedError " + errorCode + " " + failingUrl
637 + " " + description);
638 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700639 }
640
641 /**
642 * Check with the user if it is ok to resend POST data as the page they
643 * are trying to navigate to is the result of a POST.
644 */
645 @Override
646 public void onFormResubmission(WebView view, final Message dontResend,
647 final Message resend) {
648 if (!mInForeground) {
649 dontResend.sendToTarget();
650 return;
651 }
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500652 if (mDontResend != null) {
653 Log.w(LOGTAG, "onFormResubmission should not be called again "
654 + "while dialog is still up");
655 dontResend.sendToTarget();
656 return;
657 }
658 mDontResend = dontResend;
659 mResend = resend;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700660 new AlertDialog.Builder(mActivity).setTitle(
661 R.string.browserFrameFormResubmitLabel).setMessage(
662 R.string.browserFrameFormResubmitMessage)
663 .setPositiveButton(R.string.ok,
664 new DialogInterface.OnClickListener() {
665 public void onClick(DialogInterface dialog,
666 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500667 if (mResend != null) {
668 mResend.sendToTarget();
669 mResend = null;
670 mDontResend = null;
671 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700672 }
673 }).setNegativeButton(R.string.cancel,
674 new DialogInterface.OnClickListener() {
675 public void onClick(DialogInterface dialog,
676 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500677 if (mDontResend != null) {
678 mDontResend.sendToTarget();
679 mResend = null;
680 mDontResend = null;
681 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700682 }
683 }).setOnCancelListener(new OnCancelListener() {
684 public void onCancel(DialogInterface dialog) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500685 if (mDontResend != null) {
686 mDontResend.sendToTarget();
687 mResend = null;
688 mDontResend = null;
689 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700690 }
691 }).show();
692 }
693
694 /**
695 * Insert the url into the visited history database.
696 * @param url The url to be inserted.
697 * @param isReload True if this url is being reloaded.
698 * FIXME: Not sure what to do when reloading the page.
699 */
700 @Override
701 public void doUpdateVisitedHistory(WebView view, String url,
702 boolean isReload) {
John Reck324d4402011-01-11 16:56:42 -0800703 mWebViewController.doUpdateVisitedHistory(Tab.this, isReload);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700704 }
705
706 /**
707 * Displays SSL error(s) dialog to the user.
708 */
709 @Override
710 public void onReceivedSslError(final WebView view,
711 final SslErrorHandler handler, final SslError error) {
712 if (!mInForeground) {
713 handler.cancel();
John Reck30c714c2010-12-16 17:30:34 -0800714 setLockIconType(LockIcon.LOCK_ICON_UNSECURE);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700715 return;
716 }
John Reck35e9dd62011-04-25 09:01:54 -0700717 if (mSettings.showSecurityWarnings()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700718 final LayoutInflater factory =
719 LayoutInflater.from(mActivity);
720 final View warningsView =
721 factory.inflate(R.layout.ssl_warnings, null);
722 final LinearLayout placeholder =
723 (LinearLayout)warningsView.findViewById(R.id.placeholder);
724
725 if (error.hasError(SslError.SSL_UNTRUSTED)) {
726 LinearLayout ll = (LinearLayout)factory
727 .inflate(R.layout.ssl_warning, null);
728 ((TextView)ll.findViewById(R.id.warning))
729 .setText(R.string.ssl_untrusted);
730 placeholder.addView(ll);
731 }
732
733 if (error.hasError(SslError.SSL_IDMISMATCH)) {
734 LinearLayout ll = (LinearLayout)factory
735 .inflate(R.layout.ssl_warning, null);
736 ((TextView)ll.findViewById(R.id.warning))
737 .setText(R.string.ssl_mismatch);
738 placeholder.addView(ll);
739 }
740
741 if (error.hasError(SslError.SSL_EXPIRED)) {
742 LinearLayout ll = (LinearLayout)factory
743 .inflate(R.layout.ssl_warning, null);
744 ((TextView)ll.findViewById(R.id.warning))
745 .setText(R.string.ssl_expired);
746 placeholder.addView(ll);
747 }
748
749 if (error.hasError(SslError.SSL_NOTYETVALID)) {
750 LinearLayout ll = (LinearLayout)factory
751 .inflate(R.layout.ssl_warning, null);
752 ((TextView)ll.findViewById(R.id.warning))
753 .setText(R.string.ssl_not_yet_valid);
754 placeholder.addView(ll);
755 }
756
757 new AlertDialog.Builder(mActivity).setTitle(
758 R.string.security_warning).setIcon(
759 android.R.drawable.ic_dialog_alert).setView(
760 warningsView).setPositiveButton(R.string.ssl_continue,
761 new DialogInterface.OnClickListener() {
762 public void onClick(DialogInterface dialog,
763 int whichButton) {
764 handler.proceed();
765 }
766 }).setNeutralButton(R.string.view_certificate,
767 new DialogInterface.OnClickListener() {
768 public void onClick(DialogInterface dialog,
769 int whichButton) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700770 mWebViewController.showSslCertificateOnError(view,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700771 handler, error);
772 }
Ben Murdocha49b8292010-11-16 11:56:04 +0000773 }).setNegativeButton(R.string.ssl_go_back,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700774 new DialogInterface.OnClickListener() {
775 public void onClick(DialogInterface dialog,
776 int whichButton) {
John Reck30c714c2010-12-16 17:30:34 -0800777 dialog.cancel();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700778 }
779 }).setOnCancelListener(
780 new DialogInterface.OnCancelListener() {
781 public void onCancel(DialogInterface dialog) {
782 handler.cancel();
John Reck30c714c2010-12-16 17:30:34 -0800783 setLockIconType(LockIcon.LOCK_ICON_UNSECURE);
784 mWebViewController.onUserCanceledSsl(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700785 }
786 }).show();
787 } else {
788 handler.proceed();
789 }
790 }
791
792 /**
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700793 * Displays client certificate request to the user.
794 */
795 @Override
796 public void onReceivedClientCertRequest(final WebView view,
797 final ClientCertRequestHandler handler, final String host_and_port) {
798 if (!mInForeground) {
799 handler.ignore();
800 return;
801 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700802 int colon = host_and_port.lastIndexOf(':');
803 String host;
804 int port;
805 if (colon == -1) {
806 host = host_and_port;
807 port = -1;
808 } else {
809 String portString = host_and_port.substring(colon + 1);
810 try {
811 port = Integer.parseInt(portString);
812 host = host_and_port.substring(0, colon);
813 } catch (NumberFormatException e) {
814 host = host_and_port;
815 port = -1;
816 }
817 }
Brian Carlstromaa09cd82011-06-09 16:04:40 -0700818 KeyChain.choosePrivateKeyAlias(mActivity, new KeyChainAliasCallback() {
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700819 @Override public void alias(String alias) {
820 if (alias == null) {
821 handler.cancel();
822 return;
823 }
824 new KeyChainLookup(mActivity, handler, alias).execute();
825 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700826 }, null, null, host, port, null);
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700827 }
828
829 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700830 * Handles an HTTP authentication request.
831 *
832 * @param handler The authentication handler
833 * @param host The host
834 * @param realm The realm
835 */
836 @Override
837 public void onReceivedHttpAuthRequest(WebView view,
838 final HttpAuthHandler handler, final String host,
839 final String realm) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700840 mWebViewController.onReceivedHttpAuthRequest(Tab.this, view, handler, host, realm);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700841 }
842
843 @Override
John Reck438bf462011-01-12 18:11:46 -0800844 public WebResourceResponse shouldInterceptRequest(WebView view,
845 String url) {
846 WebResourceResponse res = HomeProvider.shouldInterceptRequest(
847 mActivity, url);
848 return res;
849 }
850
851 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700852 public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
853 if (!mInForeground) {
854 return false;
855 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700856 return mWebViewController.shouldOverrideKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700857 }
858
859 @Override
860 public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700861 if (!mInForeground) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700862 return;
863 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700864 mWebViewController.onUnhandledKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700865 }
Patrick Scott92066772011-03-10 08:46:27 -0500866
867 @Override
868 public void onReceivedLoginRequest(WebView view, String realm,
869 String account, String args) {
870 new DeviceAccountLogin(mActivity, view, Tab.this, mWebViewController)
871 .handleLogin(realm, account, args);
872 }
873
Grace Kloba22ac16e2009-10-07 18:00:23 -0700874 };
875
Patrick Scott92066772011-03-10 08:46:27 -0500876 // Called by DeviceAccountLogin when the Tab needs to have the auto-login UI
877 // displayed.
878 void setDeviceAccountLogin(DeviceAccountLogin login) {
879 mDeviceAccountLogin = login;
880 }
881
882 // Returns non-null if the title bar should display the auto-login UI.
883 DeviceAccountLogin getDeviceAccountLogin() {
884 return mDeviceAccountLogin;
885 }
886
Grace Kloba22ac16e2009-10-07 18:00:23 -0700887 // -------------------------------------------------------------------------
888 // WebChromeClient implementation for the main WebView
889 // -------------------------------------------------------------------------
890
891 private final WebChromeClient mWebChromeClient = new WebChromeClient() {
892 // Helper method to create a new tab or sub window.
893 private void createWindow(final boolean dialog, final Message msg) {
894 WebView.WebViewTransport transport =
895 (WebView.WebViewTransport) msg.obj;
896 if (dialog) {
897 createSubWindow();
Michael Kolb8233fac2010-10-26 16:08:53 -0700898 mWebViewController.attachSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700899 transport.setWebView(mSubView);
900 } else {
Michael Kolb7bcafde2011-05-09 13:55:59 -0700901 final Tab newTab = mWebViewController.openTab(null,
John Reck5949c662011-05-27 09:52:29 -0700902 Tab.this, true, true);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700903 transport.setWebView(newTab.getWebView());
904 }
905 msg.sendToTarget();
906 }
907
908 @Override
909 public boolean onCreateWindow(WebView view, final boolean dialog,
910 final boolean userGesture, final Message resultMsg) {
911 // only allow new window or sub window for the foreground case
912 if (!mInForeground) {
913 return false;
914 }
915 // Short-circuit if we can't create any more tabs or sub windows.
916 if (dialog && mSubView != null) {
917 new AlertDialog.Builder(mActivity)
918 .setTitle(R.string.too_many_subwindows_dialog_title)
919 .setIcon(android.R.drawable.ic_dialog_alert)
920 .setMessage(R.string.too_many_subwindows_dialog_message)
921 .setPositiveButton(R.string.ok, null)
922 .show();
923 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700924 } else if (!mWebViewController.getTabControl().canCreateNewTab()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700925 new AlertDialog.Builder(mActivity)
926 .setTitle(R.string.too_many_windows_dialog_title)
927 .setIcon(android.R.drawable.ic_dialog_alert)
928 .setMessage(R.string.too_many_windows_dialog_message)
929 .setPositiveButton(R.string.ok, null)
930 .show();
931 return false;
932 }
933
934 // Short-circuit if this was a user gesture.
935 if (userGesture) {
936 createWindow(dialog, resultMsg);
937 return true;
938 }
939
940 // Allow the popup and create the appropriate window.
941 final AlertDialog.OnClickListener allowListener =
942 new AlertDialog.OnClickListener() {
943 public void onClick(DialogInterface d,
944 int which) {
945 createWindow(dialog, resultMsg);
946 }
947 };
948
949 // Block the popup by returning a null WebView.
950 final AlertDialog.OnClickListener blockListener =
951 new AlertDialog.OnClickListener() {
952 public void onClick(DialogInterface d, int which) {
953 resultMsg.sendToTarget();
954 }
955 };
956
957 // Build a confirmation dialog to display to the user.
958 final AlertDialog d =
959 new AlertDialog.Builder(mActivity)
960 .setTitle(R.string.attention)
961 .setIcon(android.R.drawable.ic_dialog_alert)
962 .setMessage(R.string.popup_window_attempt)
963 .setPositiveButton(R.string.allow, allowListener)
964 .setNegativeButton(R.string.block, blockListener)
965 .setCancelable(false)
966 .create();
967
968 // Show the confirmation dialog.
969 d.show();
970 return true;
971 }
972
973 @Override
Patrick Scotteb5061b2009-11-18 15:00:30 -0500974 public void onRequestFocus(WebView view) {
975 if (!mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700976 mWebViewController.switchToTab(Tab.this);
Patrick Scotteb5061b2009-11-18 15:00:30 -0500977 }
978 }
979
980 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700981 public void onCloseWindow(WebView window) {
Michael Kolbc831b632011-05-11 09:30:34 -0700982 if (mParent != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700983 // JavaScript can only close popup window.
984 if (mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700985 mWebViewController.switchToTab(mParent);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700986 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700987 mWebViewController.closeTab(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700988 }
989 }
990
991 @Override
992 public void onProgressChanged(WebView view, int newProgress) {
John Reck30c714c2010-12-16 17:30:34 -0800993 mPageLoadProgress = newProgress;
994 mWebViewController.onProgressChanged(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700995 }
996
997 @Override
Leon Scroggins21d9b902010-03-11 09:33:11 -0500998 public void onReceivedTitle(WebView view, final String title) {
John Reck30c714c2010-12-16 17:30:34 -0800999 mCurrentState.mTitle = title;
Michael Kolb8233fac2010-10-26 16:08:53 -07001000 mWebViewController.onReceivedTitle(Tab.this, title);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001001 }
1002
1003 @Override
1004 public void onReceivedIcon(WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -08001005 mCurrentState.mFavicon = icon;
Michael Kolb8233fac2010-10-26 16:08:53 -07001006 mWebViewController.onFavicon(Tab.this, view, icon);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001007 }
1008
1009 @Override
1010 public void onReceivedTouchIconUrl(WebView view, String url,
1011 boolean precomposed) {
1012 final ContentResolver cr = mActivity.getContentResolver();
Leon Scrogginsc8393d92010-04-23 14:58:16 -04001013 // Let precomposed icons take precedence over non-composed
1014 // icons.
1015 if (precomposed && mTouchIconLoader != null) {
1016 mTouchIconLoader.cancel(false);
1017 mTouchIconLoader = null;
1018 }
1019 // Have only one async task at a time.
1020 if (mTouchIconLoader == null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001021 mTouchIconLoader = new DownloadTouchIcon(Tab.this,
1022 mActivity, cr, view);
Leon Scrogginsc8393d92010-04-23 14:58:16 -04001023 mTouchIconLoader.execute(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001024 }
1025 }
1026
1027 @Override
1028 public void onShowCustomView(View view,
1029 WebChromeClient.CustomViewCallback callback) {
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001030 onShowCustomView(view, mActivity.getRequestedOrientation(), callback);
1031 }
1032
1033 @Override
1034 public void onShowCustomView(View view, int requestedOrientation,
1035 WebChromeClient.CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001036 if (mInForeground) mWebViewController.showCustomView(Tab.this, view,
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001037 requestedOrientation, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001038 }
1039
1040 @Override
1041 public void onHideCustomView() {
Michael Kolb8233fac2010-10-26 16:08:53 -07001042 if (mInForeground) mWebViewController.hideCustomView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001043 }
1044
1045 /**
1046 * The origin has exceeded its database quota.
1047 * @param url the URL that exceeded the quota
1048 * @param databaseIdentifier the identifier of the database on which the
1049 * transaction that caused the quota overflow was run
1050 * @param currentQuota the current quota for the origin.
1051 * @param estimatedSize the estimated size of the database.
1052 * @param totalUsedQuota is the sum of all origins' quota.
1053 * @param quotaUpdater The callback to run when a decision to allow or
1054 * deny quota has been made. Don't forget to call this!
1055 */
1056 @Override
1057 public void onExceededDatabaseQuota(String url,
1058 String databaseIdentifier, long currentQuota, long estimatedSize,
1059 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -07001060 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001061 .onExceededDatabaseQuota(url, databaseIdentifier,
1062 currentQuota, estimatedSize, totalUsedQuota,
1063 quotaUpdater);
1064 }
1065
1066 /**
1067 * The Application Cache has exceeded its max size.
1068 * @param spaceNeeded is the amount of disk space that would be needed
1069 * in order for the last appcache operation to succeed.
1070 * @param totalUsedQuota is the sum of all origins' quota.
1071 * @param quotaUpdater A callback to inform the WebCore thread that a
1072 * new app cache size is available. This callback must always
1073 * be executed at some point to ensure that the sleeping
1074 * WebCore thread is woken up.
1075 */
1076 @Override
1077 public void onReachedMaxAppCacheSize(long spaceNeeded,
1078 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -07001079 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001080 .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota,
1081 quotaUpdater);
1082 }
1083
1084 /**
1085 * Instructs the browser to show a prompt to ask the user to set the
1086 * Geolocation permission state for the specified origin.
1087 * @param origin The origin for which Geolocation permissions are
1088 * requested.
1089 * @param callback The callback to call once the user has set the
1090 * Geolocation permission state.
1091 */
1092 @Override
1093 public void onGeolocationPermissionsShowPrompt(String origin,
1094 GeolocationPermissions.Callback callback) {
1095 if (mInForeground) {
Grace Kloba50c241e2010-04-20 11:07:50 -07001096 getGeolocationPermissionsPrompt().show(origin, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001097 }
1098 }
1099
1100 /**
1101 * Instructs the browser to hide the Geolocation permissions prompt.
1102 */
1103 @Override
1104 public void onGeolocationPermissionsHidePrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001105 if (mInForeground && mGeolocationPermissionsPrompt != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001106 mGeolocationPermissionsPrompt.hide();
1107 }
1108 }
1109
Ben Murdoch65acc352009-11-19 18:16:04 +00001110 /* Adds a JavaScript error message to the system log and if the JS
1111 * console is enabled in the about:debug options, to that console
1112 * also.
Ben Murdochc42addf2010-01-28 15:19:59 +00001113 * @param consoleMessage the message object.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001114 */
1115 @Override
Ben Murdochc42addf2010-01-28 15:19:59 +00001116 public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001117 if (mInForeground) {
1118 // call getErrorConsole(true) so it will create one if needed
1119 ErrorConsoleView errorConsole = getErrorConsole(true);
Ben Murdochc42addf2010-01-28 15:19:59 +00001120 errorConsole.addErrorMessage(consoleMessage);
Michael Kolb8233fac2010-10-26 16:08:53 -07001121 if (mWebViewController.shouldShowErrorConsole()
1122 && errorConsole.getShowState() !=
1123 ErrorConsoleView.SHOW_MAXIMIZED) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001124 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
1125 }
1126 }
Ben Murdochc42addf2010-01-28 15:19:59 +00001127
Jeff Hamilton47654f42010-09-07 09:57:51 -05001128 // Don't log console messages in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001129 if (isPrivateBrowsingEnabled()) return true;
Jeff Hamilton47654f42010-09-07 09:57:51 -05001130
Ben Murdochc42addf2010-01-28 15:19:59 +00001131 String message = "Console: " + consoleMessage.message() + " "
1132 + consoleMessage.sourceId() + ":"
1133 + consoleMessage.lineNumber();
1134
1135 switch (consoleMessage.messageLevel()) {
1136 case TIP:
1137 Log.v(CONSOLE_LOGTAG, message);
1138 break;
1139 case LOG:
1140 Log.i(CONSOLE_LOGTAG, message);
1141 break;
1142 case WARNING:
1143 Log.w(CONSOLE_LOGTAG, message);
1144 break;
1145 case ERROR:
1146 Log.e(CONSOLE_LOGTAG, message);
1147 break;
1148 case DEBUG:
1149 Log.d(CONSOLE_LOGTAG, message);
1150 break;
1151 }
1152
1153 return true;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001154 }
1155
1156 /**
1157 * Ask the browser for an icon to represent a <video> element.
1158 * This icon will be used if the Web page did not specify a poster attribute.
1159 * @return Bitmap The icon or null if no such icon is available.
1160 */
1161 @Override
1162 public Bitmap getDefaultVideoPoster() {
1163 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001164 return mWebViewController.getDefaultVideoPoster();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001165 }
1166 return null;
1167 }
1168
1169 /**
1170 * Ask the host application for a custom progress view to show while
1171 * a <video> is loading.
1172 * @return View The progress view.
1173 */
1174 @Override
1175 public View getVideoLoadingProgressView() {
1176 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001177 return mWebViewController.getVideoLoadingProgressView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001178 }
1179 return null;
1180 }
1181
1182 @Override
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01001183 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001184 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001185 mWebViewController.openFileChooser(uploadMsg, acceptType);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001186 } else {
1187 uploadMsg.onReceiveValue(null);
1188 }
1189 }
1190
1191 /**
1192 * Deliver a list of already-visited URLs
1193 */
1194 @Override
1195 public void getVisitedHistory(final ValueCallback<String[]> callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001196 mWebViewController.getVisitedHistory(callback);
1197 }
Ben Murdoch8029a772010-11-16 11:58:21 +00001198
1199 @Override
1200 public void setupAutoFill(Message message) {
1201 // Prompt the user to set up their profile.
1202 final Message msg = message;
1203 AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
Ben Murdoch1d676b62011-01-17 12:54:24 +00001204 LayoutInflater inflater = (LayoutInflater) mActivity.getSystemService(
1205 Context.LAYOUT_INFLATER_SERVICE);
1206 final View layout = inflater.inflate(R.layout.setup_autofill_dialog, null);
1207
1208 builder.setView(layout)
1209 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1210 @Override
1211 public void onClick(DialogInterface dialog, int id) {
1212 CheckBox disableAutoFill = (CheckBox) layout.findViewById(
1213 R.id.setup_autofill_dialog_disable_autofill);
1214
1215 if (disableAutoFill.isChecked()) {
1216 // Disable autofill and show a toast with how to turn it on again.
John Reck35e9dd62011-04-25 09:01:54 -07001217 mSettings.setAutofillEnabled(false);
Ben Murdoch1d676b62011-01-17 12:54:24 +00001218 Toast.makeText(mActivity,
1219 R.string.autofill_setup_dialog_negative_toast,
1220 Toast.LENGTH_LONG).show();
1221 } else {
1222 // Take user to the AutoFill profile editor. When they return,
1223 // we will send the message that we pass here which will trigger
1224 // the form to get filled out with their new profile.
1225 mWebViewController.setupAutoFill(msg);
1226 }
1227 }
1228 })
1229 .setNegativeButton(R.string.cancel, null)
1230 .show();
Ben Murdoch8029a772010-11-16 11:58:21 +00001231 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001232 };
1233
1234 // -------------------------------------------------------------------------
1235 // WebViewClient implementation for the sub window
1236 // -------------------------------------------------------------------------
1237
1238 // Subclass of WebViewClient used in subwindows to notify the main
1239 // WebViewClient of certain WebView activities.
1240 private static class SubWindowClient extends WebViewClient {
1241 // The main WebViewClient.
1242 private final WebViewClient mClient;
Michael Kolb8233fac2010-10-26 16:08:53 -07001243 private final WebViewController mController;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001244
Michael Kolb8233fac2010-10-26 16:08:53 -07001245 SubWindowClient(WebViewClient client, WebViewController controller) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001246 mClient = client;
Michael Kolb8233fac2010-10-26 16:08:53 -07001247 mController = controller;
Leon Scroggins III211ba542010-04-19 13:21:13 -04001248 }
1249 @Override
1250 public void onPageStarted(WebView view, String url, Bitmap favicon) {
1251 // Unlike the others, do not call mClient's version, which would
1252 // change the progress bar. However, we do want to remove the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001253 // find or select dialog.
Michael Kolb8233fac2010-10-26 16:08:53 -07001254 mController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001255 }
1256 @Override
1257 public void doUpdateVisitedHistory(WebView view, String url,
1258 boolean isReload) {
1259 mClient.doUpdateVisitedHistory(view, url, isReload);
1260 }
1261 @Override
1262 public boolean shouldOverrideUrlLoading(WebView view, String url) {
1263 return mClient.shouldOverrideUrlLoading(view, url);
1264 }
1265 @Override
1266 public void onReceivedSslError(WebView view, SslErrorHandler handler,
1267 SslError error) {
1268 mClient.onReceivedSslError(view, handler, error);
1269 }
1270 @Override
Brian Carlstrom8862c1d2011-06-02 01:05:55 -07001271 public void onReceivedClientCertRequest(WebView view,
1272 ClientCertRequestHandler handler, String host_and_port) {
1273 mClient.onReceivedClientCertRequest(view, handler, host_and_port);
1274 }
1275 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -07001276 public void onReceivedHttpAuthRequest(WebView view,
1277 HttpAuthHandler handler, String host, String realm) {
1278 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
1279 }
1280 @Override
1281 public void onFormResubmission(WebView view, Message dontResend,
1282 Message resend) {
1283 mClient.onFormResubmission(view, dontResend, resend);
1284 }
1285 @Override
1286 public void onReceivedError(WebView view, int errorCode,
1287 String description, String failingUrl) {
1288 mClient.onReceivedError(view, errorCode, description, failingUrl);
1289 }
1290 @Override
1291 public boolean shouldOverrideKeyEvent(WebView view,
1292 android.view.KeyEvent event) {
1293 return mClient.shouldOverrideKeyEvent(view, event);
1294 }
1295 @Override
1296 public void onUnhandledKeyEvent(WebView view,
1297 android.view.KeyEvent event) {
1298 mClient.onUnhandledKeyEvent(view, event);
1299 }
1300 }
1301
1302 // -------------------------------------------------------------------------
1303 // WebChromeClient implementation for the sub window
1304 // -------------------------------------------------------------------------
1305
1306 private class SubWindowChromeClient extends WebChromeClient {
1307 // The main WebChromeClient.
1308 private final WebChromeClient mClient;
1309
1310 SubWindowChromeClient(WebChromeClient client) {
1311 mClient = client;
1312 }
1313 @Override
1314 public void onProgressChanged(WebView view, int newProgress) {
1315 mClient.onProgressChanged(view, newProgress);
1316 }
1317 @Override
1318 public boolean onCreateWindow(WebView view, boolean dialog,
1319 boolean userGesture, android.os.Message resultMsg) {
1320 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
1321 }
1322 @Override
1323 public void onCloseWindow(WebView window) {
1324 if (window != mSubView) {
1325 Log.e(LOGTAG, "Can't close the window");
1326 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001327 mWebViewController.dismissSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001328 }
1329 }
1330
1331 // -------------------------------------------------------------------------
1332
Michael Kolb8233fac2010-10-26 16:08:53 -07001333 // TODO temporarily use activity here
1334 // remove later
1335
Grace Kloba22ac16e2009-10-07 18:00:23 -07001336 // Construct a new tab
Michael Kolb7bcafde2011-05-09 13:55:59 -07001337 Tab(WebViewController wvcontroller, WebView w) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001338 mWebViewController = wvcontroller;
1339 mActivity = mWebViewController.getActivity();
John Reck35e9dd62011-04-25 09:01:54 -07001340 mSettings = BrowserSettings.getInstance();
John Recke969cc52010-12-21 17:24:43 -08001341 mDataController = DataController.getInstance(mActivity);
John Reck847b5322011-04-14 17:02:18 -07001342 mCurrentState = new PageState(mActivity, w != null
1343 ? w.isPrivateBrowsingEnabled() : false);
Michael Kolb8233fac2010-10-26 16:08:53 -07001344 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001345 mInForeground = false;
1346
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001347 mDownloadListener = new DownloadListener() {
1348 public void onDownloadStart(String url, String userAgent,
1349 String contentDisposition, String mimetype,
1350 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001351 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001352 mimetype, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001353 }
1354 };
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001355 mWebBackForwardListClient = new WebBackForwardListClient() {
1356 @Override
1357 public void onNewHistoryItem(WebHistoryItem item) {
1358 if (isInVoiceSearchMode()) {
1359 item.setCustomData(mVoiceSearchData.mVoiceSearchIntent);
1360 }
1361 }
1362 @Override
1363 public void onIndexChanged(WebHistoryItem item, int index) {
1364 Object data = item.getCustomData();
1365 if (data != null && data instanceof Intent) {
1366 activateVoiceSearchMode((Intent) data);
1367 }
1368 }
1369 };
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001370
Grace Kloba22ac16e2009-10-07 18:00:23 -07001371 setWebView(w);
1372 }
1373
Michael Kolbc831b632011-05-11 09:30:34 -07001374 public void setId(long id) {
1375 mId = id;
1376 }
1377
1378 public long getId() {
1379 return mId;
1380 }
1381
Grace Kloba22ac16e2009-10-07 18:00:23 -07001382 /**
1383 * Sets the WebView for this tab, correctly removing the old WebView from
1384 * the container view.
1385 */
1386 void setWebView(WebView w) {
1387 if (mMainView == w) {
1388 return;
1389 }
Michael Kolba713ec82010-11-29 17:27:06 -08001390
Grace Kloba22ac16e2009-10-07 18:00:23 -07001391 // If the WebView is changing, the page will be reloaded, so any ongoing
1392 // Geolocation permission requests are void.
Grace Kloba50c241e2010-04-20 11:07:50 -07001393 if (mGeolocationPermissionsPrompt != null) {
1394 mGeolocationPermissionsPrompt.hide();
1395 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001396
Michael Kolba713ec82010-11-29 17:27:06 -08001397 mWebViewController.onSetWebView(this, w);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001398
1399 // set the new one
1400 mMainView = w;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001401 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001402 if (mMainView != null) {
1403 mMainView.setWebViewClient(mWebViewClient);
1404 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001405 // Attach DownloadManager so that downloads can start in an active
1406 // or a non-active window. This can happen when going to a site that
1407 // does a redirect after a period of time. The user could have
1408 // switched to another tab while waiting for the download to start.
1409 mMainView.setDownloadListener(mDownloadListener);
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001410 mMainView.setWebBackForwardListClient(mWebBackForwardListClient);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001411 }
1412 }
1413
1414 /**
1415 * Destroy the tab's main WebView and subWindow if any
1416 */
1417 void destroy() {
1418 if (mMainView != null) {
1419 dismissSubWindow();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001420 // save the WebView to call destroy() after detach it from the tab
1421 WebView webView = mMainView;
1422 setWebView(null);
1423 webView.destroy();
1424 }
1425 }
1426
1427 /**
1428 * Remove the tab from the parent
1429 */
1430 void removeFromTree() {
1431 // detach the children
Michael Kolbc831b632011-05-11 09:30:34 -07001432 if (mChildren != null) {
1433 for(Tab t : mChildren) {
1434 t.setParent(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001435 }
1436 }
1437 // remove itself from the parent list
Michael Kolbc831b632011-05-11 09:30:34 -07001438 if (mParent != null) {
1439 mParent.mChildren.remove(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001440 }
1441 }
1442
1443 /**
1444 * Create a new subwindow unless a subwindow already exists.
1445 * @return True if a new subwindow was created. False if one already exists.
1446 */
1447 boolean createSubWindow() {
1448 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001449 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001450 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001451 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001452 mSubView.setWebChromeClient(new SubWindowChromeClient(
1453 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001454 // Set a different DownloadListener for the mSubView, since it will
1455 // just need to dismiss the mSubView, rather than close the Tab
1456 mSubView.setDownloadListener(new DownloadListener() {
1457 public void onDownloadStart(String url, String userAgent,
1458 String contentDisposition, String mimetype,
1459 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001460 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001461 contentDisposition, mimetype, contentLength);
1462 if (mSubView.copyBackForwardList().getSize() == 0) {
1463 // This subwindow was opened for the sole purpose of
1464 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001465 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001466 }
1467 }
1468 });
Grace Kloba22ac16e2009-10-07 18:00:23 -07001469 mSubView.setOnCreateContextMenuListener(mActivity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001470 return true;
1471 }
1472 return false;
1473 }
1474
1475 /**
1476 * Dismiss the subWindow for the tab.
1477 */
1478 void dismissSubWindow() {
1479 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001480 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001481 mSubView.destroy();
1482 mSubView = null;
1483 mSubViewContainer = null;
1484 }
1485 }
1486
Grace Kloba22ac16e2009-10-07 18:00:23 -07001487
1488 /**
1489 * Set the parent tab of this tab.
1490 */
Michael Kolbc831b632011-05-11 09:30:34 -07001491 void setParent(Tab parent) {
1492 mParent = parent;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001493 // This tab may have been freed due to low memory. If that is the case,
Michael Kolbc831b632011-05-11 09:30:34 -07001494 // the parent tab id is already saved. If we are changing that id
Grace Kloba22ac16e2009-10-07 18:00:23 -07001495 // (most likely due to removing the parent tab) we must update the
Michael Kolbc831b632011-05-11 09:30:34 -07001496 // parent tab id in the saved Bundle.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001497 if (mSavedState != null) {
1498 if (parent == null) {
1499 mSavedState.remove(PARENTTAB);
1500 } else {
Michael Kolbc831b632011-05-11 09:30:34 -07001501 mSavedState.putLong(PARENTTAB, parent.getId());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001502 }
1503 }
John Reckb0a86db2011-05-24 14:05:58 -07001504
1505 // Sync the WebView useragent with the parent
1506 if (parent != null && mSettings.hasDesktopUseragent(parent.getWebView())
1507 != mSettings.hasDesktopUseragent(getWebView())) {
1508 mSettings.toggleDesktopUseragent(getWebView());
1509 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001510 }
1511
1512 /**
Michael Kolbc831b632011-05-11 09:30:34 -07001513 * If this Tab was created through another Tab, then this method returns
1514 * that Tab.
1515 * @return the Tab parent or null
1516 */
1517 public Tab getParent() {
1518 return mParent;
1519 }
1520
1521 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001522 * When a Tab is created through the content of another Tab, then we
1523 * associate the Tabs.
1524 * @param child the Tab that was created from this Tab
1525 */
1526 void addChildTab(Tab child) {
Michael Kolbc831b632011-05-11 09:30:34 -07001527 if (mChildren == null) {
1528 mChildren = new Vector<Tab>();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001529 }
Michael Kolbc831b632011-05-11 09:30:34 -07001530 mChildren.add(child);
1531 child.setParent(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001532 }
1533
Michael Kolbc831b632011-05-11 09:30:34 -07001534 Vector<Tab> getChildren() {
1535 return mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001536 }
1537
1538 void resume() {
1539 if (mMainView != null) {
1540 mMainView.onResume();
1541 if (mSubView != null) {
1542 mSubView.onResume();
1543 }
1544 }
1545 }
1546
1547 void pause() {
1548 if (mMainView != null) {
1549 mMainView.onPause();
1550 if (mSubView != null) {
1551 mSubView.onPause();
1552 }
1553 }
1554 }
1555
1556 void putInForeground() {
1557 mInForeground = true;
1558 resume();
1559 mMainView.setOnCreateContextMenuListener(mActivity);
1560 if (mSubView != null) {
1561 mSubView.setOnCreateContextMenuListener(mActivity);
1562 }
1563 // Show the pending error dialog if the queue is not empty
1564 if (mQueuedErrors != null && mQueuedErrors.size() > 0) {
1565 showError(mQueuedErrors.getFirst());
1566 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001567 mWebViewController.bookmarkedStatusHasChanged(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001568 }
1569
1570 void putInBackground() {
1571 mInForeground = false;
1572 pause();
1573 mMainView.setOnCreateContextMenuListener(null);
1574 if (mSubView != null) {
1575 mSubView.setOnCreateContextMenuListener(null);
1576 }
1577 }
1578
Michael Kolb8233fac2010-10-26 16:08:53 -07001579 boolean inForeground() {
1580 return mInForeground;
1581 }
1582
Grace Kloba22ac16e2009-10-07 18:00:23 -07001583 /**
1584 * Return the top window of this tab; either the subwindow if it is not
1585 * null or the main window.
1586 * @return The top window of this tab.
1587 */
1588 WebView getTopWindow() {
1589 if (mSubView != null) {
1590 return mSubView;
1591 }
1592 return mMainView;
1593 }
1594
1595 /**
1596 * Return the main window of this tab. Note: if a tab is freed in the
1597 * background, this can return null. It is only guaranteed to be
1598 * non-null for the current tab.
1599 * @return The main WebView of this tab.
1600 */
1601 WebView getWebView() {
1602 return mMainView;
1603 }
1604
Michael Kolba713ec82010-11-29 17:27:06 -08001605 void setViewContainer(View container) {
1606 mContainer = container;
1607 }
1608
Michael Kolb8233fac2010-10-26 16:08:53 -07001609 View getViewContainer() {
1610 return mContainer;
1611 }
1612
Grace Kloba22ac16e2009-10-07 18:00:23 -07001613 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001614 * Return whether private browsing is enabled for the main window of
1615 * this tab.
1616 * @return True if private browsing is enabled.
1617 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001618 boolean isPrivateBrowsingEnabled() {
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001619 WebView webView = getWebView();
1620 if (webView == null) {
1621 return false;
1622 }
1623 return webView.isPrivateBrowsingEnabled();
1624 }
1625
1626 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001627 * Return the subwindow of this tab or null if there is no subwindow.
1628 * @return The subwindow of this tab or null.
1629 */
1630 WebView getSubWebView() {
1631 return mSubView;
1632 }
1633
Michael Kolb1514bb72010-11-22 09:11:48 -08001634 void setSubWebView(WebView subView) {
1635 mSubView = subView;
1636 }
1637
Michael Kolb8233fac2010-10-26 16:08:53 -07001638 View getSubViewContainer() {
1639 return mSubViewContainer;
1640 }
1641
Michael Kolb1514bb72010-11-22 09:11:48 -08001642 void setSubViewContainer(View subViewContainer) {
1643 mSubViewContainer = subViewContainer;
1644 }
1645
Grace Kloba22ac16e2009-10-07 18:00:23 -07001646 /**
1647 * @return The geolocation permissions prompt for this tab.
1648 */
1649 GeolocationPermissionsPrompt getGeolocationPermissionsPrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001650 if (mGeolocationPermissionsPrompt == null) {
1651 ViewStub stub = (ViewStub) mContainer
1652 .findViewById(R.id.geolocation_permissions_prompt);
1653 mGeolocationPermissionsPrompt = (GeolocationPermissionsPrompt) stub
1654 .inflate();
1655 mGeolocationPermissionsPrompt.init();
1656 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001657 return mGeolocationPermissionsPrompt;
1658 }
1659
1660 /**
1661 * @return The application id string
1662 */
1663 String getAppId() {
1664 return mAppId;
1665 }
1666
1667 /**
1668 * Set the application id string
1669 * @param id
1670 */
1671 void setAppId(String id) {
1672 mAppId = id;
1673 }
1674
Grace Kloba22ac16e2009-10-07 18:00:23 -07001675 String getUrl() {
John Reck324d4402011-01-11 16:56:42 -08001676 return UrlUtils.filteredUrl(mCurrentState.mUrl);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001677 }
1678
John Reck49a603c2011-03-03 09:33:05 -08001679 String getOriginalUrl() {
1680 if (mMainView == null) {
1681 return "";
1682 }
1683 return UrlUtils.filteredUrl(mMainView.getOriginalUrl());
1684 }
1685
Grace Kloba22ac16e2009-10-07 18:00:23 -07001686 /**
John Reck30c714c2010-12-16 17:30:34 -08001687 * Get the title of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001688 */
1689 String getTitle() {
John Reck30c714c2010-12-16 17:30:34 -08001690 if (mCurrentState.mTitle == null && mInPageLoad) {
1691 return mActivity.getString(R.string.title_bar_loading);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001692 }
John Reck30c714c2010-12-16 17:30:34 -08001693 return mCurrentState.mTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001694 }
1695
1696 /**
John Reck30c714c2010-12-16 17:30:34 -08001697 * Get the favicon of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001698 */
1699 Bitmap getFavicon() {
John Reck30c714c2010-12-16 17:30:34 -08001700 return mCurrentState.mFavicon;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001701 }
1702
John Recke969cc52010-12-21 17:24:43 -08001703 public boolean isBookmarkedSite() {
1704 return mCurrentState.mIsBookmarkedSite;
1705 }
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001706
Grace Kloba22ac16e2009-10-07 18:00:23 -07001707 /**
1708 * Return the tab's error console. Creates the console if createIfNEcessary
1709 * is true and we haven't already created the console.
1710 * @param createIfNecessary Flag to indicate if the console should be
1711 * created if it has not been already.
1712 * @return The tab's error console, or null if one has not been created and
1713 * createIfNecessary is false.
1714 */
1715 ErrorConsoleView getErrorConsole(boolean createIfNecessary) {
1716 if (createIfNecessary && mErrorConsole == null) {
1717 mErrorConsole = new ErrorConsoleView(mActivity);
1718 mErrorConsole.setWebView(mMainView);
1719 }
1720 return mErrorConsole;
1721 }
1722
John Reck30c714c2010-12-16 17:30:34 -08001723 private void setLockIconType(LockIcon icon) {
1724 mCurrentState.mLockIcon = icon;
1725 mWebViewController.onUpdatedLockIcon(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001726 }
1727
1728 /**
1729 * @return The tab's lock icon type.
1730 */
John Reck30c714c2010-12-16 17:30:34 -08001731 LockIcon getLockIconType() {
1732 return mCurrentState.mLockIcon;
1733 }
1734
1735 int getLoadProgress() {
1736 if (mInPageLoad) {
1737 return mPageLoadProgress;
1738 }
1739 return 100;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001740 }
1741
1742 /**
1743 * @return TRUE if onPageStarted is called while onPageFinished is not
1744 * called yet.
1745 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001746 boolean inPageLoad() {
1747 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001748 }
1749
1750 // force mInLoad to be false. This should only be called before closing the
1751 // tab to ensure BrowserActivity's pauseWebViewTimers() is called correctly.
Michael Kolb8233fac2010-10-26 16:08:53 -07001752 void clearInPageLoad() {
1753 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001754 }
1755
Grace Kloba22ac16e2009-10-07 18:00:23 -07001756 /**
John Reck30c714c2010-12-16 17:30:34 -08001757 * Get the cached saved state bundle.
1758 * @return cached state bundle
Grace Kloba22ac16e2009-10-07 18:00:23 -07001759 */
1760 Bundle getSavedState() {
1761 return mSavedState;
1762 }
1763
John Reckaed9c542011-05-27 16:08:53 -07001764 Bundle getSavedState(boolean saveImages) {
1765 if (saveImages && mScreenshot != null) {
1766 Bundle b = new Bundle(mSavedState);
1767 b.putParcelable(SCREENSHOT, mScreenshot);
1768 return b;
1769 }
1770 return mSavedState;
1771 }
1772
Grace Kloba22ac16e2009-10-07 18:00:23 -07001773 /**
1774 * Set the saved state.
1775 */
1776 void setSavedState(Bundle state) {
1777 mSavedState = state;
1778 }
1779
1780 /**
1781 * @return TRUE if succeed in saving the state.
1782 */
1783 boolean saveState() {
1784 // If the WebView is null it means we ran low on memory and we already
1785 // stored the saved state in mSavedState.
1786 if (mMainView == null) {
1787 return mSavedState != null;
1788 }
John Reck24f18262011-06-17 14:47:20 -07001789 // If the tab is the homepage or has no URL, don't save it
1790 String homepage = BrowserSettings.getInstance().getHomePage();
1791 if (TextUtils.equals(homepage, mCurrentState.mUrl)
1792 || TextUtils.isEmpty(mCurrentState.mUrl)) {
1793 return false;
1794 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001795
1796 mSavedState = new Bundle();
John Reck541f55a2011-06-07 16:34:43 -07001797 mMainView.saveState(mSavedState);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001798
Michael Kolbc831b632011-05-11 09:30:34 -07001799 mSavedState.putLong(ID, mId);
John Reck30c714c2010-12-16 17:30:34 -08001800 mSavedState.putString(CURRURL, mCurrentState.mUrl);
1801 mSavedState.putString(CURRTITLE, mCurrentState.mTitle);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001802 if (mAppId != null) {
1803 mSavedState.putString(APPID, mAppId);
1804 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001805 // Remember the parent tab so the relationship can be restored.
Michael Kolbc831b632011-05-11 09:30:34 -07001806 if (mParent != null) {
1807 mSavedState.putLong(PARENTTAB, mParent.mId);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001808 }
John Reckb0a86db2011-05-24 14:05:58 -07001809 mSavedState.putBoolean(USERAGENT,
1810 mSettings.hasDesktopUseragent(getWebView()));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001811 return true;
1812 }
1813
1814 /*
1815 * Restore the state of the tab.
1816 */
1817 boolean restoreState(Bundle b) {
1818 if (b == null) {
1819 return false;
1820 }
1821 // Restore the internal state even if the WebView fails to restore.
1822 // This will maintain the app id, original url and close-on-exit values.
1823 mSavedState = null;
Michael Kolbc831b632011-05-11 09:30:34 -07001824 mId = b.getLong(ID);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001825 mAppId = b.getString(APPID);
Michael Kolbeb95db42011-03-03 10:38:40 -08001826 mScreenshot = b.getParcelable(SCREENSHOT);
John Reckb0a86db2011-05-24 14:05:58 -07001827 if (b.getBoolean(USERAGENT)
1828 != mSettings.hasDesktopUseragent(getWebView())) {
1829 mSettings.toggleDesktopUseragent(getWebView());
1830 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001831
1832 final WebBackForwardList list = mMainView.restoreState(b);
1833 if (list == null) {
1834 return false;
1835 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001836 return true;
1837 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04001838
Leon Scroggins1961ed22010-12-07 15:22:21 -05001839 public void updateBookmarkedStatus() {
John Recke969cc52010-12-21 17:24:43 -08001840 mDataController.queryBookmarkStatus(getUrl(), mIsBookmarkCallback);
Leon Scroggins1961ed22010-12-07 15:22:21 -05001841 }
1842
John Recke969cc52010-12-21 17:24:43 -08001843 private DataController.OnQueryUrlIsBookmark mIsBookmarkCallback
1844 = new DataController.OnQueryUrlIsBookmark() {
1845 @Override
1846 public void onQueryUrlIsBookmark(String url, boolean isBookmark) {
1847 if (mCurrentState.mUrl.equals(url)) {
1848 mCurrentState.mIsBookmarkedSite = isBookmark;
1849 mWebViewController.bookmarkedStatusHasChanged(Tab.this);
1850 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001851 }
John Recke969cc52010-12-21 17:24:43 -08001852 };
Michael Kolb1acef692011-03-08 14:12:06 -08001853
Michael Kolbeb95db42011-03-03 10:38:40 -08001854 public void setScreenshot(Bitmap screenshot) {
1855 mScreenshot = screenshot;
1856 }
1857
1858 public Bitmap getScreenshot() {
1859 return mScreenshot;
1860 }
1861
John Reck541f55a2011-06-07 16:34:43 -07001862 public boolean isSnapshot() {
John Reck541f55a2011-06-07 16:34:43 -07001863 return false;
1864 }
1865
John Reckd8c74522011-06-14 08:45:00 -07001866 public ContentValues createSnapshotValues() {
1867 if (mMainView == null) return null;
1868 ByteArrayOutputStream stream = new ByteArrayOutputStream();
1869 if (!mMainView.saveViewState(stream)) {
John Reck541f55a2011-06-07 16:34:43 -07001870 return null;
1871 }
John Reckd8c74522011-06-14 08:45:00 -07001872 byte[] data = stream.toByteArray();
John Reckd8c74522011-06-14 08:45:00 -07001873 ContentValues values = new ContentValues();
1874 values.put(Snapshots.TITLE, mCurrentState.mTitle);
1875 values.put(Snapshots.URL, mCurrentState.mUrl);
1876 values.put(Snapshots.VIEWSTATE, data);
1877 values.put(Snapshots.BACKGROUND, mMainView.getPageBackgroundColor());
1878 return values;
John Reck541f55a2011-06-07 16:34:43 -07001879 }
1880
John Reck26b18322011-06-21 13:08:58 -07001881 public void loadUrl(String url, Map<String, String> headers) {
1882 if (mMainView != null) {
1883 mCurrentState = new PageState(mActivity, false, url, null);
1884 mWebViewController.onPageStarted(this, mMainView, null);
1885 mMainView.loadUrl(url, headers);
1886 }
1887 }
1888
Grace Kloba22ac16e2009-10-07 18:00:23 -07001889}