blob: 8dcc54b65df3335b315c761b7c925e7c99b67131 [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;
John Reck6c2e2f32011-08-22 13:41:23 -070059import android.webkit.WebBackForwardList;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -050060import android.webkit.WebBackForwardListClient;
Grace Kloba22ac16e2009-10-07 18:00:23 -070061import android.webkit.WebChromeClient;
62import android.webkit.WebHistoryItem;
John Reck438bf462011-01-12 18:11:46 -080063import android.webkit.WebResourceResponse;
Grace Kloba22ac16e2009-10-07 18:00:23 -070064import android.webkit.WebStorage;
65import android.webkit.WebView;
Michael Kolb9ef259a2011-07-12 15:33:08 -070066import android.webkit.WebView.PictureListener;
Grace Kloba22ac16e2009-10-07 18:00:23 -070067import android.webkit.WebViewClient;
Ben Murdoch1d676b62011-01-17 12:54:24 +000068import android.widget.CheckBox;
Ben Murdoch8029a772010-11-16 11:58:21 +000069import android.widget.Toast;
Grace Kloba22ac16e2009-10-07 18:00:23 -070070
John Reck8ee633f2011-08-09 16:00:35 -070071import com.android.browser.TabControl.OnThumbnailUpdatedListener;
John Reck541f55a2011-06-07 16:34:43 -070072import com.android.browser.homepages.HomeProvider;
John Reck1cf4b792011-07-26 10:22:22 -070073import com.android.browser.provider.BrowserProvider2.Thumbnails;
John Reck8cc92352011-07-06 17:41:52 -070074import com.android.browser.provider.SnapshotProvider.Snapshots;
John Reck541f55a2011-06-07 16:34:43 -070075import com.android.common.speech.LoggingEvents;
76
77import java.io.ByteArrayOutputStream;
John Reck1cf4b792011-07-26 10:22:22 -070078import java.nio.ByteBuffer;
Michael Kolbfe251992010-07-08 15:41:55 -070079import java.util.ArrayList;
80import java.util.HashMap;
81import java.util.Iterator;
82import java.util.LinkedList;
83import java.util.Map;
84import java.util.Vector;
Mathew Inwooda829d552011-09-02 14:16:25 +010085import java.util.regex.Pattern;
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 /**
Mathew Inwoodb4e831b2011-09-05 18:58:48 +0100175 * See {@link #clearBackStackWhenItemAdded(Pattern)}.
Mathew Inwood1dd8e822011-08-03 14:34:29 +0100176 */
Mathew Inwooda829d552011-09-02 14:16:25 +0100177 private Pattern mClearHistoryUrlPattern;
Mathew Inwood1dd8e822011-08-03 14:34:29 +0100178
Mathew Inwoodb4e831b2011-09-05 18:58:48 +0100179 private OnPageLoadCompleteListener mOnPageLoadCompleteListener;
180 private Pattern mOnPageLoadCompleteUrlMatch;
181
John Reck1cf4b792011-07-26 10:22:22 -0700182 private static synchronized Bitmap getDefaultFavicon(Context context) {
183 if (sDefaultFavicon == null) {
184 sDefaultFavicon = BitmapFactory.decodeResource(
185 context.getResources(), R.drawable.app_web_browser_sm);
186 }
187 return sDefaultFavicon;
188 }
Michael Kolbeb95db42011-03-03 10:38:40 -0800189
John Reck30c714c2010-12-16 17:30:34 -0800190 // All the state needed for a page
John Reckd8c74522011-06-14 08:45:00 -0700191 protected static class PageState {
John Reck30c714c2010-12-16 17:30:34 -0800192 String mUrl;
John Reckdb22ec42011-06-29 11:31:24 -0700193 String mOriginalUrl;
John Reck30c714c2010-12-16 17:30:34 -0800194 String mTitle;
195 LockIcon mLockIcon;
196 Bitmap mFavicon;
John Reck502a3532011-08-16 14:21:46 -0700197 boolean mIsBookmarkedSite = false;
198 boolean mIncognito = false;
John Reck30c714c2010-12-16 17:30:34 -0800199
200 PageState(Context c, boolean incognito) {
John Reck502a3532011-08-16 14:21:46 -0700201 mIncognito = incognito;
202 if (mIncognito) {
John Reckdb22ec42011-06-29 11:31:24 -0700203 mOriginalUrl = mUrl = "browser:incognito";
John Reck30c714c2010-12-16 17:30:34 -0800204 mTitle = c.getString(R.string.new_incognito_tab);
John Reck30c714c2010-12-16 17:30:34 -0800205 } else {
John Reckdb22ec42011-06-29 11:31:24 -0700206 mOriginalUrl = mUrl = "";
John Reck30c714c2010-12-16 17:30:34 -0800207 mTitle = c.getString(R.string.new_tab);
John Reck30c714c2010-12-16 17:30:34 -0800208 }
John Reck1cf4b792011-07-26 10:22:22 -0700209 mFavicon = null;
John Reck30c714c2010-12-16 17:30:34 -0800210 mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
211 }
212
213 PageState(Context c, boolean incognito, String url, Bitmap favicon) {
John Reck502a3532011-08-16 14:21:46 -0700214 mIncognito = incognito;
John Reckdb22ec42011-06-29 11:31:24 -0700215 mOriginalUrl = mUrl = url;
John Reck30c714c2010-12-16 17:30:34 -0800216 mTitle = null;
217 if (URLUtil.isHttpsUrl(url)) {
218 mLockIcon = LockIcon.LOCK_ICON_SECURE;
219 } else {
220 mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
221 }
John Reck1cf4b792011-07-26 10:22:22 -0700222 mFavicon = favicon;
John Reck30c714c2010-12-16 17:30:34 -0800223 }
John Reck1cf4b792011-07-26 10:22:22 -0700224
Grace Kloba22ac16e2009-10-07 18:00:23 -0700225 }
226
John Reck30c714c2010-12-16 17:30:34 -0800227 // The current/loading page's state
John Reckd8c74522011-06-14 08:45:00 -0700228 protected PageState mCurrentState;
John Reck30c714c2010-12-16 17:30:34 -0800229
Grace Kloba22ac16e2009-10-07 18:00:23 -0700230 // Used for saving and restoring each Tab
Michael Kolbc831b632011-05-11 09:30:34 -0700231 static final String ID = "ID";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700232 static final String CURRURL = "currentUrl";
233 static final String CURRTITLE = "currentTitle";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700234 static final String PARENTTAB = "parentTab";
235 static final String APPID = "appid";
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700236 static final String INCOGNITO = "privateBrowsingEnabled";
John Reckb0a86db2011-05-24 14:05:58 -0700237 static final String USERAGENT = "useragent";
Michael Kolbe28b3472011-08-04 16:54:31 -0700238 static final String CLOSEFLAG = "closeOnBack";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700239
240 // -------------------------------------------------------------------------
241
Leon Scroggins58d56c62010-01-28 15:12:40 -0500242 /**
243 * Private information regarding the latest voice search. If the Tab is not
244 * in voice search mode, this will be null.
245 */
246 private VoiceSearchData mVoiceSearchData;
247 /**
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400248 * Remove voice search mode from this tab.
249 */
250 public void revertVoiceSearchMode() {
251 if (mVoiceSearchData != null) {
252 mVoiceSearchData = null;
253 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700254 mWebViewController.revertVoiceSearchMode(this);
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400255 }
256 }
257 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700258
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400259 /**
Leon Scroggins58d56c62010-01-28 15:12:40 -0500260 * Return whether the tab is in voice search mode.
261 */
262 public boolean isInVoiceSearchMode() {
263 return mVoiceSearchData != null;
264 }
265 /**
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400266 * Return true if the Tab is in voice search mode and the voice search
267 * Intent came with a String identifying that Google provided the Intent.
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500268 */
269 public boolean voiceSearchSourceIsGoogle() {
270 return mVoiceSearchData != null && mVoiceSearchData.mSourceIsGoogle;
271 }
272 /**
Leon Scroggins58d56c62010-01-28 15:12:40 -0500273 * Get the title to display for the current voice search page. If the Tab
274 * is not in voice search mode, return null.
275 */
276 public String getVoiceDisplayTitle() {
277 if (mVoiceSearchData == null) return null;
278 return mVoiceSearchData.mLastVoiceSearchTitle;
279 }
280 /**
281 * Get the latest array of voice search results, to be passed to the
282 * BrowserProvider. If the Tab is not in voice search mode, return null.
283 */
284 public ArrayList<String> getVoiceSearchResults() {
285 if (mVoiceSearchData == null) return null;
286 return mVoiceSearchData.mVoiceSearchResults;
287 }
288 /**
289 * Activate voice search mode.
290 * @param intent Intent which has the results to use, or an index into the
291 * results when reusing the old results.
292 */
293 /* package */ void activateVoiceSearchMode(Intent intent) {
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500294 int index = 0;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500295 ArrayList<String> results = intent.getStringArrayListExtra(
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -0500296 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_STRINGS);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500297 if (results != null) {
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500298 ArrayList<String> urls = intent.getStringArrayListExtra(
299 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_URLS);
300 ArrayList<String> htmls = intent.getStringArrayListExtra(
301 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_HTML);
302 ArrayList<String> baseUrls = intent.getStringArrayListExtra(
303 RecognizerResultsIntent
304 .EXTRA_VOICE_SEARCH_RESULT_HTML_BASE_URLS);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500305 // This tab is now entering voice search mode for the first time, or
306 // a new voice search was done.
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500307 int size = results.size();
308 if (urls == null || size != urls.size()) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500309 throw new AssertionError("improper extras passed in Intent");
310 }
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500311 if (htmls == null || htmls.size() != size || baseUrls == null ||
312 (baseUrls.size() != size && baseUrls.size() != 1)) {
313 // If either of these arrays are empty/incorrectly sized, ignore
314 // them.
315 htmls = null;
316 baseUrls = null;
317 }
318 mVoiceSearchData = new VoiceSearchData(results, urls, htmls,
319 baseUrls);
Leon Scroggins9df94972010-03-08 18:20:35 -0500320 mVoiceSearchData.mHeaders = intent.getParcelableArrayListExtra(
321 RecognizerResultsIntent
322 .EXTRA_VOICE_SEARCH_RESULT_HTTP_HEADERS);
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500323 mVoiceSearchData.mSourceIsGoogle = intent.getBooleanExtra(
324 VoiceSearchData.SOURCE_IS_GOOGLE, false);
Leon Scroggins2ee4a5a2010-03-15 16:56:57 -0400325 mVoiceSearchData.mVoiceSearchIntent = new Intent(intent);
Leon Scrogginse10dde52010-03-08 19:53:03 -0500326 }
327 String extraData = intent.getStringExtra(
328 SearchManager.EXTRA_DATA_KEY);
329 if (extraData != null) {
330 index = Integer.parseInt(extraData);
331 if (index >= mVoiceSearchData.mVoiceSearchResults.size()) {
332 throw new AssertionError("index must be less than "
333 + "size of mVoiceSearchResults");
334 }
335 if (mVoiceSearchData.mSourceIsGoogle) {
336 Intent logIntent = new Intent(
337 LoggingEvents.ACTION_LOG_EVENT);
338 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
339 LoggingEvents.VoiceSearch.N_BEST_CHOOSE);
340 logIntent.putExtra(
341 LoggingEvents.VoiceSearch.EXTRA_N_BEST_CHOOSE_INDEX,
342 index);
Michael Kolb14612442011-06-24 13:06:29 -0700343 mContext.sendBroadcast(logIntent);
Leon Scrogginse10dde52010-03-08 19:53:03 -0500344 }
345 if (mVoiceSearchData.mVoiceSearchIntent != null) {
Leon Scroggins2ee4a5a2010-03-15 16:56:57 -0400346 // Copy the Intent, so that each history item will have its own
347 // Intent, with different (or none) extra data.
348 Intent latest = new Intent(mVoiceSearchData.mVoiceSearchIntent);
349 latest.putExtra(SearchManager.EXTRA_DATA_KEY, extraData);
350 mVoiceSearchData.mVoiceSearchIntent = latest;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500351 }
352 }
353 mVoiceSearchData.mLastVoiceSearchTitle
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500354 = mVoiceSearchData.mVoiceSearchResults.get(index);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500355 if (mInForeground) {
Michael Kolb11d19782011-03-20 10:17:40 -0700356 mWebViewController.activateVoiceSearchMode(
357 mVoiceSearchData.mLastVoiceSearchTitle,
358 mVoiceSearchData.mVoiceSearchResults);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500359 }
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500360 if (mVoiceSearchData.mVoiceSearchHtmls != null) {
361 // When index was found it was already ensured that it was valid
362 String uriString = mVoiceSearchData.mVoiceSearchHtmls.get(index);
363 if (uriString != null) {
364 Uri dataUri = Uri.parse(uriString);
365 if (RecognizerResultsIntent.URI_SCHEME_INLINE.equals(
366 dataUri.getScheme())) {
367 // If there is only one base URL, use it. If there are
368 // more, there will be one for each index, so use the base
369 // URL corresponding to the index.
370 String baseUrl = mVoiceSearchData.mVoiceSearchBaseUrls.get(
371 mVoiceSearchData.mVoiceSearchBaseUrls.size() > 1 ?
372 index : 0);
373 mVoiceSearchData.mLastVoiceSearchUrl = baseUrl;
374 mMainView.loadDataWithBaseURL(baseUrl,
375 uriString.substring(RecognizerResultsIntent
376 .URI_SCHEME_INLINE.length() + 1), "text/html",
377 "utf-8", baseUrl);
378 return;
379 }
380 }
381 }
Leon Scroggins58d56c62010-01-28 15:12:40 -0500382 mVoiceSearchData.mLastVoiceSearchUrl
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500383 = mVoiceSearchData.mVoiceSearchUrls.get(index);
384 if (null == mVoiceSearchData.mLastVoiceSearchUrl) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700385 mVoiceSearchData.mLastVoiceSearchUrl = UrlUtils.smartUrlFilter(
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500386 mVoiceSearchData.mLastVoiceSearchTitle);
387 }
Leon Scroggins9df94972010-03-08 18:20:35 -0500388 Map<String, String> headers = null;
389 if (mVoiceSearchData.mHeaders != null) {
390 int bundleIndex = mVoiceSearchData.mHeaders.size() == 1 ? 0
391 : index;
392 Bundle bundle = mVoiceSearchData.mHeaders.get(bundleIndex);
393 if (bundle != null && !bundle.isEmpty()) {
394 Iterator<String> iter = bundle.keySet().iterator();
395 headers = new HashMap<String, String>();
396 while (iter.hasNext()) {
397 String key = iter.next();
398 headers.put(key, bundle.getString(key));
399 }
400 }
401 }
402 mMainView.loadUrl(mVoiceSearchData.mLastVoiceSearchUrl, headers);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500403 }
404 /* package */ static class VoiceSearchData {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500405 public VoiceSearchData(ArrayList<String> results,
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500406 ArrayList<String> urls, ArrayList<String> htmls,
407 ArrayList<String> baseUrls) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500408 mVoiceSearchResults = results;
409 mVoiceSearchUrls = urls;
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500410 mVoiceSearchHtmls = htmls;
411 mVoiceSearchBaseUrls = baseUrls;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500412 }
413 /*
414 * ArrayList of suggestions to be displayed when opening the
415 * SearchManager
416 */
417 public ArrayList<String> mVoiceSearchResults;
418 /*
419 * ArrayList of urls, associated with the suggestions in
420 * mVoiceSearchResults.
421 */
422 public ArrayList<String> mVoiceSearchUrls;
423 /*
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500424 * ArrayList holding content to load for each item in
425 * mVoiceSearchResults.
426 */
427 public ArrayList<String> mVoiceSearchHtmls;
428 /*
429 * ArrayList holding base urls for the items in mVoiceSearchResults.
430 * If non null, this will either have the same size as
431 * mVoiceSearchResults or have a size of 1, in which case all will use
432 * the same base url
433 */
434 public ArrayList<String> mVoiceSearchBaseUrls;
435 /*
Leon Scroggins58d56c62010-01-28 15:12:40 -0500436 * The last url provided by voice search. Used for comparison to see if
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500437 * we are going to a page by some method besides voice search.
Leon Scroggins58d56c62010-01-28 15:12:40 -0500438 */
439 public String mLastVoiceSearchUrl;
440 /**
441 * The last title used for voice search. Needed to update the title bar
442 * when switching tabs.
443 */
444 public String mLastVoiceSearchTitle;
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500445 /**
446 * Whether the Intent which turned on voice search mode contained the
447 * String signifying that Google was the source.
448 */
449 public boolean mSourceIsGoogle;
450 /**
Leon Scroggins9df94972010-03-08 18:20:35 -0500451 * List of headers to be passed into the WebView containing location
452 * information
453 */
454 public ArrayList<Bundle> mHeaders;
455 /**
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500456 * The Intent used to invoke voice search. Placed on the
457 * WebHistoryItem so that when coming back to a previous voice search
458 * page we can again activate voice search.
459 */
Leon Scrogginse10dde52010-03-08 19:53:03 -0500460 public Intent mVoiceSearchIntent;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500461 /**
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500462 * String used to identify Google as the source of voice search.
463 */
464 public static String SOURCE_IS_GOOGLE
465 = "android.speech.extras.SOURCE_IS_GOOGLE";
Leon Scroggins58d56c62010-01-28 15:12:40 -0500466 }
467
Grace Kloba22ac16e2009-10-07 18:00:23 -0700468 // Container class for the next error dialog that needs to be displayed
469 private class ErrorDialog {
470 public final int mTitle;
471 public final String mDescription;
472 public final int mError;
473 ErrorDialog(int title, String desc, int error) {
474 mTitle = title;
475 mDescription = desc;
476 mError = error;
477 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700478 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700479
480 private void processNextError() {
481 if (mQueuedErrors == null) {
482 return;
483 }
484 // The first one is currently displayed so just remove it.
485 mQueuedErrors.removeFirst();
486 if (mQueuedErrors.size() == 0) {
487 mQueuedErrors = null;
488 return;
489 }
490 showError(mQueuedErrors.getFirst());
491 }
492
493 private DialogInterface.OnDismissListener mDialogListener =
494 new DialogInterface.OnDismissListener() {
495 public void onDismiss(DialogInterface d) {
496 processNextError();
497 }
498 };
499 private LinkedList<ErrorDialog> mQueuedErrors;
500
501 private void queueError(int err, String desc) {
502 if (mQueuedErrors == null) {
503 mQueuedErrors = new LinkedList<ErrorDialog>();
504 }
505 for (ErrorDialog d : mQueuedErrors) {
506 if (d.mError == err) {
507 // Already saw a similar error, ignore the new one.
508 return;
509 }
510 }
511 ErrorDialog errDialog = new ErrorDialog(
512 err == WebViewClient.ERROR_FILE_NOT_FOUND ?
513 R.string.browserFrameFileErrorLabel :
514 R.string.browserFrameNetworkErrorLabel,
515 desc, err);
516 mQueuedErrors.addLast(errDialog);
517
518 // Show the dialog now if the queue was empty and it is in foreground
519 if (mQueuedErrors.size() == 1 && mInForeground) {
520 showError(errDialog);
521 }
522 }
523
524 private void showError(ErrorDialog errDialog) {
525 if (mInForeground) {
Michael Kolb14612442011-06-24 13:06:29 -0700526 AlertDialog d = new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700527 .setTitle(errDialog.mTitle)
528 .setMessage(errDialog.mDescription)
529 .setPositiveButton(R.string.ok, null)
530 .create();
531 d.setOnDismissListener(mDialogListener);
532 d.show();
533 }
534 }
535
Mathew Inwoodb4e831b2011-09-05 18:58:48 +0100536 public interface OnPageLoadCompleteListener {
537 void onPageLoadComplete();
538 }
539
540 /**
541 * Requests a notification when the next page load completes. This is a one shot notification,
542 * the listener will be discarded after the first callback, or if the page load is cancelled.
543 * @param listener
544 */
545 public void setOnPageLoadCompleteListener(Pattern urlMatch,
546 OnPageLoadCompleteListener listener) {
547 mOnPageLoadCompleteListener = listener;
548 mOnPageLoadCompleteUrlMatch = urlMatch;
549 }
550
551 public void clearPageLoadCompleteListener() {
552 mOnPageLoadCompleteListener = null;
553 mOnPageLoadCompleteUrlMatch = null;
554 }
555
Grace Kloba22ac16e2009-10-07 18:00:23 -0700556 // -------------------------------------------------------------------------
557 // WebViewClient implementation for the main WebView
558 // -------------------------------------------------------------------------
559
560 private final WebViewClient mWebViewClient = new WebViewClient() {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500561 private Message mDontResend;
562 private Message mResend;
Michael Kolb47bd1e42011-09-01 15:25:00 -0700563
564 private boolean providersDiffer(String url, String otherUrl) {
565 Uri uri1 = Uri.parse(url);
566 Uri uri2 = Uri.parse(otherUrl);
567 return !uri1.getEncodedAuthority().equals(uri2.getEncodedAuthority());
568 }
569
Grace Kloba22ac16e2009-10-07 18:00:23 -0700570 @Override
571 public void onPageStarted(WebView view, String url, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700572 mInPageLoad = true;
John Reck30c714c2010-12-16 17:30:34 -0800573 mPageLoadProgress = 0;
Michael Kolb14612442011-06-24 13:06:29 -0700574 mCurrentState = new PageState(mContext,
John Reck30c714c2010-12-16 17:30:34 -0800575 view.isPrivateBrowsingEnabled(), url, favicon);
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000576 mLoadStartTime = SystemClock.uptimeMillis();
Leon Scroggins58d56c62010-01-28 15:12:40 -0500577 if (mVoiceSearchData != null
Michael Kolb47bd1e42011-09-01 15:25:00 -0700578 && providersDiffer(url, mVoiceSearchData.mLastVoiceSearchUrl)) {
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500579 if (mVoiceSearchData.mSourceIsGoogle) {
580 Intent i = new Intent(LoggingEvents.ACTION_LOG_EVENT);
581 i.putExtra(LoggingEvents.EXTRA_FLUSH, true);
Michael Kolb14612442011-06-24 13:06:29 -0700582 mContext.sendBroadcast(i);
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500583 }
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400584 revertVoiceSearchMode();
Leon Scroggins58d56c62010-01-28 15:12:40 -0500585 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700586
Grace Kloba22ac16e2009-10-07 18:00:23 -0700587
588 // If we start a touch icon load and then load a new page, we don't
589 // want to cancel the current touch icon loader. But, we do want to
590 // create a new one when the touch icon url is known.
591 if (mTouchIconLoader != null) {
592 mTouchIconLoader.mTab = null;
593 mTouchIconLoader = null;
594 }
595
596 // reset the error console
597 if (mErrorConsole != null) {
598 mErrorConsole.clearErrorMessages();
Michael Kolb8233fac2010-10-26 16:08:53 -0700599 if (mWebViewController.shouldShowErrorConsole()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700600 mErrorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
601 }
602 }
603
Patrick Scott92066772011-03-10 08:46:27 -0500604 // Cancel the auto-login process.
605 if (mDeviceAccountLogin != null) {
606 mDeviceAccountLogin.cancel();
607 mDeviceAccountLogin = null;
608 mWebViewController.hideAutoLogin(Tab.this);
609 }
610
Grace Kloba22ac16e2009-10-07 18:00:23 -0700611 // finally update the UI in the activity if it is in the foreground
John Reck324d4402011-01-11 16:56:42 -0800612 mWebViewController.onPageStarted(Tab.this, view, favicon);
Leon Scroggins4cd97792010-12-03 15:31:56 -0500613
John Recke969cc52010-12-21 17:24:43 -0800614 updateBookmarkedStatus();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700615 }
616
617 @Override
618 public void onPageFinished(WebView view, String url) {
Mathew Inwoodb4e831b2011-09-05 18:58:48 +0100619 if (mOnPageLoadCompleteListener != null) {
620 if (mOnPageLoadCompleteUrlMatch == null
621 || mOnPageLoadCompleteUrlMatch.matcher(url).matches())
622 mOnPageLoadCompleteListener.onPageLoadComplete();
623 mOnPageLoadCompleteListener = null;
624 mOnPageLoadCompleteUrlMatch = null;
625 }
John Recka1696282011-07-08 14:10:37 -0700626 if (!mInPageLoad) {
627 // In page navigation links (www.something.com#footer) will
628 // trigger an onPageFinished which we don't care about.
629 return;
630 }
John Reck5b691842010-11-29 11:21:13 -0800631 if (!isPrivateBrowsingEnabled()) {
632 LogTag.logPageFinishedLoading(
633 url, SystemClock.uptimeMillis() - mLoadStartTime);
634 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700635 mInPageLoad = false;
John Reck1cf4b792011-07-26 10:22:22 -0700636 syncCurrentState(view, url);
John Reck324d4402011-01-11 16:56:42 -0800637 mWebViewController.onPageFinished(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700638 }
639
640 // return true if want to hijack the url to let another app to handle it
641 @Override
642 public boolean shouldOverrideUrlLoading(WebView view, String url) {
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400643 if (voiceSearchSourceIsGoogle()) {
644 // This method is called when the user clicks on a link.
645 // VoiceSearchMode is turned off when the user leaves the
646 // Google results page, so at this point the user must be on
647 // that page. If the user clicked a link on that page, assume
648 // that the voice search was effective, and broadcast an Intent
649 // so a receiver can take note of that fact.
650 Intent logIntent = new Intent(LoggingEvents.ACTION_LOG_EVENT);
651 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
652 LoggingEvents.VoiceSearch.RESULT_CLICKED);
Michael Kolb14612442011-06-24 13:06:29 -0700653 mContext.sendBroadcast(logIntent);
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400654 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700655 if (mInForeground) {
Michael Kolb18eb3772010-12-10 14:29:51 -0800656 return mWebViewController.shouldOverrideUrlLoading(Tab.this,
657 view, url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700658 } else {
659 return false;
660 }
661 }
662
663 /**
664 * Updates the lock icon. This method is called when we discover another
665 * resource to be loaded for this page (for example, javascript). While
666 * we update the icon type, we do not update the lock icon itself until
667 * we are done loading, it is slightly more secure this way.
668 */
669 @Override
670 public void onLoadResource(WebView view, String url) {
671 if (url != null && url.length() > 0) {
672 // It is only if the page claims to be secure that we may have
673 // to update the lock:
John Reck30c714c2010-12-16 17:30:34 -0800674 if (mCurrentState.mLockIcon == LockIcon.LOCK_ICON_SECURE) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700675 // If NOT a 'safe' url, change the lock to mixed content!
676 if (!(URLUtil.isHttpsUrl(url) || URLUtil.isDataUrl(url)
677 || URLUtil.isAboutUrl(url))) {
John Reck30c714c2010-12-16 17:30:34 -0800678 mCurrentState.mLockIcon = LockIcon.LOCK_ICON_MIXED;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700679 }
680 }
681 }
682 }
683
684 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700685 * Show a dialog informing the user of the network error reported by
686 * WebCore if it is in the foreground.
687 */
688 @Override
689 public void onReceivedError(WebView view, int errorCode,
690 String description, String failingUrl) {
691 if (errorCode != WebViewClient.ERROR_HOST_LOOKUP &&
692 errorCode != WebViewClient.ERROR_CONNECT &&
693 errorCode != WebViewClient.ERROR_BAD_URL &&
694 errorCode != WebViewClient.ERROR_UNSUPPORTED_SCHEME &&
695 errorCode != WebViewClient.ERROR_FILE) {
696 queueError(errorCode, description);
697 }
Jeff Hamilton47654f42010-09-07 09:57:51 -0500698
699 // Don't log URLs when in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -0700700 if (!isPrivateBrowsingEnabled()) {
Jeff Hamilton47654f42010-09-07 09:57:51 -0500701 Log.e(LOGTAG, "onReceivedError " + errorCode + " " + failingUrl
702 + " " + description);
703 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700704 }
705
706 /**
707 * Check with the user if it is ok to resend POST data as the page they
708 * are trying to navigate to is the result of a POST.
709 */
710 @Override
711 public void onFormResubmission(WebView view, final Message dontResend,
712 final Message resend) {
713 if (!mInForeground) {
714 dontResend.sendToTarget();
715 return;
716 }
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500717 if (mDontResend != null) {
718 Log.w(LOGTAG, "onFormResubmission should not be called again "
719 + "while dialog is still up");
720 dontResend.sendToTarget();
721 return;
722 }
723 mDontResend = dontResend;
724 mResend = resend;
Michael Kolb14612442011-06-24 13:06:29 -0700725 new AlertDialog.Builder(mContext).setTitle(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700726 R.string.browserFrameFormResubmitLabel).setMessage(
727 R.string.browserFrameFormResubmitMessage)
728 .setPositiveButton(R.string.ok,
729 new DialogInterface.OnClickListener() {
730 public void onClick(DialogInterface dialog,
731 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500732 if (mResend != null) {
733 mResend.sendToTarget();
734 mResend = null;
735 mDontResend = null;
736 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700737 }
738 }).setNegativeButton(R.string.cancel,
739 new DialogInterface.OnClickListener() {
740 public void onClick(DialogInterface dialog,
741 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500742 if (mDontResend != null) {
743 mDontResend.sendToTarget();
744 mResend = null;
745 mDontResend = null;
746 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700747 }
748 }).setOnCancelListener(new OnCancelListener() {
749 public void onCancel(DialogInterface dialog) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500750 if (mDontResend != null) {
751 mDontResend.sendToTarget();
752 mResend = null;
753 mDontResend = null;
754 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700755 }
756 }).show();
757 }
758
759 /**
760 * Insert the url into the visited history database.
761 * @param url The url to be inserted.
762 * @param isReload True if this url is being reloaded.
763 * FIXME: Not sure what to do when reloading the page.
764 */
765 @Override
766 public void doUpdateVisitedHistory(WebView view, String url,
767 boolean isReload) {
John Reck324d4402011-01-11 16:56:42 -0800768 mWebViewController.doUpdateVisitedHistory(Tab.this, isReload);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700769 }
770
771 /**
772 * Displays SSL error(s) dialog to the user.
773 */
774 @Override
775 public void onReceivedSslError(final WebView view,
776 final SslErrorHandler handler, final SslError error) {
777 if (!mInForeground) {
778 handler.cancel();
John Reck30c714c2010-12-16 17:30:34 -0800779 setLockIconType(LockIcon.LOCK_ICON_UNSECURE);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700780 return;
781 }
John Reck35e9dd62011-04-25 09:01:54 -0700782 if (mSettings.showSecurityWarnings()) {
John Reckcb28b2c2011-08-26 17:39:44 -0700783 new AlertDialog.Builder(mContext)
784 .setTitle(R.string.security_warning)
785 .setMessage(R.string.ssl_warnings_header)
786 .setIcon(android.R.drawable.ic_dialog_alert)
787 .setPositiveButton(R.string.ssl_continue,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700788 new DialogInterface.OnClickListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700789 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700790 public void onClick(DialogInterface dialog,
791 int whichButton) {
792 handler.proceed();
793 }
John Reckcb28b2c2011-08-26 17:39:44 -0700794 })
795 .setNeutralButton(R.string.view_certificate,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700796 new DialogInterface.OnClickListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700797 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700798 public void onClick(DialogInterface dialog,
799 int whichButton) {
John Reckcb28b2c2011-08-26 17:39:44 -0700800 mWebViewController.showSslCertificateOnError(
801 view, handler, error);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700802 }
John Reckcb28b2c2011-08-26 17:39:44 -0700803 })
804 .setNegativeButton(R.string.ssl_go_back,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700805 new DialogInterface.OnClickListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700806 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700807 public void onClick(DialogInterface dialog,
808 int whichButton) {
John Reck30c714c2010-12-16 17:30:34 -0800809 dialog.cancel();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700810 }
John Reckcb28b2c2011-08-26 17:39:44 -0700811 })
812 .setOnCancelListener(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700813 new DialogInterface.OnCancelListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700814 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700815 public void onCancel(DialogInterface dialog) {
816 handler.cancel();
John Reck30c714c2010-12-16 17:30:34 -0800817 setLockIconType(LockIcon.LOCK_ICON_UNSECURE);
818 mWebViewController.onUserCanceledSsl(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700819 }
John Reckcb28b2c2011-08-26 17:39:44 -0700820 })
821 .show();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700822 } else {
823 handler.proceed();
824 }
825 }
826
827 /**
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700828 * Displays client certificate request to the user.
829 */
830 @Override
831 public void onReceivedClientCertRequest(final WebView view,
832 final ClientCertRequestHandler handler, final String host_and_port) {
833 if (!mInForeground) {
834 handler.ignore();
835 return;
836 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700837 int colon = host_and_port.lastIndexOf(':');
838 String host;
839 int port;
840 if (colon == -1) {
841 host = host_and_port;
842 port = -1;
843 } else {
844 String portString = host_and_port.substring(colon + 1);
845 try {
846 port = Integer.parseInt(portString);
847 host = host_and_port.substring(0, colon);
848 } catch (NumberFormatException e) {
849 host = host_and_port;
850 port = -1;
851 }
852 }
Michael Kolb14612442011-06-24 13:06:29 -0700853 KeyChain.choosePrivateKeyAlias(
854 mWebViewController.getActivity(), new KeyChainAliasCallback() {
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700855 @Override public void alias(String alias) {
856 if (alias == null) {
857 handler.cancel();
858 return;
859 }
Michael Kolb14612442011-06-24 13:06:29 -0700860 new KeyChainLookup(mContext, handler, alias).execute();
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700861 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700862 }, null, null, host, port, null);
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700863 }
864
865 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700866 * Handles an HTTP authentication request.
867 *
868 * @param handler The authentication handler
869 * @param host The host
870 * @param realm The realm
871 */
872 @Override
873 public void onReceivedHttpAuthRequest(WebView view,
874 final HttpAuthHandler handler, final String host,
875 final String realm) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700876 mWebViewController.onReceivedHttpAuthRequest(Tab.this, view, handler, host, realm);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700877 }
878
879 @Override
John Reck438bf462011-01-12 18:11:46 -0800880 public WebResourceResponse shouldInterceptRequest(WebView view,
881 String url) {
882 WebResourceResponse res = HomeProvider.shouldInterceptRequest(
Michael Kolb14612442011-06-24 13:06:29 -0700883 mContext, url);
John Reck438bf462011-01-12 18:11:46 -0800884 return res;
885 }
886
887 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700888 public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
889 if (!mInForeground) {
890 return false;
891 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700892 return mWebViewController.shouldOverrideKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700893 }
894
895 @Override
896 public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700897 if (!mInForeground) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700898 return;
899 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700900 mWebViewController.onUnhandledKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700901 }
Patrick Scott92066772011-03-10 08:46:27 -0500902
903 @Override
904 public void onReceivedLoginRequest(WebView view, String realm,
905 String account, String args) {
Michael Kolb14612442011-06-24 13:06:29 -0700906 new DeviceAccountLogin(mWebViewController.getActivity(), view, Tab.this, mWebViewController)
Patrick Scott92066772011-03-10 08:46:27 -0500907 .handleLogin(realm, account, args);
908 }
909
Grace Kloba22ac16e2009-10-07 18:00:23 -0700910 };
911
John Reck1cf4b792011-07-26 10:22:22 -0700912 private void syncCurrentState(WebView view, String url) {
913 // Sync state (in case of stop/timeout)
914 mCurrentState.mUrl = view.getUrl();
915 if (mCurrentState.mUrl == null) {
John Reck8bcafc12011-08-29 16:43:02 -0700916 mCurrentState.mUrl = "";
John Reck1cf4b792011-07-26 10:22:22 -0700917 }
918 mCurrentState.mOriginalUrl = view.getOriginalUrl();
919 mCurrentState.mTitle = view.getTitle();
920 mCurrentState.mFavicon = view.getFavicon();
921 if (!URLUtil.isHttpsUrl(mCurrentState.mUrl)) {
922 // In case we stop when loading an HTTPS page from an HTTP page
923 // but before a provisional load occurred
924 mCurrentState.mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
925 }
John Reck502a3532011-08-16 14:21:46 -0700926 mCurrentState.mIncognito = view.isPrivateBrowsingEnabled();
John Reck1cf4b792011-07-26 10:22:22 -0700927 }
928
Patrick Scott92066772011-03-10 08:46:27 -0500929 // Called by DeviceAccountLogin when the Tab needs to have the auto-login UI
930 // displayed.
931 void setDeviceAccountLogin(DeviceAccountLogin login) {
932 mDeviceAccountLogin = login;
933 }
934
935 // Returns non-null if the title bar should display the auto-login UI.
936 DeviceAccountLogin getDeviceAccountLogin() {
937 return mDeviceAccountLogin;
938 }
939
Grace Kloba22ac16e2009-10-07 18:00:23 -0700940 // -------------------------------------------------------------------------
941 // WebChromeClient implementation for the main WebView
942 // -------------------------------------------------------------------------
943
944 private final WebChromeClient mWebChromeClient = new WebChromeClient() {
945 // Helper method to create a new tab or sub window.
946 private void createWindow(final boolean dialog, final Message msg) {
947 WebView.WebViewTransport transport =
948 (WebView.WebViewTransport) msg.obj;
949 if (dialog) {
950 createSubWindow();
Michael Kolb8233fac2010-10-26 16:08:53 -0700951 mWebViewController.attachSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700952 transport.setWebView(mSubView);
953 } else {
Michael Kolb7bcafde2011-05-09 13:55:59 -0700954 final Tab newTab = mWebViewController.openTab(null,
John Reck5949c662011-05-27 09:52:29 -0700955 Tab.this, true, true);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700956 transport.setWebView(newTab.getWebView());
957 }
958 msg.sendToTarget();
959 }
960
961 @Override
962 public boolean onCreateWindow(WebView view, final boolean dialog,
963 final boolean userGesture, final Message resultMsg) {
964 // only allow new window or sub window for the foreground case
965 if (!mInForeground) {
966 return false;
967 }
968 // Short-circuit if we can't create any more tabs or sub windows.
969 if (dialog && mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -0700970 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700971 .setTitle(R.string.too_many_subwindows_dialog_title)
972 .setIcon(android.R.drawable.ic_dialog_alert)
973 .setMessage(R.string.too_many_subwindows_dialog_message)
974 .setPositiveButton(R.string.ok, null)
975 .show();
976 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700977 } else if (!mWebViewController.getTabControl().canCreateNewTab()) {
Michael Kolb14612442011-06-24 13:06:29 -0700978 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700979 .setTitle(R.string.too_many_windows_dialog_title)
980 .setIcon(android.R.drawable.ic_dialog_alert)
981 .setMessage(R.string.too_many_windows_dialog_message)
982 .setPositiveButton(R.string.ok, null)
983 .show();
984 return false;
985 }
986
987 // Short-circuit if this was a user gesture.
988 if (userGesture) {
989 createWindow(dialog, resultMsg);
990 return true;
991 }
992
993 // Allow the popup and create the appropriate window.
994 final AlertDialog.OnClickListener allowListener =
995 new AlertDialog.OnClickListener() {
996 public void onClick(DialogInterface d,
997 int which) {
998 createWindow(dialog, resultMsg);
999 }
1000 };
1001
1002 // Block the popup by returning a null WebView.
1003 final AlertDialog.OnClickListener blockListener =
1004 new AlertDialog.OnClickListener() {
1005 public void onClick(DialogInterface d, int which) {
1006 resultMsg.sendToTarget();
1007 }
1008 };
1009
1010 // Build a confirmation dialog to display to the user.
1011 final AlertDialog d =
Michael Kolb14612442011-06-24 13:06:29 -07001012 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -07001013 .setTitle(R.string.attention)
1014 .setIcon(android.R.drawable.ic_dialog_alert)
1015 .setMessage(R.string.popup_window_attempt)
1016 .setPositiveButton(R.string.allow, allowListener)
1017 .setNegativeButton(R.string.block, blockListener)
1018 .setCancelable(false)
1019 .create();
1020
1021 // Show the confirmation dialog.
1022 d.show();
1023 return true;
1024 }
1025
1026 @Override
Patrick Scotteb5061b2009-11-18 15:00:30 -05001027 public void onRequestFocus(WebView view) {
1028 if (!mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -07001029 mWebViewController.switchToTab(Tab.this);
Patrick Scotteb5061b2009-11-18 15:00:30 -05001030 }
1031 }
1032
1033 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -07001034 public void onCloseWindow(WebView window) {
Michael Kolbc831b632011-05-11 09:30:34 -07001035 if (mParent != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001036 // JavaScript can only close popup window.
1037 if (mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -07001038 mWebViewController.switchToTab(mParent);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001039 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001040 mWebViewController.closeTab(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001041 }
1042 }
1043
1044 @Override
1045 public void onProgressChanged(WebView view, int newProgress) {
John Reck30c714c2010-12-16 17:30:34 -08001046 mPageLoadProgress = newProgress;
1047 mWebViewController.onProgressChanged(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001048 }
1049
1050 @Override
Leon Scroggins21d9b902010-03-11 09:33:11 -05001051 public void onReceivedTitle(WebView view, final String title) {
John Reck30c714c2010-12-16 17:30:34 -08001052 mCurrentState.mTitle = title;
Michael Kolb8233fac2010-10-26 16:08:53 -07001053 mWebViewController.onReceivedTitle(Tab.this, title);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001054 }
1055
1056 @Override
1057 public void onReceivedIcon(WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -08001058 mCurrentState.mFavicon = icon;
Michael Kolb8233fac2010-10-26 16:08:53 -07001059 mWebViewController.onFavicon(Tab.this, view, icon);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001060 }
1061
1062 @Override
1063 public void onReceivedTouchIconUrl(WebView view, String url,
1064 boolean precomposed) {
Michael Kolb14612442011-06-24 13:06:29 -07001065 final ContentResolver cr = mContext.getContentResolver();
Leon Scrogginsc8393d92010-04-23 14:58:16 -04001066 // Let precomposed icons take precedence over non-composed
1067 // icons.
1068 if (precomposed && mTouchIconLoader != null) {
1069 mTouchIconLoader.cancel(false);
1070 mTouchIconLoader = null;
1071 }
1072 // Have only one async task at a time.
1073 if (mTouchIconLoader == null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001074 mTouchIconLoader = new DownloadTouchIcon(Tab.this,
Michael Kolb14612442011-06-24 13:06:29 -07001075 mContext, cr, view);
Leon Scrogginsc8393d92010-04-23 14:58:16 -04001076 mTouchIconLoader.execute(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001077 }
1078 }
1079
1080 @Override
1081 public void onShowCustomView(View view,
1082 WebChromeClient.CustomViewCallback callback) {
Michael Kolb14612442011-06-24 13:06:29 -07001083 Activity activity = mWebViewController.getActivity();
1084 if (activity != null) {
1085 onShowCustomView(view, activity.getRequestedOrientation(), callback);
1086 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001087 }
1088
1089 @Override
1090 public void onShowCustomView(View view, int requestedOrientation,
1091 WebChromeClient.CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001092 if (mInForeground) mWebViewController.showCustomView(Tab.this, view,
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001093 requestedOrientation, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001094 }
1095
1096 @Override
1097 public void onHideCustomView() {
Michael Kolb8233fac2010-10-26 16:08:53 -07001098 if (mInForeground) mWebViewController.hideCustomView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001099 }
1100
1101 /**
1102 * The origin has exceeded its database quota.
1103 * @param url the URL that exceeded the quota
1104 * @param databaseIdentifier the identifier of the database on which the
1105 * transaction that caused the quota overflow was run
1106 * @param currentQuota the current quota for the origin.
1107 * @param estimatedSize the estimated size of the database.
1108 * @param totalUsedQuota is the sum of all origins' quota.
1109 * @param quotaUpdater The callback to run when a decision to allow or
1110 * deny quota has been made. Don't forget to call this!
1111 */
1112 @Override
1113 public void onExceededDatabaseQuota(String url,
1114 String databaseIdentifier, long currentQuota, long estimatedSize,
1115 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -07001116 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001117 .onExceededDatabaseQuota(url, databaseIdentifier,
1118 currentQuota, estimatedSize, totalUsedQuota,
1119 quotaUpdater);
1120 }
1121
1122 /**
1123 * The Application Cache has exceeded its max size.
1124 * @param spaceNeeded is the amount of disk space that would be needed
1125 * in order for the last appcache operation to succeed.
1126 * @param totalUsedQuota is the sum of all origins' quota.
1127 * @param quotaUpdater A callback to inform the WebCore thread that a
1128 * new app cache size is available. This callback must always
1129 * be executed at some point to ensure that the sleeping
1130 * WebCore thread is woken up.
1131 */
1132 @Override
1133 public void onReachedMaxAppCacheSize(long spaceNeeded,
1134 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -07001135 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001136 .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota,
1137 quotaUpdater);
1138 }
1139
1140 /**
1141 * Instructs the browser to show a prompt to ask the user to set the
1142 * Geolocation permission state for the specified origin.
1143 * @param origin The origin for which Geolocation permissions are
1144 * requested.
1145 * @param callback The callback to call once the user has set the
1146 * Geolocation permission state.
1147 */
1148 @Override
1149 public void onGeolocationPermissionsShowPrompt(String origin,
1150 GeolocationPermissions.Callback callback) {
1151 if (mInForeground) {
Grace Kloba50c241e2010-04-20 11:07:50 -07001152 getGeolocationPermissionsPrompt().show(origin, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001153 }
1154 }
1155
1156 /**
1157 * Instructs the browser to hide the Geolocation permissions prompt.
1158 */
1159 @Override
1160 public void onGeolocationPermissionsHidePrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001161 if (mInForeground && mGeolocationPermissionsPrompt != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001162 mGeolocationPermissionsPrompt.hide();
1163 }
1164 }
1165
Ben Murdoch65acc352009-11-19 18:16:04 +00001166 /* Adds a JavaScript error message to the system log and if the JS
1167 * console is enabled in the about:debug options, to that console
1168 * also.
Ben Murdochc42addf2010-01-28 15:19:59 +00001169 * @param consoleMessage the message object.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001170 */
1171 @Override
Ben Murdochc42addf2010-01-28 15:19:59 +00001172 public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001173 if (mInForeground) {
1174 // call getErrorConsole(true) so it will create one if needed
1175 ErrorConsoleView errorConsole = getErrorConsole(true);
Ben Murdochc42addf2010-01-28 15:19:59 +00001176 errorConsole.addErrorMessage(consoleMessage);
Michael Kolb8233fac2010-10-26 16:08:53 -07001177 if (mWebViewController.shouldShowErrorConsole()
1178 && errorConsole.getShowState() !=
1179 ErrorConsoleView.SHOW_MAXIMIZED) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001180 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
1181 }
1182 }
Ben Murdochc42addf2010-01-28 15:19:59 +00001183
Jeff Hamilton47654f42010-09-07 09:57:51 -05001184 // Don't log console messages in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001185 if (isPrivateBrowsingEnabled()) return true;
Jeff Hamilton47654f42010-09-07 09:57:51 -05001186
Ben Murdochc42addf2010-01-28 15:19:59 +00001187 String message = "Console: " + consoleMessage.message() + " "
1188 + consoleMessage.sourceId() + ":"
1189 + consoleMessage.lineNumber();
1190
1191 switch (consoleMessage.messageLevel()) {
1192 case TIP:
1193 Log.v(CONSOLE_LOGTAG, message);
1194 break;
1195 case LOG:
1196 Log.i(CONSOLE_LOGTAG, message);
1197 break;
1198 case WARNING:
1199 Log.w(CONSOLE_LOGTAG, message);
1200 break;
1201 case ERROR:
1202 Log.e(CONSOLE_LOGTAG, message);
1203 break;
1204 case DEBUG:
1205 Log.d(CONSOLE_LOGTAG, message);
1206 break;
1207 }
1208
1209 return true;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001210 }
1211
1212 /**
1213 * Ask the browser for an icon to represent a <video> element.
1214 * This icon will be used if the Web page did not specify a poster attribute.
1215 * @return Bitmap The icon or null if no such icon is available.
1216 */
1217 @Override
1218 public Bitmap getDefaultVideoPoster() {
1219 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001220 return mWebViewController.getDefaultVideoPoster();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001221 }
1222 return null;
1223 }
1224
1225 /**
1226 * Ask the host application for a custom progress view to show while
1227 * a <video> is loading.
1228 * @return View The progress view.
1229 */
1230 @Override
1231 public View getVideoLoadingProgressView() {
1232 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001233 return mWebViewController.getVideoLoadingProgressView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001234 }
1235 return null;
1236 }
1237
1238 @Override
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01001239 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001240 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001241 mWebViewController.openFileChooser(uploadMsg, acceptType);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001242 } else {
1243 uploadMsg.onReceiveValue(null);
1244 }
1245 }
1246
1247 /**
1248 * Deliver a list of already-visited URLs
1249 */
1250 @Override
1251 public void getVisitedHistory(final ValueCallback<String[]> callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001252 mWebViewController.getVisitedHistory(callback);
1253 }
Ben Murdoch8029a772010-11-16 11:58:21 +00001254
1255 @Override
1256 public void setupAutoFill(Message message) {
1257 // Prompt the user to set up their profile.
1258 final Message msg = message;
Michael Kolb14612442011-06-24 13:06:29 -07001259 AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
1260 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
Ben Murdoch1d676b62011-01-17 12:54:24 +00001261 Context.LAYOUT_INFLATER_SERVICE);
1262 final View layout = inflater.inflate(R.layout.setup_autofill_dialog, null);
1263
1264 builder.setView(layout)
Ben Murdochb7e6f942011-07-08 13:00:21 +01001265 .setTitle(R.string.autofill_setup_dialog_title)
Ben Murdoch1d676b62011-01-17 12:54:24 +00001266 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1267 @Override
1268 public void onClick(DialogInterface dialog, int id) {
1269 CheckBox disableAutoFill = (CheckBox) layout.findViewById(
1270 R.id.setup_autofill_dialog_disable_autofill);
1271
1272 if (disableAutoFill.isChecked()) {
1273 // Disable autofill and show a toast with how to turn it on again.
John Reck35e9dd62011-04-25 09:01:54 -07001274 mSettings.setAutofillEnabled(false);
Michael Kolb14612442011-06-24 13:06:29 -07001275 Toast.makeText(mContext,
Ben Murdoch1d676b62011-01-17 12:54:24 +00001276 R.string.autofill_setup_dialog_negative_toast,
1277 Toast.LENGTH_LONG).show();
1278 } else {
1279 // Take user to the AutoFill profile editor. When they return,
1280 // we will send the message that we pass here which will trigger
1281 // the form to get filled out with their new profile.
1282 mWebViewController.setupAutoFill(msg);
1283 }
1284 }
1285 })
1286 .setNegativeButton(R.string.cancel, null)
1287 .show();
Ben Murdoch8029a772010-11-16 11:58:21 +00001288 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001289 };
1290
1291 // -------------------------------------------------------------------------
1292 // WebViewClient implementation for the sub window
1293 // -------------------------------------------------------------------------
1294
1295 // Subclass of WebViewClient used in subwindows to notify the main
1296 // WebViewClient of certain WebView activities.
1297 private static class SubWindowClient extends WebViewClient {
1298 // The main WebViewClient.
1299 private final WebViewClient mClient;
Michael Kolb8233fac2010-10-26 16:08:53 -07001300 private final WebViewController mController;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001301
Michael Kolb8233fac2010-10-26 16:08:53 -07001302 SubWindowClient(WebViewClient client, WebViewController controller) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001303 mClient = client;
Michael Kolb8233fac2010-10-26 16:08:53 -07001304 mController = controller;
Leon Scroggins III211ba542010-04-19 13:21:13 -04001305 }
1306 @Override
1307 public void onPageStarted(WebView view, String url, Bitmap favicon) {
1308 // Unlike the others, do not call mClient's version, which would
1309 // change the progress bar. However, we do want to remove the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001310 // find or select dialog.
Michael Kolb8233fac2010-10-26 16:08:53 -07001311 mController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001312 }
1313 @Override
1314 public void doUpdateVisitedHistory(WebView view, String url,
1315 boolean isReload) {
1316 mClient.doUpdateVisitedHistory(view, url, isReload);
1317 }
1318 @Override
1319 public boolean shouldOverrideUrlLoading(WebView view, String url) {
1320 return mClient.shouldOverrideUrlLoading(view, url);
1321 }
1322 @Override
1323 public void onReceivedSslError(WebView view, SslErrorHandler handler,
1324 SslError error) {
1325 mClient.onReceivedSslError(view, handler, error);
1326 }
1327 @Override
Brian Carlstrom8862c1d2011-06-02 01:05:55 -07001328 public void onReceivedClientCertRequest(WebView view,
1329 ClientCertRequestHandler handler, String host_and_port) {
1330 mClient.onReceivedClientCertRequest(view, handler, host_and_port);
1331 }
1332 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -07001333 public void onReceivedHttpAuthRequest(WebView view,
1334 HttpAuthHandler handler, String host, String realm) {
1335 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
1336 }
1337 @Override
1338 public void onFormResubmission(WebView view, Message dontResend,
1339 Message resend) {
1340 mClient.onFormResubmission(view, dontResend, resend);
1341 }
1342 @Override
1343 public void onReceivedError(WebView view, int errorCode,
1344 String description, String failingUrl) {
1345 mClient.onReceivedError(view, errorCode, description, failingUrl);
1346 }
1347 @Override
1348 public boolean shouldOverrideKeyEvent(WebView view,
1349 android.view.KeyEvent event) {
1350 return mClient.shouldOverrideKeyEvent(view, event);
1351 }
1352 @Override
1353 public void onUnhandledKeyEvent(WebView view,
1354 android.view.KeyEvent event) {
1355 mClient.onUnhandledKeyEvent(view, event);
1356 }
1357 }
1358
1359 // -------------------------------------------------------------------------
1360 // WebChromeClient implementation for the sub window
1361 // -------------------------------------------------------------------------
1362
1363 private class SubWindowChromeClient extends WebChromeClient {
1364 // The main WebChromeClient.
1365 private final WebChromeClient mClient;
1366
1367 SubWindowChromeClient(WebChromeClient client) {
1368 mClient = client;
1369 }
1370 @Override
1371 public void onProgressChanged(WebView view, int newProgress) {
1372 mClient.onProgressChanged(view, newProgress);
1373 }
1374 @Override
1375 public boolean onCreateWindow(WebView view, boolean dialog,
1376 boolean userGesture, android.os.Message resultMsg) {
1377 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
1378 }
1379 @Override
1380 public void onCloseWindow(WebView window) {
1381 if (window != mSubView) {
1382 Log.e(LOGTAG, "Can't close the window");
1383 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001384 mWebViewController.dismissSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001385 }
1386 }
1387
1388 // -------------------------------------------------------------------------
1389
1390 // Construct a new tab
Michael Kolb7bcafde2011-05-09 13:55:59 -07001391 Tab(WebViewController wvcontroller, WebView w) {
John Reck1cf4b792011-07-26 10:22:22 -07001392 this(wvcontroller, w, null);
1393 }
1394
1395 Tab(WebViewController wvcontroller, Bundle state) {
1396 this(wvcontroller, null, state);
1397 }
1398
1399 Tab(WebViewController wvcontroller, WebView w, Bundle state) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001400 mWebViewController = wvcontroller;
Michael Kolb14612442011-06-24 13:06:29 -07001401 mContext = mWebViewController.getContext();
John Reck35e9dd62011-04-25 09:01:54 -07001402 mSettings = BrowserSettings.getInstance();
Michael Kolb14612442011-06-24 13:06:29 -07001403 mDataController = DataController.getInstance(mContext);
1404 mCurrentState = new PageState(mContext, w != null
John Reck847b5322011-04-14 17:02:18 -07001405 ? w.isPrivateBrowsingEnabled() : false);
Michael Kolb8233fac2010-10-26 16:08:53 -07001406 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001407 mInForeground = false;
1408
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001409 mDownloadListener = new DownloadListener() {
1410 public void onDownloadStart(String url, String userAgent,
1411 String contentDisposition, String mimetype,
1412 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001413 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001414 mimetype, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001415 }
1416 };
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001417 mWebBackForwardListClient = new WebBackForwardListClient() {
1418 @Override
1419 public void onNewHistoryItem(WebHistoryItem item) {
1420 if (isInVoiceSearchMode()) {
1421 item.setCustomData(mVoiceSearchData.mVoiceSearchIntent);
1422 }
Mathew Inwooda829d552011-09-02 14:16:25 +01001423 if (mClearHistoryUrlPattern != null) {
1424 boolean match =
1425 mClearHistoryUrlPattern.matcher(item.getOriginalUrl()).matches();
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001426 if (LOGD_ENABLED) {
Mathew Inwooda829d552011-09-02 14:16:25 +01001427 Log.d(LOGTAG, "onNewHistoryItem: match=" + match + "\n\t"
1428 + item.getUrl() + "\n\t"
1429 + mClearHistoryUrlPattern);
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001430 }
Mathew Inwooda829d552011-09-02 14:16:25 +01001431 if (match) {
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001432 if (mMainView != null) {
1433 mMainView.clearHistory();
1434 }
1435 }
Mathew Inwooda829d552011-09-02 14:16:25 +01001436 mClearHistoryUrlPattern = null;
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001437 }
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001438 }
1439 @Override
1440 public void onIndexChanged(WebHistoryItem item, int index) {
1441 Object data = item.getCustomData();
1442 if (data != null && data instanceof Intent) {
1443 activateVoiceSearchMode((Intent) data);
1444 }
1445 }
1446 };
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001447
John Reck1cf4b792011-07-26 10:22:22 -07001448 mCaptureWidth = mContext.getResources().getDimensionPixelSize(
1449 R.dimen.tab_thumbnail_width);
1450 mCaptureHeight = mContext.getResources().getDimensionPixelSize(
1451 R.dimen.tab_thumbnail_height);
1452 updateShouldCaptureThumbnails();
1453 restoreState(state);
John Reck52be4782011-08-26 15:37:29 -07001454 if (getId() == -1) {
1455 mId = TabControl.getNextId();
1456 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001457 setWebView(w);
Michael Kolb9ef259a2011-07-12 15:33:08 -07001458 mHandler = new Handler() {
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001459 @Override
Michael Kolb9ef259a2011-07-12 15:33:08 -07001460 public void handleMessage(Message m) {
John Reck1cf4b792011-07-26 10:22:22 -07001461 switch (m.what) {
1462 case MSG_CAPTURE:
1463 capture();
1464 break;
1465 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001466 }
1467 };
John Reck1cf4b792011-07-26 10:22:22 -07001468 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001469
Mathew Inwoode09305e2011-09-02 12:03:26 +01001470 /**
1471 * This is used to get a new ID when the tab has been preloaded, before it is displayed and
1472 * added to TabControl. Preloaded tabs can be created before restoreInstanceState, leading
1473 * to overlapping IDs between the preloaded and restored tabs.
1474 */
1475 public void refreshIdAfterPreload() {
1476 mId = TabControl.getNextId();
1477 }
1478
John Reck1cf4b792011-07-26 10:22:22 -07001479 public void updateShouldCaptureThumbnails() {
1480 if (mWebViewController.shouldCaptureThumbnails()) {
1481 synchronized (Tab.this) {
1482 if (mCapture == null) {
1483 mCapture = Bitmap.createBitmap(mCaptureWidth, mCaptureHeight,
1484 Bitmap.Config.RGB_565);
Michael Kolbc3af0672011-08-09 10:24:41 -07001485 mCapture.eraseColor(Color.WHITE);
John Reck1cf4b792011-07-26 10:22:22 -07001486 if (mInForeground) {
1487 postCapture();
1488 }
1489 }
1490 }
1491 } else {
1492 synchronized (Tab.this) {
1493 mCapture = null;
1494 deleteThumbnail();
1495 }
1496 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001497 }
1498
Michael Kolb14612442011-06-24 13:06:29 -07001499 public void setController(WebViewController ctl) {
1500 mWebViewController = ctl;
John Reck1cf4b792011-07-26 10:22:22 -07001501 updateShouldCaptureThumbnails();
Michael Kolb14612442011-06-24 13:06:29 -07001502 }
1503
Michael Kolbc831b632011-05-11 09:30:34 -07001504 public long getId() {
1505 return mId;
1506 }
1507
Grace Kloba22ac16e2009-10-07 18:00:23 -07001508 /**
1509 * Sets the WebView for this tab, correctly removing the old WebView from
1510 * the container view.
1511 */
1512 void setWebView(WebView w) {
1513 if (mMainView == w) {
1514 return;
1515 }
Michael Kolba713ec82010-11-29 17:27:06 -08001516
Grace Kloba22ac16e2009-10-07 18:00:23 -07001517 // If the WebView is changing, the page will be reloaded, so any ongoing
1518 // Geolocation permission requests are void.
Grace Kloba50c241e2010-04-20 11:07:50 -07001519 if (mGeolocationPermissionsPrompt != null) {
1520 mGeolocationPermissionsPrompt.hide();
1521 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001522
Michael Kolba713ec82010-11-29 17:27:06 -08001523 mWebViewController.onSetWebView(this, w);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001524
John Reck1cf4b792011-07-26 10:22:22 -07001525 if (mMainView != null) {
John Reck8ee633f2011-08-09 16:00:35 -07001526 mMainView.setPictureListener(null);
John Reck1cf4b792011-07-26 10:22:22 -07001527 if (w != null) {
1528 syncCurrentState(w, null);
1529 } else {
1530 mCurrentState = new PageState(mContext, false);
1531 }
1532 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001533 // set the new one
1534 mMainView = w;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001535 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001536 if (mMainView != null) {
1537 mMainView.setWebViewClient(mWebViewClient);
1538 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001539 // Attach DownloadManager so that downloads can start in an active
1540 // or a non-active window. This can happen when going to a site that
1541 // does a redirect after a period of time. The user could have
1542 // switched to another tab while waiting for the download to start.
1543 mMainView.setDownloadListener(mDownloadListener);
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001544 mMainView.setWebBackForwardListClient(mWebBackForwardListClient);
John Reck8ee633f2011-08-09 16:00:35 -07001545 TabControl tc = mWebViewController.getTabControl();
1546 if (tc != null && tc.getOnThumbnailUpdatedListener() != null) {
1547 mMainView.setPictureListener(this);
1548 }
John Reck1cf4b792011-07-26 10:22:22 -07001549 if (mSavedState != null) {
John Reck6c2e2f32011-08-22 13:41:23 -07001550 WebBackForwardList restoredState
1551 = mMainView.restoreState(mSavedState);
1552 if (restoredState == null || restoredState.getSize() == 0) {
1553 Log.w(LOGTAG, "Failed to restore WebView state!");
1554 loadUrl(mCurrentState.mOriginalUrl, null);
1555 }
John Reck1cf4b792011-07-26 10:22:22 -07001556 mSavedState = null;
1557 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001558 }
1559 }
1560
1561 /**
1562 * Destroy the tab's main WebView and subWindow if any
1563 */
1564 void destroy() {
1565 if (mMainView != null) {
1566 dismissSubWindow();
John Reckef654f12011-07-12 16:42:08 -07001567 // Make sure the embedded title bar isn't still attached
1568 mMainView.setEmbeddedTitleBar(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001569 // save the WebView to call destroy() after detach it from the tab
1570 WebView webView = mMainView;
1571 setWebView(null);
1572 webView.destroy();
1573 }
1574 }
1575
1576 /**
1577 * Remove the tab from the parent
1578 */
1579 void removeFromTree() {
1580 // detach the children
Michael Kolbc831b632011-05-11 09:30:34 -07001581 if (mChildren != null) {
1582 for(Tab t : mChildren) {
1583 t.setParent(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001584 }
1585 }
1586 // remove itself from the parent list
Michael Kolbc831b632011-05-11 09:30:34 -07001587 if (mParent != null) {
1588 mParent.mChildren.remove(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001589 }
John Reck1cf4b792011-07-26 10:22:22 -07001590 deleteThumbnail();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001591 }
1592
1593 /**
1594 * Create a new subwindow unless a subwindow already exists.
1595 * @return True if a new subwindow was created. False if one already exists.
1596 */
1597 boolean createSubWindow() {
1598 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001599 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001600 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001601 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001602 mSubView.setWebChromeClient(new SubWindowChromeClient(
1603 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001604 // Set a different DownloadListener for the mSubView, since it will
1605 // just need to dismiss the mSubView, rather than close the Tab
1606 mSubView.setDownloadListener(new DownloadListener() {
1607 public void onDownloadStart(String url, String userAgent,
1608 String contentDisposition, String mimetype,
1609 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001610 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001611 contentDisposition, mimetype, contentLength);
1612 if (mSubView.copyBackForwardList().getSize() == 0) {
1613 // This subwindow was opened for the sole purpose of
1614 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001615 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001616 }
1617 }
1618 });
Michael Kolb14612442011-06-24 13:06:29 -07001619 mSubView.setOnCreateContextMenuListener(mWebViewController.getActivity());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001620 return true;
1621 }
1622 return false;
1623 }
1624
1625 /**
1626 * Dismiss the subWindow for the tab.
1627 */
1628 void dismissSubWindow() {
1629 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001630 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001631 mSubView.destroy();
1632 mSubView = null;
1633 mSubViewContainer = null;
1634 }
1635 }
1636
Grace Kloba22ac16e2009-10-07 18:00:23 -07001637
1638 /**
1639 * Set the parent tab of this tab.
1640 */
Michael Kolbc831b632011-05-11 09:30:34 -07001641 void setParent(Tab parent) {
John Reck52be4782011-08-26 15:37:29 -07001642 if (parent == this) {
1643 throw new IllegalStateException("Cannot set parent to self!");
1644 }
Michael Kolbc831b632011-05-11 09:30:34 -07001645 mParent = parent;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001646 // This tab may have been freed due to low memory. If that is the case,
Michael Kolbc831b632011-05-11 09:30:34 -07001647 // the parent tab id is already saved. If we are changing that id
Grace Kloba22ac16e2009-10-07 18:00:23 -07001648 // (most likely due to removing the parent tab) we must update the
Michael Kolbc831b632011-05-11 09:30:34 -07001649 // parent tab id in the saved Bundle.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001650 if (mSavedState != null) {
1651 if (parent == null) {
1652 mSavedState.remove(PARENTTAB);
1653 } else {
Michael Kolbc831b632011-05-11 09:30:34 -07001654 mSavedState.putLong(PARENTTAB, parent.getId());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001655 }
1656 }
John Reckb0a86db2011-05-24 14:05:58 -07001657
1658 // Sync the WebView useragent with the parent
1659 if (parent != null && mSettings.hasDesktopUseragent(parent.getWebView())
1660 != mSettings.hasDesktopUseragent(getWebView())) {
1661 mSettings.toggleDesktopUseragent(getWebView());
1662 }
John Reck52be4782011-08-26 15:37:29 -07001663
1664 if (parent != null && parent.getId() == getId()) {
1665 throw new IllegalStateException("Parent has same ID as child!");
1666 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001667 }
1668
1669 /**
Michael Kolbc831b632011-05-11 09:30:34 -07001670 * If this Tab was created through another Tab, then this method returns
1671 * that Tab.
1672 * @return the Tab parent or null
1673 */
1674 public Tab getParent() {
1675 return mParent;
1676 }
1677
1678 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001679 * When a Tab is created through the content of another Tab, then we
1680 * associate the Tabs.
1681 * @param child the Tab that was created from this Tab
1682 */
1683 void addChildTab(Tab child) {
Michael Kolbc831b632011-05-11 09:30:34 -07001684 if (mChildren == null) {
1685 mChildren = new Vector<Tab>();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001686 }
Michael Kolbc831b632011-05-11 09:30:34 -07001687 mChildren.add(child);
1688 child.setParent(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001689 }
1690
Michael Kolbc831b632011-05-11 09:30:34 -07001691 Vector<Tab> getChildren() {
1692 return mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001693 }
1694
1695 void resume() {
1696 if (mMainView != null) {
John Reck56c1fcf2011-08-17 10:15:16 -07001697 setupHwAcceleration(mMainView);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001698 mMainView.onResume();
1699 if (mSubView != null) {
1700 mSubView.onResume();
1701 }
1702 }
1703 }
1704
John Reck56c1fcf2011-08-17 10:15:16 -07001705 private void setupHwAcceleration(View web) {
1706 if (web == null) return;
1707 BrowserSettings settings = BrowserSettings.getInstance();
1708 if (settings.isHardwareAccelerated()) {
1709 web.setLayerType(View.LAYER_TYPE_NONE, null);
1710 } else {
1711 web.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
1712 }
1713 }
1714
Grace Kloba22ac16e2009-10-07 18:00:23 -07001715 void pause() {
1716 if (mMainView != null) {
1717 mMainView.onPause();
1718 if (mSubView != null) {
1719 mSubView.onPause();
1720 }
1721 }
1722 }
1723
1724 void putInForeground() {
John Reck8ee633f2011-08-09 16:00:35 -07001725 if (mInForeground) {
1726 return;
1727 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001728 mInForeground = true;
1729 resume();
Michael Kolb14612442011-06-24 13:06:29 -07001730 Activity activity = mWebViewController.getActivity();
1731 mMainView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001732 if (mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -07001733 mSubView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001734 }
1735 // Show the pending error dialog if the queue is not empty
1736 if (mQueuedErrors != null && mQueuedErrors.size() > 0) {
1737 showError(mQueuedErrors.getFirst());
1738 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001739 mWebViewController.bookmarkedStatusHasChanged(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001740 }
1741
1742 void putInBackground() {
John Reck8ee633f2011-08-09 16:00:35 -07001743 if (!mInForeground) {
1744 return;
1745 }
John Reck52be4782011-08-26 15:37:29 -07001746 capture();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001747 mInForeground = false;
1748 pause();
1749 mMainView.setOnCreateContextMenuListener(null);
1750 if (mSubView != null) {
1751 mSubView.setOnCreateContextMenuListener(null);
1752 }
1753 }
1754
Michael Kolb8233fac2010-10-26 16:08:53 -07001755 boolean inForeground() {
1756 return mInForeground;
1757 }
1758
Grace Kloba22ac16e2009-10-07 18:00:23 -07001759 /**
1760 * Return the top window of this tab; either the subwindow if it is not
1761 * null or the main window.
1762 * @return The top window of this tab.
1763 */
1764 WebView getTopWindow() {
1765 if (mSubView != null) {
1766 return mSubView;
1767 }
1768 return mMainView;
1769 }
1770
1771 /**
1772 * Return the main window of this tab. Note: if a tab is freed in the
1773 * background, this can return null. It is only guaranteed to be
1774 * non-null for the current tab.
1775 * @return The main WebView of this tab.
1776 */
1777 WebView getWebView() {
1778 return mMainView;
1779 }
1780
Michael Kolba713ec82010-11-29 17:27:06 -08001781 void setViewContainer(View container) {
1782 mContainer = container;
1783 }
1784
Michael Kolb8233fac2010-10-26 16:08:53 -07001785 View getViewContainer() {
1786 return mContainer;
1787 }
1788
Grace Kloba22ac16e2009-10-07 18:00:23 -07001789 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001790 * Return whether private browsing is enabled for the main window of
1791 * this tab.
1792 * @return True if private browsing is enabled.
1793 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001794 boolean isPrivateBrowsingEnabled() {
John Reck502a3532011-08-16 14:21:46 -07001795 return mCurrentState.mIncognito;
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001796 }
1797
1798 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001799 * Return the subwindow of this tab or null if there is no subwindow.
1800 * @return The subwindow of this tab or null.
1801 */
1802 WebView getSubWebView() {
1803 return mSubView;
1804 }
1805
Michael Kolb1514bb72010-11-22 09:11:48 -08001806 void setSubWebView(WebView subView) {
1807 mSubView = subView;
1808 }
1809
Michael Kolb8233fac2010-10-26 16:08:53 -07001810 View getSubViewContainer() {
1811 return mSubViewContainer;
1812 }
1813
Michael Kolb1514bb72010-11-22 09:11:48 -08001814 void setSubViewContainer(View subViewContainer) {
1815 mSubViewContainer = subViewContainer;
1816 }
1817
Grace Kloba22ac16e2009-10-07 18:00:23 -07001818 /**
1819 * @return The geolocation permissions prompt for this tab.
1820 */
1821 GeolocationPermissionsPrompt getGeolocationPermissionsPrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001822 if (mGeolocationPermissionsPrompt == null) {
1823 ViewStub stub = (ViewStub) mContainer
1824 .findViewById(R.id.geolocation_permissions_prompt);
1825 mGeolocationPermissionsPrompt = (GeolocationPermissionsPrompt) stub
1826 .inflate();
Grace Kloba50c241e2010-04-20 11:07:50 -07001827 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001828 return mGeolocationPermissionsPrompt;
1829 }
1830
1831 /**
1832 * @return The application id string
1833 */
1834 String getAppId() {
1835 return mAppId;
1836 }
1837
1838 /**
1839 * Set the application id string
1840 * @param id
1841 */
1842 void setAppId(String id) {
1843 mAppId = id;
1844 }
1845
Michael Kolbe28b3472011-08-04 16:54:31 -07001846 boolean closeOnBack() {
1847 return mCloseOnBack;
1848 }
1849
1850 void setCloseOnBack(boolean close) {
1851 mCloseOnBack = close;
1852 }
1853
Grace Kloba22ac16e2009-10-07 18:00:23 -07001854 String getUrl() {
John Reck324d4402011-01-11 16:56:42 -08001855 return UrlUtils.filteredUrl(mCurrentState.mUrl);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001856 }
1857
John Reck49a603c2011-03-03 09:33:05 -08001858 String getOriginalUrl() {
John Reckdb22ec42011-06-29 11:31:24 -07001859 if (mCurrentState.mOriginalUrl == null) {
1860 return getUrl();
John Reck49a603c2011-03-03 09:33:05 -08001861 }
John Reckdb22ec42011-06-29 11:31:24 -07001862 return UrlUtils.filteredUrl(mCurrentState.mOriginalUrl);
John Reck49a603c2011-03-03 09:33:05 -08001863 }
1864
Grace Kloba22ac16e2009-10-07 18:00:23 -07001865 /**
John Reck30c714c2010-12-16 17:30:34 -08001866 * Get the title of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001867 */
1868 String getTitle() {
John Reck30c714c2010-12-16 17:30:34 -08001869 if (mCurrentState.mTitle == null && mInPageLoad) {
Michael Kolb14612442011-06-24 13:06:29 -07001870 return mContext.getString(R.string.title_bar_loading);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001871 }
John Reck30c714c2010-12-16 17:30:34 -08001872 return mCurrentState.mTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001873 }
1874
1875 /**
John Reck30c714c2010-12-16 17:30:34 -08001876 * Get the favicon of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001877 */
1878 Bitmap getFavicon() {
John Reck1cf4b792011-07-26 10:22:22 -07001879 if (mCurrentState.mFavicon != null) {
1880 return mCurrentState.mFavicon;
1881 }
1882 return getDefaultFavicon(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001883 }
1884
John Recke969cc52010-12-21 17:24:43 -08001885 public boolean isBookmarkedSite() {
1886 return mCurrentState.mIsBookmarkedSite;
1887 }
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001888
Grace Kloba22ac16e2009-10-07 18:00:23 -07001889 /**
1890 * Return the tab's error console. Creates the console if createIfNEcessary
1891 * is true and we haven't already created the console.
1892 * @param createIfNecessary Flag to indicate if the console should be
1893 * created if it has not been already.
1894 * @return The tab's error console, or null if one has not been created and
1895 * createIfNecessary is false.
1896 */
1897 ErrorConsoleView getErrorConsole(boolean createIfNecessary) {
1898 if (createIfNecessary && mErrorConsole == null) {
Michael Kolb14612442011-06-24 13:06:29 -07001899 mErrorConsole = new ErrorConsoleView(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001900 mErrorConsole.setWebView(mMainView);
1901 }
1902 return mErrorConsole;
1903 }
1904
John Reck30c714c2010-12-16 17:30:34 -08001905 private void setLockIconType(LockIcon icon) {
1906 mCurrentState.mLockIcon = icon;
1907 mWebViewController.onUpdatedLockIcon(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001908 }
1909
1910 /**
1911 * @return The tab's lock icon type.
1912 */
John Reck30c714c2010-12-16 17:30:34 -08001913 LockIcon getLockIconType() {
1914 return mCurrentState.mLockIcon;
1915 }
1916
1917 int getLoadProgress() {
1918 if (mInPageLoad) {
1919 return mPageLoadProgress;
1920 }
1921 return 100;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001922 }
1923
1924 /**
1925 * @return TRUE if onPageStarted is called while onPageFinished is not
1926 * called yet.
1927 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001928 boolean inPageLoad() {
1929 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001930 }
1931
1932 // force mInLoad to be false. This should only be called before closing the
1933 // tab to ensure BrowserActivity's pauseWebViewTimers() is called correctly.
Michael Kolb8233fac2010-10-26 16:08:53 -07001934 void clearInPageLoad() {
1935 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001936 }
1937
Grace Kloba22ac16e2009-10-07 18:00:23 -07001938 /**
John Reck1cf4b792011-07-26 10:22:22 -07001939 * @return The Bundle with the tab's state if it can be saved, otherwise null
Grace Kloba22ac16e2009-10-07 18:00:23 -07001940 */
John Reck1cf4b792011-07-26 10:22:22 -07001941 public Bundle saveState() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001942 // If the WebView is null it means we ran low on memory and we already
1943 // stored the saved state in mSavedState.
1944 if (mMainView == null) {
John Reck1cf4b792011-07-26 10:22:22 -07001945 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001946 }
John Reck6c2e2f32011-08-22 13:41:23 -07001947
1948 if (TextUtils.isEmpty(mCurrentState.mUrl)) {
John Reck1cf4b792011-07-26 10:22:22 -07001949 return null;
John Reck24f18262011-06-17 14:47:20 -07001950 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001951
1952 mSavedState = new Bundle();
John Reck6c2e2f32011-08-22 13:41:23 -07001953 WebBackForwardList savedList = mMainView.saveState(mSavedState);
1954 if (savedList == null || savedList.getSize() == 0) {
1955 Log.w(LOGTAG, "Failed to save back/forward list for "
1956 + mCurrentState.mUrl);
1957 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001958
Michael Kolbc831b632011-05-11 09:30:34 -07001959 mSavedState.putLong(ID, mId);
John Reck30c714c2010-12-16 17:30:34 -08001960 mSavedState.putString(CURRURL, mCurrentState.mUrl);
1961 mSavedState.putString(CURRTITLE, mCurrentState.mTitle);
John Reck1cf4b792011-07-26 10:22:22 -07001962 mSavedState.putBoolean(INCOGNITO, mMainView.isPrivateBrowsingEnabled());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001963 if (mAppId != null) {
1964 mSavedState.putString(APPID, mAppId);
1965 }
Michael Kolbe28b3472011-08-04 16:54:31 -07001966 mSavedState.putBoolean(CLOSEFLAG, mCloseOnBack);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001967 // Remember the parent tab so the relationship can be restored.
Michael Kolbc831b632011-05-11 09:30:34 -07001968 if (mParent != null) {
1969 mSavedState.putLong(PARENTTAB, mParent.mId);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001970 }
John Reckb0a86db2011-05-24 14:05:58 -07001971 mSavedState.putBoolean(USERAGENT,
1972 mSettings.hasDesktopUseragent(getWebView()));
John Reck1cf4b792011-07-26 10:22:22 -07001973 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001974 }
1975
1976 /*
1977 * Restore the state of the tab.
1978 */
John Reck1cf4b792011-07-26 10:22:22 -07001979 private void restoreState(Bundle b) {
1980 mSavedState = b;
1981 if (mSavedState == null) {
1982 return;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001983 }
1984 // Restore the internal state even if the WebView fails to restore.
1985 // This will maintain the app id, original url and close-on-exit values.
Michael Kolbc831b632011-05-11 09:30:34 -07001986 mId = b.getLong(ID);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001987 mAppId = b.getString(APPID);
Michael Kolbe28b3472011-08-04 16:54:31 -07001988 mCloseOnBack = b.getBoolean(CLOSEFLAG);
John Reckb0a86db2011-05-24 14:05:58 -07001989 if (b.getBoolean(USERAGENT)
1990 != mSettings.hasDesktopUseragent(getWebView())) {
1991 mSettings.toggleDesktopUseragent(getWebView());
1992 }
John Reck1cf4b792011-07-26 10:22:22 -07001993 String url = b.getString(CURRURL);
1994 String title = b.getString(CURRTITLE);
1995 boolean incognito = b.getBoolean(INCOGNITO);
1996 mCurrentState = new PageState(mContext, incognito, url, null);
1997 mCurrentState.mTitle = title;
1998 synchronized (Tab.this) {
1999 if (mCapture != null) {
2000 BackgroundHandler.execute(mLoadThumbnail);
2001 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002002 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07002003 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04002004
Leon Scroggins1961ed22010-12-07 15:22:21 -05002005 public void updateBookmarkedStatus() {
John Recke969cc52010-12-21 17:24:43 -08002006 mDataController.queryBookmarkStatus(getUrl(), mIsBookmarkCallback);
Leon Scroggins1961ed22010-12-07 15:22:21 -05002007 }
2008
John Recke969cc52010-12-21 17:24:43 -08002009 private DataController.OnQueryUrlIsBookmark mIsBookmarkCallback
2010 = new DataController.OnQueryUrlIsBookmark() {
2011 @Override
2012 public void onQueryUrlIsBookmark(String url, boolean isBookmark) {
2013 if (mCurrentState.mUrl.equals(url)) {
2014 mCurrentState.mIsBookmarkedSite = isBookmark;
2015 mWebViewController.bookmarkedStatusHasChanged(Tab.this);
2016 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05002017 }
John Recke969cc52010-12-21 17:24:43 -08002018 };
Michael Kolb1acef692011-03-08 14:12:06 -08002019
Michael Kolbeb95db42011-03-03 10:38:40 -08002020 public Bitmap getScreenshot() {
John Reck1cf4b792011-07-26 10:22:22 -07002021 synchronized (Tab.this) {
2022 return mCapture;
2023 }
Michael Kolbeb95db42011-03-03 10:38:40 -08002024 }
2025
John Reck541f55a2011-06-07 16:34:43 -07002026 public boolean isSnapshot() {
John Reck541f55a2011-06-07 16:34:43 -07002027 return false;
2028 }
2029
John Reckd8c74522011-06-14 08:45:00 -07002030 public ContentValues createSnapshotValues() {
2031 if (mMainView == null) return null;
John Reck8cc92352011-07-06 17:41:52 -07002032 ByteArrayOutputStream bos = new ByteArrayOutputStream();
2033 try {
2034 GZIPOutputStream stream = new GZIPOutputStream(bos);
2035 if (!mMainView.saveViewState(stream)) {
2036 return null;
2037 }
2038 stream.flush();
2039 stream.close();
2040 } catch (Exception e) {
2041 Log.w(LOGTAG, "Failed to save view state", e);
John Reck541f55a2011-06-07 16:34:43 -07002042 return null;
2043 }
John Reck8cc92352011-07-06 17:41:52 -07002044 byte[] data = bos.toByteArray();
John Reckd8c74522011-06-14 08:45:00 -07002045 ContentValues values = new ContentValues();
2046 values.put(Snapshots.TITLE, mCurrentState.mTitle);
2047 values.put(Snapshots.URL, mCurrentState.mUrl);
2048 values.put(Snapshots.VIEWSTATE, data);
2049 values.put(Snapshots.BACKGROUND, mMainView.getPageBackgroundColor());
John Reck8cc92352011-07-06 17:41:52 -07002050 values.put(Snapshots.DATE_CREATED, System.currentTimeMillis());
2051 values.put(Snapshots.FAVICON, compressBitmap(getFavicon()));
2052 Bitmap screenshot = Controller.createScreenshot(mMainView,
2053 Controller.getDesiredThumbnailWidth(mContext),
2054 Controller.getDesiredThumbnailHeight(mContext));
2055 values.put(Snapshots.THUMBNAIL, compressBitmap(screenshot));
John Reckd8c74522011-06-14 08:45:00 -07002056 return values;
John Reck541f55a2011-06-07 16:34:43 -07002057 }
2058
John Reck8cc92352011-07-06 17:41:52 -07002059 public byte[] compressBitmap(Bitmap bitmap) {
2060 if (bitmap == null) {
2061 return null;
2062 }
2063 ByteArrayOutputStream stream = new ByteArrayOutputStream();
2064 bitmap.compress(CompressFormat.PNG, 100, stream);
2065 return stream.toByteArray();
2066 }
2067
John Reck26b18322011-06-21 13:08:58 -07002068 public void loadUrl(String url, Map<String, String> headers) {
2069 if (mMainView != null) {
Michael Kolb14612442011-06-24 13:06:29 -07002070 mCurrentState = new PageState(mContext, false, url, null);
John Reck26b18322011-06-21 13:08:58 -07002071 mWebViewController.onPageStarted(this, mMainView, null);
2072 mMainView.loadUrl(url, headers);
Mathew Inwoodb4e831b2011-09-05 18:58:48 +01002073 clearPageLoadCompleteListener();
John Reck26b18322011-06-21 13:08:58 -07002074 }
2075 }
2076
Michael Kolb9ef259a2011-07-12 15:33:08 -07002077 protected void capture() {
2078 if (mMainView == null || mCapture == null) return;
2079 Canvas c = new Canvas(mCapture);
2080 final int left = mMainView.getScrollX();
2081 final int top = mMainView.getScrollY() + mMainView.getVisibleTitleHeight();
2082 c.translate(-left, -top);
2083 float scale = mCaptureWidth / (float) mMainView.getWidth();
2084 c.scale(scale, scale, left, top);
John Reck8ee633f2011-08-09 16:00:35 -07002085 if (mMainView instanceof BrowserWebView) {
2086 ((BrowserWebView)mMainView).drawContent(c);
2087 } else {
2088 mMainView.draw(c);
2089 }
Dianne Hackborn43cfe8a2011-08-02 16:59:35 -07002090 c.setBitmap(null);
John Reck8ee633f2011-08-09 16:00:35 -07002091 mHandler.removeMessages(MSG_CAPTURE);
John Reck1cf4b792011-07-26 10:22:22 -07002092 persistThumbnail();
John Reck8ee633f2011-08-09 16:00:35 -07002093 TabControl tc = mWebViewController.getTabControl();
2094 if (tc != null) {
2095 OnThumbnailUpdatedListener updateListener
2096 = tc.getOnThumbnailUpdatedListener();
2097 if (updateListener != null) {
2098 updateListener.onThumbnailUpdated(this);
2099 }
2100 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07002101 }
2102
2103 @Override
2104 public void onNewPicture(WebView view, Picture picture) {
2105 //update screenshot
John Reck1cf4b792011-07-26 10:22:22 -07002106 postCapture();
2107 }
2108
2109 private void postCapture() {
Michael Kolb9ef259a2011-07-12 15:33:08 -07002110 if (!mHandler.hasMessages(MSG_CAPTURE)) {
2111 mHandler.sendEmptyMessageDelayed(MSG_CAPTURE, CAPTURE_DELAY);
2112 }
2113 }
2114
John Reckef654f12011-07-12 16:42:08 -07002115 public boolean canGoBack() {
2116 return mMainView != null ? mMainView.canGoBack() : false;
2117 }
2118
2119 public boolean canGoForward() {
2120 return mMainView != null ? mMainView.canGoForward() : false;
2121 }
2122
2123 public void goBack() {
2124 if (mMainView != null) {
Mathew Inwoodb4e831b2011-09-05 18:58:48 +01002125 clearPageLoadCompleteListener();
John Reckef654f12011-07-12 16:42:08 -07002126 mMainView.goBack();
2127 }
2128 }
2129
2130 public void goForward() {
2131 if (mMainView != null) {
Mathew Inwoodb4e831b2011-09-05 18:58:48 +01002132 clearPageLoadCompleteListener();
John Reckef654f12011-07-12 16:42:08 -07002133 mMainView.goForward();
2134 }
2135 }
2136
Mathew Inwood1dd8e822011-08-03 14:34:29 +01002137 /**
2138 * Causes the tab back/forward stack to be cleared once, if the given URL is the next URL
2139 * to be added to the stack.
2140 *
2141 * This is used to ensure that preloaded URLs that are not subsequently seen by the user do
2142 * not appear in the back stack.
2143 */
Mathew Inwooda829d552011-09-02 14:16:25 +01002144 public void clearBackStackWhenItemAdded(Pattern urlPattern) {
2145 mClearHistoryUrlPattern = urlPattern;
Mathew Inwood1dd8e822011-08-03 14:34:29 +01002146 }
2147
John Reck1cf4b792011-07-26 10:22:22 -07002148 protected void persistThumbnail() {
2149 BackgroundHandler.execute(mSaveThumbnail);
2150 }
2151
2152 protected void deleteThumbnail() {
2153 BackgroundHandler.execute(mDeleteThumbnail);
2154 }
2155
2156 private void updateCaptureFromBlob(byte[] blob) {
2157 synchronized (Tab.this) {
2158 if (mCapture == null) {
2159 return;
2160 }
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002161 ByteBuffer buffer = ByteBuffer.wrap(blob);
Michael Kolbd837a112011-08-09 14:00:18 -07002162 try {
2163 mCapture.copyPixelsFromBuffer(buffer);
2164 } catch (RuntimeException rex) {
2165 Log.e(LOGTAG, "Load capture has mismatched sizes; buffer: "
2166 + buffer.capacity() + " blob: " + blob.length
2167 + "capture: " + mCapture.getByteCount());
2168 throw rex;
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002169 }
John Reck1cf4b792011-07-26 10:22:22 -07002170 }
2171 }
2172
2173 private byte[] getCaptureBlob() {
2174 synchronized (Tab.this) {
2175 if (mCapture == null) {
2176 return null;
2177 }
2178 ByteBuffer buffer = ByteBuffer.allocate(mCapture.getByteCount());
2179 mCapture.copyPixelsToBuffer(buffer);
2180 return buffer.array();
2181 }
2182 }
2183
2184 private Runnable mSaveThumbnail = new Runnable() {
2185
2186 @Override
2187 public void run() {
2188 byte[] blob = getCaptureBlob();
2189 if (blob == null) {
2190 return;
2191 }
2192 ContentResolver cr = mContext.getContentResolver();
2193 ContentValues values = new ContentValues();
2194 values.put(Thumbnails._ID, mId);
2195 values.put(Thumbnails.THUMBNAIL, blob);
2196 cr.insert(Thumbnails.CONTENT_URI, values);
2197 }
2198 };
2199
2200 private Runnable mDeleteThumbnail = new Runnable() {
2201
2202 @Override
2203 public void run() {
2204 ContentResolver cr = mContext.getContentResolver();
2205 try {
2206 cr.delete(ContentUris.withAppendedId(Thumbnails.CONTENT_URI, mId),
2207 null, null);
2208 } catch (Throwable t) {}
2209 }
2210 };
2211
2212 private Runnable mLoadThumbnail = new Runnable() {
2213
2214 @Override
2215 public void run() {
2216 ContentResolver cr = mContext.getContentResolver();
2217 Cursor c = null;
2218 try {
2219 Uri uri = ContentUris.withAppendedId(Thumbnails.CONTENT_URI, mId);
2220 c = cr.query(uri, new String[] {Thumbnails._ID,
2221 Thumbnails.THUMBNAIL}, null, null, null);
2222 if (c.moveToFirst()) {
2223 byte[] data = c.getBlob(1);
2224 if (data != null && data.length > 0) {
2225 updateCaptureFromBlob(data);
2226 }
2227 }
2228 } finally {
2229 if (c != null) {
2230 c.close();
2231 }
2232 }
2233 }
2234 };
2235
John Reck52be4782011-08-26 15:37:29 -07002236 @Override
2237 public String toString() {
2238 StringBuilder builder = new StringBuilder(100);
2239 builder.append(mId);
2240 builder.append(") has parent: ");
2241 if (getParent() != null) {
2242 builder.append("true[");
2243 builder.append(getParent().getId());
2244 builder.append("]");
2245 } else {
2246 builder.append("false");
2247 }
2248 builder.append(", incog: ");
2249 builder.append(isPrivateBrowsingEnabled());
2250 if (!isPrivateBrowsingEnabled()) {
2251 builder.append(", title: ");
2252 builder.append(getTitle());
2253 builder.append(", url: ");
2254 builder.append(getUrl());
2255 }
2256 return builder.toString();
2257 }
2258
Grace Kloba22ac16e2009-10-07 18:00:23 -07002259}