metrics: remove gflags dependency

We are switching to using chromeos/flag_helper.h instead to standardize the
code everywhere.

BUG=chromium:375017
TEST=`FEATURES=test emerge-panther metrics`
TEST=`cbuildbot --remote --hwtest -p 'chromiumos/platform2'
      falco-paladin` passes

Change-Id: Icd08f65fd639e82ac6fe1581c763d60a189db827
Reviewed-on: https://chromium-review.googlesource.com/221757
Reviewed-by: Bertrand Simonnet <bsimonnet@chromium.org>
Commit-Queue: Steve Fung <stevefung@chromium.org>
Tested-by: Steve Fung <stevefung@chromium.org>
diff --git a/metrics/metrics.gyp b/metrics/metrics.gyp
index c6c7ffe..cd07682 100644
--- a/metrics/metrics.gyp
+++ b/metrics/metrics.gyp
@@ -27,7 +27,6 @@
       'link_settings': {
         'libraries': [
           '-lrootdev',
-          '-lgflags',
         ],
       },
       'sources': [
@@ -56,7 +55,6 @@
       ],
       'link_settings': {
         'libraries': [
-          '-lgflags',
           '-lvboot_host',
         ],
       },
diff --git a/metrics/metrics_daemon.cc b/metrics/metrics_daemon.cc
index 5f9d851..bdd181c 100644
--- a/metrics/metrics_daemon.cc
+++ b/metrics/metrics_daemon.cc
@@ -187,8 +187,8 @@
 }
 
 void MetricsDaemon::RunUploaderTest() {
-  upload_service_.reset(new UploadService(testing_));
-  upload_service_->Init();
+  upload_service_.reset(new UploadService(testing_, server_));
+  upload_service_->Init(upload_interval_secs_, metrics_file_);
   upload_service_->UploadEvent();
 }
 
@@ -215,11 +215,18 @@
                          const string& diskstats_path,
                          const string& vmstats_path,
                          const string& scaling_max_freq_path,
-                         const string& cpuinfo_max_freq_path) {
+                         const string& cpuinfo_max_freq_path,
+                         int upload_interval_secs,
+                         const string& server,
+                         const string& metrics_file) {
   testing_ = testing;
   DCHECK(metrics_lib != nullptr);
   metrics_lib_ = metrics_lib;
 
+  upload_interval_secs_ = upload_interval_secs;
+  server_ = server;
+  metrics_file_ = metrics_file;
+
   // Get ticks per second (HZ) on this system.
   // Sysconf cannot fail, so no sanity checks are needed.
   ticks_per_second_ = sysconf(_SC_CLK_TCK);
@@ -314,8 +321,8 @@
 
   if (uploader_active) {
     LOG(INFO) << "uploader enabled";
-    upload_service_.reset(new UploadService(testing_));
-    upload_service_->Init();
+    upload_service_.reset(new UploadService(testing_, server_));
+    upload_service_->Init(upload_interval_secs_, metrics_file_);
   }
 }
 
diff --git a/metrics/metrics_daemon.h b/metrics/metrics_daemon.h
index 205ea69..07072f9 100644
--- a/metrics/metrics_daemon.h
+++ b/metrics/metrics_daemon.h
@@ -36,7 +36,10 @@
             const std::string& diskstats_path,
             const std::string& vmstats_path,
             const std::string& cpuinfo_max_freq_path,
-            const std::string& scaling_max_freq_path);
+            const std::string& scaling_max_freq_path,
+            int upload_interval_secs,
+            const std::string& server,
+            const std::string& metrics_file);
 
   // Does all the work. If |run_as_daemon| is true, daemonizes by
   // forking.
@@ -372,6 +375,10 @@
   std::string scaling_max_freq_path_;
   std::string cpuinfo_max_freq_path_;
 
+  int upload_interval_secs_;
+  std::string server_;
+  std::string metrics_file_;
+
   scoped_ptr<UploadService> upload_service_;
 };
 
diff --git a/metrics/metrics_daemon_main.cc b/metrics/metrics_daemon_main.cc
index e529afd..e92ce31 100644
--- a/metrics/metrics_daemon_main.cc
+++ b/metrics/metrics_daemon_main.cc
@@ -6,8 +6,8 @@
 #include <base/command_line.h>
 #include <base/logging.h>
 #include <base/strings/string_util.h>
+#include <chromeos/flag_helper.h>
 #include <chromeos/syslog_logging.h>
-#include <gflags/gflags.h>
 #include <rootdev/rootdev.h>
 
 #include "metrics/metrics_daemon.h"
@@ -17,19 +17,6 @@
 const char kCpuinfoMaxFreqPath[] =
     "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq";
 
-// TODO(bsimonnet): Change this to use base::CommandLine (crbug.com/375017)
-DEFINE_bool(daemon, true, "run as daemon (use -nodaemon for debugging)");
-
-// The uploader is disabled by default on ChromeOS as Chrome is responsible for
-// sending the metrics.
-DEFINE_bool(uploader, false, "activate the uploader");
-
-// Upload the metrics once and exit. (used for testing)
-DEFINE_bool(
-    uploader_test,
-    false,
-    "run the uploader once and exit");
-
 // Returns the path to the disk stats in the sysfs.  Returns the null string if
 // it cannot find the disk stats file.
 static
@@ -56,8 +43,30 @@
 }
 
 int main(int argc, char** argv) {
-  CommandLine::Init(argc, argv);
-  google::ParseCommandLineFlags(&argc, &argv, true);
+  DEFINE_bool(daemon, true, "run as daemon (use -nodaemon for debugging)");
+
+  // The uploader is disabled by default on ChromeOS as Chrome is responsible
+  // for sending the metrics.
+  DEFINE_bool(uploader, false, "activate the uploader");
+
+  // Upload the metrics once and exit. (used for testing)
+  DEFINE_bool(uploader_test,
+              false,
+              "run the uploader once and exit");
+
+  // Upload Service flags.
+  DEFINE_int32(upload_interval_secs,
+               1800,
+               "Interval at which metrics_daemon sends the metrics. (needs "
+               "-uploader)");
+  DEFINE_string(server,
+                "https://clients4.google.com/uma/v2",
+                "Server to upload the metrics to. (needs -uploader)");
+  DEFINE_string(metrics_file,
+                "/var/run/metrics/uma-events",
+                "File to use as a proxy for uploading the metrics");
+
+  chromeos::FlagHelper::Init(argc, argv, "Chromium OS Metrics Daemon");
 
   // Also log to stderr when not running as daemon.
   chromeos::InitLog(chromeos::kLogToSyslog | chromeos::kLogHeader |
@@ -71,7 +80,10 @@
               MetricsMainDiskStatsPath(),
               "/proc/vmstat",
               kScalingMaxFreqPath,
-              kCpuinfoMaxFreqPath);
+              kCpuinfoMaxFreqPath,
+              FLAGS_upload_interval_secs,
+              FLAGS_server,
+              FLAGS_metrics_file);
 
 
   base::AtExitManager at_exit_manager;
diff --git a/metrics/metrics_daemon_test.cc b/metrics/metrics_daemon_test.cc
index 9b60ca4..78559df 100644
--- a/metrics/metrics_daemon_test.cc
+++ b/metrics/metrics_daemon_test.cc
@@ -45,6 +45,8 @@
 static const char kFakeVmStatsName[] = "fake-vm-stats";
 static const char kFakeScalingMaxFreqPath[] = "fake-scaling-max-freq";
 static const char kFakeCpuinfoMaxFreqPath[] = "fake-cpuinfo-max-freq";
+static const char kMetricsServer[] = "https://clients4.google.com/uma/v2";
+static const char kMetricsFilePath[] = "/var/run/metrics/uma-events";
 
 class MetricsDaemonTest : public testing::Test {
  protected:
@@ -69,7 +71,10 @@
                  kFakeDiskStatsName,
                  kFakeVmStatsName,
                  kFakeScalingMaxFreqPath,
-                 kFakeCpuinfoMaxFreqPath);
+                 kFakeCpuinfoMaxFreqPath,
+                 1800,
+                 kMetricsServer,
+                 kMetricsFilePath);
 
     // Replace original persistent values with mock ones.
     daily_active_use_mock_ =
diff --git a/metrics/uploader/upload_service.cc b/metrics/uploader/upload_service.cc
index 3266a8e..0ebfc78 100644
--- a/metrics/uploader/upload_service.cc
+++ b/metrics/uploader/upload_service.cc
@@ -17,36 +17,24 @@
 #include <base/sha1.h>
 #include <components/metrics/chromeos/metric_sample.h>
 #include <components/metrics/chromeos/serialization_utils.h>
-#include <gflags/gflags.h>
 
 #include "metrics/uploader/metrics_log.h"
 #include "metrics/uploader/sender_http.h"
 #include "metrics/uploader/system_profile_cache.h"
 
-DEFINE_int32(
-    upload_interval_secs,
-    1800,
-    "Interval at which metrics_daemon sends the metrics. (needs -uploader)");
-
-DEFINE_string(server,
-              "https://clients4.google.com/uma/v2",
-              "Server to upload the metrics to. (needs -uploader)");
-
-DEFINE_string(metrics_file,
-              "/var/run/metrics/uma-events",
-              "File to use as a proxy for uploading the metrics");
-
 const int UploadService::kMaxFailedUpload = 10;
 
-UploadService::UploadService(bool testing)
+UploadService::UploadService(bool testing, const std::string& server)
     : system_profile_setter_(new SystemProfileCache(testing)),
       histogram_snapshot_manager_(this),
-      sender_(new HttpSender(FLAGS_server)) {
+      sender_(new HttpSender(server)) {
 }
 
-void UploadService::Init() {
+void UploadService::Init(int upload_interval_secs,
+                         const std::string& metrics_file) {
   base::StatisticsRecorder::Initialize();
-  g_timeout_add_seconds(FLAGS_upload_interval_secs, &UploadEventStatic, this);
+  metrics_file_ = metrics_file;
+  g_timeout_add_seconds(upload_interval_secs, &UploadEventStatic, this);
 }
 
 void UploadService::StartNewLog() {
@@ -116,7 +104,7 @@
 
   ScopedVector<metrics::MetricSample> vector;
   metrics::SerializationUtils::ReadAndTruncateMetricsFromFile(
-      FLAGS_metrics_file, &vector);
+      metrics_file_, &vector);
 
   int i = 0;
   for (ScopedVector<metrics::MetricSample>::iterator it = vector.begin();
diff --git a/metrics/uploader/upload_service.h b/metrics/uploader/upload_service.h
index 114c5e4..c0a1430 100644
--- a/metrics/uploader/upload_service.h
+++ b/metrics/uploader/upload_service.h
@@ -54,9 +54,10 @@
 //
 class UploadService : public base::HistogramFlattener {
  public:
-  explicit UploadService(bool testing);
+  explicit UploadService(bool testing, const std::string& server);
 
-  void Init();
+  void Init(int upload_interval_secs,
+            const std::string& metrics_file);
 
   // Starts a new log. The log needs to be regenerated after each successful
   // launch as it is destroyed when staging the log.
@@ -133,6 +134,8 @@
   int failed_upload_count_;
   scoped_ptr<MetricsLog> current_log_;
   scoped_ptr<MetricsLog> staged_log_;
+
+  std::string metrics_file_;
 };
 
 #endif  // METRICS_UPLOADER_UPLOAD_SERVICE_H_
diff --git a/metrics/uploader/upload_service_test.cc b/metrics/uploader/upload_service_test.cc
index 94453b2..9badf64 100644
--- a/metrics/uploader/upload_service_test.cc
+++ b/metrics/uploader/upload_service_test.cc
@@ -19,16 +19,19 @@
 #include "uploader/system_profile_cache.h"
 #include "uploader/upload_service.h"
 
+static const char kMetricsServer[] = "https://clients4.google.com/uma/v2";
+static const char kMetricsFilePath[] = "/var/run/metrics/uma-events";
+
 class UploadServiceTest : public testing::Test {
  protected:
   UploadServiceTest()
       : cache_(true),
-        upload_service_(true),
+        upload_service_(true, kMetricsServer),
         exit_manager_(new base::AtExitManager()) {
     sender_ = new SenderMock;
     upload_service_.sender_.reset(sender_);
     upload_service_.system_profile_setter_.reset(new MockSystemProfileSetter());
-    upload_service_.Init();
+    upload_service_.Init(1800, kMetricsFilePath);
   }
 
   virtual void SetUp() {
@@ -124,7 +127,7 @@
 }
 
 TEST_F(UploadServiceTest, LogEmptyByDefault) {
-  UploadService upload_service(true);
+  UploadService upload_service(true, kMetricsServer);
 
   // current_log_ should be initialized later as it needs AtExitManager to exit
   // in order to gather system information from SysInfo.