Flutter Engine
fml::jni Namespace Reference

Classes

class  JavaObjectWeakGlobalRef
 
class  JavaRef
 
class  JavaRef< jobject >
 
class  ScopedJavaGlobalRef
 
class  ScopedJavaLocalFrame
 
class  ScopedJavaLocalRef
 

Functions

void InitJavaVM (JavaVM *vm)
 
JNIEnv * AttachCurrentThread ()
 
void DetachFromVM ()
 
static std::string UTF16StringToUTF8String (const char16_t *chars, size_t len)
 
std::string JavaStringToString (JNIEnv *env, jstring str)
 
static std::u16string UTF8StringToUTF16String (const std::string &string)
 
ScopedJavaLocalRef< jstring > StringToJavaString (JNIEnv *env, const std::string &u8_string)
 
std::vector< std::string > StringArrayToVector (JNIEnv *env, jobjectArray array)
 
ScopedJavaLocalRef< jobjectArray > VectorToStringArray (JNIEnv *env, const std::vector< std::string > &vector)
 
bool HasException (JNIEnv *env)
 
bool ClearException (JNIEnv *env)
 
std::string GetJavaExceptionInfo (JNIEnv *env, jthrowable java_throwable)
 
ScopedJavaLocalRef< jobject > GetRealObject (JNIEnv *env, jweak obj)
 

Variables

static JavaVM * g_jvm = nullptr
 
static const int kDefaultLocalFrameCapacity = 16
 

Function Documentation

◆ AttachCurrentThread()

JNIEnv * fml::jni::AttachCurrentThread ( )

Definition at line 26 of file jni_util.cc.

References args, and FML_DCHECK.

Referenced by flutter::PlatformViewAndroidJNIImpl::FlutterViewBeginFrame(), flutter::PlatformViewAndroidJNIImpl::FlutterViewComputePlatformResolvedLocale(), flutter::PlatformViewAndroidJNIImpl::FlutterViewCreateOverlaySurface(), flutter::PlatformViewAndroidJNIImpl::FlutterViewDestroyOverlaySurfaces(), flutter::PlatformViewAndroidJNIImpl::FlutterViewDisplayOverlaySurface(), flutter::PlatformViewAndroidJNIImpl::FlutterViewEndFrame(), flutter::PlatformViewAndroidJNIImpl::FlutterViewHandlePlatformMessage(), flutter::PlatformViewAndroidJNIImpl::FlutterViewHandlePlatformMessageResponse(), flutter::PlatformViewAndroidJNIImpl::FlutterViewOnDisplayPlatformView(), flutter::PlatformViewAndroidJNIImpl::FlutterViewOnFirstFrame(), flutter::PlatformViewAndroidJNIImpl::FlutterViewOnPreEngineRestart(), flutter::PlatformViewAndroidJNIImpl::FlutterViewUpdateCustomAccessibilityActions(), flutter::PlatformViewAndroidJNIImpl::FlutterViewUpdateSemantics(), flutter::PlatformViewAndroidJNIImpl::GetDisplayRefreshRate(), GetRealObject(), JNI_OnLoad(), fml::jni::JavaObjectWeakGlobalRef::reset(), fml::jni::JavaRef< jobject >::ResetGlobalRef(), fml::jni::JavaRef< jobject >::ResetLocalRef(), fml::jni::JavaRef< jobject >::SetNewGlobalRef(), fml::jni::JavaRef< jobject >::SetNewLocalRef(), flutter::PlatformViewAndroidJNIImpl::SurfaceTextureAttachToGLContext(), flutter::PlatformViewAndroidJNIImpl::SurfaceTextureDetachFromGLContext(), flutter::PlatformViewAndroidJNIImpl::SurfaceTextureGetTransformMatrix(), flutter::PlatformViewAndroidJNIImpl::SurfaceTextureUpdateTexImage(), and flutter::VsyncWaiterAndroid::VsyncWaiterAndroid().

26  {
27  FML_DCHECK(g_jvm != nullptr)
28  << "Trying to attach to current thread without calling InitJavaVM first.";
29  JNIEnv* env = nullptr;
30  JavaVMAttachArgs args;
31  args.version = JNI_VERSION_1_4;
32  args.group = nullptr;
33  // 16 is the maximum size for thread names on Android.
34  char thread_name[16];
35  int err = prctl(PR_GET_NAME, thread_name);
36  if (err < 0) {
37  args.name = nullptr;
38  } else {
39  args.name = thread_name;
40  }
41  jint ret = g_jvm->AttachCurrentThread(&env, &args);
42  FML_DCHECK(JNI_OK == ret);
43  return env;
44 }
G_BEGIN_DECLS FlValue * args
#define FML_DCHECK(condition)
Definition: logging.h:86
static JavaVM * g_jvm
Definition: jni_util.cc:17

◆ ClearException()

bool fml::jni::ClearException ( JNIEnv *  env)

Definition at line 131 of file jni_util.cc.

References HasException().

Referenced by GetJavaExceptionInfo().

131  {
132  if (!HasException(env))
133  return false;
134  env->ExceptionDescribe();
135  env->ExceptionClear();
136  return true;
137 }
bool HasException(JNIEnv *env)
Definition: jni_util.cc:127

◆ DetachFromVM()

void fml::jni::DetachFromVM ( )

Definition at line 46 of file jni_util.cc.

Referenced by flutter::AndroidShellHolder::~AndroidShellHolder().

46  {
47  if (g_jvm) {
48  g_jvm->DetachCurrentThread();
49  }
50 }
static JavaVM * g_jvm
Definition: jni_util.cc:17

◆ GetJavaExceptionInfo()

std::string fml::jni::GetJavaExceptionInfo ( JNIEnv *  env,
jthrowable  java_throwable 
)

Definition at line 139 of file jni_util.cc.

References ClearException(), JavaStringToString(), and fml::jni::JavaRef< T >::obj().

139  {
140  ScopedJavaLocalRef<jclass> throwable_clazz(
141  env, env->FindClass("java/lang/Throwable"));
142  jmethodID throwable_printstacktrace = env->GetMethodID(
143  throwable_clazz.obj(), "printStackTrace", "(Ljava/io/PrintStream;)V");
144 
145  // Create an instance of ByteArrayOutputStream.
146  ScopedJavaLocalRef<jclass> bytearray_output_stream_clazz(
147  env, env->FindClass("java/io/ByteArrayOutputStream"));
148  jmethodID bytearray_output_stream_constructor =
149  env->GetMethodID(bytearray_output_stream_clazz.obj(), "<init>", "()V");
150  jmethodID bytearray_output_stream_tostring = env->GetMethodID(
151  bytearray_output_stream_clazz.obj(), "toString", "()Ljava/lang/String;");
152  ScopedJavaLocalRef<jobject> bytearray_output_stream(
153  env, env->NewObject(bytearray_output_stream_clazz.obj(),
154  bytearray_output_stream_constructor));
155 
156  // Create an instance of PrintStream.
157  ScopedJavaLocalRef<jclass> printstream_clazz(
158  env, env->FindClass("java/io/PrintStream"));
159  jmethodID printstream_constructor = env->GetMethodID(
160  printstream_clazz.obj(), "<init>", "(Ljava/io/OutputStream;)V");
161  ScopedJavaLocalRef<jobject> printstream(
162  env, env->NewObject(printstream_clazz.obj(), printstream_constructor,
163  bytearray_output_stream.obj()));
164 
165  // Call Throwable.printStackTrace(PrintStream)
166  env->CallVoidMethod(java_throwable, throwable_printstacktrace,
167  printstream.obj());
168 
169  // Call ByteArrayOutputStream.toString()
170  ScopedJavaLocalRef<jstring> exception_string(
171  env,
172  static_cast<jstring>(env->CallObjectMethod(
173  bytearray_output_stream.obj(), bytearray_output_stream_tostring)));
174  if (ClearException(env)) {
175  return "Java OOM'd in exception handling, check logcat";
176  }
177 
178  return JavaStringToString(env, exception_string.obj());
179 }
bool ClearException(JNIEnv *env)
Definition: jni_util.cc:131
std::string JavaStringToString(JNIEnv *env, jstring str)
Definition: jni_util.cc:58

◆ GetRealObject()

ScopedJavaLocalRef< jobject > fml::jni::GetRealObject ( JNIEnv *  env,
jweak  obj 
)

Definition at line 45 of file jni_weak_ref.cc.

References AttachCurrentThread(), and FML_DLOG.

Referenced by fml::jni::JavaObjectWeakGlobalRef::get(), and fml::jni::JavaObjectWeakGlobalRef::is_empty().

45  {
46  jobject real = NULL;
47  if (obj) {
48  real = env->NewLocalRef(obj);
49  if (!real) {
50  FML_DLOG(ERROR) << "The real object has been deleted!";
51  }
52  }
53  return ScopedJavaLocalRef<jobject>(env, real);
54 }
#define FML_DLOG(severity)
Definition: logging.h:85

◆ HasException()

bool fml::jni::HasException ( JNIEnv *  env)

Definition at line 127 of file jni_util.cc.

Referenced by ClearException().

127  {
128  return env->ExceptionCheck() != JNI_FALSE;
129 }

◆ InitJavaVM()

void fml::jni::InitJavaVM ( JavaVM *  vm)

Definition at line 21 of file jni_util.cc.

References FML_DCHECK.

Referenced by JNI_OnLoad().

21  {
22  FML_DCHECK(g_jvm == nullptr);
23  g_jvm = vm;
24 }
#define FML_DCHECK(condition)
Definition: logging.h:86
static JavaVM * g_jvm
Definition: jni_util.cc:17

◆ JavaStringToString()

std::string fml::jni::JavaStringToString ( JNIEnv *  env,
jstring  str 
)

Definition at line 58 of file jni_util.cc.

References ASSERT_NO_EXCEPTION, and UTF16StringToUTF8String().

Referenced by flutter::DispatchEmptyPlatformMessage(), flutter::DispatchPlatformMessage(), flutter::PlatformViewAndroidJNIImpl::FlutterViewComputePlatformResolvedLocale(), GetJavaExceptionInfo(), flutter::FlutterMain::GetSettings(), flutter::RunBundleAndSnapshotFromLibrary(), and StringArrayToVector().

58  {
59  if (env == nullptr || str == nullptr) {
60  return "";
61  }
62  const jchar* chars = env->GetStringChars(str, NULL);
63  if (chars == nullptr) {
64  return "";
65  }
66  std::string u8_string = UTF16StringToUTF8String(
67  reinterpret_cast<const char16_t*>(chars), env->GetStringLength(str));
68  env->ReleaseStringChars(str, chars);
70  return u8_string;
71 }
#define ASSERT_NO_EXCEPTION()
Definition: jni_util.cc:19
static std::string UTF16StringToUTF8String(const char16_t *chars, size_t len)
Definition: jni_util.cc:52

◆ StringArrayToVector()

std::vector< std::string > fml::jni::StringArrayToVector ( JNIEnv *  env,
jobjectArray  array 
)

Definition at line 88 of file jni_util.cc.

References JavaStringToString(), length, and fml::jni::JavaRef< T >::obj().

Referenced by flutter::FlutterMain::GetSettings().

88  {
89  std::vector<std::string> out;
90  if (env == nullptr || array == nullptr) {
91  return out;
92  }
93 
94  jsize length = env->GetArrayLength(array);
95 
96  if (length == -1) {
97  return out;
98  }
99 
100  out.resize(length);
101  for (jsize i = 0; i < length; ++i) {
102  ScopedJavaLocalRef<jstring> java_string(
103  env, static_cast<jstring>(env->GetObjectArrayElement(array, i)));
104  out[i] = JavaStringToString(env, java_string.obj());
105  }
106 
107  return out;
108 }
size_t length
std::string JavaStringToString(JNIEnv *env, jstring str)
Definition: jni_util.cc:58

◆ StringToJavaString()

ScopedJavaLocalRef< jstring > fml::jni::StringToJavaString ( JNIEnv *  env,
const std::string &  u8_string 
)

Definition at line 78 of file jni_util.cc.

References ASSERT_NO_EXCEPTION, and UTF8StringToUTF16String().

Referenced by flutter::PlatformViewAndroidJNIImpl::FlutterViewHandlePlatformMessage(), flutter::FlutterMain::GetSettings(), and VectorToStringArray().

79  {
80  std::u16string u16_string = UTF8StringToUTF16String(u8_string);
81  auto result = ScopedJavaLocalRef<jstring>(
82  env, env->NewString(reinterpret_cast<const jchar*>(u16_string.data()),
83  u16_string.length()));
85  return result;
86 }
#define ASSERT_NO_EXCEPTION()
Definition: jni_util.cc:19
static std::u16string UTF8StringToUTF16String(const std::string &string)
Definition: jni_util.cc:73

◆ UTF16StringToUTF8String()

static std::string fml::jni::UTF16StringToUTF8String ( const char16_t *  chars,
size_t  len 
)
static

Definition at line 52 of file jni_util.cc.

Referenced by JavaStringToString().

52  {
53  std::u16string u16_string(chars, len);
54  return std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t>{}
55  .to_bytes(u16_string);
56 }

◆ UTF8StringToUTF16String()

static std::u16string fml::jni::UTF8StringToUTF16String ( const std::string &  string)
static

Definition at line 73 of file jni_util.cc.

Referenced by StringToJavaString().

73  {
74  return std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t>{}
75  .from_bytes(string);
76 }

◆ VectorToStringArray()

ScopedJavaLocalRef< jobjectArray > fml::jni::VectorToStringArray ( JNIEnv *  env,
const std::vector< std::string > &  vector 
)

Definition at line 110 of file jni_util.cc.

References ASSERT_NO_EXCEPTION, FML_DCHECK, fml::jni::JavaRef< T >::obj(), and StringToJavaString().

Referenced by flutter::PlatformViewAndroidJNIImpl::FlutterViewComputePlatformResolvedLocale(), flutter::PlatformViewAndroidJNIImpl::FlutterViewUpdateCustomAccessibilityActions(), and flutter::PlatformViewAndroidJNIImpl::FlutterViewUpdateSemantics().

112  {
113  FML_DCHECK(env);
114  ScopedJavaLocalRef<jclass> string_clazz(env,
115  env->FindClass("java/lang/String"));
116  FML_DCHECK(!string_clazz.is_null());
117  jobjectArray joa =
118  env->NewObjectArray(vector.size(), string_clazz.obj(), NULL);
120  for (size_t i = 0; i < vector.size(); ++i) {
121  ScopedJavaLocalRef<jstring> item = StringToJavaString(env, vector[i]);
122  env->SetObjectArrayElement(joa, i, item.obj());
123  }
124  return ScopedJavaLocalRef<jobjectArray>(env, joa);
125 }
#define FML_DCHECK(condition)
Definition: logging.h:86
ScopedJavaLocalRef< jstring > StringToJavaString(JNIEnv *env, const std::string &u8_string)
Definition: jni_util.cc:78
#define ASSERT_NO_EXCEPTION()
Definition: jni_util.cc:19

Variable Documentation

◆ g_jvm

JavaVM* fml::jni::g_jvm = nullptr
static

Definition at line 17 of file jni_util.cc.

◆ kDefaultLocalFrameCapacity

const int fml::jni::kDefaultLocalFrameCapacity = 16
static

Definition at line 13 of file scoped_java_ref.cc.