1 // Copyright (c) 2011 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. 4 5 #include "include/base/cef_weak_ptr.h" 6 7 namespace base { 8 namespace internal { 9 Flag()10WeakReference::Flag::Flag() { 11 // Flags only become bound when checked for validity, or invalidated, 12 // so that we can check that later validity/invalidation operations on 13 // the same Flag take place on the same threadd thread. 14 thread_checker_.DetachFromThread(); 15 } 16 Invalidate()17void WeakReference::Flag::Invalidate() { 18 // The flag being invalidated with a single ref implies that there are no 19 // weak pointers in existence. Allow deletion on other thread in this case. 20 #if DCHECK_IS_ON() 21 DCHECK(thread_checker_.CalledOnValidThread() || HasOneRef()) 22 << "WeakPtrs must be invalidated on the same thread as where " 23 << "they are bound.\n"; 24 #endif 25 invalidated_.Set(); 26 } 27 IsValid() const28bool WeakReference::Flag::IsValid() const { 29 // WeakPtrs must be checked on the same threadd thread. 30 DCHECK(thread_checker_.CalledOnValidThread()); 31 return !invalidated_.IsSet(); 32 } 33 MaybeValid() const34bool WeakReference::Flag::MaybeValid() const { 35 return !invalidated_.IsSet(); 36 } 37 DetachFromThread()38void WeakReference::Flag::DetachFromThread() { 39 thread_checker_.DetachFromThread(); 40 } 41 42 WeakReference::Flag::~Flag() = default; 43 44 WeakReference::WeakReference() = default; 45 WeakReference(const scoped_refptr<Flag> & flag)46WeakReference::WeakReference(const scoped_refptr<Flag>& flag) : flag_(flag) {} 47 48 WeakReference::~WeakReference() = default; 49 50 WeakReference::WeakReference(WeakReference&& other) noexcept = default; 51 52 WeakReference::WeakReference(const WeakReference& other) = default; 53 IsValid() const54bool WeakReference::IsValid() const { 55 return flag_ && flag_->IsValid(); 56 } 57 MaybeValid() const58bool WeakReference::MaybeValid() const { 59 return flag_ && flag_->MaybeValid(); 60 } 61 WeakReferenceOwner()62WeakReferenceOwner::WeakReferenceOwner() 63 : flag_(MakeRefCounted<WeakReference::Flag>()) {} 64 ~WeakReferenceOwner()65WeakReferenceOwner::~WeakReferenceOwner() { 66 flag_->Invalidate(); 67 } 68 GetRef() const69WeakReference WeakReferenceOwner::GetRef() const { 70 // If we hold the last reference to the Flag then detach the ThreadChecker. 71 if (!HasRefs()) 72 flag_->DetachFromThread(); 73 74 return WeakReference(flag_); 75 } 76 Invalidate()77void WeakReferenceOwner::Invalidate() { 78 flag_->Invalidate(); 79 flag_ = MakeRefCounted<WeakReference::Flag>(); 80 } 81 WeakPtrBase()82WeakPtrBase::WeakPtrBase() : ptr_(0) {} 83 84 WeakPtrBase::~WeakPtrBase() = default; 85 WeakPtrBase(const WeakReference & ref,uintptr_t ptr)86WeakPtrBase::WeakPtrBase(const WeakReference& ref, uintptr_t ptr) 87 : ref_(ref), ptr_(ptr) { 88 DCHECK(ptr_); 89 } 90 WeakPtrFactoryBase(uintptr_t ptr)91WeakPtrFactoryBase::WeakPtrFactoryBase(uintptr_t ptr) : ptr_(ptr) { 92 DCHECK(ptr_); 93 } 94 ~WeakPtrFactoryBase()95WeakPtrFactoryBase::~WeakPtrFactoryBase() { 96 ptr_ = 0; 97 } 98 99 } // namespace internal 100 } // namespace base 101