SharedImageRequest.cpp 5.7 KB

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