Skip to content

Commit

Permalink
Devtools: Split InspectorBackend into two files
Browse files Browse the repository at this point in the history
BUG=

Review URL: https://codereview.chromium.org/1514333004

Cr-Commit-Position: refs/heads/master@{#368690}
  • Loading branch information
sergeyv authored and Commit bot committed Jan 11, 2016
1 parent 0f411ee commit afcece0
Show file tree
Hide file tree
Showing 6 changed files with 285 additions and 315 deletions.
1 change: 1 addition & 0 deletions devtools.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@
'front_end/main/renderingOptions.css',
'front_end/main/targetCrashedScreen.css',
'front_end/main/AdvancedApp.js',
'front_end/main/Connections.js',
'front_end/main/FrontendWebSocketAPI.js',
'front_end/main/Main.js',
'front_end/main/OverlayController.js',
Expand Down
143 changes: 143 additions & 0 deletions front_end/main/Connections.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
// 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.

/**
* @constructor
* @extends {InspectorBackendClass.Connection}
*/
WebInspector.MainConnection = function()
{
InspectorBackendClass.Connection.call(this);
InspectorFrontendHost.events.addEventListener(InspectorFrontendHostAPI.Events.DispatchMessage, this._dispatchMessage, this);
InspectorFrontendHost.events.addEventListener(InspectorFrontendHostAPI.Events.DispatchMessageChunk, this._dispatchMessageChunk, this);
}

WebInspector.MainConnection.prototype = {
/**
* @override
* @param {!Object} messageObject
*/
sendMessage: function(messageObject)
{
var message = JSON.stringify(messageObject);
InspectorFrontendHost.sendMessageToBackend(message);
},

/**
* @param {!WebInspector.Event} event
*/
_dispatchMessage: function(event)
{
this.dispatch(/** @type {string} */ (event.data));
},

/**
* @param {!WebInspector.Event} event
*/
_dispatchMessageChunk: function(event)
{
var messageChunk = /** @type {string} */ (event.data["messageChunk"]);
var messageSize = /** @type {number} */ (event.data["messageSize"]);
if (messageSize) {
this._messageBuffer = "";
this._messageSize = messageSize;
}
this._messageBuffer += messageChunk;
if (this._messageBuffer.length === this._messageSize) {
this.dispatch(this._messageBuffer);
this._messageBuffer = "";
this._messageSize = 0;
}
},

__proto__: InspectorBackendClass.Connection.prototype
}

/**
* @constructor
* @extends {InspectorBackendClass.Connection}
* @param {string} url
* @param {function(!InspectorBackendClass.Connection)} onConnectionReady
*/
WebInspector.WebSocketConnection = function(url, onConnectionReady)
{
InspectorBackendClass.Connection.call(this);
this._socket = new WebSocket(url);
this._socket.onmessage = this._onMessage.bind(this);
this._socket.onerror = this._onError.bind(this);
this._socket.onopen = onConnectionReady.bind(null, this);
this._socket.onclose = this.connectionClosed.bind(this, "websocket_closed");
}

/**
* @param {string} url
* @param {function(!InspectorBackendClass.Connection)} onConnectionReady
*/
WebInspector.WebSocketConnection.Create = function(url, onConnectionReady)
{
new WebInspector.WebSocketConnection(url, onConnectionReady);
}

WebInspector.WebSocketConnection.prototype = {

/**
* @param {!MessageEvent} message
*/
_onMessage: function(message)
{
var data = /** @type {string} */ (message.data);
this.dispatch(data);
},

/**
* @param {!Event} error
*/
_onError: function(error)
{
console.error(error);
},

/**
* @override
* @param {!Object} messageObject
*/
sendMessage: function(messageObject)
{
var message = JSON.stringify(messageObject);
this._socket.send(message);
},

__proto__: InspectorBackendClass.Connection.prototype
}

/**
* @constructor
* @extends {InspectorBackendClass.Connection}
*/
WebInspector.StubConnection = function()
{
InspectorBackendClass.Connection.call(this);
}

WebInspector.StubConnection.prototype = {
/**
* @override
* @param {!Object} messageObject
*/
sendMessage: function(messageObject)
{
setTimeout(this._respondWithError.bind(this, messageObject), 0);
},

/**
* @param {!Object} messageObject
*/
_respondWithError: function(messageObject)
{
var error = { message: "This is a stub connection, can't dispatch message.", code: InspectorBackendClass.DevToolsStubErrorCode, data: messageObject };
this.dispatch({ id: messageObject.id, error: error });
},

__proto__: InspectorBackendClass.Connection.prototype
}
8 changes: 4 additions & 4 deletions front_end/main/Main.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,16 +281,16 @@ WebInspector.Main.prototype = {

if (Runtime.queryParam("ws")) {
var ws = "ws://" + Runtime.queryParam("ws");
InspectorBackendClass.WebSocketConnection.Create(ws, this._connectionEstablished.bind(this));
WebInspector.WebSocketConnection.Create(ws, this._connectionEstablished.bind(this));
return;
}

if (!InspectorFrontendHost.isHostedMode()) {
this._connectionEstablished(new InspectorBackendClass.MainConnection());
this._connectionEstablished(new WebInspector.MainConnection());
return;
}

this._connectionEstablished(new InspectorBackendClass.StubConnection());
this._connectionEstablished(new WebInspector.StubConnection());
},

/**
Expand Down Expand Up @@ -780,7 +780,7 @@ WebInspector.Main._addWebSocketTarget = function(ws)
{
WebInspector.targetManager.createTarget(ws, WebInspector.Target.Type.Page, connection, null);
}
new InspectorBackendClass.WebSocketConnection(ws, callback);
new WebInspector.WebSocketConnection(ws, callback);
}

/**
Expand Down
1 change: 1 addition & 0 deletions front_end/main/module.json
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@
"SimpleApp.js",
"Tests.js",
"OverlayController.js",
"Connections.js",
"Main.js"
],
"skip_compilation": [
Expand Down
Loading

0 comments on commit afcece0

Please sign in to comment.