Chromium Code Reviews| 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/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/process/kill.h" | 8 #include "base/process/kill.h" |
| 9 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
| 10 #include "base/win/registry.h" | 10 #include "base/win/registry.h" |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 23 } // namespace | 23 } // namespace |
| 24 | 24 |
| 25 ExitCodeWatcher::ExitCodeWatcher(const base::char16* registry_path) : | 25 ExitCodeWatcher::ExitCodeWatcher(const base::char16* registry_path) : |
| 26 registry_path_(registry_path), exit_code_(STILL_ACTIVE) { | 26 registry_path_(registry_path), exit_code_(STILL_ACTIVE) { |
| 27 } | 27 } |
| 28 | 28 |
| 29 ExitCodeWatcher::~ExitCodeWatcher() { | 29 ExitCodeWatcher::~ExitCodeWatcher() { |
| 30 } | 30 } |
| 31 | 31 |
| 32 bool ExitCodeWatcher::Initialize(base::Process process) { | 32 bool ExitCodeWatcher::Initialize(base::Process process) { |
| 33 DWORD process_pid = process.pid(); | 33 if (!process.IsValid()) { |
| 34 LOG(ERROR) << "Invalid parent handle, can't get parent process ID."; | |
| 35 return false; | |
| 36 } | |
| 37 | |
| 38 DWORD process_pid = process.Pid(); | |
| 34 if (process_pid == 0) { | 39 if (process_pid == 0) { |
|
Lei Zhang
2015/01/23 23:53:44
Is this possible when |process| is valid?
rvargas (doing something else)
2015/01/23 23:55:55
I thought it was not, but there are two unit tests
| |
| 35 LOG(ERROR) << "Invalid parent handle, can't get parent process ID."; | 40 LOG(ERROR) << "Invalid parent handle, can't get parent process ID."; |
| 36 return false; | 41 return false; |
| 37 } | 42 } |
| 38 | 43 |
| 39 FILETIME creation_time = {}; | 44 FILETIME creation_time = {}; |
| 40 FILETIME dummy = {}; | 45 FILETIME dummy = {}; |
| 41 if (!::GetProcessTimes(process.Handle(), &creation_time, &dummy, &dummy, | 46 if (!::GetProcessTimes(process.Handle(), &creation_time, &dummy, &dummy, |
| 42 &dummy)) { | 47 &dummy)) { |
| 43 PLOG(ERROR) << "Invalid parent handle, can't get parent process times."; | 48 PLOG(ERROR) << "Invalid parent handle, can't get parent process times."; |
| 44 return false; | 49 return false; |
| 45 } | 50 } |
| 46 | 51 |
| 47 // Success, take ownership of the process handle. | 52 // Success, take ownership of the process. |
| 48 process_ = process.Pass(); | 53 process_ = process.Pass(); |
| 49 process_creation_time_ = base::Time::FromFileTime(creation_time); | 54 process_creation_time_ = base::Time::FromFileTime(creation_time); |
| 50 | 55 |
| 51 // Start by writing the value STILL_ACTIVE to registry, to allow detection | 56 // Start by writing the value STILL_ACTIVE to registry, to allow detection |
| 52 // of the case where the watcher itself is somehow terminated before it can | 57 // of the case where the watcher itself is somehow terminated before it can |
| 53 // write the process' actual exit code. | 58 // write the process' actual exit code. |
| 54 return WriteProcessExitCode(STILL_ACTIVE); | 59 return WriteProcessExitCode(STILL_ACTIVE); |
| 55 } | 60 } |
| 56 | 61 |
| 57 void ExitCodeWatcher::WaitForExit() { | 62 void ExitCodeWatcher::WaitForExit() { |
| 58 if (!process_.WaitForExit(&exit_code_)) { | 63 if (!process_.WaitForExit(&exit_code_)) { |
| 59 LOG(ERROR) << "Failed to wait for process."; | 64 LOG(ERROR) << "Failed to wait for process."; |
| 60 return; | 65 return; |
| 61 } | 66 } |
| 62 | 67 |
| 63 WriteProcessExitCode(exit_code_); | 68 WriteProcessExitCode(exit_code_); |
| 64 } | 69 } |
| 65 | 70 |
| 66 bool ExitCodeWatcher::WriteProcessExitCode(int exit_code) { | 71 bool ExitCodeWatcher::WriteProcessExitCode(int exit_code) { |
| 67 base::win::RegKey key(HKEY_CURRENT_USER, | 72 base::win::RegKey key(HKEY_CURRENT_USER, |
| 68 registry_path_.c_str(), | 73 registry_path_.c_str(), |
| 69 KEY_WRITE); | 74 KEY_WRITE); |
| 70 base::string16 value_name( | 75 base::string16 value_name( |
| 71 GetValueName(process_creation_time_, process_.pid())); | 76 GetValueName(process_creation_time_, process_.Pid())); |
| 72 | 77 |
| 73 ULONG result = key.WriteValue(value_name.c_str(), exit_code); | 78 ULONG result = key.WriteValue(value_name.c_str(), exit_code); |
| 74 if (result != ERROR_SUCCESS) { | 79 if (result != ERROR_SUCCESS) { |
| 75 LOG(ERROR) << "Unable to write to registry, error " << result; | 80 LOG(ERROR) << "Unable to write to registry, error " << result; |
| 76 return false; | 81 return false; |
| 77 } | 82 } |
| 78 | 83 |
| 79 return true; | 84 return true; |
| 80 } | 85 } |
| 81 | 86 |
| 82 } // namespace browser_watcher | 87 } // namespace browser_watcher |
| OLD | NEW |