Application.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright (c) 2024, Andrew Kaster <akaster@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Badge.h>
  8. #include <AK/ByteString.h>
  9. #include <AK/LexicalPath.h>
  10. #include <AK/Optional.h>
  11. #include <AK/Swift.h>
  12. #include <LibCore/EventLoop.h>
  13. #include <LibCore/Forward.h>
  14. #include <LibMain/Main.h>
  15. #include <LibURL/URL.h>
  16. #include <LibWebView/Options.h>
  17. #include <LibWebView/Process.h>
  18. #include <LibWebView/ProcessManager.h>
  19. namespace WebView {
  20. class Application {
  21. AK_MAKE_NONCOPYABLE(Application);
  22. public:
  23. virtual ~Application();
  24. int execute();
  25. static Application& the() { return *s_the; }
  26. static ChromeOptions const& chrome_options() { return the().m_chrome_options; }
  27. static WebContentOptions const& web_content_options() { return the().m_web_content_options; }
  28. static CookieJar& cookie_jar() { return *the().m_cookie_jar; }
  29. Core::EventLoop& event_loop() { return m_event_loop; }
  30. void add_child_process(Process&&);
  31. // FIXME: Should these methods be part of Application, instead of deferring to ProcessManager?
  32. #if defined(AK_OS_MACH)
  33. void set_process_mach_port(pid_t, Core::MachPort&&);
  34. #endif
  35. Optional<Process&> find_process(pid_t);
  36. // FIXME: Should we just expose the ProcessManager via a getter?
  37. void update_process_statistics();
  38. String generate_process_statistics_html();
  39. ErrorOr<LexicalPath> path_for_downloaded_file(StringView file) const;
  40. protected:
  41. template<DerivedFrom<Application> ApplicationType>
  42. static NonnullOwnPtr<ApplicationType> create(Main::Arguments& arguments, URL::URL new_tab_page_url)
  43. {
  44. auto app = adopt_own(*new ApplicationType { {}, arguments });
  45. app->initialize(arguments, move(new_tab_page_url));
  46. return app;
  47. }
  48. Application();
  49. virtual void process_did_exit(Process&&);
  50. virtual void create_platform_arguments(Core::ArgsParser&) { }
  51. virtual void create_platform_options(ChromeOptions&, WebContentOptions&) { }
  52. virtual Optional<ByteString> ask_user_for_download_folder() const { return {}; }
  53. private:
  54. void initialize(Main::Arguments const& arguments, URL::URL new_tab_page_url);
  55. static Application* s_the;
  56. ChromeOptions m_chrome_options;
  57. WebContentOptions m_web_content_options;
  58. RefPtr<Database> m_database;
  59. OwnPtr<CookieJar> m_cookie_jar;
  60. OwnPtr<Core::TimeZoneWatcher> m_time_zone_watcher;
  61. Core::EventLoop m_event_loop;
  62. ProcessManager m_process_manager;
  63. bool m_in_shutdown { false };
  64. } SWIFT_IMMORTAL_REFERENCE;
  65. }
  66. #define WEB_VIEW_APPLICATION(ApplicationType) \
  67. public: \
  68. static NonnullOwnPtr<ApplicationType> create(Main::Arguments& arguments, URL::URL new_tab_page_url) \
  69. { \
  70. return WebView::Application::create<ApplicationType>(arguments, move(new_tab_page_url)); \
  71. } \
  72. \
  73. ApplicationType(Badge<WebView::Application>, Main::Arguments&);