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. 4 5 #include "base/android/jni_android.h" 6 #include "base/android/jni_string.h" 7 #include "chrome/browser/google/google_util.h" 8 #include "chrome/common/net/url_fixer_upper.h" 9 #include "jni/UrlUtilities_jni.h" 10 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" 11 #include "url/gurl.h" 12 13 using base::android::ConvertJavaStringToUTF8; 14 15 namespace { 16 17 net::registry_controlled_domains::PrivateRegistryFilter GetRegistryFilter( 18 jboolean include_private) { 19 return include_private 20 ? net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES 21 : net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES; 22 } 23 24 } 25 26 static jboolean SameDomainOrHost(JNIEnv* env, 27 jclass clazz, 28 jstring url_1_str, 29 jstring url_2_str, 30 jboolean include_private) { 31 GURL url_1(ConvertJavaStringToUTF8(env, url_1_str)); 32 GURL url_2(ConvertJavaStringToUTF8(env, url_2_str)); 33 34 net::registry_controlled_domains::PrivateRegistryFilter filter = 35 GetRegistryFilter(include_private); 36 37 return net::registry_controlled_domains::SameDomainOrHost(url_1, 38 url_2, 39 filter); 40 } 41 42 static jstring GetDomainAndRegistry(JNIEnv* env, 43 jclass clazz, 44 jstring url, 45 jboolean include_private) { 46 GURL gurl = GURL(ConvertJavaStringToUTF8(env, url)); 47 if (gurl.is_empty()) 48 return NULL; 49 50 net::registry_controlled_domains::PrivateRegistryFilter filter = 51 GetRegistryFilter(include_private); 52 53 // OK to release, JNI binding. 54 return base::android::ConvertUTF8ToJavaString( 55 env, net::registry_controlled_domains::GetDomainAndRegistry( 56 gurl, filter)).Release(); 57 } 58 59 static jboolean IsGoogleSearchUrl(JNIEnv* env, jclass clazz, jstring url) { 60 GURL gurl = GURL(ConvertJavaStringToUTF8(env, url)); 61 if (gurl.is_empty()) 62 return false; 63 return google_util::IsGoogleSearchUrl(gurl); 64 } 65 66 static jboolean IsGoogleHomePageUrl(JNIEnv* env, jclass clazz, jstring url) { 67 GURL gurl = GURL(ConvertJavaStringToUTF8(env, url)); 68 if (gurl.is_empty()) 69 return false; 70 return google_util::IsGoogleHomePageUrl(gurl); 71 } 72 73 static jstring FixupUrl(JNIEnv* env, 74 jclass clazz, 75 jstring url, 76 jstring desired_tld) { 77 GURL fixed_url = URLFixerUpper::FixupURL( 78 base::android::ConvertJavaStringToUTF8(env, url), 79 base::android::ConvertJavaStringToUTF8(env, desired_tld)); 80 81 82 return fixed_url.is_valid() ? 83 base::android::ConvertUTF8ToJavaString(env, fixed_url.spec()).Release() : 84 NULL; 85 } 86 87 // Register native methods 88 bool RegisterUrlUtilities(JNIEnv* env) { 89 return RegisterNativesImpl(env); 90 } 91