Skip to content

Commit

Permalink
Command typification.
Browse files Browse the repository at this point in the history
  • Loading branch information
sadym-chromium committed Oct 11, 2021
1 parent ec0a287 commit 896600f
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 31 deletions.
78 changes: 72 additions & 6 deletions src/bidiMapper/bidiProtocolTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export namespace CommonDataTypes {
// TODO sadym: declare `RemoteValue` properly according to
// https://w3c.github.io/webdriver-bidi/#type-common-RemoteValue.
export type RemoteValue = any;
export type EmptyParams = {};

export type ExceptionDetails = {
columnNumber: number;
Expand All @@ -10,24 +11,34 @@ export namespace CommonDataTypes {
stackTrace: StackTrace;
text: string;
};

export type StackTrace = {
callFrames: StackFrame[];
};

export type StackFrame = {
url: string;
functionName: string;
lineNumber: number;
columnNumber: number;
};
}

export namespace Script {
export type RealTarget = {
export type RealmTarget = {
// TODO sadym: implement.
};

export type ContextTarget = {
context: BrowsingContext.BrowsingContext;
};
export type Target = ContextTarget | RealTarget;

export type Target = ContextTarget | RealmTarget;

export type ScriptEvaluateCommand = {
method: 'script.evaluate';
params: ScriptEvaluateParameters;
};

export type ScriptExceptionResult = {
exceptionDetails: CommonDataTypes.ExceptionDetails;
Expand All @@ -48,6 +59,11 @@ export namespace Script {
};

export namespace PROTO {
export type ScriptInvokeCommand = {
method: 'PROTO.script.invoke';
params: ScriptInvokeParameters;
};

export type ScriptInvokeParameters = {
functionDeclaration: string;
args: InvokeArgument[];
Expand All @@ -74,9 +90,59 @@ export namespace Script {
value: any;
};
}
}

// https://w3c.github.io/webdriver-bidi/#module-browsingContext
export namespace BrowsingContext {
export type BrowsingContext = string;
}
// https://w3c.github.io/webdriver-bidi/#module-browsingContext
export namespace BrowsingContext {
export type BrowsingContext = string;

export type BrowsingContextGetTreeCommand = {
method: 'browsingContext.getTree';
params: BrowsingContextGetTreeParameters;
};

export type BrowsingContextGetTreeParameters = {
maxDepth?: number;
parent?: BrowsingContext;
};

export type BrowsingContextGetTreeResult = {
contexts: BrowsingContextInfoList;
};

export type BrowsingContextInfoList = BrowsingContextInfo[];

export type BrowsingContextInfo = {
context: BrowsingContext;
parent?: BrowsingContext;
url: string;
children: BrowsingContextInfoList;
};

// `browsingContext.create`
export type BrowsingContextCreateCommand = {
method: 'browsingContext.create';
params: BrowsingContextCreateParameters;
};

export type BrowsingContextCreateType = 'tab' | 'window';

export type BrowsingContextCreateParameters = {
type: BrowsingContextCreateType;
};
export type BrowsingContextCreateResult = {
context: BrowsingContext;
};
}

export namespace Session {
export type SessionStatusCommand = {
method: 'session.status';
params: CommonDataTypes.EmptyParams;
};

export type SessionStatusResult = {
ready: boolean;
message: string;
};
}
45 changes: 31 additions & 14 deletions src/bidiMapper/commandProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { BrowsingContextProcessor } from './domains/context/browsingContextProce
import { Context } from './domains/context/context';
import { Protocol } from 'devtools-protocol';
import { BidiCommandMessage, IBidiServer } from './utils/bidiServer';
import { Script } from './bidiProtocolTypes';
import { BrowsingContext, Script, Session } from './bidiProtocolTypes';

export class CommandProcessor {
private _browserCdpClient: CdpClient;
Expand Down Expand Up @@ -114,16 +114,25 @@ export class CommandProcessor {
this._bidiServer.sendMessage(errorResponse);
}

private _targetToContext(target: Protocol.Target.TargetInfo) {
private _targetToContext(
target: Protocol.Target.TargetInfo
): BrowsingContext.BrowsingContextInfo {
return {
context: target.targetId,
parent: target.openerId ? target.openerId : null,
parent: target.openerId ? target.openerId : undefined,
url: target.url,
// TODO sadym: implement.
children: [],
};
}

private async _process_browsingContext_getTree(params: object) {
private async _process_browsingContext_getTree(
commandData: BrowsingContext.BrowsingContextGetTreeCommand
): Promise<BrowsingContext.BrowsingContextGetTreeResult> {
const { targetInfos } = await this._browserCdpClient.Target.getTargets();
// TODO sadym: implement.
if (commandData.params.maxDepth || commandData.params.parent)
throw new Error('not implemented yet');
const contexts = targetInfos
// Don't expose any information about the tab with Mapper running.
.filter(this._isValidTarget)
Expand All @@ -138,7 +147,9 @@ export class CommandProcessor {
return {};
}

private _process_session_status = async function (params: object) {
private _process_session_status = async function (
commandData: Session.SessionStatusCommand
): Promise<Session.SessionStatusResult> {
return { ready: true, message: 'ready' };
};

Expand All @@ -148,6 +159,7 @@ export class CommandProcessor {
params: context.toBidi(),
});
}

private async _onContextDestroyed(context: Context) {
await this._bidiServer.sendMessage({
method: 'browsingContext.contextDestroyed',
Expand All @@ -158,21 +170,25 @@ export class CommandProcessor {
private async _processCommand(commandData: BidiCommandMessage) {
switch (commandData.method) {
case 'session.status':
return await this._process_session_status(commandData.params);
return await this._process_session_status(
commandData as Session.SessionStatusCommand
);
case 'browsingContext.getTree':
return await this._process_browsingContext_getTree(commandData.params);
return await this._process_browsingContext_getTree(
commandData as BrowsingContext.BrowsingContextGetTreeCommand
);
case 'script.evaluate':
return await this._contextProcessor.process_script_evaluate(
commandData.params as Script.ScriptEvaluateParameters
commandData as Script.ScriptEvaluateCommand
);

case 'PROTO.script.invoke':
return await this._contextProcessor.process_PROTO_script_invoke(
commandData.params as Script.PROTO.ScriptInvokeParameters
commandData as Script.PROTO.ScriptInvokeCommand
);
case 'PROTO.browsingContext.createContext':
case 'browsingContext.create':
return await this._contextProcessor.process_createContext(
commandData.params
commandData as BrowsingContext.BrowsingContextCreateCommand
);

case 'DEBUG.Page.close':
Expand All @@ -193,9 +209,10 @@ export class CommandProcessor {
};

this._bidiServer.sendMessage(response);
} catch (e: any) {
console.error(e);
this._respondWithError(message, 'unknown error', e.message);
} catch (e) {
const error = e as Error;
console.error(error);
this._respondWithError(message, 'unknown error', error.message);
}
};
}
15 changes: 10 additions & 5 deletions src/bidiMapper/domains/context/browsingContextProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import { log } from '../../../utils/log';
import { CdpConnection } from '../../../cdp';
import { Context } from './context';
import { Script } from '../../bidiProtocolTypes';
import { BrowsingContext, Script } from '../../bidiProtocolTypes';
import Protocol from 'devtools-protocol';

const logContext = log('context');
Expand Down Expand Up @@ -122,7 +122,10 @@ export class BrowsingContextProcessor {
}
}

async process_createContext(params: any): Promise<any> {
async process_createContext(
commandData: BrowsingContext.BrowsingContextCreateCommand
): Promise<BrowsingContext.BrowsingContextCreateResult> {
const params = commandData.params;
return new Promise(async (resolve) => {
let targetId: string;

Expand All @@ -147,15 +150,16 @@ export class BrowsingContextProcessor {
browserCdpClient.Target.on('attachedToTarget', onAttachedToTarget);

const result = await browserCdpClient.Target.createTarget({
url: params.url,
url: 'about:blank',
});
targetId = result.targetId;
});
}

async process_script_evaluate(
params: Script.ScriptEvaluateParameters
commandData: Script.ScriptEvaluateCommand
): Promise<Script.ScriptEvaluateResult> {
const params = commandData.params;
const context = this._getKnownContext(
(params.target as Script.ContextTarget).context
);
Expand All @@ -166,8 +170,9 @@ export class BrowsingContextProcessor {
}

async process_PROTO_script_invoke(
params: Script.PROTO.ScriptInvokeParameters
commandData: Script.PROTO.ScriptInvokeCommand
): Promise<Script.PROTO.ScriptInvokeResult> {
const params = commandData.params;
const context = this._getKnownContext(
(params.target as Script.ContextTarget).context
);
Expand Down
22 changes: 16 additions & 6 deletions tests/test_bidi.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,19 +153,29 @@ async def test_getTree_contextReturned(websocket):
"result": {
"contexts": [{
"context": contextID,
"parent": None,
"children": [],
"url": "about:blank"}]}}

@pytest.mark.asyncio
# Not implemented yet.
async def ignore_test_getTreeWithGivenParent_contextReturned(websocket):
ignore = True
# TODO sadym: implement

@pytest.mark.asyncio
# Not implemented yet.
async def ignore_test_getTreeWithNestedContexts_contextReturned(websocket):
ignore = True
# TODO sadym: implement

@pytest.mark.asyncio
# Not implemented yet.
async def _ignore_test_createContext_eventContextCreatedEmittedAndContextCreated(websocket):
# Send command.
command = {
"id": 9,
"method": "PROTO.browsingContext.createContext",
"params": {
"url": "data:text/html,<h2>test</h2>"}}
"method": "browsingContext.create",
"params": {}}
await send_JSON_command(websocket, command)

# Assert "browsingContext.contextCreated" event emitted.
Expand All @@ -176,7 +186,7 @@ async def _ignore_test_createContext_eventContextCreatedEmittedAndContextCreated
"params": {
"context":contextID,
"parent": None,
"url": "data:text/html,<h2>test</h2>"}}
"url": ""}}

# Assert command done.
resp = await read_JSON_message(websocket)
Expand All @@ -185,7 +195,7 @@ async def _ignore_test_createContext_eventContextCreatedEmittedAndContextCreated
"result": {
"context": contextID,
"parent": None,
"url": "data:text/html,<h2>test</h2>"}}
"url": ""}}

@pytest.mark.asyncio
# Not implemented yet.
Expand Down

0 comments on commit 896600f

Please sign in to comment.