blob: b4fc8cb6b39cecc7f84b8f72e5f5f1ac54106a59 [file] [log] [blame]
[email protected]7e27c372013-09-11 12:10:301// Copyright 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
palmer91246922015-09-20 23:16:165#include <string>
6
[email protected]7e27c372013-09-11 12:10:307#include "base/android/jni_android.h"
8#include "base/android/jni_string.h"
pkotwiczb9096f1be2017-01-19 03:09:399#include "base/strings/string_util.h"
John Abd-El-Malek9cf3d7f02018-07-27 02:40:3910#include "components/google/core/common/google_util.h"
[email protected]7e27c372013-09-11 12:10:3011#include "jni/UrlUtilities_jni.h"
12#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
13#include "url/gurl.h"
14
[email protected]21bb906f2013-11-04 19:12:5515using base::android::ConvertJavaStringToUTF8;
torne86560112016-08-04 15:59:0416using base::android::JavaParamRef;
17using base::android::ScopedJavaLocalRef;
[email protected]21bb906f2013-11-04 19:12:5518
[email protected]7e27c372013-09-11 12:10:3019namespace {
20
mariakhomenkof2f761ac2016-08-19 00:07:0521static const char* const g_supported_schemes[] = { "about", "data", "file",
22 "http", "https", "inline", "javascript", nullptr };
23
24static const char* const g_downloadable_schemes[] = {
25 "data", "blob", "file", "filesystem", "http", "https", nullptr };
26
27static const char* const g_fallback_valid_schemes[] = {
28 "http", "https", nullptr };
29
Daniel Bratell7aacf952017-11-21 17:51:2530GURL JNI_UrlUtilities_ConvertJavaStringToGURL(JNIEnv* env, jstring url) {
jdduke6a374932014-11-26 18:19:0431 return url ? GURL(ConvertJavaStringToUTF8(env, url)) : GURL();
32}
33
mariakhomenkof2f761ac2016-08-19 00:07:0534bool CheckSchemeBelongsToList(
35 JNIEnv* env,
36 const JavaParamRef<jstring>& url,
37 const char* const* scheme_list) {
Daniel Bratell7aacf952017-11-21 17:51:2538 GURL gurl = JNI_UrlUtilities_ConvertJavaStringToGURL(env, url);
mariakhomenkof2f761ac2016-08-19 00:07:0539 if (gurl.is_valid()) {
40 for (size_t i = 0; scheme_list[i]; i++) {
41 if (gurl.scheme() == scheme_list[i]) {
42 return true;
43 }
44 }
45 }
46 return false;
47}
48
[email protected]7e27c372013-09-11 12:10:3049net::registry_controlled_domains::PrivateRegistryFilter GetRegistryFilter(
50 jboolean include_private) {
51 return include_private
52 ? net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES
53 : net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES;
54}
55
jdduke6a374932014-11-26 18:19:0456} // namespace
[email protected]7e27c372013-09-11 12:10:3057
Donn Denman72af9102019-04-19 17:52:4858// Returns whether the given URLs have the same domain or host.
59// See net::registry_controlled_domains::SameDomainOrHost for details.
Daniel Bratell7aacf952017-11-21 17:51:2560static jboolean JNI_UrlUtilities_SameDomainOrHost(
61 JNIEnv* env,
Daniel Bratell7aacf952017-11-21 17:51:2562 const JavaParamRef<jstring>& url_1_str,
63 const JavaParamRef<jstring>& url_2_str,
64 jboolean include_private) {
65 GURL url_1 = JNI_UrlUtilities_ConvertJavaStringToGURL(env, url_1_str);
66 GURL url_2 = JNI_UrlUtilities_ConvertJavaStringToGURL(env, url_2_str);
[email protected]7e27c372013-09-11 12:10:3067
68 net::registry_controlled_domains::PrivateRegistryFilter filter =
69 GetRegistryFilter(include_private);
70
71 return net::registry_controlled_domains::SameDomainOrHost(url_1,
72 url_2,
73 filter);
74}
75
Donn Denman72af9102019-04-19 17:52:4876// Returns the Domain and Registry of the given URL.
77// See net::registry_controlled_domains::GetDomainAndRegistry for details.
Daniel Bratell7aacf952017-11-21 17:51:2578static ScopedJavaLocalRef<jstring> JNI_UrlUtilities_GetDomainAndRegistry(
tornef71efb32015-08-26 14:07:3279 JNIEnv* env,
torne89cc5d92015-09-04 11:16:3580 const JavaParamRef<jstring>& url,
tornef71efb32015-08-26 14:07:3281 jboolean include_private) {
jdduke6a374932014-11-26 18:19:0482 DCHECK(url);
Daniel Bratell7aacf952017-11-21 17:51:2583 GURL gurl = JNI_UrlUtilities_ConvertJavaStringToGURL(env, url);
[email protected]7e27c372013-09-11 12:10:3084 if (gurl.is_empty())
tornef71efb32015-08-26 14:07:3285 return ScopedJavaLocalRef<jstring>();
[email protected]7e27c372013-09-11 12:10:3086
87 net::registry_controlled_domains::PrivateRegistryFilter filter =
88 GetRegistryFilter(include_private);
89
[email protected]7e27c372013-09-11 12:10:3090 return base::android::ConvertUTF8ToJavaString(
tornef71efb32015-08-26 14:07:3291 env,
92 net::registry_controlled_domains::GetDomainAndRegistry(gurl, filter));
[email protected]7e27c372013-09-11 12:10:3093}
94
Donn Denman72af9102019-04-19 17:52:4895// Return whether the given URL uses the Google.com domain.
96// See google_util::IsGoogleDomainUrl for details.
Arthur Sonzogniea4bf432019-01-14 11:28:1297static jboolean JNI_UrlUtilities_IsGoogleDomainUrl(
98 JNIEnv* env,
99 const JavaParamRef<jstring>& url,
100 jboolean allow_non_standard_port) {
101 GURL gurl = JNI_UrlUtilities_ConvertJavaStringToGURL(env, url);
102 if (gurl.is_empty())
103 return false;
104 return google_util::IsGoogleDomainUrl(
105 gurl, google_util::DISALLOW_SUBDOMAIN,
106 allow_non_standard_port == JNI_TRUE
107 ? google_util::ALLOW_NON_STANDARD_PORTS
108 : google_util::DISALLOW_NON_STANDARD_PORTS);
109}
110
Donn Denman72af9102019-04-19 17:52:48111// Returns whether the given URL is a Google.com domain or sub-domain.
112// See google_util::IsGoogleDomainUrl for details.
113static jboolean JNI_UrlUtilities_IsGoogleSubDomainUrl(
114 JNIEnv* env,
115 const JavaParamRef<jstring>& url) {
116 GURL gurl = JNI_UrlUtilities_ConvertJavaStringToGURL(env, url);
117 if (gurl.is_empty())
118 return false;
119 return google_util::IsGoogleDomainUrl(
120 gurl, google_util::ALLOW_SUBDOMAIN,
121 google_util::DISALLOW_NON_STANDARD_PORTS);
122}
123
124// Returns whether the given URL is a Google.com Search URL.
125// See google_util::IsGoogleSearchUrl for details.
Daniel Bratell7aacf952017-11-21 17:51:25126static jboolean JNI_UrlUtilities_IsGoogleSearchUrl(
127 JNIEnv* env,
Daniel Bratell7aacf952017-11-21 17:51:25128 const JavaParamRef<jstring>& url) {
129 GURL gurl = JNI_UrlUtilities_ConvertJavaStringToGURL(env, url);
[email protected]7e27c372013-09-11 12:10:30130 if (gurl.is_empty())
131 return false;
mariakhomenko38c3c8c82017-01-04 21:20:50132 return google_util::IsGoogleSearchUrl(gurl);
[email protected]7e27c372013-09-11 12:10:30133}
134
Donn Denman72af9102019-04-19 17:52:48135// Returns whether the given URL is the Google Web Search URL.
136// See google_util::IsGoogleHomePageUrl for details.
Daniel Bratell7aacf952017-11-21 17:51:25137static jboolean JNI_UrlUtilities_IsGoogleHomePageUrl(
138 JNIEnv* env,
Daniel Bratell7aacf952017-11-21 17:51:25139 const JavaParamRef<jstring>& url) {
140 GURL gurl = JNI_UrlUtilities_ConvertJavaStringToGURL(env, url);
[email protected]21bb906f2013-11-04 19:12:55141 if (gurl.is_empty())
142 return false;
143 return google_util::IsGoogleHomePageUrl(gurl);
144}
145
Daniel Bratell7aacf952017-11-21 17:51:25146static jboolean JNI_UrlUtilities_IsUrlWithinScope(
147 JNIEnv* env,
Daniel Bratell7aacf952017-11-21 17:51:25148 const JavaParamRef<jstring>& url,
149 const JavaParamRef<jstring>& scope_url) {
150 GURL gurl = JNI_UrlUtilities_ConvertJavaStringToGURL(env, url);
151 GURL gscope_url = JNI_UrlUtilities_ConvertJavaStringToGURL(env, scope_url);
pkotwiczb9096f1be2017-01-19 03:09:39152 return gurl.GetOrigin() == gscope_url.GetOrigin() &&
153 base::StartsWith(gurl.path(), gscope_url.path(),
154 base::CompareCase::SENSITIVE);
155}
156
Donn Denman72af9102019-04-19 17:52:48157// Returns whether the given URLs match, ignoring the fragment portions of the
158// URLs.
Daniel Bratell7aacf952017-11-21 17:51:25159static jboolean JNI_UrlUtilities_UrlsMatchIgnoringFragments(
160 JNIEnv* env,
Daniel Bratell7aacf952017-11-21 17:51:25161 const JavaParamRef<jstring>& url,
162 const JavaParamRef<jstring>& url2) {
163 GURL gurl = JNI_UrlUtilities_ConvertJavaStringToGURL(env, url);
164 GURL gurl2 = JNI_UrlUtilities_ConvertJavaStringToGURL(env, url2);
lizeb3c1fe9cb2016-03-08 15:12:01165 if (gurl.is_empty())
166 return gurl2.is_empty();
167 if (!gurl.is_valid() || !gurl2.is_valid())
168 return false;
169
170 GURL::Replacements replacements;
171 replacements.SetRefStr("");
172 return gurl.ReplaceComponents(replacements) ==
173 gurl2.ReplaceComponents(replacements);
174}
175
Donn Denman72af9102019-04-19 17:52:48176// Returns whether the given URLs have fragments that differ.
Daniel Bratell7aacf952017-11-21 17:51:25177static jboolean JNI_UrlUtilities_UrlsFragmentsDiffer(
178 JNIEnv* env,
Daniel Bratell7aacf952017-11-21 17:51:25179 const JavaParamRef<jstring>& url,
180 const JavaParamRef<jstring>& url2) {
181 GURL gurl = JNI_UrlUtilities_ConvertJavaStringToGURL(env, url);
182 GURL gurl2 = JNI_UrlUtilities_ConvertJavaStringToGURL(env, url2);
lizeb3c1fe9cb2016-03-08 15:12:01183 if (gurl.is_empty())
184 return !gurl2.is_empty();
185 if (!gurl.is_valid() || !gurl2.is_valid())
186 return true;
187 return gurl.ref() != gurl2.ref();
188}
189
Daniel Bratell7aacf952017-11-21 17:51:25190static jboolean JNI_UrlUtilities_IsAcceptedScheme(
191 JNIEnv* env,
Daniel Bratell7aacf952017-11-21 17:51:25192 const JavaParamRef<jstring>& url) {
mariakhomenkof2f761ac2016-08-19 00:07:05193 return CheckSchemeBelongsToList(env, url, g_supported_schemes);
194}
195
Daniel Bratell7aacf952017-11-21 17:51:25196static jboolean JNI_UrlUtilities_IsValidForIntentFallbackNavigation(
mariakhomenkof2f761ac2016-08-19 00:07:05197 JNIEnv* env,
mariakhomenkof2f761ac2016-08-19 00:07:05198 const JavaParamRef<jstring>& url) {
199 return CheckSchemeBelongsToList(env, url, g_fallback_valid_schemes);
200}
201
Daniel Bratell7aacf952017-11-21 17:51:25202static jboolean JNI_UrlUtilities_IsDownloadable(
203 JNIEnv* env,
Daniel Bratell7aacf952017-11-21 17:51:25204 const JavaParamRef<jstring>& url) {
mariakhomenkof2f761ac2016-08-19 00:07:05205 return CheckSchemeBelongsToList(env, url, g_downloadable_schemes);
206}