| 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 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 60 : m_iterationState(NotIterating) | 60 : m_iterationState(NotIterating) |
| 61 { | 61 { |
| 62 } | 62 } |
| 63 | 63 |
| 64 #if DCHECK_IS_ON() | 64 #if DCHECK_IS_ON() |
| 65 T* context() { return static_cast<T*>(this); } | 65 T* context() { return static_cast<T*>(this); } |
| 66 #endif | 66 #endif |
| 67 | 67 |
| 68 using ObserverSet = HeapHashSet<WeakMember<Observer>>; | 68 using ObserverSet = HeapHashSet<WeakMember<Observer>>; |
| 69 | 69 |
| 70 void removePending(ObserverSet&); |
| 71 |
| 70 enum IterationState { | 72 enum IterationState { |
| 71 AllowingNone = 0, | 73 AllowingNone = 0, |
| 72 AllowingAddition, | 74 AllowingAddition = 1, |
| 73 AllowingRemoval, | 75 AllowingRemoval = 2, |
| 74 NotIterating = AllowingAddition | AllowingRemoval, | 76 NotIterating = AllowingAddition | AllowingRemoval, |
| 77 AllowPendingRemoval = 4, |
| 75 }; | 78 }; |
| 76 | 79 |
| 77 // Iteration state is recorded while iterating the observer set, | 80 // Iteration state is recorded while iterating the observer set, |
| 78 // optionally barring add or remove mutations. | 81 // optionally barring add or remove mutations. |
| 79 IterationState m_iterationState; | 82 IterationState m_iterationState; |
| 80 ObserverSet m_observers; | 83 ObserverSet m_observers; |
| 81 }; | 84 }; |
| 82 | 85 |
| 83 template<typename T, typename Observer> | 86 template<typename T, typename Observer> |
| 84 inline LifecycleNotifier<T, Observer>::~LifecycleNotifier() | 87 inline LifecycleNotifier<T, Observer>::~LifecycleNotifier() |
| (...skipping 18 matching lines...) Expand all Loading... |
| 103 template<typename T, typename Observer> | 106 template<typename T, typename Observer> |
| 104 inline void LifecycleNotifier<T, Observer>::addObserver(Observer* observer) | 107 inline void LifecycleNotifier<T, Observer>::addObserver(Observer* observer) |
| 105 { | 108 { |
| 106 RELEASE_ASSERT(m_iterationState & AllowingAddition); | 109 RELEASE_ASSERT(m_iterationState & AllowingAddition); |
| 107 m_observers.add(observer); | 110 m_observers.add(observer); |
| 108 } | 111 } |
| 109 | 112 |
| 110 template<typename T, typename Observer> | 113 template<typename T, typename Observer> |
| 111 inline void LifecycleNotifier<T, Observer>::removeObserver(Observer* observer) | 114 inline void LifecycleNotifier<T, Observer>::removeObserver(Observer* observer) |
| 112 { | 115 { |
| 116 // If immediate removal isn't currently allowed, |
| 117 // |observer| is recorded for pending removal. |
| 118 if (m_iterationState & AllowPendingRemoval) { |
| 119 m_observers.add(observer); |
| 120 return; |
| 121 } |
| 113 RELEASE_ASSERT(m_iterationState & AllowingRemoval); | 122 RELEASE_ASSERT(m_iterationState & AllowingRemoval); |
| 114 m_observers.remove(observer); | 123 m_observers.remove(observer); |
| 115 } | 124 } |
| 116 | 125 |
| 126 template<typename T, typename Observer> |
| 127 inline void LifecycleNotifier<T, Observer>::removePending(ObserverSet& observers
) |
| 128 { |
| 129 if (m_observers.size()) { |
| 130 // Prevent allocation (==shrinking) while removing; |
| 131 // the table is likely to become garbage soon. |
| 132 ThreadState::NoAllocationScope scope(ThreadState::current()); |
| 133 observers.removeAll(m_observers); |
| 134 } |
| 135 m_observers.swap(observers); |
| 136 } |
| 137 |
| 117 } // namespace blink | 138 } // namespace blink |
| 118 | 139 |
| 119 #endif // LifecycleNotifier_h | 140 #endif // LifecycleNotifier_h |
| OLD | NEW |