blob: 904a9c8505c6caf66be81e8668671b631eb4e19a [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>
[email protected]d4651ff2008-12-02 16:51:589#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:2910#include <windows.h>
[email protected]d4651ff2008-12-02 16:51:5811#endif
tfarina4da8275d2015-09-16 09:56:2112
initial.commit09911bf2008-07-26 23:55:2913#include <string>
tfarina4da8275d2015-09-16 09:56:2114
[email protected]40c4627d2011-09-09 21:10:3115#include "base/memory/scoped_ptr.h"
[email protected]946d1b22009-07-22 23:57:2116#include "ipc/ipc_message.h"
initial.commit09911bf2008-07-26 23:55:2917
[email protected]1c4947f2009-01-15 22:25:1118namespace base {
19class WaitableEvent;
20}
21
initial.commit09911bf2008-07-26 23:55:2922namespace IPC {
23
24class MessageReplyDeserializer;
25
[email protected]7c854372011-08-15 20:41:4626class IPC_EXPORT SyncMessage : public Message {
initial.commit09911bf2008-07-26 23:55:2927 public:
tfarina10a5c062015-09-04 18:47:5728 SyncMessage(int32_t routing_id,
29 uint32_t type,
30 PriorityValue priority,
initial.commit09911bf2008-07-26 23:55:2931 MessageReplyDeserializer* deserializer);
dchengfe61fca2014-10-22 02:29:5232 ~SyncMessage() override;
initial.commit09911bf2008-07-26 23:55:2933
34 // Call this to get a deserializer for the output parameters.
35 // Note that this can only be called once, and the caller is responsible
36 // for deleting the deserializer when they're done.
37 MessageReplyDeserializer* GetReplyDeserializer();
38
39 // If this message can cause the receiver to block while waiting for user
40 // input (i.e. by calling MessageBox), then the caller needs to pump window
41 // messages and dispatch asynchronous messages while waiting for the reply.
[email protected]1c4947f2009-01-15 22:25:1142 // If this event is passed in, then window messages will start being pumped
[email protected]3cdb7af812008-10-24 19:21:1343 // when it's set. Note that this behavior will continue even if the event is
[email protected]1c4947f2009-01-15 22:25:1144 // later reset. The event must be valid until after the Send call returns.
45 void set_pump_messages_event(base::WaitableEvent* event) {
initial.commit09911bf2008-07-26 23:55:2946 pump_messages_event_ = event;
47 if (event) {
48 header()->flags |= PUMPING_MSGS_BIT;
49 } else {
50 header()->flags &= ~PUMPING_MSGS_BIT;
51 }
52 }
53
[email protected]d65cab7a2008-08-12 01:25:4154 // Call this if you always want to pump messages. You can call this method
55 // or set_pump_messages_event but not both.
56 void EnableMessagePumping();
57
[email protected]1c4947f2009-01-15 22:25:1158 base::WaitableEvent* pump_messages_event() const {
59 return pump_messages_event_;
60 }
initial.commit09911bf2008-07-26 23:55:2961
62 // Returns true if the message is a reply to the given request id.
63 static bool IsMessageReplyTo(const Message& msg, int request_id);
64
65 // Given a reply message, returns an iterator to the beginning of the data
66 // (i.e. skips over the synchronous specific data).
brettw05cfd8ddb2015-06-02 07:02:4767 static base::PickleIterator GetDataIterator(const Message* msg);
initial.commit09911bf2008-07-26 23:55:2968
69 // Given a synchronous message (or its reply), returns its id.
70 static int GetMessageId(const Message& msg);
71
72 // Generates a reply message to the given message.
73 static Message* GenerateReply(const Message* msg);
74
75 private:
76 struct SyncHeader {
77 // unique ID (unique per sender)
78 int message_id;
79 };
80
81 static bool ReadSyncHeader(const Message& msg, SyncHeader* header);
82 static bool WriteSyncHeader(Message* msg, const SyncHeader& header);
83
[email protected]40c4627d2011-09-09 21:10:3184 scoped_ptr<MessageReplyDeserializer> deserializer_;
[email protected]1c4947f2009-01-15 22:25:1185 base::WaitableEvent* pump_messages_event_;
initial.commit09911bf2008-07-26 23:55:2986};
87
88// Used to deserialize parameters from a reply to a synchronous message
[email protected]7c854372011-08-15 20:41:4689class IPC_EXPORT MessageReplyDeserializer {
initial.commit09911bf2008-07-26 23:55:2990 public:
[email protected]20cb5f482009-12-16 01:01:2591 virtual ~MessageReplyDeserializer() {}
initial.commit09911bf2008-07-26 23:55:2992 bool SerializeOutputParameters(const Message& msg);
93 private:
94 // Derived classes need to implement this, using the given iterator (which
95 // is skipped past the header for synchronous messages).
[email protected]ce208f872012-03-07 20:42:5696 virtual bool SerializeOutputParameters(const Message& msg,
brettw05cfd8ddb2015-06-02 07:02:4797 base::PickleIterator iter) = 0;
initial.commit09911bf2008-07-26 23:55:2998};
99
[email protected]1e9499c2010-04-06 20:33:36100// When sending a synchronous message, this structure contains an object
101// that knows how to deserialize the response.
102struct PendingSyncMsg {
103 PendingSyncMsg(int id,
104 MessageReplyDeserializer* d,
105 base::WaitableEvent* e)
106 : id(id), deserializer(d), done_event(e), send_result(false) { }
107 int id;
108 MessageReplyDeserializer* deserializer;
109 base::WaitableEvent* done_event;
110 bool send_result;
111};
112
initial.commit09911bf2008-07-26 23:55:29113} // namespace IPC
114
[email protected]20cb5f482009-12-16 01:01:25115#endif // IPC_IPC_SYNC_MESSAGE_H_