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.
5 #ifndef IPC_IPC_MESSAGE_H_
6 #define IPC_IPC_MESSAGE_H_
10 #include "base/basictypes.h"
11 #include "base/debug/trace_event.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/pickle.h"
14 #include "base/trace_event/trace_event.h"
15 #include "ipc/ipc_export.h"
18 #define IPC_MESSAGE_LOG_ENABLED
23 //------------------------------------------------------------------------------
26 class MessageAttachment
;
27 class MessageAttachmentSet
;
29 class IPC_EXPORT Message
: public Pickle
{
37 // Bit values used in the flags field.
38 // Upper 24 bits of flags store a reference number, so this enum is limited to
41 PRIORITY_MASK
= 0x03, // Low 2 bits of store the priority value.
44 REPLY_ERROR_BIT
= 0x10,
46 PUMPING_MSGS_BIT
= 0x40,
47 HAS_SENT_TIME_BIT
= 0x80,
54 // Initialize a message with a user-defined type, priority value, and
55 // destination WebView ID.
56 Message(int32 routing_id
, uint32 type
, PriorityValue priority
);
58 // Initializes a message from a const block of data. The data is not copied;
59 // instead the data is merely referenced by this message. Only const methods
60 // should be used on the message when initialized this way.
61 Message(const char* data
, int data_len
);
63 Message(const Message
& other
);
64 Message
& operator=(const Message
& other
);
66 PriorityValue
priority() const {
67 return static_cast<PriorityValue
>(header()->flags
& PRIORITY_MASK
);
70 // True if this is a synchronous message.
72 header()->flags
|= SYNC_BIT
;
74 bool is_sync() const {
75 return (header()->flags
& SYNC_BIT
) != 0;
78 // Set this on a reply to a synchronous message.
80 header()->flags
|= REPLY_BIT
;
83 bool is_reply() const {
84 return (header()->flags
& REPLY_BIT
) != 0;
87 // Set this on a reply to a synchronous message to indicate that no receiver
89 void set_reply_error() {
90 header()->flags
|= REPLY_ERROR_BIT
;
93 bool is_reply_error() const {
94 return (header()->flags
& REPLY_ERROR_BIT
) != 0;
97 // Normally when a receiver gets a message and they're blocked on a
98 // synchronous message Send, they buffer a message. Setting this flag causes
99 // the receiver to be unblocked and the message to be dispatched immediately.
100 void set_unblock(bool unblock
) {
102 header()->flags
|= UNBLOCK_BIT
;
104 header()->flags
&= ~UNBLOCK_BIT
;
108 bool should_unblock() const {
109 return (header()->flags
& UNBLOCK_BIT
) != 0;
112 // Tells the receiver that the caller is pumping messages while waiting
114 bool is_caller_pumping_messages() const {
115 return (header()->flags
& PUMPING_MSGS_BIT
) != 0;
118 void set_dispatch_error() const {
119 dispatch_error_
= true;
122 bool dispatch_error() const {
123 return dispatch_error_
;
126 uint32
type() const {
127 return header()->type
;
130 int32
routing_id() const {
131 return header()->routing
;
134 void set_routing_id(int32 new_id
) {
135 header()->routing
= new_id
;
138 uint32
flags() const {
139 return header()->flags
;
142 // Sets all the given header values. The message should be empty at this
144 void SetHeaderValues(int32 routing
, uint32 type
, uint32 flags
);
146 template<class T
, class S
, class P
>
147 static bool Dispatch(const Message
* msg
, T
* obj
, S
* sender
, P
* parameter
,
153 template<class T
, class S
, class P
>
154 static bool Dispatch(const Message
* msg
, T
* obj
, S
* sender
, P
* parameter
,
155 void (T::*func
)(P
*)) {
156 (obj
->*func
)(parameter
);
160 // Used for async messages with no parameters.
161 static void Log(std::string
* name
, const Message
* msg
, std::string
* l
) {
164 // Find the end of the message data that starts at range_start. Returns NULL
165 // if the entire message is not found in the given data range.
166 static const char* FindNext(const char* range_start
, const char* range_end
) {
167 return Pickle::FindNext(sizeof(Header
), range_start
, range_end
);
170 // WriteAttachment appends |attachment| to the end of the set. It returns
171 // false iff the set is full.
172 bool WriteAttachment(scoped_refptr
<MessageAttachment
> attachment
);
173 // ReadAttachment parses an attachment given the parsing state |iter| and
174 // writes it to |*attachment|. It returns true on success.
175 bool ReadAttachment(PickleIterator
* iter
,
176 scoped_refptr
<MessageAttachment
>* attachment
) const;
177 // Returns true if there are any attachment in this message.
178 bool HasAttachments() const;
180 #ifdef IPC_MESSAGE_LOG_ENABLED
181 // Adds the outgoing time from Time::Now() at the end of the message and sets
182 // a bit to indicate that it's been added.
183 void set_sent_time(int64 time
);
184 int64
sent_time() const;
186 void set_received_time(int64 time
) const;
187 int64
received_time() const { return received_time_
; }
188 void set_output_params(const std::string
& op
) const { output_params_
= op
; }
189 const std::string
& output_params() const { return output_params_
; }
190 // The following four functions are needed so we can log sync messages with
191 // delayed replies. We stick the log data from the sent message into the
192 // reply message, so that when it's sent and we have the output parameters
193 // we can log it. As such, we set a flag on the sent message to not log it.
194 void set_sync_log_data(LogData
* data
) const { log_data_
= data
; }
195 LogData
* sync_log_data() const { return log_data_
; }
196 void set_dont_log() const { dont_log_
= true; }
197 bool dont_log() const { return dont_log_
; }
200 // Called to trace when message is sent.
201 void TraceMessageBegin() {
202 TRACE_EVENT_FLOW_BEGIN0(TRACE_DISABLED_BY_DEFAULT("ipc.flow"), "IPC",
205 // Called to trace when message is received.
206 void TraceMessageEnd() {
207 TRACE_EVENT_FLOW_END0(TRACE_DISABLED_BY_DEFAULT("ipc.flow"), "IPC",
212 friend class Channel
;
213 friend class ChannelMojo
;
214 friend class ChannelNacl
;
215 friend class ChannelPosix
;
216 friend class ChannelWin
;
217 friend class MessageReplyDeserializer
;
218 friend class SyncMessage
;
220 #pragma pack(push, 4)
221 struct Header
: Pickle::Header
{
222 int32 routing
; // ID of the view that this message is destined for
223 uint32 type
; // specifies the user-defined message type
224 uint32 flags
; // specifies control flags for the message
225 #if defined(OS_POSIX)
226 uint16 num_fds
; // the number of descriptors included with this message
227 uint16 pad
; // explicitly initialize this to appease valgrind
233 return headerT
<Header
>();
235 const Header
* header() const {
236 return headerT
<Header
>();
241 // Used internally to support IPC::Listener::OnBadMessageReceived.
242 mutable bool dispatch_error_
;
244 // The set of file descriptors associated with this message.
245 scoped_refptr
<MessageAttachmentSet
> attachment_set_
;
247 // Ensure that a MessageAttachmentSet is allocated
248 void EnsureMessageAttachmentSet();
250 MessageAttachmentSet
* attachment_set() {
251 EnsureMessageAttachmentSet();
252 return attachment_set_
.get();
254 const MessageAttachmentSet
* attachment_set() const {
255 return attachment_set_
.get();
258 #ifdef IPC_MESSAGE_LOG_ENABLED
260 mutable int64 received_time_
;
261 mutable std::string output_params_
;
262 mutable LogData
* log_data_
;
263 mutable bool dont_log_
;
267 //------------------------------------------------------------------------------
271 enum SpecialRoutingIDs
{
272 // indicates that we don't have a routing ID yet.
273 MSG_ROUTING_NONE
= -2,
275 // indicates a general message not sent to a particular tab.
276 MSG_ROUTING_CONTROL
= kint32max
,
279 #define IPC_REPLY_ID 0xFFFFFFF0 // Special message id for replies
280 #define IPC_LOGGING_ID 0xFFFFFFF1 // Special message id for logging
282 #endif // IPC_IPC_MESSAGE_H_