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: Nathan Bullock <nathanbullock@google.com>
Reviewed-by: Bertrand Simonnet <bsimonnet@chromium.org>
Tested-by: Stephen Fung <stevefung@chromium.org>
Commit-Queue: Stephen Fung <stevefung@chromium.org>
Trybot-Ready: Stephen Fung <stevefung@chromium.org>
This commit is contained in:
Steve Fung 2015-01-26 17:13:24 -08:00 committed by ChromeOS Commit Bot
parent a8bcc18bea
commit ae4bdc4c92
5 changed files with 37 additions and 18 deletions

View File

@ -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)',
]

View File

@ -4,7 +4,6 @@
#include "metrics/uploader/system_profile_cache.h"
#include <glib.h>
#include <string>
#include <vector>

View File

@ -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() {

View File

@ -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_

View File

@ -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_);