blob: a34f7c1fdf7921c88560b0e95775d977124a7937 [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>
Alex Vakulenko57123b22014-10-28 13:50:16 -070012#include <vector>
Alex Vakulenko07216fe2014-09-19 15:31:09 -070013
Vitaly Buka247620b2015-05-26 15:42:20 -070014#include <base/callback.h>
Alex Vakulenko07216fe2014-09-19 15:31:09 -070015#include <base/macros.h>
Alex Vakulenko07216fe2014-09-19 15:31:09 -070016#include <chromeos/errors/error.h>
Alex Vakulenko576c9792014-09-22 16:49:45 -070017#include <chromeos/variant_dictionary.h>
Alex Vakulenko07216fe2014-09-19 15:31:09 -070018
Alex Vakulenko57123b22014-10-28 13:50:16 -070019#include "buffet/states/state_change_queue_interface.h"
Alex Vakulenko07216fe2014-09-19 15:31:09 -070020#include "buffet/states/state_package.h"
21
22namespace base {
23class DictionaryValue;
24class FilePath;
Alex Vakulenkoff73cf22014-10-29 09:53:52 -070025class Time;
Alex Vakulenko07216fe2014-09-19 15:31:09 -070026} // namespace base
27
28namespace buffet {
29
30// StateManager is the class that aggregates the device state fragments
31// provided by device daemons and makes the aggregate device state available
32// to the GCD cloud server and local clients.
33class StateManager final {
34 public:
Alex Vakulenko57123b22014-10-28 13:50:16 -070035 explicit StateManager(StateChangeQueueInterface* state_change_queue);
Alex Vakulenko07216fe2014-09-19 15:31:09 -070036
Vitaly Buka247620b2015-05-26 15:42:20 -070037 void AddOnChangedCallback(const base::Closure& callback);
38
Alex Vakulenko07216fe2014-09-19 15:31:09 -070039 // Initializes the state manager and load device state fragments.
40 // Called by Buffet daemon at startup.
41 void Startup();
42
43 // Returns aggregated state properties across all registered packages as
44 // a JSON object that can be used to send the device state to the GCD server.
45 std::unique_ptr<base::DictionaryValue> GetStateValuesAsJson(
46 chromeos::ErrorPtr* error) const;
47
Vitaly Buka247620b2015-05-26 15:42:20 -070048 // Updates a multiple property values.
49 bool SetProperties(const chromeos::VariantDictionary& property_set,
50 chromeos::ErrorPtr* error);
Alex Vakulenko07216fe2014-09-19 15:31:09 -070051
52 // Returns all the categories the state properties are registered from.
53 // As with GCD command handling, the category normally represent a device
54 // service (daemon) that is responsible for a set of properties.
55 const std::set<std::string>& GetCategories() const {
56 return categories_;
57 }
58
Alex Vakulenko57123b22014-10-28 13:50:16 -070059 // Returns the recorded state changes since last time this method has been
60 // called.
61 std::vector<StateChange> GetAndClearRecordedStateChanges();
62
Alex Vakulenko07216fe2014-09-19 15:31:09 -070063 private:
Vitaly Buka2f7efdb2015-05-27 16:00:21 -070064 friend class BaseApiHandlerTest;
Vitaly Buka247620b2015-05-26 15:42:20 -070065 friend class StateManagerTest;
66
67 // Updates a single property value. |full_property_name| must be the full
68 // name of the property to update in format "package.property".
69 bool SetPropertyValue(const std::string& full_property_name,
70 const chromeos::Any& value,
71 const base::Time& timestamp,
72 chromeos::ErrorPtr* error);
73
Alex Vakulenko07216fe2014-09-19 15:31:09 -070074 // Loads a device state fragment from a JSON object. |category| represents
75 // a device daemon providing the state fragment or empty string for the
76 // base state fragment.
77 bool LoadStateDefinition(const base::DictionaryValue& json,
78 const std::string& category,
79 chromeos::ErrorPtr* error);
80
81 // Loads a device state fragment JSON file. The file name (without extension)
82 // is used as the state fragment category.
83 bool LoadStateDefinition(const base::FilePath& json_file_path,
84 chromeos::ErrorPtr* error);
85
86 // Loads the base device state fragment JSON file. This state fragment
87 // defines the standard state properties from the 'base' package as defined
88 // by GCD specification.
89 bool LoadBaseStateDefinition(const base::FilePath& json_file_path,
90 chromeos::ErrorPtr* error);
91
92 // Loads state default values from JSON object.
93 bool LoadStateDefaults(const base::DictionaryValue& json,
94 chromeos::ErrorPtr* error);
95
96 // Loads state default values from JSON file.
97 bool LoadStateDefaults(const base::FilePath& json_file_path,
98 chromeos::ErrorPtr* error);
99
100 // Finds a package by its name. Returns nullptr if not found.
101 StatePackage* FindPackage(const std::string& package_name);
102 // Finds a package by its name. If none exists, one will be created.
103 StatePackage* FindOrCreatePackage(const std::string& package_name);
104
Alex Vakulenko57123b22014-10-28 13:50:16 -0700105 StateChangeQueueInterface* state_change_queue_; // Owned by buffet::Manager.
Alex Vakulenko07216fe2014-09-19 15:31:09 -0700106 std::map<std::string, std::unique_ptr<StatePackage>> packages_;
107 std::set<std::string> categories_;
108
Vitaly Buka247620b2015-05-26 15:42:20 -0700109 std::vector<base::Closure> on_changed_;
110
Alex Vakulenko07216fe2014-09-19 15:31:09 -0700111 DISALLOW_COPY_AND_ASSIGN(StateManager);
112};
113
114} // namespace buffet
115
116#endif // BUFFET_STATES_STATE_MANAGER_H_