blob: 55faf768d43b8e36e19239695b952100fa01d377 [file] [log] [blame]
Vitaly Buka4615e0d2015-10-14 15:35:12 -07001// Copyright 2015 The Weave Authors. All rights reserved.
Alex Vakulenko07216fe2014-09-19 15:31:09 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Vitaly Buka912b6982015-07-06 11:13:03 -07005#ifndef LIBWEAVE_SRC_STATES_STATE_MANAGER_H_
6#define LIBWEAVE_SRC_STATES_STATE_MANAGER_H_
Alex Vakulenko07216fe2014-09-19 15:31:09 -07007
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>
Vitaly Buka0801a1f2015-08-14 10:03:46 -070017#include <weave/error.h>
Alex Vakulenko07216fe2014-09-19 15:31:09 -070018
Stefan Sauer2d16dfa2015-09-25 17:08:35 +020019#include "src/states/state_change_queue_interface.h"
20#include "src/states/state_package.h"
Alex Vakulenko07216fe2014-09-19 15:31:09 -070021
22namespace base {
23class DictionaryValue;
Alex Vakulenkoff73cf22014-10-29 09:53:52 -070024class Time;
Alex Vakulenko07216fe2014-09-19 15:31:09 -070025} // namespace base
26
Vitaly Bukab6f015a2015-07-09 14:59:23 -070027namespace weave {
Alex Vakulenko07216fe2014-09-19 15:31:09 -070028
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 Buka4c981352015-10-01 23:04:24 -070032class StateManager final {
Alex Vakulenko07216fe2014-09-19 15:31:09 -070033 public:
Alex Vakulenko57123b22014-10-28 13:50:16 -070034 explicit StateManager(StateChangeQueueInterface* state_change_queue);
Vitaly Buka4c981352015-10-01 23:04:24 -070035 ~StateManager();
Alex Vakulenko07216fe2014-09-19 15:31:09 -070036
Vitaly Buka4c981352015-10-01 23:04:24 -070037 void AddChangedCallback(const base::Closure& callback);
Vitaly Buka77c2bff2015-10-06 18:31:20 -070038 bool LoadStateDefinition(const base::DictionaryValue& dict, ErrorPtr* error);
Vitaly Buka216e86d2015-10-06 20:23:02 -070039 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 Buka4c981352015-10-01 23:04:24 -070042 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 Buka247620b2015-05-26 15:42:20 -070047
Alex Vakulenko57123b22014-10-28 13:50:16 -070048 // Returns the recorded state changes since last time this method has been
49 // called.
Alex Vakulenkobe4254b2015-06-26 11:34:03 -070050 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 Vakulenko57123b22014-10-28 13:50:16 -070060
Alex Vakulenko07216fe2014-09-19 15:31:09 -070061 private:
Vitaly Buka2f7efdb2015-05-27 16:00:21 -070062 friend class BaseApiHandlerTest;
Vitaly Buka247620b2015-05-26 15:42:20 -070063 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 Bukae527a642015-07-28 21:39:45 -070068 const base::Value& value,
Vitaly Buka247620b2015-05-26 15:42:20 -070069 const base::Time& timestamp,
Vitaly Buka0801a1f2015-08-14 10:03:46 -070070 ErrorPtr* error);
Vitaly Buka247620b2015-05-26 15:42:20 -070071
Alex Vakulenko07216fe2014-09-19 15:31:09 -070072 // Finds a package by its name. Returns nullptr if not found.
73 StatePackage* FindPackage(const std::string& package_name);
Vitaly Buka4c981352015-10-01 23:04:24 -070074 const StatePackage* FindPackage(const std::string& package_name) const;
Alex Vakulenko07216fe2014-09-19 15:31:09 -070075 // Finds a package by its name. If none exists, one will be created.
76 StatePackage* FindOrCreatePackage(const std::string& package_name);
77
Vitaly Bukab6f015a2015-07-09 14:59:23 -070078 StateChangeQueueInterface* state_change_queue_; // Owned by Manager.
Alex Vakulenko07216fe2014-09-19 15:31:09 -070079 std::map<std::string, std::unique_ptr<StatePackage>> packages_;
Alex Vakulenko07216fe2014-09-19 15:31:09 -070080
Vitaly Buka247620b2015-05-26 15:42:20 -070081 std::vector<base::Closure> on_changed_;
82
Alex Vakulenko07216fe2014-09-19 15:31:09 -070083 DISALLOW_COPY_AND_ASSIGN(StateManager);
84};
85
Vitaly Bukab6f015a2015-07-09 14:59:23 -070086} // namespace weave
Alex Vakulenko07216fe2014-09-19 15:31:09 -070087
Vitaly Buka912b6982015-07-06 11:13:03 -070088#endif // LIBWEAVE_SRC_STATES_STATE_MANAGER_H_