Avi Drissman | ea1be23 | 2022-09-14 23:29:06 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Arthur Sonzogni | 9967fb92 | 2024-07-18 18:36:22 | [diff] [blame] | 5 | #ifdef UNSAFE_BUFFERS_BUILD |
| 6 | // TODO(crbug.com/351564777): Remove this and convert code to safer constructs. |
| 7 | #pragma allow_unsafe_buffers |
| 8 | #endif |
| 9 | |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 10 | #include "ipc/ipc_message_utils.h" |
| 11 | |
avi | 246998d8 | 2015-12-22 02:39:04 | [diff] [blame] | 12 | #include <stddef.h> |
| 13 | #include <stdint.h> |
| 14 | |
Hyowon Kim | af11d36 | 2023-12-21 02:15:09 | [diff] [blame] | 15 | #include <string_view> |
Adam Rice | c62d805 | 2023-08-03 05:48:44 | [diff] [blame] | 16 | #include <type_traits> |
| 17 | |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 18 | #include "base/files/file_path.h" |
[email protected] | 93d49d7 | 2009-10-23 20:00:20 | [diff] [blame] | 19 | #include "base/json/json_writer.h" |
Hans Wennborg | 2439759 | 2020-06-17 16:58:50 | [diff] [blame] | 20 | #include "base/logging.h" |
jdoerrie | f1e72e3 | 2017-04-26 16:23:55 | [diff] [blame] | 21 | #include "base/memory/ptr_util.h" |
Hans Wennborg | 5f62968b | 2021-04-22 18:44:05 | [diff] [blame] | 22 | #include "base/notreached.h" |
[email protected] | 4aa794a1 | 2013-06-11 06:32:18 | [diff] [blame] | 23 | #include "base/strings/string_number_conversions.h" |
Lei Zhang | e02299a | 2021-04-26 23:12:24 | [diff] [blame] | 24 | #include "base/strings/stringprintf.h" |
[email protected] | 90626587 | 2013-06-07 22:40:45 | [diff] [blame] | 25 | #include "base/strings/utf_string_conversions.h" |
[email protected] | b43e556 | 2013-06-28 15:20:02 | [diff] [blame] | 26 | #include "base/time/time.h" |
tguilbert | 4a5ac60 | 2016-09-19 21:11:25 | [diff] [blame] | 27 | #include "base/unguessable_token.h" |
avi | 246998d8 | 2015-12-22 02:39:04 | [diff] [blame] | 28 | #include "build/build_config.h" |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 29 | #include "ipc/ipc_channel_handle.h" |
Lei Zhang | 5226db7 | 2022-01-25 03:38:55 | [diff] [blame] | 30 | #include "ipc/ipc_message.h" |
morrita | 1aa788c | 2015-01-31 05:45:42 | [diff] [blame] | 31 | #include "ipc/ipc_message_attachment.h" |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 32 | #include "ipc/ipc_message_attachment_set.h" |
amistry | 3618252 | 2016-06-27 06:34:42 | [diff] [blame] | 33 | #include "ipc/ipc_mojo_param_traits.h" |
Peter Kasting | a9813a5 | 2023-08-03 18:50:43 | [diff] [blame] | 34 | #include "third_party/abseil-cpp/absl/strings/ascii.h" |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 35 | |
Ali Juma | dff201a | 2022-05-31 23:51:41 | [diff] [blame] | 36 | #if BUILDFLAG(IS_APPLE) |
erikchen | af8299d | 2015-10-09 19:12:06 | [diff] [blame] | 37 | #include "ipc/mach_port_mac.h" |
| 38 | #endif |
| 39 | |
Xiaohan Wang | ab909b3 | 2022-01-12 17:57:39 | [diff] [blame] | 40 | #if BUILDFLAG(IS_WIN) |
[email protected] | 2e02cfe8 | 2012-11-21 00:58:00 | [diff] [blame] | 41 | #include <tchar.h> |
Takuto Ikuta | c8d6b16f | 2024-04-15 16:59:19 | [diff] [blame] | 42 | |
erikchen | 5ea2ab7 | 2015-09-25 22:34:31 | [diff] [blame] | 43 | #include "ipc/handle_win.h" |
erikchen | d804e105 | 2017-04-29 02:24:36 | [diff] [blame] | 44 | #include "ipc/ipc_platform_file.h" |
Xiaohan Wang | ab909b3 | 2022-01-12 17:57:39 | [diff] [blame] | 45 | #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) |
Fabrice de Gans-Riberi | cbce434 | 2018-05-07 20:02:09 | [diff] [blame] | 46 | #include "base/file_descriptor_posix.h" |
| 47 | #include "ipc/ipc_platform_file_attachment_posix.h" |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 48 | #endif |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 49 | |
Xiaohan Wang | ab909b3 | 2022-01-12 17:57:39 | [diff] [blame] | 50 | #if BUILDFLAG(IS_FUCHSIA) |
Sergey Ulanov | 04367c7 | 2019-06-21 15:51:43 | [diff] [blame] | 51 | #include "base/fuchsia/fuchsia_logging.h" |
Sergey Ulanov | a97f328 | 2019-04-17 01:27:34 | [diff] [blame] | 52 | #include "ipc/handle_attachment_fuchsia.h" |
Scott Graham | 3eebff0 | 2017-06-30 01:07:10 | [diff] [blame] | 53 | #endif |
| 54 | |
Xiaohan Wang | ab909b3 | 2022-01-12 17:57:39 | [diff] [blame] | 55 | #if BUILDFLAG(IS_ANDROID) |
Ken Rockot | 097248f0 | 2018-04-23 16:23:34 | [diff] [blame] | 56 | #include "base/android/scoped_hardware_buffer_handle.h" |
| 57 | #include "ipc/ipc_mojo_handle_attachment.h" |
| 58 | #include "mojo/public/cpp/system/message_pipe.h" |
| 59 | #include "mojo/public/cpp/system/scope_to_message_pipe.h" |
| 60 | #endif |
| 61 | |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 62 | namespace IPC { |
| 63 | |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 64 | namespace { |
| 65 | |
joaodasilva | 383b174a | 2017-01-10 09:55:36 | [diff] [blame] | 66 | const int kMaxRecursionDepth = 200; |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 67 | |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 68 | template<typename CharType> |
| 69 | void LogBytes(const std::vector<CharType>& data, std::string* out) { |
Xiaohan Wang | ab909b3 | 2022-01-12 17:57:39 | [diff] [blame] | 70 | #if BUILDFLAG(IS_WIN) |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 71 | // Windows has a GUI for logging, which can handle arbitrary binary data. |
| 72 | for (size_t i = 0; i < data.size(); ++i) |
| 73 | out->push_back(data[i]); |
Xiaohan Wang | ab909b3 | 2022-01-12 17:57:39 | [diff] [blame] | 74 | #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 75 | // On POSIX, we log to stdout, which we assume can display ASCII. |
| 76 | static const size_t kMaxBytesToLog = 100; |
| 77 | for (size_t i = 0; i < std::min(data.size(), kMaxBytesToLog); ++i) { |
Peter Kasting | a9813a5 | 2023-08-03 18:50:43 | [diff] [blame] | 78 | if (absl::ascii_isprint(static_cast<unsigned char>(data[i]))) { |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 79 | out->push_back(data[i]); |
Peter Kasting | a9813a5 | 2023-08-03 18:50:43 | [diff] [blame] | 80 | } else { |
[email protected] | 7d3cbc9 | 2013-03-18 22:33:04 | [diff] [blame] | 81 | out->append( |
| 82 | base::StringPrintf("[%02X]", static_cast<unsigned char>(data[i]))); |
Peter Kasting | a9813a5 | 2023-08-03 18:50:43 | [diff] [blame] | 83 | } |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 84 | } |
| 85 | if (data.size() > kMaxBytesToLog) { |
[email protected] | f8660f8 | 2013-03-30 17:29:28 | [diff] [blame] | 86 | out->append(base::StringPrintf( |
| 87 | " and %u more bytes", |
| 88 | static_cast<unsigned>(data.size() - kMaxBytesToLog))); |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 89 | } |
| 90 | #endif |
| 91 | } |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 92 | |
Adam Rice | c62d805 | 2023-08-03 05:48:44 | [diff] [blame] | 93 | template <typename CharType> |
| 94 | void WriteCharVector(base::Pickle* m, const std::vector<CharType>& p) { |
| 95 | static_assert(sizeof(CharType) == 1); |
| 96 | static_assert(std::is_integral_v<CharType>); |
| 97 | if (p.empty()) { |
| 98 | m->WriteData(nullptr, 0); |
| 99 | } else { |
| 100 | const char* data = reinterpret_cast<const char*>(p.data()); |
| 101 | m->WriteData(data, p.size()); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | template <typename CharType> |
| 106 | bool ReadCharVector(const base::Pickle* m, |
| 107 | base::PickleIterator* iter, |
| 108 | std::vector<CharType>* r) { |
| 109 | static_assert(sizeof(CharType) == 1); |
| 110 | static_assert(std::is_integral_v<CharType>); |
| 111 | const char* data; |
| 112 | size_t data_size = 0; |
| 113 | if (!iter->ReadData(&data, &data_size)) { |
| 114 | return false; |
| 115 | } |
| 116 | const CharType* begin = reinterpret_cast<const CharType*>(data); |
| 117 | const CharType* end = begin + data_size; |
| 118 | r->assign(begin, end); |
| 119 | return true; |
| 120 | } |
| 121 | |
Fabrice de Gans | 43eebf70 | 2022-04-01 23:43:04 | [diff] [blame] | 122 | void WriteValue(const base::Value& value, int recursion, base::Pickle* pickle); |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 123 | |
Fabrice de Gans | 43eebf70 | 2022-04-01 23:43:04 | [diff] [blame] | 124 | void WriteDictValue(const base::Value::Dict& value, |
| 125 | int recursion, |
| 126 | base::Pickle* pickle) { |
| 127 | WriteParam(pickle, base::checked_cast<int>(value.size())); |
| 128 | for (const auto entry : value) { |
| 129 | WriteParam(pickle, entry.first); |
| 130 | WriteValue(entry.second, recursion + 1, pickle); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | void WriteListValue(const base::Value::List& value, |
| 135 | int recursion, |
| 136 | base::Pickle* pickle) { |
| 137 | WriteParam(pickle, base::checked_cast<int>(value.size())); |
| 138 | for (const auto& entry : value) { |
| 139 | WriteValue(entry, recursion + 1, pickle); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | void WriteValue(const base::Value& value, int recursion, base::Pickle* pickle) { |
[email protected] | dbc761a | 2012-07-26 01:29:21 | [diff] [blame] | 144 | bool result; |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 145 | if (recursion > kMaxRecursionDepth) { |
joaodasilva | 383b174a | 2017-01-10 09:55:36 | [diff] [blame] | 146 | LOG(ERROR) << "Max recursion depth hit in WriteValue."; |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 147 | return; |
| 148 | } |
| 149 | |
Fabrice de Gans | 43eebf70 | 2022-04-01 23:43:04 | [diff] [blame] | 150 | pickle->WriteInt(static_cast<int>(value.type())); |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 151 | |
Fabrice de Gans | 43eebf70 | 2022-04-01 23:43:04 | [diff] [blame] | 152 | switch (value.type()) { |
jdoerrie | dc72ee94 | 2016-12-07 15:43:28 | [diff] [blame] | 153 | case base::Value::Type::NONE: |
jdoerrie | e1b1f3a | 2019-03-16 04:08:01 | [diff] [blame] | 154 | break; |
jdoerrie | dc72ee94 | 2016-12-07 15:43:28 | [diff] [blame] | 155 | case base::Value::Type::BOOLEAN: { |
Fabrice de Gans | 43eebf70 | 2022-04-01 23:43:04 | [diff] [blame] | 156 | WriteParam(pickle, value.GetBool()); |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 157 | break; |
| 158 | } |
jdoerrie | dc72ee94 | 2016-12-07 15:43:28 | [diff] [blame] | 159 | case base::Value::Type::INTEGER: { |
Fabrice de Gans | 43eebf70 | 2022-04-01 23:43:04 | [diff] [blame] | 160 | DCHECK(value.is_int()); |
| 161 | WriteParam(pickle, value.GetInt()); |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 162 | break; |
| 163 | } |
jdoerrie | dc72ee94 | 2016-12-07 15:43:28 | [diff] [blame] | 164 | case base::Value::Type::DOUBLE: { |
Fabrice de Gans | 43eebf70 | 2022-04-01 23:43:04 | [diff] [blame] | 165 | DCHECK(value.is_int() || value.is_double()); |
| 166 | WriteParam(pickle, value.GetDouble()); |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 167 | break; |
| 168 | } |
jdoerrie | dc72ee94 | 2016-12-07 15:43:28 | [diff] [blame] | 169 | case base::Value::Type::STRING: { |
Fabrice de Gans | 43eebf70 | 2022-04-01 23:43:04 | [diff] [blame] | 170 | const std::string* val = value.GetIfString(); |
Sammie Quon | dab84eba | 2021-08-18 02:34:48 | [diff] [blame] | 171 | result = !!val; |
[email protected] | dbc761a | 2012-07-26 01:29:21 | [diff] [blame] | 172 | DCHECK(result); |
Fabrice de Gans | 43eebf70 | 2022-04-01 23:43:04 | [diff] [blame] | 173 | WriteParam(pickle, *val); |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 174 | break; |
| 175 | } |
jdoerrie | dc72ee94 | 2016-12-07 15:43:28 | [diff] [blame] | 176 | case base::Value::Type::BINARY: { |
Fabrice de Gans | 43eebf70 | 2022-04-01 23:43:04 | [diff] [blame] | 177 | pickle->WriteData(reinterpret_cast<const char*>(value.GetBlob().data()), |
Peter Kasting | 28b51cf | 2022-06-28 15:02:43 | [diff] [blame] | 178 | value.GetBlob().size()); |
[email protected] | e4dad9fb | 2009-10-06 18:15:58 | [diff] [blame] | 179 | break; |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 180 | } |
Fabrice de Gans | 43eebf70 | 2022-04-01 23:43:04 | [diff] [blame] | 181 | case base::Value::Type::DICT: { |
| 182 | DCHECK(value.is_dict()); |
| 183 | WriteDictValue(value.GetDict(), recursion, pickle); |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 184 | break; |
| 185 | } |
jdoerrie | dc72ee94 | 2016-12-07 15:43:28 | [diff] [blame] | 186 | case base::Value::Type::LIST: { |
Fabrice de Gans | 43eebf70 | 2022-04-01 23:43:04 | [diff] [blame] | 187 | DCHECK(value.is_list()); |
| 188 | WriteListValue(value.GetList(), recursion, pickle); |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 189 | break; |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | |
Fabrice de Gans | 43eebf70 | 2022-04-01 23:43:04 | [diff] [blame] | 194 | bool ReadValue(const base::Pickle* pickle, |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 195 | base::PickleIterator* iter, |
Fabrice de Gans | 43eebf70 | 2022-04-01 23:43:04 | [diff] [blame] | 196 | int recursion, |
| 197 | base::Value* value); |
| 198 | |
| 199 | // Helper for ReadValue that reads a Value::Dict into a pre-allocated object. |
| 200 | bool ReadDictValue(const base::Pickle* pickle, |
| 201 | base::PickleIterator* iter, |
| 202 | int recursion, |
| 203 | base::Value::Dict* value) { |
| 204 | int size; |
| 205 | if (!ReadParam(pickle, iter, &size)) |
| 206 | return false; |
| 207 | |
| 208 | for (int i = 0; i < size; i++) { |
| 209 | std::string key; |
| 210 | base::Value subvalue; |
| 211 | if (!ReadParam(pickle, iter, &key) || |
| 212 | !ReadValue(pickle, iter, recursion + 1, &subvalue)) { |
| 213 | return false; |
| 214 | } |
| 215 | value->Set(key, std::move(subvalue)); |
| 216 | } |
| 217 | |
| 218 | return true; |
| 219 | } |
| 220 | |
| 221 | // Helper for ReadValue that reads a Value::List into a pre-allocated object. |
| 222 | bool ReadListValue(const base::Pickle* pickle, |
| 223 | base::PickleIterator* iter, |
| 224 | int recursion, |
| 225 | base::Value::List* value) { |
| 226 | int size; |
| 227 | if (!ReadParam(pickle, iter, &size)) |
| 228 | return false; |
| 229 | |
| 230 | value->reserve(size); |
| 231 | for (int i = 0; i < size; i++) { |
| 232 | base::Value subval; |
| 233 | if (!ReadValue(pickle, iter, recursion + 1, &subval)) |
| 234 | return false; |
| 235 | value->Append(std::move(subval)); |
| 236 | } |
| 237 | return true; |
| 238 | } |
| 239 | |
| 240 | bool ReadValue(const base::Pickle* pickle, |
| 241 | base::PickleIterator* iter, |
| 242 | int recursion, |
| 243 | base::Value* value) { |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 244 | if (recursion > kMaxRecursionDepth) { |
joaodasilva | 383b174a | 2017-01-10 09:55:36 | [diff] [blame] | 245 | LOG(ERROR) << "Max recursion depth hit in ReadValue."; |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 246 | return false; |
| 247 | } |
| 248 | |
| 249 | int type; |
Fabrice de Gans | 43eebf70 | 2022-04-01 23:43:04 | [diff] [blame] | 250 | if (!ReadParam(pickle, iter, &type)) |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 251 | return false; |
| 252 | |
Devlin Cronin | 7178a5bd | 2021-02-02 02:56:47 | [diff] [blame] | 253 | constexpr int kMinValueType = static_cast<int>(base::Value::Type::NONE); |
| 254 | constexpr int kMaxValueType = static_cast<int>(base::Value::Type::LIST); |
| 255 | if (type > kMaxValueType || type < kMinValueType) |
| 256 | return false; |
| 257 | |
jdoerrie | dc72ee94 | 2016-12-07 15:43:28 | [diff] [blame] | 258 | switch (static_cast<base::Value::Type>(type)) { |
| 259 | case base::Value::Type::NONE: |
Jeremy Roman | 2d8d780 | 2020-06-11 22:08:20 | [diff] [blame] | 260 | *value = base::Value(); |
jdoerrie | e067999a | 2017-04-07 06:39:00 | [diff] [blame] | 261 | break; |
jdoerrie | dc72ee94 | 2016-12-07 15:43:28 | [diff] [blame] | 262 | case base::Value::Type::BOOLEAN: { |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 263 | bool val; |
Fabrice de Gans | 43eebf70 | 2022-04-01 23:43:04 | [diff] [blame] | 264 | if (!ReadParam(pickle, iter, &val)) |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 265 | return false; |
Jeremy Roman | 2d8d780 | 2020-06-11 22:08:20 | [diff] [blame] | 266 | *value = base::Value(val); |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 267 | break; |
| 268 | } |
jdoerrie | dc72ee94 | 2016-12-07 15:43:28 | [diff] [blame] | 269 | case base::Value::Type::INTEGER: { |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 270 | int val; |
Fabrice de Gans | 43eebf70 | 2022-04-01 23:43:04 | [diff] [blame] | 271 | if (!ReadParam(pickle, iter, &val)) |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 272 | return false; |
Jeremy Roman | 2d8d780 | 2020-06-11 22:08:20 | [diff] [blame] | 273 | *value = base::Value(val); |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 274 | break; |
| 275 | } |
jdoerrie | dc72ee94 | 2016-12-07 15:43:28 | [diff] [blame] | 276 | case base::Value::Type::DOUBLE: { |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 277 | double val; |
Fabrice de Gans | 43eebf70 | 2022-04-01 23:43:04 | [diff] [blame] | 278 | if (!ReadParam(pickle, iter, &val)) |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 279 | return false; |
Jeremy Roman | 2d8d780 | 2020-06-11 22:08:20 | [diff] [blame] | 280 | *value = base::Value(val); |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 281 | break; |
| 282 | } |
jdoerrie | dc72ee94 | 2016-12-07 15:43:28 | [diff] [blame] | 283 | case base::Value::Type::STRING: { |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 284 | std::string val; |
Fabrice de Gans | 43eebf70 | 2022-04-01 23:43:04 | [diff] [blame] | 285 | if (!ReadParam(pickle, iter, &val)) |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 286 | return false; |
Jeremy Roman | 2d8d780 | 2020-06-11 22:08:20 | [diff] [blame] | 287 | *value = base::Value(std::move(val)); |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 288 | break; |
| 289 | } |
jdoerrie | dc72ee94 | 2016-12-07 15:43:28 | [diff] [blame] | 290 | case base::Value::Type::BINARY: { |
Arthur Sonzogni | 59ac822 | 2023-11-10 09:46:54 | [diff] [blame] | 291 | std::optional<base::span<const uint8_t>> data = iter->ReadData(); |
Claudio DeSouza | c537f85d | 2023-03-02 18:42:52 | [diff] [blame] | 292 | if (!data) { |
[email protected] | e4dad9fb | 2009-10-06 18:15:58 | [diff] [blame] | 293 | return false; |
Claudio DeSouza | c537f85d | 2023-03-02 18:42:52 | [diff] [blame] | 294 | } |
| 295 | *value = base::Value(*data); |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 296 | break; |
| 297 | } |
Avi Drissman | d2a736f | 2023-01-28 02:31:26 | [diff] [blame] | 298 | case base::Value::Type::DICT: { |
Fabrice de Gans | 43eebf70 | 2022-04-01 23:43:04 | [diff] [blame] | 299 | base::Value::Dict val; |
| 300 | if (!ReadDictValue(pickle, iter, recursion, &val)) |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 301 | return false; |
Fabrice de Gans | 43eebf70 | 2022-04-01 23:43:04 | [diff] [blame] | 302 | *value = base::Value(std::move(val)); |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 303 | break; |
| 304 | } |
jdoerrie | dc72ee94 | 2016-12-07 15:43:28 | [diff] [blame] | 305 | case base::Value::Type::LIST: { |
Fabrice de Gans | 43eebf70 | 2022-04-01 23:43:04 | [diff] [blame] | 306 | base::Value::List val; |
| 307 | if (!ReadListValue(pickle, iter, recursion, &val)) |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 308 | return false; |
Fabrice de Gans | 43eebf70 | 2022-04-01 23:43:04 | [diff] [blame] | 309 | *value = base::Value(std::move(val)); |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 310 | break; |
| 311 | } |
[email protected] | e4dad9fb | 2009-10-06 18:15:58 | [diff] [blame] | 312 | default: |
Peter Boström | b7e3e0824 | 2024-09-24 22:38:10 | [diff] [blame] | 313 | NOTREACHED(); |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | return true; |
| 317 | } |
| 318 | |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 319 | } // namespace |
| 320 | |
| 321 | // ----------------------------------------------------------------------------- |
| 322 | |
| 323 | LogData::LogData() |
| 324 | : routing_id(0), |
| 325 | type(0), |
| 326 | sent(0), |
| 327 | receive(0), |
| 328 | dispatch(0) { |
| 329 | } |
| 330 | |
vmpstr | bf0d713a | 2016-03-24 20:22:54 | [diff] [blame] | 331 | LogData::LogData(const LogData& other) = default; |
| 332 | |
Chris Watkins | 2d879af | 2017-11-30 02:11:59 | [diff] [blame] | 333 | LogData::~LogData() = default; |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 334 | |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 335 | void ParamTraits<bool>::Log(const param_type& p, std::string* l) { |
| 336 | l->append(p ? "true" : "false"); |
| 337 | } |
| 338 | |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 339 | void ParamTraits<signed char>::Write(base::Pickle* m, const param_type& p) { |
ortuno | 19ecf184 | 2015-10-30 00:46:20 | [diff] [blame] | 340 | m->WriteBytes(&p, sizeof(param_type)); |
| 341 | } |
| 342 | |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 343 | bool ParamTraits<signed char>::Read(const base::Pickle* m, |
| 344 | base::PickleIterator* iter, |
| 345 | param_type* r) { |
ortuno | 19ecf184 | 2015-10-30 00:46:20 | [diff] [blame] | 346 | const char* data; |
| 347 | if (!iter->ReadBytes(&data, sizeof(param_type))) |
| 348 | return false; |
| 349 | memcpy(r, data, sizeof(param_type)); |
| 350 | return true; |
| 351 | } |
| 352 | |
| 353 | void ParamTraits<signed char>::Log(const param_type& p, std::string* l) { |
Raul Tambre | f89a510 | 2019-02-08 23:01:38 | [diff] [blame] | 354 | l->append(base::NumberToString(p)); |
ortuno | 19ecf184 | 2015-10-30 00:46:20 | [diff] [blame] | 355 | } |
| 356 | |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 357 | void ParamTraits<unsigned char>::Write(base::Pickle* m, const param_type& p) { |
[email protected] | c1ee48d | 2013-07-12 23:12:28 | [diff] [blame] | 358 | m->WriteBytes(&p, sizeof(param_type)); |
| 359 | } |
| 360 | |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 361 | bool ParamTraits<unsigned char>::Read(const base::Pickle* m, |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 362 | base::PickleIterator* iter, |
| 363 | param_type* r) { |
[email protected] | c1ee48d | 2013-07-12 23:12:28 | [diff] [blame] | 364 | const char* data; |
avi | 48fc13b | 2014-12-28 23:31:48 | [diff] [blame] | 365 | if (!iter->ReadBytes(&data, sizeof(param_type))) |
[email protected] | c1ee48d | 2013-07-12 23:12:28 | [diff] [blame] | 366 | return false; |
| 367 | memcpy(r, data, sizeof(param_type)); |
| 368 | return true; |
| 369 | } |
| 370 | |
| 371 | void ParamTraits<unsigned char>::Log(const param_type& p, std::string* l) { |
Raul Tambre | f89a510 | 2019-02-08 23:01:38 | [diff] [blame] | 372 | l->append(base::NumberToString(p)); |
[email protected] | c1ee48d | 2013-07-12 23:12:28 | [diff] [blame] | 373 | } |
| 374 | |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 375 | void ParamTraits<unsigned short>::Write(base::Pickle* m, const param_type& p) { |
[email protected] | c1ee48d | 2013-07-12 23:12:28 | [diff] [blame] | 376 | m->WriteBytes(&p, sizeof(param_type)); |
| 377 | } |
| 378 | |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 379 | bool ParamTraits<unsigned short>::Read(const base::Pickle* m, |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 380 | base::PickleIterator* iter, |
[email protected] | c1ee48d | 2013-07-12 23:12:28 | [diff] [blame] | 381 | param_type* r) { |
| 382 | const char* data; |
avi | 48fc13b | 2014-12-28 23:31:48 | [diff] [blame] | 383 | if (!iter->ReadBytes(&data, sizeof(param_type))) |
[email protected] | c1ee48d | 2013-07-12 23:12:28 | [diff] [blame] | 384 | return false; |
| 385 | memcpy(r, data, sizeof(param_type)); |
| 386 | return true; |
| 387 | } |
| 388 | |
| 389 | void ParamTraits<unsigned short>::Log(const param_type& p, std::string* l) { |
Daniel Cheng | 3d199b1 | 2017-12-12 03:51:09 | [diff] [blame] | 390 | l->append(base::NumberToString(p)); |
[email protected] | c1ee48d | 2013-07-12 23:12:28 | [diff] [blame] | 391 | } |
| 392 | |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 393 | void ParamTraits<int>::Log(const param_type& p, std::string* l) { |
Daniel Cheng | 3d199b1 | 2017-12-12 03:51:09 | [diff] [blame] | 394 | l->append(base::NumberToString(p)); |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | void ParamTraits<unsigned int>::Log(const param_type& p, std::string* l) { |
Daniel Cheng | 3d199b1 | 2017-12-12 03:51:09 | [diff] [blame] | 398 | l->append(base::NumberToString(p)); |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 399 | } |
| 400 | |
Xiaohan Wang | ab909b3 | 2022-01-12 17:57:39 | [diff] [blame] | 401 | #if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || \ |
| 402 | BUILDFLAG(IS_FUCHSIA) || \ |
| 403 | (BUILDFLAG(IS_ANDROID) && defined(ARCH_CPU_64_BITS)) |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 404 | void ParamTraits<long>::Log(const param_type& p, std::string* l) { |
Daniel Cheng | 3d199b1 | 2017-12-12 03:51:09 | [diff] [blame] | 405 | l->append(base::NumberToString(p)); |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | void ParamTraits<unsigned long>::Log(const param_type& p, std::string* l) { |
Daniel Cheng | 3d199b1 | 2017-12-12 03:51:09 | [diff] [blame] | 409 | l->append(base::NumberToString(p)); |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 410 | } |
jam | 03d8a78 | 2016-02-10 20:13:39 | [diff] [blame] | 411 | #endif |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 412 | |
| 413 | void ParamTraits<long long>::Log(const param_type& p, std::string* l) { |
Daniel Cheng | 3d199b1 | 2017-12-12 03:51:09 | [diff] [blame] | 414 | l->append(base::NumberToString(p)); |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | void ParamTraits<unsigned long long>::Log(const param_type& p, std::string* l) { |
Daniel Cheng | 3d199b1 | 2017-12-12 03:51:09 | [diff] [blame] | 418 | l->append(base::NumberToString(p)); |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 419 | } |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 420 | |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 421 | void ParamTraits<float>::Log(const param_type& p, std::string* l) { |
[email protected] | 7d3cbc9 | 2013-03-18 22:33:04 | [diff] [blame] | 422 | l->append(base::StringPrintf("%e", p)); |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 423 | } |
| 424 | |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 425 | void ParamTraits<double>::Write(base::Pickle* m, const param_type& p) { |
[email protected] | 48328ff | 2013-10-31 09:27:31 | [diff] [blame] | 426 | m->WriteBytes(reinterpret_cast<const char*>(&p), sizeof(param_type)); |
[email protected] | d84e48b | 2010-10-21 22:04:52 | [diff] [blame] | 427 | } |
| 428 | |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 429 | bool ParamTraits<double>::Read(const base::Pickle* m, |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 430 | base::PickleIterator* iter, |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 431 | param_type* r) { |
| 432 | const char *data; |
avi | 48fc13b | 2014-12-28 23:31:48 | [diff] [blame] | 433 | if (!iter->ReadBytes(&data, sizeof(*r))) { |
Peter Boström | b7e3e0824 | 2024-09-24 22:38:10 | [diff] [blame] | 434 | NOTREACHED(); |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 435 | } |
| 436 | memcpy(r, data, sizeof(param_type)); |
| 437 | return true; |
[email protected] | d84e48b | 2010-10-21 22:04:52 | [diff] [blame] | 438 | } |
| 439 | |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 440 | void ParamTraits<double>::Log(const param_type& p, std::string* l) { |
[email protected] | 7d3cbc9 | 2013-03-18 22:33:04 | [diff] [blame] | 441 | l->append(base::StringPrintf("%e", p)); |
[email protected] | 1d14f58 | 2011-09-02 20:42:04 | [diff] [blame] | 442 | } |
| 443 | |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 444 | |
| 445 | void ParamTraits<std::string>::Log(const param_type& p, std::string* l) { |
| 446 | l->append(p); |
[email protected] | 1d14f58 | 2011-09-02 20:42:04 | [diff] [blame] | 447 | } |
| 448 | |
Jan Wilken Dörrie | 739ccc21 | 2021-03-11 18:13:05 | [diff] [blame] | 449 | void ParamTraits<std::u16string>::Log(const param_type& p, std::string* l) { |
[email protected] | ad65a3e | 2013-12-25 18:18:01 | [diff] [blame] | 450 | l->append(base::UTF16ToUTF8(p)); |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 451 | } |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 452 | |
Xiaohan Wang | ab909b3 | 2022-01-12 17:57:39 | [diff] [blame] | 453 | #if BUILDFLAG(IS_WIN) |
Jan Wilken Dörrie | cf5a6690 | 2020-12-07 20:51:31 | [diff] [blame] | 454 | bool ParamTraits<std::wstring>::Read(const base::Pickle* m, |
| 455 | base::PickleIterator* iter, |
| 456 | param_type* r) { |
Hyowon Kim | 6f3d96c | 2023-12-01 08:16:20 | [diff] [blame] | 457 | std::u16string_view piece16; |
Jan Wilken Dörrie | cf5a6690 | 2020-12-07 20:51:31 | [diff] [blame] | 458 | if (!iter->ReadStringPiece16(&piece16)) |
| 459 | return false; |
| 460 | |
| 461 | *r = base::AsWString(piece16); |
| 462 | return true; |
| 463 | } |
| 464 | |
| 465 | void ParamTraits<std::wstring>::Log(const param_type& p, std::string* l) { |
| 466 | l->append(base::WideToUTF8(p)); |
| 467 | } |
| 468 | #endif |
| 469 | |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 470 | void ParamTraits<std::vector<char>>::Write(base::Pickle* m, |
| 471 | const param_type& p) { |
Adam Rice | c62d805 | 2023-08-03 05:48:44 | [diff] [blame] | 472 | WriteCharVector(m, p); |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 473 | } |
| 474 | |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 475 | bool ParamTraits<std::vector<char>>::Read(const base::Pickle* m, |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 476 | base::PickleIterator* iter, |
| 477 | param_type* r) { |
Adam Rice | c62d805 | 2023-08-03 05:48:44 | [diff] [blame] | 478 | return ReadCharVector(m, iter, r); |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 479 | } |
| 480 | |
Peter Kasting | a9813a5 | 2023-08-03 18:50:43 | [diff] [blame] | 481 | void ParamTraits<std::vector<char>>::Log(const param_type& p, std::string* l) { |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 482 | LogBytes(p, l); |
| 483 | } |
| 484 | |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 485 | void ParamTraits<std::vector<unsigned char>>::Write(base::Pickle* m, |
| 486 | const param_type& p) { |
Adam Rice | c62d805 | 2023-08-03 05:48:44 | [diff] [blame] | 487 | WriteCharVector(m, p); |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 488 | } |
| 489 | |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 490 | bool ParamTraits<std::vector<unsigned char>>::Read(const base::Pickle* m, |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 491 | base::PickleIterator* iter, |
| 492 | param_type* r) { |
Adam Rice | c62d805 | 2023-08-03 05:48:44 | [diff] [blame] | 493 | return ReadCharVector(m, iter, r); |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | void ParamTraits<std::vector<unsigned char> >::Log(const param_type& p, |
| 497 | std::string* l) { |
| 498 | LogBytes(p, l); |
| 499 | } |
| 500 | |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 501 | void ParamTraits<std::vector<bool>>::Write(base::Pickle* m, |
| 502 | const param_type& p) { |
Chris Palmer | c5ea9b9 | 2017-09-25 22:53:22 | [diff] [blame] | 503 | WriteParam(m, base::checked_cast<int>(p.size())); |
[email protected] | d412485 | 2013-03-20 20:25:00 | [diff] [blame] | 504 | // Cast to bool below is required because libc++'s |
| 505 | // vector<bool>::const_reference is different from bool, and we want to avoid |
| 506 | // writing an extra specialization of ParamTraits for it. |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 507 | for (size_t i = 0; i < p.size(); i++) |
[email protected] | d412485 | 2013-03-20 20:25:00 | [diff] [blame] | 508 | WriteParam(m, static_cast<bool>(p[i])); |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 509 | } |
| 510 | |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 511 | bool ParamTraits<std::vector<bool>>::Read(const base::Pickle* m, |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 512 | base::PickleIterator* iter, |
| 513 | param_type* r) { |
Peter Kasting | 28b51cf | 2022-06-28 15:02:43 | [diff] [blame] | 514 | size_t size; |
avi | 48fc13b | 2014-12-28 23:31:48 | [diff] [blame] | 515 | if (!iter->ReadLength(&size)) |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 516 | return false; |
| 517 | r->resize(size); |
Peter Kasting | 28b51cf | 2022-06-28 15:02:43 | [diff] [blame] | 518 | for (size_t i = 0; i < size; i++) { |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 519 | bool value; |
| 520 | if (!ReadParam(m, iter, &value)) |
| 521 | return false; |
| 522 | (*r)[i] = value; |
| 523 | } |
| 524 | return true; |
| 525 | } |
| 526 | |
| 527 | void ParamTraits<std::vector<bool> >::Log(const param_type& p, std::string* l) { |
| 528 | for (size_t i = 0; i < p.size(); ++i) { |
| 529 | if (i != 0) |
| 530 | l->push_back(' '); |
[email protected] | d412485 | 2013-03-20 20:25:00 | [diff] [blame] | 531 | LogParam(static_cast<bool>(p[i]), l); |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 532 | } |
[email protected] | d84e48b | 2010-10-21 22:04:52 | [diff] [blame] | 533 | } |
| 534 | |
Fabrice de Gans | 43eebf70 | 2022-04-01 23:43:04 | [diff] [blame] | 535 | void ParamTraits<base::Value::Dict>::Write(base::Pickle* m, |
| 536 | const param_type& p) { |
| 537 | WriteDictValue(p, 0, m); |
| 538 | } |
| 539 | |
| 540 | bool ParamTraits<base::Value::Dict>::Read(const base::Pickle* m, |
| 541 | base::PickleIterator* iter, |
| 542 | param_type* r) { |
| 543 | return ReadDictValue(m, iter, 0, r); |
| 544 | } |
| 545 | |
| 546 | void ParamTraits<base::Value::Dict>::Log(const param_type& p, std::string* l) { |
| 547 | std::string json; |
| 548 | base::JSONWriter::Write(p, &json); |
| 549 | l->append(json); |
| 550 | } |
| 551 | |
Xiaohan Wang | ab909b3 | 2022-01-12 17:57:39 | [diff] [blame] | 552 | #if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 553 | void ParamTraits<base::FileDescriptor>::Write(base::Pickle* m, |
| 554 | const param_type& p) { |
erikchen | 1452520 | 2017-05-06 19:16:51 | [diff] [blame] | 555 | // This serialization must be kept in sync with |
erikchen | 9d6afd71 | 2017-05-18 17:49:06 | [diff] [blame] | 556 | // nacl_message_scanner.cc:WriteHandle(). |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 557 | const bool valid = p.fd >= 0; |
| 558 | WriteParam(m, valid); |
| 559 | |
morrita | 9669385 | 2014-09-24 20:11:45 | [diff] [blame] | 560 | if (!valid) |
| 561 | return; |
| 562 | |
| 563 | if (p.auto_close) { |
morrita | 1aa788c | 2015-01-31 05:45:42 | [diff] [blame] | 564 | if (!m->WriteAttachment( |
| 565 | new internal::PlatformFileAttachment(base::ScopedFD(p.fd)))) |
Peter Boström | b7e3e0824 | 2024-09-24 22:38:10 | [diff] [blame] | 566 | NOTREACHED(); |
morrita | 9669385 | 2014-09-24 20:11:45 | [diff] [blame] | 567 | } else { |
morrita | 1aa788c | 2015-01-31 05:45:42 | [diff] [blame] | 568 | if (!m->WriteAttachment(new internal::PlatformFileAttachment(p.fd))) |
Peter Boström | b7e3e0824 | 2024-09-24 22:38:10 | [diff] [blame] | 569 | NOTREACHED(); |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 570 | } |
| 571 | } |
| 572 | |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 573 | bool ParamTraits<base::FileDescriptor>::Read(const base::Pickle* m, |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 574 | base::PickleIterator* iter, |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 575 | param_type* r) { |
morrita | 9669385 | 2014-09-24 20:11:45 | [diff] [blame] | 576 | *r = base::FileDescriptor(); |
| 577 | |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 578 | bool valid; |
| 579 | if (!ReadParam(m, iter, &valid)) |
| 580 | return false; |
| 581 | |
morrita | 9669385 | 2014-09-24 20:11:45 | [diff] [blame] | 582 | if (!valid) |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 583 | return true; |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 584 | |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 585 | scoped_refptr<base::Pickle::Attachment> attachment; |
morrita | 1aa788c | 2015-01-31 05:45:42 | [diff] [blame] | 586 | if (!m->ReadAttachment(iter, &attachment)) |
morrita | 9669385 | 2014-09-24 20:11:45 | [diff] [blame] | 587 | return false; |
| 588 | |
sammc | 6ed3efb | 2016-11-23 03:17:35 | [diff] [blame] | 589 | if (static_cast<MessageAttachment*>(attachment.get())->GetType() != |
| 590 | MessageAttachment::Type::PLATFORM_FILE) { |
| 591 | return false; |
| 592 | } |
| 593 | |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 594 | *r = base::FileDescriptor( |
sammc | 6ed3efb | 2016-11-23 03:17:35 | [diff] [blame] | 595 | static_cast<internal::PlatformFileAttachment*>(attachment.get()) |
| 596 | ->TakePlatformFile(), |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 597 | true); |
morrita | 9669385 | 2014-09-24 20:11:45 | [diff] [blame] | 598 | return true; |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | void ParamTraits<base::FileDescriptor>::Log(const param_type& p, |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 602 | std::string* l) { |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 603 | if (p.auto_close) { |
[email protected] | 7d3cbc9 | 2013-03-18 22:33:04 | [diff] [blame] | 604 | l->append(base::StringPrintf("FD(%d auto-close)", p.fd)); |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 605 | } else { |
[email protected] | 7d3cbc9 | 2013-03-18 22:33:04 | [diff] [blame] | 606 | l->append(base::StringPrintf("FD(%d)", p.fd)); |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 607 | } |
| 608 | } |
Sergey Ulanov | ac89bb9 | 2019-03-22 18:46:49 | [diff] [blame] | 609 | |
| 610 | void ParamTraits<base::ScopedFD>::Write(base::Pickle* m, const param_type& p) { |
| 611 | // This serialization must be kept in sync with |
| 612 | // nacl_message_scanner.cc:WriteHandle(). |
| 613 | const bool valid = p.is_valid(); |
| 614 | WriteParam(m, valid); |
| 615 | |
| 616 | if (!valid) |
| 617 | return; |
| 618 | |
| 619 | if (!m->WriteAttachment(new internal::PlatformFileAttachment( |
| 620 | std::move(const_cast<param_type&>(p))))) { |
Peter Boström | b7e3e0824 | 2024-09-24 22:38:10 | [diff] [blame] | 621 | NOTREACHED(); |
Sergey Ulanov | ac89bb9 | 2019-03-22 18:46:49 | [diff] [blame] | 622 | } |
| 623 | } |
| 624 | |
| 625 | bool ParamTraits<base::ScopedFD>::Read(const base::Pickle* m, |
| 626 | base::PickleIterator* iter, |
| 627 | param_type* r) { |
| 628 | r->reset(); |
| 629 | |
| 630 | bool valid; |
| 631 | if (!ReadParam(m, iter, &valid)) |
| 632 | return false; |
| 633 | |
| 634 | if (!valid) |
| 635 | return true; |
| 636 | |
| 637 | scoped_refptr<base::Pickle::Attachment> attachment; |
| 638 | if (!m->ReadAttachment(iter, &attachment)) |
| 639 | return false; |
| 640 | |
| 641 | if (static_cast<MessageAttachment*>(attachment.get())->GetType() != |
| 642 | MessageAttachment::Type::PLATFORM_FILE) { |
| 643 | return false; |
| 644 | } |
| 645 | |
| 646 | *r = base::ScopedFD( |
| 647 | static_cast<internal::PlatformFileAttachment*>(attachment.get()) |
| 648 | ->TakePlatformFile()); |
| 649 | return true; |
| 650 | } |
| 651 | |
| 652 | void ParamTraits<base::ScopedFD>::Log(const param_type& p, std::string* l) { |
| 653 | l->append(base::StringPrintf("ScopedFD(%d)", p.get())); |
| 654 | } |
Xiaohan Wang | ab909b3 | 2022-01-12 17:57:39 | [diff] [blame] | 655 | #endif // BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 656 | |
Xiaohan Wang | ab909b3 | 2022-01-12 17:57:39 | [diff] [blame] | 657 | #if BUILDFLAG(IS_WIN) |
Robert Sesek | 0291066 | 2020-02-28 20:15:51 | [diff] [blame] | 658 | void ParamTraits<base::win::ScopedHandle>::Write(base::Pickle* m, |
| 659 | const param_type& p) { |
| 660 | const bool valid = p.IsValid(); |
| 661 | WriteParam(m, valid); |
| 662 | if (!valid) |
| 663 | return; |
| 664 | |
| 665 | HandleWin handle(p.Get()); |
| 666 | WriteParam(m, handle); |
| 667 | } |
| 668 | |
| 669 | bool ParamTraits<base::win::ScopedHandle>::Read(const base::Pickle* m, |
| 670 | base::PickleIterator* iter, |
| 671 | param_type* r) { |
| 672 | r->Close(); |
| 673 | |
| 674 | bool valid; |
| 675 | if (!ReadParam(m, iter, &valid)) |
| 676 | return false; |
| 677 | if (!valid) |
| 678 | return true; |
| 679 | |
| 680 | HandleWin handle; |
| 681 | if (!ReadParam(m, iter, &handle)) |
| 682 | return false; |
| 683 | |
| 684 | r->Set(handle.get_handle()); |
| 685 | return true; |
| 686 | } |
| 687 | |
| 688 | void ParamTraits<base::win::ScopedHandle>::Log(const param_type& p, |
| 689 | std::string* l) { |
| 690 | l->append(base::StringPrintf("ScopedHandle(%p)", p.Get())); |
| 691 | } |
Xiaohan Wang | ab909b3 | 2022-01-12 17:57:39 | [diff] [blame] | 692 | #endif // BUILDFLAG(IS_WIN) |
Robert Sesek | 0291066 | 2020-02-28 20:15:51 | [diff] [blame] | 693 | |
Xiaohan Wang | ab909b3 | 2022-01-12 17:57:39 | [diff] [blame] | 694 | #if BUILDFLAG(IS_FUCHSIA) |
Sergey Ulanov | a97f328 | 2019-04-17 01:27:34 | [diff] [blame] | 695 | void ParamTraits<zx::vmo>::Write(base::Pickle* m, const param_type& p) { |
| 696 | // This serialization must be kept in sync with |
| 697 | // nacl_message_scanner.cc:WriteHandle(). |
| 698 | const bool valid = p.is_valid(); |
| 699 | WriteParam(m, valid); |
| 700 | |
| 701 | if (!valid) |
| 702 | return; |
| 703 | |
| 704 | if (!m->WriteAttachment(new internal::HandleAttachmentFuchsia( |
Sergey Ulanov | 04367c7 | 2019-06-21 15:51:43 | [diff] [blame] | 705 | std::move(const_cast<param_type&>(p))))) { |
Peter Boström | b7e3e0824 | 2024-09-24 22:38:10 | [diff] [blame] | 706 | NOTREACHED(); |
Sergey Ulanov | a97f328 | 2019-04-17 01:27:34 | [diff] [blame] | 707 | } |
| 708 | } |
| 709 | |
| 710 | bool ParamTraits<zx::vmo>::Read(const base::Pickle* m, |
| 711 | base::PickleIterator* iter, |
| 712 | param_type* r) { |
| 713 | r->reset(); |
| 714 | |
| 715 | bool valid; |
| 716 | if (!ReadParam(m, iter, &valid)) |
| 717 | return false; |
| 718 | |
| 719 | if (!valid) |
| 720 | return true; |
| 721 | |
| 722 | scoped_refptr<base::Pickle::Attachment> attachment; |
| 723 | if (!m->ReadAttachment(iter, &attachment)) |
| 724 | return false; |
| 725 | |
| 726 | if (static_cast<MessageAttachment*>(attachment.get())->GetType() != |
| 727 | MessageAttachment::Type::FUCHSIA_HANDLE) { |
| 728 | return false; |
| 729 | } |
| 730 | |
| 731 | *r = zx::vmo(static_cast<internal::HandleAttachmentFuchsia*>(attachment.get()) |
| 732 | ->Take()); |
| 733 | return true; |
| 734 | } |
| 735 | |
| 736 | void ParamTraits<zx::vmo>::Log(const param_type& p, std::string* l) { |
| 737 | l->append("ZirconVMO"); |
| 738 | } |
Sergey Ulanov | 7b343ff | 2019-08-15 07:52:37 | [diff] [blame] | 739 | |
| 740 | void ParamTraits<zx::channel>::Write(base::Pickle* m, const param_type& p) { |
| 741 | // This serialization must be kept in sync with |
| 742 | // nacl_message_scanner.cc:WriteHandle(). |
| 743 | const bool valid = p.is_valid(); |
| 744 | WriteParam(m, valid); |
| 745 | |
| 746 | if (!valid) |
| 747 | return; |
| 748 | |
| 749 | if (!m->WriteAttachment(new internal::HandleAttachmentFuchsia( |
| 750 | std::move(const_cast<param_type&>(p))))) { |
Peter Boström | b7e3e0824 | 2024-09-24 22:38:10 | [diff] [blame] | 751 | NOTREACHED(); |
Sergey Ulanov | 7b343ff | 2019-08-15 07:52:37 | [diff] [blame] | 752 | } |
| 753 | } |
| 754 | |
| 755 | bool ParamTraits<zx::channel>::Read(const base::Pickle* m, |
| 756 | base::PickleIterator* iter, |
| 757 | param_type* r) { |
| 758 | r->reset(); |
| 759 | |
| 760 | bool valid; |
| 761 | if (!ReadParam(m, iter, &valid)) |
| 762 | return false; |
| 763 | |
| 764 | if (!valid) |
| 765 | return true; |
| 766 | |
| 767 | scoped_refptr<base::Pickle::Attachment> attachment; |
| 768 | if (!m->ReadAttachment(iter, &attachment)) |
| 769 | return false; |
| 770 | |
| 771 | if (static_cast<MessageAttachment*>(attachment.get())->GetType() != |
| 772 | MessageAttachment::Type::FUCHSIA_HANDLE) { |
| 773 | return false; |
| 774 | } |
| 775 | |
| 776 | *r = zx::channel( |
| 777 | static_cast<internal::HandleAttachmentFuchsia*>(attachment.get()) |
| 778 | ->Take()); |
| 779 | return true; |
| 780 | } |
| 781 | |
| 782 | void ParamTraits<zx::channel>::Log(const param_type& p, std::string* l) { |
| 783 | l->append("ZirconChannel"); |
| 784 | } |
Xiaohan Wang | ab909b3 | 2022-01-12 17:57:39 | [diff] [blame] | 785 | #endif // BUILDFLAG(IS_FUCHSIA) |
Sergey Ulanov | a97f328 | 2019-04-17 01:27:34 | [diff] [blame] | 786 | |
Xiaohan Wang | ab909b3 | 2022-01-12 17:57:39 | [diff] [blame] | 787 | #if BUILDFLAG(IS_ANDROID) |
Alexandr Ilin | 0443a8f | 2018-07-20 20:14:50 | [diff] [blame] | 788 | void ParamTraits<base::android::ScopedHardwareBufferHandle>::Write( |
| 789 | base::Pickle* m, |
| 790 | const param_type& p) { |
| 791 | const bool is_valid = p.is_valid(); |
Ken Rockot | 097248f0 | 2018-04-23 16:23:34 | [diff] [blame] | 792 | WriteParam(m, is_valid); |
| 793 | if (!is_valid) |
| 794 | return; |
| 795 | |
Ken Rockot | 097248f0 | 2018-04-23 16:23:34 | [diff] [blame] | 796 | // We must keep a ref to the AHardwareBuffer alive until the receiver has |
| 797 | // acquired its own reference. We do this by sending a message pipe handle |
| 798 | // along with the buffer. When the receiver deserializes (or even if they |
| 799 | // die without ever reading the message) their end of the pipe will be |
| 800 | // closed. We will eventually detect this and release the AHB reference. |
| 801 | mojo::MessagePipe tracking_pipe; |
| 802 | m->WriteAttachment(new internal::MojoHandleAttachment( |
| 803 | mojo::ScopedHandle::From(std::move(tracking_pipe.handle0)))); |
Alexandr Ilin | 0443a8f | 2018-07-20 20:14:50 | [diff] [blame] | 804 | WriteParam(m, base::FileDescriptor(p.SerializeAsFileDescriptor().release(), |
| 805 | true /* auto_close */)); |
Ken Rockot | 097248f0 | 2018-04-23 16:23:34 | [diff] [blame] | 806 | |
| 807 | // Pass ownership of the input handle to our tracking pipe to keep the AHB |
| 808 | // alive long enough to be deserialized by the receiver. |
Alexandr Ilin | 0443a8f | 2018-07-20 20:14:50 | [diff] [blame] | 809 | mojo::ScopeToMessagePipe(std::move(const_cast<param_type&>(p)), |
| 810 | std::move(tracking_pipe.handle1)); |
Klaus Weidner | 3824a888 | 2017-11-03 06:24:57 | [diff] [blame] | 811 | } |
| 812 | |
Alexandr Ilin | 0443a8f | 2018-07-20 20:14:50 | [diff] [blame] | 813 | bool ParamTraits<base::android::ScopedHardwareBufferHandle>::Read( |
| 814 | const base::Pickle* m, |
| 815 | base::PickleIterator* iter, |
| 816 | param_type* r) { |
| 817 | *r = base::android::ScopedHardwareBufferHandle(); |
Ken Rockot | 097248f0 | 2018-04-23 16:23:34 | [diff] [blame] | 818 | |
| 819 | bool is_valid; |
| 820 | if (!ReadParam(m, iter, &is_valid)) |
Klaus Weidner | 3824a888 | 2017-11-03 06:24:57 | [diff] [blame] | 821 | return false; |
Ken Rockot | 097248f0 | 2018-04-23 16:23:34 | [diff] [blame] | 822 | if (!is_valid) |
| 823 | return true; |
| 824 | |
| 825 | scoped_refptr<base::Pickle::Attachment> tracking_pipe_attachment; |
| 826 | if (!m->ReadAttachment(iter, &tracking_pipe_attachment)) |
Klaus Weidner | 3824a888 | 2017-11-03 06:24:57 | [diff] [blame] | 827 | return false; |
Ken Rockot | 097248f0 | 2018-04-23 16:23:34 | [diff] [blame] | 828 | |
| 829 | // We keep this alive until the AHB is safely deserialized below. When this |
| 830 | // goes out of scope, the sender holding the other end of this pipe will treat |
| 831 | // this handle closure as a signal that it's safe to release their AHB |
| 832 | // keepalive ref. |
| 833 | mojo::ScopedHandle tracking_pipe = |
| 834 | static_cast<MessageAttachment*>(tracking_pipe_attachment.get()) |
| 835 | ->TakeMojoHandle(); |
| 836 | |
| 837 | base::FileDescriptor descriptor; |
| 838 | if (!ReadParam(m, iter, &descriptor)) |
| 839 | return false; |
| 840 | |
| 841 | // NOTE: It is valid to deserialize an invalid FileDescriptor, so the success |
| 842 | // of |ReadParam()| above does not imply that |descriptor| is valid. |
| 843 | base::ScopedFD scoped_fd(descriptor.fd); |
| 844 | if (!scoped_fd.is_valid()) |
| 845 | return false; |
| 846 | |
| 847 | *r = base::android::ScopedHardwareBufferHandle::DeserializeFromFileDescriptor( |
Alexandr Ilin | 0443a8f | 2018-07-20 20:14:50 | [diff] [blame] | 848 | std::move(scoped_fd)); |
Klaus Weidner | 3824a888 | 2017-11-03 06:24:57 | [diff] [blame] | 849 | return true; |
| 850 | } |
| 851 | |
Alexandr Ilin | 0443a8f | 2018-07-20 20:14:50 | [diff] [blame] | 852 | void ParamTraits<base::android::ScopedHardwareBufferHandle>::Log( |
| 853 | const param_type& p, |
| 854 | std::string* l) { |
| 855 | l->append(base::StringPrintf("base::android::ScopedHardwareBufferHandle(%p)", |
| 856 | p.get())); |
Klaus Weidner | 3824a888 | 2017-11-03 06:24:57 | [diff] [blame] | 857 | } |
Xiaohan Wang | ab909b3 | 2022-01-12 17:57:39 | [diff] [blame] | 858 | #endif // BUILDFLAG(IS_ANDROID) |
Klaus Weidner | 3824a888 | 2017-11-03 06:24:57 | [diff] [blame] | 859 | |
Alexandr Ilin | d497eee | 2018-04-19 22:50:54 | [diff] [blame] | 860 | void ParamTraits<base::ReadOnlySharedMemoryRegion>::Write(base::Pickle* m, |
| 861 | const param_type& p) { |
| 862 | base::subtle::PlatformSharedMemoryRegion handle = |
| 863 | base::ReadOnlySharedMemoryRegion::TakeHandleForSerialization( |
| 864 | std::move(const_cast<param_type&>(p))); |
| 865 | WriteParam(m, std::move(handle)); |
| 866 | } |
| 867 | |
| 868 | bool ParamTraits<base::ReadOnlySharedMemoryRegion>::Read( |
| 869 | const base::Pickle* m, |
| 870 | base::PickleIterator* iter, |
| 871 | param_type* r) { |
| 872 | base::subtle::PlatformSharedMemoryRegion handle; |
| 873 | if (!ReadParam(m, iter, &handle)) |
| 874 | return false; |
| 875 | |
| 876 | *r = base::ReadOnlySharedMemoryRegion::Deserialize(std::move(handle)); |
| 877 | return true; |
| 878 | } |
| 879 | |
| 880 | void ParamTraits<base::ReadOnlySharedMemoryRegion>::Log(const param_type& p, |
| 881 | std::string* l) { |
| 882 | *l = "<base::ReadOnlySharedMemoryRegion>"; |
| 883 | // TODO(alexilin): currently there is no way to access underlying handle |
| 884 | // without destructing a ReadOnlySharedMemoryRegion instance. |
| 885 | } |
| 886 | |
| 887 | void ParamTraits<base::WritableSharedMemoryRegion>::Write(base::Pickle* m, |
| 888 | const param_type& p) { |
| 889 | base::subtle::PlatformSharedMemoryRegion handle = |
| 890 | base::WritableSharedMemoryRegion::TakeHandleForSerialization( |
| 891 | std::move(const_cast<param_type&>(p))); |
| 892 | WriteParam(m, std::move(handle)); |
| 893 | } |
| 894 | |
| 895 | bool ParamTraits<base::WritableSharedMemoryRegion>::Read( |
| 896 | const base::Pickle* m, |
| 897 | base::PickleIterator* iter, |
| 898 | param_type* r) { |
| 899 | base::subtle::PlatformSharedMemoryRegion handle; |
| 900 | if (!ReadParam(m, iter, &handle)) |
| 901 | return false; |
| 902 | |
| 903 | *r = base::WritableSharedMemoryRegion::Deserialize(std::move(handle)); |
| 904 | return true; |
| 905 | } |
| 906 | |
| 907 | void ParamTraits<base::WritableSharedMemoryRegion>::Log(const param_type& p, |
| 908 | std::string* l) { |
| 909 | *l = "<base::WritableSharedMemoryRegion>"; |
| 910 | // TODO(alexilin): currently there is no way to access underlying handle |
| 911 | // without destructing a ReadOnlySharedMemoryRegion instance. |
| 912 | } |
| 913 | |
| 914 | void ParamTraits<base::UnsafeSharedMemoryRegion>::Write(base::Pickle* m, |
| 915 | const param_type& p) { |
| 916 | base::subtle::PlatformSharedMemoryRegion handle = |
| 917 | base::UnsafeSharedMemoryRegion::TakeHandleForSerialization( |
| 918 | std::move(const_cast<param_type&>(p))); |
| 919 | WriteParam(m, std::move(handle)); |
| 920 | } |
| 921 | |
| 922 | bool ParamTraits<base::UnsafeSharedMemoryRegion>::Read( |
| 923 | const base::Pickle* m, |
| 924 | base::PickleIterator* iter, |
| 925 | param_type* r) { |
| 926 | base::subtle::PlatformSharedMemoryRegion handle; |
| 927 | if (!ReadParam(m, iter, &handle)) |
| 928 | return false; |
| 929 | |
| 930 | *r = base::UnsafeSharedMemoryRegion::Deserialize(std::move(handle)); |
| 931 | return true; |
| 932 | } |
| 933 | |
| 934 | void ParamTraits<base::UnsafeSharedMemoryRegion>::Log(const param_type& p, |
| 935 | std::string* l) { |
| 936 | *l = "<base::UnsafeSharedMemoryRegion>"; |
| 937 | // TODO(alexilin): currently there is no way to access underlying handle |
| 938 | // without destructing a ReadOnlySharedMemoryRegion instance. |
| 939 | } |
| 940 | |
| 941 | void ParamTraits<base::subtle::PlatformSharedMemoryRegion>::Write( |
| 942 | base::Pickle* m, |
| 943 | const param_type& p) { |
Alexandr Ilin | ebab9da | 2018-06-01 02:37:47 | [diff] [blame] | 944 | // This serialization must be kept in sync with |
| 945 | // nacl_message_scanner.cc::WriteHandle(). |
Alexandr Ilin | d497eee | 2018-04-19 22:50:54 | [diff] [blame] | 946 | const bool valid = p.IsValid(); |
| 947 | WriteParam(m, valid); |
| 948 | |
| 949 | if (!valid) |
| 950 | return; |
| 951 | |
| 952 | WriteParam(m, p.GetMode()); |
| 953 | WriteParam(m, static_cast<uint64_t>(p.GetSize())); |
| 954 | WriteParam(m, p.GetGUID()); |
| 955 | |
Xiaohan Wang | ab909b3 | 2022-01-12 17:57:39 | [diff] [blame] | 956 | #if BUILDFLAG(IS_WIN) |
Fabrice de Gans-Riberi | cbce434 | 2018-05-07 20:02:09 | [diff] [blame] | 957 | base::win::ScopedHandle h = const_cast<param_type&>(p).PassPlatformHandle(); |
Alexandr Ilin | 6544b8a | 2018-08-15 23:08:54 | [diff] [blame] | 958 | HandleWin handle_win(h.Get()); |
Fabrice de Gans-Riberi | cbce434 | 2018-05-07 20:02:09 | [diff] [blame] | 959 | WriteParam(m, handle_win); |
Xiaohan Wang | ab909b3 | 2022-01-12 17:57:39 | [diff] [blame] | 960 | #elif BUILDFLAG(IS_FUCHSIA) |
Sergey Ulanov | 04367c7 | 2019-06-21 15:51:43 | [diff] [blame] | 961 | zx::vmo vmo = const_cast<param_type&>(p).PassPlatformHandle(); |
| 962 | WriteParam(m, vmo); |
Ali Juma | dff201a | 2022-05-31 23:51:41 | [diff] [blame] | 963 | #elif BUILDFLAG(IS_APPLE) |
Avi Drissman | dd92b53 | 2023-08-16 22:26:17 | [diff] [blame] | 964 | base::apple::ScopedMachSendRight h = |
Fabrice de Gans-Riberi | cbce434 | 2018-05-07 20:02:09 | [diff] [blame] | 965 | const_cast<param_type&>(p).PassPlatformHandle(); |
Alexandr Ilin | 6544b8a | 2018-08-15 23:08:54 | [diff] [blame] | 966 | MachPortMac mach_port_mac(h.get()); |
Fabrice de Gans-Riberi | cbce434 | 2018-05-07 20:02:09 | [diff] [blame] | 967 | WriteParam(m, mach_port_mac); |
Xiaohan Wang | ab909b3 | 2022-01-12 17:57:39 | [diff] [blame] | 968 | #elif BUILDFLAG(IS_ANDROID) |
Alexandr Ilin | d497eee | 2018-04-19 22:50:54 | [diff] [blame] | 969 | m->WriteAttachment(new internal::PlatformFileAttachment( |
| 970 | base::ScopedFD(const_cast<param_type&>(p).PassPlatformHandle()))); |
Xiaohan Wang | ab909b3 | 2022-01-12 17:57:39 | [diff] [blame] | 971 | #elif BUILDFLAG(IS_POSIX) |
Alexandr Ilin | d497eee | 2018-04-19 22:50:54 | [diff] [blame] | 972 | base::subtle::ScopedFDPair h = |
| 973 | const_cast<param_type&>(p).PassPlatformHandle(); |
| 974 | m->WriteAttachment(new internal::PlatformFileAttachment(std::move(h.fd))); |
| 975 | if (p.GetMode() == |
| 976 | base::subtle::PlatformSharedMemoryRegion::Mode::kWritable) { |
| 977 | m->WriteAttachment( |
| 978 | new internal::PlatformFileAttachment(std::move(h.readonly_fd))); |
| 979 | } |
| 980 | #endif |
| 981 | } |
| 982 | |
| 983 | bool ParamTraits<base::subtle::PlatformSharedMemoryRegion>::Read( |
| 984 | const base::Pickle* m, |
| 985 | base::PickleIterator* iter, |
| 986 | param_type* r) { |
| 987 | bool valid; |
| 988 | if (!ReadParam(m, iter, &valid)) |
| 989 | return false; |
| 990 | if (!valid) { |
| 991 | *r = base::subtle::PlatformSharedMemoryRegion(); |
| 992 | return true; |
| 993 | } |
| 994 | |
| 995 | base::subtle::PlatformSharedMemoryRegion::Mode mode; |
| 996 | uint64_t shm_size; |
| 997 | base::UnguessableToken guid; |
| 998 | if (!ReadParam(m, iter, &mode) || !ReadParam(m, iter, &shm_size) || |
| 999 | !base::IsValueInRangeForNumericType<size_t>(shm_size) || |
| 1000 | !ReadParam(m, iter, &guid)) { |
| 1001 | return false; |
| 1002 | } |
| 1003 | size_t size = static_cast<size_t>(shm_size); |
| 1004 | |
Xiaohan Wang | ab909b3 | 2022-01-12 17:57:39 | [diff] [blame] | 1005 | #if BUILDFLAG(IS_WIN) |
Fabrice de Gans-Riberi | cbce434 | 2018-05-07 20:02:09 | [diff] [blame] | 1006 | HandleWin handle_win; |
| 1007 | if (!ReadParam(m, iter, &handle_win)) |
Alexandr Ilin | d497eee | 2018-04-19 22:50:54 | [diff] [blame] | 1008 | return false; |
| 1009 | *r = base::subtle::PlatformSharedMemoryRegion::Take( |
Fabrice de Gans-Riberi | cbce434 | 2018-05-07 20:02:09 | [diff] [blame] | 1010 | base::win::ScopedHandle(handle_win.get_handle()), mode, size, guid); |
Xiaohan Wang | ab909b3 | 2022-01-12 17:57:39 | [diff] [blame] | 1011 | #elif BUILDFLAG(IS_FUCHSIA) |
Sergey Ulanov | 04367c7 | 2019-06-21 15:51:43 | [diff] [blame] | 1012 | zx::vmo vmo; |
| 1013 | if (!ReadParam(m, iter, &vmo)) |
Alexandr Ilin | d497eee | 2018-04-19 22:50:54 | [diff] [blame] | 1014 | return false; |
Sergey Ulanov | 04367c7 | 2019-06-21 15:51:43 | [diff] [blame] | 1015 | *r = base::subtle::PlatformSharedMemoryRegion::Take(std::move(vmo), mode, |
| 1016 | size, guid); |
Ali Juma | dff201a | 2022-05-31 23:51:41 | [diff] [blame] | 1017 | #elif BUILDFLAG(IS_APPLE) |
Fabrice de Gans-Riberi | cbce434 | 2018-05-07 20:02:09 | [diff] [blame] | 1018 | MachPortMac mach_port_mac; |
| 1019 | if (!ReadParam(m, iter, &mach_port_mac)) |
Alexandr Ilin | d497eee | 2018-04-19 22:50:54 | [diff] [blame] | 1020 | return false; |
| 1021 | *r = base::subtle::PlatformSharedMemoryRegion::Take( |
Avi Drissman | dd92b53 | 2023-08-16 22:26:17 | [diff] [blame] | 1022 | base::apple::ScopedMachSendRight(mach_port_mac.get_mach_port()), mode, |
| 1023 | size, guid); |
Xiaohan Wang | ab909b3 | 2022-01-12 17:57:39 | [diff] [blame] | 1024 | #elif BUILDFLAG(IS_POSIX) |
Alexandr Ilin | d497eee | 2018-04-19 22:50:54 | [diff] [blame] | 1025 | scoped_refptr<base::Pickle::Attachment> attachment; |
| 1026 | if (!m->ReadAttachment(iter, &attachment)) |
| 1027 | return false; |
| 1028 | if (static_cast<MessageAttachment*>(attachment.get())->GetType() != |
| 1029 | MessageAttachment::Type::PLATFORM_FILE) { |
| 1030 | return false; |
| 1031 | } |
| 1032 | |
Xiaohan Wang | ab909b3 | 2022-01-12 17:57:39 | [diff] [blame] | 1033 | #if BUILDFLAG(IS_ANDROID) |
Alexandr Ilin | d497eee | 2018-04-19 22:50:54 | [diff] [blame] | 1034 | *r = base::subtle::PlatformSharedMemoryRegion::Take( |
| 1035 | base::ScopedFD( |
| 1036 | static_cast<internal::PlatformFileAttachment*>(attachment.get()) |
| 1037 | ->TakePlatformFile()), |
| 1038 | mode, size, guid); |
| 1039 | #else |
| 1040 | scoped_refptr<base::Pickle::Attachment> readonly_attachment; |
| 1041 | if (mode == base::subtle::PlatformSharedMemoryRegion::Mode::kWritable) { |
| 1042 | if (!m->ReadAttachment(iter, &readonly_attachment)) |
| 1043 | return false; |
| 1044 | |
| 1045 | if (static_cast<MessageAttachment*>(readonly_attachment.get())->GetType() != |
| 1046 | MessageAttachment::Type::PLATFORM_FILE) { |
| 1047 | return false; |
| 1048 | } |
| 1049 | } |
| 1050 | *r = base::subtle::PlatformSharedMemoryRegion::Take( |
| 1051 | base::subtle::ScopedFDPair( |
| 1052 | base::ScopedFD( |
| 1053 | static_cast<internal::PlatformFileAttachment*>(attachment.get()) |
| 1054 | ->TakePlatformFile()), |
| 1055 | readonly_attachment |
| 1056 | ? base::ScopedFD(static_cast<internal::PlatformFileAttachment*>( |
| 1057 | readonly_attachment.get()) |
| 1058 | ->TakePlatformFile()) |
| 1059 | : base::ScopedFD()), |
| 1060 | mode, size, guid); |
Xiaohan Wang | ab909b3 | 2022-01-12 17:57:39 | [diff] [blame] | 1061 | #endif // BUILDFLAG(IS_ANDROID) |
Alexandr Ilin | d497eee | 2018-04-19 22:50:54 | [diff] [blame] | 1062 | |
| 1063 | #endif |
| 1064 | |
| 1065 | return true; |
| 1066 | } |
| 1067 | |
| 1068 | void ParamTraits<base::subtle::PlatformSharedMemoryRegion>::Log( |
| 1069 | const param_type& p, |
| 1070 | std::string* l) { |
Xiaohan Wang | ab909b3 | 2022-01-12 17:57:39 | [diff] [blame] | 1071 | #if BUILDFLAG(IS_FUCHSIA) |
Wez | b908e43 | 2018-09-05 17:35:22 | [diff] [blame] | 1072 | l->append("Handle: "); |
| 1073 | LogParam(p.GetPlatformHandle()->get(), l); |
Xiaohan Wang | ab909b3 | 2022-01-12 17:57:39 | [diff] [blame] | 1074 | #elif BUILDFLAG(IS_WIN) |
Alexandr Ilin | d497eee | 2018-04-19 22:50:54 | [diff] [blame] | 1075 | l->append("Handle: "); |
| 1076 | LogParam(p.GetPlatformHandle(), l); |
Ali Juma | dff201a | 2022-05-31 23:51:41 | [diff] [blame] | 1077 | #elif BUILDFLAG(IS_APPLE) |
Fabrice de Gans-Riberi | cbce434 | 2018-05-07 20:02:09 | [diff] [blame] | 1078 | l->append("Mach port: "); |
| 1079 | LogParam(p.GetPlatformHandle(), l); |
Xiaohan Wang | ab909b3 | 2022-01-12 17:57:39 | [diff] [blame] | 1080 | #elif BUILDFLAG(IS_ANDROID) |
Alexandr Ilin | d497eee | 2018-04-19 22:50:54 | [diff] [blame] | 1081 | l->append("FD: "); |
| 1082 | LogParam(p.GetPlatformHandle(), l); |
Xiaohan Wang | ab909b3 | 2022-01-12 17:57:39 | [diff] [blame] | 1083 | #elif BUILDFLAG(IS_POSIX) |
Alexandr Ilin | d497eee | 2018-04-19 22:50:54 | [diff] [blame] | 1084 | base::subtle::FDPair h = p.GetPlatformHandle(); |
| 1085 | l->append("FD: "); |
| 1086 | LogParam(h.fd, l); |
| 1087 | l->append("Read-only FD: "); |
| 1088 | LogParam(h.readonly_fd, l); |
| 1089 | #endif |
| 1090 | |
| 1091 | l->append("Mode: "); |
| 1092 | LogParam(p.GetMode(), l); |
| 1093 | l->append("size: "); |
| 1094 | LogParam(static_cast<uint64_t>(p.GetSize()), l); |
| 1095 | l->append("GUID: "); |
| 1096 | LogParam(p.GetGUID(), l); |
| 1097 | } |
| 1098 | |
| 1099 | void ParamTraits<base::subtle::PlatformSharedMemoryRegion::Mode>::Write( |
| 1100 | base::Pickle* m, |
| 1101 | const param_type& value) { |
| 1102 | DCHECK(static_cast<int>(value) >= 0 && |
| 1103 | static_cast<int>(value) <= static_cast<int>(param_type::kMaxValue)); |
| 1104 | m->WriteInt(static_cast<int>(value)); |
| 1105 | } |
| 1106 | |
| 1107 | bool ParamTraits<base::subtle::PlatformSharedMemoryRegion::Mode>::Read( |
| 1108 | const base::Pickle* m, |
| 1109 | base::PickleIterator* iter, |
| 1110 | param_type* p) { |
| 1111 | int value; |
| 1112 | if (!iter->ReadInt(&value)) |
| 1113 | return false; |
| 1114 | if (!(static_cast<int>(value) >= 0 && |
| 1115 | static_cast<int>(value) <= static_cast<int>(param_type::kMaxValue))) { |
| 1116 | return false; |
| 1117 | } |
| 1118 | *p = static_cast<param_type>(value); |
| 1119 | return true; |
| 1120 | } |
| 1121 | |
| 1122 | void ParamTraits<base::subtle::PlatformSharedMemoryRegion::Mode>::Log( |
| 1123 | const param_type& p, |
| 1124 | std::string* l) { |
| 1125 | LogParam(static_cast<int>(p), l); |
| 1126 | } |
| 1127 | |
Xiaohan Wang | ab909b3 | 2022-01-12 17:57:39 | [diff] [blame] | 1128 | #if BUILDFLAG(IS_WIN) |
erikchen | d804e105 | 2017-04-29 02:24:36 | [diff] [blame] | 1129 | void ParamTraits<PlatformFileForTransit>::Write(base::Pickle* m, |
| 1130 | const param_type& p) { |
| 1131 | m->WriteBool(p.IsValid()); |
| 1132 | if (p.IsValid()) { |
Wez | 51eaaad | 2017-08-09 05:51:38 | [diff] [blame] | 1133 | HandleWin handle_win(p.GetHandle()); |
erikchen | d804e105 | 2017-04-29 02:24:36 | [diff] [blame] | 1134 | ParamTraits<HandleWin>::Write(m, handle_win); |
| 1135 | ::CloseHandle(p.GetHandle()); |
| 1136 | } |
| 1137 | } |
| 1138 | |
| 1139 | bool ParamTraits<PlatformFileForTransit>::Read(const base::Pickle* m, |
| 1140 | base::PickleIterator* iter, |
| 1141 | param_type* r) { |
| 1142 | bool is_valid; |
| 1143 | if (!iter->ReadBool(&is_valid)) |
| 1144 | return false; |
| 1145 | if (!is_valid) { |
| 1146 | *r = PlatformFileForTransit(); |
| 1147 | return true; |
| 1148 | } |
| 1149 | |
| 1150 | HandleWin handle_win; |
| 1151 | if (!ParamTraits<HandleWin>::Read(m, iter, &handle_win)) |
| 1152 | return false; |
| 1153 | *r = PlatformFileForTransit(handle_win.get_handle()); |
| 1154 | return true; |
| 1155 | } |
| 1156 | |
| 1157 | void ParamTraits<PlatformFileForTransit>::Log(const param_type& p, |
| 1158 | std::string* l) { |
| 1159 | LogParam(p.GetHandle(), l); |
| 1160 | } |
Xiaohan Wang | ab909b3 | 2022-01-12 17:57:39 | [diff] [blame] | 1161 | #endif // BUILDFLAG(IS_WIN) |
erikchen | d804e105 | 2017-04-29 02:24:36 | [diff] [blame] | 1162 | |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 1163 | void ParamTraits<base::FilePath>::Write(base::Pickle* m, const param_type& p) { |
[email protected] | aeae59f | 2013-01-28 13:47:55 | [diff] [blame] | 1164 | p.WriteToPickle(m); |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1165 | } |
| 1166 | |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 1167 | bool ParamTraits<base::FilePath>::Read(const base::Pickle* m, |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 1168 | base::PickleIterator* iter, |
[email protected] | 6d4b67a | 2013-02-10 04:49:30 | [diff] [blame] | 1169 | param_type* r) { |
[email protected] | aeae59f | 2013-01-28 13:47:55 | [diff] [blame] | 1170 | return r->ReadFromPickle(iter); |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1171 | } |
| 1172 | |
[email protected] | 6d4b67a | 2013-02-10 04:49:30 | [diff] [blame] | 1173 | void ParamTraits<base::FilePath>::Log(const param_type& p, std::string* l) { |
| 1174 | ParamTraits<base::FilePath::StringType>::Log(p.value(), l); |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1175 | } |
| 1176 | |
Fabrice de Gans | 43eebf70 | 2022-04-01 23:43:04 | [diff] [blame] | 1177 | void ParamTraits<base::Value::List>::Write(base::Pickle* m, |
| 1178 | const param_type& p) { |
| 1179 | WriteListValue(p, 0, m); |
| 1180 | } |
| 1181 | |
| 1182 | bool ParamTraits<base::Value::List>::Read(const base::Pickle* m, |
| 1183 | base::PickleIterator* iter, |
| 1184 | param_type* r) { |
| 1185 | return ReadListValue(m, iter, 0, r); |
| 1186 | } |
| 1187 | |
| 1188 | void ParamTraits<base::Value::List>::Log(const param_type& p, std::string* l) { |
| 1189 | std::string json; |
| 1190 | base::JSONWriter::Write(p, &json); |
| 1191 | l->append(json); |
| 1192 | } |
| 1193 | |
Devlin Cronin | 7178a5bd | 2021-02-02 02:56:47 | [diff] [blame] | 1194 | void ParamTraits<base::Value>::Write(base::Pickle* m, const param_type& p) { |
Fabrice de Gans | 43eebf70 | 2022-04-01 23:43:04 | [diff] [blame] | 1195 | WriteValue(p, 0, m); |
Devlin Cronin | 7178a5bd | 2021-02-02 02:56:47 | [diff] [blame] | 1196 | } |
| 1197 | |
| 1198 | bool ParamTraits<base::Value>::Read(const base::Pickle* m, |
| 1199 | base::PickleIterator* iter, |
| 1200 | param_type* r) { |
Fabrice de Gans | 43eebf70 | 2022-04-01 23:43:04 | [diff] [blame] | 1201 | return ReadValue(m, iter, 0, r); |
Devlin Cronin | 7178a5bd | 2021-02-02 02:56:47 | [diff] [blame] | 1202 | } |
| 1203 | |
| 1204 | void ParamTraits<base::Value>::Log(const param_type& p, std::string* l) { |
| 1205 | std::string json; |
| 1206 | base::JSONWriter::Write(p, &json); |
| 1207 | l->append(json); |
| 1208 | } |
| 1209 | |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 1210 | void ParamTraits<base::File::Info>::Write(base::Pickle* m, |
[email protected] | 141bcc5 | 2014-01-27 21:36:00 | [diff] [blame] | 1211 | const param_type& p) { |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1212 | WriteParam(m, p.size); |
| 1213 | WriteParam(m, p.is_directory); |
Peter Kasting | 08b91b4 | 2023-10-21 03:46:09 | [diff] [blame] | 1214 | WriteParam(m, p.last_modified.InSecondsFSinceUnixEpoch()); |
| 1215 | WriteParam(m, p.last_accessed.InSecondsFSinceUnixEpoch()); |
| 1216 | WriteParam(m, p.creation_time.InSecondsFSinceUnixEpoch()); |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1217 | } |
| 1218 | |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 1219 | bool ParamTraits<base::File::Info>::Read(const base::Pickle* m, |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 1220 | base::PickleIterator* iter, |
[email protected] | 141bcc5 | 2014-01-27 21:36:00 | [diff] [blame] | 1221 | param_type* p) { |
[email protected] | 481c3e8 | 2014-07-18 01:40:47 | [diff] [blame] | 1222 | double last_modified, last_accessed, creation_time; |
| 1223 | if (!ReadParam(m, iter, &p->size) || |
| 1224 | !ReadParam(m, iter, &p->is_directory) || |
| 1225 | !ReadParam(m, iter, &last_modified) || |
| 1226 | !ReadParam(m, iter, &last_accessed) || |
| 1227 | !ReadParam(m, iter, &creation_time)) |
| 1228 | return false; |
Peter Kasting | 08b91b4 | 2023-10-21 03:46:09 | [diff] [blame] | 1229 | p->last_modified = base::Time::FromSecondsSinceUnixEpoch(last_modified); |
| 1230 | p->last_accessed = base::Time::FromSecondsSinceUnixEpoch(last_accessed); |
| 1231 | p->creation_time = base::Time::FromSecondsSinceUnixEpoch(creation_time); |
[email protected] | 481c3e8 | 2014-07-18 01:40:47 | [diff] [blame] | 1232 | return true; |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1233 | } |
| 1234 | |
[email protected] | 141bcc5 | 2014-01-27 21:36:00 | [diff] [blame] | 1235 | void ParamTraits<base::File::Info>::Log(const param_type& p, |
| 1236 | std::string* l) { |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1237 | l->append("("); |
| 1238 | LogParam(p.size, l); |
| 1239 | l->append(","); |
| 1240 | LogParam(p.is_directory, l); |
| 1241 | l->append(","); |
Peter Kasting | 08b91b4 | 2023-10-21 03:46:09 | [diff] [blame] | 1242 | LogParam(p.last_modified.InSecondsFSinceUnixEpoch(), l); |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1243 | l->append(","); |
Peter Kasting | 08b91b4 | 2023-10-21 03:46:09 | [diff] [blame] | 1244 | LogParam(p.last_accessed.InSecondsFSinceUnixEpoch(), l); |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1245 | l->append(","); |
Peter Kasting | 08b91b4 | 2023-10-21 03:46:09 | [diff] [blame] | 1246 | LogParam(p.creation_time.InSecondsFSinceUnixEpoch(), l); |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1247 | l->append(")"); |
| 1248 | } |
| 1249 | |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 1250 | void ParamTraits<base::Time>::Write(base::Pickle* m, const param_type& p) { |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 1251 | ParamTraits<int64_t>::Write(m, p.ToInternalValue()); |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1252 | } |
| 1253 | |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 1254 | bool ParamTraits<base::Time>::Read(const base::Pickle* m, |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 1255 | base::PickleIterator* iter, |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1256 | param_type* r) { |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 1257 | int64_t value; |
| 1258 | if (!ParamTraits<int64_t>::Read(m, iter, &value)) |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1259 | return false; |
| 1260 | *r = base::Time::FromInternalValue(value); |
| 1261 | return true; |
| 1262 | } |
| 1263 | |
| 1264 | void ParamTraits<base::Time>::Log(const param_type& p, std::string* l) { |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 1265 | ParamTraits<int64_t>::Log(p.ToInternalValue(), l); |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1266 | } |
| 1267 | |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 1268 | void ParamTraits<base::TimeDelta>::Write(base::Pickle* m, const param_type& p) { |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 1269 | ParamTraits<int64_t>::Write(m, p.ToInternalValue()); |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1270 | } |
| 1271 | |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 1272 | bool ParamTraits<base::TimeDelta>::Read(const base::Pickle* m, |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 1273 | base::PickleIterator* iter, |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1274 | param_type* r) { |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 1275 | int64_t value; |
| 1276 | bool ret = ParamTraits<int64_t>::Read(m, iter, &value); |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1277 | if (ret) |
| 1278 | *r = base::TimeDelta::FromInternalValue(value); |
| 1279 | |
| 1280 | return ret; |
| 1281 | } |
| 1282 | |
| 1283 | void ParamTraits<base::TimeDelta>::Log(const param_type& p, std::string* l) { |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 1284 | ParamTraits<int64_t>::Log(p.ToInternalValue(), l); |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1285 | } |
| 1286 | |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 1287 | void ParamTraits<base::TimeTicks>::Write(base::Pickle* m, const param_type& p) { |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 1288 | ParamTraits<int64_t>::Write(m, p.ToInternalValue()); |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1289 | } |
| 1290 | |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 1291 | bool ParamTraits<base::TimeTicks>::Read(const base::Pickle* m, |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 1292 | base::PickleIterator* iter, |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1293 | param_type* r) { |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 1294 | int64_t value; |
| 1295 | bool ret = ParamTraits<int64_t>::Read(m, iter, &value); |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1296 | if (ret) |
| 1297 | *r = base::TimeTicks::FromInternalValue(value); |
| 1298 | |
| 1299 | return ret; |
| 1300 | } |
| 1301 | |
| 1302 | void ParamTraits<base::TimeTicks>::Log(const param_type& p, std::string* l) { |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 1303 | ParamTraits<int64_t>::Log(p.ToInternalValue(), l); |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1304 | } |
| 1305 | |
tguilbert | 4a5ac60 | 2016-09-19 21:11:25 | [diff] [blame] | 1306 | // If base::UnguessableToken is no longer 128 bits, the IPC serialization logic |
| 1307 | // below should be updated. |
| 1308 | static_assert(sizeof(base::UnguessableToken) == 2 * sizeof(uint64_t), |
| 1309 | "base::UnguessableToken should be of size 2 * sizeof(uint64_t)."); |
| 1310 | |
tguilbert | 4a5ac60 | 2016-09-19 21:11:25 | [diff] [blame] | 1311 | void ParamTraits<base::UnguessableToken>::Write(base::Pickle* m, |
| 1312 | const param_type& p) { |
| 1313 | DCHECK(!p.is_empty()); |
| 1314 | |
| 1315 | ParamTraits<uint64_t>::Write(m, p.GetHighForSerialization()); |
| 1316 | ParamTraits<uint64_t>::Write(m, p.GetLowForSerialization()); |
| 1317 | } |
| 1318 | |
| 1319 | bool ParamTraits<base::UnguessableToken>::Read(const base::Pickle* m, |
| 1320 | base::PickleIterator* iter, |
| 1321 | param_type* r) { |
| 1322 | uint64_t high, low; |
| 1323 | if (!ParamTraits<uint64_t>::Read(m, iter, &high) || |
| 1324 | !ParamTraits<uint64_t>::Read(m, iter, &low)) |
| 1325 | return false; |
| 1326 | |
Andrew Williams | c07db02 | 2023-01-24 13:38:52 | [diff] [blame] | 1327 | // This is not mapped as nullable_is_same_type, so any UnguessableToken |
| 1328 | // deserialized by the traits should always yield a non-empty token. |
| 1329 | // If deserialization results in an empty token, the data is malformed. |
Arthur Sonzogni | 59ac822 | 2023-11-10 09:46:54 | [diff] [blame] | 1330 | std::optional<base::UnguessableToken> token = |
Andrew Williams | 228be95c | 2023-01-26 15:13:01 | [diff] [blame] | 1331 | base::UnguessableToken::Deserialize(high, low); |
Andrew Williams | c07db02 | 2023-01-24 13:38:52 | [diff] [blame] | 1332 | if (!token.has_value()) { |
tguilbert | 4a5ac60 | 2016-09-19 21:11:25 | [diff] [blame] | 1333 | return false; |
Andrew Williams | c07db02 | 2023-01-24 13:38:52 | [diff] [blame] | 1334 | } |
tguilbert | 4a5ac60 | 2016-09-19 21:11:25 | [diff] [blame] | 1335 | |
Andrew Williams | c07db02 | 2023-01-24 13:38:52 | [diff] [blame] | 1336 | *r = token.value(); |
tguilbert | 4a5ac60 | 2016-09-19 21:11:25 | [diff] [blame] | 1337 | return true; |
| 1338 | } |
| 1339 | |
| 1340 | void ParamTraits<base::UnguessableToken>::Log(const param_type& p, |
| 1341 | std::string* l) { |
| 1342 | l->append(p.ToString()); |
| 1343 | } |
| 1344 | |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 1345 | void ParamTraits<IPC::ChannelHandle>::Write(base::Pickle* m, |
| 1346 | const param_type& p) { |
amistry | 3618252 | 2016-06-27 06:34:42 | [diff] [blame] | 1347 | WriteParam(m, p.mojo_handle); |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 1348 | } |
| 1349 | |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 1350 | bool ParamTraits<IPC::ChannelHandle>::Read(const base::Pickle* m, |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 1351 | base::PickleIterator* iter, |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 1352 | param_type* r) { |
sammc | 9bf370c | 2016-11-14 03:29:08 | [diff] [blame] | 1353 | return ReadParam(m, iter, &r->mojo_handle); |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 1354 | } |
| 1355 | |
| 1356 | void ParamTraits<IPC::ChannelHandle>::Log(const param_type& p, |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 1357 | std::string* l) { |
sammc | 9bf370c | 2016-11-14 03:29:08 | [diff] [blame] | 1358 | l->append("ChannelHandle("); |
amistry | 3618252 | 2016-06-27 06:34:42 | [diff] [blame] | 1359 | LogParam(p.mojo_handle, l); |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 1360 | l->append(")"); |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 1361 | } |
| 1362 | |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 1363 | void ParamTraits<LogData>::Write(base::Pickle* m, const param_type& p) { |
[email protected] | 20f0487a | 2010-09-30 20:06:30 | [diff] [blame] | 1364 | WriteParam(m, p.channel); |
| 1365 | WriteParam(m, p.routing_id); |
[email protected] | 8bf55ca | 2011-10-17 22:15:27 | [diff] [blame] | 1366 | WriteParam(m, p.type); |
[email protected] | 20f0487a | 2010-09-30 20:06:30 | [diff] [blame] | 1367 | WriteParam(m, p.flags); |
| 1368 | WriteParam(m, p.sent); |
| 1369 | WriteParam(m, p.receive); |
| 1370 | WriteParam(m, p.dispatch); |
[email protected] | bae578e9 | 2012-11-15 03:17:45 | [diff] [blame] | 1371 | WriteParam(m, p.message_name); |
[email protected] | 20f0487a | 2010-09-30 20:06:30 | [diff] [blame] | 1372 | WriteParam(m, p.params); |
| 1373 | } |
| 1374 | |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 1375 | bool ParamTraits<LogData>::Read(const base::Pickle* m, |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 1376 | base::PickleIterator* iter, |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 1377 | param_type* r) { |
[email protected] | 8bf55ca | 2011-10-17 22:15:27 | [diff] [blame] | 1378 | return |
[email protected] | 20f0487a | 2010-09-30 20:06:30 | [diff] [blame] | 1379 | ReadParam(m, iter, &r->channel) && |
| 1380 | ReadParam(m, iter, &r->routing_id) && |
[email protected] | 8bf55ca | 2011-10-17 22:15:27 | [diff] [blame] | 1381 | ReadParam(m, iter, &r->type) && |
[email protected] | 20f0487a | 2010-09-30 20:06:30 | [diff] [blame] | 1382 | ReadParam(m, iter, &r->flags) && |
| 1383 | ReadParam(m, iter, &r->sent) && |
| 1384 | ReadParam(m, iter, &r->receive) && |
| 1385 | ReadParam(m, iter, &r->dispatch) && |
[email protected] | bae578e9 | 2012-11-15 03:17:45 | [diff] [blame] | 1386 | ReadParam(m, iter, &r->message_name) && |
[email protected] | 20f0487a | 2010-09-30 20:06:30 | [diff] [blame] | 1387 | ReadParam(m, iter, &r->params); |
[email protected] | 20f0487a | 2010-09-30 20:06:30 | [diff] [blame] | 1388 | } |
| 1389 | |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1390 | void ParamTraits<LogData>::Log(const param_type& p, std::string* l) { |
| 1391 | // Doesn't make sense to implement this! |
| 1392 | } |
| 1393 | |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 1394 | void ParamTraits<Message>::Write(base::Pickle* m, const Message& p) { |
Xiaohan Wang | ab909b3 | 2022-01-12 17:57:39 | [diff] [blame] | 1395 | #if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) |
[email protected] | 34d4861 | 2012-06-29 00:05:04 | [diff] [blame] | 1396 | // We don't serialize the file descriptors in the nested message, so there |
| 1397 | // better not be any. |
morrita | 1aa788c | 2015-01-31 05:45:42 | [diff] [blame] | 1398 | DCHECK(!p.HasAttachments()); |
[email protected] | 34d4861 | 2012-06-29 00:05:04 | [diff] [blame] | 1399 | #endif |
| 1400 | |
Tom Sepez | 2fc05a7 | 2025-06-24 20:02:59 | [diff] [blame] | 1401 | // Don't just write out the message. This used to send messages between |
[email protected] | 34d4861 | 2012-06-29 00:05:04 | [diff] [blame] | 1402 | // NaCl (Posix environment) and the browser (could be on Windows). The message |
| 1403 | // header formats differ between these systems (so does handle sharing, but |
| 1404 | // we already asserted we don't have any handles). So just write out the |
| 1405 | // parts of the header we use. |
| 1406 | // |
| 1407 | // Be careful also to use only explicitly-sized types. The NaCl environment |
| 1408 | // could be 64-bit and the host browser could be 32-bits. The nested message |
| 1409 | // may or may not be safe to send between 32-bit and 64-bit systems, but we |
| 1410 | // leave that up to the code sending the message to ensure. |
Tom Sepez | 2fc05a7 | 2025-06-24 20:02:59 | [diff] [blame] | 1411 | // TODO(crbug.com/40511454): remove this code. |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 1412 | m->WriteUInt32(static_cast<uint32_t>(p.routing_id())); |
[email protected] | 34d4861 | 2012-06-29 00:05:04 | [diff] [blame] | 1413 | m->WriteUInt32(p.type()); |
| 1414 | m->WriteUInt32(p.flags()); |
Tom Sepez | 311d878 | 2024-02-14 09:30:52 | [diff] [blame] | 1415 | m->WriteData(p.payload_bytes()); |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1416 | } |
| 1417 | |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 1418 | bool ParamTraits<Message>::Read(const base::Pickle* m, |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 1419 | base::PickleIterator* iter, |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1420 | Message* r) { |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 1421 | uint32_t routing_id, type, flags; |
avi | 48fc13b | 2014-12-28 23:31:48 | [diff] [blame] | 1422 | if (!iter->ReadUInt32(&routing_id) || |
| 1423 | !iter->ReadUInt32(&type) || |
| 1424 | !iter->ReadUInt32(&flags)) |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1425 | return false; |
[email protected] | 34d4861 | 2012-06-29 00:05:04 | [diff] [blame] | 1426 | |
Peter Kasting | 28b51cf | 2022-06-28 15:02:43 | [diff] [blame] | 1427 | size_t payload_size; |
[email protected] | 34d4861 | 2012-06-29 00:05:04 | [diff] [blame] | 1428 | const char* payload; |
avi | 48fc13b | 2014-12-28 23:31:48 | [diff] [blame] | 1429 | if (!iter->ReadData(&payload, &payload_size)) |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1430 | return false; |
[email protected] | 34d4861 | 2012-06-29 00:05:04 | [diff] [blame] | 1431 | |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 1432 | r->SetHeaderValues(static_cast<int32_t>(routing_id), type, flags); |
Daniel Cheng | 0d89f922 | 2017-09-22 05:05:07 | [diff] [blame] | 1433 | r->WriteBytes(payload, payload_size); |
| 1434 | return true; |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1435 | } |
| 1436 | |
| 1437 | void ParamTraits<Message>::Log(const Message& p, std::string* l) { |
| 1438 | l->append("<IPC::Message>"); |
| 1439 | } |
| 1440 | |
Xiaohan Wang | ab909b3 | 2022-01-12 17:57:39 | [diff] [blame] | 1441 | #if BUILDFLAG(IS_WIN) |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1442 | // Note that HWNDs/HANDLE/HCURSOR/HACCEL etc are always 32 bits, even on 64 |
[email protected] | 4a635b7 | 2013-03-04 02:29:03 | [diff] [blame] | 1443 | // bit systems. That's why we use the Windows macros to convert to 32 bits. |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 1444 | void ParamTraits<HANDLE>::Write(base::Pickle* m, const param_type& p) { |
[email protected] | 4a635b7 | 2013-03-04 02:29:03 | [diff] [blame] | 1445 | m->WriteInt(HandleToLong(p)); |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1446 | } |
| 1447 | |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 1448 | bool ParamTraits<HANDLE>::Read(const base::Pickle* m, |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 1449 | base::PickleIterator* iter, |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1450 | param_type* r) { |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 1451 | int32_t temp; |
avi | 48fc13b | 2014-12-28 23:31:48 | [diff] [blame] | 1452 | if (!iter->ReadInt(&temp)) |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1453 | return false; |
[email protected] | 4a635b7 | 2013-03-04 02:29:03 | [diff] [blame] | 1454 | *r = LongToHandle(temp); |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1455 | return true; |
| 1456 | } |
| 1457 | |
| 1458 | void ParamTraits<HANDLE>::Log(const param_type& p, std::string* l) { |
brucedawson | 5604a11d | 2015-10-06 19:22:00 | [diff] [blame] | 1459 | l->append(base::StringPrintf("0x%p", p)); |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1460 | } |
| 1461 | |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 1462 | void ParamTraits<MSG>::Write(base::Pickle* m, const param_type& p) { |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1463 | m->WriteData(reinterpret_cast<const char*>(&p), sizeof(MSG)); |
| 1464 | } |
| 1465 | |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 1466 | bool ParamTraits<MSG>::Read(const base::Pickle* m, |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 1467 | base::PickleIterator* iter, |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1468 | param_type* r) { |
| 1469 | const char *data; |
Peter Kasting | 28b51cf | 2022-06-28 15:02:43 | [diff] [blame] | 1470 | size_t data_size = 0; |
avi | 48fc13b | 2014-12-28 23:31:48 | [diff] [blame] | 1471 | bool result = iter->ReadData(&data, &data_size); |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1472 | if (result && data_size == sizeof(MSG)) { |
| 1473 | memcpy(r, data, sizeof(MSG)); |
| 1474 | } else { |
Peter Boström | b7e3e0824 | 2024-09-24 22:38:10 | [diff] [blame] | 1475 | NOTREACHED(); |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1476 | } |
| 1477 | |
| 1478 | return result; |
| 1479 | } |
| 1480 | |
| 1481 | void ParamTraits<MSG>::Log(const param_type& p, std::string* l) { |
| 1482 | l->append("<MSG>"); |
| 1483 | } |
| 1484 | |
Xiaohan Wang | 53a51163 | 2022-01-21 18:14:58 | [diff] [blame] | 1485 | #endif // BUILDFLAG(IS_WIN) |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1486 | |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 1487 | } // namespace IPC |