blob: ded51646c50ec4859a0a53cd1effdcc3b801810d [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_CHROMEOS_POLICY_H_
18#define UPDATE_ENGINE_UPDATE_MANAGER_CHROMEOS_POLICY_H_
19
20#include <string>
Alex Deymoc705cc82014-02-19 11:15:00 -080021
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070022#include <base/time/time.h>
Alex Deymo0d11c602014-04-23 20:12:20 -070023
Amin Hassani186ff6a2018-02-27 11:06:03 -080024#include "update_engine/update_manager/next_update_check_policy_impl.h"
25#include "update_engine/update_manager/policy_utils.h"
Alex Deymoc705cc82014-02-19 11:15:00 -080026
Alex Deymo63784a52014-05-28 10:46:14 -070027namespace chromeos_update_manager {
Alex Deymoc705cc82014-02-19 11:15:00 -080028
Gilad Arnolddc4bb262014-07-23 10:45:19 -070029// Output information from UpdateBackoffAndDownloadUrl.
30struct UpdateBackoffAndDownloadUrlResult {
31 // Whether the failed attempt count (maintained by the caller) needs to be
32 // incremented.
33 bool do_increment_failures;
34 // The current backoff expiry. Null if backoff is not in effect.
35 base::Time backoff_expiry;
36 // The new URL index to use and number of download errors associated with it.
37 // Significant iff |do_increment_failures| is false and |backoff_expiry| is
38 // null. Negative value means no usable URL was found.
Gilad Arnoldb3b05442014-05-30 14:25:05 -070039 int url_idx;
Gilad Arnolddc4bb262014-07-23 10:45:19 -070040 int url_num_errors;
Gilad Arnoldb3b05442014-05-30 14:25:05 -070041};
42
Gilad Arnolddc4bb262014-07-23 10:45:19 -070043// Parameters for update scattering, as returned by UpdateScattering.
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070044struct UpdateScatteringResult {
45 bool is_scattering;
46 base::TimeDelta wait_period;
47 int check_threshold;
48};
49
Alex Deymoc705cc82014-02-19 11:15:00 -080050// ChromeOSPolicy implements the policy-related logic used in ChromeOS.
51class ChromeOSPolicy : public Policy {
52 public:
53 ChromeOSPolicy() {}
Alex Deymo610277e2014-11-11 21:18:11 -080054 ~ChromeOSPolicy() override {}
Alex Deymoc705cc82014-02-19 11:15:00 -080055
56 // Policy overrides.
Amin Hassani4b717432019-01-14 16:24:20 -080057 EvalStatus UpdateCheckAllowed(EvaluationContext* ec,
58 State* state,
59 std::string* error,
60 UpdateCheckParams* result) const override;
Gilad Arnoldaf2f6ae2014-04-28 14:14:52 -070061
Aaron Wood23bd3392017-10-06 14:48:25 -070062 EvalStatus UpdateCanBeApplied(
63 EvaluationContext* ec,
64 State* state,
65 std::string* error,
66 chromeos_update_engine::ErrorCode* result,
67 chromeos_update_engine::InstallPlan* install_plan) const override;
68
Amin Hassani4b717432019-01-14 16:24:20 -080069 EvalStatus UpdateCanStart(EvaluationContext* ec,
70 State* state,
71 std::string* error,
72 UpdateDownloadParams* result,
73 UpdateState update_state) const override;
Alex Deymoc705cc82014-02-19 11:15:00 -080074
Amin Hassani4b717432019-01-14 16:24:20 -080075 EvalStatus UpdateDownloadAllowed(EvaluationContext* ec,
76 State* state,
77 std::string* error,
78 bool* result) const override;
Gilad Arnold0adbc942014-05-12 10:35:43 -070079
Amin Hassani4b717432019-01-14 16:24:20 -080080 EvalStatus P2PEnabled(EvaluationContext* ec,
81 State* state,
82 std::string* error,
83 bool* result) const override;
Gilad Arnold78ecbfc2014-10-22 14:38:25 -070084
Amin Hassani4b717432019-01-14 16:24:20 -080085 EvalStatus P2PEnabledChanged(EvaluationContext* ec,
86 State* state,
87 std::string* error,
88 bool* result,
89 bool prev_result) const override;
Gilad Arnold78ecbfc2014-10-22 14:38:25 -070090
Gilad Arnoldb3b05442014-05-30 14:25:05 -070091 protected:
92 // Policy override.
Alex Vakulenko157fe302014-08-11 15:59:58 -070093 std::string PolicyName() const override { return "ChromeOSPolicy"; }
Gilad Arnoldb3b05442014-05-30 14:25:05 -070094
Alex Deymoc705cc82014-02-19 11:15:00 -080095 private:
Alex Deymo63784a52014-05-28 10:46:14 -070096 friend class UmChromeOSPolicyTest;
Alex Deymo63784a52014-05-28 10:46:14 -070097 FRIEND_TEST(UmChromeOSPolicyTest, UpdateCheckAllowedWaitsForTheTimeout);
Gilad Arnolda1eabcd2014-07-09 15:42:40 -070098 FRIEND_TEST(UmChromeOSPolicyTest, UpdateCheckAllowedWaitsForOOBE);
Alex Deymo63784a52014-05-28 10:46:14 -070099 FRIEND_TEST(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700100 UpdateCanStartNotAllowedScatteringNewWaitPeriodApplies);
Alex Deymo63784a52014-05-28 10:46:14 -0700101 FRIEND_TEST(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700102 UpdateCanStartNotAllowedScatteringPrevWaitPeriodStillApplies);
Alex Deymo63784a52014-05-28 10:46:14 -0700103 FRIEND_TEST(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700104 UpdateCanStartNotAllowedScatteringNewCountThresholdApplies);
Alex Deymo63784a52014-05-28 10:46:14 -0700105 FRIEND_TEST(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700106 UpdateCanStartNotAllowedScatteringPrevCountThresholdStillApplies);
Alex Deymo63784a52014-05-28 10:46:14 -0700107 FRIEND_TEST(UmChromeOSPolicyTest, UpdateCanStartAllowedScatteringSatisfied);
108 FRIEND_TEST(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700109 UpdateCanStartAllowedInteractivePreventsScattering);
Gilad Arnold349ac832014-10-06 14:20:28 -0700110 FRIEND_TEST(UmChromeOSPolicyTest,
Gilad Arnold78ecbfc2014-10-22 14:38:25 -0700111 UpdateCanStartAllowedP2PDownloadingBlockedDueToNumAttempts);
Gilad Arnold349ac832014-10-06 14:20:28 -0700112 FRIEND_TEST(UmChromeOSPolicyTest,
Gilad Arnold78ecbfc2014-10-22 14:38:25 -0700113 UpdateCanStartAllowedP2PDownloadingBlockedDueToAttemptsPeriod);
Adolfo Victoria94ffe132018-06-28 16:14:56 -0700114 FRIEND_TEST(UmChromeOSPolicyTest,
115 UpdateCheckAllowedNextUpdateCheckOutsideDisallowedInterval);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700116
117 // Auxiliary constant (zero by default).
118 const base::TimeDelta kZeroInterval;
Alex Deymo0d11c602014-04-23 20:12:20 -0700119
Amin Hassani186ff6a2018-02-27 11:06:03 -0800120 static const NextUpdateCheckPolicyConstants kNextUpdateCheckPolicyConstants;
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700121
Gilad Arnold349ac832014-10-06 14:20:28 -0700122 // Maximum number of times we'll allow using P2P for the same update payload.
123 static const int kMaxP2PAttempts;
124 // Maximum period of time allowed for download a payload via P2P, in seconds.
125 static const int kMaxP2PAttemptsPeriodInSeconds;
126
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700127 // A private policy for determining backoff and the download URL to use.
128 // Within |update_state|, |backoff_expiry| and |is_backoff_disabled| are used
129 // for determining whether backoff is still in effect; if not,
130 // |download_errors| is scanned past |failures_last_updated|, and a new
131 // download URL from |download_urls| is found and written to |result->url_idx|
132 // (-1 means no usable URL exists); |download_errors_max| determines the
133 // maximum number of attempts per URL, according to the Omaha response. If an
134 // update failure is identified then |result->do_increment_failures| is set to
135 // true; if backoff is enabled, a new backoff period is computed (from the
136 // time of failure) based on |num_failures|. Otherwise, backoff expiry is
137 // nullified, indicating that no backoff is in effect.
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700138 //
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700139 // If backing off but the previous backoff expiry is unchanged, returns
140 // |EvalStatus::kAskMeAgainLater|. Otherwise:
141 //
142 // * If backing off with a new expiry time, then |result->backoff_expiry| is
143 // set to this time.
144 //
145 // * Else, |result->backoff_expiry| is set to null, indicating that no backoff
146 // is in effect.
147 //
148 // In any of these cases, returns |EvalStatus::kSucceeded|. If an error
149 // occurred, returns |EvalStatus::kFailed|.
150 EvalStatus UpdateBackoffAndDownloadUrl(
Amin Hassani4b717432019-01-14 16:24:20 -0800151 EvaluationContext* ec,
152 State* state,
153 std::string* error,
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700154 UpdateBackoffAndDownloadUrlResult* result,
155 const UpdateState& update_state) const;
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700156
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700157 // A private policy for checking whether scattering is due. Writes in |result|
158 // the decision as to whether or not to scatter; a wallclock-based scatter
159 // wait period, which ranges from zero (do not wait) and no greater than the
160 // current scatter factor provided by the device policy (if available) or the
161 // maximum wait period determined by Omaha; and an update check-based
162 // threshold between zero (no threshold) and the maximum number determined by
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700163 // the update engine. Within |update_state|, |scatter_wait_period| should
164 // contain the last scattering period returned by this function, or zero if no
165 // wait period is known; |scatter_check_threshold| is the last update check
166 // threshold, or zero if no such threshold is known. If not scattering, or if
167 // any of the scattering values has changed, returns |EvalStatus::kSucceeded|;
168 // otherwise, |EvalStatus::kAskMeAgainLater|.
Amin Hassani4b717432019-01-14 16:24:20 -0800169 EvalStatus UpdateScattering(EvaluationContext* ec,
170 State* state,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700171 std::string* error,
172 UpdateScatteringResult* result,
173 const UpdateState& update_state) const;
174
Alex Deymoc705cc82014-02-19 11:15:00 -0800175 DISALLOW_COPY_AND_ASSIGN(ChromeOSPolicy);
176};
177
Alex Deymo63784a52014-05-28 10:46:14 -0700178} // namespace chromeos_update_manager
Alex Deymoc705cc82014-02-19 11:15:00 -0800179
Gilad Arnold48415f12014-06-27 07:10:58 -0700180#endif // UPDATE_ENGINE_UPDATE_MANAGER_CHROMEOS_POLICY_H_