OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/android/scoped_java_ref.h" | 5 #include "base/android/scoped_java_ref.h" |
6 | 6 |
| 7 #include "base/android/jni_android.h" |
7 #include "base/logging.h" | 8 #include "base/logging.h" |
8 | 9 |
9 namespace base { | 10 namespace base { |
10 namespace android { | 11 namespace android { |
11 | 12 |
12 JavaRef<jobject>::JavaRef() : env_(NULL), obj_(NULL) {} | 13 JavaRef<jobject>::JavaRef() : obj_(NULL) {} |
13 | 14 |
14 JavaRef<jobject>::JavaRef(JNIEnv* env, jobject obj) | 15 JavaRef<jobject>::JavaRef(JNIEnv* env, jobject obj) : obj_(obj) { |
15 : env_(env), | |
16 obj_(obj) { | |
17 if (obj) { | 16 if (obj) { |
18 DCHECK(env); | 17 DCHECK(env && env->GetObjectRefType(obj) == JNILocalRefType); |
19 DCHECK_EQ(JNILocalRefType, env->GetObjectRefType(obj)); | |
20 } | 18 } |
21 } | 19 } |
22 | 20 |
23 JavaRef<jobject>::~JavaRef() { | 21 JavaRef<jobject>::~JavaRef() { |
24 } | 22 } |
25 | 23 |
26 void JavaRef<jobject>::SetNewLocalRef(JNIEnv* env, jobject obj) { | 24 JNIEnv* JavaRef<jobject>::SetNewLocalRef(JNIEnv* env, jobject obj) { |
| 25 if (!env) { |
| 26 env = AttachCurrentThread(); |
| 27 } else { |
| 28 DCHECK_EQ(env, AttachCurrentThread()); // Is |env| on correct thread. |
| 29 } |
27 if (obj) | 30 if (obj) |
28 obj = env->NewLocalRef(obj); | 31 obj = env->NewLocalRef(obj); |
29 if (obj_) | 32 if (obj_) |
30 env_->DeleteLocalRef(obj_); | 33 env->DeleteLocalRef(obj_); |
31 env_ = env; | 34 obj_ = obj; |
| 35 return env; |
| 36 } |
| 37 |
| 38 void JavaRef<jobject>::SetNewGlobalRef(JNIEnv* env, jobject obj) { |
| 39 if (!env) { |
| 40 env = AttachCurrentThread(); |
| 41 } else { |
| 42 DCHECK_EQ(env, AttachCurrentThread()); // Is |env| on correct thread. |
| 43 } |
| 44 if (obj) |
| 45 obj = env->NewGlobalRef(obj); |
| 46 if (obj_) |
| 47 env->DeleteGlobalRef(obj_); |
32 obj_ = obj; | 48 obj_ = obj; |
33 } | 49 } |
34 | 50 |
35 void JavaRef<jobject>::SetNewGlobalRef(JNIEnv* env, jobject obj) { | 51 void JavaRef<jobject>::ResetLocalRef(JNIEnv* env) { |
36 if (obj) | 52 if (obj_) { |
37 obj = env->NewGlobalRef(obj); | 53 DCHECK_EQ(env, AttachCurrentThread()); // Is |env| on correct thread. |
38 if (obj_) | 54 env->DeleteLocalRef(obj_); |
39 env_->DeleteGlobalRef(obj_); | 55 obj_ = NULL; |
40 env_ = env; | 56 } |
41 obj_ = obj; | |
42 } | |
43 | |
44 void JavaRef<jobject>::ResetLocalRef() { | |
45 if (obj_) | |
46 env_->DeleteLocalRef(obj_); | |
47 env_ = NULL; | |
48 obj_ = NULL; | |
49 } | 57 } |
50 | 58 |
51 void JavaRef<jobject>::ResetGlobalRef() { | 59 void JavaRef<jobject>::ResetGlobalRef() { |
52 if (obj_) | 60 if (obj_) { |
53 env_->DeleteGlobalRef(obj_); | 61 AttachCurrentThread()->DeleteGlobalRef(obj_); |
54 env_ = NULL; | 62 obj_ = NULL; |
55 obj_ = NULL; | 63 } |
56 } | 64 } |
57 | 65 |
58 jobject JavaRef<jobject>::ReleaseInternal() { | 66 jobject JavaRef<jobject>::ReleaseInternal() { |
59 jobject obj = obj_; | 67 jobject obj = obj_; |
60 env_ = NULL; | |
61 obj_ = NULL; | 68 obj_ = NULL; |
62 return obj; | 69 return obj; |
63 } | 70 } |
64 | 71 |
65 } // namespace android | 72 } // namespace android |
66 } // namespace base | 73 } // namespace base |
OLD | NEW |