Application.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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/Swift.h>
  9. #include <LibCore/EventLoop.h>
  10. #include <LibCore/Forward.h>
  11. #include <LibMain/Main.h>
  12. #include <LibURL/URL.h>
  13. #include <LibWebView/Options.h>
  14. #include <LibWebView/Process.h>
  15. #include <LibWebView/ProcessManager.h>
  16. namespace WebView {
  17. class Application {
  18. AK_MAKE_NONCOPYABLE(Application);
  19. public:
  20. virtual ~Application();
  21. int execute();
  22. static Application& the() { return *s_the; }
  23. static ChromeOptions const& chrome_options() { return the().m_chrome_options; }
  24. static WebContentOptions const& web_content_options() { return the().m_web_content_options; }
  25. Core::EventLoop& event_loop() { return m_event_loop; }
  26. void add_child_process(Process&&);
  27. // FIXME: Should these methods be part of Application, instead of deferring to ProcessManager?
  28. #if defined(AK_OS_MACH)
  29. void set_process_mach_port(pid_t, Core::MachPort&&);
  30. #endif
  31. Optional<Process&> find_process(pid_t);
  32. // FIXME: Should we just expose the ProcessManager via a getter?
  33. void update_process_statistics();
  34. String generate_process_statistics_html();
  35. protected:
  36. template<DerivedFrom<Application> ApplicationType>
  37. static NonnullOwnPtr<ApplicationType> create(Main::Arguments& arguments, URL::URL new_tab_page_url)
  38. {
  39. auto app = adopt_own(*new ApplicationType { {}, arguments });
  40. app->initialize(arguments, move(new_tab_page_url));
  41. return app;
  42. }
  43. Application();
  44. virtual void process_did_exit(Process&&);
  45. virtual void create_platform_arguments(Core::ArgsParser&) { }
  46. virtual void create_platform_options(ChromeOptions&, WebContentOptions&) { }
  47. private:
  48. void initialize(Main::Arguments const& arguments, URL::URL new_tab_page_url);
  49. static Application* s_the;
  50. ChromeOptions m_chrome_options;
  51. WebContentOptions m_web_content_options;
  52. OwnPtr<Core::TimeZoneWatcher> m_time_zone_watcher;
  53. Core::EventLoop m_event_loop;
  54. ProcessManager m_process_manager;
  55. bool m_in_shutdown { false };
  56. } SWIFT_IMMORTAL_REFERENCE;
  57. }
  58. #define WEB_VIEW_APPLICATION(ApplicationType) \
  59. public: \
  60. static NonnullOwnPtr<ApplicationType> create(Main::Arguments& arguments, URL::URL new_tab_page_url) \
  61. { \
  62. return WebView::Application::create<ApplicationType>(arguments, move(new_tab_page_url)); \
  63. } \
  64. \
  65. ApplicationType(Badge<WebView::Application>, Main::Arguments&);