electron_browser_main_parts.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. // Copyright (c) 2013 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #ifndef ELECTRON_SHELL_BROWSER_ELECTRON_BROWSER_MAIN_PARTS_H_
  5. #define ELECTRON_SHELL_BROWSER_ELECTRON_BROWSER_MAIN_PARTS_H_
  6. #include <memory>
  7. #include <optional>
  8. #include <string>
  9. #include "base/functional/callback_forward.h"
  10. #include "base/task/single_thread_task_runner.h"
  11. #include "content/public/browser/browser_main_parts.h"
  12. #include "electron/buildflags/buildflags.h"
  13. #include "mojo/public/cpp/bindings/remote.h"
  14. #include "services/device/public/mojom/geolocation_control.mojom.h"
  15. class BrowserProcessImpl;
  16. class IconManager;
  17. namespace base {
  18. class FieldTrialList;
  19. }
  20. namespace display {
  21. class Screen;
  22. class ScopedNativeScreen;
  23. } // namespace display
  24. #if defined(USE_AURA)
  25. namespace wm {
  26. class WMState;
  27. }
  28. namespace display {
  29. class Screen;
  30. }
  31. #endif
  32. namespace node {
  33. class Environment;
  34. }
  35. namespace ui {
  36. class LinuxUiGetter;
  37. class DarkModeManagerLinux;
  38. } // namespace ui
  39. namespace views {
  40. class LayoutProvider;
  41. } // namespace views
  42. namespace electron {
  43. class Browser;
  44. class ElectronBindings;
  45. class JavascriptEnvironment;
  46. class NodeBindings;
  47. #if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
  48. class ElectronExtensionsClient;
  49. class ElectronExtensionsBrowserClient;
  50. #endif
  51. #if defined(TOOLKIT_VIEWS)
  52. class ViewsDelegate;
  53. #endif
  54. #if BUILDFLAG(IS_MAC)
  55. class ViewsDelegateMac;
  56. #endif
  57. class ElectronBrowserMainParts : public content::BrowserMainParts {
  58. public:
  59. ElectronBrowserMainParts();
  60. ~ElectronBrowserMainParts() override;
  61. // disable copy
  62. ElectronBrowserMainParts(const ElectronBrowserMainParts&) = delete;
  63. ElectronBrowserMainParts& operator=(const ElectronBrowserMainParts&) = delete;
  64. static ElectronBrowserMainParts* Get();
  65. // Sets the exit code, will fail if the message loop is not ready.
  66. bool SetExitCode(int code);
  67. // Gets the exit code
  68. int GetExitCode() const;
  69. // Returns the connection to GeolocationControl which can be
  70. // used to enable the location services once per client.
  71. device::mojom::GeolocationControl* GetGeolocationControl();
  72. // Returns handle to the class responsible for extracting file icons.
  73. IconManager* GetIconManager();
  74. Browser* browser() { return browser_.get(); }
  75. BrowserProcessImpl* browser_process() { return fake_browser_process_.get(); }
  76. protected:
  77. // content::BrowserMainParts:
  78. int PreEarlyInitialization() override;
  79. void PostEarlyInitialization() override;
  80. int PreCreateThreads() override;
  81. void ToolkitInitialized() override;
  82. int PreMainMessageLoopRun() override;
  83. void WillRunMainMessageLoop(
  84. std::unique_ptr<base::RunLoop>& run_loop) override;
  85. void PostCreateMainMessageLoop() override;
  86. void PostMainMessageLoopRun() override;
  87. void PreCreateMainMessageLoop() override;
  88. void PostCreateThreads() override;
  89. void PostDestroyThreads() override;
  90. private:
  91. void PreCreateMainMessageLoopCommon();
  92. #if BUILDFLAG(IS_POSIX)
  93. // Set signal handlers.
  94. void HandleSIGCHLD();
  95. void InstallShutdownSignalHandlers(
  96. base::OnceCallback<void()> shutdown_callback,
  97. const scoped_refptr<base::SingleThreadTaskRunner>& task_runner);
  98. #endif
  99. #if BUILDFLAG(IS_LINUX)
  100. void DetectOzonePlatform();
  101. #endif
  102. #if BUILDFLAG(IS_MAC)
  103. void FreeAppDelegate();
  104. void RegisterURLHandler();
  105. void InitializeMainNib();
  106. static std::string GetCurrentSystemLocale();
  107. #endif
  108. #if BUILDFLAG(IS_MAC)
  109. std::unique_ptr<ViewsDelegateMac> views_delegate_;
  110. #else
  111. std::unique_ptr<ViewsDelegate> views_delegate_;
  112. #endif
  113. #if defined(USE_AURA)
  114. std::unique_ptr<wm::WMState> wm_state_;
  115. std::unique_ptr<display::Screen> screen_;
  116. #endif
  117. #if BUILDFLAG(IS_LINUX)
  118. std::unique_ptr<ui::DarkModeManagerLinux> dark_mode_manager_;
  119. std::unique_ptr<ui::LinuxUiGetter> linux_ui_getter_;
  120. #endif
  121. std::unique_ptr<views::LayoutProvider> layout_provider_;
  122. // A fake BrowserProcess object that used to feed the source code from chrome.
  123. std::unique_ptr<BrowserProcessImpl> fake_browser_process_;
  124. // A place to remember the exit code once the message loop is ready.
  125. // Before then, we just exit() without any intermediate steps.
  126. std::optional<int> exit_code_;
  127. const std::unique_ptr<NodeBindings> node_bindings_;
  128. // depends-on: node_bindings_
  129. const std::unique_ptr<ElectronBindings> electron_bindings_;
  130. // depends-on: node_bindings_
  131. std::unique_ptr<JavascriptEnvironment> js_env_;
  132. // depends-on: js_env_'s isolate
  133. std::shared_ptr<node::Environment> node_env_;
  134. // depends-on: js_env_'s isolate
  135. std::unique_ptr<Browser> browser_;
  136. std::unique_ptr<IconManager> icon_manager_;
  137. std::unique_ptr<base::FieldTrialList> field_trial_list_;
  138. #if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
  139. std::unique_ptr<ElectronExtensionsClient> extensions_client_;
  140. std::unique_ptr<ElectronExtensionsBrowserClient> extensions_browser_client_;
  141. #endif
  142. mojo::Remote<device::mojom::GeolocationControl> geolocation_control_;
  143. #if BUILDFLAG(IS_MAC)
  144. std::unique_ptr<display::ScopedNativeScreen> screen_;
  145. #endif
  146. static ElectronBrowserMainParts* self_;
  147. };
  148. } // namespace electron
  149. #endif // ELECTRON_SHELL_BROWSER_ELECTRON_BROWSER_MAIN_PARTS_H_