blob: abb06c77313d12cd5630839bb6f428c0e8744ee7 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
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 Deymoc705cc82014-02-19 11:15:00 -080016
Alex Deymo63784a52014-05-28 10:46:14 -070017#include "update_engine/update_manager/chromeos_policy.h"
Alex Deymo0d11c602014-04-23 20:12:20 -070018
Gilad Arnolde1218812014-05-07 12:21:36 -070019#include <algorithm>
Gilad Arnold0adbc942014-05-12 10:35:43 -070020#include <set>
Alex Deymoc705cc82014-02-19 11:15:00 -080021#include <string>
Amin Hassani186ff6a2018-02-27 11:06:03 -080022#include <vector>
Alex Deymoc705cc82014-02-19 11:15:00 -080023
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070024#include <base/logging.h>
Gilad Arnoldb3b05442014-05-30 14:25:05 -070025#include <base/strings/string_util.h>
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070026#include <base/time/time.h>
27
Alex Deymo39910dc2015-11-09 17:04:30 -080028#include "update_engine/common/error_code.h"
Alex Deymoe88e9fe2016-02-03 16:38:00 -080029#include "update_engine/common/error_code_utils.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080030#include "update_engine/common/utils.h"
Alex Deymo63784a52014-05-28 10:46:14 -070031#include "update_engine/update_manager/device_policy_provider.h"
Amin Hassani186ff6a2018-02-27 11:06:03 -080032#include "update_engine/update_manager/enough_slots_ab_updates_policy_impl.h"
33#include "update_engine/update_manager/enterprise_device_policy_impl.h"
34#include "update_engine/update_manager/interactive_update_policy_impl.h"
35#include "update_engine/update_manager/official_build_check_policy_impl.h"
36#include "update_engine/update_manager/out_of_box_experience_policy_impl.h"
Alex Deymo63784a52014-05-28 10:46:14 -070037#include "update_engine/update_manager/policy_utils.h"
38#include "update_engine/update_manager/shill_provider.h"
Adolfo Victoria94ffe132018-06-28 16:14:56 -070039#include "update_engine/update_manager/update_time_restrictions_policy_impl.h"
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070040
Alex Deymo0d11c602014-04-23 20:12:20 -070041using base::Time;
42using base::TimeDelta;
Sen Jiang255e22b2016-05-20 16:15:29 -070043using chromeos_update_engine::ConnectionTethering;
44using chromeos_update_engine::ConnectionType;
Gilad Arnoldb3b05442014-05-30 14:25:05 -070045using chromeos_update_engine::ErrorCode;
Aaron Wood23bd3392017-10-06 14:48:25 -070046using chromeos_update_engine::InstallPlan;
Gilad Arnolddc4bb262014-07-23 10:45:19 -070047using std::get;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -070048using std::min;
Gilad Arnold0adbc942014-05-12 10:35:43 -070049using std::set;
Alex Deymoc705cc82014-02-19 11:15:00 -080050using std::string;
Amin Hassani186ff6a2018-02-27 11:06:03 -080051using std::vector;
Alex Deymoc705cc82014-02-19 11:15:00 -080052
Gilad Arnoldb3b05442014-05-30 14:25:05 -070053namespace {
54
Gilad Arnolddc4bb262014-07-23 10:45:19 -070055// Examines |err_code| and decides whether the URL index needs to be advanced,
56// the error count for the URL incremented, or none of the above. In the first
57// case, returns true; in the second case, increments |*url_num_error_p| and
58// returns false; otherwise just returns false.
Gilad Arnoldb3b05442014-05-30 14:25:05 -070059//
60// TODO(garnold) Adapted from PayloadState::UpdateFailed() (to be retired).
Gilad Arnolddc4bb262014-07-23 10:45:19 -070061bool HandleErrorCode(ErrorCode err_code, int* url_num_error_p) {
Gilad Arnoldb3b05442014-05-30 14:25:05 -070062 err_code = chromeos_update_engine::utils::GetBaseErrorCode(err_code);
63 switch (err_code) {
64 // Errors which are good indicators of a problem with a particular URL or
65 // the protocol used in the URL or entities in the communication channel
66 // (e.g. proxies). We should try the next available URL in the next update
67 // check to quickly recover from these errors.
68 case ErrorCode::kPayloadHashMismatchError:
69 case ErrorCode::kPayloadSizeMismatchError:
70 case ErrorCode::kDownloadPayloadVerificationError:
71 case ErrorCode::kDownloadPayloadPubKeyVerificationError:
72 case ErrorCode::kSignedDeltaPayloadExpectedError:
73 case ErrorCode::kDownloadInvalidMetadataMagicString:
74 case ErrorCode::kDownloadSignatureMissingInManifest:
75 case ErrorCode::kDownloadManifestParseError:
76 case ErrorCode::kDownloadMetadataSignatureError:
77 case ErrorCode::kDownloadMetadataSignatureVerificationError:
78 case ErrorCode::kDownloadMetadataSignatureMismatch:
79 case ErrorCode::kDownloadOperationHashVerificationError:
80 case ErrorCode::kDownloadOperationExecutionError:
81 case ErrorCode::kDownloadOperationHashMismatch:
82 case ErrorCode::kDownloadInvalidMetadataSize:
83 case ErrorCode::kDownloadInvalidMetadataSignature:
84 case ErrorCode::kDownloadOperationHashMissingError:
85 case ErrorCode::kDownloadMetadataSignatureMissingError:
86 case ErrorCode::kPayloadMismatchedType:
87 case ErrorCode::kUnsupportedMajorPayloadVersion:
88 case ErrorCode::kUnsupportedMinorPayloadVersion:
89 LOG(INFO) << "Advancing download URL due to error "
Alex Deymoe88e9fe2016-02-03 16:38:00 -080090 << chromeos_update_engine::utils::ErrorCodeToString(err_code)
Gilad Arnoldb3b05442014-05-30 14:25:05 -070091 << " (" << static_cast<int>(err_code) << ")";
Gilad Arnoldb3b05442014-05-30 14:25:05 -070092 return true;
93
94 // Errors which seem to be just transient network/communication related
95 // failures and do not indicate any inherent problem with the URL itself.
96 // So, we should keep the current URL but just increment the
97 // failure count to give it more chances. This way, while we maximize our
98 // chances of downloading from the URLs that appear earlier in the response
99 // (because download from a local server URL that appears earlier in a
100 // response is preferable than downloading from the next URL which could be
Alex Vakulenko072359c2014-07-18 11:41:07 -0700101 // an Internet URL and thus could be more expensive).
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700102 case ErrorCode::kError:
103 case ErrorCode::kDownloadTransferError:
104 case ErrorCode::kDownloadWriteError:
105 case ErrorCode::kDownloadStateInitializationError:
Gilad Arnold684219d2014-07-07 14:54:57 -0700106 case ErrorCode::kOmahaErrorInHTTPResponse: // Aggregate for HTTP errors.
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700107 LOG(INFO) << "Incrementing URL failure count due to error "
Alex Deymoe88e9fe2016-02-03 16:38:00 -0800108 << chromeos_update_engine::utils::ErrorCodeToString(err_code)
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700109 << " (" << static_cast<int>(err_code) << ")";
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700110 *url_num_error_p += 1;
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700111 return false;
112
113 // Errors which are not specific to a URL and hence shouldn't result in
114 // the URL being penalized. This can happen in two cases:
115 // 1. We haven't started downloading anything: These errors don't cost us
116 // anything in terms of actual payload bytes, so we should just do the
117 // regular retries at the next update check.
118 // 2. We have successfully downloaded the payload: In this case, the
119 // payload attempt number would have been incremented and would take care
Alex Vakulenko072359c2014-07-18 11:41:07 -0700120 // of the back-off at the next update check.
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700121 // In either case, there's no need to update URL index or failure count.
122 case ErrorCode::kOmahaRequestError:
123 case ErrorCode::kOmahaResponseHandlerError:
124 case ErrorCode::kPostinstallRunnerError:
125 case ErrorCode::kFilesystemCopierError:
126 case ErrorCode::kInstallDeviceOpenError:
127 case ErrorCode::kKernelDeviceOpenError:
128 case ErrorCode::kDownloadNewPartitionInfoError:
129 case ErrorCode::kNewRootfsVerificationError:
130 case ErrorCode::kNewKernelVerificationError:
131 case ErrorCode::kPostinstallBootedFromFirmwareB:
132 case ErrorCode::kPostinstallFirmwareRONotUpdatable:
133 case ErrorCode::kOmahaRequestEmptyResponseError:
134 case ErrorCode::kOmahaRequestXMLParseError:
135 case ErrorCode::kOmahaResponseInvalid:
136 case ErrorCode::kOmahaUpdateIgnoredPerPolicy:
137 case ErrorCode::kOmahaUpdateDeferredPerPolicy:
Kevin Cernekee2494e282016-03-29 18:03:53 -0700138 case ErrorCode::kNonCriticalUpdateInOOBE:
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700139 case ErrorCode::kOmahaUpdateDeferredForBackoff:
140 case ErrorCode::kPostinstallPowerwashError:
141 case ErrorCode::kUpdateCanceledByChannelChange:
David Zeuthenf3e28012014-08-26 18:23:52 -0400142 case ErrorCode::kOmahaRequestXMLHasEntityDecl:
Allie Woodeb9e6d82015-04-17 13:55:30 -0700143 case ErrorCode::kFilesystemVerifierError:
Alex Deymo1f19dcc2016-02-03 09:22:17 -0800144 case ErrorCode::kUserCanceled:
Weidong Guo421ff332017-04-17 10:08:38 -0700145 case ErrorCode::kOmahaUpdateIgnoredOverCellular:
Sen Jiang02c49422017-10-31 15:14:11 -0700146 case ErrorCode::kUpdatedButNotActive:
Sen Jiang3978ddd2018-03-22 18:05:44 -0700147 case ErrorCode::kNoUpdate:
Marton Hunyady199152d2018-05-07 19:08:48 +0200148 case ErrorCode::kRollbackNotPossible:
Amin Hassani80f4d4c2018-05-16 13:34:00 -0700149 case ErrorCode::kFirstActiveOmahaPingSentPersistenceError:
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700150 LOG(INFO) << "Not changing URL index or failure count due to error "
Alex Deymoe88e9fe2016-02-03 16:38:00 -0800151 << chromeos_update_engine::utils::ErrorCodeToString(err_code)
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700152 << " (" << static_cast<int>(err_code) << ")";
153 return false;
154
155 case ErrorCode::kSuccess: // success code
156 case ErrorCode::kUmaReportedMax: // not an error code
157 case ErrorCode::kOmahaRequestHTTPResponseBase: // aggregated already
Alex Deymo1f19dcc2016-02-03 09:22:17 -0800158 case ErrorCode::kDevModeFlag: // not an error code
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700159 case ErrorCode::kResumedFlag: // not an error code
160 case ErrorCode::kTestImageFlag: // not an error code
161 case ErrorCode::kTestOmahaUrlFlag: // not an error code
162 case ErrorCode::kSpecialFlags: // not an error code
163 // These shouldn't happen. Enumerating these explicitly here so that we
164 // can let the compiler warn about new error codes that are added to
165 // action_processor.h but not added here.
166 LOG(WARNING) << "Unexpected error "
Alex Deymoe88e9fe2016-02-03 16:38:00 -0800167 << chromeos_update_engine::utils::ErrorCodeToString(err_code)
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700168 << " (" << static_cast<int>(err_code) << ")";
169 // Note: Not adding a default here so as to let the compiler warn us of
170 // any new enums that were added in the .h but not listed in this switch.
171 }
172 return false;
173}
174
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700175// Checks whether |url| can be used under given download restrictions.
176bool IsUrlUsable(const string& url, bool http_allowed) {
Alex Vakulenko0103c362016-01-20 07:56:15 -0800177 return http_allowed ||
178 !base::StartsWith(url, "http://",
179 base::CompareCase::INSENSITIVE_ASCII);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700180}
181
182} // namespace
183
Alex Deymo63784a52014-05-28 10:46:14 -0700184namespace chromeos_update_manager {
Alex Deymoc705cc82014-02-19 11:15:00 -0800185
Amin Hassani186ff6a2018-02-27 11:06:03 -0800186const NextUpdateCheckPolicyConstants
187 ChromeOSPolicy::kNextUpdateCheckPolicyConstants = {
188 .timeout_initial_interval = 7 * 60,
189 .timeout_periodic_interval = 45 * 60,
190 .timeout_max_backoff_interval = 4 * 60 * 60,
191 .timeout_regular_fuzz = 10 * 60,
192 .attempt_backoff_max_interval_in_days = 16,
193 .attempt_backoff_fuzz_in_hours = 12,
194};
Alex Deymo14e7dde2015-10-20 14:46:33 -0700195
Gilad Arnold349ac832014-10-06 14:20:28 -0700196const int ChromeOSPolicy::kMaxP2PAttempts = 10;
197const int ChromeOSPolicy::kMaxP2PAttemptsPeriodInSeconds = 5 * 24 * 60 * 60;
Gilad Arnolda2e8eaa2014-09-24 13:12:33 -0700198
Alex Deymo0d11c602014-04-23 20:12:20 -0700199EvalStatus ChromeOSPolicy::UpdateCheckAllowed(
200 EvaluationContext* ec, State* state, string* error,
201 UpdateCheckParams* result) const {
Gilad Arnold42f253b2014-06-25 12:39:17 -0700202 // Set the default return values.
203 result->updates_enabled = true;
204 result->target_channel.clear();
Gilad Arnoldd4b30322014-07-21 15:35:27 -0700205 result->target_version_prefix.clear();
Marton Hunyadyba51c3f2018-04-25 15:18:10 +0200206 result->rollback_allowed = false;
Marton Hunyady0e0e3542018-02-21 18:51:39 +0100207 result->rollback_allowed_milestones = -1;
Amin Hassanied37d682018-04-06 13:22:00 -0700208 result->interactive = false;
Gilad Arnold42f253b2014-06-25 12:39:17 -0700209
Amin Hassani186ff6a2018-02-27 11:06:03 -0800210 EnoughSlotsAbUpdatesPolicyImpl enough_slots_ab_updates_policy;
211 EnterpriseDevicePolicyImpl enterprise_device_policy;
212 OnlyUpdateOfficialBuildsPolicyImpl only_update_official_builds_policy;
213 InteractiveUpdatePolicyImpl interactive_update_policy;
214 OobePolicyImpl oobe_policy;
215 NextUpdateCheckTimePolicyImpl next_update_check_time_policy(
216 kNextUpdateCheckPolicyConstants);
Gilad Arnolda1eabcd2014-07-09 15:42:40 -0700217
Amin Hassani186ff6a2018-02-27 11:06:03 -0800218 vector<Policy const*> policies_to_consult = {
219 // Do not perform any updates if there are not enough slots to do A/B
220 // updates.
221 &enough_slots_ab_updates_policy,
222
223 // Check to see if Enterprise-managed (has DevicePolicy) and/or
224 // Kiosk-mode. If so, then defer to those settings.
225 &enterprise_device_policy,
226
227 // Check to see if an interactive update was requested.
228 &interactive_update_policy,
229
230 // Unofficial builds should not perform periodic update checks.
231 &only_update_official_builds_policy,
232
233 // If OOBE is enabled, wait until it is completed.
234 &oobe_policy,
235
236 // Ensure that periodic update checks are timed properly.
237 &next_update_check_time_policy,
238 };
239
240 // Now that the list of policy implementations, and the order to consult them,
241 // has been setup, consult the policies. If none of the policies make a
242 // definitive decisions about whether or not to check for updates, then allow
243 // the update check to happen.
244 EvalStatus status = ConsultPolicies(policies_to_consult,
245 &Policy::UpdateCheckAllowed,
246 ec,
247 state,
248 error,
249 result);
250 if (EvalStatus::kContinue != status) {
251 return status;
252 } else {
253 // It is time to check for an update.
254 LOG(INFO) << "Allowing update check.";
Gilad Arnoldbfc44f72014-07-09 14:41:39 -0700255 return EvalStatus::kSucceeded;
256 }
Alex Deymoc705cc82014-02-19 11:15:00 -0800257}
258
Aaron Wood23bd3392017-10-06 14:48:25 -0700259EvalStatus ChromeOSPolicy::UpdateCanBeApplied(EvaluationContext* ec,
260 State* state,
261 std::string* error,
262 ErrorCode* result,
263 InstallPlan* install_plan) const {
Adolfo Victoria0dbf1f92018-07-19 14:18:11 -0700264 UpdateTimeRestrictionsPolicyImpl update_time_restrictions_policy;
265 InteractiveUpdatePolicyImpl interactive_update_policy;
266
267 vector<Policy const*> policies_to_consult = {
268 // Check to see if an interactive update has been requested.
269 &interactive_update_policy,
270
271 // Do not apply or download an update if we are inside one of the
272 // restricted times.
273 &update_time_restrictions_policy,
274 };
275
276 EvalStatus status = ConsultPolicies(policies_to_consult,
277 &Policy::UpdateCanBeApplied,
278 ec,
279 state,
280 error,
281 result,
282 install_plan);
283 if (EvalStatus::kContinue != status) {
284 return status;
285 } else {
286 // The update can proceed.
287 LOG(INFO) << "Allowing update to be applied.";
288 *result = ErrorCode::kSuccess;
289 return EvalStatus::kSucceeded;
290 }
Aaron Wood23bd3392017-10-06 14:48:25 -0700291}
292
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700293EvalStatus ChromeOSPolicy::UpdateCanStart(
294 EvaluationContext* ec,
295 State* state,
296 string* error,
Gilad Arnold42f253b2014-06-25 12:39:17 -0700297 UpdateDownloadParams* result,
Gilad Arnoldd78caf92014-09-24 09:28:14 -0700298 const UpdateState update_state) const {
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700299 // Set the default return values. Note that we set persisted values (backoff,
300 // scattering) to the same values presented in the update state. The reason is
301 // that preemptive returns, such as the case where an update check is due,
302 // should not clear off the said values; rather, it is the deliberate
303 // inference of new values that should cause them to be reset.
Gilad Arnold14a9e702014-10-08 08:09:09 -0700304 result->update_can_start = false;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700305 result->cannot_start_reason = UpdateCannotStartReason::kUndefined;
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700306 result->download_url_idx = -1;
Gilad Arnold14a9e702014-10-08 08:09:09 -0700307 result->download_url_allowed = true;
308 result->download_url_num_errors = 0;
Gilad Arnoldb2f99192014-10-07 13:01:52 -0700309 result->p2p_downloading_allowed = false;
310 result->p2p_sharing_allowed = false;
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700311 result->do_increment_failures = false;
312 result->backoff_expiry = update_state.backoff_expiry;
313 result->scatter_wait_period = update_state.scatter_wait_period;
314 result->scatter_check_threshold = update_state.scatter_check_threshold;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700315
316 // Make sure that we're not due for an update check.
317 UpdateCheckParams check_result;
318 EvalStatus check_status = UpdateCheckAllowed(ec, state, error, &check_result);
319 if (check_status == EvalStatus::kFailed)
320 return EvalStatus::kFailed;
Gilad Arnold14a9e702014-10-08 08:09:09 -0700321 bool is_check_due = (check_status == EvalStatus::kSucceeded &&
322 check_result.updates_enabled == true);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700323
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700324 // Check whether backoff applies, and if not then which URL can be used for
325 // downloading. These require scanning the download error log, and so they are
326 // done together.
327 UpdateBackoffAndDownloadUrlResult backoff_url_result;
328 EvalStatus backoff_url_status = UpdateBackoffAndDownloadUrl(
329 ec, state, error, &backoff_url_result, update_state);
Gilad Arnold14a9e702014-10-08 08:09:09 -0700330 if (backoff_url_status == EvalStatus::kFailed)
331 return EvalStatus::kFailed;
332 result->download_url_idx = backoff_url_result.url_idx;
333 result->download_url_num_errors = backoff_url_result.url_num_errors;
334 result->do_increment_failures = backoff_url_result.do_increment_failures;
335 result->backoff_expiry = backoff_url_result.backoff_expiry;
336 bool is_backoff_active =
337 (backoff_url_status == EvalStatus::kAskMeAgainLater) ||
338 !backoff_url_result.backoff_expiry.is_null();
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700339
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700340 DevicePolicyProvider* const dp_provider = state->device_policy_provider();
Gilad Arnold14a9e702014-10-08 08:09:09 -0700341 bool is_scattering_active = false;
342 EvalStatus scattering_status = EvalStatus::kSucceeded;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700343
344 const bool* device_policy_is_loaded_p = ec->GetValue(
345 dp_provider->var_device_policy_is_loaded());
346 if (device_policy_is_loaded_p && *device_policy_is_loaded_p) {
Gilad Arnold76a11f62014-05-20 09:02:12 -0700347 // Check whether scattering applies to this update attempt. We should not be
348 // scattering if this is an interactive update check, or if OOBE is enabled
349 // but not completed.
350 //
351 // Note: current code further suppresses scattering if a "deadline"
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700352 // attribute is found in the Omaha response. However, it appears that the
Gilad Arnold76a11f62014-05-20 09:02:12 -0700353 // presence of this attribute is merely indicative of an OOBE update, during
354 // which we suppress scattering anyway.
Gilad Arnold14a9e702014-10-08 08:09:09 -0700355 bool is_scattering_applicable = false;
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700356 result->scatter_wait_period = kZeroInterval;
357 result->scatter_check_threshold = 0;
Amin Hassanied37d682018-04-06 13:22:00 -0700358 if (!update_state.interactive) {
Gilad Arnold76a11f62014-05-20 09:02:12 -0700359 const bool* is_oobe_enabled_p = ec->GetValue(
360 state->config_provider()->var_is_oobe_enabled());
361 if (is_oobe_enabled_p && !(*is_oobe_enabled_p)) {
Gilad Arnold14a9e702014-10-08 08:09:09 -0700362 is_scattering_applicable = true;
Gilad Arnold76a11f62014-05-20 09:02:12 -0700363 } else {
364 const bool* is_oobe_complete_p = ec->GetValue(
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700365 state->system_provider()->var_is_oobe_complete());
Gilad Arnold14a9e702014-10-08 08:09:09 -0700366 is_scattering_applicable = (is_oobe_complete_p && *is_oobe_complete_p);
Gilad Arnold76a11f62014-05-20 09:02:12 -0700367 }
368 }
369
370 // Compute scattering values.
Gilad Arnold14a9e702014-10-08 08:09:09 -0700371 if (is_scattering_applicable) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700372 UpdateScatteringResult scatter_result;
Gilad Arnold14a9e702014-10-08 08:09:09 -0700373 scattering_status = UpdateScattering(ec, state, error, &scatter_result,
374 update_state);
375 if (scattering_status == EvalStatus::kFailed) {
376 return EvalStatus::kFailed;
377 } else {
378 result->scatter_wait_period = scatter_result.wait_period;
379 result->scatter_check_threshold = scatter_result.check_threshold;
380 if (scattering_status == EvalStatus::kAskMeAgainLater ||
381 scatter_result.is_scattering)
382 is_scattering_active = true;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700383 }
384 }
Gilad Arnold78ecbfc2014-10-22 14:38:25 -0700385 }
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700386
Gilad Arnold78ecbfc2014-10-22 14:38:25 -0700387 // Find out whether P2P is globally enabled.
388 bool p2p_enabled;
389 EvalStatus p2p_enabled_status = P2PEnabled(ec, state, error, &p2p_enabled);
390 if (p2p_enabled_status != EvalStatus::kSucceeded)
391 return EvalStatus::kFailed;
392
393 // Is P2P is enabled, consider allowing it for downloading and/or sharing.
394 if (p2p_enabled) {
395 // Sharing via P2P is allowed if not disabled by Omaha.
396 if (update_state.p2p_sharing_disabled) {
397 LOG(INFO) << "Blocked P2P sharing because it is disabled by Omaha.";
Gilad Arnoldb2f99192014-10-07 13:01:52 -0700398 } else {
Gilad Arnold78ecbfc2014-10-22 14:38:25 -0700399 result->p2p_sharing_allowed = true;
Gilad Arnoldef8d0872014-10-03 14:14:06 -0700400 }
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700401
Gilad Arnold78ecbfc2014-10-22 14:38:25 -0700402 // Downloading via P2P is allowed if not disabled by Omaha, an update is not
403 // interactive, and other limits haven't been reached.
404 if (update_state.p2p_downloading_disabled) {
405 LOG(INFO) << "Blocked P2P downloading because it is disabled by Omaha.";
Amin Hassanied37d682018-04-06 13:22:00 -0700406 } else if (update_state.interactive) {
Gilad Arnold78ecbfc2014-10-22 14:38:25 -0700407 LOG(INFO) << "Blocked P2P downloading because update is interactive.";
Gilad Arnoldb2f99192014-10-07 13:01:52 -0700408 } else if (update_state.p2p_num_attempts >= kMaxP2PAttempts) {
Gilad Arnold78ecbfc2014-10-22 14:38:25 -0700409 LOG(INFO) << "Blocked P2P downloading as it was attempted too many "
410 "times.";
Gilad Arnoldb2f99192014-10-07 13:01:52 -0700411 } else if (!update_state.p2p_first_attempted.is_null() &&
412 ec->IsWallclockTimeGreaterThan(
413 update_state.p2p_first_attempted +
414 TimeDelta::FromSeconds(kMaxP2PAttemptsPeriodInSeconds))) {
Gilad Arnold78ecbfc2014-10-22 14:38:25 -0700415 LOG(INFO) << "Blocked P2P downloading as its usage timespan exceeds "
416 "limit.";
Gilad Arnoldb2f99192014-10-07 13:01:52 -0700417 } else {
Gilad Arnold14a9e702014-10-08 08:09:09 -0700418 // P2P download is allowed; if backoff or scattering are active, be sure
419 // to suppress them, yet prevent any download URL from being used.
Gilad Arnoldb2f99192014-10-07 13:01:52 -0700420 result->p2p_downloading_allowed = true;
Gilad Arnold14a9e702014-10-08 08:09:09 -0700421 if (is_backoff_active || is_scattering_active) {
422 is_backoff_active = is_scattering_active = false;
423 result->download_url_allowed = false;
424 }
Gilad Arnoldb2f99192014-10-07 13:01:52 -0700425 }
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700426 }
427
Gilad Arnold14a9e702014-10-08 08:09:09 -0700428 // Check for various deterrents.
429 if (is_check_due) {
430 result->cannot_start_reason = UpdateCannotStartReason::kCheckDue;
431 return EvalStatus::kSucceeded;
432 }
433 if (is_backoff_active) {
434 result->cannot_start_reason = UpdateCannotStartReason::kBackoff;
435 return backoff_url_status;
436 }
437 if (is_scattering_active) {
438 result->cannot_start_reason = UpdateCannotStartReason::kScattering;
439 return scattering_status;
440 }
Gilad Arnoldb2f99192014-10-07 13:01:52 -0700441 if (result->download_url_idx < 0 && !result->p2p_downloading_allowed) {
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700442 result->cannot_start_reason = UpdateCannotStartReason::kCannotDownload;
Gilad Arnold14a9e702014-10-08 08:09:09 -0700443 return EvalStatus::kSucceeded;
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700444 }
445
Gilad Arnold14a9e702014-10-08 08:09:09 -0700446 // Update is good to go.
447 result->update_can_start = true;
Gilad Arnoldaf2f6ae2014-04-28 14:14:52 -0700448 return EvalStatus::kSucceeded;
449}
450
Gilad Arnolda8262e22014-06-02 13:54:27 -0700451// TODO(garnold) Logic in this method is based on
452// ConnectionManager::IsUpdateAllowedOver(); be sure to deprecate the latter.
453//
454// TODO(garnold) The current logic generally treats the list of allowed
455// connections coming from the device policy as a whitelist, meaning that it
456// can only be used for enabling connections, but not disable them. Further,
457// certain connection types (like Bluetooth) cannot be enabled even by policy.
458// In effect, the only thing that device policy can change is to enable
459// updates over a cellular network (disabled by default). We may want to
460// revisit this semantics, allowing greater flexibility in defining specific
461// permissions over all types of networks.
Gilad Arnold684219d2014-07-07 14:54:57 -0700462EvalStatus ChromeOSPolicy::UpdateDownloadAllowed(
Gilad Arnolda8262e22014-06-02 13:54:27 -0700463 EvaluationContext* ec,
464 State* state,
465 string* error,
466 bool* result) const {
467 // Get the current connection type.
468 ShillProvider* const shill_provider = state->shill_provider();
469 const ConnectionType* conn_type_p = ec->GetValue(
470 shill_provider->var_conn_type());
471 POLICY_CHECK_VALUE_AND_FAIL(conn_type_p, error);
472 ConnectionType conn_type = *conn_type_p;
473
474 // If we're tethering, treat it as a cellular connection.
475 if (conn_type != ConnectionType::kCellular) {
476 const ConnectionTethering* conn_tethering_p = ec->GetValue(
477 shill_provider->var_conn_tethering());
478 POLICY_CHECK_VALUE_AND_FAIL(conn_tethering_p, error);
479 if (*conn_tethering_p == ConnectionTethering::kConfirmed)
480 conn_type = ConnectionType::kCellular;
481 }
482
483 // By default, we allow updates for all connection types, with exceptions as
484 // noted below. This also determines whether a device policy can override the
485 // default.
486 *result = true;
487 bool device_policy_can_override = false;
488 switch (conn_type) {
489 case ConnectionType::kBluetooth:
490 *result = false;
491 break;
492
493 case ConnectionType::kCellular:
494 *result = false;
495 device_policy_can_override = true;
496 break;
497
498 case ConnectionType::kUnknown:
499 if (error)
500 *error = "Unknown connection type";
501 return EvalStatus::kFailed;
502
503 default:
504 break; // Nothing to do.
505 }
506
507 // If update is allowed, we're done.
508 if (*result)
509 return EvalStatus::kSucceeded;
510
511 // Check whether the device policy specifically allows this connection.
Gilad Arnolda8262e22014-06-02 13:54:27 -0700512 if (device_policy_can_override) {
513 DevicePolicyProvider* const dp_provider = state->device_policy_provider();
514 const bool* device_policy_is_loaded_p = ec->GetValue(
515 dp_provider->var_device_policy_is_loaded());
516 if (device_policy_is_loaded_p && *device_policy_is_loaded_p) {
517 const set<ConnectionType>* allowed_conn_types_p = ec->GetValue(
518 dp_provider->var_allowed_connection_types_for_update());
519 if (allowed_conn_types_p) {
520 if (allowed_conn_types_p->count(conn_type)) {
521 *result = true;
522 return EvalStatus::kSucceeded;
523 }
Gilad Arnold28d6be62014-06-30 14:04:04 -0700524 } else if (conn_type == ConnectionType::kCellular) {
525 // Local user settings can allow updates over cellular iff a policy was
526 // loaded but no allowed connections were specified in it.
527 const bool* update_over_cellular_allowed_p = ec->GetValue(
528 state->updater_provider()->var_cellular_enabled());
529 if (update_over_cellular_allowed_p && *update_over_cellular_allowed_p)
530 *result = true;
Gilad Arnolda8262e22014-06-02 13:54:27 -0700531 }
532 }
533 }
534
Gilad Arnold28d6be62014-06-30 14:04:04 -0700535 return (*result ? EvalStatus::kSucceeded : EvalStatus::kAskMeAgainLater);
Gilad Arnolda8262e22014-06-02 13:54:27 -0700536}
537
Gilad Arnold78ecbfc2014-10-22 14:38:25 -0700538EvalStatus ChromeOSPolicy::P2PEnabled(EvaluationContext* ec,
539 State* state,
Alex Deymo39910dc2015-11-09 17:04:30 -0800540 string* error,
Gilad Arnold78ecbfc2014-10-22 14:38:25 -0700541 bool* result) const {
542 bool enabled = false;
543
544 // Determine whether use of P2P is allowed by policy. Even if P2P is not
545 // explicitly allowed, we allow it if the device is enterprise enrolled (that
546 // is, missing or empty owner string).
547 DevicePolicyProvider* const dp_provider = state->device_policy_provider();
548 const bool* device_policy_is_loaded_p = ec->GetValue(
549 dp_provider->var_device_policy_is_loaded());
550 if (device_policy_is_loaded_p && *device_policy_is_loaded_p) {
551 const bool* policy_au_p2p_enabled_p = ec->GetValue(
552 dp_provider->var_au_p2p_enabled());
553 if (policy_au_p2p_enabled_p) {
554 enabled = *policy_au_p2p_enabled_p;
555 } else {
556 const string* policy_owner_p = ec->GetValue(dp_provider->var_owner());
557 if (!policy_owner_p || policy_owner_p->empty())
558 enabled = true;
559 }
560 }
561
562 // Enable P2P, if so mandated by the updater configuration. This is additive
563 // to whether or not P2P is enabled by device policy.
564 if (!enabled) {
565 const bool* updater_p2p_enabled_p = ec->GetValue(
566 state->updater_provider()->var_p2p_enabled());
567 enabled = updater_p2p_enabled_p && *updater_p2p_enabled_p;
568 }
569
570 *result = enabled;
571 return EvalStatus::kSucceeded;
572}
573
574EvalStatus ChromeOSPolicy::P2PEnabledChanged(EvaluationContext* ec,
575 State* state,
Alex Deymo39910dc2015-11-09 17:04:30 -0800576 string* error,
Gilad Arnold78ecbfc2014-10-22 14:38:25 -0700577 bool* result,
578 bool prev_result) const {
579 EvalStatus status = P2PEnabled(ec, state, error, result);
580 if (status == EvalStatus::kSucceeded && *result == prev_result)
581 return EvalStatus::kAskMeAgainLater;
582 return status;
583}
584
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700585EvalStatus ChromeOSPolicy::UpdateBackoffAndDownloadUrl(
Alex Deymof329b932014-10-30 01:37:48 -0700586 EvaluationContext* ec, State* state, string* error,
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700587 UpdateBackoffAndDownloadUrlResult* result,
588 const UpdateState& update_state) const {
589 // Sanity checks.
590 DCHECK_GE(update_state.download_errors_max, 0);
591
592 // Set default result values.
593 result->do_increment_failures = false;
594 result->backoff_expiry = update_state.backoff_expiry;
595 result->url_idx = -1;
596 result->url_num_errors = 0;
597
598 const bool* is_official_build_p = ec->GetValue(
599 state->system_provider()->var_is_official_build());
600 bool is_official_build = (is_official_build_p ? *is_official_build_p : true);
601
602 // Check whether backoff is enabled.
603 bool may_backoff = false;
604 if (update_state.is_backoff_disabled) {
605 LOG(INFO) << "Backoff disabled by Omaha.";
Amin Hassanied37d682018-04-06 13:22:00 -0700606 } else if (update_state.interactive) {
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700607 LOG(INFO) << "No backoff for interactive updates.";
608 } else if (update_state.is_delta_payload) {
609 LOG(INFO) << "No backoff for delta payloads.";
610 } else if (!is_official_build) {
611 LOG(INFO) << "No backoff for unofficial builds.";
612 } else {
613 may_backoff = true;
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700614 }
615
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700616 // If previous backoff still in effect, block.
617 if (may_backoff && !update_state.backoff_expiry.is_null() &&
618 !ec->IsWallclockTimeGreaterThan(update_state.backoff_expiry)) {
619 LOG(INFO) << "Previous backoff has not expired, waiting.";
620 return EvalStatus::kAskMeAgainLater;
621 }
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700622
623 // Determine whether HTTP downloads are forbidden by policy. This only
624 // applies to official system builds; otherwise, HTTP is always enabled.
625 bool http_allowed = true;
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700626 if (is_official_build) {
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700627 DevicePolicyProvider* const dp_provider = state->device_policy_provider();
628 const bool* device_policy_is_loaded_p = ec->GetValue(
629 dp_provider->var_device_policy_is_loaded());
630 if (device_policy_is_loaded_p && *device_policy_is_loaded_p) {
631 const bool* policy_http_downloads_enabled_p = ec->GetValue(
632 dp_provider->var_http_downloads_enabled());
633 http_allowed = (!policy_http_downloads_enabled_p ||
634 *policy_http_downloads_enabled_p);
635 }
636 }
637
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700638 int url_idx = update_state.last_download_url_idx;
639 if (url_idx < 0)
640 url_idx = -1;
641 bool do_advance_url = false;
642 bool is_failure_occurred = false;
643 Time err_time;
644
645 // Scan the relevant part of the download error log, tracking which URLs are
646 // being used, and accounting the number of errors for each URL. Note that
647 // this process may not traverse all errors provided, as it may decide to bail
648 // out midway depending on the particular errors exhibited, the number of
649 // failures allowed, etc. When this ends, |url_idx| will point to the last URL
650 // used (-1 if starting fresh), |do_advance_url| will determine whether the
651 // URL needs to be advanced, and |err_time| the point in time when the last
652 // reported error occurred. Additionally, if the error log indicates that an
653 // update attempt has failed (abnormal), then |is_failure_occurred| will be
654 // set to true.
655 const int num_urls = update_state.download_urls.size();
656 int prev_url_idx = -1;
657 int url_num_errors = update_state.last_download_url_num_errors;
658 Time prev_err_time;
659 bool is_first = true;
660 for (const auto& err_tuple : update_state.download_errors) {
661 // Do some sanity checks.
662 int used_url_idx = get<0>(err_tuple);
663 if (is_first && url_idx >= 0 && used_url_idx != url_idx) {
664 LOG(WARNING) << "First URL in error log (" << used_url_idx
665 << ") not as expected (" << url_idx << ")";
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700666 }
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700667 is_first = false;
668 url_idx = used_url_idx;
669 if (url_idx < 0 || url_idx >= num_urls) {
670 LOG(ERROR) << "Download error log contains an invalid URL index ("
671 << url_idx << ")";
672 return EvalStatus::kFailed;
673 }
674 err_time = get<2>(err_tuple);
675 if (!(prev_err_time.is_null() || err_time >= prev_err_time)) {
676 // TODO(garnold) Monotonicity cannot really be assumed when dealing with
677 // wallclock-based timestamps. However, we're making a simplifying
678 // assumption so as to keep the policy implementation straightforward, for
679 // now. In general, we should convert all timestamp handling in the
680 // UpdateManager to use monotonic time (instead of wallclock), including
681 // the computation of various expiration times (backoff, scattering, etc).
682 // The client will do whatever conversions necessary when
683 // persisting/retrieving these values across reboots. See chromium:408794.
684 LOG(ERROR) << "Download error timestamps not monotonically increasing.";
685 return EvalStatus::kFailed;
686 }
687 prev_err_time = err_time;
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700688
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700689 // Ignore errors that happened before the last known failed attempt.
690 if (!update_state.failures_last_updated.is_null() &&
691 err_time <= update_state.failures_last_updated)
692 continue;
693
694 if (prev_url_idx >= 0) {
695 if (url_idx < prev_url_idx) {
696 LOG(ERROR) << "The URLs in the download error log have wrapped around ("
697 << prev_url_idx << "->" << url_idx
698 << "). This should not have happened and means that there's "
699 "a bug. To be conservative, we record a failed attempt "
700 "(invalidating the rest of the error log) and resume "
701 "download from the first usable URL.";
702 url_idx = -1;
703 is_failure_occurred = true;
704 break;
705 }
706
707 if (url_idx > prev_url_idx) {
708 url_num_errors = 0;
709 do_advance_url = false;
710 }
711 }
712
713 if (HandleErrorCode(get<1>(err_tuple), &url_num_errors) ||
714 url_num_errors > update_state.download_errors_max)
715 do_advance_url = true;
716
717 prev_url_idx = url_idx;
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700718 }
719
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700720 // If required, advance to the next usable URL. If the URLs wraparound, we
721 // mark an update attempt failure. Also be sure to set the download error
722 // count to zero.
723 if (url_idx < 0 || do_advance_url) {
724 url_num_errors = 0;
725 int start_url_idx = -1;
726 do {
727 if (++url_idx == num_urls) {
728 url_idx = 0;
729 // We only mark failure if an actual advancing of a URL was required.
730 if (do_advance_url)
731 is_failure_occurred = true;
732 }
733
734 if (start_url_idx < 0)
735 start_url_idx = url_idx;
736 else if (url_idx == start_url_idx)
737 url_idx = -1; // No usable URL.
738 } while (url_idx >= 0 &&
739 !IsUrlUsable(update_state.download_urls[url_idx], http_allowed));
740 }
741
742 // If we have a download URL but a failure was observed, compute a new backoff
743 // expiry (if allowed). The backoff period is generally 2 ^ (num_failures - 1)
744 // days, bounded by the size of int and kAttemptBackoffMaxIntervalInDays, and
745 // fuzzed by kAttemptBackoffFuzzInHours hours. Backoff expiry is computed from
746 // the latest recorded time of error.
747 Time backoff_expiry;
748 if (url_idx >= 0 && is_failure_occurred && may_backoff) {
749 CHECK(!err_time.is_null())
750 << "We must have an error timestamp if a failure occurred!";
751 const uint64_t* seed = ec->GetValue(state->random_provider()->var_seed());
752 POLICY_CHECK_VALUE_AND_FAIL(seed, error);
753 PRNG prng(*seed);
Alex Deymof329b932014-10-30 01:37:48 -0700754 int exp = min(update_state.num_failures,
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700755 static_cast<int>(sizeof(int)) * 8 - 2);
Amin Hassani186ff6a2018-02-27 11:06:03 -0800756 TimeDelta backoff_interval = TimeDelta::FromDays(min(
757 1 << exp,
758 kNextUpdateCheckPolicyConstants.attempt_backoff_max_interval_in_days));
759 TimeDelta backoff_fuzz = TimeDelta::FromHours(
760 kNextUpdateCheckPolicyConstants.attempt_backoff_fuzz_in_hours);
761 TimeDelta wait_period = NextUpdateCheckTimePolicyImpl::FuzzedInterval(
762 &prng, backoff_interval.InSeconds(), backoff_fuzz.InSeconds());
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700763 backoff_expiry = err_time + wait_period;
764
765 // If the newly computed backoff already expired, nullify it.
766 if (ec->IsWallclockTimeGreaterThan(backoff_expiry))
767 backoff_expiry = Time();
768 }
769
770 result->do_increment_failures = is_failure_occurred;
771 result->backoff_expiry = backoff_expiry;
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700772 result->url_idx = url_idx;
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700773 result->url_num_errors = url_num_errors;
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700774 return EvalStatus::kSucceeded;
775}
776
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700777EvalStatus ChromeOSPolicy::UpdateScattering(
778 EvaluationContext* ec,
779 State* state,
780 string* error,
781 UpdateScatteringResult* result,
782 const UpdateState& update_state) const {
783 // Preconditions. These stem from the postconditions and usage contract.
784 DCHECK(update_state.scatter_wait_period >= kZeroInterval);
785 DCHECK_GE(update_state.scatter_check_threshold, 0);
786
787 // Set default result values.
788 result->is_scattering = false;
789 result->wait_period = kZeroInterval;
790 result->check_threshold = 0;
791
792 DevicePolicyProvider* const dp_provider = state->device_policy_provider();
793
794 // Ensure that a device policy is loaded.
795 const bool* device_policy_is_loaded_p = ec->GetValue(
796 dp_provider->var_device_policy_is_loaded());
797 if (!(device_policy_is_loaded_p && *device_policy_is_loaded_p))
798 return EvalStatus::kSucceeded;
799
800 // Is scattering enabled by policy?
801 const TimeDelta* scatter_factor_p = ec->GetValue(
802 dp_provider->var_scatter_factor());
803 if (!scatter_factor_p || *scatter_factor_p == kZeroInterval)
804 return EvalStatus::kSucceeded;
805
806 // Obtain a pseudo-random number generator.
807 const uint64_t* seed = ec->GetValue(state->random_provider()->var_seed());
808 POLICY_CHECK_VALUE_AND_FAIL(seed, error);
809 PRNG prng(*seed);
810
811 // Step 1: Maintain the scattering wait period.
812 //
813 // If no wait period was previously determined, or it no longer fits in the
814 // scatter factor, then generate a new one. Otherwise, keep the one we have.
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700815 TimeDelta wait_period = update_state.scatter_wait_period;
816 if (wait_period == kZeroInterval || wait_period > *scatter_factor_p) {
817 wait_period = TimeDelta::FromSeconds(
818 prng.RandMinMax(1, scatter_factor_p->InSeconds()));
819 }
820
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700821 // If we surpassed the wait period or the max scatter period associated with
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700822 // the update, then no wait is needed.
823 Time wait_expires = (update_state.first_seen +
824 min(wait_period, update_state.scatter_wait_period_max));
Gilad Arnolda65fced2014-07-23 09:01:31 -0700825 if (ec->IsWallclockTimeGreaterThan(wait_expires))
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700826 wait_period = kZeroInterval;
827
828 // Step 2: Maintain the update check threshold count.
829 //
830 // If an update check threshold is not specified then generate a new
831 // one.
832 int check_threshold = update_state.scatter_check_threshold;
833 if (check_threshold == 0) {
834 check_threshold = prng.RandMinMax(
835 update_state.scatter_check_threshold_min,
836 update_state.scatter_check_threshold_max);
837 }
838
839 // If the update check threshold is not within allowed range then nullify it.
840 // TODO(garnold) This is compliant with current logic found in
841 // OmahaRequestAction::IsUpdateCheckCountBasedWaitingSatisfied(). We may want
842 // to change it so that it behaves similarly to the wait period case, namely
843 // if the current value exceeds the maximum, we set a new one within range.
844 if (check_threshold > update_state.scatter_check_threshold_max)
845 check_threshold = 0;
846
847 // If the update check threshold is non-zero and satisfied, then nullify it.
848 if (check_threshold > 0 && update_state.num_checks >= check_threshold)
849 check_threshold = 0;
850
851 bool is_scattering = (wait_period != kZeroInterval || check_threshold);
852 EvalStatus ret = EvalStatus::kSucceeded;
853 if (is_scattering && wait_period == update_state.scatter_wait_period &&
854 check_threshold == update_state.scatter_check_threshold)
855 ret = EvalStatus::kAskMeAgainLater;
856 result->is_scattering = is_scattering;
857 result->wait_period = wait_period;
858 result->check_threshold = check_threshold;
859 return ret;
860}
861
Alex Deymo63784a52014-05-28 10:46:14 -0700862} // namespace chromeos_update_manager