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 | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 18 | // Parameters for update download URL, as determined by UpdateDownloadUrl. |
| 19 | struct UpdateDownloadUrlResult { |
| 20 | int url_idx; |
| 21 | int url_num_failures; |
| 22 | }; |
| 23 | |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 24 | // Parameters for update scattering, as determined by UpdateNotScattering. |
| 25 | struct UpdateScatteringResult { |
| 26 | bool is_scattering; |
| 27 | base::TimeDelta wait_period; |
| 28 | int check_threshold; |
| 29 | }; |
| 30 | |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 31 | // ChromeOSPolicy implements the policy-related logic used in ChromeOS. |
| 32 | class ChromeOSPolicy : public Policy { |
| 33 | public: |
| 34 | ChromeOSPolicy() {} |
| 35 | virtual ~ChromeOSPolicy() {} |
| 36 | |
| 37 | // Policy overrides. |
Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 38 | virtual EvalStatus UpdateCheckAllowed( |
| 39 | EvaluationContext* ec, State* state, std::string* error, |
| 40 | UpdateCheckParams* result) const override; |
Gilad Arnold | af2f6ae | 2014-04-28 14:14:52 -0700 | [diff] [blame] | 41 | |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 42 | virtual EvalStatus UpdateCanStart( |
| 43 | EvaluationContext* ec, |
| 44 | State* state, |
| 45 | std::string* error, |
Gilad Arnold | 42f253b | 2014-06-25 12:39:17 -0700 | [diff] [blame] | 46 | UpdateDownloadParams* result, |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 47 | const bool interactive, |
| 48 | const UpdateState& update_state) const override; |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 49 | |
Gilad Arnold | 684219d | 2014-07-07 14:54:57 -0700 | [diff] [blame] | 50 | virtual EvalStatus UpdateDownloadAllowed( |
Gilad Arnold | 0adbc94 | 2014-05-12 10:35:43 -0700 | [diff] [blame] | 51 | EvaluationContext* ec, |
| 52 | State* state, |
| 53 | std::string* error, |
| 54 | bool* result) const override; |
| 55 | |
Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 56 | protected: |
| 57 | // Policy override. |
| 58 | virtual std::string PolicyName() const override { |
| 59 | return "ChromeOSPolicy"; |
| 60 | } |
| 61 | |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 62 | private: |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 63 | friend class UmChromeOSPolicyTest; |
| 64 | FRIEND_TEST(UmChromeOSPolicyTest, |
Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 65 | FirstCheckIsAtMostInitialIntervalAfterStart); |
Gilad Arnold | 38b1402 | 2014-07-09 12:45:56 -0700 | [diff] [blame] | 66 | FRIEND_TEST(UmChromeOSPolicyTest, RecurringCheckBaseIntervalAndFuzz); |
| 67 | FRIEND_TEST(UmChromeOSPolicyTest, RecurringCheckBackoffIntervalAndFuzz); |
Gilad Arnold | a0258a5 | 2014-07-10 16:21:19 -0700 | [diff] [blame] | 68 | FRIEND_TEST(UmChromeOSPolicyTest, RecurringCheckServerDictatedPollInterval); |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 69 | FRIEND_TEST(UmChromeOSPolicyTest, ExponentialBackoffIsCapped); |
| 70 | FRIEND_TEST(UmChromeOSPolicyTest, UpdateCheckAllowedWaitsForTheTimeout); |
Gilad Arnold | a1eabcd | 2014-07-09 15:42:40 -0700 | [diff] [blame] | 71 | FRIEND_TEST(UmChromeOSPolicyTest, UpdateCheckAllowedWaitsForOOBE); |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 72 | FRIEND_TEST(UmChromeOSPolicyTest, |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 73 | UpdateCanStartNotAllowedScatteringNewWaitPeriodApplies); |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 74 | FRIEND_TEST(UmChromeOSPolicyTest, |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 75 | UpdateCanStartNotAllowedScatteringPrevWaitPeriodStillApplies); |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 76 | FRIEND_TEST(UmChromeOSPolicyTest, |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 77 | UpdateCanStartNotAllowedScatteringNewCountThresholdApplies); |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 78 | FRIEND_TEST(UmChromeOSPolicyTest, |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 79 | UpdateCanStartNotAllowedScatteringPrevCountThresholdStillApplies); |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 80 | FRIEND_TEST(UmChromeOSPolicyTest, UpdateCanStartAllowedScatteringSatisfied); |
| 81 | FRIEND_TEST(UmChromeOSPolicyTest, |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 82 | UpdateCanStartAllowedInteractivePreventsScattering); |
| 83 | |
| 84 | // Auxiliary constant (zero by default). |
| 85 | const base::TimeDelta kZeroInterval; |
Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 86 | |
| 87 | // Default update check timeout interval/fuzz values used to compute the |
| 88 | // NextUpdateCheckTime(), in seconds. Actual fuzz is within +/- half of the |
| 89 | // indicated value. |
| 90 | static const int kTimeoutInitialInterval = 7 * 60; |
| 91 | static const int kTimeoutPeriodicInterval = 45 * 60; |
| 92 | static const int kTimeoutQuickInterval = 1 * 60; |
| 93 | static const int kTimeoutMaxBackoffInterval = 4 * 60 * 60; |
| 94 | static const int kTimeoutRegularFuzz = 10 * 60; |
| 95 | |
| 96 | // A private policy implementation returning the wallclock timestamp when |
| 97 | // the next update check should happen. |
Gilad Arnold | a65fced | 2014-07-23 09:01:31 -0700 | [diff] [blame^] | 98 | // TODO(garnold) We should probably change that to infer a monotonic |
| 99 | // timestamp, which will make the update check intervals more resilient to |
| 100 | // clock skews. Might require switching some of the variables exported by the |
| 101 | // UpdaterProvider to report monotonic time, as well. |
Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 102 | EvalStatus NextUpdateCheckTime(EvaluationContext* ec, State* state, |
| 103 | std::string* error, |
| 104 | base::Time* next_update_check) const; |
| 105 | |
| 106 | // Returns a TimeDelta based on the provided |interval| seconds +/- half |
| 107 | // |fuzz| seconds. The return value is guaranteed to be a non-negative |
| 108 | // TimeDelta. |
| 109 | static base::TimeDelta FuzzedInterval(PRNG* prng, int interval, int fuzz); |
| 110 | |
Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 111 | // A private policy for determining which download URL to use. Within |
| 112 | // |update_state|, |download_urls| should contain the download URLs as listed |
| 113 | // in the Omaha response; |download_failures_max| the maximum number of |
| 114 | // failures per URL allowed per the response; |download_url_idx| the index of |
| 115 | // the previously used URL; |download_url_num_failures| the previously known |
| 116 | // number of failures associated with that URL; and |download_url_error_codes| |
| 117 | // the list of failures occurring since the latest evaluation. |
| 118 | // |
Alex Vakulenko | 072359c | 2014-07-18 11:41:07 -0700 | [diff] [blame] | 119 | // Upon successfully deciding a URL to use, returns |EvalStatus::kSucceeded| |
| 120 | // and writes the current URL index and the number of failures associated with |
| 121 | // it in |result|. Otherwise, returns |EvalStatus::kFailed|. |
Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 122 | EvalStatus UpdateDownloadUrl(EvaluationContext* ec, State* state, |
| 123 | std::string* error, |
| 124 | UpdateDownloadUrlResult* result, |
| 125 | const UpdateState& update_state) const; |
| 126 | |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 127 | // A private policy for checking whether scattering is due. Writes in |result| |
| 128 | // the decision as to whether or not to scatter; a wallclock-based scatter |
| 129 | // wait period, which ranges from zero (do not wait) and no greater than the |
| 130 | // current scatter factor provided by the device policy (if available) or the |
| 131 | // maximum wait period determined by Omaha; and an update check-based |
| 132 | // threshold between zero (no threshold) and the maximum number determined by |
Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 133 | // the update engine. Within |update_state|, |scatter_wait_period| should |
| 134 | // contain the last scattering period returned by this function, or zero if no |
| 135 | // wait period is known; |scatter_check_threshold| is the last update check |
| 136 | // threshold, or zero if no such threshold is known. If not scattering, or if |
| 137 | // any of the scattering values has changed, returns |EvalStatus::kSucceeded|; |
| 138 | // otherwise, |EvalStatus::kAskMeAgainLater|. |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 139 | EvalStatus UpdateScattering(EvaluationContext* ec, State* state, |
| 140 | std::string* error, |
| 141 | UpdateScatteringResult* result, |
| 142 | const UpdateState& update_state) const; |
| 143 | |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 144 | DISALLOW_COPY_AND_ASSIGN(ChromeOSPolicy); |
| 145 | }; |
| 146 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 147 | } // namespace chromeos_update_manager |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 148 | |
Gilad Arnold | 48415f1 | 2014-06-27 07:10:58 -0700 | [diff] [blame] | 149 | #endif // UPDATE_ENGINE_UPDATE_MANAGER_CHROMEOS_POLICY_H_ |