ViewImplementation.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2023, Linus Groh <linusg@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/Forward.h>
  9. #include <AK/String.h>
  10. #include <LibGfx/Forward.h>
  11. #include <LibGfx/StandardCursor.h>
  12. #include <LibWeb/Forward.h>
  13. #include <LibWeb/HTML/ActivateTab.h>
  14. #include <LibWebView/Forward.h>
  15. #include <LibWebView/WebContentClient.h>
  16. namespace WebView {
  17. // Note: This only exists inside Serenity to avoid #ifdefs in all implementors of ViewImplementation.
  18. enum class EnableCallgrindProfiling {
  19. No,
  20. Yes
  21. };
  22. class ViewImplementation {
  23. public:
  24. virtual ~ViewImplementation() { }
  25. struct DOMNodeProperties {
  26. String computed_style_json;
  27. String resolved_style_json;
  28. String custom_properties_json;
  29. String node_box_sizing_json;
  30. };
  31. AK::URL const& url() const { return m_url; }
  32. String const& handle() const { return m_client_state.client_handle; }
  33. void load(AK::URL const&);
  34. void load_html(StringView, AK::URL const&);
  35. void load_empty_document();
  36. void zoom_in();
  37. void zoom_out();
  38. void reset_zoom();
  39. float zoom_level() const { return m_zoom_level; }
  40. void set_preferred_color_scheme(Web::CSS::PreferredColorScheme);
  41. DeprecatedString selected_text();
  42. void select_all();
  43. void get_source();
  44. void inspect_dom_tree();
  45. void inspect_accessibility_tree();
  46. ErrorOr<DOMNodeProperties> inspect_dom_node(i32 node_id, Optional<Web::CSS::Selector::PseudoElement> pseudo_element);
  47. void clear_inspected_dom_node();
  48. i32 get_hovered_node_id();
  49. void debug_request(DeprecatedString const& request, DeprecatedString const& argument = {});
  50. void run_javascript(StringView);
  51. virtual void notify_server_did_layout(Badge<WebContentClient>, Gfx::IntSize content_size) = 0;
  52. virtual void notify_server_did_paint(Badge<WebContentClient>, i32 bitmap_id) = 0;
  53. virtual void notify_server_did_invalidate_content_rect(Badge<WebContentClient>, Gfx::IntRect const&) = 0;
  54. virtual void notify_server_did_change_selection(Badge<WebContentClient>) = 0;
  55. virtual void notify_server_did_request_cursor_change(Badge<WebContentClient>, Gfx::StandardCursor cursor) = 0;
  56. virtual void notify_server_did_change_title(Badge<WebContentClient>, DeprecatedString const&) = 0;
  57. virtual void notify_server_did_request_scroll(Badge<WebContentClient>, i32, i32) = 0;
  58. virtual void notify_server_did_request_scroll_to(Badge<WebContentClient>, Gfx::IntPoint) = 0;
  59. virtual void notify_server_did_request_scroll_into_view(Badge<WebContentClient>, Gfx::IntRect const&) = 0;
  60. virtual void notify_server_did_enter_tooltip_area(Badge<WebContentClient>, Gfx::IntPoint, DeprecatedString const&) = 0;
  61. virtual void notify_server_did_leave_tooltip_area(Badge<WebContentClient>) = 0;
  62. virtual void notify_server_did_hover_link(Badge<WebContentClient>, const AK::URL&) = 0;
  63. virtual void notify_server_did_unhover_link(Badge<WebContentClient>) = 0;
  64. virtual void notify_server_did_click_link(Badge<WebContentClient>, const AK::URL&, DeprecatedString const& target, unsigned modifiers) = 0;
  65. virtual void notify_server_did_middle_click_link(Badge<WebContentClient>, const AK::URL&, DeprecatedString const& target, unsigned modifiers) = 0;
  66. virtual void notify_server_did_start_loading(Badge<WebContentClient>, const AK::URL&, bool is_redirect) = 0;
  67. virtual void notify_server_did_finish_loading(Badge<WebContentClient>, const AK::URL&) = 0;
  68. virtual void notify_server_did_request_navigate_back(Badge<WebContentClient>) = 0;
  69. virtual void notify_server_did_request_navigate_forward(Badge<WebContentClient>) = 0;
  70. virtual void notify_server_did_request_refresh(Badge<WebContentClient>) = 0;
  71. virtual void notify_server_did_request_context_menu(Badge<WebContentClient>, Gfx::IntPoint) = 0;
  72. virtual void notify_server_did_request_link_context_menu(Badge<WebContentClient>, Gfx::IntPoint, const AK::URL&, DeprecatedString const& target, unsigned modifiers) = 0;
  73. virtual void notify_server_did_request_image_context_menu(Badge<WebContentClient>, Gfx::IntPoint, const AK::URL&, DeprecatedString const& target, unsigned modifiers, Gfx::ShareableBitmap const&) = 0;
  74. virtual void notify_server_did_request_alert(Badge<WebContentClient>, String const& message) = 0;
  75. virtual void notify_server_did_request_confirm(Badge<WebContentClient>, String const& message) = 0;
  76. virtual void notify_server_did_request_prompt(Badge<WebContentClient>, String const& message, String const& default_) = 0;
  77. virtual void notify_server_did_request_set_prompt_text(Badge<WebContentClient>, String const& message) = 0;
  78. virtual void notify_server_did_request_accept_dialog(Badge<WebContentClient>) = 0;
  79. virtual void notify_server_did_request_dismiss_dialog(Badge<WebContentClient>) = 0;
  80. virtual void notify_server_did_get_source(const AK::URL& url, DeprecatedString const& source) = 0;
  81. virtual void notify_server_did_get_dom_tree(DeprecatedString const& dom_tree) = 0;
  82. virtual void notify_server_did_get_dom_node_properties(i32 node_id, DeprecatedString const& computed_style, DeprecatedString const& resolved_style, DeprecatedString const& custom_properties, DeprecatedString const& node_box_sizing) = 0;
  83. virtual void notify_server_did_get_accessibility_tree(DeprecatedString const& accessibility_tree) = 0;
  84. virtual void notify_server_did_output_js_console_message(i32 message_index) = 0;
  85. virtual void notify_server_did_get_js_console_messages(i32 start_index, Vector<DeprecatedString> const& message_types, Vector<DeprecatedString> const& messages) = 0;
  86. virtual void notify_server_did_change_favicon(Gfx::Bitmap const& favicon) = 0;
  87. virtual Vector<Web::Cookie::Cookie> notify_server_did_request_all_cookies(Badge<WebContentClient>, AK::URL const& url) = 0;
  88. virtual Optional<Web::Cookie::Cookie> notify_server_did_request_named_cookie(Badge<WebContentClient>, AK::URL const& url, DeprecatedString const& name) = 0;
  89. virtual DeprecatedString notify_server_did_request_cookie(Badge<WebContentClient>, const AK::URL& url, Web::Cookie::Source source) = 0;
  90. virtual void notify_server_did_set_cookie(Badge<WebContentClient>, const AK::URL& url, Web::Cookie::ParsedCookie const& cookie, Web::Cookie::Source source) = 0;
  91. virtual void notify_server_did_update_cookie(Badge<WebContentClient>, Web::Cookie::Cookie const& cookie) = 0;
  92. virtual String notify_server_did_request_new_tab(Badge<WebContentClient>, Web::HTML::ActivateTab activate_tab) = 0;
  93. virtual void notify_server_did_request_activate_tab(Badge<WebContentClient>) = 0;
  94. virtual void notify_server_did_close_browsing_context(Badge<WebContentClient>) = 0;
  95. virtual void notify_server_did_update_resource_count(i32 count_waiting) = 0;
  96. virtual void notify_server_did_request_restore_window() = 0;
  97. virtual Gfx::IntPoint notify_server_did_request_reposition_window(Gfx::IntPoint) = 0;
  98. virtual Gfx::IntSize notify_server_did_request_resize_window(Gfx::IntSize) = 0;
  99. virtual Gfx::IntRect notify_server_did_request_maximize_window() = 0;
  100. virtual Gfx::IntRect notify_server_did_request_minimize_window() = 0;
  101. virtual Gfx::IntRect notify_server_did_request_fullscreen_window() = 0;
  102. virtual void notify_server_did_request_file(Badge<WebContentClient>, DeprecatedString const& path, i32) = 0;
  103. virtual void notify_server_did_finish_handling_input_event(bool event_was_accepted) = 0;
  104. protected:
  105. static constexpr auto ZOOM_MIN_LEVEL = 0.3f;
  106. static constexpr auto ZOOM_MAX_LEVEL = 5.0f;
  107. static constexpr auto ZOOM_STEP = 0.1f;
  108. WebContentClient& client();
  109. WebContentClient const& client() const;
  110. virtual void update_zoom() = 0;
  111. virtual void create_client(EnableCallgrindProfiling = EnableCallgrindProfiling::No) {};
  112. #if !defined(AK_OS_SERENITY)
  113. ErrorOr<NonnullRefPtr<WebView::WebContentClient>> launch_web_content_process(ReadonlySpan<String> candidate_web_content_paths, EnableCallgrindProfiling = EnableCallgrindProfiling::No);
  114. #endif
  115. struct SharedBitmap {
  116. i32 id { -1 };
  117. i32 pending_paints { 0 };
  118. RefPtr<Gfx::Bitmap> bitmap;
  119. };
  120. struct ClientState {
  121. RefPtr<WebContentClient> client;
  122. String client_handle;
  123. SharedBitmap front_bitmap;
  124. SharedBitmap back_bitmap;
  125. i32 next_bitmap_id { 0 };
  126. bool has_usable_bitmap { false };
  127. bool got_repaint_requests_while_painting { false };
  128. } m_client_state;
  129. AK::URL m_url;
  130. float m_zoom_level { 1.0 };
  131. float m_device_pixel_ratio { 1.0 };
  132. };
  133. }