| 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 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <map> | 10 #include <map> |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 117 | 117 |
| 118 namespace IPC { | 118 namespace IPC { |
| 119 | 119 |
| 120 struct ChannelHandle; | 120 struct ChannelHandle; |
| 121 | 121 |
| 122 //----------------------------------------------------------------------------- | 122 //----------------------------------------------------------------------------- |
| 123 // An iterator class for reading the fields contained within a Message. | 123 // An iterator class for reading the fields contained within a Message. |
| 124 | 124 |
| 125 class MessageIterator { | 125 class MessageIterator { |
| 126 public: | 126 public: |
| 127 explicit MessageIterator(const Message& m) : msg_(m), iter_(NULL) { | 127 explicit MessageIterator(const Message& m) : iter_(m) { |
| 128 } | 128 } |
| 129 int NextInt() const { | 129 int NextInt() const { |
| 130 int val = -1; | 130 int val = -1; |
| 131 if (!msg_.ReadInt(&iter_, &val)) | 131 if (!iter_.ReadInt(&val)) |
| 132 NOTREACHED(); | 132 NOTREACHED(); |
| 133 return val; | 133 return val; |
| 134 } | 134 } |
| 135 const std::string NextString() const { | 135 const std::string NextString() const { |
| 136 std::string val; | 136 std::string val; |
| 137 if (!msg_.ReadString(&iter_, &val)) | 137 if (!iter_.ReadString(&val)) |
| 138 NOTREACHED(); | 138 NOTREACHED(); |
| 139 return val; | 139 return val; |
| 140 } | 140 } |
| 141 const std::wstring NextWString() const { | 141 const std::wstring NextWString() const { |
| 142 std::wstring val; | 142 std::wstring val; |
| 143 if (!msg_.ReadWString(&iter_, &val)) | 143 if (!iter_.ReadWString(&val)) |
| 144 NOTREACHED(); | 144 NOTREACHED(); |
| 145 return val; | 145 return val; |
| 146 } | 146 } |
| 147 void NextData(const char** data, int* length) const { | 147 void NextData(const char** data, int* length) const { |
| 148 if (!msg_.ReadData(&iter_, data, length)) { | 148 if (!iter_.ReadData(data, length)) { |
| 149 NOTREACHED(); | 149 NOTREACHED(); |
| 150 } | 150 } |
| 151 } | 151 } |
| 152 private: | 152 private: |
| 153 const Message& msg_; | 153 mutable PickleIterator iter_; |
| 154 mutable void* iter_; | |
| 155 }; | 154 }; |
| 156 | 155 |
| 157 //----------------------------------------------------------------------------- | 156 //----------------------------------------------------------------------------- |
| 158 // A dummy struct to place first just to allow leading commas for all | 157 // A dummy struct to place first just to allow leading commas for all |
| 159 // members in the macro-generated constructor initializer lists. | 158 // members in the macro-generated constructor initializer lists. |
| 160 struct NoParams { | 159 struct NoParams { |
| 161 }; | 160 }; |
| 162 | 161 |
| 163 //----------------------------------------------------------------------------- | 162 //----------------------------------------------------------------------------- |
| 164 // ParamTraits specializations, etc. | 163 // ParamTraits specializations, etc. |
| 165 | 164 |
| 166 template <class P> | 165 template <class P> |
| 167 static inline void WriteParam(Message* m, const P& p) { | 166 static inline void WriteParam(Message* m, const P& p) { |
| 168 typedef typename SimilarTypeTraits<P>::Type Type; | 167 typedef typename SimilarTypeTraits<P>::Type Type; |
| 169 ParamTraits<Type>::Write(m, static_cast<const Type& >(p)); | 168 ParamTraits<Type>::Write(m, static_cast<const Type& >(p)); |
| 170 } | 169 } |
| 171 | 170 |
| 172 template <class P> | 171 template <class P> |
| 173 static inline bool WARN_UNUSED_RESULT ReadParam(const Message* m, void** iter, | 172 static inline bool WARN_UNUSED_RESULT ReadParam(const Message* m, |
| 173 PickleIterator* iter, |
| 174 P* p) { | 174 P* p) { |
| 175 typedef typename SimilarTypeTraits<P>::Type Type; | 175 typedef typename SimilarTypeTraits<P>::Type Type; |
| 176 return ParamTraits<Type>::Read(m, iter, reinterpret_cast<Type* >(p)); | 176 return ParamTraits<Type>::Read(m, iter, reinterpret_cast<Type* >(p)); |
| 177 } | 177 } |
| 178 | 178 |
| 179 template <class P> | 179 template <class P> |
| 180 static inline void LogParam(const P& p, std::string* l) { | 180 static inline void LogParam(const P& p, std::string* l) { |
| 181 typedef typename SimilarTypeTraits<P>::Type Type; | 181 typedef typename SimilarTypeTraits<P>::Type Type; |
| 182 ParamTraits<Type>::Log(static_cast<const Type& >(p), l); | 182 ParamTraits<Type>::Log(static_cast<const Type& >(p), l); |
| 183 } | 183 } |
| 184 | 184 |
| 185 template <> | 185 template <> |
| 186 struct ParamTraits<bool> { | 186 struct ParamTraits<bool> { |
| 187 typedef bool param_type; | 187 typedef bool param_type; |
| 188 static void Write(Message* m, const param_type& p) { | 188 static void Write(Message* m, const param_type& p) { |
| 189 m->WriteBool(p); | 189 m->WriteBool(p); |
| 190 } | 190 } |
| 191 static bool Read(const Message* m, void** iter, param_type* r) { | 191 static bool Read(const Message* m, PickleIterator* iter, |
| 192 param_type* r) { |
| 192 return m->ReadBool(iter, r); | 193 return m->ReadBool(iter, r); |
| 193 } | 194 } |
| 194 static void Log(const param_type& p, std::string* l) { | 195 static void Log(const param_type& p, std::string* l) { |
| 195 l->append(p ? "true" : "false"); | 196 l->append(p ? "true" : "false"); |
| 196 } | 197 } |
| 197 }; | 198 }; |
| 198 | 199 |
| 199 template <> | 200 template <> |
| 200 struct ParamTraits<int> { | 201 struct ParamTraits<int> { |
| 201 typedef int param_type; | 202 typedef int param_type; |
| 202 static void Write(Message* m, const param_type& p) { | 203 static void Write(Message* m, const param_type& p) { |
| 203 m->WriteInt(p); | 204 m->WriteInt(p); |
| 204 } | 205 } |
| 205 static bool Read(const Message* m, void** iter, param_type* r) { | 206 static bool Read(const Message* m, PickleIterator* iter, |
| 207 param_type* r) { |
| 206 return m->ReadInt(iter, r); | 208 return m->ReadInt(iter, r); |
| 207 } | 209 } |
| 208 IPC_EXPORT static void Log(const param_type& p, std::string* l); | 210 IPC_EXPORT static void Log(const param_type& p, std::string* l); |
| 209 }; | 211 }; |
| 210 | 212 |
| 211 template <> | 213 template <> |
| 212 struct ParamTraits<unsigned int> { | 214 struct ParamTraits<unsigned int> { |
| 213 typedef unsigned int param_type; | 215 typedef unsigned int param_type; |
| 214 static void Write(Message* m, const param_type& p) { | 216 static void Write(Message* m, const param_type& p) { |
| 215 m->WriteInt(p); | 217 m->WriteInt(p); |
| 216 } | 218 } |
| 217 static bool Read(const Message* m, void** iter, param_type* r) { | 219 static bool Read(const Message* m, PickleIterator* iter, |
| 220 param_type* r) { |
| 218 return m->ReadInt(iter, reinterpret_cast<int*>(r)); | 221 return m->ReadInt(iter, reinterpret_cast<int*>(r)); |
| 219 } | 222 } |
| 220 IPC_EXPORT static void Log(const param_type& p, std::string* l); | 223 IPC_EXPORT static void Log(const param_type& p, std::string* l); |
| 221 }; | 224 }; |
| 222 | 225 |
| 223 template <> | 226 template <> |
| 224 struct ParamTraits<long> { | 227 struct ParamTraits<long> { |
| 225 typedef long param_type; | 228 typedef long param_type; |
| 226 static void Write(Message* m, const param_type& p) { | 229 static void Write(Message* m, const param_type& p) { |
| 227 m->WriteLong(p); | 230 m->WriteLong(p); |
| 228 } | 231 } |
| 229 static bool Read(const Message* m, void** iter, param_type* r) { | 232 static bool Read(const Message* m, PickleIterator* iter, |
| 233 param_type* r) { |
| 230 return m->ReadLong(iter, r); | 234 return m->ReadLong(iter, r); |
| 231 } | 235 } |
| 232 IPC_EXPORT static void Log(const param_type& p, std::string* l); | 236 IPC_EXPORT static void Log(const param_type& p, std::string* l); |
| 233 }; | 237 }; |
| 234 | 238 |
| 235 template <> | 239 template <> |
| 236 struct ParamTraits<unsigned long> { | 240 struct ParamTraits<unsigned long> { |
| 237 typedef unsigned long param_type; | 241 typedef unsigned long param_type; |
| 238 static void Write(Message* m, const param_type& p) { | 242 static void Write(Message* m, const param_type& p) { |
| 239 m->WriteLong(p); | 243 m->WriteLong(p); |
| 240 } | 244 } |
| 241 static bool Read(const Message* m, void** iter, param_type* r) { | 245 static bool Read(const Message* m, PickleIterator* iter, |
| 246 param_type* r) { |
| 242 return m->ReadLong(iter, reinterpret_cast<long*>(r)); | 247 return m->ReadLong(iter, reinterpret_cast<long*>(r)); |
| 243 } | 248 } |
| 244 IPC_EXPORT static void Log(const param_type& p, std::string* l); | 249 IPC_EXPORT static void Log(const param_type& p, std::string* l); |
| 245 }; | 250 }; |
| 246 | 251 |
| 247 template <> | 252 template <> |
| 248 struct ParamTraits<long long> { | 253 struct ParamTraits<long long> { |
| 249 typedef long long param_type; | 254 typedef long long param_type; |
| 250 static void Write(Message* m, const param_type& p) { | 255 static void Write(Message* m, const param_type& p) { |
| 251 m->WriteInt64(static_cast<int64>(p)); | 256 m->WriteInt64(static_cast<int64>(p)); |
| 252 } | 257 } |
| 253 static bool Read(const Message* m, void** iter, param_type* r) { | 258 static bool Read(const Message* m, PickleIterator* iter, |
| 259 param_type* r) { |
| 254 return m->ReadInt64(iter, reinterpret_cast<int64*>(r)); | 260 return m->ReadInt64(iter, reinterpret_cast<int64*>(r)); |
| 255 } | 261 } |
| 256 IPC_EXPORT static void Log(const param_type& p, std::string* l); | 262 IPC_EXPORT static void Log(const param_type& p, std::string* l); |
| 257 }; | 263 }; |
| 258 | 264 |
| 259 template <> | 265 template <> |
| 260 struct ParamTraits<unsigned long long> { | 266 struct ParamTraits<unsigned long long> { |
| 261 typedef unsigned long long param_type; | 267 typedef unsigned long long param_type; |
| 262 static void Write(Message* m, const param_type& p) { | 268 static void Write(Message* m, const param_type& p) { |
| 263 m->WriteInt64(p); | 269 m->WriteInt64(p); |
| 264 } | 270 } |
| 265 static bool Read(const Message* m, void** iter, param_type* r) { | 271 static bool Read(const Message* m, PickleIterator* iter, |
| 272 param_type* r) { |
| 266 return m->ReadInt64(iter, reinterpret_cast<int64*>(r)); | 273 return m->ReadInt64(iter, reinterpret_cast<int64*>(r)); |
| 267 } | 274 } |
| 268 IPC_EXPORT static void Log(const param_type& p, std::string* l); | 275 IPC_EXPORT static void Log(const param_type& p, std::string* l); |
| 269 }; | 276 }; |
| 270 | 277 |
| 271 template <> | 278 template <> |
| 272 struct IPC_EXPORT ParamTraits<unsigned short> { | 279 struct IPC_EXPORT ParamTraits<unsigned short> { |
| 273 typedef unsigned short param_type; | 280 typedef unsigned short param_type; |
| 274 static void Write(Message* m, const param_type& p); | 281 static void Write(Message* m, const param_type& p); |
| 275 static bool Read(const Message* m, void** iter, param_type* r); | 282 static bool Read(const Message* m, PickleIterator* iter, param_type* r); |
| 276 static void Log(const param_type& p, std::string* l); | 283 static void Log(const param_type& p, std::string* l); |
| 277 }; | 284 }; |
| 278 | 285 |
| 279 // Note that the IPC layer doesn't sanitize NaNs and +/- INF values. Clients | 286 // Note that the IPC layer doesn't sanitize NaNs and +/- INF values. Clients |
| 280 // should be sure to check the sanity of these values after receiving them over | 287 // should be sure to check the sanity of these values after receiving them over |
| 281 // IPC. | 288 // IPC. |
| 282 template <> | 289 template <> |
| 283 struct ParamTraits<float> { | 290 struct ParamTraits<float> { |
| 284 typedef float param_type; | 291 typedef float param_type; |
| 285 static void Write(Message* m, const param_type& p) { | 292 static void Write(Message* m, const param_type& p) { |
| 286 m->WriteData(reinterpret_cast<const char*>(&p), sizeof(param_type)); | 293 m->WriteData(reinterpret_cast<const char*>(&p), sizeof(param_type)); |
| 287 } | 294 } |
| 288 static bool Read(const Message* m, void** iter, param_type* r) { | 295 static bool Read(const Message* m, PickleIterator* iter, |
| 296 param_type* r) { |
| 289 const char *data; | 297 const char *data; |
| 290 int data_size; | 298 int data_size; |
| 291 if (!m->ReadData(iter, &data, &data_size) || | 299 if (!m->ReadData(iter, &data, &data_size) || |
| 292 data_size != sizeof(param_type)) { | 300 data_size != sizeof(param_type)) { |
| 293 NOTREACHED(); | 301 NOTREACHED(); |
| 294 return false; | 302 return false; |
| 295 } | 303 } |
| 296 memcpy(r, data, sizeof(param_type)); | 304 memcpy(r, data, sizeof(param_type)); |
| 297 return true; | 305 return true; |
| 298 } | 306 } |
| 299 static void Log(const param_type& p, std::string* l) { | 307 static void Log(const param_type& p, std::string* l) { |
| 300 l->append(StringPrintf("%e", p)); | 308 l->append(StringPrintf("%e", p)); |
| 301 } | 309 } |
| 302 }; | 310 }; |
| 303 | 311 |
| 304 template <> | 312 template <> |
| 305 struct ParamTraits<double> { | 313 struct ParamTraits<double> { |
| 306 typedef double param_type; | 314 typedef double param_type; |
| 307 static void Write(Message* m, const param_type& p) { | 315 static void Write(Message* m, const param_type& p) { |
| 308 m->WriteData(reinterpret_cast<const char*>(&p), sizeof(param_type)); | 316 m->WriteData(reinterpret_cast<const char*>(&p), sizeof(param_type)); |
| 309 } | 317 } |
| 310 static bool Read(const Message* m, void** iter, param_type* r) { | 318 static bool Read(const Message* m, PickleIterator* iter, |
| 319 param_type* r) { |
| 311 const char *data; | 320 const char *data; |
| 312 int data_size; | 321 int data_size; |
| 313 if (!m->ReadData(iter, &data, &data_size) || | 322 if (!m->ReadData(iter, &data, &data_size) || |
| 314 data_size != sizeof(param_type)) { | 323 data_size != sizeof(param_type)) { |
| 315 NOTREACHED(); | 324 NOTREACHED(); |
| 316 return false; | 325 return false; |
| 317 } | 326 } |
| 318 memcpy(r, data, sizeof(param_type)); | 327 memcpy(r, data, sizeof(param_type)); |
| 319 return true; | 328 return true; |
| 320 } | 329 } |
| 321 static void Log(const param_type& p, std::string* l) { | 330 static void Log(const param_type& p, std::string* l) { |
| 322 l->append(StringPrintf("%e", p)); | 331 l->append(StringPrintf("%e", p)); |
| 323 } | 332 } |
| 324 }; | 333 }; |
| 325 | 334 |
| 326 template <> | 335 template <> |
| 327 struct IPC_EXPORT ParamTraits<base::Time> { | 336 struct IPC_EXPORT ParamTraits<base::Time> { |
| 328 typedef base::Time param_type; | 337 typedef base::Time param_type; |
| 329 static void Write(Message* m, const param_type& p); | 338 static void Write(Message* m, const param_type& p); |
| 330 static bool Read(const Message* m, void** iter, param_type* r); | 339 static bool Read(const Message* m, PickleIterator* iter, param_type* r); |
| 331 static void Log(const param_type& p, std::string* l); | 340 static void Log(const param_type& p, std::string* l); |
| 332 }; | 341 }; |
| 333 | 342 |
| 334 template <> | 343 template <> |
| 335 struct IPC_EXPORT ParamTraits<base::TimeDelta> { | 344 struct IPC_EXPORT ParamTraits<base::TimeDelta> { |
| 336 typedef base::TimeDelta param_type; | 345 typedef base::TimeDelta param_type; |
| 337 static void Write(Message* m, const param_type& p); | 346 static void Write(Message* m, const param_type& p); |
| 338 static bool Read(const Message* m, void** iter, param_type* r); | 347 static bool Read(const Message* m, PickleIterator* iter, param_type* r); |
| 339 static void Log(const param_type& p, std::string* l); | 348 static void Log(const param_type& p, std::string* l); |
| 340 }; | 349 }; |
| 341 | 350 |
| 342 template <> | 351 template <> |
| 343 struct IPC_EXPORT ParamTraits<base::TimeTicks> { | 352 struct IPC_EXPORT ParamTraits<base::TimeTicks> { |
| 344 typedef base::TimeTicks param_type; | 353 typedef base::TimeTicks param_type; |
| 345 static void Write(Message* m, const param_type& p); | 354 static void Write(Message* m, const param_type& p); |
| 346 static bool Read(const Message* m, void** iter, param_type* r); | 355 static bool Read(const Message* m, PickleIterator* iter, param_type* r); |
| 347 static void Log(const param_type& p, std::string* l); | 356 static void Log(const param_type& p, std::string* l); |
| 348 }; | 357 }; |
| 349 | 358 |
| 350 #if defined(OS_WIN) | 359 #if defined(OS_WIN) |
| 351 template <> | 360 template <> |
| 352 struct ParamTraits<LOGFONT> { | 361 struct ParamTraits<LOGFONT> { |
| 353 typedef LOGFONT param_type; | 362 typedef LOGFONT param_type; |
| 354 static void Write(Message* m, const param_type& p) { | 363 static void Write(Message* m, const param_type& p) { |
| 355 m->WriteData(reinterpret_cast<const char*>(&p), sizeof(LOGFONT)); | 364 m->WriteData(reinterpret_cast<const char*>(&p), sizeof(LOGFONT)); |
| 356 } | 365 } |
| 357 static bool Read(const Message* m, void** iter, param_type* r) { | 366 static bool Read(const Message* m, PickleIterator* iter, |
| 367 param_type* r) { |
| 358 const char *data; | 368 const char *data; |
| 359 int data_size = 0; | 369 int data_size = 0; |
| 360 bool result = m->ReadData(iter, &data, &data_size); | 370 bool result = m->ReadData(iter, &data, &data_size); |
| 361 if (result && data_size == sizeof(LOGFONT)) { | 371 if (result && data_size == sizeof(LOGFONT)) { |
| 362 memcpy(r, data, sizeof(LOGFONT)); | 372 memcpy(r, data, sizeof(LOGFONT)); |
| 363 } else { | 373 } else { |
| 364 result = false; | 374 result = false; |
| 365 NOTREACHED(); | 375 NOTREACHED(); |
| 366 } | 376 } |
| 367 | 377 |
| 368 return result; | 378 return result; |
| 369 } | 379 } |
| 370 static void Log(const param_type& p, std::string* l) { | 380 static void Log(const param_type& p, std::string* l) { |
| 371 l->append(StringPrintf("<LOGFONT>")); | 381 l->append(StringPrintf("<LOGFONT>")); |
| 372 } | 382 } |
| 373 }; | 383 }; |
| 374 | 384 |
| 375 template <> | 385 template <> |
| 376 struct ParamTraits<MSG> { | 386 struct ParamTraits<MSG> { |
| 377 typedef MSG param_type; | 387 typedef MSG param_type; |
| 378 static void Write(Message* m, const param_type& p) { | 388 static void Write(Message* m, const param_type& p) { |
| 379 m->WriteData(reinterpret_cast<const char*>(&p), sizeof(MSG)); | 389 m->WriteData(reinterpret_cast<const char*>(&p), sizeof(MSG)); |
| 380 } | 390 } |
| 381 static bool Read(const Message* m, void** iter, param_type* r) { | 391 static bool Read(const Message* m, PickleIterator* iter, |
| 392 param_type* r) { |
| 382 const char *data; | 393 const char *data; |
| 383 int data_size = 0; | 394 int data_size = 0; |
| 384 bool result = m->ReadData(iter, &data, &data_size); | 395 bool result = m->ReadData(iter, &data, &data_size); |
| 385 if (result && data_size == sizeof(MSG)) { | 396 if (result && data_size == sizeof(MSG)) { |
| 386 memcpy(r, data, sizeof(MSG)); | 397 memcpy(r, data, sizeof(MSG)); |
| 387 } else { | 398 } else { |
| 388 result = false; | 399 result = false; |
| 389 NOTREACHED(); | 400 NOTREACHED(); |
| 390 } | 401 } |
| 391 | 402 |
| 392 return result; | 403 return result; |
| 393 } | 404 } |
| 394 static void Log(const param_type& p, std::string* l) { | 405 static void Log(const param_type& p, std::string* l) { |
| 395 l->append("<MSG>"); | 406 l->append("<MSG>"); |
| 396 } | 407 } |
| 397 }; | 408 }; |
| 398 #endif // defined(OS_WIN) | 409 #endif // defined(OS_WIN) |
| 399 | 410 |
| 400 template <> | 411 template <> |
| 401 struct IPC_EXPORT ParamTraits<base::DictionaryValue> { | 412 struct IPC_EXPORT ParamTraits<base::DictionaryValue> { |
| 402 typedef base::DictionaryValue param_type; | 413 typedef base::DictionaryValue param_type; |
| 403 static void Write(Message* m, const param_type& p); | 414 static void Write(Message* m, const param_type& p); |
| 404 static bool Read(const Message* m, void** iter, param_type* r); | 415 static bool Read(const Message* m, PickleIterator* iter, param_type* r); |
| 405 static void Log(const param_type& p, std::string* l); | 416 static void Log(const param_type& p, std::string* l); |
| 406 }; | 417 }; |
| 407 | 418 |
| 408 template <> | 419 template <> |
| 409 struct IPC_EXPORT ParamTraits<base::ListValue> { | 420 struct IPC_EXPORT ParamTraits<base::ListValue> { |
| 410 typedef base::ListValue param_type; | 421 typedef base::ListValue param_type; |
| 411 static void Write(Message* m, const param_type& p); | 422 static void Write(Message* m, const param_type& p); |
| 412 static bool Read(const Message* m, void** iter, param_type* r); | 423 static bool Read(const Message* m, PickleIterator* iter, param_type* r); |
| 413 static void Log(const param_type& p, std::string* l); | 424 static void Log(const param_type& p, std::string* l); |
| 414 }; | 425 }; |
| 415 | 426 |
| 416 template <> | 427 template <> |
| 417 struct ParamTraits<std::string> { | 428 struct ParamTraits<std::string> { |
| 418 typedef std::string param_type; | 429 typedef std::string param_type; |
| 419 static void Write(Message* m, const param_type& p) { | 430 static void Write(Message* m, const param_type& p) { |
| 420 m->WriteString(p); | 431 m->WriteString(p); |
| 421 } | 432 } |
| 422 static bool Read(const Message* m, void** iter, param_type* r) { | 433 static bool Read(const Message* m, PickleIterator* iter, |
| 434 param_type* r) { |
| 423 return m->ReadString(iter, r); | 435 return m->ReadString(iter, r); |
| 424 } | 436 } |
| 425 static void Log(const param_type& p, std::string* l) { | 437 static void Log(const param_type& p, std::string* l) { |
| 426 l->append(p); | 438 l->append(p); |
| 427 } | 439 } |
| 428 }; | 440 }; |
| 429 | 441 |
| 430 template<typename CharType> | 442 template<typename CharType> |
| 431 static void LogBytes(const std::vector<CharType>& data, std::string* out) { | 443 static void LogBytes(const std::vector<CharType>& data, std::string* out) { |
| 432 #if defined(OS_WIN) | 444 #if defined(OS_WIN) |
| (...skipping 21 matching lines...) Expand all Loading... |
| 454 struct ParamTraits<std::vector<unsigned char> > { | 466 struct ParamTraits<std::vector<unsigned char> > { |
| 455 typedef std::vector<unsigned char> param_type; | 467 typedef std::vector<unsigned char> param_type; |
| 456 static void Write(Message* m, const param_type& p) { | 468 static void Write(Message* m, const param_type& p) { |
| 457 if (p.empty()) { | 469 if (p.empty()) { |
| 458 m->WriteData(NULL, 0); | 470 m->WriteData(NULL, 0); |
| 459 } else { | 471 } else { |
| 460 m->WriteData(reinterpret_cast<const char*>(&p.front()), | 472 m->WriteData(reinterpret_cast<const char*>(&p.front()), |
| 461 static_cast<int>(p.size())); | 473 static_cast<int>(p.size())); |
| 462 } | 474 } |
| 463 } | 475 } |
| 464 static bool Read(const Message* m, void** iter, param_type* r) { | 476 static bool Read(const Message* m, PickleIterator* iter, |
| 477 param_type* r) { |
| 465 const char *data; | 478 const char *data; |
| 466 int data_size = 0; | 479 int data_size = 0; |
| 467 if (!m->ReadData(iter, &data, &data_size) || data_size < 0) | 480 if (!m->ReadData(iter, &data, &data_size) || data_size < 0) |
| 468 return false; | 481 return false; |
| 469 r->resize(data_size); | 482 r->resize(data_size); |
| 470 if (data_size) | 483 if (data_size) |
| 471 memcpy(&r->front(), data, data_size); | 484 memcpy(&r->front(), data, data_size); |
| 472 return true; | 485 return true; |
| 473 } | 486 } |
| 474 static void Log(const param_type& p, std::string* l) { | 487 static void Log(const param_type& p, std::string* l) { |
| 475 LogBytes(p, l); | 488 LogBytes(p, l); |
| 476 } | 489 } |
| 477 }; | 490 }; |
| 478 | 491 |
| 479 template <> | 492 template <> |
| 480 struct ParamTraits<std::vector<char> > { | 493 struct ParamTraits<std::vector<char> > { |
| 481 typedef std::vector<char> param_type; | 494 typedef std::vector<char> param_type; |
| 482 static void Write(Message* m, const param_type& p) { | 495 static void Write(Message* m, const param_type& p) { |
| 483 if (p.empty()) { | 496 if (p.empty()) { |
| 484 m->WriteData(NULL, 0); | 497 m->WriteData(NULL, 0); |
| 485 } else { | 498 } else { |
| 486 m->WriteData(&p.front(), static_cast<int>(p.size())); | 499 m->WriteData(&p.front(), static_cast<int>(p.size())); |
| 487 } | 500 } |
| 488 } | 501 } |
| 489 static bool Read(const Message* m, void** iter, param_type* r) { | 502 static bool Read(const Message* m, PickleIterator* iter, |
| 503 param_type* r) { |
| 490 const char *data; | 504 const char *data; |
| 491 int data_size = 0; | 505 int data_size = 0; |
| 492 if (!m->ReadData(iter, &data, &data_size) || data_size < 0) | 506 if (!m->ReadData(iter, &data, &data_size) || data_size < 0) |
| 493 return false; | 507 return false; |
| 494 r->resize(data_size); | 508 r->resize(data_size); |
| 495 if (data_size) | 509 if (data_size) |
| 496 memcpy(&r->front(), data, data_size); | 510 memcpy(&r->front(), data, data_size); |
| 497 return true; | 511 return true; |
| 498 } | 512 } |
| 499 static void Log(const param_type& p, std::string* l) { | 513 static void Log(const param_type& p, std::string* l) { |
| 500 LogBytes(p, l); | 514 LogBytes(p, l); |
| 501 } | 515 } |
| 502 }; | 516 }; |
| 503 | 517 |
| 504 template <> | 518 template <> |
| 505 struct ParamTraits<std::vector<bool> > { | 519 struct ParamTraits<std::vector<bool> > { |
| 506 typedef std::vector<bool> param_type; | 520 typedef std::vector<bool> param_type; |
| 507 static void Write(Message* m, const param_type& p) { | 521 static void Write(Message* m, const param_type& p) { |
| 508 WriteParam(m, static_cast<int>(p.size())); | 522 WriteParam(m, static_cast<int>(p.size())); |
| 509 for (size_t i = 0; i < p.size(); i++) | 523 for (size_t i = 0; i < p.size(); i++) |
| 510 WriteParam(m, p[i]); | 524 WriteParam(m, p[i]); |
| 511 } | 525 } |
| 512 static bool Read(const Message* m, void** iter, param_type* r) { | 526 static bool Read(const Message* m, PickleIterator* iter, |
| 527 param_type* r) { |
| 513 int size; | 528 int size; |
| 514 // ReadLength() checks for < 0 itself. | 529 // ReadLength() checks for < 0 itself. |
| 515 if (!m->ReadLength(iter, &size)) | 530 if (!m->ReadLength(iter, &size)) |
| 516 return false; | 531 return false; |
| 517 r->resize(size); | 532 r->resize(size); |
| 518 for (int i = 0; i < size; i++) { | 533 for (int i = 0; i < size; i++) { |
| 519 bool value; | 534 bool value; |
| 520 if (!ReadParam(m, iter, &value)) | 535 if (!ReadParam(m, iter, &value)) |
| 521 return false; | 536 return false; |
| 522 (*r)[i] = value; | 537 (*r)[i] = value; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 533 }; | 548 }; |
| 534 | 549 |
| 535 template <class P> | 550 template <class P> |
| 536 struct ParamTraits<std::vector<P> > { | 551 struct ParamTraits<std::vector<P> > { |
| 537 typedef std::vector<P> param_type; | 552 typedef std::vector<P> param_type; |
| 538 static void Write(Message* m, const param_type& p) { | 553 static void Write(Message* m, const param_type& p) { |
| 539 WriteParam(m, static_cast<int>(p.size())); | 554 WriteParam(m, static_cast<int>(p.size())); |
| 540 for (size_t i = 0; i < p.size(); i++) | 555 for (size_t i = 0; i < p.size(); i++) |
| 541 WriteParam(m, p[i]); | 556 WriteParam(m, p[i]); |
| 542 } | 557 } |
| 543 static bool Read(const Message* m, void** iter, param_type* r) { | 558 static bool Read(const Message* m, PickleIterator* iter, |
| 559 param_type* r) { |
| 544 int size; | 560 int size; |
| 545 // ReadLength() checks for < 0 itself. | 561 // ReadLength() checks for < 0 itself. |
| 546 if (!m->ReadLength(iter, &size)) | 562 if (!m->ReadLength(iter, &size)) |
| 547 return false; | 563 return false; |
| 548 // Resizing beforehand is not safe, see BUG 1006367 for details. | 564 // Resizing beforehand is not safe, see BUG 1006367 for details. |
| 549 if (INT_MAX / sizeof(P) <= static_cast<size_t>(size)) | 565 if (INT_MAX / sizeof(P) <= static_cast<size_t>(size)) |
| 550 return false; | 566 return false; |
| 551 r->resize(size); | 567 r->resize(size); |
| 552 for (int i = 0; i < size; i++) { | 568 for (int i = 0; i < size; i++) { |
| 553 if (!ReadParam(m, iter, &(*r)[i])) | 569 if (!ReadParam(m, iter, &(*r)[i])) |
| (...skipping 12 matching lines...) Expand all Loading... |
| 566 | 582 |
| 567 template <class P> | 583 template <class P> |
| 568 struct ParamTraits<std::set<P> > { | 584 struct ParamTraits<std::set<P> > { |
| 569 typedef std::set<P> param_type; | 585 typedef std::set<P> param_type; |
| 570 static void Write(Message* m, const param_type& p) { | 586 static void Write(Message* m, const param_type& p) { |
| 571 WriteParam(m, static_cast<int>(p.size())); | 587 WriteParam(m, static_cast<int>(p.size())); |
| 572 typename param_type::const_iterator iter; | 588 typename param_type::const_iterator iter; |
| 573 for (iter = p.begin(); iter != p.end(); ++iter) | 589 for (iter = p.begin(); iter != p.end(); ++iter) |
| 574 WriteParam(m, *iter); | 590 WriteParam(m, *iter); |
| 575 } | 591 } |
| 576 static bool Read(const Message* m, void** iter, param_type* r) { | 592 static bool Read(const Message* m, PickleIterator* iter, |
| 593 param_type* r) { |
| 577 int size; | 594 int size; |
| 578 if (!m->ReadLength(iter, &size)) | 595 if (!m->ReadLength(iter, &size)) |
| 579 return false; | 596 return false; |
| 580 for (int i = 0; i < size; ++i) { | 597 for (int i = 0; i < size; ++i) { |
| 581 P item; | 598 P item; |
| 582 if (!ReadParam(m, iter, &item)) | 599 if (!ReadParam(m, iter, &item)) |
| 583 return false; | 600 return false; |
| 584 r->insert(item); | 601 r->insert(item); |
| 585 } | 602 } |
| 586 return true; | 603 return true; |
| 587 } | 604 } |
| 588 static void Log(const param_type& p, std::string* l) { | 605 static void Log(const param_type& p, std::string* l) { |
| 589 l->append("<std::set>"); | 606 l->append("<std::set>"); |
| 590 } | 607 } |
| 591 }; | 608 }; |
| 592 | 609 |
| 593 | 610 |
| 594 template <class K, class V> | 611 template <class K, class V> |
| 595 struct ParamTraits<std::map<K, V> > { | 612 struct ParamTraits<std::map<K, V> > { |
| 596 typedef std::map<K, V> param_type; | 613 typedef std::map<K, V> param_type; |
| 597 static void Write(Message* m, const param_type& p) { | 614 static void Write(Message* m, const param_type& p) { |
| 598 WriteParam(m, static_cast<int>(p.size())); | 615 WriteParam(m, static_cast<int>(p.size())); |
| 599 typename param_type::const_iterator iter; | 616 typename param_type::const_iterator iter; |
| 600 for (iter = p.begin(); iter != p.end(); ++iter) { | 617 for (iter = p.begin(); iter != p.end(); ++iter) { |
| 601 WriteParam(m, iter->first); | 618 WriteParam(m, iter->first); |
| 602 WriteParam(m, iter->second); | 619 WriteParam(m, iter->second); |
| 603 } | 620 } |
| 604 } | 621 } |
| 605 static bool Read(const Message* m, void** iter, param_type* r) { | 622 static bool Read(const Message* m, PickleIterator* iter, |
| 623 param_type* r) { |
| 606 int size; | 624 int size; |
| 607 if (!ReadParam(m, iter, &size) || size < 0) | 625 if (!ReadParam(m, iter, &size) || size < 0) |
| 608 return false; | 626 return false; |
| 609 for (int i = 0; i < size; ++i) { | 627 for (int i = 0; i < size; ++i) { |
| 610 K k; | 628 K k; |
| 611 if (!ReadParam(m, iter, &k)) | 629 if (!ReadParam(m, iter, &k)) |
| 612 return false; | 630 return false; |
| 613 V& value = (*r)[k]; | 631 V& value = (*r)[k]; |
| 614 if (!ReadParam(m, iter, &value)) | 632 if (!ReadParam(m, iter, &value)) |
| 615 return false; | 633 return false; |
| 616 } | 634 } |
| 617 return true; | 635 return true; |
| 618 } | 636 } |
| 619 static void Log(const param_type& p, std::string* l) { | 637 static void Log(const param_type& p, std::string* l) { |
| 620 l->append("<std::map>"); | 638 l->append("<std::map>"); |
| 621 } | 639 } |
| 622 }; | 640 }; |
| 623 | 641 |
| 624 | 642 |
| 625 template <> | 643 template <> |
| 626 struct ParamTraits<std::wstring> { | 644 struct ParamTraits<std::wstring> { |
| 627 typedef std::wstring param_type; | 645 typedef std::wstring param_type; |
| 628 static void Write(Message* m, const param_type& p) { | 646 static void Write(Message* m, const param_type& p) { |
| 629 m->WriteWString(p); | 647 m->WriteWString(p); |
| 630 } | 648 } |
| 631 static bool Read(const Message* m, void** iter, param_type* r) { | 649 static bool Read(const Message* m, PickleIterator* iter, |
| 650 param_type* r) { |
| 632 return m->ReadWString(iter, r); | 651 return m->ReadWString(iter, r); |
| 633 } | 652 } |
| 634 IPC_EXPORT static void Log(const param_type& p, std::string* l); | 653 IPC_EXPORT static void Log(const param_type& p, std::string* l); |
| 635 }; | 654 }; |
| 636 | 655 |
| 637 template <class A, class B> | 656 template <class A, class B> |
| 638 struct ParamTraits<std::pair<A, B> > { | 657 struct ParamTraits<std::pair<A, B> > { |
| 639 typedef std::pair<A, B> param_type; | 658 typedef std::pair<A, B> param_type; |
| 640 static void Write(Message* m, const param_type& p) { | 659 static void Write(Message* m, const param_type& p) { |
| 641 WriteParam(m, p.first); | 660 WriteParam(m, p.first); |
| 642 WriteParam(m, p.second); | 661 WriteParam(m, p.second); |
| 643 } | 662 } |
| 644 static bool Read(const Message* m, void** iter, param_type* r) { | 663 static bool Read(const Message* m, PickleIterator* iter, |
| 664 param_type* r) { |
| 645 return ReadParam(m, iter, &r->first) && ReadParam(m, iter, &r->second); | 665 return ReadParam(m, iter, &r->first) && ReadParam(m, iter, &r->second); |
| 646 } | 666 } |
| 647 static void Log(const param_type& p, std::string* l) { | 667 static void Log(const param_type& p, std::string* l) { |
| 648 l->append("("); | 668 l->append("("); |
| 649 LogParam(p.first, l); | 669 LogParam(p.first, l); |
| 650 l->append(", "); | 670 l->append(", "); |
| 651 LogParam(p.second, l); | 671 LogParam(p.second, l); |
| 652 l->append(")"); | 672 l->append(")"); |
| 653 } | 673 } |
| 654 }; | 674 }; |
| 655 | 675 |
| 656 template <> | 676 template <> |
| 657 struct IPC_EXPORT ParamTraits<NullableString16> { | 677 struct IPC_EXPORT ParamTraits<NullableString16> { |
| 658 typedef NullableString16 param_type; | 678 typedef NullableString16 param_type; |
| 659 static void Write(Message* m, const param_type& p); | 679 static void Write(Message* m, const param_type& p); |
| 660 static bool Read(const Message* m, void** iter, param_type* r); | 680 static bool Read(const Message* m, PickleIterator* iter, |
| 681 param_type* r); |
| 661 static void Log(const param_type& p, std::string* l); | 682 static void Log(const param_type& p, std::string* l); |
| 662 }; | 683 }; |
| 663 | 684 |
| 664 // If WCHAR_T_IS_UTF16 is defined, then string16 is a std::wstring so we don't | 685 // If WCHAR_T_IS_UTF16 is defined, then string16 is a std::wstring so we don't |
| 665 // need this trait. | 686 // need this trait. |
| 666 #if !defined(WCHAR_T_IS_UTF16) | 687 #if !defined(WCHAR_T_IS_UTF16) |
| 667 template <> | 688 template <> |
| 668 struct ParamTraits<string16> { | 689 struct ParamTraits<string16> { |
| 669 typedef string16 param_type; | 690 typedef string16 param_type; |
| 670 static void Write(Message* m, const param_type& p) { | 691 static void Write(Message* m, const param_type& p) { |
| 671 m->WriteString16(p); | 692 m->WriteString16(p); |
| 672 } | 693 } |
| 673 static bool Read(const Message* m, void** iter, param_type* r) { | 694 static bool Read(const Message* m, PickleIterator* iter, |
| 695 param_type* r) { |
| 674 return m->ReadString16(iter, r); | 696 return m->ReadString16(iter, r); |
| 675 } | 697 } |
| 676 IPC_EXPORT static void Log(const param_type& p, std::string* l); | 698 IPC_EXPORT static void Log(const param_type& p, std::string* l); |
| 677 }; | 699 }; |
| 678 #endif | 700 #endif |
| 679 | 701 |
| 680 // and, a few more useful types... | 702 // and, a few more useful types... |
| 681 #if defined(OS_WIN) | 703 #if defined(OS_WIN) |
| 682 template <> | 704 template <> |
| 683 struct ParamTraits<HANDLE> { | 705 struct ParamTraits<HANDLE> { |
| 684 typedef HANDLE param_type; | 706 typedef HANDLE param_type; |
| 685 static void Write(Message* m, const param_type& p) { | 707 static void Write(Message* m, const param_type& p) { |
| 686 // Note that HWNDs/HANDLE/HCURSOR/HACCEL etc are always 32 bits, even on 64 | 708 // Note that HWNDs/HANDLE/HCURSOR/HACCEL etc are always 32 bits, even on 64 |
| 687 // bit systems. | 709 // bit systems. |
| 688 m->WriteUInt32(reinterpret_cast<uint32>(p)); | 710 m->WriteUInt32(reinterpret_cast<uint32>(p)); |
| 689 } | 711 } |
| 690 static bool Read(const Message* m, void** iter, param_type* r) { | 712 static bool Read(const Message* m, PickleIterator* iter, |
| 713 param_type* r) { |
| 691 DCHECK_EQ(sizeof(param_type), sizeof(uint32)); | 714 DCHECK_EQ(sizeof(param_type), sizeof(uint32)); |
| 692 return m->ReadUInt32(iter, reinterpret_cast<uint32*>(r)); | 715 return m->ReadUInt32(iter, reinterpret_cast<uint32*>(r)); |
| 693 } | 716 } |
| 694 static void Log(const param_type& p, std::string* l) { | 717 static void Log(const param_type& p, std::string* l) { |
| 695 l->append(StringPrintf("0x%X", p)); | 718 l->append(StringPrintf("0x%X", p)); |
| 696 } | 719 } |
| 697 }; | 720 }; |
| 698 | 721 |
| 699 template <> | 722 template <> |
| 700 struct ParamTraits<HCURSOR> { | 723 struct ParamTraits<HCURSOR> { |
| 701 typedef HCURSOR param_type; | 724 typedef HCURSOR param_type; |
| 702 static void Write(Message* m, const param_type& p) { | 725 static void Write(Message* m, const param_type& p) { |
| 703 m->WriteUInt32(reinterpret_cast<uint32>(p)); | 726 m->WriteUInt32(reinterpret_cast<uint32>(p)); |
| 704 } | 727 } |
| 705 static bool Read(const Message* m, void** iter, param_type* r) { | 728 static bool Read(const Message* m, PickleIterator* iter, param_type* r) { |
| 706 DCHECK_EQ(sizeof(param_type), sizeof(uint32)); | 729 DCHECK_EQ(sizeof(param_type), sizeof(uint32)); |
| 707 return m->ReadUInt32(iter, reinterpret_cast<uint32*>(r)); | 730 return m->ReadUInt32(iter, reinterpret_cast<uint32*>(r)); |
| 708 } | 731 } |
| 709 static void Log(const param_type& p, std::string* l) { | 732 static void Log(const param_type& p, std::string* l) { |
| 710 l->append(StringPrintf("0x%X", p)); | 733 l->append(StringPrintf("0x%X", p)); |
| 711 } | 734 } |
| 712 }; | 735 }; |
| 713 | 736 |
| 714 template <> | 737 template <> |
| 715 struct ParamTraits<HACCEL> { | 738 struct ParamTraits<HACCEL> { |
| 716 typedef HACCEL param_type; | 739 typedef HACCEL param_type; |
| 717 static void Write(Message* m, const param_type& p) { | 740 static void Write(Message* m, const param_type& p) { |
| 718 m->WriteUInt32(reinterpret_cast<uint32>(p)); | 741 m->WriteUInt32(reinterpret_cast<uint32>(p)); |
| 719 } | 742 } |
| 720 static bool Read(const Message* m, void** iter, param_type* r) { | 743 static bool Read(const Message* m, PickleIterator* iter, param_type* r) { |
| 721 DCHECK_EQ(sizeof(param_type), sizeof(uint32)); | 744 DCHECK_EQ(sizeof(param_type), sizeof(uint32)); |
| 722 return m->ReadUInt32(iter, reinterpret_cast<uint32*>(r)); | 745 return m->ReadUInt32(iter, reinterpret_cast<uint32*>(r)); |
| 723 } | 746 } |
| 724 }; | 747 }; |
| 725 | 748 |
| 726 template <> | 749 template <> |
| 727 struct ParamTraits<POINT> { | 750 struct ParamTraits<POINT> { |
| 728 typedef POINT param_type; | 751 typedef POINT param_type; |
| 729 static void Write(Message* m, const param_type& p) { | 752 static void Write(Message* m, const param_type& p) { |
| 730 m->WriteInt(p.x); | 753 m->WriteInt(p.x); |
| 731 m->WriteInt(p.y); | 754 m->WriteInt(p.y); |
| 732 } | 755 } |
| 733 static bool Read(const Message* m, void** iter, param_type* r) { | 756 static bool Read(const Message* m, PickleIterator* iter, param_type* r) { |
| 734 int x, y; | 757 int x, y; |
| 735 if (!m->ReadInt(iter, &x) || !m->ReadInt(iter, &y)) | 758 if (!m->ReadInt(iter, &x) || !m->ReadInt(iter, &y)) |
| 736 return false; | 759 return false; |
| 737 r->x = x; | 760 r->x = x; |
| 738 r->y = y; | 761 r->y = y; |
| 739 return true; | 762 return true; |
| 740 } | 763 } |
| 741 static void Log(const param_type& p, std::string* l) { | 764 static void Log(const param_type& p, std::string* l) { |
| 742 l->append(StringPrintf("(%d, %d)", p.x, p.y)); | 765 l->append(StringPrintf("(%d, %d)", p.x, p.y)); |
| 743 } | 766 } |
| 744 }; | 767 }; |
| 745 #endif // defined(OS_WIN) | 768 #endif // defined(OS_WIN) |
| 746 | 769 |
| 747 template <> | 770 template <> |
| 748 struct IPC_EXPORT ParamTraits<FilePath> { | 771 struct IPC_EXPORT ParamTraits<FilePath> { |
| 749 typedef FilePath param_type; | 772 typedef FilePath param_type; |
| 750 static void Write(Message* m, const param_type& p); | 773 static void Write(Message* m, const param_type& p); |
| 751 static bool Read(const Message* m, void** iter, param_type* r); | 774 static bool Read(const Message* m, PickleIterator* iter, param_type* r); |
| 752 static void Log(const param_type& p, std::string* l); | 775 static void Log(const param_type& p, std::string* l); |
| 753 }; | 776 }; |
| 754 | 777 |
| 755 #if defined(OS_POSIX) | 778 #if defined(OS_POSIX) |
| 756 // FileDescriptors may be serialised over IPC channels on POSIX. On the | 779 // FileDescriptors may be serialised over IPC channels on POSIX. On the |
| 757 // receiving side, the FileDescriptor is a valid duplicate of the file | 780 // receiving side, the FileDescriptor is a valid duplicate of the file |
| 758 // descriptor which was transmitted: *it is not just a copy of the integer like | 781 // descriptor which was transmitted: *it is not just a copy of the integer like |
| 759 // HANDLEs on Windows*. The only exception is if the file descriptor is < 0. In | 782 // HANDLEs on Windows*. The only exception is if the file descriptor is < 0. In |
| 760 // this case, the receiving end will see a value of -1. *Zero is a valid file | 783 // this case, the receiving end will see a value of -1. *Zero is a valid file |
| 761 // descriptor*. | 784 // descriptor*. |
| 762 // | 785 // |
| 763 // The received file descriptor will have the |auto_close| flag set to true. The | 786 // The received file descriptor will have the |auto_close| flag set to true. The |
| 764 // code which handles the message is responsible for taking ownership of it. | 787 // code which handles the message is responsible for taking ownership of it. |
| 765 // File descriptors are OS resources and must be closed when no longer needed. | 788 // File descriptors are OS resources and must be closed when no longer needed. |
| 766 // | 789 // |
| 767 // When sending a file descriptor, the file descriptor must be valid at the time | 790 // When sending a file descriptor, the file descriptor must be valid at the time |
| 768 // of transmission. Since transmission is not synchronous, one should consider | 791 // of transmission. Since transmission is not synchronous, one should consider |
| 769 // dup()ing any file descriptors to be transmitted and setting the |auto_close| | 792 // dup()ing any file descriptors to be transmitted and setting the |auto_close| |
| 770 // flag, which causes the file descriptor to be closed after writing. | 793 // flag, which causes the file descriptor to be closed after writing. |
| 771 template<> | 794 template<> |
| 772 struct IPC_EXPORT ParamTraits<base::FileDescriptor> { | 795 struct IPC_EXPORT ParamTraits<base::FileDescriptor> { |
| 773 typedef base::FileDescriptor param_type; | 796 typedef base::FileDescriptor param_type; |
| 774 static void Write(Message* m, const param_type& p); | 797 static void Write(Message* m, const param_type& p); |
| 775 static bool Read(const Message* m, void** iter, param_type* r); | 798 static bool Read(const Message* m, PickleIterator* iter, param_type* r); |
| 776 static void Log(const param_type& p, std::string* l); | 799 static void Log(const param_type& p, std::string* l); |
| 777 }; | 800 }; |
| 778 #endif // defined(OS_POSIX) | 801 #endif // defined(OS_POSIX) |
| 779 | 802 |
| 780 // A ChannelHandle is basically a platform-inspecific wrapper around the | 803 // A ChannelHandle is basically a platform-inspecific wrapper around the |
| 781 // fact that IPC endpoints are handled specially on POSIX. See above comments | 804 // fact that IPC endpoints are handled specially on POSIX. See above comments |
| 782 // on FileDescriptor for more background. | 805 // on FileDescriptor for more background. |
| 783 template<> | 806 template<> |
| 784 struct IPC_EXPORT ParamTraits<IPC::ChannelHandle> { | 807 struct IPC_EXPORT ParamTraits<IPC::ChannelHandle> { |
| 785 typedef ChannelHandle param_type; | 808 typedef ChannelHandle param_type; |
| 786 static void Write(Message* m, const param_type& p); | 809 static void Write(Message* m, const param_type& p); |
| 787 static bool Read(const Message* m, void** iter, param_type* r); | 810 static bool Read(const Message* m, PickleIterator* iter, param_type* r); |
| 788 static void Log(const param_type& p, std::string* l); | 811 static void Log(const param_type& p, std::string* l); |
| 789 }; | 812 }; |
| 790 | 813 |
| 791 #if defined(OS_WIN) | 814 #if defined(OS_WIN) |
| 792 template <> | 815 template <> |
| 793 struct ParamTraits<XFORM> { | 816 struct ParamTraits<XFORM> { |
| 794 typedef XFORM param_type; | 817 typedef XFORM param_type; |
| 795 static void Write(Message* m, const param_type& p) { | 818 static void Write(Message* m, const param_type& p) { |
| 796 m->WriteData(reinterpret_cast<const char*>(&p), sizeof(XFORM)); | 819 m->WriteData(reinterpret_cast<const char*>(&p), sizeof(XFORM)); |
| 797 } | 820 } |
| 798 static bool Read(const Message* m, void** iter, param_type* r) { | 821 static bool Read(const Message* m, PickleIterator* iter, param_type* r) { |
| 799 const char *data; | 822 const char *data; |
| 800 int data_size = 0; | 823 int data_size = 0; |
| 801 bool result = m->ReadData(iter, &data, &data_size); | 824 bool result = m->ReadData(iter, &data, &data_size); |
| 802 if (result && data_size == sizeof(XFORM)) { | 825 if (result && data_size == sizeof(XFORM)) { |
| 803 memcpy(r, data, sizeof(XFORM)); | 826 memcpy(r, data, sizeof(XFORM)); |
| 804 } else { | 827 } else { |
| 805 result = false; | 828 result = false; |
| 806 NOTREACHED(); | 829 NOTREACHED(); |
| 807 } | 830 } |
| 808 | 831 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 828 int64 dispatch; // Time after it was dispatched (i.e. after calling | 851 int64 dispatch; // Time after it was dispatched (i.e. after calling |
| 829 // OnMessageReceived). | 852 // OnMessageReceived). |
| 830 std::string message_name; | 853 std::string message_name; |
| 831 std::string params; | 854 std::string params; |
| 832 }; | 855 }; |
| 833 | 856 |
| 834 template <> | 857 template <> |
| 835 struct IPC_EXPORT ParamTraits<LogData> { | 858 struct IPC_EXPORT ParamTraits<LogData> { |
| 836 typedef LogData param_type; | 859 typedef LogData param_type; |
| 837 static void Write(Message* m, const param_type& p); | 860 static void Write(Message* m, const param_type& p); |
| 838 static bool Read(const Message* m, void** iter, param_type* r); | 861 static bool Read(const Message* m, PickleIterator* iter, param_type* r); |
| 839 static void Log(const param_type& p, std::string* l) { | 862 static void Log(const param_type& p, std::string* l) { |
| 840 // Doesn't make sense to implement this! | 863 // Doesn't make sense to implement this! |
| 841 } | 864 } |
| 842 }; | 865 }; |
| 843 | 866 |
| 844 template <> | 867 template <> |
| 845 struct ParamTraits<Message> { | 868 struct ParamTraits<Message> { |
| 846 static void Write(Message* m, const Message& p) { | 869 static void Write(Message* m, const Message& p) { |
| 847 DCHECK(p.size() <= INT_MAX); | 870 DCHECK(p.size() <= INT_MAX); |
| 848 int message_size = static_cast<int>(p.size()); | 871 int message_size = static_cast<int>(p.size()); |
| 849 m->WriteInt(message_size); | 872 m->WriteInt(message_size); |
| 850 m->WriteData(reinterpret_cast<const char*>(p.data()), message_size); | 873 m->WriteData(reinterpret_cast<const char*>(p.data()), message_size); |
| 851 } | 874 } |
| 852 static bool Read(const Message* m, void** iter, Message* r) { | 875 static bool Read(const Message* m, PickleIterator* iter, Message* r) { |
| 853 int size; | 876 int size; |
| 854 if (!m->ReadInt(iter, &size)) | 877 if (!m->ReadInt(iter, &size)) |
| 855 return false; | 878 return false; |
| 856 const char* data; | 879 const char* data; |
| 857 if (!m->ReadData(iter, &data, &size)) | 880 if (!m->ReadData(iter, &data, &size)) |
| 858 return false; | 881 return false; |
| 859 *r = Message(data, size); | 882 *r = Message(data, size); |
| 860 return true; | 883 return true; |
| 861 } | 884 } |
| 862 static void Log(const Message& p, std::string* l) { | 885 static void Log(const Message& p, std::string* l) { |
| 863 l->append("<IPC::Message>"); | 886 l->append("<IPC::Message>"); |
| 864 } | 887 } |
| 865 }; | 888 }; |
| 866 | 889 |
| 867 template <> | 890 template <> |
| 868 struct ParamTraits<Tuple0> { | 891 struct ParamTraits<Tuple0> { |
| 869 typedef Tuple0 param_type; | 892 typedef Tuple0 param_type; |
| 870 static void Write(Message* m, const param_type& p) { | 893 static void Write(Message* m, const param_type& p) { |
| 871 } | 894 } |
| 872 static bool Read(const Message* m, void** iter, param_type* r) { | 895 static bool Read(const Message* m, PickleIterator* iter, param_type* r) { |
| 873 return true; | 896 return true; |
| 874 } | 897 } |
| 875 static void Log(const param_type& p, std::string* l) { | 898 static void Log(const param_type& p, std::string* l) { |
| 876 } | 899 } |
| 877 }; | 900 }; |
| 878 | 901 |
| 879 template <class A> | 902 template <class A> |
| 880 struct ParamTraits< Tuple1<A> > { | 903 struct ParamTraits< Tuple1<A> > { |
| 881 typedef Tuple1<A> param_type; | 904 typedef Tuple1<A> param_type; |
| 882 static void Write(Message* m, const param_type& p) { | 905 static void Write(Message* m, const param_type& p) { |
| 883 WriteParam(m, p.a); | 906 WriteParam(m, p.a); |
| 884 } | 907 } |
| 885 static bool Read(const Message* m, void** iter, param_type* r) { | 908 static bool Read(const Message* m, PickleIterator* iter, param_type* r) { |
| 886 return ReadParam(m, iter, &r->a); | 909 return ReadParam(m, iter, &r->a); |
| 887 } | 910 } |
| 888 static void Log(const param_type& p, std::string* l) { | 911 static void Log(const param_type& p, std::string* l) { |
| 889 LogParam(p.a, l); | 912 LogParam(p.a, l); |
| 890 } | 913 } |
| 891 }; | 914 }; |
| 892 | 915 |
| 893 template <class A, class B> | 916 template <class A, class B> |
| 894 struct ParamTraits< Tuple2<A, B> > { | 917 struct ParamTraits< Tuple2<A, B> > { |
| 895 typedef Tuple2<A, B> param_type; | 918 typedef Tuple2<A, B> param_type; |
| 896 static void Write(Message* m, const param_type& p) { | 919 static void Write(Message* m, const param_type& p) { |
| 897 WriteParam(m, p.a); | 920 WriteParam(m, p.a); |
| 898 WriteParam(m, p.b); | 921 WriteParam(m, p.b); |
| 899 } | 922 } |
| 900 static bool Read(const Message* m, void** iter, param_type* r) { | 923 static bool Read(const Message* m, PickleIterator* iter, param_type* r) { |
| 901 return (ReadParam(m, iter, &r->a) && | 924 return (ReadParam(m, iter, &r->a) && |
| 902 ReadParam(m, iter, &r->b)); | 925 ReadParam(m, iter, &r->b)); |
| 903 } | 926 } |
| 904 static void Log(const param_type& p, std::string* l) { | 927 static void Log(const param_type& p, std::string* l) { |
| 905 LogParam(p.a, l); | 928 LogParam(p.a, l); |
| 906 l->append(", "); | 929 l->append(", "); |
| 907 LogParam(p.b, l); | 930 LogParam(p.b, l); |
| 908 } | 931 } |
| 909 }; | 932 }; |
| 910 | 933 |
| 911 template <class A, class B, class C> | 934 template <class A, class B, class C> |
| 912 struct ParamTraits< Tuple3<A, B, C> > { | 935 struct ParamTraits< Tuple3<A, B, C> > { |
| 913 typedef Tuple3<A, B, C> param_type; | 936 typedef Tuple3<A, B, C> param_type; |
| 914 static void Write(Message* m, const param_type& p) { | 937 static void Write(Message* m, const param_type& p) { |
| 915 WriteParam(m, p.a); | 938 WriteParam(m, p.a); |
| 916 WriteParam(m, p.b); | 939 WriteParam(m, p.b); |
| 917 WriteParam(m, p.c); | 940 WriteParam(m, p.c); |
| 918 } | 941 } |
| 919 static bool Read(const Message* m, void** iter, param_type* r) { | 942 static bool Read(const Message* m, PickleIterator* iter, param_type* r) { |
| 920 return (ReadParam(m, iter, &r->a) && | 943 return (ReadParam(m, iter, &r->a) && |
| 921 ReadParam(m, iter, &r->b) && | 944 ReadParam(m, iter, &r->b) && |
| 922 ReadParam(m, iter, &r->c)); | 945 ReadParam(m, iter, &r->c)); |
| 923 } | 946 } |
| 924 static void Log(const param_type& p, std::string* l) { | 947 static void Log(const param_type& p, std::string* l) { |
| 925 LogParam(p.a, l); | 948 LogParam(p.a, l); |
| 926 l->append(", "); | 949 l->append(", "); |
| 927 LogParam(p.b, l); | 950 LogParam(p.b, l); |
| 928 l->append(", "); | 951 l->append(", "); |
| 929 LogParam(p.c, l); | 952 LogParam(p.c, l); |
| 930 } | 953 } |
| 931 }; | 954 }; |
| 932 | 955 |
| 933 template <class A, class B, class C, class D> | 956 template <class A, class B, class C, class D> |
| 934 struct ParamTraits< Tuple4<A, B, C, D> > { | 957 struct ParamTraits< Tuple4<A, B, C, D> > { |
| 935 typedef Tuple4<A, B, C, D> param_type; | 958 typedef Tuple4<A, B, C, D> param_type; |
| 936 static void Write(Message* m, const param_type& p) { | 959 static void Write(Message* m, const param_type& p) { |
| 937 WriteParam(m, p.a); | 960 WriteParam(m, p.a); |
| 938 WriteParam(m, p.b); | 961 WriteParam(m, p.b); |
| 939 WriteParam(m, p.c); | 962 WriteParam(m, p.c); |
| 940 WriteParam(m, p.d); | 963 WriteParam(m, p.d); |
| 941 } | 964 } |
| 942 static bool Read(const Message* m, void** iter, param_type* r) { | 965 static bool Read(const Message* m, PickleIterator* iter, param_type* r) { |
| 943 return (ReadParam(m, iter, &r->a) && | 966 return (ReadParam(m, iter, &r->a) && |
| 944 ReadParam(m, iter, &r->b) && | 967 ReadParam(m, iter, &r->b) && |
| 945 ReadParam(m, iter, &r->c) && | 968 ReadParam(m, iter, &r->c) && |
| 946 ReadParam(m, iter, &r->d)); | 969 ReadParam(m, iter, &r->d)); |
| 947 } | 970 } |
| 948 static void Log(const param_type& p, std::string* l) { | 971 static void Log(const param_type& p, std::string* l) { |
| 949 LogParam(p.a, l); | 972 LogParam(p.a, l); |
| 950 l->append(", "); | 973 l->append(", "); |
| 951 LogParam(p.b, l); | 974 LogParam(p.b, l); |
| 952 l->append(", "); | 975 l->append(", "); |
| 953 LogParam(p.c, l); | 976 LogParam(p.c, l); |
| 954 l->append(", "); | 977 l->append(", "); |
| 955 LogParam(p.d, l); | 978 LogParam(p.d, l); |
| 956 } | 979 } |
| 957 }; | 980 }; |
| 958 | 981 |
| 959 template <class A, class B, class C, class D, class E> | 982 template <class A, class B, class C, class D, class E> |
| 960 struct ParamTraits< Tuple5<A, B, C, D, E> > { | 983 struct ParamTraits< Tuple5<A, B, C, D, E> > { |
| 961 typedef Tuple5<A, B, C, D, E> param_type; | 984 typedef Tuple5<A, B, C, D, E> param_type; |
| 962 static void Write(Message* m, const param_type& p) { | 985 static void Write(Message* m, const param_type& p) { |
| 963 WriteParam(m, p.a); | 986 WriteParam(m, p.a); |
| 964 WriteParam(m, p.b); | 987 WriteParam(m, p.b); |
| 965 WriteParam(m, p.c); | 988 WriteParam(m, p.c); |
| 966 WriteParam(m, p.d); | 989 WriteParam(m, p.d); |
| 967 WriteParam(m, p.e); | 990 WriteParam(m, p.e); |
| 968 } | 991 } |
| 969 static bool Read(const Message* m, void** iter, param_type* r) { | 992 static bool Read(const Message* m, PickleIterator* iter, param_type* r) { |
| 970 return (ReadParam(m, iter, &r->a) && | 993 return (ReadParam(m, iter, &r->a) && |
| 971 ReadParam(m, iter, &r->b) && | 994 ReadParam(m, iter, &r->b) && |
| 972 ReadParam(m, iter, &r->c) && | 995 ReadParam(m, iter, &r->c) && |
| 973 ReadParam(m, iter, &r->d) && | 996 ReadParam(m, iter, &r->d) && |
| 974 ReadParam(m, iter, &r->e)); | 997 ReadParam(m, iter, &r->e)); |
| 975 } | 998 } |
| 976 static void Log(const param_type& p, std::string* l) { | 999 static void Log(const param_type& p, std::string* l) { |
| 977 LogParam(p.a, l); | 1000 LogParam(p.a, l); |
| 978 l->append(", "); | 1001 l->append(", "); |
| 979 LogParam(p.b, l); | 1002 LogParam(p.b, l); |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1047 #endif | 1070 #endif |
| 1048 | 1071 |
| 1049 // This class assumes that its template argument is a RefTuple (a Tuple with | 1072 // This class assumes that its template argument is a RefTuple (a Tuple with |
| 1050 // reference elements). This would go into ipc_message_utils_impl.h, but it is | 1073 // reference elements). This would go into ipc_message_utils_impl.h, but it is |
| 1051 // also used by chrome_frame. | 1074 // also used by chrome_frame. |
| 1052 template <class RefTuple> | 1075 template <class RefTuple> |
| 1053 class ParamDeserializer : public MessageReplyDeserializer { | 1076 class ParamDeserializer : public MessageReplyDeserializer { |
| 1054 public: | 1077 public: |
| 1055 explicit ParamDeserializer(const RefTuple& out) : out_(out) { } | 1078 explicit ParamDeserializer(const RefTuple& out) : out_(out) { } |
| 1056 | 1079 |
| 1057 bool SerializeOutputParameters(const IPC::Message& msg, void* iter) { | 1080 bool SerializeOutputParameters(const IPC::Message& msg, PickleIterator iter) { |
| 1058 return ReadParam(&msg, &iter, &out_); | 1081 return ReadParam(&msg, &iter, &out_); |
| 1059 } | 1082 } |
| 1060 | 1083 |
| 1061 RefTuple out_; | 1084 RefTuple out_; |
| 1062 }; | 1085 }; |
| 1063 | 1086 |
| 1064 // Used for synchronous messages. | 1087 // Used for synchronous messages. |
| 1065 template <class SendParamType, class ReplyParamType> | 1088 template <class SendParamType, class ReplyParamType> |
| 1066 class SyncMessageSchema { | 1089 class SyncMessageSchema { |
| 1067 public: | 1090 public: |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1140 ReplyParam p(a, b, c, d, e); | 1163 ReplyParam p(a, b, c, d, e); |
| 1141 WriteParam(reply, p); | 1164 WriteParam(reply, p); |
| 1142 } | 1165 } |
| 1143 }; | 1166 }; |
| 1144 | 1167 |
| 1145 //----------------------------------------------------------------------------- | 1168 //----------------------------------------------------------------------------- |
| 1146 | 1169 |
| 1147 } // namespace IPC | 1170 } // namespace IPC |
| 1148 | 1171 |
| 1149 #endif // IPC_IPC_MESSAGE_UTILS_H_ | 1172 #endif // IPC_IPC_MESSAGE_UTILS_H_ |
| OLD | NEW |