blob: 17f2c29dc8994084ac79a5fcfced9e7e83a7e4c9 [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() {}
43 virtual ~ChromeOSPolicy() {}
44
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 Arnoldb3b05442014-05-30 14:25:05 -070063 protected:
64 // Policy override.
Alex Vakulenko157fe302014-08-11 15:59:58 -070065 std::string PolicyName() const override { return "ChromeOSPolicy"; }
Gilad Arnoldb3b05442014-05-30 14:25:05 -070066
Alex Deymoc705cc82014-02-19 11:15:00 -080067 private:
Alex Deymo63784a52014-05-28 10:46:14 -070068 friend class UmChromeOSPolicyTest;
69 FRIEND_TEST(UmChromeOSPolicyTest,
Alex Deymo0d11c602014-04-23 20:12:20 -070070 FirstCheckIsAtMostInitialIntervalAfterStart);
Gilad Arnold38b14022014-07-09 12:45:56 -070071 FRIEND_TEST(UmChromeOSPolicyTest, RecurringCheckBaseIntervalAndFuzz);
72 FRIEND_TEST(UmChromeOSPolicyTest, RecurringCheckBackoffIntervalAndFuzz);
Gilad Arnolda0258a52014-07-10 16:21:19 -070073 FRIEND_TEST(UmChromeOSPolicyTest, RecurringCheckServerDictatedPollInterval);
Alex Deymo63784a52014-05-28 10:46:14 -070074 FRIEND_TEST(UmChromeOSPolicyTest, ExponentialBackoffIsCapped);
75 FRIEND_TEST(UmChromeOSPolicyTest, UpdateCheckAllowedWaitsForTheTimeout);
Gilad Arnolda1eabcd2014-07-09 15:42:40 -070076 FRIEND_TEST(UmChromeOSPolicyTest, UpdateCheckAllowedWaitsForOOBE);
Alex Deymo63784a52014-05-28 10:46:14 -070077 FRIEND_TEST(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070078 UpdateCanStartNotAllowedScatteringNewWaitPeriodApplies);
Alex Deymo63784a52014-05-28 10:46:14 -070079 FRIEND_TEST(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070080 UpdateCanStartNotAllowedScatteringPrevWaitPeriodStillApplies);
Alex Deymo63784a52014-05-28 10:46:14 -070081 FRIEND_TEST(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070082 UpdateCanStartNotAllowedScatteringNewCountThresholdApplies);
Alex Deymo63784a52014-05-28 10:46:14 -070083 FRIEND_TEST(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070084 UpdateCanStartNotAllowedScatteringPrevCountThresholdStillApplies);
Alex Deymo63784a52014-05-28 10:46:14 -070085 FRIEND_TEST(UmChromeOSPolicyTest, UpdateCanStartAllowedScatteringSatisfied);
86 FRIEND_TEST(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070087 UpdateCanStartAllowedInteractivePreventsScattering);
88
89 // Auxiliary constant (zero by default).
90 const base::TimeDelta kZeroInterval;
Alex Deymo0d11c602014-04-23 20:12:20 -070091
92 // Default update check timeout interval/fuzz values used to compute the
93 // NextUpdateCheckTime(), in seconds. Actual fuzz is within +/- half of the
94 // indicated value.
Gilad Arnolda2e8eaa2014-09-24 13:12:33 -070095 static const int kTimeoutInitialInterval;
96 static const int kTimeoutPeriodicInterval;
97 static const int kTimeoutMaxBackoffInterval;
98 static const int kTimeoutRegularFuzz;
Alex Deymo0d11c602014-04-23 20:12:20 -070099
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700100 // Maximum update attempt backoff interval and fuzz.
101 static const int kAttemptBackoffMaxIntervalInDays;
102 static const int kAttemptBackoffFuzzInHours;
103
Alex Deymo0d11c602014-04-23 20:12:20 -0700104 // A private policy implementation returning the wallclock timestamp when
105 // the next update check should happen.
Gilad Arnolda65fced2014-07-23 09:01:31 -0700106 // TODO(garnold) We should probably change that to infer a monotonic
107 // timestamp, which will make the update check intervals more resilient to
108 // clock skews. Might require switching some of the variables exported by the
109 // UpdaterProvider to report monotonic time, as well.
Alex Deymo0d11c602014-04-23 20:12:20 -0700110 EvalStatus NextUpdateCheckTime(EvaluationContext* ec, State* state,
111 std::string* error,
112 base::Time* next_update_check) const;
113
114 // Returns a TimeDelta based on the provided |interval| seconds +/- half
115 // |fuzz| seconds. The return value is guaranteed to be a non-negative
116 // TimeDelta.
117 static base::TimeDelta FuzzedInterval(PRNG* prng, int interval, int fuzz);
118
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700119 // A private policy for determining backoff and the download URL to use.
120 // Within |update_state|, |backoff_expiry| and |is_backoff_disabled| are used
121 // for determining whether backoff is still in effect; if not,
122 // |download_errors| is scanned past |failures_last_updated|, and a new
123 // download URL from |download_urls| is found and written to |result->url_idx|
124 // (-1 means no usable URL exists); |download_errors_max| determines the
125 // maximum number of attempts per URL, according to the Omaha response. If an
126 // update failure is identified then |result->do_increment_failures| is set to
127 // true; if backoff is enabled, a new backoff period is computed (from the
128 // time of failure) based on |num_failures|. Otherwise, backoff expiry is
129 // nullified, indicating that no backoff is in effect.
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700130 //
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700131 // If backing off but the previous backoff expiry is unchanged, returns
132 // |EvalStatus::kAskMeAgainLater|. Otherwise:
133 //
134 // * If backing off with a new expiry time, then |result->backoff_expiry| is
135 // set to this time.
136 //
137 // * Else, |result->backoff_expiry| is set to null, indicating that no backoff
138 // is in effect.
139 //
140 // In any of these cases, returns |EvalStatus::kSucceeded|. If an error
141 // occurred, returns |EvalStatus::kFailed|.
142 EvalStatus UpdateBackoffAndDownloadUrl(
143 EvaluationContext* ec, State* state, std::string* error,
144 UpdateBackoffAndDownloadUrlResult* result,
145 const UpdateState& update_state) const;
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700146
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700147 // A private policy for checking whether scattering is due. Writes in |result|
148 // the decision as to whether or not to scatter; a wallclock-based scatter
149 // wait period, which ranges from zero (do not wait) and no greater than the
150 // current scatter factor provided by the device policy (if available) or the
151 // maximum wait period determined by Omaha; and an update check-based
152 // threshold between zero (no threshold) and the maximum number determined by
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700153 // the update engine. Within |update_state|, |scatter_wait_period| should
154 // contain the last scattering period returned by this function, or zero if no
155 // wait period is known; |scatter_check_threshold| is the last update check
156 // threshold, or zero if no such threshold is known. If not scattering, or if
157 // any of the scattering values has changed, returns |EvalStatus::kSucceeded|;
158 // otherwise, |EvalStatus::kAskMeAgainLater|.
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700159 EvalStatus UpdateScattering(EvaluationContext* ec, State* state,
160 std::string* error,
161 UpdateScatteringResult* result,
162 const UpdateState& update_state) const;
163
Alex Deymoc705cc82014-02-19 11:15:00 -0800164 DISALLOW_COPY_AND_ASSIGN(ChromeOSPolicy);
165};
166
Alex Deymo63784a52014-05-28 10:46:14 -0700167} // namespace chromeos_update_manager
Alex Deymoc705cc82014-02-19 11:15:00 -0800168
Gilad Arnold48415f12014-06-27 07:10:58 -0700169#endif // UPDATE_ENGINE_UPDATE_MANAGER_CHROMEOS_POLICY_H_