blob: 35080050edf35edbefa446e9fa32dc726601c158 [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
John Reck438bf462011-01-12 18:11:46 -080019import com.android.browser.homepages.HomeProvider;
Jeff Hamilton8ce956c2010-08-17 11:13:53 -050020import com.android.common.speech.LoggingEvents;
21
Michael Kolb8233fac2010-10-26 16:08:53 -070022import android.app.Activity;
Grace Kloba22ac16e2009-10-07 18:00:23 -070023import android.app.AlertDialog;
Leon Scroggins58d56c62010-01-28 15:12:40 -050024import android.app.SearchManager;
Grace Kloba22ac16e2009-10-07 18:00:23 -070025import android.content.ContentResolver;
John Reck30c714c2010-12-16 17:30:34 -080026import android.content.Context;
Grace Kloba22ac16e2009-10-07 18:00:23 -070027import android.content.DialogInterface;
Michael Kolbfe251992010-07-08 15:41:55 -070028import android.content.DialogInterface.OnCancelListener;
Jeff Hamilton8ce956c2010-08-17 11:13:53 -050029import android.content.Intent;
Grace Kloba22ac16e2009-10-07 18:00:23 -070030import android.graphics.Bitmap;
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;
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -050037import android.speech.RecognizerResultsIntent;
Grace Kloba22ac16e2009-10-07 18:00:23 -070038import android.util.Log;
39import android.view.KeyEvent;
40import android.view.LayoutInflater;
41import android.view.View;
Grace Kloba50c241e2010-04-20 11:07:50 -070042import android.view.ViewStub;
Ben Murdochc42addf2010-01-28 15:19:59 +000043import android.webkit.ConsoleMessage;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -050044import android.webkit.DownloadListener;
Grace Kloba22ac16e2009-10-07 18:00:23 -070045import android.webkit.GeolocationPermissions;
46import android.webkit.HttpAuthHandler;
47import android.webkit.SslErrorHandler;
48import android.webkit.URLUtil;
49import android.webkit.ValueCallback;
50import android.webkit.WebBackForwardList;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -050051import android.webkit.WebBackForwardListClient;
Grace Kloba22ac16e2009-10-07 18:00:23 -070052import android.webkit.WebChromeClient;
53import android.webkit.WebHistoryItem;
John Reck438bf462011-01-12 18:11:46 -080054import android.webkit.WebResourceResponse;
Grace Kloba22ac16e2009-10-07 18:00:23 -070055import android.webkit.WebStorage;
56import android.webkit.WebView;
57import android.webkit.WebViewClient;
Ben Murdoch1d676b62011-01-17 12:54:24 +000058import android.widget.CheckBox;
Grace Kloba22ac16e2009-10-07 18:00:23 -070059import android.widget.LinearLayout;
60import android.widget.TextView;
Ben Murdoch8029a772010-11-16 11:58:21 +000061import android.widget.Toast;
Grace Kloba22ac16e2009-10-07 18:00:23 -070062
Michael Kolbfe251992010-07-08 15:41:55 -070063import java.util.ArrayList;
64import java.util.HashMap;
65import java.util.Iterator;
66import java.util.LinkedList;
67import java.util.Map;
68import java.util.Vector;
69
Grace Kloba22ac16e2009-10-07 18:00:23 -070070/**
71 * Class for maintaining Tabs with a main WebView and a subwindow.
72 */
73class Tab {
Michael Kolb8233fac2010-10-26 16:08:53 -070074
Grace Kloba22ac16e2009-10-07 18:00:23 -070075 // Log Tag
76 private static final String LOGTAG = "Tab";
Ben Murdochc42addf2010-01-28 15:19:59 +000077 // Special case the logtag for messages for the Console to make it easier to
78 // filter them and match the logtag used for these messages in older versions
79 // of the browser.
80 private static final String CONSOLE_LOGTAG = "browser";
81
John Reck30c714c2010-12-16 17:30:34 -080082 public enum LockIcon {
83 LOCK_ICON_UNSECURE,
84 LOCK_ICON_SECURE,
85 LOCK_ICON_MIXED,
86 }
Michael Kolb8233fac2010-10-26 16:08:53 -070087
88 Activity mActivity;
89 private WebViewController mWebViewController;
90
Grace Kloba22ac16e2009-10-07 18:00:23 -070091 // The Geolocation permissions prompt
92 private GeolocationPermissionsPrompt mGeolocationPermissionsPrompt;
93 // Main WebView wrapper
Michael Kolba713ec82010-11-29 17:27:06 -080094 private View mContainer;
Grace Kloba22ac16e2009-10-07 18:00:23 -070095 // Main WebView
96 private WebView mMainView;
97 // Subwindow container
98 private View mSubViewContainer;
99 // Subwindow WebView
100 private WebView mSubView;
101 // Saved bundle for when we are running low on memory. It contains the
102 // information needed to restore the WebView if the user goes back to the
103 // tab.
104 private Bundle mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700105 // Parent Tab. This is the Tab that created this Tab, or null if the Tab was
106 // created by the UI
107 private Tab mParentTab;
108 // Tab that constructed by this Tab. This is used when this Tab is
109 // destroyed, it clears all mParentTab values in the children.
110 private Vector<Tab> mChildTabs;
111 // If true, the tab will be removed when back out of the first page.
112 private boolean mCloseOnExit;
113 // If true, the tab is in the foreground of the current activity.
114 private boolean mInForeground;
Michael Kolb8233fac2010-10-26 16:08:53 -0700115 // If true, the tab is in page loading state (after onPageStarted,
116 // before onPageFinsihed)
117 private boolean mInPageLoad;
John Reck30c714c2010-12-16 17:30:34 -0800118 // The last reported progress of the current page
119 private int mPageLoadProgress;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000120 // The time the load started, used to find load page time
121 private long mLoadStartTime;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700122 // Application identifier used to find tabs that another application wants
123 // to reuse.
124 private String mAppId;
125 // Keep the original url around to avoid killing the old WebView if the url
126 // has not changed.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700127 // Error console for the tab
128 private ErrorConsoleView mErrorConsole;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -0500129 // The listener that gets invoked when a download is started from the
130 // mMainView
131 private final DownloadListener mDownloadListener;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500132 // Listener used to know when we move forward or back in the history list.
133 private final WebBackForwardListClient mWebBackForwardListClient;
John Recke969cc52010-12-21 17:24:43 -0800134 private DataController mDataController;
Patrick Scott92066772011-03-10 08:46:27 -0500135 // State of the auto-login request.
136 private DeviceAccountLogin mDeviceAccountLogin;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700137
138 // AsyncTask for downloading touch icons
139 DownloadTouchIcon mTouchIconLoader;
140
Michael Kolbeb95db42011-03-03 10:38:40 -0800141 private Bitmap mScreenshot;
John Reck35e9dd62011-04-25 09:01:54 -0700142 private BrowserSettings mSettings;
Michael Kolbeb95db42011-03-03 10:38:40 -0800143
John Reck30c714c2010-12-16 17:30:34 -0800144 // All the state needed for a page
145 private static class PageState {
146 String mUrl;
147 String mTitle;
148 LockIcon mLockIcon;
149 Bitmap mFavicon;
John Recke969cc52010-12-21 17:24:43 -0800150 Boolean mIsBookmarkedSite = false;
John Reck30c714c2010-12-16 17:30:34 -0800151
152 PageState(Context c, boolean incognito) {
153 if (incognito) {
154 mUrl = "browser:incognito";
155 mTitle = c.getString(R.string.new_incognito_tab);
John Reck30c714c2010-12-16 17:30:34 -0800156 } else {
157 mUrl = "";
158 mTitle = c.getString(R.string.new_tab);
John Reck30c714c2010-12-16 17:30:34 -0800159 }
Justin Hodc6cbb72011-01-24 15:14:54 -0800160 mFavicon = BitmapFactory.decodeResource(
161 c.getResources(), R.drawable.app_web_browser_sm);
John Reck30c714c2010-12-16 17:30:34 -0800162 mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
163 }
164
165 PageState(Context c, boolean incognito, String url, Bitmap favicon) {
166 mUrl = url;
167 mTitle = null;
168 if (URLUtil.isHttpsUrl(url)) {
169 mLockIcon = LockIcon.LOCK_ICON_SECURE;
170 } else {
171 mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
172 }
173 if (favicon != null) {
174 mFavicon = favicon;
175 } else {
Justin Hodc6cbb72011-01-24 15:14:54 -0800176 mFavicon = BitmapFactory.decodeResource(
177 c.getResources(), R.drawable.app_web_browser_sm);
John Reck30c714c2010-12-16 17:30:34 -0800178 }
179 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700180 }
181
John Reck30c714c2010-12-16 17:30:34 -0800182 // The current/loading page's state
183 private PageState mCurrentState;
184
Grace Kloba22ac16e2009-10-07 18:00:23 -0700185 // Used for saving and restoring each Tab
John Reck30c714c2010-12-16 17:30:34 -0800186 // TODO: Figure out who uses what and where
187 // Some of these aren't use in this class, and some are only used in
188 // restoring state but not saving it - FIX THIS
Grace Kloba22ac16e2009-10-07 18:00:23 -0700189 static final String WEBVIEW = "webview";
190 static final String NUMTABS = "numTabs";
191 static final String CURRTAB = "currentTab";
192 static final String CURRURL = "currentUrl";
193 static final String CURRTITLE = "currentTitle";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700194 static final String CLOSEONEXIT = "closeonexit";
195 static final String PARENTTAB = "parentTab";
196 static final String APPID = "appid";
197 static final String ORIGINALURL = "originalUrl";
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700198 static final String INCOGNITO = "privateBrowsingEnabled";
Michael Kolbeb95db42011-03-03 10:38:40 -0800199 static final String SCREENSHOT = "screenshot";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700200
201 // -------------------------------------------------------------------------
202
Leon Scroggins58d56c62010-01-28 15:12:40 -0500203 /**
204 * Private information regarding the latest voice search. If the Tab is not
205 * in voice search mode, this will be null.
206 */
207 private VoiceSearchData mVoiceSearchData;
208 /**
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400209 * Remove voice search mode from this tab.
210 */
211 public void revertVoiceSearchMode() {
212 if (mVoiceSearchData != null) {
213 mVoiceSearchData = null;
214 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700215 mWebViewController.revertVoiceSearchMode(this);
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400216 }
217 }
218 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700219
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400220 /**
Leon Scroggins58d56c62010-01-28 15:12:40 -0500221 * Return whether the tab is in voice search mode.
222 */
223 public boolean isInVoiceSearchMode() {
224 return mVoiceSearchData != null;
225 }
226 /**
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400227 * Return true if the Tab is in voice search mode and the voice search
228 * Intent came with a String identifying that Google provided the Intent.
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500229 */
230 public boolean voiceSearchSourceIsGoogle() {
231 return mVoiceSearchData != null && mVoiceSearchData.mSourceIsGoogle;
232 }
233 /**
Leon Scroggins58d56c62010-01-28 15:12:40 -0500234 * Get the title to display for the current voice search page. If the Tab
235 * is not in voice search mode, return null.
236 */
237 public String getVoiceDisplayTitle() {
238 if (mVoiceSearchData == null) return null;
239 return mVoiceSearchData.mLastVoiceSearchTitle;
240 }
241 /**
242 * Get the latest array of voice search results, to be passed to the
243 * BrowserProvider. If the Tab is not in voice search mode, return null.
244 */
245 public ArrayList<String> getVoiceSearchResults() {
246 if (mVoiceSearchData == null) return null;
247 return mVoiceSearchData.mVoiceSearchResults;
248 }
249 /**
250 * Activate voice search mode.
251 * @param intent Intent which has the results to use, or an index into the
252 * results when reusing the old results.
253 */
254 /* package */ void activateVoiceSearchMode(Intent intent) {
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500255 int index = 0;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500256 ArrayList<String> results = intent.getStringArrayListExtra(
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -0500257 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_STRINGS);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500258 if (results != null) {
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500259 ArrayList<String> urls = intent.getStringArrayListExtra(
260 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_URLS);
261 ArrayList<String> htmls = intent.getStringArrayListExtra(
262 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_HTML);
263 ArrayList<String> baseUrls = intent.getStringArrayListExtra(
264 RecognizerResultsIntent
265 .EXTRA_VOICE_SEARCH_RESULT_HTML_BASE_URLS);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500266 // This tab is now entering voice search mode for the first time, or
267 // a new voice search was done.
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500268 int size = results.size();
269 if (urls == null || size != urls.size()) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500270 throw new AssertionError("improper extras passed in Intent");
271 }
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500272 if (htmls == null || htmls.size() != size || baseUrls == null ||
273 (baseUrls.size() != size && baseUrls.size() != 1)) {
274 // If either of these arrays are empty/incorrectly sized, ignore
275 // them.
276 htmls = null;
277 baseUrls = null;
278 }
279 mVoiceSearchData = new VoiceSearchData(results, urls, htmls,
280 baseUrls);
Leon Scroggins9df94972010-03-08 18:20:35 -0500281 mVoiceSearchData.mHeaders = intent.getParcelableArrayListExtra(
282 RecognizerResultsIntent
283 .EXTRA_VOICE_SEARCH_RESULT_HTTP_HEADERS);
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500284 mVoiceSearchData.mSourceIsGoogle = intent.getBooleanExtra(
285 VoiceSearchData.SOURCE_IS_GOOGLE, false);
Leon Scroggins2ee4a5a2010-03-15 16:56:57 -0400286 mVoiceSearchData.mVoiceSearchIntent = new Intent(intent);
Leon Scrogginse10dde52010-03-08 19:53:03 -0500287 }
288 String extraData = intent.getStringExtra(
289 SearchManager.EXTRA_DATA_KEY);
290 if (extraData != null) {
291 index = Integer.parseInt(extraData);
292 if (index >= mVoiceSearchData.mVoiceSearchResults.size()) {
293 throw new AssertionError("index must be less than "
294 + "size of mVoiceSearchResults");
295 }
296 if (mVoiceSearchData.mSourceIsGoogle) {
297 Intent logIntent = new Intent(
298 LoggingEvents.ACTION_LOG_EVENT);
299 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
300 LoggingEvents.VoiceSearch.N_BEST_CHOOSE);
301 logIntent.putExtra(
302 LoggingEvents.VoiceSearch.EXTRA_N_BEST_CHOOSE_INDEX,
303 index);
304 mActivity.sendBroadcast(logIntent);
305 }
306 if (mVoiceSearchData.mVoiceSearchIntent != null) {
Leon Scroggins2ee4a5a2010-03-15 16:56:57 -0400307 // Copy the Intent, so that each history item will have its own
308 // Intent, with different (or none) extra data.
309 Intent latest = new Intent(mVoiceSearchData.mVoiceSearchIntent);
310 latest.putExtra(SearchManager.EXTRA_DATA_KEY, extraData);
311 mVoiceSearchData.mVoiceSearchIntent = latest;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500312 }
313 }
314 mVoiceSearchData.mLastVoiceSearchTitle
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500315 = mVoiceSearchData.mVoiceSearchResults.get(index);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500316 if (mInForeground) {
Michael Kolb11d19782011-03-20 10:17:40 -0700317 mWebViewController.activateVoiceSearchMode(
318 mVoiceSearchData.mLastVoiceSearchTitle,
319 mVoiceSearchData.mVoiceSearchResults);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500320 }
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500321 if (mVoiceSearchData.mVoiceSearchHtmls != null) {
322 // When index was found it was already ensured that it was valid
323 String uriString = mVoiceSearchData.mVoiceSearchHtmls.get(index);
324 if (uriString != null) {
325 Uri dataUri = Uri.parse(uriString);
326 if (RecognizerResultsIntent.URI_SCHEME_INLINE.equals(
327 dataUri.getScheme())) {
328 // If there is only one base URL, use it. If there are
329 // more, there will be one for each index, so use the base
330 // URL corresponding to the index.
331 String baseUrl = mVoiceSearchData.mVoiceSearchBaseUrls.get(
332 mVoiceSearchData.mVoiceSearchBaseUrls.size() > 1 ?
333 index : 0);
334 mVoiceSearchData.mLastVoiceSearchUrl = baseUrl;
335 mMainView.loadDataWithBaseURL(baseUrl,
336 uriString.substring(RecognizerResultsIntent
337 .URI_SCHEME_INLINE.length() + 1), "text/html",
338 "utf-8", baseUrl);
339 return;
340 }
341 }
342 }
Leon Scroggins58d56c62010-01-28 15:12:40 -0500343 mVoiceSearchData.mLastVoiceSearchUrl
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500344 = mVoiceSearchData.mVoiceSearchUrls.get(index);
345 if (null == mVoiceSearchData.mLastVoiceSearchUrl) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700346 mVoiceSearchData.mLastVoiceSearchUrl = UrlUtils.smartUrlFilter(
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500347 mVoiceSearchData.mLastVoiceSearchTitle);
348 }
Leon Scroggins9df94972010-03-08 18:20:35 -0500349 Map<String, String> headers = null;
350 if (mVoiceSearchData.mHeaders != null) {
351 int bundleIndex = mVoiceSearchData.mHeaders.size() == 1 ? 0
352 : index;
353 Bundle bundle = mVoiceSearchData.mHeaders.get(bundleIndex);
354 if (bundle != null && !bundle.isEmpty()) {
355 Iterator<String> iter = bundle.keySet().iterator();
356 headers = new HashMap<String, String>();
357 while (iter.hasNext()) {
358 String key = iter.next();
359 headers.put(key, bundle.getString(key));
360 }
361 }
362 }
363 mMainView.loadUrl(mVoiceSearchData.mLastVoiceSearchUrl, headers);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500364 }
365 /* package */ static class VoiceSearchData {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500366 public VoiceSearchData(ArrayList<String> results,
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500367 ArrayList<String> urls, ArrayList<String> htmls,
368 ArrayList<String> baseUrls) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500369 mVoiceSearchResults = results;
370 mVoiceSearchUrls = urls;
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500371 mVoiceSearchHtmls = htmls;
372 mVoiceSearchBaseUrls = baseUrls;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500373 }
374 /*
375 * ArrayList of suggestions to be displayed when opening the
376 * SearchManager
377 */
378 public ArrayList<String> mVoiceSearchResults;
379 /*
380 * ArrayList of urls, associated with the suggestions in
381 * mVoiceSearchResults.
382 */
383 public ArrayList<String> mVoiceSearchUrls;
384 /*
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500385 * ArrayList holding content to load for each item in
386 * mVoiceSearchResults.
387 */
388 public ArrayList<String> mVoiceSearchHtmls;
389 /*
390 * ArrayList holding base urls for the items in mVoiceSearchResults.
391 * If non null, this will either have the same size as
392 * mVoiceSearchResults or have a size of 1, in which case all will use
393 * the same base url
394 */
395 public ArrayList<String> mVoiceSearchBaseUrls;
396 /*
Leon Scroggins58d56c62010-01-28 15:12:40 -0500397 * The last url provided by voice search. Used for comparison to see if
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500398 * we are going to a page by some method besides voice search.
Leon Scroggins58d56c62010-01-28 15:12:40 -0500399 */
400 public String mLastVoiceSearchUrl;
401 /**
402 * The last title used for voice search. Needed to update the title bar
403 * when switching tabs.
404 */
405 public String mLastVoiceSearchTitle;
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500406 /**
407 * Whether the Intent which turned on voice search mode contained the
408 * String signifying that Google was the source.
409 */
410 public boolean mSourceIsGoogle;
411 /**
Leon Scroggins9df94972010-03-08 18:20:35 -0500412 * List of headers to be passed into the WebView containing location
413 * information
414 */
415 public ArrayList<Bundle> mHeaders;
416 /**
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500417 * The Intent used to invoke voice search. Placed on the
418 * WebHistoryItem so that when coming back to a previous voice search
419 * page we can again activate voice search.
420 */
Leon Scrogginse10dde52010-03-08 19:53:03 -0500421 public Intent mVoiceSearchIntent;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500422 /**
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500423 * String used to identify Google as the source of voice search.
424 */
425 public static String SOURCE_IS_GOOGLE
426 = "android.speech.extras.SOURCE_IS_GOOGLE";
Leon Scroggins58d56c62010-01-28 15:12:40 -0500427 }
428
Grace Kloba22ac16e2009-10-07 18:00:23 -0700429 // Container class for the next error dialog that needs to be displayed
430 private class ErrorDialog {
431 public final int mTitle;
432 public final String mDescription;
433 public final int mError;
434 ErrorDialog(int title, String desc, int error) {
435 mTitle = title;
436 mDescription = desc;
437 mError = error;
438 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700439 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700440
441 private void processNextError() {
442 if (mQueuedErrors == null) {
443 return;
444 }
445 // The first one is currently displayed so just remove it.
446 mQueuedErrors.removeFirst();
447 if (mQueuedErrors.size() == 0) {
448 mQueuedErrors = null;
449 return;
450 }
451 showError(mQueuedErrors.getFirst());
452 }
453
454 private DialogInterface.OnDismissListener mDialogListener =
455 new DialogInterface.OnDismissListener() {
456 public void onDismiss(DialogInterface d) {
457 processNextError();
458 }
459 };
460 private LinkedList<ErrorDialog> mQueuedErrors;
461
462 private void queueError(int err, String desc) {
463 if (mQueuedErrors == null) {
464 mQueuedErrors = new LinkedList<ErrorDialog>();
465 }
466 for (ErrorDialog d : mQueuedErrors) {
467 if (d.mError == err) {
468 // Already saw a similar error, ignore the new one.
469 return;
470 }
471 }
472 ErrorDialog errDialog = new ErrorDialog(
473 err == WebViewClient.ERROR_FILE_NOT_FOUND ?
474 R.string.browserFrameFileErrorLabel :
475 R.string.browserFrameNetworkErrorLabel,
476 desc, err);
477 mQueuedErrors.addLast(errDialog);
478
479 // Show the dialog now if the queue was empty and it is in foreground
480 if (mQueuedErrors.size() == 1 && mInForeground) {
481 showError(errDialog);
482 }
483 }
484
485 private void showError(ErrorDialog errDialog) {
486 if (mInForeground) {
487 AlertDialog d = new AlertDialog.Builder(mActivity)
488 .setTitle(errDialog.mTitle)
489 .setMessage(errDialog.mDescription)
490 .setPositiveButton(R.string.ok, null)
491 .create();
492 d.setOnDismissListener(mDialogListener);
493 d.show();
494 }
495 }
496
497 // -------------------------------------------------------------------------
498 // WebViewClient implementation for the main WebView
499 // -------------------------------------------------------------------------
500
501 private final WebViewClient mWebViewClient = new WebViewClient() {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500502 private Message mDontResend;
503 private Message mResend;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700504 @Override
505 public void onPageStarted(WebView view, String url, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700506 mInPageLoad = true;
John Reck30c714c2010-12-16 17:30:34 -0800507 mPageLoadProgress = 0;
508 mCurrentState = new PageState(mActivity,
509 view.isPrivateBrowsingEnabled(), url, favicon);
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000510 mLoadStartTime = SystemClock.uptimeMillis();
Leon Scroggins58d56c62010-01-28 15:12:40 -0500511 if (mVoiceSearchData != null
512 && !url.equals(mVoiceSearchData.mLastVoiceSearchUrl)) {
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500513 if (mVoiceSearchData.mSourceIsGoogle) {
514 Intent i = new Intent(LoggingEvents.ACTION_LOG_EVENT);
515 i.putExtra(LoggingEvents.EXTRA_FLUSH, true);
516 mActivity.sendBroadcast(i);
517 }
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400518 revertVoiceSearchMode();
Leon Scroggins58d56c62010-01-28 15:12:40 -0500519 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700520
Grace Kloba22ac16e2009-10-07 18:00:23 -0700521
522 // If we start a touch icon load and then load a new page, we don't
523 // want to cancel the current touch icon loader. But, we do want to
524 // create a new one when the touch icon url is known.
525 if (mTouchIconLoader != null) {
526 mTouchIconLoader.mTab = null;
527 mTouchIconLoader = null;
528 }
529
530 // reset the error console
531 if (mErrorConsole != null) {
532 mErrorConsole.clearErrorMessages();
Michael Kolb8233fac2010-10-26 16:08:53 -0700533 if (mWebViewController.shouldShowErrorConsole()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700534 mErrorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
535 }
536 }
537
Patrick Scott92066772011-03-10 08:46:27 -0500538 // Cancel the auto-login process.
539 if (mDeviceAccountLogin != null) {
540 mDeviceAccountLogin.cancel();
541 mDeviceAccountLogin = null;
542 mWebViewController.hideAutoLogin(Tab.this);
543 }
544
Grace Kloba22ac16e2009-10-07 18:00:23 -0700545 // finally update the UI in the activity if it is in the foreground
John Reck324d4402011-01-11 16:56:42 -0800546 mWebViewController.onPageStarted(Tab.this, view, favicon);
Leon Scroggins4cd97792010-12-03 15:31:56 -0500547
John Recke969cc52010-12-21 17:24:43 -0800548 updateBookmarkedStatus();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700549 }
550
551 @Override
552 public void onPageFinished(WebView view, String url) {
John Reck5b691842010-11-29 11:21:13 -0800553 if (!isPrivateBrowsingEnabled()) {
554 LogTag.logPageFinishedLoading(
555 url, SystemClock.uptimeMillis() - mLoadStartTime);
556 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700557 mInPageLoad = false;
John Reck30c714c2010-12-16 17:30:34 -0800558 // Sync state (in case of stop/timeout)
559 mCurrentState.mUrl = view.getUrl();
John Reck6c702ee2011-01-07 09:41:53 -0800560 if (mCurrentState.mUrl == null) {
561 mCurrentState.mUrl = url != null ? url : "";
562 }
John Reck30c714c2010-12-16 17:30:34 -0800563 mCurrentState.mTitle = view.getTitle();
564 mCurrentState.mFavicon = view.getFavicon();
565 if (!URLUtil.isHttpsUrl(mCurrentState.mUrl)) {
566 // In case we stop when loading an HTTPS page from an HTTP page
567 // but before a provisional load occurred
568 mCurrentState.mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
569 }
John Reck324d4402011-01-11 16:56:42 -0800570 mWebViewController.onPageFinished(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700571 }
572
573 // return true if want to hijack the url to let another app to handle it
574 @Override
575 public boolean shouldOverrideUrlLoading(WebView view, String url) {
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400576 if (voiceSearchSourceIsGoogle()) {
577 // This method is called when the user clicks on a link.
578 // VoiceSearchMode is turned off when the user leaves the
579 // Google results page, so at this point the user must be on
580 // that page. If the user clicked a link on that page, assume
581 // that the voice search was effective, and broadcast an Intent
582 // so a receiver can take note of that fact.
583 Intent logIntent = new Intent(LoggingEvents.ACTION_LOG_EVENT);
584 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
585 LoggingEvents.VoiceSearch.RESULT_CLICKED);
586 mActivity.sendBroadcast(logIntent);
587 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700588 if (mInForeground) {
Michael Kolb18eb3772010-12-10 14:29:51 -0800589 return mWebViewController.shouldOverrideUrlLoading(Tab.this,
590 view, url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700591 } else {
592 return false;
593 }
594 }
595
596 /**
597 * Updates the lock icon. This method is called when we discover another
598 * resource to be loaded for this page (for example, javascript). While
599 * we update the icon type, we do not update the lock icon itself until
600 * we are done loading, it is slightly more secure this way.
601 */
602 @Override
603 public void onLoadResource(WebView view, String url) {
604 if (url != null && url.length() > 0) {
605 // It is only if the page claims to be secure that we may have
606 // to update the lock:
John Reck30c714c2010-12-16 17:30:34 -0800607 if (mCurrentState.mLockIcon == LockIcon.LOCK_ICON_SECURE) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700608 // If NOT a 'safe' url, change the lock to mixed content!
609 if (!(URLUtil.isHttpsUrl(url) || URLUtil.isDataUrl(url)
610 || URLUtil.isAboutUrl(url))) {
John Reck30c714c2010-12-16 17:30:34 -0800611 mCurrentState.mLockIcon = LockIcon.LOCK_ICON_MIXED;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700612 }
613 }
614 }
615 }
616
617 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700618 * Show a dialog informing the user of the network error reported by
619 * WebCore if it is in the foreground.
620 */
621 @Override
622 public void onReceivedError(WebView view, int errorCode,
623 String description, String failingUrl) {
624 if (errorCode != WebViewClient.ERROR_HOST_LOOKUP &&
625 errorCode != WebViewClient.ERROR_CONNECT &&
626 errorCode != WebViewClient.ERROR_BAD_URL &&
627 errorCode != WebViewClient.ERROR_UNSUPPORTED_SCHEME &&
628 errorCode != WebViewClient.ERROR_FILE) {
629 queueError(errorCode, description);
630 }
Jeff Hamilton47654f42010-09-07 09:57:51 -0500631
632 // Don't log URLs when in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -0700633 if (!isPrivateBrowsingEnabled()) {
Jeff Hamilton47654f42010-09-07 09:57:51 -0500634 Log.e(LOGTAG, "onReceivedError " + errorCode + " " + failingUrl
635 + " " + description);
636 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700637 }
638
639 /**
640 * Check with the user if it is ok to resend POST data as the page they
641 * are trying to navigate to is the result of a POST.
642 */
643 @Override
644 public void onFormResubmission(WebView view, final Message dontResend,
645 final Message resend) {
646 if (!mInForeground) {
647 dontResend.sendToTarget();
648 return;
649 }
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500650 if (mDontResend != null) {
651 Log.w(LOGTAG, "onFormResubmission should not be called again "
652 + "while dialog is still up");
653 dontResend.sendToTarget();
654 return;
655 }
656 mDontResend = dontResend;
657 mResend = resend;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700658 new AlertDialog.Builder(mActivity).setTitle(
659 R.string.browserFrameFormResubmitLabel).setMessage(
660 R.string.browserFrameFormResubmitMessage)
661 .setPositiveButton(R.string.ok,
662 new DialogInterface.OnClickListener() {
663 public void onClick(DialogInterface dialog,
664 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500665 if (mResend != null) {
666 mResend.sendToTarget();
667 mResend = null;
668 mDontResend = null;
669 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700670 }
671 }).setNegativeButton(R.string.cancel,
672 new DialogInterface.OnClickListener() {
673 public void onClick(DialogInterface dialog,
674 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500675 if (mDontResend != null) {
676 mDontResend.sendToTarget();
677 mResend = null;
678 mDontResend = null;
679 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700680 }
681 }).setOnCancelListener(new OnCancelListener() {
682 public void onCancel(DialogInterface dialog) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500683 if (mDontResend != null) {
684 mDontResend.sendToTarget();
685 mResend = null;
686 mDontResend = null;
687 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700688 }
689 }).show();
690 }
691
692 /**
693 * Insert the url into the visited history database.
694 * @param url The url to be inserted.
695 * @param isReload True if this url is being reloaded.
696 * FIXME: Not sure what to do when reloading the page.
697 */
698 @Override
699 public void doUpdateVisitedHistory(WebView view, String url,
700 boolean isReload) {
John Reck324d4402011-01-11 16:56:42 -0800701 mWebViewController.doUpdateVisitedHistory(Tab.this, isReload);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700702 }
703
704 /**
705 * Displays SSL error(s) dialog to the user.
706 */
707 @Override
708 public void onReceivedSslError(final WebView view,
709 final SslErrorHandler handler, final SslError error) {
710 if (!mInForeground) {
711 handler.cancel();
John Reck30c714c2010-12-16 17:30:34 -0800712 setLockIconType(LockIcon.LOCK_ICON_UNSECURE);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700713 return;
714 }
John Reck35e9dd62011-04-25 09:01:54 -0700715 if (mSettings.showSecurityWarnings()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700716 final LayoutInflater factory =
717 LayoutInflater.from(mActivity);
718 final View warningsView =
719 factory.inflate(R.layout.ssl_warnings, null);
720 final LinearLayout placeholder =
721 (LinearLayout)warningsView.findViewById(R.id.placeholder);
722
723 if (error.hasError(SslError.SSL_UNTRUSTED)) {
724 LinearLayout ll = (LinearLayout)factory
725 .inflate(R.layout.ssl_warning, null);
726 ((TextView)ll.findViewById(R.id.warning))
727 .setText(R.string.ssl_untrusted);
728 placeholder.addView(ll);
729 }
730
731 if (error.hasError(SslError.SSL_IDMISMATCH)) {
732 LinearLayout ll = (LinearLayout)factory
733 .inflate(R.layout.ssl_warning, null);
734 ((TextView)ll.findViewById(R.id.warning))
735 .setText(R.string.ssl_mismatch);
736 placeholder.addView(ll);
737 }
738
739 if (error.hasError(SslError.SSL_EXPIRED)) {
740 LinearLayout ll = (LinearLayout)factory
741 .inflate(R.layout.ssl_warning, null);
742 ((TextView)ll.findViewById(R.id.warning))
743 .setText(R.string.ssl_expired);
744 placeholder.addView(ll);
745 }
746
747 if (error.hasError(SslError.SSL_NOTYETVALID)) {
748 LinearLayout ll = (LinearLayout)factory
749 .inflate(R.layout.ssl_warning, null);
750 ((TextView)ll.findViewById(R.id.warning))
751 .setText(R.string.ssl_not_yet_valid);
752 placeholder.addView(ll);
753 }
754
755 new AlertDialog.Builder(mActivity).setTitle(
756 R.string.security_warning).setIcon(
757 android.R.drawable.ic_dialog_alert).setView(
758 warningsView).setPositiveButton(R.string.ssl_continue,
759 new DialogInterface.OnClickListener() {
760 public void onClick(DialogInterface dialog,
761 int whichButton) {
762 handler.proceed();
763 }
764 }).setNeutralButton(R.string.view_certificate,
765 new DialogInterface.OnClickListener() {
766 public void onClick(DialogInterface dialog,
767 int whichButton) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700768 mWebViewController.showSslCertificateOnError(view,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700769 handler, error);
770 }
Ben Murdocha49b8292010-11-16 11:56:04 +0000771 }).setNegativeButton(R.string.ssl_go_back,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700772 new DialogInterface.OnClickListener() {
773 public void onClick(DialogInterface dialog,
774 int whichButton) {
John Reck30c714c2010-12-16 17:30:34 -0800775 dialog.cancel();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700776 }
777 }).setOnCancelListener(
778 new DialogInterface.OnCancelListener() {
779 public void onCancel(DialogInterface dialog) {
780 handler.cancel();
John Reck30c714c2010-12-16 17:30:34 -0800781 setLockIconType(LockIcon.LOCK_ICON_UNSECURE);
782 mWebViewController.onUserCanceledSsl(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700783 }
784 }).show();
785 } else {
786 handler.proceed();
787 }
788 }
789
790 /**
791 * Handles an HTTP authentication request.
792 *
793 * @param handler The authentication handler
794 * @param host The host
795 * @param realm The realm
796 */
797 @Override
798 public void onReceivedHttpAuthRequest(WebView view,
799 final HttpAuthHandler handler, final String host,
800 final String realm) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700801 mWebViewController.onReceivedHttpAuthRequest(Tab.this, view, handler, host, realm);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700802 }
803
804 @Override
John Reck438bf462011-01-12 18:11:46 -0800805 public WebResourceResponse shouldInterceptRequest(WebView view,
806 String url) {
807 WebResourceResponse res = HomeProvider.shouldInterceptRequest(
808 mActivity, url);
809 return res;
810 }
811
812 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700813 public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
814 if (!mInForeground) {
815 return false;
816 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700817 return mWebViewController.shouldOverrideKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700818 }
819
820 @Override
821 public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700822 if (!mInForeground) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700823 return;
824 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700825 mWebViewController.onUnhandledKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700826 }
Patrick Scott92066772011-03-10 08:46:27 -0500827
828 @Override
829 public void onReceivedLoginRequest(WebView view, String realm,
830 String account, String args) {
831 new DeviceAccountLogin(mActivity, view, Tab.this, mWebViewController)
832 .handleLogin(realm, account, args);
833 }
834
Grace Kloba22ac16e2009-10-07 18:00:23 -0700835 };
836
Patrick Scott92066772011-03-10 08:46:27 -0500837 // Called by DeviceAccountLogin when the Tab needs to have the auto-login UI
838 // displayed.
839 void setDeviceAccountLogin(DeviceAccountLogin login) {
840 mDeviceAccountLogin = login;
841 }
842
843 // Returns non-null if the title bar should display the auto-login UI.
844 DeviceAccountLogin getDeviceAccountLogin() {
845 return mDeviceAccountLogin;
846 }
847
Grace Kloba22ac16e2009-10-07 18:00:23 -0700848 // -------------------------------------------------------------------------
849 // WebChromeClient implementation for the main WebView
850 // -------------------------------------------------------------------------
851
852 private final WebChromeClient mWebChromeClient = new WebChromeClient() {
853 // Helper method to create a new tab or sub window.
854 private void createWindow(final boolean dialog, final Message msg) {
855 WebView.WebViewTransport transport =
856 (WebView.WebViewTransport) msg.obj;
857 if (dialog) {
858 createSubWindow();
Michael Kolb8233fac2010-10-26 16:08:53 -0700859 mWebViewController.attachSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700860 transport.setWebView(mSubView);
861 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -0700862 final Tab newTab = mWebViewController.openTabAndShow(
Michael Kolb18eb3772010-12-10 14:29:51 -0800863 Tab.this,
Michael Kolb8233fac2010-10-26 16:08:53 -0700864 IntentHandler.EMPTY_URL_DATA, false, null);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700865 if (newTab != Tab.this) {
866 Tab.this.addChildTab(newTab);
867 }
868 transport.setWebView(newTab.getWebView());
869 }
870 msg.sendToTarget();
871 }
872
873 @Override
874 public boolean onCreateWindow(WebView view, final boolean dialog,
875 final boolean userGesture, final Message resultMsg) {
876 // only allow new window or sub window for the foreground case
877 if (!mInForeground) {
878 return false;
879 }
880 // Short-circuit if we can't create any more tabs or sub windows.
881 if (dialog && mSubView != null) {
882 new AlertDialog.Builder(mActivity)
883 .setTitle(R.string.too_many_subwindows_dialog_title)
884 .setIcon(android.R.drawable.ic_dialog_alert)
885 .setMessage(R.string.too_many_subwindows_dialog_message)
886 .setPositiveButton(R.string.ok, null)
887 .show();
888 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700889 } else if (!mWebViewController.getTabControl().canCreateNewTab()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700890 new AlertDialog.Builder(mActivity)
891 .setTitle(R.string.too_many_windows_dialog_title)
892 .setIcon(android.R.drawable.ic_dialog_alert)
893 .setMessage(R.string.too_many_windows_dialog_message)
894 .setPositiveButton(R.string.ok, null)
895 .show();
896 return false;
897 }
898
899 // Short-circuit if this was a user gesture.
900 if (userGesture) {
901 createWindow(dialog, resultMsg);
902 return true;
903 }
904
905 // Allow the popup and create the appropriate window.
906 final AlertDialog.OnClickListener allowListener =
907 new AlertDialog.OnClickListener() {
908 public void onClick(DialogInterface d,
909 int which) {
910 createWindow(dialog, resultMsg);
911 }
912 };
913
914 // Block the popup by returning a null WebView.
915 final AlertDialog.OnClickListener blockListener =
916 new AlertDialog.OnClickListener() {
917 public void onClick(DialogInterface d, int which) {
918 resultMsg.sendToTarget();
919 }
920 };
921
922 // Build a confirmation dialog to display to the user.
923 final AlertDialog d =
924 new AlertDialog.Builder(mActivity)
925 .setTitle(R.string.attention)
926 .setIcon(android.R.drawable.ic_dialog_alert)
927 .setMessage(R.string.popup_window_attempt)
928 .setPositiveButton(R.string.allow, allowListener)
929 .setNegativeButton(R.string.block, blockListener)
930 .setCancelable(false)
931 .create();
932
933 // Show the confirmation dialog.
934 d.show();
935 return true;
936 }
937
938 @Override
Patrick Scotteb5061b2009-11-18 15:00:30 -0500939 public void onRequestFocus(WebView view) {
940 if (!mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700941 mWebViewController.switchToTab(mWebViewController.getTabControl().getTabIndex(
Patrick Scotteb5061b2009-11-18 15:00:30 -0500942 Tab.this));
943 }
944 }
945
946 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700947 public void onCloseWindow(WebView window) {
948 if (mParentTab != null) {
949 // JavaScript can only close popup window.
950 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700951 mWebViewController.switchToTab(mWebViewController.getTabControl()
Grace Kloba22ac16e2009-10-07 18:00:23 -0700952 .getTabIndex(mParentTab));
953 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700954 mWebViewController.closeTab(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700955 }
956 }
957
958 @Override
959 public void onProgressChanged(WebView view, int newProgress) {
John Reck30c714c2010-12-16 17:30:34 -0800960 mPageLoadProgress = newProgress;
961 mWebViewController.onProgressChanged(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700962 }
963
964 @Override
Leon Scroggins21d9b902010-03-11 09:33:11 -0500965 public void onReceivedTitle(WebView view, final String title) {
John Reck30c714c2010-12-16 17:30:34 -0800966 mCurrentState.mTitle = title;
Michael Kolb8233fac2010-10-26 16:08:53 -0700967 mWebViewController.onReceivedTitle(Tab.this, title);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700968 }
969
970 @Override
971 public void onReceivedIcon(WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -0800972 mCurrentState.mFavicon = icon;
Michael Kolb8233fac2010-10-26 16:08:53 -0700973 mWebViewController.onFavicon(Tab.this, view, icon);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700974 }
975
976 @Override
977 public void onReceivedTouchIconUrl(WebView view, String url,
978 boolean precomposed) {
979 final ContentResolver cr = mActivity.getContentResolver();
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400980 // Let precomposed icons take precedence over non-composed
981 // icons.
982 if (precomposed && mTouchIconLoader != null) {
983 mTouchIconLoader.cancel(false);
984 mTouchIconLoader = null;
985 }
986 // Have only one async task at a time.
987 if (mTouchIconLoader == null) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700988 mTouchIconLoader = new DownloadTouchIcon(Tab.this,
989 mActivity, cr, view);
Leon Scrogginsc8393d92010-04-23 14:58:16 -0400990 mTouchIconLoader.execute(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700991 }
992 }
993
994 @Override
995 public void onShowCustomView(View view,
996 WebChromeClient.CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700997 if (mInForeground) mWebViewController.showCustomView(Tab.this, view,
998 callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700999 }
1000
1001 @Override
1002 public void onHideCustomView() {
Michael Kolb8233fac2010-10-26 16:08:53 -07001003 if (mInForeground) mWebViewController.hideCustomView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001004 }
1005
1006 /**
1007 * The origin has exceeded its database quota.
1008 * @param url the URL that exceeded the quota
1009 * @param databaseIdentifier the identifier of the database on which the
1010 * transaction that caused the quota overflow was run
1011 * @param currentQuota the current quota for the origin.
1012 * @param estimatedSize the estimated size of the database.
1013 * @param totalUsedQuota is the sum of all origins' quota.
1014 * @param quotaUpdater The callback to run when a decision to allow or
1015 * deny quota has been made. Don't forget to call this!
1016 */
1017 @Override
1018 public void onExceededDatabaseQuota(String url,
1019 String databaseIdentifier, long currentQuota, long estimatedSize,
1020 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -07001021 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001022 .onExceededDatabaseQuota(url, databaseIdentifier,
1023 currentQuota, estimatedSize, totalUsedQuota,
1024 quotaUpdater);
1025 }
1026
1027 /**
1028 * The Application Cache has exceeded its max size.
1029 * @param spaceNeeded is the amount of disk space that would be needed
1030 * in order for the last appcache operation to succeed.
1031 * @param totalUsedQuota is the sum of all origins' quota.
1032 * @param quotaUpdater A callback to inform the WebCore thread that a
1033 * new app cache size is available. This callback must always
1034 * be executed at some point to ensure that the sleeping
1035 * WebCore thread is woken up.
1036 */
1037 @Override
1038 public void onReachedMaxAppCacheSize(long spaceNeeded,
1039 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -07001040 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001041 .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota,
1042 quotaUpdater);
1043 }
1044
1045 /**
1046 * Instructs the browser to show a prompt to ask the user to set the
1047 * Geolocation permission state for the specified origin.
1048 * @param origin The origin for which Geolocation permissions are
1049 * requested.
1050 * @param callback The callback to call once the user has set the
1051 * Geolocation permission state.
1052 */
1053 @Override
1054 public void onGeolocationPermissionsShowPrompt(String origin,
1055 GeolocationPermissions.Callback callback) {
1056 if (mInForeground) {
Grace Kloba50c241e2010-04-20 11:07:50 -07001057 getGeolocationPermissionsPrompt().show(origin, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001058 }
1059 }
1060
1061 /**
1062 * Instructs the browser to hide the Geolocation permissions prompt.
1063 */
1064 @Override
1065 public void onGeolocationPermissionsHidePrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001066 if (mInForeground && mGeolocationPermissionsPrompt != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001067 mGeolocationPermissionsPrompt.hide();
1068 }
1069 }
1070
Ben Murdoch65acc352009-11-19 18:16:04 +00001071 /* Adds a JavaScript error message to the system log and if the JS
1072 * console is enabled in the about:debug options, to that console
1073 * also.
Ben Murdochc42addf2010-01-28 15:19:59 +00001074 * @param consoleMessage the message object.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001075 */
1076 @Override
Ben Murdochc42addf2010-01-28 15:19:59 +00001077 public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001078 if (mInForeground) {
1079 // call getErrorConsole(true) so it will create one if needed
1080 ErrorConsoleView errorConsole = getErrorConsole(true);
Ben Murdochc42addf2010-01-28 15:19:59 +00001081 errorConsole.addErrorMessage(consoleMessage);
Michael Kolb8233fac2010-10-26 16:08:53 -07001082 if (mWebViewController.shouldShowErrorConsole()
1083 && errorConsole.getShowState() !=
1084 ErrorConsoleView.SHOW_MAXIMIZED) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001085 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
1086 }
1087 }
Ben Murdochc42addf2010-01-28 15:19:59 +00001088
Jeff Hamilton47654f42010-09-07 09:57:51 -05001089 // Don't log console messages in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001090 if (isPrivateBrowsingEnabled()) return true;
Jeff Hamilton47654f42010-09-07 09:57:51 -05001091
Ben Murdochc42addf2010-01-28 15:19:59 +00001092 String message = "Console: " + consoleMessage.message() + " "
1093 + consoleMessage.sourceId() + ":"
1094 + consoleMessage.lineNumber();
1095
1096 switch (consoleMessage.messageLevel()) {
1097 case TIP:
1098 Log.v(CONSOLE_LOGTAG, message);
1099 break;
1100 case LOG:
1101 Log.i(CONSOLE_LOGTAG, message);
1102 break;
1103 case WARNING:
1104 Log.w(CONSOLE_LOGTAG, message);
1105 break;
1106 case ERROR:
1107 Log.e(CONSOLE_LOGTAG, message);
1108 break;
1109 case DEBUG:
1110 Log.d(CONSOLE_LOGTAG, message);
1111 break;
1112 }
1113
1114 return true;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001115 }
1116
1117 /**
1118 * Ask the browser for an icon to represent a <video> element.
1119 * This icon will be used if the Web page did not specify a poster attribute.
1120 * @return Bitmap The icon or null if no such icon is available.
1121 */
1122 @Override
1123 public Bitmap getDefaultVideoPoster() {
1124 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001125 return mWebViewController.getDefaultVideoPoster();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001126 }
1127 return null;
1128 }
1129
1130 /**
1131 * Ask the host application for a custom progress view to show while
1132 * a <video> is loading.
1133 * @return View The progress view.
1134 */
1135 @Override
1136 public View getVideoLoadingProgressView() {
1137 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001138 return mWebViewController.getVideoLoadingProgressView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001139 }
1140 return null;
1141 }
1142
1143 @Override
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01001144 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001145 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001146 mWebViewController.openFileChooser(uploadMsg, acceptType);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001147 } else {
1148 uploadMsg.onReceiveValue(null);
1149 }
1150 }
1151
1152 /**
1153 * Deliver a list of already-visited URLs
1154 */
1155 @Override
1156 public void getVisitedHistory(final ValueCallback<String[]> callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001157 mWebViewController.getVisitedHistory(callback);
1158 }
Ben Murdoch8029a772010-11-16 11:58:21 +00001159
1160 @Override
1161 public void setupAutoFill(Message message) {
1162 // Prompt the user to set up their profile.
1163 final Message msg = message;
1164 AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
Ben Murdoch1d676b62011-01-17 12:54:24 +00001165 LayoutInflater inflater = (LayoutInflater) mActivity.getSystemService(
1166 Context.LAYOUT_INFLATER_SERVICE);
1167 final View layout = inflater.inflate(R.layout.setup_autofill_dialog, null);
1168
1169 builder.setView(layout)
1170 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1171 @Override
1172 public void onClick(DialogInterface dialog, int id) {
1173 CheckBox disableAutoFill = (CheckBox) layout.findViewById(
1174 R.id.setup_autofill_dialog_disable_autofill);
1175
1176 if (disableAutoFill.isChecked()) {
1177 // Disable autofill and show a toast with how to turn it on again.
John Reck35e9dd62011-04-25 09:01:54 -07001178 mSettings.setAutofillEnabled(false);
Ben Murdoch1d676b62011-01-17 12:54:24 +00001179 Toast.makeText(mActivity,
1180 R.string.autofill_setup_dialog_negative_toast,
1181 Toast.LENGTH_LONG).show();
1182 } else {
1183 // Take user to the AutoFill profile editor. When they return,
1184 // we will send the message that we pass here which will trigger
1185 // the form to get filled out with their new profile.
1186 mWebViewController.setupAutoFill(msg);
1187 }
1188 }
1189 })
1190 .setNegativeButton(R.string.cancel, null)
1191 .show();
Ben Murdoch8029a772010-11-16 11:58:21 +00001192 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001193 };
1194
1195 // -------------------------------------------------------------------------
1196 // WebViewClient implementation for the sub window
1197 // -------------------------------------------------------------------------
1198
1199 // Subclass of WebViewClient used in subwindows to notify the main
1200 // WebViewClient of certain WebView activities.
1201 private static class SubWindowClient extends WebViewClient {
1202 // The main WebViewClient.
1203 private final WebViewClient mClient;
Michael Kolb8233fac2010-10-26 16:08:53 -07001204 private final WebViewController mController;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001205
Michael Kolb8233fac2010-10-26 16:08:53 -07001206 SubWindowClient(WebViewClient client, WebViewController controller) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001207 mClient = client;
Michael Kolb8233fac2010-10-26 16:08:53 -07001208 mController = controller;
Leon Scroggins III211ba542010-04-19 13:21:13 -04001209 }
1210 @Override
1211 public void onPageStarted(WebView view, String url, Bitmap favicon) {
1212 // Unlike the others, do not call mClient's version, which would
1213 // change the progress bar. However, we do want to remove the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001214 // find or select dialog.
Michael Kolb8233fac2010-10-26 16:08:53 -07001215 mController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001216 }
1217 @Override
1218 public void doUpdateVisitedHistory(WebView view, String url,
1219 boolean isReload) {
1220 mClient.doUpdateVisitedHistory(view, url, isReload);
1221 }
1222 @Override
1223 public boolean shouldOverrideUrlLoading(WebView view, String url) {
1224 return mClient.shouldOverrideUrlLoading(view, url);
1225 }
1226 @Override
1227 public void onReceivedSslError(WebView view, SslErrorHandler handler,
1228 SslError error) {
1229 mClient.onReceivedSslError(view, handler, error);
1230 }
1231 @Override
1232 public void onReceivedHttpAuthRequest(WebView view,
1233 HttpAuthHandler handler, String host, String realm) {
1234 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
1235 }
1236 @Override
1237 public void onFormResubmission(WebView view, Message dontResend,
1238 Message resend) {
1239 mClient.onFormResubmission(view, dontResend, resend);
1240 }
1241 @Override
1242 public void onReceivedError(WebView view, int errorCode,
1243 String description, String failingUrl) {
1244 mClient.onReceivedError(view, errorCode, description, failingUrl);
1245 }
1246 @Override
1247 public boolean shouldOverrideKeyEvent(WebView view,
1248 android.view.KeyEvent event) {
1249 return mClient.shouldOverrideKeyEvent(view, event);
1250 }
1251 @Override
1252 public void onUnhandledKeyEvent(WebView view,
1253 android.view.KeyEvent event) {
1254 mClient.onUnhandledKeyEvent(view, event);
1255 }
1256 }
1257
1258 // -------------------------------------------------------------------------
1259 // WebChromeClient implementation for the sub window
1260 // -------------------------------------------------------------------------
1261
1262 private class SubWindowChromeClient extends WebChromeClient {
1263 // The main WebChromeClient.
1264 private final WebChromeClient mClient;
1265
1266 SubWindowChromeClient(WebChromeClient client) {
1267 mClient = client;
1268 }
1269 @Override
1270 public void onProgressChanged(WebView view, int newProgress) {
1271 mClient.onProgressChanged(view, newProgress);
1272 }
1273 @Override
1274 public boolean onCreateWindow(WebView view, boolean dialog,
1275 boolean userGesture, android.os.Message resultMsg) {
1276 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
1277 }
1278 @Override
1279 public void onCloseWindow(WebView window) {
1280 if (window != mSubView) {
1281 Log.e(LOGTAG, "Can't close the window");
1282 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001283 mWebViewController.dismissSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001284 }
1285 }
1286
1287 // -------------------------------------------------------------------------
1288
Michael Kolb8233fac2010-10-26 16:08:53 -07001289 // TODO temporarily use activity here
1290 // remove later
1291
Grace Kloba22ac16e2009-10-07 18:00:23 -07001292 // Construct a new tab
Michael Kolb8233fac2010-10-26 16:08:53 -07001293 Tab(WebViewController wvcontroller, WebView w, boolean closeOnExit, String appId,
Grace Kloba22ac16e2009-10-07 18:00:23 -07001294 String url) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001295 mWebViewController = wvcontroller;
1296 mActivity = mWebViewController.getActivity();
John Reck35e9dd62011-04-25 09:01:54 -07001297 mSettings = BrowserSettings.getInstance();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001298 mCloseOnExit = closeOnExit;
1299 mAppId = appId;
John Recke969cc52010-12-21 17:24:43 -08001300 mDataController = DataController.getInstance(mActivity);
John Reck847b5322011-04-14 17:02:18 -07001301 mCurrentState = new PageState(mActivity, w != null
1302 ? w.isPrivateBrowsingEnabled() : false);
Michael Kolb8233fac2010-10-26 16:08:53 -07001303 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001304 mInForeground = false;
1305
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001306 mDownloadListener = new DownloadListener() {
1307 public void onDownloadStart(String url, String userAgent,
1308 String contentDisposition, String mimetype,
1309 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001310 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001311 mimetype, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001312 }
1313 };
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001314 mWebBackForwardListClient = new WebBackForwardListClient() {
1315 @Override
1316 public void onNewHistoryItem(WebHistoryItem item) {
1317 if (isInVoiceSearchMode()) {
1318 item.setCustomData(mVoiceSearchData.mVoiceSearchIntent);
1319 }
1320 }
1321 @Override
1322 public void onIndexChanged(WebHistoryItem item, int index) {
1323 Object data = item.getCustomData();
1324 if (data != null && data instanceof Intent) {
1325 activateVoiceSearchMode((Intent) data);
1326 }
1327 }
1328 };
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001329
Grace Kloba22ac16e2009-10-07 18:00:23 -07001330 setWebView(w);
1331 }
1332
1333 /**
1334 * Sets the WebView for this tab, correctly removing the old WebView from
1335 * the container view.
1336 */
1337 void setWebView(WebView w) {
1338 if (mMainView == w) {
1339 return;
1340 }
Michael Kolba713ec82010-11-29 17:27:06 -08001341
Grace Kloba22ac16e2009-10-07 18:00:23 -07001342 // If the WebView is changing, the page will be reloaded, so any ongoing
1343 // Geolocation permission requests are void.
Grace Kloba50c241e2010-04-20 11:07:50 -07001344 if (mGeolocationPermissionsPrompt != null) {
1345 mGeolocationPermissionsPrompt.hide();
1346 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001347
Michael Kolba713ec82010-11-29 17:27:06 -08001348 mWebViewController.onSetWebView(this, w);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001349
1350 // set the new one
1351 mMainView = w;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001352 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001353 if (mMainView != null) {
1354 mMainView.setWebViewClient(mWebViewClient);
1355 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001356 // Attach DownloadManager so that downloads can start in an active
1357 // or a non-active window. This can happen when going to a site that
1358 // does a redirect after a period of time. The user could have
1359 // switched to another tab while waiting for the download to start.
1360 mMainView.setDownloadListener(mDownloadListener);
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001361 mMainView.setWebBackForwardListClient(mWebBackForwardListClient);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001362 }
1363 }
1364
1365 /**
1366 * Destroy the tab's main WebView and subWindow if any
1367 */
1368 void destroy() {
1369 if (mMainView != null) {
1370 dismissSubWindow();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001371 // save the WebView to call destroy() after detach it from the tab
1372 WebView webView = mMainView;
1373 setWebView(null);
1374 webView.destroy();
1375 }
1376 }
1377
1378 /**
1379 * Remove the tab from the parent
1380 */
1381 void removeFromTree() {
1382 // detach the children
1383 if (mChildTabs != null) {
1384 for(Tab t : mChildTabs) {
1385 t.setParentTab(null);
1386 }
1387 }
1388 // remove itself from the parent list
1389 if (mParentTab != null) {
1390 mParentTab.mChildTabs.remove(this);
1391 }
1392 }
1393
1394 /**
1395 * Create a new subwindow unless a subwindow already exists.
1396 * @return True if a new subwindow was created. False if one already exists.
1397 */
1398 boolean createSubWindow() {
1399 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001400 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001401 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001402 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001403 mSubView.setWebChromeClient(new SubWindowChromeClient(
1404 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001405 // Set a different DownloadListener for the mSubView, since it will
1406 // just need to dismiss the mSubView, rather than close the Tab
1407 mSubView.setDownloadListener(new DownloadListener() {
1408 public void onDownloadStart(String url, String userAgent,
1409 String contentDisposition, String mimetype,
1410 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001411 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001412 contentDisposition, mimetype, contentLength);
1413 if (mSubView.copyBackForwardList().getSize() == 0) {
1414 // This subwindow was opened for the sole purpose of
1415 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001416 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001417 }
1418 }
1419 });
Grace Kloba22ac16e2009-10-07 18:00:23 -07001420 mSubView.setOnCreateContextMenuListener(mActivity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001421 return true;
1422 }
1423 return false;
1424 }
1425
1426 /**
1427 * Dismiss the subWindow for the tab.
1428 */
1429 void dismissSubWindow() {
1430 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001431 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001432 mSubView.destroy();
1433 mSubView = null;
1434 mSubViewContainer = null;
1435 }
1436 }
1437
Grace Kloba22ac16e2009-10-07 18:00:23 -07001438
1439 /**
1440 * Set the parent tab of this tab.
1441 */
1442 void setParentTab(Tab parent) {
1443 mParentTab = parent;
1444 // This tab may have been freed due to low memory. If that is the case,
1445 // the parent tab index is already saved. If we are changing that index
1446 // (most likely due to removing the parent tab) we must update the
1447 // parent tab index in the saved Bundle.
1448 if (mSavedState != null) {
1449 if (parent == null) {
1450 mSavedState.remove(PARENTTAB);
1451 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -07001452 mSavedState.putInt(PARENTTAB, mWebViewController.getTabControl()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001453 .getTabIndex(parent));
1454 }
1455 }
1456 }
1457
1458 /**
1459 * When a Tab is created through the content of another Tab, then we
1460 * associate the Tabs.
1461 * @param child the Tab that was created from this Tab
1462 */
1463 void addChildTab(Tab child) {
1464 if (mChildTabs == null) {
1465 mChildTabs = new Vector<Tab>();
1466 }
1467 mChildTabs.add(child);
1468 child.setParentTab(this);
1469 }
1470
1471 Vector<Tab> getChildTabs() {
1472 return mChildTabs;
1473 }
1474
1475 void resume() {
1476 if (mMainView != null) {
1477 mMainView.onResume();
1478 if (mSubView != null) {
1479 mSubView.onResume();
1480 }
1481 }
1482 }
1483
1484 void pause() {
1485 if (mMainView != null) {
1486 mMainView.onPause();
1487 if (mSubView != null) {
1488 mSubView.onPause();
1489 }
1490 }
1491 }
1492
1493 void putInForeground() {
1494 mInForeground = true;
1495 resume();
1496 mMainView.setOnCreateContextMenuListener(mActivity);
1497 if (mSubView != null) {
1498 mSubView.setOnCreateContextMenuListener(mActivity);
1499 }
1500 // Show the pending error dialog if the queue is not empty
1501 if (mQueuedErrors != null && mQueuedErrors.size() > 0) {
1502 showError(mQueuedErrors.getFirst());
1503 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001504 mWebViewController.bookmarkedStatusHasChanged(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001505 }
1506
1507 void putInBackground() {
1508 mInForeground = false;
1509 pause();
1510 mMainView.setOnCreateContextMenuListener(null);
1511 if (mSubView != null) {
1512 mSubView.setOnCreateContextMenuListener(null);
1513 }
1514 }
1515
Michael Kolb8233fac2010-10-26 16:08:53 -07001516 boolean inForeground() {
1517 return mInForeground;
1518 }
1519
Grace Kloba22ac16e2009-10-07 18:00:23 -07001520 /**
1521 * Return the top window of this tab; either the subwindow if it is not
1522 * null or the main window.
1523 * @return The top window of this tab.
1524 */
1525 WebView getTopWindow() {
1526 if (mSubView != null) {
1527 return mSubView;
1528 }
1529 return mMainView;
1530 }
1531
1532 /**
1533 * Return the main window of this tab. Note: if a tab is freed in the
1534 * background, this can return null. It is only guaranteed to be
1535 * non-null for the current tab.
1536 * @return The main WebView of this tab.
1537 */
1538 WebView getWebView() {
1539 return mMainView;
1540 }
1541
Michael Kolba713ec82010-11-29 17:27:06 -08001542 void setViewContainer(View container) {
1543 mContainer = container;
1544 }
1545
Michael Kolb8233fac2010-10-26 16:08:53 -07001546 View getViewContainer() {
1547 return mContainer;
1548 }
1549
Grace Kloba22ac16e2009-10-07 18:00:23 -07001550 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001551 * Return whether private browsing is enabled for the main window of
1552 * this tab.
1553 * @return True if private browsing is enabled.
1554 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001555 boolean isPrivateBrowsingEnabled() {
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001556 WebView webView = getWebView();
1557 if (webView == null) {
1558 return false;
1559 }
1560 return webView.isPrivateBrowsingEnabled();
1561 }
1562
1563 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001564 * Return the subwindow of this tab or null if there is no subwindow.
1565 * @return The subwindow of this tab or null.
1566 */
1567 WebView getSubWebView() {
1568 return mSubView;
1569 }
1570
Michael Kolb1514bb72010-11-22 09:11:48 -08001571 void setSubWebView(WebView subView) {
1572 mSubView = subView;
1573 }
1574
Michael Kolb8233fac2010-10-26 16:08:53 -07001575 View getSubViewContainer() {
1576 return mSubViewContainer;
1577 }
1578
Michael Kolb1514bb72010-11-22 09:11:48 -08001579 void setSubViewContainer(View subViewContainer) {
1580 mSubViewContainer = subViewContainer;
1581 }
1582
Grace Kloba22ac16e2009-10-07 18:00:23 -07001583 /**
1584 * @return The geolocation permissions prompt for this tab.
1585 */
1586 GeolocationPermissionsPrompt getGeolocationPermissionsPrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001587 if (mGeolocationPermissionsPrompt == null) {
1588 ViewStub stub = (ViewStub) mContainer
1589 .findViewById(R.id.geolocation_permissions_prompt);
1590 mGeolocationPermissionsPrompt = (GeolocationPermissionsPrompt) stub
1591 .inflate();
1592 mGeolocationPermissionsPrompt.init();
1593 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001594 return mGeolocationPermissionsPrompt;
1595 }
1596
1597 /**
1598 * @return The application id string
1599 */
1600 String getAppId() {
1601 return mAppId;
1602 }
1603
1604 /**
1605 * Set the application id string
1606 * @param id
1607 */
1608 void setAppId(String id) {
1609 mAppId = id;
1610 }
1611
Grace Kloba22ac16e2009-10-07 18:00:23 -07001612 String getUrl() {
John Reck324d4402011-01-11 16:56:42 -08001613 return UrlUtils.filteredUrl(mCurrentState.mUrl);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001614 }
1615
John Reck49a603c2011-03-03 09:33:05 -08001616 String getOriginalUrl() {
1617 if (mMainView == null) {
1618 return "";
1619 }
1620 return UrlUtils.filteredUrl(mMainView.getOriginalUrl());
1621 }
1622
Grace Kloba22ac16e2009-10-07 18:00:23 -07001623 /**
John Reck30c714c2010-12-16 17:30:34 -08001624 * Get the title of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001625 */
1626 String getTitle() {
John Reck30c714c2010-12-16 17:30:34 -08001627 if (mCurrentState.mTitle == null && mInPageLoad) {
1628 return mActivity.getString(R.string.title_bar_loading);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001629 }
John Reck30c714c2010-12-16 17:30:34 -08001630 return mCurrentState.mTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001631 }
1632
1633 /**
John Reck30c714c2010-12-16 17:30:34 -08001634 * Get the favicon of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001635 */
1636 Bitmap getFavicon() {
John Reck30c714c2010-12-16 17:30:34 -08001637 return mCurrentState.mFavicon;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001638 }
1639
John Recke969cc52010-12-21 17:24:43 -08001640 public boolean isBookmarkedSite() {
1641 return mCurrentState.mIsBookmarkedSite;
1642 }
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001643
Grace Kloba22ac16e2009-10-07 18:00:23 -07001644 /**
1645 * Return the tab's error console. Creates the console if createIfNEcessary
1646 * is true and we haven't already created the console.
1647 * @param createIfNecessary Flag to indicate if the console should be
1648 * created if it has not been already.
1649 * @return The tab's error console, or null if one has not been created and
1650 * createIfNecessary is false.
1651 */
1652 ErrorConsoleView getErrorConsole(boolean createIfNecessary) {
1653 if (createIfNecessary && mErrorConsole == null) {
1654 mErrorConsole = new ErrorConsoleView(mActivity);
1655 mErrorConsole.setWebView(mMainView);
1656 }
1657 return mErrorConsole;
1658 }
1659
1660 /**
1661 * If this Tab was created through another Tab, then this method returns
1662 * that Tab.
1663 * @return the Tab parent or null
1664 */
1665 public Tab getParentTab() {
1666 return mParentTab;
1667 }
1668
1669 /**
1670 * Return whether this tab should be closed when it is backing out of the
1671 * first page.
1672 * @return TRUE if this tab should be closed when exit.
1673 */
1674 boolean closeOnExit() {
1675 return mCloseOnExit;
1676 }
1677
John Reck30c714c2010-12-16 17:30:34 -08001678 private void setLockIconType(LockIcon icon) {
1679 mCurrentState.mLockIcon = icon;
1680 mWebViewController.onUpdatedLockIcon(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001681 }
1682
1683 /**
1684 * @return The tab's lock icon type.
1685 */
John Reck30c714c2010-12-16 17:30:34 -08001686 LockIcon getLockIconType() {
1687 return mCurrentState.mLockIcon;
1688 }
1689
1690 int getLoadProgress() {
1691 if (mInPageLoad) {
1692 return mPageLoadProgress;
1693 }
1694 return 100;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001695 }
1696
1697 /**
1698 * @return TRUE if onPageStarted is called while onPageFinished is not
1699 * called yet.
1700 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001701 boolean inPageLoad() {
1702 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001703 }
1704
1705 // force mInLoad to be false. This should only be called before closing the
1706 // tab to ensure BrowserActivity's pauseWebViewTimers() is called correctly.
Michael Kolb8233fac2010-10-26 16:08:53 -07001707 void clearInPageLoad() {
1708 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001709 }
1710
Grace Kloba22ac16e2009-10-07 18:00:23 -07001711 /**
John Reck30c714c2010-12-16 17:30:34 -08001712 * Get the cached saved state bundle.
1713 * @return cached state bundle
Grace Kloba22ac16e2009-10-07 18:00:23 -07001714 */
1715 Bundle getSavedState() {
1716 return mSavedState;
1717 }
1718
1719 /**
1720 * Set the saved state.
1721 */
1722 void setSavedState(Bundle state) {
1723 mSavedState = state;
1724 }
1725
1726 /**
1727 * @return TRUE if succeed in saving the state.
1728 */
1729 boolean saveState() {
1730 // If the WebView is null it means we ran low on memory and we already
1731 // stored the saved state in mSavedState.
1732 if (mMainView == null) {
1733 return mSavedState != null;
1734 }
1735
1736 mSavedState = new Bundle();
1737 final WebBackForwardList list = mMainView.saveState(mSavedState);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001738
1739 // Store some extra info for displaying the tab in the picker.
1740 final WebHistoryItem item = list != null ? list.getCurrentItem() : null;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001741
John Reck30c714c2010-12-16 17:30:34 -08001742 mSavedState.putString(CURRURL, mCurrentState.mUrl);
1743 mSavedState.putString(CURRTITLE, mCurrentState.mTitle);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001744 mSavedState.putBoolean(CLOSEONEXIT, mCloseOnExit);
1745 if (mAppId != null) {
1746 mSavedState.putString(APPID, mAppId);
1747 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001748 // Remember the parent tab so the relationship can be restored.
1749 if (mParentTab != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001750 mSavedState.putInt(PARENTTAB, mWebViewController.getTabControl().getTabIndex(
Grace Kloba22ac16e2009-10-07 18:00:23 -07001751 mParentTab));
1752 }
Michael Kolbeb95db42011-03-03 10:38:40 -08001753 if (mScreenshot != null) {
1754 mSavedState.putParcelable(SCREENSHOT, mScreenshot);
1755 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001756 return true;
1757 }
1758
1759 /*
1760 * Restore the state of the tab.
1761 */
1762 boolean restoreState(Bundle b) {
1763 if (b == null) {
1764 return false;
1765 }
1766 // Restore the internal state even if the WebView fails to restore.
1767 // This will maintain the app id, original url and close-on-exit values.
1768 mSavedState = null;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001769 mCloseOnExit = b.getBoolean(CLOSEONEXIT);
1770 mAppId = b.getString(APPID);
Michael Kolbeb95db42011-03-03 10:38:40 -08001771 mScreenshot = b.getParcelable(SCREENSHOT);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001772
1773 final WebBackForwardList list = mMainView.restoreState(b);
1774 if (list == null) {
1775 return false;
1776 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001777 return true;
1778 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04001779
Leon Scroggins1961ed22010-12-07 15:22:21 -05001780 public void updateBookmarkedStatus() {
John Recke969cc52010-12-21 17:24:43 -08001781 mDataController.queryBookmarkStatus(getUrl(), mIsBookmarkCallback);
Leon Scroggins1961ed22010-12-07 15:22:21 -05001782 }
1783
John Recke969cc52010-12-21 17:24:43 -08001784 private DataController.OnQueryUrlIsBookmark mIsBookmarkCallback
1785 = new DataController.OnQueryUrlIsBookmark() {
1786 @Override
1787 public void onQueryUrlIsBookmark(String url, boolean isBookmark) {
1788 if (mCurrentState.mUrl.equals(url)) {
1789 mCurrentState.mIsBookmarkedSite = isBookmark;
1790 mWebViewController.bookmarkedStatusHasChanged(Tab.this);
1791 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001792 }
John Recke969cc52010-12-21 17:24:43 -08001793 };
Michael Kolb1acef692011-03-08 14:12:06 -08001794
Michael Kolbeb95db42011-03-03 10:38:40 -08001795 public void setScreenshot(Bitmap screenshot) {
1796 mScreenshot = screenshot;
1797 }
1798
1799 public Bitmap getScreenshot() {
1800 return mScreenshot;
1801 }
1802
Grace Kloba22ac16e2009-10-07 18:00:23 -07001803}