[Pkg-mozext-commits] [requestpolicy] 106/280: move WindowListener to separate file

David Prévot taffit at moszumanska.debian.org
Sat May 2 20:30:07 UTC 2015


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

taffit pushed a commit to branch master
in repository requestpolicy.

commit b9491535f046daa270610af0a878c902da5bcf52
Author: Martin Kimmerle <dev at 256k.de>
Date:   Mon Jan 5 23:40:17 2015 +0100

    move WindowListener to separate file
---
 src/content/lib/window-manager.jsm         |  95 +++++++++---------
 src/content/lib/window-manager.listener.js | 150 +++++++++++++++++++++++++++++
 src/content/ui/overlay.js                  |   8 ++
 3 files changed, 210 insertions(+), 43 deletions(-)

diff --git a/src/content/lib/window-manager.jsm b/src/content/lib/window-manager.jsm
index ff11526..8c0156b 100644
--- a/src/content/lib/window-manager.jsm
+++ b/src/content/lib/window-manager.jsm
@@ -25,6 +25,7 @@ let EXPORTED_SYMBOLS = ["rpWindowManager"];
 
 let globalScope = this;
 
+
 let rpWindowManager = (function(self) {
 
   const Ci = Components.interfaces;
@@ -42,6 +43,11 @@ let rpWindowManager = (function(self) {
     "process-environment"
   ], globalScope);
 
+  // import the WindowListener
+  Services.scriptloader.loadSubScript("chrome://requestpolicy/content/lib/" +
+                                      "window-manager.listener.js",
+                                      globalScope);
+
   let styleSheets = [
     "chrome://requestpolicy/skin/requestpolicy.css",
     "chrome://requestpolicy/skin/toolbarbutton.css"
@@ -49,45 +55,10 @@ let rpWindowManager = (function(self) {
 
 
 
-  let WindowListener = {
-    onOpenWindow: function(xulWindow) {
-      let window = xulWindow.QueryInterface(Ci.nsIInterfaceRequestor)
-          .getInterface(Ci.nsIDOMWindow);
-      addEventListenersToWindow(window);
-    },
-    onCloseWindow: function(xulWindow) {},
-    onWindowTitleChange: function(xulWindow, newTitle) {}
-  }
-
-  function addEventListenersToWindow(window) {
-    function onLoad(event) {
-      window.removeEventListener("load", onLoad);
-      if (window.document.documentElement.getAttribute("windowtype") ==
-          "navigator:browser") {
-        loadIntoWindow(window);
-      } else {
-        window.removeEventListener("unload", onUnload);
-      }
-    }
-
-    function onUnload(event) {
-      window.removeEventListener("unload", onUnload);
-      if (window.document.documentElement.getAttribute("windowtype") ==
-          "navigator:browser") {
-        unloadFromWindow(window);
-      }
-    }
-
-    // Event handler for when the window is closed. We listen for "unload"
-    // rather than "close" because "close" will fire when a print preview
-    // opened from this window is closed.
-    window.addEventListener("unload", onUnload, false);
-
-    // Registers event handlers for documents loaded in the window.
-    window.addEventListener("load", onLoad, false);
-  }
-
   function loadIntoWindow(window) {
+    // ==================================
+    // # 1 : add all XUL elements
+    // --------------------------
     try {
       XULUtils.addTreeElementsToWindow(window, "mainTree");
     } catch (e) {
@@ -95,9 +66,17 @@ let rpWindowManager = (function(self) {
                      "Couldn't add tree elements to window.");
     }
 
+
+    // ==================================
+    // # 2 : create a scope variable for RP for this window
+    // ----------------------------------------------------
+    window.requestpolicy = {};
+
+
+    // ==================================
+    // # 3 : load the overlay's and menu's javascript
+    // ----------------------------------------------
     try {
-      // create a scope variable for RP for this window
-      window.requestpolicy = {};
       Services.scriptloader.loadSubScript(
           "chrome://requestpolicy/content/ui/overlay.js", window);
       Services.scriptloader.loadSubScript(
@@ -109,6 +88,10 @@ let rpWindowManager = (function(self) {
                      "Error loading subscripts for window: "+e, e);
     }
 
+
+    // ==================================
+    // # 4 : toolbar button
+    // --------------------
     try {
       self.addToolbarButtonToWindow(window);
     } catch (e) {
@@ -116,6 +99,10 @@ let rpWindowManager = (function(self) {
                      "button to the window: "+e, e);
     }
 
+
+    // ==================================
+    // # 5 : init the overlay
+    // ----------------------
     try {
       // init and onWindowLoad must be called last, because they assume that
       // everything else is ready
@@ -126,6 +113,8 @@ let rpWindowManager = (function(self) {
                      "An error occurred while initializing the overlay: "+e, e);
     }
 
+    // ==================================
+    // # 6 : load frame scripts
     try {
       window.messageManager.loadFrameScript(
           "chrome://requestpolicy/content/ui/frame.js", true);
@@ -135,14 +124,32 @@ let rpWindowManager = (function(self) {
   }
 
   function unloadFromWindow(window) {
+    // # 6 : unload frame scripts
+    // --------------------------
+    // not needed
+
+
+    // # 5 : "shutdown" the overlay
+    // ----------------------------
     if (window.requestpolicy) {
       window.requestpolicy.overlay.onWindowUnload();
     }
 
-    XULUtils.removeTreeElementsFromWindow(window, "mainTree");
+
+    // # 4 : remove the toolbarbutton
+    // ------------------------------
     self.removeToolbarButtonFromWindow(window);
 
+
+    // # 3 and 2 : remove the `requestpolicy` variable from the window
+    // ---------------------------------------------------------
+    // This wouldn't be needed when the window is closed, but this has to be
+    // done when RP is being disabled.
     delete window.requestpolicy;
+
+
+    // # 1 : remove all XUL elements
+    XULUtils.removeTreeElementsFromWindow(window, "mainTree");
   }
 
 
@@ -151,14 +158,16 @@ let rpWindowManager = (function(self) {
 
   ProcessEnvironment.enqueueStartupFunction(function(data, reason) {
     forEachOpenWindow(loadIntoWindow);
-    Services.wm.addListener(WindowListener);
+    WindowListener.setLoadFunction(loadIntoWindow);
+    WindowListener.setUnloadFunction(unloadFromWindow);
+    WindowListener.startListening();
 
     loadStyleSheets();
   });
 
   ProcessEnvironment.pushShutdownFunction(function() {
     forEachOpenWindow(unloadFromWindow);
-    Services.wm.removeListener(WindowListener);
+    WindowListener.stopListening();
 
     unloadStyleSheets();
   });
diff --git a/src/content/lib/window-manager.listener.js b/src/content/lib/window-manager.listener.js
new file mode 100644
index 0000000..ba6fb4d
--- /dev/null
+++ b/src/content/lib/window-manager.listener.js
@@ -0,0 +1,150 @@
+/*
+ * ***** BEGIN LICENSE BLOCK *****
+ *
+ * RequestPolicy - A Firefox extension for control over cross-site requests.
+ * Copyright (c) 2008-2012 Justin Samuel
+ * Copyright (c) 2014-2015 Martin Kimmerle
+ *
+ * This program is free software: you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later
+ * version.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program. If not, see {tag: "http"://www.gnu.org/licenses}.
+ *
+ * ***** END LICENSE BLOCK *****
+ */
+
+let WindowListener = (function() {
+  let self = {};
+
+  let nextWinID = 0;
+  let listeners = {};
+
+
+  let addEvLis = function(eventName, winID) {
+    if ((typeof listeners[winID]) !== 'undefined' &&
+        listeners[winID][eventName] !== null) {
+      listeners[winID].window.addEventListener(eventName,
+                                               listeners[winID][eventName],
+                                               false);
+    }
+  };
+
+  let removeEvLis = function(eventName, winID) {
+    if (typeof listeners[winID] !== 'undefined' &&
+        listeners[winID][eventName] !== null) {
+      listeners[winID].window.removeEventListener(eventName,
+                                                  listeners[winID][eventName]);
+      if (eventName == 'unload') {
+        // when removing the 'unload' listener, also remove the 'load'
+        // listener and then delete listener[winID].
+        removeEvLis("load", winID);
+        // cleaning up -- listeners[winID] is not needed anymore
+        delete listeners[winID];
+      }
+    }
+  };
+
+
+
+  let addEventListenersToWindow = function(window) {
+    let winID = nextWinID++;
+
+    // ===========================
+    // create new functions specific for each window.
+    // ----------------------------------------------
+    let onLoad = function(event) {
+      removeEvLis("load", winID);
+      
+      if (window.document.documentElement.getAttribute("windowtype") ==
+          "navigator:browser") {
+        if (!!externalLoadFunction) {
+          externalLoadFunction(window);
+        }
+      } else {
+        removeEvLis("unload", winID);
+      }
+    };
+    let onUnload = function(event) {
+      removeEvLis("unload", onUnload);
+
+      if (window.document.documentElement.getAttribute("windowtype") ==
+          "navigator:browser") {
+        if (!!externalUnloadFunction) {
+          externalUnloadFunction(window);
+        }
+      }
+    };
+    // ===========================
+
+    listeners[winID] = {window: window, load: onLoad, unload: onUnload};
+
+    // Event handler for when the window is closed. We listen for "unload"
+    // rather than "close" because "close" will fire when a print preview
+    // opened from this window is closed.
+    addEvLis("unload", winID);
+
+    // Registers event handlers for documents loaded in the window.
+    addEvLis("load", winID);
+
+    return winID;
+  };
+
+
+  function removeAllEventListeners() {
+    for (let winID in listeners) {
+      removeEvLis("load", winID);
+      removeEvLis("unload", winID);
+    }
+    listeners = {};
+    nextWinID = 0;
+  }
+
+
+
+  // external functions to be called on "load" or "unload" events
+  let externalLoadFunction = null;
+  let externalUnloadFunction = null;
+  self.setLoadFunction = function(f) {
+    externalLoadFunction = f;
+  };
+  self.setUnloadFunction = function(f) {
+    externalUnloadFunction = f;
+  };
+
+
+  let listening = false;
+  self.startListening = function() {
+    if (listening === false) {
+      Services.wm.addListener(WindowListener);
+      listening = true;
+    }
+  };
+  self.stopListening = function() {
+    if (listening === true) {
+      Services.wm.removeListener(WindowListener);
+      listening = false;
+    }
+    // remove all "load" and "unload" event listeners.
+    removeAllEventListeners();
+  };
+
+
+
+  self.onOpenWindow = function(xulWindow) {
+    let window = xulWindow.QueryInterface(Ci.nsIInterfaceRequestor)
+        .getInterface(Ci.nsIDOMWindow);
+    addEventListenersToWindow(window);
+  };
+  self.onCloseWindow = function(xulWindow) {};
+  self.onWindowTitleChange = function(xulWindow, newTitle) {};
+
+  return self;
+}());
diff --git a/src/content/ui/overlay.js b/src/content/ui/overlay.js
index eaa5264..a1c1779 100644
--- a/src/content/ui/overlay.js
+++ b/src/content/ui/overlay.js
@@ -52,6 +52,10 @@ requestpolicy.overlay = (function() {
   let {rpService} = iMod("requestpolicy-service");
   let {MMID} = iMod("constants");
 
+  let $ = function(id) {
+    return document.getElementById(id);
+  }
+
   //let _extensionConflictInfoUri = "http://www.requestpolicy.com/conflict?ext=";
 
   //let _prefetchInfoUri = "http://www.requestpolicy.com/help/prefetch.html";
@@ -148,6 +152,10 @@ requestpolicy.overlay = (function() {
     RequestProcessor.removeRequestObserver(self);
     self._removeHistoryObserver();
     self._removeLocationObserver();
+
+    if ($("requestpolicy-requestLog").hidden === false) {
+      self.toggleRequestLog();
+    }
   };
 
   /**

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



More information about the Pkg-mozext-commits mailing list