blob: 8023375fcd6e37543b522f0e2bfad213fc0d2335 [file] [log] [blame]
Grace Kloba22ac16e2009-10-07 18:00:23 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.browser;
18
Michael Kolb8233fac2010-10-26 16:08:53 -070019import android.app.Activity;
Grace Kloba22ac16e2009-10-07 18:00:23 -070020import android.app.AlertDialog;
Leon Scroggins58d56c62010-01-28 15:12:40 -050021import android.app.SearchManager;
Grace Kloba22ac16e2009-10-07 18:00:23 -070022import android.content.ContentResolver;
John Reck1cf4b792011-07-26 10:22:22 -070023import android.content.ContentUris;
John Reckd8c74522011-06-14 08:45:00 -070024import android.content.ContentValues;
John Reck30c714c2010-12-16 17:30:34 -080025import android.content.Context;
Grace Kloba22ac16e2009-10-07 18:00:23 -070026import android.content.DialogInterface;
Michael Kolbfe251992010-07-08 15:41:55 -070027import android.content.DialogInterface.OnCancelListener;
Jeff Hamilton8ce956c2010-08-17 11:13:53 -050028import android.content.Intent;
John Reck1cf4b792011-07-26 10:22:22 -070029import android.database.Cursor;
Grace Kloba22ac16e2009-10-07 18:00:23 -070030import android.graphics.Bitmap;
John Reck8cc92352011-07-06 17:41:52 -070031import android.graphics.Bitmap.CompressFormat;
Michael Kolb9ef259a2011-07-12 15:33:08 -070032import android.graphics.BitmapFactory;
33import android.graphics.Canvas;
Michael Kolbc3af0672011-08-09 10:24:41 -070034import android.graphics.Color;
Michael Kolba3194d02011-09-07 11:23:51 -070035import android.graphics.Paint;
Michael Kolb9ef259a2011-07-12 15:33:08 -070036import android.graphics.Picture;
Michael Kolba3194d02011-09-07 11:23:51 -070037import android.graphics.PorterDuff;
38import android.graphics.PorterDuffXfermode;
Grace Kloba22ac16e2009-10-07 18:00:23 -070039import android.net.Uri;
40import android.net.http.SslError;
Grace Kloba22ac16e2009-10-07 18:00:23 -070041import android.os.Bundle;
Michael Kolb9ef259a2011-07-12 15:33:08 -070042import android.os.Handler;
Grace Kloba22ac16e2009-10-07 18:00:23 -070043import android.os.Message;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +000044import android.os.SystemClock;
Brian Carlstrom8862c1d2011-06-02 01:05:55 -070045import android.security.KeyChain;
Brian Carlstromaa09cd82011-06-09 16:04:40 -070046import android.security.KeyChainAliasCallback;
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -050047import android.speech.RecognizerResultsIntent;
John Reck24f18262011-06-17 14:47:20 -070048import android.text.TextUtils;
Grace Kloba22ac16e2009-10-07 18:00:23 -070049import android.util.Log;
50import android.view.KeyEvent;
51import android.view.LayoutInflater;
52import android.view.View;
Grace Kloba50c241e2010-04-20 11:07:50 -070053import android.view.ViewStub;
Brian Carlstrom8862c1d2011-06-02 01:05:55 -070054import android.webkit.ClientCertRequestHandler;
Ben Murdochc42addf2010-01-28 15:19:59 +000055import android.webkit.ConsoleMessage;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -050056import android.webkit.DownloadListener;
Grace Kloba22ac16e2009-10-07 18:00:23 -070057import android.webkit.GeolocationPermissions;
58import android.webkit.HttpAuthHandler;
59import android.webkit.SslErrorHandler;
60import android.webkit.URLUtil;
61import android.webkit.ValueCallback;
John Reck6c2e2f32011-08-22 13:41:23 -070062import android.webkit.WebBackForwardList;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -050063import android.webkit.WebBackForwardListClient;
Grace Kloba22ac16e2009-10-07 18:00:23 -070064import android.webkit.WebChromeClient;
65import android.webkit.WebHistoryItem;
John Reck438bf462011-01-12 18:11:46 -080066import android.webkit.WebResourceResponse;
Grace Kloba22ac16e2009-10-07 18:00:23 -070067import android.webkit.WebStorage;
68import android.webkit.WebView;
Michael Kolb9ef259a2011-07-12 15:33:08 -070069import android.webkit.WebView.PictureListener;
Grace Kloba22ac16e2009-10-07 18:00:23 -070070import android.webkit.WebViewClient;
Ben Murdoch1d676b62011-01-17 12:54:24 +000071import android.widget.CheckBox;
Ben Murdoch8029a772010-11-16 11:58:21 +000072import android.widget.Toast;
Grace Kloba22ac16e2009-10-07 18:00:23 -070073
John Reck8ee633f2011-08-09 16:00:35 -070074import com.android.browser.TabControl.OnThumbnailUpdatedListener;
John Reck541f55a2011-06-07 16:34:43 -070075import com.android.browser.homepages.HomeProvider;
John Reck1cf4b792011-07-26 10:22:22 -070076import com.android.browser.provider.BrowserProvider2.Thumbnails;
John Reck8cc92352011-07-06 17:41:52 -070077import com.android.browser.provider.SnapshotProvider.Snapshots;
John Reck541f55a2011-06-07 16:34:43 -070078import com.android.common.speech.LoggingEvents;
79
80import java.io.ByteArrayOutputStream;
John Reck1cf4b792011-07-26 10:22:22 -070081import java.nio.ByteBuffer;
Michael Kolbfe251992010-07-08 15:41:55 -070082import java.util.ArrayList;
83import java.util.HashMap;
84import java.util.Iterator;
85import java.util.LinkedList;
86import java.util.Map;
87import java.util.Vector;
Mathew Inwooda829d552011-09-02 14:16:25 +010088import java.util.regex.Pattern;
John Reck8cc92352011-07-06 17:41:52 -070089import java.util.zip.GZIPOutputStream;
Michael Kolbfe251992010-07-08 15:41:55 -070090
Grace Kloba22ac16e2009-10-07 18:00:23 -070091/**
92 * Class for maintaining Tabs with a main WebView and a subwindow.
93 */
Michael Kolb9ef259a2011-07-12 15:33:08 -070094class Tab implements PictureListener {
Michael Kolb8233fac2010-10-26 16:08:53 -070095
Grace Kloba22ac16e2009-10-07 18:00:23 -070096 // Log Tag
97 private static final String LOGTAG = "Tab";
Mathew Inwood1dd8e822011-08-03 14:34:29 +010098 private static final boolean LOGD_ENABLED = com.android.browser.Browser.LOGD_ENABLED;
Ben Murdochc42addf2010-01-28 15:19:59 +000099 // Special case the logtag for messages for the Console to make it easier to
100 // filter them and match the logtag used for these messages in older versions
101 // of the browser.
102 private static final String CONSOLE_LOGTAG = "browser";
103
Michael Kolb9ef259a2011-07-12 15:33:08 -0700104 private static final int MSG_CAPTURE = 42;
John Reck8ee633f2011-08-09 16:00:35 -0700105 private static final int CAPTURE_DELAY = 100;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700106
John Reck1cf4b792011-07-26 10:22:22 -0700107 private static Bitmap sDefaultFavicon;
108
Michael Kolba3194d02011-09-07 11:23:51 -0700109 private static Paint sAlphaPaint = new Paint();
110 static {
111 sAlphaPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
112 sAlphaPaint.setColor(Color.TRANSPARENT);
113 }
114
Steve Block2466eff2011-10-03 15:33:09 +0100115 public enum SecurityState {
116 // The page does not use SSL.
117 SECURITY_STATE_NOT_SECURE,
118 // The page uses SSL, the certificate is good and all elements are secure.
119 SECURITY_STATE_SECURE,
120 // The page uses SSL and the certificate is good, but some elements are insecure.
121 SECURITY_STATE_MIXED,
122 // TODO: Add SECURITY_STATE_BAD_CERTIFICATE
123 // See http://b/5403366
124 // The page uses SSL but there is a problem with the certificate.
125 //SECURITY_STATE_BAD_CERTIFICATE,
John Reck30c714c2010-12-16 17:30:34 -0800126 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700127
Michael Kolb14612442011-06-24 13:06:29 -0700128 Context mContext;
John Reckd8c74522011-06-14 08:45:00 -0700129 protected WebViewController mWebViewController;
Michael Kolb8233fac2010-10-26 16:08:53 -0700130
Michael Kolbc831b632011-05-11 09:30:34 -0700131 // The tab ID
John Reckd8c74522011-06-14 08:45:00 -0700132 private long mId = -1;
Michael Kolbc831b632011-05-11 09:30:34 -0700133
Grace Kloba22ac16e2009-10-07 18:00:23 -0700134 // The Geolocation permissions prompt
135 private GeolocationPermissionsPrompt mGeolocationPermissionsPrompt;
136 // Main WebView wrapper
Michael Kolba713ec82010-11-29 17:27:06 -0800137 private View mContainer;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700138 // Main WebView
139 private WebView mMainView;
140 // Subwindow container
141 private View mSubViewContainer;
142 // Subwindow WebView
143 private WebView mSubView;
144 // Saved bundle for when we are running low on memory. It contains the
145 // information needed to restore the WebView if the user goes back to the
146 // tab.
147 private Bundle mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700148 // Parent Tab. This is the Tab that created this Tab, or null if the Tab was
149 // created by the UI
Michael Kolbc831b632011-05-11 09:30:34 -0700150 private Tab mParent;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700151 // Tab that constructed by this Tab. This is used when this Tab is
152 // destroyed, it clears all mParentTab values in the children.
Michael Kolbc831b632011-05-11 09:30:34 -0700153 private Vector<Tab> mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700154 // If true, the tab is in the foreground of the current activity.
155 private boolean mInForeground;
Michael Kolb8233fac2010-10-26 16:08:53 -0700156 // If true, the tab is in page loading state (after onPageStarted,
157 // before onPageFinsihed)
158 private boolean mInPageLoad;
John Reck30c714c2010-12-16 17:30:34 -0800159 // The last reported progress of the current page
160 private int mPageLoadProgress;
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000161 // The time the load started, used to find load page time
162 private long mLoadStartTime;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700163 // Application identifier used to find tabs that another application wants
164 // to reuse.
165 private String mAppId;
Michael Kolbe28b3472011-08-04 16:54:31 -0700166 // flag to indicate if tab should be closed on back
167 private boolean mCloseOnBack;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700168 // Keep the original url around to avoid killing the old WebView if the url
169 // has not changed.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700170 // Error console for the tab
171 private ErrorConsoleView mErrorConsole;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -0500172 // The listener that gets invoked when a download is started from the
173 // mMainView
174 private final DownloadListener mDownloadListener;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500175 // Listener used to know when we move forward or back in the history list.
176 private final WebBackForwardListClient mWebBackForwardListClient;
John Recke969cc52010-12-21 17:24:43 -0800177 private DataController mDataController;
Patrick Scott92066772011-03-10 08:46:27 -0500178 // State of the auto-login request.
179 private DeviceAccountLogin mDeviceAccountLogin;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700180
181 // AsyncTask for downloading touch icons
182 DownloadTouchIcon mTouchIconLoader;
183
John Reck35e9dd62011-04-25 09:01:54 -0700184 private BrowserSettings mSettings;
Michael Kolb9ef259a2011-07-12 15:33:08 -0700185 private int mCaptureWidth;
186 private int mCaptureHeight;
187 private Bitmap mCapture;
188 private Handler mHandler;
189
Mathew Inwood1dd8e822011-08-03 14:34:29 +0100190 /**
Mathew Inwood9ad1eac2011-09-15 11:29:50 +0100191 * See {@link #clearBackStackWhenItemAdded(String)}.
Mathew Inwood1dd8e822011-08-03 14:34:29 +0100192 */
Mathew Inwooda829d552011-09-02 14:16:25 +0100193 private Pattern mClearHistoryUrlPattern;
Mathew Inwood1dd8e822011-08-03 14:34:29 +0100194
John Reck1cf4b792011-07-26 10:22:22 -0700195 private static synchronized Bitmap getDefaultFavicon(Context context) {
196 if (sDefaultFavicon == null) {
197 sDefaultFavicon = BitmapFactory.decodeResource(
198 context.getResources(), R.drawable.app_web_browser_sm);
199 }
200 return sDefaultFavicon;
201 }
Michael Kolbeb95db42011-03-03 10:38:40 -0800202
John Reck30c714c2010-12-16 17:30:34 -0800203 // All the state needed for a page
John Reckd8c74522011-06-14 08:45:00 -0700204 protected static class PageState {
John Reck30c714c2010-12-16 17:30:34 -0800205 String mUrl;
John Reckdb22ec42011-06-29 11:31:24 -0700206 String mOriginalUrl;
John Reck30c714c2010-12-16 17:30:34 -0800207 String mTitle;
Steve Block2466eff2011-10-03 15:33:09 +0100208 SecurityState mSecurityState;
John Reck30c714c2010-12-16 17:30:34 -0800209 Bitmap mFavicon;
John Reck502a3532011-08-16 14:21:46 -0700210 boolean mIsBookmarkedSite = false;
211 boolean mIncognito = false;
John Reck30c714c2010-12-16 17:30:34 -0800212
213 PageState(Context c, boolean incognito) {
John Reck502a3532011-08-16 14:21:46 -0700214 mIncognito = incognito;
215 if (mIncognito) {
John Reckdb22ec42011-06-29 11:31:24 -0700216 mOriginalUrl = mUrl = "browser:incognito";
John Reck30c714c2010-12-16 17:30:34 -0800217 mTitle = c.getString(R.string.new_incognito_tab);
John Reck30c714c2010-12-16 17:30:34 -0800218 } else {
John Reckdb22ec42011-06-29 11:31:24 -0700219 mOriginalUrl = mUrl = "";
John Reck30c714c2010-12-16 17:30:34 -0800220 mTitle = c.getString(R.string.new_tab);
John Reck30c714c2010-12-16 17:30:34 -0800221 }
John Reck1cf4b792011-07-26 10:22:22 -0700222 mFavicon = null;
Steve Block2466eff2011-10-03 15:33:09 +0100223 mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
John Reck30c714c2010-12-16 17:30:34 -0800224 }
225
226 PageState(Context c, boolean incognito, String url, Bitmap favicon) {
John Reck502a3532011-08-16 14:21:46 -0700227 mIncognito = incognito;
John Reckdb22ec42011-06-29 11:31:24 -0700228 mOriginalUrl = mUrl = url;
John Reck30c714c2010-12-16 17:30:34 -0800229 mTitle = null;
230 if (URLUtil.isHttpsUrl(url)) {
Steve Block2466eff2011-10-03 15:33:09 +0100231 mSecurityState = SecurityState.SECURITY_STATE_SECURE;
John Reck30c714c2010-12-16 17:30:34 -0800232 } else {
Steve Block2466eff2011-10-03 15:33:09 +0100233 mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
John Reck30c714c2010-12-16 17:30:34 -0800234 }
John Reck1cf4b792011-07-26 10:22:22 -0700235 mFavicon = favicon;
John Reck30c714c2010-12-16 17:30:34 -0800236 }
John Reck1cf4b792011-07-26 10:22:22 -0700237
Grace Kloba22ac16e2009-10-07 18:00:23 -0700238 }
239
John Reck30c714c2010-12-16 17:30:34 -0800240 // The current/loading page's state
John Reckd8c74522011-06-14 08:45:00 -0700241 protected PageState mCurrentState;
John Reck30c714c2010-12-16 17:30:34 -0800242
Grace Kloba22ac16e2009-10-07 18:00:23 -0700243 // Used for saving and restoring each Tab
Michael Kolbc831b632011-05-11 09:30:34 -0700244 static final String ID = "ID";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700245 static final String CURRURL = "currentUrl";
246 static final String CURRTITLE = "currentTitle";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700247 static final String PARENTTAB = "parentTab";
248 static final String APPID = "appid";
Elliott Slaughter3d6df162010-08-25 13:17:44 -0700249 static final String INCOGNITO = "privateBrowsingEnabled";
John Reckb0a86db2011-05-24 14:05:58 -0700250 static final String USERAGENT = "useragent";
Michael Kolbe28b3472011-08-04 16:54:31 -0700251 static final String CLOSEFLAG = "closeOnBack";
Grace Kloba22ac16e2009-10-07 18:00:23 -0700252
253 // -------------------------------------------------------------------------
254
Leon Scroggins58d56c62010-01-28 15:12:40 -0500255 /**
256 * Private information regarding the latest voice search. If the Tab is not
257 * in voice search mode, this will be null.
258 */
259 private VoiceSearchData mVoiceSearchData;
260 /**
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400261 * Remove voice search mode from this tab.
262 */
263 public void revertVoiceSearchMode() {
264 if (mVoiceSearchData != null) {
265 mVoiceSearchData = null;
266 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700267 mWebViewController.revertVoiceSearchMode(this);
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400268 }
269 }
270 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700271
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400272 /**
Leon Scroggins58d56c62010-01-28 15:12:40 -0500273 * Return whether the tab is in voice search mode.
274 */
275 public boolean isInVoiceSearchMode() {
276 return mVoiceSearchData != null;
277 }
278 /**
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400279 * Return true if the Tab is in voice search mode and the voice search
280 * Intent came with a String identifying that Google provided the Intent.
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500281 */
282 public boolean voiceSearchSourceIsGoogle() {
283 return mVoiceSearchData != null && mVoiceSearchData.mSourceIsGoogle;
284 }
285 /**
Leon Scroggins58d56c62010-01-28 15:12:40 -0500286 * Get the title to display for the current voice search page. If the Tab
287 * is not in voice search mode, return null.
288 */
289 public String getVoiceDisplayTitle() {
290 if (mVoiceSearchData == null) return null;
291 return mVoiceSearchData.mLastVoiceSearchTitle;
292 }
293 /**
294 * Get the latest array of voice search results, to be passed to the
295 * BrowserProvider. If the Tab is not in voice search mode, return null.
296 */
297 public ArrayList<String> getVoiceSearchResults() {
298 if (mVoiceSearchData == null) return null;
299 return mVoiceSearchData.mVoiceSearchResults;
300 }
301 /**
302 * Activate voice search mode.
303 * @param intent Intent which has the results to use, or an index into the
304 * results when reusing the old results.
305 */
306 /* package */ void activateVoiceSearchMode(Intent intent) {
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500307 int index = 0;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500308 ArrayList<String> results = intent.getStringArrayListExtra(
Leon Scrogginsa1cc3fd2010-02-01 16:14:11 -0500309 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_STRINGS);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500310 if (results != null) {
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500311 ArrayList<String> urls = intent.getStringArrayListExtra(
312 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_URLS);
313 ArrayList<String> htmls = intent.getStringArrayListExtra(
314 RecognizerResultsIntent.EXTRA_VOICE_SEARCH_RESULT_HTML);
315 ArrayList<String> baseUrls = intent.getStringArrayListExtra(
316 RecognizerResultsIntent
317 .EXTRA_VOICE_SEARCH_RESULT_HTML_BASE_URLS);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500318 // This tab is now entering voice search mode for the first time, or
319 // a new voice search was done.
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500320 int size = results.size();
321 if (urls == null || size != urls.size()) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500322 throw new AssertionError("improper extras passed in Intent");
323 }
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500324 if (htmls == null || htmls.size() != size || baseUrls == null ||
325 (baseUrls.size() != size && baseUrls.size() != 1)) {
326 // If either of these arrays are empty/incorrectly sized, ignore
327 // them.
328 htmls = null;
329 baseUrls = null;
330 }
331 mVoiceSearchData = new VoiceSearchData(results, urls, htmls,
332 baseUrls);
Leon Scroggins9df94972010-03-08 18:20:35 -0500333 mVoiceSearchData.mHeaders = intent.getParcelableArrayListExtra(
334 RecognizerResultsIntent
335 .EXTRA_VOICE_SEARCH_RESULT_HTTP_HEADERS);
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500336 mVoiceSearchData.mSourceIsGoogle = intent.getBooleanExtra(
337 VoiceSearchData.SOURCE_IS_GOOGLE, false);
Leon Scroggins2ee4a5a2010-03-15 16:56:57 -0400338 mVoiceSearchData.mVoiceSearchIntent = new Intent(intent);
Leon Scrogginse10dde52010-03-08 19:53:03 -0500339 }
340 String extraData = intent.getStringExtra(
341 SearchManager.EXTRA_DATA_KEY);
342 if (extraData != null) {
343 index = Integer.parseInt(extraData);
344 if (index >= mVoiceSearchData.mVoiceSearchResults.size()) {
345 throw new AssertionError("index must be less than "
346 + "size of mVoiceSearchResults");
347 }
348 if (mVoiceSearchData.mSourceIsGoogle) {
349 Intent logIntent = new Intent(
350 LoggingEvents.ACTION_LOG_EVENT);
351 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
352 LoggingEvents.VoiceSearch.N_BEST_CHOOSE);
353 logIntent.putExtra(
354 LoggingEvents.VoiceSearch.EXTRA_N_BEST_CHOOSE_INDEX,
355 index);
Michael Kolb14612442011-06-24 13:06:29 -0700356 mContext.sendBroadcast(logIntent);
Leon Scrogginse10dde52010-03-08 19:53:03 -0500357 }
358 if (mVoiceSearchData.mVoiceSearchIntent != null) {
Leon Scroggins2ee4a5a2010-03-15 16:56:57 -0400359 // Copy the Intent, so that each history item will have its own
360 // Intent, with different (or none) extra data.
361 Intent latest = new Intent(mVoiceSearchData.mVoiceSearchIntent);
362 latest.putExtra(SearchManager.EXTRA_DATA_KEY, extraData);
363 mVoiceSearchData.mVoiceSearchIntent = latest;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500364 }
365 }
366 mVoiceSearchData.mLastVoiceSearchTitle
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500367 = mVoiceSearchData.mVoiceSearchResults.get(index);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500368 if (mInForeground) {
Michael Kolb11d19782011-03-20 10:17:40 -0700369 mWebViewController.activateVoiceSearchMode(
370 mVoiceSearchData.mLastVoiceSearchTitle,
371 mVoiceSearchData.mVoiceSearchResults);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500372 }
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500373 if (mVoiceSearchData.mVoiceSearchHtmls != null) {
374 // When index was found it was already ensured that it was valid
375 String uriString = mVoiceSearchData.mVoiceSearchHtmls.get(index);
376 if (uriString != null) {
377 Uri dataUri = Uri.parse(uriString);
378 if (RecognizerResultsIntent.URI_SCHEME_INLINE.equals(
379 dataUri.getScheme())) {
380 // If there is only one base URL, use it. If there are
381 // more, there will be one for each index, so use the base
382 // URL corresponding to the index.
383 String baseUrl = mVoiceSearchData.mVoiceSearchBaseUrls.get(
384 mVoiceSearchData.mVoiceSearchBaseUrls.size() > 1 ?
385 index : 0);
386 mVoiceSearchData.mLastVoiceSearchUrl = baseUrl;
387 mMainView.loadDataWithBaseURL(baseUrl,
388 uriString.substring(RecognizerResultsIntent
389 .URI_SCHEME_INLINE.length() + 1), "text/html",
390 "utf-8", baseUrl);
391 return;
392 }
393 }
394 }
Leon Scroggins58d56c62010-01-28 15:12:40 -0500395 mVoiceSearchData.mLastVoiceSearchUrl
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500396 = mVoiceSearchData.mVoiceSearchUrls.get(index);
397 if (null == mVoiceSearchData.mLastVoiceSearchUrl) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700398 mVoiceSearchData.mLastVoiceSearchUrl = UrlUtils.smartUrlFilter(
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500399 mVoiceSearchData.mLastVoiceSearchTitle);
400 }
Leon Scroggins9df94972010-03-08 18:20:35 -0500401 Map<String, String> headers = null;
402 if (mVoiceSearchData.mHeaders != null) {
403 int bundleIndex = mVoiceSearchData.mHeaders.size() == 1 ? 0
404 : index;
405 Bundle bundle = mVoiceSearchData.mHeaders.get(bundleIndex);
406 if (bundle != null && !bundle.isEmpty()) {
407 Iterator<String> iter = bundle.keySet().iterator();
408 headers = new HashMap<String, String>();
409 while (iter.hasNext()) {
410 String key = iter.next();
411 headers.put(key, bundle.getString(key));
412 }
413 }
414 }
415 mMainView.loadUrl(mVoiceSearchData.mLastVoiceSearchUrl, headers);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500416 }
417 /* package */ static class VoiceSearchData {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500418 public VoiceSearchData(ArrayList<String> results,
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500419 ArrayList<String> urls, ArrayList<String> htmls,
420 ArrayList<String> baseUrls) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500421 mVoiceSearchResults = results;
422 mVoiceSearchUrls = urls;
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500423 mVoiceSearchHtmls = htmls;
424 mVoiceSearchBaseUrls = baseUrls;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500425 }
426 /*
427 * ArrayList of suggestions to be displayed when opening the
428 * SearchManager
429 */
430 public ArrayList<String> mVoiceSearchResults;
431 /*
432 * ArrayList of urls, associated with the suggestions in
433 * mVoiceSearchResults.
434 */
435 public ArrayList<String> mVoiceSearchUrls;
436 /*
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500437 * ArrayList holding content to load for each item in
438 * mVoiceSearchResults.
439 */
440 public ArrayList<String> mVoiceSearchHtmls;
441 /*
442 * ArrayList holding base urls for the items in mVoiceSearchResults.
443 * If non null, this will either have the same size as
444 * mVoiceSearchResults or have a size of 1, in which case all will use
445 * the same base url
446 */
447 public ArrayList<String> mVoiceSearchBaseUrls;
448 /*
Leon Scroggins58d56c62010-01-28 15:12:40 -0500449 * The last url provided by voice search. Used for comparison to see if
Leon Scroggins82c1baa2010-02-02 16:10:57 -0500450 * we are going to a page by some method besides voice search.
Leon Scroggins58d56c62010-01-28 15:12:40 -0500451 */
452 public String mLastVoiceSearchUrl;
453 /**
454 * The last title used for voice search. Needed to update the title bar
455 * when switching tabs.
456 */
457 public String mLastVoiceSearchTitle;
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500458 /**
459 * Whether the Intent which turned on voice search mode contained the
460 * String signifying that Google was the source.
461 */
462 public boolean mSourceIsGoogle;
463 /**
Leon Scroggins9df94972010-03-08 18:20:35 -0500464 * List of headers to be passed into the WebView containing location
465 * information
466 */
467 public ArrayList<Bundle> mHeaders;
468 /**
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500469 * The Intent used to invoke voice search. Placed on the
470 * WebHistoryItem so that when coming back to a previous voice search
471 * page we can again activate voice search.
472 */
Leon Scrogginse10dde52010-03-08 19:53:03 -0500473 public Intent mVoiceSearchIntent;
Leon Scroggins0c75a8e2010-03-03 16:40:58 -0500474 /**
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500475 * String used to identify Google as the source of voice search.
476 */
477 public static String SOURCE_IS_GOOGLE
478 = "android.speech.extras.SOURCE_IS_GOOGLE";
Leon Scroggins58d56c62010-01-28 15:12:40 -0500479 }
480
Grace Kloba22ac16e2009-10-07 18:00:23 -0700481 // Container class for the next error dialog that needs to be displayed
482 private class ErrorDialog {
483 public final int mTitle;
484 public final String mDescription;
485 public final int mError;
486 ErrorDialog(int title, String desc, int error) {
487 mTitle = title;
488 mDescription = desc;
489 mError = error;
490 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700491 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700492
493 private void processNextError() {
494 if (mQueuedErrors == null) {
495 return;
496 }
497 // The first one is currently displayed so just remove it.
498 mQueuedErrors.removeFirst();
499 if (mQueuedErrors.size() == 0) {
500 mQueuedErrors = null;
501 return;
502 }
503 showError(mQueuedErrors.getFirst());
504 }
505
506 private DialogInterface.OnDismissListener mDialogListener =
507 new DialogInterface.OnDismissListener() {
508 public void onDismiss(DialogInterface d) {
509 processNextError();
510 }
511 };
512 private LinkedList<ErrorDialog> mQueuedErrors;
513
514 private void queueError(int err, String desc) {
515 if (mQueuedErrors == null) {
516 mQueuedErrors = new LinkedList<ErrorDialog>();
517 }
518 for (ErrorDialog d : mQueuedErrors) {
519 if (d.mError == err) {
520 // Already saw a similar error, ignore the new one.
521 return;
522 }
523 }
524 ErrorDialog errDialog = new ErrorDialog(
525 err == WebViewClient.ERROR_FILE_NOT_FOUND ?
526 R.string.browserFrameFileErrorLabel :
527 R.string.browserFrameNetworkErrorLabel,
528 desc, err);
529 mQueuedErrors.addLast(errDialog);
530
531 // Show the dialog now if the queue was empty and it is in foreground
532 if (mQueuedErrors.size() == 1 && mInForeground) {
533 showError(errDialog);
534 }
535 }
536
537 private void showError(ErrorDialog errDialog) {
538 if (mInForeground) {
Michael Kolb14612442011-06-24 13:06:29 -0700539 AlertDialog d = new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700540 .setTitle(errDialog.mTitle)
541 .setMessage(errDialog.mDescription)
542 .setPositiveButton(R.string.ok, null)
543 .create();
544 d.setOnDismissListener(mDialogListener);
545 d.show();
546 }
547 }
548
549 // -------------------------------------------------------------------------
550 // WebViewClient implementation for the main WebView
551 // -------------------------------------------------------------------------
552
553 private final WebViewClient mWebViewClient = new WebViewClient() {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500554 private Message mDontResend;
555 private Message mResend;
Michael Kolb47bd1e42011-09-01 15:25:00 -0700556
557 private boolean providersDiffer(String url, String otherUrl) {
558 Uri uri1 = Uri.parse(url);
559 Uri uri2 = Uri.parse(otherUrl);
560 return !uri1.getEncodedAuthority().equals(uri2.getEncodedAuthority());
561 }
562
Grace Kloba22ac16e2009-10-07 18:00:23 -0700563 @Override
564 public void onPageStarted(WebView view, String url, Bitmap favicon) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700565 mInPageLoad = true;
John Reck30c714c2010-12-16 17:30:34 -0800566 mPageLoadProgress = 0;
Michael Kolb14612442011-06-24 13:06:29 -0700567 mCurrentState = new PageState(mContext,
John Reck30c714c2010-12-16 17:30:34 -0800568 view.isPrivateBrowsingEnabled(), url, favicon);
Kristian Monsen4dce3bf2010-02-02 13:37:09 +0000569 mLoadStartTime = SystemClock.uptimeMillis();
Leon Scroggins58d56c62010-01-28 15:12:40 -0500570 if (mVoiceSearchData != null
Michael Kolb47bd1e42011-09-01 15:25:00 -0700571 && providersDiffer(url, mVoiceSearchData.mLastVoiceSearchUrl)) {
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500572 if (mVoiceSearchData.mSourceIsGoogle) {
573 Intent i = new Intent(LoggingEvents.ACTION_LOG_EVENT);
574 i.putExtra(LoggingEvents.EXTRA_FLUSH, true);
Michael Kolb14612442011-06-24 13:06:29 -0700575 mContext.sendBroadcast(i);
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500576 }
Leon Scroggins III95d9bfd2010-09-14 14:02:36 -0400577 revertVoiceSearchMode();
Leon Scroggins58d56c62010-01-28 15:12:40 -0500578 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700579
Grace Kloba22ac16e2009-10-07 18:00:23 -0700580
581 // If we start a touch icon load and then load a new page, we don't
582 // want to cancel the current touch icon loader. But, we do want to
583 // create a new one when the touch icon url is known.
584 if (mTouchIconLoader != null) {
585 mTouchIconLoader.mTab = null;
586 mTouchIconLoader = null;
587 }
588
589 // reset the error console
590 if (mErrorConsole != null) {
591 mErrorConsole.clearErrorMessages();
Michael Kolb8233fac2010-10-26 16:08:53 -0700592 if (mWebViewController.shouldShowErrorConsole()) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700593 mErrorConsole.showConsole(ErrorConsoleView.SHOW_NONE);
594 }
595 }
596
Patrick Scott92066772011-03-10 08:46:27 -0500597 // Cancel the auto-login process.
598 if (mDeviceAccountLogin != null) {
599 mDeviceAccountLogin.cancel();
600 mDeviceAccountLogin = null;
601 mWebViewController.hideAutoLogin(Tab.this);
602 }
603
Grace Kloba22ac16e2009-10-07 18:00:23 -0700604 // finally update the UI in the activity if it is in the foreground
John Reck324d4402011-01-11 16:56:42 -0800605 mWebViewController.onPageStarted(Tab.this, view, favicon);
Leon Scroggins4cd97792010-12-03 15:31:56 -0500606
John Recke969cc52010-12-21 17:24:43 -0800607 updateBookmarkedStatus();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700608 }
609
610 @Override
611 public void onPageFinished(WebView view, String url) {
John Recka1696282011-07-08 14:10:37 -0700612 if (!mInPageLoad) {
613 // In page navigation links (www.something.com#footer) will
614 // trigger an onPageFinished which we don't care about.
615 return;
616 }
John Reck5b691842010-11-29 11:21:13 -0800617 if (!isPrivateBrowsingEnabled()) {
618 LogTag.logPageFinishedLoading(
619 url, SystemClock.uptimeMillis() - mLoadStartTime);
620 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700621 mInPageLoad = false;
John Reck1cf4b792011-07-26 10:22:22 -0700622 syncCurrentState(view, url);
John Reck324d4402011-01-11 16:56:42 -0800623 mWebViewController.onPageFinished(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700624 }
625
626 // return true if want to hijack the url to let another app to handle it
627 @Override
628 public boolean shouldOverrideUrlLoading(WebView view, String url) {
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400629 if (voiceSearchSourceIsGoogle()) {
630 // This method is called when the user clicks on a link.
631 // VoiceSearchMode is turned off when the user leaves the
632 // Google results page, so at this point the user must be on
633 // that page. If the user clicked a link on that page, assume
634 // that the voice search was effective, and broadcast an Intent
635 // so a receiver can take note of that fact.
636 Intent logIntent = new Intent(LoggingEvents.ACTION_LOG_EVENT);
637 logIntent.putExtra(LoggingEvents.EXTRA_EVENT,
638 LoggingEvents.VoiceSearch.RESULT_CLICKED);
Michael Kolb14612442011-06-24 13:06:29 -0700639 mContext.sendBroadcast(logIntent);
Leon Scroggins IIIc1f5ae22010-06-29 17:11:29 -0400640 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700641 if (mInForeground) {
Michael Kolb18eb3772010-12-10 14:29:51 -0800642 return mWebViewController.shouldOverrideUrlLoading(Tab.this,
643 view, url);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700644 } else {
645 return false;
646 }
647 }
648
649 /**
Steve Block2466eff2011-10-03 15:33:09 +0100650 * Updates the security state. This method is called when we discover
651 * another resource to be loaded for this page (for example,
652 * javascript). While we update the security state, we do not update
653 * the lock icon until we are done loading, as it is slightly more
654 * secure this way.
Grace Kloba22ac16e2009-10-07 18:00:23 -0700655 */
656 @Override
657 public void onLoadResource(WebView view, String url) {
658 if (url != null && url.length() > 0) {
659 // It is only if the page claims to be secure that we may have
Steve Block2466eff2011-10-03 15:33:09 +0100660 // to update the security state:
661 if (mCurrentState.mSecurityState == SecurityState.SECURITY_STATE_SECURE) {
662 // If NOT a 'safe' url, change the state to mixed content!
Grace Kloba22ac16e2009-10-07 18:00:23 -0700663 if (!(URLUtil.isHttpsUrl(url) || URLUtil.isDataUrl(url)
664 || URLUtil.isAboutUrl(url))) {
Steve Block2466eff2011-10-03 15:33:09 +0100665 mCurrentState.mSecurityState = SecurityState.SECURITY_STATE_MIXED;
Grace Kloba22ac16e2009-10-07 18:00:23 -0700666 }
667 }
668 }
669 }
670
671 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700672 * Show a dialog informing the user of the network error reported by
673 * WebCore if it is in the foreground.
674 */
675 @Override
676 public void onReceivedError(WebView view, int errorCode,
677 String description, String failingUrl) {
678 if (errorCode != WebViewClient.ERROR_HOST_LOOKUP &&
679 errorCode != WebViewClient.ERROR_CONNECT &&
680 errorCode != WebViewClient.ERROR_BAD_URL &&
681 errorCode != WebViewClient.ERROR_UNSUPPORTED_SCHEME &&
682 errorCode != WebViewClient.ERROR_FILE) {
683 queueError(errorCode, description);
684 }
Jeff Hamilton47654f42010-09-07 09:57:51 -0500685
686 // Don't log URLs when in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -0700687 if (!isPrivateBrowsingEnabled()) {
Jeff Hamilton47654f42010-09-07 09:57:51 -0500688 Log.e(LOGTAG, "onReceivedError " + errorCode + " " + failingUrl
689 + " " + description);
690 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700691 }
692
693 /**
694 * Check with the user if it is ok to resend POST data as the page they
695 * are trying to navigate to is the result of a POST.
696 */
697 @Override
698 public void onFormResubmission(WebView view, final Message dontResend,
699 final Message resend) {
700 if (!mInForeground) {
701 dontResend.sendToTarget();
702 return;
703 }
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500704 if (mDontResend != null) {
705 Log.w(LOGTAG, "onFormResubmission should not be called again "
706 + "while dialog is still up");
707 dontResend.sendToTarget();
708 return;
709 }
710 mDontResend = dontResend;
711 mResend = resend;
Michael Kolb14612442011-06-24 13:06:29 -0700712 new AlertDialog.Builder(mContext).setTitle(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700713 R.string.browserFrameFormResubmitLabel).setMessage(
714 R.string.browserFrameFormResubmitMessage)
715 .setPositiveButton(R.string.ok,
716 new DialogInterface.OnClickListener() {
717 public void onClick(DialogInterface dialog,
718 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500719 if (mResend != null) {
720 mResend.sendToTarget();
721 mResend = null;
722 mDontResend = null;
723 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700724 }
725 }).setNegativeButton(R.string.cancel,
726 new DialogInterface.OnClickListener() {
727 public void onClick(DialogInterface dialog,
728 int which) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500729 if (mDontResend != null) {
730 mDontResend.sendToTarget();
731 mResend = null;
732 mDontResend = null;
733 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700734 }
735 }).setOnCancelListener(new OnCancelListener() {
736 public void onCancel(DialogInterface dialog) {
Leon Scroggins4a64a8a2010-03-02 17:57:40 -0500737 if (mDontResend != null) {
738 mDontResend.sendToTarget();
739 mResend = null;
740 mDontResend = null;
741 }
Grace Kloba22ac16e2009-10-07 18:00:23 -0700742 }
743 }).show();
744 }
745
746 /**
747 * Insert the url into the visited history database.
748 * @param url The url to be inserted.
749 * @param isReload True if this url is being reloaded.
750 * FIXME: Not sure what to do when reloading the page.
751 */
752 @Override
753 public void doUpdateVisitedHistory(WebView view, String url,
754 boolean isReload) {
John Reck324d4402011-01-11 16:56:42 -0800755 mWebViewController.doUpdateVisitedHistory(Tab.this, isReload);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700756 }
757
758 /**
759 * Displays SSL error(s) dialog to the user.
760 */
761 @Override
762 public void onReceivedSslError(final WebView view,
763 final SslErrorHandler handler, final SslError error) {
764 if (!mInForeground) {
765 handler.cancel();
Steve Block2466eff2011-10-03 15:33:09 +0100766 setSecurityState(SecurityState.SECURITY_STATE_NOT_SECURE);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700767 return;
768 }
John Reck35e9dd62011-04-25 09:01:54 -0700769 if (mSettings.showSecurityWarnings()) {
John Reckcb28b2c2011-08-26 17:39:44 -0700770 new AlertDialog.Builder(mContext)
771 .setTitle(R.string.security_warning)
772 .setMessage(R.string.ssl_warnings_header)
773 .setIcon(android.R.drawable.ic_dialog_alert)
774 .setPositiveButton(R.string.ssl_continue,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700775 new DialogInterface.OnClickListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700776 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700777 public void onClick(DialogInterface dialog,
778 int whichButton) {
779 handler.proceed();
780 }
John Reckcb28b2c2011-08-26 17:39:44 -0700781 })
782 .setNeutralButton(R.string.view_certificate,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700783 new DialogInterface.OnClickListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700784 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700785 public void onClick(DialogInterface dialog,
786 int whichButton) {
John Reckcb28b2c2011-08-26 17:39:44 -0700787 mWebViewController.showSslCertificateOnError(
788 view, handler, error);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700789 }
John Reckcb28b2c2011-08-26 17:39:44 -0700790 })
791 .setNegativeButton(R.string.ssl_go_back,
Grace Kloba22ac16e2009-10-07 18:00:23 -0700792 new DialogInterface.OnClickListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700793 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700794 public void onClick(DialogInterface dialog,
795 int whichButton) {
John Reck30c714c2010-12-16 17:30:34 -0800796 dialog.cancel();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700797 }
John Reckcb28b2c2011-08-26 17:39:44 -0700798 })
799 .setOnCancelListener(
Grace Kloba22ac16e2009-10-07 18:00:23 -0700800 new DialogInterface.OnCancelListener() {
John Reckcb28b2c2011-08-26 17:39:44 -0700801 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700802 public void onCancel(DialogInterface dialog) {
803 handler.cancel();
Steve Block2466eff2011-10-03 15:33:09 +0100804 setSecurityState(SecurityState.SECURITY_STATE_NOT_SECURE);
John Reck30c714c2010-12-16 17:30:34 -0800805 mWebViewController.onUserCanceledSsl(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700806 }
John Reckcb28b2c2011-08-26 17:39:44 -0700807 })
808 .show();
Grace Kloba22ac16e2009-10-07 18:00:23 -0700809 } else {
810 handler.proceed();
811 }
812 }
813
814 /**
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700815 * Displays client certificate request to the user.
816 */
817 @Override
818 public void onReceivedClientCertRequest(final WebView view,
819 final ClientCertRequestHandler handler, final String host_and_port) {
820 if (!mInForeground) {
821 handler.ignore();
822 return;
823 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700824 int colon = host_and_port.lastIndexOf(':');
825 String host;
826 int port;
827 if (colon == -1) {
828 host = host_and_port;
829 port = -1;
830 } else {
831 String portString = host_and_port.substring(colon + 1);
832 try {
833 port = Integer.parseInt(portString);
834 host = host_and_port.substring(0, colon);
835 } catch (NumberFormatException e) {
836 host = host_and_port;
837 port = -1;
838 }
839 }
Michael Kolb14612442011-06-24 13:06:29 -0700840 KeyChain.choosePrivateKeyAlias(
841 mWebViewController.getActivity(), new KeyChainAliasCallback() {
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700842 @Override public void alias(String alias) {
843 if (alias == null) {
844 handler.cancel();
845 return;
846 }
Michael Kolb14612442011-06-24 13:06:29 -0700847 new KeyChainLookup(mContext, handler, alias).execute();
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700848 }
Brian Carlstrom6d85fab2011-06-24 14:26:46 -0700849 }, null, null, host, port, null);
Brian Carlstrom8862c1d2011-06-02 01:05:55 -0700850 }
851
852 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -0700853 * Handles an HTTP authentication request.
854 *
855 * @param handler The authentication handler
856 * @param host The host
857 * @param realm The realm
858 */
859 @Override
860 public void onReceivedHttpAuthRequest(WebView view,
861 final HttpAuthHandler handler, final String host,
862 final String realm) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700863 mWebViewController.onReceivedHttpAuthRequest(Tab.this, view, handler, host, realm);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700864 }
865
866 @Override
John Reck438bf462011-01-12 18:11:46 -0800867 public WebResourceResponse shouldInterceptRequest(WebView view,
868 String url) {
869 WebResourceResponse res = HomeProvider.shouldInterceptRequest(
Michael Kolb14612442011-06-24 13:06:29 -0700870 mContext, url);
John Reck438bf462011-01-12 18:11:46 -0800871 return res;
872 }
873
874 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -0700875 public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
876 if (!mInForeground) {
877 return false;
878 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700879 return mWebViewController.shouldOverrideKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700880 }
881
882 @Override
883 public void onUnhandledKeyEvent(WebView view, KeyEvent event) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700884 if (!mInForeground) {
Grace Kloba22ac16e2009-10-07 18:00:23 -0700885 return;
886 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700887 mWebViewController.onUnhandledKeyEvent(event);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700888 }
Patrick Scott92066772011-03-10 08:46:27 -0500889
890 @Override
891 public void onReceivedLoginRequest(WebView view, String realm,
892 String account, String args) {
Michael Kolb14612442011-06-24 13:06:29 -0700893 new DeviceAccountLogin(mWebViewController.getActivity(), view, Tab.this, mWebViewController)
Patrick Scott92066772011-03-10 08:46:27 -0500894 .handleLogin(realm, account, args);
895 }
896
Grace Kloba22ac16e2009-10-07 18:00:23 -0700897 };
898
John Reck1cf4b792011-07-26 10:22:22 -0700899 private void syncCurrentState(WebView view, String url) {
900 // Sync state (in case of stop/timeout)
901 mCurrentState.mUrl = view.getUrl();
902 if (mCurrentState.mUrl == null) {
John Reck8bcafc12011-08-29 16:43:02 -0700903 mCurrentState.mUrl = "";
John Reck1cf4b792011-07-26 10:22:22 -0700904 }
905 mCurrentState.mOriginalUrl = view.getOriginalUrl();
906 mCurrentState.mTitle = view.getTitle();
907 mCurrentState.mFavicon = view.getFavicon();
908 if (!URLUtil.isHttpsUrl(mCurrentState.mUrl)) {
909 // In case we stop when loading an HTTPS page from an HTTP page
910 // but before a provisional load occurred
Steve Block2466eff2011-10-03 15:33:09 +0100911 mCurrentState.mSecurityState = SecurityState.SECURITY_STATE_NOT_SECURE;
John Reck1cf4b792011-07-26 10:22:22 -0700912 }
John Reck502a3532011-08-16 14:21:46 -0700913 mCurrentState.mIncognito = view.isPrivateBrowsingEnabled();
John Reck1cf4b792011-07-26 10:22:22 -0700914 }
915
Patrick Scott92066772011-03-10 08:46:27 -0500916 // Called by DeviceAccountLogin when the Tab needs to have the auto-login UI
917 // displayed.
918 void setDeviceAccountLogin(DeviceAccountLogin login) {
919 mDeviceAccountLogin = login;
920 }
921
922 // Returns non-null if the title bar should display the auto-login UI.
923 DeviceAccountLogin getDeviceAccountLogin() {
924 return mDeviceAccountLogin;
925 }
926
Grace Kloba22ac16e2009-10-07 18:00:23 -0700927 // -------------------------------------------------------------------------
928 // WebChromeClient implementation for the main WebView
929 // -------------------------------------------------------------------------
930
931 private final WebChromeClient mWebChromeClient = new WebChromeClient() {
932 // Helper method to create a new tab or sub window.
933 private void createWindow(final boolean dialog, final Message msg) {
934 WebView.WebViewTransport transport =
935 (WebView.WebViewTransport) msg.obj;
936 if (dialog) {
937 createSubWindow();
Michael Kolb8233fac2010-10-26 16:08:53 -0700938 mWebViewController.attachSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700939 transport.setWebView(mSubView);
940 } else {
Michael Kolb7bcafde2011-05-09 13:55:59 -0700941 final Tab newTab = mWebViewController.openTab(null,
John Reck5949c662011-05-27 09:52:29 -0700942 Tab.this, true, true);
Grace Kloba22ac16e2009-10-07 18:00:23 -0700943 transport.setWebView(newTab.getWebView());
944 }
945 msg.sendToTarget();
946 }
947
948 @Override
949 public boolean onCreateWindow(WebView view, final boolean dialog,
950 final boolean userGesture, final Message resultMsg) {
951 // only allow new window or sub window for the foreground case
952 if (!mInForeground) {
953 return false;
954 }
955 // Short-circuit if we can't create any more tabs or sub windows.
956 if (dialog && mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -0700957 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700958 .setTitle(R.string.too_many_subwindows_dialog_title)
959 .setIcon(android.R.drawable.ic_dialog_alert)
960 .setMessage(R.string.too_many_subwindows_dialog_message)
961 .setPositiveButton(R.string.ok, null)
962 .show();
963 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -0700964 } else if (!mWebViewController.getTabControl().canCreateNewTab()) {
Michael Kolb14612442011-06-24 13:06:29 -0700965 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -0700966 .setTitle(R.string.too_many_windows_dialog_title)
967 .setIcon(android.R.drawable.ic_dialog_alert)
968 .setMessage(R.string.too_many_windows_dialog_message)
969 .setPositiveButton(R.string.ok, null)
970 .show();
971 return false;
972 }
973
974 // Short-circuit if this was a user gesture.
975 if (userGesture) {
976 createWindow(dialog, resultMsg);
977 return true;
978 }
979
980 // Allow the popup and create the appropriate window.
981 final AlertDialog.OnClickListener allowListener =
982 new AlertDialog.OnClickListener() {
983 public void onClick(DialogInterface d,
984 int which) {
985 createWindow(dialog, resultMsg);
986 }
987 };
988
989 // Block the popup by returning a null WebView.
990 final AlertDialog.OnClickListener blockListener =
991 new AlertDialog.OnClickListener() {
992 public void onClick(DialogInterface d, int which) {
993 resultMsg.sendToTarget();
994 }
995 };
996
997 // Build a confirmation dialog to display to the user.
998 final AlertDialog d =
Michael Kolb14612442011-06-24 13:06:29 -0700999 new AlertDialog.Builder(mContext)
Grace Kloba22ac16e2009-10-07 18:00:23 -07001000 .setTitle(R.string.attention)
1001 .setIcon(android.R.drawable.ic_dialog_alert)
1002 .setMessage(R.string.popup_window_attempt)
1003 .setPositiveButton(R.string.allow, allowListener)
1004 .setNegativeButton(R.string.block, blockListener)
1005 .setCancelable(false)
1006 .create();
1007
1008 // Show the confirmation dialog.
1009 d.show();
1010 return true;
1011 }
1012
1013 @Override
Patrick Scotteb5061b2009-11-18 15:00:30 -05001014 public void onRequestFocus(WebView view) {
1015 if (!mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -07001016 mWebViewController.switchToTab(Tab.this);
Patrick Scotteb5061b2009-11-18 15:00:30 -05001017 }
1018 }
1019
1020 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -07001021 public void onCloseWindow(WebView window) {
Michael Kolbc831b632011-05-11 09:30:34 -07001022 if (mParent != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001023 // JavaScript can only close popup window.
1024 if (mInForeground) {
Michael Kolbc831b632011-05-11 09:30:34 -07001025 mWebViewController.switchToTab(mParent);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001026 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001027 mWebViewController.closeTab(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001028 }
1029 }
1030
1031 @Override
1032 public void onProgressChanged(WebView view, int newProgress) {
John Reck30c714c2010-12-16 17:30:34 -08001033 mPageLoadProgress = newProgress;
1034 mWebViewController.onProgressChanged(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001035 }
1036
1037 @Override
Leon Scroggins21d9b902010-03-11 09:33:11 -05001038 public void onReceivedTitle(WebView view, final String title) {
John Reck30c714c2010-12-16 17:30:34 -08001039 mCurrentState.mTitle = title;
Michael Kolb8233fac2010-10-26 16:08:53 -07001040 mWebViewController.onReceivedTitle(Tab.this, title);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001041 }
1042
1043 @Override
1044 public void onReceivedIcon(WebView view, Bitmap icon) {
John Reck30c714c2010-12-16 17:30:34 -08001045 mCurrentState.mFavicon = icon;
Michael Kolb8233fac2010-10-26 16:08:53 -07001046 mWebViewController.onFavicon(Tab.this, view, icon);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001047 }
1048
1049 @Override
1050 public void onReceivedTouchIconUrl(WebView view, String url,
1051 boolean precomposed) {
Michael Kolb14612442011-06-24 13:06:29 -07001052 final ContentResolver cr = mContext.getContentResolver();
Leon Scrogginsc8393d92010-04-23 14:58:16 -04001053 // Let precomposed icons take precedence over non-composed
1054 // icons.
1055 if (precomposed && mTouchIconLoader != null) {
1056 mTouchIconLoader.cancel(false);
1057 mTouchIconLoader = null;
1058 }
1059 // Have only one async task at a time.
1060 if (mTouchIconLoader == null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001061 mTouchIconLoader = new DownloadTouchIcon(Tab.this,
Michael Kolb14612442011-06-24 13:06:29 -07001062 mContext, cr, view);
Leon Scrogginsc8393d92010-04-23 14:58:16 -04001063 mTouchIconLoader.execute(url);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001064 }
1065 }
1066
1067 @Override
1068 public void onShowCustomView(View view,
1069 WebChromeClient.CustomViewCallback callback) {
Michael Kolb14612442011-06-24 13:06:29 -07001070 Activity activity = mWebViewController.getActivity();
1071 if (activity != null) {
1072 onShowCustomView(view, activity.getRequestedOrientation(), callback);
1073 }
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001074 }
1075
1076 @Override
1077 public void onShowCustomView(View view, int requestedOrientation,
1078 WebChromeClient.CustomViewCallback callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001079 if (mInForeground) mWebViewController.showCustomView(Tab.this, view,
Derek Sollenberger2d4f1e22011-06-01 14:50:42 -04001080 requestedOrientation, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001081 }
1082
1083 @Override
1084 public void onHideCustomView() {
Michael Kolb8233fac2010-10-26 16:08:53 -07001085 if (mInForeground) mWebViewController.hideCustomView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001086 }
1087
1088 /**
1089 * The origin has exceeded its database quota.
1090 * @param url the URL that exceeded the quota
1091 * @param databaseIdentifier the identifier of the database on which the
1092 * transaction that caused the quota overflow was run
1093 * @param currentQuota the current quota for the origin.
1094 * @param estimatedSize the estimated size of the database.
1095 * @param totalUsedQuota is the sum of all origins' quota.
1096 * @param quotaUpdater The callback to run when a decision to allow or
1097 * deny quota has been made. Don't forget to call this!
1098 */
1099 @Override
1100 public void onExceededDatabaseQuota(String url,
1101 String databaseIdentifier, long currentQuota, long estimatedSize,
1102 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -07001103 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001104 .onExceededDatabaseQuota(url, databaseIdentifier,
1105 currentQuota, estimatedSize, totalUsedQuota,
1106 quotaUpdater);
1107 }
1108
1109 /**
1110 * The Application Cache has exceeded its max size.
1111 * @param spaceNeeded is the amount of disk space that would be needed
1112 * in order for the last appcache operation to succeed.
1113 * @param totalUsedQuota is the sum of all origins' quota.
1114 * @param quotaUpdater A callback to inform the WebCore thread that a
1115 * new app cache size is available. This callback must always
1116 * be executed at some point to ensure that the sleeping
1117 * WebCore thread is woken up.
1118 */
1119 @Override
1120 public void onReachedMaxAppCacheSize(long spaceNeeded,
1121 long totalUsedQuota, WebStorage.QuotaUpdater quotaUpdater) {
John Reck35e9dd62011-04-25 09:01:54 -07001122 mSettings.getWebStorageSizeManager()
Grace Kloba22ac16e2009-10-07 18:00:23 -07001123 .onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota,
1124 quotaUpdater);
1125 }
1126
1127 /**
1128 * Instructs the browser to show a prompt to ask the user to set the
1129 * Geolocation permission state for the specified origin.
1130 * @param origin The origin for which Geolocation permissions are
1131 * requested.
1132 * @param callback The callback to call once the user has set the
1133 * Geolocation permission state.
1134 */
1135 @Override
1136 public void onGeolocationPermissionsShowPrompt(String origin,
1137 GeolocationPermissions.Callback callback) {
1138 if (mInForeground) {
Grace Kloba50c241e2010-04-20 11:07:50 -07001139 getGeolocationPermissionsPrompt().show(origin, callback);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001140 }
1141 }
1142
1143 /**
1144 * Instructs the browser to hide the Geolocation permissions prompt.
1145 */
1146 @Override
1147 public void onGeolocationPermissionsHidePrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001148 if (mInForeground && mGeolocationPermissionsPrompt != null) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001149 mGeolocationPermissionsPrompt.hide();
1150 }
1151 }
1152
Ben Murdoch65acc352009-11-19 18:16:04 +00001153 /* Adds a JavaScript error message to the system log and if the JS
1154 * console is enabled in the about:debug options, to that console
1155 * also.
Ben Murdochc42addf2010-01-28 15:19:59 +00001156 * @param consoleMessage the message object.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001157 */
1158 @Override
Ben Murdochc42addf2010-01-28 15:19:59 +00001159 public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001160 if (mInForeground) {
1161 // call getErrorConsole(true) so it will create one if needed
1162 ErrorConsoleView errorConsole = getErrorConsole(true);
Ben Murdochc42addf2010-01-28 15:19:59 +00001163 errorConsole.addErrorMessage(consoleMessage);
Michael Kolb8233fac2010-10-26 16:08:53 -07001164 if (mWebViewController.shouldShowErrorConsole()
1165 && errorConsole.getShowState() !=
1166 ErrorConsoleView.SHOW_MAXIMIZED) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001167 errorConsole.showConsole(ErrorConsoleView.SHOW_MINIMIZED);
1168 }
1169 }
Ben Murdochc42addf2010-01-28 15:19:59 +00001170
Jeff Hamilton47654f42010-09-07 09:57:51 -05001171 // Don't log console messages in private browsing mode
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001172 if (isPrivateBrowsingEnabled()) return true;
Jeff Hamilton47654f42010-09-07 09:57:51 -05001173
Ben Murdochc42addf2010-01-28 15:19:59 +00001174 String message = "Console: " + consoleMessage.message() + " "
1175 + consoleMessage.sourceId() + ":"
1176 + consoleMessage.lineNumber();
1177
1178 switch (consoleMessage.messageLevel()) {
1179 case TIP:
1180 Log.v(CONSOLE_LOGTAG, message);
1181 break;
1182 case LOG:
1183 Log.i(CONSOLE_LOGTAG, message);
1184 break;
1185 case WARNING:
1186 Log.w(CONSOLE_LOGTAG, message);
1187 break;
1188 case ERROR:
1189 Log.e(CONSOLE_LOGTAG, message);
1190 break;
1191 case DEBUG:
1192 Log.d(CONSOLE_LOGTAG, message);
1193 break;
1194 }
1195
1196 return true;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001197 }
1198
1199 /**
1200 * Ask the browser for an icon to represent a <video> element.
1201 * This icon will be used if the Web page did not specify a poster attribute.
1202 * @return Bitmap The icon or null if no such icon is available.
1203 */
1204 @Override
1205 public Bitmap getDefaultVideoPoster() {
1206 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001207 return mWebViewController.getDefaultVideoPoster();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001208 }
1209 return null;
1210 }
1211
1212 /**
1213 * Ask the host application for a custom progress view to show while
1214 * a <video> is loading.
1215 * @return View The progress view.
1216 */
1217 @Override
1218 public View getVideoLoadingProgressView() {
1219 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001220 return mWebViewController.getVideoLoadingProgressView();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001221 }
1222 return null;
1223 }
1224
1225 @Override
Ben Murdoch62b1b7e2010-05-19 20:38:56 +01001226 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001227 if (mInForeground) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001228 mWebViewController.openFileChooser(uploadMsg, acceptType);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001229 } else {
1230 uploadMsg.onReceiveValue(null);
1231 }
1232 }
1233
1234 /**
1235 * Deliver a list of already-visited URLs
1236 */
1237 @Override
1238 public void getVisitedHistory(final ValueCallback<String[]> callback) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001239 mWebViewController.getVisitedHistory(callback);
1240 }
Ben Murdoch8029a772010-11-16 11:58:21 +00001241
1242 @Override
1243 public void setupAutoFill(Message message) {
1244 // Prompt the user to set up their profile.
1245 final Message msg = message;
Michael Kolb14612442011-06-24 13:06:29 -07001246 AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
1247 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
Ben Murdoch1d676b62011-01-17 12:54:24 +00001248 Context.LAYOUT_INFLATER_SERVICE);
1249 final View layout = inflater.inflate(R.layout.setup_autofill_dialog, null);
1250
1251 builder.setView(layout)
Ben Murdochb7e6f942011-07-08 13:00:21 +01001252 .setTitle(R.string.autofill_setup_dialog_title)
Ben Murdoch1d676b62011-01-17 12:54:24 +00001253 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
1254 @Override
1255 public void onClick(DialogInterface dialog, int id) {
1256 CheckBox disableAutoFill = (CheckBox) layout.findViewById(
1257 R.id.setup_autofill_dialog_disable_autofill);
1258
1259 if (disableAutoFill.isChecked()) {
1260 // Disable autofill and show a toast with how to turn it on again.
John Reck35e9dd62011-04-25 09:01:54 -07001261 mSettings.setAutofillEnabled(false);
Michael Kolb14612442011-06-24 13:06:29 -07001262 Toast.makeText(mContext,
Ben Murdoch1d676b62011-01-17 12:54:24 +00001263 R.string.autofill_setup_dialog_negative_toast,
1264 Toast.LENGTH_LONG).show();
1265 } else {
1266 // Take user to the AutoFill profile editor. When they return,
1267 // we will send the message that we pass here which will trigger
1268 // the form to get filled out with their new profile.
1269 mWebViewController.setupAutoFill(msg);
1270 }
1271 }
1272 })
1273 .setNegativeButton(R.string.cancel, null)
1274 .show();
Ben Murdoch8029a772010-11-16 11:58:21 +00001275 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001276 };
1277
1278 // -------------------------------------------------------------------------
1279 // WebViewClient implementation for the sub window
1280 // -------------------------------------------------------------------------
1281
1282 // Subclass of WebViewClient used in subwindows to notify the main
1283 // WebViewClient of certain WebView activities.
1284 private static class SubWindowClient extends WebViewClient {
1285 // The main WebViewClient.
1286 private final WebViewClient mClient;
Michael Kolb8233fac2010-10-26 16:08:53 -07001287 private final WebViewController mController;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001288
Michael Kolb8233fac2010-10-26 16:08:53 -07001289 SubWindowClient(WebViewClient client, WebViewController controller) {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001290 mClient = client;
Michael Kolb8233fac2010-10-26 16:08:53 -07001291 mController = controller;
Leon Scroggins III211ba542010-04-19 13:21:13 -04001292 }
1293 @Override
1294 public void onPageStarted(WebView view, String url, Bitmap favicon) {
1295 // Unlike the others, do not call mClient's version, which would
1296 // change the progress bar. However, we do want to remove the
Cary Clark01cfcdd2010-06-04 16:36:45 -04001297 // find or select dialog.
Michael Kolb8233fac2010-10-26 16:08:53 -07001298 mController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001299 }
1300 @Override
1301 public void doUpdateVisitedHistory(WebView view, String url,
1302 boolean isReload) {
1303 mClient.doUpdateVisitedHistory(view, url, isReload);
1304 }
1305 @Override
1306 public boolean shouldOverrideUrlLoading(WebView view, String url) {
1307 return mClient.shouldOverrideUrlLoading(view, url);
1308 }
1309 @Override
1310 public void onReceivedSslError(WebView view, SslErrorHandler handler,
1311 SslError error) {
1312 mClient.onReceivedSslError(view, handler, error);
1313 }
1314 @Override
Brian Carlstrom8862c1d2011-06-02 01:05:55 -07001315 public void onReceivedClientCertRequest(WebView view,
1316 ClientCertRequestHandler handler, String host_and_port) {
1317 mClient.onReceivedClientCertRequest(view, handler, host_and_port);
1318 }
1319 @Override
Grace Kloba22ac16e2009-10-07 18:00:23 -07001320 public void onReceivedHttpAuthRequest(WebView view,
1321 HttpAuthHandler handler, String host, String realm) {
1322 mClient.onReceivedHttpAuthRequest(view, handler, host, realm);
1323 }
1324 @Override
1325 public void onFormResubmission(WebView view, Message dontResend,
1326 Message resend) {
1327 mClient.onFormResubmission(view, dontResend, resend);
1328 }
1329 @Override
1330 public void onReceivedError(WebView view, int errorCode,
1331 String description, String failingUrl) {
1332 mClient.onReceivedError(view, errorCode, description, failingUrl);
1333 }
1334 @Override
1335 public boolean shouldOverrideKeyEvent(WebView view,
1336 android.view.KeyEvent event) {
1337 return mClient.shouldOverrideKeyEvent(view, event);
1338 }
1339 @Override
1340 public void onUnhandledKeyEvent(WebView view,
1341 android.view.KeyEvent event) {
1342 mClient.onUnhandledKeyEvent(view, event);
1343 }
1344 }
1345
1346 // -------------------------------------------------------------------------
1347 // WebChromeClient implementation for the sub window
1348 // -------------------------------------------------------------------------
1349
1350 private class SubWindowChromeClient extends WebChromeClient {
1351 // The main WebChromeClient.
1352 private final WebChromeClient mClient;
1353
1354 SubWindowChromeClient(WebChromeClient client) {
1355 mClient = client;
1356 }
1357 @Override
1358 public void onProgressChanged(WebView view, int newProgress) {
1359 mClient.onProgressChanged(view, newProgress);
1360 }
1361 @Override
1362 public boolean onCreateWindow(WebView view, boolean dialog,
1363 boolean userGesture, android.os.Message resultMsg) {
1364 return mClient.onCreateWindow(view, dialog, userGesture, resultMsg);
1365 }
1366 @Override
1367 public void onCloseWindow(WebView window) {
1368 if (window != mSubView) {
1369 Log.e(LOGTAG, "Can't close the window");
1370 }
Michael Kolb8233fac2010-10-26 16:08:53 -07001371 mWebViewController.dismissSubWindow(Tab.this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001372 }
1373 }
1374
1375 // -------------------------------------------------------------------------
1376
1377 // Construct a new tab
Michael Kolb7bcafde2011-05-09 13:55:59 -07001378 Tab(WebViewController wvcontroller, WebView w) {
John Reck1cf4b792011-07-26 10:22:22 -07001379 this(wvcontroller, w, null);
1380 }
1381
1382 Tab(WebViewController wvcontroller, Bundle state) {
1383 this(wvcontroller, null, state);
1384 }
1385
1386 Tab(WebViewController wvcontroller, WebView w, Bundle state) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001387 mWebViewController = wvcontroller;
Michael Kolb14612442011-06-24 13:06:29 -07001388 mContext = mWebViewController.getContext();
John Reck35e9dd62011-04-25 09:01:54 -07001389 mSettings = BrowserSettings.getInstance();
Michael Kolb14612442011-06-24 13:06:29 -07001390 mDataController = DataController.getInstance(mContext);
1391 mCurrentState = new PageState(mContext, w != null
John Reck847b5322011-04-14 17:02:18 -07001392 ? w.isPrivateBrowsingEnabled() : false);
Michael Kolb8233fac2010-10-26 16:08:53 -07001393 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001394 mInForeground = false;
1395
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001396 mDownloadListener = new DownloadListener() {
1397 public void onDownloadStart(String url, String userAgent,
1398 String contentDisposition, String mimetype,
1399 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001400 mWebViewController.onDownloadStart(Tab.this, url, userAgent, contentDisposition,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001401 mimetype, contentLength);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001402 }
1403 };
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001404 mWebBackForwardListClient = new WebBackForwardListClient() {
1405 @Override
1406 public void onNewHistoryItem(WebHistoryItem item) {
1407 if (isInVoiceSearchMode()) {
1408 item.setCustomData(mVoiceSearchData.mVoiceSearchIntent);
1409 }
Mathew Inwooda829d552011-09-02 14:16:25 +01001410 if (mClearHistoryUrlPattern != null) {
1411 boolean match =
1412 mClearHistoryUrlPattern.matcher(item.getOriginalUrl()).matches();
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001413 if (LOGD_ENABLED) {
Mathew Inwooda829d552011-09-02 14:16:25 +01001414 Log.d(LOGTAG, "onNewHistoryItem: match=" + match + "\n\t"
1415 + item.getUrl() + "\n\t"
1416 + mClearHistoryUrlPattern);
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001417 }
Mathew Inwooda829d552011-09-02 14:16:25 +01001418 if (match) {
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001419 if (mMainView != null) {
1420 mMainView.clearHistory();
1421 }
1422 }
Mathew Inwooda829d552011-09-02 14:16:25 +01001423 mClearHistoryUrlPattern = null;
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001424 }
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001425 }
1426 @Override
1427 public void onIndexChanged(WebHistoryItem item, int index) {
1428 Object data = item.getCustomData();
1429 if (data != null && data instanceof Intent) {
1430 activateVoiceSearchMode((Intent) data);
1431 }
1432 }
1433 };
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001434
John Reck1cf4b792011-07-26 10:22:22 -07001435 mCaptureWidth = mContext.getResources().getDimensionPixelSize(
1436 R.dimen.tab_thumbnail_width);
1437 mCaptureHeight = mContext.getResources().getDimensionPixelSize(
1438 R.dimen.tab_thumbnail_height);
1439 updateShouldCaptureThumbnails();
1440 restoreState(state);
John Reck52be4782011-08-26 15:37:29 -07001441 if (getId() == -1) {
1442 mId = TabControl.getNextId();
1443 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001444 setWebView(w);
Michael Kolb9ef259a2011-07-12 15:33:08 -07001445 mHandler = new Handler() {
Mathew Inwood1dd8e822011-08-03 14:34:29 +01001446 @Override
Michael Kolb9ef259a2011-07-12 15:33:08 -07001447 public void handleMessage(Message m) {
John Reck1cf4b792011-07-26 10:22:22 -07001448 switch (m.what) {
1449 case MSG_CAPTURE:
1450 capture();
1451 break;
1452 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001453 }
1454 };
John Reck1cf4b792011-07-26 10:22:22 -07001455 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07001456
Mathew Inwoode09305e2011-09-02 12:03:26 +01001457 /**
1458 * This is used to get a new ID when the tab has been preloaded, before it is displayed and
1459 * added to TabControl. Preloaded tabs can be created before restoreInstanceState, leading
1460 * to overlapping IDs between the preloaded and restored tabs.
1461 */
1462 public void refreshIdAfterPreload() {
1463 mId = TabControl.getNextId();
1464 }
1465
John Reck1cf4b792011-07-26 10:22:22 -07001466 public void updateShouldCaptureThumbnails() {
1467 if (mWebViewController.shouldCaptureThumbnails()) {
1468 synchronized (Tab.this) {
1469 if (mCapture == null) {
1470 mCapture = Bitmap.createBitmap(mCaptureWidth, mCaptureHeight,
1471 Bitmap.Config.RGB_565);
Michael Kolbc3af0672011-08-09 10:24:41 -07001472 mCapture.eraseColor(Color.WHITE);
John Reck1cf4b792011-07-26 10:22:22 -07001473 if (mInForeground) {
1474 postCapture();
1475 }
1476 }
1477 }
1478 } else {
1479 synchronized (Tab.this) {
1480 mCapture = null;
1481 deleteThumbnail();
1482 }
1483 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001484 }
1485
Michael Kolb14612442011-06-24 13:06:29 -07001486 public void setController(WebViewController ctl) {
1487 mWebViewController = ctl;
John Reck1cf4b792011-07-26 10:22:22 -07001488 updateShouldCaptureThumbnails();
Michael Kolb14612442011-06-24 13:06:29 -07001489 }
1490
Michael Kolbc831b632011-05-11 09:30:34 -07001491 public long getId() {
1492 return mId;
1493 }
1494
Grace Kloba22ac16e2009-10-07 18:00:23 -07001495 /**
1496 * Sets the WebView for this tab, correctly removing the old WebView from
1497 * the container view.
1498 */
1499 void setWebView(WebView w) {
1500 if (mMainView == w) {
1501 return;
1502 }
Michael Kolba713ec82010-11-29 17:27:06 -08001503
Grace Kloba22ac16e2009-10-07 18:00:23 -07001504 // If the WebView is changing, the page will be reloaded, so any ongoing
1505 // Geolocation permission requests are void.
Grace Kloba50c241e2010-04-20 11:07:50 -07001506 if (mGeolocationPermissionsPrompt != null) {
1507 mGeolocationPermissionsPrompt.hide();
1508 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001509
Michael Kolba713ec82010-11-29 17:27:06 -08001510 mWebViewController.onSetWebView(this, w);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001511
John Reck1cf4b792011-07-26 10:22:22 -07001512 if (mMainView != null) {
John Reck8ee633f2011-08-09 16:00:35 -07001513 mMainView.setPictureListener(null);
John Reck1cf4b792011-07-26 10:22:22 -07001514 if (w != null) {
1515 syncCurrentState(w, null);
1516 } else {
1517 mCurrentState = new PageState(mContext, false);
1518 }
1519 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001520 // set the new one
1521 mMainView = w;
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001522 // attach the WebViewClient, WebChromeClient and DownloadListener
Grace Kloba22ac16e2009-10-07 18:00:23 -07001523 if (mMainView != null) {
1524 mMainView.setWebViewClient(mWebViewClient);
1525 mMainView.setWebChromeClient(mWebChromeClient);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001526 // Attach DownloadManager so that downloads can start in an active
1527 // or a non-active window. This can happen when going to a site that
1528 // does a redirect after a period of time. The user could have
1529 // switched to another tab while waiting for the download to start.
1530 mMainView.setDownloadListener(mDownloadListener);
Leon Scroggins0c75a8e2010-03-03 16:40:58 -05001531 mMainView.setWebBackForwardListClient(mWebBackForwardListClient);
John Reck8ee633f2011-08-09 16:00:35 -07001532 TabControl tc = mWebViewController.getTabControl();
1533 if (tc != null && tc.getOnThumbnailUpdatedListener() != null) {
1534 mMainView.setPictureListener(this);
1535 }
John Reck1cf4b792011-07-26 10:22:22 -07001536 if (mSavedState != null) {
John Reck6c2e2f32011-08-22 13:41:23 -07001537 WebBackForwardList restoredState
1538 = mMainView.restoreState(mSavedState);
1539 if (restoredState == null || restoredState.getSize() == 0) {
1540 Log.w(LOGTAG, "Failed to restore WebView state!");
1541 loadUrl(mCurrentState.mOriginalUrl, null);
1542 }
John Reck1cf4b792011-07-26 10:22:22 -07001543 mSavedState = null;
1544 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001545 }
1546 }
1547
1548 /**
1549 * Destroy the tab's main WebView and subWindow if any
1550 */
1551 void destroy() {
1552 if (mMainView != null) {
1553 dismissSubWindow();
John Reckef654f12011-07-12 16:42:08 -07001554 // Make sure the embedded title bar isn't still attached
1555 mMainView.setEmbeddedTitleBar(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001556 // save the WebView to call destroy() after detach it from the tab
1557 WebView webView = mMainView;
1558 setWebView(null);
1559 webView.destroy();
1560 }
1561 }
1562
1563 /**
1564 * Remove the tab from the parent
1565 */
1566 void removeFromTree() {
1567 // detach the children
Michael Kolbc831b632011-05-11 09:30:34 -07001568 if (mChildren != null) {
1569 for(Tab t : mChildren) {
1570 t.setParent(null);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001571 }
1572 }
1573 // remove itself from the parent list
Michael Kolbc831b632011-05-11 09:30:34 -07001574 if (mParent != null) {
1575 mParent.mChildren.remove(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001576 }
John Reck1cf4b792011-07-26 10:22:22 -07001577 deleteThumbnail();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001578 }
1579
1580 /**
1581 * Create a new subwindow unless a subwindow already exists.
1582 * @return True if a new subwindow was created. False if one already exists.
1583 */
1584 boolean createSubWindow() {
1585 if (mSubView == null) {
Michael Kolb1514bb72010-11-22 09:11:48 -08001586 mWebViewController.createSubWindow(this);
Leon Scroggins III211ba542010-04-19 13:21:13 -04001587 mSubView.setWebViewClient(new SubWindowClient(mWebViewClient,
Michael Kolb8233fac2010-10-26 16:08:53 -07001588 mWebViewController));
Grace Kloba22ac16e2009-10-07 18:00:23 -07001589 mSubView.setWebChromeClient(new SubWindowChromeClient(
1590 mWebChromeClient));
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001591 // Set a different DownloadListener for the mSubView, since it will
1592 // just need to dismiss the mSubView, rather than close the Tab
1593 mSubView.setDownloadListener(new DownloadListener() {
1594 public void onDownloadStart(String url, String userAgent,
1595 String contentDisposition, String mimetype,
1596 long contentLength) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001597 mWebViewController.onDownloadStart(Tab.this, url, userAgent,
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001598 contentDisposition, mimetype, contentLength);
1599 if (mSubView.copyBackForwardList().getSize() == 0) {
1600 // This subwindow was opened for the sole purpose of
1601 // downloading a file. Remove it.
Michael Kolb8233fac2010-10-26 16:08:53 -07001602 mWebViewController.dismissSubWindow(Tab.this);
Leon Scrogginsdcc5eeb2010-02-23 17:26:37 -05001603 }
1604 }
1605 });
Michael Kolb14612442011-06-24 13:06:29 -07001606 mSubView.setOnCreateContextMenuListener(mWebViewController.getActivity());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001607 return true;
1608 }
1609 return false;
1610 }
1611
1612 /**
1613 * Dismiss the subWindow for the tab.
1614 */
1615 void dismissSubWindow() {
1616 if (mSubView != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -07001617 mWebViewController.endActionMode();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001618 mSubView.destroy();
1619 mSubView = null;
1620 mSubViewContainer = null;
1621 }
1622 }
1623
Grace Kloba22ac16e2009-10-07 18:00:23 -07001624
1625 /**
1626 * Set the parent tab of this tab.
1627 */
Michael Kolbc831b632011-05-11 09:30:34 -07001628 void setParent(Tab parent) {
John Reck52be4782011-08-26 15:37:29 -07001629 if (parent == this) {
1630 throw new IllegalStateException("Cannot set parent to self!");
1631 }
Michael Kolbc831b632011-05-11 09:30:34 -07001632 mParent = parent;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001633 // This tab may have been freed due to low memory. If that is the case,
Michael Kolbc831b632011-05-11 09:30:34 -07001634 // the parent tab id is already saved. If we are changing that id
Grace Kloba22ac16e2009-10-07 18:00:23 -07001635 // (most likely due to removing the parent tab) we must update the
Michael Kolbc831b632011-05-11 09:30:34 -07001636 // parent tab id in the saved Bundle.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001637 if (mSavedState != null) {
1638 if (parent == null) {
1639 mSavedState.remove(PARENTTAB);
1640 } else {
Michael Kolbc831b632011-05-11 09:30:34 -07001641 mSavedState.putLong(PARENTTAB, parent.getId());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001642 }
1643 }
John Reckb0a86db2011-05-24 14:05:58 -07001644
1645 // Sync the WebView useragent with the parent
1646 if (parent != null && mSettings.hasDesktopUseragent(parent.getWebView())
1647 != mSettings.hasDesktopUseragent(getWebView())) {
1648 mSettings.toggleDesktopUseragent(getWebView());
1649 }
John Reck52be4782011-08-26 15:37:29 -07001650
1651 if (parent != null && parent.getId() == getId()) {
1652 throw new IllegalStateException("Parent has same ID as child!");
1653 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001654 }
1655
1656 /**
Michael Kolbc831b632011-05-11 09:30:34 -07001657 * If this Tab was created through another Tab, then this method returns
1658 * that Tab.
1659 * @return the Tab parent or null
1660 */
1661 public Tab getParent() {
1662 return mParent;
1663 }
1664
1665 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001666 * When a Tab is created through the content of another Tab, then we
1667 * associate the Tabs.
1668 * @param child the Tab that was created from this Tab
1669 */
1670 void addChildTab(Tab child) {
Michael Kolbc831b632011-05-11 09:30:34 -07001671 if (mChildren == null) {
1672 mChildren = new Vector<Tab>();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001673 }
Michael Kolbc831b632011-05-11 09:30:34 -07001674 mChildren.add(child);
1675 child.setParent(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001676 }
1677
Michael Kolbc831b632011-05-11 09:30:34 -07001678 Vector<Tab> getChildren() {
1679 return mChildren;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001680 }
1681
1682 void resume() {
1683 if (mMainView != null) {
John Reck56c1fcf2011-08-17 10:15:16 -07001684 setupHwAcceleration(mMainView);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001685 mMainView.onResume();
1686 if (mSubView != null) {
1687 mSubView.onResume();
1688 }
1689 }
1690 }
1691
John Reck56c1fcf2011-08-17 10:15:16 -07001692 private void setupHwAcceleration(View web) {
1693 if (web == null) return;
1694 BrowserSettings settings = BrowserSettings.getInstance();
1695 if (settings.isHardwareAccelerated()) {
1696 web.setLayerType(View.LAYER_TYPE_NONE, null);
1697 } else {
1698 web.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
1699 }
1700 }
1701
Grace Kloba22ac16e2009-10-07 18:00:23 -07001702 void pause() {
1703 if (mMainView != null) {
1704 mMainView.onPause();
1705 if (mSubView != null) {
1706 mSubView.onPause();
1707 }
1708 }
1709 }
1710
1711 void putInForeground() {
John Reck8ee633f2011-08-09 16:00:35 -07001712 if (mInForeground) {
1713 return;
1714 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001715 mInForeground = true;
1716 resume();
Michael Kolb14612442011-06-24 13:06:29 -07001717 Activity activity = mWebViewController.getActivity();
1718 mMainView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001719 if (mSubView != null) {
Michael Kolb14612442011-06-24 13:06:29 -07001720 mSubView.setOnCreateContextMenuListener(activity);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001721 }
1722 // Show the pending error dialog if the queue is not empty
1723 if (mQueuedErrors != null && mQueuedErrors.size() > 0) {
1724 showError(mQueuedErrors.getFirst());
1725 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05001726 mWebViewController.bookmarkedStatusHasChanged(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001727 }
1728
1729 void putInBackground() {
John Reck8ee633f2011-08-09 16:00:35 -07001730 if (!mInForeground) {
1731 return;
1732 }
John Reck52be4782011-08-26 15:37:29 -07001733 capture();
Grace Kloba22ac16e2009-10-07 18:00:23 -07001734 mInForeground = false;
1735 pause();
1736 mMainView.setOnCreateContextMenuListener(null);
1737 if (mSubView != null) {
1738 mSubView.setOnCreateContextMenuListener(null);
1739 }
1740 }
1741
Michael Kolb8233fac2010-10-26 16:08:53 -07001742 boolean inForeground() {
1743 return mInForeground;
1744 }
1745
Grace Kloba22ac16e2009-10-07 18:00:23 -07001746 /**
1747 * Return the top window of this tab; either the subwindow if it is not
1748 * null or the main window.
1749 * @return The top window of this tab.
1750 */
1751 WebView getTopWindow() {
1752 if (mSubView != null) {
1753 return mSubView;
1754 }
1755 return mMainView;
1756 }
1757
1758 /**
1759 * Return the main window of this tab. Note: if a tab is freed in the
1760 * background, this can return null. It is only guaranteed to be
1761 * non-null for the current tab.
1762 * @return The main WebView of this tab.
1763 */
1764 WebView getWebView() {
1765 return mMainView;
1766 }
1767
Michael Kolba713ec82010-11-29 17:27:06 -08001768 void setViewContainer(View container) {
1769 mContainer = container;
1770 }
1771
Michael Kolb8233fac2010-10-26 16:08:53 -07001772 View getViewContainer() {
1773 return mContainer;
1774 }
1775
Grace Kloba22ac16e2009-10-07 18:00:23 -07001776 /**
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001777 * Return whether private browsing is enabled for the main window of
1778 * this tab.
1779 * @return True if private browsing is enabled.
1780 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001781 boolean isPrivateBrowsingEnabled() {
John Reck502a3532011-08-16 14:21:46 -07001782 return mCurrentState.mIncognito;
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001783 }
1784
1785 /**
Grace Kloba22ac16e2009-10-07 18:00:23 -07001786 * Return the subwindow of this tab or null if there is no subwindow.
1787 * @return The subwindow of this tab or null.
1788 */
1789 WebView getSubWebView() {
1790 return mSubView;
1791 }
1792
Michael Kolb1514bb72010-11-22 09:11:48 -08001793 void setSubWebView(WebView subView) {
1794 mSubView = subView;
1795 }
1796
Michael Kolb8233fac2010-10-26 16:08:53 -07001797 View getSubViewContainer() {
1798 return mSubViewContainer;
1799 }
1800
Michael Kolb1514bb72010-11-22 09:11:48 -08001801 void setSubViewContainer(View subViewContainer) {
1802 mSubViewContainer = subViewContainer;
1803 }
1804
Grace Kloba22ac16e2009-10-07 18:00:23 -07001805 /**
1806 * @return The geolocation permissions prompt for this tab.
1807 */
1808 GeolocationPermissionsPrompt getGeolocationPermissionsPrompt() {
Grace Kloba50c241e2010-04-20 11:07:50 -07001809 if (mGeolocationPermissionsPrompt == null) {
1810 ViewStub stub = (ViewStub) mContainer
1811 .findViewById(R.id.geolocation_permissions_prompt);
1812 mGeolocationPermissionsPrompt = (GeolocationPermissionsPrompt) stub
1813 .inflate();
Grace Kloba50c241e2010-04-20 11:07:50 -07001814 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001815 return mGeolocationPermissionsPrompt;
1816 }
1817
1818 /**
1819 * @return The application id string
1820 */
1821 String getAppId() {
1822 return mAppId;
1823 }
1824
1825 /**
1826 * Set the application id string
1827 * @param id
1828 */
1829 void setAppId(String id) {
1830 mAppId = id;
1831 }
1832
Michael Kolbe28b3472011-08-04 16:54:31 -07001833 boolean closeOnBack() {
1834 return mCloseOnBack;
1835 }
1836
1837 void setCloseOnBack(boolean close) {
1838 mCloseOnBack = close;
1839 }
1840
Grace Kloba22ac16e2009-10-07 18:00:23 -07001841 String getUrl() {
John Reck324d4402011-01-11 16:56:42 -08001842 return UrlUtils.filteredUrl(mCurrentState.mUrl);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001843 }
1844
John Reck49a603c2011-03-03 09:33:05 -08001845 String getOriginalUrl() {
John Reckdb22ec42011-06-29 11:31:24 -07001846 if (mCurrentState.mOriginalUrl == null) {
1847 return getUrl();
John Reck49a603c2011-03-03 09:33:05 -08001848 }
John Reckdb22ec42011-06-29 11:31:24 -07001849 return UrlUtils.filteredUrl(mCurrentState.mOriginalUrl);
John Reck49a603c2011-03-03 09:33:05 -08001850 }
1851
Grace Kloba22ac16e2009-10-07 18:00:23 -07001852 /**
John Reck30c714c2010-12-16 17:30:34 -08001853 * Get the title of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001854 */
1855 String getTitle() {
John Reck30c714c2010-12-16 17:30:34 -08001856 if (mCurrentState.mTitle == null && mInPageLoad) {
Michael Kolb14612442011-06-24 13:06:29 -07001857 return mContext.getString(R.string.title_bar_loading);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001858 }
John Reck30c714c2010-12-16 17:30:34 -08001859 return mCurrentState.mTitle;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001860 }
1861
1862 /**
John Reck30c714c2010-12-16 17:30:34 -08001863 * Get the favicon of this tab.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001864 */
1865 Bitmap getFavicon() {
John Reck1cf4b792011-07-26 10:22:22 -07001866 if (mCurrentState.mFavicon != null) {
1867 return mCurrentState.mFavicon;
1868 }
1869 return getDefaultFavicon(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001870 }
1871
John Recke969cc52010-12-21 17:24:43 -08001872 public boolean isBookmarkedSite() {
1873 return mCurrentState.mIsBookmarkedSite;
1874 }
Rob Tsukf8bdfce2010-10-07 15:41:16 -07001875
Grace Kloba22ac16e2009-10-07 18:00:23 -07001876 /**
1877 * Return the tab's error console. Creates the console if createIfNEcessary
1878 * is true and we haven't already created the console.
1879 * @param createIfNecessary Flag to indicate if the console should be
1880 * created if it has not been already.
1881 * @return The tab's error console, or null if one has not been created and
1882 * createIfNecessary is false.
1883 */
1884 ErrorConsoleView getErrorConsole(boolean createIfNecessary) {
1885 if (createIfNecessary && mErrorConsole == null) {
Michael Kolb14612442011-06-24 13:06:29 -07001886 mErrorConsole = new ErrorConsoleView(mContext);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001887 mErrorConsole.setWebView(mMainView);
1888 }
1889 return mErrorConsole;
1890 }
1891
Steve Block2466eff2011-10-03 15:33:09 +01001892 private void setSecurityState(SecurityState securityState) {
1893 mCurrentState.mSecurityState = securityState;
1894 mWebViewController.onUpdatedSecurityState(this);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001895 }
1896
1897 /**
Steve Block2466eff2011-10-03 15:33:09 +01001898 * @return The tab's security state.
Grace Kloba22ac16e2009-10-07 18:00:23 -07001899 */
Steve Block2466eff2011-10-03 15:33:09 +01001900 SecurityState getSecurityState() {
1901 return mCurrentState.mSecurityState;
John Reck30c714c2010-12-16 17:30:34 -08001902 }
1903
1904 int getLoadProgress() {
1905 if (mInPageLoad) {
1906 return mPageLoadProgress;
1907 }
1908 return 100;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001909 }
1910
1911 /**
1912 * @return TRUE if onPageStarted is called while onPageFinished is not
1913 * called yet.
1914 */
Michael Kolb8233fac2010-10-26 16:08:53 -07001915 boolean inPageLoad() {
1916 return mInPageLoad;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001917 }
1918
1919 // force mInLoad to be false. This should only be called before closing the
1920 // tab to ensure BrowserActivity's pauseWebViewTimers() is called correctly.
Michael Kolb8233fac2010-10-26 16:08:53 -07001921 void clearInPageLoad() {
1922 mInPageLoad = false;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001923 }
1924
Grace Kloba22ac16e2009-10-07 18:00:23 -07001925 /**
John Reck1cf4b792011-07-26 10:22:22 -07001926 * @return The Bundle with the tab's state if it can be saved, otherwise null
Grace Kloba22ac16e2009-10-07 18:00:23 -07001927 */
John Reck1cf4b792011-07-26 10:22:22 -07001928 public Bundle saveState() {
Grace Kloba22ac16e2009-10-07 18:00:23 -07001929 // If the WebView is null it means we ran low on memory and we already
1930 // stored the saved state in mSavedState.
1931 if (mMainView == null) {
John Reck1cf4b792011-07-26 10:22:22 -07001932 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001933 }
John Reck6c2e2f32011-08-22 13:41:23 -07001934
1935 if (TextUtils.isEmpty(mCurrentState.mUrl)) {
John Reck1cf4b792011-07-26 10:22:22 -07001936 return null;
John Reck24f18262011-06-17 14:47:20 -07001937 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001938
1939 mSavedState = new Bundle();
John Reck6c2e2f32011-08-22 13:41:23 -07001940 WebBackForwardList savedList = mMainView.saveState(mSavedState);
1941 if (savedList == null || savedList.getSize() == 0) {
1942 Log.w(LOGTAG, "Failed to save back/forward list for "
1943 + mCurrentState.mUrl);
1944 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001945
Michael Kolbc831b632011-05-11 09:30:34 -07001946 mSavedState.putLong(ID, mId);
John Reck30c714c2010-12-16 17:30:34 -08001947 mSavedState.putString(CURRURL, mCurrentState.mUrl);
1948 mSavedState.putString(CURRTITLE, mCurrentState.mTitle);
John Reck1cf4b792011-07-26 10:22:22 -07001949 mSavedState.putBoolean(INCOGNITO, mMainView.isPrivateBrowsingEnabled());
Grace Kloba22ac16e2009-10-07 18:00:23 -07001950 if (mAppId != null) {
1951 mSavedState.putString(APPID, mAppId);
1952 }
Michael Kolbe28b3472011-08-04 16:54:31 -07001953 mSavedState.putBoolean(CLOSEFLAG, mCloseOnBack);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001954 // Remember the parent tab so the relationship can be restored.
Michael Kolbc831b632011-05-11 09:30:34 -07001955 if (mParent != null) {
1956 mSavedState.putLong(PARENTTAB, mParent.mId);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001957 }
John Reckb0a86db2011-05-24 14:05:58 -07001958 mSavedState.putBoolean(USERAGENT,
1959 mSettings.hasDesktopUseragent(getWebView()));
John Reck1cf4b792011-07-26 10:22:22 -07001960 return mSavedState;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001961 }
1962
1963 /*
1964 * Restore the state of the tab.
1965 */
John Reck1cf4b792011-07-26 10:22:22 -07001966 private void restoreState(Bundle b) {
1967 mSavedState = b;
1968 if (mSavedState == null) {
1969 return;
Grace Kloba22ac16e2009-10-07 18:00:23 -07001970 }
1971 // Restore the internal state even if the WebView fails to restore.
1972 // This will maintain the app id, original url and close-on-exit values.
Michael Kolbc831b632011-05-11 09:30:34 -07001973 mId = b.getLong(ID);
Grace Kloba22ac16e2009-10-07 18:00:23 -07001974 mAppId = b.getString(APPID);
Michael Kolbe28b3472011-08-04 16:54:31 -07001975 mCloseOnBack = b.getBoolean(CLOSEFLAG);
John Reckb0a86db2011-05-24 14:05:58 -07001976 if (b.getBoolean(USERAGENT)
1977 != mSettings.hasDesktopUseragent(getWebView())) {
1978 mSettings.toggleDesktopUseragent(getWebView());
1979 }
John Reck1cf4b792011-07-26 10:22:22 -07001980 String url = b.getString(CURRURL);
1981 String title = b.getString(CURRTITLE);
1982 boolean incognito = b.getBoolean(INCOGNITO);
1983 mCurrentState = new PageState(mContext, incognito, url, null);
1984 mCurrentState.mTitle = title;
1985 synchronized (Tab.this) {
1986 if (mCapture != null) {
1987 BackgroundHandler.execute(mLoadThumbnail);
1988 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001989 }
Grace Kloba22ac16e2009-10-07 18:00:23 -07001990 }
Leon Scroggins III211ba542010-04-19 13:21:13 -04001991
Leon Scroggins1961ed22010-12-07 15:22:21 -05001992 public void updateBookmarkedStatus() {
John Recke969cc52010-12-21 17:24:43 -08001993 mDataController.queryBookmarkStatus(getUrl(), mIsBookmarkCallback);
Leon Scroggins1961ed22010-12-07 15:22:21 -05001994 }
1995
John Recke969cc52010-12-21 17:24:43 -08001996 private DataController.OnQueryUrlIsBookmark mIsBookmarkCallback
1997 = new DataController.OnQueryUrlIsBookmark() {
1998 @Override
1999 public void onQueryUrlIsBookmark(String url, boolean isBookmark) {
2000 if (mCurrentState.mUrl.equals(url)) {
2001 mCurrentState.mIsBookmarkedSite = isBookmark;
2002 mWebViewController.bookmarkedStatusHasChanged(Tab.this);
2003 }
Leon Scroggins1961ed22010-12-07 15:22:21 -05002004 }
John Recke969cc52010-12-21 17:24:43 -08002005 };
Michael Kolb1acef692011-03-08 14:12:06 -08002006
Michael Kolbeb95db42011-03-03 10:38:40 -08002007 public Bitmap getScreenshot() {
John Reck1cf4b792011-07-26 10:22:22 -07002008 synchronized (Tab.this) {
2009 return mCapture;
2010 }
Michael Kolbeb95db42011-03-03 10:38:40 -08002011 }
2012
John Reck541f55a2011-06-07 16:34:43 -07002013 public boolean isSnapshot() {
John Reck541f55a2011-06-07 16:34:43 -07002014 return false;
2015 }
2016
John Reckd8c74522011-06-14 08:45:00 -07002017 public ContentValues createSnapshotValues() {
2018 if (mMainView == null) return null;
John Reck9d2718e2011-10-05 17:10:17 -07002019 SnapshotByteArrayOutputStream bos = new SnapshotByteArrayOutputStream();
John Reck8cc92352011-07-06 17:41:52 -07002020 try {
2021 GZIPOutputStream stream = new GZIPOutputStream(bos);
2022 if (!mMainView.saveViewState(stream)) {
2023 return null;
2024 }
2025 stream.flush();
2026 stream.close();
2027 } catch (Exception e) {
2028 Log.w(LOGTAG, "Failed to save view state", e);
John Reck541f55a2011-06-07 16:34:43 -07002029 return null;
2030 }
John Reck8cc92352011-07-06 17:41:52 -07002031 byte[] data = bos.toByteArray();
John Reckd8c74522011-06-14 08:45:00 -07002032 ContentValues values = new ContentValues();
2033 values.put(Snapshots.TITLE, mCurrentState.mTitle);
2034 values.put(Snapshots.URL, mCurrentState.mUrl);
2035 values.put(Snapshots.VIEWSTATE, data);
2036 values.put(Snapshots.BACKGROUND, mMainView.getPageBackgroundColor());
John Reck8cc92352011-07-06 17:41:52 -07002037 values.put(Snapshots.DATE_CREATED, System.currentTimeMillis());
2038 values.put(Snapshots.FAVICON, compressBitmap(getFavicon()));
2039 Bitmap screenshot = Controller.createScreenshot(mMainView,
2040 Controller.getDesiredThumbnailWidth(mContext),
2041 Controller.getDesiredThumbnailHeight(mContext));
2042 values.put(Snapshots.THUMBNAIL, compressBitmap(screenshot));
John Reckd8c74522011-06-14 08:45:00 -07002043 return values;
John Reck541f55a2011-06-07 16:34:43 -07002044 }
2045
John Reck8cc92352011-07-06 17:41:52 -07002046 public byte[] compressBitmap(Bitmap bitmap) {
2047 if (bitmap == null) {
2048 return null;
2049 }
2050 ByteArrayOutputStream stream = new ByteArrayOutputStream();
2051 bitmap.compress(CompressFormat.PNG, 100, stream);
2052 return stream.toByteArray();
2053 }
2054
John Reck26b18322011-06-21 13:08:58 -07002055 public void loadUrl(String url, Map<String, String> headers) {
2056 if (mMainView != null) {
Michael Kolb14612442011-06-24 13:06:29 -07002057 mCurrentState = new PageState(mContext, false, url, null);
John Reck26b18322011-06-21 13:08:58 -07002058 mWebViewController.onPageStarted(this, mMainView, null);
2059 mMainView.loadUrl(url, headers);
2060 }
2061 }
2062
Michael Kolb9ef259a2011-07-12 15:33:08 -07002063 protected void capture() {
2064 if (mMainView == null || mCapture == null) return;
2065 Canvas c = new Canvas(mCapture);
2066 final int left = mMainView.getScrollX();
2067 final int top = mMainView.getScrollY() + mMainView.getVisibleTitleHeight();
Michael Kolba3194d02011-09-07 11:23:51 -07002068 int state = c.save();
Michael Kolb9ef259a2011-07-12 15:33:08 -07002069 c.translate(-left, -top);
2070 float scale = mCaptureWidth / (float) mMainView.getWidth();
2071 c.scale(scale, scale, left, top);
John Reck8ee633f2011-08-09 16:00:35 -07002072 if (mMainView instanceof BrowserWebView) {
2073 ((BrowserWebView)mMainView).drawContent(c);
2074 } else {
2075 mMainView.draw(c);
2076 }
Michael Kolba3194d02011-09-07 11:23:51 -07002077 c.restoreToCount(state);
2078 // manually anti-alias the edges for the tilt
2079 c.drawRect(0, 0, 1, mCapture.getHeight(), sAlphaPaint);
2080 c.drawRect(mCapture.getWidth() - 1, 0, mCapture.getWidth(),
2081 mCapture.getHeight(), sAlphaPaint);
2082 c.drawRect(0, 0, mCapture.getWidth(), 1, sAlphaPaint);
2083 c.drawRect(0, mCapture.getHeight() - 1, mCapture.getWidth(),
2084 mCapture.getHeight(), sAlphaPaint);
Dianne Hackborn43cfe8a2011-08-02 16:59:35 -07002085 c.setBitmap(null);
John Reck8ee633f2011-08-09 16:00:35 -07002086 mHandler.removeMessages(MSG_CAPTURE);
John Reck1cf4b792011-07-26 10:22:22 -07002087 persistThumbnail();
John Reck8ee633f2011-08-09 16:00:35 -07002088 TabControl tc = mWebViewController.getTabControl();
2089 if (tc != null) {
2090 OnThumbnailUpdatedListener updateListener
2091 = tc.getOnThumbnailUpdatedListener();
2092 if (updateListener != null) {
2093 updateListener.onThumbnailUpdated(this);
2094 }
2095 }
Michael Kolb9ef259a2011-07-12 15:33:08 -07002096 }
2097
2098 @Override
2099 public void onNewPicture(WebView view, Picture picture) {
2100 //update screenshot
John Reck1cf4b792011-07-26 10:22:22 -07002101 postCapture();
2102 }
2103
2104 private void postCapture() {
Michael Kolb9ef259a2011-07-12 15:33:08 -07002105 if (!mHandler.hasMessages(MSG_CAPTURE)) {
2106 mHandler.sendEmptyMessageDelayed(MSG_CAPTURE, CAPTURE_DELAY);
2107 }
2108 }
2109
John Reckef654f12011-07-12 16:42:08 -07002110 public boolean canGoBack() {
2111 return mMainView != null ? mMainView.canGoBack() : false;
2112 }
2113
2114 public boolean canGoForward() {
2115 return mMainView != null ? mMainView.canGoForward() : false;
2116 }
2117
2118 public void goBack() {
2119 if (mMainView != null) {
2120 mMainView.goBack();
2121 }
2122 }
2123
2124 public void goForward() {
2125 if (mMainView != null) {
2126 mMainView.goForward();
2127 }
2128 }
2129
Mathew Inwood1dd8e822011-08-03 14:34:29 +01002130 /**
2131 * Causes the tab back/forward stack to be cleared once, if the given URL is the next URL
2132 * to be added to the stack.
2133 *
2134 * This is used to ensure that preloaded URLs that are not subsequently seen by the user do
2135 * not appear in the back stack.
2136 */
Mathew Inwooda829d552011-09-02 14:16:25 +01002137 public void clearBackStackWhenItemAdded(Pattern urlPattern) {
2138 mClearHistoryUrlPattern = urlPattern;
Mathew Inwood1dd8e822011-08-03 14:34:29 +01002139 }
2140
John Reck1cf4b792011-07-26 10:22:22 -07002141 protected void persistThumbnail() {
2142 BackgroundHandler.execute(mSaveThumbnail);
2143 }
2144
2145 protected void deleteThumbnail() {
2146 BackgroundHandler.execute(mDeleteThumbnail);
2147 }
2148
2149 private void updateCaptureFromBlob(byte[] blob) {
2150 synchronized (Tab.this) {
2151 if (mCapture == null) {
2152 return;
2153 }
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002154 ByteBuffer buffer = ByteBuffer.wrap(blob);
Michael Kolbd837a112011-08-09 14:00:18 -07002155 try {
2156 mCapture.copyPixelsFromBuffer(buffer);
2157 } catch (RuntimeException rex) {
2158 Log.e(LOGTAG, "Load capture has mismatched sizes; buffer: "
2159 + buffer.capacity() + " blob: " + blob.length
2160 + "capture: " + mCapture.getByteCount());
2161 throw rex;
Michael Kolbbd4c00a2011-08-02 11:27:12 -07002162 }
John Reck1cf4b792011-07-26 10:22:22 -07002163 }
2164 }
2165
2166 private byte[] getCaptureBlob() {
2167 synchronized (Tab.this) {
2168 if (mCapture == null) {
2169 return null;
2170 }
2171 ByteBuffer buffer = ByteBuffer.allocate(mCapture.getByteCount());
2172 mCapture.copyPixelsToBuffer(buffer);
2173 return buffer.array();
2174 }
2175 }
2176
2177 private Runnable mSaveThumbnail = new Runnable() {
2178
2179 @Override
2180 public void run() {
2181 byte[] blob = getCaptureBlob();
2182 if (blob == null) {
2183 return;
2184 }
2185 ContentResolver cr = mContext.getContentResolver();
2186 ContentValues values = new ContentValues();
2187 values.put(Thumbnails._ID, mId);
2188 values.put(Thumbnails.THUMBNAIL, blob);
2189 cr.insert(Thumbnails.CONTENT_URI, values);
2190 }
2191 };
2192
2193 private Runnable mDeleteThumbnail = new Runnable() {
2194
2195 @Override
2196 public void run() {
2197 ContentResolver cr = mContext.getContentResolver();
2198 try {
2199 cr.delete(ContentUris.withAppendedId(Thumbnails.CONTENT_URI, mId),
2200 null, null);
2201 } catch (Throwable t) {}
2202 }
2203 };
2204
2205 private Runnable mLoadThumbnail = new Runnable() {
2206
2207 @Override
2208 public void run() {
2209 ContentResolver cr = mContext.getContentResolver();
2210 Cursor c = null;
2211 try {
2212 Uri uri = ContentUris.withAppendedId(Thumbnails.CONTENT_URI, mId);
2213 c = cr.query(uri, new String[] {Thumbnails._ID,
2214 Thumbnails.THUMBNAIL}, null, null, null);
2215 if (c.moveToFirst()) {
2216 byte[] data = c.getBlob(1);
2217 if (data != null && data.length > 0) {
2218 updateCaptureFromBlob(data);
2219 }
2220 }
2221 } finally {
2222 if (c != null) {
2223 c.close();
2224 }
2225 }
2226 }
2227 };
2228
John Reck52be4782011-08-26 15:37:29 -07002229 @Override
2230 public String toString() {
2231 StringBuilder builder = new StringBuilder(100);
2232 builder.append(mId);
2233 builder.append(") has parent: ");
2234 if (getParent() != null) {
2235 builder.append("true[");
2236 builder.append(getParent().getId());
2237 builder.append("]");
2238 } else {
2239 builder.append("false");
2240 }
2241 builder.append(", incog: ");
2242 builder.append(isPrivateBrowsingEnabled());
2243 if (!isPrivateBrowsingEnabled()) {
2244 builder.append(", title: ");
2245 builder.append(getTitle());
2246 builder.append(", url: ");
2247 builder.append(getUrl());
2248 }
2249 return builder.toString();
2250 }
2251
Grace Kloba22ac16e2009-10-07 18:00:23 -07002252}