javascript_environment.h 1.3 KB

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