mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-12-07 21:59:54 +00:00
LibWeb: Remove now-unused Resource and ResourceClient
And deal with the fallout of transitive includes.
This commit is contained in:
parent
6057719f63
commit
ac246caa0c
Notes:
github-actions[bot]
2025-11-05 17:28:39 +00:00
Author: https://github.com/trflynn89
Commit: ac246caa0c
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/6708
11 changed files with 12 additions and 372 deletions
|
|
@ -758,7 +758,6 @@ set(SOURCES
|
|||
Loader/GeneratedPagesLoader.cpp
|
||||
Loader/LoadRequest.cpp
|
||||
Loader/ProxyMappings.cpp
|
||||
Loader/Resource.cpp
|
||||
Loader/ResourceLoader.cpp
|
||||
MathML/AttributeNames.cpp
|
||||
MathML/MathMLElement.cpp
|
||||
|
|
|
|||
|
|
@ -92,6 +92,7 @@
|
|||
#include <LibWeb/MimeSniff/MimeType.h>
|
||||
#include <LibWeb/MimeSniff/Resource.h>
|
||||
#include <LibWeb/Namespace.h>
|
||||
#include <LibWeb/Page/Page.h>
|
||||
#include <LibWeb/Painting/PaintableBox.h>
|
||||
#include <LibWeb/Platform/FontPlugin.h>
|
||||
#include <math.h>
|
||||
|
|
@ -3097,7 +3098,7 @@ void StyleComputer::compute_custom_properties(ComputedProperties&, DOM::Abstract
|
|||
static CSSPixels line_width_keyword_to_css_pixels(Keyword keyword)
|
||||
{
|
||||
// https://drafts.csswg.org/css-backgrounds/#typedef-line-width
|
||||
// The thin, medium, and thick keywords are equivalent to 1px, 3px, and 5px, respectively.
|
||||
// The thin, medium, and thick keywords are equivalent to 1px, 3px, and 5px, respectively.
|
||||
switch (keyword) {
|
||||
case Keyword::Thin:
|
||||
return CSSPixels { 1 };
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@
|
|||
#include <LibCore/Directory.h>
|
||||
#include <LibCore/Resource.h>
|
||||
#include <LibCore/System.h>
|
||||
#include <LibURL/URL.h>
|
||||
#include <LibWeb/Loader/GeneratedPagesLoader.h>
|
||||
#include <LibWeb/Loader/UserAgent.h>
|
||||
|
||||
|
|
|
|||
|
|
@ -6,9 +6,8 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <LibWeb/Export.h>
|
||||
#include <LibWeb/Fetch/Infrastructure/HTTP/Responses.h>
|
||||
#include <LibWeb/Loader/Resource.h>
|
||||
#include <LibURL/Forward.h>
|
||||
#include <LibWeb/Forward.h>
|
||||
|
||||
namespace Web {
|
||||
|
||||
|
|
|
|||
|
|
@ -1,191 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2020, Andreas Kling <andreas@ladybird.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <AK/Debug.h>
|
||||
#include <AK/Function.h>
|
||||
#include <LibCore/MimeData.h>
|
||||
#include <LibTextCodec/Decoder.h>
|
||||
#include <LibWeb/HTML/HTMLImageElement.h>
|
||||
#include <LibWeb/Loader/Resource.h>
|
||||
#include <LibWeb/Loader/ResourceLoader.h>
|
||||
#include <LibWeb/Platform/EventLoopPlugin.h>
|
||||
|
||||
namespace Web {
|
||||
|
||||
NonnullRefPtr<Resource> Resource::create(Badge<ResourceLoader>, Type type, LoadRequest const& request)
|
||||
{
|
||||
return adopt_ref(*new Resource(type, request));
|
||||
}
|
||||
|
||||
Resource::Resource(Type type, LoadRequest const& request)
|
||||
: m_request(request)
|
||||
, m_type(type)
|
||||
{
|
||||
}
|
||||
|
||||
Resource::Resource(Type type, Resource& resource)
|
||||
: m_request(resource.m_request)
|
||||
, m_encoded_data(move(resource.m_encoded_data))
|
||||
, m_type(type)
|
||||
, m_state(resource.m_state)
|
||||
, m_error(move(resource.m_error))
|
||||
, m_encoding(move(resource.m_encoding))
|
||||
, m_mime_type(move(resource.m_mime_type))
|
||||
, m_response_headers(move(resource.m_response_headers))
|
||||
, m_status_code(move(resource.m_status_code))
|
||||
{
|
||||
ResourceLoader::the().evict_from_cache(m_request);
|
||||
}
|
||||
|
||||
Resource::~Resource() = default;
|
||||
|
||||
void Resource::for_each_client(Function<void(ResourceClient&)> callback)
|
||||
{
|
||||
Vector<WeakPtr<ResourceClient>, 16> clients_copy;
|
||||
clients_copy.ensure_capacity(m_clients.size());
|
||||
for (auto* client : m_clients)
|
||||
clients_copy.append(client->make_weak_ptr());
|
||||
for (auto client : clients_copy) {
|
||||
if (client)
|
||||
callback(*client);
|
||||
}
|
||||
}
|
||||
|
||||
static Optional<ByteString> encoding_from_content_type(ByteString const& content_type)
|
||||
{
|
||||
auto offset = content_type.find("charset="sv);
|
||||
if (offset.has_value()) {
|
||||
auto encoding = content_type.substring(offset.value() + 8, content_type.length() - offset.value() - 8).to_lowercase();
|
||||
if (encoding.length() >= 2 && encoding.starts_with('"') && encoding.ends_with('"'))
|
||||
return encoding.substring(1, encoding.length() - 2);
|
||||
if (encoding.length() >= 2 && encoding.starts_with('\'') && encoding.ends_with('\''))
|
||||
return encoding.substring(1, encoding.length() - 2);
|
||||
return encoding;
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
static ByteString mime_type_from_content_type(ByteString const& content_type)
|
||||
{
|
||||
auto offset = content_type.find(';');
|
||||
if (offset.has_value())
|
||||
return content_type.substring(0, offset.value()).to_lowercase();
|
||||
|
||||
return content_type;
|
||||
}
|
||||
|
||||
static bool is_valid_encoding(StringView encoding)
|
||||
{
|
||||
return TextCodec::decoder_for(encoding).has_value();
|
||||
}
|
||||
|
||||
void Resource::did_load(Badge<ResourceLoader>, ReadonlyBytes data, HTTP::HeaderMap const& headers, Optional<u32> status_code)
|
||||
{
|
||||
VERIFY(m_state == State::Pending);
|
||||
// FIXME: Handle OOM failure.
|
||||
m_encoded_data = ByteBuffer::copy(data).release_value_but_fixme_should_propagate_errors();
|
||||
m_response_headers = headers;
|
||||
m_status_code = move(status_code);
|
||||
m_state = State::Loaded;
|
||||
|
||||
auto content_type = headers.get("Content-Type");
|
||||
|
||||
if (content_type.has_value()) {
|
||||
dbgln_if(RESOURCE_DEBUG, "Content-Type header: '{}'", content_type.value());
|
||||
m_mime_type = mime_type_from_content_type(content_type.value());
|
||||
} else {
|
||||
auto content_type_options = headers.get("X-Content-Type-Options");
|
||||
if (content_type_options.value_or("").equals_ignoring_ascii_case("nosniff"sv)) {
|
||||
m_mime_type = "text/plain";
|
||||
} else {
|
||||
m_mime_type = Core::guess_mime_type_based_on_filename(url()->file_path());
|
||||
}
|
||||
}
|
||||
|
||||
m_encoding = {};
|
||||
if (content_type.has_value()) {
|
||||
auto encoding = encoding_from_content_type(content_type.value());
|
||||
if (encoding.has_value() && is_valid_encoding(encoding.value())) {
|
||||
dbgln_if(RESOURCE_DEBUG, "Set encoding '{}' from Content-Type", encoding.value());
|
||||
m_encoding = encoding.value();
|
||||
}
|
||||
}
|
||||
|
||||
for_each_client([](auto& client) {
|
||||
client.resource_did_load();
|
||||
});
|
||||
}
|
||||
|
||||
void Resource::did_fail(Badge<ResourceLoader>, ByteString const& error, ReadonlyBytes data, HTTP::HeaderMap const& headers, Optional<u32> status_code)
|
||||
{
|
||||
m_error = error;
|
||||
m_encoded_data = ByteBuffer::copy(data).release_value_but_fixme_should_propagate_errors();
|
||||
m_response_headers = headers;
|
||||
m_status_code = move(status_code);
|
||||
m_state = State::Failed;
|
||||
|
||||
for_each_client([](auto& client) {
|
||||
client.resource_did_fail();
|
||||
});
|
||||
}
|
||||
|
||||
void Resource::register_client(Badge<ResourceClient>, ResourceClient& client)
|
||||
{
|
||||
VERIFY(!m_clients.contains(&client));
|
||||
m_clients.set(&client);
|
||||
}
|
||||
|
||||
void Resource::unregister_client(Badge<ResourceClient>, ResourceClient& client)
|
||||
{
|
||||
VERIFY(m_clients.contains(&client));
|
||||
m_clients.remove(&client);
|
||||
}
|
||||
|
||||
void ResourceClient::set_resource(Resource* resource)
|
||||
{
|
||||
if (m_resource)
|
||||
m_resource->unregister_client({}, *this);
|
||||
m_resource = resource;
|
||||
if (m_resource) {
|
||||
VERIFY(resource->type() == client_type());
|
||||
|
||||
m_resource->register_client({}, *this);
|
||||
|
||||
// For resources that are already loaded, we fire their load/fail callbacks via the event loop.
|
||||
// This ensures that these callbacks always happen in a consistent way, instead of being invoked
|
||||
// synchronously in some cases, and asynchronously in others.
|
||||
if (resource->is_loaded() || resource->is_failed()) {
|
||||
Platform::EventLoopPlugin::the().deferred_invoke(GC::create_function(ResourceLoader::the().heap(), [weak_this = make_weak_ptr(), strong_resource = NonnullRefPtr { *m_resource }] {
|
||||
if (!weak_this)
|
||||
return;
|
||||
|
||||
if (weak_this->m_resource != strong_resource.ptr())
|
||||
return;
|
||||
|
||||
// Make sure that reused resources also have their load callback fired.
|
||||
if (weak_this->m_resource->is_loaded()) {
|
||||
weak_this->resource_did_load();
|
||||
return;
|
||||
}
|
||||
|
||||
// Make sure that reused resources also have their fail callback fired.
|
||||
if (weak_this->m_resource->is_failed()) {
|
||||
weak_this->resource_did_fail();
|
||||
return;
|
||||
}
|
||||
}));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ResourceClient::~ResourceClient()
|
||||
{
|
||||
if (m_resource)
|
||||
m_resource->unregister_client({}, *this);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,110 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2020, Andreas Kling <andreas@ladybird.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/ByteBuffer.h>
|
||||
#include <AK/HashTable.h>
|
||||
#include <AK/Noncopyable.h>
|
||||
#include <AK/RefCounted.h>
|
||||
#include <AK/WeakPtr.h>
|
||||
#include <AK/Weakable.h>
|
||||
#include <LibGfx/Forward.h>
|
||||
#include <LibHTTP/HeaderMap.h>
|
||||
#include <LibURL/URL.h>
|
||||
#include <LibWeb/Forward.h>
|
||||
#include <LibWeb/Loader/LoadRequest.h>
|
||||
|
||||
namespace Web {
|
||||
|
||||
class ResourceClient;
|
||||
|
||||
class Resource : public RefCounted<Resource> {
|
||||
AK_MAKE_NONCOPYABLE(Resource);
|
||||
AK_MAKE_NONMOVABLE(Resource);
|
||||
|
||||
public:
|
||||
enum class Type {
|
||||
Generic,
|
||||
};
|
||||
|
||||
static NonnullRefPtr<Resource> create(Badge<ResourceLoader>, Type, LoadRequest const&);
|
||||
virtual ~Resource();
|
||||
|
||||
Type type() const { return m_type; }
|
||||
|
||||
enum class State {
|
||||
Pending,
|
||||
Loaded,
|
||||
Failed,
|
||||
};
|
||||
|
||||
bool is_pending() const { return m_state == State::Pending; }
|
||||
bool is_loaded() const { return m_state == State::Loaded; }
|
||||
bool is_failed() const { return m_state == State::Failed; }
|
||||
|
||||
ByteString const& error() const { return m_error; }
|
||||
|
||||
bool has_encoded_data() const { return !m_encoded_data.is_empty(); }
|
||||
|
||||
Optional<URL::URL> const& url() const { return m_request.url(); }
|
||||
ByteBuffer const& encoded_data() const { return m_encoded_data; }
|
||||
|
||||
[[nodiscard]] HTTP::HeaderMap const& response_headers() const { return m_response_headers; }
|
||||
|
||||
[[nodiscard]] Optional<u32> status_code() const { return m_status_code; }
|
||||
|
||||
void register_client(Badge<ResourceClient>, ResourceClient&);
|
||||
void unregister_client(Badge<ResourceClient>, ResourceClient&);
|
||||
|
||||
bool has_encoding() const { return m_encoding.has_value(); }
|
||||
Optional<ByteString> const& encoding() const { return m_encoding; }
|
||||
ByteString const& mime_type() const { return m_mime_type; }
|
||||
|
||||
void for_each_client(Function<void(ResourceClient&)>);
|
||||
|
||||
void did_load(Badge<ResourceLoader>, ReadonlyBytes data, HTTP::HeaderMap const&, Optional<u32> status_code);
|
||||
void did_fail(Badge<ResourceLoader>, ByteString const& error, ReadonlyBytes data, HTTP::HeaderMap const&, Optional<u32> status_code);
|
||||
|
||||
protected:
|
||||
explicit Resource(Type, LoadRequest const&);
|
||||
Resource(Type, Resource&);
|
||||
|
||||
LoadRequest request() const { return m_request; }
|
||||
|
||||
private:
|
||||
LoadRequest m_request;
|
||||
ByteBuffer m_encoded_data;
|
||||
Type m_type { Type::Generic };
|
||||
State m_state { State::Pending };
|
||||
ByteString m_error;
|
||||
Optional<ByteString> m_encoding;
|
||||
|
||||
ByteString m_mime_type;
|
||||
HTTP::HeaderMap m_response_headers;
|
||||
Optional<u32> m_status_code;
|
||||
HashTable<ResourceClient*> m_clients;
|
||||
};
|
||||
|
||||
class ResourceClient : public Weakable<ResourceClient> {
|
||||
public:
|
||||
virtual ~ResourceClient();
|
||||
|
||||
virtual void resource_did_load() { }
|
||||
virtual void resource_did_fail() { }
|
||||
|
||||
protected:
|
||||
virtual Resource::Type client_type() const { return Resource::Type::Generic; }
|
||||
|
||||
Resource* resource() { return m_resource; }
|
||||
Resource const* resource() const { return m_resource; }
|
||||
void set_resource(Resource*);
|
||||
|
||||
private:
|
||||
RefPtr<Resource> m_resource;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
@ -21,7 +21,6 @@
|
|||
#include <LibWeb/Loader/GeneratedPagesLoader.h>
|
||||
#include <LibWeb/Loader/LoadRequest.h>
|
||||
#include <LibWeb/Loader/ProxyMappings.h>
|
||||
#include <LibWeb/Loader/Resource.h>
|
||||
#include <LibWeb/Loader/ResourceLoader.h>
|
||||
#include <LibWeb/Page/Page.h>
|
||||
#include <LibWeb/Platform/EventLoopPlugin.h>
|
||||
|
|
@ -96,44 +95,6 @@ void ResourceLoader::preconnect(URL::URL const& url)
|
|||
m_request_client->ensure_connection(url, RequestServer::CacheLevel::CreateConnection);
|
||||
}
|
||||
|
||||
static HashMap<LoadRequest, NonnullRefPtr<Resource>> s_resource_cache;
|
||||
|
||||
RefPtr<Resource> ResourceLoader::load_resource(Resource::Type type, LoadRequest& request)
|
||||
{
|
||||
if (!request.is_valid())
|
||||
return nullptr;
|
||||
|
||||
bool use_cache = request.url()->scheme() != "file";
|
||||
|
||||
if (use_cache) {
|
||||
auto it = s_resource_cache.find(request);
|
||||
if (it != s_resource_cache.end()) {
|
||||
if (it->value->type() != type) {
|
||||
dbgln("FIXME: Not using cached resource for {} since there's a type mismatch.", request.url());
|
||||
} else {
|
||||
dbgln_if(CACHE_DEBUG, "Reusing cached resource for: {}", request.url());
|
||||
return it->value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
auto resource = Resource::create({}, type, request);
|
||||
|
||||
if (use_cache)
|
||||
s_resource_cache.set(request, resource);
|
||||
|
||||
load(
|
||||
request,
|
||||
GC::create_function(m_heap, [resource](ReadonlyBytes data, Requests::RequestTimingInfo const&, HTTP::HeaderMap const& headers, Optional<u32> status_code, Optional<String> const&) {
|
||||
resource->did_load({}, data, headers, status_code);
|
||||
}),
|
||||
GC::create_function(m_heap, [resource](ByteString const& error, Requests::RequestTimingInfo const&, Optional<u32> status_code, Optional<String> const&, ReadonlyBytes data, HTTP::HeaderMap const& headers) {
|
||||
resource->did_fail({}, error, data, headers, status_code);
|
||||
}));
|
||||
|
||||
return resource;
|
||||
}
|
||||
|
||||
static ByteString sanitized_url_for_logging(URL::URL const& url)
|
||||
{
|
||||
if (url.scheme() == "data"sv)
|
||||
|
|
@ -585,11 +546,6 @@ void ResourceLoader::handle_network_response_headers(LoadRequest const& request,
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (auto cache_control = response_headers.get("Cache-Control"); cache_control.has_value()) {
|
||||
if (cache_control.value().contains("no-store"sv))
|
||||
s_resource_cache.remove(request);
|
||||
}
|
||||
}
|
||||
|
||||
void ResourceLoader::finish_network_request(NonnullRefPtr<Requests::Request> protocol_request)
|
||||
|
|
@ -604,16 +560,4 @@ void ResourceLoader::finish_network_request(NonnullRefPtr<Requests::Request> pro
|
|||
});
|
||||
}
|
||||
|
||||
void ResourceLoader::clear_cache()
|
||||
{
|
||||
dbgln_if(CACHE_DEBUG, "Clearing {} items from ResourceLoader cache", s_resource_cache.size());
|
||||
s_resource_cache.clear();
|
||||
}
|
||||
|
||||
void ResourceLoader::evict_from_cache(LoadRequest const& request)
|
||||
{
|
||||
dbgln_if(CACHE_DEBUG, "Removing resource {} from cache", request.url());
|
||||
s_resource_cache.remove(request);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,24 +11,24 @@
|
|||
#include <AK/Function.h>
|
||||
#include <AK/HashTable.h>
|
||||
#include <LibCore/EventReceiver.h>
|
||||
#include <LibGC/Function.h>
|
||||
#include <LibHTTP/HeaderMap.h>
|
||||
#include <LibRequests/Forward.h>
|
||||
#include <LibURL/URL.h>
|
||||
#include <LibWeb/Export.h>
|
||||
#include <LibWeb/Loader/Resource.h>
|
||||
#include <LibWeb/Forward.h>
|
||||
#include <LibWeb/Loader/UserAgent.h>
|
||||
|
||||
namespace Web {
|
||||
|
||||
class WEB_API ResourceLoader : public Core::EventReceiver {
|
||||
C_OBJECT_ABSTRACT(ResourceLoader)
|
||||
|
||||
public:
|
||||
static void initialize(GC::Heap&, NonnullRefPtr<Requests::RequestClient>);
|
||||
static ResourceLoader& the();
|
||||
|
||||
void set_client(NonnullRefPtr<Requests::RequestClient>);
|
||||
|
||||
RefPtr<Resource> load_resource(Resource::Type, LoadRequest&);
|
||||
|
||||
using SuccessCallback = GC::Function<void(ReadonlyBytes, Requests::RequestTimingInfo const&, HTTP::HeaderMap const& response_headers, Optional<u32> status_code, Optional<String> const& reason_phrase)>;
|
||||
using ErrorCallback = GC::Function<void(ByteString const&, Requests::RequestTimingInfo const&, Optional<u32> status_code, Optional<String> const& reason_phrase, ReadonlyBytes payload, HTTP::HeaderMap const& response_headers)>;
|
||||
using TimeoutCallback = GC::Function<void()>;
|
||||
|
|
@ -69,11 +69,6 @@ public:
|
|||
bool enable_global_privacy_control() const { return m_enable_global_privacy_control; }
|
||||
void set_enable_global_privacy_control(bool enable) { m_enable_global_privacy_control = enable; }
|
||||
|
||||
void clear_cache();
|
||||
void evict_from_cache(LoadRequest const&);
|
||||
|
||||
GC::Heap& heap() { return m_heap; }
|
||||
|
||||
private:
|
||||
explicit ResourceLoader(GC::Heap&, NonnullRefPtr<Requests::RequestClient>);
|
||||
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@
|
|||
*/
|
||||
|
||||
#include <LibWeb/CookieStore/CookieStore.h>
|
||||
#include <LibWeb/Page/Page.h>
|
||||
#include <LibWeb/ServiceWorker/EventNames.h>
|
||||
#include <LibWeb/ServiceWorker/ServiceWorkerGlobalScope.h>
|
||||
|
||||
|
|
|
|||
|
|
@ -24,6 +24,7 @@
|
|||
#include <LibWeb/HTML/MessageEvent.h>
|
||||
#include <LibWeb/HTML/WindowOrWorkerGlobalScope.h>
|
||||
#include <LibWeb/Loader/ResourceLoader.h>
|
||||
#include <LibWeb/Page/Page.h>
|
||||
#include <LibWeb/Platform/EventLoopPlugin.h>
|
||||
#include <LibWeb/WebIDL/AbstractOperations.h>
|
||||
#include <LibWeb/WebIDL/Buffers.h>
|
||||
|
|
|
|||
|
|
@ -386,7 +386,7 @@ void ConnectionFromClient::debug_request(u64 page_id, ByteString request, ByteSt
|
|||
}
|
||||
|
||||
if (request == "clear-cache") {
|
||||
Web::ResourceLoader::the().clear_cache();
|
||||
// FIXME: Clear the Fetch cache.
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue