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()) { |
| 53 LOG(ERROR) << "EnsureInitialized called after a failed LaunchWatcher " |
| 54 "call, or without calling LaunchWatcher."; |
| 55 return false; |
| 56 } |
| 57 |
| 58 DCHECK(on_initialized_event_.IsValid()); |
| 59 |
| 60 HANDLE handles[] = {on_initialized_event_.Get(), process_.Handle()}; |
| 61 DWORD result = ::WaitForMultipleObjects(arraysize(handles), handles, |
| 62 FALSE, INFINITE); |
| 63 |
| 64 switch (result) { |
| 65 case WAIT_OBJECT_0: |
| 66 return true; |
| 67 case WAIT_OBJECT_0 + 1: |
| 68 LOG(ERROR) << "Chrome watcher process failed to launch."; |
| 69 return false; |
| 70 case WAIT_FAILED: |
| 71 PLOG(ERROR) |
| 72 << "Failure while waiting on Chrome watcher process launch."; |
| 73 return false; |
| 74 default: |
| 75 NOTREACHED() << "Unexpected result while waiting on Chrome watcher " |
| 76 "process launch: " << result; |
| 77 return false; |
| 78 } |
| 79 } |
| 80 |
| 81 bool ChromeWatcherClient::WaitForExit(int* exit_code) { |
| 82 if (!process_.IsValid()) { |
| 83 LOG(ERROR) << "WaitForExit called after a failed LaunchWatcher call, or " |
| 84 "without calling LaunchWatcher."; |
| 85 return false; |
| 86 } |
| 87 |
| 88 return process_.WaitForExit(exit_code); |
| 89 } |
| 90 |
| 91 bool ChromeWatcherClient::WaitForExitWithTimeout(const base::TimeDelta& timeout, |
| 92 int* exit_code) { |
| 93 if (!process_.IsValid()) { |
| 94 LOG(ERROR) << "WaitForExitWithTimeout called after a failed LaunchWatcher " |
| 95 "call, or without calling LaunchWatcher."; |
| 96 return false; |
| 97 } |
| 98 |
| 99 return process_.WaitForExitWithTimeout(timeout, exit_code); |
| 100 } |
OLD | NEW |