| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ash/display/json_converter.h" | 5 #include "ash/display/json_converter.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "ash/display/display_layout.h" | |
| 10 #include "ash/display/display_pref_util.h" | 9 #include "ash/display/display_pref_util.h" |
| 11 #include "base/logging.h" | 10 #include "base/logging.h" |
| 12 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
| 13 #include "base/values.h" | 12 #include "base/values.h" |
| 13 #include "ui/display/manager/display_layout.h" |
| 14 | 14 |
| 15 namespace ash { | 15 namespace ash { |
| 16 | 16 |
| 17 namespace { | 17 namespace { |
| 18 | 18 |
| 19 // Persistent key names | 19 // Persistent key names |
| 20 const char kMirroredKey[] = "mirrored"; | 20 const char kMirroredKey[] = "mirrored"; |
| 21 const char kDefaultUnifiedKey[] = "default_unified"; | 21 const char kDefaultUnifiedKey[] = "default_unified"; |
| 22 const char kPrimaryIdKey[] = "primary-id"; | 22 const char kPrimaryIdKey[] = "primary-id"; |
| 23 const char kDisplayPlacementKey[] = "display_placement"; | 23 const char kDisplayPlacementKey[] = "display_placement"; |
| 24 | 24 |
| 25 // DisplayPlacement key names | 25 // DisplayPlacement key names |
| 26 const char kPositionKey[] = "position"; | 26 const char kPositionKey[] = "position"; |
| 27 const char kOffsetKey[] = "offset"; | 27 const char kOffsetKey[] = "offset"; |
| 28 const char kDisplayPlacementDisplayIdKey[] = "display_id"; | 28 const char kDisplayPlacementDisplayIdKey[] = "display_id"; |
| 29 const char kDisplayPlacementParentDisplayIdKey[] = "parent_display_id"; | 29 const char kDisplayPlacementParentDisplayIdKey[] = "parent_display_id"; |
| 30 | 30 |
| 31 bool AddLegacyValuesFromValue(const base::Value& value, DisplayLayout* layout) { | 31 bool AddLegacyValuesFromValue(const base::Value& value, |
| 32 display::DisplayLayout* layout) { |
| 32 const base::DictionaryValue* dict_value = nullptr; | 33 const base::DictionaryValue* dict_value = nullptr; |
| 33 if (!value.GetAsDictionary(&dict_value)) | 34 if (!value.GetAsDictionary(&dict_value)) |
| 34 return false; | 35 return false; |
| 35 int offset; | 36 int offset; |
| 36 if (dict_value->GetInteger(kOffsetKey, &offset)) { | 37 if (dict_value->GetInteger(kOffsetKey, &offset)) { |
| 37 DisplayPlacement::Position position; | 38 display::DisplayPlacement::Position position; |
| 38 std::string position_str; | 39 std::string position_str; |
| 39 if (!dict_value->GetString(kPositionKey, &position_str)) | 40 if (!dict_value->GetString(kPositionKey, &position_str)) |
| 40 return false; | 41 return false; |
| 41 DisplayPlacement::StringToPosition(position_str, &position); | 42 display::DisplayPlacement::StringToPosition(position_str, &position); |
| 42 layout->placement_list.emplace_back(position, offset); | 43 layout->placement_list.emplace_back(position, offset); |
| 43 } | 44 } |
| 44 return true; | 45 return true; |
| 45 } | 46 } |
| 46 | 47 |
| 47 // Returns true if | 48 // Returns true if |
| 48 // The key is missing - output is left unchanged | 49 // The key is missing - output is left unchanged |
| 49 // The key matches the type - output is updated to the value. | 50 // The key matches the type - output is updated to the value. |
| 50 template <typename Getter, typename Output> | 51 template <typename Getter, typename Output> |
| 51 bool UpdateFromDict(const base::DictionaryValue* dict_value, | 52 bool UpdateFromDict(const base::DictionaryValue* dict_value, |
| (...skipping 27 matching lines...) Expand all Loading... |
| 79 bool UpdateFromDict(const base::DictionaryValue* dict_value, | 80 bool UpdateFromDict(const base::DictionaryValue* dict_value, |
| 80 const std::string& field_name, | 81 const std::string& field_name, |
| 81 int* output) { | 82 int* output) { |
| 82 return UpdateFromDict(dict_value, field_name, &base::Value::GetAsInteger, | 83 return UpdateFromDict(dict_value, field_name, &base::Value::GetAsInteger, |
| 83 output); | 84 output); |
| 84 } | 85 } |
| 85 | 86 |
| 86 template <> | 87 template <> |
| 87 bool UpdateFromDict(const base::DictionaryValue* dict_value, | 88 bool UpdateFromDict(const base::DictionaryValue* dict_value, |
| 88 const std::string& field_name, | 89 const std::string& field_name, |
| 89 DisplayPlacement::Position* output) { | 90 display::DisplayPlacement::Position* output) { |
| 90 bool (base::Value::*getter)(std::string*) const = &base::Value::GetAsString; | 91 bool (base::Value::*getter)(std::string*) const = &base::Value::GetAsString; |
| 91 std::string value; | 92 std::string value; |
| 92 if (!UpdateFromDict(dict_value, field_name, getter, &value)) | 93 if (!UpdateFromDict(dict_value, field_name, getter, &value)) |
| 93 return false; | 94 return false; |
| 94 | 95 |
| 95 return value.empty() ? true | 96 return value.empty() ? true : display::DisplayPlacement::StringToPosition( |
| 96 : DisplayPlacement::StringToPosition(value, output); | 97 value, output); |
| 97 } | 98 } |
| 98 | 99 |
| 99 template <> | 100 template <> |
| 100 bool UpdateFromDict(const base::DictionaryValue* dict_value, | 101 bool UpdateFromDict(const base::DictionaryValue* dict_value, |
| 101 const std::string& field_name, | 102 const std::string& field_name, |
| 102 int64_t* output) { | 103 int64_t* output) { |
| 103 bool (base::Value::*getter)(std::string*) const = &base::Value::GetAsString; | 104 bool (base::Value::*getter)(std::string*) const = &base::Value::GetAsString; |
| 104 std::string value; | 105 std::string value; |
| 105 if (!UpdateFromDict(dict_value, field_name, getter, &value)) | 106 if (!UpdateFromDict(dict_value, field_name, getter, &value)) |
| 106 return false; | 107 return false; |
| 107 | 108 |
| 108 return value.empty() ? true : base::StringToInt64(value, output); | 109 return value.empty() ? true : base::StringToInt64(value, output); |
| 109 } | 110 } |
| 110 | 111 |
| 111 template <> | 112 template <> |
| 112 bool UpdateFromDict(const base::DictionaryValue* dict_value, | 113 bool UpdateFromDict(const base::DictionaryValue* dict_value, |
| 113 const std::string& field_name, | 114 const std::string& field_name, |
| 114 std::vector<DisplayPlacement>* output) { | 115 std::vector<display::DisplayPlacement>* output) { |
| 115 bool (base::Value::*getter)(const base::ListValue**) const = | 116 bool (base::Value::*getter)(const base::ListValue**) const = |
| 116 &base::Value::GetAsList; | 117 &base::Value::GetAsList; |
| 117 const base::ListValue* list = nullptr; | 118 const base::ListValue* list = nullptr; |
| 118 if (!UpdateFromDict(dict_value, field_name, getter, &list)) | 119 if (!UpdateFromDict(dict_value, field_name, getter, &list)) |
| 119 return false; | 120 return false; |
| 120 | 121 |
| 121 if (list == nullptr) | 122 if (list == nullptr) |
| 122 return true; | 123 return true; |
| 123 | 124 |
| 124 output->reserve(list->GetSize()); | 125 output->reserve(list->GetSize()); |
| 125 for (const auto& list_item : *list) { | 126 for (const auto& list_item : *list) { |
| 126 const base::DictionaryValue* item_values = nullptr; | 127 const base::DictionaryValue* item_values = nullptr; |
| 127 if (!list_item->GetAsDictionary(&item_values)) | 128 if (!list_item->GetAsDictionary(&item_values)) |
| 128 return false; | 129 return false; |
| 129 | 130 |
| 130 DisplayPlacement item; | 131 display::DisplayPlacement item; |
| 131 if (!UpdateFromDict(item_values, kOffsetKey, &item.offset) || | 132 if (!UpdateFromDict(item_values, kOffsetKey, &item.offset) || |
| 132 !UpdateFromDict(item_values, kPositionKey, &item.position) || | 133 !UpdateFromDict(item_values, kPositionKey, &item.position) || |
| 133 !UpdateFromDict(item_values, kDisplayPlacementDisplayIdKey, | 134 !UpdateFromDict(item_values, kDisplayPlacementDisplayIdKey, |
| 134 &item.display_id) || | 135 &item.display_id) || |
| 135 !UpdateFromDict(item_values, kDisplayPlacementParentDisplayIdKey, | 136 !UpdateFromDict(item_values, kDisplayPlacementParentDisplayIdKey, |
| 136 &item.parent_display_id)) { | 137 &item.parent_display_id)) { |
| 137 return false; | 138 return false; |
| 138 } | 139 } |
| 139 | 140 |
| 140 output->push_back(item); | 141 output->push_back(item); |
| 141 } | 142 } |
| 142 return true; | 143 return true; |
| 143 } | 144 } |
| 144 | 145 |
| 145 } // namespace | 146 } // namespace |
| 146 | 147 |
| 147 bool JsonToDisplayLayout(const base::Value& value, DisplayLayout* layout) { | 148 bool JsonToDisplayLayout(const base::Value& value, |
| 149 display::DisplayLayout* layout) { |
| 148 layout->placement_list.clear(); | 150 layout->placement_list.clear(); |
| 149 const base::DictionaryValue* dict_value = nullptr; | 151 const base::DictionaryValue* dict_value = nullptr; |
| 150 if (!value.GetAsDictionary(&dict_value)) | 152 if (!value.GetAsDictionary(&dict_value)) |
| 151 return false; | 153 return false; |
| 152 | 154 |
| 153 if (!UpdateFromDict(dict_value, kMirroredKey, &layout->mirrored) || | 155 if (!UpdateFromDict(dict_value, kMirroredKey, &layout->mirrored) || |
| 154 !UpdateFromDict(dict_value, kDefaultUnifiedKey, | 156 !UpdateFromDict(dict_value, kDefaultUnifiedKey, |
| 155 &layout->default_unified) || | 157 &layout->default_unified) || |
| 156 !UpdateFromDict(dict_value, kPrimaryIdKey, &layout->primary_id)) { | 158 !UpdateFromDict(dict_value, kPrimaryIdKey, &layout->primary_id)) { |
| 157 return false; | 159 return false; |
| 158 } | 160 } |
| 159 | 161 |
| 160 UpdateFromDict(dict_value, kDisplayPlacementKey, &layout->placement_list); | 162 UpdateFromDict(dict_value, kDisplayPlacementKey, &layout->placement_list); |
| 161 | 163 |
| 162 if (layout->placement_list.size() != 0u) | 164 if (layout->placement_list.size() != 0u) |
| 163 return true; | 165 return true; |
| 164 | 166 |
| 165 // For compatibility with old format. | 167 // For compatibility with old format. |
| 166 return AddLegacyValuesFromValue(value, layout); | 168 return AddLegacyValuesFromValue(value, layout); |
| 167 } | 169 } |
| 168 | 170 |
| 169 bool DisplayLayoutToJson(const DisplayLayout& layout, base::Value* value) { | 171 bool DisplayLayoutToJson(const display::DisplayLayout& layout, |
| 172 base::Value* value) { |
| 170 base::DictionaryValue* dict_value = nullptr; | 173 base::DictionaryValue* dict_value = nullptr; |
| 171 if (!value->GetAsDictionary(&dict_value)) | 174 if (!value->GetAsDictionary(&dict_value)) |
| 172 return false; | 175 return false; |
| 173 | 176 |
| 174 dict_value->SetBoolean(kMirroredKey, layout.mirrored); | 177 dict_value->SetBoolean(kMirroredKey, layout.mirrored); |
| 175 dict_value->SetBoolean(kDefaultUnifiedKey, layout.default_unified); | 178 dict_value->SetBoolean(kDefaultUnifiedKey, layout.default_unified); |
| 176 dict_value->SetString(kPrimaryIdKey, base::Int64ToString(layout.primary_id)); | 179 dict_value->SetString(kPrimaryIdKey, base::Int64ToString(layout.primary_id)); |
| 177 | 180 |
| 178 scoped_ptr<base::ListValue> placement_list(new base::ListValue); | 181 scoped_ptr<base::ListValue> placement_list(new base::ListValue); |
| 179 for (const auto& placement : layout.placement_list) { | 182 for (const auto& placement : layout.placement_list) { |
| 180 scoped_ptr<base::DictionaryValue> placement_value( | 183 scoped_ptr<base::DictionaryValue> placement_value( |
| 181 new base::DictionaryValue); | 184 new base::DictionaryValue); |
| 182 placement_value->SetString( | 185 placement_value->SetString( |
| 183 kPositionKey, DisplayPlacement::PositionToString(placement.position)); | 186 kPositionKey, |
| 187 display::DisplayPlacement::PositionToString(placement.position)); |
| 184 placement_value->SetInteger(kOffsetKey, placement.offset); | 188 placement_value->SetInteger(kOffsetKey, placement.offset); |
| 185 placement_value->SetString(kDisplayPlacementDisplayIdKey, | 189 placement_value->SetString(kDisplayPlacementDisplayIdKey, |
| 186 base::Int64ToString(placement.display_id)); | 190 base::Int64ToString(placement.display_id)); |
| 187 placement_value->SetString( | 191 placement_value->SetString( |
| 188 kDisplayPlacementParentDisplayIdKey, | 192 kDisplayPlacementParentDisplayIdKey, |
| 189 base::Int64ToString(placement.parent_display_id)); | 193 base::Int64ToString(placement.parent_display_id)); |
| 190 placement_list->Append(std::move(placement_value)); | 194 placement_list->Append(std::move(placement_value)); |
| 191 } | 195 } |
| 192 dict_value->Set(kDisplayPlacementKey, std::move(placement_list)); | 196 dict_value->Set(kDisplayPlacementKey, std::move(placement_list)); |
| 193 return true; | 197 return true; |
| 194 } | 198 } |
| 195 | 199 |
| 196 } // namespace ash | 200 } // namespace ash |
| OLD | NEW |