Vitaly Buka | 4615e0d | 2015-10-14 15:35:12 -0700 | [diff] [blame] | 1 | // Copyright 2015 The Weave Authors. All rights reserved. |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Vitaly Buka | 912b698 | 2015-07-06 11:13:03 -0700 | [diff] [blame] | 5 | #ifndef LIBWEAVE_SRC_STATES_STATE_MANAGER_H_ |
| 6 | #define LIBWEAVE_SRC_STATES_STATE_MANAGER_H_ |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 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> |
Vitaly Buka | 0801a1f | 2015-08-14 10:03:46 -0700 | [diff] [blame] | 17 | #include <weave/error.h> |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 18 | |
Stefan Sauer | 2d16dfa | 2015-09-25 17:08:35 +0200 | [diff] [blame] | 19 | #include "src/states/state_change_queue_interface.h" |
| 20 | #include "src/states/state_package.h" |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 21 | |
| 22 | namespace base { |
| 23 | class DictionaryValue; |
Alex Vakulenko | ff73cf2 | 2014-10-29 09:53:52 -0700 | [diff] [blame] | 24 | class Time; |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 25 | } // namespace base |
| 26 | |
Vitaly Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 27 | namespace weave { |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 28 | |
| 29 | // StateManager is the class that aggregates the device state fragments |
| 30 | // provided by device daemons and makes the aggregate device state available |
| 31 | // to the GCD cloud server and local clients. |
Vitaly Buka | 4c98135 | 2015-10-01 23:04:24 -0700 | [diff] [blame] | 32 | class StateManager final { |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 33 | public: |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 34 | explicit StateManager(StateChangeQueueInterface* state_change_queue); |
Vitaly Buka | 4c98135 | 2015-10-01 23:04:24 -0700 | [diff] [blame] | 35 | ~StateManager(); |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 36 | |
Vitaly Buka | 4c98135 | 2015-10-01 23:04:24 -0700 | [diff] [blame] | 37 | void AddChangedCallback(const base::Closure& callback); |
Vitaly Buka | 77c2bff | 2015-10-06 18:31:20 -0700 | [diff] [blame] | 38 | bool LoadStateDefinition(const base::DictionaryValue& dict, ErrorPtr* error); |
Vitaly Buka | 216e86d | 2015-10-06 20:23:02 -0700 | [diff] [blame] | 39 | bool LoadStateDefinitionFromJson(const std::string& json, ErrorPtr* error); |
| 40 | bool SetProperties(const base::DictionaryValue& dict, ErrorPtr* error); |
| 41 | bool SetPropertiesFromJson(const std::string& json, ErrorPtr* error); |
Vitaly Buka | 4c98135 | 2015-10-01 23:04:24 -0700 | [diff] [blame] | 42 | std::unique_ptr<base::Value> GetProperty(const std::string& name) const; |
| 43 | bool SetProperty(const std::string& name, |
| 44 | const base::Value& value, |
| 45 | ErrorPtr* error); |
| 46 | std::unique_ptr<base::DictionaryValue> GetState() const; |
Vitaly Buka | 247620b | 2015-05-26 15:42:20 -0700 | [diff] [blame] | 47 | |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 48 | // Returns the recorded state changes since last time this method has been |
| 49 | // called. |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 50 | std::pair<StateChangeQueueInterface::UpdateID, std::vector<StateChange>> |
| 51 | GetAndClearRecordedStateChanges(); |
| 52 | |
| 53 | // Called to notify that the state patch with |id| has been successfully sent |
| 54 | // to the server and processed. |
| 55 | void NotifyStateUpdatedOnServer(StateChangeQueueInterface::UpdateID id); |
| 56 | |
| 57 | StateChangeQueueInterface* GetStateChangeQueue() const { |
| 58 | return state_change_queue_; |
| 59 | } |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 60 | |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 61 | private: |
Vitaly Buka | 2f7efdb | 2015-05-27 16:00:21 -0700 | [diff] [blame] | 62 | friend class BaseApiHandlerTest; |
Vitaly Buka | 247620b | 2015-05-26 15:42:20 -0700 | [diff] [blame] | 63 | friend class StateManagerTest; |
| 64 | |
| 65 | // Updates a single property value. |full_property_name| must be the full |
| 66 | // name of the property to update in format "package.property". |
| 67 | bool SetPropertyValue(const std::string& full_property_name, |
Vitaly Buka | e527a64 | 2015-07-28 21:39:45 -0700 | [diff] [blame] | 68 | const base::Value& value, |
Vitaly Buka | 247620b | 2015-05-26 15:42:20 -0700 | [diff] [blame] | 69 | const base::Time& timestamp, |
Vitaly Buka | 0801a1f | 2015-08-14 10:03:46 -0700 | [diff] [blame] | 70 | ErrorPtr* error); |
Vitaly Buka | 247620b | 2015-05-26 15:42:20 -0700 | [diff] [blame] | 71 | |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 72 | // Finds a package by its name. Returns nullptr if not found. |
| 73 | StatePackage* FindPackage(const std::string& package_name); |
Vitaly Buka | 4c98135 | 2015-10-01 23:04:24 -0700 | [diff] [blame] | 74 | const StatePackage* FindPackage(const std::string& package_name) const; |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 75 | // Finds a package by its name. If none exists, one will be created. |
| 76 | StatePackage* FindOrCreatePackage(const std::string& package_name); |
| 77 | |
Vitaly Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 78 | StateChangeQueueInterface* state_change_queue_; // Owned by Manager. |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 79 | std::map<std::string, std::unique_ptr<StatePackage>> packages_; |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 80 | |
Vitaly Buka | 247620b | 2015-05-26 15:42:20 -0700 | [diff] [blame] | 81 | std::vector<base::Closure> on_changed_; |
| 82 | |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 83 | DISALLOW_COPY_AND_ASSIGN(StateManager); |
| 84 | }; |
| 85 | |
Vitaly Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 86 | } // namespace weave |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 87 | |
Vitaly Buka | 912b698 | 2015-07-06 11:13:03 -0700 | [diff] [blame] | 88 | #endif // LIBWEAVE_SRC_STATES_STATE_MANAGER_H_ |