update_engine: Break UpdateEngineDaemon into their own implementations

It looks like Android and ChromeOS have completely different
implementation of the daemon. So instead of polluting the source with
USE_* flags, just break it into their own implementation files. At the
very least this allows deprecating USE_BINDER and USE_OMAHA flag
completely.

BUG=chromium:978672
TEST=unittest, cros flash two times.

Change-Id: Ia5c4f9274e275a2c1ba9334111b694514914a475
Reviewed-on: https://chromium-review.googlesource.com/1674583
Tested-by: Amin Hassani <ahassani@chromium.org>
Commit-Ready: Amin Hassani <ahassani@chromium.org>
Legacy-Commit-Queue: Commit Bot <commit-bot@chromium.org>
Reviewed-by: Amin Hassani <ahassani@chromium.org>
diff --git a/Android.bp b/Android.bp
index 9031913..d9f3524 100644
--- a/Android.bp
+++ b/Android.bp
@@ -29,12 +29,10 @@
 
     cflags: [
         "-DBASE_VER=576279",
-        "-DUSE_BINDER=1",
         "-DUSE_CHROME_NETWORK_PROXY=0",
         "-DUSE_CHROME_KIOSK_APP=0",
         "-DUSE_HWID_OVERRIDE=0",
         "-DUSE_MTD=0",
-        "-DUSE_OMAHA=0",
         "-D_FILE_OFFSET_BITS=64",
         "-D_POSIX_C_SOURCE=199309L",
         "-Wa,--noexecstack",
@@ -269,7 +267,7 @@
         ":libupdate_engine_aidl",
         "binder_service_android.cc",
         "certificate_checker.cc",
-        "daemon.cc",
+        "daemon_android.cc",
         "daemon_state_android.cc",
         "hardware_android.cc",
         "libcurl_http_fetcher.cc",
diff --git a/BUILD.gn b/BUILD.gn
index 1279938..14c7a92 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -69,14 +69,12 @@
     "__CHROMEOS__",
     "_FILE_OFFSET_BITS=64",
     "_POSIX_C_SOURCE=199309L",
-    "USE_BINDER=0",
     "USE_DBUS=${use.dbus}",
     "USE_FEC=0",
     "USE_HWID_OVERRIDE=${use.hwid_override}",
     "USE_CHROME_KIOSK_APP=${use.chrome_kiosk_app}",
     "USE_CHROME_NETWORK_PROXY=${use.chrome_network_proxy}",
     "USE_MTD=${use.mtd}",
-    "USE_OMAHA=1",
     "USE_SHILL=1",
   ]
   include_dirs = [
@@ -194,7 +192,7 @@
     "common_service.cc",
     "connection_manager.cc",
     "connection_utils.cc",
-    "daemon.cc",
+    "daemon_chromeos.cc",
     "dbus_connection.cc",
     "dbus_service.cc",
     "hardware_chromeos.cc",
diff --git a/daemon_android.cc b/daemon_android.cc
new file mode 100644
index 0000000..1aa921f
--- /dev/null
+++ b/daemon_android.cc
@@ -0,0 +1,64 @@
+//
+// 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/daemon_android.h"
+
+#include <sysexits.h>
+
+#include <binderwrapper/binder_wrapper.h>
+
+#include "update_engine/daemon_state_android.h"
+
+using std::unique_ptr;
+
+namespace chromeos_update_engine {
+
+unique_ptr<DaemonBase> DaemonBase::CreateInstance() {
+  return std::make_unique<DaemonAndroid>();
+}
+
+int DaemonAndroid::OnInit() {
+  // Register the |subprocess_| singleton with this Daemon as the signal
+  // handler.
+  subprocess_.Init(this);
+
+  int exit_code = brillo::Daemon::OnInit();
+  if (exit_code != EX_OK)
+    return exit_code;
+
+  android::BinderWrapper::Create();
+  binder_watcher_.Init();
+
+  DaemonStateAndroid* daemon_state_android = new DaemonStateAndroid();
+  daemon_state_.reset(daemon_state_android);
+  LOG_IF(ERROR, !daemon_state_android->Initialize())
+      << "Failed to initialize system state.";
+
+  // Create the Binder Service.
+  binder_service_ = new BinderUpdateEngineAndroidService{
+      daemon_state_android->service_delegate()};
+  auto binder_wrapper = android::BinderWrapper::Get();
+  if (!binder_wrapper->RegisterService(binder_service_->ServiceName(),
+                                       binder_service_)) {
+    LOG(ERROR) << "Failed to register binder service.";
+  }
+
+  daemon_state_->AddObserver(binder_service_.get());
+  daemon_state_->StartUpdater();
+  return EX_OK;
+}
+
+}  // namespace chromeos_update_engine
diff --git a/daemon.h b/daemon_android.h
similarity index 60%
copy from daemon.h
copy to daemon_android.h
index 3c896bc..baead37 100644
--- a/daemon.h
+++ b/daemon_android.h
@@ -14,63 +14,43 @@
 // limitations under the License.
 //
 
-#ifndef UPDATE_ENGINE_DAEMON_H_
-#define UPDATE_ENGINE_DAEMON_H_
+#ifndef UPDATE_ENGINE_DAEMON_ANDROID_H_
+#define UPDATE_ENGINE_DAEMON_ANDROID_H_
 
 #include <memory>
-#include <string>
 
-#if USE_BINDER
 #include <brillo/binder_watcher.h>
-#endif  // USE_BINDER
-#include <brillo/daemons/daemon.h>
 
-#if USE_BINDER
 #include "update_engine/binder_service_android.h"
-#endif  // USE_BINDER
 #include "update_engine/common/subprocess.h"
+#include "update_engine/daemon_base.h"
 #include "update_engine/daemon_state_interface.h"
-#if USE_DBUS
-#include "update_engine/dbus_service.h"
-#endif  // USE_DBUS
 
 namespace chromeos_update_engine {
 
-class UpdateEngineDaemon : public brillo::Daemon {
+class DaemonAndroid : public DaemonBase {
  public:
-  UpdateEngineDaemon() = default;
+  DaemonAndroid() = default;
 
  protected:
   int OnInit() override;
 
  private:
-#if USE_DBUS
-  // Run from the main loop when the |dbus_adaptor_| object is registered. At
-  // this point we can request ownership of the DBus service name and continue
-  // initialization.
-  void OnDBusRegistered(bool succeeded);
-
-  // Main D-Bus service adaptor.
-  std::unique_ptr<UpdateEngineAdaptor> dbus_adaptor_;
-#endif  // USE_DBUS
-
-  // The Subprocess singleton class requires a brillo::MessageLoop in the
+  // The Subprocess singleton class requires a |brillo::MessageLoop| in the
   // current thread, so we need to initialize it from this class instead of
   // the main() function.
   Subprocess subprocess_;
 
-#if USE_BINDER
   brillo::BinderWatcher binder_watcher_;
   android::sp<BinderUpdateEngineAndroidService> binder_service_;
-#endif  // USE_BINDER
 
   // The daemon state with all the required daemon classes for the configured
   // platform.
   std::unique_ptr<DaemonStateInterface> daemon_state_;
 
-  DISALLOW_COPY_AND_ASSIGN(UpdateEngineDaemon);
+  DISALLOW_COPY_AND_ASSIGN(DaemonAndroid);
 };
 
 }  // namespace chromeos_update_engine
 
-#endif  // UPDATE_ENGINE_DAEMON_H_
+#endif  // UPDATE_ENGINE_DAEMON_ANDROID_H_
diff --git a/daemon_base.h b/daemon_base.h
new file mode 100644
index 0000000..742a0ba
--- /dev/null
+++ b/daemon_base.h
@@ -0,0 +1,40 @@
+//
+// Copyright (C) 2019 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_DAEMON_BASE_H_
+#define UPDATE_ENGINE_DAEMON_BASE_H_
+
+#include <memory>
+
+#include <brillo/daemons/daemon.h>
+
+namespace chromeos_update_engine {
+
+class DaemonBase : public brillo::Daemon {
+ public:
+  DaemonBase() = default;
+  virtual ~DaemonBase() = default;
+
+  // Creates an instance of the daemon.
+  static std::unique_ptr<DaemonBase> CreateInstance();
+
+ private:
+  DISALLOW_COPY_AND_ASSIGN(DaemonBase);
+};
+
+}  // namespace chromeos_update_engine
+
+#endif  // UPDATE_ENGINE_DAEMON_BASE_H_
diff --git a/daemon.cc b/daemon_chromeos.cc
similarity index 61%
rename from daemon.cc
rename to daemon_chromeos.cc
index f370564..21740d8 100644
--- a/daemon.cc
+++ b/daemon_chromeos.cc
@@ -14,25 +14,25 @@
 // limitations under the License.
 //
 
-#include "update_engine/daemon.h"
+#include "update_engine/daemon_chromeos.h"
 
 #include <sysexits.h>
 
 #include <base/bind.h>
 #include <base/location.h>
-#if USE_BINDER
-#include <binderwrapper/binder_wrapper.h>
-#endif  // USE_BINDER
 
-#if USE_OMAHA
 #include "update_engine/real_system_state.h"
-#else  // !USE_OMAHA
-#include "update_engine/daemon_state_android.h"
-#endif  // USE_OMAHA
+
+using brillo::Daemon;
+using std::unique_ptr;
 
 namespace chromeos_update_engine {
 
-int UpdateEngineDaemon::OnInit() {
+unique_ptr<DaemonBase> DaemonBase::CreateInstance() {
+  return std::make_unique<DaemonChromeOS>();
+}
+
+int DaemonChromeOS::OnInit() {
   // Register the |subprocess_| singleton with this Daemon as the signal
   // handler.
   subprocess_.Init(this);
@@ -41,12 +41,6 @@
   if (exit_code != EX_OK)
     return exit_code;
 
-#if USE_BINDER
-  android::BinderWrapper::Create();
-  binder_watcher_.Init();
-#endif  // USE_BINDER
-
-#if USE_OMAHA
   // Initialize update engine global state but continue if something fails.
   // TODO(deymo): Move the daemon_state_ initialization to a factory method
   // avoiding the explicit re-usage of the |bus| instance, shared between
@@ -55,42 +49,18 @@
   daemon_state_.reset(real_system_state);
   LOG_IF(ERROR, !real_system_state->Initialize())
       << "Failed to initialize system state.";
-#else  // !USE_OMAHA
-  DaemonStateAndroid* daemon_state_android = new DaemonStateAndroid();
-  daemon_state_.reset(daemon_state_android);
-  LOG_IF(ERROR, !daemon_state_android->Initialize())
-      << "Failed to initialize system state.";
-#endif  // USE_OMAHA
 
-#if USE_BINDER
-  // Create the Binder Service.
-  binder_service_ = new BinderUpdateEngineAndroidService{
-      daemon_state_android->service_delegate()};
-  auto binder_wrapper = android::BinderWrapper::Get();
-  if (!binder_wrapper->RegisterService(binder_service_->ServiceName(),
-                                       binder_service_)) {
-    LOG(ERROR) << "Failed to register binder service.";
-  }
-
-  daemon_state_->AddObserver(binder_service_.get());
-#endif  // USE_BINDER
-
-#if USE_DBUS
   // Create the DBus service.
   dbus_adaptor_.reset(new UpdateEngineAdaptor(real_system_state));
   daemon_state_->AddObserver(dbus_adaptor_.get());
 
-  dbus_adaptor_->RegisterAsync(base::Bind(&UpdateEngineDaemon::OnDBusRegistered,
-                                          base::Unretained(this)));
+  dbus_adaptor_->RegisterAsync(
+      base::Bind(&DaemonChromeOS::OnDBusRegistered, base::Unretained(this)));
   LOG(INFO) << "Waiting for DBus object to be registered.";
-#else   // !USE_DBUS
-  daemon_state_->StartUpdater();
-#endif  // USE_DBUS
   return EX_OK;
 }
 
-#if USE_DBUS
-void UpdateEngineDaemon::OnDBusRegistered(bool succeeded) {
+void DaemonChromeOS::OnDBusRegistered(bool succeeded) {
   if (!succeeded) {
     LOG(ERROR) << "Registering the UpdateEngineAdaptor";
     QuitWithExitCode(1);
@@ -108,6 +78,5 @@
   }
   daemon_state_->StartUpdater();
 }
-#endif  // USE_DBUS
 
 }  // namespace chromeos_update_engine
diff --git a/daemon.h b/daemon_chromeos.h
similarity index 70%
rename from daemon.h
rename to daemon_chromeos.h
index 3c896bc..657e797 100644
--- a/daemon.h
+++ b/daemon_chromeos.h
@@ -14,37 +14,26 @@
 // limitations under the License.
 //
 
-#ifndef UPDATE_ENGINE_DAEMON_H_
-#define UPDATE_ENGINE_DAEMON_H_
+#ifndef UPDATE_ENGINE_DAEMON_CHROMEOS_H_
+#define UPDATE_ENGINE_DAEMON_CHROMEOS_H_
 
 #include <memory>
-#include <string>
 
-#if USE_BINDER
-#include <brillo/binder_watcher.h>
-#endif  // USE_BINDER
-#include <brillo/daemons/daemon.h>
-
-#if USE_BINDER
-#include "update_engine/binder_service_android.h"
-#endif  // USE_BINDER
 #include "update_engine/common/subprocess.h"
+#include "update_engine/daemon_base.h"
 #include "update_engine/daemon_state_interface.h"
-#if USE_DBUS
 #include "update_engine/dbus_service.h"
-#endif  // USE_DBUS
 
 namespace chromeos_update_engine {
 
-class UpdateEngineDaemon : public brillo::Daemon {
+class DaemonChromeOS : public DaemonBase {
  public:
-  UpdateEngineDaemon() = default;
+  DaemonChromeOS() = default;
 
  protected:
   int OnInit() override;
 
  private:
-#if USE_DBUS
   // Run from the main loop when the |dbus_adaptor_| object is registered. At
   // this point we can request ownership of the DBus service name and continue
   // initialization.
@@ -52,25 +41,19 @@
 
   // Main D-Bus service adaptor.
   std::unique_ptr<UpdateEngineAdaptor> dbus_adaptor_;
-#endif  // USE_DBUS
 
   // The Subprocess singleton class requires a brillo::MessageLoop in the
   // current thread, so we need to initialize it from this class instead of
   // the main() function.
   Subprocess subprocess_;
 
-#if USE_BINDER
-  brillo::BinderWatcher binder_watcher_;
-  android::sp<BinderUpdateEngineAndroidService> binder_service_;
-#endif  // USE_BINDER
-
   // The daemon state with all the required daemon classes for the configured
   // platform.
   std::unique_ptr<DaemonStateInterface> daemon_state_;
 
-  DISALLOW_COPY_AND_ASSIGN(UpdateEngineDaemon);
+  DISALLOW_COPY_AND_ASSIGN(DaemonChromeOS);
 };
 
 }  // namespace chromeos_update_engine
 
-#endif  // UPDATE_ENGINE_DAEMON_H_
+#endif  // UPDATE_ENGINE_DAEMON_CHROMEOS_H_
diff --git a/libcurl_http_fetcher.cc b/libcurl_http_fetcher.cc
index f469435..d39351c 100644
--- a/libcurl_http_fetcher.cc
+++ b/libcurl_http_fetcher.cc
@@ -269,11 +269,11 @@
     } else if (base::StartsWith(
                    url_, "https://", base::CompareCase::INSENSITIVE_ASCII)) {
       SetCurlOptionsForHttps();
-#if !USE_OMAHA
+#ifdef __ANDROID__
     } else if (base::StartsWith(
                    url_, "file://", base::CompareCase::INSENSITIVE_ASCII)) {
       SetCurlOptionsForFile();
-#endif
+#endif  // __ANDROID__
     } else {
       LOG(ERROR) << "Received invalid URI: " << url_;
       // Lock down to no protocol supported for the transfer.
diff --git a/main.cc b/main.cc
index 26f9efb..b435467 100644
--- a/main.cc
+++ b/main.cc
@@ -33,9 +33,10 @@
 #include <base/strings/stringprintf.h>
 #include <brillo/flag_helper.h>
 
+#include "update_engine/common/subprocess.h"
 #include "update_engine/common/terminator.h"
 #include "update_engine/common/utils.h"
-#include "update_engine/daemon.h"
+#include "update_engine/daemon_base.h"
 
 using std::string;
 
@@ -190,8 +191,8 @@
   // Done _after_ log file creation.
   umask(S_IRWXG | S_IRWXO);
 
-  chromeos_update_engine::UpdateEngineDaemon update_engine_daemon;
-  int exit_code = update_engine_daemon.Run();
+  auto daemon = chromeos_update_engine::DaemonBase::CreateInstance();
+  int exit_code = daemon->Run();
 
   chromeos_update_engine::Subprocess::Get().FlushBufferedLogsAtExit();