Fix infinite recursion on hiding panel when created during fullscreen mode.
[chromium-blink-merge.git] / components / cloud_devices / description_items.h
blob383aa559064954a23c171dd60fcd9674f4ba6299
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.
11 #include <vector>
13 #include "base/logging.h"
14 #include "base/numerics/safe_conversions.h"
15 #include "components/cloud_devices/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() {
52 options_.clear();
55 bool IsValid() const;
57 bool empty() const {
58 return options_.empty();
61 size_t size() const {
62 return options_.size();
65 const Option& operator[](size_t i) const {
66 return options_[i];
69 bool Contains(const Option& option) const{
70 return std::find(options_.begin(), options_.end(), option) !=
71 options_.end();
74 void AddOption(const Option& option) {
75 options_.push_back(option);
78 private:
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>},
87 // {<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 {
92 public:
93 SelectionCapability();
94 ~SelectionCapability();
96 bool LoadFrom(const CloudDeviceDescription& description);
97 void SaveTo(CloudDeviceDescription* description) const;
99 void Reset() {
100 options_.clear();
101 default_idx_ = -1;
104 bool IsValid() const;
106 bool empty() const {
107 return options_.empty();
110 size_t size() const {
111 return options_.size();
114 const Option& operator[](size_t i) const {
115 return options_[i];
118 bool Contains(const Option& option) const{
119 return std::find(options_.begin(), options_.end(), option) !=
120 options_.end();
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) {
133 if (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);
141 private:
142 typedef std::vector<Option> OptionVector;
144 OptionVector options_;
145 int default_idx_;
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 {
155 public:
156 BooleanCapability();
157 ~BooleanCapability();
159 bool LoadFrom(const CloudDeviceDescription& description);
160 void SaveTo(CloudDeviceDescription* description) const;
162 void Reset() {
163 default_value_ = false;
166 void set_default_value(bool value) {
167 default_value_ = value;
170 bool default_value() const {
171 return default_value_;
174 private:
175 bool 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 {
185 public:
186 EmptyCapability() {};
187 ~EmptyCapability() {};
189 bool LoadFrom(const CloudDeviceDescription& description);
190 void SaveTo(CloudDeviceDescription* description) const;
192 private:
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 {
202 public:
203 ValueCapability();
204 ~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; }
217 private:
218 Option 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>
228 class TicketItem {
229 public:
230 TicketItem();
231 ~TicketItem();
233 bool LoadFrom(const CloudDeviceDescription& description);
234 void SaveTo(CloudDeviceDescription* description) const;
236 void Reset() {
237 value_ = Option();
240 bool IsValid() const;
242 const Option& value() const {
243 return value_;
246 void set_value(const Option& value) {
247 value_ = value;
250 private:
251 Option value_;
253 DISALLOW_COPY_AND_ASSIGN(TicketItem);
256 } // namespace cloud_devices
258 #endif // COMPONENTS_CLOUD_DEVICES_CAPABILITY_INTERFACES_H_