ImageRequest.h 3.3 KB

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