#ifndef LifecycleNotifier_h
#define LifecycleNotifier_h
#include "platform/LifecycleObserver.h"
#include "wtf/HashSet.h"
#include "wtf/PassOwnPtr.h"
#include "wtf/TemporaryChange.h"
namespace WebCore {
template<typename T> class LifecycleContext;
template<typename T>
class LifecycleNotifier {
public:
typedef LifecycleObserver<T> Observer;
typedef T Context;
static PassOwnPtr<LifecycleNotifier> create(Context* context)
{
return adoptPtr(new LifecycleNotifier(context));
}
virtual ~LifecycleNotifier();
virtual void addObserver(Observer*);
virtual void removeObserver(Observer*);
bool isIteratingOverObservers() const { return m_iterating != IteratingNone; }
protected:
explicit LifecycleNotifier(Context* context)
: m_iterating(IteratingNone)
, m_context(context)
{
}
Context* context() const { return m_context; }
enum IterationType {
IteratingNone,
IteratingOverAll,
IteratingOverActiveDOMObjects,
IteratingOverContextObservers,
IteratingOverDocumentObservers,
IteratingOverPageObservers,
IteratingOverDOMWindowObservers
};
IterationType m_iterating;
private:
typedef HashSet<Observer*> ObserverSet;
ObserverSet m_observers;
Context* m_context;
};
template<typename T>
inline LifecycleNotifier<T>::~LifecycleNotifier()
{
TemporaryChange<IterationType> scope(this->m_iterating, IteratingOverAll);
for (typename ObserverSet::iterator it = m_observers.begin(); it != m_observers.end(); it = m_observers.begin()) {
Observer* observer = *it;
m_observers.remove(observer);
ASSERT(observer->lifecycleContext() == m_context);
observer->contextDestroyed();
}
}
template<typename T>
inline void LifecycleNotifier<T>::addObserver(typename LifecycleNotifier<T>::Observer* observer)
{
RELEASE_ASSERT(m_iterating != IteratingOverAll);
m_observers.add(observer);
}
template<typename T>
inline void LifecycleNotifier<T>::removeObserver(typename LifecycleNotifier<T>::Observer* observer)
{
RELEASE_ASSERT(m_iterating != IteratingOverAll);
m_observers.remove(observer);
}
}
#endif