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