blob: cfac2ded1243b9d1f268f8370d5127f6669f891a [file] [log] [blame]
[email protected]ce208f872012-03-07 20:42:561// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]20cb5f482009-12-16 01:01:255#ifndef IPC_IPC_SYNC_MESSAGE_H_
6#define IPC_IPC_SYNC_MESSAGE_H_
initial.commit09911bf2008-07-26 23:55:297
tfarina10a5c062015-09-04 18:47:578#include <stdint.h>
danakj03de39b22016-04-23 04:21:099
[email protected]d4651ff2008-12-02 16:51:5810#if defined(OS_WIN)
Bruce Dawsonbfdc3fd2018-01-03 20:32:3611#include "base/win/windows_types.h"
[email protected]d4651ff2008-12-02 16:51:5812#endif
tfarina4da82752015-09-16 09:56:2113
danakj03de39b22016-04-23 04:21:0914#include <memory>
initial.commit09911bf2008-07-26 23:55:2915#include <string>
tfarina4da82752015-09-16 09:56:2116
avi246998d82015-12-22 02:39:0417#include "build/build_config.h"
[email protected]946d1b22009-07-22 23:57:2118#include "ipc/ipc_message.h"
Ken Rockotfd907632017-09-14 04:23:4119#include "ipc/ipc_message_support_export.h"
initial.commit09911bf2008-07-26 23:55:2920
rockotb62e2e32017-03-24 18:36:4421namespace base {
22class WaitableEvent;
23}
24
initial.commit09911bf2008-07-26 23:55:2925namespace IPC {
26
27class MessageReplyDeserializer;
28
Ken Rockotfd907632017-09-14 04:23:4129class IPC_MESSAGE_SUPPORT_EXPORT SyncMessage : public Message {
initial.commit09911bf2008-07-26 23:55:2930 public:
tfarina10a5c062015-09-04 18:47:5731 SyncMessage(int32_t routing_id,
32 uint32_t type,
33 PriorityValue priority,
initial.commit09911bf2008-07-26 23:55:2934 MessageReplyDeserializer* deserializer);
dchengfe61fca2014-10-22 02:29:5235 ~SyncMessage() override;
initial.commit09911bf2008-07-26 23:55:2936
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.
rockot3c236292016-07-07 20:26:0245 // 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.commit09911bf2008-07-26 23:55:2949 }
50
rockot3c236292016-07-07 20:26:0251 // 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]1c4947f2009-01-15 22:25:1155 }
initial.commit09911bf2008-07-26 23:55:2956
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).
brettw05cfd8ddb2015-06-02 07:02:4762 static base::PickleIterator GetDataIterator(const Message* msg);
initial.commit09911bf2008-07-26 23:55:2963
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
danakj03de39b22016-04-23 04:21:0979 std::unique_ptr<MessageReplyDeserializer> deserializer_;
initial.commit09911bf2008-07-26 23:55:2980};
81
82// Used to deserialize parameters from a reply to a synchronous message
Ken Rockotfd907632017-09-14 04:23:4183class IPC_MESSAGE_SUPPORT_EXPORT MessageReplyDeserializer {
initial.commit09911bf2008-07-26 23:55:2984 public:
[email protected]20cb5f482009-12-16 01:01:2585 virtual ~MessageReplyDeserializer() {}
initial.commit09911bf2008-07-26 23:55:2986 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]ce208f872012-03-07 20:42:5690 virtual bool SerializeOutputParameters(const Message& msg,
brettw05cfd8ddb2015-06-02 07:02:4791 base::PickleIterator iter) = 0;
initial.commit09911bf2008-07-26 23:55:2992};
93
[email protected]1e9499c2010-04-06 20:33:3694// When sending a synchronous message, this structure contains an object
95// that knows how to deserialize the response.
96struct PendingSyncMsg {
rockotb62e2e32017-03-24 18:36:4497 PendingSyncMsg(int id, MessageReplyDeserializer* d, base::WaitableEvent* e)
98 : id(id), deserializer(d), done_event(e), send_result(false) {}
rockot3c236292016-07-07 20:26:0299
[email protected]1e9499c2010-04-06 20:33:36100 int id;
101 MessageReplyDeserializer* deserializer;
rockotb62e2e32017-03-24 18:36:44102 base::WaitableEvent* done_event;
[email protected]1e9499c2010-04-06 20:33:36103 bool send_result;
104};
105
initial.commit09911bf2008-07-26 23:55:29106} // namespace IPC
107
[email protected]20cb5f482009-12-16 01:01:25108#endif // IPC_IPC_SYNC_MESSAGE_H_