| 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 #include "mojo/system/transport_data.h" | 5 #include "mojo/system/transport_data.h" |
| 6 | 6 |
| 7 #include <string.h> | 7 #include <string.h> |
| 8 | 8 |
| 9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 28 matching lines...) Expand all Loading... |
| 39 COMPILE_ASSERT(kMaxSerializedDispatcherSize % | 39 COMPILE_ASSERT(kMaxSerializedDispatcherSize % |
| 40 MessageInTransit::kMessageAlignment == 0, | 40 MessageInTransit::kMessageAlignment == 0, |
| 41 kMaxSerializedDispatcherSize_not_a_multiple_of_alignment); | 41 kMaxSerializedDispatcherSize_not_a_multiple_of_alignment); |
| 42 | 42 |
| 43 // The size of |HandleTableEntry| must be a multiple of the alignment. | 43 // The size of |HandleTableEntry| must be a multiple of the alignment. |
| 44 COMPILE_ASSERT(sizeof(HandleTableEntry) % | 44 COMPILE_ASSERT(sizeof(HandleTableEntry) % |
| 45 MessageInTransit::kMessageAlignment == 0, | 45 MessageInTransit::kMessageAlignment == 0, |
| 46 sizeof_MessageInTransit_HandleTableEntry_invalid); | 46 sizeof_MessageInTransit_HandleTableEntry_invalid); |
| 47 }; | 47 }; |
| 48 | 48 |
| 49 TransportData::TransportData( | 49 TransportData::TransportData(scoped_ptr<DispatcherVector> dispatchers, |
| 50 scoped_ptr<std::vector<scoped_refptr<Dispatcher> > > dispatchers, | 50 Channel* channel) |
| 51 Channel* channel) | |
| 52 : buffer_size_(0) { | 51 : buffer_size_(0) { |
| 53 DCHECK(dispatchers); | 52 DCHECK(dispatchers); |
| 54 DCHECK(channel); | 53 DCHECK(channel); |
| 55 | 54 |
| 56 const size_t num_handles = dispatchers->size(); | 55 const size_t num_handles = dispatchers->size(); |
| 57 DCHECK_GT(num_handles, 0u); | 56 DCHECK_GT(num_handles, 0u); |
| 58 | 57 |
| 59 // The offset to the start of the (Mojo) handle table. | 58 // The offset to the start of the (Mojo) handle table. |
| 60 const size_t handle_table_start_offset = sizeof(Header); | 59 const size_t handle_table_start_offset = sizeof(Header); |
| 61 // The offset to the start of the serialized dispatcher data. | 60 // The offset to the start of the serialized dispatcher data. |
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 212 // Note: This is an overflow-safe check for |offset + size > buffer_size| | 211 // Note: This is an overflow-safe check for |offset + size > buffer_size| |
| 213 // (we know that |size <= buffer_size| from the previous check). | 212 // (we know that |size <= buffer_size| from the previous check). |
| 214 if (offset > buffer_size - size) | 213 if (offset > buffer_size - size) |
| 215 return kInvalidSerializedDispatcher; | 214 return kInvalidSerializedDispatcher; |
| 216 } | 215 } |
| 217 | 216 |
| 218 return NULL; | 217 return NULL; |
| 219 } | 218 } |
| 220 | 219 |
| 221 // static | 220 // static |
| 222 scoped_ptr<std::vector<scoped_refptr<Dispatcher> > > | 221 scoped_ptr<DispatcherVector> TransportData::DeserializeDispatchersFromBuffer( |
| 223 TransportData::DeserializeDispatchersFromBuffer(const void* buffer, | 222 const void* buffer, |
| 224 size_t buffer_size, | 223 size_t buffer_size, |
| 225 Channel* channel) { | 224 Channel* channel) { |
| 226 DCHECK(buffer); | 225 DCHECK(buffer); |
| 227 DCHECK_GT(buffer_size, 0u); | 226 DCHECK_GT(buffer_size, 0u); |
| 228 DCHECK(channel); | 227 DCHECK(channel); |
| 229 | 228 |
| 230 const Header* header = static_cast<const Header*>(buffer); | 229 const Header* header = static_cast<const Header*>(buffer); |
| 231 const size_t num_handles = header->num_handles; | 230 const size_t num_handles = header->num_handles; |
| 232 scoped_ptr<std::vector<scoped_refptr<Dispatcher> > > dispatchers( | 231 scoped_ptr<DispatcherVector> dispatchers(new DispatcherVector(num_handles)); |
| 233 new std::vector<scoped_refptr<Dispatcher> >(num_handles)); | |
| 234 | 232 |
| 235 const HandleTableEntry* handle_table = | 233 const HandleTableEntry* handle_table = |
| 236 reinterpret_cast<const HandleTableEntry*>( | 234 reinterpret_cast<const HandleTableEntry*>( |
| 237 static_cast<const char*>(buffer) + sizeof(Header)); | 235 static_cast<const char*>(buffer) + sizeof(Header)); |
| 238 for (size_t i = 0; i < num_handles; i++) { | 236 for (size_t i = 0; i < num_handles; i++) { |
| 239 size_t offset = handle_table[i].offset; | 237 size_t offset = handle_table[i].offset; |
| 240 size_t size = handle_table[i].size; | 238 size_t size = handle_table[i].size; |
| 241 // Should already have been checked by |ValidateBuffer()|: | 239 // Should already have been checked by |ValidateBuffer()|: |
| 242 DCHECK_EQ(offset % MessageInTransit::kMessageAlignment, 0u); | 240 DCHECK_EQ(offset % MessageInTransit::kMessageAlignment, 0u); |
| 243 DCHECK_LE(offset, buffer_size); | 241 DCHECK_LE(offset, buffer_size); |
| 244 DCHECK_LE(offset + size, buffer_size); | 242 DCHECK_LE(offset + size, buffer_size); |
| 245 | 243 |
| 246 const void* source = static_cast<const char*>(buffer) + offset; | 244 const void* source = static_cast<const char*>(buffer) + offset; |
| 247 (*dispatchers)[i] = Dispatcher::TransportDataAccess::Deserialize( | 245 (*dispatchers)[i] = Dispatcher::TransportDataAccess::Deserialize( |
| 248 channel, handle_table[i].type, source, size); | 246 channel, handle_table[i].type, source, size); |
| 249 } | 247 } |
| 250 | 248 |
| 251 return dispatchers.Pass(); | 249 return dispatchers.Pass(); |
| 252 } | 250 } |
| 253 | 251 |
| 254 } // namespace system | 252 } // namespace system |
| 255 } // namespace mojo | 253 } // namespace mojo |
| OLD | NEW |