| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef IPC_IPC_MESSAGE_UTILS_H_ | 5 #ifndef IPC_IPC_MESSAGE_UTILS_H_ |
| 6 #define IPC_IPC_MESSAGE_UTILS_H_ | 6 #define IPC_IPC_MESSAGE_UTILS_H_ |
| 7 | 7 |
| 8 #include <limits.h> | 8 #include <limits.h> |
| 9 #include <stddef.h> | 9 #include <stddef.h> |
| 10 #include <stdint.h> | 10 #include <stdint.h> |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 93 }; | 93 }; |
| 94 | 94 |
| 95 //----------------------------------------------------------------------------- | 95 //----------------------------------------------------------------------------- |
| 96 | 96 |
| 97 // A dummy struct to place first just to allow leading commas for all | 97 // A dummy struct to place first just to allow leading commas for all |
| 98 // members in the macro-generated constructor initializer lists. | 98 // members in the macro-generated constructor initializer lists. |
| 99 struct NoParams { | 99 struct NoParams { |
| 100 }; | 100 }; |
| 101 | 101 |
| 102 template <class P> | 102 template <class P> |
| 103 static inline void WriteParam(Message* m, const P& p) { | 103 static inline void WriteParam(base::Pickle* m, const P& p) { |
| 104 typedef typename SimilarTypeTraits<P>::Type Type; | 104 typedef typename SimilarTypeTraits<P>::Type Type; |
| 105 ParamTraits<Type>::Write(m, static_cast<const Type& >(p)); | 105 ParamTraits<Type>::Write(m, static_cast<const Type& >(p)); |
| 106 } | 106 } |
| 107 | 107 |
| 108 template <class P> | 108 template <class P> |
| 109 static inline bool WARN_UNUSED_RESULT | 109 static inline bool WARN_UNUSED_RESULT ReadParam(const base::Pickle* m, |
| 110 ReadParam(const Message* m, base::PickleIterator* iter, P* p) { | 110 base::PickleIterator* iter, |
| 111 P* p) { |
| 111 typedef typename SimilarTypeTraits<P>::Type Type; | 112 typedef typename SimilarTypeTraits<P>::Type Type; |
| 112 return ParamTraits<Type>::Read(m, iter, reinterpret_cast<Type* >(p)); | 113 return ParamTraits<Type>::Read(m, iter, reinterpret_cast<Type* >(p)); |
| 113 } | 114 } |
| 114 | 115 |
| 115 template <class P> | 116 template <class P> |
| 116 static inline void LogParam(const P& p, std::string* l) { | 117 static inline void LogParam(const P& p, std::string* l) { |
| 117 typedef typename SimilarTypeTraits<P>::Type Type; | 118 typedef typename SimilarTypeTraits<P>::Type Type; |
| 118 ParamTraits<Type>::Log(static_cast<const Type& >(p), l); | 119 ParamTraits<Type>::Log(static_cast<const Type& >(p), l); |
| 119 } | 120 } |
| 120 | 121 |
| 121 // Primitive ParamTraits ------------------------------------------------------- | 122 // Primitive ParamTraits ------------------------------------------------------- |
| 122 | 123 |
| 123 template <> | 124 template <> |
| 124 struct ParamTraits<bool> { | 125 struct ParamTraits<bool> { |
| 125 typedef bool param_type; | 126 typedef bool param_type; |
| 126 static void Write(Message* m, const param_type& p) { | 127 static void Write(base::Pickle* m, const param_type& p) { m->WriteBool(p); } |
| 127 m->WriteBool(p); | 128 static bool Read(const base::Pickle* m, |
| 128 } | |
| 129 static bool Read(const Message* m, | |
| 130 base::PickleIterator* iter, | 129 base::PickleIterator* iter, |
| 131 param_type* r) { | 130 param_type* r) { |
| 132 return iter->ReadBool(r); | 131 return iter->ReadBool(r); |
| 133 } | 132 } |
| 134 IPC_EXPORT static void Log(const param_type& p, std::string* l); | 133 IPC_EXPORT static void Log(const param_type& p, std::string* l); |
| 135 }; | 134 }; |
| 136 | 135 |
| 137 template <> | 136 template <> |
| 138 struct IPC_EXPORT ParamTraits<signed char> { | 137 struct IPC_EXPORT ParamTraits<signed char> { |
| 139 typedef signed char param_type; | 138 typedef signed char param_type; |
| 140 static void Write(Message* m, const param_type& p); | 139 static void Write(base::Pickle* m, const param_type& p); |
| 141 static bool Read(const Message* m, base::PickleIterator* iter, param_type* r); | 140 static bool Read(const base::Pickle* m, |
| 141 base::PickleIterator* iter, |
| 142 param_type* r); |
| 142 static void Log(const param_type& p, std::string* l); | 143 static void Log(const param_type& p, std::string* l); |
| 143 }; | 144 }; |
| 144 | 145 |
| 145 template <> | 146 template <> |
| 146 struct IPC_EXPORT ParamTraits<unsigned char> { | 147 struct IPC_EXPORT ParamTraits<unsigned char> { |
| 147 typedef unsigned char param_type; | 148 typedef unsigned char param_type; |
| 148 static void Write(Message* m, const param_type& p); | 149 static void Write(base::Pickle* m, const param_type& p); |
| 149 static bool Read(const Message* m, base::PickleIterator* iter, param_type* r); | 150 static bool Read(const base::Pickle* m, |
| 151 base::PickleIterator* iter, |
| 152 param_type* r); |
| 150 static void Log(const param_type& p, std::string* l); | 153 static void Log(const param_type& p, std::string* l); |
| 151 }; | 154 }; |
| 152 | 155 |
| 153 template <> | 156 template <> |
| 154 struct IPC_EXPORT ParamTraits<unsigned short> { | 157 struct IPC_EXPORT ParamTraits<unsigned short> { |
| 155 typedef unsigned short param_type; | 158 typedef unsigned short param_type; |
| 156 static void Write(Message* m, const param_type& p); | 159 static void Write(base::Pickle* m, const param_type& p); |
| 157 static bool Read(const Message* m, | 160 static bool Read(const base::Pickle* m, |
| 158 base::PickleIterator* iter, | 161 base::PickleIterator* iter, |
| 159 param_type* r); | 162 param_type* r); |
| 160 static void Log(const param_type& p, std::string* l); | 163 static void Log(const param_type& p, std::string* l); |
| 161 }; | 164 }; |
| 162 | 165 |
| 163 template <> | 166 template <> |
| 164 struct ParamTraits<int> { | 167 struct ParamTraits<int> { |
| 165 typedef int param_type; | 168 typedef int param_type; |
| 166 static void Write(Message* m, const param_type& p) { | 169 static void Write(base::Pickle* m, const param_type& p) { m->WriteInt(p); } |
| 167 m->WriteInt(p); | 170 static bool Read(const base::Pickle* m, |
| 168 } | |
| 169 static bool Read(const Message* m, | |
| 170 base::PickleIterator* iter, | 171 base::PickleIterator* iter, |
| 171 param_type* r) { | 172 param_type* r) { |
| 172 return iter->ReadInt(r); | 173 return iter->ReadInt(r); |
| 173 } | 174 } |
| 174 IPC_EXPORT static void Log(const param_type& p, std::string* l); | 175 IPC_EXPORT static void Log(const param_type& p, std::string* l); |
| 175 }; | 176 }; |
| 176 | 177 |
| 177 template <> | 178 template <> |
| 178 struct ParamTraits<unsigned int> { | 179 struct ParamTraits<unsigned int> { |
| 179 typedef unsigned int param_type; | 180 typedef unsigned int param_type; |
| 180 static void Write(Message* m, const param_type& p) { | 181 static void Write(base::Pickle* m, const param_type& p) { m->WriteInt(p); } |
| 181 m->WriteInt(p); | 182 static bool Read(const base::Pickle* m, |
| 182 } | |
| 183 static bool Read(const Message* m, | |
| 184 base::PickleIterator* iter, | 183 base::PickleIterator* iter, |
| 185 param_type* r) { | 184 param_type* r) { |
| 186 return iter->ReadInt(reinterpret_cast<int*>(r)); | 185 return iter->ReadInt(reinterpret_cast<int*>(r)); |
| 187 } | 186 } |
| 188 IPC_EXPORT static void Log(const param_type& p, std::string* l); | 187 IPC_EXPORT static void Log(const param_type& p, std::string* l); |
| 189 }; | 188 }; |
| 190 | 189 |
| 191 template <> | 190 template <> |
| 192 struct ParamTraits<long> { | 191 struct ParamTraits<long> { |
| 193 typedef long param_type; | 192 typedef long param_type; |
| 194 static void Write(Message* m, const param_type& p) { | 193 static void Write(base::Pickle* m, const param_type& p) { |
| 195 m->WriteLongUsingDangerousNonPortableLessPersistableForm(p); | 194 m->WriteLongUsingDangerousNonPortableLessPersistableForm(p); |
| 196 } | 195 } |
| 197 static bool Read(const Message* m, | 196 static bool Read(const base::Pickle* m, |
| 198 base::PickleIterator* iter, | 197 base::PickleIterator* iter, |
| 199 param_type* r) { | 198 param_type* r) { |
| 200 return iter->ReadLong(r); | 199 return iter->ReadLong(r); |
| 201 } | 200 } |
| 202 IPC_EXPORT static void Log(const param_type& p, std::string* l); | 201 IPC_EXPORT static void Log(const param_type& p, std::string* l); |
| 203 }; | 202 }; |
| 204 | 203 |
| 205 template <> | 204 template <> |
| 206 struct ParamTraits<unsigned long> { | 205 struct ParamTraits<unsigned long> { |
| 207 typedef unsigned long param_type; | 206 typedef unsigned long param_type; |
| 208 static void Write(Message* m, const param_type& p) { | 207 static void Write(base::Pickle* m, const param_type& p) { |
| 209 m->WriteLongUsingDangerousNonPortableLessPersistableForm(p); | 208 m->WriteLongUsingDangerousNonPortableLessPersistableForm(p); |
| 210 } | 209 } |
| 211 static bool Read(const Message* m, | 210 static bool Read(const base::Pickle* m, |
| 212 base::PickleIterator* iter, | 211 base::PickleIterator* iter, |
| 213 param_type* r) { | 212 param_type* r) { |
| 214 return iter->ReadLong(reinterpret_cast<long*>(r)); | 213 return iter->ReadLong(reinterpret_cast<long*>(r)); |
| 215 } | 214 } |
| 216 IPC_EXPORT static void Log(const param_type& p, std::string* l); | 215 IPC_EXPORT static void Log(const param_type& p, std::string* l); |
| 217 }; | 216 }; |
| 218 | 217 |
| 219 template <> | 218 template <> |
| 220 struct ParamTraits<long long> { | 219 struct ParamTraits<long long> { |
| 221 typedef long long param_type; | 220 typedef long long param_type; |
| 222 static void Write(Message* m, const param_type& p) { | 221 static void Write(base::Pickle* m, const param_type& p) { |
| 223 m->WriteInt64(static_cast<int64_t>(p)); | 222 m->WriteInt64(static_cast<int64_t>(p)); |
| 224 } | 223 } |
| 225 static bool Read(const Message* m, | 224 static bool Read(const base::Pickle* m, |
| 226 base::PickleIterator* iter, | 225 base::PickleIterator* iter, |
| 227 param_type* r) { | 226 param_type* r) { |
| 228 return iter->ReadInt64(reinterpret_cast<int64_t*>(r)); | 227 return iter->ReadInt64(reinterpret_cast<int64_t*>(r)); |
| 229 } | 228 } |
| 230 IPC_EXPORT static void Log(const param_type& p, std::string* l); | 229 IPC_EXPORT static void Log(const param_type& p, std::string* l); |
| 231 }; | 230 }; |
| 232 | 231 |
| 233 template <> | 232 template <> |
| 234 struct ParamTraits<unsigned long long> { | 233 struct ParamTraits<unsigned long long> { |
| 235 typedef unsigned long long param_type; | 234 typedef unsigned long long param_type; |
| 236 static void Write(Message* m, const param_type& p) { | 235 static void Write(base::Pickle* m, const param_type& p) { m->WriteInt64(p); } |
| 237 m->WriteInt64(p); | 236 static bool Read(const base::Pickle* m, |
| 238 } | |
| 239 static bool Read(const Message* m, | |
| 240 base::PickleIterator* iter, | 237 base::PickleIterator* iter, |
| 241 param_type* r) { | 238 param_type* r) { |
| 242 return iter->ReadInt64(reinterpret_cast<int64_t*>(r)); | 239 return iter->ReadInt64(reinterpret_cast<int64_t*>(r)); |
| 243 } | 240 } |
| 244 IPC_EXPORT static void Log(const param_type& p, std::string* l); | 241 IPC_EXPORT static void Log(const param_type& p, std::string* l); |
| 245 }; | 242 }; |
| 246 | 243 |
| 247 // Note that the IPC layer doesn't sanitize NaNs and +/- INF values. Clients | 244 // Note that the IPC layer doesn't sanitize NaNs and +/- INF values. Clients |
| 248 // should be sure to check the sanity of these values after receiving them over | 245 // should be sure to check the sanity of these values after receiving them over |
| 249 // IPC. | 246 // IPC. |
| 250 template <> | 247 template <> |
| 251 struct IPC_EXPORT ParamTraits<float> { | 248 struct IPC_EXPORT ParamTraits<float> { |
| 252 typedef float param_type; | 249 typedef float param_type; |
| 253 static void Write(Message* m, const param_type& p) { | 250 static void Write(base::Pickle* m, const param_type& p) { m->WriteFloat(p); } |
| 254 m->WriteFloat(p); | 251 static bool Read(const base::Pickle* m, |
| 255 } | |
| 256 static bool Read(const Message* m, | |
| 257 base::PickleIterator* iter, | 252 base::PickleIterator* iter, |
| 258 param_type* r) { | 253 param_type* r) { |
| 259 return iter->ReadFloat(r); | 254 return iter->ReadFloat(r); |
| 260 } | 255 } |
| 261 static void Log(const param_type& p, std::string* l); | 256 static void Log(const param_type& p, std::string* l); |
| 262 }; | 257 }; |
| 263 | 258 |
| 264 template <> | 259 template <> |
| 265 struct IPC_EXPORT ParamTraits<double> { | 260 struct IPC_EXPORT ParamTraits<double> { |
| 266 typedef double param_type; | 261 typedef double param_type; |
| 267 static void Write(Message* m, const param_type& p); | 262 static void Write(base::Pickle* m, const param_type& p); |
| 268 static bool Read(const Message* m, | 263 static bool Read(const base::Pickle* m, |
| 269 base::PickleIterator* iter, | 264 base::PickleIterator* iter, |
| 270 param_type* r); | 265 param_type* r); |
| 271 static void Log(const param_type& p, std::string* l); | 266 static void Log(const param_type& p, std::string* l); |
| 272 }; | 267 }; |
| 273 | 268 |
| 274 // STL ParamTraits ------------------------------------------------------------- | 269 // STL ParamTraits ------------------------------------------------------------- |
| 275 | 270 |
| 276 template <> | 271 template <> |
| 277 struct ParamTraits<std::string> { | 272 struct ParamTraits<std::string> { |
| 278 typedef std::string param_type; | 273 typedef std::string param_type; |
| 279 static void Write(Message* m, const param_type& p) { | 274 static void Write(base::Pickle* m, const param_type& p) { m->WriteString(p); } |
| 280 m->WriteString(p); | 275 static bool Read(const base::Pickle* m, |
| 281 } | |
| 282 static bool Read(const Message* m, | |
| 283 base::PickleIterator* iter, | 276 base::PickleIterator* iter, |
| 284 param_type* r) { | 277 param_type* r) { |
| 285 return iter->ReadString(r); | 278 return iter->ReadString(r); |
| 286 } | 279 } |
| 287 IPC_EXPORT static void Log(const param_type& p, std::string* l); | 280 IPC_EXPORT static void Log(const param_type& p, std::string* l); |
| 288 }; | 281 }; |
| 289 | 282 |
| 290 template <> | 283 template <> |
| 291 struct ParamTraits<base::string16> { | 284 struct ParamTraits<base::string16> { |
| 292 typedef base::string16 param_type; | 285 typedef base::string16 param_type; |
| 293 static void Write(Message* m, const param_type& p) { | 286 static void Write(base::Pickle* m, const param_type& p) { |
| 294 m->WriteString16(p); | 287 m->WriteString16(p); |
| 295 } | 288 } |
| 296 static bool Read(const Message* m, | 289 static bool Read(const base::Pickle* m, |
| 297 base::PickleIterator* iter, | 290 base::PickleIterator* iter, |
| 298 param_type* r) { | 291 param_type* r) { |
| 299 return iter->ReadString16(r); | 292 return iter->ReadString16(r); |
| 300 } | 293 } |
| 301 IPC_EXPORT static void Log(const param_type& p, std::string* l); | 294 IPC_EXPORT static void Log(const param_type& p, std::string* l); |
| 302 }; | 295 }; |
| 303 | 296 |
| 304 template <> | 297 template <> |
| 305 struct IPC_EXPORT ParamTraits<std::vector<char> > { | 298 struct IPC_EXPORT ParamTraits<std::vector<char> > { |
| 306 typedef std::vector<char> param_type; | 299 typedef std::vector<char> param_type; |
| 307 static void Write(Message* m, const param_type& p); | 300 static void Write(base::Pickle* m, const param_type& p); |
| 308 static bool Read(const Message*, | 301 static bool Read(const base::Pickle*, |
| 309 base::PickleIterator* iter, | 302 base::PickleIterator* iter, |
| 310 param_type* r); | 303 param_type* r); |
| 311 static void Log(const param_type& p, std::string* l); | 304 static void Log(const param_type& p, std::string* l); |
| 312 }; | 305 }; |
| 313 | 306 |
| 314 template <> | 307 template <> |
| 315 struct IPC_EXPORT ParamTraits<std::vector<unsigned char> > { | 308 struct IPC_EXPORT ParamTraits<std::vector<unsigned char> > { |
| 316 typedef std::vector<unsigned char> param_type; | 309 typedef std::vector<unsigned char> param_type; |
| 317 static void Write(Message* m, const param_type& p); | 310 static void Write(base::Pickle* m, const param_type& p); |
| 318 static bool Read(const Message* m, | 311 static bool Read(const base::Pickle* m, |
| 319 base::PickleIterator* iter, | 312 base::PickleIterator* iter, |
| 320 param_type* r); | 313 param_type* r); |
| 321 static void Log(const param_type& p, std::string* l); | 314 static void Log(const param_type& p, std::string* l); |
| 322 }; | 315 }; |
| 323 | 316 |
| 324 template <> | 317 template <> |
| 325 struct IPC_EXPORT ParamTraits<std::vector<bool> > { | 318 struct IPC_EXPORT ParamTraits<std::vector<bool> > { |
| 326 typedef std::vector<bool> param_type; | 319 typedef std::vector<bool> param_type; |
| 327 static void Write(Message* m, const param_type& p); | 320 static void Write(base::Pickle* m, const param_type& p); |
| 328 static bool Read(const Message* m, | 321 static bool Read(const base::Pickle* m, |
| 329 base::PickleIterator* iter, | 322 base::PickleIterator* iter, |
| 330 param_type* r); | 323 param_type* r); |
| 331 static void Log(const param_type& p, std::string* l); | 324 static void Log(const param_type& p, std::string* l); |
| 332 }; | 325 }; |
| 333 | 326 |
| 334 template <class P> | 327 template <class P> |
| 335 struct ParamTraits<std::vector<P> > { | 328 struct ParamTraits<std::vector<P> > { |
| 336 typedef std::vector<P> param_type; | 329 typedef std::vector<P> param_type; |
| 337 static void Write(Message* m, const param_type& p) { | 330 static void Write(base::Pickle* m, const param_type& p) { |
| 338 WriteParam(m, static_cast<int>(p.size())); | 331 WriteParam(m, static_cast<int>(p.size())); |
| 339 for (size_t i = 0; i < p.size(); i++) | 332 for (size_t i = 0; i < p.size(); i++) |
| 340 WriteParam(m, p[i]); | 333 WriteParam(m, p[i]); |
| 341 } | 334 } |
| 342 static bool Read(const Message* m, | 335 static bool Read(const base::Pickle* m, |
| 343 base::PickleIterator* iter, | 336 base::PickleIterator* iter, |
| 344 param_type* r) { | 337 param_type* r) { |
| 345 int size; | 338 int size; |
| 346 // ReadLength() checks for < 0 itself. | 339 // ReadLength() checks for < 0 itself. |
| 347 if (!iter->ReadLength(&size)) | 340 if (!iter->ReadLength(&size)) |
| 348 return false; | 341 return false; |
| 349 // Resizing beforehand is not safe, see BUG 1006367 for details. | 342 // Resizing beforehand is not safe, see BUG 1006367 for details. |
| 350 if (INT_MAX / sizeof(P) <= static_cast<size_t>(size)) | 343 if (INT_MAX / sizeof(P) <= static_cast<size_t>(size)) |
| 351 return false; | 344 return false; |
| 352 r->resize(size); | 345 r->resize(size); |
| 353 for (int i = 0; i < size; i++) { | 346 for (int i = 0; i < size; i++) { |
| 354 if (!ReadParam(m, iter, &(*r)[i])) | 347 if (!ReadParam(m, iter, &(*r)[i])) |
| 355 return false; | 348 return false; |
| 356 } | 349 } |
| 357 return true; | 350 return true; |
| 358 } | 351 } |
| 359 static void Log(const param_type& p, std::string* l) { | 352 static void Log(const param_type& p, std::string* l) { |
| 360 for (size_t i = 0; i < p.size(); ++i) { | 353 for (size_t i = 0; i < p.size(); ++i) { |
| 361 if (i != 0) | 354 if (i != 0) |
| 362 l->append(" "); | 355 l->append(" "); |
| 363 LogParam((p[i]), l); | 356 LogParam((p[i]), l); |
| 364 } | 357 } |
| 365 } | 358 } |
| 366 }; | 359 }; |
| 367 | 360 |
| 368 template <class P> | 361 template <class P> |
| 369 struct ParamTraits<std::set<P> > { | 362 struct ParamTraits<std::set<P> > { |
| 370 typedef std::set<P> param_type; | 363 typedef std::set<P> param_type; |
| 371 static void Write(Message* m, const param_type& p) { | 364 static void Write(base::Pickle* m, const param_type& p) { |
| 372 WriteParam(m, static_cast<int>(p.size())); | 365 WriteParam(m, static_cast<int>(p.size())); |
| 373 typename param_type::const_iterator iter; | 366 typename param_type::const_iterator iter; |
| 374 for (iter = p.begin(); iter != p.end(); ++iter) | 367 for (iter = p.begin(); iter != p.end(); ++iter) |
| 375 WriteParam(m, *iter); | 368 WriteParam(m, *iter); |
| 376 } | 369 } |
| 377 static bool Read(const Message* m, | 370 static bool Read(const base::Pickle* m, |
| 378 base::PickleIterator* iter, | 371 base::PickleIterator* iter, |
| 379 param_type* r) { | 372 param_type* r) { |
| 380 int size; | 373 int size; |
| 381 if (!iter->ReadLength(&size)) | 374 if (!iter->ReadLength(&size)) |
| 382 return false; | 375 return false; |
| 383 for (int i = 0; i < size; ++i) { | 376 for (int i = 0; i < size; ++i) { |
| 384 P item; | 377 P item; |
| 385 if (!ReadParam(m, iter, &item)) | 378 if (!ReadParam(m, iter, &item)) |
| 386 return false; | 379 return false; |
| 387 r->insert(item); | 380 r->insert(item); |
| 388 } | 381 } |
| 389 return true; | 382 return true; |
| 390 } | 383 } |
| 391 static void Log(const param_type& p, std::string* l) { | 384 static void Log(const param_type& p, std::string* l) { |
| 392 l->append("<std::set>"); | 385 l->append("<std::set>"); |
| 393 } | 386 } |
| 394 }; | 387 }; |
| 395 | 388 |
| 396 template <class K, class V, class C, class A> | 389 template <class K, class V, class C, class A> |
| 397 struct ParamTraits<std::map<K, V, C, A> > { | 390 struct ParamTraits<std::map<K, V, C, A> > { |
| 398 typedef std::map<K, V, C, A> param_type; | 391 typedef std::map<K, V, C, A> param_type; |
| 399 static void Write(Message* m, const param_type& p) { | 392 static void Write(base::Pickle* m, const param_type& p) { |
| 400 WriteParam(m, static_cast<int>(p.size())); | 393 WriteParam(m, static_cast<int>(p.size())); |
| 401 typename param_type::const_iterator iter; | 394 typename param_type::const_iterator iter; |
| 402 for (iter = p.begin(); iter != p.end(); ++iter) { | 395 for (iter = p.begin(); iter != p.end(); ++iter) { |
| 403 WriteParam(m, iter->first); | 396 WriteParam(m, iter->first); |
| 404 WriteParam(m, iter->second); | 397 WriteParam(m, iter->second); |
| 405 } | 398 } |
| 406 } | 399 } |
| 407 static bool Read(const Message* m, | 400 static bool Read(const base::Pickle* m, |
| 408 base::PickleIterator* iter, | 401 base::PickleIterator* iter, |
| 409 param_type* r) { | 402 param_type* r) { |
| 410 int size; | 403 int size; |
| 411 if (!ReadParam(m, iter, &size) || size < 0) | 404 if (!ReadParam(m, iter, &size) || size < 0) |
| 412 return false; | 405 return false; |
| 413 for (int i = 0; i < size; ++i) { | 406 for (int i = 0; i < size; ++i) { |
| 414 K k; | 407 K k; |
| 415 if (!ReadParam(m, iter, &k)) | 408 if (!ReadParam(m, iter, &k)) |
| 416 return false; | 409 return false; |
| 417 V& value = (*r)[k]; | 410 V& value = (*r)[k]; |
| 418 if (!ReadParam(m, iter, &value)) | 411 if (!ReadParam(m, iter, &value)) |
| 419 return false; | 412 return false; |
| 420 } | 413 } |
| 421 return true; | 414 return true; |
| 422 } | 415 } |
| 423 static void Log(const param_type& p, std::string* l) { | 416 static void Log(const param_type& p, std::string* l) { |
| 424 l->append("<std::map>"); | 417 l->append("<std::map>"); |
| 425 } | 418 } |
| 426 }; | 419 }; |
| 427 | 420 |
| 428 template <class A, class B> | 421 template <class A, class B> |
| 429 struct ParamTraits<std::pair<A, B> > { | 422 struct ParamTraits<std::pair<A, B> > { |
| 430 typedef std::pair<A, B> param_type; | 423 typedef std::pair<A, B> param_type; |
| 431 static void Write(Message* m, const param_type& p) { | 424 static void Write(base::Pickle* m, const param_type& p) { |
| 432 WriteParam(m, p.first); | 425 WriteParam(m, p.first); |
| 433 WriteParam(m, p.second); | 426 WriteParam(m, p.second); |
| 434 } | 427 } |
| 435 static bool Read(const Message* m, | 428 static bool Read(const base::Pickle* m, |
| 436 base::PickleIterator* iter, | 429 base::PickleIterator* iter, |
| 437 param_type* r) { | 430 param_type* r) { |
| 438 return ReadParam(m, iter, &r->first) && ReadParam(m, iter, &r->second); | 431 return ReadParam(m, iter, &r->first) && ReadParam(m, iter, &r->second); |
| 439 } | 432 } |
| 440 static void Log(const param_type& p, std::string* l) { | 433 static void Log(const param_type& p, std::string* l) { |
| 441 l->append("("); | 434 l->append("("); |
| 442 LogParam(p.first, l); | 435 LogParam(p.first, l); |
| 443 l->append(", "); | 436 l->append(", "); |
| 444 LogParam(p.second, l); | 437 LogParam(p.second, l); |
| 445 l->append(")"); | 438 l->append(")"); |
| 446 } | 439 } |
| 447 }; | 440 }; |
| 448 | 441 |
| 449 // IPC ParamTraits ------------------------------------------------------------- | 442 // IPC ParamTraits ------------------------------------------------------------- |
| 450 template <> | 443 template <> |
| 451 struct IPC_EXPORT ParamTraits<BrokerableAttachment::AttachmentId> { | 444 struct IPC_EXPORT ParamTraits<BrokerableAttachment::AttachmentId> { |
| 452 typedef BrokerableAttachment::AttachmentId param_type; | 445 typedef BrokerableAttachment::AttachmentId param_type; |
| 453 static void Write(Message* m, const param_type& p); | 446 static void Write(base::Pickle* m, const param_type& p); |
| 454 static bool Read(const Message* m, base::PickleIterator* iter, param_type* r); | 447 static bool Read(const base::Pickle* m, |
| 448 base::PickleIterator* iter, |
| 449 param_type* r); |
| 455 static void Log(const param_type& p, std::string* l); | 450 static void Log(const param_type& p, std::string* l); |
| 456 }; | 451 }; |
| 457 | 452 |
| 458 // Base ParamTraits ------------------------------------------------------------ | 453 // Base ParamTraits ------------------------------------------------------------ |
| 459 | 454 |
| 460 template <> | 455 template <> |
| 461 struct IPC_EXPORT ParamTraits<base::DictionaryValue> { | 456 struct IPC_EXPORT ParamTraits<base::DictionaryValue> { |
| 462 typedef base::DictionaryValue param_type; | 457 typedef base::DictionaryValue param_type; |
| 463 static void Write(Message* m, const param_type& p); | 458 static void Write(base::Pickle* m, const param_type& p); |
| 464 static bool Read(const Message* m, | 459 static bool Read(const base::Pickle* m, |
| 465 base::PickleIterator* iter, | 460 base::PickleIterator* iter, |
| 466 param_type* r); | 461 param_type* r); |
| 467 static void Log(const param_type& p, std::string* l); | 462 static void Log(const param_type& p, std::string* l); |
| 468 }; | 463 }; |
| 469 | 464 |
| 470 #if defined(OS_POSIX) | 465 #if defined(OS_POSIX) |
| 471 // FileDescriptors may be serialised over IPC channels on POSIX. On the | 466 // FileDescriptors may be serialised over IPC channels on POSIX. On the |
| 472 // receiving side, the FileDescriptor is a valid duplicate of the file | 467 // receiving side, the FileDescriptor is a valid duplicate of the file |
| 473 // descriptor which was transmitted: *it is not just a copy of the integer like | 468 // descriptor which was transmitted: *it is not just a copy of the integer like |
| 474 // HANDLEs on Windows*. The only exception is if the file descriptor is < 0. In | 469 // HANDLEs on Windows*. The only exception is if the file descriptor is < 0. In |
| 475 // this case, the receiving end will see a value of -1. *Zero is a valid file | 470 // this case, the receiving end will see a value of -1. *Zero is a valid file |
| 476 // descriptor*. | 471 // descriptor*. |
| 477 // | 472 // |
| 478 // The received file descriptor will have the |auto_close| flag set to true. The | 473 // The received file descriptor will have the |auto_close| flag set to true. The |
| 479 // code which handles the message is responsible for taking ownership of it. | 474 // code which handles the message is responsible for taking ownership of it. |
| 480 // File descriptors are OS resources and must be closed when no longer needed. | 475 // File descriptors are OS resources and must be closed when no longer needed. |
| 481 // | 476 // |
| 482 // When sending a file descriptor, the file descriptor must be valid at the time | 477 // When sending a file descriptor, the file descriptor must be valid at the time |
| 483 // of transmission. Since transmission is not synchronous, one should consider | 478 // of transmission. Since transmission is not synchronous, one should consider |
| 484 // dup()ing any file descriptors to be transmitted and setting the |auto_close| | 479 // dup()ing any file descriptors to be transmitted and setting the |auto_close| |
| 485 // flag, which causes the file descriptor to be closed after writing. | 480 // flag, which causes the file descriptor to be closed after writing. |
| 486 template<> | 481 template<> |
| 487 struct IPC_EXPORT ParamTraits<base::FileDescriptor> { | 482 struct IPC_EXPORT ParamTraits<base::FileDescriptor> { |
| 488 typedef base::FileDescriptor param_type; | 483 typedef base::FileDescriptor param_type; |
| 489 static void Write(Message* m, const param_type& p); | 484 static void Write(base::Pickle* m, const param_type& p); |
| 490 static bool Read(const Message* m, | 485 static bool Read(const base::Pickle* m, |
| 491 base::PickleIterator* iter, | 486 base::PickleIterator* iter, |
| 492 param_type* r); | 487 param_type* r); |
| 493 static void Log(const param_type& p, std::string* l); | 488 static void Log(const param_type& p, std::string* l); |
| 494 }; | 489 }; |
| 495 #endif // defined(OS_POSIX) | 490 #endif // defined(OS_POSIX) |
| 496 | 491 |
| 497 #if (defined(OS_MACOSX) && !defined(OS_IOS)) || defined(OS_WIN) | 492 #if (defined(OS_MACOSX) && !defined(OS_IOS)) || defined(OS_WIN) |
| 498 template <> | 493 template <> |
| 499 struct IPC_EXPORT ParamTraits<base::SharedMemoryHandle> { | 494 struct IPC_EXPORT ParamTraits<base::SharedMemoryHandle> { |
| 500 typedef base::SharedMemoryHandle param_type; | 495 typedef base::SharedMemoryHandle param_type; |
| 501 static void Write(Message* m, const param_type& p); | 496 static void Write(base::Pickle* m, const param_type& p); |
| 502 static bool Read(const Message* m, base::PickleIterator* iter, param_type* r); | 497 static bool Read(const base::Pickle* m, |
| 498 base::PickleIterator* iter, |
| 499 param_type* r); |
| 503 static void Log(const param_type& p, std::string* l); | 500 static void Log(const param_type& p, std::string* l); |
| 504 }; | 501 }; |
| 505 #endif // (defined(OS_MACOSX) && !defined(OS_IOS)) || defined(OS_WIN) | 502 #endif // (defined(OS_MACOSX) && !defined(OS_IOS)) || defined(OS_WIN) |
| 506 | 503 |
| 507 template <> | 504 template <> |
| 508 struct IPC_EXPORT ParamTraits<base::FilePath> { | 505 struct IPC_EXPORT ParamTraits<base::FilePath> { |
| 509 typedef base::FilePath param_type; | 506 typedef base::FilePath param_type; |
| 510 static void Write(Message* m, const param_type& p); | 507 static void Write(base::Pickle* m, const param_type& p); |
| 511 static bool Read(const Message* m, | 508 static bool Read(const base::Pickle* m, |
| 512 base::PickleIterator* iter, | 509 base::PickleIterator* iter, |
| 513 param_type* r); | 510 param_type* r); |
| 514 static void Log(const param_type& p, std::string* l); | 511 static void Log(const param_type& p, std::string* l); |
| 515 }; | 512 }; |
| 516 | 513 |
| 517 template <> | 514 template <> |
| 518 struct IPC_EXPORT ParamTraits<base::ListValue> { | 515 struct IPC_EXPORT ParamTraits<base::ListValue> { |
| 519 typedef base::ListValue param_type; | 516 typedef base::ListValue param_type; |
| 520 static void Write(Message* m, const param_type& p); | 517 static void Write(base::Pickle* m, const param_type& p); |
| 521 static bool Read(const Message* m, | 518 static bool Read(const base::Pickle* m, |
| 522 base::PickleIterator* iter, | 519 base::PickleIterator* iter, |
| 523 param_type* r); | 520 param_type* r); |
| 524 static void Log(const param_type& p, std::string* l); | 521 static void Log(const param_type& p, std::string* l); |
| 525 }; | 522 }; |
| 526 | 523 |
| 527 template <> | 524 template <> |
| 528 struct IPC_EXPORT ParamTraits<base::NullableString16> { | 525 struct IPC_EXPORT ParamTraits<base::NullableString16> { |
| 529 typedef base::NullableString16 param_type; | 526 typedef base::NullableString16 param_type; |
| 530 static void Write(Message* m, const param_type& p); | 527 static void Write(base::Pickle* m, const param_type& p); |
| 531 static bool Read(const Message* m, | 528 static bool Read(const base::Pickle* m, |
| 532 base::PickleIterator* iter, | 529 base::PickleIterator* iter, |
| 533 param_type* r); | 530 param_type* r); |
| 534 static void Log(const param_type& p, std::string* l); | 531 static void Log(const param_type& p, std::string* l); |
| 535 }; | 532 }; |
| 536 | 533 |
| 537 template <> | 534 template <> |
| 538 struct IPC_EXPORT ParamTraits<base::File::Info> { | 535 struct IPC_EXPORT ParamTraits<base::File::Info> { |
| 539 typedef base::File::Info param_type; | 536 typedef base::File::Info param_type; |
| 540 static void Write(Message* m, const param_type& p); | 537 static void Write(base::Pickle* m, const param_type& p); |
| 541 static bool Read(const Message* m, | 538 static bool Read(const base::Pickle* m, |
| 542 base::PickleIterator* iter, | 539 base::PickleIterator* iter, |
| 543 param_type* r); | 540 param_type* r); |
| 544 static void Log(const param_type& p, std::string* l); | 541 static void Log(const param_type& p, std::string* l); |
| 545 }; | 542 }; |
| 546 | 543 |
| 547 template <> | 544 template <> |
| 548 struct SimilarTypeTraits<base::File::Error> { | 545 struct SimilarTypeTraits<base::File::Error> { |
| 549 typedef int Type; | 546 typedef int Type; |
| 550 }; | 547 }; |
| 551 | 548 |
| 552 #if defined(OS_WIN) | 549 #if defined(OS_WIN) |
| 553 template <> | 550 template <> |
| 554 struct SimilarTypeTraits<HWND> { | 551 struct SimilarTypeTraits<HWND> { |
| 555 typedef HANDLE Type; | 552 typedef HANDLE Type; |
| 556 }; | 553 }; |
| 557 #endif // defined(OS_WIN) | 554 #endif // defined(OS_WIN) |
| 558 | 555 |
| 559 template <> | 556 template <> |
| 560 struct IPC_EXPORT ParamTraits<base::Time> { | 557 struct IPC_EXPORT ParamTraits<base::Time> { |
| 561 typedef base::Time param_type; | 558 typedef base::Time param_type; |
| 562 static void Write(Message* m, const param_type& p); | 559 static void Write(base::Pickle* m, const param_type& p); |
| 563 static bool Read(const Message* m, | 560 static bool Read(const base::Pickle* m, |
| 564 base::PickleIterator* iter, | 561 base::PickleIterator* iter, |
| 565 param_type* r); | 562 param_type* r); |
| 566 static void Log(const param_type& p, std::string* l); | 563 static void Log(const param_type& p, std::string* l); |
| 567 }; | 564 }; |
| 568 | 565 |
| 569 template <> | 566 template <> |
| 570 struct IPC_EXPORT ParamTraits<base::TimeDelta> { | 567 struct IPC_EXPORT ParamTraits<base::TimeDelta> { |
| 571 typedef base::TimeDelta param_type; | 568 typedef base::TimeDelta param_type; |
| 572 static void Write(Message* m, const param_type& p); | 569 static void Write(base::Pickle* m, const param_type& p); |
| 573 static bool Read(const Message* m, | 570 static bool Read(const base::Pickle* m, |
| 574 base::PickleIterator* iter, | 571 base::PickleIterator* iter, |
| 575 param_type* r); | 572 param_type* r); |
| 576 static void Log(const param_type& p, std::string* l); | 573 static void Log(const param_type& p, std::string* l); |
| 577 }; | 574 }; |
| 578 | 575 |
| 579 template <> | 576 template <> |
| 580 struct IPC_EXPORT ParamTraits<base::TimeTicks> { | 577 struct IPC_EXPORT ParamTraits<base::TimeTicks> { |
| 581 typedef base::TimeTicks param_type; | 578 typedef base::TimeTicks param_type; |
| 582 static void Write(Message* m, const param_type& p); | 579 static void Write(base::Pickle* m, const param_type& p); |
| 583 static bool Read(const Message* m, | 580 static bool Read(const base::Pickle* m, |
| 584 base::PickleIterator* iter, | 581 base::PickleIterator* iter, |
| 585 param_type* r); | 582 param_type* r); |
| 586 static void Log(const param_type& p, std::string* l); | 583 static void Log(const param_type& p, std::string* l); |
| 587 }; | 584 }; |
| 588 | 585 |
| 589 template <> | 586 template <> |
| 590 struct ParamTraits<base::Tuple<>> { | 587 struct ParamTraits<base::Tuple<>> { |
| 591 typedef base::Tuple<> param_type; | 588 typedef base::Tuple<> param_type; |
| 592 static void Write(Message* m, const param_type& p) { | 589 static void Write(base::Pickle* m, const param_type& p) {} |
| 593 } | 590 static bool Read(const base::Pickle* m, |
| 594 static bool Read(const Message* m, | |
| 595 base::PickleIterator* iter, | 591 base::PickleIterator* iter, |
| 596 param_type* r) { | 592 param_type* r) { |
| 597 return true; | 593 return true; |
| 598 } | 594 } |
| 599 static void Log(const param_type& p, std::string* l) { | 595 static void Log(const param_type& p, std::string* l) { |
| 600 } | 596 } |
| 601 }; | 597 }; |
| 602 | 598 |
| 603 template <class A> | 599 template <class A> |
| 604 struct ParamTraits<base::Tuple<A>> { | 600 struct ParamTraits<base::Tuple<A>> { |
| 605 typedef base::Tuple<A> param_type; | 601 typedef base::Tuple<A> param_type; |
| 606 static void Write(Message* m, const param_type& p) { | 602 static void Write(base::Pickle* m, const param_type& p) { |
| 607 WriteParam(m, base::get<0>(p)); | 603 WriteParam(m, base::get<0>(p)); |
| 608 } | 604 } |
| 609 static bool Read(const Message* m, | 605 static bool Read(const base::Pickle* m, |
| 610 base::PickleIterator* iter, | 606 base::PickleIterator* iter, |
| 611 param_type* r) { | 607 param_type* r) { |
| 612 return ReadParam(m, iter, &base::get<0>(*r)); | 608 return ReadParam(m, iter, &base::get<0>(*r)); |
| 613 } | 609 } |
| 614 static void Log(const param_type& p, std::string* l) { | 610 static void Log(const param_type& p, std::string* l) { |
| 615 LogParam(base::get<0>(p), l); | 611 LogParam(base::get<0>(p), l); |
| 616 } | 612 } |
| 617 }; | 613 }; |
| 618 | 614 |
| 619 template <class A, class B> | 615 template <class A, class B> |
| 620 struct ParamTraits<base::Tuple<A, B>> { | 616 struct ParamTraits<base::Tuple<A, B>> { |
| 621 typedef base::Tuple<A, B> param_type; | 617 typedef base::Tuple<A, B> param_type; |
| 622 static void Write(Message* m, const param_type& p) { | 618 static void Write(base::Pickle* m, const param_type& p) { |
| 623 WriteParam(m, base::get<0>(p)); | 619 WriteParam(m, base::get<0>(p)); |
| 624 WriteParam(m, base::get<1>(p)); | 620 WriteParam(m, base::get<1>(p)); |
| 625 } | 621 } |
| 626 static bool Read(const Message* m, | 622 static bool Read(const base::Pickle* m, |
| 627 base::PickleIterator* iter, | 623 base::PickleIterator* iter, |
| 628 param_type* r) { | 624 param_type* r) { |
| 629 return (ReadParam(m, iter, &base::get<0>(*r)) && | 625 return (ReadParam(m, iter, &base::get<0>(*r)) && |
| 630 ReadParam(m, iter, &base::get<1>(*r))); | 626 ReadParam(m, iter, &base::get<1>(*r))); |
| 631 } | 627 } |
| 632 static void Log(const param_type& p, std::string* l) { | 628 static void Log(const param_type& p, std::string* l) { |
| 633 LogParam(base::get<0>(p), l); | 629 LogParam(base::get<0>(p), l); |
| 634 l->append(", "); | 630 l->append(", "); |
| 635 LogParam(base::get<1>(p), l); | 631 LogParam(base::get<1>(p), l); |
| 636 } | 632 } |
| 637 }; | 633 }; |
| 638 | 634 |
| 639 template <class A, class B, class C> | 635 template <class A, class B, class C> |
| 640 struct ParamTraits<base::Tuple<A, B, C>> { | 636 struct ParamTraits<base::Tuple<A, B, C>> { |
| 641 typedef base::Tuple<A, B, C> param_type; | 637 typedef base::Tuple<A, B, C> param_type; |
| 642 static void Write(Message* m, const param_type& p) { | 638 static void Write(base::Pickle* m, const param_type& p) { |
| 643 WriteParam(m, base::get<0>(p)); | 639 WriteParam(m, base::get<0>(p)); |
| 644 WriteParam(m, base::get<1>(p)); | 640 WriteParam(m, base::get<1>(p)); |
| 645 WriteParam(m, base::get<2>(p)); | 641 WriteParam(m, base::get<2>(p)); |
| 646 } | 642 } |
| 647 static bool Read(const Message* m, | 643 static bool Read(const base::Pickle* m, |
| 648 base::PickleIterator* iter, | 644 base::PickleIterator* iter, |
| 649 param_type* r) { | 645 param_type* r) { |
| 650 return (ReadParam(m, iter, &base::get<0>(*r)) && | 646 return (ReadParam(m, iter, &base::get<0>(*r)) && |
| 651 ReadParam(m, iter, &base::get<1>(*r)) && | 647 ReadParam(m, iter, &base::get<1>(*r)) && |
| 652 ReadParam(m, iter, &base::get<2>(*r))); | 648 ReadParam(m, iter, &base::get<2>(*r))); |
| 653 } | 649 } |
| 654 static void Log(const param_type& p, std::string* l) { | 650 static void Log(const param_type& p, std::string* l) { |
| 655 LogParam(base::get<0>(p), l); | 651 LogParam(base::get<0>(p), l); |
| 656 l->append(", "); | 652 l->append(", "); |
| 657 LogParam(base::get<1>(p), l); | 653 LogParam(base::get<1>(p), l); |
| 658 l->append(", "); | 654 l->append(", "); |
| 659 LogParam(base::get<2>(p), l); | 655 LogParam(base::get<2>(p), l); |
| 660 } | 656 } |
| 661 }; | 657 }; |
| 662 | 658 |
| 663 template <class A, class B, class C, class D> | 659 template <class A, class B, class C, class D> |
| 664 struct ParamTraits<base::Tuple<A, B, C, D>> { | 660 struct ParamTraits<base::Tuple<A, B, C, D>> { |
| 665 typedef base::Tuple<A, B, C, D> param_type; | 661 typedef base::Tuple<A, B, C, D> param_type; |
| 666 static void Write(Message* m, const param_type& p) { | 662 static void Write(base::Pickle* m, const param_type& p) { |
| 667 WriteParam(m, base::get<0>(p)); | 663 WriteParam(m, base::get<0>(p)); |
| 668 WriteParam(m, base::get<1>(p)); | 664 WriteParam(m, base::get<1>(p)); |
| 669 WriteParam(m, base::get<2>(p)); | 665 WriteParam(m, base::get<2>(p)); |
| 670 WriteParam(m, base::get<3>(p)); | 666 WriteParam(m, base::get<3>(p)); |
| 671 } | 667 } |
| 672 static bool Read(const Message* m, | 668 static bool Read(const base::Pickle* m, |
| 673 base::PickleIterator* iter, | 669 base::PickleIterator* iter, |
| 674 param_type* r) { | 670 param_type* r) { |
| 675 return (ReadParam(m, iter, &base::get<0>(*r)) && | 671 return (ReadParam(m, iter, &base::get<0>(*r)) && |
| 676 ReadParam(m, iter, &base::get<1>(*r)) && | 672 ReadParam(m, iter, &base::get<1>(*r)) && |
| 677 ReadParam(m, iter, &base::get<2>(*r)) && | 673 ReadParam(m, iter, &base::get<2>(*r)) && |
| 678 ReadParam(m, iter, &base::get<3>(*r))); | 674 ReadParam(m, iter, &base::get<3>(*r))); |
| 679 } | 675 } |
| 680 static void Log(const param_type& p, std::string* l) { | 676 static void Log(const param_type& p, std::string* l) { |
| 681 LogParam(base::get<0>(p), l); | 677 LogParam(base::get<0>(p), l); |
| 682 l->append(", "); | 678 l->append(", "); |
| 683 LogParam(base::get<1>(p), l); | 679 LogParam(base::get<1>(p), l); |
| 684 l->append(", "); | 680 l->append(", "); |
| 685 LogParam(base::get<2>(p), l); | 681 LogParam(base::get<2>(p), l); |
| 686 l->append(", "); | 682 l->append(", "); |
| 687 LogParam(base::get<3>(p), l); | 683 LogParam(base::get<3>(p), l); |
| 688 } | 684 } |
| 689 }; | 685 }; |
| 690 | 686 |
| 691 template <class A, class B, class C, class D, class E> | 687 template <class A, class B, class C, class D, class E> |
| 692 struct ParamTraits<base::Tuple<A, B, C, D, E>> { | 688 struct ParamTraits<base::Tuple<A, B, C, D, E>> { |
| 693 typedef base::Tuple<A, B, C, D, E> param_type; | 689 typedef base::Tuple<A, B, C, D, E> param_type; |
| 694 static void Write(Message* m, const param_type& p) { | 690 static void Write(base::Pickle* m, const param_type& p) { |
| 695 WriteParam(m, base::get<0>(p)); | 691 WriteParam(m, base::get<0>(p)); |
| 696 WriteParam(m, base::get<1>(p)); | 692 WriteParam(m, base::get<1>(p)); |
| 697 WriteParam(m, base::get<2>(p)); | 693 WriteParam(m, base::get<2>(p)); |
| 698 WriteParam(m, base::get<3>(p)); | 694 WriteParam(m, base::get<3>(p)); |
| 699 WriteParam(m, base::get<4>(p)); | 695 WriteParam(m, base::get<4>(p)); |
| 700 } | 696 } |
| 701 static bool Read(const Message* m, | 697 static bool Read(const base::Pickle* m, |
| 702 base::PickleIterator* iter, | 698 base::PickleIterator* iter, |
| 703 param_type* r) { | 699 param_type* r) { |
| 704 return (ReadParam(m, iter, &base::get<0>(*r)) && | 700 return (ReadParam(m, iter, &base::get<0>(*r)) && |
| 705 ReadParam(m, iter, &base::get<1>(*r)) && | 701 ReadParam(m, iter, &base::get<1>(*r)) && |
| 706 ReadParam(m, iter, &base::get<2>(*r)) && | 702 ReadParam(m, iter, &base::get<2>(*r)) && |
| 707 ReadParam(m, iter, &base::get<3>(*r)) && | 703 ReadParam(m, iter, &base::get<3>(*r)) && |
| 708 ReadParam(m, iter, &base::get<4>(*r))); | 704 ReadParam(m, iter, &base::get<4>(*r))); |
| 709 } | 705 } |
| 710 static void Log(const param_type& p, std::string* l) { | 706 static void Log(const param_type& p, std::string* l) { |
| 711 LogParam(base::get<0>(p), l); | 707 LogParam(base::get<0>(p), l); |
| 712 l->append(", "); | 708 l->append(", "); |
| 713 LogParam(base::get<1>(p), l); | 709 LogParam(base::get<1>(p), l); |
| 714 l->append(", "); | 710 l->append(", "); |
| 715 LogParam(base::get<2>(p), l); | 711 LogParam(base::get<2>(p), l); |
| 716 l->append(", "); | 712 l->append(", "); |
| 717 LogParam(base::get<3>(p), l); | 713 LogParam(base::get<3>(p), l); |
| 718 l->append(", "); | 714 l->append(", "); |
| 719 LogParam(base::get<4>(p), l); | 715 LogParam(base::get<4>(p), l); |
| 720 } | 716 } |
| 721 }; | 717 }; |
| 722 | 718 |
| 723 template<class P> | 719 template<class P> |
| 724 struct ParamTraits<ScopedVector<P> > { | 720 struct ParamTraits<ScopedVector<P> > { |
| 725 typedef ScopedVector<P> param_type; | 721 typedef ScopedVector<P> param_type; |
| 726 static void Write(Message* m, const param_type& p) { | 722 static void Write(base::Pickle* m, const param_type& p) { |
| 727 WriteParam(m, static_cast<int>(p.size())); | 723 WriteParam(m, static_cast<int>(p.size())); |
| 728 for (size_t i = 0; i < p.size(); i++) | 724 for (size_t i = 0; i < p.size(); i++) |
| 729 WriteParam(m, *p[i]); | 725 WriteParam(m, *p[i]); |
| 730 } | 726 } |
| 731 static bool Read(const Message* m, | 727 static bool Read(const base::Pickle* m, |
| 732 base::PickleIterator* iter, | 728 base::PickleIterator* iter, |
| 733 param_type* r) { | 729 param_type* r) { |
| 734 int size = 0; | 730 int size = 0; |
| 735 if (!iter->ReadLength(&size)) | 731 if (!iter->ReadLength(&size)) |
| 736 return false; | 732 return false; |
| 737 if (INT_MAX/sizeof(P) <= static_cast<size_t>(size)) | 733 if (INT_MAX/sizeof(P) <= static_cast<size_t>(size)) |
| 738 return false; | 734 return false; |
| 739 r->resize(size); | 735 r->resize(size); |
| 740 for (int i = 0; i < size; i++) { | 736 for (int i = 0; i < size; i++) { |
| 741 (*r)[i] = new P(); | 737 (*r)[i] = new P(); |
| 742 if (!ReadParam(m, iter, (*r)[i])) | 738 if (!ReadParam(m, iter, (*r)[i])) |
| 743 return false; | 739 return false; |
| 744 } | 740 } |
| 745 return true; | 741 return true; |
| 746 } | 742 } |
| 747 static void Log(const param_type& p, std::string* l) { | 743 static void Log(const param_type& p, std::string* l) { |
| 748 for (size_t i = 0; i < p.size(); ++i) { | 744 for (size_t i = 0; i < p.size(); ++i) { |
| 749 if (i != 0) | 745 if (i != 0) |
| 750 l->append(" "); | 746 l->append(" "); |
| 751 LogParam(*p[i], l); | 747 LogParam(*p[i], l); |
| 752 } | 748 } |
| 753 } | 749 } |
| 754 }; | 750 }; |
| 755 | 751 |
| 756 template <class P, size_t stack_capacity> | 752 template <class P, size_t stack_capacity> |
| 757 struct ParamTraits<base::StackVector<P, stack_capacity> > { | 753 struct ParamTraits<base::StackVector<P, stack_capacity> > { |
| 758 typedef base::StackVector<P, stack_capacity> param_type; | 754 typedef base::StackVector<P, stack_capacity> param_type; |
| 759 static void Write(Message* m, const param_type& p) { | 755 static void Write(base::Pickle* m, const param_type& p) { |
| 760 WriteParam(m, static_cast<int>(p->size())); | 756 WriteParam(m, static_cast<int>(p->size())); |
| 761 for (size_t i = 0; i < p->size(); i++) | 757 for (size_t i = 0; i < p->size(); i++) |
| 762 WriteParam(m, p[i]); | 758 WriteParam(m, p[i]); |
| 763 } | 759 } |
| 764 static bool Read(const Message* m, | 760 static bool Read(const base::Pickle* m, |
| 765 base::PickleIterator* iter, | 761 base::PickleIterator* iter, |
| 766 param_type* r) { | 762 param_type* r) { |
| 767 int size; | 763 int size; |
| 768 // ReadLength() checks for < 0 itself. | 764 // ReadLength() checks for < 0 itself. |
| 769 if (!iter->ReadLength(&size)) | 765 if (!iter->ReadLength(&size)) |
| 770 return false; | 766 return false; |
| 771 // Sanity check for the vector size. | 767 // Sanity check for the vector size. |
| 772 if (INT_MAX / sizeof(P) <= static_cast<size_t>(size)) | 768 if (INT_MAX / sizeof(P) <= static_cast<size_t>(size)) |
| 773 return false; | 769 return false; |
| 774 P value; | 770 P value; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 789 }; | 785 }; |
| 790 | 786 |
| 791 template <typename NormalMap, | 787 template <typename NormalMap, |
| 792 int kArraySize, | 788 int kArraySize, |
| 793 typename EqualKey, | 789 typename EqualKey, |
| 794 typename MapInit> | 790 typename MapInit> |
| 795 struct ParamTraits<base::SmallMap<NormalMap, kArraySize, EqualKey, MapInit> > { | 791 struct ParamTraits<base::SmallMap<NormalMap, kArraySize, EqualKey, MapInit> > { |
| 796 typedef base::SmallMap<NormalMap, kArraySize, EqualKey, MapInit> param_type; | 792 typedef base::SmallMap<NormalMap, kArraySize, EqualKey, MapInit> param_type; |
| 797 typedef typename param_type::key_type K; | 793 typedef typename param_type::key_type K; |
| 798 typedef typename param_type::data_type V; | 794 typedef typename param_type::data_type V; |
| 799 static void Write(Message* m, const param_type& p) { | 795 static void Write(base::Pickle* m, const param_type& p) { |
| 800 WriteParam(m, static_cast<int>(p.size())); | 796 WriteParam(m, static_cast<int>(p.size())); |
| 801 typename param_type::const_iterator iter; | 797 typename param_type::const_iterator iter; |
| 802 for (iter = p.begin(); iter != p.end(); ++iter) { | 798 for (iter = p.begin(); iter != p.end(); ++iter) { |
| 803 WriteParam(m, iter->first); | 799 WriteParam(m, iter->first); |
| 804 WriteParam(m, iter->second); | 800 WriteParam(m, iter->second); |
| 805 } | 801 } |
| 806 } | 802 } |
| 807 static bool Read(const Message* m, | 803 static bool Read(const base::Pickle* m, |
| 808 base::PickleIterator* iter, | 804 base::PickleIterator* iter, |
| 809 param_type* r) { | 805 param_type* r) { |
| 810 int size; | 806 int size; |
| 811 if (!iter->ReadLength(&size)) | 807 if (!iter->ReadLength(&size)) |
| 812 return false; | 808 return false; |
| 813 for (int i = 0; i < size; ++i) { | 809 for (int i = 0; i < size; ++i) { |
| 814 K key; | 810 K key; |
| 815 if (!ReadParam(m, iter, &key)) | 811 if (!ReadParam(m, iter, &key)) |
| 816 return false; | 812 return false; |
| 817 V& value = (*r)[key]; | 813 V& value = (*r)[key]; |
| 818 if (!ReadParam(m, iter, &value)) | 814 if (!ReadParam(m, iter, &value)) |
| 819 return false; | 815 return false; |
| 820 } | 816 } |
| 821 return true; | 817 return true; |
| 822 } | 818 } |
| 823 static void Log(const param_type& p, std::string* l) { | 819 static void Log(const param_type& p, std::string* l) { |
| 824 l->append("<base::SmallMap>"); | 820 l->append("<base::SmallMap>"); |
| 825 } | 821 } |
| 826 }; | 822 }; |
| 827 | 823 |
| 828 template <class P> | 824 template <class P> |
| 829 struct ParamTraits<scoped_ptr<P> > { | 825 struct ParamTraits<scoped_ptr<P> > { |
| 830 typedef scoped_ptr<P> param_type; | 826 typedef scoped_ptr<P> param_type; |
| 831 static void Write(Message* m, const param_type& p) { | 827 static void Write(base::Pickle* m, const param_type& p) { |
| 832 bool valid = !!p; | 828 bool valid = !!p; |
| 833 WriteParam(m, valid); | 829 WriteParam(m, valid); |
| 834 if (valid) | 830 if (valid) |
| 835 WriteParam(m, *p); | 831 WriteParam(m, *p); |
| 836 } | 832 } |
| 837 static bool Read(const Message* m, | 833 static bool Read(const base::Pickle* m, |
| 838 base::PickleIterator* iter, | 834 base::PickleIterator* iter, |
| 839 param_type* r) { | 835 param_type* r) { |
| 840 bool valid = false; | 836 bool valid = false; |
| 841 if (!ReadParam(m, iter, &valid)) | 837 if (!ReadParam(m, iter, &valid)) |
| 842 return false; | 838 return false; |
| 843 | 839 |
| 844 if (!valid) { | 840 if (!valid) { |
| 845 r->reset(); | 841 r->reset(); |
| 846 return true; | 842 return true; |
| 847 } | 843 } |
| (...skipping 14 matching lines...) Expand all Loading... |
| 862 }; | 858 }; |
| 863 | 859 |
| 864 // IPC types ParamTraits ------------------------------------------------------- | 860 // IPC types ParamTraits ------------------------------------------------------- |
| 865 | 861 |
| 866 // A ChannelHandle is basically a platform-inspecific wrapper around the | 862 // A ChannelHandle is basically a platform-inspecific wrapper around the |
| 867 // fact that IPC endpoints are handled specially on POSIX. See above comments | 863 // fact that IPC endpoints are handled specially on POSIX. See above comments |
| 868 // on FileDescriptor for more background. | 864 // on FileDescriptor for more background. |
| 869 template<> | 865 template<> |
| 870 struct IPC_EXPORT ParamTraits<IPC::ChannelHandle> { | 866 struct IPC_EXPORT ParamTraits<IPC::ChannelHandle> { |
| 871 typedef ChannelHandle param_type; | 867 typedef ChannelHandle param_type; |
| 872 static void Write(Message* m, const param_type& p); | 868 static void Write(base::Pickle* m, const param_type& p); |
| 873 static bool Read(const Message* m, | 869 static bool Read(const base::Pickle* m, |
| 874 base::PickleIterator* iter, | 870 base::PickleIterator* iter, |
| 875 param_type* r); | 871 param_type* r); |
| 876 static void Log(const param_type& p, std::string* l); | 872 static void Log(const param_type& p, std::string* l); |
| 877 }; | 873 }; |
| 878 | 874 |
| 879 template <> | 875 template <> |
| 880 struct IPC_EXPORT ParamTraits<LogData> { | 876 struct IPC_EXPORT ParamTraits<LogData> { |
| 881 typedef LogData param_type; | 877 typedef LogData param_type; |
| 882 static void Write(Message* m, const param_type& p); | 878 static void Write(base::Pickle* m, const param_type& p); |
| 883 static bool Read(const Message* m, | 879 static bool Read(const base::Pickle* m, |
| 884 base::PickleIterator* iter, | 880 base::PickleIterator* iter, |
| 885 param_type* r); | 881 param_type* r); |
| 886 static void Log(const param_type& p, std::string* l); | 882 static void Log(const param_type& p, std::string* l); |
| 887 }; | 883 }; |
| 888 | 884 |
| 889 template <> | 885 template <> |
| 890 struct IPC_EXPORT ParamTraits<Message> { | 886 struct IPC_EXPORT ParamTraits<Message> { |
| 891 static void Write(Message* m, const Message& p); | 887 static void Write(base::Pickle* m, const Message& p); |
| 892 static bool Read(const Message* m, | 888 static bool Read(const base::Pickle* m, |
| 893 base::PickleIterator* iter, | 889 base::PickleIterator* iter, |
| 894 Message* r); | 890 Message* r); |
| 895 static void Log(const Message& p, std::string* l); | 891 static void Log(const Message& p, std::string* l); |
| 896 }; | 892 }; |
| 897 | 893 |
| 898 // Windows ParamTraits --------------------------------------------------------- | 894 // Windows ParamTraits --------------------------------------------------------- |
| 899 | 895 |
| 900 #if defined(OS_WIN) | 896 #if defined(OS_WIN) |
| 901 template <> | 897 template <> |
| 902 struct IPC_EXPORT ParamTraits<HANDLE> { | 898 struct IPC_EXPORT ParamTraits<HANDLE> { |
| 903 typedef HANDLE param_type; | 899 typedef HANDLE param_type; |
| 904 static void Write(Message* m, const param_type& p); | 900 static void Write(base::Pickle* m, const param_type& p); |
| 905 static bool Read(const Message* m, | 901 static bool Read(const base::Pickle* m, |
| 906 base::PickleIterator* iter, | 902 base::PickleIterator* iter, |
| 907 param_type* r); | 903 param_type* r); |
| 908 static void Log(const param_type& p, std::string* l); | 904 static void Log(const param_type& p, std::string* l); |
| 909 }; | 905 }; |
| 910 | 906 |
| 911 template <> | 907 template <> |
| 912 struct IPC_EXPORT ParamTraits<LOGFONT> { | 908 struct IPC_EXPORT ParamTraits<LOGFONT> { |
| 913 typedef LOGFONT param_type; | 909 typedef LOGFONT param_type; |
| 914 static void Write(Message* m, const param_type& p); | 910 static void Write(base::Pickle* m, const param_type& p); |
| 915 static bool Read(const Message* m, | 911 static bool Read(const base::Pickle* m, |
| 916 base::PickleIterator* iter, | 912 base::PickleIterator* iter, |
| 917 param_type* r); | 913 param_type* r); |
| 918 static void Log(const param_type& p, std::string* l); | 914 static void Log(const param_type& p, std::string* l); |
| 919 }; | 915 }; |
| 920 | 916 |
| 921 template <> | 917 template <> |
| 922 struct IPC_EXPORT ParamTraits<MSG> { | 918 struct IPC_EXPORT ParamTraits<MSG> { |
| 923 typedef MSG param_type; | 919 typedef MSG param_type; |
| 924 static void Write(Message* m, const param_type& p); | 920 static void Write(base::Pickle* m, const param_type& p); |
| 925 static bool Read(const Message* m, | 921 static bool Read(const base::Pickle* m, |
| 926 base::PickleIterator* iter, | 922 base::PickleIterator* iter, |
| 927 param_type* r); | 923 param_type* r); |
| 928 static void Log(const param_type& p, std::string* l); | 924 static void Log(const param_type& p, std::string* l); |
| 929 }; | 925 }; |
| 930 #endif // defined(OS_WIN) | 926 #endif // defined(OS_WIN) |
| 931 | 927 |
| 932 //----------------------------------------------------------------------------- | 928 //----------------------------------------------------------------------------- |
| 933 // Generic message subclasses | 929 // Generic message subclasses |
| 934 | 930 |
| 935 // Used for asynchronous messages. | 931 // Used for asynchronous messages. |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1058 template <typename... Ts> | 1054 template <typename... Ts> |
| 1059 static void WriteReplyParams(Message* reply, Ts... args) { | 1055 static void WriteReplyParams(Message* reply, Ts... args) { |
| 1060 ReplyParam p(args...); | 1056 ReplyParam p(args...); |
| 1061 WriteParam(reply, p); | 1057 WriteParam(reply, p); |
| 1062 } | 1058 } |
| 1063 }; | 1059 }; |
| 1064 | 1060 |
| 1065 } // namespace IPC | 1061 } // namespace IPC |
| 1066 | 1062 |
| 1067 #endif // IPC_IPC_MESSAGE_UTILS_H_ | 1063 #endif // IPC_IPC_MESSAGE_UTILS_H_ |
| OLD | NEW |