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 #ifndef IPC_IPC_SYNC_MESSAGE_H_
6 #define IPC_IPC_SYNC_MESSAGE_H_
13 #include "base/basictypes.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "ipc/ipc_message.h"
23 class MessageReplyDeserializer
;
25 class IPC_EXPORT SyncMessage
: public Message
{
27 SyncMessage(int32_t routing_id
,
29 PriorityValue priority
,
30 MessageReplyDeserializer
* deserializer
);
31 ~SyncMessage() override
;
33 // Call this to get a deserializer for the output parameters.
34 // Note that this can only be called once, and the caller is responsible
35 // for deleting the deserializer when they're done.
36 MessageReplyDeserializer
* GetReplyDeserializer();
38 // If this message can cause the receiver to block while waiting for user
39 // input (i.e. by calling MessageBox), then the caller needs to pump window
40 // messages and dispatch asynchronous messages while waiting for the reply.
41 // If this event is passed in, then window messages will start being pumped
42 // when it's set. Note that this behavior will continue even if the event is
43 // later reset. The event must be valid until after the Send call returns.
44 void set_pump_messages_event(base::WaitableEvent
* event
) {
45 pump_messages_event_
= event
;
47 header()->flags
|= PUMPING_MSGS_BIT
;
49 header()->flags
&= ~PUMPING_MSGS_BIT
;
53 // Call this if you always want to pump messages. You can call this method
54 // or set_pump_messages_event but not both.
55 void EnableMessagePumping();
57 base::WaitableEvent
* pump_messages_event() const {
58 return pump_messages_event_
;
61 // Returns true if the message is a reply to the given request id.
62 static bool IsMessageReplyTo(const Message
& msg
, int request_id
);
64 // Given a reply message, returns an iterator to the beginning of the data
65 // (i.e. skips over the synchronous specific data).
66 static base::PickleIterator
GetDataIterator(const Message
* msg
);
68 // Given a synchronous message (or its reply), returns its id.
69 static int GetMessageId(const Message
& msg
);
71 // Generates a reply message to the given message.
72 static Message
* GenerateReply(const Message
* msg
);
76 // unique ID (unique per sender)
80 static bool ReadSyncHeader(const Message
& msg
, SyncHeader
* header
);
81 static bool WriteSyncHeader(Message
* msg
, const SyncHeader
& header
);
83 scoped_ptr
<MessageReplyDeserializer
> deserializer_
;
84 base::WaitableEvent
* pump_messages_event_
;
87 // Used to deserialize parameters from a reply to a synchronous message
88 class IPC_EXPORT MessageReplyDeserializer
{
90 virtual ~MessageReplyDeserializer() {}
91 bool SerializeOutputParameters(const Message
& msg
);
93 // Derived classes need to implement this, using the given iterator (which
94 // is skipped past the header for synchronous messages).
95 virtual bool SerializeOutputParameters(const Message
& msg
,
96 base::PickleIterator iter
) = 0;
99 // When sending a synchronous message, this structure contains an object
100 // that knows how to deserialize the response.
101 struct PendingSyncMsg
{
102 PendingSyncMsg(int id
,
103 MessageReplyDeserializer
* d
,
104 base::WaitableEvent
* e
)
105 : id(id
), deserializer(d
), done_event(e
), send_result(false) { }
107 MessageReplyDeserializer
* deserializer
;
108 base::WaitableEvent
* done_event
;
114 #endif // IPC_IPC_SYNC_MESSAGE_H_