Index: base/memory/shared_memory_handle.h |
diff --git a/base/memory/shared_memory_handle.h b/base/memory/shared_memory_handle.h |
index 942cba4f2bec02ed2d271c16801aaff8ef993e96..4af796bfa140a3f3c33f5506ea2a4aef9a625829 100644 |
--- a/base/memory/shared_memory_handle.h |
+++ b/base/memory/shared_memory_handle.h |
@@ -27,33 +27,18 @@ namespace base { |
// SharedMemoryHandle is a platform specific type which represents |
// the underlying OS handle to a shared memory segment. |
-// TODO(erikchen): Refactor this to create a single class on all platforms, |
-// rather than 3 separate class definitions that are very similar. |
-// https://crbug.com/713763. |
-#if defined(OS_POSIX) && !(defined(OS_MACOSX) && !defined(OS_IOS)) |
class BASE_EXPORT SharedMemoryHandle { |
public: |
// The default constructor returns an invalid SharedMemoryHandle. |
SharedMemoryHandle(); |
- // This constructor is deprecated, as it fails to propagate the GUID, which |
- // will be added in the near future. |
- // TODO(rockot): Remove this constructor once Mojo supports GUIDs. |
- // https://crbug.com/713763. |
- explicit SharedMemoryHandle(const base::FileDescriptor& file_descriptor); |
- |
- // Creates a SharedMemoryHandle from an |fd| supplied from an external |
- // service. |
- static SharedMemoryHandle ImportHandle(int fd); |
- |
- // Returns the underlying OS resource. |
- int GetHandle() const; |
- |
- // Takes ownership of the OS resource. |
- void SetHandle(int fd); |
+ // Standard copy constructor. The new instance shares the underlying OS |
+ // primitives. |
+ SharedMemoryHandle(const SharedMemoryHandle& handle); |
- // Whether the underlying OS resource is valid. |
- bool IsValid() const; |
+ // Standard assignment operator. The updated instance shares the underlying |
+ // OS primitives. |
+ SharedMemoryHandle& operator=(const SharedMemoryHandle& handle); |
// Closes the underlying OS resource. |
// The fact that this method needs to be "const" is an artifact of the |
@@ -63,77 +48,15 @@ class BASE_EXPORT SharedMemoryHandle { |
// https://crbug.com/716072. |
void Close() const; |
- // Invalidates [but doesn't close] the underlying OS resource. This will leak |
- // unless the caller is careful. |
- int Release(); |
- |
- // Duplicates the underlying OS resource. |
- SharedMemoryHandle Duplicate() const; |
- |
- // If this returns true, then ownership of the underlying OS resource is |
- // implicitly passed to the IPC subsystem during serialization. |
+ // Whether ownership of the underlying OS resource is implicitly passed to |
+ // the IPC subsystem during serialization. |
void SetOwnershipPassesToIPC(bool ownership_passes); |
bool OwnershipPassesToIPC() const; |
- private: |
- // This must be public to support serialization by Chrome IPC. |
- FileDescriptor file_descriptor_; |
-}; |
-#elif defined(OS_WIN) |
-class BASE_EXPORT SharedMemoryHandle { |
- public: |
- // The default constructor returns an invalid SharedMemoryHandle. |
- SharedMemoryHandle(); |
- SharedMemoryHandle(HANDLE h, base::ProcessId pid); |
- |
- // Standard copy constructor. The new instance shares the underlying OS |
- // primitives. |
- SharedMemoryHandle(const SharedMemoryHandle& handle); |
- |
- // Standard assignment operator. The updated instance shares the underlying |
- // OS primitives. |
- SharedMemoryHandle& operator=(const SharedMemoryHandle& handle); |
- |
- // Comparison operators. |
- bool operator==(const SharedMemoryHandle& handle) const; |
- bool operator!=(const SharedMemoryHandle& handle) const; |
- |
- // Closes the underlying OS resources. |
- void Close() const; |
- |
- // Whether the underlying OS primitive is valid. |
+ // Whether the underlying OS resource is valid. |
bool IsValid() const; |
- // Whether |pid_| is the same as the current process's id. |
- bool BelongsToCurrentProcess() const; |
- |
- // Whether handle_ needs to be duplicated into the destination process when |
- // an instance of this class is passed over a Chrome IPC channel. |
- bool NeedsBrokering() const; |
- |
- void SetOwnershipPassesToIPC(bool ownership_passes); |
- bool OwnershipPassesToIPC() const; |
- |
- HANDLE GetHandle() const; |
- base::ProcessId GetPID() const; |
- |
- private: |
- HANDLE handle_; |
- |
- // The process in which |handle_| is valid and can be used. If |handle_| is |
- // invalid, this will be kNullProcessId. |
- base::ProcessId pid_; |
- |
- // Whether passing this object as a parameter to an IPC message passes |
- // ownership of |handle_| to the IPC stack. This is meant to mimic the |
- // behavior of the |auto_close| parameter of FileDescriptor. This member only |
- // affects attachment-brokered SharedMemoryHandles. |
- // Defaults to |false|. |
- bool ownership_passes_to_ipc_; |
-}; |
-#else |
-class BASE_EXPORT SharedMemoryHandle { |
- public: |
+#if defined(OS_MACOSX) && !defined(OS_IOS) |
enum Type { |
// The SharedMemoryHandle is backed by a POSIX fd. |
POSIX, |
@@ -141,9 +64,6 @@ class BASE_EXPORT SharedMemoryHandle { |
MACH, |
}; |
- // The default constructor returns an invalid SharedMemoryHandle. |
- SharedMemoryHandle(); |
- |
// Constructs a SharedMemoryHandle backed by the components of a |
// FileDescriptor. The newly created instance has the same ownership semantics |
// as base::FileDescriptor. This typically means that the SharedMemoryHandle |
@@ -163,24 +83,12 @@ class BASE_EXPORT SharedMemoryHandle { |
mach_vm_size_t size, |
base::ProcessId pid); |
- // Standard copy constructor. The new instance shares the underlying OS |
- // primitives. |
- SharedMemoryHandle(const SharedMemoryHandle& handle); |
- |
- // Standard assignment operator. The updated instance shares the underlying |
- // OS primitives. |
- SharedMemoryHandle& operator=(const SharedMemoryHandle& handle); |
- |
- // Duplicates the underlying OS resources. |
- SharedMemoryHandle Duplicate() const; |
- |
// Comparison operators. |
bool operator==(const SharedMemoryHandle& handle) const; |
bool operator!=(const SharedMemoryHandle& handle) const; |
- // Whether the underlying OS primitive is valid. Once the SharedMemoryHandle |
- // is backed by a valid OS primitive, it becomes immutable. |
- bool IsValid() const; |
+ // Duplicates the underlying OS resources. |
+ SharedMemoryHandle Duplicate() const; |
// Exposed so that the SharedMemoryHandle can be transported between |
// processes. |
@@ -195,14 +103,49 @@ class BASE_EXPORT SharedMemoryHandle { |
// On success, |memory| is an output variable that contains the start of the |
// mapped memory. |
bool MapAt(off_t offset, size_t bytes, void** memory, bool read_only); |
+#elif defined(OS_WIN) |
+ SharedMemoryHandle(HANDLE h, base::ProcessId pid); |
- // Closes the underlying OS primitive. |
- void Close() const; |
+ // Comparison operators. |
+ bool operator==(const SharedMemoryHandle& handle) const; |
+ bool operator!=(const SharedMemoryHandle& handle) const; |
- void SetOwnershipPassesToIPC(bool ownership_passes); |
- bool OwnershipPassesToIPC() const; |
+ // Whether |pid_| is the same as the current process's id. |
+ bool BelongsToCurrentProcess() const; |
+ |
+ // Whether handle_ needs to be duplicated into the destination process when |
+ // an instance of this class is passed over a Chrome IPC channel. |
+ bool NeedsBrokering() const; |
+ |
+ HANDLE GetHandle() const; |
+ base::ProcessId GetPID() const; |
+#else |
+ // This constructor is deprecated, as it fails to propagate the GUID, which |
+ // will be added in the near future. |
+ // TODO(rockot): Remove this constructor once Mojo supports GUIDs. |
+ // https://crbug.com/713763. |
+ explicit SharedMemoryHandle(const base::FileDescriptor& file_descriptor); |
+ |
+ // Creates a SharedMemoryHandle from an |fd| supplied from an external |
+ // service. |
+ static SharedMemoryHandle ImportHandle(int fd); |
+ |
+ // Returns the underlying OS resource. |
+ int GetHandle() const; |
+ |
+ // Takes ownership of the OS resource. |
+ void SetHandle(int fd); |
+ |
+ // Invalidates [but doesn't close] the underlying OS resource. This will leak |
+ // unless the caller is careful. |
+ int Release(); |
+ |
+ // Duplicates the underlying OS resource. |
+ SharedMemoryHandle Duplicate() const; |
+#endif |
private: |
+#if defined(OS_MACOSX) && !defined(OS_IOS) |
friend class SharedMemory; |
// Shared code between copy constructor and operator=. |
@@ -233,8 +176,23 @@ class BASE_EXPORT SharedMemoryHandle { |
bool ownership_passes_to_ipc_; |
}; |
}; |
-}; |
+#elif defined(OS_WIN) |
+ HANDLE handle_; |
+ |
+ // The process in which |handle_| is valid and can be used. If |handle_| is |
+ // invalid, this will be kNullProcessId. |
+ base::ProcessId pid_; |
+ |
+ // Whether passing this object as a parameter to an IPC message passes |
+ // ownership of |handle_| to the IPC stack. This is meant to mimic the |
+ // behavior of the |auto_close| parameter of FileDescriptor. This member only |
+ // affects attachment-brokered SharedMemoryHandles. |
+ // Defaults to |false|. |
+ bool ownership_passes_to_ipc_; |
+#else |
+ FileDescriptor file_descriptor_; |
#endif |
+}; |
} // namespace base |