main.cpp 3.8 KB

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