blob: 1430b5cb94a20b44cd4bebec6671a7e00057a079 [file] [log] [blame]
Alex Vakulenko07216fe2014-09-19 15:31:09 -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_STATES_STATE_PACKAGE_H_
6#define BUFFET_STATES_STATE_PACKAGE_H_
7
8#include <map>
9#include <memory>
10#include <string>
11
12#include <base/macros.h>
13#include <chromeos/any.h>
14#include <chromeos/errors/error.h>
15
16#include "buffet/commands/object_schema.h"
17#include "buffet/commands/prop_values.h"
18
19namespace base {
20class DictionaryValue;
21} // namespace base
22
23namespace buffet {
24
25// A package is a set of related state properties. GCD specification defines
26// a number of standard state properties in "base" package such as
27// "base.manufacturer", "base.model", "base.firmwareVersion" and so on.
28class StatePackage final {
29 public:
30 explicit StatePackage(const std::string& name);
31
32 // Loads state property definitions from a JSON object and adds them
33 // to the current package.
34 bool AddSchemaFromJson(const base::DictionaryValue* json,
35 chromeos::ErrorPtr* error);
36 // Loads a set of state property values from a JSON object and assigns them
37 // to existing properties. A property must be defined prior to loading its
38 // value. We use this when we load default values during buffet startup.
39 bool AddValuesFromJson(const base::DictionaryValue* json,
40 chromeos::ErrorPtr* error);
41
42 // Returns a set of state properties and their values as a JSON object.
43 // After being aggregated across multiple packages, this becomes the device
44 // state object passed to the GCD server or a local client in the format
45 // described by GCD specification, e.g.:
46 // {
47 // "base": {
48 // "manufacturer":"...",
49 // "model":"..."
50 // },
51 // "printer": {
52 // "message": "Printer low on cyan ink"
53 // }
54 // }
55 std::unique_ptr<base::DictionaryValue> GetValuesAsJson(
56 chromeos::ErrorPtr* error) const;
57
58 // Gets the value for a specific state property. |property_name| must not
59 // include the package name as part of the property name.
60 chromeos::Any GetPropertyValue(const std::string& property_name,
61 chromeos::ErrorPtr* error) const;
62 // Sets the value for a specific state property. |property_name| must not
63 // include the package name as part of the property name.
64 bool SetPropertyValue(const std::string& property_name,
65 const chromeos::Any& value,
66 chromeos::ErrorPtr* error);
67
Anton Muhin01829452014-11-21 02:16:04 +040068 std::shared_ptr<const PropValue>
69 GetProperty(const std::string& property_name) const {
70 auto it = values_.find(property_name);
71 return it != values_.end() ?
72 it->second : std::shared_ptr<const PropValue>{};
73 }
74
Alex Vakulenko07216fe2014-09-19 15:31:09 -070075 // Returns the name of the this package.
76 const std::string& GetName() const { return name_; }
77
78 private:
79 std::string name_;
80 ObjectSchema types_;
81 native_types::Object values_;
82
83 friend class StatePackageTestHelper;
84 DISALLOW_COPY_AND_ASSIGN(StatePackage);
85};
86
87} // namespace buffet
88
89#endif // BUFFET_STATES_STATE_PACKAGE_H_