Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / components / cloud_devices / description_items.h
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 // 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&);
28 //
29 //   // Returns json path relative to the root of CDD/CJT.
30 //   static std::string GetItemPath();
31 //
32 //   // Loads ticket item. Returns false if failed.
33 //   static bool Load(const base::DictionaryValue& dict, ContentType* option);
34 //
35 //   // Saves ticket item.
36 //   static void Save(ContentType option, base::DictionaryValue* dict);
37
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();
47
48   bool LoadFrom(const CloudDeviceDescription& description);
49   void SaveTo(CloudDeviceDescription* description) const;
50
51   void Reset() {
52     options_.clear();
53   }
54
55   bool IsValid() const;
56
57   bool empty() const {
58     return options_.empty();
59   }
60
61   size_t size() const {
62     return options_.size();
63   }
64
65   const Option& operator[](size_t i) const {
66     return options_[i];
67   }
68
69   bool Contains(const Option& option) const{
70     return std::find(options_.begin(), options_.end(), option) !=
71            options_.end();
72   }
73
74   void AddOption(const Option& option) {
75     options_.push_back(option);
76   }
77
78  private:
79   typedef std::vector<Option> OptionVector;
80   OptionVector options_;
81
82   DISALLOW_COPY_AND_ASSIGN(ListCapability);
83 };
84
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();
95
96   bool LoadFrom(const CloudDeviceDescription& description);
97   void SaveTo(CloudDeviceDescription* description) const;
98
99   void Reset() {
100     options_.clear();
101     default_idx_ = -1;
102   }
103
104   bool IsValid() const;
105
106   bool empty() const {
107     return options_.empty();
108   }
109
110   size_t size() const {
111     return options_.size();
112   }
113
114   const Option& operator[](size_t i) const {
115     return options_[i];
116   }
117
118   bool Contains(const Option& option) const{
119     return std::find(options_.begin(), options_.end(), option) !=
120            options_.end();
121   }
122
123   const Option& GetDefault() const {
124     CHECK_GE(default_idx_, 0);
125     return options_[default_idx_];
126   }
127
128   void AddOption(const Option& option) {
129     AddDefaultOption(option, false);
130   }
131
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_ = static_cast<int>(size());
137     }
138     options_.push_back(option);
139   }
140
141  private:
142   typedef std::vector<Option> OptionVector;
143
144   OptionVector options_;
145   int default_idx_;
146
147   DISALLOW_COPY_AND_ASSIGN(SelectionCapability);
148 };
149
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();
158
159   bool LoadFrom(const CloudDeviceDescription& description);
160   void SaveTo(CloudDeviceDescription* description) const;
161
162   void Reset() {
163     default_value_ = false;
164   }
165
166   void set_default_value(bool value) {
167     default_value_ = value;
168   }
169
170   bool default_value() const {
171     return default_value_;
172   }
173
174  private:
175   bool default_value_;
176
177   DISALLOW_COPY_AND_ASSIGN(BooleanCapability);
178 };
179
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() {};
188
189   bool LoadFrom(const CloudDeviceDescription& description);
190   void SaveTo(CloudDeviceDescription* description) const;
191
192  private:
193   DISALLOW_COPY_AND_ASSIGN(EmptyCapability);
194 };
195
196 // Represents CJT items.
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 TicketItem {
202  public:
203   TicketItem();
204   ~TicketItem();
205
206   bool LoadFrom(const CloudDeviceDescription& description);
207   void SaveTo(CloudDeviceDescription* description) const;
208
209   void Reset() {
210     value_ = Option();
211   }
212
213   bool IsValid() const;
214
215   const Option& value() const {
216     return value_;
217   }
218
219   void set_value(const Option& value) {
220     value_ = value;
221   }
222
223  private:
224   Option value_;
225
226   DISALLOW_COPY_AND_ASSIGN(TicketItem);
227 };
228
229 }  // namespace cloud_devices
230
231 #endif  // COMPONENTS_CLOUD_DEVICES_CAPABILITY_INTERFACES_H_