Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.browser; |
| 18 | |
| 19 | import android.app.Activity; |
| 20 | import android.content.ActivityNotFoundException; |
| 21 | import android.content.Intent; |
John Reck | 95a49ff | 2011-02-08 18:23:22 -0800 | [diff] [blame] | 22 | import android.content.IntentFilter; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 23 | import android.content.pm.PackageManager; |
John Reck | 95a49ff | 2011-02-08 18:23:22 -0800 | [diff] [blame] | 24 | import android.content.pm.ResolveInfo; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 25 | import android.database.Cursor; |
| 26 | import android.net.Uri; |
Ben Murdoch | a941f6e | 2010-12-07 16:09:16 +0000 | [diff] [blame] | 27 | import android.os.AsyncTask; |
kaiyiz | c4ada32 | 2013-07-30 09:58:07 +0800 | [diff] [blame] | 28 | import android.os.SystemProperties; |
John Reck | 8bcafc1 | 2011-08-29 16:43:02 -0700 | [diff] [blame] | 29 | import android.provider.Browser; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 30 | import android.util.Log; |
| 31 | import android.webkit.WebView; |
kaiyiz | c4ada32 | 2013-07-30 09:58:07 +0800 | [diff] [blame] | 32 | import android.widget.Toast; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 33 | |
kaiyiz | c4ada32 | 2013-07-30 09:58:07 +0800 | [diff] [blame] | 34 | import java.io.UnsupportedEncodingException; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 35 | import java.net.URISyntaxException; |
John Reck | 95a49ff | 2011-02-08 18:23:22 -0800 | [diff] [blame] | 36 | import java.util.List; |
John Reck | db3d43d | 2011-02-11 11:56:38 -0800 | [diff] [blame] | 37 | import java.util.regex.Matcher; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 38 | |
| 39 | /** |
| 40 | * |
| 41 | */ |
| 42 | public class UrlHandler { |
| 43 | |
kaiyiz | c4ada32 | 2013-07-30 09:58:07 +0800 | [diff] [blame] | 44 | private final static String TAG = "UrlHandler"; |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 45 | static final String RLZ_PROVIDER = "com.google.android.partnersetup.rlzappprovider"; |
| 46 | static final Uri RLZ_PROVIDER_URI = Uri.parse("content://" + RLZ_PROVIDER + "/"); |
| 47 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 48 | // Use in overrideUrlLoading |
| 49 | /* package */ final static String SCHEME_WTAI = "wtai://wp/"; |
| 50 | /* package */ final static String SCHEME_WTAI_MC = "wtai://wp/mc;"; |
| 51 | /* package */ final static String SCHEME_WTAI_SD = "wtai://wp/sd;"; |
| 52 | /* package */ final static String SCHEME_WTAI_AP = "wtai://wp/ap;"; |
| 53 | |
| 54 | Controller mController; |
| 55 | Activity mActivity; |
| 56 | |
| 57 | private Boolean mIsProviderPresent = null; |
| 58 | private Uri mRlzUri = null; |
| 59 | |
| 60 | public UrlHandler(Controller controller) { |
| 61 | mController = controller; |
| 62 | mActivity = mController.getActivity(); |
| 63 | } |
| 64 | |
Michael Kolb | 18eb377 | 2010-12-10 14:29:51 -0800 | [diff] [blame] | 65 | boolean shouldOverrideUrlLoading(Tab tab, WebView view, String url) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 66 | if (view.isPrivateBrowsingEnabled()) { |
| 67 | // Don't allow urls to leave the browser app when in |
| 68 | // private browsing mode |
Patrick Scott | d05faa6 | 2010-12-16 09:15:34 -0500 | [diff] [blame] | 69 | return false; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | if (url.startsWith(SCHEME_WTAI)) { |
| 73 | // wtai://wp/mc;number |
| 74 | // number=string(phone-number) |
| 75 | if (url.startsWith(SCHEME_WTAI_MC)) { |
| 76 | Intent intent = new Intent(Intent.ACTION_VIEW, |
| 77 | Uri.parse(WebView.SCHEME_TEL + |
| 78 | url.substring(SCHEME_WTAI_MC.length()))); |
| 79 | mActivity.startActivity(intent); |
| 80 | // before leaving BrowserActivity, close the empty child tab. |
| 81 | // If a new tab is created through JavaScript open to load this |
| 82 | // url, we would like to close it as we will load this url in a |
| 83 | // different Activity. |
John Reck | 8bcafc1 | 2011-08-29 16:43:02 -0700 | [diff] [blame] | 84 | mController.closeEmptyTab(); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 85 | return true; |
| 86 | } |
| 87 | // wtai://wp/sd;dtmf |
| 88 | // dtmf=string(dialstring) |
| 89 | if (url.startsWith(SCHEME_WTAI_SD)) { |
| 90 | // TODO: only send when there is active voice connection |
| 91 | return false; |
| 92 | } |
| 93 | // wtai://wp/ap;number;name |
| 94 | // number=string(phone-number) |
| 95 | // name=string |
| 96 | if (url.startsWith(SCHEME_WTAI_AP)) { |
| 97 | // TODO |
| 98 | return false; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | // The "about:" schemes are internal to the browser; don't want these to |
| 103 | // be dispatched to other apps. |
| 104 | if (url.startsWith("about:")) { |
| 105 | return false; |
| 106 | } |
| 107 | |
| 108 | // If this is a Google search, attempt to add an RLZ string |
| 109 | // (if one isn't already present). |
| 110 | if (rlzProviderPresent()) { |
| 111 | Uri siteUri = Uri.parse(url); |
| 112 | if (needsRlzString(siteUri)) { |
Ben Murdoch | a941f6e | 2010-12-07 16:09:16 +0000 | [diff] [blame] | 113 | // Need to look up the RLZ info from a database, so do it in an |
| 114 | // AsyncTask. Although we are not overriding the URL load synchronously, |
| 115 | // we guarantee that we will handle this URL load after the task executes, |
| 116 | // so it's safe to just return true to WebCore now to stop its own loading. |
Russell Brenner | 14e1ae2 | 2011-01-12 14:54:23 -0800 | [diff] [blame] | 117 | new RLZTask(tab, siteUri, view).execute(); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 118 | return true; |
| 119 | } |
| 120 | } |
| 121 | |
kaiyiz | c4ada32 | 2013-07-30 09:58:07 +0800 | [diff] [blame] | 122 | // add for carrier wap2estore feature |
| 123 | boolean wap2estore = SystemProperties.getBoolean( |
| 124 | "persist.env.browser.wap2estore", false); |
| 125 | if (wap2estore && isEstoreTypeUrl(url)) { |
| 126 | handleEstoreTypeUrl(url); |
| 127 | return true; |
| 128 | } |
| 129 | |
John Reck | 8bcafc1 | 2011-08-29 16:43:02 -0700 | [diff] [blame] | 130 | if (startActivityForUrl(tab, url)) { |
Russell Brenner | d4afde1 | 2011-01-07 11:09:36 -0800 | [diff] [blame] | 131 | return true; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Russell Brenner | 14e1ae2 | 2011-01-12 14:54:23 -0800 | [diff] [blame] | 134 | if (handleMenuClick(tab, url)) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 135 | return true; |
| 136 | } |
Russell Brenner | d4afde1 | 2011-01-07 11:09:36 -0800 | [diff] [blame] | 137 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 138 | return false; |
| 139 | } |
| 140 | |
kaiyiz | c4ada32 | 2013-07-30 09:58:07 +0800 | [diff] [blame] | 141 | private boolean isEstoreTypeUrl(String url) { |
| 142 | String utf8Url = null; |
| 143 | try { |
| 144 | utf8Url = new String(url.getBytes("UTF-8"), "UTF-8"); |
| 145 | } catch (UnsupportedEncodingException e) { |
| 146 | Log.e(TAG, "err " + e); |
| 147 | } |
| 148 | if (utf8Url != null && utf8Url.startsWith("estore:")) { |
| 149 | return true; |
| 150 | } |
| 151 | return false; |
| 152 | } |
| 153 | |
| 154 | private void handleEstoreTypeUrl(String url) { |
| 155 | String utf8Url = null, finalUrl = null; |
| 156 | try { |
| 157 | utf8Url = new String(url.getBytes("UTF-8"), "UTF-8"); |
| 158 | } catch (UnsupportedEncodingException e) { |
| 159 | Log.e(TAG, "err " + e); |
| 160 | } |
| 161 | if (utf8Url != null) { |
| 162 | finalUrl = utf8Url; |
| 163 | } else { |
| 164 | finalUrl = url; |
| 165 | } |
| 166 | if (finalUrl.replaceFirst("estore:", "").length() > 256) { |
| 167 | Toast.makeText(mActivity, R.string.estore_url_warning, Toast.LENGTH_LONG).show(); |
| 168 | return; |
| 169 | } |
| 170 | Intent intent = new Intent(Intent.ACTION_VIEW); |
| 171 | intent.setData(Uri.parse(finalUrl)); |
| 172 | try { |
| 173 | mActivity.startActivity(intent); |
| 174 | } catch (ActivityNotFoundException ex) { |
| 175 | String downloadUrl = mActivity.getResources().getString(R.string.estore_homepage); |
| 176 | mController.loadUrl(mController.getCurrentTab(), downloadUrl); |
| 177 | Toast.makeText(mActivity, R.string.download_estore_app, Toast.LENGTH_LONG).show(); |
| 178 | } |
| 179 | } |
| 180 | |
John Reck | 8bcafc1 | 2011-08-29 16:43:02 -0700 | [diff] [blame] | 181 | boolean startActivityForUrl(Tab tab, String url) { |
Russell Brenner | d4afde1 | 2011-01-07 11:09:36 -0800 | [diff] [blame] | 182 | Intent intent; |
| 183 | // perform generic parsing of the URI to turn it into an Intent. |
| 184 | try { |
| 185 | intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME); |
| 186 | } catch (URISyntaxException ex) { |
| 187 | Log.w("Browser", "Bad URI " + url + ": " + ex.getMessage()); |
| 188 | return false; |
| 189 | } |
| 190 | |
| 191 | // check whether the intent can be resolved. If not, we will see |
| 192 | // whether we can download it from the Market. |
| 193 | if (mActivity.getPackageManager().resolveActivity(intent, 0) == null) { |
| 194 | String packagename = intent.getPackage(); |
| 195 | if (packagename != null) { |
| 196 | intent = new Intent(Intent.ACTION_VIEW, Uri |
| 197 | .parse("market://search?q=pname:" + packagename)); |
| 198 | intent.addCategory(Intent.CATEGORY_BROWSABLE); |
| 199 | mActivity.startActivity(intent); |
| 200 | // before leaving BrowserActivity, close the empty child tab. |
| 201 | // If a new tab is created through JavaScript open to load this |
| 202 | // url, we would like to close it as we will load this url in a |
| 203 | // different Activity. |
John Reck | 8bcafc1 | 2011-08-29 16:43:02 -0700 | [diff] [blame] | 204 | mController.closeEmptyTab(); |
Russell Brenner | d4afde1 | 2011-01-07 11:09:36 -0800 | [diff] [blame] | 205 | return true; |
| 206 | } else { |
| 207 | return false; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | // sanitize the Intent, ensuring web pages can not bypass browser |
| 212 | // security (only access to BROWSABLE activities). |
| 213 | intent.addCategory(Intent.CATEGORY_BROWSABLE); |
| 214 | intent.setComponent(null); |
John Reck | 8bcafc1 | 2011-08-29 16:43:02 -0700 | [diff] [blame] | 215 | // Re-use the existing tab if the intent comes back to us |
| 216 | if (tab != null) { |
| 217 | if (tab.getAppId() == null) { |
Michael Kolb | f1286a4 | 2012-04-17 14:10:52 -0700 | [diff] [blame] | 218 | tab.setAppId(mActivity.getPackageName() + "-" + tab.getId()); |
John Reck | 8bcafc1 | 2011-08-29 16:43:02 -0700 | [diff] [blame] | 219 | } |
| 220 | intent.putExtra(Browser.EXTRA_APPLICATION_ID, tab.getAppId()); |
| 221 | } |
John Reck | db3d43d | 2011-02-11 11:56:38 -0800 | [diff] [blame] | 222 | // Make sure webkit can handle it internally before checking for specialized |
| 223 | // handlers. If webkit can't handle it internally, we need to call |
| 224 | // startActivityIfNeeded |
| 225 | Matcher m = UrlUtils.ACCEPTED_URI_SCHEMA.matcher(url); |
| 226 | if (m.matches() && !isSpecializedHandlerAvailable(intent)) { |
John Reck | 95a49ff | 2011-02-08 18:23:22 -0800 | [diff] [blame] | 227 | return false; |
| 228 | } |
Russell Brenner | d4afde1 | 2011-01-07 11:09:36 -0800 | [diff] [blame] | 229 | try { |
John Reck | 38b3965 | 2012-06-05 09:22:59 -0700 | [diff] [blame] | 230 | intent.putExtra(BrowserActivity.EXTRA_DISABLE_URL_OVERRIDE, true); |
Russell Brenner | d4afde1 | 2011-01-07 11:09:36 -0800 | [diff] [blame] | 231 | if (mActivity.startActivityIfNeeded(intent, -1)) { |
| 232 | // before leaving BrowserActivity, close the empty child tab. |
| 233 | // If a new tab is created through JavaScript open to load this |
| 234 | // url, we would like to close it as we will load this url in a |
| 235 | // different Activity. |
John Reck | 8bcafc1 | 2011-08-29 16:43:02 -0700 | [diff] [blame] | 236 | mController.closeEmptyTab(); |
Russell Brenner | d4afde1 | 2011-01-07 11:09:36 -0800 | [diff] [blame] | 237 | return true; |
| 238 | } |
| 239 | } catch (ActivityNotFoundException ex) { |
| 240 | // ignore the error. If no application can handle the URL, |
| 241 | // eg about:blank, assume the browser can handle it. |
| 242 | } |
| 243 | |
| 244 | return false; |
| 245 | } |
| 246 | |
John Reck | 95a49ff | 2011-02-08 18:23:22 -0800 | [diff] [blame] | 247 | /** |
| 248 | * Search for intent handlers that are specific to this URL |
| 249 | * aka, specialized apps like google maps or youtube |
| 250 | */ |
| 251 | private boolean isSpecializedHandlerAvailable(Intent intent) { |
| 252 | PackageManager pm = mActivity.getPackageManager(); |
| 253 | List<ResolveInfo> handlers = pm.queryIntentActivities(intent, |
| 254 | PackageManager.GET_RESOLVED_FILTER); |
| 255 | if (handlers == null || handlers.size() == 0) { |
| 256 | return false; |
| 257 | } |
| 258 | for (ResolveInfo resolveInfo : handlers) { |
| 259 | IntentFilter filter = resolveInfo.filter; |
| 260 | if (filter == null) { |
| 261 | // No intent filter matches this intent? |
| 262 | // Error on the side of staying in the browser, ignore |
| 263 | continue; |
| 264 | } |
John Reck | 1e6a287 | 2011-12-15 14:35:02 -0800 | [diff] [blame] | 265 | if (filter.countDataAuthorities() == 0 && filter.countDataPaths() == 0) { |
John Reck | 95a49ff | 2011-02-08 18:23:22 -0800 | [diff] [blame] | 266 | // Generic handler, skip |
| 267 | continue; |
| 268 | } |
| 269 | return true; |
| 270 | } |
| 271 | return false; |
| 272 | } |
| 273 | |
Russell Brenner | 14e1ae2 | 2011-01-12 14:54:23 -0800 | [diff] [blame] | 274 | // In case a physical keyboard is attached, handle clicks with the menu key |
| 275 | // depressed by opening in a new tab |
Russell Brenner | ca9898e | 2011-01-21 13:34:02 -0800 | [diff] [blame] | 276 | boolean handleMenuClick(Tab tab, String url) { |
Russell Brenner | 14e1ae2 | 2011-01-12 14:54:23 -0800 | [diff] [blame] | 277 | if (mController.isMenuDown()) { |
Michael Kolb | 7bcafde | 2011-05-09 13:55:59 -0700 | [diff] [blame] | 278 | mController.openTab(url, |
| 279 | (tab != null) && tab.isPrivateBrowsingEnabled(), |
| 280 | !BrowserSettings.getInstance().openInBackground(), true); |
Russell Brenner | 14e1ae2 | 2011-01-12 14:54:23 -0800 | [diff] [blame] | 281 | mActivity.closeOptionsMenu(); |
| 282 | return true; |
| 283 | } |
| 284 | |
| 285 | return false; |
| 286 | } |
| 287 | |
Russell Brenner | ca9898e | 2011-01-21 13:34:02 -0800 | [diff] [blame] | 288 | // TODO: Move this class into Tab, where it can be properly stopped upon |
| 289 | // closure of the tab |
Ben Murdoch | a941f6e | 2010-12-07 16:09:16 +0000 | [diff] [blame] | 290 | private class RLZTask extends AsyncTask<Void, Void, String> { |
Russell Brenner | 14e1ae2 | 2011-01-12 14:54:23 -0800 | [diff] [blame] | 291 | private Tab mTab; |
Ben Murdoch | a941f6e | 2010-12-07 16:09:16 +0000 | [diff] [blame] | 292 | private Uri mSiteUri; |
| 293 | private WebView mWebView; |
| 294 | |
Russell Brenner | 14e1ae2 | 2011-01-12 14:54:23 -0800 | [diff] [blame] | 295 | public RLZTask(Tab tab, Uri uri, WebView webView) { |
| 296 | mTab = tab; |
Ben Murdoch | a941f6e | 2010-12-07 16:09:16 +0000 | [diff] [blame] | 297 | mSiteUri = uri; |
| 298 | mWebView = webView; |
| 299 | } |
| 300 | |
| 301 | protected String doInBackground(Void... unused) { |
| 302 | String result = mSiteUri.toString(); |
| 303 | Cursor cur = null; |
| 304 | try { |
| 305 | cur = mActivity.getContentResolver() |
| 306 | .query(getRlzUri(), null, null, null, null); |
| 307 | if (cur != null && cur.moveToFirst() && !cur.isNull(0)) { |
| 308 | result = mSiteUri.buildUpon() |
| 309 | .appendQueryParameter("rlz", cur.getString(0)) |
| 310 | .build().toString(); |
| 311 | } |
| 312 | } finally { |
| 313 | if (cur != null) { |
| 314 | cur.close(); |
| 315 | } |
| 316 | } |
| 317 | return result; |
| 318 | } |
| 319 | |
| 320 | protected void onPostExecute(String result) { |
Michael Kolb | e57c709 | 2011-07-19 11:56:56 -0700 | [diff] [blame] | 321 | // abort if we left browser already |
| 322 | if (mController.isActivityPaused()) return; |
Russell Brenner | ca9898e | 2011-01-21 13:34:02 -0800 | [diff] [blame] | 323 | // Make sure the Tab was not closed while handling the task |
Michael Kolb | c831b63 | 2011-05-11 09:30:34 -0700 | [diff] [blame] | 324 | if (mController.getTabControl().getTabPosition(mTab) != -1) { |
Russell Brenner | ca9898e | 2011-01-21 13:34:02 -0800 | [diff] [blame] | 325 | // If the Activity Manager is not invoked, load the URL directly |
John Reck | 8bcafc1 | 2011-08-29 16:43:02 -0700 | [diff] [blame] | 326 | if (!startActivityForUrl(mTab, result)) { |
Russell Brenner | ca9898e | 2011-01-21 13:34:02 -0800 | [diff] [blame] | 327 | if (!handleMenuClick(mTab, result)) { |
John Reck | 26b1832 | 2011-06-21 13:08:58 -0700 | [diff] [blame] | 328 | mController.loadUrl(mTab, result); |
Russell Brenner | ca9898e | 2011-01-21 13:34:02 -0800 | [diff] [blame] | 329 | } |
Russell Brenner | 14e1ae2 | 2011-01-12 14:54:23 -0800 | [diff] [blame] | 330 | } |
| 331 | } |
Ben Murdoch | a941f6e | 2010-12-07 16:09:16 +0000 | [diff] [blame] | 332 | } |
| 333 | } |
| 334 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 335 | // Determine whether the RLZ provider is present on the system. |
| 336 | private boolean rlzProviderPresent() { |
| 337 | if (mIsProviderPresent == null) { |
| 338 | PackageManager pm = mActivity.getPackageManager(); |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 339 | mIsProviderPresent = pm.resolveContentProvider(RLZ_PROVIDER, 0) != null; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 340 | } |
| 341 | return mIsProviderPresent; |
| 342 | } |
| 343 | |
| 344 | // Retrieve the RLZ access point string and cache the URI used to |
| 345 | // retrieve RLZ values. |
| 346 | private Uri getRlzUri() { |
| 347 | if (mRlzUri == null) { |
| 348 | String ap = mActivity.getResources() |
| 349 | .getString(R.string.rlz_access_point); |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 350 | mRlzUri = Uri.withAppendedPath(RLZ_PROVIDER_URI, ap); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 351 | } |
| 352 | return mRlzUri; |
| 353 | } |
| 354 | |
| 355 | // Determine if this URI appears to be for a Google search |
| 356 | // and does not have an RLZ parameter. |
| 357 | // Taken largely from Chrome source, src/chrome/browser/google_url_tracker.cc |
| 358 | private static boolean needsRlzString(Uri uri) { |
| 359 | String scheme = uri.getScheme(); |
| 360 | if (("http".equals(scheme) || "https".equals(scheme)) && |
| 361 | (uri.getQueryParameter("q") != null) && |
| 362 | (uri.getQueryParameter("rlz") == null)) { |
| 363 | String host = uri.getHost(); |
| 364 | if (host == null) { |
| 365 | return false; |
| 366 | } |
| 367 | String[] hostComponents = host.split("\\."); |
| 368 | |
| 369 | if (hostComponents.length < 2) { |
| 370 | return false; |
| 371 | } |
| 372 | int googleComponent = hostComponents.length - 2; |
| 373 | String component = hostComponents[googleComponent]; |
| 374 | if (!"google".equals(component)) { |
| 375 | if (hostComponents.length < 3 || |
| 376 | (!"co".equals(component) && !"com".equals(component))) { |
| 377 | return false; |
| 378 | } |
| 379 | googleComponent = hostComponents.length - 3; |
| 380 | if (!"google".equals(hostComponents[googleComponent])) { |
| 381 | return false; |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | // Google corp network handling. |
| 386 | if (googleComponent > 0 && "corp".equals( |
| 387 | hostComponents[googleComponent - 1])) { |
| 388 | return false; |
| 389 | } |
| 390 | |
| 391 | return true; |
| 392 | } |
| 393 | return false; |
| 394 | } |
| 395 | |
| 396 | } |