Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(853)

Unified Diff: base/android/scoped_java_global_reference.h

Issue 7480016: Add AutoJObject and AutoGlobalJObject (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Correct upload error Created 9 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | base/android/scoped_java_reference.h » ('j') | base/android/scoped_java_reference.h » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/android/scoped_java_global_reference.h
diff --git a/base/android/scoped_java_global_reference.h b/base/android/scoped_java_global_reference.h
new file mode 100644
index 0000000000000000000000000000000000000000..b54dded4bf2ce50629f3578e7792375931c8bc3c
--- /dev/null
+++ b/base/android/scoped_java_global_reference.h
@@ -0,0 +1,61 @@
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef BASE_ANDROID_SCOPED_JAVA_GLOBAL_REFERENCE_H_
+#define BASE_ANDROID_SCOPED_JAVA_GLOBAL_REFERENCE_H_
+
+#include <jni.h>
+#include <stddef.h>
+
+#include "base/android/scoped_java_reference.h"
+#include "base/basictypes.h"
+
+namespace base {
+namespace android {
+
+// Holds a global reference to a Java object. The global reference is scoped
+// 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.
+template<typename T>
+class ScopedJavaGlobalReference {
+ public:
+ // Holds a NULL reference.
+ ScopedJavaGlobalReference()
+ : env_(NULL),
+ obj_(NULL) {
+ }
+ // Takes a new global reference to the Java object held by obj. The global
+ // 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.
+ explicit ScopedJavaGlobalReference(const ScopedJavaReference<T>& obj)
+ : env_(obj.env()),
+ obj_(static_cast<T>(obj.env()->NewGlobalRef(obj.obj()))) {
+ }
+ ~ScopedJavaGlobalReference() {
+ Reset();
+ }
+
+ void Reset() {
+ if (env_ && obj_)
+ env_->DeleteGlobalRef(obj_);
+ env_ = NULL;
+ obj_ = NULL;
+ }
+ void Reset(const ScopedJavaGlobalReference& other) {
+ Reset();
+ env_ = other.env_;
+ obj_ = other.env_ ? static_cast<T>(other.env_->NewGlobalRef(other.obj_)) :
+ NULL;
+ }
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.
+ T obj() const { return obj_; }
+
+ private:
+ JNIEnv* env_;
+ T obj_;
+
+ DISALLOW_COPY_AND_ASSIGN(ScopedJavaGlobalReference);
+};
+
+} // namespace android
+} // namespace base
+
+#endif // BASE_ANDROID_SCOPED_JAVA_GLOBAL_REFERENCE_H_
« no previous file with comments | « no previous file | base/android/scoped_java_reference.h » ('j') | base/android/scoped_java_reference.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698