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

Side by Side Diff: third_party/WebKit/Source/devtools/front_end/services/ServiceManager.js

Issue 2417703006: DevTools: introduce a stub for the new audits panel (behind experiment). (Closed)
Patch Set: 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
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 /** 5 /**
6 * @constructor 6 * @constructor
7 */ 7 */
8 WebInspector.ServiceManager = function() 8 WebInspector.ServiceManager = function()
9 { 9 {
10 } 10 }
(...skipping 12 matching lines...) Expand all
23 return /** @type {!Promise<?WebInspector.ServiceManager.Service> } */ (Promise.resolve(null)); 23 return /** @type {!Promise<?WebInspector.ServiceManager.Service> } */ (Promise.resolve(null));
24 } 24 }
25 this._remoteConnection = new WebInspector.ServiceManager.Connection( new WebInspector.ServiceManager.RemoteServicePort(url)); 25 this._remoteConnection = new WebInspector.ServiceManager.Connection( new WebInspector.ServiceManager.RemoteServicePort(url));
26 } 26 }
27 return this._remoteConnection._createService(serviceName); 27 return this._remoteConnection._createService(serviceName);
28 }, 28 },
29 29
30 /** 30 /**
31 * @param {string} appName 31 * @param {string} appName
32 * @param {string} serviceName 32 * @param {string} serviceName
33 * @param {boolean} isShared 33 * @param {boolean} isSharedWorker
34 * @return {!Promise<?WebInspector.ServiceManager.Service>} 34 * @return {!Promise<?WebInspector.ServiceManager.Service>}
35 */ 35 */
36 createWorkerService: function(appName, serviceName, isShared) 36 createAppService: function(appName, serviceName, isSharedWorker)
37 { 37 {
38 var connection = new WebInspector.ServiceManager.Connection(new WebInspe ctor.ServiceManager.WorkerServicePort(appName, isShared)); 38 var url = appName + ".js";
39 var remoteBase = Runtime.queryParam("remoteBase");
40 if (remoteBase)
41 url += "?remoteBase=" + remoteBase;
42
43 var worker = isSharedWorker ? new SharedWorker(url, appName) : new Worke r(url);
44 var connection = new WebInspector.ServiceManager.Connection(new WebInspe ctor.ServiceManager.WorkerServicePort(worker));
39 return connection._createService(serviceName); 45 return connection._createService(serviceName);
40 } 46 }
41 } 47 }
42 48
43 /** 49 /**
44 * @constructor 50 * @constructor
45 * @param {!ServicePort} port 51 * @param {!ServicePort} port
46 */ 52 */
47 WebInspector.ServiceManager.Connection = function(port) 53 WebInspector.ServiceManager.Connection = function(port)
48 { 54 {
49 this._port = port; 55 this._port = port;
50 this._port.setHandlers(this._onMessage.bind(this), this._connectionClosed.bi nd(this)); 56 this._port.setHandlers(this._onMessage.bind(this), this._connectionClosed.bi nd(this));
51 57
52 this._lastId = 1; 58 this._lastId = 1;
53 /** @type {!Map<number, function(?Object)>}*/ 59 /** @type {!Map<number, function(?Object)>}*/
54 this._callbacks = new Map(); 60 this._callbacks = new Map();
55 /** @type {!Map<string, !WebInspector.ServiceManager.Service>}*/ 61 /** @type {!Map<string, !WebInspector.ServiceManager.Service>}*/
56 this._services = new Map(); 62 this._services = new Map();
57 } 63 }
58 64
59 WebInspector.ServiceManager.Connection.prototype = { 65 WebInspector.ServiceManager.Connection.prototype = {
60 /** 66 /**
61 * @param {string} serviceName 67 * @param {string} serviceName
62 * @return {!Promise<?WebInspector.ServiceManager.Service>} 68 * @return {!Promise<?WebInspector.ServiceManager.Service>}
63 */ 69 */
64 _createService: function(serviceName) 70 _createService: function(serviceName)
65 { 71 {
66 return this._sendCommand(serviceName + ".create").then(result => { 72 return this._sendCommand(serviceName + ".create").then(result => {
67 if (!result) 73 if (!result) {
74 console.error("Could not initialize service");
dgozman 2016/10/14 03:14:58 nit: let's print serviceName as well
68 return null; 75 return null;
76 }
69 var service = new WebInspector.ServiceManager.Service(this, serviceN ame, result.id); 77 var service = new WebInspector.ServiceManager.Service(this, serviceN ame, result.id);
70 this._services.set(serviceName + ":" + result.id, service); 78 this._services.set(serviceName + ":" + result.id, service);
71 return service; 79 return service;
72 }); 80 });
73 }, 81 },
74 82
75 /** 83 /**
76 * @param {!WebInspector.ServiceManager.Service} service 84 * @param {!WebInspector.ServiceManager.Service} service
77 */ 85 */
78 _serviceDisposed: function(service) 86 _serviceDisposed: function(service)
(...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after
301 delete this._connectionPromise; 309 delete this._connectionPromise;
302 } 310 }
303 return true; 311 return true;
304 }); 312 });
305 }, 313 },
306 } 314 }
307 315
308 /** 316 /**
309 * @constructor 317 * @constructor
310 * @implements {ServicePort} 318 * @implements {ServicePort}
311 * @param {string} appName 319 * @param {!Worker|!SharedWorker} worker
312 * @param {boolean} isSharedWorker
313 */ 320 */
314 WebInspector.ServiceManager.WorkerServicePort = function(appName, isSharedWorker ) 321 WebInspector.ServiceManager.WorkerServicePort = function(worker)
315 { 322 {
316 this._appName = appName; 323 this._worker = worker;
317 this._isSharedWorker = isSharedWorker; 324
325 var callback;
dgozman 2016/10/14 03:14:58 Our pattern has var fulfill, not callback.
326 this._workerPromise = new Promise(fulfill => callback = fulfill);
327 this._isSharedWorker = worker instanceof SharedWorker;
328
329 if (this._isSharedWorker) {
330 this._worker.port.onmessage = onMessage.bind(this);
331 this._worker.port.onclose = this._closeHandler;
332 } else {
333 this._worker.onmessage = onMessage.bind(this);
334 this._worker.onclose = this._closeHandler;
335 }
336
337 /**
338 * @param {!Event} event
339 * @this {WebInspector.ServiceManager.WorkerServicePort}
340 */
341 function onMessage(event)
342 {
343 if (event.data === "workerReady") {
344 callback(true);
345 return;
346 }
347 this._messageHandler(event.data);
348 }
318 } 349 }
319 350
320 WebInspector.ServiceManager.WorkerServicePort.prototype = { 351 WebInspector.ServiceManager.WorkerServicePort.prototype = {
321 /** 352 /**
322 * @override 353 * @override
323 * @param {function(string)} messageHandler 354 * @param {function(string)} messageHandler
324 * @param {function(string)} closeHandler 355 * @param {function(string)} closeHandler
325 */ 356 */
326 setHandlers: function(messageHandler, closeHandler) 357 setHandlers: function(messageHandler, closeHandler)
327 { 358 {
328 this._messageHandler = messageHandler; 359 this._messageHandler = messageHandler;
329 this._closeHandler = closeHandler; 360 this._closeHandler = closeHandler;
330 }, 361 },
331 362
332 /** 363 /**
333 * @return {!Promise<boolean>}
334 */
335 _open: function()
336 {
337 if (this._workerPromise)
338 return this._workerPromise;
339
340 var url = this._appName + ".js";
341 var remoteBase = Runtime.queryParam("remoteBase");
342 if (remoteBase)
343 url += "?remoteBase=" + remoteBase;
344
345 this._workerPromise = new Promise(promiseBody.bind(this));
346
347 /**
348 * @param {function(boolean)} fulfill
349 * @this {WebInspector.ServiceManager.WorkerServicePort}
350 */
351 function promiseBody(fulfill)
352 {
353 if (this._isSharedWorker) {
354 this._worker = new SharedWorker(url, this._appName);
355 this._worker.port.onmessage = onMessage.bind(this);
356 this._worker.port.onclose = this._closeHandler;
357 } else {
358 this._worker = new Worker(url);
359 this._worker.onmessage = onMessage.bind(this);
360 this._worker.onclose = this._closeHandler;
361 }
362
363 /**
364 * @param {!Event} event
365 * @this {WebInspector.ServiceManager.WorkerServicePort}
366 */
367 function onMessage(event)
368 {
369 if (event.data === "workerReady") {
370 fulfill(true);
371 return;
372 }
373 this._messageHandler(event.data);
374 }
375 }
376 return this._workerPromise;
377 },
378
379 /**
380 * @override 364 * @override
381 * @param {string} message 365 * @param {string} message
382 * @return {!Promise<boolean>} 366 * @return {!Promise<boolean>}
383 */ 367 */
384 send: function(message) 368 send: function(message)
385 { 369 {
386 return this._open().then(() => { 370 return this._workerPromise.then(() => {
387 try { 371 try {
388 if (this._isSharedWorker) 372 if (this._isSharedWorker)
389 this._worker.port.postMessage(message); 373 this._worker.port.postMessage(message);
390 else 374 else
391 this._worker.postMessage(message); 375 this._worker.postMessage(message);
392 return true; 376 return true;
393 } catch (e) { 377 } catch (e) {
394 return false; 378 return false;
395 } 379 }
396 }); 380 });
397 }, 381 },
398 382
399 /** 383 /**
400 * @override 384 * @override
401 * @return {!Promise} 385 * @return {!Promise}
402 */ 386 */
403 close: function() 387 close: function()
404 { 388 {
405 return this._open().then(() => { 389 return this._workerPromise.then(() => {
406 if (this._worker) 390 if (this._worker)
407 this._worker.terminate(); 391 this._worker.terminate();
408 return false; 392 return false;
409 }); 393 });
410 } 394 }
411 } 395 }
412 396
413 WebInspector.serviceManager = new WebInspector.ServiceManager(); 397 WebInspector.serviceManager = new WebInspector.ServiceManager();
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698