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 "base/memory/weak_ptr.h" 6 7 namespace base { 8 namespace internal { 9 Flag()10WeakReference::Flag::Flag() : is_valid_(true) { 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 sequenced thread. 14 sequence_checker_.DetachFromSequence(); 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 DCHECK(sequence_checker_.CalledOnValidSequence() || HasOneRef()) 21 << "WeakPtrs must be invalidated on the same sequenced thread."; 22 is_valid_ = false; 23 } 24 IsValid() const25bool WeakReference::Flag::IsValid() const { 26 DCHECK(sequence_checker_.CalledOnValidSequence()) 27 << "WeakPtrs must be checked on the same sequenced thread."; 28 return is_valid_; 29 } 30 31 WeakReference::Flag::~Flag() = default; 32 33 WeakReference::WeakReference() = default; 34 WeakReference(const scoped_refptr<Flag> & flag)35WeakReference::WeakReference(const scoped_refptr<Flag>& flag) : flag_(flag) {} 36 37 WeakReference::~WeakReference() = default; 38 39 WeakReference::WeakReference(WeakReference&& other) = default; 40 41 WeakReference::WeakReference(const WeakReference& other) = default; 42 is_valid() const43bool WeakReference::is_valid() const { 44 return flag_ && flag_->IsValid(); 45 } 46 47 WeakReferenceOwner::WeakReferenceOwner() = default; 48 ~WeakReferenceOwner()49WeakReferenceOwner::~WeakReferenceOwner() { 50 Invalidate(); 51 } 52 GetRef() const53WeakReference WeakReferenceOwner::GetRef() const { 54 // If we hold the last reference to the Flag then create a new one. 55 if (!HasRefs()) 56 flag_ = new WeakReference::Flag(); 57 58 return WeakReference(flag_); 59 } 60 Invalidate()61void WeakReferenceOwner::Invalidate() { 62 if (flag_) { 63 flag_->Invalidate(); 64 flag_ = nullptr; 65 } 66 } 67 WeakPtrBase()68WeakPtrBase::WeakPtrBase() : ptr_(0) {} 69 70 WeakPtrBase::~WeakPtrBase() = default; 71 WeakPtrBase(const WeakReference & ref,uintptr_t ptr)72WeakPtrBase::WeakPtrBase(const WeakReference& ref, uintptr_t ptr) 73 : ref_(ref), ptr_(ptr) { 74 DCHECK(ptr_); 75 } 76 WeakPtrFactoryBase(uintptr_t ptr)77WeakPtrFactoryBase::WeakPtrFactoryBase(uintptr_t ptr) : ptr_(ptr) { 78 DCHECK(ptr_); 79 } 80 ~WeakPtrFactoryBase()81WeakPtrFactoryBase::~WeakPtrFactoryBase() { 82 ptr_ = 0; 83 } 84 85 } // namespace internal 86 } // namespace base 87