2 * Copyright (C) 2008 Apple Inc. All Rights Reserved.
3 * Copyright (C) 2013 Google Inc. All Rights Reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #ifndef LifecycleNotifier_h
28 #define LifecycleNotifier_h
30 #include "platform/heap/Handle.h"
31 #include "wtf/HashSet.h"
32 #include "wtf/TemporaryChange.h"
36 template<typename T
, typename Observer
>
37 class LifecycleNotifier
: public virtual WillBeGarbageCollectedMixin
{
39 virtual ~LifecycleNotifier();
41 void addObserver(Observer
*);
42 void removeObserver(Observer
*);
44 // notifyContextDestroyed() should be explicitly dispatched from an
45 // observed context to notify observers that contextDestroyed().
47 // When contextDestroyed() is called, the observer's lifecycleContext()
48 // is still valid and safe to use during the notification.
49 virtual void notifyContextDestroyed();
51 DEFINE_INLINE_VIRTUAL_TRACE()
54 visitor
->trace(m_observers
);
58 bool isIteratingOverObservers() const { return m_iterating
!= IteratingNone
; }
62 : m_iterating(IteratingNone
)
63 , m_didCallContextDestroyed(false)
72 IterationType m_iterating
;
75 using ObserverSet
= WillBeHeapHashSet
<RawPtrWillBeWeakMember
<Observer
>>;
77 ObserverSet m_observers
;
80 T
* context() { return static_cast<T
*>(this); }
84 bool m_didCallContextDestroyed
;
87 template<typename T
, typename Observer
>
88 inline LifecycleNotifier
<T
, Observer
>::~LifecycleNotifier()
90 // FIXME: Enable the following ASSERT. Also see a FIXME in Document::detach().
91 // ASSERT(!m_observers.size() || m_didCallContextDestroyed);
94 TemporaryChange
<IterationType
> scope(m_iterating
, IteratingOverAll
);
95 for (Observer
* observer
: m_observers
) {
96 ASSERT(observer
->lifecycleContext() == context());
97 observer
->clearLifecycleContext();
102 template<typename T
, typename Observer
>
103 inline void LifecycleNotifier
<T
, Observer
>::notifyContextDestroyed()
105 // Don't notify contextDestroyed() twice.
106 if (m_didCallContextDestroyed
)
109 TemporaryChange
<IterationType
> scope(m_iterating
, IteratingOverAll
);
110 Vector
<Observer
*> snapshotOfObservers
;
111 copyToVector(m_observers
, snapshotOfObservers
);
112 for (Observer
* observer
: snapshotOfObservers
) {
113 // FIXME: Oilpan: At the moment, it's possible that the Observer is
114 // destructed during the iteration. Once we enable Oilpan by default
115 // for Observers, we can remove the hack by making m_observers
116 // a HeapHashSet<WeakMember<Observers>>. (i.e., we can just iterate
117 // m_observers without taking a snapshot).
118 if (m_observers
.contains(observer
)) {
119 ASSERT(observer
->lifecycleContext() == context());
120 observer
->contextDestroyed();
123 m_didCallContextDestroyed
= true;
126 template<typename T
, typename Observer
>
127 inline void LifecycleNotifier
<T
, Observer
>::addObserver(Observer
* observer
)
129 RELEASE_ASSERT(m_iterating
!= IteratingOverAll
);
130 m_observers
.add(observer
);
133 template<typename T
, typename Observer
>
134 inline void LifecycleNotifier
<T
, Observer
>::removeObserver(Observer
* observer
)
136 m_observers
.remove(observer
);
141 #endif // LifecycleNotifier_h