| 
									
										
										
										
											2023-08-23 10:47:05 -06:00
										 |  |  |  | /*
 | 
					
						
							|  |  |  |  |  * Copyright (c) 2023, Andrew Kaster <akaster@serenityos.org> | 
					
						
							|  |  |  |  |  * | 
					
						
							|  |  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  |  */ | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | #include <LibJS/Heap/Heap.h>
 | 
					
						
							|  |  |  |  | #include <LibJS/Runtime/Realm.h>
 | 
					
						
							|  |  |  |  | #include <LibWeb/Bindings/Intrinsics.h>
 | 
					
						
							|  |  |  |  | #include <LibWeb/Bindings/NavigationHistoryEntryPrototype.h>
 | 
					
						
							|  |  |  |  | #include <LibWeb/DOM/Document.h>
 | 
					
						
							|  |  |  |  | #include <LibWeb/HTML/DocumentState.h>
 | 
					
						
							| 
									
										
										
										
											2023-08-23 11:08:05 -06:00
										 |  |  |  | #include <LibWeb/HTML/Navigation.h>
 | 
					
						
							| 
									
										
										
										
											2023-08-23 10:47:05 -06:00
										 |  |  |  | #include <LibWeb/HTML/NavigationHistoryEntry.h>
 | 
					
						
							|  |  |  |  | #include <LibWeb/HTML/Window.h>
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | namespace Web::HTML { | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-19 19:47:52 +01:00
										 |  |  |  | JS_DEFINE_ALLOCATOR(NavigationHistoryEntry); | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-23 10:47:05 -06:00
										 |  |  |  | JS::NonnullGCPtr<NavigationHistoryEntry> NavigationHistoryEntry::create(JS::Realm& realm, JS::NonnullGCPtr<SessionHistoryEntry> she) | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     return realm.heap().allocate<NavigationHistoryEntry>(realm, realm, she); | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | NavigationHistoryEntry::NavigationHistoryEntry(JS::Realm& realm, JS::NonnullGCPtr<SessionHistoryEntry> she) | 
					
						
							|  |  |  |  |     : DOM::EventTarget(realm) | 
					
						
							|  |  |  |  |     , m_session_history_entry(she) | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | NavigationHistoryEntry::~NavigationHistoryEntry() = default; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | void NavigationHistoryEntry::initialize(JS::Realm& realm) | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     Base::initialize(realm); | 
					
						
							| 
									
										
										
										
											2023-11-22 12:55:21 +13:00
										 |  |  |  |     set_prototype(&Bindings::ensure_web_prototype<Bindings::NavigationHistoryEntryPrototype>(realm, "NavigationHistoryEntry"_fly_string)); | 
					
						
							| 
									
										
										
										
											2023-08-23 10:47:05 -06:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | void NavigationHistoryEntry::visit_edges(JS::Cell::Visitor& visitor) | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     Base::visit_edges(visitor); | 
					
						
							|  |  |  |  |     visitor.visit(m_session_history_entry); | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigationhistoryentry-url
 | 
					
						
							|  |  |  |  | WebIDL::ExceptionOr<Optional<String>> NavigationHistoryEntry::url() const | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     // The url getter steps are:
 | 
					
						
							|  |  |  |  |     // 1. Let document be this's relevant global object's associated Document.
 | 
					
						
							|  |  |  |  |     auto& document = verify_cast<HTML::Window>(relevant_global_object(*this)).associated_document(); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 2. If document is not fully active, then return the empty string.
 | 
					
						
							|  |  |  |  |     if (!document.is_fully_active()) | 
					
						
							|  |  |  |  |         return String {}; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 3. Let she be this's session history entry.
 | 
					
						
							|  |  |  |  |     auto const& she = this->m_session_history_entry; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 4. If she's document does not equal document, and she's document state's request referrer policy
 | 
					
						
							|  |  |  |  |     //    is "no-referrer" or "origin", then return null.
 | 
					
						
							|  |  |  |  |     if ((she->document_state->document() != &document) | 
					
						
							|  |  |  |  |         && (she->document_state->request_referrer_policy() == ReferrerPolicy::ReferrerPolicy::NoReferrer | 
					
						
							|  |  |  |  |             || she->document_state->request_referrer_policy() == ReferrerPolicy::ReferrerPolicy::Origin)) | 
					
						
							|  |  |  |  |         return OptionalNone {}; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 5. Return she's URL, serialized.
 | 
					
						
							|  |  |  |  |     return TRY_OR_THROW_OOM(vm(), String::from_deprecated_string(she->url.serialize())); | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | // https://html.spec.whatwg.org/multipage/nav-history-apis.html#concept-navigationhistoryentry-key
 | 
					
						
							|  |  |  |  | String NavigationHistoryEntry::key() const | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     // The key of a NavigationHistoryEntry nhe is given by the return value of the following algorithm:
 | 
					
						
							|  |  |  |  |     // 1. If nhe's relevant global object's associated Document is not fully active, then return the empty string.
 | 
					
						
							|  |  |  |  |     auto& associated_document = verify_cast<HTML::Window>(relevant_global_object(*this)).associated_document(); | 
					
						
							|  |  |  |  |     if (!associated_document.is_fully_active()) | 
					
						
							|  |  |  |  |         return {}; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 2. Return nhe's session history entry's navigation API key.
 | 
					
						
							|  |  |  |  |     return m_session_history_entry->navigation_api_key; | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | // https://html.spec.whatwg.org/multipage/nav-history-apis.html#concept-navigationhistoryentry-id
 | 
					
						
							|  |  |  |  | String NavigationHistoryEntry::id() const | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     // The ID of a NavigationHistoryEntry nhe is given by the return value of the following algorithm:
 | 
					
						
							|  |  |  |  |     // 1. If nhe's relevant global object's associated Document is not fully active, then return the empty string.
 | 
					
						
							|  |  |  |  |     auto& associated_document = verify_cast<HTML::Window>(relevant_global_object(*this)).associated_document(); | 
					
						
							|  |  |  |  |     if (!associated_document.is_fully_active()) | 
					
						
							|  |  |  |  |         return {}; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 2. Return nhe's session history entry's navigation API ID.
 | 
					
						
							|  |  |  |  |     return m_session_history_entry->navigation_api_id; | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | // https://html.spec.whatwg.org/multipage/nav-history-apis.html#concept-navigationhistoryentry-index
 | 
					
						
							|  |  |  |  | i64 NavigationHistoryEntry::index() const | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     // The index of a NavigationHistoryEntry nhe is given by the return value of the following algorithm:
 | 
					
						
							|  |  |  |  |     // 1. If nhe's relevant global object's associated Document is not fully active, then return −1.
 | 
					
						
							|  |  |  |  |     auto& this_relevant_global_object = verify_cast<HTML::Window>(relevant_global_object(*this)); | 
					
						
							|  |  |  |  |     if (!this_relevant_global_object.associated_document().is_fully_active()) | 
					
						
							|  |  |  |  |         return -1; | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-23 11:08:05 -06:00
										 |  |  |  |     // 2. Return the result of getting the navigation API entry index of this's session history entry
 | 
					
						
							| 
									
										
										
										
											2023-08-23 10:47:05 -06:00
										 |  |  |  |     // within this's relevant global object's navigation API.
 | 
					
						
							| 
									
										
										
										
											2023-08-23 11:08:05 -06:00
										 |  |  |  |     return this_relevant_global_object.navigation()->get_the_navigation_api_entry_index(*m_session_history_entry); | 
					
						
							| 
									
										
										
										
											2023-08-23 10:47:05 -06:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigationhistoryentry-samedocument
 | 
					
						
							|  |  |  |  | bool NavigationHistoryEntry::same_document() const | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     // The sameDocument getter steps are:
 | 
					
						
							|  |  |  |  |     // 1. Let document be this's relevant global object's associated Document.
 | 
					
						
							|  |  |  |  |     auto& document = verify_cast<HTML::Window>(relevant_global_object(*this)).associated_document(); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 2. If document is not fully active, then return false.
 | 
					
						
							|  |  |  |  |     if (!document.is_fully_active()) | 
					
						
							|  |  |  |  |         return false; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 3. Return true if this's session history entry's document equals document, and false otherwise.
 | 
					
						
							|  |  |  |  |     return m_session_history_entry->document_state->document() == &document; | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | // https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigationhistoryentry-getstate
 | 
					
						
							|  |  |  |  | WebIDL::ExceptionOr<JS::Value> NavigationHistoryEntry::get_state() | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     // The getState() method steps are:
 | 
					
						
							|  |  |  |  |     // 1. If this's relevant global object's associated Document is not fully active, then return undefined.
 | 
					
						
							|  |  |  |  |     auto& associated_document = verify_cast<HTML::Window>(relevant_global_object(*this)).associated_document(); | 
					
						
							|  |  |  |  |     if (!associated_document.is_fully_active()) | 
					
						
							|  |  |  |  |         return JS::js_undefined(); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 2. Return StructuredDeserialize(this's session history entry's navigation API state). Rethrow any exceptions.
 | 
					
						
							|  |  |  |  |     //    NOTE: This can in theory throw an exception, if attempting to deserialize a large ArrayBuffer
 | 
					
						
							|  |  |  |  |     //          when not enough memory is available.
 | 
					
						
							|  |  |  |  |     return structured_deserialize(vm(), m_session_history_entry->navigation_api_state, realm(), {}); | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | void NavigationHistoryEntry::set_ondispose(WebIDL::CallbackType* event_handler) | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     set_event_handler_attribute(HTML::EventNames::dispose, event_handler); | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | WebIDL::CallbackType* NavigationHistoryEntry::ondispose() | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  |     return event_handler_attribute(HTML::EventNames::dispose); | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | } |