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 jobject java_byte_buffer
=
24 env
->NewDirectByteBuffer(const_cast<char*>(text
.data()), text
.length());
25 base::android::ScopedJavaLocalRef
<jstring
> java_charset
=
26 base::android::ConvertUTF8ToJavaString(env
, base::StringPiece(charset
));
27 ScopedJavaLocalRef
<jstring
> java_result
=
28 android::Java_NetStringUtil_convertToUnicode(env
, java_byte_buffer
,
33 // Attempts to convert |text| encoded in |charset| to a jstring (Java unicode
34 // string) and then normalizes the string. Returns the result jstring, or NULL
36 ScopedJavaLocalRef
<jstring
> ConvertToNormalizedJstring(
37 const std::string
& text
, const char* charset
) {
38 JNIEnv
* env
= base::android::AttachCurrentThread();
39 jobject java_byte_buffer
=
40 env
->NewDirectByteBuffer(const_cast<char*>(text
.data()), text
.length());
41 base::android::ScopedJavaLocalRef
<jstring
> java_charset
=
42 base::android::ConvertUTF8ToJavaString(env
, base::StringPiece(charset
));
43 ScopedJavaLocalRef
<jstring
> java_result
=
44 android::Java_NetStringUtil_convertToUnicodeAndNormalize(
45 env
, java_byte_buffer
, java_charset
.obj());
49 // Converts |text| encoded in |charset| to a jstring (Java unicode string).
50 // Any characters that can not be converted are replaced with U+FFFD.
51 ScopedJavaLocalRef
<jstring
> ConvertToJstringWithSubstitutions(
52 const std::string
& text
, const char* charset
) {
53 JNIEnv
* env
= base::android::AttachCurrentThread();
54 jobject java_byte_buffer
=
55 env
->NewDirectByteBuffer(const_cast<char*>(text
.data()), text
.length());
56 base::android::ScopedJavaLocalRef
<jstring
> java_charset
=
57 base::android::ConvertUTF8ToJavaString(env
, base::StringPiece(charset
));
58 ScopedJavaLocalRef
<jstring
> java_result
=
59 android::Java_NetStringUtil_convertToUnicodeWithSubstitutions(
60 env
, java_byte_buffer
, java_charset
.obj());
66 const char* const kCharsetLatin1
= "ISO-8859-1";
68 bool ConvertToUtf8(const std::string
& text
, const char* charset
,
69 std::string
* output
) {
71 ScopedJavaLocalRef
<jstring
> java_result
= ConvertToJstring(text
, charset
);
72 if (java_result
.is_null())
74 *output
= base::android::ConvertJavaStringToUTF8(java_result
);
78 bool ConvertToUtf8AndNormalize(const std::string
& text
, const char* charset
,
79 std::string
* output
) {
81 ScopedJavaLocalRef
<jstring
> java_result
= ConvertToNormalizedJstring(
83 if (java_result
.is_null())
85 *output
= base::android::ConvertJavaStringToUTF8(java_result
);
89 bool ConvertToUTF16(const std::string
& text
, const char* charset
,
90 base::string16
* output
) {
92 ScopedJavaLocalRef
<jstring
> java_result
= ConvertToJstring(text
, charset
);
93 if (java_result
.is_null())
95 *output
= base::android::ConvertJavaStringToUTF16(java_result
);
99 bool ConvertToUTF16WithSubstitutions(const std::string
& text
,
101 base::string16
* output
) {
103 ScopedJavaLocalRef
<jstring
> java_result
=
104 ConvertToJstringWithSubstitutions(text
, charset
);
105 if (java_result
.is_null())
107 *output
= base::android::ConvertJavaStringToUTF16(java_result
);
111 bool RegisterNetStringUtils(JNIEnv
* env
) {
112 return android::RegisterNativesImpl(env
);