ladybird/Userland/Libraries/LibWeb/HTML/SharedImageRequest.h
Shannon Booth e800605ad3 AK+LibURL: Move AK::URL into a new URL library
This URL library ends up being a relatively fundamental base library of
the system, as LibCore depends on LibURL.

This change has two main benefits:
 * Moving AK back more towards being an agnostic library that can
   be used between the kernel and userspace. URL has never really fit
   that description - and is not used in the kernel.
 * URL _should_ depend on LibUnicode, as it needs punnycode support.
   However, it's not really possible to do this inside of AK as it can't
   depend on any external library. This change brings us a little closer
   to being able to do that, but unfortunately we aren't there quite
   yet, as the code generators depend on LibCore.
2024-03-18 14:06:28 -04:00

77 lines
2.0 KiB
C++

/*
* Copyright (c) 2023, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Error.h>
#include <AK/OwnPtr.h>
#include <LibGfx/Size.h>
#include <LibJS/Heap/Handle.h>
#include <LibJS/Heap/HeapFunction.h>
#include <LibJS/SafeFunction.h>
#include <LibURL/URL.h>
#include <LibWeb/Forward.h>
namespace Web::HTML {
class SharedImageRequest final : public JS::Cell {
JS_CELL(ImageRequest, JS::Cell);
JS_DECLARE_ALLOCATOR(SharedImageRequest);
public:
[[nodiscard]] static JS::NonnullGCPtr<SharedImageRequest> get_or_create(JS::Realm&, JS::NonnullGCPtr<Page>, URL::URL const&);
virtual ~SharedImageRequest() override;
URL::URL const& url() const { return m_url; }
[[nodiscard]] JS::GCPtr<DecodedImageData> image_data() const;
[[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(Function<void()> on_finish, Function<void()> on_fail);
bool is_fetching() const;
bool needs_fetching() const;
private:
explicit SharedImageRequest(JS::NonnullGCPtr<Page>, URL::URL, JS::NonnullGCPtr<DOM::Document>);
virtual void finalize() override;
virtual void visit_edges(JS::Cell::Visitor&) override;
void handle_successful_fetch(URL::URL const&, StringView mime_type, ByteBuffer data);
void handle_failed_fetch();
enum class State {
New,
Fetching,
Finished,
Failed,
};
State m_state { State::New };
JS::NonnullGCPtr<Page> m_page;
struct Callbacks {
JS::GCPtr<JS::HeapFunction<void()>> on_finish;
JS::GCPtr<JS::HeapFunction<void()>> on_fail;
};
Vector<Callbacks> m_callbacks;
URL::URL m_url;
JS::GCPtr<DecodedImageData> m_image_data;
JS::GCPtr<Fetch::Infrastructure::FetchController> m_fetch_controller;
JS::GCPtr<DOM::Document> m_document;
};
}