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