blob: c4fa75a4ff57f6312bfa50dd978b6cb2d9665349 [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
Amin Hassanifbb600f2019-08-14 19:52:30 -070019#include <memory>
Aaron Wood56d8ab32017-09-22 15:56:18 -070020#include <string>
21#include <vector>
22
23#include <base/logging.h>
24#include <base/time/time.h>
25
Aaron Woodbf5a2522017-10-04 10:58:36 -070026#include "update_engine/update_manager/api_restricted_downloads_policy_impl.h"
Aaron Wood56d8ab32017-09-22 15:56:18 -070027#include "update_engine/update_manager/enough_slots_ab_updates_policy_impl.h"
28#include "update_engine/update_manager/interactive_update_policy_impl.h"
29#include "update_engine/update_manager/official_build_check_policy_impl.h"
30
31using base::Time;
32using chromeos_update_engine::ErrorCode;
33using std::string;
Amin Hassanifbb600f2019-08-14 19:52:30 -070034using std::unique_ptr;
Aaron Wood56d8ab32017-09-22 15:56:18 -070035using std::vector;
36
37namespace chromeos_update_manager {
38
Amin Hassanifbb600f2019-08-14 19:52:30 -070039unique_ptr<Policy> GetSystemPolicy() {
40 return std::make_unique<AndroidThingsPolicy>();
41}
42
Aaron Wood56d8ab32017-09-22 15:56:18 -070043const NextUpdateCheckPolicyConstants
44 AndroidThingsPolicy::kNextUpdateCheckPolicyConstants = {
45 .timeout_initial_interval = 7 * 60,
46 .timeout_periodic_interval = 5 * 60 * 60,
47 .timeout_max_backoff_interval = 26 * 60 * 60,
48 .timeout_regular_fuzz = 10 * 60,
49 .attempt_backoff_max_interval_in_days = 16,
50 .attempt_backoff_fuzz_in_hours = 12,
51};
52
53EvalStatus AndroidThingsPolicy::UpdateCheckAllowed(
54 EvaluationContext* ec,
55 State* state,
56 string* error,
57 UpdateCheckParams* result) const {
58 // Set the default return values.
59 result->updates_enabled = true;
60 result->target_channel.clear();
Amin Hassani37b67232020-08-13 09:29:48 -070061 result->lts_tag.clear();
Aaron Wood56d8ab32017-09-22 15:56:18 -070062 result->target_version_prefix.clear();
Marton Hunyadyba51c3f2018-04-25 15:18:10 +020063 result->rollback_allowed = false;
Zentaro Kavanagh28def4f2019-01-15 17:15:01 -080064 result->rollback_data_save_requested = false;
Marton Hunyady0e0e3542018-02-21 18:51:39 +010065 result->rollback_allowed_milestones = -1;
Miriam Polzeraff72002020-08-27 08:20:39 +020066 result->rollback_on_channel_downgrade = false;
Amin Hassanied37d682018-04-06 13:22:00 -070067 result->interactive = false;
Aaron Wood56d8ab32017-09-22 15:56:18 -070068
69 // Build a list of policies to consult. Note that each policy may modify the
70 // result structure, even if it signals kContinue.
71 EnoughSlotsAbUpdatesPolicyImpl enough_slots_ab_updates_policy;
72 OnlyUpdateOfficialBuildsPolicyImpl only_update_official_builds_policy;
73 InteractiveUpdatePolicyImpl interactive_update_policy;
74 NextUpdateCheckTimePolicyImpl next_update_check_time_policy(
75 kNextUpdateCheckPolicyConstants);
76
77 vector<Policy const*> policies_to_consult = {
78 // Do not perform any updates if there are not enough slots to do
79 // A/B updates
80 &enough_slots_ab_updates_policy,
81
Aaron Wood56d8ab32017-09-22 15:56:18 -070082 // Check to see if an interactive update was requested.
83 &interactive_update_policy,
84
Sen Jianga57d53e2018-03-30 17:14:47 -070085 // Unofficial builds should not perform periodic update checks.
86 &only_update_official_builds_policy,
87
Aaron Wood56d8ab32017-09-22 15:56:18 -070088 // Ensure that periodic update checks are timed properly.
89 &next_update_check_time_policy,
90 };
91
92 // Now that the list of policy implementations, and the order to consult them,
93 // as been setup, do that. If none of the policies make a definitive
94 // decisions about whether or not to check for updates, then allow the update
95 // check to happen.
96 EvalStatus status = ConsultPolicies(policies_to_consult,
97 &Policy::UpdateCheckAllowed,
98 ec,
99 state,
100 error,
101 result);
102 if (status != EvalStatus::kContinue) {
103 return status;
104 } else {
105 // It is time to check for an update.
106 LOG(INFO) << "Allowing update check.";
107 return EvalStatus::kSucceeded;
108 }
109}
110
Aaron Woodbf5a2522017-10-04 10:58:36 -0700111// Uses the |UpdateRestrictions| to determine if the download and apply can
112// occur at this time.
Aaron Wood56d8ab32017-09-22 15:56:18 -0700113EvalStatus AndroidThingsPolicy::UpdateCanBeApplied(
114 EvaluationContext* ec,
115 State* state,
116 string* error,
Aaron Woodbf5a2522017-10-04 10:58:36 -0700117 ErrorCode* result,
Aaron Wood56d8ab32017-09-22 15:56:18 -0700118 chromeos_update_engine::InstallPlan* install_plan) const {
Aaron Woodbf5a2522017-10-04 10:58:36 -0700119 // Build a list of policies to consult. Note that each policy may modify the
120 // result structure, even if it signals kContinue.
121 ApiRestrictedDownloadsPolicyImpl api_restricted_downloads_policy;
122
123 vector<Policy const*> policies_to_consult = {
Aaron Woodbf5a2522017-10-04 10:58:36 -0700124 // Do not apply the update if all updates are restricted by the API.
125 &api_restricted_downloads_policy,
126 };
127
128 // Now that the list of policy implementations, and the order to consult them,
129 // as been setup, do that. If none of the policies make a definitive
130 // decisions about whether or not to check for updates, then allow the update
131 // check to happen.
132 EvalStatus status = ConsultPolicies(policies_to_consult,
133 &Policy::UpdateCanBeApplied,
134 ec,
135 state,
136 error,
137 result,
138 install_plan);
139 if (EvalStatus::kContinue != status) {
140 return status;
141 } else {
142 // The update can proceed.
143 LOG(INFO) << "Allowing update to be applied.";
144 *result = ErrorCode::kSuccess;
145 return EvalStatus::kSucceeded;
146 }
Aaron Wood56d8ab32017-09-22 15:56:18 -0700147}
148
149// Always returns |EvalStatus::kSucceeded|
150EvalStatus AndroidThingsPolicy::UpdateCanStart(EvaluationContext* ec,
151 State* state,
152 string* error,
153 UpdateDownloadParams* result,
154 UpdateState update_state) const {
155 // Update is good to go.
156 result->update_can_start = true;
157 return EvalStatus::kSucceeded;
158}
159
160// Always returns |EvalStatus::kSucceeded|
161EvalStatus AndroidThingsPolicy::UpdateDownloadAllowed(EvaluationContext* ec,
162 State* state,
163 string* error,
164 bool* result) const {
165 // By default, we allow updates.
166 *result = true;
167 return EvalStatus::kSucceeded;
168}
169
170// P2P is always disabled. Returns |result|==|false| and
171// |EvalStatus::kSucceeded|
172EvalStatus AndroidThingsPolicy::P2PEnabled(EvaluationContext* ec,
173 State* state,
174 string* error,
175 bool* result) const {
176 *result = false;
177 return EvalStatus::kSucceeded;
178}
179
180// This will return immediately with |EvalStatus::kSucceeded| and set
181// |result|==|false|
182EvalStatus AndroidThingsPolicy::P2PEnabledChanged(EvaluationContext* ec,
183 State* state,
184 string* error,
185 bool* result,
186 bool prev_result) const {
187 *result = false;
188 return EvalStatus::kSucceeded;
189}
190
191} // namespace chromeos_update_manager