Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 1 | // 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 Arnold | 48415f1 | 2014-06-27 07:10:58 -0700 | [diff] [blame] | 5 | #ifndef UPDATE_ENGINE_UPDATE_MANAGER_CHROMEOS_POLICY_H_ |
| 6 | #define UPDATE_ENGINE_UPDATE_MANAGER_CHROMEOS_POLICY_H_ |
| 7 | |
| 8 | #include <string> |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 9 | |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 10 | #include <base/time/time.h> |
Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 11 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
| 12 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 13 | #include "update_engine/update_manager/policy.h" |
| 14 | #include "update_engine/update_manager/prng.h" |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 15 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 16 | namespace chromeos_update_manager { |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 17 | |
Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame^] | 18 | // Output information from UpdateBackoffAndDownloadUrl. |
| 19 | struct UpdateBackoffAndDownloadUrlResult { |
| 20 | // Whether the failed attempt count (maintained by the caller) needs to be |
| 21 | // incremented. |
| 22 | bool do_increment_failures; |
| 23 | // The current backoff expiry. Null if backoff is not in effect. |
| 24 | base::Time backoff_expiry; |
| 25 | // The new URL index to use and number of download errors associated with it. |
| 26 | // Significant iff |do_increment_failures| is false and |backoff_expiry| is |
| 27 | // null. Negative value means no usable URL was found. |
Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 28 | int url_idx; |
Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame^] | 29 | int url_num_errors; |
Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 30 | }; |
| 31 | |
Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame^] | 32 | // Parameters for update scattering, as returned by UpdateScattering. |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 33 | struct UpdateScatteringResult { |
| 34 | bool is_scattering; |
| 35 | base::TimeDelta wait_period; |
| 36 | int check_threshold; |
| 37 | }; |
| 38 | |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 39 | // ChromeOSPolicy implements the policy-related logic used in ChromeOS. |
| 40 | class ChromeOSPolicy : public Policy { |
| 41 | public: |
| 42 | ChromeOSPolicy() {} |
| 43 | virtual ~ChromeOSPolicy() {} |
| 44 | |
| 45 | // Policy overrides. |
Alex Vakulenko | 157fe30 | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 46 | EvalStatus UpdateCheckAllowed( |
Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 47 | EvaluationContext* ec, State* state, std::string* error, |
| 48 | UpdateCheckParams* result) const override; |
Gilad Arnold | af2f6ae | 2014-04-28 14:14:52 -0700 | [diff] [blame] | 49 | |
Alex Vakulenko | 157fe30 | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 50 | EvalStatus UpdateCanStart( |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 51 | EvaluationContext* ec, |
| 52 | State* state, |
| 53 | std::string* error, |
Gilad Arnold | 42f253b | 2014-06-25 12:39:17 -0700 | [diff] [blame] | 54 | UpdateDownloadParams* result, |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 55 | const UpdateState& update_state) const override; |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 56 | |
Alex Vakulenko | 157fe30 | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 57 | EvalStatus UpdateDownloadAllowed( |
Gilad Arnold | 0adbc94 | 2014-05-12 10:35:43 -0700 | [diff] [blame] | 58 | EvaluationContext* ec, |
| 59 | State* state, |
| 60 | std::string* error, |
| 61 | bool* result) const override; |
| 62 | |
Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 63 | protected: |
| 64 | // Policy override. |
Alex Vakulenko | 157fe30 | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 65 | std::string PolicyName() const override { return "ChromeOSPolicy"; } |
Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 66 | |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 67 | private: |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 68 | friend class UmChromeOSPolicyTest; |
| 69 | FRIEND_TEST(UmChromeOSPolicyTest, |
Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 70 | FirstCheckIsAtMostInitialIntervalAfterStart); |
Gilad Arnold | 38b1402 | 2014-07-09 12:45:56 -0700 | [diff] [blame] | 71 | FRIEND_TEST(UmChromeOSPolicyTest, RecurringCheckBaseIntervalAndFuzz); |
| 72 | FRIEND_TEST(UmChromeOSPolicyTest, RecurringCheckBackoffIntervalAndFuzz); |
Gilad Arnold | a0258a5 | 2014-07-10 16:21:19 -0700 | [diff] [blame] | 73 | FRIEND_TEST(UmChromeOSPolicyTest, RecurringCheckServerDictatedPollInterval); |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 74 | FRIEND_TEST(UmChromeOSPolicyTest, ExponentialBackoffIsCapped); |
| 75 | FRIEND_TEST(UmChromeOSPolicyTest, UpdateCheckAllowedWaitsForTheTimeout); |
Gilad Arnold | a1eabcd | 2014-07-09 15:42:40 -0700 | [diff] [blame] | 76 | FRIEND_TEST(UmChromeOSPolicyTest, UpdateCheckAllowedWaitsForOOBE); |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 77 | FRIEND_TEST(UmChromeOSPolicyTest, |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 78 | UpdateCanStartNotAllowedScatteringNewWaitPeriodApplies); |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 79 | FRIEND_TEST(UmChromeOSPolicyTest, |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 80 | UpdateCanStartNotAllowedScatteringPrevWaitPeriodStillApplies); |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 81 | FRIEND_TEST(UmChromeOSPolicyTest, |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 82 | UpdateCanStartNotAllowedScatteringNewCountThresholdApplies); |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 83 | FRIEND_TEST(UmChromeOSPolicyTest, |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 84 | UpdateCanStartNotAllowedScatteringPrevCountThresholdStillApplies); |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 85 | FRIEND_TEST(UmChromeOSPolicyTest, UpdateCanStartAllowedScatteringSatisfied); |
| 86 | FRIEND_TEST(UmChromeOSPolicyTest, |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 87 | UpdateCanStartAllowedInteractivePreventsScattering); |
| 88 | |
| 89 | // Auxiliary constant (zero by default). |
| 90 | const base::TimeDelta kZeroInterval; |
Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 91 | |
| 92 | // Default update check timeout interval/fuzz values used to compute the |
| 93 | // NextUpdateCheckTime(), in seconds. Actual fuzz is within +/- half of the |
| 94 | // indicated value. |
Gilad Arnold | a2e8eaa | 2014-09-24 13:12:33 -0700 | [diff] [blame] | 95 | static const int kTimeoutInitialInterval; |
| 96 | static const int kTimeoutPeriodicInterval; |
| 97 | static const int kTimeoutMaxBackoffInterval; |
| 98 | static const int kTimeoutRegularFuzz; |
Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 99 | |
Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame^] | 100 | // Maximum update attempt backoff interval and fuzz. |
| 101 | static const int kAttemptBackoffMaxIntervalInDays; |
| 102 | static const int kAttemptBackoffFuzzInHours; |
| 103 | |
Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 104 | // A private policy implementation returning the wallclock timestamp when |
| 105 | // the next update check should happen. |
Gilad Arnold | a65fced | 2014-07-23 09:01:31 -0700 | [diff] [blame] | 106 | // TODO(garnold) We should probably change that to infer a monotonic |
| 107 | // timestamp, which will make the update check intervals more resilient to |
| 108 | // clock skews. Might require switching some of the variables exported by the |
| 109 | // UpdaterProvider to report monotonic time, as well. |
Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 110 | EvalStatus NextUpdateCheckTime(EvaluationContext* ec, State* state, |
| 111 | std::string* error, |
| 112 | base::Time* next_update_check) const; |
| 113 | |
| 114 | // Returns a TimeDelta based on the provided |interval| seconds +/- half |
| 115 | // |fuzz| seconds. The return value is guaranteed to be a non-negative |
| 116 | // TimeDelta. |
| 117 | static base::TimeDelta FuzzedInterval(PRNG* prng, int interval, int fuzz); |
| 118 | |
Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame^] | 119 | // A private policy for determining backoff and the download URL to use. |
| 120 | // Within |update_state|, |backoff_expiry| and |is_backoff_disabled| are used |
| 121 | // for determining whether backoff is still in effect; if not, |
| 122 | // |download_errors| is scanned past |failures_last_updated|, and a new |
| 123 | // download URL from |download_urls| is found and written to |result->url_idx| |
| 124 | // (-1 means no usable URL exists); |download_errors_max| determines the |
| 125 | // maximum number of attempts per URL, according to the Omaha response. If an |
| 126 | // update failure is identified then |result->do_increment_failures| is set to |
| 127 | // true; if backoff is enabled, a new backoff period is computed (from the |
| 128 | // time of failure) based on |num_failures|. Otherwise, backoff expiry is |
| 129 | // nullified, indicating that no backoff is in effect. |
Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 130 | // |
Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame^] | 131 | // If backing off but the previous backoff expiry is unchanged, returns |
| 132 | // |EvalStatus::kAskMeAgainLater|. Otherwise: |
| 133 | // |
| 134 | // * If backing off with a new expiry time, then |result->backoff_expiry| is |
| 135 | // set to this time. |
| 136 | // |
| 137 | // * Else, |result->backoff_expiry| is set to null, indicating that no backoff |
| 138 | // is in effect. |
| 139 | // |
| 140 | // In any of these cases, returns |EvalStatus::kSucceeded|. If an error |
| 141 | // occurred, returns |EvalStatus::kFailed|. |
| 142 | EvalStatus UpdateBackoffAndDownloadUrl( |
| 143 | EvaluationContext* ec, State* state, std::string* error, |
| 144 | UpdateBackoffAndDownloadUrlResult* result, |
| 145 | const UpdateState& update_state) const; |
Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 146 | |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 147 | // A private policy for checking whether scattering is due. Writes in |result| |
| 148 | // the decision as to whether or not to scatter; a wallclock-based scatter |
| 149 | // wait period, which ranges from zero (do not wait) and no greater than the |
| 150 | // current scatter factor provided by the device policy (if available) or the |
| 151 | // maximum wait period determined by Omaha; and an update check-based |
| 152 | // threshold between zero (no threshold) and the maximum number determined by |
Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 153 | // the update engine. Within |update_state|, |scatter_wait_period| should |
| 154 | // contain the last scattering period returned by this function, or zero if no |
| 155 | // wait period is known; |scatter_check_threshold| is the last update check |
| 156 | // threshold, or zero if no such threshold is known. If not scattering, or if |
| 157 | // any of the scattering values has changed, returns |EvalStatus::kSucceeded|; |
| 158 | // otherwise, |EvalStatus::kAskMeAgainLater|. |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 159 | EvalStatus UpdateScattering(EvaluationContext* ec, State* state, |
| 160 | std::string* error, |
| 161 | UpdateScatteringResult* result, |
| 162 | const UpdateState& update_state) const; |
| 163 | |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 164 | DISALLOW_COPY_AND_ASSIGN(ChromeOSPolicy); |
| 165 | }; |
| 166 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 167 | } // namespace chromeos_update_manager |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 168 | |
Gilad Arnold | 48415f1 | 2014-06-27 07:10:58 -0700 | [diff] [blame] | 169 | #endif // UPDATE_ENGINE_UPDATE_MANAGER_CHROMEOS_POLICY_H_ |