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

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

Issue 2846893003: Used a single class for SharedMemoryHandle. (Closed)
Patch Set: clang format. 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_handle_posix.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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"
11 11
12 #if defined(OS_WIN) 12 #if defined(OS_WIN)
13 #include <windows.h> 13 #include <windows.h>
14 #include "base/process/process_handle.h" 14 #include "base/process/process_handle.h"
15 #elif defined(OS_MACOSX) && !defined(OS_IOS) 15 #elif defined(OS_MACOSX) && !defined(OS_IOS)
16 #include <mach/mach.h> 16 #include <mach/mach.h>
17 #include "base/base_export.h" 17 #include "base/base_export.h"
18 #include "base/file_descriptor_posix.h" 18 #include "base/file_descriptor_posix.h"
19 #include "base/macros.h" 19 #include "base/macros.h"
20 #include "base/process/process_handle.h" 20 #include "base/process/process_handle.h"
21 #elif defined(OS_POSIX) 21 #elif defined(OS_POSIX)
22 #include <sys/types.h> 22 #include <sys/types.h>
23 #include "base/file_descriptor_posix.h" 23 #include "base/file_descriptor_posix.h"
24 #endif 24 #endif
25 25
26 namespace base { 26 namespace base {
Nico 2017/04/27 19:50:09 Maybe add an #if defined(OS_IOS) #error at the top
erikchen 2017/04/27 19:54:28 There's no behavior change here. We've always had
Nico 2017/04/27 19:56:13 Are you sure? The LHS has #if defined(OS_POSIX) &
27 27
28 // SharedMemoryHandle is a platform specific type which represents 28 // SharedMemoryHandle is a platform specific type which represents
29 // the underlying OS handle to a shared memory segment. 29 // the underlying OS handle to a shared memory segment.
30 // TODO(erikchen): Refactor this to create a single class on all platforms,
31 // rather than 3 separate class definitions that are very similar.
32 // https://crbug.com/713763.
33 #if defined(OS_POSIX) && !(defined(OS_MACOSX) && !defined(OS_IOS))
34 class BASE_EXPORT SharedMemoryHandle { 30 class BASE_EXPORT SharedMemoryHandle {
35 public: 31 public:
36 // The default constructor returns an invalid SharedMemoryHandle. 32 // The default constructor returns an invalid SharedMemoryHandle.
37 SharedMemoryHandle(); 33 SharedMemoryHandle();
38 34
39 // This constructor is deprecated, as it fails to propagate the GUID, which 35 // Standard copy constructor. The new instance shares the underlying OS
40 // will be added in the near future. 36 // primitives.
41 // TODO(rockot): Remove this constructor once Mojo supports GUIDs. 37 SharedMemoryHandle(const SharedMemoryHandle& handle);
42 // https://crbug.com/713763.
43 explicit SharedMemoryHandle(const base::FileDescriptor& file_descriptor);
44 38
45 // Creates a SharedMemoryHandle from an |fd| supplied from an external 39 // Standard assignment operator. The updated instance shares the underlying
46 // service. 40 // OS primitives.
47 static SharedMemoryHandle ImportHandle(int fd); 41 SharedMemoryHandle& operator=(const SharedMemoryHandle& handle);
48
49 // Returns the underlying OS resource.
50 int GetHandle() const;
51
52 // Takes ownership of the OS resource.
53 void SetHandle(int fd);
54
55 // Whether the underlying OS resource is valid.
56 bool IsValid() const;
57 42
58 // Closes the underlying OS resource. 43 // Closes the underlying OS resource.
59 // The fact that this method needs to be "const" is an artifact of the 44 // The fact that this method needs to be "const" is an artifact of the
60 // original interface for base::SharedMemory::CloseHandle. 45 // original interface for base::SharedMemory::CloseHandle.
61 // TODO(erikchen): This doesn't clear the underlying reference, which seems 46 // TODO(erikchen): This doesn't clear the underlying reference, which seems
62 // like a bug, but is how this class has always worked. Fix this: 47 // like a bug, but is how this class has always worked. Fix this:
63 // https://crbug.com/716072. 48 // https://crbug.com/716072.
64 void Close() const; 49 void Close() const;
65 50
66 // Invalidates [but doesn't close] the underlying OS resource. This will leak 51 // Whether ownership of the underlying OS resource is implicitly passed to
67 // unless the caller is careful. 52 // the IPC subsystem during serialization.
68 int Release();
69
70 // Duplicates the underlying OS resource.
71 SharedMemoryHandle Duplicate() const;
72
73 // If this returns true, then ownership of the underlying OS resource is
74 // implicitly passed to the IPC subsystem during serialization.
75 void SetOwnershipPassesToIPC(bool ownership_passes); 53 void SetOwnershipPassesToIPC(bool ownership_passes);
76 bool OwnershipPassesToIPC() const; 54 bool OwnershipPassesToIPC() const;
77 55
78 private: 56 // Whether the underlying OS resource is valid.
79 // This must be public to support serialization by Chrome IPC.
80 FileDescriptor file_descriptor_;
81 };
82 #elif defined(OS_WIN)
83 class BASE_EXPORT SharedMemoryHandle {
84 public:
85 // The default constructor returns an invalid SharedMemoryHandle.
86 SharedMemoryHandle();
87 SharedMemoryHandle(HANDLE h, base::ProcessId pid);
88
89 // Standard copy constructor. The new instance shares the underlying OS
90 // primitives.
91 SharedMemoryHandle(const SharedMemoryHandle& handle);
92
93 // Standard assignment operator. The updated instance shares the underlying
94 // OS primitives.
95 SharedMemoryHandle& operator=(const SharedMemoryHandle& handle);
96
97 // Comparison operators.
98 bool operator==(const SharedMemoryHandle& handle) const;
99 bool operator!=(const SharedMemoryHandle& handle) const;
100
101 // Closes the underlying OS resources.
102 void Close() const;
103
104 // Whether the underlying OS primitive is valid.
105 bool IsValid() const; 57 bool IsValid() const;
106 58
107 // Whether |pid_| is the same as the current process's id. 59 #if defined(OS_MACOSX) && !defined(OS_IOS)
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
114 void SetOwnershipPassesToIPC(bool ownership_passes);
115 bool OwnershipPassesToIPC() const;
116
117 HANDLE GetHandle() const;
118 base::ProcessId GetPID() const;
119
120 private:
121 HANDLE handle_;
122
123 // The process in which |handle_| is valid and can be used. If |handle_| is
124 // invalid, this will be kNullProcessId.
125 base::ProcessId pid_;
126
127 // Whether passing this object as a parameter to an IPC message passes
128 // ownership of |handle_| to the IPC stack. This is meant to mimic the
129 // behavior of the |auto_close| parameter of FileDescriptor. This member only
130 // affects attachment-brokered SharedMemoryHandles.
131 // Defaults to |false|.
132 bool ownership_passes_to_ipc_;
133 };
134 #else
135 class BASE_EXPORT SharedMemoryHandle {
136 public:
137 enum Type { 60 enum Type {
138 // The SharedMemoryHandle is backed by a POSIX fd. 61 // The SharedMemoryHandle is backed by a POSIX fd.
139 POSIX, 62 POSIX,
140 // The SharedMemoryHandle is backed by the Mach primitive "memory object". 63 // The SharedMemoryHandle is backed by the Mach primitive "memory object".
141 MACH, 64 MACH,
142 }; 65 };
143 66
144 // The default constructor returns an invalid SharedMemoryHandle.
145 SharedMemoryHandle();
146
147 // Constructs a SharedMemoryHandle backed by the components of a 67 // Constructs a SharedMemoryHandle backed by the components of a
148 // FileDescriptor. The newly created instance has the same ownership semantics 68 // FileDescriptor. The newly created instance has the same ownership semantics
149 // as base::FileDescriptor. This typically means that the SharedMemoryHandle 69 // as base::FileDescriptor. This typically means that the SharedMemoryHandle
150 // takes ownership of the |fd| if |auto_close| is true. Unfortunately, it's 70 // takes ownership of the |fd| if |auto_close| is true. Unfortunately, it's
151 // common for existing code to make shallow copies of SharedMemoryHandle, and 71 // common for existing code to make shallow copies of SharedMemoryHandle, and
152 // the one that is finally passed into a base::SharedMemory is the one that 72 // the one that is finally passed into a base::SharedMemory is the one that
153 // "consumes" the fd. 73 // "consumes" the fd.
154 explicit SharedMemoryHandle(const base::FileDescriptor& file_descriptor); 74 explicit SharedMemoryHandle(const base::FileDescriptor& file_descriptor);
155 75
156 // Makes a Mach-based SharedMemoryHandle of the given size. On error, 76 // Makes a Mach-based SharedMemoryHandle of the given size. On error,
157 // subsequent calls to IsValid() return false. 77 // subsequent calls to IsValid() return false.
158 explicit SharedMemoryHandle(mach_vm_size_t size); 78 explicit SharedMemoryHandle(mach_vm_size_t size);
159 79
160 // Makes a Mach-based SharedMemoryHandle from |memory_object|, a named entry 80 // Makes a Mach-based SharedMemoryHandle from |memory_object|, a named entry
161 // in the task with process id |pid|. The memory region has size |size|. 81 // in the task with process id |pid|. The memory region has size |size|.
162 SharedMemoryHandle(mach_port_t memory_object, 82 SharedMemoryHandle(mach_port_t memory_object,
163 mach_vm_size_t size, 83 mach_vm_size_t size,
164 base::ProcessId pid); 84 base::ProcessId pid);
165 85
166 // Standard copy constructor. The new instance shares the underlying OS
167 // primitives.
168 SharedMemoryHandle(const SharedMemoryHandle& handle);
169
170 // Standard assignment operator. The updated instance shares the underlying
171 // OS primitives.
172 SharedMemoryHandle& operator=(const SharedMemoryHandle& handle);
173
174 // Duplicates the underlying OS resources.
175 SharedMemoryHandle Duplicate() const;
176
177 // Comparison operators. 86 // Comparison operators.
178 bool operator==(const SharedMemoryHandle& handle) const; 87 bool operator==(const SharedMemoryHandle& handle) const;
179 bool operator!=(const SharedMemoryHandle& handle) const; 88 bool operator!=(const SharedMemoryHandle& handle) const;
180 89
181 // Whether the underlying OS primitive is valid. Once the SharedMemoryHandle 90 // Duplicates the underlying OS resources.
182 // is backed by a valid OS primitive, it becomes immutable. 91 SharedMemoryHandle Duplicate() const;
183 bool IsValid() const;
184 92
185 // Exposed so that the SharedMemoryHandle can be transported between 93 // Exposed so that the SharedMemoryHandle can be transported between
186 // processes. 94 // processes.
187 mach_port_t GetMemoryObject() const; 95 mach_port_t GetMemoryObject() const;
188 96
189 // Returns false on a failure to determine the size. On success, populates the 97 // Returns false on a failure to determine the size. On success, populates the
190 // output variable |size|. 98 // output variable |size|.
191 bool GetSize(size_t* size) const; 99 bool GetSize(size_t* size) const;
192 100
193 // The SharedMemoryHandle must be valid. 101 // The SharedMemoryHandle must be valid.
194 // Returns whether the SharedMemoryHandle was successfully mapped into memory. 102 // Returns whether the SharedMemoryHandle was successfully mapped into memory.
195 // On success, |memory| is an output variable that contains the start of the 103 // On success, |memory| is an output variable that contains the start of the
196 // mapped memory. 104 // mapped memory.
197 bool MapAt(off_t offset, size_t bytes, void** memory, bool read_only); 105 bool MapAt(off_t offset, size_t bytes, void** memory, bool read_only);
106 #elif defined(OS_WIN)
107 SharedMemoryHandle(HANDLE h, base::ProcessId pid);
198 108
199 // Closes the underlying OS primitive. 109 // Comparison operators.
200 void Close() const; 110 bool operator==(const SharedMemoryHandle& handle) const;
111 bool operator!=(const SharedMemoryHandle& handle) const;
201 112
202 void SetOwnershipPassesToIPC(bool ownership_passes); 113 // Whether |pid_| is the same as the current process's id.
203 bool OwnershipPassesToIPC() const; 114 bool BelongsToCurrentProcess() const;
115
116 // Whether handle_ needs to be duplicated into the destination process when
117 // an instance of this class is passed over a Chrome IPC channel.
118 bool NeedsBrokering() const;
119
120 HANDLE GetHandle() const;
121 base::ProcessId GetPID() const;
122 #else
123 // This constructor is deprecated, as it fails to propagate the GUID, which
124 // will be added in the near future.
125 // TODO(rockot): Remove this constructor once Mojo supports GUIDs.
126 // https://crbug.com/713763.
127 explicit SharedMemoryHandle(const base::FileDescriptor& file_descriptor);
128
129 // Creates a SharedMemoryHandle from an |fd| supplied from an external
130 // service.
131 static SharedMemoryHandle ImportHandle(int fd);
132
133 // Returns the underlying OS resource.
134 int GetHandle() const;
135
136 // Takes ownership of the OS resource.
137 void SetHandle(int fd);
138
139 // Invalidates [but doesn't close] the underlying OS resource. This will leak
140 // unless the caller is careful.
141 int Release();
142
143 // Duplicates the underlying OS resource.
144 SharedMemoryHandle Duplicate() const;
145 #endif
204 146
205 private: 147 private:
148 #if defined(OS_MACOSX) && !defined(OS_IOS)
206 friend class SharedMemory; 149 friend class SharedMemory;
207 150
208 // Shared code between copy constructor and operator=. 151 // Shared code between copy constructor and operator=.
209 void CopyRelevantData(const SharedMemoryHandle& handle); 152 void CopyRelevantData(const SharedMemoryHandle& handle);
210 153
211 Type type_; 154 Type type_;
212 155
213 // Each instance of a SharedMemoryHandle is backed either by a POSIX fd or a 156 // Each instance of a SharedMemoryHandle is backed either by a POSIX fd or a
214 // mach port. |type_| determines the backing member. 157 // mach port. |type_| determines the backing member.
215 union { 158 union {
(...skipping 10 matching lines...) Expand all
226 // relevant if |memory_object_| is not |MACH_PORT_NULL|. 169 // relevant if |memory_object_| is not |MACH_PORT_NULL|.
227 base::ProcessId pid_; 170 base::ProcessId pid_;
228 171
229 // Whether passing this object as a parameter to an IPC message passes 172 // Whether passing this object as a parameter to an IPC message passes
230 // ownership of |memory_object_| to the IPC stack. This is meant to mimic 173 // ownership of |memory_object_| to the IPC stack. This is meant to mimic
231 // the behavior of the |auto_close| parameter of FileDescriptor. 174 // the behavior of the |auto_close| parameter of FileDescriptor.
232 // Defaults to |false|. 175 // Defaults to |false|.
233 bool ownership_passes_to_ipc_; 176 bool ownership_passes_to_ipc_;
234 }; 177 };
235 }; 178 };
179 #elif defined(OS_WIN)
180 HANDLE handle_;
181
182 // The process in which |handle_| is valid and can be used. If |handle_| is
183 // invalid, this will be kNullProcessId.
184 base::ProcessId pid_;
185
186 // Whether passing this object as a parameter to an IPC message passes
187 // ownership of |handle_| to the IPC stack. This is meant to mimic the
188 // behavior of the |auto_close| parameter of FileDescriptor. This member only
189 // affects attachment-brokered SharedMemoryHandles.
190 // Defaults to |false|.
191 bool ownership_passes_to_ipc_;
192 #else
193 FileDescriptor file_descriptor_;
194 #endif
236 }; 195 };
237 #endif
238 196
239 } // namespace base 197 } // namespace base
240 198
241 #endif // BASE_MEMORY_SHARED_MEMORY_HANDLE_H_ 199 #endif // BASE_MEMORY_SHARED_MEMORY_HANDLE_H_
OLDNEW
« no previous file with comments | « no previous file | base/memory/shared_memory_handle_posix.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698