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

Unified Diff: chrome/app/chrome_watcher_client_win.cc

Issue 886613002: Introduce the ability to wait for the watcher process to initialize. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Review comments. Created 5 years, 11 months 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 side-by-side diff with in-line comments
Download patch
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..0b0bb2d494468e0c52db5849e8702fe70bfac338
--- /dev/null
+++ b/chrome/app/chrome_watcher_client_win.cc
@@ -0,0 +1,92 @@
+// 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()) {
grt (UTC plus 2) 2015/02/04 03:04:48 nit: no braces
erikwright (departed) 2015/02/04 04:19:18 Done.
+ 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())
+ return false;
+
+ 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.
+}
+
+bool ChromeWatcherClient::WaitForExitWithTimeout(const base::TimeDelta& timeout,
+ int* exit_code) {
+ if (!process_.IsValid())
+ return false;
+
+ return process_.WaitForExitWithTimeout(timeout, exit_code);
+}

Powered by Google App Engine
This is Rietveld 408576698