Commit c5584c2f authored by Alexey Kozyatinskiy's avatar Alexey Kozyatinskiy Committed by Commit Bot

[DevTools] introduce Connection.sendRawMessage

Some clients needs to intercept messages on frontend side.

BUG=none
R=pfeldman@chromium.org

Change-Id: Ia14cc0a88ae91eead3834a47276ddcfc6701bcf7
Reviewed-on: https://chromium-review.googlesource.com/1154036
Commit-Queue: Pavel Feldman <pfeldman@chromium.org>
Reviewed-by: default avatarPavel Feldman <pfeldman@chromium.org>
Cr-Commit-Position: refs/heads/master@{#578900}
parent 311d9e7b
......@@ -13,7 +13,7 @@
var scriptURL = 'http://127.0.0.1:8000/devtools/service-workers/resources/service-worker-empty.js';
var scope = 'http://127.0.0.1:8000/devtools/service-workers/resources/scope1/';
TestRunner.addSniffer(SDK.MainConnection.prototype, 'sendMessage', function(messageString) {
TestRunner.addSniffer(SDK.MainConnection.prototype, 'sendRawMessage', function(messageString) {
var message = JSON.parse(messageString);
if (!messageString.includes('Target.sendMessageToTarget'))
return;
......
......@@ -74,7 +74,7 @@ Audits2.ProtocolService = class extends Common.Object {
* @param {string} message
*/
_sendProtocolMessage(message) {
this._rawConnection.sendMessage(message);
this._rawConnection.sendRawMessage(message);
}
/**
......
......@@ -180,22 +180,37 @@ Protocol.InspectorBackend.DevToolsStubErrorCode = -32015;
Protocol.inspectorBackend = new Protocol.InspectorBackend();
/**
* @interface
* @unrestricted
*/
Protocol.InspectorBackend.Connection = function() {};
Protocol.InspectorBackend.Connection = class {
/**
* @param {string} domain
* @param {!Protocol.InspectorBackend.Connection.MessageObject} messageObject
*/
sendMessage(domain, messageObject) {
this.sendRawMessage(JSON.stringify(messageObject));
}
Protocol.InspectorBackend.Connection.prototype = {
/**
* @param {string} message
*/
sendMessage(message) {},
sendRawMessage(message) {}
/**
* @return {!Promise}
*/
disconnect() {},
disconnect() {}
};
/**
* @typedef {!{
* id: number,
* method: string,
* params: (!Object|undefined)
* }}
*/
Protocol.InspectorBackend.Connection.MessageObject;
/**
* @typedef {!{
* onMessage: function((!Object|string)),
......@@ -287,17 +302,16 @@ Protocol.TargetBase = class extends Common.Object {
messageObject.params = params;
const wrappedCallback = this._wrap(callback, domain, method);
const message = JSON.stringify(messageObject);
if (Protocol.InspectorBackend.Options.dumpInspectorProtocolMessages)
this._dumpProtocolMessage('frontend: ' + message, '[FE] ' + domain);
this._dumpProtocolMessage('frontend: ' + JSON.stringify(messageObject), '[FE] ' + domain);
if (this.hasEventListeners(Protocol.TargetBase.Events.MessageSent)) {
this.dispatchEventToListeners(
Protocol.TargetBase.Events.MessageSent,
{domain, method, params: JSON.parse(JSON.stringify(params)), id: messageId});
}
this._connection.sendMessage(message);
this._connection.sendMessage(domain, messageObject);
++this._pendingResponsesCount;
this._callbacks[messageId] = wrappedCallback;
}
......
// Copyright (c) 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/**
* @implements {Protocol.InspectorBackend.Connection}
* @unrestricted
*/
SDK.MainConnection = class {
SDK.MainConnection = class extends Protocol.InspectorBackend.Connection {
/**
* @param {!Protocol.InspectorBackend.Connection.Params} params
*/
constructor(params) {
super();
this._onMessage = params.onMessage;
this._onDisconnect = params.onDisconnect;
this._disconnected = false;
......@@ -25,7 +26,7 @@ SDK.MainConnection = class {
* @override
* @param {string} message
*/
sendMessage(message) {
sendRawMessage(message) {
if (!this._disconnected)
InspectorFrontendHost.sendMessageToBackend(message);
}
......@@ -77,16 +78,16 @@ SDK.MainConnection = class {
};
/**
* @implements {Protocol.InspectorBackend.Connection}
* @unrestricted
*/
SDK.WebSocketConnection = class {
SDK.WebSocketConnection = class extends Protocol.InspectorBackend.Connection {
/**
* @param {string} url
* @param {function()} onWebSocketDisconnect
* @param {!Protocol.InspectorBackend.Connection.Params} params
*/
constructor(url, onWebSocketDisconnect, params) {
super();
this._socket = new WebSocket(url);
this._socket.onerror = this._onError.bind(this);
this._socket.onopen = this._onOpen.bind(this);
......@@ -137,7 +138,7 @@ SDK.WebSocketConnection = class {
* @override
* @param {string} message
*/
sendMessage(message) {
sendRawMessage(message) {
if (this._connected)
this._socket.send(message);
else
......@@ -160,14 +161,14 @@ SDK.WebSocketConnection = class {
};
/**
* @implements {Protocol.InspectorBackend.Connection}
* @unrestricted
*/
SDK.StubConnection = class {
SDK.StubConnection = class extends Protocol.InspectorBackend.Connection {
/**
* @param {!Protocol.InspectorBackend.Connection.Params} params
*/
constructor(params) {
super();
this._onMessage = params.onMessage;
this._onDisconnect = params.onDisconnect;
}
......@@ -176,7 +177,7 @@ SDK.StubConnection = class {
* @override
* @param {string} message
*/
sendMessage(message) {
sendRawMessage(message) {
setTimeout(this._respondWithError.bind(this, message), 0);
}
......@@ -205,16 +206,14 @@ SDK.StubConnection = class {
}
};
/**
* @implements {Protocol.InspectorBackend.Connection}
*/
SDK.ChildConnection = class {
SDK.ChildConnection = class extends Protocol.InspectorBackend.Connection {
/**
* @param {!Protocol.TargetAgent} agent
* @param {string} sessionId
* @param {!Protocol.InspectorBackend.Connection.Params} params
*/
constructor(agent, sessionId, params) {
super();
this._agent = agent;
this._sessionId = sessionId;
this.onMessage = params.onMessage;
......@@ -225,7 +224,7 @@ SDK.ChildConnection = class {
* @override
* @param {string} message
*/
sendMessage(message) {
sendRawMessage(message) {
this._agent.sendMessageToTarget(message, this._sessionId);
}
......
......@@ -190,7 +190,7 @@ SDKTestRunner.PageMock = class {
return true;
}
_dispatch(id, methodName, params, message) {
_dispatch(id, methodName, params) {
const handler = (this._isSupportedDomain(methodName) ? this._dispatchMap[methodName] : null);
if (handler)
......@@ -230,9 +230,8 @@ MockPageConnection = class {
setTimeout(() => this._onMessage.call(null, JSON.stringify(message)), 0);
}
sendMessage(message) {
const json = JSON.parse(message);
this._page._dispatch(json.id, json.method, json.params, message);
sendMessage(domain, message) {
this._page._dispatch(message.id, message.method, message.params);
}
disconnect() {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment