Truly independent web browser
/*
* Copyright (c) 2018-2020, Andreas Kling <[email protected]>
* Copyright (c) 2023, Andrew Kaster <[email protected]>
* Copyright (c) 2023, Lucas Chollet <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <ImageDecoder/ControlConnection.h>
#include <ImageDecoder/Sandbox.h>
#include <LibCore/ArgsParser.h>
#include <LibCore/CrashHandler.h>
#include <LibCore/EventLoop.h>
#include <LibCore/Platform/TaskRole.h>
#include <LibCore/Platform/ThreadQoS.h>
#include <LibCore/Process.h>
#include <LibCore/StatsLog.h>
#include <LibIPC/SingleServer.h>
#include <LibMain/Main.h>
ErrorOr<int> ladybird_main(Main::Arguments arguments)
{
AK::set_rich_debug_enabled(true);
int crash_report_fd = -1;
StringView stats_log_directory;
Core::ArgsParser args_parser;
args_parser.add_option(stats_log_directory, "Directory for process statistics (JSON lines)", "stats-log-dir", 0, "path");
args_parser.add_option(crash_report_fd, "Descriptor for anonymous crash diagnostics", "crash-report-fd", 0, "fd");
StringView mach_server_name;
bool wait_for_debugger = false;
bool disable_sandbox = false;
args_parser.add_option(mach_server_name, "Mach server name", "mach-server-name", 0, "mach_server_name");
args_parser.add_option(wait_for_debugger, "Wait for debugger", "wait-for-debugger");
args_parser.add_option(disable_sandbox, "Disable process sandboxing", "disable-sandbox");
args_parser.parse(arguments);
auto stats_log = TRY(Core::StatsLog::create(stats_log_directory, "ImageDecoder"sv));
if (crash_report_fd >= 0) {
if (auto result = Core::CrashHandler::initialize(crash_report_fd); result.is_error())
warnln("Could not install crash report handler: {}", result.error());
}
if (wait_for_debugger)
Core::Process::wait_for_debugger_and_break();
if (auto result = Core::Platform::adopt_foreground_application_task_role(); result.is_error())
warnln("Could not adopt the foreground application task role: {}", result.error());
if (auto result = Core::Platform::set_current_thread_qos(Core::Platform::ThreadQoS::UserInitiated); result.is_error())
warnln("Could not set main thread QoS: {}", result.error());
auto& event_loop = Core::EventLoop::initialize_for_current_thread();
if (!disable_sandbox)
TRY(ImageDecoder::apply_sandbox(mach_server_name));
auto client = TRY(IPC::take_over_accepted_client_from_system_server<ImageDecoder::ControlConnection>(mach_server_name));
if (stats_log)
stats_log->start_sampling();
return event_loop.exec();
}