blob: 8072c595c6a4cd4a5d85d06d5bb049d10f456b78 [file] [log] [blame]
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +01001/*
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 are
6 * met:
7 *
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#include "config.h"
32#include "core/dom/ScopedEventQueue.h"
33
34#include "core/dom/Event.h"
35#include "core/dom/EventDispatchMediator.h"
36#include "core/dom/EventDispatcher.h"
37#include "core/dom/EventTarget.h"
Ben Murdoche69819b2013-07-17 14:56:49 +010038#include "wtf/OwnPtr.h"
39#include "wtf/RefPtr.h"
Torne (Richard Coles)53e740f2013-05-09 18:38:43 +010040
41namespace WebCore {
42
43ScopedEventQueue* ScopedEventQueue::s_instance = 0;
44
45ScopedEventQueue::ScopedEventQueue()
46 : m_scopingLevel(0)
47{
48}
49
50ScopedEventQueue::~ScopedEventQueue()
51{
52 ASSERT(!m_scopingLevel);
53 ASSERT(!m_queuedEventDispatchMediators.size());
54}
55
56void ScopedEventQueue::initialize()
57{
58 ASSERT(!s_instance);
59 OwnPtr<ScopedEventQueue> instance = adoptPtr(new ScopedEventQueue);
60 s_instance = instance.leakPtr();
61}
62
63void ScopedEventQueue::enqueueEventDispatchMediator(PassRefPtr<EventDispatchMediator> mediator)
64{
65 if (m_scopingLevel)
66 m_queuedEventDispatchMediators.append(mediator);
67 else
68 dispatchEvent(mediator);
69}
70
71void ScopedEventQueue::dispatchAllEvents()
72{
73 Vector<RefPtr<EventDispatchMediator> > queuedEventDispatchMediators;
74 queuedEventDispatchMediators.swap(m_queuedEventDispatchMediators);
75
76 for (size_t i = 0; i < queuedEventDispatchMediators.size(); i++)
77 dispatchEvent(queuedEventDispatchMediators[i].release());
78}
79
80void ScopedEventQueue::dispatchEvent(PassRefPtr<EventDispatchMediator> mediator) const
81{
82 ASSERT(mediator->event()->target());
83 Node* node = mediator->event()->target()->toNode();
84 EventDispatcher::dispatchEvent(node, mediator);
85}
86
87ScopedEventQueue* ScopedEventQueue::instance()
88{
89 if (!s_instance)
90 initialize();
91
92 return s_instance;
93}
94
95void ScopedEventQueue::incrementScopingLevel()
96{
97 m_scopingLevel++;
98}
99
100void ScopedEventQueue::decrementScopingLevel()
101{
102 ASSERT(m_scopingLevel);
103 m_scopingLevel--;
104 if (!m_scopingLevel)
105 dispatchAllEvents();
106}
107
108}