| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "ipc/ipc_message.h" | 5 #include "ipc/ipc_message.h" |
| 6 | 6 |
| 7 #include <limits.h> | 7 #include <limits.h> |
| 8 | 8 |
| 9 #include "base/atomic_sequence_num.h" | 9 #include "base/atomic_sequence_num.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 42 namespace IPC { | 42 namespace IPC { |
| 43 | 43 |
| 44 //------------------------------------------------------------------------------ | 44 //------------------------------------------------------------------------------ |
| 45 | 45 |
| 46 Message::~Message() { | 46 Message::~Message() { |
| 47 } | 47 } |
| 48 | 48 |
| 49 Message::Message() : base::Pickle(sizeof(Header)) { | 49 Message::Message() : base::Pickle(sizeof(Header)) { |
| 50 header()->routing = header()->type = 0; | 50 header()->routing = header()->type = 0; |
| 51 header()->flags = GetRefNumUpper24(); | 51 header()->flags = GetRefNumUpper24(); |
| 52 #if USE_ATTACHMENT_BROKER |
| 53 header()->num_brokered_attachments = 0; |
| 54 #endif |
| 52 #if defined(OS_POSIX) | 55 #if defined(OS_POSIX) |
| 53 header()->num_fds = 0; | 56 header()->num_fds = 0; |
| 54 header()->pad = 0; | 57 header()->pad = 0; |
| 55 #endif | 58 #endif |
| 56 Init(); | 59 Init(); |
| 57 } | 60 } |
| 58 | 61 |
| 59 Message::Message(int32_t routing_id, uint32_t type, PriorityValue priority) | 62 Message::Message(int32_t routing_id, uint32_t type, PriorityValue priority) |
| 60 : base::Pickle(sizeof(Header)) { | 63 : base::Pickle(sizeof(Header)) { |
| 61 header()->routing = routing_id; | 64 header()->routing = routing_id; |
| 62 header()->type = type; | 65 header()->type = type; |
| 63 DCHECK((priority & 0xffffff00) == 0); | 66 DCHECK((priority & 0xffffff00) == 0); |
| 64 header()->flags = priority | GetRefNumUpper24(); | 67 header()->flags = priority | GetRefNumUpper24(); |
| 68 #if USE_ATTACHMENT_BROKER |
| 69 header()->num_brokered_attachments = 0; |
| 70 #endif |
| 65 #if defined(OS_POSIX) | 71 #if defined(OS_POSIX) |
| 66 header()->num_fds = 0; | 72 header()->num_fds = 0; |
| 67 header()->pad = 0; | 73 header()->pad = 0; |
| 68 #endif | 74 #endif |
| 69 Init(); | 75 Init(); |
| 70 } | 76 } |
| 71 | 77 |
| 72 Message::Message(const char* data, int data_len) | 78 Message::Message(const char* data, int data_len) |
| 73 : base::Pickle(data, data_len) { | 79 : base::Pickle(data, data_len) { |
| 74 Init(); | 80 Init(); |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 142 const char* pickle_end = | 148 const char* pickle_end = |
| 143 base::Pickle::FindNext(sizeof(Header), range_start, range_end); | 149 base::Pickle::FindNext(sizeof(Header), range_start, range_end); |
| 144 if (!pickle_end) | 150 if (!pickle_end) |
| 145 return; | 151 return; |
| 146 info->pickle_end = pickle_end; | 152 info->pickle_end = pickle_end; |
| 147 | 153 |
| 148 #if USE_ATTACHMENT_BROKER | 154 #if USE_ATTACHMENT_BROKER |
| 149 // The data is not copied. | 155 // The data is not copied. |
| 150 size_t pickle_len = static_cast<size_t>(pickle_end - range_start); | 156 size_t pickle_len = static_cast<size_t>(pickle_end - range_start); |
| 151 Message message(range_start, static_cast<int>(pickle_len)); | 157 Message message(range_start, static_cast<int>(pickle_len)); |
| 152 int num_attachments = message.header()->num_brokered_attachments; | 158 size_t num_attachments = message.header()->num_brokered_attachments; |
| 153 | 159 |
| 154 // Check for possible overflows. | 160 // Check for possible overflows. |
| 155 size_t max_size_t = std::numeric_limits<size_t>::max(); | 161 size_t max_size_t = std::numeric_limits<size_t>::max(); |
| 156 if (num_attachments >= max_size_t / BrokerableAttachment::kNonceSize) | 162 if (num_attachments >= max_size_t / BrokerableAttachment::kNonceSize) |
| 157 return; | 163 return; |
| 158 | 164 |
| 159 size_t attachment_length = num_attachments * BrokerableAttachment::kNonceSize; | 165 size_t attachment_length = num_attachments * BrokerableAttachment::kNonceSize; |
| 160 if (pickle_len > max_size_t - attachment_length) | 166 if (pickle_len > max_size_t - attachment_length) |
| 161 return; | 167 return; |
| 162 | 168 |
| 163 // Check whether the range includes the attachments. | 169 // Check whether the range includes the attachments. |
| 164 size_t buffer_length = static_cast<size_t>(range_end - range_start); | 170 size_t buffer_length = static_cast<size_t>(range_end - range_start); |
| 165 if (buffer_length < attachment_length + pickle_len) | 171 if (buffer_length < attachment_length + pickle_len) |
| 166 return; | 172 return; |
| 167 | 173 |
| 168 for (int i = 0; i < num_attachments; ++i) { | 174 for (size_t i = 0; i < num_attachments; ++i) { |
| 169 const char* attachment_start = | 175 const char* attachment_start = |
| 170 pickle_end + i * BrokerableAttachment::kNonceSize; | 176 pickle_end + i * BrokerableAttachment::kNonceSize; |
| 171 BrokerableAttachment::AttachmentId id(attachment_start, | 177 BrokerableAttachment::AttachmentId id(attachment_start, |
| 172 BrokerableAttachment::kNonceSize); | 178 BrokerableAttachment::kNonceSize); |
| 173 info->attachment_ids.push_back(id); | 179 info->attachment_ids.push_back(id); |
| 174 } | 180 } |
| 175 info->message_end = | 181 info->message_end = |
| 176 pickle_end + num_attachments * BrokerableAttachment::kNonceSize; | 182 pickle_end + num_attachments * BrokerableAttachment::kNonceSize; |
| 177 #else | 183 #else |
| 178 info->message_end = pickle_end; | 184 info->message_end = pickle_end; |
| 179 #endif // USE_ATTACHMENT_BROKER | 185 #endif // USE_ATTACHMENT_BROKER |
| 180 | 186 |
| 181 info->message_found = true; | 187 info->message_found = true; |
| 182 } | 188 } |
| 183 | 189 |
| 184 bool Message::AddPlaceholderBrokerableAttachmentWithId( | 190 bool Message::AddPlaceholderBrokerableAttachmentWithId( |
| 185 BrokerableAttachment::AttachmentId id) { | 191 BrokerableAttachment::AttachmentId id) { |
| 186 scoped_refptr<PlaceholderBrokerableAttachment> attachment( | 192 scoped_refptr<PlaceholderBrokerableAttachment> attachment( |
| 187 new PlaceholderBrokerableAttachment(id)); | 193 new PlaceholderBrokerableAttachment(id)); |
| 188 return attachment_set()->AddAttachment(attachment); | 194 return attachment_set()->AddAttachment(attachment); |
| 189 } | 195 } |
| 190 | 196 |
| 191 bool Message::WriteAttachment(scoped_refptr<MessageAttachment> attachment) { | 197 bool Message::WriteAttachment(scoped_refptr<MessageAttachment> attachment) { |
| 192 // We write the index of the descriptor so that we don't have to | 198 // We write the index of the descriptor so that we don't have to |
| 193 // keep the current descriptor as extra decoding state when deserialising. | 199 // keep the current descriptor as extra decoding state when deserialising. |
| 194 WriteInt(attachment_set()->size()); | 200 WriteInt(attachment_set()->size()); |
| 201 |
| 202 #if USE_ATTACHMENT_BROKER |
| 203 if (attachment->GetType() == MessageAttachment::TYPE_BROKERABLE_ATTACHMENT) |
| 204 header()->num_brokered_attachments += 1; |
| 205 #endif |
| 206 |
| 195 return attachment_set()->AddAttachment(attachment); | 207 return attachment_set()->AddAttachment(attachment); |
| 196 } | 208 } |
| 197 | 209 |
| 198 bool Message::ReadAttachment( | 210 bool Message::ReadAttachment( |
| 199 base::PickleIterator* iter, | 211 base::PickleIterator* iter, |
| 200 scoped_refptr<MessageAttachment>* attachment) const { | 212 scoped_refptr<MessageAttachment>* attachment) const { |
| 201 int descriptor_index; | 213 int descriptor_index; |
| 202 if (!iter->ReadInt(&descriptor_index)) | 214 if (!iter->ReadInt(&descriptor_index)) |
| 203 return false; | 215 return false; |
| 204 | 216 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 217 bool Message::HasMojoHandles() const { | 229 bool Message::HasMojoHandles() const { |
| 218 return attachment_set_.get() && attachment_set_->num_mojo_handles() > 0; | 230 return attachment_set_.get() && attachment_set_->num_mojo_handles() > 0; |
| 219 } | 231 } |
| 220 | 232 |
| 221 bool Message::HasBrokerableAttachments() const { | 233 bool Message::HasBrokerableAttachments() const { |
| 222 return attachment_set_.get() && | 234 return attachment_set_.get() && |
| 223 attachment_set_->num_brokerable_attachments() > 0; | 235 attachment_set_->num_brokerable_attachments() > 0; |
| 224 } | 236 } |
| 225 | 237 |
| 226 } // namespace IPC | 238 } // namespace IPC |
| OLD | NEW |