Mark AwContentsClientFullScreenTest#testPowerSaveBlockerIsTransferredToFullscreen...
[chromium-blink-merge.git] / ipc / ipc_message.h
blob09d335cb0b1582289ce5467dd9ba8c8b654c45dc
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/files/file.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 #if defined(OS_POSIX)
21 #include "base/memory/ref_counted.h"
22 #endif
24 namespace IPC {
26 //------------------------------------------------------------------------------
28 struct LogData;
29 class MessageAttachmentSet;
31 class IPC_EXPORT Message : public Pickle {
32 public:
33 enum PriorityValue {
34 PRIORITY_LOW = 1,
35 PRIORITY_NORMAL,
36 PRIORITY_HIGH
39 // Bit values used in the flags field.
40 // Upper 24 bits of flags store a reference number, so this enum is limited to
41 // 8 bits.
42 enum {
43 PRIORITY_MASK = 0x03, // Low 2 bits of store the priority value.
44 SYNC_BIT = 0x04,
45 REPLY_BIT = 0x08,
46 REPLY_ERROR_BIT = 0x10,
47 UNBLOCK_BIT = 0x20,
48 PUMPING_MSGS_BIT = 0x40,
49 HAS_SENT_TIME_BIT = 0x80,
52 ~Message() override;
54 Message();
56 // Initialize a message with a user-defined type, priority value, and
57 // destination WebView ID.
58 Message(int32 routing_id, uint32 type, PriorityValue priority);
60 // Initializes a message from a const block of data. The data is not copied;
61 // instead the data is merely referenced by this message. Only const methods
62 // should be used on the message when initialized this way.
63 Message(const char* data, int data_len);
65 Message(const Message& other);
66 Message& operator=(const Message& other);
68 PriorityValue priority() const {
69 return static_cast<PriorityValue>(header()->flags & PRIORITY_MASK);
72 // True if this is a synchronous message.
73 void set_sync() {
74 header()->flags |= SYNC_BIT;
76 bool is_sync() const {
77 return (header()->flags & SYNC_BIT) != 0;
80 // Set this on a reply to a synchronous message.
81 void set_reply() {
82 header()->flags |= REPLY_BIT;
85 bool is_reply() const {
86 return (header()->flags & REPLY_BIT) != 0;
89 // Set this on a reply to a synchronous message to indicate that no receiver
90 // was found.
91 void set_reply_error() {
92 header()->flags |= REPLY_ERROR_BIT;
95 bool is_reply_error() const {
96 return (header()->flags & REPLY_ERROR_BIT) != 0;
99 // Normally when a receiver gets a message and they're blocked on a
100 // synchronous message Send, they buffer a message. Setting this flag causes
101 // the receiver to be unblocked and the message to be dispatched immediately.
102 void set_unblock(bool unblock) {
103 if (unblock) {
104 header()->flags |= UNBLOCK_BIT;
105 } else {
106 header()->flags &= ~UNBLOCK_BIT;
110 bool should_unblock() const {
111 return (header()->flags & UNBLOCK_BIT) != 0;
114 // Tells the receiver that the caller is pumping messages while waiting
115 // for the result.
116 bool is_caller_pumping_messages() const {
117 return (header()->flags & PUMPING_MSGS_BIT) != 0;
120 void set_dispatch_error() const {
121 dispatch_error_ = true;
124 bool dispatch_error() const {
125 return dispatch_error_;
128 uint32 type() const {
129 return header()->type;
132 int32 routing_id() const {
133 return header()->routing;
136 void set_routing_id(int32 new_id) {
137 header()->routing = new_id;
140 uint32 flags() const {
141 return header()->flags;
144 // Sets all the given header values. The message should be empty at this
145 // call.
146 void SetHeaderValues(int32 routing, uint32 type, uint32 flags);
148 template<class T, class S, class P>
149 static bool Dispatch(const Message* msg, T* obj, S* sender, P* parameter,
150 void (T::*func)()) {
151 (obj->*func)();
152 return true;
155 template<class T, class S, class P>
156 static bool Dispatch(const Message* msg, T* obj, S* sender, P* parameter,
157 void (T::*func)(P*)) {
158 (obj->*func)(parameter);
159 return true;
162 // Used for async messages with no parameters.
163 static void Log(std::string* name, const Message* msg, std::string* l) {
166 // Find the end of the message data that starts at range_start. Returns NULL
167 // if the entire message is not found in the given data range.
168 static const char* FindNext(const char* range_start, const char* range_end) {
169 return Pickle::FindNext(sizeof(Header), range_start, range_end);
172 #if defined(OS_POSIX)
173 // On POSIX, a message supports reading / writing FileDescriptor objects.
174 // This is used to pass a file descriptor to the peer of an IPC channel.
176 // Add a descriptor to the end of the set. Returns false if the set is full.
177 bool WriteFile(base::ScopedFD descriptor);
178 bool WriteBorrowingFile(const base::PlatformFile& descriptor);
180 // Get a file descriptor from the message. Returns false on error.
181 // iter: a Pickle iterator to the current location in the message.
182 bool ReadFile(PickleIterator* iter, base::ScopedFD* file) const;
184 // Returns true if there are any file descriptors in this message.
185 bool HasFileDescriptors() const;
186 #endif
188 #ifdef IPC_MESSAGE_LOG_ENABLED
189 // Adds the outgoing time from Time::Now() at the end of the message and sets
190 // a bit to indicate that it's been added.
191 void set_sent_time(int64 time);
192 int64 sent_time() const;
194 void set_received_time(int64 time) const;
195 int64 received_time() const { return received_time_; }
196 void set_output_params(const std::string& op) const { output_params_ = op; }
197 const std::string& output_params() const { return output_params_; }
198 // The following four functions are needed so we can log sync messages with
199 // delayed replies. We stick the log data from the sent message into the
200 // reply message, so that when it's sent and we have the output parameters
201 // we can log it. As such, we set a flag on the sent message to not log it.
202 void set_sync_log_data(LogData* data) const { log_data_ = data; }
203 LogData* sync_log_data() const { return log_data_; }
204 void set_dont_log() const { dont_log_ = true; }
205 bool dont_log() const { return dont_log_; }
206 #endif
208 // Called to trace when message is sent.
209 void TraceMessageBegin() {
210 TRACE_EVENT_FLOW_BEGIN0(TRACE_DISABLED_BY_DEFAULT("ipc.flow"), "IPC",
211 header()->flags);
213 // Called to trace when message is received.
214 void TraceMessageEnd() {
215 TRACE_EVENT_FLOW_END0(TRACE_DISABLED_BY_DEFAULT("ipc.flow"), "IPC",
216 header()->flags);
219 protected:
220 friend class Channel;
221 friend class ChannelMojo;
222 friend class ChannelNacl;
223 friend class ChannelPosix;
224 friend class ChannelWin;
225 friend class MessageReplyDeserializer;
226 friend class SyncMessage;
228 #pragma pack(push, 4)
229 struct Header : Pickle::Header {
230 int32 routing; // ID of the view that this message is destined for
231 uint32 type; // specifies the user-defined message type
232 uint32 flags; // specifies control flags for the message
233 #if defined(OS_POSIX)
234 uint16 num_fds; // the number of descriptors included with this message
235 uint16 pad; // explicitly initialize this to appease valgrind
236 #endif
238 #pragma pack(pop)
240 Header* header() {
241 return headerT<Header>();
243 const Header* header() const {
244 return headerT<Header>();
247 void Init();
249 // Used internally to support IPC::Listener::OnBadMessageReceived.
250 mutable bool dispatch_error_;
252 // The set of file descriptors associated with this message.
253 scoped_refptr<MessageAttachmentSet> attachment_set_;
255 // Ensure that a MessageAttachmentSet is allocated
256 void EnsureMessageAttachmentSet();
258 MessageAttachmentSet* attachment_set() {
259 EnsureMessageAttachmentSet();
260 return attachment_set_.get();
262 const MessageAttachmentSet* attachment_set() const {
263 return attachment_set_.get();
266 #ifdef IPC_MESSAGE_LOG_ENABLED
267 // Used for logging.
268 mutable int64 received_time_;
269 mutable std::string output_params_;
270 mutable LogData* log_data_;
271 mutable bool dont_log_;
272 #endif
275 //------------------------------------------------------------------------------
277 } // namespace IPC
279 enum SpecialRoutingIDs {
280 // indicates that we don't have a routing ID yet.
281 MSG_ROUTING_NONE = -2,
283 // indicates a general message not sent to a particular tab.
284 MSG_ROUTING_CONTROL = kint32max,
287 #define IPC_REPLY_ID 0xFFFFFFF0 // Special message id for replies
288 #define IPC_LOGGING_ID 0xFFFFFFF1 // Special message id for logging
290 #endif // IPC_IPC_MESSAGE_H_