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