blob: be30f26b0e8f533756f0dac084596640c9750392 [file] [log] [blame]
Alex Deymoc705cc82014-02-19 11:15:00 -08001// 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 Arnold48415f12014-06-27 07:10:58 -07005#ifndef UPDATE_ENGINE_UPDATE_MANAGER_POLICY_H_
6#define UPDATE_ENGINE_UPDATE_MANAGER_POLICY_H_
Alex Deymoc705cc82014-02-19 11:15:00 -08007
Alex Deymo0d11c602014-04-23 20:12:20 -07008#include <string>
Gilad Arnolddc4bb262014-07-23 10:45:19 -07009#include <tuple>
Gilad Arnoldb3b05442014-05-30 14:25:05 -070010#include <vector>
Alex Deymo0d11c602014-04-23 20:12:20 -070011
Gilad Arnoldb3b05442014-05-30 14:25:05 -070012#include "update_engine/error_code.h"
Alex Deymo63784a52014-05-28 10:46:14 -070013#include "update_engine/update_manager/evaluation_context.h"
14#include "update_engine/update_manager/state.h"
Alex Deymoc705cc82014-02-19 11:15:00 -080015
Alex Deymo63784a52014-05-28 10:46:14 -070016namespace chromeos_update_manager {
Alex Deymoc705cc82014-02-19 11:15:00 -080017
18// The three different results of a policy request.
Alex Deymoe636c3c2014-03-11 19:02:08 -070019enum class EvalStatus {
20 kFailed,
21 kSucceeded,
22 kAskMeAgainLater,
Alex Deymoc705cc82014-02-19 11:15:00 -080023};
24
Alex Deymo0d11c602014-04-23 20:12:20 -070025std::string ToString(EvalStatus status);
26
27// Parameters of an update check. These parameters are determined by the
28// UpdateCheckAllowed policy.
29struct UpdateCheckParams {
30 bool updates_enabled; // Whether the auto-updates are enabled on this build.
Gilad Arnold42f253b2014-06-25 12:39:17 -070031
32 // Attributes pertaining to the case where update checks are allowed.
33 //
Gilad Arnoldd4b30322014-07-21 15:35:27 -070034 // A target version prefix, if imposed by policy; otherwise, an empty string.
35 std::string target_version_prefix;
Gilad Arnold42f253b2014-06-25 12:39:17 -070036 // A target channel, if so imposed by policy; otherwise, an empty string.
37 std::string target_channel;
Gilad Arnold44dc3bf2014-07-18 23:39:38 -070038
39 // Whether the allowed update is interactive (user-initiated) or periodic.
40 bool is_interactive;
Alex Deymo0d11c602014-04-23 20:12:20 -070041};
42
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070043// Input arguments to UpdateCanStart.
44//
Gilad Arnolddc4bb262014-07-23 10:45:19 -070045// 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 Arnoldf62a4b82014-05-01 07:41:07 -070048struct UpdateState {
Gilad Arnolddc4bb262014-07-23 10:45:19 -070049 // Information pertaining to the current update payload and/or check.
Gilad Arnoldb3b05442014-05-30 14:25:05 -070050 //
Gilad Arnolddc4bb262014-07-23 10:45:19 -070051 // 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 Arnoldf62a4b82014-05-01 07:41:07 -070057 base::Time first_seen;
Gilad Arnolddc4bb262014-07-23 10:45:19 -070058 // Number of consecutive update checks returning the current update.
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070059 int num_checks;
Gilad Arnolddc4bb262014-07-23 10:45:19 -070060 // 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 Arnoldb3b05442014-05-30 14:25:05 -070068
Gilad Arnolddc4bb262014-07-23 10:45:19 -070069 // Information pertaining to downloading and applying of the current update.
Gilad Arnoldb3b05442014-05-30 14:25:05 -070070 //
71 // An array of download URLs provided by Omaha.
72 std::vector<std::string> download_urls;
Gilad Arnolddc4bb262014-07-23 10:45:19 -070073 // 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;
Gilad Arnold78ecbfc2014-10-22 14:38:25 -070089 // Whether Omaha forbids use of P2P for downloading and/or sharing.
90 bool p2p_downloading_disabled;
91 bool p2p_sharing_disabled;
Gilad Arnold349ac832014-10-06 14:20:28 -070092 // The number of P2P download attempts and wallclock-based time when P2P
93 // download was first attempted.
94 int p2p_num_attempts;
95 base::Time p2p_first_attempted;
Gilad Arnolddc4bb262014-07-23 10:45:19 -070096
97 // Information pertaining to update backoff mechanism.
98 //
99 // The currently known (persisted) wallclock-based backoff expiration time;
100 // zero if none.
101 base::Time backoff_expiry;
102 // Whether backoff is disabled by Omaha.
103 bool is_backoff_disabled;
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700104
105 // Information pertaining to update scattering.
106 //
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700107 // The currently knwon (persisted) scattering wallclock-based wait period and
108 // update check threshold; zero if none.
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700109 base::TimeDelta scatter_wait_period;
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700110 int scatter_check_threshold;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700111 // Maximum wait period allowed for this update, as determined by Omaha.
112 base::TimeDelta scatter_wait_period_max;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700113 // Minimum/maximum check threshold values.
114 // TODO(garnold) These appear to not be related to the current update and so
Gilad Arnoldddd3fe32014-05-22 12:57:09 -0700115 // should probably be obtained as variables via UpdaterProvider.
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700116 int scatter_check_threshold_min;
117 int scatter_check_threshold_max;
118};
119
120// Results regarding the downloading and applying of an update, as determined by
121// UpdateCanStart.
122//
123// An enumerator for the reasons of not allowing an update to start.
124enum class UpdateCannotStartReason {
125 kUndefined,
126 kCheckDue,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700127 kScattering,
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700128 kBackoff,
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700129 kCannotDownload,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700130};
131
Gilad Arnold42f253b2014-06-25 12:39:17 -0700132struct UpdateDownloadParams {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700133 // Whether the update attempt is allowed to proceed.
134 bool update_can_start;
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700135 // If update cannot proceed, a reason code for why it cannot do so.
136 UpdateCannotStartReason cannot_start_reason;
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700137
Gilad Arnold14a9e702014-10-08 08:09:09 -0700138 // Download related attributes. The update engine uses them to choose the
139 // means for downloading and applying an update.
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700140 //
Gilad Arnold14a9e702014-10-08 08:09:09 -0700141 // The index of the download URL to use (-1 means no suitable URL was found)
142 // and whether it can be used. Even if there's no URL or its use is not
143 // allowed (backoff, scattering) there may still be other means for download
144 // (like P2P). The URL index needs to be persisted and handed back to the
145 // policy on the next time it is called.
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700146 int download_url_idx;
Gilad Arnold14a9e702014-10-08 08:09:09 -0700147 bool download_url_allowed;
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700148 // The number of download errors associated with this download URL. This value
149 // needs to be persisted and handed back to the policy on the next time it is
150 // called.
151 int download_url_num_errors;
Gilad Arnoldb2f99192014-10-07 13:01:52 -0700152 // Whether P2P download and sharing are allowed.
153 bool p2p_downloading_allowed;
154 bool p2p_sharing_allowed;
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700155
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700156 // Other values that need to be persisted and handed to the policy as need on
157 // the next call.
158 //
159 // Whether an update failure has been identified by the policy. The client
160 // should increment and persist its update failure count, and record the time
161 // when this was done; it needs to hand these values back to the policy
162 // (UpdateState.{num_failures,failures_last_updated}) on the next time it is
163 // called.
164 bool do_increment_failures;
165 // The current backof expiry.
166 base::Time backoff_expiry;
167 // The scattering wait period and check threshold.
168 base::TimeDelta scatter_wait_period;
169 int scatter_check_threshold;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700170};
Alex Deymo0d11c602014-04-23 20:12:20 -0700171
Alex Deymoc705cc82014-02-19 11:15:00 -0800172// The Policy class is an interface to the ensemble of policy requests that the
173// client can make. A derived class includes the policy implementations of
174// these.
175//
176// When compile-time selection of the policy is required due to missing or extra
177// parts in a given platform, a different Policy subclass can be used.
178class Policy {
179 public:
180 virtual ~Policy() {}
181
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700182 // Returns the name of a public policy request.
183 // IMPORTANT: Be sure to add a conditional for each new public policy that is
184 // being added to this class in the future.
185 template<typename R, typename... Args>
186 std::string PolicyRequestName(
187 EvalStatus (Policy::*policy_method)(EvaluationContext*, State*,
188 std::string*, R*,
189 Args...) const) const {
190 std::string class_name = PolicyName() + "::";
191
192 if (reinterpret_cast<typeof(&Policy::UpdateCheckAllowed)>(
193 policy_method) == &Policy::UpdateCheckAllowed)
194 return class_name + "UpdateCheckAllowed";
195 if (reinterpret_cast<typeof(&Policy::UpdateCanStart)>(
196 policy_method) == &Policy::UpdateCanStart)
197 return class_name + "UpdateCanStart";
Gilad Arnold684219d2014-07-07 14:54:57 -0700198 if (reinterpret_cast<typeof(&Policy::UpdateDownloadAllowed)>(
199 policy_method) == &Policy::UpdateDownloadAllowed)
200 return class_name + "UpdateDownloadAllowed";
Gilad Arnold78ecbfc2014-10-22 14:38:25 -0700201 if (reinterpret_cast<typeof(&Policy::P2PEnabled)>(
202 policy_method) == &Policy::P2PEnabled)
203 return class_name + "P2PEnabled";
204 if (reinterpret_cast<typeof(&Policy::P2PEnabledChanged)>(
205 policy_method) == &Policy::P2PEnabledChanged)
206 return class_name + "P2PEnabledChanged";
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700207
208 NOTREACHED();
209 return class_name + "(unknown)";
210 }
211
212
Alex Deymoc705cc82014-02-19 11:15:00 -0800213 // List of policy requests. A policy request takes an EvaluationContext as the
Alex Deymo2de23f52014-02-26 14:30:13 -0800214 // first argument, a State instance, a returned error message, a returned
215 // value and optionally followed by one or more arbitrary constant arguments.
Alex Deymoc705cc82014-02-19 11:15:00 -0800216 //
Alex Deymoe636c3c2014-03-11 19:02:08 -0700217 // When the implementation fails, the method returns EvalStatus::kFailed and
218 // sets the |error| string.
Alex Deymoc705cc82014-02-19 11:15:00 -0800219
220 // UpdateCheckAllowed returns whether it is allowed to request an update check
221 // to Omaha.
Alex Deymo0d11c602014-04-23 20:12:20 -0700222 virtual EvalStatus UpdateCheckAllowed(
223 EvaluationContext* ec, State* state, std::string* error,
224 UpdateCheckParams* result) const = 0;
Alex Deymoc705cc82014-02-19 11:15:00 -0800225
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700226 // Returns EvalStatus::kSucceeded if either an update can start being
227 // processed, or the attempt needs to be aborted. In cases where the update
228 // needs to wait for some condition to be satisfied, but none of the values
229 // that need to be persisted has changed, returns
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700230 // EvalStatus::kAskMeAgainLater. Arguments include an |update_state| that
Alex Vakulenko072359c2014-07-18 11:41:07 -0700231 // encapsulates data pertaining to the current ongoing update process.
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700232 virtual EvalStatus UpdateCanStart(
233 EvaluationContext* ec,
234 State* state,
235 std::string* error,
Gilad Arnold42f253b2014-06-25 12:39:17 -0700236 UpdateDownloadParams* result,
Gilad Arnoldd78caf92014-09-24 09:28:14 -0700237 UpdateState update_state) const = 0;
Gilad Arnoldaf2f6ae2014-04-28 14:14:52 -0700238
Gilad Arnold684219d2014-07-07 14:54:57 -0700239 // Checks whether downloading of an update is allowed; currently, this checks
240 // whether the network connection type is suitable for updating over. May
241 // consult the shill provider as well as the device policy (if available).
Gilad Arnold0adbc942014-05-12 10:35:43 -0700242 // Returns |EvalStatus::kSucceeded|, setting |result| according to whether or
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700243 // not the current connection can be used; on error, returns
Gilad Arnold0adbc942014-05-12 10:35:43 -0700244 // |EvalStatus::kFailed| and sets |error| accordingly.
Gilad Arnold684219d2014-07-07 14:54:57 -0700245 virtual EvalStatus UpdateDownloadAllowed(
Gilad Arnold0adbc942014-05-12 10:35:43 -0700246 EvaluationContext* ec,
247 State* state,
248 std::string* error,
249 bool* result) const = 0;
250
Gilad Arnold78ecbfc2014-10-22 14:38:25 -0700251 // Checks whether P2P is enabled. This may consult device policy and other
252 // global settings.
253 virtual EvalStatus P2PEnabled(
254 EvaluationContext* ec, State* state, std::string* error,
255 bool* result) const = 0;
256
257 // Checks whether P2P is enabled, but blocks (returns
258 // |EvalStatus::kAskMeAgainLater|) until it is different from |prev_result|.
259 // If the P2P enabled status is not expected to change, will return
260 // immediately with |EvalStatus::kSucceeded|. This internally uses the
261 // P2PEnabled() policy above.
262 virtual EvalStatus P2PEnabledChanged(
263 EvaluationContext* ec, State* state, std::string* error,
264 bool* result, bool prev_result) const = 0;
265
Alex Deymoc705cc82014-02-19 11:15:00 -0800266 protected:
267 Policy() {}
268
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700269 // Returns the name of the actual policy class.
270 virtual std::string PolicyName() const = 0;
271
Alex Deymoc705cc82014-02-19 11:15:00 -0800272 private:
273 DISALLOW_COPY_AND_ASSIGN(Policy);
274};
275
Alex Deymo63784a52014-05-28 10:46:14 -0700276} // namespace chromeos_update_manager
Alex Deymoc705cc82014-02-19 11:15:00 -0800277
Gilad Arnold48415f12014-06-27 07:10:58 -0700278#endif // UPDATE_ENGINE_UPDATE_MANAGER_POLICY_H_