| OLD | NEW |
| (Empty) | |
| 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_DESCRIPTION_DESCRIPTION_ITEMS_INL_H_ |
| 6 #define COMPONENTS_CLOUD_DEVICES_DESCRIPTION_DESCRIPTION_ITEMS_INL_H_ |
| 7 |
| 8 #include <vector> |
| 9 |
| 10 #include "components/cloud_devices/description_items.h" |
| 11 |
| 12 // Implementation of templates defined in header file. |
| 13 // This file should be included from CC file with implementation of device |
| 14 // specific capabilities. |
| 15 |
| 16 namespace cloud_devices { |
| 17 |
| 18 template <class Option, class Traits> |
| 19 ListCapability<Option, Traits>::ListCapability() { |
| 20 Reset(); |
| 21 } |
| 22 |
| 23 template <class Option, class Traits> |
| 24 ListCapability<Option, Traits>::~ListCapability() { } |
| 25 |
| 26 template <class Option, class Traits> |
| 27 bool ListCapability<Option, Traits>::IsValid() const { |
| 28 if (empty()) |
| 29 return false; // This type of capabilities can't be empty. |
| 30 for (size_t i = 0; i < options_.size(); ++i) { |
| 31 if (!Traits::IsValid(options_[i])) |
| 32 return false; |
| 33 } |
| 34 return true; |
| 35 } |
| 36 |
| 37 template <class Option, class Traits> |
| 38 bool ListCapability<Option, Traits>::LoadFrom( |
| 39 const CloudDeviceDescription& description) { |
| 40 Reset(); |
| 41 const base::ListValue* options = |
| 42 description.GetListItem(Traits::GetItemPath()); |
| 43 if (!options) |
| 44 return false; |
| 45 for (size_t i = 0; i < options->GetSize(); ++i) { |
| 46 const base::DictionaryValue* option_value = NULL; |
| 47 if (!options->GetDictionary(i, &option_value)) |
| 48 return false; // Every entry must be a dictionary. |
| 49 Option option; |
| 50 if (!Traits::Load(*option_value, &option)) |
| 51 return false; |
| 52 AddOption(option); |
| 53 } |
| 54 return IsValid(); |
| 55 } |
| 56 |
| 57 template <class Option, class Traits> |
| 58 void ListCapability<Option, Traits>::SaveTo( |
| 59 CloudDeviceDescription* description) const { |
| 60 DCHECK(IsValid()); |
| 61 base::ListValue* options_list = |
| 62 description->CreateListItem(Traits::GetItemPath()); |
| 63 for (size_t i = 0; i < options_.size(); ++i) { |
| 64 base::DictionaryValue* option_value = new base::DictionaryValue; |
| 65 options_list->Append(option_value); |
| 66 Traits::Save(options_[i], option_value); |
| 67 } |
| 68 } |
| 69 |
| 70 template <class Option, class Traits> |
| 71 SelectionCapability<Option, Traits>::SelectionCapability() { |
| 72 Reset(); |
| 73 } |
| 74 |
| 75 template <class Option, class Traits> |
| 76 SelectionCapability<Option, Traits>::~SelectionCapability() { } |
| 77 |
| 78 template <class Option, class Traits> |
| 79 bool SelectionCapability<Option, Traits>::IsValid() const { |
| 80 if (empty()) |
| 81 return false; // This type of capabilities can't be empty |
| 82 for (size_t i = 0; i < options_.size(); ++i) { |
| 83 if (!Traits::IsValid(options_[i])) |
| 84 return false; |
| 85 } |
| 86 return default_idx_ >= 0 && default_idx_ < static_cast<int>(size()); |
| 87 } |
| 88 |
| 89 template <class Option, class Traits> |
| 90 bool SelectionCapability<Option, Traits>::LoadFrom( |
| 91 const CloudDeviceDescription& description) { |
| 92 Reset(); |
| 93 const base::DictionaryValue* item = |
| 94 description.GetItem(Traits::GetItemPath()); |
| 95 if (!item) |
| 96 return false; |
| 97 const base::ListValue* options = NULL; |
| 98 if (!item->GetList(json::kKeyOption, &options)) |
| 99 return false; |
| 100 for (size_t i = 0; i < options->GetSize(); ++i) { |
| 101 const base::DictionaryValue* option_value = NULL; |
| 102 if (!options->GetDictionary(i, &option_value)) |
| 103 return false; // Every entry must be a dictionary. |
| 104 Option option; |
| 105 if (!Traits::Load(*option_value, &option)) |
| 106 return false; |
| 107 bool is_default = false; |
| 108 option_value->GetBoolean(json::kKeyIsDefault, &is_default); |
| 109 if (is_default && default_idx_ >= 0) { |
| 110 return false; // Multiple defaults. |
| 111 } |
| 112 AddDefaultOption(option, is_default); |
| 113 } |
| 114 return IsValid(); |
| 115 } |
| 116 |
| 117 template <class Option, class Traits> |
| 118 void SelectionCapability<Option, Traits>::SaveTo( |
| 119 CloudDeviceDescription* description) const { |
| 120 DCHECK(IsValid()); |
| 121 base::ListValue* options_list = new base::ListValue; |
| 122 description->CreateItem(Traits::GetItemPath())->Set(json::kKeyOption, |
| 123 options_list); |
| 124 for (size_t i = 0; i < options_.size(); ++i) { |
| 125 base::DictionaryValue* option_value = new base::DictionaryValue; |
| 126 options_list->Append(option_value); |
| 127 if (static_cast<int>(i) == default_idx_) |
| 128 option_value->SetBoolean(json::kKeyIsDefault, true); |
| 129 Traits::Save(options_[i], option_value); |
| 130 } |
| 131 } |
| 132 |
| 133 template <class Traits> |
| 134 BooleanCapability<Traits>::BooleanCapability() { |
| 135 Reset(); |
| 136 } |
| 137 |
| 138 template <class Traits> |
| 139 BooleanCapability<Traits>::~BooleanCapability() { } |
| 140 |
| 141 template <class Traits> |
| 142 bool BooleanCapability<Traits>::LoadFrom( |
| 143 const CloudDeviceDescription& description) { |
| 144 Reset(); |
| 145 const base::DictionaryValue* dict = |
| 146 description.GetItem(Traits::GetItemPath()); |
| 147 if (!dict) |
| 148 return false; |
| 149 default_value_ = Traits::kDefault; |
| 150 dict->GetBoolean(json::kKeyDefault, &default_value_); |
| 151 return true; |
| 152 } |
| 153 |
| 154 template <class Traits> |
| 155 void BooleanCapability<Traits>::SaveTo( |
| 156 CloudDeviceDescription* description) const { |
| 157 base::DictionaryValue* dict = description->CreateItem(Traits::GetItemPath()); |
| 158 if (default_value_ != Traits::kDefault) |
| 159 dict->SetBoolean(json::kKeyDefault, default_value_); |
| 160 } |
| 161 |
| 162 template <class Traits> |
| 163 bool EmptyCapability<Traits>::LoadFrom( |
| 164 const CloudDeviceDescription& description) { |
| 165 return description.GetItem(Traits::GetItemPath()) != NULL; |
| 166 } |
| 167 |
| 168 template <class Traits> |
| 169 void EmptyCapability<Traits>::SaveTo( |
| 170 CloudDeviceDescription* description) const { |
| 171 description->CreateItem(Traits::GetItemPath()); |
| 172 } |
| 173 |
| 174 template <class Option, class Traits> |
| 175 TicketItem<Option, Traits>::TicketItem() { |
| 176 Reset(); |
| 177 } |
| 178 |
| 179 template <class Option, class Traits> |
| 180 TicketItem<Option, Traits>::~TicketItem() { } |
| 181 |
| 182 template <class Option, class Traits> |
| 183 bool TicketItem<Option, Traits>::IsValid() const { |
| 184 return Traits::IsValid(value()); |
| 185 } |
| 186 |
| 187 template <class Option, class Traits> |
| 188 bool TicketItem<Option, Traits>::LoadFrom( |
| 189 const CloudDeviceDescription& description) { |
| 190 Reset(); |
| 191 const base::DictionaryValue* option_value = |
| 192 description.GetItem(Traits::GetItemPath()); |
| 193 if (!option_value) |
| 194 return false; |
| 195 Option option; |
| 196 if (!Traits::Load(*option_value, &option)) |
| 197 return false; |
| 198 set_value(option); |
| 199 return IsValid(); |
| 200 } |
| 201 |
| 202 template <class Option, class Traits> |
| 203 void TicketItem<Option, Traits>::SaveTo( |
| 204 CloudDeviceDescription* description) const { |
| 205 DCHECK(IsValid()); |
| 206 Traits::Save(value(), description->CreateItem(Traits::GetItemPath())); |
| 207 } |
| 208 |
| 209 } // namespace cloud_devices |
| 210 |
| 211 #endif // COMPONENTS_CLOUD_DEVICES_DESCRIPTION_DESCRIPTION_ITEMS_INL_H_ |
| OLD | NEW |