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/real_system_state.h b/cros/real_system_state.h
index 1e45dc1..348c31b 100644
--- a/cros/real_system_state.h
+++ b/cros/real_system_state.h
@@ -47,14 +47,14 @@
 // used by the actual product code.
 class RealSystemState : public SystemState {
  public:
-  // Constructs all system objects that do not require separate initialization;
-  // see Initialize() below for the remaining ones.
-  RealSystemState() = default;
   ~RealSystemState() = default;
 
-  // Initializes and sets systems objects that require an initialization
-  // separately from construction. Returns |true| on success.
-  bool Initialize();
+  static void CreateInstance() {
+    CHECK(!g_instance_) << "SystemState has been previously created.";
+    RealSystemState* rss = new RealSystemState();
+    g_instance_.reset(rss);
+    LOG_IF(FATAL, !rss->Initialize()) << "Failed to initialize system state.";
+  }
 
   // SystemState overrides.
   void set_device_policy(const policy::DevicePolicy* device_policy) override {
@@ -108,6 +108,14 @@
   DlcServiceInterface* dlcservice() override { return dlcservice_.get(); }
 
  private:
+  // Constructs all system objects that do not require separate initialization;
+  // see Initialize() below for the remaining ones.
+  RealSystemState() = default;
+
+  // Initializes and sets systems objects that require an initialization
+  // separately from construction. Returns |true| on success.
+  bool Initialize();
+
   // Real DBus proxies using the DBus connection.
   std::unique_ptr<org::chromium::KioskAppServiceInterfaceProxy>
       kiosk_app_proxy_;
@@ -155,7 +163,7 @@
   std::unique_ptr<UpdateAttempter> update_attempter_;
 
   // Common parameters for all Omaha requests.
-  OmahaRequestParams request_params_{this};
+  OmahaRequestParams request_params_;
 
   std::unique_ptr<P2PManager> p2p_manager_;