OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011 The Chromium 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 BASE_ANDROID_SCOPED_JAVA_GLOBAL_REFERENCE_H_ | |
6 #define BASE_ANDROID_SCOPED_JAVA_GLOBAL_REFERENCE_H_ | |
7 | |
8 #include <jni.h> | |
9 #include <stddef.h> | |
10 | |
11 #include "base/android/scoped_java_reference.h" | |
12 #include "base/basictypes.h" | |
13 | |
14 namespace base { | |
15 namespace android { | |
16 | |
17 // Holds a global reference to a Java object. The global reference is scoped | |
18 // to the lifetime of this object. | |
bulach
2011/08/05 12:03:02
perhaps we can also add a comment saying that this
steveblock
2011/08/08 11:28:32
Done.
| |
19 template<typename T> | |
20 class ScopedJavaGlobalReference { | |
21 public: | |
22 // Holds a NULL reference. | |
23 ScopedJavaGlobalReference() | |
24 : env_(NULL), | |
25 obj_(NULL) { | |
26 } | |
27 // Takes a new global reference to the Java object held by obj. The global | |
28 // reference is deleted when this object goes out of scope. | |
bulach
2011/08/05 12:03:02
the second sentence seems a bit redundant with the
steveblock
2011/08/08 11:28:32
Done.
| |
29 explicit ScopedJavaGlobalReference(const ScopedJavaReference<T>& obj) | |
30 : env_(obj.env()), | |
31 obj_(static_cast<T>(obj.env()->NewGlobalRef(obj.obj()))) { | |
32 } | |
33 ~ScopedJavaGlobalReference() { | |
34 Reset(); | |
35 } | |
36 | |
37 void Reset() { | |
38 if (env_ && obj_) | |
39 env_->DeleteGlobalRef(obj_); | |
40 env_ = NULL; | |
41 obj_ = NULL; | |
42 } | |
43 void Reset(const ScopedJavaGlobalReference& other) { | |
44 Reset(); | |
45 env_ = other.env_; | |
46 obj_ = other.env_ ? static_cast<T>(other.env_->NewGlobalRef(other.obj_)) : | |
47 NULL; | |
48 } | |
bulach
2011/08/05 12:03:02
I'm not sure if we have a pattern for \n, but I'd
steveblock
2011/08/08 11:28:32
Done.
| |
49 T obj() const { return obj_; } | |
50 | |
51 private: | |
52 JNIEnv* env_; | |
53 T obj_; | |
54 | |
55 DISALLOW_COPY_AND_ASSIGN(ScopedJavaGlobalReference); | |
56 }; | |
57 | |
58 } // namespace android | |
59 } // namespace base | |
60 | |
61 #endif // BASE_ANDROID_SCOPED_JAVA_GLOBAL_REFERENCE_H_ | |
OLD | NEW |