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

Side by Side Diff: components/cloud_devices/description_items.h

Issue 150993002: Reading, writing of CDD and CJT JSON formats. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 10 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 | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright 2014 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 #ifndef COMPONENTS_CLOUD_DEVICES_CAPABILITY_INTERFACES_H_
6 #define COMPONENTS_CLOUD_DEVICES_CAPABILITY_INTERFACES_H_
7
8 // Defines common templates that could be used to create device specific
9 // capabilities and print tickets.
10
11 #include <vector>
12
13 #include "base/logging.h"
14
15 #include "components/cloud_devices/cloud_device_description.h"
16
17 namespace base {
18 class DictionaryValue;
19 }
20
21 namespace cloud_devices {
22
23 // Represents CDD capability that stored JSON list.
Noam Samuel 2014/02/03 20:30:34 Nit: Rephrase as "Represents a CDD capability tha
Vitaly Buka (NO REVIEWS) 2014/02/03 22:06:52 Done.
24 // Ex: "capability": [ {<VALUE>}, {<VALUE>}, {<VALUE>} ]
Noam Samuel 2014/02/03 20:30:34 Nit: Since the key is not literally the word capab
Vitaly Buka (NO REVIEWS) 2014/02/03 22:06:52 Done.
25 // Option specifies data type for <VALUE>.
26 // Traits specifies how <VALUE> is stored in JSON and semantical validation.
Noam Samuel 2014/02/03 20:30:34 Nit: Semantical->Semantic
Vitaly Buka (NO REVIEWS) 2014/02/03 22:06:52 Done.
27 template <class Option, class Traits>
Noam Samuel 2014/02/03 20:30:34 Please write in a comment somewhere the interface
Vitaly Buka (NO REVIEWS) 2014/02/03 22:06:52 Done.
28 class ListCapability {
29 public:
30 ListCapability();
31 ~ListCapability();
32
33 bool LoadFrom(const CloudDeviceDescription& description);
34 void SaveTo(CloudDeviceDescription* description) const;
35
36 void Reset() {
37 options_.clear();
38 }
39
40 bool IsValid() const;
41
42 bool empty() const {
43 return options_.empty();
44 }
45
46 size_t size() const {
47 return options_.size();
48 }
49
50 const Option& operator[](size_t i) const {
51 return options_[i];
52 }
53
54 bool Contains(const Option& option) const{
55 return std::find(options_.begin(), options_.end(), option) !=
56 options_.end();
57 }
58
59 void AddOption(const Option& option) {
60 options_.push_back(option);
61 }
62
63 private:
64 typedef std::vector<Option> OptionVector;
65 OptionVector options_;
66
67 DISALLOW_COPY_AND_ASSIGN(ListCapability);
68 };
69
70 // Represents CDD capability stored as JSON list with default_value value.
71 // Ex: "capability": { "option": [{ "is_default": true, <VALUE>}, {<VALUE>} ]}
Noam Samuel 2014/02/03 20:30:34 Ditto on "capability"
Vitaly Buka (NO REVIEWS) 2014/02/03 22:06:52 Done.
72 // Option specifies data type for <VALUE>.
73 // Traits specifies how <VALUE> is stored in JSON and semantical validation.
74 template <class Option, class Traits>
75 class SelectionCapability {
76 public:
77 SelectionCapability();
78 ~SelectionCapability();
79
80 bool LoadFrom(const CloudDeviceDescription& description);
81 void SaveTo(CloudDeviceDescription* description) const;
82
83 void Reset() {
84 options_.clear();
85 default_idx_ = 0;
86 }
87
88 bool IsValid() const;
89
90 bool empty() const {
91 return options_.empty();
92 }
93
94 size_t size() const {
95 return options_.size();
96 }
97
98 const Option& operator[](size_t i) const {
99 return options_[i];
100 }
101
102 bool Contains(const Option& option) const{
103 return std::find(options_.begin(), options_.end(), option) !=
104 options_.end();
105 }
106
107 const Option& GetDefault() const {
108 return options_[default_idx_];
109 }
110
111 void AddOption(const Option& option) {
112 AddDefaultOption(option, false);
113 }
114
115 void AddDefaultOption(const Option& option, bool is_default) {
116 bool no_default = (default_idx_ >= size());
Noam Samuel 2014/02/03 20:30:34 Why not use a signed default_idx_ == -1?
Vitaly Buka (NO REVIEWS) 2014/02/03 22:06:52 Done.
117 options_.push_back(option);
118 if (is_default) {
119 DCHECK(no_default);
120 // Point to the last element.
121 default_idx_ = size() - 1;
122 } else {
123 if (no_default) {
124 // Move default index after the last element.
125 default_idx_ = size();
126 }
127 }
128 }
129
130 private:
131 typedef std::vector<Option> OptionVector;
132
133 OptionVector options_;
134 size_t default_idx_;
135
136 DISALLOW_COPY_AND_ASSIGN(SelectionCapability);
137 };
138
139 // Represents CDD capability that can be true or false.
140 // Ex: "capability": { "default_value": true }
Noam Samuel 2014/02/03 20:30:34 Ditto on "capability"
Vitaly Buka (NO REVIEWS) 2014/02/03 22:06:52 Done.
141 // Traits specifies how <VALUE> is stored in JSON and semantical validation.
142 template <class Traits>
143 class BooleanCapability {
144 public:
145 BooleanCapability();
146 ~BooleanCapability();
147
148 bool LoadFrom(const CloudDeviceDescription& description);
149 void SaveTo(CloudDeviceDescription* description) const;
150
151 void Reset() {
152 default_value_ = false;
153 }
154
155 void set_default_value(bool value) {
156 default_value_ = value;
157 }
158
159 bool default_value() const {
160 return default_value_;
161 }
162
163 private:
164 bool default_value_;
165
166 DISALLOW_COPY_AND_ASSIGN(BooleanCapability);
167 };
168
169 // Represents CDD capability that only existence is important for us.
Noam Samuel 2014/02/03 20:30:34 Nit: that->for which
Vitaly Buka (NO REVIEWS) 2014/02/03 22:06:52 Done.
170 // Ex: "capability": { }
Noam Samuel 2014/02/03 20:30:34 Ditto on "capability"
Vitaly Buka (NO REVIEWS) 2014/02/03 22:06:52 Done.
171 // Traits specifies how <VALUE> is stored in JSON and semantical validation.
172 template <class Traits>
173 class EmptyCapability {
174 public:
175 EmptyCapability() {};
176 ~EmptyCapability() {};
177
178 bool LoadFrom(const CloudDeviceDescription& description);
179 void SaveTo(CloudDeviceDescription* description) const;
180
181 private:
182 DISALLOW_COPY_AND_ASSIGN(EmptyCapability);
183 };
184
185 // Represents CJT items.
186 // Ex: "capability": {<VALUE>}
Noam Samuel 2014/02/03 20:30:34 Ditto on "capability"
Vitaly Buka (NO REVIEWS) 2014/02/03 22:06:52 Done.
187 // Option specifies data type for <VALUE>.
188 // Traits specifies how <VALUE> is stored in JSON and semantical validation.
189 template <class Option, class Traits>
190 class TicketItem {
191 public:
192 TicketItem();
193 ~TicketItem();
194
195 bool LoadFrom(const CloudDeviceDescription& description);
196 void SaveTo(CloudDeviceDescription* description) const;
197
198 void Reset() {
199 value_ = Option();
200 }
201
202 bool IsValid() const;
203
204 const Option& value() const {
205 return value_;
206 }
207
208 void set_value(const Option& value) {
209 value_ = value;
210 }
211
212 private:
213 Option value_;
214
215 DISALLOW_COPY_AND_ASSIGN(TicketItem);
216 };
217
218 } // namespace cloud_devices
219
220 #endif // COMPONENTS_CLOUD_DEVICES_CAPABILITY_INTERFACES_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698