node_main.cc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 electron {
  28. #if !defined(OS_LINUX)
  29. void AddExtraParameter(const std::string& key, const std::string& value) {
  30. crash_reporter::CrashReporter::GetInstance()->AddExtraParameter(key, value);
  31. }
  32. void RemoveExtraParameter(const std::string& key) {
  33. crash_reporter::CrashReporter::GetInstance()->RemoveExtraParameter(key);
  34. }
  35. #endif
  36. int NodeMain(int argc, char* argv[]) {
  37. base::CommandLine::Init(argc, argv);
  38. int exit_code = 1;
  39. {
  40. // Feed gin::PerIsolateData with a task runner.
  41. argv = uv_setup_args(argc, argv);
  42. uv_loop_t* loop = uv_default_loop();
  43. scoped_refptr<UvTaskRunner> uv_task_runner(new UvTaskRunner(loop));
  44. base::ThreadTaskRunnerHandle handle(uv_task_runner);
  45. // Initialize feature list.
  46. auto feature_list = std::make_unique<base::FeatureList>();
  47. feature_list->InitializeFromCommandLine("", "");
  48. base::FeatureList::SetInstance(std::move(feature_list));
  49. #if defined(_WIN64)
  50. crash_reporter::CrashReporterWin::SetUnhandledExceptionFilter();
  51. #endif
  52. // Explicitly register electron's builtin modules.
  53. NodeBindings::RegisterBuiltinModules();
  54. int exec_argc;
  55. const char** exec_argv;
  56. node::Init(&argc, const_cast<const char**>(argv), &exec_argc, &exec_argv);
  57. gin::V8Initializer::LoadV8Snapshot(
  58. gin::V8Initializer::V8SnapshotFileType::kWithAdditionalContext);
  59. // V8 requires a task scheduler apparently
  60. base::ThreadPoolInstance::CreateAndStartWithDefaultParams("Electron");
  61. // Initialize gin::IsolateHolder.
  62. JavascriptEnvironment gin_env(loop);
  63. node::IsolateData* isolate_data =
  64. node::CreateIsolateData(gin_env.isolate(), loop, gin_env.platform());
  65. CHECK_NE(nullptr, isolate_data);
  66. node::Environment* env =
  67. node::CreateEnvironment(isolate_data, gin_env.context(), argc, argv,
  68. exec_argc, exec_argv, false);
  69. CHECK_NE(nullptr, env);
  70. // Enable support for v8 inspector.
  71. NodeDebugger node_debugger(env);
  72. node_debugger.Start();
  73. node::BootstrapEnvironment(env);
  74. gin_helper::Dictionary process(gin_env.isolate(), env->process_object());
  75. #if defined(OS_WIN)
  76. process.SetMethod("log", &ElectronBindings::Log);
  77. #endif
  78. process.SetMethod("crash", &ElectronBindings::Crash);
  79. // Setup process.crashReporter.start in child node processes
  80. gin_helper::Dictionary reporter =
  81. gin::Dictionary::CreateEmpty(gin_env.isolate());
  82. reporter.SetMethod("start", &crash_reporter::CrashReporter::StartInstance);
  83. #if !defined(OS_LINUX)
  84. reporter.SetMethod("addExtraParameter", &AddExtraParameter);
  85. reporter.SetMethod("removeExtraParameter", &RemoveExtraParameter);
  86. #endif
  87. process.Set("crashReporter", reporter);
  88. gin_helper::Dictionary versions;
  89. if (process.Get("versions", &versions)) {
  90. versions.SetReadOnly(ELECTRON_PROJECT_NAME, ELECTRON_VERSION_STRING);
  91. }
  92. node::LoadEnvironment(env);
  93. bool more;
  94. do {
  95. more = uv_run(env->event_loop(), UV_RUN_ONCE);
  96. gin_env.platform()->DrainTasks(env->isolate());
  97. if (more == false) {
  98. node::EmitBeforeExit(env);
  99. // Emit `beforeExit` if the loop became alive either after emitting
  100. // event, or after running some callbacks.
  101. more = uv_loop_alive(env->event_loop());
  102. if (uv_run(env->event_loop(), UV_RUN_NOWAIT) != 0)
  103. more = true;
  104. }
  105. } while (more == true);
  106. node_debugger.Stop();
  107. exit_code = node::EmitExit(env);
  108. env->set_can_call_into_js(false);
  109. node::RunAtExit(env);
  110. v8::Isolate* isolate = env->isolate();
  111. node::FreeEnvironment(env);
  112. node::FreeIsolateData(isolate_data);
  113. gin_env.platform()->DrainTasks(isolate);
  114. gin_env.platform()->CancelPendingDelayedTasks(isolate);
  115. gin_env.platform()->UnregisterIsolate(isolate);
  116. }
  117. // According to "src/gin/shell/gin_main.cc":
  118. //
  119. // gin::IsolateHolder waits for tasks running in ThreadPool in its
  120. // destructor and thus must be destroyed before ThreadPool starts skipping
  121. // CONTINUE_ON_SHUTDOWN tasks.
  122. base::ThreadPoolInstance::Get()->Shutdown();
  123. v8::V8::Dispose();
  124. return exit_code;
  125. }
  126. } // namespace electron