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 DCHECK(str); | 27 DCHECK(str); |
28 if (!str) { | |
29 LOG(WARNING) << "ConvertJavaStringToUTF8 called with null string."; | |
Bernhard Bauer
2016/04/22 20:29:17
FWIW, I probably would have done this with a NOTRE
| |
30 result->clear(); | |
31 return; | |
32 } | |
28 const jsize length = env->GetStringLength(str); | 33 const jsize length = env->GetStringLength(str); |
29 if (!length) { | 34 if (!length) { |
30 result->clear(); | 35 result->clear(); |
31 CheckException(env); | 36 CheckException(env); |
32 return; | 37 return; |
33 } | 38 } |
34 // JNI's GetStringUTFChars() returns strings in Java "modified" UTF8, so | 39 // JNI's GetStringUTFChars() returns strings in Java "modified" UTF8, so |
35 // instead get the String in UTF16 and convert using chromium's conversion | 40 // instead get the String in UTF16 and convert using chromium's conversion |
36 // function that yields plain (non Java-modified) UTF8. | 41 // function that yields plain (non Java-modified) UTF8. |
37 const jchar* chars = env->GetStringChars(str, NULL); | 42 const jchar* chars = env->GetStringChars(str, NULL); |
(...skipping 26 matching lines...) Expand all Loading... | |
64 // a trusted source. We can't guarantee that all UTF8 will be sanitized before | 69 // 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. | 70 // it gets here, so constructing via UTF16 side-steps this issue. |
66 // (Dalvik stores strings internally as UTF16 anyway, so there shouldn't be | 71 // (Dalvik stores strings internally as UTF16 anyway, so there shouldn't be |
67 // a significant performance hit by doing it this way). | 72 // a significant performance hit by doing it this way). |
68 return ScopedJavaLocalRef<jstring>(env, ConvertUTF16ToJavaStringImpl( | 73 return ScopedJavaLocalRef<jstring>(env, ConvertUTF16ToJavaStringImpl( |
69 env, UTF8ToUTF16(str))); | 74 env, UTF8ToUTF16(str))); |
70 } | 75 } |
71 | 76 |
72 void ConvertJavaStringToUTF16(JNIEnv* env, jstring str, string16* result) { | 77 void ConvertJavaStringToUTF16(JNIEnv* env, jstring str, string16* result) { |
73 DCHECK(str); | 78 DCHECK(str); |
79 if (!str) { | |
80 LOG(WARNING) << "ConvertJavaStringToUTF16 called with null string."; | |
81 result->clear(); | |
82 return; | |
83 } | |
74 const jsize length = env->GetStringLength(str); | 84 const jsize length = env->GetStringLength(str); |
75 if (!length) { | 85 if (!length) { |
76 result->clear(); | 86 result->clear(); |
77 CheckException(env); | 87 CheckException(env); |
78 return; | 88 return; |
79 } | 89 } |
80 const jchar* chars = env->GetStringChars(str, NULL); | 90 const jchar* chars = env->GetStringChars(str, NULL); |
81 DCHECK(chars); | 91 DCHECK(chars); |
82 // GetStringChars isn't required to NULL-terminate the strings | 92 // GetStringChars isn't required to NULL-terminate the strings |
83 // it returns, so the length must be explicitly checked. | 93 // it returns, so the length must be explicitly checked. |
(...skipping 18 matching lines...) Expand all Loading... | |
102 | 112 |
103 ScopedJavaLocalRef<jstring> ConvertUTF16ToJavaString( | 113 ScopedJavaLocalRef<jstring> ConvertUTF16ToJavaString( |
104 JNIEnv* env, | 114 JNIEnv* env, |
105 const base::StringPiece16& str) { | 115 const base::StringPiece16& str) { |
106 return ScopedJavaLocalRef<jstring>(env, | 116 return ScopedJavaLocalRef<jstring>(env, |
107 ConvertUTF16ToJavaStringImpl(env, str)); | 117 ConvertUTF16ToJavaStringImpl(env, str)); |
108 } | 118 } |
109 | 119 |
110 } // namespace android | 120 } // namespace android |
111 } // namespace base | 121 } // namespace base |
OLD | NEW |