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