blob: bc5868f29622718df86d6e239982ca22e1759633 [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 Carlstromaa09cd82011-06-09 16:04:40 -0700802 KeyChain.choosePrivateKeyAlias(mActivity, new KeyChainAliasCallback() {
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700803 @Override public void alias(String alias) {
804 if (alias == null) {
805 handler.cancel();
806 return;
807 }
808 new KeyChainLookup(mActivity, handler, alias).execute();
809 }
Brian Carlstromaa09cd82011-06-09 16:04:40 -0700810 }, null, null, null, -1);
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700811 }
812
813 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700814 * Handles an HTTP authentication request.
815 *
816 * @param handler The authentication handler
817 * @param host The host
818 * @param realm The realm
819 */
820 @Override
821 public void onReceivedHttpAuthRequest(WebView view,
822 final HttpAuthHandler handler, final String host,
823 final String realm) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700824 mWebViewController.onReceivedHttpAuthRequest(Tab.this, view, handler, host, realm);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700825 }
826
827 @Override
John Reck438bf462011-01-12 18:11:46 -0800828 public WebResourceResponse shouldInterceptRequest(WebView view,
829 String url) {
830 WebResourceResponse res = HomeProvider.shouldInterceptRequest(
831 mActivity, url);
832 return res;
833 }
834
835 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700836 public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
837 if (!mInForeground) {
838 return false;
839 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700840 return mWebViewController.shouldOverrideKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700841 }
842
843 @Override
844 public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700845 if (!mInForeground) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700846 return;
847 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700848 mWebViewController.onUnhandledKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700849 }
Patrick Scott92066772011-03-10 08:46:27 -0500850
851 @Override
852 public void onReceivedLoginRequest(WebView view, String realm,
853 String account, String args) {
854 new DeviceAccountLogin(mActivity, view, Tab.this, mWebViewController)
855 .handleLogin(realm, account, args);
856 }
857
Grace Kloba22ac16e2009-10-07 18:00:23 -0700858 };
859
Patrick Scott92066772011-03-10 08:46:27 -0500860 // Called by DeviceAccountLogin when the Tab needs to have the auto-login UI
861 // displayed.
862 void setDeviceAccountLogin(DeviceAccountLogin login) {
863 mDeviceAccountLogin = login;
864 }
865
866 // Returns non-null if the title bar should display the auto-login UI.
867 DeviceAccountLogin getDeviceAccountLogin() {
868 return mDeviceAccountLogin;
869 }
870
Grace Kloba22ac16e2009-10-07 18:00:23 -0700871 // -------------------------------------------------------------------------
872 // WebChromeClient implementation for the main WebView
873 // -------------------------------------------------------------------------
874
875 private final WebChromeClient mWebChromeClient = new WebChromeClient() {
876 // Helper method to create a new tab or sub window.
877 private void createWindow(final boolean dialog, final Message msg) {
878 WebView.WebViewTransport transport =
879 (WebView.WebViewTransport) msg.obj;
880 if (dialog) {
881 createSubWindow();
Michael Kolb8233fac2010-10-26 16:08:53 -0700882 mWebViewController.attachSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700883 transport.setWebView(mSubView);
884 } else {
Michael Kolb7bcafde2011-05-09 13:55:59 -0700885 final Tab newTab = mWebViewController.openTab(null,
John Reck5949c662011-05-27 09:52:29 -0700886 Tab.this, true, true);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700887 transport.setWebView(newTab.getWebView());
888 }
889 msg.sendToTarget();
890 }
891
892 @Override
893 public boolean onCreateWindow(WebView view, final boolean dialog,
894 final boolean userGesture, final Message resultMsg) {
895 // only allow new window or sub window for the foreground case
896 if (!mInForeground) {
897 return false;
898 }
899 // Short-circuit if we can't create any more tabs or sub windows.
900 if (dialog && mSubView != null) {
901 new AlertDialog.Builder(mActivity)
902 .setTitle(R.string.too_many_subwindows_dialog_title)
903 .setIcon(android.R.drawable.ic_dialog_alert)
904 .setMessage(R.string.too_many_subwindows_dialog_message)
905 .setPositiveButton(R.string.ok, null)
906 .show();
907 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700908 } else if (!mWebViewController.getTabControl().canCreateNewTab()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700909 new AlertDialog.Builder(mActivity)
910 .setTitle(R.string.too_many_windows_dialog_title)
911 .setIcon(android.R.drawable.ic_dialog_alert)
912 .setMessage(R.string.too_many_windows_dialog_message)
913 .setPositiveButton(R.string.ok, null)
914 .show();
915 return false;
916 }
917
918 // Short-circuit if this was a user gesture.
919 if (userGesture) {
920 createWindow(dialog, resultMsg);
921 return true;
922 }
923
924 // Allow the popup and create the appropriate window.
925 final AlertDialog.OnClickListener allowListener =
926 new AlertDialog.OnClickListener() {
927 public void onClick(DialogInterface d,
928 int which) {
929 createWindow(dialog, resultMsg);
930 }
931 };
932
933 // Block the popup by returning a null WebView.
934 final AlertDialog.OnClickListener blockListener =
935 new AlertDialog.OnClickListener() {
936 public void onClick(DialogInterface d, int which) {
937 resultMsg.sendToTarget();
938 }
939 };
940
941 // Build a confirmation dialog to display to the user.
942 final AlertDialog d =
943 new AlertDialog.Builder(mActivity)
944 .setTitle(R.string.attention)
945 .setIcon(android.R.drawable.ic_dialog_alert)
946 .setMessage(R.string.popup_window_attempt)
947 .setPositiveButton(R.string.allow, allowListener)
948 .setNegativeButton(R.string.block, blockListener)
949 .setCancelable(false)
950 .create();
951
952 // Show the confirmation dialog.
953 d.show();
954 return true;
955 }
956
957 @Override
Patrick Scotteb5061b2009-11-18 15:00:30 -0500958 public void onRequestFocus(WebView view) {
959 if (!mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700960 mWebViewController.switchToTab(Tab.this);
Patrick Scotteb5061b2009-11-18 15:00:30 -0500961 }
962 }
963
964 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700965 public void onCloseWindow(WebView window) {
Michael Kolbc831b632011-05-11 09:30:34 -0700966 if (mParent != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700967 // JavaScript can only close popup window.
968 if (mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700969 mWebViewController.switchToTab(mParent);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700970 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700971 mWebViewController.closeTab(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700972 }
973 }
974
975 @Override
976 public void onProgressChanged(WebView view, int newProgress) {
John Reck30c714c2010-12-16 17:30:34 -0800977 mPageLoadProgress = newProgress;
978 mWebViewController.onProgressChanged(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700979 }
980
981 @Override
Leon Scroggins21d9b902010-03-11 09:33:11 -0500982 public void onReceivedTitle(WebView view, final String title) {
John Reck30c714c2010-12-16 17:30:34 -0800983 mCurrentState.mTitle = title;
Michael Kolb8233fac2010-10-26 16:08:53 -0700984 mWebViewController.onReceivedTitle(Tab.this, title);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700985 }
986
987 @Override
988 public void onReceivedIcon(WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -0800989 mCurrentState.mFavicon = icon;
Michael Kolb8233fac2010-10-26 16:08:53 -0700990 mWebViewController.onFavicon(Tab.this, view, icon);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700991 }
992
993 @Override
994 public void onReceivedTouchIconUrl(WebView view, String url,
995 boolean precomposed) {
996 final ContentResolver cr = mActivity.getContentResolver();
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400997 // Let precomposed icons take precedence over non-composed
998 // icons.
999 if (precomposed && mTouchIconLoader != null) {
1000 mTouchIconLoader.cancel(false);
1001 mTouchIconLoader = null;
1002 }
1003 // Have only one async task at a time.
1004 if (mTouchIconLoader == null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001005 mTouchIconLoader = new DownloadTouchIcon(Tab.this,
1006 mActivity, cr, view);
Leon Scrogginsc8393d92010-04-23 14:58:16 -04001007 mTouchIconLoader.execute(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001008 }
1009 }
1010
1011 @Override
1012 public void onShowCustomView(View view,
1013 WebChromeClient.CustomViewCallback callback) {
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001014 onShowCustomView(view, mActivity.getRequestedOrientation(), callback);
1015 }
1016
1017 @Override
1018 public void onShowCustomView(View view, int requestedOrientation,
1019 WebChromeClient.CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001020 if (mInForeground) mWebViewController.showCustomView(Tab.this, view,
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001021 requestedOrientation, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001022 }
1023
1024 @Override
1025 public void onHideCustomView() {
Michael Kolb8233fac2010-10-26 16:08:53 -07001026 if (mInForeground) mWebViewController.hideCustomView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001027 }
1028
1029 /**
1030 * The origin has exceeded its database quota.
1031 * @param url the URL that exceeded the quota
1032 * @param databaseIdentifier the identifier of the database on which the
1033 * transaction that caused the quota overflow was run
1034 * @param currentQuota the current quota for the origin.
1035 * @param estimatedSize the estimated size of the database.
1036 * @param totalUsedQuota is the sum of all origins' quota.
1037 * @param quotaUpdater The callback to run when a decision to allow or
1038 * deny quota has been made. Don't forget to call this!
1039 */
1040 @Override
1041 public void onExceededDatabaseQuota(String url,
1042 String databaseIdentifier, long currentQuota, long estimatedSize,
1043 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -07001044 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001045 .onExceededDatabaseQuota(url, databaseIdentifier,
1046 currentQuota, estimatedSize, totalUsedQuota,
1047 quotaUpdater);
1048 }
1049
1050 /**
1051 * The Application Cache has exceeded its max size.
1052 * @param spaceNeeded is the amount of disk space that would be needed
1053 * in order for the last appcache operation to succeed.
1054 * @param totalUsedQuota is the sum of all origins' quota.
1055 * @param quotaUpdater A callback to inform the WebCore thread that a
1056 * new app cache size is available. This callback must always
1057 * be executed at some point to ensure that the sleeping
1058 * WebCore thread is woken up.
1059 */
1060 @Override
1061 public void onReachedMaxAppCacheSize(long spaceNeeded,
1062 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -07001063 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001064 .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota,
1065 quotaUpdater);
1066 }
1067
1068 /**
1069 * Instructs the browser to show a prompt to ask the user to set the
1070 * Geolocation permission state for the specified origin.
1071 * @param origin The origin for which Geolocation permissions are
1072 * requested.
1073 * @param callback The callback to call once the user has set the
1074 * Geolocation permission state.
1075 */
1076 @Override
1077 public void onGeolocationPermissionsShowPrompt(String origin,
1078 GeolocationPermissions.Callback callback) {
1079 if (mInForeground) {
Grace Kloba50c241e2010-04-20 11:07:50 -07001080 getGeolocationPermissionsPrompt().show(origin, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001081 }
1082 }
1083
1084 /**
1085 * Instructs the browser to hide the Geolocation permissions prompt.
1086 */
1087 @Override
1088 public void onGeolocationPermissionsHidePrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001089 if (mInForeground && mGeolocationPermissionsPrompt != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001090 mGeolocationPermissionsPrompt.hide();
1091 }
1092 }
1093
Ben Murdoch65acc352009-11-19 18:16:04 +00001094 /* Adds a JavaScript error message to the system log and if the JS
1095 * console is enabled in the about:debug options, to that console
1096 * also.
Ben Murdochc42addf2010-01-28 15:19:59 +00001097 * @param consoleMessage the message object.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001098 */
1099 @Override
Ben Murdochc42addf2010-01-28 15:19:59 +00001100 public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001101 if (mInForeground) {
1102 // call getErrorConsole(true) so it will create one if needed
1103 ErrorConsoleView errorConsole = getErrorConsole(true);
Ben Murdochc42addf2010-01-28 15:19:59 +00001104 errorConsole.addErrorMessage(consoleMessage);
Michael Kolb8233fac2010-10-26 16:08:53 -07001105 if (mWebViewController.shouldShowErrorConsole()
1106 && errorConsole.getShowState() !=
1107 ErrorConsoleView.SHOW_MAXIMIZED) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001108 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
1109 }
1110 }
Ben Murdochc42addf2010-01-28 15:19:59 +00001111
Jeff Hamilton47654f42010-09-07 09:57:51 -05001112 // Don't log console messages in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001113 if (isPrivateBrowsingEnabled()) return true;
Jeff Hamilton47654f42010-09-07 09:57:51 -05001114
Ben Murdochc42addf2010-01-28 15:19:59 +00001115 String message = "Console: " + consoleMessage.message() + " "
1116 + consoleMessage.sourceId() + ":"
1117 + consoleMessage.lineNumber();
1118
1119 switch (consoleMessage.messageLevel()) {
1120 case TIP:
1121 Log.v(CONSOLE_LOGTAG, message);
1122 break;
1123 case LOG:
1124 Log.i(CONSOLE_LOGTAG, message);
1125 break;
1126 case WARNING:
1127 Log.w(CONSOLE_LOGTAG, message);
1128 break;
1129 case ERROR:
1130 Log.e(CONSOLE_LOGTAG, message);
1131 break;
1132 case DEBUG:
1133 Log.d(CONSOLE_LOGTAG, message);
1134 break;
1135 }
1136
1137 return true;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001138 }
1139
1140 /**
1141 * Ask the browser for an icon to represent a <video> element.
1142 * This icon will be used if the Web page did not specify a poster attribute.
1143 * @return Bitmap The icon or null if no such icon is available.
1144 */
1145 @Override
1146 public Bitmap getDefaultVideoPoster() {
1147 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001148 return mWebViewController.getDefaultVideoPoster();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001149 }
1150 return null;
1151 }
1152
1153 /**
1154 * Ask the host application for a custom progress view to show while
1155 * a <video> is loading.
1156 * @return View The progress view.
1157 */
1158 @Override
1159 public View getVideoLoadingProgressView() {
1160 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001161 return mWebViewController.getVideoLoadingProgressView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001162 }
1163 return null;
1164 }
1165
1166 @Override
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01001167 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001168 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001169 mWebViewController.openFileChooser(uploadMsg, acceptType);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001170 } else {
1171 uploadMsg.onReceiveValue(null);
1172 }
1173 }
1174
1175 /**
1176 * Deliver a list of already-visited URLs
1177 */
1178 @Override
1179 public void getVisitedHistory(final ValueCallback<String[]> callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001180 mWebViewController.getVisitedHistory(callback);
1181 }
Ben Murdoch8029a772010-11-16 11:58:21 +00001182
1183 @Override
1184 public void setupAutoFill(Message message) {
1185 // Prompt the user to set up their profile.
1186 final Message msg = message;
1187 AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
Ben Murdoch1d676b62011-01-17 12:54:24 +00001188 LayoutInflater inflater = (LayoutInflater) mActivity.getSystemService(
1189 Context.LAYOUT_INFLATER_SERVICE);
1190 final View layout = inflater.inflate(R.layout.setup_autofill_dialog, null);
1191
1192 builder.setView(layout)
1193 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1194 @Override
1195 public void onClick(DialogInterface dialog, int id) {
1196 CheckBox disableAutoFill = (CheckBox) layout.findViewById(
1197 R.id.setup_autofill_dialog_disable_autofill);
1198
1199 if (disableAutoFill.isChecked()) {
1200 // Disable autofill and show a toast with how to turn it on again.
John Reck35e9dd62011-04-25 09:01:54 -07001201 mSettings.setAutofillEnabled(false);
Ben Murdoch1d676b62011-01-17 12:54:24 +00001202 Toast.makeText(mActivity,
1203 R.string.autofill_setup_dialog_negative_toast,
1204 Toast.LENGTH_LONG).show();
1205 } else {
1206 // Take user to the AutoFill profile editor. When they return,
1207 // we will send the message that we pass here which will trigger
1208 // the form to get filled out with their new profile.
1209 mWebViewController.setupAutoFill(msg);
1210 }
1211 }
1212 })
1213 .setNegativeButton(R.string.cancel, null)
1214 .show();
Ben Murdoch8029a772010-11-16 11:58:21 +00001215 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001216 };
1217
1218 // -------------------------------------------------------------------------
1219 // WebViewClient implementation for the sub window
1220 // -------------------------------------------------------------------------
1221
1222 // Subclass of WebViewClient used in subwindows to notify the main
1223 // WebViewClient of certain WebView activities.
1224 private static class SubWindowClient extends WebViewClient {
1225 // The main WebViewClient.
1226 private final WebViewClient mClient;
Michael Kolb8233fac2010-10-26 16:08:53 -07001227 private final WebViewController mController;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001228
Michael Kolb8233fac2010-10-26 16:08:53 -07001229 SubWindowClient(WebViewClient client, WebViewController controller) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001230 mClient = client;
Michael Kolb8233fac2010-10-26 16:08:53 -07001231 mController = controller;
Leon Scroggins III211ba542010-04-19 13:21:13 -04001232 }
1233 @Override
1234 public void onPageStarted(WebView view, String url, Bitmap favicon) {
1235 // Unlike the others, do not call mClient's version, which would
1236 // change the progress bar. However, we do want to remove the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001237 // find or select dialog.
Michael Kolb8233fac2010-10-26 16:08:53 -07001238 mController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001239 }
1240 @Override
1241 public void doUpdateVisitedHistory(WebView view, String url,
1242 boolean isReload) {
1243 mClient.doUpdateVisitedHistory(view, url, isReload);
1244 }
1245 @Override
1246 public boolean shouldOverrideUrlLoading(WebView view, String url) {
1247 return mClient.shouldOverrideUrlLoading(view, url);
1248 }
1249 @Override
1250 public void onReceivedSslError(WebView view, SslErrorHandler handler,
1251 SslError error) {
1252 mClient.onReceivedSslError(view, handler, error);
1253 }
1254 @Override
Brian Carlstrom8862c1d2011-06-02 01:05:55 -07001255 public void onReceivedClientCertRequest(WebView view,
1256 ClientCertRequestHandler handler, String host_and_port) {
1257 mClient.onReceivedClientCertRequest(view, handler, host_and_port);
1258 }
1259 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -07001260 public void onReceivedHttpAuthRequest(WebView view,
1261 HttpAuthHandler handler, String host, String realm) {
1262 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
1263 }
1264 @Override
1265 public void onFormResubmission(WebView view, Message dontResend,
1266 Message resend) {
1267 mClient.onFormResubmission(view, dontResend, resend);
1268 }
1269 @Override
1270 public void onReceivedError(WebView view, int errorCode,
1271 String description, String failingUrl) {
1272 mClient.onReceivedError(view, errorCode, description, failingUrl);
1273 }
1274 @Override
1275 public boolean shouldOverrideKeyEvent(WebView view,
1276 android.view.KeyEvent event) {
1277 return mClient.shouldOverrideKeyEvent(view, event);
1278 }
1279 @Override
1280 public void onUnhandledKeyEvent(WebView view,
1281 android.view.KeyEvent event) {
1282 mClient.onUnhandledKeyEvent(view, event);
1283 }
1284 }
1285
1286 // -------------------------------------------------------------------------
1287 // WebChromeClient implementation for the sub window
1288 // -------------------------------------------------------------------------
1289
1290 private class SubWindowChromeClient extends WebChromeClient {
1291 // The main WebChromeClient.
1292 private final WebChromeClient mClient;
1293
1294 SubWindowChromeClient(WebChromeClient client) {
1295 mClient = client;
1296 }
1297 @Override
1298 public void onProgressChanged(WebView view, int newProgress) {
1299 mClient.onProgressChanged(view, newProgress);
1300 }
1301 @Override
1302 public boolean onCreateWindow(WebView view, boolean dialog,
1303 boolean userGesture, android.os.Message resultMsg) {
1304 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
1305 }
1306 @Override
1307 public void onCloseWindow(WebView window) {
1308 if (window != mSubView) {
1309 Log.e(LOGTAG, "Can't close the window");
1310 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001311 mWebViewController.dismissSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001312 }
1313 }
1314
1315 // -------------------------------------------------------------------------
1316
Michael Kolb8233fac2010-10-26 16:08:53 -07001317 // TODO temporarily use activity here
1318 // remove later
1319
Grace Kloba22ac16e2009-10-07 18:00:23 -07001320 // Construct a new tab
Michael Kolb7bcafde2011-05-09 13:55:59 -07001321 Tab(WebViewController wvcontroller, WebView w) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001322 mWebViewController = wvcontroller;
1323 mActivity = mWebViewController.getActivity();
John Reck35e9dd62011-04-25 09:01:54 -07001324 mSettings = BrowserSettings.getInstance();
John Recke969cc52010-12-21 17:24:43 -08001325 mDataController = DataController.getInstance(mActivity);
John Reck847b5322011-04-14 17:02:18 -07001326 mCurrentState = new PageState(mActivity, w != null
1327 ? w.isPrivateBrowsingEnabled() : false);
Michael Kolb8233fac2010-10-26 16:08:53 -07001328 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001329 mInForeground = false;
1330
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001331 mDownloadListener = new DownloadListener() {
1332 public void onDownloadStart(String url, String userAgent,
1333 String contentDisposition, String mimetype,
1334 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001335 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001336 mimetype, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001337 }
1338 };
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001339 mWebBackForwardListClient = new WebBackForwardListClient() {
1340 @Override
1341 public void onNewHistoryItem(WebHistoryItem item) {
1342 if (isInVoiceSearchMode()) {
1343 item.setCustomData(mVoiceSearchData.mVoiceSearchIntent);
1344 }
1345 }
1346 @Override
1347 public void onIndexChanged(WebHistoryItem item, int index) {
1348 Object data = item.getCustomData();
1349 if (data != null && data instanceof Intent) {
1350 activateVoiceSearchMode((Intent) data);
1351 }
1352 }
1353 };
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001354
Grace Kloba22ac16e2009-10-07 18:00:23 -07001355 setWebView(w);
1356 }
1357
Michael Kolbc831b632011-05-11 09:30:34 -07001358 public void setId(long id) {
1359 mId = id;
1360 }
1361
1362 public long getId() {
1363 return mId;
1364 }
1365
Grace Kloba22ac16e2009-10-07 18:00:23 -07001366 /**
1367 * Sets the WebView for this tab, correctly removing the old WebView from
1368 * the container view.
1369 */
1370 void setWebView(WebView w) {
1371 if (mMainView == w) {
1372 return;
1373 }
Michael Kolba713ec82010-11-29 17:27:06 -08001374
Grace Kloba22ac16e2009-10-07 18:00:23 -07001375 // If the WebView is changing, the page will be reloaded, so any ongoing
1376 // Geolocation permission requests are void.
Grace Kloba50c241e2010-04-20 11:07:50 -07001377 if (mGeolocationPermissionsPrompt != null) {
1378 mGeolocationPermissionsPrompt.hide();
1379 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001380
Michael Kolba713ec82010-11-29 17:27:06 -08001381 mWebViewController.onSetWebView(this, w);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001382
1383 // set the new one
1384 mMainView = w;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001385 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001386 if (mMainView != null) {
1387 mMainView.setWebViewClient(mWebViewClient);
1388 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001389 // Attach DownloadManager so that downloads can start in an active
1390 // or a non-active window. This can happen when going to a site that
1391 // does a redirect after a period of time. The user could have
1392 // switched to another tab while waiting for the download to start.
1393 mMainView.setDownloadListener(mDownloadListener);
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001394 mMainView.setWebBackForwardListClient(mWebBackForwardListClient);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001395 }
1396 }
1397
1398 /**
1399 * Destroy the tab's main WebView and subWindow if any
1400 */
1401 void destroy() {
1402 if (mMainView != null) {
1403 dismissSubWindow();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001404 // save the WebView to call destroy() after detach it from the tab
1405 WebView webView = mMainView;
1406 setWebView(null);
1407 webView.destroy();
1408 }
1409 }
1410
1411 /**
1412 * Remove the tab from the parent
1413 */
1414 void removeFromTree() {
1415 // detach the children
Michael Kolbc831b632011-05-11 09:30:34 -07001416 if (mChildren != null) {
1417 for(Tab t : mChildren) {
1418 t.setParent(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001419 }
1420 }
1421 // remove itself from the parent list
Michael Kolbc831b632011-05-11 09:30:34 -07001422 if (mParent != null) {
1423 mParent.mChildren.remove(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001424 }
1425 }
1426
1427 /**
1428 * Create a new subwindow unless a subwindow already exists.
1429 * @return True if a new subwindow was created. False if one already exists.
1430 */
1431 boolean createSubWindow() {
1432 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001433 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001434 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001435 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001436 mSubView.setWebChromeClient(new SubWindowChromeClient(
1437 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001438 // Set a different DownloadListener for the mSubView, since it will
1439 // just need to dismiss the mSubView, rather than close the Tab
1440 mSubView.setDownloadListener(new DownloadListener() {
1441 public void onDownloadStart(String url, String userAgent,
1442 String contentDisposition, String mimetype,
1443 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001444 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001445 contentDisposition, mimetype, contentLength);
1446 if (mSubView.copyBackForwardList().getSize() == 0) {
1447 // This subwindow was opened for the sole purpose of
1448 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001449 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001450 }
1451 }
1452 });
Grace Kloba22ac16e2009-10-07 18:00:23 -07001453 mSubView.setOnCreateContextMenuListener(mActivity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001454 return true;
1455 }
1456 return false;
1457 }
1458
1459 /**
1460 * Dismiss the subWindow for the tab.
1461 */
1462 void dismissSubWindow() {
1463 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001464 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001465 mSubView.destroy();
1466 mSubView = null;
1467 mSubViewContainer = null;
1468 }
1469 }
1470
Grace Kloba22ac16e2009-10-07 18:00:23 -07001471
1472 /**
1473 * Set the parent tab of this tab.
1474 */
Michael Kolbc831b632011-05-11 09:30:34 -07001475 void setParent(Tab parent) {
1476 mParent = parent;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001477 // This tab may have been freed due to low memory. If that is the case,
Michael Kolbc831b632011-05-11 09:30:34 -07001478 // the parent tab id is already saved. If we are changing that id
Grace Kloba22ac16e2009-10-07 18:00:23 -07001479 // (most likely due to removing the parent tab) we must update the
Michael Kolbc831b632011-05-11 09:30:34 -07001480 // parent tab id in the saved Bundle.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001481 if (mSavedState != null) {
1482 if (parent == null) {
1483 mSavedState.remove(PARENTTAB);
1484 } else {
Michael Kolbc831b632011-05-11 09:30:34 -07001485 mSavedState.putLong(PARENTTAB, parent.getId());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001486 }
1487 }
John Reckb0a86db2011-05-24 14:05:58 -07001488
1489 // Sync the WebView useragent with the parent
1490 if (parent != null && mSettings.hasDesktopUseragent(parent.getWebView())
1491 != mSettings.hasDesktopUseragent(getWebView())) {
1492 mSettings.toggleDesktopUseragent(getWebView());
1493 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001494 }
1495
1496 /**
Michael Kolbc831b632011-05-11 09:30:34 -07001497 * If this Tab was created through another Tab, then this method returns
1498 * that Tab.
1499 * @return the Tab parent or null
1500 */
1501 public Tab getParent() {
1502 return mParent;
1503 }
1504
1505 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001506 * When a Tab is created through the content of another Tab, then we
1507 * associate the Tabs.
1508 * @param child the Tab that was created from this Tab
1509 */
1510 void addChildTab(Tab child) {
Michael Kolbc831b632011-05-11 09:30:34 -07001511 if (mChildren == null) {
1512 mChildren = new Vector<Tab>();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001513 }
Michael Kolbc831b632011-05-11 09:30:34 -07001514 mChildren.add(child);
1515 child.setParent(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001516 }
1517
Michael Kolbc831b632011-05-11 09:30:34 -07001518 Vector<Tab> getChildren() {
1519 return mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001520 }
1521
1522 void resume() {
1523 if (mMainView != null) {
1524 mMainView.onResume();
1525 if (mSubView != null) {
1526 mSubView.onResume();
1527 }
1528 }
1529 }
1530
1531 void pause() {
1532 if (mMainView != null) {
1533 mMainView.onPause();
1534 if (mSubView != null) {
1535 mSubView.onPause();
1536 }
1537 }
1538 }
1539
1540 void putInForeground() {
1541 mInForeground = true;
1542 resume();
1543 mMainView.setOnCreateContextMenuListener(mActivity);
1544 if (mSubView != null) {
1545 mSubView.setOnCreateContextMenuListener(mActivity);
1546 }
1547 // Show the pending error dialog if the queue is not empty
1548 if (mQueuedErrors != null && mQueuedErrors.size() > 0) {
1549 showError(mQueuedErrors.getFirst());
1550 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001551 mWebViewController.bookmarkedStatusHasChanged(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001552 }
1553
1554 void putInBackground() {
1555 mInForeground = false;
1556 pause();
1557 mMainView.setOnCreateContextMenuListener(null);
1558 if (mSubView != null) {
1559 mSubView.setOnCreateContextMenuListener(null);
1560 }
1561 }
1562
Michael Kolb8233fac2010-10-26 16:08:53 -07001563 boolean inForeground() {
1564 return mInForeground;
1565 }
1566
Grace Kloba22ac16e2009-10-07 18:00:23 -07001567 /**
1568 * Return the top window of this tab; either the subwindow if it is not
1569 * null or the main window.
1570 * @return The top window of this tab.
1571 */
1572 WebView getTopWindow() {
1573 if (mSubView != null) {
1574 return mSubView;
1575 }
1576 return mMainView;
1577 }
1578
1579 /**
1580 * Return the main window of this tab. Note: if a tab is freed in the
1581 * background, this can return null. It is only guaranteed to be
1582 * non-null for the current tab.
1583 * @return The main WebView of this tab.
1584 */
1585 WebView getWebView() {
1586 return mMainView;
1587 }
1588
Michael Kolba713ec82010-11-29 17:27:06 -08001589 void setViewContainer(View container) {
1590 mContainer = container;
1591 }
1592
Michael Kolb8233fac2010-10-26 16:08:53 -07001593 View getViewContainer() {
1594 return mContainer;
1595 }
1596
Grace Kloba22ac16e2009-10-07 18:00:23 -07001597 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001598 * Return whether private browsing is enabled for the main window of
1599 * this tab.
1600 * @return True if private browsing is enabled.
1601 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001602 boolean isPrivateBrowsingEnabled() {
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001603 WebView webView = getWebView();
1604 if (webView == null) {
1605 return false;
1606 }
1607 return webView.isPrivateBrowsingEnabled();
1608 }
1609
1610 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001611 * Return the subwindow of this tab or null if there is no subwindow.
1612 * @return The subwindow of this tab or null.
1613 */
1614 WebView getSubWebView() {
1615 return mSubView;
1616 }
1617
Michael Kolb1514bb72010-11-22 09:11:48 -08001618 void setSubWebView(WebView subView) {
1619 mSubView = subView;
1620 }
1621
Michael Kolb8233fac2010-10-26 16:08:53 -07001622 View getSubViewContainer() {
1623 return mSubViewContainer;
1624 }
1625
Michael Kolb1514bb72010-11-22 09:11:48 -08001626 void setSubViewContainer(View subViewContainer) {
1627 mSubViewContainer = subViewContainer;
1628 }
1629
Grace Kloba22ac16e2009-10-07 18:00:23 -07001630 /**
1631 * @return The geolocation permissions prompt for this tab.
1632 */
1633 GeolocationPermissionsPrompt getGeolocationPermissionsPrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001634 if (mGeolocationPermissionsPrompt == null) {
1635 ViewStub stub = (ViewStub) mContainer
1636 .findViewById(R.id.geolocation_permissions_prompt);
1637 mGeolocationPermissionsPrompt = (GeolocationPermissionsPrompt) stub
1638 .inflate();
1639 mGeolocationPermissionsPrompt.init();
1640 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001641 return mGeolocationPermissionsPrompt;
1642 }
1643
1644 /**
1645 * @return The application id string
1646 */
1647 String getAppId() {
1648 return mAppId;
1649 }
1650
1651 /**
1652 * Set the application id string
1653 * @param id
1654 */
1655 void setAppId(String id) {
1656 mAppId = id;
1657 }
1658
Grace Kloba22ac16e2009-10-07 18:00:23 -07001659 String getUrl() {
John Reck324d4402011-01-11 16:56:42 -08001660 return UrlUtils.filteredUrl(mCurrentState.mUrl);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001661 }
1662
John Reck49a603c2011-03-03 09:33:05 -08001663 String getOriginalUrl() {
1664 if (mMainView == null) {
1665 return "";
1666 }
1667 return UrlUtils.filteredUrl(mMainView.getOriginalUrl());
1668 }
1669
Grace Kloba22ac16e2009-10-07 18:00:23 -07001670 /**
John Reck30c714c2010-12-16 17:30:34 -08001671 * Get the title of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001672 */
1673 String getTitle() {
John Reck30c714c2010-12-16 17:30:34 -08001674 if (mCurrentState.mTitle == null && mInPageLoad) {
1675 return mActivity.getString(R.string.title_bar_loading);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001676 }
John Reck30c714c2010-12-16 17:30:34 -08001677 return mCurrentState.mTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001678 }
1679
1680 /**
John Reck30c714c2010-12-16 17:30:34 -08001681 * Get the favicon of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001682 */
1683 Bitmap getFavicon() {
John Reck30c714c2010-12-16 17:30:34 -08001684 return mCurrentState.mFavicon;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001685 }
1686
John Recke969cc52010-12-21 17:24:43 -08001687 public boolean isBookmarkedSite() {
1688 return mCurrentState.mIsBookmarkedSite;
1689 }
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001690
Grace Kloba22ac16e2009-10-07 18:00:23 -07001691 /**
1692 * Return the tab's error console. Creates the console if createIfNEcessary
1693 * is true and we haven't already created the console.
1694 * @param createIfNecessary Flag to indicate if the console should be
1695 * created if it has not been already.
1696 * @return The tab's error console, or null if one has not been created and
1697 * createIfNecessary is false.
1698 */
1699 ErrorConsoleView getErrorConsole(boolean createIfNecessary) {
1700 if (createIfNecessary && mErrorConsole == null) {
1701 mErrorConsole = new ErrorConsoleView(mActivity);
1702 mErrorConsole.setWebView(mMainView);
1703 }
1704 return mErrorConsole;
1705 }
1706
John Reck30c714c2010-12-16 17:30:34 -08001707 private void setLockIconType(LockIcon icon) {
1708 mCurrentState.mLockIcon = icon;
1709 mWebViewController.onUpdatedLockIcon(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001710 }
1711
1712 /**
1713 * @return The tab's lock icon type.
1714 */
John Reck30c714c2010-12-16 17:30:34 -08001715 LockIcon getLockIconType() {
1716 return mCurrentState.mLockIcon;
1717 }
1718
1719 int getLoadProgress() {
1720 if (mInPageLoad) {
1721 return mPageLoadProgress;
1722 }
1723 return 100;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001724 }
1725
1726 /**
1727 * @return TRUE if onPageStarted is called while onPageFinished is not
1728 * called yet.
1729 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001730 boolean inPageLoad() {
1731 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001732 }
1733
1734 // force mInLoad to be false. This should only be called before closing the
1735 // tab to ensure BrowserActivity's pauseWebViewTimers() is called correctly.
Michael Kolb8233fac2010-10-26 16:08:53 -07001736 void clearInPageLoad() {
1737 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001738 }
1739
Grace Kloba22ac16e2009-10-07 18:00:23 -07001740 /**
John Reck30c714c2010-12-16 17:30:34 -08001741 * Get the cached saved state bundle.
1742 * @return cached state bundle
Grace Kloba22ac16e2009-10-07 18:00:23 -07001743 */
1744 Bundle getSavedState() {
1745 return mSavedState;
1746 }
1747
John Reckaed9c542011-05-27 16:08:53 -07001748 Bundle getSavedState(boolean saveImages) {
1749 if (saveImages && mScreenshot != null) {
1750 Bundle b = new Bundle(mSavedState);
1751 b.putParcelable(SCREENSHOT, mScreenshot);
1752 return b;
1753 }
1754 return mSavedState;
1755 }
1756
Grace Kloba22ac16e2009-10-07 18:00:23 -07001757 /**
1758 * Set the saved state.
1759 */
1760 void setSavedState(Bundle state) {
1761 mSavedState = state;
1762 }
1763
1764 /**
1765 * @return TRUE if succeed in saving the state.
1766 */
1767 boolean saveState() {
1768 // If the WebView is null it means we ran low on memory and we already
1769 // stored the saved state in mSavedState.
1770 if (mMainView == null) {
1771 return mSavedState != null;
1772 }
John Reck24f18262011-06-17 14:47:20 -07001773 // If the tab is the homepage or has no URL, don't save it
1774 String homepage = BrowserSettings.getInstance().getHomePage();
1775 if (TextUtils.equals(homepage, mCurrentState.mUrl)
1776 || TextUtils.isEmpty(mCurrentState.mUrl)) {
1777 return false;
1778 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001779
1780 mSavedState = new Bundle();
John Reck541f55a2011-06-07 16:34:43 -07001781 mMainView.saveState(mSavedState);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001782
Michael Kolbc831b632011-05-11 09:30:34 -07001783 mSavedState.putLong(ID, mId);
John Reck30c714c2010-12-16 17:30:34 -08001784 mSavedState.putString(CURRURL, mCurrentState.mUrl);
1785 mSavedState.putString(CURRTITLE, mCurrentState.mTitle);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001786 if (mAppId != null) {
1787 mSavedState.putString(APPID, mAppId);
1788 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001789 // Remember the parent tab so the relationship can be restored.
Michael Kolbc831b632011-05-11 09:30:34 -07001790 if (mParent != null) {
1791 mSavedState.putLong(PARENTTAB, mParent.mId);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001792 }
John Reckb0a86db2011-05-24 14:05:58 -07001793 mSavedState.putBoolean(USERAGENT,
1794 mSettings.hasDesktopUseragent(getWebView()));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001795 return true;
1796 }
1797
1798 /*
1799 * Restore the state of the tab.
1800 */
1801 boolean restoreState(Bundle b) {
1802 if (b == null) {
1803 return false;
1804 }
1805 // Restore the internal state even if the WebView fails to restore.
1806 // This will maintain the app id, original url and close-on-exit values.
1807 mSavedState = null;
Michael Kolbc831b632011-05-11 09:30:34 -07001808 mId = b.getLong(ID);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001809 mAppId = b.getString(APPID);
Michael Kolbeb95db42011-03-03 10:38:40 -08001810 mScreenshot = b.getParcelable(SCREENSHOT);
John Reckb0a86db2011-05-24 14:05:58 -07001811 if (b.getBoolean(USERAGENT)
1812 != mSettings.hasDesktopUseragent(getWebView())) {
1813 mSettings.toggleDesktopUseragent(getWebView());
1814 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001815
1816 final WebBackForwardList list = mMainView.restoreState(b);
1817 if (list == null) {
1818 return false;
1819 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001820 return true;
1821 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04001822
Leon Scroggins1961ed22010-12-07 15:22:21 -05001823 public void updateBookmarkedStatus() {
John Recke969cc52010-12-21 17:24:43 -08001824 mDataController.queryBookmarkStatus(getUrl(), mIsBookmarkCallback);
Leon Scroggins1961ed22010-12-07 15:22:21 -05001825 }
1826
John Recke969cc52010-12-21 17:24:43 -08001827 private DataController.OnQueryUrlIsBookmark mIsBookmarkCallback
1828 = new DataController.OnQueryUrlIsBookmark() {
1829 @Override
1830 public void onQueryUrlIsBookmark(String url, boolean isBookmark) {
1831 if (mCurrentState.mUrl.equals(url)) {
1832 mCurrentState.mIsBookmarkedSite = isBookmark;
1833 mWebViewController.bookmarkedStatusHasChanged(Tab.this);
1834 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001835 }
John Recke969cc52010-12-21 17:24:43 -08001836 };
Michael Kolb1acef692011-03-08 14:12:06 -08001837
Michael Kolbeb95db42011-03-03 10:38:40 -08001838 public void setScreenshot(Bitmap screenshot) {
1839 mScreenshot = screenshot;
1840 }
1841
1842 public Bitmap getScreenshot() {
1843 return mScreenshot;
1844 }
1845
John Reck541f55a2011-06-07 16:34:43 -07001846 public boolean isSnapshot() {
John Reck541f55a2011-06-07 16:34:43 -07001847 return false;
1848 }
1849
John Reckd8c74522011-06-14 08:45:00 -07001850 public ContentValues createSnapshotValues() {
1851 if (mMainView == null) return null;
1852 ByteArrayOutputStream stream = new ByteArrayOutputStream();
1853 if (!mMainView.saveViewState(stream)) {
John Reck541f55a2011-06-07 16:34:43 -07001854 return null;
1855 }
John Reckd8c74522011-06-14 08:45:00 -07001856 byte[] data = stream.toByteArray();
John Reckd8c74522011-06-14 08:45:00 -07001857 ContentValues values = new ContentValues();
1858 values.put(Snapshots.TITLE, mCurrentState.mTitle);
1859 values.put(Snapshots.URL, mCurrentState.mUrl);
1860 values.put(Snapshots.VIEWSTATE, data);
1861 values.put(Snapshots.BACKGROUND, mMainView.getPageBackgroundColor());
1862 return values;
John Reck541f55a2011-06-07 16:34:43 -07001863 }
1864
John Reck26b18322011-06-21 13:08:58 -07001865 public void loadUrl(String url, Map<String, String> headers) {
1866 if (mMainView != null) {
1867 mCurrentState = new PageState(mActivity, false, url, null);
1868 mWebViewController.onPageStarted(this, mMainView, null);
1869 mMainView.loadUrl(url, headers);
1870 }
1871 }
1872
Grace Kloba22ac16e2009-10-07 18:00:23 -07001873}