From db4e856a572a7a4e23518fdb647da44032e642c9 Mon Sep 17 00:00:00 2001 From: Dmitry Gozman Date: Tue, 21 Jul 2020 15:58:00 -0700 Subject: [PATCH] feat(rpc): use SerializedValue for CDPSession (#3076) This is our way to define a schema for arbitrary values. --- src/rpc/channels.ts | 12 +++--------- src/rpc/client/cdpSession.ts | 11 ++++++++--- src/rpc/protocol.pdl | 6 +++--- src/rpc/server/cdpSessionDispatcher.ts | 13 +++++++++---- 4 files changed, 23 insertions(+), 19 deletions(-) diff --git a/src/rpc/channels.ts b/src/rpc/channels.ts index 9ee5fd81e9..ffa32805a0 100644 --- a/src/rpc/channels.ts +++ b/src/rpc/channels.ts @@ -1571,21 +1571,15 @@ export interface CDPSessionChannel extends Channel { } export type CDPSessionEventEvent = { method: string, - params?: { - - }, + params?: SerializedValue, }; export type CDPSessionDisconnectedEvent = {}; export type CDPSessionSendParams = { method: string, - params?: { - - }, + params?: SerializedValue, }; export type CDPSessionSendResult = { - result: { - - }, + result: SerializedValue, }; export type CDPSessionDetachParams = {}; export type CDPSessionDetachResult = void; diff --git a/src/rpc/client/cdpSession.ts b/src/rpc/client/cdpSession.ts index 467aaa22d4..60c4a1000d 100644 --- a/src/rpc/client/cdpSession.ts +++ b/src/rpc/client/cdpSession.ts @@ -17,6 +17,7 @@ import { CDPSessionChannel, CDPSessionInitializer } from '../channels'; import { ChannelOwner } from './channelOwner'; import { Protocol } from '../../chromium/protocol'; +import { parseResult, serializeArgument } from './jsHandle'; export class CDPSession extends ChannelOwner { static from(cdpSession: CDPSessionChannel): CDPSession { @@ -32,7 +33,10 @@ export class CDPSession extends ChannelOwner this.emit(method, params)); + this._channel.on('event', ({ method, params }) => { + const cdpParams = params ? parseResult(params) : undefined; + this.emit(method, cdpParams); + }); this._channel.on('disconnected', () => this._dispose()); this.on = super.on; @@ -47,8 +51,9 @@ export class CDPSession extends ChannelOwner { return this._wrapApiCall('cdpSession.send', async () => { - const result = await this._channel.send({ method, params }); - return result.result as Protocol.CommandReturnValues[T]; + const protocolParams = params ? serializeArgument(params).value : undefined; + const result = await this._channel.send({ method, params: protocolParams }); + return parseResult(result.result) as Protocol.CommandReturnValues[T]; }); } diff --git a/src/rpc/protocol.pdl b/src/rpc/protocol.pdl index bc559cbf52..83e9d774b0 100644 --- a/src/rpc/protocol.pdl +++ b/src/rpc/protocol.pdl @@ -1407,16 +1407,16 @@ interface CDPSession event event parameters method: string - params?: object + params?: SerializedValue event disconnected command send parameters method: string - params?: object + params?: SerializedValue returns - result: object + result: SerializedValue command detach diff --git a/src/rpc/server/cdpSessionDispatcher.ts b/src/rpc/server/cdpSessionDispatcher.ts index a7e1ad85d9..fd0d41af92 100644 --- a/src/rpc/server/cdpSessionDispatcher.ts +++ b/src/rpc/server/cdpSessionDispatcher.ts @@ -15,21 +15,26 @@ */ import { CRSession, CRSessionEvents } from '../../chromium/crConnection'; -import { CDPSessionChannel, CDPSessionInitializer } from '../channels'; +import { CDPSessionChannel, CDPSessionInitializer, SerializedValue } from '../channels'; import { Dispatcher, DispatcherScope } from './dispatcher'; +import { serializeResult, parseArgument } from './jsHandleDispatcher'; export class CDPSessionDispatcher extends Dispatcher implements CDPSessionChannel { constructor(scope: DispatcherScope, crSession: CRSession) { super(scope, crSession, 'cdpSession', {}, true); - crSession._eventListener = (method, params) => this._dispatchEvent('event', { method, params }); + crSession._eventListener = (method, cdpParams) => { + const params = cdpParams ? serializeResult(cdpParams) : undefined; + this._dispatchEvent('event', { method, params }); + }; crSession.on(CRSessionEvents.Disconnected, () => { this._dispatchEvent('disconnected'); this._dispose(); }); } - async send(params: { method: string, params?: Object }): Promise<{ result: Object }> { - return { result: await this._object.send(params.method as any, params.params) }; + async send(params: { method: string, params?: SerializedValue }): Promise<{ result: SerializedValue }> { + const cdpParams = params.params ? parseArgument({ value: params.params, handles: [] }) : undefined; + return { result: serializeResult(await this._object.send(params.method as any, cdpParams)) }; } async detach(): Promise {