summaryrefslogtreecommitdiffstats
path: root/remote/webdriver-bidi/modules/root/network.sys.mjs
diff options
context:
space:
mode:
Diffstat (limited to 'remote/webdriver-bidi/modules/root/network.sys.mjs')
-rw-r--r--remote/webdriver-bidi/modules/root/network.sys.mjs81
1 files changed, 74 insertions, 7 deletions
diff --git a/remote/webdriver-bidi/modules/root/network.sys.mjs b/remote/webdriver-bidi/modules/root/network.sys.mjs
index 238b9f3640..6850e3f372 100644
--- a/remote/webdriver-bidi/modules/root/network.sys.mjs
+++ b/remote/webdriver-bidi/modules/root/network.sys.mjs
@@ -340,6 +340,9 @@ class NetworkModule extends Module {
* of any intercept, the request will be suspended.
*
* @param {object=} options
+ * @param {Array<string>=} options.contexts
+ * The list of browsing context ids where this intercept should be used.
+ * Optional, defaults to null.
* @param {Array<InterceptPhase>} options.phases
* The phases where this intercept should be checked.
* @param {Array<URLPattern>=} options.urlPatterns
@@ -353,7 +356,34 @@ class NetworkModule extends Module {
* Raised if an argument is of an invalid type or value.
*/
addIntercept(options = {}) {
- const { phases, urlPatterns = [] } = options;
+ const { contexts = null, phases, urlPatterns = [] } = options;
+
+ if (contexts !== null) {
+ lazy.assert.array(
+ contexts,
+ `Expected "contexts" to be an array, got ${contexts}`
+ );
+
+ if (!options.contexts.length) {
+ throw new lazy.error.InvalidArgumentError(
+ `Expected "contexts" to contain at least one item, got an empty array`
+ );
+ }
+
+ for (const contextId of contexts) {
+ lazy.assert.string(
+ contextId,
+ `Expected elements of "contexts" to be a string, got ${contextId}`
+ );
+ const context = this.#getBrowsingContext(contextId);
+
+ if (context.parent) {
+ throw new lazy.error.InvalidArgumentError(
+ `Context with id ${contextId} is not a top-level browsing context`
+ );
+ }
+ }
+ }
lazy.assert.array(
phases,
@@ -386,6 +416,7 @@ class NetworkModule extends Module {
const interceptId = lazy.generateUUID();
this.#interceptMap.set(interceptId, {
+ contexts,
phases,
urlPatterns: parsedPatterns,
});
@@ -1122,6 +1153,23 @@ class NetworkModule extends Module {
return challenges;
}
+ #getBrowsingContext(contextId) {
+ const context = lazy.TabManager.getBrowsingContextById(contextId);
+ if (context === null) {
+ throw new lazy.error.NoSuchFrameError(
+ `Browsing Context with id ${contextId} not found`
+ );
+ }
+
+ if (!context.currentWindowGlobal) {
+ throw new lazy.error.NoSuchFrameError(
+ `No window found for BrowsingContext with id ${contextId}`
+ );
+ }
+
+ return context;
+ }
+
#getContextInfo(browsingContext) {
return {
contextId: browsingContext.id,
@@ -1129,11 +1177,7 @@ class NetworkModule extends Module {
};
}
- #getSuspendMarkerText(requestData, phase) {
- return `Request (id: ${requestData.request}) suspended by WebDriver BiDi in ${phase} phase`;
- }
-
- #getNetworkIntercepts(event, requestData) {
+ #getNetworkIntercepts(event, requestData, contextId) {
const intercepts = [];
let phase;
@@ -1153,8 +1197,23 @@ class NetworkModule extends Module {
return intercepts;
}
+ // Retrieve the top browsing context id for this network event.
+ const browsingContext = lazy.TabManager.getBrowsingContextById(contextId);
+ const topLevelContextId = lazy.TabManager.getIdForBrowsingContext(
+ browsingContext.top
+ );
+
const url = requestData.url;
for (const [interceptId, intercept] of this.#interceptMap) {
+ if (
+ intercept.contexts !== null &&
+ !intercept.contexts.includes(topLevelContextId)
+ ) {
+ // Skip this intercept if the event's context does not match the list
+ // of contexts for this intercept.
+ continue;
+ }
+
if (intercept.phases.includes(phase)) {
const urlPatterns = intercept.urlPatterns;
if (
@@ -1196,6 +1255,10 @@ class NetworkModule extends Module {
return navigation ? navigation.navigationId : null;
}
+ #getSuspendMarkerText(requestData, phase) {
+ return `Request (id: ${requestData.request}) suspended by WebDriver BiDi in ${phase} phase`;
+ }
+
#onAuthRequired = (name, data) => {
const {
authCallbacks,
@@ -1580,7 +1643,11 @@ class NetworkModule extends Module {
#processNetworkEvent(eventName, data) {
const { contextId, navigation, redirectCount, requestData, timestamp } =
data;
- const intercepts = this.#getNetworkIntercepts(eventName, requestData);
+ const intercepts = this.#getNetworkIntercepts(
+ eventName,
+ requestData,
+ contextId
+ );
const isBlocked = !!intercepts.length;
const baseParameters = {