blob: 26bd0ba97f373e3a7234b633912f0b24cea8c149 [file] [log] [blame]
Aaron Wood56d8ab32017-09-22 15:56:18 -07001//
2// Copyright (C) 2017 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//
16
17#include "update_engine/update_manager/android_things_policy.h"
18
19#include <string>
20#include <vector>
21
22#include <base/logging.h>
23#include <base/time/time.h>
24
Aaron Woodbf5a2522017-10-04 10:58:36 -070025#include "update_engine/update_manager/api_restricted_downloads_policy_impl.h"
Aaron Wood56d8ab32017-09-22 15:56:18 -070026#include "update_engine/update_manager/enough_slots_ab_updates_policy_impl.h"
27#include "update_engine/update_manager/interactive_update_policy_impl.h"
28#include "update_engine/update_manager/official_build_check_policy_impl.h"
29
30using base::Time;
31using chromeos_update_engine::ErrorCode;
32using std::string;
33using std::vector;
34
35namespace chromeos_update_manager {
36
37const NextUpdateCheckPolicyConstants
38 AndroidThingsPolicy::kNextUpdateCheckPolicyConstants = {
39 .timeout_initial_interval = 7 * 60,
40 .timeout_periodic_interval = 5 * 60 * 60,
41 .timeout_max_backoff_interval = 26 * 60 * 60,
42 .timeout_regular_fuzz = 10 * 60,
43 .attempt_backoff_max_interval_in_days = 16,
44 .attempt_backoff_fuzz_in_hours = 12,
45};
46
47EvalStatus AndroidThingsPolicy::UpdateCheckAllowed(
48 EvaluationContext* ec,
49 State* state,
50 string* error,
51 UpdateCheckParams* result) const {
52 // Set the default return values.
53 result->updates_enabled = true;
54 result->target_channel.clear();
55 result->target_version_prefix.clear();
Marton Hunyadyba51c3f2018-04-25 15:18:10 +020056 result->rollback_allowed = false;
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -080057 result->rollback_data_save_requested = false;
Marton Hunyady0e0e3542018-02-21 18:51:39 +010058 result->rollback_allowed_milestones = -1;
Amin Hassanied37d682018-04-06 13:22:00 -070059 result->interactive = false;
Aaron Wood56d8ab32017-09-22 15:56:18 -070060
61 // Build a list of policies to consult. Note that each policy may modify the
62 // result structure, even if it signals kContinue.
63 EnoughSlotsAbUpdatesPolicyImpl enough_slots_ab_updates_policy;
64 OnlyUpdateOfficialBuildsPolicyImpl only_update_official_builds_policy;
65 InteractiveUpdatePolicyImpl interactive_update_policy;
66 NextUpdateCheckTimePolicyImpl next_update_check_time_policy(
67 kNextUpdateCheckPolicyConstants);
68
69 vector<Policy const*> policies_to_consult = {
70 // Do not perform any updates if there are not enough slots to do
71 // A/B updates
72 &enough_slots_ab_updates_policy,
73
Aaron Wood56d8ab32017-09-22 15:56:18 -070074 // Check to see if an interactive update was requested.
75 &interactive_update_policy,
76
Sen Jianga57d53e2018-03-30 17:14:47 -070077 // Unofficial builds should not perform periodic update checks.
78 &only_update_official_builds_policy,
79
Aaron Wood56d8ab32017-09-22 15:56:18 -070080 // Ensure that periodic update checks are timed properly.
81 &next_update_check_time_policy,
82 };
83
84 // Now that the list of policy implementations, and the order to consult them,
85 // as been setup, do that. If none of the policies make a definitive
86 // decisions about whether or not to check for updates, then allow the update
87 // check to happen.
88 EvalStatus status = ConsultPolicies(policies_to_consult,
89 &Policy::UpdateCheckAllowed,
90 ec,
91 state,
92 error,
93 result);
94 if (status != EvalStatus::kContinue) {
95 return status;
96 } else {
97 // It is time to check for an update.
98 LOG(INFO) << "Allowing update check.";
99 return EvalStatus::kSucceeded;
100 }
101}
102
Aaron Woodbf5a2522017-10-04 10:58:36 -0700103// Uses the |UpdateRestrictions| to determine if the download and apply can
104// occur at this time.
Aaron Wood56d8ab32017-09-22 15:56:18 -0700105EvalStatus AndroidThingsPolicy::UpdateCanBeApplied(
106 EvaluationContext* ec,
107 State* state,
108 string* error,
Aaron Woodbf5a2522017-10-04 10:58:36 -0700109 ErrorCode* result,
Aaron Wood56d8ab32017-09-22 15:56:18 -0700110 chromeos_update_engine::InstallPlan* install_plan) const {
Aaron Woodbf5a2522017-10-04 10:58:36 -0700111 // Build a list of policies to consult. Note that each policy may modify the
112 // result structure, even if it signals kContinue.
113 ApiRestrictedDownloadsPolicyImpl api_restricted_downloads_policy;
114
115 vector<Policy const*> policies_to_consult = {
Aaron Woodbf5a2522017-10-04 10:58:36 -0700116 // Do not apply the update if all updates are restricted by the API.
117 &api_restricted_downloads_policy,
118 };
119
120 // Now that the list of policy implementations, and the order to consult them,
121 // as been setup, do that. If none of the policies make a definitive
122 // decisions about whether or not to check for updates, then allow the update
123 // check to happen.
124 EvalStatus status = ConsultPolicies(policies_to_consult,
125 &Policy::UpdateCanBeApplied,
126 ec,
127 state,
128 error,
129 result,
130 install_plan);
131 if (EvalStatus::kContinue != status) {
132 return status;
133 } else {
134 // The update can proceed.
135 LOG(INFO) << "Allowing update to be applied.";
136 *result = ErrorCode::kSuccess;
137 return EvalStatus::kSucceeded;
138 }
Aaron Wood56d8ab32017-09-22 15:56:18 -0700139}
140
141// Always returns |EvalStatus::kSucceeded|
142EvalStatus AndroidThingsPolicy::UpdateCanStart(EvaluationContext* ec,
143 State* state,
144 string* error,
145 UpdateDownloadParams* result,
146 UpdateState update_state) const {
147 // Update is good to go.
148 result->update_can_start = true;
149 return EvalStatus::kSucceeded;
150}
151
152// Always returns |EvalStatus::kSucceeded|
153EvalStatus AndroidThingsPolicy::UpdateDownloadAllowed(EvaluationContext* ec,
154 State* state,
155 string* error,
156 bool* result) const {
157 // By default, we allow updates.
158 *result = true;
159 return EvalStatus::kSucceeded;
160}
161
162// P2P is always disabled. Returns |result|==|false| and
163// |EvalStatus::kSucceeded|
164EvalStatus AndroidThingsPolicy::P2PEnabled(EvaluationContext* ec,
165 State* state,
166 string* error,
167 bool* result) const {
168 *result = false;
169 return EvalStatus::kSucceeded;
170}
171
172// This will return immediately with |EvalStatus::kSucceeded| and set
173// |result|==|false|
174EvalStatus AndroidThingsPolicy::P2PEnabledChanged(EvaluationContext* ec,
175 State* state,
176 string* error,
177 bool* result,
178 bool prev_result) const {
179 *result = false;
180 return EvalStatus::kSucceeded;
181}
182
183} // namespace chromeos_update_manager