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

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: Refactored the test Created 3 years, 7 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..20ad0a425319579514a4c5d71ab26bcdd3756d20
--- /dev/null
+++ b/headless/lib/browser/devtools_api/devtools_connection.js
@@ -0,0 +1,142 @@
+// 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');
+
+/**
+ * @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;
+
+ /**
+ * @private {number}
+ */
+ this.commandId_ = 1;
+
+ /**
+ * An object containing pending DevTools protocol commands keyed by id.
+ *
+ * @type {!Object<!function(!Object)>}
+ * @private
+ */
+ this.pendingCommands_ = {};
+
+ /**
+ * An object containing DevTools protocol events we are listening for keyed by
+ * name.
+ *
+ * @type {!Object<!Array<!function(!Object)>>}
+ * @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.
+ * @param {!function(!Object)} listener The callback issued
+ * when we receive a DevTools protocol event corresponding to the given name.
+ */
+chromium.DevTools.Connection.prototype.addEventListener = function(eventName,
+ 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.
+ 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 {!function(!Object)} listener The callback to remove.
+ */
+chromium.DevTools.Connection.prototype.removeEventListener = function(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.
+ */
+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}));
+ return new Promise((resolve, reject) => {
+ this.pendingCommands_[id] = resolve;
+ });
+};
+
+
+/**
+ * @param {string} json_message An object containing a DevTools protocol
+ * 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);
+ 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