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