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