Roll src/third_party/skia 9389b87:aba1dc8
[chromium-blink-merge.git] / ipc / ipc_message.h
blobd0e38afc342218365423056330b0eed5b734d8ba
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_
8 #include <string>
10 #include "base/basictypes.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/pickle.h"
13 #include "base/trace_event/trace_event.h"
14 #include "ipc/attachment_broker.h"
15 #include "ipc/ipc_export.h"
17 #if !defined(NDEBUG)
18 #define IPC_MESSAGE_LOG_ENABLED
19 #endif
21 namespace IPC {
23 namespace internal {
24 class ChannelReader;
25 } // namespace internal
27 //------------------------------------------------------------------------------
29 struct LogData;
30 class MessageAttachment;
31 class MessageAttachmentSet;
33 class IPC_EXPORT Message : public base::Pickle {
34 public:
35 enum PriorityValue {
36 PRIORITY_LOW = 1,
37 PRIORITY_NORMAL,
38 PRIORITY_HIGH
41 // Bit values used in the flags field.
42 // Upper 24 bits of flags store a reference number, so this enum is limited to
43 // 8 bits.
44 enum {
45 PRIORITY_MASK = 0x03, // Low 2 bits of store the priority value.
46 SYNC_BIT = 0x04,
47 REPLY_BIT = 0x08,
48 REPLY_ERROR_BIT = 0x10,
49 UNBLOCK_BIT = 0x20,
50 PUMPING_MSGS_BIT = 0x40,
51 HAS_SENT_TIME_BIT = 0x80,
54 ~Message() override;
56 Message();
58 // Initialize a message with a user-defined type, priority value, and
59 // destination WebView ID.
60 Message(int32 routing_id, uint32 type, PriorityValue priority);
62 // Initializes a message from a const block of data. The data is not copied;
63 // instead the data is merely referenced by this message. Only const methods
64 // should be used on the message when initialized this way.
65 Message(const char* data, int data_len);
67 Message(const Message& other);
68 Message& operator=(const Message& other);
70 PriorityValue priority() const {
71 return static_cast<PriorityValue>(header()->flags & PRIORITY_MASK);
74 // True if this is a synchronous message.
75 void set_sync() {
76 header()->flags |= SYNC_BIT;
78 bool is_sync() const {
79 return (header()->flags & SYNC_BIT) != 0;
82 // Set this on a reply to a synchronous message.
83 void set_reply() {
84 header()->flags |= REPLY_BIT;
87 bool is_reply() const {
88 return (header()->flags & REPLY_BIT) != 0;
91 // Set this on a reply to a synchronous message to indicate that no receiver
92 // was found.
93 void set_reply_error() {
94 header()->flags |= REPLY_ERROR_BIT;
97 bool is_reply_error() const {
98 return (header()->flags & REPLY_ERROR_BIT) != 0;
101 // Normally when a receiver gets a message and they're blocked on a
102 // synchronous message Send, they buffer a message. Setting this flag causes
103 // the receiver to be unblocked and the message to be dispatched immediately.
104 void set_unblock(bool unblock) {
105 if (unblock) {
106 header()->flags |= UNBLOCK_BIT;
107 } else {
108 header()->flags &= ~UNBLOCK_BIT;
112 bool should_unblock() const {
113 return (header()->flags & UNBLOCK_BIT) != 0;
116 // Tells the receiver that the caller is pumping messages while waiting
117 // for the result.
118 bool is_caller_pumping_messages() const {
119 return (header()->flags & PUMPING_MSGS_BIT) != 0;
122 void set_dispatch_error() const {
123 dispatch_error_ = true;
126 bool dispatch_error() const {
127 return dispatch_error_;
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
147 // call.
148 void SetHeaderValues(int32 routing, uint32 type, uint32 flags);
150 template<class T, class S, class P>
151 static bool Dispatch(const Message* msg, T* obj, S* sender, P* parameter,
152 void (T::*func)()) {
153 (obj->*func)();
154 return true;
157 template<class T, class S, class P>
158 static bool Dispatch(const Message* msg, T* obj, S* sender, P* parameter,
159 void (T::*func)(P*)) {
160 (obj->*func)(parameter);
161 return true;
164 // Used for async messages with no parameters.
165 static void Log(std::string* name, const Message* msg, std::string* l) {
168 // Find the end of the message data that starts at range_start. Returns NULL
169 // if the entire message is not found in the given data range.
170 static const char* FindNext(const char* range_start, const char* range_end) {
171 return base::Pickle::FindNext(sizeof(Header), range_start, range_end);
174 // WriteAttachment appends |attachment| to the end of the set. It returns
175 // false iff the set is full.
176 bool WriteAttachment(scoped_refptr<MessageAttachment> attachment);
177 // ReadAttachment parses an attachment given the parsing state |iter| and
178 // writes it to |*attachment|. It returns true on success.
179 bool ReadAttachment(base::PickleIterator* iter,
180 scoped_refptr<MessageAttachment>* attachment) const;
181 // Returns true if there are any attachment in this message.
182 bool HasAttachments() const;
183 // Returns true if there are any MojoHandleAttachments in this message.
184 bool HasMojoHandles() const;
185 // Whether the message has any brokerable attachments.
186 bool HasBrokerableAttachments() const;
188 void set_sender_pid(base::ProcessId id) { sender_pid_ = id; }
189 base::ProcessId get_sender_pid() const { return sender_pid_; }
191 #ifdef IPC_MESSAGE_LOG_ENABLED
192 // Adds the outgoing time from Time::Now() at the end of the message and sets
193 // a bit to indicate that it's been added.
194 void set_sent_time(int64 time);
195 int64 sent_time() const;
197 void set_received_time(int64 time) const;
198 int64 received_time() const { return received_time_; }
199 void set_output_params(const std::string& op) const { output_params_ = op; }
200 const std::string& output_params() const { return output_params_; }
201 // The following four functions are needed so we can log sync messages with
202 // delayed replies. We stick the log data from the sent message into the
203 // reply message, so that when it's sent and we have the output parameters
204 // we can log it. As such, we set a flag on the sent message to not log it.
205 void set_sync_log_data(LogData* data) const { log_data_ = data; }
206 LogData* sync_log_data() const { return log_data_; }
207 void set_dont_log() const { dont_log_ = true; }
208 bool dont_log() const { return dont_log_; }
209 #endif
211 protected:
212 friend class Channel;
213 friend class ChannelMojo;
214 friend class ChannelNacl;
215 friend class ChannelPosix;
216 friend class ChannelWin;
217 friend class internal::ChannelReader;
218 friend class MessageReplyDeserializer;
219 friend class SyncMessage;
221 #pragma pack(push, 4)
222 struct Header : base::Pickle::Header {
223 int32 routing; // ID of the view that this message is destined for
224 uint32 type; // specifies the user-defined message type
225 uint32 flags; // specifies control flags for the message
226 #if USE_ATTACHMENT_BROKER
227 // The number of brokered attachments included with this message. The
228 // ids of the brokered attachment ids are sent immediately after the pickled
229 // message, before the next pickled message is sent.
230 uint32 num_brokered_attachments;
231 #endif
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
235 #endif
237 #pragma pack(pop)
239 Header* header() {
240 return headerT<Header>();
242 const Header* header() const {
243 return headerT<Header>();
246 void Init();
248 // Used internally to support IPC::Listener::OnBadMessageReceived.
249 mutable bool dispatch_error_;
251 // The set of file descriptors associated with this message.
252 scoped_refptr<MessageAttachmentSet> attachment_set_;
254 // Ensure that a MessageAttachmentSet is allocated
255 void EnsureMessageAttachmentSet();
257 MessageAttachmentSet* attachment_set() {
258 EnsureMessageAttachmentSet();
259 return attachment_set_.get();
261 const MessageAttachmentSet* attachment_set() const {
262 return attachment_set_.get();
265 // The process id of the sender of the message. This member is populated with
266 // a valid value for every message dispatched to listeners.
267 base::ProcessId sender_pid_;
269 #ifdef IPC_MESSAGE_LOG_ENABLED
270 // Used for logging.
271 mutable int64 received_time_;
272 mutable std::string output_params_;
273 mutable LogData* log_data_;
274 mutable bool dont_log_;
275 #endif
278 //------------------------------------------------------------------------------
280 } // namespace IPC
282 enum SpecialRoutingIDs {
283 // indicates that we don't have a routing ID yet.
284 MSG_ROUTING_NONE = -2,
286 // indicates a general message not sent to a particular tab.
287 MSG_ROUTING_CONTROL = kint32max,
290 #define IPC_REPLY_ID 0xFFFFFFF0 // Special message id for replies
291 #define IPC_LOGGING_ID 0xFFFFFFF1 // Special message id for logging
293 #endif // IPC_IPC_MESSAGE_H_