| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 PPAPI_PROXY_SERIALIZED_HANDLES_H_ | 5 #ifndef PPAPI_PROXY_SERIALIZED_HANDLES_H_ |
| 6 #define PPAPI_PROXY_SERIALIZED_HANDLES_H_ | 6 #define PPAPI_PROXY_SERIALIZED_HANDLES_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 | 22 |
| 23 namespace ppapi { | 23 namespace ppapi { |
| 24 namespace proxy { | 24 namespace proxy { |
| 25 | 25 |
| 26 // SerializedHandle is a unified structure for holding a handle (e.g., a shared | 26 // SerializedHandle is a unified structure for holding a handle (e.g., a shared |
| 27 // memory handle, socket descriptor, etc). This is useful for passing handles in | 27 // memory handle, socket descriptor, etc). This is useful for passing handles in |
| 28 // resource messages and also makes it easier to translate handles in | 28 // resource messages and also makes it easier to translate handles in |
| 29 // NaClIPCAdapter for use in NaCl. | 29 // NaClIPCAdapter for use in NaCl. |
| 30 class PPAPI_PROXY_EXPORT SerializedHandle { | 30 class PPAPI_PROXY_EXPORT SerializedHandle { |
| 31 public: | 31 public: |
| 32 enum Type { INVALID, SHARED_MEMORY, SOCKET, CHANNEL_HANDLE, FILE }; | 32 enum Type { INVALID, SHARED_MEMORY, SOCKET, FILE }; |
| 33 // Header contains the fields that we send in IPC messages, apart from the | 33 // Header contains the fields that we send in IPC messages, apart from the |
| 34 // actual handle. See comments on the SerializedHandle fields below. | 34 // actual handle. See comments on the SerializedHandle fields below. |
| 35 struct Header { | 35 struct Header { |
| 36 Header() : type(INVALID), size(0), open_flags(0) {} | 36 Header() : type(INVALID), size(0), open_flags(0) {} |
| 37 Header(Type type_arg, | 37 Header(Type type_arg, |
| 38 uint32 size_arg, | 38 uint32 size_arg, |
| 39 int32 open_flags_arg, | 39 int32 open_flags_arg, |
| 40 PP_Resource file_io_arg) | 40 PP_Resource file_io_arg) |
| 41 : type(type_arg), | 41 : type(type_arg), |
| 42 size(size_arg), | 42 size(size_arg), |
| 43 open_flags(open_flags_arg), | 43 open_flags(open_flags_arg), |
| 44 file_io(file_io_arg) { | 44 file_io(file_io_arg) { |
| 45 } | 45 } |
| 46 | 46 |
| 47 Type type; | 47 Type type; |
| 48 uint32 size; | 48 uint32 size; |
| 49 int32 open_flags; | 49 int32 open_flags; |
| 50 PP_Resource file_io; | 50 PP_Resource file_io; |
| 51 }; | 51 }; |
| 52 | 52 |
| 53 SerializedHandle(); | 53 SerializedHandle(); |
| 54 // Create an invalid handle of the given type. | 54 // Create an invalid handle of the given type. |
| 55 explicit SerializedHandle(Type type); | 55 explicit SerializedHandle(Type type); |
| 56 | 56 |
| 57 // Create a shared memory handle. | 57 // Create a shared memory handle. |
| 58 SerializedHandle(const base::SharedMemoryHandle& handle, uint32 size); | 58 SerializedHandle(const base::SharedMemoryHandle& handle, uint32 size); |
| 59 | 59 |
| 60 // Create a socket, channel or file handle. | 60 // Create a socket or file handle. |
| 61 SerializedHandle(const Type type, | 61 SerializedHandle(const Type type, |
| 62 const IPC::PlatformFileForTransit& descriptor); | 62 const IPC::PlatformFileForTransit& descriptor); |
| 63 | 63 |
| 64 Type type() const { return type_; } | 64 Type type() const { return type_; } |
| 65 bool is_shmem() const { return type_ == SHARED_MEMORY; } | 65 bool is_shmem() const { return type_ == SHARED_MEMORY; } |
| 66 bool is_socket() const { return type_ == SOCKET; } | 66 bool is_socket() const { return type_ == SOCKET; } |
| 67 bool is_channel_handle() const { return type_ == CHANNEL_HANDLE; } | |
| 68 bool is_file() const { return type_ == FILE; } | 67 bool is_file() const { return type_ == FILE; } |
| 69 const base::SharedMemoryHandle& shmem() const { | 68 const base::SharedMemoryHandle& shmem() const { |
| 70 DCHECK(is_shmem()); | 69 DCHECK(is_shmem()); |
| 71 return shm_handle_; | 70 return shm_handle_; |
| 72 } | 71 } |
| 73 uint32 size() const { | 72 uint32 size() const { |
| 74 DCHECK(is_shmem()); | 73 DCHECK(is_shmem()); |
| 75 return size_; | 74 return size_; |
| 76 } | 75 } |
| 77 const IPC::PlatformFileForTransit& descriptor() const { | 76 const IPC::PlatformFileForTransit& descriptor() const { |
| 78 DCHECK(is_socket() || is_channel_handle() || is_file()); | 77 DCHECK(is_socket() || is_file()); |
| 79 return descriptor_; | 78 return descriptor_; |
| 80 } | 79 } |
| 81 int32 open_flags() const { | 80 int32 open_flags() const { |
| 82 return open_flags_; | 81 return open_flags_; |
| 83 } | 82 } |
| 84 PP_Resource file_io() const { | 83 PP_Resource file_io() const { |
| 85 return file_io_; | 84 return file_io_; |
| 86 } | 85 } |
| 87 void set_shmem(const base::SharedMemoryHandle& handle, uint32 size) { | 86 void set_shmem(const base::SharedMemoryHandle& handle, uint32 size) { |
| 88 type_ = SHARED_MEMORY; | 87 type_ = SHARED_MEMORY; |
| 89 shm_handle_ = handle; | 88 shm_handle_ = handle; |
| 90 size_ = size; | 89 size_ = size; |
| 91 | 90 |
| 92 descriptor_ = IPC::InvalidPlatformFileForTransit(); | 91 descriptor_ = IPC::InvalidPlatformFileForTransit(); |
| 93 } | 92 } |
| 94 void set_socket(const IPC::PlatformFileForTransit& socket) { | 93 void set_socket(const IPC::PlatformFileForTransit& socket) { |
| 95 type_ = SOCKET; | 94 type_ = SOCKET; |
| 96 descriptor_ = socket; | 95 descriptor_ = socket; |
| 97 | 96 |
| 98 shm_handle_ = base::SharedMemory::NULLHandle(); | 97 shm_handle_ = base::SharedMemory::NULLHandle(); |
| 99 size_ = 0; | 98 size_ = 0; |
| 100 } | 99 } |
| 101 void set_channel_handle(const IPC::PlatformFileForTransit& descriptor) { | |
| 102 type_ = CHANNEL_HANDLE; | |
| 103 | |
| 104 descriptor_ = descriptor; | |
| 105 shm_handle_ = base::SharedMemory::NULLHandle(); | |
| 106 size_ = 0; | |
| 107 } | |
| 108 void set_file_handle(const IPC::PlatformFileForTransit& descriptor, | 100 void set_file_handle(const IPC::PlatformFileForTransit& descriptor, |
| 109 int32 open_flags, | 101 int32 open_flags, |
| 110 PP_Resource file_io) { | 102 PP_Resource file_io) { |
| 111 type_ = FILE; | 103 type_ = FILE; |
| 112 | 104 |
| 113 descriptor_ = descriptor; | 105 descriptor_ = descriptor; |
| 114 shm_handle_ = base::SharedMemory::NULLHandle(); | 106 shm_handle_ = base::SharedMemory::NULLHandle(); |
| 115 size_ = 0; | 107 size_ = 0; |
| 116 open_flags_ = open_flags; | 108 open_flags_ = open_flags; |
| 117 file_io_ = file_io; | 109 file_io_ = file_io; |
| 118 } | 110 } |
| 119 void set_null_shmem() { | 111 void set_null_shmem() { |
| 120 set_shmem(base::SharedMemory::NULLHandle(), 0); | 112 set_shmem(base::SharedMemory::NULLHandle(), 0); |
| 121 } | 113 } |
| 122 void set_null_socket() { | 114 void set_null_socket() { |
| 123 set_socket(IPC::InvalidPlatformFileForTransit()); | 115 set_socket(IPC::InvalidPlatformFileForTransit()); |
| 124 } | 116 } |
| 125 void set_null_channel_handle() { | |
| 126 set_channel_handle(IPC::InvalidPlatformFileForTransit()); | |
| 127 } | |
| 128 void set_null_file_handle() { | 117 void set_null_file_handle() { |
| 129 set_file_handle(IPC::InvalidPlatformFileForTransit(), 0, 0); | 118 set_file_handle(IPC::InvalidPlatformFileForTransit(), 0, 0); |
| 130 } | 119 } |
| 131 bool IsHandleValid() const; | 120 bool IsHandleValid() const; |
| 132 | 121 |
| 133 Header header() const { | 122 Header header() const { |
| 134 return Header(type_, size_, open_flags_, file_io_); | 123 return Header(type_, size_, open_flags_, file_io_); |
| 135 } | 124 } |
| 136 | 125 |
| 137 // Closes the handle and sets it to invalid. | 126 // Closes the handle and sets it to invalid. |
| (...skipping 10 matching lines...) Expand all Loading... |
| 148 Type type_; | 137 Type type_; |
| 149 | 138 |
| 150 // We hold more members than we really need; we can't easily use a union, | 139 // We hold more members than we really need; we can't easily use a union, |
| 151 // because we hold non-POD types. But these types are pretty light-weight. If | 140 // because we hold non-POD types. But these types are pretty light-weight. If |
| 152 // we add more complex things later, we should come up with a more memory- | 141 // we add more complex things later, we should come up with a more memory- |
| 153 // efficient strategy. | 142 // efficient strategy. |
| 154 // These are valid if type == SHARED_MEMORY. | 143 // These are valid if type == SHARED_MEMORY. |
| 155 base::SharedMemoryHandle shm_handle_; | 144 base::SharedMemoryHandle shm_handle_; |
| 156 uint32 size_; | 145 uint32 size_; |
| 157 | 146 |
| 158 // This is valid if type == SOCKET || type == CHANNEL_HANDLE || type == FILE. | 147 // This is valid if type == SOCKET || type == FILE. |
| 159 IPC::PlatformFileForTransit descriptor_; | 148 IPC::PlatformFileForTransit descriptor_; |
| 160 | 149 |
| 161 // The following fields are valid if type == FILE. | 150 // The following fields are valid if type == FILE. |
| 162 int32 open_flags_; | 151 int32 open_flags_; |
| 163 // This is non-zero if file writes require quota checking. | 152 // This is non-zero if file writes require quota checking. |
| 164 PP_Resource file_io_; | 153 PP_Resource file_io_; |
| 165 }; | 154 }; |
| 166 | 155 |
| 167 } // namespace proxy | 156 } // namespace proxy |
| 168 } // namespace ppapi | 157 } // namespace ppapi |
| 169 | 158 |
| 170 #endif // PPAPI_PROXY_SERIALIZED_HANDLES_H_ | 159 #endif // PPAPI_PROXY_SERIALIZED_HANDLES_H_ |
| OLD | NEW |