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