[email protected] | efb5f57 | 2012-01-29 10:57:33 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 5 | #ifndef IPC_IPC_MESSAGE_UTILS_H_ |
| 6 | #define IPC_IPC_MESSAGE_UTILS_H_ |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 7 | |
avi | 51ba3e69 | 2015-12-26 17:30:50 | [diff] [blame] | 8 | #include <limits.h> |
avi | 246998d8 | 2015-12-22 02:39:04 | [diff] [blame] | 9 | #include <stddef.h> |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 10 | #include <stdint.h> |
| 11 | |
[email protected] | 379e7a5 | 2010-03-09 00:38:41 | [diff] [blame] | 12 | #include <algorithm> |
Yao Xiao | af79ca9b | 2019-03-12 19:54:18 | [diff] [blame] | 13 | #include <bitset> |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 14 | #include <map> |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 15 | #include <memory> |
[email protected] | 96da696 | 2010-05-13 19:10:34 | [diff] [blame] | 16 | #include <set> |
[email protected] | 663bd9e | 2011-03-21 01:07:01 | [diff] [blame] | 17 | #include <string> |
tzik | 55e3e4d | 2016-03-08 05:47:44 | [diff] [blame] | 18 | #include <tuple> |
Stuart Langley | 4830039 | 2017-11-02 03:54:44 | [diff] [blame] | 19 | #include <unordered_map> |
[email protected] | 663bd9e | 2011-03-21 01:07:01 | [diff] [blame] | 20 | #include <vector> |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 21 | |
Ken Rockot | 3044d21 | 2018-01-23 02:44:39 | [diff] [blame] | 22 | #include "base/component_export.h" |
brettw | ca2ec763 | 2017-04-20 06:10:20 | [diff] [blame] | 23 | #include "base/containers/flat_map.h" |
[email protected] | 205294b | 2014-03-18 20:48:35 | [diff] [blame] | 24 | #include "base/containers/small_map.h" |
miletus | 1edf97f9 | 2015-07-23 19:42:36 | [diff] [blame] | 25 | #include "base/containers/stack_container.h" |
[email protected] | 141bcc5 | 2014-01-27 21:36:00 | [diff] [blame] | 26 | #include "base/files/file.h" |
[email protected] | dce5df5 | 2009-06-29 17:58:25 | [diff] [blame] | 27 | #include "base/format_macros.h" |
Alexandr Ilin | d497eee | 2018-04-19 22:50:54 | [diff] [blame] | 28 | #include "base/memory/platform_shared_memory_region.h" |
| 29 | #include "base/memory/read_only_shared_memory_region.h" |
Klaus Weidner | 3824a888 | 2017-11-03 06:24:57 | [diff] [blame] | 30 | #include "base/memory/shared_memory_handle.h" |
Alexandr Ilin | d497eee | 2018-04-19 22:50:54 | [diff] [blame] | 31 | #include "base/memory/unsafe_shared_memory_region.h" |
| 32 | #include "base/memory/writable_shared_memory_region.h" |
brettw | ca2ec763 | 2017-04-20 06:10:20 | [diff] [blame] | 33 | #include "base/numerics/safe_conversions.h" |
bmcquade | 18573e1 | 2016-07-01 13:13:50 | [diff] [blame] | 34 | #include "base/optional.h" |
[email protected] | 4aa794a1 | 2013-06-11 06:32:18 | [diff] [blame] | 35 | #include "base/strings/string16.h" |
| 36 | #include "base/strings/string_util.h" |
| 37 | #include "base/strings/stringprintf.h" |
Maciej Pawlowski | a37782a0 | 2019-05-16 06:40:28 | [diff] [blame] | 38 | #include "base/util/type_safety/id_type.h" |
avi | 246998d8 | 2015-12-22 02:39:04 | [diff] [blame] | 39 | #include "build/build_config.h" |
[email protected] | f950981 | 2012-10-23 23:03:35 | [diff] [blame] | 40 | #include "ipc/ipc_message_start.h" |
[email protected] | 939856a | 2010-08-24 20:29:02 | [diff] [blame] | 41 | #include "ipc/ipc_param_traits.h" |
[email protected] | 0cfe5dae | 2010-08-17 00:24:54 | [diff] [blame] | 42 | #include "ipc/ipc_sync_message.h" |
[email protected] | 55b4e21 | 2010-08-13 20:43:58 | [diff] [blame] | 43 | |
Ken Rockot | 097248f0 | 2018-04-23 16:23:34 | [diff] [blame] | 44 | #if defined(OS_ANDROID) |
| 45 | #include "base/android/scoped_hardware_buffer_handle.h" |
| 46 | #endif |
| 47 | |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 48 | namespace base { |
[email protected] | f3a1c64 | 2011-07-12 19:15:03 | [diff] [blame] | 49 | class DictionaryValue; |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 50 | class FilePath; |
[email protected] | f3a1c64 | 2011-07-12 19:15:03 | [diff] [blame] | 51 | class ListValue; |
[email protected] | 0238a16 | 2013-06-13 13:47:46 | [diff] [blame] | 52 | class NullableString16; |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 53 | class Time; |
[email protected] | d84e48b | 2010-10-21 22:04:52 | [diff] [blame] | 54 | class TimeDelta; |
[email protected] | 1d14f58 | 2011-09-02 20:42:04 | [diff] [blame] | 55 | class TimeTicks; |
tguilbert | 4a5ac60 | 2016-09-19 21:11:25 | [diff] [blame] | 56 | class UnguessableToken; |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 57 | struct FileDescriptor; |
| 58 | } |
| 59 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 60 | namespace IPC { |
| 61 | |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 62 | struct ChannelHandle; |
| 63 | |
erikchen | d804e105 | 2017-04-29 02:24:36 | [diff] [blame] | 64 | #if defined(OS_WIN) |
| 65 | class PlatformFileForTransit; |
| 66 | #endif |
| 67 | |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 68 | // ----------------------------------------------------------------------------- |
| 69 | // How we send IPC message logs across channels. |
Ken Rockot | 3044d21 | 2018-01-23 02:44:39 | [diff] [blame] | 70 | struct COMPONENT_EXPORT(IPC) LogData { |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 71 | LogData(); |
vmpstr | bf0d713a | 2016-03-24 20:22:54 | [diff] [blame] | 72 | LogData(const LogData& other); |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 73 | ~LogData(); |
| 74 | |
| 75 | std::string channel; |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 76 | int32_t routing_id; |
| 77 | uint32_t type; // "User-defined" message type, from ipc_message.h. |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 78 | std::string flags; |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 79 | int64_t sent; // Time that the message was sent (i.e. at Send()). |
| 80 | int64_t receive; // Time before it was dispatched (i.e. before calling |
| 81 | // OnMessageReceived). |
| 82 | int64_t dispatch; // Time after it was dispatched (i.e. after calling |
| 83 | // OnMessageReceived). |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 84 | std::string message_name; |
| 85 | std::string params; |
| 86 | }; |
| 87 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 88 | //----------------------------------------------------------------------------- |
[email protected] | 7edae3d0 | 2012-12-17 20:23:47 | [diff] [blame] | 89 | |
[email protected] | 6476c72c | 2011-02-11 18:46:19 | [diff] [blame] | 90 | // A dummy struct to place first just to allow leading commas for all |
| 91 | // members in the macro-generated constructor initializer lists. |
| 92 | struct NoParams { |
| 93 | }; |
| 94 | |
dskiba | 2bc8946 | 2016-04-06 15:51:06 | [diff] [blame] | 95 | // Specializations are checked by 'IPC checker' part of find-bad-constructs |
| 96 | // Clang plugin (see WriteParam() below for the details). |
| 97 | template <typename... Ts> |
| 98 | struct CheckedTuple { |
| 99 | typedef std::tuple<Ts...> Tuple; |
| 100 | }; |
| 101 | |
dskiba | 2bc8946 | 2016-04-06 15:51:06 | [diff] [blame] | 102 | // This function is checked by 'IPC checker' part of find-bad-constructs |
| 103 | // Clang plugin to make it's not called on the following types: |
| 104 | // 1. long / unsigned long (but not typedefs to) |
| 105 | // 2. intmax_t, uintmax_t, intptr_t, uintptr_t, wint_t, |
| 106 | // size_t, rsize_t, ssize_t, ptrdiff_t, dev_t, off_t, clock_t, |
| 107 | // time_t, suseconds_t (including typedefs to) |
| 108 | // 3. Any template referencing types above (e.g. std::vector<size_t>) |
rockot | 0457af10 | 2016-02-05 02:12:32 | [diff] [blame] | 109 | template <class P> |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 110 | static inline void WriteParam(base::Pickle* m, const P& p) { |
[email protected] | 7b291f9 | 2009-08-14 05:43:53 | [diff] [blame] | 111 | typedef typename SimilarTypeTraits<P>::Type Type; |
[email protected] | 46ce5b56 | 2010-06-16 18:39:53 | [diff] [blame] | 112 | ParamTraits<Type>::Write(m, static_cast<const Type& >(p)); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | template <class P> |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 116 | static inline bool WARN_UNUSED_RESULT ReadParam(const base::Pickle* m, |
| 117 | base::PickleIterator* iter, |
| 118 | P* p) { |
[email protected] | 7b291f9 | 2009-08-14 05:43:53 | [diff] [blame] | 119 | typedef typename SimilarTypeTraits<P>::Type Type; |
| 120 | return ParamTraits<Type>::Read(m, iter, reinterpret_cast<Type* >(p)); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | template <class P> |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 124 | static inline void LogParam(const P& p, std::string* l) { |
[email protected] | 7b291f9 | 2009-08-14 05:43:53 | [diff] [blame] | 125 | typedef typename SimilarTypeTraits<P>::Type Type; |
[email protected] | 46ce5b56 | 2010-06-16 18:39:53 | [diff] [blame] | 126 | ParamTraits<Type>::Log(static_cast<const Type& >(p), l); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 127 | } |
| 128 | |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 129 | // Primitive ParamTraits ------------------------------------------------------- |
| 130 | |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 131 | template <> |
| 132 | struct ParamTraits<bool> { |
| 133 | typedef bool param_type; |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 134 | static void Write(base::Pickle* m, const param_type& p) { m->WriteBool(p); } |
| 135 | static bool Read(const base::Pickle* m, |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 136 | base::PickleIterator* iter, |
brettw | 05cfd8ddb | 2015-06-02 07:02:47 | [diff] [blame] | 137 | param_type* r) { |
avi | 48fc13b | 2014-12-28 23:31:48 | [diff] [blame] | 138 | return iter->ReadBool(r); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 139 | } |
Ken Rockot | 3044d21 | 2018-01-23 02:44:39 | [diff] [blame] | 140 | COMPONENT_EXPORT(IPC) static void Log(const param_type& p, std::string* l); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 141 | }; |
| 142 | |
| 143 | template <> |
Ken Rockot | 3044d21 | 2018-01-23 02:44:39 | [diff] [blame] | 144 | struct COMPONENT_EXPORT(IPC) ParamTraits<signed char> { |
ortuno | 19ecf184 | 2015-10-30 00:46:20 | [diff] [blame] | 145 | typedef signed char param_type; |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 146 | static void Write(base::Pickle* m, const param_type& p); |
| 147 | static bool Read(const base::Pickle* m, |
| 148 | base::PickleIterator* iter, |
| 149 | param_type* r); |
ortuno | 19ecf184 | 2015-10-30 00:46:20 | [diff] [blame] | 150 | static void Log(const param_type& p, std::string* l); |
| 151 | }; |
| 152 | |
| 153 | template <> |
Ken Rockot | 3044d21 | 2018-01-23 02:44:39 | [diff] [blame] | 154 | struct COMPONENT_EXPORT(IPC) ParamTraits<unsigned char> { |
[email protected] | 7bea735 | 2013-07-13 04:42:10 | [diff] [blame] | 155 | typedef unsigned char param_type; |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 156 | static void Write(base::Pickle* m, const param_type& p); |
| 157 | static bool Read(const base::Pickle* m, |
| 158 | base::PickleIterator* iter, |
| 159 | param_type* r); |
[email protected] | c1ee48d | 2013-07-12 23:12:28 | [diff] [blame] | 160 | static void Log(const param_type& p, std::string* l); |
| 161 | }; |
| 162 | |
| 163 | template <> |
Ken Rockot | 3044d21 | 2018-01-23 02:44:39 | [diff] [blame] | 164 | struct COMPONENT_EXPORT(IPC) ParamTraits<unsigned short> { |
[email protected] | c1ee48d | 2013-07-12 23:12:28 | [diff] [blame] | 165 | typedef unsigned short param_type; |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 166 | static void Write(base::Pickle* m, const param_type& p); |
| 167 | static bool Read(const base::Pickle* m, |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 168 | base::PickleIterator* iter, |
| 169 | param_type* r); |
[email protected] | c1ee48d | 2013-07-12 23:12:28 | [diff] [blame] | 170 | static void Log(const param_type& p, std::string* l); |
| 171 | }; |
| 172 | |
| 173 | template <> |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 174 | struct ParamTraits<int> { |
| 175 | typedef int param_type; |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 176 | static void Write(base::Pickle* m, const param_type& p) { m->WriteInt(p); } |
| 177 | static bool Read(const base::Pickle* m, |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 178 | base::PickleIterator* iter, |
brettw | 05cfd8ddb | 2015-06-02 07:02:47 | [diff] [blame] | 179 | param_type* r) { |
avi | 48fc13b | 2014-12-28 23:31:48 | [diff] [blame] | 180 | return iter->ReadInt(r); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 181 | } |
Ken Rockot | 3044d21 | 2018-01-23 02:44:39 | [diff] [blame] | 182 | COMPONENT_EXPORT(IPC) static void Log(const param_type& p, std::string* l); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 183 | }; |
| 184 | |
| 185 | template <> |
[email protected] | 63263f9 | 2009-07-28 19:35:08 | [diff] [blame] | 186 | struct ParamTraits<unsigned int> { |
| 187 | typedef unsigned int param_type; |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 188 | static void Write(base::Pickle* m, const param_type& p) { m->WriteInt(p); } |
| 189 | static bool Read(const base::Pickle* m, |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 190 | base::PickleIterator* iter, |
brettw | 05cfd8ddb | 2015-06-02 07:02:47 | [diff] [blame] | 191 | param_type* r) { |
avi | 48fc13b | 2014-12-28 23:31:48 | [diff] [blame] | 192 | return iter->ReadInt(reinterpret_cast<int*>(r)); |
[email protected] | 63263f9 | 2009-07-28 19:35:08 | [diff] [blame] | 193 | } |
Ken Rockot | 3044d21 | 2018-01-23 02:44:39 | [diff] [blame] | 194 | COMPONENT_EXPORT(IPC) static void Log(const param_type& p, std::string* l); |
[email protected] | 63263f9 | 2009-07-28 19:35:08 | [diff] [blame] | 195 | }; |
| 196 | |
jam | 03d8a78 | 2016-02-10 20:13:39 | [diff] [blame] | 197 | // long isn't safe to send over IPC because it's 4 bytes on 32 bit builds but |
| 198 | // 8 bytes on 64 bit builds. So if a 32 bit and 64 bit process have a channel |
| 199 | // that would cause problem. |
| 200 | // We need to keep this on for a few configs: |
| 201 | // 1) Windows because DWORD is typedef'd to it, which is fine because we have |
| 202 | // very few IPCs that cross this boundary. |
| 203 | // 2) We also need to keep it for Linux for two reasons: int64_t is typedef'd |
| 204 | // to long, and gfx::PluginWindow is long and is used in one GPU IPC. |
Scott Graham | f4b38b4 | 2017-06-19 23:09:33 | [diff] [blame] | 205 | // 3) Android 64 bit and Fuchsia also have int64_t typedef'd to long. |
jam | 03d8a78 | 2016-02-10 20:13:39 | [diff] [blame] | 206 | // Since we want to support Android 32<>64 bit IPC, as long as we don't have |
| 207 | // these traits for 32 bit ARM then that'll catch any errors. |
Scott Graham | f4b38b4 | 2017-06-19 23:09:33 | [diff] [blame] | 208 | #if defined(OS_WIN) || defined(OS_LINUX) || defined(OS_FUCHSIA) || \ |
jam | ac78d7d8 | 2016-02-11 00:50:28 | [diff] [blame] | 209 | (defined(OS_ANDROID) && defined(ARCH_CPU_64_BITS)) |
[email protected] | 63263f9 | 2009-07-28 19:35:08 | [diff] [blame] | 210 | template <> |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 211 | struct ParamTraits<long> { |
| 212 | typedef long param_type; |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 213 | static void Write(base::Pickle* m, const param_type& p) { |
jam | 03d8a78 | 2016-02-10 20:13:39 | [diff] [blame] | 214 | m->WriteLong(p); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 215 | } |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 216 | static bool Read(const base::Pickle* m, |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 217 | base::PickleIterator* iter, |
brettw | 05cfd8ddb | 2015-06-02 07:02:47 | [diff] [blame] | 218 | param_type* r) { |
avi | 48fc13b | 2014-12-28 23:31:48 | [diff] [blame] | 219 | return iter->ReadLong(r); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 220 | } |
Ken Rockot | 3044d21 | 2018-01-23 02:44:39 | [diff] [blame] | 221 | COMPONENT_EXPORT(IPC) static void Log(const param_type& p, std::string* l); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 222 | }; |
| 223 | |
[email protected] | 140c303 | 2009-06-26 18:22:54 | [diff] [blame] | 224 | template <> |
| 225 | struct ParamTraits<unsigned long> { |
| 226 | typedef unsigned long param_type; |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 227 | static void Write(base::Pickle* m, const param_type& p) { |
jam | 03d8a78 | 2016-02-10 20:13:39 | [diff] [blame] | 228 | m->WriteLong(p); |
[email protected] | 140c303 | 2009-06-26 18:22:54 | [diff] [blame] | 229 | } |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 230 | static bool Read(const base::Pickle* m, |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 231 | base::PickleIterator* iter, |
brettw | 05cfd8ddb | 2015-06-02 07:02:47 | [diff] [blame] | 232 | param_type* r) { |
avi | 48fc13b | 2014-12-28 23:31:48 | [diff] [blame] | 233 | return iter->ReadLong(reinterpret_cast<long*>(r)); |
[email protected] | 140c303 | 2009-06-26 18:22:54 | [diff] [blame] | 234 | } |
Ken Rockot | 3044d21 | 2018-01-23 02:44:39 | [diff] [blame] | 235 | COMPONENT_EXPORT(IPC) static void Log(const param_type& p, std::string* l); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 236 | }; |
jam | 03d8a78 | 2016-02-10 20:13:39 | [diff] [blame] | 237 | #endif |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 238 | |
| 239 | template <> |
[email protected] | 63263f9 | 2009-07-28 19:35:08 | [diff] [blame] | 240 | struct ParamTraits<long long> { |
| 241 | typedef long long param_type; |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 242 | static void Write(base::Pickle* m, const param_type& p) { |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 243 | m->WriteInt64(static_cast<int64_t>(p)); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 244 | } |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 245 | static bool Read(const base::Pickle* m, |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 246 | base::PickleIterator* iter, |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 247 | param_type* r) { |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 248 | return iter->ReadInt64(reinterpret_cast<int64_t*>(r)); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 249 | } |
Ken Rockot | 3044d21 | 2018-01-23 02:44:39 | [diff] [blame] | 250 | COMPONENT_EXPORT(IPC) static void Log(const param_type& p, std::string* l); |
[email protected] | 63263f9 | 2009-07-28 19:35:08 | [diff] [blame] | 251 | }; |
| 252 | |
| 253 | template <> |
| 254 | struct ParamTraits<unsigned long long> { |
| 255 | typedef unsigned long long param_type; |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 256 | static void Write(base::Pickle* m, const param_type& p) { m->WriteInt64(p); } |
| 257 | static bool Read(const base::Pickle* m, |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 258 | base::PickleIterator* iter, |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 259 | param_type* r) { |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 260 | return iter->ReadInt64(reinterpret_cast<int64_t*>(r)); |
[email protected] | 63263f9 | 2009-07-28 19:35:08 | [diff] [blame] | 261 | } |
Ken Rockot | 3044d21 | 2018-01-23 02:44:39 | [diff] [blame] | 262 | COMPONENT_EXPORT(IPC) static void Log(const param_type& p, std::string* l); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 263 | }; |
| 264 | |
[email protected] | 2019966 | 2010-06-17 03:29:26 | [diff] [blame] | 265 | // Note that the IPC layer doesn't sanitize NaNs and +/- INF values. Clients |
| 266 | // should be sure to check the sanity of these values after receiving them over |
| 267 | // IPC. |
| 268 | template <> |
Ken Rockot | 3044d21 | 2018-01-23 02:44:39 | [diff] [blame] | 269 | struct COMPONENT_EXPORT(IPC) ParamTraits<float> { |
[email protected] | 2019966 | 2010-06-17 03:29:26 | [diff] [blame] | 270 | typedef float param_type; |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 271 | static void Write(base::Pickle* m, const param_type& p) { m->WriteFloat(p); } |
| 272 | static bool Read(const base::Pickle* m, |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 273 | base::PickleIterator* iter, |
brettw | 05cfd8ddb | 2015-06-02 07:02:47 | [diff] [blame] | 274 | param_type* r) { |
avi | 48fc13b | 2014-12-28 23:31:48 | [diff] [blame] | 275 | return iter->ReadFloat(r); |
[email protected] | 48328ff | 2013-10-31 09:27:31 | [diff] [blame] | 276 | } |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 277 | static void Log(const param_type& p, std::string* l); |
[email protected] | 2019966 | 2010-06-17 03:29:26 | [diff] [blame] | 278 | }; |
| 279 | |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 280 | template <> |
Ken Rockot | 3044d21 | 2018-01-23 02:44:39 | [diff] [blame] | 281 | struct COMPONENT_EXPORT(IPC) ParamTraits<double> { |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 282 | typedef double param_type; |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 283 | static void Write(base::Pickle* m, const param_type& p); |
| 284 | static bool Read(const base::Pickle* m, |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 285 | base::PickleIterator* iter, |
| 286 | param_type* r); |
[email protected] | c410e02 | 2012-05-30 21:15:57 | [diff] [blame] | 287 | static void Log(const param_type& p, std::string* l); |
| 288 | }; |
| 289 | |
ericrk | b5de460 | 2017-02-10 17:49:35 | [diff] [blame] | 290 | template <class P, size_t Size> |
| 291 | struct ParamTraits<P[Size]> { |
| 292 | using param_type = P[Size]; |
ericrk | b5de460 | 2017-02-10 17:49:35 | [diff] [blame] | 293 | static void Write(base::Pickle* m, const param_type& p) { |
| 294 | for (const P& element : p) |
| 295 | WriteParam(m, element); |
| 296 | } |
| 297 | static bool Read(const base::Pickle* m, |
| 298 | base::PickleIterator* iter, |
| 299 | param_type* r) { |
| 300 | for (P& element : *r) { |
| 301 | if (!ReadParam(m, iter, &element)) |
| 302 | return false; |
| 303 | } |
| 304 | return true; |
| 305 | } |
| 306 | static void Log(const param_type& p, std::string* l) { |
| 307 | l->append("["); |
| 308 | for (const P& element : p) { |
| 309 | if (&element != &p[0]) |
| 310 | l->append(" "); |
| 311 | LogParam(element, l); |
| 312 | } |
| 313 | l->append("]"); |
| 314 | } |
| 315 | }; |
| 316 | |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 317 | // STL ParamTraits ------------------------------------------------------------- |
[email protected] | 584f2b2 | 2009-05-21 01:01:59 | [diff] [blame] | 318 | |
| 319 | template <> |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 320 | struct ParamTraits<std::string> { |
| 321 | typedef std::string param_type; |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 322 | static void Write(base::Pickle* m, const param_type& p) { m->WriteString(p); } |
| 323 | static bool Read(const base::Pickle* m, |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 324 | base::PickleIterator* iter, |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 325 | param_type* r) { |
avi | 48fc13b | 2014-12-28 23:31:48 | [diff] [blame] | 326 | return iter->ReadString(r); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 327 | } |
Ken Rockot | 3044d21 | 2018-01-23 02:44:39 | [diff] [blame] | 328 | COMPONENT_EXPORT(IPC) static void Log(const param_type& p, std::string* l); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 329 | }; |
| 330 | |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 331 | template <> |
[email protected] | 476dafb | 2013-12-03 00:39:26 | [diff] [blame] | 332 | struct ParamTraits<base::string16> { |
| 333 | typedef base::string16 param_type; |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 334 | static void Write(base::Pickle* m, const param_type& p) { |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 335 | m->WriteString16(p); |
| 336 | } |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 337 | static bool Read(const base::Pickle* m, |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 338 | base::PickleIterator* iter, |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 339 | param_type* r) { |
avi | 48fc13b | 2014-12-28 23:31:48 | [diff] [blame] | 340 | return iter->ReadString16(r); |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 341 | } |
Ken Rockot | 3044d21 | 2018-01-23 02:44:39 | [diff] [blame] | 342 | COMPONENT_EXPORT(IPC) static void Log(const param_type& p, std::string* l); |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 343 | }; |
[email protected] | 3dd7a7a | 2009-07-27 21:09:07 | [diff] [blame] | 344 | |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 345 | template <> |
Ken Rockot | 3044d21 | 2018-01-23 02:44:39 | [diff] [blame] | 346 | struct COMPONENT_EXPORT(IPC) ParamTraits<std::vector<char>> { |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 347 | typedef std::vector<char> param_type; |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 348 | static void Write(base::Pickle* m, const param_type& p); |
| 349 | static bool Read(const base::Pickle*, |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 350 | base::PickleIterator* iter, |
| 351 | param_type* r); |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 352 | static void Log(const param_type& p, std::string* l); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 353 | }; |
| 354 | |
[email protected] | 51b63f6 | 2011-10-05 18:55:42 | [diff] [blame] | 355 | template <> |
Ken Rockot | 3044d21 | 2018-01-23 02:44:39 | [diff] [blame] | 356 | struct COMPONENT_EXPORT(IPC) ParamTraits<std::vector<unsigned char>> { |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 357 | typedef std::vector<unsigned char> param_type; |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 358 | static void Write(base::Pickle* m, const param_type& p); |
| 359 | static bool Read(const base::Pickle* m, |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 360 | base::PickleIterator* iter, |
| 361 | param_type* r); |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 362 | static void Log(const param_type& p, std::string* l); |
| 363 | }; |
| 364 | |
| 365 | template <> |
Ken Rockot | 3044d21 | 2018-01-23 02:44:39 | [diff] [blame] | 366 | struct COMPONENT_EXPORT(IPC) ParamTraits<std::vector<bool>> { |
[email protected] | 51b63f6 | 2011-10-05 18:55:42 | [diff] [blame] | 367 | typedef std::vector<bool> param_type; |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 368 | static void Write(base::Pickle* m, const param_type& p); |
| 369 | static bool Read(const base::Pickle* m, |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 370 | base::PickleIterator* iter, |
| 371 | param_type* r); |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 372 | static void Log(const param_type& p, std::string* l); |
[email protected] | 51b63f6 | 2011-10-05 18:55:42 | [diff] [blame] | 373 | }; |
| 374 | |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 375 | template <class P> |
bmcquade | 18573e1 | 2016-07-01 13:13:50 | [diff] [blame] | 376 | struct ParamTraits<std::vector<P>> { |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 377 | typedef std::vector<P> param_type; |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 378 | static void Write(base::Pickle* m, const param_type& p) { |
Chris Palmer | c5ea9b9 | 2017-09-25 22:53:22 | [diff] [blame] | 379 | WriteParam(m, base::checked_cast<int>(p.size())); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 380 | for (size_t i = 0; i < p.size(); i++) |
| 381 | WriteParam(m, p[i]); |
| 382 | } |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 383 | static bool Read(const base::Pickle* m, |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 384 | base::PickleIterator* iter, |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 385 | param_type* r) { |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 386 | int size; |
[email protected] | 86440f5 | 2009-12-31 05:17:23 | [diff] [blame] | 387 | // ReadLength() checks for < 0 itself. |
avi | 48fc13b | 2014-12-28 23:31:48 | [diff] [blame] | 388 | if (!iter->ReadLength(&size)) |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 389 | return false; |
| 390 | // Resizing beforehand is not safe, see BUG 1006367 for details. |
[email protected] | 86440f5 | 2009-12-31 05:17:23 | [diff] [blame] | 391 | if (INT_MAX / sizeof(P) <= static_cast<size_t>(size)) |
| 392 | return false; |
| 393 | r->resize(size); |
| 394 | for (int i = 0; i < size; i++) { |
| 395 | if (!ReadParam(m, iter, &(*r)[i])) |
| 396 | return false; |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 397 | } |
| 398 | return true; |
| 399 | } |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 400 | static void Log(const param_type& p, std::string* l) { |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 401 | for (size_t i = 0; i < p.size(); ++i) { |
| 402 | if (i != 0) |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 403 | l->append(" "); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 404 | LogParam((p[i]), l); |
| 405 | } |
| 406 | } |
| 407 | }; |
| 408 | |
Yao Xiao | af79ca9b | 2019-03-12 19:54:18 | [diff] [blame] | 409 | template <std::size_t N> |
| 410 | struct ParamTraits<std::bitset<N>> { |
| 411 | typedef std::bitset<N> param_type; |
| 412 | static void Write(base::Pickle* m, const param_type& p) { |
| 413 | WriteParam(m, base::checked_cast<int>(p.size())); |
| 414 | for (size_t i = 0; i < p.size(); i++) |
| 415 | WriteParam(m, p.test(i)); |
| 416 | } |
| 417 | |
| 418 | static bool Read(const base::Pickle* m, |
| 419 | base::PickleIterator* iter, |
| 420 | param_type* r) { |
| 421 | int size; |
| 422 | // ReadLength() checks for < 0 itself. |
| 423 | if (!iter->ReadLength(&size)) |
| 424 | return false; |
| 425 | if (static_cast<size_t>(size) != r->size()) |
| 426 | return false; |
| 427 | for (size_t i = 0; i < r->size(); i++) { |
| 428 | bool value; |
| 429 | if (!ReadParam(m, iter, &value)) |
| 430 | return false; |
| 431 | (*r)[i] = value; |
| 432 | } |
| 433 | return true; |
| 434 | } |
| 435 | |
| 436 | static void Log(const param_type& p, std::string* l) { |
| 437 | for (size_t i = 0; i < p.size(); ++i) { |
| 438 | if (i != 0) |
| 439 | l->push_back(' '); |
| 440 | LogParam(p.test(i), l); |
| 441 | } |
| 442 | } |
| 443 | }; |
| 444 | |
[email protected] | 96da696 | 2010-05-13 19:10:34 | [diff] [blame] | 445 | template <class P> |
| 446 | struct ParamTraits<std::set<P> > { |
| 447 | typedef std::set<P> param_type; |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 448 | static void Write(base::Pickle* m, const param_type& p) { |
Chris Palmer | c5ea9b9 | 2017-09-25 22:53:22 | [diff] [blame] | 449 | WriteParam(m, base::checked_cast<int>(p.size())); |
[email protected] | 96da696 | 2010-05-13 19:10:34 | [diff] [blame] | 450 | typename param_type::const_iterator iter; |
| 451 | for (iter = p.begin(); iter != p.end(); ++iter) |
| 452 | WriteParam(m, *iter); |
| 453 | } |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 454 | static bool Read(const base::Pickle* m, |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 455 | base::PickleIterator* iter, |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 456 | param_type* r) { |
[email protected] | 96da696 | 2010-05-13 19:10:34 | [diff] [blame] | 457 | int size; |
avi | 48fc13b | 2014-12-28 23:31:48 | [diff] [blame] | 458 | if (!iter->ReadLength(&size)) |
[email protected] | 96da696 | 2010-05-13 19:10:34 | [diff] [blame] | 459 | return false; |
| 460 | for (int i = 0; i < size; ++i) { |
| 461 | P item; |
| 462 | if (!ReadParam(m, iter, &item)) |
| 463 | return false; |
| 464 | r->insert(item); |
| 465 | } |
| 466 | return true; |
| 467 | } |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 468 | static void Log(const param_type& p, std::string* l) { |
| 469 | l->append("<std::set>"); |
[email protected] | 96da696 | 2010-05-13 19:10:34 | [diff] [blame] | 470 | } |
| 471 | }; |
| 472 | |
jkarlin | f96d19ba | 2014-09-24 11:42:37 | [diff] [blame] | 473 | template <class K, class V, class C, class A> |
| 474 | struct ParamTraits<std::map<K, V, C, A> > { |
| 475 | typedef std::map<K, V, C, A> param_type; |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 476 | static void Write(base::Pickle* m, const param_type& p) { |
Chris Palmer | c5ea9b9 | 2017-09-25 22:53:22 | [diff] [blame] | 477 | WriteParam(m, base::checked_cast<int>(p.size())); |
Stuart Langley | 4830039 | 2017-11-02 03:54:44 | [diff] [blame] | 478 | for (const auto& iter : p) { |
| 479 | WriteParam(m, iter.first); |
| 480 | WriteParam(m, iter.second); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 481 | } |
| 482 | } |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 483 | static bool Read(const base::Pickle* m, |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 484 | base::PickleIterator* iter, |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 485 | param_type* r) { |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 486 | int size; |
| 487 | if (!ReadParam(m, iter, &size) || size < 0) |
| 488 | return false; |
| 489 | for (int i = 0; i < size; ++i) { |
| 490 | K k; |
| 491 | if (!ReadParam(m, iter, &k)) |
| 492 | return false; |
| 493 | V& value = (*r)[k]; |
| 494 | if (!ReadParam(m, iter, &value)) |
| 495 | return false; |
| 496 | } |
| 497 | return true; |
| 498 | } |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 499 | static void Log(const param_type& p, std::string* l) { |
| 500 | l->append("<std::map>"); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 501 | } |
| 502 | }; |
| 503 | |
Stuart Langley | 4830039 | 2017-11-02 03:54:44 | [diff] [blame] | 504 | template <class K, class V, class C, class A> |
| 505 | struct ParamTraits<std::unordered_map<K, V, C, A>> { |
| 506 | typedef std::unordered_map<K, V, C, A> param_type; |
| 507 | static void Write(base::Pickle* m, const param_type& p) { |
| 508 | WriteParam(m, base::checked_cast<int>(p.size())); |
| 509 | for (const auto& iter : p) { |
| 510 | WriteParam(m, iter.first); |
| 511 | WriteParam(m, iter.second); |
| 512 | } |
| 513 | } |
| 514 | static bool Read(const base::Pickle* m, |
| 515 | base::PickleIterator* iter, |
| 516 | param_type* r) { |
| 517 | int size; |
| 518 | if (!ReadParam(m, iter, &size) || size < 0) |
| 519 | return false; |
| 520 | for (int i = 0; i < size; ++i) { |
| 521 | K k; |
| 522 | if (!ReadParam(m, iter, &k)) |
| 523 | return false; |
| 524 | V& value = (*r)[k]; |
| 525 | if (!ReadParam(m, iter, &value)) |
| 526 | return false; |
| 527 | } |
| 528 | return true; |
| 529 | } |
| 530 | static void Log(const param_type& p, std::string* l) { |
| 531 | l->append("<std::unordered_map>"); |
| 532 | } |
| 533 | }; |
| 534 | |
[email protected] | a5da6d61 | 2009-08-04 02:00:56 | [diff] [blame] | 535 | template <class A, class B> |
| 536 | struct ParamTraits<std::pair<A, B> > { |
| 537 | typedef std::pair<A, B> param_type; |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 538 | static void Write(base::Pickle* m, const param_type& p) { |
[email protected] | a5da6d61 | 2009-08-04 02:00:56 | [diff] [blame] | 539 | WriteParam(m, p.first); |
| 540 | WriteParam(m, p.second); |
| 541 | } |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 542 | static bool Read(const base::Pickle* m, |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 543 | base::PickleIterator* iter, |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 544 | param_type* r) { |
[email protected] | a5da6d61 | 2009-08-04 02:00:56 | [diff] [blame] | 545 | return ReadParam(m, iter, &r->first) && ReadParam(m, iter, &r->second); |
| 546 | } |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 547 | static void Log(const param_type& p, std::string* l) { |
| 548 | l->append("("); |
[email protected] | a5da6d61 | 2009-08-04 02:00:56 | [diff] [blame] | 549 | LogParam(p.first, l); |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 550 | l->append(", "); |
[email protected] | a5da6d61 | 2009-08-04 02:00:56 | [diff] [blame] | 551 | LogParam(p.second, l); |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 552 | l->append(")"); |
[email protected] | a5da6d61 | 2009-08-04 02:00:56 | [diff] [blame] | 553 | } |
| 554 | }; |
| 555 | |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 556 | // Base ParamTraits ------------------------------------------------------------ |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 557 | |
| 558 | template <> |
Ken Rockot | 3044d21 | 2018-01-23 02:44:39 | [diff] [blame] | 559 | struct COMPONENT_EXPORT(IPC) ParamTraits<base::DictionaryValue> { |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 560 | typedef base::DictionaryValue param_type; |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 561 | static void Write(base::Pickle* m, const param_type& p); |
| 562 | static bool Read(const base::Pickle* m, |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 563 | base::PickleIterator* iter, |
| 564 | param_type* r); |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 565 | static void Log(const param_type& p, std::string* l); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 566 | }; |
| 567 | |
Fabrice de Gans-Riberi | cbce434 | 2018-05-07 20:02:09 | [diff] [blame] | 568 | #if defined(OS_POSIX) || defined(OS_FUCHSIA) |
[email protected] | 2749885f | 2009-03-05 21:40:11 | [diff] [blame] | 569 | // FileDescriptors may be serialised over IPC channels on POSIX. On the |
| 570 | // receiving side, the FileDescriptor is a valid duplicate of the file |
| 571 | // descriptor which was transmitted: *it is not just a copy of the integer like |
| 572 | // HANDLEs on Windows*. The only exception is if the file descriptor is < 0. In |
| 573 | // this case, the receiving end will see a value of -1. *Zero is a valid file |
| 574 | // descriptor*. |
| 575 | // |
| 576 | // The received file descriptor will have the |auto_close| flag set to true. The |
| 577 | // code which handles the message is responsible for taking ownership of it. |
| 578 | // File descriptors are OS resources and must be closed when no longer needed. |
| 579 | // |
| 580 | // When sending a file descriptor, the file descriptor must be valid at the time |
| 581 | // of transmission. Since transmission is not synchronous, one should consider |
| 582 | // dup()ing any file descriptors to be transmitted and setting the |auto_close| |
| 583 | // flag, which causes the file descriptor to be closed after writing. |
Ken Rockot | 3044d21 | 2018-01-23 02:44:39 | [diff] [blame] | 584 | template <> |
| 585 | struct COMPONENT_EXPORT(IPC) ParamTraits<base::FileDescriptor> { |
[email protected] | 5fe733de | 2009-02-11 18:59:20 | [diff] [blame] | 586 | typedef base::FileDescriptor param_type; |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 587 | static void Write(base::Pickle* m, const param_type& p); |
| 588 | static bool Read(const base::Pickle* m, |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 589 | base::PickleIterator* iter, |
| 590 | param_type* r); |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 591 | static void Log(const param_type& p, std::string* l); |
[email protected] | 526776c | 2009-02-07 00:39:26 | [diff] [blame] | 592 | }; |
Sergey Ulanov | ac89bb9 | 2019-03-22 18:46:49 | [diff] [blame] | 593 | |
| 594 | template <> |
| 595 | struct COMPONENT_EXPORT(IPC) ParamTraits<base::ScopedFD> { |
| 596 | typedef base::ScopedFD param_type; |
| 597 | static void Write(base::Pickle* m, const param_type& p); |
| 598 | static bool Read(const base::Pickle* m, |
| 599 | base::PickleIterator* iter, |
| 600 | param_type* r); |
| 601 | static void Log(const param_type& p, std::string* l); |
| 602 | }; |
| 603 | |
Fabrice de Gans-Riberi | cbce434 | 2018-05-07 20:02:09 | [diff] [blame] | 604 | #endif // defined(OS_POSIX) || defined(OS_FUCHSIA) |
[email protected] | 526776c | 2009-02-07 00:39:26 | [diff] [blame] | 605 | |
Sergey Ulanov | a97f328 | 2019-04-17 01:27:34 | [diff] [blame] | 606 | #if defined(OS_FUCHSIA) |
| 607 | template <> |
| 608 | struct COMPONENT_EXPORT(IPC) ParamTraits<zx::vmo> { |
| 609 | typedef zx::vmo param_type; |
| 610 | static void Write(base::Pickle* m, const param_type& p); |
| 611 | static bool Read(const base::Pickle* m, |
| 612 | base::PickleIterator* iter, |
| 613 | param_type* r); |
| 614 | static void Log(const param_type& p, std::string* l); |
| 615 | }; |
| 616 | #endif // defined(OS_FUCHSIA) |
| 617 | |
scottmg | d19b4f7 | 2015-06-19 22:51:00 | [diff] [blame] | 618 | template <> |
Ken Rockot | 3044d21 | 2018-01-23 02:44:39 | [diff] [blame] | 619 | struct COMPONENT_EXPORT(IPC) ParamTraits<base::SharedMemoryHandle> { |
scottmg | d19b4f7 | 2015-06-19 22:51:00 | [diff] [blame] | 620 | typedef base::SharedMemoryHandle param_type; |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 621 | static void Write(base::Pickle* m, const param_type& p); |
| 622 | static bool Read(const base::Pickle* m, |
| 623 | base::PickleIterator* iter, |
| 624 | param_type* r); |
scottmg | d19b4f7 | 2015-06-19 22:51:00 | [diff] [blame] | 625 | static void Log(const param_type& p, std::string* l); |
| 626 | }; |
scottmg | d19b4f7 | 2015-06-19 22:51:00 | [diff] [blame] | 627 | |
Klaus Weidner | 3824a888 | 2017-11-03 06:24:57 | [diff] [blame] | 628 | #if defined(OS_ANDROID) |
| 629 | template <> |
Alexandr Ilin | 0443a8f | 2018-07-20 20:14:50 | [diff] [blame] | 630 | struct COMPONENT_EXPORT(IPC) |
| 631 | ParamTraits<base::android::ScopedHardwareBufferHandle> { |
| 632 | typedef base::android::ScopedHardwareBufferHandle param_type; |
Klaus Weidner | 3824a888 | 2017-11-03 06:24:57 | [diff] [blame] | 633 | static void Write(base::Pickle* m, const param_type& p); |
| 634 | static bool Read(const base::Pickle* m, |
| 635 | base::PickleIterator* iter, |
| 636 | param_type* r); |
| 637 | static void Log(const param_type& p, std::string* l); |
| 638 | }; |
| 639 | #endif |
| 640 | |
Alexandr Ilin | d497eee | 2018-04-19 22:50:54 | [diff] [blame] | 641 | template <> |
| 642 | struct COMPONENT_EXPORT(IPC) ParamTraits<base::ReadOnlySharedMemoryRegion> { |
| 643 | typedef base::ReadOnlySharedMemoryRegion param_type; |
| 644 | static void Write(base::Pickle* m, const param_type& p); |
| 645 | static bool Read(const base::Pickle* m, |
| 646 | base::PickleIterator* iter, |
| 647 | param_type* r); |
| 648 | static void Log(const param_type& p, std::string* l); |
| 649 | }; |
| 650 | |
| 651 | template <> |
| 652 | struct COMPONENT_EXPORT(IPC) ParamTraits<base::WritableSharedMemoryRegion> { |
| 653 | typedef base::WritableSharedMemoryRegion param_type; |
| 654 | static void Write(base::Pickle* m, const param_type& p); |
| 655 | static bool Read(const base::Pickle* m, |
| 656 | base::PickleIterator* iter, |
| 657 | param_type* r); |
| 658 | static void Log(const param_type& p, std::string* l); |
| 659 | }; |
| 660 | |
| 661 | template <> |
| 662 | struct COMPONENT_EXPORT(IPC) ParamTraits<base::UnsafeSharedMemoryRegion> { |
| 663 | typedef base::UnsafeSharedMemoryRegion param_type; |
| 664 | static void Write(base::Pickle* m, const param_type& p); |
| 665 | static bool Read(const base::Pickle* m, |
| 666 | base::PickleIterator* iter, |
| 667 | param_type* r); |
| 668 | static void Log(const param_type& p, std::string* l); |
| 669 | }; |
| 670 | |
| 671 | template <> |
| 672 | struct COMPONENT_EXPORT(IPC) |
| 673 | ParamTraits<base::subtle::PlatformSharedMemoryRegion> { |
| 674 | typedef base::subtle::PlatformSharedMemoryRegion param_type; |
| 675 | static void Write(base::Pickle* m, const param_type& p); |
| 676 | static bool Read(const base::Pickle* m, |
| 677 | base::PickleIterator* iter, |
| 678 | param_type* r); |
| 679 | static void Log(const param_type& p, std::string* l); |
| 680 | }; |
| 681 | |
| 682 | template <> |
| 683 | struct COMPONENT_EXPORT(IPC) |
| 684 | ParamTraits<base::subtle::PlatformSharedMemoryRegion::Mode> { |
| 685 | typedef base::subtle::PlatformSharedMemoryRegion::Mode param_type; |
| 686 | static void Write(base::Pickle* m, const param_type& p); |
| 687 | static bool Read(const base::Pickle* m, |
| 688 | base::PickleIterator* iter, |
| 689 | param_type* r); |
| 690 | static void Log(const param_type& p, std::string* l); |
| 691 | }; |
| 692 | |
erikchen | d804e105 | 2017-04-29 02:24:36 | [diff] [blame] | 693 | #if defined(OS_WIN) |
| 694 | template <> |
Ken Rockot | 3044d21 | 2018-01-23 02:44:39 | [diff] [blame] | 695 | struct COMPONENT_EXPORT(IPC) ParamTraits<PlatformFileForTransit> { |
erikchen | d804e105 | 2017-04-29 02:24:36 | [diff] [blame] | 696 | typedef PlatformFileForTransit param_type; |
erikchen | d804e105 | 2017-04-29 02:24:36 | [diff] [blame] | 697 | static void Write(base::Pickle* m, const param_type& p); |
| 698 | static bool Read(const base::Pickle* m, |
| 699 | base::PickleIterator* iter, |
| 700 | param_type* r); |
| 701 | static void Log(const param_type& p, std::string* l); |
| 702 | }; |
| 703 | #endif // defined(OS_WIN) |
| 704 | |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 705 | template <> |
Ken Rockot | 3044d21 | 2018-01-23 02:44:39 | [diff] [blame] | 706 | struct COMPONENT_EXPORT(IPC) ParamTraits<base::FilePath> { |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 707 | typedef base::FilePath param_type; |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 708 | static void Write(base::Pickle* m, const param_type& p); |
| 709 | static bool Read(const base::Pickle* m, |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 710 | base::PickleIterator* iter, |
| 711 | param_type* r); |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 712 | static void Log(const param_type& p, std::string* l); |
[email protected] | d2e884d | 2009-06-22 20:37:52 | [diff] [blame] | 713 | }; |
| 714 | |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 715 | template <> |
Ken Rockot | 3044d21 | 2018-01-23 02:44:39 | [diff] [blame] | 716 | struct COMPONENT_EXPORT(IPC) ParamTraits<base::ListValue> { |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 717 | typedef base::ListValue param_type; |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 718 | static void Write(base::Pickle* m, const param_type& p); |
| 719 | static bool Read(const base::Pickle* m, |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 720 | base::PickleIterator* iter, |
| 721 | param_type* r); |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 722 | static void Log(const param_type& p, std::string* l); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 723 | }; |
| 724 | |
[email protected] | eb47a13 | 2009-03-04 00:39:56 | [diff] [blame] | 725 | template <> |
Ken Rockot | 3044d21 | 2018-01-23 02:44:39 | [diff] [blame] | 726 | struct COMPONENT_EXPORT(IPC) ParamTraits<base::NullableString16> { |
[email protected] | 0238a16 | 2013-06-13 13:47:46 | [diff] [blame] | 727 | typedef base::NullableString16 param_type; |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 728 | static void Write(base::Pickle* m, const param_type& p); |
| 729 | static bool Read(const base::Pickle* m, |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 730 | base::PickleIterator* iter, |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 731 | param_type* r); |
| 732 | static void Log(const param_type& p, std::string* l); |
| 733 | }; |
| 734 | |
| 735 | template <> |
Ken Rockot | 3044d21 | 2018-01-23 02:44:39 | [diff] [blame] | 736 | struct COMPONENT_EXPORT(IPC) ParamTraits<base::File::Info> { |
[email protected] | 141bcc5 | 2014-01-27 21:36:00 | [diff] [blame] | 737 | typedef base::File::Info param_type; |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 738 | static void Write(base::Pickle* m, const param_type& p); |
| 739 | static bool Read(const base::Pickle* m, |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 740 | base::PickleIterator* iter, |
| 741 | param_type* r); |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 742 | static void Log(const param_type& p, std::string* l); |
| 743 | }; |
| 744 | |
| 745 | template <> |
[email protected] | 141bcc5 | 2014-01-27 21:36:00 | [diff] [blame] | 746 | struct SimilarTypeTraits<base::File::Error> { |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 747 | typedef int Type; |
| 748 | }; |
| 749 | |
[email protected] | ecf59c7 | 2013-02-28 21:46:11 | [diff] [blame] | 750 | #if defined(OS_WIN) |
| 751 | template <> |
| 752 | struct SimilarTypeTraits<HWND> { |
| 753 | typedef HANDLE Type; |
| 754 | }; |
| 755 | #endif // defined(OS_WIN) |
| 756 | |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 757 | template <> |
Ken Rockot | 3044d21 | 2018-01-23 02:44:39 | [diff] [blame] | 758 | struct COMPONENT_EXPORT(IPC) ParamTraits<base::Time> { |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 759 | typedef base::Time param_type; |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 760 | static void Write(base::Pickle* m, const param_type& p); |
| 761 | static bool Read(const base::Pickle* m, |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 762 | base::PickleIterator* iter, |
| 763 | param_type* r); |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 764 | static void Log(const param_type& p, std::string* l); |
| 765 | }; |
| 766 | |
| 767 | template <> |
Ken Rockot | 3044d21 | 2018-01-23 02:44:39 | [diff] [blame] | 768 | struct COMPONENT_EXPORT(IPC) ParamTraits<base::TimeDelta> { |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 769 | typedef base::TimeDelta param_type; |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 770 | static void Write(base::Pickle* m, const param_type& p); |
| 771 | static bool Read(const base::Pickle* m, |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 772 | base::PickleIterator* iter, |
| 773 | param_type* r); |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 774 | static void Log(const param_type& p, std::string* l); |
| 775 | }; |
| 776 | |
| 777 | template <> |
Ken Rockot | 3044d21 | 2018-01-23 02:44:39 | [diff] [blame] | 778 | struct COMPONENT_EXPORT(IPC) ParamTraits<base::TimeTicks> { |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 779 | typedef base::TimeTicks param_type; |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 780 | static void Write(base::Pickle* m, const param_type& p); |
| 781 | static bool Read(const base::Pickle* m, |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 782 | base::PickleIterator* iter, |
| 783 | param_type* r); |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 784 | static void Log(const param_type& p, std::string* l); |
[email protected] | 503683f | 2009-02-26 09:13:01 | [diff] [blame] | 785 | }; |
| 786 | |
| 787 | template <> |
Ken Rockot | 3044d21 | 2018-01-23 02:44:39 | [diff] [blame] | 788 | struct COMPONENT_EXPORT(IPC) ParamTraits<base::UnguessableToken> { |
tguilbert | 4a5ac60 | 2016-09-19 21:11:25 | [diff] [blame] | 789 | typedef base::UnguessableToken param_type; |
tguilbert | 4a5ac60 | 2016-09-19 21:11:25 | [diff] [blame] | 790 | static void Write(base::Pickle* m, const param_type& p); |
| 791 | static bool Read(const base::Pickle* m, |
| 792 | base::PickleIterator* iter, |
| 793 | param_type* r); |
| 794 | static void Log(const param_type& p, std::string* l); |
| 795 | }; |
| 796 | |
| 797 | template <> |
tzik | 9ca30219 | 2016-02-11 10:24:45 | [diff] [blame] | 798 | struct ParamTraits<std::tuple<>> { |
| 799 | typedef std::tuple<> param_type; |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 800 | static void Write(base::Pickle* m, const param_type& p) {} |
| 801 | static bool Read(const base::Pickle* m, |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 802 | base::PickleIterator* iter, |
brettw | 05cfd8ddb | 2015-06-02 07:02:47 | [diff] [blame] | 803 | param_type* r) { |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 804 | return true; |
| 805 | } |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 806 | static void Log(const param_type& p, std::string* l) { |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 807 | } |
| 808 | }; |
| 809 | |
ossu | fdd19a3 | 2017-06-22 09:35:39 | [diff] [blame] | 810 | template <typename T, int index, int count> |
| 811 | struct TupleParamTraitsHelper { |
| 812 | using Next = TupleParamTraitsHelper<T, index + 1, count>; |
| 813 | |
ossu | fdd19a3 | 2017-06-22 09:35:39 | [diff] [blame] | 814 | static void Write(base::Pickle* m, const T& p) { |
| 815 | WriteParam(m, std::get<index>(p)); |
| 816 | Next::Write(m, p); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 817 | } |
ossu | fdd19a3 | 2017-06-22 09:35:39 | [diff] [blame] | 818 | |
| 819 | static bool Read(const base::Pickle* m, base::PickleIterator* iter, T* r) { |
| 820 | return ReadParam(m, iter, &std::get<index>(*r)) && Next::Read(m, iter, r); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 821 | } |
ossu | fdd19a3 | 2017-06-22 09:35:39 | [diff] [blame] | 822 | |
| 823 | static void Log(const T& p, std::string* l) { |
| 824 | LogParam(std::get<index>(p), l); |
| 825 | if (index < count - 1) |
| 826 | l->append(", "); |
| 827 | Next::Log(p, l); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 828 | } |
| 829 | }; |
| 830 | |
ossu | fdd19a3 | 2017-06-22 09:35:39 | [diff] [blame] | 831 | template <typename T, int index> |
| 832 | struct TupleParamTraitsHelper<T, index, index> { |
ossu | fdd19a3 | 2017-06-22 09:35:39 | [diff] [blame] | 833 | static void Write(base::Pickle* m, const T& p) {} |
| 834 | static bool Read(const base::Pickle* m, base::PickleIterator* iter, T* r) { |
| 835 | return true; |
rockot | 0457af10 | 2016-02-05 02:12:32 | [diff] [blame] | 836 | } |
ossu | fdd19a3 | 2017-06-22 09:35:39 | [diff] [blame] | 837 | static void Log(const T& p, std::string* l) {} |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 838 | }; |
| 839 | |
ossu | fdd19a3 | 2017-06-22 09:35:39 | [diff] [blame] | 840 | template <typename... Args> |
| 841 | struct ParamTraits<std::tuple<Args...>> { |
| 842 | using param_type = std::tuple<Args...>; |
| 843 | using Helper = |
| 844 | TupleParamTraitsHelper<param_type, 0, std::tuple_size<param_type>::value>; |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 845 | |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 846 | static void Write(base::Pickle* m, const param_type& p) { |
ossu | fdd19a3 | 2017-06-22 09:35:39 | [diff] [blame] | 847 | Helper::Write(m, p); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 848 | } |
ossu | fdd19a3 | 2017-06-22 09:35:39 | [diff] [blame] | 849 | |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 850 | static bool Read(const base::Pickle* m, |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 851 | base::PickleIterator* iter, |
brettw | 05cfd8ddb | 2015-06-02 07:02:47 | [diff] [blame] | 852 | param_type* r) { |
ossu | fdd19a3 | 2017-06-22 09:35:39 | [diff] [blame] | 853 | return Helper::Read(m, iter, r); |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 854 | } |
ossu | fdd19a3 | 2017-06-22 09:35:39 | [diff] [blame] | 855 | |
| 856 | static void Log(const param_type& p, std::string* l) { Helper::Log(p, l); } |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 857 | }; |
| 858 | |
miletus | 1edf97f9 | 2015-07-23 19:42:36 | [diff] [blame] | 859 | template <class P, size_t stack_capacity> |
| 860 | struct ParamTraits<base::StackVector<P, stack_capacity> > { |
| 861 | typedef base::StackVector<P, stack_capacity> param_type; |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 862 | static void Write(base::Pickle* m, const param_type& p) { |
Chris Palmer | c5ea9b9 | 2017-09-25 22:53:22 | [diff] [blame] | 863 | WriteParam(m, base::checked_cast<int>(p->size())); |
miletus | 1edf97f9 | 2015-07-23 19:42:36 | [diff] [blame] | 864 | for (size_t i = 0; i < p->size(); i++) |
| 865 | WriteParam(m, p[i]); |
| 866 | } |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 867 | static bool Read(const base::Pickle* m, |
miletus | 1edf97f9 | 2015-07-23 19:42:36 | [diff] [blame] | 868 | base::PickleIterator* iter, |
| 869 | param_type* r) { |
| 870 | int size; |
| 871 | // ReadLength() checks for < 0 itself. |
| 872 | if (!iter->ReadLength(&size)) |
| 873 | return false; |
| 874 | // Sanity check for the vector size. |
| 875 | if (INT_MAX / sizeof(P) <= static_cast<size_t>(size)) |
| 876 | return false; |
| 877 | P value; |
| 878 | for (int i = 0; i < size; i++) { |
| 879 | if (!ReadParam(m, iter, &value)) |
| 880 | return false; |
| 881 | (*r)->push_back(value); |
| 882 | } |
| 883 | return true; |
| 884 | } |
| 885 | static void Log(const param_type& p, std::string* l) { |
| 886 | for (size_t i = 0; i < p->size(); ++i) { |
| 887 | if (i != 0) |
| 888 | l->append(" "); |
| 889 | LogParam((p[i]), l); |
| 890 | } |
| 891 | } |
| 892 | }; |
| 893 | |
[email protected] | 205294b | 2014-03-18 20:48:35 | [diff] [blame] | 894 | template <typename NormalMap, |
| 895 | int kArraySize, |
| 896 | typename EqualKey, |
| 897 | typename MapInit> |
brettw | ca2ec763 | 2017-04-20 06:10:20 | [diff] [blame] | 898 | struct ParamTraits<base::small_map<NormalMap, kArraySize, EqualKey, MapInit>> { |
| 899 | using param_type = base::small_map<NormalMap, kArraySize, EqualKey, MapInit>; |
| 900 | using K = typename param_type::key_type; |
| 901 | using V = typename param_type::data_type; |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 902 | static void Write(base::Pickle* m, const param_type& p) { |
Chris Palmer | c5ea9b9 | 2017-09-25 22:53:22 | [diff] [blame] | 903 | WriteParam(m, base::checked_cast<int>(p.size())); |
[email protected] | 205294b | 2014-03-18 20:48:35 | [diff] [blame] | 904 | typename param_type::const_iterator iter; |
| 905 | for (iter = p.begin(); iter != p.end(); ++iter) { |
| 906 | WriteParam(m, iter->first); |
| 907 | WriteParam(m, iter->second); |
| 908 | } |
| 909 | } |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 910 | static bool Read(const base::Pickle* m, |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 911 | base::PickleIterator* iter, |
brettw | 05cfd8ddb | 2015-06-02 07:02:47 | [diff] [blame] | 912 | param_type* r) { |
[email protected] | 205294b | 2014-03-18 20:48:35 | [diff] [blame] | 913 | int size; |
avi | 48fc13b | 2014-12-28 23:31:48 | [diff] [blame] | 914 | if (!iter->ReadLength(&size)) |
[email protected] | 205294b | 2014-03-18 20:48:35 | [diff] [blame] | 915 | return false; |
| 916 | for (int i = 0; i < size; ++i) { |
| 917 | K key; |
| 918 | if (!ReadParam(m, iter, &key)) |
| 919 | return false; |
| 920 | V& value = (*r)[key]; |
| 921 | if (!ReadParam(m, iter, &value)) |
| 922 | return false; |
| 923 | } |
| 924 | return true; |
| 925 | } |
| 926 | static void Log(const param_type& p, std::string* l) { |
brettw | ca2ec763 | 2017-04-20 06:10:20 | [diff] [blame] | 927 | l->append("<base::small_map>"); |
| 928 | } |
| 929 | }; |
| 930 | |
| 931 | template <class Key, class Mapped, class Compare> |
| 932 | struct ParamTraits<base::flat_map<Key, Mapped, Compare>> { |
| 933 | using param_type = base::flat_map<Key, Mapped, Compare>; |
brettw | ca2ec763 | 2017-04-20 06:10:20 | [diff] [blame] | 934 | static void Write(base::Pickle* m, const param_type& p) { |
| 935 | DCHECK(base::IsValueInRangeForNumericType<int>(p.size())); |
Chris Palmer | c5ea9b9 | 2017-09-25 22:53:22 | [diff] [blame] | 936 | WriteParam(m, base::checked_cast<int>(p.size())); |
brettw | ca2ec763 | 2017-04-20 06:10:20 | [diff] [blame] | 937 | for (const auto& iter : p) { |
| 938 | WriteParam(m, iter.first); |
| 939 | WriteParam(m, iter.second); |
| 940 | } |
| 941 | } |
| 942 | static bool Read(const base::Pickle* m, |
| 943 | base::PickleIterator* iter, |
| 944 | param_type* r) { |
| 945 | int size; |
| 946 | if (!iter->ReadLength(&size)) |
| 947 | return false; |
| 948 | |
| 949 | // Construct by creating in a vector and moving into the flat_map. Properly |
| 950 | // serialized flat_maps will be in-order so this will be O(n). Incorrectly |
| 951 | // serialized ones will still be handled properly. |
| 952 | std::vector<typename param_type::value_type> vect; |
| 953 | vect.resize(size); |
| 954 | for (int i = 0; i < size; ++i) { |
| 955 | if (!ReadParam(m, iter, &vect[i].first)) |
| 956 | return false; |
| 957 | if (!ReadParam(m, iter, &vect[i].second)) |
| 958 | return false; |
| 959 | } |
| 960 | |
| 961 | *r = param_type(std::move(vect), base::KEEP_FIRST_OF_DUPES); |
| 962 | return true; |
| 963 | } |
| 964 | static void Log(const param_type& p, std::string* l) { |
| 965 | l->append("<base::flat_map>"); |
[email protected] | 205294b | 2014-03-18 20:48:35 | [diff] [blame] | 966 | } |
| 967 | }; |
| 968 | |
[email protected] | 8e431f203 | 2014-05-20 02:34:56 | [diff] [blame] | 969 | template <class P> |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 970 | struct ParamTraits<std::unique_ptr<P>> { |
| 971 | typedef std::unique_ptr<P> param_type; |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 972 | static void Write(base::Pickle* m, const param_type& p) { |
[email protected] | 8e431f203 | 2014-05-20 02:34:56 | [diff] [blame] | 973 | bool valid = !!p; |
| 974 | WriteParam(m, valid); |
| 975 | if (valid) |
| 976 | WriteParam(m, *p); |
| 977 | } |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 978 | static bool Read(const base::Pickle* m, |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 979 | base::PickleIterator* iter, |
brettw | 05cfd8ddb | 2015-06-02 07:02:47 | [diff] [blame] | 980 | param_type* r) { |
[email protected] | 8e431f203 | 2014-05-20 02:34:56 | [diff] [blame] | 981 | bool valid = false; |
| 982 | if (!ReadParam(m, iter, &valid)) |
| 983 | return false; |
| 984 | |
| 985 | if (!valid) { |
| 986 | r->reset(); |
| 987 | return true; |
| 988 | } |
| 989 | |
| 990 | param_type temp(new P()); |
| 991 | if (!ReadParam(m, iter, temp.get())) |
| 992 | return false; |
| 993 | |
| 994 | r->swap(temp); |
| 995 | return true; |
| 996 | } |
| 997 | static void Log(const param_type& p, std::string* l) { |
| 998 | if (p) |
| 999 | LogParam(*p, l); |
| 1000 | else |
| 1001 | l->append("NULL"); |
| 1002 | } |
| 1003 | }; |
| 1004 | |
bmcquade | 18573e1 | 2016-07-01 13:13:50 | [diff] [blame] | 1005 | template <class P> |
| 1006 | struct ParamTraits<base::Optional<P>> { |
| 1007 | typedef base::Optional<P> param_type; |
bmcquade | 18573e1 | 2016-07-01 13:13:50 | [diff] [blame] | 1008 | static void Write(base::Pickle* m, const param_type& p) { |
| 1009 | const bool is_set = static_cast<bool>(p); |
| 1010 | WriteParam(m, is_set); |
| 1011 | if (is_set) |
| 1012 | WriteParam(m, p.value()); |
| 1013 | } |
| 1014 | static bool Read(const base::Pickle* m, |
| 1015 | base::PickleIterator* iter, |
| 1016 | param_type* r) { |
| 1017 | bool is_set = false; |
| 1018 | if (!iter->ReadBool(&is_set)) |
| 1019 | return false; |
| 1020 | if (is_set) { |
| 1021 | P value; |
| 1022 | if (!ReadParam(m, iter, &value)) |
| 1023 | return false; |
| 1024 | *r = std::move(value); |
| 1025 | } |
| 1026 | return true; |
| 1027 | } |
| 1028 | static void Log(const param_type& p, std::string* l) { |
| 1029 | if (p) |
| 1030 | LogParam(p.value(), l); |
| 1031 | else |
| 1032 | l->append("(unset)"); |
| 1033 | } |
| 1034 | }; |
| 1035 | |
Maciej Pawlowski | 4a3ac685 | 2019-05-09 17:06:14 | [diff] [blame] | 1036 | // base/util types ParamTraits |
| 1037 | |
| 1038 | template <typename TypeMarker, typename WrappedType, WrappedType kInvalidValue> |
| 1039 | struct ParamTraits<util::IdType<TypeMarker, WrappedType, kInvalidValue>> { |
| 1040 | using param_type = util::IdType<TypeMarker, WrappedType, kInvalidValue>; |
| 1041 | static void Write(base::Pickle* m, const param_type& p) { |
| 1042 | WriteParam(m, p.GetUnsafeValue()); |
| 1043 | } |
| 1044 | static bool Read(const base::Pickle* m, |
| 1045 | base::PickleIterator* iter, |
| 1046 | param_type* r) { |
| 1047 | WrappedType value; |
| 1048 | if (!ReadParam(m, iter, &value)) |
| 1049 | return false; |
| 1050 | *r = param_type::FromUnsafeValue(value); |
| 1051 | return true; |
| 1052 | } |
| 1053 | static void Log(const param_type& p, std::string* l) { |
| 1054 | LogParam(p.GetUnsafeValue(), l); |
| 1055 | } |
| 1056 | }; |
| 1057 | |
Maciej Pawlowski | c00800e | 2019-05-23 08:43:03 | [diff] [blame] | 1058 | template <typename TagType, typename UnderlyingType> |
| 1059 | struct ParamTraits<util::StrongAlias<TagType, UnderlyingType>> { |
| 1060 | using param_type = util::StrongAlias<TagType, UnderlyingType>; |
| 1061 | static void Write(base::Pickle* m, const param_type& p) { |
| 1062 | WriteParam(m, p.value()); |
| 1063 | } |
| 1064 | static bool Read(const base::Pickle* m, |
| 1065 | base::PickleIterator* iter, |
| 1066 | param_type* r) { |
| 1067 | UnderlyingType value; |
| 1068 | if (!ReadParam(m, iter, &value)) |
| 1069 | return false; |
| 1070 | *r = param_type::StrongAlias(value); |
| 1071 | return true; |
| 1072 | } |
| 1073 | static void Log(const param_type& p, std::string* l) { |
| 1074 | LogParam(p.value(), l); |
| 1075 | } |
| 1076 | }; |
| 1077 | |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1078 | // IPC types ParamTraits ------------------------------------------------------- |
| 1079 | |
| 1080 | // A ChannelHandle is basically a platform-inspecific wrapper around the |
| 1081 | // fact that IPC endpoints are handled specially on POSIX. See above comments |
| 1082 | // on FileDescriptor for more background. |
Ken Rockot | 3044d21 | 2018-01-23 02:44:39 | [diff] [blame] | 1083 | template <> |
| 1084 | struct COMPONENT_EXPORT(IPC) ParamTraits<IPC::ChannelHandle> { |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1085 | typedef ChannelHandle param_type; |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 1086 | static void Write(base::Pickle* m, const param_type& p); |
| 1087 | static bool Read(const base::Pickle* m, |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 1088 | base::PickleIterator* iter, |
| 1089 | param_type* r); |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1090 | static void Log(const param_type& p, std::string* l); |
| 1091 | }; |
| 1092 | |
| 1093 | template <> |
Ken Rockot | 3044d21 | 2018-01-23 02:44:39 | [diff] [blame] | 1094 | struct COMPONENT_EXPORT(IPC) ParamTraits<LogData> { |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1095 | typedef LogData param_type; |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 1096 | static void Write(base::Pickle* m, const param_type& p); |
| 1097 | static bool Read(const base::Pickle* m, |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 1098 | base::PickleIterator* iter, |
| 1099 | param_type* r); |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1100 | static void Log(const param_type& p, std::string* l); |
| 1101 | }; |
| 1102 | |
| 1103 | template <> |
Ken Rockot | 3044d21 | 2018-01-23 02:44:39 | [diff] [blame] | 1104 | struct COMPONENT_EXPORT(IPC) ParamTraits<Message> { |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 1105 | static void Write(base::Pickle* m, const Message& p); |
| 1106 | static bool Read(const base::Pickle* m, |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 1107 | base::PickleIterator* iter, |
| 1108 | Message* r); |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1109 | static void Log(const Message& p, std::string* l); |
| 1110 | }; |
| 1111 | |
| 1112 | // Windows ParamTraits --------------------------------------------------------- |
| 1113 | |
| 1114 | #if defined(OS_WIN) |
| 1115 | template <> |
Ken Rockot | 3044d21 | 2018-01-23 02:44:39 | [diff] [blame] | 1116 | struct COMPONENT_EXPORT(IPC) ParamTraits<HANDLE> { |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1117 | typedef HANDLE param_type; |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 1118 | static void Write(base::Pickle* m, const param_type& p); |
| 1119 | static bool Read(const base::Pickle* m, |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 1120 | base::PickleIterator* iter, |
| 1121 | param_type* r); |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1122 | static void Log(const param_type& p, std::string* l); |
| 1123 | }; |
| 1124 | |
| 1125 | template <> |
Ken Rockot | 3044d21 | 2018-01-23 02:44:39 | [diff] [blame] | 1126 | struct COMPONENT_EXPORT(IPC) ParamTraits<MSG> { |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1127 | typedef MSG param_type; |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 1128 | static void Write(base::Pickle* m, const param_type& p); |
| 1129 | static bool Read(const base::Pickle* m, |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 1130 | base::PickleIterator* iter, |
| 1131 | param_type* r); |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1132 | static void Log(const param_type& p, std::string* l); |
| 1133 | }; |
| 1134 | #endif // defined(OS_WIN) |
| 1135 | |
[email protected] | 7d5c3ac | 2009-02-04 08:58:19 | [diff] [blame] | 1136 | //----------------------------------------------------------------------------- |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1137 | // Generic message subclasses |
| 1138 | |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 1139 | // defined in ipc_logging.cc |
Ken Rockot | 3044d21 | 2018-01-23 02:44:39 | [diff] [blame] | 1140 | COMPONENT_EXPORT(IPC) |
| 1141 | void GenerateLogData(const Message& message, LogData* data, bool get_params); |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 1142 | |
davidsz | 041528a | 2017-05-12 09:19:23 | [diff] [blame] | 1143 | #if BUILDFLAG(IPC_MESSAGE_LOG_ENABLED) |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 1144 | inline void AddOutputParamsToLog(const Message* msg, std::string* l) { |
| 1145 | const std::string& output_params = msg->output_params(); |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 1146 | if (!l->empty() && !output_params.empty()) |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 1147 | l->append(", "); |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 1148 | |
| 1149 | l->append(output_params); |
| 1150 | } |
| 1151 | |
| 1152 | template <class ReplyParamType> |
| 1153 | inline void LogReplyParamsToMessage(const ReplyParamType& reply_params, |
| 1154 | const Message* msg) { |
| 1155 | if (msg->received_time() != 0) { |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 1156 | std::string output_params; |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 1157 | LogParam(reply_params, &output_params); |
| 1158 | msg->set_output_params(output_params); |
| 1159 | } |
| 1160 | } |
| 1161 | |
| 1162 | inline void ConnectMessageAndReply(const Message* msg, Message* reply) { |
| 1163 | if (msg->sent_time()) { |
| 1164 | // Don't log the sync message after dispatch, as we don't have the |
| 1165 | // output parameters at that point. Instead, save its data and log it |
| 1166 | // with the outgoing reply message when it's sent. |
| 1167 | LogData* data = new LogData; |
sammc | a1b3b4d | 2016-11-15 00:34:36 | [diff] [blame] | 1168 | GenerateLogData(*msg, data, true); |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 1169 | msg->set_dont_log(); |
| 1170 | reply->set_sync_log_data(data); |
| 1171 | } |
| 1172 | } |
| 1173 | #else |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 1174 | inline void AddOutputParamsToLog(const Message* msg, std::string* l) {} |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 1175 | |
| 1176 | template <class ReplyParamType> |
| 1177 | inline void LogReplyParamsToMessage(const ReplyParamType& reply_params, |
| 1178 | const Message* msg) {} |
| 1179 | |
| 1180 | inline void ConnectMessageAndReply(const Message* msg, Message* reply) {} |
| 1181 | #endif |
| 1182 | |
[email protected] | 3178f4e2 | 2008-08-05 21:20:41 | [diff] [blame] | 1183 | } // namespace IPC |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1184 | |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 1185 | #endif // IPC_IPC_MESSAGE_UTILS_H_ |