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

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: 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 /**
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 lookupService: function(serviceName)
dgozman 2016/09/27 21:01:34 I'd say lookupService would return existing instan
pfeldman 2016/09/27 21:48:59 Done.
23 {
24 return this._sendCommand(serviceName + ".create").then(result => {
25 if (!result)
26 return null;
dgozman 2016/09/27 21:01:34 This is not compiled, because jscompiler requires
pfeldman 2016/09/27 21:48:59 It did compile though :)
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 socket.onerror = console.error.bind(console);
73 }
74 },
75
76 /**
77 * @param {!MessageEvent} message
78 */
79 _onMessage: function(message)
80 {
81 var data = /** @type {string} */ (message.data);
82 var object = JSON.parse(data);
dgozman 2016/09/27 21:01:34 try catch this
pfeldman 2016/09/27 21:48:59 Done.
83 if (object.id) {
84 if (object.error)
85 console.error("Service error: " + object.error);
86 this._callbacks.get(object.id)(object.error ? null : object.result);
87 this._callbacks.delete(object.id);
88 return;
89 }
90
91 var tokens = object.method.split(".");
92 var serviceName = tokens[0];
93 var methodName = tokens[1];
94 var service = this._services.get(serviceName + ":" + object.params.id);
95 if (!service) {
96 console.error("Unable to lookup stub for " + serviceName + ":" + obj ect.params.id);
97 return;
98 }
99 service._dispatchNotification(methodName, object.params);
100 },
101
102 /**
103 * @param {!WebSocket} socket
104 * @param {function()} callback
105 */
106 _connectionOpened: function(socket, callback)
107 {
108 this._socket = socket;
109 callback();
110 },
111
112 _connectionClosed: function()
113 {
114 for (var callback of this._callbacks.values())
115 callback(null);
116 this._callbacks.clear();
117 for (var service of this._services.values())
118 service._dispatchNotification("disposed");
119 this._services.clear();
120 delete this._connectionPromise;
121 }
122 }
123
124 /**
125 * @constructor
126 * @param {!WebInspector.ServiceManager} manager
127 * @param {string} serviceName
128 * @param {string} objectId
129 */
130 WebInspector.ServiceManager.Service = function(manager, serviceName, objectId)
131 {
132 this._manager = manager;
133 this._serviceName = serviceName;
134 this._objectId = objectId;
135 /** @type {!Map<string, function(!Object=)>}*/
136 this._notificationHandlers = new Map();
137 }
138
139 WebInspector.ServiceManager.Service.prototype = {
140 /**
141 * @return {!Promise}
142 */
143 dispose: function()
144 {
145 var params = { id: this._objectId };
146 this._manager._services.delete(this._serviceName + ":" + this._objectId) ;
147 return this._manager._sendCommand(this._serviceName + ".dispose", params );
148 },
149
150 /**
151 * @param {string} methodName
152 * @param {function(!Object=)} handler
153 */
154 on: function(methodName, handler)
155 {
156 this._notificationHandlers.set(methodName, handler);
157 },
158
159 /**
160 * @param {string} methodName
161 * @param {!Object=} params
162 * @return {!Promise}
163 */
164 send: function(methodName, params)
165 {
166 params = params || {};
167 params.id = this._objectId;
168 return this._manager._sendCommand(this._serviceName + "." + methodName, params);
169 },
170
171 /**
172 * @param {string} methodName
173 * @param {!Object=} params
174 */
175 _dispatchNotification: function(methodName, params)
176 {
177 var handler = this._notificationHandlers.get(methodName);
178 if (!handler) {
179 console.error("Could not report notification '" + methodName + "' on '" + this._objectId + "'");
180 return;
181 }
182 handler(params);
183 }
184 }
185
186 WebInspector.serviceManager = new WebInspector.ServiceManager();
dgozman 2016/09/27 21:01:34 Let's make it lazy instance().
pfeldman 2016/09/27 21:48:59 It is lazy along with its module.
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698