Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(288)

Side by Side Diff: ash/display/json_converter.cc

Issue 1808253004: Refactor JSONValueConverter Out of DisplayLayout (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Update Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ash/display/json_converter.h ('k') | ash/display/json_converter_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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>
oshima 2016/03/21 19:16:53 do you need this (and map) ?
robliao 2016/03/21 20:13:02 Nope. Done. These were needed during code transiti
10
11 #include "ash/display/display_layout.h"
12 #include "ash/display/display_pref_util.h"
13 #include "base/logging.h"
14 #include "base/memory/scoped_vector.h"
15 #include "base/strings/string_number_conversions.h"
16 #include "base/values.h"
17
18 namespace ash {
19
20 namespace {
21
22 // Persistent key names
23 const char kMirroredKey[] = "mirrored";
24 const char kDefaultUnifiedKey[] = "default_unified";
25 const char kPrimaryIdKey[] = "primary-id";
26 const char kDisplayPlacementKey[] = "display_placement";
27
28 // DisplayPlacement key names
29 const char kPositionKey[] = "position";
30 const char kOffsetKey[] = "offset";
31 const char kDisplayPlacementDisplayIdKey[] = "display_id";
32 const char kDisplayPlacementParentDisplayIdKey[] = "parent_display_id";
33
34 bool AddLegacyValuesFromValue(const base::Value& value, DisplayLayout* layout) {
35 const base::DictionaryValue* dict_value = nullptr;
36 if (!value.GetAsDictionary(&dict_value))
37 return false;
38 int offset;
39 if (dict_value->GetInteger(kOffsetKey, &offset)) {
40 DisplayPlacement::Position position;
41 std::string position_str;
42 if (!dict_value->GetString(kPositionKey, &position_str))
43 return false;
44 DisplayPlacement::StringToPosition(position_str, &position);
45 layout->placement_list.push_back(new DisplayPlacement(position, offset));
46 }
47 return true;
48 }
49
50 // Returns true if
51 // The key is missing - output is left unchanged
52 // The key matches the type - output is updated to the value.
53 template <typename Getter, typename Output>
54 bool UpdateFromDict(const base::DictionaryValue* dict_value,
55 const std::string& field_name,
56 Getter getter,
57 Output* output) {
58 const base::Value* field = nullptr;
59 if (!dict_value->Get(field_name, &field)) {
60 LOG(WARNING) << "Missing field: " << field_name;
61 return true;
62 }
63
64 return (field->*getter)(output);
65 }
66
67 // No implementation here as specialization is required.
68 template <typename Output>
69 bool UpdateFromDict(const base::DictionaryValue* dict_value,
70 const std::string& field_name,
71 Output* output);
72
73 template <>
74 bool UpdateFromDict(const base::DictionaryValue* dict_value,
75 const std::string& field_name,
76 bool* output) {
77 return UpdateFromDict(dict_value, field_name, &base::Value::GetAsBoolean,
78 output);
79 }
80
81 template <>
82 bool UpdateFromDict(const base::DictionaryValue* dict_value,
83 const std::string& field_name,
84 int* output) {
85 return UpdateFromDict(dict_value, field_name, &base::Value::GetAsInteger,
86 output);
87 }
88
89 template <>
90 bool UpdateFromDict(const base::DictionaryValue* dict_value,
91 const std::string& field_name,
92 DisplayPlacement::Position* output) {
93 bool (base::Value::*getter)(std::string*) const = &base::Value::GetAsString;
94 std::string value;
95 if (!UpdateFromDict(dict_value, field_name, getter, &value))
96 return false;
97
98 return value.empty() ? true
99 : DisplayPlacement::StringToPosition(value, output);
100 }
101
102 template <>
103 bool UpdateFromDict(const base::DictionaryValue* dict_value,
104 const std::string& field_name,
105 int64_t* output) {
106 bool (base::Value::*getter)(std::string*) const = &base::Value::GetAsString;
107 std::string value;
108 if (!UpdateFromDict(dict_value, field_name, getter, &value))
109 return false;
110
111 return value.empty() ? true : base::StringToInt64(value, output);
112 }
113
114 template <>
115 bool UpdateFromDict(const base::DictionaryValue* dict_value,
116 const std::string& field_name,
117 ScopedVector<DisplayPlacement>* output) {
118 bool (base::Value::*getter)(const base::ListValue**) const =
119 &base::Value::GetAsList;
120 const base::ListValue* list = nullptr;
121 if (!UpdateFromDict(dict_value, field_name, getter, &list))
122 return false;
123
124 if (list == nullptr)
125 return true;
126
127 output->reserve(list->GetSize());
128 for (const auto& list_item : *list) {
129 const base::DictionaryValue* item_values = nullptr;
130 if (!list_item->GetAsDictionary(&item_values))
131 return false;
132
133 scoped_ptr<DisplayPlacement> item(new DisplayPlacement);
134 if (!UpdateFromDict(item_values, kOffsetKey, &item->offset) ||
135 !UpdateFromDict(item_values, kPositionKey, &item->position) ||
136 !UpdateFromDict(item_values, kDisplayPlacementDisplayIdKey,
137 &item->display_id) ||
138 !UpdateFromDict(item_values, kDisplayPlacementParentDisplayIdKey,
139 &item->parent_display_id)) {
140 return false;
141 }
142
143 output->push_back(std::move(item));
144 }
145 return true;
146 }
147
148 } // namespace
149
150 bool JsonToDisplayLayout(const base::Value& value, 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 DisplayLayout& layout, base::Value* value) {
173 base::DictionaryValue* dict_value = nullptr;
174 if (!value->GetAsDictionary(&dict_value))
175 return false;
176
177 dict_value->SetBoolean(kMirroredKey, layout.mirrored);
178 dict_value->SetBoolean(kDefaultUnifiedKey, layout.default_unified);
179 dict_value->SetString(kPrimaryIdKey, base::Int64ToString(layout.primary_id));
180
181 scoped_ptr<base::ListValue> placement_list(new base::ListValue);
182 for (const auto* placement : layout.placement_list) {
183 scoped_ptr<base::DictionaryValue> placement_value(
184 new base::DictionaryValue);
185 placement_value->SetString(
186 kPositionKey, DisplayPlacement::PositionToString(placement->position));
187 placement_value->SetInteger(kOffsetKey, placement->offset);
188 placement_value->SetString(kDisplayPlacementDisplayIdKey,
189 base::Int64ToString(placement->display_id));
190 placement_value->SetString(
191 kDisplayPlacementParentDisplayIdKey,
192 base::Int64ToString(placement->parent_display_id));
193 placement_list->Append(std::move(placement_value));
194 }
195 dict_value->Set(kDisplayPlacementKey, std::move(placement_list));
196 return true;
197 }
198
199 } // namespace ash
OLDNEW
« no previous file with comments | « ash/display/json_converter.h ('k') | ash/display/json_converter_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698