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

Commit ae4bdc4c authored by Steve Fung's avatar Steve Fung Committed by ChromeOS Commit Bot
Browse files

metrics: Fix upload_service to work with base::MessageLoop

With an earlier change, metrics_daemon was switched from the glib message loop
to base::MessageLoop.  UploadService still is trying to interact with the
glib message loop, it needs to be switched to using base::MessageLoop.

BUG=chromium:452228
TEST=`FEATURES=test emerge-panther metrics`

Change-Id: I38eb52ca1995d75cfb7d0e69a434e649493e7c6f
Reviewed-on: https://chromium-review.googlesource.com/243429


Reviewed-by: default avatarNathan Bullock <nathanbullock@google.com>
Reviewed-by: default avatarBertrand Simonnet <bsimonnet@chromium.org>
Tested-by: default avatarStephen Fung <stevefung@chromium.org>
Commit-Queue: Stephen Fung <stevefung@chromium.org>
Trybot-Ready: Stephen Fung <stevefung@chromium.org>
parent a8bcc18b
Loading
Loading
Loading
Loading
+0 −4
Original line number Diff line number Diff line
@@ -3,10 +3,6 @@
    'variables': {
      'deps': [
        'dbus-1',
        'dbus-glib-1',
        'glib-2.0',
        'gobject-2.0',
        'gthread-2.0',
        'libchrome-<(libbase_ver)',
        'libchromeos-<(libbase_ver)',
      ]
+0 −1
Original line number Diff line number Diff line
@@ -4,7 +4,6 @@

#include "metrics/uploader/system_profile_cache.h"

#include <glib.h>
#include <string>
#include <vector>

+27 −9
Original line number Diff line number Diff line
@@ -4,11 +4,12 @@

#include "metrics/uploader/upload_service.h"

#include <glib.h>
#include <string>

#include <base/bind.h>
#include <base/logging.h>
#include <base/memory/scoped_vector.h>
#include <base/message_loop/message_loop.h>
#include <base/metrics/histogram.h>
#include <base/metrics/histogram_base.h>
#include <base/metrics/histogram_snapshot_manager.h>
@@ -28,14 +29,29 @@ UploadService::UploadService(SystemProfileSetter* setter,
                             const std::string& server)
    : system_profile_setter_(setter),
      histogram_snapshot_manager_(this),
      sender_(new HttpSender(server)) {
      sender_(new HttpSender(server)),
      testing_(false) {
}

UploadService::UploadService(SystemProfileSetter* setter,
                             const std::string& server,
                             bool testing)
    : UploadService(setter, server) {
  testing_ = testing;
}

void UploadService::Init(const base::TimeDelta& upload_interval,
                         const std::string& metrics_file) {
  base::StatisticsRecorder::Initialize();
  metrics_file_ = metrics_file;
  g_timeout_add_seconds(upload_interval.InSeconds(), &UploadEventStatic, this);

  if (!testing_) {
    base::MessageLoop::current()->PostDelayedTask(FROM_HERE,
        base::Bind(&UploadService::UploadEventCallback,
                   base::Unretained(this),
                   upload_interval),
        upload_interval);
  }
}

void UploadService::StartNewLog() {
@@ -46,12 +62,14 @@ void UploadService::StartNewLog() {
  current_log_.reset(log);
}

// static
int UploadService::UploadEventStatic(void* uploader) {
  CHECK(uploader);
  // This is called by glib with a pointer to an UploadEvent object.
  static_cast<UploadService*>(uploader)->UploadEvent();
  return 1;
void UploadService::UploadEventCallback(const base::TimeDelta& interval) {
  UploadEvent();

  base::MessageLoop::current()->PostDelayedTask(FROM_HERE,
      base::Bind(&UploadService::UploadEventCallback,
                 base::Unretained(this),
                 interval),
      interval);
}

void UploadService::UploadEvent() {
+9 −3
Original line number Diff line number Diff line
@@ -64,9 +64,8 @@ class UploadService : public base::HistogramFlattener {
  // launch as it is destroyed when staging the log.
  void StartNewLog();

  // Glib takes a function pointer and passes the object as a void*.
  // Uploader is expected to be an UploaderService.
  static int UploadEventStatic(void* uploader);
  // Event callback for handling MessageLoop events.
  void UploadEventCallback(const base::TimeDelta& interval);

  // Triggers an upload event.
  void UploadEvent();
@@ -98,6 +97,11 @@ class UploadService : public base::HistogramFlattener {
  FRIEND_TEST(UploadServiceTest, UnknownCrashIgnored);
  FRIEND_TEST(UploadServiceTest, ValuesInConfigFileAreSent);

  // Private constructor for use in unit testing.
  UploadService(SystemProfileSetter* setter,
                const std::string& server,
                bool testing);

  // If a staged log fails to upload more than kMaxFailedUpload times, it
  // will be discarded.
  static const int kMaxFailedUpload;
@@ -137,6 +141,8 @@ class UploadService : public base::HistogramFlattener {
  scoped_ptr<MetricsLog> staged_log_;

  std::string metrics_file_;

  bool testing_;
};

#endif  // METRICS_UPLOADER_UPLOAD_SERVICE_H_
+1 −1
Original line number Diff line number Diff line
@@ -26,7 +26,7 @@ class UploadServiceTest : public testing::Test {
 protected:
  UploadServiceTest()
      : cache_(true, "/"),
        upload_service_(new MockSystemProfileSetter(), kMetricsServer),
        upload_service_(new MockSystemProfileSetter(), kMetricsServer, true),
        exit_manager_(new base::AtExitManager()) {
    sender_ = new SenderMock;
    upload_service_.sender_.reset(sender_);