1 /*
2 * Copyright (C) 2010 Google 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 COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 *
25 */
26
27 #include "config.h"
28 #include "EventQueue.h"
29
30 #include "DOMWindow.h"
31 #include "Document.h"
32 #include "Event.h"
33 #include "EventNames.h"
34 #include "RuntimeApplicationChecks.h"
35 #include "ScriptExecutionContext.h"
36 #include "SuspendableTimer.h"
37
38 namespace WebCore {
39
shouldDispatchScrollEventSynchronously(Document * document)40 static inline bool shouldDispatchScrollEventSynchronously(Document* document)
41 {
42 ASSERT_ARG(document, document);
43 return applicationIsSafari() && (document->url().protocolIs("feed") || document->url().protocolIs("feeds"));
44 }
45
46 class EventQueueTimer : public SuspendableTimer {
47 WTF_MAKE_NONCOPYABLE(EventQueueTimer);
48 public:
EventQueueTimer(EventQueue * eventQueue,ScriptExecutionContext * context)49 EventQueueTimer(EventQueue* eventQueue, ScriptExecutionContext* context)
50 : SuspendableTimer(context)
51 , m_eventQueue(eventQueue) { }
52
53 private:
fired()54 virtual void fired() { m_eventQueue->pendingEventTimerFired(); }
55 EventQueue* m_eventQueue;
56 };
57
create(ScriptExecutionContext * context)58 PassRefPtr<EventQueue> EventQueue::create(ScriptExecutionContext* context)
59 {
60 return adoptRef(new EventQueue(context));
61 }
62
EventQueue(ScriptExecutionContext * context)63 EventQueue::EventQueue(ScriptExecutionContext* context)
64 : m_pendingEventTimer(adoptPtr(new EventQueueTimer(this, context)))
65 {
66 }
67
~EventQueue()68 EventQueue::~EventQueue()
69 {
70 }
71
enqueueEvent(PassRefPtr<Event> event)72 void EventQueue::enqueueEvent(PassRefPtr<Event> event)
73 {
74 ASSERT(event->target());
75 bool wasAdded = m_queuedEvents.add(event).second;
76 ASSERT_UNUSED(wasAdded, wasAdded); // It should not have already been in the list.
77
78 if (!m_pendingEventTimer->isActive())
79 m_pendingEventTimer->startOneShot(0);
80 }
81
enqueueOrDispatchScrollEvent(PassRefPtr<Node> target,ScrollEventTargetType targetType)82 void EventQueue::enqueueOrDispatchScrollEvent(PassRefPtr<Node> target, ScrollEventTargetType targetType)
83 {
84 // Per the W3C CSSOM View Module, scroll events fired at the document should bubble, others should not.
85 bool canBubble = targetType == ScrollEventDocumentTarget;
86 RefPtr<Event> scrollEvent = Event::create(eventNames().scrollEvent, canBubble, false /* non cancelleable */);
87
88 if (shouldDispatchScrollEventSynchronously(target->document())) {
89 target->dispatchEvent(scrollEvent.release());
90 return;
91 }
92
93 if (!m_nodesWithQueuedScrollEvents.add(target.get()).second)
94 return;
95
96 scrollEvent->setTarget(target);
97 enqueueEvent(scrollEvent.release());
98 }
99
cancelEvent(Event * event)100 bool EventQueue::cancelEvent(Event* event)
101 {
102 bool found = m_queuedEvents.contains(event);
103 m_queuedEvents.remove(event);
104 if (m_queuedEvents.isEmpty())
105 m_pendingEventTimer->stop();
106 return found;
107 }
108
cancelQueuedEvents()109 void EventQueue::cancelQueuedEvents()
110 {
111 m_pendingEventTimer->stop();
112 m_queuedEvents.clear();
113 }
114
pendingEventTimerFired()115 void EventQueue::pendingEventTimerFired()
116 {
117 ASSERT(!m_pendingEventTimer->isActive());
118 ASSERT(!m_queuedEvents.isEmpty());
119
120 m_nodesWithQueuedScrollEvents.clear();
121
122 // Insert a marker for where we should stop.
123 ASSERT(!m_queuedEvents.contains(0));
124 bool wasAdded = m_queuedEvents.add(0).second;
125 ASSERT_UNUSED(wasAdded, wasAdded); // It should not have already been in the list.
126
127 RefPtr<EventQueue> protector(this);
128
129 while (!m_queuedEvents.isEmpty()) {
130 ListHashSet<RefPtr<Event> >::iterator iter = m_queuedEvents.begin();
131 RefPtr<Event> event = *iter;
132 m_queuedEvents.remove(iter);
133 if (!event)
134 break;
135 dispatchEvent(event.get());
136 }
137 }
138
dispatchEvent(PassRefPtr<Event> event)139 void EventQueue::dispatchEvent(PassRefPtr<Event> event)
140 {
141 EventTarget* eventTarget = event->target();
142 if (eventTarget->toDOMWindow())
143 eventTarget->toDOMWindow()->dispatchEvent(event, 0);
144 else
145 eventTarget->dispatchEvent(event);
146 }
147
148 }
149