| 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 16 matching lines...) Expand all Loading... |
| 27 #ifndef LifecycleNotifier_h | 27 #ifndef LifecycleNotifier_h |
| 28 #define LifecycleNotifier_h | 28 #define LifecycleNotifier_h |
| 29 | 29 |
| 30 #include "platform/heap/Handle.h" | 30 #include "platform/heap/Handle.h" |
| 31 #include "wtf/HashSet.h" | 31 #include "wtf/HashSet.h" |
| 32 #include "wtf/TemporaryChange.h" | 32 #include "wtf/TemporaryChange.h" |
| 33 | 33 |
| 34 namespace blink { | 34 namespace blink { |
| 35 | 35 |
| 36 template<typename T, typename Observer> | 36 template<typename T, typename Observer> |
| 37 class LifecycleNotifier : public virtual WillBeGarbageCollectedMixin { | 37 class LifecycleNotifier : public virtual GarbageCollectedMixin { |
| 38 public: | 38 public: |
| 39 virtual ~LifecycleNotifier(); | 39 virtual ~LifecycleNotifier(); |
| 40 | 40 |
| 41 void addObserver(Observer*); | 41 void addObserver(Observer*); |
| 42 void removeObserver(Observer*); | 42 void removeObserver(Observer*); |
| 43 | 43 |
| 44 // notifyContextDestroyed() should be explicitly dispatched from an | 44 // notifyContextDestroyed() should be explicitly dispatched from an |
| 45 // observed context to notify observers that contextDestroyed(). | 45 // observed context to notify observers that contextDestroyed(). |
| 46 // | 46 // |
| 47 // When contextDestroyed() is called, the observer's lifecycleContext() | 47 // When contextDestroyed() is called, the observer's lifecycleContext() |
| (...skipping 17 matching lines...) Expand all Loading... |
| 65 } | 65 } |
| 66 | 66 |
| 67 enum IterationType { | 67 enum IterationType { |
| 68 IteratingNone, | 68 IteratingNone, |
| 69 IteratingOverAll, | 69 IteratingOverAll, |
| 70 }; | 70 }; |
| 71 | 71 |
| 72 IterationType m_iterating; | 72 IterationType m_iterating; |
| 73 | 73 |
| 74 protected: | 74 protected: |
| 75 using ObserverSet = WillBeHeapHashSet<RawPtrWillBeWeakMember<Observer>>; | 75 using ObserverSet = HeapHashSet<WeakMember<Observer>>; |
| 76 | 76 |
| 77 ObserverSet m_observers; | 77 ObserverSet m_observers; |
| 78 | 78 |
| 79 #if ENABLE(ASSERT) | 79 #if ENABLE(ASSERT) |
| 80 T* context() { return static_cast<T*>(this); } | 80 T* context() { return static_cast<T*>(this); } |
| 81 #endif | 81 #endif |
| 82 | 82 |
| 83 private: | 83 private: |
| 84 bool m_didCallContextDestroyed; | 84 bool m_didCallContextDestroyed; |
| 85 }; | 85 }; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 99 } | 99 } |
| 100 | 100 |
| 101 template<typename T, typename Observer> | 101 template<typename T, typename Observer> |
| 102 inline void LifecycleNotifier<T, Observer>::notifyContextDestroyed() | 102 inline void LifecycleNotifier<T, Observer>::notifyContextDestroyed() |
| 103 { | 103 { |
| 104 // Don't notify contextDestroyed() twice. | 104 // Don't notify contextDestroyed() twice. |
| 105 if (m_didCallContextDestroyed) | 105 if (m_didCallContextDestroyed) |
| 106 return; | 106 return; |
| 107 | 107 |
| 108 TemporaryChange<IterationType> scope(m_iterating, IteratingOverAll); | 108 TemporaryChange<IterationType> scope(m_iterating, IteratingOverAll); |
| 109 Vector<RawPtrWillBeUntracedMember<Observer>> snapshotOfObservers; | 109 Vector<UntracedMember<Observer>> snapshotOfObservers; |
| 110 copyToVector(m_observers, snapshotOfObservers); | 110 copyToVector(m_observers, snapshotOfObservers); |
| 111 for (Observer* observer : snapshotOfObservers) { | 111 for (Observer* observer : snapshotOfObservers) { |
| 112 // FIXME: Oilpan: At the moment, it's possible that the Observer is | 112 // FIXME: Oilpan: At the moment, it's possible that the Observer is |
| 113 // destructed during the iteration. | 113 // destructed during the iteration. |
| 114 // Once we enable Oilpan by default for Observers *and* | 114 // Once we enable Oilpan by default for Observers *and* |
| 115 // Observer::contextDestroyed() does not call removeObserver(), | 115 // Observer::contextDestroyed() does not call removeObserver(), |
| 116 // we can remove the hack by making m_observers | 116 // we can remove the hack by making m_observers |
| 117 // a HeapHashSet<WeakMember<Observers>>. (i.e., we can just iterate | 117 // a HeapHashSet<WeakMember<Observers>>. (i.e., we can just iterate |
| 118 // m_observers without taking a snapshot). | 118 // m_observers without taking a snapshot). |
| 119 if (m_observers.contains(observer)) { | 119 if (m_observers.contains(observer)) { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 134 | 134 |
| 135 template<typename T, typename Observer> | 135 template<typename T, typename Observer> |
| 136 inline void LifecycleNotifier<T, Observer>::removeObserver(Observer* observer) | 136 inline void LifecycleNotifier<T, Observer>::removeObserver(Observer* observer) |
| 137 { | 137 { |
| 138 m_observers.remove(observer); | 138 m_observers.remove(observer); |
| 139 } | 139 } |
| 140 | 140 |
| 141 } // namespace blink | 141 } // namespace blink |
| 142 | 142 |
| 143 #endif // LifecycleNotifier_h | 143 #endif // LifecycleNotifier_h |
| OLD | NEW |