| Index: chrome/app/chrome_watcher_client_win.cc
|
| diff --git a/chrome/app/chrome_watcher_client_win.cc b/chrome/app/chrome_watcher_client_win.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..0bfeea9d5a5220231576b3f774e63cc7bea09e2f
|
| --- /dev/null
|
| +++ b/chrome/app/chrome_watcher_client_win.cc
|
| @@ -0,0 +1,100 @@
|
| +// Copyright 2015 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "chrome/app/chrome_watcher_client_win.h"
|
| +
|
| +#include "base/bind.h"
|
| +#include "base/logging.h"
|
| +#include "components/browser_watcher/watcher_client_win.h"
|
| +
|
| +ChromeWatcherClient::ChromeWatcherClient(
|
| + const CommandLineGenerator& command_line_generator)
|
| + : command_line_generator_(command_line_generator) {
|
| +}
|
| +
|
| +ChromeWatcherClient::~ChromeWatcherClient() {
|
| +}
|
| +
|
| +bool ChromeWatcherClient::LaunchWatcher() {
|
| + // Create an inheritable event that the child process will signal when it has
|
| + // completed initialization.
|
| + SECURITY_ATTRIBUTES on_initialized_event_attributes = {
|
| + sizeof(SECURITY_ATTRIBUTES), // nLength
|
| + nullptr, // lpSecurityDescriptor
|
| + TRUE // bInheritHandle
|
| + };
|
| + on_initialized_event_.Set(::CreateEvent(&on_initialized_event_attributes,
|
| + TRUE, // manual reset
|
| + FALSE, nullptr));
|
| + if (!on_initialized_event_.IsValid()) {
|
| + PLOG(ERROR) << "Failed to create an event.";
|
| + return false;
|
| + }
|
| +
|
| + // Configure the basic WatcherClient, binding in the initialization event
|
| + // HANDLE.
|
| + browser_watcher::WatcherClient watcher_client(
|
| + base::Bind(command_line_generator_, on_initialized_event_.Get()));
|
| + // Indicate that the event HANDLE should be inherited.
|
| + watcher_client.AddInheritedHandle(on_initialized_event_.Get());
|
| + // Launch the watcher.
|
| + watcher_client.LaunchWatcher();
|
| + // Grab a handle to the watcher so that we may later wait on its
|
| + // initialization.
|
| + process_ = watcher_client.process().Duplicate();
|
| + if (!process_.IsValid())
|
| + on_initialized_event_.Close();
|
| + return process_.IsValid();
|
| +}
|
| +
|
| +bool ChromeWatcherClient::EnsureInitialized() {
|
| + if (!process_.IsValid()) {
|
| + LOG(ERROR) << "EnsureInitialized called after a failed LaunchWatcher "
|
| + "call, or without calling LaunchWatcher.";
|
| + return false;
|
| + }
|
| +
|
| + DCHECK(on_initialized_event_.IsValid());
|
| +
|
| + HANDLE handles[] = {on_initialized_event_.Get(), process_.Handle()};
|
| + DWORD result = ::WaitForMultipleObjects(arraysize(handles), handles,
|
| + FALSE, INFINITE);
|
| +
|
| + switch (result) {
|
| + case WAIT_OBJECT_0:
|
| + return true;
|
| + case WAIT_OBJECT_0 + 1:
|
| + LOG(ERROR) << "Chrome watcher process failed to launch.";
|
| + return false;
|
| + case WAIT_FAILED:
|
| + PLOG(ERROR)
|
| + << "Failure while waiting on Chrome watcher process launch.";
|
| + return false;
|
| + default:
|
| + NOTREACHED() << "Unexpected result while waiting on Chrome watcher "
|
| + "process launch: " << result;
|
| + return false;
|
| + }
|
| +}
|
| +
|
| +bool ChromeWatcherClient::WaitForExit(int* exit_code) {
|
| + if (!process_.IsValid()) {
|
| + LOG(ERROR) << "WaitForExit called after a failed LaunchWatcher call, or "
|
| + "without calling LaunchWatcher.";
|
| + return false;
|
| + }
|
| +
|
| + return process_.WaitForExit(exit_code);
|
| +}
|
| +
|
| +bool ChromeWatcherClient::WaitForExitWithTimeout(const base::TimeDelta& timeout,
|
| + int* exit_code) {
|
| + if (!process_.IsValid()) {
|
| + LOG(ERROR) << "WaitForExitWithTimeout called after a failed LaunchWatcher "
|
| + "call, or without calling LaunchWatcher.";
|
| + return false;
|
| + }
|
| +
|
| + return process_.WaitForExitWithTimeout(timeout, exit_code);
|
| +}
|
|
|