ipc_message_protobuf_utils.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright 2017 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef IPC_IPC_MESSAGE_PROTOBUF_UTILS_H_
  5. #define IPC_IPC_MESSAGE_PROTOBUF_UTILS_H_
  6. #include "build/build_config.h"
  7. #include "base/pickle.h"
  8. #include "ipc/ipc_param_traits.h"
  9. #include "ipc/ipc_message_utils.h"
  10. #include "third_party/protobuf/src/google/protobuf/repeated_field.h"
  11. namespace IPC {
  12. template <class RepeatedFieldLike, class StorageType>
  13. struct RepeatedFieldParamTraits {
  14. typedef RepeatedFieldLike param_type;
  15. static void Write(base::Pickle* m, const param_type& p) {
  16. WriteParam(m, p.size());
  17. for (int i = 0; i < p.size(); i++)
  18. WriteParam(m, p.Get(i));
  19. }
  20. static bool Read(const base::Pickle* m,
  21. base::PickleIterator* iter,
  22. param_type* r) {
  23. size_t size;
  24. if (!iter->ReadLength(&size))
  25. return false;
  26. // Avoid integer overflow / assertion failure in Reserve() function.
  27. if (size > INT_MAX / sizeof(StorageType))
  28. return false;
  29. r->Reserve(size);
  30. for (size_t i = 0; i < size; i++) {
  31. if (!ReadParam(m, iter, r->Add()))
  32. return false;
  33. }
  34. return true;
  35. }
  36. static void Log(const param_type& p, std::string* l) {
  37. for (int i = 0; i < p.size(); ++i) {
  38. if (i != 0)
  39. l->append(" ");
  40. LogParam(p.Get(i), l);
  41. }
  42. }
  43. };
  44. template <class P>
  45. struct ParamTraits<google::protobuf::RepeatedField<P>> :
  46. RepeatedFieldParamTraits<google::protobuf::RepeatedField<P>, P> {};
  47. template <class P>
  48. struct ParamTraits<google::protobuf::RepeatedPtrField<P>> :
  49. RepeatedFieldParamTraits<google::protobuf::RepeatedPtrField<P>, void*> {};
  50. } // namespace IPC
  51. #endif // IPC_IPC_MESSAGE_PROTOBUF_UTILS_H_