exit_code_watcher_win.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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.
  4. #ifndef COMPONENTS_BROWSER_WATCHER_EXIT_CODE_WATCHER_WIN_H_
  5. #define COMPONENTS_BROWSER_WATCHER_EXIT_CODE_WATCHER_WIN_H_
  6. #include "base/process/process.h"
  7. #include "base/threading/thread.h"
  8. #include "base/win/scoped_handle.h"
  9. namespace browser_watcher {
  10. // Watches for the exit code of a process and records
  11. class ExitCodeWatcher {
  12. public:
  13. ExitCodeWatcher();
  14. ExitCodeWatcher(const ExitCodeWatcher&) = delete;
  15. ExitCodeWatcher& operator=(const ExitCodeWatcher&) = delete;
  16. ~ExitCodeWatcher();
  17. // This function expects |process| to be open with sufficient privilege to
  18. // wait and retrieve the process exit code.
  19. // It checks the handle for validity and takes ownership of it.
  20. bool Initialize(base::Process process);
  21. bool StartWatching();
  22. void StopWatching();
  23. const base::Process& process() const { return process_; }
  24. int ExitCodeForTesting() const { return exit_code_; }
  25. private:
  26. // Waits for the process to exit and records its exit code in a histogram.
  27. // This is a blocking call.
  28. void WaitForExit();
  29. // Watched process and its creation time.
  30. base::Process process_;
  31. // The thread that runs WaitForExit().
  32. base::Thread background_thread_;
  33. // The exit code of the watched process. Valid after WaitForExit.
  34. int exit_code_;
  35. // Event handle to use to stop exit watcher thread
  36. base::win::ScopedHandle stop_watching_handle_;
  37. };
  38. } // namespace browser_watcher
  39. #endif // COMPONENTS_BROWSER_WATCHER_EXIT_CODE_WATCHER_WIN_H_