ImageRequest.cpp 4.7 KB

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