From 0ad981249f002df27ef5398ea61b068c766b2455 Mon Sep 17 00:00:00 2001 From: Julian Descottes Date: Fri, 12 Jan 2024 11:08:14 +0000 Subject: [PATCH] Bug 1853883 - [bidi] Implement network.failRequest command r=webdriver-reviewers,necko-reviewers,jesup,whimboo Depends on D197652 Differential Revision: https://phabricator.services.mozilla.com/D197653 --- netwerk/base/nsILoadInfo.idl | 2 + .../modules/root/network.sys.mjs | 61 ++++++++++++++++++- .../bidi/network/fail_request/invalid.py.ini | 27 -------- 3 files changed, 61 insertions(+), 29 deletions(-) delete mode 100644 testing/web-platform/meta/webdriver/tests/bidi/network/fail_request/invalid.py.ini diff --git a/netwerk/base/nsILoadInfo.idl b/netwerk/base/nsILoadInfo.idl index 7fc57124ecfe..ddfcb223e612 100644 --- a/netwerk/base/nsILoadInfo.idl +++ b/netwerk/base/nsILoadInfo.idl @@ -1420,6 +1420,8 @@ interface nsILoadInfo : nsISupports const uint32_t BLOCKING_REASON_NOT_SAME_ORIGIN = 5000; // The reason used when an extension cancels the request via the WebRequest api. const uint32_t BLOCKING_REASON_EXTENSION_WEBREQUEST = 6000; + // The reason used when a request is cancelled via WebDriver BiDi network interception. + const uint32_t BLOCKING_REASON_WEBDRIVER_BIDI = 7000; /** * If the request associated with this load info was blocked by some of diff --git a/remote/webdriver-bidi/modules/root/network.sys.mjs b/remote/webdriver-bidi/modules/root/network.sys.mjs index d938150963ca..09545d9a4cb7 100644 --- a/remote/webdriver-bidi/modules/root/network.sys.mjs +++ b/remote/webdriver-bidi/modules/root/network.sys.mjs @@ -462,6 +462,53 @@ class NetworkModule extends Module { resolveBlockedEvent(); } + /** + * Fails a request that is blocked by a network intercept. + * + * @param {object=} options + * @param {string} options.request + * The id of the blocked request that should be continued. + * + * @throws {InvalidArgumentError} + * Raised if an argument is of an invalid type or value. + * @throws {NoSuchRequestError} + * Raised if the request id does not match any request in the blocked + * requests map. + */ + async failRequest(options = {}) { + this.assertExperimentalCommandsEnabled("network.failRequest"); + const { request: requestId } = options; + + lazy.assert.string( + requestId, + `Expected "request" to be a string, got ${requestId}` + ); + + if (!this.#blockedRequests.has(requestId)) { + throw new lazy.error.NoSuchRequestError( + `Blocked request with id ${requestId} not found` + ); + } + + const { phase, request, resolveBlockedEvent } = + this.#blockedRequests.get(requestId); + + if (phase === InterceptPhase.AuthRequired) { + throw new lazy.error.InvalidArgumentError( + `Expected blocked request not to be in "authRequired" phase` + ); + } + + const wrapper = ChannelWrapper.get(request); + wrapper.resume(); + wrapper.cancel( + Cr.NS_ERROR_ABORT, + Ci.nsILoadInfo.BLOCKING_REASON_WEBDRIVER_BIDI + ); + + resolveBlockedEvent(); + } + /** * Removes an existing network intercept. * @@ -575,6 +622,10 @@ class NetworkModule extends Module { }; } + #getSuspendMarkerText(requestData, phase) { + return `Request (id: ${requestData.request}) suspended by WebDriver BiDi in ${phase} phase`; + } + #getNetworkIntercepts(event, requestData) { const intercepts = []; @@ -809,7 +860,10 @@ class NetworkModule extends Module { if (beforeRequestSentEvent.isBlocked) { // TODO: Requests suspended in beforeRequestSent still reach the server at // the moment. https://bugzilla.mozilla.org/show_bug.cgi?id=1849686 - requestChannel.suspend(); + const wrapper = ChannelWrapper.get(requestChannel); + wrapper.suspend( + this.#getSuspendMarkerText(requestData, "beforeRequestSent") + ); this.#addBlockedRequest( beforeRequestSentEvent.request.request, @@ -982,7 +1036,10 @@ class NetworkModule extends Module { protocolEventName === "network.responseStarted" && responseEvent.isBlocked ) { - requestChannel.suspend(); + const wrapper = ChannelWrapper.get(requestChannel); + wrapper.suspend( + this.#getSuspendMarkerText(requestData, "responseStarted") + ); this.#addBlockedRequest( responseEvent.request.request, diff --git a/testing/web-platform/meta/webdriver/tests/bidi/network/fail_request/invalid.py.ini b/testing/web-platform/meta/webdriver/tests/bidi/network/fail_request/invalid.py.ini deleted file mode 100644 index f2f31280e03d..000000000000 --- a/testing/web-platform/meta/webdriver/tests/bidi/network/fail_request/invalid.py.ini +++ /dev/null @@ -1,27 +0,0 @@ -[invalid.py] - [test_params_request_invalid_phase] - expected: FAIL - - [test_params_request_invalid_type[None\]] - expected: FAIL - - [test_params_request_invalid_type[False\]] - expected: FAIL - - [test_params_request_invalid_type[42\]] - expected: FAIL - - [test_params_request_invalid_type[value3\]] - expected: FAIL - - [test_params_request_invalid_type[value4\]] - expected: FAIL - - [test_params_request_invalid_value[\]] - expected: FAIL - - [test_params_request_invalid_value[foo\]] - expected: FAIL - - [test_params_request_no_such_request] - expected: FAIL