blob: 914daeb7ab0002c522ce1e6a3cc68af7ff782aa4 [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 Vakulenkobe4254b2015-06-26 11:34:03 -070012#include <utility>
Alex Vakulenko57123b22014-10-28 13:50:16 -070013#include <vector>
Alex Vakulenko07216fe2014-09-19 15:31:09 -070014
Vitaly Buka247620b2015-05-26 15:42:20 -070015#include <base/callback.h>
Alex Vakulenko07216fe2014-09-19 15:31:09 -070016#include <base/macros.h>
Alex Vakulenko07216fe2014-09-19 15:31:09 -070017#include <chromeos/errors/error.h>
Alex Vakulenko576c9792014-09-22 16:49:45 -070018#include <chromeos/variant_dictionary.h>
Alex Vakulenko07216fe2014-09-19 15:31:09 -070019
Alex Vakulenko57123b22014-10-28 13:50:16 -070020#include "buffet/states/state_change_queue_interface.h"
Alex Vakulenko07216fe2014-09-19 15:31:09 -070021#include "buffet/states/state_package.h"
22
23namespace base {
24class DictionaryValue;
25class FilePath;
Alex Vakulenkoff73cf22014-10-29 09:53:52 -070026class Time;
Alex Vakulenko07216fe2014-09-19 15:31:09 -070027} // namespace base
28
29namespace buffet {
30
31// StateManager is the class that aggregates the device state fragments
32// provided by device daemons and makes the aggregate device state available
33// to the GCD cloud server and local clients.
34class StateManager final {
35 public:
Alex Vakulenko57123b22014-10-28 13:50:16 -070036 explicit StateManager(StateChangeQueueInterface* state_change_queue);
Alex Vakulenko07216fe2014-09-19 15:31:09 -070037
Vitaly Buka247620b2015-05-26 15:42:20 -070038 void AddOnChangedCallback(const base::Closure& callback);
39
Alex Vakulenko07216fe2014-09-19 15:31:09 -070040 // Initializes the state manager and load device state fragments.
41 // Called by Buffet daemon at startup.
42 void Startup();
43
44 // Returns aggregated state properties across all registered packages as
45 // a JSON object that can be used to send the device state to the GCD server.
46 std::unique_ptr<base::DictionaryValue> GetStateValuesAsJson(
47 chromeos::ErrorPtr* error) const;
48
Vitaly Buka247620b2015-05-26 15:42:20 -070049 // Updates a multiple property values.
50 bool SetProperties(const chromeos::VariantDictionary& property_set,
51 chromeos::ErrorPtr* error);
Alex Vakulenko07216fe2014-09-19 15:31:09 -070052
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
Alex Vakulenko57123b22014-10-28 13:50:16 -070060 // Returns the recorded state changes since last time this method has been
61 // called.
Alex Vakulenkobe4254b2015-06-26 11:34:03 -070062 std::pair<StateChangeQueueInterface::UpdateID, std::vector<StateChange>>
63 GetAndClearRecordedStateChanges();
64
65 // Called to notify that the state patch with |id| has been successfully sent
66 // to the server and processed.
67 void NotifyStateUpdatedOnServer(StateChangeQueueInterface::UpdateID id);
68
69 StateChangeQueueInterface* GetStateChangeQueue() const {
70 return state_change_queue_;
71 }
Alex Vakulenko57123b22014-10-28 13:50:16 -070072
Alex Vakulenko07216fe2014-09-19 15:31:09 -070073 private:
Vitaly Buka2f7efdb2015-05-27 16:00:21 -070074 friend class BaseApiHandlerTest;
Vitaly Buka247620b2015-05-26 15:42:20 -070075 friend class StateManagerTest;
76
77 // Updates a single property value. |full_property_name| must be the full
78 // name of the property to update in format "package.property".
79 bool SetPropertyValue(const std::string& full_property_name,
80 const chromeos::Any& value,
81 const base::Time& timestamp,
82 chromeos::ErrorPtr* error);
83
Alex Vakulenko07216fe2014-09-19 15:31:09 -070084 // Loads a device state fragment from a JSON object. |category| represents
85 // a device daemon providing the state fragment or empty string for the
86 // base state fragment.
87 bool LoadStateDefinition(const base::DictionaryValue& json,
88 const std::string& category,
89 chromeos::ErrorPtr* error);
90
91 // Loads a device state fragment JSON file. The file name (without extension)
92 // is used as the state fragment category.
93 bool LoadStateDefinition(const base::FilePath& json_file_path,
94 chromeos::ErrorPtr* error);
95
96 // Loads the base device state fragment JSON file. This state fragment
97 // defines the standard state properties from the 'base' package as defined
98 // by GCD specification.
99 bool LoadBaseStateDefinition(const base::FilePath& json_file_path,
100 chromeos::ErrorPtr* error);
101
102 // Loads state default values from JSON object.
103 bool LoadStateDefaults(const base::DictionaryValue& json,
104 chromeos::ErrorPtr* error);
105
106 // Loads state default values from JSON file.
107 bool LoadStateDefaults(const base::FilePath& json_file_path,
108 chromeos::ErrorPtr* error);
109
110 // Finds a package by its name. Returns nullptr if not found.
111 StatePackage* FindPackage(const std::string& package_name);
112 // Finds a package by its name. If none exists, one will be created.
113 StatePackage* FindOrCreatePackage(const std::string& package_name);
114
Alex Vakulenko57123b22014-10-28 13:50:16 -0700115 StateChangeQueueInterface* state_change_queue_; // Owned by buffet::Manager.
Alex Vakulenko07216fe2014-09-19 15:31:09 -0700116 std::map<std::string, std::unique_ptr<StatePackage>> packages_;
117 std::set<std::string> categories_;
118
Vitaly Buka247620b2015-05-26 15:42:20 -0700119 std::vector<base::Closure> on_changed_;
120
Alex Vakulenko07216fe2014-09-19 15:31:09 -0700121 DISALLOW_COPY_AND_ASSIGN(StateManager);
122};
123
124} // namespace buffet
125
126#endif // BUFFET_STATES_STATE_MANAGER_H_