blob: 846bf57b01aee4e00a0d1de61cb6ef5caaaf7ef0 [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 Vakulenko157fe302014-08-11 15:59:58 -070038 EvalStatus UpdateCheckAllowed(
Alex Deymo0d11c602014-04-23 20:12:20 -070039 EvaluationContext* ec, State* state, std::string* error,
40 UpdateCheckParams* result) const override;
Gilad Arnoldaf2f6ae2014-04-28 14:14:52 -070041
Alex Vakulenko157fe302014-08-11 15:59:58 -070042 EvalStatus UpdateCanStart(
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070043 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
Alex Vakulenko157fe302014-08-11 15:59:58 -070050 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.
Alex Vakulenko157fe302014-08-11 15:59:58 -070058 std::string PolicyName() const override { return "ChromeOSPolicy"; }
Gilad Arnoldb3b05442014-05-30 14:25:05 -070059
Alex Deymoc705cc82014-02-19 11:15:00 -080060 private:
Alex Deymo63784a52014-05-28 10:46:14 -070061 friend class UmChromeOSPolicyTest;
62 FRIEND_TEST(UmChromeOSPolicyTest,
Alex Deymo0d11c602014-04-23 20:12:20 -070063 FirstCheckIsAtMostInitialIntervalAfterStart);
Gilad Arnold38b14022014-07-09 12:45:56 -070064 FRIEND_TEST(UmChromeOSPolicyTest, RecurringCheckBaseIntervalAndFuzz);
65 FRIEND_TEST(UmChromeOSPolicyTest, RecurringCheckBackoffIntervalAndFuzz);
Gilad Arnolda0258a52014-07-10 16:21:19 -070066 FRIEND_TEST(UmChromeOSPolicyTest, RecurringCheckServerDictatedPollInterval);
Alex Deymo63784a52014-05-28 10:46:14 -070067 FRIEND_TEST(UmChromeOSPolicyTest, ExponentialBackoffIsCapped);
68 FRIEND_TEST(UmChromeOSPolicyTest, UpdateCheckAllowedWaitsForTheTimeout);
Gilad Arnolda1eabcd2014-07-09 15:42:40 -070069 FRIEND_TEST(UmChromeOSPolicyTest, UpdateCheckAllowedWaitsForOOBE);
Alex Deymo63784a52014-05-28 10:46:14 -070070 FRIEND_TEST(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070071 UpdateCanStartNotAllowedScatteringNewWaitPeriodApplies);
Alex Deymo63784a52014-05-28 10:46:14 -070072 FRIEND_TEST(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070073 UpdateCanStartNotAllowedScatteringPrevWaitPeriodStillApplies);
Alex Deymo63784a52014-05-28 10:46:14 -070074 FRIEND_TEST(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070075 UpdateCanStartNotAllowedScatteringNewCountThresholdApplies);
Alex Deymo63784a52014-05-28 10:46:14 -070076 FRIEND_TEST(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070077 UpdateCanStartNotAllowedScatteringPrevCountThresholdStillApplies);
Alex Deymo63784a52014-05-28 10:46:14 -070078 FRIEND_TEST(UmChromeOSPolicyTest, UpdateCanStartAllowedScatteringSatisfied);
79 FRIEND_TEST(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070080 UpdateCanStartAllowedInteractivePreventsScattering);
81
82 // Auxiliary constant (zero by default).
83 const base::TimeDelta kZeroInterval;
Alex Deymo0d11c602014-04-23 20:12:20 -070084
85 // Default update check timeout interval/fuzz values used to compute the
86 // NextUpdateCheckTime(), in seconds. Actual fuzz is within +/- half of the
87 // indicated value.
Gilad Arnolda2e8eaa2014-09-24 13:12:33 -070088 static const int kTimeoutInitialInterval;
89 static const int kTimeoutPeriodicInterval;
90 static const int kTimeoutMaxBackoffInterval;
91 static const int kTimeoutRegularFuzz;
Alex Deymo0d11c602014-04-23 20:12:20 -070092
93 // A private policy implementation returning the wallclock timestamp when
94 // the next update check should happen.
Gilad Arnolda65fced2014-07-23 09:01:31 -070095 // TODO(garnold) We should probably change that to infer a monotonic
96 // timestamp, which will make the update check intervals more resilient to
97 // clock skews. Might require switching some of the variables exported by the
98 // UpdaterProvider to report monotonic time, as well.
Alex Deymo0d11c602014-04-23 20:12:20 -070099 EvalStatus NextUpdateCheckTime(EvaluationContext* ec, State* state,
100 std::string* error,
101 base::Time* next_update_check) const;
102
103 // Returns a TimeDelta based on the provided |interval| seconds +/- half
104 // |fuzz| seconds. The return value is guaranteed to be a non-negative
105 // TimeDelta.
106 static base::TimeDelta FuzzedInterval(PRNG* prng, int interval, int fuzz);
107
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700108 // A private policy for determining which download URL to use. Within
109 // |update_state|, |download_urls| should contain the download URLs as listed
110 // in the Omaha response; |download_failures_max| the maximum number of
111 // failures per URL allowed per the response; |download_url_idx| the index of
112 // the previously used URL; |download_url_num_failures| the previously known
113 // number of failures associated with that URL; and |download_url_error_codes|
114 // the list of failures occurring since the latest evaluation.
115 //
Alex Vakulenko072359c2014-07-18 11:41:07 -0700116 // Upon successfully deciding a URL to use, returns |EvalStatus::kSucceeded|
117 // and writes the current URL index and the number of failures associated with
118 // it in |result|. Otherwise, returns |EvalStatus::kFailed|.
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700119 EvalStatus UpdateDownloadUrl(EvaluationContext* ec, State* state,
120 std::string* error,
121 UpdateDownloadUrlResult* result,
122 const UpdateState& update_state) const;
123
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700124 // A private policy for checking whether scattering is due. Writes in |result|
125 // the decision as to whether or not to scatter; a wallclock-based scatter
126 // wait period, which ranges from zero (do not wait) and no greater than the
127 // current scatter factor provided by the device policy (if available) or the
128 // maximum wait period determined by Omaha; and an update check-based
129 // threshold between zero (no threshold) and the maximum number determined by
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700130 // the update engine. Within |update_state|, |scatter_wait_period| should
131 // contain the last scattering period returned by this function, or zero if no
132 // wait period is known; |scatter_check_threshold| is the last update check
133 // threshold, or zero if no such threshold is known. If not scattering, or if
134 // any of the scattering values has changed, returns |EvalStatus::kSucceeded|;
135 // otherwise, |EvalStatus::kAskMeAgainLater|.
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700136 EvalStatus UpdateScattering(EvaluationContext* ec, State* state,
137 std::string* error,
138 UpdateScatteringResult* result,
139 const UpdateState& update_state) const;
140
Alex Deymoc705cc82014-02-19 11:15:00 -0800141 DISALLOW_COPY_AND_ASSIGN(ChromeOSPolicy);
142};
143
Alex Deymo63784a52014-05-28 10:46:14 -0700144} // namespace chromeos_update_manager
Alex Deymoc705cc82014-02-19 11:15:00 -0800145
Gilad Arnold48415f12014-06-27 07:10:58 -0700146#endif // UPDATE_ENGINE_UPDATE_MANAGER_CHROMEOS_POLICY_H_