Add UMAs to measure Android tab state transition
[chromium-blink-merge.git] / ipc / ipc_message.cc
blob07d5d254759a23d208829d86d924c5e207a56ce3
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 attachment_set_ = other.attachment_set_;
79 void Message::Init() {
80 dispatch_error_ = false;
81 sender_pid_ = base::kNullProcessId;
82 #ifdef IPC_MESSAGE_LOG_ENABLED
83 received_time_ = 0;
84 dont_log_ = false;
85 log_data_ = NULL;
86 #endif
89 Message& Message::operator=(const Message& other) {
90 *static_cast<base::Pickle*>(this) = other;
91 attachment_set_ = other.attachment_set_;
92 return *this;
95 void Message::SetHeaderValues(int32 routing, uint32 type, uint32 flags) {
96 // This should only be called when the message is already empty.
97 DCHECK(payload_size() == 0);
99 header()->routing = routing;
100 header()->type = type;
101 header()->flags = flags;
104 void Message::EnsureMessageAttachmentSet() {
105 if (attachment_set_.get() == NULL)
106 attachment_set_ = new MessageAttachmentSet;
109 #ifdef IPC_MESSAGE_LOG_ENABLED
110 void Message::set_sent_time(int64 time) {
111 DCHECK((header()->flags & HAS_SENT_TIME_BIT) == 0);
112 header()->flags |= HAS_SENT_TIME_BIT;
113 WriteInt64(time);
116 int64 Message::sent_time() const {
117 if ((header()->flags & HAS_SENT_TIME_BIT) == 0)
118 return 0;
120 const char* data = end_of_payload();
121 data -= sizeof(int64);
122 return *(reinterpret_cast<const int64*>(data));
125 void Message::set_received_time(int64 time) const {
126 received_time_ = time;
128 #endif
130 bool Message::WriteAttachment(scoped_refptr<MessageAttachment> attachment) {
131 // We write the index of the descriptor so that we don't have to
132 // keep the current descriptor as extra decoding state when deserialising.
133 WriteInt(attachment_set()->size());
134 return attachment_set()->AddAttachment(attachment);
137 bool Message::ReadAttachment(
138 base::PickleIterator* iter,
139 scoped_refptr<MessageAttachment>* attachment) const {
140 int descriptor_index;
141 if (!iter->ReadInt(&descriptor_index))
142 return false;
144 MessageAttachmentSet* attachment_set = attachment_set_.get();
145 if (!attachment_set)
146 return false;
148 *attachment = attachment_set->GetAttachmentAt(descriptor_index);
149 return nullptr != attachment->get();
152 bool Message::HasAttachments() const {
153 return attachment_set_.get() && !attachment_set_->empty();
156 bool Message::HasMojoHandles() const {
157 return attachment_set_.get() && attachment_set_->num_mojo_handles() > 0;
160 bool Message::HasBrokerableAttachments() const {
161 return attachment_set_.get() &&
162 attachment_set_->num_brokerable_attachments() > 0;
165 } // namespace IPC