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