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.
5 #ifndef COMPONENTS_CLOUD_DEVICES_CAPABILITY_INTERFACES_H_
6 #define COMPONENTS_CLOUD_DEVICES_CAPABILITY_INTERFACES_H_
8 // Defines common templates that could be used to create device specific
9 // capabilities and print tickets.
13 #include "base/logging.h"
14 #include "base/numerics/safe_conversions.h"
15 #include "components/cloud_devices/cloud_device_description.h"
18 class DictionaryValue
;
21 namespace cloud_devices
{
23 // All traits below specify how to serialize and validate capabilities and
25 // Traits should have following methods:
26 // // Returns true if capability semantically valid.
27 // static bool IsValid(const Option&);
29 // // Returns json path relative to the root of CDD/CJT.
30 // static std::string GetItemPath();
32 // // Loads ticket item. Returns false if failed.
33 // static bool Load(const base::DictionaryValue& dict, ContentType* option);
35 // // Saves ticket item.
36 // static void Save(ContentType option, base::DictionaryValue* dict);
38 // Represents a CDD capability that is stored as a JSON list
39 // Ex: "<CAPABILITY_NAME>": [ {<VALUE>}, {<VALUE>}, {<VALUE>} ]
40 // Option specifies data type for <VALUE>.
41 // Traits specifies how <VALUE> is stored in JSON and semantic validation.
42 template <class Option
, class Traits
>
43 class ListCapability
{
48 bool LoadFrom(const CloudDeviceDescription
& description
);
49 void SaveTo(CloudDeviceDescription
* description
) const;
58 return options_
.empty();
62 return options_
.size();
65 const Option
& operator[](size_t i
) const {
69 bool Contains(const Option
& option
) const{
70 return std::find(options_
.begin(), options_
.end(), option
) !=
74 void AddOption(const Option
& option
) {
75 options_
.push_back(option
);
79 typedef std::vector
<Option
> OptionVector
;
80 OptionVector options_
;
82 DISALLOW_COPY_AND_ASSIGN(ListCapability
);
85 // Represents CDD capability stored as JSON list with default_value value.
86 // Ex: "<CAPABILITY_NAME>": { "option": [{ "is_default": true, <VALUE>},
88 // Option specifies data type for <VALUE>.
89 // Traits specifies how <VALUE> is stored in JSON and semantic validation.
90 template <class Option
, class Traits
>
91 class SelectionCapability
{
93 SelectionCapability();
94 ~SelectionCapability();
96 bool LoadFrom(const CloudDeviceDescription
& description
);
97 void SaveTo(CloudDeviceDescription
* description
) const;
104 bool IsValid() const;
107 return options_
.empty();
110 size_t size() const {
111 return options_
.size();
114 const Option
& operator[](size_t i
) const {
118 bool Contains(const Option
& option
) const{
119 return std::find(options_
.begin(), options_
.end(), option
) !=
123 const Option
& GetDefault() const {
124 CHECK_GE(default_idx_
, 0);
125 return options_
[default_idx_
];
128 void AddOption(const Option
& option
) {
129 AddDefaultOption(option
, false);
132 void AddDefaultOption(const Option
& option
, bool is_default
) {
134 DCHECK_EQ(default_idx_
, -1);
135 // Point to the last element.
136 default_idx_
= base::checked_cast
<int>(size());
138 options_
.push_back(option
);
142 typedef std::vector
<Option
> OptionVector
;
144 OptionVector options_
;
147 DISALLOW_COPY_AND_ASSIGN(SelectionCapability
);
150 // Represents CDD capability that can be true or false.
151 // Ex: "<CAPABILITY_NAME>": { "default_value": true }
152 // Traits specifies how <VALUE> is stored in JSON and semantic validation.
153 template <class Traits
>
154 class BooleanCapability
{
157 ~BooleanCapability();
159 bool LoadFrom(const CloudDeviceDescription
& description
);
160 void SaveTo(CloudDeviceDescription
* description
) const;
163 default_value_
= false;
166 void set_default_value(bool value
) {
167 default_value_
= value
;
170 bool default_value() const {
171 return default_value_
;
177 DISALLOW_COPY_AND_ASSIGN(BooleanCapability
);
180 // Represents CDD capability for which existence is only important.
181 // Ex: "<CAPABILITY_NAME>": { }
182 // Traits specifies how <VALUE> is stored in JSON and semantic validation.
183 template <class Traits
>
184 class EmptyCapability
{
186 EmptyCapability() {};
187 ~EmptyCapability() {};
189 bool LoadFrom(const CloudDeviceDescription
& description
);
190 void SaveTo(CloudDeviceDescription
* description
) const;
193 DISALLOW_COPY_AND_ASSIGN(EmptyCapability
);
196 // Represents an item that is of a specific value type.
197 // Ex: "<CAPABILITY_NAME>": {<VALUE>}
198 // Option specifies data type for <VALUE>.
199 // Traits specifies how <VALUE> is stored in JSON and semantic validation.
200 template <class Option
, class Traits
>
201 class ValueCapability
{
206 bool LoadFrom(const CloudDeviceDescription
& description
);
207 void SaveTo(CloudDeviceDescription
* description
) const;
209 void Reset() { value_
= Option(); }
211 bool IsValid() const;
213 const Option
& value() const { return value_
; }
215 void set_value(const Option
& value
) { value_
= value
; }
220 DISALLOW_COPY_AND_ASSIGN(ValueCapability
);
223 // Represents CJT items.
224 // Ex: "<CAPABILITY_NAME>": {<VALUE>}
225 // Option specifies data type for <VALUE>.
226 // Traits specifies how <VALUE> is stored in JSON and semantic validation.
227 template <class Option
, class Traits
>
233 bool LoadFrom(const CloudDeviceDescription
& description
);
234 void SaveTo(CloudDeviceDescription
* description
) const;
240 bool IsValid() const;
242 const Option
& value() const {
246 void set_value(const Option
& value
) {
253 DISALLOW_COPY_AND_ASSIGN(TicketItem
);
256 } // namespace cloud_devices
258 #endif // COMPONENTS_CLOUD_DEVICES_CAPABILITY_INTERFACES_H_