ImageRequest.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright (c) 2023, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/HashTable.h>
  7. #include <LibGfx/Bitmap.h>
  8. #include <LibWeb/Fetch/Fetching/Fetching.h>
  9. #include <LibWeb/Fetch/Infrastructure/FetchAlgorithms.h>
  10. #include <LibWeb/Fetch/Infrastructure/FetchController.h>
  11. #include <LibWeb/Fetch/Infrastructure/HTTP/Responses.h>
  12. #include <LibWeb/HTML/AnimatedBitmapDecodedImageData.h>
  13. #include <LibWeb/HTML/DecodedImageData.h>
  14. #include <LibWeb/HTML/ImageRequest.h>
  15. #include <LibWeb/HTML/ListOfAvailableImages.h>
  16. #include <LibWeb/Platform/ImageCodecPlugin.h>
  17. #include <LibWeb/SVG/SVGDecodedImageData.h>
  18. namespace Web::HTML {
  19. JS_DEFINE_ALLOCATOR(ImageRequest);
  20. JS::NonnullGCPtr<ImageRequest> ImageRequest::create(JS::Realm& realm, Page& page)
  21. {
  22. return realm.heap().allocate<ImageRequest>(realm, page);
  23. }
  24. ImageRequest::ImageRequest(Page& page)
  25. : m_page(page)
  26. {
  27. }
  28. ImageRequest::~ImageRequest()
  29. {
  30. }
  31. void ImageRequest::visit_edges(JS::Cell::Visitor& visitor)
  32. {
  33. Base::visit_edges(visitor);
  34. visitor.visit(m_shared_image_request);
  35. }
  36. // https://html.spec.whatwg.org/multipage/images.html#img-available
  37. bool ImageRequest::is_available() const
  38. {
  39. // When an image request's state is either partially available or completely available, the image request is said to be available.
  40. return m_state == State::PartiallyAvailable || m_state == State::CompletelyAvailable;
  41. }
  42. bool ImageRequest::is_fetching() const
  43. {
  44. return m_shared_image_request && m_shared_image_request->is_fetching();
  45. }
  46. ImageRequest::State ImageRequest::state() const
  47. {
  48. return m_state;
  49. }
  50. void ImageRequest::set_state(State state)
  51. {
  52. m_state = state;
  53. }
  54. AK::URL const& ImageRequest::current_url() const
  55. {
  56. return m_current_url;
  57. }
  58. void ImageRequest::set_current_url(JS::Realm& realm, AK::URL url)
  59. {
  60. m_current_url = move(url);
  61. if (m_current_url.is_valid())
  62. m_shared_image_request = SharedImageRequest::get_or_create(realm, m_page, m_current_url);
  63. }
  64. // https://html.spec.whatwg.org/multipage/images.html#abort-the-image-request
  65. void abort_the_image_request(JS::Realm&, ImageRequest* image_request)
  66. {
  67. // 1. If image request is null, then return.
  68. if (!image_request)
  69. return;
  70. // 2. Forget image request's image data, if any.
  71. image_request->set_image_data(nullptr);
  72. // 3. Abort any instance of the fetching algorithm for image request,
  73. // discarding any pending tasks generated by that algorithm.
  74. // AD-HOC: We simply don't do this, since our SharedImageRequest may be used by someone else.
  75. }
  76. RefPtr<DecodedImageData const> ImageRequest::image_data() const
  77. {
  78. return m_image_data;
  79. }
  80. void ImageRequest::set_image_data(RefPtr<DecodedImageData const> data)
  81. {
  82. m_image_data = move(data);
  83. }
  84. // https://html.spec.whatwg.org/multipage/images.html#prepare-an-image-for-presentation
  85. void ImageRequest::prepare_for_presentation(HTMLImageElement&)
  86. {
  87. // FIXME: 1. Let exifTagMap be the EXIF tags obtained from req's image data, as defined by the relevant codec. [EXIF]
  88. // FIXME: 2. Let physicalWidth and physicalHeight be the width and height obtained from req's image data, as defined by the relevant codec.
  89. // FIXME: 3. Let dimX be the value of exifTagMap's tag 0xA002 (PixelXDimension).
  90. // FIXME: 4. Let dimY be the value of exifTagMap's tag 0xA003 (PixelYDimension).
  91. // FIXME: 5. Let resX be the value of exifTagMap's tag 0x011A (XResolution).
  92. // FIXME: 6. Let resY be the value of exifTagMap's tag 0x011B (YResolution).
  93. // FIXME: 7. Let resUnit be the value of exifTagMap's tag 0x0128 (ResolutionUnit).
  94. // FIXME: 8. If either dimX or dimY is not a positive integer, then return.
  95. // FIXME: 9. If either resX or resY is not a positive floating-point number, then return.
  96. // FIXME: 10. If resUnit is not equal to 2 (Inch), then return.
  97. // FIXME: 11. Let widthFromDensity be the value of physicalWidth, multiplied by 72 and divided by resX.
  98. // FIXME: 12. Let heightFromDensity be the value of physicalHeight, multiplied by 72 and divided by resY.
  99. // FIXME: 13. If widthFromDensity is not equal to dimX or heightFromDensity is not equal to dimY, then return.
  100. // 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.
  101. // FIXME: 15. Set req's preferred density-corrected dimensions to a struct with its width set to dimX and its height set to dimY.
  102. // FIXME: 16. Update req's img element's presentation appropriately.
  103. }
  104. void ImageRequest::fetch_image(JS::Realm& realm, JS::NonnullGCPtr<Fetch::Infrastructure::Request> request)
  105. {
  106. VERIFY(m_shared_image_request);
  107. m_shared_image_request->fetch_image(realm, request);
  108. }
  109. void ImageRequest::add_callbacks(Function<void()> on_finish, Function<void()> on_fail)
  110. {
  111. VERIFY(m_shared_image_request);
  112. m_shared_image_request->add_callbacks(move(on_finish), move(on_fail));
  113. }
  114. }