[email protected] | 225c8f5 | 2010-02-05 22:23:20 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
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 | |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 5 | #ifndef IPC_IPC_MESSAGE_UTILS_H_ |
| 6 | #define IPC_IPC_MESSAGE_UTILS_H_ |
[email protected] | 32b76ef | 2010-07-26 23:08:24 | [diff] [blame] | 7 | #pragma once |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 8 | |
[email protected] | 379e7a5 | 2010-03-09 00:38:41 | [diff] [blame] | 9 | #include <algorithm> |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 10 | #include <string> |
| 11 | #include <vector> |
| 12 | #include <map> |
[email protected] | 96da696 | 2010-05-13 19:10:34 | [diff] [blame] | 13 | #include <set> |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 14 | |
[email protected] | dce5df5 | 2009-06-29 17:58:25 | [diff] [blame] | 15 | #include "base/format_macros.h" |
[email protected] | eb47a13 | 2009-03-04 00:39:56 | [diff] [blame] | 16 | #include "base/string16.h" |
[email protected] | dce5df5 | 2009-06-29 17:58:25 | [diff] [blame] | 17 | #include "base/string_util.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 18 | #include "base/tuple.h" |
[email protected] | 939856a | 2010-08-24 20:29:02 | [diff] [blame] | 19 | #include "ipc/ipc_param_traits.h" |
[email protected] | 0cfe5dae | 2010-08-17 00:24:54 | [diff] [blame] | 20 | #include "ipc/ipc_sync_message.h" |
[email protected] | 55b4e21 | 2010-08-13 20:43:58 | [diff] [blame] | 21 | |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 22 | #if defined(COMPILER_GCC) |
| 23 | // GCC "helpfully" tries to inline template methods in release mode. Except we |
| 24 | // want the majority of the template junk being expanded once in the |
| 25 | // implementation file (and only provide the definitions in |
| 26 | // ipc_message_utils_impl.h in those files) and exported, instead of expanded |
| 27 | // at every call site. Special note: GCC happily accepts the attribute before |
| 28 | // the method declaration, but only acts on it if it is after. |
[email protected] | efafbbd | 2010-08-19 20:23:25 | [diff] [blame] | 29 | #if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100) >= 40500 |
| 30 | // Starting in gcc 4.5, the noinline no longer implies the concept covered by |
| 31 | // the introduced noclone attribute, which will create specialized versions of |
| 32 | // functions/methods when certain types are constant. |
| 33 | // www.gnu.org/software/gcc/gcc-4.5/changes.html |
| 34 | #define IPC_MSG_NOINLINE __attribute__((noinline, noclone)); |
| 35 | #else |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 36 | #define IPC_MSG_NOINLINE __attribute__((noinline)); |
[email protected] | efafbbd | 2010-08-19 20:23:25 | [diff] [blame] | 37 | #endif |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 38 | #elif defined(COMPILER_MSVC) |
| 39 | // MSVC++ doesn't do this. |
| 40 | #define IPC_MSG_NOINLINE |
| 41 | #else |
| 42 | #error "Please add the noinline property for your new compiler here." |
| 43 | #endif |
| 44 | |
[email protected] | f91cb99 | 2009-02-04 20:10:12 | [diff] [blame] | 45 | // Used by IPC_BEGIN_MESSAGES so that each message class starts from a unique |
| 46 | // base. Messages have unique IDs across channels in order for the IPC logging |
| 47 | // code to figure out the message class from its ID. |
| 48 | enum IPCMessageStart { |
[email protected] | f91cb99 | 2009-02-04 20:10:12 | [diff] [blame] | 49 | AutomationMsgStart = 0, |
| 50 | ViewMsgStart, |
[email protected] | f91cb99 | 2009-02-04 20:10:12 | [diff] [blame] | 51 | PluginMsgStart, |
[email protected] | 21fa3a1 | 2010-12-08 23:34:16 | [diff] [blame] | 52 | ProfileImportMsgStart, |
[email protected] | f91cb99 | 2009-02-04 20:10:12 | [diff] [blame] | 53 | TestMsgStart, |
[email protected] | 21fa3a1 | 2010-12-08 23:34:16 | [diff] [blame] | 54 | DevToolsMsgStart, |
[email protected] | eb47a13 | 2009-03-04 00:39:56 | [diff] [blame] | 55 | WorkerMsgStart, |
[email protected] | 21fa3a1 | 2010-12-08 23:34:16 | [diff] [blame] | 56 | NaClMsgStart, |
[email protected] | 60f1439 | 2009-12-15 20:46:32 | [diff] [blame] | 57 | UtilityMsgStart, |
[email protected] | c0fc094 | 2010-01-13 00:55:37 | [diff] [blame] | 58 | GpuMsgStart, |
[email protected] | 38fe196 | 2010-07-31 07:57:00 | [diff] [blame] | 59 | ServiceMsgStart, |
[email protected] | 709a847e | 2010-11-10 01:16:11 | [diff] [blame] | 60 | PpapiMsgStart, |
[email protected] | 21fa3a1 | 2010-12-08 23:34:16 | [diff] [blame] | 61 | FirefoxImporterUnittestMsgStart, |
[email protected] | 9f547bf | 2010-12-13 17:00:42 | [diff] [blame] | 62 | FileUtilitiesMsgStart, |
| 63 | MimeRegistryMsgStart, |
[email protected] | 26a9acf | 2010-12-13 19:35:05 | [diff] [blame^] | 64 | DatabaseMsgStart, |
[email protected] | f91cb99 | 2009-02-04 20:10:12 | [diff] [blame] | 65 | }; |
| 66 | |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 67 | class DictionaryValue; |
| 68 | class FilePath; |
| 69 | class ListValue; |
| 70 | class NullableString16; |
| 71 | |
| 72 | namespace base { |
| 73 | class Time; |
[email protected] | d84e48b | 2010-10-21 22:04:52 | [diff] [blame] | 74 | class TimeDelta; |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 75 | struct FileDescriptor; |
| 76 | } |
| 77 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 78 | namespace IPC { |
| 79 | |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 80 | struct ChannelHandle; |
| 81 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 82 | //----------------------------------------------------------------------------- |
| 83 | // An iterator class for reading the fields contained within a Message. |
| 84 | |
| 85 | class MessageIterator { |
| 86 | public: |
[email protected] | e1981f43 | 2008-08-12 15:22:13 | [diff] [blame] | 87 | explicit MessageIterator(const Message& m) : msg_(m), iter_(NULL) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 88 | } |
| 89 | int NextInt() const { |
[email protected] | 9422b7dc | 2009-12-30 20:09:02 | [diff] [blame] | 90 | int val = -1; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 91 | if (!msg_.ReadInt(&iter_, &val)) |
| 92 | NOTREACHED(); |
| 93 | return val; |
| 94 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 95 | const std::string NextString() const { |
| 96 | std::string val; |
| 97 | if (!msg_.ReadString(&iter_, &val)) |
| 98 | NOTREACHED(); |
| 99 | return val; |
| 100 | } |
| 101 | const std::wstring NextWString() const { |
| 102 | std::wstring val; |
| 103 | if (!msg_.ReadWString(&iter_, &val)) |
| 104 | NOTREACHED(); |
| 105 | return val; |
| 106 | } |
[email protected] | 225c8f5 | 2010-02-05 22:23:20 | [diff] [blame] | 107 | void NextData(const char** data, int* length) const { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 108 | if (!msg_.ReadData(&iter_, data, length)) { |
| 109 | NOTREACHED(); |
| 110 | } |
| 111 | } |
| 112 | private: |
| 113 | const Message& msg_; |
| 114 | mutable void* iter_; |
| 115 | }; |
| 116 | |
| 117 | //----------------------------------------------------------------------------- |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 118 | // ParamTraits specializations, etc. |
| 119 | |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 120 | template <class P> |
| 121 | static inline void WriteParam(Message* m, const P& p) { |
[email protected] | 7b291f9 | 2009-08-14 05:43:53 | [diff] [blame] | 122 | typedef typename SimilarTypeTraits<P>::Type Type; |
[email protected] | 46ce5b56 | 2010-06-16 18:39:53 | [diff] [blame] | 123 | ParamTraits<Type>::Write(m, static_cast<const Type& >(p)); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | template <class P> |
[email protected] | 1e86aa6 | 2009-04-24 21:22:33 | [diff] [blame] | 127 | static inline bool WARN_UNUSED_RESULT ReadParam(const Message* m, void** iter, |
| 128 | P* p) { |
[email protected] | 7b291f9 | 2009-08-14 05:43:53 | [diff] [blame] | 129 | typedef typename SimilarTypeTraits<P>::Type Type; |
| 130 | return ParamTraits<Type>::Read(m, iter, reinterpret_cast<Type* >(p)); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | template <class P> |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 134 | static inline void LogParam(const P& p, std::string* l) { |
[email protected] | 7b291f9 | 2009-08-14 05:43:53 | [diff] [blame] | 135 | typedef typename SimilarTypeTraits<P>::Type Type; |
[email protected] | 46ce5b56 | 2010-06-16 18:39:53 | [diff] [blame] | 136 | ParamTraits<Type>::Log(static_cast<const Type& >(p), l); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | template <> |
| 140 | struct ParamTraits<bool> { |
| 141 | typedef bool param_type; |
| 142 | static void Write(Message* m, const param_type& p) { |
| 143 | m->WriteBool(p); |
| 144 | } |
| 145 | static bool Read(const Message* m, void** iter, param_type* r) { |
| 146 | return m->ReadBool(iter, r); |
| 147 | } |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 148 | static void Log(const param_type& p, std::string* l) { |
| 149 | l->append(p ? "true" : "false"); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 150 | } |
| 151 | }; |
| 152 | |
| 153 | template <> |
| 154 | struct ParamTraits<int> { |
| 155 | typedef int param_type; |
| 156 | static void Write(Message* m, const param_type& p) { |
| 157 | m->WriteInt(p); |
| 158 | } |
| 159 | static bool Read(const Message* m, void** iter, param_type* r) { |
| 160 | return m->ReadInt(iter, r); |
| 161 | } |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 162 | static void Log(const param_type& p, std::string* l); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 163 | }; |
| 164 | |
| 165 | template <> |
[email protected] | 63263f9 | 2009-07-28 19:35:08 | [diff] [blame] | 166 | struct ParamTraits<unsigned int> { |
| 167 | typedef unsigned int param_type; |
| 168 | static void Write(Message* m, const param_type& p) { |
| 169 | m->WriteInt(p); |
| 170 | } |
| 171 | static bool Read(const Message* m, void** iter, param_type* r) { |
| 172 | return m->ReadInt(iter, reinterpret_cast<int*>(r)); |
| 173 | } |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 174 | static void Log(const param_type& p, std::string* l); |
[email protected] | 63263f9 | 2009-07-28 19:35:08 | [diff] [blame] | 175 | }; |
| 176 | |
| 177 | template <> |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 178 | struct ParamTraits<long> { |
| 179 | typedef long param_type; |
| 180 | static void Write(Message* m, const param_type& p) { |
| 181 | m->WriteLong(p); |
| 182 | } |
| 183 | static bool Read(const Message* m, void** iter, param_type* r) { |
| 184 | return m->ReadLong(iter, r); |
| 185 | } |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 186 | static void Log(const param_type& p, std::string* l); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 187 | }; |
| 188 | |
[email protected] | 140c303 | 2009-06-26 18:22:54 | [diff] [blame] | 189 | template <> |
| 190 | struct ParamTraits<unsigned long> { |
| 191 | typedef unsigned long param_type; |
| 192 | static void Write(Message* m, const param_type& p) { |
| 193 | m->WriteLong(p); |
| 194 | } |
| 195 | static bool Read(const Message* m, void** iter, param_type* r) { |
[email protected] | 63263f9 | 2009-07-28 19:35:08 | [diff] [blame] | 196 | return m->ReadLong(iter, reinterpret_cast<long*>(r)); |
[email protected] | 140c303 | 2009-06-26 18:22:54 | [diff] [blame] | 197 | } |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 198 | static void Log(const param_type& p, std::string* l); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 199 | }; |
| 200 | |
| 201 | template <> |
[email protected] | 63263f9 | 2009-07-28 19:35:08 | [diff] [blame] | 202 | struct ParamTraits<long long> { |
| 203 | typedef long long param_type; |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 204 | static void Write(Message* m, const param_type& p) { |
| 205 | m->WriteInt64(static_cast<int64>(p)); |
| 206 | } |
| 207 | static bool Read(const Message* m, void** iter, param_type* r) { |
| 208 | return m->ReadInt64(iter, reinterpret_cast<int64*>(r)); |
| 209 | } |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 210 | static void Log(const param_type& p, std::string* l); |
[email protected] | 63263f9 | 2009-07-28 19:35:08 | [diff] [blame] | 211 | }; |
| 212 | |
| 213 | template <> |
| 214 | struct ParamTraits<unsigned long long> { |
| 215 | typedef unsigned long long param_type; |
| 216 | static void Write(Message* m, const param_type& p) { |
| 217 | m->WriteInt64(p); |
| 218 | } |
| 219 | static bool Read(const Message* m, void** iter, param_type* r) { |
| 220 | return m->ReadInt64(iter, reinterpret_cast<int64*>(r)); |
| 221 | } |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 222 | static void Log(const param_type& p, std::string* l); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 223 | }; |
| 224 | |
[email protected] | 43a4020 | 2010-11-12 16:25:01 | [diff] [blame] | 225 | template <> |
| 226 | struct ParamTraits<unsigned short> { |
| 227 | typedef unsigned short param_type; |
| 228 | static void Write(Message* m, const param_type& p); |
| 229 | static bool Read(const Message* m, void** iter, param_type* r); |
| 230 | static void Log(const param_type& p, std::string* l); |
| 231 | }; |
| 232 | |
[email protected] | 2019966 | 2010-06-17 03:29:26 | [diff] [blame] | 233 | // Note that the IPC layer doesn't sanitize NaNs and +/- INF values. Clients |
| 234 | // should be sure to check the sanity of these values after receiving them over |
| 235 | // IPC. |
| 236 | template <> |
| 237 | struct ParamTraits<float> { |
| 238 | typedef float param_type; |
| 239 | static void Write(Message* m, const param_type& p) { |
| 240 | m->WriteData(reinterpret_cast<const char*>(&p), sizeof(param_type)); |
| 241 | } |
| 242 | static bool Read(const Message* m, void** iter, param_type* r) { |
| 243 | const char *data; |
| 244 | int data_size; |
| 245 | if (!m->ReadData(iter, &data, &data_size) || |
| 246 | data_size != sizeof(param_type)) { |
| 247 | NOTREACHED(); |
| 248 | return false; |
| 249 | } |
| 250 | memcpy(r, data, sizeof(param_type)); |
| 251 | return true; |
| 252 | } |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 253 | static void Log(const param_type& p, std::string* l) { |
| 254 | l->append(StringPrintf("%e", p)); |
[email protected] | 2019966 | 2010-06-17 03:29:26 | [diff] [blame] | 255 | } |
| 256 | }; |
| 257 | |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 258 | template <> |
| 259 | struct ParamTraits<double> { |
| 260 | typedef double param_type; |
| 261 | static void Write(Message* m, const param_type& p) { |
| 262 | m->WriteData(reinterpret_cast<const char*>(&p), sizeof(param_type)); |
| 263 | } |
| 264 | static bool Read(const Message* m, void** iter, param_type* r) { |
| 265 | const char *data; |
[email protected] | 2019966 | 2010-06-17 03:29:26 | [diff] [blame] | 266 | int data_size; |
| 267 | if (!m->ReadData(iter, &data, &data_size) || |
| 268 | data_size != sizeof(param_type)) { |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 269 | NOTREACHED(); |
[email protected] | 2019966 | 2010-06-17 03:29:26 | [diff] [blame] | 270 | return false; |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 271 | } |
[email protected] | 2019966 | 2010-06-17 03:29:26 | [diff] [blame] | 272 | memcpy(r, data, sizeof(param_type)); |
| 273 | return true; |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 274 | } |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 275 | static void Log(const param_type& p, std::string* l) { |
| 276 | l->append(StringPrintf("%e", p)); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 277 | } |
| 278 | }; |
| 279 | |
| 280 | template <> |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 281 | struct ParamTraits<base::Time> { |
| 282 | typedef base::Time param_type; |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 283 | static void Write(Message* m, const param_type& p); |
| 284 | static bool Read(const Message* m, void** iter, param_type* r); |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 285 | static void Log(const param_type& p, std::string* l); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 286 | }; |
| 287 | |
[email protected] | d84e48b | 2010-10-21 22:04:52 | [diff] [blame] | 288 | template <> |
| 289 | struct ParamTraits<base::TimeDelta> { |
| 290 | typedef base::TimeDelta param_type; |
| 291 | static void Write(Message* m, const param_type& p); |
| 292 | static bool Read(const Message* m, void** iter, param_type* r); |
| 293 | static void Log(const param_type& p, std::string* l); |
| 294 | }; |
| 295 | |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 296 | #if defined(OS_WIN) |
| 297 | template <> |
| 298 | struct ParamTraits<LOGFONT> { |
| 299 | typedef LOGFONT param_type; |
| 300 | static void Write(Message* m, const param_type& p) { |
| 301 | m->WriteData(reinterpret_cast<const char*>(&p), sizeof(LOGFONT)); |
| 302 | } |
| 303 | static bool Read(const Message* m, void** iter, param_type* r) { |
| 304 | const char *data; |
| 305 | int data_size = 0; |
| 306 | bool result = m->ReadData(iter, &data, &data_size); |
| 307 | if (result && data_size == sizeof(LOGFONT)) { |
| 308 | memcpy(r, data, sizeof(LOGFONT)); |
| 309 | } else { |
| 310 | result = false; |
| 311 | NOTREACHED(); |
| 312 | } |
| 313 | |
| 314 | return result; |
| 315 | } |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 316 | static void Log(const param_type& p, std::string* l) { |
| 317 | l->append(StringPrintf("<LOGFONT>")); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 318 | } |
| 319 | }; |
| 320 | |
| 321 | template <> |
| 322 | struct ParamTraits<MSG> { |
| 323 | typedef MSG param_type; |
| 324 | static void Write(Message* m, const param_type& p) { |
| 325 | m->WriteData(reinterpret_cast<const char*>(&p), sizeof(MSG)); |
| 326 | } |
| 327 | static bool Read(const Message* m, void** iter, param_type* r) { |
| 328 | const char *data; |
| 329 | int data_size = 0; |
| 330 | bool result = m->ReadData(iter, &data, &data_size); |
| 331 | if (result && data_size == sizeof(MSG)) { |
| 332 | memcpy(r, data, sizeof(MSG)); |
| 333 | } else { |
| 334 | result = false; |
| 335 | NOTREACHED(); |
| 336 | } |
| 337 | |
| 338 | return result; |
| 339 | } |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 340 | static void Log(const param_type& p, std::string* l) { |
| 341 | l->append("<MSG>"); |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 342 | } |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 343 | }; |
| 344 | #endif // defined(OS_WIN) |
| 345 | |
| 346 | template <> |
[email protected] | 584f2b2 | 2009-05-21 01:01:59 | [diff] [blame] | 347 | struct ParamTraits<DictionaryValue> { |
| 348 | typedef DictionaryValue param_type; |
| 349 | static void Write(Message* m, const param_type& p); |
| 350 | static bool Read(const Message* m, void** iter, param_type* r); |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 351 | static void Log(const param_type& p, std::string* l); |
[email protected] | 584f2b2 | 2009-05-21 01:01:59 | [diff] [blame] | 352 | }; |
| 353 | |
| 354 | template <> |
| 355 | struct ParamTraits<ListValue> { |
| 356 | typedef ListValue param_type; |
| 357 | static void Write(Message* m, const param_type& p); |
| 358 | static bool Read(const Message* m, void** iter, param_type* r); |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 359 | static void Log(const param_type& p, std::string* l); |
[email protected] | 584f2b2 | 2009-05-21 01:01:59 | [diff] [blame] | 360 | }; |
| 361 | |
| 362 | template <> |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 363 | struct ParamTraits<std::string> { |
| 364 | typedef std::string param_type; |
| 365 | static void Write(Message* m, const param_type& p) { |
| 366 | m->WriteString(p); |
| 367 | } |
| 368 | static bool Read(const Message* m, void** iter, param_type* r) { |
| 369 | return m->ReadString(iter, r); |
| 370 | } |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 371 | static void Log(const param_type& p, std::string* l) { |
| 372 | l->append(p); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 373 | } |
| 374 | }; |
| 375 | |
[email protected] | 3dd7a7a | 2009-07-27 21:09:07 | [diff] [blame] | 376 | template<typename CharType> |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 377 | static void LogBytes(const std::vector<CharType>& data, std::string* out) { |
[email protected] | 3dd7a7a | 2009-07-27 21:09:07 | [diff] [blame] | 378 | #if defined(OS_WIN) |
| 379 | // Windows has a GUI for logging, which can handle arbitrary binary data. |
| 380 | for (size_t i = 0; i < data.size(); ++i) |
| 381 | out->push_back(data[i]); |
| 382 | #else |
| 383 | // On POSIX, we log to stdout, which we assume can display ASCII. |
| 384 | static const size_t kMaxBytesToLog = 100; |
| 385 | for (size_t i = 0; i < std::min(data.size(), kMaxBytesToLog); ++i) { |
| 386 | if (isprint(data[i])) |
| 387 | out->push_back(data[i]); |
| 388 | else |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 389 | out->append(StringPrintf("[%02X]", static_cast<unsigned char>(data[i]))); |
[email protected] | 3dd7a7a | 2009-07-27 21:09:07 | [diff] [blame] | 390 | } |
| 391 | if (data.size() > kMaxBytesToLog) { |
| 392 | out->append( |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 393 | StringPrintf(" and %u more bytes", |
[email protected] | 3dd7a7a | 2009-07-27 21:09:07 | [diff] [blame] | 394 | static_cast<unsigned>(data.size() - kMaxBytesToLog))); |
| 395 | } |
| 396 | #endif |
| 397 | } |
| 398 | |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 399 | template <> |
| 400 | struct ParamTraits<std::vector<unsigned char> > { |
| 401 | typedef std::vector<unsigned char> param_type; |
| 402 | static void Write(Message* m, const param_type& p) { |
| 403 | if (p.size() == 0) { |
| 404 | m->WriteData(NULL, 0); |
| 405 | } else { |
| 406 | m->WriteData(reinterpret_cast<const char*>(&p.front()), |
| 407 | static_cast<int>(p.size())); |
| 408 | } |
| 409 | } |
| 410 | static bool Read(const Message* m, void** iter, param_type* r) { |
| 411 | const char *data; |
| 412 | int data_size = 0; |
| 413 | if (!m->ReadData(iter, &data, &data_size) || data_size < 0) |
| 414 | return false; |
| 415 | r->resize(data_size); |
| 416 | if (data_size) |
| 417 | memcpy(&r->front(), data, data_size); |
| 418 | return true; |
| 419 | } |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 420 | static void Log(const param_type& p, std::string* l) { |
[email protected] | 3dd7a7a | 2009-07-27 21:09:07 | [diff] [blame] | 421 | LogBytes(p, l); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 422 | } |
| 423 | }; |
| 424 | |
| 425 | template <> |
| 426 | struct ParamTraits<std::vector<char> > { |
| 427 | typedef std::vector<char> param_type; |
| 428 | static void Write(Message* m, const param_type& p) { |
| 429 | if (p.size() == 0) { |
| 430 | m->WriteData(NULL, 0); |
| 431 | } else { |
| 432 | m->WriteData(&p.front(), static_cast<int>(p.size())); |
| 433 | } |
| 434 | } |
| 435 | static bool Read(const Message* m, void** iter, param_type* r) { |
| 436 | const char *data; |
| 437 | int data_size = 0; |
| 438 | if (!m->ReadData(iter, &data, &data_size) || data_size < 0) |
| 439 | return false; |
| 440 | r->resize(data_size); |
| 441 | if (data_size) |
| 442 | memcpy(&r->front(), data, data_size); |
| 443 | return true; |
| 444 | } |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 445 | static void Log(const param_type& p, std::string* l) { |
[email protected] | 3dd7a7a | 2009-07-27 21:09:07 | [diff] [blame] | 446 | LogBytes(p, l); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 447 | } |
| 448 | }; |
| 449 | |
| 450 | template <class P> |
| 451 | struct ParamTraits<std::vector<P> > { |
| 452 | typedef std::vector<P> param_type; |
| 453 | static void Write(Message* m, const param_type& p) { |
| 454 | WriteParam(m, static_cast<int>(p.size())); |
| 455 | for (size_t i = 0; i < p.size(); i++) |
| 456 | WriteParam(m, p[i]); |
| 457 | } |
| 458 | static bool Read(const Message* m, void** iter, param_type* r) { |
| 459 | int size; |
[email protected] | 86440f5 | 2009-12-31 05:17:23 | [diff] [blame] | 460 | // ReadLength() checks for < 0 itself. |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 461 | if (!m->ReadLength(iter, &size)) |
| 462 | return false; |
| 463 | // Resizing beforehand is not safe, see BUG 1006367 for details. |
[email protected] | 86440f5 | 2009-12-31 05:17:23 | [diff] [blame] | 464 | if (INT_MAX / sizeof(P) <= static_cast<size_t>(size)) |
| 465 | return false; |
| 466 | r->resize(size); |
| 467 | for (int i = 0; i < size; i++) { |
| 468 | if (!ReadParam(m, iter, &(*r)[i])) |
| 469 | return false; |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 470 | } |
| 471 | return true; |
| 472 | } |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 473 | static void Log(const param_type& p, std::string* l) { |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 474 | for (size_t i = 0; i < p.size(); ++i) { |
| 475 | if (i != 0) |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 476 | l->append(" "); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 477 | LogParam((p[i]), l); |
| 478 | } |
| 479 | } |
| 480 | }; |
| 481 | |
[email protected] | 96da696 | 2010-05-13 19:10:34 | [diff] [blame] | 482 | template <class P> |
| 483 | struct ParamTraits<std::set<P> > { |
| 484 | typedef std::set<P> param_type; |
| 485 | static void Write(Message* m, const param_type& p) { |
| 486 | WriteParam(m, static_cast<int>(p.size())); |
| 487 | typename param_type::const_iterator iter; |
| 488 | for (iter = p.begin(); iter != p.end(); ++iter) |
| 489 | WriteParam(m, *iter); |
| 490 | } |
| 491 | static bool Read(const Message* m, void** iter, param_type* r) { |
| 492 | int size; |
| 493 | if (!m->ReadLength(iter, &size)) |
| 494 | return false; |
| 495 | for (int i = 0; i < size; ++i) { |
| 496 | P item; |
| 497 | if (!ReadParam(m, iter, &item)) |
| 498 | return false; |
| 499 | r->insert(item); |
| 500 | } |
| 501 | return true; |
| 502 | } |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 503 | static void Log(const param_type& p, std::string* l) { |
| 504 | l->append("<std::set>"); |
[email protected] | 96da696 | 2010-05-13 19:10:34 | [diff] [blame] | 505 | } |
| 506 | }; |
| 507 | |
| 508 | |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 509 | template <class K, class V> |
| 510 | struct ParamTraits<std::map<K, V> > { |
| 511 | typedef std::map<K, V> param_type; |
| 512 | static void Write(Message* m, const param_type& p) { |
| 513 | WriteParam(m, static_cast<int>(p.size())); |
| 514 | typename param_type::const_iterator iter; |
| 515 | for (iter = p.begin(); iter != p.end(); ++iter) { |
| 516 | WriteParam(m, iter->first); |
| 517 | WriteParam(m, iter->second); |
| 518 | } |
| 519 | } |
| 520 | static bool Read(const Message* m, void** iter, param_type* r) { |
| 521 | int size; |
| 522 | if (!ReadParam(m, iter, &size) || size < 0) |
| 523 | return false; |
| 524 | for (int i = 0; i < size; ++i) { |
| 525 | K k; |
| 526 | if (!ReadParam(m, iter, &k)) |
| 527 | return false; |
| 528 | V& value = (*r)[k]; |
| 529 | if (!ReadParam(m, iter, &value)) |
| 530 | return false; |
| 531 | } |
| 532 | return true; |
| 533 | } |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 534 | static void Log(const param_type& p, std::string* l) { |
| 535 | l->append("<std::map>"); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 536 | } |
| 537 | }; |
| 538 | |
[email protected] | eb47a13 | 2009-03-04 00:39:56 | [diff] [blame] | 539 | |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 540 | template <> |
| 541 | struct ParamTraits<std::wstring> { |
| 542 | typedef std::wstring param_type; |
| 543 | static void Write(Message* m, const param_type& p) { |
| 544 | m->WriteWString(p); |
| 545 | } |
| 546 | static bool Read(const Message* m, void** iter, param_type* r) { |
| 547 | return m->ReadWString(iter, r); |
| 548 | } |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 549 | static void Log(const param_type& p, std::string* l); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 550 | }; |
| 551 | |
[email protected] | a5da6d61 | 2009-08-04 02:00:56 | [diff] [blame] | 552 | template <class A, class B> |
| 553 | struct ParamTraits<std::pair<A, B> > { |
| 554 | typedef std::pair<A, B> param_type; |
| 555 | static void Write(Message* m, const param_type& p) { |
| 556 | WriteParam(m, p.first); |
| 557 | WriteParam(m, p.second); |
| 558 | } |
| 559 | static bool Read(const Message* m, void** iter, param_type* r) { |
| 560 | return ReadParam(m, iter, &r->first) && ReadParam(m, iter, &r->second); |
| 561 | } |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 562 | static void Log(const param_type& p, std::string* l) { |
| 563 | l->append("("); |
[email protected] | a5da6d61 | 2009-08-04 02:00:56 | [diff] [blame] | 564 | LogParam(p.first, l); |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 565 | l->append(", "); |
[email protected] | a5da6d61 | 2009-08-04 02:00:56 | [diff] [blame] | 566 | LogParam(p.second, l); |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 567 | l->append(")"); |
[email protected] | a5da6d61 | 2009-08-04 02:00:56 | [diff] [blame] | 568 | } |
| 569 | }; |
| 570 | |
[email protected] | 15bf871 | 2009-08-27 00:55:02 | [diff] [blame] | 571 | template <> |
| 572 | struct ParamTraits<NullableString16> { |
| 573 | typedef NullableString16 param_type; |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 574 | static void Write(Message* m, const param_type& p); |
| 575 | static bool Read(const Message* m, void** iter, param_type* r); |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 576 | static void Log(const param_type& p, std::string* l); |
[email protected] | 15bf871 | 2009-08-27 00:55:02 | [diff] [blame] | 577 | }; |
| 578 | |
[email protected] | eb47a13 | 2009-03-04 00:39:56 | [diff] [blame] | 579 | // If WCHAR_T_IS_UTF16 is defined, then string16 is a std::wstring so we don't |
| 580 | // need this trait. |
| 581 | #if !defined(WCHAR_T_IS_UTF16) |
[email protected] | eb47a13 | 2009-03-04 00:39:56 | [diff] [blame] | 582 | template <> |
| 583 | struct ParamTraits<string16> { |
| 584 | typedef string16 param_type; |
| 585 | static void Write(Message* m, const param_type& p) { |
[email protected] | 3a2a5d2 | 2009-03-04 03:36:36 | [diff] [blame] | 586 | m->WriteString16(p); |
[email protected] | eb47a13 | 2009-03-04 00:39:56 | [diff] [blame] | 587 | } |
| 588 | static bool Read(const Message* m, void** iter, param_type* r) { |
[email protected] | 3a2a5d2 | 2009-03-04 03:36:36 | [diff] [blame] | 589 | return m->ReadString16(iter, r); |
[email protected] | eb47a13 | 2009-03-04 00:39:56 | [diff] [blame] | 590 | } |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 591 | static void Log(const param_type& p, std::string* l); |
[email protected] | eb47a13 | 2009-03-04 00:39:56 | [diff] [blame] | 592 | }; |
[email protected] | eb47a13 | 2009-03-04 00:39:56 | [diff] [blame] | 593 | #endif |
| 594 | |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 595 | // and, a few more useful types... |
| 596 | #if defined(OS_WIN) |
| 597 | template <> |
| 598 | struct ParamTraits<HANDLE> { |
| 599 | typedef HANDLE param_type; |
| 600 | static void Write(Message* m, const param_type& p) { |
[email protected] | 3747791 | 2010-02-09 08:06:35 | [diff] [blame] | 601 | // Note that HWNDs/HANDLE/HCURSOR/HACCEL etc are always 32 bits, even on 64 |
| 602 | // bit systems. |
| 603 | m->WriteUInt32(reinterpret_cast<uint32>(p)); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 604 | } |
| 605 | static bool Read(const Message* m, void** iter, param_type* r) { |
[email protected] | 3747791 | 2010-02-09 08:06:35 | [diff] [blame] | 606 | DCHECK_EQ(sizeof(param_type), sizeof(uint32)); |
| 607 | return m->ReadUInt32(iter, reinterpret_cast<uint32*>(r)); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 608 | } |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 609 | static void Log(const param_type& p, std::string* l) { |
| 610 | l->append(StringPrintf("0x%X", p)); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 611 | } |
| 612 | }; |
| 613 | |
| 614 | template <> |
| 615 | struct ParamTraits<HCURSOR> { |
| 616 | typedef HCURSOR param_type; |
| 617 | static void Write(Message* m, const param_type& p) { |
[email protected] | 3747791 | 2010-02-09 08:06:35 | [diff] [blame] | 618 | m->WriteUInt32(reinterpret_cast<uint32>(p)); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 619 | } |
| 620 | static bool Read(const Message* m, void** iter, param_type* r) { |
[email protected] | 3747791 | 2010-02-09 08:06:35 | [diff] [blame] | 621 | DCHECK_EQ(sizeof(param_type), sizeof(uint32)); |
| 622 | return m->ReadUInt32(iter, reinterpret_cast<uint32*>(r)); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 623 | } |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 624 | static void Log(const param_type& p, std::string* l) { |
| 625 | l->append(StringPrintf("0x%X", p)); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 626 | } |
| 627 | }; |
| 628 | |
| 629 | template <> |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 630 | struct ParamTraits<HACCEL> { |
| 631 | typedef HACCEL param_type; |
| 632 | static void Write(Message* m, const param_type& p) { |
[email protected] | 3747791 | 2010-02-09 08:06:35 | [diff] [blame] | 633 | m->WriteUInt32(reinterpret_cast<uint32>(p)); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 634 | } |
| 635 | static bool Read(const Message* m, void** iter, param_type* r) { |
[email protected] | 3747791 | 2010-02-09 08:06:35 | [diff] [blame] | 636 | DCHECK_EQ(sizeof(param_type), sizeof(uint32)); |
| 637 | return m->ReadUInt32(iter, reinterpret_cast<uint32*>(r)); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 638 | } |
| 639 | }; |
| 640 | |
| 641 | template <> |
| 642 | struct ParamTraits<POINT> { |
| 643 | typedef POINT param_type; |
| 644 | static void Write(Message* m, const param_type& p) { |
| 645 | m->WriteInt(p.x); |
| 646 | m->WriteInt(p.y); |
| 647 | } |
| 648 | static bool Read(const Message* m, void** iter, param_type* r) { |
| 649 | int x, y; |
| 650 | if (!m->ReadInt(iter, &x) || !m->ReadInt(iter, &y)) |
| 651 | return false; |
| 652 | r->x = x; |
| 653 | r->y = y; |
| 654 | return true; |
| 655 | } |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 656 | static void Log(const param_type& p, std::string* l) { |
| 657 | l->append(StringPrintf("(%d, %d)", p.x, p.y)); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 658 | } |
| 659 | }; |
| 660 | #endif // defined(OS_WIN) |
| 661 | |
| 662 | template <> |
| 663 | struct ParamTraits<FilePath> { |
| 664 | typedef FilePath param_type; |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 665 | static void Write(Message* m, const param_type& p); |
| 666 | static bool Read(const Message* m, void** iter, param_type* r); |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 667 | static void Log(const param_type& p, std::string* l); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 668 | }; |
| 669 | |
[email protected] | 526776c | 2009-02-07 00:39:26 | [diff] [blame] | 670 | #if defined(OS_POSIX) |
[email protected] | 2749885f | 2009-03-05 21:40:11 | [diff] [blame] | 671 | // FileDescriptors may be serialised over IPC channels on POSIX. On the |
| 672 | // receiving side, the FileDescriptor is a valid duplicate of the file |
| 673 | // descriptor which was transmitted: *it is not just a copy of the integer like |
| 674 | // HANDLEs on Windows*. The only exception is if the file descriptor is < 0. In |
| 675 | // this case, the receiving end will see a value of -1. *Zero is a valid file |
| 676 | // descriptor*. |
| 677 | // |
| 678 | // The received file descriptor will have the |auto_close| flag set to true. The |
| 679 | // code which handles the message is responsible for taking ownership of it. |
| 680 | // File descriptors are OS resources and must be closed when no longer needed. |
| 681 | // |
| 682 | // When sending a file descriptor, the file descriptor must be valid at the time |
| 683 | // of transmission. Since transmission is not synchronous, one should consider |
| 684 | // dup()ing any file descriptors to be transmitted and setting the |auto_close| |
| 685 | // flag, which causes the file descriptor to be closed after writing. |
[email protected] | 526776c | 2009-02-07 00:39:26 | [diff] [blame] | 686 | template<> |
[email protected] | 5fe733de | 2009-02-11 18:59:20 | [diff] [blame] | 687 | struct ParamTraits<base::FileDescriptor> { |
| 688 | typedef base::FileDescriptor param_type; |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 689 | static void Write(Message* m, const param_type& p); |
| 690 | static bool Read(const Message* m, void** iter, param_type* r); |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 691 | static void Log(const param_type& p, std::string* l); |
[email protected] | 526776c | 2009-02-07 00:39:26 | [diff] [blame] | 692 | }; |
[email protected] | 379e7a5 | 2010-03-09 00:38:41 | [diff] [blame] | 693 | #endif // defined(OS_POSIX) |
[email protected] | 526776c | 2009-02-07 00:39:26 | [diff] [blame] | 694 | |
[email protected] | d2e884d | 2009-06-22 20:37:52 | [diff] [blame] | 695 | // A ChannelHandle is basically a platform-inspecific wrapper around the |
| 696 | // fact that IPC endpoints are handled specially on POSIX. See above comments |
| 697 | // on FileDescriptor for more background. |
| 698 | template<> |
| 699 | struct ParamTraits<IPC::ChannelHandle> { |
| 700 | typedef ChannelHandle param_type; |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 701 | static void Write(Message* m, const param_type& p); |
| 702 | static bool Read(const Message* m, void** iter, param_type* r); |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 703 | static void Log(const param_type& p, std::string* l); |
[email protected] | d2e884d | 2009-06-22 20:37:52 | [diff] [blame] | 704 | }; |
| 705 | |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 706 | #if defined(OS_WIN) |
| 707 | template <> |
| 708 | struct ParamTraits<XFORM> { |
| 709 | typedef XFORM param_type; |
| 710 | static void Write(Message* m, const param_type& p) { |
| 711 | m->WriteData(reinterpret_cast<const char*>(&p), sizeof(XFORM)); |
| 712 | } |
| 713 | static bool Read(const Message* m, void** iter, param_type* r) { |
| 714 | const char *data; |
| 715 | int data_size = 0; |
| 716 | bool result = m->ReadData(iter, &data, &data_size); |
| 717 | if (result && data_size == sizeof(XFORM)) { |
| 718 | memcpy(r, data, sizeof(XFORM)); |
| 719 | } else { |
| 720 | result = false; |
| 721 | NOTREACHED(); |
| 722 | } |
| 723 | |
| 724 | return result; |
| 725 | } |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 726 | static void Log(const param_type& p, std::string* l) { |
| 727 | l->append("<XFORM>"); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 728 | } |
| 729 | }; |
| 730 | #endif // defined(OS_WIN) |
| 731 | |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 732 | struct LogData { |
[email protected] | 20f0487a | 2010-09-30 20:06:30 | [diff] [blame] | 733 | LogData(); |
| 734 | ~LogData(); |
| 735 | |
[email protected] | 9a3a293b | 2009-06-04 22:28:16 | [diff] [blame] | 736 | std::string channel; |
[email protected] | 8bef70e | 2009-04-14 19:11:24 | [diff] [blame] | 737 | int32 routing_id; |
[email protected] | 168ae92 | 2009-12-04 18:08:45 | [diff] [blame] | 738 | uint32 type; // "User-defined" message type, from ipc_message.h. |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 739 | std::string flags; |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 740 | int64 sent; // Time that the message was sent (i.e. at Send()). |
| 741 | int64 receive; // Time before it was dispatched (i.e. before calling |
| 742 | // OnMessageReceived). |
| 743 | int64 dispatch; // Time after it was dispatched (i.e. after calling |
| 744 | // OnMessageReceived). |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 745 | std::string message_name; |
| 746 | std::string params; |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 747 | }; |
| 748 | |
| 749 | template <> |
| 750 | struct ParamTraits<LogData> { |
| 751 | typedef LogData param_type; |
[email protected] | 20f0487a | 2010-09-30 20:06:30 | [diff] [blame] | 752 | static void Write(Message* m, const param_type& p); |
| 753 | static bool Read(const Message* m, void** iter, param_type* r); |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 754 | static void Log(const param_type& p, std::string* l) { |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 755 | // Doesn't make sense to implement this! |
| 756 | } |
| 757 | }; |
| 758 | |
[email protected] | eb47a13 | 2009-03-04 00:39:56 | [diff] [blame] | 759 | template <> |
[email protected] | 503683f | 2009-02-26 09:13:01 | [diff] [blame] | 760 | struct ParamTraits<Message> { |
| 761 | static void Write(Message* m, const Message& p) { |
| 762 | m->WriteInt(p.size()); |
| 763 | m->WriteData(reinterpret_cast<const char*>(p.data()), p.size()); |
| 764 | } |
| 765 | static bool Read(const Message* m, void** iter, Message* r) { |
| 766 | int size; |
| 767 | if (!m->ReadInt(iter, &size)) |
| 768 | return false; |
| 769 | const char* data; |
| 770 | if (!m->ReadData(iter, &data, &size)) |
| 771 | return false; |
| 772 | *r = Message(data, size); |
| 773 | return true; |
| 774 | } |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 775 | static void Log(const Message& p, std::string* l) { |
| 776 | l->append("<IPC::Message>"); |
[email protected] | 503683f | 2009-02-26 09:13:01 | [diff] [blame] | 777 | } |
| 778 | }; |
| 779 | |
| 780 | template <> |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 781 | struct ParamTraits<Tuple0> { |
| 782 | typedef Tuple0 param_type; |
| 783 | static void Write(Message* m, const param_type& p) { |
| 784 | } |
| 785 | static bool Read(const Message* m, void** iter, param_type* r) { |
| 786 | return true; |
| 787 | } |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 788 | static void Log(const param_type& p, std::string* l) { |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 789 | } |
| 790 | }; |
| 791 | |
| 792 | template <class A> |
| 793 | struct ParamTraits< Tuple1<A> > { |
| 794 | typedef Tuple1<A> param_type; |
| 795 | static void Write(Message* m, const param_type& p) { |
| 796 | WriteParam(m, p.a); |
| 797 | } |
| 798 | static bool Read(const Message* m, void** iter, param_type* r) { |
| 799 | return ReadParam(m, iter, &r->a); |
| 800 | } |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 801 | static void Log(const param_type& p, std::string* l) { |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 802 | LogParam(p.a, l); |
| 803 | } |
| 804 | }; |
| 805 | |
| 806 | template <class A, class B> |
| 807 | struct ParamTraits< Tuple2<A, B> > { |
| 808 | typedef Tuple2<A, B> param_type; |
| 809 | static void Write(Message* m, const param_type& p) { |
| 810 | WriteParam(m, p.a); |
| 811 | WriteParam(m, p.b); |
| 812 | } |
| 813 | static bool Read(const Message* m, void** iter, param_type* r) { |
| 814 | return (ReadParam(m, iter, &r->a) && |
| 815 | ReadParam(m, iter, &r->b)); |
| 816 | } |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 817 | static void Log(const param_type& p, std::string* l) { |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 818 | LogParam(p.a, l); |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 819 | l->append(", "); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 820 | LogParam(p.b, l); |
| 821 | } |
| 822 | }; |
| 823 | |
| 824 | template <class A, class B, class C> |
| 825 | struct ParamTraits< Tuple3<A, B, C> > { |
| 826 | typedef Tuple3<A, B, C> param_type; |
| 827 | static void Write(Message* m, const param_type& p) { |
| 828 | WriteParam(m, p.a); |
| 829 | WriteParam(m, p.b); |
| 830 | WriteParam(m, p.c); |
| 831 | } |
| 832 | static bool Read(const Message* m, void** iter, param_type* r) { |
| 833 | return (ReadParam(m, iter, &r->a) && |
| 834 | ReadParam(m, iter, &r->b) && |
| 835 | ReadParam(m, iter, &r->c)); |
| 836 | } |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 837 | static void Log(const param_type& p, std::string* l) { |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 838 | LogParam(p.a, l); |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 839 | l->append(", "); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 840 | LogParam(p.b, l); |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 841 | l->append(", "); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 842 | LogParam(p.c, l); |
| 843 | } |
| 844 | }; |
| 845 | |
| 846 | template <class A, class B, class C, class D> |
| 847 | struct ParamTraits< Tuple4<A, B, C, D> > { |
| 848 | typedef Tuple4<A, B, C, D> param_type; |
| 849 | static void Write(Message* m, const param_type& p) { |
| 850 | WriteParam(m, p.a); |
| 851 | WriteParam(m, p.b); |
| 852 | WriteParam(m, p.c); |
| 853 | WriteParam(m, p.d); |
| 854 | } |
| 855 | static bool Read(const Message* m, void** iter, param_type* r) { |
| 856 | return (ReadParam(m, iter, &r->a) && |
| 857 | ReadParam(m, iter, &r->b) && |
| 858 | ReadParam(m, iter, &r->c) && |
| 859 | ReadParam(m, iter, &r->d)); |
| 860 | } |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 861 | static void Log(const param_type& p, std::string* l) { |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 862 | LogParam(p.a, l); |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 863 | l->append(", "); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 864 | LogParam(p.b, l); |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 865 | l->append(", "); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 866 | LogParam(p.c, l); |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 867 | l->append(", "); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 868 | LogParam(p.d, l); |
| 869 | } |
| 870 | }; |
| 871 | |
| 872 | template <class A, class B, class C, class D, class E> |
| 873 | struct ParamTraits< Tuple5<A, B, C, D, E> > { |
| 874 | typedef Tuple5<A, B, C, D, E> param_type; |
| 875 | static void Write(Message* m, const param_type& p) { |
| 876 | WriteParam(m, p.a); |
| 877 | WriteParam(m, p.b); |
| 878 | WriteParam(m, p.c); |
| 879 | WriteParam(m, p.d); |
| 880 | WriteParam(m, p.e); |
| 881 | } |
| 882 | static bool Read(const Message* m, void** iter, param_type* r) { |
| 883 | return (ReadParam(m, iter, &r->a) && |
| 884 | ReadParam(m, iter, &r->b) && |
| 885 | ReadParam(m, iter, &r->c) && |
| 886 | ReadParam(m, iter, &r->d) && |
| 887 | ReadParam(m, iter, &r->e)); |
| 888 | } |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 889 | static void Log(const param_type& p, std::string* l) { |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 890 | LogParam(p.a, l); |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 891 | l->append(", "); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 892 | LogParam(p.b, l); |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 893 | l->append(", "); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 894 | LogParam(p.c, l); |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 895 | l->append(", "); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 896 | LogParam(p.d, l); |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 897 | l->append(", "); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 898 | LogParam(p.e, l); |
| 899 | } |
| 900 | }; |
| 901 | |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 902 | //----------------------------------------------------------------------------- |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 903 | // Generic message subclasses |
| 904 | |
| 905 | // Used for asynchronous messages. |
[email protected] | 81a3441 | 2009-01-05 19:17:24 | [diff] [blame] | 906 | template <class ParamType> |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 907 | class MessageWithTuple : public Message { |
| 908 | public: |
[email protected] | 81a3441 | 2009-01-05 19:17:24 | [diff] [blame] | 909 | typedef ParamType Param; |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 910 | typedef typename TupleTypes<ParamType>::ParamTuple RefParam; |
[email protected] | 81a3441 | 2009-01-05 19:17:24 | [diff] [blame] | 911 | |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 912 | // The constructor and the Read() method's templated implementations are in |
| 913 | // ipc_message_utils_impl.h. The subclass constructor and Log() methods call |
| 914 | // the templated versions of these and make sure there are instantiations in |
| 915 | // those translation units. |
| 916 | MessageWithTuple(int32 routing_id, uint32 type, const RefParam& p); |
[email protected] | 071b4b9 | 2010-08-09 16:06:58 | [diff] [blame] | 917 | |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 918 | static bool Read(const Message* msg, Param* p) IPC_MSG_NOINLINE; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 919 | |
| 920 | // Generic dispatcher. Should cover most cases. |
| 921 | template<class T, class Method> |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 922 | static bool Dispatch(const Message* msg, T* obj, Method func) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 923 | Param p; |
| 924 | if (Read(msg, &p)) { |
| 925 | DispatchToMethod(obj, func, p); |
| 926 | return true; |
| 927 | } |
| 928 | return false; |
| 929 | } |
| 930 | |
| 931 | // The following dispatchers exist for the case where the callback function |
| 932 | // needs the message as well. They assume that "Param" is a type of Tuple |
| 933 | // (except the one arg case, as there is no Tuple1). |
| 934 | template<class T, typename TA> |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 935 | static bool Dispatch(const Message* msg, T* obj, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 936 | void (T::*func)(const Message&, TA)) { |
| 937 | Param p; |
| 938 | if (Read(msg, &p)) { |
[email protected] | c2fe3154 | 2009-05-20 18:24:14 | [diff] [blame] | 939 | (obj->*func)(*msg, p.a); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 940 | return true; |
| 941 | } |
| 942 | return false; |
| 943 | } |
| 944 | |
| 945 | template<class T, typename TA, typename TB> |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 946 | static bool Dispatch(const Message* msg, T* obj, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 947 | void (T::*func)(const Message&, TA, TB)) { |
| 948 | Param p; |
| 949 | if (Read(msg, &p)) { |
| 950 | (obj->*func)(*msg, p.a, p.b); |
| 951 | return true; |
| 952 | } |
| 953 | return false; |
| 954 | } |
| 955 | |
| 956 | template<class T, typename TA, typename TB, typename TC> |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 957 | static bool Dispatch(const Message* msg, T* obj, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 958 | void (T::*func)(const Message&, TA, TB, TC)) { |
| 959 | Param p; |
| 960 | if (Read(msg, &p)) { |
| 961 | (obj->*func)(*msg, p.a, p.b, p.c); |
| 962 | return true; |
| 963 | } |
| 964 | return false; |
| 965 | } |
| 966 | |
| 967 | template<class T, typename TA, typename TB, typename TC, typename TD> |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 968 | static bool Dispatch(const Message* msg, T* obj, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 969 | void (T::*func)(const Message&, TA, TB, TC, TD)) { |
| 970 | Param p; |
| 971 | if (Read(msg, &p)) { |
| 972 | (obj->*func)(*msg, p.a, p.b, p.c, p.d); |
| 973 | return true; |
| 974 | } |
| 975 | return false; |
| 976 | } |
| 977 | |
| 978 | template<class T, typename TA, typename TB, typename TC, typename TD, |
| 979 | typename TE> |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 980 | static bool Dispatch(const Message* msg, T* obj, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 981 | void (T::*func)(const Message&, TA, TB, TC, TD, TE)) { |
| 982 | Param p; |
| 983 | if (Read(msg, &p)) { |
| 984 | (obj->*func)(*msg, p.a, p.b, p.c, p.d, p.e); |
| 985 | return true; |
| 986 | } |
| 987 | return false; |
| 988 | } |
| 989 | |
[email protected] | deb5740 | 2009-02-06 01:35:30 | [diff] [blame] | 990 | // Functions used to do manual unpacking. Only used by the automation code, |
| 991 | // these should go away once that code uses SyncChannel. |
| 992 | template<typename TA, typename TB> |
| 993 | static bool Read(const IPC::Message* msg, TA* a, TB* b) { |
| 994 | ParamType params; |
| 995 | if (!Read(msg, ¶ms)) |
| 996 | return false; |
| 997 | *a = params.a; |
| 998 | *b = params.b; |
| 999 | return true; |
| 1000 | } |
| 1001 | |
| 1002 | template<typename TA, typename TB, typename TC> |
| 1003 | static bool Read(const IPC::Message* msg, TA* a, TB* b, TC* c) { |
| 1004 | ParamType params; |
| 1005 | if (!Read(msg, ¶ms)) |
| 1006 | return false; |
| 1007 | *a = params.a; |
| 1008 | *b = params.b; |
| 1009 | *c = params.c; |
| 1010 | return true; |
| 1011 | } |
| 1012 | |
| 1013 | template<typename TA, typename TB, typename TC, typename TD> |
| 1014 | static bool Read(const IPC::Message* msg, TA* a, TB* b, TC* c, TD* d) { |
| 1015 | ParamType params; |
| 1016 | if (!Read(msg, ¶ms)) |
| 1017 | return false; |
| 1018 | *a = params.a; |
| 1019 | *b = params.b; |
| 1020 | *c = params.c; |
| 1021 | *d = params.d; |
| 1022 | return true; |
| 1023 | } |
| 1024 | |
| 1025 | template<typename TA, typename TB, typename TC, typename TD, typename TE> |
| 1026 | static bool Read(const IPC::Message* msg, TA* a, TB* b, TC* c, TD* d, TE* e) { |
| 1027 | ParamType params; |
| 1028 | if (!Read(msg, ¶ms)) |
| 1029 | return false; |
| 1030 | *a = params.a; |
| 1031 | *b = params.b; |
| 1032 | *c = params.c; |
| 1033 | *d = params.d; |
| 1034 | *e = params.e; |
| 1035 | return true; |
| 1036 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1037 | }; |
| 1038 | |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 1039 | // defined in ipc_logging.cc |
| 1040 | void GenerateLogData(const std::string& channel, const Message& message, |
| 1041 | LogData* data); |
| 1042 | |
| 1043 | |
| 1044 | #if defined(IPC_MESSAGE_LOG_ENABLED) |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 1045 | inline void AddOutputParamsToLog(const Message* msg, std::string* l) { |
| 1046 | const std::string& output_params = msg->output_params(); |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 1047 | if (!l->empty() && !output_params.empty()) |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 1048 | l->append(", "); |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 1049 | |
| 1050 | l->append(output_params); |
| 1051 | } |
| 1052 | |
| 1053 | template <class ReplyParamType> |
| 1054 | inline void LogReplyParamsToMessage(const ReplyParamType& reply_params, |
| 1055 | const Message* msg) { |
| 1056 | if (msg->received_time() != 0) { |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 1057 | std::string output_params; |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 1058 | LogParam(reply_params, &output_params); |
| 1059 | msg->set_output_params(output_params); |
| 1060 | } |
| 1061 | } |
| 1062 | |
| 1063 | inline void ConnectMessageAndReply(const Message* msg, Message* reply) { |
| 1064 | if (msg->sent_time()) { |
| 1065 | // Don't log the sync message after dispatch, as we don't have the |
| 1066 | // output parameters at that point. Instead, save its data and log it |
| 1067 | // with the outgoing reply message when it's sent. |
| 1068 | LogData* data = new LogData; |
| 1069 | GenerateLogData("", *msg, data); |
| 1070 | msg->set_dont_log(); |
| 1071 | reply->set_sync_log_data(data); |
| 1072 | } |
| 1073 | } |
| 1074 | #else |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 1075 | inline void AddOutputParamsToLog(const Message* msg, std::string* l) {} |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 1076 | |
| 1077 | template <class ReplyParamType> |
| 1078 | inline void LogReplyParamsToMessage(const ReplyParamType& reply_params, |
| 1079 | const Message* msg) {} |
| 1080 | |
| 1081 | inline void ConnectMessageAndReply(const Message* msg, Message* reply) {} |
| 1082 | #endif |
| 1083 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1084 | // This class assumes that its template argument is a RefTuple (a Tuple with |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 1085 | // reference elements). This would go into ipc_message_utils_impl.h, but it is |
| 1086 | // also used by chrome_frame. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1087 | template <class RefTuple> |
| 1088 | class ParamDeserializer : public MessageReplyDeserializer { |
| 1089 | public: |
[email protected] | e1981f43 | 2008-08-12 15:22:13 | [diff] [blame] | 1090 | explicit ParamDeserializer(const RefTuple& out) : out_(out) { } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1091 | |
| 1092 | bool SerializeOutputParameters(const IPC::Message& msg, void* iter) { |
| 1093 | return ReadParam(&msg, &iter, &out_); |
| 1094 | } |
| 1095 | |
| 1096 | RefTuple out_; |
| 1097 | }; |
| 1098 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1099 | // Used for synchronous messages. |
[email protected] | 75e5a87 | 2009-04-02 23:56:11 | [diff] [blame] | 1100 | template <class SendParamType, class ReplyParamType> |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1101 | class MessageWithReply : public SyncMessage { |
| 1102 | public: |
[email protected] | 75e5a87 | 2009-04-02 23:56:11 | [diff] [blame] | 1103 | typedef SendParamType SendParam; |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 1104 | typedef typename TupleTypes<SendParam>::ParamTuple RefSendParam; |
[email protected] | 75e5a87 | 2009-04-02 23:56:11 | [diff] [blame] | 1105 | typedef ReplyParamType ReplyParam; |
| 1106 | |
[email protected] | 168ae92 | 2009-12-04 18:08:45 | [diff] [blame] | 1107 | MessageWithReply(int32 routing_id, uint32 type, |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 1108 | const RefSendParam& send, const ReplyParam& reply); |
| 1109 | static bool ReadSendParam(const Message* msg, SendParam* p) IPC_MSG_NOINLINE; |
| 1110 | static bool ReadReplyParam( |
| 1111 | const Message* msg, |
| 1112 | typename TupleTypes<ReplyParam>::ValueTuple* p) IPC_MSG_NOINLINE; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1113 | |
| 1114 | template<class T, class Method> |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 1115 | static bool Dispatch(const Message* msg, T* obj, Method func) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1116 | SendParam send_params; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1117 | Message* reply = GenerateReply(msg); |
| 1118 | bool error; |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 1119 | if (ReadSendParam(msg, &send_params)) { |
| 1120 | typename TupleTypes<ReplyParam>::ValueTuple reply_params; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1121 | DispatchToMethod(obj, func, send_params, &reply_params); |
| 1122 | WriteParam(reply, reply_params); |
| 1123 | error = false; |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 1124 | LogReplyParamsToMessage(reply_params, msg); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1125 | } else { |
| 1126 | NOTREACHED() << "Error deserializing message " << msg->type(); |
| 1127 | reply->set_reply_error(); |
| 1128 | error = true; |
| 1129 | } |
| 1130 | |
| 1131 | obj->Send(reply); |
| 1132 | return !error; |
| 1133 | } |
| 1134 | |
| 1135 | template<class T, class Method> |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 1136 | static bool DispatchDelayReply(const Message* msg, T* obj, Method func) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1137 | SendParam send_params; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1138 | Message* reply = GenerateReply(msg); |
| 1139 | bool error; |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 1140 | if (ReadSendParam(msg, &send_params)) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1141 | Tuple1<Message&> t = MakeRefTuple(*reply); |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 1142 | ConnectMessageAndReply(msg, reply); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1143 | DispatchToMethod(obj, func, send_params, &t); |
| 1144 | error = false; |
| 1145 | } else { |
| 1146 | NOTREACHED() << "Error deserializing message " << msg->type(); |
| 1147 | reply->set_reply_error(); |
| 1148 | obj->Send(reply); |
| 1149 | error = true; |
| 1150 | } |
| 1151 | return !error; |
| 1152 | } |
| 1153 | |
| 1154 | template<typename TA> |
| 1155 | static void WriteReplyParams(Message* reply, TA a) { |
| 1156 | ReplyParam p(a); |
| 1157 | WriteParam(reply, p); |
| 1158 | } |
| 1159 | |
| 1160 | template<typename TA, typename TB> |
| 1161 | static void WriteReplyParams(Message* reply, TA a, TB b) { |
| 1162 | ReplyParam p(a, b); |
| 1163 | WriteParam(reply, p); |
| 1164 | } |
| 1165 | |
| 1166 | template<typename TA, typename TB, typename TC> |
| 1167 | static void WriteReplyParams(Message* reply, TA a, TB b, TC c) { |
| 1168 | ReplyParam p(a, b, c); |
| 1169 | WriteParam(reply, p); |
| 1170 | } |
| 1171 | |
| 1172 | template<typename TA, typename TB, typename TC, typename TD> |
| 1173 | static void WriteReplyParams(Message* reply, TA a, TB b, TC c, TD d) { |
| 1174 | ReplyParam p(a, b, c, d); |
| 1175 | WriteParam(reply, p); |
| 1176 | } |
| 1177 | |
| 1178 | template<typename TA, typename TB, typename TC, typename TD, typename TE> |
| 1179 | static void WriteReplyParams(Message* reply, TA a, TB b, TC c, TD d, TE e) { |
| 1180 | ReplyParam p(a, b, c, d, e); |
| 1181 | WriteParam(reply, p); |
| 1182 | } |
| 1183 | }; |
| 1184 | |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 1185 | //----------------------------------------------------------------------------- |
| 1186 | |
[email protected] | 3178f4e2 | 2008-08-05 21:20:41 | [diff] [blame] | 1187 | } // namespace IPC |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1188 | |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 1189 | #endif // IPC_IPC_MESSAGE_UTILS_H_ |