jbates@chromium.org | 0fc8736 | 2012-03-08 05:42:56 +0900 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
jamesr@chromium.org | ab4c65e | 2009-12-16 10:01:25 +0900 | [diff] [blame] | 5 | #ifndef IPC_IPC_SYNC_MESSAGE_H_ |
| 6 | #define IPC_IPC_SYNC_MESSAGE_H_ |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 7 | |
| 8 | #if defined(OS_WIN) |
| 9 | #include <windows.h> |
| 10 | #endif |
| 11 | #include <string> |
| 12 | #include "base/basictypes.h" |
dmichael@chromium.org | 18bbd0d | 2011-09-10 06:10:31 +0900 | [diff] [blame] | 13 | #include "base/memory/scoped_ptr.h" |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 14 | #include "ipc/ipc_message.h" |
| 15 | |
| 16 | namespace base { |
| 17 | class WaitableEvent; |
| 18 | } |
| 19 | |
| 20 | namespace IPC { |
| 21 | |
| 22 | class MessageReplyDeserializer; |
| 23 | |
darin@chromium.org | 80e4c5e | 2011-08-16 05:41:46 +0900 | [diff] [blame] | 24 | class IPC_EXPORT SyncMessage : public Message { |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 25 | public: |
apatrick@google.com | a240677 | 2009-12-05 03:08:45 +0900 | [diff] [blame] | 26 | SyncMessage(int32 routing_id, uint32 type, PriorityValue priority, |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 27 | MessageReplyDeserializer* deserializer); |
dmichael@chromium.org | 18bbd0d | 2011-09-10 06:10:31 +0900 | [diff] [blame] | 28 | virtual ~SyncMessage(); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 29 | |
| 30 | // Call this to get a deserializer for the output parameters. |
| 31 | // Note that this can only be called once, and the caller is responsible |
| 32 | // for deleting the deserializer when they're done. |
| 33 | MessageReplyDeserializer* GetReplyDeserializer(); |
| 34 | |
| 35 | // If this message can cause the receiver to block while waiting for user |
| 36 | // input (i.e. by calling MessageBox), then the caller needs to pump window |
| 37 | // messages and dispatch asynchronous messages while waiting for the reply. |
| 38 | // If this event is passed in, then window messages will start being pumped |
| 39 | // when it's set. Note that this behavior will continue even if the event is |
| 40 | // later reset. The event must be valid until after the Send call returns. |
| 41 | void set_pump_messages_event(base::WaitableEvent* event) { |
| 42 | pump_messages_event_ = event; |
| 43 | if (event) { |
| 44 | header()->flags |= PUMPING_MSGS_BIT; |
| 45 | } else { |
| 46 | header()->flags &= ~PUMPING_MSGS_BIT; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | // Call this if you always want to pump messages. You can call this method |
| 51 | // or set_pump_messages_event but not both. |
| 52 | void EnableMessagePumping(); |
| 53 | |
| 54 | base::WaitableEvent* pump_messages_event() const { |
| 55 | return pump_messages_event_; |
| 56 | } |
| 57 | |
| 58 | // Returns true if the message is a reply to the given request id. |
| 59 | static bool IsMessageReplyTo(const Message& msg, int request_id); |
| 60 | |
| 61 | // Given a reply message, returns an iterator to the beginning of the data |
| 62 | // (i.e. skips over the synchronous specific data). |
jbates@chromium.org | 0fc8736 | 2012-03-08 05:42:56 +0900 | [diff] [blame] | 63 | static PickleIterator GetDataIterator(const Message* msg); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 64 | |
| 65 | // Given a synchronous message (or its reply), returns its id. |
| 66 | static int GetMessageId(const Message& msg); |
| 67 | |
| 68 | // Generates a reply message to the given message. |
| 69 | static Message* GenerateReply(const Message* msg); |
| 70 | |
| 71 | private: |
| 72 | struct SyncHeader { |
| 73 | // unique ID (unique per sender) |
| 74 | int message_id; |
| 75 | }; |
| 76 | |
| 77 | static bool ReadSyncHeader(const Message& msg, SyncHeader* header); |
| 78 | static bool WriteSyncHeader(Message* msg, const SyncHeader& header); |
| 79 | |
dmichael@chromium.org | 18bbd0d | 2011-09-10 06:10:31 +0900 | [diff] [blame] | 80 | scoped_ptr<MessageReplyDeserializer> deserializer_; |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 81 | base::WaitableEvent* pump_messages_event_; |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 82 | }; |
| 83 | |
| 84 | // Used to deserialize parameters from a reply to a synchronous message |
darin@chromium.org | 80e4c5e | 2011-08-16 05:41:46 +0900 | [diff] [blame] | 85 | class IPC_EXPORT MessageReplyDeserializer { |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 86 | public: |
jamesr@chromium.org | ab4c65e | 2009-12-16 10:01:25 +0900 | [diff] [blame] | 87 | virtual ~MessageReplyDeserializer() {} |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 88 | bool SerializeOutputParameters(const Message& msg); |
| 89 | private: |
| 90 | // Derived classes need to implement this, using the given iterator (which |
| 91 | // is skipped past the header for synchronous messages). |
jbates@chromium.org | 0fc8736 | 2012-03-08 05:42:56 +0900 | [diff] [blame] | 92 | virtual bool SerializeOutputParameters(const Message& msg, |
| 93 | PickleIterator iter) = 0; |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 94 | }; |
| 95 | |
jabdelmalek@google.com | eb92165 | 2010-04-07 05:33:36 +0900 | [diff] [blame] | 96 | // When sending a synchronous message, this structure contains an object |
| 97 | // that knows how to deserialize the response. |
| 98 | struct PendingSyncMsg { |
| 99 | PendingSyncMsg(int id, |
| 100 | MessageReplyDeserializer* d, |
| 101 | base::WaitableEvent* e) |
| 102 | : id(id), deserializer(d), done_event(e), send_result(false) { } |
| 103 | int id; |
| 104 | MessageReplyDeserializer* deserializer; |
| 105 | base::WaitableEvent* done_event; |
| 106 | bool send_result; |
| 107 | }; |
| 108 | |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 109 | } // namespace IPC |
| 110 | |
jamesr@chromium.org | ab4c65e | 2009-12-16 10:01:25 +0900 | [diff] [blame] | 111 | #endif // IPC_IPC_SYNC_MESSAGE_H_ |