blob: 8cacfe8d933e2fb5528d54dda052dbf1feaf5072 [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
10#if defined(OS_NACL_NONSFI)
11static_assert(false,
12 "ipc_message_protobuf_utils is not able to work with "
13 "nacl_nonsfi configuration.");
14#endif
15
16#include "base/pickle.h"
17#include "ipc/ipc_param_traits.h"
18#include "ipc/ipc_message_utils.h"
19#include "third_party/protobuf/src/google/protobuf/repeated_field.h"
20
21namespace IPC {
22
23template <class RepeatedFieldLike, class StorageType>
24struct RepeatedFieldParamTraits {
25 typedef RepeatedFieldLike param_type;
26 static void GetSize(base::PickleSizer* sizer, const param_type& p) {
27 GetParamSize(sizer, p.size());
28 for (int i = 0; i < p.size(); i++)
29 GetParamSize(sizer, p.Get(i));
30 }
31 static void Write(base::Pickle* m, const param_type& p) {
32 WriteParam(m, p.size());
33 for (int i = 0; i < p.size(); i++)
34 WriteParam(m, p.Get(i));
35 }
36 static bool Read(const base::Pickle* m,
37 base::PickleIterator* iter,
38 param_type* r) {
39 int size;
40 // ReadLength() checks for < 0 itself.
41 if (!iter->ReadLength(&size))
42 return false;
43 // Avoid integer overflow / assertion failure in Reserve() function.
44 if (INT_MAX / sizeof(StorageType) <= static_cast<size_t>(size))
45 return false;
46 r->Reserve(size);
47 for (int i = 0; i < size; i++) {
48 if (!ReadParam(m, iter, r->Add()))
49 return false;
50 }
51 return true;
52 }
53 static void Log(const param_type& p, std::string* l) {
54 for (int i = 0; i < p.size(); ++i) {
55 if (i != 0)
56 l->append(" ");
57 LogParam(p.Get(i), l);
58 }
59 }
60};
61
62template <class P>
63struct ParamTraits<google::protobuf::RepeatedField<P>> :
64 RepeatedFieldParamTraits<google::protobuf::RepeatedField<P>, P> {};
65
66template <class P>
67struct ParamTraits<google::protobuf::RepeatedPtrField<P>> :
68 RepeatedFieldParamTraits<google::protobuf::RepeatedPtrField<P>, void*> {};
69
70} // namespace IPC
71
72#endif // IPC_IPC_MESSAGE_PROTOBUF_UTILS_H_