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