Resource.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. #pragma once
  27. #include <AK/ByteBuffer.h>
  28. #include <AK/HashMap.h>
  29. #include <AK/HashTable.h>
  30. #include <AK/Noncopyable.h>
  31. #include <AK/RefCounted.h>
  32. #include <AK/URL.h>
  33. #include <AK/WeakPtr.h>
  34. #include <AK/Weakable.h>
  35. #include <LibGfx/Forward.h>
  36. #include <LibWeb/Forward.h>
  37. #include <LibWeb/Loader/LoadRequest.h>
  38. namespace Web {
  39. class ResourceClient;
  40. class Resource : public RefCounted<Resource> {
  41. AK_MAKE_NONCOPYABLE(Resource);
  42. AK_MAKE_NONMOVABLE(Resource);
  43. public:
  44. enum class Type {
  45. Generic,
  46. Image,
  47. };
  48. static NonnullRefPtr<Resource> create(Badge<ResourceLoader>, Type, const LoadRequest&);
  49. virtual ~Resource();
  50. Type type() const { return m_type; }
  51. bool is_loaded() const { return m_loaded; }
  52. bool is_failed() const { return m_failed; }
  53. const String& error() const { return m_error; }
  54. bool has_encoded_data() const { return !m_encoded_data.is_null(); }
  55. const URL& url() const { return m_request.url(); }
  56. const ByteBuffer& encoded_data() const { return m_encoded_data; }
  57. const HashMap<String, String, CaseInsensitiveStringTraits>& response_headers() const { return m_response_headers; }
  58. void register_client(Badge<ResourceClient>, ResourceClient&);
  59. void unregister_client(Badge<ResourceClient>, ResourceClient&);
  60. const String& encoding() const { return m_encoding; }
  61. const String& mime_type() const { return m_mime_type; }
  62. void for_each_client(Function<void(ResourceClient&)>);
  63. void did_load(Badge<ResourceLoader>, const ByteBuffer& data, const HashMap<String, String, CaseInsensitiveStringTraits>& headers);
  64. void did_fail(Badge<ResourceLoader>, const String& error);
  65. protected:
  66. explicit Resource(Type, const LoadRequest&);
  67. private:
  68. LoadRequest m_request;
  69. ByteBuffer m_encoded_data;
  70. Type m_type { Type::Generic };
  71. bool m_loaded { false };
  72. bool m_failed { false };
  73. String m_error;
  74. String m_encoding;
  75. String m_mime_type;
  76. HashMap<String, String, CaseInsensitiveStringTraits> m_response_headers;
  77. HashTable<ResourceClient*> m_clients;
  78. };
  79. class ResourceClient : public Weakable<ResourceClient> {
  80. public:
  81. virtual ~ResourceClient();
  82. virtual void resource_did_load() { }
  83. virtual void resource_did_fail() { }
  84. protected:
  85. virtual Resource::Type client_type() const { return Resource::Type::Generic; }
  86. Resource* resource() { return m_resource; }
  87. const Resource* resource() const { return m_resource; }
  88. void set_resource(Resource*);
  89. private:
  90. RefPtr<Resource> m_resource;
  91. };
  92. }