blob: 58a7491b195c241ad0b8fff7049ed3f3a157ff27 [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.ContentUris;
24import android.content.ContentValues;
John Reck30c714c2010-12-16 17:30:34 -080025import android.content.Context;
Grace Kloba22ac16e2009-10-07 18:00:23 -070026import android.content.DialogInterface;
Michael Kolbfe251992010-07-08 15:41:55 -070027import android.content.DialogInterface.OnCancelListener;
Jeff Hamilton8ce956c2010-08-17 11:13:53 -050028import android.content.Intent;
Grace Kloba22ac16e2009-10-07 18:00:23 -070029import android.graphics.Bitmap;
John Reck541f55a2011-06-07 16:34:43 -070030import android.graphics.Bitmap.CompressFormat;
John Reck30c714c2010-12-16 17:30:34 -080031import android.graphics.BitmapFactory;
Grace Kloba22ac16e2009-10-07 18:00:23 -070032import android.net.Uri;
33import android.net.http.SslError;
Grace Kloba22ac16e2009-10-07 18:00:23 -070034import android.os.Bundle;
35import android.os.Message;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +000036import android.os.SystemClock;
Brian Carlstrom8862c1d2011-06-02 01:05:55 -070037import android.security.KeyChain;
Brian Carlstromaa09cd82011-06-09 16:04:40 -070038import android.security.KeyChainAliasCallback;
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -050039import android.speech.RecognizerResultsIntent;
John Reck24f18262011-06-17 14:47:20 -070040import android.text.TextUtils;
Grace Kloba22ac16e2009-10-07 18:00:23 -070041import android.util.Log;
42import android.view.KeyEvent;
43import android.view.LayoutInflater;
44import android.view.View;
Grace Kloba50c241e2010-04-20 11:07:50 -070045import android.view.ViewStub;
Brian Carlstrom8862c1d2011-06-02 01:05:55 -070046import android.webkit.ClientCertRequestHandler;
Ben Murdochc42addf2010-01-28 15:19:59 +000047import android.webkit.ConsoleMessage;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -050048import android.webkit.DownloadListener;
Grace Kloba22ac16e2009-10-07 18:00:23 -070049import android.webkit.GeolocationPermissions;
50import android.webkit.HttpAuthHandler;
51import android.webkit.SslErrorHandler;
52import android.webkit.URLUtil;
53import android.webkit.ValueCallback;
54import android.webkit.WebBackForwardList;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -050055import android.webkit.WebBackForwardListClient;
Grace Kloba22ac16e2009-10-07 18:00:23 -070056import android.webkit.WebChromeClient;
57import android.webkit.WebHistoryItem;
John Reck438bf462011-01-12 18:11:46 -080058import android.webkit.WebResourceResponse;
Grace Kloba22ac16e2009-10-07 18:00:23 -070059import android.webkit.WebStorage;
60import android.webkit.WebView;
61import android.webkit.WebViewClient;
Ben Murdoch1d676b62011-01-17 12:54:24 +000062import android.widget.CheckBox;
Grace Kloba22ac16e2009-10-07 18:00:23 -070063import android.widget.LinearLayout;
64import android.widget.TextView;
Ben Murdoch8029a772010-11-16 11:58:21 +000065import android.widget.Toast;
Grace Kloba22ac16e2009-10-07 18:00:23 -070066
John Reck541f55a2011-06-07 16:34:43 -070067import com.android.browser.homepages.HomeProvider;
John Reckd8c74522011-06-14 08:45:00 -070068import com.android.browser.provider.BrowserProvider2.Snapshots;
John Reck541f55a2011-06-07 16:34:43 -070069import com.android.common.speech.LoggingEvents;
70
71import java.io.ByteArrayOutputStream;
72import java.io.DataInputStream;
73import java.io.DataOutputStream;
74import java.io.IOException;
75import java.io.InputStream;
76import java.io.OutputStream;
Michael Kolbfe251992010-07-08 15:41:55 -070077import java.util.ArrayList;
78import java.util.HashMap;
79import java.util.Iterator;
80import java.util.LinkedList;
81import java.util.Map;
82import java.util.Vector;
83
Grace Kloba22ac16e2009-10-07 18:00:23 -070084/**
85 * Class for maintaining Tabs with a main WebView and a subwindow.
86 */
87class Tab {
Michael Kolb8233fac2010-10-26 16:08:53 -070088
Grace Kloba22ac16e2009-10-07 18:00:23 -070089 // Log Tag
90 private static final String LOGTAG = "Tab";
Ben Murdochc42addf2010-01-28 15:19:59 +000091 // Special case the logtag for messages for the Console to make it easier to
92 // filter them and match the logtag used for these messages in older versions
93 // of the browser.
94 private static final String CONSOLE_LOGTAG = "browser";
95
John Reck30c714c2010-12-16 17:30:34 -080096 public enum LockIcon {
97 LOCK_ICON_UNSECURE,
98 LOCK_ICON_SECURE,
99 LOCK_ICON_MIXED,
100 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700101
102 Activity mActivity;
John Reckd8c74522011-06-14 08:45:00 -0700103 protected WebViewController mWebViewController;
Michael Kolb8233fac2010-10-26 16:08:53 -0700104
Michael Kolbc831b632011-05-11 09:30:34 -0700105 // The tab ID
John Reckd8c74522011-06-14 08:45:00 -0700106 private long mId = -1;
Michael Kolbc831b632011-05-11 09:30:34 -0700107
Grace Kloba22ac16e2009-10-07 18:00:23 -0700108 // The Geolocation permissions prompt
109 private GeolocationPermissionsPrompt mGeolocationPermissionsPrompt;
110 // Main WebView wrapper
Michael Kolba713ec82010-11-29 17:27:06 -0800111 private View mContainer;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700112 // Main WebView
113 private WebView mMainView;
114 // Subwindow container
115 private View mSubViewContainer;
116 // Subwindow WebView
117 private WebView mSubView;
118 // Saved bundle for when we are running low on memory. It contains the
119 // information needed to restore the WebView if the user goes back to the
120 // tab.
121 private Bundle mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700122 // Parent Tab. This is the Tab that created this Tab, or null if the Tab was
123 // created by the UI
Michael Kolbc831b632011-05-11 09:30:34 -0700124 private Tab mParent;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700125 // Tab that constructed by this Tab. This is used when this Tab is
126 // destroyed, it clears all mParentTab values in the children.
Michael Kolbc831b632011-05-11 09:30:34 -0700127 private Vector<Tab> mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700128 // If true, the tab is in the foreground of the current activity.
129 private boolean mInForeground;
Michael Kolb8233fac2010-10-26 16:08:53 -0700130 // If true, the tab is in page loading state (after onPageStarted,
131 // before onPageFinsihed)
132 private boolean mInPageLoad;
John Reck30c714c2010-12-16 17:30:34 -0800133 // The last reported progress of the current page
134 private int mPageLoadProgress;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000135 // The time the load started, used to find load page time
136 private long mLoadStartTime;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700137 // Application identifier used to find tabs that another application wants
138 // to reuse.
139 private String mAppId;
140 // Keep the original url around to avoid killing the old WebView if the url
141 // has not changed.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700142 // Error console for the tab
143 private ErrorConsoleView mErrorConsole;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -0500144 // The listener that gets invoked when a download is started from the
145 // mMainView
146 private final DownloadListener mDownloadListener;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500147 // Listener used to know when we move forward or back in the history list.
148 private final WebBackForwardListClient mWebBackForwardListClient;
John Recke969cc52010-12-21 17:24:43 -0800149 private DataController mDataController;
Patrick Scott92066772011-03-10 08:46:27 -0500150 // State of the auto-login request.
151 private DeviceAccountLogin mDeviceAccountLogin;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700152
153 // AsyncTask for downloading touch icons
154 DownloadTouchIcon mTouchIconLoader;
155
Michael Kolbeb95db42011-03-03 10:38:40 -0800156 private Bitmap mScreenshot;
John Reck35e9dd62011-04-25 09:01:54 -0700157 private BrowserSettings mSettings;
Michael Kolbeb95db42011-03-03 10:38:40 -0800158
John Reck30c714c2010-12-16 17:30:34 -0800159 // All the state needed for a page
John Reckd8c74522011-06-14 08:45:00 -0700160 protected static class PageState {
John Reck30c714c2010-12-16 17:30:34 -0800161 String mUrl;
162 String mTitle;
163 LockIcon mLockIcon;
164 Bitmap mFavicon;
John Recke969cc52010-12-21 17:24:43 -0800165 Boolean mIsBookmarkedSite = false;
John Reck30c714c2010-12-16 17:30:34 -0800166
167 PageState(Context c, boolean incognito) {
168 if (incognito) {
169 mUrl = "browser:incognito";
170 mTitle = c.getString(R.string.new_incognito_tab);
John Reck30c714c2010-12-16 17:30:34 -0800171 } else {
172 mUrl = "";
173 mTitle = c.getString(R.string.new_tab);
John Reck30c714c2010-12-16 17:30:34 -0800174 }
Justin Hodc6cbb72011-01-24 15:14:54 -0800175 mFavicon = BitmapFactory.decodeResource(
176 c.getResources(), R.drawable.app_web_browser_sm);
John Reck30c714c2010-12-16 17:30:34 -0800177 mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
178 }
179
180 PageState(Context c, boolean incognito, String url, Bitmap favicon) {
181 mUrl = url;
182 mTitle = null;
183 if (URLUtil.isHttpsUrl(url)) {
184 mLockIcon = LockIcon.LOCK_ICON_SECURE;
185 } else {
186 mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
187 }
188 if (favicon != null) {
189 mFavicon = favicon;
190 } else {
Justin Hodc6cbb72011-01-24 15:14:54 -0800191 mFavicon = BitmapFactory.decodeResource(
192 c.getResources(), R.drawable.app_web_browser_sm);
John Reck30c714c2010-12-16 17:30:34 -0800193 }
194 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700195 }
196
John Reck30c714c2010-12-16 17:30:34 -0800197 // The current/loading page's state
John Reckd8c74522011-06-14 08:45:00 -0700198 protected PageState mCurrentState;
John Reck30c714c2010-12-16 17:30:34 -0800199
Grace Kloba22ac16e2009-10-07 18:00:23 -0700200 // Used for saving and restoring each Tab
Michael Kolbc831b632011-05-11 09:30:34 -0700201 static final String ID = "ID";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700202 static final String CURRURL = "currentUrl";
203 static final String CURRTITLE = "currentTitle";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700204 static final String PARENTTAB = "parentTab";
205 static final String APPID = "appid";
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700206 static final String INCOGNITO = "privateBrowsingEnabled";
Michael Kolbeb95db42011-03-03 10:38:40 -0800207 static final String SCREENSHOT = "screenshot";
John Reckb0a86db2011-05-24 14:05:58 -0700208 static final String USERAGENT = "useragent";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700209
210 // -------------------------------------------------------------------------
211
Leon Scroggins58d56c62010-01-28 15:12:40 -0500212 /**
213 * Private information regarding the latest voice search. If the Tab is not
214 * in voice search mode, this will be null.
215 */
216 private VoiceSearchData mVoiceSearchData;
217 /**
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400218 * Remove voice search mode from this tab.
219 */
220 public void revertVoiceSearchMode() {
221 if (mVoiceSearchData != null) {
222 mVoiceSearchData = null;
223 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700224 mWebViewController.revertVoiceSearchMode(this);
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400225 }
226 }
227 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700228
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400229 /**
Leon Scroggins58d56c62010-01-28 15:12:40 -0500230 * Return whether the tab is in voice search mode.
231 */
232 public boolean isInVoiceSearchMode() {
233 return mVoiceSearchData != null;
234 }
235 /**
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400236 * Return true if the Tab is in voice search mode and the voice search
237 * Intent came with a String identifying that Google provided the Intent.
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500238 */
239 public boolean voiceSearchSourceIsGoogle() {
240 return mVoiceSearchData != null && mVoiceSearchData.mSourceIsGoogle;
241 }
242 /**
Leon Scroggins58d56c62010-01-28 15:12:40 -0500243 * Get the title to display for the current voice search page. If the Tab
244 * is not in voice search mode, return null.
245 */
246 public String getVoiceDisplayTitle() {
247 if (mVoiceSearchData == null) return null;
248 return mVoiceSearchData.mLastVoiceSearchTitle;
249 }
250 /**
251 * Get the latest array of voice search results, to be passed to the
252 * BrowserProvider. If the Tab is not in voice search mode, return null.
253 */
254 public ArrayList<String> getVoiceSearchResults() {
255 if (mVoiceSearchData == null) return null;
256 return mVoiceSearchData.mVoiceSearchResults;
257 }
258 /**
259 * Activate voice search mode.
260 * @param intent Intent which has the results to use, or an index into the
261 * results when reusing the old results.
262 */
263 /* package */ void activateVoiceSearchMode(Intent intent) {
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500264 int index = 0;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500265 ArrayList<String> results = intent.getStringArrayListExtra(
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -0500266 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_STRINGS);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500267 if (results != null) {
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500268 ArrayList<String> urls = intent.getStringArrayListExtra(
269 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_URLS);
270 ArrayList<String> htmls = intent.getStringArrayListExtra(
271 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_HTML);
272 ArrayList<String> baseUrls = intent.getStringArrayListExtra(
273 RecognizerResultsIntent
274 .EXTRA_VOICE_SEARCH_RESULT_HTML_BASE_URLS);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500275 // This tab is now entering voice search mode for the first time, or
276 // a new voice search was done.
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500277 int size = results.size();
278 if (urls == null || size != urls.size()) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500279 throw new AssertionError("improper extras passed in Intent");
280 }
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500281 if (htmls == null || htmls.size() != size || baseUrls == null ||
282 (baseUrls.size() != size && baseUrls.size() != 1)) {
283 // If either of these arrays are empty/incorrectly sized, ignore
284 // them.
285 htmls = null;
286 baseUrls = null;
287 }
288 mVoiceSearchData = new VoiceSearchData(results, urls, htmls,
289 baseUrls);
Leon Scroggins9df94972010-03-08 18:20:35 -0500290 mVoiceSearchData.mHeaders = intent.getParcelableArrayListExtra(
291 RecognizerResultsIntent
292 .EXTRA_VOICE_SEARCH_RESULT_HTTP_HEADERS);
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500293 mVoiceSearchData.mSourceIsGoogle = intent.getBooleanExtra(
294 VoiceSearchData.SOURCE_IS_GOOGLE, false);
Leon Scroggins2ee4a5a2010-03-15 16:56:57 -0400295 mVoiceSearchData.mVoiceSearchIntent = new Intent(intent);
Leon Scrogginse10dde52010-03-08 19:53:03 -0500296 }
297 String extraData = intent.getStringExtra(
298 SearchManager.EXTRA_DATA_KEY);
299 if (extraData != null) {
300 index = Integer.parseInt(extraData);
301 if (index >= mVoiceSearchData.mVoiceSearchResults.size()) {
302 throw new AssertionError("index must be less than "
303 + "size of mVoiceSearchResults");
304 }
305 if (mVoiceSearchData.mSourceIsGoogle) {
306 Intent logIntent = new Intent(
307 LoggingEvents.ACTION_LOG_EVENT);
308 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
309 LoggingEvents.VoiceSearch.N_BEST_CHOOSE);
310 logIntent.putExtra(
311 LoggingEvents.VoiceSearch.EXTRA_N_BEST_CHOOSE_INDEX,
312 index);
313 mActivity.sendBroadcast(logIntent);
314 }
315 if (mVoiceSearchData.mVoiceSearchIntent != null) {
Leon Scroggins2ee4a5a2010-03-15 16:56:57 -0400316 // Copy the Intent, so that each history item will have its own
317 // Intent, with different (or none) extra data.
318 Intent latest = new Intent(mVoiceSearchData.mVoiceSearchIntent);
319 latest.putExtra(SearchManager.EXTRA_DATA_KEY, extraData);
320 mVoiceSearchData.mVoiceSearchIntent = latest;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500321 }
322 }
323 mVoiceSearchData.mLastVoiceSearchTitle
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500324 = mVoiceSearchData.mVoiceSearchResults.get(index);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500325 if (mInForeground) {
Michael Kolb11d19782011-03-20 10:17:40 -0700326 mWebViewController.activateVoiceSearchMode(
327 mVoiceSearchData.mLastVoiceSearchTitle,
328 mVoiceSearchData.mVoiceSearchResults);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500329 }
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500330 if (mVoiceSearchData.mVoiceSearchHtmls != null) {
331 // When index was found it was already ensured that it was valid
332 String uriString = mVoiceSearchData.mVoiceSearchHtmls.get(index);
333 if (uriString != null) {
334 Uri dataUri = Uri.parse(uriString);
335 if (RecognizerResultsIntent.URI_SCHEME_INLINE.equals(
336 dataUri.getScheme())) {
337 // If there is only one base URL, use it. If there are
338 // more, there will be one for each index, so use the base
339 // URL corresponding to the index.
340 String baseUrl = mVoiceSearchData.mVoiceSearchBaseUrls.get(
341 mVoiceSearchData.mVoiceSearchBaseUrls.size() > 1 ?
342 index : 0);
343 mVoiceSearchData.mLastVoiceSearchUrl = baseUrl;
344 mMainView.loadDataWithBaseURL(baseUrl,
345 uriString.substring(RecognizerResultsIntent
346 .URI_SCHEME_INLINE.length() + 1), "text/html",
347 "utf-8", baseUrl);
348 return;
349 }
350 }
351 }
Leon Scroggins58d56c62010-01-28 15:12:40 -0500352 mVoiceSearchData.mLastVoiceSearchUrl
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500353 = mVoiceSearchData.mVoiceSearchUrls.get(index);
354 if (null == mVoiceSearchData.mLastVoiceSearchUrl) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700355 mVoiceSearchData.mLastVoiceSearchUrl = UrlUtils.smartUrlFilter(
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500356 mVoiceSearchData.mLastVoiceSearchTitle);
357 }
Leon Scroggins9df94972010-03-08 18:20:35 -0500358 Map<String, String> headers = null;
359 if (mVoiceSearchData.mHeaders != null) {
360 int bundleIndex = mVoiceSearchData.mHeaders.size() == 1 ? 0
361 : index;
362 Bundle bundle = mVoiceSearchData.mHeaders.get(bundleIndex);
363 if (bundle != null && !bundle.isEmpty()) {
364 Iterator<String> iter = bundle.keySet().iterator();
365 headers = new HashMap<String, String>();
366 while (iter.hasNext()) {
367 String key = iter.next();
368 headers.put(key, bundle.getString(key));
369 }
370 }
371 }
372 mMainView.loadUrl(mVoiceSearchData.mLastVoiceSearchUrl, headers);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500373 }
374 /* package */ static class VoiceSearchData {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500375 public VoiceSearchData(ArrayList<String> results,
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500376 ArrayList<String> urls, ArrayList<String> htmls,
377 ArrayList<String> baseUrls) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500378 mVoiceSearchResults = results;
379 mVoiceSearchUrls = urls;
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500380 mVoiceSearchHtmls = htmls;
381 mVoiceSearchBaseUrls = baseUrls;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500382 }
383 /*
384 * ArrayList of suggestions to be displayed when opening the
385 * SearchManager
386 */
387 public ArrayList<String> mVoiceSearchResults;
388 /*
389 * ArrayList of urls, associated with the suggestions in
390 * mVoiceSearchResults.
391 */
392 public ArrayList<String> mVoiceSearchUrls;
393 /*
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500394 * ArrayList holding content to load for each item in
395 * mVoiceSearchResults.
396 */
397 public ArrayList<String> mVoiceSearchHtmls;
398 /*
399 * ArrayList holding base urls for the items in mVoiceSearchResults.
400 * If non null, this will either have the same size as
401 * mVoiceSearchResults or have a size of 1, in which case all will use
402 * the same base url
403 */
404 public ArrayList<String> mVoiceSearchBaseUrls;
405 /*
Leon Scroggins58d56c62010-01-28 15:12:40 -0500406 * The last url provided by voice search. Used for comparison to see if
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500407 * we are going to a page by some method besides voice search.
Leon Scroggins58d56c62010-01-28 15:12:40 -0500408 */
409 public String mLastVoiceSearchUrl;
410 /**
411 * The last title used for voice search. Needed to update the title bar
412 * when switching tabs.
413 */
414 public String mLastVoiceSearchTitle;
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500415 /**
416 * Whether the Intent which turned on voice search mode contained the
417 * String signifying that Google was the source.
418 */
419 public boolean mSourceIsGoogle;
420 /**
Leon Scroggins9df94972010-03-08 18:20:35 -0500421 * List of headers to be passed into the WebView containing location
422 * information
423 */
424 public ArrayList<Bundle> mHeaders;
425 /**
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500426 * The Intent used to invoke voice search. Placed on the
427 * WebHistoryItem so that when coming back to a previous voice search
428 * page we can again activate voice search.
429 */
Leon Scrogginse10dde52010-03-08 19:53:03 -0500430 public Intent mVoiceSearchIntent;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500431 /**
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500432 * String used to identify Google as the source of voice search.
433 */
434 public static String SOURCE_IS_GOOGLE
435 = "android.speech.extras.SOURCE_IS_GOOGLE";
Leon Scroggins58d56c62010-01-28 15:12:40 -0500436 }
437
Grace Kloba22ac16e2009-10-07 18:00:23 -0700438 // Container class for the next error dialog that needs to be displayed
439 private class ErrorDialog {
440 public final int mTitle;
441 public final String mDescription;
442 public final int mError;
443 ErrorDialog(int title, String desc, int error) {
444 mTitle = title;
445 mDescription = desc;
446 mError = error;
447 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700448 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700449
450 private void processNextError() {
451 if (mQueuedErrors == null) {
452 return;
453 }
454 // The first one is currently displayed so just remove it.
455 mQueuedErrors.removeFirst();
456 if (mQueuedErrors.size() == 0) {
457 mQueuedErrors = null;
458 return;
459 }
460 showError(mQueuedErrors.getFirst());
461 }
462
463 private DialogInterface.OnDismissListener mDialogListener =
464 new DialogInterface.OnDismissListener() {
465 public void onDismiss(DialogInterface d) {
466 processNextError();
467 }
468 };
469 private LinkedList<ErrorDialog> mQueuedErrors;
470
471 private void queueError(int err, String desc) {
472 if (mQueuedErrors == null) {
473 mQueuedErrors = new LinkedList<ErrorDialog>();
474 }
475 for (ErrorDialog d : mQueuedErrors) {
476 if (d.mError == err) {
477 // Already saw a similar error, ignore the new one.
478 return;
479 }
480 }
481 ErrorDialog errDialog = new ErrorDialog(
482 err == WebViewClient.ERROR_FILE_NOT_FOUND ?
483 R.string.browserFrameFileErrorLabel :
484 R.string.browserFrameNetworkErrorLabel,
485 desc, err);
486 mQueuedErrors.addLast(errDialog);
487
488 // Show the dialog now if the queue was empty and it is in foreground
489 if (mQueuedErrors.size() == 1 && mInForeground) {
490 showError(errDialog);
491 }
492 }
493
494 private void showError(ErrorDialog errDialog) {
495 if (mInForeground) {
496 AlertDialog d = new AlertDialog.Builder(mActivity)
497 .setTitle(errDialog.mTitle)
498 .setMessage(errDialog.mDescription)
499 .setPositiveButton(R.string.ok, null)
500 .create();
501 d.setOnDismissListener(mDialogListener);
502 d.show();
503 }
504 }
505
506 // -------------------------------------------------------------------------
507 // WebViewClient implementation for the main WebView
508 // -------------------------------------------------------------------------
509
510 private final WebViewClient mWebViewClient = new WebViewClient() {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500511 private Message mDontResend;
512 private Message mResend;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700513 @Override
514 public void onPageStarted(WebView view, String url, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700515 mInPageLoad = true;
John Reck30c714c2010-12-16 17:30:34 -0800516 mPageLoadProgress = 0;
517 mCurrentState = new PageState(mActivity,
518 view.isPrivateBrowsingEnabled(), url, favicon);
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000519 mLoadStartTime = SystemClock.uptimeMillis();
Leon Scroggins58d56c62010-01-28 15:12:40 -0500520 if (mVoiceSearchData != null
521 && !url.equals(mVoiceSearchData.mLastVoiceSearchUrl)) {
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500522 if (mVoiceSearchData.mSourceIsGoogle) {
523 Intent i = new Intent(LoggingEvents.ACTION_LOG_EVENT);
524 i.putExtra(LoggingEvents.EXTRA_FLUSH, true);
525 mActivity.sendBroadcast(i);
526 }
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400527 revertVoiceSearchMode();
Leon Scroggins58d56c62010-01-28 15:12:40 -0500528 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700529
Grace Kloba22ac16e2009-10-07 18:00:23 -0700530
531 // If we start a touch icon load and then load a new page, we don't
532 // want to cancel the current touch icon loader. But, we do want to
533 // create a new one when the touch icon url is known.
534 if (mTouchIconLoader != null) {
535 mTouchIconLoader.mTab = null;
536 mTouchIconLoader = null;
537 }
538
539 // reset the error console
540 if (mErrorConsole != null) {
541 mErrorConsole.clearErrorMessages();
Michael Kolb8233fac2010-10-26 16:08:53 -0700542 if (mWebViewController.shouldShowErrorConsole()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700543 mErrorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
544 }
545 }
546
Patrick Scott92066772011-03-10 08:46:27 -0500547 // Cancel the auto-login process.
548 if (mDeviceAccountLogin != null) {
549 mDeviceAccountLogin.cancel();
550 mDeviceAccountLogin = null;
551 mWebViewController.hideAutoLogin(Tab.this);
552 }
553
Grace Kloba22ac16e2009-10-07 18:00:23 -0700554 // finally update the UI in the activity if it is in the foreground
John Reck324d4402011-01-11 16:56:42 -0800555 mWebViewController.onPageStarted(Tab.this, view, favicon);
Leon Scroggins4cd97792010-12-03 15:31:56 -0500556
John Recke969cc52010-12-21 17:24:43 -0800557 updateBookmarkedStatus();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700558 }
559
560 @Override
561 public void onPageFinished(WebView view, String url) {
John Reck5b691842010-11-29 11:21:13 -0800562 if (!isPrivateBrowsingEnabled()) {
563 LogTag.logPageFinishedLoading(
564 url, SystemClock.uptimeMillis() - mLoadStartTime);
565 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700566 mInPageLoad = false;
John Reck30c714c2010-12-16 17:30:34 -0800567 // Sync state (in case of stop/timeout)
568 mCurrentState.mUrl = view.getUrl();
John Reck6c702ee2011-01-07 09:41:53 -0800569 if (mCurrentState.mUrl == null) {
570 mCurrentState.mUrl = url != null ? url : "";
571 }
John Reck30c714c2010-12-16 17:30:34 -0800572 mCurrentState.mTitle = view.getTitle();
573 mCurrentState.mFavicon = view.getFavicon();
574 if (!URLUtil.isHttpsUrl(mCurrentState.mUrl)) {
575 // In case we stop when loading an HTTPS page from an HTTP page
576 // but before a provisional load occurred
577 mCurrentState.mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
578 }
John Reck324d4402011-01-11 16:56:42 -0800579 mWebViewController.onPageFinished(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700580 }
581
582 // return true if want to hijack the url to let another app to handle it
583 @Override
584 public boolean shouldOverrideUrlLoading(WebView view, String url) {
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400585 if (voiceSearchSourceIsGoogle()) {
586 // This method is called when the user clicks on a link.
587 // VoiceSearchMode is turned off when the user leaves the
588 // Google results page, so at this point the user must be on
589 // that page. If the user clicked a link on that page, assume
590 // that the voice search was effective, and broadcast an Intent
591 // so a receiver can take note of that fact.
592 Intent logIntent = new Intent(LoggingEvents.ACTION_LOG_EVENT);
593 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
594 LoggingEvents.VoiceSearch.RESULT_CLICKED);
595 mActivity.sendBroadcast(logIntent);
596 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700597 if (mInForeground) {
Michael Kolb18eb3772010-12-10 14:29:51 -0800598 return mWebViewController.shouldOverrideUrlLoading(Tab.this,
599 view, url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700600 } else {
601 return false;
602 }
603 }
604
605 /**
606 * Updates the lock icon. This method is called when we discover another
607 * resource to be loaded for this page (for example, javascript). While
608 * we update the icon type, we do not update the lock icon itself until
609 * we are done loading, it is slightly more secure this way.
610 */
611 @Override
612 public void onLoadResource(WebView view, String url) {
613 if (url != null && url.length() > 0) {
614 // It is only if the page claims to be secure that we may have
615 // to update the lock:
John Reck30c714c2010-12-16 17:30:34 -0800616 if (mCurrentState.mLockIcon == LockIcon.LOCK_ICON_SECURE) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700617 // If NOT a 'safe' url, change the lock to mixed content!
618 if (!(URLUtil.isHttpsUrl(url) || URLUtil.isDataUrl(url)
619 || URLUtil.isAboutUrl(url))) {
John Reck30c714c2010-12-16 17:30:34 -0800620 mCurrentState.mLockIcon = LockIcon.LOCK_ICON_MIXED;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700621 }
622 }
623 }
624 }
625
626 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700627 * Show a dialog informing the user of the network error reported by
628 * WebCore if it is in the foreground.
629 */
630 @Override
631 public void onReceivedError(WebView view, int errorCode,
632 String description, String failingUrl) {
633 if (errorCode != WebViewClient.ERROR_HOST_LOOKUP &&
634 errorCode != WebViewClient.ERROR_CONNECT &&
635 errorCode != WebViewClient.ERROR_BAD_URL &&
636 errorCode != WebViewClient.ERROR_UNSUPPORTED_SCHEME &&
637 errorCode != WebViewClient.ERROR_FILE) {
638 queueError(errorCode, description);
639 }
Jeff Hamilton47654f42010-09-07 09:57:51 -0500640
641 // Don't log URLs when in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -0700642 if (!isPrivateBrowsingEnabled()) {
Jeff Hamilton47654f42010-09-07 09:57:51 -0500643 Log.e(LOGTAG, "onReceivedError " + errorCode + " " + failingUrl
644 + " " + description);
645 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700646 }
647
648 /**
649 * Check with the user if it is ok to resend POST data as the page they
650 * are trying to navigate to is the result of a POST.
651 */
652 @Override
653 public void onFormResubmission(WebView view, final Message dontResend,
654 final Message resend) {
655 if (!mInForeground) {
656 dontResend.sendToTarget();
657 return;
658 }
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500659 if (mDontResend != null) {
660 Log.w(LOGTAG, "onFormResubmission should not be called again "
661 + "while dialog is still up");
662 dontResend.sendToTarget();
663 return;
664 }
665 mDontResend = dontResend;
666 mResend = resend;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700667 new AlertDialog.Builder(mActivity).setTitle(
668 R.string.browserFrameFormResubmitLabel).setMessage(
669 R.string.browserFrameFormResubmitMessage)
670 .setPositiveButton(R.string.ok,
671 new DialogInterface.OnClickListener() {
672 public void onClick(DialogInterface dialog,
673 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500674 if (mResend != null) {
675 mResend.sendToTarget();
676 mResend = null;
677 mDontResend = null;
678 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700679 }
680 }).setNegativeButton(R.string.cancel,
681 new DialogInterface.OnClickListener() {
682 public void onClick(DialogInterface dialog,
683 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500684 if (mDontResend != null) {
685 mDontResend.sendToTarget();
686 mResend = null;
687 mDontResend = null;
688 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700689 }
690 }).setOnCancelListener(new OnCancelListener() {
691 public void onCancel(DialogInterface dialog) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500692 if (mDontResend != null) {
693 mDontResend.sendToTarget();
694 mResend = null;
695 mDontResend = null;
696 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700697 }
698 }).show();
699 }
700
701 /**
702 * Insert the url into the visited history database.
703 * @param url The url to be inserted.
704 * @param isReload True if this url is being reloaded.
705 * FIXME: Not sure what to do when reloading the page.
706 */
707 @Override
708 public void doUpdateVisitedHistory(WebView view, String url,
709 boolean isReload) {
John Reck324d4402011-01-11 16:56:42 -0800710 mWebViewController.doUpdateVisitedHistory(Tab.this, isReload);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700711 }
712
713 /**
714 * Displays SSL error(s) dialog to the user.
715 */
716 @Override
717 public void onReceivedSslError(final WebView view,
718 final SslErrorHandler handler, final SslError error) {
719 if (!mInForeground) {
720 handler.cancel();
John Reck30c714c2010-12-16 17:30:34 -0800721 setLockIconType(LockIcon.LOCK_ICON_UNSECURE);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700722 return;
723 }
John Reck35e9dd62011-04-25 09:01:54 -0700724 if (mSettings.showSecurityWarnings()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700725 final LayoutInflater factory =
726 LayoutInflater.from(mActivity);
727 final View warningsView =
728 factory.inflate(R.layout.ssl_warnings, null);
729 final LinearLayout placeholder =
730 (LinearLayout)warningsView.findViewById(R.id.placeholder);
731
732 if (error.hasError(SslError.SSL_UNTRUSTED)) {
733 LinearLayout ll = (LinearLayout)factory
734 .inflate(R.layout.ssl_warning, null);
735 ((TextView)ll.findViewById(R.id.warning))
736 .setText(R.string.ssl_untrusted);
737 placeholder.addView(ll);
738 }
739
740 if (error.hasError(SslError.SSL_IDMISMATCH)) {
741 LinearLayout ll = (LinearLayout)factory
742 .inflate(R.layout.ssl_warning, null);
743 ((TextView)ll.findViewById(R.id.warning))
744 .setText(R.string.ssl_mismatch);
745 placeholder.addView(ll);
746 }
747
748 if (error.hasError(SslError.SSL_EXPIRED)) {
749 LinearLayout ll = (LinearLayout)factory
750 .inflate(R.layout.ssl_warning, null);
751 ((TextView)ll.findViewById(R.id.warning))
752 .setText(R.string.ssl_expired);
753 placeholder.addView(ll);
754 }
755
756 if (error.hasError(SslError.SSL_NOTYETVALID)) {
757 LinearLayout ll = (LinearLayout)factory
758 .inflate(R.layout.ssl_warning, null);
759 ((TextView)ll.findViewById(R.id.warning))
760 .setText(R.string.ssl_not_yet_valid);
761 placeholder.addView(ll);
762 }
763
764 new AlertDialog.Builder(mActivity).setTitle(
765 R.string.security_warning).setIcon(
766 android.R.drawable.ic_dialog_alert).setView(
767 warningsView).setPositiveButton(R.string.ssl_continue,
768 new DialogInterface.OnClickListener() {
769 public void onClick(DialogInterface dialog,
770 int whichButton) {
771 handler.proceed();
772 }
773 }).setNeutralButton(R.string.view_certificate,
774 new DialogInterface.OnClickListener() {
775 public void onClick(DialogInterface dialog,
776 int whichButton) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700777 mWebViewController.showSslCertificateOnError(view,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700778 handler, error);
779 }
Ben Murdocha49b8292010-11-16 11:56:04 +0000780 }).setNegativeButton(R.string.ssl_go_back,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700781 new DialogInterface.OnClickListener() {
782 public void onClick(DialogInterface dialog,
783 int whichButton) {
John Reck30c714c2010-12-16 17:30:34 -0800784 dialog.cancel();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700785 }
786 }).setOnCancelListener(
787 new DialogInterface.OnCancelListener() {
788 public void onCancel(DialogInterface dialog) {
789 handler.cancel();
John Reck30c714c2010-12-16 17:30:34 -0800790 setLockIconType(LockIcon.LOCK_ICON_UNSECURE);
791 mWebViewController.onUserCanceledSsl(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700792 }
793 }).show();
794 } else {
795 handler.proceed();
796 }
797 }
798
799 /**
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700800 * Displays client certificate request to the user.
801 */
802 @Override
803 public void onReceivedClientCertRequest(final WebView view,
804 final ClientCertRequestHandler handler, final String host_and_port) {
805 if (!mInForeground) {
806 handler.ignore();
807 return;
808 }
Brian Carlstromaa09cd82011-06-09 16:04:40 -0700809 KeyChain.choosePrivateKeyAlias(mActivity, new KeyChainAliasCallback() {
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700810 @Override public void alias(String alias) {
811 if (alias == null) {
812 handler.cancel();
813 return;
814 }
815 new KeyChainLookup(mActivity, handler, alias).execute();
816 }
Brian Carlstromaa09cd82011-06-09 16:04:40 -0700817 }, null, null, null, -1);
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700818 }
819
820 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700821 * Handles an HTTP authentication request.
822 *
823 * @param handler The authentication handler
824 * @param host The host
825 * @param realm The realm
826 */
827 @Override
828 public void onReceivedHttpAuthRequest(WebView view,
829 final HttpAuthHandler handler, final String host,
830 final String realm) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700831 mWebViewController.onReceivedHttpAuthRequest(Tab.this, view, handler, host, realm);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700832 }
833
834 @Override
John Reck438bf462011-01-12 18:11:46 -0800835 public WebResourceResponse shouldInterceptRequest(WebView view,
836 String url) {
837 WebResourceResponse res = HomeProvider.shouldInterceptRequest(
838 mActivity, url);
839 return res;
840 }
841
842 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700843 public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
844 if (!mInForeground) {
845 return false;
846 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700847 return mWebViewController.shouldOverrideKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700848 }
849
850 @Override
851 public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700852 if (!mInForeground) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700853 return;
854 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700855 mWebViewController.onUnhandledKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700856 }
Patrick Scott92066772011-03-10 08:46:27 -0500857
858 @Override
859 public void onReceivedLoginRequest(WebView view, String realm,
860 String account, String args) {
861 new DeviceAccountLogin(mActivity, view, Tab.this, mWebViewController)
862 .handleLogin(realm, account, args);
863 }
864
Grace Kloba22ac16e2009-10-07 18:00:23 -0700865 };
866
Patrick Scott92066772011-03-10 08:46:27 -0500867 // Called by DeviceAccountLogin when the Tab needs to have the auto-login UI
868 // displayed.
869 void setDeviceAccountLogin(DeviceAccountLogin login) {
870 mDeviceAccountLogin = login;
871 }
872
873 // Returns non-null if the title bar should display the auto-login UI.
874 DeviceAccountLogin getDeviceAccountLogin() {
875 return mDeviceAccountLogin;
876 }
877
Grace Kloba22ac16e2009-10-07 18:00:23 -0700878 // -------------------------------------------------------------------------
879 // WebChromeClient implementation for the main WebView
880 // -------------------------------------------------------------------------
881
882 private final WebChromeClient mWebChromeClient = new WebChromeClient() {
883 // Helper method to create a new tab or sub window.
884 private void createWindow(final boolean dialog, final Message msg) {
885 WebView.WebViewTransport transport =
886 (WebView.WebViewTransport) msg.obj;
887 if (dialog) {
888 createSubWindow();
Michael Kolb8233fac2010-10-26 16:08:53 -0700889 mWebViewController.attachSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700890 transport.setWebView(mSubView);
891 } else {
Michael Kolb7bcafde2011-05-09 13:55:59 -0700892 final Tab newTab = mWebViewController.openTab(null,
John Reck5949c662011-05-27 09:52:29 -0700893 Tab.this, true, true);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700894 transport.setWebView(newTab.getWebView());
895 }
896 msg.sendToTarget();
897 }
898
899 @Override
900 public boolean onCreateWindow(WebView view, final boolean dialog,
901 final boolean userGesture, final Message resultMsg) {
902 // only allow new window or sub window for the foreground case
903 if (!mInForeground) {
904 return false;
905 }
906 // Short-circuit if we can't create any more tabs or sub windows.
907 if (dialog && mSubView != null) {
908 new AlertDialog.Builder(mActivity)
909 .setTitle(R.string.too_many_subwindows_dialog_title)
910 .setIcon(android.R.drawable.ic_dialog_alert)
911 .setMessage(R.string.too_many_subwindows_dialog_message)
912 .setPositiveButton(R.string.ok, null)
913 .show();
914 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700915 } else if (!mWebViewController.getTabControl().canCreateNewTab()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700916 new AlertDialog.Builder(mActivity)
917 .setTitle(R.string.too_many_windows_dialog_title)
918 .setIcon(android.R.drawable.ic_dialog_alert)
919 .setMessage(R.string.too_many_windows_dialog_message)
920 .setPositiveButton(R.string.ok, null)
921 .show();
922 return false;
923 }
924
925 // Short-circuit if this was a user gesture.
926 if (userGesture) {
927 createWindow(dialog, resultMsg);
928 return true;
929 }
930
931 // Allow the popup and create the appropriate window.
932 final AlertDialog.OnClickListener allowListener =
933 new AlertDialog.OnClickListener() {
934 public void onClick(DialogInterface d,
935 int which) {
936 createWindow(dialog, resultMsg);
937 }
938 };
939
940 // Block the popup by returning a null WebView.
941 final AlertDialog.OnClickListener blockListener =
942 new AlertDialog.OnClickListener() {
943 public void onClick(DialogInterface d, int which) {
944 resultMsg.sendToTarget();
945 }
946 };
947
948 // Build a confirmation dialog to display to the user.
949 final AlertDialog d =
950 new AlertDialog.Builder(mActivity)
951 .setTitle(R.string.attention)
952 .setIcon(android.R.drawable.ic_dialog_alert)
953 .setMessage(R.string.popup_window_attempt)
954 .setPositiveButton(R.string.allow, allowListener)
955 .setNegativeButton(R.string.block, blockListener)
956 .setCancelable(false)
957 .create();
958
959 // Show the confirmation dialog.
960 d.show();
961 return true;
962 }
963
964 @Override
Patrick Scotteb5061b2009-11-18 15:00:30 -0500965 public void onRequestFocus(WebView view) {
966 if (!mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700967 mWebViewController.switchToTab(Tab.this);
Patrick Scotteb5061b2009-11-18 15:00:30 -0500968 }
969 }
970
971 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700972 public void onCloseWindow(WebView window) {
Michael Kolbc831b632011-05-11 09:30:34 -0700973 if (mParent != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700974 // JavaScript can only close popup window.
975 if (mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -0700976 mWebViewController.switchToTab(mParent);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700977 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700978 mWebViewController.closeTab(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700979 }
980 }
981
982 @Override
983 public void onProgressChanged(WebView view, int newProgress) {
John Reck30c714c2010-12-16 17:30:34 -0800984 mPageLoadProgress = newProgress;
985 mWebViewController.onProgressChanged(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700986 }
987
988 @Override
Leon Scroggins21d9b902010-03-11 09:33:11 -0500989 public void onReceivedTitle(WebView view, final String title) {
John Reck30c714c2010-12-16 17:30:34 -0800990 mCurrentState.mTitle = title;
Michael Kolb8233fac2010-10-26 16:08:53 -0700991 mWebViewController.onReceivedTitle(Tab.this, title);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700992 }
993
994 @Override
995 public void onReceivedIcon(WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -0800996 mCurrentState.mFavicon = icon;
Michael Kolb8233fac2010-10-26 16:08:53 -0700997 mWebViewController.onFavicon(Tab.this, view, icon);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700998 }
999
1000 @Override
1001 public void onReceivedTouchIconUrl(WebView view, String url,
1002 boolean precomposed) {
1003 final ContentResolver cr = mActivity.getContentResolver();
Leon Scrogginsc8393d92010-04-23 14:58:16 -04001004 // Let precomposed icons take precedence over non-composed
1005 // icons.
1006 if (precomposed && mTouchIconLoader != null) {
1007 mTouchIconLoader.cancel(false);
1008 mTouchIconLoader = null;
1009 }
1010 // Have only one async task at a time.
1011 if (mTouchIconLoader == null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001012 mTouchIconLoader = new DownloadTouchIcon(Tab.this,
1013 mActivity, cr, view);
Leon Scrogginsc8393d92010-04-23 14:58:16 -04001014 mTouchIconLoader.execute(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001015 }
1016 }
1017
1018 @Override
1019 public void onShowCustomView(View view,
1020 WebChromeClient.CustomViewCallback callback) {
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001021 onShowCustomView(view, mActivity.getRequestedOrientation(), callback);
1022 }
1023
1024 @Override
1025 public void onShowCustomView(View view, int requestedOrientation,
1026 WebChromeClient.CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001027 if (mInForeground) mWebViewController.showCustomView(Tab.this, view,
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001028 requestedOrientation, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001029 }
1030
1031 @Override
1032 public void onHideCustomView() {
Michael Kolb8233fac2010-10-26 16:08:53 -07001033 if (mInForeground) mWebViewController.hideCustomView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001034 }
1035
1036 /**
1037 * The origin has exceeded its database quota.
1038 * @param url the URL that exceeded the quota
1039 * @param databaseIdentifier the identifier of the database on which the
1040 * transaction that caused the quota overflow was run
1041 * @param currentQuota the current quota for the origin.
1042 * @param estimatedSize the estimated size of the database.
1043 * @param totalUsedQuota is the sum of all origins' quota.
1044 * @param quotaUpdater The callback to run when a decision to allow or
1045 * deny quota has been made. Don't forget to call this!
1046 */
1047 @Override
1048 public void onExceededDatabaseQuota(String url,
1049 String databaseIdentifier, long currentQuota, long estimatedSize,
1050 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -07001051 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001052 .onExceededDatabaseQuota(url, databaseIdentifier,
1053 currentQuota, estimatedSize, totalUsedQuota,
1054 quotaUpdater);
1055 }
1056
1057 /**
1058 * The Application Cache has exceeded its max size.
1059 * @param spaceNeeded is the amount of disk space that would be needed
1060 * in order for the last appcache operation to succeed.
1061 * @param totalUsedQuota is the sum of all origins' quota.
1062 * @param quotaUpdater A callback to inform the WebCore thread that a
1063 * new app cache size is available. This callback must always
1064 * be executed at some point to ensure that the sleeping
1065 * WebCore thread is woken up.
1066 */
1067 @Override
1068 public void onReachedMaxAppCacheSize(long spaceNeeded,
1069 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -07001070 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001071 .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota,
1072 quotaUpdater);
1073 }
1074
1075 /**
1076 * Instructs the browser to show a prompt to ask the user to set the
1077 * Geolocation permission state for the specified origin.
1078 * @param origin The origin for which Geolocation permissions are
1079 * requested.
1080 * @param callback The callback to call once the user has set the
1081 * Geolocation permission state.
1082 */
1083 @Override
1084 public void onGeolocationPermissionsShowPrompt(String origin,
1085 GeolocationPermissions.Callback callback) {
1086 if (mInForeground) {
Grace Kloba50c241e2010-04-20 11:07:50 -07001087 getGeolocationPermissionsPrompt().show(origin, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001088 }
1089 }
1090
1091 /**
1092 * Instructs the browser to hide the Geolocation permissions prompt.
1093 */
1094 @Override
1095 public void onGeolocationPermissionsHidePrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001096 if (mInForeground && mGeolocationPermissionsPrompt != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001097 mGeolocationPermissionsPrompt.hide();
1098 }
1099 }
1100
Ben Murdoch65acc352009-11-19 18:16:04 +00001101 /* Adds a JavaScript error message to the system log and if the JS
1102 * console is enabled in the about:debug options, to that console
1103 * also.
Ben Murdochc42addf2010-01-28 15:19:59 +00001104 * @param consoleMessage the message object.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001105 */
1106 @Override
Ben Murdochc42addf2010-01-28 15:19:59 +00001107 public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001108 if (mInForeground) {
1109 // call getErrorConsole(true) so it will create one if needed
1110 ErrorConsoleView errorConsole = getErrorConsole(true);
Ben Murdochc42addf2010-01-28 15:19:59 +00001111 errorConsole.addErrorMessage(consoleMessage);
Michael Kolb8233fac2010-10-26 16:08:53 -07001112 if (mWebViewController.shouldShowErrorConsole()
1113 && errorConsole.getShowState() !=
1114 ErrorConsoleView.SHOW_MAXIMIZED) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001115 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
1116 }
1117 }
Ben Murdochc42addf2010-01-28 15:19:59 +00001118
Jeff Hamilton47654f42010-09-07 09:57:51 -05001119 // Don't log console messages in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001120 if (isPrivateBrowsingEnabled()) return true;
Jeff Hamilton47654f42010-09-07 09:57:51 -05001121
Ben Murdochc42addf2010-01-28 15:19:59 +00001122 String message = "Console: " + consoleMessage.message() + " "
1123 + consoleMessage.sourceId() + ":"
1124 + consoleMessage.lineNumber();
1125
1126 switch (consoleMessage.messageLevel()) {
1127 case TIP:
1128 Log.v(CONSOLE_LOGTAG, message);
1129 break;
1130 case LOG:
1131 Log.i(CONSOLE_LOGTAG, message);
1132 break;
1133 case WARNING:
1134 Log.w(CONSOLE_LOGTAG, message);
1135 break;
1136 case ERROR:
1137 Log.e(CONSOLE_LOGTAG, message);
1138 break;
1139 case DEBUG:
1140 Log.d(CONSOLE_LOGTAG, message);
1141 break;
1142 }
1143
1144 return true;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001145 }
1146
1147 /**
1148 * Ask the browser for an icon to represent a <video> element.
1149 * This icon will be used if the Web page did not specify a poster attribute.
1150 * @return Bitmap The icon or null if no such icon is available.
1151 */
1152 @Override
1153 public Bitmap getDefaultVideoPoster() {
1154 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001155 return mWebViewController.getDefaultVideoPoster();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001156 }
1157 return null;
1158 }
1159
1160 /**
1161 * Ask the host application for a custom progress view to show while
1162 * a <video> is loading.
1163 * @return View The progress view.
1164 */
1165 @Override
1166 public View getVideoLoadingProgressView() {
1167 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001168 return mWebViewController.getVideoLoadingProgressView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001169 }
1170 return null;
1171 }
1172
1173 @Override
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01001174 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001175 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001176 mWebViewController.openFileChooser(uploadMsg, acceptType);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001177 } else {
1178 uploadMsg.onReceiveValue(null);
1179 }
1180 }
1181
1182 /**
1183 * Deliver a list of already-visited URLs
1184 */
1185 @Override
1186 public void getVisitedHistory(final ValueCallback<String[]> callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001187 mWebViewController.getVisitedHistory(callback);
1188 }
Ben Murdoch8029a772010-11-16 11:58:21 +00001189
1190 @Override
1191 public void setupAutoFill(Message message) {
1192 // Prompt the user to set up their profile.
1193 final Message msg = message;
1194 AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
Ben Murdoch1d676b62011-01-17 12:54:24 +00001195 LayoutInflater inflater = (LayoutInflater) mActivity.getSystemService(
1196 Context.LAYOUT_INFLATER_SERVICE);
1197 final View layout = inflater.inflate(R.layout.setup_autofill_dialog, null);
1198
1199 builder.setView(layout)
1200 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1201 @Override
1202 public void onClick(DialogInterface dialog, int id) {
1203 CheckBox disableAutoFill = (CheckBox) layout.findViewById(
1204 R.id.setup_autofill_dialog_disable_autofill);
1205
1206 if (disableAutoFill.isChecked()) {
1207 // Disable autofill and show a toast with how to turn it on again.
John Reck35e9dd62011-04-25 09:01:54 -07001208 mSettings.setAutofillEnabled(false);
Ben Murdoch1d676b62011-01-17 12:54:24 +00001209 Toast.makeText(mActivity,
1210 R.string.autofill_setup_dialog_negative_toast,
1211 Toast.LENGTH_LONG).show();
1212 } else {
1213 // Take user to the AutoFill profile editor. When they return,
1214 // we will send the message that we pass here which will trigger
1215 // the form to get filled out with their new profile.
1216 mWebViewController.setupAutoFill(msg);
1217 }
1218 }
1219 })
1220 .setNegativeButton(R.string.cancel, null)
1221 .show();
Ben Murdoch8029a772010-11-16 11:58:21 +00001222 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001223 };
1224
1225 // -------------------------------------------------------------------------
1226 // WebViewClient implementation for the sub window
1227 // -------------------------------------------------------------------------
1228
1229 // Subclass of WebViewClient used in subwindows to notify the main
1230 // WebViewClient of certain WebView activities.
1231 private static class SubWindowClient extends WebViewClient {
1232 // The main WebViewClient.
1233 private final WebViewClient mClient;
Michael Kolb8233fac2010-10-26 16:08:53 -07001234 private final WebViewController mController;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001235
Michael Kolb8233fac2010-10-26 16:08:53 -07001236 SubWindowClient(WebViewClient client, WebViewController controller) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001237 mClient = client;
Michael Kolb8233fac2010-10-26 16:08:53 -07001238 mController = controller;
Leon Scroggins III211ba542010-04-19 13:21:13 -04001239 }
1240 @Override
1241 public void onPageStarted(WebView view, String url, Bitmap favicon) {
1242 // Unlike the others, do not call mClient's version, which would
1243 // change the progress bar. However, we do want to remove the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001244 // find or select dialog.
Michael Kolb8233fac2010-10-26 16:08:53 -07001245 mController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001246 }
1247 @Override
1248 public void doUpdateVisitedHistory(WebView view, String url,
1249 boolean isReload) {
1250 mClient.doUpdateVisitedHistory(view, url, isReload);
1251 }
1252 @Override
1253 public boolean shouldOverrideUrlLoading(WebView view, String url) {
1254 return mClient.shouldOverrideUrlLoading(view, url);
1255 }
1256 @Override
1257 public void onReceivedSslError(WebView view, SslErrorHandler handler,
1258 SslError error) {
1259 mClient.onReceivedSslError(view, handler, error);
1260 }
1261 @Override
Brian Carlstrom8862c1d2011-06-02 01:05:55 -07001262 public void onReceivedClientCertRequest(WebView view,
1263 ClientCertRequestHandler handler, String host_and_port) {
1264 mClient.onReceivedClientCertRequest(view, handler, host_and_port);
1265 }
1266 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -07001267 public void onReceivedHttpAuthRequest(WebView view,
1268 HttpAuthHandler handler, String host, String realm) {
1269 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
1270 }
1271 @Override
1272 public void onFormResubmission(WebView view, Message dontResend,
1273 Message resend) {
1274 mClient.onFormResubmission(view, dontResend, resend);
1275 }
1276 @Override
1277 public void onReceivedError(WebView view, int errorCode,
1278 String description, String failingUrl) {
1279 mClient.onReceivedError(view, errorCode, description, failingUrl);
1280 }
1281 @Override
1282 public boolean shouldOverrideKeyEvent(WebView view,
1283 android.view.KeyEvent event) {
1284 return mClient.shouldOverrideKeyEvent(view, event);
1285 }
1286 @Override
1287 public void onUnhandledKeyEvent(WebView view,
1288 android.view.KeyEvent event) {
1289 mClient.onUnhandledKeyEvent(view, event);
1290 }
1291 }
1292
1293 // -------------------------------------------------------------------------
1294 // WebChromeClient implementation for the sub window
1295 // -------------------------------------------------------------------------
1296
1297 private class SubWindowChromeClient extends WebChromeClient {
1298 // The main WebChromeClient.
1299 private final WebChromeClient mClient;
1300
1301 SubWindowChromeClient(WebChromeClient client) {
1302 mClient = client;
1303 }
1304 @Override
1305 public void onProgressChanged(WebView view, int newProgress) {
1306 mClient.onProgressChanged(view, newProgress);
1307 }
1308 @Override
1309 public boolean onCreateWindow(WebView view, boolean dialog,
1310 boolean userGesture, android.os.Message resultMsg) {
1311 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
1312 }
1313 @Override
1314 public void onCloseWindow(WebView window) {
1315 if (window != mSubView) {
1316 Log.e(LOGTAG, "Can't close the window");
1317 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001318 mWebViewController.dismissSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001319 }
1320 }
1321
1322 // -------------------------------------------------------------------------
1323
Michael Kolb8233fac2010-10-26 16:08:53 -07001324 // TODO temporarily use activity here
1325 // remove later
1326
Grace Kloba22ac16e2009-10-07 18:00:23 -07001327 // Construct a new tab
Michael Kolb7bcafde2011-05-09 13:55:59 -07001328 Tab(WebViewController wvcontroller, WebView w) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001329 mWebViewController = wvcontroller;
1330 mActivity = mWebViewController.getActivity();
John Reck35e9dd62011-04-25 09:01:54 -07001331 mSettings = BrowserSettings.getInstance();
John Recke969cc52010-12-21 17:24:43 -08001332 mDataController = DataController.getInstance(mActivity);
John Reck847b5322011-04-14 17:02:18 -07001333 mCurrentState = new PageState(mActivity, w != null
1334 ? w.isPrivateBrowsingEnabled() : false);
Michael Kolb8233fac2010-10-26 16:08:53 -07001335 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001336 mInForeground = false;
1337
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001338 mDownloadListener = new DownloadListener() {
1339 public void onDownloadStart(String url, String userAgent,
1340 String contentDisposition, String mimetype,
1341 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001342 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001343 mimetype, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001344 }
1345 };
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001346 mWebBackForwardListClient = new WebBackForwardListClient() {
1347 @Override
1348 public void onNewHistoryItem(WebHistoryItem item) {
1349 if (isInVoiceSearchMode()) {
1350 item.setCustomData(mVoiceSearchData.mVoiceSearchIntent);
1351 }
1352 }
1353 @Override
1354 public void onIndexChanged(WebHistoryItem item, int index) {
1355 Object data = item.getCustomData();
1356 if (data != null && data instanceof Intent) {
1357 activateVoiceSearchMode((Intent) data);
1358 }
1359 }
1360 };
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001361
Grace Kloba22ac16e2009-10-07 18:00:23 -07001362 setWebView(w);
1363 }
1364
Michael Kolbc831b632011-05-11 09:30:34 -07001365 public void setId(long id) {
1366 mId = id;
1367 }
1368
1369 public long getId() {
1370 return mId;
1371 }
1372
Grace Kloba22ac16e2009-10-07 18:00:23 -07001373 /**
1374 * Sets the WebView for this tab, correctly removing the old WebView from
1375 * the container view.
1376 */
1377 void setWebView(WebView w) {
1378 if (mMainView == w) {
1379 return;
1380 }
Michael Kolba713ec82010-11-29 17:27:06 -08001381
Grace Kloba22ac16e2009-10-07 18:00:23 -07001382 // If the WebView is changing, the page will be reloaded, so any ongoing
1383 // Geolocation permission requests are void.
Grace Kloba50c241e2010-04-20 11:07:50 -07001384 if (mGeolocationPermissionsPrompt != null) {
1385 mGeolocationPermissionsPrompt.hide();
1386 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001387
Michael Kolba713ec82010-11-29 17:27:06 -08001388 mWebViewController.onSetWebView(this, w);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001389
1390 // set the new one
1391 mMainView = w;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001392 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001393 if (mMainView != null) {
1394 mMainView.setWebViewClient(mWebViewClient);
1395 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001396 // Attach DownloadManager so that downloads can start in an active
1397 // or a non-active window. This can happen when going to a site that
1398 // does a redirect after a period of time. The user could have
1399 // switched to another tab while waiting for the download to start.
1400 mMainView.setDownloadListener(mDownloadListener);
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001401 mMainView.setWebBackForwardListClient(mWebBackForwardListClient);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001402 }
1403 }
1404
1405 /**
1406 * Destroy the tab's main WebView and subWindow if any
1407 */
1408 void destroy() {
1409 if (mMainView != null) {
1410 dismissSubWindow();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001411 // save the WebView to call destroy() after detach it from the tab
1412 WebView webView = mMainView;
1413 setWebView(null);
1414 webView.destroy();
1415 }
1416 }
1417
1418 /**
1419 * Remove the tab from the parent
1420 */
1421 void removeFromTree() {
1422 // detach the children
Michael Kolbc831b632011-05-11 09:30:34 -07001423 if (mChildren != null) {
1424 for(Tab t : mChildren) {
1425 t.setParent(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001426 }
1427 }
1428 // remove itself from the parent list
Michael Kolbc831b632011-05-11 09:30:34 -07001429 if (mParent != null) {
1430 mParent.mChildren.remove(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001431 }
1432 }
1433
1434 /**
1435 * Create a new subwindow unless a subwindow already exists.
1436 * @return True if a new subwindow was created. False if one already exists.
1437 */
1438 boolean createSubWindow() {
1439 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001440 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001441 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001442 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001443 mSubView.setWebChromeClient(new SubWindowChromeClient(
1444 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001445 // Set a different DownloadListener for the mSubView, since it will
1446 // just need to dismiss the mSubView, rather than close the Tab
1447 mSubView.setDownloadListener(new DownloadListener() {
1448 public void onDownloadStart(String url, String userAgent,
1449 String contentDisposition, String mimetype,
1450 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001451 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001452 contentDisposition, mimetype, contentLength);
1453 if (mSubView.copyBackForwardList().getSize() == 0) {
1454 // This subwindow was opened for the sole purpose of
1455 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001456 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001457 }
1458 }
1459 });
Grace Kloba22ac16e2009-10-07 18:00:23 -07001460 mSubView.setOnCreateContextMenuListener(mActivity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001461 return true;
1462 }
1463 return false;
1464 }
1465
1466 /**
1467 * Dismiss the subWindow for the tab.
1468 */
1469 void dismissSubWindow() {
1470 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001471 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001472 mSubView.destroy();
1473 mSubView = null;
1474 mSubViewContainer = null;
1475 }
1476 }
1477
Grace Kloba22ac16e2009-10-07 18:00:23 -07001478
1479 /**
1480 * Set the parent tab of this tab.
1481 */
Michael Kolbc831b632011-05-11 09:30:34 -07001482 void setParent(Tab parent) {
1483 mParent = parent;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001484 // This tab may have been freed due to low memory. If that is the case,
Michael Kolbc831b632011-05-11 09:30:34 -07001485 // the parent tab id is already saved. If we are changing that id
Grace Kloba22ac16e2009-10-07 18:00:23 -07001486 // (most likely due to removing the parent tab) we must update the
Michael Kolbc831b632011-05-11 09:30:34 -07001487 // parent tab id in the saved Bundle.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001488 if (mSavedState != null) {
1489 if (parent == null) {
1490 mSavedState.remove(PARENTTAB);
1491 } else {
Michael Kolbc831b632011-05-11 09:30:34 -07001492 mSavedState.putLong(PARENTTAB, parent.getId());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001493 }
1494 }
John Reckb0a86db2011-05-24 14:05:58 -07001495
1496 // Sync the WebView useragent with the parent
1497 if (parent != null && mSettings.hasDesktopUseragent(parent.getWebView())
1498 != mSettings.hasDesktopUseragent(getWebView())) {
1499 mSettings.toggleDesktopUseragent(getWebView());
1500 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001501 }
1502
1503 /**
Michael Kolbc831b632011-05-11 09:30:34 -07001504 * If this Tab was created through another Tab, then this method returns
1505 * that Tab.
1506 * @return the Tab parent or null
1507 */
1508 public Tab getParent() {
1509 return mParent;
1510 }
1511
1512 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001513 * When a Tab is created through the content of another Tab, then we
1514 * associate the Tabs.
1515 * @param child the Tab that was created from this Tab
1516 */
1517 void addChildTab(Tab child) {
Michael Kolbc831b632011-05-11 09:30:34 -07001518 if (mChildren == null) {
1519 mChildren = new Vector<Tab>();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001520 }
Michael Kolbc831b632011-05-11 09:30:34 -07001521 mChildren.add(child);
1522 child.setParent(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001523 }
1524
Michael Kolbc831b632011-05-11 09:30:34 -07001525 Vector<Tab> getChildren() {
1526 return mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001527 }
1528
1529 void resume() {
1530 if (mMainView != null) {
1531 mMainView.onResume();
1532 if (mSubView != null) {
1533 mSubView.onResume();
1534 }
1535 }
1536 }
1537
1538 void pause() {
1539 if (mMainView != null) {
1540 mMainView.onPause();
1541 if (mSubView != null) {
1542 mSubView.onPause();
1543 }
1544 }
1545 }
1546
1547 void putInForeground() {
1548 mInForeground = true;
1549 resume();
1550 mMainView.setOnCreateContextMenuListener(mActivity);
1551 if (mSubView != null) {
1552 mSubView.setOnCreateContextMenuListener(mActivity);
1553 }
1554 // Show the pending error dialog if the queue is not empty
1555 if (mQueuedErrors != null && mQueuedErrors.size() > 0) {
1556 showError(mQueuedErrors.getFirst());
1557 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001558 mWebViewController.bookmarkedStatusHasChanged(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001559 }
1560
1561 void putInBackground() {
1562 mInForeground = false;
1563 pause();
1564 mMainView.setOnCreateContextMenuListener(null);
1565 if (mSubView != null) {
1566 mSubView.setOnCreateContextMenuListener(null);
1567 }
1568 }
1569
Michael Kolb8233fac2010-10-26 16:08:53 -07001570 boolean inForeground() {
1571 return mInForeground;
1572 }
1573
Grace Kloba22ac16e2009-10-07 18:00:23 -07001574 /**
1575 * Return the top window of this tab; either the subwindow if it is not
1576 * null or the main window.
1577 * @return The top window of this tab.
1578 */
1579 WebView getTopWindow() {
1580 if (mSubView != null) {
1581 return mSubView;
1582 }
1583 return mMainView;
1584 }
1585
1586 /**
1587 * Return the main window of this tab. Note: if a tab is freed in the
1588 * background, this can return null. It is only guaranteed to be
1589 * non-null for the current tab.
1590 * @return The main WebView of this tab.
1591 */
1592 WebView getWebView() {
1593 return mMainView;
1594 }
1595
Michael Kolba713ec82010-11-29 17:27:06 -08001596 void setViewContainer(View container) {
1597 mContainer = container;
1598 }
1599
Michael Kolb8233fac2010-10-26 16:08:53 -07001600 View getViewContainer() {
1601 return mContainer;
1602 }
1603
Grace Kloba22ac16e2009-10-07 18:00:23 -07001604 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001605 * Return whether private browsing is enabled for the main window of
1606 * this tab.
1607 * @return True if private browsing is enabled.
1608 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001609 boolean isPrivateBrowsingEnabled() {
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001610 WebView webView = getWebView();
1611 if (webView == null) {
1612 return false;
1613 }
1614 return webView.isPrivateBrowsingEnabled();
1615 }
1616
1617 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001618 * Return the subwindow of this tab or null if there is no subwindow.
1619 * @return The subwindow of this tab or null.
1620 */
1621 WebView getSubWebView() {
1622 return mSubView;
1623 }
1624
Michael Kolb1514bb72010-11-22 09:11:48 -08001625 void setSubWebView(WebView subView) {
1626 mSubView = subView;
1627 }
1628
Michael Kolb8233fac2010-10-26 16:08:53 -07001629 View getSubViewContainer() {
1630 return mSubViewContainer;
1631 }
1632
Michael Kolb1514bb72010-11-22 09:11:48 -08001633 void setSubViewContainer(View subViewContainer) {
1634 mSubViewContainer = subViewContainer;
1635 }
1636
Grace Kloba22ac16e2009-10-07 18:00:23 -07001637 /**
1638 * @return The geolocation permissions prompt for this tab.
1639 */
1640 GeolocationPermissionsPrompt getGeolocationPermissionsPrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001641 if (mGeolocationPermissionsPrompt == null) {
1642 ViewStub stub = (ViewStub) mContainer
1643 .findViewById(R.id.geolocation_permissions_prompt);
1644 mGeolocationPermissionsPrompt = (GeolocationPermissionsPrompt) stub
1645 .inflate();
1646 mGeolocationPermissionsPrompt.init();
1647 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001648 return mGeolocationPermissionsPrompt;
1649 }
1650
1651 /**
1652 * @return The application id string
1653 */
1654 String getAppId() {
1655 return mAppId;
1656 }
1657
1658 /**
1659 * Set the application id string
1660 * @param id
1661 */
1662 void setAppId(String id) {
1663 mAppId = id;
1664 }
1665
Grace Kloba22ac16e2009-10-07 18:00:23 -07001666 String getUrl() {
John Reck324d4402011-01-11 16:56:42 -08001667 return UrlUtils.filteredUrl(mCurrentState.mUrl);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001668 }
1669
John Reck49a603c2011-03-03 09:33:05 -08001670 String getOriginalUrl() {
1671 if (mMainView == null) {
1672 return "";
1673 }
1674 return UrlUtils.filteredUrl(mMainView.getOriginalUrl());
1675 }
1676
Grace Kloba22ac16e2009-10-07 18:00:23 -07001677 /**
John Reck30c714c2010-12-16 17:30:34 -08001678 * Get the title of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001679 */
1680 String getTitle() {
John Reck30c714c2010-12-16 17:30:34 -08001681 if (mCurrentState.mTitle == null && mInPageLoad) {
1682 return mActivity.getString(R.string.title_bar_loading);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001683 }
John Reck30c714c2010-12-16 17:30:34 -08001684 return mCurrentState.mTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001685 }
1686
1687 /**
John Reck30c714c2010-12-16 17:30:34 -08001688 * Get the favicon of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001689 */
1690 Bitmap getFavicon() {
John Reck30c714c2010-12-16 17:30:34 -08001691 return mCurrentState.mFavicon;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001692 }
1693
John Recke969cc52010-12-21 17:24:43 -08001694 public boolean isBookmarkedSite() {
1695 return mCurrentState.mIsBookmarkedSite;
1696 }
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001697
Grace Kloba22ac16e2009-10-07 18:00:23 -07001698 /**
1699 * Return the tab's error console. Creates the console if createIfNEcessary
1700 * is true and we haven't already created the console.
1701 * @param createIfNecessary Flag to indicate if the console should be
1702 * created if it has not been already.
1703 * @return The tab's error console, or null if one has not been created and
1704 * createIfNecessary is false.
1705 */
1706 ErrorConsoleView getErrorConsole(boolean createIfNecessary) {
1707 if (createIfNecessary && mErrorConsole == null) {
1708 mErrorConsole = new ErrorConsoleView(mActivity);
1709 mErrorConsole.setWebView(mMainView);
1710 }
1711 return mErrorConsole;
1712 }
1713
John Reck30c714c2010-12-16 17:30:34 -08001714 private void setLockIconType(LockIcon icon) {
1715 mCurrentState.mLockIcon = icon;
1716 mWebViewController.onUpdatedLockIcon(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001717 }
1718
1719 /**
1720 * @return The tab's lock icon type.
1721 */
John Reck30c714c2010-12-16 17:30:34 -08001722 LockIcon getLockIconType() {
1723 return mCurrentState.mLockIcon;
1724 }
1725
1726 int getLoadProgress() {
1727 if (mInPageLoad) {
1728 return mPageLoadProgress;
1729 }
1730 return 100;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001731 }
1732
1733 /**
1734 * @return TRUE if onPageStarted is called while onPageFinished is not
1735 * called yet.
1736 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001737 boolean inPageLoad() {
1738 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001739 }
1740
1741 // force mInLoad to be false. This should only be called before closing the
1742 // tab to ensure BrowserActivity's pauseWebViewTimers() is called correctly.
Michael Kolb8233fac2010-10-26 16:08:53 -07001743 void clearInPageLoad() {
1744 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001745 }
1746
Grace Kloba22ac16e2009-10-07 18:00:23 -07001747 /**
John Reck30c714c2010-12-16 17:30:34 -08001748 * Get the cached saved state bundle.
1749 * @return cached state bundle
Grace Kloba22ac16e2009-10-07 18:00:23 -07001750 */
1751 Bundle getSavedState() {
1752 return mSavedState;
1753 }
1754
John Reckaed9c542011-05-27 16:08:53 -07001755 Bundle getSavedState(boolean saveImages) {
1756 if (saveImages && mScreenshot != null) {
1757 Bundle b = new Bundle(mSavedState);
1758 b.putParcelable(SCREENSHOT, mScreenshot);
1759 return b;
1760 }
1761 return mSavedState;
1762 }
1763
Grace Kloba22ac16e2009-10-07 18:00:23 -07001764 /**
1765 * Set the saved state.
1766 */
1767 void setSavedState(Bundle state) {
1768 mSavedState = state;
1769 }
1770
1771 /**
1772 * @return TRUE if succeed in saving the state.
1773 */
1774 boolean saveState() {
1775 // If the WebView is null it means we ran low on memory and we already
1776 // stored the saved state in mSavedState.
1777 if (mMainView == null) {
1778 return mSavedState != null;
1779 }
John Reck24f18262011-06-17 14:47:20 -07001780 // If the tab is the homepage or has no URL, don't save it
1781 String homepage = BrowserSettings.getInstance().getHomePage();
1782 if (TextUtils.equals(homepage, mCurrentState.mUrl)
1783 || TextUtils.isEmpty(mCurrentState.mUrl)) {
1784 return false;
1785 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001786
1787 mSavedState = new Bundle();
John Reck541f55a2011-06-07 16:34:43 -07001788 mMainView.saveState(mSavedState);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001789
Michael Kolbc831b632011-05-11 09:30:34 -07001790 mSavedState.putLong(ID, mId);
John Reck30c714c2010-12-16 17:30:34 -08001791 mSavedState.putString(CURRURL, mCurrentState.mUrl);
1792 mSavedState.putString(CURRTITLE, mCurrentState.mTitle);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001793 if (mAppId != null) {
1794 mSavedState.putString(APPID, mAppId);
1795 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001796 // Remember the parent tab so the relationship can be restored.
Michael Kolbc831b632011-05-11 09:30:34 -07001797 if (mParent != null) {
1798 mSavedState.putLong(PARENTTAB, mParent.mId);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001799 }
John Reckb0a86db2011-05-24 14:05:58 -07001800 mSavedState.putBoolean(USERAGENT,
1801 mSettings.hasDesktopUseragent(getWebView()));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001802 return true;
1803 }
1804
1805 /*
1806 * Restore the state of the tab.
1807 */
1808 boolean restoreState(Bundle b) {
1809 if (b == null) {
1810 return false;
1811 }
1812 // Restore the internal state even if the WebView fails to restore.
1813 // This will maintain the app id, original url and close-on-exit values.
1814 mSavedState = null;
Michael Kolbc831b632011-05-11 09:30:34 -07001815 mId = b.getLong(ID);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001816 mAppId = b.getString(APPID);
Michael Kolbeb95db42011-03-03 10:38:40 -08001817 mScreenshot = b.getParcelable(SCREENSHOT);
John Reckb0a86db2011-05-24 14:05:58 -07001818 if (b.getBoolean(USERAGENT)
1819 != mSettings.hasDesktopUseragent(getWebView())) {
1820 mSettings.toggleDesktopUseragent(getWebView());
1821 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001822
1823 final WebBackForwardList list = mMainView.restoreState(b);
1824 if (list == null) {
1825 return false;
1826 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001827 return true;
1828 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04001829
Leon Scroggins1961ed22010-12-07 15:22:21 -05001830 public void updateBookmarkedStatus() {
John Recke969cc52010-12-21 17:24:43 -08001831 mDataController.queryBookmarkStatus(getUrl(), mIsBookmarkCallback);
Leon Scroggins1961ed22010-12-07 15:22:21 -05001832 }
1833
John Recke969cc52010-12-21 17:24:43 -08001834 private DataController.OnQueryUrlIsBookmark mIsBookmarkCallback
1835 = new DataController.OnQueryUrlIsBookmark() {
1836 @Override
1837 public void onQueryUrlIsBookmark(String url, boolean isBookmark) {
1838 if (mCurrentState.mUrl.equals(url)) {
1839 mCurrentState.mIsBookmarkedSite = isBookmark;
1840 mWebViewController.bookmarkedStatusHasChanged(Tab.this);
1841 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001842 }
John Recke969cc52010-12-21 17:24:43 -08001843 };
Michael Kolb1acef692011-03-08 14:12:06 -08001844
Michael Kolbeb95db42011-03-03 10:38:40 -08001845 public void setScreenshot(Bitmap screenshot) {
1846 mScreenshot = screenshot;
1847 }
1848
1849 public Bitmap getScreenshot() {
1850 return mScreenshot;
1851 }
1852
John Reck541f55a2011-06-07 16:34:43 -07001853 public boolean isSnapshot() {
John Reck541f55a2011-06-07 16:34:43 -07001854 return false;
1855 }
1856
John Reckd8c74522011-06-14 08:45:00 -07001857 public ContentValues createSnapshotValues() {
1858 if (mMainView == null) return null;
1859 ByteArrayOutputStream stream = new ByteArrayOutputStream();
1860 if (!mMainView.saveViewState(stream)) {
John Reck541f55a2011-06-07 16:34:43 -07001861 return null;
1862 }
John Reckd8c74522011-06-14 08:45:00 -07001863 byte[] data = stream.toByteArray();
1864 ContentResolver cr = mActivity.getContentResolver();
1865 ContentValues values = new ContentValues();
1866 values.put(Snapshots.TITLE, mCurrentState.mTitle);
1867 values.put(Snapshots.URL, mCurrentState.mUrl);
1868 values.put(Snapshots.VIEWSTATE, data);
1869 values.put(Snapshots.BACKGROUND, mMainView.getPageBackgroundColor());
1870 return values;
John Reck541f55a2011-06-07 16:34:43 -07001871 }
1872
Grace Kloba22ac16e2009-10-07 18:00:23 -07001873}