| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2006-2008 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 CHROME_COMMON_IPC_MESSAGE_H__ | |
| 6 #define CHROME_COMMON_IPC_MESSAGE_H__ | |
| 7 | |
| 8 #include <string> | |
| 9 | |
| 10 #include "base/basictypes.h" | |
| 11 #include "base/pickle.h" | |
| 12 | |
| 13 #ifndef NDEBUG | |
| 14 #define IPC_MESSAGE_LOG_ENABLED | |
| 15 #endif | |
| 16 | |
| 17 #if defined(OS_POSIX) | |
| 18 #include "base/ref_counted.h" | |
| 19 #endif | |
| 20 | |
| 21 namespace base { | |
| 22 class FileDescriptor; | |
| 23 } | |
| 24 | |
| 25 class FileDescriptorSet; | |
| 26 | |
| 27 namespace IPC { | |
| 28 | |
| 29 //------------------------------------------------------------------------------ | |
| 30 | |
| 31 class Channel; | |
| 32 class Message; | |
| 33 struct LogData; | |
| 34 | |
| 35 class Message : public Pickle { | |
| 36 public: | |
| 37 // Implemented by objects that can send IPC messages across a channel. | |
| 38 class Sender { | |
| 39 public: | |
| 40 virtual ~Sender() {} | |
| 41 | |
| 42 // Sends the given IPC message. The implementor takes ownership of the | |
| 43 // given Message regardless of whether or not this method succeeds. This | |
| 44 // is done to make this method easier to use. Returns true on success and | |
| 45 // false otherwise. | |
| 46 virtual bool Send(Message* msg) = 0; | |
| 47 }; | |
| 48 | |
| 49 enum PriorityValue { | |
| 50 PRIORITY_LOW = 1, | |
| 51 PRIORITY_NORMAL, | |
| 52 PRIORITY_HIGH | |
| 53 }; | |
| 54 | |
| 55 virtual ~Message(); | |
| 56 | |
| 57 Message(); | |
| 58 | |
| 59 // Initialize a message with a user-defined type, priority value, and | |
| 60 // destination WebView ID. | |
| 61 Message(int32 routing_id, uint16 type, PriorityValue priority); | |
| 62 | |
| 63 // Initializes a message from a const block of data. The data is not copied; | |
| 64 // instead the data is merely referenced by this message. Only const methods | |
| 65 // should be used on the message when initialized this way. | |
| 66 Message(const char* data, int data_len); | |
| 67 | |
| 68 Message(const Message& other); | |
| 69 Message& operator=(const Message& other); | |
| 70 | |
| 71 PriorityValue priority() const { | |
| 72 return static_cast<PriorityValue>(header()->flags & PRIORITY_MASK); | |
| 73 } | |
| 74 | |
| 75 // True if this is a synchronous message. | |
| 76 bool is_sync() const { | |
| 77 return (header()->flags & SYNC_BIT) != 0; | |
| 78 } | |
| 79 | |
| 80 // Set this on a reply to a synchronous message. | |
| 81 void set_reply() { | |
| 82 header()->flags |= REPLY_BIT; | |
| 83 } | |
| 84 | |
| 85 bool is_reply() const { | |
| 86 return (header()->flags & REPLY_BIT) != 0; | |
| 87 } | |
| 88 | |
| 89 // Set this on a reply to a synchronous message to indicate that no receiver | |
| 90 // was found. | |
| 91 void set_reply_error() { | |
| 92 header()->flags |= REPLY_ERROR_BIT; | |
| 93 } | |
| 94 | |
| 95 bool is_reply_error() const { | |
| 96 return (header()->flags & REPLY_ERROR_BIT) != 0; | |
| 97 } | |
| 98 | |
| 99 // Normally when a receiver gets a message and they're blocked on a | |
| 100 // synchronous message Send, they buffer a message. Setting this flag causes | |
| 101 // the receiver to be unblocked and the message to be dispatched immediately. | |
| 102 void set_unblock(bool unblock) { | |
| 103 if (unblock) { | |
| 104 header()->flags |= UNBLOCK_BIT; | |
| 105 } else { | |
| 106 header()->flags &= ~UNBLOCK_BIT; | |
| 107 } | |
| 108 } | |
| 109 | |
| 110 bool should_unblock() const { | |
| 111 return (header()->flags & UNBLOCK_BIT) != 0; | |
| 112 } | |
| 113 | |
| 114 // Tells the receiver that the caller is pumping messages while waiting | |
| 115 // for the result. | |
| 116 bool is_caller_pumping_messages() const { | |
| 117 return (header()->flags & PUMPING_MSGS_BIT) != 0; | |
| 118 } | |
| 119 | |
| 120 uint16 type() const { | |
| 121 return header()->type; | |
| 122 } | |
| 123 | |
| 124 int32 routing_id() const { | |
| 125 return header()->routing; | |
| 126 } | |
| 127 | |
| 128 void set_routing_id(int32 new_id) { | |
| 129 header()->routing = new_id; | |
| 130 } | |
| 131 | |
| 132 template<class T> | |
| 133 static bool Dispatch(const Message* msg, T* obj, void (T::*func)()) { | |
| 134 (obj->*func)(); | |
| 135 return true; | |
| 136 } | |
| 137 | |
| 138 template<class T> | |
| 139 static bool Dispatch(const Message* msg, T* obj, void (T::*func)() const) { | |
| 140 (obj->*func)(); | |
| 141 return true; | |
| 142 } | |
| 143 | |
| 144 template<class T> | |
| 145 static bool Dispatch(const Message* msg, T* obj, | |
| 146 void (T::*func)(const Message&)) { | |
| 147 (obj->*func)(*msg); | |
| 148 return true; | |
| 149 } | |
| 150 | |
| 151 template<class T> | |
| 152 static bool Dispatch(const Message* msg, T* obj, | |
| 153 void (T::*func)(const Message&) const) { | |
| 154 (obj->*func)(*msg); | |
| 155 return true; | |
| 156 } | |
| 157 | |
| 158 // Used for async messages with no parameters. | |
| 159 static void Log(const Message* msg, std::wstring* l) { | |
| 160 } | |
| 161 | |
| 162 // Find the end of the message data that starts at range_start. Returns NULL | |
| 163 // if the entire message is not found in the given data range. | |
| 164 static const char* FindNext(const char* range_start, const char* range_end) { | |
| 165 return Pickle::FindNext(sizeof(Header), range_start, range_end); | |
| 166 } | |
| 167 | |
| 168 #if defined(OS_POSIX) | |
| 169 // On POSIX, a message supports reading / writing FileDescriptor objects. | |
| 170 // This is used to pass a file descriptor to the peer of an IPC channel. | |
| 171 | |
| 172 // Add a descriptor to the end of the set. Returns false iff the set is full. | |
| 173 bool WriteFileDescriptor(const base::FileDescriptor& descriptor); | |
| 174 // Get a file descriptor from the message. Returns false on error. | |
| 175 // iter: a Pickle iterator to the current location in the message. | |
| 176 bool ReadFileDescriptor(void** iter, base::FileDescriptor* descriptor) const; | |
| 177 #endif | |
| 178 | |
| 179 #ifdef IPC_MESSAGE_LOG_ENABLED | |
| 180 // Adds the outgoing time from Time::Now() at the end of the message and sets | |
| 181 // a bit to indicate that it's been added. | |
| 182 void set_sent_time(int64 time); | |
| 183 int64 sent_time() const; | |
| 184 | |
| 185 void set_received_time(int64 time) const; | |
| 186 int64 received_time() const { return received_time_; } | |
| 187 void set_output_params(const std::wstring& op) const { output_params_ = op; } | |
| 188 const std::wstring& output_params() const { return output_params_; } | |
| 189 // The following four functions are needed so we can log sync messages with | |
| 190 // delayed replies. We stick the log data from the sent message into the | |
| 191 // reply message, so that when it's sent and we have the output parameters | |
| 192 // we can log it. As such, we set a flag on the sent message to not log it. | |
| 193 void set_sync_log_data(LogData* data) const { log_data_ = data; } | |
| 194 LogData* sync_log_data() const { return log_data_; } | |
| 195 void set_dont_log() const { dont_log_ = true; } | |
| 196 bool dont_log() const { return dont_log_; } | |
| 197 #endif | |
| 198 | |
| 199 protected: | |
| 200 friend class Channel; | |
| 201 friend class MessageReplyDeserializer; | |
| 202 friend class SyncMessage; | |
| 203 | |
| 204 void set_sync() { | |
| 205 header()->flags |= SYNC_BIT; | |
| 206 } | |
| 207 | |
| 208 // flags | |
| 209 enum { | |
| 210 PRIORITY_MASK = 0x0003, | |
| 211 SYNC_BIT = 0x0004, | |
| 212 REPLY_BIT = 0x0008, | |
| 213 REPLY_ERROR_BIT = 0x0010, | |
| 214 UNBLOCK_BIT = 0x0020, | |
| 215 PUMPING_MSGS_BIT= 0x0040, | |
| 216 HAS_SENT_TIME_BIT = 0x0080, | |
| 217 }; | |
| 218 | |
| 219 #pragma pack(push, 2) | |
| 220 struct Header : Pickle::Header { | |
| 221 int32 routing; // ID of the view that this message is destined for | |
| 222 uint16 type; // specifies the user-defined message type | |
| 223 uint16 flags; // specifies control flags for the message | |
| 224 #if defined(OS_POSIX) | |
| 225 uint32 num_fds; // the number of descriptors included with this message | |
| 226 #endif | |
| 227 }; | |
| 228 #pragma pack(pop) | |
| 229 | |
| 230 Header* header() { | |
| 231 return headerT<Header>(); | |
| 232 } | |
| 233 const Header* header() const { | |
| 234 return headerT<Header>(); | |
| 235 } | |
| 236 | |
| 237 void InitLoggingVariables(); | |
| 238 | |
| 239 #if defined(OS_POSIX) | |
| 240 // The set of file descriptors associated with this message. | |
| 241 scoped_refptr<FileDescriptorSet> file_descriptor_set_; | |
| 242 | |
| 243 // Ensure that a FileDescriptorSet is allocated | |
| 244 void EnsureFileDescriptorSet(); | |
| 245 | |
| 246 FileDescriptorSet* file_descriptor_set() { | |
| 247 EnsureFileDescriptorSet(); | |
| 248 return file_descriptor_set_.get(); | |
| 249 } | |
| 250 const FileDescriptorSet* file_descriptor_set() const { | |
| 251 return file_descriptor_set_.get(); | |
| 252 } | |
| 253 #endif | |
| 254 | |
| 255 #ifdef IPC_MESSAGE_LOG_ENABLED | |
| 256 // Used for logging. | |
| 257 mutable int64 received_time_; | |
| 258 mutable std::wstring output_params_; | |
| 259 mutable LogData* log_data_; | |
| 260 mutable bool dont_log_; | |
| 261 #endif | |
| 262 }; | |
| 263 | |
| 264 //------------------------------------------------------------------------------ | |
| 265 | |
| 266 } // namespace IPC | |
| 267 | |
| 268 enum SpecialRoutingIDs { | |
| 269 // indicates that we don't have a routing ID yet. | |
| 270 MSG_ROUTING_NONE = -2, | |
| 271 | |
| 272 // indicates a general message not sent to a particular tab. | |
| 273 MSG_ROUTING_CONTROL = kint32max, | |
| 274 }; | |
| 275 | |
| 276 #define IPC_REPLY_ID 0xFFF0 // Special message id for replies | |
| 277 #define IPC_LOGGING_ID 0xFFF1 // Special message id for logging | |
| 278 | |
| 279 #endif // CHROME_COMMON_IPC_MESSAGE_H__ | |
| OLD | NEW |