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
); }
31 JavaRef
<jobject
>::JavaRef() : obj_(NULL
) {}
33 JavaRef
<jobject
>::JavaRef(JNIEnv
* env
, jobject obj
) : obj_(obj
) {
35 DCHECK(env
&& env
->GetObjectRefType(obj
) == JNILocalRefType
);
39 JavaRef
<jobject
>::~JavaRef() {
42 JNIEnv
* JavaRef
<jobject
>::SetNewLocalRef(JNIEnv
* env
, jobject obj
) {
44 env
= AttachCurrentThread();
46 DCHECK_EQ(env
, AttachCurrentThread()); // Is |env| on correct thread.
49 obj
= env
->NewLocalRef(obj
);
51 env
->DeleteLocalRef(obj_
);
56 void JavaRef
<jobject
>::SetNewGlobalRef(JNIEnv
* env
, jobject obj
) {
58 env
= AttachCurrentThread();
60 DCHECK_EQ(env
, AttachCurrentThread()); // Is |env| on correct thread.
63 obj
= env
->NewGlobalRef(obj
);
65 env
->DeleteGlobalRef(obj_
);
69 void JavaRef
<jobject
>::ResetLocalRef(JNIEnv
* env
) {
71 DCHECK_EQ(env
, AttachCurrentThread()); // Is |env| on correct thread.
72 env
->DeleteLocalRef(obj_
);
77 void JavaRef
<jobject
>::ResetGlobalRef() {
79 AttachCurrentThread()->DeleteGlobalRef(obj_
);
84 jobject JavaRef
<jobject
>::ReleaseInternal() {
90 } // namespace android