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"
19 // Ipc logging adds a dependency from the 'chrome' target on all ipc message
20 // classes. In a component build, this would require exporting all message
21 // classes, so don't support ipc logging in the components build.
22 #if !defined(NDEBUG) && !defined(COMPONENT_BUILD)
23 #define IPC_MESSAGE_LOG_ENABLED
27 #include "base/memory/ref_counted.h"
31 struct FileDescriptor
;
34 class FileDescriptorSet
;
38 //------------------------------------------------------------------------------
44 class IPC_EXPORT Message
: public Pickle
{
52 // Bit values used in the flags field.
53 // Upper 24 bits of flags store a reference number, so this enum is limited to
56 PRIORITY_MASK
= 0x03, // Low 2 bits of store the priority value.
59 REPLY_ERROR_BIT
= 0x10,
61 PUMPING_MSGS_BIT
= 0x40,
62 HAS_SENT_TIME_BIT
= 0x80,
69 // Initialize a message with a user-defined type, priority value, and
70 // destination WebView ID.
71 Message(int32 routing_id
, uint32 type
, PriorityValue priority
);
73 // Initializes a message from a const block of data. The data is not copied;
74 // instead the data is merely referenced by this message. Only const methods
75 // should be used on the message when initialized this way.
76 Message(const char* data
, int data_len
);
78 Message(const Message
& other
);
79 Message
& operator=(const Message
& other
);
81 PriorityValue
priority() const {
82 return static_cast<PriorityValue
>(header()->flags
& PRIORITY_MASK
);
85 // True if this is a synchronous message.
87 header()->flags
|= SYNC_BIT
;
89 bool is_sync() const {
90 return (header()->flags
& SYNC_BIT
) != 0;
93 // Set this on a reply to a synchronous message.
95 header()->flags
|= REPLY_BIT
;
98 bool is_reply() const {
99 return (header()->flags
& REPLY_BIT
) != 0;
102 // Set this on a reply to a synchronous message to indicate that no receiver
104 void set_reply_error() {
105 header()->flags
|= REPLY_ERROR_BIT
;
108 bool is_reply_error() const {
109 return (header()->flags
& REPLY_ERROR_BIT
) != 0;
112 // Normally when a receiver gets a message and they're blocked on a
113 // synchronous message Send, they buffer a message. Setting this flag causes
114 // the receiver to be unblocked and the message to be dispatched immediately.
115 void set_unblock(bool unblock
) {
117 header()->flags
|= UNBLOCK_BIT
;
119 header()->flags
&= ~UNBLOCK_BIT
;
123 bool should_unblock() const {
124 return (header()->flags
& UNBLOCK_BIT
) != 0;
127 // Tells the receiver that the caller is pumping messages while waiting
129 bool is_caller_pumping_messages() const {
130 return (header()->flags
& PUMPING_MSGS_BIT
) != 0;
133 uint32
type() const {
134 return header()->type
;
137 int32
routing_id() const {
138 return header()->routing
;
141 void set_routing_id(int32 new_id
) {
142 header()->routing
= new_id
;
145 uint32
flags() const {
146 return header()->flags
;
149 // Sets all the given header values. The message should be empty at this
151 void SetHeaderValues(int32 routing
, uint32 type
, uint32 flags
);
153 template<class T
, class S
>
154 static bool Dispatch(const Message
* msg
, T
* obj
, S
* sender
,
160 template<class T
, class S
>
161 static bool Dispatch(const Message
* msg
, T
* obj
, S
* sender
,
162 void (T::*func
)() const) {
167 template<class T
, class S
>
168 static bool Dispatch(const Message
* msg
, T
* obj
, S
* sender
,
169 void (T::*func
)(const Message
&)) {
174 template<class T
, class S
>
175 static bool Dispatch(const Message
* msg
, T
* obj
, S
* sender
,
176 void (T::*func
)(const Message
&) const) {
181 // Used for async messages with no parameters.
182 static void Log(std::string
* name
, const Message
* msg
, std::string
* l
) {
185 // Find the end of the message data that starts at range_start. Returns NULL
186 // if the entire message is not found in the given data range.
187 static const char* FindNext(const char* range_start
, const char* range_end
) {
188 return Pickle::FindNext(sizeof(Header
), range_start
, range_end
);
191 #if defined(OS_POSIX)
192 // On POSIX, a message supports reading / writing FileDescriptor objects.
193 // This is used to pass a file descriptor to the peer of an IPC channel.
195 // Add a descriptor to the end of the set. Returns false if the set is full.
196 bool WriteFileDescriptor(const base::FileDescriptor
& descriptor
);
198 // Get a file descriptor from the message. Returns false on error.
199 // iter: a Pickle iterator to the current location in the message.
200 bool ReadFileDescriptor(PickleIterator
* iter
,
201 base::FileDescriptor
* descriptor
) const;
203 // Returns true if there are any file descriptors in this message.
204 bool HasFileDescriptors() const;
207 #ifdef IPC_MESSAGE_LOG_ENABLED
208 // Adds the outgoing time from Time::Now() at the end of the message and sets
209 // a bit to indicate that it's been added.
210 void set_sent_time(int64 time
);
211 int64
sent_time() const;
213 void set_received_time(int64 time
) const;
214 int64
received_time() const { return received_time_
; }
215 void set_output_params(const std::string
& op
) const { output_params_
= op
; }
216 const std::string
& output_params() const { return output_params_
; }
217 // The following four functions are needed so we can log sync messages with
218 // delayed replies. We stick the log data from the sent message into the
219 // reply message, so that when it's sent and we have the output parameters
220 // we can log it. As such, we set a flag on the sent message to not log it.
221 void set_sync_log_data(LogData
* data
) const { log_data_
= data
; }
222 LogData
* sync_log_data() const { return log_data_
; }
223 void set_dont_log() const { dont_log_
= true; }
224 bool dont_log() const { return dont_log_
; }
227 // Called to trace when message is sent.
228 void TraceMessageBegin() {
229 TRACE_EVENT_FLOW_BEGIN0("ipc", "IPC", header()->flags
);
231 // Called to trace when message is received.
232 void TraceMessageEnd() {
233 TRACE_EVENT_FLOW_END0("ipc", "IPC", header()->flags
);
237 friend class Channel
;
238 friend class MessageReplyDeserializer
;
239 friend class SyncMessage
;
241 #pragma pack(push, 4)
242 struct Header
: Pickle::Header
{
243 int32 routing
; // ID of the view that this message is destined for
244 uint32 type
; // specifies the user-defined message type
245 uint32 flags
; // specifies control flags for the message
246 #if defined(OS_POSIX)
247 uint16 num_fds
; // the number of descriptors included with this message
248 uint16 pad
; // explicitly initialize this to appease valgrind
254 return headerT
<Header
>();
256 const Header
* header() const {
257 return headerT
<Header
>();
260 void InitLoggingVariables();
262 #if defined(OS_POSIX)
263 // The set of file descriptors associated with this message.
264 scoped_refptr
<FileDescriptorSet
> file_descriptor_set_
;
266 // Ensure that a FileDescriptorSet is allocated
267 void EnsureFileDescriptorSet();
269 FileDescriptorSet
* file_descriptor_set() {
270 EnsureFileDescriptorSet();
271 return file_descriptor_set_
.get();
273 const FileDescriptorSet
* file_descriptor_set() const {
274 return file_descriptor_set_
.get();
278 #ifdef IPC_MESSAGE_LOG_ENABLED
280 mutable int64 received_time_
;
281 mutable std::string output_params_
;
282 mutable LogData
* log_data_
;
283 mutable bool dont_log_
;
287 //------------------------------------------------------------------------------
291 enum SpecialRoutingIDs
{
292 // indicates that we don't have a routing ID yet.
293 MSG_ROUTING_NONE
= -2,
295 // indicates a general message not sent to a particular tab.
296 MSG_ROUTING_CONTROL
= kint32max
,
299 #define IPC_REPLY_ID 0xFFFFFFF0 // Special message id for replies
300 #define IPC_LOGGING_ID 0xFFFFFFF1 // Special message id for logging
302 #endif // IPC_IPC_MESSAGE_H_