/src/serenity/Userland/Libraries/LibWeb/HTML/ImageRequest.h
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2023, Andreas Kling <kling@serenityos.org> |
3 | | * |
4 | | * SPDX-License-Identifier: BSD-2-Clause |
5 | | */ |
6 | | |
7 | | #pragma once |
8 | | |
9 | | #include <AK/Error.h> |
10 | | #include <AK/OwnPtr.h> |
11 | | #include <LibGfx/Size.h> |
12 | | #include <LibJS/Heap/Handle.h> |
13 | | #include <LibURL/URL.h> |
14 | | #include <LibWeb/Forward.h> |
15 | | |
16 | | namespace Web::HTML { |
17 | | |
18 | | // https://html.spec.whatwg.org/multipage/images.html#image-request |
19 | | class ImageRequest final : public JS::Cell { |
20 | | JS_CELL(ImageRequest, JS::Cell); |
21 | | JS_DECLARE_ALLOCATOR(ImageRequest); |
22 | | |
23 | | public: |
24 | | [[nodiscard]] static JS::NonnullGCPtr<ImageRequest> create(JS::Realm&, JS::NonnullGCPtr<Page>); |
25 | | |
26 | | ~ImageRequest(); |
27 | | |
28 | | // https://html.spec.whatwg.org/multipage/images.html#img-req-state |
29 | | enum class State { |
30 | | Unavailable, |
31 | | PartiallyAvailable, |
32 | | CompletelyAvailable, |
33 | | Broken, |
34 | | }; |
35 | | |
36 | | bool is_available() const; |
37 | | bool is_fetching() const; |
38 | | |
39 | | State state() const; |
40 | | void set_state(State); |
41 | | |
42 | | URL::URL const& current_url() const; |
43 | | void set_current_url(JS::Realm&, URL::URL); |
44 | | |
45 | | [[nodiscard]] JS::GCPtr<DecodedImageData> image_data() const; |
46 | | void set_image_data(JS::GCPtr<DecodedImageData>); |
47 | | |
48 | 0 | [[nodiscard]] float current_pixel_density() const { return m_current_pixel_density; } |
49 | 0 | void set_current_pixel_density(float density) { m_current_pixel_density = density; } |
50 | | |
51 | 0 | [[nodiscard]] Optional<Gfx::FloatSize> const& preferred_density_corrected_dimensions() const { return m_preferred_density_corrected_dimensions; } |
52 | 0 | void set_preferred_density_corrected_dimensions(Optional<Gfx::FloatSize> dimensions) { m_preferred_density_corrected_dimensions = move(dimensions); } |
53 | | |
54 | | // https://html.spec.whatwg.org/multipage/images.html#prepare-an-image-for-presentation |
55 | | void prepare_for_presentation(HTMLImageElement&); |
56 | | |
57 | | void fetch_image(JS::Realm&, JS::NonnullGCPtr<Fetch::Infrastructure::Request>); |
58 | | void add_callbacks(Function<void()> on_finish, Function<void()> on_fail); |
59 | | |
60 | 0 | JS::GCPtr<SharedResourceRequest const> shared_resource_request() const { return m_shared_resource_request; } |
61 | | |
62 | | virtual void visit_edges(JS::Cell::Visitor&) override; |
63 | | |
64 | | private: |
65 | | explicit ImageRequest(JS::NonnullGCPtr<Page>); |
66 | | |
67 | | JS::NonnullGCPtr<Page> m_page; |
68 | | |
69 | | // https://html.spec.whatwg.org/multipage/images.html#img-req-state |
70 | | // An image request's state is initially unavailable. |
71 | | State m_state { State::Unavailable }; |
72 | | |
73 | | // https://html.spec.whatwg.org/multipage/images.html#img-req-url |
74 | | // An image request's current URL is initially the empty string. |
75 | | URL::URL m_current_url; |
76 | | |
77 | | // https://html.spec.whatwg.org/multipage/images.html#img-req-data |
78 | | JS::GCPtr<DecodedImageData> m_image_data; |
79 | | |
80 | | // https://html.spec.whatwg.org/multipage/images.html#current-pixel-density |
81 | | // Each image request has a current pixel density, which must initially be 1. |
82 | | float m_current_pixel_density { 1 }; |
83 | | |
84 | | // https://html.spec.whatwg.org/multipage/images.html#preferred-density-corrected-dimensions |
85 | | // Each image request has preferred density-corrected dimensions, |
86 | | // which is either a struct consisting of a width and a height or is null. It must initially be null. |
87 | | Optional<Gfx::FloatSize> m_preferred_density_corrected_dimensions; |
88 | | |
89 | | JS::GCPtr<SharedResourceRequest> m_shared_resource_request; |
90 | | }; |
91 | | |
92 | | // https://html.spec.whatwg.org/multipage/images.html#abort-the-image-request |
93 | | void abort_the_image_request(JS::Realm&, ImageRequest*); |
94 | | |
95 | | } |