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/attachment_broker.h"
16 #include "ipc/brokerable_attachment.h"
17 #include "ipc/ipc_export.h"
20 #define IPC_MESSAGE_LOG_ENABLED
27 } // namespace internal
29 //------------------------------------------------------------------------------
32 class MessageAttachment
;
33 class MessageAttachmentSet
;
35 class IPC_EXPORT Message
: public base::Pickle
{
43 // Bit values used in the flags field.
44 // Upper 24 bits of flags store a reference number, so this enum is limited to
47 PRIORITY_MASK
= 0x03, // Low 2 bits of store the priority value.
50 REPLY_ERROR_BIT
= 0x10,
52 PUMPING_MSGS_BIT
= 0x40,
53 HAS_SENT_TIME_BIT
= 0x80,
60 // Initialize a message with a user-defined type, priority value, and
61 // destination WebView ID.
62 Message(int32_t routing_id
, uint32_t type
, PriorityValue priority
);
64 // Initializes a message from a const block of data. The data is not copied;
65 // instead the data is merely referenced by this message. Only const methods
66 // should be used on the message when initialized this way.
67 Message(const char* data
, int data_len
);
69 Message(const Message
& other
);
70 Message
& operator=(const Message
& other
);
72 PriorityValue
priority() const {
73 return static_cast<PriorityValue
>(header()->flags
& PRIORITY_MASK
);
76 // True if this is a synchronous message.
78 header()->flags
|= SYNC_BIT
;
80 bool is_sync() const {
81 return (header()->flags
& SYNC_BIT
) != 0;
84 // Set this on a reply to a synchronous message.
86 header()->flags
|= REPLY_BIT
;
89 bool is_reply() const {
90 return (header()->flags
& REPLY_BIT
) != 0;
93 // Set this on a reply to a synchronous message to indicate that no receiver
95 void set_reply_error() {
96 header()->flags
|= REPLY_ERROR_BIT
;
99 bool is_reply_error() const {
100 return (header()->flags
& REPLY_ERROR_BIT
) != 0;
103 // Normally when a receiver gets a message and they're blocked on a
104 // synchronous message Send, they buffer a message. Setting this flag causes
105 // the receiver to be unblocked and the message to be dispatched immediately.
106 void set_unblock(bool unblock
) {
108 header()->flags
|= UNBLOCK_BIT
;
110 header()->flags
&= ~UNBLOCK_BIT
;
114 bool should_unblock() const {
115 return (header()->flags
& UNBLOCK_BIT
) != 0;
118 // Tells the receiver that the caller is pumping messages while waiting
120 bool is_caller_pumping_messages() const {
121 return (header()->flags
& PUMPING_MSGS_BIT
) != 0;
124 void set_dispatch_error() const {
125 dispatch_error_
= true;
128 bool dispatch_error() const {
129 return dispatch_error_
;
132 uint32_t type() const {
133 return header()->type
;
136 int32_t routing_id() const {
137 return header()->routing
;
140 void set_routing_id(int32_t new_id
) {
141 header()->routing
= new_id
;
144 uint32_t flags() const {
145 return header()->flags
;
148 // Sets all the given header values. The message should be empty at this
150 void SetHeaderValues(int32_t routing
, uint32_t type
, uint32_t flags
);
152 template<class T
, class S
, class P
>
153 static bool Dispatch(const Message
* msg
, T
* obj
, S
* sender
, P
* parameter
,
159 template<class T
, class S
, class P
>
160 static bool Dispatch(const Message
* msg
, T
* obj
, S
* sender
, P
* parameter
,
161 void (T::*func
)(P
*)) {
162 (obj
->*func
)(parameter
);
166 // Used for async messages with no parameters.
167 static void Log(std::string
* name
, const Message
* msg
, std::string
* l
) {
170 // The static method FindNext() returns several pieces of information, which
171 // are aggregated into an instance of this struct.
172 struct NextMessageInfo
{
176 // Whether an entire message was found in the given memory range.
178 // Only filled in if |message_found| is true.
179 // The start address is passed into FindNext() by the caller, so isn't
180 // repeated in this struct. The end address of the pickle should be used to
181 // construct a base::Pickle.
182 const char* pickle_end
;
183 // Only filled in if |message_found| is true.
184 // The end address of the message should be used to determine the start
185 // address of the next message.
186 const char* message_end
;
187 // If the message has brokerable attachments, this vector will contain the
188 // ids of the brokerable attachments. The caller of FindNext() is
189 // responsible for adding the attachments to the message.
190 std::vector
<BrokerableAttachment::AttachmentId
> attachment_ids
;
193 // |info| is an output parameter and must not be nullptr.
194 static void FindNext(const char* range_start
,
195 const char* range_end
,
196 NextMessageInfo
* info
);
198 // Adds a placeholder brokerable attachment that must be replaced before the
199 // message can be dispatched.
200 bool AddPlaceholderBrokerableAttachmentWithId(
201 BrokerableAttachment::AttachmentId id
);
203 // WriteAttachment appends |attachment| to the end of the set. It returns
204 // false iff the set is full.
205 bool WriteAttachment(scoped_refptr
<MessageAttachment
> attachment
);
206 // ReadAttachment parses an attachment given the parsing state |iter| and
207 // writes it to |*attachment|. It returns true on success.
208 bool ReadAttachment(base::PickleIterator
* iter
,
209 scoped_refptr
<MessageAttachment
>* attachment
) const;
210 // Returns true if there are any attachment in this message.
211 bool HasAttachments() const;
212 // Returns true if there are any MojoHandleAttachments in this message.
213 bool HasMojoHandles() const;
214 // Whether the message has any brokerable attachments.
215 bool HasBrokerableAttachments() const;
217 void set_sender_pid(base::ProcessId id
) { sender_pid_
= id
; }
218 base::ProcessId
get_sender_pid() const { return sender_pid_
; }
220 #ifdef IPC_MESSAGE_LOG_ENABLED
221 // Adds the outgoing time from Time::Now() at the end of the message and sets
222 // a bit to indicate that it's been added.
223 void set_sent_time(int64_t time
);
224 int64_t sent_time() const;
226 void set_received_time(int64_t time
) const;
227 int64_t received_time() const { return received_time_
; }
228 void set_output_params(const std::string
& op
) const { output_params_
= op
; }
229 const std::string
& output_params() const { return output_params_
; }
230 // The following four functions are needed so we can log sync messages with
231 // delayed replies. We stick the log data from the sent message into the
232 // reply message, so that when it's sent and we have the output parameters
233 // we can log it. As such, we set a flag on the sent message to not log it.
234 void set_sync_log_data(LogData
* data
) const { log_data_
= data
; }
235 LogData
* sync_log_data() const { return log_data_
; }
236 void set_dont_log() const { dont_log_
= true; }
237 bool dont_log() const { return dont_log_
; }
241 friend class Channel
;
242 friend class ChannelMojo
;
243 friend class ChannelNacl
;
244 friend class ChannelPosix
;
245 friend class ChannelWin
;
246 friend class internal::ChannelReader
;
247 friend class MessageReplyDeserializer
;
248 friend class SyncMessage
;
250 #pragma pack(push, 4)
251 struct Header
: base::Pickle::Header
{
252 int32_t routing
; // ID of the view that this message is destined for
253 uint32_t type
; // specifies the user-defined message type
254 uint32_t flags
; // specifies control flags for the message
255 #if USE_ATTACHMENT_BROKER
256 // The number of brokered attachments included with this message. The
257 // ids of the brokered attachment ids are sent immediately after the pickled
258 // message, before the next pickled message is sent.
259 uint32_t num_brokered_attachments
;
261 #if defined(OS_POSIX)
262 uint16_t num_fds
; // the number of descriptors included with this message
263 uint16_t pad
; // explicitly initialize this to appease valgrind
269 return headerT
<Header
>();
271 const Header
* header() const {
272 return headerT
<Header
>();
277 // Used internally to support IPC::Listener::OnBadMessageReceived.
278 mutable bool dispatch_error_
;
280 // The set of file descriptors associated with this message.
281 scoped_refptr
<MessageAttachmentSet
> attachment_set_
;
283 // Ensure that a MessageAttachmentSet is allocated
284 void EnsureMessageAttachmentSet();
286 MessageAttachmentSet
* attachment_set() {
287 EnsureMessageAttachmentSet();
288 return attachment_set_
.get();
290 const MessageAttachmentSet
* attachment_set() const {
291 return attachment_set_
.get();
294 // The process id of the sender of the message. This member is populated with
295 // a valid value for every message dispatched to listeners.
296 base::ProcessId sender_pid_
;
298 #ifdef IPC_MESSAGE_LOG_ENABLED
300 mutable int64_t received_time_
;
301 mutable std::string output_params_
;
302 mutable LogData
* log_data_
;
303 mutable bool dont_log_
;
307 //------------------------------------------------------------------------------
311 enum SpecialRoutingIDs
{
312 // indicates that we don't have a routing ID yet.
313 MSG_ROUTING_NONE
= -2,
315 // indicates a general message not sent to a particular tab.
316 MSG_ROUTING_CONTROL
= INT32_MAX
,
319 #define IPC_REPLY_ID 0xFFFFFFF0 // Special message id for replies
320 #define IPC_LOGGING_ID 0xFFFFFFF1 // Special message id for logging
322 #endif // IPC_IPC_MESSAGE_H_