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