blob: 6dd3b632451028de379642cd0c8f35775c053d1b [file] [log] [blame]
jbates@chromium.org0fc87362012-03-08 05:42:56 +09001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
agl@chromium.org1c6dcf22009-07-23 08:57:21 +09002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
jamesr@chromium.orgab4c65e2009-12-16 10:01:25 +09005#ifndef IPC_IPC_SYNC_MESSAGE_H_
6#define IPC_IPC_SYNC_MESSAGE_H_
agl@chromium.org1c6dcf22009-07-23 08:57:21 +09007
tfarina1cbfa082015-09-05 03:47:57 +09008#include <stdint.h>
agl@chromium.org1c6dcf22009-07-23 08:57:21 +09009#if defined(OS_WIN)
10#include <windows.h>
11#endif
tfarina2d565d32015-09-16 18:56:21 +090012
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090013#include <string>
tfarina2d565d32015-09-16 18:56:21 +090014
dmichael@chromium.org18bbd0d2011-09-10 06:10:31 +090015#include "base/memory/scoped_ptr.h"
avi42ebda42015-12-22 11:39:04 +090016#include "build/build_config.h"
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090017#include "ipc/ipc_message.h"
18
19namespace base {
20class WaitableEvent;
21}
22
23namespace IPC {
24
25class MessageReplyDeserializer;
26
darin@chromium.org80e4c5e2011-08-16 05:41:46 +090027class IPC_EXPORT SyncMessage : public Message {
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090028 public:
tfarina1cbfa082015-09-05 03:47:57 +090029 SyncMessage(int32_t routing_id,
30 uint32_t type,
31 PriorityValue priority,
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090032 MessageReplyDeserializer* deserializer);
dchengef7721a2014-10-22 11:29:52 +090033 ~SyncMessage() override;
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090034
35 // Call this to get a deserializer for the output parameters.
36 // Note that this can only be called once, and the caller is responsible
37 // for deleting the deserializer when they're done.
38 MessageReplyDeserializer* GetReplyDeserializer();
39
40 // If this message can cause the receiver to block while waiting for user
41 // input (i.e. by calling MessageBox), then the caller needs to pump window
42 // messages and dispatch asynchronous messages while waiting for the reply.
43 // If this event is passed in, then window messages will start being pumped
44 // when it's set. Note that this behavior will continue even if the event is
45 // later reset. The event must be valid until after the Send call returns.
46 void set_pump_messages_event(base::WaitableEvent* event) {
47 pump_messages_event_ = event;
48 if (event) {
49 header()->flags |= PUMPING_MSGS_BIT;
50 } else {
51 header()->flags &= ~PUMPING_MSGS_BIT;
52 }
53 }
54
55 // Call this if you always want to pump messages. You can call this method
56 // or set_pump_messages_event but not both.
57 void EnableMessagePumping();
58
59 base::WaitableEvent* pump_messages_event() const {
60 return pump_messages_event_;
61 }
62
63 // Returns true if the message is a reply to the given request id.
64 static bool IsMessageReplyTo(const Message& msg, int request_id);
65
66 // Given a reply message, returns an iterator to the beginning of the data
67 // (i.e. skips over the synchronous specific data).
brettwa4879472015-06-02 16:02:47 +090068 static base::PickleIterator GetDataIterator(const Message* msg);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090069
70 // Given a synchronous message (or its reply), returns its id.
71 static int GetMessageId(const Message& msg);
72
73 // Generates a reply message to the given message.
74 static Message* GenerateReply(const Message* msg);
75
76 private:
77 struct SyncHeader {
78 // unique ID (unique per sender)
79 int message_id;
80 };
81
82 static bool ReadSyncHeader(const Message& msg, SyncHeader* header);
83 static bool WriteSyncHeader(Message* msg, const SyncHeader& header);
84
dmichael@chromium.org18bbd0d2011-09-10 06:10:31 +090085 scoped_ptr<MessageReplyDeserializer> deserializer_;
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090086 base::WaitableEvent* pump_messages_event_;
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090087};
88
89// Used to deserialize parameters from a reply to a synchronous message
darin@chromium.org80e4c5e2011-08-16 05:41:46 +090090class IPC_EXPORT MessageReplyDeserializer {
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090091 public:
jamesr@chromium.orgab4c65e2009-12-16 10:01:25 +090092 virtual ~MessageReplyDeserializer() {}
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090093 bool SerializeOutputParameters(const Message& msg);
94 private:
95 // Derived classes need to implement this, using the given iterator (which
96 // is skipped past the header for synchronous messages).
jbates@chromium.org0fc87362012-03-08 05:42:56 +090097 virtual bool SerializeOutputParameters(const Message& msg,
brettwa4879472015-06-02 16:02:47 +090098 base::PickleIterator iter) = 0;
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090099};
100
jabdelmalek@google.comeb921652010-04-07 05:33:36 +0900101// When sending a synchronous message, this structure contains an object
102// that knows how to deserialize the response.
103struct PendingSyncMsg {
104 PendingSyncMsg(int id,
105 MessageReplyDeserializer* d,
106 base::WaitableEvent* e)
107 : id(id), deserializer(d), done_event(e), send_result(false) { }
108 int id;
109 MessageReplyDeserializer* deserializer;
110 base::WaitableEvent* done_event;
111 bool send_result;
112};
113
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900114} // namespace IPC
115
jamesr@chromium.orgab4c65e2009-12-16 10:01:25 +0900116#endif // IPC_IPC_SYNC_MESSAGE_H_