Bug 1211665 - Filter add-ons console messages based on ConsoleID or originAttributes.addonId. r=baku
☠☠ backed out by b6ea6a3bb8a6 ☠ ☠
authorLuca Greco <lgreco@mozilla.com>
Tue, 29 Mar 2016 17:40:39 +0200
changeset 291277 75d0e51772db9f842eb5e1dee98230e774c69b33
parent 291276 44c3d66a71e3f2bf2f5e38ac3a20f6afd506aca1
child 291278 5e0b6fd97fd6e467899d2073667719c693c818fb
push id19656
push usergwagner@mozilla.com
push dateMon, 04 Apr 2016 13:43:23 +0000
treeherderb2g-inbound@e99061fde28a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbaku
bugs1211665
milestone48.0a1
Bug 1211665 - Filter add-ons console messages based on ConsoleID or originAttributes.addonId. r=baku MozReview-Commit-ID: 2yEWhX6shkx
devtools/server/actors/addon.js
devtools/shared/tests/unit/test_consoleID.js
devtools/shared/webconsole/utils.js
--- a/devtools/server/actors/addon.js
+++ b/devtools/server/actors/addon.js
@@ -311,17 +311,17 @@ update(AddonConsoleActor.prototype, {
     let startedListeners = [];
 
     while (aRequest.listeners.length > 0) {
       let listener = aRequest.listeners.shift();
       switch (listener) {
         case "ConsoleAPI":
           if (!this.consoleAPIListener) {
             this.consoleAPIListener =
-              new ConsoleAPIListener(null, this, "addon/" + this.addon.id);
+              new ConsoleAPIListener(null, this, { addonId: this.addon.id });
             this.consoleAPIListener.init();
           }
           startedListeners.push(listener);
           break;
       }
     }
     return {
       startedListeners: startedListeners,
--- a/devtools/shared/tests/unit/test_consoleID.js
+++ b/devtools/shared/tests/unit/test_consoleID.js
@@ -62,17 +62,17 @@ function run_test() {
   console.log("Hello from default console");
   console1.warn("Warning from foo");
   console2.error("Error from bar");
   do_check_eq(seenMessages, 3);
   do_check_eq(seenTypes, 7);
 
   listener.destroy();
 
-  listener = new ConsoleAPIListener(null, callback, "foo");
+  listener = new ConsoleAPIListener(null, callback, {consoleID: "foo"});
   listener.init();
   messages = listener.getCachedMessages();
 
   seenTypes = 0;
   seenMessages = 0;
   messages.forEach(callback.onConsoleAPICall);
   do_check_eq(seenMessages, 2);
   do_check_eq(seenTypes, 1);
--- a/devtools/shared/webconsole/utils.js
+++ b/devtools/shared/webconsole/utils.js
@@ -789,22 +789,24 @@ ConsoleServiceListener.prototype =
  * @param nsIDOMWindow window
  *        Optional - the window object for which we are created. This is used
  *        for filtering out messages that belong to other windows.
  * @param object owner
  *        The owner object must have the following methods:
  *        - onConsoleAPICall(). This method is invoked with one argument, the
  *        Console API message that comes from the observer service, whenever
  *        a relevant console API call is received.
- * @param string consoleID
- *        Options - The consoleID that this listener should listen to
+ * @param object filteringOptions
+ *        Options - The filteringOptions that this listener should listen to
+          (e.g. { addonId: "..." } or { consoleID: "..." }).
  */
-function ConsoleAPIListener(window, owner, consoleID) {
+function ConsoleAPIListener(window, owner, {addonId, consoleID} = {}) {
   this.window = window;
   this.owner = owner;
+  this.addonId = addonId;
   this.consoleID = consoleID;
 }
 exports.ConsoleAPIListener = ConsoleAPIListener;
 
 ConsoleAPIListener.prototype =
 {
   QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver]),
 
@@ -820,20 +822,20 @@ ConsoleAPIListener.prototype =
    * console API call object that comes from the observer service.
    *
    * @type object
    * @see WebConsoleActor
    */
   owner: null,
 
   /**
-   * The consoleID that we listen for. If not null then only messages from this
+   * The addonId that we listen for. If not null then only messages from this
    * console will be returned.
    */
-  consoleID: null,
+  addonId: null,
 
   /**
    * Initialize the window.console API observer.
    */
   init: function() {
     // Note that the observer is process-wide. We will filter the messages as
     // needed, see CAL_observe().
     Services.obs.addObserver(this, "console-api-log-event", false);
@@ -891,18 +893,41 @@ ConsoleAPIListener.prototype =
     if (this.window && !workerType) {
       let msgWindow = Services.wm.getCurrentInnerWindowWithId(message.innerID);
       if (!msgWindow || !isWindowIncluded(this.window, msgWindow)) {
         // Not the same window!
         return false;
       }
     }
 
-    if (this.consoleID && message.consoleID !== this.consoleID) {
-      return false;
+    if (this.consoleID) {
+      // Filtering based on the old-style consoleID property used by
+      // the legacy Console JSM module.
+      if (message.consoleID !== this.consoleID) {
+        return false;
+      }
+    }
+
+    if (this.addonId) {
+      if (!message.consoleID && !message.originAttributes) {
+        return false;
+      }
+
+      // Filtering based on the old-style consoleID property used by
+      // the legacy Console JSM module.
+      if (message.consoleID && message.consoleID !== `addon/${this.addonId}`) {
+        return false;
+      }
+
+      // Filtering based on the originAttributes used by
+      // the Console API object.
+      if (message.originAttributes &&
+          message.originAttributes.addonId !== this.addonId) {
+        return false;
+      }
     }
 
     return true;
   },
 
   /**
    * Get the cached messages for the current inner window and its (i)frames.
    *