blob: 71fec40ce19bb1d38f280070265c41a847139197 [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:
Sen Jiang8e768e92017-06-28 17:13:19 -070089 case ErrorCode::kPayloadTimestampError:
Gilad Arnoldb3b05442014-05-30 14:25:05 -070090 LOG(INFO) << "Advancing download URL due to error "
Alex Deymoe88e9fe2016-02-03 16:38:00 -080091 << chromeos_update_engine::utils::ErrorCodeToString(err_code)
Gilad Arnoldb3b05442014-05-30 14:25:05 -070092 << " (" << static_cast<int>(err_code) << ")";
Gilad Arnoldb3b05442014-05-30 14:25:05 -070093 return true;
94
95 // Errors which seem to be just transient network/communication related
96 // failures and do not indicate any inherent problem with the URL itself.
97 // So, we should keep the current URL but just increment the
98 // failure count to give it more chances. This way, while we maximize our
99 // chances of downloading from the URLs that appear earlier in the response
100 // (because download from a local server URL that appears earlier in a
101 // response is preferable than downloading from the next URL which could be
Alex Vakulenko072359c2014-07-18 11:41:07 -0700102 // an Internet URL and thus could be more expensive).
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700103 case ErrorCode::kError:
104 case ErrorCode::kDownloadTransferError:
105 case ErrorCode::kDownloadWriteError:
106 case ErrorCode::kDownloadStateInitializationError:
Gilad Arnold684219d2014-07-07 14:54:57 -0700107 case ErrorCode::kOmahaErrorInHTTPResponse: // Aggregate for HTTP errors.
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700108 LOG(INFO) << "Incrementing URL failure count due to error "
Alex Deymoe88e9fe2016-02-03 16:38:00 -0800109 << chromeos_update_engine::utils::ErrorCodeToString(err_code)
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700110 << " (" << static_cast<int>(err_code) << ")";
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700111 *url_num_error_p += 1;
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700112 return false;
113
114 // Errors which are not specific to a URL and hence shouldn't result in
115 // the URL being penalized. This can happen in two cases:
116 // 1. We haven't started downloading anything: These errors don't cost us
117 // anything in terms of actual payload bytes, so we should just do the
118 // regular retries at the next update check.
119 // 2. We have successfully downloaded the payload: In this case, the
120 // payload attempt number would have been incremented and would take care
Alex Vakulenko072359c2014-07-18 11:41:07 -0700121 // of the back-off at the next update check.
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700122 // In either case, there's no need to update URL index or failure count.
123 case ErrorCode::kOmahaRequestError:
124 case ErrorCode::kOmahaResponseHandlerError:
125 case ErrorCode::kPostinstallRunnerError:
126 case ErrorCode::kFilesystemCopierError:
127 case ErrorCode::kInstallDeviceOpenError:
128 case ErrorCode::kKernelDeviceOpenError:
129 case ErrorCode::kDownloadNewPartitionInfoError:
130 case ErrorCode::kNewRootfsVerificationError:
131 case ErrorCode::kNewKernelVerificationError:
132 case ErrorCode::kPostinstallBootedFromFirmwareB:
133 case ErrorCode::kPostinstallFirmwareRONotUpdatable:
134 case ErrorCode::kOmahaRequestEmptyResponseError:
135 case ErrorCode::kOmahaRequestXMLParseError:
136 case ErrorCode::kOmahaResponseInvalid:
137 case ErrorCode::kOmahaUpdateIgnoredPerPolicy:
138 case ErrorCode::kOmahaUpdateDeferredPerPolicy:
Kevin Cernekee2494e282016-03-29 18:03:53 -0700139 case ErrorCode::kNonCriticalUpdateInOOBE:
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700140 case ErrorCode::kOmahaUpdateDeferredForBackoff:
141 case ErrorCode::kPostinstallPowerwashError:
142 case ErrorCode::kUpdateCanceledByChannelChange:
David Zeuthenf3e28012014-08-26 18:23:52 -0400143 case ErrorCode::kOmahaRequestXMLHasEntityDecl:
Allie Woodeb9e6d82015-04-17 13:55:30 -0700144 case ErrorCode::kFilesystemVerifierError:
Alex Deymo1f19dcc2016-02-03 09:22:17 -0800145 case ErrorCode::kUserCanceled:
Weidong Guo421ff332017-04-17 10:08:38 -0700146 case ErrorCode::kOmahaUpdateIgnoredOverCellular:
Sen Jiangfe522822017-10-31 15:14:11 -0700147 case ErrorCode::kUpdatedButNotActive:
Sen Jiang89e24c12018-03-22 18:05:44 -0700148 case ErrorCode::kNoUpdate:
Marton Hunyady199152d2018-05-07 19:08:48 +0200149 case ErrorCode::kRollbackNotPossible:
Amin Hassani80f4d4c2018-05-16 13:34:00 -0700150 case ErrorCode::kFirstActiveOmahaPingSentPersistenceError:
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700151 LOG(INFO) << "Not changing URL index or failure count due to error "
Alex Deymoe88e9fe2016-02-03 16:38:00 -0800152 << chromeos_update_engine::utils::ErrorCodeToString(err_code)
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700153 << " (" << static_cast<int>(err_code) << ")";
154 return false;
155
156 case ErrorCode::kSuccess: // success code
157 case ErrorCode::kUmaReportedMax: // not an error code
158 case ErrorCode::kOmahaRequestHTTPResponseBase: // aggregated already
Alex Deymo1f19dcc2016-02-03 09:22:17 -0800159 case ErrorCode::kDevModeFlag: // not an error code
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700160 case ErrorCode::kResumedFlag: // not an error code
161 case ErrorCode::kTestImageFlag: // not an error code
162 case ErrorCode::kTestOmahaUrlFlag: // not an error code
163 case ErrorCode::kSpecialFlags: // not an error code
164 // These shouldn't happen. Enumerating these explicitly here so that we
165 // can let the compiler warn about new error codes that are added to
166 // action_processor.h but not added here.
167 LOG(WARNING) << "Unexpected error "
Alex Deymoe88e9fe2016-02-03 16:38:00 -0800168 << chromeos_update_engine::utils::ErrorCodeToString(err_code)
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700169 << " (" << static_cast<int>(err_code) << ")";
170 // Note: Not adding a default here so as to let the compiler warn us of
171 // any new enums that were added in the .h but not listed in this switch.
172 }
173 return false;
174}
175
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700176// Checks whether |url| can be used under given download restrictions.
177bool IsUrlUsable(const string& url, bool http_allowed) {
Alex Vakulenko0103c362016-01-20 07:56:15 -0800178 return http_allowed ||
179 !base::StartsWith(url, "http://",
180 base::CompareCase::INSENSITIVE_ASCII);
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700181}
182
183} // namespace
184
Alex Deymo63784a52014-05-28 10:46:14 -0700185namespace chromeos_update_manager {
Alex Deymoc705cc82014-02-19 11:15:00 -0800186
Amin Hassani186ff6a2018-02-27 11:06:03 -0800187const NextUpdateCheckPolicyConstants
188 ChromeOSPolicy::kNextUpdateCheckPolicyConstants = {
189 .timeout_initial_interval = 7 * 60,
190 .timeout_periodic_interval = 45 * 60,
191 .timeout_max_backoff_interval = 4 * 60 * 60,
192 .timeout_regular_fuzz = 10 * 60,
193 .attempt_backoff_max_interval_in_days = 16,
194 .attempt_backoff_fuzz_in_hours = 12,
195};
Alex Deymo14e7dde2015-10-20 14:46:33 -0700196
Gilad Arnold349ac832014-10-06 14:20:28 -0700197const int ChromeOSPolicy::kMaxP2PAttempts = 10;
198const int ChromeOSPolicy::kMaxP2PAttemptsPeriodInSeconds = 5 * 24 * 60 * 60;
Gilad Arnolda2e8eaa2014-09-24 13:12:33 -0700199
Alex Deymo0d11c602014-04-23 20:12:20 -0700200EvalStatus ChromeOSPolicy::UpdateCheckAllowed(
201 EvaluationContext* ec, State* state, string* error,
202 UpdateCheckParams* result) const {
Gilad Arnold42f253b2014-06-25 12:39:17 -0700203 // Set the default return values.
204 result->updates_enabled = true;
205 result->target_channel.clear();
Gilad Arnoldd4b30322014-07-21 15:35:27 -0700206 result->target_version_prefix.clear();
Marton Hunyadyba51c3f2018-04-25 15:18:10 +0200207 result->rollback_allowed = false;
Marton Hunyady0e0e3542018-02-21 18:51:39 +0100208 result->rollback_allowed_milestones = -1;
Amin Hassanied37d682018-04-06 13:22:00 -0700209 result->interactive = false;
Gilad Arnold42f253b2014-06-25 12:39:17 -0700210
Amin Hassani186ff6a2018-02-27 11:06:03 -0800211 EnoughSlotsAbUpdatesPolicyImpl enough_slots_ab_updates_policy;
212 EnterpriseDevicePolicyImpl enterprise_device_policy;
213 OnlyUpdateOfficialBuildsPolicyImpl only_update_official_builds_policy;
214 InteractiveUpdatePolicyImpl interactive_update_policy;
215 OobePolicyImpl oobe_policy;
216 NextUpdateCheckTimePolicyImpl next_update_check_time_policy(
217 kNextUpdateCheckPolicyConstants);
Gilad Arnolda1eabcd2014-07-09 15:42:40 -0700218
Amin Hassani186ff6a2018-02-27 11:06:03 -0800219 vector<Policy const*> policies_to_consult = {
220 // Do not perform any updates if there are not enough slots to do A/B
221 // updates.
222 &enough_slots_ab_updates_policy,
223
224 // Check to see if Enterprise-managed (has DevicePolicy) and/or
225 // Kiosk-mode. If so, then defer to those settings.
226 &enterprise_device_policy,
227
228 // Check to see if an interactive update was requested.
229 &interactive_update_policy,
230
231 // Unofficial builds should not perform periodic update checks.
232 &only_update_official_builds_policy,
233
234 // If OOBE is enabled, wait until it is completed.
235 &oobe_policy,
236
237 // Ensure that periodic update checks are timed properly.
238 &next_update_check_time_policy,
239 };
240
241 // Now that the list of policy implementations, and the order to consult them,
242 // has been setup, consult the policies. If none of the policies make a
243 // definitive decisions about whether or not to check for updates, then allow
244 // the update check to happen.
245 EvalStatus status = ConsultPolicies(policies_to_consult,
246 &Policy::UpdateCheckAllowed,
247 ec,
248 state,
249 error,
250 result);
251 if (EvalStatus::kContinue != status) {
252 return status;
253 } else {
254 // It is time to check for an update.
255 LOG(INFO) << "Allowing update check.";
Gilad Arnoldbfc44f72014-07-09 14:41:39 -0700256 return EvalStatus::kSucceeded;
257 }
Alex Deymoc705cc82014-02-19 11:15:00 -0800258}
259
Aaron Wood23bd3392017-10-06 14:48:25 -0700260EvalStatus ChromeOSPolicy::UpdateCanBeApplied(EvaluationContext* ec,
261 State* state,
262 std::string* error,
263 ErrorCode* result,
264 InstallPlan* install_plan) const {
Adolfo Victoria0dbf1f92018-07-19 14:18:11 -0700265 UpdateTimeRestrictionsPolicyImpl update_time_restrictions_policy;
266 InteractiveUpdatePolicyImpl interactive_update_policy;
267
268 vector<Policy const*> policies_to_consult = {
269 // Check to see if an interactive update has been requested.
270 &interactive_update_policy,
271
272 // Do not apply or download an update if we are inside one of the
273 // restricted times.
274 &update_time_restrictions_policy,
275 };
276
277 EvalStatus status = ConsultPolicies(policies_to_consult,
278 &Policy::UpdateCanBeApplied,
279 ec,
280 state,
281 error,
282 result,
283 install_plan);
284 if (EvalStatus::kContinue != status) {
285 return status;
286 } else {
287 // The update can proceed.
288 LOG(INFO) << "Allowing update to be applied.";
289 *result = ErrorCode::kSuccess;
290 return EvalStatus::kSucceeded;
291 }
Aaron Wood23bd3392017-10-06 14:48:25 -0700292}
293
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700294EvalStatus ChromeOSPolicy::UpdateCanStart(
295 EvaluationContext* ec,
296 State* state,
297 string* error,
Gilad Arnold42f253b2014-06-25 12:39:17 -0700298 UpdateDownloadParams* result,
Gilad Arnoldd78caf92014-09-24 09:28:14 -0700299 const UpdateState update_state) const {
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700300 // Set the default return values. Note that we set persisted values (backoff,
301 // scattering) to the same values presented in the update state. The reason is
302 // that preemptive returns, such as the case where an update check is due,
303 // should not clear off the said values; rather, it is the deliberate
304 // inference of new values that should cause them to be reset.
Gilad Arnold14a9e702014-10-08 08:09:09 -0700305 result->update_can_start = false;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700306 result->cannot_start_reason = UpdateCannotStartReason::kUndefined;
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700307 result->download_url_idx = -1;
Gilad Arnold14a9e702014-10-08 08:09:09 -0700308 result->download_url_allowed = true;
309 result->download_url_num_errors = 0;
Gilad Arnoldb2f99192014-10-07 13:01:52 -0700310 result->p2p_downloading_allowed = false;
311 result->p2p_sharing_allowed = false;
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700312 result->do_increment_failures = false;
313 result->backoff_expiry = update_state.backoff_expiry;
314 result->scatter_wait_period = update_state.scatter_wait_period;
315 result->scatter_check_threshold = update_state.scatter_check_threshold;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700316
317 // Make sure that we're not due for an update check.
318 UpdateCheckParams check_result;
319 EvalStatus check_status = UpdateCheckAllowed(ec, state, error, &check_result);
320 if (check_status == EvalStatus::kFailed)
321 return EvalStatus::kFailed;
Gilad Arnold14a9e702014-10-08 08:09:09 -0700322 bool is_check_due = (check_status == EvalStatus::kSucceeded &&
323 check_result.updates_enabled == true);
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700324
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700325 // Check whether backoff applies, and if not then which URL can be used for
326 // downloading. These require scanning the download error log, and so they are
327 // done together.
328 UpdateBackoffAndDownloadUrlResult backoff_url_result;
329 EvalStatus backoff_url_status = UpdateBackoffAndDownloadUrl(
330 ec, state, error, &backoff_url_result, update_state);
Gilad Arnold14a9e702014-10-08 08:09:09 -0700331 if (backoff_url_status == EvalStatus::kFailed)
332 return EvalStatus::kFailed;
333 result->download_url_idx = backoff_url_result.url_idx;
334 result->download_url_num_errors = backoff_url_result.url_num_errors;
335 result->do_increment_failures = backoff_url_result.do_increment_failures;
336 result->backoff_expiry = backoff_url_result.backoff_expiry;
337 bool is_backoff_active =
338 (backoff_url_status == EvalStatus::kAskMeAgainLater) ||
339 !backoff_url_result.backoff_expiry.is_null();
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700340
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700341 DevicePolicyProvider* const dp_provider = state->device_policy_provider();
Gilad Arnold14a9e702014-10-08 08:09:09 -0700342 bool is_scattering_active = false;
343 EvalStatus scattering_status = EvalStatus::kSucceeded;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700344
345 const bool* device_policy_is_loaded_p = ec->GetValue(
346 dp_provider->var_device_policy_is_loaded());
347 if (device_policy_is_loaded_p && *device_policy_is_loaded_p) {
Gilad Arnold76a11f62014-05-20 09:02:12 -0700348 // Check whether scattering applies to this update attempt. We should not be
349 // scattering if this is an interactive update check, or if OOBE is enabled
350 // but not completed.
351 //
352 // Note: current code further suppresses scattering if a "deadline"
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700353 // attribute is found in the Omaha response. However, it appears that the
Gilad Arnold76a11f62014-05-20 09:02:12 -0700354 // presence of this attribute is merely indicative of an OOBE update, during
355 // which we suppress scattering anyway.
Gilad Arnold14a9e702014-10-08 08:09:09 -0700356 bool is_scattering_applicable = false;
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700357 result->scatter_wait_period = kZeroInterval;
358 result->scatter_check_threshold = 0;
Amin Hassanied37d682018-04-06 13:22:00 -0700359 if (!update_state.interactive) {
Gilad Arnold76a11f62014-05-20 09:02:12 -0700360 const bool* is_oobe_enabled_p = ec->GetValue(
361 state->config_provider()->var_is_oobe_enabled());
362 if (is_oobe_enabled_p && !(*is_oobe_enabled_p)) {
Gilad Arnold14a9e702014-10-08 08:09:09 -0700363 is_scattering_applicable = true;
Gilad Arnold76a11f62014-05-20 09:02:12 -0700364 } else {
365 const bool* is_oobe_complete_p = ec->GetValue(
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700366 state->system_provider()->var_is_oobe_complete());
Gilad Arnold14a9e702014-10-08 08:09:09 -0700367 is_scattering_applicable = (is_oobe_complete_p && *is_oobe_complete_p);
Gilad Arnold76a11f62014-05-20 09:02:12 -0700368 }
369 }
370
371 // Compute scattering values.
Gilad Arnold14a9e702014-10-08 08:09:09 -0700372 if (is_scattering_applicable) {
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700373 UpdateScatteringResult scatter_result;
Gilad Arnold14a9e702014-10-08 08:09:09 -0700374 scattering_status = UpdateScattering(ec, state, error, &scatter_result,
375 update_state);
376 if (scattering_status == EvalStatus::kFailed) {
377 return EvalStatus::kFailed;
378 } else {
379 result->scatter_wait_period = scatter_result.wait_period;
380 result->scatter_check_threshold = scatter_result.check_threshold;
381 if (scattering_status == EvalStatus::kAskMeAgainLater ||
382 scatter_result.is_scattering)
383 is_scattering_active = true;
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700384 }
385 }
Gilad Arnold78ecbfc2014-10-22 14:38:25 -0700386 }
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700387
Gilad Arnold78ecbfc2014-10-22 14:38:25 -0700388 // Find out whether P2P is globally enabled.
389 bool p2p_enabled;
390 EvalStatus p2p_enabled_status = P2PEnabled(ec, state, error, &p2p_enabled);
391 if (p2p_enabled_status != EvalStatus::kSucceeded)
392 return EvalStatus::kFailed;
393
394 // Is P2P is enabled, consider allowing it for downloading and/or sharing.
395 if (p2p_enabled) {
396 // Sharing via P2P is allowed if not disabled by Omaha.
397 if (update_state.p2p_sharing_disabled) {
398 LOG(INFO) << "Blocked P2P sharing because it is disabled by Omaha.";
Gilad Arnoldb2f99192014-10-07 13:01:52 -0700399 } else {
Gilad Arnold78ecbfc2014-10-22 14:38:25 -0700400 result->p2p_sharing_allowed = true;
Gilad Arnoldef8d0872014-10-03 14:14:06 -0700401 }
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700402
Gilad Arnold78ecbfc2014-10-22 14:38:25 -0700403 // Downloading via P2P is allowed if not disabled by Omaha, an update is not
404 // interactive, and other limits haven't been reached.
405 if (update_state.p2p_downloading_disabled) {
406 LOG(INFO) << "Blocked P2P downloading because it is disabled by Omaha.";
Amin Hassanied37d682018-04-06 13:22:00 -0700407 } else if (update_state.interactive) {
Gilad Arnold78ecbfc2014-10-22 14:38:25 -0700408 LOG(INFO) << "Blocked P2P downloading because update is interactive.";
Gilad Arnoldb2f99192014-10-07 13:01:52 -0700409 } else if (update_state.p2p_num_attempts >= kMaxP2PAttempts) {
Gilad Arnold78ecbfc2014-10-22 14:38:25 -0700410 LOG(INFO) << "Blocked P2P downloading as it was attempted too many "
411 "times.";
Gilad Arnoldb2f99192014-10-07 13:01:52 -0700412 } else if (!update_state.p2p_first_attempted.is_null() &&
413 ec->IsWallclockTimeGreaterThan(
414 update_state.p2p_first_attempted +
415 TimeDelta::FromSeconds(kMaxP2PAttemptsPeriodInSeconds))) {
Gilad Arnold78ecbfc2014-10-22 14:38:25 -0700416 LOG(INFO) << "Blocked P2P downloading as its usage timespan exceeds "
417 "limit.";
Gilad Arnoldb2f99192014-10-07 13:01:52 -0700418 } else {
Gilad Arnold14a9e702014-10-08 08:09:09 -0700419 // P2P download is allowed; if backoff or scattering are active, be sure
420 // to suppress them, yet prevent any download URL from being used.
Gilad Arnoldb2f99192014-10-07 13:01:52 -0700421 result->p2p_downloading_allowed = true;
Gilad Arnold14a9e702014-10-08 08:09:09 -0700422 if (is_backoff_active || is_scattering_active) {
423 is_backoff_active = is_scattering_active = false;
424 result->download_url_allowed = false;
425 }
Gilad Arnoldb2f99192014-10-07 13:01:52 -0700426 }
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700427 }
428
Gilad Arnold14a9e702014-10-08 08:09:09 -0700429 // Check for various deterrents.
430 if (is_check_due) {
431 result->cannot_start_reason = UpdateCannotStartReason::kCheckDue;
432 return EvalStatus::kSucceeded;
433 }
434 if (is_backoff_active) {
435 result->cannot_start_reason = UpdateCannotStartReason::kBackoff;
436 return backoff_url_status;
437 }
438 if (is_scattering_active) {
439 result->cannot_start_reason = UpdateCannotStartReason::kScattering;
440 return scattering_status;
441 }
Gilad Arnoldb2f99192014-10-07 13:01:52 -0700442 if (result->download_url_idx < 0 && !result->p2p_downloading_allowed) {
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700443 result->cannot_start_reason = UpdateCannotStartReason::kCannotDownload;
Gilad Arnold14a9e702014-10-08 08:09:09 -0700444 return EvalStatus::kSucceeded;
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700445 }
446
Gilad Arnold14a9e702014-10-08 08:09:09 -0700447 // Update is good to go.
448 result->update_can_start = true;
Gilad Arnoldaf2f6ae2014-04-28 14:14:52 -0700449 return EvalStatus::kSucceeded;
450}
451
Gilad Arnolda8262e22014-06-02 13:54:27 -0700452// TODO(garnold) Logic in this method is based on
453// ConnectionManager::IsUpdateAllowedOver(); be sure to deprecate the latter.
454//
455// TODO(garnold) The current logic generally treats the list of allowed
456// connections coming from the device policy as a whitelist, meaning that it
457// can only be used for enabling connections, but not disable them. Further,
458// certain connection types (like Bluetooth) cannot be enabled even by policy.
459// In effect, the only thing that device policy can change is to enable
460// updates over a cellular network (disabled by default). We may want to
461// revisit this semantics, allowing greater flexibility in defining specific
462// permissions over all types of networks.
Gilad Arnold684219d2014-07-07 14:54:57 -0700463EvalStatus ChromeOSPolicy::UpdateDownloadAllowed(
Gilad Arnolda8262e22014-06-02 13:54:27 -0700464 EvaluationContext* ec,
465 State* state,
466 string* error,
467 bool* result) const {
468 // Get the current connection type.
469 ShillProvider* const shill_provider = state->shill_provider();
470 const ConnectionType* conn_type_p = ec->GetValue(
471 shill_provider->var_conn_type());
472 POLICY_CHECK_VALUE_AND_FAIL(conn_type_p, error);
473 ConnectionType conn_type = *conn_type_p;
474
475 // If we're tethering, treat it as a cellular connection.
476 if (conn_type != ConnectionType::kCellular) {
477 const ConnectionTethering* conn_tethering_p = ec->GetValue(
478 shill_provider->var_conn_tethering());
479 POLICY_CHECK_VALUE_AND_FAIL(conn_tethering_p, error);
480 if (*conn_tethering_p == ConnectionTethering::kConfirmed)
481 conn_type = ConnectionType::kCellular;
482 }
483
484 // By default, we allow updates for all connection types, with exceptions as
485 // noted below. This also determines whether a device policy can override the
486 // default.
487 *result = true;
488 bool device_policy_can_override = false;
489 switch (conn_type) {
490 case ConnectionType::kBluetooth:
491 *result = false;
492 break;
493
494 case ConnectionType::kCellular:
495 *result = false;
496 device_policy_can_override = true;
497 break;
498
499 case ConnectionType::kUnknown:
500 if (error)
501 *error = "Unknown connection type";
502 return EvalStatus::kFailed;
503
504 default:
505 break; // Nothing to do.
506 }
507
508 // If update is allowed, we're done.
509 if (*result)
510 return EvalStatus::kSucceeded;
511
512 // Check whether the device policy specifically allows this connection.
Gilad Arnolda8262e22014-06-02 13:54:27 -0700513 if (device_policy_can_override) {
514 DevicePolicyProvider* const dp_provider = state->device_policy_provider();
515 const bool* device_policy_is_loaded_p = ec->GetValue(
516 dp_provider->var_device_policy_is_loaded());
517 if (device_policy_is_loaded_p && *device_policy_is_loaded_p) {
518 const set<ConnectionType>* allowed_conn_types_p = ec->GetValue(
519 dp_provider->var_allowed_connection_types_for_update());
520 if (allowed_conn_types_p) {
521 if (allowed_conn_types_p->count(conn_type)) {
522 *result = true;
523 return EvalStatus::kSucceeded;
524 }
Gilad Arnold28d6be62014-06-30 14:04:04 -0700525 } else if (conn_type == ConnectionType::kCellular) {
526 // Local user settings can allow updates over cellular iff a policy was
527 // loaded but no allowed connections were specified in it.
528 const bool* update_over_cellular_allowed_p = ec->GetValue(
529 state->updater_provider()->var_cellular_enabled());
530 if (update_over_cellular_allowed_p && *update_over_cellular_allowed_p)
531 *result = true;
Gilad Arnolda8262e22014-06-02 13:54:27 -0700532 }
533 }
534 }
535
Gilad Arnold28d6be62014-06-30 14:04:04 -0700536 return (*result ? EvalStatus::kSucceeded : EvalStatus::kAskMeAgainLater);
Gilad Arnolda8262e22014-06-02 13:54:27 -0700537}
538
Gilad Arnold78ecbfc2014-10-22 14:38:25 -0700539EvalStatus ChromeOSPolicy::P2PEnabled(EvaluationContext* ec,
540 State* state,
Alex Deymo39910dc2015-11-09 17:04:30 -0800541 string* error,
Gilad Arnold78ecbfc2014-10-22 14:38:25 -0700542 bool* result) const {
543 bool enabled = false;
544
545 // Determine whether use of P2P is allowed by policy. Even if P2P is not
546 // explicitly allowed, we allow it if the device is enterprise enrolled (that
547 // is, missing or empty owner string).
548 DevicePolicyProvider* const dp_provider = state->device_policy_provider();
549 const bool* device_policy_is_loaded_p = ec->GetValue(
550 dp_provider->var_device_policy_is_loaded());
551 if (device_policy_is_loaded_p && *device_policy_is_loaded_p) {
552 const bool* policy_au_p2p_enabled_p = ec->GetValue(
553 dp_provider->var_au_p2p_enabled());
554 if (policy_au_p2p_enabled_p) {
555 enabled = *policy_au_p2p_enabled_p;
556 } else {
557 const string* policy_owner_p = ec->GetValue(dp_provider->var_owner());
558 if (!policy_owner_p || policy_owner_p->empty())
559 enabled = true;
560 }
561 }
562
563 // Enable P2P, if so mandated by the updater configuration. This is additive
564 // to whether or not P2P is enabled by device policy.
565 if (!enabled) {
566 const bool* updater_p2p_enabled_p = ec->GetValue(
567 state->updater_provider()->var_p2p_enabled());
568 enabled = updater_p2p_enabled_p && *updater_p2p_enabled_p;
569 }
570
571 *result = enabled;
572 return EvalStatus::kSucceeded;
573}
574
575EvalStatus ChromeOSPolicy::P2PEnabledChanged(EvaluationContext* ec,
576 State* state,
Alex Deymo39910dc2015-11-09 17:04:30 -0800577 string* error,
Gilad Arnold78ecbfc2014-10-22 14:38:25 -0700578 bool* result,
579 bool prev_result) const {
580 EvalStatus status = P2PEnabled(ec, state, error, result);
581 if (status == EvalStatus::kSucceeded && *result == prev_result)
582 return EvalStatus::kAskMeAgainLater;
583 return status;
584}
585
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700586EvalStatus ChromeOSPolicy::UpdateBackoffAndDownloadUrl(
Alex Deymof329b932014-10-30 01:37:48 -0700587 EvaluationContext* ec, State* state, string* error,
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700588 UpdateBackoffAndDownloadUrlResult* result,
589 const UpdateState& update_state) const {
590 // Sanity checks.
591 DCHECK_GE(update_state.download_errors_max, 0);
592
593 // Set default result values.
594 result->do_increment_failures = false;
595 result->backoff_expiry = update_state.backoff_expiry;
596 result->url_idx = -1;
597 result->url_num_errors = 0;
598
599 const bool* is_official_build_p = ec->GetValue(
600 state->system_provider()->var_is_official_build());
601 bool is_official_build = (is_official_build_p ? *is_official_build_p : true);
602
603 // Check whether backoff is enabled.
604 bool may_backoff = false;
605 if (update_state.is_backoff_disabled) {
606 LOG(INFO) << "Backoff disabled by Omaha.";
Amin Hassanied37d682018-04-06 13:22:00 -0700607 } else if (update_state.interactive) {
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700608 LOG(INFO) << "No backoff for interactive updates.";
609 } else if (update_state.is_delta_payload) {
610 LOG(INFO) << "No backoff for delta payloads.";
611 } else if (!is_official_build) {
612 LOG(INFO) << "No backoff for unofficial builds.";
613 } else {
614 may_backoff = true;
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700615 }
616
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700617 // If previous backoff still in effect, block.
618 if (may_backoff && !update_state.backoff_expiry.is_null() &&
619 !ec->IsWallclockTimeGreaterThan(update_state.backoff_expiry)) {
620 LOG(INFO) << "Previous backoff has not expired, waiting.";
621 return EvalStatus::kAskMeAgainLater;
622 }
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700623
624 // Determine whether HTTP downloads are forbidden by policy. This only
625 // applies to official system builds; otherwise, HTTP is always enabled.
626 bool http_allowed = true;
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700627 if (is_official_build) {
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700628 DevicePolicyProvider* const dp_provider = state->device_policy_provider();
629 const bool* device_policy_is_loaded_p = ec->GetValue(
630 dp_provider->var_device_policy_is_loaded());
631 if (device_policy_is_loaded_p && *device_policy_is_loaded_p) {
632 const bool* policy_http_downloads_enabled_p = ec->GetValue(
633 dp_provider->var_http_downloads_enabled());
634 http_allowed = (!policy_http_downloads_enabled_p ||
635 *policy_http_downloads_enabled_p);
636 }
637 }
638
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700639 int url_idx = update_state.last_download_url_idx;
640 if (url_idx < 0)
641 url_idx = -1;
642 bool do_advance_url = false;
643 bool is_failure_occurred = false;
644 Time err_time;
645
646 // Scan the relevant part of the download error log, tracking which URLs are
647 // being used, and accounting the number of errors for each URL. Note that
648 // this process may not traverse all errors provided, as it may decide to bail
649 // out midway depending on the particular errors exhibited, the number of
650 // failures allowed, etc. When this ends, |url_idx| will point to the last URL
651 // used (-1 if starting fresh), |do_advance_url| will determine whether the
652 // URL needs to be advanced, and |err_time| the point in time when the last
653 // reported error occurred. Additionally, if the error log indicates that an
654 // update attempt has failed (abnormal), then |is_failure_occurred| will be
655 // set to true.
656 const int num_urls = update_state.download_urls.size();
657 int prev_url_idx = -1;
658 int url_num_errors = update_state.last_download_url_num_errors;
659 Time prev_err_time;
660 bool is_first = true;
661 for (const auto& err_tuple : update_state.download_errors) {
662 // Do some sanity checks.
663 int used_url_idx = get<0>(err_tuple);
664 if (is_first && url_idx >= 0 && used_url_idx != url_idx) {
665 LOG(WARNING) << "First URL in error log (" << used_url_idx
666 << ") not as expected (" << url_idx << ")";
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700667 }
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700668 is_first = false;
669 url_idx = used_url_idx;
670 if (url_idx < 0 || url_idx >= num_urls) {
671 LOG(ERROR) << "Download error log contains an invalid URL index ("
672 << url_idx << ")";
673 return EvalStatus::kFailed;
674 }
675 err_time = get<2>(err_tuple);
676 if (!(prev_err_time.is_null() || err_time >= prev_err_time)) {
677 // TODO(garnold) Monotonicity cannot really be assumed when dealing with
678 // wallclock-based timestamps. However, we're making a simplifying
679 // assumption so as to keep the policy implementation straightforward, for
680 // now. In general, we should convert all timestamp handling in the
681 // UpdateManager to use monotonic time (instead of wallclock), including
682 // the computation of various expiration times (backoff, scattering, etc).
683 // The client will do whatever conversions necessary when
684 // persisting/retrieving these values across reboots. See chromium:408794.
685 LOG(ERROR) << "Download error timestamps not monotonically increasing.";
686 return EvalStatus::kFailed;
687 }
688 prev_err_time = err_time;
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700689
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700690 // Ignore errors that happened before the last known failed attempt.
691 if (!update_state.failures_last_updated.is_null() &&
692 err_time <= update_state.failures_last_updated)
693 continue;
694
695 if (prev_url_idx >= 0) {
696 if (url_idx < prev_url_idx) {
697 LOG(ERROR) << "The URLs in the download error log have wrapped around ("
698 << prev_url_idx << "->" << url_idx
699 << "). This should not have happened and means that there's "
700 "a bug. To be conservative, we record a failed attempt "
701 "(invalidating the rest of the error log) and resume "
702 "download from the first usable URL.";
703 url_idx = -1;
704 is_failure_occurred = true;
705 break;
706 }
707
708 if (url_idx > prev_url_idx) {
709 url_num_errors = 0;
710 do_advance_url = false;
711 }
712 }
713
714 if (HandleErrorCode(get<1>(err_tuple), &url_num_errors) ||
715 url_num_errors > update_state.download_errors_max)
716 do_advance_url = true;
717
718 prev_url_idx = url_idx;
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700719 }
720
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700721 // If required, advance to the next usable URL. If the URLs wraparound, we
722 // mark an update attempt failure. Also be sure to set the download error
723 // count to zero.
724 if (url_idx < 0 || do_advance_url) {
725 url_num_errors = 0;
726 int start_url_idx = -1;
727 do {
728 if (++url_idx == num_urls) {
729 url_idx = 0;
730 // We only mark failure if an actual advancing of a URL was required.
731 if (do_advance_url)
732 is_failure_occurred = true;
733 }
734
735 if (start_url_idx < 0)
736 start_url_idx = url_idx;
737 else if (url_idx == start_url_idx)
738 url_idx = -1; // No usable URL.
739 } while (url_idx >= 0 &&
740 !IsUrlUsable(update_state.download_urls[url_idx], http_allowed));
741 }
742
743 // If we have a download URL but a failure was observed, compute a new backoff
744 // expiry (if allowed). The backoff period is generally 2 ^ (num_failures - 1)
745 // days, bounded by the size of int and kAttemptBackoffMaxIntervalInDays, and
746 // fuzzed by kAttemptBackoffFuzzInHours hours. Backoff expiry is computed from
747 // the latest recorded time of error.
748 Time backoff_expiry;
749 if (url_idx >= 0 && is_failure_occurred && may_backoff) {
750 CHECK(!err_time.is_null())
751 << "We must have an error timestamp if a failure occurred!";
752 const uint64_t* seed = ec->GetValue(state->random_provider()->var_seed());
753 POLICY_CHECK_VALUE_AND_FAIL(seed, error);
754 PRNG prng(*seed);
Alex Deymof329b932014-10-30 01:37:48 -0700755 int exp = min(update_state.num_failures,
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700756 static_cast<int>(sizeof(int)) * 8 - 2);
Amin Hassani186ff6a2018-02-27 11:06:03 -0800757 TimeDelta backoff_interval = TimeDelta::FromDays(min(
758 1 << exp,
759 kNextUpdateCheckPolicyConstants.attempt_backoff_max_interval_in_days));
760 TimeDelta backoff_fuzz = TimeDelta::FromHours(
761 kNextUpdateCheckPolicyConstants.attempt_backoff_fuzz_in_hours);
762 TimeDelta wait_period = NextUpdateCheckTimePolicyImpl::FuzzedInterval(
763 &prng, backoff_interval.InSeconds(), backoff_fuzz.InSeconds());
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700764 backoff_expiry = err_time + wait_period;
765
766 // If the newly computed backoff already expired, nullify it.
767 if (ec->IsWallclockTimeGreaterThan(backoff_expiry))
768 backoff_expiry = Time();
769 }
770
771 result->do_increment_failures = is_failure_occurred;
772 result->backoff_expiry = backoff_expiry;
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700773 result->url_idx = url_idx;
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700774 result->url_num_errors = url_num_errors;
Gilad Arnoldb3b05442014-05-30 14:25:05 -0700775 return EvalStatus::kSucceeded;
776}
777
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700778EvalStatus ChromeOSPolicy::UpdateScattering(
779 EvaluationContext* ec,
780 State* state,
781 string* error,
782 UpdateScatteringResult* result,
783 const UpdateState& update_state) const {
784 // Preconditions. These stem from the postconditions and usage contract.
785 DCHECK(update_state.scatter_wait_period >= kZeroInterval);
786 DCHECK_GE(update_state.scatter_check_threshold, 0);
787
788 // Set default result values.
789 result->is_scattering = false;
790 result->wait_period = kZeroInterval;
791 result->check_threshold = 0;
792
793 DevicePolicyProvider* const dp_provider = state->device_policy_provider();
794
795 // Ensure that a device policy is loaded.
796 const bool* device_policy_is_loaded_p = ec->GetValue(
797 dp_provider->var_device_policy_is_loaded());
798 if (!(device_policy_is_loaded_p && *device_policy_is_loaded_p))
799 return EvalStatus::kSucceeded;
800
801 // Is scattering enabled by policy?
802 const TimeDelta* scatter_factor_p = ec->GetValue(
803 dp_provider->var_scatter_factor());
804 if (!scatter_factor_p || *scatter_factor_p == kZeroInterval)
805 return EvalStatus::kSucceeded;
806
807 // Obtain a pseudo-random number generator.
808 const uint64_t* seed = ec->GetValue(state->random_provider()->var_seed());
809 POLICY_CHECK_VALUE_AND_FAIL(seed, error);
810 PRNG prng(*seed);
811
812 // Step 1: Maintain the scattering wait period.
813 //
814 // If no wait period was previously determined, or it no longer fits in the
815 // scatter factor, then generate a new one. Otherwise, keep the one we have.
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700816 TimeDelta wait_period = update_state.scatter_wait_period;
817 if (wait_period == kZeroInterval || wait_period > *scatter_factor_p) {
818 wait_period = TimeDelta::FromSeconds(
819 prng.RandMinMax(1, scatter_factor_p->InSeconds()));
820 }
821
Gilad Arnolddc4bb262014-07-23 10:45:19 -0700822 // If we surpassed the wait period or the max scatter period associated with
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700823 // the update, then no wait is needed.
824 Time wait_expires = (update_state.first_seen +
825 min(wait_period, update_state.scatter_wait_period_max));
Gilad Arnolda65fced2014-07-23 09:01:31 -0700826 if (ec->IsWallclockTimeGreaterThan(wait_expires))
Gilad Arnoldf62a4b82014-05-01 07:41:07 -0700827 wait_period = kZeroInterval;
828
829 // Step 2: Maintain the update check threshold count.
830 //
831 // If an update check threshold is not specified then generate a new
832 // one.
833 int check_threshold = update_state.scatter_check_threshold;
834 if (check_threshold == 0) {
835 check_threshold = prng.RandMinMax(
836 update_state.scatter_check_threshold_min,
837 update_state.scatter_check_threshold_max);
838 }
839
840 // If the update check threshold is not within allowed range then nullify it.
841 // TODO(garnold) This is compliant with current logic found in
842 // OmahaRequestAction::IsUpdateCheckCountBasedWaitingSatisfied(). We may want
843 // to change it so that it behaves similarly to the wait period case, namely
844 // if the current value exceeds the maximum, we set a new one within range.
845 if (check_threshold > update_state.scatter_check_threshold_max)
846 check_threshold = 0;
847
848 // If the update check threshold is non-zero and satisfied, then nullify it.
849 if (check_threshold > 0 && update_state.num_checks >= check_threshold)
850 check_threshold = 0;
851
852 bool is_scattering = (wait_period != kZeroInterval || check_threshold);
853 EvalStatus ret = EvalStatus::kSucceeded;
854 if (is_scattering && wait_period == update_state.scatter_wait_period &&
855 check_threshold == update_state.scatter_check_threshold)
856 ret = EvalStatus::kAskMeAgainLater;
857 result->is_scattering = is_scattering;
858 result->wait_period = wait_period;
859 result->check_threshold = check_threshold;
860 return ret;
861}
862
Alex Deymo63784a52014-05-28 10:46:14 -0700863} // namespace chromeos_update_manager