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

Side by Side 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, 2 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 var http = require("http");
6 var ws = require("ws");
7
8 function Dispatcher()
9 {
10 this._constructors = new Map();
11 this._objects = new Map();
12 this._lastObjectId = 1;
13 }
14
15 Dispatcher.prototype = {
16 start: function(port)
17 {
18 var http_server = http.createServer();
19 http_server.listen(port);
20
21 var WebSocketServer = ws.Server;
22 var options = { server: http_server, path: "/endpoint" };
23 var wss = new WebSocketServer(options);
24 wss.on("connection", (socket) => {
25 this._socket = socket;
26 this._socket.on("message", this._dispatchMessageWrapped.bind(this));
27 this._socket.on("close", this._connectionClosed.bind(this));
28 });
29 },
30
31 registerObject: function(name, constructor)
32 {
33 this._constructors.set(name, constructor);
34 },
35
36 _dispatchMessageWrapped: function(data)
37 {
38 try {
39 var message = JSON.parse(data);
40 this._dispatchMessage(message);
41 } catch(e) {
42 this._sendErrorResponse(message.id, e.toString());
43 }
44 },
45
46 _dispatchMessage: function(message)
47 {
48 var [objectName, method] = message.method.split(".");
49 var result = JSON.stringify({id: message.id});
50 var constructor = this._constructors.get(objectName);
51 if (!constructor) {
52 this._sendErrorResponse(message.id, "Could not resolve service '" + objectName + "'");
53 return;
54 }
55 if (method === "create") {
56 var id = String(this._lastObjectId++);
57 var object = new constructor(this._notify.bind(this, id, objectName) );
58 this._objects.set(id, object);
59 this._sendResponse(message.id, { id: id });
60 } else if (method === "dispose") {
61 var object = this._objects.get(message.params.id);
62 if (!object) {
63 console.error("Could not look up object with id for " + JSON.str ingify(message));
64 return;
65 }
66 this._objects.delete(message.params.id);
67 object.dispose().then(() => this._sendResponse(message.id));
68 } else {
69 if (!message.params) {
70 console.error("No params in the message: " + JSON.stringify(mess age));
71 return;
72 }
73 var object = this._objects.get(message.params.id);
74 if (!object) {
75 console.error("Could not look up object with id for " + JSON.str ingify(message));
76 return;
77 }
78 var handler = object[method];
79 if (!(handler instanceof Function)) {
80 console.error("Handler for '" + method + "' is missing.");
81 return;
82 }
83 object[method](message.params).then(result => this._sendResponse(mes sage.id, result));
84 }
85 },
86
87 _connectionClosed: function()
88 {
89 for (var object of this._objects.values())
90 object.dispose();
91 this._objects.clear();
92 },
93
94 _notify: function(objectId, objectName, method, params)
95 {
96 params["id"] = objectId;
97 var message = { method: objectName + "." + method, params: params };
98 this._socket.send(JSON.stringify(message));
99 },
100
101 _sendResponse: function(messageId, result)
102 {
103 var message = { id: messageId, result: result };
104 this._socket.send(JSON.stringify(message));
105 },
106
107 _sendErrorResponse: function(messageId, error)
108 {
109 var message = { id: messageId, error: error };
110 this._socket.send(JSON.stringify(message));
111 },
112 }
113
114 exports.Dispatcher = Dispatcher;
OLDNEW
« 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