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/update_manager/real_system_provider.cc b/update_manager/real_system_provider.cc
index 8d30f7f..34397f3 100644
--- a/update_manager/real_system_provider.cc
+++ b/update_manager/real_system_provider.cc
@@ -24,11 +24,13 @@
 
 #include "update_engine/common/boot_control_interface.h"
 #include "update_engine/common/hardware_interface.h"
+#include "update_engine/common/system_state.h"
 #include "update_engine/common/utils.h"
 #include "update_engine/cros/omaha_request_params.h"
 #include "update_engine/update_manager/generic_variables.h"
 #include "update_engine/update_manager/variable.h"
 
+using chromeos_update_engine::SystemState;
 using std::string;
 
 namespace chromeos_update_manager {
@@ -98,19 +100,20 @@
 
 bool RealSystemProvider::Init() {
   var_is_normal_boot_mode_.reset(new ConstCopyVariable<bool>(
-      "is_normal_boot_mode", system_state_->hardware()->IsNormalBootMode()));
+      "is_normal_boot_mode",
+      SystemState::Get()->hardware()->IsNormalBootMode()));
 
   var_is_official_build_.reset(new ConstCopyVariable<bool>(
-      "is_official_build", system_state_->hardware()->IsOfficialBuild()));
+      "is_official_build", SystemState::Get()->hardware()->IsOfficialBuild()));
 
   var_is_oobe_complete_.reset(new CallCopyVariable<bool>(
       "is_oobe_complete",
       base::Bind(&chromeos_update_engine::HardwareInterface::IsOOBEComplete,
-                 base::Unretained(system_state_->hardware()),
+                 base::Unretained(SystemState::Get()->hardware()),
                  nullptr)));
 
   var_num_slots_.reset(new ConstCopyVariable<unsigned int>(
-      "num_slots", system_state_->boot_control()->GetNumSlots()));
+      "num_slots", SystemState::Get()->boot_control()->GetNumSlots()));
 
   var_kiosk_required_platform_version_.reset(new RetryPollVariable<string>(
       "kiosk_required_platform_version",
@@ -120,7 +123,7 @@
 
   var_chromeos_version_.reset(new ConstCopyVariable<base::Version>(
       "chromeos_version",
-      base::Version(system_state_->request_params()->app_version())));
+      base::Version(SystemState::Get()->request_params()->app_version())));
 
   return true;
 }
diff --git a/update_manager/real_system_provider.h b/update_manager/real_system_provider.h
index 91fee7f..558d3be 100644
--- a/update_manager/real_system_provider.h
+++ b/update_manager/real_system_provider.h
@@ -22,7 +22,6 @@
 
 #include <base/version.h>
 
-#include "update_engine/common/system_state.h"
 #include "update_engine/update_manager/system_provider.h"
 
 namespace org {
@@ -37,10 +36,8 @@
 class RealSystemProvider : public SystemProvider {
  public:
   RealSystemProvider(
-      chromeos_update_engine::SystemState* system_state,
       org::chromium::KioskAppServiceInterfaceProxyInterface* kiosk_app_proxy)
-      : system_state_(system_state), kiosk_app_proxy_(kiosk_app_proxy) {
-  }
+      : kiosk_app_proxy_(kiosk_app_proxy) {}
 
   // Initializes the provider and returns whether it succeeded.
   bool Init();
@@ -80,8 +77,6 @@
   std::unique_ptr<Variable<std::string>> var_kiosk_required_platform_version_;
   std::unique_ptr<Variable<base::Version>> var_chromeos_version_;
 
-  chromeos_update_engine::SystemState* const system_state_;
-
   org::chromium::KioskAppServiceInterfaceProxyInterface* const kiosk_app_proxy_;
 
   DISALLOW_COPY_AND_ASSIGN(RealSystemProvider);
diff --git a/update_manager/real_system_provider_unittest.cc b/update_manager/real_system_provider_unittest.cc
index 3c77ac7..9abcad0 100644
--- a/update_manager/real_system_provider_unittest.cc
+++ b/update_manager/real_system_provider_unittest.cc
@@ -29,6 +29,7 @@
 #include "update_engine/cros/fake_system_state.h"
 #include "update_engine/update_manager/umtest_utils.h"
 
+using chromeos_update_engine::FakeSystemState;
 using org::chromium::KioskAppServiceInterfaceProxyMock;
 using std::unique_ptr;
 using testing::_;
@@ -45,17 +46,16 @@
 class UmRealSystemProviderTest : public ::testing::Test {
  protected:
   void SetUp() override {
+    FakeSystemState::CreateInstance();
     kiosk_app_proxy_mock_.reset(new KioskAppServiceInterfaceProxyMock());
     ON_CALL(*kiosk_app_proxy_mock_, GetRequiredPlatformVersion(_, _, _))
         .WillByDefault(
             DoAll(SetArgPointee<0>(kRequiredPlatformVersion), Return(true)));
 
-    provider_.reset(new RealSystemProvider(&fake_system_state_,
-                                           kiosk_app_proxy_mock_.get()));
+    provider_.reset(new RealSystemProvider(kiosk_app_proxy_mock_.get()));
     EXPECT_TRUE(provider_->Init());
   }
 
-  chromeos_update_engine::FakeSystemState fake_system_state_;
   unique_ptr<RealSystemProvider> provider_;
 
   unique_ptr<KioskAppServiceInterfaceProxyMock> kiosk_app_proxy_mock_;
@@ -70,17 +70,17 @@
 }
 
 TEST_F(UmRealSystemProviderTest, IsOOBECompleteTrue) {
-  fake_system_state_.fake_hardware()->SetIsOOBEComplete(base::Time());
+  FakeSystemState::Get()->fake_hardware()->SetIsOOBEComplete(base::Time());
   UmTestUtils::ExpectVariableHasValue(true, provider_->var_is_oobe_complete());
 }
 
 TEST_F(UmRealSystemProviderTest, IsOOBECompleteFalse) {
-  fake_system_state_.fake_hardware()->UnsetIsOOBEComplete();
+  FakeSystemState::Get()->fake_hardware()->UnsetIsOOBEComplete();
   UmTestUtils::ExpectVariableHasValue(false, provider_->var_is_oobe_complete());
 }
 
 TEST_F(UmRealSystemProviderTest, VersionFromRequestParams) {
-  fake_system_state_.request_params()->set_app_version("1.2.3");
+  FakeSystemState::Get()->request_params()->set_app_version("1.2.3");
   // Call |Init| again to pick up the version.
   EXPECT_TRUE(provider_->Init());
 
diff --git a/update_manager/real_updater_provider.cc b/update_manager/real_updater_provider.cc
index e975b80..6da30c9 100644
--- a/update_manager/real_updater_provider.cc
+++ b/update_manager/real_updater_provider.cc
@@ -29,6 +29,7 @@
 #include "update_engine/client_library/include/update_engine/update_status.h"
 #include "update_engine/common/clock_interface.h"
 #include "update_engine/common/prefs.h"
+#include "update_engine/common/system_state.h"
 #include "update_engine/cros/omaha_request_params.h"
 #include "update_engine/cros/update_attempter.h"
 #include "update_engine/update_status_utils.h"
@@ -49,25 +50,16 @@
 template <typename T>
 class UpdaterVariableBase : public Variable<T> {
  public:
-  UpdaterVariableBase(const string& name,
-                      VariableMode mode,
-                      SystemState* system_state)
-      : Variable<T>(name, mode), system_state_(system_state) {}
-
- protected:
-  // The system state used for pulling information from the updater.
-  inline SystemState* system_state() const { return system_state_; }
-
- private:
-  SystemState* const system_state_;
+  UpdaterVariableBase(const string& name, VariableMode mode)
+      : Variable<T>(name, mode) {}
 };
 
 // Helper class for issuing a GetStatus() to the UpdateAttempter.
 class GetStatusHelper {
  public:
-  GetStatusHelper(SystemState* system_state, string* errmsg) {
-    is_success_ =
-        system_state->update_attempter()->GetStatus(&update_engine_status_);
+  explicit GetStatusHelper(string* errmsg) {
+    is_success_ = SystemState::Get()->update_attempter()->GetStatus(
+        &update_engine_status_);
     if (!is_success_ && errmsg) {
       *errmsg = "Failed to get a status update from the update engine";
     }
@@ -97,12 +89,12 @@
 // A variable reporting the time when a last update check was issued.
 class LastCheckedTimeVariable : public UpdaterVariableBase<Time> {
  public:
-  LastCheckedTimeVariable(const string& name, SystemState* system_state)
-      : UpdaterVariableBase<Time>(name, kVariableModePoll, system_state) {}
+  explicit LastCheckedTimeVariable(const string& name)
+      : UpdaterVariableBase<Time>(name, kVariableModePoll) {}
 
  private:
   const Time* GetValue(TimeDelta /* timeout */, string* errmsg) override {
-    GetStatusHelper raw(system_state(), errmsg);
+    GetStatusHelper raw(errmsg);
     if (!raw.is_success())
       return nullptr;
 
@@ -116,12 +108,12 @@
 // between 0.0 and 1.0.
 class ProgressVariable : public UpdaterVariableBase<double> {
  public:
-  ProgressVariable(const string& name, SystemState* system_state)
-      : UpdaterVariableBase<double>(name, kVariableModePoll, system_state) {}
+  explicit ProgressVariable(const string& name)
+      : UpdaterVariableBase<double>(name, kVariableModePoll) {}
 
  private:
   const double* GetValue(TimeDelta /* timeout */, string* errmsg) override {
-    GetStatusHelper raw(system_state(), errmsg);
+    GetStatusHelper raw(errmsg);
     if (!raw.is_success())
       return nullptr;
 
@@ -142,8 +134,8 @@
 // A variable reporting the stage in which the update process is.
 class StageVariable : public UpdaterVariableBase<Stage> {
  public:
-  StageVariable(const string& name, SystemState* system_state)
-      : UpdaterVariableBase<Stage>(name, kVariableModePoll, system_state) {}
+  explicit StageVariable(const string& name)
+      : UpdaterVariableBase<Stage>(name, kVariableModePoll) {}
 
  private:
   struct CurrOpStrToStage {
@@ -175,7 +167,7 @@
 };
 
 const Stage* StageVariable::GetValue(TimeDelta /* timeout */, string* errmsg) {
-  GetStatusHelper raw(system_state(), errmsg);
+  GetStatusHelper raw(errmsg);
   if (!raw.is_success())
     return nullptr;
 
@@ -191,12 +183,12 @@
 // A variable reporting the version number that an update is updating to.
 class NewVersionVariable : public UpdaterVariableBase<string> {
  public:
-  NewVersionVariable(const string& name, SystemState* system_state)
-      : UpdaterVariableBase<string>(name, kVariableModePoll, system_state) {}
+  explicit NewVersionVariable(const string& name)
+      : UpdaterVariableBase<string>(name, kVariableModePoll) {}
 
  private:
   const string* GetValue(TimeDelta /* timeout */, string* errmsg) override {
-    GetStatusHelper raw(system_state(), errmsg);
+    GetStatusHelper raw(errmsg);
     if (!raw.is_success())
       return nullptr;
 
@@ -209,12 +201,12 @@
 // A variable reporting the size of the update being processed in bytes.
 class PayloadSizeVariable : public UpdaterVariableBase<uint64_t> {
  public:
-  PayloadSizeVariable(const string& name, SystemState* system_state)
-      : UpdaterVariableBase<uint64_t>(name, kVariableModePoll, system_state) {}
+  explicit PayloadSizeVariable(const string& name)
+      : UpdaterVariableBase<uint64_t>(name, kVariableModePoll) {}
 
  private:
   const uint64_t* GetValue(TimeDelta /* timeout */, string* errmsg) override {
-    GetStatusHelper raw(system_state(), errmsg);
+    GetStatusHelper raw(errmsg);
     if (!raw.is_success())
       return nullptr;
 
@@ -233,20 +225,20 @@
 // policy request.
 class UpdateCompletedTimeVariable : public UpdaterVariableBase<Time> {
  public:
-  UpdateCompletedTimeVariable(const string& name, SystemState* system_state)
-      : UpdaterVariableBase<Time>(name, kVariableModePoll, system_state) {}
+  explicit UpdateCompletedTimeVariable(const string& name)
+      : UpdaterVariableBase<Time>(name, kVariableModePoll) {}
 
  private:
   const Time* GetValue(TimeDelta /* timeout */, string* errmsg) override {
     Time update_boottime;
-    if (!system_state()->update_attempter()->GetBootTimeAtUpdate(
+    if (!SystemState::Get()->update_attempter()->GetBootTimeAtUpdate(
             &update_boottime)) {
       if (errmsg)
         *errmsg = "Update completed time could not be read";
       return nullptr;
     }
 
-    chromeos_update_engine::ClockInterface* clock = system_state()->clock();
+    chromeos_update_engine::ClockInterface* clock = SystemState::Get()->clock();
     Time curr_boottime = clock->GetBootTime();
     if (curr_boottime < update_boottime) {
       if (errmsg)
@@ -263,12 +255,12 @@
 // Variables reporting the current image channel.
 class CurrChannelVariable : public UpdaterVariableBase<string> {
  public:
-  CurrChannelVariable(const string& name, SystemState* system_state)
-      : UpdaterVariableBase<string>(name, kVariableModePoll, system_state) {}
+  explicit CurrChannelVariable(const string& name)
+      : UpdaterVariableBase<string>(name, kVariableModePoll) {}
 
  private:
   const string* GetValue(TimeDelta /* timeout */, string* errmsg) override {
-    OmahaRequestParams* request_params = system_state()->request_params();
+    OmahaRequestParams* request_params = SystemState::Get()->request_params();
     string channel = request_params->current_channel();
     if (channel.empty()) {
       if (errmsg)
@@ -284,12 +276,12 @@
 // Variables reporting the new image channel.
 class NewChannelVariable : public UpdaterVariableBase<string> {
  public:
-  NewChannelVariable(const string& name, SystemState* system_state)
-      : UpdaterVariableBase<string>(name, kVariableModePoll, system_state) {}
+  explicit NewChannelVariable(const string& name)
+      : UpdaterVariableBase<string>(name, kVariableModePoll) {}
 
  private:
   const string* GetValue(TimeDelta /* timeout */, string* errmsg) override {
-    OmahaRequestParams* request_params = system_state()->request_params();
+    OmahaRequestParams* request_params = SystemState::Get()->request_params();
     string channel = request_params->target_channel();
     if (channel.empty()) {
       if (errmsg)
@@ -346,16 +338,16 @@
 class ConsecutiveFailedUpdateChecksVariable
     : public UpdaterVariableBase<unsigned int> {
  public:
-  ConsecutiveFailedUpdateChecksVariable(const string& name,
-                                        SystemState* system_state)
-      : UpdaterVariableBase<unsigned int>(
-            name, kVariableModePoll, system_state) {}
+  explicit ConsecutiveFailedUpdateChecksVariable(const string& name)
+      : UpdaterVariableBase<unsigned int>(name, kVariableModePoll) {}
 
  private:
   const unsigned int* GetValue(TimeDelta /* timeout */,
                                string* /* errmsg */) override {
-    return new unsigned int(
-        system_state()->update_attempter()->consecutive_failed_update_checks());
+    // NOLINTNEXTLINE(readability/casting)
+    return new unsigned int(SystemState::Get()
+                                ->update_attempter()
+                                ->consecutive_failed_update_checks());
   }
 
   DISALLOW_COPY_AND_ASSIGN(ConsecutiveFailedUpdateChecksVariable);
@@ -365,16 +357,16 @@
 class ServerDictatedPollIntervalVariable
     : public UpdaterVariableBase<unsigned int> {
  public:
-  ServerDictatedPollIntervalVariable(const string& name,
-                                     SystemState* system_state)
-      : UpdaterVariableBase<unsigned int>(
-            name, kVariableModePoll, system_state) {}
+  explicit ServerDictatedPollIntervalVariable(const string& name)
+      : UpdaterVariableBase<unsigned int>(name, kVariableModePoll) {}
 
  private:
   const unsigned int* GetValue(TimeDelta /* timeout */,
                                string* /* errmsg */) override {
-    return new unsigned int(
-        system_state()->update_attempter()->server_dictated_poll_interval());
+    // NOLINTNEXTLINE(readability/casting)
+    return new unsigned int(SystemState::Get()
+                                ->update_attempter()
+                                ->server_dictated_poll_interval());
   }
 
   DISALLOW_COPY_AND_ASSIGN(ServerDictatedPollIntervalVariable);
@@ -384,10 +376,10 @@
 class ForcedUpdateRequestedVariable
     : public UpdaterVariableBase<UpdateRequestStatus> {
  public:
-  ForcedUpdateRequestedVariable(const string& name, SystemState* system_state)
+  explicit ForcedUpdateRequestedVariable(const string& name)
       : UpdaterVariableBase<UpdateRequestStatus>::UpdaterVariableBase(
-            name, kVariableModeAsync, system_state) {
-    system_state->update_attempter()->set_forced_update_pending_callback(
+            name, kVariableModeAsync) {
+    SystemState::Get()->update_attempter()->set_forced_update_pending_callback(
         new base::Callback<void(bool, bool)>(  // NOLINT(readability/function)
             base::Bind(&ForcedUpdateRequestedVariable::Reset,
                        base::Unretained(this))));
@@ -419,15 +411,14 @@
 class UpdateRestrictionsVariable
     : public UpdaterVariableBase<UpdateRestrictions> {
  public:
-  UpdateRestrictionsVariable(const string& name, SystemState* system_state)
-      : UpdaterVariableBase<UpdateRestrictions>(
-            name, kVariableModePoll, system_state) {}
+  explicit UpdateRestrictionsVariable(const string& name)
+      : UpdaterVariableBase<UpdateRestrictions>(name, kVariableModePoll) {}
 
  private:
   const UpdateRestrictions* GetValue(TimeDelta /* timeout */,
                                      string* /* errmsg */) override {
     UpdateAttemptFlags attempt_flags =
-        system_state()->update_attempter()->GetCurrentUpdateAttemptFlags();
+        SystemState::Get()->update_attempter()->GetCurrentUpdateAttemptFlags();
     UpdateRestrictions restriction_flags = UpdateRestrictions::kNone;
     // Don't blindly copy the whole value, test and set bits that should
     // transfer from one set of flags to the other.
@@ -484,40 +475,40 @@
 
 // RealUpdaterProvider methods.
 
-RealUpdaterProvider::RealUpdaterProvider(SystemState* system_state)
-    : system_state_(system_state),
-      var_updater_started_time_("updater_started_time",
-                                system_state->clock()->GetWallclockTime()),
-      var_last_checked_time_(
-          new LastCheckedTimeVariable("last_checked_time", system_state_)),
-      var_update_completed_time_(new UpdateCompletedTimeVariable(
-          "update_completed_time", system_state_)),
-      var_progress_(new ProgressVariable("progress", system_state_)),
-      var_stage_(new StageVariable("stage", system_state_)),
-      var_new_version_(new NewVersionVariable("new_version", system_state_)),
-      var_payload_size_(new PayloadSizeVariable("payload_size", system_state_)),
-      var_curr_channel_(new CurrChannelVariable("curr_channel", system_state_)),
-      var_new_channel_(new NewChannelVariable("new_channel", system_state_)),
+RealUpdaterProvider::RealUpdaterProvider()
+    : var_updater_started_time_(
+          "updater_started_time",
+          SystemState::Get()->clock()->GetWallclockTime()),
+      var_last_checked_time_(new LastCheckedTimeVariable("last_checked_time")),
+      var_update_completed_time_(
+          new UpdateCompletedTimeVariable("update_completed_time")),
+      var_progress_(new ProgressVariable("progress")),
+      var_stage_(new StageVariable("stage")),
+      var_new_version_(new NewVersionVariable("new_version")),
+      var_payload_size_(new PayloadSizeVariable("payload_size")),
+      var_curr_channel_(new CurrChannelVariable("curr_channel")),
+      var_new_channel_(new NewChannelVariable("new_channel")),
       var_p2p_enabled_(
           new BooleanPrefVariable("p2p_enabled",
-                                  system_state_->prefs(),
+                                  SystemState::Get()->prefs(),
                                   chromeos_update_engine::kPrefsP2PEnabled,
                                   false)),
       var_cellular_enabled_(new BooleanPrefVariable(
           "cellular_enabled",
-          system_state_->prefs(),
+          SystemState::Get()->prefs(),
           chromeos_update_engine::kPrefsUpdateOverCellularPermission,
           false)),
       var_consecutive_failed_update_checks_(
           new ConsecutiveFailedUpdateChecksVariable(
-              "consecutive_failed_update_checks", system_state_)),
+              "consecutive_failed_update_checks")),
       var_server_dictated_poll_interval_(new ServerDictatedPollIntervalVariable(
-          "server_dictated_poll_interval", system_state_)),
-      var_forced_update_requested_(new ForcedUpdateRequestedVariable(
-          "forced_update_requested", system_state_)),
+          "server_dictated_poll_interval")),
+      var_forced_update_requested_(
+          new ForcedUpdateRequestedVariable("forced_update_requested")),
       var_update_restrictions_(
-          new UpdateRestrictionsVariable("update_restrictions", system_state_)),
+          new UpdateRestrictionsVariable("update_restrictions")),
       var_test_update_check_interval_timeout_(
           new TestUpdateCheckIntervalTimeoutVariable(
-              "test_update_check_interval_timeout", system_state_->prefs())) {}
+              "test_update_check_interval_timeout",
+              SystemState::Get()->prefs())) {}
 }  // namespace chromeos_update_manager
diff --git a/update_manager/real_updater_provider.h b/update_manager/real_updater_provider.h
index a32e7e9..24298d7 100644
--- a/update_manager/real_updater_provider.h
+++ b/update_manager/real_updater_provider.h
@@ -20,7 +20,6 @@
 #include <memory>
 #include <string>
 
-#include "update_engine/common/system_state.h"
 #include "update_engine/update_manager/generic_variables.h"
 #include "update_engine/update_manager/updater_provider.h"
 
@@ -34,8 +33,7 @@
   // guarantees that parts of the system state can be mocked out at any time
   // during testing. We further assume that, by the time Init() is called, the
   // system state object is fully populated and usable.
-  explicit RealUpdaterProvider(
-      chromeos_update_engine::SystemState* system_state);
+  RealUpdaterProvider();
 
   // Initializes the provider and returns whether it succeeded.
   bool Init() { return true; }
@@ -99,9 +97,6 @@
   }
 
  private:
-  // A pointer to the update engine's system state aggregator.
-  chromeos_update_engine::SystemState* system_state_;
-
   // Variable implementations.
   ConstCopyVariable<base::Time> var_updater_started_time_;
   std::unique_ptr<Variable<base::Time>> var_last_checked_time_;
diff --git a/update_manager/real_updater_provider_unittest.cc b/update_manager/real_updater_provider_unittest.cc
index 0dc56ac..bde074e 100644
--- a/update_manager/real_updater_provider_unittest.cc
+++ b/update_manager/real_updater_provider_unittest.cc
@@ -100,10 +100,10 @@
 class UmRealUpdaterProviderTest : public ::testing::Test {
  protected:
   void SetUp() override {
-    fake_clock_ = fake_sys_state_.fake_clock();
-    fake_sys_state_.set_prefs(&fake_prefs_);
-    provider_.reset(new RealUpdaterProvider(&fake_sys_state_));
-    ASSERT_NE(nullptr, provider_.get());
+    FakeSystemState::CreateInstance();
+    fake_clock_ = FakeSystemState::Get()->fake_clock();
+    FakeSystemState::Get()->set_prefs(&fake_prefs_);
+    provider_.reset(new RealUpdaterProvider());
     // Check that provider initializes correctly.
     ASSERT_TRUE(provider_->Init());
   }
@@ -117,7 +117,7 @@
     const Time kCurrBootTime = (valid ? kUpdateBootTime + kDurationSinceUpdate
                                       : kUpdateBootTime - kDurationSinceUpdate);
     const Time kCurrWallclockTime = FixedTime();
-    EXPECT_CALL(*fake_sys_state_.mock_update_attempter(),
+    EXPECT_CALL(*FakeSystemState::Get()->mock_update_attempter(),
                 GetBootTimeAtUpdate(_))
         .WillOnce(DoAll(SetArgPointee<0>(kUpdateBootTime), Return(true)));
     fake_clock_->SetBootTime(kCurrBootTime);
@@ -125,8 +125,7 @@
     return kCurrWallclockTime - kDurationSinceUpdate;
   }
 
-  FakeSystemState fake_sys_state_;
-  FakeClock* fake_clock_;  // Short for fake_sys_state_.fake_clock()
+  FakeClock* fake_clock_;  // Short for FakeSystemState::Get()->fake_clock()
   FakePrefs fake_prefs_;
   unique_ptr<RealUpdaterProvider> provider_;
 };
@@ -134,14 +133,15 @@
 TEST_F(UmRealUpdaterProviderTest, UpdaterStartedTimeIsWallclockTime) {
   fake_clock_->SetWallclockTime(Time::FromDoubleT(123.456));
   fake_clock_->SetMonotonicTime(Time::FromDoubleT(456.123));
-  // Run SetUp again to re-setup the provider under test to use these values.
-  SetUp();
+  // Re-initialize to re-setup the provider under test to use these values.
+  provider_.reset(new RealUpdaterProvider());
+  ASSERT_TRUE(provider_->Init());
   UmTestUtils::ExpectVariableHasValue(Time::FromDoubleT(123.456),
                                       provider_->var_updater_started_time());
 }
 
 TEST_F(UmRealUpdaterProviderTest, GetLastCheckedTimeOkay) {
-  EXPECT_CALL(*fake_sys_state_.mock_update_attempter(), GetStatus(_))
+  EXPECT_CALL(*FakeSystemState::Get()->mock_update_attempter(), GetStatus(_))
       .WillOnce(DoAll(
           ActionSetUpdateEngineStatusLastCheckedTime(FixedTime().ToTimeT()),
           Return(true)));
@@ -150,49 +150,49 @@
 }
 
 TEST_F(UmRealUpdaterProviderTest, GetLastCheckedTimeFailNoValue) {
-  EXPECT_CALL(*fake_sys_state_.mock_update_attempter(), GetStatus(_))
+  EXPECT_CALL(*FakeSystemState::Get()->mock_update_attempter(), GetStatus(_))
       .WillOnce(Return(false));
   UmTestUtils::ExpectVariableNotSet(provider_->var_last_checked_time());
 }
 
 TEST_F(UmRealUpdaterProviderTest, GetProgressOkayMin) {
-  EXPECT_CALL(*fake_sys_state_.mock_update_attempter(), GetStatus(_))
+  EXPECT_CALL(*FakeSystemState::Get()->mock_update_attempter(), GetStatus(_))
       .WillOnce(DoAll(ActionSetUpdateEngineStatusProgress(0.0), Return(true)));
   UmTestUtils::ExpectVariableHasValue(0.0, provider_->var_progress());
 }
 
 TEST_F(UmRealUpdaterProviderTest, GetProgressOkayMid) {
-  EXPECT_CALL(*fake_sys_state_.mock_update_attempter(), GetStatus(_))
+  EXPECT_CALL(*FakeSystemState::Get()->mock_update_attempter(), GetStatus(_))
       .WillOnce(DoAll(ActionSetUpdateEngineStatusProgress(0.3), Return(true)));
   UmTestUtils::ExpectVariableHasValue(0.3, provider_->var_progress());
 }
 
 TEST_F(UmRealUpdaterProviderTest, GetProgressOkayMax) {
-  EXPECT_CALL(*fake_sys_state_.mock_update_attempter(), GetStatus(_))
+  EXPECT_CALL(*FakeSystemState::Get()->mock_update_attempter(), GetStatus(_))
       .WillOnce(DoAll(ActionSetUpdateEngineStatusProgress(1.0), Return(true)));
   UmTestUtils::ExpectVariableHasValue(1.0, provider_->var_progress());
 }
 
 TEST_F(UmRealUpdaterProviderTest, GetProgressFailNoValue) {
-  EXPECT_CALL(*fake_sys_state_.mock_update_attempter(), GetStatus(_))
+  EXPECT_CALL(*FakeSystemState::Get()->mock_update_attempter(), GetStatus(_))
       .WillOnce(Return(false));
   UmTestUtils::ExpectVariableNotSet(provider_->var_progress());
 }
 
 TEST_F(UmRealUpdaterProviderTest, GetProgressFailTooSmall) {
-  EXPECT_CALL(*fake_sys_state_.mock_update_attempter(), GetStatus(_))
+  EXPECT_CALL(*FakeSystemState::Get()->mock_update_attempter(), GetStatus(_))
       .WillOnce(DoAll(ActionSetUpdateEngineStatusProgress(-2.0), Return(true)));
   UmTestUtils::ExpectVariableNotSet(provider_->var_progress());
 }
 
 TEST_F(UmRealUpdaterProviderTest, GetProgressFailTooBig) {
-  EXPECT_CALL(*fake_sys_state_.mock_update_attempter(), GetStatus(_))
+  EXPECT_CALL(*FakeSystemState::Get()->mock_update_attempter(), GetStatus(_))
       .WillOnce(DoAll(ActionSetUpdateEngineStatusProgress(2.0), Return(true)));
   UmTestUtils::ExpectVariableNotSet(provider_->var_progress());
 }
 
 TEST_F(UmRealUpdaterProviderTest, GetStageOkayIdle) {
-  EXPECT_CALL(*fake_sys_state_.mock_update_attempter(), GetStatus(_))
+  EXPECT_CALL(*FakeSystemState::Get()->mock_update_attempter(), GetStatus(_))
       .WillOnce(DoAll(
           ActionSetUpdateEngineStatusStatus(update_engine::UpdateStatus::IDLE),
           Return(true)));
@@ -200,7 +200,7 @@
 }
 
 TEST_F(UmRealUpdaterProviderTest, GetStageOkayCheckingForUpdate) {
-  EXPECT_CALL(*fake_sys_state_.mock_update_attempter(), GetStatus(_))
+  EXPECT_CALL(*FakeSystemState::Get()->mock_update_attempter(), GetStatus(_))
       .WillOnce(DoAll(ActionSetUpdateEngineStatusStatus(
                           update_engine::UpdateStatus::CHECKING_FOR_UPDATE),
                       Return(true)));
@@ -209,7 +209,7 @@
 }
 
 TEST_F(UmRealUpdaterProviderTest, GetStageOkayUpdateAvailable) {
-  EXPECT_CALL(*fake_sys_state_.mock_update_attempter(), GetStatus(_))
+  EXPECT_CALL(*FakeSystemState::Get()->mock_update_attempter(), GetStatus(_))
       .WillOnce(DoAll(ActionSetUpdateEngineStatusStatus(
                           update_engine::UpdateStatus::UPDATE_AVAILABLE),
                       Return(true)));
@@ -218,7 +218,7 @@
 }
 
 TEST_F(UmRealUpdaterProviderTest, GetStageOkayDownloading) {
-  EXPECT_CALL(*fake_sys_state_.mock_update_attempter(), GetStatus(_))
+  EXPECT_CALL(*FakeSystemState::Get()->mock_update_attempter(), GetStatus(_))
       .WillOnce(DoAll(ActionSetUpdateEngineStatusStatus(
                           update_engine::UpdateStatus::DOWNLOADING),
                       Return(true)));
@@ -227,7 +227,7 @@
 }
 
 TEST_F(UmRealUpdaterProviderTest, GetStageOkayVerifying) {
-  EXPECT_CALL(*fake_sys_state_.mock_update_attempter(), GetStatus(_))
+  EXPECT_CALL(*FakeSystemState::Get()->mock_update_attempter(), GetStatus(_))
       .WillOnce(DoAll(ActionSetUpdateEngineStatusStatus(
                           update_engine::UpdateStatus::VERIFYING),
                       Return(true)));
@@ -236,7 +236,7 @@
 }
 
 TEST_F(UmRealUpdaterProviderTest, GetStageOkayFinalizing) {
-  EXPECT_CALL(*fake_sys_state_.mock_update_attempter(), GetStatus(_))
+  EXPECT_CALL(*FakeSystemState::Get()->mock_update_attempter(), GetStatus(_))
       .WillOnce(DoAll(ActionSetUpdateEngineStatusStatus(
                           update_engine::UpdateStatus::FINALIZING),
                       Return(true)));
@@ -245,7 +245,7 @@
 }
 
 TEST_F(UmRealUpdaterProviderTest, GetStageOkayUpdatedNeedReboot) {
-  EXPECT_CALL(*fake_sys_state_.mock_update_attempter(), GetStatus(_))
+  EXPECT_CALL(*FakeSystemState::Get()->mock_update_attempter(), GetStatus(_))
       .WillOnce(DoAll(ActionSetUpdateEngineStatusStatus(
                           update_engine::UpdateStatus::UPDATED_NEED_REBOOT),
                       Return(true)));
@@ -254,7 +254,7 @@
 }
 
 TEST_F(UmRealUpdaterProviderTest, GetStageOkayReportingErrorEvent) {
-  EXPECT_CALL(*fake_sys_state_.mock_update_attempter(), GetStatus(_))
+  EXPECT_CALL(*FakeSystemState::Get()->mock_update_attempter(), GetStatus(_))
       .WillOnce(DoAll(ActionSetUpdateEngineStatusStatus(
                           update_engine::UpdateStatus::REPORTING_ERROR_EVENT),
                       Return(true)));
@@ -263,7 +263,7 @@
 }
 
 TEST_F(UmRealUpdaterProviderTest, GetStageOkayAttemptingRollback) {
-  EXPECT_CALL(*fake_sys_state_.mock_update_attempter(), GetStatus(_))
+  EXPECT_CALL(*FakeSystemState::Get()->mock_update_attempter(), GetStatus(_))
       .WillOnce(DoAll(ActionSetUpdateEngineStatusStatus(
                           update_engine::UpdateStatus::ATTEMPTING_ROLLBACK),
                       Return(true)));
@@ -272,13 +272,13 @@
 }
 
 TEST_F(UmRealUpdaterProviderTest, GetStageFailNoValue) {
-  EXPECT_CALL(*fake_sys_state_.mock_update_attempter(), GetStatus(_))
+  EXPECT_CALL(*FakeSystemState::Get()->mock_update_attempter(), GetStatus(_))
       .WillOnce(Return(false));
   UmTestUtils::ExpectVariableNotSet(provider_->var_stage());
 }
 
 TEST_F(UmRealUpdaterProviderTest, GetNewVersionOkay) {
-  EXPECT_CALL(*fake_sys_state_.mock_update_attempter(), GetStatus(_))
+  EXPECT_CALL(*FakeSystemState::Get()->mock_update_attempter(), GetStatus(_))
       .WillOnce(
           DoAll(ActionSetUpdateEngineStatusNewVersion("1.2.0"), Return(true)));
   UmTestUtils::ExpectVariableHasValue(string("1.2.0"),
@@ -286,13 +286,13 @@
 }
 
 TEST_F(UmRealUpdaterProviderTest, GetNewVersionFailNoValue) {
-  EXPECT_CALL(*fake_sys_state_.mock_update_attempter(), GetStatus(_))
+  EXPECT_CALL(*FakeSystemState::Get()->mock_update_attempter(), GetStatus(_))
       .WillOnce(Return(false));
   UmTestUtils::ExpectVariableNotSet(provider_->var_new_version());
 }
 
 TEST_F(UmRealUpdaterProviderTest, GetPayloadSizeOkayZero) {
-  EXPECT_CALL(*fake_sys_state_.mock_update_attempter(), GetStatus(_))
+  EXPECT_CALL(*FakeSystemState::Get()->mock_update_attempter(), GetStatus(_))
       .WillOnce(DoAll(
           ActionSetUpdateEngineStatusNewSizeBytes(static_cast<uint64_t>(0)),
           Return(true)));
@@ -301,7 +301,7 @@
 }
 
 TEST_F(UmRealUpdaterProviderTest, GetPayloadSizeOkayArbitrary) {
-  EXPECT_CALL(*fake_sys_state_.mock_update_attempter(), GetStatus(_))
+  EXPECT_CALL(*FakeSystemState::Get()->mock_update_attempter(), GetStatus(_))
       .WillOnce(DoAll(ActionSetUpdateEngineStatusNewSizeBytes(
                           static_cast<uint64_t>(567890)),
                       Return(true)));
@@ -310,7 +310,7 @@
 }
 
 TEST_F(UmRealUpdaterProviderTest, GetPayloadSizeOkayTwoGigabytes) {
-  EXPECT_CALL(*fake_sys_state_.mock_update_attempter(), GetStatus(_))
+  EXPECT_CALL(*FakeSystemState::Get()->mock_update_attempter(), GetStatus(_))
       .WillOnce(DoAll(ActionSetUpdateEngineStatusNewSizeBytes(
                           static_cast<uint64_t>(1) << 31),
                       Return(true)));
@@ -319,44 +319,44 @@
 }
 
 TEST_F(UmRealUpdaterProviderTest, GetPayloadSizeFailNoValue) {
-  EXPECT_CALL(*fake_sys_state_.mock_update_attempter(), GetStatus(_))
+  EXPECT_CALL(*FakeSystemState::Get()->mock_update_attempter(), GetStatus(_))
       .WillOnce(Return(false));
   UmTestUtils::ExpectVariableNotSet(provider_->var_payload_size());
 }
 
 TEST_F(UmRealUpdaterProviderTest, GetCurrChannelOkay) {
   const string kChannelName("foo-channel");
-  OmahaRequestParams request_params(&fake_sys_state_);
+  OmahaRequestParams request_params;
   request_params.Init("", "", {});
   request_params.set_current_channel(kChannelName);
-  fake_sys_state_.set_request_params(&request_params);
+  FakeSystemState::Get()->set_request_params(&request_params);
   UmTestUtils::ExpectVariableHasValue(kChannelName,
                                       provider_->var_curr_channel());
 }
 
 TEST_F(UmRealUpdaterProviderTest, GetCurrChannelFailEmpty) {
-  OmahaRequestParams request_params(&fake_sys_state_);
+  OmahaRequestParams request_params;
   request_params.Init("", "", {});
   request_params.set_current_channel("");
-  fake_sys_state_.set_request_params(&request_params);
+  FakeSystemState::Get()->set_request_params(&request_params);
   UmTestUtils::ExpectVariableNotSet(provider_->var_curr_channel());
 }
 
 TEST_F(UmRealUpdaterProviderTest, GetNewChannelOkay) {
   const string kChannelName("foo-channel");
-  OmahaRequestParams request_params(&fake_sys_state_);
+  OmahaRequestParams request_params;
   request_params.Init("", "", {});
   request_params.set_target_channel(kChannelName);
-  fake_sys_state_.set_request_params(&request_params);
+  FakeSystemState::Get()->set_request_params(&request_params);
   UmTestUtils::ExpectVariableHasValue(kChannelName,
                                       provider_->var_new_channel());
 }
 
 TEST_F(UmRealUpdaterProviderTest, GetNewChannelFailEmpty) {
-  OmahaRequestParams request_params(&fake_sys_state_);
+  OmahaRequestParams request_params;
   request_params.Init("", "", {});
   request_params.set_target_channel("");
-  fake_sys_state_.set_request_params(&request_params);
+  FakeSystemState::Get()->set_request_params(&request_params);
   UmTestUtils::ExpectVariableNotSet(provider_->var_new_channel());
 }
 
@@ -401,7 +401,8 @@
 }
 
 TEST_F(UmRealUpdaterProviderTest, GetUpdateCompletedTimeFailNoValue) {
-  EXPECT_CALL(*fake_sys_state_.mock_update_attempter(), GetBootTimeAtUpdate(_))
+  EXPECT_CALL(*FakeSystemState::Get()->mock_update_attempter(),
+              GetBootTimeAtUpdate(_))
       .WillOnce(Return(false));
   UmTestUtils::ExpectVariableNotSet(provider_->var_update_completed_time());
 }
@@ -413,7 +414,7 @@
 
 TEST_F(UmRealUpdaterProviderTest, GetConsecutiveFailedUpdateChecks) {
   const unsigned int kNumFailedChecks = 3;
-  EXPECT_CALL(*fake_sys_state_.mock_update_attempter(),
+  EXPECT_CALL(*FakeSystemState::Get()->mock_update_attempter(),
               consecutive_failed_update_checks())
       .WillRepeatedly(Return(kNumFailedChecks));
   UmTestUtils::ExpectVariableHasValue(
@@ -422,7 +423,7 @@
 
 TEST_F(UmRealUpdaterProviderTest, GetServerDictatedPollInterval) {
   const unsigned int kPollInterval = 2 * 60 * 60;  // Two hours.
-  EXPECT_CALL(*fake_sys_state_.mock_update_attempter(),
+  EXPECT_CALL(*FakeSystemState::Get()->mock_update_attempter(),
               server_dictated_poll_interval())
       .WillRepeatedly(Return(kPollInterval));
   UmTestUtils::ExpectVariableHasValue(
@@ -430,7 +431,7 @@
 }
 
 TEST_F(UmRealUpdaterProviderTest, GetUpdateRestrictions) {
-  EXPECT_CALL(*fake_sys_state_.mock_update_attempter(),
+  EXPECT_CALL(*FakeSystemState::Get()->mock_update_attempter(),
               GetCurrentUpdateAttemptFlags())
       .WillRepeatedly(Return(UpdateAttemptFlags::kFlagRestrictDownload |
                              UpdateAttemptFlags::kFlagNonInteractive));
@@ -439,7 +440,7 @@
 }
 
 TEST_F(UmRealUpdaterProviderTest, GetUpdateRestrictionsNone) {
-  EXPECT_CALL(*fake_sys_state_.mock_update_attempter(),
+  EXPECT_CALL(*FakeSystemState::Get()->mock_update_attempter(),
               GetCurrentUpdateAttemptFlags())
       .WillRepeatedly(Return(UpdateAttemptFlags::kNone));
   UmTestUtils::ExpectVariableHasValue(UpdateRestrictions::kNone,
diff --git a/update_manager/staging_utils.cc b/update_manager/staging_utils.cc
index e8f07bb..186da73 100644
--- a/update_manager/staging_utils.cc
+++ b/update_manager/staging_utils.cc
@@ -27,7 +27,6 @@
 #include "update_engine/common/constants.h"
 #include "update_engine/common/hardware_interface.h"
 #include "update_engine/common/prefs_interface.h"
-#include "update_engine/common/system_state.h"
 
 using base::TimeDelta;
 using chromeos_update_engine::kPrefsWallClockStagingWaitPeriod;
diff --git a/update_manager/state_factory.cc b/update_manager/state_factory.cc
index 2168974..badbe23 100644
--- a/update_manager/state_factory.cc
+++ b/update_manager/state_factory.cc
@@ -27,6 +27,7 @@
 #if USE_DBUS
 #include "update_engine/cros/dbus_connection.h"
 #endif  // USE_DBUS
+#include "update_engine/common/system_state.h"
 #include "update_engine/cros/shill_proxy.h"
 #include "update_engine/update_manager/fake_shill_provider.h"
 #include "update_engine/update_manager/real_config_provider.h"
@@ -38,17 +39,18 @@
 #include "update_engine/update_manager/real_time_provider.h"
 #include "update_engine/update_manager/real_updater_provider.h"
 
+using chromeos_update_engine::SystemState;
 using std::unique_ptr;
 
 namespace chromeos_update_manager {
 
 State* DefaultStateFactory(
     policy::PolicyProvider* policy_provider,
-    org::chromium::KioskAppServiceInterfaceProxyInterface* kiosk_app_proxy,
-    chromeos_update_engine::SystemState* system_state) {
-  chromeos_update_engine::ClockInterface* const clock = system_state->clock();
+    org::chromium::KioskAppServiceInterfaceProxyInterface* kiosk_app_proxy) {
+  chromeos_update_engine::ClockInterface* const clock =
+      SystemState::Get()->clock();
   unique_ptr<RealConfigProvider> config_provider(
-      new RealConfigProvider(system_state->hardware()));
+      new RealConfigProvider(SystemState::Get()->hardware()));
 #if USE_DBUS
   scoped_refptr<dbus::Bus> bus =
       chromeos_update_engine::DBusConnection::Get()->GetDBus();
@@ -64,11 +66,10 @@
       new RealShillProvider(new chromeos_update_engine::ShillProxy(), clock));
   unique_ptr<RealRandomProvider> random_provider(new RealRandomProvider());
   unique_ptr<RealSystemProvider> system_provider(
-      new RealSystemProvider(system_state, kiosk_app_proxy));
+      new RealSystemProvider(kiosk_app_proxy));
 
   unique_ptr<RealTimeProvider> time_provider(new RealTimeProvider(clock));
-  unique_ptr<RealUpdaterProvider> updater_provider(
-      new RealUpdaterProvider(system_state));
+  unique_ptr<RealUpdaterProvider> updater_provider(new RealUpdaterProvider());
 
   if (!(config_provider->Init() && device_policy_provider->Init() &&
         random_provider->Init() &&
diff --git a/update_manager/state_factory.h b/update_manager/state_factory.h
index ac3bf6b..c53bb9c 100644
--- a/update_manager/state_factory.h
+++ b/update_manager/state_factory.h
@@ -17,7 +17,6 @@
 #ifndef UPDATE_ENGINE_UPDATE_MANAGER_STATE_FACTORY_H_
 #define UPDATE_ENGINE_UPDATE_MANAGER_STATE_FACTORY_H_
 
-#include "update_engine/common/system_state.h"
 #include "update_engine/update_manager/state.h"
 
 namespace org {
@@ -35,8 +34,7 @@
 // to initialize.
 State* DefaultStateFactory(
     policy::PolicyProvider* policy_provider,
-    org::chromium::KioskAppServiceInterfaceProxyInterface* kiosk_app_proxy,
-    chromeos_update_engine::SystemState* system_state);
+    org::chromium::KioskAppServiceInterfaceProxyInterface* kiosk_app_proxy);
 
 }  // namespace chromeos_update_manager