ImageRequest.cpp 5.0 KB

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