jbates@chromium.org | 0fc8736 | 2012-03-08 05:42:56 +0900 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "ipc/ipc_message.h" |
| 6 | |
erikchen | c8dc3f5 | 2015-09-12 04:08:53 +0900 | [diff] [blame] | 7 | #include <limits.h> |
avi | 42ebda4 | 2015-12-22 11:39:04 +0900 | [diff] [blame] | 8 | #include <stddef.h> |
| 9 | #include <stdint.h> |
erikchen | c8dc3f5 | 2015-09-12 04:08:53 +0900 | [diff] [blame] | 10 | |
andrew@tullo.ch | 1134b24 | 2014-02-26 00:30:15 +0900 | [diff] [blame] | 11 | #include "base/atomic_sequence_num.h" |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 12 | #include "base/logging.h" |
| 13 | #include "build/build_config.h" |
morrita | 7d1bfcc | 2015-01-31 14:45:42 +0900 | [diff] [blame] | 14 | #include "ipc/ipc_message_attachment.h" |
morrita | 33a3590 | 2015-01-15 06:17:06 +0900 | [diff] [blame] | 15 | #include "ipc/ipc_message_attachment_set.h" |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 16 | |
| 17 | #if defined(OS_POSIX) |
morrita | ab20725 | 2014-09-25 05:11:45 +0900 | [diff] [blame] | 18 | #include "base/file_descriptor_posix.h" |
morrita | 7d1bfcc | 2015-01-31 14:45:42 +0900 | [diff] [blame] | 19 | #include "ipc/ipc_platform_file_attachment_posix.h" |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 20 | #endif |
| 21 | |
jbates@chromium.org | 4b8271c | 2012-09-05 08:08:09 +0900 | [diff] [blame] | 22 | namespace { |
| 23 | |
andrew@tullo.ch | 1134b24 | 2014-02-26 00:30:15 +0900 | [diff] [blame] | 24 | base::StaticAtomicSequenceNumber g_ref_num; |
jbates@chromium.org | 4b8271c | 2012-09-05 08:08:09 +0900 | [diff] [blame] | 25 | |
| 26 | // Create a reference number for identifying IPC messages in traces. The return |
| 27 | // values has the reference number stored in the upper 24 bits, leaving the low |
| 28 | // 8 bits set to 0 for use as flags. |
tfarina | 1cbfa08 | 2015-09-05 03:47:57 +0900 | [diff] [blame] | 29 | inline uint32_t GetRefNumUpper24() { |
ssid | 9873550 | 2015-02-10 06:25:39 +0900 | [diff] [blame] | 30 | base::trace_event::TraceLog* trace_log = |
| 31 | base::trace_event::TraceLog::GetInstance(); |
tfarina | 1cbfa08 | 2015-09-05 03:47:57 +0900 | [diff] [blame] | 32 | uint32_t pid = trace_log ? trace_log->process_id() : 0; |
| 33 | uint32_t count = g_ref_num.GetNext(); |
jbates@chromium.org | 4b8271c | 2012-09-05 08:08:09 +0900 | [diff] [blame] | 34 | // The 24 bit hash is composed of 14 bits of the count and 10 bits of the |
| 35 | // Process ID. With the current trace event buffer cap, the 14-bit count did |
| 36 | // not appear to wrap during a trace. Note that it is not a big deal if |
| 37 | // collisions occur, as this is only used for debugging and trace analysis. |
dsinclair@chromium.org | 682119c | 2013-08-14 09:37:47 +0900 | [diff] [blame] | 38 | return ((pid << 14) | (count & 0x3fff)) << 8; |
jbates@chromium.org | 4b8271c | 2012-09-05 08:08:09 +0900 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | } // namespace |
| 42 | |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 43 | namespace IPC { |
| 44 | |
| 45 | //------------------------------------------------------------------------------ |
| 46 | |
| 47 | Message::~Message() { |
| 48 | } |
| 49 | |
brettw | f314620 | 2015-06-03 13:29:25 +0900 | [diff] [blame] | 50 | Message::Message() : base::Pickle(sizeof(Header)) { |
jbates@chromium.org | 4b8271c | 2012-09-05 08:08:09 +0900 | [diff] [blame] | 51 | header()->routing = header()->type = 0; |
| 52 | header()->flags = GetRefNumUpper24(); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 53 | #if defined(OS_POSIX) |
| 54 | header()->num_fds = 0; |
apatrick@google.com | b2bde43 | 2009-12-05 09:00:58 +0900 | [diff] [blame] | 55 | header()->pad = 0; |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 56 | #endif |
jam@chromium.org | 822f1fb | 2014-05-16 08:06:07 +0900 | [diff] [blame] | 57 | Init(); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 58 | } |
| 59 | |
tfarina | 1cbfa08 | 2015-09-05 03:47:57 +0900 | [diff] [blame] | 60 | Message::Message(int32_t routing_id, uint32_t type, PriorityValue priority) |
brettw | f314620 | 2015-06-03 13:29:25 +0900 | [diff] [blame] | 61 | : base::Pickle(sizeof(Header)) { |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 62 | header()->routing = routing_id; |
| 63 | header()->type = type; |
bbudge@chromium.org | ab4c6bc | 2013-11-05 07:28:12 +0900 | [diff] [blame] | 64 | DCHECK((priority & 0xffffff00) == 0); |
| 65 | header()->flags = priority | GetRefNumUpper24(); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 66 | #if defined(OS_POSIX) |
| 67 | header()->num_fds = 0; |
apatrick@google.com | b2bde43 | 2009-12-05 09:00:58 +0900 | [diff] [blame] | 68 | header()->pad = 0; |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 69 | #endif |
jam@chromium.org | 822f1fb | 2014-05-16 08:06:07 +0900 | [diff] [blame] | 70 | Init(); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 71 | } |
| 72 | |
brettw | f314620 | 2015-06-03 13:29:25 +0900 | [diff] [blame] | 73 | Message::Message(const char* data, int data_len) |
| 74 | : base::Pickle(data, data_len) { |
jam@chromium.org | 822f1fb | 2014-05-16 08:06:07 +0900 | [diff] [blame] | 75 | Init(); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 76 | } |
| 77 | |
brettw | f314620 | 2015-06-03 13:29:25 +0900 | [diff] [blame] | 78 | Message::Message(const Message& other) : base::Pickle(other) { |
jam@chromium.org | 822f1fb | 2014-05-16 08:06:07 +0900 | [diff] [blame] | 79 | Init(); |
morrita | 33a3590 | 2015-01-15 06:17:06 +0900 | [diff] [blame] | 80 | attachment_set_ = other.attachment_set_; |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 81 | } |
| 82 | |
jam@chromium.org | 822f1fb | 2014-05-16 08:06:07 +0900 | [diff] [blame] | 83 | void Message::Init() { |
| 84 | dispatch_error_ = false; |
davidsz | 3bf38eb | 2017-05-12 18:19:23 +0900 | [diff] [blame] | 85 | #if BUILDFLAG(IPC_MESSAGE_LOG_ENABLED) |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 86 | received_time_ = 0; |
| 87 | dont_log_ = false; |
| 88 | log_data_ = NULL; |
| 89 | #endif |
| 90 | } |
| 91 | |
| 92 | Message& Message::operator=(const Message& other) { |
brettw | f314620 | 2015-06-03 13:29:25 +0900 | [diff] [blame] | 93 | *static_cast<base::Pickle*>(this) = other; |
morrita | 33a3590 | 2015-01-15 06:17:06 +0900 | [diff] [blame] | 94 | attachment_set_ = other.attachment_set_; |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 95 | return *this; |
| 96 | } |
| 97 | |
tfarina | 1cbfa08 | 2015-09-05 03:47:57 +0900 | [diff] [blame] | 98 | void Message::SetHeaderValues(int32_t routing, uint32_t type, uint32_t flags) { |
brettw@chromium.org | f48910a | 2012-06-29 09:05:04 +0900 | [diff] [blame] | 99 | // This should only be called when the message is already empty. |
| 100 | DCHECK(payload_size() == 0); |
| 101 | |
| 102 | header()->routing = routing; |
| 103 | header()->type = type; |
| 104 | header()->flags = flags; |
| 105 | } |
| 106 | |
morrita | 33a3590 | 2015-01-15 06:17:06 +0900 | [diff] [blame] | 107 | void Message::EnsureMessageAttachmentSet() { |
| 108 | if (attachment_set_.get() == NULL) |
| 109 | attachment_set_ = new MessageAttachmentSet; |
| 110 | } |
| 111 | |
davidsz | 3bf38eb | 2017-05-12 18:19:23 +0900 | [diff] [blame] | 112 | #if BUILDFLAG(IPC_MESSAGE_LOG_ENABLED) |
tfarina | 1cbfa08 | 2015-09-05 03:47:57 +0900 | [diff] [blame] | 113 | void Message::set_sent_time(int64_t time) { |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 114 | DCHECK((header()->flags & HAS_SENT_TIME_BIT) == 0); |
| 115 | header()->flags |= HAS_SENT_TIME_BIT; |
| 116 | WriteInt64(time); |
| 117 | } |
| 118 | |
tfarina | 1cbfa08 | 2015-09-05 03:47:57 +0900 | [diff] [blame] | 119 | int64_t Message::sent_time() const { |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 120 | if ((header()->flags & HAS_SENT_TIME_BIT) == 0) |
| 121 | return 0; |
| 122 | |
| 123 | const char* data = end_of_payload(); |
tfarina | 1cbfa08 | 2015-09-05 03:47:57 +0900 | [diff] [blame] | 124 | data -= sizeof(int64_t); |
| 125 | return *(reinterpret_cast<const int64_t*>(data)); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 126 | } |
| 127 | |
tfarina | 1cbfa08 | 2015-09-05 03:47:57 +0900 | [diff] [blame] | 128 | void Message::set_received_time(int64_t time) const { |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 129 | received_time_ = time; |
| 130 | } |
davidsz | 3bf38eb | 2017-05-12 18:19:23 +0900 | [diff] [blame] | 131 | #endif // BUILDFLAG(IPC_MESSAGE_LOG_ENABLED) |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 132 | |
erikchen | c8dc3f5 | 2015-09-12 04:08:53 +0900 | [diff] [blame] | 133 | Message::NextMessageInfo::NextMessageInfo() |
dskiba | aa08dcb | 2015-10-01 02:24:30 +0900 | [diff] [blame] | 134 | : message_size(0), message_found(false), pickle_end(nullptr), |
| 135 | message_end(nullptr) {} |
erikchen | c8dc3f5 | 2015-09-12 04:08:53 +0900 | [diff] [blame] | 136 | Message::NextMessageInfo::~NextMessageInfo() {} |
| 137 | |
| 138 | // static |
| 139 | void Message::FindNext(const char* range_start, |
| 140 | const char* range_end, |
| 141 | NextMessageInfo* info) { |
| 142 | DCHECK(info); |
dskiba | aa08dcb | 2015-10-01 02:24:30 +0900 | [diff] [blame] | 143 | info->message_found = false; |
| 144 | info->message_size = 0; |
| 145 | |
| 146 | size_t pickle_size = 0; |
| 147 | if (!base::Pickle::PeekNext(sizeof(Header), |
| 148 | range_start, range_end, &pickle_size)) |
erikchen | c8dc3f5 | 2015-09-12 04:08:53 +0900 | [diff] [blame] | 149 | return; |
dskiba | aa08dcb | 2015-10-01 02:24:30 +0900 | [diff] [blame] | 150 | |
| 151 | bool have_entire_pickle = |
| 152 | static_cast<size_t>(range_end - range_start) >= pickle_size; |
erikchen | c8dc3f5 | 2015-09-12 04:08:53 +0900 | [diff] [blame] | 153 | |
dskiba | aa08dcb | 2015-10-01 02:24:30 +0900 | [diff] [blame] | 154 | info->message_size = pickle_size; |
| 155 | |
| 156 | if (!have_entire_pickle) |
| 157 | return; |
| 158 | |
| 159 | const char* pickle_end = range_start + pickle_size; |
| 160 | |
erikchen | c8dc3f5 | 2015-09-12 04:08:53 +0900 | [diff] [blame] | 161 | info->message_end = pickle_end; |
erikchen | c8dc3f5 | 2015-09-12 04:08:53 +0900 | [diff] [blame] | 162 | |
dskiba | aa08dcb | 2015-10-01 02:24:30 +0900 | [diff] [blame] | 163 | info->pickle_end = pickle_end; |
erikchen | c8dc3f5 | 2015-09-12 04:08:53 +0900 | [diff] [blame] | 164 | info->message_found = true; |
| 165 | } |
| 166 | |
rockot | 6dbfea5 | 2016-02-04 05:20:16 +0900 | [diff] [blame] | 167 | bool Message::WriteAttachment( |
| 168 | scoped_refptr<base::Pickle::Attachment> attachment) { |
erikchen | c8fa421 | 2015-10-10 11:43:49 +0900 | [diff] [blame] | 169 | size_t index; |
rockot | 6dbfea5 | 2016-02-04 05:20:16 +0900 | [diff] [blame] | 170 | bool success = attachment_set()->AddAttachment( |
| 171 | make_scoped_refptr(static_cast<MessageAttachment*>(attachment.get())), |
sammc | 1458336 | 2016-11-23 12:17:35 +0900 | [diff] [blame] | 172 | &index); |
erikchen | c8fa421 | 2015-10-10 11:43:49 +0900 | [diff] [blame] | 173 | DCHECK(success); |
| 174 | |
jam | 7f5c574 | 2016-05-12 06:05:05 +0900 | [diff] [blame] | 175 | // NOTE: If you add more data to the pickle, make sure to update |
| 176 | // PickleSizer::AddAttachment. |
| 177 | |
erikchen | c8fa421 | 2015-10-10 11:43:49 +0900 | [diff] [blame] | 178 | // Write the index of the descriptor so that we don't have to |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 179 | // keep the current descriptor as extra decoding state when deserialising. |
erikchen | c8fa421 | 2015-10-10 11:43:49 +0900 | [diff] [blame] | 180 | WriteInt(static_cast<int>(index)); |
| 181 | |
erikchen | c8fa421 | 2015-10-10 11:43:49 +0900 | [diff] [blame] | 182 | return success; |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 183 | } |
| 184 | |
morrita | 7d1bfcc | 2015-01-31 14:45:42 +0900 | [diff] [blame] | 185 | bool Message::ReadAttachment( |
brettw | f314620 | 2015-06-03 13:29:25 +0900 | [diff] [blame] | 186 | base::PickleIterator* iter, |
rockot | 6dbfea5 | 2016-02-04 05:20:16 +0900 | [diff] [blame] | 187 | scoped_refptr<base::Pickle::Attachment>* attachment) const { |
erikchen | c8fa421 | 2015-10-10 11:43:49 +0900 | [diff] [blame] | 188 | int index; |
| 189 | if (!iter->ReadInt(&index)) |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 190 | return false; |
| 191 | |
morrita | 33a3590 | 2015-01-15 06:17:06 +0900 | [diff] [blame] | 192 | MessageAttachmentSet* attachment_set = attachment_set_.get(); |
| 193 | if (!attachment_set) |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 194 | return false; |
| 195 | |
sammc | 1458336 | 2016-11-23 12:17:35 +0900 | [diff] [blame] | 196 | *attachment = attachment_set->GetAttachmentAt(index); |
erikchen | c8fa421 | 2015-10-10 11:43:49 +0900 | [diff] [blame] | 197 | |
morrita | 7d1bfcc | 2015-01-31 14:45:42 +0900 | [diff] [blame] | 198 | return nullptr != attachment->get(); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 199 | } |
| 200 | |
morrita | 7d1bfcc | 2015-01-31 14:45:42 +0900 | [diff] [blame] | 201 | bool Message::HasAttachments() const { |
morrita | 33a3590 | 2015-01-15 06:17:06 +0900 | [diff] [blame] | 202 | return attachment_set_.get() && !attachment_set_->empty(); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 203 | } |
| 204 | |
thakis@chromium.org | bf9558b | 2010-01-06 08:20:30 +0900 | [diff] [blame] | 205 | } // namespace IPC |