| OLD | NEW |
| 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/simple_platform_shared_buffer.h" | 5 #include "mojo/edk/embedder/simple_platform_shared_buffer.h" |
| 6 | 6 |
| 7 #include <windows.h> | 7 #include <windows.h> |
| 8 | 8 |
| 9 #include <limits> | 9 #include <limits> |
| 10 | 10 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 static_cast<uint64_t>(std::numeric_limits<DWORD>::max())) { | 26 static_cast<uint64_t>(std::numeric_limits<DWORD>::max())) { |
| 27 return false; | 27 return false; |
| 28 } | 28 } |
| 29 | 29 |
| 30 // IMPORTANT NOTE: Unnamed objects are NOT SECURABLE. Thus if we ever want to | 30 // IMPORTANT NOTE: Unnamed objects are NOT SECURABLE. Thus if we ever want to |
| 31 // share read-only to other processes, we'll have to name our file mapping | 31 // share read-only to other processes, we'll have to name our file mapping |
| 32 // object. | 32 // object. |
| 33 // TODO(vtl): Unlike |base::SharedMemory|, we don't round up the size (to a | 33 // TODO(vtl): Unlike |base::SharedMemory|, we don't round up the size (to a |
| 34 // multiple of 64 KB). This may cause problems with NaCl. Cross this bridge | 34 // multiple of 64 KB). This may cause problems with NaCl. Cross this bridge |
| 35 // when we get there. crbug.com/210609 | 35 // when we get there. crbug.com/210609 |
| 36 handle_.reset(PlatformHandle(CreateFileMapping(INVALID_HANDLE_VALUE, | 36 handle_.reset(PlatformHandle( |
| 37 nullptr, | 37 CreateFileMapping(INVALID_HANDLE_VALUE, nullptr, PAGE_READWRITE, 0, |
| 38 PAGE_READWRITE, | 38 static_cast<DWORD>(num_bytes_), nullptr))); |
| 39 0, | |
| 40 static_cast<DWORD>(num_bytes_), | |
| 41 nullptr))); | |
| 42 if (!handle_.is_valid()) { | 39 if (!handle_.is_valid()) { |
| 43 PLOG(ERROR) << "CreateFileMapping"; | 40 PLOG(ERROR) << "CreateFileMapping"; |
| 44 return false; | 41 return false; |
| 45 } | 42 } |
| 46 | 43 |
| 47 return true; | 44 return true; |
| 48 } | 45 } |
| 49 | 46 |
| 50 bool SimplePlatformSharedBuffer::InitFromPlatformHandle( | 47 bool SimplePlatformSharedBuffer::InitFromPlatformHandle( |
| 51 ScopedPlatformHandle platform_handle) { | 48 ScopedPlatformHandle platform_handle) { |
| 52 DCHECK(!handle_.is_valid()); | 49 DCHECK(!handle_.is_valid()); |
| 53 | 50 |
| 54 // TODO(vtl): Implement. | 51 // TODO(vtl): Implement. |
| 55 NOTIMPLEMENTED(); | 52 NOTIMPLEMENTED(); |
| 56 return false; | 53 return false; |
| 57 } | 54 } |
| 58 | 55 |
| 59 scoped_ptr<PlatformSharedBufferMapping> SimplePlatformSharedBuffer::MapImpl( | 56 scoped_ptr<PlatformSharedBufferMapping> SimplePlatformSharedBuffer::MapImpl( |
| 60 size_t offset, | 57 size_t offset, |
| 61 size_t length) { | 58 size_t length) { |
| 62 size_t offset_rounding = offset % base::SysInfo::VMAllocationGranularity(); | 59 size_t offset_rounding = offset % base::SysInfo::VMAllocationGranularity(); |
| 63 size_t real_offset = offset - offset_rounding; | 60 size_t real_offset = offset - offset_rounding; |
| 64 size_t real_length = length + offset_rounding; | 61 size_t real_length = length + offset_rounding; |
| 65 | 62 |
| 66 // This should hold (since we checked |num_bytes| versus the maximum value of | 63 // This should hold (since we checked |num_bytes| versus the maximum value of |
| 67 // |off_t| on creation, but it never hurts to be paranoid. | 64 // |off_t| on creation, but it never hurts to be paranoid. |
| 68 DCHECK_LE(static_cast<uint64_t>(real_offset), | 65 DCHECK_LE(static_cast<uint64_t>(real_offset), |
| 69 static_cast<uint64_t>(std::numeric_limits<DWORD>::max())); | 66 static_cast<uint64_t>(std::numeric_limits<DWORD>::max())); |
| 70 | 67 |
| 71 void* real_base = MapViewOfFile(handle_.get().handle, | 68 void* real_base = |
| 72 FILE_MAP_READ | FILE_MAP_WRITE, | 69 MapViewOfFile(handle_.get().handle, FILE_MAP_READ | FILE_MAP_WRITE, 0, |
| 73 0, | 70 static_cast<DWORD>(real_offset), real_length); |
| 74 static_cast<DWORD>(real_offset), | |
| 75 real_length); | |
| 76 if (!real_base) { | 71 if (!real_base) { |
| 77 PLOG(ERROR) << "MapViewOfFile"; | 72 PLOG(ERROR) << "MapViewOfFile"; |
| 78 return nullptr; | 73 return nullptr; |
| 79 } | 74 } |
| 80 | 75 |
| 81 void* base = static_cast<char*>(real_base) + offset_rounding; | 76 void* base = static_cast<char*>(real_base) + offset_rounding; |
| 82 return make_scoped_ptr(new SimplePlatformSharedBufferMapping( | 77 return make_scoped_ptr(new SimplePlatformSharedBufferMapping( |
| 83 base, length, real_base, real_length)); | 78 base, length, real_base, real_length)); |
| 84 } | 79 } |
| 85 | 80 |
| 86 // SimplePlatformSharedBufferMapping ------------------------------------------- | 81 // SimplePlatformSharedBufferMapping ------------------------------------------- |
| 87 | 82 |
| 88 void SimplePlatformSharedBufferMapping::Unmap() { | 83 void SimplePlatformSharedBufferMapping::Unmap() { |
| 89 BOOL result = UnmapViewOfFile(real_base_); | 84 BOOL result = UnmapViewOfFile(real_base_); |
| 90 PLOG_IF(ERROR, !result) << "UnmapViewOfFile"; | 85 PLOG_IF(ERROR, !result) << "UnmapViewOfFile"; |
| 91 } | 86 } |
| 92 | 87 |
| 93 } // namespace embedder | 88 } // namespace embedder |
| 94 } // namespace mojo | 89 } // namespace mojo |
| OLD | NEW |