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