blob: 96920a49ff820bcb4701b6f21ed712e5c50665a9 [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 Kolba3194d02011-09-07 11:23:51 -070035import android.graphics.Paint;
Michael Kolb9ef259a2011-07-12 15:33:08 -070036import android.graphics.Picture;
Michael Kolba3194d02011-09-07 11:23:51 -070037import android.graphics.PorterDuff;
38import android.graphics.PorterDuffXfermode;
Grace Kloba22ac16e2009-10-07 18:00:23 -070039import android.net.Uri;
40import android.net.http.SslError;
Grace Kloba22ac16e2009-10-07 18:00:23 -070041import android.os.Bundle;
Michael Kolb9ef259a2011-07-12 15:33:08 -070042import android.os.Handler;
Grace Kloba22ac16e2009-10-07 18:00:23 -070043import android.os.Message;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +000044import android.os.SystemClock;
Brian Carlstrom8862c1d2011-06-02 01:05:55 -070045import android.security.KeyChain;
Brian Carlstromaa09cd82011-06-09 16:04:40 -070046import android.security.KeyChainAliasCallback;
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -050047import android.speech.RecognizerResultsIntent;
John Reck24f18262011-06-17 14:47:20 -070048import android.text.TextUtils;
Grace Kloba22ac16e2009-10-07 18:00:23 -070049import android.util.Log;
50import android.view.KeyEvent;
51import android.view.LayoutInflater;
52import android.view.View;
Grace Kloba50c241e2010-04-20 11:07:50 -070053import android.view.ViewStub;
Brian Carlstrom8862c1d2011-06-02 01:05:55 -070054import android.webkit.ClientCertRequestHandler;
Ben Murdochc42addf2010-01-28 15:19:59 +000055import android.webkit.ConsoleMessage;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -050056import android.webkit.DownloadListener;
Grace Kloba22ac16e2009-10-07 18:00:23 -070057import android.webkit.GeolocationPermissions;
58import android.webkit.HttpAuthHandler;
59import android.webkit.SslErrorHandler;
60import android.webkit.URLUtil;
61import android.webkit.ValueCallback;
John Reck6c2e2f32011-08-22 13:41:23 -070062import android.webkit.WebBackForwardList;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -050063import android.webkit.WebBackForwardListClient;
Grace Kloba22ac16e2009-10-07 18:00:23 -070064import android.webkit.WebChromeClient;
65import android.webkit.WebHistoryItem;
John Reck438bf462011-01-12 18:11:46 -080066import android.webkit.WebResourceResponse;
Grace Kloba22ac16e2009-10-07 18:00:23 -070067import android.webkit.WebStorage;
68import android.webkit.WebView;
Michael Kolb9ef259a2011-07-12 15:33:08 -070069import android.webkit.WebView.PictureListener;
Grace Kloba22ac16e2009-10-07 18:00:23 -070070import android.webkit.WebViewClient;
Ben Murdoch1d676b62011-01-17 12:54:24 +000071import android.widget.CheckBox;
Ben Murdoch8029a772010-11-16 11:58:21 +000072import android.widget.Toast;
Grace Kloba22ac16e2009-10-07 18:00:23 -070073
John Reck8ee633f2011-08-09 16:00:35 -070074import com.android.browser.TabControl.OnThumbnailUpdatedListener;
John Reck541f55a2011-06-07 16:34:43 -070075import com.android.browser.homepages.HomeProvider;
John Reck1cf4b792011-07-26 10:22:22 -070076import com.android.browser.provider.BrowserProvider2.Thumbnails;
John Reck8cc92352011-07-06 17:41:52 -070077import com.android.browser.provider.SnapshotProvider.Snapshots;
John Reck541f55a2011-06-07 16:34:43 -070078import com.android.common.speech.LoggingEvents;
79
80import java.io.ByteArrayOutputStream;
John Reck1cf4b792011-07-26 10:22:22 -070081import java.nio.ByteBuffer;
Michael Kolbfe251992010-07-08 15:41:55 -070082import java.util.ArrayList;
83import java.util.HashMap;
84import java.util.Iterator;
85import java.util.LinkedList;
86import java.util.Map;
87import java.util.Vector;
Mathew Inwooda829d552011-09-02 14:16:25 +010088import java.util.regex.Pattern;
John Reck8cc92352011-07-06 17:41:52 -070089import java.util.zip.GZIPOutputStream;
Michael Kolbfe251992010-07-08 15:41:55 -070090
Grace Kloba22ac16e2009-10-07 18:00:23 -070091/**
92 * Class for maintaining Tabs with a main WebView and a subwindow.
93 */
Michael Kolb9ef259a2011-07-12 15:33:08 -070094class Tab implements PictureListener {
Michael Kolb8233fac2010-10-26 16:08:53 -070095
Grace Kloba22ac16e2009-10-07 18:00:23 -070096 // Log Tag
97 private static final String LOGTAG = "Tab";
Mathew Inwood1dd8e822011-08-03 14:34:29 +010098 private static final boolean LOGD_ENABLED = com.android.browser.Browser.LOGD_ENABLED;
Ben Murdochc42addf2010-01-28 15:19:59 +000099 // Special case the logtag for messages for the Console to make it easier to
100 // filter them and match the logtag used for these messages in older versions
101 // of the browser.
102 private static final String CONSOLE_LOGTAG = "browser";
103
Michael Kolb9ef259a2011-07-12 15:33:08 -0700104 private static final int MSG_CAPTURE = 42;
John Reck8ee633f2011-08-09 16:00:35 -0700105 private static final int CAPTURE_DELAY = 100;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700106
John Reck1cf4b792011-07-26 10:22:22 -0700107 private static Bitmap sDefaultFavicon;
108
Michael Kolba3194d02011-09-07 11:23:51 -0700109 private static Paint sAlphaPaint = new Paint();
110 static {
111 sAlphaPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
112 sAlphaPaint.setColor(Color.TRANSPARENT);
113 }
114
John Reck30c714c2010-12-16 17:30:34 -0800115 public enum LockIcon {
116 LOCK_ICON_UNSECURE,
117 LOCK_ICON_SECURE,
118 LOCK_ICON_MIXED,
119 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700120
Michael Kolb14612442011-06-24 13:06:29 -0700121 Context mContext;
John Reckd8c74522011-06-14 08:45:00 -0700122 protected WebViewController mWebViewController;
Michael Kolb8233fac2010-10-26 16:08:53 -0700123
Michael Kolbc831b632011-05-11 09:30:34 -0700124 // The tab ID
John Reckd8c74522011-06-14 08:45:00 -0700125 private long mId = -1;
Michael Kolbc831b632011-05-11 09:30:34 -0700126
Grace Kloba22ac16e2009-10-07 18:00:23 -0700127 // The Geolocation permissions prompt
128 private GeolocationPermissionsPrompt mGeolocationPermissionsPrompt;
129 // Main WebView wrapper
Michael Kolba713ec82010-11-29 17:27:06 -0800130 private View mContainer;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700131 // Main WebView
132 private WebView mMainView;
133 // Subwindow container
134 private View mSubViewContainer;
135 // Subwindow WebView
136 private WebView mSubView;
137 // Saved bundle for when we are running low on memory. It contains the
138 // information needed to restore the WebView if the user goes back to the
139 // tab.
140 private Bundle mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700141 // Parent Tab. This is the Tab that created this Tab, or null if the Tab was
142 // created by the UI
Michael Kolbc831b632011-05-11 09:30:34 -0700143 private Tab mParent;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700144 // Tab that constructed by this Tab. This is used when this Tab is
145 // destroyed, it clears all mParentTab values in the children.
Michael Kolbc831b632011-05-11 09:30:34 -0700146 private Vector<Tab> mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700147 // If true, the tab is in the foreground of the current activity.
148 private boolean mInForeground;
Michael Kolb8233fac2010-10-26 16:08:53 -0700149 // If true, the tab is in page loading state (after onPageStarted,
150 // before onPageFinsihed)
151 private boolean mInPageLoad;
John Reck30c714c2010-12-16 17:30:34 -0800152 // The last reported progress of the current page
153 private int mPageLoadProgress;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000154 // The time the load started, used to find load page time
155 private long mLoadStartTime;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700156 // Application identifier used to find tabs that another application wants
157 // to reuse.
158 private String mAppId;
Michael Kolbe28b3472011-08-04 16:54:31 -0700159 // flag to indicate if tab should be closed on back
160 private boolean mCloseOnBack;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700161 // Keep the original url around to avoid killing the old WebView if the url
162 // has not changed.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700163 // Error console for the tab
164 private ErrorConsoleView mErrorConsole;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -0500165 // The listener that gets invoked when a download is started from the
166 // mMainView
167 private final DownloadListener mDownloadListener;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500168 // Listener used to know when we move forward or back in the history list.
169 private final WebBackForwardListClient mWebBackForwardListClient;
John Recke969cc52010-12-21 17:24:43 -0800170 private DataController mDataController;
Patrick Scott92066772011-03-10 08:46:27 -0500171 // State of the auto-login request.
172 private DeviceAccountLogin mDeviceAccountLogin;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700173
174 // AsyncTask for downloading touch icons
175 DownloadTouchIcon mTouchIconLoader;
176
John Reck35e9dd62011-04-25 09:01:54 -0700177 private BrowserSettings mSettings;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700178 private int mCaptureWidth;
179 private int mCaptureHeight;
180 private Bitmap mCapture;
181 private Handler mHandler;
182
Mathew Inwood1dd8e822011-08-03 14:34:29 +0100183 /**
Mathew Inwood9ad1eac2011-09-15 11:29:50 +0100184 * See {@link #clearBackStackWhenItemAdded(String)}.
Mathew Inwood1dd8e822011-08-03 14:34:29 +0100185 */
Mathew Inwooda829d552011-09-02 14:16:25 +0100186 private Pattern mClearHistoryUrlPattern;
Mathew Inwood1dd8e822011-08-03 14:34:29 +0100187
John Reck1cf4b792011-07-26 10:22:22 -0700188 private static synchronized Bitmap getDefaultFavicon(Context context) {
189 if (sDefaultFavicon == null) {
190 sDefaultFavicon = BitmapFactory.decodeResource(
191 context.getResources(), R.drawable.app_web_browser_sm);
192 }
193 return sDefaultFavicon;
194 }
Michael Kolbeb95db42011-03-03 10:38:40 -0800195
John Reck30c714c2010-12-16 17:30:34 -0800196 // All the state needed for a page
John Reckd8c74522011-06-14 08:45:00 -0700197 protected static class PageState {
John Reck30c714c2010-12-16 17:30:34 -0800198 String mUrl;
John Reckdb22ec42011-06-29 11:31:24 -0700199 String mOriginalUrl;
John Reck30c714c2010-12-16 17:30:34 -0800200 String mTitle;
201 LockIcon mLockIcon;
202 Bitmap mFavicon;
John Reck502a3532011-08-16 14:21:46 -0700203 boolean mIsBookmarkedSite = false;
204 boolean mIncognito = false;
John Reck30c714c2010-12-16 17:30:34 -0800205
206 PageState(Context c, boolean incognito) {
John Reck502a3532011-08-16 14:21:46 -0700207 mIncognito = incognito;
208 if (mIncognito) {
John Reckdb22ec42011-06-29 11:31:24 -0700209 mOriginalUrl = mUrl = "browser:incognito";
John Reck30c714c2010-12-16 17:30:34 -0800210 mTitle = c.getString(R.string.new_incognito_tab);
John Reck30c714c2010-12-16 17:30:34 -0800211 } else {
John Reckdb22ec42011-06-29 11:31:24 -0700212 mOriginalUrl = mUrl = "";
John Reck30c714c2010-12-16 17:30:34 -0800213 mTitle = c.getString(R.string.new_tab);
John Reck30c714c2010-12-16 17:30:34 -0800214 }
John Reck1cf4b792011-07-26 10:22:22 -0700215 mFavicon = null;
John Reck30c714c2010-12-16 17:30:34 -0800216 mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
217 }
218
219 PageState(Context c, boolean incognito, String url, Bitmap favicon) {
John Reck502a3532011-08-16 14:21:46 -0700220 mIncognito = incognito;
John Reckdb22ec42011-06-29 11:31:24 -0700221 mOriginalUrl = mUrl = url;
John Reck30c714c2010-12-16 17:30:34 -0800222 mTitle = null;
223 if (URLUtil.isHttpsUrl(url)) {
224 mLockIcon = LockIcon.LOCK_ICON_SECURE;
225 } else {
226 mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
227 }
John Reck1cf4b792011-07-26 10:22:22 -0700228 mFavicon = favicon;
John Reck30c714c2010-12-16 17:30:34 -0800229 }
John Reck1cf4b792011-07-26 10:22:22 -0700230
Grace Kloba22ac16e2009-10-07 18:00:23 -0700231 }
232
John Reck30c714c2010-12-16 17:30:34 -0800233 // The current/loading page's state
John Reckd8c74522011-06-14 08:45:00 -0700234 protected PageState mCurrentState;
John Reck30c714c2010-12-16 17:30:34 -0800235
Grace Kloba22ac16e2009-10-07 18:00:23 -0700236 // Used for saving and restoring each Tab
Michael Kolbc831b632011-05-11 09:30:34 -0700237 static final String ID = "ID";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700238 static final String CURRURL = "currentUrl";
239 static final String CURRTITLE = "currentTitle";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700240 static final String PARENTTAB = "parentTab";
241 static final String APPID = "appid";
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700242 static final String INCOGNITO = "privateBrowsingEnabled";
John Reckb0a86db2011-05-24 14:05:58 -0700243 static final String USERAGENT = "useragent";
Michael Kolbe28b3472011-08-04 16:54:31 -0700244 static final String CLOSEFLAG = "closeOnBack";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700245
246 // -------------------------------------------------------------------------
247
Leon Scroggins58d56c62010-01-28 15:12:40 -0500248 /**
249 * Private information regarding the latest voice search. If the Tab is not
250 * in voice search mode, this will be null.
251 */
252 private VoiceSearchData mVoiceSearchData;
253 /**
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400254 * Remove voice search mode from this tab.
255 */
256 public void revertVoiceSearchMode() {
257 if (mVoiceSearchData != null) {
258 mVoiceSearchData = null;
259 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700260 mWebViewController.revertVoiceSearchMode(this);
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400261 }
262 }
263 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700264
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400265 /**
Leon Scroggins58d56c62010-01-28 15:12:40 -0500266 * Return whether the tab is in voice search mode.
267 */
268 public boolean isInVoiceSearchMode() {
269 return mVoiceSearchData != null;
270 }
271 /**
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400272 * Return true if the Tab is in voice search mode and the voice search
273 * Intent came with a String identifying that Google provided the Intent.
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500274 */
275 public boolean voiceSearchSourceIsGoogle() {
276 return mVoiceSearchData != null && mVoiceSearchData.mSourceIsGoogle;
277 }
278 /**
Leon Scroggins58d56c62010-01-28 15:12:40 -0500279 * Get the title to display for the current voice search page. If the Tab
280 * is not in voice search mode, return null.
281 */
282 public String getVoiceDisplayTitle() {
283 if (mVoiceSearchData == null) return null;
284 return mVoiceSearchData.mLastVoiceSearchTitle;
285 }
286 /**
287 * Get the latest array of voice search results, to be passed to the
288 * BrowserProvider. If the Tab is not in voice search mode, return null.
289 */
290 public ArrayList<String> getVoiceSearchResults() {
291 if (mVoiceSearchData == null) return null;
292 return mVoiceSearchData.mVoiceSearchResults;
293 }
294 /**
295 * Activate voice search mode.
296 * @param intent Intent which has the results to use, or an index into the
297 * results when reusing the old results.
298 */
299 /* package */ void activateVoiceSearchMode(Intent intent) {
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500300 int index = 0;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500301 ArrayList<String> results = intent.getStringArrayListExtra(
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -0500302 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_STRINGS);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500303 if (results != null) {
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500304 ArrayList<String> urls = intent.getStringArrayListExtra(
305 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_URLS);
306 ArrayList<String> htmls = intent.getStringArrayListExtra(
307 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_HTML);
308 ArrayList<String> baseUrls = intent.getStringArrayListExtra(
309 RecognizerResultsIntent
310 .EXTRA_VOICE_SEARCH_RESULT_HTML_BASE_URLS);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500311 // This tab is now entering voice search mode for the first time, or
312 // a new voice search was done.
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500313 int size = results.size();
314 if (urls == null || size != urls.size()) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500315 throw new AssertionError("improper extras passed in Intent");
316 }
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500317 if (htmls == null || htmls.size() != size || baseUrls == null ||
318 (baseUrls.size() != size && baseUrls.size() != 1)) {
319 // If either of these arrays are empty/incorrectly sized, ignore
320 // them.
321 htmls = null;
322 baseUrls = null;
323 }
324 mVoiceSearchData = new VoiceSearchData(results, urls, htmls,
325 baseUrls);
Leon Scroggins9df94972010-03-08 18:20:35 -0500326 mVoiceSearchData.mHeaders = intent.getParcelableArrayListExtra(
327 RecognizerResultsIntent
328 .EXTRA_VOICE_SEARCH_RESULT_HTTP_HEADERS);
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500329 mVoiceSearchData.mSourceIsGoogle = intent.getBooleanExtra(
330 VoiceSearchData.SOURCE_IS_GOOGLE, false);
Leon Scroggins2ee4a5a2010-03-15 16:56:57 -0400331 mVoiceSearchData.mVoiceSearchIntent = new Intent(intent);
Leon Scrogginse10dde52010-03-08 19:53:03 -0500332 }
333 String extraData = intent.getStringExtra(
334 SearchManager.EXTRA_DATA_KEY);
335 if (extraData != null) {
336 index = Integer.parseInt(extraData);
337 if (index >= mVoiceSearchData.mVoiceSearchResults.size()) {
338 throw new AssertionError("index must be less than "
339 + "size of mVoiceSearchResults");
340 }
341 if (mVoiceSearchData.mSourceIsGoogle) {
342 Intent logIntent = new Intent(
343 LoggingEvents.ACTION_LOG_EVENT);
344 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
345 LoggingEvents.VoiceSearch.N_BEST_CHOOSE);
346 logIntent.putExtra(
347 LoggingEvents.VoiceSearch.EXTRA_N_BEST_CHOOSE_INDEX,
348 index);
Michael Kolb14612442011-06-24 13:06:29 -0700349 mContext.sendBroadcast(logIntent);
Leon Scrogginse10dde52010-03-08 19:53:03 -0500350 }
351 if (mVoiceSearchData.mVoiceSearchIntent != null) {
Leon Scroggins2ee4a5a2010-03-15 16:56:57 -0400352 // Copy the Intent, so that each history item will have its own
353 // Intent, with different (or none) extra data.
354 Intent latest = new Intent(mVoiceSearchData.mVoiceSearchIntent);
355 latest.putExtra(SearchManager.EXTRA_DATA_KEY, extraData);
356 mVoiceSearchData.mVoiceSearchIntent = latest;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500357 }
358 }
359 mVoiceSearchData.mLastVoiceSearchTitle
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500360 = mVoiceSearchData.mVoiceSearchResults.get(index);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500361 if (mInForeground) {
Michael Kolb11d19782011-03-20 10:17:40 -0700362 mWebViewController.activateVoiceSearchMode(
363 mVoiceSearchData.mLastVoiceSearchTitle,
364 mVoiceSearchData.mVoiceSearchResults);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500365 }
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500366 if (mVoiceSearchData.mVoiceSearchHtmls != null) {
367 // When index was found it was already ensured that it was valid
368 String uriString = mVoiceSearchData.mVoiceSearchHtmls.get(index);
369 if (uriString != null) {
370 Uri dataUri = Uri.parse(uriString);
371 if (RecognizerResultsIntent.URI_SCHEME_INLINE.equals(
372 dataUri.getScheme())) {
373 // If there is only one base URL, use it. If there are
374 // more, there will be one for each index, so use the base
375 // URL corresponding to the index.
376 String baseUrl = mVoiceSearchData.mVoiceSearchBaseUrls.get(
377 mVoiceSearchData.mVoiceSearchBaseUrls.size() > 1 ?
378 index : 0);
379 mVoiceSearchData.mLastVoiceSearchUrl = baseUrl;
380 mMainView.loadDataWithBaseURL(baseUrl,
381 uriString.substring(RecognizerResultsIntent
382 .URI_SCHEME_INLINE.length() + 1), "text/html",
383 "utf-8", baseUrl);
384 return;
385 }
386 }
387 }
Leon Scroggins58d56c62010-01-28 15:12:40 -0500388 mVoiceSearchData.mLastVoiceSearchUrl
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500389 = mVoiceSearchData.mVoiceSearchUrls.get(index);
390 if (null == mVoiceSearchData.mLastVoiceSearchUrl) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700391 mVoiceSearchData.mLastVoiceSearchUrl = UrlUtils.smartUrlFilter(
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500392 mVoiceSearchData.mLastVoiceSearchTitle);
393 }
Leon Scroggins9df94972010-03-08 18:20:35 -0500394 Map<String, String> headers = null;
395 if (mVoiceSearchData.mHeaders != null) {
396 int bundleIndex = mVoiceSearchData.mHeaders.size() == 1 ? 0
397 : index;
398 Bundle bundle = mVoiceSearchData.mHeaders.get(bundleIndex);
399 if (bundle != null && !bundle.isEmpty()) {
400 Iterator<String> iter = bundle.keySet().iterator();
401 headers = new HashMap<String, String>();
402 while (iter.hasNext()) {
403 String key = iter.next();
404 headers.put(key, bundle.getString(key));
405 }
406 }
407 }
408 mMainView.loadUrl(mVoiceSearchData.mLastVoiceSearchUrl, headers);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500409 }
410 /* package */ static class VoiceSearchData {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500411 public VoiceSearchData(ArrayList<String> results,
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500412 ArrayList<String> urls, ArrayList<String> htmls,
413 ArrayList<String> baseUrls) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500414 mVoiceSearchResults = results;
415 mVoiceSearchUrls = urls;
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500416 mVoiceSearchHtmls = htmls;
417 mVoiceSearchBaseUrls = baseUrls;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500418 }
419 /*
420 * ArrayList of suggestions to be displayed when opening the
421 * SearchManager
422 */
423 public ArrayList<String> mVoiceSearchResults;
424 /*
425 * ArrayList of urls, associated with the suggestions in
426 * mVoiceSearchResults.
427 */
428 public ArrayList<String> mVoiceSearchUrls;
429 /*
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500430 * ArrayList holding content to load for each item in
431 * mVoiceSearchResults.
432 */
433 public ArrayList<String> mVoiceSearchHtmls;
434 /*
435 * ArrayList holding base urls for the items in mVoiceSearchResults.
436 * If non null, this will either have the same size as
437 * mVoiceSearchResults or have a size of 1, in which case all will use
438 * the same base url
439 */
440 public ArrayList<String> mVoiceSearchBaseUrls;
441 /*
Leon Scroggins58d56c62010-01-28 15:12:40 -0500442 * The last url provided by voice search. Used for comparison to see if
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500443 * we are going to a page by some method besides voice search.
Leon Scroggins58d56c62010-01-28 15:12:40 -0500444 */
445 public String mLastVoiceSearchUrl;
446 /**
447 * The last title used for voice search. Needed to update the title bar
448 * when switching tabs.
449 */
450 public String mLastVoiceSearchTitle;
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500451 /**
452 * Whether the Intent which turned on voice search mode contained the
453 * String signifying that Google was the source.
454 */
455 public boolean mSourceIsGoogle;
456 /**
Leon Scroggins9df94972010-03-08 18:20:35 -0500457 * List of headers to be passed into the WebView containing location
458 * information
459 */
460 public ArrayList<Bundle> mHeaders;
461 /**
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500462 * The Intent used to invoke voice search. Placed on the
463 * WebHistoryItem so that when coming back to a previous voice search
464 * page we can again activate voice search.
465 */
Leon Scrogginse10dde52010-03-08 19:53:03 -0500466 public Intent mVoiceSearchIntent;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500467 /**
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500468 * String used to identify Google as the source of voice search.
469 */
470 public static String SOURCE_IS_GOOGLE
471 = "android.speech.extras.SOURCE_IS_GOOGLE";
Leon Scroggins58d56c62010-01-28 15:12:40 -0500472 }
473
Grace Kloba22ac16e2009-10-07 18:00:23 -0700474 // Container class for the next error dialog that needs to be displayed
475 private class ErrorDialog {
476 public final int mTitle;
477 public final String mDescription;
478 public final int mError;
479 ErrorDialog(int title, String desc, int error) {
480 mTitle = title;
481 mDescription = desc;
482 mError = error;
483 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700484 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700485
486 private void processNextError() {
487 if (mQueuedErrors == null) {
488 return;
489 }
490 // The first one is currently displayed so just remove it.
491 mQueuedErrors.removeFirst();
492 if (mQueuedErrors.size() == 0) {
493 mQueuedErrors = null;
494 return;
495 }
496 showError(mQueuedErrors.getFirst());
497 }
498
499 private DialogInterface.OnDismissListener mDialogListener =
500 new DialogInterface.OnDismissListener() {
501 public void onDismiss(DialogInterface d) {
502 processNextError();
503 }
504 };
505 private LinkedList<ErrorDialog> mQueuedErrors;
506
507 private void queueError(int err, String desc) {
508 if (mQueuedErrors == null) {
509 mQueuedErrors = new LinkedList<ErrorDialog>();
510 }
511 for (ErrorDialog d : mQueuedErrors) {
512 if (d.mError == err) {
513 // Already saw a similar error, ignore the new one.
514 return;
515 }
516 }
517 ErrorDialog errDialog = new ErrorDialog(
518 err == WebViewClient.ERROR_FILE_NOT_FOUND ?
519 R.string.browserFrameFileErrorLabel :
520 R.string.browserFrameNetworkErrorLabel,
521 desc, err);
522 mQueuedErrors.addLast(errDialog);
523
524 // Show the dialog now if the queue was empty and it is in foreground
525 if (mQueuedErrors.size() == 1 && mInForeground) {
526 showError(errDialog);
527 }
528 }
529
530 private void showError(ErrorDialog errDialog) {
531 if (mInForeground) {
Michael Kolb14612442011-06-24 13:06:29 -0700532 AlertDialog d = new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700533 .setTitle(errDialog.mTitle)
534 .setMessage(errDialog.mDescription)
535 .setPositiveButton(R.string.ok, null)
536 .create();
537 d.setOnDismissListener(mDialogListener);
538 d.show();
539 }
540 }
541
542 // -------------------------------------------------------------------------
543 // WebViewClient implementation for the main WebView
544 // -------------------------------------------------------------------------
545
546 private final WebViewClient mWebViewClient = new WebViewClient() {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500547 private Message mDontResend;
548 private Message mResend;
Michael Kolb47bd1e42011-09-01 15:25:00 -0700549
550 private boolean providersDiffer(String url, String otherUrl) {
551 Uri uri1 = Uri.parse(url);
552 Uri uri2 = Uri.parse(otherUrl);
553 return !uri1.getEncodedAuthority().equals(uri2.getEncodedAuthority());
554 }
555
Grace Kloba22ac16e2009-10-07 18:00:23 -0700556 @Override
557 public void onPageStarted(WebView view, String url, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700558 mInPageLoad = true;
John Reck30c714c2010-12-16 17:30:34 -0800559 mPageLoadProgress = 0;
Michael Kolb14612442011-06-24 13:06:29 -0700560 mCurrentState = new PageState(mContext,
John Reck30c714c2010-12-16 17:30:34 -0800561 view.isPrivateBrowsingEnabled(), url, favicon);
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000562 mLoadStartTime = SystemClock.uptimeMillis();
Leon Scroggins58d56c62010-01-28 15:12:40 -0500563 if (mVoiceSearchData != null
Michael Kolb47bd1e42011-09-01 15:25:00 -0700564 && providersDiffer(url, mVoiceSearchData.mLastVoiceSearchUrl)) {
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500565 if (mVoiceSearchData.mSourceIsGoogle) {
566 Intent i = new Intent(LoggingEvents.ACTION_LOG_EVENT);
567 i.putExtra(LoggingEvents.EXTRA_FLUSH, true);
Michael Kolb14612442011-06-24 13:06:29 -0700568 mContext.sendBroadcast(i);
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500569 }
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400570 revertVoiceSearchMode();
Leon Scroggins58d56c62010-01-28 15:12:40 -0500571 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700572
Grace Kloba22ac16e2009-10-07 18:00:23 -0700573
574 // If we start a touch icon load and then load a new page, we don't
575 // want to cancel the current touch icon loader. But, we do want to
576 // create a new one when the touch icon url is known.
577 if (mTouchIconLoader != null) {
578 mTouchIconLoader.mTab = null;
579 mTouchIconLoader = null;
580 }
581
582 // reset the error console
583 if (mErrorConsole != null) {
584 mErrorConsole.clearErrorMessages();
Michael Kolb8233fac2010-10-26 16:08:53 -0700585 if (mWebViewController.shouldShowErrorConsole()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700586 mErrorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
587 }
588 }
589
Patrick Scott92066772011-03-10 08:46:27 -0500590 // Cancel the auto-login process.
591 if (mDeviceAccountLogin != null) {
592 mDeviceAccountLogin.cancel();
593 mDeviceAccountLogin = null;
594 mWebViewController.hideAutoLogin(Tab.this);
595 }
596
Grace Kloba22ac16e2009-10-07 18:00:23 -0700597 // finally update the UI in the activity if it is in the foreground
John Reck324d4402011-01-11 16:56:42 -0800598 mWebViewController.onPageStarted(Tab.this, view, favicon);
Leon Scroggins4cd97792010-12-03 15:31:56 -0500599
John Recke969cc52010-12-21 17:24:43 -0800600 updateBookmarkedStatus();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700601 }
602
603 @Override
604 public void onPageFinished(WebView view, String url) {
John Recka1696282011-07-08 14:10:37 -0700605 if (!mInPageLoad) {
606 // In page navigation links (www.something.com#footer) will
607 // trigger an onPageFinished which we don't care about.
608 return;
609 }
John Reck5b691842010-11-29 11:21:13 -0800610 if (!isPrivateBrowsingEnabled()) {
611 LogTag.logPageFinishedLoading(
612 url, SystemClock.uptimeMillis() - mLoadStartTime);
613 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700614 mInPageLoad = false;
John Reck1cf4b792011-07-26 10:22:22 -0700615 syncCurrentState(view, url);
John Reck324d4402011-01-11 16:56:42 -0800616 mWebViewController.onPageFinished(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700617 }
618
619 // return true if want to hijack the url to let another app to handle it
620 @Override
621 public boolean shouldOverrideUrlLoading(WebView view, String url) {
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400622 if (voiceSearchSourceIsGoogle()) {
623 // This method is called when the user clicks on a link.
624 // VoiceSearchMode is turned off when the user leaves the
625 // Google results page, so at this point the user must be on
626 // that page. If the user clicked a link on that page, assume
627 // that the voice search was effective, and broadcast an Intent
628 // so a receiver can take note of that fact.
629 Intent logIntent = new Intent(LoggingEvents.ACTION_LOG_EVENT);
630 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
631 LoggingEvents.VoiceSearch.RESULT_CLICKED);
Michael Kolb14612442011-06-24 13:06:29 -0700632 mContext.sendBroadcast(logIntent);
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400633 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700634 if (mInForeground) {
Michael Kolb18eb3772010-12-10 14:29:51 -0800635 return mWebViewController.shouldOverrideUrlLoading(Tab.this,
636 view, url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700637 } else {
638 return false;
639 }
640 }
641
642 /**
643 * Updates the lock icon. This method is called when we discover another
644 * resource to be loaded for this page (for example, javascript). While
645 * we update the icon type, we do not update the lock icon itself until
646 * we are done loading, it is slightly more secure this way.
647 */
648 @Override
649 public void onLoadResource(WebView view, String url) {
650 if (url != null && url.length() > 0) {
651 // It is only if the page claims to be secure that we may have
652 // to update the lock:
John Reck30c714c2010-12-16 17:30:34 -0800653 if (mCurrentState.mLockIcon == LockIcon.LOCK_ICON_SECURE) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700654 // If NOT a 'safe' url, change the lock to mixed content!
655 if (!(URLUtil.isHttpsUrl(url) || URLUtil.isDataUrl(url)
656 || URLUtil.isAboutUrl(url))) {
John Reck30c714c2010-12-16 17:30:34 -0800657 mCurrentState.mLockIcon = LockIcon.LOCK_ICON_MIXED;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700658 }
659 }
660 }
661 }
662
663 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700664 * Show a dialog informing the user of the network error reported by
665 * WebCore if it is in the foreground.
666 */
667 @Override
668 public void onReceivedError(WebView view, int errorCode,
669 String description, String failingUrl) {
670 if (errorCode != WebViewClient.ERROR_HOST_LOOKUP &&
671 errorCode != WebViewClient.ERROR_CONNECT &&
672 errorCode != WebViewClient.ERROR_BAD_URL &&
673 errorCode != WebViewClient.ERROR_UNSUPPORTED_SCHEME &&
674 errorCode != WebViewClient.ERROR_FILE) {
675 queueError(errorCode, description);
676 }
Jeff Hamilton47654f42010-09-07 09:57:51 -0500677
678 // Don't log URLs when in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -0700679 if (!isPrivateBrowsingEnabled()) {
Jeff Hamilton47654f42010-09-07 09:57:51 -0500680 Log.e(LOGTAG, "onReceivedError " + errorCode + " " + failingUrl
681 + " " + description);
682 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700683 }
684
685 /**
686 * Check with the user if it is ok to resend POST data as the page they
687 * are trying to navigate to is the result of a POST.
688 */
689 @Override
690 public void onFormResubmission(WebView view, final Message dontResend,
691 final Message resend) {
692 if (!mInForeground) {
693 dontResend.sendToTarget();
694 return;
695 }
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500696 if (mDontResend != null) {
697 Log.w(LOGTAG, "onFormResubmission should not be called again "
698 + "while dialog is still up");
699 dontResend.sendToTarget();
700 return;
701 }
702 mDontResend = dontResend;
703 mResend = resend;
Michael Kolb14612442011-06-24 13:06:29 -0700704 new AlertDialog.Builder(mContext).setTitle(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700705 R.string.browserFrameFormResubmitLabel).setMessage(
706 R.string.browserFrameFormResubmitMessage)
707 .setPositiveButton(R.string.ok,
708 new DialogInterface.OnClickListener() {
709 public void onClick(DialogInterface dialog,
710 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500711 if (mResend != null) {
712 mResend.sendToTarget();
713 mResend = null;
714 mDontResend = null;
715 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700716 }
717 }).setNegativeButton(R.string.cancel,
718 new DialogInterface.OnClickListener() {
719 public void onClick(DialogInterface dialog,
720 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500721 if (mDontResend != null) {
722 mDontResend.sendToTarget();
723 mResend = null;
724 mDontResend = null;
725 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700726 }
727 }).setOnCancelListener(new OnCancelListener() {
728 public void onCancel(DialogInterface dialog) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500729 if (mDontResend != null) {
730 mDontResend.sendToTarget();
731 mResend = null;
732 mDontResend = null;
733 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700734 }
735 }).show();
736 }
737
738 /**
739 * Insert the url into the visited history database.
740 * @param url The url to be inserted.
741 * @param isReload True if this url is being reloaded.
742 * FIXME: Not sure what to do when reloading the page.
743 */
744 @Override
745 public void doUpdateVisitedHistory(WebView view, String url,
746 boolean isReload) {
John Reck324d4402011-01-11 16:56:42 -0800747 mWebViewController.doUpdateVisitedHistory(Tab.this, isReload);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700748 }
749
750 /**
751 * Displays SSL error(s) dialog to the user.
752 */
753 @Override
754 public void onReceivedSslError(final WebView view,
755 final SslErrorHandler handler, final SslError error) {
756 if (!mInForeground) {
757 handler.cancel();
John Reck30c714c2010-12-16 17:30:34 -0800758 setLockIconType(LockIcon.LOCK_ICON_UNSECURE);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700759 return;
760 }
John Reck35e9dd62011-04-25 09:01:54 -0700761 if (mSettings.showSecurityWarnings()) {
John Reckcb28b2c2011-08-26 17:39:44 -0700762 new AlertDialog.Builder(mContext)
763 .setTitle(R.string.security_warning)
764 .setMessage(R.string.ssl_warnings_header)
765 .setIcon(android.R.drawable.ic_dialog_alert)
766 .setPositiveButton(R.string.ssl_continue,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700767 new DialogInterface.OnClickListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700768 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700769 public void onClick(DialogInterface dialog,
770 int whichButton) {
771 handler.proceed();
772 }
John Reckcb28b2c2011-08-26 17:39:44 -0700773 })
774 .setNeutralButton(R.string.view_certificate,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700775 new DialogInterface.OnClickListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700776 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700777 public void onClick(DialogInterface dialog,
778 int whichButton) {
John Reckcb28b2c2011-08-26 17:39:44 -0700779 mWebViewController.showSslCertificateOnError(
780 view, handler, error);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700781 }
John Reckcb28b2c2011-08-26 17:39:44 -0700782 })
783 .setNegativeButton(R.string.ssl_go_back,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700784 new DialogInterface.OnClickListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700785 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700786 public void onClick(DialogInterface dialog,
787 int whichButton) {
John Reck30c714c2010-12-16 17:30:34 -0800788 dialog.cancel();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700789 }
John Reckcb28b2c2011-08-26 17:39:44 -0700790 })
791 .setOnCancelListener(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700792 new DialogInterface.OnCancelListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700793 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700794 public void onCancel(DialogInterface dialog) {
795 handler.cancel();
John Reck30c714c2010-12-16 17:30:34 -0800796 setLockIconType(LockIcon.LOCK_ICON_UNSECURE);
797 mWebViewController.onUserCanceledSsl(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700798 }
John Reckcb28b2c2011-08-26 17:39:44 -0700799 })
800 .show();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700801 } else {
802 handler.proceed();
803 }
804 }
805
806 /**
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700807 * Displays client certificate request to the user.
808 */
809 @Override
810 public void onReceivedClientCertRequest(final WebView view,
811 final ClientCertRequestHandler handler, final String host_and_port) {
812 if (!mInForeground) {
813 handler.ignore();
814 return;
815 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700816 int colon = host_and_port.lastIndexOf(':');
817 String host;
818 int port;
819 if (colon == -1) {
820 host = host_and_port;
821 port = -1;
822 } else {
823 String portString = host_and_port.substring(colon + 1);
824 try {
825 port = Integer.parseInt(portString);
826 host = host_and_port.substring(0, colon);
827 } catch (NumberFormatException e) {
828 host = host_and_port;
829 port = -1;
830 }
831 }
Michael Kolb14612442011-06-24 13:06:29 -0700832 KeyChain.choosePrivateKeyAlias(
833 mWebViewController.getActivity(), new KeyChainAliasCallback() {
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700834 @Override public void alias(String alias) {
835 if (alias == null) {
836 handler.cancel();
837 return;
838 }
Michael Kolb14612442011-06-24 13:06:29 -0700839 new KeyChainLookup(mContext, handler, alias).execute();
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700840 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700841 }, null, null, host, port, null);
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700842 }
843
844 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700845 * Handles an HTTP authentication request.
846 *
847 * @param handler The authentication handler
848 * @param host The host
849 * @param realm The realm
850 */
851 @Override
852 public void onReceivedHttpAuthRequest(WebView view,
853 final HttpAuthHandler handler, final String host,
854 final String realm) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700855 mWebViewController.onReceivedHttpAuthRequest(Tab.this, view, handler, host, realm);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700856 }
857
858 @Override
John Reck438bf462011-01-12 18:11:46 -0800859 public WebResourceResponse shouldInterceptRequest(WebView view,
860 String url) {
861 WebResourceResponse res = HomeProvider.shouldInterceptRequest(
Michael Kolb14612442011-06-24 13:06:29 -0700862 mContext, url);
John Reck438bf462011-01-12 18:11:46 -0800863 return res;
864 }
865
866 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700867 public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
868 if (!mInForeground) {
869 return false;
870 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700871 return mWebViewController.shouldOverrideKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700872 }
873
874 @Override
875 public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700876 if (!mInForeground) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700877 return;
878 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700879 mWebViewController.onUnhandledKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700880 }
Patrick Scott92066772011-03-10 08:46:27 -0500881
882 @Override
883 public void onReceivedLoginRequest(WebView view, String realm,
884 String account, String args) {
Michael Kolb14612442011-06-24 13:06:29 -0700885 new DeviceAccountLogin(mWebViewController.getActivity(), view, Tab.this, mWebViewController)
Patrick Scott92066772011-03-10 08:46:27 -0500886 .handleLogin(realm, account, args);
887 }
888
Grace Kloba22ac16e2009-10-07 18:00:23 -0700889 };
890
John Reck1cf4b792011-07-26 10:22:22 -0700891 private void syncCurrentState(WebView view, String url) {
892 // Sync state (in case of stop/timeout)
893 mCurrentState.mUrl = view.getUrl();
894 if (mCurrentState.mUrl == null) {
John Reck8bcafc12011-08-29 16:43:02 -0700895 mCurrentState.mUrl = "";
John Reck1cf4b792011-07-26 10:22:22 -0700896 }
897 mCurrentState.mOriginalUrl = view.getOriginalUrl();
898 mCurrentState.mTitle = view.getTitle();
899 mCurrentState.mFavicon = view.getFavicon();
900 if (!URLUtil.isHttpsUrl(mCurrentState.mUrl)) {
901 // In case we stop when loading an HTTPS page from an HTTP page
902 // but before a provisional load occurred
903 mCurrentState.mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
904 }
John Reck502a3532011-08-16 14:21:46 -0700905 mCurrentState.mIncognito = view.isPrivateBrowsingEnabled();
John Reck1cf4b792011-07-26 10:22:22 -0700906 }
907
Patrick Scott92066772011-03-10 08:46:27 -0500908 // Called by DeviceAccountLogin when the Tab needs to have the auto-login UI
909 // displayed.
910 void setDeviceAccountLogin(DeviceAccountLogin login) {
911 mDeviceAccountLogin = login;
912 }
913
914 // Returns non-null if the title bar should display the auto-login UI.
915 DeviceAccountLogin getDeviceAccountLogin() {
916 return mDeviceAccountLogin;
917 }
918
Grace Kloba22ac16e2009-10-07 18:00:23 -0700919 // -------------------------------------------------------------------------
920 // WebChromeClient implementation for the main WebView
921 // -------------------------------------------------------------------------
922
923 private final WebChromeClient mWebChromeClient = new WebChromeClient() {
924 // Helper method to create a new tab or sub window.
925 private void createWindow(final boolean dialog, final Message msg) {
926 WebView.WebViewTransport transport =
927 (WebView.WebViewTransport) msg.obj;
928 if (dialog) {
929 createSubWindow();
Michael Kolb8233fac2010-10-26 16:08:53 -0700930 mWebViewController.attachSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700931 transport.setWebView(mSubView);
932 } else {
Michael Kolb7bcafde2011-05-09 13:55:59 -0700933 final Tab newTab = mWebViewController.openTab(null,
John Reck5949c662011-05-27 09:52:29 -0700934 Tab.this, true, true);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700935 transport.setWebView(newTab.getWebView());
936 }
937 msg.sendToTarget();
938 }
939
940 @Override
941 public boolean onCreateWindow(WebView view, final boolean dialog,
942 final boolean userGesture, final Message resultMsg) {
943 // only allow new window or sub window for the foreground case
944 if (!mInForeground) {
945 return false;
946 }
947 // Short-circuit if we can't create any more tabs or sub windows.
948 if (dialog && mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -0700949 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700950 .setTitle(R.string.too_many_subwindows_dialog_title)
951 .setIcon(android.R.drawable.ic_dialog_alert)
952 .setMessage(R.string.too_many_subwindows_dialog_message)
953 .setPositiveButton(R.string.ok, null)
954 .show();
955 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700956 } else if (!mWebViewController.getTabControl().canCreateNewTab()) {
Michael Kolb14612442011-06-24 13:06:29 -0700957 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700958 .setTitle(R.string.too_many_windows_dialog_title)
959 .setIcon(android.R.drawable.ic_dialog_alert)
960 .setMessage(R.string.too_many_windows_dialog_message)
961 .setPositiveButton(R.string.ok, null)
962 .show();
963 return false;
964 }
965
966 // Short-circuit if this was a user gesture.
967 if (userGesture) {
968 createWindow(dialog, resultMsg);
969 return true;
970 }
971
972 // Allow the popup and create the appropriate window.
973 final AlertDialog.OnClickListener allowListener =
974 new AlertDialog.OnClickListener() {
975 public void onClick(DialogInterface d,
976 int which) {
977 createWindow(dialog, resultMsg);
978 }
979 };
980
981 // Block the popup by returning a null WebView.
982 final AlertDialog.OnClickListener blockListener =
983 new AlertDialog.OnClickListener() {
984 public void onClick(DialogInterface d, int which) {
985 resultMsg.sendToTarget();
986 }
987 };
988
989 // Build a confirmation dialog to display to the user.
990 final AlertDialog d =
Michael Kolb14612442011-06-24 13:06:29 -0700991 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700992 .setTitle(R.string.attention)
993 .setIcon(android.R.drawable.ic_dialog_alert)
994 .setMessage(R.string.popup_window_attempt)
995 .setPositiveButton(R.string.allow, allowListener)
996 .setNegativeButton(R.string.block, blockListener)
997 .setCancelable(false)
998 .create();
999
1000 // Show the confirmation dialog.
1001 d.show();
1002 return true;
1003 }
1004
1005 @Override
Patrick Scotteb5061b2009-11-18 15:00:30 -05001006 public void onRequestFocus(WebView view) {
1007 if (!mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -07001008 mWebViewController.switchToTab(Tab.this);
Patrick Scotteb5061b2009-11-18 15:00:30 -05001009 }
1010 }
1011
1012 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -07001013 public void onCloseWindow(WebView window) {
Michael Kolbc831b632011-05-11 09:30:34 -07001014 if (mParent != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001015 // JavaScript can only close popup window.
1016 if (mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -07001017 mWebViewController.switchToTab(mParent);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001018 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001019 mWebViewController.closeTab(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001020 }
1021 }
1022
1023 @Override
1024 public void onProgressChanged(WebView view, int newProgress) {
John Reck30c714c2010-12-16 17:30:34 -08001025 mPageLoadProgress = newProgress;
1026 mWebViewController.onProgressChanged(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001027 }
1028
1029 @Override
Leon Scroggins21d9b902010-03-11 09:33:11 -05001030 public void onReceivedTitle(WebView view, final String title) {
John Reck30c714c2010-12-16 17:30:34 -08001031 mCurrentState.mTitle = title;
Michael Kolb8233fac2010-10-26 16:08:53 -07001032 mWebViewController.onReceivedTitle(Tab.this, title);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001033 }
1034
1035 @Override
1036 public void onReceivedIcon(WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -08001037 mCurrentState.mFavicon = icon;
Michael Kolb8233fac2010-10-26 16:08:53 -07001038 mWebViewController.onFavicon(Tab.this, view, icon);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001039 }
1040
1041 @Override
1042 public void onReceivedTouchIconUrl(WebView view, String url,
1043 boolean precomposed) {
Michael Kolb14612442011-06-24 13:06:29 -07001044 final ContentResolver cr = mContext.getContentResolver();
Leon Scrogginsc8393d92010-04-23 14:58:16 -04001045 // Let precomposed icons take precedence over non-composed
1046 // icons.
1047 if (precomposed && mTouchIconLoader != null) {
1048 mTouchIconLoader.cancel(false);
1049 mTouchIconLoader = null;
1050 }
1051 // Have only one async task at a time.
1052 if (mTouchIconLoader == null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001053 mTouchIconLoader = new DownloadTouchIcon(Tab.this,
Michael Kolb14612442011-06-24 13:06:29 -07001054 mContext, cr, view);
Leon Scrogginsc8393d92010-04-23 14:58:16 -04001055 mTouchIconLoader.execute(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001056 }
1057 }
1058
1059 @Override
1060 public void onShowCustomView(View view,
1061 WebChromeClient.CustomViewCallback callback) {
Michael Kolb14612442011-06-24 13:06:29 -07001062 Activity activity = mWebViewController.getActivity();
1063 if (activity != null) {
1064 onShowCustomView(view, activity.getRequestedOrientation(), callback);
1065 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001066 }
1067
1068 @Override
1069 public void onShowCustomView(View view, int requestedOrientation,
1070 WebChromeClient.CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001071 if (mInForeground) mWebViewController.showCustomView(Tab.this, view,
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001072 requestedOrientation, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001073 }
1074
1075 @Override
1076 public void onHideCustomView() {
Michael Kolb8233fac2010-10-26 16:08:53 -07001077 if (mInForeground) mWebViewController.hideCustomView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001078 }
1079
1080 /**
1081 * The origin has exceeded its database quota.
1082 * @param url the URL that exceeded the quota
1083 * @param databaseIdentifier the identifier of the database on which the
1084 * transaction that caused the quota overflow was run
1085 * @param currentQuota the current quota for the origin.
1086 * @param estimatedSize the estimated size of the database.
1087 * @param totalUsedQuota is the sum of all origins' quota.
1088 * @param quotaUpdater The callback to run when a decision to allow or
1089 * deny quota has been made. Don't forget to call this!
1090 */
1091 @Override
1092 public void onExceededDatabaseQuota(String url,
1093 String databaseIdentifier, long currentQuota, long estimatedSize,
1094 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -07001095 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001096 .onExceededDatabaseQuota(url, databaseIdentifier,
1097 currentQuota, estimatedSize, totalUsedQuota,
1098 quotaUpdater);
1099 }
1100
1101 /**
1102 * The Application Cache has exceeded its max size.
1103 * @param spaceNeeded is the amount of disk space that would be needed
1104 * in order for the last appcache operation to succeed.
1105 * @param totalUsedQuota is the sum of all origins' quota.
1106 * @param quotaUpdater A callback to inform the WebCore thread that a
1107 * new app cache size is available. This callback must always
1108 * be executed at some point to ensure that the sleeping
1109 * WebCore thread is woken up.
1110 */
1111 @Override
1112 public void onReachedMaxAppCacheSize(long spaceNeeded,
1113 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -07001114 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001115 .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota,
1116 quotaUpdater);
1117 }
1118
1119 /**
1120 * Instructs the browser to show a prompt to ask the user to set the
1121 * Geolocation permission state for the specified origin.
1122 * @param origin The origin for which Geolocation permissions are
1123 * requested.
1124 * @param callback The callback to call once the user has set the
1125 * Geolocation permission state.
1126 */
1127 @Override
1128 public void onGeolocationPermissionsShowPrompt(String origin,
1129 GeolocationPermissions.Callback callback) {
1130 if (mInForeground) {
Grace Kloba50c241e2010-04-20 11:07:50 -07001131 getGeolocationPermissionsPrompt().show(origin, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001132 }
1133 }
1134
1135 /**
1136 * Instructs the browser to hide the Geolocation permissions prompt.
1137 */
1138 @Override
1139 public void onGeolocationPermissionsHidePrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001140 if (mInForeground && mGeolocationPermissionsPrompt != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001141 mGeolocationPermissionsPrompt.hide();
1142 }
1143 }
1144
Ben Murdoch65acc352009-11-19 18:16:04 +00001145 /* Adds a JavaScript error message to the system log and if the JS
1146 * console is enabled in the about:debug options, to that console
1147 * also.
Ben Murdochc42addf2010-01-28 15:19:59 +00001148 * @param consoleMessage the message object.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001149 */
1150 @Override
Ben Murdochc42addf2010-01-28 15:19:59 +00001151 public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001152 if (mInForeground) {
1153 // call getErrorConsole(true) so it will create one if needed
1154 ErrorConsoleView errorConsole = getErrorConsole(true);
Ben Murdochc42addf2010-01-28 15:19:59 +00001155 errorConsole.addErrorMessage(consoleMessage);
Michael Kolb8233fac2010-10-26 16:08:53 -07001156 if (mWebViewController.shouldShowErrorConsole()
1157 && errorConsole.getShowState() !=
1158 ErrorConsoleView.SHOW_MAXIMIZED) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001159 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
1160 }
1161 }
Ben Murdochc42addf2010-01-28 15:19:59 +00001162
Jeff Hamilton47654f42010-09-07 09:57:51 -05001163 // Don't log console messages in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001164 if (isPrivateBrowsingEnabled()) return true;
Jeff Hamilton47654f42010-09-07 09:57:51 -05001165
Ben Murdochc42addf2010-01-28 15:19:59 +00001166 String message = "Console: " + consoleMessage.message() + " "
1167 + consoleMessage.sourceId() + ":"
1168 + consoleMessage.lineNumber();
1169
1170 switch (consoleMessage.messageLevel()) {
1171 case TIP:
1172 Log.v(CONSOLE_LOGTAG, message);
1173 break;
1174 case LOG:
1175 Log.i(CONSOLE_LOGTAG, message);
1176 break;
1177 case WARNING:
1178 Log.w(CONSOLE_LOGTAG, message);
1179 break;
1180 case ERROR:
1181 Log.e(CONSOLE_LOGTAG, message);
1182 break;
1183 case DEBUG:
1184 Log.d(CONSOLE_LOGTAG, message);
1185 break;
1186 }
1187
1188 return true;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001189 }
1190
1191 /**
1192 * Ask the browser for an icon to represent a <video> element.
1193 * This icon will be used if the Web page did not specify a poster attribute.
1194 * @return Bitmap The icon or null if no such icon is available.
1195 */
1196 @Override
1197 public Bitmap getDefaultVideoPoster() {
1198 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001199 return mWebViewController.getDefaultVideoPoster();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001200 }
1201 return null;
1202 }
1203
1204 /**
1205 * Ask the host application for a custom progress view to show while
1206 * a <video> is loading.
1207 * @return View The progress view.
1208 */
1209 @Override
1210 public View getVideoLoadingProgressView() {
1211 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001212 return mWebViewController.getVideoLoadingProgressView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001213 }
1214 return null;
1215 }
1216
1217 @Override
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01001218 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001219 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001220 mWebViewController.openFileChooser(uploadMsg, acceptType);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001221 } else {
1222 uploadMsg.onReceiveValue(null);
1223 }
1224 }
1225
1226 /**
1227 * Deliver a list of already-visited URLs
1228 */
1229 @Override
1230 public void getVisitedHistory(final ValueCallback<String[]> callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001231 mWebViewController.getVisitedHistory(callback);
1232 }
Ben Murdoch8029a772010-11-16 11:58:21 +00001233
1234 @Override
1235 public void setupAutoFill(Message message) {
1236 // Prompt the user to set up their profile.
1237 final Message msg = message;
Michael Kolb14612442011-06-24 13:06:29 -07001238 AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
1239 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
Ben Murdoch1d676b62011-01-17 12:54:24 +00001240 Context.LAYOUT_INFLATER_SERVICE);
1241 final View layout = inflater.inflate(R.layout.setup_autofill_dialog, null);
1242
1243 builder.setView(layout)
Ben Murdochb7e6f942011-07-08 13:00:21 +01001244 .setTitle(R.string.autofill_setup_dialog_title)
Ben Murdoch1d676b62011-01-17 12:54:24 +00001245 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1246 @Override
1247 public void onClick(DialogInterface dialog, int id) {
1248 CheckBox disableAutoFill = (CheckBox) layout.findViewById(
1249 R.id.setup_autofill_dialog_disable_autofill);
1250
1251 if (disableAutoFill.isChecked()) {
1252 // Disable autofill and show a toast with how to turn it on again.
John Reck35e9dd62011-04-25 09:01:54 -07001253 mSettings.setAutofillEnabled(false);
Michael Kolb14612442011-06-24 13:06:29 -07001254 Toast.makeText(mContext,
Ben Murdoch1d676b62011-01-17 12:54:24 +00001255 R.string.autofill_setup_dialog_negative_toast,
1256 Toast.LENGTH_LONG).show();
1257 } else {
1258 // Take user to the AutoFill profile editor. When they return,
1259 // we will send the message that we pass here which will trigger
1260 // the form to get filled out with their new profile.
1261 mWebViewController.setupAutoFill(msg);
1262 }
1263 }
1264 })
1265 .setNegativeButton(R.string.cancel, null)
1266 .show();
Ben Murdoch8029a772010-11-16 11:58:21 +00001267 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001268 };
1269
1270 // -------------------------------------------------------------------------
1271 // WebViewClient implementation for the sub window
1272 // -------------------------------------------------------------------------
1273
1274 // Subclass of WebViewClient used in subwindows to notify the main
1275 // WebViewClient of certain WebView activities.
1276 private static class SubWindowClient extends WebViewClient {
1277 // The main WebViewClient.
1278 private final WebViewClient mClient;
Michael Kolb8233fac2010-10-26 16:08:53 -07001279 private final WebViewController mController;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001280
Michael Kolb8233fac2010-10-26 16:08:53 -07001281 SubWindowClient(WebViewClient client, WebViewController controller) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001282 mClient = client;
Michael Kolb8233fac2010-10-26 16:08:53 -07001283 mController = controller;
Leon Scroggins III211ba542010-04-19 13:21:13 -04001284 }
1285 @Override
1286 public void onPageStarted(WebView view, String url, Bitmap favicon) {
1287 // Unlike the others, do not call mClient's version, which would
1288 // change the progress bar. However, we do want to remove the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001289 // find or select dialog.
Michael Kolb8233fac2010-10-26 16:08:53 -07001290 mController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001291 }
1292 @Override
1293 public void doUpdateVisitedHistory(WebView view, String url,
1294 boolean isReload) {
1295 mClient.doUpdateVisitedHistory(view, url, isReload);
1296 }
1297 @Override
1298 public boolean shouldOverrideUrlLoading(WebView view, String url) {
1299 return mClient.shouldOverrideUrlLoading(view, url);
1300 }
1301 @Override
1302 public void onReceivedSslError(WebView view, SslErrorHandler handler,
1303 SslError error) {
1304 mClient.onReceivedSslError(view, handler, error);
1305 }
1306 @Override
Brian Carlstrom8862c1d2011-06-02 01:05:55 -07001307 public void onReceivedClientCertRequest(WebView view,
1308 ClientCertRequestHandler handler, String host_and_port) {
1309 mClient.onReceivedClientCertRequest(view, handler, host_and_port);
1310 }
1311 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -07001312 public void onReceivedHttpAuthRequest(WebView view,
1313 HttpAuthHandler handler, String host, String realm) {
1314 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
1315 }
1316 @Override
1317 public void onFormResubmission(WebView view, Message dontResend,
1318 Message resend) {
1319 mClient.onFormResubmission(view, dontResend, resend);
1320 }
1321 @Override
1322 public void onReceivedError(WebView view, int errorCode,
1323 String description, String failingUrl) {
1324 mClient.onReceivedError(view, errorCode, description, failingUrl);
1325 }
1326 @Override
1327 public boolean shouldOverrideKeyEvent(WebView view,
1328 android.view.KeyEvent event) {
1329 return mClient.shouldOverrideKeyEvent(view, event);
1330 }
1331 @Override
1332 public void onUnhandledKeyEvent(WebView view,
1333 android.view.KeyEvent event) {
1334 mClient.onUnhandledKeyEvent(view, event);
1335 }
1336 }
1337
1338 // -------------------------------------------------------------------------
1339 // WebChromeClient implementation for the sub window
1340 // -------------------------------------------------------------------------
1341
1342 private class SubWindowChromeClient extends WebChromeClient {
1343 // The main WebChromeClient.
1344 private final WebChromeClient mClient;
1345
1346 SubWindowChromeClient(WebChromeClient client) {
1347 mClient = client;
1348 }
1349 @Override
1350 public void onProgressChanged(WebView view, int newProgress) {
1351 mClient.onProgressChanged(view, newProgress);
1352 }
1353 @Override
1354 public boolean onCreateWindow(WebView view, boolean dialog,
1355 boolean userGesture, android.os.Message resultMsg) {
1356 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
1357 }
1358 @Override
1359 public void onCloseWindow(WebView window) {
1360 if (window != mSubView) {
1361 Log.e(LOGTAG, "Can't close the window");
1362 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001363 mWebViewController.dismissSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001364 }
1365 }
1366
1367 // -------------------------------------------------------------------------
1368
1369 // Construct a new tab
Michael Kolb7bcafde2011-05-09 13:55:59 -07001370 Tab(WebViewController wvcontroller, WebView w) {
John Reck1cf4b792011-07-26 10:22:22 -07001371 this(wvcontroller, w, null);
1372 }
1373
1374 Tab(WebViewController wvcontroller, Bundle state) {
1375 this(wvcontroller, null, state);
1376 }
1377
1378 Tab(WebViewController wvcontroller, WebView w, Bundle state) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001379 mWebViewController = wvcontroller;
Michael Kolb14612442011-06-24 13:06:29 -07001380 mContext = mWebViewController.getContext();
John Reck35e9dd62011-04-25 09:01:54 -07001381 mSettings = BrowserSettings.getInstance();
Michael Kolb14612442011-06-24 13:06:29 -07001382 mDataController = DataController.getInstance(mContext);
1383 mCurrentState = new PageState(mContext, w != null
John Reck847b5322011-04-14 17:02:18 -07001384 ? w.isPrivateBrowsingEnabled() : false);
Michael Kolb8233fac2010-10-26 16:08:53 -07001385 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001386 mInForeground = false;
1387
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001388 mDownloadListener = new DownloadListener() {
1389 public void onDownloadStart(String url, String userAgent,
1390 String contentDisposition, String mimetype,
1391 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001392 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001393 mimetype, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001394 }
1395 };
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001396 mWebBackForwardListClient = new WebBackForwardListClient() {
1397 @Override
1398 public void onNewHistoryItem(WebHistoryItem item) {
1399 if (isInVoiceSearchMode()) {
1400 item.setCustomData(mVoiceSearchData.mVoiceSearchIntent);
1401 }
Mathew Inwooda829d552011-09-02 14:16:25 +01001402 if (mClearHistoryUrlPattern != null) {
1403 boolean match =
1404 mClearHistoryUrlPattern.matcher(item.getOriginalUrl()).matches();
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001405 if (LOGD_ENABLED) {
Mathew Inwooda829d552011-09-02 14:16:25 +01001406 Log.d(LOGTAG, "onNewHistoryItem: match=" + match + "\n\t"
1407 + item.getUrl() + "\n\t"
1408 + mClearHistoryUrlPattern);
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001409 }
Mathew Inwooda829d552011-09-02 14:16:25 +01001410 if (match) {
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001411 if (mMainView != null) {
1412 mMainView.clearHistory();
1413 }
1414 }
Mathew Inwooda829d552011-09-02 14:16:25 +01001415 mClearHistoryUrlPattern = null;
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001416 }
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001417 }
1418 @Override
1419 public void onIndexChanged(WebHistoryItem item, int index) {
1420 Object data = item.getCustomData();
1421 if (data != null && data instanceof Intent) {
1422 activateVoiceSearchMode((Intent) data);
1423 }
1424 }
1425 };
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001426
John Reck1cf4b792011-07-26 10:22:22 -07001427 mCaptureWidth = mContext.getResources().getDimensionPixelSize(
1428 R.dimen.tab_thumbnail_width);
1429 mCaptureHeight = mContext.getResources().getDimensionPixelSize(
1430 R.dimen.tab_thumbnail_height);
1431 updateShouldCaptureThumbnails();
1432 restoreState(state);
John Reck52be4782011-08-26 15:37:29 -07001433 if (getId() == -1) {
1434 mId = TabControl.getNextId();
1435 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001436 setWebView(w);
Michael Kolb9ef259a2011-07-12 15:33:08 -07001437 mHandler = new Handler() {
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001438 @Override
Michael Kolb9ef259a2011-07-12 15:33:08 -07001439 public void handleMessage(Message m) {
John Reck1cf4b792011-07-26 10:22:22 -07001440 switch (m.what) {
1441 case MSG_CAPTURE:
1442 capture();
1443 break;
1444 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001445 }
1446 };
John Reck1cf4b792011-07-26 10:22:22 -07001447 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001448
Mathew Inwoode09305e2011-09-02 12:03:26 +01001449 /**
1450 * This is used to get a new ID when the tab has been preloaded, before it is displayed and
1451 * added to TabControl. Preloaded tabs can be created before restoreInstanceState, leading
1452 * to overlapping IDs between the preloaded and restored tabs.
1453 */
1454 public void refreshIdAfterPreload() {
1455 mId = TabControl.getNextId();
1456 }
1457
John Reck1cf4b792011-07-26 10:22:22 -07001458 public void updateShouldCaptureThumbnails() {
1459 if (mWebViewController.shouldCaptureThumbnails()) {
1460 synchronized (Tab.this) {
1461 if (mCapture == null) {
1462 mCapture = Bitmap.createBitmap(mCaptureWidth, mCaptureHeight,
1463 Bitmap.Config.RGB_565);
Michael Kolbc3af0672011-08-09 10:24:41 -07001464 mCapture.eraseColor(Color.WHITE);
John Reck1cf4b792011-07-26 10:22:22 -07001465 if (mInForeground) {
1466 postCapture();
1467 }
1468 }
1469 }
1470 } else {
1471 synchronized (Tab.this) {
1472 mCapture = null;
1473 deleteThumbnail();
1474 }
1475 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001476 }
1477
Michael Kolb14612442011-06-24 13:06:29 -07001478 public void setController(WebViewController ctl) {
1479 mWebViewController = ctl;
John Reck1cf4b792011-07-26 10:22:22 -07001480 updateShouldCaptureThumbnails();
Michael Kolb14612442011-06-24 13:06:29 -07001481 }
1482
Michael Kolbc831b632011-05-11 09:30:34 -07001483 public long getId() {
1484 return mId;
1485 }
1486
Grace Kloba22ac16e2009-10-07 18:00:23 -07001487 /**
1488 * Sets the WebView for this tab, correctly removing the old WebView from
1489 * the container view.
1490 */
1491 void setWebView(WebView w) {
1492 if (mMainView == w) {
1493 return;
1494 }
Michael Kolba713ec82010-11-29 17:27:06 -08001495
Grace Kloba22ac16e2009-10-07 18:00:23 -07001496 // If the WebView is changing, the page will be reloaded, so any ongoing
1497 // Geolocation permission requests are void.
Grace Kloba50c241e2010-04-20 11:07:50 -07001498 if (mGeolocationPermissionsPrompt != null) {
1499 mGeolocationPermissionsPrompt.hide();
1500 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001501
Michael Kolba713ec82010-11-29 17:27:06 -08001502 mWebViewController.onSetWebView(this, w);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001503
John Reck1cf4b792011-07-26 10:22:22 -07001504 if (mMainView != null) {
John Reck8ee633f2011-08-09 16:00:35 -07001505 mMainView.setPictureListener(null);
John Reck1cf4b792011-07-26 10:22:22 -07001506 if (w != null) {
1507 syncCurrentState(w, null);
1508 } else {
1509 mCurrentState = new PageState(mContext, false);
1510 }
1511 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001512 // set the new one
1513 mMainView = w;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001514 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001515 if (mMainView != null) {
1516 mMainView.setWebViewClient(mWebViewClient);
1517 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001518 // Attach DownloadManager so that downloads can start in an active
1519 // or a non-active window. This can happen when going to a site that
1520 // does a redirect after a period of time. The user could have
1521 // switched to another tab while waiting for the download to start.
1522 mMainView.setDownloadListener(mDownloadListener);
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001523 mMainView.setWebBackForwardListClient(mWebBackForwardListClient);
John Reck8ee633f2011-08-09 16:00:35 -07001524 TabControl tc = mWebViewController.getTabControl();
1525 if (tc != null && tc.getOnThumbnailUpdatedListener() != null) {
1526 mMainView.setPictureListener(this);
1527 }
John Reck1cf4b792011-07-26 10:22:22 -07001528 if (mSavedState != null) {
John Reck6c2e2f32011-08-22 13:41:23 -07001529 WebBackForwardList restoredState
1530 = mMainView.restoreState(mSavedState);
1531 if (restoredState == null || restoredState.getSize() == 0) {
1532 Log.w(LOGTAG, "Failed to restore WebView state!");
1533 loadUrl(mCurrentState.mOriginalUrl, null);
1534 }
John Reck1cf4b792011-07-26 10:22:22 -07001535 mSavedState = null;
1536 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001537 }
1538 }
1539
1540 /**
1541 * Destroy the tab's main WebView and subWindow if any
1542 */
1543 void destroy() {
1544 if (mMainView != null) {
1545 dismissSubWindow();
John Reckef654f12011-07-12 16:42:08 -07001546 // Make sure the embedded title bar isn't still attached
1547 mMainView.setEmbeddedTitleBar(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001548 // save the WebView to call destroy() after detach it from the tab
1549 WebView webView = mMainView;
1550 setWebView(null);
1551 webView.destroy();
1552 }
1553 }
1554
1555 /**
1556 * Remove the tab from the parent
1557 */
1558 void removeFromTree() {
1559 // detach the children
Michael Kolbc831b632011-05-11 09:30:34 -07001560 if (mChildren != null) {
1561 for(Tab t : mChildren) {
1562 t.setParent(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001563 }
1564 }
1565 // remove itself from the parent list
Michael Kolbc831b632011-05-11 09:30:34 -07001566 if (mParent != null) {
1567 mParent.mChildren.remove(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001568 }
John Reck1cf4b792011-07-26 10:22:22 -07001569 deleteThumbnail();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001570 }
1571
1572 /**
1573 * Create a new subwindow unless a subwindow already exists.
1574 * @return True if a new subwindow was created. False if one already exists.
1575 */
1576 boolean createSubWindow() {
1577 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001578 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001579 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001580 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001581 mSubView.setWebChromeClient(new SubWindowChromeClient(
1582 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001583 // Set a different DownloadListener for the mSubView, since it will
1584 // just need to dismiss the mSubView, rather than close the Tab
1585 mSubView.setDownloadListener(new DownloadListener() {
1586 public void onDownloadStart(String url, String userAgent,
1587 String contentDisposition, String mimetype,
1588 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001589 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001590 contentDisposition, mimetype, contentLength);
1591 if (mSubView.copyBackForwardList().getSize() == 0) {
1592 // This subwindow was opened for the sole purpose of
1593 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001594 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001595 }
1596 }
1597 });
Michael Kolb14612442011-06-24 13:06:29 -07001598 mSubView.setOnCreateContextMenuListener(mWebViewController.getActivity());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001599 return true;
1600 }
1601 return false;
1602 }
1603
1604 /**
1605 * Dismiss the subWindow for the tab.
1606 */
1607 void dismissSubWindow() {
1608 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001609 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001610 mSubView.destroy();
1611 mSubView = null;
1612 mSubViewContainer = null;
1613 }
1614 }
1615
Grace Kloba22ac16e2009-10-07 18:00:23 -07001616
1617 /**
1618 * Set the parent tab of this tab.
1619 */
Michael Kolbc831b632011-05-11 09:30:34 -07001620 void setParent(Tab parent) {
John Reck52be4782011-08-26 15:37:29 -07001621 if (parent == this) {
1622 throw new IllegalStateException("Cannot set parent to self!");
1623 }
Michael Kolbc831b632011-05-11 09:30:34 -07001624 mParent = parent;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001625 // This tab may have been freed due to low memory. If that is the case,
Michael Kolbc831b632011-05-11 09:30:34 -07001626 // the parent tab id is already saved. If we are changing that id
Grace Kloba22ac16e2009-10-07 18:00:23 -07001627 // (most likely due to removing the parent tab) we must update the
Michael Kolbc831b632011-05-11 09:30:34 -07001628 // parent tab id in the saved Bundle.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001629 if (mSavedState != null) {
1630 if (parent == null) {
1631 mSavedState.remove(PARENTTAB);
1632 } else {
Michael Kolbc831b632011-05-11 09:30:34 -07001633 mSavedState.putLong(PARENTTAB, parent.getId());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001634 }
1635 }
John Reckb0a86db2011-05-24 14:05:58 -07001636
1637 // Sync the WebView useragent with the parent
1638 if (parent != null && mSettings.hasDesktopUseragent(parent.getWebView())
1639 != mSettings.hasDesktopUseragent(getWebView())) {
1640 mSettings.toggleDesktopUseragent(getWebView());
1641 }
John Reck52be4782011-08-26 15:37:29 -07001642
1643 if (parent != null && parent.getId() == getId()) {
1644 throw new IllegalStateException("Parent has same ID as child!");
1645 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001646 }
1647
1648 /**
Michael Kolbc831b632011-05-11 09:30:34 -07001649 * If this Tab was created through another Tab, then this method returns
1650 * that Tab.
1651 * @return the Tab parent or null
1652 */
1653 public Tab getParent() {
1654 return mParent;
1655 }
1656
1657 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001658 * When a Tab is created through the content of another Tab, then we
1659 * associate the Tabs.
1660 * @param child the Tab that was created from this Tab
1661 */
1662 void addChildTab(Tab child) {
Michael Kolbc831b632011-05-11 09:30:34 -07001663 if (mChildren == null) {
1664 mChildren = new Vector<Tab>();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001665 }
Michael Kolbc831b632011-05-11 09:30:34 -07001666 mChildren.add(child);
1667 child.setParent(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001668 }
1669
Michael Kolbc831b632011-05-11 09:30:34 -07001670 Vector<Tab> getChildren() {
1671 return mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001672 }
1673
1674 void resume() {
1675 if (mMainView != null) {
John Reck56c1fcf2011-08-17 10:15:16 -07001676 setupHwAcceleration(mMainView);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001677 mMainView.onResume();
1678 if (mSubView != null) {
1679 mSubView.onResume();
1680 }
1681 }
1682 }
1683
John Reck56c1fcf2011-08-17 10:15:16 -07001684 private void setupHwAcceleration(View web) {
1685 if (web == null) return;
1686 BrowserSettings settings = BrowserSettings.getInstance();
1687 if (settings.isHardwareAccelerated()) {
1688 web.setLayerType(View.LAYER_TYPE_NONE, null);
1689 } else {
1690 web.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
1691 }
1692 }
1693
Grace Kloba22ac16e2009-10-07 18:00:23 -07001694 void pause() {
1695 if (mMainView != null) {
1696 mMainView.onPause();
1697 if (mSubView != null) {
1698 mSubView.onPause();
1699 }
1700 }
1701 }
1702
1703 void putInForeground() {
John Reck8ee633f2011-08-09 16:00:35 -07001704 if (mInForeground) {
1705 return;
1706 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001707 mInForeground = true;
1708 resume();
Michael Kolb14612442011-06-24 13:06:29 -07001709 Activity activity = mWebViewController.getActivity();
1710 mMainView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001711 if (mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -07001712 mSubView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001713 }
1714 // Show the pending error dialog if the queue is not empty
1715 if (mQueuedErrors != null && mQueuedErrors.size() > 0) {
1716 showError(mQueuedErrors.getFirst());
1717 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001718 mWebViewController.bookmarkedStatusHasChanged(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001719 }
1720
1721 void putInBackground() {
John Reck8ee633f2011-08-09 16:00:35 -07001722 if (!mInForeground) {
1723 return;
1724 }
John Reck52be4782011-08-26 15:37:29 -07001725 capture();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001726 mInForeground = false;
1727 pause();
1728 mMainView.setOnCreateContextMenuListener(null);
1729 if (mSubView != null) {
1730 mSubView.setOnCreateContextMenuListener(null);
1731 }
1732 }
1733
Michael Kolb8233fac2010-10-26 16:08:53 -07001734 boolean inForeground() {
1735 return mInForeground;
1736 }
1737
Grace Kloba22ac16e2009-10-07 18:00:23 -07001738 /**
1739 * Return the top window of this tab; either the subwindow if it is not
1740 * null or the main window.
1741 * @return The top window of this tab.
1742 */
1743 WebView getTopWindow() {
1744 if (mSubView != null) {
1745 return mSubView;
1746 }
1747 return mMainView;
1748 }
1749
1750 /**
1751 * Return the main window of this tab. Note: if a tab is freed in the
1752 * background, this can return null. It is only guaranteed to be
1753 * non-null for the current tab.
1754 * @return The main WebView of this tab.
1755 */
1756 WebView getWebView() {
1757 return mMainView;
1758 }
1759
Michael Kolba713ec82010-11-29 17:27:06 -08001760 void setViewContainer(View container) {
1761 mContainer = container;
1762 }
1763
Michael Kolb8233fac2010-10-26 16:08:53 -07001764 View getViewContainer() {
1765 return mContainer;
1766 }
1767
Grace Kloba22ac16e2009-10-07 18:00:23 -07001768 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001769 * Return whether private browsing is enabled for the main window of
1770 * this tab.
1771 * @return True if private browsing is enabled.
1772 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001773 boolean isPrivateBrowsingEnabled() {
John Reck502a3532011-08-16 14:21:46 -07001774 return mCurrentState.mIncognito;
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001775 }
1776
1777 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001778 * Return the subwindow of this tab or null if there is no subwindow.
1779 * @return The subwindow of this tab or null.
1780 */
1781 WebView getSubWebView() {
1782 return mSubView;
1783 }
1784
Michael Kolb1514bb72010-11-22 09:11:48 -08001785 void setSubWebView(WebView subView) {
1786 mSubView = subView;
1787 }
1788
Michael Kolb8233fac2010-10-26 16:08:53 -07001789 View getSubViewContainer() {
1790 return mSubViewContainer;
1791 }
1792
Michael Kolb1514bb72010-11-22 09:11:48 -08001793 void setSubViewContainer(View subViewContainer) {
1794 mSubViewContainer = subViewContainer;
1795 }
1796
Grace Kloba22ac16e2009-10-07 18:00:23 -07001797 /**
1798 * @return The geolocation permissions prompt for this tab.
1799 */
1800 GeolocationPermissionsPrompt getGeolocationPermissionsPrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001801 if (mGeolocationPermissionsPrompt == null) {
1802 ViewStub stub = (ViewStub) mContainer
1803 .findViewById(R.id.geolocation_permissions_prompt);
1804 mGeolocationPermissionsPrompt = (GeolocationPermissionsPrompt) stub
1805 .inflate();
Grace Kloba50c241e2010-04-20 11:07:50 -07001806 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001807 return mGeolocationPermissionsPrompt;
1808 }
1809
1810 /**
1811 * @return The application id string
1812 */
1813 String getAppId() {
1814 return mAppId;
1815 }
1816
1817 /**
1818 * Set the application id string
1819 * @param id
1820 */
1821 void setAppId(String id) {
1822 mAppId = id;
1823 }
1824
Michael Kolbe28b3472011-08-04 16:54:31 -07001825 boolean closeOnBack() {
1826 return mCloseOnBack;
1827 }
1828
1829 void setCloseOnBack(boolean close) {
1830 mCloseOnBack = close;
1831 }
1832
Grace Kloba22ac16e2009-10-07 18:00:23 -07001833 String getUrl() {
John Reck324d4402011-01-11 16:56:42 -08001834 return UrlUtils.filteredUrl(mCurrentState.mUrl);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001835 }
1836
John Reck49a603c2011-03-03 09:33:05 -08001837 String getOriginalUrl() {
John Reckdb22ec42011-06-29 11:31:24 -07001838 if (mCurrentState.mOriginalUrl == null) {
1839 return getUrl();
John Reck49a603c2011-03-03 09:33:05 -08001840 }
John Reckdb22ec42011-06-29 11:31:24 -07001841 return UrlUtils.filteredUrl(mCurrentState.mOriginalUrl);
John Reck49a603c2011-03-03 09:33:05 -08001842 }
1843
Grace Kloba22ac16e2009-10-07 18:00:23 -07001844 /**
John Reck30c714c2010-12-16 17:30:34 -08001845 * Get the title of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001846 */
1847 String getTitle() {
John Reck30c714c2010-12-16 17:30:34 -08001848 if (mCurrentState.mTitle == null && mInPageLoad) {
Michael Kolb14612442011-06-24 13:06:29 -07001849 return mContext.getString(R.string.title_bar_loading);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001850 }
John Reck30c714c2010-12-16 17:30:34 -08001851 return mCurrentState.mTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001852 }
1853
1854 /**
John Reck30c714c2010-12-16 17:30:34 -08001855 * Get the favicon of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001856 */
1857 Bitmap getFavicon() {
John Reck1cf4b792011-07-26 10:22:22 -07001858 if (mCurrentState.mFavicon != null) {
1859 return mCurrentState.mFavicon;
1860 }
1861 return getDefaultFavicon(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001862 }
1863
John Recke969cc52010-12-21 17:24:43 -08001864 public boolean isBookmarkedSite() {
1865 return mCurrentState.mIsBookmarkedSite;
1866 }
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001867
Grace Kloba22ac16e2009-10-07 18:00:23 -07001868 /**
1869 * Return the tab's error console. Creates the console if createIfNEcessary
1870 * is true and we haven't already created the console.
1871 * @param createIfNecessary Flag to indicate if the console should be
1872 * created if it has not been already.
1873 * @return The tab's error console, or null if one has not been created and
1874 * createIfNecessary is false.
1875 */
1876 ErrorConsoleView getErrorConsole(boolean createIfNecessary) {
1877 if (createIfNecessary && mErrorConsole == null) {
Michael Kolb14612442011-06-24 13:06:29 -07001878 mErrorConsole = new ErrorConsoleView(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001879 mErrorConsole.setWebView(mMainView);
1880 }
1881 return mErrorConsole;
1882 }
1883
John Reck30c714c2010-12-16 17:30:34 -08001884 private void setLockIconType(LockIcon icon) {
1885 mCurrentState.mLockIcon = icon;
1886 mWebViewController.onUpdatedLockIcon(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001887 }
1888
1889 /**
1890 * @return The tab's lock icon type.
1891 */
John Reck30c714c2010-12-16 17:30:34 -08001892 LockIcon getLockIconType() {
1893 return mCurrentState.mLockIcon;
1894 }
1895
1896 int getLoadProgress() {
1897 if (mInPageLoad) {
1898 return mPageLoadProgress;
1899 }
1900 return 100;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001901 }
1902
1903 /**
1904 * @return TRUE if onPageStarted is called while onPageFinished is not
1905 * called yet.
1906 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001907 boolean inPageLoad() {
1908 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001909 }
1910
1911 // force mInLoad to be false. This should only be called before closing the
1912 // tab to ensure BrowserActivity's pauseWebViewTimers() is called correctly.
Michael Kolb8233fac2010-10-26 16:08:53 -07001913 void clearInPageLoad() {
1914 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001915 }
1916
Grace Kloba22ac16e2009-10-07 18:00:23 -07001917 /**
John Reck1cf4b792011-07-26 10:22:22 -07001918 * @return The Bundle with the tab's state if it can be saved, otherwise null
Grace Kloba22ac16e2009-10-07 18:00:23 -07001919 */
John Reck1cf4b792011-07-26 10:22:22 -07001920 public Bundle saveState() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001921 // If the WebView is null it means we ran low on memory and we already
1922 // stored the saved state in mSavedState.
1923 if (mMainView == null) {
John Reck1cf4b792011-07-26 10:22:22 -07001924 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001925 }
John Reck6c2e2f32011-08-22 13:41:23 -07001926
1927 if (TextUtils.isEmpty(mCurrentState.mUrl)) {
John Reck1cf4b792011-07-26 10:22:22 -07001928 return null;
John Reck24f18262011-06-17 14:47:20 -07001929 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001930
1931 mSavedState = new Bundle();
John Reck6c2e2f32011-08-22 13:41:23 -07001932 WebBackForwardList savedList = mMainView.saveState(mSavedState);
1933 if (savedList == null || savedList.getSize() == 0) {
1934 Log.w(LOGTAG, "Failed to save back/forward list for "
1935 + mCurrentState.mUrl);
1936 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001937
Michael Kolbc831b632011-05-11 09:30:34 -07001938 mSavedState.putLong(ID, mId);
John Reck30c714c2010-12-16 17:30:34 -08001939 mSavedState.putString(CURRURL, mCurrentState.mUrl);
1940 mSavedState.putString(CURRTITLE, mCurrentState.mTitle);
John Reck1cf4b792011-07-26 10:22:22 -07001941 mSavedState.putBoolean(INCOGNITO, mMainView.isPrivateBrowsingEnabled());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001942 if (mAppId != null) {
1943 mSavedState.putString(APPID, mAppId);
1944 }
Michael Kolbe28b3472011-08-04 16:54:31 -07001945 mSavedState.putBoolean(CLOSEFLAG, mCloseOnBack);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001946 // Remember the parent tab so the relationship can be restored.
Michael Kolbc831b632011-05-11 09:30:34 -07001947 if (mParent != null) {
1948 mSavedState.putLong(PARENTTAB, mParent.mId);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001949 }
John Reckb0a86db2011-05-24 14:05:58 -07001950 mSavedState.putBoolean(USERAGENT,
1951 mSettings.hasDesktopUseragent(getWebView()));
John Reck1cf4b792011-07-26 10:22:22 -07001952 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001953 }
1954
1955 /*
1956 * Restore the state of the tab.
1957 */
John Reck1cf4b792011-07-26 10:22:22 -07001958 private void restoreState(Bundle b) {
1959 mSavedState = b;
1960 if (mSavedState == null) {
1961 return;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001962 }
1963 // Restore the internal state even if the WebView fails to restore.
1964 // This will maintain the app id, original url and close-on-exit values.
Michael Kolbc831b632011-05-11 09:30:34 -07001965 mId = b.getLong(ID);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001966 mAppId = b.getString(APPID);
Michael Kolbe28b3472011-08-04 16:54:31 -07001967 mCloseOnBack = b.getBoolean(CLOSEFLAG);
John Reckb0a86db2011-05-24 14:05:58 -07001968 if (b.getBoolean(USERAGENT)
1969 != mSettings.hasDesktopUseragent(getWebView())) {
1970 mSettings.toggleDesktopUseragent(getWebView());
1971 }
John Reck1cf4b792011-07-26 10:22:22 -07001972 String url = b.getString(CURRURL);
1973 String title = b.getString(CURRTITLE);
1974 boolean incognito = b.getBoolean(INCOGNITO);
1975 mCurrentState = new PageState(mContext, incognito, url, null);
1976 mCurrentState.mTitle = title;
1977 synchronized (Tab.this) {
1978 if (mCapture != null) {
1979 BackgroundHandler.execute(mLoadThumbnail);
1980 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001981 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001982 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04001983
Leon Scroggins1961ed22010-12-07 15:22:21 -05001984 public void updateBookmarkedStatus() {
John Recke969cc52010-12-21 17:24:43 -08001985 mDataController.queryBookmarkStatus(getUrl(), mIsBookmarkCallback);
Leon Scroggins1961ed22010-12-07 15:22:21 -05001986 }
1987
John Recke969cc52010-12-21 17:24:43 -08001988 private DataController.OnQueryUrlIsBookmark mIsBookmarkCallback
1989 = new DataController.OnQueryUrlIsBookmark() {
1990 @Override
1991 public void onQueryUrlIsBookmark(String url, boolean isBookmark) {
1992 if (mCurrentState.mUrl.equals(url)) {
1993 mCurrentState.mIsBookmarkedSite = isBookmark;
1994 mWebViewController.bookmarkedStatusHasChanged(Tab.this);
1995 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001996 }
John Recke969cc52010-12-21 17:24:43 -08001997 };
Michael Kolb1acef692011-03-08 14:12:06 -08001998
Michael Kolbeb95db42011-03-03 10:38:40 -08001999 public Bitmap getScreenshot() {
John Reck1cf4b792011-07-26 10:22:22 -07002000 synchronized (Tab.this) {
2001 return mCapture;
2002 }
Michael Kolbeb95db42011-03-03 10:38:40 -08002003 }
2004
John Reck541f55a2011-06-07 16:34:43 -07002005 public boolean isSnapshot() {
John Reck541f55a2011-06-07 16:34:43 -07002006 return false;
2007 }
2008
John Reckd8c74522011-06-14 08:45:00 -07002009 public ContentValues createSnapshotValues() {
2010 if (mMainView == null) return null;
John Reck8cc92352011-07-06 17:41:52 -07002011 ByteArrayOutputStream bos = new ByteArrayOutputStream();
2012 try {
2013 GZIPOutputStream stream = new GZIPOutputStream(bos);
2014 if (!mMainView.saveViewState(stream)) {
2015 return null;
2016 }
2017 stream.flush();
2018 stream.close();
2019 } catch (Exception e) {
2020 Log.w(LOGTAG, "Failed to save view state", e);
John Reck541f55a2011-06-07 16:34:43 -07002021 return null;
2022 }
John Reck8cc92352011-07-06 17:41:52 -07002023 byte[] data = bos.toByteArray();
John Reckd8c74522011-06-14 08:45:00 -07002024 ContentValues values = new ContentValues();
2025 values.put(Snapshots.TITLE, mCurrentState.mTitle);
2026 values.put(Snapshots.URL, mCurrentState.mUrl);
2027 values.put(Snapshots.VIEWSTATE, data);
2028 values.put(Snapshots.BACKGROUND, mMainView.getPageBackgroundColor());
John Reck8cc92352011-07-06 17:41:52 -07002029 values.put(Snapshots.DATE_CREATED, System.currentTimeMillis());
2030 values.put(Snapshots.FAVICON, compressBitmap(getFavicon()));
2031 Bitmap screenshot = Controller.createScreenshot(mMainView,
2032 Controller.getDesiredThumbnailWidth(mContext),
2033 Controller.getDesiredThumbnailHeight(mContext));
2034 values.put(Snapshots.THUMBNAIL, compressBitmap(screenshot));
John Reckd8c74522011-06-14 08:45:00 -07002035 return values;
John Reck541f55a2011-06-07 16:34:43 -07002036 }
2037
John Reck8cc92352011-07-06 17:41:52 -07002038 public byte[] compressBitmap(Bitmap bitmap) {
2039 if (bitmap == null) {
2040 return null;
2041 }
2042 ByteArrayOutputStream stream = new ByteArrayOutputStream();
2043 bitmap.compress(CompressFormat.PNG, 100, stream);
2044 return stream.toByteArray();
2045 }
2046
John Reck26b18322011-06-21 13:08:58 -07002047 public void loadUrl(String url, Map<String, String> headers) {
2048 if (mMainView != null) {
Michael Kolb14612442011-06-24 13:06:29 -07002049 mCurrentState = new PageState(mContext, false, url, null);
John Reck26b18322011-06-21 13:08:58 -07002050 mWebViewController.onPageStarted(this, mMainView, null);
2051 mMainView.loadUrl(url, headers);
2052 }
2053 }
2054
Michael Kolb9ef259a2011-07-12 15:33:08 -07002055 protected void capture() {
2056 if (mMainView == null || mCapture == null) return;
2057 Canvas c = new Canvas(mCapture);
2058 final int left = mMainView.getScrollX();
2059 final int top = mMainView.getScrollY() + mMainView.getVisibleTitleHeight();
Michael Kolba3194d02011-09-07 11:23:51 -07002060 int state = c.save();
Michael Kolb9ef259a2011-07-12 15:33:08 -07002061 c.translate(-left, -top);
2062 float scale = mCaptureWidth / (float) mMainView.getWidth();
2063 c.scale(scale, scale, left, top);
John Reck8ee633f2011-08-09 16:00:35 -07002064 if (mMainView instanceof BrowserWebView) {
2065 ((BrowserWebView)mMainView).drawContent(c);
2066 } else {
2067 mMainView.draw(c);
2068 }
Michael Kolba3194d02011-09-07 11:23:51 -07002069 c.restoreToCount(state);
2070 // manually anti-alias the edges for the tilt
2071 c.drawRect(0, 0, 1, mCapture.getHeight(), sAlphaPaint);
2072 c.drawRect(mCapture.getWidth() - 1, 0, mCapture.getWidth(),
2073 mCapture.getHeight(), sAlphaPaint);
2074 c.drawRect(0, 0, mCapture.getWidth(), 1, sAlphaPaint);
2075 c.drawRect(0, mCapture.getHeight() - 1, mCapture.getWidth(),
2076 mCapture.getHeight(), sAlphaPaint);
Dianne Hackborn43cfe8a2011-08-02 16:59:35 -07002077 c.setBitmap(null);
John Reck8ee633f2011-08-09 16:00:35 -07002078 mHandler.removeMessages(MSG_CAPTURE);
John Reck1cf4b792011-07-26 10:22:22 -07002079 persistThumbnail();
John Reck8ee633f2011-08-09 16:00:35 -07002080 TabControl tc = mWebViewController.getTabControl();
2081 if (tc != null) {
2082 OnThumbnailUpdatedListener updateListener
2083 = tc.getOnThumbnailUpdatedListener();
2084 if (updateListener != null) {
2085 updateListener.onThumbnailUpdated(this);
2086 }
2087 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07002088 }
2089
2090 @Override
2091 public void onNewPicture(WebView view, Picture picture) {
2092 //update screenshot
John Reck1cf4b792011-07-26 10:22:22 -07002093 postCapture();
2094 }
2095
2096 private void postCapture() {
Michael Kolb9ef259a2011-07-12 15:33:08 -07002097 if (!mHandler.hasMessages(MSG_CAPTURE)) {
2098 mHandler.sendEmptyMessageDelayed(MSG_CAPTURE, CAPTURE_DELAY);
2099 }
2100 }
2101
John Reckef654f12011-07-12 16:42:08 -07002102 public boolean canGoBack() {
2103 return mMainView != null ? mMainView.canGoBack() : false;
2104 }
2105
2106 public boolean canGoForward() {
2107 return mMainView != null ? mMainView.canGoForward() : false;
2108 }
2109
2110 public void goBack() {
2111 if (mMainView != null) {
2112 mMainView.goBack();
2113 }
2114 }
2115
2116 public void goForward() {
2117 if (mMainView != null) {
2118 mMainView.goForward();
2119 }
2120 }
2121
Mathew Inwood1dd8e822011-08-03 14:34:29 +01002122 /**
2123 * Causes the tab back/forward stack to be cleared once, if the given URL is the next URL
2124 * to be added to the stack.
2125 *
2126 * This is used to ensure that preloaded URLs that are not subsequently seen by the user do
2127 * not appear in the back stack.
2128 */
Mathew Inwooda829d552011-09-02 14:16:25 +01002129 public void clearBackStackWhenItemAdded(Pattern urlPattern) {
2130 mClearHistoryUrlPattern = urlPattern;
Mathew Inwood1dd8e822011-08-03 14:34:29 +01002131 }
2132
John Reck1cf4b792011-07-26 10:22:22 -07002133 protected void persistThumbnail() {
2134 BackgroundHandler.execute(mSaveThumbnail);
2135 }
2136
2137 protected void deleteThumbnail() {
2138 BackgroundHandler.execute(mDeleteThumbnail);
2139 }
2140
2141 private void updateCaptureFromBlob(byte[] blob) {
2142 synchronized (Tab.this) {
2143 if (mCapture == null) {
2144 return;
2145 }
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002146 ByteBuffer buffer = ByteBuffer.wrap(blob);
Michael Kolbd837a112011-08-09 14:00:18 -07002147 try {
2148 mCapture.copyPixelsFromBuffer(buffer);
2149 } catch (RuntimeException rex) {
2150 Log.e(LOGTAG, "Load capture has mismatched sizes; buffer: "
2151 + buffer.capacity() + " blob: " + blob.length
2152 + "capture: " + mCapture.getByteCount());
2153 throw rex;
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002154 }
John Reck1cf4b792011-07-26 10:22:22 -07002155 }
2156 }
2157
2158 private byte[] getCaptureBlob() {
2159 synchronized (Tab.this) {
2160 if (mCapture == null) {
2161 return null;
2162 }
2163 ByteBuffer buffer = ByteBuffer.allocate(mCapture.getByteCount());
2164 mCapture.copyPixelsToBuffer(buffer);
2165 return buffer.array();
2166 }
2167 }
2168
2169 private Runnable mSaveThumbnail = new Runnable() {
2170
2171 @Override
2172 public void run() {
2173 byte[] blob = getCaptureBlob();
2174 if (blob == null) {
2175 return;
2176 }
2177 ContentResolver cr = mContext.getContentResolver();
2178 ContentValues values = new ContentValues();
2179 values.put(Thumbnails._ID, mId);
2180 values.put(Thumbnails.THUMBNAIL, blob);
2181 cr.insert(Thumbnails.CONTENT_URI, values);
2182 }
2183 };
2184
2185 private Runnable mDeleteThumbnail = new Runnable() {
2186
2187 @Override
2188 public void run() {
2189 ContentResolver cr = mContext.getContentResolver();
2190 try {
2191 cr.delete(ContentUris.withAppendedId(Thumbnails.CONTENT_URI, mId),
2192 null, null);
2193 } catch (Throwable t) {}
2194 }
2195 };
2196
2197 private Runnable mLoadThumbnail = new Runnable() {
2198
2199 @Override
2200 public void run() {
2201 ContentResolver cr = mContext.getContentResolver();
2202 Cursor c = null;
2203 try {
2204 Uri uri = ContentUris.withAppendedId(Thumbnails.CONTENT_URI, mId);
2205 c = cr.query(uri, new String[] {Thumbnails._ID,
2206 Thumbnails.THUMBNAIL}, null, null, null);
2207 if (c.moveToFirst()) {
2208 byte[] data = c.getBlob(1);
2209 if (data != null && data.length > 0) {
2210 updateCaptureFromBlob(data);
2211 }
2212 }
2213 } finally {
2214 if (c != null) {
2215 c.close();
2216 }
2217 }
2218 }
2219 };
2220
John Reck52be4782011-08-26 15:37:29 -07002221 @Override
2222 public String toString() {
2223 StringBuilder builder = new StringBuilder(100);
2224 builder.append(mId);
2225 builder.append(") has parent: ");
2226 if (getParent() != null) {
2227 builder.append("true[");
2228 builder.append(getParent().getId());
2229 builder.append("]");
2230 } else {
2231 builder.append("false");
2232 }
2233 builder.append(", incog: ");
2234 builder.append(isPrivateBrowsingEnabled());
2235 if (!isPrivateBrowsingEnabled()) {
2236 builder.append(", title: ");
2237 builder.append(getTitle());
2238 builder.append(", url: ");
2239 builder.append(getUrl());
2240 }
2241 return builder.toString();
2242 }
2243
Grace Kloba22ac16e2009-10-07 18:00:23 -07002244}