Add default implementations for AppWindowRegistry::Observer notifications.
[chromium-blink-merge.git] / chrome / app / chrome_breakpad_client.cc
blob57b604b92cbbe779bb5f8efb96663287226f62e5
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_breakpad_client.h"
7 #include "base/atomicops.h"
8 #include "base/command_line.h"
9 #include "base/environment.h"
10 #include "base/files/file_path.h"
11 #include "base/logging.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/path_service.h"
14 #include "base/strings/safe_sprintf.h"
15 #include "base/strings/string_split.h"
16 #include "base/strings/utf_string_conversions.h"
17 #include "chrome/common/chrome_constants.h"
18 #include "chrome/common/chrome_paths.h"
19 #include "chrome/common/chrome_result_codes.h"
20 #include "chrome/common/chrome_switches.h"
21 #include "chrome/common/crash_keys.h"
22 #include "chrome/common/env_vars.h"
23 #include "chrome/installer/util/google_update_settings.h"
25 #if defined(OS_WIN)
26 #include <windows.h>
28 #include "base/file_version_info.h"
29 #include "base/win/registry.h"
30 #include "chrome/installer/util/google_chrome_sxs_distribution.h"
31 #include "chrome/installer/util/install_util.h"
32 #include "policy/policy_constants.h"
33 #endif
35 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_IOS)
36 #include "chrome/browser/crash_upload_list.h"
37 #include "chrome/common/chrome_version_info_posix.h"
38 #endif
40 #if defined(OS_POSIX)
41 #include "base/debug/dump_without_crashing.h"
42 #endif
44 #if defined(OS_ANDROID)
45 #include "chrome/common/descriptors_android.h"
46 #endif
48 #if defined(OS_CHROMEOS)
49 #include "chrome/common/chrome_version_info.h"
50 #include "chromeos/chromeos_switches.h"
51 #endif
53 namespace chrome {
55 namespace {
57 #if defined(OS_WIN)
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";
62 // The value name prefix will be of the form {chrome-version}-{pid}-{timestamp}
63 // (i.e., "#####.#####.#####.#####-########-########") which easily fits into a
64 // 63 character buffer.
65 const char kBrowserCrashDumpPrefixTemplate[] = "%s-%08x-%08x";
66 const size_t kBrowserCrashDumpPrefixLength = 63;
67 char g_browser_crash_dump_prefix[kBrowserCrashDumpPrefixLength + 1] = {};
69 // These registry key to which we'll write a value for each crash dump attempt.
70 HKEY g_browser_crash_dump_regkey = NULL;
72 // A atomic counter to make each crash dump value name unique.
73 base::subtle::Atomic32 g_browser_crash_dump_count = 0;
74 #endif
76 } // namespace
78 ChromeBreakpadClient::ChromeBreakpadClient() {}
80 ChromeBreakpadClient::~ChromeBreakpadClient() {}
82 void ChromeBreakpadClient::SetClientID(const std::string& client_id) {
83 crash_keys::SetClientID(client_id);
86 #if defined(OS_WIN)
87 bool ChromeBreakpadClient::GetAlternativeCrashDumpLocation(
88 base::FilePath* crash_dir) {
89 // By setting the BREAKPAD_DUMP_LOCATION environment variable, an alternate
90 // location to write breakpad crash dumps can be set.
91 scoped_ptr<base::Environment> env(base::Environment::Create());
92 std::string alternate_crash_dump_location;
93 if (env->GetVar("BREAKPAD_DUMP_LOCATION", &alternate_crash_dump_location)) {
94 *crash_dir = base::FilePath::FromUTF8Unsafe(alternate_crash_dump_location);
95 return true;
98 return false;
101 void ChromeBreakpadClient::GetProductNameAndVersion(
102 const base::FilePath& exe_path,
103 base::string16* product_name,
104 base::string16* version,
105 base::string16* special_build,
106 base::string16* channel_name) {
107 DCHECK(product_name);
108 DCHECK(version);
109 DCHECK(special_build);
110 DCHECK(channel_name);
112 scoped_ptr<FileVersionInfo> version_info(
113 FileVersionInfo::CreateFileVersionInfo(exe_path));
115 if (version_info.get()) {
116 // Get the information from the file.
117 *version = version_info->product_version();
118 if (!version_info->is_official_build())
119 version->append(base::ASCIIToUTF16("-devel"));
121 *product_name = version_info->product_short_name();
122 *special_build = version_info->special_build();
123 } else {
124 // No version info found. Make up the values.
125 *product_name = base::ASCIIToUTF16("Chrome");
126 *version = base::ASCIIToUTF16("0.0.0.0-devel");
129 std::wstring channel_string;
130 GoogleUpdateSettings::GetChromeChannelAndModifiers(
131 !GetIsPerUserInstall(exe_path), &channel_string);
132 *channel_name = base::WideToUTF16(channel_string);
135 bool ChromeBreakpadClient::ShouldShowRestartDialog(base::string16* title,
136 base::string16* message,
137 bool* is_rtl_locale) {
138 scoped_ptr<base::Environment> env(base::Environment::Create());
139 if (!env->HasVar(env_vars::kShowRestart) ||
140 !env->HasVar(env_vars::kRestartInfo) ||
141 env->HasVar(env_vars::kMetroConnected)) {
142 return false;
145 std::string restart_info;
146 env->GetVar(env_vars::kRestartInfo, &restart_info);
148 // The CHROME_RESTART var contains the dialog strings separated by '|'.
149 // See ChromeBrowserMainPartsWin::PrepareRestartOnCrashEnviroment()
150 // for details.
151 std::vector<std::string> dlg_strings;
152 base::SplitString(restart_info, '|', &dlg_strings);
154 if (dlg_strings.size() < 3)
155 return false;
157 *title = base::UTF8ToUTF16(dlg_strings[0]);
158 *message = base::UTF8ToUTF16(dlg_strings[1]);
159 *is_rtl_locale = dlg_strings[2] == env_vars::kRtlLocale;
160 return true;
163 bool ChromeBreakpadClient::AboutToRestart() {
164 scoped_ptr<base::Environment> env(base::Environment::Create());
165 if (!env->HasVar(env_vars::kRestartInfo))
166 return false;
168 env->SetVar(env_vars::kShowRestart, "1");
169 return true;
172 bool ChromeBreakpadClient::GetDeferredUploadsSupported(
173 bool is_per_user_install) {
174 Version update_version = GoogleUpdateSettings::GetGoogleUpdateVersion(
175 !is_per_user_install);
176 if (!update_version.IsValid() ||
177 update_version.IsOlderThan(std::string(kMinUpdateVersion)))
178 return false;
180 return true;
183 bool ChromeBreakpadClient::GetIsPerUserInstall(const base::FilePath& exe_path) {
184 return InstallUtil::IsPerUserInstall(exe_path.value().c_str());
187 bool ChromeBreakpadClient::GetShouldDumpLargerDumps(bool is_per_user_install) {
188 base::string16 channel_name(base::WideToUTF16(
189 GoogleUpdateSettings::GetChromeChannel(!is_per_user_install)));
191 // Capture more detail in crash dumps for beta and dev channel builds.
192 if (channel_name == base::ASCIIToUTF16("dev") ||
193 channel_name == base::ASCIIToUTF16("beta") ||
194 channel_name == GoogleChromeSxSDistribution::ChannelName())
195 return true;
196 return false;
199 int ChromeBreakpadClient::GetResultCodeRespawnFailed() {
200 return chrome::RESULT_CODE_RESPAWN_FAILED;
203 void ChromeBreakpadClient::InitBrowserCrashDumpsRegKey() {
204 DCHECK(g_browser_crash_dump_regkey == NULL);
206 base::win::RegKey regkey;
207 if (regkey.Create(HKEY_CURRENT_USER,
208 chrome::kBrowserCrashDumpAttemptsRegistryPath,
209 KEY_ALL_ACCESS) != ERROR_SUCCESS) {
210 return;
213 // We use the current process id and the current tick count as a (hopefully)
214 // unique combination for the crash dump value. There's a small chance that
215 // across a reboot we might have a crash dump signal written, and the next
216 // browser process might have the same process id and tick count, but crash
217 // before consuming the signal (overwriting the signal with an identical one).
218 // For now, we're willing to live with that risk.
219 int length = base::strings::SafeSPrintf(g_browser_crash_dump_prefix,
220 kBrowserCrashDumpPrefixTemplate,
221 chrome::kChromeVersion,
222 ::GetCurrentProcessId(),
223 ::GetTickCount());
224 if (length <= 0) {
225 NOTREACHED();
226 g_browser_crash_dump_prefix[0] = '\0';
227 return;
230 // Hold the registry key in a global for update on crash dump.
231 g_browser_crash_dump_regkey = regkey.Take();
234 void ChromeBreakpadClient::RecordCrashDumpAttempt(bool is_real_crash) {
235 // If we're not a browser (or the registry is unavailable to us for some
236 // reason) then there's nothing to do.
237 if (g_browser_crash_dump_regkey == NULL)
238 return;
240 // Generate the final value name we'll use (appends the crash number to the
241 // base value name).
242 const size_t kMaxValueSize = 2 * kBrowserCrashDumpPrefixLength;
243 char value_name[kMaxValueSize + 1] = {};
244 int length = base::strings::SafeSPrintf(
245 value_name,
246 "%s-%x",
247 g_browser_crash_dump_prefix,
248 base::subtle::NoBarrier_AtomicIncrement(&g_browser_crash_dump_count, 1));
250 if (length > 0) {
251 DWORD value_dword = is_real_crash ? 1 : 0;
252 ::RegSetValueExA(g_browser_crash_dump_regkey, value_name, 0, REG_DWORD,
253 reinterpret_cast<BYTE*>(&value_dword),
254 sizeof(value_dword));
258 bool ChromeBreakpadClient::ReportingIsEnforcedByPolicy(bool* breakpad_enabled) {
259 // Determine whether configuration management allows loading the crash reporter.
260 // Since the configuration management infrastructure is not initialized at this
261 // point, we read the corresponding registry key directly. The return status
262 // indicates whether policy data was successfully read. If it is true,
263 // |breakpad_enabled| contains the value set by policy.
264 base::string16 key_name =
265 base::UTF8ToUTF16(policy::key::kMetricsReportingEnabled);
266 DWORD value = 0;
267 base::win::RegKey hklm_policy_key(HKEY_LOCAL_MACHINE,
268 policy::kRegistryChromePolicyKey, KEY_READ);
269 if (hklm_policy_key.ReadValueDW(key_name.c_str(), &value) == ERROR_SUCCESS) {
270 *breakpad_enabled = value != 0;
271 return true;
274 base::win::RegKey hkcu_policy_key(HKEY_CURRENT_USER,
275 policy::kRegistryChromePolicyKey, KEY_READ);
276 if (hkcu_policy_key.ReadValueDW(key_name.c_str(), &value) == ERROR_SUCCESS) {
277 *breakpad_enabled = value != 0;
278 return true;
281 return false;
283 #endif
285 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_IOS)
286 void ChromeBreakpadClient::GetProductNameAndVersion(std::string* product_name,
287 std::string* version) {
288 DCHECK(product_name);
289 DCHECK(version);
290 #if defined(OS_ANDROID)
291 *product_name = "Chrome_Android";
292 #elif defined(OS_CHROMEOS)
293 *product_name = "Chrome_ChromeOS";
294 #else // OS_LINUX
295 #if !defined(ADDRESS_SANITIZER)
296 *product_name = "Chrome_Linux";
297 #else
298 *product_name = "Chrome_Linux_ASan";
299 #endif
300 #endif
302 *version = PRODUCT_VERSION;
305 base::FilePath ChromeBreakpadClient::GetReporterLogFilename() {
306 return base::FilePath(CrashUploadList::kReporterLogFilename);
308 #endif
310 bool ChromeBreakpadClient::GetCrashDumpLocation(base::FilePath* crash_dir) {
311 // By setting the BREAKPAD_DUMP_LOCATION environment variable, an alternate
312 // location to write breakpad crash dumps can be set.
313 scoped_ptr<base::Environment> env(base::Environment::Create());
314 std::string alternate_crash_dump_location;
315 if (env->GetVar("BREAKPAD_DUMP_LOCATION", &alternate_crash_dump_location)) {
316 base::FilePath crash_dumps_dir_path =
317 base::FilePath::FromUTF8Unsafe(alternate_crash_dump_location);
318 PathService::Override(chrome::DIR_CRASH_DUMPS, crash_dumps_dir_path);
321 return PathService::Get(chrome::DIR_CRASH_DUMPS, crash_dir);
324 size_t ChromeBreakpadClient::RegisterCrashKeys() {
325 // Note: This is not called on Windows because Breakpad is initialized in the
326 // EXE module, but code that uses crash keys is in the DLL module.
327 // RegisterChromeCrashKeys() will be called after the DLL is loaded.
328 return crash_keys::RegisterChromeCrashKeys();
331 bool ChromeBreakpadClient::IsRunningUnattended() {
332 scoped_ptr<base::Environment> env(base::Environment::Create());
333 return env->HasVar(env_vars::kHeadless);
336 bool ChromeBreakpadClient::GetCollectStatsConsent() {
337 // Convert #define to a variable so that we can use if() rather than
338 // #if below and so at least compile-test the Chrome code in
339 // Chromium builds.
340 #if defined(GOOGLE_CHROME_BUILD)
341 bool is_chrome_build = true;
342 #else
343 bool is_chrome_build = false;
344 #endif
346 #if defined(OS_CHROMEOS)
347 bool is_guest_session = CommandLine::ForCurrentProcess()->HasSwitch(
348 chromeos::switches::kGuestSession);
349 bool is_stable_channel =
350 chrome::VersionInfo::GetChannel() == chrome::VersionInfo::CHANNEL_STABLE;
352 if (is_guest_session && is_stable_channel)
353 return false;
354 #endif // defined(OS_CHROMEOS)
356 #if defined(OS_ANDROID)
357 // TODO(jcivelli): we should not initialize the crash-reporter when it was not
358 // enabled. Right now if it is disabled we still generate the minidumps but we
359 // do not upload them.
360 return is_chrome_build;
361 #else // !defined(OS_ANDROID)
362 return is_chrome_build && GoogleUpdateSettings::GetCollectStatsConsent();
363 #endif // defined(OS_ANDROID)
366 #if defined(OS_ANDROID)
367 int ChromeBreakpadClient::GetAndroidMinidumpDescriptor() {
368 return kAndroidMinidumpDescriptor;
370 #endif
372 bool ChromeBreakpadClient::EnableBreakpadForProcess(
373 const std::string& process_type) {
374 return process_type == switches::kRendererProcess ||
375 process_type == switches::kPluginProcess ||
376 process_type == switches::kPpapiPluginProcess ||
377 process_type == switches::kZygoteProcess ||
378 process_type == switches::kGpuProcess;
381 } // namespace chrome