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 Contains a class which marshals DevTools protocol messages over | |
7 * a provided low level message transport. This transport might be a headless | |
8 * TabSocket, or a WebSocket or a mock for testing. | |
9 */ | |
10 | |
11 'use strict'; | |
12 | |
13 goog.module('chromium.DevTools.Connection'); | |
14 | |
15 /** | |
16 * Handles sending and receiving DevTools JSON protocol messages over the | |
17 * provided low level message transport. | |
18 */ | |
19 class Connection { | |
dpapad
2017/06/08 17:12:03
FYI, yesterday I discovered an issue when using ES
alex clarke (OOO till 29th)
2017/06/09 08:37:16
Noted, thanks.
| |
20 /** | |
21 * @param {!Object} transport The API providing transport for devtools | |
22 * commands. | |
23 */ | |
24 constructor(transport) { | |
25 /** @private {!Object} */ | |
26 this.transport_ = transport; | |
27 | |
28 /** @private {number} */ | |
29 this.commandId_ = 1; | |
30 | |
31 /** | |
32 * An object containing pending DevTools protocol commands keyed by id. | |
33 * | |
34 * @private {!Map<number, !Connection.CallbackFunction>} | |
35 */ | |
36 this.pendingCommands_ = new Map(); | |
37 | |
38 /** @private {number} */ | |
39 this.nextListenerId_ = 1; | |
40 | |
41 /** | |
42 * An object containing DevTools protocol events we are listening for keyed | |
43 * by name. | |
44 * | |
45 * @private {!Map<string, !Map<number, !Connection.CallbackFunction>>} | |
46 */ | |
47 this.eventListeners_ = new Map(); | |
48 | |
49 /** | |
50 * Used for removing event listeners by id. | |
51 * | |
52 * @private {!Map<number, string>} | |
53 */ | |
54 this.eventListenerIdToEventName_ = new Map(); | |
55 | |
56 this.transport_.onmessage = this.onMessage_.bind(this); | |
57 } | |
58 | |
59 /** | |
60 * Listens for DevTools protocol events of the specified name and issues the | |
61 * callback upon reception. | |
62 * | |
63 * @param {string} eventName Name of the DevTools protocol event to listen | |
64 * for. | |
65 * @param {!Connection.CallbackFunction} listener The callback issued when we | |
66 * receive a DevTools protocol event corresponding to the given name. | |
67 * @return {number} The id of this event listener. | |
68 */ | |
69 addEventListener(eventName, listener) { | |
70 if (!this.eventListeners_.has(eventName)) { | |
71 this.eventListeners_.set(eventName, new Map()); | |
72 } | |
73 let id = this.nextListenerId_++; | |
74 this.eventListeners_.get(eventName).set(id, listener); | |
75 this.eventListenerIdToEventName_.set(id, eventName); | |
76 return id; | |
77 } | |
78 | |
79 | |
80 /** | |
81 * Removes an event listener previously added by | |
82 * <code>addEventListener</code>. | |
83 * | |
84 * @param {number} id The id of the event listener to remove. | |
85 * @return {boolean} Whether the event listener was actually removed. | |
86 */ | |
87 removeEventListener(id) { | |
88 if (!this.eventListenerIdToEventName_.has(id)) return false; | |
89 let eventName = this.eventListenerIdToEventName_.get(id); | |
90 this.eventListenerIdToEventName_.delete(id); | |
91 // This shouldn't happen, but lets check anyway. | |
92 if (!this.eventListeners_.has(eventName)) return false; | |
93 return this.eventListeners_.get(eventName).delete(id); | |
94 } | |
95 | |
96 | |
97 /** | |
98 * Issues a DevTools protocol command and returns a promise for the results. | |
99 * | |
100 * @param {string} method The name of the DevTools protocol command method. | |
101 * @param {!Object=} params An object containing the command parameters if | |
102 * any. | |
103 * @return {!Promise<!Object>} A promise for the results object. | |
104 */ | |
105 sendDevToolsMessage(method, params = {}) { | |
106 let id = this.commandId_; | |
107 // We increment by two because these bindings are intended to be used in | |
108 // conjunction with HeadlessDevToolsClient::RawProtocolListener and using | |
109 // odd numbers for js generated IDs lets the implementation of = | |
110 // OnProtocolMessage easily distinguish between C++ and JS generated | |
111 // commands and route the response accordingly. | |
112 this.commandId_ += 2; | |
113 // Note the names are in quotes to prevent closure compiler name mangling. | |
114 this.transport_.send( | |
115 JSON.stringify({'method': method, 'id': id, 'params': params})); | |
116 return new Promise((resolve, reject) => { | |
117 this.pendingCommands_.set(id, resolve); | |
118 }); | |
119 } | |
120 | |
121 | |
122 /** | |
123 * @param {string} jsonMessage An object containing a DevTools protocol | |
124 * message. | |
125 * @private | |
126 */ | |
127 onMessage_(jsonMessage) { | |
128 const message = JSON.parse(jsonMessage); | |
129 if (message.hasOwnProperty('id')) { | |
130 if (!this.pendingCommands_.has(message.id)) | |
131 throw new Error('Unrecognized id:' + jsonMessage); | |
132 if (message.hasOwnProperty('error')) | |
133 throw new Error('DevTools protocol error: ' + message.error); | |
134 this.pendingCommands_.get(message.id)(message.result); | |
135 this.pendingCommands_.delete(message.id); | |
136 } else { | |
137 if (!message.hasOwnProperty('method') || | |
138 !message.hasOwnProperty('params')) { | |
139 throw new Error('Bad message:' + jsonMessage); | |
140 } | |
141 const method = message['method']; | |
142 const params = message['params']; | |
143 if (this.eventListeners_.has(method)) { | |
144 this.eventListeners_.get(method).forEach(function(listener) { | |
145 listener(params); | |
146 }); | |
147 } | |
148 } | |
149 } | |
150 } | |
151 | |
152 /** | |
153 * @typedef {function(Object): undefined|function(string): undefined} | |
154 */ | |
155 Connection.CallbackFunction; | |
156 | |
157 exports = Connection; | |
OLD | NEW |