| Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // | 
|  | 2 | // Copyright (C) 2014 The Android Open Source Project | 
|  | 3 | // | 
|  | 4 | // Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | // you may not use this file except in compliance with the License. | 
|  | 6 | // You may obtain a copy of the License at | 
|  | 7 | // | 
|  | 8 | //      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | // | 
|  | 10 | // Unless required by applicable law or agreed to in writing, software | 
|  | 11 | // distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | // See the License for the specific language governing permissions and | 
|  | 14 | // limitations under the License. | 
|  | 15 | // | 
| Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 16 |  | 
| Gilad Arnold | 48415f1 | 2014-06-27 07:10:58 -0700 | [diff] [blame] | 17 | #ifndef UPDATE_ENGINE_UPDATE_MANAGER_CHROMEOS_POLICY_H_ | 
|  | 18 | #define UPDATE_ENGINE_UPDATE_MANAGER_CHROMEOS_POLICY_H_ | 
|  | 19 |  | 
|  | 20 | #include <string> | 
| Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 21 |  | 
| Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 22 | #include <base/time/time.h> | 
| Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 23 | #include <gtest/gtest_prod.h>  // for FRIEND_TEST | 
|  | 24 |  | 
| Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 25 | #include "update_engine/update_manager/policy.h" | 
|  | 26 | #include "update_engine/update_manager/prng.h" | 
| Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 27 |  | 
| Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 28 | namespace chromeos_update_manager { | 
| Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 29 |  | 
| Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 30 | // Output information from UpdateBackoffAndDownloadUrl. | 
|  | 31 | struct UpdateBackoffAndDownloadUrlResult { | 
|  | 32 | // Whether the failed attempt count (maintained by the caller) needs to be | 
|  | 33 | // incremented. | 
|  | 34 | bool do_increment_failures; | 
|  | 35 | // The current backoff expiry. Null if backoff is not in effect. | 
|  | 36 | base::Time backoff_expiry; | 
|  | 37 | // The new URL index to use and number of download errors associated with it. | 
|  | 38 | // Significant iff |do_increment_failures| is false and |backoff_expiry| is | 
|  | 39 | // null. Negative value means no usable URL was found. | 
| Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 40 | int url_idx; | 
| Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 41 | int url_num_errors; | 
| Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 42 | }; | 
|  | 43 |  | 
| Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 44 | // Parameters for update scattering, as returned by UpdateScattering. | 
| Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 45 | struct UpdateScatteringResult { | 
|  | 46 | bool is_scattering; | 
|  | 47 | base::TimeDelta wait_period; | 
|  | 48 | int check_threshold; | 
|  | 49 | }; | 
|  | 50 |  | 
| Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 51 | // ChromeOSPolicy implements the policy-related logic used in ChromeOS. | 
|  | 52 | class ChromeOSPolicy : public Policy { | 
|  | 53 | public: | 
|  | 54 | ChromeOSPolicy() {} | 
| Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 55 | ~ChromeOSPolicy() override {} | 
| Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 56 |  | 
|  | 57 | // Policy overrides. | 
| Alex Vakulenko | 157fe30 | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 58 | EvalStatus UpdateCheckAllowed( | 
| Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 59 | EvaluationContext* ec, State* state, std::string* error, | 
|  | 60 | UpdateCheckParams* result) const override; | 
| Gilad Arnold | af2f6ae | 2014-04-28 14:14:52 -0700 | [diff] [blame] | 61 |  | 
| Aaron Wood | 23bd339 | 2017-10-06 14:48:25 -0700 | [diff] [blame] | 62 | EvalStatus UpdateCanBeApplied( | 
|  | 63 | EvaluationContext* ec, | 
|  | 64 | State* state, | 
|  | 65 | std::string* error, | 
|  | 66 | chromeos_update_engine::ErrorCode* result, | 
|  | 67 | chromeos_update_engine::InstallPlan* install_plan) const override; | 
|  | 68 |  | 
| Alex Vakulenko | 157fe30 | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 69 | EvalStatus UpdateCanStart( | 
| Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 70 | EvaluationContext* ec, | 
|  | 71 | State* state, | 
|  | 72 | std::string* error, | 
| Gilad Arnold | 42f253b | 2014-06-25 12:39:17 -0700 | [diff] [blame] | 73 | UpdateDownloadParams* result, | 
| Gilad Arnold | d78caf9 | 2014-09-24 09:28:14 -0700 | [diff] [blame] | 74 | UpdateState update_state) const override; | 
| Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 75 |  | 
| Alex Vakulenko | 157fe30 | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 76 | EvalStatus UpdateDownloadAllowed( | 
| Gilad Arnold | 0adbc94 | 2014-05-12 10:35:43 -0700 | [diff] [blame] | 77 | EvaluationContext* ec, | 
|  | 78 | State* state, | 
|  | 79 | std::string* error, | 
|  | 80 | bool* result) const override; | 
|  | 81 |  | 
| Gilad Arnold | 78ecbfc | 2014-10-22 14:38:25 -0700 | [diff] [blame] | 82 | EvalStatus P2PEnabled( | 
|  | 83 | EvaluationContext* ec, | 
|  | 84 | State* state, | 
|  | 85 | std::string* error, | 
|  | 86 | bool* result) const override; | 
|  | 87 |  | 
|  | 88 | EvalStatus P2PEnabledChanged( | 
|  | 89 | EvaluationContext* ec, | 
|  | 90 | State* state, | 
|  | 91 | std::string* error, | 
|  | 92 | bool* result, | 
|  | 93 | bool prev_result) const override; | 
|  | 94 |  | 
| Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 95 | protected: | 
|  | 96 | // Policy override. | 
| Alex Vakulenko | 157fe30 | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 97 | std::string PolicyName() const override { return "ChromeOSPolicy"; } | 
| Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 98 |  | 
| Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 99 | private: | 
| Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 100 | friend class UmChromeOSPolicyTest; | 
|  | 101 | FRIEND_TEST(UmChromeOSPolicyTest, | 
| Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 102 | FirstCheckIsAtMostInitialIntervalAfterStart); | 
| Gilad Arnold | 38b1402 | 2014-07-09 12:45:56 -0700 | [diff] [blame] | 103 | FRIEND_TEST(UmChromeOSPolicyTest, RecurringCheckBaseIntervalAndFuzz); | 
|  | 104 | FRIEND_TEST(UmChromeOSPolicyTest, RecurringCheckBackoffIntervalAndFuzz); | 
| Gilad Arnold | a0258a5 | 2014-07-10 16:21:19 -0700 | [diff] [blame] | 105 | FRIEND_TEST(UmChromeOSPolicyTest, RecurringCheckServerDictatedPollInterval); | 
| Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 106 | FRIEND_TEST(UmChromeOSPolicyTest, ExponentialBackoffIsCapped); | 
|  | 107 | FRIEND_TEST(UmChromeOSPolicyTest, UpdateCheckAllowedWaitsForTheTimeout); | 
| Gilad Arnold | a1eabcd | 2014-07-09 15:42:40 -0700 | [diff] [blame] | 108 | FRIEND_TEST(UmChromeOSPolicyTest, UpdateCheckAllowedWaitsForOOBE); | 
| Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 109 | FRIEND_TEST(UmChromeOSPolicyTest, | 
| Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 110 | UpdateCanStartNotAllowedScatteringNewWaitPeriodApplies); | 
| Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 111 | FRIEND_TEST(UmChromeOSPolicyTest, | 
| Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 112 | UpdateCanStartNotAllowedScatteringPrevWaitPeriodStillApplies); | 
| Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 113 | FRIEND_TEST(UmChromeOSPolicyTest, | 
| Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 114 | UpdateCanStartNotAllowedScatteringNewCountThresholdApplies); | 
| Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 115 | FRIEND_TEST(UmChromeOSPolicyTest, | 
| Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 116 | UpdateCanStartNotAllowedScatteringPrevCountThresholdStillApplies); | 
| Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 117 | FRIEND_TEST(UmChromeOSPolicyTest, UpdateCanStartAllowedScatteringSatisfied); | 
|  | 118 | FRIEND_TEST(UmChromeOSPolicyTest, | 
| Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 119 | UpdateCanStartAllowedInteractivePreventsScattering); | 
| Gilad Arnold | 349ac83 | 2014-10-06 14:20:28 -0700 | [diff] [blame] | 120 | FRIEND_TEST(UmChromeOSPolicyTest, | 
| Gilad Arnold | 78ecbfc | 2014-10-22 14:38:25 -0700 | [diff] [blame] | 121 | UpdateCanStartAllowedP2PDownloadingBlockedDueToNumAttempts); | 
| Gilad Arnold | 349ac83 | 2014-10-06 14:20:28 -0700 | [diff] [blame] | 122 | FRIEND_TEST(UmChromeOSPolicyTest, | 
| Gilad Arnold | 78ecbfc | 2014-10-22 14:38:25 -0700 | [diff] [blame] | 123 | UpdateCanStartAllowedP2PDownloadingBlockedDueToAttemptsPeriod); | 
| Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 124 |  | 
|  | 125 | // Auxiliary constant (zero by default). | 
|  | 126 | const base::TimeDelta kZeroInterval; | 
| Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 127 |  | 
|  | 128 | // Default update check timeout interval/fuzz values used to compute the | 
|  | 129 | // NextUpdateCheckTime(), in seconds. Actual fuzz is within +/- half of the | 
|  | 130 | // indicated value. | 
| Gilad Arnold | a2e8eaa | 2014-09-24 13:12:33 -0700 | [diff] [blame] | 131 | static const int kTimeoutInitialInterval; | 
|  | 132 | static const int kTimeoutPeriodicInterval; | 
|  | 133 | static const int kTimeoutMaxBackoffInterval; | 
|  | 134 | static const int kTimeoutRegularFuzz; | 
| Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 135 |  | 
| Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 136 | // Maximum update attempt backoff interval and fuzz. | 
|  | 137 | static const int kAttemptBackoffMaxIntervalInDays; | 
|  | 138 | static const int kAttemptBackoffFuzzInHours; | 
|  | 139 |  | 
| Gilad Arnold | 349ac83 | 2014-10-06 14:20:28 -0700 | [diff] [blame] | 140 | // Maximum number of times we'll allow using P2P for the same update payload. | 
|  | 141 | static const int kMaxP2PAttempts; | 
|  | 142 | // Maximum period of time allowed for download a payload via P2P, in seconds. | 
|  | 143 | static const int kMaxP2PAttemptsPeriodInSeconds; | 
|  | 144 |  | 
| Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 145 | // A private policy implementation returning the wallclock timestamp when | 
|  | 146 | // the next update check should happen. | 
| Gilad Arnold | a65fced | 2014-07-23 09:01:31 -0700 | [diff] [blame] | 147 | // TODO(garnold) We should probably change that to infer a monotonic | 
|  | 148 | // timestamp, which will make the update check intervals more resilient to | 
|  | 149 | // clock skews. Might require switching some of the variables exported by the | 
|  | 150 | // UpdaterProvider to report monotonic time, as well. | 
| Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 151 | EvalStatus NextUpdateCheckTime(EvaluationContext* ec, State* state, | 
|  | 152 | std::string* error, | 
|  | 153 | base::Time* next_update_check) const; | 
|  | 154 |  | 
|  | 155 | // Returns a TimeDelta based on the provided |interval| seconds +/- half | 
|  | 156 | // |fuzz| seconds. The return value is guaranteed to be a non-negative | 
|  | 157 | // TimeDelta. | 
|  | 158 | static base::TimeDelta FuzzedInterval(PRNG* prng, int interval, int fuzz); | 
|  | 159 |  | 
| Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 160 | // A private policy for determining backoff and the download URL to use. | 
|  | 161 | // Within |update_state|, |backoff_expiry| and |is_backoff_disabled| are used | 
|  | 162 | // for determining whether backoff is still in effect; if not, | 
|  | 163 | // |download_errors| is scanned past |failures_last_updated|, and a new | 
|  | 164 | // download URL from |download_urls| is found and written to |result->url_idx| | 
|  | 165 | // (-1 means no usable URL exists); |download_errors_max| determines the | 
|  | 166 | // maximum number of attempts per URL, according to the Omaha response. If an | 
|  | 167 | // update failure is identified then |result->do_increment_failures| is set to | 
|  | 168 | // true; if backoff is enabled, a new backoff period is computed (from the | 
|  | 169 | // time of failure) based on |num_failures|. Otherwise, backoff expiry is | 
|  | 170 | // nullified, indicating that no backoff is in effect. | 
| Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 171 | // | 
| Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 172 | // If backing off but the previous backoff expiry is unchanged, returns | 
|  | 173 | // |EvalStatus::kAskMeAgainLater|. Otherwise: | 
|  | 174 | // | 
|  | 175 | // * If backing off with a new expiry time, then |result->backoff_expiry| is | 
|  | 176 | //   set to this time. | 
|  | 177 | // | 
|  | 178 | // * Else, |result->backoff_expiry| is set to null, indicating that no backoff | 
|  | 179 | //   is in effect. | 
|  | 180 | // | 
|  | 181 | // In any of these cases, returns |EvalStatus::kSucceeded|. If an error | 
|  | 182 | // occurred, returns |EvalStatus::kFailed|. | 
|  | 183 | EvalStatus UpdateBackoffAndDownloadUrl( | 
|  | 184 | EvaluationContext* ec, State* state, std::string* error, | 
|  | 185 | UpdateBackoffAndDownloadUrlResult* result, | 
|  | 186 | const UpdateState& update_state) const; | 
| Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 187 |  | 
| Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 188 | // A private policy for checking whether scattering is due. Writes in |result| | 
|  | 189 | // the decision as to whether or not to scatter; a wallclock-based scatter | 
|  | 190 | // wait period, which ranges from zero (do not wait) and no greater than the | 
|  | 191 | // current scatter factor provided by the device policy (if available) or the | 
|  | 192 | // maximum wait period determined by Omaha; and an update check-based | 
|  | 193 | // threshold between zero (no threshold) and the maximum number determined by | 
| Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 194 | // the update engine. Within |update_state|, |scatter_wait_period| should | 
|  | 195 | // contain the last scattering period returned by this function, or zero if no | 
|  | 196 | // wait period is known; |scatter_check_threshold| is the last update check | 
|  | 197 | // threshold, or zero if no such threshold is known. If not scattering, or if | 
|  | 198 | // any of the scattering values has changed, returns |EvalStatus::kSucceeded|; | 
|  | 199 | // otherwise, |EvalStatus::kAskMeAgainLater|. | 
| Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 200 | EvalStatus UpdateScattering(EvaluationContext* ec, State* state, | 
|  | 201 | std::string* error, | 
|  | 202 | UpdateScatteringResult* result, | 
|  | 203 | const UpdateState& update_state) const; | 
|  | 204 |  | 
| Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 205 | DISALLOW_COPY_AND_ASSIGN(ChromeOSPolicy); | 
|  | 206 | }; | 
|  | 207 |  | 
| Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 208 | }  // namespace chromeos_update_manager | 
| Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 209 |  | 
| Gilad Arnold | 48415f1 | 2014-06-27 07:10:58 -0700 | [diff] [blame] | 210 | #endif  // UPDATE_ENGINE_UPDATE_MANAGER_CHROMEOS_POLICY_H_ |