Page.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/SourceLocation.h>
  7. #include <LibWeb/DOM/Document.h>
  8. #include <LibWeb/HTML/BrowsingContext.h>
  9. #include <LibWeb/HTML/EventLoop/EventLoop.h>
  10. #include <LibWeb/HTML/Scripting/Environments.h>
  11. #include <LibWeb/Page/Page.h>
  12. #include <LibWeb/Platform/EventLoopPlugin.h>
  13. namespace Web {
  14. Page::Page(PageClient& client)
  15. : m_client(client)
  16. {
  17. m_top_level_browsing_context = JS::make_handle(*HTML::BrowsingContext::create_a_new_top_level_browsing_context(*this));
  18. }
  19. Page::~Page() = default;
  20. HTML::BrowsingContext& Page::focused_context()
  21. {
  22. if (m_focused_context)
  23. return *m_focused_context;
  24. return top_level_browsing_context();
  25. }
  26. void Page::set_focused_browsing_context(Badge<EventHandler>, HTML::BrowsingContext& browsing_context)
  27. {
  28. m_focused_context = browsing_context.make_weak_ptr();
  29. }
  30. void Page::load(const AK::URL& url)
  31. {
  32. top_level_browsing_context().loader().load(url, FrameLoader::Type::Navigation);
  33. }
  34. void Page::load(LoadRequest& request)
  35. {
  36. top_level_browsing_context().loader().load(request, FrameLoader::Type::Navigation);
  37. }
  38. void Page::load_html(StringView html, const AK::URL& url)
  39. {
  40. top_level_browsing_context().loader().load_html(html, url);
  41. }
  42. Gfx::Palette Page::palette() const
  43. {
  44. return m_client.palette();
  45. }
  46. Gfx::IntRect Page::screen_rect() const
  47. {
  48. return m_client.screen_rect();
  49. }
  50. CSS::PreferredColorScheme Page::preferred_color_scheme() const
  51. {
  52. return m_client.preferred_color_scheme();
  53. }
  54. bool Page::handle_mousewheel(Gfx::IntPoint const& position, unsigned button, unsigned buttons, unsigned modifiers, int wheel_delta_x, int wheel_delta_y)
  55. {
  56. return top_level_browsing_context().event_handler().handle_mousewheel(position, button, buttons, modifiers, wheel_delta_x, wheel_delta_y);
  57. }
  58. bool Page::handle_mouseup(Gfx::IntPoint const& position, unsigned button, unsigned buttons, unsigned modifiers)
  59. {
  60. return top_level_browsing_context().event_handler().handle_mouseup(position, button, buttons, modifiers);
  61. }
  62. bool Page::handle_mousedown(Gfx::IntPoint const& position, unsigned button, unsigned buttons, unsigned modifiers)
  63. {
  64. return top_level_browsing_context().event_handler().handle_mousedown(position, button, buttons, modifiers);
  65. }
  66. bool Page::handle_mousemove(Gfx::IntPoint const& position, unsigned buttons, unsigned modifiers)
  67. {
  68. return top_level_browsing_context().event_handler().handle_mousemove(position, buttons, modifiers);
  69. }
  70. bool Page::handle_doubleclick(Gfx::IntPoint const& position, unsigned button, unsigned buttons, unsigned modifiers)
  71. {
  72. return top_level_browsing_context().event_handler().handle_doubleclick(position, button, buttons, modifiers);
  73. }
  74. bool Page::handle_keydown(KeyCode key, unsigned modifiers, u32 code_point)
  75. {
  76. return focused_context().event_handler().handle_keydown(key, modifiers, code_point);
  77. }
  78. bool Page::handle_keyup(KeyCode key, unsigned modifiers, u32 code_point)
  79. {
  80. return focused_context().event_handler().handle_keyup(key, modifiers, code_point);
  81. }
  82. HTML::BrowsingContext& Page::top_level_browsing_context()
  83. {
  84. return *m_top_level_browsing_context;
  85. }
  86. HTML::BrowsingContext const& Page::top_level_browsing_context() const
  87. {
  88. return *m_top_level_browsing_context;
  89. }
  90. template<typename ResponseType>
  91. static ResponseType spin_event_loop_until_dialog_closed(PageClient& client, Optional<ResponseType>& response, SourceLocation location = SourceLocation::current())
  92. {
  93. auto& event_loop = Web::HTML::current_settings_object().responsible_event_loop();
  94. ScopeGuard guard { [&] { event_loop.set_execution_paused(false); } };
  95. event_loop.set_execution_paused(true);
  96. Web::Platform::EventLoopPlugin::the().spin_until([&]() {
  97. return response.has_value() || !client.is_connection_open();
  98. });
  99. if (!client.is_connection_open()) {
  100. dbgln("WebContent client disconnected during {}. Exiting peacefully.", location.function_name());
  101. exit(0);
  102. }
  103. return response.release_value();
  104. }
  105. void Page::did_request_alert(String const& message)
  106. {
  107. m_pending_dialog = PendingDialog::Alert;
  108. m_client.page_did_request_alert(message);
  109. if (!message.is_empty())
  110. m_pending_dialog_text = message;
  111. spin_event_loop_until_dialog_closed(m_client, m_pending_alert_response);
  112. }
  113. void Page::alert_closed()
  114. {
  115. if (m_pending_dialog == PendingDialog::Alert) {
  116. m_pending_dialog = PendingDialog::None;
  117. m_pending_alert_response = Empty {};
  118. m_pending_dialog_text.clear();
  119. }
  120. }
  121. bool Page::did_request_confirm(String const& message)
  122. {
  123. m_pending_dialog = PendingDialog::Confirm;
  124. m_client.page_did_request_confirm(message);
  125. if (!message.is_empty())
  126. m_pending_dialog_text = message;
  127. return spin_event_loop_until_dialog_closed(m_client, m_pending_confirm_response);
  128. }
  129. void Page::confirm_closed(bool accepted)
  130. {
  131. if (m_pending_dialog == PendingDialog::Confirm) {
  132. m_pending_dialog = PendingDialog::None;
  133. m_pending_confirm_response = accepted;
  134. m_pending_dialog_text.clear();
  135. }
  136. }
  137. String Page::did_request_prompt(String const& message, String const& default_)
  138. {
  139. m_pending_dialog = PendingDialog::Prompt;
  140. m_client.page_did_request_prompt(message, default_);
  141. if (!message.is_empty())
  142. m_pending_dialog_text = message;
  143. return spin_event_loop_until_dialog_closed(m_client, m_pending_prompt_response);
  144. }
  145. void Page::prompt_closed(String response)
  146. {
  147. if (m_pending_dialog == PendingDialog::Prompt) {
  148. m_pending_dialog = PendingDialog::None;
  149. m_pending_prompt_response = move(response);
  150. m_pending_dialog_text.clear();
  151. }
  152. }
  153. void Page::dismiss_dialog()
  154. {
  155. switch (m_pending_dialog) {
  156. case PendingDialog::None:
  157. break;
  158. case PendingDialog::Alert:
  159. m_client.page_did_request_accept_dialog();
  160. break;
  161. case PendingDialog::Confirm:
  162. case PendingDialog::Prompt:
  163. m_client.page_did_request_dismiss_dialog();
  164. break;
  165. }
  166. }
  167. void Page::accept_dialog()
  168. {
  169. switch (m_pending_dialog) {
  170. case PendingDialog::None:
  171. break;
  172. case PendingDialog::Alert:
  173. case PendingDialog::Confirm:
  174. case PendingDialog::Prompt:
  175. m_client.page_did_request_accept_dialog();
  176. break;
  177. }
  178. }
  179. }