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