1 // Copyright 2014 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/jni_weak_ref.h"
7 #include "base/android/jni_android.h"
8 #include "base/logging.h"
10 using base::android::AttachCurrentThread
;
12 JavaObjectWeakGlobalRef::JavaObjectWeakGlobalRef()
16 JavaObjectWeakGlobalRef::JavaObjectWeakGlobalRef(
17 const JavaObjectWeakGlobalRef
& orig
)
22 JavaObjectWeakGlobalRef::JavaObjectWeakGlobalRef(JNIEnv
* env
, jobject obj
)
23 : obj_(env
->NewWeakGlobalRef(obj
)) {
27 JavaObjectWeakGlobalRef::~JavaObjectWeakGlobalRef() {
31 void JavaObjectWeakGlobalRef::operator=(const JavaObjectWeakGlobalRef
& rhs
) {
35 void JavaObjectWeakGlobalRef::reset() {
37 AttachCurrentThread()->DeleteWeakGlobalRef(obj_
);
42 base::android::ScopedJavaLocalRef
<jobject
>
43 JavaObjectWeakGlobalRef::get(JNIEnv
* env
) const {
44 return GetRealObject(env
, obj_
);
47 base::android::ScopedJavaLocalRef
<jobject
> GetRealObject(
48 JNIEnv
* env
, jweak obj
) {
51 real
= env
->NewLocalRef(obj
);
53 DLOG(ERROR
) << "The real object has been deleted!";
55 return base::android::ScopedJavaLocalRef
<jobject
>(env
, real
);
58 void JavaObjectWeakGlobalRef::Assign(const JavaObjectWeakGlobalRef
& other
) {
62 JNIEnv
* env
= AttachCurrentThread();
64 env
->DeleteWeakGlobalRef(obj_
);
66 obj_
= other
.obj_
? env
->NewWeakGlobalRef(other
.obj_
) : NULL
;