blob: 2555ab500b39c9b1ad8c28af7524ee8c5dc63f15 [file] [log] [blame]
// Copyright 2014 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <map>
#include <memory>
#include <set>
#include <string>
#include <base/macros.h>
#include <chromeos/errors/error.h>
#include <chromeos/variant_dictionary.h>
#include "buffet/states/state_package.h"
namespace base {
class DictionaryValue;
class FilePath;
} // namespace base
namespace buffet {
// StateManager is the class that aggregates the device state fragments
// provided by device daemons and makes the aggregate device state available
// to the GCD cloud server and local clients.
class StateManager final {
StateManager() = default;
// Initializes the state manager and load device state fragments.
// Called by Buffet daemon at startup.
void Startup();
// Returns aggregated state properties across all registered packages as
// a JSON object that can be used to send the device state to the GCD server.
std::unique_ptr<base::DictionaryValue> GetStateValuesAsJson(
chromeos::ErrorPtr* error) const;
// Updates a single property value. |full_property_name| must be the full
// name of the property to update in format "".
bool SetPropertyValue(const std::string& full_property_name,
const chromeos::Any& value,
chromeos::ErrorPtr* error);
// Updates a number of state properties in one shot.
// |property_set| is a (full_property_name)-to-(property_value) map.
bool UpdateProperties(const chromeos::VariantDictionary& property_set,
chromeos::ErrorPtr* error);
// Returns all the categories the state properties are registered from.
// As with GCD command handling, the category normally represent a device
// service (daemon) that is responsible for a set of properties.
const std::set<std::string>& GetCategories() const {
return categories_;
// Loads a device state fragment from a JSON object. |category| represents
// a device daemon providing the state fragment or empty string for the
// base state fragment.
bool LoadStateDefinition(const base::DictionaryValue& json,
const std::string& category,
chromeos::ErrorPtr* error);
// Loads a device state fragment JSON file. The file name (without extension)
// is used as the state fragment category.
bool LoadStateDefinition(const base::FilePath& json_file_path,
chromeos::ErrorPtr* error);
// Loads the base device state fragment JSON file. This state fragment
// defines the standard state properties from the 'base' package as defined
// by GCD specification.
bool LoadBaseStateDefinition(const base::FilePath& json_file_path,
chromeos::ErrorPtr* error);
// Loads state default values from JSON object.
bool LoadStateDefaults(const base::DictionaryValue& json,
chromeos::ErrorPtr* error);
// Loads state default values from JSON file.
bool LoadStateDefaults(const base::FilePath& json_file_path,
chromeos::ErrorPtr* error);
// Finds a package by its name. Returns nullptr if not found.
StatePackage* FindPackage(const std::string& package_name);
// Finds a package by its name. If none exists, one will be created.
StatePackage* FindOrCreatePackage(const std::string& package_name);
std::map<std::string, std::unique_ptr<StatePackage>> packages_;
std::set<std::string> categories_;
friend class StateManagerTest;
} // namespace buffet