Removing ContentViewCore dependencies from few functions which acts as direct wrapper...
[chromium-blink-merge.git] / net / base / net_string_util_icu_alternatives_android.cc
blobca0e7f0315db7677a924b825495b0d27fbdb2df3
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 const char* const kCharsetLatin1 = "ISO-8859-1";
71 bool ConvertToUtf8(const std::string& text, const char* charset,
72 std::string* output) {
73 output->clear();
74 ScopedJavaLocalRef<jstring> java_result = ConvertToJstring(text, charset);
75 if (java_result.is_null())
76 return false;
77 *output = base::android::ConvertJavaStringToUTF8(java_result);
78 return true;
81 bool ConvertToUtf8AndNormalize(const std::string& text, const char* charset,
82 std::string* output) {
83 output->clear();
84 ScopedJavaLocalRef<jstring> java_result = ConvertToNormalizedJstring(
85 text, charset);
86 if (java_result.is_null())
87 return false;
88 *output = base::android::ConvertJavaStringToUTF8(java_result);
89 return true;
92 bool ConvertToUTF16(const std::string& text, const char* charset,
93 base::string16* output) {
94 output->clear();
95 ScopedJavaLocalRef<jstring> java_result = ConvertToJstring(text, charset);
96 if (java_result.is_null())
97 return false;
98 *output = base::android::ConvertJavaStringToUTF16(java_result);
99 return true;
102 bool ConvertToUTF16WithSubstitutions(const std::string& text,
103 const char* charset,
104 base::string16* output) {
105 output->clear();
106 ScopedJavaLocalRef<jstring> java_result =
107 ConvertToJstringWithSubstitutions(text, charset);
108 if (java_result.is_null())
109 return false;
110 *output = base::android::ConvertJavaStringToUTF16(java_result);
111 return true;
114 bool RegisterNetStringUtils(JNIEnv* env) {
115 return android::RegisterNativesImpl(env);
118 } // namespace net