node_bindings.h 4.9 KB

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