| 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 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 53 #if ENABLE(OILPAN) | 53 #if ENABLE(OILPAN) |
| 54 visitor->trace(m_observers); | 54 visitor->trace(m_observers); |
| 55 #endif | 55 #endif |
| 56 } | 56 } |
| 57 | 57 |
| 58 bool isIteratingOverObservers() const { return m_iterating != IteratingNone;
} | 58 bool isIteratingOverObservers() const { return m_iterating != IteratingNone;
} |
| 59 | 59 |
| 60 protected: | 60 protected: |
| 61 LifecycleNotifier() | 61 LifecycleNotifier() |
| 62 : m_iterating(IteratingNone) | 62 : m_iterating(IteratingNone) |
| 63 , m_didCallContextDestroyed(false) | |
| 64 { | 63 { |
| 65 } | 64 } |
| 66 | 65 |
| 67 enum IterationType { | 66 enum IterationType { |
| 68 IteratingNone, | 67 IteratingNone, |
| 69 IteratingOverAll, | 68 IteratingOverAll, |
| 70 }; | 69 }; |
| 71 | 70 |
| 72 IterationType m_iterating; | 71 IterationType m_iterating; |
| 73 | 72 |
| 74 protected: | 73 protected: |
| 75 using ObserverSet = WillBeHeapHashSet<RawPtrWillBeWeakMember<Observer>>; | 74 using ObserverSet = WillBeHeapHashSet<RawPtrWillBeWeakMember<Observer>>; |
| 76 | 75 |
| 77 ObserverSet m_observers; | 76 ObserverSet m_observers; |
| 78 | 77 |
| 79 #if ENABLE(ASSERT) | 78 #if ENABLE(ASSERT) |
| 80 T* context() { return static_cast<T*>(this); } | 79 T* context() { return static_cast<T*>(this); } |
| 81 #endif | 80 #endif |
| 82 | |
| 83 private: | |
| 84 bool m_didCallContextDestroyed; | |
| 85 }; | 81 }; |
| 86 | 82 |
| 87 template<typename T, typename Observer> | 83 template<typename T, typename Observer> |
| 88 inline LifecycleNotifier<T, Observer>::~LifecycleNotifier() | 84 inline LifecycleNotifier<T, Observer>::~LifecycleNotifier() |
| 89 { | 85 { |
| 90 // FIXME: Enable the following ASSERT. Also see a FIXME in Document::detach(
). | 86 // FIXME: Enable the following ASSERT. Also see a FIXME in Document::detach(
). |
| 91 // ASSERT(!m_observers.size() || m_didCallContextDestroyed); | 87 // ASSERT(!m_observers.size()); |
| 92 | 88 |
| 93 #if !ENABLE(OILPAN) | 89 #if !ENABLE(OILPAN) |
| 94 TemporaryChange<IterationType> scope(m_iterating, IteratingOverAll); | 90 TemporaryChange<IterationType> scope(m_iterating, IteratingOverAll); |
| 95 for (Observer* observer : m_observers) { | 91 for (Observer* observer : m_observers) { |
| 96 observer->clearLifecycleContext(); | 92 observer->clearLifecycleContext(); |
| 97 } | 93 } |
| 98 #endif | 94 #endif |
| 99 } | 95 } |
| 100 | 96 |
| 101 template<typename T, typename Observer> | 97 template<typename T, typename Observer> |
| 102 inline void LifecycleNotifier<T, Observer>::notifyContextDestroyed() | 98 inline void LifecycleNotifier<T, Observer>::notifyContextDestroyed() |
| 103 { | 99 { |
| 104 // Don't notify contextDestroyed() twice. | 100 // Don't notify contextDestroyed() twice. |
| 105 if (m_didCallContextDestroyed) | 101 if (m_observers.isEmpty()) |
| 106 return; | 102 return; |
| 107 | 103 |
| 108 TemporaryChange<IterationType> scope(m_iterating, IteratingOverAll); | 104 TemporaryChange<IterationType> scope(m_iterating, IteratingOverAll); |
| 109 Vector<RawPtrWillBeUntracedMember<Observer>> snapshotOfObservers; | 105 Vector<RawPtrWillBeUntracedMember<Observer>> snapshotOfObservers; |
| 110 copyToVector(m_observers, snapshotOfObservers); | 106 copyToVector(m_observers, snapshotOfObservers); |
| 111 for (Observer* observer : snapshotOfObservers) { | 107 for (Observer* observer : snapshotOfObservers) { |
| 112 // FIXME: Oilpan: At the moment, it's possible that the Observer is | 108 // FIXME: Oilpan: At the moment, it's possible that the Observer is |
| 113 // destructed during the iteration. | 109 // destructed during the iteration. |
| 114 // Once we enable Oilpan by default for Observers *and* | 110 // Once we enable Oilpan by default for Observers *and* |
| 115 // Observer::contextDestroyed() does not call removeObserver(), | 111 // Observer::contextDestroyed() does not call removeObserver(), |
| 116 // we can remove the hack by making m_observers | 112 // we can remove the hack by making m_observers |
| 117 // a HeapHashSet<WeakMember<Observers>>. (i.e., we can just iterate | 113 // a HeapHashSet<WeakMember<Observers>>. (i.e., we can just iterate |
| 118 // m_observers without taking a snapshot). | 114 // m_observers without taking a snapshot). |
| 119 if (m_observers.contains(observer)) { | 115 if (m_observers.contains(observer)) { |
| 120 ASSERT(observer->lifecycleContext() == context()); | 116 ASSERT(observer->lifecycleContext() == context()); |
| 121 observer->contextDestroyed(); | 117 observer->contextDestroyed(); |
| 118 observer->clearLifecycleContext(); |
| 122 } | 119 } |
| 123 } | 120 } |
| 124 | 121 m_observers.clear(); |
| 125 m_didCallContextDestroyed = true; | |
| 126 } | 122 } |
| 127 | 123 |
| 128 template<typename T, typename Observer> | 124 template<typename T, typename Observer> |
| 129 inline void LifecycleNotifier<T, Observer>::addObserver(Observer* observer) | 125 inline void LifecycleNotifier<T, Observer>::addObserver(Observer* observer) |
| 130 { | 126 { |
| 131 RELEASE_ASSERT(m_iterating != IteratingOverAll); | 127 RELEASE_ASSERT(m_iterating != IteratingOverAll); |
| 132 m_observers.add(observer); | 128 m_observers.add(observer); |
| 133 } | 129 } |
| 134 | 130 |
| 135 template<typename T, typename Observer> | 131 template<typename T, typename Observer> |
| 136 inline void LifecycleNotifier<T, Observer>::removeObserver(Observer* observer) | 132 inline void LifecycleNotifier<T, Observer>::removeObserver(Observer* observer) |
| 137 { | 133 { |
| 138 m_observers.remove(observer); | 134 m_observers.remove(observer); |
| 139 } | 135 } |
| 140 | 136 |
| 141 } // namespace blink | 137 } // namespace blink |
| 142 | 138 |
| 143 #endif // LifecycleNotifier_h | 139 #endif // LifecycleNotifier_h |
| OLD | NEW |