|
1 // Copyright (c) 2006-2008 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. |
|
4 |
|
5 #ifndef CHROME_COMMON_IPC_SYNC_MESSAGE_H__ |
|
6 #define CHROME_COMMON_IPC_SYNC_MESSAGE_H__ |
|
7 |
|
8 #if defined(OS_WIN) |
|
9 #include <windows.h> |
|
10 #endif |
|
11 #include <string> |
|
12 #include "base/basictypes.h" |
|
13 #include "chrome/common/ipc_message.h" |
|
14 |
|
15 namespace base { |
|
16 class WaitableEvent; |
|
17 } |
|
18 |
|
19 namespace IPC { |
|
20 |
|
21 class MessageReplyDeserializer; |
|
22 |
|
23 class SyncMessage : public Message { |
|
24 public: |
|
25 SyncMessage(int32_t routing_id, uint16_t type, PriorityValue priority, |
|
26 MessageReplyDeserializer* deserializer); |
|
27 |
|
28 // Call this to get a deserializer for the output parameters. |
|
29 // Note that this can only be called once, and the caller is responsible |
|
30 // for deleting the deserializer when they're done. |
|
31 MessageReplyDeserializer* GetReplyDeserializer(); |
|
32 |
|
33 // If this message can cause the receiver to block while waiting for user |
|
34 // input (i.e. by calling MessageBox), then the caller needs to pump window |
|
35 // messages and dispatch asynchronous messages while waiting for the reply. |
|
36 // If this event is passed in, then window messages will start being pumped |
|
37 // when it's set. Note that this behavior will continue even if the event is |
|
38 // later reset. The event must be valid until after the Send call returns. |
|
39 void set_pump_messages_event(base::WaitableEvent* event) { |
|
40 pump_messages_event_ = event; |
|
41 if (event) { |
|
42 header()->flags |= PUMPING_MSGS_BIT; |
|
43 } else { |
|
44 header()->flags &= ~PUMPING_MSGS_BIT; |
|
45 } |
|
46 } |
|
47 |
|
48 // Call this if you always want to pump messages. You can call this method |
|
49 // or set_pump_messages_event but not both. |
|
50 void EnableMessagePumping(); |
|
51 |
|
52 base::WaitableEvent* pump_messages_event() const { |
|
53 return pump_messages_event_; |
|
54 } |
|
55 |
|
56 // Returns true if the message is a reply to the given request id. |
|
57 static bool IsMessageReplyTo(const Message& msg, int request_id); |
|
58 |
|
59 // Given a reply message, returns an iterator to the beginning of the data |
|
60 // (i.e. skips over the synchronous specific data). |
|
61 static void* GetDataIterator(const Message* msg); |
|
62 |
|
63 // Given a synchronous message (or its reply), returns its id. |
|
64 static int GetMessageId(const Message& msg); |
|
65 |
|
66 // Generates a reply message to the given message. |
|
67 static Message* GenerateReply(const Message* msg); |
|
68 |
|
69 private: |
|
70 struct SyncHeader { |
|
71 // unique ID (unique per sender) |
|
72 int message_id; |
|
73 }; |
|
74 |
|
75 static bool ReadSyncHeader(const Message& msg, SyncHeader* header); |
|
76 static bool WriteSyncHeader(Message* msg, const SyncHeader& header); |
|
77 |
|
78 MessageReplyDeserializer* deserializer_; |
|
79 base::WaitableEvent* pump_messages_event_; |
|
80 |
|
81 static uint32_t next_id_; // for generation of unique ids |
|
82 }; |
|
83 |
|
84 // Used to deserialize parameters from a reply to a synchronous message |
|
85 class MessageReplyDeserializer { |
|
86 public: |
|
87 bool SerializeOutputParameters(const Message& msg); |
|
88 virtual ~MessageReplyDeserializer() {} |
|
89 private: |
|
90 // Derived classes need to implement this, using the given iterator (which |
|
91 // is skipped past the header for synchronous messages). |
|
92 virtual bool SerializeOutputParameters(const Message& msg, void* iter) = 0; |
|
93 }; |
|
94 |
|
95 } // namespace IPC |
|
96 |
|
97 #endif // CHROME_COMMON_IPC_SYNC_MESSAGE_H__ |