From 3548686a2a5e3142b77f478d999c0137a10864b9 Mon Sep 17 00:00:00 2001 From: Asami Doi Date: Wed, 20 Oct 2021 05:35:40 +0000 Subject: [PATCH] PlzDedicatedWorker: support DevTools for browser-initiated dedicated workers PlzDedicatedWorker makes the worker script fetch a browser-initiated and dedicated workers will be real service worker clients. Before the feature, the worker script fetch was a renderer-initiated and dedicated workers are treated as a subresource in the parent frame. I2S: https://groups.google.com/a/chromium.org/g/blink-dev/c/FhGd1AqB3ng/ This CL 1) Removes http/tests/inspector-protocol/fetch/dedicated-worker-main-script.js because `dp.Fetch.onRequestPaused` intercepts a renderer-initiated request in the frame, but after PlzDedicatedWorker, the worker script fetch runs on the browser process. 2) Instead, adds http/tests/inspector-protocol/fetch/worker-interception.js that observes browser-initiated requests via `globalFetcher`. 3) Implements the DevTools integration for PlzDedicatedWorker. 3-1) Creates WorkerDevToolsAgentHost in DedicatedWorkerHostFactoryImpl. 3-2) Sets a callback to TargetHandler as `worker_throttle_`. 3-3) Resumes the script fetch in DedicatedWorkerHost::StartScriptLoad(). The worker script fetch of `worker.js` is not observed without the implementation (3) and the worker-interception.js test fails: > Tests that dedicated worker requests are intercepted. > [browser] Request to http://127.0.0.1:8000/inspector-protocol/fetch/resources/empty.html, type: Document > -[browser] Request to http://127.0.0.1:8000/inspector-protocol/fetch/resources/worker.js, type: Other > [browser] Request to http://127.0.0.1:8000/inspector-protocol/fetch/resources/fetch-data.txt, type: XHR > -Response after Fetch.fulfillRequest: Fetched for real! > +Response after Fetch.fulfillRequest: overridden response body See the test result of http/tests/inspector-protocol/fetch/worker-interception.js in https://ci.chromium.org/ui/p/chromium/builders/try/linux_layout_tests_composite_after_paint/55826/test-results Bug: 1143102 Change-Id: I9d2f23ed4076f9fa36805da599daac06bc2b163d Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3068908 Reviewed-by: Koji Ishii Reviewed-by: Andrey Kosyakov Reviewed-by: Hiroki Nakagawa Commit-Queue: Asami Doi Cr-Commit-Position: refs/heads/main@{#933339} NOKEYCHECK=True GitOrigin-RevId: 1ee5fb813d06cfb948ba4d71929b954db1971754 --- blink/web_tests/VirtualTestSuites | 1 + .../fetch/dedicated-worker-main-script.js | 33 ------------------- .../{service-worker.html => empty.html} | 0 .../fetch/resources/shared-worker.js | 6 ++++ .../fetch/resources/worker.js | 7 ++-- .../service-worker-interception-expected.txt | 6 ++-- ...vice-worker-interception-late-expected.txt | 2 +- .../fetch/service-worker-interception-late.js | 2 +- .../fetch/service-worker-interception.js | 2 +- .../fetch/worker-interception-expected.txt | 5 +++ .../fetch/worker-interception.js | 33 +++++++++++++++++++ 11 files changed, 54 insertions(+), 43 deletions(-) delete mode 100644 blink/web_tests/http/tests/inspector-protocol/fetch/dedicated-worker-main-script.js rename blink/web_tests/http/tests/inspector-protocol/fetch/resources/{service-worker.html => empty.html} (100%) create mode 100644 blink/web_tests/http/tests/inspector-protocol/fetch/resources/shared-worker.js create mode 100644 blink/web_tests/http/tests/inspector-protocol/fetch/worker-interception-expected.txt create mode 100644 blink/web_tests/http/tests/inspector-protocol/fetch/worker-interception.js diff --git a/blink/web_tests/VirtualTestSuites b/blink/web_tests/VirtualTestSuites index 61e867617cd9..7bf04fc528ab 100644 --- a/blink/web_tests/VirtualTestSuites +++ b/blink/web_tests/VirtualTestSuites @@ -467,6 +467,7 @@ "external/wpt/workers", "external/wpt/xhr", "fast/workers", + "http/tests/inspector-protocol/fetch", "http/tests/origin_trials", "http/tests/workers"], "args": ["--enable-features=PlzDedicatedWorker,SharedArrayBuffer"] diff --git a/blink/web_tests/http/tests/inspector-protocol/fetch/dedicated-worker-main-script.js b/blink/web_tests/http/tests/inspector-protocol/fetch/dedicated-worker-main-script.js deleted file mode 100644 index 4eb46e0a7202..000000000000 --- a/blink/web_tests/http/tests/inspector-protocol/fetch/dedicated-worker-main-script.js +++ /dev/null @@ -1,33 +0,0 @@ -(async function(testRunner) { - const {page, session, dp} = await testRunner.startBlank( - `Tests that Fetch.requestPaused is emitted for the main script of dedciated worker`); - - await dp.Target.setAutoAttach( - {autoAttach: true, waitForDebuggerOnStart: true, flatten: true}); - - await dp.Fetch.enable({patterns: [{}]}); - dp.Fetch.onRequestPaused(event => { - if (event.params.request.url.endsWith('/worker.js')) { - dp.Fetch.fulfillRequest({ - requestId: event.params.requestId, - responseCode: 200, - responseHeaders: [{name: 'Content-Type', value: 'application/x-javascript'}], - body: btoa(`console.log('PASSED: intercepted script')`) - }) - } - }); - const consoleMessagePromise = new Promise(resolve => { - dp.Target.onAttachedToTarget(async event => { - const wdp = session.createChild(event.params.sessionId).protocol; - wdp.Runtime.enable(); - wdp.Runtime.onConsoleAPICalled(e => { - resolve(e.params.args[0].value); - }) - wdp.Runtime.runIfWaitingForDebugger(); - }); - }); - - session.evaluate(`new Worker('/inspector-protocol/network/resources/worker.js')`); - testRunner.log(await consoleMessagePromise); - testRunner.completeTest(); -}) diff --git a/blink/web_tests/http/tests/inspector-protocol/fetch/resources/service-worker.html b/blink/web_tests/http/tests/inspector-protocol/fetch/resources/empty.html similarity index 100% rename from blink/web_tests/http/tests/inspector-protocol/fetch/resources/service-worker.html rename to blink/web_tests/http/tests/inspector-protocol/fetch/resources/empty.html diff --git a/blink/web_tests/http/tests/inspector-protocol/fetch/resources/shared-worker.js b/blink/web_tests/http/tests/inspector-protocol/fetch/resources/shared-worker.js new file mode 100644 index 000000000000..c1628fa2b2e8 --- /dev/null +++ b/blink/web_tests/http/tests/inspector-protocol/fetch/resources/shared-worker.js @@ -0,0 +1,6 @@ +self.testToken = 'FAIL: original value'; + +self.addEventListener('connect', e => { + e.ports[0].postMessage('ready'); +}); + diff --git a/blink/web_tests/http/tests/inspector-protocol/fetch/resources/worker.js b/blink/web_tests/http/tests/inspector-protocol/fetch/resources/worker.js index c1628fa2b2e8..c17876a2aa1d 100644 --- a/blink/web_tests/http/tests/inspector-protocol/fetch/resources/worker.js +++ b/blink/web_tests/http/tests/inspector-protocol/fetch/resources/worker.js @@ -1,6 +1,5 @@ -self.testToken = 'FAIL: original value'; +console.log("Worker"); -self.addEventListener('connect', e => { - e.ports[0].postMessage('ready'); +self.addEventListener('message', e => { + self.postMessage('ready'); }); - diff --git a/blink/web_tests/http/tests/inspector-protocol/fetch/service-worker-interception-expected.txt b/blink/web_tests/http/tests/inspector-protocol/fetch/service-worker-interception-expected.txt index 87952f9aef3a..1eac883f0451 100644 --- a/blink/web_tests/http/tests/inspector-protocol/fetch/service-worker-interception-expected.txt +++ b/blink/web_tests/http/tests/inspector-protocol/fetch/service-worker-interception-expected.txt @@ -1,9 +1,9 @@ Tests that service worker requests are intercepted. -[browser] Request to http://127.0.0.1:8000/inspector-protocol/fetch/resources/service-worker.html, type: Document +[browser] Request to http://127.0.0.1:8000/inspector-protocol/fetch/resources/empty.html, type: Document [renderer] Request to http://127.0.0.1:8000/inspector-protocol/fetch/resources/service-worker.js, type: Other [browser] Request to http://127.0.0.1:8000/inspector-protocol/fetch/resources/service-worker.js, type: Other -[renderer] Request to http://127.0.0.1:8000/inspector-protocol/fetch/resources/service-worker.html, type: XHR -[browser] Request to http://127.0.0.1:8000/inspector-protocol/fetch/resources/service-worker.html, type: XHR +[renderer] Request to http://127.0.0.1:8000/inspector-protocol/fetch/resources/empty.html, type: XHR +[browser] Request to http://127.0.0.1:8000/inspector-protocol/fetch/resources/empty.html, type: XHR Response fulfilled by service worker: response from service worker [renderer] Request to http://127.0.0.1:8000/inspector-protocol/fetch/resources/fetch-data.txt, type: XHR [browser] Request to http://127.0.0.1:8000/inspector-protocol/fetch/resources/fetch-data.txt, type: XHR diff --git a/blink/web_tests/http/tests/inspector-protocol/fetch/service-worker-interception-late-expected.txt b/blink/web_tests/http/tests/inspector-protocol/fetch/service-worker-interception-late-expected.txt index 683bdde35c65..dbcc877935a9 100644 --- a/blink/web_tests/http/tests/inspector-protocol/fetch/service-worker-interception-late-expected.txt +++ b/blink/web_tests/http/tests/inspector-protocol/fetch/service-worker-interception-late-expected.txt @@ -2,7 +2,7 @@ Tests that service worker requests are intercepted when DevTools attached after Response before interception enabled: Fetched for real! [renderer] Request to http://127.0.0.1:8000/inspector-protocol/fetch/resources/service-worker-import.js, type: Script Imported script after interception enabled: overriden imported script! -[renderer] Request to http://127.0.0.1:8000/inspector-protocol/fetch/resources/service-worker.html, type: XHR +[renderer] Request to http://127.0.0.1:8000/inspector-protocol/fetch/resources/empty.html, type: XHR [renderer] Request to http://127.0.0.1:8000/inspector-protocol/fetch/resources/fetch-data.txt, type: XHR Response after interception enabled: overriden response body diff --git a/blink/web_tests/http/tests/inspector-protocol/fetch/service-worker-interception-late.js b/blink/web_tests/http/tests/inspector-protocol/fetch/service-worker-interception-late.js index 9ce62139a4f1..aa439be68996 100644 --- a/blink/web_tests/http/tests/inspector-protocol/fetch/service-worker-interception-late.js +++ b/blink/web_tests/http/tests/inspector-protocol/fetch/service-worker-interception-late.js @@ -13,7 +13,7 @@ }); await dp.ServiceWorker.enable(); - await session.navigate("resources/service-worker.html"); + await session.navigate("resources/empty.html"); session.evaluateAsync(` navigator.serviceWorker.register('service-worker.js?defer-install')`); diff --git a/blink/web_tests/http/tests/inspector-protocol/fetch/service-worker-interception.js b/blink/web_tests/http/tests/inspector-protocol/fetch/service-worker-interception.js index f7f4d73cf975..afeb350ceeac 100644 --- a/blink/web_tests/http/tests/inspector-protocol/fetch/service-worker-interception.js +++ b/blink/web_tests/http/tests/inspector-protocol/fetch/service-worker-interception.js @@ -20,7 +20,7 @@ }); await dp.ServiceWorker.enable(); - await session.navigate("resources/service-worker.html"); + await session.navigate("resources/empty.html"); session.evaluateAsync(`navigator.serviceWorker.register('service-worker.js')`); async function waitForServiceWorkerActivation() { diff --git a/blink/web_tests/http/tests/inspector-protocol/fetch/worker-interception-expected.txt b/blink/web_tests/http/tests/inspector-protocol/fetch/worker-interception-expected.txt new file mode 100644 index 000000000000..f14ece015f1d --- /dev/null +++ b/blink/web_tests/http/tests/inspector-protocol/fetch/worker-interception-expected.txt @@ -0,0 +1,5 @@ +Tests that dedicated worker requests are intercepted. +[browser] Request to http://127.0.0.1:8000/inspector-protocol/fetch/resources/empty.html, type: Document +[browser] Request to http://127.0.0.1:8000/inspector-protocol/fetch/resources/worker.js, type: Other +worker is ready + diff --git a/blink/web_tests/http/tests/inspector-protocol/fetch/worker-interception.js b/blink/web_tests/http/tests/inspector-protocol/fetch/worker-interception.js new file mode 100644 index 000000000000..ca5eddf7b2f2 --- /dev/null +++ b/blink/web_tests/http/tests/inspector-protocol/fetch/worker-interception.js @@ -0,0 +1,33 @@ +(async function(testRunner) { + const {page, session, dp} = await testRunner.startBlank( + `Tests that dedicated worker requests are intercepted.`); + + const FetchHelper = await testRunner.loadScript("resources/fetch-test.js"); + const globalFetcher = new FetchHelper(testRunner, testRunner.browserP()); + globalFetcher.setLogPrefix("[browser] "); + await globalFetcher.enable(); + + globalFetcher.onRequest().continueRequest({}); + + await dp.Target.setAutoAttach({ + autoAttach: true, waitForDebuggerOnStart: true, flatten: true}); + dp.Target.onAttachedToTarget(async event => { + const wdp = session.createChild(event.params.sessionId).protocol; + await wdp.Runtime.runIfWaitingForDebugger(); + }); + + await dp.Page.enable(); + await session.navigate("resources/empty.html"); + + const result = await session.evaluateAsync(` + const w = new Worker('/inspector-protocol/fetch/resources/worker.js'); + new Promise((resolve, reject) => { + w.onmessage = e => resolve('worker is ready'); + w.onerror = e => reject(e.message); + w.postMessage('start a worker'); + }) + `); + + testRunner.log(result); + testRunner.completeTest(); +});