Simplify About Chrome settings page.
[chromium-blink-merge.git] / ipc / ipc_message.cc
blob7aa268e6dfba9143a8c9e4d9f7987bd60ffa7628
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"
10 #include "ipc/ipc_message_attachment.h"
11 #include "ipc/ipc_message_attachment_set.h"
13 #if defined(OS_POSIX)
14 #include "base/file_descriptor_posix.h"
15 #include "ipc/ipc_platform_file_attachment_posix.h"
16 #endif
18 namespace {
20 base::StaticAtomicSequenceNumber g_ref_num;
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.
25 inline uint32 GetRefNumUpper24() {
26 base::trace_event::TraceLog* trace_log =
27 base::trace_event::TraceLog::GetInstance();
28 uint32 pid = trace_log ? trace_log->process_id() : 0;
29 uint32 count = g_ref_num.GetNext();
30 // 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.
34 return ((pid << 14) | (count & 0x3fff)) << 8;
37 } // namespace
39 namespace IPC {
41 //------------------------------------------------------------------------------
43 Message::~Message() {
46 Message::Message() : base::Pickle(sizeof(Header)) {
47 header()->routing = header()->type = 0;
48 header()->flags = GetRefNumUpper24();
49 #if defined(OS_POSIX)
50 header()->num_fds = 0;
51 header()->pad = 0;
52 #endif
53 Init();
56 Message::Message(int32 routing_id, uint32 type, PriorityValue priority)
57 : base::Pickle(sizeof(Header)) {
58 header()->routing = routing_id;
59 header()->type = type;
60 DCHECK((priority & 0xffffff00) == 0);
61 header()->flags = priority | GetRefNumUpper24();
62 #if defined(OS_POSIX)
63 header()->num_fds = 0;
64 header()->pad = 0;
65 #endif
66 Init();
69 Message::Message(const char* data, int data_len)
70 : base::Pickle(data, data_len) {
71 Init();
74 Message::Message(const Message& other) : base::Pickle(other) {
75 Init();
76 #if defined(OS_POSIX)
77 attachment_set_ = other.attachment_set_;
78 #endif
81 void Message::Init() {
82 dispatch_error_ = false;
83 #ifdef IPC_MESSAGE_LOG_ENABLED
84 received_time_ = 0;
85 dont_log_ = false;
86 log_data_ = NULL;
87 #endif
90 Message& Message::operator=(const Message& other) {
91 *static_cast<base::Pickle*>(this) = other;
92 #if defined(OS_POSIX)
93 attachment_set_ = other.attachment_set_;
94 #endif
95 return *this;
98 void 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);
102 header()->routing = routing;
103 header()->type = type;
104 header()->flags = flags;
107 void Message::EnsureMessageAttachmentSet() {
108 if (attachment_set_.get() == NULL)
109 attachment_set_ = new MessageAttachmentSet;
112 #ifdef IPC_MESSAGE_LOG_ENABLED
113 void 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);
119 int64 Message::sent_time() const {
120 if ((header()->flags & HAS_SENT_TIME_BIT) == 0)
121 return 0;
123 const char* data = end_of_payload();
124 data -= sizeof(int64);
125 return *(reinterpret_cast<const int64*>(data));
128 void Message::set_received_time(int64 time) const {
129 received_time_ = time;
131 #endif
133 bool Message::WriteAttachment(scoped_refptr<MessageAttachment> attachment) {
134 // 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.
136 WriteInt(attachment_set()->size());
137 return attachment_set()->AddAttachment(attachment);
140 bool Message::ReadAttachment(
141 base::PickleIterator* iter,
142 scoped_refptr<MessageAttachment>* attachment) const {
143 int descriptor_index;
144 if (!iter->ReadInt(&descriptor_index))
145 return false;
147 MessageAttachmentSet* attachment_set = attachment_set_.get();
148 if (!attachment_set)
149 return false;
151 *attachment = attachment_set->GetAttachmentAt(descriptor_index);
152 return nullptr != attachment->get();
155 bool Message::HasAttachments() const {
156 return attachment_set_.get() && !attachment_set_->empty();
159 bool Message::HasMojoHandles() const {
160 return attachment_set_.get() && 0 < attachment_set_->num_mojo_handles();
163 } // namespace IPC