node_main.cc 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 <map>
  6. #include <memory>
  7. #include <string>
  8. #include <utility>
  9. #include "base/base_switches.h"
  10. #include "base/command_line.h"
  11. #include "base/feature_list.h"
  12. #include "base/task/thread_pool/thread_pool_instance.h"
  13. #include "base/threading/thread_task_runner_handle.h"
  14. #include "content/public/common/content_switches.h"
  15. #include "electron/electron_version.h"
  16. #include "gin/array_buffer.h"
  17. #include "gin/public/isolate_holder.h"
  18. #include "gin/v8_initializer.h"
  19. #include "shell/app/uv_task_runner.h"
  20. #include "shell/browser/javascript_environment.h"
  21. #include "shell/browser/node_debugger.h"
  22. #include "shell/common/api/electron_bindings.h"
  23. #include "shell/common/gin_helper/dictionary.h"
  24. #include "shell/common/node_bindings.h"
  25. #include "shell/common/node_includes.h"
  26. #if defined(OS_LINUX)
  27. #include "components/crash/core/app/breakpad_linux.h"
  28. #endif
  29. #if defined(OS_WIN)
  30. #include "chrome/child/v8_crashpad_support_win.h"
  31. #endif
  32. #if !defined(MAS_BUILD)
  33. #include "components/crash/core/app/crashpad.h" // nogncheck
  34. #include "shell/app/electron_crash_reporter_client.h"
  35. #include "shell/browser/api/electron_api_crash_reporter.h"
  36. #include "shell/common/crash_keys.h"
  37. #endif
  38. namespace {
  39. bool AllowWasmCodeGenerationCallback(v8::Local<v8::Context> context,
  40. v8::Local<v8::String>) {
  41. v8::Local<v8::Value> wasm_code_gen = context->GetEmbedderData(
  42. node::ContextEmbedderIndex::kAllowWasmCodeGeneration);
  43. return wasm_code_gen->IsUndefined() || wasm_code_gen->IsTrue();
  44. }
  45. #if defined(MAS_BUILD)
  46. void SetCrashKeyStub(const std::string& key, const std::string& value) {}
  47. void ClearCrashKeyStub(const std::string& key) {}
  48. #endif
  49. } // namespace
  50. namespace electron {
  51. #if defined(OS_LINUX)
  52. void CrashReporterStart(gin_helper::Dictionary options) {
  53. std::string submit_url;
  54. bool upload_to_server = true;
  55. bool ignore_system_crash_handler = false;
  56. bool rate_limit = false;
  57. bool compress = false;
  58. std::map<std::string, std::string> global_extra;
  59. std::map<std::string, std::string> extra;
  60. options.Get("submitURL", &submit_url);
  61. options.Get("uploadToServer", &upload_to_server);
  62. options.Get("ignoreSystemCrashHandler", &ignore_system_crash_handler);
  63. options.Get("rateLimit", &rate_limit);
  64. options.Get("compress", &compress);
  65. options.Get("extra", &extra);
  66. options.Get("globalExtra", &global_extra);
  67. std::string product_name;
  68. if (options.Get("productName", &product_name))
  69. global_extra["_productName"] = product_name;
  70. std::string company_name;
  71. if (options.Get("companyName", &company_name))
  72. global_extra["_companyName"] = company_name;
  73. api::crash_reporter::Start(submit_url, upload_to_server,
  74. ignore_system_crash_handler, rate_limit, compress,
  75. global_extra, extra, true);
  76. }
  77. #endif
  78. v8::Local<v8::Value> GetParameters(v8::Isolate* isolate) {
  79. std::map<std::string, std::string> keys;
  80. #if !defined(MAS_BUILD)
  81. electron::crash_keys::GetCrashKeys(&keys);
  82. #endif
  83. return gin::ConvertToV8(isolate, keys);
  84. }
  85. int NodeMain(int argc, char* argv[]) {
  86. base::CommandLine::Init(argc, argv);
  87. #if defined(OS_WIN)
  88. v8_crashpad_support::SetUp();
  89. #endif
  90. #if !defined(MAS_BUILD)
  91. ElectronCrashReporterClient::Create();
  92. #endif
  93. #if defined(OS_WIN) || (defined(OS_MACOSX) && !defined(MAS_BUILD))
  94. crash_reporter::InitializeCrashpad(false, "node");
  95. #endif
  96. #if !defined(MAS_BUILD)
  97. crash_keys::SetCrashKeysFromCommandLine(
  98. *base::CommandLine::ForCurrentProcess());
  99. crash_keys::SetPlatformCrashKey();
  100. #endif
  101. int exit_code = 1;
  102. {
  103. // Feed gin::PerIsolateData with a task runner.
  104. argv = uv_setup_args(argc, argv);
  105. uv_loop_t* loop = uv_default_loop();
  106. scoped_refptr<UvTaskRunner> uv_task_runner(new UvTaskRunner(loop));
  107. base::ThreadTaskRunnerHandle handle(uv_task_runner);
  108. // Initialize feature list.
  109. auto feature_list = std::make_unique<base::FeatureList>();
  110. feature_list->InitializeFromCommandLine("", "");
  111. base::FeatureList::SetInstance(std::move(feature_list));
  112. // Explicitly register electron's builtin modules.
  113. NodeBindings::RegisterBuiltinModules();
  114. int exec_argc;
  115. const char** exec_argv;
  116. node::Init(&argc, const_cast<const char**>(argv), &exec_argc, &exec_argv);
  117. gin::V8Initializer::LoadV8Snapshot(
  118. gin::V8Initializer::V8SnapshotFileType::kWithAdditionalContext);
  119. // V8 requires a task scheduler apparently
  120. base::ThreadPoolInstance::CreateAndStartWithDefaultParams("Electron");
  121. // Initialize gin::IsolateHolder.
  122. JavascriptEnvironment gin_env(loop);
  123. v8::Isolate* isolate = gin_env.isolate();
  124. isolate->SetMicrotasksPolicy(v8::MicrotasksPolicy::kExplicit);
  125. node::IsolateData* isolate_data =
  126. node::CreateIsolateData(isolate, loop, gin_env.platform());
  127. CHECK_NE(nullptr, isolate_data);
  128. v8::Locker locker(isolate);
  129. v8::Isolate::Scope isolate_scope(isolate);
  130. v8::HandleScope handle_scope(isolate);
  131. node::Environment* env = node::CreateEnvironment(
  132. isolate_data, gin_env.context(), argc, argv, exec_argc, exec_argv);
  133. CHECK_NE(nullptr, env);
  134. // Enable support for v8 inspector.
  135. NodeDebugger node_debugger(env);
  136. node_debugger.Start();
  137. gin_helper::Dictionary process(isolate, env->process_object());
  138. isolate->SetAllowWasmCodeGenerationCallback(
  139. AllowWasmCodeGenerationCallback);
  140. #if defined(OS_WIN)
  141. process.SetMethod("log", &ElectronBindings::Log);
  142. #endif
  143. process.SetMethod("crash", &ElectronBindings::Crash);
  144. // Setup process.crashReporter.start in child node processes
  145. gin_helper::Dictionary reporter = gin::Dictionary::CreateEmpty(isolate);
  146. #if defined(OS_LINUX)
  147. reporter.SetMethod("start", &CrashReporterStart);
  148. #endif
  149. reporter.SetMethod("getParameters", &GetParameters);
  150. #if defined(MAS_BUILD)
  151. reporter.SetMethod("addExtraParameter", &SetCrashKeyStub);
  152. reporter.SetMethod("removeExtraParameter", &ClearCrashKeyStub);
  153. #else
  154. reporter.SetMethod("addExtraParameter", &electron::crash_keys::SetCrashKey);
  155. reporter.SetMethod("removeExtraParameter",
  156. &electron::crash_keys::ClearCrashKey);
  157. #endif
  158. process.Set("crashReporter", reporter);
  159. gin_helper::Dictionary versions;
  160. if (process.Get("versions", &versions)) {
  161. versions.SetReadOnly(ELECTRON_PROJECT_NAME, ELECTRON_VERSION_STRING);
  162. }
  163. node::LoadEnvironment(env);
  164. env->set_trace_sync_io(env->options()->trace_sync_io);
  165. {
  166. v8::SealHandleScope seal(isolate);
  167. bool more;
  168. do {
  169. uv_run(env->event_loop(), UV_RUN_DEFAULT);
  170. gin_env.platform()->DrainTasks(isolate);
  171. more = uv_loop_alive(env->event_loop());
  172. if (more && !env->is_stopping())
  173. continue;
  174. if (!uv_loop_alive(env->event_loop())) {
  175. EmitBeforeExit(env);
  176. }
  177. // Emit `beforeExit` if the loop became alive either after emitting
  178. // event, or after running some callbacks.
  179. more = uv_loop_alive(env->event_loop());
  180. } while (more && !env->is_stopping());
  181. }
  182. node_debugger.Stop();
  183. env->set_trace_sync_io(false);
  184. exit_code = node::EmitExit(env);
  185. node::ResetStdio();
  186. node::Stop(env);
  187. env->stop_sub_worker_contexts();
  188. env->RunCleanup();
  189. node::RunAtExit(env);
  190. node::FreeEnvironment(env);
  191. node::FreeIsolateData(isolate_data);
  192. gin_env.platform()->DrainTasks(isolate);
  193. gin_env.platform()->CancelPendingDelayedTasks(isolate);
  194. gin_env.platform()->UnregisterIsolate(isolate);
  195. }
  196. // According to "src/gin/shell/gin_main.cc":
  197. //
  198. // gin::IsolateHolder waits for tasks running in ThreadPool in its
  199. // destructor and thus must be destroyed before ThreadPool starts skipping
  200. // CONTINUE_ON_SHUTDOWN tasks.
  201. base::ThreadPoolInstance::Get()->Shutdown();
  202. v8::V8::Dispose();
  203. return exit_code;
  204. }
  205. } // namespace electron