blob: f28f6498ece95f400f2a34981030e4693dfac22a [file] [log] [blame]
peter@chromium.orge4a43a52012-02-10 06:02:27 +09001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
michaelbai@google.com686190b2011-08-03 01:11:16 +09002// 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.org17f60622013-06-08 03:37:07 +09009#include "base/strings/utf_string_conversions.h"
michaelbai@google.com686190b2011-08-03 01:11:16 +090010
peter@chromium.orge4a43a52012-02-10 06:02:27 +090011namespace {
12
13// Internal version that does not use a scoped local pointer.
james.wei@intel.comdc733542012-06-12 12:25:44 +090014jstring ConvertUTF16ToJavaStringImpl(JNIEnv* env,
15 const base::StringPiece16& str) {
peter@chromium.orge4a43a52012-02-10 06:02:27 +090016 jstring result = env->NewString(str.data(), str.length());
17 base::android::CheckException(env);
18 return result;
19}
20
danakj651c3e22015-03-07 10:51:42 +090021} // namespace
peter@chromium.orge4a43a52012-02-10 06:02:27 +090022
michaelbai@google.com686190b2011-08-03 01:11:16 +090023namespace base {
24namespace android {
25
joth@chromium.org3a08d892012-12-14 10:31:17 +090026void ConvertJavaStringToUTF8(JNIEnv* env, jstring str, std::string* result) {
bauerbb2ac4592016-03-31 02:02:48 +090027 DCHECK(str);
tornec11bc592016-04-23 03:13:35 +090028 if (!str) {
29 LOG(WARNING) << "ConvertJavaStringToUTF8 called with null string.";
30 result->clear();
31 return;
32 }
jdduke12e68322015-08-05 05:35:59 +090033 const jsize length = env->GetStringLength(str);
34 if (!length) {
35 result->clear();
36 CheckException(env);
37 return;
38 }
joth@chromium.org3a08d892012-12-14 10:31:17 +090039 // 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);
jdduke12e68322015-08-05 05:35:59 +090044 UTF16ToUTF8(chars, length, result);
joth@chromium.org3a08d892012-12-14 10:31:17 +090045 env->ReleaseStringChars(str, chars);
46 CheckException(env);
47}
48
michaelbai@google.com686190b2011-08-03 01:11:16 +090049std::string ConvertJavaStringToUTF8(JNIEnv* env, jstring str) {
joth@chromium.org3a08d892012-12-14 10:31:17 +090050 std::string result;
51 ConvertJavaStringToUTF8(env, str, &result);
52 return result;
michaelbai@google.com686190b2011-08-03 01:11:16 +090053}
54
peter@chromium.orge4a43a52012-02-10 06:02:27 +090055std::string ConvertJavaStringToUTF8(const JavaRef<jstring>& str) {
joth@chromium.org6f067622012-03-22 22:52:57 +090056 return ConvertJavaStringToUTF8(AttachCurrentThread(), str.obj());
peter@chromium.orge4a43a52012-02-10 06:02:27 +090057}
58
torne96db31b2015-09-15 21:22:36 +090059std::string ConvertJavaStringToUTF8(JNIEnv* env, const JavaRef<jstring>& str) {
60 return ConvertJavaStringToUTF8(env, str.obj());
61}
62
peter@chromium.orge4a43a52012-02-10 06:02:27 +090063ScopedJavaLocalRef<jstring> ConvertUTF8ToJavaString(
james.wei@intel.comdc733542012-06-12 12:25:44 +090064 JNIEnv* env,
65 const base::StringPiece& str) {
joth@chromium.orged27f3a2011-09-09 19:17:35 +090066 // 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.orge4a43a52012-02-10 06:02:27 +090073 return ScopedJavaLocalRef<jstring>(env, ConvertUTF16ToJavaStringImpl(
74 env, UTF8ToUTF16(str)));
michaelbai@google.com686190b2011-08-03 01:11:16 +090075}
76
joth@chromium.org3a08d892012-12-14 10:31:17 +090077void ConvertJavaStringToUTF16(JNIEnv* env, jstring str, string16* result) {
bauerbb2ac4592016-03-31 02:02:48 +090078 DCHECK(str);
tornec11bc592016-04-23 03:13:35 +090079 if (!str) {
80 LOG(WARNING) << "ConvertJavaStringToUTF16 called with null string.";
81 result->clear();
82 return;
83 }
jdduke12e68322015-08-05 05:35:59 +090084 const jsize length = env->GetStringLength(str);
85 if (!length) {
86 result->clear();
87 CheckException(env);
88 return;
89 }
michaelbai@google.com686190b2011-08-03 01:11:16 +090090 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.
jdduke12e68322015-08-05 05:35:59 +090094 result->assign(chars, length);
michaelbai@google.com686190b2011-08-03 01:11:16 +090095 env->ReleaseStringChars(str, chars);
96 CheckException(env);
joth@chromium.org3a08d892012-12-14 10:31:17 +090097}
98
99string16 ConvertJavaStringToUTF16(JNIEnv* env, jstring str) {
100 string16 result;
101 ConvertJavaStringToUTF16(env, str, &result);
michaelbai@google.com686190b2011-08-03 01:11:16 +0900102 return result;
103}
104
peter@chromium.orge4a43a52012-02-10 06:02:27 +0900105string16 ConvertJavaStringToUTF16(const JavaRef<jstring>& str) {
joth@chromium.org6f067622012-03-22 22:52:57 +0900106 return ConvertJavaStringToUTF16(AttachCurrentThread(), str.obj());
peter@chromium.orge4a43a52012-02-10 06:02:27 +0900107}
108
torne96db31b2015-09-15 21:22:36 +0900109string16 ConvertJavaStringToUTF16(JNIEnv* env, const JavaRef<jstring>& str) {
110 return ConvertJavaStringToUTF16(env, str.obj());
111}
112
james.wei@intel.comdc733542012-06-12 12:25:44 +0900113ScopedJavaLocalRef<jstring> ConvertUTF16ToJavaString(
114 JNIEnv* env,
115 const base::StringPiece16& str) {
peter@chromium.orge4a43a52012-02-10 06:02:27 +0900116 return ScopedJavaLocalRef<jstring>(env,
117 ConvertUTF16ToJavaStringImpl(env, str));
michaelbai@google.com686190b2011-08-03 01:11:16 +0900118}
119
120} // namespace android
121} // namespace base