Skip to content

Truly independent web browser

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

#pragma once

#include <LibCompositing/Scrolling/AsyncScrollingState.h>
#include <LibCompositing/Types.h>
#include <LibGC/Root.h>
#include <LibWeb/Bindings/Internals.h>
#include <LibWeb/Bindings/MediaSession.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/Export.h>
#include <LibWeb/Forward.h>
#include <LibWeb/HTML/Scripting/DevToolsLoggedObjects.h>
#include <LibWeb/HTML/Scripting/DevToolsObjectTable.h>
#include <LibWeb/Internals/InternalAnimationTimeline.h>
#include <LibWeb/Internals/InternalsBase.h>
#include <LibWeb/Painting/Forward.h>
#include <LibWebCommon/Page/InputEvent.h>
#include <LibWebCommon/UIEvents/MouseButton.h>
#include <LibWebCommon/WebIDL/Types.h>

namespace JS {

class Object;
class Realm;

}

namespace Web::Internals {

class WEB_API Internals final : public InternalsBase {
    WEB_WRAPPABLE(Internals, InternalsBase);
    GC_DECLARE_ALLOCATOR(Internals);

public:
    // Same as Internals.idl
    static constexpr unsigned short MOD_NONE = 0;
    static constexpr unsigned short MOD_ALT = 1;
    static constexpr unsigned short MOD_CTRL = 2;
    static constexpr unsigned short MOD_SHIFT = 4;
    static constexpr unsigned short MOD_SUPER = 8;
    static constexpr unsigned short MOD_KEYPAD = 16;

    static constexpr unsigned short BUTTON_LEFT = 0;
    static constexpr unsigned short BUTTON_MIDDLE = 1;
    static constexpr unsigned short BUTTON_RIGHT = 2;

    virtual ~Internals() override;

    void signal_test_is_done(Utf16String const& text);
    void set_test_timeout(double milliseconds);
    void force_incompatible_visual_context_tree_rebuild();
    void set_force_dark_enabled(bool);
    void set_show_line_box_borders(bool);
    void set_force_dark_thresholds(i32, i32);
    Utf16String dump_visual_context_nodes(DOM::Element&);
    Utf16String dump_compositor_animations();
    void send_mismatched_visual_context_tree_update_to_compositor();
    WebIDL::ExceptionOr<void> load_reference_test_metadata();

    bool has_completed_initial_paint() const;
    bool initial_paint_had_pending_fonts() const;
    bool has_pending_image_loads() const;
    bool had_user_edits() const;
    void set_font_display_time(CSS::FontFace&, u32 milliseconds);
    WebIDL::ExceptionOr<Utf16String> set_time_zone(Utf16String const& time_zone);

    void gc();
    u32 worker_agent_count() const;
    void gc_async(bool move_cells, GC::Ref<WebIDL::Promise>);
    WebIDL::UnsignedLongLong discard_decoded_image_frames();
    WebIDL::ExceptionOr<void> mark_as_garbage(Utf16String const& variable_name);
    bool wrapper_is_preserved(JS::Object&);
    bool has_pending_activity(JS::Object&, GC::Ptr<DOM::Document>);
    WebIDL::UnsignedLongLong message_port_pending_outgoing_message_count(HTML::MessagePort&);
    bool html_collection_has_valid_cache(DOM::HTMLCollection&);
    void fail_next_message_port_transfer(HTML::MessagePort&);
    bool message_ports_are_directly_entangled(HTML::MessagePort&, HTML::MessagePort&);
    String message_port_entanglement_kind(HTML::MessagePort&);
    bool message_port_transport_is_closed(HTML::MessagePort&);
    GC::Ref<XHR::XMLHttpRequest> create_xml_http_request_for_document(DOM::Document&);
    Optional<Painting::HitTestResult> hit_test(double x, double y);
    GC::Ptr<JS::Object> hit_test_result(double x, double y);
    GC::Ptr<JS::Object> take_context_menu_request();
    void toggle_media_context_menu_picture_in_picture();

    WebIDL::ExceptionOr<void> add_spelling_error_for_testing(GC::Ref<DOM::Node>, WebIDL::UnsignedLong start, WebIDL::UnsignedLong end);
    Utf16String spelling_error_ranges(GC::Ref<DOM::Node>);
    GC::Ref<WebIDL::Promise> check_spelling_for_testing(Utf16String const& text);
    GC::Ref<WebIDL::Promise> spell_check_settled();

    void translate_page(Utf16String const& target_language);
    GC::Ref<WebIDL::Promise> translation_settled();

    GC::Ref<WebIDL::Promise> request_writing_suggestion_for_testing(Utf16String const& text, WebIDL::UnsignedLong caret_offset);
    Utf16String writing_suggestion();
    GC::Ref<WebIDL::Promise> writing_suggestion_settled();
    GC::Ref<WebIDL::Promise> writing_suggestion_request_sent();
    GC::Ref<WebIDL::Promise> activate_spelling_menu_item(Bindings::SpellingMenuItem, WebIDL::UnsignedLong guess_index);

    void send_text(HTML::HTMLElement&, Utf16String const&, WebIDL::UnsignedShort modifiers);
    void send_text_through_ui_process(Utf16String const&);
    void send_key_through_ui_process(Utf16String const& key_name);
    void grant_transient_activation();
    void click_through_ui_process(double x, double y, GC::Ref<WebIDL::Promise>);
    void perform_actions(Utf16String const&, GC::Ref<WebIDL::Promise>);
    void mouse_event_through_ui_process(Bindings::MouseEventThroughUIProcessType, double x, double y, Bindings::MouseEventThroughUIProcessOptions const&, GC::Ref<WebIDL::Promise>);
    void wheel_through_ui_process(double x, double y, double delta_x, double delta_y, bool precise, Bindings::ScrollGesturePhase, Bindings::WheelTicks const&, GC::Ref<WebIDL::Promise>);
    void send_key(HTML::HTMLElement&, Utf16String const&, WebIDL::UnsignedShort modifiers, WebIDL::UnsignedLong repeat_count);
    WebIDL::ExceptionOr<void> send_text_editing_command(HTML::HTMLElement&, Utf16String const& command_name, bool extend_selection);
    void paste(HTML::HTMLElement& target, Utf16String const& text);
    void paste_from_clipboard();
    void commit_text();

    // Low-level mouse primitives
    void mouse_down(double x, double y, WebIDL::UnsignedShort click_count, WebIDL::UnsignedShort button, WebIDL::UnsignedShort modifiers, Optional<WebIDL::UnsignedShort> buttons = {});
    void mouse_up(double x, double y, WebIDL::UnsignedShort button, WebIDL::UnsignedShort modifiers, WebIDL::UnsignedShort buttons = 0);
    void mouse_move(double x, double y, WebIDL::UnsignedShort modifiers, WebIDL::UnsignedShort buttons = 0);
    void mouse_leave();
    void mouse_cancel();

    // High-level mouse conveniences
    void click(double x, double y, WebIDL::UnsignedShort click_count, WebIDL::UnsignedShort button, WebIDL::UnsignedShort modifiers);
    void click_and_hold(double x, double y, WebIDL::UnsignedShort click_count, WebIDL::UnsignedShort button, WebIDL::UnsignedShort modifiers);
    void wheel(GC::Ref<WebIDL::Promise>, double x, double y, double delta_x, double delta_y, bool precise, Bindings::ScrollGesturePhase, Bindings::WheelTicks const&);
    void wheel(double x, double y, double delta_x, double delta_y, bool precise, Bindings::ScrollGesturePhase, Bindings::WheelTicks const&, GC::Ref<WebIDL::Promise>);
    GC::Ref<JS::Object> compositor_scroll_state(GC::Ptr<DOM::Element> scroller);
    void pinch(double x, double y, double scale_delta, WebIDL::UnsignedShort modifiers);
    void pinch_through_ui_process(double x, double y, double scale_delta, bool continues_gesture);
    void reset_zoom();

    Optional<Utf16String> hovered_link();
    Utf16String current_cursor();
    Optional<Utf16String> current_cursor_pixel(i32 x, i32 y);
    Utf16String theme_color();

    Utf16String selected_text_for_clipboard();
    Optional<Utf16String> generate_text_fragment_url(DOM::Range&, Utf16String const& current_url);
    Utf16String devtools_grip(JS::Value);
    Utf16String devtools_properties(JS::Value);
    Utf16String devtools_log(GC::RootVector<JS::Value> const& arguments);
    Utf16String devtools_logged_properties(u64 object_id);
    Utf16String devtools_log_table(GC::Root<JS::Object> const& table_data);
    void set_long_stack_traces_enabled(bool);
    Optional<Vector<String>> complete_console_input(Utf16String const& input);

    void find_in_page(Utf16String const& query, bool highlight_all);
    void find_in_page_next_match();
    void find_in_page_end();

    WebIDL::ExceptionOr<void> set_clipboard_file(Utf16String const& name, Utf16String const& mime_type, Utf16String const& data);

    void set_marked_text_from_input_method(Utf16String const& text);
    void commit_text_from_input_method(Utf16String const& text, WebIDL::Long replacement_start, WebIDL::Long replacement_length);
    void unmark_text_from_input_method();
    GC::Ptr<Geometry::DOMRect> current_caret_rect();

    WebIDL::ExceptionOr<bool> dispatch_user_activated_event(DOM::EventTarget&, DOM::Event& event);

    void spoof_current_url(Utf16String const& url);
    void spoof_current_origin_in_this_process(Utf16String const& url);
    void set_navigator_compatibility_mode(Bindings::InternalsNavigatorCompatibilityMode);
    void load_url(Utf16String const& url);

    GC::Ref<InternalAnimationTimeline> create_internal_animation_timeline();

    WebIDL::ExceptionOr<void> simulate_drag_start(double x, double y, Utf16String const& name, Utf16String const& contents);
    void simulate_drag_move(double x, double y);
    void simulate_drop(double x, double y);

    void expire_cookies_with_time_offset(WebIDL::LongLong seconds);
    void clear_storage_for_testing(Utf16String const& type);
    GC::Ref<WebIDL::Promise> delete_all_cookies();
    WebIDL::ExceptionOr<bool> has_cookie_for_url(Utf16String const& url, String const& name, String const& value);

    bool set_http_memory_cache_enabled(bool enabled);
    bool functions_share_code(JS::Value a, JS::Value b);
    bool compiled_script_cache_contains(Utf16String const& url);
    bool set_disk_cache_enabled_for_navigations(bool enabled);
    void simulate_request_server_connection_loss();
    void simulate_worker_request_server_connection_loss();
    WebIDL::ExceptionOr<void> send_bad_ipc_message_for_testing(Utf16String const& kind);
    WebIDL::ExceptionOr<void> set_content_blockers(Utf16String const& patterns);
    WebIDL::ExceptionOr<void> set_site_compatibility_data(Utf16String const& source);
    void set_content_blocking_enabled(bool enabled);
    void begin_layout_trace(Bindings::LayoutTraceOptions const&);
    void update_layout_for_testing();
    Utf16String take_layout_trace();
    void begin_display_list_trace();
    void begin_visual_context_trace();
    Utf16String take_visual_context_trace();
    void update_visual_contexts_for_testing();
    u64 describe_owners_of_visual_context_nodes();
    Utf16String take_display_list_trace();
    void begin_input_trace();
    GC::Ref<JS::Object> take_input_trace();
    GC::Ref<JS::Object> compositor_wheel_target_at(double x, double y, double delta_x, double delta_y);
    GC::Ref<JS::Object> recorded_display_list_wheel_target_at(double x, double y, double delta_x, double delta_y);
    bool recorded_display_list_blocks_wheel_event_at(double x, double y);
    GC::Ref<JS::Object> recorded_async_scrolling_state_object();
    void record_display_list_for_testing(bool paint_overlay, bool cold, bool finest_runs);
    void set_autoplay_policy(Utf16String const& policy);

    Utf16String get_computed_role(DOM::Element& element);
    Utf16String get_computed_label(DOM::Element& element);
    Utf16String get_computed_aria_level(DOM::Element& element);
    void set_accessibility_focus_target(DOM::Element& element);
    void set_accessibility_interested();

    static u16 get_echo_server_port();
    static void set_echo_server_port(u16 port);
    static u16 get_https_echo_server_port();
    static void set_https_echo_server_port(u16 port);

    WebIDL::ExceptionOr<void> set_hsts_policy(Utf16String const& domain, u64 max_age, bool include_sub_domains);
    WebIDL::ExceptionOr<void> ingest_hsts_header(Utf16String const& url, Utf16String const& header_value);
    WebIDL::ExceptionOr<bool> is_known_hsts_host(Utf16String const& domain);

    void set_browser_zoom(double factor);
    void set_device_pixel_ratio(double ratio);
    void resize_window(i32 width, i32 height);

    bool headless();
    bool screen_wake_lock_active();
    Utf16String picture_in_picture_window_state();

    bool needs_repaint();
    bool needs_display_list_record();
    bool has_animation_frame_callbacks();

    Utf16String dump_display_list(Bindings::DisplayListDumpOptions const&);
    Utf16String dump_display_list_changes(Bindings::DisplayListDumpOptions const&);
    Utf16String dump_accessibility_tree();
    String accessibility_character_offsets(GC::Ref<DOM::Node>);
    String dump_accessibility_node_data(GC::Ref<DOM::Node>);
    String accessibility_cached_text_mismatches();
    String accessibility_cached_record_mismatches();
    String accessibility_cached_geometry_mismatches();
    String accessibility_change_tracking_mismatches();
    String accessibility_last_change_size() const { return m_last_accessibility_change_size; }
    Utf16String dump_layout_tree(GC::Ref<DOM::Node>);
    Utf16String dump_stacking_context_tree();
    Utf16String stacking_context_structure_verification_report();
    Utf16String dump_gc_graph();
    Utf16String heap_census();
    Utf16String dump_session_history();
    Utf16String dump_ui_process_session_history();
    Utf16String dump_ui_process_session_history_without_update();
    bool capture_session_history_snapshot();
    bool restore_captured_session_history_snapshot();
    bool record_reported_session_history();
    Utf16String dump_reported_session_history();
    Utf16String dump_site_isolation_process_tree();
    bool has_populated_document(HTML::HTMLIFrameElement&);
    bool canvas_has_unpresented_content(HTML::HTMLCanvasElement&);
    void crash_remote_frame_processes();
    i32 web_content_process_id();
    Vector<String> documents_in_this_process();
    bool is_in_realm_of_destroyed_document(JS::Object&);
    void close_tab_through_ui_process();
    void stop_loading_through_ui_process();
    void reload_through_ui_process();
    void reload_bypassing_cache_through_ui_process();
    void traverse_history_through_ui_process(i32 delta);
    GC::Ref<WebIDL::Promise> flush_session_history_traversal_queue();
    void exhaust_navigation_rate_limit();
    bool html_parser_body_is_exhausted(DOM::Document&);
    bool has_html_parser_end_state(DOM::Document& document) { return document.has_html_parser_end_state(); }

    bool has_shadow_root(GC::Ref<DOM::Element>);
    GC::Ptr<DOM::ShadowRoot> get_shadow_root(GC::Ref<DOM::Element>);

    void pump_gamepad_events();

    GC::Ref<InternalGamepad> connect_virtual_gamepad();
    void disconnect_virtual_gamepad(GC::Ref<InternalGamepad>);

    void perform_per_test_cleanup();

    void set_highlighted_node(GC::Ptr<DOM::Node> node);

    void clear_element(HTML::HTMLElement&);
    void set_environments_top_level_url(Utf16String const& url);
    void set_geolocation_emulated_position(double latitude, double longitude, double accuracy, Optional<double> altitude, Optional<double> altitude_accuracy, Optional<double> heading, Optional<double> speed);
    void set_geolocation_emulated_position_unavailable();

    WebIDL::ExceptionOr<GC::Ref<WebIDL::Promise>> request_permission(Utf16String const& name);
    GC::Ref<WebIDL::Promise> set_permission(Utf16String const& name, Utf16String const& state);
    void set_next_permission_prompt_outcome(Utf16String const& name, Utf16String const& outcome, bool held);
    void set_system_microphone_authorization(Utf16String const& authorization, Utf16String const& answer, bool held);
    void release_system_microphone_answer();
    Utf16String microphone_state();
    bool audio_capture_is_pending();
    void release_held_permission_prompt(WebIDL::UnsignedLong waiters);
    GC::Ref<WebIDL::Promise> advance_permission_clock(WebIDL::UnsignedLongLong milliseconds);
    Vector<String> permission_cache_origins();
    String notifications_state_for_testing();
    void click_notification_for_testing(NotificationsAPI::Notification&);
    void click_notification_by_id_for_testing(WebIDL::UnsignedLongLong browser_notification_id);
    void dismiss_notification_for_testing(NotificationsAPI::Notification&);
    void hold_next_notification_show_for_testing();
    void fail_held_notification_show_for_testing();

    void set_manual_rendering_opportunities(bool enabled);
    void inject_rendering_opportunity(double frame_time_ms);
    Utf16String frame_scheduler_state() const;
    void synchronize_compositor();
    void hold_next_frame(Utf16String const& hold);
    Utf16String last_frame_presented_by(DOM::Document&);
    void inject_clock_tick(double frame_time_ms, Optional<double> viewport_scroll_y);
    void inject_clock_tick_scrolling_element(double frame_time_ms, DOM::Element& scroller, double x, double y);
    void inject_hover_pointer(double x, double y, Optional<double> frame_time_ms, Optional<u64> input_event_id, u32 buttons);
    void inject_hover_pointer_left(double frame_time_ms);
    void note_animated_content_moved_under_pointer(double frame_time_ms);
    void inject_pointer_rests_on_moving_content(double frame_time_ms);
    u64 queue_mouse_move(double x, double y, u32 buttons);
    void move_hover_pointer(double x, double y, Optional<u64> input_event_id);
    Utf16String clock_lane_state(DOM::Document&);
    bool clock_lane_is_coming(DOM::Document&);
    Utf16String presented_selection();
    Utf16String painted_selection();
    GC::Ptr<Geometry::DOMRect> presented_border_box(DOM::Element&);
    GC::Ptr<Geometry::DOMPointReadOnly> presented_scroll_offset(DOM::Element&);
    Optional<Vector<String>> presented_compositor_animations(DOM::Element&);
    Optional<double> presented_opacity(DOM::Element&);
    Optional<String> presented_color(DOM::Element&, Optional<Utf16String> const& pseudo_element);
    bool last_frame_keyboard_scroll_state_is_current();
    void release_held_frame();
    bool rendering_update_holds_tasks_of(DOM::Document&);
    void update_compositor_animations();
    bool run_empty_animation_style_update_for_testing();
    void arm_compositor_animation_timers_for_testing();
    void fire_compositor_animation_wakeup_for_testing(double frame_time_ms);
    void force_visual_context_tree_rebuild_on_next_compositor_animation_update_for_testing();
    void request_reentrant_animation_style_flush_for_testing(GC::Ref<DOM::Node>);
    GC::Ref<JS::Object> compare_layout_tree_with_full_rebuild();
    void request_full_layout_tree_update();
    void force_parallel_layout();
    GC::Ref<JS::Object> style_engine_counters();
    GC::Ref<JS::Object> style_memory_report();
    u64 style_memory_of_frames();
    void panic_render_state_for_testing();
    Vector<String> style_self_check();
    void begin_style_trace();
    void begin_rendering_trace();
    String take_rendering_trace();
    Utf16String take_style_trace();
    Utf16String style_engine_matched_rules();
    Utf16String dump_style_records(GC::Ptr<DOM::Node> root, Bindings::StyleRecordsDumpOptions const&);
    void update_style();
    void set_user_style(Utf16String const& source);
    void set_preferred_color_scheme(Utf16String const& color_scheme);
    void set_page_focus(bool has_focus);
    void set_system_visibility_state(Utf16String const& state);
    void set_hidden_document_timer_wake_up_interval(double milliseconds);
    void set_hidden_document_intensive_timer_throttling(double wake_up_interval, double grace_period_once_loaded, double grace_period_while_loading);
    WebIDL::UnsignedLongLong active_timer_count(JS::Object& window);
    Vector<WebIDL::Long> active_timer_ids(JS::Object& window);
    WebIDL::UnsignedLongLong task_queue_length();
    Utf16String canvas_color_scheme();
    Vector<String> image_request_urls(DOM::Document const&) const;
    WebIDL::ExceptionOr<GC::Ref<JS::Object>> image_animation_state_for_url(Utf16String const& url);
    bool media_element_is_fetching(HTML::HTMLMediaElement&);
    bool media_element_is_playing_audio(HTML::HTMLMediaElement&);
    JS::Value active_media_session_state();
    JS::Value now_playing_state_for_testing();
    WebIDL::ExceptionOr<void> trigger_now_playing_action_for_testing(Bindings::MediaSessionAction, GC::Ptr<JS::Object> details);
    GC::Ref<WebIDL::Promise> wait_for_media_session_artwork();
    WebIDL::ExceptionOr<void> trigger_media_session_action(Bindings::MediaSessionAction, GC::Ptr<JS::Object> details);
    bool document_is_playing_audio(DOM::Document&);
    bool tab_is_playing_audio();
    bool tab_is_heard_playing_audio();
    bool tab_is_using_microphone();
    bool tab_is_capturing_audio();
    void report_audio_capture_of_unknown_page(bool capturing);
    WebIDL::UnsignedLongLong picture_in_picture_window_page_id();
    void report_audio_capture_of_page(WebIDL::UnsignedLongLong page_id, bool capturing);
    void retire_picture_in_picture_window();
    WebIDL::UnsignedLongLong audio_capture_stream_count();
    Vector<String> tab_audibility_transitions();
    bool media_element_video_sink_is_ticking(HTML::HTMLMediaElement&);
    void set_media_element_ready_state(HTML::HTMLMediaElement&, u16 ready_state);
    void set_media_element_paused(HTML::HTMLMediaElement&, bool paused);
    void set_media_element_seeking(HTML::HTMLMediaElement&, bool seeking);
    void disconnect_media_server();
    void disconnect_audio_server();
    String viewport_overflow_x();

    GC::Ref<Streams::ReadableStream> create_stream_pulling_from_bytes(String const& bytes, WebIDL::UnsignedLong pull_size);

private:
    GC::Ref<JS::Object> first_wheel_step_route_to_object(Compositing::FirstWheelStepRouteForTesting const&);
    Web::MouseEvent mouse_event_through_ui_process_at(Web::MouseEvent::Type, double x, double y);
    void send_mouse_events_through_ui_process(Vector<Web::MouseEvent>, GC::Ref<WebIDL::Promise>);
    explicit Internals(HTML::Window&);

    virtual void visit_edges(GC::Cell::Visitor&) override;

    UIEvents::MouseButton button_from_unsigned_short(WebIDL::UnsignedShort button);

    GC::Root<JS::Cell> m_actions_executor;
    bool m_actions_running { false };
    Optional<Utf16String> m_pending_action_test_result;

    struct DisplayListDumpForChanges {
        UniqueNodeID document_id;
        String jsonl;
    };
    Optional<DisplayListDumpForChanges> m_display_list_dump_for_changes;
    Vector<GC::Ref<InternalGamepad>> m_gamepads;
    Optional<HTML::DevToolsObjectTable> m_devtools_object_table;
    HTML::DevToolsLoggedObjects m_devtools_logged_objects;
    // The accessibility tree as accessibility_change_tracking_mismatches() last left it, built up from changes alone.
    DOM::AccessibilityChangeTracker m_accessibility_change_tracker;
    HashMap<i64, WebView::AccessibilityNodeData> m_accessibility_tracked_tree;
    String m_last_accessibility_change_size;
};

}