bug 782549 - check if the window has device sensor listeners, instead of global listener count (r=dougt)
authorMook <mook.moz+hg@gmail.com>
Sat, 20 Oct 2012 21:40:56 -0700
changeset 111454 888ac1c4683084aaea58aeba89dbb50074b4008a
parent 111453 acbfd1eebb3bdabb5f02a83f67dfe66a49938c6a
child 111455 cb88e87e664c924b5b764ce1fca2397cb740a675
push id93
push usernmatsakis@mozilla.com
push dateWed, 31 Oct 2012 21:26:57 +0000
reviewersdougt
bugs782549
milestone19.0a1
bug 782549 - check if the window has device sensor listeners, instead of global listener count (r=dougt)
content/events/test/test_bug742376.html
dom/system/nsDeviceSensors.cpp
xpcom/system/nsIDeviceSensors.idl
--- a/content/events/test/test_bug742376.html
+++ b/content/events/test/test_bug742376.html
@@ -11,41 +11,44 @@ https://bugzilla.mozilla.org/show_bug.cg
 </head>
 
 <body>
 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=742376">Mozilla Bug 742376</a>
 <script class="testbody" type="text/javascript">
 
 /** 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) {}
 
 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);
 }, 0);
 
 SimpleTest.waitForExplicitFinish();
 
 </script>
--- a/dom/system/nsDeviceSensors.cpp
+++ b/dom/system/nsDeviceSensors.cpp
@@ -114,24 +114,23 @@ nsDeviceSensors::~nsDeviceSensors()
       UnregisterSensorObserver((SensorType)i, this);
   }
 
   for (int i = 0; i < NUM_SENSOR_TYPE; i++) {
     delete mWindowListeners[i];
   }
 }
 
-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;
 }
 
 NS_IMETHODIMP nsDeviceSensors::AddWindowListener(uint32_t aType, nsIDOMWindow *aWindow)
 {
   if (!mEnabled)
     return NS_OK;
 
--- a/xpcom/system/nsIDeviceSensors.idl
+++ b/xpcom/system/nsIDeviceSensors.idl
@@ -19,20 +19,23 @@ interface nsIDeviceSensorData : nsISuppo
 
   readonly attribute unsigned long type;
 
   readonly attribute double x;
   readonly attribute double y;
   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.
 
   [noscript] void addWindowListener(in unsigned long aType, in nsIDOMWindow aWindow);
   [noscript] void removeWindowListener(in unsigned long aType, in nsIDOMWindow aWindow);
   [noscript] void removeWindowAsListener(in nsIDOMWindow aWindow);
 };