OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 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 "chrome/app/chrome_watcher_client_win.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/logging.h" | |
9 #include "components/browser_watcher/watcher_client_win.h" | |
10 | |
11 ChromeWatcherClient::ChromeWatcherClient( | |
12 const CommandLineGenerator& command_line_generator) | |
13 : command_line_generator_(command_line_generator) { | |
14 } | |
15 | |
16 ChromeWatcherClient::~ChromeWatcherClient() { | |
17 } | |
18 | |
19 bool ChromeWatcherClient::LaunchWatcher() { | |
20 // Create an inheritable event that the child process will signal when it has | |
21 // completed initialization. | |
22 SECURITY_ATTRIBUTES on_initialized_event_attributes = { | |
23 sizeof(SECURITY_ATTRIBUTES), // nLength | |
24 nullptr, // lpSecurityDescriptor | |
25 TRUE // bInheritHandle | |
26 }; | |
27 on_initialized_event_.Set(::CreateEvent(&on_initialized_event_attributes, | |
28 TRUE, // manual reset | |
29 FALSE, nullptr)); | |
30 if (!on_initialized_event_.IsValid()) { | |
31 PLOG(ERROR) << "Failed to create an event."; | |
32 return false; | |
33 } | |
34 | |
35 // Configure the basic WatcherClient, binding in the initialization event | |
36 // HANDLE. | |
37 browser_watcher::WatcherClient watcher_client( | |
38 base::Bind(command_line_generator_, on_initialized_event_.Get())); | |
39 // Indicate that the event HANDLE should be inherited. | |
40 watcher_client.AddInheritedHandle(on_initialized_event_.Get()); | |
41 // Launch the watcher. | |
42 watcher_client.LaunchWatcher(); | |
43 // Grab a handle to the watcher so that we may later wait on its | |
44 // initialization. | |
45 process_ = watcher_client.process().Duplicate(); | |
46 if (!process_.IsValid()) | |
47 on_initialized_event_.Close(); | |
48 return process_.IsValid(); | |
49 } | |
50 | |
51 bool ChromeWatcherClient::EnsureInitialized() { | |
52 if (!process_.IsValid()) { | |
grt (UTC plus 2)
2015/02/04 03:04:48
nit: no braces
erikwright (departed)
2015/02/04 04:19:18
Done.
| |
53 return false; | |
54 } | |
55 | |
56 DCHECK(on_initialized_event_.IsValid()); | |
57 | |
58 HANDLE handles[] = {on_initialized_event_.Get(), process_.Handle()}; | |
59 DWORD result = ::WaitForMultipleObjects(arraysize(handles), handles, | |
60 FALSE, INFINITE); | |
61 | |
62 switch (result) { | |
63 case WAIT_OBJECT_0: | |
64 return true; | |
65 case WAIT_OBJECT_0 + 1: | |
66 LOG(ERROR) << "Chrome watcher process failed to launch."; | |
67 return false; | |
68 case WAIT_FAILED: | |
69 PLOG(ERROR) | |
70 << "Failure while waiting on Chrome watcher process launch."; | |
71 return false; | |
72 default: | |
73 NOTREACHED() << "Unexpected result while waiting on Chrome watcher " | |
74 "process launch: " << result; | |
75 return false; | |
76 } | |
77 } | |
78 | |
79 bool ChromeWatcherClient::WaitForExit(int* exit_code) { | |
80 if (!process_.IsValid()) | |
81 return false; | |
82 | |
83 return process_.WaitForExit(exit_code); | |
grt (UTC plus 2)
2015/02/04 03:04:47
nit (here and below):
return process_.IsValid()
erikwright (departed)
2015/02/04 04:19:18
Done.
| |
84 } | |
85 | |
86 bool ChromeWatcherClient::WaitForExitWithTimeout(const base::TimeDelta& timeout, | |
87 int* exit_code) { | |
88 if (!process_.IsValid()) | |
89 return false; | |
90 | |
91 return process_.WaitForExitWithTimeout(timeout, exit_code); | |
92 } | |
OLD | NEW |