blob: b4370c4393a347d93c8656f4c04b08e43768562c [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#include <gtest/gtest_prod.h> // for FRIEND_TEST
24
Alex Deymo63784a52014-05-28 10:46:14 -070025#include "update_engine/update_manager/policy.h"
26#include "update_engine/update_manager/prng.h"
Alex Deymoc705cc82014-02-19 11:15:00 -080027
Alex Deymo63784a52014-05-28 10:46:14 -070028namespace chromeos_update_manager {
Alex Deymoc705cc82014-02-19 11:15:00 -080029
Gilad Arnolddc4bb262014-07-23 10:45:19 -070030// Output information from UpdateBackoffAndDownloadUrl.
31struct UpdateBackoffAndDownloadUrlResult {
32 // Whether the failed attempt count (maintained by the caller) needs to be
33 // incremented.
34 bool do_increment_failures;
35 // The current backoff expiry. Null if backoff is not in effect.
36 base::Time backoff_expiry;
37 // The new URL index to use and number of download errors associated with it.
38 // Significant iff |do_increment_failures| is false and |backoff_expiry| is
39 // null. Negative value means no usable URL was found.
Gilad Arnoldb3b05442014-05-30 14:25:05 -070040 int url_idx;
Gilad Arnolddc4bb262014-07-23 10:45:19 -070041 int url_num_errors;
Gilad Arnoldb3b05442014-05-30 14:25:05 -070042};
43
Gilad Arnolddc4bb262014-07-23 10:45:19 -070044// Parameters for update scattering, as returned by UpdateScattering.
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070045struct UpdateScatteringResult {
46 bool is_scattering;
47 base::TimeDelta wait_period;
48 int check_threshold;
49};
50
Alex Deymoc705cc82014-02-19 11:15:00 -080051// ChromeOSPolicy implements the policy-related logic used in ChromeOS.
52class ChromeOSPolicy : public Policy {
53 public:
54 ChromeOSPolicy() {}
Alex Deymo610277e2014-11-11 21:18:11 -080055 ~ChromeOSPolicy() override {}
Alex Deymoc705cc82014-02-19 11:15:00 -080056
57 // Policy overrides.
Alex Vakulenko157fe302014-08-11 15:59:58 -070058 EvalStatus UpdateCheckAllowed(
Alex Deymo0d11c602014-04-23 20:12:20 -070059 EvaluationContext* ec, State* state, std::string* error,
60 UpdateCheckParams* result) const override;
Gilad Arnoldaf2f6ae2014-04-28 14:14:52 -070061
Alex Vakulenko157fe302014-08-11 15:59:58 -070062 EvalStatus UpdateCanStart(
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070063 EvaluationContext* ec,
64 State* state,
65 std::string* error,
Gilad Arnold42f253b2014-06-25 12:39:17 -070066 UpdateDownloadParams* result,
Gilad Arnoldd78caf92014-09-24 09:28:14 -070067 UpdateState update_state) const override;
Alex Deymoc705cc82014-02-19 11:15:00 -080068
Alex Vakulenko157fe302014-08-11 15:59:58 -070069 EvalStatus UpdateDownloadAllowed(
Gilad Arnold0adbc942014-05-12 10:35:43 -070070 EvaluationContext* ec,
71 State* state,
72 std::string* error,
73 bool* result) const override;
74
Gilad Arnold78ecbfc2014-10-22 14:38:25 -070075 EvalStatus P2PEnabled(
76 EvaluationContext* ec,
77 State* state,
78 std::string* error,
79 bool* result) const override;
80
81 EvalStatus P2PEnabledChanged(
82 EvaluationContext* ec,
83 State* state,
84 std::string* error,
85 bool* result,
86 bool prev_result) const override;
87
Gilad Arnoldb3b05442014-05-30 14:25:05 -070088 protected:
89 // Policy override.
Alex Vakulenko157fe302014-08-11 15:59:58 -070090 std::string PolicyName() const override { return "ChromeOSPolicy"; }
Gilad Arnoldb3b05442014-05-30 14:25:05 -070091
Alex Deymoc705cc82014-02-19 11:15:00 -080092 private:
Alex Deymo63784a52014-05-28 10:46:14 -070093 friend class UmChromeOSPolicyTest;
94 FRIEND_TEST(UmChromeOSPolicyTest,
Alex Deymo0d11c602014-04-23 20:12:20 -070095 FirstCheckIsAtMostInitialIntervalAfterStart);
Gilad Arnold38b14022014-07-09 12:45:56 -070096 FRIEND_TEST(UmChromeOSPolicyTest, RecurringCheckBaseIntervalAndFuzz);
97 FRIEND_TEST(UmChromeOSPolicyTest, RecurringCheckBackoffIntervalAndFuzz);
Gilad Arnolda0258a52014-07-10 16:21:19 -070098 FRIEND_TEST(UmChromeOSPolicyTest, RecurringCheckServerDictatedPollInterval);
Alex Deymo63784a52014-05-28 10:46:14 -070099 FRIEND_TEST(UmChromeOSPolicyTest, ExponentialBackoffIsCapped);
100 FRIEND_TEST(UmChromeOSPolicyTest, UpdateCheckAllowedWaitsForTheTimeout);
Gilad Arnolda1eabcd2014-07-09 15:42:40 -0700101 FRIEND_TEST(UmChromeOSPolicyTest, UpdateCheckAllowedWaitsForOOBE);
Alex Deymo63784a52014-05-28 10:46:14 -0700102 FRIEND_TEST(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700103 UpdateCanStartNotAllowedScatteringNewWaitPeriodApplies);
Alex Deymo63784a52014-05-28 10:46:14 -0700104 FRIEND_TEST(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700105 UpdateCanStartNotAllowedScatteringPrevWaitPeriodStillApplies);
Alex Deymo63784a52014-05-28 10:46:14 -0700106 FRIEND_TEST(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700107 UpdateCanStartNotAllowedScatteringNewCountThresholdApplies);
Alex Deymo63784a52014-05-28 10:46:14 -0700108 FRIEND_TEST(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700109 UpdateCanStartNotAllowedScatteringPrevCountThresholdStillApplies);
Alex Deymo63784a52014-05-28 10:46:14 -0700110 FRIEND_TEST(UmChromeOSPolicyTest, UpdateCanStartAllowedScatteringSatisfied);
111 FRIEND_TEST(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700112 UpdateCanStartAllowedInteractivePreventsScattering);
Gilad Arnold349ac832014-10-06 14:20:28 -0700113 FRIEND_TEST(UmChromeOSPolicyTest,
Gilad Arnold78ecbfc2014-10-22 14:38:25 -0700114 UpdateCanStartAllowedP2PDownloadingBlockedDueToNumAttempts);
Gilad Arnold349ac832014-10-06 14:20:28 -0700115 FRIEND_TEST(UmChromeOSPolicyTest,
Gilad Arnold78ecbfc2014-10-22 14:38:25 -0700116 UpdateCanStartAllowedP2PDownloadingBlockedDueToAttemptsPeriod);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700117
118 // Auxiliary constant (zero by default).
119 const base::TimeDelta kZeroInterval;
Alex Deymo0d11c602014-04-23 20:12:20 -0700120
121 // Default update check timeout interval/fuzz values used to compute the
122 // NextUpdateCheckTime(), in seconds. Actual fuzz is within +/- half of the
123 // indicated value.
Gilad Arnolda2e8eaa2014-09-24 13:12:33 -0700124 static const int kTimeoutInitialInterval;
125 static const int kTimeoutPeriodicInterval;
126 static const int kTimeoutMaxBackoffInterval;
127 static const int kTimeoutRegularFuzz;
Alex Deymo0d11c602014-04-23 20:12:20 -0700128
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700129 // Maximum update attempt backoff interval and fuzz.
130 static const int kAttemptBackoffMaxIntervalInDays;
131 static const int kAttemptBackoffFuzzInHours;
132
Gilad Arnold349ac832014-10-06 14:20:28 -0700133 // Maximum number of times we'll allow using P2P for the same update payload.
134 static const int kMaxP2PAttempts;
135 // Maximum period of time allowed for download a payload via P2P, in seconds.
136 static const int kMaxP2PAttemptsPeriodInSeconds;
137
Alex Deymo0d11c602014-04-23 20:12:20 -0700138 // A private policy implementation returning the wallclock timestamp when
139 // the next update check should happen.
Gilad Arnolda65fced2014-07-23 09:01:31 -0700140 // TODO(garnold) We should probably change that to infer a monotonic
141 // timestamp, which will make the update check intervals more resilient to
142 // clock skews. Might require switching some of the variables exported by the
143 // UpdaterProvider to report monotonic time, as well.
Alex Deymo0d11c602014-04-23 20:12:20 -0700144 EvalStatus NextUpdateCheckTime(EvaluationContext* ec, State* state,
145 std::string* error,
146 base::Time* next_update_check) const;
147
148 // Returns a TimeDelta based on the provided |interval| seconds +/- half
149 // |fuzz| seconds. The return value is guaranteed to be a non-negative
150 // TimeDelta.
151 static base::TimeDelta FuzzedInterval(PRNG* prng, int interval, int fuzz);
152
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700153 // A private policy for determining backoff and the download URL to use.
154 // Within |update_state|, |backoff_expiry| and |is_backoff_disabled| are used
155 // for determining whether backoff is still in effect; if not,
156 // |download_errors| is scanned past |failures_last_updated|, and a new
157 // download URL from |download_urls| is found and written to |result->url_idx|
158 // (-1 means no usable URL exists); |download_errors_max| determines the
159 // maximum number of attempts per URL, according to the Omaha response. If an
160 // update failure is identified then |result->do_increment_failures| is set to
161 // true; if backoff is enabled, a new backoff period is computed (from the
162 // time of failure) based on |num_failures|. Otherwise, backoff expiry is
163 // nullified, indicating that no backoff is in effect.
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700164 //
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700165 // If backing off but the previous backoff expiry is unchanged, returns
166 // |EvalStatus::kAskMeAgainLater|. Otherwise:
167 //
168 // * If backing off with a new expiry time, then |result->backoff_expiry| is
169 // set to this time.
170 //
171 // * Else, |result->backoff_expiry| is set to null, indicating that no backoff
172 // is in effect.
173 //
174 // In any of these cases, returns |EvalStatus::kSucceeded|. If an error
175 // occurred, returns |EvalStatus::kFailed|.
176 EvalStatus UpdateBackoffAndDownloadUrl(
177 EvaluationContext* ec, State* state, std::string* error,
178 UpdateBackoffAndDownloadUrlResult* result,
179 const UpdateState& update_state) const;
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700180
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700181 // A private policy for checking whether scattering is due. Writes in |result|
182 // the decision as to whether or not to scatter; a wallclock-based scatter
183 // wait period, which ranges from zero (do not wait) and no greater than the
184 // current scatter factor provided by the device policy (if available) or the
185 // maximum wait period determined by Omaha; and an update check-based
186 // threshold between zero (no threshold) and the maximum number determined by
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700187 // the update engine. Within |update_state|, |scatter_wait_period| should
188 // contain the last scattering period returned by this function, or zero if no
189 // wait period is known; |scatter_check_threshold| is the last update check
190 // threshold, or zero if no such threshold is known. If not scattering, or if
191 // any of the scattering values has changed, returns |EvalStatus::kSucceeded|;
192 // otherwise, |EvalStatus::kAskMeAgainLater|.
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700193 EvalStatus UpdateScattering(EvaluationContext* ec, State* state,
194 std::string* error,
195 UpdateScatteringResult* result,
196 const UpdateState& update_state) const;
197
Alex Deymoc705cc82014-02-19 11:15:00 -0800198 DISALLOW_COPY_AND_ASSIGN(ChromeOSPolicy);
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_CHROMEOS_POLICY_H_