Bug 1468754 Part 1: Add a ChangesActor to devtools. r=pbro
authorBrad Werth <bwerth@mozilla.com>
Mon, 22 Oct 2018 19:17:45 +0000
changeset 490802 9a9f1d46b0a9ab893e527bf6d1fe073a1b456dc7
parent 490801 2eda4a5dffcca83112096781c7ff8cca39308e42
child 490803 17e7f300cc64af47f7f8eed90848378338449a32
push id247
push userfmarier@mozilla.com
push dateSat, 27 Oct 2018 01:06:44 +0000
reviewerspbro
bugs1468754
milestone65.0a1
Bug 1468754 Part 1: Add a ChangesActor to devtools. r=pbro MozReview-Commit-ID: 1Y8esljnLk9 Differential Revision: https://phabricator.services.mozilla.com/D4399
devtools/server/actors/changes.js
devtools/server/actors/moz.build
devtools/server/actors/utils/actor-registry.js
devtools/shared/fronts/changes.js
devtools/shared/fronts/moz.build
devtools/shared/specs/changes.js
devtools/shared/specs/index.js
devtools/shared/specs/moz.build
new file mode 100644
--- /dev/null
+++ b/devtools/server/actors/changes.js
@@ -0,0 +1,70 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+"use strict";
+
+const protocol = require("devtools/shared/protocol");
+const { changesSpec } = require("devtools/shared/specs/changes");
+const TrackChangeEmitter = require("devtools/server/actors/utils/track-change-emitter");
+
+/**
+ * The ChangesActor stores a stack of changes made by devtools on
+ * the document in the associated tab.
+ */
+const ChangesActor = protocol.ActorClassWithSpec(changesSpec, {
+  /**
+   * Create a ChangesActor.
+   *
+   * @param {DebuggerServerConnection} conn
+   *    The server connection.
+   * @param {TargetActor} targetActor
+   *    The top-level Actor for this tab.
+   */
+  initialize: function(conn, targetActor) {
+    protocol.Actor.prototype.initialize.call(this, conn);
+    this.targetActor = targetActor;
+
+    this.onTrackChange = this.pushChange.bind(this);
+    TrackChangeEmitter.on("track-change", this.onTrackChange);
+
+    this.changes = [];
+  },
+
+  destroy: function() {
+    this.clearChanges();
+    TrackChangeEmitter.off("track-change", this.onTrackChange);
+    protocol.Actor.prototype.destroy.call(this);
+  },
+
+  changeCount: function() {
+    return this.changes.length;
+  },
+
+  change: function(index) {
+    if (index >= 0 && index < this.changes.length) {
+      // Return a copy of the change at index.
+      return Object.assign({}, this.changes[index]);
+    }
+    // No change at that index -- return undefined.
+    return undefined;
+  },
+
+  allChanges: function() {
+    return this.changes.slice();
+  },
+
+  pushChange: function(change) {
+    this.changes.push(change);
+  },
+
+  popChange: function() {
+    return this.changes.pop();
+  },
+
+  clearChanges: function() {
+    this.changes.length = 0;
+  },
+});
+
+exports.ChangesActor = ChangesActor;
--- a/devtools/server/actors/moz.build
+++ b/devtools/server/actors/moz.build
@@ -23,16 +23,17 @@ DevToolsModules(
     'accessibility-parent.js',
     'accessibility.js',
     'actor-registry.js',
     'animation-type-longhand.js',
     'animation.js',
     'array-buffer.js',
     'breakpoint.js',
     'canvas.js',
+    'changes.js',
     'common.js',
     'css-properties.js',
     'csscoverage.js',
     'device.js',
     'emulation.js',
     'environment.js',
     'errordocs.js',
     'frame.js',
--- a/devtools/server/actors/utils/actor-registry.js
+++ b/devtools/server/actors/utils/actor-registry.js
@@ -251,16 +251,21 @@ const ActorRegistry = {
       constructor: "AccessibilityActor",
       type: { target: true },
     });
     this.registerModule("devtools/server/actors/screenshot", {
       prefix: "screenshot",
       constructor: "ScreenshotActor",
       type: { target: true },
     });
+    this.registerModule("devtools/server/actors/changes", {
+      prefix: "changes",
+      constructor: "ChangesActor",
+      type: { target: true },
+    });
   },
 
   /**
    * Registers handlers for new target-scoped request types defined dynamically.
    *
    * Note that the name or actorPrefix of the request type is not allowed to clash with
    * existing protocol packet properties, like 'title', 'url' or 'actor', since that would
    * break the protocol.
new file mode 100644
--- /dev/null
+++ b/devtools/shared/fronts/changes.js
@@ -0,0 +1,24 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+"use strict";
+
+const protocol = require("devtools/shared/protocol");
+const {changesSpec} = require("devtools/shared/specs/changes");
+
+/**
+ * ChangesFront, the front object for the ChangesActor
+ */
+const ChangesFront = protocol.FrontClassWithSpec(changesSpec, {
+  initialize: function(client, {changesActor}) {
+    protocol.Front.prototype.initialize.call(this, client, {actor: changesActor});
+    this.manage(this);
+  },
+
+  destroy: function() {
+    protocol.Front.prototype.destroy.call(this);
+  },
+});
+
+exports.ChangesFront = ChangesFront;
--- a/devtools/shared/fronts/moz.build
+++ b/devtools/shared/fronts/moz.build
@@ -9,16 +9,17 @@ DIRS += [
     'targets',
 ]
 
 DevToolsModules(
     'accessibility.js',
     'actor-registry.js',
     'animation.js',
     'canvas.js',
+    'changes.js',
     'css-properties.js',
     'csscoverage.js',
     'device.js',
     'emulation.js',
     'framerate.js',
     'function-call.js',
     'highlighters.js',
     'inspector.js',
new file mode 100644
--- /dev/null
+++ b/devtools/shared/specs/changes.js
@@ -0,0 +1,24 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+"use strict";
+
+const {
+  generateActorSpec,
+  RetVal,
+} = require("devtools/shared/protocol");
+
+const changesSpec = generateActorSpec({
+  typeName: "changes",
+
+  methods: {
+    "allChanges": {
+      response: {
+        changes: RetVal("array:json"),
+      },
+    },
+  },
+});
+
+exports.changesSpec = changesSpec;
--- a/devtools/shared/specs/index.js
+++ b/devtools/shared/specs/index.js
@@ -54,16 +54,21 @@ const Types = exports.__TypesForTests = 
     front: null,
   },
   {
     types: ["frame-snapshot", "canvas"],
     spec: "devtools/shared/specs/canvas",
     front: "devtools/shared/fronts/canvas",
   },
   {
+    types: ["changes"],
+    spec: "devtools/shared/specs/changes",
+    front: "devtools/shared/fronts/changes",
+  },
+  {
     types: ["cssProperties"],
     spec: "devtools/shared/specs/css-properties",
     front: "devtools/shared/fronts/css-properties",
   },
   {
     types: ["cssUsage"],
     spec: "devtools/shared/specs/csscoverage",
     front: "devtools/shared/fronts/csscoverage",
--- a/devtools/shared/specs/moz.build
+++ b/devtools/shared/specs/moz.build
@@ -11,16 +11,17 @@ DIRS += [
 ]
 
 DevToolsModules(
     'accessibility.js',
     'actor-registry.js',
     'animation.js',
     'breakpoint.js',
     'canvas.js',
+    'changes.js',
     'css-properties.js',
     'csscoverage.js',
     'device.js',
     'emulation.js',
     'environment.js',
     'frame.js',
     'framerate.js',
     'function-call.js',