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/debug/trace_event.h"
12 #include "base/pickle.h"
13 #include "ipc/ipc_export.h"
16 #define IPC_MESSAGE_LOG_ENABLED
20 #include "base/memory/ref_counted.h"
24 struct FileDescriptor
;
27 class FileDescriptorSet
;
31 //------------------------------------------------------------------------------
35 class IPC_EXPORT Message
: public 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 routing_id
, uint32 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
type() const {
133 return header()->type
;
136 int32
routing_id() const {
137 return header()->routing
;
140 void set_routing_id(int32 new_id
) {
141 header()->routing
= new_id
;
144 uint32
flags() const {
145 return header()->flags
;
148 // Sets all the given header values. The message should be empty at this
150 void SetHeaderValues(int32 routing
, uint32 type
, uint32 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 // Find the end of the message data that starts at range_start. Returns NULL
171 // if the entire message is not found in the given data range.
172 static const char* FindNext(const char* range_start
, const char* range_end
) {
173 return Pickle::FindNext(sizeof(Header
), range_start
, range_end
);
176 #if defined(OS_POSIX)
177 // On POSIX, a message supports reading / writing FileDescriptor objects.
178 // This is used to pass a file descriptor to the peer of an IPC channel.
180 // Add a descriptor to the end of the set. Returns false if the set is full.
181 bool WriteFileDescriptor(const base::FileDescriptor
& descriptor
);
183 // Get a file descriptor from the message. Returns false on error.
184 // iter: a Pickle iterator to the current location in the message.
185 bool ReadFileDescriptor(PickleIterator
* iter
,
186 base::FileDescriptor
* descriptor
) const;
188 // Returns true if there are any file descriptors in this message.
189 bool HasFileDescriptors() const;
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 time
);
196 int64
sent_time() const;
198 void set_received_time(int64 time
) const;
199 int64
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_
; }
212 // Called to trace when message is sent.
213 void TraceMessageBegin() {
214 TRACE_EVENT_FLOW_BEGIN0(TRACE_DISABLED_BY_DEFAULT("ipc.flow"), "IPC",
217 // Called to trace when message is received.
218 void TraceMessageEnd() {
219 TRACE_EVENT_FLOW_END0(TRACE_DISABLED_BY_DEFAULT("ipc.flow"), "IPC",
224 friend class Channel
;
225 friend class ChannelNacl
;
226 friend class ChannelPosix
;
227 friend class ChannelWin
;
228 friend class MessageReplyDeserializer
;
229 friend class SyncMessage
;
231 #pragma pack(push, 4)
232 struct Header
: Pickle::Header
{
233 int32 routing
; // ID of the view that this message is destined for
234 uint32 type
; // specifies the user-defined message type
235 uint32 flags
; // specifies control flags for the message
236 #if defined(OS_POSIX)
237 uint16 num_fds
; // the number of descriptors included with this message
238 uint16 pad
; // explicitly initialize this to appease valgrind
244 return headerT
<Header
>();
246 const Header
* header() const {
247 return headerT
<Header
>();
252 // Used internally to support IPC::Listener::OnBadMessageReceived.
253 mutable bool dispatch_error_
;
255 #if defined(OS_POSIX)
256 // The set of file descriptors associated with this message.
257 scoped_refptr
<FileDescriptorSet
> file_descriptor_set_
;
259 // Ensure that a FileDescriptorSet is allocated
260 void EnsureFileDescriptorSet();
262 FileDescriptorSet
* file_descriptor_set() {
263 EnsureFileDescriptorSet();
264 return file_descriptor_set_
.get();
266 const FileDescriptorSet
* file_descriptor_set() const {
267 return file_descriptor_set_
.get();
271 #ifdef IPC_MESSAGE_LOG_ENABLED
273 mutable int64 received_time_
;
274 mutable std::string output_params_
;
275 mutable LogData
* log_data_
;
276 mutable bool dont_log_
;
280 //------------------------------------------------------------------------------
284 enum SpecialRoutingIDs
{
285 // indicates that we don't have a routing ID yet.
286 MSG_ROUTING_NONE
= -2,
288 // indicates a general message not sent to a particular tab.
289 MSG_ROUTING_CONTROL
= kint32max
,
292 #define IPC_REPLY_ID 0xFFFFFFF0 // Special message id for replies
293 #define IPC_LOGGING_ID 0xFFFFFFF1 // Special message id for logging
295 #endif // IPC_IPC_MESSAGE_H_