ImageRequest.h 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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(JS::Realm&, 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. virtual void visit_edges(JS::Cell::Visitor&) override;
  46. private:
  47. explicit ImageRequest(Page&);
  48. Page& m_page;
  49. // https://html.spec.whatwg.org/multipage/images.html#img-req-state
  50. // An image request's state is initially unavailable.
  51. State m_state { State::Unavailable };
  52. // https://html.spec.whatwg.org/multipage/images.html#img-req-url
  53. // An image request's current URL is initially the empty string.
  54. AK::URL m_current_url;
  55. // https://html.spec.whatwg.org/multipage/images.html#img-req-data
  56. RefPtr<DecodedImageData const> m_image_data;
  57. // https://html.spec.whatwg.org/multipage/images.html#current-pixel-density
  58. // Each image request has a current pixel density, which must initially be 1.
  59. float m_current_pixel_density { 1 };
  60. // https://html.spec.whatwg.org/multipage/images.html#preferred-density-corrected-dimensions
  61. // Each image request has preferred density-corrected dimensions,
  62. // which is either a struct consisting of a width and a height or is null. It must initially be null.
  63. Optional<Gfx::FloatSize> m_preferred_density_corrected_dimensions;
  64. JS::GCPtr<SharedImageRequest> m_shared_image_request;
  65. };
  66. // https://html.spec.whatwg.org/multipage/images.html#abort-the-image-request
  67. void abort_the_image_request(JS::Realm&, ImageRequest*);
  68. }