blob: 68cda65a6b3342480e6693fa12fe651eddff2ecd [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
Alex Deymo63784a52014-05-28 10:46:14 -07005#ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_UPDATE_MANAGER_CHROMEOS_POLICY_H_
6#define CHROMEOS_PLATFORM_UPDATE_ENGINE_UPDATE_MANAGER_CHROMEOS_POLICY_H_
Alex Deymoc705cc82014-02-19 11:15:00 -08007
Gilad Arnoldf62a4b82014-05-01 07:41:07 -07008#include <base/time/time.h>
Alex Deymo0d11c602014-04-23 20:12:20 -07009#include <gtest/gtest_prod.h> // for FRIEND_TEST
10
Alex Deymo63784a52014-05-28 10:46:14 -070011#include "update_engine/update_manager/policy.h"
12#include "update_engine/update_manager/prng.h"
Alex Deymoc705cc82014-02-19 11:15:00 -080013
Alex Deymo63784a52014-05-28 10:46:14 -070014namespace chromeos_update_manager {
Alex Deymoc705cc82014-02-19 11:15:00 -080015
Gilad Arnoldb3b05442014-05-30 14:25:05 -070016// Parameters for update download URL, as determined by UpdateDownloadUrl.
17struct UpdateDownloadUrlResult {
18 int url_idx;
19 int url_num_failures;
20};
21
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070022// Parameters for update scattering, as determined by UpdateNotScattering.
23struct UpdateScatteringResult {
24 bool is_scattering;
25 base::TimeDelta wait_period;
26 int check_threshold;
27};
28
Alex Deymoc705cc82014-02-19 11:15:00 -080029// ChromeOSPolicy implements the policy-related logic used in ChromeOS.
30class ChromeOSPolicy : public Policy {
31 public:
32 ChromeOSPolicy() {}
33 virtual ~ChromeOSPolicy() {}
34
35 // Policy overrides.
Alex Deymo0d11c602014-04-23 20:12:20 -070036 virtual EvalStatus UpdateCheckAllowed(
37 EvaluationContext* ec, State* state, std::string* error,
38 UpdateCheckParams* result) const override;
Gilad Arnoldaf2f6ae2014-04-28 14:14:52 -070039
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070040 virtual EvalStatus UpdateCanStart(
41 EvaluationContext* ec,
42 State* state,
43 std::string* error,
44 UpdateCanStartResult* result,
45 const bool interactive,
46 const UpdateState& update_state) const override;
Alex Deymoc705cc82014-02-19 11:15:00 -080047
Gilad Arnold0adbc942014-05-12 10:35:43 -070048 virtual EvalStatus UpdateCurrentConnectionAllowed(
49 EvaluationContext* ec,
50 State* state,
51 std::string* error,
52 bool* result) const override;
53
Gilad Arnoldb3b05442014-05-30 14:25:05 -070054 protected:
55 // Policy override.
56 virtual std::string PolicyName() const override {
57 return "ChromeOSPolicy";
58 }
59
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);
Alex Deymo63784a52014-05-28 10:46:14 -070064 FRIEND_TEST(UmChromeOSPolicyTest, ExponentialBackoffIsCapped);
65 FRIEND_TEST(UmChromeOSPolicyTest, UpdateCheckAllowedWaitsForTheTimeout);
66 FRIEND_TEST(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070067 UpdateCanStartNotAllowedScatteringNewWaitPeriodApplies);
Alex Deymo63784a52014-05-28 10:46:14 -070068 FRIEND_TEST(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070069 UpdateCanStartNotAllowedScatteringPrevWaitPeriodStillApplies);
Alex Deymo63784a52014-05-28 10:46:14 -070070 FRIEND_TEST(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070071 UpdateCanStartNotAllowedScatteringNewCountThresholdApplies);
Alex Deymo63784a52014-05-28 10:46:14 -070072 FRIEND_TEST(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070073 UpdateCanStartNotAllowedScatteringPrevCountThresholdStillApplies);
Alex Deymo63784a52014-05-28 10:46:14 -070074 FRIEND_TEST(UmChromeOSPolicyTest, UpdateCanStartAllowedScatteringSatisfied);
75 FRIEND_TEST(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070076 UpdateCanStartAllowedInteractivePreventsScattering);
77
78 // Auxiliary constant (zero by default).
79 const base::TimeDelta kZeroInterval;
Alex Deymo0d11c602014-04-23 20:12:20 -070080
81 // Default update check timeout interval/fuzz values used to compute the
82 // NextUpdateCheckTime(), in seconds. Actual fuzz is within +/- half of the
83 // indicated value.
84 static const int kTimeoutInitialInterval = 7 * 60;
85 static const int kTimeoutPeriodicInterval = 45 * 60;
86 static const int kTimeoutQuickInterval = 1 * 60;
87 static const int kTimeoutMaxBackoffInterval = 4 * 60 * 60;
88 static const int kTimeoutRegularFuzz = 10 * 60;
89
90 // A private policy implementation returning the wallclock timestamp when
91 // the next update check should happen.
92 EvalStatus NextUpdateCheckTime(EvaluationContext* ec, State* state,
93 std::string* error,
94 base::Time* next_update_check) const;
95
96 // Returns a TimeDelta based on the provided |interval| seconds +/- half
97 // |fuzz| seconds. The return value is guaranteed to be a non-negative
98 // TimeDelta.
99 static base::TimeDelta FuzzedInterval(PRNG* prng, int interval, int fuzz);
100
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700101 // A private policy for determining which download URL to use. Within
102 // |update_state|, |download_urls| should contain the download URLs as listed
103 // in the Omaha response; |download_failures_max| the maximum number of
104 // failures per URL allowed per the response; |download_url_idx| the index of
105 // the previously used URL; |download_url_num_failures| the previously known
106 // number of failures associated with that URL; and |download_url_error_codes|
107 // the list of failures occurring since the latest evaluation.
108 //
109 // Upon successly deciding a URL to use, returns |EvalStatus::kSucceeded| and
110 // writes the current URL index and the number of failures associated with it
111 // in |result|. Otherwise, returns |EvalStatus::kFailed|.
112 EvalStatus UpdateDownloadUrl(EvaluationContext* ec, State* state,
113 std::string* error,
114 UpdateDownloadUrlResult* result,
115 const UpdateState& update_state) const;
116
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700117 // A private policy for checking whether scattering is due. Writes in |result|
118 // the decision as to whether or not to scatter; a wallclock-based scatter
119 // wait period, which ranges from zero (do not wait) and no greater than the
120 // current scatter factor provided by the device policy (if available) or the
121 // maximum wait period determined by Omaha; and an update check-based
122 // threshold between zero (no threshold) and the maximum number determined by
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700123 // the update engine. Within |update_state|, |scatter_wait_period| should
124 // contain the last scattering period returned by this function, or zero if no
125 // wait period is known; |scatter_check_threshold| is the last update check
126 // threshold, or zero if no such threshold is known. If not scattering, or if
127 // any of the scattering values has changed, returns |EvalStatus::kSucceeded|;
128 // otherwise, |EvalStatus::kAskMeAgainLater|.
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700129 EvalStatus UpdateScattering(EvaluationContext* ec, State* state,
130 std::string* error,
131 UpdateScatteringResult* result,
132 const UpdateState& update_state) const;
133
Alex Deymoc705cc82014-02-19 11:15:00 -0800134 DISALLOW_COPY_AND_ASSIGN(ChromeOSPolicy);
135};
136
Alex Deymo63784a52014-05-28 10:46:14 -0700137} // namespace chromeos_update_manager
Alex Deymoc705cc82014-02-19 11:15:00 -0800138
Alex Deymo63784a52014-05-28 10:46:14 -0700139#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_UPDATE_MANAGER_CHROMEOS_POLICY_H_