ImageRequest.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. public:
  19. [[nodiscard]] static JS::NonnullGCPtr<ImageRequest> create(JS::Realm&, Page&);
  20. ~ImageRequest();
  21. // https://html.spec.whatwg.org/multipage/images.html#img-req-state
  22. enum class State {
  23. Unavailable,
  24. PartiallyAvailable,
  25. CompletelyAvailable,
  26. Broken,
  27. };
  28. bool is_available() const;
  29. bool is_fetching() const;
  30. State state() const;
  31. void set_state(State);
  32. AK::URL const& current_url() const;
  33. void set_current_url(AK::URL);
  34. [[nodiscard]] RefPtr<DecodedImageData const> image_data() const;
  35. void set_image_data(RefPtr<DecodedImageData const>);
  36. [[nodiscard]] float current_pixel_density() const { return m_current_pixel_density; }
  37. void set_current_pixel_density(float density) { m_current_pixel_density = density; }
  38. [[nodiscard]] Optional<Gfx::FloatSize> const& preferred_density_corrected_dimensions() const { return m_preferred_density_corrected_dimensions; }
  39. void set_preferred_density_corrected_dimensions(Optional<Gfx::FloatSize> dimensions) { m_preferred_density_corrected_dimensions = move(dimensions); }
  40. // https://html.spec.whatwg.org/multipage/images.html#prepare-an-image-for-presentation
  41. void prepare_for_presentation(HTMLImageElement&);
  42. void fetch_image(JS::Realm&, JS::NonnullGCPtr<Fetch::Infrastructure::Request>);
  43. void add_callbacks(JS::SafeFunction<void()> on_finish, JS::SafeFunction<void()> on_fail);
  44. SharedImageRequest const* shared_image_request() const { return m_shared_image_request; }
  45. private:
  46. explicit ImageRequest(Page&);
  47. Page& m_page;
  48. // https://html.spec.whatwg.org/multipage/images.html#img-req-state
  49. // An image request's state is initially unavailable.
  50. State m_state { State::Unavailable };
  51. // https://html.spec.whatwg.org/multipage/images.html#img-req-url
  52. // An image request's current URL is initially the empty string.
  53. AK::URL m_current_url;
  54. // https://html.spec.whatwg.org/multipage/images.html#img-req-data
  55. RefPtr<DecodedImageData const> m_image_data;
  56. // https://html.spec.whatwg.org/multipage/images.html#current-pixel-density
  57. // Each image request has a current pixel density, which must initially be 1.
  58. float m_current_pixel_density { 1 };
  59. // https://html.spec.whatwg.org/multipage/images.html#preferred-density-corrected-dimensions
  60. // Each image request has preferred density-corrected dimensions,
  61. // which is either a struct consisting of a width and a height or is null. It must initially be null.
  62. Optional<Gfx::FloatSize> m_preferred_density_corrected_dimensions;
  63. RefPtr<SharedImageRequest> m_shared_image_request;
  64. };
  65. // https://html.spec.whatwg.org/multipage/images.html#abort-the-image-request
  66. void abort_the_image_request(JS::Realm&, ImageRequest*);
  67. }