blob: c65184621bc95741009bdecf181344e980db366a [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
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//
Alex Deymoc705cc82014-02-19 11:15:00 -080016
Gilad Arnold48415f12014-06-27 07:10:58 -070017#ifndef UPDATE_ENGINE_UPDATE_MANAGER_POLICY_H_
18#define UPDATE_ENGINE_UPDATE_MANAGER_POLICY_H_
Alex Deymoc705cc82014-02-19 11:15:00 -080019
Alex Deymo0d11c602014-04-23 20:12:20 -070020#include <string>
Gilad Arnolddc4bb262014-07-23 10:45:19 -070021#include <tuple>
Gilad Arnoldb3b05442014-05-30 14:25:05 -070022#include <vector>
Alex Deymo0d11c602014-04-23 20:12:20 -070023
Alex Deymo39910dc2015-11-09 17:04:30 -080024#include "update_engine/common/error_code.h"
Aaron Wood23bd3392017-10-06 14:48:25 -070025#include "update_engine/payload_consumer/install_plan.h"
Alex Deymo63784a52014-05-28 10:46:14 -070026#include "update_engine/update_manager/evaluation_context.h"
Marton Hunyady0e0e3542018-02-21 18:51:39 +010027#include "update_engine/update_manager/rollback_prefs.h"
Alex Deymo63784a52014-05-28 10:46:14 -070028#include "update_engine/update_manager/state.h"
Alex Deymoc705cc82014-02-19 11:15:00 -080029
Alex Deymo63784a52014-05-28 10:46:14 -070030namespace chromeos_update_manager {
Alex Deymoc705cc82014-02-19 11:15:00 -080031
32// The three different results of a policy request.
Alex Deymoe636c3c2014-03-11 19:02:08 -070033enum class EvalStatus {
34 kFailed,
35 kSucceeded,
36 kAskMeAgainLater,
Aaron Wood56d8ab32017-09-22 15:56:18 -070037 kContinue,
Alex Deymoc705cc82014-02-19 11:15:00 -080038};
39
Alex Deymo0d11c602014-04-23 20:12:20 -070040std::string ToString(EvalStatus status);
41
42// Parameters of an update check. These parameters are determined by the
43// UpdateCheckAllowed policy.
44struct UpdateCheckParams {
45 bool updates_enabled; // Whether the auto-updates are enabled on this build.
Gilad Arnold42f253b2014-06-25 12:39:17 -070046
47 // Attributes pertaining to the case where update checks are allowed.
48 //
Gilad Arnoldd4b30322014-07-21 15:35:27 -070049 // A target version prefix, if imposed by policy; otherwise, an empty string.
50 std::string target_version_prefix;
Marton Hunyady0e0e3542018-02-21 18:51:39 +010051 // Specifies what should happen if target_version_prefix specifies an earlier
52 // version than the current version of the OS.
53 RollbackToTargetVersion rollback_to_target_version;
54 // Specifies the number of Chrome milestones rollback should be allowed,
55 // starting from the stable version at any time. Value is -1 if unspecified
56 // (e.g. no device policy is available yet), in this case no version
57 // roll-forward should happen.
58 int rollback_allowed_milestones;
Gilad Arnold42f253b2014-06-25 12:39:17 -070059 // A target channel, if so imposed by policy; otherwise, an empty string.
60 std::string target_channel;
Gilad Arnold44dc3bf2014-07-18 23:39:38 -070061
62 // Whether the allowed update is interactive (user-initiated) or periodic.
63 bool is_interactive;
Alex Deymo0d11c602014-04-23 20:12:20 -070064};
65
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070066// Input arguments to UpdateCanStart.
67//
Gilad Arnolddc4bb262014-07-23 10:45:19 -070068// A snapshot of the state of the current update process. This includes
69// everything that a policy might need and that occurred since the first time
70// the current payload was first seen and attempted (consecutively).
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070071struct UpdateState {
Gilad Arnolddc4bb262014-07-23 10:45:19 -070072 // Information pertaining to the current update payload and/or check.
Gilad Arnoldb3b05442014-05-30 14:25:05 -070073 //
Gilad Arnolddc4bb262014-07-23 10:45:19 -070074 // Whether the current update check is an interactive one. The caller should
75 // feed the value returned by the preceding call to UpdateCheckAllowed().
76 bool is_interactive;
77 // Whether it is a delta payload.
78 bool is_delta_payload;
79 // Wallclock time when payload was first (consecutively) offered by Omaha.
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070080 base::Time first_seen;
Gilad Arnolddc4bb262014-07-23 10:45:19 -070081 // Number of consecutive update checks returning the current update.
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070082 int num_checks;
Gilad Arnolddc4bb262014-07-23 10:45:19 -070083 // Number of update payload failures and the wallclock time when it was last
84 // updated by the updater. These should both be nullified whenever a new
85 // update is seen; they are updated at the policy's descretion (via
86 // UpdateDownloadParams.do_increment_failures) once all of the usable download
87 // URLs for the payload have been used without success. They should be
88 // persisted across reboots.
89 int num_failures;
90 base::Time failures_last_updated;
Gilad Arnoldb3b05442014-05-30 14:25:05 -070091
Gilad Arnolddc4bb262014-07-23 10:45:19 -070092 // Information pertaining to downloading and applying of the current update.
Gilad Arnoldb3b05442014-05-30 14:25:05 -070093 //
94 // An array of download URLs provided by Omaha.
95 std::vector<std::string> download_urls;
Gilad Arnolddc4bb262014-07-23 10:45:19 -070096 // Max number of errors allowed per download URL.
97 int download_errors_max;
98 // The index of the URL to download from, as determined in the previous call
99 // to the policy. For a newly seen payload, this should be -1.
100 int last_download_url_idx;
101 // The number of successive download errors pertaining to this last URL, as
102 // determined in the previous call to the policy. For a newly seen payload,
103 // this should be zero.
104 int last_download_url_num_errors;
105 // An array of errors that occurred while trying to download this update since
106 // the previous call to this policy has returned, or since this payload was
107 // first seen, or since the updater process has started (whichever is later).
108 // Includes the URL index attempted, the error code, and the wallclock-based
109 // timestamp when it occurred.
110 std::vector<std::tuple<int, chromeos_update_engine::ErrorCode, base::Time>>
111 download_errors;
Gilad Arnold78ecbfc2014-10-22 14:38:25 -0700112 // Whether Omaha forbids use of P2P for downloading and/or sharing.
113 bool p2p_downloading_disabled;
114 bool p2p_sharing_disabled;
Gilad Arnold349ac832014-10-06 14:20:28 -0700115 // The number of P2P download attempts and wallclock-based time when P2P
116 // download was first attempted.
117 int p2p_num_attempts;
118 base::Time p2p_first_attempted;
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700119
120 // Information pertaining to update backoff mechanism.
121 //
122 // The currently known (persisted) wallclock-based backoff expiration time;
123 // zero if none.
124 base::Time backoff_expiry;
125 // Whether backoff is disabled by Omaha.
126 bool is_backoff_disabled;
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700127
128 // Information pertaining to update scattering.
129 //
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700130 // The currently knwon (persisted) scattering wallclock-based wait period and
131 // update check threshold; zero if none.
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700132 base::TimeDelta scatter_wait_period;
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700133 int scatter_check_threshold;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700134 // Maximum wait period allowed for this update, as determined by Omaha.
135 base::TimeDelta scatter_wait_period_max;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700136 // Minimum/maximum check threshold values.
137 // TODO(garnold) These appear to not be related to the current update and so
Gilad Arnoldddd3fe32014-05-22 12:57:09 -0700138 // should probably be obtained as variables via UpdaterProvider.
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700139 int scatter_check_threshold_min;
140 int scatter_check_threshold_max;
141};
142
143// Results regarding the downloading and applying of an update, as determined by
144// UpdateCanStart.
145//
146// An enumerator for the reasons of not allowing an update to start.
147enum class UpdateCannotStartReason {
148 kUndefined,
149 kCheckDue,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700150 kScattering,
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700151 kBackoff,
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700152 kCannotDownload,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700153};
154
Gilad Arnold42f253b2014-06-25 12:39:17 -0700155struct UpdateDownloadParams {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700156 // Whether the update attempt is allowed to proceed.
157 bool update_can_start;
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700158 // If update cannot proceed, a reason code for why it cannot do so.
159 UpdateCannotStartReason cannot_start_reason;
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700160
Gilad Arnold14a9e702014-10-08 08:09:09 -0700161 // Download related attributes. The update engine uses them to choose the
162 // means for downloading and applying an update.
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700163 //
Gilad Arnold14a9e702014-10-08 08:09:09 -0700164 // The index of the download URL to use (-1 means no suitable URL was found)
165 // and whether it can be used. Even if there's no URL or its use is not
166 // allowed (backoff, scattering) there may still be other means for download
167 // (like P2P). The URL index needs to be persisted and handed back to the
168 // policy on the next time it is called.
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700169 int download_url_idx;
Gilad Arnold14a9e702014-10-08 08:09:09 -0700170 bool download_url_allowed;
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700171 // The number of download errors associated with this download URL. This value
172 // needs to be persisted and handed back to the policy on the next time it is
173 // called.
174 int download_url_num_errors;
Gilad Arnoldb2f99192014-10-07 13:01:52 -0700175 // Whether P2P download and sharing are allowed.
176 bool p2p_downloading_allowed;
177 bool p2p_sharing_allowed;
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700178
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700179 // Other values that need to be persisted and handed to the policy as need on
180 // the next call.
181 //
182 // Whether an update failure has been identified by the policy. The client
183 // should increment and persist its update failure count, and record the time
184 // when this was done; it needs to hand these values back to the policy
185 // (UpdateState.{num_failures,failures_last_updated}) on the next time it is
186 // called.
187 bool do_increment_failures;
188 // The current backof expiry.
189 base::Time backoff_expiry;
190 // The scattering wait period and check threshold.
191 base::TimeDelta scatter_wait_period;
192 int scatter_check_threshold;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700193};
Alex Deymo0d11c602014-04-23 20:12:20 -0700194
Alex Deymoc705cc82014-02-19 11:15:00 -0800195// The Policy class is an interface to the ensemble of policy requests that the
196// client can make. A derived class includes the policy implementations of
197// these.
198//
199// When compile-time selection of the policy is required due to missing or extra
200// parts in a given platform, a different Policy subclass can be used.
201class Policy {
202 public:
203 virtual ~Policy() {}
204
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700205 // Returns the name of a public policy request.
206 // IMPORTANT: Be sure to add a conditional for each new public policy that is
207 // being added to this class in the future.
208 template<typename R, typename... Args>
209 std::string PolicyRequestName(
210 EvalStatus (Policy::*policy_method)(EvaluationContext*, State*,
211 std::string*, R*,
212 Args...) const) const {
213 std::string class_name = PolicyName() + "::";
214
215 if (reinterpret_cast<typeof(&Policy::UpdateCheckAllowed)>(
216 policy_method) == &Policy::UpdateCheckAllowed)
217 return class_name + "UpdateCheckAllowed";
Aaron Wood23bd3392017-10-06 14:48:25 -0700218 if (reinterpret_cast<typeof(&Policy::UpdateCanBeApplied)>(policy_method) ==
219 &Policy::UpdateCanBeApplied)
220 return class_name + "UpdateCanBeApplied";
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700221 if (reinterpret_cast<typeof(&Policy::UpdateCanStart)>(
222 policy_method) == &Policy::UpdateCanStart)
223 return class_name + "UpdateCanStart";
Gilad Arnold684219d2014-07-07 14:54:57 -0700224 if (reinterpret_cast<typeof(&Policy::UpdateDownloadAllowed)>(
225 policy_method) == &Policy::UpdateDownloadAllowed)
226 return class_name + "UpdateDownloadAllowed";
Gilad Arnold78ecbfc2014-10-22 14:38:25 -0700227 if (reinterpret_cast<typeof(&Policy::P2PEnabled)>(
228 policy_method) == &Policy::P2PEnabled)
229 return class_name + "P2PEnabled";
230 if (reinterpret_cast<typeof(&Policy::P2PEnabledChanged)>(
231 policy_method) == &Policy::P2PEnabledChanged)
232 return class_name + "P2PEnabledChanged";
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700233
234 NOTREACHED();
235 return class_name + "(unknown)";
236 }
237
238
Alex Deymoc705cc82014-02-19 11:15:00 -0800239 // List of policy requests. A policy request takes an EvaluationContext as the
Alex Deymo2de23f52014-02-26 14:30:13 -0800240 // first argument, a State instance, a returned error message, a returned
241 // value and optionally followed by one or more arbitrary constant arguments.
Alex Deymoc705cc82014-02-19 11:15:00 -0800242 //
Alex Deymoe636c3c2014-03-11 19:02:08 -0700243 // When the implementation fails, the method returns EvalStatus::kFailed and
244 // sets the |error| string.
Alex Deymoc705cc82014-02-19 11:15:00 -0800245
246 // UpdateCheckAllowed returns whether it is allowed to request an update check
247 // to Omaha.
Alex Deymo0d11c602014-04-23 20:12:20 -0700248 virtual EvalStatus UpdateCheckAllowed(
249 EvaluationContext* ec, State* state, std::string* error,
250 UpdateCheckParams* result) const = 0;
Alex Deymoc705cc82014-02-19 11:15:00 -0800251
Aaron Wood23bd3392017-10-06 14:48:25 -0700252 // UpdateCanBeApplied returns whether the given |install_plan| can be acted
253 // on at this time. The reason for not applying is returned in |result|.
254 // The Policy may modify the passed-in |install_plan|, based on the
255 // implementation in the Policy and values provided by the EvaluationContext.
256 virtual EvalStatus UpdateCanBeApplied(
257 EvaluationContext* ec,
258 State* state,
259 std::string* error,
260 chromeos_update_engine::ErrorCode* result,
261 chromeos_update_engine::InstallPlan* install_plan) const = 0;
262
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700263 // Returns EvalStatus::kSucceeded if either an update can start being
264 // processed, or the attempt needs to be aborted. In cases where the update
265 // needs to wait for some condition to be satisfied, but none of the values
266 // that need to be persisted has changed, returns
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700267 // EvalStatus::kAskMeAgainLater. Arguments include an |update_state| that
Alex Vakulenko072359c2014-07-18 11:41:07 -0700268 // encapsulates data pertaining to the current ongoing update process.
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700269 virtual EvalStatus UpdateCanStart(
270 EvaluationContext* ec,
271 State* state,
272 std::string* error,
Gilad Arnold42f253b2014-06-25 12:39:17 -0700273 UpdateDownloadParams* result,
Gilad Arnoldd78caf92014-09-24 09:28:14 -0700274 UpdateState update_state) const = 0;
Gilad Arnoldaf2f6ae2014-04-28 14:14:52 -0700275
Gilad Arnold684219d2014-07-07 14:54:57 -0700276 // Checks whether downloading of an update is allowed; currently, this checks
277 // whether the network connection type is suitable for updating over. May
278 // consult the shill provider as well as the device policy (if available).
Gilad Arnold0adbc942014-05-12 10:35:43 -0700279 // Returns |EvalStatus::kSucceeded|, setting |result| according to whether or
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700280 // not the current connection can be used; on error, returns
Gilad Arnold0adbc942014-05-12 10:35:43 -0700281 // |EvalStatus::kFailed| and sets |error| accordingly.
Gilad Arnold684219d2014-07-07 14:54:57 -0700282 virtual EvalStatus UpdateDownloadAllowed(
Gilad Arnold0adbc942014-05-12 10:35:43 -0700283 EvaluationContext* ec,
284 State* state,
285 std::string* error,
286 bool* result) const = 0;
287
Gilad Arnold78ecbfc2014-10-22 14:38:25 -0700288 // Checks whether P2P is enabled. This may consult device policy and other
289 // global settings.
290 virtual EvalStatus P2PEnabled(
291 EvaluationContext* ec, State* state, std::string* error,
292 bool* result) const = 0;
293
294 // Checks whether P2P is enabled, but blocks (returns
295 // |EvalStatus::kAskMeAgainLater|) until it is different from |prev_result|.
296 // If the P2P enabled status is not expected to change, will return
297 // immediately with |EvalStatus::kSucceeded|. This internally uses the
298 // P2PEnabled() policy above.
299 virtual EvalStatus P2PEnabledChanged(
300 EvaluationContext* ec, State* state, std::string* error,
301 bool* result, bool prev_result) const = 0;
302
Alex Deymoc705cc82014-02-19 11:15:00 -0800303 protected:
304 Policy() {}
305
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700306 // Returns the name of the actual policy class.
307 virtual std::string PolicyName() const = 0;
308
Alex Deymoc705cc82014-02-19 11:15:00 -0800309 private:
310 DISALLOW_COPY_AND_ASSIGN(Policy);
311};
312
Alex Deymo63784a52014-05-28 10:46:14 -0700313} // namespace chromeos_update_manager
Alex Deymoc705cc82014-02-19 11:15:00 -0800314
Gilad Arnold48415f12014-06-27 07:10:58 -0700315#endif // UPDATE_ENGINE_UPDATE_MANAGER_POLICY_H_