Resource.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. String encoding_from_content_type(const String& content_type)
  56. {
  57. auto offset = content_type.index_of("charset=");
  58. if (offset.has_value())
  59. return content_type.substring(offset.value() + 8, content_type.length() - offset.value() - 8).to_lowercase();
  60. return "utf-8";
  61. }
  62. String mime_type_from_content_type(const String& content_type)
  63. {
  64. auto offset = content_type.index_of(";");
  65. if (offset.has_value())
  66. return content_type.substring(0, offset.value()).to_lowercase();
  67. return content_type;
  68. }
  69. static String guess_mime_type_based_on_filename(const URL& url)
  70. {
  71. if (url.path().ends_with(".png"))
  72. return "image/png";
  73. if (url.path().ends_with(".gif"))
  74. return "image/gif";
  75. if (url.path().ends_with(".md"))
  76. return "text/markdown";
  77. if (url.path().ends_with(".html") || url.path().ends_with(".htm"))
  78. return "text/html";
  79. return "text/plain";
  80. }
  81. void Resource::did_load(Badge<ResourceLoader>, const ByteBuffer& data, const HashMap<String, String, CaseInsensitiveStringTraits>& headers)
  82. {
  83. ASSERT(!m_loaded);
  84. m_encoded_data = data;
  85. m_response_headers = headers;
  86. m_loaded = true;
  87. auto content_type = headers.get("Content-Type");
  88. if (content_type.has_value()) {
  89. dbg() << "Content-Type header: _" << content_type.value() << "_";
  90. m_encoding = encoding_from_content_type(content_type.value());
  91. m_mime_type = mime_type_from_content_type(content_type.value());
  92. } else if (url().protocol() == "data" && !url().data_mime_type().is_empty()) {
  93. dbg() << "This is a data URL with mime-type _" << url().data_mime_type() << "_";
  94. m_encoding = "utf-8"; // FIXME: This doesn't seem nice.
  95. m_mime_type = url().data_mime_type();
  96. } else {
  97. dbg() << "No Content-Type header to go on! Guessing based on filename...";
  98. m_encoding = "utf-8"; // FIXME: This doesn't seem nice.
  99. m_mime_type = guess_mime_type_based_on_filename(url());
  100. }
  101. for_each_client([](auto& client) {
  102. client.resource_did_load();
  103. });
  104. }
  105. void Resource::did_fail(Badge<ResourceLoader>, const String& error)
  106. {
  107. m_error = error;
  108. m_failed = true;
  109. for_each_client([](auto& client) {
  110. client.resource_did_fail();
  111. });
  112. }
  113. void Resource::register_client(Badge<ResourceClient>, ResourceClient& client)
  114. {
  115. ASSERT(!m_clients.contains(&client));
  116. m_clients.set(&client);
  117. }
  118. void Resource::unregister_client(Badge<ResourceClient>, ResourceClient& client)
  119. {
  120. ASSERT(m_clients.contains(&client));
  121. m_clients.remove(&client);
  122. }
  123. void ResourceClient::set_resource(Resource* resource)
  124. {
  125. if (m_resource)
  126. m_resource->unregister_client({}, *this);
  127. m_resource = resource;
  128. if (m_resource) {
  129. ASSERT(resource->type() == client_type());
  130. m_resource->register_client({}, *this);
  131. // Make sure that reused resources also have their load callback fired.
  132. if (resource->is_loaded())
  133. resource_did_load();
  134. // Make sure that reused resources also have their fail callback fired.
  135. if (resource->is_failed())
  136. resource_did_fail();
  137. }
  138. }
  139. ResourceClient::~ResourceClient()
  140. {
  141. if (m_resource)
  142. m_resource->unregister_client({}, *this);
  143. }
  144. }