blob: d7ce29b3ef659d48e191eb9b1015abc99dfa8562 [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 Reck1cf4b792011-07-26 10:22:22 -070023import android.content.ContentUris;
John Reckd8c74522011-06-14 08:45:00 -070024import 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;
John Reck1cf4b792011-07-26 10:22:22 -070029import android.database.Cursor;
Grace Kloba22ac16e2009-10-07 18:00:23 -070030import android.graphics.Bitmap;
John Reck8cc92352011-07-06 17:41:52 -070031import android.graphics.Bitmap.CompressFormat;
Michael Kolb9ef259a2011-07-12 15:33:08 -070032import android.graphics.BitmapFactory;
33import android.graphics.Canvas;
Michael Kolbc3af0672011-08-09 10:24:41 -070034import android.graphics.Color;
Michael Kolb9ef259a2011-07-12 15:33:08 -070035import android.graphics.Picture;
Grace Kloba22ac16e2009-10-07 18:00:23 -070036import android.net.Uri;
37import android.net.http.SslError;
Grace Kloba22ac16e2009-10-07 18:00:23 -070038import android.os.Bundle;
Michael Kolb9ef259a2011-07-12 15:33:08 -070039import android.os.Handler;
Grace Kloba22ac16e2009-10-07 18:00:23 -070040import android.os.Message;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +000041import android.os.SystemClock;
Brian Carlstrom8862c1d2011-06-02 01:05:55 -070042import android.security.KeyChain;
Brian Carlstromaa09cd82011-06-09 16:04:40 -070043import android.security.KeyChainAliasCallback;
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -050044import android.speech.RecognizerResultsIntent;
John Reck24f18262011-06-17 14:47:20 -070045import android.text.TextUtils;
Grace Kloba22ac16e2009-10-07 18:00:23 -070046import android.util.Log;
47import android.view.KeyEvent;
48import android.view.LayoutInflater;
49import android.view.View;
Grace Kloba50c241e2010-04-20 11:07:50 -070050import android.view.ViewStub;
Brian Carlstrom8862c1d2011-06-02 01:05:55 -070051import android.webkit.ClientCertRequestHandler;
Ben Murdochc42addf2010-01-28 15:19:59 +000052import android.webkit.ConsoleMessage;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -050053import android.webkit.DownloadListener;
Grace Kloba22ac16e2009-10-07 18:00:23 -070054import android.webkit.GeolocationPermissions;
55import android.webkit.HttpAuthHandler;
56import android.webkit.SslErrorHandler;
57import android.webkit.URLUtil;
58import android.webkit.ValueCallback;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -050059import android.webkit.WebBackForwardListClient;
Grace Kloba22ac16e2009-10-07 18:00:23 -070060import android.webkit.WebChromeClient;
61import android.webkit.WebHistoryItem;
John Reck438bf462011-01-12 18:11:46 -080062import android.webkit.WebResourceResponse;
Grace Kloba22ac16e2009-10-07 18:00:23 -070063import android.webkit.WebStorage;
64import android.webkit.WebView;
Michael Kolb9ef259a2011-07-12 15:33:08 -070065import android.webkit.WebView.PictureListener;
Grace Kloba22ac16e2009-10-07 18:00:23 -070066import android.webkit.WebViewClient;
Ben Murdoch1d676b62011-01-17 12:54:24 +000067import android.widget.CheckBox;
Grace Kloba22ac16e2009-10-07 18:00:23 -070068import android.widget.LinearLayout;
69import android.widget.TextView;
Ben Murdoch8029a772010-11-16 11:58:21 +000070import android.widget.Toast;
Grace Kloba22ac16e2009-10-07 18:00:23 -070071
John Reck8ee633f2011-08-09 16:00:35 -070072import com.android.browser.TabControl.OnThumbnailUpdatedListener;
John Reck541f55a2011-06-07 16:34:43 -070073import com.android.browser.homepages.HomeProvider;
John Reck1cf4b792011-07-26 10:22:22 -070074import com.android.browser.provider.BrowserProvider2.Thumbnails;
John Reck8cc92352011-07-06 17:41:52 -070075import com.android.browser.provider.SnapshotProvider.Snapshots;
John Reck541f55a2011-06-07 16:34:43 -070076import com.android.common.speech.LoggingEvents;
77
78import java.io.ByteArrayOutputStream;
John Reck1cf4b792011-07-26 10:22:22 -070079import java.nio.ByteBuffer;
Michael Kolbfe251992010-07-08 15:41:55 -070080import java.util.ArrayList;
81import java.util.HashMap;
82import java.util.Iterator;
83import java.util.LinkedList;
84import java.util.Map;
85import java.util.Vector;
John Reck8cc92352011-07-06 17:41:52 -070086import java.util.zip.GZIPOutputStream;
Michael Kolbfe251992010-07-08 15:41:55 -070087
Grace Kloba22ac16e2009-10-07 18:00:23 -070088/**
89 * Class for maintaining Tabs with a main WebView and a subwindow.
90 */
Michael Kolb9ef259a2011-07-12 15:33:08 -070091class Tab implements PictureListener {
Michael Kolb8233fac2010-10-26 16:08:53 -070092
Grace Kloba22ac16e2009-10-07 18:00:23 -070093 // Log Tag
94 private static final String LOGTAG = "Tab";
Mathew Inwood1dd8e822011-08-03 14:34:29 +010095 private static final boolean LOGD_ENABLED = com.android.browser.Browser.LOGD_ENABLED;
Ben Murdochc42addf2010-01-28 15:19:59 +000096 // Special case the logtag for messages for the Console to make it easier to
97 // filter them and match the logtag used for these messages in older versions
98 // of the browser.
99 private static final String CONSOLE_LOGTAG = "browser";
100
Michael Kolb9ef259a2011-07-12 15:33:08 -0700101 private static final int MSG_CAPTURE = 42;
John Reck8ee633f2011-08-09 16:00:35 -0700102 private static final int CAPTURE_DELAY = 100;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700103
John Reck1cf4b792011-07-26 10:22:22 -0700104 private static Bitmap sDefaultFavicon;
105
John Reck30c714c2010-12-16 17:30:34 -0800106 public enum LockIcon {
107 LOCK_ICON_UNSECURE,
108 LOCK_ICON_SECURE,
109 LOCK_ICON_MIXED,
110 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700111
Michael Kolb14612442011-06-24 13:06:29 -0700112 Context mContext;
John Reckd8c74522011-06-14 08:45:00 -0700113 protected WebViewController mWebViewController;
Michael Kolb8233fac2010-10-26 16:08:53 -0700114
Michael Kolbc831b632011-05-11 09:30:34 -0700115 // The tab ID
John Reckd8c74522011-06-14 08:45:00 -0700116 private long mId = -1;
Michael Kolbc831b632011-05-11 09:30:34 -0700117
Grace Kloba22ac16e2009-10-07 18:00:23 -0700118 // The Geolocation permissions prompt
119 private GeolocationPermissionsPrompt mGeolocationPermissionsPrompt;
120 // Main WebView wrapper
Michael Kolba713ec82010-11-29 17:27:06 -0800121 private View mContainer;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700122 // Main WebView
123 private WebView mMainView;
124 // Subwindow container
125 private View mSubViewContainer;
126 // Subwindow WebView
127 private WebView mSubView;
128 // Saved bundle for when we are running low on memory. It contains the
129 // information needed to restore the WebView if the user goes back to the
130 // tab.
131 private Bundle mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700132 // Parent Tab. This is the Tab that created this Tab, or null if the Tab was
133 // created by the UI
Michael Kolbc831b632011-05-11 09:30:34 -0700134 private Tab mParent;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700135 // Tab that constructed by this Tab. This is used when this Tab is
136 // destroyed, it clears all mParentTab values in the children.
Michael Kolbc831b632011-05-11 09:30:34 -0700137 private Vector<Tab> mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700138 // If true, the tab is in the foreground of the current activity.
139 private boolean mInForeground;
Michael Kolb8233fac2010-10-26 16:08:53 -0700140 // If true, the tab is in page loading state (after onPageStarted,
141 // before onPageFinsihed)
142 private boolean mInPageLoad;
John Reck30c714c2010-12-16 17:30:34 -0800143 // The last reported progress of the current page
144 private int mPageLoadProgress;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000145 // The time the load started, used to find load page time
146 private long mLoadStartTime;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700147 // Application identifier used to find tabs that another application wants
148 // to reuse.
149 private String mAppId;
Michael Kolbe28b3472011-08-04 16:54:31 -0700150 // flag to indicate if tab should be closed on back
151 private boolean mCloseOnBack;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700152 // Keep the original url around to avoid killing the old WebView if the url
153 // has not changed.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700154 // Error console for the tab
155 private ErrorConsoleView mErrorConsole;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -0500156 // The listener that gets invoked when a download is started from the
157 // mMainView
158 private final DownloadListener mDownloadListener;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500159 // Listener used to know when we move forward or back in the history list.
160 private final WebBackForwardListClient mWebBackForwardListClient;
John Recke969cc52010-12-21 17:24:43 -0800161 private DataController mDataController;
Patrick Scott92066772011-03-10 08:46:27 -0500162 // State of the auto-login request.
163 private DeviceAccountLogin mDeviceAccountLogin;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700164
165 // AsyncTask for downloading touch icons
166 DownloadTouchIcon mTouchIconLoader;
167
John Reck35e9dd62011-04-25 09:01:54 -0700168 private BrowserSettings mSettings;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700169 private int mCaptureWidth;
170 private int mCaptureHeight;
171 private Bitmap mCapture;
172 private Handler mHandler;
173
Mathew Inwood1dd8e822011-08-03 14:34:29 +0100174 /**
175 * See {@link #clearBackStackWhenItemAdded(String)}.
176 */
177 private String mClearHistoryMatchUrl;
178
John Reck1cf4b792011-07-26 10:22:22 -0700179 private static synchronized Bitmap getDefaultFavicon(Context context) {
180 if (sDefaultFavicon == null) {
181 sDefaultFavicon = BitmapFactory.decodeResource(
182 context.getResources(), R.drawable.app_web_browser_sm);
183 }
184 return sDefaultFavicon;
185 }
Michael Kolbeb95db42011-03-03 10:38:40 -0800186
John Reck30c714c2010-12-16 17:30:34 -0800187 // All the state needed for a page
John Reckd8c74522011-06-14 08:45:00 -0700188 protected static class PageState {
John Reck30c714c2010-12-16 17:30:34 -0800189 String mUrl;
John Reckdb22ec42011-06-29 11:31:24 -0700190 String mOriginalUrl;
John Reck30c714c2010-12-16 17:30:34 -0800191 String mTitle;
192 LockIcon mLockIcon;
193 Bitmap mFavicon;
John Reck502a3532011-08-16 14:21:46 -0700194 boolean mIsBookmarkedSite = false;
195 boolean mIncognito = false;
John Reck30c714c2010-12-16 17:30:34 -0800196
197 PageState(Context c, boolean incognito) {
John Reck502a3532011-08-16 14:21:46 -0700198 mIncognito = incognito;
199 if (mIncognito) {
John Reckdb22ec42011-06-29 11:31:24 -0700200 mOriginalUrl = mUrl = "browser:incognito";
John Reck30c714c2010-12-16 17:30:34 -0800201 mTitle = c.getString(R.string.new_incognito_tab);
John Reck30c714c2010-12-16 17:30:34 -0800202 } else {
John Reckdb22ec42011-06-29 11:31:24 -0700203 mOriginalUrl = mUrl = "";
John Reck30c714c2010-12-16 17:30:34 -0800204 mTitle = c.getString(R.string.new_tab);
John Reck30c714c2010-12-16 17:30:34 -0800205 }
John Reck1cf4b792011-07-26 10:22:22 -0700206 mFavicon = null;
John Reck30c714c2010-12-16 17:30:34 -0800207 mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
208 }
209
210 PageState(Context c, boolean incognito, String url, Bitmap favicon) {
John Reck502a3532011-08-16 14:21:46 -0700211 mIncognito = incognito;
John Reckdb22ec42011-06-29 11:31:24 -0700212 mOriginalUrl = mUrl = url;
John Reck30c714c2010-12-16 17:30:34 -0800213 mTitle = null;
214 if (URLUtil.isHttpsUrl(url)) {
215 mLockIcon = LockIcon.LOCK_ICON_SECURE;
216 } else {
217 mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
218 }
John Reck1cf4b792011-07-26 10:22:22 -0700219 mFavicon = favicon;
John Reck30c714c2010-12-16 17:30:34 -0800220 }
John Reck1cf4b792011-07-26 10:22:22 -0700221
Grace Kloba22ac16e2009-10-07 18:00:23 -0700222 }
223
John Reck30c714c2010-12-16 17:30:34 -0800224 // The current/loading page's state
John Reckd8c74522011-06-14 08:45:00 -0700225 protected PageState mCurrentState;
John Reck30c714c2010-12-16 17:30:34 -0800226
Grace Kloba22ac16e2009-10-07 18:00:23 -0700227 // Used for saving and restoring each Tab
Michael Kolbc831b632011-05-11 09:30:34 -0700228 static final String ID = "ID";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700229 static final String CURRURL = "currentUrl";
230 static final String CURRTITLE = "currentTitle";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700231 static final String PARENTTAB = "parentTab";
232 static final String APPID = "appid";
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700233 static final String INCOGNITO = "privateBrowsingEnabled";
John Reckb0a86db2011-05-24 14:05:58 -0700234 static final String USERAGENT = "useragent";
Michael Kolbe28b3472011-08-04 16:54:31 -0700235 static final String CLOSEFLAG = "closeOnBack";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700236
237 // -------------------------------------------------------------------------
238
Leon Scroggins58d56c62010-01-28 15:12:40 -0500239 /**
240 * Private information regarding the latest voice search. If the Tab is not
241 * in voice search mode, this will be null.
242 */
243 private VoiceSearchData mVoiceSearchData;
244 /**
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400245 * Remove voice search mode from this tab.
246 */
247 public void revertVoiceSearchMode() {
248 if (mVoiceSearchData != null) {
249 mVoiceSearchData = null;
250 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700251 mWebViewController.revertVoiceSearchMode(this);
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400252 }
253 }
254 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700255
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400256 /**
Leon Scroggins58d56c62010-01-28 15:12:40 -0500257 * Return whether the tab is in voice search mode.
258 */
259 public boolean isInVoiceSearchMode() {
260 return mVoiceSearchData != null;
261 }
262 /**
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400263 * Return true if the Tab is in voice search mode and the voice search
264 * Intent came with a String identifying that Google provided the Intent.
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500265 */
266 public boolean voiceSearchSourceIsGoogle() {
267 return mVoiceSearchData != null && mVoiceSearchData.mSourceIsGoogle;
268 }
269 /**
Leon Scroggins58d56c62010-01-28 15:12:40 -0500270 * Get the title to display for the current voice search page. If the Tab
271 * is not in voice search mode, return null.
272 */
273 public String getVoiceDisplayTitle() {
274 if (mVoiceSearchData == null) return null;
275 return mVoiceSearchData.mLastVoiceSearchTitle;
276 }
277 /**
278 * Get the latest array of voice search results, to be passed to the
279 * BrowserProvider. If the Tab is not in voice search mode, return null.
280 */
281 public ArrayList<String> getVoiceSearchResults() {
282 if (mVoiceSearchData == null) return null;
283 return mVoiceSearchData.mVoiceSearchResults;
284 }
285 /**
286 * Activate voice search mode.
287 * @param intent Intent which has the results to use, or an index into the
288 * results when reusing the old results.
289 */
290 /* package */ void activateVoiceSearchMode(Intent intent) {
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500291 int index = 0;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500292 ArrayList<String> results = intent.getStringArrayListExtra(
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -0500293 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_STRINGS);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500294 if (results != null) {
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500295 ArrayList<String> urls = intent.getStringArrayListExtra(
296 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_URLS);
297 ArrayList<String> htmls = intent.getStringArrayListExtra(
298 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_HTML);
299 ArrayList<String> baseUrls = intent.getStringArrayListExtra(
300 RecognizerResultsIntent
301 .EXTRA_VOICE_SEARCH_RESULT_HTML_BASE_URLS);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500302 // This tab is now entering voice search mode for the first time, or
303 // a new voice search was done.
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500304 int size = results.size();
305 if (urls == null || size != urls.size()) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500306 throw new AssertionError("improper extras passed in Intent");
307 }
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500308 if (htmls == null || htmls.size() != size || baseUrls == null ||
309 (baseUrls.size() != size && baseUrls.size() != 1)) {
310 // If either of these arrays are empty/incorrectly sized, ignore
311 // them.
312 htmls = null;
313 baseUrls = null;
314 }
315 mVoiceSearchData = new VoiceSearchData(results, urls, htmls,
316 baseUrls);
Leon Scroggins9df94972010-03-08 18:20:35 -0500317 mVoiceSearchData.mHeaders = intent.getParcelableArrayListExtra(
318 RecognizerResultsIntent
319 .EXTRA_VOICE_SEARCH_RESULT_HTTP_HEADERS);
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500320 mVoiceSearchData.mSourceIsGoogle = intent.getBooleanExtra(
321 VoiceSearchData.SOURCE_IS_GOOGLE, false);
Leon Scroggins2ee4a5a2010-03-15 16:56:57 -0400322 mVoiceSearchData.mVoiceSearchIntent = new Intent(intent);
Leon Scrogginse10dde52010-03-08 19:53:03 -0500323 }
324 String extraData = intent.getStringExtra(
325 SearchManager.EXTRA_DATA_KEY);
326 if (extraData != null) {
327 index = Integer.parseInt(extraData);
328 if (index >= mVoiceSearchData.mVoiceSearchResults.size()) {
329 throw new AssertionError("index must be less than "
330 + "size of mVoiceSearchResults");
331 }
332 if (mVoiceSearchData.mSourceIsGoogle) {
333 Intent logIntent = new Intent(
334 LoggingEvents.ACTION_LOG_EVENT);
335 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
336 LoggingEvents.VoiceSearch.N_BEST_CHOOSE);
337 logIntent.putExtra(
338 LoggingEvents.VoiceSearch.EXTRA_N_BEST_CHOOSE_INDEX,
339 index);
Michael Kolb14612442011-06-24 13:06:29 -0700340 mContext.sendBroadcast(logIntent);
Leon Scrogginse10dde52010-03-08 19:53:03 -0500341 }
342 if (mVoiceSearchData.mVoiceSearchIntent != null) {
Leon Scroggins2ee4a5a2010-03-15 16:56:57 -0400343 // Copy the Intent, so that each history item will have its own
344 // Intent, with different (or none) extra data.
345 Intent latest = new Intent(mVoiceSearchData.mVoiceSearchIntent);
346 latest.putExtra(SearchManager.EXTRA_DATA_KEY, extraData);
347 mVoiceSearchData.mVoiceSearchIntent = latest;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500348 }
349 }
350 mVoiceSearchData.mLastVoiceSearchTitle
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500351 = mVoiceSearchData.mVoiceSearchResults.get(index);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500352 if (mInForeground) {
Michael Kolb11d19782011-03-20 10:17:40 -0700353 mWebViewController.activateVoiceSearchMode(
354 mVoiceSearchData.mLastVoiceSearchTitle,
355 mVoiceSearchData.mVoiceSearchResults);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500356 }
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500357 if (mVoiceSearchData.mVoiceSearchHtmls != null) {
358 // When index was found it was already ensured that it was valid
359 String uriString = mVoiceSearchData.mVoiceSearchHtmls.get(index);
360 if (uriString != null) {
361 Uri dataUri = Uri.parse(uriString);
362 if (RecognizerResultsIntent.URI_SCHEME_INLINE.equals(
363 dataUri.getScheme())) {
364 // If there is only one base URL, use it. If there are
365 // more, there will be one for each index, so use the base
366 // URL corresponding to the index.
367 String baseUrl = mVoiceSearchData.mVoiceSearchBaseUrls.get(
368 mVoiceSearchData.mVoiceSearchBaseUrls.size() > 1 ?
369 index : 0);
370 mVoiceSearchData.mLastVoiceSearchUrl = baseUrl;
371 mMainView.loadDataWithBaseURL(baseUrl,
372 uriString.substring(RecognizerResultsIntent
373 .URI_SCHEME_INLINE.length() + 1), "text/html",
374 "utf-8", baseUrl);
375 return;
376 }
377 }
378 }
Leon Scroggins58d56c62010-01-28 15:12:40 -0500379 mVoiceSearchData.mLastVoiceSearchUrl
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500380 = mVoiceSearchData.mVoiceSearchUrls.get(index);
381 if (null == mVoiceSearchData.mLastVoiceSearchUrl) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700382 mVoiceSearchData.mLastVoiceSearchUrl = UrlUtils.smartUrlFilter(
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500383 mVoiceSearchData.mLastVoiceSearchTitle);
384 }
Leon Scroggins9df94972010-03-08 18:20:35 -0500385 Map<String, String> headers = null;
386 if (mVoiceSearchData.mHeaders != null) {
387 int bundleIndex = mVoiceSearchData.mHeaders.size() == 1 ? 0
388 : index;
389 Bundle bundle = mVoiceSearchData.mHeaders.get(bundleIndex);
390 if (bundle != null && !bundle.isEmpty()) {
391 Iterator<String> iter = bundle.keySet().iterator();
392 headers = new HashMap<String, String>();
393 while (iter.hasNext()) {
394 String key = iter.next();
395 headers.put(key, bundle.getString(key));
396 }
397 }
398 }
399 mMainView.loadUrl(mVoiceSearchData.mLastVoiceSearchUrl, headers);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500400 }
401 /* package */ static class VoiceSearchData {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500402 public VoiceSearchData(ArrayList<String> results,
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500403 ArrayList<String> urls, ArrayList<String> htmls,
404 ArrayList<String> baseUrls) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500405 mVoiceSearchResults = results;
406 mVoiceSearchUrls = urls;
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500407 mVoiceSearchHtmls = htmls;
408 mVoiceSearchBaseUrls = baseUrls;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500409 }
410 /*
411 * ArrayList of suggestions to be displayed when opening the
412 * SearchManager
413 */
414 public ArrayList<String> mVoiceSearchResults;
415 /*
416 * ArrayList of urls, associated with the suggestions in
417 * mVoiceSearchResults.
418 */
419 public ArrayList<String> mVoiceSearchUrls;
420 /*
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500421 * ArrayList holding content to load for each item in
422 * mVoiceSearchResults.
423 */
424 public ArrayList<String> mVoiceSearchHtmls;
425 /*
426 * ArrayList holding base urls for the items in mVoiceSearchResults.
427 * If non null, this will either have the same size as
428 * mVoiceSearchResults or have a size of 1, in which case all will use
429 * the same base url
430 */
431 public ArrayList<String> mVoiceSearchBaseUrls;
432 /*
Leon Scroggins58d56c62010-01-28 15:12:40 -0500433 * The last url provided by voice search. Used for comparison to see if
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500434 * we are going to a page by some method besides voice search.
Leon Scroggins58d56c62010-01-28 15:12:40 -0500435 */
436 public String mLastVoiceSearchUrl;
437 /**
438 * The last title used for voice search. Needed to update the title bar
439 * when switching tabs.
440 */
441 public String mLastVoiceSearchTitle;
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500442 /**
443 * Whether the Intent which turned on voice search mode contained the
444 * String signifying that Google was the source.
445 */
446 public boolean mSourceIsGoogle;
447 /**
Leon Scroggins9df94972010-03-08 18:20:35 -0500448 * List of headers to be passed into the WebView containing location
449 * information
450 */
451 public ArrayList<Bundle> mHeaders;
452 /**
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500453 * The Intent used to invoke voice search. Placed on the
454 * WebHistoryItem so that when coming back to a previous voice search
455 * page we can again activate voice search.
456 */
Leon Scrogginse10dde52010-03-08 19:53:03 -0500457 public Intent mVoiceSearchIntent;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500458 /**
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500459 * String used to identify Google as the source of voice search.
460 */
461 public static String SOURCE_IS_GOOGLE
462 = "android.speech.extras.SOURCE_IS_GOOGLE";
Leon Scroggins58d56c62010-01-28 15:12:40 -0500463 }
464
Grace Kloba22ac16e2009-10-07 18:00:23 -0700465 // Container class for the next error dialog that needs to be displayed
466 private class ErrorDialog {
467 public final int mTitle;
468 public final String mDescription;
469 public final int mError;
470 ErrorDialog(int title, String desc, int error) {
471 mTitle = title;
472 mDescription = desc;
473 mError = error;
474 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700475 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700476
477 private void processNextError() {
478 if (mQueuedErrors == null) {
479 return;
480 }
481 // The first one is currently displayed so just remove it.
482 mQueuedErrors.removeFirst();
483 if (mQueuedErrors.size() == 0) {
484 mQueuedErrors = null;
485 return;
486 }
487 showError(mQueuedErrors.getFirst());
488 }
489
490 private DialogInterface.OnDismissListener mDialogListener =
491 new DialogInterface.OnDismissListener() {
492 public void onDismiss(DialogInterface d) {
493 processNextError();
494 }
495 };
496 private LinkedList<ErrorDialog> mQueuedErrors;
497
498 private void queueError(int err, String desc) {
499 if (mQueuedErrors == null) {
500 mQueuedErrors = new LinkedList<ErrorDialog>();
501 }
502 for (ErrorDialog d : mQueuedErrors) {
503 if (d.mError == err) {
504 // Already saw a similar error, ignore the new one.
505 return;
506 }
507 }
508 ErrorDialog errDialog = new ErrorDialog(
509 err == WebViewClient.ERROR_FILE_NOT_FOUND ?
510 R.string.browserFrameFileErrorLabel :
511 R.string.browserFrameNetworkErrorLabel,
512 desc, err);
513 mQueuedErrors.addLast(errDialog);
514
515 // Show the dialog now if the queue was empty and it is in foreground
516 if (mQueuedErrors.size() == 1 && mInForeground) {
517 showError(errDialog);
518 }
519 }
520
521 private void showError(ErrorDialog errDialog) {
522 if (mInForeground) {
Michael Kolb14612442011-06-24 13:06:29 -0700523 AlertDialog d = new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700524 .setTitle(errDialog.mTitle)
525 .setMessage(errDialog.mDescription)
526 .setPositiveButton(R.string.ok, null)
527 .create();
528 d.setOnDismissListener(mDialogListener);
529 d.show();
530 }
531 }
532
533 // -------------------------------------------------------------------------
534 // WebViewClient implementation for the main WebView
535 // -------------------------------------------------------------------------
536
537 private final WebViewClient mWebViewClient = new WebViewClient() {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500538 private Message mDontResend;
539 private Message mResend;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700540 @Override
541 public void onPageStarted(WebView view, String url, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700542 mInPageLoad = true;
John Reck30c714c2010-12-16 17:30:34 -0800543 mPageLoadProgress = 0;
Michael Kolb14612442011-06-24 13:06:29 -0700544 mCurrentState = new PageState(mContext,
John Reck30c714c2010-12-16 17:30:34 -0800545 view.isPrivateBrowsingEnabled(), url, favicon);
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000546 mLoadStartTime = SystemClock.uptimeMillis();
Leon Scroggins58d56c62010-01-28 15:12:40 -0500547 if (mVoiceSearchData != null
548 && !url.equals(mVoiceSearchData.mLastVoiceSearchUrl)) {
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500549 if (mVoiceSearchData.mSourceIsGoogle) {
550 Intent i = new Intent(LoggingEvents.ACTION_LOG_EVENT);
551 i.putExtra(LoggingEvents.EXTRA_FLUSH, true);
Michael Kolb14612442011-06-24 13:06:29 -0700552 mContext.sendBroadcast(i);
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500553 }
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400554 revertVoiceSearchMode();
Leon Scroggins58d56c62010-01-28 15:12:40 -0500555 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700556
Grace Kloba22ac16e2009-10-07 18:00:23 -0700557
558 // If we start a touch icon load and then load a new page, we don't
559 // want to cancel the current touch icon loader. But, we do want to
560 // create a new one when the touch icon url is known.
561 if (mTouchIconLoader != null) {
562 mTouchIconLoader.mTab = null;
563 mTouchIconLoader = null;
564 }
565
566 // reset the error console
567 if (mErrorConsole != null) {
568 mErrorConsole.clearErrorMessages();
Michael Kolb8233fac2010-10-26 16:08:53 -0700569 if (mWebViewController.shouldShowErrorConsole()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700570 mErrorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
571 }
572 }
573
Patrick Scott92066772011-03-10 08:46:27 -0500574 // Cancel the auto-login process.
575 if (mDeviceAccountLogin != null) {
576 mDeviceAccountLogin.cancel();
577 mDeviceAccountLogin = null;
578 mWebViewController.hideAutoLogin(Tab.this);
579 }
580
Grace Kloba22ac16e2009-10-07 18:00:23 -0700581 // finally update the UI in the activity if it is in the foreground
John Reck324d4402011-01-11 16:56:42 -0800582 mWebViewController.onPageStarted(Tab.this, view, favicon);
Leon Scroggins4cd97792010-12-03 15:31:56 -0500583
John Recke969cc52010-12-21 17:24:43 -0800584 updateBookmarkedStatus();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700585 }
586
587 @Override
588 public void onPageFinished(WebView view, String url) {
John Recka1696282011-07-08 14:10:37 -0700589 if (!mInPageLoad) {
590 // In page navigation links (www.something.com#footer) will
591 // trigger an onPageFinished which we don't care about.
592 return;
593 }
John Reck5b691842010-11-29 11:21:13 -0800594 if (!isPrivateBrowsingEnabled()) {
595 LogTag.logPageFinishedLoading(
596 url, SystemClock.uptimeMillis() - mLoadStartTime);
597 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700598 mInPageLoad = false;
John Reck1cf4b792011-07-26 10:22:22 -0700599 syncCurrentState(view, url);
John Reck324d4402011-01-11 16:56:42 -0800600 mWebViewController.onPageFinished(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700601 }
602
603 // return true if want to hijack the url to let another app to handle it
604 @Override
605 public boolean shouldOverrideUrlLoading(WebView view, String url) {
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400606 if (voiceSearchSourceIsGoogle()) {
607 // This method is called when the user clicks on a link.
608 // VoiceSearchMode is turned off when the user leaves the
609 // Google results page, so at this point the user must be on
610 // that page. If the user clicked a link on that page, assume
611 // that the voice search was effective, and broadcast an Intent
612 // so a receiver can take note of that fact.
613 Intent logIntent = new Intent(LoggingEvents.ACTION_LOG_EVENT);
614 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
615 LoggingEvents.VoiceSearch.RESULT_CLICKED);
Michael Kolb14612442011-06-24 13:06:29 -0700616 mContext.sendBroadcast(logIntent);
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400617 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700618 if (mInForeground) {
Michael Kolb18eb3772010-12-10 14:29:51 -0800619 return mWebViewController.shouldOverrideUrlLoading(Tab.this,
620 view, url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700621 } else {
622 return false;
623 }
624 }
625
626 /**
627 * Updates the lock icon. This method is called when we discover another
628 * resource to be loaded for this page (for example, javascript). While
629 * we update the icon type, we do not update the lock icon itself until
630 * we are done loading, it is slightly more secure this way.
631 */
632 @Override
633 public void onLoadResource(WebView view, String url) {
634 if (url != null && url.length() > 0) {
635 // It is only if the page claims to be secure that we may have
636 // to update the lock:
John Reck30c714c2010-12-16 17:30:34 -0800637 if (mCurrentState.mLockIcon == LockIcon.LOCK_ICON_SECURE) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700638 // If NOT a 'safe' url, change the lock to mixed content!
639 if (!(URLUtil.isHttpsUrl(url) || URLUtil.isDataUrl(url)
640 || URLUtil.isAboutUrl(url))) {
John Reck30c714c2010-12-16 17:30:34 -0800641 mCurrentState.mLockIcon = LockIcon.LOCK_ICON_MIXED;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700642 }
643 }
644 }
645 }
646
647 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700648 * Show a dialog informing the user of the network error reported by
649 * WebCore if it is in the foreground.
650 */
651 @Override
652 public void onReceivedError(WebView view, int errorCode,
653 String description, String failingUrl) {
654 if (errorCode != WebViewClient.ERROR_HOST_LOOKUP &&
655 errorCode != WebViewClient.ERROR_CONNECT &&
656 errorCode != WebViewClient.ERROR_BAD_URL &&
657 errorCode != WebViewClient.ERROR_UNSUPPORTED_SCHEME &&
658 errorCode != WebViewClient.ERROR_FILE) {
659 queueError(errorCode, description);
660 }
Jeff Hamilton47654f42010-09-07 09:57:51 -0500661
662 // Don't log URLs when in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -0700663 if (!isPrivateBrowsingEnabled()) {
Jeff Hamilton47654f42010-09-07 09:57:51 -0500664 Log.e(LOGTAG, "onReceivedError " + errorCode + " " + failingUrl
665 + " " + description);
666 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700667 }
668
669 /**
670 * Check with the user if it is ok to resend POST data as the page they
671 * are trying to navigate to is the result of a POST.
672 */
673 @Override
674 public void onFormResubmission(WebView view, final Message dontResend,
675 final Message resend) {
676 if (!mInForeground) {
677 dontResend.sendToTarget();
678 return;
679 }
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500680 if (mDontResend != null) {
681 Log.w(LOGTAG, "onFormResubmission should not be called again "
682 + "while dialog is still up");
683 dontResend.sendToTarget();
684 return;
685 }
686 mDontResend = dontResend;
687 mResend = resend;
Michael Kolb14612442011-06-24 13:06:29 -0700688 new AlertDialog.Builder(mContext).setTitle(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700689 R.string.browserFrameFormResubmitLabel).setMessage(
690 R.string.browserFrameFormResubmitMessage)
691 .setPositiveButton(R.string.ok,
692 new DialogInterface.OnClickListener() {
693 public void onClick(DialogInterface dialog,
694 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500695 if (mResend != null) {
696 mResend.sendToTarget();
697 mResend = null;
698 mDontResend = null;
699 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700700 }
701 }).setNegativeButton(R.string.cancel,
702 new DialogInterface.OnClickListener() {
703 public void onClick(DialogInterface dialog,
704 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500705 if (mDontResend != null) {
706 mDontResend.sendToTarget();
707 mResend = null;
708 mDontResend = null;
709 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700710 }
711 }).setOnCancelListener(new OnCancelListener() {
712 public void onCancel(DialogInterface dialog) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500713 if (mDontResend != null) {
714 mDontResend.sendToTarget();
715 mResend = null;
716 mDontResend = null;
717 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700718 }
719 }).show();
720 }
721
722 /**
723 * Insert the url into the visited history database.
724 * @param url The url to be inserted.
725 * @param isReload True if this url is being reloaded.
726 * FIXME: Not sure what to do when reloading the page.
727 */
728 @Override
729 public void doUpdateVisitedHistory(WebView view, String url,
730 boolean isReload) {
John Reck324d4402011-01-11 16:56:42 -0800731 mWebViewController.doUpdateVisitedHistory(Tab.this, isReload);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700732 }
733
734 /**
735 * Displays SSL error(s) dialog to the user.
736 */
737 @Override
738 public void onReceivedSslError(final WebView view,
739 final SslErrorHandler handler, final SslError error) {
740 if (!mInForeground) {
741 handler.cancel();
John Reck30c714c2010-12-16 17:30:34 -0800742 setLockIconType(LockIcon.LOCK_ICON_UNSECURE);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700743 return;
744 }
John Reck35e9dd62011-04-25 09:01:54 -0700745 if (mSettings.showSecurityWarnings()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700746 final LayoutInflater factory =
Michael Kolb14612442011-06-24 13:06:29 -0700747 LayoutInflater.from(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700748 final View warningsView =
749 factory.inflate(R.layout.ssl_warnings, null);
750 final LinearLayout placeholder =
751 (LinearLayout)warningsView.findViewById(R.id.placeholder);
752
753 if (error.hasError(SslError.SSL_UNTRUSTED)) {
754 LinearLayout ll = (LinearLayout)factory
755 .inflate(R.layout.ssl_warning, null);
756 ((TextView)ll.findViewById(R.id.warning))
757 .setText(R.string.ssl_untrusted);
758 placeholder.addView(ll);
759 }
760
761 if (error.hasError(SslError.SSL_IDMISMATCH)) {
762 LinearLayout ll = (LinearLayout)factory
763 .inflate(R.layout.ssl_warning, null);
764 ((TextView)ll.findViewById(R.id.warning))
765 .setText(R.string.ssl_mismatch);
766 placeholder.addView(ll);
767 }
768
769 if (error.hasError(SslError.SSL_EXPIRED)) {
770 LinearLayout ll = (LinearLayout)factory
771 .inflate(R.layout.ssl_warning, null);
772 ((TextView)ll.findViewById(R.id.warning))
773 .setText(R.string.ssl_expired);
774 placeholder.addView(ll);
775 }
776
777 if (error.hasError(SslError.SSL_NOTYETVALID)) {
778 LinearLayout ll = (LinearLayout)factory
779 .inflate(R.layout.ssl_warning, null);
780 ((TextView)ll.findViewById(R.id.warning))
781 .setText(R.string.ssl_not_yet_valid);
782 placeholder.addView(ll);
783 }
784
Michael Kolb14612442011-06-24 13:06:29 -0700785 new AlertDialog.Builder(mContext).setTitle(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700786 R.string.security_warning).setIcon(
787 android.R.drawable.ic_dialog_alert).setView(
788 warningsView).setPositiveButton(R.string.ssl_continue,
789 new DialogInterface.OnClickListener() {
790 public void onClick(DialogInterface dialog,
791 int whichButton) {
792 handler.proceed();
793 }
794 }).setNeutralButton(R.string.view_certificate,
795 new DialogInterface.OnClickListener() {
796 public void onClick(DialogInterface dialog,
797 int whichButton) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700798 mWebViewController.showSslCertificateOnError(view,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700799 handler, error);
800 }
Ben Murdocha49b8292010-11-16 11:56:04 +0000801 }).setNegativeButton(R.string.ssl_go_back,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700802 new DialogInterface.OnClickListener() {
803 public void onClick(DialogInterface dialog,
804 int whichButton) {
John Reck30c714c2010-12-16 17:30:34 -0800805 dialog.cancel();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700806 }
807 }).setOnCancelListener(
808 new DialogInterface.OnCancelListener() {
809 public void onCancel(DialogInterface dialog) {
810 handler.cancel();
John Reck30c714c2010-12-16 17:30:34 -0800811 setLockIconType(LockIcon.LOCK_ICON_UNSECURE);
812 mWebViewController.onUserCanceledSsl(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700813 }
814 }).show();
815 } else {
816 handler.proceed();
817 }
818 }
819
820 /**
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700821 * Displays client certificate request to the user.
822 */
823 @Override
824 public void onReceivedClientCertRequest(final WebView view,
825 final ClientCertRequestHandler handler, final String host_and_port) {
826 if (!mInForeground) {
827 handler.ignore();
828 return;
829 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700830 int colon = host_and_port.lastIndexOf(':');
831 String host;
832 int port;
833 if (colon == -1) {
834 host = host_and_port;
835 port = -1;
836 } else {
837 String portString = host_and_port.substring(colon + 1);
838 try {
839 port = Integer.parseInt(portString);
840 host = host_and_port.substring(0, colon);
841 } catch (NumberFormatException e) {
842 host = host_and_port;
843 port = -1;
844 }
845 }
Michael Kolb14612442011-06-24 13:06:29 -0700846 KeyChain.choosePrivateKeyAlias(
847 mWebViewController.getActivity(), new KeyChainAliasCallback() {
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700848 @Override public void alias(String alias) {
849 if (alias == null) {
850 handler.cancel();
851 return;
852 }
Michael Kolb14612442011-06-24 13:06:29 -0700853 new KeyChainLookup(mContext, handler, alias).execute();
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700854 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700855 }, null, null, host, port, null);
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700856 }
857
858 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700859 * Handles an HTTP authentication request.
860 *
861 * @param handler The authentication handler
862 * @param host The host
863 * @param realm The realm
864 */
865 @Override
866 public void onReceivedHttpAuthRequest(WebView view,
867 final HttpAuthHandler handler, final String host,
868 final String realm) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700869 mWebViewController.onReceivedHttpAuthRequest(Tab.this, view, handler, host, realm);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700870 }
871
872 @Override
John Reck438bf462011-01-12 18:11:46 -0800873 public WebResourceResponse shouldInterceptRequest(WebView view,
874 String url) {
875 WebResourceResponse res = HomeProvider.shouldInterceptRequest(
Michael Kolb14612442011-06-24 13:06:29 -0700876 mContext, url);
John Reck438bf462011-01-12 18:11:46 -0800877 return res;
878 }
879
880 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700881 public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
882 if (!mInForeground) {
883 return false;
884 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700885 return mWebViewController.shouldOverrideKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700886 }
887
888 @Override
889 public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700890 if (!mInForeground) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700891 return;
892 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700893 mWebViewController.onUnhandledKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700894 }
Patrick Scott92066772011-03-10 08:46:27 -0500895
896 @Override
897 public void onReceivedLoginRequest(WebView view, String realm,
898 String account, String args) {
Michael Kolb14612442011-06-24 13:06:29 -0700899 new DeviceAccountLogin(mWebViewController.getActivity(), view, Tab.this, mWebViewController)
Patrick Scott92066772011-03-10 08:46:27 -0500900 .handleLogin(realm, account, args);
901 }
902
Grace Kloba22ac16e2009-10-07 18:00:23 -0700903 };
904
John Reck1cf4b792011-07-26 10:22:22 -0700905 private void syncCurrentState(WebView view, String url) {
906 // Sync state (in case of stop/timeout)
907 mCurrentState.mUrl = view.getUrl();
908 if (mCurrentState.mUrl == null) {
909 mCurrentState.mUrl = url != null ? url : "";
910 }
911 mCurrentState.mOriginalUrl = view.getOriginalUrl();
912 mCurrentState.mTitle = view.getTitle();
913 mCurrentState.mFavicon = view.getFavicon();
914 if (!URLUtil.isHttpsUrl(mCurrentState.mUrl)) {
915 // In case we stop when loading an HTTPS page from an HTTP page
916 // but before a provisional load occurred
917 mCurrentState.mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
918 }
John Reck502a3532011-08-16 14:21:46 -0700919 mCurrentState.mIncognito = view.isPrivateBrowsingEnabled();
John Reck1cf4b792011-07-26 10:22:22 -0700920 }
921
Patrick Scott92066772011-03-10 08:46:27 -0500922 // Called by DeviceAccountLogin when the Tab needs to have the auto-login UI
923 // displayed.
924 void setDeviceAccountLogin(DeviceAccountLogin login) {
925 mDeviceAccountLogin = login;
926 }
927
928 // Returns non-null if the title bar should display the auto-login UI.
929 DeviceAccountLogin getDeviceAccountLogin() {
930 return mDeviceAccountLogin;
931 }
932
Grace Kloba22ac16e2009-10-07 18:00:23 -0700933 // -------------------------------------------------------------------------
934 // WebChromeClient implementation for the main WebView
935 // -------------------------------------------------------------------------
936
937 private final WebChromeClient mWebChromeClient = new WebChromeClient() {
938 // Helper method to create a new tab or sub window.
939 private void createWindow(final boolean dialog, final Message msg) {
940 WebView.WebViewTransport transport =
941 (WebView.WebViewTransport) msg.obj;
942 if (dialog) {
943 createSubWindow();
Michael Kolb8233fac2010-10-26 16:08:53 -0700944 mWebViewController.attachSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700945 transport.setWebView(mSubView);
946 } else {
Michael Kolb7bcafde2011-05-09 13:55:59 -0700947 final Tab newTab = mWebViewController.openTab(null,
John Reck5949c662011-05-27 09:52:29 -0700948 Tab.this, true, true);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700949 transport.setWebView(newTab.getWebView());
950 }
951 msg.sendToTarget();
952 }
953
954 @Override
955 public boolean onCreateWindow(WebView view, final boolean dialog,
956 final boolean userGesture, final Message resultMsg) {
957 // only allow new window or sub window for the foreground case
958 if (!mInForeground) {
959 return false;
960 }
961 // Short-circuit if we can't create any more tabs or sub windows.
962 if (dialog && mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -0700963 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700964 .setTitle(R.string.too_many_subwindows_dialog_title)
965 .setIcon(android.R.drawable.ic_dialog_alert)
966 .setMessage(R.string.too_many_subwindows_dialog_message)
967 .setPositiveButton(R.string.ok, null)
968 .show();
969 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700970 } else if (!mWebViewController.getTabControl().canCreateNewTab()) {
Michael Kolb14612442011-06-24 13:06:29 -0700971 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700972 .setTitle(R.string.too_many_windows_dialog_title)
973 .setIcon(android.R.drawable.ic_dialog_alert)
974 .setMessage(R.string.too_many_windows_dialog_message)
975 .setPositiveButton(R.string.ok, null)
976 .show();
977 return false;
978 }
979
980 // Short-circuit if this was a user gesture.
981 if (userGesture) {
982 createWindow(dialog, resultMsg);
983 return true;
984 }
985
986 // Allow the popup and create the appropriate window.
987 final AlertDialog.OnClickListener allowListener =
988 new AlertDialog.OnClickListener() {
989 public void onClick(DialogInterface d,
990 int which) {
991 createWindow(dialog, resultMsg);
992 }
993 };
994
995 // Block the popup by returning a null WebView.
996 final AlertDialog.OnClickListener blockListener =
997 new AlertDialog.OnClickListener() {
998 public void onClick(DialogInterface d, int which) {
999 resultMsg.sendToTarget();
1000 }
1001 };
1002
1003 // Build a confirmation dialog to display to the user.
1004 final AlertDialog d =
Michael Kolb14612442011-06-24 13:06:29 -07001005 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -07001006 .setTitle(R.string.attention)
1007 .setIcon(android.R.drawable.ic_dialog_alert)
1008 .setMessage(R.string.popup_window_attempt)
1009 .setPositiveButton(R.string.allow, allowListener)
1010 .setNegativeButton(R.string.block, blockListener)
1011 .setCancelable(false)
1012 .create();
1013
1014 // Show the confirmation dialog.
1015 d.show();
1016 return true;
1017 }
1018
1019 @Override
Patrick Scotteb5061b2009-11-18 15:00:30 -05001020 public void onRequestFocus(WebView view) {
1021 if (!mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -07001022 mWebViewController.switchToTab(Tab.this);
Patrick Scotteb5061b2009-11-18 15:00:30 -05001023 }
1024 }
1025
1026 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -07001027 public void onCloseWindow(WebView window) {
Michael Kolbc831b632011-05-11 09:30:34 -07001028 if (mParent != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001029 // JavaScript can only close popup window.
1030 if (mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -07001031 mWebViewController.switchToTab(mParent);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001032 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001033 mWebViewController.closeTab(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001034 }
1035 }
1036
1037 @Override
1038 public void onProgressChanged(WebView view, int newProgress) {
John Reck30c714c2010-12-16 17:30:34 -08001039 mPageLoadProgress = newProgress;
1040 mWebViewController.onProgressChanged(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001041 }
1042
1043 @Override
Leon Scroggins21d9b902010-03-11 09:33:11 -05001044 public void onReceivedTitle(WebView view, final String title) {
John Reck30c714c2010-12-16 17:30:34 -08001045 mCurrentState.mTitle = title;
Michael Kolb8233fac2010-10-26 16:08:53 -07001046 mWebViewController.onReceivedTitle(Tab.this, title);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001047 }
1048
1049 @Override
1050 public void onReceivedIcon(WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -08001051 mCurrentState.mFavicon = icon;
Michael Kolb8233fac2010-10-26 16:08:53 -07001052 mWebViewController.onFavicon(Tab.this, view, icon);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001053 }
1054
1055 @Override
1056 public void onReceivedTouchIconUrl(WebView view, String url,
1057 boolean precomposed) {
Michael Kolb14612442011-06-24 13:06:29 -07001058 final ContentResolver cr = mContext.getContentResolver();
Leon Scrogginsc8393d92010-04-23 14:58:16 -04001059 // Let precomposed icons take precedence over non-composed
1060 // icons.
1061 if (precomposed && mTouchIconLoader != null) {
1062 mTouchIconLoader.cancel(false);
1063 mTouchIconLoader = null;
1064 }
1065 // Have only one async task at a time.
1066 if (mTouchIconLoader == null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001067 mTouchIconLoader = new DownloadTouchIcon(Tab.this,
Michael Kolb14612442011-06-24 13:06:29 -07001068 mContext, cr, view);
Leon Scrogginsc8393d92010-04-23 14:58:16 -04001069 mTouchIconLoader.execute(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001070 }
1071 }
1072
1073 @Override
1074 public void onShowCustomView(View view,
1075 WebChromeClient.CustomViewCallback callback) {
Michael Kolb14612442011-06-24 13:06:29 -07001076 Activity activity = mWebViewController.getActivity();
1077 if (activity != null) {
1078 onShowCustomView(view, activity.getRequestedOrientation(), callback);
1079 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001080 }
1081
1082 @Override
1083 public void onShowCustomView(View view, int requestedOrientation,
1084 WebChromeClient.CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001085 if (mInForeground) mWebViewController.showCustomView(Tab.this, view,
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001086 requestedOrientation, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001087 }
1088
1089 @Override
1090 public void onHideCustomView() {
Michael Kolb8233fac2010-10-26 16:08:53 -07001091 if (mInForeground) mWebViewController.hideCustomView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001092 }
1093
1094 /**
1095 * The origin has exceeded its database quota.
1096 * @param url the URL that exceeded the quota
1097 * @param databaseIdentifier the identifier of the database on which the
1098 * transaction that caused the quota overflow was run
1099 * @param currentQuota the current quota for the origin.
1100 * @param estimatedSize the estimated size of the database.
1101 * @param totalUsedQuota is the sum of all origins' quota.
1102 * @param quotaUpdater The callback to run when a decision to allow or
1103 * deny quota has been made. Don't forget to call this!
1104 */
1105 @Override
1106 public void onExceededDatabaseQuota(String url,
1107 String databaseIdentifier, long currentQuota, long estimatedSize,
1108 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -07001109 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001110 .onExceededDatabaseQuota(url, databaseIdentifier,
1111 currentQuota, estimatedSize, totalUsedQuota,
1112 quotaUpdater);
1113 }
1114
1115 /**
1116 * The Application Cache has exceeded its max size.
1117 * @param spaceNeeded is the amount of disk space that would be needed
1118 * in order for the last appcache operation to succeed.
1119 * @param totalUsedQuota is the sum of all origins' quota.
1120 * @param quotaUpdater A callback to inform the WebCore thread that a
1121 * new app cache size is available. This callback must always
1122 * be executed at some point to ensure that the sleeping
1123 * WebCore thread is woken up.
1124 */
1125 @Override
1126 public void onReachedMaxAppCacheSize(long spaceNeeded,
1127 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -07001128 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001129 .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota,
1130 quotaUpdater);
1131 }
1132
1133 /**
1134 * Instructs the browser to show a prompt to ask the user to set the
1135 * Geolocation permission state for the specified origin.
1136 * @param origin The origin for which Geolocation permissions are
1137 * requested.
1138 * @param callback The callback to call once the user has set the
1139 * Geolocation permission state.
1140 */
1141 @Override
1142 public void onGeolocationPermissionsShowPrompt(String origin,
1143 GeolocationPermissions.Callback callback) {
1144 if (mInForeground) {
Grace Kloba50c241e2010-04-20 11:07:50 -07001145 getGeolocationPermissionsPrompt().show(origin, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001146 }
1147 }
1148
1149 /**
1150 * Instructs the browser to hide the Geolocation permissions prompt.
1151 */
1152 @Override
1153 public void onGeolocationPermissionsHidePrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001154 if (mInForeground && mGeolocationPermissionsPrompt != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001155 mGeolocationPermissionsPrompt.hide();
1156 }
1157 }
1158
Ben Murdoch65acc352009-11-19 18:16:04 +00001159 /* Adds a JavaScript error message to the system log and if the JS
1160 * console is enabled in the about:debug options, to that console
1161 * also.
Ben Murdochc42addf2010-01-28 15:19:59 +00001162 * @param consoleMessage the message object.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001163 */
1164 @Override
Ben Murdochc42addf2010-01-28 15:19:59 +00001165 public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001166 if (mInForeground) {
1167 // call getErrorConsole(true) so it will create one if needed
1168 ErrorConsoleView errorConsole = getErrorConsole(true);
Ben Murdochc42addf2010-01-28 15:19:59 +00001169 errorConsole.addErrorMessage(consoleMessage);
Michael Kolb8233fac2010-10-26 16:08:53 -07001170 if (mWebViewController.shouldShowErrorConsole()
1171 && errorConsole.getShowState() !=
1172 ErrorConsoleView.SHOW_MAXIMIZED) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001173 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
1174 }
1175 }
Ben Murdochc42addf2010-01-28 15:19:59 +00001176
Jeff Hamilton47654f42010-09-07 09:57:51 -05001177 // Don't log console messages in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001178 if (isPrivateBrowsingEnabled()) return true;
Jeff Hamilton47654f42010-09-07 09:57:51 -05001179
Ben Murdochc42addf2010-01-28 15:19:59 +00001180 String message = "Console: " + consoleMessage.message() + " "
1181 + consoleMessage.sourceId() + ":"
1182 + consoleMessage.lineNumber();
1183
1184 switch (consoleMessage.messageLevel()) {
1185 case TIP:
1186 Log.v(CONSOLE_LOGTAG, message);
1187 break;
1188 case LOG:
1189 Log.i(CONSOLE_LOGTAG, message);
1190 break;
1191 case WARNING:
1192 Log.w(CONSOLE_LOGTAG, message);
1193 break;
1194 case ERROR:
1195 Log.e(CONSOLE_LOGTAG, message);
1196 break;
1197 case DEBUG:
1198 Log.d(CONSOLE_LOGTAG, message);
1199 break;
1200 }
1201
1202 return true;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001203 }
1204
1205 /**
1206 * Ask the browser for an icon to represent a <video> element.
1207 * This icon will be used if the Web page did not specify a poster attribute.
1208 * @return Bitmap The icon or null if no such icon is available.
1209 */
1210 @Override
1211 public Bitmap getDefaultVideoPoster() {
1212 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001213 return mWebViewController.getDefaultVideoPoster();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001214 }
1215 return null;
1216 }
1217
1218 /**
1219 * Ask the host application for a custom progress view to show while
1220 * a <video> is loading.
1221 * @return View The progress view.
1222 */
1223 @Override
1224 public View getVideoLoadingProgressView() {
1225 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001226 return mWebViewController.getVideoLoadingProgressView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001227 }
1228 return null;
1229 }
1230
1231 @Override
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01001232 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001233 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001234 mWebViewController.openFileChooser(uploadMsg, acceptType);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001235 } else {
1236 uploadMsg.onReceiveValue(null);
1237 }
1238 }
1239
1240 /**
1241 * Deliver a list of already-visited URLs
1242 */
1243 @Override
1244 public void getVisitedHistory(final ValueCallback<String[]> callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001245 mWebViewController.getVisitedHistory(callback);
1246 }
Ben Murdoch8029a772010-11-16 11:58:21 +00001247
1248 @Override
1249 public void setupAutoFill(Message message) {
1250 // Prompt the user to set up their profile.
1251 final Message msg = message;
Michael Kolb14612442011-06-24 13:06:29 -07001252 AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
1253 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
Ben Murdoch1d676b62011-01-17 12:54:24 +00001254 Context.LAYOUT_INFLATER_SERVICE);
1255 final View layout = inflater.inflate(R.layout.setup_autofill_dialog, null);
1256
1257 builder.setView(layout)
Ben Murdochb7e6f942011-07-08 13:00:21 +01001258 .setTitle(R.string.autofill_setup_dialog_title)
Ben Murdoch1d676b62011-01-17 12:54:24 +00001259 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1260 @Override
1261 public void onClick(DialogInterface dialog, int id) {
1262 CheckBox disableAutoFill = (CheckBox) layout.findViewById(
1263 R.id.setup_autofill_dialog_disable_autofill);
1264
1265 if (disableAutoFill.isChecked()) {
1266 // Disable autofill and show a toast with how to turn it on again.
John Reck35e9dd62011-04-25 09:01:54 -07001267 mSettings.setAutofillEnabled(false);
Michael Kolb14612442011-06-24 13:06:29 -07001268 Toast.makeText(mContext,
Ben Murdoch1d676b62011-01-17 12:54:24 +00001269 R.string.autofill_setup_dialog_negative_toast,
1270 Toast.LENGTH_LONG).show();
1271 } else {
1272 // Take user to the AutoFill profile editor. When they return,
1273 // we will send the message that we pass here which will trigger
1274 // the form to get filled out with their new profile.
1275 mWebViewController.setupAutoFill(msg);
1276 }
1277 }
1278 })
1279 .setNegativeButton(R.string.cancel, null)
1280 .show();
Ben Murdoch8029a772010-11-16 11:58:21 +00001281 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001282 };
1283
1284 // -------------------------------------------------------------------------
1285 // WebViewClient implementation for the sub window
1286 // -------------------------------------------------------------------------
1287
1288 // Subclass of WebViewClient used in subwindows to notify the main
1289 // WebViewClient of certain WebView activities.
1290 private static class SubWindowClient extends WebViewClient {
1291 // The main WebViewClient.
1292 private final WebViewClient mClient;
Michael Kolb8233fac2010-10-26 16:08:53 -07001293 private final WebViewController mController;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001294
Michael Kolb8233fac2010-10-26 16:08:53 -07001295 SubWindowClient(WebViewClient client, WebViewController controller) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001296 mClient = client;
Michael Kolb8233fac2010-10-26 16:08:53 -07001297 mController = controller;
Leon Scroggins III211ba542010-04-19 13:21:13 -04001298 }
1299 @Override
1300 public void onPageStarted(WebView view, String url, Bitmap favicon) {
1301 // Unlike the others, do not call mClient's version, which would
1302 // change the progress bar. However, we do want to remove the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001303 // find or select dialog.
Michael Kolb8233fac2010-10-26 16:08:53 -07001304 mController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001305 }
1306 @Override
1307 public void doUpdateVisitedHistory(WebView view, String url,
1308 boolean isReload) {
1309 mClient.doUpdateVisitedHistory(view, url, isReload);
1310 }
1311 @Override
1312 public boolean shouldOverrideUrlLoading(WebView view, String url) {
1313 return mClient.shouldOverrideUrlLoading(view, url);
1314 }
1315 @Override
1316 public void onReceivedSslError(WebView view, SslErrorHandler handler,
1317 SslError error) {
1318 mClient.onReceivedSslError(view, handler, error);
1319 }
1320 @Override
Brian Carlstrom8862c1d2011-06-02 01:05:55 -07001321 public void onReceivedClientCertRequest(WebView view,
1322 ClientCertRequestHandler handler, String host_and_port) {
1323 mClient.onReceivedClientCertRequest(view, handler, host_and_port);
1324 }
1325 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -07001326 public void onReceivedHttpAuthRequest(WebView view,
1327 HttpAuthHandler handler, String host, String realm) {
1328 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
1329 }
1330 @Override
1331 public void onFormResubmission(WebView view, Message dontResend,
1332 Message resend) {
1333 mClient.onFormResubmission(view, dontResend, resend);
1334 }
1335 @Override
1336 public void onReceivedError(WebView view, int errorCode,
1337 String description, String failingUrl) {
1338 mClient.onReceivedError(view, errorCode, description, failingUrl);
1339 }
1340 @Override
1341 public boolean shouldOverrideKeyEvent(WebView view,
1342 android.view.KeyEvent event) {
1343 return mClient.shouldOverrideKeyEvent(view, event);
1344 }
1345 @Override
1346 public void onUnhandledKeyEvent(WebView view,
1347 android.view.KeyEvent event) {
1348 mClient.onUnhandledKeyEvent(view, event);
1349 }
1350 }
1351
1352 // -------------------------------------------------------------------------
1353 // WebChromeClient implementation for the sub window
1354 // -------------------------------------------------------------------------
1355
1356 private class SubWindowChromeClient extends WebChromeClient {
1357 // The main WebChromeClient.
1358 private final WebChromeClient mClient;
1359
1360 SubWindowChromeClient(WebChromeClient client) {
1361 mClient = client;
1362 }
1363 @Override
1364 public void onProgressChanged(WebView view, int newProgress) {
1365 mClient.onProgressChanged(view, newProgress);
1366 }
1367 @Override
1368 public boolean onCreateWindow(WebView view, boolean dialog,
1369 boolean userGesture, android.os.Message resultMsg) {
1370 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
1371 }
1372 @Override
1373 public void onCloseWindow(WebView window) {
1374 if (window != mSubView) {
1375 Log.e(LOGTAG, "Can't close the window");
1376 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001377 mWebViewController.dismissSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001378 }
1379 }
1380
1381 // -------------------------------------------------------------------------
1382
1383 // Construct a new tab
Michael Kolb7bcafde2011-05-09 13:55:59 -07001384 Tab(WebViewController wvcontroller, WebView w) {
John Reck1cf4b792011-07-26 10:22:22 -07001385 this(wvcontroller, w, null);
1386 }
1387
1388 Tab(WebViewController wvcontroller, Bundle state) {
1389 this(wvcontroller, null, state);
1390 }
1391
1392 Tab(WebViewController wvcontroller, WebView w, Bundle state) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001393 mWebViewController = wvcontroller;
Michael Kolb14612442011-06-24 13:06:29 -07001394 mContext = mWebViewController.getContext();
John Reck35e9dd62011-04-25 09:01:54 -07001395 mSettings = BrowserSettings.getInstance();
Michael Kolb14612442011-06-24 13:06:29 -07001396 mDataController = DataController.getInstance(mContext);
1397 mCurrentState = new PageState(mContext, w != null
John Reck847b5322011-04-14 17:02:18 -07001398 ? w.isPrivateBrowsingEnabled() : false);
Michael Kolb8233fac2010-10-26 16:08:53 -07001399 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001400 mInForeground = false;
1401
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001402 mDownloadListener = new DownloadListener() {
1403 public void onDownloadStart(String url, String userAgent,
1404 String contentDisposition, String mimetype,
1405 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001406 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001407 mimetype, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001408 }
1409 };
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001410 mWebBackForwardListClient = new WebBackForwardListClient() {
1411 @Override
1412 public void onNewHistoryItem(WebHistoryItem item) {
1413 if (isInVoiceSearchMode()) {
1414 item.setCustomData(mVoiceSearchData.mVoiceSearchIntent);
1415 }
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001416 if (mClearHistoryMatchUrl != null) {
1417 if (LOGD_ENABLED) {
1418 Log.d(LOGTAG, "onNewHistoryItem:\n\t" + item.getUrl() + "\n\t"
1419 + mClearHistoryMatchUrl);
1420 }
1421 if (TextUtils.equals(item.getOriginalUrl(), mClearHistoryMatchUrl)) {
1422 if (mMainView != null) {
1423 mMainView.clearHistory();
1424 }
1425 }
1426 mClearHistoryMatchUrl = null;
1427 }
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001428 }
1429 @Override
1430 public void onIndexChanged(WebHistoryItem item, int index) {
1431 Object data = item.getCustomData();
1432 if (data != null && data instanceof Intent) {
1433 activateVoiceSearchMode((Intent) data);
1434 }
1435 }
1436 };
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001437
John Reck1cf4b792011-07-26 10:22:22 -07001438 mCaptureWidth = mContext.getResources().getDimensionPixelSize(
1439 R.dimen.tab_thumbnail_width);
1440 mCaptureHeight = mContext.getResources().getDimensionPixelSize(
1441 R.dimen.tab_thumbnail_height);
1442 updateShouldCaptureThumbnails();
1443 restoreState(state);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001444 setWebView(w);
Michael Kolb9ef259a2011-07-12 15:33:08 -07001445 mHandler = new Handler() {
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001446 @Override
Michael Kolb9ef259a2011-07-12 15:33:08 -07001447 public void handleMessage(Message m) {
John Reck1cf4b792011-07-26 10:22:22 -07001448 switch (m.what) {
1449 case MSG_CAPTURE:
1450 capture();
1451 break;
1452 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001453 }
1454 };
John Reck1cf4b792011-07-26 10:22:22 -07001455 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001456
John Reck1cf4b792011-07-26 10:22:22 -07001457 public void updateShouldCaptureThumbnails() {
1458 if (mWebViewController.shouldCaptureThumbnails()) {
1459 synchronized (Tab.this) {
1460 if (mCapture == null) {
1461 mCapture = Bitmap.createBitmap(mCaptureWidth, mCaptureHeight,
1462 Bitmap.Config.RGB_565);
Michael Kolbc3af0672011-08-09 10:24:41 -07001463 mCapture.eraseColor(Color.WHITE);
John Reck1cf4b792011-07-26 10:22:22 -07001464 if (mInForeground) {
1465 postCapture();
1466 }
1467 }
1468 }
1469 } else {
1470 synchronized (Tab.this) {
1471 mCapture = null;
1472 deleteThumbnail();
1473 }
1474 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001475 }
1476
Michael Kolb14612442011-06-24 13:06:29 -07001477 public void setController(WebViewController ctl) {
1478 mWebViewController = ctl;
John Reck1cf4b792011-07-26 10:22:22 -07001479 updateShouldCaptureThumbnails();
Michael Kolb14612442011-06-24 13:06:29 -07001480 }
1481
Michael Kolbc831b632011-05-11 09:30:34 -07001482 public void setId(long id) {
1483 mId = id;
1484 }
1485
1486 public long getId() {
1487 return mId;
1488 }
1489
Grace Kloba22ac16e2009-10-07 18:00:23 -07001490 /**
1491 * Sets the WebView for this tab, correctly removing the old WebView from
1492 * the container view.
1493 */
1494 void setWebView(WebView w) {
1495 if (mMainView == w) {
1496 return;
1497 }
Michael Kolba713ec82010-11-29 17:27:06 -08001498
Grace Kloba22ac16e2009-10-07 18:00:23 -07001499 // If the WebView is changing, the page will be reloaded, so any ongoing
1500 // Geolocation permission requests are void.
Grace Kloba50c241e2010-04-20 11:07:50 -07001501 if (mGeolocationPermissionsPrompt != null) {
1502 mGeolocationPermissionsPrompt.hide();
1503 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001504
Michael Kolba713ec82010-11-29 17:27:06 -08001505 mWebViewController.onSetWebView(this, w);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001506
John Reck1cf4b792011-07-26 10:22:22 -07001507 if (mMainView != null) {
John Reck8ee633f2011-08-09 16:00:35 -07001508 mMainView.setPictureListener(null);
John Reck1cf4b792011-07-26 10:22:22 -07001509 if (w != null) {
1510 syncCurrentState(w, null);
1511 } else {
1512 mCurrentState = new PageState(mContext, false);
1513 }
1514 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001515 // set the new one
1516 mMainView = w;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001517 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001518 if (mMainView != null) {
1519 mMainView.setWebViewClient(mWebViewClient);
1520 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001521 // Attach DownloadManager so that downloads can start in an active
1522 // or a non-active window. This can happen when going to a site that
1523 // does a redirect after a period of time. The user could have
1524 // switched to another tab while waiting for the download to start.
1525 mMainView.setDownloadListener(mDownloadListener);
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001526 mMainView.setWebBackForwardListClient(mWebBackForwardListClient);
John Reck8ee633f2011-08-09 16:00:35 -07001527 TabControl tc = mWebViewController.getTabControl();
1528 if (tc != null && tc.getOnThumbnailUpdatedListener() != null) {
1529 mMainView.setPictureListener(this);
1530 }
John Reck1cf4b792011-07-26 10:22:22 -07001531 if (mSavedState != null) {
1532 mMainView.restoreState(mSavedState);
1533 mSavedState = null;
1534 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001535 }
1536 }
1537
1538 /**
1539 * Destroy the tab's main WebView and subWindow if any
1540 */
1541 void destroy() {
1542 if (mMainView != null) {
1543 dismissSubWindow();
John Reckef654f12011-07-12 16:42:08 -07001544 // Make sure the embedded title bar isn't still attached
1545 mMainView.setEmbeddedTitleBar(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001546 // save the WebView to call destroy() after detach it from the tab
1547 WebView webView = mMainView;
1548 setWebView(null);
1549 webView.destroy();
1550 }
1551 }
1552
1553 /**
1554 * Remove the tab from the parent
1555 */
1556 void removeFromTree() {
1557 // detach the children
Michael Kolbc831b632011-05-11 09:30:34 -07001558 if (mChildren != null) {
1559 for(Tab t : mChildren) {
1560 t.setParent(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001561 }
1562 }
1563 // remove itself from the parent list
Michael Kolbc831b632011-05-11 09:30:34 -07001564 if (mParent != null) {
1565 mParent.mChildren.remove(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001566 }
John Reck1cf4b792011-07-26 10:22:22 -07001567 deleteThumbnail();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001568 }
1569
1570 /**
1571 * Create a new subwindow unless a subwindow already exists.
1572 * @return True if a new subwindow was created. False if one already exists.
1573 */
1574 boolean createSubWindow() {
1575 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001576 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001577 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001578 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001579 mSubView.setWebChromeClient(new SubWindowChromeClient(
1580 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001581 // Set a different DownloadListener for the mSubView, since it will
1582 // just need to dismiss the mSubView, rather than close the Tab
1583 mSubView.setDownloadListener(new DownloadListener() {
1584 public void onDownloadStart(String url, String userAgent,
1585 String contentDisposition, String mimetype,
1586 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001587 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001588 contentDisposition, mimetype, contentLength);
1589 if (mSubView.copyBackForwardList().getSize() == 0) {
1590 // This subwindow was opened for the sole purpose of
1591 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001592 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001593 }
1594 }
1595 });
Michael Kolb14612442011-06-24 13:06:29 -07001596 mSubView.setOnCreateContextMenuListener(mWebViewController.getActivity());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001597 return true;
1598 }
1599 return false;
1600 }
1601
1602 /**
1603 * Dismiss the subWindow for the tab.
1604 */
1605 void dismissSubWindow() {
1606 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001607 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001608 mSubView.destroy();
1609 mSubView = null;
1610 mSubViewContainer = null;
1611 }
1612 }
1613
Grace Kloba22ac16e2009-10-07 18:00:23 -07001614
1615 /**
1616 * Set the parent tab of this tab.
1617 */
Michael Kolbc831b632011-05-11 09:30:34 -07001618 void setParent(Tab parent) {
1619 mParent = parent;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001620 // This tab may have been freed due to low memory. If that is the case,
Michael Kolbc831b632011-05-11 09:30:34 -07001621 // the parent tab id is already saved. If we are changing that id
Grace Kloba22ac16e2009-10-07 18:00:23 -07001622 // (most likely due to removing the parent tab) we must update the
Michael Kolbc831b632011-05-11 09:30:34 -07001623 // parent tab id in the saved Bundle.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001624 if (mSavedState != null) {
1625 if (parent == null) {
1626 mSavedState.remove(PARENTTAB);
1627 } else {
Michael Kolbc831b632011-05-11 09:30:34 -07001628 mSavedState.putLong(PARENTTAB, parent.getId());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001629 }
1630 }
John Reckb0a86db2011-05-24 14:05:58 -07001631
1632 // Sync the WebView useragent with the parent
1633 if (parent != null && mSettings.hasDesktopUseragent(parent.getWebView())
1634 != mSettings.hasDesktopUseragent(getWebView())) {
1635 mSettings.toggleDesktopUseragent(getWebView());
1636 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001637 }
1638
1639 /**
Michael Kolbc831b632011-05-11 09:30:34 -07001640 * If this Tab was created through another Tab, then this method returns
1641 * that Tab.
1642 * @return the Tab parent or null
1643 */
1644 public Tab getParent() {
1645 return mParent;
1646 }
1647
1648 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001649 * When a Tab is created through the content of another Tab, then we
1650 * associate the Tabs.
1651 * @param child the Tab that was created from this Tab
1652 */
1653 void addChildTab(Tab child) {
Michael Kolbc831b632011-05-11 09:30:34 -07001654 if (mChildren == null) {
1655 mChildren = new Vector<Tab>();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001656 }
Michael Kolbc831b632011-05-11 09:30:34 -07001657 mChildren.add(child);
1658 child.setParent(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001659 }
1660
Michael Kolbc831b632011-05-11 09:30:34 -07001661 Vector<Tab> getChildren() {
1662 return mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001663 }
1664
1665 void resume() {
1666 if (mMainView != null) {
John Reck56c1fcf2011-08-17 10:15:16 -07001667 setupHwAcceleration(mMainView);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001668 mMainView.onResume();
1669 if (mSubView != null) {
1670 mSubView.onResume();
1671 }
1672 }
1673 }
1674
John Reck56c1fcf2011-08-17 10:15:16 -07001675 private void setupHwAcceleration(View web) {
1676 if (web == null) return;
1677 BrowserSettings settings = BrowserSettings.getInstance();
1678 if (settings.isHardwareAccelerated()) {
1679 web.setLayerType(View.LAYER_TYPE_NONE, null);
1680 } else {
1681 web.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
1682 }
1683 }
1684
Grace Kloba22ac16e2009-10-07 18:00:23 -07001685 void pause() {
1686 if (mMainView != null) {
1687 mMainView.onPause();
1688 if (mSubView != null) {
1689 mSubView.onPause();
1690 }
1691 }
1692 }
1693
1694 void putInForeground() {
John Reck8ee633f2011-08-09 16:00:35 -07001695 if (mInForeground) {
1696 return;
1697 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001698 mInForeground = true;
1699 resume();
Michael Kolb14612442011-06-24 13:06:29 -07001700 Activity activity = mWebViewController.getActivity();
1701 mMainView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001702 if (mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -07001703 mSubView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001704 }
1705 // Show the pending error dialog if the queue is not empty
1706 if (mQueuedErrors != null && mQueuedErrors.size() > 0) {
1707 showError(mQueuedErrors.getFirst());
1708 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001709 mWebViewController.bookmarkedStatusHasChanged(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001710 }
1711
1712 void putInBackground() {
John Reck8ee633f2011-08-09 16:00:35 -07001713 if (!mInForeground) {
1714 return;
1715 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001716 mInForeground = false;
1717 pause();
1718 mMainView.setOnCreateContextMenuListener(null);
1719 if (mSubView != null) {
1720 mSubView.setOnCreateContextMenuListener(null);
1721 }
1722 }
1723
Michael Kolb8233fac2010-10-26 16:08:53 -07001724 boolean inForeground() {
1725 return mInForeground;
1726 }
1727
Grace Kloba22ac16e2009-10-07 18:00:23 -07001728 /**
1729 * Return the top window of this tab; either the subwindow if it is not
1730 * null or the main window.
1731 * @return The top window of this tab.
1732 */
1733 WebView getTopWindow() {
1734 if (mSubView != null) {
1735 return mSubView;
1736 }
1737 return mMainView;
1738 }
1739
1740 /**
1741 * Return the main window of this tab. Note: if a tab is freed in the
1742 * background, this can return null. It is only guaranteed to be
1743 * non-null for the current tab.
1744 * @return The main WebView of this tab.
1745 */
1746 WebView getWebView() {
1747 return mMainView;
1748 }
1749
Michael Kolba713ec82010-11-29 17:27:06 -08001750 void setViewContainer(View container) {
1751 mContainer = container;
1752 }
1753
Michael Kolb8233fac2010-10-26 16:08:53 -07001754 View getViewContainer() {
1755 return mContainer;
1756 }
1757
Grace Kloba22ac16e2009-10-07 18:00:23 -07001758 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001759 * Return whether private browsing is enabled for the main window of
1760 * this tab.
1761 * @return True if private browsing is enabled.
1762 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001763 boolean isPrivateBrowsingEnabled() {
John Reck502a3532011-08-16 14:21:46 -07001764 return mCurrentState.mIncognito;
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001765 }
1766
1767 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001768 * Return the subwindow of this tab or null if there is no subwindow.
1769 * @return The subwindow of this tab or null.
1770 */
1771 WebView getSubWebView() {
1772 return mSubView;
1773 }
1774
Michael Kolb1514bb72010-11-22 09:11:48 -08001775 void setSubWebView(WebView subView) {
1776 mSubView = subView;
1777 }
1778
Michael Kolb8233fac2010-10-26 16:08:53 -07001779 View getSubViewContainer() {
1780 return mSubViewContainer;
1781 }
1782
Michael Kolb1514bb72010-11-22 09:11:48 -08001783 void setSubViewContainer(View subViewContainer) {
1784 mSubViewContainer = subViewContainer;
1785 }
1786
Grace Kloba22ac16e2009-10-07 18:00:23 -07001787 /**
1788 * @return The geolocation permissions prompt for this tab.
1789 */
1790 GeolocationPermissionsPrompt getGeolocationPermissionsPrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001791 if (mGeolocationPermissionsPrompt == null) {
1792 ViewStub stub = (ViewStub) mContainer
1793 .findViewById(R.id.geolocation_permissions_prompt);
1794 mGeolocationPermissionsPrompt = (GeolocationPermissionsPrompt) stub
1795 .inflate();
Grace Kloba50c241e2010-04-20 11:07:50 -07001796 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001797 return mGeolocationPermissionsPrompt;
1798 }
1799
1800 /**
1801 * @return The application id string
1802 */
1803 String getAppId() {
1804 return mAppId;
1805 }
1806
1807 /**
1808 * Set the application id string
1809 * @param id
1810 */
1811 void setAppId(String id) {
1812 mAppId = id;
1813 }
1814
Michael Kolbe28b3472011-08-04 16:54:31 -07001815 boolean closeOnBack() {
1816 return mCloseOnBack;
1817 }
1818
1819 void setCloseOnBack(boolean close) {
1820 mCloseOnBack = close;
1821 }
1822
Grace Kloba22ac16e2009-10-07 18:00:23 -07001823 String getUrl() {
John Reck324d4402011-01-11 16:56:42 -08001824 return UrlUtils.filteredUrl(mCurrentState.mUrl);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001825 }
1826
John Reck49a603c2011-03-03 09:33:05 -08001827 String getOriginalUrl() {
John Reckdb22ec42011-06-29 11:31:24 -07001828 if (mCurrentState.mOriginalUrl == null) {
1829 return getUrl();
John Reck49a603c2011-03-03 09:33:05 -08001830 }
John Reckdb22ec42011-06-29 11:31:24 -07001831 return UrlUtils.filteredUrl(mCurrentState.mOriginalUrl);
John Reck49a603c2011-03-03 09:33:05 -08001832 }
1833
Grace Kloba22ac16e2009-10-07 18:00:23 -07001834 /**
John Reck30c714c2010-12-16 17:30:34 -08001835 * Get the title of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001836 */
1837 String getTitle() {
John Reck30c714c2010-12-16 17:30:34 -08001838 if (mCurrentState.mTitle == null && mInPageLoad) {
Michael Kolb14612442011-06-24 13:06:29 -07001839 return mContext.getString(R.string.title_bar_loading);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001840 }
John Reck30c714c2010-12-16 17:30:34 -08001841 return mCurrentState.mTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001842 }
1843
1844 /**
John Reck30c714c2010-12-16 17:30:34 -08001845 * Get the favicon of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001846 */
1847 Bitmap getFavicon() {
John Reck1cf4b792011-07-26 10:22:22 -07001848 if (mCurrentState.mFavicon != null) {
1849 return mCurrentState.mFavicon;
1850 }
1851 return getDefaultFavicon(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001852 }
1853
John Recke969cc52010-12-21 17:24:43 -08001854 public boolean isBookmarkedSite() {
1855 return mCurrentState.mIsBookmarkedSite;
1856 }
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001857
Grace Kloba22ac16e2009-10-07 18:00:23 -07001858 /**
1859 * Return the tab's error console. Creates the console if createIfNEcessary
1860 * is true and we haven't already created the console.
1861 * @param createIfNecessary Flag to indicate if the console should be
1862 * created if it has not been already.
1863 * @return The tab's error console, or null if one has not been created and
1864 * createIfNecessary is false.
1865 */
1866 ErrorConsoleView getErrorConsole(boolean createIfNecessary) {
1867 if (createIfNecessary && mErrorConsole == null) {
Michael Kolb14612442011-06-24 13:06:29 -07001868 mErrorConsole = new ErrorConsoleView(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001869 mErrorConsole.setWebView(mMainView);
1870 }
1871 return mErrorConsole;
1872 }
1873
John Reck30c714c2010-12-16 17:30:34 -08001874 private void setLockIconType(LockIcon icon) {
1875 mCurrentState.mLockIcon = icon;
1876 mWebViewController.onUpdatedLockIcon(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001877 }
1878
1879 /**
1880 * @return The tab's lock icon type.
1881 */
John Reck30c714c2010-12-16 17:30:34 -08001882 LockIcon getLockIconType() {
1883 return mCurrentState.mLockIcon;
1884 }
1885
1886 int getLoadProgress() {
1887 if (mInPageLoad) {
1888 return mPageLoadProgress;
1889 }
1890 return 100;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001891 }
1892
1893 /**
1894 * @return TRUE if onPageStarted is called while onPageFinished is not
1895 * called yet.
1896 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001897 boolean inPageLoad() {
1898 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001899 }
1900
1901 // force mInLoad to be false. This should only be called before closing the
1902 // tab to ensure BrowserActivity's pauseWebViewTimers() is called correctly.
Michael Kolb8233fac2010-10-26 16:08:53 -07001903 void clearInPageLoad() {
1904 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001905 }
1906
Grace Kloba22ac16e2009-10-07 18:00:23 -07001907 /**
John Reck1cf4b792011-07-26 10:22:22 -07001908 * @return The Bundle with the tab's state if it can be saved, otherwise null
Grace Kloba22ac16e2009-10-07 18:00:23 -07001909 */
John Reck1cf4b792011-07-26 10:22:22 -07001910 public Bundle saveState() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001911 // If the WebView is null it means we ran low on memory and we already
1912 // stored the saved state in mSavedState.
1913 if (mMainView == null) {
John Reck1cf4b792011-07-26 10:22:22 -07001914 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001915 }
John Reck24f18262011-06-17 14:47:20 -07001916 // If the tab is the homepage or has no URL, don't save it
1917 String homepage = BrowserSettings.getInstance().getHomePage();
1918 if (TextUtils.equals(homepage, mCurrentState.mUrl)
1919 || TextUtils.isEmpty(mCurrentState.mUrl)) {
John Reck1cf4b792011-07-26 10:22:22 -07001920 return null;
John Reck24f18262011-06-17 14:47:20 -07001921 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001922
1923 mSavedState = new Bundle();
John Reck541f55a2011-06-07 16:34:43 -07001924 mMainView.saveState(mSavedState);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001925
Michael Kolbc831b632011-05-11 09:30:34 -07001926 mSavedState.putLong(ID, mId);
John Reck30c714c2010-12-16 17:30:34 -08001927 mSavedState.putString(CURRURL, mCurrentState.mUrl);
1928 mSavedState.putString(CURRTITLE, mCurrentState.mTitle);
John Reck1cf4b792011-07-26 10:22:22 -07001929 mSavedState.putBoolean(INCOGNITO, mMainView.isPrivateBrowsingEnabled());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001930 if (mAppId != null) {
1931 mSavedState.putString(APPID, mAppId);
1932 }
Michael Kolbe28b3472011-08-04 16:54:31 -07001933 mSavedState.putBoolean(CLOSEFLAG, mCloseOnBack);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001934 // Remember the parent tab so the relationship can be restored.
Michael Kolbc831b632011-05-11 09:30:34 -07001935 if (mParent != null) {
1936 mSavedState.putLong(PARENTTAB, mParent.mId);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001937 }
John Reckb0a86db2011-05-24 14:05:58 -07001938 mSavedState.putBoolean(USERAGENT,
1939 mSettings.hasDesktopUseragent(getWebView()));
John Reck1cf4b792011-07-26 10:22:22 -07001940 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001941 }
1942
1943 /*
1944 * Restore the state of the tab.
1945 */
John Reck1cf4b792011-07-26 10:22:22 -07001946 private void restoreState(Bundle b) {
1947 mSavedState = b;
1948 if (mSavedState == null) {
1949 return;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001950 }
1951 // Restore the internal state even if the WebView fails to restore.
1952 // This will maintain the app id, original url and close-on-exit values.
Michael Kolbc831b632011-05-11 09:30:34 -07001953 mId = b.getLong(ID);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001954 mAppId = b.getString(APPID);
Michael Kolbe28b3472011-08-04 16:54:31 -07001955 mCloseOnBack = b.getBoolean(CLOSEFLAG);
John Reckb0a86db2011-05-24 14:05:58 -07001956 if (b.getBoolean(USERAGENT)
1957 != mSettings.hasDesktopUseragent(getWebView())) {
1958 mSettings.toggleDesktopUseragent(getWebView());
1959 }
John Reck1cf4b792011-07-26 10:22:22 -07001960 String url = b.getString(CURRURL);
1961 String title = b.getString(CURRTITLE);
1962 boolean incognito = b.getBoolean(INCOGNITO);
1963 mCurrentState = new PageState(mContext, incognito, url, null);
1964 mCurrentState.mTitle = title;
1965 synchronized (Tab.this) {
1966 if (mCapture != null) {
1967 BackgroundHandler.execute(mLoadThumbnail);
1968 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001969 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001970 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04001971
Leon Scroggins1961ed22010-12-07 15:22:21 -05001972 public void updateBookmarkedStatus() {
John Recke969cc52010-12-21 17:24:43 -08001973 mDataController.queryBookmarkStatus(getUrl(), mIsBookmarkCallback);
Leon Scroggins1961ed22010-12-07 15:22:21 -05001974 }
1975
John Recke969cc52010-12-21 17:24:43 -08001976 private DataController.OnQueryUrlIsBookmark mIsBookmarkCallback
1977 = new DataController.OnQueryUrlIsBookmark() {
1978 @Override
1979 public void onQueryUrlIsBookmark(String url, boolean isBookmark) {
1980 if (mCurrentState.mUrl.equals(url)) {
1981 mCurrentState.mIsBookmarkedSite = isBookmark;
1982 mWebViewController.bookmarkedStatusHasChanged(Tab.this);
1983 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001984 }
John Recke969cc52010-12-21 17:24:43 -08001985 };
Michael Kolb1acef692011-03-08 14:12:06 -08001986
Michael Kolbeb95db42011-03-03 10:38:40 -08001987 public Bitmap getScreenshot() {
John Reck1cf4b792011-07-26 10:22:22 -07001988 synchronized (Tab.this) {
1989 return mCapture;
1990 }
Michael Kolbeb95db42011-03-03 10:38:40 -08001991 }
1992
John Reck541f55a2011-06-07 16:34:43 -07001993 public boolean isSnapshot() {
John Reck541f55a2011-06-07 16:34:43 -07001994 return false;
1995 }
1996
John Reckd8c74522011-06-14 08:45:00 -07001997 public ContentValues createSnapshotValues() {
1998 if (mMainView == null) return null;
John Reck8cc92352011-07-06 17:41:52 -07001999 ByteArrayOutputStream bos = new ByteArrayOutputStream();
2000 try {
2001 GZIPOutputStream stream = new GZIPOutputStream(bos);
2002 if (!mMainView.saveViewState(stream)) {
2003 return null;
2004 }
2005 stream.flush();
2006 stream.close();
2007 } catch (Exception e) {
2008 Log.w(LOGTAG, "Failed to save view state", e);
John Reck541f55a2011-06-07 16:34:43 -07002009 return null;
2010 }
John Reck8cc92352011-07-06 17:41:52 -07002011 byte[] data = bos.toByteArray();
John Reckd8c74522011-06-14 08:45:00 -07002012 ContentValues values = new ContentValues();
2013 values.put(Snapshots.TITLE, mCurrentState.mTitle);
2014 values.put(Snapshots.URL, mCurrentState.mUrl);
2015 values.put(Snapshots.VIEWSTATE, data);
2016 values.put(Snapshots.BACKGROUND, mMainView.getPageBackgroundColor());
John Reck8cc92352011-07-06 17:41:52 -07002017 values.put(Snapshots.DATE_CREATED, System.currentTimeMillis());
2018 values.put(Snapshots.FAVICON, compressBitmap(getFavicon()));
2019 Bitmap screenshot = Controller.createScreenshot(mMainView,
2020 Controller.getDesiredThumbnailWidth(mContext),
2021 Controller.getDesiredThumbnailHeight(mContext));
2022 values.put(Snapshots.THUMBNAIL, compressBitmap(screenshot));
John Reckd8c74522011-06-14 08:45:00 -07002023 return values;
John Reck541f55a2011-06-07 16:34:43 -07002024 }
2025
John Reck8cc92352011-07-06 17:41:52 -07002026 public byte[] compressBitmap(Bitmap bitmap) {
2027 if (bitmap == null) {
2028 return null;
2029 }
2030 ByteArrayOutputStream stream = new ByteArrayOutputStream();
2031 bitmap.compress(CompressFormat.PNG, 100, stream);
2032 return stream.toByteArray();
2033 }
2034
John Reck26b18322011-06-21 13:08:58 -07002035 public void loadUrl(String url, Map<String, String> headers) {
2036 if (mMainView != null) {
Michael Kolb14612442011-06-24 13:06:29 -07002037 mCurrentState = new PageState(mContext, false, url, null);
John Reck26b18322011-06-21 13:08:58 -07002038 mWebViewController.onPageStarted(this, mMainView, null);
2039 mMainView.loadUrl(url, headers);
2040 }
2041 }
2042
Michael Kolb9ef259a2011-07-12 15:33:08 -07002043 protected void capture() {
2044 if (mMainView == null || mCapture == null) return;
2045 Canvas c = new Canvas(mCapture);
2046 final int left = mMainView.getScrollX();
2047 final int top = mMainView.getScrollY() + mMainView.getVisibleTitleHeight();
2048 c.translate(-left, -top);
2049 float scale = mCaptureWidth / (float) mMainView.getWidth();
2050 c.scale(scale, scale, left, top);
John Reck8ee633f2011-08-09 16:00:35 -07002051 if (mMainView instanceof BrowserWebView) {
2052 ((BrowserWebView)mMainView).drawContent(c);
2053 } else {
2054 mMainView.draw(c);
2055 }
Dianne Hackborn43cfe8a2011-08-02 16:59:35 -07002056 c.setBitmap(null);
John Reck8ee633f2011-08-09 16:00:35 -07002057 mHandler.removeMessages(MSG_CAPTURE);
John Reck1cf4b792011-07-26 10:22:22 -07002058 persistThumbnail();
John Reck8ee633f2011-08-09 16:00:35 -07002059 TabControl tc = mWebViewController.getTabControl();
2060 if (tc != null) {
2061 OnThumbnailUpdatedListener updateListener
2062 = tc.getOnThumbnailUpdatedListener();
2063 if (updateListener != null) {
2064 updateListener.onThumbnailUpdated(this);
2065 }
2066 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07002067 }
2068
2069 @Override
2070 public void onNewPicture(WebView view, Picture picture) {
2071 //update screenshot
John Reck1cf4b792011-07-26 10:22:22 -07002072 postCapture();
2073 }
2074
2075 private void postCapture() {
Michael Kolb9ef259a2011-07-12 15:33:08 -07002076 if (!mHandler.hasMessages(MSG_CAPTURE)) {
2077 mHandler.sendEmptyMessageDelayed(MSG_CAPTURE, CAPTURE_DELAY);
2078 }
2079 }
2080
John Reckef654f12011-07-12 16:42:08 -07002081 public boolean canGoBack() {
2082 return mMainView != null ? mMainView.canGoBack() : false;
2083 }
2084
2085 public boolean canGoForward() {
2086 return mMainView != null ? mMainView.canGoForward() : false;
2087 }
2088
2089 public void goBack() {
2090 if (mMainView != null) {
2091 mMainView.goBack();
2092 }
2093 }
2094
2095 public void goForward() {
2096 if (mMainView != null) {
2097 mMainView.goForward();
2098 }
2099 }
2100
Mathew Inwood1dd8e822011-08-03 14:34:29 +01002101 /**
2102 * Causes the tab back/forward stack to be cleared once, if the given URL is the next URL
2103 * to be added to the stack.
2104 *
2105 * This is used to ensure that preloaded URLs that are not subsequently seen by the user do
2106 * not appear in the back stack.
2107 */
2108 public void clearBackStackWhenItemAdded(String urlToMatch) {
2109 mClearHistoryMatchUrl = urlToMatch;
2110 }
2111
John Reck1cf4b792011-07-26 10:22:22 -07002112 protected void persistThumbnail() {
2113 BackgroundHandler.execute(mSaveThumbnail);
2114 }
2115
2116 protected void deleteThumbnail() {
2117 BackgroundHandler.execute(mDeleteThumbnail);
2118 }
2119
2120 private void updateCaptureFromBlob(byte[] blob) {
2121 synchronized (Tab.this) {
2122 if (mCapture == null) {
2123 return;
2124 }
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002125 ByteBuffer buffer = ByteBuffer.wrap(blob);
Michael Kolbd837a112011-08-09 14:00:18 -07002126 try {
2127 mCapture.copyPixelsFromBuffer(buffer);
2128 } catch (RuntimeException rex) {
2129 Log.e(LOGTAG, "Load capture has mismatched sizes; buffer: "
2130 + buffer.capacity() + " blob: " + blob.length
2131 + "capture: " + mCapture.getByteCount());
2132 throw rex;
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002133 }
John Reck1cf4b792011-07-26 10:22:22 -07002134 }
2135 }
2136
2137 private byte[] getCaptureBlob() {
2138 synchronized (Tab.this) {
2139 if (mCapture == null) {
2140 return null;
2141 }
2142 ByteBuffer buffer = ByteBuffer.allocate(mCapture.getByteCount());
2143 mCapture.copyPixelsToBuffer(buffer);
2144 return buffer.array();
2145 }
2146 }
2147
2148 private Runnable mSaveThumbnail = new Runnable() {
2149
2150 @Override
2151 public void run() {
2152 byte[] blob = getCaptureBlob();
2153 if (blob == null) {
2154 return;
2155 }
2156 ContentResolver cr = mContext.getContentResolver();
2157 ContentValues values = new ContentValues();
2158 values.put(Thumbnails._ID, mId);
2159 values.put(Thumbnails.THUMBNAIL, blob);
2160 cr.insert(Thumbnails.CONTENT_URI, values);
2161 }
2162 };
2163
2164 private Runnable mDeleteThumbnail = new Runnable() {
2165
2166 @Override
2167 public void run() {
2168 ContentResolver cr = mContext.getContentResolver();
2169 try {
2170 cr.delete(ContentUris.withAppendedId(Thumbnails.CONTENT_URI, mId),
2171 null, null);
2172 } catch (Throwable t) {}
2173 }
2174 };
2175
2176 private Runnable mLoadThumbnail = new Runnable() {
2177
2178 @Override
2179 public void run() {
2180 ContentResolver cr = mContext.getContentResolver();
2181 Cursor c = null;
2182 try {
2183 Uri uri = ContentUris.withAppendedId(Thumbnails.CONTENT_URI, mId);
2184 c = cr.query(uri, new String[] {Thumbnails._ID,
2185 Thumbnails.THUMBNAIL}, null, null, null);
2186 if (c.moveToFirst()) {
2187 byte[] data = c.getBlob(1);
2188 if (data != null && data.length > 0) {
2189 updateCaptureFromBlob(data);
2190 }
2191 }
2192 } finally {
2193 if (c != null) {
2194 c.close();
2195 }
2196 }
2197 }
2198 };
2199
Grace Kloba22ac16e2009-10-07 18:00:23 -07002200}