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 22 matching lines...) Expand all Loading... |
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> |
38 class LifecycleNotifier { | 38 class LifecycleNotifier { |
39 public: | 39 public: |
40 typedef LifecycleObserver<T> Observer; | 40 typedef LifecycleObserver<T> Observer; |
41 typedef T Context; | 41 typedef T Context; |
42 | 42 |
| 43 static PassOwnPtr<LifecycleNotifier> create(Context* context) |
| 44 { |
| 45 return adoptPtr(new LifecycleNotifier(context)); |
| 46 } |
| 47 |
43 virtual ~LifecycleNotifier(); | 48 virtual ~LifecycleNotifier(); |
44 virtual bool isContextThread() const { return true; } | |
45 | 49 |
46 // notifyContextDestroyed() should be explicitly dispatched from an | 50 // notifyContextDestroyed() should be explicitly dispatched from an |
47 // observed context to notify observers contextDestroyed(). | 51 // observed context to notify observers contextDestroyed(). |
48 // At the point of contextDestroyed() is called, m_context is still | 52 // At the point of contextDestroyed() is called, m_context is still |
49 // valid and thus it is safe to use m_context during the notification. | 53 // valid and thus it is safe to use m_context during the notification. |
50 virtual void notifyContextDestroyed(); | 54 virtual void notifyContextDestroyed(); |
51 | 55 |
52 // FIXME: this won't need to be virtual anymore. | 56 // FIXME: this won't need to be virtual anymore. |
53 virtual void addObserver(Observer*); | 57 virtual void addObserver(Observer*); |
54 virtual void removeObserver(Observer*); | 58 virtual void removeObserver(Observer*); |
55 | 59 |
56 virtual void trace(Visitor*) { } | 60 bool isIteratingOverObservers() const { return m_iterating != IteratingNone;
} |
57 | 61 |
58 protected: | 62 protected: |
59 explicit LifecycleNotifier(Context* context) | 63 explicit LifecycleNotifier(Context* context) |
60 : m_iterating(IteratingNone) | 64 : m_iterating(IteratingNone) |
61 , m_context(context) | 65 , m_context(context) |
62 , m_didCallContextDestroyed(false) | 66 , m_didCallContextDestroyed(false) |
63 { | 67 { |
64 } | 68 } |
65 | 69 |
66 Context* context() const { return m_context; } | 70 Context* context() const { return m_context; } |
(...skipping 17 matching lines...) Expand all Loading... |
84 bool m_didCallContextDestroyed; | 88 bool m_didCallContextDestroyed; |
85 }; | 89 }; |
86 | 90 |
87 template<typename T> | 91 template<typename T> |
88 inline LifecycleNotifier<T>::~LifecycleNotifier() | 92 inline LifecycleNotifier<T>::~LifecycleNotifier() |
89 { | 93 { |
90 // FIXME: Enable the following ASSERT. Also see a FIXME in Document::detach(
). | 94 // FIXME: Enable the following ASSERT. Also see a FIXME in Document::detach(
). |
91 // ASSERT(!m_observers.size() || m_didCallContextDestroyed); | 95 // ASSERT(!m_observers.size() || m_didCallContextDestroyed); |
92 | 96 |
93 #if !ENABLE(OILPAN) | 97 #if !ENABLE(OILPAN) |
94 TemporaryChange<IterationType> scope(m_iterating, IteratingOverAll); | 98 TemporaryChange<IterationType> scope(this->m_iterating, IteratingOverAll); |
95 for (Observer* observer : m_observers) { | 99 for (Observer* observer : m_observers) { |
96 ASSERT(observer->lifecycleContext() == m_context); | 100 ASSERT(observer->lifecycleContext() == m_context); |
97 observer->clearLifecycleContext(); | 101 observer->clearLifecycleContext(); |
98 } | 102 } |
99 #endif | 103 #endif |
100 } | 104 } |
101 | 105 |
102 template<typename T> | 106 template<typename T> |
103 inline void LifecycleNotifier<T>::notifyContextDestroyed() | 107 inline void LifecycleNotifier<T>::notifyContextDestroyed() |
104 { | 108 { |
105 // Don't notify contextDestroyed() twice. | 109 // Don't notify contextDestroyed() twice. |
106 if (m_didCallContextDestroyed) | 110 if (m_didCallContextDestroyed) |
107 return; | 111 return; |
108 | 112 |
109 TemporaryChange<IterationType> scope(m_iterating, IteratingOverAll); | 113 TemporaryChange<IterationType> scope(this->m_iterating, IteratingOverAll); |
110 Vector<Observer*> snapshotOfObservers; | 114 Vector<Observer*> snapshotOfObservers; |
111 copyToVector(m_observers, snapshotOfObservers); | 115 copyToVector(m_observers, snapshotOfObservers); |
112 for (Observer* observer : snapshotOfObservers) { | 116 for (Observer* observer : snapshotOfObservers) { |
113 // FIXME: Oilpan: At the moment, it's possible that the Observer is | 117 // FIXME: Oilpan: At the moment, it's possible that the Observer is |
114 // destructed during the iteration. Once we enable Oilpan by default | 118 // destructed during the iteration. Once we enable Oilpan by default |
115 // for Observers, we can remove the hack by making m_observers | 119 // for Observers, we can remove the hack by making m_observers |
116 // a HeapHashSet<WeakMember<Observers>>. (i.e., we can just iterate | 120 // a HeapHashSet<WeakMember<Observers>>. (i.e., we can just iterate |
117 // m_observers without taking a snapshot). | 121 // m_observers without taking a snapshot). |
118 if (m_observers.contains(observer)) { | 122 if (m_observers.contains(observer)) { |
119 ASSERT(observer->lifecycleContext() == m_context); | 123 ASSERT(observer->lifecycleContext() == m_context); |
(...skipping 12 matching lines...) Expand all Loading... |
132 | 136 |
133 template<typename T> | 137 template<typename T> |
134 inline void LifecycleNotifier<T>::removeObserver(typename LifecycleNotifier<T>::
Observer* observer) | 138 inline void LifecycleNotifier<T>::removeObserver(typename LifecycleNotifier<T>::
Observer* observer) |
135 { | 139 { |
136 m_observers.remove(observer); | 140 m_observers.remove(observer); |
137 } | 141 } |
138 | 142 |
139 } // namespace blink | 143 } // namespace blink |
140 | 144 |
141 #endif // LifecycleNotifier_h | 145 #endif // LifecycleNotifier_h |
OLD | NEW |