Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 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 "ash/display/json_converter.h" | |
| 6 | |
| 7 #include <map> | |
| 8 #include <string> | |
| 9 #include <unordered_map> | |
| 10 | |
| 11 #include "ash/display/display_layout.h" | |
| 12 #include "ash/display/display_pref_util.h" | |
| 13 #include "base/memory/scoped_vector.h" | |
| 14 #include "base/strings/string_number_conversions.h" | |
| 15 #include "base/values.h" | |
| 16 | |
| 17 namespace ash { | |
| 18 | |
| 19 namespace { | |
| 20 | |
| 21 // Persistent key names | |
| 22 const char kMirroredKey[] = "mirrored"; | |
| 23 const char kDefaultUnifiedKey[] = "default_unified"; | |
| 24 const char kPrimaryIdKey[] = "primary-id"; | |
| 25 const char kDisplayPlacementKey[] = "display_placement"; | |
| 26 | |
| 27 // DisplayPlacement key names | |
| 28 const char kPositionKey[] = "position"; | |
| 29 const char kOffsetKey[] = "offset"; | |
| 30 const char kDisplayPlacementDisplayIdKey[] = "display_id"; | |
| 31 const char kDisplayPlacementParentDisplayIdKey[] = "parent_display_id"; | |
| 32 | |
| 33 bool AddLegacyValuesFromValue(const base::Value& value, DisplayLayout* layout) { | |
| 34 const base::DictionaryValue* dict_value = nullptr; | |
| 35 if (!value.GetAsDictionary(&dict_value) || dict_value == nullptr) | |
| 36 return false; | |
| 37 int offset; | |
| 38 if (dict_value->GetInteger(kOffsetKey, &offset)) { | |
| 39 DisplayPlacement::Position position; | |
| 40 std::string position_str; | |
| 41 if (!dict_value->GetString(kPositionKey, &position_str)) | |
| 42 return false; | |
| 43 DisplayPlacement::StringToPosition(position_str, &position); | |
| 44 layout->placement_list.push_back(new DisplayPlacement(position, offset)); | |
| 45 } | |
| 46 return true; | |
| 47 } | |
| 48 | |
| 49 // Returns true if | |
| 50 // The key is missing - output is left unchanged | |
|
oshima
2016/03/17 23:19:38
can we have a separate function that checks for th
robliao
2016/03/18 21:39:57
This oddly enough is the behavior of JSONValueConv
oshima
2016/03/18 21:53:23
It should use just a default value, maybe error me
robliao
2016/03/18 22:55:58
Sounds like this is what we want then. The input i
| |
| 51 // The key matches the type - output is updated to the value. | |
| 52 template <typename Getter, typename Output> | |
| 53 bool UpdateFromDict(const base::DictionaryValue* dict_value, | |
| 54 const std::string& field_name, | |
| 55 Getter getter, | |
| 56 Output* output) { | |
| 57 const base::Value* field = nullptr; | |
| 58 if (!dict_value->Get(field_name, &field)) | |
| 59 return true; | |
| 60 | |
| 61 return (field->*getter)(output); | |
| 62 } | |
| 63 | |
| 64 // No implementation here as specialization is required. | |
| 65 template <typename Output> | |
| 66 bool UpdateFromDict(const base::DictionaryValue* dict_value, | |
| 67 const std::string& field_name, | |
| 68 Output* output); | |
| 69 | |
| 70 template <> | |
| 71 bool UpdateFromDict(const base::DictionaryValue* dict_value, | |
| 72 const std::string& field_name, | |
| 73 bool* output) { | |
| 74 return UpdateFromDict(dict_value, field_name, &base::Value::GetAsBoolean, | |
| 75 output); | |
| 76 } | |
| 77 | |
| 78 template <> | |
| 79 bool UpdateFromDict(const base::DictionaryValue* dict_value, | |
| 80 const std::string& field_name, | |
| 81 int* output) { | |
| 82 return UpdateFromDict(dict_value, field_name, &base::Value::GetAsInteger, | |
| 83 output); | |
| 84 } | |
| 85 | |
| 86 template <> | |
| 87 bool UpdateFromDict(const base::DictionaryValue* dict_value, | |
| 88 const std::string& field_name, | |
| 89 DisplayPlacement::Position* output) { | |
| 90 bool (base::Value::*getter)(std::string*) const = &base::Value::GetAsString; | |
| 91 std::string value; | |
| 92 if (!UpdateFromDict(dict_value, field_name, getter, &value)) | |
| 93 return false; | |
| 94 | |
| 95 return value.empty() ? true | |
| 96 : DisplayPlacement::StringToPosition(value, output); | |
| 97 } | |
| 98 | |
| 99 template <> | |
| 100 bool UpdateFromDict(const base::DictionaryValue* dict_value, | |
| 101 const std::string& field_name, | |
| 102 int64_t* output) { | |
| 103 bool (base::Value::*getter)(std::string*) const = &base::Value::GetAsString; | |
| 104 std::string value; | |
| 105 if (!UpdateFromDict(dict_value, field_name, getter, &value)) | |
| 106 return false; | |
| 107 | |
| 108 return value.empty() ? true : base::StringToInt64(value, output); | |
| 109 } | |
| 110 | |
| 111 template <> | |
| 112 bool UpdateFromDict(const base::DictionaryValue* dict_value, | |
| 113 const std::string& field_name, | |
| 114 ScopedVector<DisplayPlacement>* output) { | |
| 115 bool (base::Value::*getter)(const base::ListValue**) const = | |
| 116 &base::Value::GetAsList; | |
| 117 const base::ListValue* list = nullptr; | |
| 118 if (!UpdateFromDict(dict_value, field_name, getter, &list)) | |
| 119 return false; | |
| 120 | |
| 121 if (list == nullptr) | |
| 122 return true; | |
| 123 | |
| 124 output->reserve(list->GetSize()); | |
| 125 for (const auto& list_item : *list) { | |
| 126 const base::DictionaryValue* item_values = nullptr; | |
| 127 if (!list_item->GetAsDictionary(&item_values) || item_values == nullptr) | |
|
oshima
2016/03/17 23:19:38
do you need || item_values == nullptr?
robliao
2016/03/18 21:39:57
That's a good question. I just pulled this from th
| |
| 128 return false; | |
| 129 | |
| 130 scoped_ptr<DisplayPlacement> item(new DisplayPlacement); | |
| 131 if (!UpdateFromDict(item_values, kOffsetKey, &item->offset) || | |
| 132 !UpdateFromDict(item_values, kPositionKey, &item->position) || | |
| 133 !UpdateFromDict(item_values, kDisplayPlacementDisplayIdKey, | |
| 134 &item->display_id) || | |
| 135 !UpdateFromDict(item_values, kDisplayPlacementParentDisplayIdKey, | |
| 136 &item->parent_display_id)) { | |
| 137 return false; | |
| 138 } | |
| 139 | |
| 140 output->push_back(std::move(item)); | |
| 141 } | |
| 142 return true; | |
| 143 } | |
| 144 | |
| 145 } // namespace | |
| 146 | |
| 147 bool JsonToDisplayLayout(const base::Value& value, DisplayLayout* layout) { | |
| 148 layout->placement_list.clear(); | |
| 149 const base::DictionaryValue* dict_value = nullptr; | |
| 150 if (!value.GetAsDictionary(&dict_value) || dict_value == nullptr) | |
|
oshima
2016/03/17 23:19:38
ditto
robliao
2016/03/18 21:39:57
Resolved. See above.
| |
| 151 return false; | |
| 152 | |
| 153 if (!UpdateFromDict(dict_value, kMirroredKey, &layout->mirrored) || | |
| 154 !UpdateFromDict(dict_value, kDefaultUnifiedKey, | |
| 155 &layout->default_unified) || | |
| 156 !UpdateFromDict(dict_value, kPrimaryIdKey, &layout->primary_id)) { | |
| 157 return false; | |
| 158 } | |
| 159 | |
| 160 UpdateFromDict(dict_value, kDisplayPlacementKey, &layout->placement_list); | |
| 161 | |
| 162 if (layout->placement_list.size() != 0u) | |
| 163 return true; | |
| 164 | |
| 165 // For compatibility with old format. | |
| 166 return AddLegacyValuesFromValue(value, layout); | |
| 167 } | |
| 168 | |
| 169 bool DisplayLayoutToJson(const DisplayLayout& layout, base::Value* value) { | |
| 170 base::DictionaryValue* dict_value = nullptr; | |
| 171 if (!value->GetAsDictionary(&dict_value) || dict_value == nullptr) | |
| 172 return false; | |
| 173 | |
| 174 dict_value->SetBoolean(kMirroredKey, layout.mirrored); | |
| 175 dict_value->SetBoolean(kDefaultUnifiedKey, layout.default_unified); | |
| 176 dict_value->SetString(kPrimaryIdKey, base::Int64ToString(layout.primary_id)); | |
| 177 | |
| 178 scoped_ptr<base::ListValue> placement_list(new base::ListValue); | |
| 179 for (const auto* placement : layout.placement_list) { | |
| 180 scoped_ptr<base::DictionaryValue> placement_value( | |
| 181 new base::DictionaryValue); | |
| 182 placement_value->SetString( | |
| 183 kPositionKey, DisplayPlacement::PositionToString(placement->position)); | |
| 184 placement_value->SetInteger(kOffsetKey, placement->offset); | |
| 185 placement_value->SetString(kDisplayPlacementDisplayIdKey, | |
| 186 base::Int64ToString(placement->display_id)); | |
| 187 placement_value->SetString( | |
| 188 kDisplayPlacementParentDisplayIdKey, | |
| 189 base::Int64ToString(placement->parent_display_id)); | |
| 190 placement_list->Append(std::move(placement_value)); | |
| 191 } | |
| 192 dict_value->Set(kDisplayPlacementKey, std::move(placement_list)); | |
| 193 return true; | |
| 194 } | |
| 195 | |
| 196 } // namespace ash | |
| OLD | NEW |