ImageRequest.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. namespace Web::HTML {
  14. // https://html.spec.whatwg.org/multipage/images.html#image-request
  15. class ImageRequest : public RefCounted<ImageRequest> {
  16. public:
  17. static ErrorOr<NonnullRefPtr<ImageRequest>> create();
  18. ~ImageRequest();
  19. // https://html.spec.whatwg.org/multipage/images.html#img-req-state
  20. enum class State {
  21. Unavailable,
  22. PartiallyAvailable,
  23. CompletelyAvailable,
  24. Broken,
  25. };
  26. bool is_available() const;
  27. State state() const;
  28. void set_state(State);
  29. AK::URL const& current_url() const;
  30. void set_current_url(AK::URL);
  31. // https://html.spec.whatwg.org/multipage/images.html#abort-the-image-request
  32. void abort(JS::Realm&);
  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 set_fetch_controller(JS::GCPtr<Fetch::Infrastructure::FetchController>);
  42. private:
  43. ImageRequest();
  44. // https://html.spec.whatwg.org/multipage/images.html#img-req-state
  45. // An image request's state is initially unavailable.
  46. State m_state { State::Unavailable };
  47. // https://html.spec.whatwg.org/multipage/images.html#img-req-url
  48. // An image request's current URL is initially the empty string.
  49. AK::URL m_current_url;
  50. // https://html.spec.whatwg.org/multipage/images.html#img-req-data
  51. RefPtr<DecodedImageData const> m_image_data;
  52. // https://html.spec.whatwg.org/multipage/images.html#current-pixel-density
  53. // Each image request has a current pixel density, which must initially be 1.
  54. float m_current_pixel_density { 1 };
  55. // https://html.spec.whatwg.org/multipage/images.html#preferred-density-corrected-dimensions
  56. // Each image request has preferred density-corrected dimensions,
  57. // which is either a struct consisting of a width and a height or is null. It must initially be null.
  58. Optional<Gfx::FloatSize> m_preferred_density_corrected_dimensions;
  59. JS::Handle<Fetch::Infrastructure::FetchController> m_fetch_controller;
  60. };
  61. }