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

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

Issue 1995753002: [mojo-edk] Expose portable API for platform handle wrapping (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 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
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/memory/ptr_util.h" 12 #include "base/memory/ptr_util.h"
13 #include "base/memory/shared_memory.h" 13 #include "base/memory/shared_memory.h"
14 #include "base/process/process_handle.h" 14 #include "base/process/process_handle.h"
15 #include "base/sys_info.h" 15 #include "base/sys_info.h"
16 #include "mojo/edk/embedder/platform_handle_utils.h" 16 #include "mojo/edk/embedder/platform_handle_utils.h"
17 17
18 namespace mojo { 18 namespace mojo {
19 namespace edk { 19 namespace edk {
20 20
21 namespace {
22
23 // Takes ownership of |memory_handle|.
24 ScopedPlatformHandle SharedMemoryToPlatformHandle(
25 base::SharedMemoryHandle memory_handle) {
26 #if defined(OS_POSIX) && !(defined(OS_MACOSX) && !defined(OS_IOS))
27 return ScopedPlatformHandle(PlatformHandle(memory_handle.fd));
28 #elif defined(OS_WIN)
29 return ScopedPlatformHandle(PlatformHandle(memory_handle.GetHandle()));
30 #else
31 return ScopedPlatformHandle(PlatformHandle(memory_handle.GetMemoryObject()));
32 #endif
33 }
34
35 } // namespace
36
37 // static 21 // static
38 PlatformSharedBuffer* PlatformSharedBuffer::Create(size_t num_bytes) { 22 PlatformSharedBuffer* PlatformSharedBuffer::Create(size_t num_bytes) {
39 DCHECK_GT(num_bytes, 0u); 23 DCHECK_GT(num_bytes, 0u);
40 24
41 PlatformSharedBuffer* rv = new PlatformSharedBuffer(num_bytes, false); 25 PlatformSharedBuffer* rv = new PlatformSharedBuffer(num_bytes, false);
42 if (!rv->Init()) { 26 if (!rv->Init()) {
43 // We can't just delete it directly, due to the "in destructor" (debug) 27 // We can't just delete it directly, due to the "in destructor" (debug)
44 // check. 28 // check.
45 scoped_refptr<PlatformSharedBuffer> deleter(rv); 29 scoped_refptr<PlatformSharedBuffer> deleter(rv);
46 return nullptr; 30 return nullptr;
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
94 bool read_only, 78 bool read_only,
95 base::SharedMemoryHandle handle) { 79 base::SharedMemoryHandle handle) {
96 DCHECK_GT(num_bytes, 0u); 80 DCHECK_GT(num_bytes, 0u);
97 81
98 PlatformSharedBuffer* rv = new PlatformSharedBuffer(num_bytes, read_only); 82 PlatformSharedBuffer* rv = new PlatformSharedBuffer(num_bytes, read_only);
99 rv->InitFromSharedMemoryHandle(handle); 83 rv->InitFromSharedMemoryHandle(handle);
100 84
101 return rv; 85 return rv;
102 } 86 }
103 87
88 // static
89 base::SharedMemoryHandle
90 PlatformSharedBuffer::GetSharedMemoryHandleFromPlatformHandle(
91 MojoPlatformHandle platform_handle,
92 size_t size,
93 bool read_only) {
94 #if defined(OS_POSIX) && !(defined(OS_MACOSX) && !defined(OS_IOS))
95 return base::SharedMemoryHandle(static_cast<int>(platform_handle),
96 false);
97 #elif defined(OS_WIN)
98 return base::SharedMemoryHandle(static_cast<HANDLE>(platform_handle),
99 base::GetCurrentProcessHandle());
100 #else
101 return base::SharedMemoryHandle(static_cast<mach_port_t>(platform_handle),
102 static_cast<mach_vm_size_t>(num_bytes),
103 base::GetCurrentProcId());
104 #endif
105 }
106
107 // static
108 PlatformHandle PlatformSharedBuffer::GetPlatformHandleFromSharedMemoryHandle(
109 base::SharedMemoryHandle memory_handle) {
110 #if defined(OS_POSIX) && !(defined(OS_MACOSX) && !defined(OS_IOS))
111 return PlatformHandle(memory_handle.fd);
112 #elif defined(OS_WIN)
113 return PlatformHandle(memory_handle.GetHandle());
114 #else
115 return PlatformHandle(memory_handle.GetMemoryObject());
116 #endif
117 }
118
104 size_t PlatformSharedBuffer::GetNumBytes() const { 119 size_t PlatformSharedBuffer::GetNumBytes() const {
105 return num_bytes_; 120 return num_bytes_;
106 } 121 }
107 122
108 bool PlatformSharedBuffer::IsReadOnly() const { 123 bool PlatformSharedBuffer::IsReadOnly() const {
109 return read_only_; 124 return read_only_;
110 } 125 }
111 126
112 std::unique_ptr<PlatformSharedBufferMapping> PlatformSharedBuffer::Map( 127 std::unique_ptr<PlatformSharedBufferMapping> PlatformSharedBuffer::Map(
113 size_t offset, 128 size_t offset,
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
154 ScopedPlatformHandle PlatformSharedBuffer::DuplicatePlatformHandle() { 169 ScopedPlatformHandle PlatformSharedBuffer::DuplicatePlatformHandle() {
155 DCHECK(shared_memory_); 170 DCHECK(shared_memory_);
156 base::SharedMemoryHandle handle; 171 base::SharedMemoryHandle handle;
157 { 172 {
158 base::AutoLock locker(lock_); 173 base::AutoLock locker(lock_);
159 handle = base::SharedMemory::DuplicateHandle(shared_memory_->handle()); 174 handle = base::SharedMemory::DuplicateHandle(shared_memory_->handle());
160 } 175 }
161 if (handle == base::SharedMemory::NULLHandle()) 176 if (handle == base::SharedMemory::NULLHandle())
162 return ScopedPlatformHandle(); 177 return ScopedPlatformHandle();
163 178
164 return SharedMemoryToPlatformHandle(handle); 179 return ScopedPlatformHandle(GetPlatformHandleFromSharedMemoryHandle(handle));
165 } 180 }
166 181
167 ScopedPlatformHandle PlatformSharedBuffer::PassPlatformHandle() { 182 ScopedPlatformHandle PlatformSharedBuffer::PassPlatformHandle() {
168 DCHECK(HasOneRef()); 183 DCHECK(HasOneRef());
169 184
170 // The only way to pass a handle from base::SharedMemory is to duplicate it 185 // The only way to pass a handle from base::SharedMemory is to duplicate it
171 // and close the original. 186 // and close the original.
172 ScopedPlatformHandle handle = DuplicatePlatformHandle(); 187 ScopedPlatformHandle handle = DuplicatePlatformHandle();
173 188
174 base::AutoLock locker(lock_); 189 base::AutoLock locker(lock_);
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
223 options.share_read_only = true; 238 options.share_read_only = true;
224 239
225 shared_memory_.reset(new base::SharedMemory); 240 shared_memory_.reset(new base::SharedMemory);
226 return shared_memory_->Create(options); 241 return shared_memory_->Create(options);
227 } 242 }
228 243
229 bool PlatformSharedBuffer::InitFromPlatformHandle( 244 bool PlatformSharedBuffer::InitFromPlatformHandle(
230 ScopedPlatformHandle platform_handle) { 245 ScopedPlatformHandle platform_handle) {
231 DCHECK(!shared_memory_); 246 DCHECK(!shared_memory_);
232 247
233 #if defined(OS_WIN) 248 #if defined(OS_WIN)
Anand Mistry (off Chromium) 2016/05/20 06:25:40 Please change this to use the new function.
Ken Rockot(use gerrit already) 2016/05/20 22:19:43 No longer relevant since I removed the new functio
234 base::SharedMemoryHandle handle(platform_handle.release().handle, 249 base::SharedMemoryHandle handle(platform_handle.release().handle,
235 base::GetCurrentProcId()); 250 base::GetCurrentProcId());
236 #elif defined(OS_MACOSX) && !defined(OS_IOS) 251 #elif defined(OS_MACOSX) && !defined(OS_IOS)
237 base::SharedMemoryHandle handle; 252 base::SharedMemoryHandle handle;
238 handle = base::SharedMemoryHandle(platform_handle.release().port, num_bytes_, 253 handle = base::SharedMemoryHandle(platform_handle.release().port, num_bytes_,
239 base::GetCurrentProcId()); 254 base::GetCurrentProcId());
240 #else 255 #else
241 base::SharedMemoryHandle handle(platform_handle.release().handle, false); 256 base::SharedMemoryHandle handle(platform_handle.release().handle, false);
242 #endif 257 #endif
243 258
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
298 base_ = static_cast<char*>(shared_memory_.memory()) + offset_rounding; 313 base_ = static_cast<char*>(shared_memory_.memory()) + offset_rounding;
299 return true; 314 return true;
300 } 315 }
301 316
302 void PlatformSharedBufferMapping::Unmap() { 317 void PlatformSharedBufferMapping::Unmap() {
303 shared_memory_.Unmap(); 318 shared_memory_.Unmap();
304 } 319 }
305 320
306 } // namespace edk 321 } // namespace edk
307 } // namespace mojo 322 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698