javascript_environment.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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_BROWSER_JAVASCRIPT_ENVIRONMENT_H_
  5. #define ELECTRON_SHELL_BROWSER_JAVASCRIPT_ENVIRONMENT_H_
  6. #include <memory>
  7. #include "gin/public/isolate_holder.h"
  8. #include "uv.h" // NOLINT(build/include_directory)
  9. #include "v8/include/v8-locker.h"
  10. namespace node {
  11. class Environment;
  12. class MultiIsolatePlatform;
  13. } // namespace node
  14. namespace electron {
  15. class MicrotasksRunner;
  16. // Manage the V8 isolate and context automatically.
  17. class JavascriptEnvironment {
  18. public:
  19. explicit JavascriptEnvironment(uv_loop_t* event_loop);
  20. ~JavascriptEnvironment();
  21. // disable copy
  22. JavascriptEnvironment(const JavascriptEnvironment&) = delete;
  23. JavascriptEnvironment& operator=(const JavascriptEnvironment&) = delete;
  24. void OnMessageLoopCreated();
  25. void OnMessageLoopDestroying();
  26. node::MultiIsolatePlatform* platform() const { return platform_; }
  27. v8::Isolate* isolate() const { return isolate_; }
  28. v8::Local<v8::Context> context() const {
  29. return v8::Local<v8::Context>::New(isolate_, context_);
  30. }
  31. static v8::Isolate* GetIsolate();
  32. private:
  33. v8::Isolate* Initialize(uv_loop_t* event_loop);
  34. // Leaked on exit.
  35. node::MultiIsolatePlatform* platform_;
  36. v8::Isolate* isolate_;
  37. gin::IsolateHolder isolate_holder_;
  38. v8::Locker locker_;
  39. v8::Global<v8::Context> context_;
  40. std::unique_ptr<MicrotasksRunner> microtasks_runner_;
  41. };
  42. // Manage the Node Environment automatically.
  43. class NodeEnvironment {
  44. public:
  45. explicit NodeEnvironment(node::Environment* env);
  46. ~NodeEnvironment();
  47. // disable copy
  48. NodeEnvironment(const NodeEnvironment&) = delete;
  49. NodeEnvironment& operator=(const NodeEnvironment&) = delete;
  50. node::Environment* env() { return env_; }
  51. private:
  52. node::Environment* env_;
  53. };
  54. } // namespace electron
  55. #endif // ELECTRON_SHELL_BROWSER_JAVASCRIPT_ENVIRONMENT_H_