1 // Copyright (c) 2014 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.
7 #include "base/at_exit.h"
9 #include "base/bind_helpers.h"
10 #include "base/command_line.h"
11 #include "base/logging_win.h"
12 #include "base/macros.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/message_loop/message_loop.h"
15 #include "base/message_loop/message_loop_proxy.h"
16 #include "base/process/process.h"
17 #include "base/run_loop.h"
18 #include "base/sequenced_task_runner.h"
19 #include "base/synchronization/waitable_event.h"
20 #include "base/template_util.h"
21 #include "base/threading/thread.h"
22 #include "base/time/time.h"
23 #include "chrome/chrome_watcher/chrome_watcher_main_api.h"
24 #include "components/browser_watcher/endsession_watcher_window_win.h"
25 #include "components/browser_watcher/exit_code_watcher_win.h"
26 #include "components/browser_watcher/exit_funnel_win.h"
30 // Use the same log facility as Chrome for convenience.
31 // {7FE69228-633E-4f06-80C1-527FEA23E3A7}
32 const GUID kChromeWatcherTraceProviderName
= {
33 0x7fe69228, 0x633e, 0x4f06,
34 { 0x80, 0xc1, 0x52, 0x7f, 0xea, 0x23, 0xe3, 0xa7 } };
36 // Takes care of monitoring a browser. This class watches for a browser's exit
37 // code, as well as listening for WM_ENDSESSION messages. Events are recorded
38 // in an exit funnel, for reporting the next time Chrome runs.
39 class BrowserMonitor
{
41 BrowserMonitor(base::RunLoop
* run_loop
, const base::char16
* registry_path
);
44 // Starts the monitor, returns true on success.
45 bool StartWatching(const base::char16
* registry_path
,
46 base::Process process
);
49 // Called from EndSessionWatcherWindow on a WM_ENDSESSION message.
50 void OnEndSession(LPARAM lparam
);
52 // Blocking function that runs on |background_thread_|.
55 // Posted to main thread from Watch when browser exits.
58 // True if BrowserExited has run.
61 // The funnel used to record events for this browser.
62 browser_watcher::ExitFunnel exit_funnel_
;
64 browser_watcher::ExitCodeWatcher exit_code_watcher_
;
65 browser_watcher::EndSessionWatcherWindow end_session_watcher_window_
;
67 // The thread that runs Watch().
68 base::Thread background_thread_
;
70 // The run loop for the main thread and its task runner.
71 base::RunLoop
* run_loop_
;
72 scoped_refptr
<base::SequencedTaskRunner
> main_thread_
;
74 DISALLOW_COPY_AND_ASSIGN(BrowserMonitor
);
77 BrowserMonitor::BrowserMonitor(base::RunLoop
* run_loop
,
78 const base::char16
* registry_path
) :
79 browser_exited_(false),
80 exit_code_watcher_(registry_path
),
81 end_session_watcher_window_(
82 base::Bind(&BrowserMonitor::OnEndSession
, base::Unretained(this))),
83 background_thread_("BrowserWatcherThread"),
85 main_thread_(base::MessageLoopProxy::current()) {
88 BrowserMonitor::~BrowserMonitor() {
91 bool BrowserMonitor::StartWatching(const base::char16
* registry_path
,
92 base::Process process
) {
93 if (!exit_code_watcher_
.Initialize(process
.Pass()))
96 if (!exit_funnel_
.Init(registry_path
,
97 exit_code_watcher_
.process().Handle())) {
101 if (!background_thread_
.StartWithOptions(
102 base::Thread::Options(base::MessageLoop::TYPE_IO
, 0))) {
106 if (!background_thread_
.task_runner()->PostTask(FROM_HERE
,
107 base::Bind(&BrowserMonitor::Watch
, base::Unretained(this)))) {
108 background_thread_
.Stop();
115 void BrowserMonitor::OnEndSession(LPARAM lparam
) {
116 DCHECK_EQ(main_thread_
, base::MessageLoopProxy::current());
118 exit_funnel_
.RecordEvent(L
"WatcherLogoff");
119 if (lparam
& ENDSESSION_CLOSEAPP
)
120 exit_funnel_
.RecordEvent(L
"ES_CloseApp");
121 if (lparam
& ENDSESSION_CRITICAL
)
122 exit_funnel_
.RecordEvent(L
"ES_Critical");
123 if (lparam
& ENDSESSION_LOGOFF
)
124 exit_funnel_
.RecordEvent(L
"ES_Logoff");
125 const LPARAM kKnownBits
=
126 ENDSESSION_CLOSEAPP
| ENDSESSION_CRITICAL
| ENDSESSION_LOGOFF
;
127 if (lparam
& ~kKnownBits
)
128 exit_funnel_
.RecordEvent(L
"ES_Other");
130 // Belt-and-suspenders; make sure our message loop exits ASAP.
135 void BrowserMonitor::Watch() {
136 // This needs to run on an IO thread.
137 DCHECK_NE(main_thread_
, base::MessageLoopProxy::current());
139 exit_code_watcher_
.WaitForExit();
140 exit_funnel_
.RecordEvent(L
"BrowserExit");
142 main_thread_
->PostTask(FROM_HERE
,
143 base::Bind(&BrowserMonitor::BrowserExited
, base::Unretained(this)));
146 void BrowserMonitor::BrowserExited() {
147 // This runs in the main thread.
148 DCHECK_EQ(main_thread_
, base::MessageLoopProxy::current());
150 // Note that the browser has exited.
151 browser_exited_
= true;
153 // Our background thread has served it's purpose.
154 background_thread_
.Stop();
156 const int exit_code
= exit_code_watcher_
.exit_code();
157 if (exit_code
>= 0 && exit_code
<= 28) {
158 // The browser exited with a well-known exit code, quit this process
162 // The browser exited abnormally, wait around for a little bit to see
163 // whether this instance will get a logoff message.
164 main_thread_
->PostDelayedTask(FROM_HERE
,
165 run_loop_
->QuitClosure(),
166 base::TimeDelta::FromSeconds(30));
172 // The main entry point to the watcher, declared as extern "C" to avoid name
174 extern "C" int WatcherMain(const base::char16
* registry_path
,
175 HANDLE process_handle
) {
176 base::Process
process(process_handle
);
178 // The exit manager is in charge of calling the dtors of singletons.
179 base::AtExitManager exit_manager
;
180 // Initialize the commandline singleton from the environment.
181 base::CommandLine::Init(0, nullptr);
183 logging::LogEventProvider::Initialize(kChromeWatcherTraceProviderName
);
185 // Arrange to be shut down as late as possible, as we want to outlive
186 // chrome.exe in order to report its exit status.
187 ::SetProcessShutdownParameters(0x100, SHUTDOWN_NORETRY
);
189 // Run a UI message loop on the main thread.
190 base::MessageLoop
msg_loop(base::MessageLoop::TYPE_UI
);
191 msg_loop
.set_thread_name("WatcherMainThread");
193 base::RunLoop run_loop
;
194 BrowserMonitor
monitor(&run_loop
, registry_path
);
195 if (!monitor
.StartWatching(registry_path
, process
.Pass()))
200 // Wind logging down.
201 logging::LogEventProvider::Uninitialize();
207 base::is_same
<decltype(&WatcherMain
), ChromeWatcherMainFunction
>::value
,
208 "WatcherMain() has wrong type");