[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | 20cb5f48 | 2009-12-16 01:01:25 | [diff] [blame] | 5 | #ifndef IPC_IPC_SYNC_MESSAGE_H_ |
| 6 | #define IPC_IPC_SYNC_MESSAGE_H_ |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 7 | |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 8 | #include <stdint.h> |
[email protected] | d4651ff | 2008-12-02 16:51:58 | [diff] [blame] | 9 | #if defined(OS_WIN) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 10 | #include <windows.h> |
[email protected] | d4651ff | 2008-12-02 16:51:58 | [diff] [blame] | 11 | #endif |
tfarina | 4da8275d | 2015-09-16 09:56:21 | [diff] [blame] | 12 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 13 | #include <string> |
tfarina | 4da8275d | 2015-09-16 09:56:21 | [diff] [blame] | 14 | |
[email protected] | 40c4627d | 2011-09-09 21:10:31 | [diff] [blame] | 15 | #include "base/memory/scoped_ptr.h" |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 16 | #include "ipc/ipc_message.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 17 | |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 18 | namespace base { |
| 19 | class WaitableEvent; |
| 20 | } |
| 21 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 22 | namespace IPC { |
| 23 | |
| 24 | class MessageReplyDeserializer; |
| 25 | |
[email protected] | 7c85437 | 2011-08-15 20:41:46 | [diff] [blame] | 26 | class IPC_EXPORT SyncMessage : public Message { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 27 | public: |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 28 | SyncMessage(int32_t routing_id, |
| 29 | uint32_t type, |
| 30 | PriorityValue priority, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 31 | MessageReplyDeserializer* deserializer); |
dcheng | fe61fca | 2014-10-22 02:29:52 | [diff] [blame] | 32 | ~SyncMessage() override; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 33 | |
| 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(); |
| 38 | |
| 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. |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 42 | // If this event is passed in, then window messages will start being pumped |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 43 | // when it's set. Note that this behavior will continue even if the event is |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 44 | // later reset. The event must be valid until after the Send call returns. |
| 45 | void set_pump_messages_event(base::WaitableEvent* event) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 46 | pump_messages_event_ = event; |
| 47 | if (event) { |
| 48 | header()->flags |= PUMPING_MSGS_BIT; |
| 49 | } else { |
| 50 | header()->flags &= ~PUMPING_MSGS_BIT; |
| 51 | } |
| 52 | } |
| 53 | |
[email protected] | d65cab7a | 2008-08-12 01:25:41 | [diff] [blame] | 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(); |
| 57 | |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 58 | base::WaitableEvent* pump_messages_event() const { |
| 59 | return pump_messages_event_; |
| 60 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 61 | |
| 62 | // Returns true if the message is a reply to the given request id. |
| 63 | static bool IsMessageReplyTo(const Message& msg, int request_id); |
| 64 | |
| 65 | // Given a reply message, returns an iterator to the beginning of the data |
| 66 | // (i.e. skips over the synchronous specific data). |
brettw | 05cfd8ddb | 2015-06-02 07:02:47 | [diff] [blame] | 67 | static base::PickleIterator GetDataIterator(const Message* msg); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 68 | |
| 69 | // Given a synchronous message (or its reply), returns its id. |
| 70 | static int GetMessageId(const Message& msg); |
| 71 | |
| 72 | // Generates a reply message to the given message. |
| 73 | static Message* GenerateReply(const Message* msg); |
| 74 | |
| 75 | private: |
| 76 | struct SyncHeader { |
| 77 | // unique ID (unique per sender) |
| 78 | int message_id; |
| 79 | }; |
| 80 | |
| 81 | static bool ReadSyncHeader(const Message& msg, SyncHeader* header); |
| 82 | static bool WriteSyncHeader(Message* msg, const SyncHeader& header); |
| 83 | |
[email protected] | 40c4627d | 2011-09-09 21:10:31 | [diff] [blame] | 84 | scoped_ptr<MessageReplyDeserializer> deserializer_; |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 85 | base::WaitableEvent* pump_messages_event_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 86 | }; |
| 87 | |
| 88 | // Used to deserialize parameters from a reply to a synchronous message |
[email protected] | 7c85437 | 2011-08-15 20:41:46 | [diff] [blame] | 89 | class IPC_EXPORT MessageReplyDeserializer { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 90 | public: |
[email protected] | 20cb5f48 | 2009-12-16 01:01:25 | [diff] [blame] | 91 | virtual ~MessageReplyDeserializer() {} |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 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). |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 96 | virtual bool SerializeOutputParameters(const Message& msg, |
brettw | 05cfd8ddb | 2015-06-02 07:02:47 | [diff] [blame] | 97 | base::PickleIterator iter) = 0; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 98 | }; |
| 99 | |
[email protected] | 1e9499c | 2010-04-06 20:33:36 | [diff] [blame] | 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; |
| 111 | }; |
| 112 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 113 | } // namespace IPC |
| 114 | |
[email protected] | 20cb5f48 | 2009-12-16 01:01:25 | [diff] [blame] | 115 | #endif // IPC_IPC_SYNC_MESSAGE_H_ |