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_DESCRIPTION_DESCRIPTION_ITEMS_INL_H_
6 #define COMPONENTS_CLOUD_DEVICES_COMMON_DESCRIPTION_DESCRIPTION_ITEMS_INL_H_
10 #include "base/numerics/safe_conversions.h"
11 #include "components/cloud_devices/common/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() {
28 template <class Option
, class Traits
>
29 bool ListCapability
<Option
, Traits
>::IsValid() const {
31 return false; // This type of capabilities can't be empty.
32 for (size_t i
= 0; i
< options_
.size(); ++i
) {
33 if (!Traits::IsValid(options_
[i
]))
39 template <class Option
, class Traits
>
40 bool ListCapability
<Option
, Traits
>::LoadFrom(
41 const CloudDeviceDescription
& description
) {
43 const base::ListValue
* options
=
44 description
.GetListItem(Traits::GetCapabilityPath());
47 for (size_t i
= 0; i
< options
->GetSize(); ++i
) {
48 const base::DictionaryValue
* option_value
= NULL
;
49 if (!options
->GetDictionary(i
, &option_value
))
50 return false; // Every entry must be a dictionary.
52 if (!Traits::Load(*option_value
, &option
))
59 template <class Option
, class Traits
>
60 void ListCapability
<Option
, Traits
>::SaveTo(
61 CloudDeviceDescription
* description
) const {
63 base::ListValue
* options_list
=
64 description
->CreateListItem(Traits::GetCapabilityPath());
65 for (size_t i
= 0; i
< options_
.size(); ++i
) {
66 base::DictionaryValue
* option_value
= new base::DictionaryValue
;
67 options_list
->Append(option_value
);
68 Traits::Save(options_
[i
], option_value
);
72 template <class Option
, class Traits
>
73 SelectionCapability
<Option
, Traits
>::SelectionCapability() {
77 template <class Option
, class Traits
>
78 SelectionCapability
<Option
, Traits
>::~SelectionCapability() {
81 template <class Option
, class Traits
>
82 bool SelectionCapability
<Option
, Traits
>::IsValid() const {
84 return false; // This type of capabilities can't be empty
85 for (size_t i
= 0; i
< options_
.size(); ++i
) {
86 if (!Traits::IsValid(options_
[i
]))
89 return default_idx_
>= 0 && default_idx_
< base::checked_cast
<int>(size());
92 template <class Option
, class Traits
>
93 bool SelectionCapability
<Option
, Traits
>::LoadFrom(
94 const CloudDeviceDescription
& description
) {
96 const base::DictionaryValue
* item
=
97 description
.GetItem(Traits::GetCapabilityPath());
100 const base::ListValue
* options
= NULL
;
101 if (!item
->GetList(json::kKeyOption
, &options
))
103 for (size_t i
= 0; i
< options
->GetSize(); ++i
) {
104 const base::DictionaryValue
* option_value
= NULL
;
105 if (!options
->GetDictionary(i
, &option_value
))
106 return false; // Every entry must be a dictionary.
108 if (!Traits::Load(*option_value
, &option
))
110 bool is_default
= false;
111 option_value
->GetBoolean(json::kKeyIsDefault
, &is_default
);
112 if (is_default
&& default_idx_
>= 0) {
113 return false; // Multiple defaults.
115 AddDefaultOption(option
, is_default
);
120 template <class Option
, class Traits
>
121 void SelectionCapability
<Option
, Traits
>::SaveTo(
122 CloudDeviceDescription
* description
) const {
124 base::ListValue
* options_list
= new base::ListValue
;
125 description
->CreateItem(Traits::GetCapabilityPath())
126 ->Set(json::kKeyOption
, options_list
);
127 for (size_t i
= 0; i
< options_
.size(); ++i
) {
128 base::DictionaryValue
* option_value
= new base::DictionaryValue
;
129 options_list
->Append(option_value
);
130 if (base::checked_cast
<int>(i
) == default_idx_
)
131 option_value
->SetBoolean(json::kKeyIsDefault
, true);
132 Traits::Save(options_
[i
], option_value
);
136 template <class Traits
>
137 BooleanCapability
<Traits
>::BooleanCapability() {
141 template <class Traits
>
142 BooleanCapability
<Traits
>::~BooleanCapability() {
145 template <class Traits
>
146 bool BooleanCapability
<Traits
>::LoadFrom(
147 const CloudDeviceDescription
& description
) {
149 const base::DictionaryValue
* dict
=
150 description
.GetItem(Traits::GetCapabilityPath());
153 default_value_
= Traits::kDefault
;
154 dict
->GetBoolean(json::kKeyDefault
, &default_value_
);
158 template <class Traits
>
159 void BooleanCapability
<Traits
>::SaveTo(
160 CloudDeviceDescription
* description
) const {
161 base::DictionaryValue
* dict
=
162 description
->CreateItem(Traits::GetCapabilityPath());
163 if (default_value_
!= Traits::kDefault
)
164 dict
->SetBoolean(json::kKeyDefault
, default_value_
);
167 template <class Traits
>
168 bool EmptyCapability
<Traits
>::LoadFrom(
169 const CloudDeviceDescription
& description
) {
170 return description
.GetItem(Traits::GetCapabilityPath()) != NULL
;
173 template <class Traits
>
174 void EmptyCapability
<Traits
>::SaveTo(
175 CloudDeviceDescription
* description
) const {
176 description
->CreateItem(Traits::GetCapabilityPath());
179 template <class Option
, class Traits
>
180 ValueCapability
<Option
, Traits
>::ValueCapability() {
184 template <class Option
, class Traits
>
185 ValueCapability
<Option
, Traits
>::~ValueCapability() {
188 template <class Option
, class Traits
>
189 bool ValueCapability
<Option
, Traits
>::IsValid() const {
190 return Traits::IsValid(value());
193 template <class Option
, class Traits
>
194 bool ValueCapability
<Option
, Traits
>::LoadFrom(
195 const CloudDeviceDescription
& description
) {
197 const base::DictionaryValue
* option_value
=
198 description
.GetItem(Traits::GetCapabilityPath());
202 if (!Traits::Load(*option_value
, &option
))
208 template <class Option
, class Traits
>
209 void ValueCapability
<Option
, Traits
>::SaveTo(
210 CloudDeviceDescription
* description
) const {
212 Traits::Save(value(), description
->CreateItem(Traits::GetCapabilityPath()));
215 template <class Option
, class Traits
>
216 TicketItem
<Option
, Traits
>::TicketItem() {
220 template <class Option
, class Traits
>
221 TicketItem
<Option
, Traits
>::~TicketItem() {
224 template <class Option
, class Traits
>
225 bool TicketItem
<Option
, Traits
>::IsValid() const {
226 return Traits::IsValid(value());
229 template <class Option
, class Traits
>
230 bool TicketItem
<Option
, Traits
>::LoadFrom(
231 const CloudDeviceDescription
& description
) {
233 const base::DictionaryValue
* option_value
=
234 description
.GetItem(Traits::GetTicketItemPath());
238 if (!Traits::Load(*option_value
, &option
))
244 template <class Option
, class Traits
>
245 void TicketItem
<Option
, Traits
>::SaveTo(
246 CloudDeviceDescription
* description
) const {
248 Traits::Save(value(), description
->CreateItem(Traits::GetTicketItemPath()));
251 } // namespace cloud_devices
253 #endif // COMPONENTS_CLOUD_DEVICES_COMMON_DESCRIPTION_DESCRIPTION_ITEMS_INL_H_