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