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 const char* const kCharsetLatin1
= "ISO-8859-1";
71 bool ConvertToUtf8(const std::string
& text
, const char* charset
,
72 std::string
* output
) {
74 ScopedJavaLocalRef
<jstring
> java_result
= ConvertToJstring(text
, charset
);
75 if (java_result
.is_null())
77 *output
= base::android::ConvertJavaStringToUTF8(java_result
);
81 bool ConvertToUtf8AndNormalize(const std::string
& text
, const char* charset
,
82 std::string
* output
) {
84 ScopedJavaLocalRef
<jstring
> java_result
= ConvertToNormalizedJstring(
86 if (java_result
.is_null())
88 *output
= base::android::ConvertJavaStringToUTF8(java_result
);
92 bool ConvertToUTF16(const std::string
& text
, const char* charset
,
93 base::string16
* output
) {
95 ScopedJavaLocalRef
<jstring
> java_result
= ConvertToJstring(text
, charset
);
96 if (java_result
.is_null())
98 *output
= base::android::ConvertJavaStringToUTF16(java_result
);
102 bool ConvertToUTF16WithSubstitutions(const std::string
& text
,
104 base::string16
* output
) {
106 ScopedJavaLocalRef
<jstring
> java_result
=
107 ConvertToJstringWithSubstitutions(text
, charset
);
108 if (java_result
.is_null())
110 *output
= base::android::ConvertJavaStringToUTF16(java_result
);
114 bool RegisterNetStringUtils(JNIEnv
* env
) {
115 return android::RegisterNativesImpl(env
);