| 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 27 matching lines...) Expand all Loading... |
| 38 class LifecycleNotifier { | 38 class LifecycleNotifier { |
| 39 public: | 39 public: |
| 40 typedef LifecycleObserver<T> Observer; | 40 typedef LifecycleObserver<T> Observer; |
| 41 typedef T Context; | 41 typedef T Context; |
| 42 | 42 |
| 43 static PassOwnPtr<LifecycleNotifier> create(Context* context) | 43 static PassOwnPtr<LifecycleNotifier> create(Context* context) |
| 44 { | 44 { |
| 45 return adoptPtr(new LifecycleNotifier(context)); | 45 return adoptPtr(new LifecycleNotifier(context)); |
| 46 } | 46 } |
| 47 | 47 |
| 48 virtual ~LifecycleNotifier() |
| 49 { |
| 50 ASSERT(!m_observers.size()); |
| 51 ASSERT(!m_context); |
| 52 } |
| 48 | 53 |
| 49 virtual ~LifecycleNotifier(); | 54 void contextDestroyed(); |
| 50 | |
| 51 | 55 |
| 52 // FIXME: this won't need to be virtual anymore. | 56 // FIXME: this won't need to be virtual anymore. |
| 53 virtual void addObserver(Observer*); | 57 virtual void addObserver(Observer*); |
| 54 virtual void removeObserver(Observer*); | 58 virtual void removeObserver(Observer*); |
| 55 | 59 |
| 56 bool isIteratingOverObservers() const { return m_iterating != IteratingNone;
} | 60 bool isIteratingOverObservers() const { return m_iterating != IteratingNone;
} |
| 57 | 61 |
| 58 protected: | 62 protected: |
| 59 explicit LifecycleNotifier(Context* context) | 63 explicit LifecycleNotifier(Context* context) |
| 60 : m_iterating(IteratingNone) | 64 : m_iterating(IteratingNone) |
| (...skipping 16 matching lines...) Expand all Loading... |
| 77 IterationType m_iterating; | 81 IterationType m_iterating; |
| 78 | 82 |
| 79 private: | 83 private: |
| 80 typedef HashSet<Observer*> ObserverSet; | 84 typedef HashSet<Observer*> ObserverSet; |
| 81 | 85 |
| 82 ObserverSet m_observers; | 86 ObserverSet m_observers; |
| 83 Context* m_context; | 87 Context* m_context; |
| 84 }; | 88 }; |
| 85 | 89 |
| 86 template<typename T> | 90 template<typename T> |
| 87 inline LifecycleNotifier<T>::~LifecycleNotifier() | 91 inline void LifecycleNotifier<T>::contextDestroyed() |
| 88 { | 92 { |
| 89 TemporaryChange<IterationType> scope(this->m_iterating, IteratingOverAll); | 93 TemporaryChange<IterationType> scope(this->m_iterating, IteratingOverAll); |
| 90 for (typename ObserverSet::iterator it = m_observers.begin(); it != m_observ
ers.end(); it = m_observers.begin()) { | 94 for (typename ObserverSet::iterator it = m_observers.begin(); it != m_observ
ers.end(); it = m_observers.begin()) { |
| 91 Observer* observer = *it; | 95 Observer* observer = *it; |
| 92 m_observers.remove(observer); | 96 m_observers.remove(observer); |
| 93 ASSERT(observer->lifecycleContext() == m_context); | 97 ASSERT(observer->lifecycleContext() == m_context); |
| 94 observer->contextDestroyed(); | 98 observer->contextDestroyed(); |
| 95 } | 99 } |
| 100 m_context = nullptr; |
| 96 } | 101 } |
| 97 | 102 |
| 98 template<typename T> | 103 template<typename T> |
| 99 inline void LifecycleNotifier<T>::addObserver(typename LifecycleNotifier<T>::Obs
erver* observer) | 104 inline void LifecycleNotifier<T>::addObserver(typename LifecycleNotifier<T>::Obs
erver* observer) |
| 100 { | 105 { |
| 101 RELEASE_ASSERT(m_iterating != IteratingOverAll); | 106 RELEASE_ASSERT(m_iterating != IteratingOverAll); |
| 102 m_observers.add(observer); | 107 m_observers.add(observer); |
| 103 } | 108 } |
| 104 | 109 |
| 105 template<typename T> | 110 template<typename T> |
| 106 inline void LifecycleNotifier<T>::removeObserver(typename LifecycleNotifier<T>::
Observer* observer) | 111 inline void LifecycleNotifier<T>::removeObserver(typename LifecycleNotifier<T>::
Observer* observer) |
| 107 { | 112 { |
| 108 RELEASE_ASSERT(m_iterating != IteratingOverAll); | 113 RELEASE_ASSERT(m_iterating != IteratingOverAll); |
| 109 m_observers.remove(observer); | 114 m_observers.remove(observer); |
| 110 } | 115 } |
| 111 | 116 |
| 112 | 117 |
| 113 | 118 |
| 114 } // namespace blink | 119 } // namespace blink |
| 115 | 120 |
| 116 #endif // LifecycleNotifier_h | 121 #endif // LifecycleNotifier_h |
| OLD | NEW |