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 |
(...skipping 16 matching lines...) Expand all Loading... | |
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/LifecycleObserver.h" |
31 #include "wtf/HashSet.h" | 31 #include "wtf/HashSet.h" |
32 #include "wtf/PassOwnPtr.h" | 32 #include "wtf/PassOwnPtr.h" |
33 #include "wtf/TemporaryChange.h" | 33 #include "wtf/TemporaryChange.h" |
34 | 34 |
35 namespace blink { | 35 namespace blink { |
36 | 36 |
37 template<typename T> | 37 template<typename T, typename Observer> |
38 class LifecycleNotifier { | 38 class LifecycleNotifier { |
39 public: | 39 public: |
40 typedef LifecycleObserver<T> Observer; | |
41 typedef T Context; | 40 typedef T Context; |
42 | 41 |
43 virtual ~LifecycleNotifier(); | 42 virtual ~LifecycleNotifier(); |
44 virtual bool isContextThread() const { return true; } | 43 virtual bool isContextThread() const { return true; } |
45 | 44 |
46 // notifyContextDestroyed() should be explicitly dispatched from an | 45 // notifyContextDestroyed() should be explicitly dispatched from an |
47 // observed context to notify observers contextDestroyed(). | 46 // observed context to notify observers that contextDestroyed(). |
48 // At the point of contextDestroyed() is called, m_context is still | 47 // |
49 // valid and thus it is safe to use m_context during the notification. | 48 // When contextDestroyed() is called, m_context is still |
49 // valid and safe to use m_context during the notification. | |
50 virtual void notifyContextDestroyed(); | 50 virtual void notifyContextDestroyed(); |
51 | 51 |
52 // FIXME: this won't need to be virtual anymore. | |
53 virtual void addObserver(Observer*); | |
54 virtual void removeObserver(Observer*); | |
55 | |
56 DEFINE_INLINE_VIRTUAL_TRACE() { } | 52 DEFINE_INLINE_VIRTUAL_TRACE() { } |
57 | 53 |
58 bool isIteratingOverObservers() const { return m_iterating != IteratingNone; } | 54 bool isIteratingOverObservers() const { return m_iterating != IteratingNone; } |
59 | 55 |
60 protected: | 56 protected: |
61 explicit LifecycleNotifier(Context* context) | 57 explicit LifecycleNotifier(Context* context) |
62 : m_iterating(IteratingNone) | 58 : m_iterating(IteratingNone) |
63 , m_context(context) | 59 , m_context(context) |
64 , m_didCallContextDestroyed(false) | 60 , m_didCallContextDestroyed(false) |
65 { | 61 { |
66 } | 62 } |
67 | 63 |
64 void addObserver(Observer*); | |
65 void removeObserver(Observer*); | |
66 | |
68 Context* context() const { return m_context; } | 67 Context* context() const { return m_context; } |
69 | 68 |
70 enum IterationType { | 69 enum IterationType { |
71 IteratingNone, | 70 IteratingNone, |
haraken
2015/03/01 09:18:39
It looks like IteratingNone is unused.
sof
2015/03/01 17:21:37
It represents the default no-current-iteration sta
| |
72 IteratingOverAll, | 71 IteratingOverAll, |
73 IteratingOverActiveDOMObjects, | 72 IteratingOverActiveDOMObjects, |
74 IteratingOverDocumentObservers, | |
75 IteratingOverPageObservers, | |
76 IteratingOverDOMWindowObservers | |
77 }; | 73 }; |
78 | 74 |
79 IterationType m_iterating; | 75 IterationType m_iterating; |
80 | 76 |
81 private: | 77 protected: |
82 typedef HashSet<Observer*> ObserverSet; | 78 using ObserverSet = HashSet<Observer*>; |
83 | 79 |
84 ObserverSet m_observers; | 80 ObserverSet m_observers; |
81 | |
82 private: | |
85 Context* m_context; | 83 Context* m_context; |
86 bool m_didCallContextDestroyed; | 84 bool m_didCallContextDestroyed; |
87 }; | 85 }; |
88 | 86 |
89 template<typename T> | 87 template<typename T, typename Observer> |
90 inline LifecycleNotifier<T>::~LifecycleNotifier() | 88 inline LifecycleNotifier<T, Observer>::~LifecycleNotifier() |
91 { | 89 { |
92 // FIXME: Enable the following ASSERT. Also see a FIXME in Document::detach( ). | 90 // FIXME: Enable the following ASSERT. Also see a FIXME in Document::detach( ). |
93 // ASSERT(!m_observers.size() || m_didCallContextDestroyed); | 91 // ASSERT(!m_observers.size() || m_didCallContextDestroyed); |
94 | 92 |
95 #if !ENABLE(OILPAN) | 93 #if !ENABLE(OILPAN) |
96 TemporaryChange<IterationType> scope(this->m_iterating, IteratingOverAll); | 94 TemporaryChange<IterationType> scope(m_iterating, IteratingOverAll); |
97 for (Observer* observer : m_observers) { | 95 for (Observer* observer : m_observers) { |
98 ASSERT(observer->lifecycleContext() == m_context); | 96 ASSERT(observer->lifecycleContext() == m_context); |
99 observer->clearLifecycleContext(); | 97 observer->clearLifecycleContext(); |
100 } | 98 } |
101 #endif | 99 #endif |
102 } | 100 } |
103 | 101 |
104 template<typename T> | 102 template<typename T, typename Observer> |
105 inline void LifecycleNotifier<T>::notifyContextDestroyed() | 103 inline void LifecycleNotifier<T, Observer>::notifyContextDestroyed() |
106 { | 104 { |
107 // Don't notify contextDestroyed() twice. | 105 // Don't notify contextDestroyed() twice. |
108 if (m_didCallContextDestroyed) | 106 if (m_didCallContextDestroyed) |
109 return; | 107 return; |
110 | 108 |
111 TemporaryChange<IterationType> scope(this->m_iterating, IteratingOverAll); | 109 TemporaryChange<IterationType> scope(m_iterating, IteratingOverAll); |
112 Vector<Observer*> snapshotOfObservers; | 110 Vector<Observer*> snapshotOfObservers; |
113 copyToVector(m_observers, snapshotOfObservers); | 111 copyToVector(m_observers, snapshotOfObservers); |
114 for (Observer* observer : snapshotOfObservers) { | 112 for (Observer* observer : snapshotOfObservers) { |
115 // FIXME: Oilpan: At the moment, it's possible that the Observer is | 113 // FIXME: Oilpan: At the moment, it's possible that the Observer is |
116 // destructed during the iteration. Once we enable Oilpan by default | 114 // destructed during the iteration. Once we enable Oilpan by default |
117 // for Observers, we can remove the hack by making m_observers | 115 // for Observers, we can remove the hack by making m_observers |
118 // a HeapHashSet<WeakMember<Observers>>. (i.e., we can just iterate | 116 // a HeapHashSet<WeakMember<Observers>>. (i.e., we can just iterate |
119 // m_observers without taking a snapshot). | 117 // m_observers without taking a snapshot). |
120 if (m_observers.contains(observer)) { | 118 if (m_observers.contains(observer)) { |
121 ASSERT(observer->lifecycleContext() == m_context); | 119 ASSERT(observer->lifecycleContext() == m_context); |
122 observer->contextDestroyed(); | 120 observer->contextDestroyed(); |
123 } | 121 } |
124 } | 122 } |
125 m_didCallContextDestroyed = true; | 123 m_didCallContextDestroyed = true; |
126 } | 124 } |
127 | 125 |
128 template<typename T> | 126 template<typename T, typename Observer> |
129 inline void LifecycleNotifier<T>::addObserver(typename LifecycleNotifier<T>::Obs erver* observer) | 127 inline void LifecycleNotifier<T, Observer>::addObserver(Observer* observer) |
130 { | 128 { |
131 RELEASE_ASSERT(m_iterating != IteratingOverAll); | 129 RELEASE_ASSERT(m_iterating != IteratingOverAll); |
132 m_observers.add(observer); | 130 m_observers.add(observer); |
133 } | 131 } |
134 | 132 |
135 template<typename T> | 133 template<typename T, typename Observer> |
136 inline void LifecycleNotifier<T>::removeObserver(typename LifecycleNotifier<T>:: Observer* observer) | 134 inline void LifecycleNotifier<T, Observer>::removeObserver(Observer* observer) |
137 { | 135 { |
138 m_observers.remove(observer); | 136 m_observers.remove(observer); |
139 } | 137 } |
140 | 138 |
141 } // namespace blink | 139 } // namespace blink |
142 | 140 |
143 #endif // LifecycleNotifier_h | 141 #endif // LifecycleNotifier_h |
OLD | NEW |