ImageRequest.cpp 5.0 KB

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