Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 82b02de5 authored by Alex Vakulenko's avatar Alex Vakulenko
Browse files

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
parent 9e27cab6
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -74,7 +74,7 @@ metrics_daemon_shared_libraries := $(libmetrics_shared_libraries) \
  libmetrics \
  libprotobuf-cpp-lite \
  librootdev \
  libweaved-client \
  libweaved \

# Shared library for metrics.
# ========================================================
+23 −26
Original line number Diff line number Diff line
@@ -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<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.";
@@ -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.";
@@ -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";
  }
}
+6 −11
Original line number Diff line number Diff line
@@ -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"
@@ -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.
@@ -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_