| 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" |
| 12 | 13 |
| 13 #if defined(OS_POSIX) | 14 #if defined(OS_POSIX) |
| 14 #include "base/file_descriptor_posix.h" | 15 #include "base/file_descriptor_posix.h" |
| 15 #include "ipc/ipc_platform_file_attachment_posix.h" | 16 #include "ipc/ipc_platform_file_attachment_posix.h" |
| 16 #endif | 17 #endif |
| 17 | 18 |
| 18 namespace { | 19 namespace { |
| 19 | 20 |
| 20 base::StaticAtomicSequenceNumber g_ref_num; | 21 base::StaticAtomicSequenceNumber g_ref_num; |
| 21 | 22 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 39 namespace IPC { | 40 namespace IPC { |
| 40 | 41 |
| 41 //------------------------------------------------------------------------------ | 42 //------------------------------------------------------------------------------ |
| 42 | 43 |
| 43 Message::~Message() { | 44 Message::~Message() { |
| 44 } | 45 } |
| 45 | 46 |
| 46 Message::Message() : base::Pickle(sizeof(Header)) { | 47 Message::Message() : base::Pickle(sizeof(Header)) { |
| 47 header()->routing = header()->type = 0; | 48 header()->routing = header()->type = 0; |
| 48 header()->flags = GetRefNumUpper24(); | 49 header()->flags = GetRefNumUpper24(); |
| 50 #if USE_ATTACHMENT_BROKER |
| 51 header()->num_brokered_attachments = 0; |
| 52 #endif |
| 49 #if defined(OS_POSIX) | 53 #if defined(OS_POSIX) |
| 50 header()->num_fds = 0; | 54 header()->num_fds = 0; |
| 51 header()->pad = 0; | 55 header()->pad = 0; |
| 52 #endif | 56 #endif |
| 53 Init(); | 57 Init(); |
| 54 } | 58 } |
| 55 | 59 |
| 56 Message::Message(int32 routing_id, uint32 type, PriorityValue priority) | 60 Message::Message(int32 routing_id, uint32 type, PriorityValue priority) |
| 57 : base::Pickle(sizeof(Header)) { | 61 : base::Pickle(sizeof(Header)) { |
| 58 header()->routing = routing_id; | 62 header()->routing = routing_id; |
| 59 header()->type = type; | 63 header()->type = type; |
| 60 DCHECK((priority & 0xffffff00) == 0); | 64 DCHECK((priority & 0xffffff00) == 0); |
| 61 header()->flags = priority | GetRefNumUpper24(); | 65 header()->flags = priority | GetRefNumUpper24(); |
| 66 #if USE_ATTACHMENT_BROKER |
| 67 header()->num_brokered_attachments = 0; |
| 68 #endif |
| 62 #if defined(OS_POSIX) | 69 #if defined(OS_POSIX) |
| 63 header()->num_fds = 0; | 70 header()->num_fds = 0; |
| 64 header()->pad = 0; | 71 header()->pad = 0; |
| 65 #endif | 72 #endif |
| 66 Init(); | 73 Init(); |
| 67 } | 74 } |
| 68 | 75 |
| 69 Message::Message(const char* data, int data_len) | 76 Message::Message(const char* data, int data_len) |
| 70 : base::Pickle(data, data_len) { | 77 : base::Pickle(data, data_len) { |
| 71 Init(); | 78 Init(); |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 120 const char* data = end_of_payload(); | 127 const char* data = end_of_payload(); |
| 121 data -= sizeof(int64); | 128 data -= sizeof(int64); |
| 122 return *(reinterpret_cast<const int64*>(data)); | 129 return *(reinterpret_cast<const int64*>(data)); |
| 123 } | 130 } |
| 124 | 131 |
| 125 void Message::set_received_time(int64 time) const { | 132 void Message::set_received_time(int64 time) const { |
| 126 received_time_ = time; | 133 received_time_ = time; |
| 127 } | 134 } |
| 128 #endif | 135 #endif |
| 129 | 136 |
| 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 |
| 130 bool Message::WriteAttachment(scoped_refptr<MessageAttachment> attachment) { | 212 bool Message::WriteAttachment(scoped_refptr<MessageAttachment> attachment) { |
| 131 // We write the index of the descriptor so that we don't have to | 213 // 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. | 214 // keep the current descriptor as extra decoding state when deserialising. |
| 133 WriteInt(attachment_set()->size()); | 215 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 |
| 134 return attachment_set()->AddAttachment(attachment); | 222 return attachment_set()->AddAttachment(attachment); |
| 135 } | 223 } |
| 136 | 224 |
| 137 bool Message::ReadAttachment( | 225 bool Message::ReadAttachment( |
| 138 base::PickleIterator* iter, | 226 base::PickleIterator* iter, |
| 139 scoped_refptr<MessageAttachment>* attachment) const { | 227 scoped_refptr<MessageAttachment>* attachment) const { |
| 140 int descriptor_index; | 228 int descriptor_index; |
| 141 if (!iter->ReadInt(&descriptor_index)) | 229 if (!iter->ReadInt(&descriptor_index)) |
| 142 return false; | 230 return false; |
| 143 | 231 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 156 bool Message::HasMojoHandles() const { | 244 bool Message::HasMojoHandles() const { |
| 157 return attachment_set_.get() && attachment_set_->num_mojo_handles() > 0; | 245 return attachment_set_.get() && attachment_set_->num_mojo_handles() > 0; |
| 158 } | 246 } |
| 159 | 247 |
| 160 bool Message::HasBrokerableAttachments() const { | 248 bool Message::HasBrokerableAttachments() const { |
| 161 return attachment_set_.get() && | 249 return attachment_set_.get() && |
| 162 attachment_set_->num_brokerable_attachments() > 0; | 250 attachment_set_->num_brokerable_attachments() > 0; |
| 163 } | 251 } |
| 164 | 252 |
| 165 } // namespace IPC | 253 } // namespace IPC |
| OLD | NEW |