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> |
| 8 |
7 #include "base/atomic_sequence_num.h" | 9 #include "base/atomic_sequence_num.h" |
8 #include "base/logging.h" | 10 #include "base/logging.h" |
9 #include "build/build_config.h" | 11 #include "build/build_config.h" |
10 #include "ipc/ipc_message_attachment.h" | 12 #include "ipc/ipc_message_attachment.h" |
11 #include "ipc/ipc_message_attachment_set.h" | 13 #include "ipc/ipc_message_attachment_set.h" |
12 | 14 |
13 #if defined(OS_POSIX) | 15 #if defined(OS_POSIX) |
14 #include "base/file_descriptor_posix.h" | 16 #include "base/file_descriptor_posix.h" |
15 #include "ipc/ipc_platform_file_attachment_posix.h" | 17 #include "ipc/ipc_platform_file_attachment_posix.h" |
16 #endif | 18 #endif |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 const char* data = end_of_payload(); | 122 const char* data = end_of_payload(); |
121 data -= sizeof(int64); | 123 data -= sizeof(int64); |
122 return *(reinterpret_cast<const int64*>(data)); | 124 return *(reinterpret_cast<const int64*>(data)); |
123 } | 125 } |
124 | 126 |
125 void Message::set_received_time(int64 time) const { | 127 void Message::set_received_time(int64 time) const { |
126 received_time_ = time; | 128 received_time_ = time; |
127 } | 129 } |
128 #endif | 130 #endif |
129 | 131 |
| 132 Message::NextMessageInfo::NextMessageInfo() |
| 133 : message_found(false), pickle_end(nullptr), message_end(nullptr) {} |
| 134 Message::NextMessageInfo::~NextMessageInfo() {} |
| 135 |
| 136 // static |
| 137 void Message::FindNext(const char* range_start, |
| 138 const char* range_end, |
| 139 NextMessageInfo* info) { |
| 140 DCHECK(info); |
| 141 const char* pickle_end = |
| 142 base::Pickle::FindNext(sizeof(Header), range_start, range_end); |
| 143 if (!pickle_end) |
| 144 return; |
| 145 info->pickle_end = pickle_end; |
| 146 |
| 147 #if USE_ATTACHMENT_BROKER |
| 148 // The data is not copied. |
| 149 size_t pickle_len = static_cast<size_t>(pickle_end - range_start); |
| 150 Message message(range_start, static_cast<int>(pickle_len)); |
| 151 int num_attachments = message.header()->num_brokered_attachments; |
| 152 |
| 153 // Check for possible overflows. |
| 154 size_t max_size_t = std::numeric_limits<size_t>::max(); |
| 155 if (num_attachments >= max_size_t / BrokerableAttachment::kNonceSize) |
| 156 return; |
| 157 |
| 158 size_t attachment_length = num_attachments * BrokerableAttachment::kNonceSize; |
| 159 if (pickle_len > max_size_t - attachment_length) |
| 160 return; |
| 161 |
| 162 // Check whether the range includes the attachments. |
| 163 size_t buffer_length = static_cast<size_t>(range_end - range_start); |
| 164 if (buffer_length < attachment_length + pickle_len) |
| 165 return; |
| 166 |
| 167 for (int i = 0; i < num_attachments; ++i) { |
| 168 const char* attachment_start = |
| 169 pickle_end + i * BrokerableAttachment::kNonceSize; |
| 170 BrokerableAttachment::AttachmentId id(attachment_start, |
| 171 BrokerableAttachment::kNonceSize); |
| 172 info->attachment_ids.push_back(id); |
| 173 } |
| 174 info->message_end = |
| 175 pickle_end + num_attachments * BrokerableAttachment::kNonceSize; |
| 176 #else |
| 177 info->message_end = pickle_end; |
| 178 #endif // USE_ATTACHMENT_BROKER |
| 179 |
| 180 info->message_found = true; |
| 181 } |
| 182 |
130 bool Message::WriteAttachment(scoped_refptr<MessageAttachment> attachment) { | 183 bool Message::WriteAttachment(scoped_refptr<MessageAttachment> attachment) { |
131 // We write the index of the descriptor so that we don't have to | 184 // We write the index of the descriptor so that we don't have to |
132 // keep the current descriptor as extra decoding state when deserialising. | 185 // keep the current descriptor as extra decoding state when deserialising. |
133 WriteInt(attachment_set()->size()); | 186 WriteInt(attachment_set()->size()); |
134 return attachment_set()->AddAttachment(attachment); | 187 return attachment_set()->AddAttachment(attachment); |
135 } | 188 } |
136 | 189 |
137 bool Message::ReadAttachment( | 190 bool Message::ReadAttachment( |
138 base::PickleIterator* iter, | 191 base::PickleIterator* iter, |
139 scoped_refptr<MessageAttachment>* attachment) const { | 192 scoped_refptr<MessageAttachment>* attachment) const { |
(...skipping 16 matching lines...) Expand all Loading... |
156 bool Message::HasMojoHandles() const { | 209 bool Message::HasMojoHandles() const { |
157 return attachment_set_.get() && attachment_set_->num_mojo_handles() > 0; | 210 return attachment_set_.get() && attachment_set_->num_mojo_handles() > 0; |
158 } | 211 } |
159 | 212 |
160 bool Message::HasBrokerableAttachments() const { | 213 bool Message::HasBrokerableAttachments() const { |
161 return attachment_set_.get() && | 214 return attachment_set_.get() && |
162 attachment_set_->num_brokerable_attachments() > 0; | 215 attachment_set_->num_brokerable_attachments() > 0; |
163 } | 216 } |
164 | 217 |
165 } // namespace IPC | 218 } // namespace IPC |
OLD | NEW |