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 @@ ...@@ -13,7 +13,7 @@
var scriptURL = 'http://127.0.0.1:8000/devtools/service-workers/resources/service-worker-empty.js'; 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/'; 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); var message = JSON.parse(messageString);
if (!messageString.includes('Target.sendMessageToTarget')) if (!messageString.includes('Target.sendMessageToTarget'))
return; return;
......
...@@ -74,7 +74,7 @@ Audits2.ProtocolService = class extends Common.Object { ...@@ -74,7 +74,7 @@ Audits2.ProtocolService = class extends Common.Object {
* @param {string} message * @param {string} message
*/ */
_sendProtocolMessage(message) { _sendProtocolMessage(message) {
this._rawConnection.sendMessage(message); this._rawConnection.sendRawMessage(message);
} }
/** /**
......
...@@ -180,22 +180,37 @@ Protocol.InspectorBackend.DevToolsStubErrorCode = -32015; ...@@ -180,22 +180,37 @@ Protocol.InspectorBackend.DevToolsStubErrorCode = -32015;
Protocol.inspectorBackend = new Protocol.InspectorBackend(); 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 * @param {string} message
*/ */
sendMessage(message) {}, sendRawMessage(message) {}
/** /**
* @return {!Promise} * @return {!Promise}
*/ */
disconnect() {}, disconnect() {}
}; };
/**
* @typedef {!{
* id: number,
* method: string,
* params: (!Object|undefined)
* }}
*/
Protocol.InspectorBackend.Connection.MessageObject;
/** /**
* @typedef {!{ * @typedef {!{
* onMessage: function((!Object|string)), * onMessage: function((!Object|string)),
...@@ -287,17 +302,16 @@ Protocol.TargetBase = class extends Common.Object { ...@@ -287,17 +302,16 @@ Protocol.TargetBase = class extends Common.Object {
messageObject.params = params; messageObject.params = params;
const wrappedCallback = this._wrap(callback, domain, method); const wrappedCallback = this._wrap(callback, domain, method);
const message = JSON.stringify(messageObject);
if (Protocol.InspectorBackend.Options.dumpInspectorProtocolMessages) 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)) { if (this.hasEventListeners(Protocol.TargetBase.Events.MessageSent)) {
this.dispatchEventToListeners( this.dispatchEventToListeners(
Protocol.TargetBase.Events.MessageSent, Protocol.TargetBase.Events.MessageSent,
{domain, method, params: JSON.parse(JSON.stringify(params)), id: messageId}); {domain, method, params: JSON.parse(JSON.stringify(params)), id: messageId});
} }
this._connection.sendMessage(message); this._connection.sendMessage(domain, messageObject);
++this._pendingResponsesCount; ++this._pendingResponsesCount;
this._callbacks[messageId] = wrappedCallback; this._callbacks[messageId] = wrappedCallback;
} }
......
// Copyright (c) 2015 The Chromium Authors. All rights reserved. // Copyright (c) 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
/** /**
* @implements {Protocol.InspectorBackend.Connection}
* @unrestricted * @unrestricted
*/ */
SDK.MainConnection = class { SDK.MainConnection = class extends Protocol.InspectorBackend.Connection {
/** /**
* @param {!Protocol.InspectorBackend.Connection.Params} params * @param {!Protocol.InspectorBackend.Connection.Params} params
*/ */
constructor(params) { constructor(params) {
super();
this._onMessage = params.onMessage; this._onMessage = params.onMessage;
this._onDisconnect = params.onDisconnect; this._onDisconnect = params.onDisconnect;
this._disconnected = false; this._disconnected = false;
...@@ -25,7 +26,7 @@ SDK.MainConnection = class { ...@@ -25,7 +26,7 @@ SDK.MainConnection = class {
* @override * @override
* @param {string} message * @param {string} message
*/ */
sendMessage(message) { sendRawMessage(message) {
if (!this._disconnected) if (!this._disconnected)
InspectorFrontendHost.sendMessageToBackend(message); InspectorFrontendHost.sendMessageToBackend(message);
} }
...@@ -77,16 +78,16 @@ SDK.MainConnection = class { ...@@ -77,16 +78,16 @@ SDK.MainConnection = class {
}; };
/** /**
* @implements {Protocol.InspectorBackend.Connection}
* @unrestricted * @unrestricted
*/ */
SDK.WebSocketConnection = class { SDK.WebSocketConnection = class extends Protocol.InspectorBackend.Connection {
/** /**
* @param {string} url * @param {string} url
* @param {function()} onWebSocketDisconnect * @param {function()} onWebSocketDisconnect
* @param {!Protocol.InspectorBackend.Connection.Params} params * @param {!Protocol.InspectorBackend.Connection.Params} params
*/ */
constructor(url, onWebSocketDisconnect, params) { constructor(url, onWebSocketDisconnect, params) {
super();
this._socket = new WebSocket(url); this._socket = new WebSocket(url);
this._socket.onerror = this._onError.bind(this); this._socket.onerror = this._onError.bind(this);
this._socket.onopen = this._onOpen.bind(this); this._socket.onopen = this._onOpen.bind(this);
...@@ -137,7 +138,7 @@ SDK.WebSocketConnection = class { ...@@ -137,7 +138,7 @@ SDK.WebSocketConnection = class {
* @override * @override
* @param {string} message * @param {string} message
*/ */
sendMessage(message) { sendRawMessage(message) {
if (this._connected) if (this._connected)
this._socket.send(message); this._socket.send(message);
else else
...@@ -160,14 +161,14 @@ SDK.WebSocketConnection = class { ...@@ -160,14 +161,14 @@ SDK.WebSocketConnection = class {
}; };
/** /**
* @implements {Protocol.InspectorBackend.Connection}
* @unrestricted * @unrestricted
*/ */
SDK.StubConnection = class { SDK.StubConnection = class extends Protocol.InspectorBackend.Connection {
/** /**
* @param {!Protocol.InspectorBackend.Connection.Params} params * @param {!Protocol.InspectorBackend.Connection.Params} params
*/ */
constructor(params) { constructor(params) {
super();
this._onMessage = params.onMessage; this._onMessage = params.onMessage;
this._onDisconnect = params.onDisconnect; this._onDisconnect = params.onDisconnect;
} }
...@@ -176,7 +177,7 @@ SDK.StubConnection = class { ...@@ -176,7 +177,7 @@ SDK.StubConnection = class {
* @override * @override
* @param {string} message * @param {string} message
*/ */
sendMessage(message) { sendRawMessage(message) {
setTimeout(this._respondWithError.bind(this, message), 0); setTimeout(this._respondWithError.bind(this, message), 0);
} }
...@@ -205,16 +206,14 @@ SDK.StubConnection = class { ...@@ -205,16 +206,14 @@ SDK.StubConnection = class {
} }
}; };
/** SDK.ChildConnection = class extends Protocol.InspectorBackend.Connection {
* @implements {Protocol.InspectorBackend.Connection}
*/
SDK.ChildConnection = class {
/** /**
* @param {!Protocol.TargetAgent} agent * @param {!Protocol.TargetAgent} agent
* @param {string} sessionId * @param {string} sessionId
* @param {!Protocol.InspectorBackend.Connection.Params} params * @param {!Protocol.InspectorBackend.Connection.Params} params
*/ */
constructor(agent, sessionId, params) { constructor(agent, sessionId, params) {
super();
this._agent = agent; this._agent = agent;
this._sessionId = sessionId; this._sessionId = sessionId;
this.onMessage = params.onMessage; this.onMessage = params.onMessage;
...@@ -225,7 +224,7 @@ SDK.ChildConnection = class { ...@@ -225,7 +224,7 @@ SDK.ChildConnection = class {
* @override * @override
* @param {string} message * @param {string} message
*/ */
sendMessage(message) { sendRawMessage(message) {
this._agent.sendMessageToTarget(message, this._sessionId); this._agent.sendMessageToTarget(message, this._sessionId);
} }
......
...@@ -190,7 +190,7 @@ SDKTestRunner.PageMock = class { ...@@ -190,7 +190,7 @@ SDKTestRunner.PageMock = class {
return true; return true;
} }
_dispatch(id, methodName, params, message) { _dispatch(id, methodName, params) {
const handler = (this._isSupportedDomain(methodName) ? this._dispatchMap[methodName] : null); const handler = (this._isSupportedDomain(methodName) ? this._dispatchMap[methodName] : null);
if (handler) if (handler)
...@@ -230,9 +230,8 @@ MockPageConnection = class { ...@@ -230,9 +230,8 @@ MockPageConnection = class {
setTimeout(() => this._onMessage.call(null, JSON.stringify(message)), 0); setTimeout(() => this._onMessage.call(null, JSON.stringify(message)), 0);
} }
sendMessage(message) { sendMessage(domain, message) {
const json = JSON.parse(message); this._page._dispatch(message.id, message.method, message.params);
this._page._dispatch(json.id, json.method, json.params, message);
} }
disconnect() { 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