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 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 return m_iterationState != NotIterating; | 54 return m_iterationState != NotIterating; |
55 } | 55 } |
56 | 56 |
57 protected: | 57 protected: |
58 LifecycleNotifier() : m_iterationState(NotIterating) {} | 58 LifecycleNotifier() : m_iterationState(NotIterating) {} |
59 | 59 |
60 T* context() { return static_cast<T*>(this); } | 60 T* context() { return static_cast<T*>(this); } |
61 | 61 |
62 using ObserverSet = HeapHashSet<WeakMember<Observer>>; | 62 using ObserverSet = HeapHashSet<WeakMember<Observer>>; |
63 | 63 |
64 void removePending(ObserverSet&); | |
65 | |
66 enum IterationState { | 64 enum IterationState { |
67 AllowingNone = 0, | 65 AllowingNone = 0, |
68 AllowingAddition = 1, | 66 AllowingAddition = 1, |
69 AllowingRemoval = 2, | 67 AllowingRemoval = 2, |
70 NotIterating = AllowingAddition | AllowingRemoval, | 68 NotIterating = AllowingAddition | AllowingRemoval, |
71 AllowPendingRemoval = 4, | 69 AllowPendingRemoval = 4, |
72 }; | 70 }; |
73 | 71 |
74 // Iteration state is recorded while iterating the observer set, | 72 // Iteration state is recorded while iterating the observer set, |
75 // optionally barring add or remove mutations. | 73 // optionally barring add or remove mutations. |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
108 // If immediate removal isn't currently allowed, | 106 // If immediate removal isn't currently allowed, |
109 // |observer| is recorded for pending removal. | 107 // |observer| is recorded for pending removal. |
110 if (m_iterationState & AllowPendingRemoval) { | 108 if (m_iterationState & AllowPendingRemoval) { |
111 m_observers.add(observer); | 109 m_observers.add(observer); |
112 return; | 110 return; |
113 } | 111 } |
114 RELEASE_ASSERT(m_iterationState & AllowingRemoval); | 112 RELEASE_ASSERT(m_iterationState & AllowingRemoval); |
115 m_observers.remove(observer); | 113 m_observers.remove(observer); |
116 } | 114 } |
117 | 115 |
118 template <typename T, typename Observer> | |
119 inline void LifecycleNotifier<T, Observer>::removePending( | |
120 ObserverSet& observers) { | |
121 if (m_observers.size()) { | |
122 // Prevent allocation (==shrinking) while removing; | |
123 // the table is likely to become garbage soon. | |
124 ThreadState::NoAllocationScope scope(ThreadState::current()); | |
125 observers.removeAll(m_observers); | |
126 } | |
127 m_observers.swap(observers); | |
128 } | |
129 | |
130 } // namespace blink | 116 } // namespace blink |
131 | 117 |
132 #endif // LifecycleNotifier_h | 118 #endif // LifecycleNotifier_h |
OLD | NEW |