IdTargetObserverRegistry.cpp [plain text]
#include "config.h"
#include "IdTargetObserverRegistry.h"
#include "IdTargetObserver.h"
namespace WebCore {
void IdTargetObserverRegistry::addObserver(const AtomicString& id, IdTargetObserver* observer)
{
if (id.isEmpty())
return;
IdToObserverSetMap::AddResult result = m_registry.add(id.impl(), nullptr);
if (result.isNewEntry)
result.iterator->value = std::make_unique<ObserverSet>();
result.iterator->value->add(observer);
}
void IdTargetObserverRegistry::removeObserver(const AtomicString& id, IdTargetObserver* observer)
{
if (id.isEmpty() || m_registry.isEmpty())
return;
IdToObserverSetMap::iterator iter = m_registry.find(id.impl());
ObserverSet* set = iter->value.get();
set->remove(observer);
if (set->isEmpty() && set != m_notifyingObserversInSet)
m_registry.remove(iter);
}
void IdTargetObserverRegistry::notifyObserversInternal(const AtomicStringImpl& id)
{
ASSERT(!m_registry.isEmpty());
m_notifyingObserversInSet = m_registry.get(&id);
if (!m_notifyingObserversInSet)
return;
Vector<IdTargetObserver*> copy;
copyToVector(*m_notifyingObserversInSet, copy);
for (Vector<IdTargetObserver*>::const_iterator it = copy.begin(); it != copy.end(); ++it) {
if (m_notifyingObserversInSet->contains(*it))
(*it)->idTargetChanged();
}
if (m_notifyingObserversInSet->isEmpty())
m_registry.remove(&id);
m_notifyingObserversInSet = 0;
}
}