WebContentView.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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/DeprecatedString.h>
  9. #include <AK/Function.h>
  10. #include <AK/HashMap.h>
  11. #include <AK/OwnPtr.h>
  12. #include <AK/URL.h>
  13. #include <LibGfx/Forward.h>
  14. #include <LibGfx/Rect.h>
  15. #include <LibGfx/StandardCursor.h>
  16. #include <LibWeb/CSS/PreferredColorScheme.h>
  17. #include <LibWeb/CSS/Selector.h>
  18. #include <LibWeb/Forward.h>
  19. #include <LibWeb/HTML/ActivateTab.h>
  20. #include <LibWebView/ViewImplementation.h>
  21. #include <QAbstractScrollArea>
  22. #include <QPointer>
  23. class QTextEdit;
  24. class QLineEdit;
  25. namespace Ladybird {
  26. class ConsoleWidget;
  27. class InspectorWidget;
  28. }
  29. namespace WebView {
  30. class WebContentClient;
  31. }
  32. using WebView::WebContentClient;
  33. class Tab;
  34. class WebContentView final
  35. : public QAbstractScrollArea
  36. , public WebView::ViewImplementation {
  37. Q_OBJECT
  38. public:
  39. explicit WebContentView(StringView webdriver_content_ipc_path, WebView::EnableCallgrindProfiling);
  40. virtual ~WebContentView() override;
  41. Function<String(Web::HTML::ActivateTab)> on_new_tab;
  42. Function<String(const AK::URL&, Web::HTML::ActivateTab)> on_tab_open_request;
  43. Function<void()> on_close;
  44. Function<void(Gfx::IntPoint screen_position)> on_context_menu_request;
  45. Function<void(const AK::URL&, DeprecatedString const& target, unsigned modifiers)> on_link_click;
  46. Function<void(const AK::URL&, Gfx::IntPoint screen_position)> on_link_context_menu_request;
  47. Function<void(const AK::URL&, Gfx::IntPoint screen_position, Gfx::ShareableBitmap const&)> on_image_context_menu_request;
  48. Function<void(const AK::URL&, DeprecatedString const& target, unsigned modifiers)> on_link_middle_click;
  49. Function<void(const AK::URL&)> on_link_hover;
  50. Function<void(DeprecatedString const&)> on_title_change;
  51. Function<void(const AK::URL&)> on_load_start;
  52. Function<void(const AK::URL&)> on_load_finish;
  53. Function<void(Gfx::Bitmap const&)> on_favicon_change;
  54. Function<void(const AK::URL&)> on_url_drop;
  55. Function<void(Web::DOM::Document*)> on_set_document;
  56. Function<void(const AK::URL&, DeprecatedString const&)> on_get_source;
  57. Function<void(DeprecatedString const&)> on_get_dom_tree;
  58. Function<void(i32 node_id, DeprecatedString const& specified_style, DeprecatedString const& computed_style, DeprecatedString const& custom_properties, DeprecatedString const& node_box_sizing)> on_get_dom_node_properties;
  59. Function<void(i32 message_id)> on_js_console_new_message;
  60. Function<void(i32 start_index, Vector<DeprecatedString> const& message_types, Vector<DeprecatedString> const& messages)> on_get_js_console_messages;
  61. Function<Vector<Web::Cookie::Cookie>(AK::URL const& url)> on_get_all_cookies;
  62. Function<Optional<Web::Cookie::Cookie>(AK::URL const& url, DeprecatedString const& name)> on_get_named_cookie;
  63. Function<DeprecatedString(const AK::URL& url, Web::Cookie::Source source)> on_get_cookie;
  64. Function<void(const AK::URL& url, Web::Cookie::ParsedCookie const& cookie, Web::Cookie::Source source)> on_set_cookie;
  65. Function<void(Web::Cookie::Cookie const& cookie)> on_update_cookie;
  66. Function<void(i32 count_waiting)> on_resource_status_change;
  67. virtual void paintEvent(QPaintEvent*) override;
  68. virtual void resizeEvent(QResizeEvent*) override;
  69. virtual void mouseMoveEvent(QMouseEvent*) override;
  70. virtual void mousePressEvent(QMouseEvent*) override;
  71. virtual void mouseReleaseEvent(QMouseEvent*) override;
  72. virtual void dragEnterEvent(QDragEnterEvent*) override;
  73. virtual void dropEvent(QDropEvent*) override;
  74. virtual void keyPressEvent(QKeyEvent* event) override;
  75. virtual void keyReleaseEvent(QKeyEvent* event) override;
  76. virtual void showEvent(QShowEvent*) override;
  77. virtual void hideEvent(QHideEvent*) override;
  78. virtual void focusInEvent(QFocusEvent*) override;
  79. virtual void focusOutEvent(QFocusEvent*) override;
  80. virtual bool event(QEvent*) override;
  81. void did_output_js_console_message(i32 message_index);
  82. void did_get_js_console_messages(i32 start_index, Vector<DeprecatedString> message_types, Vector<DeprecatedString> messages);
  83. void show_js_console();
  84. enum class InspectorTarget {
  85. Document,
  86. HoveredElement
  87. };
  88. void show_inspector(InspectorTarget = InspectorTarget::Document);
  89. Ladybird::ConsoleWidget* console() { return m_console_widget; };
  90. ErrorOr<String> dump_layout_tree();
  91. void set_viewport_rect(Gfx::IntRect);
  92. void set_window_size(Gfx::IntSize);
  93. void set_window_position(Gfx::IntPoint);
  94. Gfx::IntPoint to_content(Gfx::IntPoint) const;
  95. Gfx::IntPoint to_widget(Gfx::IntPoint) const;
  96. enum class PaletteMode {
  97. Default,
  98. Dark,
  99. };
  100. void update_palette(PaletteMode = PaletteMode::Default);
  101. virtual void notify_server_did_layout(Badge<WebContentClient>, Gfx::IntSize content_size) override;
  102. virtual void notify_server_did_paint(Badge<WebContentClient>, i32 bitmap_id) override;
  103. virtual void notify_server_did_invalidate_content_rect(Badge<WebContentClient>, Gfx::IntRect const&) override;
  104. virtual void notify_server_did_change_selection(Badge<WebContentClient>) override;
  105. virtual void notify_server_did_request_cursor_change(Badge<WebContentClient>, Gfx::StandardCursor cursor) override;
  106. virtual void notify_server_did_change_title(Badge<WebContentClient>, DeprecatedString const&) override;
  107. virtual void notify_server_did_request_scroll(Badge<WebContentClient>, i32, i32) override;
  108. virtual void notify_server_did_request_scroll_to(Badge<WebContentClient>, Gfx::IntPoint) override;
  109. virtual void notify_server_did_request_scroll_into_view(Badge<WebContentClient>, Gfx::IntRect const&) override;
  110. virtual void notify_server_did_enter_tooltip_area(Badge<WebContentClient>, Gfx::IntPoint, DeprecatedString const&) override;
  111. virtual void notify_server_did_leave_tooltip_area(Badge<WebContentClient>) override;
  112. virtual void notify_server_did_hover_link(Badge<WebContentClient>, const AK::URL&) override;
  113. virtual void notify_server_did_unhover_link(Badge<WebContentClient>) override;
  114. virtual void notify_server_did_click_link(Badge<WebContentClient>, const AK::URL&, DeprecatedString const& target, unsigned modifiers) override;
  115. virtual void notify_server_did_middle_click_link(Badge<WebContentClient>, const AK::URL&, DeprecatedString const& target, unsigned modifiers) override;
  116. virtual void notify_server_did_start_loading(Badge<WebContentClient>, const AK::URL&, bool) override;
  117. virtual void notify_server_did_finish_loading(Badge<WebContentClient>, const AK::URL&) override;
  118. virtual void notify_server_did_request_navigate_back(Badge<WebContentClient>) override;
  119. virtual void notify_server_did_request_navigate_forward(Badge<WebContentClient>) override;
  120. virtual void notify_server_did_request_refresh(Badge<WebContentClient>) override;
  121. virtual void notify_server_did_request_context_menu(Badge<WebContentClient>, Gfx::IntPoint) override;
  122. virtual void notify_server_did_request_link_context_menu(Badge<WebContentClient>, Gfx::IntPoint, const AK::URL&, DeprecatedString const& target, unsigned modifiers) override;
  123. virtual void notify_server_did_request_image_context_menu(Badge<WebContentClient>, Gfx::IntPoint, const AK::URL&, DeprecatedString const& target, unsigned modifiers, Gfx::ShareableBitmap const&) override;
  124. virtual void notify_server_did_request_alert(Badge<WebContentClient>, String const& message) override;
  125. virtual void notify_server_did_request_confirm(Badge<WebContentClient>, String const& message) override;
  126. virtual void notify_server_did_request_prompt(Badge<WebContentClient>, String const& message, String const& default_) override;
  127. virtual void notify_server_did_request_set_prompt_text(Badge<WebContentClient>, String const& message) override;
  128. virtual void notify_server_did_request_accept_dialog(Badge<WebContentClient>) override;
  129. virtual void notify_server_did_request_dismiss_dialog(Badge<WebContentClient>) override;
  130. virtual void notify_server_did_get_source(const AK::URL& url, DeprecatedString const& source) override;
  131. virtual void notify_server_did_get_dom_tree(DeprecatedString const& dom_tree) override;
  132. virtual void notify_server_did_get_dom_node_properties(i32 node_id, DeprecatedString const& specified_style, DeprecatedString const& computed_style, DeprecatedString const& custom_properties, DeprecatedString const& node_box_sizing) override;
  133. virtual void notify_server_did_get_accessibility_tree(DeprecatedString const& accessibility_tree) override;
  134. virtual void notify_server_did_output_js_console_message(i32 message_index) override;
  135. virtual void notify_server_did_get_js_console_messages(i32 start_index, Vector<DeprecatedString> const& message_types, Vector<DeprecatedString> const& messages) override;
  136. virtual void notify_server_did_change_favicon(Gfx::Bitmap const& favicon) override;
  137. virtual Vector<Web::Cookie::Cookie> notify_server_did_request_all_cookies(Badge<WebContentClient>, AK::URL const& url) override;
  138. virtual Optional<Web::Cookie::Cookie> notify_server_did_request_named_cookie(Badge<WebContentClient>, AK::URL const& url, DeprecatedString const& name) override;
  139. virtual DeprecatedString notify_server_did_request_cookie(Badge<WebContentClient>, const AK::URL& url, Web::Cookie::Source source) override;
  140. virtual void notify_server_did_set_cookie(Badge<WebContentClient>, const AK::URL& url, Web::Cookie::ParsedCookie const& cookie, Web::Cookie::Source source) override;
  141. virtual void notify_server_did_update_cookie(Badge<WebContentClient>, Web::Cookie::Cookie const& cookie) override;
  142. virtual String notify_server_did_request_new_tab(Badge<WebContentClient>, Web::HTML::ActivateTab activate_tab) override;
  143. virtual void notify_server_did_request_activate_tab(Badge<WebContentClient>) override;
  144. virtual void notify_server_did_close_browsing_context(Badge<WebContentClient>) override;
  145. virtual void notify_server_did_update_resource_count(i32 count_waiting) override;
  146. virtual void notify_server_did_request_restore_window() override;
  147. virtual Gfx::IntPoint notify_server_did_request_reposition_window(Gfx::IntPoint) override;
  148. virtual Gfx::IntSize notify_server_did_request_resize_window(Gfx::IntSize) override;
  149. virtual Gfx::IntRect notify_server_did_request_maximize_window() override;
  150. virtual Gfx::IntRect notify_server_did_request_minimize_window() override;
  151. virtual Gfx::IntRect notify_server_did_request_fullscreen_window() override;
  152. virtual void notify_server_did_request_file(Badge<WebContentClient>, DeprecatedString const& path, i32) override;
  153. virtual void notify_server_did_finish_handling_input_event(bool event_was_accepted) override;
  154. signals:
  155. void activate_tab();
  156. void close();
  157. void link_hovered(QString, int timeout = 0);
  158. void link_unhovered();
  159. void back_mouse_button();
  160. void forward_mouse_button();
  161. void load_started(const URL&, bool);
  162. void title_changed(QString);
  163. void favicon_changed(QIcon);
  164. void got_source(URL, QString);
  165. void navigate_back();
  166. void navigate_forward();
  167. void refresh();
  168. void restore_window();
  169. void urls_dropped(QList<QUrl> const&);
  170. Gfx::IntPoint reposition_window(Gfx::IntPoint);
  171. Gfx::IntSize resize_window(Gfx::IntSize);
  172. Gfx::IntRect maximize_window();
  173. Gfx::IntRect minimize_window();
  174. Gfx::IntRect fullscreen_window();
  175. private:
  176. // ^WebView::ViewImplementation
  177. virtual void create_client(WebView::EnableCallgrindProfiling = WebView::EnableCallgrindProfiling::No) override;
  178. virtual void update_zoom() override;
  179. void request_repaint();
  180. void update_viewport_rect();
  181. void handle_resize();
  182. void ensure_js_console_widget();
  183. void ensure_inspector_widget();
  184. bool is_inspector_open() const;
  185. void close_sub_widgets();
  186. qreal m_inverse_pixel_scaling_ratio { 1.0 };
  187. bool m_should_show_line_box_borders { false };
  188. QPointer<QDialog> m_dialog;
  189. Ladybird::ConsoleWidget* m_console_widget { nullptr };
  190. Ladybird::InspectorWidget* m_inspector_widget { nullptr };
  191. Gfx::IntRect m_viewport_rect;
  192. void handle_web_content_process_crash();
  193. RefPtr<Gfx::Bitmap> m_backup_bitmap;
  194. StringView m_webdriver_content_ipc_path;
  195. };