| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ipc/ipc_message_utils.h" | 5 #include "ipc/ipc_message_utils.h" |
| 6 | 6 |
| 7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
| 8 #include "base/json/json_writer.h" | 8 #include "base/json/json_writer.h" |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/strings/nullable_string16.h" | 10 #include "base/strings/nullable_string16.h" |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 43 base::StringPrintf("[%02X]", static_cast<unsigned char>(data[i]))); | 43 base::StringPrintf("[%02X]", static_cast<unsigned char>(data[i]))); |
| 44 } | 44 } |
| 45 if (data.size() > kMaxBytesToLog) { | 45 if (data.size() > kMaxBytesToLog) { |
| 46 out->append(base::StringPrintf( | 46 out->append(base::StringPrintf( |
| 47 " and %u more bytes", | 47 " and %u more bytes", |
| 48 static_cast<unsigned>(data.size() - kMaxBytesToLog))); | 48 static_cast<unsigned>(data.size() - kMaxBytesToLog))); |
| 49 } | 49 } |
| 50 #endif | 50 #endif |
| 51 } | 51 } |
| 52 | 52 |
| 53 bool ReadValue(const Message* m, PickleIterator* iter, Value** value, | 53 bool ReadValue(const Message* m, PickleIterator* iter, base::Value** value, |
| 54 int recursion); | 54 int recursion); |
| 55 | 55 |
| 56 void WriteValue(Message* m, const Value* value, int recursion) { | 56 void WriteValue(Message* m, const base::Value* value, int recursion) { |
| 57 bool result; | 57 bool result; |
| 58 if (recursion > kMaxRecursionDepth) { | 58 if (recursion > kMaxRecursionDepth) { |
| 59 LOG(WARNING) << "Max recursion depth hit in WriteValue."; | 59 LOG(WARNING) << "Max recursion depth hit in WriteValue."; |
| 60 return; | 60 return; |
| 61 } | 61 } |
| 62 | 62 |
| 63 m->WriteInt(value->GetType()); | 63 m->WriteInt(value->GetType()); |
| 64 | 64 |
| 65 switch (value->GetType()) { | 65 switch (value->GetType()) { |
| 66 case Value::TYPE_NULL: | 66 case base::Value::TYPE_NULL: |
| 67 break; | 67 break; |
| 68 case Value::TYPE_BOOLEAN: { | 68 case base::Value::TYPE_BOOLEAN: { |
| 69 bool val; | 69 bool val; |
| 70 result = value->GetAsBoolean(&val); | 70 result = value->GetAsBoolean(&val); |
| 71 DCHECK(result); | 71 DCHECK(result); |
| 72 WriteParam(m, val); | 72 WriteParam(m, val); |
| 73 break; | 73 break; |
| 74 } | 74 } |
| 75 case Value::TYPE_INTEGER: { | 75 case base::Value::TYPE_INTEGER: { |
| 76 int val; | 76 int val; |
| 77 result = value->GetAsInteger(&val); | 77 result = value->GetAsInteger(&val); |
| 78 DCHECK(result); | 78 DCHECK(result); |
| 79 WriteParam(m, val); | 79 WriteParam(m, val); |
| 80 break; | 80 break; |
| 81 } | 81 } |
| 82 case Value::TYPE_DOUBLE: { | 82 case base::Value::TYPE_DOUBLE: { |
| 83 double val; | 83 double val; |
| 84 result = value->GetAsDouble(&val); | 84 result = value->GetAsDouble(&val); |
| 85 DCHECK(result); | 85 DCHECK(result); |
| 86 WriteParam(m, val); | 86 WriteParam(m, val); |
| 87 break; | 87 break; |
| 88 } | 88 } |
| 89 case Value::TYPE_STRING: { | 89 case base::Value::TYPE_STRING: { |
| 90 std::string val; | 90 std::string val; |
| 91 result = value->GetAsString(&val); | 91 result = value->GetAsString(&val); |
| 92 DCHECK(result); | 92 DCHECK(result); |
| 93 WriteParam(m, val); | 93 WriteParam(m, val); |
| 94 break; | 94 break; |
| 95 } | 95 } |
| 96 case Value::TYPE_BINARY: { | 96 case base::Value::TYPE_BINARY: { |
| 97 const base::BinaryValue* binary = | 97 const base::BinaryValue* binary = |
| 98 static_cast<const base::BinaryValue*>(value); | 98 static_cast<const base::BinaryValue*>(value); |
| 99 m->WriteData(binary->GetBuffer(), static_cast<int>(binary->GetSize())); | 99 m->WriteData(binary->GetBuffer(), static_cast<int>(binary->GetSize())); |
| 100 break; | 100 break; |
| 101 } | 101 } |
| 102 case Value::TYPE_DICTIONARY: { | 102 case base::Value::TYPE_DICTIONARY: { |
| 103 const DictionaryValue* dict = static_cast<const DictionaryValue*>(value); | 103 const base::DictionaryValue* dict = |
| 104 static_cast<const base::DictionaryValue*>(value); |
| 104 | 105 |
| 105 WriteParam(m, static_cast<int>(dict->size())); | 106 WriteParam(m, static_cast<int>(dict->size())); |
| 106 | 107 |
| 107 for (DictionaryValue::Iterator it(*dict); !it.IsAtEnd(); it.Advance()) { | 108 for (base::DictionaryValue::Iterator it(*dict); !it.IsAtEnd(); |
| 109 it.Advance()) { |
| 108 WriteParam(m, it.key()); | 110 WriteParam(m, it.key()); |
| 109 WriteValue(m, &it.value(), recursion + 1); | 111 WriteValue(m, &it.value(), recursion + 1); |
| 110 } | 112 } |
| 111 break; | 113 break; |
| 112 } | 114 } |
| 113 case Value::TYPE_LIST: { | 115 case base::Value::TYPE_LIST: { |
| 114 const ListValue* list = static_cast<const ListValue*>(value); | 116 const base::ListValue* list = static_cast<const base::ListValue*>(value); |
| 115 WriteParam(m, static_cast<int>(list->GetSize())); | 117 WriteParam(m, static_cast<int>(list->GetSize())); |
| 116 for (ListValue::const_iterator it = list->begin(); it != list->end(); | 118 for (base::ListValue::const_iterator it = list->begin(); |
| 117 ++it) { | 119 it != list->end(); ++it) { |
| 118 WriteValue(m, *it, recursion + 1); | 120 WriteValue(m, *it, recursion + 1); |
| 119 } | 121 } |
| 120 break; | 122 break; |
| 121 } | 123 } |
| 122 } | 124 } |
| 123 } | 125 } |
| 124 | 126 |
| 125 // Helper for ReadValue that reads a DictionaryValue into a pre-allocated | 127 // Helper for ReadValue that reads a DictionaryValue into a pre-allocated |
| 126 // object. | 128 // object. |
| 127 bool ReadDictionaryValue(const Message* m, PickleIterator* iter, | 129 bool ReadDictionaryValue(const Message* m, PickleIterator* iter, |
| 128 DictionaryValue* value, int recursion) { | 130 base::DictionaryValue* value, int recursion) { |
| 129 int size; | 131 int size; |
| 130 if (!ReadParam(m, iter, &size)) | 132 if (!ReadParam(m, iter, &size)) |
| 131 return false; | 133 return false; |
| 132 | 134 |
| 133 for (int i = 0; i < size; ++i) { | 135 for (int i = 0; i < size; ++i) { |
| 134 std::string key; | 136 std::string key; |
| 135 Value* subval; | 137 Value* subval; |
| 136 if (!ReadParam(m, iter, &key) || | 138 if (!ReadParam(m, iter, &key) || |
| 137 !ReadValue(m, iter, &subval, recursion + 1)) | 139 !ReadValue(m, iter, &subval, recursion + 1)) |
| 138 return false; | 140 return false; |
| 139 value->SetWithoutPathExpansion(key, subval); | 141 value->SetWithoutPathExpansion(key, subval); |
| 140 } | 142 } |
| 141 | 143 |
| 142 return true; | 144 return true; |
| 143 } | 145 } |
| 144 | 146 |
| 145 // Helper for ReadValue that reads a ReadListValue into a pre-allocated | 147 // Helper for ReadValue that reads a ReadListValue into a pre-allocated |
| 146 // object. | 148 // object. |
| 147 bool ReadListValue(const Message* m, PickleIterator* iter, | 149 bool ReadListValue(const Message* m, PickleIterator* iter, |
| 148 ListValue* value, int recursion) { | 150 base::ListValue* value, int recursion) { |
| 149 int size; | 151 int size; |
| 150 if (!ReadParam(m, iter, &size)) | 152 if (!ReadParam(m, iter, &size)) |
| 151 return false; | 153 return false; |
| 152 | 154 |
| 153 for (int i = 0; i < size; ++i) { | 155 for (int i = 0; i < size; ++i) { |
| 154 Value* subval; | 156 base::Value* subval; |
| 155 if (!ReadValue(m, iter, &subval, recursion + 1)) | 157 if (!ReadValue(m, iter, &subval, recursion + 1)) |
| 156 return false; | 158 return false; |
| 157 value->Set(i, subval); | 159 value->Set(i, subval); |
| 158 } | 160 } |
| 159 | 161 |
| 160 return true; | 162 return true; |
| 161 } | 163 } |
| 162 | 164 |
| 163 bool ReadValue(const Message* m, PickleIterator* iter, Value** value, | 165 bool ReadValue(const Message* m, PickleIterator* iter, base::Value** value, |
| 164 int recursion) { | 166 int recursion) { |
| 165 if (recursion > kMaxRecursionDepth) { | 167 if (recursion > kMaxRecursionDepth) { |
| 166 LOG(WARNING) << "Max recursion depth hit in ReadValue."; | 168 LOG(WARNING) << "Max recursion depth hit in ReadValue."; |
| 167 return false; | 169 return false; |
| 168 } | 170 } |
| 169 | 171 |
| 170 int type; | 172 int type; |
| 171 if (!ReadParam(m, iter, &type)) | 173 if (!ReadParam(m, iter, &type)) |
| 172 return false; | 174 return false; |
| 173 | 175 |
| 174 switch (type) { | 176 switch (type) { |
| 175 case Value::TYPE_NULL: | 177 case base::Value::TYPE_NULL: |
| 176 *value = Value::CreateNullValue(); | 178 *value = base::Value::CreateNullValue(); |
| 177 break; | 179 break; |
| 178 case Value::TYPE_BOOLEAN: { | 180 case base::Value::TYPE_BOOLEAN: { |
| 179 bool val; | 181 bool val; |
| 180 if (!ReadParam(m, iter, &val)) | 182 if (!ReadParam(m, iter, &val)) |
| 181 return false; | 183 return false; |
| 182 *value = new base::FundamentalValue(val); | 184 *value = new base::FundamentalValue(val); |
| 183 break; | 185 break; |
| 184 } | 186 } |
| 185 case Value::TYPE_INTEGER: { | 187 case base::Value::TYPE_INTEGER: { |
| 186 int val; | 188 int val; |
| 187 if (!ReadParam(m, iter, &val)) | 189 if (!ReadParam(m, iter, &val)) |
| 188 return false; | 190 return false; |
| 189 *value = new base::FundamentalValue(val); | 191 *value = new base::FundamentalValue(val); |
| 190 break; | 192 break; |
| 191 } | 193 } |
| 192 case Value::TYPE_DOUBLE: { | 194 case base::Value::TYPE_DOUBLE: { |
| 193 double val; | 195 double val; |
| 194 if (!ReadParam(m, iter, &val)) | 196 if (!ReadParam(m, iter, &val)) |
| 195 return false; | 197 return false; |
| 196 *value = new base::FundamentalValue(val); | 198 *value = new base::FundamentalValue(val); |
| 197 break; | 199 break; |
| 198 } | 200 } |
| 199 case Value::TYPE_STRING: { | 201 case base::Value::TYPE_STRING: { |
| 200 std::string val; | 202 std::string val; |
| 201 if (!ReadParam(m, iter, &val)) | 203 if (!ReadParam(m, iter, &val)) |
| 202 return false; | 204 return false; |
| 203 *value = new base::StringValue(val); | 205 *value = new base::StringValue(val); |
| 204 break; | 206 break; |
| 205 } | 207 } |
| 206 case Value::TYPE_BINARY: { | 208 case base::Value::TYPE_BINARY: { |
| 207 const char* data; | 209 const char* data; |
| 208 int length; | 210 int length; |
| 209 if (!m->ReadData(iter, &data, &length)) | 211 if (!m->ReadData(iter, &data, &length)) |
| 210 return false; | 212 return false; |
| 211 *value = base::BinaryValue::CreateWithCopiedBuffer(data, length); | 213 *value = base::BinaryValue::CreateWithCopiedBuffer(data, length); |
| 212 break; | 214 break; |
| 213 } | 215 } |
| 214 case Value::TYPE_DICTIONARY: { | 216 case base::Value::TYPE_DICTIONARY: { |
| 215 scoped_ptr<DictionaryValue> val(new DictionaryValue()); | 217 scoped_ptr<base::DictionaryValue> val(new base::DictionaryValue()); |
| 216 if (!ReadDictionaryValue(m, iter, val.get(), recursion)) | 218 if (!ReadDictionaryValue(m, iter, val.get(), recursion)) |
| 217 return false; | 219 return false; |
| 218 *value = val.release(); | 220 *value = val.release(); |
| 219 break; | 221 break; |
| 220 } | 222 } |
| 221 case Value::TYPE_LIST: { | 223 case base::Value::TYPE_LIST: { |
| 222 scoped_ptr<ListValue> val(new ListValue()); | 224 scoped_ptr<base::ListValue> val(new base::ListValue()); |
| 223 if (!ReadListValue(m, iter, val.get(), recursion)) | 225 if (!ReadListValue(m, iter, val.get(), recursion)) |
| 224 return false; | 226 return false; |
| 225 *value = val.release(); | 227 *value = val.release(); |
| 226 break; | 228 break; |
| 227 } | 229 } |
| 228 default: | 230 default: |
| 229 return false; | 231 return false; |
| 230 } | 232 } |
| 231 | 233 |
| 232 return true; | 234 return true; |
| (...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 429 } | 431 } |
| 430 | 432 |
| 431 void ParamTraits<std::vector<bool> >::Log(const param_type& p, std::string* l) { | 433 void ParamTraits<std::vector<bool> >::Log(const param_type& p, std::string* l) { |
| 432 for (size_t i = 0; i < p.size(); ++i) { | 434 for (size_t i = 0; i < p.size(); ++i) { |
| 433 if (i != 0) | 435 if (i != 0) |
| 434 l->push_back(' '); | 436 l->push_back(' '); |
| 435 LogParam(static_cast<bool>(p[i]), l); | 437 LogParam(static_cast<bool>(p[i]), l); |
| 436 } | 438 } |
| 437 } | 439 } |
| 438 | 440 |
| 439 void ParamTraits<DictionaryValue>::Write(Message* m, const param_type& p) { | 441 void ParamTraits<base::DictionaryValue>::Write(Message* m, |
| 442 const param_type& p) { |
| 440 WriteValue(m, &p, 0); | 443 WriteValue(m, &p, 0); |
| 441 } | 444 } |
| 442 | 445 |
| 443 bool ParamTraits<DictionaryValue>::Read( | 446 bool ParamTraits<base::DictionaryValue>::Read( |
| 444 const Message* m, PickleIterator* iter, param_type* r) { | 447 const Message* m, PickleIterator* iter, param_type* r) { |
| 445 int type; | 448 int type; |
| 446 if (!ReadParam(m, iter, &type) || type != Value::TYPE_DICTIONARY) | 449 if (!ReadParam(m, iter, &type) || type != Value::TYPE_DICTIONARY) |
| 447 return false; | 450 return false; |
| 448 | 451 |
| 449 return ReadDictionaryValue(m, iter, r, 0); | 452 return ReadDictionaryValue(m, iter, r, 0); |
| 450 } | 453 } |
| 451 | 454 |
| 452 void ParamTraits<DictionaryValue>::Log(const param_type& p, std::string* l) { | 455 void ParamTraits<base::DictionaryValue>::Log(const param_type& p, |
| 456 std::string* l) { |
| 453 std::string json; | 457 std::string json; |
| 454 base::JSONWriter::Write(&p, &json); | 458 base::JSONWriter::Write(&p, &json); |
| 455 l->append(json); | 459 l->append(json); |
| 456 } | 460 } |
| 457 | 461 |
| 458 #if defined(OS_POSIX) | 462 #if defined(OS_POSIX) |
| 459 void ParamTraits<base::FileDescriptor>::Write(Message* m, const param_type& p) { | 463 void ParamTraits<base::FileDescriptor>::Write(Message* m, const param_type& p) { |
| 460 const bool valid = p.fd >= 0; | 464 const bool valid = p.fd >= 0; |
| 461 WriteParam(m, valid); | 465 WriteParam(m, valid); |
| 462 | 466 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 499 bool ParamTraits<base::FilePath>::Read(const Message* m, | 503 bool ParamTraits<base::FilePath>::Read(const Message* m, |
| 500 PickleIterator* iter, | 504 PickleIterator* iter, |
| 501 param_type* r) { | 505 param_type* r) { |
| 502 return r->ReadFromPickle(iter); | 506 return r->ReadFromPickle(iter); |
| 503 } | 507 } |
| 504 | 508 |
| 505 void ParamTraits<base::FilePath>::Log(const param_type& p, std::string* l) { | 509 void ParamTraits<base::FilePath>::Log(const param_type& p, std::string* l) { |
| 506 ParamTraits<base::FilePath::StringType>::Log(p.value(), l); | 510 ParamTraits<base::FilePath::StringType>::Log(p.value(), l); |
| 507 } | 511 } |
| 508 | 512 |
| 509 void ParamTraits<ListValue>::Write(Message* m, const param_type& p) { | 513 void ParamTraits<base::ListValue>::Write(Message* m, const param_type& p) { |
| 510 WriteValue(m, &p, 0); | 514 WriteValue(m, &p, 0); |
| 511 } | 515 } |
| 512 | 516 |
| 513 bool ParamTraits<ListValue>::Read( | 517 bool ParamTraits<base::ListValue>::Read( |
| 514 const Message* m, PickleIterator* iter, param_type* r) { | 518 const Message* m, PickleIterator* iter, param_type* r) { |
| 515 int type; | 519 int type; |
| 516 if (!ReadParam(m, iter, &type) || type != Value::TYPE_LIST) | 520 if (!ReadParam(m, iter, &type) || type != Value::TYPE_LIST) |
| 517 return false; | 521 return false; |
| 518 | 522 |
| 519 return ReadListValue(m, iter, r, 0); | 523 return ReadListValue(m, iter, r, 0); |
| 520 } | 524 } |
| 521 | 525 |
| 522 void ParamTraits<ListValue>::Log(const param_type& p, std::string* l) { | 526 void ParamTraits<base::ListValue>::Log(const param_type& p, std::string* l) { |
| 523 std::string json; | 527 std::string json; |
| 524 base::JSONWriter::Write(&p, &json); | 528 base::JSONWriter::Write(&p, &json); |
| 525 l->append(json); | 529 l->append(json); |
| 526 } | 530 } |
| 527 | 531 |
| 528 void ParamTraits<base::NullableString16>::Write(Message* m, | 532 void ParamTraits<base::NullableString16>::Write(Message* m, |
| 529 const param_type& p) { | 533 const param_type& p) { |
| 530 WriteParam(m, p.string()); | 534 WriteParam(m, p.string()); |
| 531 WriteParam(m, p.is_null()); | 535 WriteParam(m, p.is_null()); |
| 532 } | 536 } |
| (...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 821 return result; | 825 return result; |
| 822 } | 826 } |
| 823 | 827 |
| 824 void ParamTraits<MSG>::Log(const param_type& p, std::string* l) { | 828 void ParamTraits<MSG>::Log(const param_type& p, std::string* l) { |
| 825 l->append("<MSG>"); | 829 l->append("<MSG>"); |
| 826 } | 830 } |
| 827 | 831 |
| 828 #endif // OS_WIN | 832 #endif // OS_WIN |
| 829 | 833 |
| 830 } // namespace IPC | 834 } // namespace IPC |
| OLD | NEW |