[WebView] Reword crash handler message.
[chromium-blink-merge.git] / ipc / ipc_message.cc
blob99c56efa56e108fb09f18217ab4b031f65cbc231
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::debug::TraceLog* trace_log = base::debug::TraceLog::GetInstance();
27 uint32 pid = trace_log ? trace_log->process_id() : 0;
28 uint32 count = g_ref_num.GetNext();
29 // The 24 bit hash is composed of 14 bits of the count and 10 bits of the
30 // Process ID. With the current trace event buffer cap, the 14-bit count did
31 // not appear to wrap during a trace. Note that it is not a big deal if
32 // collisions occur, as this is only used for debugging and trace analysis.
33 return ((pid << 14) | (count & 0x3fff)) << 8;
36 } // namespace
38 namespace IPC {
40 //------------------------------------------------------------------------------
42 Message::~Message() {
45 Message::Message()
46 : 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 : 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) : Pickle(data, data_len) {
70 Init();
73 Message::Message(const Message& other) : Pickle(other) {
74 Init();
75 #if defined(OS_POSIX)
76 attachment_set_ = other.attachment_set_;
77 #endif
80 void Message::Init() {
81 dispatch_error_ = false;
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<Pickle*>(this) = other;
91 #if defined(OS_POSIX)
92 attachment_set_ = other.attachment_set_;
93 #endif
94 return *this;
97 void Message::SetHeaderValues(int32 routing, uint32 type, uint32 flags) {
98 // This should only be called when the message is already empty.
99 DCHECK(payload_size() == 0);
101 header()->routing = routing;
102 header()->type = type;
103 header()->flags = flags;
106 void Message::EnsureMessageAttachmentSet() {
107 if (attachment_set_.get() == NULL)
108 attachment_set_ = new MessageAttachmentSet;
111 #ifdef IPC_MESSAGE_LOG_ENABLED
112 void Message::set_sent_time(int64 time) {
113 DCHECK((header()->flags & HAS_SENT_TIME_BIT) == 0);
114 header()->flags |= HAS_SENT_TIME_BIT;
115 WriteInt64(time);
118 int64 Message::sent_time() const {
119 if ((header()->flags & HAS_SENT_TIME_BIT) == 0)
120 return 0;
122 const char* data = end_of_payload();
123 data -= sizeof(int64);
124 return *(reinterpret_cast<const int64*>(data));
127 void Message::set_received_time(int64 time) const {
128 received_time_ = time;
130 #endif
132 bool Message::WriteAttachment(scoped_refptr<MessageAttachment> attachment) {
133 // We write the index of the descriptor so that we don't have to
134 // keep the current descriptor as extra decoding state when deserialising.
135 WriteInt(attachment_set()->size());
136 return attachment_set()->AddAttachment(attachment);
139 bool Message::ReadAttachment(
140 PickleIterator* iter,
141 scoped_refptr<MessageAttachment>* attachment) const {
142 int descriptor_index;
143 if (!iter->ReadInt(&descriptor_index))
144 return false;
146 MessageAttachmentSet* attachment_set = attachment_set_.get();
147 if (!attachment_set)
148 return false;
150 *attachment = attachment_set->GetAttachmentAt(descriptor_index);
151 return nullptr != attachment->get();
154 bool Message::HasAttachments() const {
155 return attachment_set_.get() && !attachment_set_->empty();
158 bool Message::HasMojoHandles() const {
159 return attachment_set_.get() && 0 < attachment_set_->num_mojo_handles();
162 } // namespace IPC