1 // Copyright 2012 The Chromium Authors
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/check_op.h"
14 const int kDefaultLocalFrameCapacity = 16;
18 ScopedJavaLocalFrame::ScopedJavaLocalFrame(JNIEnv* env) : env_(env) {
19 int failed = env_->PushLocalFrame(kDefaultLocalFrameCapacity);
23 ScopedJavaLocalFrame::ScopedJavaLocalFrame(JNIEnv* env, int capacity)
25 int failed = env_->PushLocalFrame(capacity);
29 ScopedJavaLocalFrame::~ScopedJavaLocalFrame() {
30 env_->PopLocalFrame(nullptr);
34 // This constructor is inlined when DCHECKs are disabled; don't add anything
36 JavaRef<jobject>::JavaRef(JNIEnv* env, jobject obj) : obj_(obj) {
38 DCHECK(env && env->GetObjectRefType(obj) == JNILocalRefType);
43 JNIEnv* JavaRef<jobject>::SetNewLocalRef(JNIEnv* env, jobject obj) {
45 env = AttachCurrentThread();
47 DCHECK_EQ(env, AttachCurrentThread()); // Is |env| on correct thread.
50 obj = env->NewLocalRef(obj);
52 env->DeleteLocalRef(obj_);
57 void JavaRef<jobject>::SetNewGlobalRef(JNIEnv* env, jobject obj) {
59 env = AttachCurrentThread();
61 DCHECK_EQ(env, AttachCurrentThread()); // Is |env| on correct thread.
64 obj = env->NewGlobalRef(obj);
66 env->DeleteGlobalRef(obj_);
70 void JavaRef<jobject>::ResetLocalRef(JNIEnv* env) {
72 DCHECK_EQ(env, AttachCurrentThread()); // Is |env| on correct thread.
73 env->DeleteLocalRef(obj_);
78 void JavaRef<jobject>::ResetGlobalRef() {
80 AttachCurrentThread()->DeleteGlobalRef(obj_);
85 jobject JavaRef<jobject>::ReleaseInternal() {
91 } // namespace android