blob: f5e9ac7a8b807cd04842aa8139a5b9b8cfb0b9ee [file] [log] [blame]
[email protected]ce208f872012-03-07 20:42:561// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]946d1b22009-07-22 23:57:215#include "ipc/ipc_message.h"
initial.commit09911bf2008-07-26 23:55:296
erikchen29e0f2f2015-09-11 19:08:537#include <limits.h>
avi246998d82015-12-22 02:39:048#include <stddef.h>
9#include <stdint.h>
erikchen29e0f2f2015-09-11 19:08:5310
[email protected]dc4fa482014-02-25 15:30:1511#include "base/atomic_sequence_num.h"
initial.commit09911bf2008-07-26 23:55:2912#include "base/logging.h"
[email protected]526776c2009-02-07 00:39:2613#include "build/build_config.h"
morrita1aa788c2015-01-31 05:45:4214#include "ipc/ipc_message_attachment.h"
morrita4b5c28e22015-01-14 21:17:0615#include "ipc/ipc_message_attachment_set.h"
initial.commit09911bf2008-07-26 23:55:2916
[email protected]7135bb042009-02-12 04:05:2817#if defined(OS_POSIX)
morrita96693852014-09-24 20:11:4518#include "base/file_descriptor_posix.h"
morrita1aa788c2015-01-31 05:45:4219#include "ipc/ipc_platform_file_attachment_posix.h"
[email protected]7135bb042009-02-12 04:05:2820#endif
21
[email protected]2a2e3d62012-09-04 23:08:0922namespace {
23
[email protected]dc4fa482014-02-25 15:30:1524base::StaticAtomicSequenceNumber g_ref_num;
[email protected]2a2e3d62012-09-04 23:08:0925
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.
tfarina10a5c062015-09-04 18:47:5729inline uint32_t GetRefNumUpper24() {
ssid759dd8c2015-02-09 21:25:3930 base::trace_event::TraceLog* trace_log =
31 base::trace_event::TraceLog::GetInstance();
tfarina10a5c062015-09-04 18:47:5732 uint32_t pid = trace_log ? trace_log->process_id() : 0;
33 uint32_t count = g_ref_num.GetNext();
[email protected]2a2e3d62012-09-04 23:08:0934 // 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.
[email protected]40cb13052013-08-14 00:37:4738 return ((pid << 14) | (count & 0x3fff)) << 8;
[email protected]2a2e3d62012-09-04 23:08:0939}
40
41} // namespace
42
initial.commit09911bf2008-07-26 23:55:2943namespace IPC {
44
45//------------------------------------------------------------------------------
46
47Message::~Message() {
48}
49
brettwbd4d7112015-06-03 04:29:2550Message::Message() : base::Pickle(sizeof(Header)) {
[email protected]2a2e3d62012-09-04 23:08:0951 header()->routing = header()->type = 0;
52 header()->flags = GetRefNumUpper24();
[email protected]526776c2009-02-07 00:39:2653#if defined(OS_POSIX)
54 header()->num_fds = 0;
[email protected]15b97af032009-12-05 00:00:5855 header()->pad = 0;
[email protected]526776c2009-02-07 00:39:2656#endif
[email protected]ef2f6ba2014-05-15 23:06:0757 Init();
initial.commit09911bf2008-07-26 23:55:2958}
59
tfarina10a5c062015-09-04 18:47:5760Message::Message(int32_t routing_id, uint32_t type, PriorityValue priority)
brettwbd4d7112015-06-03 04:29:2561 : base::Pickle(sizeof(Header)) {
initial.commit09911bf2008-07-26 23:55:2962 header()->routing = routing_id;
63 header()->type = type;
[email protected]753bb252013-11-04 22:28:1264 DCHECK((priority & 0xffffff00) == 0);
65 header()->flags = priority | GetRefNumUpper24();
[email protected]526776c2009-02-07 00:39:2666#if defined(OS_POSIX)
67 header()->num_fds = 0;
[email protected]15b97af032009-12-05 00:00:5868 header()->pad = 0;
[email protected]526776c2009-02-07 00:39:2669#endif
[email protected]ef2f6ba2014-05-15 23:06:0770 Init();
initial.commit09911bf2008-07-26 23:55:2971}
72
brettwbd4d7112015-06-03 04:29:2573Message::Message(const char* data, int data_len)
74 : base::Pickle(data, data_len) {
[email protected]ef2f6ba2014-05-15 23:06:0775 Init();
initial.commit09911bf2008-07-26 23:55:2976}
77
brettwbd4d7112015-06-03 04:29:2578Message::Message(const Message& other) : base::Pickle(other) {
[email protected]ef2f6ba2014-05-15 23:06:0779 Init();
morrita4b5c28e22015-01-14 21:17:0680 attachment_set_ = other.attachment_set_;
initial.commit09911bf2008-07-26 23:55:2981}
82
[email protected]ef2f6ba2014-05-15 23:06:0783void Message::Init() {
84 dispatch_error_ = false;
initial.commit09911bf2008-07-26 23:55:2985#ifdef IPC_MESSAGE_LOG_ENABLED
86 received_time_ = 0;
87 dont_log_ = false;
88 log_data_ = NULL;
89#endif
90}
91
92Message& Message::operator=(const Message& other) {
brettwbd4d7112015-06-03 04:29:2593 *static_cast<base::Pickle*>(this) = other;
morrita4b5c28e22015-01-14 21:17:0694 attachment_set_ = other.attachment_set_;
initial.commit09911bf2008-07-26 23:55:2995 return *this;
96}
97
tfarina10a5c062015-09-04 18:47:5798void Message::SetHeaderValues(int32_t routing, uint32_t type, uint32_t flags) {
[email protected]34d48612012-06-29 00:05:0499 // 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
morrita4b5c28e22015-01-14 21:17:06107void Message::EnsureMessageAttachmentSet() {
108 if (attachment_set_.get() == NULL)
109 attachment_set_ = new MessageAttachmentSet;
110}
111
initial.commit09911bf2008-07-26 23:55:29112#ifdef IPC_MESSAGE_LOG_ENABLED
tfarina10a5c062015-09-04 18:47:57113void Message::set_sent_time(int64_t time) {
initial.commit09911bf2008-07-26 23:55:29114 DCHECK((header()->flags & HAS_SENT_TIME_BIT) == 0);
115 header()->flags |= HAS_SENT_TIME_BIT;
116 WriteInt64(time);
117}
118
tfarina10a5c062015-09-04 18:47:57119int64_t Message::sent_time() const {
initial.commit09911bf2008-07-26 23:55:29120 if ((header()->flags & HAS_SENT_TIME_BIT) == 0)
121 return 0;
122
123 const char* data = end_of_payload();
tfarina10a5c062015-09-04 18:47:57124 data -= sizeof(int64_t);
125 return *(reinterpret_cast<const int64_t*>(data));
initial.commit09911bf2008-07-26 23:55:29126}
127
tfarina10a5c062015-09-04 18:47:57128void Message::set_received_time(int64_t time) const {
initial.commit09911bf2008-07-26 23:55:29129 received_time_ = time;
130}
131#endif
132
erikchen29e0f2f2015-09-11 19:08:53133Message::NextMessageInfo::NextMessageInfo()
dskiba6f3790a2015-09-30 17:24:30134 : message_size(0), message_found(false), pickle_end(nullptr),
135 message_end(nullptr) {}
erikchen29e0f2f2015-09-11 19:08:53136Message::NextMessageInfo::~NextMessageInfo() {}
137
138// static
139void Message::FindNext(const char* range_start,
140 const char* range_end,
141 NextMessageInfo* info) {
142 DCHECK(info);
dskiba6f3790a2015-09-30 17:24:30143 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))
erikchen29e0f2f2015-09-11 19:08:53149 return;
dskiba6f3790a2015-09-30 17:24:30150
151 bool have_entire_pickle =
152 static_cast<size_t>(range_end - range_start) >= pickle_size;
erikchen29e0f2f2015-09-11 19:08:53153
dskiba6f3790a2015-09-30 17:24:30154 info->message_size = pickle_size;
155
156 if (!have_entire_pickle)
157 return;
158
159 const char* pickle_end = range_start + pickle_size;
160
erikchen29e0f2f2015-09-11 19:08:53161 info->message_end = pickle_end;
erikchen29e0f2f2015-09-11 19:08:53162
dskiba6f3790a2015-09-30 17:24:30163 info->pickle_end = pickle_end;
erikchen29e0f2f2015-09-11 19:08:53164 info->message_found = true;
165}
166
rockot502c94f2016-02-03 20:20:16167bool Message::WriteAttachment(
168 scoped_refptr<base::Pickle::Attachment> attachment) {
erikchenae6d3212015-10-10 02:43:49169 size_t index;
rockot502c94f2016-02-03 20:20:16170 bool success = attachment_set()->AddAttachment(
171 make_scoped_refptr(static_cast<MessageAttachment*>(attachment.get())),
sammc6ed3efb2016-11-23 03:17:35172 &index);
erikchenae6d3212015-10-10 02:43:49173 DCHECK(success);
174
jam45eceef2016-05-11 21:05:05175 // NOTE: If you add more data to the pickle, make sure to update
176 // PickleSizer::AddAttachment.
177
erikchenae6d3212015-10-10 02:43:49178 // Write the index of the descriptor so that we don't have to
[email protected]7135bb042009-02-12 04:05:28179 // keep the current descriptor as extra decoding state when deserialising.
erikchenae6d3212015-10-10 02:43:49180 WriteInt(static_cast<int>(index));
181
erikchenae6d3212015-10-10 02:43:49182 return success;
[email protected]7135bb042009-02-12 04:05:28183}
184
morrita1aa788c2015-01-31 05:45:42185bool Message::ReadAttachment(
brettwbd4d7112015-06-03 04:29:25186 base::PickleIterator* iter,
rockot502c94f2016-02-03 20:20:16187 scoped_refptr<base::Pickle::Attachment>* attachment) const {
erikchenae6d3212015-10-10 02:43:49188 int index;
189 if (!iter->ReadInt(&index))
[email protected]7135bb042009-02-12 04:05:28190 return false;
191
morrita4b5c28e22015-01-14 21:17:06192 MessageAttachmentSet* attachment_set = attachment_set_.get();
193 if (!attachment_set)
[email protected]7135bb042009-02-12 04:05:28194 return false;
195
sammc6ed3efb2016-11-23 03:17:35196 *attachment = attachment_set->GetAttachmentAt(index);
erikchenae6d3212015-10-10 02:43:49197
morrita1aa788c2015-01-31 05:45:42198 return nullptr != attachment->get();
[email protected]7135bb042009-02-12 04:05:28199}
200
morrita1aa788c2015-01-31 05:45:42201bool Message::HasAttachments() const {
morrita4b5c28e22015-01-14 21:17:06202 return attachment_set_.get() && !attachment_set_->empty();
[email protected]7135bb042009-02-12 04:05:28203}
204
[email protected]d284a5f2010-01-05 23:20:30205} // namespace IPC