Home | History | Annotate | Download | only in update_manager
      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 
     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/api_restricted_downloads_policy_impl.h"
     26 #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 
     30 using base::Time;
     31 using chromeos_update_engine::ErrorCode;
     32 using std::string;
     33 using std::vector;
     34 
     35 namespace chromeos_update_manager {
     36 
     37 const 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 
     47 EvalStatus 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 
    100 // Uses the |UpdateRestrictions| to determine if the download and apply can
    101 // occur at this time.
    102 EvalStatus AndroidThingsPolicy::UpdateCanBeApplied(
    103     EvaluationContext* ec,
    104     State* state,
    105     string* error,
    106     ErrorCode* result,
    107     chromeos_update_engine::InstallPlan* install_plan) const {
    108   // 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       // Do not apply the update if all updates are restricted by the API.
    114       &api_restricted_downloads_policy,
    115   };
    116 
    117   // Now that the list of policy implementations, and the order to consult them,
    118   // as been setup, do that.  If none of the policies make a definitive
    119   // decisions about whether or not to check for updates, then allow the update
    120   // check to happen.
    121   EvalStatus status = ConsultPolicies(policies_to_consult,
    122                                       &Policy::UpdateCanBeApplied,
    123                                       ec,
    124                                       state,
    125                                       error,
    126                                       result,
    127                                       install_plan);
    128   if (EvalStatus::kContinue != status) {
    129     return status;
    130   } else {
    131     // The update can proceed.
    132     LOG(INFO) << "Allowing update to be applied.";
    133     *result = ErrorCode::kSuccess;
    134     return EvalStatus::kSucceeded;
    135   }
    136 }
    137 
    138 // Always returns |EvalStatus::kSucceeded|
    139 EvalStatus AndroidThingsPolicy::UpdateCanStart(EvaluationContext* ec,
    140                                                State* state,
    141                                                string* error,
    142                                                UpdateDownloadParams* result,
    143                                                UpdateState update_state) const {
    144   // Update is good to go.
    145   result->update_can_start = true;
    146   return EvalStatus::kSucceeded;
    147 }
    148 
    149 // Always returns |EvalStatus::kSucceeded|
    150 EvalStatus AndroidThingsPolicy::UpdateDownloadAllowed(EvaluationContext* ec,
    151                                                       State* state,
    152                                                       string* error,
    153                                                       bool* result) const {
    154   // By default, we allow updates.
    155   *result = true;
    156   return EvalStatus::kSucceeded;
    157 }
    158 
    159 // P2P is always disabled.  Returns |result|==|false| and
    160 // |EvalStatus::kSucceeded|
    161 EvalStatus AndroidThingsPolicy::P2PEnabled(EvaluationContext* ec,
    162                                            State* state,
    163                                            string* error,
    164                                            bool* result) const {
    165   *result = false;
    166   return EvalStatus::kSucceeded;
    167 }
    168 
    169 // This will return immediately with |EvalStatus::kSucceeded| and set
    170 // |result|==|false|
    171 EvalStatus AndroidThingsPolicy::P2PEnabledChanged(EvaluationContext* ec,
    172                                                   State* state,
    173                                                   string* error,
    174                                                   bool* result,
    175                                                   bool prev_result) const {
    176   *result = false;
    177   return EvalStatus::kSucceeded;
    178 }
    179 
    180 }  // namespace chromeos_update_manager
    181