1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 12:48:10 +00:00

LibWeb: Move image fetching & decoding into ImageRequest

This forces us to diverge from the spec, but it's for a good cause:
by moving it into ImageRequest, we'll be able to reuse fetching and
decoding logic from CSS and other places.

This patch also makes ImageRequests shareable, currently keyed by
the URL (this part needs improvement!)
This commit is contained in:
Andreas Kling 2023-06-11 14:46:54 +02:00
parent e27081a8ca
commit f70d3faa0f
4 changed files with 245 additions and 152 deletions

View file

@ -18,7 +18,8 @@ namespace Web::HTML {
// https://html.spec.whatwg.org/multipage/images.html#image-request
class ImageRequest : public RefCounted<ImageRequest> {
public:
static ErrorOr<NonnullRefPtr<ImageRequest>> create();
static ErrorOr<NonnullRefPtr<ImageRequest>> create(Page&);
static ErrorOr<NonnullRefPtr<ImageRequest>> get_shareable_or_create(Page&, AK::URL const&);
~ImageRequest();
// https://html.spec.whatwg.org/multipage/images.html#img-req-state
@ -52,8 +53,23 @@ public:
[[nodiscard]] JS::GCPtr<Fetch::Infrastructure::FetchController> fetch_controller();
void set_fetch_controller(JS::GCPtr<Fetch::Infrastructure::FetchController>);
void fetch_image(JS::Realm&, JS::NonnullGCPtr<Fetch::Infrastructure::Request>);
void add_callbacks(JS::SafeFunction<void()> on_finish, JS::SafeFunction<void()> on_fail);
private:
ImageRequest();
explicit ImageRequest(Page&);
void handle_successful_fetch(AK::URL const&, StringView mime_type, ByteBuffer data);
void handle_failed_fetch();
Page& m_page;
struct Callbacks {
JS::SafeFunction<void()> on_finish;
JS::SafeFunction<void()> on_fail;
};
Vector<Callbacks> m_callbacks;
// https://html.spec.whatwg.org/multipage/images.html#img-req-state
// An image request's state is initially unavailable.