fixup! [M120 Migration] Notify media device state to webbrowser
[platform/framework/web/chromium-efl.git] / base / observer_list_types.h
1 // Copyright 2018 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef BASE_OBSERVER_LIST_TYPES_H_
6 #define BASE_OBSERVER_LIST_TYPES_H_
7
8 #include "base/base_export.h"
9 #include "base/memory/weak_ptr.h"
10
11 namespace base {
12 namespace internal {
13 class CheckedObserverAdapter;
14 }
15
16 // A CheckedObserver serves as a base class for an observer interface designed
17 // to be used with base::ObserverList. It helps detect potential use-after-free
18 // issues that can occur when observers fail to remove themselves from an
19 // observer list upon destruction.
20 //
21 // A CheckedObserver will CHECK() if an ObserverList iteration is attempted over
22 // a destroyed Observer.
23 //
24 // Note that a CheckedObserver subclass must be deleted on the same thread as
25 // the ObserverList(s) it is added to. This is DCHECK()ed via WeakPtr.
26 class BASE_EXPORT CheckedObserver {
27  public:
28   CheckedObserver();
29   CheckedObserver(const CheckedObserver&) = delete;
30   CheckedObserver& operator=(const CheckedObserver&) = delete;
31
32   virtual ~CheckedObserver();
33
34   // Returns whether |this| is in any ObserverList. Subclasses can CHECK() this
35   // in their destructor to obtain a nicer stacktrace.
36   bool IsInObserverList() const;
37
38  private:
39   friend class internal::CheckedObserverAdapter;
40
41   // Must be mutable to allow ObserverList<const Foo>.
42   mutable WeakPtrFactory<CheckedObserver> factory_{this};
43 };
44
45 }  // namespace base
46
47 #endif  // BASE_OBSERVER_LIST_TYPES_H_