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

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: Add some 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..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.";
grt (UTC plus 2) 2015/02/03 21:20:44 should any of these be debug-only? are they needed
erikwright (departed) 2015/02/03 21:56:47 Good question. I don't know. I googled for a polic
grt (UTC plus 2) 2015/02/04 03:04:47 Every LOG adds weight. If it will never be reached
erikwright (departed) 2015/02/04 04:19:18 Thanks for filling me in on the philosophy. It see
+ 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 "
grt (UTC plus 2) 2015/02/03 21:20:44 is this documenting the API contract? why not just
erikwright (departed) 2015/02/03 21:56:47 In fact, it seems reasonable to call this even if
+ "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 "
grt (UTC plus 2) 2015/02/03 21:20:44 same comment as above
erikwright (departed) 2015/02/03 21:56:47 Done.
+ "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 "
grt (UTC plus 2) 2015/02/03 21:20:44 and here
erikwright (departed) 2015/02/03 21:56:47 Done.
+ "call, or without calling LaunchWatcher.";
+ return false;
+ }
+
+ return process_.WaitForExitWithTimeout(timeout, exit_code);
+}

Powered by Google App Engine
This is Rietveld 408576698