OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "components/browser_watcher/exit_funnel_win.h" |
| 6 |
| 7 #include <windows.h> |
| 8 |
| 9 #include "base/strings/stringprintf.h" |
| 10 #include "base/time/time.h" |
| 11 |
| 12 namespace browser_watcher { |
| 13 |
| 14 ExitFunnel::ExitFunnel() { |
| 15 } |
| 16 |
| 17 ExitFunnel::~ExitFunnel() { |
| 18 } |
| 19 |
| 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); |
| 25 |
| 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; |
| 32 } |
| 33 |
| 34 base::string16 key_name = registry_path; |
| 35 base::StringAppendF(&key_name, L"\\%d-%lld", pid, |
| 36 base::Time::FromFileTime(creation_time).ToInternalValue()); |
| 37 |
| 38 LONG res = key_.Create(HKEY_CURRENT_USER, key_name.c_str(), KEY_SET_VALUE); |
| 39 if (res != ERROR_SUCCESS) { |
| 40 LOG(ERROR) << "Unable to create key " << key_name << " error " << res; |
| 41 return false; |
| 42 } |
| 43 |
| 44 return true; |
| 45 } |
| 46 |
| 47 bool ExitFunnel::RecordEvent(const base::char16* event_name) { |
| 48 if (!key_.Valid()) |
| 49 return false; |
| 50 |
| 51 int64 now = base::Time::Now().ToInternalValue(); |
| 52 |
| 53 LONG res = key_.WriteValue(event_name, &now, sizeof(now), REG_QWORD); |
| 54 if (res != ERROR_SUCCESS) { |
| 55 LOG(ERROR) << "Unable to write value " << event_name << " error " << res; |
| 56 return false; |
| 57 } |
| 58 |
| 59 return true; |
| 60 } |
| 61 |
| 62 bool ExitFunnel::RecordSingleEvent(const base::char16* registry_path, |
| 63 const base::char16* event_name) { |
| 64 ExitFunnel funnel; |
| 65 |
| 66 if (!funnel.Init(registry_path, base::GetCurrentProcessHandle())) |
| 67 return false; |
| 68 |
| 69 return funnel.RecordEvent(event_name); |
| 70 } |
| 71 |
| 72 } // namespace browser_watcher |
OLD | NEW |