Flutter Engine
fml::jni Namespace Reference

Classes

class  JavaObjectWeakGlobalRef
 
class  JavaRef
 
class  JavaRef< jobject >
 
struct  JNIDetach
 
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)
 
ScopedJavaLocalRef< jobjectArray > VectorToBufferArray (JNIEnv *env, const std::vector< std::vector< uint8_t >> &vector)
 
bool HasException (JNIEnv *env)
 
bool ClearException (JNIEnv *env)
 
bool CheckException (JNIEnv *env)
 
std::string GetJavaExceptionInfo (JNIEnv *env, jthrowable java_throwable)
 
ScopedJavaLocalRef< jobject > GetRealObject (JNIEnv *env, jweak obj)
 

Variables

static JavaVM * g_jvm = nullptr
 
FML_THREAD_LOCAL fml::ThreadLocalUniquePtr< JNIDetachtls_jni_detach
 
static const int kDefaultLocalFrameCapacity = 16
 

Function Documentation

◆ AttachCurrentThread()

JNIEnv * fml::jni::AttachCurrentThread ( )

Definition at line 34 of file jni_util.cc.

References args, and FML_DCHECK.

Referenced by flutter::AndroidImageGenerator::DecodeImage(), 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(), flutter::PlatformViewAndroidJNIImpl::RequestDartDeferredLibrary(), 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().

34  {
35  FML_DCHECK(g_jvm != nullptr)
36  << "Trying to attach to current thread without calling InitJavaVM first.";
37 
38  JNIEnv* env = nullptr;
39  if (g_jvm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_4) ==
40  JNI_OK) {
41  return env;
42  }
43 
44  JavaVMAttachArgs args;
45  args.version = JNI_VERSION_1_4;
46  args.group = nullptr;
47  // 16 is the maximum size for thread names on Android.
48  char thread_name[16];
49  int err = prctl(PR_GET_NAME, thread_name);
50  if (err < 0) {
51  args.name = nullptr;
52  } else {
53  args.name = thread_name;
54  }
55  jint ret = g_jvm->AttachCurrentThread(&env, &args);
56  FML_DCHECK(JNI_OK == ret);
57 
58  FML_DCHECK(tls_jni_detach.get() == nullptr);
59  tls_jni_detach.reset(new JNIDetach());
60 
61  return env;
62 }
G_BEGIN_DECLS FlValue * args
#define FML_DCHECK(condition)
Definition: logging.h:86
FML_THREAD_LOCAL fml::ThreadLocalUniquePtr< JNIDetach > tls_jni_detach
Definition: jni_util.cc:27
static JavaVM * g_jvm
Definition: jni_util.cc:18

◆ CheckException()

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

Definition at line 176 of file jni_util.cc.

References FML_LOG, GetJavaExceptionInfo(), and HasException().

Referenced by flutter::AndroidImageGenerator::DecodeImage(), 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::RequestDartDeferredLibrary(), flutter::PlatformViewAndroidJNIImpl::SurfaceTextureAttachToGLContext(), flutter::PlatformViewAndroidJNIImpl::SurfaceTextureDetachFromGLContext(), flutter::PlatformViewAndroidJNIImpl::SurfaceTextureGetTransformMatrix(), and flutter::PlatformViewAndroidJNIImpl::SurfaceTextureUpdateTexImage().

176  {
177  if (!HasException(env))
178  return true;
179 
180  jthrowable exception = env->ExceptionOccurred();
181  env->ExceptionClear();
182  FML_LOG(ERROR) << fml::jni::GetJavaExceptionInfo(env, exception);
183  env->DeleteLocalRef(exception);
184  return false;
185 }
bool HasException(JNIEnv *env)
Definition: jni_util.cc:164
#define FML_LOG(severity)
Definition: logging.h:65
std::string GetJavaExceptionInfo(JNIEnv *env, jthrowable java_throwable)
Definition: jni_util.cc:187

◆ ClearException()

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

Definition at line 168 of file jni_util.cc.

References HasException().

Referenced by GetJavaExceptionInfo().

168  {
169  if (!HasException(env))
170  return false;
171  env->ExceptionDescribe();
172  env->ExceptionClear();
173  return true;
174 }
bool HasException(JNIEnv *env)
Definition: jni_util.cc:164

◆ DetachFromVM()

void fml::jni::DetachFromVM ( )

Definition at line 64 of file jni_util.cc.

Referenced by fml::jni::JNIDetach::~JNIDetach().

64  {
65  if (g_jvm) {
66  g_jvm->DetachCurrentThread();
67  }
68 }
static JavaVM * g_jvm
Definition: jni_util.cc:18

◆ GetJavaExceptionInfo()

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

Definition at line 187 of file jni_util.cc.

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

Referenced by CheckException().

187  {
188  ScopedJavaLocalRef<jclass> throwable_clazz(
189  env, env->FindClass("java/lang/Throwable"));
190  jmethodID throwable_printstacktrace = env->GetMethodID(
191  throwable_clazz.obj(), "printStackTrace", "(Ljava/io/PrintStream;)V");
192 
193  // Create an instance of ByteArrayOutputStream.
194  ScopedJavaLocalRef<jclass> bytearray_output_stream_clazz(
195  env, env->FindClass("java/io/ByteArrayOutputStream"));
196  jmethodID bytearray_output_stream_constructor =
197  env->GetMethodID(bytearray_output_stream_clazz.obj(), "<init>", "()V");
198  jmethodID bytearray_output_stream_tostring = env->GetMethodID(
199  bytearray_output_stream_clazz.obj(), "toString", "()Ljava/lang/String;");
200  ScopedJavaLocalRef<jobject> bytearray_output_stream(
201  env, env->NewObject(bytearray_output_stream_clazz.obj(),
202  bytearray_output_stream_constructor));
203 
204  // Create an instance of PrintStream.
205  ScopedJavaLocalRef<jclass> printstream_clazz(
206  env, env->FindClass("java/io/PrintStream"));
207  jmethodID printstream_constructor = env->GetMethodID(
208  printstream_clazz.obj(), "<init>", "(Ljava/io/OutputStream;)V");
209  ScopedJavaLocalRef<jobject> printstream(
210  env, env->NewObject(printstream_clazz.obj(), printstream_constructor,
211  bytearray_output_stream.obj()));
212 
213  // Call Throwable.printStackTrace(PrintStream)
214  env->CallVoidMethod(java_throwable, throwable_printstacktrace,
215  printstream.obj());
216 
217  // Call ByteArrayOutputStream.toString()
218  ScopedJavaLocalRef<jstring> exception_string(
219  env,
220  static_cast<jstring>(env->CallObjectMethod(
221  bytearray_output_stream.obj(), bytearray_output_stream_tostring)));
222  if (ClearException(env)) {
223  return "Java OOM'd in exception handling, check logcat";
224  }
225 
226  return JavaStringToString(env, exception_string.obj());
227 }
bool ClearException(JNIEnv *env)
Definition: jni_util.cc:168
std::string JavaStringToString(JNIEnv *env, jstring str)
Definition: jni_util.cc:76

◆ 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 164 of file jni_util.cc.

Referenced by CheckException(), and ClearException().

164  {
165  return env->ExceptionCheck() != JNI_FALSE;
166 }

◆ InitJavaVM()

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

Definition at line 29 of file jni_util.cc.

References FML_DCHECK.

Referenced by JNI_OnLoad().

29  {
30  FML_DCHECK(g_jvm == nullptr);
31  g_jvm = vm;
32 }
#define FML_DCHECK(condition)
Definition: logging.h:86
static JavaVM * g_jvm
Definition: jni_util.cc:18

◆ JavaStringToString()

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

Definition at line 76 of file jni_util.cc.

References ASSERT_NO_EXCEPTION, and UTF16StringToUTF8String().

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

76  {
77  if (env == nullptr || str == nullptr) {
78  return "";
79  }
80  const jchar* chars = env->GetStringChars(str, NULL);
81  if (chars == nullptr) {
82  return "";
83  }
84  std::string u8_string = UTF16StringToUTF8String(
85  reinterpret_cast<const char16_t*>(chars), env->GetStringLength(str));
86  env->ReleaseStringChars(str, chars);
88  return u8_string;
89 }
#define ASSERT_NO_EXCEPTION()
Definition: jni_util.cc:20
static std::string UTF16StringToUTF8String(const char16_t *chars, size_t len)
Definition: jni_util.cc:70

◆ StringArrayToVector()

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

Definition at line 106 of file jni_util.cc.

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

Referenced by flutter::FlutterMain::GetSettings(), and flutter::LoadDartDeferredLibrary().

106  {
107  std::vector<std::string> out;
108  if (env == nullptr || array == nullptr) {
109  return out;
110  }
111 
112  jsize length = env->GetArrayLength(array);
113 
114  if (length == -1) {
115  return out;
116  }
117 
118  out.resize(length);
119  for (jsize i = 0; i < length; ++i) {
120  ScopedJavaLocalRef<jstring> java_string(
121  env, static_cast<jstring>(env->GetObjectArrayElement(array, i)));
122  out[i] = JavaStringToString(env, java_string.obj());
123  }
124 
125  return out;
126 }
size_t length
std::string JavaStringToString(JNIEnv *env, jstring str)
Definition: jni_util.cc:76

◆ StringToJavaString()

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

Definition at line 96 of file jni_util.cc.

References ASSERT_NO_EXCEPTION, result, and UTF8StringToUTF16String().

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

97  {
98  std::u16string u16_string = UTF8StringToUTF16String(u8_string);
99  auto result = ScopedJavaLocalRef<jstring>(
100  env, env->NewString(reinterpret_cast<const jchar*>(u16_string.data()),
101  u16_string.length()));
103  return result;
104 }
GAsyncResult * result
#define ASSERT_NO_EXCEPTION()
Definition: jni_util.cc:20
static std::u16string UTF8StringToUTF16String(const std::string &string)
Definition: jni_util.cc:91

◆ UTF16StringToUTF8String()

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

Definition at line 70 of file jni_util.cc.

Referenced by JavaStringToString().

70  {
71  std::u16string u16_string(chars, len);
72  return std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t>{}
73  .to_bytes(u16_string);
74 }

◆ UTF8StringToUTF16String()

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

Definition at line 91 of file jni_util.cc.

Referenced by StringToJavaString().

91  {
92  return std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t>{}
93  .from_bytes(string);
94 }

◆ VectorToBufferArray()

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

Definition at line 145 of file jni_util.cc.

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

Referenced by flutter::PlatformViewAndroidJNIImpl::FlutterViewUpdateSemantics().

147  {
148  FML_DCHECK(env);
149  ScopedJavaLocalRef<jclass> byte_buffer_clazz(
150  env, env->FindClass("java/nio/ByteBuffer"));
151  FML_DCHECK(!byte_buffer_clazz.is_null());
152  jobjectArray java_array =
153  env->NewObjectArray(vector.size(), byte_buffer_clazz.obj(), NULL);
155  for (size_t i = 0; i < vector.size(); ++i) {
156  ScopedJavaLocalRef<jobject> item(
157  env,
158  env->NewDirectByteBuffer((void*)(vector[i].data()), vector[i].size()));
159  env->SetObjectArrayElement(java_array, i, item.obj());
160  }
161  return ScopedJavaLocalRef<jobjectArray>(env, java_array);
162 }
#define FML_DCHECK(condition)
Definition: logging.h:86
constexpr std::size_t size(T(&array)[N])
Definition: size.h:13
#define ASSERT_NO_EXCEPTION()
Definition: jni_util.cc:20

◆ VectorToStringArray()

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

Definition at line 128 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().

130  {
131  FML_DCHECK(env);
132  ScopedJavaLocalRef<jclass> string_clazz(env,
133  env->FindClass("java/lang/String"));
134  FML_DCHECK(!string_clazz.is_null());
135  jobjectArray java_array =
136  env->NewObjectArray(vector.size(), string_clazz.obj(), NULL);
138  for (size_t i = 0; i < vector.size(); ++i) {
139  ScopedJavaLocalRef<jstring> item = StringToJavaString(env, vector[i]);
140  env->SetObjectArrayElement(java_array, i, item.obj());
141  }
142  return ScopedJavaLocalRef<jobjectArray>(env, java_array);
143 }
#define FML_DCHECK(condition)
Definition: logging.h:86
ScopedJavaLocalRef< jstring > StringToJavaString(JNIEnv *env, const std::string &u8_string)
Definition: jni_util.cc:96
#define ASSERT_NO_EXCEPTION()
Definition: jni_util.cc:20

Variable Documentation

◆ g_jvm

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

Definition at line 18 of file jni_util.cc.

◆ kDefaultLocalFrameCapacity

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

Definition at line 13 of file scoped_java_ref.cc.

◆ tls_jni_detach

Definition at line 27 of file jni_util.cc.