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_
12 #include "base/basictypes.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/pickle.h"
15 #include "base/trace_event/trace_event.h"
16 #include "ipc/ipc_export.h"
19 #define IPC_MESSAGE_LOG_ENABLED
26 } // namespace internal
28 //------------------------------------------------------------------------------
31 class MessageAttachment
;
32 class MessageAttachmentSet
;
34 class IPC_EXPORT Message
: public base::Pickle
{
42 // Bit values used in the flags field.
43 // Upper 24 bits of flags store a reference number, so this enum is limited to
46 PRIORITY_MASK
= 0x03, // Low 2 bits of store the priority value.
49 REPLY_ERROR_BIT
= 0x10,
51 PUMPING_MSGS_BIT
= 0x40,
52 HAS_SENT_TIME_BIT
= 0x80,
59 // Initialize a message with a user-defined type, priority value, and
60 // destination WebView ID.
61 Message(int32_t routing_id
, uint32_t type
, PriorityValue priority
);
63 // Initializes a message from a const block of data. The data is not copied;
64 // instead the data is merely referenced by this message. Only const methods
65 // should be used on the message when initialized this way.
66 Message(const char* data
, int data_len
);
68 Message(const Message
& other
);
69 Message
& operator=(const Message
& other
);
71 PriorityValue
priority() const {
72 return static_cast<PriorityValue
>(header()->flags
& PRIORITY_MASK
);
75 // True if this is a synchronous message.
77 header()->flags
|= SYNC_BIT
;
79 bool is_sync() const {
80 return (header()->flags
& SYNC_BIT
) != 0;
83 // Set this on a reply to a synchronous message.
85 header()->flags
|= REPLY_BIT
;
88 bool is_reply() const {
89 return (header()->flags
& REPLY_BIT
) != 0;
92 // Set this on a reply to a synchronous message to indicate that no receiver
94 void set_reply_error() {
95 header()->flags
|= REPLY_ERROR_BIT
;
98 bool is_reply_error() const {
99 return (header()->flags
& REPLY_ERROR_BIT
) != 0;
102 // Normally when a receiver gets a message and they're blocked on a
103 // synchronous message Send, they buffer a message. Setting this flag causes
104 // the receiver to be unblocked and the message to be dispatched immediately.
105 void set_unblock(bool unblock
) {
107 header()->flags
|= UNBLOCK_BIT
;
109 header()->flags
&= ~UNBLOCK_BIT
;
113 bool should_unblock() const {
114 return (header()->flags
& UNBLOCK_BIT
) != 0;
117 // Tells the receiver that the caller is pumping messages while waiting
119 bool is_caller_pumping_messages() const {
120 return (header()->flags
& PUMPING_MSGS_BIT
) != 0;
123 void set_dispatch_error() const {
124 dispatch_error_
= true;
127 bool dispatch_error() const {
128 return dispatch_error_
;
131 uint32_t type() const {
132 return header()->type
;
135 int32_t routing_id() const {
136 return header()->routing
;
139 void set_routing_id(int32_t new_id
) {
140 header()->routing
= new_id
;
143 uint32_t flags() const {
144 return header()->flags
;
147 // Sets all the given header values. The message should be empty at this
149 void SetHeaderValues(int32_t routing
, uint32_t type
, uint32_t flags
);
151 template<class T
, class S
, class P
>
152 static bool Dispatch(const Message
* msg
, T
* obj
, S
* sender
, P
* parameter
,
158 template<class T
, class S
, class P
>
159 static bool Dispatch(const Message
* msg
, T
* obj
, S
* sender
, P
* parameter
,
160 void (T::*func
)(P
*)) {
161 (obj
->*func
)(parameter
);
165 // Used for async messages with no parameters.
166 static void Log(std::string
* name
, const Message
* msg
, std::string
* l
) {
169 // Find the end of the message data that starts at range_start. Returns NULL
170 // if the entire message is not found in the given data range.
171 static const char* FindNext(const char* range_start
, const char* range_end
) {
172 return base::Pickle::FindNext(sizeof(Header
), range_start
, range_end
);
175 // WriteAttachment appends |attachment| to the end of the set. It returns
176 // false iff the set is full.
177 bool WriteAttachment(scoped_refptr
<MessageAttachment
> attachment
);
178 // ReadAttachment parses an attachment given the parsing state |iter| and
179 // writes it to |*attachment|. It returns true on success.
180 bool ReadAttachment(base::PickleIterator
* iter
,
181 scoped_refptr
<MessageAttachment
>* attachment
) const;
182 // Returns true if there are any attachment in this message.
183 bool HasAttachments() const;
184 // Returns true if there are any MojoHandleAttachments in this message.
185 bool HasMojoHandles() const;
186 // Whether the message has any brokerable attachments.
187 bool HasBrokerableAttachments() const;
189 void set_sender_pid(base::ProcessId id
) { sender_pid_
= id
; }
190 base::ProcessId
get_sender_pid() const { return sender_pid_
; }
192 #ifdef IPC_MESSAGE_LOG_ENABLED
193 // Adds the outgoing time from Time::Now() at the end of the message and sets
194 // a bit to indicate that it's been added.
195 void set_sent_time(int64_t time
);
196 int64_t sent_time() const;
198 void set_received_time(int64_t time
) const;
199 int64_t received_time() const { return received_time_
; }
200 void set_output_params(const std::string
& op
) const { output_params_
= op
; }
201 const std::string
& output_params() const { return output_params_
; }
202 // The following four functions are needed so we can log sync messages with
203 // delayed replies. We stick the log data from the sent message into the
204 // reply message, so that when it's sent and we have the output parameters
205 // we can log it. As such, we set a flag on the sent message to not log it.
206 void set_sync_log_data(LogData
* data
) const { log_data_
= data
; }
207 LogData
* sync_log_data() const { return log_data_
; }
208 void set_dont_log() const { dont_log_
= true; }
209 bool dont_log() const { return dont_log_
; }
213 friend class Channel
;
214 friend class ChannelMojo
;
215 friend class ChannelNacl
;
216 friend class ChannelPosix
;
217 friend class ChannelWin
;
218 friend class internal::ChannelReader
;
219 friend class MessageReplyDeserializer
;
220 friend class SyncMessage
;
222 #pragma pack(push, 4)
223 struct Header
: base::Pickle::Header
{
224 int32_t routing
; // ID of the view that this message is destined for
225 uint32_t type
; // specifies the user-defined message type
226 uint32_t flags
; // specifies control flags for the message
227 #if defined(OS_POSIX)
228 uint16_t num_fds
; // the number of descriptors included with this message
229 uint16_t pad
; // explicitly initialize this to appease valgrind
235 return headerT
<Header
>();
237 const Header
* header() const {
238 return headerT
<Header
>();
243 // Used internally to support IPC::Listener::OnBadMessageReceived.
244 mutable bool dispatch_error_
;
246 // The set of file descriptors associated with this message.
247 scoped_refptr
<MessageAttachmentSet
> attachment_set_
;
249 // Ensure that a MessageAttachmentSet is allocated
250 void EnsureMessageAttachmentSet();
252 MessageAttachmentSet
* attachment_set() {
253 EnsureMessageAttachmentSet();
254 return attachment_set_
.get();
256 const MessageAttachmentSet
* attachment_set() const {
257 return attachment_set_
.get();
260 // The process id of the sender of the message. This member is populated with
261 // a valid value for every message dispatched to listeners.
262 base::ProcessId sender_pid_
;
264 #ifdef IPC_MESSAGE_LOG_ENABLED
266 mutable int64_t received_time_
;
267 mutable std::string output_params_
;
268 mutable LogData
* log_data_
;
269 mutable bool dont_log_
;
273 //------------------------------------------------------------------------------
277 enum SpecialRoutingIDs
{
278 // indicates that we don't have a routing ID yet.
279 MSG_ROUTING_NONE
= -2,
281 // indicates a general message not sent to a particular tab.
282 MSG_ROUTING_CONTROL
= kint32max
,
285 #define IPC_REPLY_ID 0xFFFFFFF0 // Special message id for replies
286 #define IPC_LOGGING_ID 0xFFFFFFF1 // Special message id for logging
288 #endif // IPC_IPC_MESSAGE_H_