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