Chromium Code Reviews| Index: third_party/WebKit/Source/devtools/front_end/services/ServiceManager.js |
| diff --git a/third_party/WebKit/Source/devtools/front_end/services/ServiceManager.js b/third_party/WebKit/Source/devtools/front_end/services/ServiceManager.js |
| index 989056b74edf7bd02d85ddca31ff5b0433e5345a..0bc018525642fa755f46f45ea10b4625357d943e 100644 |
| --- a/third_party/WebKit/Source/devtools/front_end/services/ServiceManager.js |
| +++ b/third_party/WebKit/Source/devtools/front_end/services/ServiceManager.js |
| @@ -30,12 +30,18 @@ WebInspector.ServiceManager.prototype = { |
| /** |
| * @param {string} appName |
| * @param {string} serviceName |
| - * @param {boolean} isShared |
| + * @param {boolean} isSharedWorker |
| * @return {!Promise<?WebInspector.ServiceManager.Service>} |
| */ |
| - createWorkerService: function(appName, serviceName, isShared) |
| + createAppService: function(appName, serviceName, isSharedWorker) |
| { |
| - var connection = new WebInspector.ServiceManager.Connection(new WebInspector.ServiceManager.WorkerServicePort(appName, isShared)); |
| + var url = appName + ".js"; |
| + var remoteBase = Runtime.queryParam("remoteBase"); |
| + if (remoteBase) |
| + url += "?remoteBase=" + remoteBase; |
| + |
| + var worker = isSharedWorker ? new SharedWorker(url, appName) : new Worker(url); |
| + var connection = new WebInspector.ServiceManager.Connection(new WebInspector.ServiceManager.WorkerServicePort(worker)); |
| return connection._createService(serviceName); |
| } |
| } |
| @@ -64,8 +70,10 @@ WebInspector.ServiceManager.Connection.prototype = { |
| _createService: function(serviceName) |
| { |
| return this._sendCommand(serviceName + ".create").then(result => { |
| - if (!result) |
| + if (!result) { |
| + console.error("Could not initialize service"); |
|
dgozman
2016/10/14 03:14:58
nit: let's print serviceName as well
|
| return null; |
| + } |
| var service = new WebInspector.ServiceManager.Service(this, serviceName, result.id); |
| this._services.set(serviceName + ":" + result.id, service); |
| return service; |
| @@ -308,13 +316,36 @@ WebInspector.ServiceManager.RemoteServicePort.prototype = { |
| /** |
| * @constructor |
| * @implements {ServicePort} |
| - * @param {string} appName |
| - * @param {boolean} isSharedWorker |
| + * @param {!Worker|!SharedWorker} worker |
| */ |
| -WebInspector.ServiceManager.WorkerServicePort = function(appName, isSharedWorker) |
| +WebInspector.ServiceManager.WorkerServicePort = function(worker) |
| { |
| - this._appName = appName; |
| - this._isSharedWorker = isSharedWorker; |
| + this._worker = worker; |
| + |
| + var callback; |
|
dgozman
2016/10/14 03:14:58
Our pattern has var fulfill, not callback.
|
| + this._workerPromise = new Promise(fulfill => callback = fulfill); |
| + this._isSharedWorker = worker instanceof SharedWorker; |
| + |
| + if (this._isSharedWorker) { |
| + this._worker.port.onmessage = onMessage.bind(this); |
| + this._worker.port.onclose = this._closeHandler; |
| + } else { |
| + this._worker.onmessage = onMessage.bind(this); |
| + this._worker.onclose = this._closeHandler; |
| + } |
| + |
| + /** |
| + * @param {!Event} event |
| + * @this {WebInspector.ServiceManager.WorkerServicePort} |
| + */ |
| + function onMessage(event) |
| + { |
| + if (event.data === "workerReady") { |
| + callback(true); |
| + return; |
| + } |
| + this._messageHandler(event.data); |
| + } |
| } |
| WebInspector.ServiceManager.WorkerServicePort.prototype = { |
| @@ -330,60 +361,13 @@ WebInspector.ServiceManager.WorkerServicePort.prototype = { |
| }, |
| /** |
| - * @return {!Promise<boolean>} |
| - */ |
| - _open: function() |
| - { |
| - if (this._workerPromise) |
| - return this._workerPromise; |
| - |
| - var url = this._appName + ".js"; |
| - var remoteBase = Runtime.queryParam("remoteBase"); |
| - if (remoteBase) |
| - url += "?remoteBase=" + remoteBase; |
| - |
| - this._workerPromise = new Promise(promiseBody.bind(this)); |
| - |
| - /** |
| - * @param {function(boolean)} fulfill |
| - * @this {WebInspector.ServiceManager.WorkerServicePort} |
| - */ |
| - function promiseBody(fulfill) |
| - { |
| - if (this._isSharedWorker) { |
| - this._worker = new SharedWorker(url, this._appName); |
| - this._worker.port.onmessage = onMessage.bind(this); |
| - this._worker.port.onclose = this._closeHandler; |
| - } else { |
| - this._worker = new Worker(url); |
| - this._worker.onmessage = onMessage.bind(this); |
| - this._worker.onclose = this._closeHandler; |
| - } |
| - |
| - /** |
| - * @param {!Event} event |
| - * @this {WebInspector.ServiceManager.WorkerServicePort} |
| - */ |
| - function onMessage(event) |
| - { |
| - if (event.data === "workerReady") { |
| - fulfill(true); |
| - return; |
| - } |
| - this._messageHandler(event.data); |
| - } |
| - } |
| - return this._workerPromise; |
| - }, |
| - |
| - /** |
| * @override |
| * @param {string} message |
| * @return {!Promise<boolean>} |
| */ |
| send: function(message) |
| { |
| - return this._open().then(() => { |
| + return this._workerPromise.then(() => { |
| try { |
| if (this._isSharedWorker) |
| this._worker.port.postMessage(message); |
| @@ -402,7 +386,7 @@ WebInspector.ServiceManager.WorkerServicePort.prototype = { |
| */ |
| close: function() |
| { |
| - return this._open().then(() => { |
| + return this._workerPromise.then(() => { |
| if (this._worker) |
| this._worker.terminate(); |
| return false; |