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 // Ipc logging adds a dependency from the 'chrome' target on all ipc message
16 // classes. In a component build, this would require exporting all message
17 // classes, so don't support ipc logging in the components build.
18 #if !defined(NDEBUG) && !defined(COMPONENT_BUILD)
19 #define IPC_MESSAGE_LOG_ENABLED
23 #include "base/memory/ref_counted.h"
27 struct FileDescriptor
;
30 class FileDescriptorSet
;
34 //------------------------------------------------------------------------------
40 class IPC_EXPORT Message
: public Pickle
{
42 // Implemented by objects that can send IPC messages across a channel.
43 class IPC_EXPORT Sender
{
47 // Sends the given IPC message. The implementor takes ownership of the
48 // given Message regardless of whether or not this method succeeds. This
49 // is done to make this method easier to use. Returns true on success and
51 virtual bool Send(Message
* msg
) = 0;
64 // Initialize a message with a user-defined type, priority value, and
65 // destination WebView ID.
66 Message(int32 routing_id
, uint32 type
, PriorityValue priority
);
68 // Initializes a message from a const block of data. The data is not copied;
69 // instead the data is merely referenced by this message. Only const methods
70 // should be used on the message when initialized this way.
71 Message(const char* data
, int data_len
);
73 Message(const Message
& other
);
74 Message
& operator=(const Message
& other
);
76 PriorityValue
priority() const {
77 return static_cast<PriorityValue
>(header()->flags
& PRIORITY_MASK
);
80 // True if this is a synchronous message.
81 bool is_sync() const {
82 return (header()->flags
& SYNC_BIT
) != 0;
85 // Set this on a reply to a synchronous message.
87 header()->flags
|= REPLY_BIT
;
90 bool is_reply() const {
91 return (header()->flags
& REPLY_BIT
) != 0;
94 // Set this on a reply to a synchronous message to indicate that no receiver
96 void set_reply_error() {
97 header()->flags
|= REPLY_ERROR_BIT
;
100 bool is_reply_error() const {
101 return (header()->flags
& REPLY_ERROR_BIT
) != 0;
104 // Normally when a receiver gets a message and they're blocked on a
105 // synchronous message Send, they buffer a message. Setting this flag causes
106 // the receiver to be unblocked and the message to be dispatched immediately.
107 void set_unblock(bool unblock
) {
109 header()->flags
|= UNBLOCK_BIT
;
111 header()->flags
&= ~UNBLOCK_BIT
;
115 bool should_unblock() const {
116 return (header()->flags
& UNBLOCK_BIT
) != 0;
119 // Tells the receiver that the caller is pumping messages while waiting
121 bool is_caller_pumping_messages() const {
122 return (header()->flags
& PUMPING_MSGS_BIT
) != 0;
125 uint32
type() const {
126 return header()->type
;
129 int32
routing_id() const {
130 return header()->routing
;
133 void set_routing_id(int32 new_id
) {
134 header()->routing
= new_id
;
137 template<class T
, class S
>
138 static bool Dispatch(const Message
* msg
, T
* obj
, S
* sender
,
144 template<class T
, class S
>
145 static bool Dispatch(const Message
* msg
, T
* obj
, S
* sender
,
146 void (T::*func
)() const) {
151 template<class T
, class S
>
152 static bool Dispatch(const Message
* msg
, T
* obj
, S
* sender
,
153 void (T::*func
)(const Message
&)) {
158 template<class T
, class S
>
159 static bool Dispatch(const Message
* msg
, T
* obj
, S
* sender
,
160 void (T::*func
)(const Message
&) const) {
165 // Used for async messages with no parameters.
166 static void Log(std::string
* name
, const Message
* msg
, std::string
* l
) {
169 // Find the end of the message data that starts at range_start. Returns NULL
170 // if the entire message is not found in the given data range.
171 static const char* FindNext(const char* range_start
, const char* range_end
) {
172 return Pickle::FindNext(sizeof(Header
), range_start
, range_end
);
175 #if defined(OS_POSIX)
176 // On POSIX, a message supports reading / writing FileDescriptor objects.
177 // This is used to pass a file descriptor to the peer of an IPC channel.
179 // Add a descriptor to the end of the set. Returns false iff the set is full.
180 bool WriteFileDescriptor(const base::FileDescriptor
& descriptor
);
181 // Get a file descriptor from the message. Returns false on error.
182 // iter: a Pickle iterator to the current location in the message.
183 bool ReadFileDescriptor(PickleIterator
* iter
,
184 base::FileDescriptor
* descriptor
) const;
187 #ifdef IPC_MESSAGE_LOG_ENABLED
188 // Adds the outgoing time from Time::Now() at the end of the message and sets
189 // a bit to indicate that it's been added.
190 void set_sent_time(int64 time
);
191 int64
sent_time() const;
193 void set_received_time(int64 time
) const;
194 int64
received_time() const { return received_time_
; }
195 void set_output_params(const std::string
& op
) const { output_params_
= op
; }
196 const std::string
& output_params() const { return output_params_
; }
197 // The following four functions are needed so we can log sync messages with
198 // delayed replies. We stick the log data from the sent message into the
199 // reply message, so that when it's sent and we have the output parameters
200 // we can log it. As such, we set a flag on the sent message to not log it.
201 void set_sync_log_data(LogData
* data
) const { log_data_
= data
; }
202 LogData
* sync_log_data() const { return log_data_
; }
203 void set_dont_log() const { dont_log_
= true; }
204 bool dont_log() const { return dont_log_
; }
208 friend class Channel
;
209 friend class MessageReplyDeserializer
;
210 friend class SyncMessage
;
213 header()->flags
|= SYNC_BIT
;
218 PRIORITY_MASK
= 0x0003,
221 REPLY_ERROR_BIT
= 0x0010,
222 UNBLOCK_BIT
= 0x0020,
223 PUMPING_MSGS_BIT
= 0x0040,
224 HAS_SENT_TIME_BIT
= 0x0080,
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_