| 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 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 | 110 |
| 111 // Primitive ParamTraits ------------------------------------------------------- | 111 // Primitive ParamTraits ------------------------------------------------------- |
| 112 | 112 |
| 113 template <> | 113 template <> |
| 114 struct ParamTraits<bool> { | 114 struct ParamTraits<bool> { |
| 115 typedef bool param_type; | 115 typedef bool param_type; |
| 116 static void Write(Message* m, const param_type& p) { | 116 static void Write(Message* m, const param_type& p) { |
| 117 m->WriteBool(p); | 117 m->WriteBool(p); |
| 118 } | 118 } |
| 119 static bool Read(const Message* m, PickleIterator* iter, param_type* r) { | 119 static bool Read(const Message* m, PickleIterator* iter, param_type* r) { |
| 120 return iter->ReadBool(r); | 120 return m->ReadBool(iter, r); |
| 121 } | 121 } |
| 122 IPC_EXPORT static void Log(const param_type& p, std::string* l); | 122 IPC_EXPORT static void Log(const param_type& p, std::string* l); |
| 123 }; | 123 }; |
| 124 | 124 |
| 125 template <> | 125 template <> |
| 126 struct IPC_EXPORT ParamTraits<unsigned char> { | 126 struct IPC_EXPORT ParamTraits<unsigned char> { |
| 127 typedef unsigned char param_type; | 127 typedef unsigned char param_type; |
| 128 static void Write(Message* m, const param_type& p); | 128 static void Write(Message* m, const param_type& p); |
| 129 static bool Read(const Message* m, PickleIterator* iter, param_type* r); | 129 static bool Read(const Message* m, PickleIterator* iter, param_type* r); |
| 130 static void Log(const param_type& p, std::string* l); | 130 static void Log(const param_type& p, std::string* l); |
| 131 }; | 131 }; |
| 132 | 132 |
| 133 template <> | 133 template <> |
| 134 struct IPC_EXPORT ParamTraits<unsigned short> { | 134 struct IPC_EXPORT ParamTraits<unsigned short> { |
| 135 typedef unsigned short param_type; | 135 typedef unsigned short param_type; |
| 136 static void Write(Message* m, const param_type& p); | 136 static void Write(Message* m, const param_type& p); |
| 137 static bool Read(const Message* m, PickleIterator* iter, param_type* r); | 137 static bool Read(const Message* m, PickleIterator* iter, param_type* r); |
| 138 static void Log(const param_type& p, std::string* l); | 138 static void Log(const param_type& p, std::string* l); |
| 139 }; | 139 }; |
| 140 | 140 |
| 141 template <> | 141 template <> |
| 142 struct ParamTraits<int> { | 142 struct ParamTraits<int> { |
| 143 typedef int param_type; | 143 typedef int param_type; |
| 144 static void Write(Message* m, const param_type& p) { | 144 static void Write(Message* m, const param_type& p) { |
| 145 m->WriteInt(p); | 145 m->WriteInt(p); |
| 146 } | 146 } |
| 147 static bool Read(const Message* m, PickleIterator* iter, param_type* r) { | 147 static bool Read(const Message* m, PickleIterator* iter, param_type* r) { |
| 148 return iter->ReadInt(r); | 148 return m->ReadInt(iter, r); |
| 149 } | 149 } |
| 150 IPC_EXPORT static void Log(const param_type& p, std::string* l); | 150 IPC_EXPORT static void Log(const param_type& p, std::string* l); |
| 151 }; | 151 }; |
| 152 | 152 |
| 153 template <> | 153 template <> |
| 154 struct ParamTraits<unsigned int> { | 154 struct ParamTraits<unsigned int> { |
| 155 typedef unsigned int param_type; | 155 typedef unsigned int param_type; |
| 156 static void Write(Message* m, const param_type& p) { | 156 static void Write(Message* m, const param_type& p) { |
| 157 m->WriteInt(p); | 157 m->WriteInt(p); |
| 158 } | 158 } |
| 159 static bool Read(const Message* m, PickleIterator* iter, param_type* r) { | 159 static bool Read(const Message* m, PickleIterator* iter, param_type* r) { |
| 160 return iter->ReadInt(reinterpret_cast<int*>(r)); | 160 return m->ReadInt(iter, reinterpret_cast<int*>(r)); |
| 161 } | 161 } |
| 162 IPC_EXPORT static void Log(const param_type& p, std::string* l); | 162 IPC_EXPORT static void Log(const param_type& p, std::string* l); |
| 163 }; | 163 }; |
| 164 | 164 |
| 165 template <> | 165 template <> |
| 166 struct ParamTraits<long> { | 166 struct ParamTraits<long> { |
| 167 typedef long param_type; | 167 typedef long param_type; |
| 168 static void Write(Message* m, const param_type& p) { | 168 static void Write(Message* m, const param_type& p) { |
| 169 m->WriteLongUsingDangerousNonPortableLessPersistableForm(p); | 169 m->WriteLongUsingDangerousNonPortableLessPersistableForm(p); |
| 170 } | 170 } |
| 171 static bool Read(const Message* m, PickleIterator* iter, param_type* r) { | 171 static bool Read(const Message* m, PickleIterator* iter, param_type* r) { |
| 172 return iter->ReadLong(r); | 172 return m->ReadLong(iter, r); |
| 173 } | 173 } |
| 174 IPC_EXPORT static void Log(const param_type& p, std::string* l); | 174 IPC_EXPORT static void Log(const param_type& p, std::string* l); |
| 175 }; | 175 }; |
| 176 | 176 |
| 177 template <> | 177 template <> |
| 178 struct ParamTraits<unsigned long> { | 178 struct ParamTraits<unsigned long> { |
| 179 typedef unsigned long param_type; | 179 typedef unsigned long param_type; |
| 180 static void Write(Message* m, const param_type& p) { | 180 static void Write(Message* m, const param_type& p) { |
| 181 m->WriteLongUsingDangerousNonPortableLessPersistableForm(p); | 181 m->WriteLongUsingDangerousNonPortableLessPersistableForm(p); |
| 182 } | 182 } |
| 183 static bool Read(const Message* m, PickleIterator* iter, param_type* r) { | 183 static bool Read(const Message* m, PickleIterator* iter, param_type* r) { |
| 184 return iter->ReadLong(reinterpret_cast<long*>(r)); | 184 return m->ReadLong(iter, reinterpret_cast<long*>(r)); |
| 185 } | 185 } |
| 186 IPC_EXPORT static void Log(const param_type& p, std::string* l); | 186 IPC_EXPORT static void Log(const param_type& p, std::string* l); |
| 187 }; | 187 }; |
| 188 | 188 |
| 189 template <> | 189 template <> |
| 190 struct ParamTraits<long long> { | 190 struct ParamTraits<long long> { |
| 191 typedef long long param_type; | 191 typedef long long param_type; |
| 192 static void Write(Message* m, const param_type& p) { | 192 static void Write(Message* m, const param_type& p) { |
| 193 m->WriteInt64(static_cast<int64>(p)); | 193 m->WriteInt64(static_cast<int64>(p)); |
| 194 } | 194 } |
| 195 static bool Read(const Message* m, PickleIterator* iter, | 195 static bool Read(const Message* m, PickleIterator* iter, |
| 196 param_type* r) { | 196 param_type* r) { |
| 197 return iter->ReadInt64(reinterpret_cast<int64*>(r)); | 197 return m->ReadInt64(iter, reinterpret_cast<int64*>(r)); |
| 198 } | 198 } |
| 199 IPC_EXPORT static void Log(const param_type& p, std::string* l); | 199 IPC_EXPORT static void Log(const param_type& p, std::string* l); |
| 200 }; | 200 }; |
| 201 | 201 |
| 202 template <> | 202 template <> |
| 203 struct ParamTraits<unsigned long long> { | 203 struct ParamTraits<unsigned long long> { |
| 204 typedef unsigned long long param_type; | 204 typedef unsigned long long param_type; |
| 205 static void Write(Message* m, const param_type& p) { | 205 static void Write(Message* m, const param_type& p) { |
| 206 m->WriteInt64(p); | 206 m->WriteInt64(p); |
| 207 } | 207 } |
| 208 static bool Read(const Message* m, PickleIterator* iter, | 208 static bool Read(const Message* m, PickleIterator* iter, |
| 209 param_type* r) { | 209 param_type* r) { |
| 210 return iter->ReadInt64(reinterpret_cast<int64*>(r)); | 210 return m->ReadInt64(iter, reinterpret_cast<int64*>(r)); |
| 211 } | 211 } |
| 212 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); |
| 213 }; | 213 }; |
| 214 | 214 |
| 215 // Note that the IPC layer doesn't sanitize NaNs and +/- INF values. Clients | 215 // Note that the IPC layer doesn't sanitize NaNs and +/- INF values. Clients |
| 216 // should be sure to check the sanity of these values after receiving them over | 216 // should be sure to check the sanity of these values after receiving them over |
| 217 // IPC. | 217 // IPC. |
| 218 template <> | 218 template <> |
| 219 struct IPC_EXPORT ParamTraits<float> { | 219 struct IPC_EXPORT ParamTraits<float> { |
| 220 typedef float param_type; | 220 typedef float param_type; |
| 221 static void Write(Message* m, const param_type& p) { | 221 static void Write(Message* m, const param_type& p) { |
| 222 m->WriteFloat(p); | 222 m->WriteFloat(p); |
| 223 } | 223 } |
| 224 static bool Read(const Message* m, PickleIterator* iter, param_type* r) { | 224 static bool Read(const Message* m, PickleIterator* iter, param_type* r) { |
| 225 return iter->ReadFloat(r); | 225 return m->ReadFloat(iter, r); |
| 226 } | 226 } |
| 227 static void Log(const param_type& p, std::string* l); | 227 static void Log(const param_type& p, std::string* l); |
| 228 }; | 228 }; |
| 229 | 229 |
| 230 template <> | 230 template <> |
| 231 struct IPC_EXPORT ParamTraits<double> { | 231 struct IPC_EXPORT ParamTraits<double> { |
| 232 typedef double param_type; | 232 typedef double param_type; |
| 233 static void Write(Message* m, const param_type& p); | 233 static void Write(Message* m, const param_type& p); |
| 234 static bool Read(const Message* m, PickleIterator* iter, param_type* r); | 234 static bool Read(const Message* m, PickleIterator* iter, param_type* r); |
| 235 static void Log(const param_type& p, std::string* l); | 235 static void Log(const param_type& p, std::string* l); |
| 236 }; | 236 }; |
| 237 | 237 |
| 238 // STL ParamTraits ------------------------------------------------------------- | 238 // STL ParamTraits ------------------------------------------------------------- |
| 239 | 239 |
| 240 template <> | 240 template <> |
| 241 struct ParamTraits<std::string> { | 241 struct ParamTraits<std::string> { |
| 242 typedef std::string param_type; | 242 typedef std::string param_type; |
| 243 static void Write(Message* m, const param_type& p) { | 243 static void Write(Message* m, const param_type& p) { |
| 244 m->WriteString(p); | 244 m->WriteString(p); |
| 245 } | 245 } |
| 246 static bool Read(const Message* m, PickleIterator* iter, | 246 static bool Read(const Message* m, PickleIterator* iter, |
| 247 param_type* r) { | 247 param_type* r) { |
| 248 return iter->ReadString(r); | 248 return m->ReadString(iter, r); |
| 249 } | 249 } |
| 250 IPC_EXPORT static void Log(const param_type& p, std::string* l); | 250 IPC_EXPORT static void Log(const param_type& p, std::string* l); |
| 251 }; | 251 }; |
| 252 | 252 |
| 253 template <> | 253 template <> |
| 254 struct ParamTraits<std::wstring> { | 254 struct ParamTraits<std::wstring> { |
| 255 typedef std::wstring param_type; | 255 typedef std::wstring param_type; |
| 256 static void Write(Message* m, const param_type& p) { | 256 static void Write(Message* m, const param_type& p) { |
| 257 m->WriteWString(p); | 257 m->WriteWString(p); |
| 258 } | 258 } |
| 259 static bool Read(const Message* m, PickleIterator* iter, | 259 static bool Read(const Message* m, PickleIterator* iter, |
| 260 param_type* r) { | 260 param_type* r) { |
| 261 return iter->ReadWString(r); | 261 return m->ReadWString(iter, r); |
| 262 } | 262 } |
| 263 IPC_EXPORT static void Log(const param_type& p, std::string* l); | 263 IPC_EXPORT static void Log(const param_type& p, std::string* l); |
| 264 }; | 264 }; |
| 265 | 265 |
| 266 // If WCHAR_T_IS_UTF16 is defined, then string16 is a std::wstring so we don't | 266 // If WCHAR_T_IS_UTF16 is defined, then string16 is a std::wstring so we don't |
| 267 // need this trait. | 267 // need this trait. |
| 268 #if !defined(WCHAR_T_IS_UTF16) | 268 #if !defined(WCHAR_T_IS_UTF16) |
| 269 template <> | 269 template <> |
| 270 struct ParamTraits<base::string16> { | 270 struct ParamTraits<base::string16> { |
| 271 typedef base::string16 param_type; | 271 typedef base::string16 param_type; |
| 272 static void Write(Message* m, const param_type& p) { | 272 static void Write(Message* m, const param_type& p) { |
| 273 m->WriteString16(p); | 273 m->WriteString16(p); |
| 274 } | 274 } |
| 275 static bool Read(const Message* m, PickleIterator* iter, | 275 static bool Read(const Message* m, PickleIterator* iter, |
| 276 param_type* r) { | 276 param_type* r) { |
| 277 return iter->ReadString16(r); | 277 return m->ReadString16(iter, r); |
| 278 } | 278 } |
| 279 IPC_EXPORT static void Log(const param_type& p, std::string* l); | 279 IPC_EXPORT static void Log(const param_type& p, std::string* l); |
| 280 }; | 280 }; |
| 281 #endif | 281 #endif |
| 282 | 282 |
| 283 template <> | 283 template <> |
| 284 struct IPC_EXPORT ParamTraits<std::vector<char> > { | 284 struct IPC_EXPORT ParamTraits<std::vector<char> > { |
| 285 typedef std::vector<char> param_type; | 285 typedef std::vector<char> param_type; |
| 286 static void Write(Message* m, const param_type& p); | 286 static void Write(Message* m, const param_type& p); |
| 287 static bool Read(const Message*, PickleIterator* iter, param_type* r); | 287 static bool Read(const Message*, PickleIterator* iter, param_type* r); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 309 typedef std::vector<P> param_type; | 309 typedef std::vector<P> param_type; |
| 310 static void Write(Message* m, const param_type& p) { | 310 static void Write(Message* m, const param_type& p) { |
| 311 WriteParam(m, static_cast<int>(p.size())); | 311 WriteParam(m, static_cast<int>(p.size())); |
| 312 for (size_t i = 0; i < p.size(); i++) | 312 for (size_t i = 0; i < p.size(); i++) |
| 313 WriteParam(m, p[i]); | 313 WriteParam(m, p[i]); |
| 314 } | 314 } |
| 315 static bool Read(const Message* m, PickleIterator* iter, | 315 static bool Read(const Message* m, PickleIterator* iter, |
| 316 param_type* r) { | 316 param_type* r) { |
| 317 int size; | 317 int size; |
| 318 // ReadLength() checks for < 0 itself. | 318 // ReadLength() checks for < 0 itself. |
| 319 if (!iter->ReadLength(&size)) | 319 if (!m->ReadLength(iter, &size)) |
| 320 return false; | 320 return false; |
| 321 // Resizing beforehand is not safe, see BUG 1006367 for details. | 321 // Resizing beforehand is not safe, see BUG 1006367 for details. |
| 322 if (INT_MAX / sizeof(P) <= static_cast<size_t>(size)) | 322 if (INT_MAX / sizeof(P) <= static_cast<size_t>(size)) |
| 323 return false; | 323 return false; |
| 324 r->resize(size); | 324 r->resize(size); |
| 325 for (int i = 0; i < size; i++) { | 325 for (int i = 0; i < size; i++) { |
| 326 if (!ReadParam(m, iter, &(*r)[i])) | 326 if (!ReadParam(m, iter, &(*r)[i])) |
| 327 return false; | 327 return false; |
| 328 } | 328 } |
| 329 return true; | 329 return true; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 342 typedef std::set<P> param_type; | 342 typedef std::set<P> param_type; |
| 343 static void Write(Message* m, const param_type& p) { | 343 static void Write(Message* m, const param_type& p) { |
| 344 WriteParam(m, static_cast<int>(p.size())); | 344 WriteParam(m, static_cast<int>(p.size())); |
| 345 typename param_type::const_iterator iter; | 345 typename param_type::const_iterator iter; |
| 346 for (iter = p.begin(); iter != p.end(); ++iter) | 346 for (iter = p.begin(); iter != p.end(); ++iter) |
| 347 WriteParam(m, *iter); | 347 WriteParam(m, *iter); |
| 348 } | 348 } |
| 349 static bool Read(const Message* m, PickleIterator* iter, | 349 static bool Read(const Message* m, PickleIterator* iter, |
| 350 param_type* r) { | 350 param_type* r) { |
| 351 int size; | 351 int size; |
| 352 if (!iter->ReadLength(&size)) | 352 if (!m->ReadLength(iter, &size)) |
| 353 return false; | 353 return false; |
| 354 for (int i = 0; i < size; ++i) { | 354 for (int i = 0; i < size; ++i) { |
| 355 P item; | 355 P item; |
| 356 if (!ReadParam(m, iter, &item)) | 356 if (!ReadParam(m, iter, &item)) |
| 357 return false; | 357 return false; |
| 358 r->insert(item); | 358 r->insert(item); |
| 359 } | 359 } |
| 360 return true; | 360 return true; |
| 361 } | 361 } |
| 362 static void Log(const param_type& p, std::string* l) { | 362 static void Log(const param_type& p, std::string* l) { |
| (...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 644 template<class P> | 644 template<class P> |
| 645 struct ParamTraits<ScopedVector<P> > { | 645 struct ParamTraits<ScopedVector<P> > { |
| 646 typedef ScopedVector<P> param_type; | 646 typedef ScopedVector<P> param_type; |
| 647 static void Write(Message* m, const param_type& p) { | 647 static void Write(Message* m, const param_type& p) { |
| 648 WriteParam(m, static_cast<int>(p.size())); | 648 WriteParam(m, static_cast<int>(p.size())); |
| 649 for (size_t i = 0; i < p.size(); i++) | 649 for (size_t i = 0; i < p.size(); i++) |
| 650 WriteParam(m, *p[i]); | 650 WriteParam(m, *p[i]); |
| 651 } | 651 } |
| 652 static bool Read(const Message* m, PickleIterator* iter, param_type* r) { | 652 static bool Read(const Message* m, PickleIterator* iter, param_type* r) { |
| 653 int size = 0; | 653 int size = 0; |
| 654 if (!iter->ReadLength(&size)) | 654 if (!m->ReadLength(iter, &size)) |
| 655 return false; | 655 return false; |
| 656 if (INT_MAX/sizeof(P) <= static_cast<size_t>(size)) | 656 if (INT_MAX/sizeof(P) <= static_cast<size_t>(size)) |
| 657 return false; | 657 return false; |
| 658 r->resize(size); | 658 r->resize(size); |
| 659 for (int i = 0; i < size; i++) { | 659 for (int i = 0; i < size; i++) { |
| 660 (*r)[i] = new P(); | 660 (*r)[i] = new P(); |
| 661 if (!ReadParam(m, iter, (*r)[i])) | 661 if (!ReadParam(m, iter, (*r)[i])) |
| 662 return false; | 662 return false; |
| 663 } | 663 } |
| 664 return true; | 664 return true; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 683 static void Write(Message* m, const param_type& p) { | 683 static void Write(Message* m, const param_type& p) { |
| 684 WriteParam(m, static_cast<int>(p.size())); | 684 WriteParam(m, static_cast<int>(p.size())); |
| 685 typename param_type::const_iterator iter; | 685 typename param_type::const_iterator iter; |
| 686 for (iter = p.begin(); iter != p.end(); ++iter) { | 686 for (iter = p.begin(); iter != p.end(); ++iter) { |
| 687 WriteParam(m, iter->first); | 687 WriteParam(m, iter->first); |
| 688 WriteParam(m, iter->second); | 688 WriteParam(m, iter->second); |
| 689 } | 689 } |
| 690 } | 690 } |
| 691 static bool Read(const Message* m, PickleIterator* iter, param_type* r) { | 691 static bool Read(const Message* m, PickleIterator* iter, param_type* r) { |
| 692 int size; | 692 int size; |
| 693 if (!iter->ReadLength(&size)) | 693 if (!m->ReadLength(iter, &size)) |
| 694 return false; | 694 return false; |
| 695 for (int i = 0; i < size; ++i) { | 695 for (int i = 0; i < size; ++i) { |
| 696 K key; | 696 K key; |
| 697 if (!ReadParam(m, iter, &key)) | 697 if (!ReadParam(m, iter, &key)) |
| 698 return false; | 698 return false; |
| 699 V& value = (*r)[key]; | 699 V& value = (*r)[key]; |
| 700 if (!ReadParam(m, iter, &value)) | 700 if (!ReadParam(m, iter, &value)) |
| 701 return false; | 701 return false; |
| 702 } | 702 } |
| 703 return true; | 703 return true; |
| (...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 950 template<typename TA, typename TB, typename TC, typename TD, typename TE> | 950 template<typename TA, typename TB, typename TC, typename TD, typename TE> |
| 951 static void WriteReplyParams(Message* reply, TA a, TB b, TC c, TD d, TE e) { | 951 static void WriteReplyParams(Message* reply, TA a, TB b, TC c, TD d, TE e) { |
| 952 ReplyParam p(a, b, c, d, e); | 952 ReplyParam p(a, b, c, d, e); |
| 953 WriteParam(reply, p); | 953 WriteParam(reply, p); |
| 954 } | 954 } |
| 955 }; | 955 }; |
| 956 | 956 |
| 957 } // namespace IPC | 957 } // namespace IPC |
| 958 | 958 |
| 959 #endif // IPC_IPC_MESSAGE_UTILS_H_ | 959 #endif // IPC_IPC_MESSAGE_UTILS_H_ |
| OLD | NEW |