| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 /** | 4 /** |
| 5 * @unrestricted | 5 * @unrestricted |
| 6 */ | 6 */ |
| 7 WebInspector.ServiceManager = class { | 7 Services.ServiceManager = class { |
| 8 /** | 8 /** |
| 9 * @param {string} serviceName | 9 * @param {string} serviceName |
| 10 * @return {!Promise<?WebInspector.ServiceManager.Service>} | 10 * @return {!Promise<?Services.ServiceManager.Service>} |
| 11 */ | 11 */ |
| 12 createRemoteService(serviceName) { | 12 createRemoteService(serviceName) { |
| 13 if (!this._remoteConnection) { | 13 if (!this._remoteConnection) { |
| 14 var url = Runtime.queryParam('service-backend'); | 14 var url = Runtime.queryParam('service-backend'); |
| 15 if (!url) { | 15 if (!url) { |
| 16 console.error('No endpoint address specified'); | 16 console.error('No endpoint address specified'); |
| 17 return /** @type {!Promise<?WebInspector.ServiceManager.Service>} */ (Pr
omise.resolve(null)); | 17 return /** @type {!Promise<?Services.ServiceManager.Service>} */ (Promis
e.resolve(null)); |
| 18 } | 18 } |
| 19 this._remoteConnection = | 19 this._remoteConnection = |
| 20 new WebInspector.ServiceManager.Connection(new WebInspector.ServiceMan
ager.RemoteServicePort(url)); | 20 new Services.ServiceManager.Connection(new Services.ServiceManager.Rem
oteServicePort(url)); |
| 21 } | 21 } |
| 22 return this._remoteConnection._createService(serviceName); | 22 return this._remoteConnection._createService(serviceName); |
| 23 } | 23 } |
| 24 | 24 |
| 25 /** | 25 /** |
| 26 * @param {string} appName | 26 * @param {string} appName |
| 27 * @param {string} serviceName | 27 * @param {string} serviceName |
| 28 * @param {boolean} isSharedWorker | 28 * @param {boolean} isSharedWorker |
| 29 * @return {!Promise<?WebInspector.ServiceManager.Service>} | 29 * @return {!Promise<?Services.ServiceManager.Service>} |
| 30 */ | 30 */ |
| 31 createAppService(appName, serviceName, isSharedWorker) { | 31 createAppService(appName, serviceName, isSharedWorker) { |
| 32 var url = appName + '.js'; | 32 var url = appName + '.js'; |
| 33 var remoteBase = Runtime.queryParam('remoteBase'); | 33 var remoteBase = Runtime.queryParam('remoteBase'); |
| 34 if (remoteBase) | 34 if (remoteBase) |
| 35 url += '?remoteBase=' + remoteBase; | 35 url += '?remoteBase=' + remoteBase; |
| 36 | 36 |
| 37 var worker = isSharedWorker ? new SharedWorker(url, appName) : new Worker(ur
l); | 37 var worker = isSharedWorker ? new SharedWorker(url, appName) : new Worker(ur
l); |
| 38 var connection = | 38 var connection = |
| 39 new WebInspector.ServiceManager.Connection(new WebInspector.ServiceManag
er.WorkerServicePort(worker)); | 39 new Services.ServiceManager.Connection(new Services.ServiceManager.Worke
rServicePort(worker)); |
| 40 return connection._createService(serviceName); | 40 return connection._createService(serviceName); |
| 41 } | 41 } |
| 42 }; | 42 }; |
| 43 | 43 |
| 44 /** | 44 /** |
| 45 * @unrestricted | 45 * @unrestricted |
| 46 */ | 46 */ |
| 47 WebInspector.ServiceManager.Connection = class { | 47 Services.ServiceManager.Connection = class { |
| 48 /** | 48 /** |
| 49 * @param {!ServicePort} port | 49 * @param {!ServicePort} port |
| 50 */ | 50 */ |
| 51 constructor(port) { | 51 constructor(port) { |
| 52 this._port = port; | 52 this._port = port; |
| 53 this._port.setHandlers(this._onMessage.bind(this), this._connectionClosed.bi
nd(this)); | 53 this._port.setHandlers(this._onMessage.bind(this), this._connectionClosed.bi
nd(this)); |
| 54 | 54 |
| 55 this._lastId = 1; | 55 this._lastId = 1; |
| 56 /** @type {!Map<number, function(?Object)>}*/ | 56 /** @type {!Map<number, function(?Object)>}*/ |
| 57 this._callbacks = new Map(); | 57 this._callbacks = new Map(); |
| 58 /** @type {!Map<string, !WebInspector.ServiceManager.Service>}*/ | 58 /** @type {!Map<string, !Services.ServiceManager.Service>}*/ |
| 59 this._services = new Map(); | 59 this._services = new Map(); |
| 60 } | 60 } |
| 61 | 61 |
| 62 /** | 62 /** |
| 63 * @param {string} serviceName | 63 * @param {string} serviceName |
| 64 * @return {!Promise<?WebInspector.ServiceManager.Service>} | 64 * @return {!Promise<?Services.ServiceManager.Service>} |
| 65 */ | 65 */ |
| 66 _createService(serviceName) { | 66 _createService(serviceName) { |
| 67 return this._sendCommand(serviceName + '.create').then(result => { | 67 return this._sendCommand(serviceName + '.create').then(result => { |
| 68 if (!result) { | 68 if (!result) { |
| 69 console.error('Could not initialize service: ' + serviceName); | 69 console.error('Could not initialize service: ' + serviceName); |
| 70 return null; | 70 return null; |
| 71 } | 71 } |
| 72 var service = new WebInspector.ServiceManager.Service(this, serviceName, r
esult.id); | 72 var service = new Services.ServiceManager.Service(this, serviceName, resul
t.id); |
| 73 this._services.set(serviceName + ':' + result.id, service); | 73 this._services.set(serviceName + ':' + result.id, service); |
| 74 return service; | 74 return service; |
| 75 }); | 75 }); |
| 76 } | 76 } |
| 77 | 77 |
| 78 /** | 78 /** |
| 79 * @param {!WebInspector.ServiceManager.Service} service | 79 * @param {!Services.ServiceManager.Service} service |
| 80 */ | 80 */ |
| 81 _serviceDisposed(service) { | 81 _serviceDisposed(service) { |
| 82 this._services.delete(service._serviceName + ':' + service._objectId); | 82 this._services.delete(service._serviceName + ':' + service._objectId); |
| 83 if (!this._services.size) { | 83 if (!this._services.size) { |
| 84 // Terminate the connection since it is no longer used. | 84 // Terminate the connection since it is no longer used. |
| 85 this._port.close(); | 85 this._port.close(); |
| 86 } | 86 } |
| 87 } | 87 } |
| 88 | 88 |
| 89 /** | 89 /** |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 this._callbacks.clear(); | 137 this._callbacks.clear(); |
| 138 for (var service of this._services.values()) | 138 for (var service of this._services.values()) |
| 139 service._dispatchNotification('disposed'); | 139 service._dispatchNotification('disposed'); |
| 140 this._services.clear(); | 140 this._services.clear(); |
| 141 } | 141 } |
| 142 }; | 142 }; |
| 143 | 143 |
| 144 /** | 144 /** |
| 145 * @unrestricted | 145 * @unrestricted |
| 146 */ | 146 */ |
| 147 WebInspector.ServiceManager.Service = class { | 147 Services.ServiceManager.Service = class { |
| 148 /** | 148 /** |
| 149 * @param {!WebInspector.ServiceManager.Connection} connection | 149 * @param {!Services.ServiceManager.Connection} connection |
| 150 * @param {string} serviceName | 150 * @param {string} serviceName |
| 151 * @param {string} objectId | 151 * @param {string} objectId |
| 152 */ | 152 */ |
| 153 constructor(connection, serviceName, objectId) { | 153 constructor(connection, serviceName, objectId) { |
| 154 this._connection = connection; | 154 this._connection = connection; |
| 155 this._serviceName = serviceName; | 155 this._serviceName = serviceName; |
| 156 this._objectId = objectId; | 156 this._objectId = objectId; |
| 157 /** @type {!Map<string, function(!Object=)>}*/ | 157 /** @type {!Map<string, function(!Object=)>}*/ |
| 158 this._notificationHandlers = new Map(); | 158 this._notificationHandlers = new Map(); |
| 159 } | 159 } |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 198 return; | 198 return; |
| 199 } | 199 } |
| 200 handler(params); | 200 handler(params); |
| 201 } | 201 } |
| 202 }; | 202 }; |
| 203 | 203 |
| 204 /** | 204 /** |
| 205 * @implements {ServicePort} | 205 * @implements {ServicePort} |
| 206 * @unrestricted | 206 * @unrestricted |
| 207 */ | 207 */ |
| 208 WebInspector.ServiceManager.RemoteServicePort = class { | 208 Services.ServiceManager.RemoteServicePort = class { |
| 209 /** | 209 /** |
| 210 * @param {string} url | 210 * @param {string} url |
| 211 */ | 211 */ |
| 212 constructor(url) { | 212 constructor(url) { |
| 213 this._url = url; | 213 this._url = url; |
| 214 } | 214 } |
| 215 | 215 |
| 216 /** | 216 /** |
| 217 * @override | 217 * @override |
| 218 * @param {function(string)} messageHandler | 218 * @param {function(string)} messageHandler |
| 219 * @param {function(string)} closeHandler | 219 * @param {function(string)} closeHandler |
| 220 */ | 220 */ |
| 221 setHandlers(messageHandler, closeHandler) { | 221 setHandlers(messageHandler, closeHandler) { |
| 222 this._messageHandler = messageHandler; | 222 this._messageHandler = messageHandler; |
| 223 this._closeHandler = closeHandler; | 223 this._closeHandler = closeHandler; |
| 224 } | 224 } |
| 225 | 225 |
| 226 /** | 226 /** |
| 227 * @return {!Promise<boolean>} | 227 * @return {!Promise<boolean>} |
| 228 */ | 228 */ |
| 229 _open() { | 229 _open() { |
| 230 if (!this._connectionPromise) | 230 if (!this._connectionPromise) |
| 231 this._connectionPromise = new Promise(promiseBody.bind(this)); | 231 this._connectionPromise = new Promise(promiseBody.bind(this)); |
| 232 return this._connectionPromise; | 232 return this._connectionPromise; |
| 233 | 233 |
| 234 /** | 234 /** |
| 235 * @param {function(boolean)} fulfill | 235 * @param {function(boolean)} fulfill |
| 236 * @this {WebInspector.ServiceManager.RemoteServicePort} | 236 * @this {Services.ServiceManager.RemoteServicePort} |
| 237 */ | 237 */ |
| 238 function promiseBody(fulfill) { | 238 function promiseBody(fulfill) { |
| 239 var socket; | 239 var socket; |
| 240 try { | 240 try { |
| 241 socket = new WebSocket(/** @type {string} */ (this._url)); | 241 socket = new WebSocket(/** @type {string} */ (this._url)); |
| 242 socket.onmessage = onMessage.bind(this); | 242 socket.onmessage = onMessage.bind(this); |
| 243 socket.onclose = this._closeHandler; | 243 socket.onclose = this._closeHandler; |
| 244 socket.onopen = onConnect.bind(this); | 244 socket.onopen = onConnect.bind(this); |
| 245 } catch (e) { | 245 } catch (e) { |
| 246 fulfill(false); | 246 fulfill(false); |
| 247 } | 247 } |
| 248 | 248 |
| 249 /** | 249 /** |
| 250 * @this {WebInspector.ServiceManager.RemoteServicePort} | 250 * @this {Services.ServiceManager.RemoteServicePort} |
| 251 */ | 251 */ |
| 252 function onConnect() { | 252 function onConnect() { |
| 253 this._socket = socket; | 253 this._socket = socket; |
| 254 fulfill(true); | 254 fulfill(true); |
| 255 } | 255 } |
| 256 | 256 |
| 257 /** | 257 /** |
| 258 * @param {!Event} event | 258 * @param {!Event} event |
| 259 * @this {WebInspector.ServiceManager.RemoteServicePort} | 259 * @this {Services.ServiceManager.RemoteServicePort} |
| 260 */ | 260 */ |
| 261 function onMessage(event) { | 261 function onMessage(event) { |
| 262 this._messageHandler(event.data); | 262 this._messageHandler(event.data); |
| 263 } | 263 } |
| 264 } | 264 } |
| 265 } | 265 } |
| 266 | 266 |
| 267 /** | 267 /** |
| 268 * @override | 268 * @override |
| 269 * @param {string} message | 269 * @param {string} message |
| (...skipping 22 matching lines...) Expand all Loading... |
| 292 } | 292 } |
| 293 return true; | 293 return true; |
| 294 }); | 294 }); |
| 295 } | 295 } |
| 296 }; | 296 }; |
| 297 | 297 |
| 298 /** | 298 /** |
| 299 * @implements {ServicePort} | 299 * @implements {ServicePort} |
| 300 * @unrestricted | 300 * @unrestricted |
| 301 */ | 301 */ |
| 302 WebInspector.ServiceManager.WorkerServicePort = class { | 302 Services.ServiceManager.WorkerServicePort = class { |
| 303 /** | 303 /** |
| 304 * @param {!Worker|!SharedWorker} worker | 304 * @param {!Worker|!SharedWorker} worker |
| 305 */ | 305 */ |
| 306 constructor(worker) { | 306 constructor(worker) { |
| 307 this._worker = worker; | 307 this._worker = worker; |
| 308 | 308 |
| 309 var fulfill; | 309 var fulfill; |
| 310 this._workerPromise = new Promise(resolve => fulfill = resolve); | 310 this._workerPromise = new Promise(resolve => fulfill = resolve); |
| 311 this._isSharedWorker = worker instanceof SharedWorker; | 311 this._isSharedWorker = worker instanceof SharedWorker; |
| 312 | 312 |
| 313 if (this._isSharedWorker) { | 313 if (this._isSharedWorker) { |
| 314 this._worker.port.onmessage = onMessage.bind(this); | 314 this._worker.port.onmessage = onMessage.bind(this); |
| 315 this._worker.port.onclose = this._closeHandler; | 315 this._worker.port.onclose = this._closeHandler; |
| 316 } else { | 316 } else { |
| 317 this._worker.onmessage = onMessage.bind(this); | 317 this._worker.onmessage = onMessage.bind(this); |
| 318 this._worker.onclose = this._closeHandler; | 318 this._worker.onclose = this._closeHandler; |
| 319 } | 319 } |
| 320 | 320 |
| 321 /** | 321 /** |
| 322 * @param {!Event} event | 322 * @param {!Event} event |
| 323 * @this {WebInspector.ServiceManager.WorkerServicePort} | 323 * @this {Services.ServiceManager.WorkerServicePort} |
| 324 */ | 324 */ |
| 325 function onMessage(event) { | 325 function onMessage(event) { |
| 326 if (event.data === 'workerReady') { | 326 if (event.data === 'workerReady') { |
| 327 fulfill(true); | 327 fulfill(true); |
| 328 return; | 328 return; |
| 329 } | 329 } |
| 330 this._messageHandler(event.data); | 330 this._messageHandler(event.data); |
| 331 } | 331 } |
| 332 } | 332 } |
| 333 | 333 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 366 */ | 366 */ |
| 367 close() { | 367 close() { |
| 368 return this._workerPromise.then(() => { | 368 return this._workerPromise.then(() => { |
| 369 if (this._worker) | 369 if (this._worker) |
| 370 this._worker.terminate(); | 370 this._worker.terminate(); |
| 371 return false; | 371 return false; |
| 372 }); | 372 }); |
| 373 } | 373 } |
| 374 }; | 374 }; |
| 375 | 375 |
| 376 WebInspector.serviceManager = new WebInspector.ServiceManager(); | 376 Services.serviceManager = new Services.ServiceManager(); |
| OLD | NEW |