Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / chrome_watcher / chrome_watcher_main.cc
blobe51de7cc61b4da1b519768c10c7734ca67a3eb9d
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.
5 #include <windows.h>
6 #include <sddl.h>
8 #include "base/at_exit.h"
9 #include "base/bind.h"
10 #include "base/bind_helpers.h"
11 #include "base/callback_helpers.h"
12 #include "base/command_line.h"
13 #include "base/file_version_info.h"
14 #include "base/files/file_path.h"
15 #include "base/logging_win.h"
16 #include "base/macros.h"
17 #include "base/memory/ref_counted.h"
18 #include "base/process/process.h"
19 #include "base/run_loop.h"
20 #include "base/sequenced_task_runner.h"
21 #include "base/single_thread_task_runner.h"
22 #include "base/strings/string16.h"
23 #include "base/strings/string_number_conversions.h"
24 #include "base/strings/string_piece.h"
25 #include "base/strings/utf_string_conversions.h"
26 #include "base/synchronization/waitable_event.h"
27 #include "base/template_util.h"
28 #include "base/thread_task_runner_handle.h"
29 #include "base/threading/thread.h"
30 #include "base/time/time.h"
31 #include "base/win/scoped_handle.h"
32 #include "base/win/win_util.h"
34 #include "chrome/chrome_watcher/chrome_watcher_main_api.h"
35 #include "chrome/installer/util/util_constants.h"
36 #include "components/browser_watcher/endsession_watcher_window_win.h"
37 #include "components/browser_watcher/exit_code_watcher_win.h"
38 #include "components/browser_watcher/exit_funnel_win.h"
39 #include "components/browser_watcher/window_hang_monitor_win.h"
41 #ifdef KASKO
42 #include "syzygy/kasko/api/reporter.h"
43 #endif
45 namespace {
47 // Use the same log facility as Chrome for convenience.
48 // {7FE69228-633E-4f06-80C1-527FEA23E3A7}
49 const GUID kChromeWatcherTraceProviderName = {
50 0x7fe69228, 0x633e, 0x4f06,
51 { 0x80, 0xc1, 0x52, 0x7f, 0xea, 0x23, 0xe3, 0xa7 } };
53 // The amount of time we wait around for a WM_ENDSESSION or a process exit.
54 const int kDelayTimeSeconds = 30;
56 // Takes care of monitoring a browser. This class watches for a browser's exit
57 // code, as well as listening for WM_ENDSESSION messages. Events are recorded in
58 // an exit funnel, for reporting the next time Chrome runs.
59 class BrowserMonitor {
60 public:
61 BrowserMonitor(base::RunLoop* run_loop, const base::char16* registry_path);
62 ~BrowserMonitor();
64 // Initiates the asynchronous monitoring process, returns true on success.
65 // |on_initialized_event| will be signaled immediately before blocking on the
66 // exit of |process|.
67 bool StartWatching(const base::char16* registry_path,
68 base::Process process,
69 base::win::ScopedHandle on_initialized_event);
71 private:
72 // Called from EndSessionWatcherWindow on a end session messages.
73 void OnEndSessionMessage(UINT message, LPARAM lparam);
75 // Blocking function that runs on |background_thread_|. Signals
76 // |on_initialized_event| before waiting for the browser process to exit.
77 void Watch(base::win::ScopedHandle on_initialized_event);
79 // Posted to main thread from Watch when browser exits.
80 void BrowserExited();
82 // The funnel used to record events for this browser.
83 browser_watcher::ExitFunnel exit_funnel_;
85 browser_watcher::ExitCodeWatcher exit_code_watcher_;
86 browser_watcher::EndSessionWatcherWindow end_session_watcher_window_;
88 // The thread that runs Watch().
89 base::Thread background_thread_;
91 // Set when the browser has exited, used to stretch the watcher's lifetime
92 // when WM_ENDSESSION occurs before browser exit.
93 base::WaitableEvent browser_exited_;
95 // The run loop for the main thread and its task runner.
96 base::RunLoop* run_loop_;
97 scoped_refptr<base::SequencedTaskRunner> main_thread_;
99 DISALLOW_COPY_AND_ASSIGN(BrowserMonitor);
102 BrowserMonitor::BrowserMonitor(base::RunLoop* run_loop,
103 const base::char16* registry_path)
104 : exit_code_watcher_(registry_path),
105 end_session_watcher_window_(
106 base::Bind(&BrowserMonitor::OnEndSessionMessage,
107 base::Unretained(this))),
108 background_thread_("BrowserWatcherThread"),
109 browser_exited_(true, false), // manual reset, initially non-signalled.
110 run_loop_(run_loop),
111 main_thread_(base::ThreadTaskRunnerHandle::Get()) {
114 BrowserMonitor::~BrowserMonitor() {
117 bool BrowserMonitor::StartWatching(
118 const base::char16* registry_path,
119 base::Process process,
120 base::win::ScopedHandle on_initialized_event) {
121 if (!exit_code_watcher_.Initialize(process.Pass()))
122 return false;
124 if (!exit_funnel_.Init(registry_path,
125 exit_code_watcher_.process().Handle())) {
126 return false;
129 if (!background_thread_.StartWithOptions(
130 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))) {
131 return false;
134 if (!background_thread_.task_runner()->PostTask(
135 FROM_HERE, base::Bind(&BrowserMonitor::Watch, base::Unretained(this),
136 base::Passed(on_initialized_event.Pass())))) {
137 background_thread_.Stop();
138 return false;
141 return true;
144 void BrowserMonitor::OnEndSessionMessage(UINT message, LPARAM lparam) {
145 DCHECK_EQ(main_thread_, base::ThreadTaskRunnerHandle::Get());
147 if (message == WM_QUERYENDSESSION) {
148 exit_funnel_.RecordEvent(L"WatcherQueryEndSession");
149 } else if (message == WM_ENDSESSION) {
150 exit_funnel_.RecordEvent(L"WatcherEndSession");
152 if (lparam & ENDSESSION_CLOSEAPP)
153 exit_funnel_.RecordEvent(L"ES_CloseApp");
154 if (lparam & ENDSESSION_CRITICAL)
155 exit_funnel_.RecordEvent(L"ES_Critical");
156 if (lparam & ENDSESSION_LOGOFF)
157 exit_funnel_.RecordEvent(L"ES_Logoff");
158 const LPARAM kKnownBits =
159 ENDSESSION_CLOSEAPP | ENDSESSION_CRITICAL | ENDSESSION_LOGOFF;
160 if (lparam & ~kKnownBits)
161 exit_funnel_.RecordEvent(L"ES_Other");
163 // If the browser hasn't exited yet, dally for a bit to try and stretch this
164 // process' lifetime to give it some more time to capture the browser exit.
165 browser_exited_.TimedWait(base::TimeDelta::FromSeconds(kDelayTimeSeconds));
167 run_loop_->Quit();
170 void BrowserMonitor::Watch(base::win::ScopedHandle on_initialized_event) {
171 // This needs to run on an IO thread.
172 DCHECK_NE(main_thread_, base::ThreadTaskRunnerHandle::Get());
174 // Signal our client now that the Kasko reporter is initialized and we have
175 // cleared all of the obstacles that might lead to an early exit.
176 ::SetEvent(on_initialized_event.Get());
177 on_initialized_event.Close();
179 exit_code_watcher_.WaitForExit();
180 exit_funnel_.RecordEvent(L"BrowserExit");
182 // Note that the browser has exited.
183 browser_exited_.Signal();
185 main_thread_->PostTask(FROM_HERE,
186 base::Bind(&BrowserMonitor::BrowserExited, base::Unretained(this)));
189 void BrowserMonitor::BrowserExited() {
190 // This runs in the main thread.
191 DCHECK_EQ(main_thread_, base::ThreadTaskRunnerHandle::Get());
193 // Our background thread has served it's purpose.
194 background_thread_.Stop();
196 const int exit_code = exit_code_watcher_.exit_code();
197 if (exit_code >= 0 && exit_code <= 28) {
198 // The browser exited with a well-known exit code, quit this process
199 // immediately.
200 run_loop_->Quit();
201 } else {
202 // The browser exited abnormally, wait around for a little bit to see
203 // whether this instance will get a logoff message.
204 main_thread_->PostDelayedTask(
205 FROM_HERE,
206 run_loop_->QuitClosure(),
207 base::TimeDelta::FromSeconds(kDelayTimeSeconds));
211 void OnWindowEvent(
212 const base::string16& registry_path,
213 base::Process process,
214 const base::Callback<void(const base::Process&)>& on_hung_callback,
215 browser_watcher::WindowHangMonitor::WindowEvent window_event) {
216 browser_watcher::ExitFunnel exit_funnel;
217 if (exit_funnel.Init(registry_path.c_str(), process.Handle())) {
218 switch (window_event) {
219 case browser_watcher::WindowHangMonitor::WINDOW_NOT_FOUND:
220 exit_funnel.RecordEvent(L"MessageWindowNotFound");
221 break;
222 case browser_watcher::WindowHangMonitor::WINDOW_HUNG:
223 exit_funnel.RecordEvent(L"MessageWindowHung");
224 if (!on_hung_callback.is_null())
225 on_hung_callback.Run(process);
226 break;
227 case browser_watcher::WindowHangMonitor::WINDOW_VANISHED:
228 exit_funnel.RecordEvent(L"MessageWindowVanished");
229 break;
230 default:
231 NOTREACHED();
232 break;
237 #ifdef KASKO
238 void DumpHungBrowserProcess(const base::string16& channel,
239 const base::Process& process) {
240 // TODO(erikwright): Rather than recreating these crash keys here, it would be
241 // ideal to read them directly from the browser process.
243 // This is looking up the version of chrome_watcher.dll, which is equivalent
244 // for our purposes to chrome.dll.
245 scoped_ptr<FileVersionInfo> version_info(
246 FileVersionInfo::CreateFileVersionInfoForModule(
247 reinterpret_cast<HMODULE>(&__ImageBase)));
248 using CrashKeyStrings = std::pair<base::string16, base::string16>;
249 std::vector<CrashKeyStrings> crash_key_strings;
250 if (version_info.get()) {
251 crash_key_strings.push_back(
252 CrashKeyStrings(L"prod", version_info->product_short_name()));
253 base::string16 version = version_info->product_version();
254 if (!version_info->is_official_build())
255 version.append(base::ASCIIToUTF16("-devel"));
256 crash_key_strings.push_back(CrashKeyStrings(L"ver", version));
257 } else {
258 // No version info found. Make up the values.
259 crash_key_strings.push_back(CrashKeyStrings(L"prod", L"Chrome"));
260 crash_key_strings.push_back(CrashKeyStrings(L"ver", L"0.0.0.0-devel"));
262 crash_key_strings.push_back(CrashKeyStrings(L"channel", channel));
263 crash_key_strings.push_back(CrashKeyStrings(L"plat", L"Win32"));
264 crash_key_strings.push_back(CrashKeyStrings(L"ptype", L"browser"));
265 crash_key_strings.push_back(
266 CrashKeyStrings(L"pid", base::IntToString16(process.Pid())));
267 crash_key_strings.push_back(CrashKeyStrings(L"hung-process", L"1"));
269 std::vector<const base::char16*> key_buffers;
270 std::vector<const base::char16*> value_buffers;
271 for (auto& strings : crash_key_strings) {
272 key_buffers.push_back(strings.first.c_str());
273 value_buffers.push_back(strings.second.c_str());
275 key_buffers.push_back(nullptr);
276 value_buffers.push_back(nullptr);
277 // TODO(erikwright): Make the dump-type channel-dependent.
278 kasko::api::SendReportForProcess(process.Handle(),
279 kasko::api::LARGER_DUMP_TYPE,
280 key_buffers.data(), value_buffers.data());
283 void LoggedDeregisterEventSource(HANDLE event_source_handle) {
284 if (!::DeregisterEventSource(event_source_handle))
285 DPLOG(ERROR) << "DeregisterEventSource";
288 void LoggedLocalFree(PSID sid) {
289 if (::LocalFree(sid) != nullptr)
290 DPLOG(ERROR) << "LocalFree";
293 void OnCrashReportUpload(void* context,
294 const base::char16* report_id,
295 const base::char16* minidump_path,
296 const base::char16* const* keys,
297 const base::char16* const* values) {
298 // Open the event source.
299 HANDLE event_source_handle = ::RegisterEventSource(NULL, L"Chrome");
300 if (!event_source_handle) {
301 PLOG(ERROR) << "RegisterEventSource";
302 return;
304 // Ensure cleanup on scope exit.
305 base::ScopedClosureRunner deregister_event_source(
306 base::Bind(&LoggedDeregisterEventSource, event_source_handle));
308 // Get the user's SID for the log record.
309 base::string16 sid_string;
310 PSID sid = nullptr;
311 if (base::win::GetUserSidString(&sid_string)) {
312 if (!sid_string.empty()) {
313 if (!::ConvertStringSidToSid(sid_string.c_str(), &sid))
314 DPLOG(ERROR) << "ConvertStringSidToSid";
315 DCHECK(sid);
318 // Ensure cleanup on scope exit.
319 base::ScopedClosureRunner free_sid(
320 base::Bind(&LoggedLocalFree, base::Unretained(sid)));
322 // Generate the message.
323 // Note that the format of this message must match the consumer in
324 // chrome/browser/crash_upload_list_win.cc.
325 base::string16 message =
326 L"Crash uploaded. Id=" + base::string16(report_id) + L".";
328 // Matches Omaha.
329 const int kCrashUploadEventId = 2;
331 // Report the event.
332 const base::char16* strings[] = {message.c_str()};
333 if (!::ReportEvent(event_source_handle, EVENTLOG_INFORMATION_TYPE,
334 0, // category
335 kCrashUploadEventId, sid,
336 1, // count
337 0, strings, nullptr)) {
338 DPLOG(ERROR);
341 // TODO(erikwright): Copy minidump to some "last dump" location?
344 #endif // KASKO
346 } // namespace
348 // The main entry point to the watcher, declared as extern "C" to avoid name
349 // mangling.
350 extern "C" int WatcherMain(const base::char16* registry_path,
351 HANDLE process_handle,
352 HANDLE on_initialized_event_handle,
353 const base::char16* browser_data_directory,
354 const base::char16* message_window_name,
355 const base::char16* channel_name) {
356 base::Process process(process_handle);
357 base::win::ScopedHandle on_initialized_event(on_initialized_event_handle);
359 // The exit manager is in charge of calling the dtors of singletons.
360 base::AtExitManager exit_manager;
361 // Initialize the commandline singleton from the environment.
362 base::CommandLine::Init(0, nullptr);
364 logging::LogEventProvider::Initialize(kChromeWatcherTraceProviderName);
366 // Arrange to be shut down as late as possible, as we want to outlive
367 // chrome.exe in order to report its exit status.
368 ::SetProcessShutdownParameters(0x100, SHUTDOWN_NORETRY);
370 base::Callback<void(const base::Process&)> on_hung_callback;
372 #ifdef KASKO
373 bool launched_kasko = kasko::api::InitializeReporter(
374 GetKaskoEndpoint(process.Pid()).c_str(),
375 L"https://clients2.google.com/cr/report",
376 base::FilePath(browser_data_directory)
377 .Append(L"Crash Reports")
378 .value()
379 .c_str(),
380 base::FilePath(browser_data_directory)
381 .Append(kPermanentlyFailedReportsSubdir)
382 .value()
383 .c_str(),
384 &OnCrashReportUpload, nullptr);
385 #ifdef KASKO_HANG_REPORTS
386 if (launched_kasko &&
387 base::StringPiece16(channel_name) == installer::kChromeChannelCanary) {
388 on_hung_callback = base::Bind(&DumpHungBrowserProcess, channel_name);
390 #endif // KASKO_HANG_REPORTS
391 #endif // KASKO
393 // Run a UI message loop on the main thread.
394 base::MessageLoop msg_loop(base::MessageLoop::TYPE_UI);
395 msg_loop.set_thread_name("WatcherMainThread");
397 base::RunLoop run_loop;
398 BrowserMonitor monitor(&run_loop, registry_path);
399 if (!monitor.StartWatching(registry_path, process.Duplicate(),
400 on_initialized_event.Pass())) {
401 return 1;
405 // Scoped to force |hang_monitor| destruction before Kasko is shut down.
406 browser_watcher::WindowHangMonitor hang_monitor(
407 base::TimeDelta::FromSeconds(60), base::TimeDelta::FromSeconds(20),
408 base::Bind(&OnWindowEvent, registry_path,
409 base::Passed(process.Duplicate()), on_hung_callback));
410 hang_monitor.Initialize(process.Duplicate(), message_window_name);
412 run_loop.Run();
415 #ifdef KASKO
416 if (launched_kasko)
417 kasko::api::ShutdownReporter();
418 #endif // KASKO
420 // Wind logging down.
421 logging::LogEventProvider::Uninitialize();
423 return 0;
426 static_assert(
427 base::is_same<decltype(&WatcherMain), ChromeWatcherMainFunction>::value,
428 "WatcherMain() has wrong type");