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

Side by Side Diff: base/memory/shared_memory.h

Issue 2845113005: Replace base::SharedMemory read-only methods with GetReadOnlyHandle. (Closed)
Patch Set: comments from jyasskin Created 3 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
« no previous file with comments | « no previous file | base/memory/shared_memory_android.cc » ('j') | base/memory/shared_memory_win.cc » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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_H_ 5 #ifndef BASE_MEMORY_SHARED_MEMORY_H_
6 #define BASE_MEMORY_SHARED_MEMORY_H_ 6 #define BASE_MEMORY_SHARED_MEMORY_H_
7 7
8 #include <stddef.h> 8 #include <stddef.h>
9 9
10 #include <string> 10 #include <string>
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
73 // Similar to the default constructor, except that this allows for 73 // Similar to the default constructor, except that this allows for
74 // calling LockDeprecated() to acquire the named mutex before either Create or 74 // calling LockDeprecated() to acquire the named mutex before either Create or
75 // Open are called on Windows. 75 // Open are called on Windows.
76 explicit SharedMemory(const std::wstring& name); 76 explicit SharedMemory(const std::wstring& name);
77 #endif 77 #endif
78 78
79 // Create a new SharedMemory object from an existing, open 79 // Create a new SharedMemory object from an existing, open
80 // shared memory file. 80 // shared memory file.
81 // 81 //
82 // WARNING: This does not reduce the OS-level permissions on the handle; it 82 // WARNING: This does not reduce the OS-level permissions on the handle; it
83 // only affects how the SharedMemory will be mmapped. Use 83 // only affects how the SharedMemory will be mmapped. Use
84 // ShareReadOnlyToProcess to drop permissions. TODO(jln,jyasskin): DCHECK 84 // GetReadOnlyHandle to drop permissions. TODO(jln,jyasskin): DCHECK
85 // that |read_only| matches the permissions of the handle. 85 // that |read_only| matches the permissions of the handle.
86 SharedMemory(const SharedMemoryHandle& handle, bool read_only); 86 SharedMemory(const SharedMemoryHandle& handle, bool read_only);
87 87
88 // Closes any open files. 88 // Closes any open files.
89 ~SharedMemory(); 89 ~SharedMemory();
90 90
91 // Return true iff the given handle is valid (i.e. not the distingished 91 // Return true iff the given handle is valid (i.e. not the distingished
92 // invalid value; NULL for a HANDLE and -1 for a file descriptor) 92 // invalid value; NULL for a HANDLE and -1 for a file descriptor)
93 static bool IsHandleValid(const SharedMemoryHandle& handle); 93 static bool IsHandleValid(const SharedMemoryHandle& handle);
94 94
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
199 // SharedMemoryHandle dup = DuplicateHandle(handle()); 199 // SharedMemoryHandle dup = DuplicateHandle(handle());
200 // Close(); 200 // Close();
201 // return dup; 201 // return dup;
202 SharedMemoryHandle TakeHandle(); 202 SharedMemoryHandle TakeHandle();
203 203
204 // Closes the open shared memory segment. The memory will remain mapped if 204 // Closes the open shared memory segment. The memory will remain mapped if
205 // it was previously mapped. 205 // it was previously mapped.
206 // It is safe to call Close repeatedly. 206 // It is safe to call Close repeatedly.
207 void Close(); 207 void Close();
208 208
209 // Shares the shared memory to another process. Attempts to create a 209 // Returns a read-only handle to this shared memory region. The caller takes
210 // platform-specific new_handle which can be used in a remote process to read 210 // ownership of the handle. CHECK-fails if the region wasn't Created or
Nico 2017/05/02 15:48:39 I didn't see this promised CHECK on all platforms.
erikchen 2017/05/02 17:52:18 It's only relevant for POSIX handles. I've updated
211 // the shared memory file. new_handle is an output parameter to receive the 211 // Opened with share_read_only=true. When the handle is passed to the IPC
212 // handle for use in the remote process. 212 // subsystem, that takes ownership of the handle. As such, it's not valid to
213 // 213 // pass the sample handle to the IPC subsystem twice.
Nico 2017/05/02 15:48:39 Say that this will return an invalid handle on fai
erikchen 2017/05/02 17:52:18 Done.
214 // |*this| must have been initialized using one of the Create*() or Open() 214 SharedMemoryHandle GetReadOnlyHandle();
215 // methods with share_read_only=true. If it was constructed from a
216 // SharedMemoryHandle, this call will CHECK-fail.
217 //
218 // Returns true on success, false otherwise.
219 bool ShareReadOnlyToProcess(ProcessHandle process,
220 SharedMemoryHandle* new_handle) {
221 return ShareToProcessCommon(process, new_handle, false, SHARE_READONLY);
222 }
223
224 // Logically equivalent to:
225 // bool ok = ShareReadOnlyToProcess(process, new_handle);
226 // Close();
227 // return ok;
228 // Note that the memory is unmapped by calling this method, regardless of the
229 // return value.
230 bool GiveReadOnlyToProcess(ProcessHandle process,
231 SharedMemoryHandle* new_handle) {
232 return ShareToProcessCommon(process, new_handle, true, SHARE_READONLY);
233 }
234 215
235 // Shares the shared memory to another process. Attempts 216 // Shares the shared memory to another process. Attempts
236 // to create a platform-specific new_handle which can be 217 // to create a platform-specific new_handle which can be
237 // used in a remote process to access the shared memory 218 // used in a remote process to access the shared memory
238 // file. new_handle is an output parameter to receive 219 // file. new_handle is an output parameter to receive
239 // the handle for use in the remote process. 220 // the handle for use in the remote process.
240 // Returns true on success, false otherwise. 221 // Returns true on success, false otherwise.
241 bool ShareToProcess(ProcessHandle process, 222 bool ShareToProcess(ProcessHandle process,
242 SharedMemoryHandle* new_handle) { 223 SharedMemoryHandle* new_handle) {
243 return ShareToProcessCommon(process, new_handle, false, SHARE_CURRENT_MODE); 224 return ShareToProcessCommon(process, new_handle, false);
244 } 225 }
245 226
246 // Logically equivalent to: 227 // Logically equivalent to:
247 // bool ok = ShareToProcess(process, new_handle); 228 // bool ok = ShareToProcess(process, new_handle);
248 // Close(); 229 // Close();
249 // return ok; 230 // return ok;
250 // Note that the memory is unmapped by calling this method, regardless of the 231 // Note that the memory is unmapped by calling this method, regardless of the
251 // return value. 232 // return value.
252 bool GiveToProcess(ProcessHandle process, 233 bool GiveToProcess(ProcessHandle process,
253 SharedMemoryHandle* new_handle) { 234 SharedMemoryHandle* new_handle) {
254 return ShareToProcessCommon(process, new_handle, true, SHARE_CURRENT_MODE); 235 return ShareToProcessCommon(process, new_handle, true);
255 } 236 }
256 237
257 #if defined(OS_POSIX) && (!defined(OS_MACOSX) || defined(OS_IOS)) && \ 238 #if defined(OS_POSIX) && (!defined(OS_MACOSX) || defined(OS_IOS)) && \
258 !defined(OS_NACL) 239 !defined(OS_NACL)
259 using UniqueId = std::pair<dev_t, ino_t>; 240 using UniqueId = std::pair<dev_t, ino_t>;
260 241
261 struct UniqueIdHash { 242 struct UniqueIdHash {
262 size_t operator()(const UniqueId& id) const { 243 size_t operator()(const UniqueId& id) const {
263 return HashInts(id.first, id.second); 244 return HashInts(id.first, id.second);
264 } 245 }
265 }; 246 };
266 247
267 // Returns a unique ID for this shared memory's handle. Note this function may 248 // Returns a unique ID for this shared memory's handle. Note this function may
268 // access file system and be slow. 249 // access file system and be slow.
269 bool GetUniqueId(UniqueId* id) const; 250 bool GetUniqueId(UniqueId* id) const;
270 #endif 251 #endif
271 252
272 private: 253 private:
273 #if defined(OS_POSIX) && !defined(OS_NACL) && !defined(OS_ANDROID) && \ 254 #if defined(OS_POSIX) && !defined(OS_NACL) && !defined(OS_ANDROID) && \
274 (!defined(OS_MACOSX) || defined(OS_IOS)) 255 (!defined(OS_MACOSX) || defined(OS_IOS))
275 bool FilePathForMemoryName(const std::string& mem_name, FilePath* path); 256 bool FilePathForMemoryName(const std::string& mem_name, FilePath* path);
276 #endif 257 #endif
277 258
278 enum ShareMode {
279 SHARE_READONLY,
280 SHARE_CURRENT_MODE,
281 };
282
283 #if defined(OS_MACOSX) 259 #if defined(OS_MACOSX)
284 bool Share(SharedMemoryHandle* new_handle, ShareMode share_mode); 260 bool Share(SharedMemoryHandle* new_handle);
285 #endif 261 #endif
286 262
287 bool ShareToProcessCommon(ProcessHandle process, 263 bool ShareToProcessCommon(ProcessHandle process,
288 SharedMemoryHandle* new_handle, 264 SharedMemoryHandle* new_handle,
289 bool close_self, 265 bool close_self);
290 ShareMode);
291 266
292 #if defined(OS_WIN) 267 #if defined(OS_WIN)
293 // If true indicates this came from an external source so needs extra checks 268 // If true indicates this came from an external source so needs extra checks
294 // before being mapped. 269 // before being mapped.
295 bool external_section_; 270 bool external_section_;
296 std::wstring name_; 271 std::wstring name_;
297 win::ScopedHandle mapped_file_; 272 win::ScopedHandle mapped_file_;
298 #elif defined(OS_MACOSX) && !defined(OS_IOS) 273 #else
299 // The OS primitive that backs the shared memory region. 274 // The OS primitive that backs the shared memory region.
300 SharedMemoryHandle shm_; 275 SharedMemoryHandle shm_;
301 276
277 // If valid, points to the same memory region as shm_, but with readonly
278 // permissions.
279 SharedMemoryHandle readonly_shm_;
280 #endif
281
282 #if defined(OS_MACOSX) && !defined(OS_IOS)
302 // The mechanism by which the memory is mapped. Only valid if |memory_| is not 283 // The mechanism by which the memory is mapped. Only valid if |memory_| is not
303 // |nullptr|. 284 // |nullptr|.
304 SharedMemoryHandle::Type mapped_memory_mechanism_; 285 SharedMemoryHandle::Type mapped_memory_mechanism_;
286 #endif
305 287
306 int readonly_mapped_file_;
307 #elif defined(OS_POSIX)
308 // The OS primitive that backs the shared memory region.
309 SharedMemoryHandle shm_;
310 int readonly_mapped_file_;
311 #endif
312 size_t mapped_size_; 288 size_t mapped_size_;
313 void* memory_; 289 void* memory_;
314 bool read_only_; 290 bool read_only_;
315 size_t requested_size_; 291 size_t requested_size_;
316 292
317 DISALLOW_COPY_AND_ASSIGN(SharedMemory); 293 DISALLOW_COPY_AND_ASSIGN(SharedMemory);
318 }; 294 };
319 295
320 } // namespace base 296 } // namespace base
321 297
322 #endif // BASE_MEMORY_SHARED_MEMORY_H_ 298 #endif // BASE_MEMORY_SHARED_MEMORY_H_
OLDNEW
« no previous file with comments | « no previous file | base/memory/shared_memory_android.cc » ('j') | base/memory/shared_memory_win.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698