SharedImageRequest.cpp 6.5 KB

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