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_OBJECT_SCHEMA_H_ |
| 6 | #define BUFFET_COMMANDS_OBJECT_SCHEMA_H_ |
| 7 | |
| 8 | #include <map> |
| 9 | #include <memory> |
| 10 | #include <string> |
| 11 | |
Alex Vakulenko | a8b95bc | 2014-08-27 11:00:57 -0700 | [diff] [blame] | 12 | #include <chromeos/errors/error.h> |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 13 | |
| 14 | namespace base { |
| 15 | class Value; |
| 16 | class DictionaryValue; |
| 17 | } // namespace base |
| 18 | |
| 19 | namespace buffet { |
| 20 | |
| 21 | class PropType; |
| 22 | |
| 23 | // ObjectSchema is a class representing an object definition in GCD command |
| 24 | // schema. This could represent a GCD command definition, but also it can be |
| 25 | // used when defining custom object types for command properties such as |
| 26 | // output media type (paper) for print command. The schema definition for |
| 27 | // these type of object description is the same. |
| 28 | class ObjectSchema final { |
| 29 | public: |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 30 | // Do not inline the constructor/destructor to allow forward-declared type |
| 31 | // PropType to be part of |properties_| member. |
| 32 | ObjectSchema(); |
| 33 | ~ObjectSchema(); |
| 34 | |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 35 | // Properties is a string-to-PropType map representing a list of |
| 36 | // properties defined for a command/object. The key is the parameter |
| 37 | // name and the value is the parameter type definition object. |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 38 | using Properties = std::map<std::string, std::unique_ptr<PropType>>; |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 39 | |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 40 | // Makes a full copy of this object. |
| 41 | virtual std::unique_ptr<ObjectSchema> Clone() const; |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 42 | |
| 43 | // Add a new parameter definition. |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 44 | void AddProp(const std::string& name, std::unique_ptr<PropType> prop); |
| 45 | |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 46 | // Finds parameter type definition by name. Returns nullptr if not found. |
| 47 | const PropType* GetProp(const std::string& name) const; |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 48 | |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 49 | // Gets the list of all the properties defined. |
| 50 | const Properties& GetProps() const { return properties_; } |
| 51 | |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 52 | // Specify whether extra properties are allowed on objects described by |
| 53 | // this schema. When validating a value of an object type, we can |
| 54 | // make sure that the value has only the properties explicitly defined by |
| 55 | // the schema and no other (custom) properties are allowed. |
| 56 | // This is to support JSON Schema's "additionalProperties" specification. |
| 57 | bool GetExtraPropertiesAllowed() const { return extra_properties_allowed_; } |
| 58 | void SetExtraPropertiesAllowed(bool allowed) { |
| 59 | extra_properties_allowed_ = allowed; |
| 60 | } |
| 61 | |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 62 | // Saves the object schema to JSON. When |full_schema| is set to true, |
| 63 | // then all properties and constraints are saved, otherwise, only |
| 64 | // the overridden (not inherited) ones are saved. |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 65 | std::unique_ptr<base::DictionaryValue> ToJson( |
| 66 | bool full_schema, chromeos::ErrorPtr* error) const; |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 67 | |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 68 | // Loads the object schema from JSON. If |object_schema| is not nullptr, it is |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 69 | // used as a base schema to inherit omitted properties and constraints from. |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 70 | bool FromJson(const base::DictionaryValue* value, |
Alex Vakulenko | d94656e | 2015-03-18 09:54:37 -0700 | [diff] [blame] | 71 | const ObjectSchema* object_schema, |
| 72 | chromeos::ErrorPtr* error); |
| 73 | |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 74 | // Helper factory method to create a new instance of ObjectSchema object. |
| 75 | static std::unique_ptr<ObjectSchema> Create(); |
| 76 | |
Alex Vakulenko | d94656e | 2015-03-18 09:54:37 -0700 | [diff] [blame] | 77 | // Helper method to load property type definitions from JSON. |
| 78 | static std::unique_ptr<PropType> PropFromJson(const base::Value& value, |
| 79 | const PropType* base_schema, |
| 80 | chromeos::ErrorPtr* error); |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 81 | |
| 82 | private: |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 83 | // Internal parameter type definition map. |
| 84 | Properties properties_; |
Alex Vakulenko | 66ec292 | 2014-06-17 15:30:22 -0700 | [diff] [blame] | 85 | bool extra_properties_allowed_{false}; |
Alex Vakulenko | e439a0f | 2014-05-21 12:26:47 -0700 | [diff] [blame] | 86 | }; |
| 87 | |
| 88 | } // namespace buffet |
| 89 | |
| 90 | #endif // BUFFET_COMMANDS_OBJECT_SCHEMA_H_ |