OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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_handle.h" | 5 #include "base/memory/shared_memory_handle.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 | 8 |
9 namespace base { | 9 namespace base { |
10 | 10 |
11 SharedMemoryHandle::SharedMemoryHandle() | 11 SharedMemoryHandle::SharedMemoryHandle() |
12 : handle_(nullptr), pid_(kNullProcessId), ownership_passes_to_ipc_(false) {} | 12 : handle_(nullptr), ownership_passes_to_ipc_(false) {} |
13 | 13 |
14 SharedMemoryHandle::SharedMemoryHandle(HANDLE h, base::ProcessId pid) | 14 SharedMemoryHandle::SharedMemoryHandle(HANDLE h) |
15 : handle_(h), pid_(pid), ownership_passes_to_ipc_(false) {} | 15 : handle_(h), ownership_passes_to_ipc_(false) {} |
16 | 16 |
17 SharedMemoryHandle::SharedMemoryHandle(const SharedMemoryHandle& handle) | 17 SharedMemoryHandle::SharedMemoryHandle(const SharedMemoryHandle& handle) |
18 : handle_(handle.handle_), | 18 : handle_(handle.handle_), |
19 pid_(handle.pid_), | |
20 ownership_passes_to_ipc_(handle.ownership_passes_to_ipc_) {} | 19 ownership_passes_to_ipc_(handle.ownership_passes_to_ipc_) {} |
21 | 20 |
22 SharedMemoryHandle& SharedMemoryHandle::operator=( | 21 SharedMemoryHandle& SharedMemoryHandle::operator=( |
23 const SharedMemoryHandle& handle) { | 22 const SharedMemoryHandle& handle) { |
24 if (this == &handle) | 23 if (this == &handle) |
25 return *this; | 24 return *this; |
26 | 25 |
27 handle_ = handle.handle_; | 26 handle_ = handle.handle_; |
28 pid_ = handle.pid_; | |
29 ownership_passes_to_ipc_ = handle.ownership_passes_to_ipc_; | 27 ownership_passes_to_ipc_ = handle.ownership_passes_to_ipc_; |
30 return *this; | 28 return *this; |
31 } | 29 } |
32 | 30 |
33 void SharedMemoryHandle::Close() const { | 31 void SharedMemoryHandle::Close() const { |
34 DCHECK(handle_ != nullptr); | 32 DCHECK(handle_ != nullptr); |
35 DCHECK(BelongsToCurrentProcess()); | |
36 ::CloseHandle(handle_); | 33 ::CloseHandle(handle_); |
37 } | 34 } |
38 | 35 |
39 bool SharedMemoryHandle::IsValid() const { | 36 bool SharedMemoryHandle::IsValid() const { |
40 return handle_ != nullptr; | 37 return handle_ != nullptr; |
41 } | 38 } |
42 | 39 |
43 SharedMemoryHandle SharedMemoryHandle::Duplicate() const { | 40 SharedMemoryHandle SharedMemoryHandle::Duplicate() const { |
44 DCHECK(BelongsToCurrentProcess()); | |
45 HANDLE duped_handle; | 41 HANDLE duped_handle; |
46 ProcessHandle process = GetCurrentProcess(); | 42 ProcessHandle process = GetCurrentProcess(); |
47 BOOL success = ::DuplicateHandle(process, handle_, process, &duped_handle, 0, | 43 BOOL success = ::DuplicateHandle(process, handle_, process, &duped_handle, 0, |
48 FALSE, DUPLICATE_SAME_ACCESS); | 44 FALSE, DUPLICATE_SAME_ACCESS); |
49 if (!success) | 45 if (!success) |
50 return SharedMemoryHandle(); | 46 return SharedMemoryHandle(); |
51 | 47 |
52 base::SharedMemoryHandle handle(duped_handle, GetCurrentProcId()); | 48 base::SharedMemoryHandle handle(duped_handle); |
53 handle.SetOwnershipPassesToIPC(true); | 49 handle.SetOwnershipPassesToIPC(true); |
54 return handle; | 50 return handle; |
55 } | 51 } |
56 | 52 |
57 bool SharedMemoryHandle::BelongsToCurrentProcess() const { | |
58 return pid_ == base::GetCurrentProcId(); | |
59 } | |
60 | |
61 bool SharedMemoryHandle::NeedsBrokering() const { | |
62 return BelongsToCurrentProcess(); | |
63 } | |
64 | |
65 HANDLE SharedMemoryHandle::GetHandle() const { | 53 HANDLE SharedMemoryHandle::GetHandle() const { |
66 return handle_; | 54 return handle_; |
67 } | 55 } |
68 | 56 |
69 base::ProcessId SharedMemoryHandle::GetPID() const { | |
70 return pid_; | |
71 } | |
72 | |
73 void SharedMemoryHandle::SetOwnershipPassesToIPC(bool ownership_passes) { | 57 void SharedMemoryHandle::SetOwnershipPassesToIPC(bool ownership_passes) { |
74 ownership_passes_to_ipc_ = ownership_passes; | 58 ownership_passes_to_ipc_ = ownership_passes; |
75 } | 59 } |
76 | 60 |
77 bool SharedMemoryHandle::OwnershipPassesToIPC() const { | 61 bool SharedMemoryHandle::OwnershipPassesToIPC() const { |
78 return ownership_passes_to_ipc_; | 62 return ownership_passes_to_ipc_; |
79 } | 63 } |
80 | 64 |
81 } // namespace base | 65 } // namespace base |
OLD | NEW |