cc: Added inline to Tile::IsReadyToDraw
[chromium-blink-merge.git] / ipc / ipc_message.cc
blobcf3a65e077f08dc767f63150d48c848b79589419
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"
11 #if defined(OS_POSIX)
12 #include "ipc/file_descriptor_set_posix.h"
13 #endif
15 namespace {
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;
33 } // namespace
35 namespace IPC {
37 //------------------------------------------------------------------------------
39 Message::~Message() {
42 Message::Message()
43 : Pickle(sizeof(Header)) {
44 header()->routing = header()->type = 0;
45 header()->flags = GetRefNumUpper24();
46 #if defined(OS_POSIX)
47 header()->num_fds = 0;
48 header()->pad = 0;
49 #endif
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();
59 #if defined(OS_POSIX)
60 header()->num_fds = 0;
61 header()->pad = 0;
62 #endif
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();
72 #if defined(OS_POSIX)
73 file_descriptor_set_ = other.file_descriptor_set_;
74 #endif
77 void Message::InitLoggingVariables() {
78 #ifdef IPC_MESSAGE_LOG_ENABLED
79 received_time_ = 0;
80 dont_log_ = false;
81 log_data_ = NULL;
82 #endif
85 Message& Message::operator=(const Message& other) {
86 *static_cast<Pickle*>(this) = other;
87 #if defined(OS_POSIX)
88 file_descriptor_set_ = other.file_descriptor_set_;
89 #endif
90 return *this;
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;
106 WriteInt64(time);
109 int64 Message::sent_time() const {
110 if ((header()->flags & HAS_SENT_TIME_BIT) == 0)
111 return 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;
121 #endif
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);
130 } else {
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))
139 return false;
141 FileDescriptorSet* file_descriptor_set = file_descriptor_set_.get();
142 if (!file_descriptor_set)
143 return false;
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;
160 #endif
162 } // namespace IPC