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