Resource.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/Function.h>
  27. #include <LibWeb/DOM/HTMLImageElement.h>
  28. #include <LibWeb/Loader/Resource.h>
  29. namespace Web {
  30. NonnullRefPtr<Resource> Resource::create(Badge<ResourceLoader>, Type type, const LoadRequest& request)
  31. {
  32. if (type == Type::Image)
  33. return adopt(*new ImageResource(request));
  34. return adopt(*new Resource(type, request));
  35. }
  36. Resource::Resource(Type type, const LoadRequest& request)
  37. : m_request(request)
  38. , m_type(type)
  39. {
  40. }
  41. Resource::~Resource()
  42. {
  43. }
  44. void Resource::for_each_client(Function<void(ResourceClient&)> callback)
  45. {
  46. Vector<WeakPtr<ResourceClient>, 16> clients_copy;
  47. clients_copy.ensure_capacity(m_clients.size());
  48. for (auto* client : m_clients)
  49. clients_copy.append(client->make_weak_ptr());
  50. for (auto client : clients_copy) {
  51. if (client)
  52. callback(*client);
  53. }
  54. }
  55. void Resource::did_load(Badge<ResourceLoader>, const ByteBuffer& data, const HashMap<String, String, CaseInsensitiveStringTraits>& headers)
  56. {
  57. ASSERT(!m_loaded);
  58. m_encoded_data = data;
  59. m_response_headers = headers;
  60. m_loaded = true;
  61. for_each_client([](auto& client) {
  62. client.resource_did_load();
  63. });
  64. }
  65. void Resource::did_fail(Badge<ResourceLoader>, const String& error)
  66. {
  67. m_error = error;
  68. m_failed = true;
  69. for_each_client([](auto& client) {
  70. client.resource_did_fail();
  71. });
  72. }
  73. void Resource::register_client(Badge<ResourceClient>, ResourceClient& client)
  74. {
  75. ASSERT(!m_clients.contains(&client));
  76. m_clients.set(&client);
  77. }
  78. void Resource::unregister_client(Badge<ResourceClient>, ResourceClient& client)
  79. {
  80. ASSERT(m_clients.contains(&client));
  81. m_clients.remove(&client);
  82. }
  83. void ResourceClient::set_resource(Resource* resource)
  84. {
  85. if (m_resource)
  86. m_resource->unregister_client({}, *this);
  87. m_resource = resource;
  88. if (m_resource) {
  89. m_resource->register_client({}, *this);
  90. // Make sure that reused resources also have their load callback fired.
  91. if (resource->is_loaded())
  92. resource_did_load();
  93. // Make sure that reused resources also have their fail callback fired.
  94. if (resource->is_failed())
  95. resource_did_fail();
  96. }
  97. }
  98. ResourceClient::~ResourceClient()
  99. {
  100. if (m_resource)
  101. m_resource->unregister_client({}, *this);
  102. }
  103. }