| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "base/memory/shared_memory.h" | 5 #include "base/memory/shared_memory.h" |
| 6 | 6 |
| 7 #include <aclapi.h> | 7 #include <aclapi.h> |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 #include <stdint.h> | 9 #include <stdint.h> |
| 10 | 10 |
| (...skipping 320 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 331 | 331 |
| 332 bool SharedMemory::Unmap() { | 332 bool SharedMemory::Unmap() { |
| 333 if (memory_ == NULL) | 333 if (memory_ == NULL) |
| 334 return false; | 334 return false; |
| 335 | 335 |
| 336 UnmapViewOfFile(memory_); | 336 UnmapViewOfFile(memory_); |
| 337 memory_ = NULL; | 337 memory_ = NULL; |
| 338 return true; | 338 return true; |
| 339 } | 339 } |
| 340 | 340 |
| 341 SharedMemoryHandle SharedMemory::GetReadOnlyHandle() { |
| 342 HANDLE result; |
| 343 ProcessHandle process = GetCurrentProcess(); |
| 344 if (!::DuplicateHandle(process, mapped_file_.Get(), process, &result, |
| 345 FILE_MAP_READ | SECTION_QUERY, FALSE, 0)) { |
| 346 return SharedMemoryHandle(); |
| 347 } |
| 348 SharedMemoryHandle handle = |
| 349 SharedMemoryHandle(result, base::GetProcId(process)); |
| 350 handle.SetOwnershipPassesToIPC(true); |
| 351 return handle; |
| 352 } |
| 353 |
| 341 bool SharedMemory::ShareToProcessCommon(ProcessHandle process, | 354 bool SharedMemory::ShareToProcessCommon(ProcessHandle process, |
| 342 SharedMemoryHandle* new_handle, | 355 SharedMemoryHandle* new_handle, |
| 343 bool close_self, | 356 bool close_self) { |
| 344 ShareMode share_mode) { | |
| 345 *new_handle = SharedMemoryHandle(); | 357 *new_handle = SharedMemoryHandle(); |
| 346 DWORD access = FILE_MAP_READ | SECTION_QUERY; | 358 DWORD access = FILE_MAP_READ | SECTION_QUERY; |
| 347 DWORD options = 0; | 359 DWORD options = 0; |
| 348 HANDLE mapped_file = mapped_file_.Get(); | 360 HANDLE mapped_file = mapped_file_.Get(); |
| 349 HANDLE result; | 361 HANDLE result; |
| 350 if (share_mode == SHARE_CURRENT_MODE && !read_only_) | 362 if (!read_only_) |
| 351 access |= FILE_MAP_WRITE; | 363 access |= FILE_MAP_WRITE; |
| 352 if (close_self) { | 364 if (close_self) { |
| 353 // DUPLICATE_CLOSE_SOURCE causes DuplicateHandle to close mapped_file. | 365 // DUPLICATE_CLOSE_SOURCE causes DuplicateHandle to close mapped_file. |
| 354 options = DUPLICATE_CLOSE_SOURCE; | 366 options = DUPLICATE_CLOSE_SOURCE; |
| 355 HANDLE detached_handle = mapped_file_.Take(); | 367 HANDLE detached_handle = mapped_file_.Take(); |
| 356 DCHECK_EQ(detached_handle, mapped_file); | 368 DCHECK_EQ(detached_handle, mapped_file); |
| 357 Unmap(); | 369 Unmap(); |
| 358 } | 370 } |
| 359 | 371 |
| 360 if (process == GetCurrentProcess() && close_self) { | 372 if (process == GetCurrentProcess() && close_self) { |
| (...skipping 21 matching lines...) Expand all Loading... |
| 382 | 394 |
| 383 SharedMemoryHandle SharedMemory::TakeHandle() { | 395 SharedMemoryHandle SharedMemory::TakeHandle() { |
| 384 SharedMemoryHandle handle(mapped_file_.Take(), base::GetCurrentProcId()); | 396 SharedMemoryHandle handle(mapped_file_.Take(), base::GetCurrentProcId()); |
| 385 handle.SetOwnershipPassesToIPC(true); | 397 handle.SetOwnershipPassesToIPC(true); |
| 386 memory_ = nullptr; | 398 memory_ = nullptr; |
| 387 mapped_size_ = 0; | 399 mapped_size_ = 0; |
| 388 return handle; | 400 return handle; |
| 389 } | 401 } |
| 390 | 402 |
| 391 } // namespace base | 403 } // namespace base |
| OLD | NEW |