Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / components / browser_watcher / exit_funnel_win.cc
blob5a817684fc734ad436505612b2ebfa534737bb94
1 // Copyright (c) 2014 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 "components/browser_watcher/exit_funnel_win.h"
7 #include <windows.h>
9 #include "base/strings/stringprintf.h"
10 #include "base/time/time.h"
12 namespace browser_watcher {
14 ExitFunnel::ExitFunnel() {
17 ExitFunnel::~ExitFunnel() {
20 bool ExitFunnel::Init(const base::char16* registry_path,
21 base::ProcessHandle process_handle) {
22 // Concatenate the pid and the creation time of the process to get a
23 // unique key name.
24 base::ProcessId pid = base::GetProcId(process_handle);
26 FILETIME creation_time = {};
27 FILETIME dummy = {};
28 if (!::GetProcessTimes(process_handle, &creation_time,
29 &dummy, &dummy, &dummy)) {
30 LOG(ERROR) << "Invalid process handle, can't get process times.";
31 return false;
34 return InitImpl(registry_path, pid, base::Time::FromFileTime(creation_time));
37 bool ExitFunnel::InitImpl(const base::char16* registry_path,
38 base::ProcessId pid,
39 base::Time creation_time) {
40 base::string16 key_name = registry_path;
41 base::StringAppendF(
42 &key_name, L"\\%d-%lld", pid, creation_time.ToInternalValue());
44 LONG res = key_.Create(HKEY_CURRENT_USER, key_name.c_str(), KEY_SET_VALUE);
45 if (res != ERROR_SUCCESS) {
46 LOG(ERROR) << "Unable to create key " << key_name << " error " << res;
47 return false;
50 return true;
53 bool ExitFunnel::RecordEvent(const base::char16* event_name) {
54 if (!key_.Valid())
55 return false;
57 int64 now = base::Time::Now().ToInternalValue();
59 LONG res = key_.WriteValue(event_name, &now, sizeof(now), REG_QWORD);
60 if (res != ERROR_SUCCESS) {
61 LOG(ERROR) << "Unable to write value " << event_name << " error " << res;
62 return false;
65 return true;
68 bool ExitFunnel::RecordSingleEvent(const base::char16* registry_path,
69 const base::char16* event_name) {
70 ExitFunnel funnel;
72 if (!funnel.Init(registry_path, base::GetCurrentProcessHandle()))
73 return false;
75 return funnel.RecordEvent(event_name);
78 } // namespace browser_watcher