OLD | NEW |
1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/browser_watcher/exit_code_watcher_win.h" | 5 #include "components/browser_watcher/exit_code_watcher_win.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/process/kill.h" | 9 #include "base/process/kill.h" |
10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
11 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
12 #include "base/win/registry.h" | 12 #include "base/win/registry.h" |
13 | 13 |
14 namespace browser_watcher { | 14 namespace browser_watcher { |
15 | 15 |
16 namespace { | 16 namespace { |
17 | 17 |
18 base::string16 GetValueName(const base::Time creation_time, | 18 base::string16 GetValueName(const base::Time creation_time, |
19 base::ProcessId pid) { | 19 base::ProcessId pid) { |
20 // Convert the PID and creation time to a string value unique to this | 20 // Convert the PID and creation time to a string value unique to this |
21 // process instance. | 21 // process instance. |
22 return base::StringPrintf(L"%d-%lld", pid, creation_time.ToInternalValue()); | 22 return base::StringPrintf(L"%d-%lld", pid, creation_time.ToInternalValue()); |
23 } | 23 } |
24 | 24 |
25 } // namespace | 25 } // namespace |
26 | 26 |
27 const char ExitCodeWatcher::kParenthHandleSwitch[] = "parent-handle"; | 27 const char ExitCodeWatcher::kParenthHandleSwitch[] = "parent-handle"; |
28 | 28 |
29 ExitCodeWatcher::ExitCodeWatcher(const base::char16* registry_path) : | 29 ExitCodeWatcher::ExitCodeWatcher(const base::char16* registry_path) : |
30 registry_path_(registry_path) { | 30 registry_path_(registry_path), exit_code_(STILL_ACTIVE) { |
31 } | 31 } |
32 | 32 |
33 ExitCodeWatcher::~ExitCodeWatcher() { | 33 ExitCodeWatcher::~ExitCodeWatcher() { |
34 } | 34 } |
35 | 35 |
36 bool ExitCodeWatcher::ParseArguments(const base::CommandLine& cmd_line) { | 36 bool ExitCodeWatcher::ParseArguments(const base::CommandLine& cmd_line) { |
37 std::string process_handle_str = | 37 std::string process_handle_str = |
38 cmd_line.GetSwitchValueASCII(kParenthHandleSwitch); | 38 cmd_line.GetSwitchValueASCII(kParenthHandleSwitch); |
39 unsigned process_handle_uint = 0; | 39 unsigned process_handle_uint = 0; |
40 if (process_handle_str.empty() || | 40 if (process_handle_str.empty() || |
(...skipping 24 matching lines...) Expand all Loading... |
65 process_ = base::Process(process_handle); | 65 process_ = base::Process(process_handle); |
66 process_creation_time_ = base::Time::FromFileTime(creation_time); | 66 process_creation_time_ = base::Time::FromFileTime(creation_time); |
67 | 67 |
68 // Start by writing the value STILL_ACTIVE to registry, to allow detection | 68 // Start by writing the value STILL_ACTIVE to registry, to allow detection |
69 // of the case where the watcher itself is somehow terminated before it can | 69 // of the case where the watcher itself is somehow terminated before it can |
70 // write the process' actual exit code. | 70 // write the process' actual exit code. |
71 return WriteProcessExitCode(STILL_ACTIVE); | 71 return WriteProcessExitCode(STILL_ACTIVE); |
72 } | 72 } |
73 | 73 |
74 void ExitCodeWatcher::WaitForExit() { | 74 void ExitCodeWatcher::WaitForExit() { |
75 int exit_code = 0; | 75 if (!process_.WaitForExit(&exit_code_)) { |
76 if (!process_.WaitForExit(&exit_code)) { | |
77 LOG(ERROR) << "Failed to wait for process."; | 76 LOG(ERROR) << "Failed to wait for process."; |
78 return; | 77 return; |
79 } | 78 } |
80 | 79 |
81 WriteProcessExitCode(exit_code); | 80 WriteProcessExitCode(exit_code_); |
82 } | 81 } |
83 | 82 |
84 bool ExitCodeWatcher::WriteProcessExitCode(int exit_code) { | 83 bool ExitCodeWatcher::WriteProcessExitCode(int exit_code) { |
85 base::win::RegKey key(HKEY_CURRENT_USER, | 84 base::win::RegKey key(HKEY_CURRENT_USER, |
86 registry_path_.c_str(), | 85 registry_path_.c_str(), |
87 KEY_WRITE); | 86 KEY_WRITE); |
88 base::string16 value_name( | 87 base::string16 value_name( |
89 GetValueName(process_creation_time_, process_.pid())); | 88 GetValueName(process_creation_time_, process_.pid())); |
90 | 89 |
91 ULONG result = key.WriteValue(value_name.c_str(), exit_code); | 90 ULONG result = key.WriteValue(value_name.c_str(), exit_code); |
92 if (result != ERROR_SUCCESS) { | 91 if (result != ERROR_SUCCESS) { |
93 LOG(ERROR) << "Unable to write to registry, error " << result; | 92 LOG(ERROR) << "Unable to write to registry, error " << result; |
94 return false; | 93 return false; |
95 } | 94 } |
96 | 95 |
97 return true; | 96 return true; |
98 } | 97 } |
99 | 98 |
100 } // namespace browser_watcher | 99 } // namespace browser_watcher |
OLD | NEW |