15c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)/*
25c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * Copyright (C) 2012 Google Inc. All Rights Reserved.
35c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *
45c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * Redistribution and use in source and binary forms, with or without
55c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * modification, are permitted provided that the following conditions
65c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * are met:
75c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * 1. Redistributions of source code must retain the above copyright
85c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *    notice, this list of conditions and the following disclaimer.
95c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * 2. Redistributions in binary form must reproduce the above copyright
105c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *    notice, this list of conditions and the following disclaimer in the
115c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *    documentation and/or other materials provided with the distribution.
125c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *
135c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
145c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
155c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
165c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
175c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
185c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
195c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
205c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
215c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
225c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
235c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
245c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) */
255c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
265c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "config.h"
2753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)#include "core/dom/IdTargetObserverRegistry.h"
285c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
2953e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)#include "core/dom/IdTargetObserver.h"
305c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
31c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)namespace blink {
325c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
33f523d2789ac2f83c4eca0ee4d5161bfdb5f2d052Torne (Richard Coles)PassOwnPtrWillBeRawPtr<IdTargetObserverRegistry> IdTargetObserverRegistry::create()
345c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles){
35f523d2789ac2f83c4eca0ee4d5161bfdb5f2d052Torne (Richard Coles)    return adoptPtrWillBeNoop(new IdTargetObserverRegistry());
36f523d2789ac2f83c4eca0ee4d5161bfdb5f2d052Torne (Richard Coles)}
37f523d2789ac2f83c4eca0ee4d5161bfdb5f2d052Torne (Richard Coles)
38f523d2789ac2f83c4eca0ee4d5161bfdb5f2d052Torne (Richard Coles)void IdTargetObserverRegistry::trace(Visitor* visitor)
39f523d2789ac2f83c4eca0ee4d5161bfdb5f2d052Torne (Richard Coles){
40c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)#if ENABLE(OILPAN)
41f523d2789ac2f83c4eca0ee4d5161bfdb5f2d052Torne (Richard Coles)    visitor->trace(m_registry);
42f523d2789ac2f83c4eca0ee4d5161bfdb5f2d052Torne (Richard Coles)    visitor->trace(m_notifyingObserversInSet);
43c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)#endif
445c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)}
455c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
465c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)void IdTargetObserverRegistry::addObserver(const AtomicString& id, IdTargetObserver* observer)
475c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles){
485c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    if (id.isEmpty())
495c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        return;
5002772c6a72f1ee0b226341a4f4439970c29fc861Ben Murdoch
515c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    IdToObserverSetMap::AddResult result = m_registry.add(id.impl(), nullptr);
525c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    if (result.isNewEntry)
53f523d2789ac2f83c4eca0ee4d5161bfdb5f2d052Torne (Richard Coles)        result.storedValue->value = adoptPtrWillBeNoop(new ObserverSet());
545c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
5509380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    result.storedValue->value->add(observer);
565c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)}
575c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
585c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)void IdTargetObserverRegistry::removeObserver(const AtomicString& id, IdTargetObserver* observer)
595c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles){
605c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    if (id.isEmpty() || m_registry.isEmpty())
615c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        return;
625c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
635c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    IdToObserverSetMap::iterator iter = m_registry.find(id.impl());
645c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
655c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    ObserverSet* set = iter->value.get();
665c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    set->remove(observer);
675c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    if (set->isEmpty() && set != m_notifyingObserversInSet)
685c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        m_registry.remove(iter);
695c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)}
705c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
715c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)void IdTargetObserverRegistry::notifyObserversInternal(const AtomicString& id)
725c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles){
735c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    ASSERT(!id.isEmpty());
745c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    ASSERT(!m_registry.isEmpty());
755c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
765c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    m_notifyingObserversInSet = m_registry.get(id.impl());
775c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    if (!m_notifyingObserversInSet)
785c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        return;
795c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
80f523d2789ac2f83c4eca0ee4d5161bfdb5f2d052Torne (Richard Coles)    WillBeHeapVector<RawPtrWillBeMember<IdTargetObserver> > copy;
815c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    copyToVector(*m_notifyingObserversInSet, copy);
82f523d2789ac2f83c4eca0ee4d5161bfdb5f2d052Torne (Richard Coles)    for (WillBeHeapVector<RawPtrWillBeMember<IdTargetObserver> >::const_iterator it = copy.begin(); it != copy.end(); ++it) {
835c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        if (m_notifyingObserversInSet->contains(*it))
845c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)            (*it)->idTargetChanged();
855c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    }
865c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
875c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    if (m_notifyingObserversInSet->isEmpty())
885c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        m_registry.remove(id.impl());
895c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
90f523d2789ac2f83c4eca0ee4d5161bfdb5f2d052Torne (Richard Coles)    m_notifyingObserversInSet = nullptr;
915c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)}
925c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
9309380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)bool IdTargetObserverRegistry::hasObservers(const AtomicString& id) const
9409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles){
9509380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    if (id.isEmpty() || m_registry.isEmpty())
9609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)        return false;
9709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    ObserverSet* set = m_registry.get(id.impl());
9809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    return set && !set->isEmpty();
9909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)}
10009380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
101c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)} // namespace blink
102