SharedImageRequest.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 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, 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 = [this, request, response](ByteBuffer data) {
  76. auto extracted_mime_type = response->header_list()->extract_mime_type().release_value_but_fixme_should_propagate_errors();
  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 = [this](auto) {
  81. handle_failed_fetch();
  82. };
  83. if (response->body())
  84. response->body()->fully_read(realm, move(process_body), move(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 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. JS::GCPtr<DecodedImageData> image_data;
  121. auto handle_failed_decode = [&] {
  122. m_state = State::Failed;
  123. for (auto& callback : m_callbacks) {
  124. if (callback.on_fail)
  125. callback.on_fail->function()();
  126. }
  127. };
  128. if (is_svg_image) {
  129. auto result = SVG::SVGDecodedImageData::create(m_document->realm(), m_page, url_string, data);
  130. if (result.is_error())
  131. return handle_failed_decode();
  132. image_data = result.release_value();
  133. } else {
  134. auto result = Web::Platform::ImageCodecPlugin::the().decode_image(data.bytes());
  135. if (!result.has_value())
  136. return handle_failed_decode();
  137. Vector<AnimatedBitmapDecodedImageData::Frame> frames;
  138. for (auto& frame : result.value().frames) {
  139. frames.append(AnimatedBitmapDecodedImageData::Frame {
  140. .bitmap = Gfx::ImmutableBitmap::create(*frame.bitmap),
  141. .duration = static_cast<int>(frame.duration),
  142. });
  143. }
  144. image_data = AnimatedBitmapDecodedImageData::create(m_document->realm(), move(frames), result.value().loop_count, result.value().is_animated).release_value_but_fixme_should_propagate_errors();
  145. }
  146. m_image_data = image_data;
  147. m_state = State::Finished;
  148. for (auto& callback : m_callbacks) {
  149. if (callback.on_finish)
  150. callback.on_finish->function()();
  151. }
  152. m_callbacks.clear();
  153. }
  154. void SharedImageRequest::handle_failed_fetch()
  155. {
  156. m_state = State::Failed;
  157. for (auto& callback : m_callbacks) {
  158. if (callback.on_fail)
  159. callback.on_fail->function()();
  160. }
  161. m_callbacks.clear();
  162. }
  163. bool SharedImageRequest::needs_fetching() const
  164. {
  165. return m_state == State::New;
  166. }
  167. bool SharedImageRequest::is_fetching() const
  168. {
  169. return m_state == State::Fetching;
  170. }
  171. }