[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 | |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 7 | #include "base/files/file_path.h" |
[email protected] | 93d49d7 | 2009-10-23 20:00:20 | [diff] [blame] | 8 | #include "base/json/json_writer.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 9 | #include "base/memory/scoped_ptr.h" |
[email protected] | 0238a16 | 2013-06-13 13:47:46 | [diff] [blame] | 10 | #include "base/strings/nullable_string16.h" |
[email protected] | 4aa794a1 | 2013-06-11 06:32:18 | [diff] [blame] | 11 | #include "base/strings/string_number_conversions.h" |
[email protected] | 90626587 | 2013-06-07 22:40:45 | [diff] [blame] | 12 | #include "base/strings/utf_string_conversions.h" |
[email protected] | b43e556 | 2013-06-28 15:20:02 | [diff] [blame] | 13 | #include "base/time/time.h" |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 14 | #include "base/values.h" |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 15 | #include "ipc/ipc_channel_handle.h" |
morrita | 1aa788c | 2015-01-31 05:45:42 | [diff] [blame^] | 16 | #include "ipc/ipc_message_attachment.h" |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 17 | #include "ipc/ipc_message_attachment_set.h" |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 18 | |
morrita | 1aa788c | 2015-01-31 05:45:42 | [diff] [blame^] | 19 | #if defined(OS_POSIX) |
| 20 | #include "ipc/ipc_platform_file_attachment_posix.h" |
| 21 | #endif |
| 22 | |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 23 | #if defined(OS_WIN) |
[email protected] | 2e02cfe8 | 2012-11-21 00:58:00 | [diff] [blame] | 24 | #include <tchar.h> |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 25 | #endif |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 26 | |
| 27 | namespace IPC { |
| 28 | |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 29 | namespace { |
| 30 | |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 31 | const int kMaxRecursionDepth = 100; |
| 32 | |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 33 | template<typename CharType> |
| 34 | void LogBytes(const std::vector<CharType>& data, std::string* out) { |
| 35 | #if defined(OS_WIN) |
| 36 | // Windows has a GUI for logging, which can handle arbitrary binary data. |
| 37 | for (size_t i = 0; i < data.size(); ++i) |
| 38 | out->push_back(data[i]); |
| 39 | #else |
| 40 | // On POSIX, we log to stdout, which we assume can display ASCII. |
| 41 | static const size_t kMaxBytesToLog = 100; |
| 42 | for (size_t i = 0; i < std::min(data.size(), kMaxBytesToLog); ++i) { |
| 43 | if (isprint(data[i])) |
| 44 | out->push_back(data[i]); |
| 45 | else |
[email protected] | 7d3cbc9 | 2013-03-18 22:33:04 | [diff] [blame] | 46 | out->append( |
| 47 | base::StringPrintf("[%02X]", static_cast<unsigned char>(data[i]))); |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 48 | } |
| 49 | if (data.size() > kMaxBytesToLog) { |
[email protected] | f8660f8 | 2013-03-30 17:29:28 | [diff] [blame] | 50 | out->append(base::StringPrintf( |
| 51 | " and %u more bytes", |
| 52 | static_cast<unsigned>(data.size() - kMaxBytesToLog))); |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 53 | } |
| 54 | #endif |
| 55 | } |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 56 | |
[email protected] | ea5ef4c | 2013-06-13 22:50:27 | [diff] [blame] | 57 | bool ReadValue(const Message* m, PickleIterator* iter, base::Value** value, |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 58 | int recursion); |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 59 | |
[email protected] | ea5ef4c | 2013-06-13 22:50:27 | [diff] [blame] | 60 | void WriteValue(Message* m, const base::Value* value, int recursion) { |
[email protected] | dbc761a | 2012-07-26 01:29:21 | [diff] [blame] | 61 | bool result; |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 62 | if (recursion > kMaxRecursionDepth) { |
| 63 | LOG(WARNING) << "Max recursion depth hit in WriteValue."; |
| 64 | return; |
| 65 | } |
| 66 | |
| 67 | m->WriteInt(value->GetType()); |
| 68 | |
| 69 | switch (value->GetType()) { |
[email protected] | ea5ef4c | 2013-06-13 22:50:27 | [diff] [blame] | 70 | case base::Value::TYPE_NULL: |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 71 | break; |
[email protected] | ea5ef4c | 2013-06-13 22:50:27 | [diff] [blame] | 72 | case base::Value::TYPE_BOOLEAN: { |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 73 | bool val; |
[email protected] | dbc761a | 2012-07-26 01:29:21 | [diff] [blame] | 74 | result = value->GetAsBoolean(&val); |
| 75 | DCHECK(result); |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 76 | WriteParam(m, val); |
| 77 | break; |
| 78 | } |
[email protected] | ea5ef4c | 2013-06-13 22:50:27 | [diff] [blame] | 79 | case base::Value::TYPE_INTEGER: { |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 80 | int val; |
[email protected] | dbc761a | 2012-07-26 01:29:21 | [diff] [blame] | 81 | result = value->GetAsInteger(&val); |
| 82 | DCHECK(result); |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 83 | WriteParam(m, val); |
| 84 | break; |
| 85 | } |
[email protected] | ea5ef4c | 2013-06-13 22:50:27 | [diff] [blame] | 86 | case base::Value::TYPE_DOUBLE: { |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 87 | double val; |
[email protected] | dbc761a | 2012-07-26 01:29:21 | [diff] [blame] | 88 | result = value->GetAsDouble(&val); |
| 89 | DCHECK(result); |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 90 | WriteParam(m, val); |
| 91 | break; |
| 92 | } |
[email protected] | ea5ef4c | 2013-06-13 22:50:27 | [diff] [blame] | 93 | case base::Value::TYPE_STRING: { |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 94 | std::string val; |
[email protected] | dbc761a | 2012-07-26 01:29:21 | [diff] [blame] | 95 | result = value->GetAsString(&val); |
| 96 | DCHECK(result); |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 97 | WriteParam(m, val); |
| 98 | break; |
| 99 | } |
[email protected] | ea5ef4c | 2013-06-13 22:50:27 | [diff] [blame] | 100 | case base::Value::TYPE_BINARY: { |
[email protected] | b59ea31 | 2011-08-05 18:20:05 | [diff] [blame] | 101 | const base::BinaryValue* binary = |
| 102 | static_cast<const base::BinaryValue*>(value); |
[email protected] | 7ee1a44c | 2010-07-23 14:18:59 | [diff] [blame] | 103 | m->WriteData(binary->GetBuffer(), static_cast<int>(binary->GetSize())); |
[email protected] | e4dad9fb | 2009-10-06 18:15:58 | [diff] [blame] | 104 | break; |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 105 | } |
[email protected] | ea5ef4c | 2013-06-13 22:50:27 | [diff] [blame] | 106 | case base::Value::TYPE_DICTIONARY: { |
| 107 | const base::DictionaryValue* dict = |
| 108 | static_cast<const base::DictionaryValue*>(value); |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 109 | |
[email protected] | 4dad9ad8 | 2009-11-25 20:47:52 | [diff] [blame] | 110 | WriteParam(m, static_cast<int>(dict->size())); |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 111 | |
[email protected] | ea5ef4c | 2013-06-13 22:50:27 | [diff] [blame] | 112 | for (base::DictionaryValue::Iterator it(*dict); !it.IsAtEnd(); |
| 113 | it.Advance()) { |
[email protected] | a899c0b0 | 2013-01-18 14:43:27 | [diff] [blame] | 114 | WriteParam(m, it.key()); |
| 115 | WriteValue(m, &it.value(), recursion + 1); |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 116 | } |
| 117 | break; |
| 118 | } |
[email protected] | ea5ef4c | 2013-06-13 22:50:27 | [diff] [blame] | 119 | case base::Value::TYPE_LIST: { |
| 120 | const base::ListValue* list = static_cast<const base::ListValue*>(value); |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 121 | WriteParam(m, static_cast<int>(list->GetSize())); |
[email protected] | ea5ef4c | 2013-06-13 22:50:27 | [diff] [blame] | 122 | for (base::ListValue::const_iterator it = list->begin(); |
| 123 | it != list->end(); ++it) { |
[email protected] | a899c0b0 | 2013-01-18 14:43:27 | [diff] [blame] | 124 | WriteValue(m, *it, recursion + 1); |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 125 | } |
| 126 | break; |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | // Helper for ReadValue that reads a DictionaryValue into a pre-allocated |
| 132 | // object. |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 133 | bool ReadDictionaryValue(const Message* m, PickleIterator* iter, |
[email protected] | ea5ef4c | 2013-06-13 22:50:27 | [diff] [blame] | 134 | base::DictionaryValue* value, int recursion) { |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 135 | int size; |
| 136 | if (!ReadParam(m, iter, &size)) |
| 137 | return false; |
| 138 | |
| 139 | for (int i = 0; i < size; ++i) { |
[email protected] | e7b418b | 2010-07-30 19:47:47 | [diff] [blame] | 140 | std::string key; |
[email protected] | 0c6c1e4 | 2013-06-21 19:42:19 | [diff] [blame] | 141 | base::Value* subval; |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 142 | if (!ReadParam(m, iter, &key) || |
| 143 | !ReadValue(m, iter, &subval, recursion + 1)) |
| 144 | return false; |
[email protected] | d8b4aa4 | 2011-08-19 05:59:57 | [diff] [blame] | 145 | value->SetWithoutPathExpansion(key, subval); |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | return true; |
| 149 | } |
| 150 | |
| 151 | // Helper for ReadValue that reads a ReadListValue into a pre-allocated |
| 152 | // object. |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 153 | bool ReadListValue(const Message* m, PickleIterator* iter, |
[email protected] | ea5ef4c | 2013-06-13 22:50:27 | [diff] [blame] | 154 | base::ListValue* value, int recursion) { |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 155 | int size; |
| 156 | if (!ReadParam(m, iter, &size)) |
| 157 | return false; |
| 158 | |
| 159 | for (int i = 0; i < size; ++i) { |
[email protected] | ea5ef4c | 2013-06-13 22:50:27 | [diff] [blame] | 160 | base::Value* subval; |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 161 | if (!ReadValue(m, iter, &subval, recursion + 1)) |
| 162 | return false; |
| 163 | value->Set(i, subval); |
| 164 | } |
| 165 | |
| 166 | return true; |
| 167 | } |
| 168 | |
[email protected] | ea5ef4c | 2013-06-13 22:50:27 | [diff] [blame] | 169 | bool ReadValue(const Message* m, PickleIterator* iter, base::Value** value, |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 170 | int recursion) { |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 171 | if (recursion > kMaxRecursionDepth) { |
| 172 | LOG(WARNING) << "Max recursion depth hit in ReadValue."; |
| 173 | return false; |
| 174 | } |
| 175 | |
| 176 | int type; |
| 177 | if (!ReadParam(m, iter, &type)) |
| 178 | return false; |
| 179 | |
| 180 | switch (type) { |
[email protected] | ea5ef4c | 2013-06-13 22:50:27 | [diff] [blame] | 181 | case base::Value::TYPE_NULL: |
| 182 | *value = base::Value::CreateNullValue(); |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 183 | break; |
[email protected] | ea5ef4c | 2013-06-13 22:50:27 | [diff] [blame] | 184 | case base::Value::TYPE_BOOLEAN: { |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 185 | bool val; |
| 186 | if (!ReadParam(m, iter, &val)) |
| 187 | return false; |
[email protected] | 4038a13 | 2013-01-30 05:24:07 | [diff] [blame] | 188 | *value = new base::FundamentalValue(val); |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 189 | break; |
| 190 | } |
[email protected] | ea5ef4c | 2013-06-13 22:50:27 | [diff] [blame] | 191 | case base::Value::TYPE_INTEGER: { |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 192 | int val; |
| 193 | if (!ReadParam(m, iter, &val)) |
| 194 | return false; |
[email protected] | 4038a13 | 2013-01-30 05:24:07 | [diff] [blame] | 195 | *value = new base::FundamentalValue(val); |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 196 | break; |
| 197 | } |
[email protected] | ea5ef4c | 2013-06-13 22:50:27 | [diff] [blame] | 198 | case base::Value::TYPE_DOUBLE: { |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 199 | double val; |
| 200 | if (!ReadParam(m, iter, &val)) |
| 201 | return false; |
[email protected] | 4038a13 | 2013-01-30 05:24:07 | [diff] [blame] | 202 | *value = new base::FundamentalValue(val); |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 203 | break; |
| 204 | } |
[email protected] | ea5ef4c | 2013-06-13 22:50:27 | [diff] [blame] | 205 | case base::Value::TYPE_STRING: { |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 206 | std::string val; |
| 207 | if (!ReadParam(m, iter, &val)) |
| 208 | return false; |
[email protected] | 4038a13 | 2013-01-30 05:24:07 | [diff] [blame] | 209 | *value = new base::StringValue(val); |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 210 | break; |
| 211 | } |
[email protected] | ea5ef4c | 2013-06-13 22:50:27 | [diff] [blame] | 212 | case base::Value::TYPE_BINARY: { |
[email protected] | e4dad9fb | 2009-10-06 18:15:58 | [diff] [blame] | 213 | const char* data; |
| 214 | int length; |
avi | 48fc13b | 2014-12-28 23:31:48 | [diff] [blame] | 215 | if (!iter->ReadData(&data, &length)) |
[email protected] | e4dad9fb | 2009-10-06 18:15:58 | [diff] [blame] | 216 | return false; |
[email protected] | b59ea31 | 2011-08-05 18:20:05 | [diff] [blame] | 217 | *value = base::BinaryValue::CreateWithCopiedBuffer(data, length); |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 218 | break; |
| 219 | } |
[email protected] | ea5ef4c | 2013-06-13 22:50:27 | [diff] [blame] | 220 | case base::Value::TYPE_DICTIONARY: { |
| 221 | scoped_ptr<base::DictionaryValue> val(new base::DictionaryValue()); |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 222 | if (!ReadDictionaryValue(m, iter, val.get(), recursion)) |
| 223 | return false; |
| 224 | *value = val.release(); |
| 225 | break; |
| 226 | } |
[email protected] | ea5ef4c | 2013-06-13 22:50:27 | [diff] [blame] | 227 | case base::Value::TYPE_LIST: { |
| 228 | scoped_ptr<base::ListValue> val(new base::ListValue()); |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 229 | if (!ReadListValue(m, iter, val.get(), recursion)) |
| 230 | return false; |
| 231 | *value = val.release(); |
| 232 | break; |
| 233 | } |
[email protected] | e4dad9fb | 2009-10-06 18:15:58 | [diff] [blame] | 234 | default: |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 235 | return false; |
| 236 | } |
| 237 | |
| 238 | return true; |
| 239 | } |
| 240 | |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 241 | } // namespace |
| 242 | |
| 243 | // ----------------------------------------------------------------------------- |
| 244 | |
| 245 | LogData::LogData() |
| 246 | : routing_id(0), |
| 247 | type(0), |
| 248 | sent(0), |
| 249 | receive(0), |
| 250 | dispatch(0) { |
| 251 | } |
| 252 | |
| 253 | LogData::~LogData() { |
| 254 | } |
| 255 | |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 256 | void ParamTraits<bool>::Log(const param_type& p, std::string* l) { |
| 257 | l->append(p ? "true" : "false"); |
| 258 | } |
| 259 | |
[email protected] | c1ee48d | 2013-07-12 23:12:28 | [diff] [blame] | 260 | void ParamTraits<unsigned char>::Write(Message* m, const param_type& p) { |
| 261 | m->WriteBytes(&p, sizeof(param_type)); |
| 262 | } |
| 263 | |
| 264 | bool ParamTraits<unsigned char>::Read(const Message* m, PickleIterator* iter, |
| 265 | param_type* r) { |
| 266 | const char* data; |
avi | 48fc13b | 2014-12-28 23:31:48 | [diff] [blame] | 267 | if (!iter->ReadBytes(&data, sizeof(param_type))) |
[email protected] | c1ee48d | 2013-07-12 23:12:28 | [diff] [blame] | 268 | return false; |
| 269 | memcpy(r, data, sizeof(param_type)); |
| 270 | return true; |
| 271 | } |
| 272 | |
| 273 | void ParamTraits<unsigned char>::Log(const param_type& p, std::string* l) { |
| 274 | l->append(base::UintToString(p)); |
| 275 | } |
| 276 | |
| 277 | void ParamTraits<unsigned short>::Write(Message* m, const param_type& p) { |
| 278 | m->WriteBytes(&p, sizeof(param_type)); |
| 279 | } |
| 280 | |
| 281 | bool ParamTraits<unsigned short>::Read(const Message* m, PickleIterator* iter, |
| 282 | param_type* r) { |
| 283 | const char* data; |
avi | 48fc13b | 2014-12-28 23:31:48 | [diff] [blame] | 284 | if (!iter->ReadBytes(&data, sizeof(param_type))) |
[email protected] | c1ee48d | 2013-07-12 23:12:28 | [diff] [blame] | 285 | return false; |
| 286 | memcpy(r, data, sizeof(param_type)); |
| 287 | return true; |
| 288 | } |
| 289 | |
| 290 | void ParamTraits<unsigned short>::Log(const param_type& p, std::string* l) { |
| 291 | l->append(base::UintToString(p)); |
| 292 | } |
| 293 | |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 294 | void ParamTraits<int>::Log(const param_type& p, std::string* l) { |
| 295 | l->append(base::IntToString(p)); |
| 296 | } |
| 297 | |
| 298 | void ParamTraits<unsigned int>::Log(const param_type& p, std::string* l) { |
| 299 | l->append(base::UintToString(p)); |
| 300 | } |
| 301 | |
| 302 | void ParamTraits<long>::Log(const param_type& p, std::string* l) { |
| 303 | l->append(base::Int64ToString(static_cast<int64>(p))); |
| 304 | } |
| 305 | |
| 306 | void ParamTraits<unsigned long>::Log(const param_type& p, std::string* l) { |
| 307 | l->append(base::Uint64ToString(static_cast<uint64>(p))); |
| 308 | } |
| 309 | |
| 310 | void ParamTraits<long long>::Log(const param_type& p, std::string* l) { |
| 311 | l->append(base::Int64ToString(static_cast<int64>(p))); |
| 312 | } |
| 313 | |
| 314 | void ParamTraits<unsigned long long>::Log(const param_type& p, std::string* l) { |
| 315 | l->append(base::Uint64ToString(p)); |
| 316 | } |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 317 | |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 318 | void ParamTraits<float>::Log(const param_type& p, std::string* l) { |
[email protected] | 7d3cbc9 | 2013-03-18 22:33:04 | [diff] [blame] | 319 | l->append(base::StringPrintf("%e", p)); |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 320 | } |
| 321 | |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 322 | void ParamTraits<double>::Write(Message* m, const param_type& p) { |
[email protected] | 48328ff | 2013-10-31 09:27:31 | [diff] [blame] | 323 | m->WriteBytes(reinterpret_cast<const char*>(&p), sizeof(param_type)); |
[email protected] | d84e48b | 2010-10-21 22:04:52 | [diff] [blame] | 324 | } |
| 325 | |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 326 | bool ParamTraits<double>::Read(const Message* m, PickleIterator* iter, |
| 327 | param_type* r) { |
| 328 | const char *data; |
avi | 48fc13b | 2014-12-28 23:31:48 | [diff] [blame] | 329 | if (!iter->ReadBytes(&data, sizeof(*r))) { |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 330 | NOTREACHED(); |
| 331 | return false; |
| 332 | } |
| 333 | memcpy(r, data, sizeof(param_type)); |
| 334 | return true; |
[email protected] | d84e48b | 2010-10-21 22:04:52 | [diff] [blame] | 335 | } |
| 336 | |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 337 | void ParamTraits<double>::Log(const param_type& p, std::string* l) { |
[email protected] | 7d3cbc9 | 2013-03-18 22:33:04 | [diff] [blame] | 338 | l->append(base::StringPrintf("%e", p)); |
[email protected] | 1d14f58 | 2011-09-02 20:42:04 | [diff] [blame] | 339 | } |
| 340 | |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 341 | |
| 342 | void ParamTraits<std::string>::Log(const param_type& p, std::string* l) { |
| 343 | l->append(p); |
[email protected] | 1d14f58 | 2011-09-02 20:42:04 | [diff] [blame] | 344 | } |
| 345 | |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 346 | void ParamTraits<std::wstring>::Log(const param_type& p, std::string* l) { |
[email protected] | ad65a3e | 2013-12-25 18:18:01 | [diff] [blame] | 347 | l->append(base::WideToUTF8(p)); |
[email protected] | 1d14f58 | 2011-09-02 20:42:04 | [diff] [blame] | 348 | } |
| 349 | |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 350 | #if !defined(WCHAR_T_IS_UTF16) |
[email protected] | 476dafb | 2013-12-03 00:39:26 | [diff] [blame] | 351 | void ParamTraits<base::string16>::Log(const param_type& p, std::string* l) { |
[email protected] | ad65a3e | 2013-12-25 18:18:01 | [diff] [blame] | 352 | l->append(base::UTF16ToUTF8(p)); |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 353 | } |
| 354 | #endif |
| 355 | |
| 356 | void ParamTraits<std::vector<char> >::Write(Message* m, const param_type& p) { |
| 357 | if (p.empty()) { |
| 358 | m->WriteData(NULL, 0); |
| 359 | } else { |
| 360 | m->WriteData(&p.front(), static_cast<int>(p.size())); |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | bool ParamTraits<std::vector<char> >::Read(const Message* m, |
| 365 | PickleIterator* iter, |
| 366 | param_type* r) { |
| 367 | const char *data; |
| 368 | int data_size = 0; |
avi | 48fc13b | 2014-12-28 23:31:48 | [diff] [blame] | 369 | if (!iter->ReadData(&data, &data_size) || data_size < 0) |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 370 | return false; |
| 371 | r->resize(data_size); |
| 372 | if (data_size) |
| 373 | memcpy(&r->front(), data, data_size); |
| 374 | return true; |
| 375 | } |
| 376 | |
| 377 | void ParamTraits<std::vector<char> >::Log(const param_type& p, std::string* l) { |
| 378 | LogBytes(p, l); |
| 379 | } |
| 380 | |
| 381 | void ParamTraits<std::vector<unsigned char> >::Write(Message* m, |
| 382 | const param_type& p) { |
| 383 | if (p.empty()) { |
| 384 | m->WriteData(NULL, 0); |
| 385 | } else { |
| 386 | m->WriteData(reinterpret_cast<const char*>(&p.front()), |
| 387 | static_cast<int>(p.size())); |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | bool ParamTraits<std::vector<unsigned char> >::Read(const Message* m, |
| 392 | PickleIterator* iter, |
| 393 | param_type* r) { |
| 394 | const char *data; |
| 395 | int data_size = 0; |
avi | 48fc13b | 2014-12-28 23:31:48 | [diff] [blame] | 396 | if (!iter->ReadData(&data, &data_size) || data_size < 0) |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 397 | return false; |
| 398 | r->resize(data_size); |
| 399 | if (data_size) |
| 400 | memcpy(&r->front(), data, data_size); |
| 401 | return true; |
| 402 | } |
| 403 | |
| 404 | void ParamTraits<std::vector<unsigned char> >::Log(const param_type& p, |
| 405 | std::string* l) { |
| 406 | LogBytes(p, l); |
| 407 | } |
| 408 | |
| 409 | void ParamTraits<std::vector<bool> >::Write(Message* m, const param_type& p) { |
| 410 | WriteParam(m, static_cast<int>(p.size())); |
[email protected] | d412485 | 2013-03-20 20:25:00 | [diff] [blame] | 411 | // Cast to bool below is required because libc++'s |
| 412 | // vector<bool>::const_reference is different from bool, and we want to avoid |
| 413 | // writing an extra specialization of ParamTraits for it. |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 414 | for (size_t i = 0; i < p.size(); i++) |
[email protected] | d412485 | 2013-03-20 20:25:00 | [diff] [blame] | 415 | WriteParam(m, static_cast<bool>(p[i])); |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | bool ParamTraits<std::vector<bool> >::Read(const Message* m, |
| 419 | PickleIterator* iter, |
| 420 | param_type* r) { |
| 421 | int size; |
| 422 | // ReadLength() checks for < 0 itself. |
avi | 48fc13b | 2014-12-28 23:31:48 | [diff] [blame] | 423 | if (!iter->ReadLength(&size)) |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 424 | return false; |
| 425 | r->resize(size); |
| 426 | for (int i = 0; i < size; i++) { |
| 427 | bool value; |
| 428 | if (!ReadParam(m, iter, &value)) |
| 429 | return false; |
| 430 | (*r)[i] = value; |
| 431 | } |
| 432 | return true; |
| 433 | } |
| 434 | |
| 435 | void ParamTraits<std::vector<bool> >::Log(const param_type& p, std::string* l) { |
| 436 | for (size_t i = 0; i < p.size(); ++i) { |
| 437 | if (i != 0) |
| 438 | l->push_back(' '); |
[email protected] | d412485 | 2013-03-20 20:25:00 | [diff] [blame] | 439 | LogParam(static_cast<bool>(p[i]), l); |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 440 | } |
[email protected] | d84e48b | 2010-10-21 22:04:52 | [diff] [blame] | 441 | } |
| 442 | |
[email protected] | ea5ef4c | 2013-06-13 22:50:27 | [diff] [blame] | 443 | void ParamTraits<base::DictionaryValue>::Write(Message* m, |
| 444 | const param_type& p) { |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 445 | WriteValue(m, &p, 0); |
| 446 | } |
| 447 | |
[email protected] | ea5ef4c | 2013-06-13 22:50:27 | [diff] [blame] | 448 | bool ParamTraits<base::DictionaryValue>::Read( |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 449 | const Message* m, PickleIterator* iter, param_type* r) { |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 450 | int type; |
[email protected] | 0c6c1e4 | 2013-06-21 19:42:19 | [diff] [blame] | 451 | if (!ReadParam(m, iter, &type) || type != base::Value::TYPE_DICTIONARY) |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 452 | return false; |
| 453 | |
| 454 | return ReadDictionaryValue(m, iter, r, 0); |
| 455 | } |
| 456 | |
[email protected] | ea5ef4c | 2013-06-13 22:50:27 | [diff] [blame] | 457 | void ParamTraits<base::DictionaryValue>::Log(const param_type& p, |
| 458 | std::string* l) { |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 459 | std::string json; |
[email protected] | 4abb460 | 2012-03-16 01:59:55 | [diff] [blame] | 460 | base::JSONWriter::Write(&p, &json); |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 461 | l->append(json); |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 462 | } |
| 463 | |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 464 | #if defined(OS_POSIX) |
| 465 | void ParamTraits<base::FileDescriptor>::Write(Message* m, const param_type& p) { |
| 466 | const bool valid = p.fd >= 0; |
| 467 | WriteParam(m, valid); |
| 468 | |
morrita | 9669385 | 2014-09-24 20:11:45 | [diff] [blame] | 469 | if (!valid) |
| 470 | return; |
| 471 | |
| 472 | if (p.auto_close) { |
morrita | 1aa788c | 2015-01-31 05:45:42 | [diff] [blame^] | 473 | if (!m->WriteAttachment( |
| 474 | new internal::PlatformFileAttachment(base::ScopedFD(p.fd)))) |
morrita | 9669385 | 2014-09-24 20:11:45 | [diff] [blame] | 475 | NOTREACHED(); |
| 476 | } else { |
morrita | 1aa788c | 2015-01-31 05:45:42 | [diff] [blame^] | 477 | if (!m->WriteAttachment(new internal::PlatformFileAttachment(p.fd))) |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 478 | NOTREACHED(); |
| 479 | } |
| 480 | } |
| 481 | |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 482 | bool ParamTraits<base::FileDescriptor>::Read(const Message* m, |
| 483 | PickleIterator* iter, |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 484 | param_type* r) { |
morrita | 9669385 | 2014-09-24 20:11:45 | [diff] [blame] | 485 | *r = base::FileDescriptor(); |
| 486 | |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 487 | bool valid; |
| 488 | if (!ReadParam(m, iter, &valid)) |
| 489 | return false; |
| 490 | |
morrita | 9669385 | 2014-09-24 20:11:45 | [diff] [blame] | 491 | // TODO(morrita): Seems like this should return false. |
| 492 | if (!valid) |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 493 | return true; |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 494 | |
morrita | 1aa788c | 2015-01-31 05:45:42 | [diff] [blame^] | 495 | scoped_refptr<MessageAttachment> attachment; |
| 496 | if (!m->ReadAttachment(iter, &attachment)) |
morrita | 9669385 | 2014-09-24 20:11:45 | [diff] [blame] | 497 | return false; |
| 498 | |
morrita | 1aa788c | 2015-01-31 05:45:42 | [diff] [blame^] | 499 | *r = base::FileDescriptor(attachment->TakePlatformFile(), true); |
morrita | 9669385 | 2014-09-24 20:11:45 | [diff] [blame] | 500 | return true; |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | void ParamTraits<base::FileDescriptor>::Log(const param_type& p, |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 504 | std::string* l) { |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 505 | if (p.auto_close) { |
[email protected] | 7d3cbc9 | 2013-03-18 22:33:04 | [diff] [blame] | 506 | l->append(base::StringPrintf("FD(%d auto-close)", p.fd)); |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 507 | } else { |
[email protected] | 7d3cbc9 | 2013-03-18 22:33:04 | [diff] [blame] | 508 | l->append(base::StringPrintf("FD(%d)", p.fd)); |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 509 | } |
| 510 | } |
| 511 | #endif // defined(OS_POSIX) |
| 512 | |
[email protected] | 6d4b67a | 2013-02-10 04:49:30 | [diff] [blame] | 513 | void ParamTraits<base::FilePath>::Write(Message* m, const param_type& p) { |
[email protected] | aeae59f | 2013-01-28 13:47:55 | [diff] [blame] | 514 | p.WriteToPickle(m); |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 515 | } |
| 516 | |
[email protected] | 6d4b67a | 2013-02-10 04:49:30 | [diff] [blame] | 517 | bool ParamTraits<base::FilePath>::Read(const Message* m, |
| 518 | PickleIterator* iter, |
| 519 | param_type* r) { |
[email protected] | aeae59f | 2013-01-28 13:47:55 | [diff] [blame] | 520 | return r->ReadFromPickle(iter); |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 521 | } |
| 522 | |
[email protected] | 6d4b67a | 2013-02-10 04:49:30 | [diff] [blame] | 523 | void ParamTraits<base::FilePath>::Log(const param_type& p, std::string* l) { |
| 524 | ParamTraits<base::FilePath::StringType>::Log(p.value(), l); |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 525 | } |
| 526 | |
[email protected] | ea5ef4c | 2013-06-13 22:50:27 | [diff] [blame] | 527 | void ParamTraits<base::ListValue>::Write(Message* m, const param_type& p) { |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 528 | WriteValue(m, &p, 0); |
| 529 | } |
| 530 | |
[email protected] | ea5ef4c | 2013-06-13 22:50:27 | [diff] [blame] | 531 | bool ParamTraits<base::ListValue>::Read( |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 532 | const Message* m, PickleIterator* iter, param_type* r) { |
| 533 | int type; |
[email protected] | 0c6c1e4 | 2013-06-21 19:42:19 | [diff] [blame] | 534 | if (!ReadParam(m, iter, &type) || type != base::Value::TYPE_LIST) |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 535 | return false; |
| 536 | |
| 537 | return ReadListValue(m, iter, r, 0); |
| 538 | } |
| 539 | |
[email protected] | ea5ef4c | 2013-06-13 22:50:27 | [diff] [blame] | 540 | void ParamTraits<base::ListValue>::Log(const param_type& p, std::string* l) { |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 541 | std::string json; |
| 542 | base::JSONWriter::Write(&p, &json); |
| 543 | l->append(json); |
| 544 | } |
| 545 | |
[email protected] | 0238a16 | 2013-06-13 13:47:46 | [diff] [blame] | 546 | void ParamTraits<base::NullableString16>::Write(Message* m, |
| 547 | const param_type& p) { |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 548 | WriteParam(m, p.string()); |
| 549 | WriteParam(m, p.is_null()); |
| 550 | } |
| 551 | |
[email protected] | 0238a16 | 2013-06-13 13:47:46 | [diff] [blame] | 552 | bool ParamTraits<base::NullableString16>::Read(const Message* m, |
| 553 | PickleIterator* iter, |
| 554 | param_type* r) { |
[email protected] | 476dafb | 2013-12-03 00:39:26 | [diff] [blame] | 555 | base::string16 string; |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 556 | if (!ReadParam(m, iter, &string)) |
| 557 | return false; |
| 558 | bool is_null; |
| 559 | if (!ReadParam(m, iter, &is_null)) |
| 560 | return false; |
[email protected] | 0238a16 | 2013-06-13 13:47:46 | [diff] [blame] | 561 | *r = base::NullableString16(string, is_null); |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 562 | return true; |
| 563 | } |
| 564 | |
[email protected] | 0238a16 | 2013-06-13 13:47:46 | [diff] [blame] | 565 | void ParamTraits<base::NullableString16>::Log(const param_type& p, |
| 566 | std::string* l) { |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 567 | l->append("("); |
| 568 | LogParam(p.string(), l); |
| 569 | l->append(", "); |
| 570 | LogParam(p.is_null(), l); |
| 571 | l->append(")"); |
| 572 | } |
| 573 | |
[email protected] | 141bcc5 | 2014-01-27 21:36:00 | [diff] [blame] | 574 | void ParamTraits<base::File::Info>::Write(Message* m, |
| 575 | const param_type& p) { |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 576 | WriteParam(m, p.size); |
| 577 | WriteParam(m, p.is_directory); |
| 578 | WriteParam(m, p.last_modified.ToDoubleT()); |
| 579 | WriteParam(m, p.last_accessed.ToDoubleT()); |
| 580 | WriteParam(m, p.creation_time.ToDoubleT()); |
| 581 | } |
| 582 | |
[email protected] | 141bcc5 | 2014-01-27 21:36:00 | [diff] [blame] | 583 | bool ParamTraits<base::File::Info>::Read(const Message* m, |
| 584 | PickleIterator* iter, |
| 585 | param_type* p) { |
[email protected] | 481c3e8 | 2014-07-18 01:40:47 | [diff] [blame] | 586 | double last_modified, last_accessed, creation_time; |
| 587 | if (!ReadParam(m, iter, &p->size) || |
| 588 | !ReadParam(m, iter, &p->is_directory) || |
| 589 | !ReadParam(m, iter, &last_modified) || |
| 590 | !ReadParam(m, iter, &last_accessed) || |
| 591 | !ReadParam(m, iter, &creation_time)) |
| 592 | return false; |
| 593 | p->last_modified = base::Time::FromDoubleT(last_modified); |
| 594 | p->last_accessed = base::Time::FromDoubleT(last_accessed); |
| 595 | p->creation_time = base::Time::FromDoubleT(creation_time); |
| 596 | return true; |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 597 | } |
| 598 | |
[email protected] | 141bcc5 | 2014-01-27 21:36:00 | [diff] [blame] | 599 | void ParamTraits<base::File::Info>::Log(const param_type& p, |
| 600 | std::string* l) { |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 601 | l->append("("); |
| 602 | LogParam(p.size, l); |
| 603 | l->append(","); |
| 604 | LogParam(p.is_directory, l); |
| 605 | l->append(","); |
| 606 | LogParam(p.last_modified.ToDoubleT(), l); |
| 607 | l->append(","); |
| 608 | LogParam(p.last_accessed.ToDoubleT(), l); |
| 609 | l->append(","); |
| 610 | LogParam(p.creation_time.ToDoubleT(), l); |
| 611 | l->append(")"); |
| 612 | } |
| 613 | |
| 614 | void ParamTraits<base::Time>::Write(Message* m, const param_type& p) { |
| 615 | ParamTraits<int64>::Write(m, p.ToInternalValue()); |
| 616 | } |
| 617 | |
| 618 | bool ParamTraits<base::Time>::Read(const Message* m, PickleIterator* iter, |
| 619 | param_type* r) { |
| 620 | int64 value; |
| 621 | if (!ParamTraits<int64>::Read(m, iter, &value)) |
| 622 | return false; |
| 623 | *r = base::Time::FromInternalValue(value); |
| 624 | return true; |
| 625 | } |
| 626 | |
| 627 | void ParamTraits<base::Time>::Log(const param_type& p, std::string* l) { |
| 628 | ParamTraits<int64>::Log(p.ToInternalValue(), l); |
| 629 | } |
| 630 | |
| 631 | void ParamTraits<base::TimeDelta>::Write(Message* m, const param_type& p) { |
| 632 | ParamTraits<int64>::Write(m, p.ToInternalValue()); |
| 633 | } |
| 634 | |
| 635 | bool ParamTraits<base::TimeDelta>::Read(const Message* m, |
| 636 | PickleIterator* iter, |
| 637 | param_type* r) { |
| 638 | int64 value; |
| 639 | bool ret = ParamTraits<int64>::Read(m, iter, &value); |
| 640 | if (ret) |
| 641 | *r = base::TimeDelta::FromInternalValue(value); |
| 642 | |
| 643 | return ret; |
| 644 | } |
| 645 | |
| 646 | void ParamTraits<base::TimeDelta>::Log(const param_type& p, std::string* l) { |
| 647 | ParamTraits<int64>::Log(p.ToInternalValue(), l); |
| 648 | } |
| 649 | |
| 650 | void ParamTraits<base::TimeTicks>::Write(Message* m, const param_type& p) { |
| 651 | ParamTraits<int64>::Write(m, p.ToInternalValue()); |
| 652 | } |
| 653 | |
| 654 | bool ParamTraits<base::TimeTicks>::Read(const Message* m, |
| 655 | PickleIterator* iter, |
| 656 | param_type* r) { |
| 657 | int64 value; |
| 658 | bool ret = ParamTraits<int64>::Read(m, iter, &value); |
| 659 | if (ret) |
| 660 | *r = base::TimeTicks::FromInternalValue(value); |
| 661 | |
| 662 | return ret; |
| 663 | } |
| 664 | |
| 665 | void ParamTraits<base::TimeTicks>::Log(const param_type& p, std::string* l) { |
| 666 | ParamTraits<int64>::Log(p.ToInternalValue(), l); |
| 667 | } |
| 668 | |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 669 | void ParamTraits<IPC::ChannelHandle>::Write(Message* m, const param_type& p) { |
[email protected] | a7c03d4f3 | 2012-01-24 02:36:05 | [diff] [blame] | 670 | #if defined(OS_WIN) |
| 671 | // On Windows marshalling pipe handle is not supported. |
| 672 | DCHECK(p.pipe.handle == NULL); |
| 673 | #endif // defined (OS_WIN) |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 674 | WriteParam(m, p.name); |
| 675 | #if defined(OS_POSIX) |
| 676 | WriteParam(m, p.socket); |
| 677 | #endif |
| 678 | } |
| 679 | |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 680 | bool ParamTraits<IPC::ChannelHandle>::Read(const Message* m, |
| 681 | PickleIterator* iter, |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 682 | param_type* r) { |
| 683 | return ReadParam(m, iter, &r->name) |
| 684 | #if defined(OS_POSIX) |
| 685 | && ReadParam(m, iter, &r->socket) |
| 686 | #endif |
| 687 | ; |
| 688 | } |
| 689 | |
| 690 | void ParamTraits<IPC::ChannelHandle>::Log(const param_type& p, |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 691 | std::string* l) { |
[email protected] | 7d3cbc9 | 2013-03-18 22:33:04 | [diff] [blame] | 692 | l->append(base::StringPrintf("ChannelHandle(%s", p.name.c_str())); |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 693 | #if defined(OS_POSIX) |
[email protected] | 3cd3bce | 2011-09-23 10:32:19 | [diff] [blame] | 694 | l->append(", "); |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 695 | ParamTraits<base::FileDescriptor>::Log(p.socket, l); |
| 696 | #endif |
[email protected] | 252cad6 | 2010-08-18 18:33:57 | [diff] [blame] | 697 | l->append(")"); |
[email protected] | 7a4de7a6 | 2010-08-17 18:38:24 | [diff] [blame] | 698 | } |
| 699 | |
[email protected] | 20f0487a | 2010-09-30 20:06:30 | [diff] [blame] | 700 | void ParamTraits<LogData>::Write(Message* m, const param_type& p) { |
| 701 | WriteParam(m, p.channel); |
| 702 | WriteParam(m, p.routing_id); |
[email protected] | 8bf55ca | 2011-10-17 22:15:27 | [diff] [blame] | 703 | WriteParam(m, p.type); |
[email protected] | 20f0487a | 2010-09-30 20:06:30 | [diff] [blame] | 704 | WriteParam(m, p.flags); |
| 705 | WriteParam(m, p.sent); |
| 706 | WriteParam(m, p.receive); |
| 707 | WriteParam(m, p.dispatch); |
[email protected] | bae578e9 | 2012-11-15 03:17:45 | [diff] [blame] | 708 | WriteParam(m, p.message_name); |
[email protected] | 20f0487a | 2010-09-30 20:06:30 | [diff] [blame] | 709 | WriteParam(m, p.params); |
| 710 | } |
| 711 | |
[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 712 | bool ParamTraits<LogData>::Read(const Message* m, |
| 713 | PickleIterator* iter, |
| 714 | param_type* r) { |
[email protected] | 8bf55ca | 2011-10-17 22:15:27 | [diff] [blame] | 715 | return |
[email protected] | 20f0487a | 2010-09-30 20:06:30 | [diff] [blame] | 716 | ReadParam(m, iter, &r->channel) && |
| 717 | ReadParam(m, iter, &r->routing_id) && |
[email protected] | 8bf55ca | 2011-10-17 22:15:27 | [diff] [blame] | 718 | ReadParam(m, iter, &r->type) && |
[email protected] | 20f0487a | 2010-09-30 20:06:30 | [diff] [blame] | 719 | ReadParam(m, iter, &r->flags) && |
| 720 | ReadParam(m, iter, &r->sent) && |
| 721 | ReadParam(m, iter, &r->receive) && |
| 722 | ReadParam(m, iter, &r->dispatch) && |
[email protected] | bae578e9 | 2012-11-15 03:17:45 | [diff] [blame] | 723 | ReadParam(m, iter, &r->message_name) && |
[email protected] | 20f0487a | 2010-09-30 20:06:30 | [diff] [blame] | 724 | ReadParam(m, iter, &r->params); |
[email protected] | 20f0487a | 2010-09-30 20:06:30 | [diff] [blame] | 725 | } |
| 726 | |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 727 | void ParamTraits<LogData>::Log(const param_type& p, std::string* l) { |
| 728 | // Doesn't make sense to implement this! |
| 729 | } |
| 730 | |
| 731 | void ParamTraits<Message>::Write(Message* m, const Message& p) { |
[email protected] | 34d4861 | 2012-06-29 00:05:04 | [diff] [blame] | 732 | #if defined(OS_POSIX) |
| 733 | // We don't serialize the file descriptors in the nested message, so there |
| 734 | // better not be any. |
morrita | 1aa788c | 2015-01-31 05:45:42 | [diff] [blame^] | 735 | DCHECK(!p.HasAttachments()); |
[email protected] | 34d4861 | 2012-06-29 00:05:04 | [diff] [blame] | 736 | #endif |
| 737 | |
| 738 | // Don't just write out the message. This is used to send messages between |
| 739 | // NaCl (Posix environment) and the browser (could be on Windows). The message |
| 740 | // header formats differ between these systems (so does handle sharing, but |
| 741 | // we already asserted we don't have any handles). So just write out the |
| 742 | // parts of the header we use. |
| 743 | // |
| 744 | // Be careful also to use only explicitly-sized types. The NaCl environment |
| 745 | // could be 64-bit and the host browser could be 32-bits. The nested message |
| 746 | // may or may not be safe to send between 32-bit and 64-bit systems, but we |
| 747 | // leave that up to the code sending the message to ensure. |
| 748 | m->WriteUInt32(static_cast<uint32>(p.routing_id())); |
| 749 | m->WriteUInt32(p.type()); |
| 750 | m->WriteUInt32(p.flags()); |
| 751 | m->WriteData(p.payload(), static_cast<uint32>(p.payload_size())); |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | bool ParamTraits<Message>::Read(const Message* m, PickleIterator* iter, |
| 755 | Message* r) { |
[email protected] | 34d4861 | 2012-06-29 00:05:04 | [diff] [blame] | 756 | uint32 routing_id, type, flags; |
avi | 48fc13b | 2014-12-28 23:31:48 | [diff] [blame] | 757 | if (!iter->ReadUInt32(&routing_id) || |
| 758 | !iter->ReadUInt32(&type) || |
| 759 | !iter->ReadUInt32(&flags)) |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 760 | return false; |
[email protected] | 34d4861 | 2012-06-29 00:05:04 | [diff] [blame] | 761 | |
| 762 | int payload_size; |
| 763 | const char* payload; |
avi | 48fc13b | 2014-12-28 23:31:48 | [diff] [blame] | 764 | if (!iter->ReadData(&payload, &payload_size)) |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 765 | return false; |
[email protected] | 34d4861 | 2012-06-29 00:05:04 | [diff] [blame] | 766 | |
| 767 | r->SetHeaderValues(static_cast<int32>(routing_id), type, flags); |
| 768 | return r->WriteBytes(payload, payload_size); |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 769 | } |
| 770 | |
| 771 | void ParamTraits<Message>::Log(const Message& p, std::string* l) { |
| 772 | l->append("<IPC::Message>"); |
| 773 | } |
| 774 | |
| 775 | #if defined(OS_WIN) |
| 776 | // 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] | 777 | // bit systems. That's why we use the Windows macros to convert to 32 bits. |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 778 | void ParamTraits<HANDLE>::Write(Message* m, const param_type& p) { |
[email protected] | 4a635b7 | 2013-03-04 02:29:03 | [diff] [blame] | 779 | m->WriteInt(HandleToLong(p)); |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | bool ParamTraits<HANDLE>::Read(const Message* m, PickleIterator* iter, |
| 783 | param_type* r) { |
[email protected] | 4a635b7 | 2013-03-04 02:29:03 | [diff] [blame] | 784 | int32 temp; |
avi | 48fc13b | 2014-12-28 23:31:48 | [diff] [blame] | 785 | if (!iter->ReadInt(&temp)) |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 786 | return false; |
[email protected] | 4a635b7 | 2013-03-04 02:29:03 | [diff] [blame] | 787 | *r = LongToHandle(temp); |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 788 | return true; |
| 789 | } |
| 790 | |
| 791 | void ParamTraits<HANDLE>::Log(const param_type& p, std::string* l) { |
[email protected] | 7d3cbc9 | 2013-03-18 22:33:04 | [diff] [blame] | 792 | l->append(base::StringPrintf("0x%X", p)); |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | void ParamTraits<LOGFONT>::Write(Message* m, const param_type& p) { |
| 796 | m->WriteData(reinterpret_cast<const char*>(&p), sizeof(LOGFONT)); |
| 797 | } |
| 798 | |
| 799 | bool ParamTraits<LOGFONT>::Read(const Message* m, PickleIterator* iter, |
| 800 | param_type* r) { |
| 801 | const char *data; |
| 802 | int data_size = 0; |
avi | 48fc13b | 2014-12-28 23:31:48 | [diff] [blame] | 803 | if (iter->ReadData(&data, &data_size) && data_size == sizeof(LOGFONT)) { |
[email protected] | 2e02cfe8 | 2012-11-21 00:58:00 | [diff] [blame] | 804 | const LOGFONT *font = reinterpret_cast<LOGFONT*>(const_cast<char*>(data)); |
| 805 | if (_tcsnlen(font->lfFaceName, LF_FACESIZE) < LF_FACESIZE) { |
| 806 | memcpy(r, data, sizeof(LOGFONT)); |
| 807 | return true; |
| 808 | } |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 809 | } |
| 810 | |
[email protected] | 2e02cfe8 | 2012-11-21 00:58:00 | [diff] [blame] | 811 | NOTREACHED(); |
| 812 | return false; |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 813 | } |
| 814 | |
| 815 | void ParamTraits<LOGFONT>::Log(const param_type& p, std::string* l) { |
[email protected] | 7d3cbc9 | 2013-03-18 22:33:04 | [diff] [blame] | 816 | l->append(base::StringPrintf("<LOGFONT>")); |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 817 | } |
| 818 | |
| 819 | void ParamTraits<MSG>::Write(Message* m, const param_type& p) { |
| 820 | m->WriteData(reinterpret_cast<const char*>(&p), sizeof(MSG)); |
| 821 | } |
| 822 | |
| 823 | bool ParamTraits<MSG>::Read(const Message* m, PickleIterator* iter, |
| 824 | param_type* r) { |
| 825 | const char *data; |
| 826 | int data_size = 0; |
avi | 48fc13b | 2014-12-28 23:31:48 | [diff] [blame] | 827 | bool result = iter->ReadData(&data, &data_size); |
[email protected] | bf5aedf0 | 2012-06-04 21:18:25 | [diff] [blame] | 828 | if (result && data_size == sizeof(MSG)) { |
| 829 | memcpy(r, data, sizeof(MSG)); |
| 830 | } else { |
| 831 | result = false; |
| 832 | NOTREACHED(); |
| 833 | } |
| 834 | |
| 835 | return result; |
| 836 | } |
| 837 | |
| 838 | void ParamTraits<MSG>::Log(const param_type& p, std::string* l) { |
| 839 | l->append("<MSG>"); |
| 840 | } |
| 841 | |
| 842 | #endif // OS_WIN |
| 843 | |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 844 | } // namespace IPC |