Remove EWK_BRINGUPS for M120 #3
[platform/framework/web/chromium-efl.git] / chrome / app / exit_code_watcher_win.cc
1 // Copyright 2014 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/app/exit_code_watcher_win.h"
6
7 #include <windows.h>
8
9 #include <utility>
10
11 #include "base/check.h"
12 #include "base/functional/bind.h"
13 #include "base/message_loop/message_pump_type.h"
14 #include "base/metrics/histogram_base.h"
15 #include "base/metrics/sparse_histogram.h"
16 #include "base/process/process.h"
17 #include "base/process/process_handle.h"
18 #include "base/task/sequenced_task_runner.h"
19 #include "base/threading/thread.h"
20 #include "base/win/scoped_handle.h"
21
22 namespace {
23
24 constexpr char kBrowserExitCodeHistogramName[] = "Stability.BrowserExitCodes";
25
26 bool WriteProcessExitCode(int exit_code) {
27   if (exit_code != STILL_ACTIVE) {
28     // Record the exit codes in a sparse stability histogram, as the range of
29     // values used to report failures is large.
30     base::HistogramBase* exit_code_histogram =
31         base::SparseHistogram::FactoryGet(
32             kBrowserExitCodeHistogramName,
33             base::HistogramBase::kUmaStabilityHistogramFlag);
34     exit_code_histogram->Add(exit_code);
35     return true;
36   }
37   return false;
38 }
39
40 }  // namespace
41
42 ExitCodeWatcher::ExitCodeWatcher()
43     : background_thread_("ExitCodeWatcherThread"),
44       exit_code_(STILL_ACTIVE),
45       stop_watching_handle_(::CreateEvent(nullptr, TRUE, FALSE, nullptr)) {
46   DCHECK(stop_watching_handle_.IsValid());
47 }
48
49 ExitCodeWatcher::~ExitCodeWatcher() = default;
50
51 bool ExitCodeWatcher::Initialize(base::Process process) {
52   if (!process.IsValid()) {
53     return false;
54   }
55
56   DWORD process_pid = process.Pid();
57   if (process_pid == 0) {
58     return false;
59   }
60
61   FILETIME creation_time = {};
62   FILETIME dummy = {};
63   if (!::GetProcessTimes(process.Handle(), &creation_time, &dummy, &dummy,
64                          &dummy)) {
65     return false;
66   }
67
68   // Success, take ownership of the process.
69   process_ = std::move(process);
70   return true;
71 }
72
73 bool ExitCodeWatcher::StartWatching() {
74   if (!background_thread_.StartWithOptions(
75           base::Thread::Options(base::MessagePumpType::IO, 0))) {
76     return false;
77   }
78
79   // Unretained is safe because `this` owns the thread.
80   if (!background_thread_.task_runner()->PostTask(
81           FROM_HERE, base::BindOnce(&ExitCodeWatcher::WaitForExit,
82                                     base::Unretained(this)))) {
83     background_thread_.Stop();
84     return false;
85   }
86
87   return true;
88 }
89
90 void ExitCodeWatcher::StopWatching() {
91   if (stop_watching_handle_.IsValid()) {
92     ::SetEvent(stop_watching_handle_.Get());
93   }
94 }
95
96 void ExitCodeWatcher::WaitForExit() {
97   base::Process::WaitExitStatus wait_result =
98       process_.WaitForExitOrEvent(stop_watching_handle_, &exit_code_);
99   if (wait_result == base::Process::WaitExitStatus::PROCESS_EXITED) {
100     WriteProcessExitCode(exit_code_);
101   }
102 }