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_DESCRIPTION_DESCRIPTION_ITEMS_INL_H_
6 #define COMPONENTS_CLOUD_DEVICES_DESCRIPTION_DESCRIPTION_ITEMS_INL_H_
10 #include "base/numerics/safe_conversions.h"
11 #include "components/cloud_devices/description_items.h"
13 // Implementation of templates defined in header file.
14 // This file should be included from CC file with implementation of device
15 // specific capabilities.
17 namespace cloud_devices
{
19 template <class Option
, class Traits
>
20 ListCapability
<Option
, Traits
>::ListCapability() {
24 template <class Option
, class Traits
>
25 ListCapability
<Option
, Traits
>::~ListCapability() { }
27 template <class Option
, class Traits
>
28 bool ListCapability
<Option
, Traits
>::IsValid() const {
30 return false; // This type of capabilities can't be empty.
31 for (size_t i
= 0; i
< options_
.size(); ++i
) {
32 if (!Traits::IsValid(options_
[i
]))
38 template <class Option
, class Traits
>
39 bool ListCapability
<Option
, Traits
>::LoadFrom(
40 const CloudDeviceDescription
& description
) {
42 const base::ListValue
* options
=
43 description
.GetListItem(Traits::GetCapabilityPath());
46 for (size_t i
= 0; i
< options
->GetSize(); ++i
) {
47 const base::DictionaryValue
* option_value
= NULL
;
48 if (!options
->GetDictionary(i
, &option_value
))
49 return false; // Every entry must be a dictionary.
51 if (!Traits::Load(*option_value
, &option
))
58 template <class Option
, class Traits
>
59 void ListCapability
<Option
, Traits
>::SaveTo(
60 CloudDeviceDescription
* description
) const {
62 base::ListValue
* options_list
=
63 description
->CreateListItem(Traits::GetCapabilityPath());
64 for (size_t i
= 0; i
< options_
.size(); ++i
) {
65 base::DictionaryValue
* option_value
= new base::DictionaryValue
;
66 options_list
->Append(option_value
);
67 Traits::Save(options_
[i
], option_value
);
71 template <class Option
, class Traits
>
72 SelectionCapability
<Option
, Traits
>::SelectionCapability() {
76 template <class Option
, class Traits
>
77 SelectionCapability
<Option
, Traits
>::~SelectionCapability() { }
79 template <class Option
, class Traits
>
80 bool SelectionCapability
<Option
, Traits
>::IsValid() const {
82 return false; // This type of capabilities can't be empty
83 for (size_t i
= 0; i
< options_
.size(); ++i
) {
84 if (!Traits::IsValid(options_
[i
]))
87 return default_idx_
>= 0 && default_idx_
< base::checked_cast
<int>(size());
90 template <class Option
, class Traits
>
91 bool SelectionCapability
<Option
, Traits
>::LoadFrom(
92 const CloudDeviceDescription
& description
) {
94 const base::DictionaryValue
* item
=
95 description
.GetItem(Traits::GetCapabilityPath());
98 const base::ListValue
* options
= NULL
;
99 if (!item
->GetList(json::kKeyOption
, &options
))
101 for (size_t i
= 0; i
< options
->GetSize(); ++i
) {
102 const base::DictionaryValue
* option_value
= NULL
;
103 if (!options
->GetDictionary(i
, &option_value
))
104 return false; // Every entry must be a dictionary.
106 if (!Traits::Load(*option_value
, &option
))
108 bool is_default
= false;
109 option_value
->GetBoolean(json::kKeyIsDefault
, &is_default
);
110 if (is_default
&& default_idx_
>= 0) {
111 return false; // Multiple defaults.
113 AddDefaultOption(option
, is_default
);
118 template <class Option
, class Traits
>
119 void SelectionCapability
<Option
, Traits
>::SaveTo(
120 CloudDeviceDescription
* description
) const {
122 base::ListValue
* options_list
= new base::ListValue
;
123 description
->CreateItem(Traits::GetCapabilityPath())->Set(json::kKeyOption
,
125 for (size_t i
= 0; i
< options_
.size(); ++i
) {
126 base::DictionaryValue
* option_value
= new base::DictionaryValue
;
127 options_list
->Append(option_value
);
128 if (base::checked_cast
<int>(i
) == default_idx_
)
129 option_value
->SetBoolean(json::kKeyIsDefault
, true);
130 Traits::Save(options_
[i
], option_value
);
134 template <class Traits
>
135 BooleanCapability
<Traits
>::BooleanCapability() {
139 template <class Traits
>
140 BooleanCapability
<Traits
>::~BooleanCapability() { }
142 template <class Traits
>
143 bool BooleanCapability
<Traits
>::LoadFrom(
144 const CloudDeviceDescription
& description
) {
146 const base::DictionaryValue
* dict
=
147 description
.GetItem(Traits::GetCapabilityPath());
150 default_value_
= Traits::kDefault
;
151 dict
->GetBoolean(json::kKeyDefault
, &default_value_
);
155 template <class Traits
>
156 void BooleanCapability
<Traits
>::SaveTo(
157 CloudDeviceDescription
* description
) const {
158 base::DictionaryValue
* dict
=
159 description
->CreateItem(Traits::GetCapabilityPath());
160 if (default_value_
!= Traits::kDefault
)
161 dict
->SetBoolean(json::kKeyDefault
, default_value_
);
164 template <class Traits
>
165 bool EmptyCapability
<Traits
>::LoadFrom(
166 const CloudDeviceDescription
& description
) {
167 return description
.GetItem(Traits::GetCapabilityPath()) != NULL
;
170 template <class Traits
>
171 void EmptyCapability
<Traits
>::SaveTo(
172 CloudDeviceDescription
* description
) const {
173 description
->CreateItem(Traits::GetCapabilityPath());
176 template <class Option
, class Traits
>
177 ValueCapability
<Option
, Traits
>::ValueCapability() {
181 template <class Option
, class Traits
>
182 ValueCapability
<Option
, Traits
>::~ValueCapability() {}
184 template <class Option
, class Traits
>
185 bool ValueCapability
<Option
, Traits
>::IsValid() const {
186 return Traits::IsValid(value());
189 template <class Option
, class Traits
>
190 bool ValueCapability
<Option
, Traits
>::LoadFrom(
191 const CloudDeviceDescription
& description
) {
193 const base::DictionaryValue
* option_value
=
194 description
.GetItem(Traits::GetCapabilityPath());
198 if (!Traits::Load(*option_value
, &option
))
204 template <class Option
, class Traits
>
205 void ValueCapability
<Option
, Traits
>::SaveTo(
206 CloudDeviceDescription
* description
) const {
208 Traits::Save(value(), description
->CreateItem(Traits::GetCapabilityPath()));
211 template <class Option
, class Traits
>
212 TicketItem
<Option
, Traits
>::TicketItem() {
216 template <class Option
, class Traits
>
217 TicketItem
<Option
, Traits
>::~TicketItem() { }
219 template <class Option
, class Traits
>
220 bool TicketItem
<Option
, Traits
>::IsValid() const {
221 return Traits::IsValid(value());
224 template <class Option
, class Traits
>
225 bool TicketItem
<Option
, Traits
>::LoadFrom(
226 const CloudDeviceDescription
& description
) {
228 const base::DictionaryValue
* option_value
=
229 description
.GetItem(Traits::GetTicketItemPath());
233 if (!Traits::Load(*option_value
, &option
))
239 template <class Option
, class Traits
>
240 void TicketItem
<Option
, Traits
>::SaveTo(
241 CloudDeviceDescription
* description
) const {
243 Traits::Save(value(), description
->CreateItem(Traits::GetTicketItemPath()));
246 } // namespace cloud_devices
248 #endif // COMPONENTS_CLOUD_DEVICES_DESCRIPTION_DESCRIPTION_ITEMS_INL_H_