blob: 7aa268e6dfba9143a8c9e4d9f7987bd60ffa7628 [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
[email protected]dc4fa482014-02-25 15:30:157#include "base/atomic_sequence_num.h"
initial.commit09911bf2008-07-26 23:55:298#include "base/logging.h"
[email protected]526776c2009-02-07 00:39:269#include "build/build_config.h"
morrita1aa788c2015-01-31 05:45:4210#include "ipc/ipc_message_attachment.h"
morrita4b5c28e22015-01-14 21:17:0611#include "ipc/ipc_message_attachment_set.h"
initial.commit09911bf2008-07-26 23:55:2912
[email protected]7135bb042009-02-12 04:05:2813#if defined(OS_POSIX)
morrita96693852014-09-24 20:11:4514#include "base/file_descriptor_posix.h"
morrita1aa788c2015-01-31 05:45:4215#include "ipc/ipc_platform_file_attachment_posix.h"
[email protected]7135bb042009-02-12 04:05:2816#endif
17
[email protected]2a2e3d62012-09-04 23:08:0918namespace {
19
[email protected]dc4fa482014-02-25 15:30:1520base::StaticAtomicSequenceNumber g_ref_num;
[email protected]2a2e3d62012-09-04 23:08:0921
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.
25inline uint32 GetRefNumUpper24() {
ssid759dd8c2015-02-09 21:25:3926 base::trace_event::TraceLog* trace_log =
27 base::trace_event::TraceLog::GetInstance();
rickyz71cbda102014-11-12 01:33:2328 uint32 pid = trace_log ? trace_log->process_id() : 0;
29 uint32 count = g_ref_num.GetNext();
[email protected]2a2e3d62012-09-04 23:08:0930 // 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]40cb13052013-08-14 00:37:4734 return ((pid << 14) | (count & 0x3fff)) << 8;
[email protected]2a2e3d62012-09-04 23:08:0935}
36
37} // namespace
38
initial.commit09911bf2008-07-26 23:55:2939namespace IPC {
40
41//------------------------------------------------------------------------------
42
43Message::~Message() {
44}
45
brettwbd4d7112015-06-03 04:29:2546Message::Message() : base::Pickle(sizeof(Header)) {
[email protected]2a2e3d62012-09-04 23:08:0947 header()->routing = header()->type = 0;
48 header()->flags = GetRefNumUpper24();
[email protected]526776c2009-02-07 00:39:2649#if defined(OS_POSIX)
50 header()->num_fds = 0;
[email protected]15b97af032009-12-05 00:00:5851 header()->pad = 0;
[email protected]526776c2009-02-07 00:39:2652#endif
[email protected]ef2f6ba2014-05-15 23:06:0753 Init();
initial.commit09911bf2008-07-26 23:55:2954}
55
[email protected]753bb252013-11-04 22:28:1256Message::Message(int32 routing_id, uint32 type, PriorityValue priority)
brettwbd4d7112015-06-03 04:29:2557 : base::Pickle(sizeof(Header)) {
initial.commit09911bf2008-07-26 23:55:2958 header()->routing = routing_id;
59 header()->type = type;
[email protected]753bb252013-11-04 22:28:1260 DCHECK((priority & 0xffffff00) == 0);
61 header()->flags = priority | GetRefNumUpper24();
[email protected]526776c2009-02-07 00:39:2662#if defined(OS_POSIX)
63 header()->num_fds = 0;
[email protected]15b97af032009-12-05 00:00:5864 header()->pad = 0;
[email protected]526776c2009-02-07 00:39:2665#endif
[email protected]ef2f6ba2014-05-15 23:06:0766 Init();
initial.commit09911bf2008-07-26 23:55:2967}
68
brettwbd4d7112015-06-03 04:29:2569Message::Message(const char* data, int data_len)
70 : base::Pickle(data, data_len) {
[email protected]ef2f6ba2014-05-15 23:06:0771 Init();
initial.commit09911bf2008-07-26 23:55:2972}
73
brettwbd4d7112015-06-03 04:29:2574Message::Message(const Message& other) : base::Pickle(other) {
[email protected]ef2f6ba2014-05-15 23:06:0775 Init();
[email protected]b7273e012009-02-10 08:21:5076#if defined(OS_POSIX)
morrita4b5c28e22015-01-14 21:17:0677 attachment_set_ = other.attachment_set_;
[email protected]b7273e012009-02-10 08:21:5078#endif
initial.commit09911bf2008-07-26 23:55:2979}
80
[email protected]ef2f6ba2014-05-15 23:06:0781void Message::Init() {
82 dispatch_error_ = false;
initial.commit09911bf2008-07-26 23:55:2983#ifdef IPC_MESSAGE_LOG_ENABLED
84 received_time_ = 0;
85 dont_log_ = false;
86 log_data_ = NULL;
87#endif
88}
89
90Message& Message::operator=(const Message& other) {
brettwbd4d7112015-06-03 04:29:2591 *static_cast<base::Pickle*>(this) = other;
[email protected]6f0f2422009-02-11 01:33:3492#if defined(OS_POSIX)
morrita4b5c28e22015-01-14 21:17:0693 attachment_set_ = other.attachment_set_;
[email protected]6f0f2422009-02-11 01:33:3494#endif
initial.commit09911bf2008-07-26 23:55:2995 return *this;
96}
97
[email protected]34d48612012-06-29 00:05:0498void Message::SetHeaderValues(int32 routing, uint32 type, uint32 flags) {
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
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
113void Message::set_sent_time(int64 time) {
114 DCHECK((header()->flags & HAS_SENT_TIME_BIT) == 0);
115 header()->flags |= HAS_SENT_TIME_BIT;
116 WriteInt64(time);
117}
118
119int64 Message::sent_time() const {
120 if ((header()->flags & HAS_SENT_TIME_BIT) == 0)
121 return 0;
122
123 const char* data = end_of_payload();
124 data -= sizeof(int64);
125 return *(reinterpret_cast<const int64*>(data));
126}
127
128void Message::set_received_time(int64 time) const {
129 received_time_ = time;
130}
131#endif
132
morrita1aa788c2015-01-31 05:45:42133bool Message::WriteAttachment(scoped_refptr<MessageAttachment> attachment) {
[email protected]7135bb042009-02-12 04:05:28134 // We write the index of the descriptor so that we don't have to
135 // keep the current descriptor as extra decoding state when deserialising.
morrita4b5c28e22015-01-14 21:17:06136 WriteInt(attachment_set()->size());
morrita1aa788c2015-01-31 05:45:42137 return attachment_set()->AddAttachment(attachment);
[email protected]7135bb042009-02-12 04:05:28138}
139
morrita1aa788c2015-01-31 05:45:42140bool Message::ReadAttachment(
brettwbd4d7112015-06-03 04:29:25141 base::PickleIterator* iter,
morrita1aa788c2015-01-31 05:45:42142 scoped_refptr<MessageAttachment>* attachment) const {
[email protected]7135bb042009-02-12 04:05:28143 int descriptor_index;
avi48fc13b2014-12-28 23:31:48144 if (!iter->ReadInt(&descriptor_index))
[email protected]7135bb042009-02-12 04:05:28145 return false;
146
morrita4b5c28e22015-01-14 21:17:06147 MessageAttachmentSet* attachment_set = attachment_set_.get();
148 if (!attachment_set)
[email protected]7135bb042009-02-12 04:05:28149 return false;
150
morrita1aa788c2015-01-31 05:45:42151 *attachment = attachment_set->GetAttachmentAt(descriptor_index);
152 return nullptr != attachment->get();
[email protected]7135bb042009-02-12 04:05:28153}
154
morrita1aa788c2015-01-31 05:45:42155bool Message::HasAttachments() const {
morrita4b5c28e22015-01-14 21:17:06156 return attachment_set_.get() && !attachment_set_->empty();
[email protected]7135bb042009-02-12 04:05:28157}
158
morrita81b17e02015-02-06 00:58:30159bool Message::HasMojoHandles() const {
160 return attachment_set_.get() && 0 < attachment_set_->num_mojo_handles();
161}
162
[email protected]d284a5f2010-01-05 23:20:30163} // namespace IPC