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