blob: ee3c16cc70fab3d64aa6e7ea0d584924ac026829 [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 Arnold2cbb3852014-03-07 12:40:50 -08005#ifndef CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_CHROMEOS_POLICY_H_
6#define CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_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 Deymoc705cc82014-02-19 11:15:00 -080011#include "update_engine/policy_manager/policy.h"
Alex Deymo0d11c602014-04-23 20:12:20 -070012#include "update_engine/policy_manager/prng.h"
Alex Deymoc705cc82014-02-19 11:15:00 -080013
14namespace chromeos_policy_manager {
15
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
42 private:
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070043 friend class PmChromeOSPolicyTest;
Alex Deymo0d11c602014-04-23 20:12:20 -070044 FRIEND_TEST(PmChromeOSPolicyTest,
45 FirstCheckIsAtMostInitialIntervalAfterStart);
46 FRIEND_TEST(PmChromeOSPolicyTest, ExponentialBackoffIsCapped);
47 FRIEND_TEST(PmChromeOSPolicyTest, UpdateCheckAllowedWaitsForTheTimeout);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070048 FRIEND_TEST(PmChromeOSPolicyTest,
49 UpdateCanStartNotAllowedScatteringNewWaitPeriodApplies);
50 FRIEND_TEST(PmChromeOSPolicyTest,
51 UpdateCanStartNotAllowedScatteringPrevWaitPeriodStillApplies);
52 FRIEND_TEST(PmChromeOSPolicyTest,
53 UpdateCanStartNotAllowedScatteringNewCountThresholdApplies);
54 FRIEND_TEST(PmChromeOSPolicyTest,
55 UpdateCanStartNotAllowedScatteringPrevCountThresholdStillApplies);
56 FRIEND_TEST(PmChromeOSPolicyTest, UpdateCanStartAllowedScatteringSatisfied);
57 FRIEND_TEST(PmChromeOSPolicyTest,
58 UpdateCanStartAllowedInteractivePreventsScattering);
59
60 // Auxiliary constant (zero by default).
61 const base::TimeDelta kZeroInterval;
Alex Deymo0d11c602014-04-23 20:12:20 -070062
63 // Default update check timeout interval/fuzz values used to compute the
64 // NextUpdateCheckTime(), in seconds. Actual fuzz is within +/- half of the
65 // indicated value.
66 static const int kTimeoutInitialInterval = 7 * 60;
67 static const int kTimeoutPeriodicInterval = 45 * 60;
68 static const int kTimeoutQuickInterval = 1 * 60;
69 static const int kTimeoutMaxBackoffInterval = 4 * 60 * 60;
70 static const int kTimeoutRegularFuzz = 10 * 60;
71
72 // A private policy implementation returning the wallclock timestamp when
73 // the next update check should happen.
74 EvalStatus NextUpdateCheckTime(EvaluationContext* ec, State* state,
75 std::string* error,
76 base::Time* next_update_check) const;
77
78 // Returns a TimeDelta based on the provided |interval| seconds +/- half
79 // |fuzz| seconds. The return value is guaranteed to be a non-negative
80 // TimeDelta.
81 static base::TimeDelta FuzzedInterval(PRNG* prng, int interval, int fuzz);
82
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070083 // A private policy for checking whether scattering is due. Writes in |result|
84 // the decision as to whether or not to scatter; a wallclock-based scatter
85 // wait period, which ranges from zero (do not wait) and no greater than the
86 // current scatter factor provided by the device policy (if available) or the
87 // maximum wait period determined by Omaha; and an update check-based
88 // threshold between zero (no threshold) and the maximum number determined by
89 // the update engine. Within |update_state|, |wait_period| should contain the
90 // last scattering period returned by this function, or zero if no wait period
91 // is known; |check_threshold| is the last update check threshold, or zero if
92 // no such threshold is known. If not scattering, or if any of the scattering
93 // values has changed, returns |EvalStatus::kSucceeded|; otherwise,
94 // |EvalStatus::kAskMeAgainLater|.
95 EvalStatus UpdateScattering(EvaluationContext* ec, State* state,
96 std::string* error,
97 UpdateScatteringResult* result,
98 const UpdateState& update_state) const;
99
Alex Deymoc705cc82014-02-19 11:15:00 -0800100 DISALLOW_COPY_AND_ASSIGN(ChromeOSPolicy);
101};
102
103} // namespace chromeos_policy_manager
104
Gilad Arnold2cbb3852014-03-07 12:40:50 -0800105#endif // CHROMEOS_PLATFORM_UPDATE_ENGINE_POLICY_MANAGER_CHROMEOS_POLICY_H_