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

Side by Side Diff: mojo/edk/embedder/platform_shared_buffer.cc

Issue 2843113002: make base::SharedMemoryHandle a class on POSIX. (Closed)
Patch Set: Fix test error. Created 3 years, 7 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
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "mojo/edk/embedder/platform_shared_buffer.h" 5 #include "mojo/edk/embedder/platform_shared_buffer.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <utility> 9 #include <utility>
10 10
(...skipping 11 matching lines...) Expand all
22 #endif 22 #endif
23 23
24 namespace mojo { 24 namespace mojo {
25 namespace edk { 25 namespace edk {
26 26
27 namespace { 27 namespace {
28 28
29 // Takes ownership of |memory_handle|. 29 // Takes ownership of |memory_handle|.
30 ScopedPlatformHandle SharedMemoryToPlatformHandle( 30 ScopedPlatformHandle SharedMemoryToPlatformHandle(
31 base::SharedMemoryHandle memory_handle) { 31 base::SharedMemoryHandle memory_handle) {
32 #if defined(OS_POSIX) && !(defined(OS_MACOSX) && !defined(OS_IOS)) 32 #if defined(OS_MACOSX) && !defined(OS_IOS)
33 return ScopedPlatformHandle(PlatformHandle(memory_handle.fd)); 33 return ScopedPlatformHandle(PlatformHandle(memory_handle.GetMemoryObject()));
34 #elif defined(OS_WIN) 34 #else
35 return ScopedPlatformHandle(PlatformHandle(memory_handle.GetHandle())); 35 return ScopedPlatformHandle(PlatformHandle(memory_handle.GetHandle()));
36 #else
37 return ScopedPlatformHandle(PlatformHandle(memory_handle.GetMemoryObject()));
38 #endif 36 #endif
39 } 37 }
40 38
41 } // namespace 39 } // namespace
42 40
43 // static 41 // static
44 PlatformSharedBuffer* PlatformSharedBuffer::Create(size_t num_bytes) { 42 PlatformSharedBuffer* PlatformSharedBuffer::Create(size_t num_bytes) {
45 DCHECK_GT(num_bytes, 0u); 43 DCHECK_GT(num_bytes, 0u);
46 44
47 PlatformSharedBuffer* rv = new PlatformSharedBuffer(num_bytes, false); 45 PlatformSharedBuffer* rv = new PlatformSharedBuffer(num_bytes, false);
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
141 size_t length) { 139 size_t length) {
142 DCHECK(IsValidMap(offset, length)); 140 DCHECK(IsValidMap(offset, length));
143 DCHECK(shared_memory_); 141 DCHECK(shared_memory_);
144 base::SharedMemoryHandle handle; 142 base::SharedMemoryHandle handle;
145 { 143 {
146 base::AutoLock locker(lock_); 144 base::AutoLock locker(lock_);
147 handle = base::SharedMemory::DuplicateHandle(shared_memory_->handle()); 145 handle = base::SharedMemory::DuplicateHandle(shared_memory_->handle());
148 } 146 }
149 147
150 // TODO(crbug.com/706689): Remove this when the bug is sorted out. 148 // TODO(crbug.com/706689): Remove this when the bug is sorted out.
151 CHECK(handle != base::SharedMemory::NULLHandle()); 149 CHECK(handle.IsValid());
152 150
153 if (handle == base::SharedMemory::NULLHandle()) 151 if (!handle.IsValid())
154 return nullptr; 152 return nullptr;
155 153
156 std::unique_ptr<PlatformSharedBufferMapping> mapping( 154 std::unique_ptr<PlatformSharedBufferMapping> mapping(
157 new PlatformSharedBufferMapping(handle, read_only_, offset, length)); 155 new PlatformSharedBufferMapping(handle, read_only_, offset, length));
158 if (mapping->Map()) 156 if (mapping->Map())
159 return base::WrapUnique(mapping.release()); 157 return base::WrapUnique(mapping.release());
160 158
161 return nullptr; 159 return nullptr;
162 } 160 }
163 161
164 ScopedPlatformHandle PlatformSharedBuffer::DuplicatePlatformHandle() { 162 ScopedPlatformHandle PlatformSharedBuffer::DuplicatePlatformHandle() {
165 DCHECK(shared_memory_); 163 DCHECK(shared_memory_);
166 base::SharedMemoryHandle handle; 164 base::SharedMemoryHandle handle;
167 { 165 {
168 base::AutoLock locker(lock_); 166 base::AutoLock locker(lock_);
169 handle = base::SharedMemory::DuplicateHandle(shared_memory_->handle()); 167 handle = base::SharedMemory::DuplicateHandle(shared_memory_->handle());
170 } 168 }
171 if (handle == base::SharedMemory::NULLHandle()) 169 if (!handle.IsValid())
172 return ScopedPlatformHandle(); 170 return ScopedPlatformHandle();
173 171
174 return SharedMemoryToPlatformHandle(handle); 172 return SharedMemoryToPlatformHandle(handle);
175 } 173 }
176 174
177 ScopedPlatformHandle PlatformSharedBuffer::PassPlatformHandle() { 175 ScopedPlatformHandle PlatformSharedBuffer::PassPlatformHandle() {
178 DCHECK(HasOneRef()); 176 DCHECK(HasOneRef());
179 177
180 // The only way to pass a handle from base::SharedMemory is to duplicate it 178 // The only way to pass a handle from base::SharedMemory is to duplicate it
181 // and close the original. 179 // and close the original.
(...skipping 11 matching lines...) Expand all
193 return base::SharedMemory::DuplicateHandle(shared_memory_->handle()); 191 return base::SharedMemory::DuplicateHandle(shared_memory_->handle());
194 } 192 }
195 193
196 PlatformSharedBuffer* PlatformSharedBuffer::CreateReadOnlyDuplicate() { 194 PlatformSharedBuffer* PlatformSharedBuffer::CreateReadOnlyDuplicate() {
197 DCHECK(shared_memory_); 195 DCHECK(shared_memory_);
198 196
199 if (ro_shared_memory_) { 197 if (ro_shared_memory_) {
200 base::AutoLock locker(lock_); 198 base::AutoLock locker(lock_);
201 base::SharedMemoryHandle handle; 199 base::SharedMemoryHandle handle;
202 handle = base::SharedMemory::DuplicateHandle(ro_shared_memory_->handle()); 200 handle = base::SharedMemory::DuplicateHandle(ro_shared_memory_->handle());
203 if (handle == base::SharedMemory::NULLHandle()) 201 if (!handle.IsValid())
204 return nullptr; 202 return nullptr;
205 return CreateFromSharedMemoryHandle(num_bytes_, true, handle); 203 return CreateFromSharedMemoryHandle(num_bytes_, true, handle);
206 } 204 }
207 205
208 base::SharedMemoryHandle handle; 206 base::SharedMemoryHandle handle;
209 bool success; 207 bool success;
210 { 208 {
211 base::AutoLock locker(lock_); 209 base::AutoLock locker(lock_);
212 success = shared_memory_->ShareReadOnlyToProcess( 210 success = shared_memory_->ShareReadOnlyToProcess(
213 base::GetCurrentProcessHandle(), &handle); 211 base::GetCurrentProcessHandle(), &handle);
214 } 212 }
215 if (!success || handle == base::SharedMemory::NULLHandle()) 213 if (!success || !handle.IsValid())
216 return nullptr; 214 return nullptr;
217 215
218 return CreateFromSharedMemoryHandle(num_bytes_, true, handle); 216 return CreateFromSharedMemoryHandle(num_bytes_, true, handle);
219 } 217 }
220 218
221 PlatformSharedBuffer::PlatformSharedBuffer(size_t num_bytes, bool read_only) 219 PlatformSharedBuffer::PlatformSharedBuffer(size_t num_bytes, bool read_only)
222 : num_bytes_(num_bytes), read_only_(read_only) {} 220 : num_bytes_(num_bytes), read_only_(read_only) {}
223 221
224 PlatformSharedBuffer::~PlatformSharedBuffer() {} 222 PlatformSharedBuffer::~PlatformSharedBuffer() {}
225 223
226 bool PlatformSharedBuffer::Init() { 224 bool PlatformSharedBuffer::Init() {
(...skipping 14 matching lines...) Expand all
241 DCHECK(!shared_memory_); 239 DCHECK(!shared_memory_);
242 240
243 #if defined(OS_WIN) 241 #if defined(OS_WIN)
244 base::SharedMemoryHandle handle(platform_handle.release().handle, 242 base::SharedMemoryHandle handle(platform_handle.release().handle,
245 base::GetCurrentProcId()); 243 base::GetCurrentProcId());
246 #elif defined(OS_MACOSX) && !defined(OS_IOS) 244 #elif defined(OS_MACOSX) && !defined(OS_IOS)
247 base::SharedMemoryHandle handle; 245 base::SharedMemoryHandle handle;
248 handle = base::SharedMemoryHandle(platform_handle.release().port, num_bytes_, 246 handle = base::SharedMemoryHandle(platform_handle.release().port, num_bytes_,
249 base::GetCurrentProcId()); 247 base::GetCurrentProcId());
250 #else 248 #else
251 base::SharedMemoryHandle handle(platform_handle.release().handle, false); 249 base::SharedMemoryHandle handle(
250 base::FileDescriptor(platform_handle.release().handle, false));
252 #endif 251 #endif
253 252
254 shared_memory_.reset(new base::SharedMemory(handle, read_only_)); 253 shared_memory_.reset(new base::SharedMemory(handle, read_only_));
255 return true; 254 return true;
256 } 255 }
257 256
258 bool PlatformSharedBuffer::InitFromPlatformHandlePair( 257 bool PlatformSharedBuffer::InitFromPlatformHandlePair(
259 ScopedPlatformHandle rw_platform_handle, 258 ScopedPlatformHandle rw_platform_handle,
260 ScopedPlatformHandle ro_platform_handle) { 259 ScopedPlatformHandle ro_platform_handle) {
261 #if defined(OS_MACOSX) 260 #if defined(OS_MACOSX)
262 NOTREACHED(); 261 NOTREACHED();
263 return false; 262 return false;
264 #else // defined(OS_MACOSX) 263 #else // defined(OS_MACOSX)
265 264
266 #if defined(OS_WIN) 265 #if defined(OS_WIN)
267 base::SharedMemoryHandle handle(rw_platform_handle.release().handle, 266 base::SharedMemoryHandle handle(rw_platform_handle.release().handle,
268 base::GetCurrentProcId()); 267 base::GetCurrentProcId());
269 base::SharedMemoryHandle ro_handle(ro_platform_handle.release().handle, 268 base::SharedMemoryHandle ro_handle(ro_platform_handle.release().handle,
270 base::GetCurrentProcId()); 269 base::GetCurrentProcId());
271 #else // defined(OS_WIN) 270 #else // defined(OS_WIN)
272 base::SharedMemoryHandle handle(rw_platform_handle.release().handle, false); 271 base::SharedMemoryHandle handle(
273 base::SharedMemoryHandle ro_handle(ro_platform_handle.release().handle, 272 base::FileDescriptor(rw_platform_handle.release().handle, false));
274 false); 273 base::SharedMemoryHandle ro_handle(
274 base::FileDescriptor(ro_platform_handle.release().handle, false));
275 #endif // defined(OS_WIN) 275 #endif // defined(OS_WIN)
276 276
277 DCHECK(!shared_memory_); 277 DCHECK(!shared_memory_);
278 shared_memory_.reset(new base::SharedMemory(handle, false)); 278 shared_memory_.reset(new base::SharedMemory(handle, false));
279 ro_shared_memory_.reset(new base::SharedMemory(ro_handle, true)); 279 ro_shared_memory_.reset(new base::SharedMemory(ro_handle, true));
280 return true; 280 return true;
281 281
282 #endif // defined(OS_MACOSX) 282 #endif // defined(OS_MACOSX)
283 } 283 }
284 284
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
332 base_ = static_cast<char*>(shared_memory_.memory()) + offset_rounding; 332 base_ = static_cast<char*>(shared_memory_.memory()) + offset_rounding;
333 return true; 333 return true;
334 } 334 }
335 335
336 void PlatformSharedBufferMapping::Unmap() { 336 void PlatformSharedBufferMapping::Unmap() {
337 shared_memory_.Unmap(); 337 shared_memory_.Unmap();
338 } 338 }
339 339
340 } // namespace edk 340 } // namespace edk
341 } // namespace mojo 341 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698