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"
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() { env_
->PopLocalFrame(NULL
); }
32 // This constructor is inlined when DCHECKs are disabled; don't add anything
34 JavaRef
<jobject
>::JavaRef(JNIEnv
* env
, jobject obj
) : obj_(obj
) {
36 DCHECK(env
&& env
->GetObjectRefType(obj
) == JNILocalRefType
);
41 JNIEnv
* JavaRef
<jobject
>::SetNewLocalRef(JNIEnv
* env
, jobject obj
) {
43 env
= AttachCurrentThread();
45 DCHECK_EQ(env
, AttachCurrentThread()); // Is |env| on correct thread.
48 obj
= env
->NewLocalRef(obj
);
50 env
->DeleteLocalRef(obj_
);
55 void JavaRef
<jobject
>::SetNewGlobalRef(JNIEnv
* env
, jobject obj
) {
57 env
= AttachCurrentThread();
59 DCHECK_EQ(env
, AttachCurrentThread()); // Is |env| on correct thread.
62 obj
= env
->NewGlobalRef(obj
);
64 env
->DeleteGlobalRef(obj_
);
68 void JavaRef
<jobject
>::ResetLocalRef(JNIEnv
* env
) {
70 DCHECK_EQ(env
, AttachCurrentThread()); // Is |env| on correct thread.
71 env
->DeleteLocalRef(obj_
);
76 void JavaRef
<jobject
>::ResetGlobalRef() {
78 AttachCurrentThread()->DeleteGlobalRef(obj_
);
83 jobject JavaRef
<jobject
>::ReleaseInternal() {
89 } // namespace android