blob: 8c9dc025c972abae211160735672e805b2bf2f7b [file] [log] [blame]
Grace Kloba22ac16e2009-10-07 18:00:23 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.browser;
18
Michael Kolb8233fac2010-10-26 16:08:53 -070019import android.app.Activity;
Grace Kloba22ac16e2009-10-07 18:00:23 -070020import android.app.AlertDialog;
Leon Scroggins58d56c62010-01-28 15:12:40 -050021import android.app.SearchManager;
Grace Kloba22ac16e2009-10-07 18:00:23 -070022import android.content.ContentResolver;
John Reck1cf4b792011-07-26 10:22:22 -070023import android.content.ContentUris;
John Reckd8c74522011-06-14 08:45:00 -070024import android.content.ContentValues;
John Reck30c714c2010-12-16 17:30:34 -080025import android.content.Context;
Grace Kloba22ac16e2009-10-07 18:00:23 -070026import android.content.DialogInterface;
Michael Kolbfe251992010-07-08 15:41:55 -070027import android.content.DialogInterface.OnCancelListener;
Jeff Hamilton8ce956c2010-08-17 11:13:53 -050028import android.content.Intent;
John Reck1cf4b792011-07-26 10:22:22 -070029import android.database.Cursor;
Grace Kloba22ac16e2009-10-07 18:00:23 -070030import android.graphics.Bitmap;
John Reck8cc92352011-07-06 17:41:52 -070031import android.graphics.Bitmap.CompressFormat;
Michael Kolb9ef259a2011-07-12 15:33:08 -070032import android.graphics.BitmapFactory;
33import android.graphics.Canvas;
Michael Kolbc3af0672011-08-09 10:24:41 -070034import android.graphics.Color;
Michael Kolb9ef259a2011-07-12 15:33:08 -070035import android.graphics.Picture;
Grace Kloba22ac16e2009-10-07 18:00:23 -070036import android.net.Uri;
37import android.net.http.SslError;
Grace Kloba22ac16e2009-10-07 18:00:23 -070038import android.os.Bundle;
Michael Kolb9ef259a2011-07-12 15:33:08 -070039import android.os.Handler;
Grace Kloba22ac16e2009-10-07 18:00:23 -070040import android.os.Message;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +000041import android.os.SystemClock;
Brian Carlstrom8862c1d2011-06-02 01:05:55 -070042import android.security.KeyChain;
Brian Carlstromaa09cd82011-06-09 16:04:40 -070043import android.security.KeyChainAliasCallback;
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -050044import android.speech.RecognizerResultsIntent;
John Reck24f18262011-06-17 14:47:20 -070045import android.text.TextUtils;
Grace Kloba22ac16e2009-10-07 18:00:23 -070046import android.util.Log;
47import android.view.KeyEvent;
48import android.view.LayoutInflater;
49import android.view.View;
Grace Kloba50c241e2010-04-20 11:07:50 -070050import android.view.ViewStub;
Brian Carlstrom8862c1d2011-06-02 01:05:55 -070051import android.webkit.ClientCertRequestHandler;
Ben Murdochc42addf2010-01-28 15:19:59 +000052import android.webkit.ConsoleMessage;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -050053import android.webkit.DownloadListener;
Grace Kloba22ac16e2009-10-07 18:00:23 -070054import android.webkit.GeolocationPermissions;
55import android.webkit.HttpAuthHandler;
56import android.webkit.SslErrorHandler;
57import android.webkit.URLUtil;
58import android.webkit.ValueCallback;
John Reck6c2e2f32011-08-22 13:41:23 -070059import android.webkit.WebBackForwardList;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -050060import android.webkit.WebBackForwardListClient;
Grace Kloba22ac16e2009-10-07 18:00:23 -070061import android.webkit.WebChromeClient;
62import android.webkit.WebHistoryItem;
John Reck438bf462011-01-12 18:11:46 -080063import android.webkit.WebResourceResponse;
Grace Kloba22ac16e2009-10-07 18:00:23 -070064import android.webkit.WebStorage;
65import android.webkit.WebView;
Michael Kolb9ef259a2011-07-12 15:33:08 -070066import android.webkit.WebView.PictureListener;
Grace Kloba22ac16e2009-10-07 18:00:23 -070067import android.webkit.WebViewClient;
Ben Murdoch1d676b62011-01-17 12:54:24 +000068import android.widget.CheckBox;
Mathew Inwood9ad1eac2011-09-15 11:29:50 +010069import android.widget.LinearLayout;
70import android.widget.TextView;
Ben Murdoch8029a772010-11-16 11:58:21 +000071import android.widget.Toast;
Grace Kloba22ac16e2009-10-07 18:00:23 -070072
John Reck8ee633f2011-08-09 16:00:35 -070073import com.android.browser.TabControl.OnThumbnailUpdatedListener;
John Reck541f55a2011-06-07 16:34:43 -070074import com.android.browser.homepages.HomeProvider;
John Reck1cf4b792011-07-26 10:22:22 -070075import com.android.browser.provider.BrowserProvider2.Thumbnails;
John Reck8cc92352011-07-06 17:41:52 -070076import com.android.browser.provider.SnapshotProvider.Snapshots;
John Reck541f55a2011-06-07 16:34:43 -070077import com.android.common.speech.LoggingEvents;
78
79import java.io.ByteArrayOutputStream;
John Reck1cf4b792011-07-26 10:22:22 -070080import java.nio.ByteBuffer;
Michael Kolbfe251992010-07-08 15:41:55 -070081import java.util.ArrayList;
82import java.util.HashMap;
83import java.util.Iterator;
84import java.util.LinkedList;
85import java.util.Map;
86import java.util.Vector;
Mathew Inwooda829d552011-09-02 14:16:25 +010087import java.util.regex.Pattern;
John Reck8cc92352011-07-06 17:41:52 -070088import java.util.zip.GZIPOutputStream;
Michael Kolbfe251992010-07-08 15:41:55 -070089
Grace Kloba22ac16e2009-10-07 18:00:23 -070090/**
91 * Class for maintaining Tabs with a main WebView and a subwindow.
92 */
Michael Kolb9ef259a2011-07-12 15:33:08 -070093class Tab implements PictureListener {
Michael Kolb8233fac2010-10-26 16:08:53 -070094
Grace Kloba22ac16e2009-10-07 18:00:23 -070095 // Log Tag
96 private static final String LOGTAG = "Tab";
Mathew Inwood1dd8e822011-08-03 14:34:29 +010097 private static final boolean LOGD_ENABLED = com.android.browser.Browser.LOGD_ENABLED;
Ben Murdochc42addf2010-01-28 15:19:59 +000098 // Special case the logtag for messages for the Console to make it easier to
99 // filter them and match the logtag used for these messages in older versions
100 // of the browser.
101 private static final String CONSOLE_LOGTAG = "browser";
102
Michael Kolb9ef259a2011-07-12 15:33:08 -0700103 private static final int MSG_CAPTURE = 42;
John Reck8ee633f2011-08-09 16:00:35 -0700104 private static final int CAPTURE_DELAY = 100;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700105
John Reck1cf4b792011-07-26 10:22:22 -0700106 private static Bitmap sDefaultFavicon;
107
John Reck30c714c2010-12-16 17:30:34 -0800108 public enum LockIcon {
109 LOCK_ICON_UNSECURE,
110 LOCK_ICON_SECURE,
111 LOCK_ICON_MIXED,
112 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700113
Michael Kolb14612442011-06-24 13:06:29 -0700114 Context mContext;
John Reckd8c74522011-06-14 08:45:00 -0700115 protected WebViewController mWebViewController;
Michael Kolb8233fac2010-10-26 16:08:53 -0700116
Michael Kolbc831b632011-05-11 09:30:34 -0700117 // The tab ID
John Reckd8c74522011-06-14 08:45:00 -0700118 private long mId = -1;
Michael Kolbc831b632011-05-11 09:30:34 -0700119
Grace Kloba22ac16e2009-10-07 18:00:23 -0700120 // The Geolocation permissions prompt
121 private GeolocationPermissionsPrompt mGeolocationPermissionsPrompt;
122 // Main WebView wrapper
Michael Kolba713ec82010-11-29 17:27:06 -0800123 private View mContainer;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700124 // Main WebView
125 private WebView mMainView;
126 // Subwindow container
127 private View mSubViewContainer;
128 // Subwindow WebView
129 private WebView mSubView;
130 // Saved bundle for when we are running low on memory. It contains the
131 // information needed to restore the WebView if the user goes back to the
132 // tab.
133 private Bundle mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700134 // Parent Tab. This is the Tab that created this Tab, or null if the Tab was
135 // created by the UI
Michael Kolbc831b632011-05-11 09:30:34 -0700136 private Tab mParent;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700137 // Tab that constructed by this Tab. This is used when this Tab is
138 // destroyed, it clears all mParentTab values in the children.
Michael Kolbc831b632011-05-11 09:30:34 -0700139 private Vector<Tab> mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700140 // If true, the tab is in the foreground of the current activity.
141 private boolean mInForeground;
Michael Kolb8233fac2010-10-26 16:08:53 -0700142 // If true, the tab is in page loading state (after onPageStarted,
143 // before onPageFinsihed)
144 private boolean mInPageLoad;
John Reck30c714c2010-12-16 17:30:34 -0800145 // The last reported progress of the current page
146 private int mPageLoadProgress;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000147 // The time the load started, used to find load page time
148 private long mLoadStartTime;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700149 // Application identifier used to find tabs that another application wants
150 // to reuse.
151 private String mAppId;
Michael Kolbe28b3472011-08-04 16:54:31 -0700152 // flag to indicate if tab should be closed on back
153 private boolean mCloseOnBack;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700154 // Keep the original url around to avoid killing the old WebView if the url
155 // has not changed.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700156 // Error console for the tab
157 private ErrorConsoleView mErrorConsole;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -0500158 // The listener that gets invoked when a download is started from the
159 // mMainView
160 private final DownloadListener mDownloadListener;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500161 // Listener used to know when we move forward or back in the history list.
162 private final WebBackForwardListClient mWebBackForwardListClient;
John Recke969cc52010-12-21 17:24:43 -0800163 private DataController mDataController;
Patrick Scott92066772011-03-10 08:46:27 -0500164 // State of the auto-login request.
165 private DeviceAccountLogin mDeviceAccountLogin;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700166
167 // AsyncTask for downloading touch icons
168 DownloadTouchIcon mTouchIconLoader;
169
John Reck35e9dd62011-04-25 09:01:54 -0700170 private BrowserSettings mSettings;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700171 private int mCaptureWidth;
172 private int mCaptureHeight;
173 private Bitmap mCapture;
174 private Handler mHandler;
175
Mathew Inwood1dd8e822011-08-03 14:34:29 +0100176 /**
Mathew Inwood9ad1eac2011-09-15 11:29:50 +0100177 * See {@link #clearBackStackWhenItemAdded(String)}.
Mathew Inwood1dd8e822011-08-03 14:34:29 +0100178 */
Mathew Inwooda829d552011-09-02 14:16:25 +0100179 private Pattern mClearHistoryUrlPattern;
Mathew Inwood1dd8e822011-08-03 14:34:29 +0100180
John Reck1cf4b792011-07-26 10:22:22 -0700181 private static synchronized Bitmap getDefaultFavicon(Context context) {
182 if (sDefaultFavicon == null) {
183 sDefaultFavicon = BitmapFactory.decodeResource(
184 context.getResources(), R.drawable.app_web_browser_sm);
185 }
186 return sDefaultFavicon;
187 }
Michael Kolbeb95db42011-03-03 10:38:40 -0800188
John Reck30c714c2010-12-16 17:30:34 -0800189 // All the state needed for a page
John Reckd8c74522011-06-14 08:45:00 -0700190 protected static class PageState {
John Reck30c714c2010-12-16 17:30:34 -0800191 String mUrl;
John Reckdb22ec42011-06-29 11:31:24 -0700192 String mOriginalUrl;
John Reck30c714c2010-12-16 17:30:34 -0800193 String mTitle;
194 LockIcon mLockIcon;
195 Bitmap mFavicon;
John Reck502a3532011-08-16 14:21:46 -0700196 boolean mIsBookmarkedSite = false;
197 boolean mIncognito = false;
John Reck30c714c2010-12-16 17:30:34 -0800198
199 PageState(Context c, boolean incognito) {
John Reck502a3532011-08-16 14:21:46 -0700200 mIncognito = incognito;
201 if (mIncognito) {
John Reckdb22ec42011-06-29 11:31:24 -0700202 mOriginalUrl = mUrl = "browser:incognito";
John Reck30c714c2010-12-16 17:30:34 -0800203 mTitle = c.getString(R.string.new_incognito_tab);
John Reck30c714c2010-12-16 17:30:34 -0800204 } else {
John Reckdb22ec42011-06-29 11:31:24 -0700205 mOriginalUrl = mUrl = "";
John Reck30c714c2010-12-16 17:30:34 -0800206 mTitle = c.getString(R.string.new_tab);
John Reck30c714c2010-12-16 17:30:34 -0800207 }
John Reck1cf4b792011-07-26 10:22:22 -0700208 mFavicon = null;
John Reck30c714c2010-12-16 17:30:34 -0800209 mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
210 }
211
212 PageState(Context c, boolean incognito, String url, Bitmap favicon) {
John Reck502a3532011-08-16 14:21:46 -0700213 mIncognito = incognito;
John Reckdb22ec42011-06-29 11:31:24 -0700214 mOriginalUrl = mUrl = url;
John Reck30c714c2010-12-16 17:30:34 -0800215 mTitle = null;
216 if (URLUtil.isHttpsUrl(url)) {
217 mLockIcon = LockIcon.LOCK_ICON_SECURE;
218 } else {
219 mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
220 }
John Reck1cf4b792011-07-26 10:22:22 -0700221 mFavicon = favicon;
John Reck30c714c2010-12-16 17:30:34 -0800222 }
John Reck1cf4b792011-07-26 10:22:22 -0700223
Grace Kloba22ac16e2009-10-07 18:00:23 -0700224 }
225
John Reck30c714c2010-12-16 17:30:34 -0800226 // The current/loading page's state
John Reckd8c74522011-06-14 08:45:00 -0700227 protected PageState mCurrentState;
John Reck30c714c2010-12-16 17:30:34 -0800228
Grace Kloba22ac16e2009-10-07 18:00:23 -0700229 // Used for saving and restoring each Tab
Michael Kolbc831b632011-05-11 09:30:34 -0700230 static final String ID = "ID";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700231 static final String CURRURL = "currentUrl";
232 static final String CURRTITLE = "currentTitle";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700233 static final String PARENTTAB = "parentTab";
234 static final String APPID = "appid";
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700235 static final String INCOGNITO = "privateBrowsingEnabled";
John Reckb0a86db2011-05-24 14:05:58 -0700236 static final String USERAGENT = "useragent";
Michael Kolbe28b3472011-08-04 16:54:31 -0700237 static final String CLOSEFLAG = "closeOnBack";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700238
239 // -------------------------------------------------------------------------
240
Leon Scroggins58d56c62010-01-28 15:12:40 -0500241 /**
242 * Private information regarding the latest voice search. If the Tab is not
243 * in voice search mode, this will be null.
244 */
245 private VoiceSearchData mVoiceSearchData;
246 /**
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400247 * Remove voice search mode from this tab.
248 */
249 public void revertVoiceSearchMode() {
250 if (mVoiceSearchData != null) {
251 mVoiceSearchData = null;
252 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700253 mWebViewController.revertVoiceSearchMode(this);
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400254 }
255 }
256 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700257
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400258 /**
Leon Scroggins58d56c62010-01-28 15:12:40 -0500259 * Return whether the tab is in voice search mode.
260 */
261 public boolean isInVoiceSearchMode() {
262 return mVoiceSearchData != null;
263 }
264 /**
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400265 * Return true if the Tab is in voice search mode and the voice search
266 * Intent came with a String identifying that Google provided the Intent.
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500267 */
268 public boolean voiceSearchSourceIsGoogle() {
269 return mVoiceSearchData != null && mVoiceSearchData.mSourceIsGoogle;
270 }
271 /**
Leon Scroggins58d56c62010-01-28 15:12:40 -0500272 * Get the title to display for the current voice search page. If the Tab
273 * is not in voice search mode, return null.
274 */
275 public String getVoiceDisplayTitle() {
276 if (mVoiceSearchData == null) return null;
277 return mVoiceSearchData.mLastVoiceSearchTitle;
278 }
279 /**
280 * Get the latest array of voice search results, to be passed to the
281 * BrowserProvider. If the Tab is not in voice search mode, return null.
282 */
283 public ArrayList<String> getVoiceSearchResults() {
284 if (mVoiceSearchData == null) return null;
285 return mVoiceSearchData.mVoiceSearchResults;
286 }
287 /**
288 * Activate voice search mode.
289 * @param intent Intent which has the results to use, or an index into the
290 * results when reusing the old results.
291 */
292 /* package */ void activateVoiceSearchMode(Intent intent) {
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500293 int index = 0;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500294 ArrayList<String> results = intent.getStringArrayListExtra(
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -0500295 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_STRINGS);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500296 if (results != null) {
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500297 ArrayList<String> urls = intent.getStringArrayListExtra(
298 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_URLS);
299 ArrayList<String> htmls = intent.getStringArrayListExtra(
300 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_HTML);
301 ArrayList<String> baseUrls = intent.getStringArrayListExtra(
302 RecognizerResultsIntent
303 .EXTRA_VOICE_SEARCH_RESULT_HTML_BASE_URLS);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500304 // This tab is now entering voice search mode for the first time, or
305 // a new voice search was done.
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500306 int size = results.size();
307 if (urls == null || size != urls.size()) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500308 throw new AssertionError("improper extras passed in Intent");
309 }
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500310 if (htmls == null || htmls.size() != size || baseUrls == null ||
311 (baseUrls.size() != size && baseUrls.size() != 1)) {
312 // If either of these arrays are empty/incorrectly sized, ignore
313 // them.
314 htmls = null;
315 baseUrls = null;
316 }
317 mVoiceSearchData = new VoiceSearchData(results, urls, htmls,
318 baseUrls);
Leon Scroggins9df94972010-03-08 18:20:35 -0500319 mVoiceSearchData.mHeaders = intent.getParcelableArrayListExtra(
320 RecognizerResultsIntent
321 .EXTRA_VOICE_SEARCH_RESULT_HTTP_HEADERS);
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500322 mVoiceSearchData.mSourceIsGoogle = intent.getBooleanExtra(
323 VoiceSearchData.SOURCE_IS_GOOGLE, false);
Leon Scroggins2ee4a5a2010-03-15 16:56:57 -0400324 mVoiceSearchData.mVoiceSearchIntent = new Intent(intent);
Leon Scrogginse10dde52010-03-08 19:53:03 -0500325 }
326 String extraData = intent.getStringExtra(
327 SearchManager.EXTRA_DATA_KEY);
328 if (extraData != null) {
329 index = Integer.parseInt(extraData);
330 if (index >= mVoiceSearchData.mVoiceSearchResults.size()) {
331 throw new AssertionError("index must be less than "
332 + "size of mVoiceSearchResults");
333 }
334 if (mVoiceSearchData.mSourceIsGoogle) {
335 Intent logIntent = new Intent(
336 LoggingEvents.ACTION_LOG_EVENT);
337 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
338 LoggingEvents.VoiceSearch.N_BEST_CHOOSE);
339 logIntent.putExtra(
340 LoggingEvents.VoiceSearch.EXTRA_N_BEST_CHOOSE_INDEX,
341 index);
Michael Kolb14612442011-06-24 13:06:29 -0700342 mContext.sendBroadcast(logIntent);
Leon Scrogginse10dde52010-03-08 19:53:03 -0500343 }
344 if (mVoiceSearchData.mVoiceSearchIntent != null) {
Leon Scroggins2ee4a5a2010-03-15 16:56:57 -0400345 // Copy the Intent, so that each history item will have its own
346 // Intent, with different (or none) extra data.
347 Intent latest = new Intent(mVoiceSearchData.mVoiceSearchIntent);
348 latest.putExtra(SearchManager.EXTRA_DATA_KEY, extraData);
349 mVoiceSearchData.mVoiceSearchIntent = latest;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500350 }
351 }
352 mVoiceSearchData.mLastVoiceSearchTitle
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500353 = mVoiceSearchData.mVoiceSearchResults.get(index);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500354 if (mInForeground) {
Michael Kolb11d19782011-03-20 10:17:40 -0700355 mWebViewController.activateVoiceSearchMode(
356 mVoiceSearchData.mLastVoiceSearchTitle,
357 mVoiceSearchData.mVoiceSearchResults);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500358 }
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500359 if (mVoiceSearchData.mVoiceSearchHtmls != null) {
360 // When index was found it was already ensured that it was valid
361 String uriString = mVoiceSearchData.mVoiceSearchHtmls.get(index);
362 if (uriString != null) {
363 Uri dataUri = Uri.parse(uriString);
364 if (RecognizerResultsIntent.URI_SCHEME_INLINE.equals(
365 dataUri.getScheme())) {
366 // If there is only one base URL, use it. If there are
367 // more, there will be one for each index, so use the base
368 // URL corresponding to the index.
369 String baseUrl = mVoiceSearchData.mVoiceSearchBaseUrls.get(
370 mVoiceSearchData.mVoiceSearchBaseUrls.size() > 1 ?
371 index : 0);
372 mVoiceSearchData.mLastVoiceSearchUrl = baseUrl;
373 mMainView.loadDataWithBaseURL(baseUrl,
374 uriString.substring(RecognizerResultsIntent
375 .URI_SCHEME_INLINE.length() + 1), "text/html",
376 "utf-8", baseUrl);
377 return;
378 }
379 }
380 }
Leon Scroggins58d56c62010-01-28 15:12:40 -0500381 mVoiceSearchData.mLastVoiceSearchUrl
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500382 = mVoiceSearchData.mVoiceSearchUrls.get(index);
383 if (null == mVoiceSearchData.mLastVoiceSearchUrl) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700384 mVoiceSearchData.mLastVoiceSearchUrl = UrlUtils.smartUrlFilter(
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500385 mVoiceSearchData.mLastVoiceSearchTitle);
386 }
Leon Scroggins9df94972010-03-08 18:20:35 -0500387 Map<String, String> headers = null;
388 if (mVoiceSearchData.mHeaders != null) {
389 int bundleIndex = mVoiceSearchData.mHeaders.size() == 1 ? 0
390 : index;
391 Bundle bundle = mVoiceSearchData.mHeaders.get(bundleIndex);
392 if (bundle != null && !bundle.isEmpty()) {
393 Iterator<String> iter = bundle.keySet().iterator();
394 headers = new HashMap<String, String>();
395 while (iter.hasNext()) {
396 String key = iter.next();
397 headers.put(key, bundle.getString(key));
398 }
399 }
400 }
401 mMainView.loadUrl(mVoiceSearchData.mLastVoiceSearchUrl, headers);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500402 }
403 /* package */ static class VoiceSearchData {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500404 public VoiceSearchData(ArrayList<String> results,
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500405 ArrayList<String> urls, ArrayList<String> htmls,
406 ArrayList<String> baseUrls) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500407 mVoiceSearchResults = results;
408 mVoiceSearchUrls = urls;
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500409 mVoiceSearchHtmls = htmls;
410 mVoiceSearchBaseUrls = baseUrls;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500411 }
412 /*
413 * ArrayList of suggestions to be displayed when opening the
414 * SearchManager
415 */
416 public ArrayList<String> mVoiceSearchResults;
417 /*
418 * ArrayList of urls, associated with the suggestions in
419 * mVoiceSearchResults.
420 */
421 public ArrayList<String> mVoiceSearchUrls;
422 /*
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500423 * ArrayList holding content to load for each item in
424 * mVoiceSearchResults.
425 */
426 public ArrayList<String> mVoiceSearchHtmls;
427 /*
428 * ArrayList holding base urls for the items in mVoiceSearchResults.
429 * If non null, this will either have the same size as
430 * mVoiceSearchResults or have a size of 1, in which case all will use
431 * the same base url
432 */
433 public ArrayList<String> mVoiceSearchBaseUrls;
434 /*
Leon Scroggins58d56c62010-01-28 15:12:40 -0500435 * The last url provided by voice search. Used for comparison to see if
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500436 * we are going to a page by some method besides voice search.
Leon Scroggins58d56c62010-01-28 15:12:40 -0500437 */
438 public String mLastVoiceSearchUrl;
439 /**
440 * The last title used for voice search. Needed to update the title bar
441 * when switching tabs.
442 */
443 public String mLastVoiceSearchTitle;
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500444 /**
445 * Whether the Intent which turned on voice search mode contained the
446 * String signifying that Google was the source.
447 */
448 public boolean mSourceIsGoogle;
449 /**
Leon Scroggins9df94972010-03-08 18:20:35 -0500450 * List of headers to be passed into the WebView containing location
451 * information
452 */
453 public ArrayList<Bundle> mHeaders;
454 /**
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500455 * The Intent used to invoke voice search. Placed on the
456 * WebHistoryItem so that when coming back to a previous voice search
457 * page we can again activate voice search.
458 */
Leon Scrogginse10dde52010-03-08 19:53:03 -0500459 public Intent mVoiceSearchIntent;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500460 /**
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500461 * String used to identify Google as the source of voice search.
462 */
463 public static String SOURCE_IS_GOOGLE
464 = "android.speech.extras.SOURCE_IS_GOOGLE";
Leon Scroggins58d56c62010-01-28 15:12:40 -0500465 }
466
Grace Kloba22ac16e2009-10-07 18:00:23 -0700467 // Container class for the next error dialog that needs to be displayed
468 private class ErrorDialog {
469 public final int mTitle;
470 public final String mDescription;
471 public final int mError;
472 ErrorDialog(int title, String desc, int error) {
473 mTitle = title;
474 mDescription = desc;
475 mError = error;
476 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700477 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700478
479 private void processNextError() {
480 if (mQueuedErrors == null) {
481 return;
482 }
483 // The first one is currently displayed so just remove it.
484 mQueuedErrors.removeFirst();
485 if (mQueuedErrors.size() == 0) {
486 mQueuedErrors = null;
487 return;
488 }
489 showError(mQueuedErrors.getFirst());
490 }
491
492 private DialogInterface.OnDismissListener mDialogListener =
493 new DialogInterface.OnDismissListener() {
494 public void onDismiss(DialogInterface d) {
495 processNextError();
496 }
497 };
498 private LinkedList<ErrorDialog> mQueuedErrors;
499
500 private void queueError(int err, String desc) {
501 if (mQueuedErrors == null) {
502 mQueuedErrors = new LinkedList<ErrorDialog>();
503 }
504 for (ErrorDialog d : mQueuedErrors) {
505 if (d.mError == err) {
506 // Already saw a similar error, ignore the new one.
507 return;
508 }
509 }
510 ErrorDialog errDialog = new ErrorDialog(
511 err == WebViewClient.ERROR_FILE_NOT_FOUND ?
512 R.string.browserFrameFileErrorLabel :
513 R.string.browserFrameNetworkErrorLabel,
514 desc, err);
515 mQueuedErrors.addLast(errDialog);
516
517 // Show the dialog now if the queue was empty and it is in foreground
518 if (mQueuedErrors.size() == 1 && mInForeground) {
519 showError(errDialog);
520 }
521 }
522
523 private void showError(ErrorDialog errDialog) {
524 if (mInForeground) {
Michael Kolb14612442011-06-24 13:06:29 -0700525 AlertDialog d = new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700526 .setTitle(errDialog.mTitle)
527 .setMessage(errDialog.mDescription)
528 .setPositiveButton(R.string.ok, null)
529 .create();
530 d.setOnDismissListener(mDialogListener);
531 d.show();
532 }
533 }
534
535 // -------------------------------------------------------------------------
536 // WebViewClient implementation for the main WebView
537 // -------------------------------------------------------------------------
538
539 private final WebViewClient mWebViewClient = new WebViewClient() {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500540 private Message mDontResend;
541 private Message mResend;
Michael Kolb47bd1e42011-09-01 15:25:00 -0700542
543 private boolean providersDiffer(String url, String otherUrl) {
544 Uri uri1 = Uri.parse(url);
545 Uri uri2 = Uri.parse(otherUrl);
546 return !uri1.getEncodedAuthority().equals(uri2.getEncodedAuthority());
547 }
548
Grace Kloba22ac16e2009-10-07 18:00:23 -0700549 @Override
550 public void onPageStarted(WebView view, String url, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700551 mInPageLoad = true;
John Reck30c714c2010-12-16 17:30:34 -0800552 mPageLoadProgress = 0;
Michael Kolb14612442011-06-24 13:06:29 -0700553 mCurrentState = new PageState(mContext,
John Reck30c714c2010-12-16 17:30:34 -0800554 view.isPrivateBrowsingEnabled(), url, favicon);
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000555 mLoadStartTime = SystemClock.uptimeMillis();
Leon Scroggins58d56c62010-01-28 15:12:40 -0500556 if (mVoiceSearchData != null
Michael Kolb47bd1e42011-09-01 15:25:00 -0700557 && providersDiffer(url, mVoiceSearchData.mLastVoiceSearchUrl)) {
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500558 if (mVoiceSearchData.mSourceIsGoogle) {
559 Intent i = new Intent(LoggingEvents.ACTION_LOG_EVENT);
560 i.putExtra(LoggingEvents.EXTRA_FLUSH, true);
Michael Kolb14612442011-06-24 13:06:29 -0700561 mContext.sendBroadcast(i);
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500562 }
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400563 revertVoiceSearchMode();
Leon Scroggins58d56c62010-01-28 15:12:40 -0500564 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700565
Grace Kloba22ac16e2009-10-07 18:00:23 -0700566
567 // If we start a touch icon load and then load a new page, we don't
568 // want to cancel the current touch icon loader. But, we do want to
569 // create a new one when the touch icon url is known.
570 if (mTouchIconLoader != null) {
571 mTouchIconLoader.mTab = null;
572 mTouchIconLoader = null;
573 }
574
575 // reset the error console
576 if (mErrorConsole != null) {
577 mErrorConsole.clearErrorMessages();
Michael Kolb8233fac2010-10-26 16:08:53 -0700578 if (mWebViewController.shouldShowErrorConsole()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700579 mErrorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
580 }
581 }
582
Patrick Scott92066772011-03-10 08:46:27 -0500583 // Cancel the auto-login process.
584 if (mDeviceAccountLogin != null) {
585 mDeviceAccountLogin.cancel();
586 mDeviceAccountLogin = null;
587 mWebViewController.hideAutoLogin(Tab.this);
588 }
589
Grace Kloba22ac16e2009-10-07 18:00:23 -0700590 // finally update the UI in the activity if it is in the foreground
John Reck324d4402011-01-11 16:56:42 -0800591 mWebViewController.onPageStarted(Tab.this, view, favicon);
Leon Scroggins4cd97792010-12-03 15:31:56 -0500592
John Recke969cc52010-12-21 17:24:43 -0800593 updateBookmarkedStatus();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700594 }
595
596 @Override
597 public void onPageFinished(WebView view, String url) {
John Recka1696282011-07-08 14:10:37 -0700598 if (!mInPageLoad) {
599 // In page navigation links (www.something.com#footer) will
600 // trigger an onPageFinished which we don't care about.
601 return;
602 }
John Reck5b691842010-11-29 11:21:13 -0800603 if (!isPrivateBrowsingEnabled()) {
604 LogTag.logPageFinishedLoading(
605 url, SystemClock.uptimeMillis() - mLoadStartTime);
606 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700607 mInPageLoad = false;
John Reck1cf4b792011-07-26 10:22:22 -0700608 syncCurrentState(view, url);
John Reck324d4402011-01-11 16:56:42 -0800609 mWebViewController.onPageFinished(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700610 }
611
612 // return true if want to hijack the url to let another app to handle it
613 @Override
614 public boolean shouldOverrideUrlLoading(WebView view, String url) {
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400615 if (voiceSearchSourceIsGoogle()) {
616 // This method is called when the user clicks on a link.
617 // VoiceSearchMode is turned off when the user leaves the
618 // Google results page, so at this point the user must be on
619 // that page. If the user clicked a link on that page, assume
620 // that the voice search was effective, and broadcast an Intent
621 // so a receiver can take note of that fact.
622 Intent logIntent = new Intent(LoggingEvents.ACTION_LOG_EVENT);
623 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
624 LoggingEvents.VoiceSearch.RESULT_CLICKED);
Michael Kolb14612442011-06-24 13:06:29 -0700625 mContext.sendBroadcast(logIntent);
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400626 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700627 if (mInForeground) {
Michael Kolb18eb3772010-12-10 14:29:51 -0800628 return mWebViewController.shouldOverrideUrlLoading(Tab.this,
629 view, url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700630 } else {
631 return false;
632 }
633 }
634
635 /**
636 * Updates the lock icon. This method is called when we discover another
637 * resource to be loaded for this page (for example, javascript). While
638 * we update the icon type, we do not update the lock icon itself until
639 * we are done loading, it is slightly more secure this way.
640 */
641 @Override
642 public void onLoadResource(WebView view, String url) {
643 if (url != null && url.length() > 0) {
644 // It is only if the page claims to be secure that we may have
645 // to update the lock:
John Reck30c714c2010-12-16 17:30:34 -0800646 if (mCurrentState.mLockIcon == LockIcon.LOCK_ICON_SECURE) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700647 // If NOT a 'safe' url, change the lock to mixed content!
648 if (!(URLUtil.isHttpsUrl(url) || URLUtil.isDataUrl(url)
649 || URLUtil.isAboutUrl(url))) {
John Reck30c714c2010-12-16 17:30:34 -0800650 mCurrentState.mLockIcon = LockIcon.LOCK_ICON_MIXED;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700651 }
652 }
653 }
654 }
655
656 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700657 * Show a dialog informing the user of the network error reported by
658 * WebCore if it is in the foreground.
659 */
660 @Override
661 public void onReceivedError(WebView view, int errorCode,
662 String description, String failingUrl) {
663 if (errorCode != WebViewClient.ERROR_HOST_LOOKUP &&
664 errorCode != WebViewClient.ERROR_CONNECT &&
665 errorCode != WebViewClient.ERROR_BAD_URL &&
666 errorCode != WebViewClient.ERROR_UNSUPPORTED_SCHEME &&
667 errorCode != WebViewClient.ERROR_FILE) {
668 queueError(errorCode, description);
669 }
Jeff Hamilton47654f42010-09-07 09:57:51 -0500670
671 // Don't log URLs when in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -0700672 if (!isPrivateBrowsingEnabled()) {
Jeff Hamilton47654f42010-09-07 09:57:51 -0500673 Log.e(LOGTAG, "onReceivedError " + errorCode + " " + failingUrl
674 + " " + description);
675 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700676 }
677
678 /**
679 * Check with the user if it is ok to resend POST data as the page they
680 * are trying to navigate to is the result of a POST.
681 */
682 @Override
683 public void onFormResubmission(WebView view, final Message dontResend,
684 final Message resend) {
685 if (!mInForeground) {
686 dontResend.sendToTarget();
687 return;
688 }
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500689 if (mDontResend != null) {
690 Log.w(LOGTAG, "onFormResubmission should not be called again "
691 + "while dialog is still up");
692 dontResend.sendToTarget();
693 return;
694 }
695 mDontResend = dontResend;
696 mResend = resend;
Michael Kolb14612442011-06-24 13:06:29 -0700697 new AlertDialog.Builder(mContext).setTitle(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700698 R.string.browserFrameFormResubmitLabel).setMessage(
699 R.string.browserFrameFormResubmitMessage)
700 .setPositiveButton(R.string.ok,
701 new DialogInterface.OnClickListener() {
702 public void onClick(DialogInterface dialog,
703 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500704 if (mResend != null) {
705 mResend.sendToTarget();
706 mResend = null;
707 mDontResend = null;
708 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700709 }
710 }).setNegativeButton(R.string.cancel,
711 new DialogInterface.OnClickListener() {
712 public void onClick(DialogInterface dialog,
713 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500714 if (mDontResend != null) {
715 mDontResend.sendToTarget();
716 mResend = null;
717 mDontResend = null;
718 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700719 }
720 }).setOnCancelListener(new OnCancelListener() {
721 public void onCancel(DialogInterface dialog) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500722 if (mDontResend != null) {
723 mDontResend.sendToTarget();
724 mResend = null;
725 mDontResend = null;
726 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700727 }
728 }).show();
729 }
730
731 /**
732 * Insert the url into the visited history database.
733 * @param url The url to be inserted.
734 * @param isReload True if this url is being reloaded.
735 * FIXME: Not sure what to do when reloading the page.
736 */
737 @Override
738 public void doUpdateVisitedHistory(WebView view, String url,
739 boolean isReload) {
John Reck324d4402011-01-11 16:56:42 -0800740 mWebViewController.doUpdateVisitedHistory(Tab.this, isReload);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700741 }
742
743 /**
744 * Displays SSL error(s) dialog to the user.
745 */
746 @Override
747 public void onReceivedSslError(final WebView view,
748 final SslErrorHandler handler, final SslError error) {
749 if (!mInForeground) {
750 handler.cancel();
John Reck30c714c2010-12-16 17:30:34 -0800751 setLockIconType(LockIcon.LOCK_ICON_UNSECURE);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700752 return;
753 }
John Reck35e9dd62011-04-25 09:01:54 -0700754 if (mSettings.showSecurityWarnings()) {
John Reckcb28b2c2011-08-26 17:39:44 -0700755 new AlertDialog.Builder(mContext)
756 .setTitle(R.string.security_warning)
757 .setMessage(R.string.ssl_warnings_header)
758 .setIcon(android.R.drawable.ic_dialog_alert)
759 .setPositiveButton(R.string.ssl_continue,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700760 new DialogInterface.OnClickListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700761 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700762 public void onClick(DialogInterface dialog,
763 int whichButton) {
764 handler.proceed();
765 }
John Reckcb28b2c2011-08-26 17:39:44 -0700766 })
767 .setNeutralButton(R.string.view_certificate,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700768 new DialogInterface.OnClickListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700769 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700770 public void onClick(DialogInterface dialog,
771 int whichButton) {
John Reckcb28b2c2011-08-26 17:39:44 -0700772 mWebViewController.showSslCertificateOnError(
773 view, handler, error);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700774 }
John Reckcb28b2c2011-08-26 17:39:44 -0700775 })
776 .setNegativeButton(R.string.ssl_go_back,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700777 new DialogInterface.OnClickListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700778 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700779 public void onClick(DialogInterface dialog,
780 int whichButton) {
John Reck30c714c2010-12-16 17:30:34 -0800781 dialog.cancel();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700782 }
John Reckcb28b2c2011-08-26 17:39:44 -0700783 })
784 .setOnCancelListener(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700785 new DialogInterface.OnCancelListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700786 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700787 public void onCancel(DialogInterface dialog) {
788 handler.cancel();
John Reck30c714c2010-12-16 17:30:34 -0800789 setLockIconType(LockIcon.LOCK_ICON_UNSECURE);
790 mWebViewController.onUserCanceledSsl(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700791 }
John Reckcb28b2c2011-08-26 17:39:44 -0700792 })
793 .show();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700794 } else {
795 handler.proceed();
796 }
797 }
798
799 /**
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700800 * Displays client certificate request to the user.
801 */
802 @Override
803 public void onReceivedClientCertRequest(final WebView view,
804 final ClientCertRequestHandler handler, final String host_and_port) {
805 if (!mInForeground) {
806 handler.ignore();
807 return;
808 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700809 int colon = host_and_port.lastIndexOf(':');
810 String host;
811 int port;
812 if (colon == -1) {
813 host = host_and_port;
814 port = -1;
815 } else {
816 String portString = host_and_port.substring(colon + 1);
817 try {
818 port = Integer.parseInt(portString);
819 host = host_and_port.substring(0, colon);
820 } catch (NumberFormatException e) {
821 host = host_and_port;
822 port = -1;
823 }
824 }
Michael Kolb14612442011-06-24 13:06:29 -0700825 KeyChain.choosePrivateKeyAlias(
826 mWebViewController.getActivity(), new KeyChainAliasCallback() {
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700827 @Override public void alias(String alias) {
828 if (alias == null) {
829 handler.cancel();
830 return;
831 }
Michael Kolb14612442011-06-24 13:06:29 -0700832 new KeyChainLookup(mContext, handler, alias).execute();
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700833 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700834 }, null, null, host, port, null);
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700835 }
836
837 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700838 * Handles an HTTP authentication request.
839 *
840 * @param handler The authentication handler
841 * @param host The host
842 * @param realm The realm
843 */
844 @Override
845 public void onReceivedHttpAuthRequest(WebView view,
846 final HttpAuthHandler handler, final String host,
847 final String realm) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700848 mWebViewController.onReceivedHttpAuthRequest(Tab.this, view, handler, host, realm);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700849 }
850
851 @Override
John Reck438bf462011-01-12 18:11:46 -0800852 public WebResourceResponse shouldInterceptRequest(WebView view,
853 String url) {
854 WebResourceResponse res = HomeProvider.shouldInterceptRequest(
Michael Kolb14612442011-06-24 13:06:29 -0700855 mContext, url);
John Reck438bf462011-01-12 18:11:46 -0800856 return res;
857 }
858
859 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700860 public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
861 if (!mInForeground) {
862 return false;
863 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700864 return mWebViewController.shouldOverrideKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700865 }
866
867 @Override
868 public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700869 if (!mInForeground) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700870 return;
871 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700872 mWebViewController.onUnhandledKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700873 }
Patrick Scott92066772011-03-10 08:46:27 -0500874
875 @Override
876 public void onReceivedLoginRequest(WebView view, String realm,
877 String account, String args) {
Michael Kolb14612442011-06-24 13:06:29 -0700878 new DeviceAccountLogin(mWebViewController.getActivity(), view, Tab.this, mWebViewController)
Patrick Scott92066772011-03-10 08:46:27 -0500879 .handleLogin(realm, account, args);
880 }
881
Grace Kloba22ac16e2009-10-07 18:00:23 -0700882 };
883
John Reck1cf4b792011-07-26 10:22:22 -0700884 private void syncCurrentState(WebView view, String url) {
885 // Sync state (in case of stop/timeout)
886 mCurrentState.mUrl = view.getUrl();
887 if (mCurrentState.mUrl == null) {
John Reck8bcafc12011-08-29 16:43:02 -0700888 mCurrentState.mUrl = "";
John Reck1cf4b792011-07-26 10:22:22 -0700889 }
890 mCurrentState.mOriginalUrl = view.getOriginalUrl();
891 mCurrentState.mTitle = view.getTitle();
892 mCurrentState.mFavicon = view.getFavicon();
893 if (!URLUtil.isHttpsUrl(mCurrentState.mUrl)) {
894 // In case we stop when loading an HTTPS page from an HTTP page
895 // but before a provisional load occurred
896 mCurrentState.mLockIcon = LockIcon.LOCK_ICON_UNSECURE;
897 }
John Reck502a3532011-08-16 14:21:46 -0700898 mCurrentState.mIncognito = view.isPrivateBrowsingEnabled();
John Reck1cf4b792011-07-26 10:22:22 -0700899 }
900
Patrick Scott92066772011-03-10 08:46:27 -0500901 // Called by DeviceAccountLogin when the Tab needs to have the auto-login UI
902 // displayed.
903 void setDeviceAccountLogin(DeviceAccountLogin login) {
904 mDeviceAccountLogin = login;
905 }
906
907 // Returns non-null if the title bar should display the auto-login UI.
908 DeviceAccountLogin getDeviceAccountLogin() {
909 return mDeviceAccountLogin;
910 }
911
Grace Kloba22ac16e2009-10-07 18:00:23 -0700912 // -------------------------------------------------------------------------
913 // WebChromeClient implementation for the main WebView
914 // -------------------------------------------------------------------------
915
916 private final WebChromeClient mWebChromeClient = new WebChromeClient() {
917 // Helper method to create a new tab or sub window.
918 private void createWindow(final boolean dialog, final Message msg) {
919 WebView.WebViewTransport transport =
920 (WebView.WebViewTransport) msg.obj;
921 if (dialog) {
922 createSubWindow();
Michael Kolb8233fac2010-10-26 16:08:53 -0700923 mWebViewController.attachSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700924 transport.setWebView(mSubView);
925 } else {
Michael Kolb7bcafde2011-05-09 13:55:59 -0700926 final Tab newTab = mWebViewController.openTab(null,
John Reck5949c662011-05-27 09:52:29 -0700927 Tab.this, true, true);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700928 transport.setWebView(newTab.getWebView());
929 }
930 msg.sendToTarget();
931 }
932
933 @Override
934 public boolean onCreateWindow(WebView view, final boolean dialog,
935 final boolean userGesture, final Message resultMsg) {
936 // only allow new window or sub window for the foreground case
937 if (!mInForeground) {
938 return false;
939 }
940 // Short-circuit if we can't create any more tabs or sub windows.
941 if (dialog && mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -0700942 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700943 .setTitle(R.string.too_many_subwindows_dialog_title)
944 .setIcon(android.R.drawable.ic_dialog_alert)
945 .setMessage(R.string.too_many_subwindows_dialog_message)
946 .setPositiveButton(R.string.ok, null)
947 .show();
948 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700949 } else if (!mWebViewController.getTabControl().canCreateNewTab()) {
Michael Kolb14612442011-06-24 13:06:29 -0700950 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700951 .setTitle(R.string.too_many_windows_dialog_title)
952 .setIcon(android.R.drawable.ic_dialog_alert)
953 .setMessage(R.string.too_many_windows_dialog_message)
954 .setPositiveButton(R.string.ok, null)
955 .show();
956 return false;
957 }
958
959 // Short-circuit if this was a user gesture.
960 if (userGesture) {
961 createWindow(dialog, resultMsg);
962 return true;
963 }
964
965 // Allow the popup and create the appropriate window.
966 final AlertDialog.OnClickListener allowListener =
967 new AlertDialog.OnClickListener() {
968 public void onClick(DialogInterface d,
969 int which) {
970 createWindow(dialog, resultMsg);
971 }
972 };
973
974 // Block the popup by returning a null WebView.
975 final AlertDialog.OnClickListener blockListener =
976 new AlertDialog.OnClickListener() {
977 public void onClick(DialogInterface d, int which) {
978 resultMsg.sendToTarget();
979 }
980 };
981
982 // Build a confirmation dialog to display to the user.
983 final AlertDialog d =
Michael Kolb14612442011-06-24 13:06:29 -0700984 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700985 .setTitle(R.string.attention)
986 .setIcon(android.R.drawable.ic_dialog_alert)
987 .setMessage(R.string.popup_window_attempt)
988 .setPositiveButton(R.string.allow, allowListener)
989 .setNegativeButton(R.string.block, blockListener)
990 .setCancelable(false)
991 .create();
992
993 // Show the confirmation dialog.
994 d.show();
995 return true;
996 }
997
998 @Override
Patrick Scotteb5061b2009-11-18 15:00:30 -0500999 public void onRequestFocus(WebView view) {
1000 if (!mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -07001001 mWebViewController.switchToTab(Tab.this);
Patrick Scotteb5061b2009-11-18 15:00:30 -05001002 }
1003 }
1004
1005 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -07001006 public void onCloseWindow(WebView window) {
Michael Kolbc831b632011-05-11 09:30:34 -07001007 if (mParent != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001008 // JavaScript can only close popup window.
1009 if (mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -07001010 mWebViewController.switchToTab(mParent);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001011 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001012 mWebViewController.closeTab(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001013 }
1014 }
1015
1016 @Override
1017 public void onProgressChanged(WebView view, int newProgress) {
John Reck30c714c2010-12-16 17:30:34 -08001018 mPageLoadProgress = newProgress;
1019 mWebViewController.onProgressChanged(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001020 }
1021
1022 @Override
Leon Scroggins21d9b902010-03-11 09:33:11 -05001023 public void onReceivedTitle(WebView view, final String title) {
John Reck30c714c2010-12-16 17:30:34 -08001024 mCurrentState.mTitle = title;
Michael Kolb8233fac2010-10-26 16:08:53 -07001025 mWebViewController.onReceivedTitle(Tab.this, title);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001026 }
1027
1028 @Override
1029 public void onReceivedIcon(WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -08001030 mCurrentState.mFavicon = icon;
Michael Kolb8233fac2010-10-26 16:08:53 -07001031 mWebViewController.onFavicon(Tab.this, view, icon);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001032 }
1033
1034 @Override
1035 public void onReceivedTouchIconUrl(WebView view, String url,
1036 boolean precomposed) {
Michael Kolb14612442011-06-24 13:06:29 -07001037 final ContentResolver cr = mContext.getContentResolver();
Leon Scrogginsc8393d92010-04-23 14:58:16 -04001038 // Let precomposed icons take precedence over non-composed
1039 // icons.
1040 if (precomposed && mTouchIconLoader != null) {
1041 mTouchIconLoader.cancel(false);
1042 mTouchIconLoader = null;
1043 }
1044 // Have only one async task at a time.
1045 if (mTouchIconLoader == null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001046 mTouchIconLoader = new DownloadTouchIcon(Tab.this,
Michael Kolb14612442011-06-24 13:06:29 -07001047 mContext, cr, view);
Leon Scrogginsc8393d92010-04-23 14:58:16 -04001048 mTouchIconLoader.execute(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001049 }
1050 }
1051
1052 @Override
1053 public void onShowCustomView(View view,
1054 WebChromeClient.CustomViewCallback callback) {
Michael Kolb14612442011-06-24 13:06:29 -07001055 Activity activity = mWebViewController.getActivity();
1056 if (activity != null) {
1057 onShowCustomView(view, activity.getRequestedOrientation(), callback);
1058 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001059 }
1060
1061 @Override
1062 public void onShowCustomView(View view, int requestedOrientation,
1063 WebChromeClient.CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001064 if (mInForeground) mWebViewController.showCustomView(Tab.this, view,
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001065 requestedOrientation, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001066 }
1067
1068 @Override
1069 public void onHideCustomView() {
Michael Kolb8233fac2010-10-26 16:08:53 -07001070 if (mInForeground) mWebViewController.hideCustomView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001071 }
1072
1073 /**
1074 * The origin has exceeded its database quota.
1075 * @param url the URL that exceeded the quota
1076 * @param databaseIdentifier the identifier of the database on which the
1077 * transaction that caused the quota overflow was run
1078 * @param currentQuota the current quota for the origin.
1079 * @param estimatedSize the estimated size of the database.
1080 * @param totalUsedQuota is the sum of all origins' quota.
1081 * @param quotaUpdater The callback to run when a decision to allow or
1082 * deny quota has been made. Don't forget to call this!
1083 */
1084 @Override
1085 public void onExceededDatabaseQuota(String url,
1086 String databaseIdentifier, long currentQuota, long estimatedSize,
1087 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -07001088 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001089 .onExceededDatabaseQuota(url, databaseIdentifier,
1090 currentQuota, estimatedSize, totalUsedQuota,
1091 quotaUpdater);
1092 }
1093
1094 /**
1095 * The Application Cache has exceeded its max size.
1096 * @param spaceNeeded is the amount of disk space that would be needed
1097 * in order for the last appcache operation to succeed.
1098 * @param totalUsedQuota is the sum of all origins' quota.
1099 * @param quotaUpdater A callback to inform the WebCore thread that a
1100 * new app cache size is available. This callback must always
1101 * be executed at some point to ensure that the sleeping
1102 * WebCore thread is woken up.
1103 */
1104 @Override
1105 public void onReachedMaxAppCacheSize(long spaceNeeded,
1106 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -07001107 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001108 .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota,
1109 quotaUpdater);
1110 }
1111
1112 /**
1113 * Instructs the browser to show a prompt to ask the user to set the
1114 * Geolocation permission state for the specified origin.
1115 * @param origin The origin for which Geolocation permissions are
1116 * requested.
1117 * @param callback The callback to call once the user has set the
1118 * Geolocation permission state.
1119 */
1120 @Override
1121 public void onGeolocationPermissionsShowPrompt(String origin,
1122 GeolocationPermissions.Callback callback) {
1123 if (mInForeground) {
Grace Kloba50c241e2010-04-20 11:07:50 -07001124 getGeolocationPermissionsPrompt().show(origin, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001125 }
1126 }
1127
1128 /**
1129 * Instructs the browser to hide the Geolocation permissions prompt.
1130 */
1131 @Override
1132 public void onGeolocationPermissionsHidePrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001133 if (mInForeground && mGeolocationPermissionsPrompt != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001134 mGeolocationPermissionsPrompt.hide();
1135 }
1136 }
1137
Ben Murdoch65acc352009-11-19 18:16:04 +00001138 /* Adds a JavaScript error message to the system log and if the JS
1139 * console is enabled in the about:debug options, to that console
1140 * also.
Ben Murdochc42addf2010-01-28 15:19:59 +00001141 * @param consoleMessage the message object.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001142 */
1143 @Override
Ben Murdochc42addf2010-01-28 15:19:59 +00001144 public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001145 if (mInForeground) {
1146 // call getErrorConsole(true) so it will create one if needed
1147 ErrorConsoleView errorConsole = getErrorConsole(true);
Ben Murdochc42addf2010-01-28 15:19:59 +00001148 errorConsole.addErrorMessage(consoleMessage);
Michael Kolb8233fac2010-10-26 16:08:53 -07001149 if (mWebViewController.shouldShowErrorConsole()
1150 && errorConsole.getShowState() !=
1151 ErrorConsoleView.SHOW_MAXIMIZED) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001152 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
1153 }
1154 }
Ben Murdochc42addf2010-01-28 15:19:59 +00001155
Jeff Hamilton47654f42010-09-07 09:57:51 -05001156 // Don't log console messages in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001157 if (isPrivateBrowsingEnabled()) return true;
Jeff Hamilton47654f42010-09-07 09:57:51 -05001158
Ben Murdochc42addf2010-01-28 15:19:59 +00001159 String message = "Console: " + consoleMessage.message() + " "
1160 + consoleMessage.sourceId() + ":"
1161 + consoleMessage.lineNumber();
1162
1163 switch (consoleMessage.messageLevel()) {
1164 case TIP:
1165 Log.v(CONSOLE_LOGTAG, message);
1166 break;
1167 case LOG:
1168 Log.i(CONSOLE_LOGTAG, message);
1169 break;
1170 case WARNING:
1171 Log.w(CONSOLE_LOGTAG, message);
1172 break;
1173 case ERROR:
1174 Log.e(CONSOLE_LOGTAG, message);
1175 break;
1176 case DEBUG:
1177 Log.d(CONSOLE_LOGTAG, message);
1178 break;
1179 }
1180
1181 return true;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001182 }
1183
1184 /**
1185 * Ask the browser for an icon to represent a <video> element.
1186 * This icon will be used if the Web page did not specify a poster attribute.
1187 * @return Bitmap The icon or null if no such icon is available.
1188 */
1189 @Override
1190 public Bitmap getDefaultVideoPoster() {
1191 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001192 return mWebViewController.getDefaultVideoPoster();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001193 }
1194 return null;
1195 }
1196
1197 /**
1198 * Ask the host application for a custom progress view to show while
1199 * a <video> is loading.
1200 * @return View The progress view.
1201 */
1202 @Override
1203 public View getVideoLoadingProgressView() {
1204 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001205 return mWebViewController.getVideoLoadingProgressView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001206 }
1207 return null;
1208 }
1209
1210 @Override
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01001211 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001212 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001213 mWebViewController.openFileChooser(uploadMsg, acceptType);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001214 } else {
1215 uploadMsg.onReceiveValue(null);
1216 }
1217 }
1218
1219 /**
1220 * Deliver a list of already-visited URLs
1221 */
1222 @Override
1223 public void getVisitedHistory(final ValueCallback<String[]> callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001224 mWebViewController.getVisitedHistory(callback);
1225 }
Ben Murdoch8029a772010-11-16 11:58:21 +00001226
1227 @Override
1228 public void setupAutoFill(Message message) {
1229 // Prompt the user to set up their profile.
1230 final Message msg = message;
Michael Kolb14612442011-06-24 13:06:29 -07001231 AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
1232 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
Ben Murdoch1d676b62011-01-17 12:54:24 +00001233 Context.LAYOUT_INFLATER_SERVICE);
1234 final View layout = inflater.inflate(R.layout.setup_autofill_dialog, null);
1235
1236 builder.setView(layout)
Ben Murdochb7e6f942011-07-08 13:00:21 +01001237 .setTitle(R.string.autofill_setup_dialog_title)
Ben Murdoch1d676b62011-01-17 12:54:24 +00001238 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1239 @Override
1240 public void onClick(DialogInterface dialog, int id) {
1241 CheckBox disableAutoFill = (CheckBox) layout.findViewById(
1242 R.id.setup_autofill_dialog_disable_autofill);
1243
1244 if (disableAutoFill.isChecked()) {
1245 // Disable autofill and show a toast with how to turn it on again.
John Reck35e9dd62011-04-25 09:01:54 -07001246 mSettings.setAutofillEnabled(false);
Michael Kolb14612442011-06-24 13:06:29 -07001247 Toast.makeText(mContext,
Ben Murdoch1d676b62011-01-17 12:54:24 +00001248 R.string.autofill_setup_dialog_negative_toast,
1249 Toast.LENGTH_LONG).show();
1250 } else {
1251 // Take user to the AutoFill profile editor. When they return,
1252 // we will send the message that we pass here which will trigger
1253 // the form to get filled out with their new profile.
1254 mWebViewController.setupAutoFill(msg);
1255 }
1256 }
1257 })
1258 .setNegativeButton(R.string.cancel, null)
1259 .show();
Ben Murdoch8029a772010-11-16 11:58:21 +00001260 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001261 };
1262
1263 // -------------------------------------------------------------------------
1264 // WebViewClient implementation for the sub window
1265 // -------------------------------------------------------------------------
1266
1267 // Subclass of WebViewClient used in subwindows to notify the main
1268 // WebViewClient of certain WebView activities.
1269 private static class SubWindowClient extends WebViewClient {
1270 // The main WebViewClient.
1271 private final WebViewClient mClient;
Michael Kolb8233fac2010-10-26 16:08:53 -07001272 private final WebViewController mController;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001273
Michael Kolb8233fac2010-10-26 16:08:53 -07001274 SubWindowClient(WebViewClient client, WebViewController controller) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001275 mClient = client;
Michael Kolb8233fac2010-10-26 16:08:53 -07001276 mController = controller;
Leon Scroggins III211ba542010-04-19 13:21:13 -04001277 }
1278 @Override
1279 public void onPageStarted(WebView view, String url, Bitmap favicon) {
1280 // Unlike the others, do not call mClient's version, which would
1281 // change the progress bar. However, we do want to remove the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001282 // find or select dialog.
Michael Kolb8233fac2010-10-26 16:08:53 -07001283 mController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001284 }
1285 @Override
1286 public void doUpdateVisitedHistory(WebView view, String url,
1287 boolean isReload) {
1288 mClient.doUpdateVisitedHistory(view, url, isReload);
1289 }
1290 @Override
1291 public boolean shouldOverrideUrlLoading(WebView view, String url) {
1292 return mClient.shouldOverrideUrlLoading(view, url);
1293 }
1294 @Override
1295 public void onReceivedSslError(WebView view, SslErrorHandler handler,
1296 SslError error) {
1297 mClient.onReceivedSslError(view, handler, error);
1298 }
1299 @Override
Brian Carlstrom8862c1d2011-06-02 01:05:55 -07001300 public void onReceivedClientCertRequest(WebView view,
1301 ClientCertRequestHandler handler, String host_and_port) {
1302 mClient.onReceivedClientCertRequest(view, handler, host_and_port);
1303 }
1304 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -07001305 public void onReceivedHttpAuthRequest(WebView view,
1306 HttpAuthHandler handler, String host, String realm) {
1307 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
1308 }
1309 @Override
1310 public void onFormResubmission(WebView view, Message dontResend,
1311 Message resend) {
1312 mClient.onFormResubmission(view, dontResend, resend);
1313 }
1314 @Override
1315 public void onReceivedError(WebView view, int errorCode,
1316 String description, String failingUrl) {
1317 mClient.onReceivedError(view, errorCode, description, failingUrl);
1318 }
1319 @Override
1320 public boolean shouldOverrideKeyEvent(WebView view,
1321 android.view.KeyEvent event) {
1322 return mClient.shouldOverrideKeyEvent(view, event);
1323 }
1324 @Override
1325 public void onUnhandledKeyEvent(WebView view,
1326 android.view.KeyEvent event) {
1327 mClient.onUnhandledKeyEvent(view, event);
1328 }
1329 }
1330
1331 // -------------------------------------------------------------------------
1332 // WebChromeClient implementation for the sub window
1333 // -------------------------------------------------------------------------
1334
1335 private class SubWindowChromeClient extends WebChromeClient {
1336 // The main WebChromeClient.
1337 private final WebChromeClient mClient;
1338
1339 SubWindowChromeClient(WebChromeClient client) {
1340 mClient = client;
1341 }
1342 @Override
1343 public void onProgressChanged(WebView view, int newProgress) {
1344 mClient.onProgressChanged(view, newProgress);
1345 }
1346 @Override
1347 public boolean onCreateWindow(WebView view, boolean dialog,
1348 boolean userGesture, android.os.Message resultMsg) {
1349 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
1350 }
1351 @Override
1352 public void onCloseWindow(WebView window) {
1353 if (window != mSubView) {
1354 Log.e(LOGTAG, "Can't close the window");
1355 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001356 mWebViewController.dismissSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001357 }
1358 }
1359
1360 // -------------------------------------------------------------------------
1361
1362 // Construct a new tab
Michael Kolb7bcafde2011-05-09 13:55:59 -07001363 Tab(WebViewController wvcontroller, WebView w) {
John Reck1cf4b792011-07-26 10:22:22 -07001364 this(wvcontroller, w, null);
1365 }
1366
1367 Tab(WebViewController wvcontroller, Bundle state) {
1368 this(wvcontroller, null, state);
1369 }
1370
1371 Tab(WebViewController wvcontroller, WebView w, Bundle state) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001372 mWebViewController = wvcontroller;
Michael Kolb14612442011-06-24 13:06:29 -07001373 mContext = mWebViewController.getContext();
John Reck35e9dd62011-04-25 09:01:54 -07001374 mSettings = BrowserSettings.getInstance();
Michael Kolb14612442011-06-24 13:06:29 -07001375 mDataController = DataController.getInstance(mContext);
1376 mCurrentState = new PageState(mContext, w != null
John Reck847b5322011-04-14 17:02:18 -07001377 ? w.isPrivateBrowsingEnabled() : false);
Michael Kolb8233fac2010-10-26 16:08:53 -07001378 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001379 mInForeground = false;
1380
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001381 mDownloadListener = new DownloadListener() {
1382 public void onDownloadStart(String url, String userAgent,
1383 String contentDisposition, String mimetype,
1384 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001385 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001386 mimetype, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001387 }
1388 };
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001389 mWebBackForwardListClient = new WebBackForwardListClient() {
1390 @Override
1391 public void onNewHistoryItem(WebHistoryItem item) {
1392 if (isInVoiceSearchMode()) {
1393 item.setCustomData(mVoiceSearchData.mVoiceSearchIntent);
1394 }
Mathew Inwooda829d552011-09-02 14:16:25 +01001395 if (mClearHistoryUrlPattern != null) {
1396 boolean match =
1397 mClearHistoryUrlPattern.matcher(item.getOriginalUrl()).matches();
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001398 if (LOGD_ENABLED) {
Mathew Inwooda829d552011-09-02 14:16:25 +01001399 Log.d(LOGTAG, "onNewHistoryItem: match=" + match + "\n\t"
1400 + item.getUrl() + "\n\t"
1401 + mClearHistoryUrlPattern);
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001402 }
Mathew Inwooda829d552011-09-02 14:16:25 +01001403 if (match) {
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001404 if (mMainView != null) {
1405 mMainView.clearHistory();
1406 }
1407 }
Mathew Inwooda829d552011-09-02 14:16:25 +01001408 mClearHistoryUrlPattern = null;
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001409 }
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001410 }
1411 @Override
1412 public void onIndexChanged(WebHistoryItem item, int index) {
1413 Object data = item.getCustomData();
1414 if (data != null && data instanceof Intent) {
1415 activateVoiceSearchMode((Intent) data);
1416 }
1417 }
1418 };
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001419
John Reck1cf4b792011-07-26 10:22:22 -07001420 mCaptureWidth = mContext.getResources().getDimensionPixelSize(
1421 R.dimen.tab_thumbnail_width);
1422 mCaptureHeight = mContext.getResources().getDimensionPixelSize(
1423 R.dimen.tab_thumbnail_height);
1424 updateShouldCaptureThumbnails();
1425 restoreState(state);
John Reck52be4782011-08-26 15:37:29 -07001426 if (getId() == -1) {
1427 mId = TabControl.getNextId();
1428 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001429 setWebView(w);
Michael Kolb9ef259a2011-07-12 15:33:08 -07001430 mHandler = new Handler() {
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001431 @Override
Michael Kolb9ef259a2011-07-12 15:33:08 -07001432 public void handleMessage(Message m) {
John Reck1cf4b792011-07-26 10:22:22 -07001433 switch (m.what) {
1434 case MSG_CAPTURE:
1435 capture();
1436 break;
1437 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001438 }
1439 };
John Reck1cf4b792011-07-26 10:22:22 -07001440 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001441
Mathew Inwoode09305e2011-09-02 12:03:26 +01001442 /**
1443 * This is used to get a new ID when the tab has been preloaded, before it is displayed and
1444 * added to TabControl. Preloaded tabs can be created before restoreInstanceState, leading
1445 * to overlapping IDs between the preloaded and restored tabs.
1446 */
1447 public void refreshIdAfterPreload() {
1448 mId = TabControl.getNextId();
1449 }
1450
John Reck1cf4b792011-07-26 10:22:22 -07001451 public void updateShouldCaptureThumbnails() {
1452 if (mWebViewController.shouldCaptureThumbnails()) {
1453 synchronized (Tab.this) {
1454 if (mCapture == null) {
1455 mCapture = Bitmap.createBitmap(mCaptureWidth, mCaptureHeight,
1456 Bitmap.Config.RGB_565);
Michael Kolbc3af0672011-08-09 10:24:41 -07001457 mCapture.eraseColor(Color.WHITE);
John Reck1cf4b792011-07-26 10:22:22 -07001458 if (mInForeground) {
1459 postCapture();
1460 }
1461 }
1462 }
1463 } else {
1464 synchronized (Tab.this) {
1465 mCapture = null;
1466 deleteThumbnail();
1467 }
1468 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001469 }
1470
Michael Kolb14612442011-06-24 13:06:29 -07001471 public void setController(WebViewController ctl) {
1472 mWebViewController = ctl;
John Reck1cf4b792011-07-26 10:22:22 -07001473 updateShouldCaptureThumbnails();
Michael Kolb14612442011-06-24 13:06:29 -07001474 }
1475
Michael Kolbc831b632011-05-11 09:30:34 -07001476 public long getId() {
1477 return mId;
1478 }
1479
Grace Kloba22ac16e2009-10-07 18:00:23 -07001480 /**
1481 * Sets the WebView for this tab, correctly removing the old WebView from
1482 * the container view.
1483 */
1484 void setWebView(WebView w) {
1485 if (mMainView == w) {
1486 return;
1487 }
Michael Kolba713ec82010-11-29 17:27:06 -08001488
Grace Kloba22ac16e2009-10-07 18:00:23 -07001489 // If the WebView is changing, the page will be reloaded, so any ongoing
1490 // Geolocation permission requests are void.
Grace Kloba50c241e2010-04-20 11:07:50 -07001491 if (mGeolocationPermissionsPrompt != null) {
1492 mGeolocationPermissionsPrompt.hide();
1493 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001494
Michael Kolba713ec82010-11-29 17:27:06 -08001495 mWebViewController.onSetWebView(this, w);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001496
John Reck1cf4b792011-07-26 10:22:22 -07001497 if (mMainView != null) {
John Reck8ee633f2011-08-09 16:00:35 -07001498 mMainView.setPictureListener(null);
John Reck1cf4b792011-07-26 10:22:22 -07001499 if (w != null) {
1500 syncCurrentState(w, null);
1501 } else {
1502 mCurrentState = new PageState(mContext, false);
1503 }
1504 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001505 // set the new one
1506 mMainView = w;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001507 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001508 if (mMainView != null) {
1509 mMainView.setWebViewClient(mWebViewClient);
1510 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001511 // Attach DownloadManager so that downloads can start in an active
1512 // or a non-active window. This can happen when going to a site that
1513 // does a redirect after a period of time. The user could have
1514 // switched to another tab while waiting for the download to start.
1515 mMainView.setDownloadListener(mDownloadListener);
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001516 mMainView.setWebBackForwardListClient(mWebBackForwardListClient);
John Reck8ee633f2011-08-09 16:00:35 -07001517 TabControl tc = mWebViewController.getTabControl();
1518 if (tc != null && tc.getOnThumbnailUpdatedListener() != null) {
1519 mMainView.setPictureListener(this);
1520 }
John Reck1cf4b792011-07-26 10:22:22 -07001521 if (mSavedState != null) {
John Reck6c2e2f32011-08-22 13:41:23 -07001522 WebBackForwardList restoredState
1523 = mMainView.restoreState(mSavedState);
1524 if (restoredState == null || restoredState.getSize() == 0) {
1525 Log.w(LOGTAG, "Failed to restore WebView state!");
1526 loadUrl(mCurrentState.mOriginalUrl, null);
1527 }
John Reck1cf4b792011-07-26 10:22:22 -07001528 mSavedState = null;
1529 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001530 }
1531 }
1532
1533 /**
1534 * Destroy the tab's main WebView and subWindow if any
1535 */
1536 void destroy() {
1537 if (mMainView != null) {
1538 dismissSubWindow();
John Reckef654f12011-07-12 16:42:08 -07001539 // Make sure the embedded title bar isn't still attached
1540 mMainView.setEmbeddedTitleBar(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001541 // save the WebView to call destroy() after detach it from the tab
1542 WebView webView = mMainView;
1543 setWebView(null);
1544 webView.destroy();
1545 }
1546 }
1547
1548 /**
1549 * Remove the tab from the parent
1550 */
1551 void removeFromTree() {
1552 // detach the children
Michael Kolbc831b632011-05-11 09:30:34 -07001553 if (mChildren != null) {
1554 for(Tab t : mChildren) {
1555 t.setParent(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001556 }
1557 }
1558 // remove itself from the parent list
Michael Kolbc831b632011-05-11 09:30:34 -07001559 if (mParent != null) {
1560 mParent.mChildren.remove(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001561 }
John Reck1cf4b792011-07-26 10:22:22 -07001562 deleteThumbnail();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001563 }
1564
1565 /**
1566 * Create a new subwindow unless a subwindow already exists.
1567 * @return True if a new subwindow was created. False if one already exists.
1568 */
1569 boolean createSubWindow() {
1570 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001571 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001572 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001573 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001574 mSubView.setWebChromeClient(new SubWindowChromeClient(
1575 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001576 // Set a different DownloadListener for the mSubView, since it will
1577 // just need to dismiss the mSubView, rather than close the Tab
1578 mSubView.setDownloadListener(new DownloadListener() {
1579 public void onDownloadStart(String url, String userAgent,
1580 String contentDisposition, String mimetype,
1581 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001582 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001583 contentDisposition, mimetype, contentLength);
1584 if (mSubView.copyBackForwardList().getSize() == 0) {
1585 // This subwindow was opened for the sole purpose of
1586 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001587 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001588 }
1589 }
1590 });
Michael Kolb14612442011-06-24 13:06:29 -07001591 mSubView.setOnCreateContextMenuListener(mWebViewController.getActivity());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001592 return true;
1593 }
1594 return false;
1595 }
1596
1597 /**
1598 * Dismiss the subWindow for the tab.
1599 */
1600 void dismissSubWindow() {
1601 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001602 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001603 mSubView.destroy();
1604 mSubView = null;
1605 mSubViewContainer = null;
1606 }
1607 }
1608
Grace Kloba22ac16e2009-10-07 18:00:23 -07001609
1610 /**
1611 * Set the parent tab of this tab.
1612 */
Michael Kolbc831b632011-05-11 09:30:34 -07001613 void setParent(Tab parent) {
John Reck52be4782011-08-26 15:37:29 -07001614 if (parent == this) {
1615 throw new IllegalStateException("Cannot set parent to self!");
1616 }
Michael Kolbc831b632011-05-11 09:30:34 -07001617 mParent = parent;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001618 // This tab may have been freed due to low memory. If that is the case,
Michael Kolbc831b632011-05-11 09:30:34 -07001619 // the parent tab id is already saved. If we are changing that id
Grace Kloba22ac16e2009-10-07 18:00:23 -07001620 // (most likely due to removing the parent tab) we must update the
Michael Kolbc831b632011-05-11 09:30:34 -07001621 // parent tab id in the saved Bundle.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001622 if (mSavedState != null) {
1623 if (parent == null) {
1624 mSavedState.remove(PARENTTAB);
1625 } else {
Michael Kolbc831b632011-05-11 09:30:34 -07001626 mSavedState.putLong(PARENTTAB, parent.getId());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001627 }
1628 }
John Reckb0a86db2011-05-24 14:05:58 -07001629
1630 // Sync the WebView useragent with the parent
1631 if (parent != null && mSettings.hasDesktopUseragent(parent.getWebView())
1632 != mSettings.hasDesktopUseragent(getWebView())) {
1633 mSettings.toggleDesktopUseragent(getWebView());
1634 }
John Reck52be4782011-08-26 15:37:29 -07001635
1636 if (parent != null && parent.getId() == getId()) {
1637 throw new IllegalStateException("Parent has same ID as child!");
1638 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001639 }
1640
1641 /**
Michael Kolbc831b632011-05-11 09:30:34 -07001642 * If this Tab was created through another Tab, then this method returns
1643 * that Tab.
1644 * @return the Tab parent or null
1645 */
1646 public Tab getParent() {
1647 return mParent;
1648 }
1649
1650 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001651 * When a Tab is created through the content of another Tab, then we
1652 * associate the Tabs.
1653 * @param child the Tab that was created from this Tab
1654 */
1655 void addChildTab(Tab child) {
Michael Kolbc831b632011-05-11 09:30:34 -07001656 if (mChildren == null) {
1657 mChildren = new Vector<Tab>();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001658 }
Michael Kolbc831b632011-05-11 09:30:34 -07001659 mChildren.add(child);
1660 child.setParent(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001661 }
1662
Michael Kolbc831b632011-05-11 09:30:34 -07001663 Vector<Tab> getChildren() {
1664 return mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001665 }
1666
1667 void resume() {
1668 if (mMainView != null) {
John Reck56c1fcf2011-08-17 10:15:16 -07001669 setupHwAcceleration(mMainView);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001670 mMainView.onResume();
1671 if (mSubView != null) {
1672 mSubView.onResume();
1673 }
1674 }
1675 }
1676
John Reck56c1fcf2011-08-17 10:15:16 -07001677 private void setupHwAcceleration(View web) {
1678 if (web == null) return;
1679 BrowserSettings settings = BrowserSettings.getInstance();
1680 if (settings.isHardwareAccelerated()) {
1681 web.setLayerType(View.LAYER_TYPE_NONE, null);
1682 } else {
1683 web.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
1684 }
1685 }
1686
Grace Kloba22ac16e2009-10-07 18:00:23 -07001687 void pause() {
1688 if (mMainView != null) {
1689 mMainView.onPause();
1690 if (mSubView != null) {
1691 mSubView.onPause();
1692 }
1693 }
1694 }
1695
1696 void putInForeground() {
John Reck8ee633f2011-08-09 16:00:35 -07001697 if (mInForeground) {
1698 return;
1699 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001700 mInForeground = true;
1701 resume();
Michael Kolb14612442011-06-24 13:06:29 -07001702 Activity activity = mWebViewController.getActivity();
1703 mMainView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001704 if (mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -07001705 mSubView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001706 }
1707 // Show the pending error dialog if the queue is not empty
1708 if (mQueuedErrors != null && mQueuedErrors.size() > 0) {
1709 showError(mQueuedErrors.getFirst());
1710 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001711 mWebViewController.bookmarkedStatusHasChanged(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001712 }
1713
1714 void putInBackground() {
John Reck8ee633f2011-08-09 16:00:35 -07001715 if (!mInForeground) {
1716 return;
1717 }
John Reck52be4782011-08-26 15:37:29 -07001718 capture();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001719 mInForeground = false;
1720 pause();
1721 mMainView.setOnCreateContextMenuListener(null);
1722 if (mSubView != null) {
1723 mSubView.setOnCreateContextMenuListener(null);
1724 }
1725 }
1726
Michael Kolb8233fac2010-10-26 16:08:53 -07001727 boolean inForeground() {
1728 return mInForeground;
1729 }
1730
Grace Kloba22ac16e2009-10-07 18:00:23 -07001731 /**
1732 * Return the top window of this tab; either the subwindow if it is not
1733 * null or the main window.
1734 * @return The top window of this tab.
1735 */
1736 WebView getTopWindow() {
1737 if (mSubView != null) {
1738 return mSubView;
1739 }
1740 return mMainView;
1741 }
1742
1743 /**
1744 * Return the main window of this tab. Note: if a tab is freed in the
1745 * background, this can return null. It is only guaranteed to be
1746 * non-null for the current tab.
1747 * @return The main WebView of this tab.
1748 */
1749 WebView getWebView() {
1750 return mMainView;
1751 }
1752
Michael Kolba713ec82010-11-29 17:27:06 -08001753 void setViewContainer(View container) {
1754 mContainer = container;
1755 }
1756
Michael Kolb8233fac2010-10-26 16:08:53 -07001757 View getViewContainer() {
1758 return mContainer;
1759 }
1760
Grace Kloba22ac16e2009-10-07 18:00:23 -07001761 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001762 * Return whether private browsing is enabled for the main window of
1763 * this tab.
1764 * @return True if private browsing is enabled.
1765 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001766 boolean isPrivateBrowsingEnabled() {
John Reck502a3532011-08-16 14:21:46 -07001767 return mCurrentState.mIncognito;
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001768 }
1769
1770 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001771 * Return the subwindow of this tab or null if there is no subwindow.
1772 * @return The subwindow of this tab or null.
1773 */
1774 WebView getSubWebView() {
1775 return mSubView;
1776 }
1777
Michael Kolb1514bb72010-11-22 09:11:48 -08001778 void setSubWebView(WebView subView) {
1779 mSubView = subView;
1780 }
1781
Michael Kolb8233fac2010-10-26 16:08:53 -07001782 View getSubViewContainer() {
1783 return mSubViewContainer;
1784 }
1785
Michael Kolb1514bb72010-11-22 09:11:48 -08001786 void setSubViewContainer(View subViewContainer) {
1787 mSubViewContainer = subViewContainer;
1788 }
1789
Grace Kloba22ac16e2009-10-07 18:00:23 -07001790 /**
1791 * @return The geolocation permissions prompt for this tab.
1792 */
1793 GeolocationPermissionsPrompt getGeolocationPermissionsPrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001794 if (mGeolocationPermissionsPrompt == null) {
1795 ViewStub stub = (ViewStub) mContainer
1796 .findViewById(R.id.geolocation_permissions_prompt);
1797 mGeolocationPermissionsPrompt = (GeolocationPermissionsPrompt) stub
1798 .inflate();
Grace Kloba50c241e2010-04-20 11:07:50 -07001799 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001800 return mGeolocationPermissionsPrompt;
1801 }
1802
1803 /**
1804 * @return The application id string
1805 */
1806 String getAppId() {
1807 return mAppId;
1808 }
1809
1810 /**
1811 * Set the application id string
1812 * @param id
1813 */
1814 void setAppId(String id) {
1815 mAppId = id;
1816 }
1817
Michael Kolbe28b3472011-08-04 16:54:31 -07001818 boolean closeOnBack() {
1819 return mCloseOnBack;
1820 }
1821
1822 void setCloseOnBack(boolean close) {
1823 mCloseOnBack = close;
1824 }
1825
Grace Kloba22ac16e2009-10-07 18:00:23 -07001826 String getUrl() {
John Reck324d4402011-01-11 16:56:42 -08001827 return UrlUtils.filteredUrl(mCurrentState.mUrl);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001828 }
1829
John Reck49a603c2011-03-03 09:33:05 -08001830 String getOriginalUrl() {
John Reckdb22ec42011-06-29 11:31:24 -07001831 if (mCurrentState.mOriginalUrl == null) {
1832 return getUrl();
John Reck49a603c2011-03-03 09:33:05 -08001833 }
John Reckdb22ec42011-06-29 11:31:24 -07001834 return UrlUtils.filteredUrl(mCurrentState.mOriginalUrl);
John Reck49a603c2011-03-03 09:33:05 -08001835 }
1836
Grace Kloba22ac16e2009-10-07 18:00:23 -07001837 /**
John Reck30c714c2010-12-16 17:30:34 -08001838 * Get the title of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001839 */
1840 String getTitle() {
John Reck30c714c2010-12-16 17:30:34 -08001841 if (mCurrentState.mTitle == null && mInPageLoad) {
Michael Kolb14612442011-06-24 13:06:29 -07001842 return mContext.getString(R.string.title_bar_loading);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001843 }
John Reck30c714c2010-12-16 17:30:34 -08001844 return mCurrentState.mTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001845 }
1846
1847 /**
John Reck30c714c2010-12-16 17:30:34 -08001848 * Get the favicon of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001849 */
1850 Bitmap getFavicon() {
John Reck1cf4b792011-07-26 10:22:22 -07001851 if (mCurrentState.mFavicon != null) {
1852 return mCurrentState.mFavicon;
1853 }
1854 return getDefaultFavicon(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001855 }
1856
John Recke969cc52010-12-21 17:24:43 -08001857 public boolean isBookmarkedSite() {
1858 return mCurrentState.mIsBookmarkedSite;
1859 }
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001860
Grace Kloba22ac16e2009-10-07 18:00:23 -07001861 /**
1862 * Return the tab's error console. Creates the console if createIfNEcessary
1863 * is true and we haven't already created the console.
1864 * @param createIfNecessary Flag to indicate if the console should be
1865 * created if it has not been already.
1866 * @return The tab's error console, or null if one has not been created and
1867 * createIfNecessary is false.
1868 */
1869 ErrorConsoleView getErrorConsole(boolean createIfNecessary) {
1870 if (createIfNecessary && mErrorConsole == null) {
Michael Kolb14612442011-06-24 13:06:29 -07001871 mErrorConsole = new ErrorConsoleView(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001872 mErrorConsole.setWebView(mMainView);
1873 }
1874 return mErrorConsole;
1875 }
1876
John Reck30c714c2010-12-16 17:30:34 -08001877 private void setLockIconType(LockIcon icon) {
1878 mCurrentState.mLockIcon = icon;
1879 mWebViewController.onUpdatedLockIcon(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001880 }
1881
1882 /**
1883 * @return The tab's lock icon type.
1884 */
John Reck30c714c2010-12-16 17:30:34 -08001885 LockIcon getLockIconType() {
1886 return mCurrentState.mLockIcon;
1887 }
1888
1889 int getLoadProgress() {
1890 if (mInPageLoad) {
1891 return mPageLoadProgress;
1892 }
1893 return 100;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001894 }
1895
1896 /**
1897 * @return TRUE if onPageStarted is called while onPageFinished is not
1898 * called yet.
1899 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001900 boolean inPageLoad() {
1901 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001902 }
1903
1904 // force mInLoad to be false. This should only be called before closing the
1905 // tab to ensure BrowserActivity's pauseWebViewTimers() is called correctly.
Michael Kolb8233fac2010-10-26 16:08:53 -07001906 void clearInPageLoad() {
1907 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001908 }
1909
Grace Kloba22ac16e2009-10-07 18:00:23 -07001910 /**
John Reck1cf4b792011-07-26 10:22:22 -07001911 * @return The Bundle with the tab's state if it can be saved, otherwise null
Grace Kloba22ac16e2009-10-07 18:00:23 -07001912 */
John Reck1cf4b792011-07-26 10:22:22 -07001913 public Bundle saveState() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001914 // If the WebView is null it means we ran low on memory and we already
1915 // stored the saved state in mSavedState.
1916 if (mMainView == null) {
John Reck1cf4b792011-07-26 10:22:22 -07001917 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001918 }
John Reck6c2e2f32011-08-22 13:41:23 -07001919
1920 if (TextUtils.isEmpty(mCurrentState.mUrl)) {
John Reck1cf4b792011-07-26 10:22:22 -07001921 return null;
John Reck24f18262011-06-17 14:47:20 -07001922 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001923
1924 mSavedState = new Bundle();
John Reck6c2e2f32011-08-22 13:41:23 -07001925 WebBackForwardList savedList = mMainView.saveState(mSavedState);
1926 if (savedList == null || savedList.getSize() == 0) {
1927 Log.w(LOGTAG, "Failed to save back/forward list for "
1928 + mCurrentState.mUrl);
1929 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001930
Michael Kolbc831b632011-05-11 09:30:34 -07001931 mSavedState.putLong(ID, mId);
John Reck30c714c2010-12-16 17:30:34 -08001932 mSavedState.putString(CURRURL, mCurrentState.mUrl);
1933 mSavedState.putString(CURRTITLE, mCurrentState.mTitle);
John Reck1cf4b792011-07-26 10:22:22 -07001934 mSavedState.putBoolean(INCOGNITO, mMainView.isPrivateBrowsingEnabled());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001935 if (mAppId != null) {
1936 mSavedState.putString(APPID, mAppId);
1937 }
Michael Kolbe28b3472011-08-04 16:54:31 -07001938 mSavedState.putBoolean(CLOSEFLAG, mCloseOnBack);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001939 // Remember the parent tab so the relationship can be restored.
Michael Kolbc831b632011-05-11 09:30:34 -07001940 if (mParent != null) {
1941 mSavedState.putLong(PARENTTAB, mParent.mId);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001942 }
John Reckb0a86db2011-05-24 14:05:58 -07001943 mSavedState.putBoolean(USERAGENT,
1944 mSettings.hasDesktopUseragent(getWebView()));
John Reck1cf4b792011-07-26 10:22:22 -07001945 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001946 }
1947
1948 /*
1949 * Restore the state of the tab.
1950 */
John Reck1cf4b792011-07-26 10:22:22 -07001951 private void restoreState(Bundle b) {
1952 mSavedState = b;
1953 if (mSavedState == null) {
1954 return;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001955 }
1956 // Restore the internal state even if the WebView fails to restore.
1957 // This will maintain the app id, original url and close-on-exit values.
Michael Kolbc831b632011-05-11 09:30:34 -07001958 mId = b.getLong(ID);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001959 mAppId = b.getString(APPID);
Michael Kolbe28b3472011-08-04 16:54:31 -07001960 mCloseOnBack = b.getBoolean(CLOSEFLAG);
John Reckb0a86db2011-05-24 14:05:58 -07001961 if (b.getBoolean(USERAGENT)
1962 != mSettings.hasDesktopUseragent(getWebView())) {
1963 mSettings.toggleDesktopUseragent(getWebView());
1964 }
John Reck1cf4b792011-07-26 10:22:22 -07001965 String url = b.getString(CURRURL);
1966 String title = b.getString(CURRTITLE);
1967 boolean incognito = b.getBoolean(INCOGNITO);
1968 mCurrentState = new PageState(mContext, incognito, url, null);
1969 mCurrentState.mTitle = title;
1970 synchronized (Tab.this) {
1971 if (mCapture != null) {
1972 BackgroundHandler.execute(mLoadThumbnail);
1973 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001974 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001975 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04001976
Leon Scroggins1961ed22010-12-07 15:22:21 -05001977 public void updateBookmarkedStatus() {
John Recke969cc52010-12-21 17:24:43 -08001978 mDataController.queryBookmarkStatus(getUrl(), mIsBookmarkCallback);
Leon Scroggins1961ed22010-12-07 15:22:21 -05001979 }
1980
John Recke969cc52010-12-21 17:24:43 -08001981 private DataController.OnQueryUrlIsBookmark mIsBookmarkCallback
1982 = new DataController.OnQueryUrlIsBookmark() {
1983 @Override
1984 public void onQueryUrlIsBookmark(String url, boolean isBookmark) {
1985 if (mCurrentState.mUrl.equals(url)) {
1986 mCurrentState.mIsBookmarkedSite = isBookmark;
1987 mWebViewController.bookmarkedStatusHasChanged(Tab.this);
1988 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001989 }
John Recke969cc52010-12-21 17:24:43 -08001990 };
Michael Kolb1acef692011-03-08 14:12:06 -08001991
Michael Kolbeb95db42011-03-03 10:38:40 -08001992 public Bitmap getScreenshot() {
John Reck1cf4b792011-07-26 10:22:22 -07001993 synchronized (Tab.this) {
1994 return mCapture;
1995 }
Michael Kolbeb95db42011-03-03 10:38:40 -08001996 }
1997
John Reck541f55a2011-06-07 16:34:43 -07001998 public boolean isSnapshot() {
John Reck541f55a2011-06-07 16:34:43 -07001999 return false;
2000 }
2001
John Reckd8c74522011-06-14 08:45:00 -07002002 public ContentValues createSnapshotValues() {
2003 if (mMainView == null) return null;
John Reck8cc92352011-07-06 17:41:52 -07002004 ByteArrayOutputStream bos = new ByteArrayOutputStream();
2005 try {
2006 GZIPOutputStream stream = new GZIPOutputStream(bos);
2007 if (!mMainView.saveViewState(stream)) {
2008 return null;
2009 }
2010 stream.flush();
2011 stream.close();
2012 } catch (Exception e) {
2013 Log.w(LOGTAG, "Failed to save view state", e);
John Reck541f55a2011-06-07 16:34:43 -07002014 return null;
2015 }
John Reck8cc92352011-07-06 17:41:52 -07002016 byte[] data = bos.toByteArray();
John Reckd8c74522011-06-14 08:45:00 -07002017 ContentValues values = new ContentValues();
2018 values.put(Snapshots.TITLE, mCurrentState.mTitle);
2019 values.put(Snapshots.URL, mCurrentState.mUrl);
2020 values.put(Snapshots.VIEWSTATE, data);
2021 values.put(Snapshots.BACKGROUND, mMainView.getPageBackgroundColor());
John Reck8cc92352011-07-06 17:41:52 -07002022 values.put(Snapshots.DATE_CREATED, System.currentTimeMillis());
2023 values.put(Snapshots.FAVICON, compressBitmap(getFavicon()));
2024 Bitmap screenshot = Controller.createScreenshot(mMainView,
2025 Controller.getDesiredThumbnailWidth(mContext),
2026 Controller.getDesiredThumbnailHeight(mContext));
2027 values.put(Snapshots.THUMBNAIL, compressBitmap(screenshot));
John Reckd8c74522011-06-14 08:45:00 -07002028 return values;
John Reck541f55a2011-06-07 16:34:43 -07002029 }
2030
John Reck8cc92352011-07-06 17:41:52 -07002031 public byte[] compressBitmap(Bitmap bitmap) {
2032 if (bitmap == null) {
2033 return null;
2034 }
2035 ByteArrayOutputStream stream = new ByteArrayOutputStream();
2036 bitmap.compress(CompressFormat.PNG, 100, stream);
2037 return stream.toByteArray();
2038 }
2039
John Reck26b18322011-06-21 13:08:58 -07002040 public void loadUrl(String url, Map<String, String> headers) {
2041 if (mMainView != null) {
Michael Kolb14612442011-06-24 13:06:29 -07002042 mCurrentState = new PageState(mContext, false, url, null);
John Reck26b18322011-06-21 13:08:58 -07002043 mWebViewController.onPageStarted(this, mMainView, null);
2044 mMainView.loadUrl(url, headers);
2045 }
2046 }
2047
Michael Kolb9ef259a2011-07-12 15:33:08 -07002048 protected void capture() {
2049 if (mMainView == null || mCapture == null) return;
2050 Canvas c = new Canvas(mCapture);
2051 final int left = mMainView.getScrollX();
2052 final int top = mMainView.getScrollY() + mMainView.getVisibleTitleHeight();
2053 c.translate(-left, -top);
2054 float scale = mCaptureWidth / (float) mMainView.getWidth();
2055 c.scale(scale, scale, left, top);
John Reck8ee633f2011-08-09 16:00:35 -07002056 if (mMainView instanceof BrowserWebView) {
2057 ((BrowserWebView)mMainView).drawContent(c);
2058 } else {
2059 mMainView.draw(c);
2060 }
Dianne Hackborn43cfe8a2011-08-02 16:59:35 -07002061 c.setBitmap(null);
John Reck8ee633f2011-08-09 16:00:35 -07002062 mHandler.removeMessages(MSG_CAPTURE);
John Reck1cf4b792011-07-26 10:22:22 -07002063 persistThumbnail();
John Reck8ee633f2011-08-09 16:00:35 -07002064 TabControl tc = mWebViewController.getTabControl();
2065 if (tc != null) {
2066 OnThumbnailUpdatedListener updateListener
2067 = tc.getOnThumbnailUpdatedListener();
2068 if (updateListener != null) {
2069 updateListener.onThumbnailUpdated(this);
2070 }
2071 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07002072 }
2073
2074 @Override
2075 public void onNewPicture(WebView view, Picture picture) {
2076 //update screenshot
John Reck1cf4b792011-07-26 10:22:22 -07002077 postCapture();
2078 }
2079
2080 private void postCapture() {
Michael Kolb9ef259a2011-07-12 15:33:08 -07002081 if (!mHandler.hasMessages(MSG_CAPTURE)) {
2082 mHandler.sendEmptyMessageDelayed(MSG_CAPTURE, CAPTURE_DELAY);
2083 }
2084 }
2085
John Reckef654f12011-07-12 16:42:08 -07002086 public boolean canGoBack() {
2087 return mMainView != null ? mMainView.canGoBack() : false;
2088 }
2089
2090 public boolean canGoForward() {
2091 return mMainView != null ? mMainView.canGoForward() : false;
2092 }
2093
2094 public void goBack() {
2095 if (mMainView != null) {
2096 mMainView.goBack();
2097 }
2098 }
2099
2100 public void goForward() {
2101 if (mMainView != null) {
2102 mMainView.goForward();
2103 }
2104 }
2105
Mathew Inwood1dd8e822011-08-03 14:34:29 +01002106 /**
2107 * Causes the tab back/forward stack to be cleared once, if the given URL is the next URL
2108 * to be added to the stack.
2109 *
2110 * This is used to ensure that preloaded URLs that are not subsequently seen by the user do
2111 * not appear in the back stack.
2112 */
Mathew Inwooda829d552011-09-02 14:16:25 +01002113 public void clearBackStackWhenItemAdded(Pattern urlPattern) {
2114 mClearHistoryUrlPattern = urlPattern;
Mathew Inwood1dd8e822011-08-03 14:34:29 +01002115 }
2116
John Reck1cf4b792011-07-26 10:22:22 -07002117 protected void persistThumbnail() {
2118 BackgroundHandler.execute(mSaveThumbnail);
2119 }
2120
2121 protected void deleteThumbnail() {
2122 BackgroundHandler.execute(mDeleteThumbnail);
2123 }
2124
2125 private void updateCaptureFromBlob(byte[] blob) {
2126 synchronized (Tab.this) {
2127 if (mCapture == null) {
2128 return;
2129 }
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002130 ByteBuffer buffer = ByteBuffer.wrap(blob);
Michael Kolbd837a112011-08-09 14:00:18 -07002131 try {
2132 mCapture.copyPixelsFromBuffer(buffer);
2133 } catch (RuntimeException rex) {
2134 Log.e(LOGTAG, "Load capture has mismatched sizes; buffer: "
2135 + buffer.capacity() + " blob: " + blob.length
2136 + "capture: " + mCapture.getByteCount());
2137 throw rex;
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002138 }
John Reck1cf4b792011-07-26 10:22:22 -07002139 }
2140 }
2141
2142 private byte[] getCaptureBlob() {
2143 synchronized (Tab.this) {
2144 if (mCapture == null) {
2145 return null;
2146 }
2147 ByteBuffer buffer = ByteBuffer.allocate(mCapture.getByteCount());
2148 mCapture.copyPixelsToBuffer(buffer);
2149 return buffer.array();
2150 }
2151 }
2152
2153 private Runnable mSaveThumbnail = new Runnable() {
2154
2155 @Override
2156 public void run() {
2157 byte[] blob = getCaptureBlob();
2158 if (blob == null) {
2159 return;
2160 }
2161 ContentResolver cr = mContext.getContentResolver();
2162 ContentValues values = new ContentValues();
2163 values.put(Thumbnails._ID, mId);
2164 values.put(Thumbnails.THUMBNAIL, blob);
2165 cr.insert(Thumbnails.CONTENT_URI, values);
2166 }
2167 };
2168
2169 private Runnable mDeleteThumbnail = new Runnable() {
2170
2171 @Override
2172 public void run() {
2173 ContentResolver cr = mContext.getContentResolver();
2174 try {
2175 cr.delete(ContentUris.withAppendedId(Thumbnails.CONTENT_URI, mId),
2176 null, null);
2177 } catch (Throwable t) {}
2178 }
2179 };
2180
2181 private Runnable mLoadThumbnail = new Runnable() {
2182
2183 @Override
2184 public void run() {
2185 ContentResolver cr = mContext.getContentResolver();
2186 Cursor c = null;
2187 try {
2188 Uri uri = ContentUris.withAppendedId(Thumbnails.CONTENT_URI, mId);
2189 c = cr.query(uri, new String[] {Thumbnails._ID,
2190 Thumbnails.THUMBNAIL}, null, null, null);
2191 if (c.moveToFirst()) {
2192 byte[] data = c.getBlob(1);
2193 if (data != null && data.length > 0) {
2194 updateCaptureFromBlob(data);
2195 }
2196 }
2197 } finally {
2198 if (c != null) {
2199 c.close();
2200 }
2201 }
2202 }
2203 };
2204
John Reck52be4782011-08-26 15:37:29 -07002205 @Override
2206 public String toString() {
2207 StringBuilder builder = new StringBuilder(100);
2208 builder.append(mId);
2209 builder.append(") has parent: ");
2210 if (getParent() != null) {
2211 builder.append("true[");
2212 builder.append(getParent().getId());
2213 builder.append("]");
2214 } else {
2215 builder.append("false");
2216 }
2217 builder.append(", incog: ");
2218 builder.append(isPrivateBrowsingEnabled());
2219 if (!isPrivateBrowsingEnabled()) {
2220 builder.append(", title: ");
2221 builder.append(getTitle());
2222 builder.append(", url: ");
2223 builder.append(getUrl());
2224 }
2225 return builder.toString();
2226 }
2227
Grace Kloba22ac16e2009-10-07 18:00:23 -07002228}