diff options
author | bulach@chromium.org <bulach@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-08-08 16:13:54 +0000 |
---|---|---|
committer | bulach@chromium.org <bulach@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-08-08 16:13:54 +0000 |
commit | 11e9325ed960cb1e5e763c1699d6714b191e0037 (patch) | |
tree | cba2aae5b535c14712f5aa93b4a3cca8bd0751ae /base/android | |
parent | cdfca9705f17519a7805280cdc49397ed428f058 (diff) | |
download | chromium_src-11e9325ed960cb1e5e763c1699d6714b191e0037.zip chromium_src-11e9325ed960cb1e5e763c1699d6714b191e0037.tar.gz chromium_src-11e9325ed960cb1e5e763c1699d6714b191e0037.tar.bz2 |
Add AutoJObject and AutoGlobalJObject
BUG=
TEST=
Review URL: http://codereview.chromium.org/7480016
Patch from Steve Block <steveblock@google.com>.
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@95821 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/android')
-rw-r--r-- | base/android/scoped_java_global_reference.h | 63 | ||||
-rw-r--r-- | base/android/scoped_java_reference.h | 69 |
2 files changed, 132 insertions, 0 deletions
diff --git a/base/android/scoped_java_global_reference.h b/base/android/scoped_java_global_reference.h new file mode 100644 index 0000000..a5f33f0 --- /dev/null +++ b/base/android/scoped_java_global_reference.h @@ -0,0 +1,63 @@ +// 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. Note that since a JNI Env is only suitable +// for use on a single thread, objects of this class must be created, used and +// destroyed on the same thread. +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. + 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; + } + + 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_ diff --git a/base/android/scoped_java_reference.h b/base/android/scoped_java_reference.h new file mode 100644 index 0000000..70e8610 --- /dev/null +++ b/base/android/scoped_java_reference.h @@ -0,0 +1,69 @@ +// 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_REFERENCE_H_ +#define BASE_ANDROID_SCOPED_JAVA_REFERENCE_H_ + +#include <jni.h> +#include <stddef.h> + +namespace base { +namespace android { + +// Holds a local reference to a Java object. The local reference is scoped +// to the lifetime of this object. Note that since a JNI Env is only suitable +// for use on a single thread, objects of this class must be created, used and +// destroyed on the same thread. +template<typename T> +class ScopedJavaReference { + public: + // Holds a NULL reference. + ScopedJavaReference() + : env_(NULL), + obj_(NULL) { + } + // Assumes that obj is a local reference to a Java object and takes ownership + // of this local reference. + ScopedJavaReference(JNIEnv* env, T obj) + : env_(env), + obj_(obj) { + } + // Takes a new local reference to the Java object held by other. + ScopedJavaReference(const ScopedJavaReference& other) + : env_(other.env_), + obj_(other.obj_ ? static_cast<T>(other.env_->NewLocalRef(other.obj_)) : + NULL) { + } + ~ScopedJavaReference() { + if (obj_) + env_->DeleteLocalRef(obj_); + } + + void operator=(const ScopedJavaReference& other) { + if (obj_) + env_->DeleteLocalRef(obj_); + env_ = other.env_; + obj_ = other.obj_ ? static_cast<T>(env_->NewLocalRef(other.obj_)) : NULL; + } + + JNIEnv* env() const { return env_; } + T obj() const { return obj_; } + + // Releases the local reference to the caller. The caller *must* delete the + // local reference when it is done with it. + T Release() { + jobject obj = obj_; + obj_ = NULL; + return obj; + } + + private: + JNIEnv* env_; + T obj_; +}; + +} // namespace android +} // namespace base + +#endif // BASE_ANDROID_SCOPED_JAVA_REFERENCE_H_ |