atom_browser_main_parts.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 SHELL_BROWSER_ATOM_BROWSER_MAIN_PARTS_H_
  5. #define SHELL_BROWSER_ATOM_BROWSER_MAIN_PARTS_H_
  6. #include <list>
  7. #include <memory>
  8. #include <string>
  9. #include "base/callback.h"
  10. #include "base/metrics/field_trial.h"
  11. #include "base/timer/timer.h"
  12. #include "content/public/browser/browser_context.h"
  13. #include "content/public/browser/browser_main_parts.h"
  14. #include "content/public/common/main_function_params.h"
  15. #include "electron/buildflags/buildflags.h"
  16. #include "services/device/public/mojom/geolocation_control.mojom.h"
  17. #include "ui/views/layout/layout_provider.h"
  18. class BrowserProcess;
  19. class IconManager;
  20. #if defined(USE_AURA)
  21. namespace wm {
  22. class WMState;
  23. }
  24. #endif
  25. namespace electron {
  26. class AtomBrowserContext;
  27. class Browser;
  28. class ElectronBindings;
  29. class JavascriptEnvironment;
  30. class NodeBindings;
  31. class NodeDebugger;
  32. class NodeEnvironment;
  33. class BridgeTaskRunner;
  34. #if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
  35. class AtomExtensionsClient;
  36. class AtomExtensionsBrowserClient;
  37. #endif
  38. #if defined(TOOLKIT_VIEWS)
  39. class ViewsDelegate;
  40. #endif
  41. #if defined(OS_MACOSX)
  42. class ViewsDelegateMac;
  43. #endif
  44. #if defined(USE_X11)
  45. class DarkThemeObserver;
  46. #endif
  47. class AtomBrowserMainParts : public content::BrowserMainParts {
  48. public:
  49. explicit AtomBrowserMainParts(const content::MainFunctionParams& params);
  50. ~AtomBrowserMainParts() override;
  51. static AtomBrowserMainParts* Get();
  52. // Sets the exit code, will fail if the message loop is not ready.
  53. bool SetExitCode(int code);
  54. // Gets the exit code
  55. int GetExitCode();
  56. // Register a callback that should be destroyed before JavaScript environment
  57. // gets destroyed.
  58. // Returns a closure that can be used to remove |callback| from the list.
  59. void RegisterDestructionCallback(base::OnceClosure callback);
  60. // Returns the connection to GeolocationControl which can be
  61. // used to enable the location services once per client.
  62. device::mojom::GeolocationControl* GetGeolocationControl();
  63. // Returns handle to the class responsible for extracting file icons.
  64. IconManager* GetIconManager();
  65. Browser* browser() { return browser_.get(); }
  66. BrowserProcessImpl* browser_process() { return fake_browser_process_.get(); }
  67. protected:
  68. // content::BrowserMainParts:
  69. int PreEarlyInitialization() override;
  70. void PostEarlyInitialization() override;
  71. int PreCreateThreads() override;
  72. void ToolkitInitialized() override;
  73. void PreMainMessageLoopRun() override;
  74. bool MainMessageLoopRun(int* result_code) override;
  75. void PreDefaultMainMessageLoopRun(base::OnceClosure quit_closure) override;
  76. void PostMainMessageLoopStart() override;
  77. void PostMainMessageLoopRun() override;
  78. void PreMainMessageLoopStart() override;
  79. void PostCreateThreads() override;
  80. void PostDestroyThreads() override;
  81. private:
  82. void PreMainMessageLoopStartCommon();
  83. #if defined(OS_POSIX)
  84. // Set signal handlers.
  85. void HandleSIGCHLD();
  86. void HandleShutdownSignals();
  87. #endif
  88. #if defined(OS_MACOSX)
  89. void FreeAppDelegate();
  90. void RegisterURLHandler();
  91. void InitializeMainNib();
  92. #endif
  93. #if defined(OS_MACOSX)
  94. std::unique_ptr<ViewsDelegateMac> views_delegate_;
  95. #else
  96. std::unique_ptr<ViewsDelegate> views_delegate_;
  97. #endif
  98. #if defined(USE_AURA)
  99. std::unique_ptr<wm::WMState> wm_state_;
  100. #endif
  101. #if defined(USE_X11)
  102. std::unique_ptr<DarkThemeObserver> dark_theme_observer_;
  103. #endif
  104. std::unique_ptr<views::LayoutProvider> layout_provider_;
  105. // A fake BrowserProcess object that used to feed the source code from chrome.
  106. std::unique_ptr<BrowserProcessImpl> fake_browser_process_;
  107. // Pointer to exit code.
  108. int* exit_code_ = nullptr;
  109. std::unique_ptr<Browser> browser_;
  110. std::unique_ptr<JavascriptEnvironment> js_env_;
  111. std::unique_ptr<NodeBindings> node_bindings_;
  112. std::unique_ptr<ElectronBindings> electron_bindings_;
  113. std::unique_ptr<NodeEnvironment> node_env_;
  114. std::unique_ptr<NodeDebugger> node_debugger_;
  115. std::unique_ptr<IconManager> icon_manager_;
  116. std::unique_ptr<base::FieldTrialList> field_trial_list_;
  117. #if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
  118. std::unique_ptr<AtomExtensionsClient> extensions_client_;
  119. std::unique_ptr<AtomExtensionsBrowserClient> extensions_browser_client_;
  120. #endif
  121. base::RepeatingTimer gc_timer_;
  122. // List of callbacks should be executed before destroying JS env.
  123. std::list<base::OnceClosure> destructors_;
  124. device::mojom::GeolocationControlPtr geolocation_control_;
  125. static AtomBrowserMainParts* self_;
  126. DISALLOW_COPY_AND_ASSIGN(AtomBrowserMainParts);
  127. };
  128. } // namespace electron
  129. #endif // SHELL_BROWSER_ATOM_BROWSER_MAIN_PARTS_H_