ViewImplementation.h 13 KB

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