javascript_environment.h 1.5 KB

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