blob: 0168b150fc498a7754961f88109a06283555b77f [file] [log] [blame]
zijiehe2eda4da2017-07-19 05:39:50 +09001// 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;
zijiehe2eda4da2017-07-19 05:39:50 +090026 static void Write(base::Pickle* m, const param_type& p) {
27 WriteParam(m, p.size());
28 for (int i = 0; i < p.size(); i++)
29 WriteParam(m, p.Get(i));
30 }
31 static bool Read(const base::Pickle* m,
32 base::PickleIterator* iter,
33 param_type* r) {
34 int size;
35 // ReadLength() checks for < 0 itself.
36 if (!iter->ReadLength(&size))
37 return false;
38 // Avoid integer overflow / assertion failure in Reserve() function.
39 if (INT_MAX / sizeof(StorageType) <= static_cast<size_t>(size))
40 return false;
41 r->Reserve(size);
42 for (int i = 0; i < size; i++) {
43 if (!ReadParam(m, iter, r->Add()))
44 return false;
45 }
46 return true;
47 }
48 static void Log(const param_type& p, std::string* l) {
49 for (int i = 0; i < p.size(); ++i) {
50 if (i != 0)
51 l->append(" ");
52 LogParam(p.Get(i), l);
53 }
54 }
55};
56
57template <class P>
58struct ParamTraits<google::protobuf::RepeatedField<P>> :
59 RepeatedFieldParamTraits<google::protobuf::RepeatedField<P>, P> {};
60
61template <class P>
62struct ParamTraits<google::protobuf::RepeatedPtrField<P>> :
63 RepeatedFieldParamTraits<google::protobuf::RepeatedPtrField<P>, void*> {};
64
65} // namespace IPC
66
67#endif // IPC_IPC_MESSAGE_PROTOBUF_UTILS_H_