OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 THIRD_PARTY_MOJO_SRC_MOJO_EDK_SYSTEM_TRANSPORT_DATA_H_ | 5 #ifndef MOJO_EDK_SYSTEM_TRANSPORT_DATA_H_ |
6 #define THIRD_PARTY_MOJO_SRC_MOJO_EDK_SYSTEM_TRANSPORT_DATA_H_ | 6 #define MOJO_EDK_SYSTEM_TRANSPORT_DATA_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/memory/aligned_memory.h" | 12 #include "base/memory/aligned_memory.h" |
13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
14 #include "build/build_config.h" | 14 #include "build/build_config.h" |
| 15 #include "mojo/edk/embedder/platform_handle.h" |
| 16 #include "mojo/edk/embedder/platform_handle_vector.h" |
| 17 #include "mojo/edk/system/dispatcher.h" |
| 18 #include "mojo/edk/system/system_impl_export.h" |
15 #include "mojo/public/cpp/system/macros.h" | 19 #include "mojo/public/cpp/system/macros.h" |
16 #include "third_party/mojo/src/mojo/edk/embedder/platform_handle.h" | |
17 #include "third_party/mojo/src/mojo/edk/embedder/platform_handle_vector.h" | |
18 #include "third_party/mojo/src/mojo/edk/system/dispatcher.h" | |
19 #include "third_party/mojo/src/mojo/edk/system/system_impl_export.h" | |
20 | 20 |
21 namespace mojo { | 21 namespace mojo { |
22 namespace system { | 22 namespace edk { |
23 | |
24 class Channel; | |
25 | 23 |
26 // This class is used by |MessageInTransit| to represent handles (|Dispatcher|s) | 24 // This class is used by |MessageInTransit| to represent handles (|Dispatcher|s) |
27 // in various stages of serialization. | 25 // in various stages of serialization. |
28 // | 26 // |
29 // The stages are: | 27 // The stages are: |
30 // - Before reaching |TransportData|: Turn |DispatcherTransport|s into | 28 // - Before reaching |TransportData|: Turn |DispatcherTransport|s into |
31 // |Dispatcher|s that are "owned" by (and attached to) a |MessageInTransit|. | 29 // |Dispatcher|s that are "owned" by (and attached to) a |MessageInTransit|. |
32 // This invalidates the handles in the space of the sending application | 30 // This invalidates the handles in the space of the sending application |
33 // (and, e.g., if another thread is waiting on such a handle, it'll be | 31 // (and, e.g., if another thread is waiting on such a handle, it'll be |
34 // notified of this invalidation). | 32 // notified of this invalidation). |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
82 // The maximum number of platform handles to attach for a single serialized | 80 // The maximum number of platform handles to attach for a single serialized |
83 // dispatcher. | 81 // dispatcher. |
84 static const size_t kMaxSerializedDispatcherPlatformHandles = 2; | 82 static const size_t kMaxSerializedDispatcherPlatformHandles = 2; |
85 | 83 |
86 // The maximum possible size of a valid transport data buffer. | 84 // The maximum possible size of a valid transport data buffer. |
87 static size_t GetMaxBufferSize(); | 85 static size_t GetMaxBufferSize(); |
88 | 86 |
89 // The maximum total number of platform handles that may be attached. | 87 // The maximum total number of platform handles that may be attached. |
90 static size_t GetMaxPlatformHandles(); | 88 static size_t GetMaxPlatformHandles(); |
91 | 89 |
92 TransportData(scoped_ptr<DispatcherVector> dispatchers, Channel* channel); | 90 explicit TransportData(scoped_ptr<DispatcherVector> dispatchers); |
93 | 91 |
94 // This is used for users of |MessageInTransit|/|TransportData|/|RawChannel| | 92 // This is used for users of |MessageInTransit|/|TransportData|/|RawChannel| |
95 // that want to simply transport data and platform handles, and not | 93 // that want to simply transport data and platform handles, and not |
96 // |Dispatcher|s. (|Header| will be present, and zero except for | 94 // |Dispatcher|s. (|Header| will be present, and zero except for |
97 // |num_platform_handles|, and |platform_handle_table_offset| if necessary.) | 95 // |num_platform_handles|, and |platform_handle_table_offset| if necessary.) |
98 explicit TransportData( | 96 explicit TransportData( |
99 embedder::ScopedPlatformHandleVectorPtr platform_handles, | 97 ScopedPlatformHandleVectorPtr platform_handles, |
100 size_t serialized_platform_handle_size); | 98 size_t serialized_platform_handle_size); |
101 | 99 |
102 ~TransportData(); | 100 ~TransportData(); |
103 | 101 |
104 const void* buffer() const { return buffer_.get(); } | 102 const void* buffer() const { return buffer_.get(); } |
105 void* buffer() { return buffer_.get(); } | 103 void* buffer() { return buffer_.get(); } |
106 size_t buffer_size() const { return buffer_size_; } | 104 size_t buffer_size() const { return buffer_size_; } |
107 | 105 |
108 uint32_t platform_handle_table_offset() const { | 106 uint32_t platform_handle_table_offset() const { |
109 return header()->platform_handle_table_offset; | 107 return header()->platform_handle_table_offset; |
110 } | 108 } |
111 | 109 |
112 // Gets attached platform-specific handles; this may return null if there are | 110 // Gets attached platform-specific handles; this may return null if there are |
113 // none. Note that the caller may mutate the set of platform-specific handles. | 111 // none. Note that the caller may mutate the set of platform-specific handles. |
114 const embedder::PlatformHandleVector* platform_handles() const { | 112 const PlatformHandleVector* platform_handles() const { |
115 return platform_handles_.get(); | 113 return platform_handles_.get(); |
116 } | 114 } |
117 embedder::PlatformHandleVector* platform_handles() { | 115 PlatformHandleVector* platform_handles() { |
118 return platform_handles_.get(); | 116 return platform_handles_.get(); |
119 } | 117 } |
120 | 118 |
121 // Receive-side functions: | 119 // Receive-side functions: |
122 | 120 |
123 // Checks if the given buffer (from the "wire") looks like a valid | 121 // Checks if the given buffer (from the "wire") looks like a valid |
124 // |TransportData| buffer. (Should only be called if |buffer_size| is | 122 // |TransportData| buffer. (Should only be called if |buffer_size| is |
125 // nonzero.) Returns null if valid, and a pointer to a human-readable error | 123 // nonzero.) Returns null if valid, and a pointer to a human-readable error |
126 // message (for debug/logging purposes) on error. Note: This checks the | 124 // message (for debug/logging purposes) on error. Note: This checks the |
127 // validity of the handle table entries (i.e., does range checking), but does | 125 // validity of the handle table entries (i.e., does range checking), but does |
128 // not check that the validity of the actual serialized dispatcher | 126 // not check that the validity of the actual serialized dispatcher |
129 // information. | 127 // information. |
130 static const char* ValidateBuffer(size_t serialized_platform_handle_size, | 128 static const char* ValidateBuffer(size_t serialized_platform_handle_size, |
131 const void* buffer, | 129 const void* buffer, |
132 size_t buffer_size); | 130 size_t buffer_size); |
133 | 131 |
134 // Gets the platform handle table from a (valid) |TransportData| buffer (which | 132 // Gets the platform handle table from a (valid) |TransportData| buffer (which |
135 // should have been validated using |ValidateBuffer()| first). | 133 // should have been validated using |ValidateBuffer()| first). |
136 static void GetPlatformHandleTable(const void* transport_data_buffer, | 134 static void GetPlatformHandleTable(const void* transport_data_buffer, |
137 size_t* num_platform_handles, | 135 size_t* num_platform_handles, |
138 const void** platform_handle_table); | 136 const void** platform_handle_table); |
139 | 137 |
140 // Deserializes dispatchers from the given (serialized) transport data buffer | 138 // Deserializes dispatchers from the given (serialized) transport data buffer |
141 // (typically from a |MessageInTransit::View|) and vector of platform handles. | 139 // (typically from a |MessageInTransit::View|) and vector of platform handles. |
142 // |buffer| should be non-null and |buffer_size| should be nonzero. | 140 // |buffer| should be non-null and |buffer_size| should be nonzero. |
143 static scoped_ptr<DispatcherVector> DeserializeDispatchers( | 141 static scoped_ptr<DispatcherVector> DeserializeDispatchers( |
144 const void* buffer, | 142 const void* buffer, |
145 size_t buffer_size, | 143 size_t buffer_size, |
146 embedder::ScopedPlatformHandleVectorPtr platform_handles, | 144 ScopedPlatformHandleVectorPtr platform_handles); |
147 Channel* channel); | |
148 | 145 |
149 private: | 146 private: |
150 // To allow us to make compile-assertions about |Header|, etc. in the .cc | 147 // To allow us to make compile-assertions about |Header|, etc. in the .cc |
151 // file. | 148 // file. |
152 struct PrivateStructForCompileAsserts; | 149 struct PrivateStructForCompileAsserts; |
153 | 150 |
154 // Header for the "secondary buffer"/"transport data". Must be a multiple of | 151 // Header for the "secondary buffer"/"transport data". Must be a multiple of |
155 // |MessageInTransit::kMessageAlignment| in size. Must be POD. | 152 // |MessageInTransit::kMessageAlignment| in size. Must be POD. |
156 struct Header { | 153 struct Header { |
157 uint32_t num_handles; | 154 uint32_t num_handles; |
(...skipping 15 matching lines...) Expand all Loading... |
173 return reinterpret_cast<const Header*>(buffer_.get()); | 170 return reinterpret_cast<const Header*>(buffer_.get()); |
174 } | 171 } |
175 | 172 |
176 size_t buffer_size_; | 173 size_t buffer_size_; |
177 scoped_ptr<char, base::AlignedFreeDeleter> buffer_; // Never null. | 174 scoped_ptr<char, base::AlignedFreeDeleter> buffer_; // Never null. |
178 | 175 |
179 // Any platform-specific handles attached to this message (for inter-process | 176 // Any platform-specific handles attached to this message (for inter-process |
180 // transport). The vector (if any) owns the handles that it contains (and is | 177 // transport). The vector (if any) owns the handles that it contains (and is |
181 // responsible for closing them). | 178 // responsible for closing them). |
182 // TODO(vtl): With C++11, change it to a vector of |ScopedPlatformHandle|s. | 179 // TODO(vtl): With C++11, change it to a vector of |ScopedPlatformHandle|s. |
183 embedder::ScopedPlatformHandleVectorPtr platform_handles_; | 180 ScopedPlatformHandleVectorPtr platform_handles_; |
184 | 181 |
185 MOJO_DISALLOW_COPY_AND_ASSIGN(TransportData); | 182 MOJO_DISALLOW_COPY_AND_ASSIGN(TransportData); |
186 }; | 183 }; |
187 | 184 |
188 } // namespace system | 185 } // namespace edk |
189 } // namespace mojo | 186 } // namespace mojo |
190 | 187 |
191 #endif // THIRD_PARTY_MOJO_SRC_MOJO_EDK_SYSTEM_TRANSPORT_DATA_H_ | 188 #endif // MOJO_EDK_SYSTEM_TRANSPORT_DATA_H_ |
OLD | NEW |