view-manager: Create the root-view only when the viewport-metrics are known.
[chromium-blink-merge.git] / ipc / ipc_message.h
blob74bd74157bedb06f82e4f7150333727eea775238
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/ipc_export.h"
16 #if !defined(NDEBUG)
17 #define IPC_MESSAGE_LOG_ENABLED
18 #endif
20 namespace IPC {
22 //------------------------------------------------------------------------------
24 struct LogData;
25 class MessageAttachment;
26 class MessageAttachmentSet;
28 class IPC_EXPORT Message : public base::Pickle {
29 public:
30 enum PriorityValue {
31 PRIORITY_LOW = 1,
32 PRIORITY_NORMAL,
33 PRIORITY_HIGH
36 // Bit values used in the flags field.
37 // Upper 24 bits of flags store a reference number, so this enum is limited to
38 // 8 bits.
39 enum {
40 PRIORITY_MASK = 0x03, // Low 2 bits of store the priority value.
41 SYNC_BIT = 0x04,
42 REPLY_BIT = 0x08,
43 REPLY_ERROR_BIT = 0x10,
44 UNBLOCK_BIT = 0x20,
45 PUMPING_MSGS_BIT = 0x40,
46 HAS_SENT_TIME_BIT = 0x80,
49 ~Message() override;
51 Message();
53 // Initialize a message with a user-defined type, priority value, and
54 // destination WebView ID.
55 Message(int32 routing_id, uint32 type, PriorityValue priority);
57 // Initializes a message from a const block of data. The data is not copied;
58 // instead the data is merely referenced by this message. Only const methods
59 // should be used on the message when initialized this way.
60 Message(const char* data, int data_len);
62 Message(const Message& other);
63 Message& operator=(const Message& other);
65 PriorityValue priority() const {
66 return static_cast<PriorityValue>(header()->flags & PRIORITY_MASK);
69 // True if this is a synchronous message.
70 void set_sync() {
71 header()->flags |= SYNC_BIT;
73 bool is_sync() const {
74 return (header()->flags & SYNC_BIT) != 0;
77 // Set this on a reply to a synchronous message.
78 void set_reply() {
79 header()->flags |= REPLY_BIT;
82 bool is_reply() const {
83 return (header()->flags & REPLY_BIT) != 0;
86 // Set this on a reply to a synchronous message to indicate that no receiver
87 // was found.
88 void set_reply_error() {
89 header()->flags |= REPLY_ERROR_BIT;
92 bool is_reply_error() const {
93 return (header()->flags & REPLY_ERROR_BIT) != 0;
96 // Normally when a receiver gets a message and they're blocked on a
97 // synchronous message Send, they buffer a message. Setting this flag causes
98 // the receiver to be unblocked and the message to be dispatched immediately.
99 void set_unblock(bool unblock) {
100 if (unblock) {
101 header()->flags |= UNBLOCK_BIT;
102 } else {
103 header()->flags &= ~UNBLOCK_BIT;
107 bool should_unblock() const {
108 return (header()->flags & UNBLOCK_BIT) != 0;
111 // Tells the receiver that the caller is pumping messages while waiting
112 // for the result.
113 bool is_caller_pumping_messages() const {
114 return (header()->flags & PUMPING_MSGS_BIT) != 0;
117 void set_dispatch_error() const {
118 dispatch_error_ = true;
121 bool dispatch_error() const {
122 return dispatch_error_;
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 uint32 flags() const {
138 return header()->flags;
141 // Sets all the given header values. The message should be empty at this
142 // call.
143 void SetHeaderValues(int32 routing, uint32 type, uint32 flags);
145 template<class T, class S, class P>
146 static bool Dispatch(const Message* msg, T* obj, S* sender, P* parameter,
147 void (T::*func)()) {
148 (obj->*func)();
149 return true;
152 template<class T, class S, class P>
153 static bool Dispatch(const Message* msg, T* obj, S* sender, P* parameter,
154 void (T::*func)(P*)) {
155 (obj->*func)(parameter);
156 return true;
159 // Used for async messages with no parameters.
160 static void Log(std::string* name, const Message* msg, std::string* l) {
163 // Find the end of the message data that starts at range_start. Returns NULL
164 // if the entire message is not found in the given data range.
165 static const char* FindNext(const char* range_start, const char* range_end) {
166 return base::Pickle::FindNext(sizeof(Header), range_start, range_end);
169 // WriteAttachment appends |attachment| to the end of the set. It returns
170 // false iff the set is full.
171 bool WriteAttachment(scoped_refptr<MessageAttachment> attachment);
172 // ReadAttachment parses an attachment given the parsing state |iter| and
173 // writes it to |*attachment|. It returns true on success.
174 bool ReadAttachment(base::PickleIterator* iter,
175 scoped_refptr<MessageAttachment>* attachment) const;
176 // Returns true if there are any attachment in this message.
177 bool HasAttachments() const;
178 // Returns true if there are any MojoHandleAttachments in this message.
179 bool HasMojoHandles() const;
180 // Whether the message has any brokerable attachments.
181 bool HasBrokerableAttachments() const;
183 #ifdef IPC_MESSAGE_LOG_ENABLED
184 // Adds the outgoing time from Time::Now() at the end of the message and sets
185 // a bit to indicate that it's been added.
186 void set_sent_time(int64 time);
187 int64 sent_time() const;
189 void set_received_time(int64 time) const;
190 int64 received_time() const { return received_time_; }
191 void set_output_params(const std::string& op) const { output_params_ = op; }
192 const std::string& output_params() const { return output_params_; }
193 // The following four functions are needed so we can log sync messages with
194 // delayed replies. We stick the log data from the sent message into the
195 // reply message, so that when it's sent and we have the output parameters
196 // we can log it. As such, we set a flag on the sent message to not log it.
197 void set_sync_log_data(LogData* data) const { log_data_ = data; }
198 LogData* sync_log_data() const { return log_data_; }
199 void set_dont_log() const { dont_log_ = true; }
200 bool dont_log() const { return dont_log_; }
201 #endif
203 // Called to trace when message is sent.
204 void TraceMessageBegin() {
205 TRACE_EVENT_FLOW_BEGIN0(TRACE_DISABLED_BY_DEFAULT("ipc.flow"), "IPC",
206 header()->flags);
208 // Called to trace when message is received.
209 void TraceMessageEnd() {
210 TRACE_EVENT_FLOW_END_BIND_TO_ENCLOSING0(
211 TRACE_DISABLED_BY_DEFAULT("ipc.flow"), "IPC", header()->flags);
214 protected:
215 friend class Channel;
216 friend class ChannelMojo;
217 friend class ChannelNacl;
218 friend class ChannelPosix;
219 friend class ChannelWin;
220 friend class MessageReplyDeserializer;
221 friend class SyncMessage;
223 #pragma pack(push, 4)
224 struct Header : base::Pickle::Header {
225 int32 routing; // ID of the view that this message is destined for
226 uint32 type; // specifies the user-defined message type
227 uint32 flags; // specifies control flags for the message
228 #if defined(OS_POSIX)
229 uint16 num_fds; // the number of descriptors included with this message
230 uint16 pad; // explicitly initialize this to appease valgrind
231 #endif
233 #pragma pack(pop)
235 Header* header() {
236 return headerT<Header>();
238 const Header* header() const {
239 return headerT<Header>();
242 void Init();
244 // Used internally to support IPC::Listener::OnBadMessageReceived.
245 mutable bool dispatch_error_;
247 // The set of file descriptors associated with this message.
248 scoped_refptr<MessageAttachmentSet> attachment_set_;
250 // Ensure that a MessageAttachmentSet is allocated
251 void EnsureMessageAttachmentSet();
253 MessageAttachmentSet* attachment_set() {
254 EnsureMessageAttachmentSet();
255 return attachment_set_.get();
257 const MessageAttachmentSet* attachment_set() const {
258 return attachment_set_.get();
261 #ifdef IPC_MESSAGE_LOG_ENABLED
262 // Used for logging.
263 mutable int64 received_time_;
264 mutable std::string output_params_;
265 mutable LogData* log_data_;
266 mutable bool dont_log_;
267 #endif
270 //------------------------------------------------------------------------------
272 } // namespace IPC
274 enum SpecialRoutingIDs {
275 // indicates that we don't have a routing ID yet.
276 MSG_ROUTING_NONE = -2,
278 // indicates a general message not sent to a particular tab.
279 MSG_ROUTING_CONTROL = kint32max,
282 #define IPC_REPLY_ID 0xFFFFFFF0 // Special message id for replies
283 #define IPC_LOGGING_ID 0xFFFFFFF1 // Special message id for logging
285 #endif // IPC_IPC_MESSAGE_H_