1 // Copyright (c) 2012 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.
5 #include "base/android/scoped_java_ref.h"
7 #include "base/android/jni_android.h"
8 #include "base/logging.h"
13 JavaRef
<jobject
>::JavaRef() : obj_(NULL
) {}
15 JavaRef
<jobject
>::JavaRef(JNIEnv
* env
, jobject obj
) : obj_(obj
) {
17 DCHECK(env
&& env
->GetObjectRefType(obj
) == JNILocalRefType
);
21 JavaRef
<jobject
>::~JavaRef() {
24 JNIEnv
* JavaRef
<jobject
>::SetNewLocalRef(JNIEnv
* env
, jobject obj
) {
26 env
= AttachCurrentThread();
28 DCHECK_EQ(env
, AttachCurrentThread()); // Is |env| on correct thread.
31 obj
= env
->NewLocalRef(obj
);
33 env
->DeleteLocalRef(obj_
);
38 void JavaRef
<jobject
>::SetNewGlobalRef(JNIEnv
* env
, jobject obj
) {
40 env
= AttachCurrentThread();
42 DCHECK_EQ(env
, AttachCurrentThread()); // Is |env| on correct thread.
45 obj
= env
->NewGlobalRef(obj
);
47 env
->DeleteGlobalRef(obj_
);
51 void JavaRef
<jobject
>::ResetLocalRef(JNIEnv
* env
) {
53 DCHECK_EQ(env
, AttachCurrentThread()); // Is |env| on correct thread.
54 env
->DeleteLocalRef(obj_
);
59 void JavaRef
<jobject
>::ResetGlobalRef() {
61 AttachCurrentThread()->DeleteGlobalRef(obj_
);
66 jobject JavaRef
<jobject
>::ReleaseInternal() {
72 } // namespace android