Title: [292860] trunk
Revision
292860
Author
[email protected]
Date
2022-04-14 00:16:17 -0700 (Thu, 14 Apr 2022)

Log Message

A shared worker in a cached page should not allow the remote shared worker to continue executing
https://bugs.webkit.org/show_bug.cgi?id=239286

Reviewed by Chris Dumez.

Source/WebCore:

SharedWorker need to react to resume/suspend in case of page cache so that the corresponding
remote shared worker might get suspended if all its shared worker objects are suspended.

Test: http/tests/navigation/page-cache-shared-worker.html

* workers/shared/SharedWorker.cpp:
* workers/shared/SharedWorker.h:
* workers/shared/SharedWorkerObjectConnection.h:
* workers/shared/context/SharedWorkerContextManager.cpp:
* workers/shared/context/SharedWorkerContextManager.h:

Source/WebKit:

Store whether shared worker objects are suspended or resumed.
If all objects are suspended, suspend the remote shared worker.
Otherwise, keep the remote shared worker live.
Add plumbing code to make this happen.

* NetworkProcess/ServiceWorker/WebSWServerConnection.cpp:
* NetworkProcess/SharedWorker/WebSharedWorker.cpp:
* NetworkProcess/SharedWorker/WebSharedWorker.h:
* NetworkProcess/SharedWorker/WebSharedWorkerServer.cpp:
* NetworkProcess/SharedWorker/WebSharedWorkerServer.h:
* NetworkProcess/SharedWorker/WebSharedWorkerServerConnection.cpp:
* NetworkProcess/SharedWorker/WebSharedWorkerServerConnection.h:
* NetworkProcess/SharedWorker/WebSharedWorkerServerConnection.messages.in:
* NetworkProcess/SharedWorker/WebSharedWorkerServerToContextConnection.cpp:
* NetworkProcess/SharedWorker/WebSharedWorkerServerToContextConnection.h:
* WebProcess/Storage/WebSharedWorkerContextManagerConnection.messages.in:
* WebProcess/Storage/WebSharedWorkerObjectConnection.cpp:
* WebProcess/Storage/WebSharedWorkerObjectConnection.h:

LayoutTests:

* http/tests/navigation/page-cache-shared-worker-expected.txt: Added.
* http/tests/navigation/page-cache-shared-worker.html: Added.
* http/tests/navigation/resources/page-cache-helper-for-sharedworker.html: Added.
* http/tests/navigation/resources/shared-worker-script.js: Added.
* platform/mac-wk1/TestExpectations:
* platform/win/TestExpectations:

Modified Paths

Added Paths

Diff

Modified: trunk/LayoutTests/ChangeLog (292859 => 292860)


--- trunk/LayoutTests/ChangeLog	2022-04-14 06:19:01 UTC (rev 292859)
+++ trunk/LayoutTests/ChangeLog	2022-04-14 07:16:17 UTC (rev 292860)
@@ -1,3 +1,17 @@
+2022-04-14  Youenn Fablet  <[email protected]>
+
+        A shared worker in a cached page should not allow the remote shared worker to continue executing
+        https://bugs.webkit.org/show_bug.cgi?id=239286
+
+        Reviewed by Chris Dumez.
+
+        * http/tests/navigation/page-cache-shared-worker-expected.txt: Added.
+        * http/tests/navigation/page-cache-shared-worker.html: Added.
+        * http/tests/navigation/resources/page-cache-helper-for-sharedworker.html: Added.
+        * http/tests/navigation/resources/shared-worker-script.js: Added.
+        * platform/mac-wk1/TestExpectations:
+        * platform/win/TestExpectations:
+
 2022-04-13  Myles C. Maxfield  <[email protected]>
 
         Revert r291846 because it caused a 3% performance regression

Added: trunk/LayoutTests/http/tests/navigation/page-cache-shared-worker-expected.txt (0 => 292860)


--- trunk/LayoutTests/http/tests/navigation/page-cache-shared-worker-expected.txt	                        (rev 0)
+++ trunk/LayoutTests/http/tests/navigation/page-cache-shared-worker-expected.txt	2022-04-14 07:16:17 UTC (rev 292860)
@@ -0,0 +1,14 @@
+Tests that a page with a SharedWorker goes into the page cache.
+
+On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
+
+
+pageshow - not from cache
+pagehide - entering cache
+pageshow - from cache
+PASS Page did enter and was restored from the page cache
+PASS counter is below 100
+PASS successfullyParsed is true
+
+TEST COMPLETE
+

Added: trunk/LayoutTests/http/tests/navigation/page-cache-shared-worker.html (0 => 292860)


--- trunk/LayoutTests/http/tests/navigation/page-cache-shared-worker.html	                        (rev 0)
+++ trunk/LayoutTests/http/tests/navigation/page-cache-shared-worker.html	2022-04-14 07:16:17 UTC (rev 292860)
@@ -0,0 +1,50 @@
+<!-- webkit-test-runner [ UsesBackForwardCache=true ] -->
+<!DOCTYPE html>
+<html>
+<body>
+<script src=""
+<script>
+description('Tests that a page with a SharedWorker goes into the page cache.');
+window.jsTestIsAsync = true;
+
+var restoredFromPageCache = false;
+var sw;
+
+window.addEventListener("pageshow", async function(event) {
+    debug("pageshow - " + (event.persisted ? "" : "not ") + "from cache");
+
+    if (event.persisted) {
+        testPassed("Page did enter and was restored from the page cache");
+        restoredFromPageCache = true;
+
+        sw.port.postMessage('counter');
+        const counter = await new Promise(resolve => sw.port._onmessage_ = (event) => resolve(event.data));
+        if (counter < 100)
+            testPassed("counter is below 100");
+        else
+            testFailed("counter is above 100");
+
+        setTimeout(finishJSTest, 0);
+    }
+}, false);
+
+window.addEventListener("pagehide", function(event) {
+    debug("pagehide - " + (event.persisted ? "" : "not ") + "entering cache");
+    if (!event.persisted) {
+        testFailed("Page did not enter the page cache.");
+        finishJSTest();
+    }
+}, false);
+
+window.addEventListener('load', function() {
+    sw = new SharedWorker('resources/shared-worker-script.js');
+    setTimeout(function() {
+      // Force a back navigation back to this page.
+      window.location.href = ""
+    }, 1000);
+}, false);
+
+</script>
+<script src=""
+</body>
+</html>

Added: trunk/LayoutTests/http/tests/navigation/resources/page-cache-helper-for-sharedworker.html (0 => 292860)


--- trunk/LayoutTests/http/tests/navigation/resources/page-cache-helper-for-sharedworker.html	                        (rev 0)
+++ trunk/LayoutTests/http/tests/navigation/resources/page-cache-helper-for-sharedworker.html	2022-04-14 07:16:17 UTC (rev 292860)
@@ -0,0 +1,28 @@
+This page should go back. If a test outputs the contents of this
+page, then the test page failed to enter the page cache.
+<script>
+var channel = new BroadcastChannel('shared-worker');
+async function doTest(counter)
+{
+    if (!counter)
+        counter = 0;
+    else if (counter >= 100)
+        return;
+
+    channel.postMessage('ping');
+    return new Promise((resolve, reject) => {
+         let myTimer = setTimeout(resolve, 1000);
+         channel._onmessage_ = async () => {
+             clearTimeout(myTimer);
+             // We received a message from shared worker, let's call doTest to retry.
+             await new Promise(resolve => setTimeout(resolve, 50));
+             doTest(++counter).then(resolve, reject);
+         };
+    });
+}
+
+window.addEventListener("load", async () => {
+    await doTest();
+    history.back();
+}, false);
+</script>

Added: trunk/LayoutTests/http/tests/navigation/resources/shared-worker-script.js (0 => 292860)


--- trunk/LayoutTests/http/tests/navigation/resources/shared-worker-script.js	                        (rev 0)
+++ trunk/LayoutTests/http/tests/navigation/resources/shared-worker-script.js	2022-04-14 07:16:17 UTC (rev 292860)
@@ -0,0 +1,15 @@
+var channel = new BroadcastChannel('shared-worker');
+var counter = 0;
+channel._onmessage_ = (event) => {
+    if (event.data ="" 'ping') {
+        counter++;
+        channel.postMessage('pong');
+    }
+}
+self._onconnect_ = function(e) {
+    const port = e.ports[0];
+    port._onmessage_ = (event) => {
+        if (event.data ="" 'counter')
+            port.postMessage(counter);
+    };
+}

Modified: trunk/LayoutTests/platform/mac-wk1/TestExpectations (292859 => 292860)


--- trunk/LayoutTests/platform/mac-wk1/TestExpectations	2022-04-14 06:19:01 UTC (rev 292859)
+++ trunk/LayoutTests/platform/mac-wk1/TestExpectations	2022-04-14 07:16:17 UTC (rev 292860)
@@ -68,6 +68,7 @@
 media/audio-session-category-at-most-recent-playback.html [ Skip ]
 
 # Shared workers are only implemented for WebKit2.
+http/tests/navigation/page-cache-shared-worker.html [ Skip ]
 imported/w3c/web-platform-tests/content-security-policy/connect-src/shared-worker-connect-src-allowed.sub.html [ Skip ]
 imported/w3c/web-platform-tests/content-security-policy/connect-src/shared-worker-connect-src-blocked.sub.html [ Skip ]
 imported/w3c/web-platform-tests/content-security-policy/gen/top.http-rp/script-src-self/sharedworker-classic.http.html [ Skip ]

Modified: trunk/LayoutTests/platform/win/TestExpectations (292859 => 292860)


--- trunk/LayoutTests/platform/win/TestExpectations	2022-04-14 06:19:01 UTC (rev 292859)
+++ trunk/LayoutTests/platform/win/TestExpectations	2022-04-14 07:16:17 UTC (rev 292860)
@@ -165,6 +165,7 @@
 http/tests/referrer-policy-img/unsafe-url/cross-origin-http.https.html [ Skip ]
 
 # Shared workers are only implemented for WebKit2.
+http/tests/navigation/page-cache-shared-worker.html [ Skip ]
 imported/w3c/web-platform-tests/content-security-policy/connect-src/shared-worker-connect-src-allowed.sub.html [ Skip ]
 imported/w3c/web-platform-tests/content-security-policy/connect-src/shared-worker-connect-src-blocked.sub.html [ Skip ]
 imported/w3c/web-platform-tests/content-security-policy/gen/top.http-rp/script-src-self/sharedworker-classic.http.html [ Skip ]

Modified: trunk/Source/WebCore/ChangeLog (292859 => 292860)


--- trunk/Source/WebCore/ChangeLog	2022-04-14 06:19:01 UTC (rev 292859)
+++ trunk/Source/WebCore/ChangeLog	2022-04-14 07:16:17 UTC (rev 292860)
@@ -1,3 +1,21 @@
+2022-04-14  Youenn Fablet  <[email protected]>
+
+        A shared worker in a cached page should not allow the remote shared worker to continue executing
+        https://bugs.webkit.org/show_bug.cgi?id=239286
+
+        Reviewed by Chris Dumez.
+
+        SharedWorker need to react to resume/suspend in case of page cache so that the corresponding
+        remote shared worker might get suspended if all its shared worker objects are suspended.
+
+        Test: http/tests/navigation/page-cache-shared-worker.html
+
+        * workers/shared/SharedWorker.cpp:
+        * workers/shared/SharedWorker.h:
+        * workers/shared/SharedWorkerObjectConnection.h:
+        * workers/shared/context/SharedWorkerContextManager.cpp:
+        * workers/shared/context/SharedWorkerContextManager.h:
+
 2022-04-13  Myles C. Maxfield  <[email protected]>
 
         Revert r291846 because it caused a 3% performance regression

Modified: trunk/Source/WebCore/workers/shared/SharedWorker.cpp (292859 => 292860)


--- trunk/Source/WebCore/workers/shared/SharedWorker.cpp	2022-04-14 06:19:01 UTC (rev 292859)
+++ trunk/Source/WebCore/workers/shared/SharedWorker.cpp	2022-04-14 07:16:17 UTC (rev 292860)
@@ -165,6 +165,22 @@
     mainThreadConnection()->sharedWorkerObjectIsGoingAway(m_key, m_identifier);
 }
 
+void SharedWorker::suspend(ReasonForSuspension reason)
+{
+    if (reason == ReasonForSuspension::BackForwardCache) {
+        mainThreadConnection()->suspendForBackForwardCache(m_key, m_identifier);
+        m_isSuspendedForBackForwardCache = true;
+    }
+}
+
+void SharedWorker::resume()
+{
+    if (m_isSuspendedForBackForwardCache) {
+        mainThreadConnection()->resumeForBackForwardCache(m_key, m_identifier);
+        m_isSuspendedForBackForwardCache = false;
+    }
+}
+
 #undef SHARED_WORKER_RELEASE_LOG
 #undef SHARED_WORKER_RELEASE_LOG_ERROR
 

Modified: trunk/Source/WebCore/workers/shared/SharedWorker.h (292859 => 292860)


--- trunk/Source/WebCore/workers/shared/SharedWorker.h	2022-04-14 06:19:01 UTC (rev 292859)
+++ trunk/Source/WebCore/workers/shared/SharedWorker.h	2022-04-14 07:16:17 UTC (rev 292860)
@@ -66,6 +66,8 @@
     const char* activeDOMObjectName() const final;
     void stop() final;
     bool virtualHasPendingActivity() const final;
+    void suspend(ReasonForSuspension) final;
+    void resume() final;
 
 
     SharedWorkerKey m_key;
@@ -74,6 +76,7 @@
     String m_identifierForInspector;
     BlobURLHandle m_blobURLExtension;
     bool m_isActive { true };
+    bool m_isSuspendedForBackForwardCache { false };
 };
 
 } // namespace WebCore

Modified: trunk/Source/WebCore/workers/shared/SharedWorkerObjectConnection.h (292859 => 292860)


--- trunk/Source/WebCore/workers/shared/SharedWorkerObjectConnection.h	2022-04-14 06:19:01 UTC (rev 292859)
+++ trunk/Source/WebCore/workers/shared/SharedWorkerObjectConnection.h	2022-04-14 07:16:17 UTC (rev 292860)
@@ -47,6 +47,8 @@
 
     virtual void requestSharedWorker(const SharedWorkerKey&, SharedWorkerObjectIdentifier, TransferredMessagePort&&, const WorkerOptions&) = 0;
     virtual void sharedWorkerObjectIsGoingAway(const SharedWorkerKey&, SharedWorkerObjectIdentifier) = 0;
+    virtual void suspendForBackForwardCache(const SharedWorkerKey&, SharedWorkerObjectIdentifier) = 0;
+    virtual void resumeForBackForwardCache(const SharedWorkerKey&, SharedWorkerObjectIdentifier) = 0;
 
 protected:
     // IPC messages.

Modified: trunk/Source/WebCore/workers/shared/context/SharedWorkerContextManager.cpp (292859 => 292860)


--- trunk/Source/WebCore/workers/shared/context/SharedWorkerContextManager.cpp	2022-04-14 06:19:01 UTC (rev 292859)
+++ trunk/Source/WebCore/workers/shared/context/SharedWorkerContextManager.cpp	2022-04-14 07:16:17 UTC (rev 292860)
@@ -64,6 +64,22 @@
     });
 }
 
+void SharedWorkerContextManager::suspendSharedWorker(SharedWorkerIdentifier sharedWorkerIdentifier)
+{
+    auto* worker = m_workerMap.get(sharedWorkerIdentifier);
+    RELEASE_LOG(SharedWorker, "SharedWorkerContextManager::suspendSharedWorker: sharedWorkerIdentifier=%" PRIu64 ", worker=%p", sharedWorkerIdentifier.toUInt64(), worker);
+    if (worker)
+        worker->thread().suspend();
+}
+
+void SharedWorkerContextManager::resumeSharedWorker(SharedWorkerIdentifier sharedWorkerIdentifier)
+{
+    auto* worker = m_workerMap.get(sharedWorkerIdentifier);
+    RELEASE_LOG(SharedWorker, "SharedWorkerContextManager::resumeSharedWorker: sharedWorkerIdentifier=%" PRIu64 ", worker=%p", sharedWorkerIdentifier.toUInt64(), worker);
+    if (worker)
+        worker->thread().resume();
+}
+
 void SharedWorkerContextManager::stopAllSharedWorkers()
 {
     while (!m_workerMap.isEmpty())
@@ -113,6 +129,16 @@
     SharedWorkerContextManager::singleton().stopSharedWorker(sharedWorkerIdentifier);
 }
 
+void SharedWorkerContextManager::Connection::suspendSharedWorker(SharedWorkerIdentifier identifier)
+{
+    SharedWorkerContextManager::singleton().suspendSharedWorker(identifier);
+}
+
+void SharedWorkerContextManager::Connection::resumeSharedWorker(SharedWorkerIdentifier identifier)
+{
+    SharedWorkerContextManager::singleton().resumeSharedWorker(identifier);
+}
+
 void SharedWorkerContextManager::forEachSharedWorker(const Function<Function<void(ScriptExecutionContext&)>()>& createTask)
 {
     for (auto& worker : m_workerMap.values())

Modified: trunk/Source/WebCore/workers/shared/context/SharedWorkerContextManager.h (292859 => 292860)


--- trunk/Source/WebCore/workers/shared/context/SharedWorkerContextManager.h	2022-04-14 06:19:01 UTC (rev 292859)
+++ trunk/Source/WebCore/workers/shared/context/SharedWorkerContextManager.h	2022-04-14 07:16:17 UTC (rev 292860)
@@ -40,6 +40,8 @@
 
     SharedWorkerThreadProxy* sharedWorker(SharedWorkerIdentifier) const;
     void stopSharedWorker(SharedWorkerIdentifier);
+    void suspendSharedWorker(SharedWorkerIdentifier);
+    void resumeSharedWorker(SharedWorkerIdentifier);
     WEBCORE_EXPORT void stopAllSharedWorkers();
 
     class Connection {
@@ -56,6 +58,8 @@
         // IPC message handlers.
         WEBCORE_EXPORT void postConnectEvent(SharedWorkerIdentifier, TransferredMessagePort&&, String&& sourceOrigin);
         WEBCORE_EXPORT void terminateSharedWorker(SharedWorkerIdentifier);
+        WEBCORE_EXPORT void suspendSharedWorker(SharedWorkerIdentifier);
+        WEBCORE_EXPORT void resumeSharedWorker(SharedWorkerIdentifier);
 
     private:
         bool m_isClosed { false };

Modified: trunk/Source/WebKit/ChangeLog (292859 => 292860)


--- trunk/Source/WebKit/ChangeLog	2022-04-14 06:19:01 UTC (rev 292859)
+++ trunk/Source/WebKit/ChangeLog	2022-04-14 07:16:17 UTC (rev 292860)
@@ -1,3 +1,29 @@
+2022-04-14  Youenn Fablet  <[email protected]>
+
+        A shared worker in a cached page should not allow the remote shared worker to continue executing
+        https://bugs.webkit.org/show_bug.cgi?id=239286
+
+        Reviewed by Chris Dumez.
+
+        Store whether shared worker objects are suspended or resumed.
+        If all objects are suspended, suspend the remote shared worker.
+        Otherwise, keep the remote shared worker live.
+        Add plumbing code to make this happen.
+
+        * NetworkProcess/ServiceWorker/WebSWServerConnection.cpp:
+        * NetworkProcess/SharedWorker/WebSharedWorker.cpp:
+        * NetworkProcess/SharedWorker/WebSharedWorker.h:
+        * NetworkProcess/SharedWorker/WebSharedWorkerServer.cpp:
+        * NetworkProcess/SharedWorker/WebSharedWorkerServer.h:
+        * NetworkProcess/SharedWorker/WebSharedWorkerServerConnection.cpp:
+        * NetworkProcess/SharedWorker/WebSharedWorkerServerConnection.h:
+        * NetworkProcess/SharedWorker/WebSharedWorkerServerConnection.messages.in:
+        * NetworkProcess/SharedWorker/WebSharedWorkerServerToContextConnection.cpp:
+        * NetworkProcess/SharedWorker/WebSharedWorkerServerToContextConnection.h:
+        * WebProcess/Storage/WebSharedWorkerContextManagerConnection.messages.in:
+        * WebProcess/Storage/WebSharedWorkerObjectConnection.cpp:
+        * WebProcess/Storage/WebSharedWorkerObjectConnection.h:
+
 2022-04-13  Chris Dumez  <[email protected]>
 
         Replace calls to substring(0, x) with the more concise left(x)

Modified: trunk/Source/WebKit/NetworkProcess/SharedWorker/WebSharedWorker.cpp (292859 => 292860)


--- trunk/Source/WebKit/NetworkProcess/SharedWorker/WebSharedWorker.cpp	2022-04-14 06:19:01 UTC (rev 292859)
+++ trunk/Source/WebKit/NetworkProcess/SharedWorker/WebSharedWorker.cpp	2022-04-14 07:16:17 UTC (rev 292860)
@@ -80,7 +80,7 @@
 
 void WebSharedWorker::addSharedWorkerObject(WebCore::SharedWorkerObjectIdentifier sharedWorkerObjectIdentifier, const WebCore::TransferredMessagePort& port)
 {
-    m_sharedWorkerObjects.add(sharedWorkerObjectIdentifier, port);
+    m_sharedWorkerObjects.add(sharedWorkerObjectIdentifier, SharedWorkerObjectState { false, port });
     if (auto* connection = contextConnection())
         connection->addSharedWorkerObject(sharedWorkerObjectIdentifier);
 }
@@ -92,10 +92,46 @@
         connection->removeSharedWorkerObject(sharedWorkerObjectIdentifier);
 }
 
+void WebSharedWorker::suspend(WebCore::SharedWorkerObjectIdentifier sharedWorkerObjectIdentifier)
+{
+    auto iterator = m_sharedWorkerObjects.find(sharedWorkerObjectIdentifier);
+    if (iterator == m_sharedWorkerObjects.end())
+        return;
+
+    iterator->value.isSuspended = true;
+    ASSERT(!m_isSuspended);
+    if (m_isSuspended)
+        return;
+
+    for (auto& state : m_sharedWorkerObjects.values()) {
+        if (!state.isSuspended)
+            return;
+    }
+
+    m_isSuspended = true;
+    if (auto* connection = contextConnection())
+        connection->suspendSharedWorker(identifier());
+}
+
+void WebSharedWorker::resume(WebCore::SharedWorkerObjectIdentifier sharedWorkerObjectIdentifier)
+{
+    auto iterator = m_sharedWorkerObjects.find(sharedWorkerObjectIdentifier);
+    if (iterator == m_sharedWorkerObjects.end())
+        return;
+
+    iterator->value.isSuspended = false;
+    if (!m_isSuspended)
+        return;
+
+    m_isSuspended = false;
+    if (auto* connection = contextConnection())
+        connection->resumeSharedWorker(identifier());
+}
+
 void WebSharedWorker::forEachSharedWorkerObject(const Function<void(WebCore::SharedWorkerObjectIdentifier, const WebCore::TransferredMessagePort&)>& apply) const
 {
-    for (auto& [sharedWorkerObjectIdentifier, port] : m_sharedWorkerObjects)
-        apply(sharedWorkerObjectIdentifier, port);
+    for (auto& [sharedWorkerObjectIdentifier, state] : m_sharedWorkerObjects)
+        apply(sharedWorkerObjectIdentifier, state.port);
 }
 
 std::optional<WebCore::ProcessIdentifier> WebSharedWorker::firstSharedWorkerObjectProcess() const

Modified: trunk/Source/WebKit/NetworkProcess/SharedWorker/WebSharedWorker.h (292859 => 292860)


--- trunk/Source/WebKit/NetworkProcess/SharedWorker/WebSharedWorker.h	2022-04-14 06:19:01 UTC (rev 292859)
+++ trunk/Source/WebKit/NetworkProcess/SharedWorker/WebSharedWorker.h	2022-04-14 07:16:17 UTC (rev 292860)
@@ -60,6 +60,8 @@
 
     void addSharedWorkerObject(WebCore::SharedWorkerObjectIdentifier, const WebCore::TransferredMessagePort&);
     void removeSharedWorkerObject(WebCore::SharedWorkerObjectIdentifier);
+    void suspend(WebCore::SharedWorkerObjectIdentifier);
+    void resume(WebCore::SharedWorkerObjectIdentifier);
     unsigned sharedWorkerObjectsCount() const { return m_sharedWorkerObjects.size(); }
     void forEachSharedWorkerObject(const Function<void(WebCore::SharedWorkerObjectIdentifier, const WebCore::TransferredMessagePort&)>&) const;
     std::optional<WebCore::ProcessIdentifier> firstSharedWorkerObjectProcess() const;
@@ -79,13 +81,19 @@
     WebSharedWorker(const WebSharedWorker&) = delete;
     WebSharedWorker& operator=(const WebSharedWorker&) = delete;
 
+    struct SharedWorkerObjectState {
+        bool isSuspended { false };
+        WebCore::TransferredMessagePort port;
+    };
+
     WebSharedWorkerServer& m_server;
     WebCore::SharedWorkerIdentifier m_identifier;
     WebCore::SharedWorkerKey m_key;
     WebCore::WorkerOptions m_workerOptions;
-    HashMap<WebCore::SharedWorkerObjectIdentifier, WebCore::TransferredMessagePort> m_sharedWorkerObjects;
+    HashMap<WebCore::SharedWorkerObjectIdentifier, SharedWorkerObjectState> m_sharedWorkerObjects;
     WebCore::WorkerFetchResult m_fetchResult;
     bool m_isRunning { false };
+    bool m_isSuspended { false };
 };
 
 } // namespace WebKit

Modified: trunk/Source/WebKit/NetworkProcess/SharedWorker/WebSharedWorkerServer.cpp (292859 => 292860)


--- trunk/Source/WebKit/NetworkProcess/SharedWorker/WebSharedWorkerServer.cpp	2022-04-14 06:19:01 UTC (rev 292859)
+++ trunk/Source/WebKit/NetworkProcess/SharedWorker/WebSharedWorkerServer.cpp	2022-04-14 07:16:17 UTC (rev 292860)
@@ -205,6 +205,26 @@
     shutDownSharedWorker(sharedWorkerKey);
 }
 
+void WebSharedWorkerServer::suspendForBackForwardCache(const WebCore::SharedWorkerKey& sharedWorkerKey, WebCore::SharedWorkerObjectIdentifier sharedWorkerObjectIdentifier)
+{
+    auto* sharedWorker = m_sharedWorkers.get(sharedWorkerKey);
+    RELEASE_LOG(SharedWorker, "WebSharedWorkerServer::suspendForBackForwardCache: sharedWorkerObjectIdentifier=%{public}s, sharedWorker=%p", sharedWorkerObjectIdentifier.toString().utf8().data(), sharedWorker);
+    if (!sharedWorker)
+        return;
+
+    sharedWorker->suspend(sharedWorkerObjectIdentifier);
+}
+
+void WebSharedWorkerServer::resumeForBackForwardCache(const WebCore::SharedWorkerKey& sharedWorkerKey, WebCore::SharedWorkerObjectIdentifier sharedWorkerObjectIdentifier)
+{
+    auto* sharedWorker = m_sharedWorkers.get(sharedWorkerKey);
+    RELEASE_LOG(SharedWorker, "WebSharedWorkerServer::resumeForBackForwardCache: sharedWorkerObjectIdentifier=%{public}s, sharedWorker=%p", sharedWorkerObjectIdentifier.toString().utf8().data(), sharedWorker);
+    if (!sharedWorker)
+        return;
+
+    sharedWorker->resume(sharedWorkerObjectIdentifier);
+}
+
 void WebSharedWorkerServer::shutDownSharedWorker(const WebCore::SharedWorkerKey& key)
 {
     auto sharedWorker = m_sharedWorkers.take(key);

Modified: trunk/Source/WebKit/NetworkProcess/SharedWorker/WebSharedWorkerServer.h (292859 => 292860)


--- trunk/Source/WebKit/NetworkProcess/SharedWorker/WebSharedWorkerServer.h	2022-04-14 06:19:01 UTC (rev 292859)
+++ trunk/Source/WebKit/NetworkProcess/SharedWorker/WebSharedWorkerServer.h	2022-04-14 07:16:17 UTC (rev 292860)
@@ -63,6 +63,8 @@
 
     void requestSharedWorker(WebCore::SharedWorkerKey&&, WebCore::SharedWorkerObjectIdentifier, WebCore::TransferredMessagePort&&, WebCore::WorkerOptions&&);
     void sharedWorkerObjectIsGoingAway(const WebCore::SharedWorkerKey&, WebCore::SharedWorkerObjectIdentifier);
+    void suspendForBackForwardCache(const WebCore::SharedWorkerKey&, WebCore::SharedWorkerObjectIdentifier);
+    void resumeForBackForwardCache(const WebCore::SharedWorkerKey&, WebCore::SharedWorkerObjectIdentifier);
     void postExceptionToWorkerObject(WebCore::SharedWorkerIdentifier, const String& errorMessage, int lineNumber, int columnNumber, const String& sourceURL);
 
     void terminateContextConnectionWhenPossible(const WebCore::RegistrableDomain&, WebCore::ProcessIdentifier);

Modified: trunk/Source/WebKit/NetworkProcess/SharedWorker/WebSharedWorkerServerConnection.cpp (292859 => 292860)


--- trunk/Source/WebKit/NetworkProcess/SharedWorker/WebSharedWorkerServerConnection.cpp	2022-04-14 06:19:01 UTC (rev 292859)
+++ trunk/Source/WebKit/NetworkProcess/SharedWorker/WebSharedWorkerServerConnection.cpp	2022-04-14 07:16:17 UTC (rev 292860)
@@ -98,6 +98,22 @@
         session->ensureSharedWorkerServer().sharedWorkerObjectIsGoingAway(sharedWorkerKey, sharedWorkerObjectIdentifier);
 }
 
+void WebSharedWorkerServerConnection::suspendForBackForwardCache(WebCore::SharedWorkerKey&& sharedWorkerKey, WebCore::SharedWorkerObjectIdentifier sharedWorkerObjectIdentifier)
+{
+    CONNECTION_MESSAGE_CHECK(sharedWorkerObjectIdentifier.processIdentifier() == m_webProcessIdentifier);
+    CONNECTION_RELEASE_LOG("suspendForBackForwardCache: sharedWorkerObjectIdentifier=%{public}s", sharedWorkerObjectIdentifier.toString().utf8().data());
+    if (auto* session = this->session())
+        session->ensureSharedWorkerServer().suspendForBackForwardCache(sharedWorkerKey, sharedWorkerObjectIdentifier);
+}
+
+void WebSharedWorkerServerConnection::resumeForBackForwardCache(WebCore::SharedWorkerKey&& sharedWorkerKey, WebCore::SharedWorkerObjectIdentifier sharedWorkerObjectIdentifier)
+{
+    CONNECTION_MESSAGE_CHECK(sharedWorkerObjectIdentifier.processIdentifier() == m_webProcessIdentifier);
+    CONNECTION_RELEASE_LOG("resumeForBackForwardCache: sharedWorkerObjectIdentifier=%{public}s", sharedWorkerObjectIdentifier.toString().utf8().data());
+    if (auto* session = this->session())
+        session->ensureSharedWorkerServer().resumeForBackForwardCache(sharedWorkerKey, sharedWorkerObjectIdentifier);
+}
+
 void WebSharedWorkerServerConnection::fetchScriptInClient(const WebSharedWorker& sharedWorker, WebCore::SharedWorkerObjectIdentifier sharedWorkerObjectIdentifier, CompletionHandler<void(WebCore::WorkerFetchResult&&)>&& completionHandler)
 {
     CONNECTION_RELEASE_LOG("fetchScriptInClient: sharedWorkerObjectIdentifier=%{public}s", sharedWorkerObjectIdentifier.toString().utf8().data());

Modified: trunk/Source/WebKit/NetworkProcess/SharedWorker/WebSharedWorkerServerConnection.h (292859 => 292860)


--- trunk/Source/WebKit/NetworkProcess/SharedWorker/WebSharedWorkerServerConnection.h	2022-04-14 06:19:01 UTC (rev 292859)
+++ trunk/Source/WebKit/NetworkProcess/SharedWorker/WebSharedWorkerServerConnection.h	2022-04-14 07:16:17 UTC (rev 292860)
@@ -75,6 +75,8 @@
     // IPC messages.
     void requestSharedWorker(WebCore::SharedWorkerKey&&, WebCore::SharedWorkerObjectIdentifier, WebCore::TransferredMessagePort&&, WebCore::WorkerOptions&&);
     void sharedWorkerObjectIsGoingAway(WebCore::SharedWorkerKey&&, WebCore::SharedWorkerObjectIdentifier);
+    void suspendForBackForwardCache(WebCore::SharedWorkerKey&&, WebCore::SharedWorkerObjectIdentifier);
+    void resumeForBackForwardCache(WebCore::SharedWorkerKey&&, WebCore::SharedWorkerObjectIdentifier);
 
     Ref<IPC::Connection> m_contentConnection;
     Ref<NetworkProcess> m_networkProcess;

Modified: trunk/Source/WebKit/NetworkProcess/SharedWorker/WebSharedWorkerServerConnection.messages.in (292859 => 292860)


--- trunk/Source/WebKit/NetworkProcess/SharedWorker/WebSharedWorkerServerConnection.messages.in	2022-04-14 06:19:01 UTC (rev 292859)
+++ trunk/Source/WebKit/NetworkProcess/SharedWorker/WebSharedWorkerServerConnection.messages.in	2022-04-14 07:16:17 UTC (rev 292860)
@@ -23,4 +23,6 @@
 messages -> WebSharedWorkerServerConnection NotRefCounted {
     RequestSharedWorker(struct WebCore::SharedWorkerKey sharedWorkerKey, WebCore::SharedWorkerObjectIdentifier sharedWorkerObjectIdentifier, WebCore::TransferredMessagePort port, struct WebCore::WorkerOptions workerOptions)
     SharedWorkerObjectIsGoingAway(struct WebCore::SharedWorkerKey sharedWorkerKey, WebCore::SharedWorkerObjectIdentifier sharedWorkerObjectIdentifier)
+    SuspendForBackForwardCache(struct WebCore::SharedWorkerKey sharedWorkerKey, WebCore::SharedWorkerObjectIdentifier sharedWorkerObjectIdentifier)
+    ResumeForBackForwardCache(struct WebCore::SharedWorkerKey sharedWorkerKey, WebCore::SharedWorkerObjectIdentifier sharedWorkerObjectIdentifier)
 }

Modified: trunk/Source/WebKit/NetworkProcess/SharedWorker/WebSharedWorkerServerToContextConnection.cpp (292859 => 292860)


--- trunk/Source/WebKit/NetworkProcess/SharedWorker/WebSharedWorkerServerToContextConnection.cpp	2022-04-14 06:19:01 UTC (rev 292859)
+++ trunk/Source/WebKit/NetworkProcess/SharedWorker/WebSharedWorkerServerToContextConnection.cpp	2022-04-14 07:16:17 UTC (rev 292860)
@@ -105,6 +105,16 @@
     });
 }
 
+void WebSharedWorkerServerToContextConnection::suspendSharedWorker(WebCore::SharedWorkerIdentifier identifier)
+{
+    send(Messages::WebSharedWorkerContextManagerConnection::SuspendSharedWorker { identifier });
+}
+
+void WebSharedWorkerServerToContextConnection::resumeSharedWorker(WebCore::SharedWorkerIdentifier identifier)
+{
+    send(Messages::WebSharedWorkerContextManagerConnection::ResumeSharedWorker { identifier });
+}
+
 void WebSharedWorkerServerToContextConnection::postConnectEvent(const WebSharedWorker& sharedWorker, const WebCore::TransferredMessagePort& port)
 {
     CONTEXT_CONNECTION_RELEASE_LOG("postConnectEvent: sharedWorkerIdentifier=%" PRIu64, sharedWorker.identifier().toUInt64());

Modified: trunk/Source/WebKit/NetworkProcess/SharedWorker/WebSharedWorkerServerToContextConnection.h (292859 => 292860)


--- trunk/Source/WebKit/NetworkProcess/SharedWorker/WebSharedWorkerServerToContextConnection.h	2022-04-14 06:19:01 UTC (rev 292859)
+++ trunk/Source/WebKit/NetworkProcess/SharedWorker/WebSharedWorkerServerToContextConnection.h	2022-04-14 07:16:17 UTC (rev 292860)
@@ -64,6 +64,9 @@
     void postConnectEvent(const WebSharedWorker&, const WebCore::TransferredMessagePort&);
     void terminateSharedWorker(const WebSharedWorker&);
 
+    void suspendSharedWorker(WebCore::SharedWorkerIdentifier);
+    void resumeSharedWorker(WebCore::SharedWorkerIdentifier);
+
     const HashMap<WebCore::ProcessIdentifier, HashSet<WebCore::SharedWorkerObjectIdentifier>>& sharedWorkerObjects() const { return m_sharedWorkerObjects; }
 
     void didReceiveMessage(IPC::Connection&, IPC::Decoder&) final;

Modified: trunk/Source/WebKit/WebProcess/Storage/WebSharedWorkerContextManagerConnection.messages.in (292859 => 292860)


--- trunk/Source/WebKit/WebProcess/Storage/WebSharedWorkerContextManagerConnection.messages.in	2022-04-14 06:19:01 UTC (rev 292859)
+++ trunk/Source/WebKit/WebProcess/Storage/WebSharedWorkerContextManagerConnection.messages.in	2022-04-14 07:16:17 UTC (rev 292860)
@@ -27,4 +27,6 @@
     UpdatePreferencesStore(struct WebKit::WebPreferencesStore store)
     SetUserAgent(String userAgent)
     Close()
+    SuspendSharedWorker(WebCore::SharedWorkerIdentifier sharedWorkerIdentifier)
+    ResumeSharedWorker(WebCore::SharedWorkerIdentifier sharedWorkerIdentifier)
 }

Modified: trunk/Source/WebKit/WebProcess/Storage/WebSharedWorkerObjectConnection.cpp (292859 => 292860)


--- trunk/Source/WebKit/WebProcess/Storage/WebSharedWorkerObjectConnection.cpp	2022-04-14 06:19:01 UTC (rev 292859)
+++ trunk/Source/WebKit/WebProcess/Storage/WebSharedWorkerObjectConnection.cpp	2022-04-14 07:16:17 UTC (rev 292860)
@@ -65,6 +65,18 @@
     send(Messages::WebSharedWorkerServerConnection::SharedWorkerObjectIsGoingAway { sharedWorkerKey, sharedWorkerObjectIdentifier });
 }
 
+void WebSharedWorkerObjectConnection::suspendForBackForwardCache(const WebCore::SharedWorkerKey& sharedWorkerKey, WebCore::SharedWorkerObjectIdentifier sharedWorkerObjectIdentifier)
+{
+    CONNECTION_RELEASE_LOG("suspendForBackForwardCache: sharedWorkerObjectIdentifier=%{public}s", sharedWorkerObjectIdentifier.toString().utf8().data());
+    send(Messages::WebSharedWorkerServerConnection::SuspendForBackForwardCache { sharedWorkerKey, sharedWorkerObjectIdentifier });
+}
+
+void WebSharedWorkerObjectConnection::resumeForBackForwardCache(const WebCore::SharedWorkerKey& sharedWorkerKey, WebCore::SharedWorkerObjectIdentifier sharedWorkerObjectIdentifier)
+{
+    CONNECTION_RELEASE_LOG("resumeForBackForwardCache: sharedWorkerObjectIdentifier=%{public}s", sharedWorkerObjectIdentifier.toString().utf8().data());
+    send(Messages::WebSharedWorkerServerConnection::ResumeForBackForwardCache { sharedWorkerKey, sharedWorkerObjectIdentifier });
+}
+
 #undef CONNECTION_RELEASE_LOG
 
 } // namespace WebKit

Modified: trunk/Source/WebKit/WebProcess/Storage/WebSharedWorkerObjectConnection.h (292859 => 292860)


--- trunk/Source/WebKit/WebProcess/Storage/WebSharedWorkerObjectConnection.h	2022-04-14 06:19:01 UTC (rev 292859)
+++ trunk/Source/WebKit/WebProcess/Storage/WebSharedWorkerObjectConnection.h	2022-04-14 07:16:17 UTC (rev 292860)
@@ -44,6 +44,8 @@
     // WebCore::SharedWorkerObjectConnection.
     void requestSharedWorker(const WebCore::SharedWorkerKey&, WebCore::SharedWorkerObjectIdentifier, WebCore::TransferredMessagePort&&, const WebCore::WorkerOptions&) final;
     void sharedWorkerObjectIsGoingAway(const WebCore::SharedWorkerKey&, WebCore::SharedWorkerObjectIdentifier) final;
+    void suspendForBackForwardCache(const WebCore::SharedWorkerKey&, WebCore::SharedWorkerObjectIdentifier) final;
+    void resumeForBackForwardCache(const WebCore::SharedWorkerKey&, WebCore::SharedWorkerObjectIdentifier) final;
 
     // IPC::MessageSender.
     IPC::Connection* messageSenderConnection() const final;
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to