Serenity Operating System
at master 131 lines 5.5 kB view raw
1/* 2 * Copyright (c) 2021, Luke Wilde <lukew@serenityos.org> 3 * 4 * SPDX-License-Identifier: BSD-2-Clause 5 */ 6 7#include <LibWeb/Bindings/Intrinsics.h> 8#include <LibWeb/DOM/Document.h> 9#include <LibWeb/HTML/History.h> 10 11namespace Web::HTML { 12 13WebIDL::ExceptionOr<JS::NonnullGCPtr<History>> History::create(JS::Realm& realm, DOM::Document& document) 14{ 15 return MUST_OR_THROW_OOM(realm.heap().allocate<History>(realm, realm, document)); 16} 17 18History::History(JS::Realm& realm, DOM::Document& document) 19 : PlatformObject(realm) 20 , m_associated_document(document) 21{ 22} 23 24History::~History() = default; 25 26JS::ThrowCompletionOr<void> History::initialize(JS::Realm& realm) 27{ 28 MUST_OR_THROW_OOM(Base::initialize(realm)); 29 set_prototype(&Bindings::ensure_web_prototype<Bindings::HistoryPrototype>(realm, "History")); 30 31 return {}; 32} 33 34void History::visit_edges(Cell::Visitor& visitor) 35{ 36 Base::visit_edges(visitor); 37 visitor.visit(m_associated_document.ptr()); 38} 39 40// https://html.spec.whatwg.org/multipage/history.html#dom-history-pushstate 41WebIDL::ExceptionOr<void> History::push_state(JS::Value data, DeprecatedString const&, DeprecatedString const& url) 42{ 43 // NOTE: The second parameter of this function is intentionally unused. 44 return shared_history_push_replace_state(data, url, IsPush::Yes); 45} 46 47// https://html.spec.whatwg.org/multipage/history.html#dom-history-replacestate 48WebIDL::ExceptionOr<void> History::replace_state(JS::Value data, DeprecatedString const&, DeprecatedString const& url) 49{ 50 // NOTE: The second parameter of this function is intentionally unused. 51 return shared_history_push_replace_state(data, url, IsPush::No); 52} 53 54// https://html.spec.whatwg.org/multipage/history.html#dom-history-length 55WebIDL::ExceptionOr<u64> History::length() const 56{ 57 // 1. If this's associated Document is not fully active, then throw a "SecurityError" DOMException. 58 if (!m_associated_document->is_fully_active()) 59 return WebIDL::SecurityError::create(realm(), "Cannot perform length on a document that isn't fully active."sv); 60 61 // 2. Return the number of entries in the top-level browsing context's joint session history. 62 auto const* browsing_context = m_associated_document->browsing_context(); 63 64 // FIXME: We don't have the concept of "joint session history", this is an ad-hoc implementation. 65 // See: https://html.spec.whatwg.org/multipage/history.html#joint-session-history 66 return browsing_context->session_history().size(); 67} 68 69// https://html.spec.whatwg.org/multipage/history.html#dom-history-go 70WebIDL::ExceptionOr<void> History::go(long delta = 0) 71{ 72 // 1. Let document be this's associated Document. 73 74 // 2. If document is not fully active, then throw a "SecurityError" DOMException. 75 if (!m_associated_document->is_fully_active()) 76 return WebIDL::SecurityError::create(realm(), "Cannot perform go on a document that isn't fully active."sv); 77 78 // 3. If delta is 0, then act as if the location.reload() method was called, and return. 79 auto* browsing_context = m_associated_document->browsing_context(); 80 auto current_entry_index = browsing_context->session_history_index(); 81 auto next_entry_index = current_entry_index + delta; 82 auto const& sessions = browsing_context->session_history(); 83 if (next_entry_index < sessions.size()) { 84 auto const& next_entry = sessions.at(next_entry_index); 85 // FIXME: 4. Traverse the history by a delta with delta and document's browsing context. 86 browsing_context->loader().load(next_entry.url, FrameLoader::Type::Reload); 87 } 88 89 return {}; 90} 91 92// https://html.spec.whatwg.org/multipage/history.html#dom-history-back 93WebIDL::ExceptionOr<void> History::back() 94{ 95 // 1. Let document be this's associated Document. 96 // 2. If document is not fully active, then throw a "SecurityError" DOMException. 97 // NOTE: We already did this check in `go` method, so skip the fully active check here. 98 99 // 3. Traverse the history by a delta with −1 and document's browsing context. 100 return go(-1); 101} 102 103// https://html.spec.whatwg.org/multipage/history.html#dom-history-forward 104WebIDL::ExceptionOr<void> History::forward() 105{ 106 // 1. Let document be this's associated Document. 107 // 2. If document is not fully active, then throw a "SecurityError" DOMException. 108 // NOTE: We already did this check in `go` method, so skip the fully active check here. 109 110 // 3. Traverse the history by a delta with +1 and document's browsing context. 111 return go(1); 112} 113 114// https://html.spec.whatwg.org/multipage/history.html#shared-history-push/replace-state-steps 115WebIDL::ExceptionOr<void> History::shared_history_push_replace_state(JS::Value, DeprecatedString const&, IsPush) 116{ 117 // 1. Let document be history's associated Document. (NOTE: Not necessary) 118 119 // 2. If document is not fully active, then throw a "SecurityError" DOMException. 120 if (!m_associated_document->is_fully_active()) 121 return WebIDL::SecurityError::create(realm(), "Cannot perform pushState or replaceState on a document that isn't fully active."sv); 122 123 // 3. Optionally, return. (For example, the user agent might disallow calls to these methods that are invoked on a timer, 124 // or from event listeners that are not triggered in response to a clear user action, or that are invoked in rapid succession.) 125 dbgln("FIXME: Implement shared_history_push_replace_state."); 126 return {}; 127 128 // FIXME: Add the rest of the spec steps once they're added. 129} 130 131}