Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 1 | // 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 Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 12 | #include <vector> |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 13 | |
Vitaly Buka | 247620b | 2015-05-26 15:42:20 -0700 | [diff] [blame] | 14 | #include <base/callback.h> |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 15 | #include <base/macros.h> |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 16 | #include <chromeos/errors/error.h> |
Alex Vakulenko | 576c979 | 2014-09-22 16:49:45 -0700 | [diff] [blame] | 17 | #include <chromeos/variant_dictionary.h> |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 18 | |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 19 | #include "buffet/states/state_change_queue_interface.h" |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 20 | #include "buffet/states/state_package.h" |
| 21 | |
| 22 | namespace base { |
| 23 | class DictionaryValue; |
| 24 | class FilePath; |
Alex Vakulenko | ff73cf2 | 2014-10-29 09:53:52 -0700 | [diff] [blame] | 25 | class Time; |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 26 | } // namespace base |
| 27 | |
| 28 | namespace 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. |
| 33 | class StateManager final { |
| 34 | public: |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 35 | explicit StateManager(StateChangeQueueInterface* state_change_queue); |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 36 | |
Vitaly Buka | 247620b | 2015-05-26 15:42:20 -0700 | [diff] [blame] | 37 | void AddOnChangedCallback(const base::Closure& callback); |
| 38 | |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 39 | // 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 Buka | 247620b | 2015-05-26 15:42:20 -0700 | [diff] [blame] | 48 | // Updates a multiple property values. |
| 49 | bool SetProperties(const chromeos::VariantDictionary& property_set, |
| 50 | chromeos::ErrorPtr* error); |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 51 | |
| 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 Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 59 | // Returns the recorded state changes since last time this method has been |
| 60 | // called. |
| 61 | std::vector<StateChange> GetAndClearRecordedStateChanges(); |
| 62 | |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 63 | private: |
Vitaly Buka | 2f7efdb | 2015-05-27 16:00:21 -0700 | [diff] [blame] | 64 | friend class BaseApiHandlerTest; |
Vitaly Buka | 247620b | 2015-05-26 15:42:20 -0700 | [diff] [blame] | 65 | 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 Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 74 | // 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 Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 105 | StateChangeQueueInterface* state_change_queue_; // Owned by buffet::Manager. |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 106 | std::map<std::string, std::unique_ptr<StatePackage>> packages_; |
| 107 | std::set<std::string> categories_; |
| 108 | |
Vitaly Buka | 247620b | 2015-05-26 15:42:20 -0700 | [diff] [blame] | 109 | std::vector<base::Closure> on_changed_; |
| 110 | |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 111 | DISALLOW_COPY_AND_ASSIGN(StateManager); |
| 112 | }; |
| 113 | |
| 114 | } // namespace buffet |
| 115 | |
| 116 | #endif // BUFFET_STATES_STATE_MANAGER_H_ |