blob: f06a176f2c370da383387581ef4a342da842771a [file] [log] [blame]
zijiehe84eef4fa2017-07-18 20:39:501// 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
5#ifndef IPC_IPC_MESSAGE_PROTOBUF_UTILS_H_
6#define IPC_IPC_MESSAGE_PROTOBUF_UTILS_H_
7
8#include "build/build_config.h"
9
zijiehe84eef4fa2017-07-18 20:39:5010#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
15namespace IPC {
16
17template <class RepeatedFieldLike, class StorageType>
18struct RepeatedFieldParamTraits {
19 typedef RepeatedFieldLike param_type;
zijiehe84eef4fa2017-07-18 20:39:5020 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) {
28 int size;
29 // ReadLength() checks for < 0 itself.
30 if (!iter->ReadLength(&size))
31 return false;
32 // Avoid integer overflow / assertion failure in Reserve() function.
33 if (INT_MAX / sizeof(StorageType) <= static_cast<size_t>(size))
34 return false;
35 r->Reserve(size);
36 for (int i = 0; i < size; i++) {
37 if (!ReadParam(m, iter, r->Add()))
38 return false;
39 }
40 return true;
41 }
42 static void Log(const param_type& p, std::string* l) {
43 for (int i = 0; i < p.size(); ++i) {
44 if (i != 0)
45 l->append(" ");
46 LogParam(p.Get(i), l);
47 }
48 }
49};
50
51template <class P>
52struct ParamTraits<google::protobuf::RepeatedField<P>> :
53 RepeatedFieldParamTraits<google::protobuf::RepeatedField<P>, P> {};
54
55template <class P>
56struct ParamTraits<google::protobuf::RepeatedPtrField<P>> :
57 RepeatedFieldParamTraits<google::protobuf::RepeatedPtrField<P>, void*> {};
58
59} // namespace IPC
60
61#endif // IPC_IPC_MESSAGE_PROTOBUF_UTILS_H_