Update V8 to version 4.7.21.
[chromium-blink-merge.git] / base / android / scoped_java_ref.cc
blob4d4ef6da191ecba41239f0c7578f57d5e1204571
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"
10 namespace base {
11 namespace android {
12 namespace {
14 const int kDefaultLocalFrameCapacity = 16;
16 } // namespace
18 ScopedJavaLocalFrame::ScopedJavaLocalFrame(JNIEnv* env) : env_(env) {
19 int failed = env_->PushLocalFrame(kDefaultLocalFrameCapacity);
20 DCHECK(!failed);
23 ScopedJavaLocalFrame::ScopedJavaLocalFrame(JNIEnv* env, int capacity)
24 : env_(env) {
25 int failed = env_->PushLocalFrame(capacity);
26 DCHECK(!failed);
29 ScopedJavaLocalFrame::~ScopedJavaLocalFrame() { env_->PopLocalFrame(NULL); }
31 #if DCHECK_IS_ON()
32 // This constructor is inlined when DCHECKs are disabled; don't add anything
33 // else here.
34 JavaRef<jobject>::JavaRef(JNIEnv* env, jobject obj) : obj_(obj) {
35 if (obj) {
36 DCHECK(env && env->GetObjectRefType(obj) == JNILocalRefType);
39 #endif
41 JNIEnv* JavaRef<jobject>::SetNewLocalRef(JNIEnv* env, jobject obj) {
42 if (!env) {
43 env = AttachCurrentThread();
44 } else {
45 DCHECK_EQ(env, AttachCurrentThread()); // Is |env| on correct thread.
47 if (obj)
48 obj = env->NewLocalRef(obj);
49 if (obj_)
50 env->DeleteLocalRef(obj_);
51 obj_ = obj;
52 return env;
55 void JavaRef<jobject>::SetNewGlobalRef(JNIEnv* env, jobject obj) {
56 if (!env) {
57 env = AttachCurrentThread();
58 } else {
59 DCHECK_EQ(env, AttachCurrentThread()); // Is |env| on correct thread.
61 if (obj)
62 obj = env->NewGlobalRef(obj);
63 if (obj_)
64 env->DeleteGlobalRef(obj_);
65 obj_ = obj;
68 void JavaRef<jobject>::ResetLocalRef(JNIEnv* env) {
69 if (obj_) {
70 DCHECK_EQ(env, AttachCurrentThread()); // Is |env| on correct thread.
71 env->DeleteLocalRef(obj_);
72 obj_ = NULL;
76 void JavaRef<jobject>::ResetGlobalRef() {
77 if (obj_) {
78 AttachCurrentThread()->DeleteGlobalRef(obj_);
79 obj_ = NULL;
83 jobject JavaRef<jobject>::ReleaseInternal() {
84 jobject obj = obj_;
85 obj_ = NULL;
86 return obj;
89 } // namespace android
90 } // namespace base