OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 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 * @fileoverview The DevTools | |
Sami
2017/05/24 09:16:21
Comment could use expanding :)
alex clarke (OOO till 29th)
2017/05/24 11:38:14
Done.
| |
7 */ | |
8 | |
9 'use strict'; | |
10 | |
11 goog.provide('goog.DevTools.Connection'); | |
12 | |
13 /** | |
14 * @class Bindings for the {{domain.domain}} DevTools Domain. | |
Sami
2017/05/24 09:16:21
This comment seems wrong.
alex clarke (OOO till 29th)
2017/05/24 11:38:14
Done.
| |
15 * @param {!Object} transport The API providing transport for devtools commands. | |
16 * @constructor | |
17 */ | |
18 goog.DevTools.Connection = function(transport) { | |
19 /** | |
20 * The DevTools connection. | |
21 * | |
22 * @private {!Object} | |
23 */ | |
24 this.transport_ = transport; | |
25 | |
26 /** | |
27 * @private {number} | |
28 */ | |
29 this.commandId_ = 1; | |
30 | |
31 /** | |
32 * An object containing pending DevTools protocol commands keyed by id. | |
33 * | |
34 * @type {!Object<!function(!Object)>} | |
35 * @private | |
36 */ | |
37 this.pendingCommands_ = {}; | |
38 | |
39 /** | |
40 * An object containing DevTools protocol events we are listening for keyed by | |
41 * name. | |
42 * | |
43 * @type {!Object<!Array<!function(!Object)>>} | |
44 * @private | |
45 */ | |
46 this.eventListeners_ = {}; | |
47 | |
48 this.transport_.onmessage = this.OnMessage_.bind(this); | |
Sami
2017/05/24 09:16:21
nit: onMessage
alex clarke (OOO till 29th)
2017/05/24 11:38:14
We defined this is as onmessage in the TabSocket.
| |
49 }; | |
50 | |
51 | |
52 /** | |
53 * Listens for DevTools protocol events of the specified name and issues the | |
54 * callback upon reception. | |
55 * | |
56 * @param {string} eventName Name of the DevTools protocol event to listen for. | |
57 * @param {!function(!Object)} listener The callback issued | |
58 * when we receive a DevTools protocol event corresponding to the given name. | |
59 */ | |
60 goog.DevTools.Connection.prototype.AddEventListener = function(eventName, | |
Sami
2017/05/24 09:16:21
Should we support removing listeners too?
nit: ad
aerotwist
2017/05/24 11:06:42
I'd love to see removeEventListener added here.
alex clarke (OOO till 29th)
2017/05/24 11:38:14
Done.
| |
61 listener) { | |
62 if (!this.eventListeners_.hasOwnProperty(eventName)) { | |
63 this.eventListeners_[eventName] = []; | |
64 } | |
65 this.eventListeners_[eventName].push(listener); | |
66 }; | |
67 | |
68 | |
69 /** | |
70 * Issues a DevTools protocol command and returns a promise for the results. | |
71 * | |
72 * @param {string} method The name of the DevTools protocol command method. | |
73 * @param {!Object=} opt_params An object containing the command parameters if | |
74 * any. | |
75 * @return {Promise<!Object>} A promise for the results object. | |
76 */ | |
77 goog.DevTools.Connection.prototype.SendDevToolsMessage = function(method, | |
Sami
2017/05/24 09:16:21
nit: sendDevToolsMessage
aerotwist
2017/05/24 11:06:42
It'd be worth checking whether the DevTools API (o
alex clarke (OOO till 29th)
2017/05/24 11:38:14
In theory commands sent in the order A,B,C are pro
alex clarke (OOO till 29th)
2017/05/24 11:38:14
Done.
| |
78 opt_params) { | |
79 if (opt_params === undefined) { | |
80 opt_params = {}; | |
81 } | |
82 let id = this.commandId_; | |
83 this.commandId_ += 2; | |
Sami
2017/05/24 09:16:21
This could use a comment saying why +2
alex clarke (OOO till 29th)
2017/05/24 11:38:14
Done.
| |
84 this.transport_.send( | |
85 JSON.stringify({'method': method, 'id': id, 'params': opt_params})); | |
86 return new Promise((resolve, reject) => { | |
87 this.pendingCommands_[id] = resolve; | |
88 }); | |
89 }; | |
90 | |
91 | |
92 /** | |
93 * @param {Object} event An object containing a DevTools protocol | |
94 * message. | |
95 * @private | |
96 */ | |
97 goog.DevTools.Connection.prototype.OnMessage_ = function(event) { | |
98 let message = JSON.parse(event.detail.message); | |
99 if (message.hasOwnProperty('id')) { | |
100 if (!this.pendingCommands_.hasOwnProperty(message.id)) { | |
101 console.log('Unrecognized id:' + json_message); | |
Sami
2017/05/24 09:16:21
console.error? Or just throw an error?
alex clarke (OOO till 29th)
2017/05/24 11:38:14
Done.
| |
102 return; | |
103 } | |
104 this.pendingCommands_[message.id](message.result); | |
105 delete this.pendingCommands_[message.id]; | |
106 } else { | |
107 if (!message.hasOwnProperty('method') || | |
108 !message.hasOwnProperty('params')) { | |
109 console.log('Bad message:' + json_message); | |
Sami
2017/05/24 09:16:21
Ditto.
alex clarke (OOO till 29th)
2017/05/24 11:38:14
Done.
| |
110 return; | |
111 } | |
112 var method = message['method']; | |
113 if (this.eventListeners_.hasOwnProperty(method)) { | |
114 var listeners = this.eventListeners_[method]; | |
Sami
2017/05/24 09:16:21
let for consistency?
aerotwist
2017/05/24 11:06:42
May as well go full const here; it looks like the
alex clarke (OOO till 29th)
2017/05/24 11:38:14
Done.
| |
115 var params = message['params']; | |
116 for (var i = 0; i < listeners.length; i++) { | |
117 listeners[i](params); | |
118 } | |
119 return; | |
120 } | |
121 } | |
122 }; | |
OLD | NEW |