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