1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96

base / android / jni_string.h [blame]

// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef BASE_ANDROID_JNI_STRING_H_
#define BASE_ANDROID_JNI_STRING_H_

#include <jni.h>

#include <string>
#include <string_view>

#include "base/android/scoped_java_ref.h"
#include "base/base_export.h"

namespace base {
namespace android {

// Convert a Java string to UTF8. Returns a std string.
BASE_EXPORT void ConvertJavaStringToUTF8(JNIEnv* env,
                                         jstring str,
                                         std::string* result);
BASE_EXPORT std::string ConvertJavaStringToUTF8(JNIEnv* env, jstring str);
BASE_EXPORT std::string ConvertJavaStringToUTF8(const JavaRef<jstring>& str);
BASE_EXPORT std::string ConvertJavaStringToUTF8(JNIEnv* env,
                                                const JavaRef<jstring>& str);

// Convert a std string to Java string.
BASE_EXPORT ScopedJavaLocalRef<jstring> ConvertUTF8ToJavaString(
    JNIEnv* env,
    std::string_view str);

// Convert a Java string to UTF16. Returns a std::u16string.
BASE_EXPORT void ConvertJavaStringToUTF16(JNIEnv* env,
                                          jstring str,
                                          std::u16string* result);
BASE_EXPORT std::u16string ConvertJavaStringToUTF16(JNIEnv* env, jstring str);
BASE_EXPORT std::u16string ConvertJavaStringToUTF16(
    const JavaRef<jstring>& str);
BASE_EXPORT std::u16string ConvertJavaStringToUTF16(
    JNIEnv* env,
    const JavaRef<jstring>& str);

// Convert a std::u16string to a Java string.
BASE_EXPORT ScopedJavaLocalRef<jstring> ConvertUTF16ToJavaString(
    JNIEnv* env,
    std::u16string_view str);

}  // namespace android
}  // namespace base

namespace jni_zero {
template <>
inline std::string FromJniType<std::string>(JNIEnv* env,
                                            const JavaRef<jobject>& input) {
  return base::android::ConvertJavaStringToUTF8(
      env, static_cast<jstring>(input.obj()));
}

template <>
inline ScopedJavaLocalRef<jobject> ToJniType<std::string>(
    JNIEnv* env,
    const std::string& input) {
  return base::android::ConvertUTF8ToJavaString(env, input);
}

template <>
inline ScopedJavaLocalRef<jobject> ToJniType<const char>(JNIEnv* env,
                                                   const char* input) {
  return base::android::ConvertUTF8ToJavaString(env, input);
}

template <>
inline std::u16string FromJniType<std::u16string>(
    JNIEnv* env,
    const JavaRef<jobject>& input) {
  return base::android::ConvertJavaStringToUTF16(
      env, static_cast<jstring>(input.obj()));
}

template <>
inline ScopedJavaLocalRef<jobject> ToJniType<std::u16string>(
    JNIEnv* env,
    const std::u16string& input) {
  return base::android::ConvertUTF16ToJavaString(env, input);
}

template <>
inline ScopedJavaLocalRef<jobject> ToJniType<std::u16string_view>(
    JNIEnv* env,
    const std::u16string_view& input) {
  return base::android::ConvertUTF16ToJavaString(env, input);
}
}  // namespace jni_zero

#endif  // BASE_ANDROID_JNI_STRING_H_