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.
5 #include "net/base/net_string_util_icu_alternatives_android.h"
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/NetStringUtil_jni.h"
12 #include "net/base/net_string_util.h"
18 // Attempts to convert |text| encoded in |charset| to a jstring (Java unicode
19 // string). Returns the result jstring, or NULL on failure.
20 ScopedJavaLocalRef
<jstring
> ConvertToJstring(const std::string
& text
,
21 const char* charset
) {
22 JNIEnv
* env
= base::android::AttachCurrentThread();
23 ScopedJavaLocalRef
<jobject
> java_byte_buffer(
25 env
->NewDirectByteBuffer(const_cast<char*>(text
.data()), text
.length()));
26 base::android::ScopedJavaLocalRef
<jstring
> java_charset
=
27 base::android::ConvertUTF8ToJavaString(env
, base::StringPiece(charset
));
28 ScopedJavaLocalRef
<jstring
> java_result
=
29 android::Java_NetStringUtil_convertToUnicode(env
, java_byte_buffer
.obj(),
34 // Attempts to convert |text| encoded in |charset| to a jstring (Java unicode
35 // string) and then normalizes the string. Returns the result jstring, or NULL
37 ScopedJavaLocalRef
<jstring
> ConvertToNormalizedJstring(
38 const std::string
& text
, const char* charset
) {
39 JNIEnv
* env
= base::android::AttachCurrentThread();
40 ScopedJavaLocalRef
<jobject
> java_byte_buffer(
42 env
->NewDirectByteBuffer(const_cast<char*>(text
.data()), text
.length()));
43 base::android::ScopedJavaLocalRef
<jstring
> java_charset
=
44 base::android::ConvertUTF8ToJavaString(env
, base::StringPiece(charset
));
45 ScopedJavaLocalRef
<jstring
> java_result
=
46 android::Java_NetStringUtil_convertToUnicodeAndNormalize(
47 env
, java_byte_buffer
.obj(), java_charset
.obj());
51 // Converts |text| encoded in |charset| to a jstring (Java unicode string).
52 // Any characters that can not be converted are replaced with U+FFFD.
53 ScopedJavaLocalRef
<jstring
> ConvertToJstringWithSubstitutions(
54 const std::string
& text
, const char* charset
) {
55 JNIEnv
* env
= base::android::AttachCurrentThread();
56 ScopedJavaLocalRef
<jobject
> java_byte_buffer(
58 env
->NewDirectByteBuffer(const_cast<char*>(text
.data()), text
.length()));
59 base::android::ScopedJavaLocalRef
<jstring
> java_charset
=
60 base::android::ConvertUTF8ToJavaString(env
, base::StringPiece(charset
));
61 ScopedJavaLocalRef
<jstring
> java_result
=
62 android::Java_NetStringUtil_convertToUnicodeWithSubstitutions(
63 env
, java_byte_buffer
.obj(), java_charset
.obj());
69 // This constant cannot be defined as const char[] because it is initialized
70 // by base::kCodepageLatin1 (which is const char[]) in net_string_util_icu.cc.
71 const char* const kCharsetLatin1
= "ISO-8859-1";
73 bool ConvertToUtf8(const std::string
& text
, const char* charset
,
74 std::string
* output
) {
76 ScopedJavaLocalRef
<jstring
> java_result
= ConvertToJstring(text
, charset
);
77 if (java_result
.is_null())
79 *output
= base::android::ConvertJavaStringToUTF8(java_result
);
83 bool ConvertToUtf8AndNormalize(const std::string
& text
, const char* charset
,
84 std::string
* output
) {
86 ScopedJavaLocalRef
<jstring
> java_result
= ConvertToNormalizedJstring(
88 if (java_result
.is_null())
90 *output
= base::android::ConvertJavaStringToUTF8(java_result
);
94 bool ConvertToUTF16(const std::string
& text
, const char* charset
,
95 base::string16
* output
) {
97 ScopedJavaLocalRef
<jstring
> java_result
= ConvertToJstring(text
, charset
);
98 if (java_result
.is_null())
100 *output
= base::android::ConvertJavaStringToUTF16(java_result
);
104 bool ConvertToUTF16WithSubstitutions(const std::string
& text
,
106 base::string16
* output
) {
108 ScopedJavaLocalRef
<jstring
> java_result
=
109 ConvertToJstringWithSubstitutions(text
, charset
);
110 if (java_result
.is_null())
112 *output
= base::android::ConvertJavaStringToUTF16(java_result
);
116 bool RegisterNetStringUtils(JNIEnv
* env
) {
117 return android::RegisterNativesImpl(env
);