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_
11 #include "base/basictypes.h"
12 #include "base/pickle.h"
13 #include "ipc/ipc_export.h"
15 // TODO(brettw) remove this when the "typedef Sender" is removed below.
16 #include "ipc/ipc_sender.h"
18 // Ipc logging adds a dependency from the 'chrome' target on all ipc message
19 // classes. In a component build, this would require exporting all message
20 // classes, so don't support ipc logging in the components build.
21 #if !defined(NDEBUG) && !defined(COMPONENT_BUILD)
22 #define IPC_MESSAGE_LOG_ENABLED
26 #include "base/memory/ref_counted.h"
30 struct FileDescriptor
;
33 class FileDescriptorSet
;
37 //------------------------------------------------------------------------------
43 class IPC_EXPORT Message
: public Pickle
{
45 // IPC::Sender used to be IPC::Message::Sender which prevented forward
46 // declarations. To keep existing code compiling, we provide this backwards-
47 // compatible definition. New code should use IPC::Sender.
48 // TODO(brettw) convert users of this and delete.
49 typedef IPC::Sender Sender
;
57 // Bit values used in the flags field.
59 PRIORITY_MASK
= 0x0003, // Low 2 bits of store the priority value.
62 REPLY_ERROR_BIT
= 0x0010,
64 PUMPING_MSGS_BIT
= 0x0040,
65 HAS_SENT_TIME_BIT
= 0x0080,
72 // Initialize a message with a user-defined type, priority value, and
73 // destination WebView ID.
74 Message(int32 routing_id
, uint32 type
, PriorityValue priority
);
76 // Initializes a message from a const block of data. The data is not copied;
77 // instead the data is merely referenced by this message. Only const methods
78 // should be used on the message when initialized this way.
79 Message(const char* data
, int data_len
);
81 Message(const Message
& other
);
82 Message
& operator=(const Message
& other
);
84 PriorityValue
priority() const {
85 return static_cast<PriorityValue
>(header()->flags
& PRIORITY_MASK
);
88 // True if this is a synchronous message.
90 header()->flags
|= SYNC_BIT
;
92 bool is_sync() const {
93 return (header()->flags
& SYNC_BIT
) != 0;
96 // Set this on a reply to a synchronous message.
98 header()->flags
|= REPLY_BIT
;
101 bool is_reply() const {
102 return (header()->flags
& REPLY_BIT
) != 0;
105 // Set this on a reply to a synchronous message to indicate that no receiver
107 void set_reply_error() {
108 header()->flags
|= REPLY_ERROR_BIT
;
111 bool is_reply_error() const {
112 return (header()->flags
& REPLY_ERROR_BIT
) != 0;
115 // Normally when a receiver gets a message and they're blocked on a
116 // synchronous message Send, they buffer a message. Setting this flag causes
117 // the receiver to be unblocked and the message to be dispatched immediately.
118 void set_unblock(bool unblock
) {
120 header()->flags
|= UNBLOCK_BIT
;
122 header()->flags
&= ~UNBLOCK_BIT
;
126 bool should_unblock() const {
127 return (header()->flags
& UNBLOCK_BIT
) != 0;
130 // Tells the receiver that the caller is pumping messages while waiting
132 bool is_caller_pumping_messages() const {
133 return (header()->flags
& PUMPING_MSGS_BIT
) != 0;
136 uint32
type() const {
137 return header()->type
;
140 int32
routing_id() const {
141 return header()->routing
;
144 void set_routing_id(int32 new_id
) {
145 header()->routing
= new_id
;
148 uint32
flags() const {
149 return header()->flags
;
152 template<class T
, class S
>
153 static bool Dispatch(const Message
* msg
, T
* obj
, S
* sender
,
159 template<class T
, class S
>
160 static bool Dispatch(const Message
* msg
, T
* obj
, S
* sender
,
161 void (T::*func
)() const) {
166 template<class T
, class S
>
167 static bool Dispatch(const Message
* msg
, T
* obj
, S
* sender
,
168 void (T::*func
)(const Message
&)) {
173 template<class T
, class S
>
174 static bool Dispatch(const Message
* msg
, T
* obj
, S
* sender
,
175 void (T::*func
)(const Message
&) const) {
180 // Used for async messages with no parameters.
181 static void Log(std::string
* name
, const Message
* msg
, std::string
* l
) {
184 // Find the end of the message data that starts at range_start. Returns NULL
185 // if the entire message is not found in the given data range.
186 static const char* FindNext(const char* range_start
, const char* range_end
) {
187 return Pickle::FindNext(sizeof(Header
), range_start
, range_end
);
190 #if defined(OS_POSIX)
191 // On POSIX, a message supports reading / writing FileDescriptor objects.
192 // This is used to pass a file descriptor to the peer of an IPC channel.
194 // Add a descriptor to the end of the set. Returns false iff the set is full.
195 bool WriteFileDescriptor(const base::FileDescriptor
& descriptor
);
196 // Get a file descriptor from the message. Returns false on error.
197 // iter: a Pickle iterator to the current location in the message.
198 bool ReadFileDescriptor(PickleIterator
* iter
,
199 base::FileDescriptor
* descriptor
) const;
202 #ifdef IPC_MESSAGE_LOG_ENABLED
203 // Adds the outgoing time from Time::Now() at the end of the message and sets
204 // a bit to indicate that it's been added.
205 void set_sent_time(int64 time
);
206 int64
sent_time() const;
208 void set_received_time(int64 time
) const;
209 int64
received_time() const { return received_time_
; }
210 void set_output_params(const std::string
& op
) const { output_params_
= op
; }
211 const std::string
& output_params() const { return output_params_
; }
212 // The following four functions are needed so we can log sync messages with
213 // delayed replies. We stick the log data from the sent message into the
214 // reply message, so that when it's sent and we have the output parameters
215 // we can log it. As such, we set a flag on the sent message to not log it.
216 void set_sync_log_data(LogData
* data
) const { log_data_
= data
; }
217 LogData
* sync_log_data() const { return log_data_
; }
218 void set_dont_log() const { dont_log_
= true; }
219 bool dont_log() const { return dont_log_
; }
223 friend class Channel
;
224 friend class MessageReplyDeserializer
;
225 friend class SyncMessage
;
227 #pragma pack(push, 4)
228 struct Header
: Pickle::Header
{
229 int32 routing
; // ID of the view that this message is destined for
230 uint32 type
; // specifies the user-defined message type
231 uint32 flags
; // specifies control flags for the message
232 #if defined(OS_POSIX)
233 uint16 num_fds
; // the number of descriptors included with this message
234 uint16 pad
; // explicitly initialize this to appease valgrind
240 return headerT
<Header
>();
242 const Header
* header() const {
243 return headerT
<Header
>();
246 void InitLoggingVariables();
248 #if defined(OS_POSIX)
249 // The set of file descriptors associated with this message.
250 scoped_refptr
<FileDescriptorSet
> file_descriptor_set_
;
252 // Ensure that a FileDescriptorSet is allocated
253 void EnsureFileDescriptorSet();
255 FileDescriptorSet
* file_descriptor_set() {
256 EnsureFileDescriptorSet();
257 return file_descriptor_set_
.get();
259 const FileDescriptorSet
* file_descriptor_set() const {
260 return file_descriptor_set_
.get();
264 #ifdef IPC_MESSAGE_LOG_ENABLED
266 mutable int64 received_time_
;
267 mutable std::string output_params_
;
268 mutable LogData
* log_data_
;
269 mutable bool dont_log_
;
273 //------------------------------------------------------------------------------
277 enum SpecialRoutingIDs
{
278 // indicates that we don't have a routing ID yet.
279 MSG_ROUTING_NONE
= -2,
281 // indicates a general message not sent to a particular tab.
282 MSG_ROUTING_CONTROL
= kint32max
,
285 #define IPC_REPLY_ID 0xFFFFFFF0 // Special message id for replies
286 #define IPC_LOGGING_ID 0xFFFFFFF1 // Special message id for logging
288 #endif // IPC_IPC_MESSAGE_H_