atom_browser_main_parts.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. class BrowserProcess;
  13. namespace atom {
  14. class AtomBindings;
  15. class Browser;
  16. class JavascriptEnvironment;
  17. class NodeBindings;
  18. class NodeDebugger;
  19. class NodeEnvironment;
  20. class BridgeTaskRunner;
  21. class AtomBrowserMainParts : public brightray::BrowserMainParts {
  22. public:
  23. AtomBrowserMainParts();
  24. virtual ~AtomBrowserMainParts();
  25. static AtomBrowserMainParts* Get();
  26. // Sets the exit code, will fail if the message loop is not ready.
  27. bool SetExitCode(int code);
  28. // Gets the exit code
  29. int GetExitCode();
  30. // Register a callback that should be destroyed before JavaScript environment
  31. // gets destroyed.
  32. // Returns a closure that can be used to remove |callback| from the list.
  33. base::Closure RegisterDestructionCallback(const base::Closure& callback);
  34. Browser* browser() { return browser_.get(); }
  35. protected:
  36. // content::BrowserMainParts:
  37. void PreEarlyInitialization() override;
  38. void PostEarlyInitialization() override;
  39. void PreMainMessageLoopRun() override;
  40. bool MainMessageLoopRun(int* result_code) override;
  41. void PostMainMessageLoopStart() override;
  42. void PostMainMessageLoopRun() override;
  43. #if defined(OS_MACOSX)
  44. void PreMainMessageLoopStart() override;
  45. #endif
  46. private:
  47. #if defined(OS_POSIX)
  48. // Set signal handlers.
  49. void HandleSIGCHLD();
  50. void HandleShutdownSignals();
  51. #endif
  52. #if defined(OS_MACOSX)
  53. void FreeAppDelegate();
  54. #endif
  55. // A fake BrowserProcess object that used to feed the source code from chrome.
  56. std::unique_ptr<BrowserProcess> fake_browser_process_;
  57. // The gin::PerIsolateData requires a task runner to create, so we feed it
  58. // with a task runner that will post all work to main loop.
  59. scoped_refptr<BridgeTaskRunner> bridge_task_runner_;
  60. // Pointer to exit code.
  61. int* exit_code_;
  62. std::unique_ptr<Browser> browser_;
  63. std::unique_ptr<JavascriptEnvironment> js_env_;
  64. std::unique_ptr<NodeBindings> node_bindings_;
  65. std::unique_ptr<AtomBindings> atom_bindings_;
  66. std::unique_ptr<NodeEnvironment> node_env_;
  67. std::unique_ptr<NodeDebugger> node_debugger_;
  68. base::Timer gc_timer_;
  69. // List of callbacks should be executed before destroying JS env.
  70. std::list<base::Closure> destructors_;
  71. static AtomBrowserMainParts* self_;
  72. DISALLOW_COPY_AND_ASSIGN(AtomBrowserMainParts);
  73. };
  74. } // namespace atom
  75. #endif // ATOM_BROWSER_ATOM_BROWSER_MAIN_PARTS_H_