[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> |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 9 | |
[email protected] | d4651ff | 2008-12-02 16:51:58 | [diff] [blame] | 10 | #if defined(OS_WIN) |
Bruce Dawson | bfdc3fd | 2018-01-03 20:32:36 | [diff] [blame] | 11 | #include "base/win/windows_types.h" |
[email protected] | d4651ff | 2008-12-02 16:51:58 | [diff] [blame] | 12 | #endif |
tfarina | 4da8275 | 2015-09-16 09:56:21 | [diff] [blame] | 13 | |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 14 | #include <memory> |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 15 | #include <string> |
tfarina | 4da8275 | 2015-09-16 09:56:21 | [diff] [blame] | 16 | |
avi | 246998d8 | 2015-12-22 02:39:04 | [diff] [blame] | 17 | #include "build/build_config.h" |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 18 | #include "ipc/ipc_message.h" |
Ken Rockot | fd90763 | 2017-09-14 04:23:41 | [diff] [blame] | 19 | #include "ipc/ipc_message_support_export.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 20 | |
rockot | b62e2e3 | 2017-03-24 18:36:44 | [diff] [blame] | 21 | namespace base { |
| 22 | class WaitableEvent; |
| 23 | } |
| 24 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 25 | namespace IPC { |
| 26 | |
| 27 | class MessageReplyDeserializer; |
| 28 | |
Ken Rockot | fd90763 | 2017-09-14 04:23:41 | [diff] [blame] | 29 | class IPC_MESSAGE_SUPPORT_EXPORT SyncMessage : public Message { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 30 | public: |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 31 | SyncMessage(int32_t routing_id, |
| 32 | uint32_t type, |
| 33 | PriorityValue priority, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 34 | MessageReplyDeserializer* deserializer); |
dcheng | fe61fca | 2014-10-22 02:29:52 | [diff] [blame] | 35 | ~SyncMessage() override; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 36 | |
| 37 | // Call this to get a deserializer for the output parameters. |
| 38 | // Note that this can only be called once, and the caller is responsible |
| 39 | // for deleting the deserializer when they're done. |
| 40 | MessageReplyDeserializer* GetReplyDeserializer(); |
| 41 | |
| 42 | // If this message can cause the receiver to block while waiting for user |
| 43 | // input (i.e. by calling MessageBox), then the caller needs to pump window |
| 44 | // messages and dispatch asynchronous messages while waiting for the reply. |
rockot | 3c23629 | 2016-07-07 20:26:02 | [diff] [blame] | 45 | // This call enables message pumping behavior while waiting for a reply to |
| 46 | // this message. |
| 47 | void EnableMessagePumping() { |
| 48 | header()->flags |= PUMPING_MSGS_BIT; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 49 | } |
| 50 | |
rockot | 3c23629 | 2016-07-07 20:26:02 | [diff] [blame] | 51 | // Indicates whether window messages should be pumped while waiting for a |
| 52 | // reply to this message. |
| 53 | bool ShouldPumpMessages() const { |
| 54 | return (header()->flags & PUMPING_MSGS_BIT) != 0; |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 55 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 56 | |
| 57 | // Returns true if the message is a reply to the given request id. |
| 58 | static bool IsMessageReplyTo(const Message& msg, int request_id); |
| 59 | |
| 60 | // Given a reply message, returns an iterator to the beginning of the data |
| 61 | // (i.e. skips over the synchronous specific data). |
brettw | 05cfd8ddb | 2015-06-02 07:02:47 | [diff] [blame] | 62 | static base::PickleIterator GetDataIterator(const Message* msg); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 63 | |
| 64 | // Given a synchronous message (or its reply), returns its id. |
| 65 | static int GetMessageId(const Message& msg); |
| 66 | |
| 67 | // Generates a reply message to the given message. |
| 68 | static Message* GenerateReply(const Message* msg); |
| 69 | |
| 70 | private: |
| 71 | struct SyncHeader { |
| 72 | // unique ID (unique per sender) |
| 73 | int message_id; |
| 74 | }; |
| 75 | |
| 76 | static bool ReadSyncHeader(const Message& msg, SyncHeader* header); |
| 77 | static bool WriteSyncHeader(Message* msg, const SyncHeader& header); |
| 78 | |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 79 | std::unique_ptr<MessageReplyDeserializer> deserializer_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 80 | }; |
| 81 | |
| 82 | // Used to deserialize parameters from a reply to a synchronous message |
Ken Rockot | fd90763 | 2017-09-14 04:23:41 | [diff] [blame] | 83 | class IPC_MESSAGE_SUPPORT_EXPORT MessageReplyDeserializer { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 84 | public: |
[email protected] | 20cb5f48 | 2009-12-16 01:01:25 | [diff] [blame] | 85 | virtual ~MessageReplyDeserializer() {} |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 86 | bool SerializeOutputParameters(const Message& msg); |
| 87 | private: |
| 88 | // Derived classes need to implement this, using the given iterator (which |
| 89 | // is skipped past the header for synchronous messages). |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 90 | virtual bool SerializeOutputParameters(const Message& msg, |
brettw | 05cfd8ddb | 2015-06-02 07:02:47 | [diff] [blame] | 91 | base::PickleIterator iter) = 0; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 92 | }; |
| 93 | |
[email protected] | 1e9499c | 2010-04-06 20:33:36 | [diff] [blame] | 94 | // When sending a synchronous message, this structure contains an object |
| 95 | // that knows how to deserialize the response. |
| 96 | struct PendingSyncMsg { |
rockot | b62e2e3 | 2017-03-24 18:36:44 | [diff] [blame] | 97 | PendingSyncMsg(int id, MessageReplyDeserializer* d, base::WaitableEvent* e) |
| 98 | : id(id), deserializer(d), done_event(e), send_result(false) {} |
rockot | 3c23629 | 2016-07-07 20:26:02 | [diff] [blame] | 99 | |
[email protected] | 1e9499c | 2010-04-06 20:33:36 | [diff] [blame] | 100 | int id; |
| 101 | MessageReplyDeserializer* deserializer; |
rockot | b62e2e3 | 2017-03-24 18:36:44 | [diff] [blame] | 102 | base::WaitableEvent* done_event; |
[email protected] | 1e9499c | 2010-04-06 20:33:36 | [diff] [blame] | 103 | bool send_result; |
| 104 | }; |
| 105 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 106 | } // namespace IPC |
| 107 | |
[email protected] | 20cb5f48 | 2009-12-16 01:01:25 | [diff] [blame] | 108 | #endif // IPC_IPC_SYNC_MESSAGE_H_ |