• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef FLUTTER_FML_PLATFORM_ANDROID_JNI_WEAK_REF_H_
6 #define FLUTTER_FML_PLATFORM_ANDROID_JNI_WEAK_REF_H_
7 
8 #include <jni.h>
9 
10 #include "flutter/fml/platform/android/scoped_java_ref.h"
11 
12 namespace fml {
13 namespace jni {
14 
15 // Manages WeakGlobalRef lifecycle.
16 // This class is not thread-safe w.r.t. get() and reset(). Multiple threads may
17 // safely use get() concurrently, but if the user calls reset() (or of course,
18 // calls the destructor) they'll need to provide their own synchronization.
19 class JavaObjectWeakGlobalRef {
20  public:
21   JavaObjectWeakGlobalRef();
22 
23   JavaObjectWeakGlobalRef(const JavaObjectWeakGlobalRef& orig);
24 
25   JavaObjectWeakGlobalRef(JNIEnv* env, jobject obj);
26 
27   virtual ~JavaObjectWeakGlobalRef();
28 
29   void operator=(const JavaObjectWeakGlobalRef& rhs);
30 
31   ScopedJavaLocalRef<jobject> get(JNIEnv* env) const;
32 
is_empty()33   bool is_empty() const { return obj_ == NULL; }
34 
35   void reset();
36 
37  private:
38   void Assign(const JavaObjectWeakGlobalRef& rhs);
39 
40   jweak obj_;
41 };
42 
43 // Get the real object stored in the weak reference returned as a
44 // ScopedJavaLocalRef.
45 ScopedJavaLocalRef<jobject> GetRealObject(JNIEnv* env, jweak obj);
46 
47 }  // namespace jni
48 }  // namespace fml
49 
50 #endif  // FLUTTER_FML_PLATFORM_ANDROID_JNI_WEAK_REF_H_
51