ImageRequest.cpp 4.9 KB

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