Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(715)

Unified Diff: third_party/WebKit/Source/devtools/services/dispatcher.js

Issue 2370573002: DevTools: enable front-end to use external services for additional capabilities. (Closed)
Patch Set: review addressed Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/devtools/services/dispatcher.js
diff --git a/third_party/WebKit/Source/devtools/services/dispatcher.js b/third_party/WebKit/Source/devtools/services/dispatcher.js
new file mode 100644
index 0000000000000000000000000000000000000000..25355bddec01bb9544e068c9ea03f097dd4719c9
--- /dev/null
+++ b/third_party/WebKit/Source/devtools/services/dispatcher.js
@@ -0,0 +1,114 @@
+// Copyright 2016 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.
+
+var http = require("http");
+var ws = require("ws");
+
+function Dispatcher()
+{
+ this._constructors = new Map();
+ this._objects = new Map();
+ this._lastObjectId = 1;
+}
+
+Dispatcher.prototype = {
+ start: function(port)
+ {
+ var http_server = http.createServer();
+ http_server.listen(port);
+
+ var WebSocketServer = ws.Server;
+ var options = { server: http_server, path: "/endpoint" };
+ var wss = new WebSocketServer(options);
+ wss.on("connection", (socket) => {
+ this._socket = socket;
+ this._socket.on("message", this._dispatchMessageWrapped.bind(this));
+ this._socket.on("close", this._connectionClosed.bind(this));
+ });
+ },
+
+ registerObject: function(name, constructor)
+ {
+ this._constructors.set(name, constructor);
+ },
+
+ _dispatchMessageWrapped: function(data)
+ {
+ try {
+ var message = JSON.parse(data);
+ this._dispatchMessage(message);
+ } catch(e) {
+ this._sendErrorResponse(message.id, e.toString());
+ }
+ },
+
+ _dispatchMessage: function(message)
+ {
+ var [objectName, method] = message.method.split(".");
+ var result = JSON.stringify({id: message.id});
+ var constructor = this._constructors.get(objectName);
+ if (!constructor) {
+ this._sendErrorResponse(message.id, "Could not resolve service '" + objectName + "'");
+ return;
+ }
+ if (method === "create") {
+ var id = String(this._lastObjectId++);
+ var object = new constructor(this._notify.bind(this, id, objectName));
+ this._objects.set(id, object);
+ this._sendResponse(message.id, { id: id });
+ } else if (method === "dispose") {
+ var object = this._objects.get(message.params.id);
+ if (!object) {
+ console.error("Could not look up object with id for " + JSON.stringify(message));
+ return;
+ }
+ this._objects.delete(message.params.id);
+ object.dispose().then(() => this._sendResponse(message.id));
+ } else {
+ if (!message.params) {
+ console.error("No params in the message: " + JSON.stringify(message));
+ return;
+ }
+ var object = this._objects.get(message.params.id);
+ if (!object) {
+ console.error("Could not look up object with id for " + JSON.stringify(message));
+ return;
+ }
+ var handler = object[method];
+ if (!(handler instanceof Function)) {
+ console.error("Handler for '" + method + "' is missing.");
+ return;
+ }
+ object[method](message.params).then(result => this._sendResponse(message.id, result));
+ }
+ },
+
+ _connectionClosed: function()
+ {
+ for (var object of this._objects.values())
+ object.dispose();
+ this._objects.clear();
+ },
+
+ _notify: function(objectId, objectName, method, params)
+ {
+ params["id"] = objectId;
+ var message = { method: objectName + "." + method, params: params };
+ this._socket.send(JSON.stringify(message));
+ },
+
+ _sendResponse: function(messageId, result)
+ {
+ var message = { id: messageId, result: result };
+ this._socket.send(JSON.stringify(message));
+ },
+
+ _sendErrorResponse: function(messageId, error)
+ {
+ var message = { id: messageId, error: error };
+ this._socket.send(JSON.stringify(message));
+ },
+}
+
+exports.Dispatcher = Dispatcher;
« no previous file with comments | « third_party/WebKit/Source/devtools/services/devtools.js ('k') | third_party/WebKit/Source/devtools/services/package.json » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698