Clean up ScopedEventQueue
authorzandobersek@gmail.com <zandobersek@gmail.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 29 Oct 2013 07:42:40 +0000 (07:42 +0000)
committerzandobersek@gmail.com <zandobersek@gmail.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 29 Oct 2013 07:42:40 +0000 (07:42 +0000)
https://bugs.webkit.org/show_bug.cgi?id=123408

Reviewed by Darin Adler.

Clean up the ScopedEventQueue implementation. ScopedEventQueue::instance() should return a reference to a
NeverDestroyed<ScopedEventQueue> object. The static ScopedEventQueue::s_instance pointer is removed.

The ScopedEventQueue destructor, the dispatchAllEvents method and the scope level incrementation/decrementation
methods are made private. NeverDestroyed<ScopedEventQueue> and EventQueueScope are made friends of the
ScopedEventQueue class so they can access the constructor and the incrementation/decrementation methods, respectively.

ScopedEventQueue method definitions are reordered to follow the order of their declarations in the header file.
ScopedEventQueue::dispatchAllEvents() now uses std::move to efficiently dispatch and clear all currently queued events.

* dom/EventDispatcher.cpp:
(WebCore::EventDispatcher::dispatchScopedEvent):
* dom/ScopedEventQueue.cpp:
(WebCore::ScopedEventQueue::instance):
(WebCore::ScopedEventQueue::dispatchAllEvents):
* dom/ScopedEventQueue.h:
(WebCore::EventQueueScope::EventQueueScope):
(WebCore::EventQueueScope::~EventQueueScope):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@158182 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebCore/ChangeLog
Source/WebCore/dom/EventDispatcher.cpp
Source/WebCore/dom/ScopedEventQueue.cpp
Source/WebCore/dom/ScopedEventQueue.h

index 4d9bcd7..86d02c9 100644 (file)
@@ -1,3 +1,29 @@
+2013-10-28  Zan Dobersek  <zdobersek@igalia.com>
+
+        Clean up ScopedEventQueue
+        https://bugs.webkit.org/show_bug.cgi?id=123408
+
+        Reviewed by Darin Adler.
+
+        Clean up the ScopedEventQueue implementation. ScopedEventQueue::instance() should return a reference to a
+        NeverDestroyed<ScopedEventQueue> object. The static ScopedEventQueue::s_instance pointer is removed.
+
+        The ScopedEventQueue destructor, the dispatchAllEvents method and the scope level incrementation/decrementation
+        methods are made private. NeverDestroyed<ScopedEventQueue> and EventQueueScope are made friends of the
+        ScopedEventQueue class so they can access the constructor and the incrementation/decrementation methods, respectively.
+
+        ScopedEventQueue method definitions are reordered to follow the order of their declarations in the header file.
+        ScopedEventQueue::dispatchAllEvents() now uses std::move to efficiently dispatch and clear all currently queued events.
+
+        * dom/EventDispatcher.cpp:
+        (WebCore::EventDispatcher::dispatchScopedEvent):
+        * dom/ScopedEventQueue.cpp:
+        (WebCore::ScopedEventQueue::instance):
+        (WebCore::ScopedEventQueue::dispatchAllEvents):
+        * dom/ScopedEventQueue.h:
+        (WebCore::EventQueueScope::EventQueueScope):
+        (WebCore::EventQueueScope::~EventQueueScope):
+
 2013-10-28  Andreas Kling  <akling@apple.com>
 
         applyTextTransform() should take a const RenderStyle&.
index eafe1a2..11f712f 100644 (file)
@@ -207,7 +207,7 @@ void EventDispatcher::dispatchScopedEvent(Node& node, PassRefPtr<Event> event)
 {
     // We need to set the target here because it can go away by the time we actually fire the event.
     event->setTarget(&eventTargetRespectingTargetRules(node));
-    ScopedEventQueue::instance()->enqueueEvent(event);
+    ScopedEventQueue::instance().enqueueEvent(event);
 }
 
 void EventDispatcher::dispatchSimulatedClick(Element* element, Event* underlyingEvent, SimulatedClickMouseEventOptions mouseEventOptions, SimulatedClickVisualOptions visualOptions)
index 9e1dedc..c24550f 100644 (file)
 #include "Event.h"
 #include "EventDispatcher.h"
 #include "EventTarget.h"
-#include <wtf/OwnPtr.h>
 #include <wtf/RefPtr.h>
 
 namespace WebCore {
 
-ScopedEventQueue* ScopedEventQueue::s_instance = 0;
-
 ScopedEventQueue::ScopedEventQueue()
     : m_scopingLevel(0)
 {
@@ -52,11 +49,10 @@ ScopedEventQueue::~ScopedEventQueue()
     ASSERT(!m_queuedEvents.size());
 }
 
-void ScopedEventQueue::initialize()
+ScopedEventQueue& ScopedEventQueue::instance()
 {
-    ASSERT(!s_instance);
-    OwnPtr<ScopedEventQueue> instance = adoptPtr(new ScopedEventQueue);
-    s_instance = instance.leakPtr();
+    static NeverDestroyed<ScopedEventQueue> scopedEventQueue;
+    return scopedEventQueue;
 }
 
 void ScopedEventQueue::enqueueEvent(PassRefPtr<Event> event)
@@ -67,15 +63,6 @@ void ScopedEventQueue::enqueueEvent(PassRefPtr<Event> event)
         dispatchEvent(event);
 }
 
-void ScopedEventQueue::dispatchAllEvents()
-{
-    Vector<RefPtr<Event>> queuedEvents;
-    queuedEvents.swap(m_queuedEvents);
-
-    for (size_t i = 0; i < queuedEvents.size(); i++)
-        dispatchEvent(queuedEvents[i].release());
-}
-
 void ScopedEventQueue::dispatchEvent(PassRefPtr<Event> event) const
 {
     ASSERT(event->target());
@@ -84,12 +71,11 @@ void ScopedEventQueue::dispatchEvent(PassRefPtr<Event> event) const
     EventDispatcher::dispatchEvent(node, event);
 }
 
-ScopedEventQueue* ScopedEventQueue::instance()
+void ScopedEventQueue::dispatchAllEvents()
 {
-    if (!s_instance)
-        initialize();
-
-    return s_instance;
+    Vector<RefPtr<Event>> queuedEvents = std::move(m_queuedEvents);
+    for (size_t i = 0; i < queuedEvents.size(); i++)
+        dispatchEvent(queuedEvents[i].release());
 }
 
 void ScopedEventQueue::incrementScopingLevel()
index ee63e9c..097b4b4 100644 (file)
@@ -31,6 +31,7 @@
 #ifndef ScopedEventQueue_h
 #define ScopedEventQueue_h
 
+#include <wtf/NeverDestroyed.h>
 #include <wtf/Noncopyable.h>
 #include <wtf/PassRefPtr.h>
 #include <wtf/RefPtr.h>
 namespace WebCore {
 
 class Event;
+class EventQueueScope;
 
 class ScopedEventQueue {
     WTF_MAKE_NONCOPYABLE(ScopedEventQueue); WTF_MAKE_FAST_ALLOCATED;
 public:
-    ~ScopedEventQueue();
-
+    static ScopedEventQueue& instance();
     void enqueueEvent(PassRefPtr<Event>);
-    void dispatchAllEvents();
-    static ScopedEventQueue* instance();
-
-    void incrementScopingLevel();
-    void decrementScopingLevel();
 
 private:
     ScopedEventQueue();
-    static void initialize();
+    ~ScopedEventQueue();
+
     void dispatchEvent(PassRefPtr<Event>) const;
+    void dispatchAllEvents();
+    void incrementScopingLevel();
+    void decrementScopingLevel();
 
     Vector<RefPtr<Event>> m_queuedEvents;
     unsigned m_scopingLevel;
 
-    static ScopedEventQueue* s_instance;
+    friend NeverDestroyed<ScopedEventQueue>;
+    friend EventQueueScope;
 };
 
 class EventQueueScope {
     WTF_MAKE_NONCOPYABLE(EventQueueScope);
-
 public:
-    EventQueueScope() { ScopedEventQueue::instance()->incrementScopingLevel(); }
-    ~EventQueueScope() { ScopedEventQueue::instance()->decrementScopingLevel(); }
+    EventQueueScope() { ScopedEventQueue::instance().incrementScopingLevel(); }
+    ~EventQueueScope() { ScopedEventQueue::instance().decrementScopingLevel(); }
 };
 
 }