Add scoped_ptr-safe base::Value to Dictionary/List conversion functions.
[chromium-blink-merge.git] / ipc / ipc_message.h
blobcc7ebb31a988d77cd162f3d55a1f36020f36c6cf
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 <stdint.h>
10 #include <string>
12 #include "base/basictypes.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/pickle.h"
15 #include "base/trace_event/trace_event.h"
16 #include "ipc/ipc_export.h"
18 #if !defined(NDEBUG)
19 #define IPC_MESSAGE_LOG_ENABLED
20 #endif
22 namespace IPC {
24 namespace internal {
25 class ChannelReader;
26 } // namespace internal
28 //------------------------------------------------------------------------------
30 struct LogData;
31 class MessageAttachment;
32 class MessageAttachmentSet;
34 class IPC_EXPORT Message : public base::Pickle {
35 public:
36 enum PriorityValue {
37 PRIORITY_LOW = 1,
38 PRIORITY_NORMAL,
39 PRIORITY_HIGH
42 // Bit values used in the flags field.
43 // Upper 24 bits of flags store a reference number, so this enum is limited to
44 // 8 bits.
45 enum {
46 PRIORITY_MASK = 0x03, // Low 2 bits of store the priority value.
47 SYNC_BIT = 0x04,
48 REPLY_BIT = 0x08,
49 REPLY_ERROR_BIT = 0x10,
50 UNBLOCK_BIT = 0x20,
51 PUMPING_MSGS_BIT = 0x40,
52 HAS_SENT_TIME_BIT = 0x80,
55 ~Message() override;
57 Message();
59 // Initialize a message with a user-defined type, priority value, and
60 // destination WebView ID.
61 Message(int32_t routing_id, uint32_t type, PriorityValue priority);
63 // Initializes a message from a const block of data. The data is not copied;
64 // instead the data is merely referenced by this message. Only const methods
65 // should be used on the message when initialized this way.
66 Message(const char* data, int data_len);
68 Message(const Message& other);
69 Message& operator=(const Message& other);
71 PriorityValue priority() const {
72 return static_cast<PriorityValue>(header()->flags & PRIORITY_MASK);
75 // True if this is a synchronous message.
76 void set_sync() {
77 header()->flags |= SYNC_BIT;
79 bool is_sync() const {
80 return (header()->flags & SYNC_BIT) != 0;
83 // Set this on a reply to a synchronous message.
84 void set_reply() {
85 header()->flags |= REPLY_BIT;
88 bool is_reply() const {
89 return (header()->flags & REPLY_BIT) != 0;
92 // Set this on a reply to a synchronous message to indicate that no receiver
93 // was found.
94 void set_reply_error() {
95 header()->flags |= REPLY_ERROR_BIT;
98 bool is_reply_error() const {
99 return (header()->flags & REPLY_ERROR_BIT) != 0;
102 // Normally when a receiver gets a message and they're blocked on a
103 // synchronous message Send, they buffer a message. Setting this flag causes
104 // the receiver to be unblocked and the message to be dispatched immediately.
105 void set_unblock(bool unblock) {
106 if (unblock) {
107 header()->flags |= UNBLOCK_BIT;
108 } else {
109 header()->flags &= ~UNBLOCK_BIT;
113 bool should_unblock() const {
114 return (header()->flags & UNBLOCK_BIT) != 0;
117 // Tells the receiver that the caller is pumping messages while waiting
118 // for the result.
119 bool is_caller_pumping_messages() const {
120 return (header()->flags & PUMPING_MSGS_BIT) != 0;
123 void set_dispatch_error() const {
124 dispatch_error_ = true;
127 bool dispatch_error() const {
128 return dispatch_error_;
131 uint32_t type() const {
132 return header()->type;
135 int32_t routing_id() const {
136 return header()->routing;
139 void set_routing_id(int32_t new_id) {
140 header()->routing = new_id;
143 uint32_t flags() const {
144 return header()->flags;
147 // Sets all the given header values. The message should be empty at this
148 // call.
149 void SetHeaderValues(int32_t routing, uint32_t type, uint32_t flags);
151 template<class T, class S, class P>
152 static bool Dispatch(const Message* msg, T* obj, S* sender, P* parameter,
153 void (T::*func)()) {
154 (obj->*func)();
155 return true;
158 template<class T, class S, class P>
159 static bool Dispatch(const Message* msg, T* obj, S* sender, P* parameter,
160 void (T::*func)(P*)) {
161 (obj->*func)(parameter);
162 return true;
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 base::Pickle::FindNext(sizeof(Header), range_start, range_end);
175 // WriteAttachment appends |attachment| to the end of the set. It returns
176 // false iff the set is full.
177 bool WriteAttachment(scoped_refptr<MessageAttachment> attachment);
178 // ReadAttachment parses an attachment given the parsing state |iter| and
179 // writes it to |*attachment|. It returns true on success.
180 bool ReadAttachment(base::PickleIterator* iter,
181 scoped_refptr<MessageAttachment>* attachment) const;
182 // Returns true if there are any attachment in this message.
183 bool HasAttachments() const;
184 // Returns true if there are any MojoHandleAttachments in this message.
185 bool HasMojoHandles() const;
186 // Whether the message has any brokerable attachments.
187 bool HasBrokerableAttachments() const;
189 void set_sender_pid(base::ProcessId id) { sender_pid_ = id; }
190 base::ProcessId get_sender_pid() const { return sender_pid_; }
192 #ifdef IPC_MESSAGE_LOG_ENABLED
193 // Adds the outgoing time from Time::Now() at the end of the message and sets
194 // a bit to indicate that it's been added.
195 void set_sent_time(int64_t time);
196 int64_t sent_time() const;
198 void set_received_time(int64_t time) const;
199 int64_t received_time() const { return received_time_; }
200 void set_output_params(const std::string& op) const { output_params_ = op; }
201 const std::string& output_params() const { return output_params_; }
202 // The following four functions are needed so we can log sync messages with
203 // delayed replies. We stick the log data from the sent message into the
204 // reply message, so that when it's sent and we have the output parameters
205 // we can log it. As such, we set a flag on the sent message to not log it.
206 void set_sync_log_data(LogData* data) const { log_data_ = data; }
207 LogData* sync_log_data() const { return log_data_; }
208 void set_dont_log() const { dont_log_ = true; }
209 bool dont_log() const { return dont_log_; }
210 #endif
212 protected:
213 friend class Channel;
214 friend class ChannelMojo;
215 friend class ChannelNacl;
216 friend class ChannelPosix;
217 friend class ChannelWin;
218 friend class internal::ChannelReader;
219 friend class MessageReplyDeserializer;
220 friend class SyncMessage;
222 #pragma pack(push, 4)
223 struct Header : base::Pickle::Header {
224 int32_t routing; // ID of the view that this message is destined for
225 uint32_t type; // specifies the user-defined message type
226 uint32_t flags; // specifies control flags for the message
227 #if defined(OS_POSIX)
228 uint16_t num_fds; // the number of descriptors included with this message
229 uint16_t pad; // explicitly initialize this to appease valgrind
230 #endif
232 #pragma pack(pop)
234 Header* header() {
235 return headerT<Header>();
237 const Header* header() const {
238 return headerT<Header>();
241 void Init();
243 // Used internally to support IPC::Listener::OnBadMessageReceived.
244 mutable bool dispatch_error_;
246 // The set of file descriptors associated with this message.
247 scoped_refptr<MessageAttachmentSet> attachment_set_;
249 // Ensure that a MessageAttachmentSet is allocated
250 void EnsureMessageAttachmentSet();
252 MessageAttachmentSet* attachment_set() {
253 EnsureMessageAttachmentSet();
254 return attachment_set_.get();
256 const MessageAttachmentSet* attachment_set() const {
257 return attachment_set_.get();
260 // The process id of the sender of the message. This member is populated with
261 // a valid value for every message dispatched to listeners.
262 base::ProcessId sender_pid_;
264 #ifdef IPC_MESSAGE_LOG_ENABLED
265 // Used for logging.
266 mutable int64_t received_time_;
267 mutable std::string output_params_;
268 mutable LogData* log_data_;
269 mutable bool dont_log_;
270 #endif
273 //------------------------------------------------------------------------------
275 } // namespace IPC
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_