| 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/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
| 10 | 10 |
| (...skipping 30 matching lines...) Expand all Loading... |
| 41 // Further, Dalvik requires the string passed into NewStringUTF() to come from | 41 // Further, Dalvik requires the string passed into NewStringUTF() to come from |
| 42 // a trusted source. We can't guarantee that all UTF8 will be sanitized before | 42 // a trusted source. We can't guarantee that all UTF8 will be sanitized before |
| 43 // it gets here, so constructing via UTF16 side-steps this issue. | 43 // it gets here, so constructing via UTF16 side-steps this issue. |
| 44 // (Dalvik stores strings internally as UTF16 anyway, so there shouldn't be | 44 // (Dalvik stores strings internally as UTF16 anyway, so there shouldn't be |
| 45 // a significant performance hit by doing it this way). | 45 // a significant performance hit by doing it this way). |
| 46 return ScopedJavaLocalRef<jstring>(env, ConvertUTF16ToJavaStringImpl( | 46 return ScopedJavaLocalRef<jstring>(env, ConvertUTF16ToJavaStringImpl( |
| 47 env, UTF8ToUTF16(str))); | 47 env, UTF8ToUTF16(str))); |
| 48 } | 48 } |
| 49 | 49 |
| 50 string16 ConvertJavaStringToUTF16(JNIEnv* env, jstring str) { | 50 string16 ConvertJavaStringToUTF16(JNIEnv* env, jstring str) { |
| 51 if (!str) { |
| 52 LOG(WARNING) << " ConvertJavaStringToUTF16 called with null string."; |
| 53 return string16(); |
| 54 } |
| 51 const jchar* chars = env->GetStringChars(str, NULL); | 55 const jchar* chars = env->GetStringChars(str, NULL); |
| 52 DCHECK(chars); | 56 DCHECK(chars); |
| 53 // GetStringChars isn't required to NULL-terminate the strings | 57 // GetStringChars isn't required to NULL-terminate the strings |
| 54 // it returns, so the length must be explicitly checked. | 58 // it returns, so the length must be explicitly checked. |
| 55 string16 result(chars, env->GetStringLength(str)); | 59 string16 result(chars, env->GetStringLength(str)); |
| 56 env->ReleaseStringChars(str, chars); | 60 env->ReleaseStringChars(str, chars); |
| 57 CheckException(env); | 61 CheckException(env); |
| 58 return result; | 62 return result; |
| 59 } | 63 } |
| 60 | 64 |
| 61 string16 ConvertJavaStringToUTF16(const JavaRef<jstring>& str) { | 65 string16 ConvertJavaStringToUTF16(const JavaRef<jstring>& str) { |
| 62 return ConvertJavaStringToUTF16(AttachCurrentThread(), str.obj()); | 66 return ConvertJavaStringToUTF16(AttachCurrentThread(), str.obj()); |
| 63 } | 67 } |
| 64 | 68 |
| 65 ScopedJavaLocalRef<jstring> ConvertUTF16ToJavaString( | 69 ScopedJavaLocalRef<jstring> ConvertUTF16ToJavaString( |
| 66 JNIEnv* env, | 70 JNIEnv* env, |
| 67 const base::StringPiece16& str) { | 71 const base::StringPiece16& str) { |
| 68 return ScopedJavaLocalRef<jstring>(env, | 72 return ScopedJavaLocalRef<jstring>(env, |
| 69 ConvertUTF16ToJavaStringImpl(env, str)); | 73 ConvertUTF16ToJavaStringImpl(env, str)); |
| 70 } | 74 } |
| 71 | 75 |
| 72 } // namespace android | 76 } // namespace android |
| 73 } // namespace base | 77 } // namespace base |
| OLD | NEW |