Use extensions_path.h instead of chrome_path.h from extension_icon_image_unittest.cc
[chromium-blink-merge.git] / ipc / ipc_message.cc
blob1ac4d6e3026a35e7817b068a95ba446e72d15b4f
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/atomic_sequence_num.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::StaticAtomicSequenceNumber g_ref_num;
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 = g_ref_num.GetNext();
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 Init();
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 Init();
66 Message::Message(const char* data, int data_len) : Pickle(data, data_len) {
67 Init();
70 Message::Message(const Message& other) : Pickle(other) {
71 Init();
72 #if defined(OS_POSIX)
73 file_descriptor_set_ = other.file_descriptor_set_;
74 #endif
77 void Message::Init() {
78 dispatch_error_ = false;
79 #ifdef IPC_MESSAGE_LOG_ENABLED
80 received_time_ = 0;
81 dont_log_ = false;
82 log_data_ = NULL;
83 #endif
86 Message& Message::operator=(const Message& other) {
87 *static_cast<Pickle*>(this) = other;
88 #if defined(OS_POSIX)
89 file_descriptor_set_ = other.file_descriptor_set_;
90 #endif
91 return *this;
94 void Message::SetHeaderValues(int32 routing, uint32 type, uint32 flags) {
95 // This should only be called when the message is already empty.
96 DCHECK(payload_size() == 0);
98 header()->routing = routing;
99 header()->type = type;
100 header()->flags = flags;
103 #ifdef IPC_MESSAGE_LOG_ENABLED
104 void Message::set_sent_time(int64 time) {
105 DCHECK((header()->flags & HAS_SENT_TIME_BIT) == 0);
106 header()->flags |= HAS_SENT_TIME_BIT;
107 WriteInt64(time);
110 int64 Message::sent_time() const {
111 if ((header()->flags & HAS_SENT_TIME_BIT) == 0)
112 return 0;
114 const char* data = end_of_payload();
115 data -= sizeof(int64);
116 return *(reinterpret_cast<const int64*>(data));
119 void Message::set_received_time(int64 time) const {
120 received_time_ = time;
122 #endif
124 #if defined(OS_POSIX)
125 bool Message::WriteFileDescriptor(const base::FileDescriptor& descriptor) {
126 // We write the index of the descriptor so that we don't have to
127 // keep the current descriptor as extra decoding state when deserialising.
128 WriteInt(file_descriptor_set()->size());
129 if (descriptor.auto_close) {
130 return file_descriptor_set()->AddAndAutoClose(descriptor.fd);
131 } else {
132 return file_descriptor_set()->Add(descriptor.fd);
136 bool Message::ReadFileDescriptor(PickleIterator* iter,
137 base::FileDescriptor* descriptor) const {
138 int descriptor_index;
139 if (!ReadInt(iter, &descriptor_index))
140 return false;
142 FileDescriptorSet* file_descriptor_set = file_descriptor_set_.get();
143 if (!file_descriptor_set)
144 return false;
146 descriptor->fd = file_descriptor_set->GetDescriptorAt(descriptor_index);
147 descriptor->auto_close = true;
149 return descriptor->fd >= 0;
152 bool Message::HasFileDescriptors() const {
153 return file_descriptor_set_.get() && !file_descriptor_set_->empty();
156 void Message::EnsureFileDescriptorSet() {
157 if (file_descriptor_set_.get() == NULL)
158 file_descriptor_set_ = new FileDescriptorSet;
161 #endif
163 } // namespace IPC