Flutter Engine
The Flutter Engine
Classes | Functions | Variables
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 ()
 
std::string JavaStringToString (JNIEnv *env, jstring str)
 
ScopedJavaLocalRef< jstring > StringToJavaString (JNIEnv *env, const std::string &u8_string)
 
std::vector< std::string > StringArrayToVector (JNIEnv *env, jobjectArray array)
 
std::vector< std::string > StringListToVector (JNIEnv *env, jobject list)
 
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 silent)
 
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
 
static thread_local std::unique_ptr< JNIDetachtls_jni_detach
 
static const int kDefaultLocalFrameCapacity = 16
 

Function Documentation

◆ AttachCurrentThread()

JNIEnv * fml::jni::AttachCurrentThread ( )

Definition at line 34 of file jni_util.cc.

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 [[maybe_unused]] 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 G_MODULE_EXPORT FlValue * args
#define FML_DCHECK(condition)
Definition: logging.h:103
Definition: __init__.py:1
static thread_local std::unique_ptr< 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 199 of file jni_util.cc.

199 {
200 if (!HasException(env)) {
201 return true;
202 }
203
204 jthrowable exception = env->ExceptionOccurred();
205 env->ExceptionClear();
207 env->DeleteLocalRef(exception);
208 return false;
209}
#define FML_LOG(severity)
Definition: logging.h:82
std::string GetJavaExceptionInfo(JNIEnv *env, jthrowable java_throwable)
Definition: jni_util.cc:211
bool HasException(JNIEnv *env)
Definition: jni_util.cc:184
#define ERROR(message)
Definition: elf_loader.cc:260

◆ ClearException()

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

Definition at line 188 of file jni_util.cc.

188 {
189 if (!HasException(env)) {
190 return false;
191 }
192 if (!silent) {
193 env->ExceptionDescribe();
194 }
195 env->ExceptionClear();
196 return true;
197}

◆ DetachFromVM()

void fml::jni::DetachFromVM ( )

Definition at line 64 of file jni_util.cc.

64 {
65 if (g_jvm) {
66 g_jvm->DetachCurrentThread();
67 }
68}

◆ GetJavaExceptionInfo()

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

Definition at line 211 of file jni_util.cc.

211 {
212 ScopedJavaLocalRef<jclass> throwable_clazz(
213 env, env->FindClass("java/lang/Throwable"));
214 jmethodID throwable_printstacktrace = env->GetMethodID(
215 throwable_clazz.obj(), "printStackTrace", "(Ljava/io/PrintStream;)V");
216
217 // Create an instance of ByteArrayOutputStream.
218 ScopedJavaLocalRef<jclass> bytearray_output_stream_clazz(
219 env, env->FindClass("java/io/ByteArrayOutputStream"));
220 jmethodID bytearray_output_stream_constructor =
221 env->GetMethodID(bytearray_output_stream_clazz.obj(), "<init>", "()V");
222 jmethodID bytearray_output_stream_tostring = env->GetMethodID(
223 bytearray_output_stream_clazz.obj(), "toString", "()Ljava/lang/String;");
224 ScopedJavaLocalRef<jobject> bytearray_output_stream(
225 env, env->NewObject(bytearray_output_stream_clazz.obj(),
226 bytearray_output_stream_constructor));
227
228 // Create an instance of PrintStream.
229 ScopedJavaLocalRef<jclass> printstream_clazz(
230 env, env->FindClass("java/io/PrintStream"));
231 jmethodID printstream_constructor = env->GetMethodID(
232 printstream_clazz.obj(), "<init>", "(Ljava/io/OutputStream;)V");
233 ScopedJavaLocalRef<jobject> printstream(
234 env, env->NewObject(printstream_clazz.obj(), printstream_constructor,
235 bytearray_output_stream.obj()));
236
237 // Call Throwable.printStackTrace(PrintStream)
238 env->CallVoidMethod(java_throwable, throwable_printstacktrace,
239 printstream.obj());
240
241 // Call ByteArrayOutputStream.toString()
242 ScopedJavaLocalRef<jstring> exception_string(
243 env,
244 static_cast<jstring>(env->CallObjectMethod(
245 bytearray_output_stream.obj(), bytearray_output_stream_tostring)));
246 if (ClearException(env)) {
247 return "Java OOM'd in exception handling, check logcat";
248 }
249
250 return JavaStringToString(env, exception_string.obj());
251}
std::string JavaStringToString(JNIEnv *env, jstring str)
Definition: jni_util.cc:70
bool ClearException(JNIEnv *env, bool silent)
Definition: jni_util.cc:188

◆ GetRealObject()

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

Definition at line 45 of file jni_weak_ref.cc.

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:102

◆ HasException()

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

Definition at line 184 of file jni_util.cc.

184 {
185 return env->ExceptionCheck() != JNI_FALSE;
186}

◆ InitJavaVM()

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

Definition at line 29 of file jni_util.cc.

29 {
30 FML_DCHECK(g_jvm == nullptr);
31 g_jvm = vm;
32}

◆ JavaStringToString()

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

Definition at line 70 of file jni_util.cc.

70 {
71 if (env == nullptr || str == nullptr) {
72 return "";
73 }
74 const jchar* chars = env->GetStringChars(str, NULL);
75 if (chars == nullptr) {
76 return "";
77 }
78 std::u16string u16_string(reinterpret_cast<const char16_t*>(chars),
79 env->GetStringLength(str));
80 std::string u8_string = Utf16ToUtf8(u16_string);
81 env->ReleaseStringChars(str, chars);
83 return u8_string;
84}
#define ASSERT_NO_EXCEPTION()
Definition: jni_util.cc:20
std::string Utf16ToUtf8(const std::u16string_view string)

◆ StringArrayToVector()

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

Definition at line 96 of file jni_util.cc.

96 {
97 std::vector<std::string> out;
98 if (env == nullptr || array == nullptr) {
99 return out;
100 }
101
102 jsize length = env->GetArrayLength(array);
103
104 if (length == -1) {
105 return out;
106 }
107
108 out.resize(length);
109 for (jsize i = 0; i < length; ++i) {
110 ScopedJavaLocalRef<jstring> java_string(
111 env, static_cast<jstring>(env->GetObjectArrayElement(array, i)));
112 out[i] = JavaStringToString(env, java_string.obj());
113 }
114
115 return out;
116}
size_t length

◆ StringListToVector()

std::vector< std::string > fml::jni::StringListToVector ( JNIEnv *  env,
jobject  list 
)

Definition at line 118 of file jni_util.cc.

118 {
119 std::vector<std::string> out;
120 if (env == nullptr || list == nullptr) {
121 return out;
122 }
123
124 ScopedJavaLocalRef<jclass> list_clazz(env, env->FindClass("java/util/List"));
125 FML_DCHECK(!list_clazz.is_null());
126
127 jmethodID list_get =
128 env->GetMethodID(list_clazz.obj(), "get", "(I)Ljava/lang/Object;");
129 jmethodID list_size = env->GetMethodID(list_clazz.obj(), "size", "()I");
130
131 jint size = env->CallIntMethod(list, list_size);
132
133 if (size == 0) {
134 return out;
135 }
136
137 out.resize(size);
138 for (jint i = 0; i < size; ++i) {
139 ScopedJavaLocalRef<jstring> java_string(
140 env, static_cast<jstring>(env->CallObjectMethod(list, list_get, i)));
141 out[i] = JavaStringToString(env, java_string.obj());
142 }
143
144 return out;
145}
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
Definition: switches.h:259

◆ StringToJavaString()

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

Definition at line 86 of file jni_util.cc.

87 {
88 std::u16string u16_string = Utf8ToUtf16(u8_string);
90 env, env->NewString(reinterpret_cast<const jchar*>(u16_string.data()),
91 u16_string.length()));
93 return result;
94}
GAsyncResult * result
std::u16string Utf8ToUtf16(const std::string_view string)

◆ VectorToBufferArray()

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

Definition at line 164 of file jni_util.cc.

166 {
168 ScopedJavaLocalRef<jclass> byte_buffer_clazz(
169 env, env->FindClass("java/nio/ByteBuffer"));
170 FML_DCHECK(!byte_buffer_clazz.is_null());
171 jobjectArray java_array =
172 env->NewObjectArray(vector.size(), byte_buffer_clazz.obj(), NULL);
174 for (size_t i = 0; i < vector.size(); ++i) {
175 uint8_t* data = const_cast<uint8_t*>(vector[i].data());
177 env, env->NewDirectByteBuffer(reinterpret_cast<void*>(data),
178 vector[i].size()));
179 env->SetObjectArrayElement(java_array, i, item.obj());
180 }
181 return ScopedJavaLocalRef<jobjectArray>(env, java_array);
182}
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:63

◆ VectorToStringArray()

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

Definition at line 147 of file jni_util.cc.

149 {
151 ScopedJavaLocalRef<jclass> string_clazz(env,
152 env->FindClass("java/lang/String"));
153 FML_DCHECK(!string_clazz.is_null());
154 jobjectArray java_array =
155 env->NewObjectArray(vector.size(), string_clazz.obj(), NULL);
157 for (size_t i = 0; i < vector.size(); ++i) {
159 env->SetObjectArrayElement(java_array, i, item.obj());
160 }
161 return ScopedJavaLocalRef<jobjectArray>(env, java_array);
162}
ScopedJavaLocalRef< jstring > StringToJavaString(JNIEnv *env, const std::string &u8_string)
Definition: jni_util.cc:86

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

thread_local std::unique_ptr<JNIDetach> fml::jni::tls_jni_detach
static

Definition at line 27 of file jni_util.cc.