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 | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame^] | 12 | #include <utility> |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 13 | #include <vector> |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 14 | |
Vitaly Buka | 247620b | 2015-05-26 15:42:20 -0700 | [diff] [blame] | 15 | #include <base/callback.h> |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 16 | #include <base/macros.h> |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 17 | #include <chromeos/errors/error.h> |
Alex Vakulenko | 576c979 | 2014-09-22 16:49:45 -0700 | [diff] [blame] | 18 | #include <chromeos/variant_dictionary.h> |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 19 | |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 20 | #include "buffet/states/state_change_queue_interface.h" |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 21 | #include "buffet/states/state_package.h" |
| 22 | |
| 23 | namespace base { |
| 24 | class DictionaryValue; |
| 25 | class FilePath; |
Alex Vakulenko | ff73cf2 | 2014-10-29 09:53:52 -0700 | [diff] [blame] | 26 | class Time; |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 27 | } // namespace base |
| 28 | |
| 29 | namespace 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. |
| 34 | class StateManager final { |
| 35 | public: |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 36 | explicit StateManager(StateChangeQueueInterface* state_change_queue); |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 37 | |
Vitaly Buka | 247620b | 2015-05-26 15:42:20 -0700 | [diff] [blame] | 38 | void AddOnChangedCallback(const base::Closure& callback); |
| 39 | |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 40 | // 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 Buka | 247620b | 2015-05-26 15:42:20 -0700 | [diff] [blame] | 49 | // Updates a multiple property values. |
| 50 | bool SetProperties(const chromeos::VariantDictionary& property_set, |
| 51 | chromeos::ErrorPtr* error); |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 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 | |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 60 | // Returns the recorded state changes since last time this method has been |
| 61 | // called. |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame^] | 62 | 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 Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 72 | |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 73 | private: |
Vitaly Buka | 2f7efdb | 2015-05-27 16:00:21 -0700 | [diff] [blame] | 74 | friend class BaseApiHandlerTest; |
Vitaly Buka | 247620b | 2015-05-26 15:42:20 -0700 | [diff] [blame] | 75 | 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 Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 84 | // 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 Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 115 | StateChangeQueueInterface* state_change_queue_; // Owned by buffet::Manager. |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 116 | std::map<std::string, std::unique_ptr<StatePackage>> packages_; |
| 117 | std::set<std::string> categories_; |
| 118 | |
Vitaly Buka | 247620b | 2015-05-26 15:42:20 -0700 | [diff] [blame] | 119 | std::vector<base::Closure> on_changed_; |
| 120 | |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 121 | DISALLOW_COPY_AND_ASSIGN(StateManager); |
| 122 | }; |
| 123 | |
| 124 | } // namespace buffet |
| 125 | |
| 126 | #endif // BUFFET_STATES_STATE_MANAGER_H_ |