Title: [251953] trunk/Source/WebCore
Revision
251953
Author
cdu...@apple.com
Date
2019-11-01 16:46:25 -0700 (Fri, 01 Nov 2019)

Log Message

Port FileReader to the HTML5 event loop
https://bugs.webkit.org/show_bug.cgi?id=203749

Reviewed by Ryosuke Niwa.

* dom/AbstractEventLoop.h:
* fileapi/FileReader.cpp:
(WebCore::FileReader::FileReader):
(WebCore::FileReader::stop):
(WebCore::FileReader::hasPendingActivity const):
(WebCore::FileReader::abort):
(WebCore::FileReader::didStartLoading):
(WebCore::FileReader::didReceiveData):
(WebCore::FileReader::didFinishLoading):
(WebCore::FileReader::didFail):
(WebCore::FileReader::enqueueTask):
* fileapi/FileReader.h:

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (251952 => 251953)


--- trunk/Source/WebCore/ChangeLog	2019-11-01 23:34:27 UTC (rev 251952)
+++ trunk/Source/WebCore/ChangeLog	2019-11-01 23:46:25 UTC (rev 251953)
@@ -1,3 +1,23 @@
+2019-11-01  Chris Dumez  <cdu...@apple.com>
+
+        Port FileReader to the HTML5 event loop
+        https://bugs.webkit.org/show_bug.cgi?id=203749
+
+        Reviewed by Ryosuke Niwa.
+
+        * dom/AbstractEventLoop.h:
+        * fileapi/FileReader.cpp:
+        (WebCore::FileReader::FileReader):
+        (WebCore::FileReader::stop):
+        (WebCore::FileReader::hasPendingActivity const):
+        (WebCore::FileReader::abort):
+        (WebCore::FileReader::didStartLoading):
+        (WebCore::FileReader::didReceiveData):
+        (WebCore::FileReader::didFinishLoading):
+        (WebCore::FileReader::didFail):
+        (WebCore::FileReader::enqueueTask):
+        * fileapi/FileReader.h:
+
 2019-11-01  Eric Carlson  <eric.carl...@apple.com>
 
         Add experimental TextTrackCue API

Modified: trunk/Source/WebCore/dom/AbstractEventLoop.h (251952 => 251953)


--- trunk/Source/WebCore/dom/AbstractEventLoop.h	2019-11-01 23:34:27 UTC (rev 251952)
+++ trunk/Source/WebCore/dom/AbstractEventLoop.h	2019-11-01 23:46:25 UTC (rev 251953)
@@ -34,6 +34,7 @@
 
 enum class TaskSource : uint8_t {
     DOMManipulation,
+    FileReading,
     IdleTask,
     Networking,
     UserInteraction

Modified: trunk/Source/WebCore/fileapi/FileReader.cpp (251952 => 251953)


--- trunk/Source/WebCore/fileapi/FileReader.cpp	2019-11-01 23:34:27 UTC (rev 251952)
+++ trunk/Source/WebCore/fileapi/FileReader.cpp	2019-11-01 23:46:25 UTC (rev 251953)
@@ -31,12 +31,12 @@
 #include "config.h"
 #include "FileReader.h"
 
+#include "AbstractEventLoop.h"
 #include "EventNames.h"
 #include "File.h"
 #include "Logging.h"
 #include "ProgressEvent.h"
 #include "ScriptExecutionContext.h"
-#include "SuspendableTaskQueue.h"
 #include <_javascript_Core/ArrayBuffer.h>
 #include <wtf/IsoMallocInlines.h>
 #include <wtf/text/CString.h>
@@ -57,7 +57,6 @@
 
 FileReader::FileReader(ScriptExecutionContext& context)
     : ActiveDOMObject(&context)
-    , m_taskQueue(SuspendableTaskQueue::create(&context))
 {
 }
 
@@ -74,6 +73,7 @@
 
 void FileReader::stop()
 {
+    m_pendingTasks.clear();
     if (m_loader) {
         m_loader->cancel();
         m_loader = nullptr;
@@ -83,7 +83,7 @@
 
 bool FileReader::hasPendingActivity() const
 {
-    return m_taskQueue->hasPendingTasks() || m_state == LOADING || ActiveDOMObject::hasPendingActivity();
+    return m_state == LOADING || ActiveDOMObject::hasPendingActivity();
 }
 
 ExceptionOr<void> FileReader::readAsArrayBuffer(Blob* blob)
@@ -155,8 +155,8 @@
     m_aborting = true;
 
     // Schedule to have the abort done later since abort() might be called from the event handler and we do not want the resource loading code to be in the stack.
-    m_taskQueue->cancelAllTasks();
-    m_taskQueue->enqueueTask([this] {
+    m_pendingTasks.clear();
+    enqueueTask([this] {
         ASSERT(m_state != DONE);
 
         stop();
@@ -172,7 +172,7 @@
 
 void FileReader::didStartLoading()
 {
-    m_taskQueue->enqueueTask([this] {
+    enqueueTask([this] {
         fireEvent(eventNames().loadstartEvent);
     });
 }
@@ -179,7 +179,7 @@
 
 void FileReader::didReceiveData()
 {
-    m_taskQueue->enqueueTask([this] {
+    enqueueTask([this] {
         auto now = MonotonicTime::now();
         if (std::isnan(m_lastProgressNotificationTime)) {
             m_lastProgressNotificationTime = now;
@@ -197,7 +197,7 @@
     if (m_aborting)
         return;
 
-    m_taskQueue->enqueueTask([this] {
+    enqueueTask([this] {
         ASSERT(m_state != DONE);
         m_state = DONE;
 
@@ -213,7 +213,7 @@
     if (m_aborting)
         return;
 
-    m_taskQueue->enqueueTask([this, errorCode] {
+    enqueueTask([this, errorCode] {
         ASSERT(m_state != DONE);
         m_state = DONE;
 
@@ -245,4 +245,20 @@
     return { WTFMove(result) };
 }
 
+void FileReader::enqueueTask(Function<void()>&& task)
+{
+    auto* context = scriptExecutionContext();
+    if (!context)
+        return;
+
+    static uint64_t taskIdentifierSeed = 0;
+    uint64_t taskIdentifier = ++taskIdentifierSeed;
+    m_pendingTasks.add(taskIdentifier, WTFMove(task));
+    context->eventLoop().queueTask(TaskSource::FileReading, *context, [this, protectedThis = makeRef(*this), pendingActivity = makePendingActivity(*this), taskIdentifier] {
+        auto task = m_pendingTasks.take(taskIdentifier);
+        if (task)
+            task();
+    });
+}
+
 } // namespace WebCore

Modified: trunk/Source/WebCore/fileapi/FileReader.h (251952 => 251953)


--- trunk/Source/WebCore/fileapi/FileReader.h	2019-11-01 23:34:27 UTC (rev 251952)
+++ trunk/Source/WebCore/fileapi/FileReader.h	2019-11-01 23:46:25 UTC (rev 251953)
@@ -36,6 +36,7 @@
 #include "FileError.h"
 #include "FileReaderLoader.h"
 #include "FileReaderLoaderClient.h"
+#include <wtf/HashMap.h>
 #include <wtf/UniqueRef.h>
 
 namespace JSC {
@@ -45,7 +46,6 @@
 namespace WebCore {
 
 class Blob;
-class SuspendableTaskQueue;
 
 class FileReader final : public RefCounted<FileReader>, public ActiveDOMObject, public EventTargetWithInlineData, private FileReaderLoaderClient {
     WTF_MAKE_ISO_ALLOCATED(FileReader);
@@ -91,6 +91,8 @@
     void refEventTarget() final { ref(); }
     void derefEventTarget() final { deref(); }
 
+    void enqueueTask(Function<void()>&&);
+
     void didStartLoading() final;
     void didReceiveData() final;
     void didFinishLoading() final;
@@ -108,7 +110,7 @@
     std::unique_ptr<FileReaderLoader> m_loader;
     RefPtr<FileError> m_error;
     MonotonicTime m_lastProgressNotificationTime { MonotonicTime::nan() };
-    UniqueRef<SuspendableTaskQueue> m_taskQueue;
+    HashMap<uint64_t, Function<void()>> m_pendingTasks;
 };
 
 } // namespace WebCore
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to