OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/android/jni_string.h" | 5 #include "base/android/jni_string.h" |
6 | 6 |
7 #include "base/android/jni_android.h" | 7 #include "base/android/jni_android.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
10 | 10 |
11 namespace base { | 11 namespace base { |
12 namespace android { | 12 namespace android { |
13 | 13 |
14 std::string ConvertJavaStringToUTF8(JNIEnv* env, jstring str) { | 14 std::string ConvertJavaStringToUTF8(JNIEnv* env, jstring str) { |
15 // JNI's GetStringUTFChars() returns strings in Java-modified UTF8, so we | 15 // JNI's GetStringUTFChars() returns strings in Java-modified UTF8, so we |
16 // instead get the String in UTF16 and convert using our own utility function. | 16 // instead get the String in UTF16 and convert using our own utility function. |
17 return UTF16ToUTF8(ConvertJavaStringToUTF16(env, str)); | 17 return UTF16ToUTF8(ConvertJavaStringToUTF16(env, str)); |
18 } | 18 } |
19 | 19 |
20 jstring ConvertUTF8ToJavaString(JNIEnv* env, const std::string& str) { | 20 jstring ConvertUTF8ToJavaString(JNIEnv* env, const base::StringPiece& str) { |
21 jstring result = env->NewStringUTF(str.c_str()); | 21 // JNI's NewStringUTF expects "modified" UTF8 so instead create the string |
22 CheckException(env); | 22 // via our own UTF16 conversion utility. |
23 return result; | 23 // Further, Dalvik requires the string passed into NewStringUTF() to come from |
| 24 // a trusted source. We can't guarantee that all UTF8 will be sanitized before |
| 25 // it gets here, so constructing via UTF16 side-steps this issue. |
| 26 // (Dalvik stores strings internally as UTF16 anyway, so there shouldn't be |
| 27 // a significant performance hit by doing it this way). |
| 28 return ConvertUTF16ToJavaString(env, UTF8ToUTF16(str)); |
24 } | 29 } |
25 | 30 |
26 string16 ConvertJavaStringToUTF16(JNIEnv* env, jstring str) { | 31 string16 ConvertJavaStringToUTF16(JNIEnv* env, jstring str) { |
27 const jchar* chars = env->GetStringChars(str, NULL); | 32 const jchar* chars = env->GetStringChars(str, NULL); |
28 DCHECK(chars); | 33 DCHECK(chars); |
29 // GetStringChars isn't required to NULL-terminate the strings | 34 // GetStringChars isn't required to NULL-terminate the strings |
30 // it returns, so the length must be explicitly checked. | 35 // it returns, so the length must be explicitly checked. |
31 string16 result(chars, env->GetStringLength(str)); | 36 string16 result(chars, env->GetStringLength(str)); |
32 env->ReleaseStringChars(str, chars); | 37 env->ReleaseStringChars(str, chars); |
33 CheckException(env); | 38 CheckException(env); |
34 return result; | 39 return result; |
35 } | 40 } |
36 | 41 |
37 jstring ConvertUTF16ToJavaString(JNIEnv* env, const string16& str) { | 42 jstring ConvertUTF16ToJavaString(JNIEnv* env, const string16& str) { |
38 jstring result = env->NewString(str.data(), str.length()); | 43 jstring result = env->NewString(str.data(), str.length()); |
39 CheckException(env); | 44 CheckException(env); |
40 return result; | 45 return result; |
41 } | 46 } |
42 | 47 |
43 } // namespace android | 48 } // namespace android |
44 } // namespace base | 49 } // namespace base |
OLD | NEW |