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