Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / android / url_utilities.cc
bloba82902548bc096f966e6fd9fa74f988802f1ca15
1 // 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.
5 #include "base/android/jni_android.h"
6 #include "base/android/jni_string.h"
7 #include "components/google/core/browser/google_util.h"
8 #include "components/url_formatter/url_fixer.h"
9 #include "jni/UrlUtilities_jni.h"
10 #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
11 #include "url/gurl.h"
13 using base::android::ConvertJavaStringToUTF8;
15 namespace {
17 GURL ConvertJavaStringToGURL(JNIEnv*env, jstring url) {
18 return url ? GURL(ConvertJavaStringToUTF8(env, url)) : GURL();
21 net::registry_controlled_domains::PrivateRegistryFilter GetRegistryFilter(
22 jboolean include_private) {
23 return include_private
24 ? net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES
25 : net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES;
28 } // namespace
30 static jboolean SameDomainOrHost(JNIEnv* env,
31 const JavaParamRef<jclass>& clazz,
32 const JavaParamRef<jstring>& url_1_str,
33 const JavaParamRef<jstring>& url_2_str,
34 jboolean include_private) {
35 GURL url_1 = ConvertJavaStringToGURL(env, url_1_str);
36 GURL url_2 = ConvertJavaStringToGURL(env, url_2_str);
38 net::registry_controlled_domains::PrivateRegistryFilter filter =
39 GetRegistryFilter(include_private);
41 return net::registry_controlled_domains::SameDomainOrHost(url_1,
42 url_2,
43 filter);
46 static ScopedJavaLocalRef<jstring> GetDomainAndRegistry(
47 JNIEnv* env,
48 const JavaParamRef<jclass>& clazz,
49 const JavaParamRef<jstring>& url,
50 jboolean include_private) {
51 DCHECK(url);
52 GURL gurl = ConvertJavaStringToGURL(env, url);
53 if (gurl.is_empty())
54 return ScopedJavaLocalRef<jstring>();
56 net::registry_controlled_domains::PrivateRegistryFilter filter =
57 GetRegistryFilter(include_private);
59 return base::android::ConvertUTF8ToJavaString(
60 env,
61 net::registry_controlled_domains::GetDomainAndRegistry(gurl, filter));
64 static jboolean IsGoogleSearchUrl(JNIEnv* env,
65 const JavaParamRef<jclass>& clazz,
66 const JavaParamRef<jstring>& url) {
67 GURL gurl = ConvertJavaStringToGURL(env, url);
68 if (gurl.is_empty())
69 return false;
70 return google_util::IsGoogleSearchUrl(gurl);
73 static jboolean IsGoogleHomePageUrl(JNIEnv* env,
74 const JavaParamRef<jclass>& clazz,
75 const JavaParamRef<jstring>& url) {
76 GURL gurl = ConvertJavaStringToGURL(env, url);
77 if (gurl.is_empty())
78 return false;
79 return google_util::IsGoogleHomePageUrl(gurl);
82 static ScopedJavaLocalRef<jstring> FixupUrl(
83 JNIEnv* env,
84 const JavaParamRef<jclass>& clazz,
85 const JavaParamRef<jstring>& url,
86 const JavaParamRef<jstring>& optional_desired_tld) {
87 DCHECK(url);
88 GURL fixed_url = url_formatter::FixupURL(
89 base::android::ConvertJavaStringToUTF8(env, url),
90 optional_desired_tld
91 ? base::android::ConvertJavaStringToUTF8(env, optional_desired_tld)
92 : std::string());
94 return fixed_url.is_valid()
95 ? base::android::ConvertUTF8ToJavaString(env, fixed_url.spec())
96 : ScopedJavaLocalRef<jstring>();
99 // Register native methods
100 bool RegisterUrlUtilities(JNIEnv* env) {
101 return RegisterNativesImpl(env);