siggi | c0d0a0e | 2014-11-17 23:29:19 | [diff] [blame] | 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. |
siggi | 420541c | 2014-11-18 23:16:32 | [diff] [blame] | 4 | #ifndef COMPONENTS_BROWSER_WATCHER_EXIT_CODE_WATCHER_WIN_H_ |
| 5 | #define COMPONENTS_BROWSER_WATCHER_EXIT_CODE_WATCHER_WIN_H_ |
siggi | c0d0a0e | 2014-11-17 23:29:19 | [diff] [blame] | 6 | |
| 7 | #include "base/macros.h" |
siggi | 581938f | 2014-12-18 21:17:15 | [diff] [blame] | 8 | #include "base/process/process.h" |
Chris Davis | 2e8cc3af | 2020-02-06 21:40:32 | [diff] [blame] | 9 | #include "base/threading/thread.h" |
Mike Rorke | f36971b | 2020-03-06 17:56:29 | [diff] [blame] | 10 | #include "base/win/scoped_handle.h" |
siggi | c0d0a0e | 2014-11-17 23:29:19 | [diff] [blame] | 11 | |
siggi | c0d0a0e | 2014-11-17 23:29:19 | [diff] [blame] | 12 | namespace browser_watcher { |
| 13 | |
Chris Davis | 2e8cc3af | 2020-02-06 21:40:32 | [diff] [blame] | 14 | // Watches for the exit code of a process and records |
siggi | c0d0a0e | 2014-11-17 23:29:19 | [diff] [blame] | 15 | class ExitCodeWatcher { |
| 16 | public: |
Chris Davis | 2e8cc3af | 2020-02-06 21:40:32 | [diff] [blame] | 17 | ExitCodeWatcher(); |
siggi | c0d0a0e | 2014-11-17 23:29:19 | [diff] [blame] | 18 | ~ExitCodeWatcher(); |
| 19 | |
erikwright | 7c4a426 | 2015-01-09 18:32:33 | [diff] [blame] | 20 | // This function expects |process| to be open with sufficient privilege to |
| 21 | // wait and retrieve the process exit code. |
siggi | c0d0a0e | 2014-11-17 23:29:19 | [diff] [blame] | 22 | // It checks the handle for validity and takes ownership of it. |
erikwright | 7c4a426 | 2015-01-09 18:32:33 | [diff] [blame] | 23 | bool Initialize(base::Process process); |
siggi | c0d0a0e | 2014-11-17 23:29:19 | [diff] [blame] | 24 | |
Chris Davis | 2e8cc3af | 2020-02-06 21:40:32 | [diff] [blame] | 25 | bool StartWatching(); |
siggi | c0d0a0e | 2014-11-17 23:29:19 | [diff] [blame] | 26 | |
Mike Rorke | f36971b | 2020-03-06 17:56:29 | [diff] [blame] | 27 | void StopWatching(); |
| 28 | |
siggi | 581938f | 2014-12-18 21:17:15 | [diff] [blame] | 29 | const base::Process& process() const { return process_; } |
siggi | d497a1a | 2014-12-19 15:20:51 | [diff] [blame] | 30 | int exit_code() const { return exit_code_; } |
siggi | c0d0a0e | 2014-11-17 23:29:19 | [diff] [blame] | 31 | |
| 32 | private: |
| 33 | // Writes |exit_code| to registry, returns true on success. |
| 34 | bool WriteProcessExitCode(int exit_code); |
| 35 | |
Chris Davis | 2e8cc3af | 2020-02-06 21:40:32 | [diff] [blame] | 36 | // Waits for the process to exit and records its exit code in registry. |
| 37 | // This is a blocking call. |
| 38 | void WaitForExit(); |
siggi | c0d0a0e | 2014-11-17 23:29:19 | [diff] [blame] | 39 | |
siggi | d497a1a | 2014-12-19 15:20:51 | [diff] [blame] | 40 | // Watched process and its creation time. |
siggi | 581938f | 2014-12-18 21:17:15 | [diff] [blame] | 41 | base::Process process_; |
Chris Davis | 2e8cc3af | 2020-02-06 21:40:32 | [diff] [blame] | 42 | |
| 43 | // The thread that runs WaitForExit(). |
| 44 | base::Thread background_thread_; |
siggi | c0d0a0e | 2014-11-17 23:29:19 | [diff] [blame] | 45 | |
siggi | d497a1a | 2014-12-19 15:20:51 | [diff] [blame] | 46 | // The exit code of the watched process. Valid after WaitForExit. |
| 47 | int exit_code_; |
| 48 | |
Mike Rorke | f36971b | 2020-03-06 17:56:29 | [diff] [blame] | 49 | // Event handle to use to stop exit watcher thread |
| 50 | base::win::ScopedHandle stop_watching_handle_; |
| 51 | |
siggi | c0d0a0e | 2014-11-17 23:29:19 | [diff] [blame] | 52 | DISALLOW_COPY_AND_ASSIGN(ExitCodeWatcher); |
| 53 | }; |
| 54 | |
| 55 | } // namespace browser_watcher |
| 56 | |
siggi | 420541c | 2014-11-18 23:16:32 | [diff] [blame] | 57 | #endif // COMPONENTS_BROWSER_WATCHER_EXIT_CODE_WATCHER_WIN_H_ |