OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <string.h> | 5 #include <string.h> |
6 | 6 |
7 #include "base/android/jni_android.h" | 7 #include "base/android/jni_android.h" |
8 #include "base/android/jni_string.h" | 8 #include "base/android/jni_string.h" |
9 #include "base/strings/string16.h" | 9 #include "base/strings/string16.h" |
10 #include "base/strings/string_piece.h" | 10 #include "base/strings/string_piece.h" |
11 #include "jni/IDNStringUtil_jni.h" | 11 #include "jni/IDNStringUtil_jni.h" |
12 #include "url/url_canon_internal.h" | 12 #include "url/url_canon_internal.h" |
13 | 13 |
| 14 using base::android::ScopedJavaLocalRef; |
| 15 |
14 namespace url { | 16 namespace url { |
15 | 17 |
16 // This uses the JDK's conversion function, which uses IDNA 2003, unlike the | 18 // This uses the JDK's conversion function, which uses IDNA 2003, unlike the |
17 // ICU implementation. | 19 // ICU implementation. |
18 bool IDNToASCII(const base::char16* src, int src_len, CanonOutputW* output) { | 20 bool IDNToASCII(const base::char16* src, int src_len, CanonOutputW* output) { |
19 DCHECK_EQ(0, output->length()); // Output buffer is assumed empty. | 21 DCHECK_EQ(0, output->length()); // Output buffer is assumed empty. |
20 | 22 |
21 JNIEnv* env = base::android::AttachCurrentThread(); | 23 JNIEnv* env = base::android::AttachCurrentThread(); |
22 base::android::ScopedJavaLocalRef<jstring> java_src = | 24 base::android::ScopedJavaLocalRef<jstring> java_src = |
23 base::android::ConvertUTF16ToJavaString( | 25 base::android::ConvertUTF16ToJavaString( |
24 env, base::StringPiece16(src, src_len)); | 26 env, base::StringPiece16(src, src_len)); |
25 ScopedJavaLocalRef<jstring> java_result = | 27 ScopedJavaLocalRef<jstring> java_result = |
26 android::Java_IDNStringUtil_idnToASCII(env, java_src.obj()); | 28 android::Java_IDNStringUtil_idnToASCII(env, java_src.obj()); |
27 // NULL indicates failure. | 29 // NULL indicates failure. |
28 if (java_result.is_null()) | 30 if (java_result.is_null()) |
29 return false; | 31 return false; |
30 | 32 |
31 base::string16 utf16_result = | 33 base::string16 utf16_result = |
32 base::android::ConvertJavaStringToUTF16(java_result); | 34 base::android::ConvertJavaStringToUTF16(java_result); |
33 output->Append(utf16_result.data(), static_cast<int>(utf16_result.size())); | 35 output->Append(utf16_result.data(), static_cast<int>(utf16_result.size())); |
34 return true; | 36 return true; |
35 } | 37 } |
36 | 38 |
37 } // namespace url | 39 } // namespace url |
OLD | NEW |