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 <limits> | |
8 | |
9 #include "base/bind.h" | |
10 #include "base/logging.h" | |
11 #include "components/browser_watcher/watcher_client_win.h" | |
12 | |
13 ChromeWatcherClient::ChromeWatcherClient( | |
14 const CommandLineGenerator& command_line_generator) | |
15 : command_line_generator_(command_line_generator) { | |
Sigurður Ásgeirsson
2015/01/29 16:59:56
wonky indent?
erikwright (departed)
2015/01/30 20:39:57
This is what clang format does.
| |
16 } | |
17 | |
18 ChromeWatcherClient::~ChromeWatcherClient() { | |
19 } | |
20 | |
21 bool ChromeWatcherClient::LaunchWatcher() { | |
22 SECURITY_ATTRIBUTES on_initialized_event_attributes = { | |
Sigurður Ásgeirsson
2015/01/29 16:59:56
nit: worth providing running commentary here. Inhe
erikwright (departed)
2015/01/30 20:39:57
Done.
| |
23 sizeof(SECURITY_ATTRIBUTES), // nLength | |
24 nullptr, // lpSecurityDescriptor | |
25 TRUE // bInheritHandle | |
26 }; | |
27 on_initialized_event_.Set( | |
28 ::CreateEvent(&on_initialized_event_attributes, FALSE, FALSE, nullptr)); | |
29 if (!on_initialized_event_.IsValid()) { | |
30 PLOG(ERROR) << "Failed to create an event."; | |
31 return false; | |
32 } | |
33 browser_watcher::WatcherClient watcher_client( | |
34 base::Bind(command_line_generator_, on_initialized_event_.Get())); | |
35 watcher_client.AddInheritedHandle(on_initialized_event_.Get()); | |
36 watcher_client.LaunchWatcher(); | |
37 process_ = watcher_client.process().Duplicate(); | |
38 return process_.IsValid(); | |
39 } | |
40 | |
41 bool ChromeWatcherClient::WaitForInitialization() { | |
Sigurður Ásgeirsson
2015/01/29 16:59:56
nit: this assumes single-threaded use. Maybe menti
erikwright (departed)
2015/01/30 20:39:57
This is the default for Chrome.
| |
42 if (!on_initialized_event_.IsValid() || !process_.IsValid()) { | |
43 LOG(ERROR) | |
44 << "WaitForInitialization called twice, after a failed LaunchWatcher " | |
45 "call, or without calling LaunchWatcher."; | |
46 return false; | |
47 } | |
48 | |
49 HANDLE handles[] = {on_initialized_event_.Get(), process_.Handle()}; | |
50 DWORD result = ::WaitForMultipleObjects(arraysize(handles), handles, | |
51 FALSE, INFINITE); | |
52 | |
53 // We don't need this anymore. | |
54 on_initialized_event_.Close(); | |
55 | |
56 switch (result) { | |
57 case WAIT_OBJECT_0: | |
58 return true; | |
59 case WAIT_OBJECT_0 + 1: | |
60 LOG(ERROR) << "Chrome watcher process failed to launch."; | |
61 return false; | |
62 case WAIT_FAILED: | |
63 PLOG(ERROR) | |
64 << "Failure while waiting on Chrome watcher process launch."; | |
65 return false; | |
66 default: | |
67 NOTREACHED() << "Unexpected result while waiting on Chrome watcher " | |
68 "process launch: " << result; | |
69 return false; | |
70 } | |
71 } | |
72 | |
73 int ChromeWatcherClient::TerminateWatcher(int exit_code) { | |
74 // In case WaitForInitialization was never called. | |
75 on_initialized_event_.Close(); | |
76 | |
77 if (!process_.IsValid()) { | |
78 LOG(ERROR) << "TerminateWatcher called twice, after a failed LaunchWatcher " | |
79 "call, or without calling LaunchWatcher."; | |
80 return std::numeric_limits<int>::max(); | |
81 } | |
82 | |
83 int actual_exit_code = 0; | |
84 bool result = | |
85 process_.WaitForExitWithTimeout(base::TimeDelta(), &actual_exit_code); | |
86 if (result) | |
87 return actual_exit_code; // The watcher already exited. | |
88 | |
89 process_.Terminate(exit_code); | |
Sigurður Ásgeirsson
2015/01/29 16:59:56
interesting detail - I don't think this'll change
erikwright (departed)
2015/01/30 20:39:57
Correct. That's why we return the actual exit code
Sigurður Ásgeirsson
2015/02/02 16:59:20
In that case the wait above is redundant - no?
erikwright (departed)
2015/02/03 20:05:34
Correct.
| |
90 result = process_.WaitForExit(&actual_exit_code); | |
91 DCHECK(result); | |
92 return actual_exit_code; | |
93 } | |
OLD | NEW |