javascript_environment.cc 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. #include "atom/browser/javascript_environment.h"
  5. #include <string>
  6. #include "base/command_line.h"
  7. #include "base/message_loop/message_loop.h"
  8. #include "content/public/common/content_switches.h"
  9. #include "gin/array_buffer.h"
  10. #include "gin/v8_initializer.h"
  11. namespace atom {
  12. JavascriptEnvironment::JavascriptEnvironment()
  13. : initialized_(Initialize()),
  14. isolate_(isolate_holder_.isolate()),
  15. isolate_scope_(isolate_),
  16. locker_(isolate_),
  17. handle_scope_(isolate_),
  18. context_(isolate_, v8::Context::New(isolate_)),
  19. context_scope_(v8::Local<v8::Context>::New(isolate_, context_)) {
  20. }
  21. void JavascriptEnvironment::OnMessageLoopCreated() {
  22. isolate_holder_.AddRunMicrotasksObserver();
  23. }
  24. void JavascriptEnvironment::OnMessageLoopDestroying() {
  25. isolate_holder_.RemoveRunMicrotasksObserver();
  26. }
  27. bool JavascriptEnvironment::Initialize() {
  28. auto cmd = base::CommandLine::ForCurrentProcess();
  29. // --js-flags.
  30. std::string js_flags = cmd->GetSwitchValueASCII(switches::kJavaScriptFlags);
  31. if (!js_flags.empty())
  32. v8::V8::SetFlagsFromString(js_flags.c_str(), js_flags.size());
  33. gin::IsolateHolder::Initialize(gin::IsolateHolder::kNonStrictMode,
  34. gin::IsolateHolder::kStableV8Extras,
  35. gin::ArrayBufferAllocator::SharedInstance());
  36. return true;
  37. }
  38. } // namespace atom