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