1 // Copyright 2014 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 <string.h> 6 7 #include "base/android/jni_android.h" 8 #include "base/android/jni_string.h" 9 #include "base/strings/string16.h" 10 #include "base/strings/string_piece.h" 11 #include "jni/IDNStringUtil_jni.h" 12 #include "url/url_canon_internal.h" 13 14 namespace url { 15 16 // This uses the JDK's conversion function, which uses IDNA 2003, unlike the 17 // ICU implementation. 18 bool IDNToASCII(const base::char16* src, int src_len, CanonOutputW* output) { 19 DCHECK_EQ(0, output->length()); // Output buffer is assumed empty. 20 21 JNIEnv* env = base::android::AttachCurrentThread(); 22 base::android::ScopedJavaLocalRef<jstring> java_src = 23 base::android::ConvertUTF16ToJavaString( 24 env, base::StringPiece16(src, src_len)); 25 ScopedJavaLocalRef<jstring> java_result = 26 android::Java_IDNStringUtil_idnToASCII(env, java_src.obj()); 27 // NULL indicates failure. 28 if (java_result.is_null()) 29 return false; 30 31 base::string16 utf16_result = 32 base::android::ConvertJavaStringToUTF16(java_result); 33 output->Append(utf16_result.data(), static_cast<int>(utf16_result.size())); 34 return true; 35 } 36 37 bool RegisterIcuAlternativesJni(JNIEnv* env) { 38 return android::RegisterNativesImpl(env); 39 } 40 41 } // namespace url 42