| 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 } | 54 } |
| 55 | 55 |
| 56 bool isIteratingOverObservers() const { return m_iterationState != NotIterat
ing; } | 56 bool isIteratingOverObservers() const { return m_iterationState != NotIterat
ing; } |
| 57 | 57 |
| 58 protected: | 58 protected: |
| 59 LifecycleNotifier() | 59 LifecycleNotifier() |
| 60 : m_iterationState(NotIterating) | 60 : m_iterationState(NotIterating) |
| 61 { | 61 { |
| 62 } | 62 } |
| 63 | 63 |
| 64 #if DCHECK_IS_ON() | |
| 65 T* context() { return static_cast<T*>(this); } | 64 T* context() { return static_cast<T*>(this); } |
| 66 #endif | |
| 67 | 65 |
| 68 using ObserverSet = HeapHashSet<WeakMember<Observer>>; | 66 using ObserverSet = HeapHashSet<WeakMember<Observer>>; |
| 69 | 67 |
| 70 void removePending(ObserverSet&); | 68 void removePending(ObserverSet&); |
| 71 | 69 |
| 72 enum IterationState { | 70 enum IterationState { |
| 73 AllowingNone = 0, | 71 AllowingNone = 0, |
| 74 AllowingAddition = 1, | 72 AllowingAddition = 1, |
| 75 AllowingRemoval = 2, | 73 AllowingRemoval = 2, |
| 76 NotIterating = AllowingAddition | AllowingRemoval, | 74 NotIterating = AllowingAddition | AllowingRemoval, |
| (...skipping 14 matching lines...) Expand all Loading... |
| 91 } | 89 } |
| 92 | 90 |
| 93 template<typename T, typename Observer> | 91 template<typename T, typename Observer> |
| 94 inline void LifecycleNotifier<T, Observer>::notifyContextDestroyed() | 92 inline void LifecycleNotifier<T, Observer>::notifyContextDestroyed() |
| 95 { | 93 { |
| 96 // Observer unregistration is allowed, but effectively a no-op. | 94 // Observer unregistration is allowed, but effectively a no-op. |
| 97 AutoReset<IterationState> scope(&m_iterationState, AllowingRemoval); | 95 AutoReset<IterationState> scope(&m_iterationState, AllowingRemoval); |
| 98 ObserverSet observers; | 96 ObserverSet observers; |
| 99 m_observers.swap(observers); | 97 m_observers.swap(observers); |
| 100 for (Observer* observer : observers) { | 98 for (Observer* observer : observers) { |
| 101 ASSERT(observer->lifecycleContext() == context()); | 99 DCHECK(observer->lifecycleContext() == context()); |
| 102 observer->contextDestroyed(); | 100 observer->contextDestroyed(); |
| 101 observer->clearContext(); |
| 103 } | 102 } |
| 104 } | 103 } |
| 105 | 104 |
| 106 template<typename T, typename Observer> | 105 template<typename T, typename Observer> |
| 107 inline void LifecycleNotifier<T, Observer>::addObserver(Observer* observer) | 106 inline void LifecycleNotifier<T, Observer>::addObserver(Observer* observer) |
| 108 { | 107 { |
| 109 RELEASE_ASSERT(m_iterationState & AllowingAddition); | 108 RELEASE_ASSERT(m_iterationState & AllowingAddition); |
| 110 m_observers.add(observer); | 109 m_observers.add(observer); |
| 111 } | 110 } |
| 112 | 111 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 131 // the table is likely to become garbage soon. | 130 // the table is likely to become garbage soon. |
| 132 ThreadState::NoAllocationScope scope(ThreadState::current()); | 131 ThreadState::NoAllocationScope scope(ThreadState::current()); |
| 133 observers.removeAll(m_observers); | 132 observers.removeAll(m_observers); |
| 134 } | 133 } |
| 135 m_observers.swap(observers); | 134 m_observers.swap(observers); |
| 136 } | 135 } |
| 137 | 136 |
| 138 } // namespace blink | 137 } // namespace blink |
| 139 | 138 |
| 140 #endif // LifecycleNotifier_h | 139 #endif // LifecycleNotifier_h |
| OLD | NEW |