[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: |
jdoerrie | e1b1f3a | 2019-03-16 04:08:01 | [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 | } |
jdoerrie | e1b1f3a | 2019-03-16 04:08:01 | [diff] [blame] | 150 | |
| 151 | // TODO(crbug.com/859477): Remove after root cause is found. |
| 152 | default: |
| 153 | CHECK(false); |
| 154 | break; |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | |
| 158 | // Helper for ReadValue that reads a DictionaryValue into a pre-allocated |
| 159 | // object. |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 160 | bool ReadDictionaryValue(const base::Pickle* m, |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 161 | base::PickleIterator* iter, |
| 162 | base::DictionaryValue* value, |
| 163 | int recursion) { |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 164 | int size; |
| 165 | if (!ReadParam(m, iter, &size)) |
| 166 | return false; |
| 167 | |
| 168 | for (int i = 0; i < size; ++i) { |
[email protected] | e7b418b | 2010-07-30 19:47:47 | [diff] [blame] | 169 | std::string key; |
jdoerrie | f1e72e3 | 2017-04-26 16:23:55 | [diff] [blame] | 170 | std::unique_ptr<base::Value> subval; |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 171 | if (!ReadParam(m, iter, &key) || |
| 172 | !ReadValue(m, iter, &subval, recursion + 1)) |
| 173 | return false; |
jdoerrie | f1e72e3 | 2017-04-26 16:23:55 | [diff] [blame] | 174 | value->SetWithoutPathExpansion(key, std::move(subval)); |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | return true; |
| 178 | } |
| 179 | |
| 180 | // Helper for ReadValue that reads a ReadListValue into a pre-allocated |
| 181 | // object. |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 182 | bool ReadListValue(const base::Pickle* m, |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 183 | base::PickleIterator* iter, |
| 184 | base::ListValue* value, |
| 185 | int recursion) { |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 186 | int size; |
| 187 | if (!ReadParam(m, iter, &size)) |
| 188 | return false; |
| 189 | |
| 190 | for (int i = 0; i < size; ++i) { |
jdoerrie | f1e72e3 | 2017-04-26 16:23:55 | [diff] [blame] | 191 | std::unique_ptr<base::Value> subval; |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 192 | if (!ReadValue(m, iter, &subval, recursion + 1)) |
| 193 | return false; |
jdoerrie | f1e72e3 | 2017-04-26 16:23:55 | [diff] [blame] | 194 | value->Set(i, std::move(subval)); |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | return true; |
| 198 | } |
| 199 | |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 200 | bool ReadValue(const base::Pickle* m, |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 201 | base::PickleIterator* iter, |
jdoerrie | f1e72e3 | 2017-04-26 16:23:55 | [diff] [blame] | 202 | std::unique_ptr<base::Value>* value, |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 203 | int recursion) { |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 204 | if (recursion > kMaxRecursionDepth) { |
joaodasilva | 383b174a | 2017-01-10 09:55:36 | [diff] [blame] | 205 | LOG(ERROR) << "Max recursion depth hit in ReadValue."; |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 206 | return false; |
| 207 | } |
| 208 | |
| 209 | int type; |
| 210 | if (!ReadParam(m, iter, &type)) |
| 211 | return false; |
| 212 | |
jdoerrie | dc72ee94 | 2016-12-07 15:43:28 | [diff] [blame] | 213 | switch (static_cast<base::Value::Type>(type)) { |
| 214 | case base::Value::Type::NONE: |
Jeremy Roman | 160eb92 | 2017-08-29 17:43:43 | [diff] [blame] | 215 | *value = std::make_unique<base::Value>(); |
jdoerrie | e067999a | 2017-04-07 06:39:00 | [diff] [blame] | 216 | break; |
jdoerrie | dc72ee94 | 2016-12-07 15:43:28 | [diff] [blame] | 217 | case base::Value::Type::BOOLEAN: { |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 218 | bool val; |
| 219 | if (!ReadParam(m, iter, &val)) |
| 220 | return false; |
Jeremy Roman | 160eb92 | 2017-08-29 17:43:43 | [diff] [blame] | 221 | *value = std::make_unique<base::Value>(val); |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 222 | break; |
| 223 | } |
jdoerrie | dc72ee94 | 2016-12-07 15:43:28 | [diff] [blame] | 224 | case base::Value::Type::INTEGER: { |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 225 | int val; |
| 226 | if (!ReadParam(m, iter, &val)) |
| 227 | return false; |
Jeremy Roman | 160eb92 | 2017-08-29 17:43:43 | [diff] [blame] | 228 | *value = std::make_unique<base::Value>(val); |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 229 | break; |
| 230 | } |
jdoerrie | dc72ee94 | 2016-12-07 15:43:28 | [diff] [blame] | 231 | case base::Value::Type::DOUBLE: { |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 232 | double val; |
| 233 | if (!ReadParam(m, iter, &val)) |
| 234 | return false; |
Jeremy Roman | 160eb92 | 2017-08-29 17:43:43 | [diff] [blame] | 235 | *value = std::make_unique<base::Value>(val); |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 236 | break; |
| 237 | } |
jdoerrie | dc72ee94 | 2016-12-07 15:43:28 | [diff] [blame] | 238 | case base::Value::Type::STRING: { |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 239 | std::string val; |
| 240 | if (!ReadParam(m, iter, &val)) |
| 241 | return false; |
Jeremy Roman | 160eb92 | 2017-08-29 17:43:43 | [diff] [blame] | 242 | *value = std::make_unique<base::Value>(std::move(val)); |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 243 | break; |
| 244 | } |
jdoerrie | dc72ee94 | 2016-12-07 15:43:28 | [diff] [blame] | 245 | case base::Value::Type::BINARY: { |
[email protected] | e4dad9fb | 2009-10-06 18:15:58 | [diff] [blame] | 246 | const char* data; |
| 247 | int length; |
avi | 48fc13b | 2014-12-28 23:31:48 | [diff] [blame] | 248 | if (!iter->ReadData(&data, &length)) |
[email protected] | e4dad9fb | 2009-10-06 18:15:58 | [diff] [blame] | 249 | return false; |
jdoerrie | f1e72e3 | 2017-04-26 16:23:55 | [diff] [blame] | 250 | *value = base::Value::CreateWithCopiedBuffer(data, length); |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 251 | break; |
| 252 | } |
jdoerrie | dc72ee94 | 2016-12-07 15:43:28 | [diff] [blame] | 253 | case base::Value::Type::DICTIONARY: { |
jdoerrie | f1e72e3 | 2017-04-26 16:23:55 | [diff] [blame] | 254 | base::DictionaryValue val; |
| 255 | if (!ReadDictionaryValue(m, iter, &val, recursion)) |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 256 | return false; |
Jeremy Roman | 160eb92 | 2017-08-29 17:43:43 | [diff] [blame] | 257 | *value = std::make_unique<base::Value>(std::move(val)); |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 258 | break; |
| 259 | } |
jdoerrie | dc72ee94 | 2016-12-07 15:43:28 | [diff] [blame] | 260 | case base::Value::Type::LIST: { |
jdoerrie | f1e72e3 | 2017-04-26 16:23:55 | [diff] [blame] | 261 | base::ListValue val; |
| 262 | if (!ReadListValue(m, iter, &val, recursion)) |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 263 | return false; |
Jeremy Roman | 160eb92 | 2017-08-29 17:43:43 | [diff] [blame] | 264 | *value = std::make_unique<base::Value>(std::move(val)); |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 265 | break; |
| 266 | } |
[email protected] | e4dad9fb | 2009-10-06 18:15:58 | [diff] [blame] | 267 | default: |
jdoerrie | e1b1f3a | 2019-03-16 04:08:01 | [diff] [blame] | 268 | // TODO(crbug.com/859477): Remove after root cause is found. |
| 269 | CHECK(false); |
| 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 | |
Klaus Weidner | 3824a888 | 2017-11-03 06:24:57 | [diff] [blame] | 627 | #if defined(OS_ANDROID) |
Alexandr Ilin | 0443a8f | 2018-07-20 20:14:50 | [diff] [blame] | 628 | void ParamTraits<base::android::ScopedHardwareBufferHandle>::Write( |
| 629 | base::Pickle* m, |
| 630 | const param_type& p) { |
| 631 | const bool is_valid = p.is_valid(); |
Ken Rockot | 097248f0 | 2018-04-23 16:23:34 | [diff] [blame] | 632 | WriteParam(m, is_valid); |
| 633 | if (!is_valid) |
| 634 | return; |
| 635 | |
Ken Rockot | 097248f0 | 2018-04-23 16:23:34 | [diff] [blame] | 636 | // We must keep a ref to the AHardwareBuffer alive until the receiver has |
| 637 | // acquired its own reference. We do this by sending a message pipe handle |
| 638 | // along with the buffer. When the receiver deserializes (or even if they |
| 639 | // die without ever reading the message) their end of the pipe will be |
| 640 | // closed. We will eventually detect this and release the AHB reference. |
| 641 | mojo::MessagePipe tracking_pipe; |
| 642 | m->WriteAttachment(new internal::MojoHandleAttachment( |
| 643 | mojo::ScopedHandle::From(std::move(tracking_pipe.handle0)))); |
Alexandr Ilin | 0443a8f | 2018-07-20 20:14:50 | [diff] [blame] | 644 | WriteParam(m, base::FileDescriptor(p.SerializeAsFileDescriptor().release(), |
| 645 | true /* auto_close */)); |
Ken Rockot | 097248f0 | 2018-04-23 16:23:34 | [diff] [blame] | 646 | |
| 647 | // Pass ownership of the input handle to our tracking pipe to keep the AHB |
| 648 | // alive long enough to be deserialized by the receiver. |
Alexandr Ilin | 0443a8f | 2018-07-20 20:14:50 | [diff] [blame] | 649 | mojo::ScopeToMessagePipe(std::move(const_cast<param_type&>(p)), |
| 650 | std::move(tracking_pipe.handle1)); |
Klaus Weidner | 3824a888 | 2017-11-03 06:24:57 | [diff] [blame] | 651 | } |
| 652 | |
Alexandr Ilin | 0443a8f | 2018-07-20 20:14:50 | [diff] [blame] | 653 | bool ParamTraits<base::android::ScopedHardwareBufferHandle>::Read( |
| 654 | const base::Pickle* m, |
| 655 | base::PickleIterator* iter, |
| 656 | param_type* r) { |
| 657 | *r = base::android::ScopedHardwareBufferHandle(); |
Ken Rockot | 097248f0 | 2018-04-23 16:23:34 | [diff] [blame] | 658 | |
| 659 | bool is_valid; |
| 660 | if (!ReadParam(m, iter, &is_valid)) |
Klaus Weidner | 3824a888 | 2017-11-03 06:24:57 | [diff] [blame] | 661 | return false; |
Ken Rockot | 097248f0 | 2018-04-23 16:23:34 | [diff] [blame] | 662 | if (!is_valid) |
| 663 | return true; |
| 664 | |
| 665 | scoped_refptr<base::Pickle::Attachment> tracking_pipe_attachment; |
| 666 | if (!m->ReadAttachment(iter, &tracking_pipe_attachment)) |
Klaus Weidner | 3824a888 | 2017-11-03 06:24:57 | [diff] [blame] | 667 | return false; |
Ken Rockot | 097248f0 | 2018-04-23 16:23:34 | [diff] [blame] | 668 | |
| 669 | // We keep this alive until the AHB is safely deserialized below. When this |
| 670 | // goes out of scope, the sender holding the other end of this pipe will treat |
| 671 | // this handle closure as a signal that it's safe to release their AHB |
| 672 | // keepalive ref. |
| 673 | mojo::ScopedHandle tracking_pipe = |
| 674 | static_cast<MessageAttachment*>(tracking_pipe_attachment.get()) |
| 675 | ->TakeMojoHandle(); |
| 676 | |
| 677 | base::FileDescriptor descriptor; |
| 678 | if (!ReadParam(m, iter, &descriptor)) |
| 679 | return false; |
| 680 | |
| 681 | // NOTE: It is valid to deserialize an invalid FileDescriptor, so the success |
| 682 | // of |ReadParam()| above does not imply that |descriptor| is valid. |
| 683 | base::ScopedFD scoped_fd(descriptor.fd); |
| 684 | if (!scoped_fd.is_valid()) |
| 685 | return false; |
| 686 | |
| 687 | *r = base::android::ScopedHardwareBufferHandle::DeserializeFromFileDescriptor( |
Alexandr Ilin | 0443a8f | 2018-07-20 20:14:50 | [diff] [blame] | 688 | std::move(scoped_fd)); |
Klaus Weidner | 3824a888 | 2017-11-03 06:24:57 | [diff] [blame] | 689 | return true; |
| 690 | } |
| 691 | |
Alexandr Ilin | 0443a8f | 2018-07-20 20:14:50 | [diff] [blame] | 692 | void ParamTraits<base::android::ScopedHardwareBufferHandle>::Log( |
| 693 | const param_type& p, |
| 694 | std::string* l) { |
| 695 | l->append(base::StringPrintf("base::android::ScopedHardwareBufferHandle(%p)", |
| 696 | p.get())); |
Klaus Weidner | 3824a888 | 2017-11-03 06:24:57 | [diff] [blame] | 697 | } |
Ken Rockot | 097248f0 | 2018-04-23 16:23:34 | [diff] [blame] | 698 | #endif // defined(OS_ANDROID) |
Klaus Weidner | 3824a888 | 2017-11-03 06:24:57 | [diff] [blame] | 699 | |
erikchen | 22a813b | 2017-04-28 17:10:50 | [diff] [blame] | 700 | void ParamTraits<base::SharedMemoryHandle>::Write(base::Pickle* m, |
| 701 | const param_type& p) { |
erikchen | 1452520 | 2017-05-06 19:16:51 | [diff] [blame] | 702 | // This serialization must be kept in sync with |
erikchen | 9d6afd71 | 2017-05-18 17:49:06 | [diff] [blame] | 703 | // nacl_message_scanner.cc:WriteHandle(). |
erikchen | 22a813b | 2017-04-28 17:10:50 | [diff] [blame] | 704 | const bool valid = p.IsValid(); |
| 705 | WriteParam(m, valid); |
| 706 | |
| 707 | if (!valid) |
| 708 | return; |
| 709 | |
Fabrice de Gans-Riberi | cbce434 | 2018-05-07 20:02:09 | [diff] [blame] | 710 | #if defined(OS_WIN) |
Wez | 51eaaad | 2017-08-09 05:51:38 | [diff] [blame] | 711 | HandleWin handle_win(p.GetHandle()); |
erikchen | 9d6afd71 | 2017-05-18 17:49:06 | [diff] [blame] | 712 | WriteParam(m, handle_win); |
Wez | 8c49ebc9 | 2017-08-10 17:11:13 | [diff] [blame] | 713 | #elif defined(OS_FUCHSIA) |
| 714 | HandleFuchsia handle_fuchsia(p.GetHandle()); |
| 715 | WriteParam(m, handle_fuchsia); |
Fabrice de Gans-Riberi | cbce434 | 2018-05-07 20:02:09 | [diff] [blame] | 716 | #elif defined(OS_MACOSX) && !defined(OS_IOS) |
| 717 | MachPortMac mach_port_mac(p.GetMemoryObject()); |
| 718 | WriteParam(m, mach_port_mac); |
| 719 | #elif defined(OS_POSIX) |
Klaus Weidner | 3824a888 | 2017-11-03 06:24:57 | [diff] [blame] | 720 | #if defined(OS_ANDROID) |
David 'Digit' Turner | e2a6576 | 2018-01-19 17:51:51 | [diff] [blame] | 721 | WriteParam(m, p.IsReadOnly()); |
| 722 | |
| 723 | // Ensure the region is read-only before sending it through IPC. |
| 724 | if (p.IsReadOnly()) { |
| 725 | if (!p.IsRegionReadOnly()) { |
| 726 | LOG(ERROR) << "Sending unsealed read-only region through IPC"; |
| 727 | p.SetRegionReadOnly(); |
| 728 | } |
| 729 | } |
Klaus Weidner | 3824a888 | 2017-11-03 06:24:57 | [diff] [blame] | 730 | #endif |
erikchen | 22a813b | 2017-04-28 17:10:50 | [diff] [blame] | 731 | if (p.OwnershipPassesToIPC()) { |
| 732 | if (!m->WriteAttachment(new internal::PlatformFileAttachment( |
| 733 | base::ScopedFD(p.GetHandle())))) |
| 734 | NOTREACHED(); |
| 735 | } else { |
| 736 | if (!m->WriteAttachment( |
| 737 | new internal::PlatformFileAttachment(p.GetHandle()))) |
| 738 | NOTREACHED(); |
| 739 | } |
erikchen | 9d6afd71 | 2017-05-18 17:49:06 | [diff] [blame] | 740 | #endif |
| 741 | |
| 742 | #if (defined(OS_MACOSX) && !defined(OS_IOS)) || defined(OS_WIN) |
| 743 | // If the caller intended to pass ownership to the IPC stack, release a |
| 744 | // reference. |
| 745 | if (p.OwnershipPassesToIPC()) |
| 746 | p.Close(); |
| 747 | #endif |
| 748 | |
erikchen | 1452520 | 2017-05-06 19:16:51 | [diff] [blame] | 749 | DCHECK(!p.GetGUID().is_empty()); |
| 750 | WriteParam(m, p.GetGUID()); |
erikchen | 9d6afd71 | 2017-05-18 17:49:06 | [diff] [blame] | 751 | WriteParam(m, static_cast<uint64_t>(p.GetSize())); |
erikchen | 22a813b | 2017-04-28 17:10:50 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | bool ParamTraits<base::SharedMemoryHandle>::Read(const base::Pickle* m, |
| 755 | base::PickleIterator* iter, |
| 756 | param_type* r) { |
| 757 | *r = base::SharedMemoryHandle(); |
| 758 | |
| 759 | bool valid; |
| 760 | if (!ReadParam(m, iter, &valid)) |
| 761 | return false; |
erikchen | 22a813b | 2017-04-28 17:10:50 | [diff] [blame] | 762 | if (!valid) |
| 763 | return true; |
| 764 | |
Fabrice de Gans-Riberi | cbce434 | 2018-05-07 20:02:09 | [diff] [blame] | 765 | #if defined(OS_WIN) |
erikchen | 9d6afd71 | 2017-05-18 17:49:06 | [diff] [blame] | 766 | HandleWin handle_win; |
| 767 | if (!ReadParam(m, iter, &handle_win)) |
| 768 | return false; |
Scott Graham | 3eebff0 | 2017-06-30 01:07:10 | [diff] [blame] | 769 | #elif defined(OS_FUCHSIA) |
| 770 | HandleFuchsia handle_fuchsia; |
| 771 | if (!ReadParam(m, iter, &handle_fuchsia)) |
| 772 | return false; |
Fabrice de Gans-Riberi | cbce434 | 2018-05-07 20:02:09 | [diff] [blame] | 773 | #elif defined(OS_MACOSX) && !defined(OS_IOS) |
| 774 | MachPortMac mach_port_mac; |
| 775 | if (!ReadParam(m, iter, &mach_port_mac)) |
| 776 | return false; |
| 777 | #elif defined(OS_POSIX) |
Klaus Weidner | 3824a888 | 2017-11-03 06:24:57 | [diff] [blame] | 778 | #if defined(OS_ANDROID) |
David 'Digit' Turner | e2a6576 | 2018-01-19 17:51:51 | [diff] [blame] | 779 | bool is_read_only = false; |
Ken Rockot | 097248f0 | 2018-04-23 16:23:34 | [diff] [blame] | 780 | if (!ReadParam(m, iter, &is_read_only)) |
Klaus Weidner | 3824a888 | 2017-11-03 06:24:57 | [diff] [blame] | 781 | return false; |
| 782 | #endif |
erikchen | 22a813b | 2017-04-28 17:10:50 | [diff] [blame] | 783 | scoped_refptr<base::Pickle::Attachment> attachment; |
| 784 | if (!m->ReadAttachment(iter, &attachment)) |
| 785 | return false; |
| 786 | |
| 787 | if (static_cast<MessageAttachment*>(attachment.get())->GetType() != |
| 788 | MessageAttachment::Type::PLATFORM_FILE) { |
| 789 | return false; |
| 790 | } |
erikchen | 9d6afd71 | 2017-05-18 17:49:06 | [diff] [blame] | 791 | #endif |
erikchen | 22a813b | 2017-04-28 17:10:50 | [diff] [blame] | 792 | |
erikchen | 1452520 | 2017-05-06 19:16:51 | [diff] [blame] | 793 | base::UnguessableToken guid; |
erikchen | 9d6afd71 | 2017-05-18 17:49:06 | [diff] [blame] | 794 | uint64_t size; |
Alexandr Ilin | d497eee | 2018-04-19 22:50:54 | [diff] [blame] | 795 | if (!ReadParam(m, iter, &guid) || !ReadParam(m, iter, &size) || |
| 796 | !base::IsValueInRangeForNumericType<size_t>(size)) { |
erikchen | 1452520 | 2017-05-06 19:16:51 | [diff] [blame] | 797 | return false; |
erikchen | 9d6afd71 | 2017-05-18 17:49:06 | [diff] [blame] | 798 | } |
erikchen | 1452520 | 2017-05-06 19:16:51 | [diff] [blame] | 799 | |
Fabrice de Gans-Riberi | cbce434 | 2018-05-07 20:02:09 | [diff] [blame] | 800 | #if defined(OS_WIN) |
erikchen | 9d6afd71 | 2017-05-18 17:49:06 | [diff] [blame] | 801 | *r = base::SharedMemoryHandle(handle_win.get_handle(), |
| 802 | static_cast<size_t>(size), guid); |
Scott Graham | 3eebff0 | 2017-06-30 01:07:10 | [diff] [blame] | 803 | #elif defined(OS_FUCHSIA) |
| 804 | *r = base::SharedMemoryHandle(handle_fuchsia.get_handle(), |
| 805 | static_cast<size_t>(size), guid); |
Fabrice de Gans-Riberi | cbce434 | 2018-05-07 20:02:09 | [diff] [blame] | 806 | #elif defined(OS_MACOSX) && !defined(OS_IOS) |
| 807 | *r = base::SharedMemoryHandle(mach_port_mac.get_mach_port(), |
| 808 | static_cast<size_t>(size), guid); |
| 809 | #elif defined(OS_POSIX) |
erikchen | 1452520 | 2017-05-06 19:16:51 | [diff] [blame] | 810 | *r = base::SharedMemoryHandle( |
| 811 | base::FileDescriptor( |
| 812 | static_cast<internal::PlatformFileAttachment*>(attachment.get()) |
| 813 | ->TakePlatformFile(), |
| 814 | true), |
erikchen | 9d6afd71 | 2017-05-18 17:49:06 | [diff] [blame] | 815 | static_cast<size_t>(size), guid); |
| 816 | #endif |
| 817 | |
Ken Rockot | 097248f0 | 2018-04-23 16:23:34 | [diff] [blame] | 818 | #if defined(OS_ANDROID) |
| 819 | if (is_read_only) |
| 820 | r->SetReadOnly(); |
| 821 | #endif |
| 822 | |
erikchen | 22a813b | 2017-04-28 17:10:50 | [diff] [blame] | 823 | return true; |
| 824 | } |
| 825 | |
| 826 | void ParamTraits<base::SharedMemoryHandle>::Log(const param_type& p, |
| 827 | std::string* l) { |
Fabrice de Gans-Riberi | cbce434 | 2018-05-07 20:02:09 | [diff] [blame] | 828 | #if defined(OS_WIN) |
erikchen | 9d6afd71 | 2017-05-18 17:49:06 | [diff] [blame] | 829 | l->append("HANDLE: "); |
| 830 | LogParam(p.GetHandle(), l); |
Fabrice de Gans-Riberi | cbce434 | 2018-05-07 20:02:09 | [diff] [blame] | 831 | #elif defined(OS_MACOSX) && !defined(OS_IOS) |
| 832 | l->append("Mach port: "); |
| 833 | LogParam(p.GetMemoryObject(), l); |
| 834 | #elif defined(OS_POSIX) || defined(OS_FUCHSIA) |
erikchen | 9d6afd71 | 2017-05-18 17:49:06 | [diff] [blame] | 835 | l->append("FD: "); |
| 836 | LogParam(p.GetHandle(), l); |
| 837 | #endif |
| 838 | |
erikchen | 1452520 | 2017-05-06 19:16:51 | [diff] [blame] | 839 | l->append("GUID: "); |
erikchen | 9d6afd71 | 2017-05-18 17:49:06 | [diff] [blame] | 840 | LogParam(p.GetGUID(), l); |
| 841 | l->append("size: "); |
| 842 | LogParam(static_cast<uint64_t>(p.GetSize()), l); |
David 'Digit' Turner | e2a6576 | 2018-01-19 17:51:51 | [diff] [blame] | 843 | #if defined(OS_ANDROID) |
| 844 | l->append("read-only: "); |
| 845 | LogParam(p.IsReadOnly(), l); |
| 846 | #endif |
erikchen | 22a813b | 2017-04-28 17:10:50 | [diff] [blame] | 847 | } |
scottmg | d19b4f7 | 2015-06-19 22:51:00 | [diff] [blame] | 848 | |
Alexandr Ilin | d497eee | 2018-04-19 22:50:54 | [diff] [blame] | 849 | void ParamTraits<base::ReadOnlySharedMemoryRegion>::Write(base::Pickle* m, |
| 850 | const param_type& p) { |
| 851 | base::subtle::PlatformSharedMemoryRegion handle = |
| 852 | base::ReadOnlySharedMemoryRegion::TakeHandleForSerialization( |
| 853 | std::move(const_cast<param_type&>(p))); |
| 854 | WriteParam(m, std::move(handle)); |
| 855 | } |
| 856 | |
| 857 | bool ParamTraits<base::ReadOnlySharedMemoryRegion>::Read( |
| 858 | const base::Pickle* m, |
| 859 | base::PickleIterator* iter, |
| 860 | param_type* r) { |
| 861 | base::subtle::PlatformSharedMemoryRegion handle; |
| 862 | if (!ReadParam(m, iter, &handle)) |
| 863 | return false; |
| 864 | |
| 865 | *r = base::ReadOnlySharedMemoryRegion::Deserialize(std::move(handle)); |
| 866 | return true; |
| 867 | } |
| 868 | |
| 869 | void ParamTraits<base::ReadOnlySharedMemoryRegion>::Log(const param_type& p, |
| 870 | std::string* l) { |
| 871 | *l = "<base::ReadOnlySharedMemoryRegion>"; |
| 872 | // TODO(alexilin): currently there is no way to access underlying handle |
| 873 | // without destructing a ReadOnlySharedMemoryRegion instance. |
| 874 | } |
| 875 | |
| 876 | void ParamTraits<base::WritableSharedMemoryRegion>::Write(base::Pickle* m, |
| 877 | const param_type& p) { |
| 878 | base::subtle::PlatformSharedMemoryRegion handle = |
| 879 | base::WritableSharedMemoryRegion::TakeHandleForSerialization( |
| 880 | std::move(const_cast<param_type&>(p))); |
| 881 | WriteParam(m, std::move(handle)); |
| 882 | } |
| 883 | |
| 884 | bool ParamTraits<base::WritableSharedMemoryRegion>::Read( |
| 885 | const base::Pickle* m, |
| 886 | base::PickleIterator* iter, |
| 887 | param_type* r) { |
| 888 | base::subtle::PlatformSharedMemoryRegion handle; |
| 889 | if (!ReadParam(m, iter, &handle)) |
| 890 | return false; |
| 891 | |
| 892 | *r = base::WritableSharedMemoryRegion::Deserialize(std::move(handle)); |
| 893 | return true; |
| 894 | } |
| 895 | |
| 896 | void ParamTraits<base::WritableSharedMemoryRegion>::Log(const param_type& p, |
| 897 | std::string* l) { |
| 898 | *l = "<base::WritableSharedMemoryRegion>"; |
| 899 | // TODO(alexilin): currently there is no way to access underlying handle |
| 900 | // without destructing a ReadOnlySharedMemoryRegion instance. |
| 901 | } |
| 902 | |
| 903 | void ParamTraits<base::UnsafeSharedMemoryRegion>::Write(base::Pickle* m, |
| 904 | const param_type& p) { |
| 905 | base::subtle::PlatformSharedMemoryRegion handle = |
| 906 | base::UnsafeSharedMemoryRegion::TakeHandleForSerialization( |
| 907 | std::move(const_cast<param_type&>(p))); |
| 908 | WriteParam(m, std::move(handle)); |
| 909 | } |
| 910 | |
| 911 | bool ParamTraits<base::UnsafeSharedMemoryRegion>::Read( |
| 912 | const base::Pickle* m, |
| 913 | base::PickleIterator* iter, |
| 914 | param_type* r) { |
| 915 | base::subtle::PlatformSharedMemoryRegion handle; |
| 916 | if (!ReadParam(m, iter, &handle)) |
| 917 | return false; |
| 918 | |
| 919 | *r = base::UnsafeSharedMemoryRegion::Deserialize(std::move(handle)); |
| 920 | return true; |
| 921 | } |
| 922 | |
| 923 | void ParamTraits<base::UnsafeSharedMemoryRegion>::Log(const param_type& p, |
| 924 | std::string* l) { |
| 925 | *l = "<base::UnsafeSharedMemoryRegion>"; |
| 926 | // TODO(alexilin): currently there is no way to access underlying handle |
| 927 | // without destructing a ReadOnlySharedMemoryRegion instance. |
| 928 | } |
| 929 | |
| 930 | void ParamTraits<base::subtle::PlatformSharedMemoryRegion>::Write( |
| 931 | base::Pickle* m, |
| 932 | const param_type& p) { |
Alexandr Ilin | ebab9da | 2018-06-01 02:37:47 | [diff] [blame] | 933 | // This serialization must be kept in sync with |
| 934 | // nacl_message_scanner.cc::WriteHandle(). |
Alexandr Ilin | d497eee | 2018-04-19 22:50:54 | [diff] [blame] | 935 | const bool valid = p.IsValid(); |
| 936 | WriteParam(m, valid); |
| 937 | |
| 938 | if (!valid) |
| 939 | return; |
| 940 | |
| 941 | WriteParam(m, p.GetMode()); |
| 942 | WriteParam(m, static_cast<uint64_t>(p.GetSize())); |
| 943 | WriteParam(m, p.GetGUID()); |
| 944 | |
Fabrice de Gans-Riberi | cbce434 | 2018-05-07 20:02:09 | [diff] [blame] | 945 | #if defined(OS_WIN) |
| 946 | base::win::ScopedHandle h = const_cast<param_type&>(p).PassPlatformHandle(); |
Alexandr Ilin | 6544b8a | 2018-08-15 23:08:54 | [diff] [blame] | 947 | HandleWin handle_win(h.Get()); |
Fabrice de Gans-Riberi | cbce434 | 2018-05-07 20:02:09 | [diff] [blame] | 948 | WriteParam(m, handle_win); |
Alexandr Ilin | d497eee | 2018-04-19 22:50:54 | [diff] [blame] | 949 | #elif defined(OS_FUCHSIA) |
Wez | 157707d6 | 2018-07-10 22:48:47 | [diff] [blame] | 950 | zx::handle h = const_cast<param_type&>(p).PassPlatformHandle(); |
Alexandr Ilin | 6544b8a | 2018-08-15 23:08:54 | [diff] [blame] | 951 | HandleFuchsia handle_fuchsia(h.get()); |
Alexandr Ilin | d497eee | 2018-04-19 22:50:54 | [diff] [blame] | 952 | WriteParam(m, handle_fuchsia); |
Fabrice de Gans-Riberi | cbce434 | 2018-05-07 20:02:09 | [diff] [blame] | 953 | #elif defined(OS_MACOSX) && !defined(OS_IOS) |
| 954 | base::mac::ScopedMachSendRight h = |
| 955 | const_cast<param_type&>(p).PassPlatformHandle(); |
Alexandr Ilin | 6544b8a | 2018-08-15 23:08:54 | [diff] [blame] | 956 | MachPortMac mach_port_mac(h.get()); |
Fabrice de Gans-Riberi | cbce434 | 2018-05-07 20:02:09 | [diff] [blame] | 957 | WriteParam(m, mach_port_mac); |
Alexandr Ilin | d497eee | 2018-04-19 22:50:54 | [diff] [blame] | 958 | #elif defined(OS_ANDROID) |
| 959 | m->WriteAttachment(new internal::PlatformFileAttachment( |
| 960 | base::ScopedFD(const_cast<param_type&>(p).PassPlatformHandle()))); |
Fabrice de Gans-Riberi | cbce434 | 2018-05-07 20:02:09 | [diff] [blame] | 961 | #elif defined(OS_POSIX) |
Alexandr Ilin | d497eee | 2018-04-19 22:50:54 | [diff] [blame] | 962 | base::subtle::ScopedFDPair h = |
| 963 | const_cast<param_type&>(p).PassPlatformHandle(); |
| 964 | m->WriteAttachment(new internal::PlatformFileAttachment(std::move(h.fd))); |
| 965 | if (p.GetMode() == |
| 966 | base::subtle::PlatformSharedMemoryRegion::Mode::kWritable) { |
| 967 | m->WriteAttachment( |
| 968 | new internal::PlatformFileAttachment(std::move(h.readonly_fd))); |
| 969 | } |
| 970 | #endif |
| 971 | } |
| 972 | |
| 973 | bool ParamTraits<base::subtle::PlatformSharedMemoryRegion>::Read( |
| 974 | const base::Pickle* m, |
| 975 | base::PickleIterator* iter, |
| 976 | param_type* r) { |
| 977 | bool valid; |
| 978 | if (!ReadParam(m, iter, &valid)) |
| 979 | return false; |
| 980 | if (!valid) { |
| 981 | *r = base::subtle::PlatformSharedMemoryRegion(); |
| 982 | return true; |
| 983 | } |
| 984 | |
| 985 | base::subtle::PlatformSharedMemoryRegion::Mode mode; |
| 986 | uint64_t shm_size; |
| 987 | base::UnguessableToken guid; |
| 988 | if (!ReadParam(m, iter, &mode) || !ReadParam(m, iter, &shm_size) || |
| 989 | !base::IsValueInRangeForNumericType<size_t>(shm_size) || |
| 990 | !ReadParam(m, iter, &guid)) { |
| 991 | return false; |
| 992 | } |
| 993 | size_t size = static_cast<size_t>(shm_size); |
| 994 | |
Fabrice de Gans-Riberi | cbce434 | 2018-05-07 20:02:09 | [diff] [blame] | 995 | #if defined(OS_WIN) |
| 996 | HandleWin handle_win; |
| 997 | if (!ReadParam(m, iter, &handle_win)) |
Alexandr Ilin | d497eee | 2018-04-19 22:50:54 | [diff] [blame] | 998 | return false; |
| 999 | *r = base::subtle::PlatformSharedMemoryRegion::Take( |
Fabrice de Gans-Riberi | cbce434 | 2018-05-07 20:02:09 | [diff] [blame] | 1000 | base::win::ScopedHandle(handle_win.get_handle()), mode, size, guid); |
Alexandr Ilin | d497eee | 2018-04-19 22:50:54 | [diff] [blame] | 1001 | #elif defined(OS_FUCHSIA) |
| 1002 | HandleFuchsia handle_fuchsia; |
| 1003 | if (!ReadParam(m, iter, &handle_fuchsia)) |
| 1004 | return false; |
| 1005 | *r = base::subtle::PlatformSharedMemoryRegion::Take( |
Wez | 157707d6 | 2018-07-10 22:48:47 | [diff] [blame] | 1006 | zx::vmo(handle_fuchsia.get_handle()), mode, size, guid); |
Fabrice de Gans-Riberi | cbce434 | 2018-05-07 20:02:09 | [diff] [blame] | 1007 | #elif defined(OS_MACOSX) && !defined(OS_IOS) |
| 1008 | MachPortMac mach_port_mac; |
| 1009 | if (!ReadParam(m, iter, &mach_port_mac)) |
Alexandr Ilin | d497eee | 2018-04-19 22:50:54 | [diff] [blame] | 1010 | return false; |
| 1011 | *r = base::subtle::PlatformSharedMemoryRegion::Take( |
Fabrice de Gans-Riberi | cbce434 | 2018-05-07 20:02:09 | [diff] [blame] | 1012 | base::mac::ScopedMachSendRight(mach_port_mac.get_mach_port()), mode, size, |
| 1013 | guid); |
| 1014 | #elif defined(OS_POSIX) |
Alexandr Ilin | d497eee | 2018-04-19 22:50:54 | [diff] [blame] | 1015 | scoped_refptr<base::Pickle::Attachment> attachment; |
| 1016 | if (!m->ReadAttachment(iter, &attachment)) |
| 1017 | return false; |
| 1018 | if (static_cast<MessageAttachment*>(attachment.get())->GetType() != |
| 1019 | MessageAttachment::Type::PLATFORM_FILE) { |
| 1020 | return false; |
| 1021 | } |
| 1022 | |
| 1023 | #if defined(OS_ANDROID) |
| 1024 | *r = base::subtle::PlatformSharedMemoryRegion::Take( |
| 1025 | base::ScopedFD( |
| 1026 | static_cast<internal::PlatformFileAttachment*>(attachment.get()) |
| 1027 | ->TakePlatformFile()), |
| 1028 | mode, size, guid); |
| 1029 | #else |
| 1030 | scoped_refptr<base::Pickle::Attachment> readonly_attachment; |
| 1031 | if (mode == base::subtle::PlatformSharedMemoryRegion::Mode::kWritable) { |
| 1032 | if (!m->ReadAttachment(iter, &readonly_attachment)) |
| 1033 | return false; |
| 1034 | |
| 1035 | if (static_cast<MessageAttachment*>(readonly_attachment.get())->GetType() != |
| 1036 | MessageAttachment::Type::PLATFORM_FILE) { |
| 1037 | return false; |
| 1038 | } |
| 1039 | } |
| 1040 | *r = base::subtle::PlatformSharedMemoryRegion::Take( |
| 1041 | base::subtle::ScopedFDPair( |
| 1042 | base::ScopedFD( |
| 1043 | static_cast<internal::PlatformFileAttachment*>(attachment.get()) |
| 1044 | ->TakePlatformFile()), |
| 1045 | readonly_attachment |
| 1046 | ? base::ScopedFD(static_cast<internal::PlatformFileAttachment*>( |
| 1047 | readonly_attachment.get()) |
| 1048 | ->TakePlatformFile()) |
| 1049 | : base::ScopedFD()), |
| 1050 | mode, size, guid); |
| 1051 | #endif // defined(OS_ANDROID) |
| 1052 | |
| 1053 | #endif |
| 1054 | |
| 1055 | return true; |
| 1056 | } |
| 1057 | |
| 1058 | void ParamTraits<base::subtle::PlatformSharedMemoryRegion>::Log( |
| 1059 | const param_type& p, |
| 1060 | std::string* l) { |
Wez | b908e43 | 2018-09-05 17:35:22 | [diff] [blame] | 1061 | #if defined(OS_FUCHSIA) |
| 1062 | l->append("Handle: "); |
| 1063 | LogParam(p.GetPlatformHandle()->get(), l); |
| 1064 | #elif defined(OS_WIN) |
Alexandr Ilin | d497eee | 2018-04-19 22:50:54 | [diff] [blame] | 1065 | l->append("Handle: "); |
| 1066 | LogParam(p.GetPlatformHandle(), l); |
Fabrice de Gans-Riberi | cbce434 | 2018-05-07 20:02:09 | [diff] [blame] | 1067 | #elif defined(OS_MACOSX) && !defined(OS_IOS) |
| 1068 | l->append("Mach port: "); |
| 1069 | LogParam(p.GetPlatformHandle(), l); |
Alexandr Ilin | d497eee | 2018-04-19 22:50:54 | [diff] [blame] | 1070 | #elif defined(OS_ANDROID) |
| 1071 | l->append("FD: "); |
| 1072 | LogParam(p.GetPlatformHandle(), l); |
Fabrice de Gans-Riberi | cbce434 | 2018-05-07 20:02:09 | [diff] [blame] | 1073 | #elif defined(OS_POSIX) |
Alexandr Ilin | d497eee | 2018-04-19 22:50:54 | [diff] [blame] | 1074 | base::subtle::FDPair h = p.GetPlatformHandle(); |
| 1075 | l->append("FD: "); |
| 1076 | LogParam(h.fd, l); |
| 1077 | l->append("Read-only FD: "); |
| 1078 | LogParam(h.readonly_fd, l); |
| 1079 | #endif |
| 1080 | |
| 1081 | l->append("Mode: "); |
| 1082 | LogParam(p.GetMode(), l); |
| 1083 | l->append("size: "); |
| 1084 | LogParam(static_cast<uint64_t>(p.GetSize()), l); |
| 1085 | l->append("GUID: "); |
| 1086 | LogParam(p.GetGUID(), l); |
| 1087 | } |
| 1088 | |
| 1089 | void ParamTraits<base::subtle::PlatformSharedMemoryRegion::Mode>::Write( |
| 1090 | base::Pickle* m, |
| 1091 | const param_type& value) { |
| 1092 | DCHECK(static_cast<int>(value) >= 0 && |
| 1093 | static_cast<int>(value) <= static_cast<int>(param_type::kMaxValue)); |
| 1094 | m->WriteInt(static_cast<int>(value)); |
| 1095 | } |
| 1096 | |
| 1097 | bool ParamTraits<base::subtle::PlatformSharedMemoryRegion::Mode>::Read( |
| 1098 | const base::Pickle* m, |
| 1099 | base::PickleIterator* iter, |
| 1100 | param_type* p) { |
| 1101 | int value; |
| 1102 | if (!iter->ReadInt(&value)) |
| 1103 | return false; |
| 1104 | if (!(static_cast<int>(value) >= 0 && |
| 1105 | static_cast<int>(value) <= static_cast<int>(param_type::kMaxValue))) { |
| 1106 | return false; |
| 1107 | } |
| 1108 | *p = static_cast<param_type>(value); |
| 1109 | return true; |
| 1110 | } |
| 1111 | |
| 1112 | void ParamTraits<base::subtle::PlatformSharedMemoryRegion::Mode>::Log( |
| 1113 | const param_type& p, |
| 1114 | std::string* l) { |
| 1115 | LogParam(static_cast<int>(p), l); |
| 1116 | } |
| 1117 | |
erikchen | d804e105 | 2017-04-29 02:24:36 | [diff] [blame] | 1118 | #if defined(OS_WIN) |
erikchen | d804e105 | 2017-04-29 02:24:36 | [diff] [blame] | 1119 | void ParamTraits<PlatformFileForTransit>::Write(base::Pickle* m, |
| 1120 | const param_type& p) { |
| 1121 | m->WriteBool(p.IsValid()); |
| 1122 | if (p.IsValid()) { |
Wez | 51eaaad | 2017-08-09 05:51:38 | [diff] [blame] | 1123 | HandleWin handle_win(p.GetHandle()); |
erikchen | d804e105 | 2017-04-29 02:24:36 | [diff] [blame] | 1124 | ParamTraits<HandleWin>::Write(m, handle_win); |
| 1125 | ::CloseHandle(p.GetHandle()); |
| 1126 | } |
| 1127 | } |
| 1128 | |
| 1129 | bool ParamTraits<PlatformFileForTransit>::Read(const base::Pickle* m, |
| 1130 | base::PickleIterator* iter, |
| 1131 | param_type* r) { |
| 1132 | bool is_valid; |
| 1133 | if (!iter->ReadBool(&is_valid)) |
| 1134 | return false; |
| 1135 | if (!is_valid) { |
| 1136 | *r = PlatformFileForTransit(); |
| 1137 | return true; |
| 1138 | } |
| 1139 | |
| 1140 | HandleWin handle_win; |
| 1141 | if (!ParamTraits<HandleWin>::Read(m, iter, &handle_win)) |
| 1142 | return false; |
| 1143 | *r = PlatformFileForTransit(handle_win.get_handle()); |
| 1144 | return true; |
| 1145 | } |
| 1146 | |
| 1147 | void ParamTraits<PlatformFileForTransit>::Log(const param_type& p, |
| 1148 | std::string* l) { |
| 1149 | LogParam(p.GetHandle(), l); |
| 1150 | } |
| 1151 | #endif // defined(OS_WIN) |
| 1152 | |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 1153 | void ParamTraits<base::FilePath>::Write(base::Pickle* m, const param_type& p) { |
[email protected] | aeae59f | 2013-01-28 13:47:55 | [diff] [blame] | 1154 | p.WriteToPickle(m); |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1155 | } |
| 1156 | |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 1157 | bool ParamTraits<base::FilePath>::Read(const base::Pickle* m, |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 1158 | base::PickleIterator* iter, |
[email protected] | 6d4b67a | 2013-02-10 04:49:30 | [diff] [blame] | 1159 | param_type* r) { |
[email protected] | aeae59f | 2013-01-28 13:47:55 | [diff] [blame] | 1160 | return r->ReadFromPickle(iter); |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1161 | } |
| 1162 | |
[email protected] | 6d4b67a | 2013-02-10 04:49:30 | [diff] [blame] | 1163 | void ParamTraits<base::FilePath>::Log(const param_type& p, std::string* l) { |
| 1164 | ParamTraits<base::FilePath::StringType>::Log(p.value(), l); |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1165 | } |
| 1166 | |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 1167 | void ParamTraits<base::ListValue>::Write(base::Pickle* m, const param_type& p) { |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1168 | WriteValue(m, &p, 0); |
| 1169 | } |
| 1170 | |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 1171 | bool ParamTraits<base::ListValue>::Read(const base::Pickle* m, |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 1172 | base::PickleIterator* iter, |
| 1173 | param_type* r) { |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1174 | int type; |
jdoerrie | dc72ee94 | 2016-12-07 15:43:28 | [diff] [blame] | 1175 | if (!ReadParam(m, iter, &type) || |
| 1176 | type != static_cast<int>(base::Value::Type::LIST)) |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1177 | return false; |
| 1178 | |
| 1179 | return ReadListValue(m, iter, r, 0); |
| 1180 | } |
| 1181 | |
[email protected] | ea5ef4c | 2013-06-13 22:50:27 | [diff] [blame] | 1182 | void ParamTraits<base::ListValue>::Log(const param_type& p, std::string* l) { |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1183 | std::string json; |
estade | 8d04646 | 2015-05-16 01:02:34 | [diff] [blame] | 1184 | base::JSONWriter::Write(p, &json); |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1185 | l->append(json); |
| 1186 | } |
| 1187 | |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 1188 | void ParamTraits<base::NullableString16>::Write(base::Pickle* m, |
[email protected] | 0238a16 | 2013-06-13 13:47:46 | [diff] [blame] | 1189 | const param_type& p) { |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1190 | WriteParam(m, p.string()); |
| 1191 | WriteParam(m, p.is_null()); |
| 1192 | } |
| 1193 | |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 1194 | bool ParamTraits<base::NullableString16>::Read(const base::Pickle* m, |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 1195 | base::PickleIterator* iter, |
[email protected] | 0238a16 | 2013-06-13 13:47:46 | [diff] [blame] | 1196 | param_type* r) { |
[email protected] | 476dafb | 2013-12-03 00:39:26 | [diff] [blame] | 1197 | base::string16 string; |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1198 | if (!ReadParam(m, iter, &string)) |
| 1199 | return false; |
| 1200 | bool is_null; |
| 1201 | if (!ReadParam(m, iter, &is_null)) |
| 1202 | return false; |
[email protected] | 0238a16 | 2013-06-13 13:47:46 | [diff] [blame] | 1203 | *r = base::NullableString16(string, is_null); |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1204 | return true; |
| 1205 | } |
| 1206 | |
[email protected] | 0238a16 | 2013-06-13 13:47:46 | [diff] [blame] | 1207 | void ParamTraits<base::NullableString16>::Log(const param_type& p, |
| 1208 | std::string* l) { |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1209 | l->append("("); |
| 1210 | LogParam(p.string(), l); |
| 1211 | l->append(", "); |
| 1212 | LogParam(p.is_null(), l); |
| 1213 | l->append(")"); |
| 1214 | } |
| 1215 | |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 1216 | void ParamTraits<base::File::Info>::Write(base::Pickle* m, |
[email protected] | 141bcc5 | 2014-01-27 21:36:00 | [diff] [blame] | 1217 | const param_type& p) { |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1218 | WriteParam(m, p.size); |
| 1219 | WriteParam(m, p.is_directory); |
| 1220 | WriteParam(m, p.last_modified.ToDoubleT()); |
| 1221 | WriteParam(m, p.last_accessed.ToDoubleT()); |
| 1222 | WriteParam(m, p.creation_time.ToDoubleT()); |
| 1223 | } |
| 1224 | |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 1225 | bool ParamTraits<base::File::Info>::Read(const base::Pickle* m, |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 1226 | base::PickleIterator* iter, |
[email protected] | 141bcc5 | 2014-01-27 21:36:00 | [diff] [blame] | 1227 | param_type* p) { |
[email protected] | 481c3e8 | 2014-07-18 01:40:47 | [diff] [blame] | 1228 | double last_modified, last_accessed, creation_time; |
| 1229 | if (!ReadParam(m, iter, &p->size) || |
| 1230 | !ReadParam(m, iter, &p->is_directory) || |
| 1231 | !ReadParam(m, iter, &last_modified) || |
| 1232 | !ReadParam(m, iter, &last_accessed) || |
| 1233 | !ReadParam(m, iter, &creation_time)) |
| 1234 | return false; |
| 1235 | p->last_modified = base::Time::FromDoubleT(last_modified); |
| 1236 | p->last_accessed = base::Time::FromDoubleT(last_accessed); |
| 1237 | p->creation_time = base::Time::FromDoubleT(creation_time); |
| 1238 | return true; |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1239 | } |
| 1240 | |
[email protected] | 141bcc5 | 2014-01-27 21:36:00 | [diff] [blame] | 1241 | void ParamTraits<base::File::Info>::Log(const param_type& p, |
| 1242 | std::string* l) { |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1243 | l->append("("); |
| 1244 | LogParam(p.size, l); |
| 1245 | l->append(","); |
| 1246 | LogParam(p.is_directory, l); |
| 1247 | l->append(","); |
| 1248 | LogParam(p.last_modified.ToDoubleT(), l); |
| 1249 | l->append(","); |
| 1250 | LogParam(p.last_accessed.ToDoubleT(), l); |
| 1251 | l->append(","); |
| 1252 | LogParam(p.creation_time.ToDoubleT(), l); |
| 1253 | l->append(")"); |
| 1254 | } |
| 1255 | |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 1256 | void ParamTraits<base::Time>::Write(base::Pickle* m, const param_type& p) { |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 1257 | ParamTraits<int64_t>::Write(m, p.ToInternalValue()); |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1258 | } |
| 1259 | |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 1260 | bool ParamTraits<base::Time>::Read(const base::Pickle* m, |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 1261 | base::PickleIterator* iter, |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1262 | param_type* r) { |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 1263 | int64_t value; |
| 1264 | if (!ParamTraits<int64_t>::Read(m, iter, &value)) |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1265 | return false; |
| 1266 | *r = base::Time::FromInternalValue(value); |
| 1267 | return true; |
| 1268 | } |
| 1269 | |
| 1270 | void ParamTraits<base::Time>::Log(const param_type& p, std::string* l) { |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 1271 | ParamTraits<int64_t>::Log(p.ToInternalValue(), l); |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1272 | } |
| 1273 | |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 1274 | void ParamTraits<base::TimeDelta>::Write(base::Pickle* m, const param_type& p) { |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 1275 | ParamTraits<int64_t>::Write(m, p.ToInternalValue()); |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1276 | } |
| 1277 | |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 1278 | bool ParamTraits<base::TimeDelta>::Read(const base::Pickle* m, |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 1279 | base::PickleIterator* iter, |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1280 | param_type* r) { |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 1281 | int64_t value; |
| 1282 | bool ret = ParamTraits<int64_t>::Read(m, iter, &value); |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1283 | if (ret) |
| 1284 | *r = base::TimeDelta::FromInternalValue(value); |
| 1285 | |
| 1286 | return ret; |
| 1287 | } |
| 1288 | |
| 1289 | void ParamTraits<base::TimeDelta>::Log(const param_type& p, std::string* l) { |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 1290 | ParamTraits<int64_t>::Log(p.ToInternalValue(), l); |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1291 | } |
| 1292 | |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 1293 | void ParamTraits<base::TimeTicks>::Write(base::Pickle* m, const param_type& p) { |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 1294 | ParamTraits<int64_t>::Write(m, p.ToInternalValue()); |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1295 | } |
| 1296 | |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 1297 | bool ParamTraits<base::TimeTicks>::Read(const base::Pickle* m, |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 1298 | base::PickleIterator* iter, |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1299 | param_type* r) { |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 1300 | int64_t value; |
| 1301 | bool ret = ParamTraits<int64_t>::Read(m, iter, &value); |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1302 | if (ret) |
| 1303 | *r = base::TimeTicks::FromInternalValue(value); |
| 1304 | |
| 1305 | return ret; |
| 1306 | } |
| 1307 | |
| 1308 | void ParamTraits<base::TimeTicks>::Log(const param_type& p, std::string* l) { |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 1309 | ParamTraits<int64_t>::Log(p.ToInternalValue(), l); |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1310 | } |
| 1311 | |
tguilbert | 4a5ac60 | 2016-09-19 21:11:25 | [diff] [blame] | 1312 | // If base::UnguessableToken is no longer 128 bits, the IPC serialization logic |
| 1313 | // below should be updated. |
| 1314 | static_assert(sizeof(base::UnguessableToken) == 2 * sizeof(uint64_t), |
| 1315 | "base::UnguessableToken should be of size 2 * sizeof(uint64_t)."); |
| 1316 | |
tguilbert | 4a5ac60 | 2016-09-19 21:11:25 | [diff] [blame] | 1317 | void ParamTraits<base::UnguessableToken>::Write(base::Pickle* m, |
| 1318 | const param_type& p) { |
| 1319 | DCHECK(!p.is_empty()); |
| 1320 | |
| 1321 | ParamTraits<uint64_t>::Write(m, p.GetHighForSerialization()); |
| 1322 | ParamTraits<uint64_t>::Write(m, p.GetLowForSerialization()); |
| 1323 | } |
| 1324 | |
| 1325 | bool ParamTraits<base::UnguessableToken>::Read(const base::Pickle* m, |
| 1326 | base::PickleIterator* iter, |
| 1327 | param_type* r) { |
| 1328 | uint64_t high, low; |
| 1329 | if (!ParamTraits<uint64_t>::Read(m, iter, &high) || |
| 1330 | !ParamTraits<uint64_t>::Read(m, iter, &low)) |
| 1331 | return false; |
| 1332 | |
| 1333 | // Receiving a zeroed UnguessableToken is a security issue. |
| 1334 | if (high == 0 && low == 0) |
| 1335 | return false; |
| 1336 | |
| 1337 | *r = base::UnguessableToken::Deserialize(high, low); |
| 1338 | return true; |
| 1339 | } |
| 1340 | |
| 1341 | void ParamTraits<base::UnguessableToken>::Log(const param_type& p, |
| 1342 | std::string* l) { |
| 1343 | l->append(p.ToString()); |
| 1344 | } |
| 1345 | |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 1346 | void ParamTraits<IPC::ChannelHandle>::Write(base::Pickle* m, |
| 1347 | const param_type& p) { |
sammc | 9bf370c | 2016-11-14 03:29:08 | [diff] [blame] | 1348 | #if defined(OS_NACL_SFI) |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 1349 | WriteParam(m, p.socket); |
sammc | 9bf370c | 2016-11-14 03:29:08 | [diff] [blame] | 1350 | #else |
amistry | 3618252 | 2016-06-27 06:34:42 | [diff] [blame] | 1351 | WriteParam(m, p.mojo_handle); |
sammc | 9bf370c | 2016-11-14 03:29:08 | [diff] [blame] | 1352 | #endif |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 1353 | } |
| 1354 | |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 1355 | bool ParamTraits<IPC::ChannelHandle>::Read(const base::Pickle* m, |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 1356 | base::PickleIterator* iter, |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 1357 | param_type* r) { |
sammc | 9bf370c | 2016-11-14 03:29:08 | [diff] [blame] | 1358 | #if defined(OS_NACL_SFI) |
| 1359 | return ReadParam(m, iter, &r->socket); |
| 1360 | #else |
| 1361 | return ReadParam(m, iter, &r->mojo_handle); |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 1362 | #endif |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 1363 | } |
| 1364 | |
| 1365 | void ParamTraits<IPC::ChannelHandle>::Log(const param_type& p, |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 1366 | std::string* l) { |
sammc | 9bf370c | 2016-11-14 03:29:08 | [diff] [blame] | 1367 | l->append("ChannelHandle("); |
| 1368 | #if defined(OS_NACL_SFI) |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 1369 | ParamTraits<base::FileDescriptor>::Log(p.socket, l); |
sammc | 9bf370c | 2016-11-14 03:29:08 | [diff] [blame] | 1370 | #else |
amistry | 3618252 | 2016-06-27 06:34:42 | [diff] [blame] | 1371 | LogParam(p.mojo_handle, l); |
sammc | 9bf370c | 2016-11-14 03:29:08 | [diff] [blame] | 1372 | #endif |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 1373 | l->append(")"); |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 1374 | } |
| 1375 | |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 1376 | void ParamTraits<LogData>::Write(base::Pickle* m, const param_type& p) { |
[email protected] | 20f0487a | 2010-09-30 20:06:30 | [diff] [blame] | 1377 | WriteParam(m, p.channel); |
| 1378 | WriteParam(m, p.routing_id); |
[email protected] | 8bf55ca | 2011-10-17 22:15:27 | [diff] [blame] | 1379 | WriteParam(m, p.type); |
[email protected] | 20f0487a | 2010-09-30 20:06:30 | [diff] [blame] | 1380 | WriteParam(m, p.flags); |
| 1381 | WriteParam(m, p.sent); |
| 1382 | WriteParam(m, p.receive); |
| 1383 | WriteParam(m, p.dispatch); |
[email protected] | bae578e9 | 2012-11-15 03:17:45 | [diff] [blame] | 1384 | WriteParam(m, p.message_name); |
[email protected] | 20f0487a | 2010-09-30 20:06:30 | [diff] [blame] | 1385 | WriteParam(m, p.params); |
| 1386 | } |
| 1387 | |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 1388 | bool ParamTraits<LogData>::Read(const base::Pickle* m, |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 1389 | base::PickleIterator* iter, |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 1390 | param_type* r) { |
[email protected] | 8bf55ca | 2011-10-17 22:15:27 | [diff] [blame] | 1391 | return |
[email protected] | 20f0487a | 2010-09-30 20:06:30 | [diff] [blame] | 1392 | ReadParam(m, iter, &r->channel) && |
| 1393 | ReadParam(m, iter, &r->routing_id) && |
[email protected] | 8bf55ca | 2011-10-17 22:15:27 | [diff] [blame] | 1394 | ReadParam(m, iter, &r->type) && |
[email protected] | 20f0487a | 2010-09-30 20:06:30 | [diff] [blame] | 1395 | ReadParam(m, iter, &r->flags) && |
| 1396 | ReadParam(m, iter, &r->sent) && |
| 1397 | ReadParam(m, iter, &r->receive) && |
| 1398 | ReadParam(m, iter, &r->dispatch) && |
[email protected] | bae578e9 | 2012-11-15 03:17:45 | [diff] [blame] | 1399 | ReadParam(m, iter, &r->message_name) && |
[email protected] | 20f0487a | 2010-09-30 20:06:30 | [diff] [blame] | 1400 | ReadParam(m, iter, &r->params); |
[email protected] | 20f0487a | 2010-09-30 20:06:30 | [diff] [blame] | 1401 | } |
| 1402 | |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1403 | void ParamTraits<LogData>::Log(const param_type& p, std::string* l) { |
| 1404 | // Doesn't make sense to implement this! |
| 1405 | } |
| 1406 | |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 1407 | void ParamTraits<Message>::Write(base::Pickle* m, const Message& p) { |
Fabrice de Gans-Riberi | cbce434 | 2018-05-07 20:02:09 | [diff] [blame] | 1408 | #if defined(OS_POSIX) || defined(OS_FUCHSIA) |
[email protected] | 34d4861 | 2012-06-29 00:05:04 | [diff] [blame] | 1409 | // We don't serialize the file descriptors in the nested message, so there |
| 1410 | // better not be any. |
morrita | 1aa788c | 2015-01-31 05:45:42 | [diff] [blame] | 1411 | DCHECK(!p.HasAttachments()); |
[email protected] | 34d4861 | 2012-06-29 00:05:04 | [diff] [blame] | 1412 | #endif |
| 1413 | |
| 1414 | // Don't just write out the message. This is used to send messages between |
| 1415 | // NaCl (Posix environment) and the browser (could be on Windows). The message |
| 1416 | // header formats differ between these systems (so does handle sharing, but |
| 1417 | // we already asserted we don't have any handles). So just write out the |
| 1418 | // parts of the header we use. |
| 1419 | // |
| 1420 | // Be careful also to use only explicitly-sized types. The NaCl environment |
| 1421 | // could be 64-bit and the host browser could be 32-bits. The nested message |
| 1422 | // may or may not be safe to send between 32-bit and 64-bit systems, but we |
| 1423 | // leave that up to the code sending the message to ensure. |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 1424 | m->WriteUInt32(static_cast<uint32_t>(p.routing_id())); |
[email protected] | 34d4861 | 2012-06-29 00:05:04 | [diff] [blame] | 1425 | m->WriteUInt32(p.type()); |
| 1426 | m->WriteUInt32(p.flags()); |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 1427 | m->WriteData(p.payload(), static_cast<uint32_t>(p.payload_size())); |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1428 | } |
| 1429 | |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 1430 | bool ParamTraits<Message>::Read(const base::Pickle* m, |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 1431 | base::PickleIterator* iter, |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1432 | Message* r) { |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 1433 | uint32_t routing_id, type, flags; |
avi | 48fc13b | 2014-12-28 23:31:48 | [diff] [blame] | 1434 | if (!iter->ReadUInt32(&routing_id) || |
| 1435 | !iter->ReadUInt32(&type) || |
| 1436 | !iter->ReadUInt32(&flags)) |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1437 | return false; |
[email protected] | 34d4861 | 2012-06-29 00:05:04 | [diff] [blame] | 1438 | |
| 1439 | int payload_size; |
| 1440 | const char* payload; |
avi | 48fc13b | 2014-12-28 23:31:48 | [diff] [blame] | 1441 | if (!iter->ReadData(&payload, &payload_size)) |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1442 | return false; |
[email protected] | 34d4861 | 2012-06-29 00:05:04 | [diff] [blame] | 1443 | |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 1444 | r->SetHeaderValues(static_cast<int32_t>(routing_id), type, flags); |
Daniel Cheng | 0d89f922 | 2017-09-22 05:05:07 | [diff] [blame] | 1445 | r->WriteBytes(payload, payload_size); |
| 1446 | return true; |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1447 | } |
| 1448 | |
| 1449 | void ParamTraits<Message>::Log(const Message& p, std::string* l) { |
| 1450 | l->append("<IPC::Message>"); |
| 1451 | } |
| 1452 | |
| 1453 | #if defined(OS_WIN) |
| 1454 | // 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] | 1455 | // 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] | 1456 | void ParamTraits<HANDLE>::Write(base::Pickle* m, const param_type& p) { |
[email protected] | 4a635b7 | 2013-03-04 02:29:03 | [diff] [blame] | 1457 | m->WriteInt(HandleToLong(p)); |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1458 | } |
| 1459 | |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 1460 | bool ParamTraits<HANDLE>::Read(const base::Pickle* m, |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 1461 | base::PickleIterator* iter, |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1462 | param_type* r) { |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 1463 | int32_t temp; |
avi | 48fc13b | 2014-12-28 23:31:48 | [diff] [blame] | 1464 | if (!iter->ReadInt(&temp)) |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1465 | return false; |
[email protected] | 4a635b7 | 2013-03-04 02:29:03 | [diff] [blame] | 1466 | *r = LongToHandle(temp); |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1467 | return true; |
| 1468 | } |
| 1469 | |
| 1470 | void ParamTraits<HANDLE>::Log(const param_type& p, std::string* l) { |
brucedawson | 5604a11d | 2015-10-06 19:22:00 | [diff] [blame] | 1471 | l->append(base::StringPrintf("0x%p", p)); |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1472 | } |
| 1473 | |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 1474 | void ParamTraits<MSG>::Write(base::Pickle* m, const param_type& p) { |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1475 | m->WriteData(reinterpret_cast<const char*>(&p), sizeof(MSG)); |
| 1476 | } |
| 1477 | |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 1478 | bool ParamTraits<MSG>::Read(const base::Pickle* m, |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 1479 | base::PickleIterator* iter, |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1480 | param_type* r) { |
| 1481 | const char *data; |
| 1482 | int data_size = 0; |
avi | 48fc13b | 2014-12-28 23:31:48 | [diff] [blame] | 1483 | bool result = iter->ReadData(&data, &data_size); |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 1484 | if (result && data_size == sizeof(MSG)) { |
| 1485 | memcpy(r, data, sizeof(MSG)); |
| 1486 | } else { |
| 1487 | result = false; |
| 1488 | NOTREACHED(); |
| 1489 | } |
| 1490 | |
| 1491 | return result; |
| 1492 | } |
| 1493 | |
| 1494 | void ParamTraits<MSG>::Log(const param_type& p, std::string* l) { |
| 1495 | l->append("<MSG>"); |
| 1496 | } |
| 1497 | |
| 1498 | #endif // OS_WIN |
| 1499 | |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 1500 | } // namespace IPC |