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