[osx] Whitelist NSException thrown by Cocoa on Yosemite.
[chromium-blink-merge.git] / ipc / ipc_message.h
blob64d85d4d44c4ed3b294c416d6be4cce8e85f32c9
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 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 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(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;
181 #ifdef IPC_MESSAGE_LOG_ENABLED
182 // Adds the outgoing time from Time::Now() at the end of the message and sets
183 // a bit to indicate that it's been added.
184 void set_sent_time(int64 time);
185 int64 sent_time() const;
187 void set_received_time(int64 time) const;
188 int64 received_time() const { return received_time_; }
189 void set_output_params(const std::string& op) const { output_params_ = op; }
190 const std::string& output_params() const { return output_params_; }
191 // The following four functions are needed so we can log sync messages with
192 // delayed replies. We stick the log data from the sent message into the
193 // reply message, so that when it's sent and we have the output parameters
194 // we can log it. As such, we set a flag on the sent message to not log it.
195 void set_sync_log_data(LogData* data) const { log_data_ = data; }
196 LogData* sync_log_data() const { return log_data_; }
197 void set_dont_log() const { dont_log_ = true; }
198 bool dont_log() const { return dont_log_; }
199 #endif
201 // Called to trace when message is sent.
202 void TraceMessageBegin() {
203 TRACE_EVENT_FLOW_BEGIN0(TRACE_DISABLED_BY_DEFAULT("ipc.flow"), "IPC",
204 header()->flags);
206 // Called to trace when message is received.
207 void TraceMessageEnd() {
208 TRACE_EVENT_FLOW_END0(TRACE_DISABLED_BY_DEFAULT("ipc.flow"), "IPC",
209 header()->flags);
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 MessageReplyDeserializer;
219 friend class SyncMessage;
221 #pragma pack(push, 4)
222 struct Header : 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 defined(OS_POSIX)
227 uint16 num_fds; // the number of descriptors included with this message
228 uint16 pad; // explicitly initialize this to appease valgrind
229 #endif
231 #pragma pack(pop)
233 Header* header() {
234 return headerT<Header>();
236 const Header* header() const {
237 return headerT<Header>();
240 void Init();
242 // Used internally to support IPC::Listener::OnBadMessageReceived.
243 mutable bool dispatch_error_;
245 // The set of file descriptors associated with this message.
246 scoped_refptr<MessageAttachmentSet> attachment_set_;
248 // Ensure that a MessageAttachmentSet is allocated
249 void EnsureMessageAttachmentSet();
251 MessageAttachmentSet* attachment_set() {
252 EnsureMessageAttachmentSet();
253 return attachment_set_.get();
255 const MessageAttachmentSet* attachment_set() const {
256 return attachment_set_.get();
259 #ifdef IPC_MESSAGE_LOG_ENABLED
260 // Used for logging.
261 mutable int64 received_time_;
262 mutable std::string output_params_;
263 mutable LogData* log_data_;
264 mutable bool dont_log_;
265 #endif
268 //------------------------------------------------------------------------------
270 } // namespace IPC
272 enum SpecialRoutingIDs {
273 // indicates that we don't have a routing ID yet.
274 MSG_ROUTING_NONE = -2,
276 // indicates a general message not sent to a particular tab.
277 MSG_ROUTING_CONTROL = kint32max,
280 #define IPC_REPLY_ID 0xFFFFFFF0 // Special message id for replies
281 #define IPC_LOGGING_ID 0xFFFFFFF1 // Special message id for logging
283 #endif // IPC_IPC_MESSAGE_H_