Bug 1022587 - Kill reference to this._tabActor.browser in CSS Coverage; r=paul
authorJoe Walker <jwalker@mozilla.com>
Fri, 27 Jun 2014 08:31:21 +0100
changeset 191165 f6cee8c74faf8736354367bc6337eccca3ea66a9
parent 191164 70ff1b3ae5512ee559644ea4856a2f0a1b993b90
child 191166 8fb18794fe60317d2f79b6726a331baa2da668d9
push id8436
push usercbook@mozilla.com
push dateFri, 27 Jun 2014 13:56:57 +0000
treeherderb2g-inbound@22ea396750e8 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerspaul
bugs1022587
milestone33.0a1
Bug 1022587 - Kill reference to this._tabActor.browser in CSS Coverage; r=paul
toolkit/devtools/server/actors/csscoverage.js
--- a/toolkit/devtools/server/actors/csscoverage.js
+++ b/toolkit/devtools/server/actors/csscoverage.js
@@ -2,16 +2,17 @@
  * 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 { Cc, Ci, Cu } = require("chrome");
 
 const Services = require("Services");
+const { XPCOMUtils } = require("resource://gre/modules/XPCOMUtils.jsm");
 
 const events = require("sdk/event/core");
 const protocol = require("devtools/server/protocol");
 const { method, custom, RetVal, Arg } = protocol;
 
 loader.lazyGetter(this, "gDevTools", () => {
   return require("resource:///modules/devtools/gDevTools.jsm").gDevTools;
 });
@@ -86,16 +87,19 @@ let UsageReportActor = protocol.ActorCla
   initialize: function(conn, tabActor) {
     protocol.Actor.prototype.initialize.call(this, conn);
 
     this._tabActor = tabActor;
     this._running = false;
 
     this._onTabLoad = this._onTabLoad.bind(this);
     this._onChange = this._onChange.bind(this);
+
+    this._notifyOn = Ci.nsIWebProgress.NOTIFY_STATUS |
+                     Ci.nsIWebProgress.NOTIFY_STATE_ALL
   },
 
   destroy: function() {
     this._tabActor = undefined;
 
     delete this._onTabLoad;
     delete this._onChange;
 
@@ -110,35 +114,57 @@ let UsageReportActor = protocol.ActorCla
       throw new Error(l10n.lookup("csscoverageRunningError"));
     }
 
     this._visitedPages = new Set();
     this._knownRules = new Map();
     this._running = true;
     this._tooManyUnused = false;
 
-    this._tabActor.browser.addEventListener("load", this._onTabLoad, true);
+    this._progressListener = {
+      QueryInterface: XPCOMUtils.generateQI([ Ci.nsIWebProgressListener,
+                                              Ci.nsISupportsWeakReference ]),
+
+      onStateChange: (progress, request, flags, status) => {
+        let isStop = flags & Ci.nsIWebProgressListener.STATE_STOP;
+        let isWindow = flags & Ci.nsIWebProgressListener.STATE_IS_WINDOW;
 
-    this._observeMutations(this._tabActor.window.document);
+        if (isStop && isWindow) {
+          this._onTabLoad(progress.DOMWindow.document);
+        }
+      },
+
+      onLocationChange: () => {},
+      onProgressChange: () => {},
+      onSecurityChange: () => {},
+      onStatusChange: () => {},
+      destroy: () => {}
+    };
+
+    this._progress = this._tabActor.docShell.QueryInterface(Ci.nsIInterfaceRequestor)
+                                            .getInterface(Ci.nsIWebProgress);
+    this._progress.addProgressListener(this._progressListener, this._notifyOn);
 
     this._populateKnownRules(this._tabActor.window.document);
     this._updateUsage(this._tabActor.window.document, false);
 
     events.emit(this, "state-change", { isRunning: true });
   }),
 
   /**
    * Cease recording usage data
    */
   stop: method(function() {
     if (!this._running) {
       throw new Error(l10n.lookup("csscoverageNotRunningError"));
     }
 
-    this._tabActor.browser.removeEventListener("load", this._onTabLoad, true);
+    this._progress.removeProgressListener(this._progressListener, this._notifyOn);
+    this._progress = undefined;
+
     this._running = false;
     events.emit(this, "state-change", { isRunning: false });
   }),
 
   /**
    * Start/stop recording usage data depending on what we're currently doing.
    */
   toggle: method(function() {
@@ -157,20 +183,19 @@ let UsageReportActor = protocol.ActorCla
     this._visitedPages = new Set();
     this._knownRules = new Map();
 
     this._populateKnownRules(this._tabActor.window.document);
     this._updateUsage(this._tabActor.window.document, false);
   }),
 
   /**
-   * Called from the tab "load" event
+   * Called by the ProgressListener to simulate a "load" event
    */
-  _onTabLoad: function(ev) {
-    let document = ev.target;
+  _onTabLoad: function(document) {
     this._populateKnownRules(document);
     this._updateUsage(document, true);
 
     this._observeMutations(document);
   },
 
   /**
    * Setup a MutationObserver on the current document