blob: 686ed7b90d4cec4aefaffb1f00e6d4b8a1e6feca [file] [log] [blame]
Michael Kolb8233fac2010-10-26 16:08:53 -07001/*
2 * Copyright (C) 2010 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
19import android.app.Activity;
20import android.content.ActivityNotFoundException;
21import android.content.Intent;
John Reck95a49ff2011-02-08 18:23:22 -080022import android.content.IntentFilter;
23import android.content.pm.ActivityInfo;
Michael Kolb8233fac2010-10-26 16:08:53 -070024import android.content.pm.PackageManager;
John Reck95a49ff2011-02-08 18:23:22 -080025import android.content.pm.ResolveInfo;
Michael Kolb8233fac2010-10-26 16:08:53 -070026import android.database.Cursor;
27import android.net.Uri;
Ben Murdocha941f6e2010-12-07 16:09:16 +000028import android.os.AsyncTask;
Michael Kolb8233fac2010-10-26 16:08:53 -070029import android.util.Log;
30import android.webkit.WebView;
31
32import java.net.URISyntaxException;
John Reck95a49ff2011-02-08 18:23:22 -080033import java.util.List;
Michael Kolb8233fac2010-10-26 16:08:53 -070034
35/**
36 *
37 */
38public class UrlHandler {
39
40 // Use in overrideUrlLoading
41 /* package */ final static String SCHEME_WTAI = "wtai://wp/";
42 /* package */ final static String SCHEME_WTAI_MC = "wtai://wp/mc;";
43 /* package */ final static String SCHEME_WTAI_SD = "wtai://wp/sd;";
44 /* package */ final static String SCHEME_WTAI_AP = "wtai://wp/ap;";
45
46 Controller mController;
47 Activity mActivity;
48
49 private Boolean mIsProviderPresent = null;
50 private Uri mRlzUri = null;
51
52 public UrlHandler(Controller controller) {
53 mController = controller;
54 mActivity = mController.getActivity();
55 }
56
Michael Kolb18eb3772010-12-10 14:29:51 -080057 boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) {
Michael Kolb8233fac2010-10-26 16:08:53 -070058 if (view.isPrivateBrowsingEnabled()) {
59 // Don't allow urls to leave the browser app when in
60 // private browsing mode
Patrick Scottd05faa62010-12-16 09:15:34 -050061 return false;
Michael Kolb8233fac2010-10-26 16:08:53 -070062 }
63
64 if (url.startsWith(SCHEME_WTAI)) {
65 // wtai://wp/mc;number
66 // number=string(phone-number)
67 if (url.startsWith(SCHEME_WTAI_MC)) {
68 Intent intent = new Intent(Intent.ACTION_VIEW,
69 Uri.parse(WebView.SCHEME_TEL +
70 url.substring(SCHEME_WTAI_MC.length())));
71 mActivity.startActivity(intent);
72 // before leaving BrowserActivity, close the empty child tab.
73 // If a new tab is created through JavaScript open to load this
74 // url, we would like to close it as we will load this url in a
75 // different Activity.
76 mController.closeEmptyChildTab();
77 return true;
78 }
79 // wtai://wp/sd;dtmf
80 // dtmf=string(dialstring)
81 if (url.startsWith(SCHEME_WTAI_SD)) {
82 // TODO: only send when there is active voice connection
83 return false;
84 }
85 // wtai://wp/ap;number;name
86 // number=string(phone-number)
87 // name=string
88 if (url.startsWith(SCHEME_WTAI_AP)) {
89 // TODO
90 return false;
91 }
92 }
93
94 // The "about:" schemes are internal to the browser; don't want these to
95 // be dispatched to other apps.
96 if (url.startsWith("about:")) {
97 return false;
98 }
99
100 // If this is a Google search, attempt to add an RLZ string
101 // (if one isn't already present).
102 if (rlzProviderPresent()) {
103 Uri siteUri = Uri.parse(url);
104 if (needsRlzString(siteUri)) {
Ben Murdocha941f6e2010-12-07 16:09:16 +0000105 // Need to look up the RLZ info from a database, so do it in an
106 // AsyncTask. Although we are not overriding the URL load synchronously,
107 // we guarantee that we will handle this URL load after the task executes,
108 // so it's safe to just return true to WebCore now to stop its own loading.
Russell Brenner14e1ae22011-01-12 14:54:23 -0800109 new RLZTask(tab, siteUri, view).execute();
Michael Kolb8233fac2010-10-26 16:08:53 -0700110 return true;
111 }
112 }
113
Russell Brennerd4afde12011-01-07 11:09:36 -0800114 if (startActivityForUrl(url)) {
115 return true;
Michael Kolb8233fac2010-10-26 16:08:53 -0700116 }
117
Russell Brenner14e1ae22011-01-12 14:54:23 -0800118 if (handleMenuClick(tab, url)) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700119 return true;
120 }
Russell Brennerd4afde12011-01-07 11:09:36 -0800121
Michael Kolb8233fac2010-10-26 16:08:53 -0700122 return false;
123 }
124
Russell Brennerca9898e2011-01-21 13:34:02 -0800125 boolean startActivityForUrl(String url) {
Russell Brennerd4afde12011-01-07 11:09:36 -0800126 Intent intent;
127 // perform generic parsing of the URI to turn it into an Intent.
128 try {
129 intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
130 } catch (URISyntaxException ex) {
131 Log.w("Browser", "Bad URI " + url + ": " + ex.getMessage());
132 return false;
133 }
134
135 // check whether the intent can be resolved. If not, we will see
136 // whether we can download it from the Market.
137 if (mActivity.getPackageManager().resolveActivity(intent, 0) == null) {
138 String packagename = intent.getPackage();
139 if (packagename != null) {
140 intent = new Intent(Intent.ACTION_VIEW, Uri
141 .parse("market://search?q=pname:" + packagename));
142 intent.addCategory(Intent.CATEGORY_BROWSABLE);
143 mActivity.startActivity(intent);
144 // before leaving BrowserActivity, close the empty child tab.
145 // If a new tab is created through JavaScript open to load this
146 // url, we would like to close it as we will load this url in a
147 // different Activity.
148 mController.closeEmptyChildTab();
149 return true;
150 } else {
151 return false;
152 }
153 }
154
155 // sanitize the Intent, ensuring web pages can not bypass browser
156 // security (only access to BROWSABLE activities).
157 intent.addCategory(Intent.CATEGORY_BROWSABLE);
158 intent.setComponent(null);
John Reck95a49ff2011-02-08 18:23:22 -0800159 if (!isSpecializedHandlerAvailable(intent)) {
160 return false;
161 }
Russell Brennerd4afde12011-01-07 11:09:36 -0800162 try {
163 if (mActivity.startActivityIfNeeded(intent, -1)) {
164 // before leaving BrowserActivity, close the empty child tab.
165 // If a new tab is created through JavaScript open to load this
166 // url, we would like to close it as we will load this url in a
167 // different Activity.
168 mController.closeEmptyChildTab();
169 return true;
170 }
171 } catch (ActivityNotFoundException ex) {
172 // ignore the error. If no application can handle the URL,
173 // eg about:blank, assume the browser can handle it.
174 }
175
176 return false;
177 }
178
John Reck95a49ff2011-02-08 18:23:22 -0800179 /**
180 * Search for intent handlers that are specific to this URL
181 * aka, specialized apps like google maps or youtube
182 */
183 private boolean isSpecializedHandlerAvailable(Intent intent) {
184 PackageManager pm = mActivity.getPackageManager();
185 List<ResolveInfo> handlers = pm.queryIntentActivities(intent,
186 PackageManager.GET_RESOLVED_FILTER);
187 if (handlers == null || handlers.size() == 0) {
188 return false;
189 }
190 for (ResolveInfo resolveInfo : handlers) {
191 IntentFilter filter = resolveInfo.filter;
192 if (filter == null) {
193 // No intent filter matches this intent?
194 // Error on the side of staying in the browser, ignore
195 continue;
196 }
197 if (filter.countDataAuthorities() == 0 || filter.countDataPaths() == 0) {
198 // Generic handler, skip
199 continue;
200 }
201 return true;
202 }
203 return false;
204 }
205
Russell Brenner14e1ae22011-01-12 14:54:23 -0800206 // In case a physical keyboard is attached, handle clicks with the menu key
207 // depressed by opening in a new tab
Russell Brennerca9898e2011-01-21 13:34:02 -0800208 boolean handleMenuClick(Tab tab, String url) {
Russell Brenner14e1ae22011-01-12 14:54:23 -0800209 if (mController.isMenuDown()) {
210 mController.openTab(tab, url, false);
211 mActivity.closeOptionsMenu();
212 return true;
213 }
214
215 return false;
216 }
217
Russell Brennerca9898e2011-01-21 13:34:02 -0800218 // TODO: Move this class into Tab, where it can be properly stopped upon
219 // closure of the tab
Ben Murdocha941f6e2010-12-07 16:09:16 +0000220 private class RLZTask extends AsyncTask<Void, Void, String> {
Russell Brenner14e1ae22011-01-12 14:54:23 -0800221 private Tab mTab;
Ben Murdocha941f6e2010-12-07 16:09:16 +0000222 private Uri mSiteUri;
223 private WebView mWebView;
224
Russell Brenner14e1ae22011-01-12 14:54:23 -0800225 public RLZTask(Tab tab, Uri uri, WebView webView) {
226 mTab = tab;
Ben Murdocha941f6e2010-12-07 16:09:16 +0000227 mSiteUri = uri;
228 mWebView = webView;
229 }
230
231 protected String doInBackground(Void... unused) {
232 String result = mSiteUri.toString();
233 Cursor cur = null;
234 try {
235 cur = mActivity.getContentResolver()
236 .query(getRlzUri(), null, null, null, null);
237 if (cur != null && cur.moveToFirst() && !cur.isNull(0)) {
238 result = mSiteUri.buildUpon()
239 .appendQueryParameter("rlz", cur.getString(0))
240 .build().toString();
241 }
242 } finally {
243 if (cur != null) {
244 cur.close();
245 }
246 }
247 return result;
248 }
249
250 protected void onPostExecute(String result) {
Russell Brennerca9898e2011-01-21 13:34:02 -0800251 // Make sure the Tab was not closed while handling the task
252 if (mController.getTabControl().getTabIndex(mTab) != -1) {
253 // If the Activity Manager is not invoked, load the URL directly
254 if (!startActivityForUrl(result)) {
255 if (!handleMenuClick(mTab, result)) {
256 mController.loadUrl(mWebView, result);
257 }
Russell Brenner14e1ae22011-01-12 14:54:23 -0800258 }
259 }
Ben Murdocha941f6e2010-12-07 16:09:16 +0000260 }
261 }
262
Michael Kolb8233fac2010-10-26 16:08:53 -0700263 // Determine whether the RLZ provider is present on the system.
264 private boolean rlzProviderPresent() {
265 if (mIsProviderPresent == null) {
266 PackageManager pm = mActivity.getPackageManager();
267 mIsProviderPresent = pm.resolveContentProvider(
268 BrowserSettings.RLZ_PROVIDER, 0) != null;
269 }
270 return mIsProviderPresent;
271 }
272
273 // Retrieve the RLZ access point string and cache the URI used to
274 // retrieve RLZ values.
275 private Uri getRlzUri() {
276 if (mRlzUri == null) {
277 String ap = mActivity.getResources()
278 .getString(R.string.rlz_access_point);
279 mRlzUri = Uri.withAppendedPath(BrowserSettings.RLZ_PROVIDER_URI, ap);
280 }
281 return mRlzUri;
282 }
283
284 // Determine if this URI appears to be for a Google search
285 // and does not have an RLZ parameter.
286 // Taken largely from Chrome source, src/chrome/browser/google_url_tracker.cc
287 private static boolean needsRlzString(Uri uri) {
288 String scheme = uri.getScheme();
289 if (("http".equals(scheme) || "https".equals(scheme)) &&
290 (uri.getQueryParameter("q") != null) &&
291 (uri.getQueryParameter("rlz") == null)) {
292 String host = uri.getHost();
293 if (host == null) {
294 return false;
295 }
296 String[] hostComponents = host.split("\\.");
297
298 if (hostComponents.length < 2) {
299 return false;
300 }
301 int googleComponent = hostComponents.length - 2;
302 String component = hostComponents[googleComponent];
303 if (!"google".equals(component)) {
304 if (hostComponents.length < 3 ||
305 (!"co".equals(component) && !"com".equals(component))) {
306 return false;
307 }
308 googleComponent = hostComponents.length - 3;
309 if (!"google".equals(hostComponents[googleComponent])) {
310 return false;
311 }
312 }
313
314 // Google corp network handling.
315 if (googleComponent > 0 && "corp".equals(
316 hostComponents[googleComponent - 1])) {
317 return false;
318 }
319
320 return true;
321 }
322 return false;
323 }
324
325}