Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 1 | // Copyright (c) 2014 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Gilad Arnold | 48415f1 | 2014-06-27 07:10:58 -0700 | [diff] [blame] | 5 | #ifndef UPDATE_ENGINE_UPDATE_MANAGER_POLICY_H_ |
| 6 | #define UPDATE_ENGINE_UPDATE_MANAGER_POLICY_H_ |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 7 | |
Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 8 | #include <string> |
Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 9 | #include <tuple> |
Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 10 | #include <vector> |
Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 11 | |
Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 12 | #include "update_engine/error_code.h" |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 13 | #include "update_engine/update_manager/evaluation_context.h" |
| 14 | #include "update_engine/update_manager/state.h" |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 15 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 16 | namespace chromeos_update_manager { |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 17 | |
| 18 | // The three different results of a policy request. |
Alex Deymo | e636c3c | 2014-03-11 19:02:08 -0700 | [diff] [blame] | 19 | enum class EvalStatus { |
| 20 | kFailed, |
| 21 | kSucceeded, |
| 22 | kAskMeAgainLater, |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 23 | }; |
| 24 | |
Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 25 | std::string ToString(EvalStatus status); |
| 26 | |
| 27 | // Parameters of an update check. These parameters are determined by the |
| 28 | // UpdateCheckAllowed policy. |
| 29 | struct UpdateCheckParams { |
| 30 | bool updates_enabled; // Whether the auto-updates are enabled on this build. |
Gilad Arnold | 42f253b | 2014-06-25 12:39:17 -0700 | [diff] [blame] | 31 | |
| 32 | // Attributes pertaining to the case where update checks are allowed. |
| 33 | // |
Gilad Arnold | d4b3032 | 2014-07-21 15:35:27 -0700 | [diff] [blame] | 34 | // A target version prefix, if imposed by policy; otherwise, an empty string. |
| 35 | std::string target_version_prefix; |
Gilad Arnold | 42f253b | 2014-06-25 12:39:17 -0700 | [diff] [blame] | 36 | // A target channel, if so imposed by policy; otherwise, an empty string. |
| 37 | std::string target_channel; |
Gilad Arnold | 44dc3bf | 2014-07-18 23:39:38 -0700 | [diff] [blame] | 38 | |
| 39 | // Whether the allowed update is interactive (user-initiated) or periodic. |
| 40 | bool is_interactive; |
Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 41 | }; |
| 42 | |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 43 | // Input arguments to UpdateCanStart. |
| 44 | // |
Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 45 | // A snapshot of the state of the current update process. This includes |
| 46 | // everything that a policy might need and that occurred since the first time |
| 47 | // the current payload was first seen and attempted (consecutively). |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 48 | struct UpdateState { |
Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 49 | // Information pertaining to the current update payload and/or check. |
Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 50 | // |
Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 51 | // Whether the current update check is an interactive one. The caller should |
| 52 | // feed the value returned by the preceding call to UpdateCheckAllowed(). |
| 53 | bool is_interactive; |
| 54 | // Whether it is a delta payload. |
| 55 | bool is_delta_payload; |
| 56 | // Wallclock time when payload was first (consecutively) offered by Omaha. |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 57 | base::Time first_seen; |
Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 58 | // Number of consecutive update checks returning the current update. |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 59 | int num_checks; |
Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 60 | // Number of update payload failures and the wallclock time when it was last |
| 61 | // updated by the updater. These should both be nullified whenever a new |
| 62 | // update is seen; they are updated at the policy's descretion (via |
| 63 | // UpdateDownloadParams.do_increment_failures) once all of the usable download |
| 64 | // URLs for the payload have been used without success. They should be |
| 65 | // persisted across reboots. |
| 66 | int num_failures; |
| 67 | base::Time failures_last_updated; |
Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 68 | |
Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 69 | // Information pertaining to downloading and applying of the current update. |
Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 70 | // |
| 71 | // An array of download URLs provided by Omaha. |
| 72 | std::vector<std::string> download_urls; |
Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 73 | // Max number of errors allowed per download URL. |
| 74 | int download_errors_max; |
| 75 | // The index of the URL to download from, as determined in the previous call |
| 76 | // to the policy. For a newly seen payload, this should be -1. |
| 77 | int last_download_url_idx; |
| 78 | // The number of successive download errors pertaining to this last URL, as |
| 79 | // determined in the previous call to the policy. For a newly seen payload, |
| 80 | // this should be zero. |
| 81 | int last_download_url_num_errors; |
| 82 | // An array of errors that occurred while trying to download this update since |
| 83 | // the previous call to this policy has returned, or since this payload was |
| 84 | // first seen, or since the updater process has started (whichever is later). |
| 85 | // Includes the URL index attempted, the error code, and the wallclock-based |
| 86 | // timestamp when it occurred. |
| 87 | std::vector<std::tuple<int, chromeos_update_engine::ErrorCode, base::Time>> |
| 88 | download_errors; |
| 89 | |
| 90 | // Information pertaining to update backoff mechanism. |
| 91 | // |
| 92 | // The currently known (persisted) wallclock-based backoff expiration time; |
| 93 | // zero if none. |
| 94 | base::Time backoff_expiry; |
| 95 | // Whether backoff is disabled by Omaha. |
| 96 | bool is_backoff_disabled; |
Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 97 | |
| 98 | // Information pertaining to update scattering. |
| 99 | // |
Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 100 | // The currently knwon (persisted) scattering wallclock-based wait period and |
| 101 | // update check threshold; zero if none. |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 102 | base::TimeDelta scatter_wait_period; |
Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 103 | int scatter_check_threshold; |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 104 | // Maximum wait period allowed for this update, as determined by Omaha. |
| 105 | base::TimeDelta scatter_wait_period_max; |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 106 | // Minimum/maximum check threshold values. |
| 107 | // TODO(garnold) These appear to not be related to the current update and so |
Gilad Arnold | ddd3fe3 | 2014-05-22 12:57:09 -0700 | [diff] [blame] | 108 | // should probably be obtained as variables via UpdaterProvider. |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 109 | int scatter_check_threshold_min; |
| 110 | int scatter_check_threshold_max; |
| 111 | }; |
| 112 | |
| 113 | // Results regarding the downloading and applying of an update, as determined by |
| 114 | // UpdateCanStart. |
| 115 | // |
| 116 | // An enumerator for the reasons of not allowing an update to start. |
| 117 | enum class UpdateCannotStartReason { |
| 118 | kUndefined, |
| 119 | kCheckDue, |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 120 | kScattering, |
Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 121 | kBackoff, |
Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 122 | kCannotDownload, |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 123 | }; |
| 124 | |
Gilad Arnold | 42f253b | 2014-06-25 12:39:17 -0700 | [diff] [blame] | 125 | struct UpdateDownloadParams { |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 126 | // Whether the update attempt is allowed to proceed. |
| 127 | bool update_can_start; |
Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 128 | // If update cannot proceed, a reason code for why it cannot do so. |
| 129 | UpdateCannotStartReason cannot_start_reason; |
Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 130 | |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 131 | // Attributes pertaining to the case where update is allowed. The update |
| 132 | // engine uses them to choose the means for downloading and applying an |
| 133 | // update. |
Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 134 | // |
| 135 | // The index of the download URL to use, or -1 if no suitable URL was found; |
| 136 | // in the latter case, there may still be other means for download (like P2P). |
| 137 | // This value needs to be persisted and handed back to the policy on the next |
| 138 | // time it is called. |
Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 139 | int download_url_idx; |
Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 140 | // The number of download errors associated with this download URL. This value |
| 141 | // needs to be persisted and handed back to the policy on the next time it is |
| 142 | // called. |
| 143 | int download_url_num_errors; |
| 144 | // Whether P2P downloads are allowed. |
| 145 | bool p2p_allowed; |
Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 146 | |
Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 147 | // Other values that need to be persisted and handed to the policy as need on |
| 148 | // the next call. |
| 149 | // |
| 150 | // Whether an update failure has been identified by the policy. The client |
| 151 | // should increment and persist its update failure count, and record the time |
| 152 | // when this was done; it needs to hand these values back to the policy |
| 153 | // (UpdateState.{num_failures,failures_last_updated}) on the next time it is |
| 154 | // called. |
| 155 | bool do_increment_failures; |
| 156 | // The current backof expiry. |
| 157 | base::Time backoff_expiry; |
| 158 | // The scattering wait period and check threshold. |
| 159 | base::TimeDelta scatter_wait_period; |
| 160 | int scatter_check_threshold; |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 161 | }; |
Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 162 | |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 163 | // The Policy class is an interface to the ensemble of policy requests that the |
| 164 | // client can make. A derived class includes the policy implementations of |
| 165 | // these. |
| 166 | // |
| 167 | // When compile-time selection of the policy is required due to missing or extra |
| 168 | // parts in a given platform, a different Policy subclass can be used. |
| 169 | class Policy { |
| 170 | public: |
| 171 | virtual ~Policy() {} |
| 172 | |
Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 173 | // Returns the name of a public policy request. |
| 174 | // IMPORTANT: Be sure to add a conditional for each new public policy that is |
| 175 | // being added to this class in the future. |
| 176 | template<typename R, typename... Args> |
| 177 | std::string PolicyRequestName( |
| 178 | EvalStatus (Policy::*policy_method)(EvaluationContext*, State*, |
| 179 | std::string*, R*, |
| 180 | Args...) const) const { |
| 181 | std::string class_name = PolicyName() + "::"; |
| 182 | |
| 183 | if (reinterpret_cast<typeof(&Policy::UpdateCheckAllowed)>( |
| 184 | policy_method) == &Policy::UpdateCheckAllowed) |
| 185 | return class_name + "UpdateCheckAllowed"; |
| 186 | if (reinterpret_cast<typeof(&Policy::UpdateCanStart)>( |
| 187 | policy_method) == &Policy::UpdateCanStart) |
| 188 | return class_name + "UpdateCanStart"; |
Gilad Arnold | 684219d | 2014-07-07 14:54:57 -0700 | [diff] [blame] | 189 | if (reinterpret_cast<typeof(&Policy::UpdateDownloadAllowed)>( |
| 190 | policy_method) == &Policy::UpdateDownloadAllowed) |
| 191 | return class_name + "UpdateDownloadAllowed"; |
Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 192 | |
| 193 | NOTREACHED(); |
| 194 | return class_name + "(unknown)"; |
| 195 | } |
| 196 | |
| 197 | |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 198 | // List of policy requests. A policy request takes an EvaluationContext as the |
Alex Deymo | 2de23f5 | 2014-02-26 14:30:13 -0800 | [diff] [blame] | 199 | // first argument, a State instance, a returned error message, a returned |
| 200 | // value and optionally followed by one or more arbitrary constant arguments. |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 201 | // |
Alex Deymo | e636c3c | 2014-03-11 19:02:08 -0700 | [diff] [blame] | 202 | // When the implementation fails, the method returns EvalStatus::kFailed and |
| 203 | // sets the |error| string. |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 204 | |
| 205 | // UpdateCheckAllowed returns whether it is allowed to request an update check |
| 206 | // to Omaha. |
Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 207 | virtual EvalStatus UpdateCheckAllowed( |
| 208 | EvaluationContext* ec, State* state, std::string* error, |
| 209 | UpdateCheckParams* result) const = 0; |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 210 | |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 211 | // Returns EvalStatus::kSucceeded if either an update can start being |
| 212 | // processed, or the attempt needs to be aborted. In cases where the update |
| 213 | // needs to wait for some condition to be satisfied, but none of the values |
| 214 | // that need to be persisted has changed, returns |
Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 215 | // EvalStatus::kAskMeAgainLater. Arguments include an |update_state| that |
Alex Vakulenko | 072359c | 2014-07-18 11:41:07 -0700 | [diff] [blame] | 216 | // encapsulates data pertaining to the current ongoing update process. |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 217 | virtual EvalStatus UpdateCanStart( |
| 218 | EvaluationContext* ec, |
| 219 | State* state, |
| 220 | std::string* error, |
Gilad Arnold | 42f253b | 2014-06-25 12:39:17 -0700 | [diff] [blame] | 221 | UpdateDownloadParams* result, |
Gilad Arnold | d78caf9 | 2014-09-24 09:28:14 -0700 | [diff] [blame^] | 222 | UpdateState update_state) const = 0; |
Gilad Arnold | af2f6ae | 2014-04-28 14:14:52 -0700 | [diff] [blame] | 223 | |
Gilad Arnold | 684219d | 2014-07-07 14:54:57 -0700 | [diff] [blame] | 224 | // Checks whether downloading of an update is allowed; currently, this checks |
| 225 | // whether the network connection type is suitable for updating over. May |
| 226 | // consult the shill provider as well as the device policy (if available). |
Gilad Arnold | 0adbc94 | 2014-05-12 10:35:43 -0700 | [diff] [blame] | 227 | // Returns |EvalStatus::kSucceeded|, setting |result| according to whether or |
Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 228 | // not the current connection can be used; on error, returns |
Gilad Arnold | 0adbc94 | 2014-05-12 10:35:43 -0700 | [diff] [blame] | 229 | // |EvalStatus::kFailed| and sets |error| accordingly. |
Gilad Arnold | 684219d | 2014-07-07 14:54:57 -0700 | [diff] [blame] | 230 | virtual EvalStatus UpdateDownloadAllowed( |
Gilad Arnold | 0adbc94 | 2014-05-12 10:35:43 -0700 | [diff] [blame] | 231 | EvaluationContext* ec, |
| 232 | State* state, |
| 233 | std::string* error, |
| 234 | bool* result) const = 0; |
| 235 | |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 236 | protected: |
| 237 | Policy() {} |
| 238 | |
Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 239 | // Returns the name of the actual policy class. |
| 240 | virtual std::string PolicyName() const = 0; |
| 241 | |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 242 | private: |
| 243 | DISALLOW_COPY_AND_ASSIGN(Policy); |
| 244 | }; |
| 245 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 246 | } // namespace chromeos_update_manager |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 247 | |
Gilad Arnold | 48415f1 | 2014-06-27 07:10:58 -0700 | [diff] [blame] | 248 | #endif // UPDATE_ENGINE_UPDATE_MANAGER_POLICY_H_ |