[email protected] | a5aae12 | 2012-03-16 23:08:42 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "dbus/values_util.h" |
| 6 | |
| 7 | #include "base/json/json_writer.h" |
| 8 | #include "base/logging.h" |
| 9 | #include "base/memory/scoped_ptr.h" |
| 10 | #include "base/values.h" |
| 11 | #include "dbus/message.h" |
| 12 | |
| 13 | namespace dbus { |
| 14 | |
| 15 | namespace { |
| 16 | |
| 17 | // Returns whether |value| is exactly representable by double or not. |
| 18 | template<typename T> |
| 19 | bool IsExactlyRepresentableByDouble(T value) { |
| 20 | return value == static_cast<T>(static_cast<double>(value)); |
| 21 | } |
| 22 | |
| 23 | // Pops values from |reader| and appends them to |list_value|. |
| 24 | bool PopListElements(MessageReader* reader, ListValue* list_value) { |
| 25 | while (reader->HasMoreData()) { |
| 26 | Value* element_value = PopDataAsValue(reader); |
| 27 | if (!element_value) |
| 28 | return false; |
| 29 | list_value->Append(element_value); |
| 30 | } |
| 31 | return true; |
| 32 | } |
| 33 | |
| 34 | // Pops dict-entries from |reader| and sets them to |dictionary_value| |
| 35 | bool PopDictionaryEntries(MessageReader* reader, |
| 36 | DictionaryValue* dictionary_value) { |
| 37 | while (reader->HasMoreData()) { |
| 38 | DCHECK_EQ(Message::DICT_ENTRY, reader->GetDataType()); |
| 39 | MessageReader entry_reader(NULL); |
| 40 | if (!reader->PopDictEntry(&entry_reader)) |
| 41 | return false; |
| 42 | // Get key as a string. |
| 43 | std::string key_string; |
| 44 | if (entry_reader.GetDataType() == Message::STRING) { |
| 45 | // If the type of keys is STRING, pop it directly. |
| 46 | if (!entry_reader.PopString(&key_string)) |
| 47 | return false; |
| 48 | } else { |
| 49 | // If the type of keys is not STRING, convert it to string. |
| 50 | scoped_ptr<Value> key(PopDataAsValue(&entry_reader)); |
| 51 | if (!key.get()) |
| 52 | return false; |
| 53 | // Use JSONWriter to convert an arbitrary value to a string. |
| 54 | base::JSONWriter::Write(key.get(), &key_string); |
| 55 | } |
| 56 | // Get the value and set the key-value pair. |
| 57 | Value* value = PopDataAsValue(&entry_reader); |
| 58 | if (!value) |
| 59 | return false; |
[email protected] | 0f0117b24 | 2012-03-20 18:18:57 | [diff] [blame] | 60 | dictionary_value->SetWithoutPathExpansion(key_string, value); |
[email protected] | a5aae12 | 2012-03-16 23:08:42 | [diff] [blame] | 61 | } |
| 62 | return true; |
| 63 | } |
| 64 | |
[email protected] | 6e04d17 | 2012-03-24 20:37:18 | [diff] [blame] | 65 | // Gets the D-Bus type signature for the value. |
| 66 | std::string GetTypeSignature(const base::Value& value) { |
| 67 | switch (value.GetType()) { |
| 68 | case base::Value::TYPE_BOOLEAN: |
| 69 | return "b"; |
| 70 | case base::Value::TYPE_INTEGER: |
| 71 | return "i"; |
| 72 | case base::Value::TYPE_DOUBLE: |
| 73 | return "d"; |
| 74 | case base::Value::TYPE_STRING: |
| 75 | return "s"; |
| 76 | case base::Value::TYPE_BINARY: |
| 77 | return "ay"; |
| 78 | case base::Value::TYPE_DICTIONARY: |
| 79 | return "a{sv}"; |
| 80 | default: |
| 81 | DLOG(ERROR) << "Unexpected type " << value.GetType(); |
| 82 | return ""; |
| 83 | } |
| 84 | } |
| 85 | |
[email protected] | a5aae12 | 2012-03-16 23:08:42 | [diff] [blame] | 86 | } // namespace |
| 87 | |
| 88 | Value* PopDataAsValue(MessageReader* reader) { |
| 89 | Value* result = NULL; |
| 90 | switch (reader->GetDataType()) { |
| 91 | case Message::INVALID_DATA: |
| 92 | // Do nothing. |
| 93 | break; |
| 94 | case Message::BYTE: { |
| 95 | uint8 value = 0; |
| 96 | if (reader->PopByte(&value)) |
| 97 | result = Value::CreateIntegerValue(value); |
| 98 | break; |
| 99 | } |
| 100 | case Message::BOOL: { |
| 101 | bool value = false; |
| 102 | if (reader->PopBool(&value)) |
| 103 | result = Value::CreateBooleanValue(value); |
| 104 | break; |
| 105 | } |
| 106 | case Message::INT16: { |
| 107 | int16 value = 0; |
| 108 | if (reader->PopInt16(&value)) |
| 109 | result = Value::CreateIntegerValue(value); |
| 110 | break; |
| 111 | } |
| 112 | case Message::UINT16: { |
| 113 | uint16 value = 0; |
| 114 | if (reader->PopUint16(&value)) |
| 115 | result = Value::CreateIntegerValue(value); |
| 116 | break; |
| 117 | } |
| 118 | case Message::INT32: { |
| 119 | int32 value = 0; |
| 120 | if (reader->PopInt32(&value)) |
| 121 | result = Value::CreateIntegerValue(value); |
| 122 | break; |
| 123 | } |
| 124 | case Message::UINT32: { |
| 125 | uint32 value = 0; |
| 126 | if (reader->PopUint32(&value)) |
| 127 | result = Value::CreateDoubleValue(value); |
| 128 | break; |
| 129 | } |
| 130 | case Message::INT64: { |
| 131 | int64 value = 0; |
| 132 | if (reader->PopInt64(&value)) { |
| 133 | DLOG_IF(WARNING, !IsExactlyRepresentableByDouble(value)) << |
| 134 | value << " is not exactly representable by double"; |
| 135 | result = Value::CreateDoubleValue(value); |
| 136 | } |
| 137 | break; |
| 138 | } |
| 139 | case Message::UINT64: { |
| 140 | uint64 value = 0; |
| 141 | if (reader->PopUint64(&value)) { |
| 142 | DLOG_IF(WARNING, !IsExactlyRepresentableByDouble(value)) << |
| 143 | value << " is not exactly representable by double"; |
| 144 | result = Value::CreateDoubleValue(value); |
| 145 | } |
| 146 | break; |
| 147 | } |
| 148 | case Message::DOUBLE: { |
| 149 | double value = 0; |
| 150 | if (reader->PopDouble(&value)) |
| 151 | result = Value::CreateDoubleValue(value); |
| 152 | break; |
| 153 | } |
| 154 | case Message::STRING: { |
| 155 | std::string value; |
| 156 | if (reader->PopString(&value)) |
| 157 | result = Value::CreateStringValue(value); |
| 158 | break; |
| 159 | } |
| 160 | case Message::OBJECT_PATH: { |
| 161 | ObjectPath value; |
| 162 | if (reader->PopObjectPath(&value)) |
| 163 | result = Value::CreateStringValue(value.value()); |
| 164 | break; |
| 165 | } |
[email protected] | e146bfc | 2012-03-30 06:46:20 | [diff] [blame] | 166 | case Message::UNIX_FD: { |
| 167 | // Cannot distinguish a file descriptor from an int |
| 168 | NOTREACHED(); |
| 169 | break; |
| 170 | } |
[email protected] | a5aae12 | 2012-03-16 23:08:42 | [diff] [blame] | 171 | case Message::ARRAY: { |
| 172 | MessageReader sub_reader(NULL); |
| 173 | if (reader->PopArray(&sub_reader)) { |
| 174 | // If the type of the array's element is DICT_ENTRY, create a |
| 175 | // DictionaryValue, otherwise create a ListValue. |
| 176 | if (sub_reader.GetDataType() == Message::DICT_ENTRY) { |
| 177 | scoped_ptr<DictionaryValue> dictionary_value(new DictionaryValue); |
| 178 | if (PopDictionaryEntries(&sub_reader, dictionary_value.get())) |
| 179 | result = dictionary_value.release(); |
| 180 | } else { |
| 181 | scoped_ptr<ListValue> list_value(new ListValue); |
| 182 | if (PopListElements(&sub_reader, list_value.get())) |
| 183 | result = list_value.release(); |
| 184 | } |
| 185 | } |
| 186 | break; |
| 187 | } |
| 188 | case Message::STRUCT: { |
| 189 | MessageReader sub_reader(NULL); |
| 190 | if (reader->PopStruct(&sub_reader)) { |
| 191 | scoped_ptr<ListValue> list_value(new ListValue); |
| 192 | if (PopListElements(&sub_reader, list_value.get())) |
| 193 | result = list_value.release(); |
| 194 | } |
| 195 | break; |
| 196 | } |
| 197 | case Message::DICT_ENTRY: |
| 198 | // DICT_ENTRY must be popped as an element of an array. |
| 199 | NOTREACHED(); |
| 200 | break; |
| 201 | case Message::VARIANT: { |
| 202 | MessageReader sub_reader(NULL); |
| 203 | if (reader->PopVariant(&sub_reader)) |
| 204 | result = PopDataAsValue(&sub_reader); |
| 205 | break; |
| 206 | } |
| 207 | } |
| 208 | return result; |
| 209 | } |
| 210 | |
[email protected] | 6e04d17 | 2012-03-24 20:37:18 | [diff] [blame] | 211 | void AppendBasicTypeValueData(MessageWriter* writer, const base::Value& value) { |
| 212 | switch (value.GetType()) { |
| 213 | case base::Value::TYPE_BOOLEAN: { |
| 214 | bool bool_value = false; |
[email protected] | 3d5f3c1 | 2012-03-28 19:00:06 | [diff] [blame] | 215 | bool success = value.GetAsBoolean(&bool_value); |
| 216 | DCHECK(success); |
[email protected] | 6e04d17 | 2012-03-24 20:37:18 | [diff] [blame] | 217 | writer->AppendBool(bool_value); |
| 218 | break; |
| 219 | } |
| 220 | case base::Value::TYPE_INTEGER: { |
| 221 | int int_value = 0; |
[email protected] | 3d5f3c1 | 2012-03-28 19:00:06 | [diff] [blame] | 222 | bool success = value.GetAsInteger(&int_value); |
| 223 | DCHECK(success); |
[email protected] | 6e04d17 | 2012-03-24 20:37:18 | [diff] [blame] | 224 | writer->AppendInt32(int_value); |
| 225 | break; |
| 226 | } |
| 227 | case base::Value::TYPE_DOUBLE: { |
| 228 | double double_value = 0; |
[email protected] | 3d5f3c1 | 2012-03-28 19:00:06 | [diff] [blame] | 229 | bool success = value.GetAsDouble(&double_value); |
| 230 | DCHECK(success); |
[email protected] | 6e04d17 | 2012-03-24 20:37:18 | [diff] [blame] | 231 | writer->AppendDouble(double_value); |
| 232 | break; |
| 233 | } |
| 234 | case base::Value::TYPE_STRING: { |
| 235 | std::string string_value; |
[email protected] | 3d5f3c1 | 2012-03-28 19:00:06 | [diff] [blame] | 236 | bool success = value.GetAsString(&string_value); |
| 237 | DCHECK(success); |
[email protected] | 6e04d17 | 2012-03-24 20:37:18 | [diff] [blame] | 238 | writer->AppendString(string_value); |
| 239 | break; |
| 240 | } |
| 241 | default: |
| 242 | DLOG(ERROR) << "Unexpected type " << value.GetType(); |
| 243 | break; |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | void AppendBasicTypeValueDataAsVariant(MessageWriter* writer, |
| 248 | const base::Value& value) { |
| 249 | MessageWriter sub_writer(NULL); |
| 250 | writer->OpenVariant(GetTypeSignature(value), &sub_writer); |
| 251 | AppendBasicTypeValueData(&sub_writer, value); |
| 252 | writer->CloseContainer(&sub_writer); |
| 253 | } |
| 254 | |
[email protected] | a5aae12 | 2012-03-16 23:08:42 | [diff] [blame] | 255 | } // namespace dbus |