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"
15 // TODO(brettw) remove this and update files that depend on this being included
17 #include "ipc/ipc_sender.h"
20 #define IPC_MESSAGE_LOG_ENABLED
24 #include "base/memory/ref_counted.h"
28 struct FileDescriptor
;
31 class FileDescriptorSet
;
35 //------------------------------------------------------------------------------
41 class IPC_EXPORT Message
: public Pickle
{
49 // Bit values used in the flags field.
50 // Upper 24 bits of flags store a reference number, so this enum is limited to
53 PRIORITY_MASK
= 0x03, // Low 2 bits of store the priority value.
56 REPLY_ERROR_BIT
= 0x10,
58 PUMPING_MSGS_BIT
= 0x40,
59 HAS_SENT_TIME_BIT
= 0x80,
66 // Initialize a message with a user-defined type, priority value, and
67 // destination WebView ID.
68 Message(int32 routing_id
, uint32 type
, PriorityValue priority
);
70 // Initializes a message from a const block of data. The data is not copied;
71 // instead the data is merely referenced by this message. Only const methods
72 // should be used on the message when initialized this way.
73 Message(const char* data
, int data_len
);
75 Message(const Message
& other
);
76 Message
& operator=(const Message
& other
);
78 PriorityValue
priority() const {
79 return static_cast<PriorityValue
>(header()->flags
& PRIORITY_MASK
);
82 // True if this is a synchronous message.
84 header()->flags
|= SYNC_BIT
;
86 bool is_sync() const {
87 return (header()->flags
& SYNC_BIT
) != 0;
90 // Set this on a reply to a synchronous message.
92 header()->flags
|= REPLY_BIT
;
95 bool is_reply() const {
96 return (header()->flags
& REPLY_BIT
) != 0;
99 // Set this on a reply to a synchronous message to indicate that no receiver
101 void set_reply_error() {
102 header()->flags
|= REPLY_ERROR_BIT
;
105 bool is_reply_error() const {
106 return (header()->flags
& REPLY_ERROR_BIT
) != 0;
109 // Normally when a receiver gets a message and they're blocked on a
110 // synchronous message Send, they buffer a message. Setting this flag causes
111 // the receiver to be unblocked and the message to be dispatched immediately.
112 void set_unblock(bool unblock
) {
114 header()->flags
|= UNBLOCK_BIT
;
116 header()->flags
&= ~UNBLOCK_BIT
;
120 bool should_unblock() const {
121 return (header()->flags
& UNBLOCK_BIT
) != 0;
124 // Tells the receiver that the caller is pumping messages while waiting
126 bool is_caller_pumping_messages() const {
127 return (header()->flags
& PUMPING_MSGS_BIT
) != 0;
130 uint32
type() const {
131 return header()->type
;
134 int32
routing_id() const {
135 return header()->routing
;
138 void set_routing_id(int32 new_id
) {
139 header()->routing
= new_id
;
142 uint32
flags() const {
143 return header()->flags
;
146 // Sets all the given header values. The message should be empty at this
148 void SetHeaderValues(int32 routing
, uint32 type
, uint32 flags
);
150 template<class T
, class S
>
151 static bool Dispatch(const Message
* msg
, T
* obj
, S
* sender
,
157 template<class T
, class S
>
158 static bool Dispatch(const Message
* msg
, T
* obj
, S
* sender
,
159 void (T::*func
)() const) {
164 template<class T
, class S
>
165 static bool Dispatch(const Message
* msg
, T
* obj
, S
* sender
,
166 void (T::*func
)(const Message
&)) {
171 template<class T
, class S
>
172 static bool Dispatch(const Message
* msg
, T
* obj
, S
* sender
,
173 void (T::*func
)(const Message
&) const) {
178 // Used for async messages with no parameters.
179 static void Log(std::string
* name
, const Message
* msg
, std::string
* l
) {
182 // Find the end of the message data that starts at range_start. Returns NULL
183 // if the entire message is not found in the given data range.
184 static const char* FindNext(const char* range_start
, const char* range_end
) {
185 return Pickle::FindNext(sizeof(Header
), range_start
, range_end
);
188 #if defined(OS_POSIX)
189 // On POSIX, a message supports reading / writing FileDescriptor objects.
190 // This is used to pass a file descriptor to the peer of an IPC channel.
192 // Add a descriptor to the end of the set. Returns false if the set is full.
193 bool WriteFileDescriptor(const base::FileDescriptor
& descriptor
);
195 // Get a file descriptor from the message. Returns false on error.
196 // iter: a Pickle iterator to the current location in the message.
197 bool ReadFileDescriptor(PickleIterator
* iter
,
198 base::FileDescriptor
* descriptor
) const;
200 // Returns true if there are any file descriptors in this message.
201 bool HasFileDescriptors() const;
204 #ifdef IPC_MESSAGE_LOG_ENABLED
205 // Adds the outgoing time from Time::Now() at the end of the message and sets
206 // a bit to indicate that it's been added.
207 void set_sent_time(int64 time
);
208 int64
sent_time() const;
210 void set_received_time(int64 time
) const;
211 int64
received_time() const { return received_time_
; }
212 void set_output_params(const std::string
& op
) const { output_params_
= op
; }
213 const std::string
& output_params() const { return output_params_
; }
214 // The following four functions are needed so we can log sync messages with
215 // delayed replies. We stick the log data from the sent message into the
216 // reply message, so that when it's sent and we have the output parameters
217 // we can log it. As such, we set a flag on the sent message to not log it.
218 void set_sync_log_data(LogData
* data
) const { log_data_
= data
; }
219 LogData
* sync_log_data() const { return log_data_
; }
220 void set_dont_log() const { dont_log_
= true; }
221 bool dont_log() const { return dont_log_
; }
224 // Called to trace when message is sent.
225 void TraceMessageBegin() {
226 TRACE_EVENT_FLOW_BEGIN0("ipc", "IPC", header()->flags
);
228 // Called to trace when message is received.
229 void TraceMessageEnd() {
230 TRACE_EVENT_FLOW_END0("ipc", "IPC", header()->flags
);
234 friend class Channel
;
235 friend class MessageReplyDeserializer
;
236 friend class SyncMessage
;
238 #pragma pack(push, 4)
239 struct Header
: Pickle::Header
{
240 int32 routing
; // ID of the view that this message is destined for
241 uint32 type
; // specifies the user-defined message type
242 uint32 flags
; // specifies control flags for the message
243 #if defined(OS_POSIX)
244 uint16 num_fds
; // the number of descriptors included with this message
245 uint16 pad
; // explicitly initialize this to appease valgrind
251 return headerT
<Header
>();
253 const Header
* header() const {
254 return headerT
<Header
>();
257 void InitLoggingVariables();
259 #if defined(OS_POSIX)
260 // The set of file descriptors associated with this message.
261 scoped_refptr
<FileDescriptorSet
> file_descriptor_set_
;
263 // Ensure that a FileDescriptorSet is allocated
264 void EnsureFileDescriptorSet();
266 FileDescriptorSet
* file_descriptor_set() {
267 EnsureFileDescriptorSet();
268 return file_descriptor_set_
.get();
270 const FileDescriptorSet
* file_descriptor_set() const {
271 return file_descriptor_set_
.get();
275 #ifdef IPC_MESSAGE_LOG_ENABLED
277 mutable int64 received_time_
;
278 mutable std::string output_params_
;
279 mutable LogData
* log_data_
;
280 mutable bool dont_log_
;
284 //------------------------------------------------------------------------------
288 enum SpecialRoutingIDs
{
289 // indicates that we don't have a routing ID yet.
290 MSG_ROUTING_NONE
= -2,
292 // indicates a general message not sent to a particular tab.
293 MSG_ROUTING_CONTROL
= kint32max
,
296 #define IPC_REPLY_ID 0xFFFFFFF0 // Special message id for replies
297 #define IPC_LOGGING_ID 0xFFFFFFF1 // Special message id for logging
299 #endif // IPC_IPC_MESSAGE_H_