Index: base/memory/shared_memory.h |
diff --git a/base/memory/shared_memory.h b/base/memory/shared_memory.h |
index 34cdc1ed16273befbaa8258f01777b95af33efaf..401e214175f190ea0e24e1f2042d3b51a0f2bb6c 100644 |
--- a/base/memory/shared_memory.h |
+++ b/base/memory/shared_memory.h |
@@ -80,8 +80,8 @@ class BASE_EXPORT SharedMemory { |
// shared memory file. |
// |
// WARNING: This does not reduce the OS-level permissions on the handle; it |
- // only affects how the SharedMemory will be mmapped. Use |
- // ShareReadOnlyToProcess to drop permissions. TODO(jln,jyasskin): DCHECK |
+ // only affects how the SharedMemory will be mmapped. Use |
+ // GetReadOnlyHandle to drop permissions. TODO(jln,jyasskin): DCHECK |
// that |read_only| matches the permissions of the handle. |
SharedMemory(const SharedMemoryHandle& handle, bool read_only); |
@@ -206,31 +206,14 @@ class BASE_EXPORT SharedMemory { |
// It is safe to call Close repeatedly. |
void Close(); |
- // Shares the shared memory to another process. Attempts to create a |
- // platform-specific new_handle which can be used in a remote process to read |
- // the shared memory file. new_handle is an output parameter to receive the |
- // handle for use in the remote process. |
- // |
- // |*this| must have been initialized using one of the Create*() or Open() |
- // methods with share_read_only=true. If it was constructed from a |
- // SharedMemoryHandle, this call will CHECK-fail. |
- // |
- // Returns true on success, false otherwise. |
- bool ShareReadOnlyToProcess(ProcessHandle process, |
- SharedMemoryHandle* new_handle) { |
- return ShareToProcessCommon(process, new_handle, false, SHARE_READONLY); |
- } |
- |
- // Logically equivalent to: |
- // bool ok = ShareReadOnlyToProcess(process, new_handle); |
- // Close(); |
- // return ok; |
- // Note that the memory is unmapped by calling this method, regardless of the |
- // return value. |
- bool GiveReadOnlyToProcess(ProcessHandle process, |
- SharedMemoryHandle* new_handle) { |
- return ShareToProcessCommon(process, new_handle, true, SHARE_READONLY); |
- } |
+ // Returns a read-only handle to this shared memory region. The caller takes |
+ // ownership of the handle. For POSIX handles, CHECK-fails if the region |
+ // wasn't Created or Opened with share_read_only=true, which is required to |
+ // make the handle read-only. When the handle is passed to the IPC subsystem, |
+ // that takes ownership of the handle. As such, it's not valid to pass the |
+ // sample handle to the IPC subsystem twice. Returns an invalid handle on |
+ // failure. |
+ SharedMemoryHandle GetReadOnlyHandle(); |
// Shares the shared memory to another process. Attempts |
// to create a platform-specific new_handle which can be |
@@ -240,7 +223,7 @@ class BASE_EXPORT SharedMemory { |
// Returns true on success, false otherwise. |
bool ShareToProcess(ProcessHandle process, |
SharedMemoryHandle* new_handle) { |
- return ShareToProcessCommon(process, new_handle, false, SHARE_CURRENT_MODE); |
+ return ShareToProcessCommon(process, new_handle, false); |
} |
// Logically equivalent to: |
@@ -251,7 +234,7 @@ class BASE_EXPORT SharedMemory { |
// return value. |
bool GiveToProcess(ProcessHandle process, |
SharedMemoryHandle* new_handle) { |
- return ShareToProcessCommon(process, new_handle, true, SHARE_CURRENT_MODE); |
+ return ShareToProcessCommon(process, new_handle, true); |
} |
#if defined(OS_POSIX) && (!defined(OS_MACOSX) || defined(OS_IOS)) && \ |
@@ -275,19 +258,13 @@ class BASE_EXPORT SharedMemory { |
bool FilePathForMemoryName(const std::string& mem_name, FilePath* path); |
#endif |
- enum ShareMode { |
- SHARE_READONLY, |
- SHARE_CURRENT_MODE, |
- }; |
- |
#if defined(OS_MACOSX) |
- bool Share(SharedMemoryHandle* new_handle, ShareMode share_mode); |
+ bool Share(SharedMemoryHandle* new_handle); |
#endif |
bool ShareToProcessCommon(ProcessHandle process, |
SharedMemoryHandle* new_handle, |
- bool close_self, |
- ShareMode); |
+ bool close_self); |
#if defined(OS_WIN) |
// If true indicates this came from an external source so needs extra checks |
@@ -295,20 +272,21 @@ class BASE_EXPORT SharedMemory { |
bool external_section_; |
std::wstring name_; |
win::ScopedHandle mapped_file_; |
-#elif defined(OS_MACOSX) && !defined(OS_IOS) |
+#else |
// The OS primitive that backs the shared memory region. |
SharedMemoryHandle shm_; |
+ // If valid, points to the same memory region as shm_, but with readonly |
+ // permissions. |
+ SharedMemoryHandle readonly_shm_; |
+#endif |
+ |
+#if defined(OS_MACOSX) && !defined(OS_IOS) |
// The mechanism by which the memory is mapped. Only valid if |memory_| is not |
// |nullptr|. |
SharedMemoryHandle::Type mapped_memory_mechanism_; |
- |
- int readonly_mapped_file_; |
-#elif defined(OS_POSIX) |
- // The OS primitive that backs the shared memory region. |
- SharedMemoryHandle shm_; |
- int readonly_mapped_file_; |
#endif |
+ |
size_t mapped_size_; |
void* memory_; |
bool read_only_; |