From 8c5f95df3b4cc9b8f34f1727f09865971c13714f Mon Sep 17 00:00:00 2001 From: Vivien Nicolas <21@vingtetun.org> Date: Fri, 4 May 2012 20:02:05 +0200 Subject: [PATCH] Bug 748077 - Remove some leaks in b2g/. r=fabrice --- b2g/chrome/content/shell.js | 59 +++++++++++++++++++++--------------- b2g/chrome/content/webapi.js | 7 ++--- 2 files changed, 38 insertions(+), 28 deletions(-) diff --git a/b2g/chrome/content/shell.js b/b2g/chrome/content/shell.js index df2dbc61c39..0ab2f03bd55 100644 --- a/b2g/chrome/content/shell.js +++ b/b2g/chrome/content/shell.js @@ -14,36 +14,31 @@ Cu.import('resource://gre/modules/Services.jsm'); Cu.import('resource://gre/modules/ContactService.jsm'); Cu.import('resource://gre/modules/Webapps.jsm'); -XPCOMUtils.defineLazyGetter(Services, 'env', function() { - return Cc['@mozilla.org/process/environment;1'] - .getService(Ci.nsIEnvironment); -}); +XPCOMUtils.defineLazyServiceGetter(Services, 'env', + '@mozilla.org/process/environment;1', + 'nsIEnvironment'); -XPCOMUtils.defineLazyGetter(Services, 'ss', function() { - return Cc['@mozilla.org/content/style-sheet-service;1'] - .getService(Ci.nsIStyleSheetService); -}); +XPCOMUtils.defineLazyServiceGetter(Services, 'ss', + '@mozilla.org/content/style-sheet-service;1', + 'nsIStyleSheetService'); -XPCOMUtils.defineLazyGetter(Services, 'idle', function() { - return Cc['@mozilla.org/widget/idleservice;1'] - .getService(Ci.nsIIdleService); -}); +XPCOMUtils.defineLazyServiceGetter(Services, 'idle', + '@mozilla.org/widget/idleservice;1', + 'nsIIdleService'); -XPCOMUtils.defineLazyGetter(Services, 'audioManager', function() { #ifdef MOZ_WIDGET_GONK - return Cc['@mozilla.org/telephony/audiomanager;1'] - .getService(Ci.nsIAudioManager); +XPCOMUtils.defineLazyServiceGetter(Services, 'audioManager', + '@mozilla.org/telephony/audiomanager;1', + 'nsIAudioManager'); #else - return { - "masterVolume": 0 - }; +Services.audioManager = { + 'masterVolume': 0 +}; #endif -}); -XPCOMUtils.defineLazyServiceGetter(Services, 'fm', function() { - return Cc['@mozilla.org/focus-manager;1'] - .getService(Ci.nsFocusManager); -}); +XPCOMUtils.defineLazyServiceGetter(Services, 'fm', + '@mozilla.org/focus-manager;1', + 'nsIFocusManager'); XPCOMUtils.defineLazyGetter(this, 'DebuggerServer', function() { Cu.import('resource://gre/modules/devtools/dbg-server.jsm'); @@ -144,9 +139,20 @@ var shell = { }, stop: function shell_stop() { + ['keydown', 'keypress', 'keyup'].forEach((function unlistenKey(type) { + window.removeEventListener(type, this, false, true); + window.removeEventListener(type, this, true, true); + }).bind(this)); + + window.addEventListener('MozApplicationManifest', this); window.removeEventListener('MozApplicationManifest', this); window.removeEventListener('mozfullscreenchange', this); window.removeEventListener('sizemodechange', this); + this.contentBrowser.removeEventListener('load', this, true); + +#ifndef MOZ_WIDGET_GONK + delete Services.audioManager; +#endif }, toggleDebug: function shell_toggleDebug() { @@ -313,7 +319,7 @@ var shell = { } } - let wakeLockHandler = function wakeLockHandler(topic, state) { + let wakeLockHandler = function(topic, state) { // Turn off the screen when no one needs the it or all of them are // invisible, otherwise turn the screen on. Note that the CPU // might go to sleep as soon as the screen is turned off and @@ -354,6 +360,11 @@ var shell = { } }; + window.addEventListener('unload', function removeIdleObjects() { + Services.idle.removeIdleObserver(idleHandler, idleTimeout); + power.removeWakeLockListener(wakeLockHandler); + }); + // XXX We may override other's callback here, but this is the only // user of mozSettings in shell.js at this moment. navigator.mozSettings.onsettingchange = function onSettingChange(e) { diff --git a/b2g/chrome/content/webapi.js b/b2g/chrome/content/webapi.js index e813b925fc4..73730bb2c08 100644 --- a/b2g/chrome/content/webapi.js +++ b/b2g/chrome/content/webapi.js @@ -13,10 +13,9 @@ Cu.import('resource://gre/modules/XPCOMUtils.jsm'); Cu.import('resource://gre/modules/Services.jsm'); Cu.import('resource://gre/modules/Geometry.jsm'); -XPCOMUtils.defineLazyGetter(Services, 'fm', function() { - return Cc['@mozilla.org/focus-manager;1'] - .getService(Ci.nsIFocusManager); -}); +XPCOMUtils.defineLazyServiceGetter(Services, 'fm', + '@mozilla.org/focus-manager;1', + 'nsIFocusManager'); // MozKeyboard (function VirtualKeyboardManager() {