OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2008 Apple Inc. All Rights Reserved. | 2 * Copyright (C) 2008 Apple Inc. All Rights Reserved. |
3 * Copyright (C) 2013 Google Inc. All Rights Reserved. | 3 * Copyright (C) 2013 Google Inc. All Rights Reserved. |
4 * | 4 * |
5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
6 * modification, are permitted provided that the following conditions | 6 * modification, are permitted provided that the following conditions |
7 * are met: | 7 * are met: |
8 * 1. Redistributions of source code must retain the above copyright | 8 * 1. Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * 2. Redistributions in binary form must reproduce the above copyright | 10 * 2. Redistributions in binary form must reproduce the above copyright |
11 * notice, this list of conditions and the following disclaimer in the | 11 * notice, this list of conditions and the following disclaimer in the |
12 * documentation and/or other materials provided with the distribution. | 12 * documentation and/or other materials provided with the distribution. |
13 * | 13 * |
14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY | 14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY |
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR | 17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR |
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | 18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | 19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | 20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | 21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
25 * | 25 * |
26 */ | 26 */ |
27 #ifndef LifecycleNotifier_h | 27 #ifndef LifecycleNotifier_h |
28 #define LifecycleNotifier_h | 28 #define LifecycleNotifier_h |
29 | 29 |
30 #include "platform/LifecycleObserver.h" | 30 #include "platform/heap/Handle.h" |
31 #include "wtf/HashSet.h" | 31 #include "wtf/HashSet.h" |
32 #include "wtf/PassOwnPtr.h" | |
33 #include "wtf/TemporaryChange.h" | 32 #include "wtf/TemporaryChange.h" |
34 | 33 |
35 namespace blink { | 34 namespace blink { |
36 | 35 |
37 template<typename T, typename Observer> | 36 template<typename T, typename Observer> |
38 class LifecycleNotifier { | 37 class LifecycleNotifier { |
39 public: | 38 public: |
40 typedef T Context; | |
41 | |
42 virtual ~LifecycleNotifier(); | 39 virtual ~LifecycleNotifier(); |
43 virtual bool isContextThread() const { return true; } | |
44 | 40 |
45 void addObserver(Observer*); | 41 void addObserver(Observer*); |
46 void removeObserver(Observer*); | 42 void removeObserver(Observer*); |
47 | 43 |
48 // notifyContextDestroyed() should be explicitly dispatched from an | 44 // notifyContextDestroyed() should be explicitly dispatched from an |
49 // observed context to notify observers that contextDestroyed(). | 45 // observed context to notify observers that contextDestroyed(). |
50 // | 46 // |
51 // When contextDestroyed() is called, m_context is still | 47 // When contextDestroyed() is called, the observer's lifecycleContext() |
52 // valid and safe to use m_context during the notification. | 48 // is still valid and safe to use during the notification. |
53 virtual void notifyContextDestroyed(); | 49 virtual void notifyContextDestroyed(); |
54 | 50 |
55 DEFINE_INLINE_VIRTUAL_TRACE() { } | 51 DEFINE_INLINE_VIRTUAL_TRACE() { } |
56 | 52 |
57 bool isIteratingOverObservers() const { return m_iterating != IteratingNone;
} | 53 bool isIteratingOverObservers() const { return m_iterating != IteratingNone;
} |
58 | 54 |
59 protected: | 55 protected: |
60 explicit LifecycleNotifier(Context* context) | 56 LifecycleNotifier() |
61 : m_iterating(IteratingNone) | 57 : m_iterating(IteratingNone) |
62 , m_context(context) | |
63 , m_didCallContextDestroyed(false) | 58 , m_didCallContextDestroyed(false) |
64 { | 59 { |
65 } | 60 } |
66 | 61 |
67 Context* context() const { return m_context; } | |
68 | |
69 enum IterationType { | 62 enum IterationType { |
70 IteratingNone, | 63 IteratingNone, |
71 IteratingOverAll, | 64 IteratingOverAll, |
72 IteratingOverActiveDOMObjects, | 65 IteratingOverActiveDOMObjects, |
73 }; | 66 }; |
74 | 67 |
75 IterationType m_iterating; | 68 IterationType m_iterating; |
76 | 69 |
77 protected: | 70 protected: |
78 using ObserverSet = HashSet<Observer*>; | 71 using ObserverSet = HashSet<Observer*>; |
79 | 72 |
80 ObserverSet m_observers; | 73 ObserverSet m_observers; |
81 | 74 |
| 75 #if ENABLE(ASSERT) |
| 76 T* context() { return static_cast<T*>(this); } |
| 77 #endif |
| 78 |
82 private: | 79 private: |
83 Context* m_context; | |
84 bool m_didCallContextDestroyed; | 80 bool m_didCallContextDestroyed; |
85 }; | 81 }; |
86 | 82 |
87 template<typename T, typename Observer> | 83 template<typename T, typename Observer> |
88 inline LifecycleNotifier<T, Observer>::~LifecycleNotifier() | 84 inline LifecycleNotifier<T, Observer>::~LifecycleNotifier() |
89 { | 85 { |
90 // FIXME: Enable the following ASSERT. Also see a FIXME in Document::detach(
). | 86 // FIXME: Enable the following ASSERT. Also see a FIXME in Document::detach(
). |
91 // ASSERT(!m_observers.size() || m_didCallContextDestroyed); | 87 // ASSERT(!m_observers.size() || m_didCallContextDestroyed); |
92 | 88 |
93 #if !ENABLE(OILPAN) | 89 #if !ENABLE(OILPAN) |
94 TemporaryChange<IterationType> scope(m_iterating, IteratingOverAll); | 90 TemporaryChange<IterationType> scope(m_iterating, IteratingOverAll); |
95 for (Observer* observer : m_observers) { | 91 for (Observer* observer : m_observers) { |
96 ASSERT(observer->lifecycleContext() == m_context); | 92 ASSERT(observer->lifecycleContext() == context()); |
97 observer->clearLifecycleContext(); | 93 observer->clearLifecycleContext(); |
98 } | 94 } |
99 #endif | 95 #endif |
100 } | 96 } |
101 | 97 |
102 template<typename T, typename Observer> | 98 template<typename T, typename Observer> |
103 inline void LifecycleNotifier<T, Observer>::notifyContextDestroyed() | 99 inline void LifecycleNotifier<T, Observer>::notifyContextDestroyed() |
104 { | 100 { |
105 // Don't notify contextDestroyed() twice. | 101 // Don't notify contextDestroyed() twice. |
106 if (m_didCallContextDestroyed) | 102 if (m_didCallContextDestroyed) |
107 return; | 103 return; |
108 | 104 |
109 TemporaryChange<IterationType> scope(m_iterating, IteratingOverAll); | 105 TemporaryChange<IterationType> scope(m_iterating, IteratingOverAll); |
110 Vector<Observer*> snapshotOfObservers; | 106 Vector<Observer*> snapshotOfObservers; |
111 copyToVector(m_observers, snapshotOfObservers); | 107 copyToVector(m_observers, snapshotOfObservers); |
112 for (Observer* observer : snapshotOfObservers) { | 108 for (Observer* observer : snapshotOfObservers) { |
113 // FIXME: Oilpan: At the moment, it's possible that the Observer is | 109 // FIXME: Oilpan: At the moment, it's possible that the Observer is |
114 // destructed during the iteration. Once we enable Oilpan by default | 110 // destructed during the iteration. Once we enable Oilpan by default |
115 // for Observers, we can remove the hack by making m_observers | 111 // for Observers, we can remove the hack by making m_observers |
116 // a HeapHashSet<WeakMember<Observers>>. (i.e., we can just iterate | 112 // a HeapHashSet<WeakMember<Observers>>. (i.e., we can just iterate |
117 // m_observers without taking a snapshot). | 113 // m_observers without taking a snapshot). |
118 if (m_observers.contains(observer)) { | 114 if (m_observers.contains(observer)) { |
119 ASSERT(observer->lifecycleContext() == m_context); | 115 ASSERT(observer->lifecycleContext() == context()); |
120 observer->contextDestroyed(); | 116 observer->contextDestroyed(); |
121 } | 117 } |
122 } | 118 } |
123 m_didCallContextDestroyed = true; | 119 m_didCallContextDestroyed = true; |
124 } | 120 } |
125 | 121 |
126 template<typename T, typename Observer> | 122 template<typename T, typename Observer> |
127 inline void LifecycleNotifier<T, Observer>::addObserver(Observer* observer) | 123 inline void LifecycleNotifier<T, Observer>::addObserver(Observer* observer) |
128 { | 124 { |
129 RELEASE_ASSERT(m_iterating != IteratingOverAll); | 125 RELEASE_ASSERT(m_iterating != IteratingOverAll); |
130 m_observers.add(observer); | 126 m_observers.add(observer); |
131 } | 127 } |
132 | 128 |
133 template<typename T, typename Observer> | 129 template<typename T, typename Observer> |
134 inline void LifecycleNotifier<T, Observer>::removeObserver(Observer* observer) | 130 inline void LifecycleNotifier<T, Observer>::removeObserver(Observer* observer) |
135 { | 131 { |
136 m_observers.remove(observer); | 132 m_observers.remove(observer); |
137 } | 133 } |
138 | 134 |
139 } // namespace blink | 135 } // namespace blink |
140 | 136 |
141 #endif // LifecycleNotifier_h | 137 #endif // LifecycleNotifier_h |
OLD | NEW |