ViewImplementation.h 12 KB

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