metrics: Convert Metrics to DBusDaemon

In order to remove glib, convert Metrics from the glib message loop to
DBusDaemon, which is based on base::MessageLoop.  Also use the DBusDaemon's
dbus::Bus object directly to set up the CrashReporter signal handling, as
the ObjectProxy used by chromeos-dbus-bindings requires all signals be sent
from registered name owners.

BUG=chromium:431838
TEST=Unittests and hwtests pass.
TEST=`test_that -b panther <IP> platform_MetricsUploader` passes
CQ-DEPEND=CL:236652

Change-Id: I6bc1f66999a43065b0d48325b031cd36bb782b76
Reviewed-on: https://chromium-review.googlesource.com/234359
Reviewed-by: Alex Vakulenko <avakulenko@chromium.org>
Commit-Queue: Steve Fung <stevefung@chromium.org>
Tested-by: Steve Fung <stevefung@chromium.org>
diff --git a/metrics/metrics_daemon.h b/metrics/metrics_daemon.h
index f4f1430..397fd21 100644
--- a/metrics/metrics_daemon.h
+++ b/metrics/metrics_daemon.h
@@ -7,8 +7,6 @@
 
 #include <stdint.h>
 
-#include <dbus/dbus.h>
-#include <glib.h>
 #include <map>
 #include <string>
 #include <vector>
@@ -16,6 +14,7 @@
 #include <base/files/file_path.h>
 #include <base/memory/scoped_ptr.h>
 #include <base/time/time.h>
+#include <chromeos/daemons/dbus_daemon.h>
 #include <gtest/gtest_prod.h>  // for FRIEND_TEST
 
 #include "metrics/metrics_library.h"
@@ -24,12 +23,12 @@
 
 using chromeos_metrics::PersistentInteger;
 
-class MetricsDaemon {
+class MetricsDaemon : public chromeos::DBusDaemon {
  public:
   MetricsDaemon();
   ~MetricsDaemon();
 
-  // Initializes.
+  // Initializes metrics class variables.
   void Init(bool testing,
             bool uploader_active,
             MetricsLibraryInterface* metrics_lib,
@@ -42,9 +41,14 @@
             const std::string& metrics_file,
             const std::string& config_root);
 
-  // Does all the work. If |run_as_daemon| is true, daemonizes by
-  // forking.
-  void Run(bool run_as_daemon);
+  // Initializes DBus and MessageLoop variables before running the MessageLoop.
+  int OnInit() override;
+
+  // Clean up data set up in OnInit before shutting down message loop.
+  void OnShutdown(int* return_code) override;
+
+  // Does all the work.
+  int Run() override;
 
   // Triggers an upload event and exit. (Used to test UploadService)
   void RunUploaderTest();
@@ -147,9 +151,6 @@
   // Returns the active time since boot (uptime minus sleep time) in seconds.
   double GetActiveTime();
 
-  // Creates the event loop and enters it.
-  void Loop();
-
   // D-Bus filter callback.
   static DBusHandlerResult MessageFilter(DBusConnection* connection,
                                          DBusMessage* message,
@@ -227,22 +228,14 @@
   // Parse cumulative vm statistics from a C string.  Returns true for success.
   bool VmStatsParseStats(const char* stats, struct VmstatRecord* record);
 
-  // Reports disk and vm statistics (static version for glib).  Arguments are a
-  // glib artifact.
-  static gboolean StatsCallbackStatic(void* handle);
-
   // Reports disk and vm statistics.
   void StatsCallback();
 
   // Schedules meminfo collection callback.
   void ScheduleMeminfoCallback(int wait);
 
-  // Reports memory statistics (static version for glib).  Argument is a glib
-  // artifact.
-  static gboolean MeminfoCallbackStatic(void* handle);
-
-  // Reports memory statistics.  Returns false on failure.
-  bool MeminfoCallback();
+  // Reports memory statistics.  Reschedules callback on success.
+  void MeminfoCallback(base::TimeDelta wait);
 
   // Parses content of /proc/meminfo and sends fields of interest to UMA.
   // Returns false on errors.  |meminfo_raw| contains the content of
@@ -259,9 +252,6 @@
   // Schedule a memory use callback in |interval| seconds.
   void ScheduleMemuseCallback(double interval);
 
-  // Static wrapper for MemuseCallback.  Always returns false.
-  static gboolean MemuseCallbackStatic(void* handle);
-
   // Calls MemuseCallbackWork, and possibly schedules next callback, if enough
   // active time has passed.  Otherwise reschedules itself to simulate active
   // time callbacks (i.e. wall clock time minus sleep time).
@@ -288,7 +278,7 @@
   void UpdateStats(base::TimeTicks now_ticks, base::Time now_wall_time);
 
   // Invoked periodically by |update_stats_timeout_id_| to call UpdateStats().
-  static gboolean HandleUpdateStatsTimeout(gpointer data);
+  void HandleUpdateStatsTimeout();
 
   // Reports zram statistics.
   bool ReportZram(const base::FilePath& zram_dir);
@@ -301,6 +291,9 @@
   // Test mode.
   bool testing_;
 
+  // Whether the uploader is enabled or disabled.
+  bool uploader_active_;
+
   // Root of the configuration files to use.
   std::string config_root_;
 
@@ -315,16 +308,6 @@
   // The last time that UpdateStats() was called.
   base::TimeTicks last_update_stats_time_;
 
-  // ID of a GLib timeout that repeatedly runs UpdateStats().
-  gint update_stats_timeout_id_;
-
-  // Sleep period until the next daily usage aggregation performed by
-  // the daily use monitor (see ScheduleUseMonitor).
-  int usemon_interval_;
-
-  // Scheduled daily use monitor source (see ScheduleUseMonitor).
-  GSource* usemon_source_;
-
   // End time of current memuse stat collection interval.
   double memuse_final_time_;