| Aaron Wood | 56d8ab3 | 2017-09-22 15:56:18 -0700 | [diff] [blame] | 1 | // | 
|  | 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 Hassani | fbb600f | 2019-08-14 19:52:30 -0700 | [diff] [blame] | 19 | #include <memory> | 
| Aaron Wood | 56d8ab3 | 2017-09-22 15:56:18 -0700 | [diff] [blame] | 20 | #include <string> | 
|  | 21 | #include <vector> | 
|  | 22 |  | 
|  | 23 | #include <base/logging.h> | 
|  | 24 | #include <base/time/time.h> | 
|  | 25 |  | 
| Aaron Wood | bf5a252 | 2017-10-04 10:58:36 -0700 | [diff] [blame] | 26 | #include "update_engine/update_manager/api_restricted_downloads_policy_impl.h" | 
| Aaron Wood | 56d8ab3 | 2017-09-22 15:56:18 -0700 | [diff] [blame] | 27 | #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 |  | 
|  | 31 | using base::Time; | 
|  | 32 | using chromeos_update_engine::ErrorCode; | 
|  | 33 | using std::string; | 
| Amin Hassani | fbb600f | 2019-08-14 19:52:30 -0700 | [diff] [blame] | 34 | using std::unique_ptr; | 
| Aaron Wood | 56d8ab3 | 2017-09-22 15:56:18 -0700 | [diff] [blame] | 35 | using std::vector; | 
|  | 36 |  | 
|  | 37 | namespace chromeos_update_manager { | 
|  | 38 |  | 
| Amin Hassani | fbb600f | 2019-08-14 19:52:30 -0700 | [diff] [blame] | 39 | unique_ptr<Policy> GetSystemPolicy() { | 
|  | 40 | return std::make_unique<AndroidThingsPolicy>(); | 
|  | 41 | } | 
|  | 42 |  | 
| Aaron Wood | 56d8ab3 | 2017-09-22 15:56:18 -0700 | [diff] [blame] | 43 | const 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 |  | 
|  | 53 | EvalStatus 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 Hunyady | ba51c3f | 2018-04-25 15:18:10 +0200 | [diff] [blame] | 62 | result->rollback_allowed = false; | 
| Zentaro Kavanagh | 28def4f | 2019-01-15 17:15:01 -0800 | [diff] [blame] | 63 | result->rollback_data_save_requested = false; | 
| Marton Hunyady | 0e0e354 | 2018-02-21 18:51:39 +0100 | [diff] [blame] | 64 | result->rollback_allowed_milestones = -1; | 
| Amin Hassani | ed37d68 | 2018-04-06 13:22:00 -0700 | [diff] [blame] | 65 | result->interactive = false; | 
| Aaron Wood | 56d8ab3 | 2017-09-22 15:56:18 -0700 | [diff] [blame] | 66 |  | 
|  | 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 Wood | 56d8ab3 | 2017-09-22 15:56:18 -0700 | [diff] [blame] | 80 | // Check to see if an interactive update was requested. | 
|  | 81 | &interactive_update_policy, | 
|  | 82 |  | 
| Sen Jiang | a57d53e | 2018-03-30 17:14:47 -0700 | [diff] [blame] | 83 | // Unofficial builds should not perform periodic update checks. | 
|  | 84 | &only_update_official_builds_policy, | 
|  | 85 |  | 
| Aaron Wood | 56d8ab3 | 2017-09-22 15:56:18 -0700 | [diff] [blame] | 86 | // 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 Wood | bf5a252 | 2017-10-04 10:58:36 -0700 | [diff] [blame] | 109 | // Uses the |UpdateRestrictions| to determine if the download and apply can | 
|  | 110 | // occur at this time. | 
| Aaron Wood | 56d8ab3 | 2017-09-22 15:56:18 -0700 | [diff] [blame] | 111 | EvalStatus AndroidThingsPolicy::UpdateCanBeApplied( | 
|  | 112 | EvaluationContext* ec, | 
|  | 113 | State* state, | 
|  | 114 | string* error, | 
| Aaron Wood | bf5a252 | 2017-10-04 10:58:36 -0700 | [diff] [blame] | 115 | ErrorCode* result, | 
| Aaron Wood | 56d8ab3 | 2017-09-22 15:56:18 -0700 | [diff] [blame] | 116 | chromeos_update_engine::InstallPlan* install_plan) const { | 
| Aaron Wood | bf5a252 | 2017-10-04 10:58:36 -0700 | [diff] [blame] | 117 | // 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 Wood | bf5a252 | 2017-10-04 10:58:36 -0700 | [diff] [blame] | 122 | // 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 Wood | 56d8ab3 | 2017-09-22 15:56:18 -0700 | [diff] [blame] | 145 | } | 
|  | 146 |  | 
|  | 147 | // Always returns |EvalStatus::kSucceeded| | 
|  | 148 | EvalStatus 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| | 
|  | 159 | EvalStatus 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| | 
|  | 170 | EvalStatus 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| | 
|  | 180 | EvalStatus 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 |