1 // Copyright (c) 2012 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 "base/android/jni_string.h"
7 #include "base/android/jni_android.h"
8 #include "base/logging.h"
9 #include "base/strings/utf_string_conversions.h"
13 // Internal version that does not use a scoped local pointer.
14 jstring
ConvertUTF16ToJavaStringImpl(JNIEnv
* env
,
15 const base::StringPiece16
& str
) {
16 jstring result
= env
->NewString(str
.data(), str
.length());
17 base::android::CheckException(env
);
26 void ConvertJavaStringToUTF8(JNIEnv
* env
, jstring str
, std::string
* result
) {
28 LOG(WARNING
) << "ConvertJavaStringToUTF8 called with null string.";
32 const jsize length
= env
->GetStringLength(str
);
38 // JNI's GetStringUTFChars() returns strings in Java "modified" UTF8, so
39 // instead get the String in UTF16 and convert using chromium's conversion
40 // function that yields plain (non Java-modified) UTF8.
41 const jchar
* chars
= env
->GetStringChars(str
, NULL
);
43 UTF16ToUTF8(chars
, length
, result
);
44 env
->ReleaseStringChars(str
, chars
);
48 std::string
ConvertJavaStringToUTF8(JNIEnv
* env
, jstring str
) {
50 ConvertJavaStringToUTF8(env
, str
, &result
);
54 std::string
ConvertJavaStringToUTF8(const JavaRef
<jstring
>& str
) {
55 return ConvertJavaStringToUTF8(AttachCurrentThread(), str
.obj());
58 ScopedJavaLocalRef
<jstring
> ConvertUTF8ToJavaString(
60 const base::StringPiece
& str
) {
61 // JNI's NewStringUTF expects "modified" UTF8 so instead create the string
62 // via our own UTF16 conversion utility.
63 // Further, Dalvik requires the string passed into NewStringUTF() to come from
64 // a trusted source. We can't guarantee that all UTF8 will be sanitized before
65 // it gets here, so constructing via UTF16 side-steps this issue.
66 // (Dalvik stores strings internally as UTF16 anyway, so there shouldn't be
67 // a significant performance hit by doing it this way).
68 return ScopedJavaLocalRef
<jstring
>(env
, ConvertUTF16ToJavaStringImpl(
69 env
, UTF8ToUTF16(str
)));
72 void ConvertJavaStringToUTF16(JNIEnv
* env
, jstring str
, string16
* result
) {
74 LOG(WARNING
) << "ConvertJavaStringToUTF16 called with null string.";
78 const jsize length
= env
->GetStringLength(str
);
84 const jchar
* chars
= env
->GetStringChars(str
, NULL
);
86 // GetStringChars isn't required to NULL-terminate the strings
87 // it returns, so the length must be explicitly checked.
88 result
->assign(chars
, length
);
89 env
->ReleaseStringChars(str
, chars
);
93 string16
ConvertJavaStringToUTF16(JNIEnv
* env
, jstring str
) {
95 ConvertJavaStringToUTF16(env
, str
, &result
);
99 string16
ConvertJavaStringToUTF16(const JavaRef
<jstring
>& str
) {
100 return ConvertJavaStringToUTF16(AttachCurrentThread(), str
.obj());
103 ScopedJavaLocalRef
<jstring
> ConvertUTF16ToJavaString(
105 const base::StringPiece16
& str
) {
106 return ScopedJavaLocalRef
<jstring
>(env
,
107 ConvertUTF16ToJavaStringImpl(env
, str
));
110 } // namespace android