ViewImplementation.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * Copyright (c) 2023, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Error.h>
  7. #include <AK/String.h>
  8. #include <LibWebView/ViewImplementation.h>
  9. namespace WebView {
  10. WebContentClient& ViewImplementation::client()
  11. {
  12. VERIFY(m_client_state.client);
  13. return *m_client_state.client;
  14. }
  15. WebContentClient const& ViewImplementation::client() const
  16. {
  17. VERIFY(m_client_state.client);
  18. return *m_client_state.client;
  19. }
  20. void ViewImplementation::load(AK::URL const& url)
  21. {
  22. m_url = url;
  23. client().async_load_url(url);
  24. }
  25. void ViewImplementation::load_html(StringView html, AK::URL const& url)
  26. {
  27. m_url = url;
  28. client().async_load_html(html, url);
  29. }
  30. void ViewImplementation::load_empty_document()
  31. {
  32. load_html(""sv, {});
  33. }
  34. void ViewImplementation::zoom_in()
  35. {
  36. if (m_zoom_level >= ZOOM_MAX_LEVEL)
  37. return;
  38. m_zoom_level += ZOOM_STEP;
  39. update_zoom();
  40. }
  41. void ViewImplementation::zoom_out()
  42. {
  43. if (m_zoom_level <= ZOOM_MIN_LEVEL)
  44. return;
  45. m_zoom_level -= ZOOM_STEP;
  46. update_zoom();
  47. }
  48. void ViewImplementation::reset_zoom()
  49. {
  50. m_zoom_level = 1.0f;
  51. update_zoom();
  52. }
  53. void ViewImplementation::set_preferred_color_scheme(Web::CSS::PreferredColorScheme color_scheme)
  54. {
  55. client().async_set_preferred_color_scheme(color_scheme);
  56. }
  57. DeprecatedString ViewImplementation::selected_text()
  58. {
  59. return client().get_selected_text();
  60. }
  61. void ViewImplementation::select_all()
  62. {
  63. client().async_select_all();
  64. }
  65. void ViewImplementation::get_source()
  66. {
  67. client().async_get_source();
  68. }
  69. void ViewImplementation::inspect_dom_tree()
  70. {
  71. client().async_inspect_dom_tree();
  72. }
  73. void ViewImplementation::inspect_accessibility_tree()
  74. {
  75. client().async_inspect_accessibility_tree();
  76. }
  77. ErrorOr<ViewImplementation::DOMNodeProperties> ViewImplementation::inspect_dom_node(i32 node_id, Optional<Web::CSS::Selector::PseudoElement> pseudo_element)
  78. {
  79. auto response = client().inspect_dom_node(node_id, pseudo_element);
  80. if (!response.has_style())
  81. return Error::from_string_view("Inspected node returned no style"sv);
  82. return DOMNodeProperties {
  83. .computed_style_json = TRY(String::from_deprecated_string(response.take_computed_style())),
  84. .resolved_style_json = TRY(String::from_deprecated_string(response.take_resolved_style())),
  85. .custom_properties_json = TRY(String::from_deprecated_string(response.take_custom_properties())),
  86. .node_box_sizing_json = TRY(String::from_deprecated_string(response.take_node_box_sizing())),
  87. };
  88. }
  89. void ViewImplementation::clear_inspected_dom_node()
  90. {
  91. client().inspect_dom_node(0, {});
  92. }
  93. i32 ViewImplementation::get_hovered_node_id()
  94. {
  95. return client().get_hovered_node_id();
  96. }
  97. void ViewImplementation::debug_request(DeprecatedString const& request, DeprecatedString const& argument)
  98. {
  99. client().async_debug_request(request, argument);
  100. }
  101. void ViewImplementation::run_javascript(StringView js_source)
  102. {
  103. client().async_run_javascript(js_source);
  104. }
  105. #if !defined(AK_OS_SERENITY)
  106. ErrorOr<NonnullRefPtr<WebView::WebContentClient>> ViewImplementation::launch_web_content_process(ReadonlySpan<String> candidate_web_content_paths, EnableCallgrindProfiling enable_callgrind_profiling)
  107. {
  108. int socket_fds[2] {};
  109. TRY(Core::System::socketpair(AF_LOCAL, SOCK_STREAM, 0, socket_fds));
  110. int ui_fd = socket_fds[0];
  111. int wc_fd = socket_fds[1];
  112. int fd_passing_socket_fds[2] {};
  113. TRY(Core::System::socketpair(AF_LOCAL, SOCK_STREAM, 0, fd_passing_socket_fds));
  114. int ui_fd_passing_fd = fd_passing_socket_fds[0];
  115. int wc_fd_passing_fd = fd_passing_socket_fds[1];
  116. if (auto child_pid = TRY(Core::System::fork()); child_pid == 0) {
  117. TRY(Core::System::close(ui_fd_passing_fd));
  118. TRY(Core::System::close(ui_fd));
  119. auto takeover_string = TRY(String::formatted("WebContent:{}", wc_fd));
  120. TRY(Core::System::setenv("SOCKET_TAKEOVER"sv, takeover_string, true));
  121. auto webcontent_fd_passing_socket_string = TRY(String::number(wc_fd_passing_fd));
  122. ErrorOr<void> result;
  123. for (auto const& path : candidate_web_content_paths) {
  124. constexpr auto callgrind_prefix_length = 3;
  125. auto arguments_with_callgrind_prefix = Array {
  126. "valgrind"sv,
  127. "--tool=callgrind"sv,
  128. "--instr-atstart=no"sv,
  129. path.bytes_as_string_view(),
  130. "--webcontent-fd-passing-socket"sv,
  131. webcontent_fd_passing_socket_string
  132. };
  133. auto arguments = arguments_with_callgrind_prefix.span();
  134. if (enable_callgrind_profiling == EnableCallgrindProfiling::No)
  135. arguments = arguments.slice(callgrind_prefix_length);
  136. result = Core::System::exec(arguments[0], arguments, Core::System::SearchInPath::Yes);
  137. if (!result.is_error())
  138. break;
  139. }
  140. if (result.is_error())
  141. warnln("Could not launch any of {}: {}", candidate_web_content_paths, result.error());
  142. VERIFY_NOT_REACHED();
  143. }
  144. TRY(Core::System::close(wc_fd_passing_fd));
  145. TRY(Core::System::close(wc_fd));
  146. auto socket = TRY(Core::LocalSocket::adopt_fd(ui_fd));
  147. TRY(socket->set_blocking(true));
  148. auto new_client = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) WebView::WebContentClient(move(socket), *this)));
  149. new_client->set_fd_passing_socket(TRY(Core::LocalSocket::adopt_fd(ui_fd_passing_fd)));
  150. if (enable_callgrind_profiling == EnableCallgrindProfiling::Yes) {
  151. dbgln();
  152. dbgln("\033[1;45mLaunched WebContent process under callgrind!\033[0m");
  153. dbgln("\033[100mRun `\033[4mcallgrind_control -i on\033[24m` to start instrumentation and `\033[4mcallgrind_control -i off\033[24m` stop it again.\033[0m");
  154. dbgln();
  155. }
  156. return new_client;
  157. }
  158. #endif
  159. }