node_bindings.h 2.6 KB

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