node_bindings.h 5.5 KB

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