Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(422)

Side by Side Diff: components/browser_watcher/watcher_win.cc

Issue 729363004: Browser watcher first installment. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@lkgr
Patch Set: Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 "base/command_line.h"
6 #include "base/process/kill.h"
7 #include "base/strings/string_number_conversions.h"
8 #include "base/strings/stringprintf.h"
9 #include "base/win/registry.h"
10 #include "components/browser_watcher/watcher_win.h"
erikwright (departed) 2014/11/17 18:12:30 must be the first file included.
Sigurður Ásgeirsson 2014/11/17 21:27:12 Done.
11
12 namespace browser_watcher {
13
14 namespace {
15
16 base::string16 GetValueName(const base::Time creation_time,
17 base::ProcessId pid) {
18 // Convert the PID and creation time to a string value unique to this
19 // process instance.
20 return base::StringPrintf(L"%d-%lld", pid, creation_time.ToInternalValue());
21 }
22
23 } // namespace
24
25 ExitCodeWatcher::ExitCodeWatcher(const base::char16* registry_path) :
26 registry_path_(registry_path),
27 process_(base::kNullProcessHandle),
erikwright (departed) 2014/11/17 18:12:30 I would assume this is unnecessary for this non-PO
Sigurður Ásgeirsson 2014/11/17 21:27:12 Oops, leftover...
28 process_pid_(0) {
29 }
30
31 ExitCodeWatcher::~ExitCodeWatcher() {
32 }
33
34 bool ExitCodeWatcher::ParseArguments(const base::CommandLine& cmd_line) {
35 std::string process_handle_str =
36 cmd_line.GetSwitchValueASCII("parent-handle");
erikwright (departed) 2014/11/17 18:12:30 this string represents part of the API of this cla
Sigurður Ásgeirsson 2014/11/17 21:27:12 Done.
37 unsigned process_handle_uint = 0;
38 if (process_handle_str.empty() ||
39 !base::StringToUint(process_handle_str, &process_handle_uint)) {
40 LOG(ERROR) << "Missing or invalid parent-handle argument.";
41 return false;
42 }
43
44 HANDLE process_handle = reinterpret_cast<HANDLE>(process_handle_uint);
45 // Initial test of the handle, a zero PID indicates invalid handle, or not
46 // a process handle. In this case, bail immediately and avoid closing the
47 // handle.
48 DWORD process_pid = ::GetProcessId(process_handle);
49 if (process_pid == 0) {
50 LOG(ERROR) << "Invalid parent-handle, can't get parent PID.";
51 return false;
52 }
53
54 FILETIME creation_time = {};
55 FILETIME dummy = {};
56 if (!::GetProcessTimes(process_handle, &creation_time,
57 &dummy, &dummy, &dummy)) {
58 LOG(ERROR) << "Invalid parent handle, can't get parent process times.";
59 return false;
60 }
61
62 // Success, take ownership of the process handle.
63 process_.Set(process_handle);
64 process_pid_ = process_pid;
65 process_creation_time_ = base::Time::FromFileTime(creation_time);
66
67 // Start by writing the value STILL_ACTIVE to registry, to allow detection
68 // of the case where the watcher itself is somehow terminated before it can
69 // write the process' actual exit code.
70 base::win::RegKey key(HKEY_CURRENT_USER,
erikwright (departed) 2014/11/17 18:12:30 I'm in the habit of factoring out the writing of s
Sigurður Ásgeirsson 2014/11/17 21:27:12 IMHO there isn't enough code here to warrant a who
erikwright (departed) 2014/11/17 22:07:04 It's not abstraction (no indirection is introduced
71 registry_path_.c_str(),
72 KEY_WRITE);
73 base::string16 value_name(GetValueName(process_creation_time_, process_pid_));
74
75 ULONG result = key.WriteValue(value_name.c_str(), STILL_ACTIVE);
76 if (result != ERROR_SUCCESS) {
77 LOG(ERROR) << "Unable to write to registry, error " << result;
erikwright (departed) 2014/11/17 18:12:30 #include "base/logging.h"
Sigurður Ásgeirsson 2014/11/17 21:27:12 Done.
78 return false;
79 }
80
81 return true;
82 }
83
84 void ExitCodeWatcher::WaitForExit() {
85 int exit_code = 0;
86 if (!base::WaitForExitCode(process_.Get(), &exit_code)) {
87 LOG(ERROR) << "Failed to wait for process.";
88 return;
89 }
90 // WaitForExitCode closes the handle on success.
91 process_.Take();
92
93 base::win::RegKey key(HKEY_CURRENT_USER,
94 registry_path_.c_str(),
95 KEY_WRITE);
96 base::string16 value_name(GetValueName(process_creation_time_, process_pid_));
97
98 ULONG result = key.WriteValue(value_name.c_str(), exit_code);
99 if (result != ERROR_SUCCESS)
100 LOG(ERROR) << "Unable to write exit code, error: " << result;
101 }
102
103 } // namespace browser_watcher
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698