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_REAL_SYSTEM_STATE_H_
18 #define UPDATE_ENGINE_REAL_SYSTEM_STATE_H_
19 
20 #include "update_engine/system_state.h"
21 
22 #include <memory>
23 #include <set>
24 
25 #include <metrics/metrics_library.h>
26 #include <policy/device_policy.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/hardware_interface.h"
32 #include "update_engine/common/prefs.h"
33 #include "update_engine/connection_manager_interface.h"
34 #include "update_engine/daemon_state_interface.h"
35 #include "update_engine/p2p_manager.h"
36 #include "update_engine/payload_state.h"
37 #include "update_engine/power_manager_interface.h"
38 #include "update_engine/update_attempter.h"
39 #include "update_engine/update_manager/update_manager.h"
40 #include "update_engine/weave_service_interface.h"
41 
42 namespace chromeos_update_engine {
43 
44 // A real implementation of the SystemStateInterface which is
45 // used by the actual product code.
46 class RealSystemState : public SystemState, public DaemonStateInterface {
47  public:
48   // Constructs all system objects that do not require separate initialization;
49   // see Initialize() below for the remaining ones.
50   RealSystemState() = default;
51   ~RealSystemState() override;
52 
53   // Initializes and sets systems objects that require an initialization
54   // separately from construction. Returns |true| on success.
55   bool Initialize();
56 
57   // DaemonStateInterface overrides.
58   // Start the periodic update attempts. Must be called at the beginning of the
59   // program to start the periodic update check process.
60   bool StartUpdater() override;
61 
62   void AddObserver(ServiceObserverInterface* observer) override;
63   void RemoveObserver(ServiceObserverInterface* observer) override;
service_observers()64   const std::set<ServiceObserverInterface*>& service_observers() override {
65     CHECK(update_attempter_.get());
66     return update_attempter_->service_observers();
67   }
68 
69   // SystemState overrides.
set_device_policy(const policy::DevicePolicy * device_policy)70   inline void set_device_policy(
71       const policy::DevicePolicy* device_policy) override {
72     device_policy_ = device_policy;
73   }
74 
device_policy()75   inline const policy::DevicePolicy* device_policy() override {
76     return device_policy_;
77   }
78 
boot_control()79   inline BootControlInterface* boot_control() override {
80     return boot_control_.get();
81   }
82 
clock()83   inline ClockInterface* clock() override { return &clock_; }
84 
connection_manager()85   inline ConnectionManagerInterface* connection_manager() override {
86     return connection_manager_.get();
87   }
88 
hardware()89   inline HardwareInterface* hardware() override { return hardware_.get(); }
90 
metrics_lib()91   inline MetricsLibraryInterface* metrics_lib() override {
92     return &metrics_lib_;
93   }
94 
prefs()95   inline PrefsInterface* prefs() override { return prefs_.get(); }
96 
powerwash_safe_prefs()97   inline PrefsInterface* powerwash_safe_prefs() override {
98     return powerwash_safe_prefs_.get();
99   }
100 
payload_state()101   inline PayloadStateInterface* payload_state() override {
102     return &payload_state_;
103   }
104 
update_attempter()105   inline UpdateAttempter* update_attempter() override {
106     return update_attempter_.get();
107   }
108 
weave_service()109   inline WeaveServiceInterface* weave_service() override {
110     return weave_service_.get();
111   }
112 
request_params()113   inline OmahaRequestParams* request_params() override {
114     return &request_params_;
115   }
116 
p2p_manager()117   inline P2PManager* p2p_manager() override { return p2p_manager_.get(); }
118 
update_manager()119   inline chromeos_update_manager::UpdateManager* update_manager() override {
120     return update_manager_.get();
121   }
122 
power_manager()123   inline PowerManagerInterface* power_manager() override {
124     return power_manager_.get();
125   }
126 
system_rebooted()127   inline bool system_rebooted() override { return system_rebooted_; }
128 
129  private:
130 #if USE_LIBCROS
131   // LibCros proxy using the DBus connection.
132   LibCrosProxy libcros_proxy_;
133 #endif  // USE_LIBCROS
134 
135   // Interface for the power manager.
136   std::unique_ptr<PowerManagerInterface> power_manager_;
137 
138   // Interface for the clock.
139   std::unique_ptr<BootControlInterface> boot_control_;
140 
141   // Interface for the clock.
142   Clock clock_;
143 
144   // The latest device policy object from the policy provider.
145   const policy::DevicePolicy* device_policy_{nullptr};
146 
147   // The connection manager object that makes download decisions depending on
148   // the current type of connection.
149   std::unique_ptr<ConnectionManagerInterface> connection_manager_;
150 
151   // Interface for the hardware functions.
152   std::unique_ptr<HardwareInterface> hardware_;
153 
154   // The Metrics Library interface for reporting UMA stats.
155   MetricsLibrary metrics_lib_;
156 
157   // Interface for persisted store.
158   std::unique_ptr<PrefsInterface> prefs_;
159 
160   // Interface for persisted store that persists across powerwashes.
161   std::unique_ptr<PrefsInterface> powerwash_safe_prefs_;
162 
163   // All state pertaining to payload state such as response, URL, backoff
164   // states.
165   PayloadState payload_state_;
166 
167   // OpenSSLWrapper and CertificateChecker used for checking SSL certificates.
168   OpenSSLWrapper openssl_wrapper_;
169   std::unique_ptr<CertificateChecker> certificate_checker_;
170 
171   // Pointer to the update attempter object.
172   std::unique_ptr<UpdateAttempter> update_attempter_;
173 
174   // Common parameters for all Omaha requests.
175   OmahaRequestParams request_params_{this};
176 
177   std::unique_ptr<P2PManager> p2p_manager_;
178 
179   std::unique_ptr<WeaveServiceInterface> weave_service_;
180 
181   std::unique_ptr<chromeos_update_manager::UpdateManager> update_manager_;
182 
183   policy::PolicyProvider policy_provider_;
184 
185   // If true, this is the first instance of the update engine since the system
186   // rebooted. Important for tracking whether you are running instance of the
187   // update engine on first boot or due to a crash/restart.
188   bool system_rebooted_{false};
189 };
190 
191 }  // namespace chromeos_update_engine
192 
193 #endif  // UPDATE_ENGINE_REAL_SYSTEM_STATE_H_
194