1*635a8641SAndroid Build Coastguard Worker // Copyright 2017 The Chromium Authors. All rights reserved. 2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be 3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file. 4*635a8641SAndroid Build Coastguard Worker 5*635a8641SAndroid Build Coastguard Worker #ifndef IPC_IPC_MESSAGE_PROTOBUF_UTILS_H_ 6*635a8641SAndroid Build Coastguard Worker #define IPC_IPC_MESSAGE_PROTOBUF_UTILS_H_ 7*635a8641SAndroid Build Coastguard Worker 8*635a8641SAndroid Build Coastguard Worker #include "build/build_config.h" 9*635a8641SAndroid Build Coastguard Worker 10*635a8641SAndroid Build Coastguard Worker #if defined(OS_NACL_NONSFI) 11*635a8641SAndroid Build Coastguard Worker static_assert(false, 12*635a8641SAndroid Build Coastguard Worker "ipc_message_protobuf_utils is not able to work with " 13*635a8641SAndroid Build Coastguard Worker "nacl_nonsfi configuration."); 14*635a8641SAndroid Build Coastguard Worker #endif 15*635a8641SAndroid Build Coastguard Worker 16*635a8641SAndroid Build Coastguard Worker #include "base/pickle.h" 17*635a8641SAndroid Build Coastguard Worker #include "ipc/ipc_param_traits.h" 18*635a8641SAndroid Build Coastguard Worker #include "ipc/ipc_message_utils.h" 19*635a8641SAndroid Build Coastguard Worker #include "third_party/protobuf/src/google/protobuf/repeated_field.h" 20*635a8641SAndroid Build Coastguard Worker 21*635a8641SAndroid Build Coastguard Worker namespace IPC { 22*635a8641SAndroid Build Coastguard Worker 23*635a8641SAndroid Build Coastguard Worker template <class RepeatedFieldLike, class StorageType> 24*635a8641SAndroid Build Coastguard Worker struct RepeatedFieldParamTraits { 25*635a8641SAndroid Build Coastguard Worker typedef RepeatedFieldLike param_type; WriteRepeatedFieldParamTraits26*635a8641SAndroid Build Coastguard Worker static void Write(base::Pickle* m, const param_type& p) { 27*635a8641SAndroid Build Coastguard Worker WriteParam(m, p.size()); 28*635a8641SAndroid Build Coastguard Worker for (int i = 0; i < p.size(); i++) 29*635a8641SAndroid Build Coastguard Worker WriteParam(m, p.Get(i)); 30*635a8641SAndroid Build Coastguard Worker } ReadRepeatedFieldParamTraits31*635a8641SAndroid Build Coastguard Worker static bool Read(const base::Pickle* m, 32*635a8641SAndroid Build Coastguard Worker base::PickleIterator* iter, 33*635a8641SAndroid Build Coastguard Worker param_type* r) { 34*635a8641SAndroid Build Coastguard Worker int size; 35*635a8641SAndroid Build Coastguard Worker // ReadLength() checks for < 0 itself. 36*635a8641SAndroid Build Coastguard Worker if (!iter->ReadLength(&size)) 37*635a8641SAndroid Build Coastguard Worker return false; 38*635a8641SAndroid Build Coastguard Worker // Avoid integer overflow / assertion failure in Reserve() function. 39*635a8641SAndroid Build Coastguard Worker if (INT_MAX / sizeof(StorageType) <= static_cast<size_t>(size)) 40*635a8641SAndroid Build Coastguard Worker return false; 41*635a8641SAndroid Build Coastguard Worker r->Reserve(size); 42*635a8641SAndroid Build Coastguard Worker for (int i = 0; i < size; i++) { 43*635a8641SAndroid Build Coastguard Worker if (!ReadParam(m, iter, r->Add())) 44*635a8641SAndroid Build Coastguard Worker return false; 45*635a8641SAndroid Build Coastguard Worker } 46*635a8641SAndroid Build Coastguard Worker return true; 47*635a8641SAndroid Build Coastguard Worker } LogRepeatedFieldParamTraits48*635a8641SAndroid Build Coastguard Worker static void Log(const param_type& p, std::string* l) { 49*635a8641SAndroid Build Coastguard Worker for (int i = 0; i < p.size(); ++i) { 50*635a8641SAndroid Build Coastguard Worker if (i != 0) 51*635a8641SAndroid Build Coastguard Worker l->append(" "); 52*635a8641SAndroid Build Coastguard Worker LogParam(p.Get(i), l); 53*635a8641SAndroid Build Coastguard Worker } 54*635a8641SAndroid Build Coastguard Worker } 55*635a8641SAndroid Build Coastguard Worker }; 56*635a8641SAndroid Build Coastguard Worker 57*635a8641SAndroid Build Coastguard Worker template <class P> 58*635a8641SAndroid Build Coastguard Worker struct ParamTraits<google::protobuf::RepeatedField<P>> : 59*635a8641SAndroid Build Coastguard Worker RepeatedFieldParamTraits<google::protobuf::RepeatedField<P>, P> {}; 60*635a8641SAndroid Build Coastguard Worker 61*635a8641SAndroid Build Coastguard Worker template <class P> 62*635a8641SAndroid Build Coastguard Worker struct ParamTraits<google::protobuf::RepeatedPtrField<P>> : 63*635a8641SAndroid Build Coastguard Worker RepeatedFieldParamTraits<google::protobuf::RepeatedPtrField<P>, void*> {}; 64*635a8641SAndroid Build Coastguard Worker 65*635a8641SAndroid Build Coastguard Worker } // namespace IPC 66*635a8641SAndroid Build Coastguard Worker 67*635a8641SAndroid Build Coastguard Worker #endif // IPC_IPC_MESSAGE_PROTOBUF_UTILS_H_ 68