Roll src/third_party/WebKit 9f7fb92:f103b33 (svn 202621:202622)
[chromium-blink-merge.git] / components / cloud_devices / common / description_items.h
blob1e8ed53ff78a97ed89ab89e11b1f8d2933e152e2
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_COMMON_CAPABILITY_INTERFACES_H_
6 #define COMPONENTS_CLOUD_DEVICES_COMMON_CAPABILITY_INTERFACES_H_
8 // Defines common templates that could be used to create device specific
9 // capabilities and print tickets.
11 #include <vector>
13 #include "base/logging.h"
14 #include "base/numerics/safe_conversions.h"
15 #include "components/cloud_devices/common/cloud_device_description.h"
17 namespace base {
18 class DictionaryValue;
21 namespace cloud_devices {
23 // All traits below specify how to serialize and validate capabilities and
24 // ticket items.
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 {
44 public:
45 ListCapability();
46 ~ListCapability();
48 bool LoadFrom(const CloudDeviceDescription& description);
49 void SaveTo(CloudDeviceDescription* description) const;
51 void Reset() { options_.clear(); }
53 bool IsValid() const;
55 bool empty() const { return options_.empty(); }
57 size_t size() const { return options_.size(); }
59 const Option& operator[](size_t i) const { return options_[i]; }
61 bool Contains(const Option& option) const {
62 return std::find(options_.begin(), options_.end(), option) !=
63 options_.end();
66 void AddOption(const Option& option) { options_.push_back(option); }
68 private:
69 typedef std::vector<Option> OptionVector;
70 OptionVector options_;
72 DISALLOW_COPY_AND_ASSIGN(ListCapability);
75 // Represents CDD capability stored as JSON list with default_value value.
76 // Ex: "<CAPABILITY_NAME>": { "option": [{ "is_default": true, <VALUE>},
77 // {<VALUE>} ]}
78 // Option specifies data type for <VALUE>.
79 // Traits specifies how <VALUE> is stored in JSON and semantic validation.
80 template <class Option, class Traits>
81 class SelectionCapability {
82 public:
83 SelectionCapability();
84 ~SelectionCapability();
86 bool LoadFrom(const CloudDeviceDescription& description);
87 void SaveTo(CloudDeviceDescription* description) const;
89 void Reset() {
90 options_.clear();
91 default_idx_ = -1;
94 bool IsValid() const;
96 bool empty() const { return options_.empty(); }
98 size_t size() const { return options_.size(); }
100 const Option& operator[](size_t i) const { return options_[i]; }
102 bool Contains(const Option& option) const {
103 return std::find(options_.begin(), options_.end(), option) !=
104 options_.end();
107 const Option& GetDefault() const {
108 CHECK_GE(default_idx_, 0);
109 return options_[default_idx_];
112 void AddOption(const Option& option) { AddDefaultOption(option, false); }
114 void AddDefaultOption(const Option& option, bool is_default) {
115 if (is_default) {
116 DCHECK_EQ(default_idx_, -1);
117 // Point to the last element.
118 default_idx_ = base::checked_cast<int>(size());
120 options_.push_back(option);
123 private:
124 typedef std::vector<Option> OptionVector;
126 OptionVector options_;
127 int default_idx_;
129 DISALLOW_COPY_AND_ASSIGN(SelectionCapability);
132 // Represents CDD capability that can be true or false.
133 // Ex: "<CAPABILITY_NAME>": { "default_value": true }
134 // Traits specifies how <VALUE> is stored in JSON and semantic validation.
135 template <class Traits>
136 class BooleanCapability {
137 public:
138 BooleanCapability();
139 ~BooleanCapability();
141 bool LoadFrom(const CloudDeviceDescription& description);
142 void SaveTo(CloudDeviceDescription* description) const;
144 void Reset() { default_value_ = false; }
146 void set_default_value(bool value) { default_value_ = value; }
148 bool default_value() const { return default_value_; }
150 private:
151 bool default_value_;
153 DISALLOW_COPY_AND_ASSIGN(BooleanCapability);
156 // Represents CDD capability for which existence is only important.
157 // Ex: "<CAPABILITY_NAME>": { }
158 // Traits specifies how <VALUE> is stored in JSON and semantic validation.
159 template <class Traits>
160 class EmptyCapability {
161 public:
162 EmptyCapability() {};
163 ~EmptyCapability() {};
165 bool LoadFrom(const CloudDeviceDescription& description);
166 void SaveTo(CloudDeviceDescription* description) const;
168 private:
169 DISALLOW_COPY_AND_ASSIGN(EmptyCapability);
172 // Represents an item that is of a specific value type.
173 // Ex: "<CAPABILITY_NAME>": {<VALUE>}
174 // Option specifies data type for <VALUE>.
175 // Traits specifies how <VALUE> is stored in JSON and semantic validation.
176 template <class Option, class Traits>
177 class ValueCapability {
178 public:
179 ValueCapability();
180 ~ValueCapability();
182 bool LoadFrom(const CloudDeviceDescription& description);
183 void SaveTo(CloudDeviceDescription* description) const;
185 void Reset() { value_ = Option(); }
187 bool IsValid() const;
189 const Option& value() const { return value_; }
191 void set_value(const Option& value) { value_ = value; }
193 private:
194 Option value_;
196 DISALLOW_COPY_AND_ASSIGN(ValueCapability);
199 // Represents CJT items.
200 // Ex: "<CAPABILITY_NAME>": {<VALUE>}
201 // Option specifies data type for <VALUE>.
202 // Traits specifies how <VALUE> is stored in JSON and semantic validation.
203 template <class Option, class Traits>
204 class TicketItem {
205 public:
206 TicketItem();
207 ~TicketItem();
209 bool LoadFrom(const CloudDeviceDescription& description);
210 void SaveTo(CloudDeviceDescription* description) const;
212 void Reset() { value_ = Option(); }
214 bool IsValid() const;
216 const Option& value() const { return value_; }
218 void set_value(const Option& value) { value_ = value; }
220 private:
221 Option value_;
223 DISALLOW_COPY_AND_ASSIGN(TicketItem);
226 } // namespace cloud_devices
228 #endif // COMPONENTS_CLOUD_DEVICES_COMMON_CAPABILITY_INTERFACES_H_