blob: 672a52742852e43c0e3401655688103e4698c47c [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
13namespace base {
14class CommandLine;
15}
16
17namespace browser_watcher {
18
19// Watches for the exit code of a process and records it in a given registry
20// location.
21class ExitCodeWatcher {
22 public:
23 // Name of the switch used for the parent process handle.
24 static const char kParenthHandleSwitch[];
25
26 // Initialize the watcher with a registry path.
27 explicit ExitCodeWatcher(const base::char16* registry_path);
28 ~ExitCodeWatcher();
29
30 // Initializes from arguments on |cmd_line|, returns true on success.
31 // This function expects the process handle indicated by kParentHandleSwitch
32 // in |cmd_line| to be open with sufficient privilege to wait and retrieve
33 // the process exit code.
34 // It checks the handle for validity and takes ownership of it.
35 // The intent is for this handle to be inherited into the watcher process
36 // hosting the instance of this class.
37 bool ParseArguments(const base::CommandLine& cmd_line);
38
39 // Waits for the process to exit and records its exit code in registry.
40 // This is a blocking call.
41 void WaitForExit();
42
siggi581938f2014-12-18 21:17:1543 const base::Process& process() const { return process_; }
siggid497a1a2014-12-19 15:20:5144 int exit_code() const { return exit_code_; }
siggic0d0a0e2014-11-17 23:29:1945
46 private:
47 // Writes |exit_code| to registry, returns true on success.
48 bool WriteProcessExitCode(int exit_code);
49
50 // The registry path the exit codes are written to.
51 base::string16 registry_path_;
52
siggid497a1a2014-12-19 15:20:5153 // Watched process and its creation time.
siggi581938f2014-12-18 21:17:1554 base::Process process_;
siggic0d0a0e2014-11-17 23:29:1955 base::Time process_creation_time_;
56
siggid497a1a2014-12-19 15:20:5157 // The exit code of the watched process. Valid after WaitForExit.
58 int exit_code_;
59
siggic0d0a0e2014-11-17 23:29:1960 DISALLOW_COPY_AND_ASSIGN(ExitCodeWatcher);
61};
62
63} // namespace browser_watcher
64
siggi420541c2014-11-18 23:16:3265#endif // COMPONENTS_BROWSER_WATCHER_EXIT_CODE_WATCHER_WIN_H_