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/files/file.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
21 #include "base/memory/ref_counted.h"
26 //------------------------------------------------------------------------------
29 class MessageAttachmentSet
;
31 class IPC_EXPORT Message
: public Pickle
{
39 // Bit values used in the flags field.
40 // Upper 24 bits of flags store a reference number, so this enum is limited to
43 PRIORITY_MASK
= 0x03, // Low 2 bits of store the priority value.
46 REPLY_ERROR_BIT
= 0x10,
48 PUMPING_MSGS_BIT
= 0x40,
49 HAS_SENT_TIME_BIT
= 0x80,
56 // Initialize a message with a user-defined type, priority value, and
57 // destination WebView ID.
58 Message(int32 routing_id
, uint32 type
, PriorityValue priority
);
60 // Initializes a message from a const block of data. The data is not copied;
61 // instead the data is merely referenced by this message. Only const methods
62 // should be used on the message when initialized this way.
63 Message(const char* data
, int data_len
);
65 Message(const Message
& other
);
66 Message
& operator=(const Message
& other
);
68 PriorityValue
priority() const {
69 return static_cast<PriorityValue
>(header()->flags
& PRIORITY_MASK
);
72 // True if this is a synchronous message.
74 header()->flags
|= SYNC_BIT
;
76 bool is_sync() const {
77 return (header()->flags
& SYNC_BIT
) != 0;
80 // Set this on a reply to a synchronous message.
82 header()->flags
|= REPLY_BIT
;
85 bool is_reply() const {
86 return (header()->flags
& REPLY_BIT
) != 0;
89 // Set this on a reply to a synchronous message to indicate that no receiver
91 void set_reply_error() {
92 header()->flags
|= REPLY_ERROR_BIT
;
95 bool is_reply_error() const {
96 return (header()->flags
& REPLY_ERROR_BIT
) != 0;
99 // Normally when a receiver gets a message and they're blocked on a
100 // synchronous message Send, they buffer a message. Setting this flag causes
101 // the receiver to be unblocked and the message to be dispatched immediately.
102 void set_unblock(bool unblock
) {
104 header()->flags
|= UNBLOCK_BIT
;
106 header()->flags
&= ~UNBLOCK_BIT
;
110 bool should_unblock() const {
111 return (header()->flags
& UNBLOCK_BIT
) != 0;
114 // Tells the receiver that the caller is pumping messages while waiting
116 bool is_caller_pumping_messages() const {
117 return (header()->flags
& PUMPING_MSGS_BIT
) != 0;
120 void set_dispatch_error() const {
121 dispatch_error_
= true;
124 bool dispatch_error() const {
125 return dispatch_error_
;
128 uint32
type() const {
129 return header()->type
;
132 int32
routing_id() const {
133 return header()->routing
;
136 void set_routing_id(int32 new_id
) {
137 header()->routing
= new_id
;
140 uint32
flags() const {
141 return header()->flags
;
144 // Sets all the given header values. The message should be empty at this
146 void SetHeaderValues(int32 routing
, uint32 type
, uint32 flags
);
148 template<class T
, class S
, class P
>
149 static bool Dispatch(const Message
* msg
, T
* obj
, S
* sender
, P
* parameter
,
155 template<class T
, class S
, class P
>
156 static bool Dispatch(const Message
* msg
, T
* obj
, S
* sender
, P
* parameter
,
157 void (T::*func
)(P
*)) {
158 (obj
->*func
)(parameter
);
162 // Used for async messages with no parameters.
163 static void Log(std::string
* name
, const Message
* msg
, std::string
* l
) {
166 // Find the end of the message data that starts at range_start. Returns NULL
167 // if the entire message is not found in the given data range.
168 static const char* FindNext(const char* range_start
, const char* range_end
) {
169 return Pickle::FindNext(sizeof(Header
), range_start
, range_end
);
172 #if defined(OS_POSIX)
173 // On POSIX, a message supports reading / writing FileDescriptor objects.
174 // This is used to pass a file descriptor to the peer of an IPC channel.
176 // Add a descriptor to the end of the set. Returns false if the set is full.
177 bool WriteFile(base::ScopedFD descriptor
);
178 bool WriteBorrowingFile(const base::PlatformFile
& descriptor
);
180 // Get a file descriptor from the message. Returns false on error.
181 // iter: a Pickle iterator to the current location in the message.
182 bool ReadFile(PickleIterator
* iter
, base::ScopedFD
* file
) const;
184 // Returns true if there are any file descriptors in this message.
185 bool HasFileDescriptors() const;
188 #ifdef IPC_MESSAGE_LOG_ENABLED
189 // Adds the outgoing time from Time::Now() at the end of the message and sets
190 // a bit to indicate that it's been added.
191 void set_sent_time(int64 time
);
192 int64
sent_time() const;
194 void set_received_time(int64 time
) const;
195 int64
received_time() const { return received_time_
; }
196 void set_output_params(const std::string
& op
) const { output_params_
= op
; }
197 const std::string
& output_params() const { return output_params_
; }
198 // The following four functions are needed so we can log sync messages with
199 // delayed replies. We stick the log data from the sent message into the
200 // reply message, so that when it's sent and we have the output parameters
201 // we can log it. As such, we set a flag on the sent message to not log it.
202 void set_sync_log_data(LogData
* data
) const { log_data_
= data
; }
203 LogData
* sync_log_data() const { return log_data_
; }
204 void set_dont_log() const { dont_log_
= true; }
205 bool dont_log() const { return dont_log_
; }
208 // Called to trace when message is sent.
209 void TraceMessageBegin() {
210 TRACE_EVENT_FLOW_BEGIN0(TRACE_DISABLED_BY_DEFAULT("ipc.flow"), "IPC",
213 // Called to trace when message is received.
214 void TraceMessageEnd() {
215 TRACE_EVENT_FLOW_END0(TRACE_DISABLED_BY_DEFAULT("ipc.flow"), "IPC",
220 friend class Channel
;
221 friend class ChannelMojo
;
222 friend class ChannelNacl
;
223 friend class ChannelPosix
;
224 friend class ChannelWin
;
225 friend class MessageReplyDeserializer
;
226 friend class SyncMessage
;
228 #pragma pack(push, 4)
229 struct Header
: Pickle::Header
{
230 int32 routing
; // ID of the view that this message is destined for
231 uint32 type
; // specifies the user-defined message type
232 uint32 flags
; // specifies control flags for the message
233 #if defined(OS_POSIX)
234 uint16 num_fds
; // the number of descriptors included with this message
235 uint16 pad
; // explicitly initialize this to appease valgrind
241 return headerT
<Header
>();
243 const Header
* header() const {
244 return headerT
<Header
>();
249 // Used internally to support IPC::Listener::OnBadMessageReceived.
250 mutable bool dispatch_error_
;
252 // The set of file descriptors associated with this message.
253 scoped_refptr
<MessageAttachmentSet
> attachment_set_
;
255 // Ensure that a MessageAttachmentSet is allocated
256 void EnsureMessageAttachmentSet();
258 MessageAttachmentSet
* attachment_set() {
259 EnsureMessageAttachmentSet();
260 return attachment_set_
.get();
262 const MessageAttachmentSet
* attachment_set() const {
263 return attachment_set_
.get();
266 #ifdef IPC_MESSAGE_LOG_ENABLED
268 mutable int64 received_time_
;
269 mutable std::string output_params_
;
270 mutable LogData
* log_data_
;
271 mutable bool dont_log_
;
275 //------------------------------------------------------------------------------
279 enum SpecialRoutingIDs
{
280 // indicates that we don't have a routing ID yet.
281 MSG_ROUTING_NONE
= -2,
283 // indicates a general message not sent to a particular tab.
284 MSG_ROUTING_CONTROL
= kint32max
,
287 #define IPC_REPLY_ID 0xFFFFFFF0 // Special message id for replies
288 #define IPC_LOGGING_ID 0xFFFFFFF1 // Special message id for logging
290 #endif // IPC_IPC_MESSAGE_H_