javascript_environment.cc 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 "base/task_scheduler/initialization_util.h"
  9. #include "base/threading/thread_task_runner_handle.h"
  10. #include "content/public/common/content_switches.h"
  11. #include "gin/array_buffer.h"
  12. #include "gin/v8_initializer.h"
  13. #include "atom/common/node_includes.h"
  14. #include "tracing/trace_event.h"
  15. namespace atom {
  16. JavascriptEnvironment::JavascriptEnvironment()
  17. : initialized_(Initialize()),
  18. isolate_holder_(base::ThreadTaskRunnerHandle::Get()),
  19. isolate_(isolate_holder_.isolate()),
  20. isolate_scope_(isolate_),
  21. locker_(isolate_),
  22. handle_scope_(isolate_),
  23. context_(isolate_, v8::Context::New(isolate_)),
  24. context_scope_(v8::Local<v8::Context>::New(isolate_, context_)) {}
  25. JavascriptEnvironment::~JavascriptEnvironment() = default;
  26. void JavascriptEnvironment::OnMessageLoopCreated() {
  27. isolate_holder_.AddRunMicrotasksObserver();
  28. }
  29. void JavascriptEnvironment::OnMessageLoopDestroying() {
  30. isolate_holder_.RemoveRunMicrotasksObserver();
  31. }
  32. bool JavascriptEnvironment::Initialize() {
  33. auto* cmd = base::CommandLine::ForCurrentProcess();
  34. // --js-flags.
  35. std::string js_flags = cmd->GetSwitchValueASCII(switches::kJavaScriptFlags);
  36. if (!js_flags.empty())
  37. v8::V8::SetFlagsFromString(js_flags.c_str(), js_flags.size());
  38. // The V8Platform of gin relies on Chromium's task schedule, which has not
  39. // been started at this point, so we have to rely on Node's V8Platform.
  40. platform_ = node::CreatePlatform(
  41. base::RecommendedMaxNumberOfThreadsInPool(3, 8, 0.1, 0), nullptr);
  42. v8::V8::InitializePlatform(platform_);
  43. node::tracing::TraceEventHelper::SetTracingController(
  44. new v8::TracingController());
  45. gin::IsolateHolder::Initialize(
  46. gin::IsolateHolder::kNonStrictMode, gin::IsolateHolder::kStableV8Extras,
  47. gin::ArrayBufferAllocator::SharedInstance(), false);
  48. return true;
  49. }
  50. NodeEnvironment::NodeEnvironment(node::Environment* env) : env_(env) {}
  51. NodeEnvironment::~NodeEnvironment() {
  52. node::FreeEnvironment(env_);
  53. }
  54. } // namespace atom