Revert of JavaScript unit test framework for Chrome remote desktop - Part II (https...
[chromium-blink-merge.git] / net / base / net_string_util_icu_alternatives_android.cc
blobca0630328f5d8e5bfb96c3890945327d1210636d
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 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,
29 java_charset.obj());
30 return java_result;
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
35 // on failure.
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());
46 return java_result;
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());
61 return java_result;
64 } // namespace
66 const char* const kCharsetLatin1 = "ISO-8859-1";
68 bool ConvertToUtf8(const std::string& text, const char* charset,
69 std::string* output) {
70 output->clear();
71 ScopedJavaLocalRef<jstring> java_result = ConvertToJstring(text, charset);
72 if (java_result.is_null())
73 return false;
74 *output = base::android::ConvertJavaStringToUTF8(java_result);
75 return true;
78 bool ConvertToUtf8AndNormalize(const std::string& text, const char* charset,
79 std::string* output) {
80 output->clear();
81 ScopedJavaLocalRef<jstring> java_result = ConvertToNormalizedJstring(
82 text, charset);
83 if (java_result.is_null())
84 return false;
85 *output = base::android::ConvertJavaStringToUTF8(java_result);
86 return true;
89 bool ConvertToUTF16(const std::string& text, const char* charset,
90 base::string16* output) {
91 output->clear();
92 ScopedJavaLocalRef<jstring> java_result = ConvertToJstring(text, charset);
93 if (java_result.is_null())
94 return false;
95 *output = base::android::ConvertJavaStringToUTF16(java_result);
96 return true;
99 bool ConvertToUTF16WithSubstitutions(const std::string& text,
100 const char* charset,
101 base::string16* output) {
102 output->clear();
103 ScopedJavaLocalRef<jstring> java_result =
104 ConvertToJstringWithSubstitutions(text, charset);
105 if (java_result.is_null())
106 return false;
107 *output = base::android::ConvertJavaStringToUTF16(java_result);
108 return true;
111 bool RegisterNetStringUtils(JNIEnv* env) {
112 return android::RegisterNativesImpl(env);
115 } // namespace net