| 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 <memory> | |
| 8 #include <string> | |
| 9 | |
| 10 #include "ash/display/display_pref_util.h" | |
| 11 #include "base/logging.h" | |
| 12 #include "base/strings/string_number_conversions.h" | |
| 13 #include "base/values.h" | |
| 14 #include "ui/display/display_layout.h" | |
| 15 | |
| 16 namespace ash { | |
| 17 | |
| 18 namespace { | |
| 19 | |
| 20 // Persistent key names | |
| 21 const char kMirroredKey[] = "mirrored"; | |
| 22 const char kDefaultUnifiedKey[] = "default_unified"; | |
| 23 const char kPrimaryIdKey[] = "primary-id"; | |
| 24 const char kDisplayPlacementKey[] = "display_placement"; | |
| 25 | |
| 26 // DisplayPlacement key names | |
| 27 const char kPositionKey[] = "position"; | |
| 28 const char kOffsetKey[] = "offset"; | |
| 29 const char kDisplayPlacementDisplayIdKey[] = "display_id"; | |
| 30 const char kDisplayPlacementParentDisplayIdKey[] = "parent_display_id"; | |
| 31 | |
| 32 bool AddLegacyValuesFromValue(const base::Value& value, | |
| 33 display::DisplayLayout* layout) { | |
| 34 const base::DictionaryValue* dict_value = nullptr; | |
| 35 if (!value.GetAsDictionary(&dict_value)) | |
| 36 return false; | |
| 37 int offset; | |
| 38 if (dict_value->GetInteger(kOffsetKey, &offset)) { | |
| 39 display::DisplayPlacement::Position position; | |
| 40 std::string position_str; | |
| 41 if (!dict_value->GetString(kPositionKey, &position_str)) | |
| 42 return false; | |
| 43 display::DisplayPlacement::StringToPosition(position_str, &position); | |
| 44 layout->placement_list.emplace_back(position, offset); | |
| 45 } | |
| 46 return true; | |
| 47 } | |
| 48 | |
| 49 // Returns true if | |
| 50 // The key is missing - output is left unchanged | |
| 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 LOG(WARNING) << "Missing field: " << field_name; | |
| 60 return true; | |
| 61 } | |
| 62 | |
| 63 return (field->*getter)(output); | |
| 64 } | |
| 65 | |
| 66 // No implementation here as specialization is required. | |
| 67 template <typename Output> | |
| 68 bool UpdateFromDict(const base::DictionaryValue* dict_value, | |
| 69 const std::string& field_name, | |
| 70 Output* output); | |
| 71 | |
| 72 template <> | |
| 73 bool UpdateFromDict(const base::DictionaryValue* dict_value, | |
| 74 const std::string& field_name, | |
| 75 bool* output) { | |
| 76 return UpdateFromDict(dict_value, field_name, &base::Value::GetAsBoolean, | |
| 77 output); | |
| 78 } | |
| 79 | |
| 80 template <> | |
| 81 bool UpdateFromDict(const base::DictionaryValue* dict_value, | |
| 82 const std::string& field_name, | |
| 83 int* output) { | |
| 84 return UpdateFromDict(dict_value, field_name, &base::Value::GetAsInteger, | |
| 85 output); | |
| 86 } | |
| 87 | |
| 88 template <> | |
| 89 bool UpdateFromDict(const base::DictionaryValue* dict_value, | |
| 90 const std::string& field_name, | |
| 91 display::DisplayPlacement::Position* output) { | |
| 92 bool (base::Value::*getter)(std::string*) const = &base::Value::GetAsString; | |
| 93 std::string value; | |
| 94 if (!UpdateFromDict(dict_value, field_name, getter, &value)) | |
| 95 return false; | |
| 96 | |
| 97 return value.empty() ? true : display::DisplayPlacement::StringToPosition( | |
| 98 value, output); | |
| 99 } | |
| 100 | |
| 101 template <> | |
| 102 bool UpdateFromDict(const base::DictionaryValue* dict_value, | |
| 103 const std::string& field_name, | |
| 104 int64_t* output) { | |
| 105 bool (base::Value::*getter)(std::string*) const = &base::Value::GetAsString; | |
| 106 std::string value; | |
| 107 if (!UpdateFromDict(dict_value, field_name, getter, &value)) | |
| 108 return false; | |
| 109 | |
| 110 return value.empty() ? true : base::StringToInt64(value, output); | |
| 111 } | |
| 112 | |
| 113 template <> | |
| 114 bool UpdateFromDict(const base::DictionaryValue* dict_value, | |
| 115 const std::string& field_name, | |
| 116 std::vector<display::DisplayPlacement>* output) { | |
| 117 bool (base::Value::*getter)(const base::ListValue**) const = | |
| 118 &base::Value::GetAsList; | |
| 119 const base::ListValue* list = nullptr; | |
| 120 if (!UpdateFromDict(dict_value, field_name, getter, &list)) | |
| 121 return false; | |
| 122 | |
| 123 if (list == nullptr) | |
| 124 return true; | |
| 125 | |
| 126 output->reserve(list->GetSize()); | |
| 127 for (const auto& list_item : *list) { | |
| 128 const base::DictionaryValue* item_values = nullptr; | |
| 129 if (!list_item->GetAsDictionary(&item_values)) | |
| 130 return false; | |
| 131 | |
| 132 display::DisplayPlacement item; | |
| 133 if (!UpdateFromDict(item_values, kOffsetKey, &item.offset) || | |
| 134 !UpdateFromDict(item_values, kPositionKey, &item.position) || | |
| 135 !UpdateFromDict(item_values, kDisplayPlacementDisplayIdKey, | |
| 136 &item.display_id) || | |
| 137 !UpdateFromDict(item_values, kDisplayPlacementParentDisplayIdKey, | |
| 138 &item.parent_display_id)) { | |
| 139 return false; | |
| 140 } | |
| 141 | |
| 142 output->push_back(item); | |
| 143 } | |
| 144 return true; | |
| 145 } | |
| 146 | |
| 147 } // namespace | |
| 148 | |
| 149 bool JsonToDisplayLayout(const base::Value& value, | |
| 150 display::DisplayLayout* layout) { | |
| 151 layout->placement_list.clear(); | |
| 152 const base::DictionaryValue* dict_value = nullptr; | |
| 153 if (!value.GetAsDictionary(&dict_value)) | |
| 154 return false; | |
| 155 | |
| 156 if (!UpdateFromDict(dict_value, kMirroredKey, &layout->mirrored) || | |
| 157 !UpdateFromDict(dict_value, kDefaultUnifiedKey, | |
| 158 &layout->default_unified) || | |
| 159 !UpdateFromDict(dict_value, kPrimaryIdKey, &layout->primary_id)) { | |
| 160 return false; | |
| 161 } | |
| 162 | |
| 163 UpdateFromDict(dict_value, kDisplayPlacementKey, &layout->placement_list); | |
| 164 | |
| 165 if (layout->placement_list.size() != 0u) | |
| 166 return true; | |
| 167 | |
| 168 // For compatibility with old format. | |
| 169 return AddLegacyValuesFromValue(value, layout); | |
| 170 } | |
| 171 | |
| 172 bool DisplayLayoutToJson(const display::DisplayLayout& layout, | |
| 173 base::Value* value) { | |
| 174 base::DictionaryValue* dict_value = nullptr; | |
| 175 if (!value->GetAsDictionary(&dict_value)) | |
| 176 return false; | |
| 177 | |
| 178 dict_value->SetBoolean(kMirroredKey, layout.mirrored); | |
| 179 dict_value->SetBoolean(kDefaultUnifiedKey, layout.default_unified); | |
| 180 dict_value->SetString(kPrimaryIdKey, base::Int64ToString(layout.primary_id)); | |
| 181 | |
| 182 std::unique_ptr<base::ListValue> placement_list(new base::ListValue); | |
| 183 for (const auto& placement : layout.placement_list) { | |
| 184 std::unique_ptr<base::DictionaryValue> placement_value( | |
| 185 new base::DictionaryValue); | |
| 186 placement_value->SetString( | |
| 187 kPositionKey, | |
| 188 display::DisplayPlacement::PositionToString(placement.position)); | |
| 189 placement_value->SetInteger(kOffsetKey, placement.offset); | |
| 190 placement_value->SetString(kDisplayPlacementDisplayIdKey, | |
| 191 base::Int64ToString(placement.display_id)); | |
| 192 placement_value->SetString( | |
| 193 kDisplayPlacementParentDisplayIdKey, | |
| 194 base::Int64ToString(placement.parent_display_id)); | |
| 195 placement_list->Append(std::move(placement_value)); | |
| 196 } | |
| 197 dict_value->Set(kDisplayPlacementKey, std::move(placement_list)); | |
| 198 return true; | |
| 199 } | |
| 200 | |
| 201 } // namespace ash | |
| OLD | NEW |