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 "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 { |
| 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. | |
| 30 #if defined(OS_POSIX) && !(defined(OS_MACOSX) && !defined(OS_IOS)) | 33 #if defined(OS_POSIX) && !(defined(OS_MACOSX) && !defined(OS_IOS)) |
| 31 typedef FileDescriptor SharedMemoryHandle; | 34 class BASE_EXPORT SharedMemoryHandle { |
| 35 public: | |
| 36 // The default constructor returns an invalid SharedMemoryHandle. | |
| 37 SharedMemoryHandle(); | |
| 38 | |
| 39 // This constructor is deprecated, as it fails to propagate the GUID, which | |
| 40 // will be added in the near future. | |
| 41 // TODO(rockot): Remove this constructor once Mojo supports GUIDs. | |
| 42 // https://crbug.com/713763. | |
| 43 explicit SharedMemoryHandle(const base::FileDescriptor& file_descriptor); | |
| 44 | |
| 45 // Creates a SharedMemoryHandle from an |fd| supplied from an external | |
| 46 // service. | |
| 47 static SharedMemoryHandle ImportHandle(int fd); | |
| 48 | |
| 49 // Returns the underlying OS resource. | |
| 50 int GetHandle() const; | |
| 51 | |
| 52 // Takes ownership of the OS resource. | |
|
Robert Sesek
2017/04/28 16:19:13
What happens if this is called on a Handle that al
erikchen
2017/04/28 17:03:38
other than a light slap on the wrist?
This is onl
| |
| 53 void SetHandle(int fd); | |
| 54 | |
| 55 // Whether the underlying OS resource is valid. | |
| 56 bool IsValid() const; | |
| 57 | |
| 58 // Closes the underlying OS resource. | |
| 59 // The fact that this method needs to be "const" is an artifact of the | |
| 60 // original interface for base::SharedMemory::CloseHandle. | |
| 61 // 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: | |
| 63 // https://crbug.com/716072. | |
| 64 void Close() const; | |
| 65 | |
| 66 // Invalidates [but doesn't close] the underlying OS resource. This will leak | |
| 67 // unless the caller is careful. | |
| 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); | |
| 76 bool OwnershipPassesToIPC() const; | |
| 77 | |
| 78 private: | |
| 79 // This must be public to support serialization by Chrome IPC. | |
| 80 FileDescriptor file_descriptor_; | |
| 81 }; | |
| 32 #elif defined(OS_WIN) | 82 #elif defined(OS_WIN) |
| 33 class BASE_EXPORT SharedMemoryHandle { | 83 class BASE_EXPORT SharedMemoryHandle { |
| 34 public: | 84 public: |
| 35 // The default constructor returns an invalid SharedMemoryHandle. | 85 // The default constructor returns an invalid SharedMemoryHandle. |
| 36 SharedMemoryHandle(); | 86 SharedMemoryHandle(); |
| 37 SharedMemoryHandle(HANDLE h, base::ProcessId pid); | 87 SharedMemoryHandle(HANDLE h, base::ProcessId pid); |
| 38 | 88 |
| 39 // Standard copy constructor. The new instance shares the underlying OS | 89 // Standard copy constructor. The new instance shares the underlying OS |
| 40 // primitives. | 90 // primitives. |
| 41 SharedMemoryHandle(const SharedMemoryHandle& handle); | 91 SharedMemoryHandle(const SharedMemoryHandle& handle); |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 182 // Defaults to |false|. | 232 // Defaults to |false|. |
| 183 bool ownership_passes_to_ipc_; | 233 bool ownership_passes_to_ipc_; |
| 184 }; | 234 }; |
| 185 }; | 235 }; |
| 186 }; | 236 }; |
| 187 #endif | 237 #endif |
| 188 | 238 |
| 189 } // namespace base | 239 } // namespace base |
| 190 | 240 |
| 191 #endif // BASE_MEMORY_SHARED_MEMORY_HANDLE_H_ | 241 #endif // BASE_MEMORY_SHARED_MEMORY_HANDLE_H_ |
| OLD | NEW |