2014-07-15 00:18:51 +02:00
|
|
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "include/base/cef_weak_ptr.h"
|
|
|
|
|
|
|
|
namespace base {
|
2021-06-17 21:40:57 +02:00
|
|
|
namespace internal {
|
2014-07-15 00:18:51 +02:00
|
|
|
|
2021-06-17 21:40:57 +02:00
|
|
|
WeakReference::Flag::Flag() {
|
2014-07-15 00:18:51 +02:00
|
|
|
// Flags only become bound when checked for validity, or invalidated,
|
|
|
|
// so that we can check that later validity/invalidation operations on
|
2021-06-17 21:40:57 +02:00
|
|
|
// the same Flag take place on the same threadd thread.
|
2014-07-15 00:18:51 +02:00
|
|
|
thread_checker_.DetachFromThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WeakReference::Flag::Invalidate() {
|
|
|
|
// The flag being invalidated with a single ref implies that there are no
|
|
|
|
// weak pointers in existence. Allow deletion on other thread in this case.
|
2021-06-17 21:40:57 +02:00
|
|
|
#if DCHECK_IS_ON()
|
2014-07-15 00:18:51 +02:00
|
|
|
DCHECK(thread_checker_.CalledOnValidThread() || HasOneRef())
|
2021-06-17 21:40:57 +02:00
|
|
|
<< "WeakPtrs must be invalidated on the same thread as where "
|
|
|
|
<< "they are bound.\n";
|
|
|
|
#endif
|
|
|
|
invalidated_.Set();
|
2014-07-15 00:18:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool WeakReference::Flag::IsValid() const {
|
2021-06-17 21:40:57 +02:00
|
|
|
// WeakPtrs must be checked on the same threadd thread.
|
|
|
|
DCHECK(thread_checker_.CalledOnValidThread());
|
|
|
|
return !invalidated_.IsSet();
|
2014-07-15 00:18:51 +02:00
|
|
|
}
|
|
|
|
|
2021-06-17 21:40:57 +02:00
|
|
|
bool WeakReference::Flag::MaybeValid() const {
|
|
|
|
return !invalidated_.IsSet();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WeakReference::Flag::DetachFromThread() {
|
|
|
|
thread_checker_.DetachFromThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
WeakReference::Flag::~Flag() = default;
|
|
|
|
|
|
|
|
WeakReference::WeakReference() = default;
|
|
|
|
|
|
|
|
WeakReference::WeakReference(const scoped_refptr<Flag>& flag) : flag_(flag) {}
|
2014-07-15 00:18:51 +02:00
|
|
|
|
2021-06-17 21:40:57 +02:00
|
|
|
WeakReference::~WeakReference() = default;
|
2014-07-15 00:18:51 +02:00
|
|
|
|
2021-06-17 21:40:57 +02:00
|
|
|
WeakReference::WeakReference(WeakReference&& other) noexcept = default;
|
2014-07-15 00:18:51 +02:00
|
|
|
|
2021-06-17 21:40:57 +02:00
|
|
|
WeakReference::WeakReference(const WeakReference& other) = default;
|
2014-07-15 00:18:51 +02:00
|
|
|
|
2021-06-17 21:40:57 +02:00
|
|
|
bool WeakReference::IsValid() const {
|
|
|
|
return flag_ && flag_->IsValid();
|
2014-07-15 00:18:51 +02:00
|
|
|
}
|
|
|
|
|
2021-06-17 21:40:57 +02:00
|
|
|
bool WeakReference::MaybeValid() const {
|
|
|
|
return flag_ && flag_->MaybeValid();
|
|
|
|
}
|
|
|
|
|
|
|
|
WeakReferenceOwner::WeakReferenceOwner()
|
|
|
|
: flag_(MakeRefCounted<WeakReference::Flag>()) {}
|
2017-05-17 11:29:28 +02:00
|
|
|
|
2014-07-15 00:18:51 +02:00
|
|
|
WeakReferenceOwner::~WeakReferenceOwner() {
|
2021-06-17 21:40:57 +02:00
|
|
|
flag_->Invalidate();
|
2014-07-15 00:18:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
WeakReference WeakReferenceOwner::GetRef() const {
|
2021-06-17 21:40:57 +02:00
|
|
|
// If we hold the last reference to the Flag then detach the ThreadChecker.
|
2014-07-15 00:18:51 +02:00
|
|
|
if (!HasRefs())
|
2021-06-17 21:40:57 +02:00
|
|
|
flag_->DetachFromThread();
|
2014-07-15 00:18:51 +02:00
|
|
|
|
2021-06-17 21:40:57 +02:00
|
|
|
return WeakReference(flag_);
|
2014-07-15 00:18:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void WeakReferenceOwner::Invalidate() {
|
2021-06-17 21:40:57 +02:00
|
|
|
flag_->Invalidate();
|
|
|
|
flag_ = MakeRefCounted<WeakReference::Flag>();
|
2014-07-15 00:18:51 +02:00
|
|
|
}
|
|
|
|
|
2021-06-17 21:40:57 +02:00
|
|
|
WeakPtrBase::WeakPtrBase() : ptr_(0) {}
|
2014-07-15 00:18:51 +02:00
|
|
|
|
2021-06-17 21:40:57 +02:00
|
|
|
WeakPtrBase::~WeakPtrBase() = default;
|
|
|
|
|
|
|
|
WeakPtrBase::WeakPtrBase(const WeakReference& ref, uintptr_t ptr)
|
|
|
|
: ref_(ref), ptr_(ptr) {
|
|
|
|
DCHECK(ptr_);
|
|
|
|
}
|
2014-07-15 00:18:51 +02:00
|
|
|
|
2021-06-17 21:40:57 +02:00
|
|
|
WeakPtrFactoryBase::WeakPtrFactoryBase(uintptr_t ptr) : ptr_(ptr) {
|
|
|
|
DCHECK(ptr_);
|
|
|
|
}
|
|
|
|
|
|
|
|
WeakPtrFactoryBase::~WeakPtrFactoryBase() {
|
|
|
|
ptr_ = 0;
|
|
|
|
}
|
2014-07-15 00:18:51 +02:00
|
|
|
|
2021-06-17 21:40:57 +02:00
|
|
|
} // namespace internal
|
2014-07-15 00:18:51 +02:00
|
|
|
} // namespace base
|