OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
10 | 10 |
11 namespace { | 11 namespace { |
12 | 12 |
13 // Internal version that does not use a scoped local pointer. | 13 // Internal version that does not use a scoped local pointer. |
14 jstring ConvertUTF16ToJavaStringImpl(JNIEnv* env, | 14 jstring ConvertUTF16ToJavaStringImpl(JNIEnv* env, |
15 const base::StringPiece16& str) { | 15 const base::StringPiece16& str) { |
16 jstring result = env->NewString(str.data(), str.length()); | 16 jstring result = env->NewString(str.data(), str.length()); |
17 base::android::CheckException(env); | 17 base::android::CheckException(env); |
18 return result; | 18 return result; |
19 } | 19 } |
20 | 20 |
21 } // namespace | 21 } // namespace |
22 | 22 |
23 namespace base { | 23 namespace base { |
24 namespace android { | 24 namespace android { |
25 | 25 |
26 void ConvertJavaStringToUTF8(JNIEnv* env, jstring str, std::string* result) { | 26 void ConvertJavaStringToUTF8(JNIEnv* env, jstring str, std::string* result) { |
27 if (!str) { | 27 DCHECK(str); |
28 LOG(WARNING) << "ConvertJavaStringToUTF8 called with null string."; | |
29 result->clear(); | |
30 return; | |
31 } | |
32 const jsize length = env->GetStringLength(str); | 28 const jsize length = env->GetStringLength(str); |
33 if (!length) { | 29 if (!length) { |
34 result->clear(); | 30 result->clear(); |
35 CheckException(env); | 31 CheckException(env); |
36 return; | 32 return; |
37 } | 33 } |
38 // JNI's GetStringUTFChars() returns strings in Java "modified" UTF8, so | 34 // JNI's GetStringUTFChars() returns strings in Java "modified" UTF8, so |
39 // instead get the String in UTF16 and convert using chromium's conversion | 35 // instead get the String in UTF16 and convert using chromium's conversion |
40 // function that yields plain (non Java-modified) UTF8. | 36 // function that yields plain (non Java-modified) UTF8. |
41 const jchar* chars = env->GetStringChars(str, NULL); | 37 const jchar* chars = env->GetStringChars(str, NULL); |
(...skipping 25 matching lines...) Expand all Loading... |
67 // Further, Dalvik requires the string passed into NewStringUTF() to come from | 63 // Further, Dalvik requires the string passed into NewStringUTF() to come from |
68 // a trusted source. We can't guarantee that all UTF8 will be sanitized before | 64 // a trusted source. We can't guarantee that all UTF8 will be sanitized before |
69 // it gets here, so constructing via UTF16 side-steps this issue. | 65 // it gets here, so constructing via UTF16 side-steps this issue. |
70 // (Dalvik stores strings internally as UTF16 anyway, so there shouldn't be | 66 // (Dalvik stores strings internally as UTF16 anyway, so there shouldn't be |
71 // a significant performance hit by doing it this way). | 67 // a significant performance hit by doing it this way). |
72 return ScopedJavaLocalRef<jstring>(env, ConvertUTF16ToJavaStringImpl( | 68 return ScopedJavaLocalRef<jstring>(env, ConvertUTF16ToJavaStringImpl( |
73 env, UTF8ToUTF16(str))); | 69 env, UTF8ToUTF16(str))); |
74 } | 70 } |
75 | 71 |
76 void ConvertJavaStringToUTF16(JNIEnv* env, jstring str, string16* result) { | 72 void ConvertJavaStringToUTF16(JNIEnv* env, jstring str, string16* result) { |
77 if (!str) { | 73 DCHECK(str); |
78 LOG(WARNING) << "ConvertJavaStringToUTF16 called with null string."; | |
79 result->clear(); | |
80 return; | |
81 } | |
82 const jsize length = env->GetStringLength(str); | 74 const jsize length = env->GetStringLength(str); |
83 if (!length) { | 75 if (!length) { |
84 result->clear(); | 76 result->clear(); |
85 CheckException(env); | 77 CheckException(env); |
86 return; | 78 return; |
87 } | 79 } |
88 const jchar* chars = env->GetStringChars(str, NULL); | 80 const jchar* chars = env->GetStringChars(str, NULL); |
89 DCHECK(chars); | 81 DCHECK(chars); |
90 // GetStringChars isn't required to NULL-terminate the strings | 82 // GetStringChars isn't required to NULL-terminate the strings |
91 // it returns, so the length must be explicitly checked. | 83 // it returns, so the length must be explicitly checked. |
(...skipping 18 matching lines...) Expand all Loading... |
110 | 102 |
111 ScopedJavaLocalRef<jstring> ConvertUTF16ToJavaString( | 103 ScopedJavaLocalRef<jstring> ConvertUTF16ToJavaString( |
112 JNIEnv* env, | 104 JNIEnv* env, |
113 const base::StringPiece16& str) { | 105 const base::StringPiece16& str) { |
114 return ScopedJavaLocalRef<jstring>(env, | 106 return ScopedJavaLocalRef<jstring>(env, |
115 ConvertUTF16ToJavaStringImpl(env, str)); | 107 ConvertUTF16ToJavaStringImpl(env, str)); |
116 } | 108 } |
117 | 109 |
118 } // namespace android | 110 } // namespace android |
119 } // namespace base | 111 } // namespace base |
OLD | NEW |