Added the GenericEventQueue class in order to generalize asynchronous
event dispatching in HTMLMediaElement.

In order to support asynchronous events dispatched by HTMLTrackElement
there is a need for a generalized implementation of an event queue to
be used for both HTMLMediaElement and Text Tracks.

https://bugs.webkit.org/show_bug.cgi?id=76110

Patch by Victor Carbune <victor@rosedu.org> on 2012-01-27
Reviewed by Eric Carlson.

No new tests. No new functionality, only refactoring.

* CMakeLists.txt:
* GNUmakefile.list.am:
* Target.pri:
* WebCore.gypi:
* WebCore.vcproj/WebCore.vcproj:
* WebCore.xcodeproj/project.pbxproj:

* dom/GenericEventQueue.cpp: Added. Implements the abstract class EventQueue.
(WebCore::GenericEventQueue::GenericEventQueue):
(WebCore::GenericEventQueue::~GenericEventQueue):
(WebCore::GenericEventQueue::enqueueEvent): Append an event to the current
pending event list.
(WebCore::GenericEventQueue::cancelEvent): Removes an event from the current
pending event list.
(WebCore::GenericEventQueue::timerFired): Callback method when the timer fires.
Dispatches all events that are currently pending.
(WebCore::GenericEventQueue::close): Closes the event queue such that it cannot
be used anymore.
(WebCore::GenericEventQueue::cancelAllEvents): Removes all pending events.
(WebCore::GenericEventQueue::hasPendingEvents): True if there are pending events.
* dom/GenericEventQueue.h: Added.

* html/HTMLMediaElement.cpp:
(WebCore::HTMLMediaElement::HTMLMediaElement):
(WebCore::HTMLMediaElement::scheduleEvent): Refactored method to enqueue
the scheduled event on the GenericEventQueue instance.
(WebCore::HTMLMediaElement::cancelPendingEventsAndCallbacks): Ditto.
(WebCore::HTMLMediaElement::hasPendingActivity): Ditto.
(WebCore::HTMLMediaElement::dispatchEvent): This method is overriden
to keep an evidence when a canPlay event is fired.
* html/HTMLMediaElement.h:


git-svn-id: http://svn.webkit.org/repository/webkit/trunk@106156 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/Source/WebCore/dom/GenericEventQueue.cpp b/Source/WebCore/dom/GenericEventQueue.cpp
new file mode 100644
index 0000000..71fcaf0
--- /dev/null
+++ b/Source/WebCore/dom/GenericEventQueue.cpp
@@ -0,0 +1,102 @@
+/*
+ * Copyright (C) 2012 Victor Carbune (victor@rosedu.org)
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
+ */
+
+#include "config.h"
+
+#include "GenericEventQueue.h"
+
+#include "Event.h"
+
+namespace WebCore {
+
+GenericEventQueue::GenericEventQueue()
+    : m_timer(this, &GenericEventQueue::timerFired)
+    , m_isClosed(false)
+{
+}
+
+GenericEventQueue::~GenericEventQueue()
+{
+}
+
+bool GenericEventQueue::enqueueEvent(PassRefPtr<Event> event)
+{
+    if (m_isClosed)
+        return false;
+
+    ASSERT(event->target());
+    m_pendingEvents.append(event);
+
+    if (!m_timer.isActive())
+        m_timer.startOneShot(0);
+
+    return true;
+}
+
+bool GenericEventQueue::cancelEvent(Event* event)
+{
+    bool found = m_pendingEvents.contains(event);
+
+    if (found)
+        m_pendingEvents.remove(m_pendingEvents.find(event));
+
+    if (m_pendingEvents.isEmpty())
+        m_timer.stop();
+
+    return found;
+}
+
+void GenericEventQueue::timerFired(Timer<GenericEventQueue>*)
+{
+    ASSERT(!m_timer.isActive());
+    ASSERT(!m_pendingEvents.isEmpty());
+
+    Vector<RefPtr<Event> > pendingEvents;
+    m_pendingEvents.swap(pendingEvents);
+
+    for (unsigned i = 0; i < pendingEvents.size(); ++i)
+        pendingEvents[i]->target()->dispatchEvent(pendingEvents[i].release());
+}
+
+void GenericEventQueue::close()
+{
+    m_isClosed = true;
+
+    m_timer.stop();
+    m_pendingEvents.clear();
+}
+
+void GenericEventQueue::cancelAllEvents()
+{
+    m_timer.stop();
+    m_pendingEvents.clear();
+}
+
+bool GenericEventQueue::hasPendingEvents() const
+{
+    return m_pendingEvents.size();
+}
+
+}