1 //
2 // Copyright (C) 2013 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 #ifndef UPDATE_ENGINE_CROS_REAL_SYSTEM_STATE_H_
18 #define UPDATE_ENGINE_CROS_REAL_SYSTEM_STATE_H_
19 
20 #include "update_engine/common/system_state.h"
21 
22 #include <memory>
23 #include <set>
24 
25 #include <policy/device_policy.h>
26 #include <kiosk-app/dbus-proxies.h>
27 
28 #include "update_engine/certificate_checker.h"
29 #include "update_engine/common/boot_control_interface.h"
30 #include "update_engine/common/clock.h"
31 #include "update_engine/common/daemon_state_interface.h"
32 #include "update_engine/common/dlcservice_interface.h"
33 #include "update_engine/common/hardware_interface.h"
34 #include "update_engine/common/metrics_reporter_interface.h"
35 #include "update_engine/common/prefs.h"
36 #include "update_engine/cros/connection_manager_interface.h"
37 #include "update_engine/cros/metrics_reporter_omaha.h"
38 #include "update_engine/cros/p2p_manager.h"
39 #include "update_engine/cros/payload_state.h"
40 #include "update_engine/cros/power_manager_interface.h"
41 #include "update_engine/cros/update_attempter.h"
42 #include "update_engine/update_manager/update_manager.h"
43 
44 namespace chromeos_update_engine {
45 
46 // A real implementation of the SystemStateInterface which is
47 // used by the actual product code.
48 class RealSystemState : public SystemState {
49  public:
50   // Constructs all system objects that do not require separate initialization;
51   // see Initialize() below for the remaining ones.
52   RealSystemState() = default;
53   ~RealSystemState() = default;
54 
SetInstance(RealSystemState * system_state)55   static void SetInstance(RealSystemState* system_state) {
56     CHECK(g_pointer_ == nullptr) << "SystemState has been previously set.";
57     g_pointer_ = system_state;
58     LOG_IF(FATAL, !system_state->Initialize())
59         << "Failed to initialize system state.";
60   }
61 
62   // SystemState overrides.
set_device_policy(const policy::DevicePolicy * device_policy)63   void set_device_policy(const policy::DevicePolicy* device_policy) override {
64     device_policy_ = device_policy;
65   }
66 
device_policy()67   const policy::DevicePolicy* device_policy() override {
68     return device_policy_;
69   }
70 
boot_control()71   BootControlInterface* boot_control() override { return boot_control_.get(); }
72 
clock()73   ClockInterface* clock() override { return &clock_; }
74 
connection_manager()75   ConnectionManagerInterface* connection_manager() override {
76     return connection_manager_.get();
77   }
78 
hardware()79   HardwareInterface* hardware() override { return hardware_.get(); }
80 
metrics_reporter()81   MetricsReporterInterface* metrics_reporter() override {
82     return &metrics_reporter_;
83   }
84 
prefs()85   PrefsInterface* prefs() override { return prefs_.get(); }
86 
powerwash_safe_prefs()87   PrefsInterface* powerwash_safe_prefs() override {
88     return powerwash_safe_prefs_.get();
89   }
90 
payload_state()91   PayloadStateInterface* payload_state() override { return &payload_state_; }
92 
update_attempter()93   UpdateAttempter* update_attempter() override {
94     return update_attempter_.get();
95   }
96 
request_params()97   OmahaRequestParams* request_params() override { return &request_params_; }
98 
p2p_manager()99   P2PManager* p2p_manager() override { return p2p_manager_.get(); }
100 
update_manager()101   chromeos_update_manager::UpdateManager* update_manager() override {
102     return update_manager_.get();
103   }
104 
power_manager()105   PowerManagerInterface* power_manager() override {
106     return power_manager_.get();
107   }
108 
system_rebooted()109   bool system_rebooted() override { return system_rebooted_; }
110 
dlcservice()111   DlcServiceInterface* dlcservice() override { return dlcservice_.get(); }
112 
113  private:
114   // Initializes and sets systems objects that require an initialization
115   // separately from construction. Returns |true| on success.
116   bool Initialize();
117 
118   // Real DBus proxies using the DBus connection.
119   std::unique_ptr<org::chromium::KioskAppServiceInterfaceProxy>
120       kiosk_app_proxy_;
121 
122   // Interface for the power manager.
123   std::unique_ptr<PowerManagerInterface> power_manager_;
124 
125   // Interface for dlcservice.
126   std::unique_ptr<DlcServiceInterface> dlcservice_;
127 
128   // Interface for the bootloader control.
129   std::unique_ptr<BootControlInterface> boot_control_;
130 
131   // Interface for the clock.
132   Clock clock_;
133 
134   // The latest device policy object from the policy provider.
135   const policy::DevicePolicy* device_policy_{nullptr};
136 
137   // The connection manager object that makes download decisions depending on
138   // the current type of connection.
139   std::unique_ptr<ConnectionManagerInterface> connection_manager_;
140 
141   // Interface for the hardware functions.
142   std::unique_ptr<HardwareInterface> hardware_;
143 
144   // The Metrics reporter for reporting UMA stats.
145   MetricsReporterOmaha metrics_reporter_;
146 
147   // Interface for persisted store.
148   std::unique_ptr<PrefsInterface> prefs_;
149 
150   // Interface for persisted store that persists across powerwashes.
151   std::unique_ptr<PrefsInterface> powerwash_safe_prefs_;
152 
153   // All state pertaining to payload state such as response, URL, backoff
154   // states.
155   PayloadState payload_state_;
156 
157   // OpenSSLWrapper and CertificateChecker used for checking SSL certificates.
158   OpenSSLWrapper openssl_wrapper_;
159   std::unique_ptr<CertificateChecker> certificate_checker_;
160 
161   // Pointer to the update attempter object.
162   std::unique_ptr<UpdateAttempter> update_attempter_;
163 
164   // Common parameters for all Omaha requests.
165   OmahaRequestParams request_params_;
166 
167   std::unique_ptr<P2PManager> p2p_manager_;
168 
169   std::unique_ptr<chromeos_update_manager::UpdateManager> update_manager_;
170 
171   policy::PolicyProvider policy_provider_;
172 
173   // If true, this is the first instance of the update engine since the system
174   // rebooted. Important for tracking whether you are running instance of the
175   // update engine on first boot or due to a crash/restart.
176   bool system_rebooted_{false};
177 };
178 
179 }  // namespace chromeos_update_engine
180 
181 #endif  // UPDATE_ENGINE_CROS_REAL_SYSTEM_STATE_H_
182