1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 07:58:11 +00:00

LibWeb: Start building a new Resource class to share more resources

A Resource represents a resource that we're loading, have loaded or
will soon load. Basically, it's a downloadable resource that can be
shared by multiple clients.

A typical usecase is multiple <img> elements with the same src.
In a future patch, we will try to make sure that those <img> elements
get the same Resource if possible. This will reduce network usage,
memory usage, and CPU usage. :^)

For now, this first patch simply introduces the mechanism.

You get a Resource by calling ResourceLoader::load_resource().
To get notified about changes to a Resource's load status, you inherit
from ResourceClient and implement the callbacks you're interested in.

This patch turns HTMLImageElement into a ResourceClient.
This commit is contained in:
Andreas Kling 2020-06-01 21:33:23 +02:00
parent 7be9cf8d36
commit 5ed66cb8d9
8 changed files with 274 additions and 23 deletions

View file

@ -31,6 +31,7 @@
#include <LibCore/File.h>
#include <LibProtocol/Client.h>
#include <LibProtocol/Download.h>
#include <LibWeb/Loader/Resource.h>
#include <LibWeb/Loader/ResourceLoader.h>
namespace Web {
@ -68,6 +69,25 @@ void ResourceLoader::load_sync(const URL& url, Function<void(const ByteBuffer&,
loop.exec();
}
RefPtr<Resource> ResourceLoader::load_resource(const URL& url)
{
if (!url.is_valid())
return nullptr;
auto resource = Resource::create({}, url);
load(
url,
[=](auto& data, auto& headers) {
const_cast<Resource&>(*resource).did_load({}, data, headers);
},
[=](auto& error) {
const_cast<Resource&>(*resource).did_fail({}, error);
});
return resource;
}
void ResourceLoader::load(const URL& url, Function<void(const ByteBuffer&, const HashMap<String, String, CaseInsensitiveStringTraits>& response_headers)> success_callback, Function<void(const String&)> error_callback)
{
if (is_port_blocked(url.port())) {