blob: 3c47c6092dff48faa7c31663ffa2099fb2f23a5e [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 Arnoldf62a4b82014-05-01 07:41:07 -070016// Parameters for update scattering, as determined by UpdateNotScattering.
17struct UpdateScatteringResult {
18 bool is_scattering;
19 base::TimeDelta wait_period;
20 int check_threshold;
21};
22
Alex Deymoc705cc82014-02-19 11:15:00 -080023// ChromeOSPolicy implements the policy-related logic used in ChromeOS.
24class ChromeOSPolicy : public Policy {
25 public:
26 ChromeOSPolicy() {}
27 virtual ~ChromeOSPolicy() {}
28
29 // Policy overrides.
Alex Deymo0d11c602014-04-23 20:12:20 -070030 virtual EvalStatus UpdateCheckAllowed(
31 EvaluationContext* ec, State* state, std::string* error,
32 UpdateCheckParams* result) const override;
Gilad Arnoldaf2f6ae2014-04-28 14:14:52 -070033
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070034 virtual EvalStatus UpdateCanStart(
35 EvaluationContext* ec,
36 State* state,
37 std::string* error,
38 UpdateCanStartResult* result,
39 const bool interactive,
40 const UpdateState& update_state) const override;
Alex Deymoc705cc82014-02-19 11:15:00 -080041
Gilad Arnold0adbc942014-05-12 10:35:43 -070042 virtual EvalStatus UpdateCurrentConnectionAllowed(
43 EvaluationContext* ec,
44 State* state,
45 std::string* error,
46 bool* result) const override;
47
Alex Deymoc705cc82014-02-19 11:15:00 -080048 private:
Alex Deymo63784a52014-05-28 10:46:14 -070049 friend class UmChromeOSPolicyTest;
50 FRIEND_TEST(UmChromeOSPolicyTest,
Alex Deymo0d11c602014-04-23 20:12:20 -070051 FirstCheckIsAtMostInitialIntervalAfterStart);
Alex Deymo63784a52014-05-28 10:46:14 -070052 FRIEND_TEST(UmChromeOSPolicyTest, ExponentialBackoffIsCapped);
53 FRIEND_TEST(UmChromeOSPolicyTest, UpdateCheckAllowedWaitsForTheTimeout);
54 FRIEND_TEST(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070055 UpdateCanStartNotAllowedScatteringNewWaitPeriodApplies);
Alex Deymo63784a52014-05-28 10:46:14 -070056 FRIEND_TEST(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070057 UpdateCanStartNotAllowedScatteringPrevWaitPeriodStillApplies);
Alex Deymo63784a52014-05-28 10:46:14 -070058 FRIEND_TEST(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070059 UpdateCanStartNotAllowedScatteringNewCountThresholdApplies);
Alex Deymo63784a52014-05-28 10:46:14 -070060 FRIEND_TEST(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070061 UpdateCanStartNotAllowedScatteringPrevCountThresholdStillApplies);
Alex Deymo63784a52014-05-28 10:46:14 -070062 FRIEND_TEST(UmChromeOSPolicyTest, UpdateCanStartAllowedScatteringSatisfied);
63 FRIEND_TEST(UmChromeOSPolicyTest,
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070064 UpdateCanStartAllowedInteractivePreventsScattering);
65
66 // Auxiliary constant (zero by default).
67 const base::TimeDelta kZeroInterval;
Alex Deymo0d11c602014-04-23 20:12:20 -070068
69 // Default update check timeout interval/fuzz values used to compute the
70 // NextUpdateCheckTime(), in seconds. Actual fuzz is within +/- half of the
71 // indicated value.
72 static const int kTimeoutInitialInterval = 7 * 60;
73 static const int kTimeoutPeriodicInterval = 45 * 60;
74 static const int kTimeoutQuickInterval = 1 * 60;
75 static const int kTimeoutMaxBackoffInterval = 4 * 60 * 60;
76 static const int kTimeoutRegularFuzz = 10 * 60;
77
78 // A private policy implementation returning the wallclock timestamp when
79 // the next update check should happen.
80 EvalStatus NextUpdateCheckTime(EvaluationContext* ec, State* state,
81 std::string* error,
82 base::Time* next_update_check) const;
83
84 // Returns a TimeDelta based on the provided |interval| seconds +/- half
85 // |fuzz| seconds. The return value is guaranteed to be a non-negative
86 // TimeDelta.
87 static base::TimeDelta FuzzedInterval(PRNG* prng, int interval, int fuzz);
88
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070089 // A private policy for checking whether scattering is due. Writes in |result|
90 // the decision as to whether or not to scatter; a wallclock-based scatter
91 // wait period, which ranges from zero (do not wait) and no greater than the
92 // current scatter factor provided by the device policy (if available) or the
93 // maximum wait period determined by Omaha; and an update check-based
94 // threshold between zero (no threshold) and the maximum number determined by
95 // the update engine. Within |update_state|, |wait_period| should contain the
96 // last scattering period returned by this function, or zero if no wait period
97 // is known; |check_threshold| is the last update check threshold, or zero if
98 // no such threshold is known. If not scattering, or if any of the scattering
99 // values has changed, returns |EvalStatus::kSucceeded|; otherwise,
100 // |EvalStatus::kAskMeAgainLater|.
101 EvalStatus UpdateScattering(EvaluationContext* ec, State* state,
102 std::string* error,
103 UpdateScatteringResult* result,
104 const UpdateState& update_state) const;
105
Alex Deymoc705cc82014-02-19 11:15:00 -0800106 DISALLOW_COPY_AND_ASSIGN(ChromeOSPolicy);
107};
108
Alex Deymo63784a52014-05-28 10:46:14 -0700109} // namespace chromeos_update_manager
Alex Deymoc705cc82014-02-19 11:15:00 -0800110
Alex Deymo63784a52014-05-28 10:46:14 -0700111#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_UPDATE_MANAGER_CHROMEOS_POLICY_H_