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/common/system_state.h b/common/system_state.h
index 7a67046..dc40d36 100644
--- a/common/system_state.h
+++ b/common/system_state.h
@@ -17,6 +17,10 @@
#ifndef UPDATE_ENGINE_COMMON_SYSTEM_STATE_H_
#define UPDATE_ENGINE_COMMON_SYSTEM_STATE_H_
+#include <memory>
+
+#include <base/logging.h>
+
namespace chromeos_update_manager {
class UpdateManager;
@@ -51,13 +55,14 @@
// the current state of the system, high-level objects whose lifetime is same
// as main, system interfaces, etc.
// Carved out separately so it can be mocked for unit tests.
-// Currently it has only one method, but we should start migrating other
-// methods to use this as and when needed to unit test them.
-// TODO(jaysri): Consider renaming this to something like GlobalContext.
class SystemState {
public:
- // Destructs this object.
- virtual ~SystemState() {}
+ virtual ~SystemState() = default;
+
+ static SystemState* Get() {
+ CHECK(g_instance_);
+ return g_instance_.get();
+ }
// Sets or gets the latest device policy.
virtual void set_device_policy(const policy::DevicePolicy* device_policy) = 0;
@@ -113,6 +118,9 @@
// Returns a pointer to the DlcServiceInterface singleton.
virtual DlcServiceInterface* dlcservice() = 0;
+
+ protected:
+ static std::unique_ptr<SystemState> g_instance_;
};
} // namespace chromeos_update_engine