| 
									
										
										
										
											2022-10-03 20:58:36 +02:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2023-10-25 12:39:01 +02:00
										 |  |  |  * Copyright (c) 2022-2023, networkException <networkexception@serenityos.org> | 
					
						
							| 
									
										
										
										
											2022-10-03 20:58:36 +02:00
										 |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <LibWeb/HTML/Scripting/ModuleMap.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace Web::HTML { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  | GC_DEFINE_ALLOCATOR(ModuleMap); | 
					
						
							| 
									
										
										
										
											2023-11-19 19:47:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-04 09:49:56 +02:00
										 |  |  | void ModuleMap::visit_edges(Visitor& visitor) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Base::visit_edges(visitor); | 
					
						
							| 
									
										
										
										
											2023-10-29 01:46:02 +02:00
										 |  |  |     for (auto& it : m_values) | 
					
						
							| 
									
										
										
										
											2023-05-04 09:49:56 +02:00
										 |  |  |         visitor.visit(it.value.module_script); | 
					
						
							| 
									
										
										
										
											2023-10-29 01:46:02 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     for (auto const& it : m_callbacks) | 
					
						
							| 
									
										
										
										
											2024-04-15 13:58:21 +02:00
										 |  |  |         visitor.visit(it.value); | 
					
						
							| 
									
										
										
										
											2023-05-04 09:49:56 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-18 16:22:27 +13:00
										 |  |  | bool ModuleMap::is_fetching(URL::URL const& url, ByteString const& type) const | 
					
						
							| 
									
										
										
										
											2022-10-03 20:58:36 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     return is(url, type, EntryType::Fetching); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-18 16:22:27 +13:00
										 |  |  | bool ModuleMap::is_failed(URL::URL const& url, ByteString const& type) const | 
					
						
							| 
									
										
										
										
											2022-10-03 20:58:36 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     return is(url, type, EntryType::Failed); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-18 16:22:27 +13:00
										 |  |  | bool ModuleMap::is(URL::URL const& url, ByteString const& type, EntryType entry_type) const | 
					
						
							| 
									
										
										
										
											2022-10-03 20:58:36 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     auto value = m_values.get({ url, type }); | 
					
						
							|  |  |  |     if (!value.has_value()) | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return value->type == entry_type; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-18 16:22:27 +13:00
										 |  |  | Optional<ModuleMap::Entry> ModuleMap::get(URL::URL const& url, ByteString const& type) const | 
					
						
							| 
									
										
										
										
											2022-10-03 20:58:36 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     return m_values.get({ url, type }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-18 16:22:27 +13:00
										 |  |  | AK::HashSetResult ModuleMap::set(URL::URL const& url, ByteString const& type, Entry entry) | 
					
						
							| 
									
										
										
										
											2022-10-03 20:58:36 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-12-16 18:32:01 +01:00
										 |  |  |     // NOTE: Re-entering this function while firing wait_for_change callbacks is not allowed.
 | 
					
						
							|  |  |  |     VERIFY(!m_firing_callbacks); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-25 12:39:01 +02:00
										 |  |  |     auto value = m_values.set({ url, type }, entry); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-03 20:58:36 +02:00
										 |  |  |     auto callbacks = m_callbacks.get({ url, type }); | 
					
						
							| 
									
										
										
										
											2023-12-16 18:32:01 +01:00
										 |  |  |     if (callbacks.has_value()) { | 
					
						
							|  |  |  |         m_firing_callbacks = true; | 
					
						
							| 
									
										
										
										
											2022-10-03 20:58:36 +02:00
										 |  |  |         for (auto const& callback : *callbacks) | 
					
						
							| 
									
										
										
										
											2023-10-29 01:46:02 +02:00
										 |  |  |             callback->function()(entry); | 
					
						
							| 
									
										
										
										
											2023-12-16 18:32:01 +01:00
										 |  |  |         m_firing_callbacks = false; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-10-03 20:58:36 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-10-25 12:39:01 +02:00
										 |  |  |     return value; | 
					
						
							| 
									
										
										
										
											2022-10-03 20:58:36 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  | void ModuleMap::wait_for_change(GC::Heap& heap, URL::URL const& url, ByteString const& type, Function<void(Entry)> callback) | 
					
						
							| 
									
										
										
										
											2022-10-03 20:58:36 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |     m_callbacks.ensure({ url, type }).append(GC::create_function(heap, move(callback))); | 
					
						
							| 
									
										
										
										
											2022-10-03 20:58:36 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |