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

Unified Diff: chrome/chrome_watcher/chrome_watcher_main.cc

Issue 811603003: Add a console control handler chrome_watcher.dll. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@base_process
Patch Set: Only wait for logoff on unexpected exit codes. Created 6 years 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
« no previous file with comments | « no previous file | components/browser_watcher/exit_code_watcher_win.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/chrome_watcher/chrome_watcher_main.cc
diff --git a/chrome/chrome_watcher/chrome_watcher_main.cc b/chrome/chrome_watcher/chrome_watcher_main.cc
index 6d35d0c9f4463f54b90aa678119dcb81eb72c299..fb1b5f4fbb7a0c0a1d73ca2a6faf43574089b131 100644
--- a/chrome/chrome_watcher/chrome_watcher_main.cc
+++ b/chrome/chrome_watcher/chrome_watcher_main.cc
@@ -8,6 +8,7 @@
#include "base/command_line.h"
#include "base/logging_win.h"
#include "base/process/process.h"
+#include "base/synchronization/waitable_event.h"
#include "base/template_util.h"
#include "components/browser_watcher/exit_code_watcher_win.h"
#include "components/browser_watcher/exit_funnel_win.h"
@@ -21,6 +22,31 @@ const GUID kChromeWatcherTraceProviderName = {
0x7fe69228, 0x633e, 0x4f06,
{ 0x80, 0xc1, 0x52, 0x7f, 0xea, 0x23, 0xe3, 0xa7 } };
+// The data shared from the main function to the console handler.
+struct HandlerData {
+ HandlerData() : handler_done(true, false) {}
grt (UTC plus 2) 2014/12/19 01:57:34 nit: HandlerData() : handler_done(true /* manual
Sigurður Ásgeirsson 2014/12/19 13:40:58 Done.
+
+ base::WaitableEvent handler_done;
+ base::ProcessHandle process;
+ const base::char16* registry_path;
+};
+HandlerData *handler_data = NULL;
grt (UTC plus 2) 2014/12/19 01:57:34 NULL -> nullptr
grt (UTC plus 2) 2014/12/19 01:57:34 nit: newline before this
grt (UTC plus 2) 2014/12/19 01:57:35 g_handler_data to emphasize that this is a global
Sigurður Ásgeirsson 2014/12/19 13:40:58 Done.
Sigurður Ásgeirsson 2014/12/19 13:40:58 Done.
Sigurður Ásgeirsson 2014/12/19 13:40:58 Done.
+
+static BOOL CALLBACK ConsoleCtrlHandler(DWORD ctl_type) {
grt (UTC plus 2) 2014/12/19 01:57:35 nit: don't use "static" in the unnamed namespace
Sigurður Ásgeirsson 2014/12/19 13:40:58 Done.
+ if (handler_data && ctl_type == CTRL_LOGOFF_EVENT) {
+ // Record the watcher logoff event in the browser's exit funnel.
+ browser_watcher::ExitFunnel funnel;
+ funnel.Init(handler_data->registry_path, handler_data->process);
+ funnel.RecordEvent(L"WatcherLogoff");
+
+ // Release the main function.
+ handler_data->handler_done.Signal();
+ }
+
+ // Fall through to the next handler in turn.
+ return FALSE;
+}
+
} // namespace
// The main entry point to the watcher, declared as extern "C" to avoid name
@@ -44,6 +70,14 @@ extern "C" int WatcherMain(const base::char16* registry_path) {
// Attempt to wait on our parent process, and record its exit status.
if (exit_code_watcher.ParseArguments(
*base::CommandLine::ForCurrentProcess())) {
+ // Set up a console control handler, and provide it the data it needs
+ // to record into the browser's exit funnel.
+ HandlerData data;
+ data.process = exit_code_watcher.process().Handle();
+ data.registry_path = registry_path;
+ handler_data = &data;
+ ::SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE);
grt (UTC plus 2) 2014/12/19 01:57:34 i think explicitly passing a pointer rather than r
Sigurður Ásgeirsson 2014/12/19 13:40:58 Done.
+
// Wait on the process.
exit_code_watcher.WaitForExit();
@@ -51,6 +85,22 @@ extern "C" int WatcherMain(const base::char16* registry_path) {
funnel.Init(registry_path, exit_code_watcher.process().Handle());
funnel.RecordEvent(L"BrowserExit");
+ // Chrome/content exit codes are currently in the range of 0-28.
+ // Anything outside this range is strange, so watch harder.
+ int exit_code = exit_code_watcher.exit_code();
+ if (exit_code < 0 || exit_code > 28) {
+ // Wait for a max of 30 seconds to see whether we get notified of logoff.
+ data.handler_done.TimedWait(base::TimeDelta::FromSeconds(30));
+ }
+
+ // Remove the console control handler.
+ // There is a potential race here, where the handler might be executing
+ // already as we fall through here. Hopefully SetConsoleCtrlHandler is
+ // synchronizing against handler execution, for there's no other way to
+ // close the race. Thankfully we'll just get snuffed out, as this is logoff.
+ ::SetConsoleCtrlHandler(ConsoleCtrlHandler, FALSE);
grt (UTC plus 2) 2014/12/19 01:57:35 here, too
Sigurður Ásgeirsson 2014/12/19 13:40:58 Done.
+ handler_data = NULL;
grt (UTC plus 2) 2014/12/19 01:57:35 NULL -> nullptr
Sigurður Ásgeirsson 2014/12/19 13:40:58 Done.
+
ret = 0;
}
« no previous file with comments | « no previous file | components/browser_watcher/exit_code_watcher_win.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698