| 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 "dbus/values_util.h" | 5 #include "dbus/values_util.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/json/json_writer.h" | 9 #include "base/json/json_writer.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/memory/ptr_util.h" |
| 11 #include "base/values.h" | 12 #include "base/values.h" |
| 12 #include "dbus/message.h" | 13 #include "dbus/message.h" |
| 13 | 14 |
| 14 namespace dbus { | 15 namespace dbus { |
| 15 | 16 |
| 16 namespace { | 17 namespace { |
| 17 | 18 |
| 18 // Returns whether |value| is exactly representable by double or not. | 19 // Returns whether |value| is exactly representable by double or not. |
| 19 template<typename T> | 20 template<typename T> |
| 20 bool IsExactlyRepresentableByDouble(T value) { | 21 bool IsExactlyRepresentableByDouble(T value) { |
| 21 return value == static_cast<T>(static_cast<double>(value)); | 22 return value == static_cast<T>(static_cast<double>(value)); |
| 22 } | 23 } |
| 23 | 24 |
| 24 // Pops values from |reader| and appends them to |list_value|. | 25 // Pops values from |reader| and appends them to |list_value|. |
| 25 bool PopListElements(MessageReader* reader, base::ListValue* list_value) { | 26 bool PopListElements(MessageReader* reader, base::ListValue* list_value) { |
| 26 while (reader->HasMoreData()) { | 27 while (reader->HasMoreData()) { |
| 27 base::Value* element_value = PopDataAsValue(reader); | 28 std::unique_ptr<base::Value> element_value = PopDataAsValue(reader); |
| 28 if (!element_value) | 29 if (!element_value) |
| 29 return false; | 30 return false; |
| 30 list_value->Append(element_value); | 31 list_value->Append(std::move(element_value)); |
| 31 } | 32 } |
| 32 return true; | 33 return true; |
| 33 } | 34 } |
| 34 | 35 |
| 35 // Pops dict-entries from |reader| and sets them to |dictionary_value| | 36 // Pops dict-entries from |reader| and sets them to |dictionary_value| |
| 36 bool PopDictionaryEntries(MessageReader* reader, | 37 bool PopDictionaryEntries(MessageReader* reader, |
| 37 base::DictionaryValue* dictionary_value) { | 38 base::DictionaryValue* dictionary_value) { |
| 38 while (reader->HasMoreData()) { | 39 while (reader->HasMoreData()) { |
| 39 DCHECK_EQ(Message::DICT_ENTRY, reader->GetDataType()); | 40 DCHECK_EQ(Message::DICT_ENTRY, reader->GetDataType()); |
| 40 MessageReader entry_reader(NULL); | 41 MessageReader entry_reader(NULL); |
| 41 if (!reader->PopDictEntry(&entry_reader)) | 42 if (!reader->PopDictEntry(&entry_reader)) |
| 42 return false; | 43 return false; |
| 43 // Get key as a string. | 44 // Get key as a string. |
| 44 std::string key_string; | 45 std::string key_string; |
| 45 if (entry_reader.GetDataType() == Message::STRING) { | 46 if (entry_reader.GetDataType() == Message::STRING) { |
| 46 // If the type of keys is STRING, pop it directly. | 47 // If the type of keys is STRING, pop it directly. |
| 47 if (!entry_reader.PopString(&key_string)) | 48 if (!entry_reader.PopString(&key_string)) |
| 48 return false; | 49 return false; |
| 49 } else { | 50 } else { |
| 50 // If the type of keys is not STRING, convert it to string. | 51 // If the type of keys is not STRING, convert it to string. |
| 51 std::unique_ptr<base::Value> key(PopDataAsValue(&entry_reader)); | 52 std::unique_ptr<base::Value> key(PopDataAsValue(&entry_reader)); |
| 52 if (!key) | 53 if (!key) |
| 53 return false; | 54 return false; |
| 54 // Use JSONWriter to convert an arbitrary value to a string. | 55 // Use JSONWriter to convert an arbitrary value to a string. |
| 55 base::JSONWriter::Write(*key, &key_string); | 56 base::JSONWriter::Write(*key, &key_string); |
| 56 } | 57 } |
| 57 // Get the value and set the key-value pair. | 58 // Get the value and set the key-value pair. |
| 58 base::Value* value = PopDataAsValue(&entry_reader); | 59 std::unique_ptr<base::Value> value = PopDataAsValue(&entry_reader); |
| 59 if (!value) | 60 if (!value) |
| 60 return false; | 61 return false; |
| 61 dictionary_value->SetWithoutPathExpansion(key_string, value); | 62 dictionary_value->SetWithoutPathExpansion(key_string, std::move(value)); |
| 62 } | 63 } |
| 63 return true; | 64 return true; |
| 64 } | 65 } |
| 65 | 66 |
| 66 // Gets the D-Bus type signature for the value. | 67 // Gets the D-Bus type signature for the value. |
| 67 std::string GetTypeSignature(const base::Value& value) { | 68 std::string GetTypeSignature(const base::Value& value) { |
| 68 switch (value.GetType()) { | 69 switch (value.GetType()) { |
| 69 case base::Value::TYPE_BOOLEAN: | 70 case base::Value::TYPE_BOOLEAN: |
| 70 return "b"; | 71 return "b"; |
| 71 case base::Value::TYPE_INTEGER: | 72 case base::Value::TYPE_INTEGER: |
| 72 return "i"; | 73 return "i"; |
| 73 case base::Value::TYPE_DOUBLE: | 74 case base::Value::TYPE_DOUBLE: |
| 74 return "d"; | 75 return "d"; |
| 75 case base::Value::TYPE_STRING: | 76 case base::Value::TYPE_STRING: |
| 76 return "s"; | 77 return "s"; |
| 77 case base::Value::TYPE_BINARY: | 78 case base::Value::TYPE_BINARY: |
| 78 return "ay"; | 79 return "ay"; |
| 79 case base::Value::TYPE_DICTIONARY: | 80 case base::Value::TYPE_DICTIONARY: |
| 80 return "a{sv}"; | 81 return "a{sv}"; |
| 81 case base::Value::TYPE_LIST: | 82 case base::Value::TYPE_LIST: |
| 82 return "av"; | 83 return "av"; |
| 83 default: | 84 default: |
| 84 DLOG(ERROR) << "Unexpected type " << value.GetType(); | 85 DLOG(ERROR) << "Unexpected type " << value.GetType(); |
| 85 return std::string(); | 86 return std::string(); |
| 86 } | 87 } |
| 87 } | 88 } |
| 88 | 89 |
| 89 } // namespace | 90 } // namespace |
| 90 | 91 |
| 91 base::Value* PopDataAsValue(MessageReader* reader) { | 92 std::unique_ptr<base::Value> PopDataAsValue(MessageReader* reader) { |
| 92 base::Value* result = NULL; | 93 std::unique_ptr<base::Value> result; |
| 93 switch (reader->GetDataType()) { | 94 switch (reader->GetDataType()) { |
| 94 case Message::INVALID_DATA: | 95 case Message::INVALID_DATA: |
| 95 // Do nothing. | 96 // Do nothing. |
| 96 break; | 97 break; |
| 97 case Message::BYTE: { | 98 case Message::BYTE: { |
| 98 uint8_t value = 0; | 99 uint8_t value = 0; |
| 99 if (reader->PopByte(&value)) | 100 if (reader->PopByte(&value)) |
| 100 result = new base::FundamentalValue(value); | 101 result = base::MakeUnique<base::FundamentalValue>(value); |
| 101 break; | 102 break; |
| 102 } | 103 } |
| 103 case Message::BOOL: { | 104 case Message::BOOL: { |
| 104 bool value = false; | 105 bool value = false; |
| 105 if (reader->PopBool(&value)) | 106 if (reader->PopBool(&value)) |
| 106 result = new base::FundamentalValue(value); | 107 result = base::MakeUnique<base::FundamentalValue>(value); |
| 107 break; | 108 break; |
| 108 } | 109 } |
| 109 case Message::INT16: { | 110 case Message::INT16: { |
| 110 int16_t value = 0; | 111 int16_t value = 0; |
| 111 if (reader->PopInt16(&value)) | 112 if (reader->PopInt16(&value)) |
| 112 result = new base::FundamentalValue(value); | 113 result = base::MakeUnique<base::FundamentalValue>(value); |
| 113 break; | 114 break; |
| 114 } | 115 } |
| 115 case Message::UINT16: { | 116 case Message::UINT16: { |
| 116 uint16_t value = 0; | 117 uint16_t value = 0; |
| 117 if (reader->PopUint16(&value)) | 118 if (reader->PopUint16(&value)) |
| 118 result = new base::FundamentalValue(value); | 119 result = base::MakeUnique<base::FundamentalValue>(value); |
| 119 break; | 120 break; |
| 120 } | 121 } |
| 121 case Message::INT32: { | 122 case Message::INT32: { |
| 122 int32_t value = 0; | 123 int32_t value = 0; |
| 123 if (reader->PopInt32(&value)) | 124 if (reader->PopInt32(&value)) |
| 124 result = new base::FundamentalValue(value); | 125 result = base::MakeUnique<base::FundamentalValue>(value); |
| 125 break; | 126 break; |
| 126 } | 127 } |
| 127 case Message::UINT32: { | 128 case Message::UINT32: { |
| 128 uint32_t value = 0; | 129 uint32_t value = 0; |
| 129 if (reader->PopUint32(&value)) | 130 if (reader->PopUint32(&value)) { |
| 130 result = new base::FundamentalValue(static_cast<double>(value)); | 131 result = base::MakeUnique<base::FundamentalValue>( |
| 132 static_cast<double>(value)); |
| 133 } |
| 131 break; | 134 break; |
| 132 } | 135 } |
| 133 case Message::INT64: { | 136 case Message::INT64: { |
| 134 int64_t value = 0; | 137 int64_t value = 0; |
| 135 if (reader->PopInt64(&value)) { | 138 if (reader->PopInt64(&value)) { |
| 136 DLOG_IF(WARNING, !IsExactlyRepresentableByDouble(value)) << | 139 DLOG_IF(WARNING, !IsExactlyRepresentableByDouble(value)) << |
| 137 value << " is not exactly representable by double"; | 140 value << " is not exactly representable by double"; |
| 138 result = new base::FundamentalValue(static_cast<double>(value)); | 141 result = base::MakeUnique<base::FundamentalValue>( |
| 142 static_cast<double>(value)); |
| 139 } | 143 } |
| 140 break; | 144 break; |
| 141 } | 145 } |
| 142 case Message::UINT64: { | 146 case Message::UINT64: { |
| 143 uint64_t value = 0; | 147 uint64_t value = 0; |
| 144 if (reader->PopUint64(&value)) { | 148 if (reader->PopUint64(&value)) { |
| 145 DLOG_IF(WARNING, !IsExactlyRepresentableByDouble(value)) << | 149 DLOG_IF(WARNING, !IsExactlyRepresentableByDouble(value)) << |
| 146 value << " is not exactly representable by double"; | 150 value << " is not exactly representable by double"; |
| 147 result = new base::FundamentalValue(static_cast<double>(value)); | 151 result = base::MakeUnique<base::FundamentalValue>( |
| 152 static_cast<double>(value)); |
| 148 } | 153 } |
| 149 break; | 154 break; |
| 150 } | 155 } |
| 151 case Message::DOUBLE: { | 156 case Message::DOUBLE: { |
| 152 double value = 0; | 157 double value = 0; |
| 153 if (reader->PopDouble(&value)) | 158 if (reader->PopDouble(&value)) |
| 154 result = new base::FundamentalValue(value); | 159 result = base::MakeUnique<base::FundamentalValue>(value); |
| 155 break; | 160 break; |
| 156 } | 161 } |
| 157 case Message::STRING: { | 162 case Message::STRING: { |
| 158 std::string value; | 163 std::string value; |
| 159 if (reader->PopString(&value)) | 164 if (reader->PopString(&value)) |
| 160 result = new base::StringValue(value); | 165 result = base::MakeUnique<base::StringValue>(value); |
| 161 break; | 166 break; |
| 162 } | 167 } |
| 163 case Message::OBJECT_PATH: { | 168 case Message::OBJECT_PATH: { |
| 164 ObjectPath value; | 169 ObjectPath value; |
| 165 if (reader->PopObjectPath(&value)) | 170 if (reader->PopObjectPath(&value)) |
| 166 result = new base::StringValue(value.value()); | 171 result = base::MakeUnique<base::StringValue>(value.value()); |
| 167 break; | 172 break; |
| 168 } | 173 } |
| 169 case Message::UNIX_FD: { | 174 case Message::UNIX_FD: { |
| 170 // Cannot distinguish a file descriptor from an int | 175 // Cannot distinguish a file descriptor from an int |
| 171 NOTREACHED(); | 176 NOTREACHED(); |
| 172 break; | 177 break; |
| 173 } | 178 } |
| 174 case Message::ARRAY: { | 179 case Message::ARRAY: { |
| 175 MessageReader sub_reader(NULL); | 180 MessageReader sub_reader(NULL); |
| 176 if (reader->PopArray(&sub_reader)) { | 181 if (reader->PopArray(&sub_reader)) { |
| 177 // If the type of the array's element is DICT_ENTRY, create a | 182 // If the type of the array's element is DICT_ENTRY, create a |
| 178 // DictionaryValue, otherwise create a ListValue. | 183 // DictionaryValue, otherwise create a ListValue. |
| 179 if (sub_reader.GetDataType() == Message::DICT_ENTRY) { | 184 if (sub_reader.GetDataType() == Message::DICT_ENTRY) { |
| 180 std::unique_ptr<base::DictionaryValue> dictionary_value( | 185 std::unique_ptr<base::DictionaryValue> dictionary_value( |
| 181 new base::DictionaryValue); | 186 new base::DictionaryValue); |
| 182 if (PopDictionaryEntries(&sub_reader, dictionary_value.get())) | 187 if (PopDictionaryEntries(&sub_reader, dictionary_value.get())) |
| 183 result = dictionary_value.release(); | 188 result = std::move(dictionary_value); |
| 184 } else { | 189 } else { |
| 185 std::unique_ptr<base::ListValue> list_value(new base::ListValue); | 190 std::unique_ptr<base::ListValue> list_value(new base::ListValue); |
| 186 if (PopListElements(&sub_reader, list_value.get())) | 191 if (PopListElements(&sub_reader, list_value.get())) |
| 187 result = list_value.release(); | 192 result = std::move(list_value); |
| 188 } | 193 } |
| 189 } | 194 } |
| 190 break; | 195 break; |
| 191 } | 196 } |
| 192 case Message::STRUCT: { | 197 case Message::STRUCT: { |
| 193 MessageReader sub_reader(NULL); | 198 MessageReader sub_reader(NULL); |
| 194 if (reader->PopStruct(&sub_reader)) { | 199 if (reader->PopStruct(&sub_reader)) { |
| 195 std::unique_ptr<base::ListValue> list_value(new base::ListValue); | 200 std::unique_ptr<base::ListValue> list_value(new base::ListValue); |
| 196 if (PopListElements(&sub_reader, list_value.get())) | 201 if (PopListElements(&sub_reader, list_value.get())) |
| 197 result = list_value.release(); | 202 result = std::move(list_value); |
| 198 } | 203 } |
| 199 break; | 204 break; |
| 200 } | 205 } |
| 201 case Message::DICT_ENTRY: | 206 case Message::DICT_ENTRY: |
| 202 // DICT_ENTRY must be popped as an element of an array. | 207 // DICT_ENTRY must be popped as an element of an array. |
| 203 NOTREACHED(); | 208 NOTREACHED(); |
| 204 break; | 209 break; |
| 205 case Message::VARIANT: { | 210 case Message::VARIANT: { |
| 206 MessageReader sub_reader(NULL); | 211 MessageReader sub_reader(NULL); |
| 207 if (reader->PopVariant(&sub_reader)) | 212 if (reader->PopVariant(&sub_reader)) |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 297 } | 302 } |
| 298 | 303 |
| 299 void AppendValueDataAsVariant(MessageWriter* writer, const base::Value& value) { | 304 void AppendValueDataAsVariant(MessageWriter* writer, const base::Value& value) { |
| 300 MessageWriter variant_writer(NULL); | 305 MessageWriter variant_writer(NULL); |
| 301 writer->OpenVariant(GetTypeSignature(value), &variant_writer); | 306 writer->OpenVariant(GetTypeSignature(value), &variant_writer); |
| 302 AppendValueData(&variant_writer, value); | 307 AppendValueData(&variant_writer, value); |
| 303 writer->CloseContainer(&variant_writer); | 308 writer->CloseContainer(&variant_writer); |
| 304 } | 309 } |
| 305 | 310 |
| 306 } // namespace dbus | 311 } // namespace dbus |
| OLD | NEW |