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 | |
Amin Hassani | 186ff6a | 2018-02-27 11:06:03 -0800 | [diff] [blame] | 24 | #include "update_engine/update_manager/next_update_check_policy_impl.h" |
| 25 | #include "update_engine/update_manager/policy_utils.h" |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 26 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 27 | namespace chromeos_update_manager { |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 28 | |
Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 29 | // Output information from UpdateBackoffAndDownloadUrl. |
| 30 | struct UpdateBackoffAndDownloadUrlResult { |
| 31 | // Whether the failed attempt count (maintained by the caller) needs to be |
| 32 | // incremented. |
| 33 | bool do_increment_failures; |
| 34 | // The current backoff expiry. Null if backoff is not in effect. |
| 35 | base::Time backoff_expiry; |
| 36 | // The new URL index to use and number of download errors associated with it. |
| 37 | // Significant iff |do_increment_failures| is false and |backoff_expiry| is |
| 38 | // null. Negative value means no usable URL was found. |
Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 39 | int url_idx; |
Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 40 | int url_num_errors; |
Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 41 | }; |
| 42 | |
Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 43 | // Parameters for update scattering, as returned by UpdateScattering. |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 44 | struct UpdateScatteringResult { |
| 45 | bool is_scattering; |
| 46 | base::TimeDelta wait_period; |
| 47 | int check_threshold; |
| 48 | }; |
| 49 | |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 50 | // ChromeOSPolicy implements the policy-related logic used in ChromeOS. |
| 51 | class ChromeOSPolicy : public Policy { |
| 52 | public: |
| 53 | ChromeOSPolicy() {} |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 54 | ~ChromeOSPolicy() override {} |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 55 | |
| 56 | // Policy overrides. |
Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 57 | EvalStatus UpdateCheckAllowed(EvaluationContext* ec, |
| 58 | State* state, |
| 59 | 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 | |
Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 69 | EvalStatus UpdateCanStart(EvaluationContext* ec, |
| 70 | State* state, |
| 71 | std::string* error, |
| 72 | UpdateDownloadParams* result, |
| 73 | UpdateState update_state) const override; |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 74 | |
Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 75 | EvalStatus P2PEnabled(EvaluationContext* ec, |
| 76 | State* state, |
| 77 | std::string* error, |
| 78 | bool* result) const override; |
Gilad Arnold | 78ecbfc | 2014-10-22 14:38:25 -0700 | [diff] [blame] | 79 | |
Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 80 | EvalStatus P2PEnabledChanged(EvaluationContext* ec, |
| 81 | State* state, |
| 82 | std::string* error, |
| 83 | bool* result, |
| 84 | bool prev_result) const override; |
Gilad Arnold | 78ecbfc | 2014-10-22 14:38:25 -0700 | [diff] [blame] | 85 | |
Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 86 | protected: |
| 87 | // Policy override. |
Alex Vakulenko | 157fe30 | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 88 | std::string PolicyName() const override { return "ChromeOSPolicy"; } |
Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 89 | |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 90 | private: |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 91 | friend class UmChromeOSPolicyTest; |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 92 | FRIEND_TEST(UmChromeOSPolicyTest, UpdateCheckAllowedWaitsForTheTimeout); |
Gilad Arnold | a1eabcd | 2014-07-09 15:42:40 -0700 | [diff] [blame] | 93 | FRIEND_TEST(UmChromeOSPolicyTest, UpdateCheckAllowedWaitsForOOBE); |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 94 | FRIEND_TEST(UmChromeOSPolicyTest, |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 95 | UpdateCanStartNotAllowedScatteringNewWaitPeriodApplies); |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 96 | FRIEND_TEST(UmChromeOSPolicyTest, |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 97 | UpdateCanStartNotAllowedScatteringPrevWaitPeriodStillApplies); |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 98 | FRIEND_TEST(UmChromeOSPolicyTest, |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 99 | UpdateCanStartNotAllowedScatteringNewCountThresholdApplies); |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 100 | FRIEND_TEST(UmChromeOSPolicyTest, |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 101 | UpdateCanStartNotAllowedScatteringPrevCountThresholdStillApplies); |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 102 | FRIEND_TEST(UmChromeOSPolicyTest, UpdateCanStartAllowedScatteringSatisfied); |
| 103 | FRIEND_TEST(UmChromeOSPolicyTest, |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 104 | UpdateCanStartAllowedInteractivePreventsScattering); |
Gilad Arnold | 349ac83 | 2014-10-06 14:20:28 -0700 | [diff] [blame] | 105 | FRIEND_TEST(UmChromeOSPolicyTest, |
Gilad Arnold | 78ecbfc | 2014-10-22 14:38:25 -0700 | [diff] [blame] | 106 | UpdateCanStartAllowedP2PDownloadingBlockedDueToNumAttempts); |
Gilad Arnold | 349ac83 | 2014-10-06 14:20:28 -0700 | [diff] [blame] | 107 | FRIEND_TEST(UmChromeOSPolicyTest, |
Gilad Arnold | 78ecbfc | 2014-10-22 14:38:25 -0700 | [diff] [blame] | 108 | UpdateCanStartAllowedP2PDownloadingBlockedDueToAttemptsPeriod); |
Adolfo Victoria | 94ffe13 | 2018-06-28 16:14:56 -0700 | [diff] [blame] | 109 | FRIEND_TEST(UmChromeOSPolicyTest, |
| 110 | UpdateCheckAllowedNextUpdateCheckOutsideDisallowedInterval); |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 111 | |
| 112 | // Auxiliary constant (zero by default). |
| 113 | const base::TimeDelta kZeroInterval; |
Alex Deymo | 0d11c60 | 2014-04-23 20:12:20 -0700 | [diff] [blame] | 114 | |
Amin Hassani | 186ff6a | 2018-02-27 11:06:03 -0800 | [diff] [blame] | 115 | static const NextUpdateCheckPolicyConstants kNextUpdateCheckPolicyConstants; |
Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 116 | |
Gilad Arnold | 349ac83 | 2014-10-06 14:20:28 -0700 | [diff] [blame] | 117 | // Maximum number of times we'll allow using P2P for the same update payload. |
| 118 | static const int kMaxP2PAttempts; |
| 119 | // Maximum period of time allowed for download a payload via P2P, in seconds. |
| 120 | static const int kMaxP2PAttemptsPeriodInSeconds; |
| 121 | |
Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 122 | // A private policy for determining backoff and the download URL to use. |
| 123 | // Within |update_state|, |backoff_expiry| and |is_backoff_disabled| are used |
| 124 | // for determining whether backoff is still in effect; if not, |
| 125 | // |download_errors| is scanned past |failures_last_updated|, and a new |
| 126 | // download URL from |download_urls| is found and written to |result->url_idx| |
| 127 | // (-1 means no usable URL exists); |download_errors_max| determines the |
| 128 | // maximum number of attempts per URL, according to the Omaha response. If an |
| 129 | // update failure is identified then |result->do_increment_failures| is set to |
| 130 | // true; if backoff is enabled, a new backoff period is computed (from the |
| 131 | // time of failure) based on |num_failures|. Otherwise, backoff expiry is |
| 132 | // nullified, indicating that no backoff is in effect. |
Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 133 | // |
Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 134 | // If backing off but the previous backoff expiry is unchanged, returns |
| 135 | // |EvalStatus::kAskMeAgainLater|. Otherwise: |
| 136 | // |
| 137 | // * If backing off with a new expiry time, then |result->backoff_expiry| is |
| 138 | // set to this time. |
| 139 | // |
| 140 | // * Else, |result->backoff_expiry| is set to null, indicating that no backoff |
| 141 | // is in effect. |
| 142 | // |
| 143 | // In any of these cases, returns |EvalStatus::kSucceeded|. If an error |
| 144 | // occurred, returns |EvalStatus::kFailed|. |
| 145 | EvalStatus UpdateBackoffAndDownloadUrl( |
Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 146 | EvaluationContext* ec, |
| 147 | State* state, |
| 148 | std::string* error, |
Gilad Arnold | dc4bb26 | 2014-07-23 10:45:19 -0700 | [diff] [blame] | 149 | UpdateBackoffAndDownloadUrlResult* result, |
| 150 | const UpdateState& update_state) const; |
Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 151 | |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 152 | // A private policy for checking whether scattering is due. Writes in |result| |
| 153 | // the decision as to whether or not to scatter; a wallclock-based scatter |
| 154 | // wait period, which ranges from zero (do not wait) and no greater than the |
| 155 | // current scatter factor provided by the device policy (if available) or the |
| 156 | // maximum wait period determined by Omaha; and an update check-based |
| 157 | // threshold between zero (no threshold) and the maximum number determined by |
Gilad Arnold | b3b0544 | 2014-05-30 14:25:05 -0700 | [diff] [blame] | 158 | // the update engine. Within |update_state|, |scatter_wait_period| should |
| 159 | // contain the last scattering period returned by this function, or zero if no |
| 160 | // wait period is known; |scatter_check_threshold| is the last update check |
| 161 | // threshold, or zero if no such threshold is known. If not scattering, or if |
| 162 | // any of the scattering values has changed, returns |EvalStatus::kSucceeded|; |
| 163 | // otherwise, |EvalStatus::kAskMeAgainLater|. |
Amin Hassani | 4b71743 | 2019-01-14 16:24:20 -0800 | [diff] [blame] | 164 | EvalStatus UpdateScattering(EvaluationContext* ec, |
| 165 | State* state, |
Gilad Arnold | f62a4b8 | 2014-05-01 07:41:07 -0700 | [diff] [blame] | 166 | std::string* error, |
| 167 | UpdateScatteringResult* result, |
| 168 | const UpdateState& update_state) const; |
| 169 | |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 170 | DISALLOW_COPY_AND_ASSIGN(ChromeOSPolicy); |
| 171 | }; |
| 172 | |
Alex Deymo | 63784a5 | 2014-05-28 10:46:14 -0700 | [diff] [blame] | 173 | } // namespace chromeos_update_manager |
Alex Deymo | c705cc8 | 2014-02-19 11:15:00 -0800 | [diff] [blame] | 174 | |
Gilad Arnold | 48415f1 | 2014-06-27 07:10:58 -0700 | [diff] [blame] | 175 | #endif // UPDATE_ENGINE_UPDATE_MANAGER_CHROMEOS_POLICY_H_ |