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