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