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"
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
24 base::ProcessId pid
= base::GetProcId(process_handle
);
26 FILETIME creation_time
= {};
28 if (!::GetProcessTimes(process_handle
, &creation_time
,
29 &dummy
, &dummy
, &dummy
)) {
30 LOG(ERROR
) << "Invalid process handle, can't get process times.";
34 return InitImpl(registry_path
, pid
, base::Time::FromFileTime(creation_time
));
37 bool ExitFunnel::InitImpl(const base::char16
* registry_path
,
39 base::Time creation_time
) {
40 base::string16 key_name
= registry_path
;
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
;
53 bool ExitFunnel::RecordEvent(const base::char16
* event_name
) {
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
;
68 bool ExitFunnel::RecordSingleEvent(const base::char16
* registry_path
,
69 const base::char16
* event_name
) {
72 if (!funnel
.Init(registry_path
, base::GetCurrentProcessHandle()))
75 return funnel
.RecordEvent(event_name
);
78 } // namespace browser_watcher