WebContentService.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Copyright (c) 2023, Andrew Kaster <akaster@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "WebContentService.h"
  7. #include "LadybirdServiceBase.h"
  8. #include <AK/LexicalPath.h>
  9. #include <Ladybird/FontPlugin.h>
  10. #include <Ladybird/HelperProcess.h>
  11. #include <Ladybird/ImageCodecPlugin.h>
  12. #include <Ladybird/Utilities.h>
  13. #include <LibAudio/Loader.h>
  14. #include <LibCore/ArgsParser.h>
  15. #include <LibCore/EventLoop.h>
  16. #include <LibCore/LocalServer.h>
  17. #include <LibCore/System.h>
  18. #include <LibIPC/ConnectionFromClient.h>
  19. #include <LibJS/Bytecode/Interpreter.h>
  20. #include <LibWeb/Bindings/MainThreadVM.h>
  21. #include <LibWeb/HTML/Window.h>
  22. #include <LibWeb/Loader/ContentFilter.h>
  23. #include <LibWeb/Loader/GeneratedPagesLoader.h>
  24. #include <LibWeb/Loader/ResourceLoader.h>
  25. #include <LibWeb/PermissionsPolicy/AutoplayAllowlist.h>
  26. #include <LibWeb/Platform/AudioCodecPluginAgnostic.h>
  27. #include <LibWeb/Platform/EventLoopPluginSerenity.h>
  28. #include <LibWebView/RequestServerAdapter.h>
  29. #include <LibWebView/WebSocketClientAdapter.h>
  30. #include <WebContent/ConnectionFromClient.h>
  31. #include <WebContent/PageHost.h>
  32. template<typename Client>
  33. static ErrorOr<NonnullRefPtr<Client>> bind_service(void (*bind_method)(int, int));
  34. static ErrorOr<NonnullRefPtr<Protocol::RequestClient>> bind_request_server_service()
  35. {
  36. return bind_service<Protocol::RequestClient>(&bind_request_server_java);
  37. }
  38. static ErrorOr<NonnullRefPtr<Protocol::WebSocketClient>> bind_web_socket_service()
  39. {
  40. return bind_service<Protocol::WebSocketClient>(&bind_web_socket_java);
  41. }
  42. static ErrorOr<void> load_content_filters();
  43. static ErrorOr<void> load_autoplay_allowlist();
  44. ErrorOr<int> service_main(int ipc_socket, int fd_passing_socket)
  45. {
  46. Core::EventLoop event_loop;
  47. Web::Platform::EventLoopPlugin::install(*new Web::Platform::EventLoopPluginSerenity);
  48. Web::Platform::ImageCodecPlugin::install(*new Ladybird::ImageCodecPlugin);
  49. Web::Platform::AudioCodecPlugin::install_creation_hook([](auto loader) {
  50. (void)loader;
  51. return Error::from_string_literal("Don't know how to initialize audio in this configuration!");
  52. });
  53. auto request_server_client = TRY(bind_request_server_service());
  54. Web::ResourceLoader::initialize(TRY(WebView::RequestServerAdapter::try_create(move(request_server_client))));
  55. auto web_socket_client = TRY(bind_web_socket_service());
  56. Web::WebSockets::WebSocketClientManager::initialize(TRY(WebView::WebSocketClientManagerAdapter::try_create(move(web_socket_client))));
  57. bool is_layout_test_mode = false;
  58. Web::HTML::Window::set_internals_object_exposed(is_layout_test_mode);
  59. Web::Platform::FontPlugin::install(*new Ladybird::FontPlugin(is_layout_test_mode));
  60. Web::set_resource_directory_url(TRY(String::formatted("file://{}/res", s_serenity_resource_root)));
  61. Web::set_directory_page_url(TRY(String::formatted("file://{}/res/html/directory.html", s_serenity_resource_root)));
  62. TRY(Web::Bindings::initialize_main_thread_vm());
  63. auto maybe_content_filter_error = load_content_filters();
  64. if (maybe_content_filter_error.is_error())
  65. dbgln("Failed to load content filters: {}", maybe_content_filter_error.error());
  66. auto maybe_autoplay_allowlist_error = load_autoplay_allowlist();
  67. if (maybe_autoplay_allowlist_error.is_error())
  68. dbgln("Failed to load autoplay allowlist: {}", maybe_autoplay_allowlist_error.error());
  69. auto webcontent_socket = TRY(Core::LocalSocket::adopt_fd(ipc_socket));
  70. auto webcontent_client = TRY(WebContent::ConnectionFromClient::try_create(move(webcontent_socket)));
  71. webcontent_client->set_fd_passing_socket(TRY(Core::LocalSocket::adopt_fd(fd_passing_socket)));
  72. return event_loop.exec();
  73. }
  74. template<typename Client>
  75. static ErrorOr<NonnullRefPtr<Client>> bind_service(void (*bind_method)(int, int))
  76. {
  77. int socket_fds[2] {};
  78. TRY(Core::System::socketpair(AF_LOCAL, SOCK_STREAM, 0, socket_fds));
  79. int ui_fd = socket_fds[0];
  80. int server_fd = socket_fds[1];
  81. int fd_passing_socket_fds[2] {};
  82. TRY(Core::System::socketpair(AF_LOCAL, SOCK_STREAM, 0, fd_passing_socket_fds));
  83. int ui_fd_passing_fd = fd_passing_socket_fds[0];
  84. int server_fd_passing_fd = fd_passing_socket_fds[1];
  85. // NOTE: The java object takes ownership of the socket fds
  86. (*bind_method)(server_fd, server_fd_passing_fd);
  87. auto socket = TRY(Core::LocalSocket::adopt_fd(ui_fd));
  88. TRY(socket->set_blocking(true));
  89. auto new_client = TRY(try_make_ref_counted<Client>(move(socket)));
  90. new_client->set_fd_passing_socket(TRY(Core::LocalSocket::adopt_fd(ui_fd_passing_fd)));
  91. return new_client;
  92. }
  93. static ErrorOr<void> load_content_filters()
  94. {
  95. auto file_or_error = Core::File::open(DeprecatedString::formatted("{}/home/anon/.config/BrowserContentFilters.txt", s_serenity_resource_root), Core::File::OpenMode::Read);
  96. if (file_or_error.is_error())
  97. file_or_error = Core::File::open(DeprecatedString::formatted("{}/res/ladybird/BrowserContentFilters.txt", s_serenity_resource_root), Core::File::OpenMode::Read);
  98. if (file_or_error.is_error())
  99. return file_or_error.release_error();
  100. auto file = file_or_error.release_value();
  101. auto ad_filter_list = TRY(Core::InputBufferedFile::create(move(file)));
  102. auto buffer = TRY(ByteBuffer::create_uninitialized(4096));
  103. Vector<String> patterns;
  104. while (TRY(ad_filter_list->can_read_line())) {
  105. auto line = TRY(ad_filter_list->read_line(buffer));
  106. if (line.is_empty())
  107. continue;
  108. auto pattern = TRY(String::from_utf8(line));
  109. TRY(patterns.try_append(move(pattern)));
  110. }
  111. auto& content_filter = Web::ContentFilter::the();
  112. TRY(content_filter.set_patterns(patterns));
  113. return {};
  114. }
  115. static ErrorOr<void> load_autoplay_allowlist()
  116. {
  117. auto file_or_error = Core::File::open(TRY(String::formatted("{}/home/anon/.config/BrowserAutoplayAllowlist.txt", s_serenity_resource_root)), Core::File::OpenMode::Read);
  118. if (file_or_error.is_error())
  119. file_or_error = Core::File::open(TRY(String::formatted("{}/res/ladybird/BrowserAutoplayAllowlist.txt", s_serenity_resource_root)), Core::File::OpenMode::Read);
  120. if (file_or_error.is_error())
  121. return file_or_error.release_error();
  122. auto file = file_or_error.release_value();
  123. auto allowlist = TRY(Core::InputBufferedFile::create(move(file)));
  124. auto buffer = TRY(ByteBuffer::create_uninitialized(4096));
  125. Vector<String> origins;
  126. while (TRY(allowlist->can_read_line())) {
  127. auto line = TRY(allowlist->read_line(buffer));
  128. if (line.is_empty())
  129. continue;
  130. auto domain = TRY(String::from_utf8(line));
  131. TRY(origins.try_append(move(domain)));
  132. }
  133. auto& autoplay_allowlist = Web::PermissionsPolicy::AutoplayAllowlist::the();
  134. TRY(autoplay_allowlist.enable_for_origins(origins));
  135. return {};
  136. }