javascript_environment.h 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 ATOM_BROWSER_JAVASCRIPT_ENVIRONMENT_H_
  5. #define ATOM_BROWSER_JAVASCRIPT_ENVIRONMENT_H_
  6. #include "base/macros.h"
  7. #include "gin/public/isolate_holder.h"
  8. namespace node {
  9. class Environment;
  10. class MultiIsolatePlatform;
  11. } // namespace node
  12. namespace atom {
  13. // Manage the V8 isolate and context automatically.
  14. class JavascriptEnvironment {
  15. public:
  16. JavascriptEnvironment();
  17. ~JavascriptEnvironment();
  18. void OnMessageLoopCreated();
  19. void OnMessageLoopDestroying();
  20. node::MultiIsolatePlatform* platform() const { return platform_; }
  21. v8::Isolate* isolate() const { return isolate_; }
  22. v8::Local<v8::Context> context() const {
  23. return v8::Local<v8::Context>::New(isolate_, context_);
  24. }
  25. private:
  26. bool Initialize();
  27. // Leaked on exit.
  28. node::MultiIsolatePlatform* platform_;
  29. bool initialized_;
  30. gin::IsolateHolder isolate_holder_;
  31. v8::Isolate* isolate_;
  32. v8::Isolate::Scope isolate_scope_;
  33. v8::Locker locker_;
  34. v8::HandleScope handle_scope_;
  35. v8::UniquePersistent<v8::Context> context_;
  36. v8::Context::Scope context_scope_;
  37. DISALLOW_COPY_AND_ASSIGN(JavascriptEnvironment);
  38. };
  39. // Manage the Node Environment automatically.
  40. class NodeEnvironment {
  41. public:
  42. explicit NodeEnvironment(node::Environment* env);
  43. ~NodeEnvironment();
  44. private:
  45. node::Environment* env_;
  46. DISALLOW_COPY_AND_ASSIGN(NodeEnvironment);
  47. };
  48. } // namespace atom
  49. #endif // ATOM_BROWSER_JAVASCRIPT_ENVIRONMENT_H_