Truly independent web browser
/*
* Copyright (c) 2026-present, the Ladybird developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/AtomicRefCounted.h>
#include <AK/Badge.h>
#include <AK/NonnullRefPtr.h>
#include <AK/Optional.h>
#include <AK/RefPtr.h>
#include <LibGC/Forward.h>
#include <LibWeb/Fetch/Engine/BodyIntent.h>
#include <LibWeb/Fetch/Engine/FetchTimingSeed.h>
#include <LibWeb/Fetch/Infrastructure/FetchTimingInfo.h>
namespace Web::Fetch::Infrastructure {
class FetchController;
// The state of a fetch that its controller and its fetch params share.
class FetchControlBlock : public AtomicRefCounted<FetchControlBlock> {
public:
GC_HOLDS_NO_CELL_EDGES;
enum class State : u8 {
Ongoing,
Terminated,
Aborted,
Stopped,
};
[[nodiscard]] static NonnullRefPtr<FetchControlBlock> create() { return adopt_ref(*new FetchControlBlock); }
[[nodiscard]] State state() const { return m_state; }
void set_state(Badge<FetchController>, State state) { m_state = state; }
// https://fetch.spec.whatwg.org/#fetch-params-aborted
[[nodiscard]] bool is_aborted() const
{
// A fetch params fetchParams is aborted if its controller’s state is "aborted".
return m_state == State::Aborted;
}
// https://fetch.spec.whatwg.org/#fetch-params-canceled
[[nodiscard]] bool is_canceled() const
{
// A fetch params fetchParams is canceled if its controller’s state is "aborted" or "terminated".
return m_state == State::Aborted || m_state == State::Terminated;
}
[[nodiscard]] bool is_ongoing() const { return m_state == State::Ongoing; }
[[nodiscard]] bool is_stopped() const { return m_state == State::Stopped; }
[[nodiscard]] RefPtr<FetchTimingInfo> full_timing_info() const { return m_full_timing_info; }
void set_full_timing_info(NonnullRefPtr<FetchTimingInfo> full_timing_info) { m_full_timing_info = move(full_timing_info); }
// The agent's timing state, which only the thread of the agent that started the fetch reads and writes.
// The timing info the agent has of its latest response handover.
[[nodiscard]] RefPtr<FetchTimingInfo> agent_timing_info() const { return m_agent_timing_info; }
// Makes the agent's timing info from the fields a response handover gives it, the network phases settled last and
// the fetch's end time.
NonnullRefPtr<FetchTimingInfo> hand_over_timing(Engine::FetchTimingSeed const& seed)
{
auto timing_info = FetchTimingInfo::create(seed);
if (m_network_phases.has_value())
timing_info->set_network_phases(*m_network_phases);
timing_info->set_end_time(m_end_time);
m_agent_timing_info = timing_info;
return timing_info;
}
// Gives the network phases a load of the fetch settled to the agent's timing info, and to that of later handovers.
void settle_network_phases(Engine::FetchNetworkPhases network_phases)
{
if (m_agent_timing_info)
m_agent_timing_info->set_network_phases(network_phases);
m_network_phases = move(network_phases);
}
// The end time last set for the fetch, or 0, which the agent's timing info has, as do those of later handovers.
[[nodiscard]] HighResolutionTime::DOMHighResTimeStamp end_time() const { return m_end_time; }
void set_end_time(HighResolutionTime::DOMHighResTimeStamp end_time)
{
if (m_agent_timing_info)
m_agent_timing_info->set_end_time(end_time);
m_end_time = end_time;
}
[[nodiscard]] Engine::BodyIntent body_intent() const { return m_body_intent; }
void set_body_intent(Badge<FetchController>, Engine::BodyIntent body_intent)
{
VERIFY(!m_response_arrived);
m_body_intent = body_intent;
}
// AD-HOC: Whether this fetch's response has arrived, from the network or at main fetch. From then on,
// consume_a_preloaded_resource() no longer changes the fetch's body intent or task destination.
void set_response_arrived() { m_response_arrived = true; }
[[nodiscard]] bool response_arrived() const { return m_response_arrived; }
[[nodiscard]] bool has_started_request() const { return m_has_started_request; }
void set_has_started_request() { m_has_started_request = true; }
[[nodiscard]] bool requires_network() const { return m_requires_network; }
void set_requires_network() { m_requires_network = true; }
private:
FetchControlBlock() = default;
// https://fetch.spec.whatwg.org/#fetch-controller-state
// state (default "ongoing")
// "ongoing", "terminated", or "aborted"
State m_state { State::Ongoing };
// https://fetch.spec.whatwg.org/#fetch-controller-full-timing-info
// full timing info (default null)
// Null or a fetch timing info.
RefPtr<FetchTimingInfo> m_full_timing_info;
// The agent's timing state, which only the thread of the agent that started the fetch reads and writes.
RefPtr<FetchTimingInfo> m_agent_timing_info;
Optional<Engine::FetchNetworkPhases> m_network_phases;
HighResolutionTime::DOMHighResTimeStamp m_end_time { 0 };
Engine::BodyIntent m_body_intent { Engine::BodyIntent::DrainAndDiscard };
bool m_response_arrived { false };
bool m_has_started_request { false };
bool m_requires_network { false };
};
}