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

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: rebaselined 2 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 this._dispatchMessage(data);
40 } catch(e) {
41 console.error(e);
42 }
43 },
44
45 _dispatchMessage: function(data)
46 {
47 var message = JSON.parse(data);
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) );
dgozman 2016/09/27 22:25:40 Catch this one as well.
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 " + data);
64 return;
65 }
66 this._objects.delete(message.params.id);
67 object.dispose().then(() => this._sendResponse(message.id));
dgozman 2016/09/27 22:25:40 Catch this one as well.
68 } else {
69 if (!message.params) {
70 console.error("No params in the message: " + data);
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 " + data);
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 try {
84 object[method](message.params).then(result => this._sendResponse (message.id, result));
85 } catch (e) {
dgozman 2016/09/27 22:25:40 Should also have promise.catch.
86 this._sendErrorResponse(message.id, e.toString());
87 }
88 }
89 },
90
91 _connectionClosed: function()
92 {
93 for (var object of this._objects.values())
94 object.dispose();
95 this._objects.clear();
96 },
97
98 _notify: function(objectId, objectName, method, params)
99 {
100 params["id"] = objectId;
101 var message = { method: objectName + "." + method, params: params };
102 this._socket.send(JSON.stringify(message));
103 },
104
105 _sendResponse: function(messageId, result)
106 {
107 var message = { id: messageId, result: result };
108 this._socket.send(JSON.stringify(message));
109 },
110
111 _sendErrorResponse: function(messageId, error)
112 {
113 var message = { id: messageId, error: error };
114 this._socket.send(JSON.stringify(message));
115 },
116 }
117
118 exports.Dispatcher = Dispatcher;
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698