ViewImplementation.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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/LexicalPath.h>
  11. #include <AK/Queue.h>
  12. #include <AK/String.h>
  13. #include <LibCore/Promise.h>
  14. #include <LibGfx/Forward.h>
  15. #include <LibGfx/StandardCursor.h>
  16. #include <LibWeb/Forward.h>
  17. #include <LibWeb/HTML/ActivateTab.h>
  18. #include <LibWeb/HTML/AudioPlayState.h>
  19. #include <LibWeb/HTML/ColorPickerUpdateState.h>
  20. #include <LibWeb/HTML/FileFilter.h>
  21. #include <LibWeb/HTML/SelectItem.h>
  22. #include <LibWeb/Page/InputEvent.h>
  23. #include <LibWebView/Forward.h>
  24. #include <LibWebView/WebContentClient.h>
  25. namespace WebView {
  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. String aria_properties_state_json;
  35. };
  36. void set_url(Badge<WebContentClient>, URL::URL url) { m_url = move(url); }
  37. URL::URL const& url() const { return m_url; }
  38. String const& handle() const { return m_client_state.client_handle; }
  39. void server_did_paint(Badge<WebContentClient>, i32 bitmap_id, Gfx::IntSize size);
  40. void load(URL::URL const&);
  41. void load_html(StringView);
  42. void load_empty_document();
  43. void reload();
  44. void traverse_the_history_by_delta(int delta);
  45. void zoom_in();
  46. void zoom_out();
  47. void reset_zoom();
  48. float zoom_level() const { return m_zoom_level; }
  49. float device_pixel_ratio() const { return m_device_pixel_ratio; }
  50. void enqueue_input_event(Web::InputEvent);
  51. void did_finish_handling_input_event(Badge<WebContentClient>, bool event_was_accepted);
  52. void set_preferred_color_scheme(Web::CSS::PreferredColorScheme);
  53. ByteString selected_text();
  54. Optional<String> selected_text_with_whitespace_collapsed();
  55. void select_all();
  56. void find_in_page(String const& query, CaseSensitivity = CaseSensitivity::CaseInsensitive);
  57. void find_in_page_next_match();
  58. void find_in_page_previous_match();
  59. void paste(String const&);
  60. void get_source();
  61. void inspect_dom_tree();
  62. void inspect_dom_node(i32 node_id, Optional<Web::CSS::Selector::PseudoElement::Type> pseudo_element);
  63. void inspect_accessibility_tree();
  64. void clear_inspected_dom_node();
  65. void get_hovered_node_id();
  66. void set_dom_node_text(i32 node_id, String text);
  67. void set_dom_node_tag(i32 node_id, String name);
  68. void add_dom_node_attributes(i32 node_id, Vector<Attribute> attributes);
  69. void replace_dom_node_attribute(i32 node_id, String name, Vector<Attribute> replacement_attributes);
  70. void create_child_element(i32 node_id);
  71. void create_child_text_node(i32 node_id);
  72. void clone_dom_node(i32 node_id);
  73. void remove_dom_node(i32 node_id);
  74. void get_dom_node_html(i32 node_id);
  75. void debug_request(ByteString const& request, ByteString const& argument = {});
  76. void run_javascript(StringView);
  77. void js_console_input(ByteString const& js_source);
  78. void js_console_request_messages(i32 start_index);
  79. void alert_closed();
  80. void confirm_closed(bool accepted);
  81. void prompt_closed(Optional<String> response);
  82. void color_picker_update(Optional<Color> picked_color, Web::HTML::ColorPickerUpdateState state);
  83. void file_picker_closed(Vector<Web::HTML::SelectedFile> selected_files);
  84. void select_dropdown_closed(Optional<u32> const& selected_item_id);
  85. void toggle_media_play_state();
  86. void toggle_media_mute_state();
  87. void toggle_media_loop_state();
  88. void toggle_media_controls_state();
  89. Web::HTML::MuteState page_mute_state() const { return m_mute_state; }
  90. void toggle_page_mute_state();
  91. void did_change_audio_play_state(Badge<WebContentClient>, Web::HTML::AudioPlayState);
  92. Web::HTML::AudioPlayState audio_play_state() const { return m_audio_play_state; }
  93. void did_update_navigation_buttons_state(Badge<WebContentClient>, bool back_enabled, bool forward_enabled) const;
  94. enum class ScreenshotType {
  95. Visible,
  96. Full,
  97. };
  98. NonnullRefPtr<Core::Promise<LexicalPath>> take_screenshot(ScreenshotType);
  99. NonnullRefPtr<Core::Promise<LexicalPath>> take_dom_node_screenshot(i32);
  100. virtual void did_receive_screenshot(Badge<WebContentClient>, Gfx::ShareableBitmap const&);
  101. ErrorOr<LexicalPath> dump_gc_graph();
  102. void set_user_style_sheet(String source);
  103. // Load Native.css as the User style sheet, which attempts to make WebView content look as close to
  104. // native GUI widgets as possible.
  105. void use_native_user_style_sheet();
  106. void enable_inspector_prototype();
  107. Function<void(Gfx::IntSize)> on_did_layout;
  108. Function<void()> on_ready_to_paint;
  109. Function<String(Web::HTML::ActivateTab, Web::HTML::WebViewHints, Optional<u64>)> on_new_web_view;
  110. Function<void()> on_activate_tab;
  111. Function<void()> on_close;
  112. Function<void(Gfx::IntPoint screen_position)> on_context_menu_request;
  113. Function<void(URL::URL const&, Gfx::IntPoint screen_position)> on_link_context_menu_request;
  114. Function<void(URL::URL const&, Gfx::IntPoint screen_position, Gfx::ShareableBitmap const&)> on_image_context_menu_request;
  115. Function<void(Gfx::IntPoint screen_position, Web::Page::MediaContextMenu const&)> on_media_context_menu_request;
  116. Function<void(URL::URL const&)> on_link_hover;
  117. Function<void()> on_link_unhover;
  118. Function<void(URL::URL const&, ByteString const& target, unsigned modifiers)> on_link_click;
  119. Function<void(URL::URL const&, ByteString const& target, unsigned modifiers)> on_link_middle_click;
  120. Function<void(ByteString const&)> on_title_change;
  121. Function<void(URL::URL const&)> on_url_change;
  122. Function<void(URL::URL const&, bool)> on_load_start;
  123. Function<void(URL::URL const&)> on_load_finish;
  124. Function<void(ByteString const& path, i32)> on_request_file;
  125. Function<void()> on_navigate_back;
  126. Function<void()> on_navigate_forward;
  127. Function<void()> on_refresh;
  128. Function<void(Gfx::Bitmap const&)> on_favicon_change;
  129. Function<void(Gfx::StandardCursor)> on_cursor_change;
  130. Function<void(Gfx::IntPoint, ByteString const&)> on_enter_tooltip_area;
  131. Function<void()> on_leave_tooltip_area;
  132. Function<void(String const& message)> on_request_alert;
  133. Function<void(String const& message)> on_request_confirm;
  134. Function<void(String const& message, String const& default_)> on_request_prompt;
  135. Function<void(String const& message)> on_request_set_prompt_text;
  136. Function<void()> on_request_accept_dialog;
  137. Function<void()> on_request_dismiss_dialog;
  138. Function<void(URL::URL const&, ByteString const&)> on_received_source;
  139. Function<void(ByteString const&)> on_received_dom_tree;
  140. Function<void(Optional<DOMNodeProperties>)> on_received_dom_node_properties;
  141. Function<void(ByteString const&)> on_received_accessibility_tree;
  142. Function<void(i32 node_id)> on_received_hovered_node_id;
  143. Function<void(Optional<i32> const& node_id)> on_finshed_editing_dom_node;
  144. Function<void(String const&)> on_received_dom_node_html;
  145. Function<void(i32 message_id)> on_received_console_message;
  146. Function<void(i32 start_index, Vector<ByteString> const& message_types, Vector<ByteString> const& messages)> on_received_console_messages;
  147. Function<Vector<Web::Cookie::Cookie>(URL::URL const& url)> on_get_all_cookies;
  148. Function<Optional<Web::Cookie::Cookie>(URL::URL const& url, String const& name)> on_get_named_cookie;
  149. Function<String(URL::URL const& url, Web::Cookie::Source source)> on_get_cookie;
  150. Function<void(URL::URL const& url, Web::Cookie::ParsedCookie const& cookie, Web::Cookie::Source source)> on_set_cookie;
  151. Function<void(Web::Cookie::Cookie const& cookie)> on_update_cookie;
  152. Function<void(i32 count_waiting)> on_resource_status_change;
  153. Function<void()> on_restore_window;
  154. Function<Gfx::IntPoint(Gfx::IntPoint)> on_reposition_window;
  155. Function<Gfx::IntSize(Gfx::IntSize)> on_resize_window;
  156. Function<Gfx::IntRect()> on_maximize_window;
  157. Function<Gfx::IntRect()> on_minimize_window;
  158. Function<Gfx::IntRect()> on_fullscreen_window;
  159. Function<void(Color current_color)> on_request_color_picker;
  160. Function<void(Web::HTML::FileFilter const& accepted_file_types, Web::HTML::AllowMultipleFiles)> on_request_file_picker;
  161. Function<void(Gfx::IntPoint content_position, i32 minimum_width, Vector<Web::HTML::SelectItem> items)> on_request_select_dropdown;
  162. Function<void(Web::KeyEvent const&)> on_finish_handling_key_event;
  163. Function<void()> on_text_test_finish;
  164. Function<void(Gfx::Color)> on_theme_color_change;
  165. Function<void(String const&, String const&, String const&)> on_insert_clipboard_entry;
  166. Function<void(Web::HTML::AudioPlayState)> on_audio_play_state_changed;
  167. Function<void(bool, bool)> on_navigation_buttons_state_changed;
  168. Function<void()> on_inspector_loaded;
  169. Function<void(i32, Optional<Web::CSS::Selector::PseudoElement::Type> const&)> on_inspector_selected_dom_node;
  170. Function<void(i32, String const&)> on_inspector_set_dom_node_text;
  171. Function<void(i32, String const&)> on_inspector_set_dom_node_tag;
  172. Function<void(i32, Vector<Attribute> const&)> on_inspector_added_dom_node_attributes;
  173. Function<void(i32, size_t, Vector<Attribute> const&)> on_inspector_replaced_dom_node_attribute;
  174. Function<void(i32, Gfx::IntPoint, String const&, Optional<String> const&, Optional<size_t> const&)> on_inspector_requested_dom_tree_context_menu;
  175. Function<void(String const&)> on_inspector_executed_console_script;
  176. Function<IPC::File()> on_request_worker_agent;
  177. virtual Web::DevicePixelSize viewport_size() const = 0;
  178. virtual Gfx::IntPoint to_content_position(Gfx::IntPoint widget_position) const = 0;
  179. virtual Gfx::IntPoint to_widget_position(Gfx::IntPoint content_position) const = 0;
  180. protected:
  181. static constexpr auto ZOOM_MIN_LEVEL = 0.3f;
  182. static constexpr auto ZOOM_MAX_LEVEL = 5.0f;
  183. static constexpr auto ZOOM_STEP = 0.1f;
  184. ViewImplementation();
  185. WebContentClient& client();
  186. WebContentClient const& client() const;
  187. u64 page_id() const;
  188. virtual void update_zoom() = 0;
  189. enum class WindowResizeInProgress {
  190. No,
  191. Yes,
  192. };
  193. void resize_backing_stores_if_needed(WindowResizeInProgress);
  194. void handle_resize();
  195. enum class CreateNewClient {
  196. No,
  197. Yes,
  198. };
  199. virtual void initialize_client(CreateNewClient = CreateNewClient::Yes) { }
  200. void handle_web_content_process_crash();
  201. struct SharedBitmap {
  202. i32 id { -1 };
  203. Web::DevicePixelSize last_painted_size;
  204. RefPtr<Gfx::Bitmap> bitmap;
  205. };
  206. struct ClientState {
  207. RefPtr<WebContentClient> client;
  208. String client_handle;
  209. SharedBitmap front_bitmap;
  210. SharedBitmap back_bitmap;
  211. u64 page_index { 0 };
  212. i32 next_bitmap_id { 0 };
  213. bool has_usable_bitmap { false };
  214. } m_client_state;
  215. URL::URL m_url;
  216. float m_zoom_level { 1.0 };
  217. float m_device_pixel_ratio { 1.0 };
  218. Queue<Web::InputEvent> m_pending_input_events;
  219. RefPtr<Core::Timer> m_backing_store_shrink_timer;
  220. RefPtr<Gfx::Bitmap> m_backup_bitmap;
  221. Web::DevicePixelSize m_backup_bitmap_size;
  222. size_t m_crash_count = 0;
  223. RefPtr<Core::Timer> m_repeated_crash_timer;
  224. RefPtr<Core::Promise<LexicalPath>> m_pending_screenshot;
  225. Web::HTML::AudioPlayState m_audio_play_state { Web::HTML::AudioPlayState::Paused };
  226. size_t m_number_of_elements_playing_audio { 0 };
  227. Web::HTML::MuteState m_mute_state { Web::HTML::MuteState::Unmuted };
  228. };
  229. }