atom_browser_main_parts.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 ATOM_BROWSER_ATOM_BROWSER_MAIN_PARTS_H_
  5. #define ATOM_BROWSER_ATOM_BROWSER_MAIN_PARTS_H_
  6. #include <list>
  7. #include <string>
  8. #include "base/callback.h"
  9. #include "base/timer/timer.h"
  10. #include "brightray/browser/browser_main_parts.h"
  11. #include "content/public/browser/browser_context.h"
  12. #include "services/device/public/mojom/geolocation_control.mojom.h"
  13. class BrowserProcess;
  14. #if defined(TOOLKIT_VIEWS)
  15. namespace brightray {
  16. class ViewsDelegate;
  17. }
  18. #endif
  19. namespace atom {
  20. class AtomBindings;
  21. class Browser;
  22. class JavascriptEnvironment;
  23. class NodeBindings;
  24. class NodeDebugger;
  25. class NodeEnvironment;
  26. class BridgeTaskRunner;
  27. #if defined(OS_MACOSX)
  28. class ViewsDelegateMac;
  29. #endif
  30. class AtomBrowserMainParts : public brightray::BrowserMainParts {
  31. public:
  32. AtomBrowserMainParts();
  33. ~AtomBrowserMainParts() override;
  34. static AtomBrowserMainParts* Get();
  35. // Sets the exit code, will fail if the message loop is not ready.
  36. bool SetExitCode(int code);
  37. // Gets the exit code
  38. int GetExitCode();
  39. // Register a callback that should be destroyed before JavaScript environment
  40. // gets destroyed.
  41. // Returns a closure that can be used to remove |callback| from the list.
  42. void RegisterDestructionCallback(base::OnceClosure callback);
  43. // Returns the connection to GeolocationControl which can be
  44. // used to enable the location services once per client.
  45. device::mojom::GeolocationControl* GetGeolocationControl();
  46. Browser* browser() { return browser_.get(); }
  47. protected:
  48. // content::BrowserMainParts:
  49. int PreEarlyInitialization() override;
  50. void PostEarlyInitialization() override;
  51. int PreCreateThreads() override;
  52. void ToolkitInitialized() override;
  53. void PreMainMessageLoopRun() override;
  54. bool MainMessageLoopRun(int* result_code) override;
  55. void PostMainMessageLoopStart() override;
  56. void PostMainMessageLoopRun() override;
  57. #if defined(OS_MACOSX)
  58. void PreMainMessageLoopStart() override;
  59. #endif
  60. private:
  61. #if defined(OS_POSIX)
  62. // Set signal handlers.
  63. void HandleSIGCHLD();
  64. void HandleShutdownSignals();
  65. #endif
  66. #if defined(OS_MACOSX)
  67. void FreeAppDelegate();
  68. #endif
  69. #if defined(OS_MACOSX)
  70. std::unique_ptr<ViewsDelegateMac> views_delegate_;
  71. #else
  72. std::unique_ptr<brightray::ViewsDelegate> views_delegate_;
  73. #endif
  74. // A fake BrowserProcess object that used to feed the source code from chrome.
  75. std::unique_ptr<BrowserProcess> fake_browser_process_;
  76. // The gin::PerIsolateData requires a task runner to create, so we feed it
  77. // with a task runner that will post all work to main loop.
  78. scoped_refptr<BridgeTaskRunner> bridge_task_runner_;
  79. // Pointer to exit code.
  80. int* exit_code_ = nullptr;
  81. std::unique_ptr<Browser> browser_;
  82. std::unique_ptr<JavascriptEnvironment> js_env_;
  83. std::unique_ptr<NodeBindings> node_bindings_;
  84. std::unique_ptr<AtomBindings> atom_bindings_;
  85. std::unique_ptr<NodeEnvironment> node_env_;
  86. std::unique_ptr<NodeDebugger> node_debugger_;
  87. base::Timer gc_timer_;
  88. // List of callbacks should be executed before destroying JS env.
  89. std::list<base::OnceClosure> destructors_;
  90. device::mojom::GeolocationControlPtr geolocation_control_;
  91. static AtomBrowserMainParts* self_;
  92. DISALLOW_COPY_AND_ASSIGN(AtomBrowserMainParts);
  93. };
  94. } // namespace atom
  95. #endif // ATOM_BROWSER_ATOM_BROWSER_MAIN_PARTS_H_