Title: [241599] trunk/Source/WebCore
Revision
241599
Author
[email protected]
Date
2019-02-15 11:08:06 -0800 (Fri, 15 Feb 2019)

Log Message

WebSocket should not fire events after being stopped
https://bugs.webkit.org/show_bug.cgi?id=194690

Reviewed by Geoffrey Garen.

dispatchOrQueueErrorEvent is scheduled using RunLoop::main().dispatch or dispatch_async.
This makes it possible to dispatch an event while WebSocket is already stopped.
Instead, use Document::postTask so that the task is only executed if WebSocket is not stopped.

As a refactoring, make use of PendingActivity to keep track of setPendingActivity/unsetPendingActivity more easily.

* Modules/websockets/WebSocket.cpp:
(WebCore::WebSocket::stop):
(WebCore::WebSocket::connect):
* Modules/websockets/WebSocket.h:

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (241598 => 241599)


--- trunk/Source/WebCore/ChangeLog	2019-02-15 19:06:47 UTC (rev 241598)
+++ trunk/Source/WebCore/ChangeLog	2019-02-15 19:08:06 UTC (rev 241599)
@@ -1,5 +1,23 @@
 2019-02-15  Youenn Fablet  <[email protected]>
 
+        WebSocket should not fire events after being stopped
+        https://bugs.webkit.org/show_bug.cgi?id=194690
+
+        Reviewed by Geoffrey Garen.
+
+        dispatchOrQueueErrorEvent is scheduled using RunLoop::main().dispatch or dispatch_async.
+        This makes it possible to dispatch an event while WebSocket is already stopped.
+        Instead, use Document::postTask so that the task is only executed if WebSocket is not stopped.
+
+        As a refactoring, make use of PendingActivity to keep track of setPendingActivity/unsetPendingActivity more easily.
+
+        * Modules/websockets/WebSocket.cpp:
+        (WebCore::WebSocket::stop):
+        (WebCore::WebSocket::connect):
+        * Modules/websockets/WebSocket.h:
+
+2019-02-15  Youenn Fablet  <[email protected]>
+
         Performance should not fire events when its context is stopped
         https://bugs.webkit.org/show_bug.cgi?id=194689
 

Modified: trunk/Source/WebCore/Modules/websockets/WebSocket.cpp (241598 => 241599)


--- trunk/Source/WebCore/Modules/websockets/WebSocket.cpp	2019-02-15 19:06:47 UTC (rev 241598)
+++ trunk/Source/WebCore/Modules/websockets/WebSocket.cpp	2019-02-15 19:08:06 UTC (rev 241599)
@@ -288,28 +288,18 @@
         Document& document = downcast<Document>(context);
         RefPtr<Frame> frame = document.frame();
         if (!frame || !frame->loader().mixedContentChecker().canRunInsecureContent(document.securityOrigin(), m_url)) {
-            // Balanced by the call to unsetPendingActivity() in WebSocket::stop().
-            setPendingActivity(*this);
+            m_pendingActivity = makePendingActivity(*this);
 
             // We must block this connection. Instead of throwing an exception, we indicate this
             // using the error event. But since this code executes as part of the WebSocket's
             // constructor, we have to wait until the constructor has completed before firing the
             // event; otherwise, users can't connect to the event.
-#if USE(WEB_THREAD)
-            ref();
-            dispatch_async(dispatch_get_main_queue(), ^{
-                WebThreadRun(^{
-                    dispatchOrQueueErrorEvent();
-                    stop();
-                    deref();
-                });
+
+            document.postTask([this, protectedThis = makeRef(*this)](auto&) {
+                this->dispatchOrQueueErrorEvent();
+                this->stop();
             });
-#else
-            RunLoop::main().dispatch([this, protectedThis = makeRef(*this)]() {
-                dispatchOrQueueErrorEvent();
-                stop();
-            });
-#endif
+
             return { };
         }
     }
@@ -319,7 +309,7 @@
         protocolString = joinStrings(protocols, subprotocolSeparator());
 
     m_channel->connect(m_url, protocolString);
-    setPendingActivity(*this);
+    m_pendingActivity = makePendingActivity(*this);
 
     return { };
 }
@@ -540,7 +530,6 @@
 
 void WebSocket::stop()
 {
-    bool pending = hasPendingActivity();
     if (m_channel)
         m_channel->disconnect();
     m_channel = nullptr;
@@ -547,8 +536,7 @@
     m_state = CLOSED;
     m_pendingEvents.clear();
     ActiveDOMObject::stop();
-    if (pending)
-        unsetPendingActivity(*this);
+    m_pendingActivity = nullptr;
 }
 
 const char* WebSocket::activeDOMObjectName() const
@@ -631,8 +619,7 @@
         m_channel->disconnect();
         m_channel = nullptr;
     }
-    if (hasPendingActivity())
-        unsetPendingActivity(*this);
+    m_pendingActivity = nullptr;
 }
 
 void WebSocket::didUpgradeURL()

Modified: trunk/Source/WebCore/Modules/websockets/WebSocket.h (241598 => 241599)


--- trunk/Source/WebCore/Modules/websockets/WebSocket.h	2019-02-15 19:06:47 UTC (rev 241598)
+++ trunk/Source/WebCore/Modules/websockets/WebSocket.h	2019-02-15 19:08:06 UTC (rev 241599)
@@ -143,6 +143,7 @@
     bool m_shouldDelayEventFiring { false };
     Deque<Ref<Event>> m_pendingEvents;
     bool m_dispatchedErrorEvent { false };
+    RefPtr<PendingActivity<WebSocket>> m_pendingActivity;
 };
 
 } // namespace WebCore
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to