blob: 2b00f03b22b3ff358c449f7b191281280d157d82 [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
[email protected]d4651ff2008-12-02 16:51:588#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:299#include <windows.h>
[email protected]d4651ff2008-12-02 16:51:5810#endif
initial.commit09911bf2008-07-26 23:55:2911#include <string>
12#include "base/basictypes.h"
[email protected]40c4627d2011-09-09 21:10:3113#include "base/memory/scoped_ptr.h"
[email protected]946d1b22009-07-22 23:57:2114#include "ipc/ipc_message.h"
initial.commit09911bf2008-07-26 23:55:2915
[email protected]1c4947f2009-01-15 22:25:1116namespace base {
17class WaitableEvent;
18}
19
initial.commit09911bf2008-07-26 23:55:2920namespace IPC {
21
22class MessageReplyDeserializer;
23
[email protected]7c854372011-08-15 20:41:4624class IPC_EXPORT SyncMessage : public Message {
initial.commit09911bf2008-07-26 23:55:2925 public:
[email protected]753bb252013-11-04 22:28:1226 SyncMessage(int32 routing_id, uint32 type, PriorityValue priority,
initial.commit09911bf2008-07-26 23:55:2927 MessageReplyDeserializer* deserializer);
dchengfe61fca2014-10-22 02:29:5228 ~SyncMessage() override;
initial.commit09911bf2008-07-26 23:55:2929
30 // Call this to get a deserializer for the output parameters.
31 // Note that this can only be called once, and the caller is responsible
32 // for deleting the deserializer when they're done.
33 MessageReplyDeserializer* GetReplyDeserializer();
34
35 // If this message can cause the receiver to block while waiting for user
36 // input (i.e. by calling MessageBox), then the caller needs to pump window
37 // messages and dispatch asynchronous messages while waiting for the reply.
[email protected]1c4947f2009-01-15 22:25:1138 // If this event is passed in, then window messages will start being pumped
[email protected]3cdb7af812008-10-24 19:21:1339 // when it's set. Note that this behavior will continue even if the event is
[email protected]1c4947f2009-01-15 22:25:1140 // later reset. The event must be valid until after the Send call returns.
41 void set_pump_messages_event(base::WaitableEvent* event) {
initial.commit09911bf2008-07-26 23:55:2942 pump_messages_event_ = event;
43 if (event) {
44 header()->flags |= PUMPING_MSGS_BIT;
45 } else {
46 header()->flags &= ~PUMPING_MSGS_BIT;
47 }
48 }
49
[email protected]d65cab7a2008-08-12 01:25:4150 // Call this if you always want to pump messages. You can call this method
51 // or set_pump_messages_event but not both.
52 void EnableMessagePumping();
53
[email protected]1c4947f2009-01-15 22:25:1154 base::WaitableEvent* pump_messages_event() const {
55 return pump_messages_event_;
56 }
initial.commit09911bf2008-07-26 23:55:2957
58 // Returns true if the message is a reply to the given request id.
59 static bool IsMessageReplyTo(const Message& msg, int request_id);
60
61 // Given a reply message, returns an iterator to the beginning of the data
62 // (i.e. skips over the synchronous specific data).
[email protected]ce208f872012-03-07 20:42:5663 static PickleIterator GetDataIterator(const Message* msg);
initial.commit09911bf2008-07-26 23:55:2964
65 // Given a synchronous message (or its reply), returns its id.
66 static int GetMessageId(const Message& msg);
67
68 // Generates a reply message to the given message.
69 static Message* GenerateReply(const Message* msg);
70
71 private:
72 struct SyncHeader {
73 // unique ID (unique per sender)
74 int message_id;
75 };
76
77 static bool ReadSyncHeader(const Message& msg, SyncHeader* header);
78 static bool WriteSyncHeader(Message* msg, const SyncHeader& header);
79
[email protected]40c4627d2011-09-09 21:10:3180 scoped_ptr<MessageReplyDeserializer> deserializer_;
[email protected]1c4947f2009-01-15 22:25:1181 base::WaitableEvent* pump_messages_event_;
initial.commit09911bf2008-07-26 23:55:2982};
83
84// Used to deserialize parameters from a reply to a synchronous message
[email protected]7c854372011-08-15 20:41:4685class IPC_EXPORT MessageReplyDeserializer {
initial.commit09911bf2008-07-26 23:55:2986 public:
[email protected]20cb5f482009-12-16 01:01:2587 virtual ~MessageReplyDeserializer() {}
initial.commit09911bf2008-07-26 23:55:2988 bool SerializeOutputParameters(const Message& msg);
89 private:
90 // Derived classes need to implement this, using the given iterator (which
91 // is skipped past the header for synchronous messages).
[email protected]ce208f872012-03-07 20:42:5692 virtual bool SerializeOutputParameters(const Message& msg,
93 PickleIterator iter) = 0;
initial.commit09911bf2008-07-26 23:55:2994};
95
[email protected]1e9499c2010-04-06 20:33:3696// When sending a synchronous message, this structure contains an object
97// that knows how to deserialize the response.
98struct PendingSyncMsg {
99 PendingSyncMsg(int id,
100 MessageReplyDeserializer* d,
101 base::WaitableEvent* e)
102 : id(id), deserializer(d), done_event(e), send_result(false) { }
103 int id;
104 MessageReplyDeserializer* deserializer;
105 base::WaitableEvent* done_event;
106 bool send_result;
107};
108
initial.commit09911bf2008-07-26 23:55:29109} // namespace IPC
110
[email protected]20cb5f482009-12-16 01:01:25111#endif // IPC_IPC_SYNC_MESSAGE_H_