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

Unified Diff: headless/lib/browser/devtools_api/devtools_connection.js

Issue 2902583002: Add some closureised JS bindings for DevTools for use by headless embedder (Closed)
Patch Set: Use goog.forwardDeclare where needed Created 3 years, 6 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 side-by-side diff with in-line comments
Download patch
Index: headless/lib/browser/devtools_api/devtools_connection.js
diff --git a/headless/lib/browser/devtools_api/devtools_connection.js b/headless/lib/browser/devtools_api/devtools_connection.js
new file mode 100644
index 0000000000000000000000000000000000000000..48b91cabd2e454a07c35da888cd6bacddc637de6
--- /dev/null
+++ b/headless/lib/browser/devtools_api/devtools_connection.js
@@ -0,0 +1,148 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+/**
+ * @fileoverview Contains a class which marshals DevTools protocol messages over
+ * a provided low level message transport. This transport might be a headless
+ * TabSocket, or a WebSocket or a mock for testing.
+ */
+
+'use strict';
+
+goog.provide('chromium.DevTools.Connection');
+
+/**
+ * @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.
+ *
+ */
+chromium.DevTools.CallbackFunction;
+
+/**
+ * @class Handles sending and receiving DevTools JSON protocol messages over the
+ * provided low level message transport.
+ * @param {!Object} transport The API providing transport for devtools commands.
+ * @constructor
+ */
+chromium.DevTools.Connection = function(transport) {
+ /**
+ * The DevTools connection.
+ *
+ * @private {!Object}
+ */
+ this.transport_ = transport;
+
+ /**
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.
+ * @private {number}
+ */
+ this.commandId_ = 1;
+
+ /**
+ * An object containing pending DevTools protocol commands keyed by id.
+ *
+ * @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
+ * @private
+ */
+ this.pendingCommands_ = {};
+
+ /**
+ * An object containing DevTools protocol events we are listening for keyed by
+ * name.
+ *
+ * @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.
+ * @private
+ */
+ this.eventListeners_ = {};
+
+ this.transport_.onmessage = this.onMessage_.bind(this);
+};
+
+
+/**
+ * Listens for DevTools protocol events of the specified name and issues the
+ * callback upon reception.
+ *
+ * @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.
+ * @param {!chromium.DevTools.CallbackFunction} listener The callback issued
+ * when we receive a DevTools protocol event corresponding to the given name.
+ */
+chromium.DevTools.Connection.prototype.addEventListener = function(
+ eventName, listener) {
+ if (!this.eventListeners_.hasOwnProperty(eventName)) {
+ this.eventListeners_[eventName] = [];
+ }
+ this.eventListeners_[eventName].push(listener);
+};
+
+/**
+ * Removes an event listener previously added by <code>addEventListener</code>.
+ *
+ * @param {!string} eventName Name of the DevTools protocol event to remove.
+ * @param {!chromium.DevTools.CallbackFunction} listener The callback to remove.
+ */
+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.
+ eventName, listener) {
+ if (!this.eventListeners_.hasOwnProperty(eventName)) return;
+ let index = this.eventListeners_[eventName].indexOf(listener);
+ if (index === -1) return;
+ this.eventListeners_[eventName].splice(index, 1);
+};
+
+
+/**
+ * Issues a DevTools protocol command and returns a promise for the results.
+ *
+ * @param {string} method The name of the DevTools protocol command method.
+ * @param {!Object=} opt_params An object containing the command parameters if
+ * any.
+ * @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.
+ */
+chromium.DevTools.Connection.prototype.sendDevToolsMessage = function(
+ method, opt_params) {
+ if (opt_params === undefined) {
+ opt_params = {};
+ }
+ let id = this.commandId_;
+ // We increment by two because these bindings are intended to be used in
+ // conjunction with HeadlessDevToolsClient::RawProtocolListener and using odd
+ // numbers for js generated IDs lets the implementation of OnProtocolMessage
+ // easily distinguish between C++ and JS generated commands and route the
+ // response accordingly.
+ this.commandId_ += 2;
+ this.transport_.send(
+ 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.
+ return new Promise((resolve, reject) => {
+ this.pendingCommands_[id] = resolve;
+ });
+};
+
+
+/**
+ * @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.
+ * message.
+ * @private
+ */
+chromium.DevTools.Connection.prototype.onMessage_ = function(json_message) {
+ const message = JSON.parse(json_message);
+ if (message.hasOwnProperty('id')) {
+ if (!this.pendingCommands_.hasOwnProperty(message.id))
+ throw new Error('Unrecognized id:' + json_message);
+ if (message.hasOwnProperty('error'))
+ throw new Error('DevTools protocol error: ' + message.error);
+ this.pendingCommands_[message.id](message.result);
+ delete this.pendingCommands_[message.id];
+ } else {
+ if (!message.hasOwnProperty('method') ||
+ !message.hasOwnProperty('params')) {
+ throw new Error('Bad message:' + json_message);
+ }
+ const method = message['method'];
+ const params = message['params'];
+ if (this.eventListeners_.hasOwnProperty(method)) {
+ const listeners = this.eventListeners_[method];
+ for (let i = 0; i < listeners.length; i++) {
+ listeners[i](params);
+ }
+ }
+ }
+};

Powered by Google App Engine
This is Rietveld 408576698