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 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 5 | #ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_UPDATE_MANAGER_POLICY_H_ |
| 6 | #define CHROMEOS_PLATFORM_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 | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame^] | 9 | #include <vector> |
Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 10 | |
Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame^] | 11 | #include "update_engine/error_code.h" |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 12 | #include "update_engine/update_manager/evaluation_context.h" |
| 13 | #include "update_engine/update_manager/state.h" |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 14 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 15 | namespace chromeos_update_manager { |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 16 | |
| 17 | // The three different results of a policy request. |
Alex Deymo | e636c3c | 2014-03-11 19:02:08 -0700 | [diff] [blame] | 18 | enum class EvalStatus { |
| 19 | kFailed, |
| 20 | kSucceeded, |
| 21 | kAskMeAgainLater, |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 22 | }; |
| 23 | |
Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 24 | std::string ToString(EvalStatus status); |
| 25 | |
| 26 | // Parameters of an update check. These parameters are determined by the |
| 27 | // UpdateCheckAllowed policy. |
| 28 | struct UpdateCheckParams { |
| 29 | bool updates_enabled; // Whether the auto-updates are enabled on this build. |
| 30 | }; |
| 31 | |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 32 | // Input arguments to UpdateCanStart. |
| 33 | // |
| 34 | // A snapshot of the state of the current update process. |
| 35 | struct UpdateState { |
Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame^] | 36 | // Information pertaining to the Omaha update response. |
| 37 | // |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 38 | // Time when update was first offered by Omaha. |
| 39 | base::Time first_seen; |
| 40 | // Number of update checks returning the current update. |
| 41 | int num_checks; |
Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame^] | 42 | |
| 43 | // Information pertaining to the update download URL. |
| 44 | // |
| 45 | // An array of download URLs provided by Omaha. |
| 46 | std::vector<std::string> download_urls; |
| 47 | // Max number of failures allowed per download URL. |
| 48 | int download_failures_max; |
| 49 | // The index of the URL to use, as previously determined by the policy. This |
| 50 | // number is significant iff |num_checks| is greater than 1. |
| 51 | int download_url_idx; |
| 52 | // The number of failures already associated with this URL. |
| 53 | int download_url_num_failures; |
| 54 | // An array of failure error codes that occurred since the latest reported |
| 55 | // ones (included in the number above). |
| 56 | std::vector<chromeos_update_engine::ErrorCode> download_url_error_codes; |
| 57 | |
| 58 | // Information pertaining to update scattering. |
| 59 | // |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 60 | // Scattering wallclock-based wait period, as returned by the policy. |
| 61 | base::TimeDelta scatter_wait_period; |
| 62 | // Maximum wait period allowed for this update, as determined by Omaha. |
| 63 | base::TimeDelta scatter_wait_period_max; |
| 64 | // Scattering update check threshold, as returned by the policy. |
| 65 | int scatter_check_threshold; |
| 66 | // Minimum/maximum check threshold values. |
| 67 | // 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] | 68 | // should probably be obtained as variables via UpdaterProvider. |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 69 | int scatter_check_threshold_min; |
| 70 | int scatter_check_threshold_max; |
| 71 | }; |
| 72 | |
| 73 | // Results regarding the downloading and applying of an update, as determined by |
| 74 | // UpdateCanStart. |
| 75 | // |
| 76 | // An enumerator for the reasons of not allowing an update to start. |
| 77 | enum class UpdateCannotStartReason { |
| 78 | kUndefined, |
| 79 | kCheckDue, |
| 80 | kDisabledByPolicy, |
| 81 | kScattering, |
Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame^] | 82 | kCannotDownload, |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 83 | }; |
| 84 | |
| 85 | struct UpdateCanStartResult { |
| 86 | // Whether the update attempt is allowed to proceed. |
| 87 | bool update_can_start; |
Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame^] | 88 | |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 89 | // Attributes pertaining to the case where update is allowed. The update |
| 90 | // engine uses them to choose the means for downloading and applying an |
| 91 | // update. |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 92 | bool p2p_allowed; |
| 93 | std::string target_channel; |
Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame^] | 94 | // The index of the download URL to use, and the number of failures associated |
| 95 | // with this URL. An index value of -1 indicates that no suitable URL is |
| 96 | // available, but there may be other means for download (like P2P). |
| 97 | int download_url_idx; |
| 98 | int download_url_num_failures; |
| 99 | |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 100 | // Attributes pertaining to the case where update is not allowed. Some are |
| 101 | // needed for storing values to persistent storage, others for |
| 102 | // logging/metrics. |
| 103 | UpdateCannotStartReason cannot_start_reason; |
| 104 | base::TimeDelta scatter_wait_period; // Needs to be persisted. |
| 105 | int scatter_check_threshold; // Needs to be persisted. |
| 106 | }; |
Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 107 | |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 108 | // The Policy class is an interface to the ensemble of policy requests that the |
| 109 | // client can make. A derived class includes the policy implementations of |
| 110 | // these. |
| 111 | // |
| 112 | // When compile-time selection of the policy is required due to missing or extra |
| 113 | // parts in a given platform, a different Policy subclass can be used. |
| 114 | class Policy { |
| 115 | public: |
| 116 | virtual ~Policy() {} |
| 117 | |
Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame^] | 118 | // Returns the name of a public policy request. |
| 119 | // IMPORTANT: Be sure to add a conditional for each new public policy that is |
| 120 | // being added to this class in the future. |
| 121 | template<typename R, typename... Args> |
| 122 | std::string PolicyRequestName( |
| 123 | EvalStatus (Policy::*policy_method)(EvaluationContext*, State*, |
| 124 | std::string*, R*, |
| 125 | Args...) const) const { |
| 126 | std::string class_name = PolicyName() + "::"; |
| 127 | |
| 128 | if (reinterpret_cast<typeof(&Policy::UpdateCheckAllowed)>( |
| 129 | policy_method) == &Policy::UpdateCheckAllowed) |
| 130 | return class_name + "UpdateCheckAllowed"; |
| 131 | if (reinterpret_cast<typeof(&Policy::UpdateCanStart)>( |
| 132 | policy_method) == &Policy::UpdateCanStart) |
| 133 | return class_name + "UpdateCanStart"; |
| 134 | if (reinterpret_cast<typeof(&Policy::UpdateCurrentConnectionAllowed)>( |
| 135 | policy_method) == &Policy::UpdateCurrentConnectionAllowed) |
| 136 | return class_name + "UpdateCurrentConnectionAllowed"; |
| 137 | |
| 138 | NOTREACHED(); |
| 139 | return class_name + "(unknown)"; |
| 140 | } |
| 141 | |
| 142 | |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 143 | // List of policy requests. A policy request takes an EvaluationContext as the |
Alex Deymo | 2de23f5 | 2014-02-26 14:30:13 -0800 | [diff] [blame] | 144 | // first argument, a State instance, a returned error message, a returned |
| 145 | // value and optionally followed by one or more arbitrary constant arguments. |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 146 | // |
Alex Deymo | e636c3c | 2014-03-11 19:02:08 -0700 | [diff] [blame] | 147 | // When the implementation fails, the method returns EvalStatus::kFailed and |
| 148 | // sets the |error| string. |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 149 | |
| 150 | // UpdateCheckAllowed returns whether it is allowed to request an update check |
| 151 | // to Omaha. |
Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 152 | virtual EvalStatus UpdateCheckAllowed( |
| 153 | EvaluationContext* ec, State* state, std::string* error, |
| 154 | UpdateCheckParams* result) const = 0; |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 155 | |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 156 | // Returns EvalStatus::kSucceeded if either an update can start being |
| 157 | // processed, or the attempt needs to be aborted. In cases where the update |
| 158 | // needs to wait for some condition to be satisfied, but none of the values |
| 159 | // that need to be persisted has changed, returns |
| 160 | // EvalStatus::kAskMeAgainLater. Arguments include an |interactive| flag that |
| 161 | // tells whether the update is user initiated, and an |update_state| that |
| 162 | // encapsulates data pertaining to the currnet ongoing update process. |
| 163 | virtual EvalStatus UpdateCanStart( |
| 164 | EvaluationContext* ec, |
| 165 | State* state, |
| 166 | std::string* error, |
| 167 | UpdateCanStartResult* result, |
| 168 | const bool interactive, |
| 169 | const UpdateState& update_state) const = 0; |
Gilad Arnold | af2f6ae | 2014-04-28 14:14:52 -0700 | [diff] [blame] | 170 | |
Gilad Arnold | 0adbc94 | 2014-05-12 10:35:43 -0700 | [diff] [blame] | 171 | // Checks whether updating is allowed over the current network connection |
| 172 | // Consults the shill provider as well as the device policy (if available). |
| 173 | // Returns |EvalStatus::kSucceeded|, setting |result| according to whether or |
| 174 | // not the current connection can be used; on failure, returns |
| 175 | // |EvalStatus::kFailed| and sets |error| accordingly. |
| 176 | virtual EvalStatus UpdateCurrentConnectionAllowed( |
| 177 | EvaluationContext* ec, |
| 178 | State* state, |
| 179 | std::string* error, |
| 180 | bool* result) const = 0; |
| 181 | |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 182 | protected: |
| 183 | Policy() {} |
| 184 | |
Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame^] | 185 | // Returns the name of the actual policy class. |
| 186 | virtual std::string PolicyName() const = 0; |
| 187 | |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 188 | private: |
| 189 | DISALLOW_COPY_AND_ASSIGN(Policy); |
| 190 | }; |
| 191 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 192 | } // namespace chromeos_update_manager |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 193 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 194 | #endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_UPDATE_MANAGER_POLICY_H_ |