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/memory/ref_counted.h"
12 #include "base/pickle.h"
13 #include "base/trace_event/trace_event.h"
14 #include "ipc/ipc_export.h"
17 #define IPC_MESSAGE_LOG_ENABLED
22 //------------------------------------------------------------------------------
25 class MessageAttachment
;
26 class MessageAttachmentSet
;
28 class IPC_EXPORT Message
: public Pickle
{
36 // Bit values used in the flags field.
37 // Upper 24 bits of flags store a reference number, so this enum is limited to
40 PRIORITY_MASK
= 0x03, // Low 2 bits of store the priority value.
43 REPLY_ERROR_BIT
= 0x10,
45 PUMPING_MSGS_BIT
= 0x40,
46 HAS_SENT_TIME_BIT
= 0x80,
53 // Initialize a message with a user-defined type, priority value, and
54 // destination WebView ID.
55 Message(int32 routing_id
, uint32 type
, PriorityValue priority
);
57 // Initializes a message from a const block of data. The data is not copied;
58 // instead the data is merely referenced by this message. Only const methods
59 // should be used on the message when initialized this way.
60 Message(const char* data
, int data_len
);
62 Message(const Message
& other
);
63 Message
& operator=(const Message
& other
);
65 PriorityValue
priority() const {
66 return static_cast<PriorityValue
>(header()->flags
& PRIORITY_MASK
);
69 // True if this is a synchronous message.
71 header()->flags
|= SYNC_BIT
;
73 bool is_sync() const {
74 return (header()->flags
& SYNC_BIT
) != 0;
77 // Set this on a reply to a synchronous message.
79 header()->flags
|= REPLY_BIT
;
82 bool is_reply() const {
83 return (header()->flags
& REPLY_BIT
) != 0;
86 // Set this on a reply to a synchronous message to indicate that no receiver
88 void set_reply_error() {
89 header()->flags
|= REPLY_ERROR_BIT
;
92 bool is_reply_error() const {
93 return (header()->flags
& REPLY_ERROR_BIT
) != 0;
96 // Normally when a receiver gets a message and they're blocked on a
97 // synchronous message Send, they buffer a message. Setting this flag causes
98 // the receiver to be unblocked and the message to be dispatched immediately.
99 void set_unblock(bool unblock
) {
101 header()->flags
|= UNBLOCK_BIT
;
103 header()->flags
&= ~UNBLOCK_BIT
;
107 bool should_unblock() const {
108 return (header()->flags
& UNBLOCK_BIT
) != 0;
111 // Tells the receiver that the caller is pumping messages while waiting
113 bool is_caller_pumping_messages() const {
114 return (header()->flags
& PUMPING_MSGS_BIT
) != 0;
117 void set_dispatch_error() const {
118 dispatch_error_
= true;
121 bool dispatch_error() const {
122 return dispatch_error_
;
125 uint32
type() const {
126 return header()->type
;
129 int32
routing_id() const {
130 return header()->routing
;
133 void set_routing_id(int32 new_id
) {
134 header()->routing
= new_id
;
137 uint32
flags() const {
138 return header()->flags
;
141 // Sets all the given header values. The message should be empty at this
143 void SetHeaderValues(int32 routing
, uint32 type
, uint32 flags
);
145 template<class T
, class S
, class P
>
146 static bool Dispatch(const Message
* msg
, T
* obj
, S
* sender
, P
* parameter
,
152 template<class T
, class S
, class P
>
153 static bool Dispatch(const Message
* msg
, T
* obj
, S
* sender
, P
* parameter
,
154 void (T::*func
)(P
*)) {
155 (obj
->*func
)(parameter
);
159 // Used for async messages with no parameters.
160 static void Log(std::string
* name
, const Message
* msg
, std::string
* l
) {
163 // Find the end of the message data that starts at range_start. Returns NULL
164 // if the entire message is not found in the given data range.
165 static const char* FindNext(const char* range_start
, const char* range_end
) {
166 return Pickle::FindNext(sizeof(Header
), range_start
, range_end
);
169 // WriteAttachment appends |attachment| to the end of the set. It returns
170 // false iff the set is full.
171 bool WriteAttachment(scoped_refptr
<MessageAttachment
> attachment
);
172 // ReadAttachment parses an attachment given the parsing state |iter| and
173 // writes it to |*attachment|. It returns true on success.
174 bool ReadAttachment(PickleIterator
* iter
,
175 scoped_refptr
<MessageAttachment
>* attachment
) const;
176 // Returns true if there are any attachment in this message.
177 bool HasAttachments() const;
179 #ifdef IPC_MESSAGE_LOG_ENABLED
180 // Adds the outgoing time from Time::Now() at the end of the message and sets
181 // a bit to indicate that it's been added.
182 void set_sent_time(int64 time
);
183 int64
sent_time() const;
185 void set_received_time(int64 time
) const;
186 int64
received_time() const { return received_time_
; }
187 void set_output_params(const std::string
& op
) const { output_params_
= op
; }
188 const std::string
& output_params() const { return output_params_
; }
189 // The following four functions are needed so we can log sync messages with
190 // delayed replies. We stick the log data from the sent message into the
191 // reply message, so that when it's sent and we have the output parameters
192 // we can log it. As such, we set a flag on the sent message to not log it.
193 void set_sync_log_data(LogData
* data
) const { log_data_
= data
; }
194 LogData
* sync_log_data() const { return log_data_
; }
195 void set_dont_log() const { dont_log_
= true; }
196 bool dont_log() const { return dont_log_
; }
199 // Called to trace when message is sent.
200 void TraceMessageBegin() {
201 TRACE_EVENT_FLOW_BEGIN0(TRACE_DISABLED_BY_DEFAULT("ipc.flow"), "IPC",
204 // Called to trace when message is received.
205 void TraceMessageEnd() {
206 TRACE_EVENT_FLOW_END0(TRACE_DISABLED_BY_DEFAULT("ipc.flow"), "IPC",
211 friend class Channel
;
212 friend class ChannelMojo
;
213 friend class ChannelNacl
;
214 friend class ChannelPosix
;
215 friend class ChannelWin
;
216 friend class MessageReplyDeserializer
;
217 friend class SyncMessage
;
219 #pragma pack(push, 4)
220 struct Header
: Pickle::Header
{
221 int32 routing
; // ID of the view that this message is destined for
222 uint32 type
; // specifies the user-defined message type
223 uint32 flags
; // specifies control flags for the message
224 #if defined(OS_POSIX)
225 uint16 num_fds
; // the number of descriptors included with this message
226 uint16 pad
; // explicitly initialize this to appease valgrind
232 return headerT
<Header
>();
234 const Header
* header() const {
235 return headerT
<Header
>();
240 // Used internally to support IPC::Listener::OnBadMessageReceived.
241 mutable bool dispatch_error_
;
243 // The set of file descriptors associated with this message.
244 scoped_refptr
<MessageAttachmentSet
> attachment_set_
;
246 // Ensure that a MessageAttachmentSet is allocated
247 void EnsureMessageAttachmentSet();
249 MessageAttachmentSet
* attachment_set() {
250 EnsureMessageAttachmentSet();
251 return attachment_set_
.get();
253 const MessageAttachmentSet
* attachment_set() const {
254 return attachment_set_
.get();
257 #ifdef IPC_MESSAGE_LOG_ENABLED
259 mutable int64 received_time_
;
260 mutable std::string output_params_
;
261 mutable LogData
* log_data_
;
262 mutable bool dont_log_
;
266 //------------------------------------------------------------------------------
270 enum SpecialRoutingIDs
{
271 // indicates that we don't have a routing ID yet.
272 MSG_ROUTING_NONE
= -2,
274 // indicates a general message not sent to a particular tab.
275 MSG_ROUTING_CONTROL
= kint32max
,
278 #define IPC_REPLY_ID 0xFFFFFFF0 // Special message id for replies
279 #define IPC_LOGGING_ID 0xFFFFFFF1 // Special message id for logging
281 #endif // IPC_IPC_MESSAGE_H_