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

Side by Side Diff: third_party/WebKit/Source/devtools/backend/dispatcher.js

Issue 2370573002: DevTools: enable front-end to use external services for additional capabilities. (Closed)
Patch Set: split into parts 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)
dgozman 2016/09/27 21:01:34 Should we adopt JSDoc here for future?
pfeldman 2016/09/27 21:48:59 We could, but it would not compiler requires above
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._dispatchMessage.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)
dgozman 2016/09/27 21:01:34 Unused.
pfeldman 2016/09/27 21:48:59 Done.
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();
58 this._objects.set(id, object);
59 object.setNotify(this._notify.bind(this, id, objectName));
dgozman 2016/09/27 21:01:34 Maybe just pass notify in constructor?
pfeldman 2016/09/27 21:48:59 Done.
60 this._sendResponse(message.id, { id: id });
61 } else if (method === "dispose") {
62 var object = this._objects.get(message.params.id);
63 if (!object) {
64 console.error("Could not look up object with id for " + data);
65 return;
66 }
67 this._objects.delete(message.params.id);
68 object.dispose().then(() => this._sendResponse(message.id));
69 } else {
70 if (!message.params) {
71 console.error("No params in the message: " + data);
72 return;
73 }
74 var object = this._objects.get(message.params.id);
75 if (!object) {
76 console.error("Could not look up object with id for " + data);
77 return;
78 }
79 var handler = object[method];
80 if (!(handler instanceof Function)) {
81 console.error("Handler for '" + method + "' is missing.");
82 return;
83 }
84 object[method](message.params).then(result => this._sendResponse(mes sage.id, result));
dgozman 2016/09/27 21:01:34 Let's catch calls into object and send error respo
pfeldman 2016/09/27 21:48:59 Done.
85 }
86 },
87
88 _connectionClosed: function()
89 {
90 for (var object of this._objects.values())
91 object.dispose();
92 this._objects.clear();
93 },
94
95 _notify: function(objectId, objectName, method, params)
96 {
97 params["id"] = objectId;
98 var message = { method: objectName + "." + method, params: params };
99 this._socket.send(JSON.stringify(message));
100 },
101
102 _sendResponse: function(messageId, result)
103 {
104 var message = { id: messageId, result: result };
105 this._socket.send(JSON.stringify(message));
106 },
107
108 _sendErrorResponse: function(messageId, error)
109 {
110 var message = { id: messageId, error: error };
111 this._socket.send(JSON.stringify(message));
112 },
113 }
114
115 exports.Dispatcher = Dispatcher;
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698