| 
									
										
										
										
											2020-06-01 21:33:23 +02:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2020, Andreas Kling <kling@serenityos.org> | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2021-04-22 01:24:48 -07:00
										 |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							| 
									
										
										
										
											2020-06-01 21:33:23 +02:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <AK/ByteBuffer.h>
 | 
					
						
							|  |  |  | #include <AK/HashMap.h>
 | 
					
						
							|  |  |  | #include <AK/HashTable.h>
 | 
					
						
							|  |  |  | #include <AK/Noncopyable.h>
 | 
					
						
							|  |  |  | #include <AK/RefCounted.h>
 | 
					
						
							|  |  |  | #include <AK/URL.h>
 | 
					
						
							| 
									
										
										
										
											2020-06-01 22:09:38 +02:00
										 |  |  | #include <AK/WeakPtr.h>
 | 
					
						
							|  |  |  | #include <AK/Weakable.h>
 | 
					
						
							| 
									
										
										
										
											2020-06-02 20:27:26 +02:00
										 |  |  | #include <LibGfx/Forward.h>
 | 
					
						
							| 
									
										
										
										
											2020-06-01 21:33:23 +02:00
										 |  |  | #include <LibWeb/Forward.h>
 | 
					
						
							| 
									
										
										
										
											2020-06-01 21:58:29 +02:00
										 |  |  | #include <LibWeb/Loader/LoadRequest.h>
 | 
					
						
							| 
									
										
										
										
											2020-06-01 21:33:23 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace Web { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class ResourceClient; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class Resource : public RefCounted<Resource> { | 
					
						
							|  |  |  |     AK_MAKE_NONCOPYABLE(Resource); | 
					
						
							|  |  |  |     AK_MAKE_NONMOVABLE(Resource); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2020-06-02 20:27:26 +02:00
										 |  |  |     enum class Type { | 
					
						
							|  |  |  |         Generic, | 
					
						
							|  |  |  |         Image, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     static NonnullRefPtr<Resource> create(Badge<ResourceLoader>, Type, const LoadRequest&); | 
					
						
							|  |  |  |     virtual ~Resource(); | 
					
						
							| 
									
										
										
										
											2020-06-01 21:33:23 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-05 23:32:23 +02:00
										 |  |  |     Type type() const { return m_type; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-01 21:33:23 +02:00
										 |  |  |     bool is_loaded() const { return m_loaded; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     bool is_failed() const { return m_failed; } | 
					
						
							|  |  |  |     const String& error() const { return m_error; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-16 08:47:46 +02:00
										 |  |  |     bool has_encoded_data() const { return !m_encoded_data.is_empty(); } | 
					
						
							| 
									
										
										
										
											2020-06-01 21:33:23 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-13 00:33:23 +03:00
										 |  |  |     const AK::URL& url() const { return m_request.url(); } | 
					
						
							| 
									
										
										
										
											2020-06-01 21:33:23 +02:00
										 |  |  |     const ByteBuffer& encoded_data() const { return m_encoded_data; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-06 13:38:08 +02:00
										 |  |  |     const HashMap<String, String, CaseInsensitiveStringTraits>& response_headers() const { return m_response_headers; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-12 15:42:48 +02:00
										 |  |  |     [[nodiscard]] Optional<u32> status_code() const { return m_status_code; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-01 21:33:23 +02:00
										 |  |  |     void register_client(Badge<ResourceClient>, ResourceClient&); | 
					
						
							|  |  |  |     void unregister_client(Badge<ResourceClient>, ResourceClient&); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-12 10:21:00 +02:00
										 |  |  |     bool has_encoding() const { return m_encoding.has_value(); } | 
					
						
							|  |  |  |     const Optional<String>& encoding() const { return m_encoding; } | 
					
						
							| 
									
										
										
										
											2020-06-06 14:02:43 +02:00
										 |  |  |     const String& mime_type() const { return m_mime_type; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-01 22:09:38 +02:00
										 |  |  |     void for_each_client(Function<void(ResourceClient&)>); | 
					
						
							| 
									
										
										
										
											2020-06-01 21:33:23 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-03 15:11:36 +02:00
										 |  |  |     void did_load(Badge<ResourceLoader>, ReadonlyBytes data, const HashMap<String, String, CaseInsensitiveStringTraits>& headers, Optional<u32> status_code); | 
					
						
							|  |  |  |     void did_fail(Badge<ResourceLoader>, const String& error, Optional<u32> status_code); | 
					
						
							| 
									
										
										
										
											2020-06-01 21:33:23 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-02 20:27:26 +02:00
										 |  |  | protected: | 
					
						
							|  |  |  |     explicit Resource(Type, const LoadRequest&); | 
					
						
							| 
									
										
										
										
											2020-06-01 21:33:23 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-02 20:27:26 +02:00
										 |  |  | private: | 
					
						
							| 
									
										
										
										
											2020-06-01 21:58:29 +02:00
										 |  |  |     LoadRequest m_request; | 
					
						
							| 
									
										
										
										
											2020-06-01 21:33:23 +02:00
										 |  |  |     ByteBuffer m_encoded_data; | 
					
						
							| 
									
										
										
										
											2020-06-02 20:27:26 +02:00
										 |  |  |     Type m_type { Type::Generic }; | 
					
						
							| 
									
										
										
										
											2020-06-01 21:33:23 +02:00
										 |  |  |     bool m_loaded { false }; | 
					
						
							|  |  |  |     bool m_failed { false }; | 
					
						
							|  |  |  |     String m_error; | 
					
						
							| 
									
										
										
										
											2021-05-12 10:21:00 +02:00
										 |  |  |     Optional<String> m_encoding; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-06 14:02:43 +02:00
										 |  |  |     String m_mime_type; | 
					
						
							| 
									
										
										
										
											2020-06-01 21:33:23 +02:00
										 |  |  |     HashMap<String, String, CaseInsensitiveStringTraits> m_response_headers; | 
					
						
							| 
									
										
										
										
											2021-04-03 15:11:36 +02:00
										 |  |  |     Optional<u32> m_status_code; | 
					
						
							| 
									
										
										
										
											2020-06-01 21:33:23 +02:00
										 |  |  |     HashTable<ResourceClient*> m_clients; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-01 22:09:38 +02:00
										 |  |  | class ResourceClient : public Weakable<ResourceClient> { | 
					
						
							| 
									
										
										
										
											2020-06-01 21:33:23 +02:00
										 |  |  | public: | 
					
						
							|  |  |  |     virtual ~ResourceClient(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     virtual void resource_did_load() { } | 
					
						
							|  |  |  |     virtual void resource_did_fail() { } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | protected: | 
					
						
							| 
									
										
										
										
											2020-06-05 23:32:23 +02:00
										 |  |  |     virtual Resource::Type client_type() const { return Resource::Type::Generic; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-01 21:33:23 +02:00
										 |  |  |     Resource* resource() { return m_resource; } | 
					
						
							|  |  |  |     const Resource* resource() const { return m_resource; } | 
					
						
							|  |  |  |     void set_resource(Resource*); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							|  |  |  |     RefPtr<Resource> m_resource; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |