Flutter Engine
jni_weak_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/jni_weak_ref.h"
6 
7 #include "flutter/fml/logging.h"
8 #include "flutter/fml/platform/android/jni_util.h"
9 
10 namespace fml {
11 namespace jni {
12 
14 
16  const JavaObjectWeakGlobalRef& orig)
17  : obj_(NULL) {
18  Assign(orig);
19 }
20 
22  : obj_(env->NewWeakGlobalRef(obj)) {
23  FML_DCHECK(obj_);
24 }
25 
27  reset();
28 }
29 
31  Assign(rhs);
32 }
33 
35  if (obj_) {
36  AttachCurrentThread()->DeleteWeakGlobalRef(obj_);
37  obj_ = NULL;
38  }
39 }
40 
42  return GetRealObject(env, obj_);
43 }
44 
45 ScopedJavaLocalRef<jobject> GetRealObject(JNIEnv* env, jweak obj) {
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 }
55 
56 void JavaObjectWeakGlobalRef::Assign(const JavaObjectWeakGlobalRef& other) {
57  if (&other == this)
58  return;
59 
60  JNIEnv* env = AttachCurrentThread();
61  if (obj_)
62  env->DeleteWeakGlobalRef(obj_);
63 
64  obj_ = other.obj_ ? env->NewWeakGlobalRef(other.obj_) : NULL;
65 }
66 
67 } // namespace jni
68 } // namespace fml
#define FML_DCHECK(condition)
Definition: logging.h:86
void operator=(const JavaObjectWeakGlobalRef &rhs)
Definition: jni_weak_ref.cc:30
Definition: ascii_trie.cc:9
ScopedJavaLocalRef< jobject > get(JNIEnv *env) const
Definition: jni_weak_ref.cc:41
ScopedJavaLocalRef< jobject > GetRealObject(JNIEnv *env, jweak obj)
Definition: jni_weak_ref.cc:45
JNIEnv * AttachCurrentThread()
Definition: jni_util.cc:26
#define FML_DLOG(severity)
Definition: logging.h:85