Closed Bug 1845633 Opened 1 year ago Closed 1 year ago

DisableDevice logic is probably broken for once listeners

Categories

(Core :: DOM: Events, defect, P3)

defect

Tracking

()

RESOLVED FIXED
118 Branch
Tracking Status
firefox116 --- wontfix
firefox117 --- wontfix
firefox118 --- fixed

People

(Reporter: mstange, Assigned: mstange)

References

Details

In EventListenerManager, EnableDevice / DisableDevice need to be called once for each added/removed event listener (if it matches a certain list of event types).
nsGlobalWindowInner::EnableDeviceSensor counts the numbers of calls and expects them to be properly balanced.

EventListenerManager does this correctly for calls to AddEventListener / RemoveEventListener and SetHandler.

But for "once" listeners, we only call DisableDevice if we have no remaining listeners of that type. I'm pretty sure this is wrong - we need to call it once per removed "once" listener.

https://searchfox.org/mozilla-central/rev/bf272a7be6fef0e91bde01dfe1f68403d8fbc237/dom/events/EventListenerManager.cpp#1498-1513

if (IsDeviceType(aEvent->mMessage)) {
  // This is a device-type event, we need to check whether we can
  // disable device after removing the once listeners.
  const auto [begin, end] = mListeners.NonObservingRange();
  const bool hasAnyListener =
      std::any_of(begin, end, [aEvent](const Listener& listenerRef) {
        const Listener* listener = &listenerRef;
        return EVENT_TYPE_EQUALS(listener, aEvent->mMessage,
                                 aEvent->mSpecifiedEventType,
                                 /* all events */ false);
      });

  if (!hasAnyListener) {
    DisableDevice(aEvent->mMessage);
  }
}

The impact of this is that we may waste some power under certain conditions, by listening for device orientation / proximity / light changes unnecessarily.

Severity: -- → S3
Priority: -- → P3

The patches in bug 1834370 fix this.

Assignee: nobody → mstange.moz
Status: NEW → ASSIGNED
Depends on: 1834370
Status: ASSIGNED → RESOLVED
Closed: 1 year ago
Resolution: --- → FIXED
Target Milestone: --- → 118 Branch
You need to log in before you can comment on or make changes to this bug.