| 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 "base/atomic_sequence_num.h" | 7 #include "base/atomic_sequence_num.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "build/build_config.h" | 9 #include "build/build_config.h" |
| 10 #include "ipc/ipc_message_attachment.h" | 10 #include "ipc/ipc_message_attachment.h" |
| 11 #include "ipc/ipc_message_attachment_set.h" | 11 #include "ipc/ipc_message_attachment_set.h" |
| 12 #include "ipc/placeholder_brokerable_attachment.h" | |
| 13 | 12 |
| 14 #if defined(OS_POSIX) | 13 #if defined(OS_POSIX) |
| 15 #include "base/file_descriptor_posix.h" | 14 #include "base/file_descriptor_posix.h" |
| 16 #include "ipc/ipc_platform_file_attachment_posix.h" | 15 #include "ipc/ipc_platform_file_attachment_posix.h" |
| 17 #endif | 16 #endif |
| 18 | 17 |
| 19 namespace { | 18 namespace { |
| 20 | 19 |
| 21 base::StaticAtomicSequenceNumber g_ref_num; | 20 base::StaticAtomicSequenceNumber g_ref_num; |
| 22 | 21 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 40 namespace IPC { | 39 namespace IPC { |
| 41 | 40 |
| 42 //------------------------------------------------------------------------------ | 41 //------------------------------------------------------------------------------ |
| 43 | 42 |
| 44 Message::~Message() { | 43 Message::~Message() { |
| 45 } | 44 } |
| 46 | 45 |
| 47 Message::Message() : base::Pickle(sizeof(Header)) { | 46 Message::Message() : base::Pickle(sizeof(Header)) { |
| 48 header()->routing = header()->type = 0; | 47 header()->routing = header()->type = 0; |
| 49 header()->flags = GetRefNumUpper24(); | 48 header()->flags = GetRefNumUpper24(); |
| 50 #if USE_ATTACHMENT_BROKER | |
| 51 header()->num_brokered_attachments = 0; | |
| 52 #endif | |
| 53 #if defined(OS_POSIX) | 49 #if defined(OS_POSIX) |
| 54 header()->num_fds = 0; | 50 header()->num_fds = 0; |
| 55 header()->pad = 0; | 51 header()->pad = 0; |
| 56 #endif | 52 #endif |
| 57 Init(); | 53 Init(); |
| 58 } | 54 } |
| 59 | 55 |
| 60 Message::Message(int32 routing_id, uint32 type, PriorityValue priority) | 56 Message::Message(int32 routing_id, uint32 type, PriorityValue priority) |
| 61 : base::Pickle(sizeof(Header)) { | 57 : base::Pickle(sizeof(Header)) { |
| 62 header()->routing = routing_id; | 58 header()->routing = routing_id; |
| 63 header()->type = type; | 59 header()->type = type; |
| 64 DCHECK((priority & 0xffffff00) == 0); | 60 DCHECK((priority & 0xffffff00) == 0); |
| 65 header()->flags = priority | GetRefNumUpper24(); | 61 header()->flags = priority | GetRefNumUpper24(); |
| 66 #if USE_ATTACHMENT_BROKER | |
| 67 header()->num_brokered_attachments = 0; | |
| 68 #endif | |
| 69 #if defined(OS_POSIX) | 62 #if defined(OS_POSIX) |
| 70 header()->num_fds = 0; | 63 header()->num_fds = 0; |
| 71 header()->pad = 0; | 64 header()->pad = 0; |
| 72 #endif | 65 #endif |
| 73 Init(); | 66 Init(); |
| 74 } | 67 } |
| 75 | 68 |
| 76 Message::Message(const char* data, int data_len) | 69 Message::Message(const char* data, int data_len) |
| 77 : base::Pickle(data, data_len) { | 70 : base::Pickle(data, data_len) { |
| 78 Init(); | 71 Init(); |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 127 const char* data = end_of_payload(); | 120 const char* data = end_of_payload(); |
| 128 data -= sizeof(int64); | 121 data -= sizeof(int64); |
| 129 return *(reinterpret_cast<const int64*>(data)); | 122 return *(reinterpret_cast<const int64*>(data)); |
| 130 } | 123 } |
| 131 | 124 |
| 132 void Message::set_received_time(int64 time) const { | 125 void Message::set_received_time(int64 time) const { |
| 133 received_time_ = time; | 126 received_time_ = time; |
| 134 } | 127 } |
| 135 #endif | 128 #endif |
| 136 | 129 |
| 137 Message::NextMessageInfo::NextMessageInfo() | |
| 138 : message_found(false), pickle_end(nullptr), message_end(nullptr) {} | |
| 139 Message::NextMessageInfo::~NextMessageInfo() {} | |
| 140 | |
| 141 // static | |
| 142 Message::NextMessageInfo Message::FindNext(const char* range_start, | |
| 143 const char* range_end) { | |
| 144 NextMessageInfo info; | |
| 145 const char* pickle_end = | |
| 146 base::Pickle::FindNext(sizeof(Header), range_start, range_end); | |
| 147 if (pickle_end == nullptr) | |
| 148 return info; | |
| 149 info.pickle_end = pickle_end; | |
| 150 | |
| 151 #if USE_ATTACHMENT_BROKER | |
| 152 // The data is not copied. | |
| 153 size_t pickle_len = static_cast<size_t>(pickle_end - range_start); | |
| 154 Message message(range_start, static_cast<int>(pickle_len)); | |
| 155 int num_attachments = message.header()->num_brokered_attachments; | |
| 156 | |
| 157 // Each brokered attachment adds kNonceSize bytes of data to the message. We | |
| 158 // want to avoid overflows in our computations, so we limit the number of | |
| 159 // brokerable attachments to 100. | |
| 160 if (num_attachments > 100) | |
| 161 return info; | |
| 162 | |
| 163 // Check whether the range includes the attachments. | |
| 164 size_t attachment_length = num_attachments * BrokerableAttachment::kNonceSize; | |
| 165 size_t buffer_length = static_cast<size_t>(range_end - range_start); | |
| 166 if (buffer_length < attachment_length + pickle_len) | |
| 167 return info; | |
| 168 | |
| 169 for (int i = 0; i < num_attachments; ++i) { | |
| 170 const char* attachment_start = | |
| 171 pickle_end + i * (BrokerableAttachment::kNonceSize); | |
| 172 BrokerableAttachment::AttachmentId id(attachment_start, | |
| 173 BrokerableAttachment::kNonceSize); | |
| 174 info.attachment_ids.push_back(id); | |
| 175 } | |
| 176 info.message_end = | |
| 177 pickle_end + num_attachments * BrokerableAttachment::kNonceSize; | |
| 178 #else | |
| 179 info.message_end = pickle_end; | |
| 180 #endif // USE_ATTACHMENT_BROKER | |
| 181 | |
| 182 info.message_found = true; | |
| 183 return info; | |
| 184 } | |
| 185 | |
| 186 Message::SerializedAttachmentIds | |
| 187 Message::SerializedIdsOfBrokerableAttachments() { | |
| 188 DCHECK(HasBrokerableAttachments()); | |
| 189 std::vector<const BrokerableAttachment*> attachments = | |
| 190 attachment_set_->PeekBrokerableAttachments(); | |
| 191 size_t size = attachments.size() * BrokerableAttachment::kNonceSize; | |
| 192 char* buffer = static_cast<char*>(malloc(size)); | |
| 193 for (size_t i = 0; i < attachments.size(); ++i) { | |
| 194 const BrokerableAttachment* attachment = attachments[i]; | |
| 195 char* start_range = buffer + i * BrokerableAttachment::kNonceSize; | |
| 196 BrokerableAttachment::AttachmentId id = attachment->GetIdentifier(); | |
| 197 id.SerializeToBuffer(start_range, BrokerableAttachment::kNonceSize); | |
| 198 } | |
| 199 SerializedAttachmentIds ids; | |
| 200 ids.buffer = buffer; | |
| 201 ids.size = size; | |
| 202 return ids; | |
| 203 } | |
| 204 | |
| 205 bool Message::AddPlaceholderBrokerableAttachmentWithId( | |
| 206 BrokerableAttachment::AttachmentId id) { | |
| 207 scoped_refptr<PlaceholderBrokerableAttachment> attachment( | |
| 208 new PlaceholderBrokerableAttachment(id)); | |
| 209 return attachment_set()->AddAttachment(attachment); | |
| 210 } | |
| 211 | |
| 212 bool Message::WriteAttachment(scoped_refptr<MessageAttachment> attachment) { | 130 bool Message::WriteAttachment(scoped_refptr<MessageAttachment> attachment) { |
| 213 // We write the index of the descriptor so that we don't have to | 131 // We write the index of the descriptor so that we don't have to |
| 214 // keep the current descriptor as extra decoding state when deserialising. | 132 // keep the current descriptor as extra decoding state when deserialising. |
| 215 WriteInt(attachment_set()->size()); | 133 WriteInt(attachment_set()->size()); |
| 216 | |
| 217 #if USE_ATTACHMENT_BROKER | |
| 218 if (attachment->GetType() == MessageAttachment::TYPE_BROKERABLE_ATTACHMENT) | |
| 219 header()->num_brokered_attachments += 1; | |
| 220 #endif | |
| 221 | |
| 222 return attachment_set()->AddAttachment(attachment); | 134 return attachment_set()->AddAttachment(attachment); |
| 223 } | 135 } |
| 224 | 136 |
| 225 bool Message::ReadAttachment( | 137 bool Message::ReadAttachment( |
| 226 base::PickleIterator* iter, | 138 base::PickleIterator* iter, |
| 227 scoped_refptr<MessageAttachment>* attachment) const { | 139 scoped_refptr<MessageAttachment>* attachment) const { |
| 228 int descriptor_index; | 140 int descriptor_index; |
| 229 if (!iter->ReadInt(&descriptor_index)) | 141 if (!iter->ReadInt(&descriptor_index)) |
| 230 return false; | 142 return false; |
| 231 | 143 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 244 bool Message::HasMojoHandles() const { | 156 bool Message::HasMojoHandles() const { |
| 245 return attachment_set_.get() && attachment_set_->num_mojo_handles() > 0; | 157 return attachment_set_.get() && attachment_set_->num_mojo_handles() > 0; |
| 246 } | 158 } |
| 247 | 159 |
| 248 bool Message::HasBrokerableAttachments() const { | 160 bool Message::HasBrokerableAttachments() const { |
| 249 return attachment_set_.get() && | 161 return attachment_set_.get() && |
| 250 attachment_set_->num_brokerable_attachments() > 0; | 162 attachment_set_->num_brokerable_attachments() > 0; |
| 251 } | 163 } |
| 252 | 164 |
| 253 } // namespace IPC | 165 } // namespace IPC |
| OLD | NEW |