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