ImageRequest.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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(Page&);
  18. static ErrorOr<NonnullRefPtr<ImageRequest>> get_shareable_or_create(Page&, AK::URL const&);
  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. State state() const;
  29. void set_state(State);
  30. AK::URL const& current_url() const;
  31. void set_current_url(AK::URL);
  32. [[nodiscard]] RefPtr<DecodedImageData const> image_data() const;
  33. void set_image_data(RefPtr<DecodedImageData const>);
  34. [[nodiscard]] float current_pixel_density() const { return m_current_pixel_density; }
  35. void set_current_pixel_density(float density) { m_current_pixel_density = density; }
  36. [[nodiscard]] Optional<Gfx::FloatSize> const& preferred_density_corrected_dimensions() const { return m_preferred_density_corrected_dimensions; }
  37. void set_preferred_density_corrected_dimensions(Optional<Gfx::FloatSize> dimensions) { m_preferred_density_corrected_dimensions = move(dimensions); }
  38. // https://html.spec.whatwg.org/multipage/images.html#prepare-an-image-for-presentation
  39. void prepare_for_presentation(HTMLImageElement&);
  40. [[nodiscard]] JS::GCPtr<Fetch::Infrastructure::FetchController> fetch_controller();
  41. void set_fetch_controller(JS::GCPtr<Fetch::Infrastructure::FetchController>);
  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. private:
  45. explicit ImageRequest(Page&);
  46. void handle_successful_fetch(AK::URL const&, StringView mime_type, ByteBuffer data);
  47. void handle_failed_fetch();
  48. Page& m_page;
  49. struct Callbacks {
  50. JS::SafeFunction<void()> on_finish;
  51. JS::SafeFunction<void()> on_fail;
  52. };
  53. Vector<Callbacks> m_callbacks;
  54. // https://html.spec.whatwg.org/multipage/images.html#img-req-state
  55. // An image request's state is initially unavailable.
  56. State m_state { State::Unavailable };
  57. // https://html.spec.whatwg.org/multipage/images.html#img-req-url
  58. // An image request's current URL is initially the empty string.
  59. AK::URL m_current_url;
  60. // https://html.spec.whatwg.org/multipage/images.html#img-req-data
  61. RefPtr<DecodedImageData const> m_image_data;
  62. // https://html.spec.whatwg.org/multipage/images.html#current-pixel-density
  63. // Each image request has a current pixel density, which must initially be 1.
  64. float m_current_pixel_density { 1 };
  65. // https://html.spec.whatwg.org/multipage/images.html#preferred-density-corrected-dimensions
  66. // Each image request has preferred density-corrected dimensions,
  67. // which is either a struct consisting of a width and a height or is null. It must initially be null.
  68. Optional<Gfx::FloatSize> m_preferred_density_corrected_dimensions;
  69. JS::Handle<Fetch::Infrastructure::FetchController> m_fetch_controller;
  70. };
  71. // https://html.spec.whatwg.org/multipage/images.html#abort-the-image-request
  72. void abort_the_image_request(JS::Realm&, ImageRequest*);
  73. }