Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(114)

Side by Side Diff: base/files/scoped_platform_handle.cc

Issue 2705743002: Introduce base::ScopedPlatformHandle (Closed)
Patch Set: Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 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 "base/files/scoped_platform_handle.h"
6
7 #include "base/logging.h"
8
9 namespace base {
10
11 ScopedPlatformHandle::ScopedPlatformHandle() : ScopedPlatformHandle(nullptr) {}
12
13 ScopedPlatformHandle::ScopedPlatformHandle(std::nullptr_t)
14 : type_(Type::INVALID) {}
15
16 ScopedPlatformHandle::ScopedPlatformHandle(ScopedPlatformHandle&& other) {
17 *this = std::move(other);
18 }
19
20 #if defined(OS_WIN)
21 ScopedPlatformHandle::ScopedPlatformHandle(HANDLE handle)
22 : type_(Type::WINDOWS_HANDLE), windows_handle_(handle) {}
23
24 ScopedPlatformHandle::ScopedPlatformHandle(win::ScopedHandle handle)
25 : type_(Type::WINDOWS_HANDLE), windows_handle_(std::move(handle)) {}
26
27 #elif defined(OS_POSIX)
28
29 ScopedPlatformHandle::ScopedPlatformHandle(int fd)
30 : type_(Type::FILE_DESCRIPTOR), file_descriptor_(fd) {}
31
32 ScopedPlatformHandle::ScopedPlatformHandle(ScopedFD fd)
33 : type_(Type::FILE_DESCRIPTOR), file_descriptor_(std::move(fd)) {}
34
35 #endif // defined(OS_WIN)
36
37 #if defined(OS_MACOSX) && !defined(OS_IOS)
38
39 ScopedPlatformHandle::ScopedPlatformHandle(mach_port_t port)
40 : type_(Type::MACH_PORT_SEND), mach_port_send_(port) {}
41
42 ScopedPlatformHandle::ScopedPlatformHandle(mac::ScopedMachSendRight port)
43 : type_(Type::MACH_PORT_SEND), mach_port_send_(std::move(port)) {}
44
45 ScopedPlatformHandle::ScopedPlatformHandle(mac::ScopedMachReceiveRight port)
46 : type_(Type::MACH_PORT_RECEIVE), mach_port_receive_(std::move(port)) {}
47
48 #endif // defined(OS_MACOSX) && !defined(OS_IOS)
49
50 ScopedPlatformHandle::~ScopedPlatformHandle() {}
51
52 ScopedPlatformHandle& ScopedPlatformHandle::operator=(
53 ScopedPlatformHandle&& other) {
54 type_ = other.type_;
55 other.type_ = Type::INVALID;
56
57 #if defined(OS_WIN)
58 windows_handle_ = std::move(other.windows_handle_);
59 #elif defined(OS_POSIX)
60 file_descriptor_ = std::move(other.file_descriptor_);
61 #endif
62
63 #if defined(OS_MACOSX) && !defined(OS_IOS)
64 mach_port_send_ = std::move(other.mach_port_send_);
65 mach_port_receive_ = std::move(other.mach_port_receive_);
66 #endif
67
68 return *this;
69 }
70
71 bool ScopedPlatformHandle::is_valid() const {
72 switch (type_) {
73 case Type::INVALID:
74 return false;
75
76 #if defined(OS_WIN)
77 case Type::WINDOWS_HANDLE:
78 return windows_handle_.IsValid();
79 #elif defined(OS_POSIX)
80 case Type::FILE_DESCRIPTOR:
81 return file_descriptor_.is_valid();
82 #endif
83
84 #if defined(OS_MACOSX) && !defined(OS_IOS)
85 case Type::MACH_PORT_SEND:
86 return mach_port_send_.is_valid();
87 case Type::MACH_PORT_RECEIVE:
88 return mach_port_receive_.is_valid();
89 #endif
90 }
91 }
92
93 void ScopedPlatformHandle::reset() {
94 switch (type_) {
95 case Type::INVALID:
96 return;
97
98 #if defined(OS_WIN)
99 case Type::WINDOWS_HANDLE:
100 windows_handle_.Close();
101 break;
102 #elif defined(OS_POSIX)
103 case Type::FILE_DESCRIPTOR:
104 file_descriptor_.reset();
105 break;
106 #endif
107
108 #if defined(OS_MACOSX) && !defined(OS_IOS)
109 case Type::MACH_PORT_SEND:
110 mach_port_send_.reset();
111 break;
112 case Type::MACH_PORT_RECEIVE:
113 mach_port_receive_.reset();
114 break;
115 #endif
116 }
117
118 type_ = Type::INVALID;
119
120 #if defined(OS_WIN)
121 DCHECK(!windows_handle_.IsValid());
122 #elif defined(OS_POSIX)
123 DCHECK(!file_descriptor_.is_valid());
124 #endif
125
126 #if defined(OS_MACOSX) && !defined(OS_IOS)
127 DCHECK(!mach_port_send_.is_valid());
128 DCHECK(!mach_port_receive_.is_valid());
129 #endif
130 }
131
132 #if defined(OS_WIN)
133
134 bool ScopedPlatformHandle::GetAsWindowsHandle(HANDLE* handle) const {
135 if (type_ != Type::WINDOWS_HANDLE)
136 return false;
137 *handle = windows_handle_.Get();
138 return true;
139 }
140
141 bool ScopedPlatformHandle::TakeWindowsHandle(win::ScopedHandle* handle) {
142 if (type_ != Type::WINDOWS_HANDLE)
143 return false;
144 *handle = std::move(windows_handle_);
145 type_ = Type::INVALID;
146 return true;
147 }
148
149 bool ScopedPlatformHandle::ReleaseAsWindowsHandle(HANDLE* handle) {
150 if (type_ != Type::WINDOWS_HANDLE)
151 return false;
152 *handle = windows_handle_.Take();
153 type_ = Type::INVALID;
154 return true;
155 }
156
157 #elif defined(OS_POSIX)
158
159 bool ScopedPlatformHandle::GetAsFileDescriptor(int* fd) const {
160 if (type_ != Type::FILE_DESCRIPTOR)
161 return false;
162 *fd = file_descriptor_.get();
163 return true;
164 }
165
166 bool ScopedPlatformHandle::TakeFileDescriptor(ScopedFD* fd) {
167 if (type_ != Type::FILE_DESCRIPTOR)
168 return false;
169 *fd = std::move(file_descriptor_);
170 type_ = Type::INVALID;
171 return true;
172 }
173
174 bool ScopedPlatformHandle::ReleaseAsFileDescriptor(int* fd) {
175 if (type_ != Type::FILE_DESCRIPTOR)
176 return false;
177 *fd = file_descriptor_.release();
178 type_ = Type::INVALID;
179 return true;
180 }
181
182 #endif // defined(OS_WIN)
183
184 #if defined(OS_MACOSX) && !defined(OS_IOS)
185
186 bool ScopedPlatformHandle::GetAsMachPort(mach_port_t* port) const {
187 if (type_ == Type::MACH_PORT_SEND) {
188 *port = mach_port_send_.get();
189 return true;
190 } else if (type_ == Type::MACH_PORT_RECEIVE) {
191 *port = mach_port_receive_.get();
192 return true;
193 }
194 return false;
195 }
196
197 bool ScopedPlatformHandle::TakeMachPortSendRight(
198 mac::ScopedMachSendRight* port) {
199 if (type_ != Type::MACH_PORT_SEND)
200 return false;
201 *port = std::move(mach_port_send_);
202 type_ = Type::INVALID;
203 return true;
204 }
205
206 bool ScopedPlatformHandle::TakeMachPortReceiveRight(
207 mac::ScopedMachReceiveRight* port) {
208 if (type_ != Type::MACH_PORT_RECEIVE)
209 return false;
210 *port = std::move(mach_port_receive_);
211 type_ = Type::INVALID;
212 return true;
213 }
214
215 bool ScopedPlatformHandle::ReleaseAsMachPort(mach_port_t* port) {
216 if (type_ == Type::MACH_PORT_SEND) {
217 *port = mach_port_send_.release();
218 type_ = Type::INVALID;
219 return true;
220 } else if (type_ == Type::MACH_PORT_RECEIVE) {
221 *port = mach_port_receive_.release();
222 type_ = Type::INVALID;
223 return true;
224 }
225 return false;
226 }
227
228 #endif // defined(OS_MACOSX) && !defined(OS_IOS)
229
230 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698