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