Add a function to create a bookmark app from a WebApplicationInfo.
[chromium-blink-merge.git] / components / cloud_devices / description_items_inl.h
blob625489063b37e8f6f56bba3be3e26a29ba312dba
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_
8 #include <vector>
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() {
21 Reset();
24 template <class Option, class Traits>
25 ListCapability<Option, Traits>::~ListCapability() { }
27 template <class Option, class Traits>
28 bool ListCapability<Option, Traits>::IsValid() const {
29 if (empty())
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]))
33 return false;
35 return true;
38 template <class Option, class Traits>
39 bool ListCapability<Option, Traits>::LoadFrom(
40 const CloudDeviceDescription& description) {
41 Reset();
42 const base::ListValue* options =
43 description.GetListItem(Traits::GetCapabilityPath());
44 if (!options)
45 return false;
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.
50 Option option;
51 if (!Traits::Load(*option_value, &option))
52 return false;
53 AddOption(option);
55 return IsValid();
58 template <class Option, class Traits>
59 void ListCapability<Option, Traits>::SaveTo(
60 CloudDeviceDescription* description) const {
61 DCHECK(IsValid());
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() {
73 Reset();
76 template <class Option, class Traits>
77 SelectionCapability<Option, Traits>::~SelectionCapability() { }
79 template <class Option, class Traits>
80 bool SelectionCapability<Option, Traits>::IsValid() const {
81 if (empty())
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]))
85 return false;
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) {
93 Reset();
94 const base::DictionaryValue* item =
95 description.GetItem(Traits::GetCapabilityPath());
96 if (!item)
97 return false;
98 const base::ListValue* options = NULL;
99 if (!item->GetList(json::kKeyOption, &options))
100 return false;
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.
105 Option option;
106 if (!Traits::Load(*option_value, &option))
107 return false;
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);
115 return IsValid();
118 template <class Option, class Traits>
119 void SelectionCapability<Option, Traits>::SaveTo(
120 CloudDeviceDescription* description) const {
121 DCHECK(IsValid());
122 base::ListValue* options_list = new base::ListValue;
123 description->CreateItem(Traits::GetCapabilityPath())->Set(json::kKeyOption,
124 options_list);
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() {
136 Reset();
139 template <class Traits>
140 BooleanCapability<Traits>::~BooleanCapability() { }
142 template <class Traits>
143 bool BooleanCapability<Traits>::LoadFrom(
144 const CloudDeviceDescription& description) {
145 Reset();
146 const base::DictionaryValue* dict =
147 description.GetItem(Traits::GetCapabilityPath());
148 if (!dict)
149 return false;
150 default_value_ = Traits::kDefault;
151 dict->GetBoolean(json::kKeyDefault, &default_value_);
152 return true;
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() {
178 Reset();
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) {
192 Reset();
193 const base::DictionaryValue* option_value =
194 description.GetItem(Traits::GetCapabilityPath());
195 if (!option_value)
196 return false;
197 Option option;
198 if (!Traits::Load(*option_value, &option))
199 return false;
200 set_value(option);
201 return IsValid();
204 template <class Option, class Traits>
205 void ValueCapability<Option, Traits>::SaveTo(
206 CloudDeviceDescription* description) const {
207 DCHECK(IsValid());
208 Traits::Save(value(), description->CreateItem(Traits::GetCapabilityPath()));
211 template <class Option, class Traits>
212 TicketItem<Option, Traits>::TicketItem() {
213 Reset();
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) {
227 Reset();
228 const base::DictionaryValue* option_value =
229 description.GetItem(Traits::GetTicketItemPath());
230 if (!option_value)
231 return false;
232 Option option;
233 if (!Traits::Load(*option_value, &option))
234 return false;
235 set_value(option);
236 return IsValid();
239 template <class Option, class Traits>
240 void TicketItem<Option, Traits>::SaveTo(
241 CloudDeviceDescription* description) const {
242 DCHECK(IsValid());
243 Traits::Save(value(), description->CreateItem(Traits::GetTicketItemPath()));
246 } // namespace cloud_devices
248 #endif // COMPONENTS_CLOUD_DEVICES_DESCRIPTION_DESCRIPTION_ITEMS_INL_H_