jochen@chromium.org | 30c8e91 | 2012-01-29 19:57:33 +0900 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef IPC_IPC_MESSAGE_UTILS_H_ |
| 6 | #define IPC_IPC_MESSAGE_UTILS_H_ |
| 7 | |
tfarina | 1cbfa08 | 2015-09-05 03:47:57 +0900 | [diff] [blame] | 8 | #include <stdint.h> |
| 9 | |
jhawkins@chromium.org | 63c75eb | 2010-03-09 09:38:41 +0900 | [diff] [blame] | 10 | #include <algorithm> |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 11 | #include <map> |
mirandac@chromium.org | ee262ad | 2010-05-14 04:10:34 +0900 | [diff] [blame] | 12 | #include <set> |
tfarina@chromium.org | 439aef6 | 2011-03-21 10:07:01 +0900 | [diff] [blame] | 13 | #include <string> |
| 14 | #include <vector> |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 15 | |
jdduke@chromium.org | 556db8b | 2014-03-19 05:48:35 +0900 | [diff] [blame] | 16 | #include "base/containers/small_map.h" |
miletus | d41fe22 | 2015-07-24 04:42:36 +0900 | [diff] [blame] | 17 | #include "base/containers/stack_container.h" |
rvargas@chromium.org | 9e469f6 | 2014-01-28 06:36:00 +0900 | [diff] [blame] | 18 | #include "base/files/file.h" |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 19 | #include "base/format_macros.h" |
jdduke@chromium.org | c6d80f0 | 2014-05-20 11:34:56 +0900 | [diff] [blame] | 20 | #include "base/memory/scoped_ptr.h" |
hashimoto@chromium.org | bbf7c50 | 2012-11-14 15:35:52 +0900 | [diff] [blame] | 21 | #include "base/memory/scoped_vector.h" |
avi@chromium.org | e7eaf39 | 2013-06-11 15:32:18 +0900 | [diff] [blame] | 22 | #include "base/strings/string16.h" |
| 23 | #include "base/strings/string_util.h" |
| 24 | #include "base/strings/stringprintf.h" |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 25 | #include "base/tuple.h" |
erikchen | fdd43fe | 2015-07-08 07:13:11 +0900 | [diff] [blame] | 26 | #include "ipc/brokerable_attachment.h" |
tsepez@chromium.org | 4afe8f7 | 2012-10-24 08:03:35 +0900 | [diff] [blame] | 27 | #include "ipc/ipc_message_start.h" |
erg@google.com | b16c8a6 | 2010-08-25 05:29:02 +0900 | [diff] [blame] | 28 | #include "ipc/ipc_param_traits.h" |
erg@google.com | bfae859 | 2010-08-17 09:24:54 +0900 | [diff] [blame] | 29 | #include "ipc/ipc_sync_message.h" |
erg@google.com | cab37e7 | 2010-08-14 05:43:58 +0900 | [diff] [blame] | 30 | |
erg@google.com | e6ffcb5 | 2010-08-18 03:38:24 +0900 | [diff] [blame] | 31 | #if defined(COMPILER_GCC) |
| 32 | // GCC "helpfully" tries to inline template methods in release mode. Except we |
| 33 | // want the majority of the template junk being expanded once in the |
| 34 | // implementation file (and only provide the definitions in |
| 35 | // ipc_message_utils_impl.h in those files) and exported, instead of expanded |
| 36 | // at every call site. Special note: GCC happily accepts the attribute before |
| 37 | // the method declaration, but only acts on it if it is after. |
erg@google.com | cb9c43f | 2010-08-20 05:23:25 +0900 | [diff] [blame] | 38 | #if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100) >= 40500 |
| 39 | // Starting in gcc 4.5, the noinline no longer implies the concept covered by |
| 40 | // the introduced noclone attribute, which will create specialized versions of |
| 41 | // functions/methods when certain types are constant. |
| 42 | // www.gnu.org/software/gcc/gcc-4.5/changes.html |
| 43 | #define IPC_MSG_NOINLINE __attribute__((noinline, noclone)); |
| 44 | #else |
erg@google.com | e6ffcb5 | 2010-08-18 03:38:24 +0900 | [diff] [blame] | 45 | #define IPC_MSG_NOINLINE __attribute__((noinline)); |
erg@google.com | cb9c43f | 2010-08-20 05:23:25 +0900 | [diff] [blame] | 46 | #endif |
erg@google.com | e6ffcb5 | 2010-08-18 03:38:24 +0900 | [diff] [blame] | 47 | #elif defined(COMPILER_MSVC) |
| 48 | // MSVC++ doesn't do this. |
| 49 | #define IPC_MSG_NOINLINE |
| 50 | #else |
| 51 | #error "Please add the noinline property for your new compiler here." |
| 52 | #endif |
| 53 | |
erg@google.com | e6ffcb5 | 2010-08-18 03:38:24 +0900 | [diff] [blame] | 54 | namespace base { |
dmazzoni@chromium.org | 13e5365 | 2011-07-13 04:15:03 +0900 | [diff] [blame] | 55 | class DictionaryValue; |
brettw@chromium.org | a708694 | 2013-02-02 14:12:33 +0900 | [diff] [blame] | 56 | class FilePath; |
dmazzoni@chromium.org | 13e5365 | 2011-07-13 04:15:03 +0900 | [diff] [blame] | 57 | class ListValue; |
avi@chromium.org | d817965 | 2013-06-13 22:47:46 +0900 | [diff] [blame] | 58 | class NullableString16; |
erg@google.com | e6ffcb5 | 2010-08-18 03:38:24 +0900 | [diff] [blame] | 59 | class Time; |
apatrick@chromium.org | 519c1c6 | 2010-10-22 07:04:52 +0900 | [diff] [blame] | 60 | class TimeDelta; |
isherman@chromium.org | 9952aaf | 2011-09-03 05:42:04 +0900 | [diff] [blame] | 61 | class TimeTicks; |
erg@google.com | e6ffcb5 | 2010-08-18 03:38:24 +0900 | [diff] [blame] | 62 | struct FileDescriptor; |
scottmg | 8149127 | 2015-06-20 07:51:00 +0900 | [diff] [blame] | 63 | |
erikchen | 18430e5 | 2015-09-26 07:34:31 +0900 | [diff] [blame] | 64 | #if (defined(OS_MACOSX) && !defined(OS_IOS)) || defined(OS_WIN) |
scottmg | 8149127 | 2015-06-20 07:51:00 +0900 | [diff] [blame] | 65 | class SharedMemoryHandle; |
erikchen | 18430e5 | 2015-09-26 07:34:31 +0900 | [diff] [blame] | 66 | #endif // (defined(OS_MACOSX) && !defined(OS_IOS)) || defined(OS_WIN) |
erg@google.com | e6ffcb5 | 2010-08-18 03:38:24 +0900 | [diff] [blame] | 67 | } |
| 68 | |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 69 | namespace IPC { |
| 70 | |
erg@google.com | e6ffcb5 | 2010-08-18 03:38:24 +0900 | [diff] [blame] | 71 | struct ChannelHandle; |
| 72 | |
brettw@chromium.org | ce352e5 | 2012-06-05 06:18:25 +0900 | [diff] [blame] | 73 | // ----------------------------------------------------------------------------- |
| 74 | // How we send IPC message logs across channels. |
| 75 | struct IPC_EXPORT LogData { |
| 76 | LogData(); |
| 77 | ~LogData(); |
| 78 | |
| 79 | std::string channel; |
tfarina | 1cbfa08 | 2015-09-05 03:47:57 +0900 | [diff] [blame] | 80 | int32_t routing_id; |
| 81 | uint32_t type; // "User-defined" message type, from ipc_message.h. |
brettw@chromium.org | ce352e5 | 2012-06-05 06:18:25 +0900 | [diff] [blame] | 82 | std::string flags; |
tfarina | 1cbfa08 | 2015-09-05 03:47:57 +0900 | [diff] [blame] | 83 | int64_t sent; // Time that the message was sent (i.e. at Send()). |
| 84 | int64_t receive; // Time before it was dispatched (i.e. before calling |
| 85 | // OnMessageReceived). |
| 86 | int64_t dispatch; // Time after it was dispatched (i.e. after calling |
| 87 | // OnMessageReceived). |
brettw@chromium.org | ce352e5 | 2012-06-05 06:18:25 +0900 | [diff] [blame] | 88 | std::string message_name; |
| 89 | std::string params; |
| 90 | }; |
| 91 | |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 92 | //----------------------------------------------------------------------------- |
viettrungluu@chromium.org | c3ad051 | 2012-12-18 05:23:47 +0900 | [diff] [blame] | 93 | |
tsepez@chromium.org | 1ac4613 | 2011-02-12 03:46:19 +0900 | [diff] [blame] | 94 | // A dummy struct to place first just to allow leading commas for all |
| 95 | // members in the macro-generated constructor initializer lists. |
| 96 | struct NoParams { |
| 97 | }; |
| 98 | |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 99 | template <class P> |
| 100 | static inline void WriteParam(Message* m, const P& p) { |
aa@chromium.org | 1605b6d | 2009-08-14 14:43:53 +0900 | [diff] [blame] | 101 | typedef typename SimilarTypeTraits<P>::Type Type; |
thakis@chromium.org | b13bd1d | 2010-06-17 03:39:53 +0900 | [diff] [blame] | 102 | ParamTraits<Type>::Write(m, static_cast<const Type& >(p)); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | template <class P> |
brettw | f314620 | 2015-06-03 13:29:25 +0900 | [diff] [blame] | 106 | static inline bool WARN_UNUSED_RESULT |
| 107 | ReadParam(const Message* m, base::PickleIterator* iter, P* p) { |
aa@chromium.org | 1605b6d | 2009-08-14 14:43:53 +0900 | [diff] [blame] | 108 | typedef typename SimilarTypeTraits<P>::Type Type; |
| 109 | return ParamTraits<Type>::Read(m, iter, reinterpret_cast<Type* >(p)); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | template <class P> |
erg@google.com | 8aca727 | 2010-08-19 03:33:57 +0900 | [diff] [blame] | 113 | static inline void LogParam(const P& p, std::string* l) { |
aa@chromium.org | 1605b6d | 2009-08-14 14:43:53 +0900 | [diff] [blame] | 114 | typedef typename SimilarTypeTraits<P>::Type Type; |
thakis@chromium.org | b13bd1d | 2010-06-17 03:39:53 +0900 | [diff] [blame] | 115 | ParamTraits<Type>::Log(static_cast<const Type& >(p), l); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 116 | } |
| 117 | |
brettw@chromium.org | ce352e5 | 2012-06-05 06:18:25 +0900 | [diff] [blame] | 118 | // Primitive ParamTraits ------------------------------------------------------- |
| 119 | |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 120 | template <> |
| 121 | struct ParamTraits<bool> { |
| 122 | typedef bool param_type; |
| 123 | static void Write(Message* m, const param_type& p) { |
| 124 | m->WriteBool(p); |
| 125 | } |
brettw | f314620 | 2015-06-03 13:29:25 +0900 | [diff] [blame] | 126 | static bool Read(const Message* m, |
| 127 | base::PickleIterator* iter, |
brettw | a487947 | 2015-06-02 16:02:47 +0900 | [diff] [blame] | 128 | param_type* r) { |
avi | c9f0ad0 | 2014-12-29 08:31:48 +0900 | [diff] [blame] | 129 | return iter->ReadBool(r); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 130 | } |
brettw@chromium.org | ce352e5 | 2012-06-05 06:18:25 +0900 | [diff] [blame] | 131 | IPC_EXPORT static void Log(const param_type& p, std::string* l); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 132 | }; |
| 133 | |
| 134 | template <> |
ortuno | 6e3f7b3 | 2015-10-30 09:46:20 +0900 | [diff] [blame] | 135 | struct IPC_EXPORT ParamTraits<signed char> { |
| 136 | typedef signed char param_type; |
| 137 | static void Write(Message* m, const param_type& p); |
| 138 | static bool Read(const Message* m, base::PickleIterator* iter, param_type* r); |
| 139 | static void Log(const param_type& p, std::string* l); |
| 140 | }; |
| 141 | |
| 142 | template <> |
tsepez@chromium.org | 09eb95f | 2013-07-13 08:12:28 +0900 | [diff] [blame] | 143 | struct IPC_EXPORT ParamTraits<unsigned char> { |
jschuh@chromium.org | ccd324a | 2013-07-13 13:42:10 +0900 | [diff] [blame] | 144 | typedef unsigned char param_type; |
tsepez@chromium.org | 09eb95f | 2013-07-13 08:12:28 +0900 | [diff] [blame] | 145 | static void Write(Message* m, const param_type& p); |
brettw | f314620 | 2015-06-03 13:29:25 +0900 | [diff] [blame] | 146 | static bool Read(const Message* m, base::PickleIterator* iter, param_type* r); |
tsepez@chromium.org | 09eb95f | 2013-07-13 08:12:28 +0900 | [diff] [blame] | 147 | static void Log(const param_type& p, std::string* l); |
| 148 | }; |
| 149 | |
| 150 | template <> |
| 151 | struct IPC_EXPORT ParamTraits<unsigned short> { |
| 152 | typedef unsigned short param_type; |
| 153 | static void Write(Message* m, const param_type& p); |
brettw | f314620 | 2015-06-03 13:29:25 +0900 | [diff] [blame] | 154 | static bool Read(const Message* m, |
| 155 | base::PickleIterator* iter, |
| 156 | param_type* r); |
tsepez@chromium.org | 09eb95f | 2013-07-13 08:12:28 +0900 | [diff] [blame] | 157 | static void Log(const param_type& p, std::string* l); |
| 158 | }; |
| 159 | |
| 160 | template <> |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 161 | struct ParamTraits<int> { |
| 162 | typedef int param_type; |
| 163 | static void Write(Message* m, const param_type& p) { |
| 164 | m->WriteInt(p); |
| 165 | } |
brettw | f314620 | 2015-06-03 13:29:25 +0900 | [diff] [blame] | 166 | static bool Read(const Message* m, |
| 167 | base::PickleIterator* iter, |
brettw | a487947 | 2015-06-02 16:02:47 +0900 | [diff] [blame] | 168 | param_type* r) { |
avi | c9f0ad0 | 2014-12-29 08:31:48 +0900 | [diff] [blame] | 169 | return iter->ReadInt(r); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 170 | } |
darin@chromium.org | 80e4c5e | 2011-08-16 05:41:46 +0900 | [diff] [blame] | 171 | IPC_EXPORT static void Log(const param_type& p, std::string* l); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 172 | }; |
| 173 | |
| 174 | template <> |
deanm@chromium.org | 1543aa7 | 2009-07-29 04:35:08 +0900 | [diff] [blame] | 175 | struct ParamTraits<unsigned int> { |
| 176 | typedef unsigned int param_type; |
| 177 | static void Write(Message* m, const param_type& p) { |
| 178 | m->WriteInt(p); |
| 179 | } |
brettw | f314620 | 2015-06-03 13:29:25 +0900 | [diff] [blame] | 180 | static bool Read(const Message* m, |
| 181 | base::PickleIterator* iter, |
brettw | a487947 | 2015-06-02 16:02:47 +0900 | [diff] [blame] | 182 | param_type* r) { |
avi | c9f0ad0 | 2014-12-29 08:31:48 +0900 | [diff] [blame] | 183 | return iter->ReadInt(reinterpret_cast<int*>(r)); |
deanm@chromium.org | 1543aa7 | 2009-07-29 04:35:08 +0900 | [diff] [blame] | 184 | } |
darin@chromium.org | 80e4c5e | 2011-08-16 05:41:46 +0900 | [diff] [blame] | 185 | IPC_EXPORT static void Log(const param_type& p, std::string* l); |
deanm@chromium.org | 1543aa7 | 2009-07-29 04:35:08 +0900 | [diff] [blame] | 186 | }; |
| 187 | |
| 188 | template <> |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 189 | struct ParamTraits<long> { |
| 190 | typedef long param_type; |
| 191 | static void Write(Message* m, const param_type& p) { |
mdm@chromium.org | 69bca9d | 2012-03-23 09:03:10 +0900 | [diff] [blame] | 192 | m->WriteLongUsingDangerousNonPortableLessPersistableForm(p); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 193 | } |
brettw | f314620 | 2015-06-03 13:29:25 +0900 | [diff] [blame] | 194 | static bool Read(const Message* m, |
| 195 | base::PickleIterator* iter, |
brettw | a487947 | 2015-06-02 16:02:47 +0900 | [diff] [blame] | 196 | param_type* r) { |
avi | c9f0ad0 | 2014-12-29 08:31:48 +0900 | [diff] [blame] | 197 | return iter->ReadLong(r); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 198 | } |
darin@chromium.org | 80e4c5e | 2011-08-16 05:41:46 +0900 | [diff] [blame] | 199 | IPC_EXPORT static void Log(const param_type& p, std::string* l); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 200 | }; |
| 201 | |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 202 | template <> |
| 203 | struct ParamTraits<unsigned long> { |
| 204 | typedef unsigned long param_type; |
| 205 | static void Write(Message* m, const param_type& p) { |
mdm@chromium.org | 69bca9d | 2012-03-23 09:03:10 +0900 | [diff] [blame] | 206 | m->WriteLongUsingDangerousNonPortableLessPersistableForm(p); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 207 | } |
brettw | f314620 | 2015-06-03 13:29:25 +0900 | [diff] [blame] | 208 | static bool Read(const Message* m, |
| 209 | base::PickleIterator* iter, |
brettw | a487947 | 2015-06-02 16:02:47 +0900 | [diff] [blame] | 210 | param_type* r) { |
avi | c9f0ad0 | 2014-12-29 08:31:48 +0900 | [diff] [blame] | 211 | return iter->ReadLong(reinterpret_cast<long*>(r)); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 212 | } |
darin@chromium.org | 80e4c5e | 2011-08-16 05:41:46 +0900 | [diff] [blame] | 213 | IPC_EXPORT static void Log(const param_type& p, std::string* l); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 214 | }; |
| 215 | |
| 216 | template <> |
deanm@chromium.org | 1543aa7 | 2009-07-29 04:35:08 +0900 | [diff] [blame] | 217 | struct ParamTraits<long long> { |
| 218 | typedef long long param_type; |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 219 | static void Write(Message* m, const param_type& p) { |
tfarina | 1cbfa08 | 2015-09-05 03:47:57 +0900 | [diff] [blame] | 220 | m->WriteInt64(static_cast<int64_t>(p)); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 221 | } |
brettw | f314620 | 2015-06-03 13:29:25 +0900 | [diff] [blame] | 222 | static bool Read(const Message* m, |
| 223 | base::PickleIterator* iter, |
jbates@chromium.org | 0fc8736 | 2012-03-08 05:42:56 +0900 | [diff] [blame] | 224 | param_type* r) { |
tfarina | 1cbfa08 | 2015-09-05 03:47:57 +0900 | [diff] [blame] | 225 | return iter->ReadInt64(reinterpret_cast<int64_t*>(r)); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 226 | } |
darin@chromium.org | 80e4c5e | 2011-08-16 05:41:46 +0900 | [diff] [blame] | 227 | IPC_EXPORT static void Log(const param_type& p, std::string* l); |
deanm@chromium.org | 1543aa7 | 2009-07-29 04:35:08 +0900 | [diff] [blame] | 228 | }; |
| 229 | |
| 230 | template <> |
| 231 | struct ParamTraits<unsigned long long> { |
| 232 | typedef unsigned long long param_type; |
| 233 | static void Write(Message* m, const param_type& p) { |
| 234 | m->WriteInt64(p); |
| 235 | } |
brettw | f314620 | 2015-06-03 13:29:25 +0900 | [diff] [blame] | 236 | static bool Read(const Message* m, |
| 237 | base::PickleIterator* iter, |
jbates@chromium.org | 0fc8736 | 2012-03-08 05:42:56 +0900 | [diff] [blame] | 238 | param_type* r) { |
tfarina | 1cbfa08 | 2015-09-05 03:47:57 +0900 | [diff] [blame] | 239 | return iter->ReadInt64(reinterpret_cast<int64_t*>(r)); |
deanm@chromium.org | 1543aa7 | 2009-07-29 04:35:08 +0900 | [diff] [blame] | 240 | } |
darin@chromium.org | 80e4c5e | 2011-08-16 05:41:46 +0900 | [diff] [blame] | 241 | IPC_EXPORT static void Log(const param_type& p, std::string* l); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 242 | }; |
| 243 | |
jeremy@chromium.org | 4be619f | 2010-06-17 12:29:26 +0900 | [diff] [blame] | 244 | // Note that the IPC layer doesn't sanitize NaNs and +/- INF values. Clients |
| 245 | // should be sure to check the sanity of these values after receiving them over |
| 246 | // IPC. |
| 247 | template <> |
brettw@chromium.org | ce352e5 | 2012-06-05 06:18:25 +0900 | [diff] [blame] | 248 | struct IPC_EXPORT ParamTraits<float> { |
jeremy@chromium.org | 4be619f | 2010-06-17 12:29:26 +0900 | [diff] [blame] | 249 | typedef float param_type; |
piman@chromium.org | dd413b4 | 2013-10-31 18:27:31 +0900 | [diff] [blame] | 250 | static void Write(Message* m, const param_type& p) { |
| 251 | m->WriteFloat(p); |
| 252 | } |
brettw | f314620 | 2015-06-03 13:29:25 +0900 | [diff] [blame] | 253 | static bool Read(const Message* m, |
| 254 | base::PickleIterator* iter, |
brettw | a487947 | 2015-06-02 16:02:47 +0900 | [diff] [blame] | 255 | param_type* r) { |
avi | c9f0ad0 | 2014-12-29 08:31:48 +0900 | [diff] [blame] | 256 | return iter->ReadFloat(r); |
piman@chromium.org | dd413b4 | 2013-10-31 18:27:31 +0900 | [diff] [blame] | 257 | } |
brettw@chromium.org | ce352e5 | 2012-06-05 06:18:25 +0900 | [diff] [blame] | 258 | static void Log(const param_type& p, std::string* l); |
jeremy@chromium.org | 4be619f | 2010-06-17 12:29:26 +0900 | [diff] [blame] | 259 | }; |
| 260 | |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 261 | template <> |
brettw@chromium.org | ce352e5 | 2012-06-05 06:18:25 +0900 | [diff] [blame] | 262 | struct IPC_EXPORT ParamTraits<double> { |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 263 | typedef double param_type; |
tsepez@chromium.org | 3081fac | 2012-05-31 06:15:57 +0900 | [diff] [blame] | 264 | static void Write(Message* m, const param_type& p); |
brettw | f314620 | 2015-06-03 13:29:25 +0900 | [diff] [blame] | 265 | static bool Read(const Message* m, |
| 266 | base::PickleIterator* iter, |
| 267 | param_type* r); |
tsepez@chromium.org | 3081fac | 2012-05-31 06:15:57 +0900 | [diff] [blame] | 268 | static void Log(const param_type& p, std::string* l); |
| 269 | }; |
| 270 | |
brettw@chromium.org | ce352e5 | 2012-06-05 06:18:25 +0900 | [diff] [blame] | 271 | // STL ParamTraits ------------------------------------------------------------- |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 272 | |
| 273 | template <> |
| 274 | struct ParamTraits<std::string> { |
| 275 | typedef std::string param_type; |
| 276 | static void Write(Message* m, const param_type& p) { |
| 277 | m->WriteString(p); |
| 278 | } |
brettw | f314620 | 2015-06-03 13:29:25 +0900 | [diff] [blame] | 279 | static bool Read(const Message* m, |
| 280 | base::PickleIterator* iter, |
jbates@chromium.org | 0fc8736 | 2012-03-08 05:42:56 +0900 | [diff] [blame] | 281 | param_type* r) { |
avi | c9f0ad0 | 2014-12-29 08:31:48 +0900 | [diff] [blame] | 282 | return iter->ReadString(r); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 283 | } |
brettw@chromium.org | ce352e5 | 2012-06-05 06:18:25 +0900 | [diff] [blame] | 284 | IPC_EXPORT static void Log(const param_type& p, std::string* l); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 285 | }; |
| 286 | |
brettw@chromium.org | ce352e5 | 2012-06-05 06:18:25 +0900 | [diff] [blame] | 287 | template <> |
brettw@chromium.org | 5b04085 | 2013-12-03 09:39:26 +0900 | [diff] [blame] | 288 | struct ParamTraits<base::string16> { |
| 289 | typedef base::string16 param_type; |
brettw@chromium.org | ce352e5 | 2012-06-05 06:18:25 +0900 | [diff] [blame] | 290 | static void Write(Message* m, const param_type& p) { |
| 291 | m->WriteString16(p); |
| 292 | } |
brettw | f314620 | 2015-06-03 13:29:25 +0900 | [diff] [blame] | 293 | static bool Read(const Message* m, |
| 294 | base::PickleIterator* iter, |
brettw@chromium.org | ce352e5 | 2012-06-05 06:18:25 +0900 | [diff] [blame] | 295 | param_type* r) { |
avi | c9f0ad0 | 2014-12-29 08:31:48 +0900 | [diff] [blame] | 296 | return iter->ReadString16(r); |
brettw@chromium.org | ce352e5 | 2012-06-05 06:18:25 +0900 | [diff] [blame] | 297 | } |
| 298 | IPC_EXPORT static void Log(const param_type& p, std::string* l); |
| 299 | }; |
evan@chromium.org | a659036 | 2009-07-28 06:09:07 +0900 | [diff] [blame] | 300 | |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 301 | template <> |
brettw@chromium.org | ce352e5 | 2012-06-05 06:18:25 +0900 | [diff] [blame] | 302 | struct IPC_EXPORT ParamTraits<std::vector<char> > { |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 303 | typedef std::vector<char> param_type; |
brettw@chromium.org | ce352e5 | 2012-06-05 06:18:25 +0900 | [diff] [blame] | 304 | static void Write(Message* m, const param_type& p); |
brettw | f314620 | 2015-06-03 13:29:25 +0900 | [diff] [blame] | 305 | static bool Read(const Message*, |
| 306 | base::PickleIterator* iter, |
| 307 | param_type* r); |
brettw@chromium.org | ce352e5 | 2012-06-05 06:18:25 +0900 | [diff] [blame] | 308 | static void Log(const param_type& p, std::string* l); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 309 | }; |
| 310 | |
bauerb@chromium.org | 17246c7 | 2011-10-06 03:55:42 +0900 | [diff] [blame] | 311 | template <> |
brettw@chromium.org | ce352e5 | 2012-06-05 06:18:25 +0900 | [diff] [blame] | 312 | struct IPC_EXPORT ParamTraits<std::vector<unsigned char> > { |
| 313 | typedef std::vector<unsigned char> param_type; |
| 314 | static void Write(Message* m, const param_type& p); |
brettw | f314620 | 2015-06-03 13:29:25 +0900 | [diff] [blame] | 315 | static bool Read(const Message* m, |
| 316 | base::PickleIterator* iter, |
| 317 | param_type* r); |
brettw@chromium.org | ce352e5 | 2012-06-05 06:18:25 +0900 | [diff] [blame] | 318 | static void Log(const param_type& p, std::string* l); |
| 319 | }; |
| 320 | |
| 321 | template <> |
| 322 | struct IPC_EXPORT ParamTraits<std::vector<bool> > { |
bauerb@chromium.org | 17246c7 | 2011-10-06 03:55:42 +0900 | [diff] [blame] | 323 | typedef std::vector<bool> param_type; |
brettw@chromium.org | ce352e5 | 2012-06-05 06:18:25 +0900 | [diff] [blame] | 324 | static void Write(Message* m, const param_type& p); |
brettw | f314620 | 2015-06-03 13:29:25 +0900 | [diff] [blame] | 325 | static bool Read(const Message* m, |
| 326 | base::PickleIterator* iter, |
| 327 | param_type* r); |
brettw@chromium.org | ce352e5 | 2012-06-05 06:18:25 +0900 | [diff] [blame] | 328 | static void Log(const param_type& p, std::string* l); |
bauerb@chromium.org | 17246c7 | 2011-10-06 03:55:42 +0900 | [diff] [blame] | 329 | }; |
| 330 | |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 331 | template <class P> |
| 332 | struct ParamTraits<std::vector<P> > { |
| 333 | typedef std::vector<P> param_type; |
| 334 | static void Write(Message* m, const param_type& p) { |
| 335 | WriteParam(m, static_cast<int>(p.size())); |
| 336 | for (size_t i = 0; i < p.size(); i++) |
| 337 | WriteParam(m, p[i]); |
| 338 | } |
brettw | f314620 | 2015-06-03 13:29:25 +0900 | [diff] [blame] | 339 | static bool Read(const Message* m, |
| 340 | base::PickleIterator* iter, |
jbates@chromium.org | 0fc8736 | 2012-03-08 05:42:56 +0900 | [diff] [blame] | 341 | param_type* r) { |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 342 | int size; |
cevans@chromium.org | 7600d94 | 2009-12-31 14:17:23 +0900 | [diff] [blame] | 343 | // ReadLength() checks for < 0 itself. |
avi | c9f0ad0 | 2014-12-29 08:31:48 +0900 | [diff] [blame] | 344 | if (!iter->ReadLength(&size)) |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 345 | return false; |
| 346 | // Resizing beforehand is not safe, see BUG 1006367 for details. |
cevans@chromium.org | 7600d94 | 2009-12-31 14:17:23 +0900 | [diff] [blame] | 347 | if (INT_MAX / sizeof(P) <= static_cast<size_t>(size)) |
| 348 | return false; |
| 349 | r->resize(size); |
| 350 | for (int i = 0; i < size; i++) { |
| 351 | if (!ReadParam(m, iter, &(*r)[i])) |
| 352 | return false; |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 353 | } |
| 354 | return true; |
| 355 | } |
erg@google.com | 8aca727 | 2010-08-19 03:33:57 +0900 | [diff] [blame] | 356 | static void Log(const param_type& p, std::string* l) { |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 357 | for (size_t i = 0; i < p.size(); ++i) { |
| 358 | if (i != 0) |
erg@google.com | 8aca727 | 2010-08-19 03:33:57 +0900 | [diff] [blame] | 359 | l->append(" "); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 360 | LogParam((p[i]), l); |
| 361 | } |
| 362 | } |
| 363 | }; |
| 364 | |
mirandac@chromium.org | ee262ad | 2010-05-14 04:10:34 +0900 | [diff] [blame] | 365 | template <class P> |
| 366 | struct ParamTraits<std::set<P> > { |
| 367 | typedef std::set<P> param_type; |
| 368 | static void Write(Message* m, const param_type& p) { |
| 369 | WriteParam(m, static_cast<int>(p.size())); |
| 370 | typename param_type::const_iterator iter; |
| 371 | for (iter = p.begin(); iter != p.end(); ++iter) |
| 372 | WriteParam(m, *iter); |
| 373 | } |
brettw | f314620 | 2015-06-03 13:29:25 +0900 | [diff] [blame] | 374 | static bool Read(const Message* m, |
| 375 | base::PickleIterator* iter, |
jbates@chromium.org | 0fc8736 | 2012-03-08 05:42:56 +0900 | [diff] [blame] | 376 | param_type* r) { |
mirandac@chromium.org | ee262ad | 2010-05-14 04:10:34 +0900 | [diff] [blame] | 377 | int size; |
avi | c9f0ad0 | 2014-12-29 08:31:48 +0900 | [diff] [blame] | 378 | if (!iter->ReadLength(&size)) |
mirandac@chromium.org | ee262ad | 2010-05-14 04:10:34 +0900 | [diff] [blame] | 379 | return false; |
| 380 | for (int i = 0; i < size; ++i) { |
| 381 | P item; |
| 382 | if (!ReadParam(m, iter, &item)) |
| 383 | return false; |
| 384 | r->insert(item); |
| 385 | } |
| 386 | return true; |
| 387 | } |
erg@google.com | 8aca727 | 2010-08-19 03:33:57 +0900 | [diff] [blame] | 388 | static void Log(const param_type& p, std::string* l) { |
| 389 | l->append("<std::set>"); |
mirandac@chromium.org | ee262ad | 2010-05-14 04:10:34 +0900 | [diff] [blame] | 390 | } |
| 391 | }; |
| 392 | |
jkarlin | 45b11f9 | 2014-09-24 20:42:37 +0900 | [diff] [blame] | 393 | template <class K, class V, class C, class A> |
| 394 | struct ParamTraits<std::map<K, V, C, A> > { |
| 395 | typedef std::map<K, V, C, A> param_type; |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 396 | static void Write(Message* m, const param_type& p) { |
| 397 | WriteParam(m, static_cast<int>(p.size())); |
| 398 | typename param_type::const_iterator iter; |
| 399 | for (iter = p.begin(); iter != p.end(); ++iter) { |
| 400 | WriteParam(m, iter->first); |
| 401 | WriteParam(m, iter->second); |
| 402 | } |
| 403 | } |
brettw | f314620 | 2015-06-03 13:29:25 +0900 | [diff] [blame] | 404 | static bool Read(const Message* m, |
| 405 | base::PickleIterator* iter, |
jbates@chromium.org | 0fc8736 | 2012-03-08 05:42:56 +0900 | [diff] [blame] | 406 | param_type* r) { |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 407 | int size; |
| 408 | if (!ReadParam(m, iter, &size) || size < 0) |
| 409 | return false; |
| 410 | for (int i = 0; i < size; ++i) { |
| 411 | K k; |
| 412 | if (!ReadParam(m, iter, &k)) |
| 413 | return false; |
| 414 | V& value = (*r)[k]; |
| 415 | if (!ReadParam(m, iter, &value)) |
| 416 | return false; |
| 417 | } |
| 418 | return true; |
| 419 | } |
erg@google.com | 8aca727 | 2010-08-19 03:33:57 +0900 | [diff] [blame] | 420 | static void Log(const param_type& p, std::string* l) { |
| 421 | l->append("<std::map>"); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 422 | } |
| 423 | }; |
| 424 | |
jam@chromium.org | 0aa0502 | 2009-08-04 11:00:56 +0900 | [diff] [blame] | 425 | template <class A, class B> |
| 426 | struct ParamTraits<std::pair<A, B> > { |
| 427 | typedef std::pair<A, B> param_type; |
| 428 | static void Write(Message* m, const param_type& p) { |
| 429 | WriteParam(m, p.first); |
| 430 | WriteParam(m, p.second); |
| 431 | } |
brettw | f314620 | 2015-06-03 13:29:25 +0900 | [diff] [blame] | 432 | static bool Read(const Message* m, |
| 433 | base::PickleIterator* iter, |
jbates@chromium.org | 0fc8736 | 2012-03-08 05:42:56 +0900 | [diff] [blame] | 434 | param_type* r) { |
jam@chromium.org | 0aa0502 | 2009-08-04 11:00:56 +0900 | [diff] [blame] | 435 | return ReadParam(m, iter, &r->first) && ReadParam(m, iter, &r->second); |
| 436 | } |
erg@google.com | 8aca727 | 2010-08-19 03:33:57 +0900 | [diff] [blame] | 437 | static void Log(const param_type& p, std::string* l) { |
| 438 | l->append("("); |
jam@chromium.org | 0aa0502 | 2009-08-04 11:00:56 +0900 | [diff] [blame] | 439 | LogParam(p.first, l); |
erg@google.com | 8aca727 | 2010-08-19 03:33:57 +0900 | [diff] [blame] | 440 | l->append(", "); |
jam@chromium.org | 0aa0502 | 2009-08-04 11:00:56 +0900 | [diff] [blame] | 441 | LogParam(p.second, l); |
erg@google.com | 8aca727 | 2010-08-19 03:33:57 +0900 | [diff] [blame] | 442 | l->append(")"); |
jam@chromium.org | 0aa0502 | 2009-08-04 11:00:56 +0900 | [diff] [blame] | 443 | } |
| 444 | }; |
| 445 | |
erikchen | fdd43fe | 2015-07-08 07:13:11 +0900 | [diff] [blame] | 446 | // IPC ParamTraits ------------------------------------------------------------- |
| 447 | template <> |
| 448 | struct IPC_EXPORT ParamTraits<BrokerableAttachment::AttachmentId> { |
| 449 | typedef BrokerableAttachment::AttachmentId param_type; |
| 450 | static void Write(Message* m, const param_type& p); |
| 451 | static bool Read(const Message* m, base::PickleIterator* iter, param_type* r); |
| 452 | static void Log(const param_type& p, std::string* l); |
| 453 | }; |
| 454 | |
brettw@chromium.org | ce352e5 | 2012-06-05 06:18:25 +0900 | [diff] [blame] | 455 | // Base ParamTraits ------------------------------------------------------------ |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 456 | |
| 457 | template <> |
brettw@chromium.org | ce352e5 | 2012-06-05 06:18:25 +0900 | [diff] [blame] | 458 | struct IPC_EXPORT ParamTraits<base::DictionaryValue> { |
| 459 | typedef base::DictionaryValue param_type; |
erg@google.com | e6ffcb5 | 2010-08-18 03:38:24 +0900 | [diff] [blame] | 460 | static void Write(Message* m, const param_type& p); |
brettw | f314620 | 2015-06-03 13:29:25 +0900 | [diff] [blame] | 461 | static bool Read(const Message* m, |
| 462 | base::PickleIterator* iter, |
| 463 | param_type* r); |
erg@google.com | 8aca727 | 2010-08-19 03:33:57 +0900 | [diff] [blame] | 464 | static void Log(const param_type& p, std::string* l); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 465 | }; |
| 466 | |
| 467 | #if defined(OS_POSIX) |
| 468 | // FileDescriptors may be serialised over IPC channels on POSIX. On the |
| 469 | // receiving side, the FileDescriptor is a valid duplicate of the file |
| 470 | // descriptor which was transmitted: *it is not just a copy of the integer like |
| 471 | // HANDLEs on Windows*. The only exception is if the file descriptor is < 0. In |
| 472 | // this case, the receiving end will see a value of -1. *Zero is a valid file |
| 473 | // descriptor*. |
| 474 | // |
| 475 | // The received file descriptor will have the |auto_close| flag set to true. The |
| 476 | // code which handles the message is responsible for taking ownership of it. |
| 477 | // File descriptors are OS resources and must be closed when no longer needed. |
| 478 | // |
| 479 | // When sending a file descriptor, the file descriptor must be valid at the time |
| 480 | // of transmission. Since transmission is not synchronous, one should consider |
| 481 | // dup()ing any file descriptors to be transmitted and setting the |auto_close| |
| 482 | // flag, which causes the file descriptor to be closed after writing. |
| 483 | template<> |
darin@chromium.org | 80e4c5e | 2011-08-16 05:41:46 +0900 | [diff] [blame] | 484 | struct IPC_EXPORT ParamTraits<base::FileDescriptor> { |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 485 | typedef base::FileDescriptor param_type; |
erg@google.com | e6ffcb5 | 2010-08-18 03:38:24 +0900 | [diff] [blame] | 486 | static void Write(Message* m, const param_type& p); |
brettw | f314620 | 2015-06-03 13:29:25 +0900 | [diff] [blame] | 487 | static bool Read(const Message* m, |
| 488 | base::PickleIterator* iter, |
| 489 | param_type* r); |
erg@google.com | 8aca727 | 2010-08-19 03:33:57 +0900 | [diff] [blame] | 490 | static void Log(const param_type& p, std::string* l); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 491 | }; |
jhawkins@chromium.org | 63c75eb | 2010-03-09 09:38:41 +0900 | [diff] [blame] | 492 | #endif // defined(OS_POSIX) |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 493 | |
erikchen | 18430e5 | 2015-09-26 07:34:31 +0900 | [diff] [blame] | 494 | #if (defined(OS_MACOSX) && !defined(OS_IOS)) || defined(OS_WIN) |
scottmg | 8149127 | 2015-06-20 07:51:00 +0900 | [diff] [blame] | 495 | template <> |
| 496 | struct IPC_EXPORT ParamTraits<base::SharedMemoryHandle> { |
| 497 | typedef base::SharedMemoryHandle param_type; |
| 498 | static void Write(Message* m, const param_type& p); |
| 499 | static bool Read(const Message* m, base::PickleIterator* iter, param_type* r); |
| 500 | static void Log(const param_type& p, std::string* l); |
| 501 | }; |
erikchen | 18430e5 | 2015-09-26 07:34:31 +0900 | [diff] [blame] | 502 | #endif // (defined(OS_MACOSX) && !defined(OS_IOS)) || defined(OS_WIN) |
scottmg | 8149127 | 2015-06-20 07:51:00 +0900 | [diff] [blame] | 503 | |
brettw@chromium.org | ce352e5 | 2012-06-05 06:18:25 +0900 | [diff] [blame] | 504 | template <> |
brettw@chromium.org | a708694 | 2013-02-02 14:12:33 +0900 | [diff] [blame] | 505 | struct IPC_EXPORT ParamTraits<base::FilePath> { |
| 506 | typedef base::FilePath param_type; |
erg@google.com | e6ffcb5 | 2010-08-18 03:38:24 +0900 | [diff] [blame] | 507 | static void Write(Message* m, const param_type& p); |
brettw | f314620 | 2015-06-03 13:29:25 +0900 | [diff] [blame] | 508 | static bool Read(const Message* m, |
| 509 | base::PickleIterator* iter, |
| 510 | param_type* r); |
erg@google.com | 8aca727 | 2010-08-19 03:33:57 +0900 | [diff] [blame] | 511 | static void Log(const param_type& p, std::string* l); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 512 | }; |
| 513 | |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 514 | template <> |
brettw@chromium.org | ce352e5 | 2012-06-05 06:18:25 +0900 | [diff] [blame] | 515 | struct IPC_EXPORT ParamTraits<base::ListValue> { |
| 516 | typedef base::ListValue param_type; |
erg@google.com | 20b66e3 | 2010-10-01 05:06:30 +0900 | [diff] [blame] | 517 | static void Write(Message* m, const param_type& p); |
brettw | f314620 | 2015-06-03 13:29:25 +0900 | [diff] [blame] | 518 | static bool Read(const Message* m, |
| 519 | base::PickleIterator* iter, |
| 520 | param_type* r); |
brettw@chromium.org | ce352e5 | 2012-06-05 06:18:25 +0900 | [diff] [blame] | 521 | static void Log(const param_type& p, std::string* l); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 522 | }; |
| 523 | |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 524 | template <> |
avi@chromium.org | d817965 | 2013-06-13 22:47:46 +0900 | [diff] [blame] | 525 | struct IPC_EXPORT ParamTraits<base::NullableString16> { |
| 526 | typedef base::NullableString16 param_type; |
brettw@chromium.org | ce352e5 | 2012-06-05 06:18:25 +0900 | [diff] [blame] | 527 | static void Write(Message* m, const param_type& p); |
brettw | f314620 | 2015-06-03 13:29:25 +0900 | [diff] [blame] | 528 | static bool Read(const Message* m, |
| 529 | base::PickleIterator* iter, |
brettw@chromium.org | ce352e5 | 2012-06-05 06:18:25 +0900 | [diff] [blame] | 530 | param_type* r); |
| 531 | static void Log(const param_type& p, std::string* l); |
| 532 | }; |
| 533 | |
| 534 | template <> |
rvargas@chromium.org | 9e469f6 | 2014-01-28 06:36:00 +0900 | [diff] [blame] | 535 | struct IPC_EXPORT ParamTraits<base::File::Info> { |
| 536 | typedef base::File::Info param_type; |
brettw@chromium.org | ce352e5 | 2012-06-05 06:18:25 +0900 | [diff] [blame] | 537 | static void Write(Message* m, const param_type& p); |
brettw | f314620 | 2015-06-03 13:29:25 +0900 | [diff] [blame] | 538 | static bool Read(const Message* m, |
| 539 | base::PickleIterator* iter, |
| 540 | param_type* r); |
brettw@chromium.org | ce352e5 | 2012-06-05 06:18:25 +0900 | [diff] [blame] | 541 | static void Log(const param_type& p, std::string* l); |
| 542 | }; |
| 543 | |
| 544 | template <> |
rvargas@chromium.org | 9e469f6 | 2014-01-28 06:36:00 +0900 | [diff] [blame] | 545 | struct SimilarTypeTraits<base::File::Error> { |
brettw@chromium.org | ce352e5 | 2012-06-05 06:18:25 +0900 | [diff] [blame] | 546 | typedef int Type; |
| 547 | }; |
| 548 | |
grt@chromium.org | b9abb17 | 2013-03-01 06:46:11 +0900 | [diff] [blame] | 549 | #if defined(OS_WIN) |
| 550 | template <> |
| 551 | struct SimilarTypeTraits<HWND> { |
| 552 | typedef HANDLE Type; |
| 553 | }; |
| 554 | #endif // defined(OS_WIN) |
| 555 | |
brettw@chromium.org | ce352e5 | 2012-06-05 06:18:25 +0900 | [diff] [blame] | 556 | template <> |
| 557 | struct IPC_EXPORT ParamTraits<base::Time> { |
| 558 | typedef base::Time param_type; |
| 559 | static void Write(Message* m, const param_type& p); |
brettw | f314620 | 2015-06-03 13:29:25 +0900 | [diff] [blame] | 560 | static bool Read(const Message* m, |
| 561 | base::PickleIterator* iter, |
| 562 | param_type* r); |
brettw@chromium.org | ce352e5 | 2012-06-05 06:18:25 +0900 | [diff] [blame] | 563 | static void Log(const param_type& p, std::string* l); |
| 564 | }; |
| 565 | |
| 566 | template <> |
| 567 | struct IPC_EXPORT ParamTraits<base::TimeDelta> { |
| 568 | typedef base::TimeDelta param_type; |
| 569 | static void Write(Message* m, const param_type& p); |
brettw | f314620 | 2015-06-03 13:29:25 +0900 | [diff] [blame] | 570 | static bool Read(const Message* m, |
| 571 | base::PickleIterator* iter, |
| 572 | param_type* r); |
brettw@chromium.org | ce352e5 | 2012-06-05 06:18:25 +0900 | [diff] [blame] | 573 | static void Log(const param_type& p, std::string* l); |
| 574 | }; |
| 575 | |
| 576 | template <> |
| 577 | struct IPC_EXPORT ParamTraits<base::TimeTicks> { |
| 578 | typedef base::TimeTicks param_type; |
| 579 | static void Write(Message* m, const param_type& p); |
brettw | f314620 | 2015-06-03 13:29:25 +0900 | [diff] [blame] | 580 | static bool Read(const Message* m, |
| 581 | base::PickleIterator* iter, |
| 582 | param_type* r); |
brettw@chromium.org | ce352e5 | 2012-06-05 06:18:25 +0900 | [diff] [blame] | 583 | static void Log(const param_type& p, std::string* l); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 584 | }; |
| 585 | |
| 586 | template <> |
brettw | 79a9090 | 2015-05-30 07:15:47 +0900 | [diff] [blame] | 587 | struct ParamTraits<base::Tuple<>> { |
| 588 | typedef base::Tuple<> param_type; |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 589 | static void Write(Message* m, const param_type& p) { |
| 590 | } |
brettw | f314620 | 2015-06-03 13:29:25 +0900 | [diff] [blame] | 591 | static bool Read(const Message* m, |
| 592 | base::PickleIterator* iter, |
brettw | a487947 | 2015-06-02 16:02:47 +0900 | [diff] [blame] | 593 | param_type* r) { |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 594 | return true; |
| 595 | } |
erg@google.com | 8aca727 | 2010-08-19 03:33:57 +0900 | [diff] [blame] | 596 | static void Log(const param_type& p, std::string* l) { |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 597 | } |
| 598 | }; |
| 599 | |
| 600 | template <class A> |
brettw | 79a9090 | 2015-05-30 07:15:47 +0900 | [diff] [blame] | 601 | struct ParamTraits<base::Tuple<A>> { |
| 602 | typedef base::Tuple<A> param_type; |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 603 | static void Write(Message* m, const param_type& p) { |
brettw | 79a9090 | 2015-05-30 07:15:47 +0900 | [diff] [blame] | 604 | WriteParam(m, base::get<0>(p)); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 605 | } |
brettw | f314620 | 2015-06-03 13:29:25 +0900 | [diff] [blame] | 606 | static bool Read(const Message* m, |
| 607 | base::PickleIterator* iter, |
brettw | a487947 | 2015-06-02 16:02:47 +0900 | [diff] [blame] | 608 | param_type* r) { |
brettw | 79a9090 | 2015-05-30 07:15:47 +0900 | [diff] [blame] | 609 | return ReadParam(m, iter, &base::get<0>(*r)); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 610 | } |
erg@google.com | 8aca727 | 2010-08-19 03:33:57 +0900 | [diff] [blame] | 611 | static void Log(const param_type& p, std::string* l) { |
brettw | 79a9090 | 2015-05-30 07:15:47 +0900 | [diff] [blame] | 612 | LogParam(base::get<0>(p), l); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 613 | } |
| 614 | }; |
| 615 | |
| 616 | template <class A, class B> |
brettw | 79a9090 | 2015-05-30 07:15:47 +0900 | [diff] [blame] | 617 | struct ParamTraits<base::Tuple<A, B>> { |
| 618 | typedef base::Tuple<A, B> param_type; |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 619 | static void Write(Message* m, const param_type& p) { |
brettw | 79a9090 | 2015-05-30 07:15:47 +0900 | [diff] [blame] | 620 | WriteParam(m, base::get<0>(p)); |
| 621 | WriteParam(m, base::get<1>(p)); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 622 | } |
brettw | f314620 | 2015-06-03 13:29:25 +0900 | [diff] [blame] | 623 | static bool Read(const Message* m, |
| 624 | base::PickleIterator* iter, |
brettw | a487947 | 2015-06-02 16:02:47 +0900 | [diff] [blame] | 625 | param_type* r) { |
brettw | 79a9090 | 2015-05-30 07:15:47 +0900 | [diff] [blame] | 626 | return (ReadParam(m, iter, &base::get<0>(*r)) && |
| 627 | ReadParam(m, iter, &base::get<1>(*r))); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 628 | } |
erg@google.com | 8aca727 | 2010-08-19 03:33:57 +0900 | [diff] [blame] | 629 | static void Log(const param_type& p, std::string* l) { |
brettw | 79a9090 | 2015-05-30 07:15:47 +0900 | [diff] [blame] | 630 | LogParam(base::get<0>(p), l); |
erg@google.com | 8aca727 | 2010-08-19 03:33:57 +0900 | [diff] [blame] | 631 | l->append(", "); |
brettw | 79a9090 | 2015-05-30 07:15:47 +0900 | [diff] [blame] | 632 | LogParam(base::get<1>(p), l); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 633 | } |
| 634 | }; |
| 635 | |
| 636 | template <class A, class B, class C> |
brettw | 79a9090 | 2015-05-30 07:15:47 +0900 | [diff] [blame] | 637 | struct ParamTraits<base::Tuple<A, B, C>> { |
| 638 | typedef base::Tuple<A, B, C> param_type; |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 639 | static void Write(Message* m, const param_type& p) { |
brettw | 79a9090 | 2015-05-30 07:15:47 +0900 | [diff] [blame] | 640 | WriteParam(m, base::get<0>(p)); |
| 641 | WriteParam(m, base::get<1>(p)); |
| 642 | WriteParam(m, base::get<2>(p)); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 643 | } |
brettw | f314620 | 2015-06-03 13:29:25 +0900 | [diff] [blame] | 644 | static bool Read(const Message* m, |
| 645 | base::PickleIterator* iter, |
brettw | a487947 | 2015-06-02 16:02:47 +0900 | [diff] [blame] | 646 | param_type* r) { |
brettw | 79a9090 | 2015-05-30 07:15:47 +0900 | [diff] [blame] | 647 | return (ReadParam(m, iter, &base::get<0>(*r)) && |
| 648 | ReadParam(m, iter, &base::get<1>(*r)) && |
| 649 | ReadParam(m, iter, &base::get<2>(*r))); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 650 | } |
erg@google.com | 8aca727 | 2010-08-19 03:33:57 +0900 | [diff] [blame] | 651 | static void Log(const param_type& p, std::string* l) { |
brettw | 79a9090 | 2015-05-30 07:15:47 +0900 | [diff] [blame] | 652 | LogParam(base::get<0>(p), l); |
erg@google.com | 8aca727 | 2010-08-19 03:33:57 +0900 | [diff] [blame] | 653 | l->append(", "); |
brettw | 79a9090 | 2015-05-30 07:15:47 +0900 | [diff] [blame] | 654 | LogParam(base::get<1>(p), l); |
erg@google.com | 8aca727 | 2010-08-19 03:33:57 +0900 | [diff] [blame] | 655 | l->append(", "); |
brettw | 79a9090 | 2015-05-30 07:15:47 +0900 | [diff] [blame] | 656 | LogParam(base::get<2>(p), l); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 657 | } |
| 658 | }; |
| 659 | |
| 660 | template <class A, class B, class C, class D> |
brettw | 79a9090 | 2015-05-30 07:15:47 +0900 | [diff] [blame] | 661 | struct ParamTraits<base::Tuple<A, B, C, D>> { |
| 662 | typedef base::Tuple<A, B, C, D> param_type; |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 663 | static void Write(Message* m, const param_type& p) { |
brettw | 79a9090 | 2015-05-30 07:15:47 +0900 | [diff] [blame] | 664 | WriteParam(m, base::get<0>(p)); |
| 665 | WriteParam(m, base::get<1>(p)); |
| 666 | WriteParam(m, base::get<2>(p)); |
| 667 | WriteParam(m, base::get<3>(p)); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 668 | } |
brettw | f314620 | 2015-06-03 13:29:25 +0900 | [diff] [blame] | 669 | static bool Read(const Message* m, |
| 670 | base::PickleIterator* iter, |
brettw | a487947 | 2015-06-02 16:02:47 +0900 | [diff] [blame] | 671 | param_type* r) { |
brettw | 79a9090 | 2015-05-30 07:15:47 +0900 | [diff] [blame] | 672 | return (ReadParam(m, iter, &base::get<0>(*r)) && |
| 673 | ReadParam(m, iter, &base::get<1>(*r)) && |
| 674 | ReadParam(m, iter, &base::get<2>(*r)) && |
| 675 | ReadParam(m, iter, &base::get<3>(*r))); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 676 | } |
erg@google.com | 8aca727 | 2010-08-19 03:33:57 +0900 | [diff] [blame] | 677 | static void Log(const param_type& p, std::string* l) { |
brettw | 79a9090 | 2015-05-30 07:15:47 +0900 | [diff] [blame] | 678 | LogParam(base::get<0>(p), l); |
erg@google.com | 8aca727 | 2010-08-19 03:33:57 +0900 | [diff] [blame] | 679 | l->append(", "); |
brettw | 79a9090 | 2015-05-30 07:15:47 +0900 | [diff] [blame] | 680 | LogParam(base::get<1>(p), l); |
erg@google.com | 8aca727 | 2010-08-19 03:33:57 +0900 | [diff] [blame] | 681 | l->append(", "); |
brettw | 79a9090 | 2015-05-30 07:15:47 +0900 | [diff] [blame] | 682 | LogParam(base::get<2>(p), l); |
erg@google.com | 8aca727 | 2010-08-19 03:33:57 +0900 | [diff] [blame] | 683 | l->append(", "); |
brettw | 79a9090 | 2015-05-30 07:15:47 +0900 | [diff] [blame] | 684 | LogParam(base::get<3>(p), l); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 685 | } |
| 686 | }; |
| 687 | |
| 688 | template <class A, class B, class C, class D, class E> |
brettw | 79a9090 | 2015-05-30 07:15:47 +0900 | [diff] [blame] | 689 | struct ParamTraits<base::Tuple<A, B, C, D, E>> { |
| 690 | typedef base::Tuple<A, B, C, D, E> param_type; |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 691 | static void Write(Message* m, const param_type& p) { |
brettw | 79a9090 | 2015-05-30 07:15:47 +0900 | [diff] [blame] | 692 | WriteParam(m, base::get<0>(p)); |
| 693 | WriteParam(m, base::get<1>(p)); |
| 694 | WriteParam(m, base::get<2>(p)); |
| 695 | WriteParam(m, base::get<3>(p)); |
| 696 | WriteParam(m, base::get<4>(p)); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 697 | } |
brettw | f314620 | 2015-06-03 13:29:25 +0900 | [diff] [blame] | 698 | static bool Read(const Message* m, |
| 699 | base::PickleIterator* iter, |
brettw | a487947 | 2015-06-02 16:02:47 +0900 | [diff] [blame] | 700 | param_type* r) { |
brettw | 79a9090 | 2015-05-30 07:15:47 +0900 | [diff] [blame] | 701 | return (ReadParam(m, iter, &base::get<0>(*r)) && |
| 702 | ReadParam(m, iter, &base::get<1>(*r)) && |
| 703 | ReadParam(m, iter, &base::get<2>(*r)) && |
| 704 | ReadParam(m, iter, &base::get<3>(*r)) && |
| 705 | ReadParam(m, iter, &base::get<4>(*r))); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 706 | } |
erg@google.com | 8aca727 | 2010-08-19 03:33:57 +0900 | [diff] [blame] | 707 | static void Log(const param_type& p, std::string* l) { |
brettw | 79a9090 | 2015-05-30 07:15:47 +0900 | [diff] [blame] | 708 | LogParam(base::get<0>(p), l); |
erg@google.com | 8aca727 | 2010-08-19 03:33:57 +0900 | [diff] [blame] | 709 | l->append(", "); |
brettw | 79a9090 | 2015-05-30 07:15:47 +0900 | [diff] [blame] | 710 | LogParam(base::get<1>(p), l); |
erg@google.com | 8aca727 | 2010-08-19 03:33:57 +0900 | [diff] [blame] | 711 | l->append(", "); |
brettw | 79a9090 | 2015-05-30 07:15:47 +0900 | [diff] [blame] | 712 | LogParam(base::get<2>(p), l); |
erg@google.com | 8aca727 | 2010-08-19 03:33:57 +0900 | [diff] [blame] | 713 | l->append(", "); |
brettw | 79a9090 | 2015-05-30 07:15:47 +0900 | [diff] [blame] | 714 | LogParam(base::get<3>(p), l); |
erg@google.com | 8aca727 | 2010-08-19 03:33:57 +0900 | [diff] [blame] | 715 | l->append(", "); |
brettw | 79a9090 | 2015-05-30 07:15:47 +0900 | [diff] [blame] | 716 | LogParam(base::get<4>(p), l); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 717 | } |
| 718 | }; |
| 719 | |
hashimoto@chromium.org | bbf7c50 | 2012-11-14 15:35:52 +0900 | [diff] [blame] | 720 | template<class P> |
| 721 | struct ParamTraits<ScopedVector<P> > { |
| 722 | typedef ScopedVector<P> param_type; |
| 723 | static void Write(Message* m, const param_type& p) { |
| 724 | WriteParam(m, static_cast<int>(p.size())); |
| 725 | for (size_t i = 0; i < p.size(); i++) |
| 726 | WriteParam(m, *p[i]); |
| 727 | } |
brettw | f314620 | 2015-06-03 13:29:25 +0900 | [diff] [blame] | 728 | static bool Read(const Message* m, |
| 729 | base::PickleIterator* iter, |
brettw | a487947 | 2015-06-02 16:02:47 +0900 | [diff] [blame] | 730 | param_type* r) { |
hashimoto@chromium.org | bbf7c50 | 2012-11-14 15:35:52 +0900 | [diff] [blame] | 731 | int size = 0; |
avi | c9f0ad0 | 2014-12-29 08:31:48 +0900 | [diff] [blame] | 732 | if (!iter->ReadLength(&size)) |
hashimoto@chromium.org | bbf7c50 | 2012-11-14 15:35:52 +0900 | [diff] [blame] | 733 | return false; |
| 734 | if (INT_MAX/sizeof(P) <= static_cast<size_t>(size)) |
| 735 | return false; |
| 736 | r->resize(size); |
| 737 | for (int i = 0; i < size; i++) { |
| 738 | (*r)[i] = new P(); |
| 739 | if (!ReadParam(m, iter, (*r)[i])) |
| 740 | return false; |
| 741 | } |
| 742 | return true; |
| 743 | } |
| 744 | static void Log(const param_type& p, std::string* l) { |
| 745 | for (size_t i = 0; i < p.size(); ++i) { |
| 746 | if (i != 0) |
| 747 | l->append(" "); |
| 748 | LogParam(*p[i], l); |
| 749 | } |
| 750 | } |
| 751 | }; |
| 752 | |
miletus | d41fe22 | 2015-07-24 04:42:36 +0900 | [diff] [blame] | 753 | template <class P, size_t stack_capacity> |
| 754 | struct ParamTraits<base::StackVector<P, stack_capacity> > { |
| 755 | typedef base::StackVector<P, stack_capacity> param_type; |
| 756 | static void Write(Message* m, const param_type& p) { |
| 757 | WriteParam(m, static_cast<int>(p->size())); |
| 758 | for (size_t i = 0; i < p->size(); i++) |
| 759 | WriteParam(m, p[i]); |
| 760 | } |
| 761 | static bool Read(const Message* m, |
| 762 | base::PickleIterator* iter, |
| 763 | param_type* r) { |
| 764 | int size; |
| 765 | // ReadLength() checks for < 0 itself. |
| 766 | if (!iter->ReadLength(&size)) |
| 767 | return false; |
| 768 | // Sanity check for the vector size. |
| 769 | if (INT_MAX / sizeof(P) <= static_cast<size_t>(size)) |
| 770 | return false; |
| 771 | P value; |
| 772 | for (int i = 0; i < size; i++) { |
| 773 | if (!ReadParam(m, iter, &value)) |
| 774 | return false; |
| 775 | (*r)->push_back(value); |
| 776 | } |
| 777 | return true; |
| 778 | } |
| 779 | static void Log(const param_type& p, std::string* l) { |
| 780 | for (size_t i = 0; i < p->size(); ++i) { |
| 781 | if (i != 0) |
| 782 | l->append(" "); |
| 783 | LogParam((p[i]), l); |
| 784 | } |
| 785 | } |
| 786 | }; |
| 787 | |
jdduke@chromium.org | 556db8b | 2014-03-19 05:48:35 +0900 | [diff] [blame] | 788 | template <typename NormalMap, |
| 789 | int kArraySize, |
| 790 | typename EqualKey, |
| 791 | typename MapInit> |
| 792 | struct ParamTraits<base::SmallMap<NormalMap, kArraySize, EqualKey, MapInit> > { |
| 793 | typedef base::SmallMap<NormalMap, kArraySize, EqualKey, MapInit> param_type; |
| 794 | typedef typename param_type::key_type K; |
| 795 | typedef typename param_type::data_type V; |
| 796 | static void Write(Message* m, const param_type& p) { |
| 797 | WriteParam(m, static_cast<int>(p.size())); |
| 798 | typename param_type::const_iterator iter; |
| 799 | for (iter = p.begin(); iter != p.end(); ++iter) { |
| 800 | WriteParam(m, iter->first); |
| 801 | WriteParam(m, iter->second); |
| 802 | } |
| 803 | } |
brettw | f314620 | 2015-06-03 13:29:25 +0900 | [diff] [blame] | 804 | static bool Read(const Message* m, |
| 805 | base::PickleIterator* iter, |
brettw | a487947 | 2015-06-02 16:02:47 +0900 | [diff] [blame] | 806 | param_type* r) { |
jdduke@chromium.org | 556db8b | 2014-03-19 05:48:35 +0900 | [diff] [blame] | 807 | int size; |
avi | c9f0ad0 | 2014-12-29 08:31:48 +0900 | [diff] [blame] | 808 | if (!iter->ReadLength(&size)) |
jdduke@chromium.org | 556db8b | 2014-03-19 05:48:35 +0900 | [diff] [blame] | 809 | return false; |
| 810 | for (int i = 0; i < size; ++i) { |
| 811 | K key; |
| 812 | if (!ReadParam(m, iter, &key)) |
| 813 | return false; |
| 814 | V& value = (*r)[key]; |
| 815 | if (!ReadParam(m, iter, &value)) |
| 816 | return false; |
| 817 | } |
| 818 | return true; |
| 819 | } |
| 820 | static void Log(const param_type& p, std::string* l) { |
| 821 | l->append("<base::SmallMap>"); |
| 822 | } |
| 823 | }; |
| 824 | |
jdduke@chromium.org | c6d80f0 | 2014-05-20 11:34:56 +0900 | [diff] [blame] | 825 | template <class P> |
| 826 | struct ParamTraits<scoped_ptr<P> > { |
| 827 | typedef scoped_ptr<P> param_type; |
| 828 | static void Write(Message* m, const param_type& p) { |
| 829 | bool valid = !!p; |
| 830 | WriteParam(m, valid); |
| 831 | if (valid) |
| 832 | WriteParam(m, *p); |
| 833 | } |
brettw | f314620 | 2015-06-03 13:29:25 +0900 | [diff] [blame] | 834 | static bool Read(const Message* m, |
| 835 | base::PickleIterator* iter, |
brettw | a487947 | 2015-06-02 16:02:47 +0900 | [diff] [blame] | 836 | param_type* r) { |
jdduke@chromium.org | c6d80f0 | 2014-05-20 11:34:56 +0900 | [diff] [blame] | 837 | bool valid = false; |
| 838 | if (!ReadParam(m, iter, &valid)) |
| 839 | return false; |
| 840 | |
| 841 | if (!valid) { |
| 842 | r->reset(); |
| 843 | return true; |
| 844 | } |
| 845 | |
| 846 | param_type temp(new P()); |
| 847 | if (!ReadParam(m, iter, temp.get())) |
| 848 | return false; |
| 849 | |
| 850 | r->swap(temp); |
| 851 | return true; |
| 852 | } |
| 853 | static void Log(const param_type& p, std::string* l) { |
| 854 | if (p) |
| 855 | LogParam(*p, l); |
| 856 | else |
| 857 | l->append("NULL"); |
| 858 | } |
| 859 | }; |
| 860 | |
brettw@chromium.org | ce352e5 | 2012-06-05 06:18:25 +0900 | [diff] [blame] | 861 | // IPC types ParamTraits ------------------------------------------------------- |
| 862 | |
| 863 | // A ChannelHandle is basically a platform-inspecific wrapper around the |
| 864 | // fact that IPC endpoints are handled specially on POSIX. See above comments |
| 865 | // on FileDescriptor for more background. |
| 866 | template<> |
| 867 | struct IPC_EXPORT ParamTraits<IPC::ChannelHandle> { |
| 868 | typedef ChannelHandle param_type; |
| 869 | static void Write(Message* m, const param_type& p); |
brettw | f314620 | 2015-06-03 13:29:25 +0900 | [diff] [blame] | 870 | static bool Read(const Message* m, |
| 871 | base::PickleIterator* iter, |
| 872 | param_type* r); |
brettw@chromium.org | ce352e5 | 2012-06-05 06:18:25 +0900 | [diff] [blame] | 873 | static void Log(const param_type& p, std::string* l); |
| 874 | }; |
| 875 | |
| 876 | template <> |
| 877 | struct IPC_EXPORT ParamTraits<LogData> { |
| 878 | typedef LogData param_type; |
| 879 | static void Write(Message* m, const param_type& p); |
brettw | f314620 | 2015-06-03 13:29:25 +0900 | [diff] [blame] | 880 | static bool Read(const Message* m, |
| 881 | base::PickleIterator* iter, |
| 882 | param_type* r); |
brettw@chromium.org | ce352e5 | 2012-06-05 06:18:25 +0900 | [diff] [blame] | 883 | static void Log(const param_type& p, std::string* l); |
| 884 | }; |
| 885 | |
| 886 | template <> |
| 887 | struct IPC_EXPORT ParamTraits<Message> { |
| 888 | static void Write(Message* m, const Message& p); |
brettw | f314620 | 2015-06-03 13:29:25 +0900 | [diff] [blame] | 889 | static bool Read(const Message* m, |
| 890 | base::PickleIterator* iter, |
| 891 | Message* r); |
brettw@chromium.org | ce352e5 | 2012-06-05 06:18:25 +0900 | [diff] [blame] | 892 | static void Log(const Message& p, std::string* l); |
| 893 | }; |
| 894 | |
| 895 | // Windows ParamTraits --------------------------------------------------------- |
| 896 | |
| 897 | #if defined(OS_WIN) |
| 898 | template <> |
| 899 | struct IPC_EXPORT ParamTraits<HANDLE> { |
| 900 | typedef HANDLE param_type; |
| 901 | static void Write(Message* m, const param_type& p); |
brettw | f314620 | 2015-06-03 13:29:25 +0900 | [diff] [blame] | 902 | static bool Read(const Message* m, |
| 903 | base::PickleIterator* iter, |
| 904 | param_type* r); |
brettw@chromium.org | ce352e5 | 2012-06-05 06:18:25 +0900 | [diff] [blame] | 905 | static void Log(const param_type& p, std::string* l); |
| 906 | }; |
| 907 | |
| 908 | template <> |
| 909 | struct IPC_EXPORT ParamTraits<LOGFONT> { |
| 910 | typedef LOGFONT param_type; |
| 911 | static void Write(Message* m, const param_type& p); |
brettw | f314620 | 2015-06-03 13:29:25 +0900 | [diff] [blame] | 912 | static bool Read(const Message* m, |
| 913 | base::PickleIterator* iter, |
| 914 | param_type* r); |
brettw@chromium.org | ce352e5 | 2012-06-05 06:18:25 +0900 | [diff] [blame] | 915 | static void Log(const param_type& p, std::string* l); |
| 916 | }; |
| 917 | |
| 918 | template <> |
| 919 | struct IPC_EXPORT ParamTraits<MSG> { |
| 920 | typedef MSG param_type; |
| 921 | static void Write(Message* m, const param_type& p); |
brettw | f314620 | 2015-06-03 13:29:25 +0900 | [diff] [blame] | 922 | static bool Read(const Message* m, |
| 923 | base::PickleIterator* iter, |
| 924 | param_type* r); |
brettw@chromium.org | ce352e5 | 2012-06-05 06:18:25 +0900 | [diff] [blame] | 925 | static void Log(const param_type& p, std::string* l); |
| 926 | }; |
| 927 | #endif // defined(OS_WIN) |
| 928 | |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 929 | //----------------------------------------------------------------------------- |
| 930 | // Generic message subclasses |
| 931 | |
| 932 | // Used for asynchronous messages. |
| 933 | template <class ParamType> |
darin@chromium.org | b4587d5 | 2011-08-27 06:27:30 +0900 | [diff] [blame] | 934 | class MessageSchema { |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 935 | public: |
| 936 | typedef ParamType Param; |
brettw | 79a9090 | 2015-05-30 07:15:47 +0900 | [diff] [blame] | 937 | typedef typename base::TupleTypes<ParamType>::ParamTuple RefParam; |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 938 | |
darin@chromium.org | b4587d5 | 2011-08-27 06:27:30 +0900 | [diff] [blame] | 939 | static void Write(Message* msg, const RefParam& p) IPC_MSG_NOINLINE; |
erg@google.com | e6ffcb5 | 2010-08-18 03:38:24 +0900 | [diff] [blame] | 940 | static bool Read(const Message* msg, Param* p) IPC_MSG_NOINLINE; |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 941 | }; |
| 942 | |
erg@google.com | e6ffcb5 | 2010-08-18 03:38:24 +0900 | [diff] [blame] | 943 | // defined in ipc_logging.cc |
darin@chromium.org | 80e4c5e | 2011-08-16 05:41:46 +0900 | [diff] [blame] | 944 | IPC_EXPORT void GenerateLogData(const std::string& channel, |
| 945 | const Message& message, |
scottbyer@chromium.org | f0cf0b7 | 2011-10-21 04:34:43 +0900 | [diff] [blame] | 946 | LogData* data, bool get_params); |
erg@google.com | e6ffcb5 | 2010-08-18 03:38:24 +0900 | [diff] [blame] | 947 | |
| 948 | |
| 949 | #if defined(IPC_MESSAGE_LOG_ENABLED) |
erg@google.com | 8aca727 | 2010-08-19 03:33:57 +0900 | [diff] [blame] | 950 | inline void AddOutputParamsToLog(const Message* msg, std::string* l) { |
| 951 | const std::string& output_params = msg->output_params(); |
erg@google.com | e6ffcb5 | 2010-08-18 03:38:24 +0900 | [diff] [blame] | 952 | if (!l->empty() && !output_params.empty()) |
erg@google.com | 8aca727 | 2010-08-19 03:33:57 +0900 | [diff] [blame] | 953 | l->append(", "); |
erg@google.com | e6ffcb5 | 2010-08-18 03:38:24 +0900 | [diff] [blame] | 954 | |
| 955 | l->append(output_params); |
| 956 | } |
| 957 | |
| 958 | template <class ReplyParamType> |
| 959 | inline void LogReplyParamsToMessage(const ReplyParamType& reply_params, |
| 960 | const Message* msg) { |
| 961 | if (msg->received_time() != 0) { |
erg@google.com | 8aca727 | 2010-08-19 03:33:57 +0900 | [diff] [blame] | 962 | std::string output_params; |
erg@google.com | e6ffcb5 | 2010-08-18 03:38:24 +0900 | [diff] [blame] | 963 | LogParam(reply_params, &output_params); |
| 964 | msg->set_output_params(output_params); |
| 965 | } |
| 966 | } |
| 967 | |
| 968 | inline void ConnectMessageAndReply(const Message* msg, Message* reply) { |
| 969 | if (msg->sent_time()) { |
| 970 | // Don't log the sync message after dispatch, as we don't have the |
| 971 | // output parameters at that point. Instead, save its data and log it |
| 972 | // with the outgoing reply message when it's sent. |
| 973 | LogData* data = new LogData; |
scottbyer@chromium.org | f0cf0b7 | 2011-10-21 04:34:43 +0900 | [diff] [blame] | 974 | GenerateLogData("", *msg, data, true); |
erg@google.com | e6ffcb5 | 2010-08-18 03:38:24 +0900 | [diff] [blame] | 975 | msg->set_dont_log(); |
| 976 | reply->set_sync_log_data(data); |
| 977 | } |
| 978 | } |
| 979 | #else |
erg@google.com | 8aca727 | 2010-08-19 03:33:57 +0900 | [diff] [blame] | 980 | inline void AddOutputParamsToLog(const Message* msg, std::string* l) {} |
erg@google.com | e6ffcb5 | 2010-08-18 03:38:24 +0900 | [diff] [blame] | 981 | |
| 982 | template <class ReplyParamType> |
| 983 | inline void LogReplyParamsToMessage(const ReplyParamType& reply_params, |
| 984 | const Message* msg) {} |
| 985 | |
| 986 | inline void ConnectMessageAndReply(const Message* msg, Message* reply) {} |
| 987 | #endif |
| 988 | |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 989 | // This class assumes that its template argument is a RefTuple (a Tuple with |
erg@google.com | e6ffcb5 | 2010-08-18 03:38:24 +0900 | [diff] [blame] | 990 | // reference elements). This would go into ipc_message_utils_impl.h, but it is |
| 991 | // also used by chrome_frame. |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 992 | template <class RefTuple> |
| 993 | class ParamDeserializer : public MessageReplyDeserializer { |
| 994 | public: |
| 995 | explicit ParamDeserializer(const RefTuple& out) : out_(out) { } |
| 996 | |
dmichael | a5a9c96 | 2014-12-18 10:53:14 +0900 | [diff] [blame] | 997 | bool SerializeOutputParameters(const IPC::Message& msg, |
brettw | a487947 | 2015-06-02 16:02:47 +0900 | [diff] [blame] | 998 | base::PickleIterator iter) override { |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 999 | return ReadParam(&msg, &iter, &out_); |
| 1000 | } |
| 1001 | |
| 1002 | RefTuple out_; |
| 1003 | }; |
| 1004 | |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 1005 | // Used for synchronous messages. |
| 1006 | template <class SendParamType, class ReplyParamType> |
darin@chromium.org | b4587d5 | 2011-08-27 06:27:30 +0900 | [diff] [blame] | 1007 | class SyncMessageSchema { |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 1008 | public: |
| 1009 | typedef SendParamType SendParam; |
brettw | 79a9090 | 2015-05-30 07:15:47 +0900 | [diff] [blame] | 1010 | typedef typename base::TupleTypes<SendParam>::ParamTuple RefSendParam; |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 1011 | typedef ReplyParamType ReplyParam; |
| 1012 | |
darin@chromium.org | b4587d5 | 2011-08-27 06:27:30 +0900 | [diff] [blame] | 1013 | static void Write(Message* msg, const RefSendParam& send) IPC_MSG_NOINLINE; |
erg@google.com | e6ffcb5 | 2010-08-18 03:38:24 +0900 | [diff] [blame] | 1014 | static bool ReadSendParam(const Message* msg, SendParam* p) IPC_MSG_NOINLINE; |
| 1015 | static bool ReadReplyParam( |
| 1016 | const Message* msg, |
brettw | 79a9090 | 2015-05-30 07:15:47 +0900 | [diff] [blame] | 1017 | typename base::TupleTypes<ReplyParam>::ValueTuple* p) IPC_MSG_NOINLINE; |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 1018 | |
jam@chromium.org | 82f52f8 | 2010-12-22 05:03:24 +0900 | [diff] [blame] | 1019 | template<class T, class S, class Method> |
darin@chromium.org | b4587d5 | 2011-08-27 06:27:30 +0900 | [diff] [blame] | 1020 | static bool DispatchWithSendParams(bool ok, const SendParam& send_params, |
| 1021 | const Message* msg, T* obj, S* sender, |
| 1022 | Method func) { |
| 1023 | Message* reply = SyncMessage::GenerateReply(msg); |
| 1024 | if (ok) { |
brettw | 79a9090 | 2015-05-30 07:15:47 +0900 | [diff] [blame] | 1025 | typename base::TupleTypes<ReplyParam>::ValueTuple reply_params; |
tzik | 6c7c93f | 2015-11-20 08:31:21 +0900 | [diff] [blame] | 1026 | base::DispatchToMethod(obj, func, send_params, &reply_params); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 1027 | WriteParam(reply, reply_params); |
erg@google.com | e6ffcb5 | 2010-08-18 03:38:24 +0900 | [diff] [blame] | 1028 | LogReplyParamsToMessage(reply_params, msg); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 1029 | } else { |
| 1030 | NOTREACHED() << "Error deserializing message " << msg->type(); |
| 1031 | reply->set_reply_error(); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 1032 | } |
jam@chromium.org | 82f52f8 | 2010-12-22 05:03:24 +0900 | [diff] [blame] | 1033 | sender->Send(reply); |
darin@chromium.org | b4587d5 | 2011-08-27 06:27:30 +0900 | [diff] [blame] | 1034 | return ok; |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 1035 | } |
| 1036 | |
| 1037 | template<class T, class Method> |
darin@chromium.org | b4587d5 | 2011-08-27 06:27:30 +0900 | [diff] [blame] | 1038 | static bool DispatchDelayReplyWithSendParams(bool ok, |
| 1039 | const SendParam& send_params, |
| 1040 | const Message* msg, T* obj, |
| 1041 | Method func) { |
| 1042 | Message* reply = SyncMessage::GenerateReply(msg); |
| 1043 | if (ok) { |
brettw | 79a9090 | 2015-05-30 07:15:47 +0900 | [diff] [blame] | 1044 | base::Tuple<Message&> t = base::MakeRefTuple(*reply); |
erg@google.com | e6ffcb5 | 2010-08-18 03:38:24 +0900 | [diff] [blame] | 1045 | ConnectMessageAndReply(msg, reply); |
tzik | 6c7c93f | 2015-11-20 08:31:21 +0900 | [diff] [blame] | 1046 | base::DispatchToMethod(obj, func, send_params, &t); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 1047 | } else { |
| 1048 | NOTREACHED() << "Error deserializing message " << msg->type(); |
| 1049 | reply->set_reply_error(); |
| 1050 | obj->Send(reply); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 1051 | } |
darin@chromium.org | b4587d5 | 2011-08-27 06:27:30 +0900 | [diff] [blame] | 1052 | return ok; |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 1053 | } |
| 1054 | |
mikhail.pozdnyakov | 4c5bdf1 | 2015-03-26 21:24:40 +0900 | [diff] [blame] | 1055 | template <typename... Ts> |
| 1056 | static void WriteReplyParams(Message* reply, Ts... args) { |
| 1057 | ReplyParam p(args...); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 1058 | WriteParam(reply, p); |
| 1059 | } |
| 1060 | }; |
| 1061 | |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 1062 | } // namespace IPC |
| 1063 | |
| 1064 | #endif // IPC_IPC_MESSAGE_UTILS_H_ |