ViewImplementation.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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/Function.h>
  10. #include <AK/String.h>
  11. #include <LibGfx/Forward.h>
  12. #include <LibGfx/StandardCursor.h>
  13. #include <LibWeb/Forward.h>
  14. #include <LibWeb/HTML/ActivateTab.h>
  15. #include <LibWebView/Forward.h>
  16. #include <LibWebView/WebContentClient.h>
  17. namespace WebView {
  18. // Note: This only exists inside Serenity to avoid #ifdefs in all implementors of ViewImplementation.
  19. enum class EnableCallgrindProfiling {
  20. No,
  21. Yes
  22. };
  23. enum class IsLayoutTestMode {
  24. No,
  25. Yes
  26. };
  27. enum class UseJavaScriptBytecode {
  28. No,
  29. Yes
  30. };
  31. class ViewImplementation {
  32. public:
  33. virtual ~ViewImplementation() { }
  34. struct DOMNodeProperties {
  35. String computed_style_json;
  36. String resolved_style_json;
  37. String custom_properties_json;
  38. String node_box_sizing_json;
  39. String aria_properties_state_json;
  40. };
  41. void set_url(Badge<WebContentClient>, AK::URL url) { m_url = move(url); }
  42. AK::URL const& url() const { return m_url; }
  43. String const& handle() const { return m_client_state.client_handle; }
  44. void load(AK::URL const&);
  45. void load_html(StringView, AK::URL const&);
  46. void load_empty_document();
  47. void zoom_in();
  48. void zoom_out();
  49. void reset_zoom();
  50. float zoom_level() const { return m_zoom_level; }
  51. void set_preferred_color_scheme(Web::CSS::PreferredColorScheme);
  52. DeprecatedString selected_text();
  53. void select_all();
  54. void get_source();
  55. void inspect_dom_tree();
  56. void inspect_accessibility_tree();
  57. ErrorOr<DOMNodeProperties> inspect_dom_node(i32 node_id, Optional<Web::CSS::Selector::PseudoElement> pseudo_element);
  58. void clear_inspected_dom_node();
  59. i32 get_hovered_node_id();
  60. UseJavaScriptBytecode use_javascript_bytecode() const { return m_use_javascript_bytecode; }
  61. void debug_request(DeprecatedString const& request, DeprecatedString const& argument = {});
  62. void run_javascript(StringView);
  63. void js_console_input(DeprecatedString const& js_source);
  64. void js_console_request_messages(i32 start_index);
  65. void toggle_media_play_state();
  66. void toggle_media_mute_state();
  67. void toggle_media_loop_state();
  68. void toggle_media_controls_state();
  69. enum class ScreenshotType {
  70. Visible,
  71. Full,
  72. };
  73. ErrorOr<void> take_screenshot(ScreenshotType);
  74. Function<String(Web::HTML::ActivateTab)> on_new_tab;
  75. Function<void()> on_activate_tab;
  76. Function<void()> on_close;
  77. Function<void(Gfx::IntPoint screen_position)> on_context_menu_request;
  78. Function<void(const AK::URL&, Gfx::IntPoint screen_position)> on_link_context_menu_request;
  79. Function<void(const AK::URL&, Gfx::IntPoint screen_position, Gfx::ShareableBitmap const&)> on_image_context_menu_request;
  80. Function<void(Gfx::IntPoint screen_position, Web::Page::MediaContextMenu const&)> on_media_context_menu_request;
  81. Function<void(const AK::URL&)> on_link_hover;
  82. Function<void()> on_link_unhover;
  83. Function<void(const AK::URL&, DeprecatedString const& target, unsigned modifiers)> on_link_click;
  84. Function<void(const AK::URL&, DeprecatedString const& target, unsigned modifiers)> on_link_middle_click;
  85. Function<void(DeprecatedString const&)> on_title_change;
  86. Function<void(const AK::URL&, bool)> on_load_start;
  87. Function<void(const AK::URL&)> on_load_finish;
  88. Function<void()> on_navigate_back;
  89. Function<void()> on_navigate_forward;
  90. Function<void()> on_refresh;
  91. Function<void(Gfx::Bitmap const&)> on_favicon_change;
  92. Function<void(const AK::URL&, DeprecatedString const&)> on_get_source;
  93. Function<void(DeprecatedString const&)> on_get_dom_tree;
  94. Function<void(i32 node_id, DeprecatedString const& computed_style, DeprecatedString const& resolved_style, DeprecatedString const& custom_properties, DeprecatedString const& node_box_sizing, DeprecatedString const& aria_properties_state)> on_get_dom_node_properties;
  95. Function<void(DeprecatedString const&)> on_get_accessibility_tree;
  96. Function<void(i32 message_id)> on_js_console_new_message;
  97. Function<void(i32 start_index, Vector<DeprecatedString> const& message_types, Vector<DeprecatedString> const& messages)> on_get_js_console_messages;
  98. Function<Vector<Web::Cookie::Cookie>(AK::URL const& url)> on_get_all_cookies;
  99. Function<Optional<Web::Cookie::Cookie>(AK::URL const& url, DeprecatedString const& name)> on_get_named_cookie;
  100. Function<DeprecatedString(const AK::URL& url, Web::Cookie::Source source)> on_get_cookie;
  101. Function<void(const AK::URL& url, Web::Cookie::ParsedCookie const& cookie, Web::Cookie::Source source)> on_set_cookie;
  102. Function<void(Web::Cookie::Cookie const& cookie)> on_update_cookie;
  103. Function<void(i32 count_waiting)> on_resource_status_change;
  104. Function<void()> on_restore_window;
  105. Function<Gfx::IntPoint(Gfx::IntPoint)> on_reposition_window;
  106. Function<Gfx::IntSize(Gfx::IntSize)> on_resize_window;
  107. Function<Gfx::IntRect()> on_maximize_window;
  108. Function<Gfx::IntRect()> on_minimize_window;
  109. Function<Gfx::IntRect()> on_fullscreen_window;
  110. Function<void()> on_back_button;
  111. Function<void()> on_forward_button;
  112. virtual void notify_server_did_layout(Badge<WebContentClient>, Gfx::IntSize content_size) = 0;
  113. virtual void notify_server_did_paint(Badge<WebContentClient>, i32 bitmap_id, Gfx::IntSize) = 0;
  114. virtual void notify_server_did_invalidate_content_rect(Badge<WebContentClient>, Gfx::IntRect const&) = 0;
  115. virtual void notify_server_did_change_selection(Badge<WebContentClient>) = 0;
  116. virtual void notify_server_did_request_cursor_change(Badge<WebContentClient>, Gfx::StandardCursor cursor) = 0;
  117. virtual void notify_server_did_request_scroll(Badge<WebContentClient>, i32, i32) = 0;
  118. virtual void notify_server_did_request_scroll_to(Badge<WebContentClient>, Gfx::IntPoint) = 0;
  119. virtual void notify_server_did_request_scroll_into_view(Badge<WebContentClient>, Gfx::IntRect const&) = 0;
  120. virtual void notify_server_did_enter_tooltip_area(Badge<WebContentClient>, Gfx::IntPoint, DeprecatedString const&) = 0;
  121. virtual void notify_server_did_leave_tooltip_area(Badge<WebContentClient>) = 0;
  122. virtual void notify_server_did_request_alert(Badge<WebContentClient>, String const& message) = 0;
  123. virtual void notify_server_did_request_confirm(Badge<WebContentClient>, String const& message) = 0;
  124. virtual void notify_server_did_request_prompt(Badge<WebContentClient>, String const& message, String const& default_) = 0;
  125. virtual void notify_server_did_request_set_prompt_text(Badge<WebContentClient>, String const& message) = 0;
  126. virtual void notify_server_did_request_accept_dialog(Badge<WebContentClient>) = 0;
  127. virtual void notify_server_did_request_dismiss_dialog(Badge<WebContentClient>) = 0;
  128. virtual void notify_server_did_request_file(Badge<WebContentClient>, DeprecatedString const& path, i32) = 0;
  129. virtual void notify_server_did_finish_handling_input_event(bool event_was_accepted) = 0;
  130. virtual Gfx::IntRect viewport_rect() const = 0;
  131. virtual Gfx::IntPoint to_content_position(Gfx::IntPoint widget_position) const = 0;
  132. virtual Gfx::IntPoint to_widget_position(Gfx::IntPoint content_position) const = 0;
  133. protected:
  134. static constexpr auto ZOOM_MIN_LEVEL = 0.3f;
  135. static constexpr auto ZOOM_MAX_LEVEL = 5.0f;
  136. static constexpr auto ZOOM_STEP = 0.1f;
  137. explicit ViewImplementation(UseJavaScriptBytecode);
  138. WebContentClient& client();
  139. WebContentClient const& client() const;
  140. virtual void update_zoom() = 0;
  141. enum class WindowResizeInProgress {
  142. No,
  143. Yes,
  144. };
  145. void resize_backing_stores_if_needed(WindowResizeInProgress);
  146. void request_repaint();
  147. void handle_resize();
  148. virtual void create_client(EnableCallgrindProfiling = EnableCallgrindProfiling::No) { }
  149. #if !defined(AK_OS_SERENITY)
  150. ErrorOr<NonnullRefPtr<WebView::WebContentClient>> launch_web_content_process(ReadonlySpan<String> candidate_web_content_paths, EnableCallgrindProfiling = EnableCallgrindProfiling::No, IsLayoutTestMode = IsLayoutTestMode::No, UseJavaScriptBytecode = UseJavaScriptBytecode::No);
  151. #endif
  152. void handle_web_content_process_crash();
  153. struct SharedBitmap {
  154. i32 id { -1 };
  155. i32 pending_paints { 0 };
  156. Gfx::IntSize last_painted_size;
  157. RefPtr<Gfx::Bitmap> bitmap;
  158. };
  159. struct ClientState {
  160. RefPtr<WebContentClient> client;
  161. String client_handle;
  162. SharedBitmap front_bitmap;
  163. SharedBitmap back_bitmap;
  164. i32 next_bitmap_id { 0 };
  165. bool has_usable_bitmap { false };
  166. bool got_repaint_requests_while_painting { false };
  167. } m_client_state;
  168. AK::URL m_url;
  169. float m_zoom_level { 1.0 };
  170. float m_device_pixel_ratio { 1.0 };
  171. RefPtr<Core::Timer> m_backing_store_shrink_timer;
  172. RefPtr<Gfx::Bitmap> m_backup_bitmap;
  173. Gfx::IntSize m_backup_bitmap_size;
  174. size_t m_crash_count = 0;
  175. RefPtr<Core::Timer> m_repeated_crash_timer;
  176. UseJavaScriptBytecode m_use_javascript_bytecode {};
  177. };
  178. }