1// Copyright (c) 2012 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
5#include "chrome/common/child_process_logging.h"
6
7#include <windows.h>
8
9#include "base/debug/crash_logging.h"
10#include "base/strings/utf_string_conversions.h"
11#include "chrome/common/chrome_constants.h"
12#include "chrome/common/crash_keys.h"
13#include "chrome/installer/util/google_update_settings.h"
14
15namespace child_process_logging {
16
17namespace {
18
19// exported in breakpad_win.cc:
20//    void __declspec(dllexport) __cdecl SetCrashKeyValueImpl.
21typedef void (__cdecl *SetCrashKeyValue)(const wchar_t*, const wchar_t*);
22
23// exported in breakpad_win.cc:
24//    void __declspec(dllexport) __cdecl ClearCrashKeyValueImpl.
25typedef void (__cdecl *ClearCrashKeyValue)(const wchar_t*);
26
27void SetCrashKeyValueTrampoline(const base::StringPiece& key,
28                                const base::StringPiece& value) {
29  static SetCrashKeyValue set_crash_key = NULL;
30  if (!set_crash_key) {
31    HMODULE exe_module = GetModuleHandle(chrome::kBrowserProcessExecutableName);
32    if (!exe_module)
33      return;
34    set_crash_key = reinterpret_cast<SetCrashKeyValue>(
35        GetProcAddress(exe_module, "SetCrashKeyValueImpl"));
36  }
37
38  if (set_crash_key) {
39    (set_crash_key)(base::UTF8ToWide(key).data(),
40                    base::UTF8ToWide(value).data());
41  }
42}
43
44void ClearCrashKeyValueTrampoline(const base::StringPiece& key) {
45  static ClearCrashKeyValue clear_crash_key = NULL;
46  if (!clear_crash_key) {
47    HMODULE exe_module = GetModuleHandle(chrome::kBrowserProcessExecutableName);
48    if (!exe_module)
49      return;
50    clear_crash_key = reinterpret_cast<ClearCrashKeyValue>(
51        GetProcAddress(exe_module, "ClearCrashKeyValueImpl"));
52  }
53
54  if (clear_crash_key)
55    (clear_crash_key)(base::UTF8ToWide(key).data());
56}
57
58}  // namespace
59
60void Init() {
61  // Note: on other platforms, this is set up during Breakpad initialization,
62  // in ChromeBreakpadClient. But on Windows, that is before the DLL module is
63  // loaded, which is a prerequisite of the crash key system.
64  crash_keys::RegisterChromeCrashKeys();
65  base::debug::SetCrashKeyReportingFunctions(
66      &SetCrashKeyValueTrampoline, &ClearCrashKeyValueTrampoline);
67
68  // This would be handled by BreakpadClient::SetClientID(), but because of the
69  // aforementioned issue, crash keys aren't ready yet at the time of Breakpad
70  // initialization.
71  std::string client_id;
72  if (GoogleUpdateSettings::GetMetricsId(&client_id))
73    base::debug::SetCrashKeyValue(crash_keys::kClientID, client_id);
74}
75
76}  // namespace child_process_logging
77