electron_crash_reporter_client.cc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. void ElectronCrashReporterClient::GetProductNameAndVersion(
  80. const char** product_name,
  81. const char** version) {
  82. DCHECK(product_name);
  83. DCHECK(version);
  84. *product_name = ELECTRON_PRODUCT_NAME;
  85. *version = ELECTRON_VERSION_STRING;
  86. }
  87. void ElectronCrashReporterClient::GetProductNameAndVersion(
  88. std::string* product_name,
  89. std::string* version,
  90. std::string* channel) {
  91. const char* c_product_name;
  92. const char* c_version;
  93. GetProductNameAndVersion(&c_product_name, &c_version);
  94. *product_name = c_product_name;
  95. *version = c_version;
  96. *channel = "";
  97. }
  98. base::FilePath ElectronCrashReporterClient::GetReporterLogFilename() {
  99. return base::FilePath(CrashUploadList::kReporterLogFilename);
  100. }
  101. #endif
  102. #if BUILDFLAG(IS_WIN)
  103. void ElectronCrashReporterClient::GetProductNameAndVersion(
  104. const std::wstring& exe_path,
  105. std::wstring* product_name,
  106. std::wstring* version,
  107. std::wstring* special_build,
  108. std::wstring* channel_name) {
  109. *product_name = base::UTF8ToWide(ELECTRON_PRODUCT_NAME);
  110. *version = base::UTF8ToWide(ELECTRON_VERSION_STRING);
  111. }
  112. #endif
  113. #if BUILDFLAG(IS_WIN)
  114. bool ElectronCrashReporterClient::GetCrashDumpLocation(
  115. std::wstring* crash_dir_str) {
  116. base::FilePath crash_dir;
  117. if (!base::PathService::Get(electron::DIR_CRASH_DUMPS, &crash_dir))
  118. return false;
  119. *crash_dir_str = crash_dir.value();
  120. return true;
  121. }
  122. #else
  123. bool ElectronCrashReporterClient::GetCrashDumpLocation(
  124. base::FilePath* crash_dir) {
  125. bool result = base::PathService::Get(electron::DIR_CRASH_DUMPS, crash_dir);
  126. {
  127. // If the DIR_CRASH_DUMPS path is overridden with
  128. // app.setPath('crashDumps', ...) then the directory might not have been
  129. // created.
  130. electron::ScopedAllowBlockingForElectron allow_blocking;
  131. if (result && !base::PathExists(*crash_dir)) {
  132. return base::CreateDirectory(*crash_dir);
  133. }
  134. }
  135. return result;
  136. }
  137. #endif
  138. bool ElectronCrashReporterClient::IsRunningUnattended() {
  139. return !collect_stats_consent_;
  140. }
  141. bool ElectronCrashReporterClient::GetCollectStatsConsent() {
  142. return collect_stats_consent_;
  143. }
  144. #if BUILDFLAG(IS_MAC)
  145. bool ElectronCrashReporterClient::ReportingIsEnforcedByPolicy(
  146. bool* breakpad_enabled) {
  147. return false;
  148. }
  149. #endif
  150. bool ElectronCrashReporterClient::GetShouldRateLimit() {
  151. return rate_limit_;
  152. }
  153. bool ElectronCrashReporterClient::GetShouldCompressUploads() {
  154. return compress_uploads_;
  155. }
  156. void ElectronCrashReporterClient::GetProcessSimpleAnnotations(
  157. std::map<std::string, std::string>* annotations) {
  158. for (auto&& pair : global_annotations_) {
  159. (*annotations)[pair.first] = pair.second;
  160. }
  161. (*annotations)["prod"] = ELECTRON_PRODUCT_NAME;
  162. (*annotations)["ver"] = ELECTRON_VERSION_STRING;
  163. }
  164. #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_MAC)
  165. bool ElectronCrashReporterClient::ShouldMonitorCrashHandlerExpensively() {
  166. return false;
  167. }
  168. #endif
  169. std::string ElectronCrashReporterClient::GetUploadUrl() {
  170. return upload_url_;
  171. }
  172. bool ElectronCrashReporterClient::EnableBreakpadForProcess(
  173. const std::string& process_type) {
  174. return process_type == switches::kRendererProcess ||
  175. process_type == switches::kZygoteProcess ||
  176. process_type == switches::kGpuProcess ||
  177. process_type == switches::kUtilityProcess || process_type == "node";
  178. }