1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/android/jni_string.h"
7 #include "base/android/jni_android.h"
8 #include "base/logging.h"
9 #include "base/numerics/safe_conversions.h"
10 #include "base/strings/utf_string_conversions.h"
14 // Internal version that does not use a scoped local pointer.
15 jstring ConvertUTF16ToJavaStringImpl(JNIEnv* env,
16 const base::StringPiece16& str) {
17 jstring result = env->NewString(reinterpret_cast<const jchar*>(str.data()),
18 base::checked_cast<jsize>(str.length()));
19 base::android::CheckException(env);
28 void ConvertJavaStringToUTF8(JNIEnv* env, jstring str, std::string* result) {
31 LOG(WARNING) << "ConvertJavaStringToUTF8 called with null string.";
35 const jsize length = env->GetStringLength(str);
41 // JNI's GetStringUTFChars() returns strings in Java "modified" UTF8, so
42 // instead get the String in UTF16 and convert using chromium's conversion
43 // function that yields plain (non Java-modified) UTF8.
44 const jchar* chars = env->GetStringChars(str, NULL);
46 UTF16ToUTF8(reinterpret_cast<const char16_t*>(chars),
47 static_cast<size_t>(length), result);
48 env->ReleaseStringChars(str, chars);
52 std::string ConvertJavaStringToUTF8(JNIEnv* env, jstring str) {
54 ConvertJavaStringToUTF8(env, str, &result);
58 std::string ConvertJavaStringToUTF8(const JavaRef<jstring>& str) {
59 return ConvertJavaStringToUTF8(AttachCurrentThread(), str.obj());
62 std::string ConvertJavaStringToUTF8(JNIEnv* env, const JavaRef<jstring>& str) {
63 return ConvertJavaStringToUTF8(env, str.obj());
66 ScopedJavaLocalRef<jstring> ConvertUTF8ToJavaString(JNIEnv* env,
67 const StringPiece& str) {
68 // JNI's NewStringUTF expects "modified" UTF8 so instead create the string
69 // via our own UTF16 conversion utility.
70 // Further, Dalvik requires the string passed into NewStringUTF() to come from
71 // a trusted source. We can't guarantee that all UTF8 will be sanitized before
72 // it gets here, so constructing via UTF16 side-steps this issue.
73 // (Dalvik stores strings internally as UTF16 anyway, so there shouldn't be
74 // a significant performance hit by doing it this way).
75 return ScopedJavaLocalRef<jstring>(env, ConvertUTF16ToJavaStringImpl(
76 env, UTF8ToUTF16(str)));
79 void ConvertJavaStringToUTF16(JNIEnv* env,
81 std::u16string* result) {
84 LOG(WARNING) << "ConvertJavaStringToUTF16 called with null string.";
88 const jsize length = env->GetStringLength(str);
94 const jchar* chars = env->GetStringChars(str, NULL);
96 // GetStringChars isn't required to NULL-terminate the strings
97 // it returns, so the length must be explicitly checked.
98 result->assign(reinterpret_cast<const char16_t*>(chars),
99 static_cast<size_t>(length));
100 env->ReleaseStringChars(str, chars);
104 std::u16string ConvertJavaStringToUTF16(JNIEnv* env, jstring str) {
105 std::u16string result;
106 ConvertJavaStringToUTF16(env, str, &result);
110 std::u16string ConvertJavaStringToUTF16(const JavaRef<jstring>& str) {
111 return ConvertJavaStringToUTF16(AttachCurrentThread(), str.obj());
114 std::u16string ConvertJavaStringToUTF16(JNIEnv* env,
115 const JavaRef<jstring>& str) {
116 return ConvertJavaStringToUTF16(env, str.obj());
119 ScopedJavaLocalRef<jstring> ConvertUTF16ToJavaString(JNIEnv* env,
120 const StringPiece16& str) {
121 return ScopedJavaLocalRef<jstring>(env,
122 ConvertUTF16ToJavaStringImpl(env, str));
125 } // namespace android