Application.h 2.9 KB

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