Skip to content

Commit

Permalink
[dom] Add AbortSignal::CanAbort()
Browse files Browse the repository at this point in the history
CanAbort() returns true if the signal still might abort, which is true
until the signal is settled. This will be used in the scheduler to
implement yield() inheritance, both to determine the signal to use for
inheritance and to avoid adding abort algorithms to fixed priority
non-abortable signal.

Bug: 979020
Change-Id: Ib2f55d5bcdf7302c39c1751b11c7f010c986fa27
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4323140
Commit-Queue: Scott Haseley <shaseley@chromium.org>
Reviewed-by: Nate Chapin <japhet@chromium.org>
Reviewed-by: Mason Freed <masonf@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1115572}
  • Loading branch information
shaseley authored and Chromium LUCI CQ committed Mar 10, 2023
1 parent c25f49c commit 22fb408
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 16 deletions.
10 changes: 10 additions & 0 deletions third_party/blink/renderer/core/dom/abort_signal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,16 @@ bool AbortSignal::HasPendingActivity() const {
!abort_algorithms_->Empty();
}

bool AbortSignal::CanAbort() const {
if (aborted()) {
return false;
}
if (RuntimeEnabledFeatures::AbortSignalCompositionEnabled()) {
return !composition_manager_->IsSettled();
}
return true;
}

AbortSignal::AlgorithmHandle::AlgorithmHandle(AbortSignal::Algorithm* algorithm)
: algorithm_(algorithm) {}

Expand Down
3 changes: 3 additions & 0 deletions third_party/blink/renderer/core/dom/abort_signal.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ class CORE_EXPORT AbortSignal : public EventTargetWithInlineData,
return signal_type_ == AbortSignal::SignalType::kComposite;
}

// Returns true if this signal has not aborted and still might abort.
bool CanAbort() const;

// Returns the composition manager for this signal for the given type.
// Subclasses are expected to override this to return the composition manager
// associated with their type.
Expand Down
75 changes: 59 additions & 16 deletions third_party/blink/renderer/core/dom/abort_signal_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@

#include "base/test/scoped_feature_list.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/platform/web_runtime_features.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_binding_for_core.h"
#include "third_party/blink/renderer/core/dom/abort_controller.h"
#include "third_party/blink/renderer/core/dom/abort_signal_registry.h"
#include "third_party/blink/renderer/core/dom/events/native_event_listener.h"
#include "third_party/blink/renderer/core/frame/local_dom_window.h"
Expand All @@ -22,7 +24,18 @@ namespace blink {

namespace {

enum class TestType { kRemoveEnabled, kRemoveDisabled };
enum class TestType { kRemoveEnabled, kCompositionEnabled, kNoFeatures };

const char* TestTypeToString(TestType test_type) {
switch (test_type) {
case TestType::kRemoveEnabled:
return "RemoveEnabled";
case TestType::kCompositionEnabled:
return "CompositionEnabled";
case TestType::kNoFeatures:
return "NoFeatures";
}
}

class TestEventListener : public NativeEventListener {
public:
Expand All @@ -37,32 +50,45 @@ class AbortSignalTest : public PageTestBase,
public ::testing::WithParamInterface<TestType> {
public:
AbortSignalTest() {
if (GetParam() == TestType::kRemoveEnabled) {
feature_list_.InitWithFeatures({features::kAbortSignalHandleBasedRemoval},
{});
} else {
feature_list_.InitWithFeatures(
{}, {features::kAbortSignalHandleBasedRemoval});
switch (GetParam()) {
case TestType::kRemoveEnabled:
feature_list_.InitWithFeatures(
{features::kAbortSignalHandleBasedRemoval},
{features::kAbortSignalComposition});
break;
case TestType::kCompositionEnabled:
feature_list_.InitWithFeatures(
{features::kAbortSignalComposition},
{features::kAbortSignalHandleBasedRemoval});
break;
case TestType::kNoFeatures:
feature_list_.InitWithFeatures(
{}, {features::kAbortSignalHandleBasedRemoval,
features::kAbortSignalComposition});
break;
}
WebRuntimeFeatures::UpdateStatusFromBaseFeatures();
}

void SetUp() override {
PageTestBase::SetUp();

signal_ = MakeGarbageCollected<AbortSignal>(GetFrame().DomWindow());
controller_ = AbortController::Create(GetFrame().DomWindow());
signal_ = controller_->signal();
}

void SignalAbort() {
ScriptState* script_state = ToScriptStateForMainWorld(&GetFrame());
ScriptState::Scope script_scope(script_state);
signal_->SignalAbort(script_state);
controller_->abort(script_state);
}

AbortSignalRegistry* GetRegistry() const {
return AbortSignalRegistry::From(*GetFrame().DomWindow());
}

protected:
Persistent<AbortController> controller_;
Persistent<AbortSignal> signal_;
Persistent<AbortSignal::AlgorithmHandle> abort_handle_;
base::test::ScopedFeatureList feature_list_;
Expand Down Expand Up @@ -144,14 +170,31 @@ TEST_P(AbortSignalTest, RegisteredSignalAlgorithmListenerGCed) {
INSTANTIATE_TEST_SUITE_P(,
AbortSignalTest,
testing::Values(TestType::kRemoveEnabled,
TestType::kRemoveDisabled),
TestType::kNoFeatures),
[](const testing::TestParamInfo<TestType>& info) {
return TestTypeToString(info.param);
});

class AbortSignalCompositionTest : public AbortSignalTest {};

TEST_P(AbortSignalCompositionTest, CanAbort) {
EXPECT_TRUE(signal_->CanAbort());
SignalAbort();
EXPECT_FALSE(signal_->CanAbort());
}

TEST_P(AbortSignalCompositionTest, CanAbortAfterGC) {
controller_.Clear();
ThreadState::Current()->CollectAllGarbageForTesting();
EXPECT_EQ(signal_->CanAbort(), GetParam() == TestType::kNoFeatures);
}

INSTANTIATE_TEST_SUITE_P(,
AbortSignalCompositionTest,
testing::Values(TestType::kCompositionEnabled,
TestType::kNoFeatures),
[](const testing::TestParamInfo<TestType>& info) {
switch (info.param) {
case TestType::kRemoveEnabled:
return "RemoveEnabled";
case TestType::kRemoveDisabled:
return "RemoveDisabled";
}
return TestTypeToString(info.param);
});

} // namespace blink

0 comments on commit 22fb408

Please sign in to comment.