blob: 4d72a2b7011a936005e6ac60a8a361180eab3d75 [file] [log] [blame]
// Copyright 2018 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 "base/base_export.h"
#include "base/logging.h"
#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "base/observer_list_types.h"
namespace base {
namespace internal {
// Adapter for putting raw pointers into an ObserverList<Foo>::Unchecked.
class BASE_EXPORT UncheckedObserverAdapter {
explicit UncheckedObserverAdapter(const void* observer)
: ptr_(const_cast<void*>(observer)) {}
UncheckedObserverAdapter(UncheckedObserverAdapter&& other) = default;
UncheckedObserverAdapter& operator=(UncheckedObserverAdapter&& other) =
void MarkForRemoval() { ptr_ = nullptr; }
bool IsMarkedForRemoval() const { return !ptr_; }
bool IsEqual(const void* rhs) const { return ptr_ == rhs; }
template <class ObserverType>
static ObserverType* Get(const UncheckedObserverAdapter& adapter) {
!std::is_base_of<CheckedObserver, ObserverType>::value,
"CheckedObserver classes must not use ObserverList<T>::Unchecked.");
return static_cast<ObserverType*>(adapter.ptr_);
void* ptr_;
// Adapter for CheckedObserver types so that they can use the same syntax as a
// raw pointer when stored in the std::vector of observers in an ObserverList.
// It wraps a WeakPtr<CheckedObserver> and allows a "null" pointer via
// destruction to be distinguished from an observer marked for deferred removal
// whilst an iteration is in progress.
class BASE_EXPORT CheckedObserverAdapter {
explicit CheckedObserverAdapter(const CheckedObserver* observer);
// Move-only construction and assignment is required to store this in STL
// types.
CheckedObserverAdapter(CheckedObserverAdapter&& other);
CheckedObserverAdapter& operator=(CheckedObserverAdapter&& other);
void MarkForRemoval() {
weak_ptr_ = nullptr;
bool IsMarkedForRemoval() const {
// If |weak_ptr_| was invalidated then this attempt to iterate over the
// pointer is a UAF. Tip: If it's unclear where the `delete` occurred, try
// adding CHECK(!IsInObserverList()) to the ~CheckedObserver() (destructor)
// override. However, note that this is not always a bug: a destroyed
// observer can exist in an ObserverList so long as nothing iterates over
// the ObserverList before the list itself is destroyed.
return weak_ptr_ == nullptr;
bool IsEqual(const CheckedObserver* rhs) const {
// Note that inside an iteration, ObserverList::HasObserver() may call this
// and |weak_ptr_| may be null due to a deferred removal, which is fine.
return weak_ptr_.get() == rhs;
template <class ObserverType>
static ObserverType* Get(const CheckedObserverAdapter& adapter) {
std::is_base_of<CheckedObserver, ObserverType>::value,
"Observers should inherit from base::CheckedObserver. "
"Use ObserverList<T>::Unchecked to observe with raw pointers.");
return static_cast<ObserverType*>(adapter.weak_ptr_.get());
WeakPtr<CheckedObserver> weak_ptr_;
} // namespace internal
} // namespace base