SharedImageRequest.cpp 7.3 KB

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