blob: b018726805e6c6d7326d4ae5d4783d8a7c6e5fa4 [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.
siggi420541c2014-11-18 23:16:324#ifndef COMPONENTS_BROWSER_WATCHER_EXIT_CODE_WATCHER_WIN_H_
5#define COMPONENTS_BROWSER_WATCHER_EXIT_CODE_WATCHER_WIN_H_
siggic0d0a0e2014-11-17 23:29:196
7#include "base/macros.h"
siggi581938f2014-12-18 21:17:158#include "base/process/process.h"
Chris Davis2e8cc3af2020-02-06 21:40:329#include "base/threading/thread.h"
Mike Rorkef36971b2020-03-06 17:56:2910#include "base/win/scoped_handle.h"
siggic0d0a0e2014-11-17 23:29:1911
siggic0d0a0e2014-11-17 23:29:1912namespace browser_watcher {
13
Chris Davis2e8cc3af2020-02-06 21:40:3214// Watches for the exit code of a process and records
siggic0d0a0e2014-11-17 23:29:1915class ExitCodeWatcher {
16 public:
Chris Davis2e8cc3af2020-02-06 21:40:3217 ExitCodeWatcher();
siggic0d0a0e2014-11-17 23:29:1918 ~ExitCodeWatcher();
19
erikwright7c4a4262015-01-09 18:32:3320 // This function expects |process| to be open with sufficient privilege to
21 // wait and retrieve the process exit code.
siggic0d0a0e2014-11-17 23:29:1922 // It checks the handle for validity and takes ownership of it.
erikwright7c4a4262015-01-09 18:32:3323 bool Initialize(base::Process process);
siggic0d0a0e2014-11-17 23:29:1924
Chris Davis2e8cc3af2020-02-06 21:40:3225 bool StartWatching();
siggic0d0a0e2014-11-17 23:29:1926
Mike Rorkef36971b2020-03-06 17:56:2927 void StopWatching();
28
siggi581938f2014-12-18 21:17:1529 const base::Process& process() const { return process_; }
siggid497a1a2014-12-19 15:20:5130 int exit_code() const { return exit_code_; }
siggic0d0a0e2014-11-17 23:29:1931
32 private:
33 // Writes |exit_code| to registry, returns true on success.
34 bool WriteProcessExitCode(int exit_code);
35
Chris Davis2e8cc3af2020-02-06 21:40:3236 // Waits for the process to exit and records its exit code in registry.
37 // This is a blocking call.
38 void WaitForExit();
siggic0d0a0e2014-11-17 23:29:1939
siggid497a1a2014-12-19 15:20:5140 // Watched process and its creation time.
siggi581938f2014-12-18 21:17:1541 base::Process process_;
Chris Davis2e8cc3af2020-02-06 21:40:3242
43 // The thread that runs WaitForExit().
44 base::Thread background_thread_;
siggic0d0a0e2014-11-17 23:29:1945
siggid497a1a2014-12-19 15:20:5146 // The exit code of the watched process. Valid after WaitForExit.
47 int exit_code_;
48
Mike Rorkef36971b2020-03-06 17:56:2949 // Event handle to use to stop exit watcher thread
50 base::win::ScopedHandle stop_watching_handle_;
51
siggic0d0a0e2014-11-17 23:29:1952 DISALLOW_COPY_AND_ASSIGN(ExitCodeWatcher);
53};
54
55} // namespace browser_watcher
56
siggi420541c2014-11-18 23:16:3257#endif // COMPONENTS_BROWSER_WATCHER_EXIT_CODE_WATCHER_WIN_H_