node_bindings.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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_COMMON_NODE_BINDINGS_H_
  5. #define SHELL_COMMON_NODE_BINDINGS_H_
  6. #include <type_traits>
  7. #include "base/files/file_path.h"
  8. #include "base/macros.h"
  9. #include "base/memory/weak_ptr.h"
  10. #include "uv.h" // NOLINT(build/include_directory)
  11. #include "v8/include/v8.h"
  12. namespace base {
  13. class SingleThreadTaskRunner;
  14. }
  15. namespace node {
  16. class Environment;
  17. class MultiIsolatePlatform;
  18. class IsolateData;
  19. } // namespace node
  20. namespace electron {
  21. // A helper class to manage uv_handle_t types, e.g. uv_async_t.
  22. //
  23. // As per the uv docs: "uv_close() MUST be called on each handle before
  24. // memory is released. Moreover, the memory can only be released in
  25. // close_cb or after it has returned." This class encapsulates the work
  26. // needed to follow those requirements.
  27. template <typename T,
  28. typename std::enable_if<
  29. // these are the C-style 'subclasses' of uv_handle_t
  30. std::is_same<T, uv_async_t>::value ||
  31. std::is_same<T, uv_check_t>::value ||
  32. std::is_same<T, uv_fs_event_t>::value ||
  33. std::is_same<T, uv_fs_poll_t>::value ||
  34. std::is_same<T, uv_idle_t>::value ||
  35. std::is_same<T, uv_pipe_t>::value ||
  36. std::is_same<T, uv_poll_t>::value ||
  37. std::is_same<T, uv_prepare_t>::value ||
  38. std::is_same<T, uv_process_t>::value ||
  39. std::is_same<T, uv_signal_t>::value ||
  40. std::is_same<T, uv_stream_t>::value ||
  41. std::is_same<T, uv_tcp_t>::value ||
  42. std::is_same<T, uv_timer_t>::value ||
  43. std::is_same<T, uv_tty_t>::value ||
  44. std::is_same<T, uv_udp_t>::value>::type* = nullptr>
  45. class UvHandle {
  46. public:
  47. UvHandle() : t_(new T) {}
  48. ~UvHandle() { reset(); }
  49. T* get() { return t_; }
  50. uv_handle_t* handle() { return reinterpret_cast<uv_handle_t*>(t_); }
  51. void reset() {
  52. auto* h = handle();
  53. if (h != nullptr) {
  54. DCHECK_EQ(0, uv_is_closing(h));
  55. uv_close(h, OnClosed);
  56. t_ = nullptr;
  57. }
  58. }
  59. private:
  60. static void OnClosed(uv_handle_t* handle) {
  61. delete reinterpret_cast<T*>(handle);
  62. }
  63. T* t_ = {};
  64. };
  65. class NodeBindings {
  66. public:
  67. enum class BrowserEnvironment { kBrowser, kRenderer, kWorker };
  68. static NodeBindings* Create(BrowserEnvironment browser_env);
  69. static void RegisterBuiltinModules();
  70. static bool IsInitialized();
  71. virtual ~NodeBindings();
  72. // Setup V8, libuv.
  73. void Initialize();
  74. // Create the environment and load node.js.
  75. node::Environment* CreateEnvironment(v8::Handle<v8::Context> context,
  76. node::MultiIsolatePlatform* platform);
  77. // Load node.js in the environment.
  78. void LoadEnvironment(node::Environment* env);
  79. // Prepare for message loop integration.
  80. void PrepareMessageLoop();
  81. // Do message loop integration.
  82. virtual void RunMessageLoop();
  83. node::IsolateData* isolate_data() const { return isolate_data_; }
  84. // Gets/sets the environment to wrap uv loop.
  85. void set_uv_env(node::Environment* env) { uv_env_ = env; }
  86. node::Environment* uv_env() const { return uv_env_; }
  87. uv_loop_t* uv_loop() const { return uv_loop_; }
  88. bool in_worker_loop() const { return uv_loop_ == &worker_loop_; }
  89. protected:
  90. explicit NodeBindings(BrowserEnvironment browser_env);
  91. // Called to poll events in new thread.
  92. virtual void PollEvents() = 0;
  93. // Run the libuv loop for once.
  94. void UvRunOnce();
  95. // Make the main thread run libuv loop.
  96. void WakeupMainThread();
  97. // Interrupt the PollEvents.
  98. void WakeupEmbedThread();
  99. // Which environment we are running.
  100. const BrowserEnvironment browser_env_;
  101. // Current thread's MessageLoop.
  102. scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
  103. // Current thread's libuv loop.
  104. uv_loop_t* uv_loop_;
  105. private:
  106. // Thread to poll uv events.
  107. static void EmbedThreadRunner(void* arg);
  108. // Whether the libuv loop has ended.
  109. bool embed_closed_ = false;
  110. // Loop used when constructed in WORKER mode
  111. uv_loop_t worker_loop_;
  112. // Dummy handle to make uv's loop not quit.
  113. UvHandle<uv_async_t> dummy_uv_handle_;
  114. // Thread for polling events.
  115. uv_thread_t embed_thread_;
  116. // Semaphore to wait for main loop in the embed thread.
  117. uv_sem_t embed_sem_;
  118. // Environment that to wrap the uv loop.
  119. node::Environment* uv_env_ = nullptr;
  120. // Isolate data used in creating the environment
  121. node::IsolateData* isolate_data_ = nullptr;
  122. #if !defined(OS_WIN)
  123. int handle_ = -1;
  124. #endif
  125. base::WeakPtrFactory<NodeBindings> weak_factory_{this};
  126. DISALLOW_COPY_AND_ASSIGN(NodeBindings);
  127. };
  128. } // namespace electron
  129. #endif // SHELL_COMMON_NODE_BINDINGS_H_