ImageRequest.h 3.2 KB

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