Home | History | Annotate | Download | only in common
      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_COMMON_CAPABILITY_INTERFACES_H_
      6 #define COMPONENTS_CLOUD_DEVICES_COMMON_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 #include "base/numerics/safe_conversions.h"
     15 #include "components/cloud_devices/common/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() { options_.clear(); }
     52 
     53   bool IsValid() const;
     54 
     55   bool empty() const { return options_.empty(); }
     56 
     57   size_t size() const { return options_.size(); }
     58 
     59   const Option& operator[](size_t i) const { return options_[i]; }
     60 
     61   bool Contains(const Option& option) const {
     62     return std::find(options_.begin(), options_.end(), option) !=
     63            options_.end();
     64   }
     65 
     66   void AddOption(const Option& option) { options_.push_back(option); }
     67 
     68  private:
     69   typedef std::vector<Option> OptionVector;
     70   OptionVector options_;
     71 
     72   DISALLOW_COPY_AND_ASSIGN(ListCapability);
     73 };
     74 
     75 // Represents CDD capability stored as JSON list with default_value value.
     76 // Ex: "<CAPABILITY_NAME>": { "option": [{ "is_default": true, <VALUE>},
     77 //                                       {<VALUE>} ]}
     78 // Option specifies data type for <VALUE>.
     79 // Traits specifies how <VALUE> is stored in JSON and semantic validation.
     80 template <class Option, class Traits>
     81 class SelectionCapability {
     82  public:
     83   SelectionCapability();
     84   ~SelectionCapability();
     85 
     86   bool LoadFrom(const CloudDeviceDescription& description);
     87   void SaveTo(CloudDeviceDescription* description) const;
     88 
     89   void Reset() {
     90     options_.clear();
     91     default_idx_ = -1;
     92   }
     93 
     94   bool IsValid() const;
     95 
     96   bool empty() const { return options_.empty(); }
     97 
     98   size_t size() const { return options_.size(); }
     99 
    100   const Option& operator[](size_t i) const { return options_[i]; }
    101 
    102   bool Contains(const Option& option) const {
    103     return std::find(options_.begin(), options_.end(), option) !=
    104            options_.end();
    105   }
    106 
    107   const Option& GetDefault() const {
    108     CHECK_GE(default_idx_, 0);
    109     return options_[default_idx_];
    110   }
    111 
    112   void AddOption(const Option& option) { AddDefaultOption(option, false); }
    113 
    114   void AddDefaultOption(const Option& option, bool is_default) {
    115     if (is_default) {
    116       DCHECK_EQ(default_idx_, -1);
    117       // Point to the last element.
    118       default_idx_ = base::checked_cast<int>(size());
    119     }
    120     options_.push_back(option);
    121   }
    122 
    123  private:
    124   typedef std::vector<Option> OptionVector;
    125 
    126   OptionVector options_;
    127   int default_idx_;
    128 
    129   DISALLOW_COPY_AND_ASSIGN(SelectionCapability);
    130 };
    131 
    132 // Represents CDD capability that can be true or false.
    133 // Ex: "<CAPABILITY_NAME>": { "default_value": true }
    134 // Traits specifies how <VALUE> is stored in JSON and semantic validation.
    135 template <class Traits>
    136 class BooleanCapability {
    137  public:
    138   BooleanCapability();
    139   ~BooleanCapability();
    140 
    141   bool LoadFrom(const CloudDeviceDescription& description);
    142   void SaveTo(CloudDeviceDescription* description) const;
    143 
    144   void Reset() { default_value_ = false; }
    145 
    146   void set_default_value(bool value) { default_value_ = value; }
    147 
    148   bool default_value() const { return default_value_; }
    149 
    150  private:
    151   bool default_value_;
    152 
    153   DISALLOW_COPY_AND_ASSIGN(BooleanCapability);
    154 };
    155 
    156 // Represents CDD capability for which existence is only important.
    157 // Ex: "<CAPABILITY_NAME>": { }
    158 // Traits specifies how <VALUE> is stored in JSON and semantic validation.
    159 template <class Traits>
    160 class EmptyCapability {
    161  public:
    162   EmptyCapability() {};
    163   ~EmptyCapability() {};
    164 
    165   bool LoadFrom(const CloudDeviceDescription& description);
    166   void SaveTo(CloudDeviceDescription* description) const;
    167 
    168  private:
    169   DISALLOW_COPY_AND_ASSIGN(EmptyCapability);
    170 };
    171 
    172 // Represents an item that is of a specific value type.
    173 // Ex: "<CAPABILITY_NAME>": {<VALUE>}
    174 // Option specifies data type for <VALUE>.
    175 // Traits specifies how <VALUE> is stored in JSON and semantic validation.
    176 template <class Option, class Traits>
    177 class ValueCapability {
    178  public:
    179   ValueCapability();
    180   ~ValueCapability();
    181 
    182   bool LoadFrom(const CloudDeviceDescription& description);
    183   void SaveTo(CloudDeviceDescription* description) const;
    184 
    185   void Reset() { value_ = Option(); }
    186 
    187   bool IsValid() const;
    188 
    189   const Option& value() const { return value_; }
    190 
    191   void set_value(const Option& value) { value_ = value; }
    192 
    193  private:
    194   Option value_;
    195 
    196   DISALLOW_COPY_AND_ASSIGN(ValueCapability);
    197 };
    198 
    199 // Represents CJT items.
    200 // Ex: "<CAPABILITY_NAME>": {<VALUE>}
    201 // Option specifies data type for <VALUE>.
    202 // Traits specifies how <VALUE> is stored in JSON and semantic validation.
    203 template <class Option, class Traits>
    204 class TicketItem {
    205  public:
    206   TicketItem();
    207   ~TicketItem();
    208 
    209   bool LoadFrom(const CloudDeviceDescription& description);
    210   void SaveTo(CloudDeviceDescription* description) const;
    211 
    212   void Reset() { value_ = Option(); }
    213 
    214   bool IsValid() const;
    215 
    216   const Option& value() const { return value_; }
    217 
    218   void set_value(const Option& value) { value_ = value; }
    219 
    220  private:
    221   Option value_;
    222 
    223   DISALLOW_COPY_AND_ASSIGN(TicketItem);
    224 };
    225 
    226 }  // namespace cloud_devices
    227 
    228 #endif  // COMPONENTS_CLOUD_DEVICES_COMMON_CAPABILITY_INTERFACES_H_
    229