Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2014 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 | // |
Gilad Arnold | ae47a9a | 2014-03-26 12:16:47 -0700 | [diff] [blame] | 16 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 17 | #include "update_engine/update_manager/real_updater_provider.h" |
Gilad Arnold | ae47a9a | 2014-03-26 12:16:47 -0700 | [diff] [blame] | 18 | |
| 19 | #include <inttypes.h> |
| 20 | |
| 21 | #include <string> |
| 22 | |
Gilad Arnold | 44dc3bf | 2014-07-18 23:39:38 -0700 | [diff] [blame] | 23 | #include <base/bind.h> |
Gilad Arnold | ae47a9a | 2014-03-26 12:16:47 -0700 | [diff] [blame] | 24 | #include <base/strings/stringprintf.h> |
Alex Deymo | c7ab616 | 2014-04-25 18:32:50 -0700 | [diff] [blame] | 25 | #include <base/time/time.h> |
Alex Deymo | d6deb1d | 2015-08-28 15:54:37 -0700 | [diff] [blame] | 26 | #include <update_engine/dbus-constants.h> |
Gilad Arnold | ae47a9a | 2014-03-26 12:16:47 -0700 | [diff] [blame] | 27 | |
Aaron Wood | 7f92e2b | 2017-08-28 14:51:21 -0700 | [diff] [blame] | 28 | #include "update_engine/client_library/include/update_engine/update_status.h" |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 29 | #include "update_engine/common/clock_interface.h" |
| 30 | #include "update_engine/common/prefs.h" |
Gilad Arnold | ae47a9a | 2014-03-26 12:16:47 -0700 | [diff] [blame] | 31 | #include "update_engine/omaha_request_params.h" |
Gilad Arnold | ae47a9a | 2014-03-26 12:16:47 -0700 | [diff] [blame] | 32 | #include "update_engine/update_attempter.h" |
Aaron Wood | 7f92e2b | 2017-08-28 14:51:21 -0700 | [diff] [blame] | 33 | #include "update_engine/update_status_utils.h" |
Gilad Arnold | ae47a9a | 2014-03-26 12:16:47 -0700 | [diff] [blame] | 34 | |
| 35 | using base::StringPrintf; |
| 36 | using base::Time; |
| 37 | using base::TimeDelta; |
| 38 | using chromeos_update_engine::OmahaRequestParams; |
| 39 | using chromeos_update_engine::SystemState; |
| 40 | using std::string; |
Aaron Wood | bf5a252 | 2017-10-04 10:58:36 -0700 | [diff] [blame] | 41 | using update_engine::UpdateAttemptFlags; |
Aaron Wood | 7f92e2b | 2017-08-28 14:51:21 -0700 | [diff] [blame] | 42 | using update_engine::UpdateEngineStatus; |
Gilad Arnold | ae47a9a | 2014-03-26 12:16:47 -0700 | [diff] [blame] | 43 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 44 | namespace chromeos_update_manager { |
Gilad Arnold | ae47a9a | 2014-03-26 12:16:47 -0700 | [diff] [blame] | 45 | |
| 46 | // A templated base class for all update related variables. Provides uniform |
| 47 | // construction and a system state handle. |
Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 48 | template <typename T> |
Gilad Arnold | ae47a9a | 2014-03-26 12:16:47 -0700 | [diff] [blame] | 49 | class UpdaterVariableBase : public Variable<T> { |
| 50 | public: |
Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 51 | UpdaterVariableBase(const string& name, |
| 52 | VariableMode mode, |
Gilad Arnold | 44dc3bf | 2014-07-18 23:39:38 -0700 | [diff] [blame] | 53 | SystemState* system_state) |
| 54 | : Variable<T>(name, mode), system_state_(system_state) {} |
Gilad Arnold | ae47a9a | 2014-03-26 12:16:47 -0700 | [diff] [blame] | 55 | |
| 56 | protected: |
| 57 | // The system state used for pulling information from the updater. |
| 58 | inline SystemState* system_state() const { return system_state_; } |
| 59 | |
| 60 | private: |
| 61 | SystemState* const system_state_; |
| 62 | }; |
| 63 | |
| 64 | // Helper class for issuing a GetStatus() to the UpdateAttempter. |
| 65 | class GetStatusHelper { |
| 66 | public: |
| 67 | GetStatusHelper(SystemState* system_state, string* errmsg) { |
Aaron Wood | 7f92e2b | 2017-08-28 14:51:21 -0700 | [diff] [blame] | 68 | is_success_ = |
| 69 | system_state->update_attempter()->GetStatus(&update_engine_status_); |
| 70 | if (!is_success_ && errmsg) { |
Gilad Arnold | ae47a9a | 2014-03-26 12:16:47 -0700 | [diff] [blame] | 71 | *errmsg = "Failed to get a status update from the update engine"; |
Aaron Wood | 7f92e2b | 2017-08-28 14:51:21 -0700 | [diff] [blame] | 72 | } |
Gilad Arnold | ae47a9a | 2014-03-26 12:16:47 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | inline bool is_success() { return is_success_; } |
Aaron Wood | 7f92e2b | 2017-08-28 14:51:21 -0700 | [diff] [blame] | 76 | inline int64_t last_checked_time() { |
Aaron Wood | 795c5b4 | 2017-12-05 16:06:13 -0800 | [diff] [blame] | 77 | return update_engine_status_.last_checked_time; |
Aaron Wood | 7f92e2b | 2017-08-28 14:51:21 -0700 | [diff] [blame] | 78 | } |
| 79 | inline double progress() { return update_engine_status_.progress; } |
| 80 | inline const string update_status() { |
| 81 | return chromeos_update_engine::UpdateStatusToString( |
| 82 | update_engine_status_.status); |
| 83 | } |
| 84 | inline const string& new_version() { |
| 85 | return update_engine_status_.new_version; |
| 86 | } |
| 87 | inline uint64_t payload_size() { |
| 88 | return update_engine_status_.new_size_bytes; |
| 89 | } |
Gilad Arnold | ae47a9a | 2014-03-26 12:16:47 -0700 | [diff] [blame] | 90 | |
| 91 | private: |
| 92 | bool is_success_; |
Aaron Wood | 7f92e2b | 2017-08-28 14:51:21 -0700 | [diff] [blame] | 93 | UpdateEngineStatus update_engine_status_; |
Gilad Arnold | ae47a9a | 2014-03-26 12:16:47 -0700 | [diff] [blame] | 94 | }; |
| 95 | |
| 96 | // A variable reporting the time when a last update check was issued. |
| 97 | class LastCheckedTimeVariable : public UpdaterVariableBase<Time> { |
| 98 | public: |
Gilad Arnold | 44dc3bf | 2014-07-18 23:39:38 -0700 | [diff] [blame] | 99 | LastCheckedTimeVariable(const string& name, SystemState* system_state) |
| 100 | : UpdaterVariableBase<Time>(name, kVariableModePoll, system_state) {} |
Gilad Arnold | ae47a9a | 2014-03-26 12:16:47 -0700 | [diff] [blame] | 101 | |
| 102 | private: |
Alex Vakulenko | 157fe30 | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 103 | const Time* GetValue(TimeDelta /* timeout */, string* errmsg) override { |
Gilad Arnold | ae47a9a | 2014-03-26 12:16:47 -0700 | [diff] [blame] | 104 | GetStatusHelper raw(system_state(), errmsg); |
| 105 | if (!raw.is_success()) |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 106 | return nullptr; |
Gilad Arnold | ae47a9a | 2014-03-26 12:16:47 -0700 | [diff] [blame] | 107 | |
| 108 | return new Time(Time::FromTimeT(raw.last_checked_time())); |
| 109 | } |
| 110 | |
| 111 | DISALLOW_COPY_AND_ASSIGN(LastCheckedTimeVariable); |
| 112 | }; |
| 113 | |
| 114 | // A variable reporting the update (download) progress as a decimal fraction |
| 115 | // between 0.0 and 1.0. |
| 116 | class ProgressVariable : public UpdaterVariableBase<double> { |
| 117 | public: |
Gilad Arnold | 44dc3bf | 2014-07-18 23:39:38 -0700 | [diff] [blame] | 118 | ProgressVariable(const string& name, SystemState* system_state) |
| 119 | : UpdaterVariableBase<double>(name, kVariableModePoll, system_state) {} |
Gilad Arnold | ae47a9a | 2014-03-26 12:16:47 -0700 | [diff] [blame] | 120 | |
| 121 | private: |
Alex Vakulenko | 157fe30 | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 122 | const double* GetValue(TimeDelta /* timeout */, string* errmsg) override { |
Gilad Arnold | ae47a9a | 2014-03-26 12:16:47 -0700 | [diff] [blame] | 123 | GetStatusHelper raw(system_state(), errmsg); |
| 124 | if (!raw.is_success()) |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 125 | return nullptr; |
Gilad Arnold | ae47a9a | 2014-03-26 12:16:47 -0700 | [diff] [blame] | 126 | |
| 127 | if (raw.progress() < 0.0 || raw.progress() > 1.0) { |
| 128 | if (errmsg) { |
Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 129 | *errmsg = |
| 130 | StringPrintf("Invalid progress value received: %f", raw.progress()); |
Gilad Arnold | ae47a9a | 2014-03-26 12:16:47 -0700 | [diff] [blame] | 131 | } |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 132 | return nullptr; |
Gilad Arnold | ae47a9a | 2014-03-26 12:16:47 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | return new double(raw.progress()); |
| 136 | } |
| 137 | |
| 138 | DISALLOW_COPY_AND_ASSIGN(ProgressVariable); |
| 139 | }; |
| 140 | |
| 141 | // A variable reporting the stage in which the update process is. |
| 142 | class StageVariable : public UpdaterVariableBase<Stage> { |
| 143 | public: |
Gilad Arnold | 44dc3bf | 2014-07-18 23:39:38 -0700 | [diff] [blame] | 144 | StageVariable(const string& name, SystemState* system_state) |
| 145 | : UpdaterVariableBase<Stage>(name, kVariableModePoll, system_state) {} |
Gilad Arnold | ae47a9a | 2014-03-26 12:16:47 -0700 | [diff] [blame] | 146 | |
| 147 | private: |
| 148 | struct CurrOpStrToStage { |
| 149 | const char* str; |
| 150 | Stage stage; |
| 151 | }; |
| 152 | static const CurrOpStrToStage curr_op_str_to_stage[]; |
| 153 | |
| 154 | // Note: the method is defined outside the class so arraysize can work. |
Alex Vakulenko | 157fe30 | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 155 | const Stage* GetValue(TimeDelta /* timeout */, string* errmsg) override; |
Gilad Arnold | ae47a9a | 2014-03-26 12:16:47 -0700 | [diff] [blame] | 156 | |
| 157 | DISALLOW_COPY_AND_ASSIGN(StageVariable); |
| 158 | }; |
| 159 | |
| 160 | const StageVariable::CurrOpStrToStage StageVariable::curr_op_str_to_stage[] = { |
Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 161 | {update_engine::kUpdateStatusIdle, Stage::kIdle}, |
| 162 | {update_engine::kUpdateStatusCheckingForUpdate, Stage::kCheckingForUpdate}, |
| 163 | {update_engine::kUpdateStatusUpdateAvailable, Stage::kUpdateAvailable}, |
| 164 | {update_engine::kUpdateStatusDownloading, Stage::kDownloading}, |
| 165 | {update_engine::kUpdateStatusVerifying, Stage::kVerifying}, |
| 166 | {update_engine::kUpdateStatusFinalizing, Stage::kFinalizing}, |
| 167 | {update_engine::kUpdateStatusUpdatedNeedReboot, Stage::kUpdatedNeedReboot}, |
| 168 | {update_engine::kUpdateStatusReportingErrorEvent, |
| 169 | Stage::kReportingErrorEvent}, |
| 170 | {update_engine::kUpdateStatusAttemptingRollback, |
| 171 | Stage::kAttemptingRollback}, |
Amin Hassani | 70a90f5 | 2020-09-15 15:30:09 -0700 | [diff] [blame] | 172 | {update_engine::kUpdateStatusCleanupPreviousUpdate, |
| 173 | Stage::kCleanupPreviousUpdate}, |
Gilad Arnold | ae47a9a | 2014-03-26 12:16:47 -0700 | [diff] [blame] | 174 | }; |
| 175 | |
Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 176 | const Stage* StageVariable::GetValue(TimeDelta /* timeout */, string* errmsg) { |
Gilad Arnold | ae47a9a | 2014-03-26 12:16:47 -0700 | [diff] [blame] | 177 | GetStatusHelper raw(system_state(), errmsg); |
| 178 | if (!raw.is_success()) |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 179 | return nullptr; |
Gilad Arnold | ae47a9a | 2014-03-26 12:16:47 -0700 | [diff] [blame] | 180 | |
| 181 | for (auto& key_val : curr_op_str_to_stage) |
| 182 | if (raw.update_status() == key_val.str) |
| 183 | return new Stage(key_val.stage); |
| 184 | |
| 185 | if (errmsg) |
| 186 | *errmsg = string("Unknown update status: ") + raw.update_status(); |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 187 | return nullptr; |
Gilad Arnold | ae47a9a | 2014-03-26 12:16:47 -0700 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | // A variable reporting the version number that an update is updating to. |
| 191 | class NewVersionVariable : public UpdaterVariableBase<string> { |
| 192 | public: |
Gilad Arnold | 44dc3bf | 2014-07-18 23:39:38 -0700 | [diff] [blame] | 193 | NewVersionVariable(const string& name, SystemState* system_state) |
| 194 | : UpdaterVariableBase<string>(name, kVariableModePoll, system_state) {} |
Gilad Arnold | ae47a9a | 2014-03-26 12:16:47 -0700 | [diff] [blame] | 195 | |
| 196 | private: |
Alex Vakulenko | 157fe30 | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 197 | const string* GetValue(TimeDelta /* timeout */, string* errmsg) override { |
Gilad Arnold | ae47a9a | 2014-03-26 12:16:47 -0700 | [diff] [blame] | 198 | GetStatusHelper raw(system_state(), errmsg); |
| 199 | if (!raw.is_success()) |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 200 | return nullptr; |
Gilad Arnold | ae47a9a | 2014-03-26 12:16:47 -0700 | [diff] [blame] | 201 | |
| 202 | return new string(raw.new_version()); |
| 203 | } |
| 204 | |
| 205 | DISALLOW_COPY_AND_ASSIGN(NewVersionVariable); |
| 206 | }; |
| 207 | |
| 208 | // A variable reporting the size of the update being processed in bytes. |
Aaron Wood | 7f92e2b | 2017-08-28 14:51:21 -0700 | [diff] [blame] | 209 | class PayloadSizeVariable : public UpdaterVariableBase<uint64_t> { |
Gilad Arnold | ae47a9a | 2014-03-26 12:16:47 -0700 | [diff] [blame] | 210 | public: |
Gilad Arnold | 44dc3bf | 2014-07-18 23:39:38 -0700 | [diff] [blame] | 211 | PayloadSizeVariable(const string& name, SystemState* system_state) |
Aaron Wood | 7f92e2b | 2017-08-28 14:51:21 -0700 | [diff] [blame] | 212 | : UpdaterVariableBase<uint64_t>(name, kVariableModePoll, system_state) {} |
Gilad Arnold | ae47a9a | 2014-03-26 12:16:47 -0700 | [diff] [blame] | 213 | |
| 214 | private: |
Aaron Wood | 7f92e2b | 2017-08-28 14:51:21 -0700 | [diff] [blame] | 215 | const uint64_t* GetValue(TimeDelta /* timeout */, string* errmsg) override { |
Gilad Arnold | ae47a9a | 2014-03-26 12:16:47 -0700 | [diff] [blame] | 216 | GetStatusHelper raw(system_state(), errmsg); |
| 217 | if (!raw.is_success()) |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 218 | return nullptr; |
Gilad Arnold | ae47a9a | 2014-03-26 12:16:47 -0700 | [diff] [blame] | 219 | |
Aaron Wood | 7f92e2b | 2017-08-28 14:51:21 -0700 | [diff] [blame] | 220 | return new uint64_t(raw.payload_size()); |
Gilad Arnold | ae47a9a | 2014-03-26 12:16:47 -0700 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | DISALLOW_COPY_AND_ASSIGN(PayloadSizeVariable); |
| 224 | }; |
| 225 | |
| 226 | // A variable reporting the point in time an update last completed in the |
| 227 | // current boot cycle. |
| 228 | // |
| 229 | // TODO(garnold) In general, both the current boottime and wallclock time |
| 230 | // readings should come from the time provider and be moderated by the |
| 231 | // evaluation context, so that they are uniform throughout the evaluation of a |
| 232 | // policy request. |
| 233 | class UpdateCompletedTimeVariable : public UpdaterVariableBase<Time> { |
| 234 | public: |
Gilad Arnold | 44dc3bf | 2014-07-18 23:39:38 -0700 | [diff] [blame] | 235 | UpdateCompletedTimeVariable(const string& name, SystemState* system_state) |
| 236 | : UpdaterVariableBase<Time>(name, kVariableModePoll, system_state) {} |
Gilad Arnold | ae47a9a | 2014-03-26 12:16:47 -0700 | [diff] [blame] | 237 | |
| 238 | private: |
Alex Vakulenko | 157fe30 | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 239 | const Time* GetValue(TimeDelta /* timeout */, string* errmsg) override { |
Gilad Arnold | ae47a9a | 2014-03-26 12:16:47 -0700 | [diff] [blame] | 240 | Time update_boottime; |
| 241 | if (!system_state()->update_attempter()->GetBootTimeAtUpdate( |
| 242 | &update_boottime)) { |
| 243 | if (errmsg) |
| 244 | *errmsg = "Update completed time could not be read"; |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 245 | return nullptr; |
Gilad Arnold | ae47a9a | 2014-03-26 12:16:47 -0700 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | chromeos_update_engine::ClockInterface* clock = system_state()->clock(); |
| 249 | Time curr_boottime = clock->GetBootTime(); |
| 250 | if (curr_boottime < update_boottime) { |
| 251 | if (errmsg) |
| 252 | *errmsg = "Update completed time more recent than current time"; |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 253 | return nullptr; |
Gilad Arnold | ae47a9a | 2014-03-26 12:16:47 -0700 | [diff] [blame] | 254 | } |
| 255 | TimeDelta duration_since_update = curr_boottime - update_boottime; |
| 256 | return new Time(clock->GetWallclockTime() - duration_since_update); |
| 257 | } |
| 258 | |
| 259 | DISALLOW_COPY_AND_ASSIGN(UpdateCompletedTimeVariable); |
| 260 | }; |
| 261 | |
| 262 | // Variables reporting the current image channel. |
| 263 | class CurrChannelVariable : public UpdaterVariableBase<string> { |
| 264 | public: |
Gilad Arnold | 44dc3bf | 2014-07-18 23:39:38 -0700 | [diff] [blame] | 265 | CurrChannelVariable(const string& name, SystemState* system_state) |
| 266 | : UpdaterVariableBase<string>(name, kVariableModePoll, system_state) {} |
Gilad Arnold | ae47a9a | 2014-03-26 12:16:47 -0700 | [diff] [blame] | 267 | |
| 268 | private: |
Alex Vakulenko | 157fe30 | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 269 | const string* GetValue(TimeDelta /* timeout */, string* errmsg) override { |
Gilad Arnold | ae47a9a | 2014-03-26 12:16:47 -0700 | [diff] [blame] | 270 | OmahaRequestParams* request_params = system_state()->request_params(); |
| 271 | string channel = request_params->current_channel(); |
| 272 | if (channel.empty()) { |
| 273 | if (errmsg) |
| 274 | *errmsg = "No current channel"; |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 275 | return nullptr; |
Gilad Arnold | ae47a9a | 2014-03-26 12:16:47 -0700 | [diff] [blame] | 276 | } |
| 277 | return new string(channel); |
| 278 | } |
| 279 | |
| 280 | DISALLOW_COPY_AND_ASSIGN(CurrChannelVariable); |
| 281 | }; |
| 282 | |
| 283 | // Variables reporting the new image channel. |
| 284 | class NewChannelVariable : public UpdaterVariableBase<string> { |
| 285 | public: |
Gilad Arnold | 44dc3bf | 2014-07-18 23:39:38 -0700 | [diff] [blame] | 286 | NewChannelVariable(const string& name, SystemState* system_state) |
| 287 | : UpdaterVariableBase<string>(name, kVariableModePoll, system_state) {} |
Gilad Arnold | ae47a9a | 2014-03-26 12:16:47 -0700 | [diff] [blame] | 288 | |
| 289 | private: |
Alex Vakulenko | 157fe30 | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 290 | const string* GetValue(TimeDelta /* timeout */, string* errmsg) override { |
Gilad Arnold | ae47a9a | 2014-03-26 12:16:47 -0700 | [diff] [blame] | 291 | OmahaRequestParams* request_params = system_state()->request_params(); |
| 292 | string channel = request_params->target_channel(); |
| 293 | if (channel.empty()) { |
| 294 | if (errmsg) |
| 295 | *errmsg = "No new channel"; |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 296 | return nullptr; |
Gilad Arnold | ae47a9a | 2014-03-26 12:16:47 -0700 | [diff] [blame] | 297 | } |
| 298 | return new string(channel); |
| 299 | } |
| 300 | |
| 301 | DISALLOW_COPY_AND_ASSIGN(NewChannelVariable); |
| 302 | }; |
| 303 | |
| 304 | // A variable class for reading Boolean prefs values. |
Alex Deymo | d6f6007 | 2015-10-12 12:22:27 -0700 | [diff] [blame] | 305 | class BooleanPrefVariable |
| 306 | : public AsyncCopyVariable<bool>, |
| 307 | public chromeos_update_engine::PrefsInterface::ObserverInterface { |
Gilad Arnold | ae47a9a | 2014-03-26 12:16:47 -0700 | [diff] [blame] | 308 | public: |
Alex Deymo | d6f6007 | 2015-10-12 12:22:27 -0700 | [diff] [blame] | 309 | BooleanPrefVariable(const string& name, |
| 310 | chromeos_update_engine::PrefsInterface* prefs, |
| 311 | const char* key, |
| 312 | bool default_val) |
| 313 | : AsyncCopyVariable<bool>(name), |
| 314 | prefs_(prefs), |
| 315 | key_(key), |
| 316 | default_val_(default_val) { |
| 317 | prefs->AddObserver(key, this); |
| 318 | OnPrefSet(key); |
| 319 | } |
Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 320 | ~BooleanPrefVariable() { prefs_->RemoveObserver(key_, this); } |
Gilad Arnold | ae47a9a | 2014-03-26 12:16:47 -0700 | [diff] [blame] | 321 | |
| 322 | private: |
Alex Deymo | d6f6007 | 2015-10-12 12:22:27 -0700 | [diff] [blame] | 323 | // Reads the actual value from the Prefs instance and updates the Variable |
| 324 | // value. |
| 325 | void OnPrefSet(const string& key) override { |
Gilad Arnold | ae47a9a | 2014-03-26 12:16:47 -0700 | [diff] [blame] | 326 | bool result = default_val_; |
Alex Deymo | d6f6007 | 2015-10-12 12:22:27 -0700 | [diff] [blame] | 327 | if (prefs_ && prefs_->Exists(key_) && !prefs_->GetBoolean(key_, &result)) |
| 328 | result = default_val_; |
| 329 | // AsyncCopyVariable will take care of values that didn't change. |
| 330 | SetValue(result); |
Gilad Arnold | ae47a9a | 2014-03-26 12:16:47 -0700 | [diff] [blame] | 331 | } |
| 332 | |
Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 333 | void OnPrefDeleted(const string& key) override { SetValue(default_val_); } |
Alex Deymo | d6f6007 | 2015-10-12 12:22:27 -0700 | [diff] [blame] | 334 | |
| 335 | chromeos_update_engine::PrefsInterface* prefs_; |
| 336 | |
Gilad Arnold | ae47a9a | 2014-03-26 12:16:47 -0700 | [diff] [blame] | 337 | // The Boolean preference key and default value. |
| 338 | const char* const key_; |
| 339 | const bool default_val_; |
| 340 | |
| 341 | DISALLOW_COPY_AND_ASSIGN(BooleanPrefVariable); |
| 342 | }; |
| 343 | |
Gilad Arnold | a6dab94 | 2014-04-25 11:46:03 -0700 | [diff] [blame] | 344 | // A variable returning the number of consecutive failed update checks. |
Gilad Arnold | cf175a0 | 2014-07-10 16:48:47 -0700 | [diff] [blame] | 345 | class ConsecutiveFailedUpdateChecksVariable |
| 346 | : public UpdaterVariableBase<unsigned int> { |
Gilad Arnold | a6dab94 | 2014-04-25 11:46:03 -0700 | [diff] [blame] | 347 | public: |
Gilad Arnold | 44dc3bf | 2014-07-18 23:39:38 -0700 | [diff] [blame] | 348 | ConsecutiveFailedUpdateChecksVariable(const string& name, |
| 349 | SystemState* system_state) |
Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 350 | : UpdaterVariableBase<unsigned int>( |
| 351 | name, kVariableModePoll, system_state) {} |
Gilad Arnold | a6dab94 | 2014-04-25 11:46:03 -0700 | [diff] [blame] | 352 | |
| 353 | private: |
Alex Vakulenko | 157fe30 | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 354 | const unsigned int* GetValue(TimeDelta /* timeout */, |
| 355 | string* /* errmsg */) override { |
Gilad Arnold | a6dab94 | 2014-04-25 11:46:03 -0700 | [diff] [blame] | 356 | return new unsigned int( |
| 357 | system_state()->update_attempter()->consecutive_failed_update_checks()); |
| 358 | } |
| 359 | |
| 360 | DISALLOW_COPY_AND_ASSIGN(ConsecutiveFailedUpdateChecksVariable); |
| 361 | }; |
Gilad Arnold | ae47a9a | 2014-03-26 12:16:47 -0700 | [diff] [blame] | 362 | |
Gilad Arnold | a0258a5 | 2014-07-10 16:21:19 -0700 | [diff] [blame] | 363 | // A variable returning the server-dictated poll interval. |
| 364 | class ServerDictatedPollIntervalVariable |
| 365 | : public UpdaterVariableBase<unsigned int> { |
| 366 | public: |
Gilad Arnold | 44dc3bf | 2014-07-18 23:39:38 -0700 | [diff] [blame] | 367 | ServerDictatedPollIntervalVariable(const string& name, |
| 368 | SystemState* system_state) |
Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 369 | : UpdaterVariableBase<unsigned int>( |
| 370 | name, kVariableModePoll, system_state) {} |
Gilad Arnold | a0258a5 | 2014-07-10 16:21:19 -0700 | [diff] [blame] | 371 | |
| 372 | private: |
Alex Vakulenko | 157fe30 | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 373 | const unsigned int* GetValue(TimeDelta /* timeout */, |
| 374 | string* /* errmsg */) override { |
Gilad Arnold | a0258a5 | 2014-07-10 16:21:19 -0700 | [diff] [blame] | 375 | return new unsigned int( |
| 376 | system_state()->update_attempter()->server_dictated_poll_interval()); |
| 377 | } |
| 378 | |
| 379 | DISALLOW_COPY_AND_ASSIGN(ServerDictatedPollIntervalVariable); |
| 380 | }; |
| 381 | |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 382 | // An async variable that tracks changes to forced update requests. |
| 383 | class ForcedUpdateRequestedVariable |
| 384 | : public UpdaterVariableBase<UpdateRequestStatus> { |
Gilad Arnold | 44dc3bf | 2014-07-18 23:39:38 -0700 | [diff] [blame] | 385 | public: |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 386 | ForcedUpdateRequestedVariable(const string& name, SystemState* system_state) |
| 387 | : UpdaterVariableBase<UpdateRequestStatus>::UpdaterVariableBase( |
Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 388 | name, kVariableModeAsync, system_state) { |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 389 | system_state->update_attempter()->set_forced_update_pending_callback( |
| 390 | new base::Callback<void(bool, bool)>( // NOLINT(readability/function) |
| 391 | base::Bind(&ForcedUpdateRequestedVariable::Reset, |
Gilad Arnold | 44dc3bf | 2014-07-18 23:39:38 -0700 | [diff] [blame] | 392 | base::Unretained(this)))); |
| 393 | } |
| 394 | |
| 395 | private: |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 396 | const UpdateRequestStatus* GetValue(TimeDelta /* timeout */, |
| 397 | string* /* errmsg */) override { |
| 398 | return new UpdateRequestStatus(update_request_status_); |
Gilad Arnold | 44dc3bf | 2014-07-18 23:39:38 -0700 | [diff] [blame] | 399 | } |
| 400 | |
Amin Hassani | ed37d68 | 2018-04-06 13:22:00 -0700 | [diff] [blame] | 401 | void Reset(bool forced_update_requested, bool interactive) { |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 402 | UpdateRequestStatus new_value = UpdateRequestStatus::kNone; |
| 403 | if (forced_update_requested) |
Amin Hassani | ed37d68 | 2018-04-06 13:22:00 -0700 | [diff] [blame] | 404 | new_value = (interactive ? UpdateRequestStatus::kInteractive |
| 405 | : UpdateRequestStatus::kPeriodic); |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 406 | if (update_request_status_ != new_value) { |
| 407 | update_request_status_ = new_value; |
Gilad Arnold | 44dc3bf | 2014-07-18 23:39:38 -0700 | [diff] [blame] | 408 | NotifyValueChanged(); |
| 409 | } |
| 410 | } |
| 411 | |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 412 | UpdateRequestStatus update_request_status_ = UpdateRequestStatus::kNone; |
Gilad Arnold | 44dc3bf | 2014-07-18 23:39:38 -0700 | [diff] [blame] | 413 | |
Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 414 | DISALLOW_COPY_AND_ASSIGN(ForcedUpdateRequestedVariable); |
Gilad Arnold | 44dc3bf | 2014-07-18 23:39:38 -0700 | [diff] [blame] | 415 | }; |
| 416 | |
Aaron Wood | bf5a252 | 2017-10-04 10:58:36 -0700 | [diff] [blame] | 417 | // A variable returning the current update restrictions that are in effect. |
| 418 | class UpdateRestrictionsVariable |
| 419 | : public UpdaterVariableBase<UpdateRestrictions> { |
| 420 | public: |
| 421 | UpdateRestrictionsVariable(const string& name, SystemState* system_state) |
| 422 | : UpdaterVariableBase<UpdateRestrictions>( |
| 423 | name, kVariableModePoll, system_state) {} |
| 424 | |
| 425 | private: |
| 426 | const UpdateRestrictions* GetValue(TimeDelta /* timeout */, |
| 427 | string* /* errmsg */) override { |
| 428 | UpdateAttemptFlags attempt_flags = |
| 429 | system_state()->update_attempter()->GetCurrentUpdateAttemptFlags(); |
| 430 | UpdateRestrictions restriction_flags = UpdateRestrictions::kNone; |
| 431 | // Don't blindly copy the whole value, test and set bits that should |
| 432 | // transfer from one set of flags to the other. |
| 433 | if (attempt_flags & UpdateAttemptFlags::kFlagRestrictDownload) { |
| 434 | restriction_flags = static_cast<UpdateRestrictions>( |
| 435 | restriction_flags | UpdateRestrictions::kRestrictDownloading); |
| 436 | } |
| 437 | |
| 438 | return new UpdateRestrictions(restriction_flags); |
| 439 | } |
| 440 | |
| 441 | DISALLOW_COPY_AND_ASSIGN(UpdateRestrictionsVariable); |
| 442 | }; |
| 443 | |
Gilad Arnold | ae47a9a | 2014-03-26 12:16:47 -0700 | [diff] [blame] | 444 | // RealUpdaterProvider methods. |
| 445 | |
| 446 | RealUpdaterProvider::RealUpdaterProvider(SystemState* system_state) |
Aaron Wood | bf5a252 | 2017-10-04 10:58:36 -0700 | [diff] [blame] | 447 | : system_state_(system_state), |
| 448 | var_updater_started_time_("updater_started_time", |
| 449 | system_state->clock()->GetWallclockTime()), |
| 450 | var_last_checked_time_( |
| 451 | new LastCheckedTimeVariable("last_checked_time", system_state_)), |
| 452 | var_update_completed_time_(new UpdateCompletedTimeVariable( |
| 453 | "update_completed_time", system_state_)), |
| 454 | var_progress_(new ProgressVariable("progress", system_state_)), |
| 455 | var_stage_(new StageVariable("stage", system_state_)), |
| 456 | var_new_version_(new NewVersionVariable("new_version", system_state_)), |
| 457 | var_payload_size_(new PayloadSizeVariable("payload_size", system_state_)), |
| 458 | var_curr_channel_(new CurrChannelVariable("curr_channel", system_state_)), |
| 459 | var_new_channel_(new NewChannelVariable("new_channel", system_state_)), |
| 460 | var_p2p_enabled_( |
| 461 | new BooleanPrefVariable("p2p_enabled", |
| 462 | system_state_->prefs(), |
| 463 | chromeos_update_engine::kPrefsP2PEnabled, |
| 464 | false)), |
| 465 | var_cellular_enabled_(new BooleanPrefVariable( |
| 466 | "cellular_enabled", |
| 467 | system_state_->prefs(), |
| 468 | chromeos_update_engine::kPrefsUpdateOverCellularPermission, |
| 469 | false)), |
| 470 | var_consecutive_failed_update_checks_( |
| 471 | new ConsecutiveFailedUpdateChecksVariable( |
| 472 | "consecutive_failed_update_checks", system_state_)), |
| 473 | var_server_dictated_poll_interval_(new ServerDictatedPollIntervalVariable( |
| 474 | "server_dictated_poll_interval", system_state_)), |
| 475 | var_forced_update_requested_(new ForcedUpdateRequestedVariable( |
| 476 | "forced_update_requested", system_state_)), |
| 477 | var_update_restrictions_(new UpdateRestrictionsVariable( |
| 478 | "update_restrictions", system_state_)) {} |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 479 | } // namespace chromeos_update_manager |