<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[244097] trunk</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.webkit.org/projects/webkit/changeset/244097">244097</a></dd>
<dt>Author</dt> <dd>youenn@apple.com</dd>
<dt>Date</dt> <dd>2019-04-09 16:37:17 -0700 (Tue, 09 Apr 2019)</dd>
</dl>

<h3>Log Message</h3>
<pre>Close service worker database on network process suspension
https://bugs.webkit.org/show_bug.cgi?id=196623
<rdar://problem/48930869>

Reviewed by Alex Christensen.

Source/WebCore:

Add suspend/resume support to SWServer.
On suspend, close the service worker database and stop pushing for changes.
On resume, push changes if needed.

* workers/service/server/RegistrationDatabase.cpp:
(WebCore::RegistrationDatabase::close):
* workers/service/server/RegistrationDatabase.h:
(WebCore::RegistrationDatabase::isClosed const): Deleted.
* workers/service/server/RegistrationStore.cpp:
(WebCore::RegistrationStore::closeDatabase):
* workers/service/server/RegistrationStore.cpp:
(WebCore::RegistrationStore::pushChangesToDatabase):
(WebCore::RegistrationStore::clearAll):
(WebCore::RegistrationStore::startSuspension):
(WebCore::RegistrationStore::endSuspension):
* workers/service/server/RegistrationStore.h:
* workers/service/server/SWServer.cpp:
(WebCore::SWServer::startSuspension):
(WebCore::SWServer::endSuspension):
* workers/service/server/SWServer.h:

Source/WebKit:

Close service worker database when preparing to suspend.
On resume, push changes if any is needed.

* NetworkProcess/NetworkProcess.cpp:
(WebKit::NetworkProcess::actualPrepareToSuspend):
(WebKit::NetworkProcess::cancelPrepareToSuspend):
(WebKit::NetworkProcess::processDidResume):
* UIProcess/API/Cocoa/WKProcessPool.mm:
(-[WKProcessPool _sendNetworkProcessWillSuspendImminently]):
(-[WKProcessPool _sendNetworkProcessDidResume]):
* UIProcess/API/Cocoa/WKProcessPoolPrivate.h:
* UIProcess/Network/NetworkProcessProxy.h:
* UIProcess/WebProcessPool.cpp:
(WebKit::WebProcessPool::sendNetworkProcessWillSuspendImminently):
(WebKit::WebProcessPool::sendNetworkProcessDidResume):
* UIProcess/WebProcessPool.h:

Tools:

* TestWebKitAPI/Tests/WebKitCocoa/ServiceWorkerBasic.mm:</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceWebCoreChangeLog">trunk/Source/WebCore/ChangeLog</a></li>
<li><a href="#trunkSourceWebCoreworkersserviceserverRegistrationDatabasecpp">trunk/Source/WebCore/workers/service/server/RegistrationDatabase.cpp</a></li>
<li><a href="#trunkSourceWebCoreworkersserviceserverRegistrationDatabaseh">trunk/Source/WebCore/workers/service/server/RegistrationDatabase.h</a></li>
<li><a href="#trunkSourceWebCoreworkersserviceserverRegistrationStorecpp">trunk/Source/WebCore/workers/service/server/RegistrationStore.cpp</a></li>
<li><a href="#trunkSourceWebCoreworkersserviceserverRegistrationStoreh">trunk/Source/WebCore/workers/service/server/RegistrationStore.h</a></li>
<li><a href="#trunkSourceWebCoreworkersserviceserverSWServercpp">trunk/Source/WebCore/workers/service/server/SWServer.cpp</a></li>
<li><a href="#trunkSourceWebCoreworkersserviceserverSWServerh">trunk/Source/WebCore/workers/service/server/SWServer.h</a></li>
<li><a href="#trunkSourceWebKitChangeLog">trunk/Source/WebKit/ChangeLog</a></li>
<li><a href="#trunkSourceWebKitNetworkProcessNetworkProcesscpp">trunk/Source/WebKit/NetworkProcess/NetworkProcess.cpp</a></li>
<li><a href="#trunkSourceWebKitNetworkProcessNetworkProcessh">trunk/Source/WebKit/NetworkProcess/NetworkProcess.h</a></li>
<li><a href="#trunkSourceWebKitUIProcessAPICocoaWKProcessPoolmm">trunk/Source/WebKit/UIProcess/API/Cocoa/WKProcessPool.mm</a></li>
<li><a href="#trunkSourceWebKitUIProcessAPICocoaWKProcessPoolPrivateh">trunk/Source/WebKit/UIProcess/API/Cocoa/WKProcessPoolPrivate.h</a></li>
<li><a href="#trunkSourceWebKitUIProcessNetworkNetworkProcessProxyh">trunk/Source/WebKit/UIProcess/Network/NetworkProcessProxy.h</a></li>
<li><a href="#trunkSourceWebKitUIProcessWebProcessPoolcpp">trunk/Source/WebKit/UIProcess/WebProcessPool.cpp</a></li>
<li><a href="#trunkSourceWebKitUIProcessWebProcessPoolh">trunk/Source/WebKit/UIProcess/WebProcessPool.h</a></li>
<li><a href="#trunkToolsChangeLog">trunk/Tools/ChangeLog</a></li>
<li><a href="#trunkToolsTestWebKitAPITestsWebKitCocoaServiceWorkerBasicmm">trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/ServiceWorkerBasic.mm</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceWebCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/ChangeLog (244096 => 244097)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/ChangeLog   2019-04-09 21:45:03 UTC (rev 244096)
+++ trunk/Source/WebCore/ChangeLog      2019-04-09 23:37:17 UTC (rev 244097)
</span><span class="lines">@@ -1,3 +1,32 @@
</span><ins>+2019-04-09  Youenn Fablet  <youenn@apple.com>
+
+        Close service worker database on network process suspension
+        https://bugs.webkit.org/show_bug.cgi?id=196623
+        <rdar://problem/48930869>
+
+        Reviewed by Alex Christensen.
+
+        Add suspend/resume support to SWServer.
+        On suspend, close the service worker database and stop pushing for changes.
+        On resume, push changes if needed.
+
+        * workers/service/server/RegistrationDatabase.cpp:
+        (WebCore::RegistrationDatabase::close):
+        * workers/service/server/RegistrationDatabase.h:
+        (WebCore::RegistrationDatabase::isClosed const): Deleted.
+        * workers/service/server/RegistrationStore.cpp:
+        (WebCore::RegistrationStore::closeDatabase):
+        * workers/service/server/RegistrationStore.cpp:
+        (WebCore::RegistrationStore::pushChangesToDatabase):
+        (WebCore::RegistrationStore::clearAll):
+        (WebCore::RegistrationStore::startSuspension):
+        (WebCore::RegistrationStore::endSuspension):
+        * workers/service/server/RegistrationStore.h:
+        * workers/service/server/SWServer.cpp:
+        (WebCore::SWServer::startSuspension):
+        (WebCore::SWServer::endSuspension):
+        * workers/service/server/SWServer.h:
+
</ins><span class="cx"> 2019-04-09  Justin Fan  <justin_fan@apple.com>
</span><span class="cx"> 
</span><span class="cx">         [Web GPU] GPURenderPassEncoder updates: setBlendColor, setViewport, setScissorRect
</span></span></pre></div>
<a id="trunkSourceWebCoreworkersserviceserverRegistrationDatabasecpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/workers/service/server/RegistrationDatabase.cpp (244096 => 244097)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/workers/service/server/RegistrationDatabase.cpp     2019-04-09 21:45:03 UTC (rev 244096)
+++ trunk/Source/WebCore/workers/service/server/RegistrationDatabase.cpp        2019-04-09 23:37:17 UTC (rev 244097)
</span><span class="lines">@@ -118,6 +118,8 @@
</span><span class="cx"> 
</span><span class="cx"> void RegistrationDatabase::postTaskToWorkQueue(Function<void()>&& task)
</span><span class="cx"> {
</span><ins>+    ASSERT(isMainThread());
+
</ins><span class="cx">     m_workQueue->dispatch([protectedThis = makeRef(*this), task = WTFMove(task)]() mutable {
</span><span class="cx">         task();
</span><span class="cx">     });
</span><span class="lines">@@ -283,6 +285,14 @@
</span><span class="cx">     });
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+void RegistrationDatabase::close(CompletionHandler<void()>&& completionHandler)
+{
+    postTaskToWorkQueue([this, completionHandler = WTFMove(completionHandler)]() mutable {
+        m_database = nullptr;
+        callOnMainThread(WTFMove(completionHandler));
+    });
+}
+
</ins><span class="cx"> void RegistrationDatabase::clearAll(CompletionHandler<void()>&& completionHandler)
</span><span class="cx"> {
</span><span class="cx">     postTaskToWorkQueue([this, completionHandler = WTFMove(completionHandler)]() mutable {
</span></span></pre></div>
<a id="trunkSourceWebCoreworkersserviceserverRegistrationDatabaseh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/workers/service/server/RegistrationDatabase.h (244096 => 244097)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/workers/service/server/RegistrationDatabase.h       2019-04-09 21:45:03 UTC (rev 244096)
+++ trunk/Source/WebCore/workers/service/server/RegistrationDatabase.h  2019-04-09 23:37:17 UTC (rev 244097)
</span><span class="lines">@@ -52,10 +52,10 @@
</span><span class="cx"> 
</span><span class="cx">     ~RegistrationDatabase();
</span><span class="cx"> 
</span><del>-    bool isClosed() const { return !m_database; }
</del><span class="cx"> 
</span><span class="cx">     void pushChanges(Vector<ServiceWorkerContextData>&&, CompletionHandler<void()>&&);
</span><span class="cx">     void clearAll(CompletionHandler<void()>&&);
</span><ins>+    void close(CompletionHandler<void()>&&);
</ins><span class="cx"> 
</span><span class="cx"> private:
</span><span class="cx">     RegistrationDatabase(RegistrationStore&, String&& databaseDirectory);
</span></span></pre></div>
<a id="trunkSourceWebCoreworkersserviceserverRegistrationStorecpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/workers/service/server/RegistrationStore.cpp (244096 => 244097)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/workers/service/server/RegistrationStore.cpp        2019-04-09 21:45:03 UTC (rev 244096)
+++ trunk/Source/WebCore/workers/service/server/RegistrationStore.cpp   2019-04-09 23:37:17 UTC (rev 244097)
</span><span class="lines">@@ -52,6 +52,11 @@
</span><span class="cx"> 
</span><span class="cx"> void RegistrationStore::pushChangesToDatabase(WTF::CompletionHandler<void()>&& completionHandler)
</span><span class="cx"> {
</span><ins>+    if (m_isSuspended) {
+        m_needsPushingChanges = true;
+        return;
+    }
+
</ins><span class="cx">     Vector<ServiceWorkerContextData> changesToPush;
</span><span class="cx">     changesToPush.reserveInitialCapacity(m_updatedRegistrations.size());
</span><span class="cx">     for (auto& value : m_updatedRegistrations.values())
</span><span class="lines">@@ -63,6 +68,7 @@
</span><span class="cx"> 
</span><span class="cx"> void RegistrationStore::clearAll(WTF::CompletionHandler<void()>&& completionHandler)
</span><span class="cx"> {
</span><ins>+    m_needsPushingChanges = false;
</ins><span class="cx">     m_updatedRegistrations.clear();
</span><span class="cx">     m_databasePushTimer.stop();
</span><span class="cx">     m_database->clearAll(WTFMove(completionHandler));
</span><span class="lines">@@ -78,6 +84,19 @@
</span><span class="cx">     completionHandler();
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+void RegistrationStore::startSuspension(WTF::CompletionHandler<void()>&& completionHandler)
+{
+    m_isSuspended = true;
+    m_database->close(WTFMove(completionHandler));
+}
+
+void RegistrationStore::endSuspension()
+{
+    m_isSuspended = false;
+    if (m_needsPushingChanges)
+        scheduleDatabasePushIfNecessary();
+}
+
</ins><span class="cx"> void RegistrationStore::updateRegistration(const ServiceWorkerContextData& data)
</span><span class="cx"> {
</span><span class="cx">     m_updatedRegistrations.set(data.registration.key, data);
</span></span></pre></div>
<a id="trunkSourceWebCoreworkersserviceserverRegistrationStoreh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/workers/service/server/RegistrationStore.h (244096 => 244097)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/workers/service/server/RegistrationStore.h  2019-04-09 21:45:03 UTC (rev 244096)
+++ trunk/Source/WebCore/workers/service/server/RegistrationStore.h     2019-04-09 23:37:17 UTC (rev 244097)
</span><span class="lines">@@ -51,6 +51,9 @@
</span><span class="cx">     void clearAll(WTF::CompletionHandler<void()>&&);
</span><span class="cx">     void flushChanges(WTF::CompletionHandler<void()>&&);
</span><span class="cx"> 
</span><ins>+    void startSuspension(WTF::CompletionHandler<void()>&&);
+    void endSuspension();
+
</ins><span class="cx">     // Callbacks from the SWServer
</span><span class="cx">     void updateRegistration(const ServiceWorkerContextData&);
</span><span class="cx">     void removeRegistration(SWServerRegistration&);
</span><span class="lines">@@ -72,6 +75,9 @@
</span><span class="cx"> 
</span><span class="cx">     HashMap<ServiceWorkerRegistrationKey, ServiceWorkerContextData> m_updatedRegistrations;
</span><span class="cx">     Timer m_databasePushTimer;
</span><ins>+
+    bool m_isSuspended { false };
+    bool m_needsPushingChanges { false };
</ins><span class="cx"> };
</span><span class="cx"> 
</span><span class="cx"> } // namespace WebCore
</span></span></pre></div>
<a id="trunkSourceWebCoreworkersserviceserverSWServercpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/workers/service/server/SWServer.cpp (244096 => 244097)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/workers/service/server/SWServer.cpp 2019-04-09 21:45:03 UTC (rev 244096)
+++ trunk/Source/WebCore/workers/service/server/SWServer.cpp    2019-04-09 23:37:17 UTC (rev 244097)
</span><span class="lines">@@ -210,6 +210,18 @@
</span><span class="cx">         m_registrationStore->clearAll(WTFMove(completionHandler));
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+void SWServer::startSuspension(CompletionHandler<void()>&& completionHandler)
+{
+    if (m_registrationStore)
+        m_registrationStore->startSuspension(WTFMove(completionHandler));
+}
+
+void SWServer::endSuspension()
+{
+    if (m_registrationStore)
+        m_registrationStore->endSuspension();
+}
+
</ins><span class="cx"> void SWServer::clear(const SecurityOriginData& securityOrigin, CompletionHandler<void()>&& completionHandler)
</span><span class="cx"> {
</span><span class="cx">     if (!m_importCompleted) {
</span></span></pre></div>
<a id="trunkSourceWebCoreworkersserviceserverSWServerh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/workers/service/server/SWServer.h (244096 => 244097)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/workers/service/server/SWServer.h   2019-04-09 21:45:03 UTC (rev 244096)
+++ trunk/Source/WebCore/workers/service/server/SWServer.h      2019-04-09 23:37:17 UTC (rev 244097)
</span><span class="lines">@@ -122,6 +122,9 @@
</span><span class="cx">     WEBCORE_EXPORT void clearAll(WTF::CompletionHandler<void()>&&);
</span><span class="cx">     WEBCORE_EXPORT void clear(const SecurityOriginData&, WTF::CompletionHandler<void()>&&);
</span><span class="cx"> 
</span><ins>+    WEBCORE_EXPORT void startSuspension(CompletionHandler<void()>&&);
+    WEBCORE_EXPORT void endSuspension();
+
</ins><span class="cx">     SWServerRegistration* getRegistration(const ServiceWorkerRegistrationKey&);
</span><span class="cx">     void addRegistration(std::unique_ptr<SWServerRegistration>&&);
</span><span class="cx">     void removeRegistration(const ServiceWorkerRegistrationKey&);
</span></span></pre></div>
<a id="trunkSourceWebKitChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit/ChangeLog (244096 => 244097)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit/ChangeLog    2019-04-09 21:45:03 UTC (rev 244096)
+++ trunk/Source/WebKit/ChangeLog       2019-04-09 23:37:17 UTC (rev 244097)
</span><span class="lines">@@ -1,3 +1,28 @@
</span><ins>+2019-04-09  Youenn Fablet  <youenn@apple.com>
+
+        Close service worker database on network process suspension
+        https://bugs.webkit.org/show_bug.cgi?id=196623
+        <rdar://problem/48930869>
+
+        Reviewed by Alex Christensen.
+
+        Close service worker database when preparing to suspend.
+        On resume, push changes if any is needed.
+
+        * NetworkProcess/NetworkProcess.cpp:
+        (WebKit::NetworkProcess::actualPrepareToSuspend):
+        (WebKit::NetworkProcess::cancelPrepareToSuspend):
+        (WebKit::NetworkProcess::processDidResume):
+        * UIProcess/API/Cocoa/WKProcessPool.mm:
+        (-[WKProcessPool _sendNetworkProcessWillSuspendImminently]):
+        (-[WKProcessPool _sendNetworkProcessDidResume]):
+        * UIProcess/API/Cocoa/WKProcessPoolPrivate.h:
+        * UIProcess/Network/NetworkProcessProxy.h:
+        * UIProcess/WebProcessPool.cpp:
+        (WebKit::WebProcessPool::sendNetworkProcessWillSuspendImminently):
+        (WebKit::WebProcessPool::sendNetworkProcessDidResume):
+        * UIProcess/WebProcessPool.h:
+
</ins><span class="cx"> 2019-04-09  Daniel Bates  <dabates@apple.com>
</span><span class="cx"> 
</span><span class="cx">         [iPad] Should open popover when the spacebar is pressed
</span></span></pre></div>
<a id="trunkSourceWebKitNetworkProcessNetworkProcesscpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit/NetworkProcess/NetworkProcess.cpp (244096 => 244097)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit/NetworkProcess/NetworkProcess.cpp    2019-04-09 21:45:03 UTC (rev 244096)
+++ trunk/Source/WebKit/NetworkProcess/NetworkProcess.cpp       2019-04-09 23:37:17 UTC (rev 244097)
</span><span class="lines">@@ -1983,6 +1983,9 @@
</span><span class="cx"> 
</span><span class="cx">     for (auto& connection : m_webProcessConnections)
</span><span class="cx">         connection->cleanupForSuspension([delayedTaskCounter] { });
</span><ins>+
+    for (auto& server : m_swServers.values())
+        server->startSuspension([delayedTaskCounter] { });
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> void NetworkProcess::processWillSuspendImminently(CompletionHandler<void(bool)>&& completionHandler)
</span><span class="lines">@@ -2004,9 +2007,7 @@
</span><span class="cx">     // message. And NetworkProcessProxy expects to receive either a NetworkProcessProxy::ProcessReadyToSuspend-
</span><span class="cx">     // or NetworkProcessProxy::DidCancelProcessSuspension- message, but not both.
</span><span class="cx">     RELEASE_LOG(ProcessSuspension, "%p - NetworkProcess::cancelPrepareToSuspend()", this);
</span><del>-    platformProcessDidResume();
-    for (auto& connection : m_webProcessConnections)
-        connection->endSuspension();
</del><ins>+    resume();
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> void NetworkProcess::applicationDidEnterBackground()
</span><span class="lines">@@ -2022,9 +2023,17 @@
</span><span class="cx"> void NetworkProcess::processDidResume()
</span><span class="cx"> {
</span><span class="cx">     RELEASE_LOG(ProcessSuspension, "%p - NetworkProcess::processDidResume()", this);
</span><ins>+    resume();
+}
+
+void NetworkProcess::resume()
+{
</ins><span class="cx">     platformProcessDidResume();
</span><span class="cx">     for (auto& connection : m_webProcessConnections)
</span><span class="cx">         connection->endSuspension();
</span><ins>+
+    for (auto& server : m_swServers.values())
+        server->endSuspension();
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> void NetworkProcess::prefetchDNS(const String& hostname)
</span></span></pre></div>
<a id="trunkSourceWebKitNetworkProcessNetworkProcessh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit/NetworkProcess/NetworkProcess.h (244096 => 244097)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit/NetworkProcess/NetworkProcess.h      2019-04-09 21:45:03 UTC (rev 244096)
+++ trunk/Source/WebKit/NetworkProcess/NetworkProcess.h 2019-04-09 23:37:17 UTC (rev 244097)
</span><span class="lines">@@ -181,6 +181,7 @@
</span><span class="cx">     void prepareToSuspend();
</span><span class="cx">     void cancelPrepareToSuspend();
</span><span class="cx">     void processDidResume();
</span><ins>+    void resume();
</ins><span class="cx"> 
</span><span class="cx">     // Diagnostic messages logging.
</span><span class="cx">     void logDiagnosticMessage(uint64_t webPageID, const String& message, const String& description, WebCore::ShouldSample);
</span></span></pre></div>
<a id="trunkSourceWebKitUIProcessAPICocoaWKProcessPoolmm"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit/UIProcess/API/Cocoa/WKProcessPool.mm (244096 => 244097)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit/UIProcess/API/Cocoa/WKProcessPool.mm 2019-04-09 21:45:03 UTC (rev 244096)
+++ trunk/Source/WebKit/UIProcess/API/Cocoa/WKProcessPool.mm    2019-04-09 23:37:17 UTC (rev 244097)
</span><span class="lines">@@ -425,6 +425,16 @@
</span><span class="cx">     _processPool->terminateNetworkProcess();
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+- (void)_sendNetworkProcessWillSuspendImminently
+{
+    _processPool->sendNetworkProcessWillSuspendImminently();
+}
+
+- (void)_sendNetworkProcessDidResume
+{
+    _processPool->sendNetworkProcessDidResume();
+}
+
</ins><span class="cx"> - (void)_terminateServiceWorkerProcesses
</span><span class="cx"> {
</span><span class="cx">     _processPool->terminateServiceWorkerProcesses();
</span></span></pre></div>
<a id="trunkSourceWebKitUIProcessAPICocoaWKProcessPoolPrivateh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit/UIProcess/API/Cocoa/WKProcessPoolPrivate.h (244096 => 244097)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit/UIProcess/API/Cocoa/WKProcessPoolPrivate.h   2019-04-09 21:45:03 UTC (rev 244096)
+++ trunk/Source/WebKit/UIProcess/API/Cocoa/WKProcessPoolPrivate.h      2019-04-09 23:37:17 UTC (rev 244097)
</span><span class="lines">@@ -83,7 +83,9 @@
</span><span class="cx"> - (_WKDownload *)_resumeDownloadFromData:(NSData *)resumeData path:(NSString *)path originatingWebView:(WKWebView *)webView WK_API_AVAILABLE(macos(WK_MAC_TBA), ios(WK_IOS_TBA));
</span><span class="cx"> 
</span><span class="cx"> // Test only. Should be called only while no web content processes are running.
</span><del>-- (void)_terminateNetworkProcess;
</del><ins>+- (void)_terminateNetworkProcess WK_API_AVAILABLE(macos(WK_MAC_TBA), ios(WK_IOS_TBA));
+- (void)_sendNetworkProcessWillSuspendImminently WK_API_AVAILABLE(macos(WK_MAC_TBA), ios(WK_IOS_TBA));
+- (void)_sendNetworkProcessDidResume WK_API_AVAILABLE(macos(WK_MAC_TBA), ios(WK_IOS_TBA));
</ins><span class="cx"> - (void)_terminateServiceWorkerProcesses WK_API_AVAILABLE(macos(10.14), ios(12.0));
</span><span class="cx"> - (void)_disableServiceWorkerProcessTerminationDelay WK_API_AVAILABLE(macos(10.14), ios(12.0));
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceWebKitUIProcessNetworkNetworkProcessProxyh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit/UIProcess/Network/NetworkProcessProxy.h (244096 => 244097)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit/UIProcess/Network/NetworkProcessProxy.h      2019-04-09 21:45:03 UTC (rev 244096)
+++ trunk/Source/WebKit/UIProcess/Network/NetworkProcessProxy.h 2019-04-09 23:37:17 UTC (rev 244097)
</span><span class="lines">@@ -183,6 +183,10 @@
</span><span class="cx">     void createSymLinkForFileUpgrade(const String& indexedDatabaseDirectory);
</span><span class="cx"> #endif
</span><span class="cx"> 
</span><ins>+    // ProcessThrottlerClient
+    void sendProcessWillSuspendImminently() final;
+    void sendProcessDidResume() final;
+
</ins><span class="cx"> private:
</span><span class="cx">     // AuxiliaryProcessProxy
</span><span class="cx">     void getLaunchOptions(ProcessLauncher::LaunchOptions&) override;
</span><span class="lines">@@ -193,11 +197,9 @@
</span><span class="cx">     void clearCallbackStates();
</span><span class="cx"> 
</span><span class="cx">     // ProcessThrottlerClient
</span><del>-    void sendProcessWillSuspendImminently() override;
-    void sendPrepareToSuspend() override;
-    void sendCancelPrepareToSuspend() override;
-    void sendProcessDidResume() override;
-    void didSetAssertionState(AssertionState) override;
</del><ins>+    void sendPrepareToSuspend() final;
+    void sendCancelPrepareToSuspend() final;
+    void didSetAssertionState(AssertionState) final;
</ins><span class="cx"> 
</span><span class="cx">     // IPC::Connection::Client
</span><span class="cx">     void didReceiveMessage(IPC::Connection&, IPC::Decoder&) override;
</span></span></pre></div>
<a id="trunkSourceWebKitUIProcessWebProcessPoolcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit/UIProcess/WebProcessPool.cpp (244096 => 244097)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit/UIProcess/WebProcessPool.cpp 2019-04-09 21:45:03 UTC (rev 244096)
+++ trunk/Source/WebKit/UIProcess/WebProcessPool.cpp    2019-04-09 23:37:17 UTC (rev 244097)
</span><span class="lines">@@ -1749,6 +1749,18 @@
</span><span class="cx">     m_didNetworkProcessCrash = true;
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+void WebProcessPool::sendNetworkProcessWillSuspendImminently()
+{
+    if (m_networkProcess)
+        m_networkProcess->sendProcessWillSuspendImminently();
+}
+
+void WebProcessPool::sendNetworkProcessDidResume()
+{
+    if (m_networkProcess)
+        m_networkProcess->sendProcessDidResume();
+}
+
</ins><span class="cx"> void WebProcessPool::terminateServiceWorkerProcesses()
</span><span class="cx"> {
</span><span class="cx"> #if ENABLE(SERVICE_WORKER)
</span></span></pre></div>
<a id="trunkSourceWebKitUIProcessWebProcessPoolh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit/UIProcess/WebProcessPool.h (244096 => 244097)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit/UIProcess/WebProcessPool.h   2019-04-09 21:45:03 UTC (rev 244096)
+++ trunk/Source/WebKit/UIProcess/WebProcessPool.h      2019-04-09 23:37:17 UTC (rev 244097)
</span><span class="lines">@@ -295,6 +295,8 @@
</span><span class="cx"> 
</span><span class="cx">     void clearCachedCredentials();
</span><span class="cx">     void terminateNetworkProcess();
</span><ins>+    void sendNetworkProcessWillSuspendImminently();
+    void sendNetworkProcessDidResume();
</ins><span class="cx">     void terminateServiceWorkerProcesses();
</span><span class="cx">     void disableServiceWorkerProcessTerminationDelay();
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkToolsChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Tools/ChangeLog (244096 => 244097)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Tools/ChangeLog    2019-04-09 21:45:03 UTC (rev 244096)
+++ trunk/Tools/ChangeLog       2019-04-09 23:37:17 UTC (rev 244097)
</span><span class="lines">@@ -1,3 +1,13 @@
</span><ins>+2019-04-09  Youenn Fablet  <youenn@apple.com>
+
+        Close service worker database on network process suspension
+        https://bugs.webkit.org/show_bug.cgi?id=196623
+        <rdar://problem/48930869>
+
+        Reviewed by Alex Christensen.
+
+        * TestWebKitAPI/Tests/WebKitCocoa/ServiceWorkerBasic.mm:
+
</ins><span class="cx"> 2019-04-09  Daniel Bates  <dabates@apple.com>
</span><span class="cx"> 
</span><span class="cx">         [iPad] Should open popover when the spacebar is pressed
</span></span></pre></div>
<a id="trunkToolsTestWebKitAPITestsWebKitCocoaServiceWorkerBasicmm"></a>
<div class="modfile"><h4>Modified: trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/ServiceWorkerBasic.mm (244096 => 244097)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/ServiceWorkerBasic.mm        2019-04-09 21:45:03 UTC (rev 244096)
+++ trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/ServiceWorkerBasic.mm   2019-04-09 23:37:17 UTC (rev 244097)
</span><span class="lines">@@ -1734,3 +1734,61 @@
</span><span class="cx">         done = false;
</span><span class="cx">     }
</span><span class="cx"> }
</span><ins>+
+TEST(ServiceWorkers, SuspendNetworkProcess)
+{
+    [WKWebsiteDataStore _allowWebsiteDataRecordsForAllOrigins];
+
+    // Start with a clean slate data store
+    [[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:[WKWebsiteDataStore allWebsiteDataTypes] modifiedSince:[NSDate distantPast] completionHandler:^() {
+        done = true;
+    }];
+    TestWebKitAPI::Util::run(&done);
+    done = false;
+
+    RetainPtr<WKWebViewConfiguration> configuration = adoptNS([[WKWebViewConfiguration alloc] init]);
+
+    RetainPtr<SWMessageHandler> messageHandler = adoptNS([[SWMessageHandler alloc] init]);
+    [[configuration userContentController] addScriptMessageHandler:messageHandler.get() name:@"sw"];
+
+    RetainPtr<SWSchemes> handler = adoptNS([[SWSchemes alloc] init]);
+    handler->resources.set("sw://host/main.html", ResourceInfo { @"text/html", mainBytes });
+    handler->resources.set("sw://host/sw.js", ResourceInfo { @"application/javascript", scriptBytes });
+    [configuration setURLSchemeHandler:handler.get() forURLScheme:@"SW"];
+
+    RetainPtr<WKWebView> webView = adoptNS([[WKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) configuration:configuration.get()]);
+    [webView.get().configuration.processPool _registerURLSchemeServiceWorkersCanHandle:@"sw"];
+
+    auto delegate = adoptNS([[TestSWAsyncNavigationDelegate alloc] init]);
+    [webView setNavigationDelegate:delegate.get()];
+    [webView setUIDelegate:delegate.get()];
+
+    done = false;
+
+    // Normal load to get SW registered.
+    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"sw://host/main.html"]];
+    [webView loadRequest:request];
+
+    TestWebKitAPI::Util::run(&done);
+    done = false;
+
+    auto store = [configuration websiteDataStore];
+    auto path = store._serviceWorkerRegistrationDirectory;
+
+    NSURL* directory = [NSURL fileURLWithPath:path isDirectory:YES];
+    NSURL *swDBPath = [directory URLByAppendingPathComponent:@"ServiceWorkerRegistrations-4.sqlite3"];
+
+    EXPECT_TRUE([[NSFileManager defaultManager] fileExistsAtPath:swDBPath.path]);
+
+    [ webView.get().configuration.processPool _sendNetworkProcessWillSuspendImminently];
+
+    EXPECT_TRUE([[NSFileManager defaultManager] fileExistsAtPath:swDBPath.path]);
+
+    [ webView.get().configuration.processPool _sendNetworkProcessDidResume];
+
+    [webView loadRequest:request];
+    TestWebKitAPI::Util::run(&done);
+    done = false;
+
+    EXPECT_TRUE([[NSFileManager defaultManager] fileExistsAtPath:swDBPath.path]);
+}
</ins></span></pre>
</div>
</div>

</body>
</html>