15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/child_process_logging.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <windows.h>
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
94e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "base/debug/crash_logging.h"
105f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "base/memory/scoped_ptr.h"
11868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/utf_string_conversions.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/chrome_constants.h"
133551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#include "chrome/common/crash_keys.h"
140529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include "chrome/installer/util/google_update_settings.h"
155f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "components/metrics/client_info.h"
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace child_process_logging {
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace {
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
213551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// exported in breakpad_win.cc:
223551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)//    void __declspec(dllexport) __cdecl SetCrashKeyValueImpl.
233551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)typedef void (__cdecl *SetCrashKeyValue)(const wchar_t*, const wchar_t*);
243551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
253551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// exported in breakpad_win.cc:
263551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)//    void __declspec(dllexport) __cdecl ClearCrashKeyValueImpl.
273551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)typedef void (__cdecl *ClearCrashKeyValue)(const wchar_t*);
283551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
293551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)void SetCrashKeyValueTrampoline(const base::StringPiece& key,
303551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                                const base::StringPiece& value) {
313551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  static SetCrashKeyValue set_crash_key = NULL;
323551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  if (!set_crash_key) {
333551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    HMODULE exe_module = GetModuleHandle(chrome::kBrowserProcessExecutableName);
343551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    if (!exe_module)
353551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      return;
363551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    set_crash_key = reinterpret_cast<SetCrashKeyValue>(
373551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)        GetProcAddress(exe_module, "SetCrashKeyValueImpl"));
383551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  }
393551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (set_crash_key) {
415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    (set_crash_key)(base::UTF8ToWide(key).data(),
425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                    base::UTF8ToWide(value).data());
435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
443551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)}
453551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
463551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)void ClearCrashKeyValueTrampoline(const base::StringPiece& key) {
473551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  static ClearCrashKeyValue clear_crash_key = NULL;
483551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  if (!clear_crash_key) {
493551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    HMODULE exe_module = GetModuleHandle(chrome::kBrowserProcessExecutableName);
503551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    if (!exe_module)
513551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      return;
523551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    clear_crash_key = reinterpret_cast<ClearCrashKeyValue>(
533551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)        GetProcAddress(exe_module, "ClearCrashKeyValueImpl"));
543551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  }
553551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
563551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  if (clear_crash_key)
575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    (clear_crash_key)(base::UTF8ToWide(key).data());
583551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)}
593551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
603551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)}  // namespace
613551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
623551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)void Init() {
633551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // Note: on other platforms, this is set up during Breakpad initialization,
643551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // in ChromeBreakpadClient. But on Windows, that is before the DLL module is
653551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // loaded, which is a prerequisite of the crash key system.
663551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  crash_keys::RegisterChromeCrashKeys();
673551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  base::debug::SetCrashKeyReportingFunctions(
683551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)      &SetCrashKeyValueTrampoline, &ClearCrashKeyValueTrampoline);
690529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
70116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // This would be handled by BreakpadClient::SetCrashClientIdFromGUID(), but
71116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // because of the aforementioned issue, crash keys aren't ready yet at the
72116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // time of Breakpad initialization, load the client id backed up in Google
73116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Update settings instead.
745f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  scoped_ptr<metrics::ClientInfo> client_info =
755f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      GoogleUpdateSettings::LoadMetricsClientInfo();
765f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  if (client_info)
775f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    crash_keys::SetCrashClientIdFromGUID(client_info->client_id);
783551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)}
793551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace child_process_logging
81