Chromium Code Reviews| 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 "base/unguessable_token.h" | |
| 10 #include "build/build_config.h" | 11 #include "build/build_config.h" |
| 11 | 12 |
| 12 #if defined(OS_WIN) | 13 #if defined(OS_WIN) |
| 13 #include <windows.h> | 14 #include <windows.h> |
| 14 #include "base/process/process_handle.h" | 15 #include "base/process/process_handle.h" |
| 15 #elif defined(OS_MACOSX) && !defined(OS_IOS) | 16 #elif defined(OS_MACOSX) && !defined(OS_IOS) |
| 16 #include <mach/mach.h> | 17 #include <mach/mach.h> |
| 17 #include "base/base_export.h" | 18 #include "base/base_export.h" |
| 18 #include "base/file_descriptor_posix.h" | 19 #include "base/file_descriptor_posix.h" |
| 19 #include "base/macros.h" | 20 #include "base/macros.h" |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 54 bool OwnershipPassesToIPC() const; | 55 bool OwnershipPassesToIPC() const; |
| 55 | 56 |
| 56 // Whether the underlying OS resource is valid. | 57 // Whether the underlying OS resource is valid. |
| 57 bool IsValid() const; | 58 bool IsValid() const; |
| 58 | 59 |
| 59 // Duplicates the underlying OS resource. Using the return value as a | 60 // Duplicates the underlying OS resource. Using the return value as a |
| 60 // parameter to an IPC message will cause the IPC subsystem to consume the OS | 61 // parameter to an IPC message will cause the IPC subsystem to consume the OS |
| 61 // resource. | 62 // resource. |
| 62 SharedMemoryHandle Duplicate() const; | 63 SharedMemoryHandle Duplicate() const; |
| 63 | 64 |
| 65 base::UnguessableToken GetGUID() const; | |
|
Nico
2017/05/04 17:42:13
add comment saying what this is good for
erikchen
2017/05/04 19:21:13
Done.
| |
| 66 | |
| 64 #if defined(OS_MACOSX) && !defined(OS_IOS) | 67 #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 65 enum Type { | 68 enum Type { |
| 66 // The SharedMemoryHandle is backed by a POSIX fd. | 69 // The SharedMemoryHandle is backed by a POSIX fd. |
| 67 POSIX, | 70 POSIX, |
| 68 // The SharedMemoryHandle is backed by the Mach primitive "memory object". | 71 // The SharedMemoryHandle is backed by the Mach primitive "memory object". |
| 69 MACH, | 72 MACH, |
| 70 }; | 73 }; |
| 71 | 74 |
| 72 // Constructs a SharedMemoryHandle backed by the components of a | 75 // Constructs a SharedMemoryHandle backed by the components of a |
| 73 // FileDescriptor. The newly created instance has the same ownership semantics | 76 // FileDescriptor. The newly created instance has the same ownership semantics |
| 74 // as base::FileDescriptor. This typically means that the SharedMemoryHandle | 77 // as base::FileDescriptor. This typically means that the SharedMemoryHandle |
| 75 // takes ownership of the |fd| if |auto_close| is true. Unfortunately, it's | 78 // takes ownership of the |fd| if |auto_close| is true. Unfortunately, it's |
| 76 // common for existing code to make shallow copies of SharedMemoryHandle, and | 79 // 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 | 80 // the one that is finally passed into a base::SharedMemory is the one that |
| 78 // "consumes" the fd. | 81 // "consumes" the fd. |
|
Nico
2017/05/04 17:42:13
mention what the guid is for (can people who don't
erikchen
2017/05/04 19:21:12
Done.
| |
| 79 explicit SharedMemoryHandle(const base::FileDescriptor& file_descriptor); | 82 SharedMemoryHandle(const base::FileDescriptor& file_descriptor, |
| 83 const base::UnguessableToken& guid); | |
| 80 | 84 |
| 81 // Makes a Mach-based SharedMemoryHandle of the given size. On error, | 85 // Makes a Mach-based SharedMemoryHandle of the given size. On error, |
| 82 // subsequent calls to IsValid() return false. | 86 // subsequent calls to IsValid() return false. |
| 83 explicit SharedMemoryHandle(mach_vm_size_t size); | 87 SharedMemoryHandle(mach_vm_size_t size, const base::UnguessableToken& guid); |
| 84 | 88 |
| 85 // Makes a Mach-based SharedMemoryHandle from |memory_object|, a named entry | 89 // Makes a Mach-based SharedMemoryHandle from |memory_object|, a named entry |
| 86 // in the current task. The memory region has size |size|. | 90 // in the current task. The memory region has size |size|. |
| 87 SharedMemoryHandle(mach_port_t memory_object, mach_vm_size_t size); | 91 SharedMemoryHandle(mach_port_t memory_object, |
| 92 mach_vm_size_t size, | |
| 93 const base::UnguessableToken& guid); | |
| 88 | 94 |
| 89 // Exposed so that the SharedMemoryHandle can be transported between | 95 // Exposed so that the SharedMemoryHandle can be transported between |
| 90 // processes. | 96 // processes. |
| 91 mach_port_t GetMemoryObject() const; | 97 mach_port_t GetMemoryObject() const; |
| 92 | 98 |
| 93 // Returns false on a failure to determine the size. On success, populates the | 99 // Returns false on a failure to determine the size. On success, populates the |
| 94 // output variable |size|. | 100 // output variable |size|. |
| 95 bool GetSize(size_t* size) const; | 101 bool GetSize(size_t* size) const; |
| 96 | 102 |
| 97 // The SharedMemoryHandle must be valid. | 103 // The SharedMemoryHandle must be valid. |
| 98 // Returns whether the SharedMemoryHandle was successfully mapped into memory. | 104 // Returns whether the SharedMemoryHandle was successfully mapped into memory. |
| 99 // On success, |memory| is an output variable that contains the start of the | 105 // On success, |memory| is an output variable that contains the start of the |
| 100 // mapped memory. | 106 // mapped memory. |
| 101 bool MapAt(off_t offset, size_t bytes, void** memory, bool read_only); | 107 bool MapAt(off_t offset, size_t bytes, void** memory, bool read_only); |
| 102 #elif defined(OS_WIN) | 108 #elif defined(OS_WIN) |
| 103 // Takes implicit ownership of |h|. | 109 // Takes implicit ownership of |h|, with the given |guid|. |
| 104 SharedMemoryHandle(HANDLE h); | 110 SharedMemoryHandle(HANDLE h, const base::UnguessableToken& guid); |
| 105 | |
| 106 HANDLE GetHandle() const; | 111 HANDLE GetHandle() const; |
| 107 #else | 112 #else |
| 108 // This constructor is deprecated, as it fails to propagate the GUID, which | 113 SharedMemoryHandle(const base::FileDescriptor& file_descriptor, |
| 109 // will be added in the near future. | 114 const base::UnguessableToken& guid); |
| 110 // TODO(rockot): Remove this constructor once Mojo supports GUIDs. | |
| 111 // https://crbug.com/713763. | |
| 112 explicit SharedMemoryHandle(const base::FileDescriptor& file_descriptor); | |
| 113 | 115 |
| 114 // Creates a SharedMemoryHandle from an |fd| supplied from an external | 116 // Creates a SharedMemoryHandle from an |fd| supplied from an external |
| 115 // service. | 117 // service. |
| 116 static SharedMemoryHandle ImportHandle(int fd); | 118 static SharedMemoryHandle ImportHandle(int fd); |
| 117 | 119 |
| 118 // Returns the underlying OS resource. | 120 // Returns the underlying OS resource. |
| 119 int GetHandle() const; | 121 int GetHandle() const; |
| 120 | 122 |
| 121 // Takes ownership of the OS resource. | 123 // Takes ownership of the OS resource. |
| 122 void SetHandle(int fd); | 124 void SetHandle(int fd); |
| 123 | 125 |
| 124 // Invalidates [but doesn't close] the underlying OS resource. This will leak | 126 // Invalidates [but doesn't close] the underlying OS resource. This will leak |
| 125 // unless the caller is careful. | 127 // unless the caller is careful. |
| 126 int Release(); | 128 int Release(); |
| 127 #endif | 129 #endif |
| 128 | 130 |
| 129 private: | 131 private: |
| 130 #if defined(OS_MACOSX) && !defined(OS_IOS) | 132 #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 131 friend class SharedMemory; | 133 friend class SharedMemory; |
| 132 | 134 |
| 133 // Shared code between copy constructor and operator=. | |
| 134 void CopyRelevantData(const SharedMemoryHandle& handle); | |
| 135 | |
| 136 Type type_; | 135 Type type_; |
| 137 | 136 |
| 138 // Each instance of a SharedMemoryHandle is backed either by a POSIX fd or a | 137 // Each instance of a SharedMemoryHandle is backed either by a POSIX fd or a |
| 139 // mach port. |type_| determines the backing member. | 138 // mach port. |type_| determines the backing member. |
| 140 union { | 139 union { |
| 141 FileDescriptor file_descriptor_; | 140 FileDescriptor file_descriptor_; |
| 142 | 141 |
| 143 struct { | 142 struct { |
| 144 mach_port_t memory_object_; | 143 mach_port_t memory_object_; |
| 145 | 144 |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 159 | 158 |
| 160 // 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 |
| 161 // 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 |
| 162 // behavior of the |auto_close| parameter of FileDescriptor. This member only | 161 // behavior of the |auto_close| parameter of FileDescriptor. This member only |
| 163 // affects attachment-brokered SharedMemoryHandles. | 162 // affects attachment-brokered SharedMemoryHandles. |
| 164 // Defaults to |false|. | 163 // Defaults to |false|. |
| 165 bool ownership_passes_to_ipc_; | 164 bool ownership_passes_to_ipc_; |
| 166 #else | 165 #else |
| 167 FileDescriptor file_descriptor_; | 166 FileDescriptor file_descriptor_; |
| 168 #endif | 167 #endif |
| 168 | |
| 169 // Uniques identifies the SharedMemoryHandle. Preserved across IPC. | |
| 170 base::UnguessableToken guid_; | |
| 169 }; | 171 }; |
| 170 | 172 |
| 171 } // namespace base | 173 } // namespace base |
| 172 | 174 |
| 173 #endif // BASE_MEMORY_SHARED_MEMORY_HANDLE_H_ | 175 #endif // BASE_MEMORY_SHARED_MEMORY_HANDLE_H_ |
| OLD | NEW |