Home | History | Annotate | Download | only in events
      1 /*
      2  * Copyright (C) 2012 Victor Carbune (victor (at) rosedu.org)
      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 #include "config.h"
     27 
     28 #include "core/events/GenericEventQueue.h"
     29 
     30 #include "core/events/Event.h"
     31 #include "core/inspector/InspectorInstrumentation.h"
     32 #include "platform/TraceEvent.h"
     33 
     34 namespace WebCore {
     35 
     36 PassOwnPtrWillBeRawPtr<GenericEventQueue> GenericEventQueue::create(EventTarget* owner)
     37 {
     38     return adoptPtrWillBeNoop(new GenericEventQueue(owner));
     39 }
     40 
     41 GenericEventQueue::GenericEventQueue(EventTarget* owner)
     42     : m_owner(owner)
     43     , m_timer(this, &GenericEventQueue::timerFired)
     44     , m_isClosed(false)
     45 {
     46 }
     47 
     48 GenericEventQueue::~GenericEventQueue()
     49 {
     50 }
     51 
     52 void GenericEventQueue::trace(Visitor* visitor)
     53 {
     54     visitor->trace(m_owner);
     55     visitor->trace(m_pendingEvents);
     56     EventQueue::trace(visitor);
     57 }
     58 
     59 bool GenericEventQueue::enqueueEvent(PassRefPtrWillBeRawPtr<Event> event)
     60 {
     61     if (m_isClosed)
     62         return false;
     63 
     64     if (event->target() == m_owner)
     65         event->setTarget(nullptr);
     66 
     67     TRACE_EVENT_ASYNC_BEGIN1("event", "GenericEventQueue:enqueueEvent", event.get(), "type", event->type().ascii());
     68     InspectorInstrumentation::didEnqueueEvent(event->target() ? event->target() : m_owner.get(), event.get());
     69     m_pendingEvents.append(event);
     70 
     71     if (!m_timer.isActive())
     72         m_timer.startOneShot(0, FROM_HERE);
     73 
     74     return true;
     75 }
     76 
     77 bool GenericEventQueue::cancelEvent(Event* event)
     78 {
     79     bool found = m_pendingEvents.contains(event);
     80 
     81     if (found) {
     82         InspectorInstrumentation::didRemoveEvent(event->target() ? event->target() : m_owner.get(), event);
     83         m_pendingEvents.remove(m_pendingEvents.find(event));
     84         TRACE_EVENT_ASYNC_END2("event", "GenericEventQueue:enqueueEvent", event, "type", event->type().ascii(), "status", "cancelled");
     85     }
     86 
     87     if (m_pendingEvents.isEmpty())
     88         m_timer.stop();
     89 
     90     return found;
     91 }
     92 
     93 void GenericEventQueue::timerFired(Timer<GenericEventQueue>*)
     94 {
     95     ASSERT(!m_timer.isActive());
     96     ASSERT(!m_pendingEvents.isEmpty());
     97 
     98     WillBeHeapVector<RefPtrWillBeMember<Event> > pendingEvents;
     99     m_pendingEvents.swap(pendingEvents);
    100 
    101     RefPtrWillBeRawPtr<EventTarget> protect(m_owner.get());
    102     for (size_t i = 0; i < pendingEvents.size(); ++i) {
    103         Event* event = pendingEvents[i].get();
    104         EventTarget* target = event->target() ? event->target() : m_owner.get();
    105         CString type(event->type().ascii());
    106         TRACE_EVENT_ASYNC_STEP_INTO1("event", "GenericEventQueue:enqueueEvent", event, "dispatch", "type", type);
    107         target->dispatchEvent(pendingEvents[i]);
    108         TRACE_EVENT_ASYNC_END1("event", "GenericEventQueue:enqueueEvent", event, "type", type);
    109         InspectorInstrumentation::didRemoveEvent(target, event);
    110     }
    111 }
    112 
    113 void GenericEventQueue::close()
    114 {
    115     m_isClosed = true;
    116     cancelAllEvents();
    117 }
    118 
    119 void GenericEventQueue::cancelAllEvents()
    120 {
    121     m_timer.stop();
    122 
    123     for (size_t i = 0; i < m_pendingEvents.size(); ++i) {
    124         Event* event = m_pendingEvents[i].get();
    125         TRACE_EVENT_ASYNC_END2("event", "GenericEventQueue:enqueueEvent", event, "type", event->type().ascii(), "status", "cancelled");
    126         InspectorInstrumentation::didRemoveEvent(event->target() ? event->target() : m_owner.get(), event);
    127     }
    128     m_pendingEvents.clear();
    129 }
    130 
    131 bool GenericEventQueue::hasPendingEvents() const
    132 {
    133     return m_pendingEvents.size();
    134 }
    135 
    136 }
    137