Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 1 | // Copyright 2014 The Chromium OS 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 BUFFET_COMMANDS_PROP_CONSTRAINTS_H_ |
| 6 | #define BUFFET_COMMANDS_PROP_CONSTRAINTS_H_ |
| 7 | |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 8 | #include <string> |
| 9 | #include <type_traits> |
| 10 | #include <vector> |
| 11 | |
Alex Vakulenko | 132617a | 2014-09-04 08:59:43 -0700 | [diff] [blame] | 12 | #include <base/macros.h> |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 13 | #include <base/values.h> |
Alex Vakulenko | a8b95bc | 2014-08-27 11:00:57 -0700 | [diff] [blame] | 14 | #include <chromeos/errors/error.h> |
| 15 | #include <chromeos/strings/string_utils.h> |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 16 | |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 17 | #include "buffet/commands/prop_values.h" |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 18 | #include "buffet/commands/schema_constants.h" |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 19 | #include "buffet/commands/schema_utils.h" |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 20 | |
| 21 | namespace buffet { |
| 22 | |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 23 | enum class ConstraintType { |
| 24 | Min, |
| 25 | Max, |
| 26 | StringLengthMin, |
| 27 | StringLengthMax, |
| 28 | OneOf |
| 29 | }; |
| 30 | |
| 31 | // Abstract base class for all parameter constraints. Many constraints are |
| 32 | // type-dependent. Thus, a numeric parameter could have "minimum" and/or |
| 33 | // "maximum" constraints specified. Some constraints, such as "OneOf" apply to |
| 34 | // any data type. |
| 35 | class Constraint { |
| 36 | public: |
| 37 | Constraint() = default; |
| 38 | virtual ~Constraint(); |
| 39 | |
| 40 | // Gets the constraint type. |
| 41 | virtual ConstraintType GetType() const = 0; |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 42 | |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 43 | // Checks if any of the constraint properties/attributes are overridden |
| 44 | // from their base schema definition. If the constraint is inherited, then |
| 45 | // it will not be written to JSON when saving partial schema. |
| 46 | virtual bool HasOverriddenAttributes() const = 0; |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 47 | |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 48 | // Validates a parameter against the constraint. Returns true if parameter |
| 49 | // value satisfies the constraint, otherwise fills the optional |error| with |
| 50 | // the details for the failure. |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 51 | virtual bool Validate(const PropValue& value, |
| 52 | chromeos::ErrorPtr* error) const = 0; |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 53 | |
| 54 | // Makes a full copy of this Constraint instance. |
| 55 | virtual std::unique_ptr<Constraint> Clone() const = 0; |
| 56 | |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 57 | // Makes a copy of the constraint object, marking all the attributes |
| 58 | // as inherited from the original definition. |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 59 | virtual std::unique_ptr<Constraint> CloneAsInherited() const = 0; |
| 60 | |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 61 | // Saves the constraint into the specified JSON |dict| object, representing |
| 62 | // the object schema. If |overridden_only| is set to true, then the |
| 63 | // inherited constraints will not be added to the schema object. |
Alex Vakulenko | d94656e | 2015-03-18 09:54:37 -0700 | [diff] [blame] | 64 | virtual bool AddToJsonDict(base::DictionaryValue* dict, |
| 65 | bool overridden_only, |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 66 | chromeos::ErrorPtr* error) const; |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 67 | |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 68 | // Saves the value of constraint to JSON value. E.g., if the numeric |
| 69 | // constraint was defined as {"minimum":20} this will create a JSON value |
| 70 | // of 20. The current design implies that each constraint has one value |
| 71 | // only. If this assumption changes, this interface needs to be updated |
| 72 | // accordingly. |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 73 | virtual std::unique_ptr<base::Value> ToJson( |
| 74 | chromeos::ErrorPtr* error) const = 0; |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 75 | |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 76 | // Overloaded by the concrete class implementation, it should return the |
| 77 | // JSON object property name to store the constraint's value as. |
| 78 | // E.g., if the numeric constraint was defined as {"minimum":20} this |
| 79 | // method should return "minimum". |
| 80 | virtual const char* GetDictKey() const = 0; |
| 81 | |
| 82 | protected: |
| 83 | // Static helper methods to format common constraint validation errors. |
| 84 | // They fill the |error| object with specific error message. |
| 85 | // Since these functions could be used by constraint objects for various |
| 86 | // data types, the values used in validation are expected to be |
| 87 | // send as strings already. |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 88 | static bool ReportErrorLessThan(chromeos::ErrorPtr* error, |
| 89 | const std::string& val, |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 90 | const std::string& limit); |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 91 | static bool ReportErrorGreaterThan(chromeos::ErrorPtr* error, |
| 92 | const std::string& val, |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 93 | const std::string& limit); |
| 94 | |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 95 | static bool ReportErrorNotOneOf(chromeos::ErrorPtr* error, |
| 96 | const std::string& val, |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 97 | const std::vector<std::string>& values); |
| 98 | |
| 99 | private: |
| 100 | DISALLOW_COPY_AND_ASSIGN(Constraint); |
| 101 | }; |
| 102 | |
| 103 | // ConstraintMinMaxBase is a base class for numeric Minimum and Maximum |
| 104 | // constraints. |
| 105 | template<typename T> |
| 106 | class ConstraintMinMaxBase : public Constraint { |
| 107 | public: |
| 108 | explicit ConstraintMinMaxBase(const InheritableAttribute<T>& limit) |
| 109 | : limit_(limit) {} |
| 110 | explicit ConstraintMinMaxBase(const T& limit) |
| 111 | : limit_(limit) {} |
| 112 | |
| 113 | // Implementation of Constraint::HasOverriddenAttributes(). |
Alex Vakulenko | 5a9e718 | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 114 | bool HasOverriddenAttributes() const override { |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 115 | return !limit_.is_inherited; |
| 116 | } |
| 117 | |
| 118 | // Implementation of Constraint::ToJson(). |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 119 | std::unique_ptr<base::Value> ToJson( |
| 120 | chromeos::ErrorPtr* error) const override { |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 121 | return TypedValueToJson(limit_.value, error); |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | // Stores the upper/lower value limit for maximum/minimum constraint. |
| 125 | // |limit_.is_inherited| indicates whether the constraint is inherited |
| 126 | // from base schema or overridden. |
| 127 | InheritableAttribute<T> limit_; |
| 128 | |
| 129 | private: |
| 130 | DISALLOW_COPY_AND_ASSIGN(ConstraintMinMaxBase); |
| 131 | }; |
| 132 | |
| 133 | // Implementation of Minimum value constraint for Integer/Double types. |
| 134 | template<typename T> |
| 135 | class ConstraintMin : public ConstraintMinMaxBase<T> { |
| 136 | public: |
| 137 | explicit ConstraintMin(const InheritableAttribute<T>& limit) |
| 138 | : ConstraintMinMaxBase<T>(limit) {} |
| 139 | explicit ConstraintMin(const T& limit) |
| 140 | : ConstraintMinMaxBase<T>(limit) {} |
| 141 | |
| 142 | // Implementation of Constraint::GetType(). |
Yunlian Jiang | 89a9725 | 2015-01-28 13:33:46 -0800 | [diff] [blame] | 143 | ConstraintType GetType() const override { return ConstraintType::Min; } |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 144 | |
| 145 | // Implementation of Constraint::Validate(). |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 146 | bool Validate(const PropValue& value, |
| 147 | chromeos::ErrorPtr* error) const override { |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 148 | T v = value.GetValueAsAny().Get<T>(); |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 149 | if (v < this->limit_.value) |
| 150 | return this->ReportErrorLessThan( |
Alex Vakulenko | b8fc1df | 2014-08-20 15:38:07 -0700 | [diff] [blame] | 151 | error, chromeos::string_utils::ToString(v), |
| 152 | chromeos::string_utils::ToString(this->limit_.value)); |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 153 | return true; |
| 154 | } |
| 155 | |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 156 | // Implementation of Constraint::Clone(). |
| 157 | std::unique_ptr<Constraint> Clone() const override { |
| 158 | return std::unique_ptr<Constraint>{new ConstraintMin{this->limit_}}; |
| 159 | } |
| 160 | |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 161 | // Implementation of Constraint::CloneAsInherited(). |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 162 | std::unique_ptr<Constraint> CloneAsInherited() const override { |
| 163 | return std::unique_ptr<Constraint>{new ConstraintMin{this->limit_.value}}; |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | // Implementation of Constraint::GetDictKey(). |
Alex Vakulenko | 5a9e718 | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 167 | const char* GetDictKey() const override { |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 168 | return commands::attributes::kNumeric_Min; |
| 169 | } |
| 170 | |
| 171 | private: |
| 172 | DISALLOW_COPY_AND_ASSIGN(ConstraintMin); |
| 173 | }; |
| 174 | |
| 175 | // Implementation of Maximum value constraint for Integer/Double types. |
| 176 | template<typename T> |
| 177 | class ConstraintMax : public ConstraintMinMaxBase<T> { |
| 178 | public: |
| 179 | explicit ConstraintMax(const InheritableAttribute<T>& limit) |
| 180 | : ConstraintMinMaxBase<T>(limit) {} |
| 181 | explicit ConstraintMax(const T& limit) |
| 182 | : ConstraintMinMaxBase<T>(limit) {} |
| 183 | |
| 184 | // Implementation of Constraint::GetType(). |
Yunlian Jiang | 89a9725 | 2015-01-28 13:33:46 -0800 | [diff] [blame] | 185 | ConstraintType GetType() const override { return ConstraintType::Max; } |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 186 | |
| 187 | // Implementation of Constraint::Validate(). |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 188 | bool Validate(const PropValue& value, |
| 189 | chromeos::ErrorPtr* error) const override { |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 190 | T v = value.GetValueAsAny().Get<T>(); |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 191 | if (v > this->limit_.value) |
| 192 | return this->ReportErrorGreaterThan( |
Alex Vakulenko | b8fc1df | 2014-08-20 15:38:07 -0700 | [diff] [blame] | 193 | error, chromeos::string_utils::ToString(v), |
| 194 | chromeos::string_utils::ToString(this->limit_.value)); |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 195 | return true; |
| 196 | } |
| 197 | |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 198 | // Implementation of Constraint::Clone(). |
| 199 | std::unique_ptr<Constraint> Clone() const override { |
| 200 | return std::unique_ptr<Constraint>{new ConstraintMax{this->limit_}}; |
| 201 | } |
| 202 | |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 203 | // Implementation of Constraint::CloneAsInherited(). |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 204 | std::unique_ptr<Constraint> CloneAsInherited() const override { |
| 205 | return std::unique_ptr<Constraint>{new ConstraintMax{this->limit_.value}}; |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | // Implementation of Constraint::GetDictKey(). |
Alex Vakulenko | 5a9e718 | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 209 | const char* GetDictKey() const override { |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 210 | return commands::attributes::kNumeric_Max; |
| 211 | } |
| 212 | |
| 213 | private: |
| 214 | DISALLOW_COPY_AND_ASSIGN(ConstraintMax); |
| 215 | }; |
| 216 | |
| 217 | // ConstraintStringLength is a base class for Minimum/Maximum string length |
| 218 | // constraints, similar to ConstraintMinMaxBase of numeric types. |
| 219 | class ConstraintStringLength : public Constraint { |
| 220 | public: |
| 221 | explicit ConstraintStringLength(const InheritableAttribute<int>& limit); |
| 222 | explicit ConstraintStringLength(int limit); |
| 223 | |
| 224 | // Implementation of Constraint::HasOverriddenAttributes(). |
Alex Vakulenko | 5a9e718 | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 225 | bool HasOverriddenAttributes() const override; |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 226 | // Implementation of Constraint::ToJson(). |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 227 | std::unique_ptr<base::Value> ToJson(chromeos::ErrorPtr* error) const override; |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 228 | |
| 229 | // Stores the upper/lower value limit for string length constraint. |
| 230 | // |limit_.is_inherited| indicates whether the constraint is inherited |
| 231 | // from base schema or overridden. |
| 232 | InheritableAttribute<int> limit_; |
| 233 | |
| 234 | private: |
| 235 | DISALLOW_COPY_AND_ASSIGN(ConstraintStringLength); |
| 236 | }; |
| 237 | |
| 238 | // Implementation of Minimum string length constraint. |
| 239 | class ConstraintStringLengthMin : public ConstraintStringLength { |
| 240 | public: |
| 241 | explicit ConstraintStringLengthMin(const InheritableAttribute<int>& limit); |
| 242 | explicit ConstraintStringLengthMin(int limit); |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 243 | |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 244 | // Implementation of Constraint::GetType(). |
Alex Vakulenko | 5a9e718 | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 245 | ConstraintType GetType() const override { |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 246 | return ConstraintType::StringLengthMin; |
| 247 | } |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 248 | |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 249 | // Implementation of Constraint::Validate(). |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 250 | bool Validate(const PropValue& value, |
| 251 | chromeos::ErrorPtr* error) const override; |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 252 | |
| 253 | // Implementation of Constraint::Clone(). |
| 254 | std::unique_ptr<Constraint> Clone() const override; |
| 255 | |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 256 | // Implementation of Constraint::CloneAsInherited(). |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 257 | std::unique_ptr<Constraint> CloneAsInherited() const override; |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 258 | // Implementation of Constraint::GetDictKey(). |
Alex Vakulenko | 5a9e718 | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 259 | const char* GetDictKey() const override { |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 260 | return commands::attributes::kString_MinLength; |
| 261 | } |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 262 | |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 263 | private: |
| 264 | DISALLOW_COPY_AND_ASSIGN(ConstraintStringLengthMin); |
| 265 | }; |
| 266 | |
| 267 | // Implementation of Maximum string length constraint. |
| 268 | class ConstraintStringLengthMax : public ConstraintStringLength { |
| 269 | public: |
| 270 | explicit ConstraintStringLengthMax(const InheritableAttribute<int>& limit); |
| 271 | explicit ConstraintStringLengthMax(int limit); |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 272 | |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 273 | // Implementation of Constraint::GetType(). |
Alex Vakulenko | 5a9e718 | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 274 | ConstraintType GetType() const override { |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 275 | return ConstraintType::StringLengthMax; |
| 276 | } |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 277 | |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 278 | // Implementation of Constraint::Validate(). |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 279 | bool Validate(const PropValue& value, |
| 280 | chromeos::ErrorPtr* error) const override; |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 281 | |
| 282 | // Implementation of Constraint::Clone(). |
| 283 | std::unique_ptr<Constraint> Clone() const override; |
| 284 | |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 285 | // Implementation of Constraint::CloneAsInherited(). |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 286 | std::unique_ptr<Constraint> CloneAsInherited() const override; |
| 287 | |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 288 | // Implementation of Constraint::GetDictKey(). |
Alex Vakulenko | 5a9e718 | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 289 | const char* GetDictKey() const override { |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 290 | return commands::attributes::kString_MaxLength; |
| 291 | } |
| 292 | |
| 293 | private: |
| 294 | DISALLOW_COPY_AND_ASSIGN(ConstraintStringLengthMax); |
| 295 | }; |
| 296 | |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 297 | // Implementation of OneOf constraint for different data types. |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 298 | class ConstraintOneOf : public Constraint { |
| 299 | public: |
Alex Vakulenko | 29e6444 | 2015-03-20 13:59:19 -0700 | [diff] [blame] | 300 | explicit ConstraintOneOf(InheritableAttribute<native_types::Array> set); |
| 301 | explicit ConstraintOneOf(native_types::Array set); |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 302 | |
| 303 | // Implementation of Constraint::GetType(). |
Alex Vakulenko | 5a9e718 | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 304 | ConstraintType GetType() const override { |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 305 | return ConstraintType::OneOf; |
| 306 | } |
| 307 | |
| 308 | // Implementation of Constraint::HasOverriddenAttributes(). |
Alex Vakulenko | 5a9e718 | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 309 | bool HasOverriddenAttributes() const override { |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 310 | return !set_.is_inherited; |
| 311 | } |
| 312 | |
| 313 | // Implementation of Constraint::Validate(). |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 314 | bool Validate(const PropValue& value, |
Alex Vakulenko | 9e25ecd | 2015-03-20 09:43:10 -0700 | [diff] [blame] | 315 | chromeos::ErrorPtr* error) const override; |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 316 | |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 317 | // Implementation of Constraint::Clone(). |
Alex Vakulenko | 9e25ecd | 2015-03-20 09:43:10 -0700 | [diff] [blame] | 318 | std::unique_ptr<Constraint> Clone() const override; |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 319 | |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 320 | // Implementation of Constraint::CloneAsInherited(). |
Alex Vakulenko | 9e25ecd | 2015-03-20 09:43:10 -0700 | [diff] [blame] | 321 | std::unique_ptr<Constraint> CloneAsInherited() const override; |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 322 | |
| 323 | // Implementation of Constraint::ToJson(). |
Alex Vakulenko | 9e25ecd | 2015-03-20 09:43:10 -0700 | [diff] [blame] | 324 | std::unique_ptr<base::Value> ToJson(chromeos::ErrorPtr* error) const override; |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 325 | |
| 326 | // Implementation of Constraint::GetDictKey(). |
Alex Vakulenko | 9e25ecd | 2015-03-20 09:43:10 -0700 | [diff] [blame] | 327 | const char* GetDictKey() const override; |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 328 | |
| 329 | // Stores the list of acceptable values for the parameter. |
| 330 | // |set_.is_inherited| indicates whether the constraint is inherited |
| 331 | // from base schema or overridden. |
Alex Vakulenko | 29e6444 | 2015-03-20 13:59:19 -0700 | [diff] [blame] | 332 | InheritableAttribute<native_types::Array> set_; |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 333 | |
| 334 | private: |
| 335 | DISALLOW_COPY_AND_ASSIGN(ConstraintOneOf); |
| 336 | }; |
| 337 | |
| 338 | } // namespace buffet |
| 339 | |
| 340 | #endif // BUFFET_COMMANDS_PROP_CONSTRAINTS_H_ |