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