OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 #include "mojo/edk/system/channel.h" | 5 #include "mojo/edk/system/channel.h" |
6 | 6 |
7 #include <string.h> | 7 #include <string.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <limits> | 10 #include <limits> |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
117 // On POSIX the ScopedPlatformHandleVectorPtr will do this for us. | 117 // On POSIX the ScopedPlatformHandleVectorPtr will do this for us. |
118 for (size_t i = 0; i < header_->num_handles; ++i) | 118 for (size_t i = 0; i < header_->num_handles; ++i) |
119 handles()[i].CloseIfNecessary(); | 119 handles()[i].CloseIfNecessary(); |
120 #endif | 120 #endif |
121 base::AlignedFree(data_); | 121 base::AlignedFree(data_); |
122 } | 122 } |
123 | 123 |
124 // static | 124 // static |
125 Channel::MessagePtr Channel::Message::Deserialize(const void* data, | 125 Channel::MessagePtr Channel::Message::Deserialize(const void* data, |
126 size_t data_num_bytes) { | 126 size_t data_num_bytes) { |
127 #if !defined(OS_WIN) && !(defined(OS_MACOSX) && !defined(OS_IOS)) | |
128 // We only serialize messages into other messages when performing message | |
129 // relay on Windows and OSX. | |
130 NOTREACHED(); | |
131 return nullptr; | |
132 #else | |
133 if (data_num_bytes < sizeof(Header)) | 127 if (data_num_bytes < sizeof(Header)) |
134 return nullptr; | 128 return nullptr; |
135 | 129 |
136 const Header* header = reinterpret_cast<const Header*>(data); | 130 const Header* header = reinterpret_cast<const Header*>(data); |
137 if (header->num_bytes != data_num_bytes) { | 131 if (header->num_bytes != data_num_bytes) { |
138 DLOG(ERROR) << "Decoding invalid message: " << header->num_bytes | 132 DLOG(ERROR) << "Decoding invalid message: " << header->num_bytes |
139 << " != " << data_num_bytes; | 133 << " != " << data_num_bytes; |
140 return nullptr; | 134 return nullptr; |
141 } | 135 } |
142 | 136 |
| 137 #if !defined(OS_ANDROID) && !defined(OS_CHROMEOS) |
143 if (header->num_bytes < header->num_header_bytes) { | 138 if (header->num_bytes < header->num_header_bytes) { |
144 DLOG(ERROR) << "Decoding invalid message: " << header->num_bytes << " < " | 139 DLOG(ERROR) << "Decoding invalid message: " << header->num_bytes << " < " |
145 << header->num_header_bytes; | 140 << header->num_header_bytes; |
146 return nullptr; | 141 return nullptr; |
147 } | 142 } |
148 | 143 |
149 uint32_t extra_header_size = header->num_header_bytes - sizeof(Header); | 144 uint32_t extra_header_size = header->num_header_bytes - sizeof(Header); |
| 145 size_t payload_size = data_num_bytes - header->num_header_bytes; |
| 146 const char* payload = |
| 147 static_cast<const char*>(data) + header->num_header_bytes; |
150 #if defined(OS_WIN) | 148 #if defined(OS_WIN) |
151 uint32_t max_handles = extra_header_size / sizeof(PlatformHandle); | 149 uint32_t max_handles = extra_header_size / sizeof(PlatformHandle); |
152 #elif defined(OS_MACOSX) && !defined(OS_IOS) | 150 #elif defined(OS_MACOSX) && !defined(OS_IOS) |
153 uint32_t max_handles = extra_header_size / sizeof(MachPortsEntry); | 151 uint32_t max_handles = extra_header_size / sizeof(MachPortsEntry); |
| 152 #else |
| 153 const uint32_t max_handles = 0; |
154 #endif | 154 #endif |
| 155 |
| 156 #else // !defined(OS_ANDROID) && !defined(OS_CHROMEOS) |
| 157 // We only serialize messages with handles into other messages when performing |
| 158 // message relay on Windows and OSX. |
| 159 const uint32_t max_handles = 0; |
| 160 const char* payload = static_cast<const char*>(data) + sizeof(Header); |
| 161 size_t payload_size = data_num_bytes; |
| 162 #endif // !defined(OS_ANDROID) && !defined(OS_CHROMEOS) |
| 163 |
155 if (header->num_handles > max_handles) { | 164 if (header->num_handles > max_handles) { |
156 DLOG(ERROR) << "Decoding invalid message:" << header->num_handles | 165 DLOG(ERROR) << "Decoding invalid message:" << header->num_handles |
157 << " > " << max_handles; | 166 << " > " << max_handles; |
158 return nullptr; | 167 return nullptr; |
159 } | 168 } |
160 | 169 |
161 MessagePtr message(new Message(data_num_bytes - header->num_header_bytes, | 170 MessagePtr message(new Message(payload_size, max_handles)); |
162 max_handles)); | |
163 DCHECK_EQ(message->data_num_bytes(), data_num_bytes); | 171 DCHECK_EQ(message->data_num_bytes(), data_num_bytes); |
| 172 |
| 173 // Copy all payload bytes. |
| 174 if (payload_size) |
| 175 memcpy(message->mutable_payload(), payload, payload_size); |
| 176 |
| 177 #if !defined(OS_ANDROID) && !defined(OS_CHROMEOS) |
164 DCHECK_EQ(message->extra_header_size(), extra_header_size); | 178 DCHECK_EQ(message->extra_header_size(), extra_header_size); |
165 DCHECK_EQ(message->header_->num_header_bytes, header->num_header_bytes); | 179 DCHECK_EQ(message->header_->num_header_bytes, header->num_header_bytes); |
166 | 180 |
167 if (data_num_bytes > header->num_header_bytes) { | |
168 // Copy all payload bytes. | |
169 memcpy(message->mutable_payload(), | |
170 static_cast<const char*>(data) + header->num_header_bytes, | |
171 data_num_bytes - header->num_header_bytes); | |
172 } | |
173 | |
174 if (message->extra_header_size()) { | 181 if (message->extra_header_size()) { |
175 // Copy extra header bytes. | 182 // Copy extra header bytes. |
176 memcpy(message->mutable_extra_header(), | 183 memcpy(message->mutable_extra_header(), |
177 static_cast<const char*>(data) + sizeof(Header), | 184 static_cast<const char*>(data) + sizeof(Header), |
178 message->extra_header_size()); | 185 message->extra_header_size()); |
179 } | 186 } |
| 187 #endif // !defined(OS_ANDROID) && !defined(OS_CHROMEOS) |
180 | 188 |
181 message->header_->num_handles = header->num_handles; | 189 message->header_->num_handles = header->num_handles; |
182 | 190 |
183 return message; | 191 return message; |
184 #endif | |
185 } | 192 } |
186 | 193 |
187 size_t Channel::Message::payload_size() const { | 194 size_t Channel::Message::payload_size() const { |
188 #if defined(OS_CHROMEOS) || defined(OS_ANDROID) | 195 #if defined(OS_CHROMEOS) || defined(OS_ANDROID) |
189 return header_->num_bytes - sizeof(Header); | 196 return header_->num_bytes - sizeof(Header); |
190 #else | 197 #else |
191 return size_ - header_->num_header_bytes; | 198 return size_ - header_->num_header_bytes; |
192 #endif | 199 #endif |
193 } | 200 } |
194 | 201 |
(...skipping 394 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
589 | 596 |
590 bool Channel::OnControlMessage(Message::Header::MessageType message_type, | 597 bool Channel::OnControlMessage(Message::Header::MessageType message_type, |
591 const void* payload, | 598 const void* payload, |
592 size_t payload_size, | 599 size_t payload_size, |
593 ScopedPlatformHandleVectorPtr handles) { | 600 ScopedPlatformHandleVectorPtr handles) { |
594 return false; | 601 return false; |
595 } | 602 } |
596 | 603 |
597 } // namespace edk | 604 } // namespace edk |
598 } // namespace mojo | 605 } // namespace mojo |
OLD | NEW |