Skip to content

Truly independent web browser

6.0 KB
Raw Blame
/*
 * Copyright (c) 2023, Andrew Kaster <[email protected]>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <LibGC/Heap.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/HTML/DocumentState.h>
#include <LibWeb/HTML/LocalNavigable.h>
#include <LibWeb/HTML/Navigation.h>
#include <LibWeb/HTML/NavigationHistoryEntry.h>
#include <LibWeb/HTML/SessionHistoryEntry.h>
#include <LibWeb/HTML/StructuredSerialize.h>
#include <LibWeb/HTML/Window.h>
#include <LibWeb/Infra/SerializedURL.h>

namespace Web::HTML {

GC_DEFINE_ALLOCATOR(NavigationHistoryEntry);

GC::Ref<NavigationHistoryEntry> NavigationHistoryEntry::create(Window& window, NonnullRefPtr<SessionHistoryEntry> she)
{
    return GC::Heap::the().allocate<NavigationHistoryEntry>(window, she);
}

NavigationHistoryEntry::NavigationHistoryEntry(GC::Ref<Window> window, NonnullRefPtr<SessionHistoryEntry> she)
    : DOM::EventTarget()
    , m_session_history_entry(she)
    , m_window(window)
{
}

NavigationHistoryEntry::~NavigationHistoryEntry() = default;

void NavigationHistoryEntry::visit_edges(Cell::Visitor& visitor)
{
    Base::visit_edges(visitor);
    visitor.visit(m_window);
}

Window& NavigationHistoryEntry::window() const
{
    return m_window;
}

// https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigationhistoryentry-url
Optional<Utf16String> NavigationHistoryEntry::url() const
{
    // The url getter steps are:
    // 1. Let document be this's relevant global object's associated Document.
    auto& document = window().associated_document();

    // 2. If document is not fully active, then return the empty string.
    if (!document.is_fully_active())
        return Utf16String {};

    // 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_id() != document.unique_id())
        && (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 utf16_string_from_url_ascii(she->url().serialize());
}

// https://html.spec.whatwg.org/multipage/nav-history-apis.html#concept-navigationhistoryentry-key
Utf16String 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 = window().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
Utf16String 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 = window().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 = window();
    if (!this_relevant_global_object.associated_document().is_fully_active())
        return -1;

    // 2. Return the result of getting the navigation API entry index of this's session history entry
    // within this's relevant global object's navigation API.
    return this_relevant_global_object.navigation()->get_the_navigation_api_entry_index(*m_session_history_entry);
}

// 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 = window().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_id() == document.unique_id();
}

bool NavigationHistoryEntry::associated_document_is_fully_active() const
{
    return window().associated_document().is_fully_active();
}

// https://html.spec.whatwg.org/multipage/nav-history-apis.html#dom-navigationhistoryentry-getstate
WebIDL::ExceptionOr<JS::Value> NavigationHistoryEntry::get_state(JS::Realm& realm)
{
    // 1. If this's relevant global object's associated Document is not fully active, then return undefined.
    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.
    return HTML::structured_deserialize(realm.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);
}

}