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