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.
5 #include "chrome/app/chrome_crash_reporter_client.h"
7 #include "base/command_line.h"
8 #include "base/environment.h"
9 #include "base/files/file_path.h"
10 #include "base/logging.h"
11 #include "base/path_service.h"
12 #include "base/strings/string_split.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "chrome/common/chrome_constants.h"
15 #include "chrome/common/chrome_paths.h"
16 #include "chrome/common/chrome_result_codes.h"
17 #include "chrome/common/chrome_switches.h"
18 #include "chrome/common/crash_keys.h"
19 #include "chrome/common/env_vars.h"
20 #include "chrome/installer/util/google_update_settings.h"
25 #include "base/file_version_info.h"
26 #include "base/win/registry.h"
27 #include "chrome/installer/util/google_chrome_sxs_distribution.h"
28 #include "chrome/installer/util/install_util.h"
29 #include "chrome/installer/util/util_constants.h"
30 #include "components/browser_watcher/crash_reporting_metrics_win.h"
31 #include "policy/policy_constants.h"
34 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_IOS)
35 #include "components/upload_list/crash_upload_list.h"
36 #include "components/version_info/version_info_values.h"
40 #include "base/debug/dump_without_crashing.h"
43 #if defined(OS_ANDROID)
44 #include "chrome/common/descriptors_android.h"
47 #if defined(OS_CHROMEOS)
48 #include "chrome/common/channel_info.h"
49 #include "chromeos/chromeos_switches.h"
50 #include "components/version_info/version_info.h"
58 // This is the minimum version of google update that is required for deferred
59 // crash uploads to work.
60 const char kMinUpdateVersion
[] = "1.3.21.115";
65 ChromeCrashReporterClient::ChromeCrashReporterClient() {}
67 ChromeCrashReporterClient::~ChromeCrashReporterClient() {}
69 #if !defined(OS_MACOSX)
70 void ChromeCrashReporterClient::SetCrashReporterClientIdFromGUID(
71 const std::string
& client_guid
) {
72 crash_keys::SetMetricsClientIdFromGUID(client_guid
);
77 bool ChromeCrashReporterClient::GetAlternativeCrashDumpLocation(
78 base::FilePath
* crash_dir
) {
79 // By setting the BREAKPAD_DUMP_LOCATION environment variable, an alternate
80 // location to write breakpad crash dumps can be set.
81 scoped_ptr
<base::Environment
> env(base::Environment::Create());
82 std::string alternate_crash_dump_location
;
83 if (env
->GetVar("BREAKPAD_DUMP_LOCATION", &alternate_crash_dump_location
)) {
84 *crash_dir
= base::FilePath::FromUTF8Unsafe(alternate_crash_dump_location
);
91 void ChromeCrashReporterClient::GetProductNameAndVersion(
92 const base::FilePath
& exe_path
,
93 base::string16
* product_name
,
94 base::string16
* version
,
95 base::string16
* special_build
,
96 base::string16
* channel_name
) {
99 DCHECK(special_build
);
100 DCHECK(channel_name
);
102 scoped_ptr
<FileVersionInfo
> version_info(
103 FileVersionInfo::CreateFileVersionInfo(exe_path
));
105 if (version_info
.get()) {
106 // Get the information from the file.
107 *version
= version_info
->product_version();
108 if (!version_info
->is_official_build())
109 version
->append(base::ASCIIToUTF16("-devel"));
111 *product_name
= version_info
->product_short_name();
112 *special_build
= version_info
->special_build();
114 // No version info found. Make up the values.
115 *product_name
= base::ASCIIToUTF16("Chrome");
116 *version
= base::ASCIIToUTF16("0.0.0.0-devel");
119 GoogleUpdateSettings::GetChromeChannelAndModifiers(
120 !GetIsPerUserInstall(exe_path
), channel_name
);
123 bool ChromeCrashReporterClient::ShouldShowRestartDialog(base::string16
* title
,
124 base::string16
* message
,
125 bool* is_rtl_locale
) {
126 scoped_ptr
<base::Environment
> env(base::Environment::Create());
127 if (!env
->HasVar(env_vars::kShowRestart
) ||
128 !env
->HasVar(env_vars::kRestartInfo
) ||
129 env
->HasVar(env_vars::kMetroConnected
)) {
133 std::string restart_info
;
134 env
->GetVar(env_vars::kRestartInfo
, &restart_info
);
136 // The CHROME_RESTART var contains the dialog strings separated by '|'.
137 // See ChromeBrowserMainPartsWin::PrepareRestartOnCrashEnviroment()
139 std::vector
<std::string
> dlg_strings
= base::SplitString(
140 restart_info
, "|", base::TRIM_WHITESPACE
, base::SPLIT_WANT_ALL
);
142 if (dlg_strings
.size() < 3)
145 *title
= base::UTF8ToUTF16(dlg_strings
[0]);
146 *message
= base::UTF8ToUTF16(dlg_strings
[1]);
147 *is_rtl_locale
= dlg_strings
[2] == env_vars::kRtlLocale
;
151 bool ChromeCrashReporterClient::AboutToRestart() {
152 scoped_ptr
<base::Environment
> env(base::Environment::Create());
153 if (!env
->HasVar(env_vars::kRestartInfo
))
156 env
->SetVar(env_vars::kShowRestart
, "1");
160 bool ChromeCrashReporterClient::GetDeferredUploadsSupported(
161 bool is_per_user_install
) {
162 Version update_version
= GoogleUpdateSettings::GetGoogleUpdateVersion(
163 !is_per_user_install
);
164 if (!update_version
.IsValid() ||
165 update_version
.IsOlderThan(std::string(kMinUpdateVersion
)))
171 bool ChromeCrashReporterClient::GetIsPerUserInstall(
172 const base::FilePath
& exe_path
) {
173 return InstallUtil::IsPerUserInstall(exe_path
);
176 bool ChromeCrashReporterClient::GetShouldDumpLargerDumps(
177 bool is_per_user_install
) {
178 base::string16 channel_name
=
179 GoogleUpdateSettings::GetChromeChannel(!is_per_user_install
);
181 // Capture more detail in crash dumps for beta and dev channel builds.
182 return (channel_name
== installer::kChromeChannelDev
||
183 channel_name
== installer::kChromeChannelBeta
||
184 channel_name
== GoogleChromeSxSDistribution::ChannelName());
187 int ChromeCrashReporterClient::GetResultCodeRespawnFailed() {
188 return chrome::RESULT_CODE_RESPAWN_FAILED
;
191 void ChromeCrashReporterClient::InitBrowserCrashDumpsRegKey() {
192 #if !defined(NACL_WIN64)
193 if (GetCollectStatsConsent()){
194 crash_reporting_metrics_
.reset(new browser_watcher::CrashReportingMetrics(
195 InstallUtil::IsChromeSxSProcess()
196 ? chrome::kBrowserCrashDumpAttemptsRegistryPathSxS
197 : chrome::kBrowserCrashDumpAttemptsRegistryPath
));
202 void ChromeCrashReporterClient::RecordCrashDumpAttempt(bool is_real_crash
) {
203 #if !defined(NACL_WIN64)
204 if (!crash_reporting_metrics_
)
208 crash_reporting_metrics_
->RecordCrashDumpAttempt();
210 crash_reporting_metrics_
->RecordDumpWithoutCrashAttempt();
214 void ChromeCrashReporterClient::RecordCrashDumpAttemptResult(bool is_real_crash
,
216 #if !defined(NACL_WIN64)
217 if (!crash_reporting_metrics_
)
221 crash_reporting_metrics_
->RecordCrashDumpAttemptResult(succeeded
);
223 crash_reporting_metrics_
->RecordDumpWithoutCrashAttemptResult(succeeded
);
227 bool ChromeCrashReporterClient::ReportingIsEnforcedByPolicy(
228 bool* breakpad_enabled
) {
229 // Determine whether configuration management allows loading the crash reporter.
230 // Since the configuration management infrastructure is not initialized at this
231 // point, we read the corresponding registry key directly. The return status
232 // indicates whether policy data was successfully read. If it is true,
233 // |breakpad_enabled| contains the value set by policy.
234 base::string16 key_name
=
235 base::UTF8ToUTF16(policy::key::kMetricsReportingEnabled
);
237 base::win::RegKey
hklm_policy_key(HKEY_LOCAL_MACHINE
,
238 policy::kRegistryChromePolicyKey
, KEY_READ
);
239 if (hklm_policy_key
.ReadValueDW(key_name
.c_str(), &value
) == ERROR_SUCCESS
) {
240 *breakpad_enabled
= value
!= 0;
244 base::win::RegKey
hkcu_policy_key(HKEY_CURRENT_USER
,
245 policy::kRegistryChromePolicyKey
, KEY_READ
);
246 if (hkcu_policy_key
.ReadValueDW(key_name
.c_str(), &value
) == ERROR_SUCCESS
) {
247 *breakpad_enabled
= value
!= 0;
253 #endif // defined(OS_WIN)
255 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_IOS)
256 void ChromeCrashReporterClient::GetProductNameAndVersion(
257 const char** product_name
,
258 const char** version
) {
259 DCHECK(product_name
);
261 #if defined(OS_ANDROID)
262 *product_name
= "Chrome_Android";
263 #elif defined(OS_CHROMEOS)
264 *product_name
= "Chrome_ChromeOS";
266 #if !defined(ADDRESS_SANITIZER)
267 *product_name
= "Chrome_Linux";
269 *product_name
= "Chrome_Linux_ASan";
273 *version
= PRODUCT_VERSION
;
276 base::FilePath
ChromeCrashReporterClient::GetReporterLogFilename() {
277 return base::FilePath(CrashUploadList::kReporterLogFilename
);
281 bool ChromeCrashReporterClient::GetCrashDumpLocation(
282 base::FilePath
* crash_dir
) {
283 // By setting the BREAKPAD_DUMP_LOCATION environment variable, an alternate
284 // location to write breakpad crash dumps can be set.
285 scoped_ptr
<base::Environment
> env(base::Environment::Create());
286 std::string alternate_crash_dump_location
;
287 if (env
->GetVar("BREAKPAD_DUMP_LOCATION", &alternate_crash_dump_location
)) {
288 base::FilePath crash_dumps_dir_path
=
289 base::FilePath::FromUTF8Unsafe(alternate_crash_dump_location
);
290 PathService::Override(chrome::DIR_CRASH_DUMPS
, crash_dumps_dir_path
);
293 return PathService::Get(chrome::DIR_CRASH_DUMPS
, crash_dir
);
296 size_t ChromeCrashReporterClient::RegisterCrashKeys() {
297 // Note: On Windows this only affects the EXE. A separate invocation from
298 // child_process_logging_win.cc registers crash keys for Chrome.dll.
299 #if defined(OS_WIN) && defined(COMPONENT_BUILD)
300 // On Windows, this is not called in a component build, as in that case a
301 // single copy of 'base' is shared by the EXE and the various DLLs, and that
302 // copy is configured by child_process_logging_win.cc.
306 return crash_keys::RegisterChromeCrashKeys();
310 bool ChromeCrashReporterClient::IsRunningUnattended() {
311 scoped_ptr
<base::Environment
> env(base::Environment::Create());
312 return env
->HasVar(env_vars::kHeadless
);
315 bool ChromeCrashReporterClient::GetCollectStatsConsent() {
316 #if defined(GOOGLE_CHROME_BUILD)
317 bool is_official_chrome_build
= true;
319 bool is_official_chrome_build
= false;
322 #if defined(OS_CHROMEOS)
323 bool is_guest_session
= base::CommandLine::ForCurrentProcess()->HasSwitch(
324 chromeos::switches::kGuestSession
);
325 bool is_stable_channel
=
326 chrome::GetChannel() == version_info::Channel::STABLE
;
328 if (is_guest_session
&& is_stable_channel
)
330 #endif // defined(OS_CHROMEOS)
332 #if defined(OS_ANDROID)
333 // TODO(jcivelli): we should not initialize the crash-reporter when it was not
334 // enabled. Right now if it is disabled we still generate the minidumps but we
335 // do not upload them.
336 return is_official_chrome_build
;
337 #else // !defined(OS_ANDROID)
338 return is_official_chrome_build
&&
339 GoogleUpdateSettings::GetCollectStatsConsent();
340 #endif // defined(OS_ANDROID)
343 #if defined(OS_ANDROID)
344 int ChromeCrashReporterClient::GetAndroidMinidumpDescriptor() {
345 return kAndroidMinidumpDescriptor
;
349 bool ChromeCrashReporterClient::EnableBreakpadForProcess(
350 const std::string
& process_type
) {
351 return process_type
== switches::kRendererProcess
||
352 process_type
== switches::kPluginProcess
||
353 process_type
== switches::kPpapiPluginProcess
||
354 process_type
== switches::kZygoteProcess
||
355 process_type
== switches::kGpuProcess
;
358 } // namespace chrome