node_bindings.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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 ELECTRON_SHELL_COMMON_NODE_BINDINGS_H_
  5. #define ELECTRON_SHELL_COMMON_NODE_BINDINGS_H_
  6. #include <memory>
  7. #include <optional>
  8. #include <string>
  9. #include <type_traits>
  10. #include <vector>
  11. #include "base/files/file_path.h"
  12. #include "base/functional/callback.h"
  13. #include "base/memory/raw_ptr.h"
  14. #include "base/memory/raw_ptr_exclusion.h"
  15. #include "base/memory/weak_ptr.h"
  16. #include "base/types/to_address.h"
  17. #include "gin/public/context_holder.h"
  18. #include "gin/public/gin_embedders.h"
  19. #include "shell/common/node_includes.h"
  20. #include "uv.h" // NOLINT(build/include_directory)
  21. #include "v8/include/v8.h"
  22. namespace base {
  23. class SingleThreadTaskRunner;
  24. }
  25. namespace electron {
  26. // A helper class to manage uv_handle_t types, e.g. uv_async_t.
  27. //
  28. // As per the uv docs: "uv_close() MUST be called on each handle before
  29. // memory is released. Moreover, the memory can only be released in
  30. // close_cb or after it has returned." This class encapsulates the work
  31. // needed to follow those requirements.
  32. template <typename T,
  33. typename std::enable_if<
  34. // these are the C-style 'subclasses' of uv_handle_t
  35. std::is_same<T, uv_async_t>::value ||
  36. std::is_same<T, uv_check_t>::value ||
  37. std::is_same<T, uv_fs_event_t>::value ||
  38. std::is_same<T, uv_fs_poll_t>::value ||
  39. std::is_same<T, uv_idle_t>::value ||
  40. std::is_same<T, uv_pipe_t>::value ||
  41. std::is_same<T, uv_poll_t>::value ||
  42. std::is_same<T, uv_prepare_t>::value ||
  43. std::is_same<T, uv_process_t>::value ||
  44. std::is_same<T, uv_signal_t>::value ||
  45. std::is_same<T, uv_stream_t>::value ||
  46. std::is_same<T, uv_tcp_t>::value ||
  47. std::is_same<T, uv_timer_t>::value ||
  48. std::is_same<T, uv_tty_t>::value ||
  49. std::is_same<T, uv_udp_t>::value>::type* = nullptr>
  50. class UvHandle {
  51. public:
  52. UvHandle() : t_{new T} {}
  53. ~UvHandle() { reset(); }
  54. explicit UvHandle(UvHandle&& that) {
  55. t_ = that.t_;
  56. that.t_ = nullptr;
  57. }
  58. UvHandle& operator=(UvHandle&& that) {
  59. reset();
  60. t_ = that.t_;
  61. that.t_ = nullptr;
  62. return *this;
  63. }
  64. UvHandle(const UvHandle&) = delete;
  65. UvHandle& operator=(const UvHandle&) = delete;
  66. T* get() { return t_; }
  67. T* operator->() { return t_; }
  68. const T* get() const { return t_; }
  69. const T* operator->() const { return t_; }
  70. uv_handle_t* handle() { return reinterpret_cast<uv_handle_t*>(t_); }
  71. // compare by handle pointer address
  72. auto operator<=>(const UvHandle& that) const = default;
  73. void reset() {
  74. auto* h = handle();
  75. if (h != nullptr) {
  76. DCHECK_EQ(0, uv_is_closing(h));
  77. uv_close(h, OnClosed);
  78. t_ = nullptr;
  79. }
  80. }
  81. private:
  82. static void OnClosed(uv_handle_t* handle) {
  83. delete reinterpret_cast<T*>(handle);
  84. }
  85. RAW_PTR_EXCLUSION T* t_ = {};
  86. };
  87. // Helper for comparing UvHandles and raw uv pointers, e.g. as map keys
  88. struct UvHandleCompare {
  89. using is_transparent = void;
  90. template <typename U, typename V>
  91. bool operator()(U const& u, V const& v) const {
  92. return base::to_address(u) < base::to_address(v);
  93. }
  94. };
  95. class NodeBindings {
  96. public:
  97. enum class BrowserEnvironment { kBrowser, kRenderer, kUtility, kWorker };
  98. static std::unique_ptr<NodeBindings> Create(BrowserEnvironment browser_env);
  99. static void RegisterBuiltinBindings();
  100. static bool IsInitialized();
  101. virtual ~NodeBindings();
  102. // Setup V8, libuv.
  103. void Initialize(v8::Local<v8::Context> context);
  104. std::vector<std::string> ParseNodeCliFlags();
  105. // Create the environment and load node.js.
  106. std::shared_ptr<node::Environment> CreateEnvironment(
  107. v8::Local<v8::Context> context,
  108. node::MultiIsolatePlatform* platform,
  109. std::vector<std::string> args,
  110. std::vector<std::string> exec_args,
  111. std::optional<base::RepeatingCallback<void()>> on_app_code_ready =
  112. std::nullopt);
  113. std::shared_ptr<node::Environment> CreateEnvironment(
  114. v8::Local<v8::Context> context,
  115. node::MultiIsolatePlatform* platform,
  116. std::optional<base::RepeatingCallback<void()>> on_app_code_ready =
  117. std::nullopt);
  118. // Load node.js in the environment.
  119. void LoadEnvironment(node::Environment* env);
  120. // Prepare embed thread for message loop integration.
  121. void PrepareEmbedThread();
  122. // Notify embed thread to start polling after environment is loaded.
  123. void StartPolling();
  124. node::IsolateData* isolate_data(v8::Local<v8::Context> context) const {
  125. if (context->GetNumberOfEmbedderDataFields() <=
  126. kElectronContextEmbedderDataIndex) {
  127. return nullptr;
  128. }
  129. auto* isolate_data = static_cast<node::IsolateData*>(
  130. context->GetAlignedPointerFromEmbedderData(
  131. kElectronContextEmbedderDataIndex));
  132. CHECK(isolate_data);
  133. CHECK(isolate_data->event_loop());
  134. return isolate_data;
  135. }
  136. // Gets/sets the environment to wrap uv loop.
  137. void set_uv_env(node::Environment* env) { uv_env_ = env; }
  138. node::Environment* uv_env() const { return uv_env_; }
  139. [[nodiscard]] constexpr uv_loop_t* uv_loop() { return uv_loop_; }
  140. // disable copy
  141. NodeBindings(const NodeBindings&) = delete;
  142. NodeBindings& operator=(const NodeBindings&) = delete;
  143. // Blocks until app code is signaled to be loaded via |SetAppCodeLoaded|.
  144. // Only has an effect if called in the browser process
  145. void JoinAppCode();
  146. protected:
  147. explicit NodeBindings(BrowserEnvironment browser_env);
  148. // Called to poll events in new thread.
  149. virtual void PollEvents() = 0;
  150. // Make the main thread run libuv loop.
  151. void WakeupMainThread();
  152. // Interrupt the PollEvents.
  153. void WakeupEmbedThread();
  154. private:
  155. static uv_loop_t* InitEventLoop(BrowserEnvironment browser_env,
  156. uv_loop_t* worker_loop);
  157. // Run the libuv loop for once.
  158. void UvRunOnce();
  159. [[nodiscard]] constexpr bool in_worker_loop() const {
  160. return browser_env_ == BrowserEnvironment::kWorker;
  161. }
  162. // Which environment we are running.
  163. const BrowserEnvironment browser_env_;
  164. // Loop used when constructed in WORKER mode
  165. uv_loop_t worker_loop_;
  166. // Current thread's libuv loop.
  167. // depends-on: worker_loop_
  168. const raw_ptr<uv_loop_t> uv_loop_;
  169. // Current thread's MessageLoop.
  170. scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
  171. // Choose a reasonable unique index that's higher than any Blink uses
  172. // and thus unlikely to collide with an existing index.
  173. static constexpr int kElectronContextEmbedderDataIndex =
  174. static_cast<int>(gin::kPerContextDataStartIndex) +
  175. static_cast<int>(gin::kEmbedderElectron);
  176. // Thread to poll uv events.
  177. static void EmbedThreadRunner(void* arg);
  178. // Default callback to indicate when the node environment has finished
  179. // initializing and the primary import chain is fully resolved and executed
  180. void SetAppCodeLoaded();
  181. // Indicates whether polling thread has been created.
  182. bool initialized_ = false;
  183. // Indicates whether the app code has finished loading
  184. // for ESM this is async after the module is loaded
  185. bool app_code_loaded_ = false;
  186. // Whether the libuv loop has ended.
  187. bool embed_closed_ = false;
  188. // Dummy handle to make uv's loop not quit.
  189. UvHandle<uv_async_t> dummy_uv_handle_;
  190. // Thread for polling events.
  191. uv_thread_t embed_thread_;
  192. // Semaphore to wait for main loop in the embed thread.
  193. uv_sem_t embed_sem_;
  194. // Environment that to wrap the uv loop.
  195. raw_ptr<node::Environment> uv_env_ = nullptr;
  196. // Isolate data used in creating the environment
  197. raw_ptr<node::IsolateData> isolate_data_ = nullptr;
  198. base::WeakPtrFactory<NodeBindings> weak_factory_{this};
  199. };
  200. // A thread-safe function responsible for loading preload script which runs for
  201. // all node environments (including child processes and workers).
  202. void OnNodePreload(node::Environment* env,
  203. v8::Local<v8::Value> process,
  204. v8::Local<v8::Value> require);
  205. } // namespace electron
  206. #endif // ELECTRON_SHELL_COMMON_NODE_BINDINGS_H_