node_bindings.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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_COMMON_NODE_BINDINGS_H_
  5. #define ATOM_COMMON_NODE_BINDINGS_H_
  6. #include "base/files/file_path.h"
  7. #include "base/macros.h"
  8. #include "base/memory/weak_ptr.h"
  9. #include "base/single_thread_task_runner.h"
  10. #include "uv.h" // NOLINT(build/include)
  11. #include "v8/include/v8.h"
  12. namespace base {
  13. class MessageLoop;
  14. }
  15. namespace node {
  16. class Environment;
  17. class MultiIsolatePlatform;
  18. } // namespace node
  19. namespace atom {
  20. class NodeBindings {
  21. public:
  22. enum BrowserEnvironment {
  23. BROWSER,
  24. RENDERER,
  25. WORKER,
  26. };
  27. static NodeBindings* Create(BrowserEnvironment browser_env);
  28. static void RegisterBuiltinModules();
  29. static bool IsInitialized();
  30. static base::FilePath::StringType GetHelperResourcesPath();
  31. virtual ~NodeBindings();
  32. // Setup V8, libuv.
  33. void Initialize();
  34. // Create the environment and load node.js.
  35. node::Environment* CreateEnvironment(v8::Handle<v8::Context> context,
  36. node::MultiIsolatePlatform* platform,
  37. bool bootstrap_env);
  38. // Load node.js in the environment.
  39. void LoadEnvironment(node::Environment* env);
  40. // Prepare for message loop integration.
  41. void PrepareMessageLoop();
  42. // Do message loop integration.
  43. virtual void RunMessageLoop();
  44. // Gets/sets the environment to wrap uv loop.
  45. void set_uv_env(node::Environment* env) { uv_env_ = env; }
  46. node::Environment* uv_env() const { return uv_env_; }
  47. uv_loop_t* uv_loop() const { return uv_loop_; }
  48. protected:
  49. explicit NodeBindings(BrowserEnvironment browser_env);
  50. // Called to poll events in new thread.
  51. virtual void PollEvents() = 0;
  52. // Run the libuv loop for once.
  53. void UvRunOnce();
  54. // Make the main thread run libuv loop.
  55. void WakeupMainThread();
  56. // Interrupt the PollEvents.
  57. void WakeupEmbedThread();
  58. // Which environment we are running.
  59. BrowserEnvironment browser_env_;
  60. // Current thread's MessageLoop.
  61. scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
  62. // Current thread's libuv loop.
  63. uv_loop_t* uv_loop_;
  64. private:
  65. // Thread to poll uv events.
  66. static void EmbedThreadRunner(void* arg);
  67. // Whether the libuv loop has ended.
  68. bool embed_closed_ = false;
  69. // Loop used when constructed in WORKER mode
  70. uv_loop_t worker_loop_;
  71. // Dummy handle to make uv's loop not quit.
  72. uv_async_t dummy_uv_handle_;
  73. // Thread for polling events.
  74. uv_thread_t embed_thread_;
  75. // Semaphore to wait for main loop in the embed thread.
  76. uv_sem_t embed_sem_;
  77. // Environment that to wrap the uv loop.
  78. node::Environment* uv_env_ = nullptr;
  79. base::WeakPtrFactory<NodeBindings> weak_factory_;
  80. DISALLOW_COPY_AND_ASSIGN(NodeBindings);
  81. };
  82. } // namespace atom
  83. #endif // ATOM_COMMON_NODE_BINDINGS_H_