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 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 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 | #pragma once |
34 | |
35 | #include "RegisteredEventListener.h" |
36 | #include <atomic> |
37 | #include <memory> |
38 | #include <wtf/Forward.h> |
39 | #include <wtf/Lock.h> |
40 | #include <wtf/text/AtomicString.h> |
41 | |
42 | namespace WebCore { |
43 | |
44 | class EventTarget; |
45 | |
46 | using EventListenerVector = Vector<RefPtr<RegisteredEventListener>, 1>; |
47 | |
48 | class EventListenerMap { |
49 | public: |
50 | EventListenerMap(); |
51 | |
52 | bool isEmpty() const { return m_entries.isEmpty(); } |
53 | bool contains(const AtomicString& eventType) const { return find(eventType); } |
54 | bool containsCapturing(const AtomicString& eventType) const; |
55 | bool containsActive(const AtomicString& eventType) const; |
56 | |
57 | void clear(); |
58 | |
59 | void replace(const AtomicString& eventType, EventListener& oldListener, Ref<EventListener>&& newListener, const RegisteredEventListener::Options&); |
60 | bool add(const AtomicString& eventType, Ref<EventListener>&&, const RegisteredEventListener::Options&); |
61 | bool remove(const AtomicString& eventType, EventListener&, bool useCapture); |
62 | WEBCORE_EXPORT EventListenerVector* find(const AtomicString& eventType) const; |
63 | Vector<AtomicString> eventTypes() const; |
64 | |
65 | void removeFirstEventListenerCreatedFromMarkup(const AtomicString& eventType); |
66 | void copyEventListenersNotCreatedFromMarkupToTarget(EventTarget*); |
67 | |
68 | Lock& lock() { return m_lock; } |
69 | |
70 | private: |
71 | friend class EventListenerIterator; |
72 | |
73 | void assertNoActiveIterators() const; |
74 | |
75 | Vector<std::pair<AtomicString, std::unique_ptr<EventListenerVector>>, 2> m_entries; |
76 | |
77 | #ifndef NDEBUG |
78 | std::atomic<int> m_activeIteratorCount { 0 }; |
79 | #endif |
80 | |
81 | Lock m_lock; |
82 | }; |
83 | |
84 | class EventListenerIterator { |
85 | WTF_MAKE_NONCOPYABLE(EventListenerIterator); |
86 | public: |
87 | explicit EventListenerIterator(EventTarget*); |
88 | explicit EventListenerIterator(EventListenerMap*); |
89 | #ifndef NDEBUG |
90 | ~EventListenerIterator(); |
91 | #endif |
92 | |
93 | EventListener* nextListener(); |
94 | |
95 | private: |
96 | EventListenerMap* m_map { nullptr }; |
97 | unsigned m_entryIndex { 0 }; |
98 | unsigned m_index { 0 }; |
99 | }; |
100 | |
101 | #ifdef NDEBUG |
102 | inline void EventListenerMap::assertNoActiveIterators() const { } |
103 | #endif |
104 | |
105 | } // namespace WebCore |
106 | |