electron_crash_reporter_client.cc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. // Copyright 2013 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "shell/app/electron_crash_reporter_client.h"
  5. #include <map>
  6. #include <string>
  7. #include "base/environment.h"
  8. #include "base/files/file_path.h"
  9. #include "base/files/file_util.h"
  10. #include "base/path_service.h"
  11. #include "base/strings/utf_string_conversions.h"
  12. #include "build/build_config.h"
  13. #include "components/crash/core/common/crash_keys.h"
  14. #include "components/upload_list/crash_upload_list.h"
  15. #include "content/public/common/content_switches.h"
  16. #include "electron/electron_version.h"
  17. #include "shell/common/electron_paths.h"
  18. #include "shell/common/thread_restrictions.h"
  19. #if BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_MAC)
  20. #include "components/version_info/version_info_values.h"
  21. #endif
  22. #if BUILDFLAG(IS_POSIX)
  23. #include "base/debug/dump_without_crashing.h"
  24. #endif
  25. #if BUILDFLAG(IS_WIN)
  26. #include "base/strings/string_util_win.h"
  27. #endif
  28. namespace {
  29. ElectronCrashReporterClient* Instance() {
  30. static base::NoDestructor<ElectronCrashReporterClient> crash_client;
  31. return crash_client.get();
  32. }
  33. } // namespace
  34. // static
  35. void ElectronCrashReporterClient::Create() {
  36. crash_reporter::SetCrashReporterClient(Instance());
  37. // By setting the BREAKPAD_DUMP_LOCATION environment variable, an alternate
  38. // location to write crash dumps can be set.
  39. auto env = base::Environment::Create();
  40. std::string alternate_crash_dump_location;
  41. base::FilePath crash_dumps_dir_path;
  42. if (env->GetVar("BREAKPAD_DUMP_LOCATION", &alternate_crash_dump_location)) {
  43. crash_dumps_dir_path =
  44. base::FilePath::FromUTF8Unsafe(alternate_crash_dump_location);
  45. }
  46. if (!crash_dumps_dir_path.empty()) {
  47. electron::ScopedAllowBlockingForElectron allow_blocking;
  48. base::PathService::Override(electron::DIR_CRASH_DUMPS,
  49. crash_dumps_dir_path);
  50. }
  51. }
  52. // static
  53. ElectronCrashReporterClient* ElectronCrashReporterClient::Get() {
  54. return Instance();
  55. }
  56. void ElectronCrashReporterClient::SetCollectStatsConsent(bool upload_allowed) {
  57. collect_stats_consent_ = upload_allowed;
  58. }
  59. void ElectronCrashReporterClient::SetUploadUrl(const std::string& url) {
  60. upload_url_ = url;
  61. }
  62. void ElectronCrashReporterClient::SetShouldRateLimit(bool rate_limit) {
  63. rate_limit_ = rate_limit;
  64. }
  65. void ElectronCrashReporterClient::SetShouldCompressUploads(bool compress) {
  66. compress_uploads_ = compress;
  67. }
  68. void ElectronCrashReporterClient::SetGlobalAnnotations(
  69. const std::map<std::string, std::string>& annotations) {
  70. global_annotations_ = annotations;
  71. }
  72. ElectronCrashReporterClient::ElectronCrashReporterClient() = default;
  73. ElectronCrashReporterClient::~ElectronCrashReporterClient() = default;
  74. #if BUILDFLAG(IS_LINUX)
  75. void ElectronCrashReporterClient::SetCrashReporterClientIdFromGUID(
  76. const std::string& client_guid) {
  77. crash_keys::SetMetricsClientIdFromGUID(client_guid);
  78. }
  79. base::FilePath ElectronCrashReporterClient::GetReporterLogFilename() {
  80. return base::FilePath(CrashUploadList::kReporterLogFilename);
  81. }
  82. #endif
  83. #if BUILDFLAG(IS_WIN)
  84. void ElectronCrashReporterClient::GetProductNameAndVersion(
  85. const std::wstring& exe_path,
  86. std::wstring* product_name,
  87. std::wstring* version,
  88. std::wstring* special_build,
  89. std::wstring* channel_name) {
  90. *product_name = base::UTF8ToWide(ELECTRON_PRODUCT_NAME);
  91. *version = base::UTF8ToWide(ELECTRON_VERSION_STRING);
  92. }
  93. #endif
  94. #if BUILDFLAG(IS_WIN)
  95. bool ElectronCrashReporterClient::GetCrashDumpLocation(
  96. std::wstring* crash_dir_str) {
  97. base::FilePath crash_dir;
  98. if (!base::PathService::Get(electron::DIR_CRASH_DUMPS, &crash_dir))
  99. return false;
  100. *crash_dir_str = crash_dir.value();
  101. return true;
  102. }
  103. #else
  104. bool ElectronCrashReporterClient::GetCrashDumpLocation(
  105. base::FilePath* crash_dir) {
  106. bool result = base::PathService::Get(electron::DIR_CRASH_DUMPS, crash_dir);
  107. {
  108. // If the DIR_CRASH_DUMPS path is overridden with
  109. // app.setPath('crashDumps', ...) then the directory might not have been
  110. // created.
  111. electron::ScopedAllowBlockingForElectron allow_blocking;
  112. if (result && !base::PathExists(*crash_dir)) {
  113. return base::CreateDirectory(*crash_dir);
  114. }
  115. }
  116. return result;
  117. }
  118. #endif
  119. bool ElectronCrashReporterClient::IsRunningUnattended() {
  120. return !collect_stats_consent_;
  121. }
  122. bool ElectronCrashReporterClient::GetCollectStatsConsent() {
  123. return collect_stats_consent_;
  124. }
  125. #if BUILDFLAG(IS_MAC)
  126. bool ElectronCrashReporterClient::ReportingIsEnforcedByPolicy(
  127. bool* breakpad_enabled) {
  128. return false;
  129. }
  130. #endif
  131. bool ElectronCrashReporterClient::GetShouldRateLimit() {
  132. return rate_limit_;
  133. }
  134. bool ElectronCrashReporterClient::GetShouldCompressUploads() {
  135. return compress_uploads_;
  136. }
  137. void ElectronCrashReporterClient::GetProcessSimpleAnnotations(
  138. std::map<std::string, std::string>* annotations) {
  139. for (auto&& pair : global_annotations_) {
  140. (*annotations)[pair.first] = pair.second;
  141. }
  142. (*annotations)["prod"] = ELECTRON_PRODUCT_NAME;
  143. (*annotations)["ver"] = ELECTRON_VERSION_STRING;
  144. }
  145. #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_MAC)
  146. bool ElectronCrashReporterClient::ShouldMonitorCrashHandlerExpensively() {
  147. return false;
  148. }
  149. #endif
  150. std::string ElectronCrashReporterClient::GetUploadUrl() {
  151. return upload_url_;
  152. }
  153. void ElectronCrashReporterClient::GetProductInfo(ProductInfo* product_info) {
  154. product_info->product_name = ELECTRON_PRODUCT_NAME;
  155. product_info->version = ELECTRON_VERSION_STRING;
  156. }
  157. bool ElectronCrashReporterClient::EnableBreakpadForProcess(
  158. const std::string& process_type) {
  159. return process_type == switches::kRendererProcess ||
  160. process_type == switches::kZygoteProcess ||
  161. process_type == switches::kGpuProcess ||
  162. process_type == switches::kUtilityProcess || process_type == "node";
  163. }