blob: c6808e0a2aec3d9d1410e68d30c194595c8ffa79 [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 Recke969cc52010-12-21 17:24:43 -0800194 Boolean mIsBookmarkedSite = false;
John Reck30c714c2010-12-16 17:30:34 -0800195
196 PageState(Context c, boolean incognito) {
197 if (incognito) {
John Reckdb22ec42011-06-29 11:31:24 -0700198 mOriginalUrl = mUrl = "browser:incognito";
John Reck30c714c2010-12-16 17:30:34 -0800199 mTitle = c.getString(R.string.new_incognito_tab);
John Reck30c714c2010-12-16 17:30:34 -0800200 } else {
John Reckdb22ec42011-06-29 11:31:24 -0700201 mOriginalUrl = mUrl = "";
John Reck30c714c2010-12-16 17:30:34 -0800202 mTitle = c.getString(R.string.new_tab);
John Reck30c714c2010-12-16 17:30:34 -0800203 }
John Reck1cf4b792011-07-26 10:22:22 -0700204 mFavicon = null;
John Reck30c714c2010-12-16 17:30:34 -0800205 mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
206 }
207
208 PageState(Context c, boolean incognito, String url, Bitmap favicon) {
John Reckdb22ec42011-06-29 11:31:24 -0700209 mOriginalUrl = mUrl = url;
John Reck30c714c2010-12-16 17:30:34 -0800210 mTitle = null;
211 if (URLUtil.isHttpsUrl(url)) {
212 mLockIcon = LockIcon.LOCK_ICON_SECURE;
213 } else {
214 mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
215 }
John Reck1cf4b792011-07-26 10:22:22 -0700216 mFavicon = favicon;
John Reck30c714c2010-12-16 17:30:34 -0800217 }
John Reck1cf4b792011-07-26 10:22:22 -0700218
Grace Kloba22ac16e2009-10-07 18:00:23 -0700219 }
220
John Reck30c714c2010-12-16 17:30:34 -0800221 // The current/loading page's state
John Reckd8c74522011-06-14 08:45:00 -0700222 protected PageState mCurrentState;
John Reck30c714c2010-12-16 17:30:34 -0800223
Grace Kloba22ac16e2009-10-07 18:00:23 -0700224 // Used for saving and restoring each Tab
Michael Kolbc831b632011-05-11 09:30:34 -0700225 static final String ID = "ID";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700226 static final String CURRURL = "currentUrl";
227 static final String CURRTITLE = "currentTitle";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700228 static final String PARENTTAB = "parentTab";
229 static final String APPID = "appid";
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700230 static final String INCOGNITO = "privateBrowsingEnabled";
John Reckb0a86db2011-05-24 14:05:58 -0700231 static final String USERAGENT = "useragent";
Michael Kolbe28b3472011-08-04 16:54:31 -0700232 static final String CLOSEFLAG = "closeOnBack";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700233
234 // -------------------------------------------------------------------------
235
Leon Scroggins58d56c62010-01-28 15:12:40 -0500236 /**
237 * Private information regarding the latest voice search. If the Tab is not
238 * in voice search mode, this will be null.
239 */
240 private VoiceSearchData mVoiceSearchData;
241 /**
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400242 * Remove voice search mode from this tab.
243 */
244 public void revertVoiceSearchMode() {
245 if (mVoiceSearchData != null) {
246 mVoiceSearchData = null;
247 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700248 mWebViewController.revertVoiceSearchMode(this);
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400249 }
250 }
251 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700252
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400253 /**
Leon Scroggins58d56c62010-01-28 15:12:40 -0500254 * Return whether the tab is in voice search mode.
255 */
256 public boolean isInVoiceSearchMode() {
257 return mVoiceSearchData != null;
258 }
259 /**
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400260 * Return true if the Tab is in voice search mode and the voice search
261 * Intent came with a String identifying that Google provided the Intent.
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500262 */
263 public boolean voiceSearchSourceIsGoogle() {
264 return mVoiceSearchData != null && mVoiceSearchData.mSourceIsGoogle;
265 }
266 /**
Leon Scroggins58d56c62010-01-28 15:12:40 -0500267 * Get the title to display for the current voice search page. If the Tab
268 * is not in voice search mode, return null.
269 */
270 public String getVoiceDisplayTitle() {
271 if (mVoiceSearchData == null) return null;
272 return mVoiceSearchData.mLastVoiceSearchTitle;
273 }
274 /**
275 * Get the latest array of voice search results, to be passed to the
276 * BrowserProvider. If the Tab is not in voice search mode, return null.
277 */
278 public ArrayList<String> getVoiceSearchResults() {
279 if (mVoiceSearchData == null) return null;
280 return mVoiceSearchData.mVoiceSearchResults;
281 }
282 /**
283 * Activate voice search mode.
284 * @param intent Intent which has the results to use, or an index into the
285 * results when reusing the old results.
286 */
287 /* package */ void activateVoiceSearchMode(Intent intent) {
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500288 int index = 0;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500289 ArrayList<String> results = intent.getStringArrayListExtra(
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -0500290 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_STRINGS);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500291 if (results != null) {
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500292 ArrayList<String> urls = intent.getStringArrayListExtra(
293 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_URLS);
294 ArrayList<String> htmls = intent.getStringArrayListExtra(
295 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_HTML);
296 ArrayList<String> baseUrls = intent.getStringArrayListExtra(
297 RecognizerResultsIntent
298 .EXTRA_VOICE_SEARCH_RESULT_HTML_BASE_URLS);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500299 // This tab is now entering voice search mode for the first time, or
300 // a new voice search was done.
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500301 int size = results.size();
302 if (urls == null || size != urls.size()) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500303 throw new AssertionError("improper extras passed in Intent");
304 }
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500305 if (htmls == null || htmls.size() != size || baseUrls == null ||
306 (baseUrls.size() != size && baseUrls.size() != 1)) {
307 // If either of these arrays are empty/incorrectly sized, ignore
308 // them.
309 htmls = null;
310 baseUrls = null;
311 }
312 mVoiceSearchData = new VoiceSearchData(results, urls, htmls,
313 baseUrls);
Leon Scroggins9df94972010-03-08 18:20:35 -0500314 mVoiceSearchData.mHeaders = intent.getParcelableArrayListExtra(
315 RecognizerResultsIntent
316 .EXTRA_VOICE_SEARCH_RESULT_HTTP_HEADERS);
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500317 mVoiceSearchData.mSourceIsGoogle = intent.getBooleanExtra(
318 VoiceSearchData.SOURCE_IS_GOOGLE, false);
Leon Scroggins2ee4a5a2010-03-15 16:56:57 -0400319 mVoiceSearchData.mVoiceSearchIntent = new Intent(intent);
Leon Scrogginse10dde52010-03-08 19:53:03 -0500320 }
321 String extraData = intent.getStringExtra(
322 SearchManager.EXTRA_DATA_KEY);
323 if (extraData != null) {
324 index = Integer.parseInt(extraData);
325 if (index >= mVoiceSearchData.mVoiceSearchResults.size()) {
326 throw new AssertionError("index must be less than "
327 + "size of mVoiceSearchResults");
328 }
329 if (mVoiceSearchData.mSourceIsGoogle) {
330 Intent logIntent = new Intent(
331 LoggingEvents.ACTION_LOG_EVENT);
332 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
333 LoggingEvents.VoiceSearch.N_BEST_CHOOSE);
334 logIntent.putExtra(
335 LoggingEvents.VoiceSearch.EXTRA_N_BEST_CHOOSE_INDEX,
336 index);
Michael Kolb14612442011-06-24 13:06:29 -0700337 mContext.sendBroadcast(logIntent);
Leon Scrogginse10dde52010-03-08 19:53:03 -0500338 }
339 if (mVoiceSearchData.mVoiceSearchIntent != null) {
Leon Scroggins2ee4a5a2010-03-15 16:56:57 -0400340 // Copy the Intent, so that each history item will have its own
341 // Intent, with different (or none) extra data.
342 Intent latest = new Intent(mVoiceSearchData.mVoiceSearchIntent);
343 latest.putExtra(SearchManager.EXTRA_DATA_KEY, extraData);
344 mVoiceSearchData.mVoiceSearchIntent = latest;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500345 }
346 }
347 mVoiceSearchData.mLastVoiceSearchTitle
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500348 = mVoiceSearchData.mVoiceSearchResults.get(index);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500349 if (mInForeground) {
Michael Kolb11d19782011-03-20 10:17:40 -0700350 mWebViewController.activateVoiceSearchMode(
351 mVoiceSearchData.mLastVoiceSearchTitle,
352 mVoiceSearchData.mVoiceSearchResults);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500353 }
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500354 if (mVoiceSearchData.mVoiceSearchHtmls != null) {
355 // When index was found it was already ensured that it was valid
356 String uriString = mVoiceSearchData.mVoiceSearchHtmls.get(index);
357 if (uriString != null) {
358 Uri dataUri = Uri.parse(uriString);
359 if (RecognizerResultsIntent.URI_SCHEME_INLINE.equals(
360 dataUri.getScheme())) {
361 // If there is only one base URL, use it. If there are
362 // more, there will be one for each index, so use the base
363 // URL corresponding to the index.
364 String baseUrl = mVoiceSearchData.mVoiceSearchBaseUrls.get(
365 mVoiceSearchData.mVoiceSearchBaseUrls.size() > 1 ?
366 index : 0);
367 mVoiceSearchData.mLastVoiceSearchUrl = baseUrl;
368 mMainView.loadDataWithBaseURL(baseUrl,
369 uriString.substring(RecognizerResultsIntent
370 .URI_SCHEME_INLINE.length() + 1), "text/html",
371 "utf-8", baseUrl);
372 return;
373 }
374 }
375 }
Leon Scroggins58d56c62010-01-28 15:12:40 -0500376 mVoiceSearchData.mLastVoiceSearchUrl
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500377 = mVoiceSearchData.mVoiceSearchUrls.get(index);
378 if (null == mVoiceSearchData.mLastVoiceSearchUrl) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700379 mVoiceSearchData.mLastVoiceSearchUrl = UrlUtils.smartUrlFilter(
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500380 mVoiceSearchData.mLastVoiceSearchTitle);
381 }
Leon Scroggins9df94972010-03-08 18:20:35 -0500382 Map<String, String> headers = null;
383 if (mVoiceSearchData.mHeaders != null) {
384 int bundleIndex = mVoiceSearchData.mHeaders.size() == 1 ? 0
385 : index;
386 Bundle bundle = mVoiceSearchData.mHeaders.get(bundleIndex);
387 if (bundle != null && !bundle.isEmpty()) {
388 Iterator<String> iter = bundle.keySet().iterator();
389 headers = new HashMap<String, String>();
390 while (iter.hasNext()) {
391 String key = iter.next();
392 headers.put(key, bundle.getString(key));
393 }
394 }
395 }
396 mMainView.loadUrl(mVoiceSearchData.mLastVoiceSearchUrl, headers);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500397 }
398 /* package */ static class VoiceSearchData {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500399 public VoiceSearchData(ArrayList<String> results,
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500400 ArrayList<String> urls, ArrayList<String> htmls,
401 ArrayList<String> baseUrls) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500402 mVoiceSearchResults = results;
403 mVoiceSearchUrls = urls;
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500404 mVoiceSearchHtmls = htmls;
405 mVoiceSearchBaseUrls = baseUrls;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500406 }
407 /*
408 * ArrayList of suggestions to be displayed when opening the
409 * SearchManager
410 */
411 public ArrayList<String> mVoiceSearchResults;
412 /*
413 * ArrayList of urls, associated with the suggestions in
414 * mVoiceSearchResults.
415 */
416 public ArrayList<String> mVoiceSearchUrls;
417 /*
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500418 * ArrayList holding content to load for each item in
419 * mVoiceSearchResults.
420 */
421 public ArrayList<String> mVoiceSearchHtmls;
422 /*
423 * ArrayList holding base urls for the items in mVoiceSearchResults.
424 * If non null, this will either have the same size as
425 * mVoiceSearchResults or have a size of 1, in which case all will use
426 * the same base url
427 */
428 public ArrayList<String> mVoiceSearchBaseUrls;
429 /*
Leon Scroggins58d56c62010-01-28 15:12:40 -0500430 * The last url provided by voice search. Used for comparison to see if
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500431 * we are going to a page by some method besides voice search.
Leon Scroggins58d56c62010-01-28 15:12:40 -0500432 */
433 public String mLastVoiceSearchUrl;
434 /**
435 * The last title used for voice search. Needed to update the title bar
436 * when switching tabs.
437 */
438 public String mLastVoiceSearchTitle;
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500439 /**
440 * Whether the Intent which turned on voice search mode contained the
441 * String signifying that Google was the source.
442 */
443 public boolean mSourceIsGoogle;
444 /**
Leon Scroggins9df94972010-03-08 18:20:35 -0500445 * List of headers to be passed into the WebView containing location
446 * information
447 */
448 public ArrayList<Bundle> mHeaders;
449 /**
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500450 * The Intent used to invoke voice search. Placed on the
451 * WebHistoryItem so that when coming back to a previous voice search
452 * page we can again activate voice search.
453 */
Leon Scrogginse10dde52010-03-08 19:53:03 -0500454 public Intent mVoiceSearchIntent;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500455 /**
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500456 * String used to identify Google as the source of voice search.
457 */
458 public static String SOURCE_IS_GOOGLE
459 = "android.speech.extras.SOURCE_IS_GOOGLE";
Leon Scroggins58d56c62010-01-28 15:12:40 -0500460 }
461
Grace Kloba22ac16e2009-10-07 18:00:23 -0700462 // Container class for the next error dialog that needs to be displayed
463 private class ErrorDialog {
464 public final int mTitle;
465 public final String mDescription;
466 public final int mError;
467 ErrorDialog(int title, String desc, int error) {
468 mTitle = title;
469 mDescription = desc;
470 mError = error;
471 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700472 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700473
474 private void processNextError() {
475 if (mQueuedErrors == null) {
476 return;
477 }
478 // The first one is currently displayed so just remove it.
479 mQueuedErrors.removeFirst();
480 if (mQueuedErrors.size() == 0) {
481 mQueuedErrors = null;
482 return;
483 }
484 showError(mQueuedErrors.getFirst());
485 }
486
487 private DialogInterface.OnDismissListener mDialogListener =
488 new DialogInterface.OnDismissListener() {
489 public void onDismiss(DialogInterface d) {
490 processNextError();
491 }
492 };
493 private LinkedList<ErrorDialog> mQueuedErrors;
494
495 private void queueError(int err, String desc) {
496 if (mQueuedErrors == null) {
497 mQueuedErrors = new LinkedList<ErrorDialog>();
498 }
499 for (ErrorDialog d : mQueuedErrors) {
500 if (d.mError == err) {
501 // Already saw a similar error, ignore the new one.
502 return;
503 }
504 }
505 ErrorDialog errDialog = new ErrorDialog(
506 err == WebViewClient.ERROR_FILE_NOT_FOUND ?
507 R.string.browserFrameFileErrorLabel :
508 R.string.browserFrameNetworkErrorLabel,
509 desc, err);
510 mQueuedErrors.addLast(errDialog);
511
512 // Show the dialog now if the queue was empty and it is in foreground
513 if (mQueuedErrors.size() == 1 && mInForeground) {
514 showError(errDialog);
515 }
516 }
517
518 private void showError(ErrorDialog errDialog) {
519 if (mInForeground) {
Michael Kolb14612442011-06-24 13:06:29 -0700520 AlertDialog d = new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700521 .setTitle(errDialog.mTitle)
522 .setMessage(errDialog.mDescription)
523 .setPositiveButton(R.string.ok, null)
524 .create();
525 d.setOnDismissListener(mDialogListener);
526 d.show();
527 }
528 }
529
530 // -------------------------------------------------------------------------
531 // WebViewClient implementation for the main WebView
532 // -------------------------------------------------------------------------
533
534 private final WebViewClient mWebViewClient = new WebViewClient() {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500535 private Message mDontResend;
536 private Message mResend;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700537 @Override
538 public void onPageStarted(WebView view, String url, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700539 mInPageLoad = true;
John Reck30c714c2010-12-16 17:30:34 -0800540 mPageLoadProgress = 0;
Michael Kolb14612442011-06-24 13:06:29 -0700541 mCurrentState = new PageState(mContext,
John Reck30c714c2010-12-16 17:30:34 -0800542 view.isPrivateBrowsingEnabled(), url, favicon);
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000543 mLoadStartTime = SystemClock.uptimeMillis();
Leon Scroggins58d56c62010-01-28 15:12:40 -0500544 if (mVoiceSearchData != null
545 && !url.equals(mVoiceSearchData.mLastVoiceSearchUrl)) {
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500546 if (mVoiceSearchData.mSourceIsGoogle) {
547 Intent i = new Intent(LoggingEvents.ACTION_LOG_EVENT);
548 i.putExtra(LoggingEvents.EXTRA_FLUSH, true);
Michael Kolb14612442011-06-24 13:06:29 -0700549 mContext.sendBroadcast(i);
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500550 }
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400551 revertVoiceSearchMode();
Leon Scroggins58d56c62010-01-28 15:12:40 -0500552 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700553
Grace Kloba22ac16e2009-10-07 18:00:23 -0700554
555 // If we start a touch icon load and then load a new page, we don't
556 // want to cancel the current touch icon loader. But, we do want to
557 // create a new one when the touch icon url is known.
558 if (mTouchIconLoader != null) {
559 mTouchIconLoader.mTab = null;
560 mTouchIconLoader = null;
561 }
562
563 // reset the error console
564 if (mErrorConsole != null) {
565 mErrorConsole.clearErrorMessages();
Michael Kolb8233fac2010-10-26 16:08:53 -0700566 if (mWebViewController.shouldShowErrorConsole()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700567 mErrorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
568 }
569 }
570
Patrick Scott92066772011-03-10 08:46:27 -0500571 // Cancel the auto-login process.
572 if (mDeviceAccountLogin != null) {
573 mDeviceAccountLogin.cancel();
574 mDeviceAccountLogin = null;
575 mWebViewController.hideAutoLogin(Tab.this);
576 }
577
Grace Kloba22ac16e2009-10-07 18:00:23 -0700578 // finally update the UI in the activity if it is in the foreground
John Reck324d4402011-01-11 16:56:42 -0800579 mWebViewController.onPageStarted(Tab.this, view, favicon);
Leon Scroggins4cd97792010-12-03 15:31:56 -0500580
John Recke969cc52010-12-21 17:24:43 -0800581 updateBookmarkedStatus();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700582 }
583
584 @Override
585 public void onPageFinished(WebView view, String url) {
John Recka1696282011-07-08 14:10:37 -0700586 if (!mInPageLoad) {
587 // In page navigation links (www.something.com#footer) will
588 // trigger an onPageFinished which we don't care about.
589 return;
590 }
John Reck5b691842010-11-29 11:21:13 -0800591 if (!isPrivateBrowsingEnabled()) {
592 LogTag.logPageFinishedLoading(
593 url, SystemClock.uptimeMillis() - mLoadStartTime);
594 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700595 mInPageLoad = false;
John Reck1cf4b792011-07-26 10:22:22 -0700596 syncCurrentState(view, url);
John Reck324d4402011-01-11 16:56:42 -0800597 mWebViewController.onPageFinished(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700598 }
599
600 // return true if want to hijack the url to let another app to handle it
601 @Override
602 public boolean shouldOverrideUrlLoading(WebView view, String url) {
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400603 if (voiceSearchSourceIsGoogle()) {
604 // This method is called when the user clicks on a link.
605 // VoiceSearchMode is turned off when the user leaves the
606 // Google results page, so at this point the user must be on
607 // that page. If the user clicked a link on that page, assume
608 // that the voice search was effective, and broadcast an Intent
609 // so a receiver can take note of that fact.
610 Intent logIntent = new Intent(LoggingEvents.ACTION_LOG_EVENT);
611 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
612 LoggingEvents.VoiceSearch.RESULT_CLICKED);
Michael Kolb14612442011-06-24 13:06:29 -0700613 mContext.sendBroadcast(logIntent);
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400614 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700615 if (mInForeground) {
Michael Kolb18eb3772010-12-10 14:29:51 -0800616 return mWebViewController.shouldOverrideUrlLoading(Tab.this,
617 view, url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700618 } else {
619 return false;
620 }
621 }
622
623 /**
624 * Updates the lock icon. This method is called when we discover another
625 * resource to be loaded for this page (for example, javascript). While
626 * we update the icon type, we do not update the lock icon itself until
627 * we are done loading, it is slightly more secure this way.
628 */
629 @Override
630 public void onLoadResource(WebView view, String url) {
631 if (url != null && url.length() > 0) {
632 // It is only if the page claims to be secure that we may have
633 // to update the lock:
John Reck30c714c2010-12-16 17:30:34 -0800634 if (mCurrentState.mLockIcon == LockIcon.LOCK_ICON_SECURE) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700635 // If NOT a 'safe' url, change the lock to mixed content!
636 if (!(URLUtil.isHttpsUrl(url) || URLUtil.isDataUrl(url)
637 || URLUtil.isAboutUrl(url))) {
John Reck30c714c2010-12-16 17:30:34 -0800638 mCurrentState.mLockIcon = LockIcon.LOCK_ICON_MIXED;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700639 }
640 }
641 }
642 }
643
644 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700645 * Show a dialog informing the user of the network error reported by
646 * WebCore if it is in the foreground.
647 */
648 @Override
649 public void onReceivedError(WebView view, int errorCode,
650 String description, String failingUrl) {
651 if (errorCode != WebViewClient.ERROR_HOST_LOOKUP &&
652 errorCode != WebViewClient.ERROR_CONNECT &&
653 errorCode != WebViewClient.ERROR_BAD_URL &&
654 errorCode != WebViewClient.ERROR_UNSUPPORTED_SCHEME &&
655 errorCode != WebViewClient.ERROR_FILE) {
656 queueError(errorCode, description);
657 }
Jeff Hamilton47654f42010-09-07 09:57:51 -0500658
659 // Don't log URLs when in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -0700660 if (!isPrivateBrowsingEnabled()) {
Jeff Hamilton47654f42010-09-07 09:57:51 -0500661 Log.e(LOGTAG, "onReceivedError " + errorCode + " " + failingUrl
662 + " " + description);
663 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700664 }
665
666 /**
667 * Check with the user if it is ok to resend POST data as the page they
668 * are trying to navigate to is the result of a POST.
669 */
670 @Override
671 public void onFormResubmission(WebView view, final Message dontResend,
672 final Message resend) {
673 if (!mInForeground) {
674 dontResend.sendToTarget();
675 return;
676 }
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500677 if (mDontResend != null) {
678 Log.w(LOGTAG, "onFormResubmission should not be called again "
679 + "while dialog is still up");
680 dontResend.sendToTarget();
681 return;
682 }
683 mDontResend = dontResend;
684 mResend = resend;
Michael Kolb14612442011-06-24 13:06:29 -0700685 new AlertDialog.Builder(mContext).setTitle(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700686 R.string.browserFrameFormResubmitLabel).setMessage(
687 R.string.browserFrameFormResubmitMessage)
688 .setPositiveButton(R.string.ok,
689 new DialogInterface.OnClickListener() {
690 public void onClick(DialogInterface dialog,
691 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500692 if (mResend != null) {
693 mResend.sendToTarget();
694 mResend = null;
695 mDontResend = null;
696 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700697 }
698 }).setNegativeButton(R.string.cancel,
699 new DialogInterface.OnClickListener() {
700 public void onClick(DialogInterface dialog,
701 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500702 if (mDontResend != null) {
703 mDontResend.sendToTarget();
704 mResend = null;
705 mDontResend = null;
706 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700707 }
708 }).setOnCancelListener(new OnCancelListener() {
709 public void onCancel(DialogInterface dialog) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500710 if (mDontResend != null) {
711 mDontResend.sendToTarget();
712 mResend = null;
713 mDontResend = null;
714 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700715 }
716 }).show();
717 }
718
719 /**
720 * Insert the url into the visited history database.
721 * @param url The url to be inserted.
722 * @param isReload True if this url is being reloaded.
723 * FIXME: Not sure what to do when reloading the page.
724 */
725 @Override
726 public void doUpdateVisitedHistory(WebView view, String url,
727 boolean isReload) {
John Reck324d4402011-01-11 16:56:42 -0800728 mWebViewController.doUpdateVisitedHistory(Tab.this, isReload);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700729 }
730
731 /**
732 * Displays SSL error(s) dialog to the user.
733 */
734 @Override
735 public void onReceivedSslError(final WebView view,
736 final SslErrorHandler handler, final SslError error) {
737 if (!mInForeground) {
738 handler.cancel();
John Reck30c714c2010-12-16 17:30:34 -0800739 setLockIconType(LockIcon.LOCK_ICON_UNSECURE);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700740 return;
741 }
John Reck35e9dd62011-04-25 09:01:54 -0700742 if (mSettings.showSecurityWarnings()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700743 final LayoutInflater factory =
Michael Kolb14612442011-06-24 13:06:29 -0700744 LayoutInflater.from(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700745 final View warningsView =
746 factory.inflate(R.layout.ssl_warnings, null);
747 final LinearLayout placeholder =
748 (LinearLayout)warningsView.findViewById(R.id.placeholder);
749
750 if (error.hasError(SslError.SSL_UNTRUSTED)) {
751 LinearLayout ll = (LinearLayout)factory
752 .inflate(R.layout.ssl_warning, null);
753 ((TextView)ll.findViewById(R.id.warning))
754 .setText(R.string.ssl_untrusted);
755 placeholder.addView(ll);
756 }
757
758 if (error.hasError(SslError.SSL_IDMISMATCH)) {
759 LinearLayout ll = (LinearLayout)factory
760 .inflate(R.layout.ssl_warning, null);
761 ((TextView)ll.findViewById(R.id.warning))
762 .setText(R.string.ssl_mismatch);
763 placeholder.addView(ll);
764 }
765
766 if (error.hasError(SslError.SSL_EXPIRED)) {
767 LinearLayout ll = (LinearLayout)factory
768 .inflate(R.layout.ssl_warning, null);
769 ((TextView)ll.findViewById(R.id.warning))
770 .setText(R.string.ssl_expired);
771 placeholder.addView(ll);
772 }
773
774 if (error.hasError(SslError.SSL_NOTYETVALID)) {
775 LinearLayout ll = (LinearLayout)factory
776 .inflate(R.layout.ssl_warning, null);
777 ((TextView)ll.findViewById(R.id.warning))
778 .setText(R.string.ssl_not_yet_valid);
779 placeholder.addView(ll);
780 }
781
Michael Kolb14612442011-06-24 13:06:29 -0700782 new AlertDialog.Builder(mContext).setTitle(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700783 R.string.security_warning).setIcon(
784 android.R.drawable.ic_dialog_alert).setView(
785 warningsView).setPositiveButton(R.string.ssl_continue,
786 new DialogInterface.OnClickListener() {
787 public void onClick(DialogInterface dialog,
788 int whichButton) {
789 handler.proceed();
790 }
791 }).setNeutralButton(R.string.view_certificate,
792 new DialogInterface.OnClickListener() {
793 public void onClick(DialogInterface dialog,
794 int whichButton) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700795 mWebViewController.showSslCertificateOnError(view,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700796 handler, error);
797 }
Ben Murdocha49b8292010-11-16 11:56:04 +0000798 }).setNegativeButton(R.string.ssl_go_back,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700799 new DialogInterface.OnClickListener() {
800 public void onClick(DialogInterface dialog,
801 int whichButton) {
John Reck30c714c2010-12-16 17:30:34 -0800802 dialog.cancel();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700803 }
804 }).setOnCancelListener(
805 new DialogInterface.OnCancelListener() {
806 public void onCancel(DialogInterface dialog) {
807 handler.cancel();
John Reck30c714c2010-12-16 17:30:34 -0800808 setLockIconType(LockIcon.LOCK_ICON_UNSECURE);
809 mWebViewController.onUserCanceledSsl(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700810 }
811 }).show();
812 } else {
813 handler.proceed();
814 }
815 }
816
817 /**
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700818 * Displays client certificate request to the user.
819 */
820 @Override
821 public void onReceivedClientCertRequest(final WebView view,
822 final ClientCertRequestHandler handler, final String host_and_port) {
823 if (!mInForeground) {
824 handler.ignore();
825 return;
826 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700827 int colon = host_and_port.lastIndexOf(':');
828 String host;
829 int port;
830 if (colon == -1) {
831 host = host_and_port;
832 port = -1;
833 } else {
834 String portString = host_and_port.substring(colon + 1);
835 try {
836 port = Integer.parseInt(portString);
837 host = host_and_port.substring(0, colon);
838 } catch (NumberFormatException e) {
839 host = host_and_port;
840 port = -1;
841 }
842 }
Michael Kolb14612442011-06-24 13:06:29 -0700843 KeyChain.choosePrivateKeyAlias(
844 mWebViewController.getActivity(), new KeyChainAliasCallback() {
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700845 @Override public void alias(String alias) {
846 if (alias == null) {
847 handler.cancel();
848 return;
849 }
Michael Kolb14612442011-06-24 13:06:29 -0700850 new KeyChainLookup(mContext, handler, alias).execute();
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700851 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700852 }, null, null, host, port, null);
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700853 }
854
855 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700856 * Handles an HTTP authentication request.
857 *
858 * @param handler The authentication handler
859 * @param host The host
860 * @param realm The realm
861 */
862 @Override
863 public void onReceivedHttpAuthRequest(WebView view,
864 final HttpAuthHandler handler, final String host,
865 final String realm) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700866 mWebViewController.onReceivedHttpAuthRequest(Tab.this, view, handler, host, realm);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700867 }
868
869 @Override
John Reck438bf462011-01-12 18:11:46 -0800870 public WebResourceResponse shouldInterceptRequest(WebView view,
871 String url) {
872 WebResourceResponse res = HomeProvider.shouldInterceptRequest(
Michael Kolb14612442011-06-24 13:06:29 -0700873 mContext, url);
John Reck438bf462011-01-12 18:11:46 -0800874 return res;
875 }
876
877 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700878 public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
879 if (!mInForeground) {
880 return false;
881 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700882 return mWebViewController.shouldOverrideKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700883 }
884
885 @Override
886 public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700887 if (!mInForeground) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700888 return;
889 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700890 mWebViewController.onUnhandledKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700891 }
Patrick Scott92066772011-03-10 08:46:27 -0500892
893 @Override
894 public void onReceivedLoginRequest(WebView view, String realm,
895 String account, String args) {
Michael Kolb14612442011-06-24 13:06:29 -0700896 new DeviceAccountLogin(mWebViewController.getActivity(), view, Tab.this, mWebViewController)
Patrick Scott92066772011-03-10 08:46:27 -0500897 .handleLogin(realm, account, args);
898 }
899
Grace Kloba22ac16e2009-10-07 18:00:23 -0700900 };
901
John Reck1cf4b792011-07-26 10:22:22 -0700902 private void syncCurrentState(WebView view, String url) {
903 // Sync state (in case of stop/timeout)
904 mCurrentState.mUrl = view.getUrl();
905 if (mCurrentState.mUrl == null) {
906 mCurrentState.mUrl = url != null ? url : "";
907 }
908 mCurrentState.mOriginalUrl = view.getOriginalUrl();
909 mCurrentState.mTitle = view.getTitle();
910 mCurrentState.mFavicon = view.getFavicon();
911 if (!URLUtil.isHttpsUrl(mCurrentState.mUrl)) {
912 // In case we stop when loading an HTTPS page from an HTTP page
913 // but before a provisional load occurred
914 mCurrentState.mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
915 }
916 }
917
Patrick Scott92066772011-03-10 08:46:27 -0500918 // Called by DeviceAccountLogin when the Tab needs to have the auto-login UI
919 // displayed.
920 void setDeviceAccountLogin(DeviceAccountLogin login) {
921 mDeviceAccountLogin = login;
922 }
923
924 // Returns non-null if the title bar should display the auto-login UI.
925 DeviceAccountLogin getDeviceAccountLogin() {
926 return mDeviceAccountLogin;
927 }
928
Grace Kloba22ac16e2009-10-07 18:00:23 -0700929 // -------------------------------------------------------------------------
930 // WebChromeClient implementation for the main WebView
931 // -------------------------------------------------------------------------
932
933 private final WebChromeClient mWebChromeClient = new WebChromeClient() {
934 // Helper method to create a new tab or sub window.
935 private void createWindow(final boolean dialog, final Message msg) {
936 WebView.WebViewTransport transport =
937 (WebView.WebViewTransport) msg.obj;
938 if (dialog) {
939 createSubWindow();
Michael Kolb8233fac2010-10-26 16:08:53 -0700940 mWebViewController.attachSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700941 transport.setWebView(mSubView);
942 } else {
Michael Kolb7bcafde2011-05-09 13:55:59 -0700943 final Tab newTab = mWebViewController.openTab(null,
John Reck5949c662011-05-27 09:52:29 -0700944 Tab.this, true, true);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700945 transport.setWebView(newTab.getWebView());
946 }
947 msg.sendToTarget();
948 }
949
950 @Override
951 public boolean onCreateWindow(WebView view, final boolean dialog,
952 final boolean userGesture, final Message resultMsg) {
953 // only allow new window or sub window for the foreground case
954 if (!mInForeground) {
955 return false;
956 }
957 // Short-circuit if we can't create any more tabs or sub windows.
958 if (dialog && mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -0700959 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700960 .setTitle(R.string.too_many_subwindows_dialog_title)
961 .setIcon(android.R.drawable.ic_dialog_alert)
962 .setMessage(R.string.too_many_subwindows_dialog_message)
963 .setPositiveButton(R.string.ok, null)
964 .show();
965 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700966 } else if (!mWebViewController.getTabControl().canCreateNewTab()) {
Michael Kolb14612442011-06-24 13:06:29 -0700967 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700968 .setTitle(R.string.too_many_windows_dialog_title)
969 .setIcon(android.R.drawable.ic_dialog_alert)
970 .setMessage(R.string.too_many_windows_dialog_message)
971 .setPositiveButton(R.string.ok, null)
972 .show();
973 return false;
974 }
975
976 // Short-circuit if this was a user gesture.
977 if (userGesture) {
978 createWindow(dialog, resultMsg);
979 return true;
980 }
981
982 // Allow the popup and create the appropriate window.
983 final AlertDialog.OnClickListener allowListener =
984 new AlertDialog.OnClickListener() {
985 public void onClick(DialogInterface d,
986 int which) {
987 createWindow(dialog, resultMsg);
988 }
989 };
990
991 // Block the popup by returning a null WebView.
992 final AlertDialog.OnClickListener blockListener =
993 new AlertDialog.OnClickListener() {
994 public void onClick(DialogInterface d, int which) {
995 resultMsg.sendToTarget();
996 }
997 };
998
999 // Build a confirmation dialog to display to the user.
1000 final AlertDialog d =
Michael Kolb14612442011-06-24 13:06:29 -07001001 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -07001002 .setTitle(R.string.attention)
1003 .setIcon(android.R.drawable.ic_dialog_alert)
1004 .setMessage(R.string.popup_window_attempt)
1005 .setPositiveButton(R.string.allow, allowListener)
1006 .setNegativeButton(R.string.block, blockListener)
1007 .setCancelable(false)
1008 .create();
1009
1010 // Show the confirmation dialog.
1011 d.show();
1012 return true;
1013 }
1014
1015 @Override
Patrick Scotteb5061b2009-11-18 15:00:30 -05001016 public void onRequestFocus(WebView view) {
1017 if (!mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -07001018 mWebViewController.switchToTab(Tab.this);
Patrick Scotteb5061b2009-11-18 15:00:30 -05001019 }
1020 }
1021
1022 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -07001023 public void onCloseWindow(WebView window) {
Michael Kolbc831b632011-05-11 09:30:34 -07001024 if (mParent != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001025 // JavaScript can only close popup window.
1026 if (mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -07001027 mWebViewController.switchToTab(mParent);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001028 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001029 mWebViewController.closeTab(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001030 }
1031 }
1032
1033 @Override
1034 public void onProgressChanged(WebView view, int newProgress) {
John Reck30c714c2010-12-16 17:30:34 -08001035 mPageLoadProgress = newProgress;
1036 mWebViewController.onProgressChanged(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001037 }
1038
1039 @Override
Leon Scroggins21d9b902010-03-11 09:33:11 -05001040 public void onReceivedTitle(WebView view, final String title) {
John Reck30c714c2010-12-16 17:30:34 -08001041 mCurrentState.mTitle = title;
Michael Kolb8233fac2010-10-26 16:08:53 -07001042 mWebViewController.onReceivedTitle(Tab.this, title);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001043 }
1044
1045 @Override
1046 public void onReceivedIcon(WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -08001047 mCurrentState.mFavicon = icon;
Michael Kolb8233fac2010-10-26 16:08:53 -07001048 mWebViewController.onFavicon(Tab.this, view, icon);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001049 }
1050
1051 @Override
1052 public void onReceivedTouchIconUrl(WebView view, String url,
1053 boolean precomposed) {
Michael Kolb14612442011-06-24 13:06:29 -07001054 final ContentResolver cr = mContext.getContentResolver();
Leon Scrogginsc8393d92010-04-23 14:58:16 -04001055 // Let precomposed icons take precedence over non-composed
1056 // icons.
1057 if (precomposed && mTouchIconLoader != null) {
1058 mTouchIconLoader.cancel(false);
1059 mTouchIconLoader = null;
1060 }
1061 // Have only one async task at a time.
1062 if (mTouchIconLoader == null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001063 mTouchIconLoader = new DownloadTouchIcon(Tab.this,
Michael Kolb14612442011-06-24 13:06:29 -07001064 mContext, cr, view);
Leon Scrogginsc8393d92010-04-23 14:58:16 -04001065 mTouchIconLoader.execute(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001066 }
1067 }
1068
1069 @Override
1070 public void onShowCustomView(View view,
1071 WebChromeClient.CustomViewCallback callback) {
Michael Kolb14612442011-06-24 13:06:29 -07001072 Activity activity = mWebViewController.getActivity();
1073 if (activity != null) {
1074 onShowCustomView(view, activity.getRequestedOrientation(), callback);
1075 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001076 }
1077
1078 @Override
1079 public void onShowCustomView(View view, int requestedOrientation,
1080 WebChromeClient.CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001081 if (mInForeground) mWebViewController.showCustomView(Tab.this, view,
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001082 requestedOrientation, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001083 }
1084
1085 @Override
1086 public void onHideCustomView() {
Michael Kolb8233fac2010-10-26 16:08:53 -07001087 if (mInForeground) mWebViewController.hideCustomView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001088 }
1089
1090 /**
1091 * The origin has exceeded its database quota.
1092 * @param url the URL that exceeded the quota
1093 * @param databaseIdentifier the identifier of the database on which the
1094 * transaction that caused the quota overflow was run
1095 * @param currentQuota the current quota for the origin.
1096 * @param estimatedSize the estimated size of the database.
1097 * @param totalUsedQuota is the sum of all origins' quota.
1098 * @param quotaUpdater The callback to run when a decision to allow or
1099 * deny quota has been made. Don't forget to call this!
1100 */
1101 @Override
1102 public void onExceededDatabaseQuota(String url,
1103 String databaseIdentifier, long currentQuota, long estimatedSize,
1104 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -07001105 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001106 .onExceededDatabaseQuota(url, databaseIdentifier,
1107 currentQuota, estimatedSize, totalUsedQuota,
1108 quotaUpdater);
1109 }
1110
1111 /**
1112 * The Application Cache has exceeded its max size.
1113 * @param spaceNeeded is the amount of disk space that would be needed
1114 * in order for the last appcache operation to succeed.
1115 * @param totalUsedQuota is the sum of all origins' quota.
1116 * @param quotaUpdater A callback to inform the WebCore thread that a
1117 * new app cache size is available. This callback must always
1118 * be executed at some point to ensure that the sleeping
1119 * WebCore thread is woken up.
1120 */
1121 @Override
1122 public void onReachedMaxAppCacheSize(long spaceNeeded,
1123 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -07001124 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001125 .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota,
1126 quotaUpdater);
1127 }
1128
1129 /**
1130 * Instructs the browser to show a prompt to ask the user to set the
1131 * Geolocation permission state for the specified origin.
1132 * @param origin The origin for which Geolocation permissions are
1133 * requested.
1134 * @param callback The callback to call once the user has set the
1135 * Geolocation permission state.
1136 */
1137 @Override
1138 public void onGeolocationPermissionsShowPrompt(String origin,
1139 GeolocationPermissions.Callback callback) {
1140 if (mInForeground) {
Grace Kloba50c241e2010-04-20 11:07:50 -07001141 getGeolocationPermissionsPrompt().show(origin, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001142 }
1143 }
1144
1145 /**
1146 * Instructs the browser to hide the Geolocation permissions prompt.
1147 */
1148 @Override
1149 public void onGeolocationPermissionsHidePrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001150 if (mInForeground && mGeolocationPermissionsPrompt != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001151 mGeolocationPermissionsPrompt.hide();
1152 }
1153 }
1154
Ben Murdoch65acc352009-11-19 18:16:04 +00001155 /* Adds a JavaScript error message to the system log and if the JS
1156 * console is enabled in the about:debug options, to that console
1157 * also.
Ben Murdochc42addf2010-01-28 15:19:59 +00001158 * @param consoleMessage the message object.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001159 */
1160 @Override
Ben Murdochc42addf2010-01-28 15:19:59 +00001161 public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001162 if (mInForeground) {
1163 // call getErrorConsole(true) so it will create one if needed
1164 ErrorConsoleView errorConsole = getErrorConsole(true);
Ben Murdochc42addf2010-01-28 15:19:59 +00001165 errorConsole.addErrorMessage(consoleMessage);
Michael Kolb8233fac2010-10-26 16:08:53 -07001166 if (mWebViewController.shouldShowErrorConsole()
1167 && errorConsole.getShowState() !=
1168 ErrorConsoleView.SHOW_MAXIMIZED) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001169 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
1170 }
1171 }
Ben Murdochc42addf2010-01-28 15:19:59 +00001172
Jeff Hamilton47654f42010-09-07 09:57:51 -05001173 // Don't log console messages in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001174 if (isPrivateBrowsingEnabled()) return true;
Jeff Hamilton47654f42010-09-07 09:57:51 -05001175
Ben Murdochc42addf2010-01-28 15:19:59 +00001176 String message = "Console: " + consoleMessage.message() + " "
1177 + consoleMessage.sourceId() + ":"
1178 + consoleMessage.lineNumber();
1179
1180 switch (consoleMessage.messageLevel()) {
1181 case TIP:
1182 Log.v(CONSOLE_LOGTAG, message);
1183 break;
1184 case LOG:
1185 Log.i(CONSOLE_LOGTAG, message);
1186 break;
1187 case WARNING:
1188 Log.w(CONSOLE_LOGTAG, message);
1189 break;
1190 case ERROR:
1191 Log.e(CONSOLE_LOGTAG, message);
1192 break;
1193 case DEBUG:
1194 Log.d(CONSOLE_LOGTAG, message);
1195 break;
1196 }
1197
1198 return true;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001199 }
1200
1201 /**
1202 * Ask the browser for an icon to represent a <video> element.
1203 * This icon will be used if the Web page did not specify a poster attribute.
1204 * @return Bitmap The icon or null if no such icon is available.
1205 */
1206 @Override
1207 public Bitmap getDefaultVideoPoster() {
1208 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001209 return mWebViewController.getDefaultVideoPoster();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001210 }
1211 return null;
1212 }
1213
1214 /**
1215 * Ask the host application for a custom progress view to show while
1216 * a <video> is loading.
1217 * @return View The progress view.
1218 */
1219 @Override
1220 public View getVideoLoadingProgressView() {
1221 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001222 return mWebViewController.getVideoLoadingProgressView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001223 }
1224 return null;
1225 }
1226
1227 @Override
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01001228 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001229 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001230 mWebViewController.openFileChooser(uploadMsg, acceptType);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001231 } else {
1232 uploadMsg.onReceiveValue(null);
1233 }
1234 }
1235
1236 /**
1237 * Deliver a list of already-visited URLs
1238 */
1239 @Override
1240 public void getVisitedHistory(final ValueCallback<String[]> callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001241 mWebViewController.getVisitedHistory(callback);
1242 }
Ben Murdoch8029a772010-11-16 11:58:21 +00001243
1244 @Override
1245 public void setupAutoFill(Message message) {
1246 // Prompt the user to set up their profile.
1247 final Message msg = message;
Michael Kolb14612442011-06-24 13:06:29 -07001248 AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
1249 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
Ben Murdoch1d676b62011-01-17 12:54:24 +00001250 Context.LAYOUT_INFLATER_SERVICE);
1251 final View layout = inflater.inflate(R.layout.setup_autofill_dialog, null);
1252
1253 builder.setView(layout)
Ben Murdochb7e6f942011-07-08 13:00:21 +01001254 .setTitle(R.string.autofill_setup_dialog_title)
Ben Murdoch1d676b62011-01-17 12:54:24 +00001255 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1256 @Override
1257 public void onClick(DialogInterface dialog, int id) {
1258 CheckBox disableAutoFill = (CheckBox) layout.findViewById(
1259 R.id.setup_autofill_dialog_disable_autofill);
1260
1261 if (disableAutoFill.isChecked()) {
1262 // Disable autofill and show a toast with how to turn it on again.
John Reck35e9dd62011-04-25 09:01:54 -07001263 mSettings.setAutofillEnabled(false);
Michael Kolb14612442011-06-24 13:06:29 -07001264 Toast.makeText(mContext,
Ben Murdoch1d676b62011-01-17 12:54:24 +00001265 R.string.autofill_setup_dialog_negative_toast,
1266 Toast.LENGTH_LONG).show();
1267 } else {
1268 // Take user to the AutoFill profile editor. When they return,
1269 // we will send the message that we pass here which will trigger
1270 // the form to get filled out with their new profile.
1271 mWebViewController.setupAutoFill(msg);
1272 }
1273 }
1274 })
1275 .setNegativeButton(R.string.cancel, null)
1276 .show();
Ben Murdoch8029a772010-11-16 11:58:21 +00001277 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001278 };
1279
1280 // -------------------------------------------------------------------------
1281 // WebViewClient implementation for the sub window
1282 // -------------------------------------------------------------------------
1283
1284 // Subclass of WebViewClient used in subwindows to notify the main
1285 // WebViewClient of certain WebView activities.
1286 private static class SubWindowClient extends WebViewClient {
1287 // The main WebViewClient.
1288 private final WebViewClient mClient;
Michael Kolb8233fac2010-10-26 16:08:53 -07001289 private final WebViewController mController;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001290
Michael Kolb8233fac2010-10-26 16:08:53 -07001291 SubWindowClient(WebViewClient client, WebViewController controller) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001292 mClient = client;
Michael Kolb8233fac2010-10-26 16:08:53 -07001293 mController = controller;
Leon Scroggins III211ba542010-04-19 13:21:13 -04001294 }
1295 @Override
1296 public void onPageStarted(WebView view, String url, Bitmap favicon) {
1297 // Unlike the others, do not call mClient's version, which would
1298 // change the progress bar. However, we do want to remove the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001299 // find or select dialog.
Michael Kolb8233fac2010-10-26 16:08:53 -07001300 mController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001301 }
1302 @Override
1303 public void doUpdateVisitedHistory(WebView view, String url,
1304 boolean isReload) {
1305 mClient.doUpdateVisitedHistory(view, url, isReload);
1306 }
1307 @Override
1308 public boolean shouldOverrideUrlLoading(WebView view, String url) {
1309 return mClient.shouldOverrideUrlLoading(view, url);
1310 }
1311 @Override
1312 public void onReceivedSslError(WebView view, SslErrorHandler handler,
1313 SslError error) {
1314 mClient.onReceivedSslError(view, handler, error);
1315 }
1316 @Override
Brian Carlstrom8862c1d2011-06-02 01:05:55 -07001317 public void onReceivedClientCertRequest(WebView view,
1318 ClientCertRequestHandler handler, String host_and_port) {
1319 mClient.onReceivedClientCertRequest(view, handler, host_and_port);
1320 }
1321 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -07001322 public void onReceivedHttpAuthRequest(WebView view,
1323 HttpAuthHandler handler, String host, String realm) {
1324 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
1325 }
1326 @Override
1327 public void onFormResubmission(WebView view, Message dontResend,
1328 Message resend) {
1329 mClient.onFormResubmission(view, dontResend, resend);
1330 }
1331 @Override
1332 public void onReceivedError(WebView view, int errorCode,
1333 String description, String failingUrl) {
1334 mClient.onReceivedError(view, errorCode, description, failingUrl);
1335 }
1336 @Override
1337 public boolean shouldOverrideKeyEvent(WebView view,
1338 android.view.KeyEvent event) {
1339 return mClient.shouldOverrideKeyEvent(view, event);
1340 }
1341 @Override
1342 public void onUnhandledKeyEvent(WebView view,
1343 android.view.KeyEvent event) {
1344 mClient.onUnhandledKeyEvent(view, event);
1345 }
1346 }
1347
1348 // -------------------------------------------------------------------------
1349 // WebChromeClient implementation for the sub window
1350 // -------------------------------------------------------------------------
1351
1352 private class SubWindowChromeClient extends WebChromeClient {
1353 // The main WebChromeClient.
1354 private final WebChromeClient mClient;
1355
1356 SubWindowChromeClient(WebChromeClient client) {
1357 mClient = client;
1358 }
1359 @Override
1360 public void onProgressChanged(WebView view, int newProgress) {
1361 mClient.onProgressChanged(view, newProgress);
1362 }
1363 @Override
1364 public boolean onCreateWindow(WebView view, boolean dialog,
1365 boolean userGesture, android.os.Message resultMsg) {
1366 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
1367 }
1368 @Override
1369 public void onCloseWindow(WebView window) {
1370 if (window != mSubView) {
1371 Log.e(LOGTAG, "Can't close the window");
1372 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001373 mWebViewController.dismissSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001374 }
1375 }
1376
1377 // -------------------------------------------------------------------------
1378
1379 // Construct a new tab
Michael Kolb7bcafde2011-05-09 13:55:59 -07001380 Tab(WebViewController wvcontroller, WebView w) {
John Reck1cf4b792011-07-26 10:22:22 -07001381 this(wvcontroller, w, null);
1382 }
1383
1384 Tab(WebViewController wvcontroller, Bundle state) {
1385 this(wvcontroller, null, state);
1386 }
1387
1388 Tab(WebViewController wvcontroller, WebView w, Bundle state) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001389 mWebViewController = wvcontroller;
Michael Kolb14612442011-06-24 13:06:29 -07001390 mContext = mWebViewController.getContext();
John Reck35e9dd62011-04-25 09:01:54 -07001391 mSettings = BrowserSettings.getInstance();
Michael Kolb14612442011-06-24 13:06:29 -07001392 mDataController = DataController.getInstance(mContext);
1393 mCurrentState = new PageState(mContext, w != null
John Reck847b5322011-04-14 17:02:18 -07001394 ? w.isPrivateBrowsingEnabled() : false);
Michael Kolb8233fac2010-10-26 16:08:53 -07001395 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001396 mInForeground = false;
1397
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001398 mDownloadListener = new DownloadListener() {
1399 public void onDownloadStart(String url, String userAgent,
1400 String contentDisposition, String mimetype,
1401 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001402 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001403 mimetype, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001404 }
1405 };
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001406 mWebBackForwardListClient = new WebBackForwardListClient() {
1407 @Override
1408 public void onNewHistoryItem(WebHistoryItem item) {
1409 if (isInVoiceSearchMode()) {
1410 item.setCustomData(mVoiceSearchData.mVoiceSearchIntent);
1411 }
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001412 if (mClearHistoryMatchUrl != null) {
1413 if (LOGD_ENABLED) {
1414 Log.d(LOGTAG, "onNewHistoryItem:\n\t" + item.getUrl() + "\n\t"
1415 + mClearHistoryMatchUrl);
1416 }
1417 if (TextUtils.equals(item.getOriginalUrl(), mClearHistoryMatchUrl)) {
1418 if (mMainView != null) {
1419 mMainView.clearHistory();
1420 }
1421 }
1422 mClearHistoryMatchUrl = null;
1423 }
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001424 }
1425 @Override
1426 public void onIndexChanged(WebHistoryItem item, int index) {
1427 Object data = item.getCustomData();
1428 if (data != null && data instanceof Intent) {
1429 activateVoiceSearchMode((Intent) data);
1430 }
1431 }
1432 };
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001433
John Reck1cf4b792011-07-26 10:22:22 -07001434 mCaptureWidth = mContext.getResources().getDimensionPixelSize(
1435 R.dimen.tab_thumbnail_width);
1436 mCaptureHeight = mContext.getResources().getDimensionPixelSize(
1437 R.dimen.tab_thumbnail_height);
1438 updateShouldCaptureThumbnails();
1439 restoreState(state);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001440 setWebView(w);
Michael Kolb9ef259a2011-07-12 15:33:08 -07001441 mHandler = new Handler() {
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001442 @Override
Michael Kolb9ef259a2011-07-12 15:33:08 -07001443 public void handleMessage(Message m) {
John Reck1cf4b792011-07-26 10:22:22 -07001444 switch (m.what) {
1445 case MSG_CAPTURE:
1446 capture();
1447 break;
1448 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001449 }
1450 };
John Reck1cf4b792011-07-26 10:22:22 -07001451 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001452
John Reck1cf4b792011-07-26 10:22:22 -07001453 public void updateShouldCaptureThumbnails() {
1454 if (mWebViewController.shouldCaptureThumbnails()) {
1455 synchronized (Tab.this) {
1456 if (mCapture == null) {
1457 mCapture = Bitmap.createBitmap(mCaptureWidth, mCaptureHeight,
1458 Bitmap.Config.RGB_565);
Michael Kolbc3af0672011-08-09 10:24:41 -07001459 mCapture.eraseColor(Color.WHITE);
John Reck1cf4b792011-07-26 10:22:22 -07001460 if (mInForeground) {
1461 postCapture();
1462 }
1463 }
1464 }
1465 } else {
1466 synchronized (Tab.this) {
1467 mCapture = null;
1468 deleteThumbnail();
1469 }
1470 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001471 }
1472
Michael Kolb14612442011-06-24 13:06:29 -07001473 public void setController(WebViewController ctl) {
1474 mWebViewController = ctl;
John Reck1cf4b792011-07-26 10:22:22 -07001475 updateShouldCaptureThumbnails();
Michael Kolb14612442011-06-24 13:06:29 -07001476 }
1477
Michael Kolbc831b632011-05-11 09:30:34 -07001478 public void setId(long id) {
1479 mId = id;
1480 }
1481
1482 public long getId() {
1483 return mId;
1484 }
1485
Grace Kloba22ac16e2009-10-07 18:00:23 -07001486 /**
1487 * Sets the WebView for this tab, correctly removing the old WebView from
1488 * the container view.
1489 */
1490 void setWebView(WebView w) {
1491 if (mMainView == w) {
1492 return;
1493 }
Michael Kolba713ec82010-11-29 17:27:06 -08001494
Grace Kloba22ac16e2009-10-07 18:00:23 -07001495 // If the WebView is changing, the page will be reloaded, so any ongoing
1496 // Geolocation permission requests are void.
Grace Kloba50c241e2010-04-20 11:07:50 -07001497 if (mGeolocationPermissionsPrompt != null) {
1498 mGeolocationPermissionsPrompt.hide();
1499 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001500
Michael Kolba713ec82010-11-29 17:27:06 -08001501 mWebViewController.onSetWebView(this, w);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001502
John Reck1cf4b792011-07-26 10:22:22 -07001503 if (mMainView != null) {
John Reck8ee633f2011-08-09 16:00:35 -07001504 mMainView.setPictureListener(null);
John Reck1cf4b792011-07-26 10:22:22 -07001505 if (w != null) {
1506 syncCurrentState(w, null);
1507 } else {
1508 mCurrentState = new PageState(mContext, false);
1509 }
1510 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001511 // set the new one
1512 mMainView = w;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001513 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001514 if (mMainView != null) {
1515 mMainView.setWebViewClient(mWebViewClient);
1516 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001517 // Attach DownloadManager so that downloads can start in an active
1518 // or a non-active window. This can happen when going to a site that
1519 // does a redirect after a period of time. The user could have
1520 // switched to another tab while waiting for the download to start.
1521 mMainView.setDownloadListener(mDownloadListener);
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001522 mMainView.setWebBackForwardListClient(mWebBackForwardListClient);
John Reck8ee633f2011-08-09 16:00:35 -07001523 TabControl tc = mWebViewController.getTabControl();
1524 if (tc != null && tc.getOnThumbnailUpdatedListener() != null) {
1525 mMainView.setPictureListener(this);
1526 }
John Reck1cf4b792011-07-26 10:22:22 -07001527 if (mSavedState != null) {
1528 mMainView.restoreState(mSavedState);
1529 mSavedState = null;
1530 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001531 }
1532 }
1533
1534 /**
1535 * Destroy the tab's main WebView and subWindow if any
1536 */
1537 void destroy() {
1538 if (mMainView != null) {
1539 dismissSubWindow();
John Reckef654f12011-07-12 16:42:08 -07001540 // Make sure the embedded title bar isn't still attached
1541 mMainView.setEmbeddedTitleBar(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001542 // save the WebView to call destroy() after detach it from the tab
1543 WebView webView = mMainView;
1544 setWebView(null);
1545 webView.destroy();
1546 }
1547 }
1548
1549 /**
1550 * Remove the tab from the parent
1551 */
1552 void removeFromTree() {
1553 // detach the children
Michael Kolbc831b632011-05-11 09:30:34 -07001554 if (mChildren != null) {
1555 for(Tab t : mChildren) {
1556 t.setParent(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001557 }
1558 }
1559 // remove itself from the parent list
Michael Kolbc831b632011-05-11 09:30:34 -07001560 if (mParent != null) {
1561 mParent.mChildren.remove(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001562 }
John Reck1cf4b792011-07-26 10:22:22 -07001563 deleteThumbnail();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001564 }
1565
1566 /**
1567 * Create a new subwindow unless a subwindow already exists.
1568 * @return True if a new subwindow was created. False if one already exists.
1569 */
1570 boolean createSubWindow() {
1571 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001572 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001573 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001574 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001575 mSubView.setWebChromeClient(new SubWindowChromeClient(
1576 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001577 // Set a different DownloadListener for the mSubView, since it will
1578 // just need to dismiss the mSubView, rather than close the Tab
1579 mSubView.setDownloadListener(new DownloadListener() {
1580 public void onDownloadStart(String url, String userAgent,
1581 String contentDisposition, String mimetype,
1582 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001583 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001584 contentDisposition, mimetype, contentLength);
1585 if (mSubView.copyBackForwardList().getSize() == 0) {
1586 // This subwindow was opened for the sole purpose of
1587 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001588 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001589 }
1590 }
1591 });
Michael Kolb14612442011-06-24 13:06:29 -07001592 mSubView.setOnCreateContextMenuListener(mWebViewController.getActivity());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001593 return true;
1594 }
1595 return false;
1596 }
1597
1598 /**
1599 * Dismiss the subWindow for the tab.
1600 */
1601 void dismissSubWindow() {
1602 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001603 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001604 mSubView.destroy();
1605 mSubView = null;
1606 mSubViewContainer = null;
1607 }
1608 }
1609
Grace Kloba22ac16e2009-10-07 18:00:23 -07001610
1611 /**
1612 * Set the parent tab of this tab.
1613 */
Michael Kolbc831b632011-05-11 09:30:34 -07001614 void setParent(Tab parent) {
1615 mParent = parent;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001616 // This tab may have been freed due to low memory. If that is the case,
Michael Kolbc831b632011-05-11 09:30:34 -07001617 // the parent tab id is already saved. If we are changing that id
Grace Kloba22ac16e2009-10-07 18:00:23 -07001618 // (most likely due to removing the parent tab) we must update the
Michael Kolbc831b632011-05-11 09:30:34 -07001619 // parent tab id in the saved Bundle.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001620 if (mSavedState != null) {
1621 if (parent == null) {
1622 mSavedState.remove(PARENTTAB);
1623 } else {
Michael Kolbc831b632011-05-11 09:30:34 -07001624 mSavedState.putLong(PARENTTAB, parent.getId());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001625 }
1626 }
John Reckb0a86db2011-05-24 14:05:58 -07001627
1628 // Sync the WebView useragent with the parent
1629 if (parent != null && mSettings.hasDesktopUseragent(parent.getWebView())
1630 != mSettings.hasDesktopUseragent(getWebView())) {
1631 mSettings.toggleDesktopUseragent(getWebView());
1632 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001633 }
1634
1635 /**
Michael Kolbc831b632011-05-11 09:30:34 -07001636 * If this Tab was created through another Tab, then this method returns
1637 * that Tab.
1638 * @return the Tab parent or null
1639 */
1640 public Tab getParent() {
1641 return mParent;
1642 }
1643
1644 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001645 * When a Tab is created through the content of another Tab, then we
1646 * associate the Tabs.
1647 * @param child the Tab that was created from this Tab
1648 */
1649 void addChildTab(Tab child) {
Michael Kolbc831b632011-05-11 09:30:34 -07001650 if (mChildren == null) {
1651 mChildren = new Vector<Tab>();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001652 }
Michael Kolbc831b632011-05-11 09:30:34 -07001653 mChildren.add(child);
1654 child.setParent(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001655 }
1656
Michael Kolbc831b632011-05-11 09:30:34 -07001657 Vector<Tab> getChildren() {
1658 return mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001659 }
1660
1661 void resume() {
1662 if (mMainView != null) {
1663 mMainView.onResume();
1664 if (mSubView != null) {
1665 mSubView.onResume();
1666 }
1667 }
1668 }
1669
1670 void pause() {
1671 if (mMainView != null) {
1672 mMainView.onPause();
1673 if (mSubView != null) {
1674 mSubView.onPause();
1675 }
1676 }
1677 }
1678
1679 void putInForeground() {
John Reck8ee633f2011-08-09 16:00:35 -07001680 if (mInForeground) {
1681 return;
1682 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001683 mInForeground = true;
1684 resume();
Michael Kolb14612442011-06-24 13:06:29 -07001685 Activity activity = mWebViewController.getActivity();
1686 mMainView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001687 if (mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -07001688 mSubView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001689 }
1690 // Show the pending error dialog if the queue is not empty
1691 if (mQueuedErrors != null && mQueuedErrors.size() > 0) {
1692 showError(mQueuedErrors.getFirst());
1693 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001694 mWebViewController.bookmarkedStatusHasChanged(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001695 }
1696
1697 void putInBackground() {
John Reck8ee633f2011-08-09 16:00:35 -07001698 if (!mInForeground) {
1699 return;
1700 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001701 mInForeground = false;
1702 pause();
1703 mMainView.setOnCreateContextMenuListener(null);
1704 if (mSubView != null) {
1705 mSubView.setOnCreateContextMenuListener(null);
1706 }
1707 }
1708
Michael Kolb8233fac2010-10-26 16:08:53 -07001709 boolean inForeground() {
1710 return mInForeground;
1711 }
1712
Grace Kloba22ac16e2009-10-07 18:00:23 -07001713 /**
1714 * Return the top window of this tab; either the subwindow if it is not
1715 * null or the main window.
1716 * @return The top window of this tab.
1717 */
1718 WebView getTopWindow() {
1719 if (mSubView != null) {
1720 return mSubView;
1721 }
1722 return mMainView;
1723 }
1724
1725 /**
1726 * Return the main window of this tab. Note: if a tab is freed in the
1727 * background, this can return null. It is only guaranteed to be
1728 * non-null for the current tab.
1729 * @return The main WebView of this tab.
1730 */
1731 WebView getWebView() {
1732 return mMainView;
1733 }
1734
Michael Kolba713ec82010-11-29 17:27:06 -08001735 void setViewContainer(View container) {
1736 mContainer = container;
1737 }
1738
Michael Kolb8233fac2010-10-26 16:08:53 -07001739 View getViewContainer() {
1740 return mContainer;
1741 }
1742
Grace Kloba22ac16e2009-10-07 18:00:23 -07001743 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001744 * Return whether private browsing is enabled for the main window of
1745 * this tab.
1746 * @return True if private browsing is enabled.
1747 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001748 boolean isPrivateBrowsingEnabled() {
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001749 WebView webView = getWebView();
1750 if (webView == null) {
1751 return false;
1752 }
1753 return webView.isPrivateBrowsingEnabled();
1754 }
1755
1756 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001757 * Return the subwindow of this tab or null if there is no subwindow.
1758 * @return The subwindow of this tab or null.
1759 */
1760 WebView getSubWebView() {
1761 return mSubView;
1762 }
1763
Michael Kolb1514bb72010-11-22 09:11:48 -08001764 void setSubWebView(WebView subView) {
1765 mSubView = subView;
1766 }
1767
Michael Kolb8233fac2010-10-26 16:08:53 -07001768 View getSubViewContainer() {
1769 return mSubViewContainer;
1770 }
1771
Michael Kolb1514bb72010-11-22 09:11:48 -08001772 void setSubViewContainer(View subViewContainer) {
1773 mSubViewContainer = subViewContainer;
1774 }
1775
Grace Kloba22ac16e2009-10-07 18:00:23 -07001776 /**
1777 * @return The geolocation permissions prompt for this tab.
1778 */
1779 GeolocationPermissionsPrompt getGeolocationPermissionsPrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001780 if (mGeolocationPermissionsPrompt == null) {
1781 ViewStub stub = (ViewStub) mContainer
1782 .findViewById(R.id.geolocation_permissions_prompt);
1783 mGeolocationPermissionsPrompt = (GeolocationPermissionsPrompt) stub
1784 .inflate();
Grace Kloba50c241e2010-04-20 11:07:50 -07001785 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001786 return mGeolocationPermissionsPrompt;
1787 }
1788
1789 /**
1790 * @return The application id string
1791 */
1792 String getAppId() {
1793 return mAppId;
1794 }
1795
1796 /**
1797 * Set the application id string
1798 * @param id
1799 */
1800 void setAppId(String id) {
1801 mAppId = id;
1802 }
1803
Michael Kolbe28b3472011-08-04 16:54:31 -07001804 boolean closeOnBack() {
1805 return mCloseOnBack;
1806 }
1807
1808 void setCloseOnBack(boolean close) {
1809 mCloseOnBack = close;
1810 }
1811
Grace Kloba22ac16e2009-10-07 18:00:23 -07001812 String getUrl() {
John Reck324d4402011-01-11 16:56:42 -08001813 return UrlUtils.filteredUrl(mCurrentState.mUrl);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001814 }
1815
John Reck49a603c2011-03-03 09:33:05 -08001816 String getOriginalUrl() {
John Reckdb22ec42011-06-29 11:31:24 -07001817 if (mCurrentState.mOriginalUrl == null) {
1818 return getUrl();
John Reck49a603c2011-03-03 09:33:05 -08001819 }
John Reckdb22ec42011-06-29 11:31:24 -07001820 return UrlUtils.filteredUrl(mCurrentState.mOriginalUrl);
John Reck49a603c2011-03-03 09:33:05 -08001821 }
1822
Grace Kloba22ac16e2009-10-07 18:00:23 -07001823 /**
John Reck30c714c2010-12-16 17:30:34 -08001824 * Get the title of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001825 */
1826 String getTitle() {
John Reck30c714c2010-12-16 17:30:34 -08001827 if (mCurrentState.mTitle == null && mInPageLoad) {
Michael Kolb14612442011-06-24 13:06:29 -07001828 return mContext.getString(R.string.title_bar_loading);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001829 }
John Reck30c714c2010-12-16 17:30:34 -08001830 return mCurrentState.mTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001831 }
1832
1833 /**
John Reck30c714c2010-12-16 17:30:34 -08001834 * Get the favicon of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001835 */
1836 Bitmap getFavicon() {
John Reck1cf4b792011-07-26 10:22:22 -07001837 if (mCurrentState.mFavicon != null) {
1838 return mCurrentState.mFavicon;
1839 }
1840 return getDefaultFavicon(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001841 }
1842
John Recke969cc52010-12-21 17:24:43 -08001843 public boolean isBookmarkedSite() {
1844 return mCurrentState.mIsBookmarkedSite;
1845 }
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001846
Grace Kloba22ac16e2009-10-07 18:00:23 -07001847 /**
1848 * Return the tab's error console. Creates the console if createIfNEcessary
1849 * is true and we haven't already created the console.
1850 * @param createIfNecessary Flag to indicate if the console should be
1851 * created if it has not been already.
1852 * @return The tab's error console, or null if one has not been created and
1853 * createIfNecessary is false.
1854 */
1855 ErrorConsoleView getErrorConsole(boolean createIfNecessary) {
1856 if (createIfNecessary && mErrorConsole == null) {
Michael Kolb14612442011-06-24 13:06:29 -07001857 mErrorConsole = new ErrorConsoleView(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001858 mErrorConsole.setWebView(mMainView);
1859 }
1860 return mErrorConsole;
1861 }
1862
John Reck30c714c2010-12-16 17:30:34 -08001863 private void setLockIconType(LockIcon icon) {
1864 mCurrentState.mLockIcon = icon;
1865 mWebViewController.onUpdatedLockIcon(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001866 }
1867
1868 /**
1869 * @return The tab's lock icon type.
1870 */
John Reck30c714c2010-12-16 17:30:34 -08001871 LockIcon getLockIconType() {
1872 return mCurrentState.mLockIcon;
1873 }
1874
1875 int getLoadProgress() {
1876 if (mInPageLoad) {
1877 return mPageLoadProgress;
1878 }
1879 return 100;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001880 }
1881
1882 /**
1883 * @return TRUE if onPageStarted is called while onPageFinished is not
1884 * called yet.
1885 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001886 boolean inPageLoad() {
1887 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001888 }
1889
1890 // force mInLoad to be false. This should only be called before closing the
1891 // tab to ensure BrowserActivity's pauseWebViewTimers() is called correctly.
Michael Kolb8233fac2010-10-26 16:08:53 -07001892 void clearInPageLoad() {
1893 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001894 }
1895
Grace Kloba22ac16e2009-10-07 18:00:23 -07001896 /**
John Reck1cf4b792011-07-26 10:22:22 -07001897 * @return The Bundle with the tab's state if it can be saved, otherwise null
Grace Kloba22ac16e2009-10-07 18:00:23 -07001898 */
John Reck1cf4b792011-07-26 10:22:22 -07001899 public Bundle saveState() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001900 // If the WebView is null it means we ran low on memory and we already
1901 // stored the saved state in mSavedState.
1902 if (mMainView == null) {
John Reck1cf4b792011-07-26 10:22:22 -07001903 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001904 }
John Reck24f18262011-06-17 14:47:20 -07001905 // If the tab is the homepage or has no URL, don't save it
1906 String homepage = BrowserSettings.getInstance().getHomePage();
1907 if (TextUtils.equals(homepage, mCurrentState.mUrl)
1908 || TextUtils.isEmpty(mCurrentState.mUrl)) {
John Reck1cf4b792011-07-26 10:22:22 -07001909 return null;
John Reck24f18262011-06-17 14:47:20 -07001910 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001911
1912 mSavedState = new Bundle();
John Reck541f55a2011-06-07 16:34:43 -07001913 mMainView.saveState(mSavedState);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001914
Michael Kolbc831b632011-05-11 09:30:34 -07001915 mSavedState.putLong(ID, mId);
John Reck30c714c2010-12-16 17:30:34 -08001916 mSavedState.putString(CURRURL, mCurrentState.mUrl);
1917 mSavedState.putString(CURRTITLE, mCurrentState.mTitle);
John Reck1cf4b792011-07-26 10:22:22 -07001918 mSavedState.putBoolean(INCOGNITO, mMainView.isPrivateBrowsingEnabled());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001919 if (mAppId != null) {
1920 mSavedState.putString(APPID, mAppId);
1921 }
Michael Kolbe28b3472011-08-04 16:54:31 -07001922 mSavedState.putBoolean(CLOSEFLAG, mCloseOnBack);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001923 // Remember the parent tab so the relationship can be restored.
Michael Kolbc831b632011-05-11 09:30:34 -07001924 if (mParent != null) {
1925 mSavedState.putLong(PARENTTAB, mParent.mId);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001926 }
John Reckb0a86db2011-05-24 14:05:58 -07001927 mSavedState.putBoolean(USERAGENT,
1928 mSettings.hasDesktopUseragent(getWebView()));
John Reck1cf4b792011-07-26 10:22:22 -07001929 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001930 }
1931
1932 /*
1933 * Restore the state of the tab.
1934 */
John Reck1cf4b792011-07-26 10:22:22 -07001935 private void restoreState(Bundle b) {
1936 mSavedState = b;
1937 if (mSavedState == null) {
1938 return;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001939 }
1940 // Restore the internal state even if the WebView fails to restore.
1941 // This will maintain the app id, original url and close-on-exit values.
Michael Kolbc831b632011-05-11 09:30:34 -07001942 mId = b.getLong(ID);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001943 mAppId = b.getString(APPID);
Michael Kolbe28b3472011-08-04 16:54:31 -07001944 mCloseOnBack = b.getBoolean(CLOSEFLAG);
John Reckb0a86db2011-05-24 14:05:58 -07001945 if (b.getBoolean(USERAGENT)
1946 != mSettings.hasDesktopUseragent(getWebView())) {
1947 mSettings.toggleDesktopUseragent(getWebView());
1948 }
John Reck1cf4b792011-07-26 10:22:22 -07001949 String url = b.getString(CURRURL);
1950 String title = b.getString(CURRTITLE);
1951 boolean incognito = b.getBoolean(INCOGNITO);
1952 mCurrentState = new PageState(mContext, incognito, url, null);
1953 mCurrentState.mTitle = title;
1954 synchronized (Tab.this) {
1955 if (mCapture != null) {
1956 BackgroundHandler.execute(mLoadThumbnail);
1957 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001958 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001959 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04001960
Leon Scroggins1961ed22010-12-07 15:22:21 -05001961 public void updateBookmarkedStatus() {
John Recke969cc52010-12-21 17:24:43 -08001962 mDataController.queryBookmarkStatus(getUrl(), mIsBookmarkCallback);
Leon Scroggins1961ed22010-12-07 15:22:21 -05001963 }
1964
John Recke969cc52010-12-21 17:24:43 -08001965 private DataController.OnQueryUrlIsBookmark mIsBookmarkCallback
1966 = new DataController.OnQueryUrlIsBookmark() {
1967 @Override
1968 public void onQueryUrlIsBookmark(String url, boolean isBookmark) {
1969 if (mCurrentState.mUrl.equals(url)) {
1970 mCurrentState.mIsBookmarkedSite = isBookmark;
1971 mWebViewController.bookmarkedStatusHasChanged(Tab.this);
1972 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001973 }
John Recke969cc52010-12-21 17:24:43 -08001974 };
Michael Kolb1acef692011-03-08 14:12:06 -08001975
Michael Kolbeb95db42011-03-03 10:38:40 -08001976 public Bitmap getScreenshot() {
John Reck1cf4b792011-07-26 10:22:22 -07001977 synchronized (Tab.this) {
1978 return mCapture;
1979 }
Michael Kolbeb95db42011-03-03 10:38:40 -08001980 }
1981
John Reck541f55a2011-06-07 16:34:43 -07001982 public boolean isSnapshot() {
John Reck541f55a2011-06-07 16:34:43 -07001983 return false;
1984 }
1985
John Reckd8c74522011-06-14 08:45:00 -07001986 public ContentValues createSnapshotValues() {
1987 if (mMainView == null) return null;
John Reck8cc92352011-07-06 17:41:52 -07001988 ByteArrayOutputStream bos = new ByteArrayOutputStream();
1989 try {
1990 GZIPOutputStream stream = new GZIPOutputStream(bos);
1991 if (!mMainView.saveViewState(stream)) {
1992 return null;
1993 }
1994 stream.flush();
1995 stream.close();
1996 } catch (Exception e) {
1997 Log.w(LOGTAG, "Failed to save view state", e);
John Reck541f55a2011-06-07 16:34:43 -07001998 return null;
1999 }
John Reck8cc92352011-07-06 17:41:52 -07002000 byte[] data = bos.toByteArray();
John Reckd8c74522011-06-14 08:45:00 -07002001 ContentValues values = new ContentValues();
2002 values.put(Snapshots.TITLE, mCurrentState.mTitle);
2003 values.put(Snapshots.URL, mCurrentState.mUrl);
2004 values.put(Snapshots.VIEWSTATE, data);
2005 values.put(Snapshots.BACKGROUND, mMainView.getPageBackgroundColor());
John Reck8cc92352011-07-06 17:41:52 -07002006 values.put(Snapshots.DATE_CREATED, System.currentTimeMillis());
2007 values.put(Snapshots.FAVICON, compressBitmap(getFavicon()));
2008 Bitmap screenshot = Controller.createScreenshot(mMainView,
2009 Controller.getDesiredThumbnailWidth(mContext),
2010 Controller.getDesiredThumbnailHeight(mContext));
2011 values.put(Snapshots.THUMBNAIL, compressBitmap(screenshot));
John Reckd8c74522011-06-14 08:45:00 -07002012 return values;
John Reck541f55a2011-06-07 16:34:43 -07002013 }
2014
John Reck8cc92352011-07-06 17:41:52 -07002015 public byte[] compressBitmap(Bitmap bitmap) {
2016 if (bitmap == null) {
2017 return null;
2018 }
2019 ByteArrayOutputStream stream = new ByteArrayOutputStream();
2020 bitmap.compress(CompressFormat.PNG, 100, stream);
2021 return stream.toByteArray();
2022 }
2023
John Reck26b18322011-06-21 13:08:58 -07002024 public void loadUrl(String url, Map<String, String> headers) {
2025 if (mMainView != null) {
Michael Kolb14612442011-06-24 13:06:29 -07002026 mCurrentState = new PageState(mContext, false, url, null);
John Reck26b18322011-06-21 13:08:58 -07002027 mWebViewController.onPageStarted(this, mMainView, null);
2028 mMainView.loadUrl(url, headers);
2029 }
2030 }
2031
Michael Kolb9ef259a2011-07-12 15:33:08 -07002032 protected void capture() {
2033 if (mMainView == null || mCapture == null) return;
2034 Canvas c = new Canvas(mCapture);
2035 final int left = mMainView.getScrollX();
2036 final int top = mMainView.getScrollY() + mMainView.getVisibleTitleHeight();
2037 c.translate(-left, -top);
2038 float scale = mCaptureWidth / (float) mMainView.getWidth();
2039 c.scale(scale, scale, left, top);
John Reck8ee633f2011-08-09 16:00:35 -07002040 if (mMainView instanceof BrowserWebView) {
2041 ((BrowserWebView)mMainView).drawContent(c);
2042 } else {
2043 mMainView.draw(c);
2044 }
Dianne Hackborn43cfe8a2011-08-02 16:59:35 -07002045 c.setBitmap(null);
John Reck8ee633f2011-08-09 16:00:35 -07002046 mHandler.removeMessages(MSG_CAPTURE);
John Reck1cf4b792011-07-26 10:22:22 -07002047 persistThumbnail();
John Reck8ee633f2011-08-09 16:00:35 -07002048 TabControl tc = mWebViewController.getTabControl();
2049 if (tc != null) {
2050 OnThumbnailUpdatedListener updateListener
2051 = tc.getOnThumbnailUpdatedListener();
2052 if (updateListener != null) {
2053 updateListener.onThumbnailUpdated(this);
2054 }
2055 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07002056 }
2057
2058 @Override
2059 public void onNewPicture(WebView view, Picture picture) {
2060 //update screenshot
John Reck1cf4b792011-07-26 10:22:22 -07002061 postCapture();
2062 }
2063
2064 private void postCapture() {
Michael Kolb9ef259a2011-07-12 15:33:08 -07002065 if (!mHandler.hasMessages(MSG_CAPTURE)) {
2066 mHandler.sendEmptyMessageDelayed(MSG_CAPTURE, CAPTURE_DELAY);
2067 }
2068 }
2069
John Reckef654f12011-07-12 16:42:08 -07002070 public boolean canGoBack() {
2071 return mMainView != null ? mMainView.canGoBack() : false;
2072 }
2073
2074 public boolean canGoForward() {
2075 return mMainView != null ? mMainView.canGoForward() : false;
2076 }
2077
2078 public void goBack() {
2079 if (mMainView != null) {
2080 mMainView.goBack();
2081 }
2082 }
2083
2084 public void goForward() {
2085 if (mMainView != null) {
2086 mMainView.goForward();
2087 }
2088 }
2089
Mathew Inwood1dd8e822011-08-03 14:34:29 +01002090 /**
2091 * Causes the tab back/forward stack to be cleared once, if the given URL is the next URL
2092 * to be added to the stack.
2093 *
2094 * This is used to ensure that preloaded URLs that are not subsequently seen by the user do
2095 * not appear in the back stack.
2096 */
2097 public void clearBackStackWhenItemAdded(String urlToMatch) {
2098 mClearHistoryMatchUrl = urlToMatch;
2099 }
2100
John Reck1cf4b792011-07-26 10:22:22 -07002101 protected void persistThumbnail() {
2102 BackgroundHandler.execute(mSaveThumbnail);
2103 }
2104
2105 protected void deleteThumbnail() {
2106 BackgroundHandler.execute(mDeleteThumbnail);
2107 }
2108
2109 private void updateCaptureFromBlob(byte[] blob) {
2110 synchronized (Tab.this) {
2111 if (mCapture == null) {
2112 return;
2113 }
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002114 ByteBuffer buffer = ByteBuffer.wrap(blob);
Michael Kolbd837a112011-08-09 14:00:18 -07002115 try {
2116 mCapture.copyPixelsFromBuffer(buffer);
2117 } catch (RuntimeException rex) {
2118 Log.e(LOGTAG, "Load capture has mismatched sizes; buffer: "
2119 + buffer.capacity() + " blob: " + blob.length
2120 + "capture: " + mCapture.getByteCount());
2121 throw rex;
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002122 }
John Reck1cf4b792011-07-26 10:22:22 -07002123 }
2124 }
2125
2126 private byte[] getCaptureBlob() {
2127 synchronized (Tab.this) {
2128 if (mCapture == null) {
2129 return null;
2130 }
2131 ByteBuffer buffer = ByteBuffer.allocate(mCapture.getByteCount());
2132 mCapture.copyPixelsToBuffer(buffer);
2133 return buffer.array();
2134 }
2135 }
2136
2137 private Runnable mSaveThumbnail = new Runnable() {
2138
2139 @Override
2140 public void run() {
2141 byte[] blob = getCaptureBlob();
2142 if (blob == null) {
2143 return;
2144 }
2145 ContentResolver cr = mContext.getContentResolver();
2146 ContentValues values = new ContentValues();
2147 values.put(Thumbnails._ID, mId);
2148 values.put(Thumbnails.THUMBNAIL, blob);
2149 cr.insert(Thumbnails.CONTENT_URI, values);
2150 }
2151 };
2152
2153 private Runnable mDeleteThumbnail = new Runnable() {
2154
2155 @Override
2156 public void run() {
2157 ContentResolver cr = mContext.getContentResolver();
2158 try {
2159 cr.delete(ContentUris.withAppendedId(Thumbnails.CONTENT_URI, mId),
2160 null, null);
2161 } catch (Throwable t) {}
2162 }
2163 };
2164
2165 private Runnable mLoadThumbnail = new Runnable() {
2166
2167 @Override
2168 public void run() {
2169 ContentResolver cr = mContext.getContentResolver();
2170 Cursor c = null;
2171 try {
2172 Uri uri = ContentUris.withAppendedId(Thumbnails.CONTENT_URI, mId);
2173 c = cr.query(uri, new String[] {Thumbnails._ID,
2174 Thumbnails.THUMBNAIL}, null, null, null);
2175 if (c.moveToFirst()) {
2176 byte[] data = c.getBlob(1);
2177 if (data != null && data.length > 0) {
2178 updateCaptureFromBlob(data);
2179 }
2180 }
2181 } finally {
2182 if (c != null) {
2183 c.close();
2184 }
2185 }
2186 }
2187 };
2188
Grace Kloba22ac16e2009-10-07 18:00:23 -07002189}