Skip to content

Commit

Permalink
Implement AbortSignal.abort()
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=223071
<rdar://problem/75575483>

Reviewed by Darin Adler.

LayoutTests/imported/w3c:

Resync WPT test from upstream a8cbe9c712ad032d36 to gain test coverage.

* web-platform-tests/dom/abort/event.any-expected.txt:
* web-platform-tests/dom/abort/event.any.js:
* web-platform-tests/dom/abort/event.any.worker-expected.txt:

Source/WebCore:

Implement AbortSignal.abort() which creates and returns an already aborted AbortSignal:
- whatwg/dom#960
- web-platform-tests/wpt#28003

No new tests, covered by updated test.

* dom/AbortSignal.cpp:
(WebCore::AbortSignal::createAborted):
(WebCore::AbortSignal::AbortSignal):
* dom/AbortSignal.h:
* dom/AbortSignal.idl:


git-svn-id: http://svn.webkit.org/repository/webkit/trunk@274773 268f45cc-cd09-0410-ab3c-d52691b4dbfc
  • Loading branch information
cdumez@apple.com committed Mar 22, 2021
1 parent b7b4aff commit 70419e1
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 17 deletions.
14 changes: 14 additions & 0 deletions LayoutTests/imported/w3c/ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
2021-03-22 Chris Dumez <cdumez@apple.com>

Implement AbortSignal.abort()
https://bugs.webkit.org/show_bug.cgi?id=223071
<rdar://problem/75575483>

Reviewed by Darin Adler.

Resync WPT test from upstream a8cbe9c712ad032d36 to gain test coverage.

* web-platform-tests/dom/abort/event.any-expected.txt:
* web-platform-tests/dom/abort/event.any.js:
* web-platform-tests/dom/abort/event.any.worker-expected.txt:

2021-03-19 Chris Dumez <cdumez@apple.com>

Resync webaudio web-platform-tests from upstream
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ PASS controller.signal should always return the same object
PASS controller.abort() should do nothing the second time it is called
PASS event handler should not be called if added after controller.abort()
PASS the abort event should have the right properties
PASS the AbortSignal.abort() static returns an already aborted signal

Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,9 @@ test(t => {
controller.abort();
}, "the abort event should have the right properties");

test(t => {
const signal = AbortSignal.abort();
assert_true(signal.aborted);
}, "the AbortSignal.abort() static returns an already aborted signal");

done();
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ PASS controller.signal should always return the same object
PASS controller.abort() should do nothing the second time it is called
PASS event handler should not be called if added after controller.abort()
PASS the abort event should have the right properties
PASS the AbortSignal.abort() static returns an already aborted signal

20 changes: 20 additions & 0 deletions Source/WebCore/ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
2021-03-22 Chris Dumez <cdumez@apple.com>

Implement AbortSignal.abort()
https://bugs.webkit.org/show_bug.cgi?id=223071
<rdar://problem/75575483>

Reviewed by Darin Adler.

Implement AbortSignal.abort() which creates and returns an already aborted AbortSignal:
- https://github.com/whatwg/dom/pull/960
- https://github.com/web-platform-tests/wpt/pull/28003

No new tests, covered by updated test.

* dom/AbortSignal.cpp:
(WebCore::AbortSignal::createAborted):
(WebCore::AbortSignal::AbortSignal):
* dom/AbortSignal.h:
* dom/AbortSignal.idl:

2021-03-22 Wenson Hsieh <wenson_hsieh@apple.com>

[macOS] Context menu should account for image overlay content
Expand Down
8 changes: 4 additions & 4 deletions Source/WebCore/Modules/fetch/FetchRequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ ExceptionOr<void> FetchRequest::initializeWith(const String& url, Init&& init)

if (init.signal) {
if (auto* signal = JSAbortSignal::toWrapped(scriptExecutionContext()->vm(), init.signal))
m_signal->follow(*signal);
m_signal->signalFollow(*signal);
else if (!init.signal.isUndefinedOrNull()) {
if (auto exception = processInvalidSignal(*scriptExecutionContext()))
return WTFMove(*exception);
Expand Down Expand Up @@ -214,14 +214,14 @@ ExceptionOr<void> FetchRequest::initializeWith(FetchRequest& input, Init&& init)

if (init.signal && !init.signal.isUndefined()) {
if (auto* signal = JSAbortSignal::toWrapped(scriptExecutionContext()->vm(), init.signal))
m_signal->follow(*signal);
m_signal->signalFollow(*signal);
else if (!init.signal.isNull()) {
if (auto exception = processInvalidSignal(*scriptExecutionContext()))
return WTFMove(*exception);
}

} else
m_signal->follow(input.m_signal.get());
m_signal->signalFollow(input.m_signal.get());

if (init.hasMembers()) {
auto fillResult = init.headers ? m_headers->fill(*init.headers) : m_headers->fill(input.headers());
Expand Down Expand Up @@ -324,7 +324,7 @@ ExceptionOr<Ref<FetchRequest>> FetchRequest::clone(ScriptExecutionContext& conte

auto clone = adoptRef(*new FetchRequest(context, WTF::nullopt, FetchHeaders::create(m_headers.get()), ResourceRequest { m_request }, FetchOptions { m_options}, String { m_referrer }));
clone->cloneBody(*this);
clone->m_signal->follow(m_signal);
clone->m_signal->signalFollow(m_signal);
return clone;
}

Expand Down
2 changes: 1 addition & 1 deletion Source/WebCore/dom/AbortController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ AbortSignal& AbortController::signal()

void AbortController::abort()
{
m_signal->abort();
m_signal->signalAbort();
}

}
22 changes: 14 additions & 8 deletions Source/WebCore/dom/AbortSignal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,20 @@ Ref<AbortSignal> AbortSignal::create(ScriptExecutionContext& context)
return adoptRef(*new AbortSignal(context));
}

AbortSignal::AbortSignal(ScriptExecutionContext& context)
// https://dom.spec.whatwg.org/#dom-abortsignal-abort
Ref<AbortSignal> AbortSignal::abort(ScriptExecutionContext& context)
{
return adoptRef(*new AbortSignal(context, Aborted::Yes));
}

AbortSignal::AbortSignal(ScriptExecutionContext& context, Aborted aborted)
: ContextDestructionObserver(&context)
, m_aborted(aborted == Aborted::Yes)
{
}

// https://dom.spec.whatwg.org/#abortsignal-signal-abort
void AbortSignal::abort()
void AbortSignal::signalAbort()
{
// 1. If signal's aborted flag is set, then return.
if (m_aborted)
Expand All @@ -57,7 +64,7 @@ void AbortSignal::abort()
m_aborted = true;

auto protectedThis = makeRef(*this);
auto algorithms = WTFMove(m_algorithms);
auto algorithms = std::exchange(m_algorithms, { });
for (auto& algorithm : algorithms)
algorithm();

Expand All @@ -66,22 +73,21 @@ void AbortSignal::abort()
}

// https://dom.spec.whatwg.org/#abortsignal-follow
void AbortSignal::follow(AbortSignal& signal)
void AbortSignal::signalFollow(AbortSignal& signal)
{
if (aborted())
return;

if (signal.aborted()) {
abort();
signalAbort();
return;
}

ASSERT(!m_followingSignal);
m_followingSignal = makeWeakPtr(signal);
signal.addAlgorithm([weakThis = makeWeakPtr(this)] {
if (!weakThis)
return;
weakThis->abort();
if (weakThis)
weakThis->signalAbort();
});
}

Expand Down
11 changes: 7 additions & 4 deletions Source/WebCore/dom/AbortSignal.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,12 @@ class AbortSignal final : public RefCounted<AbortSignal>, public EventTargetWith
public:
static Ref<AbortSignal> create(ScriptExecutionContext&);

static Ref<AbortSignal> abort(ScriptExecutionContext&);

static bool whenSignalAborted(AbortSignal&, Ref<AbortAlgorithm>&&);

void abort();
void signalAbort();
void signalFollow(AbortSignal&);

bool aborted() const { return m_aborted; }

Expand All @@ -55,13 +58,13 @@ class AbortSignal final : public RefCounted<AbortSignal>, public EventTargetWith
using Algorithm = Function<void()>;
void addAlgorithm(Algorithm&& algorithm) { m_algorithms.append(WTFMove(algorithm)); }

void follow(AbortSignal&);

bool isFollowingSignal() const { return !!m_followingSignal; }

private:
explicit AbortSignal(ScriptExecutionContext&);
enum class Aborted : bool { No, Yes };
explicit AbortSignal(ScriptExecutionContext&, Aborted = Aborted::No);

// EventTarget.
EventTargetInterface eventTargetInterface() const final { return AbortSignalEventTargetInterfaceType; }
ScriptExecutionContext* scriptExecutionContext() const final { return ContextDestructionObserver::scriptExecutionContext(); }
void refEventTarget() final { ref(); }
Expand Down
1 change: 1 addition & 0 deletions Source/WebCore/dom/AbortSignal.idl
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
] interface AbortSignal : EventTarget {
[ PrivateIdentifier ] static undefined whenSignalAborted(AbortSignal object, AbortAlgorithm algorithm);

[NewObject, CallWith=ScriptExecutionContext] static AbortSignal abort();
readonly attribute boolean aborted;
attribute EventHandler onabort;
};

0 comments on commit 70419e1

Please sign in to comment.