1/*
2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 *           (C) 1999 Antti Koivisto (koivisto@kde.org)
4 *           (C) 2001 Dirk Mueller (mueller@kde.org)
5 * Copyright (C) 2004, 2005, 2006, 2007, 2012 Apple Inc. All rights reserved.
6 * Copyright (C) 2006 Alexey Proskuryakov (ap@webkit.org)
7 *           (C) 2007, 2008 Nikolas Zimmermann <zimmermann@kde.org>
8 * Copyright (C) 2011 Andreas Kling (kling@webkit.org)
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
20 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
27 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
31 */
32
33#ifndef EventListenerMap_h
34#define EventListenerMap_h
35
36#include "RegisteredEventListener.h"
37#include <wtf/Forward.h>
38#include <wtf/PassOwnPtr.h>
39#include <wtf/text/AtomicStringHash.h>
40
41namespace WebCore {
42
43class EventTarget;
44
45typedef Vector<RegisteredEventListener, 1> EventListenerVector;
46
47class EventListenerMap {
48public:
49    EventListenerMap();
50
51    bool isEmpty() const { return m_entries.isEmpty(); }
52    bool contains(const AtomicString& eventType) const;
53    bool containsCapturing(const AtomicString& eventType) const;
54
55    void clear();
56    bool add(const AtomicString& eventType, PassRefPtr<EventListener>, bool useCapture);
57    bool remove(const AtomicString& eventType, EventListener*, bool useCapture, size_t& indexOfRemovedListener);
58    EventListenerVector* find(const AtomicString& eventType);
59    Vector<AtomicString> eventTypes() const;
60
61#if ENABLE(SVG)
62    void removeFirstEventListenerCreatedFromMarkup(const AtomicString& eventType);
63    void copyEventListenersNotCreatedFromMarkupToTarget(EventTarget*);
64#endif
65
66private:
67    friend class EventListenerIterator;
68
69    void assertNoActiveIterators();
70
71    Vector<std::pair<AtomicString, OwnPtr<EventListenerVector> >, 2> m_entries;
72
73#ifndef NDEBUG
74    int m_activeIteratorCount;
75#endif
76};
77
78class EventListenerIterator {
79    WTF_MAKE_NONCOPYABLE(EventListenerIterator);
80public:
81    EventListenerIterator();
82    EventListenerIterator(EventTarget*);
83#ifndef NDEBUG
84    ~EventListenerIterator();
85#endif
86
87    EventListener* nextListener();
88
89private:
90    EventListenerMap* m_map;
91    unsigned m_entryIndex;
92    unsigned m_index;
93};
94
95#ifdef NDEBUG
96inline void EventListenerMap::assertNoActiveIterators() { }
97#endif
98
99} // namespace WebCore
100
101#endif // EventListenerMap_h
102