blob: 6362a73d4ecffc8540d042e652535522569179ea [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;
Amin Hassanied37d682018-04-06 13:22:00 -070066 result->interactive = false;
Aaron Wood56d8ab32017-09-22 15:56:18 -070067
68 // Build a list of policies to consult. Note that each policy may modify the
69 // result structure, even if it signals kContinue.
70 EnoughSlotsAbUpdatesPolicyImpl enough_slots_ab_updates_policy;
71 OnlyUpdateOfficialBuildsPolicyImpl only_update_official_builds_policy;
72 InteractiveUpdatePolicyImpl interactive_update_policy;
73 NextUpdateCheckTimePolicyImpl next_update_check_time_policy(
74 kNextUpdateCheckPolicyConstants);
75
76 vector<Policy const*> policies_to_consult = {
77 // Do not perform any updates if there are not enough slots to do
78 // A/B updates
79 &enough_slots_ab_updates_policy,
80
Aaron Wood56d8ab32017-09-22 15:56:18 -070081 // Check to see if an interactive update was requested.
82 &interactive_update_policy,
83
Sen Jianga57d53e2018-03-30 17:14:47 -070084 // Unofficial builds should not perform periodic update checks.
85 &only_update_official_builds_policy,
86
Aaron Wood56d8ab32017-09-22 15:56:18 -070087 // Ensure that periodic update checks are timed properly.
88 &next_update_check_time_policy,
89 };
90
91 // Now that the list of policy implementations, and the order to consult them,
92 // as been setup, do that. If none of the policies make a definitive
93 // decisions about whether or not to check for updates, then allow the update
94 // check to happen.
95 EvalStatus status = ConsultPolicies(policies_to_consult,
96 &Policy::UpdateCheckAllowed,
97 ec,
98 state,
99 error,
100 result);
101 if (status != EvalStatus::kContinue) {
102 return status;
103 } else {
104 // It is time to check for an update.
105 LOG(INFO) << "Allowing update check.";
106 return EvalStatus::kSucceeded;
107 }
108}
109
Aaron Woodbf5a2522017-10-04 10:58:36 -0700110// Uses the |UpdateRestrictions| to determine if the download and apply can
111// occur at this time.
Aaron Wood56d8ab32017-09-22 15:56:18 -0700112EvalStatus AndroidThingsPolicy::UpdateCanBeApplied(
113 EvaluationContext* ec,
114 State* state,
115 string* error,
Aaron Woodbf5a2522017-10-04 10:58:36 -0700116 ErrorCode* result,
Aaron Wood56d8ab32017-09-22 15:56:18 -0700117 chromeos_update_engine::InstallPlan* install_plan) const {
Aaron Woodbf5a2522017-10-04 10:58:36 -0700118 // Build a list of policies to consult. Note that each policy may modify the
119 // result structure, even if it signals kContinue.
120 ApiRestrictedDownloadsPolicyImpl api_restricted_downloads_policy;
121
122 vector<Policy const*> policies_to_consult = {
Aaron Woodbf5a2522017-10-04 10:58:36 -0700123 // Do not apply the update if all updates are restricted by the API.
124 &api_restricted_downloads_policy,
125 };
126
127 // Now that the list of policy implementations, and the order to consult them,
128 // as been setup, do that. If none of the policies make a definitive
129 // decisions about whether or not to check for updates, then allow the update
130 // check to happen.
131 EvalStatus status = ConsultPolicies(policies_to_consult,
132 &Policy::UpdateCanBeApplied,
133 ec,
134 state,
135 error,
136 result,
137 install_plan);
138 if (EvalStatus::kContinue != status) {
139 return status;
140 } else {
141 // The update can proceed.
142 LOG(INFO) << "Allowing update to be applied.";
143 *result = ErrorCode::kSuccess;
144 return EvalStatus::kSucceeded;
145 }
Aaron Wood56d8ab32017-09-22 15:56:18 -0700146}
147
148// Always returns |EvalStatus::kSucceeded|
149EvalStatus AndroidThingsPolicy::UpdateCanStart(EvaluationContext* ec,
150 State* state,
151 string* error,
152 UpdateDownloadParams* result,
153 UpdateState update_state) const {
154 // Update is good to go.
155 result->update_can_start = true;
156 return EvalStatus::kSucceeded;
157}
158
159// Always returns |EvalStatus::kSucceeded|
160EvalStatus AndroidThingsPolicy::UpdateDownloadAllowed(EvaluationContext* ec,
161 State* state,
162 string* error,
163 bool* result) const {
164 // By default, we allow updates.
165 *result = true;
166 return EvalStatus::kSucceeded;
167}
168
169// P2P is always disabled. Returns |result|==|false| and
170// |EvalStatus::kSucceeded|
171EvalStatus AndroidThingsPolicy::P2PEnabled(EvaluationContext* ec,
172 State* state,
173 string* error,
174 bool* result) const {
175 *result = false;
176 return EvalStatus::kSucceeded;
177}
178
179// This will return immediately with |EvalStatus::kSucceeded| and set
180// |result|==|false|
181EvalStatus AndroidThingsPolicy::P2PEnabledChanged(EvaluationContext* ec,
182 State* state,
183 string* error,
184 bool* result,
185 bool prev_result) const {
186 *result = false;
187 return EvalStatus::kSucceeded;
188}
189
190} // namespace chromeos_update_manager