blob: e400cdc76afc266f5b67d39e0c112e0a09e9a2a1 [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_CHROMEOS_POLICY_H_
6#define UPDATE_ENGINE_UPDATE_MANAGER_CHROMEOS_POLICY_H_
7
8#include <string>
Alex Deymoc705cc82014-02-19 11:15:00 -08009
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070010#include <base/time/time.h>
Alex Deymo0d11c602014-04-23 20:12:20 -070011#include <gtest/gtest_prod.h> // for FRIEND_TEST
12
Alex Deymo63784a52014-05-28 10:46:14 -070013#include "update_engine/update_manager/policy.h"
14#include "update_engine/update_manager/prng.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
Gilad Arnolddc4bb262014-07-23 10:45:19 -070018// Output information from UpdateBackoffAndDownloadUrl.
19struct UpdateBackoffAndDownloadUrlResult {
20 // Whether the failed attempt count (maintained by the caller) needs to be
21 // incremented.
22 bool do_increment_failures;
23 // The current backoff expiry. Null if backoff is not in effect.
24 base::Time backoff_expiry;
25 // The new URL index to use and number of download errors associated with it.
26 // Significant iff |do_increment_failures| is false and |backoff_expiry| is
27 // null. Negative value means no usable URL was found.
Gilad Arnoldb3b05442014-05-30 14:25:05 -070028 int url_idx;
Gilad Arnolddc4bb262014-07-23 10:45:19 -070029 int url_num_errors;
Gilad Arnoldb3b05442014-05-30 14:25:05 -070030};
31
Gilad Arnolddc4bb262014-07-23 10:45:19 -070032// Parameters for update scattering, as returned by UpdateScattering.
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070033struct UpdateScatteringResult {
34 bool is_scattering;
35 base::TimeDelta wait_period;
36 int check_threshold;
37};
38
Alex Deymoc705cc82014-02-19 11:15:00 -080039// ChromeOSPolicy implements the policy-related logic used in ChromeOS.
40class ChromeOSPolicy : public Policy {
41 public:
42 ChromeOSPolicy() {}
Alex Deymo610277e2014-11-11 21:18:11 -080043 ~ChromeOSPolicy() override {}
Alex Deymoc705cc82014-02-19 11:15:00 -080044
45 // Policy overrides.
Alex Vakulenko157fe302014-08-11 15:59:58 -070046 EvalStatus UpdateCheckAllowed(
Alex Deymo0d11c602014-04-23 20:12:20 -070047 EvaluationContext* ec, State* state, std::string* error,
48 UpdateCheckParams* result) const override;
Gilad Arnoldaf2f6ae2014-04-28 14:14:52 -070049
Alex Vakulenko157fe302014-08-11 15:59:58 -070050 EvalStatus UpdateCanStart(
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070051 EvaluationContext* ec,
52 State* state,
53 std::string* error,
Gilad Arnold42f253b2014-06-25 12:39:17 -070054 UpdateDownloadParams* result,
Gilad Arnoldd78caf92014-09-24 09:28:14 -070055 UpdateState update_state) const override;
Alex Deymoc705cc82014-02-19 11:15:00 -080056
Alex Vakulenko157fe302014-08-11 15:59:58 -070057 EvalStatus UpdateDownloadAllowed(
Gilad Arnold0adbc942014-05-12 10:35:43 -070058 EvaluationContext* ec,
59 State* state,
60 std::string* error,
61 bool* result) const override;
62
Gilad Arnold78ecbfc2014-10-22 14:38:25 -070063 EvalStatus P2PEnabled(
64 EvaluationContext* ec,
65 State* state,
66 std::string* error,
67 bool* result) const override;
68
69 EvalStatus P2PEnabledChanged(
70 EvaluationContext* ec,
71 State* state,
72 std::string* error,
73 bool* result,
74 bool prev_result) const override;
75
Gilad Arnoldb3b05442014-05-30 14:25:05 -070076 protected:
77 // Policy override.
Alex Vakulenko157fe302014-08-11 15:59:58 -070078 std::string PolicyName() const override { return "ChromeOSPolicy"; }
Gilad Arnoldb3b05442014-05-30 14:25:05 -070079
Alex Deymoc705cc82014-02-19 11:15:00 -080080 private:
Alex Deymo63784a52014-05-28 10:46:14 -070081 friend class UmChromeOSPolicyTest;
82 FRIEND_TEST(UmChromeOSPolicyTest,
Alex Deymo0d11c602014-04-23 20:12:20 -070083 FirstCheckIsAtMostInitialIntervalAfterStart);
Gilad Arnold38b14022014-07-09 12:45:56 -070084 FRIEND_TEST(UmChromeOSPolicyTest, RecurringCheckBaseIntervalAndFuzz);
85 FRIEND_TEST(UmChromeOSPolicyTest, RecurringCheckBackoffIntervalAndFuzz);
Gilad Arnolda0258a52014-07-10 16:21:19 -070086 FRIEND_TEST(UmChromeOSPolicyTest, RecurringCheckServerDictatedPollInterval);
Alex Deymo63784a52014-05-28 10:46:14 -070087 FRIEND_TEST(UmChromeOSPolicyTest, ExponentialBackoffIsCapped);
88 FRIEND_TEST(UmChromeOSPolicyTest, UpdateCheckAllowedWaitsForTheTimeout);
Gilad Arnolda1eabcd2014-07-09 15:42:40 -070089 FRIEND_TEST(UmChromeOSPolicyTest, UpdateCheckAllowedWaitsForOOBE);
Alex Deymo63784a52014-05-28 10:46:14 -070090 FRIEND_TEST(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070091 UpdateCanStartNotAllowedScatteringNewWaitPeriodApplies);
Alex Deymo63784a52014-05-28 10:46:14 -070092 FRIEND_TEST(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070093 UpdateCanStartNotAllowedScatteringPrevWaitPeriodStillApplies);
Alex Deymo63784a52014-05-28 10:46:14 -070094 FRIEND_TEST(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070095 UpdateCanStartNotAllowedScatteringNewCountThresholdApplies);
Alex Deymo63784a52014-05-28 10:46:14 -070096 FRIEND_TEST(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070097 UpdateCanStartNotAllowedScatteringPrevCountThresholdStillApplies);
Alex Deymo63784a52014-05-28 10:46:14 -070098 FRIEND_TEST(UmChromeOSPolicyTest, UpdateCanStartAllowedScatteringSatisfied);
99 FRIEND_TEST(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700100 UpdateCanStartAllowedInteractivePreventsScattering);
Gilad Arnold349ac832014-10-06 14:20:28 -0700101 FRIEND_TEST(UmChromeOSPolicyTest,
Gilad Arnold78ecbfc2014-10-22 14:38:25 -0700102 UpdateCanStartAllowedP2PDownloadingBlockedDueToNumAttempts);
Gilad Arnold349ac832014-10-06 14:20:28 -0700103 FRIEND_TEST(UmChromeOSPolicyTest,
Gilad Arnold78ecbfc2014-10-22 14:38:25 -0700104 UpdateCanStartAllowedP2PDownloadingBlockedDueToAttemptsPeriod);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700105
106 // Auxiliary constant (zero by default).
107 const base::TimeDelta kZeroInterval;
Alex Deymo0d11c602014-04-23 20:12:20 -0700108
109 // Default update check timeout interval/fuzz values used to compute the
110 // NextUpdateCheckTime(), in seconds. Actual fuzz is within +/- half of the
111 // indicated value.
Gilad Arnolda2e8eaa2014-09-24 13:12:33 -0700112 static const int kTimeoutInitialInterval;
113 static const int kTimeoutPeriodicInterval;
114 static const int kTimeoutMaxBackoffInterval;
115 static const int kTimeoutRegularFuzz;
Alex Deymo0d11c602014-04-23 20:12:20 -0700116
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700117 // Maximum update attempt backoff interval and fuzz.
118 static const int kAttemptBackoffMaxIntervalInDays;
119 static const int kAttemptBackoffFuzzInHours;
120
Gilad Arnold349ac832014-10-06 14:20:28 -0700121 // Maximum number of times we'll allow using P2P for the same update payload.
122 static const int kMaxP2PAttempts;
123 // Maximum period of time allowed for download a payload via P2P, in seconds.
124 static const int kMaxP2PAttemptsPeriodInSeconds;
125
Alex Deymo0d11c602014-04-23 20:12:20 -0700126 // A private policy implementation returning the wallclock timestamp when
127 // the next update check should happen.
Gilad Arnolda65fced2014-07-23 09:01:31 -0700128 // TODO(garnold) We should probably change that to infer a monotonic
129 // timestamp, which will make the update check intervals more resilient to
130 // clock skews. Might require switching some of the variables exported by the
131 // UpdaterProvider to report monotonic time, as well.
Alex Deymo0d11c602014-04-23 20:12:20 -0700132 EvalStatus NextUpdateCheckTime(EvaluationContext* ec, State* state,
133 std::string* error,
134 base::Time* next_update_check) const;
135
136 // Returns a TimeDelta based on the provided |interval| seconds +/- half
137 // |fuzz| seconds. The return value is guaranteed to be a non-negative
138 // TimeDelta.
139 static base::TimeDelta FuzzedInterval(PRNG* prng, int interval, int fuzz);
140
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700141 // A private policy for determining backoff and the download URL to use.
142 // Within |update_state|, |backoff_expiry| and |is_backoff_disabled| are used
143 // for determining whether backoff is still in effect; if not,
144 // |download_errors| is scanned past |failures_last_updated|, and a new
145 // download URL from |download_urls| is found and written to |result->url_idx|
146 // (-1 means no usable URL exists); |download_errors_max| determines the
147 // maximum number of attempts per URL, according to the Omaha response. If an
148 // update failure is identified then |result->do_increment_failures| is set to
149 // true; if backoff is enabled, a new backoff period is computed (from the
150 // time of failure) based on |num_failures|. Otherwise, backoff expiry is
151 // nullified, indicating that no backoff is in effect.
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700152 //
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700153 // If backing off but the previous backoff expiry is unchanged, returns
154 // |EvalStatus::kAskMeAgainLater|. Otherwise:
155 //
156 // * If backing off with a new expiry time, then |result->backoff_expiry| is
157 // set to this time.
158 //
159 // * Else, |result->backoff_expiry| is set to null, indicating that no backoff
160 // is in effect.
161 //
162 // In any of these cases, returns |EvalStatus::kSucceeded|. If an error
163 // occurred, returns |EvalStatus::kFailed|.
164 EvalStatus UpdateBackoffAndDownloadUrl(
165 EvaluationContext* ec, State* state, std::string* error,
166 UpdateBackoffAndDownloadUrlResult* result,
167 const UpdateState& update_state) const;
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700168
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700169 // A private policy for checking whether scattering is due. Writes in |result|
170 // the decision as to whether or not to scatter; a wallclock-based scatter
171 // wait period, which ranges from zero (do not wait) and no greater than the
172 // current scatter factor provided by the device policy (if available) or the
173 // maximum wait period determined by Omaha; and an update check-based
174 // threshold between zero (no threshold) and the maximum number determined by
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700175 // the update engine. Within |update_state|, |scatter_wait_period| should
176 // contain the last scattering period returned by this function, or zero if no
177 // wait period is known; |scatter_check_threshold| is the last update check
178 // threshold, or zero if no such threshold is known. If not scattering, or if
179 // any of the scattering values has changed, returns |EvalStatus::kSucceeded|;
180 // otherwise, |EvalStatus::kAskMeAgainLater|.
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700181 EvalStatus UpdateScattering(EvaluationContext* ec, State* state,
182 std::string* error,
183 UpdateScatteringResult* result,
184 const UpdateState& update_state) const;
185
Alex Deymoc705cc82014-02-19 11:15:00 -0800186 DISALLOW_COPY_AND_ASSIGN(ChromeOSPolicy);
187};
188
Alex Deymo63784a52014-05-28 10:46:14 -0700189} // namespace chromeos_update_manager
Alex Deymoc705cc82014-02-19 11:15:00 -0800190
Gilad Arnold48415f12014-06-27 07:10:58 -0700191#endif // UPDATE_ENGINE_UPDATE_MANAGER_CHROMEOS_POLICY_H_