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/memory/ref_counted.h"
13 #include "base/pickle.h"
14 #include "base/trace_event/trace_event.h"
15 #include "ipc/brokerable_attachment.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 // The static method FindNext() returns several pieces of information, which
170 // are aggregated into an instance of this struct.
171 struct NextMessageInfo
{
175 // Whether an entire message was found in the given memory range.
177 // Only filled in if |message_found| is true.
178 // The start address is passed into FindNext() by the caller, so isn't
179 // repeated in this struct. The end address of the pickle should be used to
180 // construct a base::Pickle.
181 const char* pickle_end
;
182 // Only filled in if |message_found| is true.
183 // The end address of the message should be used to determine the start
184 // address of the next message.
185 const char* message_end
;
186 // If the message has brokerable attachments, this vector will contain the
187 // ids of the brokerable attachments. The caller of FindNext() is
188 // responsible for adding the attachments to the message.
189 std::vector
<BrokerableAttachment::AttachmentId
> attachment_ids
;
192 // |info| is an output parameter and must not be nullptr.
193 static void FindNext(const char* range_start
,
194 const char* range_end
,
195 NextMessageInfo
* info
);
197 // Adds a placeholder brokerable attachment that must be replaced before the
198 // message can be dispatched.
199 bool AddPlaceholderBrokerableAttachmentWithId(
200 BrokerableAttachment::AttachmentId id
);
202 // WriteAttachment appends |attachment| to the end of the set. It returns
203 // false iff the set is full.
204 bool WriteAttachment(scoped_refptr
<MessageAttachment
> attachment
);
205 // ReadAttachment parses an attachment given the parsing state |iter| and
206 // writes it to |*attachment|. It returns true on success.
207 bool ReadAttachment(base::PickleIterator
* iter
,
208 scoped_refptr
<MessageAttachment
>* attachment
) const;
209 // Returns true if there are any attachment in this message.
210 bool HasAttachments() const;
211 // Returns true if there are any MojoHandleAttachments in this message.
212 bool HasMojoHandles() const;
213 // Whether the message has any brokerable attachments.
214 bool HasBrokerableAttachments() const;
216 void set_sender_pid(base::ProcessId id
) { sender_pid_
= id
; }
217 base::ProcessId
get_sender_pid() const { return sender_pid_
; }
219 #ifdef IPC_MESSAGE_LOG_ENABLED
220 // Adds the outgoing time from Time::Now() at the end of the message and sets
221 // a bit to indicate that it's been added.
222 void set_sent_time(int64_t time
);
223 int64_t sent_time() const;
225 void set_received_time(int64_t time
) const;
226 int64_t received_time() const { return received_time_
; }
227 void set_output_params(const std::string
& op
) const { output_params_
= op
; }
228 const std::string
& output_params() const { return output_params_
; }
229 // The following four functions are needed so we can log sync messages with
230 // delayed replies. We stick the log data from the sent message into the
231 // reply message, so that when it's sent and we have the output parameters
232 // we can log it. As such, we set a flag on the sent message to not log it.
233 void set_sync_log_data(LogData
* data
) const { log_data_
= data
; }
234 LogData
* sync_log_data() const { return log_data_
; }
235 void set_dont_log() const { dont_log_
= true; }
236 bool dont_log() const { return dont_log_
; }
240 friend class Channel
;
241 friend class ChannelMojo
;
242 friend class ChannelNacl
;
243 friend class ChannelPosix
;
244 friend class ChannelWin
;
245 friend class internal::ChannelReader
;
246 friend class MessageReplyDeserializer
;
247 friend class SyncMessage
;
249 #pragma pack(push, 4)
250 struct Header
: base::Pickle::Header
{
251 int32_t routing
; // ID of the view that this message is destined for
252 uint32_t type
; // specifies the user-defined message type
253 uint32_t flags
; // specifies control flags for the message
254 #if defined(OS_POSIX)
255 uint16_t num_fds
; // the number of descriptors included with this message
256 uint16_t pad
; // explicitly initialize this to appease valgrind
262 return headerT
<Header
>();
264 const Header
* header() const {
265 return headerT
<Header
>();
270 // Used internally to support IPC::Listener::OnBadMessageReceived.
271 mutable bool dispatch_error_
;
273 // The set of file descriptors associated with this message.
274 scoped_refptr
<MessageAttachmentSet
> attachment_set_
;
276 // Ensure that a MessageAttachmentSet is allocated
277 void EnsureMessageAttachmentSet();
279 MessageAttachmentSet
* attachment_set() {
280 EnsureMessageAttachmentSet();
281 return attachment_set_
.get();
283 const MessageAttachmentSet
* attachment_set() const {
284 return attachment_set_
.get();
287 // The process id of the sender of the message. This member is populated with
288 // a valid value for every message dispatched to listeners.
289 base::ProcessId sender_pid_
;
291 #ifdef IPC_MESSAGE_LOG_ENABLED
293 mutable int64_t received_time_
;
294 mutable std::string output_params_
;
295 mutable LogData
* log_data_
;
296 mutable bool dont_log_
;
300 //------------------------------------------------------------------------------
304 enum SpecialRoutingIDs
{
305 // indicates that we don't have a routing ID yet.
306 MSG_ROUTING_NONE
= -2,
308 // indicates a general message not sent to a particular tab.
309 MSG_ROUTING_CONTROL
= INT32_MAX
,
312 #define IPC_REPLY_ID 0xFFFFFFF0 // Special message id for replies
313 #define IPC_LOGGING_ID 0xFFFFFFF1 // Special message id for logging
315 #endif // IPC_IPC_MESSAGE_H_