[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 5 | #include "ipc/ipc_message.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 6 | |
[email protected] | dc4fa48 | 2014-02-25 15:30:15 | [diff] [blame] | 7 | #include "base/atomic_sequence_num.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 8 | #include "base/logging.h" |
[email protected] | 526776c | 2009-02-07 00:39:26 | [diff] [blame] | 9 | #include "build/build_config.h" |
morrita | 1aa788c | 2015-01-31 05:45:42 | [diff] [blame] | 10 | #include "ipc/ipc_message_attachment.h" |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 11 | #include "ipc/ipc_message_attachment_set.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 12 | |
[email protected] | 7135bb04 | 2009-02-12 04:05:28 | [diff] [blame] | 13 | #if defined(OS_POSIX) |
morrita | 9669385 | 2014-09-24 20:11:45 | [diff] [blame] | 14 | #include "base/file_descriptor_posix.h" |
morrita | 1aa788c | 2015-01-31 05:45:42 | [diff] [blame] | 15 | #include "ipc/ipc_platform_file_attachment_posix.h" |
[email protected] | 7135bb04 | 2009-02-12 04:05:28 | [diff] [blame] | 16 | #endif |
| 17 | |
[email protected] | 2a2e3d6 | 2012-09-04 23:08:09 | [diff] [blame] | 18 | namespace { |
| 19 | |
[email protected] | dc4fa48 | 2014-02-25 15:30:15 | [diff] [blame] | 20 | base::StaticAtomicSequenceNumber g_ref_num; |
[email protected] | 2a2e3d6 | 2012-09-04 23:08:09 | [diff] [blame] | 21 | |
| 22 | // Create a reference number for identifying IPC messages in traces. The return |
| 23 | // values has the reference number stored in the upper 24 bits, leaving the low |
| 24 | // 8 bits set to 0 for use as flags. |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame^] | 25 | inline uint32_t GetRefNumUpper24() { |
ssid | 759dd8c | 2015-02-09 21:25:39 | [diff] [blame] | 26 | base::trace_event::TraceLog* trace_log = |
| 27 | base::trace_event::TraceLog::GetInstance(); |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame^] | 28 | uint32_t pid = trace_log ? trace_log->process_id() : 0; |
| 29 | uint32_t count = g_ref_num.GetNext(); |
[email protected] | 2a2e3d6 | 2012-09-04 23:08:09 | [diff] [blame] | 30 | // The 24 bit hash is composed of 14 bits of the count and 10 bits of the |
| 31 | // Process ID. With the current trace event buffer cap, the 14-bit count did |
| 32 | // not appear to wrap during a trace. Note that it is not a big deal if |
| 33 | // collisions occur, as this is only used for debugging and trace analysis. |
[email protected] | 40cb1305 | 2013-08-14 00:37:47 | [diff] [blame] | 34 | return ((pid << 14) | (count & 0x3fff)) << 8; |
[email protected] | 2a2e3d6 | 2012-09-04 23:08:09 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | } // namespace |
| 38 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 39 | namespace IPC { |
| 40 | |
| 41 | //------------------------------------------------------------------------------ |
| 42 | |
| 43 | Message::~Message() { |
| 44 | } |
| 45 | |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 46 | Message::Message() : base::Pickle(sizeof(Header)) { |
[email protected] | 2a2e3d6 | 2012-09-04 23:08:09 | [diff] [blame] | 47 | header()->routing = header()->type = 0; |
| 48 | header()->flags = GetRefNumUpper24(); |
[email protected] | 526776c | 2009-02-07 00:39:26 | [diff] [blame] | 49 | #if defined(OS_POSIX) |
| 50 | header()->num_fds = 0; |
[email protected] | 15b97af03 | 2009-12-05 00:00:58 | [diff] [blame] | 51 | header()->pad = 0; |
[email protected] | 526776c | 2009-02-07 00:39:26 | [diff] [blame] | 52 | #endif |
[email protected] | ef2f6ba | 2014-05-15 23:06:07 | [diff] [blame] | 53 | Init(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 54 | } |
| 55 | |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame^] | 56 | Message::Message(int32_t routing_id, uint32_t type, PriorityValue priority) |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 57 | : base::Pickle(sizeof(Header)) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 58 | header()->routing = routing_id; |
| 59 | header()->type = type; |
[email protected] | 753bb25 | 2013-11-04 22:28:12 | [diff] [blame] | 60 | DCHECK((priority & 0xffffff00) == 0); |
| 61 | header()->flags = priority | GetRefNumUpper24(); |
[email protected] | 526776c | 2009-02-07 00:39:26 | [diff] [blame] | 62 | #if defined(OS_POSIX) |
| 63 | header()->num_fds = 0; |
[email protected] | 15b97af03 | 2009-12-05 00:00:58 | [diff] [blame] | 64 | header()->pad = 0; |
[email protected] | 526776c | 2009-02-07 00:39:26 | [diff] [blame] | 65 | #endif |
[email protected] | ef2f6ba | 2014-05-15 23:06:07 | [diff] [blame] | 66 | Init(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 67 | } |
| 68 | |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 69 | Message::Message(const char* data, int data_len) |
| 70 | : base::Pickle(data, data_len) { |
[email protected] | ef2f6ba | 2014-05-15 23:06:07 | [diff] [blame] | 71 | Init(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 72 | } |
| 73 | |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 74 | Message::Message(const Message& other) : base::Pickle(other) { |
[email protected] | ef2f6ba | 2014-05-15 23:06:07 | [diff] [blame] | 75 | Init(); |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 76 | attachment_set_ = other.attachment_set_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 77 | } |
| 78 | |
[email protected] | ef2f6ba | 2014-05-15 23:06:07 | [diff] [blame] | 79 | void Message::Init() { |
| 80 | dispatch_error_ = false; |
erikchen | 3c175a3 | 2015-07-28 23:16:48 | [diff] [blame] | 81 | sender_pid_ = base::kNullProcessId; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 82 | #ifdef IPC_MESSAGE_LOG_ENABLED |
| 83 | received_time_ = 0; |
| 84 | dont_log_ = false; |
| 85 | log_data_ = NULL; |
| 86 | #endif |
| 87 | } |
| 88 | |
| 89 | Message& Message::operator=(const Message& other) { |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 90 | *static_cast<base::Pickle*>(this) = other; |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 91 | attachment_set_ = other.attachment_set_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 92 | return *this; |
| 93 | } |
| 94 | |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame^] | 95 | void Message::SetHeaderValues(int32_t routing, uint32_t type, uint32_t flags) { |
[email protected] | 34d4861 | 2012-06-29 00:05:04 | [diff] [blame] | 96 | // This should only be called when the message is already empty. |
| 97 | DCHECK(payload_size() == 0); |
| 98 | |
| 99 | header()->routing = routing; |
| 100 | header()->type = type; |
| 101 | header()->flags = flags; |
| 102 | } |
| 103 | |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 104 | void Message::EnsureMessageAttachmentSet() { |
| 105 | if (attachment_set_.get() == NULL) |
| 106 | attachment_set_ = new MessageAttachmentSet; |
| 107 | } |
| 108 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 109 | #ifdef IPC_MESSAGE_LOG_ENABLED |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame^] | 110 | void Message::set_sent_time(int64_t time) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 111 | DCHECK((header()->flags & HAS_SENT_TIME_BIT) == 0); |
| 112 | header()->flags |= HAS_SENT_TIME_BIT; |
| 113 | WriteInt64(time); |
| 114 | } |
| 115 | |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame^] | 116 | int64_t Message::sent_time() const { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 117 | if ((header()->flags & HAS_SENT_TIME_BIT) == 0) |
| 118 | return 0; |
| 119 | |
| 120 | const char* data = end_of_payload(); |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame^] | 121 | data -= sizeof(int64_t); |
| 122 | return *(reinterpret_cast<const int64_t*>(data)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 123 | } |
| 124 | |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame^] | 125 | void Message::set_received_time(int64_t time) const { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 126 | received_time_ = time; |
| 127 | } |
| 128 | #endif |
| 129 | |
morrita | 1aa788c | 2015-01-31 05:45:42 | [diff] [blame] | 130 | bool Message::WriteAttachment(scoped_refptr<MessageAttachment> attachment) { |
[email protected] | 7135bb04 | 2009-02-12 04:05:28 | [diff] [blame] | 131 | // 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. |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 133 | WriteInt(attachment_set()->size()); |
morrita | 1aa788c | 2015-01-31 05:45:42 | [diff] [blame] | 134 | return attachment_set()->AddAttachment(attachment); |
[email protected] | 7135bb04 | 2009-02-12 04:05:28 | [diff] [blame] | 135 | } |
| 136 | |
morrita | 1aa788c | 2015-01-31 05:45:42 | [diff] [blame] | 137 | bool Message::ReadAttachment( |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 138 | base::PickleIterator* iter, |
morrita | 1aa788c | 2015-01-31 05:45:42 | [diff] [blame] | 139 | scoped_refptr<MessageAttachment>* attachment) const { |
[email protected] | 7135bb04 | 2009-02-12 04:05:28 | [diff] [blame] | 140 | int descriptor_index; |
avi | 48fc13b | 2014-12-28 23:31:48 | [diff] [blame] | 141 | if (!iter->ReadInt(&descriptor_index)) |
[email protected] | 7135bb04 | 2009-02-12 04:05:28 | [diff] [blame] | 142 | return false; |
| 143 | |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 144 | MessageAttachmentSet* attachment_set = attachment_set_.get(); |
| 145 | if (!attachment_set) |
[email protected] | 7135bb04 | 2009-02-12 04:05:28 | [diff] [blame] | 146 | return false; |
| 147 | |
morrita | 1aa788c | 2015-01-31 05:45:42 | [diff] [blame] | 148 | *attachment = attachment_set->GetAttachmentAt(descriptor_index); |
| 149 | return nullptr != attachment->get(); |
[email protected] | 7135bb04 | 2009-02-12 04:05:28 | [diff] [blame] | 150 | } |
| 151 | |
morrita | 1aa788c | 2015-01-31 05:45:42 | [diff] [blame] | 152 | bool Message::HasAttachments() const { |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 153 | return attachment_set_.get() && !attachment_set_->empty(); |
[email protected] | 7135bb04 | 2009-02-12 04:05:28 | [diff] [blame] | 154 | } |
| 155 | |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 156 | bool Message::HasMojoHandles() const { |
erikchen | eece6c3 | 2015-07-07 22:13:11 | [diff] [blame] | 157 | return attachment_set_.get() && attachment_set_->num_mojo_handles() > 0; |
| 158 | } |
| 159 | |
| 160 | bool Message::HasBrokerableAttachments() const { |
| 161 | return attachment_set_.get() && |
| 162 | attachment_set_->num_brokerable_attachments() > 0; |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 163 | } |
| 164 | |
[email protected] | d284a5f | 2010-01-05 23:20:30 | [diff] [blame] | 165 | } // namespace IPC |