Add ICU message format support
[chromium-blink-merge.git] / ipc / ipc_message.h
blob73a5165aed219c70cadb7428f19fb1315db7e883
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 namespace internal {
23 class ChannelReader;
24 } // namespace internal
26 //------------------------------------------------------------------------------
28 struct LogData;
29 class MessageAttachment;
30 class MessageAttachmentSet;
32 class IPC_EXPORT Message : public base::Pickle {
33 public:
34 enum PriorityValue {
35 PRIORITY_LOW = 1,
36 PRIORITY_NORMAL,
37 PRIORITY_HIGH
40 // Bit values used in the flags field.
41 // Upper 24 bits of flags store a reference number, so this enum is limited to
42 // 8 bits.
43 enum {
44 PRIORITY_MASK = 0x03, // Low 2 bits of store the priority value.
45 SYNC_BIT = 0x04,
46 REPLY_BIT = 0x08,
47 REPLY_ERROR_BIT = 0x10,
48 UNBLOCK_BIT = 0x20,
49 PUMPING_MSGS_BIT = 0x40,
50 HAS_SENT_TIME_BIT = 0x80,
53 ~Message() override;
55 Message();
57 // Initialize a message with a user-defined type, priority value, and
58 // destination WebView ID.
59 Message(int32 routing_id, uint32 type, PriorityValue priority);
61 // Initializes a message from a const block of data. The data is not copied;
62 // instead the data is merely referenced by this message. Only const methods
63 // should be used on the message when initialized this way.
64 Message(const char* data, int data_len);
66 Message(const Message& other);
67 Message& operator=(const Message& other);
69 PriorityValue priority() const {
70 return static_cast<PriorityValue>(header()->flags & PRIORITY_MASK);
73 // True if this is a synchronous message.
74 void set_sync() {
75 header()->flags |= SYNC_BIT;
77 bool is_sync() const {
78 return (header()->flags & SYNC_BIT) != 0;
81 // Set this on a reply to a synchronous message.
82 void set_reply() {
83 header()->flags |= REPLY_BIT;
86 bool is_reply() const {
87 return (header()->flags & REPLY_BIT) != 0;
90 // Set this on a reply to a synchronous message to indicate that no receiver
91 // was found.
92 void set_reply_error() {
93 header()->flags |= REPLY_ERROR_BIT;
96 bool is_reply_error() const {
97 return (header()->flags & REPLY_ERROR_BIT) != 0;
100 // Normally when a receiver gets a message and they're blocked on a
101 // synchronous message Send, they buffer a message. Setting this flag causes
102 // the receiver to be unblocked and the message to be dispatched immediately.
103 void set_unblock(bool unblock) {
104 if (unblock) {
105 header()->flags |= UNBLOCK_BIT;
106 } else {
107 header()->flags &= ~UNBLOCK_BIT;
111 bool should_unblock() const {
112 return (header()->flags & UNBLOCK_BIT) != 0;
115 // Tells the receiver that the caller is pumping messages while waiting
116 // for the result.
117 bool is_caller_pumping_messages() const {
118 return (header()->flags & PUMPING_MSGS_BIT) != 0;
121 void set_dispatch_error() const {
122 dispatch_error_ = true;
125 bool dispatch_error() const {
126 return dispatch_error_;
129 uint32 type() const {
130 return header()->type;
133 int32 routing_id() const {
134 return header()->routing;
137 void set_routing_id(int32 new_id) {
138 header()->routing = new_id;
141 uint32 flags() const {
142 return header()->flags;
145 // Sets all the given header values. The message should be empty at this
146 // call.
147 void SetHeaderValues(int32 routing, uint32 type, uint32 flags);
149 template<class T, class S, class P>
150 static bool Dispatch(const Message* msg, T* obj, S* sender, P* parameter,
151 void (T::*func)()) {
152 (obj->*func)();
153 return true;
156 template<class T, class S, class P>
157 static bool Dispatch(const Message* msg, T* obj, S* sender, P* parameter,
158 void (T::*func)(P*)) {
159 (obj->*func)(parameter);
160 return true;
163 // Used for async messages with no parameters.
164 static void Log(std::string* name, const Message* msg, std::string* l) {
167 // Find the end of the message data that starts at range_start. Returns NULL
168 // if the entire message is not found in the given data range.
169 static const char* FindNext(const char* range_start, const char* range_end) {
170 return base::Pickle::FindNext(sizeof(Header), range_start, range_end);
173 // WriteAttachment appends |attachment| to the end of the set. It returns
174 // false iff the set is full.
175 bool WriteAttachment(scoped_refptr<MessageAttachment> attachment);
176 // ReadAttachment parses an attachment given the parsing state |iter| and
177 // writes it to |*attachment|. It returns true on success.
178 bool ReadAttachment(base::PickleIterator* iter,
179 scoped_refptr<MessageAttachment>* attachment) const;
180 // Returns true if there are any attachment in this message.
181 bool HasAttachments() const;
182 // Returns true if there are any MojoHandleAttachments in this message.
183 bool HasMojoHandles() const;
184 // Whether the message has any brokerable attachments.
185 bool HasBrokerableAttachments() const;
187 void set_sender_pid(base::ProcessId id) { sender_pid_ = id; }
188 base::ProcessId get_sender_pid() const { return sender_pid_; }
190 #ifdef IPC_MESSAGE_LOG_ENABLED
191 // Adds the outgoing time from Time::Now() at the end of the message and sets
192 // a bit to indicate that it's been added.
193 void set_sent_time(int64 time);
194 int64 sent_time() const;
196 void set_received_time(int64 time) const;
197 int64 received_time() const { return received_time_; }
198 void set_output_params(const std::string& op) const { output_params_ = op; }
199 const std::string& output_params() const { return output_params_; }
200 // The following four functions are needed so we can log sync messages with
201 // delayed replies. We stick the log data from the sent message into the
202 // reply message, so that when it's sent and we have the output parameters
203 // we can log it. As such, we set a flag on the sent message to not log it.
204 void set_sync_log_data(LogData* data) const { log_data_ = data; }
205 LogData* sync_log_data() const { return log_data_; }
206 void set_dont_log() const { dont_log_ = true; }
207 bool dont_log() const { return dont_log_; }
208 #endif
210 // Called to trace when message is sent.
211 void TraceMessageBegin() {
212 TRACE_EVENT_FLOW_BEGIN0(TRACE_DISABLED_BY_DEFAULT("ipc.flow"), "IPC",
213 header()->flags);
215 // Called to trace when message is received.
216 void TraceMessageEnd() {
217 TRACE_EVENT_FLOW_END_BIND_TO_ENCLOSING0(
218 TRACE_DISABLED_BY_DEFAULT("ipc.flow"), "IPC", header()->flags);
221 protected:
222 friend class Channel;
223 friend class ChannelMojo;
224 friend class ChannelNacl;
225 friend class ChannelPosix;
226 friend class ChannelWin;
227 friend class internal::ChannelReader;
228 friend class MessageReplyDeserializer;
229 friend class SyncMessage;
231 #pragma pack(push, 4)
232 struct Header : base::Pickle::Header {
233 int32 routing; // ID of the view that this message is destined for
234 uint32 type; // specifies the user-defined message type
235 uint32 flags; // specifies control flags for the message
236 #if defined(OS_POSIX)
237 uint16 num_fds; // the number of descriptors included with this message
238 uint16 pad; // explicitly initialize this to appease valgrind
239 #endif
241 #pragma pack(pop)
243 Header* header() {
244 return headerT<Header>();
246 const Header* header() const {
247 return headerT<Header>();
250 void Init();
252 // Used internally to support IPC::Listener::OnBadMessageReceived.
253 mutable bool dispatch_error_;
255 // The set of file descriptors associated with this message.
256 scoped_refptr<MessageAttachmentSet> attachment_set_;
258 // Ensure that a MessageAttachmentSet is allocated
259 void EnsureMessageAttachmentSet();
261 MessageAttachmentSet* attachment_set() {
262 EnsureMessageAttachmentSet();
263 return attachment_set_.get();
265 const MessageAttachmentSet* attachment_set() const {
266 return attachment_set_.get();
269 // The process id of the sender of the message. This member is populated with
270 // a valid value for every message dispatched to listeners.
271 base::ProcessId sender_pid_;
273 #ifdef IPC_MESSAGE_LOG_ENABLED
274 // Used for logging.
275 mutable int64 received_time_;
276 mutable std::string output_params_;
277 mutable LogData* log_data_;
278 mutable bool dont_log_;
279 #endif
282 //------------------------------------------------------------------------------
284 } // namespace IPC
286 enum SpecialRoutingIDs {
287 // indicates that we don't have a routing ID yet.
288 MSG_ROUTING_NONE = -2,
290 // indicates a general message not sent to a particular tab.
291 MSG_ROUTING_CONTROL = kint32max,
294 #define IPC_REPLY_ID 0xFFFFFFF0 // Special message id for replies
295 #define IPC_LOGGING_ID 0xFFFFFFF1 // Special message id for logging
297 #endif // IPC_IPC_MESSAGE_H_