From fa935f4f84e4577b5b15146720bdba571db77367 Mon Sep 17 00:00:00 2001 From: "dbaron%fas.harvard.edu" Date: Wed, 6 Nov 2002 12:55:00 +0000 Subject: [PATCH] Fix shutdown leaks of observer service via cycles through JS caused by closures and global variables. b=170022 r=morse sr=jag --- .../resources/content/cookieTasksOverlay.xul | 47 +++++++++---------- xpfe/browser/resources/content/navigator.js | 5 +- .../content/cookieTasksOverlay.xul | 47 +++++++++---------- 3 files changed, 50 insertions(+), 49 deletions(-) diff --git a/extensions/cookie/resources/content/cookieTasksOverlay.xul b/extensions/cookie/resources/content/cookieTasksOverlay.xul index cf932eb804e..bcc437a4a56 100644 --- a/extensions/cookie/resources/content/cookieTasksOverlay.xul +++ b/extensions/cookie/resources/content/cookieTasksOverlay.xul @@ -34,8 +34,6 @@ /******* THE FOLLOWING IS FOR THE STATUSBAR OVERLAY *******/ - var gObserverService; - var gPref; var gButtonPressed; var cookieIconObserver = { @@ -68,10 +66,8 @@ }; function viewP3PDialog() { - if (!gObserverService) { - gObserverService = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService); - } - gObserverService.notifyObservers(null, "cookieIcon", "off"); + var observerService = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService); + observerService.notifyObservers(null, "cookieIcon", "off"); window.openDialog ("chrome://cookie/content/p3pDialog.xul","_blank","modal=yes,chrome,resizable=no", this); @@ -82,28 +78,31 @@ } } - // determine if p3p pref is set - gPref = Components.classes['@mozilla.org/preferences-service;1']; - gPref = gPref.getService(); - gPref = gPref.QueryInterface(Components.interfaces.nsIPrefBranch); - if (gPref.getIntPref("network.cookie.cookieBehavior") == 3) { + function CookieTasksStartup() { + // determine if p3p pref is set + var pref = Components.classes['@mozilla.org/preferences-service;1']. + getService(Components.interfaces.nsIPrefBranch); + if (pref.getIntPref("network.cookie.cookieBehavior") == 3) { - // make sure p3p dll exists, else we can't keep pref set - if (!("@mozilla.org/cookie-consent;1" in Components.classes)) { - gPref.setIntPref("network.cookie.cookieBehavior", 0); + // make sure p3p dll exists, else we can't keep pref set + if (!("@mozilla.org/cookie-consent;1" in Components.classes)) { + pref.setIntPref("network.cookie.cookieBehavior", 0); + } + } + + if ("@mozilla.org/cookie-consent;1" in Components.classes) { + + // p3p dll exists so create an observer for changes in visibility of cookie icon + var observerService = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService); + observerService.addObserver(cookieIconObserver, "cookieIcon", false); + + // determine whether or not cookie icon in this new window should be displayed + // we need to wait until window is created before displaying icon + addEventListener("load", listener, false); } } - if ("@mozilla.org/cookie-consent;1" in Components.classes) { - - // p3p dll exists so create an observer for changes in visibility of cookie icon - var observerService = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService); - observerService.addObserver(cookieIconObserver, "cookieIcon", false); - - // determine whether or not cookie icon in this new window should be displayed - // we need to wait until window is created before displaying icon - addEventListener("load", listener, false); - } + CookieTasksStartup(); ]]> diff --git a/xpfe/browser/resources/content/navigator.js b/xpfe/browser/resources/content/navigator.js index a95bb4f1cba..782eebb158f 100644 --- a/xpfe/browser/resources/content/navigator.js +++ b/xpfe/browser/resources/content/navigator.js @@ -373,9 +373,12 @@ function RegisterTabOpenObserver() } }; - const service = Components.classes["@mozilla.org/observer-service;1"] + var service = Components.classes["@mozilla.org/observer-service;1"] .getService(Components.interfaces.nsIObserverService); service.addObserver(observer, "open-new-tab-request", false); + // Null out service variable so the closure of the observer doesn't + // own the service and create a cycle (bug 170022). + service = null; } function Startup() diff --git a/xpfe/components/permissions/content/cookieTasksOverlay.xul b/xpfe/components/permissions/content/cookieTasksOverlay.xul index cf932eb804e..bcc437a4a56 100644 --- a/xpfe/components/permissions/content/cookieTasksOverlay.xul +++ b/xpfe/components/permissions/content/cookieTasksOverlay.xul @@ -34,8 +34,6 @@ /******* THE FOLLOWING IS FOR THE STATUSBAR OVERLAY *******/ - var gObserverService; - var gPref; var gButtonPressed; var cookieIconObserver = { @@ -68,10 +66,8 @@ }; function viewP3PDialog() { - if (!gObserverService) { - gObserverService = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService); - } - gObserverService.notifyObservers(null, "cookieIcon", "off"); + var observerService = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService); + observerService.notifyObservers(null, "cookieIcon", "off"); window.openDialog ("chrome://cookie/content/p3pDialog.xul","_blank","modal=yes,chrome,resizable=no", this); @@ -82,28 +78,31 @@ } } - // determine if p3p pref is set - gPref = Components.classes['@mozilla.org/preferences-service;1']; - gPref = gPref.getService(); - gPref = gPref.QueryInterface(Components.interfaces.nsIPrefBranch); - if (gPref.getIntPref("network.cookie.cookieBehavior") == 3) { + function CookieTasksStartup() { + // determine if p3p pref is set + var pref = Components.classes['@mozilla.org/preferences-service;1']. + getService(Components.interfaces.nsIPrefBranch); + if (pref.getIntPref("network.cookie.cookieBehavior") == 3) { - // make sure p3p dll exists, else we can't keep pref set - if (!("@mozilla.org/cookie-consent;1" in Components.classes)) { - gPref.setIntPref("network.cookie.cookieBehavior", 0); + // make sure p3p dll exists, else we can't keep pref set + if (!("@mozilla.org/cookie-consent;1" in Components.classes)) { + pref.setIntPref("network.cookie.cookieBehavior", 0); + } + } + + if ("@mozilla.org/cookie-consent;1" in Components.classes) { + + // p3p dll exists so create an observer for changes in visibility of cookie icon + var observerService = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService); + observerService.addObserver(cookieIconObserver, "cookieIcon", false); + + // determine whether or not cookie icon in this new window should be displayed + // we need to wait until window is created before displaying icon + addEventListener("load", listener, false); } } - if ("@mozilla.org/cookie-consent;1" in Components.classes) { - - // p3p dll exists so create an observer for changes in visibility of cookie icon - var observerService = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService); - observerService.addObserver(cookieIconObserver, "cookieIcon", false); - - // determine whether or not cookie icon in this new window should be displayed - // we need to wait until window is created before displaying icon - addEventListener("load", listener, false); - } + CookieTasksStartup(); ]]>