blob: 1f272e60f1e89881011407e775d16a506ccd7ffd [file] [log] [blame]
Alex Vakulenkoe439a0f2014-05-21 12:26:47 -07001// 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 Vakulenkoa8b95bc2014-08-27 11:00:57 -070012#include <chromeos/errors/error.h>
Alex Vakulenkoe439a0f2014-05-21 12:26:47 -070013
14namespace base {
15class Value;
16class DictionaryValue;
17} // namespace base
18
19namespace buffet {
20
21class 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.
28class ObjectSchema final {
29 public:
Alex Vakulenko5ef75792015-03-19 15:50:44 -070030 // 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 Vakulenkoe439a0f2014-05-21 12:26:47 -070035 // 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 Vakulenko5ef75792015-03-19 15:50:44 -070038 using Properties = std::map<std::string, std::unique_ptr<PropType>>;
Alex Vakulenkoe439a0f2014-05-21 12:26:47 -070039
Alex Vakulenko5ef75792015-03-19 15:50:44 -070040 // Makes a full copy of this object.
41 virtual std::unique_ptr<ObjectSchema> Clone() const;
Alex Vakulenkoe439a0f2014-05-21 12:26:47 -070042
43 // Add a new parameter definition.
Alex Vakulenko5ef75792015-03-19 15:50:44 -070044 void AddProp(const std::string& name, std::unique_ptr<PropType> prop);
45
Alex Vakulenkoe439a0f2014-05-21 12:26:47 -070046 // Finds parameter type definition by name. Returns nullptr if not found.
47 const PropType* GetProp(const std::string& name) const;
Alex Vakulenko5ef75792015-03-19 15:50:44 -070048
Alex Vakulenkoe439a0f2014-05-21 12:26:47 -070049 // Gets the list of all the properties defined.
50 const Properties& GetProps() const { return properties_; }
51
Alex Vakulenko66ec2922014-06-17 15:30:22 -070052 // 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 Vakulenkoe439a0f2014-05-21 12:26:47 -070062 // 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 Vakulenko5f472062014-08-14 17:54:04 -070065 std::unique_ptr<base::DictionaryValue> ToJson(
66 bool full_schema, chromeos::ErrorPtr* error) const;
Alex Vakulenko5ef75792015-03-19 15:50:44 -070067
Alex Vakulenko66ec2922014-06-17 15:30:22 -070068 // Loads the object schema from JSON. If |object_schema| is not nullptr, it is
Alex Vakulenkoe439a0f2014-05-21 12:26:47 -070069 // used as a base schema to inherit omitted properties and constraints from.
Alex Vakulenko66ec2922014-06-17 15:30:22 -070070 bool FromJson(const base::DictionaryValue* value,
Alex Vakulenkod94656e2015-03-18 09:54:37 -070071 const ObjectSchema* object_schema,
72 chromeos::ErrorPtr* error);
73
Alex Vakulenko5ef75792015-03-19 15:50:44 -070074 // Helper factory method to create a new instance of ObjectSchema object.
75 static std::unique_ptr<ObjectSchema> Create();
76
Alex Vakulenkod94656e2015-03-18 09:54:37 -070077 // 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 Vakulenkoe439a0f2014-05-21 12:26:47 -070081
82 private:
Alex Vakulenkoe439a0f2014-05-21 12:26:47 -070083 // Internal parameter type definition map.
84 Properties properties_;
Alex Vakulenko66ec2922014-06-17 15:30:22 -070085 bool extra_properties_allowed_{false};
Alex Vakulenkoe439a0f2014-05-21 12:26:47 -070086};
87
88} // namespace buffet
89
90#endif // BUFFET_COMMANDS_OBJECT_SCHEMA_H_