- Revision
- 238683
- Author
- [email protected]
- Date
- 2018-11-29 12:10:53 -0800 (Thu, 29 Nov 2018)
Log Message
Updating a service worker during a navigation load sometimes makes the load fail
https://bugs.webkit.org/show_bug.cgi?id=191986
<rdar://problem/46259790>
Reviewed by Chris Dumez.
LayoutTests/imported/w3c:
* web-platform-tests/service-workers/service-worker/resources/trickle.py: Added.
(main):
* web-platform-tests/service-workers/service-worker/resources/update-fetch-worker.py: Added.
(main):
* web-platform-tests/service-workers/service-worker/update-on-navigation.https-expected.txt: Added.
* web-platform-tests/service-workers/service-worker/update-on-navigation.https.html: Added.
Source/WebCore:
Previously, we were registering a document as service worker client at creation of the document.
According the service worker spec, this should be done when handling the fetch event of the corresponding navigation load.
This ensures that the service worker will have a client and will not get updated in the middle of the navigation load.
At navigation load start, we do not have a document yet since it is created when receiving the first bytes.
Instead, we register a temporary document when starting the navigation load and unregister it when the real document is registered.
Test: imported/w3c/web-platform-tests/service-workers/service-worker/update-on-navigation.https.html
* loader/DocumentLoader.cpp:
(WebCore::DocumentLoader::commitData):
(WebCore::DocumentLoader::registerTemporaryServiceWorkerClient):
(WebCore::DocumentLoader::unregisterTemporaryServiceWorkerClient):
(WebCore::DocumentLoader::loadMainResource):
(WebCore::DocumentLoader::clearMainResource):
* loader/DocumentLoader.h:
Modified Paths
Added Paths
Diff
Modified: trunk/LayoutTests/imported/w3c/ChangeLog (238682 => 238683)
--- trunk/LayoutTests/imported/w3c/ChangeLog 2018-11-29 20:09:08 UTC (rev 238682)
+++ trunk/LayoutTests/imported/w3c/ChangeLog 2018-11-29 20:10:53 UTC (rev 238683)
@@ -1,3 +1,18 @@
+2018-11-29 Youenn Fablet <[email protected]>
+
+ Updating a service worker during a navigation load sometimes makes the load fail
+ https://bugs.webkit.org/show_bug.cgi?id=191986
+ <rdar://problem/46259790>
+
+ Reviewed by Chris Dumez.
+
+ * web-platform-tests/service-workers/service-worker/resources/trickle.py: Added.
+ (main):
+ * web-platform-tests/service-workers/service-worker/resources/update-fetch-worker.py: Added.
+ (main):
+ * web-platform-tests/service-workers/service-worker/update-on-navigation.https-expected.txt: Added.
+ * web-platform-tests/service-workers/service-worker/update-on-navigation.https.html: Added.
+
2018-11-29 Rob Buis <[email protected]>
Import fetch/api/cors tests
Added: trunk/LayoutTests/imported/w3c/web-platform-tests/service-workers/service-worker/resources/trickle.py (0 => 238683)
--- trunk/LayoutTests/imported/w3c/web-platform-tests/service-workers/service-worker/resources/trickle.py (rev 0)
+++ trunk/LayoutTests/imported/w3c/web-platform-tests/service-workers/service-worker/resources/trickle.py 2018-11-29 20:10:53 UTC (rev 238683)
@@ -0,0 +1,14 @@
+import time
+
+def main(request, response):
+ delay = float(request.GET.first("ms", 500)) / 1E3
+ count = int(request.GET.first("count", 50))
+ # Read request body
+ request.body
+ time.sleep(delay)
+ response.headers.set("Content-type", "text/plain")
+ response.write_status_headers()
+ time.sleep(delay);
+ for i in xrange(count):
+ response.writer.write_content("TEST_TRICKLE\n")
+ time.sleep(delay)
Added: trunk/LayoutTests/imported/w3c/web-platform-tests/service-workers/service-worker/resources/update-fetch-worker.py (0 => 238683)
--- trunk/LayoutTests/imported/w3c/web-platform-tests/service-workers/service-worker/resources/update-fetch-worker.py (rev 0)
+++ trunk/LayoutTests/imported/w3c/web-platform-tests/service-workers/service-worker/resources/update-fetch-worker.py 2018-11-29 20:10:53 UTC (rev 238683)
@@ -0,0 +1,19 @@
+import time
+
+def main(request, response):
+ # no-cache itself to ensure the user agent finds a new version for each update.
+ headers = [('Cache-Control', 'no-cache, must-revalidate'),
+ ('Pragma', 'no-cache')]
+
+ content_type = ''
+ extra_body = ''
+
+ content_type = 'application/_javascript_'
+ headers.append(('Content-Type', content_type))
+
+ extra_body = "self._onfetch_ = (event) => { event.respondWith(fetch(event.request)); };"
+
+ # Return a different script for each access. Use .time() and .clock() for
+ # best time resolution across different platforms.
+ return headers, '/* %s %s */ %s' % (time.time(), time.clock(), extra_body)
+
Added: trunk/LayoutTests/imported/w3c/web-platform-tests/service-workers/service-worker/update-on-navigation.https-expected.txt (0 => 238683)
--- trunk/LayoutTests/imported/w3c/web-platform-tests/service-workers/service-worker/update-on-navigation.https-expected.txt (rev 0)
+++ trunk/LayoutTests/imported/w3c/web-platform-tests/service-workers/service-worker/update-on-navigation.https-expected.txt 2018-11-29 20:10:53 UTC (rev 238683)
@@ -0,0 +1,3 @@
+
+PASS The active service worker in charge of a navigation load should not be terminated as part of updating the registration
+
Added: trunk/LayoutTests/imported/w3c/web-platform-tests/service-workers/service-worker/update-on-navigation.https.html (0 => 238683)
--- trunk/LayoutTests/imported/w3c/web-platform-tests/service-workers/service-worker/update-on-navigation.https.html (rev 0)
+++ trunk/LayoutTests/imported/w3c/web-platform-tests/service-workers/service-worker/update-on-navigation.https.html 2018-11-29 20:10:53 UTC (rev 238683)
@@ -0,0 +1,20 @@
+<!DOCTYPE html>
+<title>Update on navigation</title>
+<script src=''></script>
+<script src=''></script>
+<script src=''></script>
+<script>
+promise_test(async (t) => {
+ var script = 'resources/update-fetch-worker.py';
+ var scope = 'resources/trickle.py?ms=1000&count=1';
+
+ const registration = await service_worker_unregister_and_register(t, script, scope);
+ t.add_cleanup(() => registration.unregister());
+
+ if (registration.installing)
+ await wait_for_state(t, registration.installing, 'activated');
+
+ const frame = await with_iframe(scope);
+ t.add_cleanup(() => frame.remove());
+}, 'The active service worker in charge of a navigation load should not be terminated as part of updating the registration');
+</script>
Modified: trunk/Source/WebCore/ChangeLog (238682 => 238683)
--- trunk/Source/WebCore/ChangeLog 2018-11-29 20:09:08 UTC (rev 238682)
+++ trunk/Source/WebCore/ChangeLog 2018-11-29 20:10:53 UTC (rev 238683)
@@ -1,3 +1,28 @@
+2018-11-29 Youenn Fablet <[email protected]>
+
+ Updating a service worker during a navigation load sometimes makes the load fail
+ https://bugs.webkit.org/show_bug.cgi?id=191986
+ <rdar://problem/46259790>
+
+ Reviewed by Chris Dumez.
+
+ Previously, we were registering a document as service worker client at creation of the document.
+ According the service worker spec, this should be done when handling the fetch event of the corresponding navigation load.
+ This ensures that the service worker will have a client and will not get updated in the middle of the navigation load.
+
+ At navigation load start, we do not have a document yet since it is created when receiving the first bytes.
+ Instead, we register a temporary document when starting the navigation load and unregister it when the real document is registered.
+
+ Test: imported/w3c/web-platform-tests/service-workers/service-worker/update-on-navigation.https.html
+
+ * loader/DocumentLoader.cpp:
+ (WebCore::DocumentLoader::commitData):
+ (WebCore::DocumentLoader::registerTemporaryServiceWorkerClient):
+ (WebCore::DocumentLoader::unregisterTemporaryServiceWorkerClient):
+ (WebCore::DocumentLoader::loadMainResource):
+ (WebCore::DocumentLoader::clearMainResource):
+ * loader/DocumentLoader.h:
+
2018-11-29 Ryan Haddad <[email protected]>
Unreviewed, rolling out r238678.
Modified: trunk/Source/WebCore/loader/DocumentLoader.cpp (238682 => 238683)
--- trunk/Source/WebCore/loader/DocumentLoader.cpp 2018-11-29 20:09:08 UTC (rev 238682)
+++ trunk/Source/WebCore/loader/DocumentLoader.cpp 2018-11-29 20:10:53 UTC (rev 238683)
@@ -76,6 +76,7 @@
#include "ScriptableDocumentParser.h"
#include "SecurityPolicy.h"
#include "ServiceWorker.h"
+#include "ServiceWorkerClientData.h"
#include "ServiceWorkerProvider.h"
#include "Settings.h"
#include "SubresourceLoader.h"
@@ -1032,8 +1033,6 @@
#endif
#if ENABLE(SERVICE_WORKER)
if (RuntimeEnabledFeatures::sharedFeatures().serviceWorkerEnabled()) {
- // FIXME: We should probably register the client as soon as we do the related navigation fetch.
- // We can probably implement this when supporting FetchEvent.reservedClientId.
if (m_serviceWorkerRegistrationData && m_serviceWorkerRegistrationData->activeWorker) {
m_frame->document()->setActiveServiceWorker(ServiceWorker::getOrCreate(*m_frame->document(), WTFMove(m_serviceWorkerRegistrationData->activeWorker.value())));
m_serviceWorkerRegistrationData = { };
@@ -1044,6 +1043,10 @@
if (m_frame->document()->activeServiceWorker() || SchemeRegistry::canServiceWorkersHandleURLScheme(m_frame->document()->url().protocol().toStringWithoutCopying()))
m_frame->document()->setServiceWorkerConnection(ServiceWorkerProvider::singleton().existingServiceWorkerConnectionForSession(m_frame->page()->sessionID()));
+
+ // We currently unregister the temporary service worker client since we now registered the real document.
+ // FIXME: We should make the real document use the temporary client identifier.
+ unregisterTemporaryServiceWorkerClient();
}
#endif
// Call receivedFirstData() exactly once per load. We should only reach this point multiple times
@@ -1759,6 +1762,44 @@
});
}
+void DocumentLoader::registerTemporaryServiceWorkerClient(const URL& url)
+{
+#if ENABLE(SERVICE_WORKER)
+ ASSERT(!m_temporaryServiceWorkerClient);
+
+ if (!m_serviceWorkerRegistrationData)
+ return;
+
+ m_temporaryServiceWorkerClient = TemporaryServiceWorkerClient {
+ generateObjectIdentifier<DocumentIdentifierType>(),
+ *ServiceWorkerProvider::singleton().existingServiceWorkerConnectionForSession(m_frame->page()->sessionID())
+ };
+
+ // FIXME: Compute ServiceWorkerClientFrameType appropriately.
+ ServiceWorkerClientData data { { m_temporaryServiceWorkerClient->serviceWorkerConnection->serverConnectionIdentifier(), m_temporaryServiceWorkerClient->documentIdentifier }, ServiceWorkerClientType::Window, ServiceWorkerClientFrameType::None, url };
+
+ RefPtr<SecurityOrigin> topOrigin;
+ if (m_frame->isMainFrame())
+ topOrigin = SecurityOrigin::create(url);
+ else
+ topOrigin = &m_frame->mainFrame().document()->topOrigin();
+ m_temporaryServiceWorkerClient->serviceWorkerConnection->registerServiceWorkerClient(*topOrigin, WTFMove(data), m_serviceWorkerRegistrationData->identifier);
+#else
+ UNUSED_PARAM(url);
+#endif
+}
+
+void DocumentLoader::unregisterTemporaryServiceWorkerClient()
+{
+#if ENABLE(SERVICE_WORKER)
+ if (!m_temporaryServiceWorkerClient)
+ return;
+
+ m_temporaryServiceWorkerClient->serviceWorkerConnection->unregisterServiceWorkerClient(m_temporaryServiceWorkerClient->documentIdentifier);
+ m_temporaryServiceWorkerClient = std::nullopt;
+#endif
+}
+
void DocumentLoader::loadMainResource(ResourceRequest&& request)
{
static NeverDestroyed<ResourceLoaderOptions> mainResourceLoadOptions(
@@ -1786,6 +1827,11 @@
#if ENABLE(SERVICE_WORKER)
mainResourceRequest.setNavigationServiceWorkerRegistrationData(m_serviceWorkerRegistrationData);
+ if (mainResourceRequest.options().serviceWorkersMode != ServiceWorkersMode::None) {
+ // As per step 12 of https://w3c.github.io/ServiceWorker/#on-fetch-request-algorithm, the active service worker should be controlling the document.
+ // Since we did not yet create the document, we register a temporary service worker client instead.
+ registerTemporaryServiceWorkerClient(mainResourceRequest.resourceRequest().url());
+ }
#endif
m_mainResource = m_cachedResourceLoader->requestMainResource(WTFMove(mainResourceRequest)).value_or(nullptr);
@@ -1883,6 +1929,8 @@
#endif
m_mainResource = nullptr;
+
+ unregisterTemporaryServiceWorkerClient();
}
void DocumentLoader::subresourceLoaderFinishedLoadingOnePart(ResourceLoader* loader)
Modified: trunk/Source/WebCore/loader/DocumentLoader.h (238682 => 238683)
--- trunk/Source/WebCore/loader/DocumentLoader.h 2018-11-29 20:09:08 UTC (rev 238682)
+++ trunk/Source/WebCore/loader/DocumentLoader.h 2018-11-29 20:10:53 UTC (rev 238683)
@@ -32,6 +32,7 @@
#include "CachedRawResourceClient.h"
#include "CachedResourceHandle.h"
#include "ContentSecurityPolicyClient.h"
+#include "DocumentIdentifier.h"
#include "DocumentWriter.h"
#include "FrameDestructionObserver.h"
#include "LinkIcon.h"
@@ -83,6 +84,7 @@
class PreviewConverter;
class ResourceLoader;
class SharedBuffer;
+class SWClientConnection;
class SubresourceLoader;
class SubstituteResource;
@@ -342,6 +344,8 @@
#if ENABLE(SERVICE_WORKER)
void matchRegistration(const URL&, CompletionHandler<void(std::optional<ServiceWorkerRegistrationData>&&)>&&);
#endif
+ void registerTemporaryServiceWorkerClient(const URL&);
+ void unregisterTemporaryServiceWorkerClient();
void loadMainResource(ResourceRequest&&);
@@ -535,6 +539,11 @@
#if ENABLE(SERVICE_WORKER)
std::optional<ServiceWorkerRegistrationData> m_serviceWorkerRegistrationData;
+ struct TemporaryServiceWorkerClient {
+ DocumentIdentifier documentIdentifier;
+ Ref<SWClientConnection> serviceWorkerConnection;
+ };
+ std::optional<TemporaryServiceWorkerClient> m_temporaryServiceWorkerClient;
#endif
#ifndef NDEBUG