blob: d296689ebccb616dd43f7b0fadf26437b6a63f3b [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 Arnoldb3b05442014-05-30 14:25:05 -070018// Parameters for update download URL, as determined by UpdateDownloadUrl.
19struct UpdateDownloadUrlResult {
20 int url_idx;
21 int url_num_failures;
22};
23
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070024// Parameters for update scattering, as determined by UpdateNotScattering.
25struct UpdateScatteringResult {
26 bool is_scattering;
27 base::TimeDelta wait_period;
28 int check_threshold;
29};
30
Alex Deymoc705cc82014-02-19 11:15:00 -080031// ChromeOSPolicy implements the policy-related logic used in ChromeOS.
32class ChromeOSPolicy : public Policy {
33 public:
34 ChromeOSPolicy() {}
35 virtual ~ChromeOSPolicy() {}
36
37 // Policy overrides.
Alex Deymo0d11c602014-04-23 20:12:20 -070038 virtual EvalStatus UpdateCheckAllowed(
39 EvaluationContext* ec, State* state, std::string* error,
40 UpdateCheckParams* result) const override;
Gilad Arnoldaf2f6ae2014-04-28 14:14:52 -070041
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070042 virtual EvalStatus UpdateCanStart(
43 EvaluationContext* ec,
44 State* state,
45 std::string* error,
Gilad Arnold42f253b2014-06-25 12:39:17 -070046 UpdateDownloadParams* result,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070047 const bool interactive,
48 const UpdateState& update_state) const override;
Alex Deymoc705cc82014-02-19 11:15:00 -080049
Gilad Arnold684219d2014-07-07 14:54:57 -070050 virtual EvalStatus UpdateDownloadAllowed(
Gilad Arnold0adbc942014-05-12 10:35:43 -070051 EvaluationContext* ec,
52 State* state,
53 std::string* error,
54 bool* result) const override;
55
Gilad Arnoldb3b05442014-05-30 14:25:05 -070056 protected:
57 // Policy override.
58 virtual std::string PolicyName() const override {
59 return "ChromeOSPolicy";
60 }
61
Alex Deymoc705cc82014-02-19 11:15:00 -080062 private:
Alex Deymo63784a52014-05-28 10:46:14 -070063 friend class UmChromeOSPolicyTest;
64 FRIEND_TEST(UmChromeOSPolicyTest,
Alex Deymo0d11c602014-04-23 20:12:20 -070065 FirstCheckIsAtMostInitialIntervalAfterStart);
Alex Deymo63784a52014-05-28 10:46:14 -070066 FRIEND_TEST(UmChromeOSPolicyTest, ExponentialBackoffIsCapped);
67 FRIEND_TEST(UmChromeOSPolicyTest, UpdateCheckAllowedWaitsForTheTimeout);
68 FRIEND_TEST(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070069 UpdateCanStartNotAllowedScatteringNewWaitPeriodApplies);
Alex Deymo63784a52014-05-28 10:46:14 -070070 FRIEND_TEST(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070071 UpdateCanStartNotAllowedScatteringPrevWaitPeriodStillApplies);
Alex Deymo63784a52014-05-28 10:46:14 -070072 FRIEND_TEST(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070073 UpdateCanStartNotAllowedScatteringNewCountThresholdApplies);
Alex Deymo63784a52014-05-28 10:46:14 -070074 FRIEND_TEST(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070075 UpdateCanStartNotAllowedScatteringPrevCountThresholdStillApplies);
Alex Deymo63784a52014-05-28 10:46:14 -070076 FRIEND_TEST(UmChromeOSPolicyTest, UpdateCanStartAllowedScatteringSatisfied);
77 FRIEND_TEST(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070078 UpdateCanStartAllowedInteractivePreventsScattering);
79
80 // Auxiliary constant (zero by default).
81 const base::TimeDelta kZeroInterval;
Alex Deymo0d11c602014-04-23 20:12:20 -070082
83 // Default update check timeout interval/fuzz values used to compute the
84 // NextUpdateCheckTime(), in seconds. Actual fuzz is within +/- half of the
85 // indicated value.
86 static const int kTimeoutInitialInterval = 7 * 60;
87 static const int kTimeoutPeriodicInterval = 45 * 60;
88 static const int kTimeoutQuickInterval = 1 * 60;
89 static const int kTimeoutMaxBackoffInterval = 4 * 60 * 60;
90 static const int kTimeoutRegularFuzz = 10 * 60;
91
92 // A private policy implementation returning the wallclock timestamp when
93 // the next update check should happen.
94 EvalStatus NextUpdateCheckTime(EvaluationContext* ec, State* state,
95 std::string* error,
96 base::Time* next_update_check) const;
97
98 // Returns a TimeDelta based on the provided |interval| seconds +/- half
99 // |fuzz| seconds. The return value is guaranteed to be a non-negative
100 // TimeDelta.
101 static base::TimeDelta FuzzedInterval(PRNG* prng, int interval, int fuzz);
102
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700103 // A private policy for determining which download URL to use. Within
104 // |update_state|, |download_urls| should contain the download URLs as listed
105 // in the Omaha response; |download_failures_max| the maximum number of
106 // failures per URL allowed per the response; |download_url_idx| the index of
107 // the previously used URL; |download_url_num_failures| the previously known
108 // number of failures associated with that URL; and |download_url_error_codes|
109 // the list of failures occurring since the latest evaluation.
110 //
111 // Upon successly deciding a URL to use, returns |EvalStatus::kSucceeded| and
112 // writes the current URL index and the number of failures associated with it
113 // in |result|. Otherwise, returns |EvalStatus::kFailed|.
114 EvalStatus UpdateDownloadUrl(EvaluationContext* ec, State* state,
115 std::string* error,
116 UpdateDownloadUrlResult* result,
117 const UpdateState& update_state) const;
118
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700119 // A private policy for checking whether scattering is due. Writes in |result|
120 // the decision as to whether or not to scatter; a wallclock-based scatter
121 // wait period, which ranges from zero (do not wait) and no greater than the
122 // current scatter factor provided by the device policy (if available) or the
123 // maximum wait period determined by Omaha; and an update check-based
124 // threshold between zero (no threshold) and the maximum number determined by
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700125 // the update engine. Within |update_state|, |scatter_wait_period| should
126 // contain the last scattering period returned by this function, or zero if no
127 // wait period is known; |scatter_check_threshold| is the last update check
128 // threshold, or zero if no such threshold is known. If not scattering, or if
129 // any of the scattering values has changed, returns |EvalStatus::kSucceeded|;
130 // otherwise, |EvalStatus::kAskMeAgainLater|.
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700131 EvalStatus UpdateScattering(EvaluationContext* ec, State* state,
132 std::string* error,
133 UpdateScatteringResult* result,
134 const UpdateState& update_state) const;
135
Alex Deymoc705cc82014-02-19 11:15:00 -0800136 DISALLOW_COPY_AND_ASSIGN(ChromeOSPolicy);
137};
138
Alex Deymo63784a52014-05-28 10:46:14 -0700139} // namespace chromeos_update_manager
Alex Deymoc705cc82014-02-19 11:15:00 -0800140
Gilad Arnold48415f12014-06-27 07:10:58 -0700141#endif // UPDATE_ENGINE_UPDATE_MANAGER_CHROMEOS_POLICY_H_