1/*
2 * Copyright (C) 2012 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#include "Timer.h"
29#include <wtf/Vector.h>
30
31namespace WebCore {
32
33template<typename T> class EventSender {
34 WTF_MAKE_NONCOPYABLE(EventSender); WTF_MAKE_FAST_ALLOCATED;
35public:
36 explicit EventSender(const AtomString& eventType);
37
38 const AtomString& eventType() const { return m_eventType; }
39 void dispatchEventSoon(T&);
40 void cancelEvent(T&);
41 void dispatchPendingEvents();
42
43#ifndef NDEBUG
44 bool hasPendingEvents(T& sender) const
45 {
46 return m_dispatchSoonList.find(&sender) != notFound || m_dispatchingList.find(&sender) != notFound;
47 }
48#endif
49
50private:
51 void timerFired() { dispatchPendingEvents(); }
52
53 AtomString m_eventType;
54 Timer m_timer;
55 Vector<T*> m_dispatchSoonList;
56 Vector<T*> m_dispatchingList;
57};
58
59template<typename T> EventSender<T>::EventSender(const AtomString& eventType)
60 : m_eventType(eventType)
61 , m_timer(*this, &EventSender::timerFired)
62{
63}
64
65template<typename T> void EventSender<T>::dispatchEventSoon(T& sender)
66{
67 m_dispatchSoonList.append(&sender);
68 if (!m_timer.isActive())
69 m_timer.startOneShot(0_s);
70}
71
72template<typename T> void EventSender<T>::cancelEvent(T& sender)
73{
74 // Remove instances of this sender from both lists.
75 // Use loops because we allow multiple instances to get into the lists.
76 for (auto& event : m_dispatchSoonList) {
77 if (event == &sender)
78 event = nullptr;
79 }
80 for (auto& event : m_dispatchingList) {
81 if (event == &sender)
82 event = nullptr;
83 }
84}
85
86template<typename T> void EventSender<T>::dispatchPendingEvents()
87{
88 // Need to avoid re-entering this function; if new dispatches are
89 // scheduled before the parent finishes processing the list, they
90 // will set a timer and eventually be processed.
91 if (!m_dispatchingList.isEmpty())
92 return;
93
94 m_timer.stop();
95
96 m_dispatchSoonList.checkConsistency();
97
98 m_dispatchingList.swap(m_dispatchSoonList);
99 for (auto& event : m_dispatchingList) {
100 if (T* sender = event) {
101 event = nullptr;
102 sender->dispatchPendingEvent(this);
103 }
104 }
105 m_dispatchingList.clear();
106}
107
108} // namespace WebCore
109