[email protected] | 76a6d45 | 2012-02-03 21:48:43 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | cd40b4a | 2008-12-22 23:58:52 | [diff] [blame] | 5 | #include "build/build_config.h" |
| 6 | |
[email protected] | 80058055 | 2008-12-22 23:10:32 | [diff] [blame] | 7 | #if defined(OS_WIN) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 8 | #include <windows.h> |
[email protected] | 80058055 | 2008-12-22 23:10:32 | [diff] [blame] | 9 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 10 | #include <stack> |
| 11 | |
[email protected] | 1e9499c | 2010-04-06 20:33:36 | [diff] [blame] | 12 | #include "base/atomic_sequence_num.h" |
[email protected] | 76a6d45 | 2012-02-03 21:48:43 | [diff] [blame] | 13 | #include "base/lazy_instance.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 14 | #include "base/logging.h" |
[email protected] | 44f9c95 | 2011-01-02 06:05:39 | [diff] [blame] | 15 | #include "base/synchronization/waitable_event.h" |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 16 | #include "ipc/ipc_sync_message.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 17 | |
[email protected] | 76a6d45 | 2012-02-03 21:48:43 | [diff] [blame] | 18 | namespace { |
| 19 | |
| 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); |
| 25 | } |
| 26 | }; |
| 27 | |
[email protected] | 8a8443f | 2012-03-13 12:07:19 | [diff] [blame] | 28 | base::LazyInstance<base::WaitableEvent, WaitableEventLazyInstanceTraits> |
[email protected] | 76a6d45 | 2012-02-03 21:48:43 | [diff] [blame] | 29 | dummy_event = LAZY_INSTANCE_INITIALIZER; |
| 30 | |
[email protected] | 8a8443f | 2012-03-13 12:07:19 | [diff] [blame] | 31 | base::StaticAtomicSequenceNumber g_next_id; |
| 32 | |
| 33 | } // namespace |
[email protected] | 76a6d45 | 2012-02-03 21:48:43 | [diff] [blame] | 34 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 35 | namespace IPC { |
| 36 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 37 | #define kSyncMessageHeaderSize 4 |
| 38 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 39 | SyncMessage::SyncMessage( |
| 40 | int32 routing_id, |
[email protected] | 168ae92 | 2009-12-04 18:08:45 | [diff] [blame] | 41 | uint32 type, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 42 | PriorityValue priority, |
| 43 | MessageReplyDeserializer* deserializer) |
| 44 | : Message(routing_id, type, priority), |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 45 | deserializer_(deserializer), |
| 46 | pump_messages_event_(NULL) |
[email protected] | 80058055 | 2008-12-22 23:10:32 | [diff] [blame] | 47 | { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 48 | set_sync(); |
| 49 | set_unblock(true); |
| 50 | |
| 51 | // Add synchronous message data before the message payload. |
| 52 | SyncHeader header; |
[email protected] | 1e9499c | 2010-04-06 20:33:36 | [diff] [blame] | 53 | header.message_id = g_next_id.GetNext(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 54 | WriteSyncHeader(this, header); |
| 55 | } |
| 56 | |
[email protected] | 40c4627d | 2011-09-09 21:10:31 | [diff] [blame] | 57 | SyncMessage::~SyncMessage() { |
| 58 | } |
| 59 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 60 | MessageReplyDeserializer* SyncMessage::GetReplyDeserializer() { |
[email protected] | 40c4627d | 2011-09-09 21:10:31 | [diff] [blame] | 61 | DCHECK(deserializer_.get()); |
| 62 | return deserializer_.release(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 63 | } |
| 64 | |
[email protected] | d65cab7a | 2008-08-12 01:25:41 | [diff] [blame] | 65 | void SyncMessage::EnableMessagePumping() { |
| 66 | DCHECK(!pump_messages_event_); |
[email protected] | 76a6d45 | 2012-02-03 21:48:43 | [diff] [blame] | 67 | set_pump_messages_event(dummy_event.Pointer()); |
[email protected] | d65cab7a | 2008-08-12 01:25:41 | [diff] [blame] | 68 | } |
| 69 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 70 | bool SyncMessage::IsMessageReplyTo(const Message& msg, int request_id) { |
| 71 | if (!msg.is_reply()) |
| 72 | return false; |
| 73 | |
| 74 | return GetMessageId(msg) == request_id; |
| 75 | } |
| 76 | |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 77 | PickleIterator SyncMessage::GetDataIterator(const Message* msg) { |
| 78 | PickleIterator iter(*msg); |
| 79 | if (!iter.SkipBytes(kSyncMessageHeaderSize)) |
| 80 | return PickleIterator(); |
| 81 | else |
| 82 | return iter; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | int SyncMessage::GetMessageId(const Message& msg) { |
| 86 | if (!msg.is_sync() && !msg.is_reply()) |
| 87 | return 0; |
| 88 | |
| 89 | SyncHeader header; |
| 90 | if (!ReadSyncHeader(msg, &header)) |
| 91 | return 0; |
| 92 | |
| 93 | return header.message_id; |
| 94 | } |
| 95 | |
| 96 | Message* SyncMessage::GenerateReply(const Message* msg) { |
| 97 | DCHECK(msg->is_sync()); |
| 98 | |
[email protected] | d321644 | 2009-03-05 21:07:27 | [diff] [blame] | 99 | Message* reply = new Message(msg->routing_id(), IPC_REPLY_ID, |
| 100 | msg->priority()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 101 | reply->set_reply(); |
| 102 | |
| 103 | SyncHeader header; |
| 104 | |
| 105 | // use the same message id, but this time reply bit is set |
| 106 | header.message_id = GetMessageId(*msg); |
| 107 | WriteSyncHeader(reply, header); |
| 108 | |
| 109 | return reply; |
| 110 | } |
| 111 | |
| 112 | bool SyncMessage::ReadSyncHeader(const Message& msg, SyncHeader* header) { |
| 113 | DCHECK(msg.is_sync() || msg.is_reply()); |
| 114 | |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 115 | PickleIterator iter(msg); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 116 | bool result = msg.ReadInt(&iter, &header->message_id); |
| 117 | if (!result) { |
| 118 | NOTREACHED(); |
| 119 | return false; |
| 120 | } |
| 121 | |
| 122 | return true; |
| 123 | } |
| 124 | |
| 125 | bool SyncMessage::WriteSyncHeader(Message* msg, const SyncHeader& header) { |
| 126 | DCHECK(msg->is_sync() || msg->is_reply()); |
| 127 | DCHECK(msg->payload_size() == 0); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 128 | bool result = msg->WriteInt(header.message_id); |
| 129 | if (!result) { |
| 130 | NOTREACHED(); |
| 131 | return false; |
| 132 | } |
| 133 | |
| 134 | // Note: if you add anything here, you need to update kSyncMessageHeaderSize. |
| 135 | DCHECK(kSyncMessageHeaderSize == msg->payload_size()); |
| 136 | |
| 137 | return true; |
| 138 | } |
| 139 | |
| 140 | |
| 141 | bool MessageReplyDeserializer::SerializeOutputParameters(const Message& msg) { |
| 142 | return SerializeOutputParameters(msg, SyncMessage::GetDataIterator(&msg)); |
| 143 | } |
| 144 | |
| 145 | } // namespace IPC |