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