blob: 2555ab500b39c9b1ad8c28af7524ee8c5dc63f15 [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_MANAGER_H_
6#define BUFFET_STATES_STATE_MANAGER_H_
7
8#include <map>
9#include <memory>
10#include <set>
11#include <string>
12
13#include <base/macros.h>
Alex Vakulenko07216fe2014-09-19 15:31:09 -070014#include <chromeos/errors/error.h>
Alex Vakulenko576c9792014-09-22 16:49:45 -070015#include <chromeos/variant_dictionary.h>
Alex Vakulenko07216fe2014-09-19 15:31:09 -070016
17#include "buffet/states/state_package.h"
18
19namespace base {
20class DictionaryValue;
21class FilePath;
22} // namespace base
23
24namespace buffet {
25
26// StateManager is the class that aggregates the device state fragments
27// provided by device daemons and makes the aggregate device state available
28// to the GCD cloud server and local clients.
29class StateManager final {
30 public:
31 StateManager() = default;
32
33 // Initializes the state manager and load device state fragments.
34 // Called by Buffet daemon at startup.
35 void Startup();
36
37 // Returns aggregated state properties across all registered packages as
38 // a JSON object that can be used to send the device state to the GCD server.
39 std::unique_ptr<base::DictionaryValue> GetStateValuesAsJson(
40 chromeos::ErrorPtr* error) const;
41
42 // Updates a single property value. |full_property_name| must be the full
43 // name of the property to update in format "package.property".
44 bool SetPropertyValue(const std::string& full_property_name,
45 const chromeos::Any& value,
46 chromeos::ErrorPtr* error);
47
48 // Updates a number of state properties in one shot.
49 // |property_set| is a (full_property_name)-to-(property_value) map.
Alex Vakulenko576c9792014-09-22 16:49:45 -070050 bool UpdateProperties(const chromeos::VariantDictionary& property_set,
Alex Vakulenko07216fe2014-09-19 15:31:09 -070051 chromeos::ErrorPtr* error);
52
53 // Returns all the categories the state properties are registered from.
54 // As with GCD command handling, the category normally represent a device
55 // service (daemon) that is responsible for a set of properties.
56 const std::set<std::string>& GetCategories() const {
57 return categories_;
58 }
59
60 private:
61 // Loads a device state fragment from a JSON object. |category| represents
62 // a device daemon providing the state fragment or empty string for the
63 // base state fragment.
64 bool LoadStateDefinition(const base::DictionaryValue& json,
65 const std::string& category,
66 chromeos::ErrorPtr* error);
67
68 // Loads a device state fragment JSON file. The file name (without extension)
69 // is used as the state fragment category.
70 bool LoadStateDefinition(const base::FilePath& json_file_path,
71 chromeos::ErrorPtr* error);
72
73 // Loads the base device state fragment JSON file. This state fragment
74 // defines the standard state properties from the 'base' package as defined
75 // by GCD specification.
76 bool LoadBaseStateDefinition(const base::FilePath& json_file_path,
77 chromeos::ErrorPtr* error);
78
79 // Loads state default values from JSON object.
80 bool LoadStateDefaults(const base::DictionaryValue& json,
81 chromeos::ErrorPtr* error);
82
83 // Loads state default values from JSON file.
84 bool LoadStateDefaults(const base::FilePath& json_file_path,
85 chromeos::ErrorPtr* error);
86
87 // Finds a package by its name. Returns nullptr if not found.
88 StatePackage* FindPackage(const std::string& package_name);
89 // Finds a package by its name. If none exists, one will be created.
90 StatePackage* FindOrCreatePackage(const std::string& package_name);
91
92 std::map<std::string, std::unique_ptr<StatePackage>> packages_;
93 std::set<std::string> categories_;
94
95 friend class StateManagerTest;
96 DISALLOW_COPY_AND_ASSIGN(StateManager);
97};
98
99} // namespace buffet
100
101#endif // BUFFET_STATES_STATE_MANAGER_H_