peter@chromium.org | e4a43a5 | 2012-02-10 06:02:27 +0900 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
michaelbai@google.com | 686190b | 2011-08-03 01:11:16 +0900 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "base/android/jni_string.h" |
| 6 | |
| 7 | #include "base/android/jni_android.h" |
| 8 | #include "base/logging.h" |
avi@chromium.org | 17f6062 | 2013-06-08 03:37:07 +0900 | [diff] [blame] | 9 | #include "base/strings/utf_string_conversions.h" |
michaelbai@google.com | 686190b | 2011-08-03 01:11:16 +0900 | [diff] [blame] | 10 | |
peter@chromium.org | e4a43a5 | 2012-02-10 06:02:27 +0900 | [diff] [blame] | 11 | namespace { |
| 12 | |
| 13 | // Internal version that does not use a scoped local pointer. |
james.wei@intel.com | dc73354 | 2012-06-12 12:25:44 +0900 | [diff] [blame] | 14 | jstring ConvertUTF16ToJavaStringImpl(JNIEnv* env, |
| 15 | const base::StringPiece16& str) { |
peter@chromium.org | e4a43a5 | 2012-02-10 06:02:27 +0900 | [diff] [blame] | 16 | jstring result = env->NewString(str.data(), str.length()); |
| 17 | base::android::CheckException(env); |
| 18 | return result; |
| 19 | } |
| 20 | |
danakj | 651c3e2 | 2015-03-07 10:51:42 +0900 | [diff] [blame] | 21 | } // namespace |
peter@chromium.org | e4a43a5 | 2012-02-10 06:02:27 +0900 | [diff] [blame] | 22 | |
michaelbai@google.com | 686190b | 2011-08-03 01:11:16 +0900 | [diff] [blame] | 23 | namespace base { |
| 24 | namespace android { |
| 25 | |
joth@chromium.org | 3a08d89 | 2012-12-14 10:31:17 +0900 | [diff] [blame] | 26 | void ConvertJavaStringToUTF8(JNIEnv* env, jstring str, std::string* result) { |
bauerb | b2ac459 | 2016-03-31 02:02:48 +0900 | [diff] [blame] | 27 | DCHECK(str); |
torne | c11bc59 | 2016-04-23 03:13:35 +0900 | [diff] [blame] | 28 | if (!str) { |
| 29 | LOG(WARNING) << "ConvertJavaStringToUTF8 called with null string."; |
| 30 | result->clear(); |
| 31 | return; |
| 32 | } |
jdduke | 12e6832 | 2015-08-05 05:35:59 +0900 | [diff] [blame] | 33 | const jsize length = env->GetStringLength(str); |
| 34 | if (!length) { |
| 35 | result->clear(); |
| 36 | CheckException(env); |
| 37 | return; |
| 38 | } |
joth@chromium.org | 3a08d89 | 2012-12-14 10:31:17 +0900 | [diff] [blame] | 39 | // JNI's GetStringUTFChars() returns strings in Java "modified" UTF8, so |
| 40 | // instead get the String in UTF16 and convert using chromium's conversion |
| 41 | // function that yields plain (non Java-modified) UTF8. |
| 42 | const jchar* chars = env->GetStringChars(str, NULL); |
| 43 | DCHECK(chars); |
jdduke | 12e6832 | 2015-08-05 05:35:59 +0900 | [diff] [blame] | 44 | UTF16ToUTF8(chars, length, result); |
joth@chromium.org | 3a08d89 | 2012-12-14 10:31:17 +0900 | [diff] [blame] | 45 | env->ReleaseStringChars(str, chars); |
| 46 | CheckException(env); |
| 47 | } |
| 48 | |
michaelbai@google.com | 686190b | 2011-08-03 01:11:16 +0900 | [diff] [blame] | 49 | std::string ConvertJavaStringToUTF8(JNIEnv* env, jstring str) { |
joth@chromium.org | 3a08d89 | 2012-12-14 10:31:17 +0900 | [diff] [blame] | 50 | std::string result; |
| 51 | ConvertJavaStringToUTF8(env, str, &result); |
| 52 | return result; |
michaelbai@google.com | 686190b | 2011-08-03 01:11:16 +0900 | [diff] [blame] | 53 | } |
| 54 | |
peter@chromium.org | e4a43a5 | 2012-02-10 06:02:27 +0900 | [diff] [blame] | 55 | std::string ConvertJavaStringToUTF8(const JavaRef<jstring>& str) { |
joth@chromium.org | 6f06762 | 2012-03-22 22:52:57 +0900 | [diff] [blame] | 56 | return ConvertJavaStringToUTF8(AttachCurrentThread(), str.obj()); |
peter@chromium.org | e4a43a5 | 2012-02-10 06:02:27 +0900 | [diff] [blame] | 57 | } |
| 58 | |
torne | 96db31b | 2015-09-15 21:22:36 +0900 | [diff] [blame] | 59 | std::string ConvertJavaStringToUTF8(JNIEnv* env, const JavaRef<jstring>& str) { |
| 60 | return ConvertJavaStringToUTF8(env, str.obj()); |
| 61 | } |
| 62 | |
peter@chromium.org | e4a43a5 | 2012-02-10 06:02:27 +0900 | [diff] [blame] | 63 | ScopedJavaLocalRef<jstring> ConvertUTF8ToJavaString( |
james.wei@intel.com | dc73354 | 2012-06-12 12:25:44 +0900 | [diff] [blame] | 64 | JNIEnv* env, |
| 65 | const base::StringPiece& str) { |
joth@chromium.org | ed27f3a | 2011-09-09 19:17:35 +0900 | [diff] [blame] | 66 | // JNI's NewStringUTF expects "modified" UTF8 so instead create the string |
| 67 | // via our own UTF16 conversion utility. |
| 68 | // Further, Dalvik requires the string passed into NewStringUTF() to come from |
| 69 | // a trusted source. We can't guarantee that all UTF8 will be sanitized before |
| 70 | // it gets here, so constructing via UTF16 side-steps this issue. |
| 71 | // (Dalvik stores strings internally as UTF16 anyway, so there shouldn't be |
| 72 | // a significant performance hit by doing it this way). |
peter@chromium.org | e4a43a5 | 2012-02-10 06:02:27 +0900 | [diff] [blame] | 73 | return ScopedJavaLocalRef<jstring>(env, ConvertUTF16ToJavaStringImpl( |
| 74 | env, UTF8ToUTF16(str))); |
michaelbai@google.com | 686190b | 2011-08-03 01:11:16 +0900 | [diff] [blame] | 75 | } |
| 76 | |
joth@chromium.org | 3a08d89 | 2012-12-14 10:31:17 +0900 | [diff] [blame] | 77 | void ConvertJavaStringToUTF16(JNIEnv* env, jstring str, string16* result) { |
bauerb | b2ac459 | 2016-03-31 02:02:48 +0900 | [diff] [blame] | 78 | DCHECK(str); |
torne | c11bc59 | 2016-04-23 03:13:35 +0900 | [diff] [blame] | 79 | if (!str) { |
| 80 | LOG(WARNING) << "ConvertJavaStringToUTF16 called with null string."; |
| 81 | result->clear(); |
| 82 | return; |
| 83 | } |
jdduke | 12e6832 | 2015-08-05 05:35:59 +0900 | [diff] [blame] | 84 | const jsize length = env->GetStringLength(str); |
| 85 | if (!length) { |
| 86 | result->clear(); |
| 87 | CheckException(env); |
| 88 | return; |
| 89 | } |
michaelbai@google.com | 686190b | 2011-08-03 01:11:16 +0900 | [diff] [blame] | 90 | const jchar* chars = env->GetStringChars(str, NULL); |
| 91 | DCHECK(chars); |
| 92 | // GetStringChars isn't required to NULL-terminate the strings |
| 93 | // it returns, so the length must be explicitly checked. |
jdduke | 12e6832 | 2015-08-05 05:35:59 +0900 | [diff] [blame] | 94 | result->assign(chars, length); |
michaelbai@google.com | 686190b | 2011-08-03 01:11:16 +0900 | [diff] [blame] | 95 | env->ReleaseStringChars(str, chars); |
| 96 | CheckException(env); |
joth@chromium.org | 3a08d89 | 2012-12-14 10:31:17 +0900 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | string16 ConvertJavaStringToUTF16(JNIEnv* env, jstring str) { |
| 100 | string16 result; |
| 101 | ConvertJavaStringToUTF16(env, str, &result); |
michaelbai@google.com | 686190b | 2011-08-03 01:11:16 +0900 | [diff] [blame] | 102 | return result; |
| 103 | } |
| 104 | |
peter@chromium.org | e4a43a5 | 2012-02-10 06:02:27 +0900 | [diff] [blame] | 105 | string16 ConvertJavaStringToUTF16(const JavaRef<jstring>& str) { |
joth@chromium.org | 6f06762 | 2012-03-22 22:52:57 +0900 | [diff] [blame] | 106 | return ConvertJavaStringToUTF16(AttachCurrentThread(), str.obj()); |
peter@chromium.org | e4a43a5 | 2012-02-10 06:02:27 +0900 | [diff] [blame] | 107 | } |
| 108 | |
torne | 96db31b | 2015-09-15 21:22:36 +0900 | [diff] [blame] | 109 | string16 ConvertJavaStringToUTF16(JNIEnv* env, const JavaRef<jstring>& str) { |
| 110 | return ConvertJavaStringToUTF16(env, str.obj()); |
| 111 | } |
| 112 | |
james.wei@intel.com | dc73354 | 2012-06-12 12:25:44 +0900 | [diff] [blame] | 113 | ScopedJavaLocalRef<jstring> ConvertUTF16ToJavaString( |
| 114 | JNIEnv* env, |
| 115 | const base::StringPiece16& str) { |
peter@chromium.org | e4a43a5 | 2012-02-10 06:02:27 +0900 | [diff] [blame] | 116 | return ScopedJavaLocalRef<jstring>(env, |
| 117 | ConvertUTF16ToJavaStringImpl(env, str)); |
michaelbai@google.com | 686190b | 2011-08-03 01:11:16 +0900 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | } // namespace android |
| 121 | } // namespace base |