| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 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 #include "third_party/mojo/src/mojo/edk/system/message_in_transit.h" | 5 #include "mojo/edk/system/message_in_transit.h" |
| 6 | 6 |
| 7 #include <string.h> | 7 #include <string.h> |
| 8 | 8 |
| 9 #include <ostream> | 9 #include <ostream> |
| 10 | 10 |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "third_party/mojo/src/mojo/edk/system/configuration.h" | 12 #include "mojo/edk/system/configuration.h" |
| 13 #include "third_party/mojo/src/mojo/edk/system/transport_data.h" | 13 #include "mojo/edk/system/transport_data.h" |
| 14 | 14 |
| 15 namespace mojo { | 15 namespace mojo { |
| 16 namespace system { | 16 namespace edk { |
| 17 | 17 |
| 18 MOJO_STATIC_CONST_MEMBER_DEFINITION const size_t | 18 MOJO_STATIC_CONST_MEMBER_DEFINITION const size_t |
| 19 MessageInTransit::kMessageAlignment; | 19 MessageInTransit::kMessageAlignment; |
| 20 | 20 |
| 21 struct MessageInTransit::PrivateStructForCompileAsserts { | 21 struct MessageInTransit::PrivateStructForCompileAsserts { |
| 22 // The size of |Header| must be a multiple of the alignment. | 22 // The size of |Header| must be a multiple of the alignment. |
| 23 static_assert(sizeof(Header) % kMessageAlignment == 0, | 23 static_assert(sizeof(Header) % kMessageAlignment == 0, |
| 24 "sizeof(MessageInTransit::Header) invalid"); | 24 "sizeof(MessageInTransit::Header) invalid"); |
| 25 }; | 25 }; |
| 26 | 26 |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 62 if (e) { | 62 if (e) { |
| 63 *error_message = e; | 63 *error_message = e; |
| 64 return false; | 64 return false; |
| 65 } | 65 } |
| 66 } | 66 } |
| 67 | 67 |
| 68 return true; | 68 return true; |
| 69 } | 69 } |
| 70 | 70 |
| 71 MessageInTransit::MessageInTransit(Type type, | 71 MessageInTransit::MessageInTransit(Type type, |
| 72 Subtype subtype, | |
| 73 uint32_t num_bytes, | 72 uint32_t num_bytes, |
| 74 const void* bytes) | 73 const void* bytes) |
| 75 : main_buffer_size_(RoundUpMessageAlignment(sizeof(Header) + num_bytes)), | 74 : main_buffer_size_(RoundUpMessageAlignment(sizeof(Header) + num_bytes)), |
| 76 main_buffer_(static_cast<char*>( | 75 main_buffer_(static_cast<char*>( |
| 77 base::AlignedAlloc(main_buffer_size_, kMessageAlignment))) { | 76 base::AlignedAlloc(main_buffer_size_, kMessageAlignment))) { |
| 78 ConstructorHelper(type, subtype, num_bytes); | 77 ConstructorHelper(type, num_bytes); |
| 79 if (bytes) { | 78 if (bytes) { |
| 80 memcpy(MessageInTransit::bytes(), bytes, num_bytes); | 79 memcpy(MessageInTransit::bytes(), bytes, num_bytes); |
| 81 memset(static_cast<char*>(MessageInTransit::bytes()) + num_bytes, 0, | 80 memset(static_cast<char*>(MessageInTransit::bytes()) + num_bytes, 0, |
| 82 main_buffer_size_ - sizeof(Header) - num_bytes); | 81 main_buffer_size_ - sizeof(Header) - num_bytes); |
| 83 } else { | 82 } else { |
| 84 memset(MessageInTransit::bytes(), 0, main_buffer_size_ - sizeof(Header)); | 83 memset(MessageInTransit::bytes(), 0, main_buffer_size_ - sizeof(Header)); |
| 85 } | 84 } |
| 86 } | 85 } |
| 87 | 86 |
| 88 MessageInTransit::MessageInTransit(Type type, | |
| 89 Subtype subtype, | |
| 90 uint32_t num_bytes, | |
| 91 UserPointer<const void> bytes) | |
| 92 : main_buffer_size_(RoundUpMessageAlignment(sizeof(Header) + num_bytes)), | |
| 93 main_buffer_(static_cast<char*>( | |
| 94 base::AlignedAlloc(main_buffer_size_, kMessageAlignment))) { | |
| 95 ConstructorHelper(type, subtype, num_bytes); | |
| 96 bytes.GetArray(MessageInTransit::bytes(), num_bytes); | |
| 97 memset(static_cast<char*>(MessageInTransit::bytes()) + num_bytes, 0, | |
| 98 main_buffer_size_ - sizeof(Header) - num_bytes); | |
| 99 } | |
| 100 | |
| 101 MessageInTransit::MessageInTransit(const View& message_view) | 87 MessageInTransit::MessageInTransit(const View& message_view) |
| 102 : main_buffer_size_(message_view.main_buffer_size()), | 88 : main_buffer_size_(message_view.main_buffer_size()), |
| 103 main_buffer_(static_cast<char*>( | 89 main_buffer_(static_cast<char*>( |
| 104 base::AlignedAlloc(main_buffer_size_, kMessageAlignment))) { | 90 base::AlignedAlloc(main_buffer_size_, kMessageAlignment))) { |
| 105 DCHECK_GE(main_buffer_size_, sizeof(Header)); | 91 DCHECK_GE(main_buffer_size_, sizeof(Header)); |
| 106 DCHECK_EQ(main_buffer_size_ % kMessageAlignment, 0u); | 92 DCHECK_EQ(main_buffer_size_ % kMessageAlignment, 0u); |
| 107 | 93 |
| 108 memcpy(main_buffer_.get(), message_view.main_buffer(), main_buffer_size_); | 94 memcpy(main_buffer_.get(), message_view.main_buffer(), main_buffer_size_); |
| 109 DCHECK_EQ(main_buffer_size_, | 95 DCHECK_EQ(main_buffer_size_, |
| 110 RoundUpMessageAlignment(sizeof(Header) + num_bytes())); | 96 RoundUpMessageAlignment(sizeof(Header) + num_bytes())); |
| 111 } | 97 } |
| 112 | 98 |
| 113 MessageInTransit::~MessageInTransit() { | 99 MessageInTransit::~MessageInTransit() { |
| 114 if (dispatchers_) { | 100 if (dispatchers_) { |
| 115 for (size_t i = 0; i < dispatchers_->size(); i++) { | 101 for (size_t i = 0; i < dispatchers_->size(); i++) { |
| 116 if (!(*dispatchers_)[i]) | 102 if (!(*dispatchers_)[i]) |
| 117 continue; | 103 continue; |
| 118 | |
| 119 DCHECK((*dispatchers_)[i]->HasOneRef()); | |
| 120 (*dispatchers_)[i]->Close(); | 104 (*dispatchers_)[i]->Close(); |
| 121 } | 105 } |
| 122 } | 106 } |
| 123 } | 107 } |
| 124 | 108 |
| 125 // static | 109 // static |
| 126 bool MessageInTransit::GetNextMessageSize(const void* buffer, | 110 bool MessageInTransit::GetNextMessageSize(const void* buffer, |
| 127 size_t buffer_size, | 111 size_t buffer_size, |
| 128 size_t* next_message_size) { | 112 size_t* next_message_size) { |
| 129 DCHECK(next_message_size); | 113 DCHECK(next_message_size); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 143 return true; | 127 return true; |
| 144 } | 128 } |
| 145 | 129 |
| 146 void MessageInTransit::SetDispatchers( | 130 void MessageInTransit::SetDispatchers( |
| 147 scoped_ptr<DispatcherVector> dispatchers) { | 131 scoped_ptr<DispatcherVector> dispatchers) { |
| 148 DCHECK(dispatchers); | 132 DCHECK(dispatchers); |
| 149 DCHECK(!dispatchers_); | 133 DCHECK(!dispatchers_); |
| 150 DCHECK(!transport_data_); | 134 DCHECK(!transport_data_); |
| 151 | 135 |
| 152 dispatchers_ = dispatchers.Pass(); | 136 dispatchers_ = dispatchers.Pass(); |
| 153 #ifndef NDEBUG | |
| 154 for (size_t i = 0; i < dispatchers_->size(); i++) | |
| 155 DCHECK(!(*dispatchers_)[i] || (*dispatchers_)[i]->HasOneRef()); | |
| 156 #endif | |
| 157 } | 137 } |
| 158 | 138 |
| 159 void MessageInTransit::SetTransportData( | 139 void MessageInTransit::SetTransportData( |
| 160 scoped_ptr<TransportData> transport_data) { | 140 scoped_ptr<TransportData> transport_data) { |
| 161 DCHECK(transport_data); | 141 DCHECK(transport_data); |
| 162 DCHECK(!transport_data_); | 142 DCHECK(!transport_data_); |
| 163 DCHECK(!dispatchers_); | 143 DCHECK(!dispatchers_); |
| 164 | 144 |
| 165 transport_data_ = transport_data.Pass(); | 145 transport_data_ = transport_data.Pass(); |
| 166 UpdateTotalSize(); | 146 UpdateTotalSize(); |
| 167 } | 147 } |
| 168 | 148 |
| 169 void MessageInTransit::SerializeAndCloseDispatchers(Channel* channel) { | 149 void MessageInTransit::SerializeAndCloseDispatchers() { |
| 170 DCHECK(channel); | |
| 171 DCHECK(!transport_data_); | 150 DCHECK(!transport_data_); |
| 172 | 151 |
| 173 if (!dispatchers_ || !dispatchers_->size()) | 152 if (!dispatchers_ || !dispatchers_->size()) |
| 174 return; | 153 return; |
| 175 | 154 |
| 176 transport_data_.reset(new TransportData(dispatchers_.Pass(), channel)); | 155 transport_data_.reset(new TransportData(dispatchers_.Pass())); |
| 177 | 156 |
| 178 // Update the sizes in the message header. | 157 // Update the sizes in the message header. |
| 179 UpdateTotalSize(); | 158 UpdateTotalSize(); |
| 180 } | 159 } |
| 181 | 160 |
| 182 void MessageInTransit::ConstructorHelper(Type type, | 161 void MessageInTransit::ConstructorHelper(Type type, |
| 183 Subtype subtype, | |
| 184 uint32_t num_bytes) { | 162 uint32_t num_bytes) { |
| 185 DCHECK_LE(num_bytes, GetConfiguration().max_message_num_bytes); | 163 DCHECK_LE(num_bytes, GetConfiguration().max_message_num_bytes); |
| 186 | 164 |
| 187 // |total_size| is updated below, from the other values. | 165 // |total_size| is updated below, from the other values. |
| 188 header()->type = type; | 166 header()->type = type; |
| 189 header()->subtype = subtype; | |
| 190 header()->source_id = ChannelEndpointId(); | |
| 191 header()->destination_id = ChannelEndpointId(); | |
| 192 header()->num_bytes = num_bytes; | 167 header()->num_bytes = num_bytes; |
| 193 header()->unused = 0; | 168 header()->unused = 0; |
| 194 // Note: If dispatchers are subsequently attached, then |total_size| will have | 169 // Note: If dispatchers are subsequently attached, then |total_size| will have |
| 195 // to be adjusted. | 170 // to be adjusted. |
| 196 UpdateTotalSize(); | 171 UpdateTotalSize(); |
| 197 } | 172 } |
| 198 | 173 |
| 199 void MessageInTransit::UpdateTotalSize() { | 174 void MessageInTransit::UpdateTotalSize() { |
| 200 DCHECK_EQ(main_buffer_size_ % kMessageAlignment, 0u); | 175 DCHECK_EQ(main_buffer_size_ % kMessageAlignment, 0u); |
| 201 header()->total_size = static_cast<uint32_t>(main_buffer_size_); | 176 header()->total_size = static_cast<uint32_t>(main_buffer_size_); |
| 202 if (transport_data_) { | 177 if (transport_data_) { |
| 203 header()->total_size += | 178 header()->total_size += |
| 204 static_cast<uint32_t>(transport_data_->buffer_size()); | 179 static_cast<uint32_t>(transport_data_->buffer_size()); |
| 205 } | 180 } |
| 206 } | 181 } |
| 207 | 182 |
| 208 } // namespace system | 183 } // namespace edk |
| 209 } // namespace mojo | 184 } // namespace mojo |
| OLD | NEW |