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