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)#ifndef IdTargetObserverRegistry_h
275c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#define IdTargetObserverRegistry_h
285c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
29f523d2789ac2f83c4eca0ee4d5161bfdb5f2d052Torne (Richard Coles)#include "platform/heap/Handle.h"
30e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdoch#include "wtf/Forward.h"
31e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdoch#include "wtf/HashMap.h"
32e69819bd8e388ea4ad1636a19aa6b2eed4952191Ben Murdoch#include "wtf/HashSet.h"
3383750176c3ee2cea66c8a9751271026a5901be3aBen Murdoch#include "wtf/text/StringHash.h"
345c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
35c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)namespace blink {
365c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
375c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)class IdTargetObserver;
385c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
39f523d2789ac2f83c4eca0ee4d5161bfdb5f2d052Torne (Richard Coles)class IdTargetObserverRegistry FINAL : public NoBaseWillBeGarbageCollectedFinalized<IdTargetObserverRegistry> {
40f523d2789ac2f83c4eca0ee4d5161bfdb5f2d052Torne (Richard Coles)    WTF_MAKE_NONCOPYABLE(IdTargetObserverRegistry);
41f523d2789ac2f83c4eca0ee4d5161bfdb5f2d052Torne (Richard Coles)    WTF_MAKE_FAST_ALLOCATED_WILL_BE_REMOVED;
425c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    friend class IdTargetObserver;
435c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)public:
44f523d2789ac2f83c4eca0ee4d5161bfdb5f2d052Torne (Richard Coles)    static PassOwnPtrWillBeRawPtr<IdTargetObserverRegistry> create();
45f523d2789ac2f83c4eca0ee4d5161bfdb5f2d052Torne (Richard Coles)    void trace(Visitor*);
465c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    void notifyObservers(const AtomicString& id);
4709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    bool hasObservers(const AtomicString& id) const;
485c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
495c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)private:
50f523d2789ac2f83c4eca0ee4d5161bfdb5f2d052Torne (Richard Coles)    IdTargetObserverRegistry() : m_notifyingObserversInSet(nullptr) { }
515c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    void addObserver(const AtomicString& id, IdTargetObserver*);
525c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    void removeObserver(const AtomicString& id, IdTargetObserver*);
535c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    void notifyObserversInternal(const AtomicString& id);
545c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
55f523d2789ac2f83c4eca0ee4d5161bfdb5f2d052Torne (Richard Coles)    typedef WillBeHeapHashSet<RawPtrWillBeMember<IdTargetObserver> > ObserverSet;
56f523d2789ac2f83c4eca0ee4d5161bfdb5f2d052Torne (Richard Coles)    typedef WillBeHeapHashMap<StringImpl*, OwnPtrWillBeMember<ObserverSet> > IdToObserverSetMap;
575c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    IdToObserverSetMap m_registry;
58f523d2789ac2f83c4eca0ee4d5161bfdb5f2d052Torne (Richard Coles)    RawPtrWillBeMember<ObserverSet> m_notifyingObserversInSet;
595c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)};
605c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
615c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)inline void IdTargetObserverRegistry::notifyObservers(const AtomicString& id)
625c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles){
635c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    ASSERT(!m_notifyingObserversInSet);
645c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    if (id.isEmpty() || m_registry.isEmpty())
655c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)        return;
665c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)    IdTargetObserverRegistry::notifyObserversInternal(id);
675c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)}
685c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
69c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)} // namespace blink
705c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
715c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#endif // IdTargetObserverRegistry_h
72