Roll src/third_party/WebKit f298044:aa8346d (svn 202628:202629)
[chromium-blink-merge.git] / ipc / ipc_sync_message.h
blob904a9c8505c6caf66be81e8668671b631eb4e19a
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_
8 #include <stdint.h>
9 #if defined(OS_WIN)
10 #include <windows.h>
11 #endif
13 #include <string>
15 #include "base/memory/scoped_ptr.h"
16 #include "ipc/ipc_message.h"
18 namespace base {
19 class WaitableEvent;
22 namespace IPC {
24 class MessageReplyDeserializer;
26 class IPC_EXPORT SyncMessage : public Message {
27 public:
28 SyncMessage(int32_t routing_id,
29 uint32_t type,
30 PriorityValue priority,
31 MessageReplyDeserializer* deserializer);
32 ~SyncMessage() override;
34 // Call this to get a deserializer for the output parameters.
35 // Note that this can only be called once, and the caller is responsible
36 // for deleting the deserializer when they're done.
37 MessageReplyDeserializer* GetReplyDeserializer();
39 // If this message can cause the receiver to block while waiting for user
40 // input (i.e. by calling MessageBox), then the caller needs to pump window
41 // messages and dispatch asynchronous messages while waiting for the reply.
42 // If this event is passed in, then window messages will start being pumped
43 // when it's set. Note that this behavior will continue even if the event is
44 // later reset. The event must be valid until after the Send call returns.
45 void set_pump_messages_event(base::WaitableEvent* event) {
46 pump_messages_event_ = event;
47 if (event) {
48 header()->flags |= PUMPING_MSGS_BIT;
49 } else {
50 header()->flags &= ~PUMPING_MSGS_BIT;
54 // Call this if you always want to pump messages. You can call this method
55 // or set_pump_messages_event but not both.
56 void EnableMessagePumping();
58 base::WaitableEvent* pump_messages_event() const {
59 return pump_messages_event_;
62 // Returns true if the message is a reply to the given request id.
63 static bool IsMessageReplyTo(const Message& msg, int request_id);
65 // Given a reply message, returns an iterator to the beginning of the data
66 // (i.e. skips over the synchronous specific data).
67 static base::PickleIterator GetDataIterator(const Message* msg);
69 // Given a synchronous message (or its reply), returns its id.
70 static int GetMessageId(const Message& msg);
72 // Generates a reply message to the given message.
73 static Message* GenerateReply(const Message* msg);
75 private:
76 struct SyncHeader {
77 // unique ID (unique per sender)
78 int message_id;
81 static bool ReadSyncHeader(const Message& msg, SyncHeader* header);
82 static bool WriteSyncHeader(Message* msg, const SyncHeader& header);
84 scoped_ptr<MessageReplyDeserializer> deserializer_;
85 base::WaitableEvent* pump_messages_event_;
88 // Used to deserialize parameters from a reply to a synchronous message
89 class IPC_EXPORT MessageReplyDeserializer {
90 public:
91 virtual ~MessageReplyDeserializer() {}
92 bool SerializeOutputParameters(const Message& msg);
93 private:
94 // Derived classes need to implement this, using the given iterator (which
95 // is skipped past the header for synchronous messages).
96 virtual bool SerializeOutputParameters(const Message& msg,
97 base::PickleIterator iter) = 0;
100 // When sending a synchronous message, this structure contains an object
101 // that knows how to deserialize the response.
102 struct PendingSyncMsg {
103 PendingSyncMsg(int id,
104 MessageReplyDeserializer* d,
105 base::WaitableEvent* e)
106 : id(id), deserializer(d), done_event(e), send_result(false) { }
107 int id;
108 MessageReplyDeserializer* deserializer;
109 base::WaitableEvent* done_event;
110 bool send_result;
113 } // namespace IPC
115 #endif // IPC_IPC_SYNC_MESSAGE_H_