Chromium Code Reviews| 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.provide('chromium.DevTools.Connection'); | |
| 14 | |
| 15 /** | |
| 16 * @typedef {!function(Object): undefined|!function(string): undefined} | |
|
dpapad
2017/06/06 17:37:46
"!" not necessary for |function| which is consider
alex clarke (OOO till 29th)
2017/06/07 15:15:39
Done.
| |
| 17 * | |
| 18 */ | |
| 19 chromium.DevTools.CallbackFunction; | |
| 20 | |
| 21 /** | |
| 22 * @class Handles sending and receiving DevTools JSON protocol messages over the | |
| 23 * provided low level message transport. | |
| 24 * @param {!Object} transport The API providing transport for devtools commands. | |
| 25 * @constructor | |
| 26 */ | |
| 27 chromium.DevTools.Connection = function(transport) { | |
| 28 /** | |
| 29 * The DevTools connection. | |
| 30 * | |
| 31 * @private {!Object} | |
| 32 */ | |
| 33 this.transport_ = transport; | |
| 34 | |
| 35 /** | |
|
dpapad
2017/06/06 17:37:45
Nit: Consider compacting those as where possible,
alex clarke (OOO till 29th)
2017/06/07 15:15:39
Done.
| |
| 36 * @private {number} | |
| 37 */ | |
| 38 this.commandId_ = 1; | |
| 39 | |
| 40 /** | |
| 41 * An object containing pending DevTools protocol commands keyed by id. | |
| 42 * | |
| 43 * @type {!Object<!chromium.DevTools.CallbackFunction>} | |
|
dpapad
2017/06/06 17:37:46
Why not using the shorthand syntax? Also why not u
alex clarke (OOO till 29th)
2017/06/07 15:15:39
On 2017/06/06 17:37:46, dpapad wrote:
> Why not us
| |
| 44 * @private | |
| 45 */ | |
| 46 this.pendingCommands_ = {}; | |
| 47 | |
| 48 /** | |
| 49 * An object containing DevTools protocol events we are listening for keyed by | |
| 50 * name. | |
| 51 * | |
| 52 * @type {!Object<!Array<!chromium.DevTools.CallbackFunction>>} | |
|
dpapad
2017/06/06 17:37:46
Same question about Object vs Map
alex clarke (OOO till 29th)
2017/06/07 15:15:39
Done.
| |
| 53 * @private | |
| 54 */ | |
| 55 this.eventListeners_ = {}; | |
| 56 | |
| 57 this.transport_.onmessage = this.onMessage_.bind(this); | |
| 58 }; | |
| 59 | |
| 60 | |
| 61 /** | |
| 62 * Listens for DevTools protocol events of the specified name and issues the | |
| 63 * callback upon reception. | |
| 64 * | |
| 65 * @param {!string} eventName Name of the DevTools protocol event to listen for. | |
|
dpapad
2017/06/06 17:37:46
"!" not necessary here (and elsewhere).
alex clarke (OOO till 29th)
2017/06/07 15:15:39
Done.
| |
| 66 * @param {!chromium.DevTools.CallbackFunction} listener The callback issued | |
| 67 * when we receive a DevTools protocol event corresponding to the given name. | |
| 68 */ | |
| 69 chromium.DevTools.Connection.prototype.addEventListener = function( | |
| 70 eventName, listener) { | |
| 71 if (!this.eventListeners_.hasOwnProperty(eventName)) { | |
| 72 this.eventListeners_[eventName] = []; | |
| 73 } | |
| 74 this.eventListeners_[eventName].push(listener); | |
| 75 }; | |
| 76 | |
| 77 /** | |
| 78 * Removes an event listener previously added by <code>addEventListener</code>. | |
| 79 * | |
| 80 * @param {!string} eventName Name of the DevTools protocol event to remove. | |
| 81 * @param {!chromium.DevTools.CallbackFunction} listener The callback to remove. | |
| 82 */ | |
| 83 chromium.DevTools.Connection.prototype.removeEventListener = function( | |
|
dpapad
2017/06/06 17:37:46
This is mimicking the addEventListener/removeEvent
alex clarke (OOO till 29th)
2017/06/07 15:15:39
Done.
| |
| 84 eventName, listener) { | |
| 85 if (!this.eventListeners_.hasOwnProperty(eventName)) return; | |
| 86 let index = this.eventListeners_[eventName].indexOf(listener); | |
| 87 if (index === -1) return; | |
| 88 this.eventListeners_[eventName].splice(index, 1); | |
| 89 }; | |
| 90 | |
| 91 | |
| 92 /** | |
| 93 * Issues a DevTools protocol command and returns a promise for the results. | |
| 94 * | |
| 95 * @param {string} method The name of the DevTools protocol command method. | |
| 96 * @param {!Object=} opt_params An object containing the command parameters if | |
| 97 * any. | |
| 98 * @return {Promise<!Object>} A promise for the results object. | |
|
dpapad
2017/06/06 17:37:46
!Promise
alex clarke (OOO till 29th)
2017/06/07 15:15:39
Done.
| |
| 99 */ | |
| 100 chromium.DevTools.Connection.prototype.sendDevToolsMessage = function( | |
| 101 method, opt_params) { | |
| 102 if (opt_params === undefined) { | |
| 103 opt_params = {}; | |
| 104 } | |
| 105 let id = this.commandId_; | |
| 106 // We increment by two because these bindings are intended to be used in | |
| 107 // conjunction with HeadlessDevToolsClient::RawProtocolListener and using odd | |
| 108 // numbers for js generated IDs lets the implementation of OnProtocolMessage | |
| 109 // easily distinguish between C++ and JS generated commands and route the | |
| 110 // response accordingly. | |
| 111 this.commandId_ += 2; | |
| 112 this.transport_.send( | |
| 113 JSON.stringify({'method': method, 'id': id, 'params': opt_params})); | |
|
dpapad
2017/06/06 17:37:46
I suppose that the single quotes here are used bec
alex clarke (OOO till 29th)
2017/06/07 15:15:39
Done.
| |
| 114 return new Promise((resolve, reject) => { | |
| 115 this.pendingCommands_[id] = resolve; | |
| 116 }); | |
| 117 }; | |
| 118 | |
| 119 | |
| 120 /** | |
| 121 * @param {string} json_message An object containing a DevTools protocol | |
|
dpapad
2017/06/06 17:37:45
Per JS Styleguide:
s/json_message/jsonMessage
alex clarke (OOO till 29th)
2017/06/07 15:15:39
Done.
| |
| 122 * message. | |
| 123 * @private | |
| 124 */ | |
| 125 chromium.DevTools.Connection.prototype.onMessage_ = function(json_message) { | |
| 126 const message = JSON.parse(json_message); | |
| 127 if (message.hasOwnProperty('id')) { | |
| 128 if (!this.pendingCommands_.hasOwnProperty(message.id)) | |
| 129 throw new Error('Unrecognized id:' + json_message); | |
| 130 if (message.hasOwnProperty('error')) | |
| 131 throw new Error('DevTools protocol error: ' + message.error); | |
| 132 this.pendingCommands_[message.id](message.result); | |
| 133 delete this.pendingCommands_[message.id]; | |
| 134 } else { | |
| 135 if (!message.hasOwnProperty('method') || | |
| 136 !message.hasOwnProperty('params')) { | |
| 137 throw new Error('Bad message:' + json_message); | |
| 138 } | |
| 139 const method = message['method']; | |
| 140 const params = message['params']; | |
| 141 if (this.eventListeners_.hasOwnProperty(method)) { | |
| 142 const listeners = this.eventListeners_[method]; | |
| 143 for (let i = 0; i < listeners.length; i++) { | |
| 144 listeners[i](params); | |
| 145 } | |
| 146 } | |
| 147 } | |
| 148 }; | |
| OLD | NEW |