• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #ifndef IPC_IPC_MESSAGE_H_
6 #define IPC_IPC_MESSAGE_H_
7 
8 #include <stddef.h>
9 #include <stdint.h>
10 
11 #include <string>
12 
13 #include "base/gtest_prod_util.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/pickle.h"
16 #include "base/trace_event/trace_event.h"
17 #include "build/build_config.h"
18 #include "ipc/ipc_buildflags.h"
19 #include "ipc/ipc_message_support_export.h"
20 
21 namespace mojo {
22 namespace internal {
23 struct UnmappedNativeStructSerializerImpl;
24 }
25 }  // namespace mojo
26 
27 namespace IPC {
28 
29 namespace internal {
30 class ChannelReader;
31 }  // namespace internal
32 
33 //------------------------------------------------------------------------------
34 
35 struct LogData;
36 class MessageAttachmentSet;
37 
38 class IPC_MESSAGE_SUPPORT_EXPORT Message : public base::Pickle {
39  public:
40   enum PriorityValue {
41     PRIORITY_LOW = 1,
42     PRIORITY_NORMAL,
43     PRIORITY_HIGH
44   };
45 
46   // Bit values used in the flags field.
47   // Upper 24 bits of flags store a reference number, so this enum is limited to
48   // 8 bits.
49   enum {
50     PRIORITY_MASK     = 0x03,  // Low 2 bits of store the priority value.
51     SYNC_BIT          = 0x04,
52     REPLY_BIT         = 0x08,
53     REPLY_ERROR_BIT   = 0x10,
54     UNBLOCK_BIT       = 0x20,
55     PUMPING_MSGS_BIT  = 0x40,
56     HAS_SENT_TIME_BIT = 0x80,
57   };
58 
59   ~Message() override;
60 
61   Message();
62 
63   // Initialize a message with a user-defined type, priority value, and
64   // destination WebView ID.
65   Message(int32_t routing_id, uint32_t type, PriorityValue priority);
66 
67   // Initializes a message from a const block of data.  The data is not copied;
68   // instead the data is merely referenced by this message.  Only const methods
69   // should be used on the message when initialized this way.
70   Message(const char* data, int data_len);
71 
72   Message(const Message& other);
73   Message& operator=(const Message& other);
74 
IsValid()75   bool IsValid() const { return header_size() == sizeof(Header) && header(); }
76 
priority()77   PriorityValue priority() const {
78     return static_cast<PriorityValue>(header()->flags & PRIORITY_MASK);
79   }
80 
81   // True if this is a synchronous message.
set_sync()82   void set_sync() {
83     header()->flags |= SYNC_BIT;
84   }
is_sync()85   bool is_sync() const {
86     return (header()->flags & SYNC_BIT) != 0;
87   }
88 
89   // Set this on a reply to a synchronous message.
set_reply()90   void set_reply() {
91     header()->flags |= REPLY_BIT;
92   }
93 
is_reply()94   bool is_reply() const {
95     return (header()->flags & REPLY_BIT) != 0;
96   }
97 
98   // Set this on a reply to a synchronous message to indicate that no receiver
99   // was found.
set_reply_error()100   void set_reply_error() {
101     header()->flags |= REPLY_ERROR_BIT;
102   }
103 
is_reply_error()104   bool is_reply_error() const {
105     return (header()->flags & REPLY_ERROR_BIT) != 0;
106   }
107 
108   // Normally when a receiver gets a message and they're blocked on a
109   // synchronous message Send, they buffer a message.  Setting this flag causes
110   // the receiver to be unblocked and the message to be dispatched immediately.
set_unblock(bool unblock)111   void set_unblock(bool unblock) {
112     if (unblock) {
113       header()->flags |= UNBLOCK_BIT;
114     } else {
115       header()->flags &= ~UNBLOCK_BIT;
116     }
117   }
118 
should_unblock()119   bool should_unblock() const {
120     return (header()->flags & UNBLOCK_BIT) != 0;
121   }
122 
123   // Tells the receiver that the caller is pumping messages while waiting
124   // for the result.
is_caller_pumping_messages()125   bool is_caller_pumping_messages() const {
126     return (header()->flags & PUMPING_MSGS_BIT) != 0;
127   }
128 
set_dispatch_error()129   void set_dispatch_error() const {
130     dispatch_error_ = true;
131   }
132 
dispatch_error()133   bool dispatch_error() const {
134     return dispatch_error_;
135   }
136 
type()137   uint32_t type() const {
138     return header()->type;
139   }
140 
routing_id()141   int32_t routing_id() const {
142     return header()->routing;
143   }
144 
set_routing_id(int32_t new_id)145   void set_routing_id(int32_t new_id) {
146     header()->routing = new_id;
147   }
148 
flags()149   uint32_t flags() const {
150     return header()->flags;
151   }
152 
153   // Sets all the given header values. The message should be empty at this
154   // call.
155   void SetHeaderValues(int32_t routing, uint32_t type, uint32_t flags);
156 
157   template<class T, class S, class P>
Dispatch(const Message * msg,T * obj,S * sender,P * parameter,void (T::* func)())158   static bool Dispatch(const Message* msg, T* obj, S* sender, P* parameter,
159                        void (T::*func)()) {
160     (obj->*func)();
161     return true;
162   }
163 
164   template<class T, class S, class P>
Dispatch(const Message * msg,T * obj,S * sender,P * parameter,void (T::* func)(P *))165   static bool Dispatch(const Message* msg, T* obj, S* sender, P* parameter,
166                        void (T::*func)(P*)) {
167     (obj->*func)(parameter);
168     return true;
169   }
170 
171   // Used for async messages with no parameters.
Log(std::string * name,const Message * msg,std::string * l)172   static void Log(std::string* name, const Message* msg, std::string* l) {
173   }
174 
175   // The static method FindNext() returns several pieces of information, which
176   // are aggregated into an instance of this struct.
177   struct IPC_MESSAGE_SUPPORT_EXPORT NextMessageInfo {
178     NextMessageInfo();
179     ~NextMessageInfo();
180 
181     // Total message size. Always valid if |message_found| is true.
182     // If |message_found| is false but we could determine message size
183     // from the header, this field is non-zero. Otherwise it's zero.
184     size_t message_size;
185     // Whether an entire message was found in the given memory range.
186     bool message_found;
187     // Only filled in if |message_found| is true.
188     // The start address is passed into FindNext() by the caller, so isn't
189     // repeated in this struct. The end address of the pickle should be used to
190     // construct a base::Pickle.
191     const char* pickle_end;
192     // Only filled in if |message_found| is true.
193     // The end address of the message should be used to determine the start
194     // address of the next message.
195     const char* message_end;
196   };
197 
198   // |info| is an output parameter and must not be nullptr.
199   static void FindNext(const char* range_start,
200                        const char* range_end,
201                        NextMessageInfo* info);
202 
203   // WriteAttachment appends |attachment| to the end of the set. It returns
204   // false iff the set is full.
205   bool WriteAttachment(
206       scoped_refptr<base::Pickle::Attachment> attachment) override;
207   // ReadAttachment parses an attachment given the parsing state |iter| and
208   // writes it to |*attachment|. It returns true on success.
209   bool ReadAttachment(
210       base::PickleIterator* iter,
211       scoped_refptr<base::Pickle::Attachment>* attachment) const override;
212   // Returns true if there are any attachment in this message.
213   bool HasAttachments() const override;
214 
215 #if BUILDFLAG(IPC_MESSAGE_LOG_ENABLED)
216   // Adds the outgoing time from Time::Now() at the end of the message and sets
217   // a bit to indicate that it's been added.
218   void set_sent_time(int64_t time);
219   int64_t sent_time() const;
220 
221   void set_received_time(int64_t time) const;
received_time()222   int64_t received_time() const { return received_time_; }
set_output_params(const std::string & op)223   void set_output_params(const std::string& op) const { output_params_ = op; }
output_params()224   const std::string& output_params() const { return output_params_; }
225   // The following four functions are needed so we can log sync messages with
226   // delayed replies.  We stick the log data from the sent message into the
227   // reply message, so that when it's sent and we have the output parameters
228   // we can log it.  As such, we set a flag on the sent message to not log it.
set_sync_log_data(LogData * data)229   void set_sync_log_data(LogData* data) const { log_data_ = data; }
sync_log_data()230   LogData* sync_log_data() const { return log_data_; }
set_dont_log()231   void set_dont_log() const { dont_log_ = true; }
dont_log()232   bool dont_log() const { return dont_log_; }
233 #endif
234 
235  protected:
236   friend class Channel;
237   friend class ChannelMojo;
238   friend class ChannelNacl;
239   friend class ChannelPosix;
240   friend class ChannelWin;
241   friend class internal::ChannelReader;
242   friend class MessageReplyDeserializer;
243   friend class SyncMessage;
244 
245   friend struct mojo::internal::UnmappedNativeStructSerializerImpl;
246 
247 #pragma pack(push, 4)
248   struct Header : base::Pickle::Header {
249     int32_t routing;  // ID of the view that this message is destined for
250     uint32_t type;    // specifies the user-defined message type
251     uint32_t flags;   // specifies control flags for the message
252 #if defined(OS_POSIX) || defined(OS_FUCHSIA)
253     uint16_t num_fds; // the number of descriptors included with this message
254     uint16_t pad;     // explicitly initialize this to appease valgrind
255 #endif
256   };
257 #pragma pack(pop)
258 
header()259   Header* header() {
260     return headerT<Header>();
261   }
header()262   const Header* header() const {
263     return headerT<Header>();
264   }
265 
266   void Init();
267 
268   // Used internally to support IPC::Listener::OnBadMessageReceived.
269   mutable bool dispatch_error_;
270 
271   // The set of file descriptors associated with this message.
272   scoped_refptr<MessageAttachmentSet> attachment_set_;
273 
274   // Ensure that a MessageAttachmentSet is allocated
275   void EnsureMessageAttachmentSet();
276 
attachment_set()277   MessageAttachmentSet* attachment_set() {
278     EnsureMessageAttachmentSet();
279     return attachment_set_.get();
280   }
attachment_set()281   const MessageAttachmentSet* attachment_set() const {
282     return attachment_set_.get();
283   }
284 
285 #if BUILDFLAG(IPC_MESSAGE_LOG_ENABLED)
286   // Used for logging.
287   mutable int64_t received_time_;
288   mutable std::string output_params_;
289   mutable LogData* log_data_;
290   mutable bool dont_log_;
291 #endif
292 
293   FRIEND_TEST_ALL_PREFIXES(IPCMessageTest, FindNext);
294   FRIEND_TEST_ALL_PREFIXES(IPCMessageTest, FindNextOverflow);
295 };
296 
297 //------------------------------------------------------------------------------
298 
299 }  // namespace IPC
300 
301 enum SpecialRoutingIDs {
302   // indicates that we don't have a routing ID yet.
303   MSG_ROUTING_NONE = -2,
304 
305   // indicates a general message not sent to a particular tab.
306   MSG_ROUTING_CONTROL = INT32_MAX,
307 };
308 
309 #define IPC_REPLY_ID 0xFFFFFFF0  // Special message id for replies
310 #define IPC_LOGGING_ID 0xFFFFFFF1  // Special message id for logging
311 
312 #endif  // IPC_IPC_MESSAGE_H_
313