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
3 // found in the LICENSE file.
5 #include "ipc/ipc_message.h"
7 #include "base/atomicops.h"
8 #include "base/logging.h"
9 #include "build/build_config.h"
12 #include "ipc/file_descriptor_set_posix.h"
17 base::subtle::Atomic32 g_ref_num
= 0;
19 // Create a reference number for identifying IPC messages in traces. The return
20 // values has the reference number stored in the upper 24 bits, leaving the low
21 // 8 bits set to 0 for use as flags.
22 inline uint32
GetRefNumUpper24() {
23 base::debug::TraceLog
* trace_log
= base::debug::TraceLog::GetInstance();
24 int32 pid
= trace_log
? trace_log
->process_id() : 0;
25 int32 count
= base::subtle::NoBarrier_AtomicIncrement(&g_ref_num
, 1);
26 // The 24 bit hash is composed of 14 bits of the count and 10 bits of the
27 // Process ID. With the current trace event buffer cap, the 14-bit count did
28 // not appear to wrap during a trace. Note that it is not a big deal if
29 // collisions occur, as this is only used for debugging and trace analysis.
30 return ((pid
<< 14) | (count
& 0x3fff)) << 8;
37 //------------------------------------------------------------------------------
43 : Pickle(sizeof(Header
)) {
44 header()->routing
= header()->type
= 0;
45 header()->flags
= GetRefNumUpper24();
47 header()->num_fds
= 0;
50 InitLoggingVariables();
53 Message::Message(int32 routing_id
, uint32 type
, PriorityValue priority
)
54 : Pickle(sizeof(Header
)) {
55 header()->routing
= routing_id
;
56 header()->type
= type
;
57 DCHECK((priority
& 0xffffff00) == 0);
58 header()->flags
= priority
| GetRefNumUpper24();
60 header()->num_fds
= 0;
63 InitLoggingVariables();
66 Message::Message(const char* data
, int data_len
) : Pickle(data
, data_len
) {
67 InitLoggingVariables();
70 Message::Message(const Message
& other
) : Pickle(other
) {
71 InitLoggingVariables();
73 file_descriptor_set_
= other
.file_descriptor_set_
;
77 void Message::InitLoggingVariables() {
78 #ifdef IPC_MESSAGE_LOG_ENABLED
85 Message
& Message::operator=(const Message
& other
) {
86 *static_cast<Pickle
*>(this) = other
;
88 file_descriptor_set_
= other
.file_descriptor_set_
;
93 void Message::SetHeaderValues(int32 routing
, uint32 type
, uint32 flags
) {
94 // This should only be called when the message is already empty.
95 DCHECK(payload_size() == 0);
97 header()->routing
= routing
;
98 header()->type
= type
;
99 header()->flags
= flags
;
102 #ifdef IPC_MESSAGE_LOG_ENABLED
103 void Message::set_sent_time(int64 time
) {
104 DCHECK((header()->flags
& HAS_SENT_TIME_BIT
) == 0);
105 header()->flags
|= HAS_SENT_TIME_BIT
;
109 int64
Message::sent_time() const {
110 if ((header()->flags
& HAS_SENT_TIME_BIT
) == 0)
113 const char* data
= end_of_payload();
114 data
-= sizeof(int64
);
115 return *(reinterpret_cast<const int64
*>(data
));
118 void Message::set_received_time(int64 time
) const {
119 received_time_
= time
;
123 #if defined(OS_POSIX)
124 bool Message::WriteFileDescriptor(const base::FileDescriptor
& descriptor
) {
125 // We write the index of the descriptor so that we don't have to
126 // keep the current descriptor as extra decoding state when deserialising.
127 WriteInt(file_descriptor_set()->size());
128 if (descriptor
.auto_close
) {
129 return file_descriptor_set()->AddAndAutoClose(descriptor
.fd
);
131 return file_descriptor_set()->Add(descriptor
.fd
);
135 bool Message::ReadFileDescriptor(PickleIterator
* iter
,
136 base::FileDescriptor
* descriptor
) const {
137 int descriptor_index
;
138 if (!ReadInt(iter
, &descriptor_index
))
141 FileDescriptorSet
* file_descriptor_set
= file_descriptor_set_
.get();
142 if (!file_descriptor_set
)
145 descriptor
->fd
= file_descriptor_set
->GetDescriptorAt(descriptor_index
);
146 descriptor
->auto_close
= true;
148 return descriptor
->fd
>= 0;
151 bool Message::HasFileDescriptors() const {
152 return file_descriptor_set_
.get() && !file_descriptor_set_
->empty();
155 void Message::EnsureFileDescriptorSet() {
156 if (file_descriptor_set_
.get() == NULL
)
157 file_descriptor_set_
= new FileDescriptorSet
;