Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Do not route the navigation preload response body to the service work…
…er if we can avoid it https://bugs.webkit.org/show_bug.cgi?id=241122 rdar://problem/94141714 Patch by Youenn Fablet <youennf@gmail.com> on 2022-06-13 Reviewed by Chris Dumez. To further optimize navigation preload, we always start the preload as soon as possible. In addition to that, we no longer pipe the body through the service worker in the common case: - When the preload receives the response, it sends it to the service worker that will resolve the preload promise. - The service worker is then expected to synchronously provide it to the FetchEvent. - If the fetch event gets the preload response and the preload response has not been started to load in the service worker, the service worker instructs network process to let the preload directly go to the WebProcess. - The preload response body might be loaded in the service worker if the response is cloned or if the service worker does not instruct the fetch event to use the preload fast enough. We schedule one task for the body to actually be loaded in the service worker. - Set service worker navigation preload redirection fetchStart based on the navigation preloader start to continue passing LayoutTests/imported/w3c/web-platform-tests/service-workers/service-worker/navigation-preload/resource-timing.https.html. We store the preload response/error in WebServiceWorkerFetchTaskClient as the preload response might be received between the time the fetch event is received and the time the fetch event is dispatched. * LayoutTests/http/wpt/service-workers/navigation-optimization-worker.js: Added. (async doTest): * LayoutTests/http/wpt/service-workers/navigation-optimization.https-expected.txt: Added. * LayoutTests/http/wpt/service-workers/navigation-optimization.https.html: Added. * LayoutTests/http/wpt/service-workers/resources/navigation-optimization.py: Added. (main): * LayoutTests/http/wpt/service-workers/resources/service-worker-iframe-preload-script.py: (main): * LayoutTests/http/wpt/service-workers/service-worker-iframe-preload.https-expected.txt: * LayoutTests/http/wpt/service-workers/service-worker-iframe-preload.https.html: * Source/WebCore/Headers.cmake: * Source/WebCore/Modules/fetch/FetchResponse.cpp: (WebCore::FetchResponse::createFetchResponse): (WebCore::FetchResponse::fetch): (WebCore::FetchResponse::startLoader): (WebCore::FetchResponse::setReceivedInternalResponse): (WebCore::FetchResponse::BodyLoader::didReceiveResponse): (WebCore::FetchResponse::markAsDisturbed): * Source/WebCore/Modules/fetch/FetchResponse.h: * Source/WebCore/WebCore.xcodeproj/project.pbxproj: * Source/WebCore/bindings/js/JSFetchEventCustom.cpp: * Source/WebCore/testing/ServiceWorkerInternals.cpp: * Source/WebCore/workers/service/FetchEvent.cpp: (WebCore::FetchEvent::preloadResponse): (WebCore::FetchEvent::navigationPreloadIsReady): (WebCore::FetchEvent::navigationPreloadFailed): * Source/WebCore/workers/service/FetchEvent.h: * Source/WebCore/workers/service/context/ServiceWorkerFetch.cpp: (WebCore::ServiceWorkerFetch::processResponse): (WebCore::ServiceWorkerFetch::dispatchFetchEvent): * Source/WebCore/workers/service/context/ServiceWorkerFetch.h: * Source/WebCore/workers/service/context/ServiceWorkerThreadProxy.cpp: (WebCore::ServiceWorkerThreadProxy::navigationPreloadIsReady): (WebCore::ServiceWorkerThreadProxy::navigationPreloadFailed): * Source/WebCore/workers/service/context/ServiceWorkerThreadProxy.h: * Source/WebKit/NetworkProcess/ServiceWorker/ServiceWorkerFetchTask.cpp: (WebKit::ServiceWorkerFetchTask::ServiceWorkerFetchTask): (WebKit::ServiceWorkerFetchTask::startFetch): (WebKit::ServiceWorkerFetchTask::usePreload): (WebKit::ServiceWorkerFetchTask::loadResponseFromPreloader): (WebKit::ServiceWorkerFetchTask::preloadResponseIsReady): * Source/WebKit/NetworkProcess/ServiceWorker/ServiceWorkerFetchTask.h: * Source/WebKit/NetworkProcess/ServiceWorker/ServiceWorkerFetchTask.messages.in: * Source/WebKit/NetworkProcess/ServiceWorker/ServiceWorkerNavigationPreloader.cpp: (WebKit::ServiceWorkerNavigationPreloader::ServiceWorkerNavigationPreloader): (WebKit::ServiceWorkerNavigationPreloader::didReceiveResponse): (WebKit::ServiceWorkerNavigationPreloader::waitForResponse): * Source/WebKit/WebProcess/Storage/WebSWContextManagerConnection.cpp: (WebKit::WebSWContextManagerConnection::navigationPreloadIsReady): (WebKit::WebSWContextManagerConnection::navigationPreloadFailed): * Source/WebKit/WebProcess/Storage/WebSWContextManagerConnection.h: * Source/WebKit/WebProcess/Storage/WebSWContextManagerConnection.messages.in: * Source/WebKit/WebProcess/Storage/WebServiceWorkerFetchTaskClient.cpp: (WebKit::WebServiceWorkerFetchTaskClient::navigationPreloadIsReady): (WebKit::WebServiceWorkerFetchTaskClient::navigationPreloadFailed): (WebKit::WebServiceWorkerFetchTaskClient::usePreload): (WebKit::WebServiceWorkerFetchTaskClient::cleanup): * Source/WebKit/WebProcess/Storage/WebServiceWorkerFetchTaskClient.h: Canonical link: https://commits.webkit.org/251493@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@295488 268f45cc-cd09-0410-ab3c-d52691b4dbfc
- Loading branch information
1 parent
cea1d90
commit b93bc2b70af6a276b242b1731f0c8f6f15174c20
Showing
31 changed files
with
410 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @@ -0,0 +1,19 @@ | ||
| async function doTest(event) | ||
| { | ||
| if (event.preloadResponse) { | ||
| event.respondWith(event.preloadResponse.then((response) => { | ||
| if (event.request.url.includes("get-body")) { | ||
| const clone = response.clone(); | ||
| clone.body.getReader(); | ||
| return response; | ||
| } | ||
| if (self.internals) | ||
| setTimeout(() => internals.terminate(), 0); | ||
| return response; | ||
| })); | ||
| return; | ||
| } | ||
| event.respondWith(fetch(event.request)); | ||
| } | ||
|
|
||
| self.addEventListener("fetch", doTest); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @@ -0,0 +1,6 @@ | ||
|
|
||
|
|
||
| PASS Setup worker | ||
| PASS Make sure a load that is transferred in network process continues even if service worker gets terminated. | ||
| PASS Make sure a load that a preload response can be read right away. | ||
|
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @@ -0,0 +1,49 @@ | ||
| <html> | ||
| <head> | ||
| <script src="/resources/testharness.js"></script> | ||
| <script src="/resources/testharnessreport.js"></script> | ||
| <script src="/service-workers/service-worker/resources/test-helpers.sub.js"></script> | ||
| </head> | ||
| <body> | ||
| <script> | ||
| promise_test(async (test) => { | ||
| const scope = "resources"; | ||
| const registration = await navigator.serviceWorker.register("navigation-optimization-worker.js", { scope : scope }); | ||
| activeWorker = registration.active; | ||
| if (activeWorker) | ||
| return; | ||
| activeWorker = registration.installing; | ||
| await new Promise(resolve => { | ||
| activeWorker.addEventListener('statechange', () => { | ||
| if (activeWorker.state === "activated") | ||
| resolve(); | ||
| }); | ||
| }); | ||
|
|
||
| await registration.navigationPreload.enable(); | ||
| }, "Setup worker"); | ||
|
|
||
| promise_test(async (test) => { | ||
| const promise1 = new Promise((resolve, reject) => { window.callback1 = resolve; setTimeout(() => reject("callback1"), 4000); }); | ||
| const promise2 = new Promise((resolve, reject) => { window.callback2 = resolve; setTimeout(() => reject("callback2"), 5000); }); | ||
|
|
||
| // The iframe is responsible to call callback1 and callback2. | ||
| with_iframe("/WebKit/service-workers/resources/navigation-optimization.py?delay=1.0"); | ||
|
|
||
| await promise1; | ||
| await promise2; | ||
| }, "Make sure a load that is transferred in network process continues even if service worker gets terminated."); | ||
|
|
||
| promise_test(async (test) => { | ||
| const promise1 = new Promise((resolve, reject) => { window.callback1 = resolve; setTimeout(() => reject("callback1"), 4000); }); | ||
| const promise2 = new Promise((resolve, reject) => { window.callback2 = resolve; setTimeout(() => reject("callback2"), 5000); }); | ||
|
|
||
| // The iframe is responsible to call callback1 and callback2. | ||
| with_iframe("/WebKit/service-workers/resources/navigation-optimization.py?get-body"); | ||
|
|
||
| await promise1; | ||
| await promise2; | ||
| }, "Make sure a load that a preload response can be read right away."); | ||
| </script> | ||
| </body> | ||
| </html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @@ -0,0 +1,14 @@ | ||
| import time | ||
|
|
||
| def main(request, response): | ||
| delay = 0.05 | ||
| headers = [] | ||
| if b"delay" in request.GET: | ||
| delay = float(request.GET.first(b"delay")) | ||
| response.headers.set(b"Content-type", b"text/html") | ||
| response.headers.append(b"Access-Control-Allow-Origin", b"*") | ||
| response.write_status_headers() | ||
| response.writer.write_content("<script>parent.callback1();</script>") | ||
| time.sleep(delay) | ||
| response.writer.write_content("<script>parent.callback2();</script>") | ||
| time.sleep(delay) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @@ -1,5 +1,5 @@ | ||
|
|
||
|
|
||
| PASS Setup activating worker | ||
| PASS Service worker load uses preload if available and fetch event was not handled | ||
| PASS Service worker load uses preload that starts as soon as possible | ||
|
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @@ -1,3 +1,4 @@ | ||
| import time | ||
| import zlib | ||
|
|
||
| def main(request, response): | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.