[Pkg-mozext-commits] [adblock-plus] 219/464: Worked around bug 721319 (memory leaks due to event listeners not being removed)

David Prévot taffit at moszumanska.debian.org
Tue Jul 22 20:44:19 UTC 2014


This is an automated email from the git hooks/post-receive script.

taffit pushed a commit to branch master
in repository adblock-plus.

commit c356e27e470b6bbda6e46d26f4b7013c2d5e3702
Author: Wladimir Palant <trev at adblockplus.org>
Date:   Thu Jan 26 11:35:23 2012 +0100

    Worked around bug 721319 (memory leaks due to event listeners not being removed)
---
 windowObserver.js | 38 +++++++++++++++++++++++++++++++++-----
 1 file changed, 33 insertions(+), 5 deletions(-)

diff --git a/windowObserver.js b/windowObserver.js
index 1db194d..bf60761 100644
--- a/windowObserver.js
+++ b/windowObserver.js
@@ -17,19 +17,32 @@ exports.WindowObserver = WindowObserver;
  * @param {String} [when]   when to execute applyToWindow(). "start" means immediately
  *                          when the window opens, "ready" when its contents are available
  *                          and "end" (default) means to wait until the "load" event.
+ * @param {Boolean} [ignoreUnloads]   Do not work around bug 721319 by calling
+ *                                    removeFromWindow() when the window unloads.
  * @constructor
  */
-function WindowObserver(listener, when)
+function WindowObserver(listener, when, ignoreUnloads)
 {
   this._listener  = listener;
   this._when = when;
 
+  if (ignoreUnloads)
+    this._unloadHandler = null;
+  else
+  {
+    // Ugly bug 721319 work-around
+    this._unloadHandler = function(event)
+    {
+      this.removeFromWindow(event.target.defaultView);
+    }.bind(this);
+  }
+
   let e = Services.ww.getWindowEnumerator();
   while (e.hasMoreElements())
   {
     let window = e.getNext().QueryInterface(Ci.nsIDOMWindow);
     if (when == "start" || window.document.readyState == "complete")
-      this._listener.applyToWindow(window);
+      this.applyToWindow(window);
     else
       this.observe(window, "domwindowopened", null);
   }
@@ -40,7 +53,7 @@ function WindowObserver(listener, when)
   {
     let e = Services.ww.getWindowEnumerator();
     while (e.hasMoreElements())
-      this._listener.removeFromWindow(e.getNext().QueryInterface(Ci.nsIDOMWindow));
+      this.removeFromWindow(e.getNext().QueryInterface(Ci.nsIDOMWindow));
 
     Services.ww.unregisterNotification(this);
   }.bind(this);
@@ -51,6 +64,7 @@ WindowObserver.prototype =
   _listener: null,
   _when: null,
   _shutdownHandler: null,
+  _unloadHandler: null,
 
   shutdown: function()
   {
@@ -62,13 +76,27 @@ WindowObserver.prototype =
     this._shutdownHandler = null;
   },
 
+  applyToWindow: function(window)
+  {
+    if (this._unloadHandler)
+      window.addEventListener("unload", this._unloadHandler, false);
+    this._listener.applyToWindow(window);
+  },
+
+  removeFromWindow: function(window)
+  {
+    if (this._unloadHandler)
+      window.removeEventListener("unload", this._unloadHandler, false);
+    this._listener.removeFromWindow(window);
+  },
+
   observe: function(subject, topic, data)
   {
     if (topic == "domwindowopened")
     {
       if (this._when == "start")
       {
-        this._listener.applyToWindow(window);
+        this.applyToWindow(window);
         return;
       }
 
@@ -78,7 +106,7 @@ WindowObserver.prototype =
       {
         window.removeEventListener(event, listener, false);
         if (this._shutdownHandler)
-          this._listener.applyToWindow(window);
+          this.applyToWindow(window);
       }.bind(this);
       window.addEventListener(event, listener, false);
     }

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-mozext/adblock-plus.git



More information about the Pkg-mozext-commits mailing list