Commit cd8da5b7 authored by Joey Arhar's avatar Joey Arhar Committed by Commit Bot

Fix aborted AbortSignal in addEventListener

If you call AbortSignal.abort() and then pass that AbortSignal to
addEventListener, it shouldn't add the event listener. It currently adds
the event listener and still runs the handler, and this patch stops it
from adding the listener in that case.

Fixed: 1149553
Change-Id: I7aee86d3fb2901947054c66d50e2aef1bb9467bf
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2541503
Auto-Submit: Joey Arhar <jarhar@chromium.org>
Commit-Queue: Mason Freed <masonfreed@chromium.org>
Reviewed-by: default avatarMason Freed <masonfreed@chromium.org>
Cr-Commit-Position: refs/heads/master@{#828071}
parent 2dbf71c3
...@@ -506,6 +506,9 @@ bool EventTarget::AddEventListenerInternal( ...@@ -506,6 +506,9 @@ bool EventTarget::AddEventListenerInternal(
if (!listener) if (!listener)
return false; return false;
if (options->signal() && options->signal()->aborted())
return false;
if (event_type == event_type_names::kTouchcancel || if (event_type == event_type_names::kTouchcancel ||
event_type == event_type_names::kTouchend || event_type == event_type_names::kTouchend ||
event_type == event_type_names::kTouchmove || event_type == event_type_names::kTouchmove ||
......
...@@ -33,4 +33,24 @@ test(t => { ...@@ -33,4 +33,24 @@ test(t => {
controller.abort(); controller.abort();
target.dispatchEvent(new Event('testevent')); target.dispatchEvent(new Event('testevent'));
}, 'Tests support for EventController to cancel capture event listeners in addEventListener.'); }, 'Tests support for EventController to cancel capture event listeners in addEventListener.');
test(function() {
let count = 0;
function handler() {
count++;
}
const et = new EventTarget();
const controller = new AbortController();
et.addEventListener('test', handler, { signal: controller.signal });
et.dispatchEvent(new Event('test'));
assert_equals(count, 1, "Adding a signal still adds a listener");
et.dispatchEvent(new Event('test'));
assert_equals(count, 2, "The listener was not added with the once flag");
controller.abort();
et.dispatchEvent(new Event('test'));
assert_equals(count, 2, "Aborting on the controller removes the listener");
et.addEventListener('test', handler, { signal: controller.signal });
et.dispatchEvent(new Event('test'));
assert_equals(count, 2, "Passing an aborted signal never adds the handler");
}, "Passing an AbortSignal to addEventListener options should allow removing a listener");
</script> </script>
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment