ImageRequest.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * Copyright (c) 2023, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibGfx/Bitmap.h>
  7. #include <LibWeb/Fetch/Infrastructure/FetchController.h>
  8. #include <LibWeb/HTML/DecodedImageData.h>
  9. #include <LibWeb/HTML/ImageRequest.h>
  10. namespace Web::HTML {
  11. ErrorOr<NonnullRefPtr<ImageRequest>> ImageRequest::create()
  12. {
  13. return adopt_nonnull_ref_or_enomem(new (nothrow) ImageRequest);
  14. }
  15. ImageRequest::ImageRequest() = default;
  16. ImageRequest::~ImageRequest() = default;
  17. // https://html.spec.whatwg.org/multipage/images.html#img-available
  18. bool ImageRequest::is_available() const
  19. {
  20. // When an image request's state is either partially available or completely available, the image request is said to be available.
  21. return m_state == State::PartiallyAvailable || m_state == State::CompletelyAvailable;
  22. }
  23. ImageRequest::State ImageRequest::state() const
  24. {
  25. return m_state;
  26. }
  27. void ImageRequest::set_state(State state)
  28. {
  29. m_state = state;
  30. }
  31. AK::URL const& ImageRequest::current_url() const
  32. {
  33. return m_current_url;
  34. }
  35. void ImageRequest::set_current_url(AK::URL url)
  36. {
  37. m_current_url = move(url);
  38. }
  39. // https://html.spec.whatwg.org/multipage/images.html#abort-the-image-request
  40. void ImageRequest::abort(JS::Realm& realm)
  41. {
  42. // 1. Forget image request's image data, if any.
  43. m_image_data = nullptr;
  44. // 2. Abort any instance of the fetching algorithm for image request,
  45. // discarding any pending tasks generated by that algorithm.
  46. if (m_fetch_controller)
  47. m_fetch_controller->abort(realm, {});
  48. m_fetch_controller = nullptr;
  49. }
  50. RefPtr<DecodedImageData const> ImageRequest::image_data() const
  51. {
  52. return m_image_data;
  53. }
  54. void ImageRequest::set_image_data(RefPtr<DecodedImageData const> data)
  55. {
  56. m_image_data = move(data);
  57. }
  58. // https://html.spec.whatwg.org/multipage/images.html#prepare-an-image-for-presentation
  59. void ImageRequest::prepare_for_presentation(HTMLImageElement&)
  60. {
  61. // FIXME: 1. Let exifTagMap be the EXIF tags obtained from req's image data, as defined by the relevant codec. [EXIF]
  62. // FIXME: 2. Let physicalWidth and physicalHeight be the width and height obtained from req's image data, as defined by the relevant codec.
  63. // FIXME: 3. Let dimX be the value of exifTagMap's tag 0xA002 (PixelXDimension).
  64. // FIXME: 4. Let dimY be the value of exifTagMap's tag 0xA003 (PixelYDimension).
  65. // FIXME: 5. Let resX be the value of exifTagMap's tag 0x011A (XResolution).
  66. // FIXME: 6. Let resY be the value of exifTagMap's tag 0x011B (YResolution).
  67. // FIXME: 7. Let resUnit be the value of exifTagMap's tag 0x0128 (ResolutionUnit).
  68. // FIXME: 8. If either dimX or dimY is not a positive integer, then return.
  69. // FIXME: 9. If either resX or resY is not a positive floating-point number, then return.
  70. // FIXME: 10. If resUnit is not equal to 2 (Inch), then return.
  71. // FIXME: 11. Let widthFromDensity be the value of physicalWidth, multiplied by 72 and divided by resX.
  72. // FIXME: 12. Let heightFromDensity be the value of physicalHeight, multiplied by 72 and divided by resY.
  73. // FIXME: 13. If widthFromDensity is not equal to dimX or heightFromDensity is not equal to dimY, then return.
  74. // FIXME: 14. If req's image data is CORS-cross-origin, then set img's intrinsic dimensions to dimX and dimY, scale img's pixel data accordingly, and return.
  75. // FIXME: 15. Set req's preferred density-corrected dimensions to a struct with its width set to dimX and its height set to dimY.
  76. // FIXME: 16. Update req's img element's presentation appropriately.
  77. }
  78. void ImageRequest::set_fetch_controller(JS::GCPtr<Fetch::Infrastructure::FetchController> fetch_controller)
  79. {
  80. m_fetch_controller = move(fetch_controller);
  81. }
  82. }