• 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 #include "mojo/core/scoped_process_handle.h"
6 
7 #include "build/build_config.h"
8 
9 #if defined(OS_WIN)
10 #include <windows.h>
11 #endif
12 
13 namespace mojo {
14 namespace core {
15 
16 namespace {
17 
GetCurrentProcessHandle()18 base::ProcessHandle GetCurrentProcessHandle() {
19 #if defined(OS_NACL_NONSFI)
20   // Doesn't really matter, it's not going to be used for anything interesting
21   // under NaCl.
22   return 1;
23 #else
24   return base::GetCurrentProcessHandle();
25 #endif
26 }
27 
28 }  // namespace
29 
30 ScopedProcessHandle::ScopedProcessHandle() = default;
31 
ScopedProcessHandle(base::ProcessHandle handle)32 ScopedProcessHandle::ScopedProcessHandle(base::ProcessHandle handle)
33     : handle_(handle) {
34   DCHECK_NE(handle, GetCurrentProcessHandle());
35 }
36 
37 ScopedProcessHandle::ScopedProcessHandle(ScopedProcessHandle&&) = default;
38 
39 ScopedProcessHandle::~ScopedProcessHandle() = default;
40 
41 // static
CloneFrom(base::ProcessHandle handle)42 ScopedProcessHandle ScopedProcessHandle::CloneFrom(base::ProcessHandle handle) {
43   DCHECK_NE(handle, GetCurrentProcessHandle());
44   if (handle == base::kNullProcessHandle)
45     return ScopedProcessHandle();
46 
47 #if defined(OS_WIN)
48   BOOL ok = ::DuplicateHandle(GetCurrentProcessHandle(), handle,
49                               GetCurrentProcessHandle(), &handle, 0, FALSE,
50                               DUPLICATE_SAME_ACCESS);
51   DCHECK(ok);
52 #endif
53   return ScopedProcessHandle(handle);
54 }
55 
56 ScopedProcessHandle& ScopedProcessHandle::operator=(ScopedProcessHandle&&) =
57     default;
58 
is_valid() const59 bool ScopedProcessHandle::is_valid() const {
60 #if defined(OS_WIN)
61   return handle_.IsValid();
62 #else
63   return handle_ != base::kNullProcessHandle;
64 #endif
65 }
66 
get() const67 base::ProcessHandle ScopedProcessHandle::get() const {
68 #if defined(OS_WIN)
69   return handle_.Get();
70 #else
71   return handle_;
72 #endif
73 }
74 
release()75 base::ProcessHandle ScopedProcessHandle::release() {
76 #if defined(OS_WIN)
77   return handle_.Take();
78 #else
79   return handle_;
80 #endif
81 }
82 
Clone() const83 ScopedProcessHandle ScopedProcessHandle::Clone() const {
84   if (is_valid())
85     return CloneFrom(get());
86   return ScopedProcessHandle();
87 }
88 
89 }  // namespace core
90 }  // namespace mojo
91