dbus: Property<>.value() should be const
[chromium-blink-merge.git] / ipc / ipc_sync_message.cc
blob875f97b1fec167162f83a5919f7fe8e4cf91b534
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 "build/build_config.h"
7 #if defined(OS_WIN)
8 #include <windows.h>
9 #endif
10 #include <stack>
12 #include "base/atomic_sequence_num.h"
13 #include "base/lazy_instance.h"
14 #include "base/logging.h"
15 #include "base/synchronization/waitable_event.h"
16 #include "ipc/ipc_sync_message.h"
18 namespace {
20 struct WaitableEventLazyInstanceTraits
21 : public base::DefaultLazyInstanceTraits<base::WaitableEvent> {
22 static base::WaitableEvent* New(void* instance) {
23 // Use placement new to initialize our instance in our preallocated space.
24 return new (instance) base::WaitableEvent(true, true);
28 static base::LazyInstance<base::WaitableEvent, WaitableEventLazyInstanceTraits>
29 dummy_event = LAZY_INSTANCE_INITIALIZER;
33 namespace IPC {
35 #define kSyncMessageHeaderSize 4
37 static base::AtomicSequenceNumber g_next_id(base::LINKER_INITIALIZED);
39 SyncMessage::SyncMessage(
40 int32 routing_id,
41 uint32 type,
42 PriorityValue priority,
43 MessageReplyDeserializer* deserializer)
44 : Message(routing_id, type, priority),
45 deserializer_(deserializer),
46 pump_messages_event_(NULL)
48 set_sync();
49 set_unblock(true);
51 // Add synchronous message data before the message payload.
52 SyncHeader header;
53 header.message_id = g_next_id.GetNext();
54 WriteSyncHeader(this, header);
57 SyncMessage::~SyncMessage() {
60 MessageReplyDeserializer* SyncMessage::GetReplyDeserializer() {
61 DCHECK(deserializer_.get());
62 return deserializer_.release();
65 void SyncMessage::EnableMessagePumping() {
66 DCHECK(!pump_messages_event_);
67 set_pump_messages_event(dummy_event.Pointer());
70 bool SyncMessage::IsMessageReplyTo(const Message& msg, int request_id) {
71 if (!msg.is_reply())
72 return false;
74 return GetMessageId(msg) == request_id;
77 void* SyncMessage::GetDataIterator(const Message* msg) {
78 void* iter = const_cast<char*>(msg->payload());
79 UpdateIter(&iter, kSyncMessageHeaderSize);
80 return iter;
83 int SyncMessage::GetMessageId(const Message& msg) {
84 if (!msg.is_sync() && !msg.is_reply())
85 return 0;
87 SyncHeader header;
88 if (!ReadSyncHeader(msg, &header))
89 return 0;
91 return header.message_id;
94 Message* SyncMessage::GenerateReply(const Message* msg) {
95 DCHECK(msg->is_sync());
97 Message* reply = new Message(msg->routing_id(), IPC_REPLY_ID,
98 msg->priority());
99 reply->set_reply();
101 SyncHeader header;
103 // use the same message id, but this time reply bit is set
104 header.message_id = GetMessageId(*msg);
105 WriteSyncHeader(reply, header);
107 return reply;
110 bool SyncMessage::ReadSyncHeader(const Message& msg, SyncHeader* header) {
111 DCHECK(msg.is_sync() || msg.is_reply());
113 void* iter = NULL;
114 bool result = msg.ReadInt(&iter, &header->message_id);
115 if (!result) {
116 NOTREACHED();
117 return false;
120 return true;
123 bool SyncMessage::WriteSyncHeader(Message* msg, const SyncHeader& header) {
124 DCHECK(msg->is_sync() || msg->is_reply());
125 DCHECK(msg->payload_size() == 0);
126 bool result = msg->WriteInt(header.message_id);
127 if (!result) {
128 NOTREACHED();
129 return false;
132 // Note: if you add anything here, you need to update kSyncMessageHeaderSize.
133 DCHECK(kSyncMessageHeaderSize == msg->payload_size());
135 return true;
139 bool MessageReplyDeserializer::SerializeOutputParameters(const Message& msg) {
140 return SerializeOutputParameters(msg, SyncMessage::GetDataIterator(&msg));
143 } // namespace IPC