main.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "BrowserWindow.h"
  7. #include "HelperProcess.h"
  8. #include "Settings.h"
  9. #include "Utilities.h"
  10. #include "WebContentView.h"
  11. #include <Browser/CookieJar.h>
  12. #include <Browser/Database.h>
  13. #include <LibCore/ArgsParser.h>
  14. #include <LibCore/EventLoop.h>
  15. #include <LibCore/File.h>
  16. #include <LibCore/Stream.h>
  17. #include <LibCore/System.h>
  18. #include <LibGfx/Font/FontDatabase.h>
  19. #include <LibMain/Main.h>
  20. #include <LibSQL/SQLClient.h>
  21. #include <QApplication>
  22. Browser::Settings* s_settings;
  23. static ErrorOr<void> handle_attached_debugger()
  24. {
  25. #ifdef AK_OS_LINUX
  26. // Let's ignore SIGINT if we're being debugged because GDB
  27. // incorrectly forwards the signal to us even when it's set to
  28. // "nopass". See https://sourceware.org/bugzilla/show_bug.cgi?id=9425
  29. // for details.
  30. auto unbuffered_status_file = TRY(Core::Stream::File::open("/proc/self/status"sv, Core::Stream::OpenMode::Read));
  31. auto status_file = TRY(Core::Stream::BufferedFile::create(move(unbuffered_status_file)));
  32. auto buffer = TRY(ByteBuffer::create_uninitialized(4096));
  33. while (TRY(status_file->can_read_line())) {
  34. auto line = TRY(status_file->read_line(buffer));
  35. auto const parts = line.split_view(':');
  36. if (parts.size() < 2 || parts[0] != "TracerPid"sv)
  37. continue;
  38. auto tracer_pid = parts[1].to_uint<u32>();
  39. if (tracer_pid != 0UL) {
  40. dbgln("Debugger is attached, ignoring SIGINT");
  41. TRY(Core::System::signal(SIGINT, SIG_IGN));
  42. }
  43. break;
  44. }
  45. #endif
  46. return {};
  47. }
  48. ErrorOr<int> serenity_main(Main::Arguments arguments)
  49. {
  50. // NOTE: This is only used for the Core::Socket inside the IPC connections.
  51. // FIXME: Refactor things so we can get rid of this somehow.
  52. Core::EventLoop event_loop;
  53. TRY(handle_attached_debugger());
  54. QApplication app(arguments.argc, arguments.argv);
  55. platform_init();
  56. // NOTE: We only instantiate this to ensure that Gfx::FontDatabase has its default queries initialized.
  57. Gfx::FontDatabase::set_default_font_query("Katica 10 400 0");
  58. Gfx::FontDatabase::set_fixed_width_font_query("Csilla 10 400 0");
  59. StringView raw_url;
  60. StringView webdriver_content_ipc_path;
  61. bool dump_layout_tree = false;
  62. Core::ArgsParser args_parser;
  63. args_parser.set_general_help("The Ladybird web browser :^)");
  64. args_parser.add_positional_argument(raw_url, "URL to open", "url", Core::ArgsParser::Required::No);
  65. args_parser.add_option(webdriver_content_ipc_path, "Path to WebDriver IPC for WebContent", "webdriver-content-path", 0, "path");
  66. args_parser.add_option(dump_layout_tree, "Dump layout tree and exit", "dump-layout-tree", 'd');
  67. args_parser.parse(arguments);
  68. auto get_formatted_url = [&](StringView const& raw_url) -> URL {
  69. URL url = raw_url;
  70. if (Core::File::exists(raw_url))
  71. url = URL::create_with_file_scheme(Core::File::real_path_for(raw_url));
  72. else if (!url.is_valid())
  73. url = DeprecatedString::formatted("http://{}", raw_url);
  74. return url;
  75. };
  76. if (dump_layout_tree) {
  77. WebContentView view({});
  78. view.on_load_finish = [&](auto&) {
  79. auto dump = view.dump_layout_tree().release_value_but_fixme_should_propagate_errors();
  80. outln("{}", dump);
  81. fflush(stdout);
  82. event_loop.quit(0);
  83. app.quit();
  84. };
  85. view.load(get_formatted_url(raw_url));
  86. return app.exec();
  87. }
  88. auto sql_server_paths = TRY(get_paths_for_helper_process("SQLServer"sv));
  89. auto sql_client = TRY(SQL::SQLClient::launch_server_and_create_client(move(sql_server_paths)));
  90. auto database = TRY(Browser::Database::create(move(sql_client)));
  91. auto cookie_jar = TRY(Browser::CookieJar::create(*database));
  92. BrowserWindow window(cookie_jar, webdriver_content_ipc_path);
  93. s_settings = new Browser::Settings(&window);
  94. window.setWindowTitle("Ladybird");
  95. window.resize(800, 600);
  96. window.show();
  97. if (auto url = get_formatted_url(raw_url); url.is_valid()) {
  98. window.view().load(url);
  99. } else if (!s_settings->homepage().isEmpty()) {
  100. auto home_url = TRY(ak_string_from_qstring(s_settings->homepage()));
  101. window.view().load(get_formatted_url(home_url.bytes_as_string_view()));
  102. }
  103. return app.exec();
  104. }