Skip to content
This repository has been archived by the owner on Nov 8, 2023. It is now read-only.

Commit

Permalink
Update metrics to use weaved's client library
Browse files Browse the repository at this point in the history
Do not use weave'd D-Bus proxies directly. Use the new client library.

Change-Id: I524d9c5c4c057bd1f82a280ec96848b8a8f4fe29
  • Loading branch information
Alex Vakulenko committed Oct 10, 2015
1 parent 9e27cab commit 82b02de
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 38 deletions.
2 changes: 1 addition & 1 deletion metricsd/Android.mk
Expand Up @@ -74,7 +74,7 @@ metrics_daemon_shared_libraries := $(libmetrics_shared_libraries) \
libmetrics \
libprotobuf-cpp-lite \
librootdev \
libweaved-client \
libweaved \

# Shared library for metrics.
# ========================================================
Expand Down
49 changes: 23 additions & 26 deletions metricsd/metrics_daemon.cc
Expand Up @@ -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,
Expand Down Expand Up @@ -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<weaved::Command>& 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.";
Expand All @@ -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<weaved::Command>& 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.";
Expand All @@ -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";
}
}
Expand Down
17 changes: 6 additions & 11 deletions metricsd/metrics_daemon.h
Expand Up @@ -26,8 +26,9 @@
#include <base/files/file_path.h>
#include <base/memory/scoped_ptr.h>
#include <base/time/time.h>
#include <buffet/dbus-proxies.h>
#include <chromeos/daemons/dbus_daemon.h>
#include <libweaved/command.h>
#include <libweaved/device.h>
#include <gtest/gtest_prod.h> // for FRIEND_TEST

#include "collectors/averaged_statistics_collector.h"
Expand Down Expand Up @@ -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<weaved::Command>& cmd);

// Disables metrics reporting.
void OnDisableMetrics(com::android::Weave::CommandProxy* command);
void OnDisableMetrics(const std::weak_ptr<weaved::Command>& 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.
Expand Down Expand Up @@ -317,7 +312,7 @@ class MetricsDaemon : public chromeos::DBusDaemon {
std::string server_;

scoped_ptr<UploadService> upload_service_;
scoped_ptr<com::android::Weave::ObjectManagerProxy> weaved_object_mgr_;
std::unique_ptr<weaved::Device> device_;
};

#endif // METRICS_METRICS_DAEMON_H_

0 comments on commit 82b02de

Please sign in to comment.