We started redesigning GpuMemoryBuffer interface to handle multiple buffers [0].
[chromium-blink-merge.git] / net / base / net_string_util_icu_alternatives_android.cc
blob0551a03c565a883aecbbc7728e12dceea0e3549f
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"
14 namespace net {
16 namespace {
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(
24 env,
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(),
30 java_charset.obj());
31 return java_result;
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
36 // on failure.
37 ScopedJavaLocalRef<jstring> ConvertToNormalizedJstring(
38 const std::string& text, const char* charset) {
39 JNIEnv* env = base::android::AttachCurrentThread();
40 ScopedJavaLocalRef<jobject> java_byte_buffer(
41 env,
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());
48 return java_result;
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(
57 env,
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());
64 return java_result;
67 } // namespace
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) {
75 output->clear();
76 ScopedJavaLocalRef<jstring> java_result = ConvertToJstring(text, charset);
77 if (java_result.is_null())
78 return false;
79 *output = base::android::ConvertJavaStringToUTF8(java_result);
80 return true;
83 bool ConvertToUtf8AndNormalize(const std::string& text, const char* charset,
84 std::string* output) {
85 output->clear();
86 ScopedJavaLocalRef<jstring> java_result = ConvertToNormalizedJstring(
87 text, charset);
88 if (java_result.is_null())
89 return false;
90 *output = base::android::ConvertJavaStringToUTF8(java_result);
91 return true;
94 bool ConvertToUTF16(const std::string& text, const char* charset,
95 base::string16* output) {
96 output->clear();
97 ScopedJavaLocalRef<jstring> java_result = ConvertToJstring(text, charset);
98 if (java_result.is_null())
99 return false;
100 *output = base::android::ConvertJavaStringToUTF16(java_result);
101 return true;
104 bool ConvertToUTF16WithSubstitutions(const std::string& text,
105 const char* charset,
106 base::string16* output) {
107 output->clear();
108 ScopedJavaLocalRef<jstring> java_result =
109 ConvertToJstringWithSubstitutions(text, charset);
110 if (java_result.is_null())
111 return false;
112 *output = base::android::ConvertJavaStringToUTF16(java_result);
113 return true;
116 bool RegisterNetStringUtils(JNIEnv* env) {
117 return android::RegisterNativesImpl(env);
120 } // namespace net