• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2018 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 MOJO_CORE_SCOPED_PROCESS_HANDLE_H_
6 #define MOJO_CORE_SCOPED_PROCESS_HANDLE_H_
7 
8 #include "base/macros.h"
9 #include "base/process/process_handle.h"
10 #include "build/build_config.h"
11 
12 #if defined(OS_WIN)
13 #include "base/win/scoped_handle.h"
14 #endif
15 
16 namespace mojo {
17 namespace core {
18 
19 // Wraps a |base::ProcessHandle| with additional scoped lifetime semantics on
20 // applicable platforms. For platforms where process handles aren't ownable
21 // references, this is just a wrapper around |base::ProcessHandle|.
22 //
23 // This essentially exists to support passing around process handles internally
24 // in a generic way while also supporting Windows process handle ownership
25 // semantics.
26 //
27 // A ScopedProcessHandle will never refer to the current process, and
28 // constructing a ScopedProcessHandle over the current process's handle is
29 // considered an error.
30 class ScopedProcessHandle {
31  public:
32   ScopedProcessHandle();
33 
34   // Assumes ownership of |handle|.
35   explicit ScopedProcessHandle(base::ProcessHandle handle);
36 
37   ScopedProcessHandle(ScopedProcessHandle&&);
38 
39   ~ScopedProcessHandle();
40 
41   // Creates a new ScopedProcessHandle from a clone of |handle|.
42   static ScopedProcessHandle CloneFrom(base::ProcessHandle handle);
43 
44   ScopedProcessHandle& operator=(ScopedProcessHandle&&);
45 
46   bool is_valid() const;
47   base::ProcessHandle get() const;
48   base::ProcessHandle release();
49 
50   ScopedProcessHandle Clone() const;
51 
52  private:
53 #if defined(OS_WIN)
54   base::win::ScopedHandle handle_;
55 #else
56   base::ProcessHandle handle_ = base::kNullProcessHandle;
57 #endif
58 
59   DISALLOW_COPY_AND_ASSIGN(ScopedProcessHandle);
60 };
61 
62 }  // namespace core
63 }  // namespace mojo
64 
65 #endif  // MOJO_CORE_SCOPED_PROCESS_HANDLE_H_
66