Flutter Engine
The Flutter Engine
scoped_java_ref.cc
Go to the documentation of this file.
1// Copyright 2013 The Flutter 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.
4
5#include "flutter/fml/platform/android/scoped_java_ref.h"
6
7#include "flutter/fml/logging.h"
8#include "flutter/fml/platform/android/jni_util.h"
9
10namespace fml {
11namespace jni {
12
13static const int kDefaultLocalFrameCapacity = 16;
14
16 [[maybe_unused]] int failed =
17 env_->PushLocalFrame(kDefaultLocalFrameCapacity);
18 FML_DCHECK(!failed);
19}
20
22 : env_(env) {
23 [[maybe_unused]] int failed = env_->PushLocalFrame(capacity);
24 FML_DCHECK(!failed);
25}
26
28 env_->PopLocalFrame(NULL);
29}
30
32
33JavaRef<jobject>::JavaRef(JNIEnv* env, jobject obj) : obj_(obj) {
34 if (obj) {
35 FML_DCHECK(env && env->GetObjectRefType(obj) == JNILocalRefType);
36 }
37}
38
40
41JNIEnv* JavaRef<jobject>::SetNewLocalRef(JNIEnv* env, jobject obj) {
42 if (!env) {
44 } else {
45 FML_DCHECK(env == AttachCurrentThread()); // Is |env| on correct thread.
46 }
47 if (obj) {
48 obj = env->NewLocalRef(obj);
49 }
50 if (obj_) {
51 env->DeleteLocalRef(obj_);
52 }
53 obj_ = obj;
54 return env;
55}
56
57void JavaRef<jobject>::SetNewGlobalRef(JNIEnv* env, jobject obj) {
58 if (!env) {
60 } else {
61 FML_DCHECK(env == AttachCurrentThread()); // Is |env| on correct thread.
62 }
63 if (obj) {
64 obj = env->NewGlobalRef(obj);
65 }
66 if (obj_) {
67 env->DeleteGlobalRef(obj_);
68 }
69 obj_ = obj;
70}
71
73 if (obj_) {
74 FML_DCHECK(env == AttachCurrentThread()); // Is |env| on correct thread.
75 env->DeleteLocalRef(obj_);
76 obj_ = NULL;
77 }
78}
79
81 if (obj_) {
82 AttachCurrentThread()->DeleteGlobalRef(obj_);
83 obj_ = NULL;
84 }
85}
86
88 jobject obj = obj_;
89 obj_ = NULL;
90 return obj;
91}
92
93} // namespace jni
94} // namespace fml
#define FML_DCHECK(condition)
Definition: logging.h:103
Definition: __init__.py:1
JNIEnv * AttachCurrentThread()
Definition: jni_util.cc:34
static const int kDefaultLocalFrameCapacity
Definition: ascii_trie.cc:9