blob: 2304f12ab66f46559bce5d131b19c3a2086cd4e1 [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
25#include "update_engine/update_manager/enough_slots_ab_updates_policy_impl.h"
26#include "update_engine/update_manager/interactive_update_policy_impl.h"
27#include "update_engine/update_manager/official_build_check_policy_impl.h"
28
29using base::Time;
30using chromeos_update_engine::ErrorCode;
31using std::string;
32using std::vector;
33
34namespace chromeos_update_manager {
35
36const NextUpdateCheckPolicyConstants
37 AndroidThingsPolicy::kNextUpdateCheckPolicyConstants = {
38 .timeout_initial_interval = 7 * 60,
39 .timeout_periodic_interval = 5 * 60 * 60,
40 .timeout_max_backoff_interval = 26 * 60 * 60,
41 .timeout_regular_fuzz = 10 * 60,
42 .attempt_backoff_max_interval_in_days = 16,
43 .attempt_backoff_fuzz_in_hours = 12,
44};
45
46EvalStatus AndroidThingsPolicy::UpdateCheckAllowed(
47 EvaluationContext* ec,
48 State* state,
49 string* error,
50 UpdateCheckParams* result) const {
51 // Set the default return values.
52 result->updates_enabled = true;
53 result->target_channel.clear();
54 result->target_version_prefix.clear();
55 result->is_interactive = false;
56
57 // Build a list of policies to consult. Note that each policy may modify the
58 // result structure, even if it signals kContinue.
59 EnoughSlotsAbUpdatesPolicyImpl enough_slots_ab_updates_policy;
60 OnlyUpdateOfficialBuildsPolicyImpl only_update_official_builds_policy;
61 InteractiveUpdatePolicyImpl interactive_update_policy;
62 NextUpdateCheckTimePolicyImpl next_update_check_time_policy(
63 kNextUpdateCheckPolicyConstants);
64
65 vector<Policy const*> policies_to_consult = {
66 // Do not perform any updates if there are not enough slots to do
67 // A/B updates
68 &enough_slots_ab_updates_policy,
69
70 // Unofficial builds should not perform periodic update checks.
71 &only_update_official_builds_policy,
72
73 // Check to see if an interactive update was requested.
74 &interactive_update_policy,
75
76 // Ensure that periodic update checks are timed properly.
77 &next_update_check_time_policy,
78 };
79
80 // Now that the list of policy implementations, and the order to consult them,
81 // as been setup, do that. If none of the policies make a definitive
82 // decisions about whether or not to check for updates, then allow the update
83 // check to happen.
84 EvalStatus status = ConsultPolicies(policies_to_consult,
85 &Policy::UpdateCheckAllowed,
86 ec,
87 state,
88 error,
89 result);
90 if (status != EvalStatus::kContinue) {
91 return status;
92 } else {
93 // It is time to check for an update.
94 LOG(INFO) << "Allowing update check.";
95 return EvalStatus::kSucceeded;
96 }
97}
98
99// Always returns |EvalStatus::kSucceeded|
100EvalStatus AndroidThingsPolicy::UpdateCanBeApplied(
101 EvaluationContext* ec,
102 State* state,
103 string* error,
104 chromeos_update_engine::ErrorCode* result,
105 chromeos_update_engine::InstallPlan* install_plan) const {
106 *result = ErrorCode::kSuccess;
107 return EvalStatus::kSucceeded;
108}
109
110// Always returns |EvalStatus::kSucceeded|
111EvalStatus AndroidThingsPolicy::UpdateCanStart(EvaluationContext* ec,
112 State* state,
113 string* error,
114 UpdateDownloadParams* result,
115 UpdateState update_state) const {
116 // Update is good to go.
117 result->update_can_start = true;
118 return EvalStatus::kSucceeded;
119}
120
121// Always returns |EvalStatus::kSucceeded|
122EvalStatus AndroidThingsPolicy::UpdateDownloadAllowed(EvaluationContext* ec,
123 State* state,
124 string* error,
125 bool* result) const {
126 // By default, we allow updates.
127 *result = true;
128 return EvalStatus::kSucceeded;
129}
130
131// P2P is always disabled. Returns |result|==|false| and
132// |EvalStatus::kSucceeded|
133EvalStatus AndroidThingsPolicy::P2PEnabled(EvaluationContext* ec,
134 State* state,
135 string* error,
136 bool* result) const {
137 *result = false;
138 return EvalStatus::kSucceeded;
139}
140
141// This will return immediately with |EvalStatus::kSucceeded| and set
142// |result|==|false|
143EvalStatus AndroidThingsPolicy::P2PEnabledChanged(EvaluationContext* ec,
144 State* state,
145 string* error,
146 bool* result,
147 bool prev_result) const {
148 *result = false;
149 return EvalStatus::kSucceeded;
150}
151
152} // namespace chromeos_update_manager