Add DBus hiding client library

This library hides update_engine's legacy DBus dependencies until we
can remove them completely.

Bug: 24547247
Test: mmm system/update_engine; emerge-panther update_engine

Change-Id: I7f87f2a7c31d0940c376ef43368e53b0f3bc3407
diff --git a/client_library/client.cc b/client_library/client.cc
new file mode 100644
index 0000000..d6e7382
--- /dev/null
+++ b/client_library/client.cc
@@ -0,0 +1,31 @@
+//
+// Copyright (C) 2015 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+#include "update_engine/client_library/include/update_engine/client.h"
+
+#include <memory>
+
+#include "update_engine/client_library/client_impl.h"
+
+using std::unique_ptr;
+
+namespace update_engine {
+
+std::unique_ptr<UpdateEngineClient> UpdateEngineClient::CreateInstance() {
+  return unique_ptr<UpdateEngineClient>{new internal::UpdateEngineClientImpl{}};
+}
+
+}  // namespace update_engine
diff --git a/client_library/client_impl.cc b/client_library/client_impl.cc
new file mode 100644
index 0000000..84ca184
--- /dev/null
+++ b/client_library/client_impl.cc
@@ -0,0 +1,91 @@
+//
+// Copyright (C) 2015 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+#include "update_engine/client_library/client_impl.h"
+
+#include <dbus/bus.h>
+#include <update_engine/dbus-constants.h>
+
+#include "update_engine/update_status_utils.h"
+
+using chromeos_update_engine::StringToUpdateStatus;
+using dbus::Bus;
+using org::chromium::UpdateEngineInterfaceProxy;
+using std::string;
+
+namespace update_engine {
+namespace internal {
+
+UpdateEngineClientImpl::UpdateEngineClientImpl() {
+  Bus::Options options;
+  options.bus_type = Bus::SYSTEM;
+  scoped_refptr<Bus> bus{new Bus{options}};
+  proxy_.reset(new UpdateEngineInterfaceProxy{bus});
+}
+
+bool UpdateEngineClientImpl::AttemptUpdate(const string& in_app_version,
+                                           const string& in_omaha_url,
+                                           bool at_user_request) {
+  return proxy_->AttemptUpdateWithFlags(
+      in_app_version,
+      in_omaha_url,
+      (at_user_request) ? 0 : kAttemptUpdateFlagNonInteractive,
+      nullptr);
+}
+
+bool UpdateEngineClientImpl::GetStatus(int64_t* out_last_checked_time,
+                                       double* out_progress,
+                                       UpdateStatus* out_update_status,
+                                       string* out_new_version,
+                                       int64_t* out_new_size) {
+  string status_as_string;
+  const bool success = proxy_->GetStatus(
+      out_last_checked_time,
+      out_progress,
+      &status_as_string,
+      out_new_version,
+      out_new_size,
+      nullptr);
+  if (!success) {
+    return false;
+  }
+
+  return StringToUpdateStatus(status_as_string, out_update_status);
+}
+
+bool UpdateEngineClientImpl::SetTargetChannel(const string& in_target_channel) {
+  return proxy_->SetChannel(
+      in_target_channel,
+      true,
+      nullptr);
+}
+
+bool UpdateEngineClientImpl::GetTargetChannel(string* out_channel) {
+  return proxy_->GetChannel(
+      false,  // Get the target channel.
+      out_channel,
+      nullptr);
+}
+
+bool UpdateEngineClientImpl::GetChannel(string* out_channel) {
+  return proxy_->GetChannel(
+      true,  // Get the current channel.
+      out_channel,
+      nullptr);
+}
+
+}  // namespace internal
+}  // namespace update_engine
diff --git a/client_library/client_impl.h b/client_library/client_impl.h
new file mode 100644
index 0000000..a16136c
--- /dev/null
+++ b/client_library/client_impl.h
@@ -0,0 +1,62 @@
+//
+// Copyright (C) 2015 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+#ifndef UPDATE_ENGINE_CLIENT_LIBRARY_CLIENT_IMPL_H_
+#define UPDATE_ENGINE_CLIENT_LIBRARY_CLIENT_IMPL_H_
+
+#include <cstdint>
+#include <memory>
+#include <string>
+
+#include <base/macros.h>
+
+#include "update_engine/dbus-proxies.h"
+#include "update_engine/client_library/include/update_engine/client.h"
+
+namespace update_engine {
+namespace internal {
+
+class UpdateEngineClientImpl : public UpdateEngineClient {
+ public:
+  UpdateEngineClientImpl();
+  virtual ~UpdateEngineClientImpl() = default;
+
+  bool AttemptUpdate(const std::string& app_version,
+                     const std::string& omaha_url,
+                     bool at_user_request) override;
+
+  bool GetStatus(int64_t* out_last_checked_time,
+                 double* out_progress,
+                 UpdateStatus* out_update_status,
+                 std::string* out_new_version,
+                 int64_t* out_new_size) override;
+
+  bool SetTargetChannel(const std::string& target_channel) override;
+
+  bool GetTargetChannel(std::string* out_channel) override;
+
+  bool GetChannel(std::string* out_channel) override;
+
+ private:
+  std::unique_ptr<org::chromium::UpdateEngineInterfaceProxy> proxy_;
+
+  DISALLOW_COPY_AND_ASSIGN(UpdateEngineClientImpl);
+};  // class UpdateEngineClientImpl
+
+}  // namespace internal
+}  // namespace update_engine
+
+#endif  // UPDATE_ENGINE_CLIENT_LIBRARY_CLIENT_IMPL_H_
diff --git a/client_library/include/update_engine/client.h b/client_library/include/update_engine/client.h
new file mode 100644
index 0000000..ec39253
--- /dev/null
+++ b/client_library/include/update_engine/client.h
@@ -0,0 +1,89 @@
+//
+// Copyright (C) 2015 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+#ifndef UDPATE_ENGINE_CLIENT_LIBRARY_INCLUDE_UPDATE_ENGINE_CLIENT_H_
+#define UDPATE_ENGINE_CLIENT_LIBRARY_INCLUDE_UPDATE_ENGINE_CLIENT_H_
+
+#include <cstdint>
+#include <memory>
+#include <string>
+
+#include "update_engine/update_status.h"
+
+namespace update_engine {
+
+class UpdateEngineClient {
+ public:
+  static std::unique_ptr<UpdateEngineClient> CreateInstance();
+
+  virtual ~UpdateEngineClient() = default;
+
+  // Force the update_engine to attempt an update.
+  // |app_version|
+  //     Attempt to update to this version.  An empty string indicates that
+  //     update engine should pick the most recent image on the current channel.
+  // |omaha_url|
+  //     Force update_engine to look for updates from the given server.  Passing
+  //     empty indicates update_engine should get this parameter from its
+  //     config.  Note that update_engine will ignore this parameter in
+  //     production mode to avoid pulling untrusted updates.
+  // |at_user_request|
+  //     This update was directly requested by the user.
+  virtual bool AttemptUpdate(const std::string& app_version,
+                             const std::string& omaha_url,
+                             bool at_user_request) = 0;
+
+  // Returns the current status of the Update Engine.
+  //
+  // |out_last_checked_time|
+  //     the last time the update engine checked for an update in seconds since
+  //     the epoc.
+  // |out_progress|
+  //     when downloading an update, this is calculated as
+  //     (number of bytes received) / (total bytes).
+  // |out_update_status|
+  //     See update_status.h.
+  // |out_new_version|
+  //     string version of the new system image.
+  // |out_new_size|
+  //     number of bytes in the new system image.
+  virtual bool GetStatus(int64_t* out_last_checked_time,
+                         double* out_progress,
+                         UpdateStatus* out_update_status,
+                         std::string* out_new_version,
+                         int64_t* out_new_size) = 0;
+
+  // Changes the current channel of the device to the target channel.
+  virtual bool SetTargetChannel(const std::string& target_channel) = 0;
+
+  // Get the channel the device will switch to on reboot.
+  virtual bool GetTargetChannel(std::string* out_channel) = 0;
+
+  // Get the channel the device is currently on.
+  virtual bool GetChannel(std::string* out_channel) = 0;
+
+ protected:
+  // Use CreateInstance().
+  UpdateEngineClient() = default;
+
+ private:
+  UpdateEngineClient(const UpdateEngineClient&) = delete;
+  void operator=(const UpdateEngineClient&) = delete;
+};  // class UpdateEngineClient
+
+}  // namespace update_engine
+
+#endif  // UDPATE_ENGINE_CLIENT_LIBRARY_INCLUDE_UPDATE_ENGINE_CLIENT_H_