Page.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/Noncopyable.h>
  9. #include <AK/OwnPtr.h>
  10. #include <AK/RefPtr.h>
  11. #include <AK/URL.h>
  12. #include <AK/WeakPtr.h>
  13. #include <AK/Weakable.h>
  14. #include <Kernel/API/KeyCode.h>
  15. #include <LibGfx/Forward.h>
  16. #include <LibGfx/Palette.h>
  17. #include <LibGfx/StandardCursor.h>
  18. #include <LibJS/Heap/Handle.h>
  19. #include <LibWeb/CSS/PreferredColorScheme.h>
  20. #include <LibWeb/Forward.h>
  21. #include <LibWeb/Loader/FileRequest.h>
  22. namespace Web {
  23. class PageClient;
  24. class Page : public Weakable<Page> {
  25. AK_MAKE_NONCOPYABLE(Page);
  26. AK_MAKE_NONMOVABLE(Page);
  27. public:
  28. explicit Page(PageClient&);
  29. ~Page();
  30. PageClient& client() { return m_client; }
  31. PageClient const& client() const { return m_client; }
  32. HTML::BrowsingContext& top_level_browsing_context();
  33. HTML::BrowsingContext const& top_level_browsing_context() const;
  34. HTML::BrowsingContext& focused_context();
  35. HTML::BrowsingContext const& focused_context() const { return const_cast<Page*>(this)->focused_context(); }
  36. void set_focused_browsing_context(Badge<EventHandler>, HTML::BrowsingContext&);
  37. void load(const AK::URL&);
  38. void load(LoadRequest&);
  39. void load_html(StringView, const AK::URL&);
  40. bool handle_mouseup(Gfx::IntPoint const&, unsigned button, unsigned modifiers);
  41. bool handle_mousedown(Gfx::IntPoint const&, unsigned button, unsigned modifiers);
  42. bool handle_mousemove(Gfx::IntPoint const&, unsigned buttons, unsigned modifiers);
  43. bool handle_mousewheel(Gfx::IntPoint const&, unsigned button, unsigned modifiers, int wheel_delta_x, int wheel_delta_y);
  44. bool handle_doubleclick(Gfx::IntPoint const&, unsigned buttons, unsigned modifiers);
  45. bool handle_keydown(KeyCode, unsigned modifiers, u32 code_point);
  46. bool handle_keyup(KeyCode, unsigned modifiers, u32 code_point);
  47. Gfx::Palette palette() const;
  48. Gfx::IntRect screen_rect() const;
  49. CSS::PreferredColorScheme preferred_color_scheme() const;
  50. bool is_same_origin_policy_enabled() const { return m_same_origin_policy_enabled; }
  51. void set_same_origin_policy_enabled(bool b) { m_same_origin_policy_enabled = b; }
  52. bool is_scripting_enabled() const { return m_is_scripting_enabled; }
  53. void set_is_scripting_enabled(bool b) { m_is_scripting_enabled = b; }
  54. bool is_webdriver_active() const { return m_is_webdriver_active; }
  55. void set_is_webdriver_active(bool b) { m_is_webdriver_active = b; }
  56. private:
  57. PageClient& m_client;
  58. JS::Handle<HTML::BrowsingContext> m_top_level_browsing_context;
  59. WeakPtr<HTML::BrowsingContext> m_focused_context;
  60. // FIXME: Enable this by default once CORS preflight checks are supported.
  61. bool m_same_origin_policy_enabled { false };
  62. bool m_is_scripting_enabled { true };
  63. // https://w3c.github.io/webdriver/#dfn-webdriver-active-flag
  64. // The webdriver-active flag is set to true when the user agent is under remote control. It is initially false.
  65. bool m_is_webdriver_active { false };
  66. };
  67. class PageClient {
  68. public:
  69. virtual Gfx::Palette palette() const = 0;
  70. virtual Gfx::IntRect screen_rect() const = 0;
  71. virtual CSS::PreferredColorScheme preferred_color_scheme() const = 0;
  72. virtual void page_did_change_title(String const&) { }
  73. virtual void page_did_start_loading(const AK::URL&) { }
  74. virtual void page_did_create_main_document() { }
  75. virtual void page_did_finish_loading(const AK::URL&) { }
  76. virtual void page_did_change_selection() { }
  77. virtual void page_did_request_cursor_change(Gfx::StandardCursor) { }
  78. virtual void page_did_request_context_menu(Gfx::IntPoint const&) { }
  79. virtual void page_did_request_link_context_menu(Gfx::IntPoint const&, const AK::URL&, [[maybe_unused]] String const& target, [[maybe_unused]] unsigned modifiers) { }
  80. virtual void page_did_request_image_context_menu(Gfx::IntPoint const&, const AK::URL&, [[maybe_unused]] String const& target, [[maybe_unused]] unsigned modifiers, Gfx::Bitmap const*) { }
  81. virtual void page_did_click_link(const AK::URL&, [[maybe_unused]] String const& target, [[maybe_unused]] unsigned modifiers) { }
  82. virtual void page_did_middle_click_link(const AK::URL&, [[maybe_unused]] String const& target, [[maybe_unused]] unsigned modifiers) { }
  83. virtual void page_did_enter_tooltip_area(Gfx::IntPoint const&, String const&) { }
  84. virtual void page_did_leave_tooltip_area() { }
  85. virtual void page_did_hover_link(const AK::URL&) { }
  86. virtual void page_did_unhover_link() { }
  87. virtual void page_did_invalidate(Gfx::IntRect const&) { }
  88. virtual void page_did_change_favicon(Gfx::Bitmap const&) { }
  89. virtual void page_did_layout() { }
  90. virtual void page_did_request_scroll(i32, i32) { }
  91. virtual void page_did_request_scroll_to(Gfx::IntPoint const&) { }
  92. virtual void page_did_request_scroll_into_view(Gfx::IntRect const&) { }
  93. virtual void page_did_request_alert(String const&) { }
  94. virtual bool page_did_request_confirm(String const&) { return false; }
  95. virtual String page_did_request_prompt(String const&, String const&) { return {}; }
  96. virtual String page_did_request_cookie(const AK::URL&, Cookie::Source) { return {}; }
  97. virtual void page_did_set_cookie(const AK::URL&, Cookie::ParsedCookie const&, Cookie::Source) { }
  98. virtual void page_did_update_resource_count(i32) { }
  99. virtual void page_did_close_browsing_context(HTML::BrowsingContext const&) { }
  100. virtual void request_file(NonnullRefPtr<FileRequest>&) = 0;
  101. // https://html.spec.whatwg.org/multipage/input.html#show-the-picker,-if-applicable
  102. virtual void page_did_request_file_picker(WeakPtr<DOM::EventTarget>, [[maybe_unused]] bool multiple) {};
  103. protected:
  104. virtual ~PageClient() = default;
  105. };
  106. }