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

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

Issue 1819533002: Convert ScopedVector<DisplayPlacement> to std::vector<DisplayPlacement> (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@jsonrefactor
Patch Set: 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
OLDNEW
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" 9 #include "ash/display/display_layout.h"
10 #include "ash/display/display_pref_util.h" 10 #include "ash/display/display_pref_util.h"
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/memory/scoped_vector.h"
13 #include "base/strings/string_number_conversions.h" 12 #include "base/strings/string_number_conversions.h"
14 #include "base/values.h" 13 #include "base/values.h"
15 14
16 namespace ash { 15 namespace ash {
17 16
18 namespace { 17 namespace {
19 18
20 // Persistent key names 19 // Persistent key names
21 const char kMirroredKey[] = "mirrored"; 20 const char kMirroredKey[] = "mirrored";
22 const char kDefaultUnifiedKey[] = "default_unified"; 21 const char kDefaultUnifiedKey[] = "default_unified";
(...skipping 10 matching lines...) Expand all
33 const base::DictionaryValue* dict_value = nullptr; 32 const base::DictionaryValue* dict_value = nullptr;
34 if (!value.GetAsDictionary(&dict_value)) 33 if (!value.GetAsDictionary(&dict_value))
35 return false; 34 return false;
36 int offset; 35 int offset;
37 if (dict_value->GetInteger(kOffsetKey, &offset)) { 36 if (dict_value->GetInteger(kOffsetKey, &offset)) {
38 DisplayPlacement::Position position; 37 DisplayPlacement::Position position;
39 std::string position_str; 38 std::string position_str;
40 if (!dict_value->GetString(kPositionKey, &position_str)) 39 if (!dict_value->GetString(kPositionKey, &position_str))
41 return false; 40 return false;
42 DisplayPlacement::StringToPosition(position_str, &position); 41 DisplayPlacement::StringToPosition(position_str, &position);
43 layout->placement_list.push_back(new DisplayPlacement(position, offset)); 42 layout->placement_list.emplace_back(position, offset);
44 } 43 }
45 return true; 44 return true;
46 } 45 }
47 46
48 // Returns true if 47 // Returns true if
49 // The key is missing - output is left unchanged 48 // The key is missing - output is left unchanged
50 // The key matches the type - output is updated to the value. 49 // The key matches the type - output is updated to the value.
51 template <typename Getter, typename Output> 50 template <typename Getter, typename Output>
52 bool UpdateFromDict(const base::DictionaryValue* dict_value, 51 bool UpdateFromDict(const base::DictionaryValue* dict_value,
53 const std::string& field_name, 52 const std::string& field_name,
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
105 std::string value; 104 std::string value;
106 if (!UpdateFromDict(dict_value, field_name, getter, &value)) 105 if (!UpdateFromDict(dict_value, field_name, getter, &value))
107 return false; 106 return false;
108 107
109 return value.empty() ? true : base::StringToInt64(value, output); 108 return value.empty() ? true : base::StringToInt64(value, output);
110 } 109 }
111 110
112 template <> 111 template <>
113 bool UpdateFromDict(const base::DictionaryValue* dict_value, 112 bool UpdateFromDict(const base::DictionaryValue* dict_value,
114 const std::string& field_name, 113 const std::string& field_name,
115 ScopedVector<DisplayPlacement>* output) { 114 std::vector<DisplayPlacement>* output) {
116 bool (base::Value::*getter)(const base::ListValue**) const = 115 bool (base::Value::*getter)(const base::ListValue**) const =
117 &base::Value::GetAsList; 116 &base::Value::GetAsList;
118 const base::ListValue* list = nullptr; 117 const base::ListValue* list = nullptr;
119 if (!UpdateFromDict(dict_value, field_name, getter, &list)) 118 if (!UpdateFromDict(dict_value, field_name, getter, &list))
120 return false; 119 return false;
121 120
122 if (list == nullptr) 121 if (list == nullptr)
123 return true; 122 return true;
124 123
125 output->reserve(list->GetSize()); 124 output->reserve(list->GetSize());
126 for (const auto& list_item : *list) { 125 for (const auto& list_item : *list) {
127 const base::DictionaryValue* item_values = nullptr; 126 const base::DictionaryValue* item_values = nullptr;
128 if (!list_item->GetAsDictionary(&item_values)) 127 if (!list_item->GetAsDictionary(&item_values))
129 return false; 128 return false;
130 129
131 scoped_ptr<DisplayPlacement> item(new DisplayPlacement); 130 DisplayPlacement item;
132 if (!UpdateFromDict(item_values, kOffsetKey, &item->offset) || 131 if (!UpdateFromDict(item_values, kOffsetKey, &item.offset) ||
133 !UpdateFromDict(item_values, kPositionKey, &item->position) || 132 !UpdateFromDict(item_values, kPositionKey, &item.position) ||
134 !UpdateFromDict(item_values, kDisplayPlacementDisplayIdKey, 133 !UpdateFromDict(item_values, kDisplayPlacementDisplayIdKey,
135 &item->display_id) || 134 &item.display_id) ||
136 !UpdateFromDict(item_values, kDisplayPlacementParentDisplayIdKey, 135 !UpdateFromDict(item_values, kDisplayPlacementParentDisplayIdKey,
137 &item->parent_display_id)) { 136 &item.parent_display_id)) {
138 return false; 137 return false;
139 } 138 }
140 139
141 output->push_back(std::move(item)); 140 output->push_back(item);
142 } 141 }
143 return true; 142 return true;
144 } 143 }
145 144
146 } // namespace 145 } // namespace
147 146
148 bool JsonToDisplayLayout(const base::Value& value, DisplayLayout* layout) { 147 bool JsonToDisplayLayout(const base::Value& value, DisplayLayout* layout) {
149 layout->placement_list.clear(); 148 layout->placement_list.clear();
150 const base::DictionaryValue* dict_value = nullptr; 149 const base::DictionaryValue* dict_value = nullptr;
151 if (!value.GetAsDictionary(&dict_value)) 150 if (!value.GetAsDictionary(&dict_value))
(...skipping 18 matching lines...) Expand all
170 bool DisplayLayoutToJson(const DisplayLayout& layout, base::Value* value) { 169 bool DisplayLayoutToJson(const DisplayLayout& layout, base::Value* value) {
171 base::DictionaryValue* dict_value = nullptr; 170 base::DictionaryValue* dict_value = nullptr;
172 if (!value->GetAsDictionary(&dict_value)) 171 if (!value->GetAsDictionary(&dict_value))
173 return false; 172 return false;
174 173
175 dict_value->SetBoolean(kMirroredKey, layout.mirrored); 174 dict_value->SetBoolean(kMirroredKey, layout.mirrored);
176 dict_value->SetBoolean(kDefaultUnifiedKey, layout.default_unified); 175 dict_value->SetBoolean(kDefaultUnifiedKey, layout.default_unified);
177 dict_value->SetString(kPrimaryIdKey, base::Int64ToString(layout.primary_id)); 176 dict_value->SetString(kPrimaryIdKey, base::Int64ToString(layout.primary_id));
178 177
179 scoped_ptr<base::ListValue> placement_list(new base::ListValue); 178 scoped_ptr<base::ListValue> placement_list(new base::ListValue);
180 for (const auto* placement : layout.placement_list) { 179 for (const auto& placement : layout.placement_list) {
181 scoped_ptr<base::DictionaryValue> placement_value( 180 scoped_ptr<base::DictionaryValue> placement_value(
182 new base::DictionaryValue); 181 new base::DictionaryValue);
183 placement_value->SetString( 182 placement_value->SetString(
184 kPositionKey, DisplayPlacement::PositionToString(placement->position)); 183 kPositionKey, DisplayPlacement::PositionToString(placement.position));
185 placement_value->SetInteger(kOffsetKey, placement->offset); 184 placement_value->SetInteger(kOffsetKey, placement.offset);
186 placement_value->SetString(kDisplayPlacementDisplayIdKey, 185 placement_value->SetString(kDisplayPlacementDisplayIdKey,
187 base::Int64ToString(placement->display_id)); 186 base::Int64ToString(placement.display_id));
188 placement_value->SetString( 187 placement_value->SetString(
189 kDisplayPlacementParentDisplayIdKey, 188 kDisplayPlacementParentDisplayIdKey,
190 base::Int64ToString(placement->parent_display_id)); 189 base::Int64ToString(placement.parent_display_id));
191 placement_list->Append(std::move(placement_value)); 190 placement_list->Append(std::move(placement_value));
192 } 191 }
193 dict_value->Set(kDisplayPlacementKey, std::move(placement_list)); 192 dict_value->Set(kDisplayPlacementKey, std::move(placement_list));
194 return true; 193 return true;
195 } 194 }
196 195
197 } // namespace ash 196 } // namespace ash
OLDNEW
« no previous file with comments | « ash/display/extended_mouse_warp_controller_unittest.cc ('k') | ash/display/json_converter_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698