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