Cleanup setting of 'sysroot' in common.gypi
[chromium-blink-merge.git] / base / android / jni_string.cc
blob0bc36597d75c34adf2b1fa5fdff55aa329329fe3
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
3 // found in the LICENSE file.
5 #include "base/android/jni_string.h"
7 #include "base/android/jni_android.h"
8 #include "base/logging.h"
9 #include "base/strings/utf_string_conversions.h"
11 namespace {
13 // Internal version that does not use a scoped local pointer.
14 jstring ConvertUTF16ToJavaStringImpl(JNIEnv* env,
15 const base::StringPiece16& str) {
16 jstring result = env->NewString(str.data(), str.length());
17 base::android::CheckException(env);
18 return result;
21 } // namespace
23 namespace base {
24 namespace android {
26 void ConvertJavaStringToUTF8(JNIEnv* env, jstring str, std::string* result) {
27 if (!str) {
28 LOG(WARNING) << "ConvertJavaStringToUTF8 called with null string.";
29 result->clear();
30 return;
32 const jsize length = env->GetStringLength(str);
33 if (!length) {
34 result->clear();
35 CheckException(env);
36 return;
38 // JNI's GetStringUTFChars() returns strings in Java "modified" UTF8, so
39 // instead get the String in UTF16 and convert using chromium's conversion
40 // function that yields plain (non Java-modified) UTF8.
41 const jchar* chars = env->GetStringChars(str, NULL);
42 DCHECK(chars);
43 UTF16ToUTF8(chars, length, result);
44 env->ReleaseStringChars(str, chars);
45 CheckException(env);
48 std::string ConvertJavaStringToUTF8(JNIEnv* env, jstring str) {
49 std::string result;
50 ConvertJavaStringToUTF8(env, str, &result);
51 return result;
54 std::string ConvertJavaStringToUTF8(const JavaRef<jstring>& str) {
55 return ConvertJavaStringToUTF8(AttachCurrentThread(), str.obj());
58 std::string ConvertJavaStringToUTF8(JNIEnv* env, const JavaRef<jstring>& str) {
59 return ConvertJavaStringToUTF8(env, str.obj());
62 ScopedJavaLocalRef<jstring> ConvertUTF8ToJavaString(
63 JNIEnv* env,
64 const base::StringPiece& str) {
65 // JNI's NewStringUTF expects "modified" UTF8 so instead create the string
66 // via our own UTF16 conversion utility.
67 // 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
69 // it gets here, so constructing via UTF16 side-steps this issue.
70 // (Dalvik stores strings internally as UTF16 anyway, so there shouldn't be
71 // a significant performance hit by doing it this way).
72 return ScopedJavaLocalRef<jstring>(env, ConvertUTF16ToJavaStringImpl(
73 env, UTF8ToUTF16(str)));
76 void ConvertJavaStringToUTF16(JNIEnv* env, jstring str, string16* result) {
77 if (!str) {
78 LOG(WARNING) << "ConvertJavaStringToUTF16 called with null string.";
79 result->clear();
80 return;
82 const jsize length = env->GetStringLength(str);
83 if (!length) {
84 result->clear();
85 CheckException(env);
86 return;
88 const jchar* chars = env->GetStringChars(str, NULL);
89 DCHECK(chars);
90 // GetStringChars isn't required to NULL-terminate the strings
91 // it returns, so the length must be explicitly checked.
92 result->assign(chars, length);
93 env->ReleaseStringChars(str, chars);
94 CheckException(env);
97 string16 ConvertJavaStringToUTF16(JNIEnv* env, jstring str) {
98 string16 result;
99 ConvertJavaStringToUTF16(env, str, &result);
100 return result;
103 string16 ConvertJavaStringToUTF16(const JavaRef<jstring>& str) {
104 return ConvertJavaStringToUTF16(AttachCurrentThread(), str.obj());
107 string16 ConvertJavaStringToUTF16(JNIEnv* env, const JavaRef<jstring>& str) {
108 return ConvertJavaStringToUTF16(env, str.obj());
111 ScopedJavaLocalRef<jstring> ConvertUTF16ToJavaString(
112 JNIEnv* env,
113 const base::StringPiece16& str) {
114 return ScopedJavaLocalRef<jstring>(env,
115 ConvertUTF16ToJavaStringImpl(env, str));
118 } // namespace android
119 } // namespace base