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

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

Issue 2845113005: Replace base::SharedMemory read-only methods with GetReadOnlyHandle. (Closed)
Patch Set: Rebase. 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_mac.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
211 // the shared memory file. new_handle is an output parameter to receive the 211 // Opened with share_read_only=true.
212 // handle for use in the remote process. 212 SharedMemoryHandle GetReadOnlyHandle();
213 //
214 // |*this| must have been initialized using one of the Create*() or Open()
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 213
235 // Shares the shared memory to another process. Attempts 214 // Shares the shared memory to another process. Attempts
236 // to create a platform-specific new_handle which can be 215 // to create a platform-specific new_handle which can be
237 // used in a remote process to access the shared memory 216 // used in a remote process to access the shared memory
238 // file. new_handle is an output parameter to receive 217 // file. new_handle is an output parameter to receive
239 // the handle for use in the remote process. 218 // the handle for use in the remote process.
240 // Returns true on success, false otherwise. 219 // Returns true on success, false otherwise.
241 bool ShareToProcess(ProcessHandle process, 220 bool ShareToProcess(ProcessHandle process,
242 SharedMemoryHandle* new_handle) { 221 SharedMemoryHandle* new_handle) {
243 return ShareToProcessCommon(process, new_handle, false, SHARE_CURRENT_MODE); 222 return ShareToProcessCommon(process, new_handle, false);
244 } 223 }
245 224
246 // Logically equivalent to: 225 // Logically equivalent to:
247 // bool ok = ShareToProcess(process, new_handle); 226 // bool ok = ShareToProcess(process, new_handle);
248 // Close(); 227 // Close();
249 // return ok; 228 // return ok;
250 // Note that the memory is unmapped by calling this method, regardless of the 229 // Note that the memory is unmapped by calling this method, regardless of the
251 // return value. 230 // return value.
252 bool GiveToProcess(ProcessHandle process, 231 bool GiveToProcess(ProcessHandle process,
253 SharedMemoryHandle* new_handle) { 232 SharedMemoryHandle* new_handle) {
254 return ShareToProcessCommon(process, new_handle, true, SHARE_CURRENT_MODE); 233 return ShareToProcessCommon(process, new_handle, true);
255 } 234 }
256 235
257 #if defined(OS_POSIX) && (!defined(OS_MACOSX) || defined(OS_IOS)) && \ 236 #if defined(OS_POSIX) && (!defined(OS_MACOSX) || defined(OS_IOS)) && \
258 !defined(OS_NACL) 237 !defined(OS_NACL)
259 using UniqueId = std::pair<dev_t, ino_t>; 238 using UniqueId = std::pair<dev_t, ino_t>;
260 239
261 struct UniqueIdHash { 240 struct UniqueIdHash {
262 size_t operator()(const UniqueId& id) const { 241 size_t operator()(const UniqueId& id) const {
263 return HashInts(id.first, id.second); 242 return HashInts(id.first, id.second);
264 } 243 }
265 }; 244 };
266 245
267 // Returns a unique ID for this shared memory's handle. Note this function may 246 // Returns a unique ID for this shared memory's handle. Note this function may
268 // access file system and be slow. 247 // access file system and be slow.
269 bool GetUniqueId(UniqueId* id) const; 248 bool GetUniqueId(UniqueId* id) const;
270 #endif 249 #endif
271 250
272 private: 251 private:
273 #if defined(OS_POSIX) && !defined(OS_NACL) && !defined(OS_ANDROID) && \ 252 #if defined(OS_POSIX) && !defined(OS_NACL) && !defined(OS_ANDROID) && \
274 (!defined(OS_MACOSX) || defined(OS_IOS)) 253 (!defined(OS_MACOSX) || defined(OS_IOS))
275 bool FilePathForMemoryName(const std::string& mem_name, FilePath* path); 254 bool FilePathForMemoryName(const std::string& mem_name, FilePath* path);
276 #endif 255 #endif
277 256
278 enum ShareMode {
279 SHARE_READONLY,
280 SHARE_CURRENT_MODE,
281 };
282
283 #if defined(OS_MACOSX) 257 #if defined(OS_MACOSX)
284 bool Share(SharedMemoryHandle* new_handle, ShareMode share_mode); 258 bool Share(SharedMemoryHandle* new_handle);
285 #endif 259 #endif
286 260
287 bool ShareToProcessCommon(ProcessHandle process, 261 bool ShareToProcessCommon(ProcessHandle process,
288 SharedMemoryHandle* new_handle, 262 SharedMemoryHandle* new_handle,
289 bool close_self, 263 bool close_self);
290 ShareMode);
291 264
292 #if defined(OS_WIN) 265 #if defined(OS_WIN)
293 // If true indicates this came from an external source so needs extra checks 266 // If true indicates this came from an external source so needs extra checks
294 // before being mapped. 267 // before being mapped.
295 bool external_section_; 268 bool external_section_;
296 std::wstring name_; 269 std::wstring name_;
297 win::ScopedHandle mapped_file_; 270 win::ScopedHandle mapped_file_;
298 #elif defined(OS_MACOSX) && !defined(OS_IOS) 271 #else
299 // The OS primitive that backs the shared memory region. 272 // The OS primitive that backs the shared memory region.
300 SharedMemoryHandle shm_; 273 SharedMemoryHandle shm_;
301 274
275 // If valid, points to the same memory region as shm_, but with readonly
276 // permissions.
277 SharedMemoryHandle readonly_shm_;
278 #endif
279
280 #if defined(OS_MACOSX) && !defined(OS_IOS)
302 // The mechanism by which the memory is mapped. Only valid if |memory_| is not 281 // The mechanism by which the memory is mapped. Only valid if |memory_| is not
303 // |nullptr|. 282 // |nullptr|.
304 SharedMemoryHandle::Type mapped_memory_mechanism_; 283 SharedMemoryHandle::Type mapped_memory_mechanism_;
284 #endif
305 285
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_; 286 size_t mapped_size_;
313 void* memory_; 287 void* memory_;
314 bool read_only_; 288 bool read_only_;
315 size_t requested_size_; 289 size_t requested_size_;
316 290
317 DISALLOW_COPY_AND_ASSIGN(SharedMemory); 291 DISALLOW_COPY_AND_ASSIGN(SharedMemory);
318 }; 292 };
319 293
320 } // namespace base 294 } // namespace base
321 295
322 #endif // BASE_MEMORY_SHARED_MEMORY_H_ 296 #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_mac.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698