blob: 394824394fbeef6370144f5471ce26af51d68a8a [file] [log] [blame]
siggic0d0a0e2014-11-17 23:29:191// 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.
4
siggi420541c2014-11-18 23:16:325#include "components/browser_watcher/exit_code_watcher_win.h"
siggic0d0a0e2014-11-17 23:29:196
dcheng70c49422016-03-02 23:20:347#include <utility>
8
siggic0d0a0e2014-11-17 23:29:199#include "base/logging.h"
Chris Davis2e8cc3af2020-02-06 21:40:3210#include "base/metrics/sparse_histogram.h"
siggic0d0a0e2014-11-17 23:29:1911#include "base/process/kill.h"
Chris Davis2e8cc3af2020-02-06 21:40:3212#include "base/process/process.h"
13#include "base/process/process_handle.h"
14#include "base/sequenced_task_runner.h"
15#include "base/threading/thread.h"
16#include "base/threading/thread_task_runner_handle.h"
siggic0d0a0e2014-11-17 23:29:1917
Bruce Dawsonbfdc3fd2018-01-03 20:32:3618#include <windows.h>
19
siggic0d0a0e2014-11-17 23:29:1920namespace browser_watcher {
21
Chris Davis2e8cc3af2020-02-06 21:40:3222const char kBrowserExitCodeHistogramName[] = "Stability.BrowserExitCodes";
siggic0d0a0e2014-11-17 23:29:1923
Chris Davis2e8cc3af2020-02-06 21:40:3224ExitCodeWatcher::ExitCodeWatcher()
25 : background_thread_("ExitCodeWatcherThread"), exit_code_(STILL_ACTIVE) {}
siggic0d0a0e2014-11-17 23:29:1926
27ExitCodeWatcher::~ExitCodeWatcher() {
28}
29
erikwright7c4a4262015-01-09 18:32:3330bool ExitCodeWatcher::Initialize(base::Process process) {
rvargas960db882015-01-24 00:27:2531 if (!process.IsValid()) {
32 LOG(ERROR) << "Invalid parent handle, can't get parent process ID.";
33 return false;
34 }
35
36 DWORD process_pid = process.Pid();
siggic0d0a0e2014-11-17 23:29:1937 if (process_pid == 0) {
erikwright7c4a4262015-01-09 18:32:3338 LOG(ERROR) << "Invalid parent handle, can't get parent process ID.";
siggic0d0a0e2014-11-17 23:29:1939 return false;
40 }
41
42 FILETIME creation_time = {};
43 FILETIME dummy = {};
erikwright7c4a4262015-01-09 18:32:3344 if (!::GetProcessTimes(process.Handle(), &creation_time, &dummy, &dummy,
45 &dummy)) {
46 PLOG(ERROR) << "Invalid parent handle, can't get parent process times.";
siggic0d0a0e2014-11-17 23:29:1947 return false;
48 }
49
rvargas960db882015-01-24 00:27:2550 // Success, take ownership of the process.
dcheng70c49422016-03-02 23:20:3451 process_ = std::move(process);
siggic0d0a0e2014-11-17 23:29:1952
Chris Davis2e8cc3af2020-02-06 21:40:3253 return true;
54}
55
56bool ExitCodeWatcher::StartWatching() {
57 if (!background_thread_.StartWithOptions(
58 base::Thread::Options(base::MessagePumpType::IO, 0))) {
59 return false;
60 }
61
62 if (!background_thread_.task_runner()->PostTask(
63 FROM_HERE, base::BindOnce(&ExitCodeWatcher::WaitForExit,
64 base::Unretained(this)))) {
65 background_thread_.Stop();
66 return false;
67 }
68
69 return true;
siggic0d0a0e2014-11-17 23:29:1970}
71
72void ExitCodeWatcher::WaitForExit() {
siggid497a1a2014-12-19 15:20:5173 if (!process_.WaitForExit(&exit_code_)) {
siggic0d0a0e2014-11-17 23:29:1974 LOG(ERROR) << "Failed to wait for process.";
75 return;
76 }
siggic0d0a0e2014-11-17 23:29:1977
siggid497a1a2014-12-19 15:20:5178 WriteProcessExitCode(exit_code_);
siggic0d0a0e2014-11-17 23:29:1979}
80
81bool ExitCodeWatcher::WriteProcessExitCode(int exit_code) {
Chris Davis2e8cc3af2020-02-06 21:40:3282 if (exit_code != STILL_ACTIVE) {
83 // Record the exit codes in a sparse stability histogram, as the range of
84 // values used to report failures is large.
85 base::HistogramBase* exit_code_histogram =
86 base::SparseHistogram::FactoryGet(
87 kBrowserExitCodeHistogramName,
88 base::HistogramBase::kUmaStabilityHistogramFlag);
89 exit_code_histogram->Add(exit_code);
90 return true;
siggic0d0a0e2014-11-17 23:29:1991 }
Chris Davis2e8cc3af2020-02-06 21:40:3292 return false;
siggic0d0a0e2014-11-17 23:29:1993}
94
95} // namespace browser_watcher