Update V8 to version 4.5.34.
[chromium-blink-merge.git] / base / android / scoped_java_ref.cc
blobbb6f5032fe3454dfc2011c5a6a38c603714a84c5
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 JavaRef<jobject>::JavaRef() : obj_(NULL) {}
33 JavaRef<jobject>::JavaRef(JNIEnv* env, jobject obj) : obj_(obj) {
34 if (obj) {
35 DCHECK(env && env->GetObjectRefType(obj) == JNILocalRefType);
39 JavaRef<jobject>::~JavaRef() {
42 JNIEnv* JavaRef<jobject>::SetNewLocalRef(JNIEnv* env, jobject obj) {
43 if (!env) {
44 env = AttachCurrentThread();
45 } else {
46 DCHECK_EQ(env, AttachCurrentThread()); // Is |env| on correct thread.
48 if (obj)
49 obj = env->NewLocalRef(obj);
50 if (obj_)
51 env->DeleteLocalRef(obj_);
52 obj_ = obj;
53 return env;
56 void JavaRef<jobject>::SetNewGlobalRef(JNIEnv* env, jobject obj) {
57 if (!env) {
58 env = AttachCurrentThread();
59 } else {
60 DCHECK_EQ(env, AttachCurrentThread()); // Is |env| on correct thread.
62 if (obj)
63 obj = env->NewGlobalRef(obj);
64 if (obj_)
65 env->DeleteGlobalRef(obj_);
66 obj_ = obj;
69 void JavaRef<jobject>::ResetLocalRef(JNIEnv* env) {
70 if (obj_) {
71 DCHECK_EQ(env, AttachCurrentThread()); // Is |env| on correct thread.
72 env->DeleteLocalRef(obj_);
73 obj_ = NULL;
77 void JavaRef<jobject>::ResetGlobalRef() {
78 if (obj_) {
79 AttachCurrentThread()->DeleteGlobalRef(obj_);
80 obj_ = NULL;
84 jobject JavaRef<jobject>::ReleaseInternal() {
85 jobject obj = obj_;
86 obj_ = NULL;
87 return obj;
90 } // namespace android
91 } // namespace base