SharedImageRequest.cpp 5.9 KB

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