Torne (Richard Coles) | 53e740f | 2013-05-09 18:38:43 +0100 | [diff] [blame^] | 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 "core/dom/DocumentEventQueue.h" |
| 29 | |
| 30 | #include "core/dom/Document.h" |
| 31 | #include "core/dom/Event.h" |
| 32 | #include "core/dom/EventNames.h" |
| 33 | #include "core/dom/ScriptExecutionContext.h" |
| 34 | #include "core/dom/WebCoreMemoryInstrumentation.h" |
| 35 | #include "core/page/DOMWindow.h" |
| 36 | #include "core/page/SuspendableTimer.h" |
| 37 | #include <wtf/MemoryInstrumentationHashSet.h> |
| 38 | #include <wtf/MemoryInstrumentationListHashSet.h> |
| 39 | |
| 40 | namespace WebCore { |
| 41 | |
| 42 | class DocumentEventQueueTimer : public SuspendableTimer { |
| 43 | WTF_MAKE_NONCOPYABLE(DocumentEventQueueTimer); |
| 44 | public: |
| 45 | DocumentEventQueueTimer(DocumentEventQueue* eventQueue, ScriptExecutionContext* context) |
| 46 | : SuspendableTimer(context) |
| 47 | , m_eventQueue(eventQueue) { } |
| 48 | |
| 49 | virtual void reportMemoryUsage(MemoryObjectInfo* memoryObjectInfo) const OVERRIDE |
| 50 | { |
| 51 | MemoryClassInfo info(memoryObjectInfo, this, WebCoreMemoryTypes::DOM); |
| 52 | SuspendableTimer::reportMemoryUsage(memoryObjectInfo); |
| 53 | info.addWeakPointer(m_eventQueue); |
| 54 | } |
| 55 | |
| 56 | private: |
| 57 | virtual void fired() { m_eventQueue->pendingEventTimerFired(); } |
| 58 | DocumentEventQueue* m_eventQueue; |
| 59 | }; |
| 60 | |
| 61 | PassRefPtr<DocumentEventQueue> DocumentEventQueue::create(ScriptExecutionContext* context) |
| 62 | { |
| 63 | return adoptRef(new DocumentEventQueue(context)); |
| 64 | } |
| 65 | |
| 66 | DocumentEventQueue::DocumentEventQueue(ScriptExecutionContext* context) |
| 67 | : m_pendingEventTimer(adoptPtr(new DocumentEventQueueTimer(this, context))) |
| 68 | , m_isClosed(false) |
| 69 | { |
| 70 | m_pendingEventTimer->suspendIfNeeded(); |
| 71 | } |
| 72 | |
| 73 | DocumentEventQueue::~DocumentEventQueue() |
| 74 | { |
| 75 | } |
| 76 | |
| 77 | bool DocumentEventQueue::enqueueEvent(PassRefPtr<Event> event) |
| 78 | { |
| 79 | if (m_isClosed) |
| 80 | return false; |
| 81 | |
| 82 | ASSERT(event->target()); |
| 83 | bool wasAdded = m_queuedEvents.add(event).isNewEntry; |
| 84 | ASSERT_UNUSED(wasAdded, wasAdded); // It should not have already been in the list. |
| 85 | |
| 86 | if (!m_pendingEventTimer->isActive()) |
| 87 | m_pendingEventTimer->startOneShot(0); |
| 88 | |
| 89 | return true; |
| 90 | } |
| 91 | |
| 92 | void DocumentEventQueue::enqueueOrDispatchScrollEvent(PassRefPtr<Node> target, ScrollEventTargetType targetType) |
| 93 | { |
| 94 | if (!target->document()->hasListenerType(Document::SCROLL_LISTENER)) |
| 95 | return; |
| 96 | |
| 97 | // Per the W3C CSSOM View Module, scroll events fired at the document should bubble, others should not. |
| 98 | bool canBubble = targetType == ScrollEventDocumentTarget; |
| 99 | RefPtr<Event> scrollEvent = Event::create(eventNames().scrollEvent, canBubble, false /* non cancelleable */); |
| 100 | |
| 101 | if (!m_nodesWithQueuedScrollEvents.add(target.get()).isNewEntry) |
| 102 | return; |
| 103 | |
| 104 | scrollEvent->setTarget(target); |
| 105 | enqueueEvent(scrollEvent.release()); |
| 106 | } |
| 107 | |
| 108 | void DocumentEventQueue::reportMemoryUsage(MemoryObjectInfo* memoryObjectInfo) const |
| 109 | { |
| 110 | MemoryClassInfo info(memoryObjectInfo, this, WebCoreMemoryTypes::DOM); |
| 111 | info.addMember(m_pendingEventTimer, "pendingEventTimer"); |
| 112 | info.addMember(m_queuedEvents, "queuedEvents"); |
| 113 | info.addMember(m_nodesWithQueuedScrollEvents, "nodesWithQueuedScrollEvents"); |
| 114 | } |
| 115 | |
| 116 | bool DocumentEventQueue::cancelEvent(Event* event) |
| 117 | { |
| 118 | bool found = m_queuedEvents.contains(event); |
| 119 | m_queuedEvents.remove(event); |
| 120 | if (m_queuedEvents.isEmpty()) |
| 121 | m_pendingEventTimer->stop(); |
| 122 | return found; |
| 123 | } |
| 124 | |
| 125 | void DocumentEventQueue::close() |
| 126 | { |
| 127 | m_isClosed = true; |
| 128 | m_pendingEventTimer->stop(); |
| 129 | m_queuedEvents.clear(); |
| 130 | } |
| 131 | |
| 132 | void DocumentEventQueue::pendingEventTimerFired() |
| 133 | { |
| 134 | ASSERT(!m_pendingEventTimer->isActive()); |
| 135 | ASSERT(!m_queuedEvents.isEmpty()); |
| 136 | |
| 137 | m_nodesWithQueuedScrollEvents.clear(); |
| 138 | |
| 139 | // Insert a marker for where we should stop. |
| 140 | ASSERT(!m_queuedEvents.contains(0)); |
| 141 | bool wasAdded = m_queuedEvents.add(0).isNewEntry; |
| 142 | ASSERT_UNUSED(wasAdded, wasAdded); // It should not have already been in the list. |
| 143 | |
| 144 | RefPtr<DocumentEventQueue> protector(this); |
| 145 | |
| 146 | while (!m_queuedEvents.isEmpty()) { |
| 147 | ListHashSet<RefPtr<Event>, 16>::iterator iter = m_queuedEvents.begin(); |
| 148 | RefPtr<Event> event = *iter; |
| 149 | m_queuedEvents.remove(iter); |
| 150 | if (!event) |
| 151 | break; |
| 152 | dispatchEvent(event.get()); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | void DocumentEventQueue::dispatchEvent(PassRefPtr<Event> event) |
| 157 | { |
| 158 | EventTarget* eventTarget = event->target(); |
| 159 | if (eventTarget->toDOMWindow()) |
| 160 | eventTarget->toDOMWindow()->dispatchEvent(event, 0); |
| 161 | else |
| 162 | eventTarget->dispatchEvent(event); |
| 163 | } |
| 164 | |
| 165 | } |