blob: 644b2a55bb885fb442d0e400131a55c96e2bf371 [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"
siggic0d0a0e2014-11-17 23:29:199#include "base/strings/string16.h"
10#include "base/time/time.h"
11#include "base/win/scoped_handle.h"
12
siggic0d0a0e2014-11-17 23:29:1913namespace browser_watcher {
14
15// Watches for the exit code of a process and records it in a given registry
16// location.
17class ExitCodeWatcher {
18 public:
siggic0d0a0e2014-11-17 23:29:1919 // Initialize the watcher with a registry path.
20 explicit ExitCodeWatcher(const base::char16* registry_path);
21 ~ExitCodeWatcher();
22
23 // Initializes from arguments on |cmd_line|, returns true on success.
erikwright7c4a4262015-01-09 18:32:3324 // This function expects |process| to be open with sufficient privilege to
25 // wait and retrieve the process exit code.
siggic0d0a0e2014-11-17 23:29:1926 // It checks the handle for validity and takes ownership of it.
27 // The intent is for this handle to be inherited into the watcher process
28 // hosting the instance of this class.
erikwright7c4a4262015-01-09 18:32:3329 bool Initialize(base::Process process);
siggic0d0a0e2014-11-17 23:29:1930
31 // Waits for the process to exit and records its exit code in registry.
32 // This is a blocking call.
33 void WaitForExit();
34
siggi581938f2014-12-18 21:17:1535 const base::Process& process() const { return process_; }
siggid497a1a2014-12-19 15:20:5136 int exit_code() const { return exit_code_; }
siggic0d0a0e2014-11-17 23:29:1937
38 private:
39 // Writes |exit_code| to registry, returns true on success.
40 bool WriteProcessExitCode(int exit_code);
41
42 // The registry path the exit codes are written to.
43 base::string16 registry_path_;
44
siggid497a1a2014-12-19 15:20:5145 // Watched process and its creation time.
siggi581938f2014-12-18 21:17:1546 base::Process process_;
siggic0d0a0e2014-11-17 23:29:1947 base::Time process_creation_time_;
48
siggid497a1a2014-12-19 15:20:5149 // The exit code of the watched process. Valid after WaitForExit.
50 int exit_code_;
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_