From 82b02de5aaf7e63d78e691ca6d240ebf2e5de99b Mon Sep 17 00:00:00 2001 From: Alex Vakulenko Date: Fri, 9 Oct 2015 20:07:47 -0700 Subject: [PATCH] Update metrics to use weaved's client library Do not use weave'd D-Bus proxies directly. Use the new client library. Change-Id: I524d9c5c4c057bd1f82a280ec96848b8a8f4fe29 --- metricsd/Android.mk | 2 +- metricsd/metrics_daemon.cc | 49 ++++++++++++++++++-------------------- metricsd/metrics_daemon.h | 17 +++++-------- 3 files changed, 30 insertions(+), 38 deletions(-) diff --git a/metricsd/Android.mk b/metricsd/Android.mk index afb45da5e..3deb7d391 100644 --- a/metricsd/Android.mk +++ b/metricsd/Android.mk @@ -74,7 +74,7 @@ metrics_daemon_shared_libraries := $(libmetrics_shared_libraries) \ libmetrics \ libprotobuf-cpp-lite \ librootdev \ - libweaved-client \ + libweaved \ # Shared library for metrics. # ======================================================== diff --git a/metricsd/metrics_daemon.cc b/metricsd/metrics_daemon.cc index f83c5d42c..2ea5bbda9 100644 --- a/metricsd/metrics_daemon.cc +++ b/metricsd/metrics_daemon.cc @@ -283,11 +283,15 @@ int MetricsDaemon::OnInit() { return EX_UNAVAILABLE; } - weaved_object_mgr_.reset(new com::android::Weave::ObjectManagerProxy{bus_}); - weaved_object_mgr_->SetCommandAddedCallback( - base::Bind(&MetricsDaemon::OnWeaveCommand, base::Unretained(this))); - weaved_object_mgr_->SetManagerAddedCallback( + device_ = weaved::Device::CreateInstance( + bus_, base::Bind(&MetricsDaemon::UpdateWeaveState, base::Unretained(this))); + device_->AddCommandHandler( + "_metrics._enableAnalyticsReporting", + base::Bind(&MetricsDaemon::OnEnableMetrics, base::Unretained(this))); + device_->AddCommandHandler( + "_metrics._disableAnalyticsReporting", + base::Bind(&MetricsDaemon::OnDisableMetrics, base::Unretained(this))); } base::MessageLoop::current()->PostDelayedTask(FROM_HERE, @@ -325,20 +329,11 @@ void MetricsDaemon::OnShutdown(int* return_code) { chromeos::DBusDaemon::OnShutdown(return_code); } -void MetricsDaemon::OnWeaveCommand(CommandProxy* command) { - if (command->state() != "queued") { +void MetricsDaemon::OnEnableMetrics(const std::weak_ptr& cmd) { + auto command = cmd.lock(); + if (!command) return; - } - VLOG(1) << "received weave command: " << command->name(); - if (command->name() == "_metrics._enableAnalyticsReporting") { - OnEnableMetrics(command); - } else if (command->name() == "_metrics._disableAnalyticsReporting") { - OnDisableMetrics(command); - } -} - -void MetricsDaemon::OnEnableMetrics(CommandProxy* command) { if (base::WriteFile(metrics_directory_.Append(metrics::kConsentFileName), "", 0) != 0) { PLOG(ERROR) << "Could not create the consent file."; @@ -347,11 +342,16 @@ void MetricsDaemon::OnEnableMetrics(CommandProxy* command) { return; } - NotifyStateChanged(); + UpdateWeaveState(); command->Complete({}, nullptr); } -void MetricsDaemon::OnDisableMetrics(CommandProxy* command) { +void MetricsDaemon::OnDisableMetrics( + const std::weak_ptr& cmd) { + auto command = cmd.lock(); + if (!command) + return; + if (!base::DeleteFile(metrics_directory_.Append(metrics::kConsentFileName), false)) { PLOG(ERROR) << "Could not delete the consent file."; @@ -360,23 +360,20 @@ void MetricsDaemon::OnDisableMetrics(CommandProxy* command) { return; } - NotifyStateChanged(); + UpdateWeaveState(); command->Complete({}, nullptr); } -void MetricsDaemon::NotifyStateChanged() { - ManagerProxy* manager = weaved_object_mgr_->GetManagerProxy(); - if (manager) - UpdateWeaveState(manager); -} +void MetricsDaemon::UpdateWeaveState() { + if (!device_) + return; -void MetricsDaemon::UpdateWeaveState(ManagerProxy* manager) { chromeos::VariantDictionary state_change{ { "_metrics._AnalyticsReportingState", metrics_lib_->AreMetricsEnabled() ? "enabled" : "disabled" } }; - if (!manager->UpdateState(state_change, nullptr)) { + if (!device_->SetStateProperties(state_change, nullptr)) { LOG(ERROR) << "failed to update weave's state"; } } diff --git a/metricsd/metrics_daemon.h b/metricsd/metrics_daemon.h index 0a8f6a5aa..146108a0c 100644 --- a/metricsd/metrics_daemon.h +++ b/metricsd/metrics_daemon.h @@ -26,8 +26,9 @@ #include #include #include -#include #include +#include +#include #include // for FRIEND_TEST #include "collectors/averaged_statistics_collector.h" @@ -122,20 +123,14 @@ class MetricsDaemon : public chromeos::DBusDaemon { DBusMessage* message, void* user_data); - // Callback for Weave commands. - void OnWeaveCommand(com::android::Weave::CommandProxy* command); - // Enables metrics reporting. - void OnEnableMetrics(com::android::Weave::CommandProxy* command); + void OnEnableMetrics(const std::weak_ptr& cmd); // Disables metrics reporting. - void OnDisableMetrics(com::android::Weave::CommandProxy* command); + void OnDisableMetrics(const std::weak_ptr& cmd); // Updates the weave device state. - void UpdateWeaveState(com::android::Weave::ManagerProxy* manager); - - // Tells Weave that the state has changed. - void NotifyStateChanged(); + void UpdateWeaveState(); // Updates the active use time and logs time between user-space // process crashes. @@ -317,7 +312,7 @@ class MetricsDaemon : public chromeos::DBusDaemon { std::string server_; scoped_ptr upload_service_; - scoped_ptr weaved_object_mgr_; + std::unique_ptr device_; }; #endif // METRICS_METRICS_DAEMON_H_