Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 if |value| is exactly representable by double or not. | |
|
stevenjb
2012/03/16 17:51:59
nit: s/if/whether/
hashimoto
2012/03/16 18:19:44
Done.
| |
| 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 base::JSONWriter::Write(key.get(), &key_string); | |
| 54 } | |
| 55 // Get the value and set the key-value pair. | |
| 56 Value* value = PopDataAsValue(&entry_reader); | |
| 57 if (!value) | |
| 58 return false; | |
| 59 dictionary_value->Set(key_string, value); | |
| 60 } | |
| 61 return true; | |
| 62 } | |
| 63 | |
| 64 } // namespace | |
| 65 | |
| 66 Value* PopDataAsValue(MessageReader* reader) { | |
| 67 Value* result = NULL; | |
| 68 switch (reader->GetDataType()) { | |
| 69 case Message::INVALID_DATA: | |
| 70 // Do nothing. | |
| 71 break; | |
| 72 case Message::BYTE: { | |
| 73 uint8 value = 0; | |
| 74 if (reader->PopByte(&value)) | |
| 75 result = Value::CreateIntegerValue(value); | |
| 76 break; | |
| 77 } | |
| 78 case Message::BOOL: { | |
| 79 bool value = false; | |
| 80 if (reader->PopBool(&value)) | |
| 81 result = Value::CreateBooleanValue(value); | |
| 82 break; | |
| 83 } | |
| 84 case Message::INT16: { | |
| 85 int16 value = 0; | |
| 86 if (reader->PopInt16(&value)) | |
| 87 result = Value::CreateIntegerValue(value); | |
| 88 break; | |
| 89 } | |
| 90 case Message::UINT16: { | |
| 91 uint16 value = 0; | |
| 92 if (reader->PopUint16(&value)) | |
| 93 result = Value::CreateIntegerValue(value); | |
| 94 break; | |
| 95 } | |
| 96 case Message::INT32: { | |
| 97 int32 value = 0; | |
| 98 if (reader->PopInt32(&value)) | |
| 99 result = Value::CreateIntegerValue(value); | |
| 100 break; | |
| 101 } | |
| 102 case Message::UINT32: { | |
| 103 uint32 value = 0; | |
| 104 if (reader->PopUint32(&value)) | |
| 105 result = Value::CreateDoubleValue(value); | |
| 106 break; | |
| 107 } | |
| 108 case Message::INT64: { | |
| 109 int64 value = 0; | |
| 110 if (reader->PopInt64(&value)) { | |
| 111 DLOG_IF(WARNING, !IsExactlyRepresentableByDouble(value)) << | |
| 112 "|value| is not exactly representable by double"; | |
|
stevenjb
2012/03/16 17:51:59
Print key/value here for debugging if this ever co
hashimoto
2012/03/16 18:19:44
Done.
| |
| 113 result = Value::CreateDoubleValue(value); | |
| 114 } | |
| 115 break; | |
| 116 } | |
| 117 case Message::UINT64: { | |
| 118 uint64 value = 0; | |
| 119 if (reader->PopUint64(&value)) { | |
| 120 DLOG_IF(WARNING, !IsExactlyRepresentableByDouble(value)) << | |
| 121 "|value| is not exactly representable by double"; | |
|
stevenjb
2012/03/16 17:51:59
Same.
hashimoto
2012/03/16 18:19:44
Done.
| |
| 122 result = Value::CreateDoubleValue(value); | |
| 123 } | |
| 124 break; | |
| 125 } | |
| 126 case Message::DOUBLE: { | |
| 127 double value = 0; | |
| 128 if (reader->PopDouble(&value)) | |
| 129 result = Value::CreateDoubleValue(value); | |
| 130 break; | |
| 131 } | |
| 132 case Message::STRING: { | |
| 133 std::string value; | |
| 134 if (reader->PopString(&value)) | |
| 135 result = Value::CreateStringValue(value); | |
| 136 break; | |
| 137 } | |
| 138 case Message::OBJECT_PATH: { | |
| 139 ObjectPath value; | |
| 140 if (reader->PopObjectPath(&value)) | |
| 141 result = Value::CreateStringValue(value.value()); | |
| 142 break; | |
| 143 } | |
| 144 case Message::ARRAY: { | |
| 145 MessageReader sub_reader(NULL); | |
| 146 if (reader->PopArray(&sub_reader)) { | |
| 147 // If the type of the array's element is DICT_ENTRY, create a | |
| 148 // DictionaryValue, otherwise create a ListValue. | |
| 149 if (sub_reader.GetDataType() == Message::DICT_ENTRY) { | |
| 150 scoped_ptr<DictionaryValue> dictionary_value(new DictionaryValue); | |
| 151 if (PopDictionaryEntries(&sub_reader, dictionary_value.get())) | |
| 152 result = dictionary_value.release(); | |
| 153 } else { | |
| 154 scoped_ptr<ListValue> list_value(new ListValue); | |
| 155 if (PopListElements(&sub_reader, list_value.get())) | |
| 156 result = list_value.release(); | |
| 157 } | |
| 158 } | |
| 159 break; | |
| 160 } | |
| 161 case Message::STRUCT: { | |
| 162 MessageReader sub_reader(NULL); | |
| 163 if (reader->PopStruct(&sub_reader)) { | |
| 164 scoped_ptr<ListValue> list_value(new ListValue); | |
| 165 if (PopListElements(&sub_reader, list_value.get())) | |
| 166 result = list_value.release(); | |
| 167 } | |
| 168 break; | |
| 169 } | |
| 170 case Message::DICT_ENTRY: | |
| 171 // DICT_ENTRY must be popped as an element of an array. | |
| 172 NOTREACHED(); | |
| 173 break; | |
| 174 case Message::VARIANT: { | |
| 175 MessageReader sub_reader(NULL); | |
| 176 if (reader->PopVariant(&sub_reader)) | |
| 177 result = PopDataAsValue(&sub_reader); | |
| 178 break; | |
| 179 } | |
| 180 } | |
| 181 return result; | |
| 182 } | |
| 183 | |
| 184 } // namespace dbus | |
| OLD | NEW |