From 37e994d1271172b827a4cc65f861eefe8eecbe02 Mon Sep 17 00:00:00 2001 From: Mook Date: Sat, 20 Oct 2012 21:40:56 -0700 Subject: [PATCH] bug 782549 - check if the window has device sensor listeners, instead of global listener count (r=dougt) --- content/events/test/test_bug742376.html | 11 +++++++---- dom/system/nsDeviceSensors.cpp | 11 +++++------ xpcom/system/nsIDeviceSensors.idl | 7 +++++-- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/content/events/test/test_bug742376.html b/content/events/test/test_bug742376.html index 4d57b4806ae5..b47f2c2f7480 100644 --- a/content/events/test/test_bug742376.html +++ b/content/events/test/test_bug742376.html @@ -16,16 +16,16 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=402089 /** Test for Bug 742376 **/ -function getListenerCount() { +function hasListeners() { var Cc = SpecialPowers.Cc; var Ci = SpecialPowers.Ci; var dss = Cc["@mozilla.org/devicesensors;1"].getService(Ci.nsIDeviceSensors); - return dss.listenerCount(Ci.nsIDeviceSensorData.TYPE_ORIENTATION); + return dss.hasWindowListener(Ci.nsIDeviceSensorData.TYPE_ORIENTATION, window); } -var startListenerCount = getListenerCount(); +is(hasListeners(), false, "Must not have listeners before tests start"); function dumbListener(event) {} function dumbListener2(event) {} @@ -33,14 +33,17 @@ function dumbListener2(event) {} window.addEventListener("deviceorientation", dumbListener, false); window.addEventListener("deviceorientation", dumbListener2, false); +is(hasListeners(), true, "Listeners should have been added"); + window.setTimeout(function() { window.removeEventListener("deviceorientation", dumbListener, false); + is(hasListeners(), true, "Only some listeners should have been removed"); window.setTimeout(function() { window.removeEventListener("deviceorientation", dumbListener2, false); window.setTimeout(function() { - is (getListenerCount(), startListenerCount, "Must have the same listeners at this point"); + is(hasListeners(), false, "Listeners should have been removed"); SimpleTest.finish(); }, 0); }, 0); diff --git a/dom/system/nsDeviceSensors.cpp b/dom/system/nsDeviceSensors.cpp index d42753878996..63b83e06fe64 100644 --- a/dom/system/nsDeviceSensors.cpp +++ b/dom/system/nsDeviceSensors.cpp @@ -119,14 +119,13 @@ nsDeviceSensors::~nsDeviceSensors() } } -NS_IMETHODIMP nsDeviceSensors::ListenerCount(uint32_t aType, int32_t *aRetVal) +NS_IMETHODIMP nsDeviceSensors::HasWindowListener(uint32_t aType, nsIDOMWindow *aWindow, bool *aRetVal) { - if (!mEnabled) { - *aRetVal = 0; - return NS_OK; - } + if (!mEnabled) + *aRetVal = false; + else + *aRetVal = mWindowListeners[aType]->IndexOf(aWindow) != NoIndex; - *aRetVal = mWindowListeners[aType]->Length(); return NS_OK; } diff --git a/xpcom/system/nsIDeviceSensors.idl b/xpcom/system/nsIDeviceSensors.idl index b57f878afc30..6c805c445886 100644 --- a/xpcom/system/nsIDeviceSensors.idl +++ b/xpcom/system/nsIDeviceSensors.idl @@ -24,10 +24,13 @@ interface nsIDeviceSensorData : nsISupports readonly attribute double z; }; -[scriptable, uuid(83306c9f-1c8f-43c4-900a-245d7f219511)] +[scriptable, uuid(e46e47c7-55ff-44c4-abce-21b14ba07f86)] interface nsIDeviceSensors : nsISupports { - long listenerCount(in unsigned long aType); + /** + * Returns true if the given window has any listeners of the given type + */ + bool hasWindowListener(in unsigned long aType, in nsIDOMWindow aWindow); // Holds pointers, not AddRef objects -- it is up to the caller // to call RemoveWindowListener before the window is deleted.