• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2011 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 
5 #ifndef BASE_MEMORY_SCOPED_OPEN_PROCESS_H_
6 #define BASE_MEMORY_SCOPED_OPEN_PROCESS_H_
7 #pragma once
8 
9 #include "base/process.h"
10 #include "base/process_util.h"
11 
12 namespace base {
13 
14 // A class that opens a process from its process id and closes it when the
15 // instance goes out of scope.
16 class ScopedOpenProcess {
17  public:
ScopedOpenProcess()18   ScopedOpenProcess() : handle_(kNullProcessHandle) {
19   }
20 
21   // Automatically close the process.
~ScopedOpenProcess()22   ~ScopedOpenProcess() {
23     Close();
24   }
25 
26   // Open a new process by pid. Closes any previously opened process (even if
27   // opening the new one fails).
Open(ProcessId pid)28   bool Open(ProcessId pid) {
29     Close();
30     return OpenProcessHandle(pid, &handle_);
31   }
32 
33   // Close the previously opened process.
Close()34   void Close() {
35     if (handle_ == kNullProcessHandle)
36       return;
37 
38     CloseProcessHandle(handle_);
39     handle_ = kNullProcessHandle;
40   }
41 
handle()42   ProcessHandle handle() const { return handle_; }
43 
44  private:
45   ProcessHandle handle_;
46   DISALLOW_COPY_AND_ASSIGN(ScopedOpenProcess);
47 };
48 }  // namespace base
49 
50 #endif  // BASE_MEMORY_SCOPED_OPEN_PROCESS_H_
51