blob: 65dfcf78aa67b346711ff1b05a2b77b35f7b2fd3 [file] [log] [blame]
Jakub Pawlowski86aa2252017-04-05 09:22:29 -07001// 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
13namespace mojo {
14namespace core {
15
16namespace {
17
18base::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
30ScopedProcessHandle::ScopedProcessHandle() = default;
31
32ScopedProcessHandle::ScopedProcessHandle(base::ProcessHandle handle)
33 : handle_(handle) {
34 DCHECK_NE(handle, GetCurrentProcessHandle());
35}
36
37ScopedProcessHandle::ScopedProcessHandle(ScopedProcessHandle&&) = default;
38
39ScopedProcessHandle::~ScopedProcessHandle() = default;
40
41// static
42ScopedProcessHandle 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
56ScopedProcessHandle& ScopedProcessHandle::operator=(ScopedProcessHandle&&) =
57 default;
58
59bool ScopedProcessHandle::is_valid() const {
60#if defined(OS_WIN)
61 return handle_.IsValid();
62#else
63 return handle_ != base::kNullProcessHandle;
64#endif
65}
66
67base::ProcessHandle ScopedProcessHandle::get() const {
68#if defined(OS_WIN)
69 return handle_.Get();
70#else
71 return handle_;
72#endif
73}
74
75base::ProcessHandle ScopedProcessHandle::release() {
76#if defined(OS_WIN)
77 return handle_.Take();
78#else
79 return handle_;
80#endif
81}
82
83ScopedProcessHandle ScopedProcessHandle::Clone() const {
84 if (is_valid())
85 return CloneFrom(get());
86 return ScopedProcessHandle();
87}
88
89} // namespace core
90} // namespace mojo