Truly independent web browser
LibWeb: Let a FetchByteChannel's consumer attach after its creation
Luke Wildea21f063cd7f2
diff --git a/Libraries/LibWeb/Fetch/Engine/FetchByteChannel.cpp b/Libraries/LibWeb/Fetch/Engine/FetchByteChannel.cppindex c2cb048d0e5..6245c5c064a 100644--- a/Libraries/LibWeb/Fetch/Engine/FetchByteChannel.cpp+++ b/Libraries/LibWeb/Fetch/Engine/FetchByteChannel.cpp@@ -8,14 +8,13 @@namespace Web::Fetch::Engine {-NonnullRefPtr<FetchByteChannel> FetchByteChannel::create(Watermarks watermarks, NonnullRefPtr<FetchByteChannelConsumerSink> consumer_sink, NonnullRefPtr<FetchByteChannelProducerSink> producer_sink)+NonnullRefPtr<FetchByteChannel> FetchByteChannel::create(Watermarks watermarks, NonnullRefPtr<FetchByteChannelProducerSink> producer_sink){- return adopt_ref(*new FetchByteChannel(watermarks, move(consumer_sink), move(producer_sink)));+ return adopt_ref(*new FetchByteChannel(watermarks, move(producer_sink)));}-FetchByteChannel::FetchByteChannel(Watermarks watermarks, NonnullRefPtr<FetchByteChannelConsumerSink> consumer_sink, NonnullRefPtr<FetchByteChannelProducerSink> producer_sink)+FetchByteChannel::FetchByteChannel(Watermarks watermarks, NonnullRefPtr<FetchByteChannelProducerSink> producer_sink): m_watermarks(watermarks)- , m_consumer_sink(move(consumer_sink)), m_producer_sink(move(producer_sink)), m_outstanding_transport_credit(watermarks.high){@@ -249,6 +248,12 @@ FetchByteChannel::TakeResult FetchByteChannel::take_locked(u64 limit, PendingNotreturn result;}+void FetchByteChannel::attach_consumer(NonnullRefPtr<FetchByteChannelConsumerSink> consumer_sink)+{+ VERIFY(!m_consumer_sink);+ m_consumer_sink = move(consumer_sink);+}+FetchByteChannel::TakeResult FetchByteChannel::take_up_to(u64 limit){PendingNotifications notifications;@@ -287,6 +292,7 @@ FetchByteChannel::TakeResult FetchByteChannel::peek_up_to(u64 limit)WakeTicket FetchByteChannel::arm_waiter(WakeOn wake_on){+ VERIFY(m_consumer_sink);wake_on.visit([](BufferedAtLeast const& buffered_at_least) { VERIFY(buffered_at_least.byte_count > 0); },[](EndOnly) { });diff --git a/Libraries/LibWeb/Fetch/Engine/FetchByteChannel.h b/Libraries/LibWeb/Fetch/Engine/FetchByteChannel.hindex c2d2b133bc2..578ba36c7bc 100644--- a/Libraries/LibWeb/Fetch/Engine/FetchByteChannel.h+++ b/Libraries/LibWeb/Fetch/Engine/FetchByteChannel.h@@ -68,7 +68,7 @@ public:u64 buffered_byte_count { 0 };};- [[nodiscard]] static NonnullRefPtr<FetchByteChannel> create(Watermarks, NonnullRefPtr<FetchByteChannelConsumerSink>, NonnullRefPtr<FetchByteChannelProducerSink>);+ [[nodiscard]] static NonnullRefPtr<FetchByteChannel> create(Watermarks, NonnullRefPtr<FetchByteChannelProducerSink>);// The credit the producer should hand the transport before the first write.[[nodiscard]] u64 initial_credit() const { return m_watermarks.high; }@@ -92,6 +92,8 @@ public:TerminateResult error(Errored);// Consumer side.+ // The consumer attaches its sink once, before any arm_waiter() call.+ void attach_consumer(NonnullRefPtr<FetchByteChannelConsumerSink>);[[nodiscard]] TakeResult take_up_to(u64 limit);[[nodiscard]] TakeResult take_all();@@ -119,7 +121,7 @@ public:void disarm_waiter(WakeTicket);private:- FetchByteChannel(Watermarks, NonnullRefPtr<FetchByteChannelConsumerSink>, NonnullRefPtr<FetchByteChannelProducerSink>);+ FetchByteChannel(Watermarks, NonnullRefPtr<FetchByteChannelProducerSink>);// Sink calls decided under the lock and made after it is released, in order.struct Wake {@@ -149,8 +151,8 @@ private:};Watermarks const m_watermarks;- NonnullRefPtr<FetchByteChannelConsumerSink> const m_consumer_sink;NonnullRefPtr<FetchByteChannelProducerSink> const m_producer_sink;+ RefPtr<FetchByteChannelConsumerSink> m_consumer_sink;mutable Mutex m_mutex;Vector<Core::ImmutableBytes> m_chunks;diff --git a/Libraries/LibWeb/Fetch/Fetching/BodyStreamPullSource.cpp b/Libraries/LibWeb/Fetch/Fetching/BodyStreamPullSource.cppindex 04df4552524..7b873b1f7e8 100644--- a/Libraries/LibWeb/Fetch/Fetching/BodyStreamPullSource.cpp+++ b/Libraries/LibWeb/Fetch/Fetching/BodyStreamPullSource.cpp@@ -318,11 +318,14 @@ void BodyStreamPullSource::drain_into_stream()// https://fetch.spec.whatwg.org/#concept-http-network-fetch// NB: Steps 10 to 13 of HTTP-network fetch.-GC::Ref<Streams::ReadableStream> create_body_stream_from_channel(JS::Realm& realm, GC::Ptr<Infrastructure::FetchParams const> fetch_params, NonnullRefPtr<Engine::FetchByteChannel> channel, BodyStreamWakeSink const& wake_sink)+GC::Ref<Streams::ReadableStream> create_body_stream_from_channel(JS::Realm& realm, GC::Ptr<Infrastructure::FetchParams const> fetch_params, NonnullRefPtr<Engine::FetchByteChannel> channel){+ auto wake_sink = BodyStreamWakeSink::create();+ channel->attach_consumer(wake_sink);+// 10. Let stream be a new ReadableStream.auto stream = Streams::ReadableStream::create(realm);- auto pull_source = GC::Heap::the().allocate<BodyStreamPullSource>(realm, fetch_params, stream, move(channel), wake_sink);+ auto pull_source = GC::Heap::the().allocate<BodyStreamPullSource>(realm, fetch_params, stream, move(channel), *wake_sink);// 11. Let pullAlgorithm be the following steps:auto pull_algorithm = GC::create_function(GC::Heap::the(), [pull_source]() {diff --git a/Libraries/LibWeb/Fetch/Fetching/BodyStreamPullSource.h b/Libraries/LibWeb/Fetch/Fetching/BodyStreamPullSource.hindex 67793bb3f59..53c88a8e376 100644--- a/Libraries/LibWeb/Fetch/Fetching/BodyStreamPullSource.h+++ b/Libraries/LibWeb/Fetch/Fetching/BodyStreamPullSource.h@@ -22,7 +22,7 @@ class BodyStreamPullSource;AK_TYPEDEF_DISTINCT_ORDERED_ID(u64, BodyStreamWakeToken);// Delivers a channel's wakes to the pull source registered under its token, on the loop that created it.-class WEB_API BodyStreamWakeSink final : public Engine::FetchByteChannelConsumerSink {+class BodyStreamWakeSink final : public Engine::FetchByteChannelConsumerSink {public:static NonnullRefPtr<BodyStreamWakeSink> create();@@ -89,7 +89,7 @@ private:bool m_detached { false };};-// A response body's stream read from channel, whose consumer sink is wake_sink.-WEB_API GC::Ref<Streams::ReadableStream> create_body_stream_from_channel(JS::Realm&, GC::Ptr<Infrastructure::FetchParams const>, NonnullRefPtr<Engine::FetchByteChannel>, BodyStreamWakeSink const&);+// A response body's stream read from channel, which becomes the channel's consumer.+WEB_API GC::Ref<Streams::ReadableStream> create_body_stream_from_channel(JS::Realm&, GC::Ptr<Infrastructure::FetchParams const>, NonnullRefPtr<Engine::FetchByteChannel>);}diff --git a/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp b/Libraries/LibWeb/Fetch/Fetching/Fetching.cppindex 12cf29a6045..f1ed72d2d1d 100644--- a/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp+++ b/Libraries/LibWeb/Fetch/Fetching/Fetching.cpp@@ -2624,12 +2624,11 @@ static NetworkBodyStream create_network_body_stream(JS::Realm& realm, GC::Ptr<In// NB: The buffer is the FetchByteChannel; its watermarks are the user-agent-chosen lower and upper limits.static constexpr Engine::FetchByteChannel::Watermarks network_body_watermarks { .low = 256 * KiB, .high = 1 * MiB };auto delivery_gate = Engine::FetchBodyDeliveryGate::create(navigation_hold);- auto wake_sink = BodyStreamWakeSink::create();- auto channel = Engine::FetchByteChannel::create(network_body_watermarks, wake_sink, delivery_gate->create_channel_sink());+ auto channel = Engine::FetchByteChannel::create(network_body_watermarks, delivery_gate->create_channel_sink());delivery_gate->grant_credit(channel->initial_credit());auto body_pump = GC::Heap::the().allocate<NetworkBodyPump>(fetch_params, channel, delivery_gate, move(http_cache));- auto stream = create_body_stream_from_channel(realm, fetch_params, channel, *wake_sink);+ auto stream = create_body_stream_from_channel(realm, fetch_params, channel);return { stream, move(channel), move(delivery_gate), body_pump };}diff --git a/Tests/LibWeb/TestFetchBodyStream.cpp b/Tests/LibWeb/TestFetchBodyStream.cppindex f1cf4e059ec..f8b3fd02835 100644--- a/Tests/LibWeb/TestFetchBodyStream.cpp+++ b/Tests/LibWeb/TestFetchBodyStream.cpp@@ -79,9 +79,8 @@ struct BodyStreamHarness {void create_stream(){- auto wake_sink = Web::Fetch::Fetching::BodyStreamWakeSink::create();- channel = FetchByteChannel::create({ .low = 16, .high = 64 }, wake_sink, adopt_ref(*new IgnoringProducerSink));- stream = Web::Fetch::Fetching::create_body_stream_from_channel(*realm, fetch_params, *channel, *wake_sink);+ channel = FetchByteChannel::create({ .low = 16, .high = 64 }, adopt_ref(*new IgnoringProducerSink));+ stream = Web::Fetch::Fetching::create_body_stream_from_channel(*realm, fetch_params, *channel);}// The bytes the channel holds that have not been pulled into the stream.diff --git a/Tests/LibWeb/TestFetchByteChannel.cpp b/Tests/LibWeb/TestFetchByteChannel.cppindex 0f8d199895c..25f2134ab71 100644--- a/Tests/LibWeb/TestFetchByteChannel.cpp+++ b/Tests/LibWeb/TestFetchByteChannel.cpp@@ -88,8 +88,9 @@ struct ChannelHarness {explicit ChannelHarness(FetchByteChannel::Watermarks watermarks = { .low = 16, .high = 64 }): consumer_sink(adopt_ref(*new RecordingConsumerSink(events))), producer_sink(adopt_ref(*new RecordingProducerSink(events)))- , channel(FetchByteChannel::create(watermarks, *consumer_sink, *producer_sink))+ , channel(FetchByteChannel::create(watermarks, *producer_sink)){+ channel->attach_consumer(*consumer_sink);}Vector<SinkEvent> take_events() { return move(events); }@@ -215,9 +216,8 @@ TEST_CASE(copied_writes_spend_credit_like_adopted_ones)TEST_CASE(a_channel_needs_a_positive_low_watermark){Vector<SinkEvent> events;- auto consumer_sink = adopt_ref(*new RecordingConsumerSink(events));auto producer_sink = adopt_ref(*new RecordingProducerSink(events));- EXPECT_DEATH("a channel without a low watermark never refills its credit", (void)FetchByteChannel::create({ .low = 0, .high = 64 }, *consumer_sink, *producer_sink));+ EXPECT_DEATH("a channel without a low watermark never refills its credit", (void)FetchByteChannel::create({ .low = 0, .high = 64 }, *producer_sink));}TEST_CASE(take_up_to_leaves_correctly_offset_remainder)@@ -335,6 +335,38 @@ TEST_CASE(a_zero_byte_threshold_dies)EXPECT_DEATH("a threshold counts at least one byte", (void)harness.channel->arm_waiter(FetchByteChannel::BufferedAtLeast { 0 }));}+TEST_CASE(arming_before_a_consumer_attaches_dies)+{+ Vector<SinkEvent> events;+ auto channel = FetchByteChannel::create({ .low = 16, .high = 64 }, adopt_ref(*new RecordingProducerSink(events)));+ EXPECT_DEATH("a waiter needs a consumer to wake", (void)channel->arm_waiter());+}++TEST_CASE(attaching_a_second_consumer_dies)+{+ ChannelHarness harness;+ EXPECT_DEATH("a channel has one consumer", harness.channel->attach_consumer(adopt_ref(*new RecordingConsumerSink(harness.events))));+}++TEST_CASE(a_channel_buffers_and_ends_before_its_consumer_attaches)+{+ Vector<SinkEvent> events;+ auto producer_sink = adopt_ref(*new RecordingProducerSink(events));+ auto channel = FetchByteChannel::create({ .low = 16, .high = 64 }, producer_sink);+ EXPECT_EQ(channel->write(bytes_of("early"sv)), WriteResult::Accepted);+ EXPECT_EQ(channel->close(), TerminateResult::Terminated);++ channel->attach_consumer(adopt_ref(*new RecordingConsumerSink(events)));+ auto ticket = channel->arm_waiter(FetchByteChannel::EndOnly {});+ EXPECT_EQ(events.size(), 1u);+ EXPECT_EQ(events[0].kind, SinkEvent::Kind::Wake);+ EXPECT_EQ(events[0].value, ticket.value());++ auto result = channel->take_all();+ EXPECT_EQ(flatten(result), "early"sv);+ EXPECT(result.state.has<Closed>());+}+TEST_CASE(armed_waiter_fires_on_write_and_arms_late){ChannelHarness harness;@@ -615,7 +647,8 @@ TEST_CASE(concurrent_writer_never_loses_a_wakeup){IGNORE_USE_IN_ESCAPING_LAMBDA auto consumer_sink = adopt_ref(*new LatestTicketSink);IGNORE_USE_IN_ESCAPING_LAMBDA auto producer_sink = adopt_ref(*new CountingProducerSink);- IGNORE_USE_IN_ESCAPING_LAMBDA auto channel = FetchByteChannel::create({ .low = 16, .high = 64 }, *consumer_sink, *producer_sink);+ IGNORE_USE_IN_ESCAPING_LAMBDA auto channel = FetchByteChannel::create({ .low = 16, .high = 64 }, *producer_sink);+ channel->attach_consumer(*consumer_sink);IGNORE_USE_IN_ESCAPING_LAMBDA Atomic<bool> timed_out { false };producer_sink->credit.store(channel->initial_credit(), AK::MemoryOrder::memory_order_release);@@ -666,7 +699,7 @@ TEST_CASE(concurrent_writer_wakes_each_of_several_waiters_once){IGNORE_USE_IN_ESCAPING_LAMBDA auto consumer_sink = adopt_ref(*new EveryTicketSink);IGNORE_USE_IN_ESCAPING_LAMBDA auto producer_sink = adopt_ref(*new CountingProducerSink);- IGNORE_USE_IN_ESCAPING_LAMBDA auto channel = FetchByteChannel::create({ .low = 16, .high = 64 }, *consumer_sink, *producer_sink);+ IGNORE_USE_IN_ESCAPING_LAMBDA auto channel = FetchByteChannel::create({ .low = 16, .high = 64 }, *producer_sink);IGNORE_USE_IN_ESCAPING_LAMBDA Atomic<bool> timed_out { false };producer_sink->credit.store(channel->initial_credit(), AK::MemoryOrder::memory_order_release);@@ -688,6 +721,11 @@ TEST_CASE(concurrent_writer_wakes_each_of_several_waiters_once)});writer->start();+ // The consumer attaches while the writer runs, once a peek shows its first byte has arrived.+ auto writing = spin_until_or_timeout([&] { return !channel->peek_up_to(1).taken.is_empty() || timed_out.load(); });+ EXPECT(writing);+ channel->attach_consumer(*consumer_sink);+// Each round arms several waiters at once and disarms one of them while the writer decides which are due.auto end_ticket = channel->arm_waiter(FetchByteChannel::EndOnly {});Vector<WakeTicket> owed;@@ -734,7 +772,8 @@ TEST_CASE(concurrent_cancellation_rejects_every_later_write){IGNORE_USE_IN_ESCAPING_LAMBDA auto consumer_sink = adopt_ref(*new LatestTicketSink);IGNORE_USE_IN_ESCAPING_LAMBDA auto producer_sink = adopt_ref(*new CountingProducerSink);- IGNORE_USE_IN_ESCAPING_LAMBDA auto channel = FetchByteChannel::create({ .low = 16, .high = 64 }, *consumer_sink, *producer_sink);+ IGNORE_USE_IN_ESCAPING_LAMBDA auto channel = FetchByteChannel::create({ .low = 16, .high = 64 }, *producer_sink);+ channel->attach_consumer(*consumer_sink);IGNORE_USE_IN_ESCAPING_LAMBDA Atomic<bool> timed_out { false };IGNORE_USE_IN_ESCAPING_LAMBDA Atomic<u64> accepted_after_rejection { 0 };producer_sink->credit.store(channel->initial_credit(), AK::MemoryOrder::memory_order_release);@@ -790,7 +829,8 @@ TEST_CASE(concurrent_transfer_moves_every_accepted_byte_once){IGNORE_USE_IN_ESCAPING_LAMBDA auto consumer_sink = adopt_ref(*new LatestTicketSink);IGNORE_USE_IN_ESCAPING_LAMBDA auto producer_sink = adopt_ref(*new CountingProducerSink);- IGNORE_USE_IN_ESCAPING_LAMBDA auto channel = FetchByteChannel::create({ .low = 16, .high = 64 }, *consumer_sink, *producer_sink);+ IGNORE_USE_IN_ESCAPING_LAMBDA auto channel = FetchByteChannel::create({ .low = 16, .high = 64 }, *producer_sink);+ channel->attach_consumer(*consumer_sink);IGNORE_USE_IN_ESCAPING_LAMBDA Atomic<bool> timed_out { false };IGNORE_USE_IN_ESCAPING_LAMBDA Atomic<bool> transferred { false };IGNORE_USE_IN_ESCAPING_LAMBDA Atomic<u64> accepted { 0 };diff --git a/Tests/LibWeb/TestFetchEngineMessages.cpp b/Tests/LibWeb/TestFetchEngineMessages.cppindex 9c466121890..c08812dd9e0 100644--- a/Tests/LibWeb/TestFetchEngineMessages.cpp+++ b/Tests/LibWeb/TestFetchEngineMessages.cpp@@ -98,10 +98,6 @@ TEST_CASE(delivered_body_requires_a_valid_handle)namespace {-class IgnoringConsumerSink final : public FetchByteChannelConsumerSink {- virtual void wake(WakeTicket) override { }-};-class IgnoringProducerSink final : public FetchByteChannelProducerSink {virtual void grant_credit(u64) override { }virtual void consumer_cancelled() override { }@@ -111,7 +107,7 @@ class IgnoringProducerSink final : public FetchByteChannelProducerSink {TEST_CASE(delivered_body_carries_a_channel_still_being_written){- auto channel = FetchByteChannel::create({ .low = 1, .high = 2 }, adopt_ref(*new IgnoringConsumerSink), adopt_ref(*new IgnoringProducerSink));+ auto channel = FetchByteChannel::create({ .low = 1, .high = 2 }, adopt_ref(*new IgnoringProducerSink));LocalBodyPresentation body = Delivered<NonNullLocalBodyHandle> { channel };auto const& delivered = body.get<Delivered<NonNullLocalBodyHandle>>();