Skip to content

Commit

Permalink
feat: prototype network request interception: scaffold protocol (#845)
Browse files Browse the repository at this point in the history
Bug: #644
Doc: http://go/webdriver-bidi:network-request-interception-proposal
  • Loading branch information
Thiago Perrotta authored Jun 30, 2023
1 parent a18236e commit 1b77f94
Show file tree
Hide file tree
Showing 15 changed files with 792 additions and 249 deletions.
137 changes: 137 additions & 0 deletions src/bidiMapper/BidiNoOpParser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/**
* Copyright 2023 Google LLC.
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import type {
BrowsingContext,
Cdp,
Input,
Network,
Script,
Session,
} from '../protocol/protocol.js';

import type {IBidiParser} from './BidiParser.js';

export class BidiNoOpParser implements IBidiParser {
// Browsing Context domain
parseCaptureScreenshotParams(
params: object
): BrowsingContext.CaptureScreenshotParameters {
return params as BrowsingContext.CaptureScreenshotParameters;
}
parseCloseParams(params: object): BrowsingContext.CloseParameters {
return params as BrowsingContext.CloseParameters;
}
parseCreateParams(params: object): BrowsingContext.CreateParameters {
return params as BrowsingContext.CreateParameters;
}
parseGetTreeParams(params: object): BrowsingContext.GetTreeParameters {
return params as BrowsingContext.GetTreeParameters;
}
parseNavigateParams(params: object): BrowsingContext.NavigateParameters {
return params as BrowsingContext.NavigateParameters;
}
parsePrintParams(params: object): BrowsingContext.PrintParameters {
return params as BrowsingContext.PrintParameters;
}
parseReloadParams(params: object): BrowsingContext.ReloadParameters {
return params as BrowsingContext.ReloadParameters;
}
parseSetViewportParams(
params: object
): BrowsingContext.SetViewportParameters {
return params as BrowsingContext.SetViewportParameters;
}

// CDP domain
parseGetSessionParams(params: object): Cdp.GetSessionParams {
return params as Cdp.GetSessionParams;
}
parseSendCommandParams(params: object): Cdp.SendCommandParams {
return params as Cdp.SendCommandParams;
}

// Script domain
parseAddPreloadScriptParams(
params: object
): Script.AddPreloadScriptParameters {
return params as Script.AddPreloadScriptParameters;
}
parseCallFunctionParams(params: object): Script.CallFunctionParameters {
return params as Script.CallFunctionParameters;
}
parseDisownParams(params: object): Script.DisownParameters {
return params as Script.DisownParameters;
}
parseEvaluateParams(params: object): Script.EvaluateParameters {
return params as Script.EvaluateParameters;
}
parseGetRealmsParams(params: object): Script.GetRealmsParameters {
return params as Script.GetRealmsParameters;
}
parseRemovePreloadScriptParams(
params: object
): Script.RemovePreloadScriptParameters {
return params as Script.RemovePreloadScriptParameters;
}

// Input domain
parsePerformActionsParams(params: object): Input.PerformActionsParameters {
return params as Input.PerformActionsParameters;
}
parseReleaseActionsParams(params: object): Input.ReleaseActionsParameters {
return params as Input.ReleaseActionsParameters;
}

// Network domain
parseAddInterceptParams(params: object): Network.AddInterceptParameters {
return params as Network.AddInterceptParameters;
}
parseContinueRequestParams(
params: object
): Network.ContinueRequestParameters {
return params as Network.ContinueRequestParameters;
}
parseContinueResponseParams(
params: object
): Network.ContinueResponseParameters {
return params as Network.ContinueResponseParameters;
}
parseContinueWithAuthParams(
params: object
): Network.ContinueWithAuthParameters {
return params as Network.ContinueWithAuthParameters;
}
parseFailRequestParams(params: object): Network.FailRequestParameters {
return params as Network.FailRequestParameters;
}
parseProvideResponseParams(
params: object
): Network.ProvideResponseParameters {
return params as Network.ProvideResponseParameters;
}
parseRemoveInterceptParams(
params: object
): Network.RemoveInterceptParameters {
return params as Network.RemoveInterceptParameters;
}

// Session domain
parseSubscribeParams(params: object): Session.SubscriptionRequest {
return params as Session.SubscriptionRequest;
}
}
75 changes: 75 additions & 0 deletions src/bidiMapper/BidiParser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**
* Copyright 2023 Google LLC.
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import type {
BrowsingContext,
Cdp,
Input,
Network,
Script,
Session,
} from '../protocol/protocol.js';

export interface IBidiParser {
// Browsing Context domain
parseCaptureScreenshotParams(
params: object
): BrowsingContext.CaptureScreenshotParameters;
parseCloseParams(params: object): BrowsingContext.CloseParameters;
parseCreateParams(params: object): BrowsingContext.CreateParameters;
parseGetTreeParams(params: object): BrowsingContext.GetTreeParameters;
parseNavigateParams(params: object): BrowsingContext.NavigateParameters;
parsePrintParams(params: object): BrowsingContext.PrintParameters;
parseReloadParams(params: object): BrowsingContext.ReloadParameters;
parseSetViewportParams(params: object): BrowsingContext.SetViewportParameters;

// CDP domain
parseSendCommandParams(params: object): Cdp.SendCommandParams;
parseGetSessionParams(params: object): Cdp.GetSessionParams;

// Input domain
parsePerformActionsParams(params: object): Input.PerformActionsParameters;
parseReleaseActionsParams(params: object): Input.ReleaseActionsParameters;

// Network domain
parseAddInterceptParams(params: object): Network.AddInterceptParameters;
parseContinueRequestParams(params: object): Network.ContinueRequestParameters;
parseContinueResponseParams(
params: object
): Network.ContinueResponseParameters;
parseContinueWithAuthParams(
params: object
): Network.ContinueWithAuthParameters;
parseFailRequestParams(params: object): Network.FailRequestParameters;
parseProvideResponseParams(params: object): Network.ProvideResponseParameters;
parseRemoveInterceptParams(params: object): Network.RemoveInterceptParameters;

// Script domain
parseAddPreloadScriptParams(
params: object
): Script.AddPreloadScriptParameters;
parseCallFunctionParams(params: object): Script.CallFunctionParameters;
parseDisownParams(params: object): Script.DisownParameters;
parseEvaluateParams(params: object): Script.EvaluateParameters;
parseGetRealmsParams(params: object): Script.GetRealmsParameters;
parseRemovePreloadScriptParams(
params: object
): Script.RemovePreloadScriptParameters;

// Session domain
parseSubscribeParams(params: object): Session.SubscriptionRequest;
}
15 changes: 8 additions & 7 deletions src/bidiMapper/BidiServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ import type {Message} from '../protocol/protocol.js';
import {ProcessingQueue} from '../utils/processingQueue.js';
import type {ICdpConnection} from '../cdp/cdpConnection.js';

import {type BidiParser, CommandProcessor} from './CommandProcessor.js';
import type {BidiTransport} from './BidiTransport.js';
import {CommandProcessor} from './CommandProcessor.js';
import type {IBidiParser} from './BidiParser.js';
import type {IBidiTransport} from './BidiTransport.js';
import {BrowsingContextStorage} from './domains/context/browsingContextStorage.js';
import {EventManager} from './domains/events/EventManager.js';
import type {OutgoingBidiMessage} from './OutgoingBidiMessage.js';
Expand All @@ -34,7 +35,7 @@ type BidiServerEvents = {

export class BidiServer extends EventEmitter<BidiServerEvents> {
#messageQueue: ProcessingQueue<OutgoingBidiMessage>;
#transport: BidiTransport;
#transport: IBidiTransport;
#commandProcessor: CommandProcessor;
#browsingContextStorage = new BrowsingContextStorage();
#realmStorage = new RealmStorage();
Expand All @@ -57,10 +58,10 @@ export class BidiServer extends EventEmitter<BidiServerEvents> {
};

private constructor(
bidiTransport: BidiTransport,
bidiTransport: IBidiTransport,
cdpConnection: ICdpConnection,
selfTargetId: string,
parser?: BidiParser,
parser?: IBidiParser,
logger?: LoggerFn
) {
super();
Expand Down Expand Up @@ -89,10 +90,10 @@ export class BidiServer extends EventEmitter<BidiServerEvents> {
}

static async createAndStart(
bidiTransport: BidiTransport,
bidiTransport: IBidiTransport,
cdpConnection: ICdpConnection,
selfTargetId: string,
parser?: BidiParser,
parser?: IBidiParser,
logger?: LoggerFn
): Promise<BidiServer> {
const server = new BidiServer(
Expand Down
2 changes: 1 addition & 1 deletion src/bidiMapper/BidiTransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import type {Message} from '../protocol/protocol.js';

export interface BidiTransport {
export interface IBidiTransport {
setOnMessage: (
handler: (message: Message.RawCommandRequest) => Promise<void> | void
) => void;
Expand Down
Loading

0 comments on commit 1b77f94

Please sign in to comment.