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