weaved: Remove brillo::Any from public weaved APIs
Replaced brillo::Any with base::Value in weaved client library
APIs in order to remove remaining dependency on D-Bus libraries.
BUG: 26558300
Change-Id: I8365743c31022a2f76a8a8768adf6ad19d509749
diff --git a/libweaved/command.cc b/libweaved/command.cc
index fa10955..fe49155 100644
--- a/libweaved/command.cc
+++ b/libweaved/command.cc
@@ -16,7 +16,6 @@
#include "android/weave/IWeaveCommand.h"
#include "common/binder_utils.h"
-#include "common/data_conversion.h"
using weaved::binder_utils::ParseDictionary;
using weaved::binder_utils::ToString;
@@ -120,29 +119,25 @@
return Command::Origin::kLocal;
}
-brillo::VariantDictionary Command::GetParameters() const {
- brillo::VariantDictionary params;
- android::String16 params_string16;
- if (binder_proxy_->getParameters(¶ms_string16).isOk()) {
- std::unique_ptr<base::DictionaryValue> dict;
- if (ParseDictionary(params_string16, &dict).isOk())
- params = details::DictionaryValueToVariantDictionary(*dict);
+const base::DictionaryValue& Command::GetParameters() const {
+ if (!parameter_cache_) {
+ android::String16 params_string16;
+ if (!binder_proxy_->getParameters(¶ms_string16).isOk() ||
+ !ParseDictionary(params_string16, ¶meter_cache_).isOk()) {
+ parameter_cache_.reset(new base::DictionaryValue);
+ }
}
- return params;
+ return *parameter_cache_;
}
-bool Command::SetProgress(const brillo::VariantDictionary& progress,
+bool Command::SetProgress(const base::DictionaryValue& progress,
brillo::ErrorPtr* error) {
- auto dict = details::VariantDictionaryToDictionaryValue(progress, error);
- return dict && StatusToError(binder_proxy_->setProgress(ToString16(*dict)),
- error);
+ return StatusToError(binder_proxy_->setProgress(ToString16(progress)), error);
}
-bool Command::Complete(const brillo::VariantDictionary& results,
+bool Command::Complete(const base::DictionaryValue& results,
brillo::ErrorPtr* error) {
- auto dict = details::VariantDictionaryToDictionaryValue(results, error);
- return dict && StatusToError(binder_proxy_->complete(ToString16(*dict)),
- error);
+ return StatusToError(binder_proxy_->complete(ToString16(results)), error);
}
bool Command::Abort(const std::string& error_code,
diff --git a/libweaved/command.h b/libweaved/command.h
index 4f7bf7c..518438c 100644
--- a/libweaved/command.h
+++ b/libweaved/command.h
@@ -20,7 +20,7 @@
#include <base/macros.h>
#include <binder/Status.h>
#include <brillo/errors/error.h>
-#include <brillo/variant_dictionary.h>
+#include <brillo/value_conversion.h>
#include <libweaved/export.h>
#include <utils/StrongPointer.h>
@@ -34,30 +34,6 @@
class ServiceImpl;
-namespace detail {
-
-// Helper function for Command::GetParameter<T>. Allows specialization.
-template <typename T>
-inline bool GetValue(const brillo::Any& any, T* value) {
- return any.GetValue<T>(value);
-}
-
-// Specialization for double, allow to extract a double from an int.
-template <>
-inline bool GetValue<double>(const brillo::Any& any, double* value) {
- if (any.GetValue<double>(value))
- return true;
-
- int int_val = 0;
- if (!any.GetValue<int>(&int_val))
- return false;
-
- *value = static_cast<double>(int_val);
- return true;
-}
-
-} // namespace detail
-
class LIBWEAVED_EXPORT Command final {
public:
enum class State {
@@ -91,7 +67,7 @@
Command::Origin GetOrigin() const;
// Returns the command parameters.
- brillo::VariantDictionary GetParameters() const;
+ const base::DictionaryValue& GetParameters() const;
// Helper function to get a command parameter of particular type T from the
// command parameter list. Returns default value for type T (e.g. 0 for int or
@@ -99,23 +75,23 @@
// is of incorrect type.
template <typename T>
T GetParameter(const std::string& name) const {
- const brillo::VariantDictionary& parameters = GetParameters();
- T value{};
- auto p = parameters.find(name);
- if (p != parameters.end())
- detail::GetValue<T>(p->second, &value);
- return value;
+ const base::DictionaryValue& parameters = GetParameters();
+ T param_value{};
+ const base::Value* value = nullptr;
+ if (parameters.Get(name, &value))
+ brillo::FromValue(*value, ¶m_value);
+ return param_value;
}
// Updates the command progress. The |progress| should match the schema.
// Returns false if |progress| value is incorrect.
- bool SetProgress(const brillo::VariantDictionary& progress,
+ bool SetProgress(const base::DictionaryValue& progress,
brillo::ErrorPtr* error);
// Sets command into terminal "done" state.
// Updates the command results. The |results| should match the schema.
// Returns false if |results| value is incorrect.
- bool Complete(const brillo::VariantDictionary& results,
+ bool Complete(const base::DictionaryValue& results,
brillo::ErrorPtr* error);
// Aborts command execution.
@@ -166,6 +142,7 @@
private:
friend class ServiceImpl;
android::sp<android::weave::IWeaveCommand> binder_proxy_;
+ mutable std::unique_ptr<base::DictionaryValue> parameter_cache_;
DISALLOW_COPY_AND_ASSIGN(Command);
};
diff --git a/libweaved/service.cc b/libweaved/service.cc
index c48fb41..2f9db9c 100644
--- a/libweaved/service.cc
+++ b/libweaved/service.cc
@@ -29,7 +29,6 @@
#include "android/weave/IWeaveServiceManager.h"
#include "common/binder_constants.h"
#include "common/binder_utils.h"
-#include "common/data_conversion.h"
using weaved::binder_utils::StatusToError;
using weaved::binder_utils::ToString;
@@ -185,12 +184,12 @@
const std::string& command_name,
const CommandHandlerCallback& callback) override;
bool SetStateProperties(const std::string& component,
- const brillo::VariantDictionary& dict,
+ const base::DictionaryValue& dict,
brillo::ErrorPtr* error) override;
bool SetStateProperty(const std::string& component,
const std::string& trait_name,
const std::string& property_name,
- const brillo::Any& value,
+ const base::Value& value,
brillo::ErrorPtr* error) override;
void SetPairingInfoListener(const PairingInfoCallback& callback) override;
@@ -337,27 +336,25 @@
}
bool ServiceImpl::SetStateProperties(const std::string& component,
- const brillo::VariantDictionary& dict,
+ const base::DictionaryValue& dict,
brillo::ErrorPtr* error) {
CHECK(!component.empty());
CHECK(weave_service_.get());
- auto properties = details::VariantDictionaryToDictionaryValue(dict, error);
- if (!properties)
- return false;
return StatusToError(weave_service_->updateState(ToString16(component),
- ToString16(*properties)),
+ ToString16(dict)),
error);
}
bool ServiceImpl::SetStateProperty(const std::string& component,
const std::string& trait_name,
const std::string& property_name,
- const brillo::Any& value,
+ const base::Value& value,
brillo::ErrorPtr* error) {
std::string name =
base::StringPrintf("%s.%s", trait_name.c_str(), property_name.c_str());
- return SetStateProperties(component, brillo::VariantDictionary{{name, value}},
- error);
+ base::DictionaryValue dict;
+ dict.Set(name, value.DeepCopy());
+ return SetStateProperties(component, dict, error);
}
void ServiceImpl::SetPairingInfoListener(const PairingInfoCallback& callback) {
diff --git a/libweaved/service.h b/libweaved/service.h
index 02cc509..26edb7a 100644
--- a/libweaved/service.h
+++ b/libweaved/service.h
@@ -74,7 +74,7 @@
// Sets a number of state properties for a given |component|.
// |dict| is a dictionary containing property-name/property-value pairs.
virtual bool SetStateProperties(const std::string& component,
- const brillo::VariantDictionary& dict,
+ const base::DictionaryValue& dict,
brillo::ErrorPtr* error) = 0;
// Sets value of the single property.
@@ -84,7 +84,7 @@
virtual bool SetStateProperty(const std::string& component,
const std::string& trait_name,
const std::string& property_name,
- const brillo::Any& value,
+ const base::Value& value,
brillo::ErrorPtr* error) = 0;
// Specifies a callback to be invoked when the device enters/exist pairing