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