Bug 1007520 - Part 3: Avoid leaking SpecialPowers to nsInProcessTabChildGlobal.mMessageManager. r=ted a=khuey
authorTing-Yu Chou <janus926@gmail.com>
Thu, 05 Jun 2014 22:17:23 +0800
changeset 206893 7b60deda620e5726b7d6af0ac9b69f40b28795f0
parent 206892 987cdc05ff389c88e6a29160325da1c5d84efb21
child 206894 40d0e9c27d210218acbbf502354ec0d88d0d3657
push id3741
push userasasaki@mozilla.com
push dateMon, 21 Jul 2014 20:25:18 +0000
treeherdermozilla-beta@4d6f46f5af68 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersted, khuey
bugs1007520
milestone32.0a2
Bug 1007520 - Part 3: Avoid leaking SpecialPowers to nsInProcessTabChildGlobal.mMessageManager. r=ted a=khuey
testing/specialpowers/content/specialpowers.js
--- a/testing/specialpowers/content/specialpowers.js
+++ b/testing/specialpowers/content/specialpowers.js
@@ -2,30 +2,47 @@
  * 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/. */
 /* This code is loaded in every child process that is started by mochitest in
  * order to be used as a replacement for UniversalXPConnect
  */
 
 function SpecialPowers(window) {
   this.window = Components.utils.getWeakReference(window);
+  this._windowID = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
+                         .getInterface(Components.interfaces.nsIDOMWindowUtils)
+                         .currentInnerWindowID;
   this._encounteredCrashDumpFiles = [];
   this._unexpectedCrashDumpFiles = { };
   this._crashDumpDir = null;
   this.DOMWindowUtils = bindDOMWindowUtils(window);
   Object.defineProperty(this, 'Components', {
       configurable: true, enumerable: true, get: function() {
           var win = this.window.get();
           if (!win)
               return null;
           return getRawComponents(win);
       }});
   this._pongHandlers = [];
   this._messageListener = this._messageReceived.bind(this);
   addMessageListener("SPPingService", this._messageListener);
+  let (self = this) {
+    Services.obs.addObserver(function onInnerWindowDestroyed(subject, topic, data) {
+      var id = subject.QueryInterface(Components.interfaces.nsISupportsPRUint64).data;
+      if (self._windowID === id) {
+        Services.obs.removeObserver(onInnerWindowDestroyed, "inner-window-destroyed");
+        try {
+          removeMessageListener("SPPingService", self._messageListener);
+        } catch (e if e.result == Components.results.NS_ERROR_ILLEGAL_VALUE) {
+          // Ignore the exception which the message manager has been destroyed.
+          ;
+        }
+      }
+    }, "inner-window-destroyed", false);
+  }
 }
 
 SpecialPowers.prototype = new SpecialPowersAPI();
 
 SpecialPowers.prototype.toString = function() { return "[SpecialPowers]"; };
 SpecialPowers.prototype.sanityCheck = function() { return "foo"; };
 
 // This gets filled in in the constructor.