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

Side by Side Diff: third_party/WebKit/Source/devtools/front_end/services/ServiceManager.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 /**
6 * @constructor
7 */
8 WebInspector.ServiceManager = function()
9 {
10 this._lastId = 1;
11 /** @type {!Map<number, function(?Object)>}*/
12 this._callbacks = new Map();
13 /** @type {!Map<string, !WebInspector.ServiceManager.Service>}*/
14 this._services = new Map();
15 }
16
17 WebInspector.ServiceManager.prototype = {
18 /**
19 * @param {string} serviceName
20 * @return {!Promise<?WebInspector.ServiceManager.Service>}
21 */
22 createService: function(serviceName)
23 {
24 return this._sendCommand(serviceName + ".create").then(result => {
25 if (!result)
26 return null;
27 var service = new WebInspector.ServiceManager.Service(this, serviceN ame, result.id);
28 this._services.set(serviceName + ":" + result.id, service);
29 return service;
30 });
31 },
32
33 /**
34 * @param {string} method
35 * @param {!Object=} params
36 * @return {!Promise<?Object>}
37 */
38 _sendCommand: function(method, params)
39 {
40 var id = this._lastId++;
41 var message = JSON.stringify({id: id, method: method, params: params || {}});
42 this._connect().then(() => this._socket ? this._socket.send(message) : t his._callbacks.get(id)(null));
43 return new Promise(fulfill => this._callbacks.set(id, fulfill));
44 },
45
46 /**
47 * @return {!Promise}
48 */
49 _connect: function()
50 {
51 var url = Runtime.queryParam("service-backend");
52 if (!url) {
53 console.error("No endpoint address specified");
54 return Promise.resolve(null);
55 }
56
57 if (!this._connectionPromise)
58 this._connectionPromise = new Promise(promiseBody.bind(this));
59 return this._connectionPromise;
60
61 /**
62 * @param {function()} fulfill
63 * @param {function()} reject
64 * @this {WebInspector.ServiceManager}
65 */
66 function promiseBody(fulfill, reject)
67 {
68 var socket = new WebSocket(/** @type {string} */(url));
69 socket.onmessage = this._onMessage.bind(this);
70 socket.onopen = this._connectionOpened.bind(this, socket, fulfill);
71 socket.onclose = this._connectionClosed.bind(this);
72 }
73 },
74
75 /**
76 * @param {!MessageEvent} message
77 */
78 _onMessage: function(message)
79 {
80 var data = /** @type {string} */ (message.data);
81 var object;
82 try {
83 object = JSON.parse(data);
84 } catch (e) {
85 console.error(e);
86 return;
87 }
88 if (object.id) {
89 if (object.error)
90 console.error("Service error: " + object.error);
91 this._callbacks.get(object.id)(object.error ? null : object.result);
92 this._callbacks.delete(object.id);
93 return;
94 }
95
96 var tokens = object.method.split(".");
97 var serviceName = tokens[0];
98 var methodName = tokens[1];
99 var service = this._services.get(serviceName + ":" + object.params.id);
100 if (!service) {
101 console.error("Unable to lookup stub for " + serviceName + ":" + obj ect.params.id);
102 return;
103 }
104 service._dispatchNotification(methodName, object.params);
105 },
106
107 /**
108 * @param {!WebSocket} socket
109 * @param {function()} callback
110 */
111 _connectionOpened: function(socket, callback)
112 {
113 this._socket = socket;
114 callback();
115 },
116
117 _connectionClosed: function()
118 {
119 for (var callback of this._callbacks.values())
120 callback(null);
121 this._callbacks.clear();
122 for (var service of this._services.values())
123 service._dispatchNotification("disposed");
124 this._services.clear();
125 delete this._connectionPromise;
126 }
127 }
128
129 /**
130 * @constructor
131 * @param {!WebInspector.ServiceManager} manager
132 * @param {string} serviceName
133 * @param {string} objectId
134 */
135 WebInspector.ServiceManager.Service = function(manager, serviceName, objectId)
136 {
137 this._manager = manager;
138 this._serviceName = serviceName;
139 this._objectId = objectId;
140 /** @type {!Map<string, function(!Object=)>}*/
141 this._notificationHandlers = new Map();
142 }
143
144 WebInspector.ServiceManager.Service.prototype = {
145 /**
146 * @return {!Promise}
147 */
148 dispose: function()
149 {
150 var params = { id: this._objectId };
151 this._manager._services.delete(this._serviceName + ":" + this._objectId) ;
152 return this._manager._sendCommand(this._serviceName + ".dispose", params );
153 },
154
155 /**
156 * @param {string} methodName
157 * @param {function(!Object=)} handler
158 */
159 on: function(methodName, handler)
160 {
161 this._notificationHandlers.set(methodName, handler);
162 },
163
164 /**
165 * @param {string} methodName
166 * @param {!Object=} params
167 * @return {!Promise}
168 */
169 send: function(methodName, params)
170 {
171 params = params || {};
172 params.id = this._objectId;
173 return this._manager._sendCommand(this._serviceName + "." + methodName, params);
174 },
175
176 /**
177 * @param {string} methodName
178 * @param {!Object=} params
179 */
180 _dispatchNotification: function(methodName, params)
181 {
182 var handler = this._notificationHandlers.get(methodName);
183 if (!handler) {
184 console.error("Could not report notification '" + methodName + "' on '" + this._objectId + "'");
185 return;
186 }
187 handler(params);
188 }
189 }
190
191 WebInspector.serviceManager = new WebInspector.ServiceManager();
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698