Limit spellcheck feedback to approximately 10 MB.
[chromium-blink-merge.git] / base / android / jni_weak_ref.cc
blob35a76dab7692bffbe4d4ed1c416189f05eee44f9
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()
13 : obj_(NULL) {
16 JavaObjectWeakGlobalRef::JavaObjectWeakGlobalRef(
17 const JavaObjectWeakGlobalRef& orig)
18 : obj_(NULL) {
19 Assign(orig);
22 JavaObjectWeakGlobalRef::JavaObjectWeakGlobalRef(JNIEnv* env, jobject obj)
23 : obj_(env->NewWeakGlobalRef(obj)) {
24 DCHECK(obj_);
27 JavaObjectWeakGlobalRef::~JavaObjectWeakGlobalRef() {
28 reset();
31 void JavaObjectWeakGlobalRef::operator=(const JavaObjectWeakGlobalRef& rhs) {
32 Assign(rhs);
35 void JavaObjectWeakGlobalRef::reset() {
36 if (obj_) {
37 AttachCurrentThread()->DeleteWeakGlobalRef(obj_);
38 obj_ = NULL;
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) {
49 jobject real = NULL;
50 if (obj) {
51 real = env->NewLocalRef(obj);
52 if (!real)
53 DLOG(ERROR) << "The real object has been deleted!";
55 return base::android::ScopedJavaLocalRef<jobject>(env, real);
58 void JavaObjectWeakGlobalRef::Assign(const JavaObjectWeakGlobalRef& other) {
59 if (&other == this)
60 return;
62 JNIEnv* env = AttachCurrentThread();
63 if (obj_)
64 env->DeleteWeakGlobalRef(obj_);
66 obj_ = other.obj_ ? env->NewWeakGlobalRef(other.obj_) : NULL;