Separate Simple Backend creation from initialization.
[chromium-blink-merge.git] / base / memory / scoped_open_process.h
blob93ba387afbb7257e6e7b7bc7a95b375be385754b
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.
5 #ifndef BASE_MEMORY_SCOPED_OPEN_PROCESS_H_
6 #define BASE_MEMORY_SCOPED_OPEN_PROCESS_H_
8 #include "base/process.h"
9 #include "base/process_util.h"
11 namespace base {
13 // A class that opens a process from its process id and closes it when the
14 // instance goes out of scope.
15 class ScopedOpenProcess {
16 public:
17 ScopedOpenProcess() : handle_(kNullProcessHandle) {
20 // Automatically close the process.
21 ~ScopedOpenProcess() {
22 Close();
25 // Open a new process by pid. Closes any previously opened process (even if
26 // opening the new one fails).
27 bool Open(ProcessId pid) {
28 Close();
29 return OpenProcessHandle(pid, &handle_);
32 // Close the previously opened process.
33 void Close() {
34 if (handle_ == kNullProcessHandle)
35 return;
37 CloseProcessHandle(handle_);
38 handle_ = kNullProcessHandle;
41 ProcessHandle handle() const { return handle_; }
43 private:
44 ProcessHandle handle_;
45 DISALLOW_COPY_AND_ASSIGN(ScopedOpenProcess);
47 } // namespace base
49 #endif // BASE_MEMORY_SCOPED_OPEN_PROCESS_H_