| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef IPC_IPC_MESSAGE_UTILS_H_ | 5 #ifndef IPC_IPC_MESSAGE_UTILS_H_ |
| 6 #define IPC_IPC_MESSAGE_UTILS_H_ | 6 #define IPC_IPC_MESSAGE_UTILS_H_ |
| 7 | 7 |
| 8 #include <limits.h> | 8 #include <limits.h> |
| 9 #include <stddef.h> | 9 #include <stddef.h> |
| 10 #include <stdint.h> | 10 #include <stdint.h> |
| 11 | 11 |
| 12 #include <algorithm> | 12 #include <algorithm> |
| 13 #include <map> | 13 #include <map> |
| 14 #include <memory> | 14 #include <memory> |
| 15 #include <set> | 15 #include <set> |
| 16 #include <string> | 16 #include <string> |
| 17 #include <tuple> | 17 #include <tuple> |
| 18 #include <vector> | 18 #include <vector> |
| 19 | 19 |
| 20 #include "base/containers/flat_map.h" |
| 20 #include "base/containers/small_map.h" | 21 #include "base/containers/small_map.h" |
| 21 #include "base/containers/stack_container.h" | 22 #include "base/containers/stack_container.h" |
| 22 #include "base/files/file.h" | 23 #include "base/files/file.h" |
| 23 #include "base/format_macros.h" | 24 #include "base/format_macros.h" |
| 25 #include "base/numerics/safe_conversions.h" |
| 24 #include "base/optional.h" | 26 #include "base/optional.h" |
| 25 #include "base/strings/string16.h" | 27 #include "base/strings/string16.h" |
| 26 #include "base/strings/string_util.h" | 28 #include "base/strings/string_util.h" |
| 27 #include "base/strings/stringprintf.h" | 29 #include "base/strings/stringprintf.h" |
| 28 #include "build/build_config.h" | 30 #include "build/build_config.h" |
| 29 #include "ipc/ipc_message_start.h" | 31 #include "ipc/ipc_message_start.h" |
| 30 #include "ipc/ipc_param_traits.h" | 32 #include "ipc/ipc_param_traits.h" |
| 31 #include "ipc/ipc_sync_message.h" | 33 #include "ipc/ipc_sync_message.h" |
| 32 | 34 |
| 33 namespace base { | 35 namespace base { |
| (...skipping 858 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 892 l->append(" "); | 894 l->append(" "); |
| 893 LogParam((p[i]), l); | 895 LogParam((p[i]), l); |
| 894 } | 896 } |
| 895 } | 897 } |
| 896 }; | 898 }; |
| 897 | 899 |
| 898 template <typename NormalMap, | 900 template <typename NormalMap, |
| 899 int kArraySize, | 901 int kArraySize, |
| 900 typename EqualKey, | 902 typename EqualKey, |
| 901 typename MapInit> | 903 typename MapInit> |
| 902 struct ParamTraits<base::SmallMap<NormalMap, kArraySize, EqualKey, MapInit> > { | 904 struct ParamTraits<base::small_map<NormalMap, kArraySize, EqualKey, MapInit>> { |
| 903 typedef base::SmallMap<NormalMap, kArraySize, EqualKey, MapInit> param_type; | 905 using param_type = base::small_map<NormalMap, kArraySize, EqualKey, MapInit>; |
| 904 typedef typename param_type::key_type K; | 906 using K = typename param_type::key_type; |
| 905 typedef typename param_type::data_type V; | 907 using V = typename param_type::data_type; |
| 906 static void GetSize(base::PickleSizer* sizer, const param_type& p) { | 908 static void GetSize(base::PickleSizer* sizer, const param_type& p) { |
| 907 GetParamSize(sizer, static_cast<int>(p.size())); | 909 GetParamSize(sizer, static_cast<int>(p.size())); |
| 908 typename param_type::const_iterator iter; | 910 typename param_type::const_iterator iter; |
| 909 for (iter = p.begin(); iter != p.end(); ++iter) { | 911 for (iter = p.begin(); iter != p.end(); ++iter) { |
| 910 GetParamSize(sizer, iter->first); | 912 GetParamSize(sizer, iter->first); |
| 911 GetParamSize(sizer, iter->second); | 913 GetParamSize(sizer, iter->second); |
| 912 } | 914 } |
| 913 } | 915 } |
| 914 static void Write(base::Pickle* m, const param_type& p) { | 916 static void Write(base::Pickle* m, const param_type& p) { |
| 915 WriteParam(m, static_cast<int>(p.size())); | 917 WriteParam(m, static_cast<int>(p.size())); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 929 K key; | 931 K key; |
| 930 if (!ReadParam(m, iter, &key)) | 932 if (!ReadParam(m, iter, &key)) |
| 931 return false; | 933 return false; |
| 932 V& value = (*r)[key]; | 934 V& value = (*r)[key]; |
| 933 if (!ReadParam(m, iter, &value)) | 935 if (!ReadParam(m, iter, &value)) |
| 934 return false; | 936 return false; |
| 935 } | 937 } |
| 936 return true; | 938 return true; |
| 937 } | 939 } |
| 938 static void Log(const param_type& p, std::string* l) { | 940 static void Log(const param_type& p, std::string* l) { |
| 939 l->append("<base::SmallMap>"); | 941 l->append("<base::small_map>"); |
| 940 } | 942 } |
| 941 }; | 943 }; |
| 942 | 944 |
| 945 template <class Key, class Mapped, class Compare> |
| 946 struct ParamTraits<base::flat_map<Key, Mapped, Compare>> { |
| 947 using param_type = base::flat_map<Key, Mapped, Compare>; |
| 948 static void GetSize(base::PickleSizer* sizer, const param_type& p) { |
| 949 DCHECK(base::IsValueInRangeForNumericType<int>(p.size())); |
| 950 GetParamSize(sizer, static_cast<int>(p.size())); |
| 951 for (const auto& iter : p) { |
| 952 GetParamSize(sizer, iter.first); |
| 953 GetParamSize(sizer, iter.second); |
| 954 } |
| 955 } |
| 956 static void Write(base::Pickle* m, const param_type& p) { |
| 957 DCHECK(base::IsValueInRangeForNumericType<int>(p.size())); |
| 958 WriteParam(m, static_cast<int>(p.size())); |
| 959 for (const auto& iter : p) { |
| 960 WriteParam(m, iter.first); |
| 961 WriteParam(m, iter.second); |
| 962 } |
| 963 } |
| 964 static bool Read(const base::Pickle* m, |
| 965 base::PickleIterator* iter, |
| 966 param_type* r) { |
| 967 int size; |
| 968 if (!iter->ReadLength(&size)) |
| 969 return false; |
| 970 |
| 971 // Construct by creating in a vector and moving into the flat_map. Properly |
| 972 // serialized flat_maps will be in-order so this will be O(n). Incorrectly |
| 973 // serialized ones will still be handled properly. |
| 974 std::vector<typename param_type::value_type> vect; |
| 975 vect.resize(size); |
| 976 for (int i = 0; i < size; ++i) { |
| 977 if (!ReadParam(m, iter, &vect[i].first)) |
| 978 return false; |
| 979 if (!ReadParam(m, iter, &vect[i].second)) |
| 980 return false; |
| 981 } |
| 982 |
| 983 *r = param_type(std::move(vect), base::KEEP_FIRST_OF_DUPES); |
| 984 return true; |
| 985 } |
| 986 static void Log(const param_type& p, std::string* l) { |
| 987 l->append("<base::flat_map>"); |
| 988 } |
| 989 }; |
| 990 |
| 943 template <class P> | 991 template <class P> |
| 944 struct ParamTraits<std::unique_ptr<P>> { | 992 struct ParamTraits<std::unique_ptr<P>> { |
| 945 typedef std::unique_ptr<P> param_type; | 993 typedef std::unique_ptr<P> param_type; |
| 946 static void GetSize(base::PickleSizer* sizer, const param_type& p) { | 994 static void GetSize(base::PickleSizer* sizer, const param_type& p) { |
| 947 bool valid = !!p; | 995 bool valid = !!p; |
| 948 GetParamSize(sizer, valid); | 996 GetParamSize(sizer, valid); |
| 949 if (valid) | 997 if (valid) |
| 950 GetParamSize(sizer, *p); | 998 GetParamSize(sizer, *p); |
| 951 } | 999 } |
| 952 static void Write(base::Pickle* m, const param_type& p) { | 1000 static void Write(base::Pickle* m, const param_type& p) { |
| (...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1136 template <class ReplyParamType> | 1184 template <class ReplyParamType> |
| 1137 inline void LogReplyParamsToMessage(const ReplyParamType& reply_params, | 1185 inline void LogReplyParamsToMessage(const ReplyParamType& reply_params, |
| 1138 const Message* msg) {} | 1186 const Message* msg) {} |
| 1139 | 1187 |
| 1140 inline void ConnectMessageAndReply(const Message* msg, Message* reply) {} | 1188 inline void ConnectMessageAndReply(const Message* msg, Message* reply) {} |
| 1141 #endif | 1189 #endif |
| 1142 | 1190 |
| 1143 } // namespace IPC | 1191 } // namespace IPC |
| 1144 | 1192 |
| 1145 #endif // IPC_IPC_MESSAGE_UTILS_H_ | 1193 #endif // IPC_IPC_MESSAGE_UTILS_H_ |
| OLD | NEW |