thakis@chromium.org | fead1e6 | 2012-02-22 01:04:50 +0900 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
darin@chromium.org | 80e4c5e | 2011-08-16 05:41:46 +0900 | [diff] [blame] | 5 | #ifndef IPC_IPC_MESSAGE_H_ |
| 6 | #define IPC_IPC_MESSAGE_H_ |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 7 | |
| 8 | #include <string> |
| 9 | |
| 10 | #include "base/basictypes.h" |
jbates@chromium.org | 4b8271c | 2012-09-05 08:08:09 +0900 | [diff] [blame] | 11 | #include "base/debug/trace_event.h" |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 12 | #include "base/pickle.h" |
darin@chromium.org | 80e4c5e | 2011-08-16 05:41:46 +0900 | [diff] [blame] | 13 | #include "ipc/ipc_export.h" |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 14 | |
jochen@chromium.org | 15866bf | 2012-11-28 23:35:59 +0900 | [diff] [blame] | 15 | #if !defined(NDEBUG) |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 16 | #define IPC_MESSAGE_LOG_ENABLED |
| 17 | #endif |
| 18 | |
| 19 | #if defined(OS_POSIX) |
levin@chromium.org | 5c52868 | 2011-03-28 10:54:15 +0900 | [diff] [blame] | 20 | #include "base/memory/ref_counted.h" |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 21 | #endif |
| 22 | |
| 23 | namespace base { |
evan@chromium.org | c16fa4f | 2010-02-02 10:24:49 +0900 | [diff] [blame] | 24 | struct FileDescriptor; |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | class FileDescriptorSet; |
| 28 | |
| 29 | namespace IPC { |
| 30 | |
| 31 | //------------------------------------------------------------------------------ |
| 32 | |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 33 | struct LogData; |
| 34 | |
darin@chromium.org | 80e4c5e | 2011-08-16 05:41:46 +0900 | [diff] [blame] | 35 | class IPC_EXPORT Message : public Pickle { |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 36 | public: |
bbudge@chromium.org | ab4c6bc | 2013-11-05 07:28:12 +0900 | [diff] [blame] | 37 | enum PriorityValue { |
| 38 | PRIORITY_LOW = 1, |
| 39 | PRIORITY_NORMAL, |
| 40 | PRIORITY_HIGH |
| 41 | }; |
| 42 | |
brettw@chromium.org | 6471c93 | 2012-03-31 07:35:27 +0900 | [diff] [blame] | 43 | // Bit values used in the flags field. |
jbates@chromium.org | 4b8271c | 2012-09-05 08:08:09 +0900 | [diff] [blame] | 44 | // Upper 24 bits of flags store a reference number, so this enum is limited to |
| 45 | // 8 bits. |
brettw@chromium.org | 6471c93 | 2012-03-31 07:35:27 +0900 | [diff] [blame] | 46 | enum { |
bbudge@chromium.org | ab4c6bc | 2013-11-05 07:28:12 +0900 | [diff] [blame] | 47 | PRIORITY_MASK = 0x03, // Low 2 bits of store the priority value. |
jbates@chromium.org | 4b8271c | 2012-09-05 08:08:09 +0900 | [diff] [blame] | 48 | SYNC_BIT = 0x04, |
| 49 | REPLY_BIT = 0x08, |
| 50 | REPLY_ERROR_BIT = 0x10, |
| 51 | UNBLOCK_BIT = 0x20, |
| 52 | PUMPING_MSGS_BIT = 0x40, |
| 53 | HAS_SENT_TIME_BIT = 0x80, |
brettw@chromium.org | 6471c93 | 2012-03-31 07:35:27 +0900 | [diff] [blame] | 54 | }; |
| 55 | |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 56 | virtual ~Message(); |
| 57 | |
| 58 | Message(); |
| 59 | |
bbudge@chromium.org | ab4c6bc | 2013-11-05 07:28:12 +0900 | [diff] [blame] | 60 | // Initialize a message with a user-defined type, priority value, and |
| 61 | // destination WebView ID. |
| 62 | Message(int32 routing_id, uint32 type, PriorityValue priority); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 63 | |
| 64 | // Initializes a message from a const block of data. The data is not copied; |
| 65 | // instead the data is merely referenced by this message. Only const methods |
| 66 | // should be used on the message when initialized this way. |
bbudge@chromium.org | ab4c6bc | 2013-11-05 07:28:12 +0900 | [diff] [blame] | 67 | Message(const char* data, int data_len); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 68 | |
| 69 | Message(const Message& other); |
| 70 | Message& operator=(const Message& other); |
| 71 | |
bbudge@chromium.org | ab4c6bc | 2013-11-05 07:28:12 +0900 | [diff] [blame] | 72 | PriorityValue priority() const { |
| 73 | return static_cast<PriorityValue>(header()->flags & PRIORITY_MASK); |
| 74 | } |
| 75 | |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 76 | // True if this is a synchronous message. |
brettw@chromium.org | 6471c93 | 2012-03-31 07:35:27 +0900 | [diff] [blame] | 77 | void set_sync() { |
| 78 | header()->flags |= SYNC_BIT; |
| 79 | } |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 80 | bool is_sync() const { |
| 81 | return (header()->flags & SYNC_BIT) != 0; |
| 82 | } |
| 83 | |
| 84 | // Set this on a reply to a synchronous message. |
| 85 | void set_reply() { |
| 86 | header()->flags |= REPLY_BIT; |
| 87 | } |
| 88 | |
| 89 | bool is_reply() const { |
| 90 | return (header()->flags & REPLY_BIT) != 0; |
| 91 | } |
| 92 | |
| 93 | // Set this on a reply to a synchronous message to indicate that no receiver |
| 94 | // was found. |
| 95 | void set_reply_error() { |
| 96 | header()->flags |= REPLY_ERROR_BIT; |
| 97 | } |
| 98 | |
| 99 | bool is_reply_error() const { |
| 100 | return (header()->flags & REPLY_ERROR_BIT) != 0; |
| 101 | } |
| 102 | |
| 103 | // Normally when a receiver gets a message and they're blocked on a |
| 104 | // synchronous message Send, they buffer a message. Setting this flag causes |
| 105 | // the receiver to be unblocked and the message to be dispatched immediately. |
| 106 | void set_unblock(bool unblock) { |
| 107 | if (unblock) { |
| 108 | header()->flags |= UNBLOCK_BIT; |
| 109 | } else { |
| 110 | header()->flags &= ~UNBLOCK_BIT; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | bool should_unblock() const { |
| 115 | return (header()->flags & UNBLOCK_BIT) != 0; |
| 116 | } |
| 117 | |
| 118 | // Tells the receiver that the caller is pumping messages while waiting |
| 119 | // for the result. |
| 120 | bool is_caller_pumping_messages() const { |
| 121 | return (header()->flags & PUMPING_MSGS_BIT) != 0; |
| 122 | } |
| 123 | |
jam@chromium.org | 822f1fb | 2014-05-16 08:06:07 +0900 | [diff] [blame] | 124 | void set_dispatch_error() const { |
| 125 | dispatch_error_ = true; |
| 126 | } |
| 127 | |
| 128 | bool dispatch_error() const { |
| 129 | return dispatch_error_; |
| 130 | } |
| 131 | |
apatrick@google.com | a240677 | 2009-12-05 03:08:45 +0900 | [diff] [blame] | 132 | uint32 type() const { |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 133 | return header()->type; |
| 134 | } |
| 135 | |
| 136 | int32 routing_id() const { |
| 137 | return header()->routing; |
| 138 | } |
| 139 | |
| 140 | void set_routing_id(int32 new_id) { |
| 141 | header()->routing = new_id; |
| 142 | } |
| 143 | |
brettw@chromium.org | 6471c93 | 2012-03-31 07:35:27 +0900 | [diff] [blame] | 144 | uint32 flags() const { |
| 145 | return header()->flags; |
| 146 | } |
| 147 | |
brettw@chromium.org | f48910a | 2012-06-29 09:05:04 +0900 | [diff] [blame] | 148 | // Sets all the given header values. The message should be empty at this |
| 149 | // call. |
| 150 | void SetHeaderValues(int32 routing, uint32 type, uint32 flags); |
| 151 | |
jam@chromium.org | f637018 | 2014-05-14 08:19:10 +0900 | [diff] [blame] | 152 | template<class T, class S, class P> |
| 153 | static bool Dispatch(const Message* msg, T* obj, S* sender, P* parameter, |
jam@chromium.org | 82f52f8 | 2010-12-22 05:03:24 +0900 | [diff] [blame] | 154 | void (T::*func)()) { |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 155 | (obj->*func)(); |
| 156 | return true; |
| 157 | } |
| 158 | |
jam@chromium.org | f637018 | 2014-05-14 08:19:10 +0900 | [diff] [blame] | 159 | template<class T, class S, class P> |
| 160 | static bool Dispatch(const Message* msg, T* obj, S* sender, P* parameter, |
| 161 | void (T::*func)(P*)) { |
| 162 | (obj->*func)(parameter); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 163 | return true; |
| 164 | } |
| 165 | |
| 166 | // Used for async messages with no parameters. |
jam@chromium.org | 86a8de1 | 2010-12-09 08:34:16 +0900 | [diff] [blame] | 167 | static void Log(std::string* name, const Message* msg, std::string* l) { |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | // Find the end of the message data that starts at range_start. Returns NULL |
| 171 | // if the entire message is not found in the given data range. |
| 172 | static const char* FindNext(const char* range_start, const char* range_end) { |
| 173 | return Pickle::FindNext(sizeof(Header), range_start, range_end); |
| 174 | } |
| 175 | |
| 176 | #if defined(OS_POSIX) |
| 177 | // On POSIX, a message supports reading / writing FileDescriptor objects. |
| 178 | // This is used to pass a file descriptor to the peer of an IPC channel. |
| 179 | |
brettw@chromium.org | f48910a | 2012-06-29 09:05:04 +0900 | [diff] [blame] | 180 | // Add a descriptor to the end of the set. Returns false if the set is full. |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 181 | bool WriteFileDescriptor(const base::FileDescriptor& descriptor); |
brettw@chromium.org | f48910a | 2012-06-29 09:05:04 +0900 | [diff] [blame] | 182 | |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 183 | // Get a file descriptor from the message. Returns false on error. |
| 184 | // iter: a Pickle iterator to the current location in the message. |
jbates@chromium.org | 0fc8736 | 2012-03-08 05:42:56 +0900 | [diff] [blame] | 185 | bool ReadFileDescriptor(PickleIterator* iter, |
| 186 | base::FileDescriptor* descriptor) const; |
brettw@chromium.org | f48910a | 2012-06-29 09:05:04 +0900 | [diff] [blame] | 187 | |
| 188 | // Returns true if there are any file descriptors in this message. |
| 189 | bool HasFileDescriptors() const; |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 190 | #endif |
| 191 | |
| 192 | #ifdef IPC_MESSAGE_LOG_ENABLED |
| 193 | // Adds the outgoing time from Time::Now() at the end of the message and sets |
| 194 | // a bit to indicate that it's been added. |
| 195 | void set_sent_time(int64 time); |
| 196 | int64 sent_time() const; |
| 197 | |
| 198 | void set_received_time(int64 time) const; |
| 199 | int64 received_time() const { return received_time_; } |
erg@google.com | 8aca727 | 2010-08-19 03:33:57 +0900 | [diff] [blame] | 200 | void set_output_params(const std::string& op) const { output_params_ = op; } |
| 201 | const std::string& output_params() const { return output_params_; } |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 202 | // The following four functions are needed so we can log sync messages with |
| 203 | // delayed replies. We stick the log data from the sent message into the |
| 204 | // reply message, so that when it's sent and we have the output parameters |
| 205 | // we can log it. As such, we set a flag on the sent message to not log it. |
| 206 | void set_sync_log_data(LogData* data) const { log_data_ = data; } |
| 207 | LogData* sync_log_data() const { return log_data_; } |
| 208 | void set_dont_log() const { dont_log_ = true; } |
| 209 | bool dont_log() const { return dont_log_; } |
| 210 | #endif |
| 211 | |
jbates@chromium.org | 7cc8033 | 2012-09-18 12:41:29 +0900 | [diff] [blame] | 212 | // Called to trace when message is sent. |
| 213 | void TraceMessageBegin() { |
epenner@chromium.org | 24f2bea | 2014-05-03 06:29:24 +0900 | [diff] [blame] | 214 | TRACE_EVENT_FLOW_BEGIN0(TRACE_DISABLED_BY_DEFAULT("ipc.flow"), "IPC", |
fmeawad@chromium.org | 4529a9d | 2014-02-07 03:28:53 +0900 | [diff] [blame] | 215 | header()->flags); |
jbates@chromium.org | 7cc8033 | 2012-09-18 12:41:29 +0900 | [diff] [blame] | 216 | } |
| 217 | // Called to trace when message is received. |
| 218 | void TraceMessageEnd() { |
epenner@chromium.org | 24f2bea | 2014-05-03 06:29:24 +0900 | [diff] [blame] | 219 | TRACE_EVENT_FLOW_END0(TRACE_DISABLED_BY_DEFAULT("ipc.flow"), "IPC", |
fmeawad@chromium.org | 4529a9d | 2014-02-07 03:28:53 +0900 | [diff] [blame] | 220 | header()->flags); |
jbates@chromium.org | 4b8271c | 2012-09-05 08:08:09 +0900 | [diff] [blame] | 221 | } |
| 222 | |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 223 | protected: |
| 224 | friend class Channel; |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 225 | friend class ChannelMojo; |
morrita@chromium.org | fde2b6b | 2014-06-07 05:13:51 +0900 | [diff] [blame] | 226 | friend class ChannelNacl; |
| 227 | friend class ChannelPosix; |
| 228 | friend class ChannelWin; |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 229 | friend class MessageReplyDeserializer; |
| 230 | friend class SyncMessage; |
| 231 | |
apatrick@google.com | a240677 | 2009-12-05 03:08:45 +0900 | [diff] [blame] | 232 | #pragma pack(push, 4) |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 233 | struct Header : Pickle::Header { |
| 234 | int32 routing; // ID of the view that this message is destined for |
apatrick@google.com | a240677 | 2009-12-05 03:08:45 +0900 | [diff] [blame] | 235 | uint32 type; // specifies the user-defined message type |
| 236 | uint32 flags; // specifies control flags for the message |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 237 | #if defined(OS_POSIX) |
apatrick@google.com | a240677 | 2009-12-05 03:08:45 +0900 | [diff] [blame] | 238 | uint16 num_fds; // the number of descriptors included with this message |
apatrick@google.com | b2bde43 | 2009-12-05 09:00:58 +0900 | [diff] [blame] | 239 | uint16 pad; // explicitly initialize this to appease valgrind |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 240 | #endif |
| 241 | }; |
| 242 | #pragma pack(pop) |
| 243 | |
| 244 | Header* header() { |
| 245 | return headerT<Header>(); |
| 246 | } |
| 247 | const Header* header() const { |
| 248 | return headerT<Header>(); |
| 249 | } |
| 250 | |
jam@chromium.org | 822f1fb | 2014-05-16 08:06:07 +0900 | [diff] [blame] | 251 | void Init(); |
| 252 | |
| 253 | // Used internally to support IPC::Listener::OnBadMessageReceived. |
| 254 | mutable bool dispatch_error_; |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 255 | |
| 256 | #if defined(OS_POSIX) |
| 257 | // The set of file descriptors associated with this message. |
| 258 | scoped_refptr<FileDescriptorSet> file_descriptor_set_; |
| 259 | |
| 260 | // Ensure that a FileDescriptorSet is allocated |
| 261 | void EnsureFileDescriptorSet(); |
| 262 | |
| 263 | FileDescriptorSet* file_descriptor_set() { |
| 264 | EnsureFileDescriptorSet(); |
| 265 | return file_descriptor_set_.get(); |
| 266 | } |
| 267 | const FileDescriptorSet* file_descriptor_set() const { |
| 268 | return file_descriptor_set_.get(); |
| 269 | } |
| 270 | #endif |
| 271 | |
| 272 | #ifdef IPC_MESSAGE_LOG_ENABLED |
| 273 | // Used for logging. |
| 274 | mutable int64 received_time_; |
erg@google.com | 8aca727 | 2010-08-19 03:33:57 +0900 | [diff] [blame] | 275 | mutable std::string output_params_; |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 276 | mutable LogData* log_data_; |
| 277 | mutable bool dont_log_; |
| 278 | #endif |
| 279 | }; |
| 280 | |
| 281 | //------------------------------------------------------------------------------ |
| 282 | |
| 283 | } // namespace IPC |
| 284 | |
| 285 | enum SpecialRoutingIDs { |
| 286 | // indicates that we don't have a routing ID yet. |
| 287 | MSG_ROUTING_NONE = -2, |
| 288 | |
| 289 | // indicates a general message not sent to a particular tab. |
| 290 | MSG_ROUTING_CONTROL = kint32max, |
| 291 | }; |
| 292 | |
apatrick@google.com | a240677 | 2009-12-05 03:08:45 +0900 | [diff] [blame] | 293 | #define IPC_REPLY_ID 0xFFFFFFF0 // Special message id for replies |
| 294 | #define IPC_LOGGING_ID 0xFFFFFFF1 // Special message id for logging |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 295 | |
darin@chromium.org | 80e4c5e | 2011-08-16 05:41:46 +0900 | [diff] [blame] | 296 | #endif // IPC_IPC_MESSAGE_H_ |