node_main.cc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // Copyright (c) 2015 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 "shell/app/node_main.h"
  5. #include <memory>
  6. #include <string>
  7. #include <utility>
  8. #include "base/command_line.h"
  9. #include "base/feature_list.h"
  10. #include "base/task/thread_pool/thread_pool_instance.h"
  11. #include "base/threading/thread_task_runner_handle.h"
  12. #include "electron/electron_version.h"
  13. #include "gin/array_buffer.h"
  14. #include "gin/public/isolate_holder.h"
  15. #include "gin/v8_initializer.h"
  16. #include "shell/app/uv_task_runner.h"
  17. #include "shell/browser/javascript_environment.h"
  18. #include "shell/browser/node_debugger.h"
  19. #include "shell/common/api/electron_bindings.h"
  20. #include "shell/common/crash_reporter/crash_reporter.h"
  21. #include "shell/common/gin_helper/dictionary.h"
  22. #include "shell/common/node_bindings.h"
  23. #include "shell/common/node_includes.h"
  24. #if defined(_WIN64)
  25. #include "shell/common/crash_reporter/crash_reporter_win.h"
  26. #endif
  27. namespace {
  28. bool AllowWasmCodeGenerationCallback(v8::Local<v8::Context> context,
  29. v8::Local<v8::String>) {
  30. v8::Local<v8::Value> wasm_code_gen = context->GetEmbedderData(
  31. node::ContextEmbedderIndex::kAllowWasmCodeGeneration);
  32. return wasm_code_gen->IsUndefined() || wasm_code_gen->IsTrue();
  33. }
  34. } // namespace
  35. namespace electron {
  36. #if !defined(OS_LINUX)
  37. void AddExtraParameter(const std::string& key, const std::string& value) {
  38. crash_reporter::CrashReporter::GetInstance()->AddExtraParameter(key, value);
  39. }
  40. void RemoveExtraParameter(const std::string& key) {
  41. crash_reporter::CrashReporter::GetInstance()->RemoveExtraParameter(key);
  42. }
  43. #endif
  44. int NodeMain(int argc, char* argv[]) {
  45. base::CommandLine::Init(argc, argv);
  46. int exit_code = 1;
  47. {
  48. // Feed gin::PerIsolateData with a task runner.
  49. argv = uv_setup_args(argc, argv);
  50. uv_loop_t* loop = uv_default_loop();
  51. scoped_refptr<UvTaskRunner> uv_task_runner(new UvTaskRunner(loop));
  52. base::ThreadTaskRunnerHandle handle(uv_task_runner);
  53. // Initialize feature list.
  54. auto feature_list = std::make_unique<base::FeatureList>();
  55. feature_list->InitializeFromCommandLine("", "");
  56. base::FeatureList::SetInstance(std::move(feature_list));
  57. #if defined(_WIN64)
  58. crash_reporter::CrashReporterWin::SetUnhandledExceptionFilter();
  59. #endif
  60. // Explicitly register electron's builtin modules.
  61. NodeBindings::RegisterBuiltinModules();
  62. int exec_argc;
  63. const char** exec_argv;
  64. node::Init(&argc, const_cast<const char**>(argv), &exec_argc, &exec_argv);
  65. gin::V8Initializer::LoadV8Snapshot(
  66. gin::V8Initializer::V8SnapshotFileType::kWithAdditionalContext);
  67. // V8 requires a task scheduler apparently
  68. base::ThreadPoolInstance::CreateAndStartWithDefaultParams("Electron");
  69. // Initialize gin::IsolateHolder.
  70. JavascriptEnvironment gin_env(loop);
  71. gin_env.isolate()->SetMicrotasksPolicy(v8::MicrotasksPolicy::kExplicit);
  72. node::IsolateData* isolate_data =
  73. node::CreateIsolateData(gin_env.isolate(), loop, gin_env.platform());
  74. CHECK_NE(nullptr, isolate_data);
  75. node::Environment* env = node::CreateEnvironment(
  76. isolate_data, gin_env.context(), argc, argv, exec_argc, exec_argv);
  77. CHECK_NE(nullptr, env);
  78. // Enable support for v8 inspector.
  79. NodeDebugger node_debugger(env);
  80. node_debugger.Start();
  81. gin_helper::Dictionary process(gin_env.isolate(), env->process_object());
  82. gin_env.isolate()->SetAllowWasmCodeGenerationCallback(
  83. AllowWasmCodeGenerationCallback);
  84. #if defined(OS_WIN)
  85. process.SetMethod("log", &ElectronBindings::Log);
  86. #endif
  87. process.SetMethod("crash", &ElectronBindings::Crash);
  88. // Setup process.crashReporter.start in child node processes
  89. gin_helper::Dictionary reporter =
  90. gin::Dictionary::CreateEmpty(gin_env.isolate());
  91. reporter.SetMethod("start", &crash_reporter::CrashReporter::StartInstance);
  92. #if !defined(OS_LINUX)
  93. reporter.SetMethod("addExtraParameter", &AddExtraParameter);
  94. reporter.SetMethod("removeExtraParameter", &RemoveExtraParameter);
  95. #endif
  96. process.Set("crashReporter", reporter);
  97. gin_helper::Dictionary versions;
  98. if (process.Get("versions", &versions)) {
  99. versions.SetReadOnly(ELECTRON_PROJECT_NAME, ELECTRON_VERSION_STRING);
  100. }
  101. node::LoadEnvironment(env);
  102. env->set_trace_sync_io(env->options()->trace_sync_io);
  103. bool more;
  104. do {
  105. more = uv_run(env->event_loop(), UV_RUN_ONCE);
  106. gin_env.platform()->DrainTasks(env->isolate());
  107. if (more == false) {
  108. node::EmitBeforeExit(env);
  109. // Emit `beforeExit` if the loop became alive either after emitting
  110. // event, or after running some callbacks.
  111. more = uv_loop_alive(env->event_loop());
  112. if (uv_run(env->event_loop(), UV_RUN_NOWAIT) != 0)
  113. more = true;
  114. }
  115. } while (more == true);
  116. node_debugger.Stop();
  117. env->set_trace_sync_io(false);
  118. exit_code = node::EmitExit(env);
  119. node::Stop(env);
  120. node::RunAtExit(env);
  121. v8::Isolate* isolate = env->isolate();
  122. node::FreeEnvironment(env);
  123. node::FreeIsolateData(isolate_data);
  124. gin_env.platform()->DrainTasks(isolate);
  125. gin_env.platform()->CancelPendingDelayedTasks(isolate);
  126. gin_env.platform()->UnregisterIsolate(isolate);
  127. }
  128. // According to "src/gin/shell/gin_main.cc":
  129. //
  130. // gin::IsolateHolder waits for tasks running in ThreadPool in its
  131. // destructor and thus must be destroyed before ThreadPool starts skipping
  132. // CONTINUE_ON_SHUTDOWN tasks.
  133. base::ThreadPoolInstance::Get()->Shutdown();
  134. v8::V8::Dispose();
  135. return exit_code;
  136. }
  137. } // namespace electron