Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-engine

Source of release 0.6.0

0.6.0 / src / web / html / close_watcher.lucb

#==============================================================================================
#
#   close_watcher - HTML::CloseWatcher: what the dialog and popover steps establish
#
#   DESCRIPTION:
#       Port of Libraries/LibWeb/HTML/CloseWatcher.cpp (lines 1-220) as far as phase 1 needs:
#       establishing a close watcher (a non-modal <dialog> and a popover do), requesting to
#       close it (its cancel and close actions are event listeners the caller adds), closing
#       and destroying it. The functions only the bindings reach (the CloseWatcher constructor,
#       requestClose() and the oncancel/onclose attributes) wait for scripting (P3).
#
#       Copyright (c) 2024, the Ladybird developers.
#       Copyright (c) 2024, Felipe Muñoz Mazur <felipe.munoz.mazur@protonmail.com>
#       SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================

## The Window a close watcher belongs to: `as<HTML::Window>(realm().global_object())`.
func html_close_watcher_window(this: HtmlCloseWatcher*) -> HtmlWindow*:
    let realm = bind_platform_object_realm(this.bind_platform_object())
    return as_html_window((gc.Cell*)realm_global_object(realm))

## Ported from Libraries/LibWeb/HTML/CloseWatcher.cpp:26 Web::HTML::CloseWatcher::establish.
# https://html.spec.whatwg.org/multipage/interaction.html#establish-a-close-watcher
pub func html_close_watcher_establish(window: HtmlWindow*, get_enabled_state: gc.Function0[bool]*) -> HtmlCloseWatcher*:
    # 1. Assert: window's associated Document is fully active.
    assert(dom_document_is_fully_active(html_window_associated_document(window)))

    # 2. Let closeWatcher be a new close watcher, with
    #    window: window
    #    cancel action: cancelAction
    #    close action: closeAction
    #    is running cancel action: false
    #    get enabled state: getEnabledState
    let realm = bind_platform_object_realm(window.bind_platform_object())
    let close_watcher = realm_create_html_close_watcher(realm, realm, get_enabled_state)
    # FIXME: cancelAction and closeAction are both set by the caller currently.

    # 3. Let manager be window's associated close watcher manager
    let manager = html_window_close_watcher_manager(window)

    # 4 - 6. Moved to CloseWatcherManager::add
    html_close_watcher_manager_add(manager, close_watcher)

    # 7. Return close_watcher.
    return close_watcher

## Ported from Libraries/LibWeb/HTML/CloseWatcher.cpp:51 Web::HTML::CloseWatcher::construct_impl.
## The CloseWatcher constructor: only script calls it.
# https://html.spec.whatwg.org/multipage/interaction.html#dom-closewatcher
pub func html_close_watcher_construct_impl(realm: Realm*, options: const HtmlCloseWatcherOptions*) -> HtmlCloseWatcher*!:
    trap("unported (P3): Web::HTML::CloseWatcher::construct_impl (new CloseWatcher() from script)")

## Ported from Libraries/LibWeb/HTML/CloseWatcher.cpp:87 Web::HTML::CloseWatcher::CloseWatcher.
pub func html_close_watcher_construct(this: HtmlCloseWatcher*, realm: Realm*, get_enabled_state: gc.Function0[bool]*):
    dom_event_target_construct(this.dom_event_target(), realm)
    html_close_watcher_init_fields(this)
    this.m_get_enabled_state = get_enabled_state

## Ported from Libraries/LibWeb/HTML/CloseWatcher.cpp:94 Web::HTML::CloseWatcher::request_close_for_bindings.
## requestClose(): only script calls it.
# https://html.spec.whatwg.org/multipage/interaction.html#dom-closewatcher-requestclose
pub func html_close_watcher_request_close_for_bindings(this: HtmlCloseWatcher*):
    trap("unported (P3): Web::HTML::CloseWatcher::request_close_for_bindings (requestClose() from script)")

## Ported from Libraries/LibWeb/HTML/CloseWatcher.cpp:101 Web::HTML::CloseWatcher::request_close.
# https://html.spec.whatwg.org/multipage/interaction.html#close-watcher-request-close
pub func html_close_watcher_request_close(this: HtmlCloseWatcher*, require_history_action_activation: bool) -> bool:
    # 1. If closeWatcher is not active, then return true.
    if not this.m_is_active:
        return true

    # 2. If the result of running closeWatcher's get enabled state is false, then return true.
    if not html_close_watcher_get_enabled_state(this):
        return true

    # 3. If closeWatcher's is running cancel action is true, then return true.
    if this.m_is_running_cancel_action:
        return true

    # 4. Let window be closeWatcher's window.
    let window = html_close_watcher_window(this)

    # 5. If window's associated Document is not fully active, then return true.
    if not dom_document_is_fully_active(html_window_associated_document(window)):
        return true

    # 6. Let canPreventClose be true if requireHistoryActionActivation is false, or if window's close watcher manager's groups's size is less than window's close watcher manager's allowed number of groups,
    # and window has history-action activation; otherwise false.
    let manager = html_window_close_watcher_manager(window)
    let can_prevent_close = not require_history_action_activation or (html_close_watcher_manager_can_prevent_close(manager) and html_window_has_history_action_activation(window))
    # 7. Set closeWatcher's is running cancel action to true.
    this.m_is_running_cancel_action = true
    # 8. Let shouldContinue be the result of running closeWatcher's cancel action given canPreventClose.
    let realm = bind_platform_object_realm(this.bind_platform_object())
    let should_continue = dom_event_target_dispatch_event(this.dom_event_target(), dom_event_create(realm, html_event_names_cancel, DomEventInit(cancelable = can_prevent_close)))
    # 9. Set closeWatcher's is running cancel action to false.
    this.m_is_running_cancel_action = false
    # 10. If shouldContinue is false, then:
    if not should_continue:
        # 10.1 Assert: canPreventClose is true.
        assert(can_prevent_close)
        # 10.2 Consume history-action user activation given window.
        html_window_consume_history_action_user_activation(window)
        # 10.3 Return false.
        return false

    # 11. Close closeWatcher.
    html_close_watcher_close(this)

    # 12. Return true.
    return true

## Ported from Libraries/LibWeb/HTML/CloseWatcher.cpp:149 Web::HTML::CloseWatcher::get_enabled_state.
pub func html_close_watcher_get_enabled_state(this: HtmlCloseWatcher*) -> bool:
    return ak.function0_call(&this.m_get_enabled_state.function)

## Ported from Libraries/LibWeb/HTML/CloseWatcher.cpp:155 Web::HTML::CloseWatcher::close.
# https://html.spec.whatwg.org/multipage/interaction.html#close-watcher-close
pub func html_close_watcher_close(this: HtmlCloseWatcher*):
    # 1. If closeWatcher is not active, then return.
    if not this.m_is_active:
        return

    # 2. If the result of running closeWatcher's get enabled state is false, then return.
    if not html_close_watcher_get_enabled_state(this):
        return

    # 3. If closeWatcher's window's associated Document is not fully active, then return.
    if not dom_document_is_fully_active(html_window_associated_document(html_close_watcher_window(this))):
        return

    # 4. Destroy closeWatcher.
    html_close_watcher_destroy(this)

    # 5. Run closeWatcher's close action.
    let realm = bind_platform_object_realm(this.bind_platform_object())
    _ = dom_event_target_dispatch_event(this.dom_event_target(), dom_event_create(realm, html_event_names_close))

## Ported from Libraries/LibWeb/HTML/CloseWatcher.cpp:177 Web::HTML::CloseWatcher::destroy.
# https://html.spec.whatwg.org/multipage/interaction.html#close-watcher-destroy
pub func html_close_watcher_destroy(this: HtmlCloseWatcher*):
    # 1. Let manager be closeWatcher's window's close watcher manager.
    let manager = html_window_close_watcher_manager(html_close_watcher_window(this))

    # 2-3. Moved to CloseWatcherManager::remove
    html_close_watcher_manager_remove(manager, this)

    this.m_is_active = false

## Ported from Libraries/LibWeb/HTML/CloseWatcher.cpp:188 Web::HTML::CloseWatcher::initialize (override).
pub func html_close_watcher_initialize_impl(base: JsCell*, realm: Realm*):
    web_set_prototype_for_interface(base, realm, BindInterfaceName.close_watcher)
    dom_event_target_initialize_impl(base, realm)

## Ported from Libraries/LibWeb/HTML/CloseWatcher.cpp:194 Web::HTML::CloseWatcher::set_oncancel.
pub func html_close_watcher_set_oncancel(this: HtmlCloseWatcher*, event_handler: IdlCallbackType*?):
    trap("unported (P3): Web::HTML::CloseWatcher::set_oncancel (the oncancel attribute from script)")

## Ported from Libraries/LibWeb/HTML/CloseWatcher.cpp:199 Web::HTML::CloseWatcher::oncancel.
pub func html_close_watcher_oncancel(this: HtmlCloseWatcher*) -> IdlCallbackType*?:
    trap("unported (P3): Web::HTML::CloseWatcher::oncancel (the oncancel attribute from script)")

## Ported from Libraries/LibWeb/HTML/CloseWatcher.cpp:204 Web::HTML::CloseWatcher::set_onclose.
pub func html_close_watcher_set_onclose(this: HtmlCloseWatcher*, event_handler: IdlCallbackType*?):
    trap("unported (P3): Web::HTML::CloseWatcher::set_onclose (the onclose attribute from script)")

## Ported from Libraries/LibWeb/HTML/CloseWatcher.cpp:209 Web::HTML::CloseWatcher::onclose.
pub func html_close_watcher_onclose(this: HtmlCloseWatcher*) -> IdlCallbackType*?:
    trap("unported (P3): Web::HTML::CloseWatcher::onclose (the onclose attribute from script)")

## Ported from Libraries/LibWeb/HTML/CloseWatcher.cpp:214 Web::HTML::CloseWatcher::visit_edges (override).
pub func html_close_watcher_visit_edges_impl(base: gc.Cell*, visitor: gc.CellVisitor*):
    let this = (HtmlCloseWatcher*)base
    dom_event_target_visit_edges_impl(base, visitor)
    gc.cell_visitor_visit_t_2(visitor, this.m_get_enabled_state)