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 #ifndef BASE_MEMORY_SHARED_MEMORY_HANDLE_H_ | 5 #ifndef BASE_MEMORY_SHARED_MEMORY_HANDLE_H_ |
6 #define BASE_MEMORY_SHARED_MEMORY_HANDLE_H_ | 6 #define BASE_MEMORY_SHARED_MEMORY_HANDLE_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 | 9 |
10 #include "build/build_config.h" | 10 #include "build/build_config.h" |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
76 // common for existing code to make shallow copies of SharedMemoryHandle, and | 76 // common for existing code to make shallow copies of SharedMemoryHandle, and |
77 // the one that is finally passed into a base::SharedMemory is the one that | 77 // the one that is finally passed into a base::SharedMemory is the one that |
78 // "consumes" the fd. | 78 // "consumes" the fd. |
79 explicit SharedMemoryHandle(const base::FileDescriptor& file_descriptor); | 79 explicit SharedMemoryHandle(const base::FileDescriptor& file_descriptor); |
80 | 80 |
81 // Makes a Mach-based SharedMemoryHandle of the given size. On error, | 81 // Makes a Mach-based SharedMemoryHandle of the given size. On error, |
82 // subsequent calls to IsValid() return false. | 82 // subsequent calls to IsValid() return false. |
83 explicit SharedMemoryHandle(mach_vm_size_t size); | 83 explicit SharedMemoryHandle(mach_vm_size_t size); |
84 | 84 |
85 // Makes a Mach-based SharedMemoryHandle from |memory_object|, a named entry | 85 // Makes a Mach-based SharedMemoryHandle from |memory_object|, a named entry |
86 // in the task with process id |pid|. The memory region has size |size|. | 86 // in the current task. The memory region has size |size|. |
87 SharedMemoryHandle(mach_port_t memory_object, | 87 SharedMemoryHandle(mach_port_t memory_object, mach_vm_size_t size); |
88 mach_vm_size_t size, | |
89 base::ProcessId pid); | |
90 | 88 |
91 // Exposed so that the SharedMemoryHandle can be transported between | 89 // Exposed so that the SharedMemoryHandle can be transported between |
92 // processes. | 90 // processes. |
93 mach_port_t GetMemoryObject() const; | 91 mach_port_t GetMemoryObject() const; |
94 | 92 |
95 // Returns false on a failure to determine the size. On success, populates the | 93 // Returns false on a failure to determine the size. On success, populates the |
96 // output variable |size|. | 94 // output variable |size|. |
97 bool GetSize(size_t* size) const; | 95 bool GetSize(size_t* size) const; |
98 | 96 |
99 // The SharedMemoryHandle must be valid. | 97 // The SharedMemoryHandle must be valid. |
100 // Returns whether the SharedMemoryHandle was successfully mapped into memory. | 98 // Returns whether the SharedMemoryHandle was successfully mapped into memory. |
101 // On success, |memory| is an output variable that contains the start of the | 99 // On success, |memory| is an output variable that contains the start of the |
102 // mapped memory. | 100 // mapped memory. |
103 bool MapAt(off_t offset, size_t bytes, void** memory, bool read_only); | 101 bool MapAt(off_t offset, size_t bytes, void** memory, bool read_only); |
104 #elif defined(OS_WIN) | 102 #elif defined(OS_WIN) |
105 SharedMemoryHandle(HANDLE h, base::ProcessId pid); | 103 SharedMemoryHandle(HANDLE h); |
106 | |
107 // Whether |pid_| is the same as the current process's id. | |
108 bool BelongsToCurrentProcess() const; | |
109 | |
110 // Whether handle_ needs to be duplicated into the destination process when | |
111 // an instance of this class is passed over a Chrome IPC channel. | |
112 bool NeedsBrokering() const; | |
113 | 104 |
114 HANDLE GetHandle() const; | 105 HANDLE GetHandle() const; |
115 base::ProcessId GetPID() const; | |
116 #else | 106 #else |
117 // This constructor is deprecated, as it fails to propagate the GUID, which | 107 // This constructor is deprecated, as it fails to propagate the GUID, which |
118 // will be added in the near future. | 108 // will be added in the near future. |
119 // TODO(rockot): Remove this constructor once Mojo supports GUIDs. | 109 // TODO(rockot): Remove this constructor once Mojo supports GUIDs. |
120 // https://crbug.com/713763. | 110 // https://crbug.com/713763. |
121 explicit SharedMemoryHandle(const base::FileDescriptor& file_descriptor); | 111 explicit SharedMemoryHandle(const base::FileDescriptor& file_descriptor); |
122 | 112 |
123 // Creates a SharedMemoryHandle from an |fd| supplied from an external | 113 // Creates a SharedMemoryHandle from an |fd| supplied from an external |
124 // service. | 114 // service. |
125 static SharedMemoryHandle ImportHandle(int fd); | 115 static SharedMemoryHandle ImportHandle(int fd); |
(...skipping 23 matching lines...) Expand all Loading... |
149 union { | 139 union { |
150 FileDescriptor file_descriptor_; | 140 FileDescriptor file_descriptor_; |
151 | 141 |
152 struct { | 142 struct { |
153 mach_port_t memory_object_; | 143 mach_port_t memory_object_; |
154 | 144 |
155 // The size of the shared memory region when |type_| is MACH. Only | 145 // The size of the shared memory region when |type_| is MACH. Only |
156 // relevant if |memory_object_| is not |MACH_PORT_NULL|. | 146 // relevant if |memory_object_| is not |MACH_PORT_NULL|. |
157 mach_vm_size_t size_; | 147 mach_vm_size_t size_; |
158 | 148 |
159 // The pid of the process in which |memory_object_| is usable. Only | |
160 // relevant if |memory_object_| is not |MACH_PORT_NULL|. | |
161 base::ProcessId pid_; | |
162 | |
163 // Whether passing this object as a parameter to an IPC message passes | 149 // Whether passing this object as a parameter to an IPC message passes |
164 // ownership of |memory_object_| to the IPC stack. This is meant to mimic | 150 // ownership of |memory_object_| to the IPC stack. This is meant to mimic |
165 // the behavior of the |auto_close| parameter of FileDescriptor. | 151 // the behavior of the |auto_close| parameter of FileDescriptor. |
166 // Defaults to |false|. | 152 // Defaults to |false|. |
167 bool ownership_passes_to_ipc_; | 153 bool ownership_passes_to_ipc_; |
168 }; | 154 }; |
169 }; | 155 }; |
170 #elif defined(OS_WIN) | 156 #elif defined(OS_WIN) |
171 HANDLE handle_; | 157 HANDLE handle_; |
172 | 158 |
173 // The process in which |handle_| is valid and can be used. If |handle_| is | |
174 // invalid, this will be kNullProcessId. | |
175 base::ProcessId pid_; | |
176 | |
177 // Whether passing this object as a parameter to an IPC message passes | 159 // Whether passing this object as a parameter to an IPC message passes |
178 // ownership of |handle_| to the IPC stack. This is meant to mimic the | 160 // ownership of |handle_| to the IPC stack. This is meant to mimic the |
179 // behavior of the |auto_close| parameter of FileDescriptor. This member only | 161 // behavior of the |auto_close| parameter of FileDescriptor. This member only |
180 // affects attachment-brokered SharedMemoryHandles. | 162 // affects attachment-brokered SharedMemoryHandles. |
181 // Defaults to |false|. | 163 // Defaults to |false|. |
182 bool ownership_passes_to_ipc_; | 164 bool ownership_passes_to_ipc_; |
183 #else | 165 #else |
184 FileDescriptor file_descriptor_; | 166 FileDescriptor file_descriptor_; |
185 #endif | 167 #endif |
186 }; | 168 }; |
187 | 169 |
188 } // namespace base | 170 } // namespace base |
189 | 171 |
190 #endif // BASE_MEMORY_SHARED_MEMORY_HANDLE_H_ | 172 #endif // BASE_MEMORY_SHARED_MEMORY_HANDLE_H_ |
OLD | NEW |