node_bindings.h 2.8 KB

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