2022-10-05 15:23:41 +02:00
/*
2023-04-24 18:02:29 +02:00
* Copyright ( c ) 2020 - 2023 , Andreas Kling < kling @ serenityos . org >
2022-10-05 15:23:41 +02:00
*
* SPDX - License - Identifier : BSD - 2 - Clause
*/
2023-04-24 14:51:19 +02:00
# include "../EventLoopImplementationQt.h"
2022-10-05 15:23:41 +02:00
# include "../FontPluginQt.h"
# include "../ImageCodecPluginLadybird.h"
# include "../RequestManagerQt.h"
# include "../Utilities.h"
# include "../WebSocketClientManagerLadybird.h"
# include <AK/LexicalPath.h>
2023-04-26 16:46:55 -04:00
# include <AK/Platform.h>
2022-11-14 08:13:37 -05:00
# include <LibCore/ArgsParser.h>
2022-10-05 15:23:41 +02:00
# include <LibCore/EventLoop.h>
# include <LibCore/LocalServer.h>
# include <LibCore/System.h>
2022-11-14 08:13:37 -05:00
# include <LibCore/SystemServerTakeover.h>
# include <LibIPC/ConnectionFromClient.h>
2022-10-05 15:23:41 +02:00
# include <LibMain/Main.h>
2023-03-17 10:56:59 -04:00
# include <LibWeb/Bindings/MainThreadVM.h>
2022-10-05 15:23:41 +02:00
# include <LibWeb/Loader/ContentFilter.h>
# include <LibWeb/Loader/FrameLoader.h>
# include <LibWeb/Loader/ResourceLoader.h>
2023-04-17 13:55:42 -04:00
# include <LibWeb/PermissionsPolicy/AutoplayAllowlist.h>
2023-04-24 18:02:29 +02:00
# include <LibWeb/Platform/EventLoopPluginSerenity.h>
2022-10-05 15:23:41 +02:00
# include <LibWeb/WebSockets/WebSocket.h>
# include <QGuiApplication>
# include <QTimer>
# include <WebContent/ConnectionFromClient.h>
2022-11-22 08:09:55 -05:00
# include <WebContent/PageHost.h>
2022-11-14 11:08:44 -05:00
# include <WebContent/WebDriverConnection.h>
2022-10-05 15:23:41 +02:00
2023-04-26 16:46:55 -04:00
# if defined(AK_OS_MACOS)
# include "MacOSSetup.h"
# endif
2022-10-05 15:23:41 +02:00
static ErrorOr < void > load_content_filters ( ) ;
2023-04-17 13:55:42 -04:00
static ErrorOr < void > load_autoplay_allowlist ( ) ;
2022-10-05 15:23:41 +02:00
2022-12-04 18:43:54 +00:00
extern DeprecatedString s_serenity_resource_root ;
2022-10-05 15:23:41 +02:00
ErrorOr < int > serenity_main ( Main : : Arguments arguments )
{
QGuiApplication app ( arguments . argc , arguments . argv ) ;
2023-04-26 16:46:55 -04:00
# if defined(AK_OS_MACOS)
prohibit_interaction ( ) ;
# endif
2023-04-25 17:38:48 +02:00
Core : : EventLoopManager : : install ( * new Ladybird : : EventLoopManagerQt ) ;
2023-04-24 14:51:19 +02:00
Core : : EventLoop event_loop ;
2022-10-07 17:08:29 -06:00
platform_init ( ) ;
2023-04-24 18:02:29 +02:00
Web : : Platform : : EventLoopPlugin : : install ( * new Web : : Platform : : EventLoopPluginSerenity ) ;
2022-10-05 15:23:41 +02:00
Web : : Platform : : ImageCodecPlugin : : install ( * new Ladybird : : ImageCodecPluginLadybird ) ;
Web : : ResourceLoader : : initialize ( RequestManagerQt : : create ( ) ) ;
Web : : WebSockets : : WebSocketClientManager : : initialize ( Ladybird : : WebSocketClientManagerLadybird : : create ( ) ) ;
2022-12-04 18:43:54 +00:00
Web : : FrameLoader : : set_default_favicon_path ( DeprecatedString : : formatted ( " {}/res/icons/16x16/app-browser.png " , s_serenity_resource_root ) ) ;
2022-10-05 15:23:41 +02:00
2023-05-05 19:48:48 +02:00
int webcontent_fd_passing_socket { - 1 } ;
2023-05-06 12:46:14 +02:00
bool is_layout_test_mode = false ;
2023-05-05 19:48:48 +02:00
Core : : ArgsParser args_parser ;
args_parser . add_option ( webcontent_fd_passing_socket , " File descriptor of the passing socket for the WebContent connection " , " webcontent-fd-passing-socket " , ' c ' , " webcontent_fd_passing_socket " ) ;
2023-05-06 12:46:14 +02:00
args_parser . add_option ( is_layout_test_mode , " Is layout test mode " , " layout-test-mode " , 0 ) ;
2023-05-05 19:48:48 +02:00
args_parser . parse ( arguments ) ;
VERIFY ( webcontent_fd_passing_socket > = 0 ) ;
2023-05-06 12:46:14 +02:00
Web : : Platform : : FontPlugin : : install ( * new Ladybird : : FontPluginQt ( is_layout_test_mode ) ) ;
2022-10-05 15:23:41 +02:00
2022-12-04 18:43:54 +00:00
Web : : FrameLoader : : set_error_page_url ( DeprecatedString : : formatted ( " file://{}/res/html/error.html " , s_serenity_resource_root ) ) ;
2022-10-05 15:23:41 +02:00
2023-03-17 10:56:59 -04:00
TRY ( Web : : Bindings : : initialize_main_thread_vm ( ) ) ;
2022-10-05 15:23:41 +02:00
auto maybe_content_filter_error = load_content_filters ( ) ;
if ( maybe_content_filter_error . is_error ( ) )
dbgln ( " Failed to load content filters: {} " , maybe_content_filter_error . error ( ) ) ;
2023-04-17 13:55:42 -04:00
auto maybe_autoplay_allowlist_error = load_autoplay_allowlist ( ) ;
if ( maybe_autoplay_allowlist_error . is_error ( ) )
dbgln ( " Failed to load autoplay allowlist: {} " , maybe_autoplay_allowlist_error . error ( ) ) ;
2022-12-15 09:18:52 -05:00
auto webcontent_socket = TRY ( Core : : take_over_socket_from_system_server ( " WebContent " sv ) ) ;
auto webcontent_client = TRY ( WebContent : : ConnectionFromClient : : try_create ( move ( webcontent_socket ) ) ) ;
2023-02-08 23:05:44 +01:00
webcontent_client - > set_fd_passing_socket ( TRY ( Core : : LocalSocket : : adopt_fd ( webcontent_fd_passing_socket ) ) ) ;
2022-12-15 09:18:52 -05:00
2023-04-24 14:51:19 +02:00
return event_loop . exec ( ) ;
2022-10-05 15:23:41 +02:00
}
static ErrorOr < void > load_content_filters ( )
{
2023-02-09 03:02:46 +01:00
auto file_or_error = Core : : File : : open ( DeprecatedString : : formatted ( " {}/home/anon/.config/BrowserContentFilters.txt " , s_serenity_resource_root ) , Core : : File : : OpenMode : : Read ) ;
2022-10-05 15:23:41 +02:00
if ( file_or_error . is_error ( ) )
2023-02-09 03:02:46 +01:00
file_or_error = Core : : File : : open ( DeprecatedString : : formatted ( " {}/res/ladybird/BrowserContentFilters.txt " , s_serenity_resource_root ) , Core : : File : : OpenMode : : Read ) ;
2022-10-05 15:23:41 +02:00
if ( file_or_error . is_error ( ) )
return file_or_error . release_error ( ) ;
2023-04-21 07:36:40 -04:00
2022-10-05 15:23:41 +02:00
auto file = file_or_error . release_value ( ) ;
2023-05-03 18:45:18 -04:00
auto ad_filter_list = TRY ( Core : : InputBufferedFile : : create ( move ( file ) ) ) ;
2022-10-05 15:23:41 +02:00
auto buffer = TRY ( ByteBuffer : : create_uninitialized ( 4096 ) ) ;
2023-04-21 07:36:40 -04:00
2023-04-21 07:54:56 -04:00
Vector < String > patterns ;
2023-04-21 07:36:40 -04:00
2022-10-05 15:23:41 +02:00
while ( TRY ( ad_filter_list - > can_read_line ( ) ) ) {
auto line = TRY ( ad_filter_list - > read_line ( buffer ) ) ;
2023-04-21 07:36:40 -04:00
if ( line . is_empty ( ) )
continue ;
2023-04-21 07:54:56 -04:00
auto pattern = TRY ( String : : from_utf8 ( line ) ) ;
TRY ( patterns . try_append ( move ( pattern ) ) ) ;
2022-10-05 15:23:41 +02:00
}
2023-04-21 07:36:40 -04:00
auto & content_filter = Web : : ContentFilter : : the ( ) ;
TRY ( content_filter . set_patterns ( patterns ) ) ;
2022-10-05 15:23:41 +02:00
return { } ;
}
2023-04-17 13:55:42 -04:00
static ErrorOr < void > load_autoplay_allowlist ( )
{
auto file_or_error = Core : : File : : open ( TRY ( String : : formatted ( " {}/home/anon/.config/BrowserAutoplayAllowlist.txt " , s_serenity_resource_root ) ) , Core : : File : : OpenMode : : Read ) ;
if ( file_or_error . is_error ( ) )
file_or_error = Core : : File : : open ( TRY ( String : : formatted ( " {}/res/ladybird/BrowserAutoplayAllowlist.txt " , s_serenity_resource_root ) ) , Core : : File : : OpenMode : : Read ) ;
if ( file_or_error . is_error ( ) )
return file_or_error . release_error ( ) ;
auto file = file_or_error . release_value ( ) ;
2023-05-03 18:45:18 -04:00
auto allowlist = TRY ( Core : : InputBufferedFile : : create ( move ( file ) ) ) ;
2023-04-17 13:55:42 -04:00
auto buffer = TRY ( ByteBuffer : : create_uninitialized ( 4096 ) ) ;
Vector < String > origins ;
while ( TRY ( allowlist - > can_read_line ( ) ) ) {
auto line = TRY ( allowlist - > read_line ( buffer ) ) ;
if ( line . is_empty ( ) )
continue ;
auto domain = TRY ( String : : from_utf8 ( line ) ) ;
TRY ( origins . try_append ( move ( domain ) ) ) ;
}
auto & autoplay_allowlist = Web : : PermissionsPolicy : : AutoplayAllowlist : : the ( ) ;
TRY ( autoplay_allowlist . enable_for_origins ( origins ) ) ;
return { } ;
}