update_engine: Make SystemState accessible from everywhere

SystemState is supposed to be a global context and is used lamost
everywhere. So instead of passing it to functions and keeping multiple
pointers to it, its better to do what we did in dlcservice and make it a
singleton class with a getter that can be get from everywhere.

BUG=b:171829801
TEST=unittests

Change-Id: I3b2de9394b7769b3911195ca52d61dbe49afd4dd
Reviewed-on: https://chromium-review.googlesource.com/c/aosp/platform/system/update_engine/+/2521792
Commit-Queue: Amin Hassani <ahassani@chromium.org>
Tested-by: Amin Hassani <ahassani@chromium.org>
Reviewed-by: Jae Hoon Kim <kimjae@chromium.org>
diff --git a/cros/common_service.cc b/cros/common_service.cc
index aecad8b..0318999 100644
--- a/cros/common_service.cc
+++ b/cros/common_service.cc
@@ -29,6 +29,7 @@
 #include "update_engine/common/clock_interface.h"
 #include "update_engine/common/hardware_interface.h"
 #include "update_engine/common/prefs.h"
+#include "update_engine/common/system_state.h"
 #include "update_engine/common/utils.h"
 #include "update_engine/cros/connection_manager_interface.h"
 #include "update_engine/cros/omaha_request_params.h"
@@ -66,8 +67,7 @@
 const char* const UpdateEngineService::kErrorFailed =
     "org.chromium.UpdateEngine.Error.Failed";
 
-UpdateEngineService::UpdateEngineService(SystemState* system_state)
-    : system_state_(system_state) {}
+UpdateEngineService::UpdateEngineService() = default;
 
 // org::chromium::UpdateEngineInterfaceInterface methods implementation.
 
@@ -79,7 +79,7 @@
             << "RestrictDownload="
             << ((flags & UpdateAttemptFlags::kFlagRestrictDownload) ? "yes"
                                                                     : "no");
-  system_state_->update_attempter()->SetUpdateAttemptFlags(flags);
+  SystemState::Get()->update_attempter()->SetUpdateAttemptFlags(flags);
   return true;
 }
 
@@ -98,7 +98,7 @@
             << "interactive=" << (interactive ? "yes " : "no ")
             << "RestrictDownload=" << (restrict_downloads ? "yes " : "no ");
 
-  *out_result = system_state_->update_attempter()->CheckForUpdate(
+  *out_result = SystemState::Get()->update_attempter()->CheckForUpdate(
       in_app_version, in_omaha_url, flags);
   return true;
 }
@@ -106,7 +106,8 @@
 bool UpdateEngineService::AttemptInstall(brillo::ErrorPtr* error,
                                          const string& omaha_url,
                                          const vector<string>& dlc_ids) {
-  if (!system_state_->update_attempter()->CheckForInstall(dlc_ids, omaha_url)) {
+  if (!SystemState::Get()->update_attempter()->CheckForInstall(dlc_ids,
+                                                               omaha_url)) {
     // TODO(xiaochu): support more detailed error messages.
     LogAndSetError(error, FROM_HERE, "Could not schedule install operation.");
     return false;
@@ -117,7 +118,7 @@
 bool UpdateEngineService::AttemptRollback(ErrorPtr* error, bool in_powerwash) {
   LOG(INFO) << "Attempting rollback to non-active partitions.";
 
-  if (!system_state_->update_attempter()->Rollback(in_powerwash)) {
+  if (!SystemState::Get()->update_attempter()->Rollback(in_powerwash)) {
     // TODO(dgarrett): Give a more specific error code/reason.
     LogAndSetError(error, FROM_HERE, "Rollback attempt failed.");
     return false;
@@ -127,14 +128,14 @@
 
 bool UpdateEngineService::CanRollback(ErrorPtr* /* error */,
                                       bool* out_can_rollback) {
-  bool can_rollback = system_state_->update_attempter()->CanRollback();
+  bool can_rollback = SystemState::Get()->update_attempter()->CanRollback();
   LOG(INFO) << "Checking to see if we can rollback . Result: " << can_rollback;
   *out_can_rollback = can_rollback;
   return true;
 }
 
 bool UpdateEngineService::ResetStatus(ErrorPtr* error) {
-  if (!system_state_->update_attempter()->ResetStatus()) {
+  if (!SystemState::Get()->update_attempter()->ResetStatus()) {
     // TODO(dgarrett): Give a more specific error code/reason.
     LogAndSetError(error, FROM_HERE, "ResetStatus failed.");
     return false;
@@ -145,8 +146,8 @@
 bool UpdateEngineService::SetDlcActiveValue(brillo::ErrorPtr* error,
                                             bool is_active,
                                             const string& dlc_id) {
-  if (!system_state_->update_attempter()->SetDlcActiveValue(is_active,
-                                                            dlc_id)) {
+  if (!SystemState::Get()->update_attempter()->SetDlcActiveValue(is_active,
+                                                                 dlc_id)) {
     LogAndSetError(error, FROM_HERE, "SetDlcActiveValue failed.");
     return false;
   }
@@ -155,7 +156,7 @@
 
 bool UpdateEngineService::GetStatus(ErrorPtr* error,
                                     UpdateEngineStatus* out_status) {
-  if (!system_state_->update_attempter()->GetStatus(out_status)) {
+  if (!SystemState::Get()->update_attempter()->GetStatus(out_status)) {
     LogAndSetError(error, FROM_HERE, "GetStatus failed.");
     return false;
   }
@@ -163,7 +164,7 @@
 }
 
 bool UpdateEngineService::RebootIfNeeded(ErrorPtr* error) {
-  if (!system_state_->update_attempter()->RebootIfNeeded()) {
+  if (!SystemState::Get()->update_attempter()->RebootIfNeeded()) {
     // TODO(dgarrett): Give a more specific error code/reason.
     LogAndSetError(error, FROM_HERE, "Reboot not needed, or attempt failed.");
     return false;
@@ -174,15 +175,16 @@
 bool UpdateEngineService::SetChannel(ErrorPtr* error,
                                      const string& in_target_channel,
                                      bool in_is_powerwash_allowed) {
-  const policy::DevicePolicy* device_policy = system_state_->device_policy();
+  const policy::DevicePolicy* device_policy =
+      SystemState::Get()->device_policy();
 
   // The device_policy is loaded in a lazy way before an update check. Load it
   // now from the libbrillo cache if it wasn't already loaded.
   if (!device_policy) {
-    UpdateAttempter* update_attempter = system_state_->update_attempter();
+    UpdateAttempter* update_attempter = SystemState::Get()->update_attempter();
     if (update_attempter) {
       update_attempter->RefreshDevicePolicy();
-      device_policy = system_state_->device_policy();
+      device_policy = SystemState::Get()->device_policy();
     }
   }
 
@@ -198,7 +200,7 @@
 
   LOG(INFO) << "Setting destination channel to: " << in_target_channel;
   string error_message;
-  if (!system_state_->request_params()->SetTargetChannel(
+  if (!SystemState::Get()->request_params()->SetTargetChannel(
           in_target_channel, in_is_powerwash_allowed, &error_message)) {
     LogAndSetError(error, FROM_HERE, error_message);
     return false;
@@ -209,7 +211,7 @@
 bool UpdateEngineService::GetChannel(ErrorPtr* /* error */,
                                      bool in_get_current_channel,
                                      string* out_channel) {
-  OmahaRequestParams* rp = system_state_->request_params();
+  OmahaRequestParams* rp = SystemState::Get()->request_params();
   *out_channel =
       (in_get_current_channel ? rp->current_channel() : rp->target_channel());
   return true;
@@ -217,7 +219,7 @@
 
 bool UpdateEngineService::SetCohortHint(ErrorPtr* error,
                                         const string& in_cohort_hint) {
-  PrefsInterface* prefs = system_state_->prefs();
+  PrefsInterface* prefs = SystemState::Get()->prefs();
 
   // It is ok to override the cohort hint with an invalid value since it is
   // stored in stateful partition. The code reading it should sanitize it
@@ -235,7 +237,7 @@
 
 bool UpdateEngineService::GetCohortHint(ErrorPtr* error,
                                         string* out_cohort_hint) {
-  PrefsInterface* prefs = system_state_->prefs();
+  PrefsInterface* prefs = SystemState::Get()->prefs();
 
   *out_cohort_hint = "";
   if (prefs->Exists(kPrefsOmahaCohortHint) &&
@@ -248,7 +250,7 @@
 
 bool UpdateEngineService::SetP2PUpdatePermission(ErrorPtr* error,
                                                  bool in_enabled) {
-  PrefsInterface* prefs = system_state_->prefs();
+  PrefsInterface* prefs = SystemState::Get()->prefs();
 
   if (!prefs->SetBoolean(kPrefsP2PEnabled, in_enabled)) {
     LogAndSetError(
@@ -263,7 +265,7 @@
 
 bool UpdateEngineService::GetP2PUpdatePermission(ErrorPtr* error,
                                                  bool* out_enabled) {
-  PrefsInterface* prefs = system_state_->prefs();
+  PrefsInterface* prefs = SystemState::Get()->prefs();
 
   bool p2p_pref = false;  // Default if no setting is present.
   if (prefs->Exists(kPrefsP2PEnabled) &&
@@ -279,7 +281,7 @@
 bool UpdateEngineService::SetUpdateOverCellularPermission(ErrorPtr* error,
                                                           bool in_allowed) {
   ConnectionManagerInterface* connection_manager =
-      system_state_->connection_manager();
+      SystemState::Get()->connection_manager();
 
   // Check if this setting is allowed by the device policy.
   if (connection_manager->IsAllowedConnectionTypesForUpdateSet()) {
@@ -293,7 +295,7 @@
   // If the policy wasn't loaded yet, then it is still OK to change the local
   // setting because the policy will be checked again during the update check.
 
-  PrefsInterface* prefs = system_state_->prefs();
+  PrefsInterface* prefs = SystemState::Get()->prefs();
 
   if (!prefs ||
       !prefs->SetBoolean(kPrefsUpdateOverCellularPermission, in_allowed)) {
@@ -311,7 +313,7 @@
     const std::string& target_version,
     int64_t target_size) {
   ConnectionManagerInterface* connection_manager =
-      system_state_->connection_manager();
+      SystemState::Get()->connection_manager();
 
   // Check if this setting is allowed by the device policy.
   if (connection_manager->IsAllowedConnectionTypesForUpdateSet()) {
@@ -325,7 +327,7 @@
   // If the policy wasn't loaded yet, then it is still OK to change the local
   // setting because the policy will be checked again during the update check.
 
-  PrefsInterface* prefs = system_state_->prefs();
+  PrefsInterface* prefs = SystemState::Get()->prefs();
 
   if (!prefs ||
       !prefs->SetString(kPrefsUpdateOverCellularTargetVersion,
@@ -341,14 +343,14 @@
 bool UpdateEngineService::GetUpdateOverCellularPermission(ErrorPtr* error,
                                                           bool* out_allowed) {
   ConnectionManagerInterface* connection_manager =
-      system_state_->connection_manager();
+      SystemState::Get()->connection_manager();
 
   if (connection_manager->IsAllowedConnectionTypesForUpdateSet()) {
     // We have device policy, so ignore the user preferences.
     *out_allowed = connection_manager->IsUpdateAllowedOver(
         ConnectionType::kCellular, ConnectionTethering::kUnknown);
   } else {
-    PrefsInterface* prefs = system_state_->prefs();
+    PrefsInterface* prefs = SystemState::Get()->prefs();
 
     if (!prefs || !prefs->Exists(kPrefsUpdateOverCellularPermission)) {
       // Update is not allowed as user preference is not set or not available.
@@ -372,26 +374,26 @@
 bool UpdateEngineService::GetDurationSinceUpdate(ErrorPtr* error,
                                                  int64_t* out_usec_wallclock) {
   base::Time time;
-  if (!system_state_->update_attempter()->GetBootTimeAtUpdate(&time)) {
+  if (!SystemState::Get()->update_attempter()->GetBootTimeAtUpdate(&time)) {
     LogAndSetError(error, FROM_HERE, "No pending update.");
     return false;
   }
 
-  ClockInterface* clock = system_state_->clock();
+  ClockInterface* clock = SystemState::Get()->clock();
   *out_usec_wallclock = (clock->GetBootTime() - time).InMicroseconds();
   return true;
 }
 
 bool UpdateEngineService::GetPrevVersion(ErrorPtr* /* error */,
                                          string* out_prev_version) {
-  *out_prev_version = system_state_->update_attempter()->GetPrevVersion();
+  *out_prev_version = SystemState::Get()->update_attempter()->GetPrevVersion();
   return true;
 }
 
 bool UpdateEngineService::GetRollbackPartition(
     ErrorPtr* /* error */, string* out_rollback_partition_name) {
   BootControlInterface::Slot rollback_slot =
-      system_state_->update_attempter()->GetRollbackSlot();
+      SystemState::Get()->update_attempter()->GetRollbackSlot();
 
   if (rollback_slot == BootControlInterface::kInvalidSlot) {
     out_rollback_partition_name->clear();
@@ -399,7 +401,7 @@
   }
 
   string name;
-  if (!system_state_->boot_control()->GetPartitionDevice(
+  if (!SystemState::Get()->boot_control()->GetPartitionDevice(
           "KERNEL", rollback_slot, &name)) {
     LOG(ERROR) << "Invalid rollback device";
     return false;
@@ -413,7 +415,7 @@
 bool UpdateEngineService::GetLastAttemptError(ErrorPtr* /* error */,
                                               int32_t* out_last_attempt_error) {
   ErrorCode error_code =
-      system_state_->update_attempter()->GetAttemptErrorCode();
+      SystemState::Get()->update_attempter()->GetAttemptErrorCode();
   *out_last_attempt_error = static_cast<int>(error_code);
   return true;
 }