dymokomi/luce-browser-engine
Source of release 0.6.0
0.6.0 / src / web / page / page_2.lucb
#==============================================================================================
#
# page_2 - Web::Page: non-blocking dialogs, clipboard requests, the registered media and
# canvas elements, the media context menu, the user style sheet, selection, find in page and
# the pending fullscreen operations
#
# DESCRIPTION:
# Port of Libraries/LibWeb/Page/Page.cpp lines 451-1074 (did_request_color_picker through
# set_viewport_is_fullscreen) with the remaining inline accessors of Page.h. The IPC
# encode/decode of MediaContextMenu (lines 1076-1102) belongs to the IPC layer, which
# phase 1 does not have.
#
# Copyright (c) 2020-2022, Andreas Kling <andreas@ladybird.org>
# SPDX-License-Identifier: BSD-2-Clause
#
#==============================================================================================
# mark: Non-blocking dialogs ====================================================================
## Ported from Page.cpp:451 Web::Page::did_request_color_picker.
pub func page_did_request_color_picker(this: Page*, target: gc.Weak[HtmlInputElement], current_color: gfx.Color):
if this.m_pending_non_blocking_dialog == PagePendingNonBlockingDialog.none_:
this.m_pending_non_blocking_dialog = PagePendingNonBlockingDialog.color_picker
this.m_pending_non_blocking_dialog_target = gc.make_weak((HtmlElement*?)gc.weak_ptr(&target))
page_client_page_did_request_color_picker(this.m_client, current_color)
## Ported from Page.cpp:461 Web::Page::color_picker_update.
pub func page_color_picker_update(this: Page*, picked_color: gfx.Color?, state: HtmlColorPickerUpdateState):
if this.m_pending_non_blocking_dialog == PagePendingNonBlockingDialog.color_picker:
if state == HtmlColorPickerUpdateState.closed:
this.m_pending_non_blocking_dialog = PagePendingNonBlockingDialog.none_
if let target = gc.weak_ptr(&this.m_pending_non_blocking_dialog_target):
let input_element = as_html_input_element(target)
html_input_element_did_pick_color(input_element, picked_color, state)
if state == HtmlColorPickerUpdateState.closed:
this.m_pending_non_blocking_dialog_target = gc.make_weak[HtmlElement](none)
## Ported from Page.cpp:476 Web::Page::did_request_file_picker.
pub func page_did_request_file_picker(this: Page*, target: gc.Weak[HtmlInputElement], accepted_file_types: HtmlFileFilter, allow_multiple_files: HtmlAllowMultipleFiles):
if this.m_pending_non_blocking_dialog == PagePendingNonBlockingDialog.none_:
this.m_pending_non_blocking_dialog = PagePendingNonBlockingDialog.file_picker
this.m_pending_non_blocking_dialog_target = gc.make_weak((HtmlElement*?)gc.weak_ptr(&target))
page_client_page_did_request_file_picker(this.m_client, accepted_file_types, allow_multiple_files)
## Ported from Page.cpp:486 Web::Page::file_picker_closed.
pub func page_file_picker_closed(this: Page*, selected_files: HtmlSelectedFile[]):
if this.m_pending_non_blocking_dialog == PagePendingNonBlockingDialog.file_picker:
this.m_pending_non_blocking_dialog = PagePendingNonBlockingDialog.none_
if let target = gc.weak_ptr(&this.m_pending_non_blocking_dialog_target):
let input_element = as_html_input_element(target)
html_input_element_did_select_files(input_element, selected_files)
this.m_pending_non_blocking_dialog_target = gc.make_weak[HtmlElement](none)
## Ported from Page.cpp:500 Web::Page::did_request_select_dropdown.
pub func page_did_request_select_dropdown(this: Page*, target: gc.Weak[HtmlSelectElement], content_position: gfx.CssPixelPoint, minimum_width: gfx.CssPixels, items: ak.Vector[HtmlSelectItem]):
if this.m_pending_non_blocking_dialog == PagePendingNonBlockingDialog.none_:
this.m_pending_non_blocking_dialog = PagePendingNonBlockingDialog.select
this.m_pending_non_blocking_dialog_target = gc.make_weak((HtmlElement*?)gc.weak_ptr(&target))
page_client_page_did_request_select_dropdown(this.m_client, content_position, minimum_width, items)
## Ported from Page.cpp:509 Web::Page::select_dropdown_closed.
pub func page_select_dropdown_closed(this: Page*, selected_item_id: u32?):
if this.m_pending_non_blocking_dialog == PagePendingNonBlockingDialog.select:
this.m_pending_non_blocking_dialog = PagePendingNonBlockingDialog.none_
if let target = gc.weak_ptr(&this.m_pending_non_blocking_dialog_target):
let select_element = as_html_select_element(target)
html_select_element_did_select_item(select_element, selected_item_id)
this.m_pending_non_blocking_dialog_target = gc.make_weak[HtmlElement](none)
# mark: Clipboard requests ======================================================================
## Ported from Page.cpp:522 Web::Page::request_clipboard_entries.
pub func page_request_clipboard_entries(this: Page*, request: gc.Function1[ak.Vector[ClipboardSystemClipboardItem], unit]*):
let request_id = this.m_next_clipboard_request_id
this.m_next_clipboard_request_id += 1
_ = ak.hash_map_set(&this.m_pending_clipboard_requests, request_id, request)
page_client_page_did_request_clipboard_entries(page_client(this), request_id)
## Ported from Page.cpp:530 Web::Page::retrieved_clipboard_entries.
pub func page_retrieved_clipboard_entries(this: Page*, request_id: u64, items: ak.Vector[ClipboardSystemClipboardItem]):
if let request = ak.hash_map_take_k(&this.m_pending_clipboard_requests, request_id):
ak.function1_call(&request.function, items)
# mark: Media and canvas elements ===============================================================
## Ported from Page.cpp:536 Web::Page::register_media_element.
pub func page_register_media_element(this: Page*, media_id: i64):
ak.vector_append_t(&this.m_media_elements, media_id)
## The remove_all_matching predicate of the unregister functions (`candidate_id == id`).
func page_is_node_id(context: void*?, candidate_id: const i64*) -> bool:
return *candidate_id == *(const i64*)(context else trap("no id"))
## Ported from Page.cpp:541 Web::Page::unregister_media_element.
pub func page_unregister_media_element(this: Page*, media_id: i64):
var id = media_id
_ = ak.vector_remove_all_matching(&this.m_media_elements, ak.Function1[const i64*, bool](call = page_is_node_id, context = &id))
## Ported from Page.cpp:549 Web::Page::for_each_media_element.
func page_for_each_media_element(this: Page*, callback: ak.Function1[HtmlMediaElement*, unit]):
for media_id in ak.vector_span(&this.m_media_elements):
if let node = dom_node_from_unique_id(media_id):
ak.function1_call(&callback, as_html_media_element(node))
## update_all_media_element_video_sinks' callback.
func page_update_media_element_video_sink(context: void*?, media_element: HtmlMediaElement*):
let should_request_another_frame = (bool*)(context else trap("no flag"))
html_media_element_update_video_frame_and_timeline(media_element)
*should_request_another_frame = true
## Ported from Page.cpp:557 Web::Page::update_all_media_element_video_sinks.
pub func page_update_all_media_element_video_sinks(this: Page*):
var should_request_another_frame = false
page_for_each_media_element(this, ak.Function1[HtmlMediaElement*, unit](call = page_update_media_element_video_sink, context = &should_request_another_frame))
if should_request_another_frame:
page_client_request_frame(page_client(this))
## Ported from Page.cpp:569 Web::Page::register_canvas_element.
pub func page_register_canvas_element(this: Page*, canvas_id: i64):
ak.vector_append_t(&this.m_canvas_elements, canvas_id)
## Ported from Page.cpp:574 Web::Page::unregister_canvas_element.
pub func page_unregister_canvas_element(this: Page*, canvas_id: i64):
var id = canvas_id
_ = ak.vector_remove_all_matching(&this.m_canvas_elements, ak.Function1[const i64*, bool](call = page_is_node_id, context = &id))
## Ported from Page.cpp:582 Web::Page::for_each_canvas_element.
func page_for_each_canvas_element(this: Page*, callback: ak.Function1[HtmlCanvasElement*, unit]):
for canvas_id in ak.vector_span(&this.m_canvas_elements):
if let node = dom_node_from_unique_id(canvas_id):
ak.function1_call(&callback, as_html_canvas_element(node))
## present_all_canvas_element_surfaces' callback.
func page_present_canvas_element(context: void*?, canvas_element: HtmlCanvasElement*):
html_canvas_element_present(canvas_element)
## Ported from Page.cpp:590 Web::Page::present_all_canvas_element_surfaces.
pub func page_present_all_canvas_element_surfaces(this: Page*):
page_for_each_canvas_element(this, ak.Function1[HtmlCanvasElement*, unit](call = page_present_canvas_element, context = none))
# mark: The media context menu ==================================================================
## Ported from Page.cpp:597 Web::Page::did_request_media_context_menu.
pub func page_did_request_media_context_menu(this: Page*, media_id: i64, position: gfx.CssPixelPoint, target: ak.ByteString, modifiers: u32, menu: PageMediaContextMenu):
this.m_media_context_menu_element_id = media_id
page_client_page_did_request_media_context_menu(page_client(this), position, target, modifiers, menu)
## Ported from Page.cpp:603 Web::Page::toggle_media_play_state.
pub func page_toggle_media_play_state(this: Page*):
let media_element = page_media_context_menu_element(this) else return
# AD-HOC: An execution context is required for Promise creation hooks.
var execution_context: HtmlTemporaryExecutionContext = ---
html_temporary_execution_context_construct_realm(&execution_context, html_media_element_realm(media_element))
defer html_temporary_execution_context_destruct(&execution_context)
if html_media_element_potentially_playing(media_element):
html_media_element_pause(media_element)
else:
_ = html_media_element_play(media_element)
## Ported from Page.cpp:618 Web::Page::toggle_media_mute_state.
pub func page_toggle_media_mute_state(this: Page*):
let media_element = page_media_context_menu_element(this) else return
# AD-HOC: An execution context is required for Promise creation hooks.
var execution_context: HtmlTemporaryExecutionContext = ---
html_temporary_execution_context_construct_realm(&execution_context, html_media_element_realm(media_element))
defer html_temporary_execution_context_destruct(&execution_context)
html_media_element_set_muted(media_element, not html_media_element_muted(media_element))
## Ported from Page.cpp:630 Web::Page::toggle_media_loop_state.
pub func page_toggle_media_loop_state(this: Page*):
let media_element = page_media_context_menu_element(this) else return
# AD-HOC: An execution context is required for Promise creation hooks.
var execution_context: HtmlTemporaryExecutionContext = ---
html_temporary_execution_context_construct_realm(&execution_context, html_media_element_realm(media_element))
defer html_temporary_execution_context_destruct(&execution_context)
let element = media_element.dom_element()
if dom_element_has_attribute(element, html_attribute_names_loop):
dom_element_remove_attribute(element, html_attribute_names_loop)
else:
dom_element_set_attribute_value(element, html_attribute_names_loop, ak.String())
## Ported from Page.cpp:645 Web::Page::toggle_media_fullscreen_state.
pub func page_toggle_media_fullscreen_state(this: Page*):
let media_element = page_media_context_menu_element(this) else return
var execution_context: HtmlTemporaryExecutionContext = ---
html_temporary_execution_context_construct_realm(&execution_context, html_media_element_realm(media_element))
defer html_temporary_execution_context_destruct(&execution_context)
html_media_element_toggle_fullscreen(media_element)
## Ported from Page.cpp:655 Web::Page::toggle_media_controls_state.
pub func page_toggle_media_controls_state(this: Page*):
let media_element = page_media_context_menu_element(this) else return
var execution_context: HtmlTemporaryExecutionContext = ---
html_temporary_execution_context_construct_realm(&execution_context, html_media_element_realm(media_element))
defer html_temporary_execution_context_destruct(&execution_context)
let element = media_element.dom_element()
if dom_element_has_attribute(element, html_attribute_names_controls):
dom_element_remove_attribute(element, html_attribute_names_controls)
else:
dom_element_set_attribute_value(element, html_attribute_names_controls, ak.String())
## toggle_page_mute_state's callback (`media_element.page_mute_state_changed({})`).
func page_media_element_page_mute_state_changed(context: void*?, media_element: HtmlMediaElement*):
html_media_element_page_mute_state_changed(media_element)
## Ported from Page.cpp:669 Web::Page::toggle_page_mute_state.
pub func page_toggle_page_mute_state(this: Page*):
this.m_mute_state = html_invert_mute_state(this.m_mute_state)
page_for_each_media_element(this, ak.Function1[HtmlMediaElement*, unit](call = page_media_element_page_mute_state_changed, context = none))
## Ported from Page.cpp:678 Web::Page::media_context_menu_element.
func page_media_context_menu_element(this: Page*) -> HtmlMediaElement*?:
let media_id = this.m_media_context_menu_element_id else return none
let dom_node = dom_node_from_unique_id(media_id) else return none
if not is_html_media_element(dom_node):
return none
return (HtmlMediaElement*)dom_node
# mark: The user style sheet and selection ======================================================
## set_user_style's for_each_shadow_root callback.
func page_invalidate_shadow_root_rule_cache(context: void*?, shadow_root: DomShadowRoot*):
css_style_scope_invalidate_rule_cache(dom_shadow_root_style_scope(shadow_root))
## Ported from Page.cpp:693 Web::Page::set_user_style.
pub func page_set_user_style(this: Page*, source: ak.String):
this.m_user_style_sheet_source = source
if page_top_level_traversable_is_initialized(this):
if let document = html_navigable_active_document(page_top_level_traversable(this).html_navigable()):
css_style_scope_invalidate_rule_cache(dom_document_style_scope(document))
dom_document_for_each_shadow_root(document, ak.Function1[DomShadowRoot*, unit](call = page_invalidate_shadow_root_rule_cache, context = none))
## documents_in_active_window's predicate (`document.window() == top_level_traversable()->active_window()`).
func page_document_is_in_active_window(context: void*?, document: DomDocument*) -> bool:
let this = (Page*)(context else trap("no page"))
return dom_document_window(document) == html_navigable_active_window(page_top_level_traversable(this).html_navigable())
## Ported from Page.cpp:705 Web::Page::documents_in_active_window.
func page_documents_in_active_window(this: Page*) -> ak.Vector[DomDocument*?]:
if not page_top_level_traversable_is_initialized(this):
return ak.Vector[DomDocument*?]()
let documents = html_event_loop_documents_in_this_event_loop_matching(html_main_thread_event_loop(), ak.Function1[DomDocument*, bool](call = page_document_is_in_active_window, context = this))
return documents
## Ported from Page.cpp:717 Web::Page::clear_selection.
pub func page_clear_selection(this: Page*):
let documents = page_documents_in_active_window(this)
for document in ak.vector_span_const(&documents):
let selection = dom_document_get_selection(document else trap("no document")) else continue
selection_selection_remove_all_ranges(selection)
# mark: Find in page ============================================================================
## perform_find_in_page_query's active_range lambda.
func page_active_range(document: DomDocument*) -> DomRange*?:
let selection = dom_document_get_selection(document) else return none
if selection_selection_is_collapsed(selection):
return none
return selection_selection_range(selection)
## perform_find_in_page_query's find_current_match_index lambda.
func page_find_current_match_index(this: Page*, range: DomRange*, matches: const ak.Vector[DomRange*?]*) -> usize?:
# Always return the first match if there is no active query.
if this.m_last_find_in_page_query == none:
return 0
for i in 0..<ak.vector_size(matches):
let match_range = *ak.vector_at_const(matches, i) else trap("no range")
let comparison = dom_range_compare_boundary_points(match_range, dom_range_start_to_start, range) catch:
_ = idl_take_pending_exception()
continue
if comparison >= 0:
return i
return none
## Ported from Page.cpp:728 Web::Page::perform_find_in_page_query.
func page_perform_find_in_page_query(this: Page*, query: PageFindInPageQuery, direction: PageSearchDirection? = none) -> PageFindInPageResult:
assert(page_top_level_traversable_is_initialized(this))
var all_matches = ak.Vector[DomRange*?]()
var should_update_match_index = false
let documents = page_documents_in_active_window(this)
for entry in ak.vector_span_const(&documents):
let document = entry else trap("no document")
var matches = dom_document_find_matching_text(document, query.string, query.case_sensitivity)
if (DomDocument*?)document == html_navigable_active_document(page_top_level_traversable(this).html_navigable()):
if let range = page_active_range(document):
let new_match_index = page_find_current_match_index(this, range, &matches)
should_update_match_index = true
this.m_find_in_page_match_index = (new_match_index else 0) + ak.vector_size(&all_matches)
else:
this.m_find_in_page_match_index = ak.vector_size(&all_matches)
ak.vector_extend(&all_matches, &matches)
if let active_document = html_navigable_active_document(page_top_level_traversable(this).html_navigable()):
let last_url = web_url.url_serialize(&this.m_last_find_in_page_url, web_url.ExcludeFragment.yes)
let active_url = dom_document_url(active_document)
let active_url_string = web_url.url_serialize(&active_url, web_url.ExcludeFragment.yes)
if not ak.string_equals(&last_url, &active_url_string):
this.m_last_find_in_page_url = dom_document_url(active_document)
this.m_find_in_page_match_index = 0
if let search_direction = direction:
if should_update_match_index:
if search_direction == PageSearchDirection.forward:
if this.m_find_in_page_match_index >= ak.vector_size(&all_matches) -% 1:
if query.wrap_around == PageWrapAround.no:
return PageFindInPageResult()
this.m_find_in_page_match_index = 0
else:
this.m_find_in_page_match_index += 1
else:
if this.m_find_in_page_match_index == 0:
if query.wrap_around == PageWrapAround.no:
return PageFindInPageResult()
this.m_find_in_page_match_index = ak.vector_size(&all_matches) -% 1
else:
this.m_find_in_page_match_index -= 1
page_update_find_in_page_selection(this, all_matches)
return PageFindInPageResult(current_match_index = this.m_find_in_page_match_index, total_match_count = ak.vector_size(&all_matches))
## Ported from Page.cpp:807 Web::Page::find_in_page.
pub func page_find_in_page(this: Page*, query: PageFindInPageQuery) -> PageFindInPageResult:
if not page_top_level_traversable_is_initialized(this):
return PageFindInPageResult()
if ak.string_is_empty(&query.string):
this.m_last_find_in_page_query = none
page_clear_selection(this)
return PageFindInPageResult()
let result = page_perform_find_in_page_query(this, query)
this.m_last_find_in_page_query = query
this.m_last_find_in_page_url = dom_document_url(html_navigable_active_document(page_top_level_traversable(this).html_navigable()) else trap("no active document"))
return result
## Ported from Page.cpp:826 Web::Page::find_in_page_next_match.
pub func page_find_in_page_next_match(this: Page*) -> PageFindInPageResult:
if not (this.m_last_find_in_page_query != none and page_top_level_traversable_is_initialized(this)):
return PageFindInPageResult()
let result = page_perform_find_in_page_query(this, this.m_last_find_in_page_query else trap("unreachable"), PageSearchDirection.forward)
return result
## Ported from Page.cpp:835 Web::Page::find_in_page_previous_match.
pub func page_find_in_page_previous_match(this: Page*) -> PageFindInPageResult:
if not (this.m_last_find_in_page_query != none and page_top_level_traversable_is_initialized(this)):
return PageFindInPageResult()
let result = page_perform_find_in_page_query(this, this.m_last_find_in_page_query else trap("unreachable"), PageSearchDirection.backward)
return result
## Ported from Page.cpp:844 Web::Page::update_find_in_page_selection.
func page_update_find_in_page_selection(this: Page*, matches: ak.Vector[DomRange*?]):
if ak.vector_is_empty(&matches):
return
page_clear_selection(this)
let current_range = *ak.vector_at_const(&matches, this.m_find_in_page_match_index) else trap("no range")
let common_ancestor_container = dom_range_common_ancestor_container(current_range)
let document = dom_node_document(common_ancestor_container)
if dom_document_window(document) == none:
return
let selection = dom_document_get_selection(document) else return
selection_selection_add_range(selection, current_range)
if let element = dom_node_parent_element(common_ancestor_container):
var scroll_options = DomScrollIntoViewOptions()
dom_scroll_into_view_options_init_fields(&scroll_options)
scroll_options.block = BindScrollLogicalPosition.nearest
scroll_options.inline_ = BindScrollLogicalPosition.nearest
scroll_options.scroll_options.behavior = BindScrollBehavior.instant
_ = dom_element_scroll_into_view(element, BoolOrDomScrollIntoViewOptions.dom_scroll_into_view_options(scroll_options))
# mark: Fullscreen ==============================================================================
## The deferred processing of the pending fullscreen operations (`[this]() { process_pending_fullscreen_operations(); }`).
func page_process_pending_fullscreen_operations_task(context: void*?):
page_process_pending_fullscreen_operations((Page*)(context else trap("no page")))
## Ported from Page.cpp:872 Web::Page::enqueue_fullscreen_enter.
pub func page_enqueue_fullscreen_enter(this: Page*, element: DomElement*, pending_doc: DomDocument*, error_: DomRequestFullscreenError, promise: JsPromiseCapability*):
ak.queue_enqueue(&this.m_pending_fullscreen_operations, PagePendingFullscreenOperation.page_pending_fullscreen_enter(PagePendingFullscreenEnter(element = element, pending_doc = pending_doc, error = error_, promise = promise)))
# NOTE: Processing is deferred because the spec says "run the remaining steps in parallel",
# meaning the caller's synchronous JS should complete before we process the operation.
platform_event_loop_plugin_deferred_invoke(platform_event_loop_plugin_the(), gc.create_function0(gc.cell_heap(this.cell()), ak.Function0[unit](call = page_process_pending_fullscreen_operations_task, context = this)))
## Ported from Page.cpp:882 Web::Page::enqueue_fullscreen_exit.
pub func page_enqueue_fullscreen_exit(this: Page*, doc: DomDocument*, resize: bool, promise: JsPromiseCapability*):
ak.queue_enqueue(&this.m_pending_fullscreen_operations, PagePendingFullscreenOperation.page_pending_fullscreen_exit(PagePendingFullscreenExit(doc = doc, resize = resize, promise = promise)))
# NOTE: Processing is deferred because the spec says "run the remaining steps in parallel",
# meaning the caller's synchronous JS should complete before we process the operation.
platform_event_loop_plugin_deferred_invoke(platform_event_loop_plugin_the(), gc.create_function0(gc.cell_heap(this.cell()), ak.Function0[unit](call = page_process_pending_fullscreen_operations_task, context = this)))
## process_pending_fullscreen_operations' visit of a PendingFullscreenEnter; false when it must
## wait for the fullscreen state to change.
func page_process_fullscreen_enter(this: Page*, enter: PagePendingFullscreenEnter*) -> bool:
# https://fullscreen.spec.whatwg.org/#dom-element-requestfullscreen
# 8. If error is false, then resize pendingDoc's node navigable's top-level traversable's
# active document's viewport's dimensions, optionally taking into account
# options["navigationUI"]:
if enter.error == DomRequestFullscreenError.false_:
if this.m_viewport_is_fullscreen == ViewportIsFullscreen.no:
if not this.m_fullscreen_ipc_sent_to_ui:
page_client_page_did_request_fullscreen_window(this.m_client)
this.m_fullscreen_ipc_sent_to_ui = true
# NB: Stop processing here and wait for a change in the fullscreen state if we aren't
# in the desired state yet.
return false
# 9. If any of the following conditions are false, then set error to true:
# * This's node document is pendingDoc.
# * The fullscreen element ready check for this returns true.
if dom_node_owner_document(enter.element.dom_node()) != (DomDocument*?)enter.pending_doc:
enter.error = DomRequestFullscreenError.element_node_doc_is_not_pending_doc
elif not dom_element_is_element_ready_for_fullscreen(enter.element):
enter.error = DomRequestFullscreenError.element_ready_check_failed
let realm = dom_element_realm(enter.element)
var context: HtmlTemporaryExecutionContext = ---
html_temporary_execution_context_construct_realm(&context, realm, HtmlTemporaryExecutionContextCallbacksEnabled.yes)
defer html_temporary_execution_context_destruct(&context)
# 10. If error is true:
if enter.error != DomRequestFullscreenError.false_:
# 1. Append (fullscreenerror, this) to pendingDoc's list of pending fullscreen events.
dom_document_append_pending_fullscreen_change(enter.pending_doc, DomPendingFullscreenEventType.error_, enter.element)
# 2. Reject promise with a TypeError exception and terminate these steps.
idl_reject_promise(realm, enter.promise, js_type_error_create(realm, ak.string_from_utf8(dom_request_fullscreen_error_to_string(enter.error)) else trap("MUST: String::from_utf8")))
return true
# 11. Let fullscreenElements be an ordered set initially consisting of this.
# (A GC::HeapVector in C++: managed memory here, which the collector scans.)
let fullscreen_elements = new ak.Vector[DomElement*]() else trap("out of memory")
ak.vector_append_t(fullscreen_elements, enter.element)
# 12. While true:
while true:
# 1. Let last be the last item of fullscreenElements.
let last = *ak.vector_last(fullscreen_elements)
# 2. Let container be last's node navigable's container.
let container = html_navigable_container(dom_node_navigable(last.dom_node()) else trap("no node navigable"))
# 3. If container is null, then break.
let element = container else break
# 4. Append container to fullscreenElements.
ak.vector_append_t(fullscreen_elements, element.dom_element())
# 13. For each element in fullscreenElements:
for element in ak.vector_span(fullscreen_elements):
# 1. Let doc be element's node document.
let doc = dom_node_document(element.dom_node())
# 2. If element is doc's fullscreen element, continue.
if dom_document_fullscreen_element(doc) == (DomElement*?)element:
continue
# 3. If element is this and this is an iframe element, then set element's iframe fullscreen flag.
if element == enter.element and is_html_htmli_frame_element(enter.element):
html_htmli_frame_element_set_iframe_fullscreen_flag(as_html_htmli_frame_element(element), true)
# 4. Fullscreen element within doc.
dom_document_fullscreen_element_within_doc(doc, element)
# 5. Append (fullscreenchange, element) to doc's list of pending fullscreen events.
dom_document_append_pending_fullscreen_change(doc, DomPendingFullscreenEventType.change, element)
# 14. Resolve promise with undefined
idl_resolve_promise(realm, enter.promise, js_undefined())
return true
## process_pending_fullscreen_operations' visit of a PendingFullscreenExit; false when it must
## wait for the fullscreen state to change.
func page_process_fullscreen_exit(this: Page*, exit: PagePendingFullscreenExit*) -> bool:
let realm = dom_document_realm(exit.doc)
var context: HtmlTemporaryExecutionContext = ---
html_temporary_execution_context_construct_realm(&context, realm, HtmlTemporaryExecutionContextCallbacksEnabled.yes)
defer html_temporary_execution_context_destruct(&context)
# https://fullscreen.spec.whatwg.org/#exit-fullscreen
# FIXME: 9. Run the fully unlock the screen orientation steps with doc.
# 10. If resize is true, resize doc's viewport to its "normal" dimensions.
if exit.resize and this.m_viewport_is_fullscreen == ViewportIsFullscreen.yes:
if not this.m_fullscreen_ipc_sent_to_ui:
page_client_page_did_request_exit_fullscreen(this.m_client)
this.m_fullscreen_ipc_sent_to_ui = true
# NB: Stop processing here and wait for a change in the fullscreen state if we aren't
# in the desired state yet.
return false
# 11. If doc's fullscreen element is null, then resolve promise with undefined and terminate these
# steps.
if dom_document_fullscreen_element(exit.doc) == none:
idl_resolve_promise(realm, exit.promise, js_undefined())
return true
# 12. Let exitDocs be the result of collecting documents to unfullscreen given doc.
let exit_docs = dom_document_collect_documents_to_unfullscreen(exit.doc)
# 13. Let descendantDocs be an ordered set consisting of doc's descendant navigables' active documents
# whose fullscreen element is non-null, if any, in tree order.
let descendant_docs = new ak.Vector[DomDocument*]() else trap("out of memory")
let descendants = dom_document_descendant_navigables(exit.doc)
for descendant in ak.vector_span_const(&descendants):
let active_document = html_navigable_active_document(descendant else trap("no navigable")) else trap("no active document")
if dom_document_fullscreen_element(active_document) != none:
ak.vector_append_t(descendant_docs, active_document)
# 14. For each exitDoc in exitDocs:
for exit_doc in ak.vector_span(exit_docs):
# 1. Append (fullscreenchange, exitDoc's fullscreen element) to exitDoc's list of pending
# fullscreen events.
dom_document_append_pending_fullscreen_change(exit_doc, DomPendingFullscreenEventType.change, dom_document_fullscreen_element(exit_doc) else trap("no fullscreen element"))
# 2. If resize is true, unfullscreen exitDoc.
if exit.resize:
dom_document_unfullscreen(exit_doc)
# 3. Otherwise, unfullscreen exitDoc's fullscreen element.
else:
dom_document_unfullscreen_element(exit_doc, dom_document_fullscreen_element(exit_doc) else trap("no fullscreen element"))
# 15. For each descendantDoc in descendantDocs:
for descendant_doc in ak.vector_span(descendant_docs):
# 1. Append (fullscreenchange, descendantDoc's fullscreen element) to descendantDoc's list of
# pending fullscreen events.
dom_document_append_pending_fullscreen_change(descendant_doc, DomPendingFullscreenEventType.change, dom_document_fullscreen_element(descendant_doc) else trap("no fullscreen element"))
# 2. Unfullscreen descendantDoc.
dom_document_unfullscreen(descendant_doc)
# 16. Resolve promise with undefined.
idl_resolve_promise(realm, exit.promise, js_undefined())
return true
## Ported from Page.cpp:892 Web::Page::process_pending_fullscreen_operations.
pub func page_process_pending_fullscreen_operations(this: Page*):
# FIXME: The Fullscreen API interacts with the top-level traversable's viewport. With site-isolation,
# an iframe's content process won't have direct access to this Page, so fullscreen operations
# will need to be routed through IPC to the top-level process.
# NOTE: Resolving/rejecting promises during processing may trigger JS microtasks that re-enter
# this function (e.g., JS calls exitFullscreen() after a requestFullscreen() promise resolves).
# The outer call's while loop will pick up newly enqueued items.
if this.m_processing_fullscreen_operations:
return
this.m_processing_fullscreen_operations = true
defer this.m_processing_fullscreen_operations = false
while not ak.queue_is_empty(&this.m_pending_fullscreen_operations):
let front = ak.queue_head(&this.m_pending_fullscreen_operations)
var processed = false
match *front:
.page_pending_fullscreen_enter(enter):
var operation = enter
processed = page_process_fullscreen_enter(this, &operation)
*front = PagePendingFullscreenOperation.page_pending_fullscreen_enter(operation)
.page_pending_fullscreen_exit(exit):
var operation = exit
processed = page_process_fullscreen_exit(this, &operation)
if not processed:
break
_ = ak.queue_dequeue(&this.m_pending_fullscreen_operations)
## Ported from Page.cpp:1066 Web::Page::set_viewport_is_fullscreen.
pub func page_set_viewport_is_fullscreen(this: Page*, is_fullscreen: ViewportIsFullscreen):
if this.m_viewport_is_fullscreen == is_fullscreen:
return
this.m_viewport_is_fullscreen = is_fullscreen
this.m_fullscreen_ipc_sent_to_ui = false
page_process_pending_fullscreen_operations(this)
# mark: Page.h accessors ========================================================================
## Ported from Page.h:224 Web::Page::page_mute_state.
pub func page_page_mute_state(this: Page*) -> HtmlMuteState:
return this.m_mute_state
## Ported from Page.h:227 Web::Page::user_style.
pub func page_user_style(this: Page*) -> ak.String?:
return this.m_user_style_sheet_source
## Ported from Page.h:230 Web::Page::pdf_viewer_supported.
pub func page_pdf_viewer_supported(this: Page*) -> bool:
return this.m_pdf_viewer_supported
## Ported from Page.h:250 Web::Page::last_find_in_page_query.
pub func page_last_find_in_page_query(this: Page*) -> PageFindInPageQuery?:
return this.m_last_find_in_page_query
## Ported from Page.h:252 Web::Page::listen_for_dom_mutations.
pub func page_listen_for_dom_mutations(this: Page*) -> bool:
return this.m_listen_for_dom_mutations
## Ported from Page.h:253 Web::Page::set_listen_for_dom_mutations.
pub func page_set_listen_for_dom_mutations(this: Page*, listen_for_dom_mutations: bool):
this.m_listen_for_dom_mutations = listen_for_dom_mutations
## Ported from Page.h:259 Web::Page::viewport_is_fullscreen.
pub func page_viewport_is_fullscreen(this: Page*) -> ViewportIsFullscreen:
return this.m_viewport_is_fullscreen