ViewImplementation.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. enum class IsLayoutTestMode {
  23. No,
  24. Yes
  25. };
  26. class ViewImplementation {
  27. public:
  28. virtual ~ViewImplementation() { }
  29. struct DOMNodeProperties {
  30. String computed_style_json;
  31. String resolved_style_json;
  32. String custom_properties_json;
  33. String node_box_sizing_json;
  34. };
  35. AK::URL const& url() const { return m_url; }
  36. String const& handle() const { return m_client_state.client_handle; }
  37. void load(AK::URL const&);
  38. void load_html(StringView, AK::URL const&);
  39. void load_empty_document();
  40. void zoom_in();
  41. void zoom_out();
  42. void reset_zoom();
  43. float zoom_level() const { return m_zoom_level; }
  44. void set_preferred_color_scheme(Web::CSS::PreferredColorScheme);
  45. DeprecatedString selected_text();
  46. void select_all();
  47. void get_source();
  48. void inspect_dom_tree();
  49. void inspect_accessibility_tree();
  50. ErrorOr<DOMNodeProperties> inspect_dom_node(i32 node_id, Optional<Web::CSS::Selector::PseudoElement> pseudo_element);
  51. void clear_inspected_dom_node();
  52. i32 get_hovered_node_id();
  53. void debug_request(DeprecatedString const& request, DeprecatedString const& argument = {});
  54. void run_javascript(StringView);
  55. void js_console_input(DeprecatedString const& js_source);
  56. void js_console_request_messages(i32 start_index);
  57. void toggle_video_play_state();
  58. void toggle_video_loop_state();
  59. void toggle_video_controls_state();
  60. enum class ScreenshotType {
  61. Visible,
  62. Full,
  63. };
  64. ErrorOr<void> take_screenshot(ScreenshotType);
  65. virtual void notify_server_did_layout(Badge<WebContentClient>, Gfx::IntSize content_size) = 0;
  66. virtual void notify_server_did_paint(Badge<WebContentClient>, i32 bitmap_id, Gfx::IntSize) = 0;
  67. virtual void notify_server_did_invalidate_content_rect(Badge<WebContentClient>, Gfx::IntRect const&) = 0;
  68. virtual void notify_server_did_change_selection(Badge<WebContentClient>) = 0;
  69. virtual void notify_server_did_request_cursor_change(Badge<WebContentClient>, Gfx::StandardCursor cursor) = 0;
  70. virtual void notify_server_did_change_title(Badge<WebContentClient>, DeprecatedString const&) = 0;
  71. virtual void notify_server_did_request_scroll(Badge<WebContentClient>, i32, i32) = 0;
  72. virtual void notify_server_did_request_scroll_to(Badge<WebContentClient>, Gfx::IntPoint) = 0;
  73. virtual void notify_server_did_request_scroll_into_view(Badge<WebContentClient>, Gfx::IntRect const&) = 0;
  74. virtual void notify_server_did_enter_tooltip_area(Badge<WebContentClient>, Gfx::IntPoint, DeprecatedString const&) = 0;
  75. virtual void notify_server_did_leave_tooltip_area(Badge<WebContentClient>) = 0;
  76. virtual void notify_server_did_hover_link(Badge<WebContentClient>, const AK::URL&) = 0;
  77. virtual void notify_server_did_unhover_link(Badge<WebContentClient>) = 0;
  78. virtual void notify_server_did_click_link(Badge<WebContentClient>, const AK::URL&, DeprecatedString const& target, unsigned modifiers) = 0;
  79. virtual void notify_server_did_middle_click_link(Badge<WebContentClient>, const AK::URL&, DeprecatedString const& target, unsigned modifiers) = 0;
  80. virtual void notify_server_did_start_loading(Badge<WebContentClient>, const AK::URL&, bool is_redirect) = 0;
  81. virtual void notify_server_did_finish_loading(Badge<WebContentClient>, const AK::URL&) = 0;
  82. virtual void notify_server_did_request_navigate_back(Badge<WebContentClient>) = 0;
  83. virtual void notify_server_did_request_navigate_forward(Badge<WebContentClient>) = 0;
  84. virtual void notify_server_did_request_refresh(Badge<WebContentClient>) = 0;
  85. virtual void notify_server_did_request_context_menu(Badge<WebContentClient>, Gfx::IntPoint) = 0;
  86. virtual void notify_server_did_request_link_context_menu(Badge<WebContentClient>, Gfx::IntPoint, const AK::URL&, DeprecatedString const& target, unsigned modifiers) = 0;
  87. 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;
  88. virtual void notify_server_did_request_video_context_menu(Badge<WebContentClient>, Gfx::IntPoint, const AK::URL&, DeprecatedString const& target, unsigned modifiers, bool is_playing, bool has_user_agent_controls, bool is_looping) = 0;
  89. virtual void notify_server_did_request_alert(Badge<WebContentClient>, String const& message) = 0;
  90. virtual void notify_server_did_request_confirm(Badge<WebContentClient>, String const& message) = 0;
  91. virtual void notify_server_did_request_prompt(Badge<WebContentClient>, String const& message, String const& default_) = 0;
  92. virtual void notify_server_did_request_set_prompt_text(Badge<WebContentClient>, String const& message) = 0;
  93. virtual void notify_server_did_request_accept_dialog(Badge<WebContentClient>) = 0;
  94. virtual void notify_server_did_request_dismiss_dialog(Badge<WebContentClient>) = 0;
  95. virtual void notify_server_did_get_source(const AK::URL& url, DeprecatedString const& source) = 0;
  96. virtual void notify_server_did_get_dom_tree(DeprecatedString const& dom_tree) = 0;
  97. 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;
  98. virtual void notify_server_did_get_accessibility_tree(DeprecatedString const& accessibility_tree) = 0;
  99. virtual void notify_server_did_output_js_console_message(i32 message_index) = 0;
  100. virtual void notify_server_did_get_js_console_messages(i32 start_index, Vector<DeprecatedString> const& message_types, Vector<DeprecatedString> const& messages) = 0;
  101. virtual void notify_server_did_change_favicon(Gfx::Bitmap const& favicon) = 0;
  102. virtual Vector<Web::Cookie::Cookie> notify_server_did_request_all_cookies(Badge<WebContentClient>, AK::URL const& url) = 0;
  103. virtual Optional<Web::Cookie::Cookie> notify_server_did_request_named_cookie(Badge<WebContentClient>, AK::URL const& url, DeprecatedString const& name) = 0;
  104. virtual DeprecatedString notify_server_did_request_cookie(Badge<WebContentClient>, const AK::URL& url, Web::Cookie::Source source) = 0;
  105. virtual void notify_server_did_set_cookie(Badge<WebContentClient>, const AK::URL& url, Web::Cookie::ParsedCookie const& cookie, Web::Cookie::Source source) = 0;
  106. virtual void notify_server_did_update_cookie(Badge<WebContentClient>, Web::Cookie::Cookie const& cookie) = 0;
  107. virtual String notify_server_did_request_new_tab(Badge<WebContentClient>, Web::HTML::ActivateTab activate_tab) = 0;
  108. virtual void notify_server_did_request_activate_tab(Badge<WebContentClient>) = 0;
  109. virtual void notify_server_did_close_browsing_context(Badge<WebContentClient>) = 0;
  110. virtual void notify_server_did_update_resource_count(i32 count_waiting) = 0;
  111. virtual void notify_server_did_request_restore_window() = 0;
  112. virtual Gfx::IntPoint notify_server_did_request_reposition_window(Gfx::IntPoint) = 0;
  113. virtual Gfx::IntSize notify_server_did_request_resize_window(Gfx::IntSize) = 0;
  114. virtual Gfx::IntRect notify_server_did_request_maximize_window() = 0;
  115. virtual Gfx::IntRect notify_server_did_request_minimize_window() = 0;
  116. virtual Gfx::IntRect notify_server_did_request_fullscreen_window() = 0;
  117. virtual void notify_server_did_request_file(Badge<WebContentClient>, DeprecatedString const& path, i32) = 0;
  118. virtual void notify_server_did_finish_handling_input_event(bool event_was_accepted) = 0;
  119. virtual Gfx::IntRect viewport_rect() const = 0;
  120. virtual Gfx::IntPoint to_content_position(Gfx::IntPoint widget_position) const = 0;
  121. virtual Gfx::IntPoint to_widget_position(Gfx::IntPoint content_position) const = 0;
  122. protected:
  123. static constexpr auto ZOOM_MIN_LEVEL = 0.3f;
  124. static constexpr auto ZOOM_MAX_LEVEL = 5.0f;
  125. static constexpr auto ZOOM_STEP = 0.1f;
  126. ViewImplementation();
  127. WebContentClient& client();
  128. WebContentClient const& client() const;
  129. virtual void update_zoom() = 0;
  130. enum class WindowResizeInProgress {
  131. No,
  132. Yes,
  133. };
  134. void resize_backing_stores_if_needed(WindowResizeInProgress);
  135. void request_repaint();
  136. void handle_resize();
  137. virtual void create_client(EnableCallgrindProfiling = EnableCallgrindProfiling::No) {};
  138. #if !defined(AK_OS_SERENITY)
  139. ErrorOr<NonnullRefPtr<WebView::WebContentClient>> launch_web_content_process(ReadonlySpan<String> candidate_web_content_paths, EnableCallgrindProfiling = EnableCallgrindProfiling::No, IsLayoutTestMode = IsLayoutTestMode::No);
  140. #endif
  141. void handle_web_content_process_crash();
  142. struct SharedBitmap {
  143. i32 id { -1 };
  144. i32 pending_paints { 0 };
  145. Gfx::IntSize last_painted_size;
  146. RefPtr<Gfx::Bitmap> bitmap;
  147. };
  148. struct ClientState {
  149. RefPtr<WebContentClient> client;
  150. String client_handle;
  151. SharedBitmap front_bitmap;
  152. SharedBitmap back_bitmap;
  153. i32 next_bitmap_id { 0 };
  154. bool has_usable_bitmap { false };
  155. bool got_repaint_requests_while_painting { false };
  156. } m_client_state;
  157. AK::URL m_url;
  158. float m_zoom_level { 1.0 };
  159. float m_device_pixel_ratio { 1.0 };
  160. RefPtr<Core::Timer> m_backing_store_shrink_timer;
  161. RefPtr<Gfx::Bitmap> m_backup_bitmap;
  162. Gfx::IntSize m_backup_bitmap_size;
  163. };
  164. }