update_engine: Switch to use Binder interface to weaved

Now that weaved provides a binder interface for its IPC, switch
to using it instead.

Bug: 23782171
Change-Id: I0b981b366a7dc42aabc9b61c4e9f90e26a2d74b4
diff --git a/Android.mk b/Android.mk
index e4a9574..fa6c067 100644
--- a/Android.mk
+++ b/Android.mk
@@ -251,6 +251,8 @@
     $(ue_update_metadata_protos_exported_shared_libraries)
 ifeq ($(BRILLO_USE_WEAVE),1)
 ue_libupdate_engine_exported_shared_libraries += \
+    libbinderwrapper \
+    libbrillo-binder \
     libweaved
 endif  # BRILLO_USE_WEAVE == 1
 
diff --git a/daemon.cc b/daemon.cc
index 0b13c18..1e39c8b 100644
--- a/daemon.cc
+++ b/daemon.cc
@@ -21,6 +21,9 @@
 #include <base/bind.h>
 #include <base/location.h>
 #include <base/time/time.h>
+#if USE_WEAVE
+#include <binderwrapper/binder_wrapper.h>
+#endif  // USE_WEAVE
 #include <brillo/message_loops/message_loop.h>
 
 #include "update_engine/common/clock.h"
@@ -74,6 +77,11 @@
   if (exit_code != EX_OK)
     return exit_code;
 
+#if USE_WEAVE
+  android::BinderWrapper::Create();
+  binder_watcher_.Init();
+#endif  // USE_WEAVE
+
   dbus::Bus::Options options;
   options.bus_type = dbus::Bus::SYSTEM;
   bus_ = new dbus::Bus(options);
diff --git a/daemon.h b/daemon.h
index 66841f6..16f22c7 100644
--- a/daemon.h
+++ b/daemon.h
@@ -20,6 +20,9 @@
 #include <memory>
 #include <string>
 
+#if USE_WEAVE
+#include <brillo/binder_watcher.h>
+#endif  // USE_WEAVE
 #include <brillo/daemons/dbus_daemon.h>
 
 #include "update_engine/common/subprocess.h"
@@ -47,6 +50,10 @@
   // the main() function.
   Subprocess subprocess_;
 
+#if USE_WEAVE
+  brillo::BinderWatcher binder_watcher_;
+#endif  // USE_WEAVE
+
   std::unique_ptr<UpdateEngineAdaptor> dbus_adaptor_;
 
   // The RealSystemState uses the previous classes so it should be defined last.
diff --git a/real_system_state.cc b/real_system_state.cc
index ffb2193..349167b 100644
--- a/real_system_state.cc
+++ b/real_system_state.cc
@@ -33,8 +33,7 @@
 namespace chromeos_update_engine {
 
 RealSystemState::RealSystemState(const scoped_refptr<dbus::Bus>& bus)
-    : bus_(bus),
-      debugd_proxy_(bus),
+    : debugd_proxy_(bus),
       power_manager_proxy_(bus),
       session_manager_proxy_(bus),
       shill_proxy_(bus),
@@ -125,7 +124,7 @@
                           &debugd_proxy_));
   update_attempter_->Init();
 
-  weave_service_ = ConstructWeaveService(bus_, update_attempter_.get());
+  weave_service_ = ConstructWeaveService(update_attempter_.get());
 
   // Initialize the Update Manager using the default state factory.
   chromeos_update_manager::State* um_state =
diff --git a/real_system_state.h b/real_system_state.h
index d7ee712..3aaa9ec 100644
--- a/real_system_state.h
+++ b/real_system_state.h
@@ -115,9 +115,6 @@
   inline bool system_rebooted() override { return system_rebooted_; }
 
  private:
-  // Reference to the DBus bus.
-  scoped_refptr<dbus::Bus> bus_;
-
   // Real DBus proxies using the DBus connection.
   org::chromium::debugdProxy debugd_proxy_;
   org::chromium::PowerManagerProxy power_manager_proxy_;
diff --git a/weave_service.cc b/weave_service.cc
index 367359a..5825324 100644
--- a/weave_service.cc
+++ b/weave_service.cc
@@ -20,8 +20,8 @@
 #include <string>
 
 #include <base/bind.h>
-#include <base/values.h>
 #include <brillo/errors/error.h>
+#include <brillo/message_loops/message_loop.h>
 
 #include "update_engine/update_status_utils.h"
 
@@ -35,26 +35,37 @@
 
 namespace chromeos_update_engine {
 
-bool WeaveService::Init(scoped_refptr<dbus::Bus> bus,
-                        DelegateInterface* delegate) {
+bool WeaveService::Init(DelegateInterface* delegate) {
   delegate_ = delegate;
-  device_ = weaved::Device::CreateInstance(
-      bus, base::Bind(&WeaveService::UpdateWeaveState, base::Unretained(this)));
-  device_->AddComponent(kWeaveComponent, {"_updater"});
-  device_->AddCommandHandler(
-      kWeaveComponent,
-      "_updater.checkForUpdates",
-      base::Bind(&WeaveService::OnCheckForUpdates, base::Unretained(this)));
-  device_->AddCommandHandler(
-      kWeaveComponent,
-      "_updater.trackChannel",
-      base::Bind(&WeaveService::OnTrackChannel, base::Unretained(this)));
-
+  weave_service_subscription_ = weaved::Service::Connect(
+      brillo::MessageLoop::current(),
+      base::Bind(&WeaveService::OnWeaveServiceConnected,
+                 base::Unretained(this)));
   return true;
 }
 
+void WeaveService::OnWeaveServiceConnected(
+    const std::weak_ptr<weaved::Service>& service) {
+  weave_service_ = service;
+  auto weave_service = weave_service_.lock();
+  if (!weave_service)
+    return;
+
+  weave_service->AddComponent(kWeaveComponent, {"_updater"}, nullptr);
+  weave_service->AddCommandHandler(
+      kWeaveComponent,
+      "_updater.checkForUpdates",
+      base::Bind(&WeaveService::OnCheckForUpdates, base::Unretained(this)));
+  weave_service->AddCommandHandler(
+      kWeaveComponent,
+      "_updater.trackChannel",
+      base::Bind(&WeaveService::OnTrackChannel, base::Unretained(this)));
+  UpdateWeaveState();
+}
+
 void WeaveService::UpdateWeaveState() {
-  if (!device_ || !delegate_)
+  auto weave_service = weave_service_.lock();
+  if (!weave_service || !delegate_)
     return;
 
   int64_t last_checked_time;
@@ -83,17 +94,12 @@
        static_cast<double>(last_checked_time)},
   };
 
-  if (!device_->SetStateProperties(kWeaveComponent, state, nullptr)) {
+  if (!weave_service->SetStateProperties(kWeaveComponent, state, nullptr)) {
     LOG(ERROR) << "Failed to update _updater state.";
   }
 }
 
-void WeaveService::OnCheckForUpdates(
-    const std::weak_ptr<weaved::Command>& cmd) {
-  auto command = cmd.lock();
-  if (!command)
-    return;
-
+void WeaveService::OnCheckForUpdates(std::unique_ptr<weaved::Command> command) {
   brillo::ErrorPtr error;
   if (!delegate_->OnCheckForUpdates(&error)) {
     command->Abort(error->GetCode(), error->GetMessage(), nullptr);
@@ -102,11 +108,7 @@
   command->Complete({}, nullptr);
 }
 
-void WeaveService::OnTrackChannel(const std::weak_ptr<weaved::Command>& cmd) {
-  auto command = cmd.lock();
-  if (!command)
-    return;
-
+void WeaveService::OnTrackChannel(std::unique_ptr<weaved::Command> command) {
   string channel = command->GetParameter<string>("channel");
   brillo::ErrorPtr error;
   if (!delegate_->OnTrackChannel(channel, &error)) {
diff --git a/weave_service.h b/weave_service.h
index 6790d54..914777e 100644
--- a/weave_service.h
+++ b/weave_service.h
@@ -19,9 +19,9 @@
 
 #include <memory>
 
-#include <dbus/bus.h>
+#include <base/memory/weak_ptr.h>
 #include <libweaved/command.h>
-#include <libweaved/device.h>
+#include <libweaved/service.h>
 
 #include "update_engine/weave_service_interface.h"
 
@@ -32,20 +32,23 @@
   WeaveService() = default;
   ~WeaveService() override = default;
 
-  bool Init(scoped_refptr<dbus::Bus> bus, DelegateInterface* delegate);
+  bool Init(DelegateInterface* delegate);
 
   // WeaveServiceInterface override.
   void UpdateWeaveState() override;
 
  private:
+  void OnWeaveServiceConnected(const std::weak_ptr<weaved::Service>& service);
+
   // Weave command handlers. These are called from the message loop whenever a
   // command is received and dispatch the synchronous call to the |delegate_|.
-  void OnCheckForUpdates(const std::weak_ptr<weaved::Command>& cmd);
-  void OnTrackChannel(const std::weak_ptr<weaved::Command>& cmd);
+  void OnCheckForUpdates(std::unique_ptr<weaved::Command> cmd);
+  void OnTrackChannel(std::unique_ptr<weaved::Command> cmd);
 
   WeaveServiceInterface::DelegateInterface* delegate_{nullptr};
 
-  std::unique_ptr<weaved::Device> device_;
+  std::unique_ptr<weaved::Service::Subscription> weave_service_subscription_;
+  std::weak_ptr<weaved::Service> weave_service_;
 };
 
 }  // namespace chromeos_update_engine
diff --git a/weave_service_factory.cc b/weave_service_factory.cc
index 793e906..24b9b79 100644
--- a/weave_service_factory.cc
+++ b/weave_service_factory.cc
@@ -23,16 +23,15 @@
 namespace chromeos_update_engine {
 
 std::unique_ptr<WeaveServiceInterface> ConstructWeaveService(
-    const scoped_refptr<dbus::Bus>& bus,
     WeaveServiceInterface::DelegateInterface* delegate) {
   std::unique_ptr<WeaveServiceInterface> result;
-  if (!delegate || !bus.get())
+  if (!delegate)
     return result;
 
 #if USE_WEAVE
   WeaveService* weave_service = new WeaveService();
   result.reset(weave_service);
-  if (!weave_service->Init(bus, delegate))
+  if (!weave_service->Init(delegate))
     result.reset();
 #endif
   return result;
diff --git a/weave_service_factory.h b/weave_service_factory.h
index b32e0b7..7b129ab 100644
--- a/weave_service_factory.h
+++ b/weave_service_factory.h
@@ -20,7 +20,6 @@
 #include <memory>
 
 #include <base/memory/ref_counted.h>
-#include <dbus/bus.h>
 
 #include "update_engine/weave_service_interface.h"
 
@@ -29,7 +28,6 @@
 // Create a new WeaveServiceInterface instance. In case of error or when weaved
 // is disabled, returns an empty pointer.
 std::unique_ptr<WeaveServiceInterface> ConstructWeaveService(
-    const scoped_refptr<dbus::Bus>& bus,
     WeaveServiceInterface::DelegateInterface* delegate);
 
 }  // namespace chromeos_update_engine