SharedResourceRequest.cpp 7.2 KB

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