SharedImageRequest.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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/SharedImageRequest.h>
  15. #include <LibWeb/Platform/ImageCodecPlugin.h>
  16. #include <LibWeb/SVG/SVGDecodedImageData.h>
  17. namespace Web::HTML {
  18. static HashMap<AK::URL, SharedImageRequest*>& shared_image_requests()
  19. {
  20. static HashMap<AK::URL, SharedImageRequest*> requests;
  21. return requests;
  22. }
  23. JS::NonnullGCPtr<SharedImageRequest> SharedImageRequest::get_or_create(JS::Realm& realm, Page& page, AK::URL const& url)
  24. {
  25. if (auto it = shared_image_requests().find(url); it != shared_image_requests().end())
  26. return *it->value;
  27. auto request = realm.heap().allocate<SharedImageRequest>(realm, page, url);
  28. shared_image_requests().set(url, request);
  29. return request;
  30. }
  31. SharedImageRequest::SharedImageRequest(Page& page, AK::URL url)
  32. : m_page(page)
  33. , m_url(move(url))
  34. {
  35. }
  36. SharedImageRequest::~SharedImageRequest()
  37. {
  38. shared_image_requests().remove(m_url);
  39. }
  40. void SharedImageRequest::visit_edges(JS::Cell::Visitor& visitor)
  41. {
  42. Base::visit_edges(visitor);
  43. visitor.visit(m_fetch_controller);
  44. for (auto& callback : m_callbacks) {
  45. visitor.visit(callback.on_finish);
  46. visitor.visit(callback.on_fail);
  47. }
  48. }
  49. RefPtr<DecodedImageData const> SharedImageRequest::image_data() const
  50. {
  51. return m_image_data;
  52. }
  53. JS::GCPtr<Fetch::Infrastructure::FetchController> SharedImageRequest::fetch_controller()
  54. {
  55. return m_fetch_controller.ptr();
  56. }
  57. void SharedImageRequest::set_fetch_controller(JS::GCPtr<Fetch::Infrastructure::FetchController> fetch_controller)
  58. {
  59. m_fetch_controller = move(fetch_controller);
  60. }
  61. void SharedImageRequest::fetch_image(JS::Realm& realm, JS::NonnullGCPtr<Fetch::Infrastructure::Request> request)
  62. {
  63. Fetch::Infrastructure::FetchAlgorithms::Input fetch_algorithms_input {};
  64. fetch_algorithms_input.process_response = [this, &realm, request](JS::NonnullGCPtr<Fetch::Infrastructure::Response> response) {
  65. // FIXME: If the response is CORS cross-origin, we must use its internal response to query any of its data. See:
  66. // https://github.com/whatwg/html/issues/9355
  67. response = response->unsafe_response();
  68. auto process_body = [this, request, response](ByteBuffer data) {
  69. auto extracted_mime_type = response->header_list()->extract_mime_type().release_value_but_fixme_should_propagate_errors();
  70. auto mime_type = extracted_mime_type.has_value() ? extracted_mime_type.value().essence().bytes_as_string_view() : StringView {};
  71. handle_successful_fetch(request->url(), mime_type, move(data));
  72. };
  73. auto process_body_error = [this](auto) {
  74. handle_failed_fetch();
  75. };
  76. if (response->body())
  77. response->body()->fully_read(realm, move(process_body), move(process_body_error), JS::NonnullGCPtr { realm.global_object() }).release_value_but_fixme_should_propagate_errors();
  78. else
  79. handle_failed_fetch();
  80. };
  81. m_state = State::Fetching;
  82. auto fetch_controller = Fetch::Fetching::fetch(
  83. realm,
  84. request,
  85. Fetch::Infrastructure::FetchAlgorithms::create(realm.vm(), move(fetch_algorithms_input)))
  86. .release_value_but_fixme_should_propagate_errors();
  87. set_fetch_controller(fetch_controller);
  88. }
  89. void SharedImageRequest::add_callbacks(Function<void()> on_finish, Function<void()> on_fail)
  90. {
  91. if (m_state == State::Finished) {
  92. if (on_finish)
  93. on_finish();
  94. return;
  95. }
  96. if (m_state == State::Failed) {
  97. if (on_fail)
  98. on_fail();
  99. return;
  100. }
  101. Callbacks callbacks;
  102. if (on_finish)
  103. callbacks.on_finish = JS::create_heap_function(vm().heap(), move(on_finish));
  104. if (on_fail)
  105. callbacks.on_fail = JS::create_heap_function(vm().heap(), move(on_fail));
  106. m_callbacks.append(move(callbacks));
  107. }
  108. void SharedImageRequest::handle_successful_fetch(AK::URL const& url_string, StringView mime_type, ByteBuffer data)
  109. {
  110. // AD-HOC: At this point, things gets very ad-hoc.
  111. // FIXME: Bring this closer to spec.
  112. bool const is_svg_image = mime_type == "image/svg+xml"sv || url_string.basename().ends_with(".svg"sv);
  113. RefPtr<DecodedImageData> image_data;
  114. auto handle_failed_decode = [&] {
  115. m_state = State::Failed;
  116. for (auto& callback : m_callbacks) {
  117. if (callback.on_fail)
  118. callback.on_fail->function()();
  119. }
  120. };
  121. if (is_svg_image) {
  122. auto result = SVG::SVGDecodedImageData::create(m_page, url_string, data);
  123. if (result.is_error())
  124. return handle_failed_decode();
  125. image_data = result.release_value();
  126. } else {
  127. auto result = Web::Platform::ImageCodecPlugin::the().decode_image(data.bytes());
  128. if (!result.has_value())
  129. return handle_failed_decode();
  130. Vector<AnimatedBitmapDecodedImageData::Frame> frames;
  131. for (auto& frame : result.value().frames) {
  132. frames.append(AnimatedBitmapDecodedImageData::Frame {
  133. .bitmap = frame.bitmap,
  134. .duration = static_cast<int>(frame.duration),
  135. });
  136. }
  137. image_data = AnimatedBitmapDecodedImageData::create(move(frames), result.value().loop_count, result.value().is_animated).release_value_but_fixme_should_propagate_errors();
  138. }
  139. m_image_data = move(image_data);
  140. m_state = State::Finished;
  141. for (auto& callback : m_callbacks) {
  142. if (callback.on_finish)
  143. callback.on_finish->function()();
  144. }
  145. m_callbacks.clear();
  146. }
  147. void SharedImageRequest::handle_failed_fetch()
  148. {
  149. m_state = State::Failed;
  150. for (auto& callback : m_callbacks) {
  151. if (callback.on_fail)
  152. callback.on_fail->function()();
  153. }
  154. m_callbacks.clear();
  155. }
  156. bool SharedImageRequest::needs_fetching() const
  157. {
  158. return m_state == State::New;
  159. }
  160. bool SharedImageRequest::is_fetching() const
  161. {
  162. return m_state == State::Fetching;
  163. }
  164. }