Resource.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 <AK/Function.h>
  8. #include <LibCore/MimeData.h>
  9. #include <LibWeb/HTML/HTMLImageElement.h>
  10. #include <LibWeb/Loader/Resource.h>
  11. namespace Web {
  12. NonnullRefPtr<Resource> Resource::create(Badge<ResourceLoader>, Type type, const LoadRequest& request)
  13. {
  14. if (type == Type::Image)
  15. return adopt_ref(*new ImageResource(request));
  16. return adopt_ref(*new Resource(type, request));
  17. }
  18. Resource::Resource(Type type, const LoadRequest& request)
  19. : m_request(request)
  20. , m_type(type)
  21. {
  22. }
  23. Resource::~Resource()
  24. {
  25. }
  26. void Resource::for_each_client(Function<void(ResourceClient&)> callback)
  27. {
  28. Vector<WeakPtr<ResourceClient>, 16> clients_copy;
  29. clients_copy.ensure_capacity(m_clients.size());
  30. for (auto* client : m_clients)
  31. clients_copy.append(client->make_weak_ptr());
  32. for (auto client : clients_copy) {
  33. if (client)
  34. callback(*client);
  35. }
  36. }
  37. static String encoding_from_content_type(const String& content_type)
  38. {
  39. auto offset = content_type.index_of("charset=");
  40. if (offset.has_value()) {
  41. auto encoding = content_type.substring(offset.value() + 8, content_type.length() - offset.value() - 8).to_lowercase();
  42. if (encoding.length() >= 2 && encoding.starts_with('"') && encoding.ends_with('"'))
  43. return encoding.substring(1, encoding.length() - 2);
  44. if (encoding.length() >= 2 && encoding.starts_with('\'') && encoding.ends_with('\''))
  45. return encoding.substring(1, encoding.length() - 2);
  46. return encoding;
  47. }
  48. return "utf-8";
  49. }
  50. static String mime_type_from_content_type(const String& content_type)
  51. {
  52. auto offset = content_type.index_of(";");
  53. if (offset.has_value())
  54. return content_type.substring(0, offset.value()).to_lowercase();
  55. return content_type;
  56. }
  57. void Resource::did_load(Badge<ResourceLoader>, ReadonlyBytes data, const HashMap<String, String, CaseInsensitiveStringTraits>& headers, Optional<u32> status_code)
  58. {
  59. VERIFY(!m_loaded);
  60. m_encoded_data = ByteBuffer::copy(data);
  61. m_response_headers = headers;
  62. m_status_code = move(status_code);
  63. m_loaded = true;
  64. auto content_type = headers.get("Content-Type");
  65. if (content_type.has_value()) {
  66. dbgln_if(RESOURCE_DEBUG, "Content-Type header: '{}'", content_type.value());
  67. m_encoding = encoding_from_content_type(content_type.value());
  68. m_mime_type = mime_type_from_content_type(content_type.value());
  69. } else if (url().protocol() == "data" && !url().data_mime_type().is_empty()) {
  70. dbgln_if(RESOURCE_DEBUG, "This is a data URL with mime-type _{}_", url().data_mime_type());
  71. m_encoding = "utf-8"; // FIXME: This doesn't seem nice.
  72. m_mime_type = url().data_mime_type();
  73. } else {
  74. dbgln_if(RESOURCE_DEBUG, "No Content-Type header to go on! Guessing based on filename...");
  75. m_encoding = "utf-8"; // FIXME: This doesn't seem nice.
  76. m_mime_type = Core::guess_mime_type_based_on_filename(url().path());
  77. }
  78. for_each_client([](auto& client) {
  79. client.resource_did_load();
  80. });
  81. }
  82. void Resource::did_fail(Badge<ResourceLoader>, const String& error, Optional<u32> status_code)
  83. {
  84. m_error = error;
  85. m_status_code = move(status_code);
  86. m_failed = true;
  87. for_each_client([](auto& client) {
  88. client.resource_did_fail();
  89. });
  90. }
  91. void Resource::register_client(Badge<ResourceClient>, ResourceClient& client)
  92. {
  93. VERIFY(!m_clients.contains(&client));
  94. m_clients.set(&client);
  95. }
  96. void Resource::unregister_client(Badge<ResourceClient>, ResourceClient& client)
  97. {
  98. VERIFY(m_clients.contains(&client));
  99. m_clients.remove(&client);
  100. }
  101. void ResourceClient::set_resource(Resource* resource)
  102. {
  103. if (m_resource)
  104. m_resource->unregister_client({}, *this);
  105. m_resource = resource;
  106. if (m_resource) {
  107. VERIFY(resource->type() == client_type());
  108. m_resource->register_client({}, *this);
  109. // Make sure that reused resources also have their load callback fired.
  110. if (resource->is_loaded())
  111. resource_did_load();
  112. // Make sure that reused resources also have their fail callback fired.
  113. if (resource->is_failed())
  114. resource_did_fail();
  115. }
  116. }
  117. ResourceClient::~ResourceClient()
  118. {
  119. if (m_resource)
  120. m_resource->unregister_client({}, *this);
  121. }
  122. }