Skip to content

Truly independent web browser

4.6 KB
Raw Blame
/*
 * Copyright (c) 2023, Andreas Kling <[email protected]>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/Error.h>
#include <AK/OwnPtr.h>
#include <AK/Utf16String.h>
#include <LibGC/Root.h>
#include <LibGfx/Size.h>
#include <LibURL/URL.h>
#include <LibWeb/Forward.h>
#include <LibWeb/HTML/CORSSettingAttribute.h>
#include <LibWeb/HTML/SharedResourceRequest.h>

namespace Web::HTML {

// https://html.spec.whatwg.org/multipage/images.html#image-request
class ImageRequest final : public JS::Cell {
    GC_CELL(ImageRequest, JS::Cell);
    GC_DECLARE_ALLOCATOR(ImageRequest);

public:
    [[nodiscard]] static GC::Ref<ImageRequest> create();

    ~ImageRequest();

    // https://html.spec.whatwg.org/multipage/images.html#img-req-state
    enum class State {
        Unavailable,
        PartiallyAvailable,
        CompletelyAvailable,
        Broken,
    };

    bool is_available() const;
    bool is_fetching() const;

    State state() const;
    void set_state(State);

    Utf16String const& current_url() const { return m_current_url; }
    void set_current_url(DOM::Document&, String, CORSSettingAttribute);
    void set_current_url(DOM::Document&, Utf16String, CORSSettingAttribute);
    void set_current_url(Utf16String, GC::Ref<SharedResourceRequest>);

    [[nodiscard]] GC::Ptr<DecodedImageData> image_data() const;
    void set_image_data(GC::Ptr<DecodedImageData>);

    [[nodiscard]] float current_pixel_density() const { return m_current_pixel_density; }
    void set_current_pixel_density(float density) { m_current_pixel_density = density; }

    [[nodiscard]] Optional<Gfx::FloatSize> const& preferred_density_corrected_dimensions() const { return m_preferred_density_corrected_dimensions; }
    void set_preferred_density_corrected_dimensions(Optional<Gfx::FloatSize> dimensions) { m_preferred_density_corrected_dimensions = move(dimensions); }

    // https://html.spec.whatwg.org/multipage/images.html#prepare-an-image-for-presentation
    void prepare_for_presentation(HTMLImageElement&);

    void fetch_image(NonnullRefPtr<Fetch::Infrastructure::Request>);
    void add_callbacks(GC::Ptr<GC::Function<void()>> on_finish, GC::Ptr<GC::Function<void()>> on_fail, GC::Ptr<GC::Function<void()>> on_stop = {}, SharedResourceRequest::DelaysLoadEvent = SharedResourceRequest::DelaysLoadEvent::Yes);

    // Adds callbacks that do not make the fetch delay the load event, and that stop_waiting_for_fetch() keeps.
    void add_callbacks_without_delaying_the_load_event(GC::Ptr<GC::Function<void()>> on_finish, GC::Ptr<GC::Function<void()>> on_fail);

    // Stops waiting for the fetch of the shared resource request, which then no longer delays the load event for
    // this image request.
    void stop_waiting_for_fetch();

    GC::Ptr<SharedResourceRequest const> shared_resource_request() const { return m_shared_resource_request; }

    [[nodiscard]] bool was_aborted() const { return m_was_aborted; }
    void set_was_aborted() { m_was_aborted = true; }

    virtual void visit_edges(JS::Cell::Visitor&) override;

private:
    ImageRequest();

    // https://html.spec.whatwg.org/multipage/images.html#img-req-state
    // An image request's state is initially unavailable.
    State m_state { State::Unavailable };

    // https://html.spec.whatwg.org/multipage/images.html#img-req-url
    // An image request's current URL is initially the empty string.
    Utf16String m_current_url;

    // https://html.spec.whatwg.org/multipage/images.html#img-req-data
    GC::Ptr<DecodedImageData> m_image_data;

    // https://html.spec.whatwg.org/multipage/images.html#current-pixel-density
    // Each image request has a current pixel density, which must initially be 1.
    float m_current_pixel_density { 1 };

    // https://html.spec.whatwg.org/multipage/images.html#preferred-density-corrected-dimensions
    // Each image request has preferred density-corrected dimensions,
    // which is either a struct consisting of a width and a height or is null. It must initially be null.
    Optional<Gfx::FloatSize> m_preferred_density_corrected_dimensions;

    GC::Ptr<SharedResourceRequest> m_shared_resource_request;

    // NB: Set when "abort the image request" is performed on this image request. Since our fetches happen through
    //     a SharedResourceRequest that may be used by others, we don't abort the fetch itself; instead, fetch
    //     callbacks use this flag to discard their work for aborted requests.
    bool m_was_aborted { false };

    Vector<u64> m_shared_resource_request_callbacks_ids;
};

// https://html.spec.whatwg.org/multipage/images.html#abort-the-image-request
void abort_the_image_request(GC::Ptr<ImageRequest>);

}