ImageLoader.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Debug.h>
  7. #include <LibCore/Timer.h>
  8. #include <LibGfx/Bitmap.h>
  9. #include <LibWeb/DOM/Document.h>
  10. #include <LibWeb/DOM/Element.h>
  11. #include <LibWeb/Loader/ImageLoader.h>
  12. #include <LibWeb/Loader/ResourceLoader.h>
  13. namespace Web {
  14. ImageLoader::ImageLoader(DOM::Element& owner_element)
  15. : m_owner_element(owner_element)
  16. , m_timer(Core::Timer::construct())
  17. {
  18. }
  19. void ImageLoader::adopt_object_resource(Badge<HTML::HTMLObjectElement>, Resource& resource)
  20. {
  21. auto image_resource = ImageResource::convert_from_resource(resource);
  22. set_resource(image_resource);
  23. }
  24. void ImageLoader::load(const AK::URL& url)
  25. {
  26. m_redirects_count = 0;
  27. load_without_resetting_redirect_counter(url);
  28. }
  29. void ImageLoader::load_without_resetting_redirect_counter(AK::URL const& url)
  30. {
  31. m_loading_state = LoadingState::Loading;
  32. auto request = LoadRequest::create_for_url_on_page(url, m_owner_element.document().page());
  33. set_resource(ResourceLoader::the().load_resource(Resource::Type::Image, request));
  34. }
  35. void ImageLoader::set_visible_in_viewport(bool visible_in_viewport) const
  36. {
  37. if (m_visible_in_viewport == visible_in_viewport)
  38. return;
  39. m_visible_in_viewport = visible_in_viewport;
  40. // FIXME: Don't update volatility every time. If we're here, we're probably scanning through
  41. // the whole document, updating "is visible in viewport" flags, and this could lead
  42. // to the same bitmap being marked volatile back and forth unnecessarily.
  43. if (resource())
  44. const_cast<ImageResource*>(resource())->update_volatility();
  45. }
  46. void ImageLoader::resource_did_load()
  47. {
  48. VERIFY(resource());
  49. // For 3xx (Redirection) responses, the Location value refers to the preferred target resource for automatically redirecting the request.
  50. auto status_code = resource()->status_code();
  51. if (status_code.has_value() && *status_code >= 300 && *status_code <= 399) {
  52. auto location = resource()->response_headers().get("Location");
  53. if (location.has_value()) {
  54. if (m_redirects_count > maximum_redirects_allowed) {
  55. m_redirects_count = 0;
  56. m_loading_state = LoadingState::Failed;
  57. if (on_fail)
  58. on_fail();
  59. return;
  60. }
  61. m_redirects_count++;
  62. load_without_resetting_redirect_counter(resource()->url().complete_url(location.value()));
  63. return;
  64. }
  65. }
  66. m_redirects_count = 0;
  67. if (!resource()->mime_type().starts_with("image/")) {
  68. m_loading_state = LoadingState::Failed;
  69. if (on_fail)
  70. on_fail();
  71. return;
  72. }
  73. m_loading_state = LoadingState::Loaded;
  74. if constexpr (IMAGE_LOADER_DEBUG) {
  75. if (!resource()->has_encoded_data()) {
  76. dbgln("ImageLoader: Resource did load, no encoded data. URL: {}", resource()->url());
  77. } else {
  78. dbgln("ImageLoader: Resource did load, has encoded data. URL: {}", resource()->url());
  79. }
  80. }
  81. if (resource()->is_animated() && resource()->frame_count() > 1) {
  82. m_timer->set_interval(resource()->frame_duration(0));
  83. m_timer->on_timeout = [this] { animate(); };
  84. m_timer->start();
  85. }
  86. if (on_load)
  87. on_load();
  88. }
  89. void ImageLoader::animate()
  90. {
  91. if (!m_visible_in_viewport)
  92. return;
  93. m_current_frame_index = (m_current_frame_index + 1) % resource()->frame_count();
  94. auto current_frame_duration = resource()->frame_duration(m_current_frame_index);
  95. if (current_frame_duration != m_timer->interval()) {
  96. m_timer->restart(current_frame_duration);
  97. }
  98. if (m_current_frame_index == resource()->frame_count() - 1) {
  99. ++m_loops_completed;
  100. if (m_loops_completed > 0 && m_loops_completed == resource()->loop_count()) {
  101. m_timer->stop();
  102. }
  103. }
  104. if (on_animate)
  105. on_animate();
  106. }
  107. void ImageLoader::resource_did_fail()
  108. {
  109. dbgln("ImageLoader: Resource did fail. URL: {}", resource()->url());
  110. m_loading_state = LoadingState::Failed;
  111. if (on_fail)
  112. on_fail();
  113. }
  114. bool ImageLoader::has_image() const
  115. {
  116. if (!resource())
  117. return false;
  118. return bitmap(0);
  119. }
  120. unsigned ImageLoader::width() const
  121. {
  122. if (!resource())
  123. return 0;
  124. return bitmap(0) ? bitmap(0)->width() : 0;
  125. }
  126. unsigned ImageLoader::height() const
  127. {
  128. if (!resource())
  129. return 0;
  130. return bitmap(0) ? bitmap(0)->height() : 0;
  131. }
  132. const Gfx::Bitmap* ImageLoader::bitmap(size_t frame_index) const
  133. {
  134. if (!resource())
  135. return nullptr;
  136. return resource()->bitmap(frame_index);
  137. }
  138. }