1 // Copyright 2012 The Chromium Authors 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/raw_ptr.h" 15 #include "base/memory/ref_counted.h" 16 #include "base/pickle.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, // Deprecated. 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, size_t 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 &= static_cast<uint32_t>(~UNBLOCK_BIT); 116 } 117 } 118 should_unblock()119 bool should_unblock() const { 120 return (header()->flags & UNBLOCK_BIT) != 0; 121 } 122 set_dispatch_error()123 void set_dispatch_error() const { 124 dispatch_error_ = true; 125 } 126 dispatch_error()127 bool dispatch_error() const { 128 return dispatch_error_; 129 } 130 type()131 uint32_t type() const { 132 return header()->type; 133 } 134 routing_id()135 int32_t routing_id() const { 136 return header()->routing; 137 } 138 set_routing_id(int32_t new_id)139 void set_routing_id(int32_t new_id) { 140 header()->routing = new_id; 141 } 142 flags()143 uint32_t flags() const { 144 return header()->flags; 145 } 146 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); 150 151 template<class T, class S, class P> Dispatch(const Message * msg,T * obj,S * sender,P * parameter,void (T::* func)())152 static bool Dispatch(const Message* msg, T* obj, S* sender, P* parameter, 153 void (T::*func)()) { 154 (obj->*func)(); 155 return true; 156 } 157 158 template<class T, class S, class P> Dispatch(const Message * msg,T * obj,S * sender,P * parameter,void (T::* func)(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; 163 } 164 165 // Used for async messages with no parameters. Log(std::string * name,const Message * msg,std::string * l)166 static void Log(std::string* name, const Message* msg, std::string* l) { 167 } 168 169 // The static method FindNext() returns several pieces of information, which 170 // are aggregated into an instance of this struct. 171 struct IPC_MESSAGE_SUPPORT_EXPORT NextMessageInfo { 172 NextMessageInfo(); 173 ~NextMessageInfo(); 174 175 // Total message size. Always valid if |message_found| is true. 176 // If |message_found| is false but we could determine message size 177 // from the header, this field is non-zero. Otherwise it's zero. 178 size_t message_size; 179 // Whether an entire message was found in the given memory range. 180 bool message_found; 181 // Only filled in if |message_found| is true. 182 // The start address is passed into FindNext() by the caller, so isn't 183 // repeated in this struct. The end address of the pickle should be used to 184 // construct a base::Pickle. 185 const char* pickle_end; 186 // Only filled in if |message_found| is true. 187 // The end address of the message should be used to determine the start 188 // address of the next message. 189 const char* message_end; 190 }; 191 192 // |info| is an output parameter and must not be nullptr. 193 static void FindNext(const char* range_start, 194 const char* range_end, 195 NextMessageInfo* info); 196 197 // WriteAttachment appends |attachment| to the end of the set. It returns 198 // false iff the set is full. 199 bool WriteAttachment( 200 scoped_refptr<base::Pickle::Attachment> attachment) override; 201 // ReadAttachment parses an attachment given the parsing state |iter| and 202 // writes it to |*attachment|. It returns true on success. 203 bool ReadAttachment( 204 base::PickleIterator* iter, 205 scoped_refptr<base::Pickle::Attachment>* attachment) const override; 206 // Returns true if there are any attachment in this message. 207 bool HasAttachments() const override; 208 209 #if BUILDFLAG(IPC_MESSAGE_LOG_ENABLED) 210 // Adds the outgoing time from Time::Now() at the end of the message and sets 211 // a bit to indicate that it's been added. 212 void set_sent_time(int64_t time); 213 int64_t sent_time() const; 214 215 void set_received_time(int64_t time) const; received_time()216 int64_t received_time() const { return received_time_; } set_output_params(const std::string & op)217 void set_output_params(const std::string& op) const { output_params_ = op; } output_params()218 const std::string& output_params() const { return output_params_; } 219 // The following four functions are needed so we can log sync messages with 220 // delayed replies. We stick the log data from the sent message into the 221 // reply message, so that when it's sent and we have the output parameters 222 // we can log it. As such, we set a flag on the sent message to not log it. set_sync_log_data(LogData * data)223 void set_sync_log_data(LogData* data) const { log_data_ = data; } sync_log_data()224 LogData* sync_log_data() const { return log_data_; } set_dont_log()225 void set_dont_log() const { dont_log_ = true; } dont_log()226 bool dont_log() const { return dont_log_; } 227 #endif 228 229 protected: 230 friend class Channel; 231 friend class ChannelMojo; 232 friend class ChannelNacl; 233 friend class ChannelPosix; 234 friend class ChannelWin; 235 friend class internal::ChannelReader; 236 friend class MessageReplyDeserializer; 237 friend class SyncMessage; 238 239 friend struct mojo::internal::UnmappedNativeStructSerializerImpl; 240 241 #pragma pack(push, 4) 242 struct Header : base::Pickle::Header { 243 int32_t routing; // ID of the view that this message is destined for 244 uint32_t type; // specifies the user-defined message type 245 uint32_t flags; // specifies control flags for the message 246 #if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) 247 uint16_t num_fds; // the number of descriptors included with this message 248 uint16_t pad; // explicitly initialize this to appease valgrind 249 #endif 250 }; 251 #pragma pack(pop) 252 header()253 Header* header() { 254 return headerT<Header>(); 255 } header()256 const Header* header() const { 257 return headerT<Header>(); 258 } 259 260 void Init(); 261 262 // Used internally to support IPC::Listener::OnBadMessageReceived. 263 mutable bool dispatch_error_; 264 265 // The set of file descriptors associated with this message. 266 scoped_refptr<MessageAttachmentSet> attachment_set_; 267 268 // Ensure that a MessageAttachmentSet is allocated 269 void EnsureMessageAttachmentSet(); 270 attachment_set()271 MessageAttachmentSet* attachment_set() { 272 EnsureMessageAttachmentSet(); 273 return attachment_set_.get(); 274 } attachment_set()275 const MessageAttachmentSet* attachment_set() const { 276 return attachment_set_.get(); 277 } 278 279 #if BUILDFLAG(IPC_MESSAGE_LOG_ENABLED) 280 // Used for logging. 281 mutable int64_t received_time_; 282 mutable std::string output_params_; 283 mutable raw_ptr<LogData> log_data_; 284 mutable bool dont_log_; 285 #endif 286 287 FRIEND_TEST_ALL_PREFIXES(IPCMessageTest, FindNext); 288 FRIEND_TEST_ALL_PREFIXES(IPCMessageTest, FindNextOverflow); 289 }; 290 291 //------------------------------------------------------------------------------ 292 293 } // namespace IPC 294 295 enum SpecialRoutingIDs { 296 // indicates that we don't have a routing ID yet. 297 MSG_ROUTING_NONE = -2, 298 299 // indicates a general message not sent to a particular tab. 300 MSG_ROUTING_CONTROL = INT32_MAX, 301 }; 302 303 #define IPC_REPLY_ID 0xFFFFFFF0 // Special message id for replies 304 #define IPC_LOGGING_ID 0xFFFFFFF1 // Special message id for logging 305 306 #endif // IPC_IPC_MESSAGE_H_ 307