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/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 // WriteAttachment appends |attachment| to the end of the set. It returns
199 // false iff the set is full.
200 bool WriteAttachment(scoped_refptr
<MessageAttachment
> attachment
);
201 // ReadAttachment parses an attachment given the parsing state |iter| and
202 // writes it to |*attachment|. It returns true on success.
203 bool ReadAttachment(base::PickleIterator
* iter
,
204 scoped_refptr
<MessageAttachment
>* attachment
) const;
205 // Returns true if there are any attachment in this message.
206 bool HasAttachments() const;
207 // Returns true if there are any MojoHandleAttachments in this message.
208 bool HasMojoHandles() const;
209 // Whether the message has any brokerable attachments.
210 bool HasBrokerableAttachments() const;
212 void set_sender_pid(base::ProcessId id
) { sender_pid_
= id
; }
213 base::ProcessId
get_sender_pid() const { return sender_pid_
; }
215 #ifdef IPC_MESSAGE_LOG_ENABLED
216 // Adds the outgoing time from Time::Now() at the end of the message and sets
217 // a bit to indicate that it's been added.
218 void set_sent_time(int64_t time
);
219 int64_t sent_time() const;
221 void set_received_time(int64_t time
) const;
222 int64_t received_time() const { return received_time_
; }
223 void set_output_params(const std::string
& op
) const { output_params_
= op
; }
224 const std::string
& output_params() const { return output_params_
; }
225 // The following four functions are needed so we can log sync messages with
226 // delayed replies. We stick the log data from the sent message into the
227 // reply message, so that when it's sent and we have the output parameters
228 // we can log it. As such, we set a flag on the sent message to not log it.
229 void set_sync_log_data(LogData
* data
) const { log_data_
= data
; }
230 LogData
* sync_log_data() const { return log_data_
; }
231 void set_dont_log() const { dont_log_
= true; }
232 bool dont_log() const { return dont_log_
; }
236 friend class Channel
;
237 friend class ChannelMojo
;
238 friend class ChannelNacl
;
239 friend class ChannelPosix
;
240 friend class ChannelWin
;
241 friend class internal::ChannelReader
;
242 friend class MessageReplyDeserializer
;
243 friend class SyncMessage
;
245 #pragma pack(push, 4)
246 struct Header
: base::Pickle::Header
{
247 int32_t routing
; // ID of the view that this message is destined for
248 uint32_t type
; // specifies the user-defined message type
249 uint32_t flags
; // specifies control flags for the message
250 #if defined(OS_POSIX)
251 uint16_t num_fds
; // the number of descriptors included with this message
252 uint16_t pad
; // explicitly initialize this to appease valgrind
258 return headerT
<Header
>();
260 const Header
* header() const {
261 return headerT
<Header
>();
266 // Used internally to support IPC::Listener::OnBadMessageReceived.
267 mutable bool dispatch_error_
;
269 // The set of file descriptors associated with this message.
270 scoped_refptr
<MessageAttachmentSet
> attachment_set_
;
272 // Ensure that a MessageAttachmentSet is allocated
273 void EnsureMessageAttachmentSet();
275 MessageAttachmentSet
* attachment_set() {
276 EnsureMessageAttachmentSet();
277 return attachment_set_
.get();
279 const MessageAttachmentSet
* attachment_set() const {
280 return attachment_set_
.get();
283 // The process id of the sender of the message. This member is populated with
284 // a valid value for every message dispatched to listeners.
285 base::ProcessId sender_pid_
;
287 #ifdef IPC_MESSAGE_LOG_ENABLED
289 mutable int64_t received_time_
;
290 mutable std::string output_params_
;
291 mutable LogData
* log_data_
;
292 mutable bool dont_log_
;
296 //------------------------------------------------------------------------------
300 enum SpecialRoutingIDs
{
301 // indicates that we don't have a routing ID yet.
302 MSG_ROUTING_NONE
= -2,
304 // indicates a general message not sent to a particular tab.
305 MSG_ROUTING_CONTROL
= kint32max
,
308 #define IPC_REPLY_ID 0xFFFFFFF0 // Special message id for replies
309 #define IPC_LOGGING_ID 0xFFFFFFF1 // Special message id for logging
311 #endif // IPC_IPC_MESSAGE_H_