• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
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)
11 static_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 
21 namespace IPC {
22 
23 template <class RepeatedFieldLike, class StorageType>
24 struct RepeatedFieldParamTraits {
25   typedef RepeatedFieldLike param_type;
WriteRepeatedFieldParamTraits26   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   }
ReadRepeatedFieldParamTraits31   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   }
LogRepeatedFieldParamTraits48   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 
57 template <class P>
58 struct ParamTraits<google::protobuf::RepeatedField<P>> :
59     RepeatedFieldParamTraits<google::protobuf::RepeatedField<P>, P> {};
60 
61 template <class P>
62 struct 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_
68