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