update_engine: Migrate UE DBus service to chrome DBus bindings.

chromeos-dbus-bindings now generates the adaptor interface that
update_engine exposes over DBus. This interface is implemented in
dbus_service.{h,cc}, which now has a UpdateEngineService class
encapsulating all the service methods implementation.

This allows to write unit test for those methods, which are included
in this CL for all the non-trivial methods.

This CL now uses chrome's DBus bindings for the update_engine serive,
but the proxy interaction is still done using dbus-glib. The main loop
in the main.cc file is now replaced with the chromeos::Dameon, which
uses a chromeos::BaseMessageLoop instead of a GlibMessageLoop. This
causes the asynchronous interactions in the proxy side to not work,
which will be fixed in the next CL.

CQ-DEPEND=CL:290990,CL:291092,CL:293334
BUG=chromium:419827
TEST=Added unittest for all dbus_service methods. deployed and tested manually that update_engine dbus interface works.

Change-Id: I6a6d142b2ac1a61a4c3abcb927665b26114abe5c
Reviewed-on: https://chromium-review.googlesource.com/225324
Reviewed-by: Gilad Arnold <garnold@chromium.org>
Reviewed-by: Alex Deymo <deymo@chromium.org>
Commit-Queue: Alex Deymo <deymo@chromium.org>
Trybot-Ready: Alex Deymo <deymo@chromium.org>
Tested-by: Alex Deymo <deymo@chromium.org>
diff --git a/dbus_service.h b/dbus_service.h
index d61e3b5..6429240 100644
--- a/dbus_service.h
+++ b/dbus_service.h
@@ -7,171 +7,153 @@
 
 #include <inttypes.h>
 
-#include <dbus/dbus-glib.h>
-#include <dbus/dbus-glib-bindings.h>
-#include <dbus/dbus-glib-lowlevel.h>
-#include <glib-object.h>
+#include <string>
+
+#include <base/memory/ref_counted.h>
+#include <chromeos/errors/error.h>
 
 #include "update_engine/update_attempter.h"
 
-// Type macros:
-#define UPDATE_ENGINE_TYPE_SERVICE (update_engine_service_get_type())
-#define UPDATE_ENGINE_SERVICE(obj)                                      \
-  (G_TYPE_CHECK_INSTANCE_CAST((obj), UPDATE_ENGINE_TYPE_SERVICE,        \
-                              UpdateEngineService))
-#define UPDATE_ENGINE_IS_SERVICE(obj)                                   \
-  (G_TYPE_CHECK_INSTANCE_TYPE((obj), UPDATE_ENGINE_TYPE_SERVICE))
-#define UPDATE_ENGINE_SERVICE_CLASS(klass)                      \
-  (G_TYPE_CHECK_CLASS_CAST((klass), UPDATE_ENGINE_TYPE_SERVICE, \
-                           UpdateEngineService))
-#define UPDATE_ENGINE_IS_SERVICE_CLASS(klass)                           \
-  (G_TYPE_CHECK_CLASS_TYPE((klass), UPDATE_ENGINE_TYPE_SERVICE))
-#define UPDATE_ENGINE_SERVICE_GET_CLASS(obj)                    \
-  (G_TYPE_INSTANCE_GET_CLASS((obj), UPDATE_ENGINE_TYPE_SERVICE, \
-                             UpdateEngineService))
+#include "update_engine/dbus_adaptor/org.chromium.UpdateEngineInterface.h"
 
-G_BEGIN_DECLS
+namespace chromeos {
+namespace dbus {
+class Bus;
+}  // namespace dbus
+}  // namespace chromeos
 
-struct UpdateEngineService {
-  GObject parent_instance;
+namespace chromeos_update_engine {
 
-  chromeos_update_engine::SystemState* system_state_;
+class UpdateEngineService
+    : public org::chromium::UpdateEngineInterfaceInterface {
+ public:
+  explicit UpdateEngineService(SystemState* system_state);
+  virtual ~UpdateEngineService() = default;
+
+  // Implementation of org::chromium::UpdateEngineInterfaceInterface.
+  bool AttemptUpdate(chromeos::ErrorPtr* error,
+                     const std::string& in_app_version,
+                     const std::string& in_omaha_url) override;
+
+  bool AttemptUpdateWithFlags(chromeos::ErrorPtr* error,
+                              const std::string& in_app_version,
+                              const std::string& in_omaha_url,
+                              int32_t in_flags_as_int) override;
+
+  bool AttemptRollback(chromeos::ErrorPtr* error, bool in_powerwash) override;
+
+  // Checks if the system rollback is available by verifying if the secondary
+  // system partition is valid and bootable.
+  bool CanRollback(chromeos::ErrorPtr* error, bool* out_can_rollback) override;
+
+  // Resets the status of the update_engine to idle, ignoring any applied
+  // update. This is used for development only.
+  bool ResetStatus(chromeos::ErrorPtr* error) override;
+
+  // Returns the current status of the Update Engine. If an update is in
+  // progress, the number of operations, size to download and overall progress
+  // is reported.
+  bool GetStatus(chromeos::ErrorPtr* error,
+                 int64_t* out_last_checked_time,
+                 double* out_progress,
+                 std::string* out_current_operation,
+                 std::string* out_new_version,
+                 int64_t* out_new_size) override;
+
+  // Reboots the device if an update is applied and a reboot is required.
+  bool RebootIfNeeded(chromeos::ErrorPtr* error) override;
+
+  // Changes the current channel of the device to the target channel. If the
+  // target channel is a less stable channel than the current channel, then the
+  // channel change happens immediately (at the next update check).  If the
+  // target channel is a more stable channel, then if is_powerwash_allowed is
+  // set to true, then also the change happens immediately but with a powerwash
+  // if required. Otherwise, the change takes effect eventually (when the
+  // version on the target channel goes above the version number of what the
+  // device currently has).
+  bool SetChannel(chromeos::ErrorPtr* error,
+                  const std::string& in_target_channel,
+                  bool in_is_powerwash_allowed) override;
+
+  // If get_current_channel is set to true, populates |channel| with the name of
+  // the channel that the device is currently on. Otherwise, it populates it
+  // with the name of the channel the device is supposed to be (in case of a
+  // pending channel change).
+  bool GetChannel(chromeos::ErrorPtr* error,
+                  bool in_get_current_channel,
+                  std::string* out_channel) override;
+
+  // Enables or disables the sharing and consuming updates over P2P feature
+  // according to the |enabled| argument passed.
+  bool SetP2PUpdatePermission(chromeos::ErrorPtr* error,
+                              bool in_enabled) override;
+
+  // Returns the current value for the P2P enabled setting. This involves both
+  // sharing and consuming updates over P2P.
+  bool GetP2PUpdatePermission(chromeos::ErrorPtr* error,
+                              bool* out_enabled) override;
+
+  // If there's no device policy installed, sets the update over cellular
+  // networks permission to the |allowed| value. Otherwise, this method returns
+  // with an error since this setting is overridden by the applied policy.
+  bool SetUpdateOverCellularPermission(chromeos::ErrorPtr* error,
+                                       bool in_allowed) override;
+
+  // Returns the current value of the update over cellular network setting,
+  // either forced by the device policy if the device is enrolled or the current
+  // user preference otherwise.
+  bool GetUpdateOverCellularPermission(chromeos::ErrorPtr* error,
+                                       bool* out_allowed) override;
+
+  // Returns the duration since the last successful update, as the
+  // duration on the wallclock. Returns an error if the device has not
+  // updated.
+  bool GetDurationSinceUpdate(chromeos::ErrorPtr* error,
+                              int64_t* out_usec_wallclock) override;
+
+  // Returns the version string of OS that was used before the last reboot
+  // into an updated version. This is available only when rebooting into an
+  // update from previous version, otherwise an empty string is returned.
+  bool GetPrevVersion(chromeos::ErrorPtr* error,
+                      std::string* out_prev_version) override;
+
+  // Returns a list of available kernel partitions and whether each of them
+  // can be booted from or not.
+  bool GetKernelDevices(chromeos::ErrorPtr* error,
+                        std::string* out_kernel_devices) override;
+
+  // Returns the name of kernel partition that can be rolled back into.
+  bool GetRollbackPartition(chromeos::ErrorPtr* error,
+                            std::string* out_rollback_partition_name) override;
+
+ private:
+  SystemState* system_state_;
 };
 
-struct UpdateEngineServiceClass {
-  GObjectClass parent_class;
+// The UpdateEngineAdaptor class runs the UpdateEngineInterface in the fixed
+// object path, without an ObjectManager notifying the interfaces, since it is
+// all static and clients don't expect it to be implemented.
+class UpdateEngineAdaptor : public org::chromium::UpdateEngineInterfaceAdaptor {
+ public:
+  UpdateEngineAdaptor(SystemState* system_state,
+                      const scoped_refptr<dbus::Bus>& bus);
+  ~UpdateEngineAdaptor() = default;
+
+  // Register the DBus object with the update engine service asynchronously.
+  // Calls |copmletion_callback| when done passing a boolean indicating if the
+  // registration succeeded.
+  void RegisterAsync(const base::Callback<void(bool)>& completion_callback);
+
+  // Takes ownership of the well-known DBus name and returns whether it
+  // succeeded.
+  bool RequestOwnership();
+
+ private:
+  scoped_refptr<dbus::Bus> bus_;
+  UpdateEngineService dbus_service_;
+  chromeos::dbus_utils::DBusObject dbus_object_;
 };
 
-UpdateEngineService* update_engine_service_new(void);
-GType update_engine_service_get_type(void);
-
-// Methods
-
-gboolean update_engine_service_attempt_update(UpdateEngineService* self,
-                                              gchar* app_version,
-                                              gchar* omaha_url,
-                                              GError **error);
-
-gboolean update_engine_service_attempt_update_with_flags(
-    UpdateEngineService* self,
-    gchar* app_version,
-    gchar* omaha_url,
-    gint flags_as_int,
-    GError **error);
-
-gboolean update_engine_service_attempt_rollback(UpdateEngineService* self,
-                                                gboolean powerwash,
-                                                GError **error);
-
-// Checks if the system rollback is available by verifying if the secondary
-// system partition is valid and bootable.
-gboolean update_engine_service_can_rollback(
-    UpdateEngineService* self,
-    gboolean* out_can_rollback,
-    GError **error);
-
-// Returns the name of kernel partition that can be rolled back into.
-gboolean update_engine_service_get_rollback_partition(
-  UpdateEngineService* self,
-  gchar** out_rollback_partition_name,
-  GError **error);
-
-// Returns a list of available kernel partitions and whether each of them
-// can be booted from or not.
-gboolean update_engine_service_get_kernel_devices(UpdateEngineService* self,
-                                                  gchar** out_kernel_devices,
-                                                  GError **error);
-
-gboolean update_engine_service_reset_status(UpdateEngineService* self,
-                                            GError **error);
-
-gboolean update_engine_service_get_status(UpdateEngineService* self,
-                                          int64_t* last_checked_time,
-                                          double* progress,
-                                          gchar** current_operation,
-                                          gchar** new_version,
-                                          int64_t* new_size,
-                                          GError **error);
-
-gboolean update_engine_service_reboot_if_needed(UpdateEngineService* self,
-                                                GError **error);
-
-// Changes the current channel of the device to the target channel. If the
-// target channel is a less stable channel than the current channel, then the
-// channel change happens immediately (at the next update check).  If the
-// target channel is a more stable channel, then if is_powerwash_allowed is set
-// to true, then also the change happens immediately but with a powerwash if
-// required. Otherwise, the change takes effect eventually (when the version on
-// the target channel goes above the version number of what the device
-// currently has).
-gboolean update_engine_service_set_channel(UpdateEngineService* self,
-                                           gchar* target_channel,
-                                           gboolean is_powerwash_allowed,
-                                           GError **error);
-
-// If get_current_channel is set to true, populates |channel| with the name of
-// the channel that the device is currently on. Otherwise, it populates it with
-// the name of the channel the device is supposed to be (in case of a pending
-// channel change).
-gboolean update_engine_service_get_channel(UpdateEngineService* self,
-                                           gboolean get_current_channel,
-                                           gchar** channel,
-                                           GError **error);
-
-// Enables or disables the sharing and consuming updates over P2P feature
-// according to the |enabled| argument passed.
-gboolean update_engine_service_set_p2p_update_permission(
-    UpdateEngineService* self,
-    gboolean enabled,
-    GError **error);
-
-// Returns in |enabled| the current value for the P2P enabled setting. This
-// involves both sharing and consuming updates over P2P.
-gboolean update_engine_service_get_p2p_update_permission(
-    UpdateEngineService* self,
-    gboolean* enabled,
-    GError **error);
-
-// If there's no device policy installed, sets the update over cellular networks
-// permission to the |allowed| value. Otherwise, this method returns with an
-// error since this setting is overridden by the applied policy.
-gboolean update_engine_service_set_update_over_cellular_permission(
-    UpdateEngineService* self,
-    gboolean allowed,
-    GError **error);
-
-// Returns the current value of the update over cellular network setting, either
-// forced by the device policy if the device is enrolled or the current user
-// preference otherwise.
-gboolean update_engine_service_get_update_over_cellular_permission(
-    UpdateEngineService* self,
-    gboolean* allowed,
-    GError **error);
-
-// Returns the duration since the last successful update, as the
-// duration on the wallclock. Returns an error if the device has not
-// updated.
-gboolean update_engine_service_get_duration_since_update(
-    UpdateEngineService* self,
-    gint64* out_usec_wallclock,
-    GError **error);
-
-gboolean update_engine_service_emit_status_update(
-    UpdateEngineService* self,
-    gint64 last_checked_time,
-    gdouble progress,
-    const gchar* current_operation,
-    const gchar* new_version,
-    gint64 new_size);
-
-// Returns the version string of OS that was used before the last reboot
-// into an updated version. This is available only when rebooting into an
-// update from previous version, otherwise an empty string is returned.
-gboolean update_engine_service_get_prev_version(
-    UpdateEngineService* self,
-    gchar** prev_version,
-    GError **error);
-
-G_END_DECLS
+}  // namespace chromeos_update_engine
 
 #endif  // UPDATE_ENGINE_DBUS_SERVICE_H_