ipc_sync_message.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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
  3. // found in the LICENSE file.
  4. #ifndef IPC_IPC_SYNC_MESSAGE_H_
  5. #define IPC_IPC_SYNC_MESSAGE_H_
  6. #include <stdint.h>
  7. #include "build/build_config.h"
  8. #if BUILDFLAG(IS_WIN)
  9. #include "base/win/windows_types.h"
  10. #endif
  11. #include <memory>
  12. #include <string>
  13. #include "build/build_config.h"
  14. #include "ipc/ipc_message.h"
  15. #include "ipc/ipc_message_support_export.h"
  16. namespace base {
  17. class WaitableEvent;
  18. }
  19. namespace IPC {
  20. class MessageReplyDeserializer;
  21. class IPC_MESSAGE_SUPPORT_EXPORT SyncMessage : public Message {
  22. public:
  23. SyncMessage(int32_t routing_id,
  24. uint32_t type,
  25. PriorityValue priority,
  26. MessageReplyDeserializer* deserializer);
  27. ~SyncMessage() override;
  28. // Call this to get a deserializer for the output parameters.
  29. // Note that this can only be called once, and the caller is responsible
  30. // for deleting the deserializer when they're done.
  31. MessageReplyDeserializer* GetReplyDeserializer();
  32. // Returns true if the message is a reply to the given request id.
  33. static bool IsMessageReplyTo(const Message& msg, int request_id);
  34. // Given a reply message, returns an iterator to the beginning of the data
  35. // (i.e. skips over the synchronous specific data).
  36. static base::PickleIterator GetDataIterator(const Message* msg);
  37. // Given a synchronous message (or its reply), returns its id.
  38. static int GetMessageId(const Message& msg);
  39. // Generates a reply message to the given message.
  40. static Message* GenerateReply(const Message* msg);
  41. private:
  42. struct SyncHeader {
  43. // unique ID (unique per sender)
  44. int message_id;
  45. };
  46. static bool ReadSyncHeader(const Message& msg, SyncHeader* header);
  47. static bool WriteSyncHeader(Message* msg, const SyncHeader& header);
  48. std::unique_ptr<MessageReplyDeserializer> deserializer_;
  49. };
  50. // Used to deserialize parameters from a reply to a synchronous message
  51. class IPC_MESSAGE_SUPPORT_EXPORT MessageReplyDeserializer {
  52. public:
  53. virtual ~MessageReplyDeserializer() {}
  54. bool SerializeOutputParameters(const Message& msg);
  55. private:
  56. // Derived classes need to implement this, using the given iterator (which
  57. // is skipped past the header for synchronous messages).
  58. virtual bool SerializeOutputParameters(const Message& msg,
  59. base::PickleIterator iter) = 0;
  60. };
  61. // When sending a synchronous message, this structure contains an object
  62. // that knows how to deserialize the response.
  63. struct PendingSyncMsg {
  64. PendingSyncMsg(int id, MessageReplyDeserializer* d, base::WaitableEvent* e)
  65. : id(id), deserializer(d), done_event(e), send_result(false) {}
  66. int id;
  67. MessageReplyDeserializer* deserializer;
  68. base::WaitableEvent* done_event;
  69. bool send_result;
  70. };
  71. } // namespace IPC
  72. #endif // IPC_IPC_SYNC_MESSAGE_H_