/* * Copyright (c) 2023, Andreas Kling * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once #include #include #include #include #include #include #include namespace Web::HTML { class SharedImageRequest : public RefCounted { public: static ErrorOr> get_or_create(Page&, AK::URL const&); ~SharedImageRequest(); AK::URL const& url() const { return m_url; } [[nodiscard]] RefPtr image_data() const; [[nodiscard]] JS::GCPtr fetch_controller(); void set_fetch_controller(JS::GCPtr); void fetch_image(JS::Realm&, JS::NonnullGCPtr); void add_callbacks(JS::SafeFunction on_finish, JS::SafeFunction on_fail); bool is_fetching() const; bool needs_fetching() const; private: explicit SharedImageRequest(Page&, AK::URL); void handle_successful_fetch(AK::URL const&, StringView mime_type, ByteBuffer data); void handle_failed_fetch(); enum class State { New, Fetching, Finished, Failed, }; State m_state { State::New }; Page& m_page; struct Callbacks { JS::SafeFunction on_finish; JS::SafeFunction on_fail; }; Vector m_callbacks; AK::URL m_url; RefPtr m_image_data; JS::Handle m_fetch_controller; }; }