| 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_set.h" | 11 #include "ipc/ipc_message_attachment_set.h" |
| 11 | 12 |
| 12 #if defined(OS_POSIX) | 13 #if defined(OS_POSIX) |
| 13 #include "base/file_descriptor_posix.h" | 14 #include "base/file_descriptor_posix.h" |
| 15 #include "ipc/ipc_platform_file_attachment_posix.h" |
| 14 #endif | 16 #endif |
| 15 | 17 |
| 16 namespace { | 18 namespace { |
| 17 | 19 |
| 18 base::StaticAtomicSequenceNumber g_ref_num; | 20 base::StaticAtomicSequenceNumber g_ref_num; |
| 19 | 21 |
| 20 // Create a reference number for identifying IPC messages in traces. The return | 22 // Create a reference number for identifying IPC messages in traces. The return |
| 21 // values has the reference number stored in the upper 24 bits, leaving the low | 23 // values has the reference number stored in the upper 24 bits, leaving the low |
| 22 // 8 bits set to 0 for use as flags. | 24 // 8 bits set to 0 for use as flags. |
| 23 inline uint32 GetRefNumUpper24() { | 25 inline uint32 GetRefNumUpper24() { |
| (...skipping 96 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 |
| 130 #if defined(OS_POSIX) | 132 bool Message::WriteAttachment(scoped_refptr<MessageAttachment> attachment) { |
| 131 bool Message::WriteFile(base::ScopedFD descriptor) { | |
| 132 // We write the index of the descriptor so that we don't have to | 133 // We write the index of the descriptor so that we don't have to |
| 133 // keep the current descriptor as extra decoding state when deserialising. | 134 // keep the current descriptor as extra decoding state when deserialising. |
| 134 WriteInt(attachment_set()->size()); | 135 WriteInt(attachment_set()->size()); |
| 135 return attachment_set()->AddToOwn(descriptor.Pass()); | 136 return attachment_set()->AddAttachment(attachment); |
| 136 } | 137 } |
| 137 | 138 |
| 138 bool Message::WriteBorrowingFile(const base::PlatformFile& descriptor) { | 139 bool Message::ReadAttachment( |
| 139 // We write the index of the descriptor so that we don't have to | 140 PickleIterator* iter, |
| 140 // keep the current descriptor as extra decoding state when deserialising. | 141 scoped_refptr<MessageAttachment>* attachment) const { |
| 141 WriteInt(attachment_set()->size()); | |
| 142 return attachment_set()->AddToBorrow(descriptor); | |
| 143 } | |
| 144 | |
| 145 bool Message::ReadFile(PickleIterator* iter, base::ScopedFD* descriptor) const { | |
| 146 int descriptor_index; | 142 int descriptor_index; |
| 147 if (!iter->ReadInt(&descriptor_index)) | 143 if (!iter->ReadInt(&descriptor_index)) |
| 148 return false; | 144 return false; |
| 149 | 145 |
| 150 MessageAttachmentSet* attachment_set = attachment_set_.get(); | 146 MessageAttachmentSet* attachment_set = attachment_set_.get(); |
| 151 if (!attachment_set) | 147 if (!attachment_set) |
| 152 return false; | 148 return false; |
| 153 | 149 |
| 154 base::PlatformFile file = attachment_set->TakeDescriptorAt(descriptor_index); | 150 *attachment = attachment_set->GetAttachmentAt(descriptor_index); |
| 155 if (file < 0) | 151 return nullptr != attachment->get(); |
| 156 return false; | |
| 157 | |
| 158 descriptor->reset(file); | |
| 159 return true; | |
| 160 } | 152 } |
| 161 | 153 |
| 162 bool Message::HasFileDescriptors() const { | 154 bool Message::HasAttachments() const { |
| 163 return attachment_set_.get() && !attachment_set_->empty(); | 155 return attachment_set_.get() && !attachment_set_->empty(); |
| 164 } | 156 } |
| 165 | 157 |
| 166 #endif | |
| 167 | |
| 168 } // namespace IPC | 158 } // namespace IPC |
| OLD | NEW |