From e9a8385bccf042c9e80b69de18f3b58179309615 Mon Sep 17 00:00:00 2001 From: Alex Vakulenko Date: Thu, 28 Jan 2016 14:34:04 -0800 Subject: 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 --- Android.mk | 10 +- common/binder_utils.cc | 1 + common/binder_utils.h | 6 +- common/data_conversion.cc | 263 ------------------------------------- common/data_conversion.h | 38 ------ common/data_conversion_unittest.cc | 200 ---------------------------- libweaved/command.cc | 29 ++-- libweaved/command.h | 45 ++----- libweaved/service.cc | 19 ++- libweaved/service.h | 4 +- 10 files changed, 46 insertions(+), 569 deletions(-) delete mode 100644 common/data_conversion.cc delete mode 100644 common/data_conversion.h delete mode 100644 common/data_conversion_unittest.cc diff --git a/Android.mk b/Android.mk index d1b171d..7f2e483 100644 --- a/Android.mk +++ b/Android.mk @@ -84,7 +84,6 @@ LOCAL_SRC_FILES := \ brillo/android/weave/IWeaveServiceManagerNotificationListener.aidl \ common/binder_constants.cc \ common/binder_utils.cc \ - common/data_conversion.cc \ include $(BUILD_STATIC_LIBRARY) @@ -165,7 +164,13 @@ LOCAL_CFLAGS := $(buffetCommonCFlags) LOCAL_CPPFLAGS := $(buffetCommonCppFlags) LOCAL_C_INCLUDES := external/gtest/include LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH) -LOCAL_SHARED_LIBRARIES := $(buffetSharedLibraries) +LOCAL_SHARED_LIBRARIES := \ + libbinder \ + libbinderwrapper \ + libbrillo \ + libchrome \ + libutils \ + LOCAL_STATIC_LIBRARIES := weave-common LOCAL_CLANG := true @@ -206,6 +211,5 @@ LOCAL_SRC_FILES := \ buffet/binder_command_proxy_unittest.cc \ buffet/buffet_config_unittest.cc \ buffet/buffet_testrunner.cc \ - common/data_conversion_unittest.cc \ include $(BUILD_NATIVE_TEST) diff --git a/common/binder_utils.cc b/common/binder_utils.cc index 6f66040..641019a 100644 --- a/common/binder_utils.cc +++ b/common/binder_utils.cc @@ -16,6 +16,7 @@ #include #include +#include namespace weaved { namespace binder_utils { diff --git a/common/binder_utils.h b/common/binder_utils.h index 65b462d..3aa5eda 100644 --- a/common/binder_utils.h +++ b/common/binder_utils.h @@ -22,9 +22,13 @@ #include #include #include -#include #include +namespace weave { +class Error; // Forward declaration. +using ErrorPtr = std::unique_ptr; +} // namespace weave + namespace weaved { namespace binder_utils { diff --git a/common/data_conversion.cc b/common/data_conversion.cc deleted file mode 100644 index fd20ad4..0000000 --- a/common/data_conversion.cc +++ /dev/null @@ -1,263 +0,0 @@ -// Copyright 2015 The Android Open Source Project -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "common/data_conversion.h" - -#include -#include - -#include - -namespace weaved { - -namespace { - -// Helpers for JsonToAny(). -template -brillo::Any ValueToAny(const base::Value& json, - bool (base::Value::*fnc)(T*) const) { - T val; - CHECK((json.*fnc)(&val)); - return val; -} - -brillo::Any ValueToAny(const base::Value& json); - -template -brillo::Any ListToAny(const base::ListValue& list, - bool (base::Value::*fnc)(T*) const) { - std::vector result; - result.reserve(list.GetSize()); - for (const base::Value* v : list) { - T val; - CHECK((v->*fnc)(&val)); - result.push_back(val); - } - return result; -} - -brillo::Any DictListToAny(const base::ListValue& list) { - std::vector result; - result.reserve(list.GetSize()); - for (const base::Value* v : list) { - const base::DictionaryValue* dict = nullptr; - CHECK(v->GetAsDictionary(&dict)); - result.push_back(details::DictionaryValueToVariantDictionary(*dict)); - } - return result; -} - -brillo::Any ListListToAny(const base::ListValue& list) { - std::vector result; - result.reserve(list.GetSize()); - for (const base::Value* v : list) - result.push_back(ValueToAny(*v)); - return result; -} - -// Converts a JSON value into an Any so it can be sent over D-Bus using -// UpdateState D-Bus method from Buffet. -brillo::Any ValueToAny(const base::Value& json) { - brillo::Any prop_value; - switch (json.GetType()) { - case base::Value::TYPE_BOOLEAN: - prop_value = ValueToAny(json, &base::Value::GetAsBoolean); - break; - case base::Value::TYPE_INTEGER: - prop_value = ValueToAny(json, &base::Value::GetAsInteger); - break; - case base::Value::TYPE_DOUBLE: - prop_value = ValueToAny(json, &base::Value::GetAsDouble); - break; - case base::Value::TYPE_STRING: - prop_value = ValueToAny(json, &base::Value::GetAsString); - break; - case base::Value::TYPE_DICTIONARY: { - const base::DictionaryValue* dict = nullptr; - CHECK(json.GetAsDictionary(&dict)); - prop_value = details::DictionaryValueToVariantDictionary(*dict); - break; - } - case base::Value::TYPE_LIST: { - const base::ListValue* list = nullptr; - CHECK(json.GetAsList(&list)); - if (list->empty()) { - // We don't know type of objects this list intended for, so we just use - // vector. - prop_value = ListListToAny(*list); - break; - } - auto type = (*list->begin())->GetType(); - for (const base::Value* v : *list) - CHECK_EQ(v->GetType(), type) << "Unsupported different type elements"; - - switch (type) { - case base::Value::TYPE_BOOLEAN: - prop_value = ListToAny(*list, &base::Value::GetAsBoolean); - break; - case base::Value::TYPE_INTEGER: - prop_value = ListToAny(*list, &base::Value::GetAsInteger); - break; - case base::Value::TYPE_DOUBLE: - prop_value = ListToAny(*list, &base::Value::GetAsDouble); - break; - case base::Value::TYPE_STRING: - prop_value = ListToAny(*list, &base::Value::GetAsString); - break; - case base::Value::TYPE_DICTIONARY: - prop_value = DictListToAny(*list); - break; - case base::Value::TYPE_LIST: - // We can't support Any{vector>} as the type is only known - // in runtime when we need to instantiate templates in compile time. - // We can use Any{vector} instead. - prop_value = ListListToAny(*list); - break; - default: - LOG(FATAL) << "Unsupported JSON value type for list element: " - << (*list->begin())->GetType(); - } - break; - } - default: - LOG(FATAL) << "Unexpected JSON value type: " << json.GetType(); - break; - } - return prop_value; -} - -template -std::unique_ptr CreateValue(const T& value, - brillo::ErrorPtr* error) { - return std::unique_ptr{new base::FundamentalValue{value}}; -} - -std::unique_ptr CreateValue(const std::string& value, - brillo::ErrorPtr* error) { - return std::unique_ptr{new base::StringValue{value}}; -} - -std::unique_ptr CreateValue(const char* value, - brillo::ErrorPtr* error) { - return std::unique_ptr{new base::StringValue{value}}; -} - -template <> -std::unique_ptr CreateValue( - const brillo::VariantDictionary& value, - brillo::ErrorPtr* error) { - return details::VariantDictionaryToDictionaryValue(value, error); -} - -template -std::unique_ptr CreateListValue(const std::vector& value, - brillo::ErrorPtr* error) { - std::unique_ptr list{new base::ListValue}; - - for (const T& i : value) { - auto item = CreateValue(i, error); - if (!item) - return nullptr; - list->Append(item.release()); - } - - return list; -} - -// Returns false only in case of error. True can be returned if type is not -// matched. -template -bool TryCreateValue(const brillo::Any& any, - std::unique_ptr* value, - brillo::ErrorPtr* error) { - if (any.IsTypeCompatible()) { - *value = CreateValue(any.Get(), error); - return *value != nullptr; - } - - if (any.IsTypeCompatible>()) { - *value = CreateListValue(any.Get>(), error); - return *value != nullptr; - } - - return true; // Not an error, we will try different type. -} - -template <> -std::unique_ptr CreateValue( - const brillo::Any& any, - brillo::ErrorPtr* error) { - std::unique_ptr result; - if (!TryCreateValue(any, &result, error) || result) - return result; - - if (!TryCreateValue(any, &result, error) || result) - return result; - - if (!TryCreateValue(any, &result, error) || result) - return result; - - if (!TryCreateValue(any, &result, error) || result) - return result; - - if (any.IsTypeCompatible()) - return CreateValue(any.Get(), error); - - if (!TryCreateValue(any, &result, error) || - result) { - return result; - } - - // This will collapse Any{Any{T}} and vector{Any{T}}. - if (!TryCreateValue(any, &result, error) || result) - return result; - - brillo::Error::AddToPrintf( - error, FROM_HERE, "buffet", "unknown_type", "Type '%s' is not supported.", - any.GetUndecoratedTypeName().c_str()); - - return nullptr; -} - -} // namespace - -namespace details { - -brillo::VariantDictionary DictionaryValueToVariantDictionary( - const base::DictionaryValue& object) { - brillo::VariantDictionary result; - - for (base::DictionaryValue::Iterator it(object); !it.IsAtEnd(); it.Advance()) - result.emplace(it.key(), ValueToAny(it.value())); - - return result; -} - -std::unique_ptr VariantDictionaryToDictionaryValue( - const brillo::VariantDictionary& object, - brillo::ErrorPtr* error) { - std::unique_ptr result{new base::DictionaryValue}; - - for (const auto& pair : object) { - auto value = CreateValue(pair.second, error); - if (!value) - return nullptr; - result->Set(pair.first, value.release()); - } - - return result; -} - -} // namespace details -} // namespace weaved diff --git a/common/data_conversion.h b/common/data_conversion.h deleted file mode 100644 index accc520..0000000 --- a/common/data_conversion.h +++ /dev/null @@ -1,38 +0,0 @@ -// Copyright 2015 The Android Open Source Project -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#ifndef COMMON_DATA_CONVERSION_H_ -#define COMMON_DATA_CONVERSION_H_ - -#include -#include -#include -#include - -namespace weaved { -namespace details { - -// Converts DictionaryValue to variant dictionary. -brillo::VariantDictionary DictionaryValueToVariantDictionary( - const base::DictionaryValue& object); - -// Converts variant dictionary to DictionaryValue. -std::unique_ptr VariantDictionaryToDictionaryValue( - const brillo::VariantDictionary& object, - brillo::ErrorPtr* error); - -} // namespace details -} // namespace weaved - -#endif // COMMON_DATA_CONVERSION_H_ diff --git a/common/data_conversion_unittest.cc b/common/data_conversion_unittest.cc deleted file mode 100644 index 7f84cdb..0000000 --- a/common/data_conversion_unittest.cc +++ /dev/null @@ -1,200 +0,0 @@ -// Copyright 2015 The Android Open Source Project -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "common/data_conversion.h" - -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include - -namespace weaved { - -namespace { - -using brillo::Any; -using brillo::VariantDictionary; -using weave::test::CreateDictionaryValue; -using weave::test::IsEqualValue; - -brillo::VariantDictionary ToVariant(const base::DictionaryValue& object) { - return details::DictionaryValueToVariantDictionary(object); -} - -std::unique_ptr FromVariant( - const brillo::VariantDictionary& object) { - brillo::ErrorPtr error; - auto result = details::VariantDictionaryToDictionaryValue(object, &error); - EXPECT_TRUE(result || error); - return result; -} - -std::unique_ptr CreateRandomValue(int children); -std::unique_ptr CreateRandomValue(int children, - base::Value::Type type); - -const base::Value::Type kRandomTypes[] = { - base::Value::TYPE_BOOLEAN, base::Value::TYPE_INTEGER, - base::Value::TYPE_DOUBLE, base::Value::TYPE_STRING, - base::Value::TYPE_DICTIONARY, base::Value::TYPE_LIST, -}; - -const base::Value::Type kRandomTypesWithChildren[] = { - base::Value::TYPE_DICTIONARY, base::Value::TYPE_LIST, -}; - -base::Value::Type CreateRandomValueType(bool with_children) { - if (with_children) { - return kRandomTypesWithChildren[base::RandInt( - 0, arraysize(kRandomTypesWithChildren) - 1)]; - } - return kRandomTypes[base::RandInt(0, arraysize(kRandomTypes) - 1)]; -} - -std::unique_ptr CreateRandomDictionary(int children) { - std::unique_ptr result{new base::DictionaryValue}; - - while (children > 0) { - int sub_children = base::RandInt(1, children); - children -= sub_children; - result->Set(base::GenerateGUID(), - CreateRandomValue(sub_children).release()); - } - - return result; -} - -std::unique_ptr CreateRandomList(int children) { - std::unique_ptr result{new base::ListValue}; - - base::Value::Type type = CreateRandomValueType(children > 0); - while (children > 0) { - size_t max_children = - (type != base::Value::TYPE_DICTIONARY && type != base::Value::TYPE_LIST) - ? 1 - : children; - size_t sub_children = base::RandInt(1, max_children); - children -= sub_children; - result->Append(CreateRandomValue(sub_children, type).release()); - } - - return result; -} - -std::unique_ptr CreateRandomValue(int children, - base::Value::Type type) { - CHECK_GE(children, 1); - switch (type) { - case base::Value::TYPE_INTEGER: - return std::unique_ptr{new base::FundamentalValue{ - base::RandInt(std::numeric_limits::min(), - std::numeric_limits::max())}}; - case base::Value::TYPE_DOUBLE: - return std::unique_ptr{ - new base::FundamentalValue{base::RandDouble()}}; - case base::Value::TYPE_STRING: - return std::unique_ptr{ - new base::StringValue{base::GenerateGUID()}}; - case base::Value::TYPE_DICTIONARY: - CHECK_GE(children, 1); - return CreateRandomDictionary(children - 1); - case base::Value::TYPE_LIST: - CHECK_GE(children, 1); - return CreateRandomList(children - 1); - default: - return std::unique_ptr{ - new base::FundamentalValue{base::RandInt(0, 1) != 0}}; - } -} - -std::unique_ptr CreateRandomValue(int children) { - return CreateRandomValue(children, CreateRandomValueType(children > 0)); -} - -} // namespace - -TEST(DBusConversionTest, DictionaryToDBusVariantDictionary) { - EXPECT_EQ((VariantDictionary{{"bool", true}}), - ToVariant(*CreateDictionaryValue("{'bool': true}"))); - EXPECT_EQ((VariantDictionary{{"int", 5}}), - ToVariant(*CreateDictionaryValue("{'int': 5}"))); - EXPECT_EQ((VariantDictionary{{"double", 6.7}}), - ToVariant(*CreateDictionaryValue("{'double': 6.7}"))); - EXPECT_EQ((VariantDictionary{{"string", std::string{"abc"}}}), - ToVariant(*CreateDictionaryValue("{'string': 'abc'}"))); - EXPECT_EQ((VariantDictionary{{"object", VariantDictionary{{"bool", true}}}}), - ToVariant(*CreateDictionaryValue("{'object': {'bool': true}}"))); - EXPECT_EQ((VariantDictionary{{"emptyList", std::vector{}}}), - ToVariant(*CreateDictionaryValue("{'emptyList': []}"))); - EXPECT_EQ((VariantDictionary{{"intList", std::vector{5}}}), - ToVariant(*CreateDictionaryValue("{'intList': [5]}"))); - EXPECT_EQ((VariantDictionary{ - {"intListList", std::vector{std::vector{5}, - std::vector{6, 7}}}}), - ToVariant(*CreateDictionaryValue( - "{'intListList': [[5], [6, 7]]}"))); - EXPECT_EQ((VariantDictionary{{"objList", - std::vector{ - {{"string", std::string{"abc"}}}}}}), - ToVariant(*CreateDictionaryValue( - "{'objList': [{'string': 'abc'}]}"))); -} - -TEST(DBusConversionTest, VariantDictionaryToDictionaryValue) { - EXPECT_JSON_EQ("{'bool': true}", *FromVariant({{"bool", true}})); - EXPECT_JSON_EQ("{'int': 5}", *FromVariant({{"int", 5}})); - EXPECT_JSON_EQ("{'double': 6.7}", *FromVariant({{"double", 6.7}})); - EXPECT_JSON_EQ("{'cString': 'abc'}", *FromVariant({{"cString", "abc"}})); - EXPECT_JSON_EQ("{'string': 'abc'}", - *FromVariant({{"string", std::string{"abc"}}})); - EXPECT_JSON_EQ("{'object': {'bool': true}}", - *FromVariant({{"object", VariantDictionary{{"bool", true}}}})); - EXPECT_JSON_EQ("{'emptyList': []}", - *FromVariant({{"emptyList", std::vector{}}})); - EXPECT_JSON_EQ("{'intList': [5]}", - *FromVariant({{"intList", std::vector{5}}})); - EXPECT_JSON_EQ( - "{'intListList': [[5], [6, 7]]}", - *FromVariant({{"intListList", - std::vector{std::vector{5}, - std::vector{6, 7}}}})); - EXPECT_JSON_EQ( - "{'objList': [{'string': 'abc'}]}", - *FromVariant({{"objList", std::vector{ - {{"string", std::string{"abc"}}}}}})); - EXPECT_JSON_EQ("{'int': 5}", *FromVariant({{"int", Any{Any{5}}}})); -} - -TEST(DBusConversionTest, VariantDictionaryToDictionaryValueErrors) { - EXPECT_FALSE(FromVariant({{"float", 1.0f}})); - EXPECT_FALSE(FromVariant({{"listList", std::vector>{}}})); - EXPECT_FALSE(FromVariant({{"any", Any{}}})); - EXPECT_FALSE(FromVariant({{"null", nullptr}})); -} - -TEST(DBusConversionTest, DBusRandomDictionaryConversion) { - auto dict = CreateRandomDictionary(10000); - auto varian_dict = ToVariant(*dict); - auto dict_restored = FromVariant(varian_dict); - EXPECT_PRED2(IsEqualValue, *dict, *dict_restored); -} - -} // namespace buffet 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 @@ Command::Origin Command::GetOrigin() const { 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 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 #include #include -#include +#include #include #include @@ -34,30 +34,6 @@ namespace weaved { class ServiceImpl; -namespace detail { - -// Helper function for Command::GetParameter. Allows specialization. -template -inline bool GetValue(const brillo::Any& any, T* value) { - return any.GetValue(value); -} - -// Specialization for double, allow to extract a double from an int. -template <> -inline bool GetValue(const brillo::Any& any, double* value) { - if (any.GetValue(value)) - return true; - - int int_val = 0; - if (!any.GetValue(&int_val)) - return false; - - *value = static_cast(int_val); - return true; -} - -} // namespace detail - class LIBWEAVED_EXPORT Command final { public: enum class State { @@ -91,7 +67,7 @@ class LIBWEAVED_EXPORT Command final { 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 @@ class LIBWEAVED_EXPORT Command final { // is of incorrect type. template 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(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 @@ class LIBWEAVED_EXPORT Command final { private: friend class ServiceImpl; android::sp binder_proxy_; + mutable std::unique_ptr 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 @@ class ServiceImpl : public std::enable_shared_from_this, 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 @@ void ServiceImpl::AddCommandHandler(const std::string& component, } 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 @@ class LIBWEAVED_EXPORT Service { // 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 @@ class LIBWEAVED_EXPORT Service { 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 -- cgit v1.2.3