blob: 8ec5c9eaa86b8ea73d4b577bbc8310c7bc4b1971 [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 Arnoldb3b05442014-05-30 14:25:05 -07009#include <vector>
Alex Deymo0d11c602014-04-23 20:12:20 -070010
Gilad Arnoldb3b05442014-05-30 14:25:05 -070011#include "update_engine/error_code.h"
Alex Deymo63784a52014-05-28 10:46:14 -070012#include "update_engine/update_manager/evaluation_context.h"
13#include "update_engine/update_manager/state.h"
Alex Deymoc705cc82014-02-19 11:15:00 -080014
Alex Deymo63784a52014-05-28 10:46:14 -070015namespace chromeos_update_manager {
Alex Deymoc705cc82014-02-19 11:15:00 -080016
17// The three different results of a policy request.
Alex Deymoe636c3c2014-03-11 19:02:08 -070018enum class EvalStatus {
19 kFailed,
20 kSucceeded,
21 kAskMeAgainLater,
Alex Deymoc705cc82014-02-19 11:15:00 -080022};
23
Alex Deymo0d11c602014-04-23 20:12:20 -070024std::string ToString(EvalStatus status);
25
26// Parameters of an update check. These parameters are determined by the
27// UpdateCheckAllowed policy.
28struct UpdateCheckParams {
29 bool updates_enabled; // Whether the auto-updates are enabled on this build.
Gilad Arnold42f253b2014-06-25 12:39:17 -070030
31 // Attributes pertaining to the case where update checks are allowed.
32 //
Gilad Arnoldd4b30322014-07-21 15:35:27 -070033 // A target version prefix, if imposed by policy; otherwise, an empty string.
34 std::string target_version_prefix;
Gilad Arnold42f253b2014-06-25 12:39:17 -070035 // A target channel, if so imposed by policy; otherwise, an empty string.
36 std::string target_channel;
Gilad Arnold44dc3bf2014-07-18 23:39:38 -070037
38 // Whether the allowed update is interactive (user-initiated) or periodic.
39 bool is_interactive;
Alex Deymo0d11c602014-04-23 20:12:20 -070040};
41
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070042// Input arguments to UpdateCanStart.
43//
44// A snapshot of the state of the current update process.
45struct UpdateState {
Gilad Arnoldb3b05442014-05-30 14:25:05 -070046 // Information pertaining to the Omaha update response.
47 //
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070048 // Time when update was first offered by Omaha.
49 base::Time first_seen;
50 // Number of update checks returning the current update.
51 int num_checks;
Gilad Arnoldb3b05442014-05-30 14:25:05 -070052
53 // Information pertaining to the update download URL.
54 //
55 // An array of download URLs provided by Omaha.
56 std::vector<std::string> download_urls;
57 // Max number of failures allowed per download URL.
58 int download_failures_max;
59 // The index of the URL to use, as previously determined by the policy. This
60 // number is significant iff |num_checks| is greater than 1.
61 int download_url_idx;
62 // The number of failures already associated with this URL.
63 int download_url_num_failures;
64 // An array of failure error codes that occurred since the latest reported
65 // ones (included in the number above).
66 std::vector<chromeos_update_engine::ErrorCode> download_url_error_codes;
67
68 // Information pertaining to update scattering.
69 //
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070070 // Scattering wallclock-based wait period, as returned by the policy.
71 base::TimeDelta scatter_wait_period;
72 // Maximum wait period allowed for this update, as determined by Omaha.
73 base::TimeDelta scatter_wait_period_max;
74 // Scattering update check threshold, as returned by the policy.
75 int scatter_check_threshold;
76 // Minimum/maximum check threshold values.
77 // TODO(garnold) These appear to not be related to the current update and so
Gilad Arnoldddd3fe32014-05-22 12:57:09 -070078 // should probably be obtained as variables via UpdaterProvider.
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070079 int scatter_check_threshold_min;
80 int scatter_check_threshold_max;
81};
82
83// Results regarding the downloading and applying of an update, as determined by
84// UpdateCanStart.
85//
86// An enumerator for the reasons of not allowing an update to start.
87enum class UpdateCannotStartReason {
88 kUndefined,
89 kCheckDue,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070090 kScattering,
Gilad Arnoldb3b05442014-05-30 14:25:05 -070091 kCannotDownload,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070092};
93
Gilad Arnold42f253b2014-06-25 12:39:17 -070094struct UpdateDownloadParams {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070095 // Whether the update attempt is allowed to proceed.
96 bool update_can_start;
Gilad Arnoldb3b05442014-05-30 14:25:05 -070097
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070098 // Attributes pertaining to the case where update is allowed. The update
99 // engine uses them to choose the means for downloading and applying an
100 // update.
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700101 bool p2p_allowed;
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700102 // The index of the download URL to use, and the number of failures associated
103 // with this URL. An index value of -1 indicates that no suitable URL is
104 // available, but there may be other means for download (like P2P).
105 int download_url_idx;
106 int download_url_num_failures;
107
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700108 // Attributes pertaining to the case where update is not allowed. Some are
109 // needed for storing values to persistent storage, others for
110 // logging/metrics.
111 UpdateCannotStartReason cannot_start_reason;
112 base::TimeDelta scatter_wait_period; // Needs to be persisted.
113 int scatter_check_threshold; // Needs to be persisted.
114};
Alex Deymo0d11c602014-04-23 20:12:20 -0700115
Alex Deymoc705cc82014-02-19 11:15:00 -0800116// The Policy class is an interface to the ensemble of policy requests that the
117// client can make. A derived class includes the policy implementations of
118// these.
119//
120// When compile-time selection of the policy is required due to missing or extra
121// parts in a given platform, a different Policy subclass can be used.
122class Policy {
123 public:
124 virtual ~Policy() {}
125
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700126 // Returns the name of a public policy request.
127 // IMPORTANT: Be sure to add a conditional for each new public policy that is
128 // being added to this class in the future.
129 template<typename R, typename... Args>
130 std::string PolicyRequestName(
131 EvalStatus (Policy::*policy_method)(EvaluationContext*, State*,
132 std::string*, R*,
133 Args...) const) const {
134 std::string class_name = PolicyName() + "::";
135
136 if (reinterpret_cast<typeof(&Policy::UpdateCheckAllowed)>(
137 policy_method) == &Policy::UpdateCheckAllowed)
138 return class_name + "UpdateCheckAllowed";
139 if (reinterpret_cast<typeof(&Policy::UpdateCanStart)>(
140 policy_method) == &Policy::UpdateCanStart)
141 return class_name + "UpdateCanStart";
Gilad Arnold684219d2014-07-07 14:54:57 -0700142 if (reinterpret_cast<typeof(&Policy::UpdateDownloadAllowed)>(
143 policy_method) == &Policy::UpdateDownloadAllowed)
144 return class_name + "UpdateDownloadAllowed";
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700145
146 NOTREACHED();
147 return class_name + "(unknown)";
148 }
149
150
Alex Deymoc705cc82014-02-19 11:15:00 -0800151 // List of policy requests. A policy request takes an EvaluationContext as the
Alex Deymo2de23f52014-02-26 14:30:13 -0800152 // first argument, a State instance, a returned error message, a returned
153 // value and optionally followed by one or more arbitrary constant arguments.
Alex Deymoc705cc82014-02-19 11:15:00 -0800154 //
Alex Deymoe636c3c2014-03-11 19:02:08 -0700155 // When the implementation fails, the method returns EvalStatus::kFailed and
156 // sets the |error| string.
Alex Deymoc705cc82014-02-19 11:15:00 -0800157
158 // UpdateCheckAllowed returns whether it is allowed to request an update check
159 // to Omaha.
Alex Deymo0d11c602014-04-23 20:12:20 -0700160 virtual EvalStatus UpdateCheckAllowed(
161 EvaluationContext* ec, State* state, std::string* error,
162 UpdateCheckParams* result) const = 0;
Alex Deymoc705cc82014-02-19 11:15:00 -0800163
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700164 // Returns EvalStatus::kSucceeded if either an update can start being
165 // processed, or the attempt needs to be aborted. In cases where the update
166 // needs to wait for some condition to be satisfied, but none of the values
167 // that need to be persisted has changed, returns
168 // EvalStatus::kAskMeAgainLater. Arguments include an |interactive| flag that
169 // tells whether the update is user initiated, and an |update_state| that
Alex Vakulenko072359c2014-07-18 11:41:07 -0700170 // encapsulates data pertaining to the current ongoing update process.
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700171 virtual EvalStatus UpdateCanStart(
172 EvaluationContext* ec,
173 State* state,
174 std::string* error,
Gilad Arnold42f253b2014-06-25 12:39:17 -0700175 UpdateDownloadParams* result,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700176 const bool interactive,
177 const UpdateState& update_state) const = 0;
Gilad Arnoldaf2f6ae2014-04-28 14:14:52 -0700178
Gilad Arnold684219d2014-07-07 14:54:57 -0700179 // Checks whether downloading of an update is allowed; currently, this checks
180 // whether the network connection type is suitable for updating over. May
181 // consult the shill provider as well as the device policy (if available).
Gilad Arnold0adbc942014-05-12 10:35:43 -0700182 // Returns |EvalStatus::kSucceeded|, setting |result| according to whether or
183 // not the current connection can be used; on failure, returns
184 // |EvalStatus::kFailed| and sets |error| accordingly.
Gilad Arnold684219d2014-07-07 14:54:57 -0700185 virtual EvalStatus UpdateDownloadAllowed(
Gilad Arnold0adbc942014-05-12 10:35:43 -0700186 EvaluationContext* ec,
187 State* state,
188 std::string* error,
189 bool* result) const = 0;
190
Alex Deymoc705cc82014-02-19 11:15:00 -0800191 protected:
192 Policy() {}
193
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700194 // Returns the name of the actual policy class.
195 virtual std::string PolicyName() const = 0;
196
Alex Deymoc705cc82014-02-19 11:15:00 -0800197 private:
198 DISALLOW_COPY_AND_ASSIGN(Policy);
199};
200
Alex Deymo63784a52014-05-28 10:46:14 -0700201} // namespace chromeos_update_manager
Alex Deymoc705cc82014-02-19 11:15:00 -0800202
Gilad Arnold48415f12014-06-27 07:10:58 -0700203#endif // UPDATE_ENGINE_UPDATE_MANAGER_POLICY_H_