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